Blame view

fs/xfs/xfs_dir2_sf.c 36.7 KB
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1
  /*
7b7187698   Nathan Scott   [XFS] Update lice...
2
3
   * Copyright (c) 2000-2003,2005 Silicon Graphics, Inc.
   * All Rights Reserved.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4
   *
7b7187698   Nathan Scott   [XFS] Update lice...
5
6
   * This program is free software; you can redistribute it and/or
   * modify it under the terms of the GNU General Public License as
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
7
8
   * published by the Free Software Foundation.
   *
7b7187698   Nathan Scott   [XFS] Update lice...
9
10
11
12
   * This program is distributed in the hope that it would be useful,
   * but WITHOUT ANY WARRANTY; without even the implied warranty of
   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   * GNU General Public License for more details.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
13
   *
7b7187698   Nathan Scott   [XFS] Update lice...
14
15
16
   * You should have received a copy of the GNU General Public License
   * along with this program; if not, write the Free Software Foundation,
   * Inc.,  51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
17
   */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
18
  #include "xfs.h"
a844f4510   Nathan Scott   [XFS] Remove xfs_...
19
  #include "xfs_fs.h"
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
20
  #include "xfs_types.h"
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
21
  #include "xfs_log.h"
a844f4510   Nathan Scott   [XFS] Remove xfs_...
22
  #include "xfs_inum.h"
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
23
24
  #include "xfs_trans.h"
  #include "xfs_sb.h"
da353b0d6   David Chinner   [XFS] Radix tree ...
25
  #include "xfs_ag.h"
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
26
27
28
  #include "xfs_dir2.h"
  #include "xfs_dmapi.h"
  #include "xfs_mount.h"
a844f4510   Nathan Scott   [XFS] Remove xfs_...
29
  #include "xfs_da_btree.h"
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
30
  #include "xfs_bmap_btree.h"
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
31
  #include "xfs_dir2_sf.h"
a844f4510   Nathan Scott   [XFS] Remove xfs_...
32
  #include "xfs_attr_sf.h"
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
33
  #include "xfs_dinode.h"
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
34
  #include "xfs_inode.h"
a844f4510   Nathan Scott   [XFS] Remove xfs_...
35
  #include "xfs_inode_item.h"
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
  #include "xfs_error.h"
  #include "xfs_dir2_data.h"
  #include "xfs_dir2_leaf.h"
  #include "xfs_dir2_block.h"
  #include "xfs_dir2_trace.h"
  
  /*
   * Prototypes for internal functions.
   */
  static void xfs_dir2_sf_addname_easy(xfs_da_args_t *args,
  				     xfs_dir2_sf_entry_t *sfep,
  				     xfs_dir2_data_aoff_t offset,
  				     int new_isize);
  static void xfs_dir2_sf_addname_hard(xfs_da_args_t *args, int objchange,
  				     int new_isize);
  static int xfs_dir2_sf_addname_pick(xfs_da_args_t *args, int objchange,
  				    xfs_dir2_sf_entry_t **sfepp,
  				    xfs_dir2_data_aoff_t *offsetp);
  #ifdef DEBUG
  static void xfs_dir2_sf_check(xfs_da_args_t *args);
  #else
  #define	xfs_dir2_sf_check(args)
  #endif /* DEBUG */
  #if XFS_BIG_INUMS
  static void xfs_dir2_sf_toino4(xfs_da_args_t *args);
  static void xfs_dir2_sf_toino8(xfs_da_args_t *args);
  #endif /* XFS_BIG_INUMS */
  
  /*
   * Given a block directory (dp/block), calculate its size as a shortform (sf)
   * directory and a header for the sf directory, if it will fit it the
   * space currently present in the inode.  If it won't fit, the output
   * size is too big (but not accurate).
   */
  int						/* size for sf form */
  xfs_dir2_block_sfsize(
  	xfs_inode_t		*dp,		/* incore inode pointer */
  	xfs_dir2_block_t	*block,		/* block directory data */
  	xfs_dir2_sf_hdr_t	*sfhp)		/* output: header for sf form */
  {
  	xfs_dir2_dataptr_t	addr;		/* data entry address */
  	xfs_dir2_leaf_entry_t	*blp;		/* leaf area of the block */
  	xfs_dir2_block_tail_t	*btp;		/* tail area of the block */
  	int			count;		/* shortform entry count */
  	xfs_dir2_data_entry_t	*dep;		/* data entry in the block */
  	int			i;		/* block entry index */
  	int			i8count;	/* count of big-inode entries */
  	int			isdot;		/* entry is "." */
  	int			isdotdot;	/* entry is ".." */
  	xfs_mount_t		*mp;		/* mount structure pointer */
  	int			namelen;	/* total name bytes */
5bde1ba99   Christoph Hellwig   [XFS] silence gcc...
87
  	xfs_ino_t		parent = 0;	/* parent inode number */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
88
89
90
91
92
  	int			size=0;		/* total computed size */
  
  	mp = dp->i_mount;
  
  	count = i8count = namelen = 0;
bbaaf5380   Christoph Hellwig   [XFS] Reduce shou...
93
94
  	btp = xfs_dir2_block_tail_p(mp, block);
  	blp = xfs_dir2_block_leaf_p(btp);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
95
96
97
98
  
  	/*
  	 * Iterate over the block's data entries by using the leaf pointers.
  	 */
e922fffa4   Nathan Scott   [XFS] endianess a...
99
  	for (i = 0; i < be32_to_cpu(btp->count); i++) {
3c1f9c158   Nathan Scott   [XFS] endianess a...
100
  		if ((addr = be32_to_cpu(blp[i].address)) == XFS_DIR2_NULL_DATAPTR)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
101
102
103
104
105
  			continue;
  		/*
  		 * Calculate the pointer to the entry at hand.
  		 */
  		dep = (xfs_dir2_data_entry_t *)
bbaaf5380   Christoph Hellwig   [XFS] Reduce shou...
106
  		      ((char *)block + xfs_dir2_dataptr_to_off(mp, addr));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
107
108
109
110
111
112
113
114
115
116
117
  		/*
  		 * Detect . and .., so we can special-case them.
  		 * . is not included in sf directories.
  		 * .. is included by just the parent inode number.
  		 */
  		isdot = dep->namelen == 1 && dep->name[0] == '.';
  		isdotdot =
  			dep->namelen == 2 &&
  			dep->name[0] == '.' && dep->name[1] == '.';
  #if XFS_BIG_INUMS
  		if (!isdot)
ff9901c1e   Christoph Hellwig   [XFS] endianess a...
118
  			i8count += be64_to_cpu(dep->inumber) > XFS_DIR2_MAX_SHORT_INUM;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
119
120
121
122
123
  #endif
  		if (!isdot && !isdotdot) {
  			count++;
  			namelen += dep->namelen;
  		} else if (isdotdot)
ff9901c1e   Christoph Hellwig   [XFS] endianess a...
124
  			parent = be64_to_cpu(dep->inumber);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
125
126
127
  		/*
  		 * Calculate the new size, see if we should give up yet.
  		 */
bbaaf5380   Christoph Hellwig   [XFS] Reduce shou...
128
  		size = xfs_dir2_sf_hdr_size(i8count) +		/* header */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
129
130
131
132
133
134
135
136
137
138
139
140
141
142
  		       count +					/* namelen */
  		       count * (uint)sizeof(xfs_dir2_sf_off_t) + /* offset */
  		       namelen +				/* name */
  		       (i8count ?				/* inumber */
  				(uint)sizeof(xfs_dir2_ino8_t) * count :
  				(uint)sizeof(xfs_dir2_ino4_t) * count);
  		if (size > XFS_IFORK_DSIZE(dp))
  			return size;		/* size value is a failure */
  	}
  	/*
  	 * Create the output header, if it worked.
  	 */
  	sfhp->count = count;
  	sfhp->i8count = i8count;
bbaaf5380   Christoph Hellwig   [XFS] Reduce shou...
143
  	xfs_dir2_sf_put_inumber((xfs_dir2_sf_t *)sfhp, &parent, &sfhp->parent);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
  	return size;
  }
  
  /*
   * Convert a block format directory to shortform.
   * Caller has already checked that it will fit, and built us a header.
   */
  int						/* error */
  xfs_dir2_block_to_sf(
  	xfs_da_args_t		*args,		/* operation arguments */
  	xfs_dabuf_t		*bp,		/* block buffer */
  	int			size,		/* shortform directory size */
  	xfs_dir2_sf_hdr_t	*sfhp)		/* shortform directory hdr */
  {
  	xfs_dir2_block_t	*block;		/* block structure */
  	xfs_dir2_block_tail_t	*btp;		/* block tail pointer */
  	xfs_dir2_data_entry_t	*dep;		/* data entry pointer */
  	xfs_inode_t		*dp;		/* incore directory inode */
  	xfs_dir2_data_unused_t	*dup;		/* unused data pointer */
  	char			*endptr;	/* end of data entries */
  	int			error;		/* error return value */
  	int			logflags;	/* inode logging flags */
  	xfs_mount_t		*mp;		/* filesystem mount point */
  	char			*ptr;		/* current data pointer */
  	xfs_dir2_sf_entry_t	*sfep;		/* shortform entry */
  	xfs_dir2_sf_t		*sfp;		/* shortform structure */
  	xfs_ino_t               temp;
  
  	xfs_dir2_trace_args_sb("block_to_sf", args, size, bp);
  	dp = args->dp;
  	mp = dp->i_mount;
  
  	/*
  	 * Make a copy of the block data, so we can shrink the inode
  	 * and add local data.
  	 */
  	block = kmem_alloc(mp->m_dirblksize, KM_SLEEP);
  	memcpy(block, bp->data, mp->m_dirblksize);
  	logflags = XFS_ILOG_CORE;
  	if ((error = xfs_dir2_shrink_inode(args, mp->m_dirdatablk, bp))) {
  		ASSERT(error != ENOSPC);
  		goto out;
  	}
  	/*
  	 * The buffer is now unconditionally gone, whether
  	 * xfs_dir2_shrink_inode worked or not.
  	 *
  	 * Convert the inode to local format.
  	 */
  	dp->i_df.if_flags &= ~XFS_IFEXTENTS;
  	dp->i_df.if_flags |= XFS_IFINLINE;
  	dp->i_d.di_format = XFS_DINODE_FMT_LOCAL;
  	ASSERT(dp->i_df.if_bytes == 0);
  	xfs_idata_realloc(dp, size, XFS_DATA_FORK);
  	logflags |= XFS_ILOG_DDATA;
  	/*
  	 * Copy the header into the newly allocate local space.
  	 */
  	sfp = (xfs_dir2_sf_t *)dp->i_df.if_u1.if_data;
bbaaf5380   Christoph Hellwig   [XFS] Reduce shou...
203
  	memcpy(sfp, sfhp, xfs_dir2_sf_hdr_size(sfhp->i8count));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
204
205
206
207
  	dp->i_d.di_size = size;
  	/*
  	 * Set up to loop over the block's entries.
  	 */
bbaaf5380   Christoph Hellwig   [XFS] Reduce shou...
208
  	btp = xfs_dir2_block_tail_p(mp, block);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
209
  	ptr = (char *)block->u;
bbaaf5380   Christoph Hellwig   [XFS] Reduce shou...
210
211
  	endptr = (char *)xfs_dir2_block_leaf_p(btp);
  	sfep = xfs_dir2_sf_firstentry(sfp);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
212
213
214
215
216
217
218
219
220
  	/*
  	 * Loop over the active and unused entries.
  	 * Stop when we reach the leaf/tail portion of the block.
  	 */
  	while (ptr < endptr) {
  		/*
  		 * If it's unused, just skip over it.
  		 */
  		dup = (xfs_dir2_data_unused_t *)ptr;
ad354eb34   Nathan Scott   [XFS] endianess a...
221
222
  		if (be16_to_cpu(dup->freetag) == XFS_DIR2_DATA_FREE_TAG) {
  			ptr += be16_to_cpu(dup->length);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
223
224
225
226
227
228
229
  			continue;
  		}
  		dep = (xfs_dir2_data_entry_t *)ptr;
  		/*
  		 * Skip .
  		 */
  		if (dep->namelen == 1 && dep->name[0] == '.')
ff9901c1e   Christoph Hellwig   [XFS] endianess a...
230
  			ASSERT(be64_to_cpu(dep->inumber) == dp->i_ino);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
231
232
233
234
235
  		/*
  		 * Skip .., but make sure the inode number is right.
  		 */
  		else if (dep->namelen == 2 &&
  			 dep->name[0] == '.' && dep->name[1] == '.')
ff9901c1e   Christoph Hellwig   [XFS] endianess a...
236
  			ASSERT(be64_to_cpu(dep->inumber) ==
bbaaf5380   Christoph Hellwig   [XFS] Reduce shou...
237
  			       xfs_dir2_sf_get_inumber(sfp, &sfp->hdr.parent));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
238
239
240
241
242
  		/*
  		 * Normal entry, copy it into shortform.
  		 */
  		else {
  			sfep->namelen = dep->namelen;
bbaaf5380   Christoph Hellwig   [XFS] Reduce shou...
243
  			xfs_dir2_sf_put_offset(sfep,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
244
245
246
  				(xfs_dir2_data_aoff_t)
  				((char *)dep - (char *)block));
  			memcpy(sfep->name, dep->name, dep->namelen);
ff9901c1e   Christoph Hellwig   [XFS] endianess a...
247
  			temp = be64_to_cpu(dep->inumber);
bbaaf5380   Christoph Hellwig   [XFS] Reduce shou...
248
249
250
  			xfs_dir2_sf_put_inumber(sfp, &temp,
  				xfs_dir2_sf_inumberp(sfep));
  			sfep = xfs_dir2_sf_nextentry(sfp, sfep);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
251
  		}
bbaaf5380   Christoph Hellwig   [XFS] Reduce shou...
252
  		ptr += xfs_dir2_data_entsize(dep->namelen);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
253
254
255
256
257
  	}
  	ASSERT((char *)sfep - (char *)sfp == size);
  	xfs_dir2_sf_check(args);
  out:
  	xfs_trans_log_inode(args->trans, dp, logflags);
f0e2d93c2   Denys Vlasenko   [XFS] Remove unus...
258
  	kmem_free(block);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
  	return error;
  }
  
  /*
   * Add a name to a shortform directory.
   * There are two algorithms, "easy" and "hard" which we decide on
   * before changing anything.
   * Convert to block form if necessary, if the new entry won't fit.
   */
  int						/* error */
  xfs_dir2_sf_addname(
  	xfs_da_args_t		*args)		/* operation arguments */
  {
  	int			add_entsize;	/* size of the new entry */
  	xfs_inode_t		*dp;		/* incore directory inode */
  	int			error;		/* error return value */
  	int			incr_isize;	/* total change in size */
  	int			new_isize;	/* di_size after adding name */
  	int			objchange;	/* changing to 8-byte inodes */
5bde1ba99   Christoph Hellwig   [XFS] silence gcc...
278
  	xfs_dir2_data_aoff_t	offset = 0;	/* offset for new entry */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
279
280
281
  	int			old_isize;	/* di_size before adding name */
  	int			pick;		/* which algorithm to use */
  	xfs_dir2_sf_t		*sfp;		/* shortform structure */
5bde1ba99   Christoph Hellwig   [XFS] silence gcc...
282
  	xfs_dir2_sf_entry_t	*sfep = NULL;	/* shortform entry */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
  
  	xfs_dir2_trace_args("sf_addname", args);
  	ASSERT(xfs_dir2_sf_lookup(args) == ENOENT);
  	dp = args->dp;
  	ASSERT(dp->i_df.if_flags & XFS_IFINLINE);
  	/*
  	 * Make sure the shortform value has some of its header.
  	 */
  	if (dp->i_d.di_size < offsetof(xfs_dir2_sf_hdr_t, parent)) {
  		ASSERT(XFS_FORCED_SHUTDOWN(dp->i_mount));
  		return XFS_ERROR(EIO);
  	}
  	ASSERT(dp->i_df.if_bytes == dp->i_d.di_size);
  	ASSERT(dp->i_df.if_u1.if_data != NULL);
  	sfp = (xfs_dir2_sf_t *)dp->i_df.if_u1.if_data;
bbaaf5380   Christoph Hellwig   [XFS] Reduce shou...
298
  	ASSERT(dp->i_d.di_size >= xfs_dir2_sf_hdr_size(sfp->hdr.i8count));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
299
300
301
  	/*
  	 * Compute entry (and change in) size.
  	 */
bbaaf5380   Christoph Hellwig   [XFS] Reduce shou...
302
  	add_entsize = xfs_dir2_sf_entsize_byname(sfp, args->namelen);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
  	incr_isize = add_entsize;
  	objchange = 0;
  #if XFS_BIG_INUMS
  	/*
  	 * Do we have to change to 8 byte inodes?
  	 */
  	if (args->inumber > XFS_DIR2_MAX_SHORT_INUM && sfp->hdr.i8count == 0) {
  		/*
  		 * Yes, adjust the entry size and the total size.
  		 */
  		add_entsize +=
  			(uint)sizeof(xfs_dir2_ino8_t) -
  			(uint)sizeof(xfs_dir2_ino4_t);
  		incr_isize +=
  			(sfp->hdr.count + 2) *
  			((uint)sizeof(xfs_dir2_ino8_t) -
  			 (uint)sizeof(xfs_dir2_ino4_t));
  		objchange = 1;
  	}
  #endif
  	old_isize = (int)dp->i_d.di_size;
  	new_isize = old_isize + incr_isize;
  	/*
  	 * Won't fit as shortform any more (due to size),
  	 * or the pick routine says it won't (due to offset values).
  	 */
  	if (new_isize > XFS_IFORK_DSIZE(dp) ||
  	    (pick =
  	     xfs_dir2_sf_addname_pick(args, objchange, &sfep, &offset)) == 0) {
  		/*
  		 * Just checking or no space reservation, it doesn't fit.
  		 */
6a178100a   Barry Naujok   [XFS] Add op_flag...
335
  		if ((args->op_flags & XFS_DA_OP_JUSTCHECK) || args->total == 0)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
336
337
338
339
340
341
342
343
344
345
346
347
  			return XFS_ERROR(ENOSPC);
  		/*
  		 * Convert to block form then add the name.
  		 */
  		error = xfs_dir2_sf_to_block(args);
  		if (error)
  			return error;
  		return xfs_dir2_block_addname(args);
  	}
  	/*
  	 * Just checking, it fits.
  	 */
6a178100a   Barry Naujok   [XFS] Add op_flag...
348
  	if (args->op_flags & XFS_DA_OP_JUSTCHECK)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
  		return 0;
  	/*
  	 * Do it the easy way - just add it at the end.
  	 */
  	if (pick == 1)
  		xfs_dir2_sf_addname_easy(args, sfep, offset, new_isize);
  	/*
  	 * Do it the hard way - look for a place to insert the new entry.
  	 * Convert to 8 byte inode numbers first if necessary.
  	 */
  	else {
  		ASSERT(pick == 2);
  #if XFS_BIG_INUMS
  		if (objchange)
  			xfs_dir2_sf_toino8(args);
  #endif
  		xfs_dir2_sf_addname_hard(args, objchange, new_isize);
  	}
  	xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE | XFS_ILOG_DDATA);
  	return 0;
  }
  
  /*
   * Add the new entry the "easy" way.
   * This is copying the old directory and adding the new entry at the end.
   * Since it's sorted by "offset" we need room after the last offset
   * that's already there, and then room to convert to a block directory.
   * This is already checked by the pick routine.
   */
  static void
  xfs_dir2_sf_addname_easy(
  	xfs_da_args_t		*args,		/* operation arguments */
  	xfs_dir2_sf_entry_t	*sfep,		/* pointer to new entry */
  	xfs_dir2_data_aoff_t	offset,		/* offset to use for new ent */
  	int			new_isize)	/* new directory size */
  {
  	int			byteoff;	/* byte offset in sf dir */
  	xfs_inode_t		*dp;		/* incore directory inode */
  	xfs_dir2_sf_t		*sfp;		/* shortform structure */
  
  	dp = args->dp;
  
  	sfp = (xfs_dir2_sf_t *)dp->i_df.if_u1.if_data;
  	byteoff = (int)((char *)sfep - (char *)sfp);
  	/*
  	 * Grow the in-inode space.
  	 */
bbaaf5380   Christoph Hellwig   [XFS] Reduce shou...
396
  	xfs_idata_realloc(dp, xfs_dir2_sf_entsize_byname(sfp, args->namelen),
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
397
398
399
400
401
402
403
404
405
406
  		XFS_DATA_FORK);
  	/*
  	 * Need to set up again due to realloc of the inode data.
  	 */
  	sfp = (xfs_dir2_sf_t *)dp->i_df.if_u1.if_data;
  	sfep = (xfs_dir2_sf_entry_t *)((char *)sfp + byteoff);
  	/*
  	 * Fill in the new entry.
  	 */
  	sfep->namelen = args->namelen;
bbaaf5380   Christoph Hellwig   [XFS] Reduce shou...
407
  	xfs_dir2_sf_put_offset(sfep, offset);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
408
  	memcpy(sfep->name, args->name, sfep->namelen);
bbaaf5380   Christoph Hellwig   [XFS] Reduce shou...
409
410
  	xfs_dir2_sf_put_inumber(sfp, &args->inumber,
  		xfs_dir2_sf_inumberp(sfep));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
  	/*
  	 * Update the header and inode.
  	 */
  	sfp->hdr.count++;
  #if XFS_BIG_INUMS
  	if (args->inumber > XFS_DIR2_MAX_SHORT_INUM)
  		sfp->hdr.i8count++;
  #endif
  	dp->i_d.di_size = new_isize;
  	xfs_dir2_sf_check(args);
  }
  
  /*
   * Add the new entry the "hard" way.
   * The caller has already converted to 8 byte inode numbers if necessary,
   * in which case we need to leave the i8count at 1.
   * Find a hole that the new entry will fit into, and copy
   * the first part of the entries, the new entry, and the last part of
   * the entries.
   */
  /* ARGSUSED */
  static void
  xfs_dir2_sf_addname_hard(
  	xfs_da_args_t		*args,		/* operation arguments */
  	int			objchange,	/* changing inode number size */
  	int			new_isize)	/* new directory size */
  {
  	int			add_datasize;	/* data size need for new ent */
  	char			*buf;		/* buffer for old */
  	xfs_inode_t		*dp;		/* incore directory inode */
  	int			eof;		/* reached end of old dir */
  	int			nbytes;		/* temp for byte copies */
  	xfs_dir2_data_aoff_t	new_offset;	/* next offset value */
  	xfs_dir2_data_aoff_t	offset;		/* current offset value */
  	int			old_isize;	/* previous di_size */
  	xfs_dir2_sf_entry_t	*oldsfep;	/* entry in original dir */
  	xfs_dir2_sf_t		*oldsfp;	/* original shortform dir */
  	xfs_dir2_sf_entry_t	*sfep;		/* entry in new dir */
  	xfs_dir2_sf_t		*sfp;		/* new shortform dir */
  
  	/*
  	 * Copy the old directory to the stack buffer.
  	 */
  	dp = args->dp;
  
  	sfp = (xfs_dir2_sf_t *)dp->i_df.if_u1.if_data;
  	old_isize = (int)dp->i_d.di_size;
  	buf = kmem_alloc(old_isize, KM_SLEEP);
  	oldsfp = (xfs_dir2_sf_t *)buf;
  	memcpy(oldsfp, sfp, old_isize);
  	/*
  	 * Loop over the old directory finding the place we're going
  	 * to insert the new entry.
  	 * If it's going to end up at the end then oldsfep will point there.
  	 */
  	for (offset = XFS_DIR2_DATA_FIRST_OFFSET,
bbaaf5380   Christoph Hellwig   [XFS] Reduce shou...
467
468
  	      oldsfep = xfs_dir2_sf_firstentry(oldsfp),
  	      add_datasize = xfs_dir2_data_entsize(args->namelen),
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
469
470
  	      eof = (char *)oldsfep == &buf[old_isize];
  	     !eof;
bbaaf5380   Christoph Hellwig   [XFS] Reduce shou...
471
472
  	     offset = new_offset + xfs_dir2_data_entsize(oldsfep->namelen),
  	      oldsfep = xfs_dir2_sf_nextentry(oldsfp, oldsfep),
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
473
  	      eof = (char *)oldsfep == &buf[old_isize]) {
bbaaf5380   Christoph Hellwig   [XFS] Reduce shou...
474
  		new_offset = xfs_dir2_sf_get_offset(oldsfep);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
  		if (offset + add_datasize <= new_offset)
  			break;
  	}
  	/*
  	 * Get rid of the old directory, then allocate space for
  	 * the new one.  We do this so xfs_idata_realloc won't copy
  	 * the data.
  	 */
  	xfs_idata_realloc(dp, -old_isize, XFS_DATA_FORK);
  	xfs_idata_realloc(dp, new_isize, XFS_DATA_FORK);
  	/*
  	 * Reset the pointer since the buffer was reallocated.
  	 */
  	sfp = (xfs_dir2_sf_t *)dp->i_df.if_u1.if_data;
  	/*
  	 * Copy the first part of the directory, including the header.
  	 */
  	nbytes = (int)((char *)oldsfep - (char *)oldsfp);
  	memcpy(sfp, oldsfp, nbytes);
  	sfep = (xfs_dir2_sf_entry_t *)((char *)sfp + nbytes);
  	/*
  	 * Fill in the new entry, and update the header counts.
  	 */
  	sfep->namelen = args->namelen;
bbaaf5380   Christoph Hellwig   [XFS] Reduce shou...
499
  	xfs_dir2_sf_put_offset(sfep, offset);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
500
  	memcpy(sfep->name, args->name, sfep->namelen);
bbaaf5380   Christoph Hellwig   [XFS] Reduce shou...
501
502
  	xfs_dir2_sf_put_inumber(sfp, &args->inumber,
  		xfs_dir2_sf_inumberp(sfep));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
503
504
505
506
507
508
509
510
511
  	sfp->hdr.count++;
  #if XFS_BIG_INUMS
  	if (args->inumber > XFS_DIR2_MAX_SHORT_INUM && !objchange)
  		sfp->hdr.i8count++;
  #endif
  	/*
  	 * If there's more left to copy, do that.
  	 */
  	if (!eof) {
bbaaf5380   Christoph Hellwig   [XFS] Reduce shou...
512
  		sfep = xfs_dir2_sf_nextentry(sfp, sfep);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
513
514
  		memcpy(sfep, oldsfep, old_isize - nbytes);
  	}
f0e2d93c2   Denys Vlasenko   [XFS] Remove unus...
515
  	kmem_free(buf);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
  	dp->i_d.di_size = new_isize;
  	xfs_dir2_sf_check(args);
  }
  
  /*
   * Decide if the new entry will fit at all.
   * If it will fit, pick between adding the new entry to the end (easy)
   * or somewhere else (hard).
   * Return 0 (won't fit), 1 (easy), 2 (hard).
   */
  /*ARGSUSED*/
  static int					/* pick result */
  xfs_dir2_sf_addname_pick(
  	xfs_da_args_t		*args,		/* operation arguments */
  	int			objchange,	/* inode # size changes */
  	xfs_dir2_sf_entry_t	**sfepp,	/* out(1): new entry ptr */
  	xfs_dir2_data_aoff_t	*offsetp)	/* out(1): new offset */
  {
  	xfs_inode_t		*dp;		/* incore directory inode */
  	int			holefit;	/* found hole it will fit in */
  	int			i;		/* entry number */
  	xfs_mount_t		*mp;		/* filesystem mount point */
  	xfs_dir2_data_aoff_t	offset;		/* data block offset */
  	xfs_dir2_sf_entry_t	*sfep;		/* shortform entry */
  	xfs_dir2_sf_t		*sfp;		/* shortform structure */
  	int			size;		/* entry's data size */
  	int			used;		/* data bytes used */
  
  	dp = args->dp;
  	mp = dp->i_mount;
  
  	sfp = (xfs_dir2_sf_t *)dp->i_df.if_u1.if_data;
bbaaf5380   Christoph Hellwig   [XFS] Reduce shou...
548
  	size = xfs_dir2_data_entsize(args->namelen);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
549
  	offset = XFS_DIR2_DATA_FIRST_OFFSET;
bbaaf5380   Christoph Hellwig   [XFS] Reduce shou...
550
  	sfep = xfs_dir2_sf_firstentry(sfp);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
551
552
553
554
555
556
557
558
  	holefit = 0;
  	/*
  	 * Loop over sf entries.
  	 * Keep track of data offset and whether we've seen a place
  	 * to insert the new entry.
  	 */
  	for (i = 0; i < sfp->hdr.count; i++) {
  		if (!holefit)
bbaaf5380   Christoph Hellwig   [XFS] Reduce shou...
559
560
561
562
  			holefit = offset + size <= xfs_dir2_sf_get_offset(sfep);
  		offset = xfs_dir2_sf_get_offset(sfep) +
  			 xfs_dir2_data_entsize(sfep->namelen);
  		sfep = xfs_dir2_sf_nextentry(sfp, sfep);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
  	}
  	/*
  	 * Calculate data bytes used excluding the new entry, if this
  	 * was a data block (block form directory).
  	 */
  	used = offset +
  	       (sfp->hdr.count + 3) * (uint)sizeof(xfs_dir2_leaf_entry_t) +
  	       (uint)sizeof(xfs_dir2_block_tail_t);
  	/*
  	 * If it won't fit in a block form then we can't insert it,
  	 * we'll go back, convert to block, then try the insert and convert
  	 * to leaf.
  	 */
  	if (used + (holefit ? 0 : size) > mp->m_dirblksize)
  		return 0;
  	/*
  	 * If changing the inode number size, do it the hard way.
  	 */
  #if XFS_BIG_INUMS
  	if (objchange) {
  		return 2;
  	}
  #else
  	ASSERT(objchange == 0);
  #endif
  	/*
  	 * If it won't fit at the end then do it the hard way (use the hole).
  	 */
  	if (used + size > mp->m_dirblksize)
  		return 2;
  	/*
  	 * Do it the easy way.
  	 */
  	*sfepp = sfep;
  	*offsetp = offset;
  	return 1;
  }
  
  #ifdef DEBUG
  /*
   * Check consistency of shortform directory, assert if bad.
   */
  static void
  xfs_dir2_sf_check(
  	xfs_da_args_t		*args)		/* operation arguments */
  {
  	xfs_inode_t		*dp;		/* incore directory inode */
  	int			i;		/* entry number */
  	int			i8count;	/* number of big inode#s */
  	xfs_ino_t		ino;		/* entry inode number */
  	int			offset;		/* data offset */
  	xfs_dir2_sf_entry_t	*sfep;		/* shortform dir entry */
  	xfs_dir2_sf_t		*sfp;		/* shortform structure */
  
  	dp = args->dp;
  
  	sfp = (xfs_dir2_sf_t *)dp->i_df.if_u1.if_data;
  	offset = XFS_DIR2_DATA_FIRST_OFFSET;
bbaaf5380   Christoph Hellwig   [XFS] Reduce shou...
621
  	ino = xfs_dir2_sf_get_inumber(sfp, &sfp->hdr.parent);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
622
  	i8count = ino > XFS_DIR2_MAX_SHORT_INUM;
bbaaf5380   Christoph Hellwig   [XFS] Reduce shou...
623
  	for (i = 0, sfep = xfs_dir2_sf_firstentry(sfp);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
624
  	     i < sfp->hdr.count;
bbaaf5380   Christoph Hellwig   [XFS] Reduce shou...
625
626
627
  	     i++, sfep = xfs_dir2_sf_nextentry(sfp, sfep)) {
  		ASSERT(xfs_dir2_sf_get_offset(sfep) >= offset);
  		ino = xfs_dir2_sf_get_inumber(sfp, xfs_dir2_sf_inumberp(sfep));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
628
629
  		i8count += ino > XFS_DIR2_MAX_SHORT_INUM;
  		offset =
bbaaf5380   Christoph Hellwig   [XFS] Reduce shou...
630
631
  			xfs_dir2_sf_get_offset(sfep) +
  			xfs_dir2_data_entsize(sfep->namelen);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
  	}
  	ASSERT(i8count == sfp->hdr.i8count);
  	ASSERT(XFS_BIG_INUMS || i8count == 0);
  	ASSERT((char *)sfep - (char *)sfp == dp->i_d.di_size);
  	ASSERT(offset +
  	       (sfp->hdr.count + 2) * (uint)sizeof(xfs_dir2_leaf_entry_t) +
  	       (uint)sizeof(xfs_dir2_block_tail_t) <=
  	       dp->i_mount->m_dirblksize);
  }
  #endif	/* DEBUG */
  
  /*
   * Create a new (shortform) directory.
   */
  int					/* error, always 0 */
  xfs_dir2_sf_create(
  	xfs_da_args_t	*args,		/* operation arguments */
  	xfs_ino_t	pino)		/* parent inode number */
  {
  	xfs_inode_t	*dp;		/* incore directory inode */
  	int		i8count;	/* parent inode is an 8-byte number */
  	xfs_dir2_sf_t	*sfp;		/* shortform structure */
  	int		size;		/* directory size */
  
  	xfs_dir2_trace_args_i("sf_create", args, pino);
  	dp = args->dp;
  
  	ASSERT(dp != NULL);
  	ASSERT(dp->i_d.di_size == 0);
  	/*
  	 * If it's currently a zero-length extent file,
  	 * convert it to local format.
  	 */
  	if (dp->i_d.di_format == XFS_DINODE_FMT_EXTENTS) {
  		dp->i_df.if_flags &= ~XFS_IFEXTENTS;	/* just in case */
  		dp->i_d.di_format = XFS_DINODE_FMT_LOCAL;
  		xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE);
  		dp->i_df.if_flags |= XFS_IFINLINE;
  	}
  	ASSERT(dp->i_df.if_flags & XFS_IFINLINE);
  	ASSERT(dp->i_df.if_bytes == 0);
  	i8count = pino > XFS_DIR2_MAX_SHORT_INUM;
bbaaf5380   Christoph Hellwig   [XFS] Reduce shou...
674
  	size = xfs_dir2_sf_hdr_size(i8count);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
675
676
677
678
679
680
681
682
683
684
685
686
  	/*
  	 * Make a buffer for the data.
  	 */
  	xfs_idata_realloc(dp, size, XFS_DATA_FORK);
  	/*
  	 * Fill in the header,
  	 */
  	sfp = (xfs_dir2_sf_t *)dp->i_df.if_u1.if_data;
  	sfp->hdr.i8count = i8count;
  	/*
  	 * Now can put in the inode number, since i8count is set.
  	 */
bbaaf5380   Christoph Hellwig   [XFS] Reduce shou...
687
  	xfs_dir2_sf_put_inumber(sfp, &pino, &sfp->hdr.parent);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
688
689
690
691
692
693
694
695
696
697
  	sfp->hdr.count = 0;
  	dp->i_d.di_size = size;
  	xfs_dir2_sf_check(args);
  	xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE | XFS_ILOG_DDATA);
  	return 0;
  }
  
  int						/* error */
  xfs_dir2_sf_getdents(
  	xfs_inode_t		*dp,		/* incore directory inode */
051e7cd44   Christoph Hellwig   [XFS] use filldir...
698
699
700
  	void			*dirent,
  	xfs_off_t		*offset,
  	filldir_t		filldir)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
701
  {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
702
703
704
  	int			i;		/* shortform entry number */
  	xfs_mount_t		*mp;		/* filesystem mount point */
  	xfs_dir2_dataptr_t	off;		/* current entry's offset */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
705
706
  	xfs_dir2_sf_entry_t	*sfep;		/* shortform directory entry */
  	xfs_dir2_sf_t		*sfp;		/* shortform structure */
051e7cd44   Christoph Hellwig   [XFS] use filldir...
707
708
709
  	xfs_dir2_dataptr_t	dot_offset;
  	xfs_dir2_dataptr_t	dotdot_offset;
  	xfs_ino_t		ino;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
710
711
712
713
714
715
716
717
718
719
720
  
  	mp = dp->i_mount;
  
  	ASSERT(dp->i_df.if_flags & XFS_IFINLINE);
  	/*
  	 * Give up if the directory is way too short.
  	 */
  	if (dp->i_d.di_size < offsetof(xfs_dir2_sf_hdr_t, parent)) {
  		ASSERT(XFS_FORCED_SHUTDOWN(mp));
  		return XFS_ERROR(EIO);
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
721
722
723
724
  	ASSERT(dp->i_df.if_bytes == dp->i_d.di_size);
  	ASSERT(dp->i_df.if_u1.if_data != NULL);
  
  	sfp = (xfs_dir2_sf_t *)dp->i_df.if_u1.if_data;
bbaaf5380   Christoph Hellwig   [XFS] Reduce shou...
725
  	ASSERT(dp->i_d.di_size >= xfs_dir2_sf_hdr_size(sfp->hdr.i8count));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
726
727
728
729
  
  	/*
  	 * If the block number in the offset is out of range, we're done.
  	 */
051e7cd44   Christoph Hellwig   [XFS] use filldir...
730
  	if (xfs_dir2_dataptr_to_db(mp, *offset) > mp->m_dirdatablk)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
731
  		return 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
732
733
  
  	/*
051e7cd44   Christoph Hellwig   [XFS] use filldir...
734
735
736
737
  	 * Precalculate offsets for . and .. as we will always need them.
  	 *
  	 * XXX(hch): the second argument is sometimes 0 and sometimes
  	 * mp->m_dirdatablk.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
738
  	 */
051e7cd44   Christoph Hellwig   [XFS] use filldir...
739
740
741
742
  	dot_offset = xfs_dir2_db_off_to_dataptr(mp, mp->m_dirdatablk,
  					     XFS_DIR2_DATA_DOT_OFFSET);
  	dotdot_offset = xfs_dir2_db_off_to_dataptr(mp, mp->m_dirdatablk,
  						XFS_DIR2_DATA_DOTDOT_OFFSET);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
743
744
745
  	/*
  	 * Put . entry unless we're starting past it.
  	 */
051e7cd44   Christoph Hellwig   [XFS] use filldir...
746
  	if (*offset <= dot_offset) {
a19d9f887   Christoph Hellwig   xfs: kill ino64 m...
747
  		if (filldir(dirent, ".", 1, dot_offset & 0x7fffffff, dp->i_ino, DT_DIR)) {
154403197   Christoph Hellwig   [XFS] truncate re...
748
  			*offset = dot_offset & 0x7fffffff;
051e7cd44   Christoph Hellwig   [XFS] use filldir...
749
  			return 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
750
751
752
753
754
755
  		}
  	}
  
  	/*
  	 * Put .. entry unless we're starting past it.
  	 */
051e7cd44   Christoph Hellwig   [XFS] use filldir...
756
  	if (*offset <= dotdot_offset) {
051e7cd44   Christoph Hellwig   [XFS] use filldir...
757
  		ino = xfs_dir2_sf_get_inumber(sfp, &sfp->hdr.parent);
154403197   Christoph Hellwig   [XFS] truncate re...
758
759
  		if (filldir(dirent, "..", 2, dotdot_offset & 0x7fffffff, ino, DT_DIR)) {
  			*offset = dotdot_offset & 0x7fffffff;
051e7cd44   Christoph Hellwig   [XFS] use filldir...
760
  			return 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
761
762
763
764
765
766
  		}
  	}
  
  	/*
  	 * Loop while there are more entries and put'ing works.
  	 */
051e7cd44   Christoph Hellwig   [XFS] use filldir...
767
768
  	sfep = xfs_dir2_sf_firstentry(sfp);
  	for (i = 0; i < sfp->hdr.count; i++) {
bbaaf5380   Christoph Hellwig   [XFS] Reduce shou...
769
770
  		off = xfs_dir2_db_off_to_dataptr(mp, mp->m_dirdatablk,
  				xfs_dir2_sf_get_offset(sfep));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
771

051e7cd44   Christoph Hellwig   [XFS] use filldir...
772
773
  		if (*offset > off) {
  			sfep = xfs_dir2_sf_nextentry(sfp, sfep);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
774
  			continue;
051e7cd44   Christoph Hellwig   [XFS] use filldir...
775
  		}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
776

051e7cd44   Christoph Hellwig   [XFS] use filldir...
777
  		ino = xfs_dir2_sf_get_inumber(sfp, xfs_dir2_sf_inumberp(sfep));
051e7cd44   Christoph Hellwig   [XFS] use filldir...
778
  		if (filldir(dirent, sfep->name, sfep->namelen,
154403197   Christoph Hellwig   [XFS] truncate re...
779
780
  			    off & 0x7fffffff, ino, DT_UNKNOWN)) {
  			*offset = off & 0x7fffffff;
051e7cd44   Christoph Hellwig   [XFS] use filldir...
781
  			return 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
782
  		}
051e7cd44   Christoph Hellwig   [XFS] use filldir...
783
  		sfep = xfs_dir2_sf_nextentry(sfp, sfep);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
784
  	}
154403197   Christoph Hellwig   [XFS] truncate re...
785
786
  	*offset = xfs_dir2_db_off_to_dataptr(mp, mp->m_dirdatablk + 1, 0) &
  			0x7fffffff;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
787
788
789
790
791
792
793
794
795
796
797
798
799
  	return 0;
  }
  
  /*
   * Lookup an entry in a shortform directory.
   * Returns EEXIST if found, ENOENT if not found.
   */
  int						/* error */
  xfs_dir2_sf_lookup(
  	xfs_da_args_t		*args)		/* operation arguments */
  {
  	xfs_inode_t		*dp;		/* incore directory inode */
  	int			i;		/* entry index */
384f3ced0   Barry Naujok   [XFS] Return case...
800
  	int			error;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
801
802
  	xfs_dir2_sf_entry_t	*sfep;		/* shortform directory entry */
  	xfs_dir2_sf_t		*sfp;		/* shortform structure */
5163f95a0   Barry Naujok   [XFS] Name operat...
803
  	enum xfs_dacmp		cmp;		/* comparison result */
384f3ced0   Barry Naujok   [XFS] Return case...
804
  	xfs_dir2_sf_entry_t	*ci_sfep;	/* case-insens. entry */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
  
  	xfs_dir2_trace_args("sf_lookup", args);
  	xfs_dir2_sf_check(args);
  	dp = args->dp;
  
  	ASSERT(dp->i_df.if_flags & XFS_IFINLINE);
  	/*
  	 * Bail out if the directory is way too short.
  	 */
  	if (dp->i_d.di_size < offsetof(xfs_dir2_sf_hdr_t, parent)) {
  		ASSERT(XFS_FORCED_SHUTDOWN(dp->i_mount));
  		return XFS_ERROR(EIO);
  	}
  	ASSERT(dp->i_df.if_bytes == dp->i_d.di_size);
  	ASSERT(dp->i_df.if_u1.if_data != NULL);
  	sfp = (xfs_dir2_sf_t *)dp->i_df.if_u1.if_data;
bbaaf5380   Christoph Hellwig   [XFS] Reduce shou...
821
  	ASSERT(dp->i_d.di_size >= xfs_dir2_sf_hdr_size(sfp->hdr.i8count));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
822
823
824
825
826
  	/*
  	 * Special case for .
  	 */
  	if (args->namelen == 1 && args->name[0] == '.') {
  		args->inumber = dp->i_ino;
5163f95a0   Barry Naujok   [XFS] Name operat...
827
  		args->cmpresult = XFS_CMP_EXACT;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
828
829
830
831
832
833
834
  		return XFS_ERROR(EEXIST);
  	}
  	/*
  	 * Special case for ..
  	 */
  	if (args->namelen == 2 &&
  	    args->name[0] == '.' && args->name[1] == '.') {
bbaaf5380   Christoph Hellwig   [XFS] Reduce shou...
835
  		args->inumber = xfs_dir2_sf_get_inumber(sfp, &sfp->hdr.parent);
5163f95a0   Barry Naujok   [XFS] Name operat...
836
  		args->cmpresult = XFS_CMP_EXACT;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
837
838
839
840
841
  		return XFS_ERROR(EEXIST);
  	}
  	/*
  	 * Loop over all the entries trying to match ours.
  	 */
384f3ced0   Barry Naujok   [XFS] Return case...
842
  	ci_sfep = NULL;
5163f95a0   Barry Naujok   [XFS] Name operat...
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
  	for (i = 0, sfep = xfs_dir2_sf_firstentry(sfp); i < sfp->hdr.count;
  				i++, sfep = xfs_dir2_sf_nextentry(sfp, sfep)) {
  		/*
  		 * Compare name and if it's an exact match, return the inode
  		 * number. If it's the first case-insensitive match, store the
  		 * inode number and continue looking for an exact match.
  		 */
  		cmp = dp->i_mount->m_dirnameops->compname(args, sfep->name,
  								sfep->namelen);
  		if (cmp != XFS_CMP_DIFFERENT && cmp != args->cmpresult) {
  			args->cmpresult = cmp;
  			args->inumber = xfs_dir2_sf_get_inumber(sfp,
  						xfs_dir2_sf_inumberp(sfep));
  			if (cmp == XFS_CMP_EXACT)
  				return XFS_ERROR(EEXIST);
384f3ced0   Barry Naujok   [XFS] Return case...
858
  			ci_sfep = sfep;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
859
860
  		}
  	}
6a178100a   Barry Naujok   [XFS] Add op_flag...
861
  	ASSERT(args->op_flags & XFS_DA_OP_OKNOENT);
5163f95a0   Barry Naujok   [XFS] Name operat...
862
863
  	/*
  	 * Here, we can only be doing a lookup (not a rename or replace).
384f3ced0   Barry Naujok   [XFS] Return case...
864
  	 * If a case-insensitive match was not found, return ENOENT.
5163f95a0   Barry Naujok   [XFS] Name operat...
865
  	 */
384f3ced0   Barry Naujok   [XFS] Return case...
866
867
868
869
870
  	if (!ci_sfep)
  		return XFS_ERROR(ENOENT);
  	/* otherwise process the CI match as required by the caller */
  	error = xfs_dir_cilookup_result(args, ci_sfep->name, ci_sfep->namelen);
  	return XFS_ERROR(error);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
  }
  
  /*
   * Remove an entry from a shortform directory.
   */
  int						/* error */
  xfs_dir2_sf_removename(
  	xfs_da_args_t		*args)
  {
  	int			byteoff;	/* offset of removed entry */
  	xfs_inode_t		*dp;		/* incore directory inode */
  	int			entsize;	/* this entry's size */
  	int			i;		/* shortform entry index */
  	int			newsize;	/* new inode size */
  	int			oldsize;	/* old inode size */
  	xfs_dir2_sf_entry_t	*sfep;		/* shortform directory entry */
  	xfs_dir2_sf_t		*sfp;		/* shortform structure */
  
  	xfs_dir2_trace_args("sf_removename", args);
  	dp = args->dp;
  
  	ASSERT(dp->i_df.if_flags & XFS_IFINLINE);
  	oldsize = (int)dp->i_d.di_size;
  	/*
  	 * Bail out if the directory is way too short.
  	 */
  	if (oldsize < offsetof(xfs_dir2_sf_hdr_t, parent)) {
  		ASSERT(XFS_FORCED_SHUTDOWN(dp->i_mount));
  		return XFS_ERROR(EIO);
  	}
  	ASSERT(dp->i_df.if_bytes == oldsize);
  	ASSERT(dp->i_df.if_u1.if_data != NULL);
  	sfp = (xfs_dir2_sf_t *)dp->i_df.if_u1.if_data;
bbaaf5380   Christoph Hellwig   [XFS] Reduce shou...
904
  	ASSERT(oldsize >= xfs_dir2_sf_hdr_size(sfp->hdr.i8count));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
905
906
907
908
  	/*
  	 * Loop over the old directory entries.
  	 * Find the one we're deleting.
  	 */
5163f95a0   Barry Naujok   [XFS] Name operat...
909
910
911
912
  	for (i = 0, sfep = xfs_dir2_sf_firstentry(sfp); i < sfp->hdr.count;
  				i++, sfep = xfs_dir2_sf_nextentry(sfp, sfep)) {
  		if (xfs_da_compname(args, sfep->name, sfep->namelen) ==
  								XFS_CMP_EXACT) {
bbaaf5380   Christoph Hellwig   [XFS] Reduce shou...
913
  			ASSERT(xfs_dir2_sf_get_inumber(sfp,
5163f95a0   Barry Naujok   [XFS] Name operat...
914
915
  						xfs_dir2_sf_inumberp(sfep)) ==
  								args->inumber);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
916
917
918
919
920
921
  			break;
  		}
  	}
  	/*
  	 * Didn't find it.
  	 */
5163f95a0   Barry Naujok   [XFS] Name operat...
922
  	if (i == sfp->hdr.count)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
923
  		return XFS_ERROR(ENOENT);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
924
925
926
927
  	/*
  	 * Calculate sizes.
  	 */
  	byteoff = (int)((char *)sfep - (char *)sfp);
bbaaf5380   Christoph Hellwig   [XFS] Reduce shou...
928
  	entsize = xfs_dir2_sf_entsize_byname(sfp, args->namelen);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
  	newsize = oldsize - entsize;
  	/*
  	 * Copy the part if any after the removed entry, sliding it down.
  	 */
  	if (byteoff + entsize < oldsize)
  		memmove((char *)sfp + byteoff, (char *)sfp + byteoff + entsize,
  			oldsize - (byteoff + entsize));
  	/*
  	 * Fix up the header and file size.
  	 */
  	sfp->hdr.count--;
  	dp->i_d.di_size = newsize;
  	/*
  	 * Reallocate, making it smaller.
  	 */
  	xfs_idata_realloc(dp, newsize - oldsize, XFS_DATA_FORK);
  	sfp = (xfs_dir2_sf_t *)dp->i_df.if_u1.if_data;
  #if XFS_BIG_INUMS
  	/*
  	 * Are we changing inode number size?
  	 */
  	if (args->inumber > XFS_DIR2_MAX_SHORT_INUM) {
  		if (sfp->hdr.i8count == 1)
  			xfs_dir2_sf_toino4(args);
  		else
  			sfp->hdr.i8count--;
  	}
  #endif
  	xfs_dir2_sf_check(args);
  	xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE | XFS_ILOG_DDATA);
  	return 0;
  }
  
  /*
   * Replace the inode number of an entry in a shortform directory.
   */
  int						/* error */
  xfs_dir2_sf_replace(
  	xfs_da_args_t		*args)		/* operation arguments */
  {
  	xfs_inode_t		*dp;		/* incore directory inode */
  	int			i;		/* entry index */
  #if XFS_BIG_INUMS || defined(DEBUG)
  	xfs_ino_t		ino=0;		/* entry old inode number */
  #endif
  #if XFS_BIG_INUMS
  	int			i8elevated;	/* sf_toino8 set i8count=1 */
  #endif
  	xfs_dir2_sf_entry_t	*sfep;		/* shortform directory entry */
  	xfs_dir2_sf_t		*sfp;		/* shortform structure */
  
  	xfs_dir2_trace_args("sf_replace", args);
  	dp = args->dp;
  
  	ASSERT(dp->i_df.if_flags & XFS_IFINLINE);
  	/*
  	 * Bail out if the shortform directory is way too small.
  	 */
  	if (dp->i_d.di_size < offsetof(xfs_dir2_sf_hdr_t, parent)) {
  		ASSERT(XFS_FORCED_SHUTDOWN(dp->i_mount));
  		return XFS_ERROR(EIO);
  	}
  	ASSERT(dp->i_df.if_bytes == dp->i_d.di_size);
  	ASSERT(dp->i_df.if_u1.if_data != NULL);
  	sfp = (xfs_dir2_sf_t *)dp->i_df.if_u1.if_data;
bbaaf5380   Christoph Hellwig   [XFS] Reduce shou...
994
  	ASSERT(dp->i_d.di_size >= xfs_dir2_sf_hdr_size(sfp->hdr.i8count));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
  #if XFS_BIG_INUMS
  	/*
  	 * New inode number is large, and need to convert to 8-byte inodes.
  	 */
  	if (args->inumber > XFS_DIR2_MAX_SHORT_INUM && sfp->hdr.i8count == 0) {
  		int	error;			/* error return value */
  		int	newsize;		/* new inode size */
  
  		newsize =
  			dp->i_df.if_bytes +
  			(sfp->hdr.count + 1) *
  			((uint)sizeof(xfs_dir2_ino8_t) -
  			 (uint)sizeof(xfs_dir2_ino4_t));
  		/*
  		 * Won't fit as shortform, convert to block then do replace.
  		 */
  		if (newsize > XFS_IFORK_DSIZE(dp)) {
  			error = xfs_dir2_sf_to_block(args);
  			if (error) {
  				return error;
  			}
  			return xfs_dir2_block_replace(args);
  		}
  		/*
  		 * Still fits, convert to 8-byte now.
  		 */
  		xfs_dir2_sf_toino8(args);
  		i8elevated = 1;
  		sfp = (xfs_dir2_sf_t *)dp->i_df.if_u1.if_data;
  	} else
  		i8elevated = 0;
  #endif
  	ASSERT(args->namelen != 1 || args->name[0] != '.');
  	/*
  	 * Replace ..'s entry.
  	 */
  	if (args->namelen == 2 &&
  	    args->name[0] == '.' && args->name[1] == '.') {
  #if XFS_BIG_INUMS || defined(DEBUG)
bbaaf5380   Christoph Hellwig   [XFS] Reduce shou...
1034
  		ino = xfs_dir2_sf_get_inumber(sfp, &sfp->hdr.parent);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1035
1036
  		ASSERT(args->inumber != ino);
  #endif
bbaaf5380   Christoph Hellwig   [XFS] Reduce shou...
1037
  		xfs_dir2_sf_put_inumber(sfp, &args->inumber, &sfp->hdr.parent);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1038
1039
1040
1041
1042
  	}
  	/*
  	 * Normal entry, look for the name.
  	 */
  	else {
bbaaf5380   Christoph Hellwig   [XFS] Reduce shou...
1043
  		for (i = 0, sfep = xfs_dir2_sf_firstentry(sfp);
5163f95a0   Barry Naujok   [XFS] Name operat...
1044
1045
1046
1047
  				i < sfp->hdr.count;
  				i++, sfep = xfs_dir2_sf_nextentry(sfp, sfep)) {
  			if (xfs_da_compname(args, sfep->name, sfep->namelen) ==
  								XFS_CMP_EXACT) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1048
  #if XFS_BIG_INUMS || defined(DEBUG)
bbaaf5380   Christoph Hellwig   [XFS] Reduce shou...
1049
1050
  				ino = xfs_dir2_sf_get_inumber(sfp,
  					xfs_dir2_sf_inumberp(sfep));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1051
1052
  				ASSERT(args->inumber != ino);
  #endif
bbaaf5380   Christoph Hellwig   [XFS] Reduce shou...
1053
1054
  				xfs_dir2_sf_put_inumber(sfp, &args->inumber,
  					xfs_dir2_sf_inumberp(sfep));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1055
1056
1057
1058
1059
1060
1061
  				break;
  			}
  		}
  		/*
  		 * Didn't find it.
  		 */
  		if (i == sfp->hdr.count) {
6a178100a   Barry Naujok   [XFS] Add op_flag...
1062
  			ASSERT(args->op_flags & XFS_DA_OP_OKNOENT);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
  #if XFS_BIG_INUMS
  			if (i8elevated)
  				xfs_dir2_sf_toino4(args);
  #endif
  			return XFS_ERROR(ENOENT);
  		}
  	}
  #if XFS_BIG_INUMS
  	/*
  	 * See if the old number was large, the new number is small.
  	 */
  	if (ino > XFS_DIR2_MAX_SHORT_INUM &&
  	    args->inumber <= XFS_DIR2_MAX_SHORT_INUM) {
  		/*
  		 * And the old count was one, so need to convert to small.
  		 */
  		if (sfp->hdr.i8count == 1)
  			xfs_dir2_sf_toino4(args);
  		else
  			sfp->hdr.i8count--;
  	}
  	/*
  	 * See if the old number was small, the new number is large.
  	 */
  	if (ino <= XFS_DIR2_MAX_SHORT_INUM &&
  	    args->inumber > XFS_DIR2_MAX_SHORT_INUM) {
  		/*
  		 * add to the i8count unless we just converted to 8-byte
  		 * inodes (which does an implied i8count = 1)
  		 */
  		ASSERT(sfp->hdr.i8count != 0);
  		if (!i8elevated)
  			sfp->hdr.i8count++;
  	}
  #endif
  	xfs_dir2_sf_check(args);
  	xfs_trans_log_inode(args->trans, dp, XFS_ILOG_DDATA);
  	return 0;
  }
  
  #if XFS_BIG_INUMS
  /*
   * Convert from 8-byte inode numbers to 4-byte inode numbers.
   * The last 8-byte inode number is gone, but the count is still 1.
   */
  static void
  xfs_dir2_sf_toino4(
  	xfs_da_args_t		*args)		/* operation arguments */
  {
  	char			*buf;		/* old dir's buffer */
  	xfs_inode_t		*dp;		/* incore directory inode */
  	int			i;		/* entry index */
  	xfs_ino_t		ino;		/* entry inode number */
  	int			newsize;	/* new inode size */
  	xfs_dir2_sf_entry_t	*oldsfep;	/* old sf entry */
  	xfs_dir2_sf_t		*oldsfp;	/* old sf directory */
  	int			oldsize;	/* old inode size */
  	xfs_dir2_sf_entry_t	*sfep;		/* new sf entry */
  	xfs_dir2_sf_t		*sfp;		/* new sf directory */
  
  	xfs_dir2_trace_args("sf_toino4", args);
  	dp = args->dp;
  
  	/*
  	 * Copy the old directory to the buffer.
  	 * Then nuke it from the inode, and add the new buffer to the inode.
  	 * Don't want xfs_idata_realloc copying the data here.
  	 */
  	oldsize = dp->i_df.if_bytes;
  	buf = kmem_alloc(oldsize, KM_SLEEP);
  	oldsfp = (xfs_dir2_sf_t *)dp->i_df.if_u1.if_data;
  	ASSERT(oldsfp->hdr.i8count == 1);
  	memcpy(buf, oldsfp, oldsize);
  	/*
  	 * Compute the new inode size.
  	 */
  	newsize =
  		oldsize -
  		(oldsfp->hdr.count + 1) *
  		((uint)sizeof(xfs_dir2_ino8_t) - (uint)sizeof(xfs_dir2_ino4_t));
  	xfs_idata_realloc(dp, -oldsize, XFS_DATA_FORK);
  	xfs_idata_realloc(dp, newsize, XFS_DATA_FORK);
  	/*
  	 * Reset our pointers, the data has moved.
  	 */
  	oldsfp = (xfs_dir2_sf_t *)buf;
  	sfp = (xfs_dir2_sf_t *)dp->i_df.if_u1.if_data;
  	/*
  	 * Fill in the new header.
  	 */
  	sfp->hdr.count = oldsfp->hdr.count;
  	sfp->hdr.i8count = 0;
bbaaf5380   Christoph Hellwig   [XFS] Reduce shou...
1155
1156
  	ino = xfs_dir2_sf_get_inumber(oldsfp, &oldsfp->hdr.parent);
  	xfs_dir2_sf_put_inumber(sfp, &ino, &sfp->hdr.parent);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1157
1158
1159
  	/*
  	 * Copy the entries field by field.
  	 */
bbaaf5380   Christoph Hellwig   [XFS] Reduce shou...
1160
1161
  	for (i = 0, sfep = xfs_dir2_sf_firstentry(sfp),
  		    oldsfep = xfs_dir2_sf_firstentry(oldsfp);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1162
  	     i < sfp->hdr.count;
bbaaf5380   Christoph Hellwig   [XFS] Reduce shou...
1163
1164
  	     i++, sfep = xfs_dir2_sf_nextentry(sfp, sfep),
  		  oldsfep = xfs_dir2_sf_nextentry(oldsfp, oldsfep)) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1165
1166
1167
  		sfep->namelen = oldsfep->namelen;
  		sfep->offset = oldsfep->offset;
  		memcpy(sfep->name, oldsfep->name, sfep->namelen);
bbaaf5380   Christoph Hellwig   [XFS] Reduce shou...
1168
1169
1170
  		ino = xfs_dir2_sf_get_inumber(oldsfp,
  			xfs_dir2_sf_inumberp(oldsfep));
  		xfs_dir2_sf_put_inumber(sfp, &ino, xfs_dir2_sf_inumberp(sfep));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1171
1172
1173
1174
  	}
  	/*
  	 * Clean up the inode.
  	 */
f0e2d93c2   Denys Vlasenko   [XFS] Remove unus...
1175
  	kmem_free(buf);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
  	dp->i_d.di_size = newsize;
  	xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE | XFS_ILOG_DDATA);
  }
  
  /*
   * Convert from 4-byte inode numbers to 8-byte inode numbers.
   * The new 8-byte inode number is not there yet, we leave with the
   * count 1 but no corresponding entry.
   */
  static void
  xfs_dir2_sf_toino8(
  	xfs_da_args_t		*args)		/* operation arguments */
  {
  	char			*buf;		/* old dir's buffer */
  	xfs_inode_t		*dp;		/* incore directory inode */
  	int			i;		/* entry index */
  	xfs_ino_t		ino;		/* entry inode number */
  	int			newsize;	/* new inode size */
  	xfs_dir2_sf_entry_t	*oldsfep;	/* old sf entry */
  	xfs_dir2_sf_t		*oldsfp;	/* old sf directory */
  	int			oldsize;	/* old inode size */
  	xfs_dir2_sf_entry_t	*sfep;		/* new sf entry */
  	xfs_dir2_sf_t		*sfp;		/* new sf directory */
  
  	xfs_dir2_trace_args("sf_toino8", args);
  	dp = args->dp;
  
  	/*
  	 * Copy the old directory to the buffer.
  	 * Then nuke it from the inode, and add the new buffer to the inode.
  	 * Don't want xfs_idata_realloc copying the data here.
  	 */
  	oldsize = dp->i_df.if_bytes;
  	buf = kmem_alloc(oldsize, KM_SLEEP);
  	oldsfp = (xfs_dir2_sf_t *)dp->i_df.if_u1.if_data;
  	ASSERT(oldsfp->hdr.i8count == 0);
  	memcpy(buf, oldsfp, oldsize);
  	/*
  	 * Compute the new inode size.
  	 */
  	newsize =
  		oldsize +
  		(oldsfp->hdr.count + 1) *
  		((uint)sizeof(xfs_dir2_ino8_t) - (uint)sizeof(xfs_dir2_ino4_t));
  	xfs_idata_realloc(dp, -oldsize, XFS_DATA_FORK);
  	xfs_idata_realloc(dp, newsize, XFS_DATA_FORK);
  	/*
  	 * Reset our pointers, the data has moved.
  	 */
  	oldsfp = (xfs_dir2_sf_t *)buf;
  	sfp = (xfs_dir2_sf_t *)dp->i_df.if_u1.if_data;
  	/*
  	 * Fill in the new header.
  	 */
  	sfp->hdr.count = oldsfp->hdr.count;
  	sfp->hdr.i8count = 1;
bbaaf5380   Christoph Hellwig   [XFS] Reduce shou...
1232
1233
  	ino = xfs_dir2_sf_get_inumber(oldsfp, &oldsfp->hdr.parent);
  	xfs_dir2_sf_put_inumber(sfp, &ino, &sfp->hdr.parent);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1234
1235
1236
  	/*
  	 * Copy the entries field by field.
  	 */
bbaaf5380   Christoph Hellwig   [XFS] Reduce shou...
1237
1238
  	for (i = 0, sfep = xfs_dir2_sf_firstentry(sfp),
  		    oldsfep = xfs_dir2_sf_firstentry(oldsfp);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1239
  	     i < sfp->hdr.count;
bbaaf5380   Christoph Hellwig   [XFS] Reduce shou...
1240
1241
  	     i++, sfep = xfs_dir2_sf_nextentry(sfp, sfep),
  		  oldsfep = xfs_dir2_sf_nextentry(oldsfp, oldsfep)) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1242
1243
1244
  		sfep->namelen = oldsfep->namelen;
  		sfep->offset = oldsfep->offset;
  		memcpy(sfep->name, oldsfep->name, sfep->namelen);
bbaaf5380   Christoph Hellwig   [XFS] Reduce shou...
1245
1246
1247
  		ino = xfs_dir2_sf_get_inumber(oldsfp,
  			xfs_dir2_sf_inumberp(oldsfep));
  		xfs_dir2_sf_put_inumber(sfp, &ino, xfs_dir2_sf_inumberp(sfep));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1248
1249
1250
1251
  	}
  	/*
  	 * Clean up the inode.
  	 */
f0e2d93c2   Denys Vlasenko   [XFS] Remove unus...
1252
  	kmem_free(buf);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1253
1254
1255
1256
  	dp->i_d.di_size = newsize;
  	xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE | XFS_ILOG_DDATA);
  }
  #endif	/* XFS_BIG_INUMS */