xref: /openbmc/linux/fs/xfs/libxfs/xfs_dir2_sf.c (revision c1d3fb8a)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (c) 2000-2003,2005 Silicon Graphics, Inc.
4  * All Rights Reserved.
5  */
6 #include "xfs.h"
7 #include "xfs_fs.h"
8 #include "xfs_shared.h"
9 #include "xfs_format.h"
10 #include "xfs_log_format.h"
11 #include "xfs_trans_resv.h"
12 #include "xfs_mount.h"
13 #include "xfs_inode.h"
14 #include "xfs_trans.h"
15 #include "xfs_dir2.h"
16 #include "xfs_dir2_priv.h"
17 #include "xfs_trace.h"
18 
19 /*
20  * Prototypes for internal functions.
21  */
22 static void xfs_dir2_sf_addname_easy(xfs_da_args_t *args,
23 				     xfs_dir2_sf_entry_t *sfep,
24 				     xfs_dir2_data_aoff_t offset,
25 				     int new_isize);
26 static void xfs_dir2_sf_addname_hard(xfs_da_args_t *args, int objchange,
27 				     int new_isize);
28 static int xfs_dir2_sf_addname_pick(xfs_da_args_t *args, int objchange,
29 				    xfs_dir2_sf_entry_t **sfepp,
30 				    xfs_dir2_data_aoff_t *offsetp);
31 #ifdef DEBUG
32 static void xfs_dir2_sf_check(xfs_da_args_t *args);
33 #else
34 #define	xfs_dir2_sf_check(args)
35 #endif /* DEBUG */
36 
37 static void xfs_dir2_sf_toino4(xfs_da_args_t *args);
38 static void xfs_dir2_sf_toino8(xfs_da_args_t *args);
39 
40 static int
41 xfs_dir2_sf_entsize(
42 	struct xfs_mount	*mp,
43 	struct xfs_dir2_sf_hdr	*hdr,
44 	int			len)
45 {
46 	int			count = len;
47 
48 	count += sizeof(struct xfs_dir2_sf_entry);	/* namelen + offset */
49 	count += hdr->i8count ? XFS_INO64_SIZE : XFS_INO32_SIZE; /* ino # */
50 
51 	if (xfs_sb_version_hasftype(&mp->m_sb))
52 		count += sizeof(uint8_t);
53 	return count;
54 }
55 
56 struct xfs_dir2_sf_entry *
57 xfs_dir2_sf_nextentry(
58 	struct xfs_mount	*mp,
59 	struct xfs_dir2_sf_hdr	*hdr,
60 	struct xfs_dir2_sf_entry *sfep)
61 {
62 	return (void *)sfep + xfs_dir2_sf_entsize(mp, hdr, sfep->namelen);
63 }
64 
65 /*
66  * In short-form directory entries the inode numbers are stored at variable
67  * offset behind the entry name. If the entry stores a filetype value, then it
68  * sits between the name and the inode number.  The actual inode numbers can
69  * come in two formats as well, either 4 bytes or 8 bytes wide.
70  */
71 xfs_ino_t
72 xfs_dir2_sf_get_ino(
73 	struct xfs_mount		*mp,
74 	struct xfs_dir2_sf_hdr		*hdr,
75 	struct xfs_dir2_sf_entry	*sfep)
76 {
77 	uint8_t				*from = sfep->name + sfep->namelen;
78 
79 	if (xfs_sb_version_hasftype(&mp->m_sb))
80 		from++;
81 
82 	if (!hdr->i8count)
83 		return get_unaligned_be32(from);
84 	return get_unaligned_be64(from) & XFS_MAXINUMBER;
85 }
86 
87 static void
88 xfs_dir2_sf_put_ino(
89 	struct xfs_mount		*mp,
90 	struct xfs_dir2_sf_hdr		*hdr,
91 	struct xfs_dir2_sf_entry	*sfep,
92 	xfs_ino_t			ino)
93 {
94 	uint8_t				*to = sfep->name + sfep->namelen;
95 
96 	ASSERT(ino <= XFS_MAXINUMBER);
97 
98 	if (xfs_sb_version_hasftype(&mp->m_sb))
99 		to++;
100 
101 	if (hdr->i8count)
102 		put_unaligned_be64(ino, to);
103 	else
104 		put_unaligned_be32(ino, to);
105 }
106 
107 xfs_ino_t
108 xfs_dir2_sf_get_parent_ino(
109 	struct xfs_dir2_sf_hdr	*hdr)
110 {
111 	if (!hdr->i8count)
112 		return get_unaligned_be32(hdr->parent);
113 	return get_unaligned_be64(hdr->parent) & XFS_MAXINUMBER;
114 }
115 
116 void
117 xfs_dir2_sf_put_parent_ino(
118 	struct xfs_dir2_sf_hdr		*hdr,
119 	xfs_ino_t			ino)
120 {
121 	ASSERT(ino <= XFS_MAXINUMBER);
122 
123 	if (hdr->i8count)
124 		put_unaligned_be64(ino, hdr->parent);
125 	else
126 		put_unaligned_be32(ino, hdr->parent);
127 }
128 
129 /*
130  * The file type field is stored at the end of the name for filetype enabled
131  * shortform directories, or not at all otherwise.
132  */
133 uint8_t
134 xfs_dir2_sf_get_ftype(
135 	struct xfs_mount		*mp,
136 	struct xfs_dir2_sf_entry	*sfep)
137 {
138 	if (xfs_sb_version_hasftype(&mp->m_sb)) {
139 		uint8_t			ftype = sfep->name[sfep->namelen];
140 
141 		if (ftype < XFS_DIR3_FT_MAX)
142 			return ftype;
143 	}
144 
145 	return XFS_DIR3_FT_UNKNOWN;
146 }
147 
148 static void
149 xfs_dir2_sf_put_ftype(
150 	struct xfs_mount	*mp,
151 	struct xfs_dir2_sf_entry *sfep,
152 	uint8_t			ftype)
153 {
154 	ASSERT(ftype < XFS_DIR3_FT_MAX);
155 
156 	if (xfs_sb_version_hasftype(&mp->m_sb))
157 		sfep->name[sfep->namelen] = ftype;
158 }
159 
160 /*
161  * Given a block directory (dp/block), calculate its size as a shortform (sf)
162  * directory and a header for the sf directory, if it will fit it the
163  * space currently present in the inode.  If it won't fit, the output
164  * size is too big (but not accurate).
165  */
166 int						/* size for sf form */
167 xfs_dir2_block_sfsize(
168 	xfs_inode_t		*dp,		/* incore inode pointer */
169 	xfs_dir2_data_hdr_t	*hdr,		/* block directory data */
170 	xfs_dir2_sf_hdr_t	*sfhp)		/* output: header for sf form */
171 {
172 	xfs_dir2_dataptr_t	addr;		/* data entry address */
173 	xfs_dir2_leaf_entry_t	*blp;		/* leaf area of the block */
174 	xfs_dir2_block_tail_t	*btp;		/* tail area of the block */
175 	int			count;		/* shortform entry count */
176 	xfs_dir2_data_entry_t	*dep;		/* data entry in the block */
177 	int			i;		/* block entry index */
178 	int			i8count;	/* count of big-inode entries */
179 	int			isdot;		/* entry is "." */
180 	int			isdotdot;	/* entry is ".." */
181 	xfs_mount_t		*mp;		/* mount structure pointer */
182 	int			namelen;	/* total name bytes */
183 	xfs_ino_t		parent = 0;	/* parent inode number */
184 	int			size=0;		/* total computed size */
185 	int			has_ftype;
186 	struct xfs_da_geometry	*geo;
187 
188 	mp = dp->i_mount;
189 	geo = mp->m_dir_geo;
190 
191 	/*
192 	 * if there is a filetype field, add the extra byte to the namelen
193 	 * for each entry that we see.
194 	 */
195 	has_ftype = xfs_sb_version_hasftype(&mp->m_sb) ? 1 : 0;
196 
197 	count = i8count = namelen = 0;
198 	btp = xfs_dir2_block_tail_p(geo, hdr);
199 	blp = xfs_dir2_block_leaf_p(btp);
200 
201 	/*
202 	 * Iterate over the block's data entries by using the leaf pointers.
203 	 */
204 	for (i = 0; i < be32_to_cpu(btp->count); i++) {
205 		if ((addr = be32_to_cpu(blp[i].address)) == XFS_DIR2_NULL_DATAPTR)
206 			continue;
207 		/*
208 		 * Calculate the pointer to the entry at hand.
209 		 */
210 		dep = (xfs_dir2_data_entry_t *)((char *)hdr +
211 				xfs_dir2_dataptr_to_off(geo, addr));
212 		/*
213 		 * Detect . and .., so we can special-case them.
214 		 * . is not included in sf directories.
215 		 * .. is included by just the parent inode number.
216 		 */
217 		isdot = dep->namelen == 1 && dep->name[0] == '.';
218 		isdotdot =
219 			dep->namelen == 2 &&
220 			dep->name[0] == '.' && dep->name[1] == '.';
221 
222 		if (!isdot)
223 			i8count += be64_to_cpu(dep->inumber) > XFS_DIR2_MAX_SHORT_INUM;
224 
225 		/* take into account the file type field */
226 		if (!isdot && !isdotdot) {
227 			count++;
228 			namelen += dep->namelen + has_ftype;
229 		} else if (isdotdot)
230 			parent = be64_to_cpu(dep->inumber);
231 		/*
232 		 * Calculate the new size, see if we should give up yet.
233 		 */
234 		size = xfs_dir2_sf_hdr_size(i8count) +	/* header */
235 		       count * 3 * sizeof(u8) +		/* namelen + offset */
236 		       namelen +			/* name */
237 		       (i8count ?			/* inumber */
238 				count * XFS_INO64_SIZE :
239 				count * XFS_INO32_SIZE);
240 		if (size > XFS_IFORK_DSIZE(dp))
241 			return size;		/* size value is a failure */
242 	}
243 	/*
244 	 * Create the output header, if it worked.
245 	 */
246 	sfhp->count = count;
247 	sfhp->i8count = i8count;
248 	xfs_dir2_sf_put_parent_ino(sfhp, parent);
249 	return size;
250 }
251 
252 /*
253  * Convert a block format directory to shortform.
254  * Caller has already checked that it will fit, and built us a header.
255  */
256 int						/* error */
257 xfs_dir2_block_to_sf(
258 	struct xfs_da_args	*args,		/* operation arguments */
259 	struct xfs_buf		*bp,
260 	int			size,		/* shortform directory size */
261 	struct xfs_dir2_sf_hdr	*sfhp)		/* shortform directory hdr */
262 {
263 	struct xfs_inode	*dp = args->dp;
264 	struct xfs_mount	*mp = dp->i_mount;
265 	int			error;		/* error return value */
266 	int			logflags;	/* inode logging flags */
267 	struct xfs_dir2_sf_entry *sfep;		/* shortform entry */
268 	struct xfs_dir2_sf_hdr	*sfp;		/* shortform directory header */
269 	unsigned int		offset = args->geo->data_entry_offset;
270 	unsigned int		end;
271 
272 	trace_xfs_dir2_block_to_sf(args);
273 
274 	/*
275 	 * Allocate a temporary destination buffer the size of the inode to
276 	 * format the data into.  Once we have formatted the data, we can free
277 	 * the block and copy the formatted data into the inode literal area.
278 	 */
279 	sfp = kmem_alloc(mp->m_sb.sb_inodesize, 0);
280 	memcpy(sfp, sfhp, xfs_dir2_sf_hdr_size(sfhp->i8count));
281 
282 	/*
283 	 * Loop over the active and unused entries.  Stop when we reach the
284 	 * leaf/tail portion of the block.
285 	 */
286 	end = xfs_dir3_data_end_offset(args->geo, bp->b_addr);
287 	sfep = xfs_dir2_sf_firstentry(sfp);
288 	while (offset < end) {
289 		struct xfs_dir2_data_unused	*dup = bp->b_addr + offset;
290 		struct xfs_dir2_data_entry	*dep = bp->b_addr + offset;
291 
292 		/*
293 		 * If it's unused, just skip over it.
294 		 */
295 		if (be16_to_cpu(dup->freetag) == XFS_DIR2_DATA_FREE_TAG) {
296 			offset += be16_to_cpu(dup->length);
297 			continue;
298 		}
299 
300 		/*
301 		 * Skip .
302 		 */
303 		if (dep->namelen == 1 && dep->name[0] == '.')
304 			ASSERT(be64_to_cpu(dep->inumber) == dp->i_ino);
305 		/*
306 		 * Skip .., but make sure the inode number is right.
307 		 */
308 		else if (dep->namelen == 2 &&
309 			 dep->name[0] == '.' && dep->name[1] == '.')
310 			ASSERT(be64_to_cpu(dep->inumber) ==
311 			       xfs_dir2_sf_get_parent_ino(sfp));
312 		/*
313 		 * Normal entry, copy it into shortform.
314 		 */
315 		else {
316 			sfep->namelen = dep->namelen;
317 			xfs_dir2_sf_put_offset(sfep, offset);
318 			memcpy(sfep->name, dep->name, dep->namelen);
319 			xfs_dir2_sf_put_ino(mp, sfp, sfep,
320 					      be64_to_cpu(dep->inumber));
321 			xfs_dir2_sf_put_ftype(mp, sfep,
322 					xfs_dir2_data_get_ftype(mp, dep));
323 
324 			sfep = xfs_dir2_sf_nextentry(mp, sfp, sfep);
325 		}
326 		offset += xfs_dir2_data_entsize(mp, dep->namelen);
327 	}
328 	ASSERT((char *)sfep - (char *)sfp == size);
329 
330 	/* now we are done with the block, we can shrink the inode */
331 	logflags = XFS_ILOG_CORE;
332 	error = xfs_dir2_shrink_inode(args, args->geo->datablk, bp);
333 	if (error) {
334 		ASSERT(error != -ENOSPC);
335 		goto out;
336 	}
337 
338 	/*
339 	 * The buffer is now unconditionally gone, whether
340 	 * xfs_dir2_shrink_inode worked or not.
341 	 *
342 	 * Convert the inode to local format and copy the data in.
343 	 */
344 	ASSERT(dp->i_df.if_bytes == 0);
345 	xfs_init_local_fork(dp, XFS_DATA_FORK, sfp, size);
346 	dp->i_d.di_format = XFS_DINODE_FMT_LOCAL;
347 	dp->i_d.di_size = size;
348 
349 	logflags |= XFS_ILOG_DDATA;
350 	xfs_dir2_sf_check(args);
351 out:
352 	xfs_trans_log_inode(args->trans, dp, logflags);
353 	kmem_free(sfp);
354 	return error;
355 }
356 
357 /*
358  * Add a name to a shortform directory.
359  * There are two algorithms, "easy" and "hard" which we decide on
360  * before changing anything.
361  * Convert to block form if necessary, if the new entry won't fit.
362  */
363 int						/* error */
364 xfs_dir2_sf_addname(
365 	xfs_da_args_t		*args)		/* operation arguments */
366 {
367 	xfs_inode_t		*dp;		/* incore directory inode */
368 	int			error;		/* error return value */
369 	int			incr_isize;	/* total change in size */
370 	int			new_isize;	/* di_size after adding name */
371 	int			objchange;	/* changing to 8-byte inodes */
372 	xfs_dir2_data_aoff_t	offset = 0;	/* offset for new entry */
373 	int			pick;		/* which algorithm to use */
374 	xfs_dir2_sf_hdr_t	*sfp;		/* shortform structure */
375 	xfs_dir2_sf_entry_t	*sfep = NULL;	/* shortform entry */
376 
377 	trace_xfs_dir2_sf_addname(args);
378 
379 	ASSERT(xfs_dir2_sf_lookup(args) == -ENOENT);
380 	dp = args->dp;
381 	ASSERT(dp->i_df.if_flags & XFS_IFINLINE);
382 	ASSERT(dp->i_d.di_size >= offsetof(struct xfs_dir2_sf_hdr, parent));
383 	ASSERT(dp->i_df.if_bytes == dp->i_d.di_size);
384 	ASSERT(dp->i_df.if_u1.if_data != NULL);
385 	sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
386 	ASSERT(dp->i_d.di_size >= xfs_dir2_sf_hdr_size(sfp->i8count));
387 	/*
388 	 * Compute entry (and change in) size.
389 	 */
390 	incr_isize = xfs_dir2_sf_entsize(dp->i_mount, sfp, args->namelen);
391 	objchange = 0;
392 
393 	/*
394 	 * Do we have to change to 8 byte inodes?
395 	 */
396 	if (args->inumber > XFS_DIR2_MAX_SHORT_INUM && sfp->i8count == 0) {
397 		/*
398 		 * Yes, adjust the inode size.  old count + (parent + new)
399 		 */
400 		incr_isize += (sfp->count + 2) * XFS_INO64_DIFF;
401 		objchange = 1;
402 	}
403 
404 	new_isize = (int)dp->i_d.di_size + incr_isize;
405 	/*
406 	 * Won't fit as shortform any more (due to size),
407 	 * or the pick routine says it won't (due to offset values).
408 	 */
409 	if (new_isize > XFS_IFORK_DSIZE(dp) ||
410 	    (pick =
411 	     xfs_dir2_sf_addname_pick(args, objchange, &sfep, &offset)) == 0) {
412 		/*
413 		 * Just checking or no space reservation, it doesn't fit.
414 		 */
415 		if ((args->op_flags & XFS_DA_OP_JUSTCHECK) || args->total == 0)
416 			return -ENOSPC;
417 		/*
418 		 * Convert to block form then add the name.
419 		 */
420 		error = xfs_dir2_sf_to_block(args);
421 		if (error)
422 			return error;
423 		return xfs_dir2_block_addname(args);
424 	}
425 	/*
426 	 * Just checking, it fits.
427 	 */
428 	if (args->op_flags & XFS_DA_OP_JUSTCHECK)
429 		return 0;
430 	/*
431 	 * Do it the easy way - just add it at the end.
432 	 */
433 	if (pick == 1)
434 		xfs_dir2_sf_addname_easy(args, sfep, offset, new_isize);
435 	/*
436 	 * Do it the hard way - look for a place to insert the new entry.
437 	 * Convert to 8 byte inode numbers first if necessary.
438 	 */
439 	else {
440 		ASSERT(pick == 2);
441 		if (objchange)
442 			xfs_dir2_sf_toino8(args);
443 		xfs_dir2_sf_addname_hard(args, objchange, new_isize);
444 	}
445 	xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE | XFS_ILOG_DDATA);
446 	return 0;
447 }
448 
449 /*
450  * Add the new entry the "easy" way.
451  * This is copying the old directory and adding the new entry at the end.
452  * Since it's sorted by "offset" we need room after the last offset
453  * that's already there, and then room to convert to a block directory.
454  * This is already checked by the pick routine.
455  */
456 static void
457 xfs_dir2_sf_addname_easy(
458 	xfs_da_args_t		*args,		/* operation arguments */
459 	xfs_dir2_sf_entry_t	*sfep,		/* pointer to new entry */
460 	xfs_dir2_data_aoff_t	offset,		/* offset to use for new ent */
461 	int			new_isize)	/* new directory size */
462 {
463 	struct xfs_inode	*dp = args->dp;
464 	struct xfs_mount	*mp = dp->i_mount;
465 	int			byteoff;	/* byte offset in sf dir */
466 	xfs_dir2_sf_hdr_t	*sfp;		/* shortform structure */
467 
468 	sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
469 	byteoff = (int)((char *)sfep - (char *)sfp);
470 	/*
471 	 * Grow the in-inode space.
472 	 */
473 	xfs_idata_realloc(dp, xfs_dir2_sf_entsize(mp, sfp, args->namelen),
474 			  XFS_DATA_FORK);
475 	/*
476 	 * Need to set up again due to realloc of the inode data.
477 	 */
478 	sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
479 	sfep = (xfs_dir2_sf_entry_t *)((char *)sfp + byteoff);
480 	/*
481 	 * Fill in the new entry.
482 	 */
483 	sfep->namelen = args->namelen;
484 	xfs_dir2_sf_put_offset(sfep, offset);
485 	memcpy(sfep->name, args->name, sfep->namelen);
486 	xfs_dir2_sf_put_ino(mp, sfp, sfep, args->inumber);
487 	xfs_dir2_sf_put_ftype(mp, sfep, args->filetype);
488 
489 	/*
490 	 * Update the header and inode.
491 	 */
492 	sfp->count++;
493 	if (args->inumber > XFS_DIR2_MAX_SHORT_INUM)
494 		sfp->i8count++;
495 	dp->i_d.di_size = new_isize;
496 	xfs_dir2_sf_check(args);
497 }
498 
499 /*
500  * Add the new entry the "hard" way.
501  * The caller has already converted to 8 byte inode numbers if necessary,
502  * in which case we need to leave the i8count at 1.
503  * Find a hole that the new entry will fit into, and copy
504  * the first part of the entries, the new entry, and the last part of
505  * the entries.
506  */
507 /* ARGSUSED */
508 static void
509 xfs_dir2_sf_addname_hard(
510 	xfs_da_args_t		*args,		/* operation arguments */
511 	int			objchange,	/* changing inode number size */
512 	int			new_isize)	/* new directory size */
513 {
514 	struct xfs_inode	*dp = args->dp;
515 	struct xfs_mount	*mp = dp->i_mount;
516 	int			add_datasize;	/* data size need for new ent */
517 	char			*buf;		/* buffer for old */
518 	int			eof;		/* reached end of old dir */
519 	int			nbytes;		/* temp for byte copies */
520 	xfs_dir2_data_aoff_t	new_offset;	/* next offset value */
521 	xfs_dir2_data_aoff_t	offset;		/* current offset value */
522 	int			old_isize;	/* previous di_size */
523 	xfs_dir2_sf_entry_t	*oldsfep;	/* entry in original dir */
524 	xfs_dir2_sf_hdr_t	*oldsfp;	/* original shortform dir */
525 	xfs_dir2_sf_entry_t	*sfep;		/* entry in new dir */
526 	xfs_dir2_sf_hdr_t	*sfp;		/* new shortform dir */
527 
528 	/*
529 	 * Copy the old directory to the stack buffer.
530 	 */
531 	sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
532 	old_isize = (int)dp->i_d.di_size;
533 	buf = kmem_alloc(old_isize, 0);
534 	oldsfp = (xfs_dir2_sf_hdr_t *)buf;
535 	memcpy(oldsfp, sfp, old_isize);
536 	/*
537 	 * Loop over the old directory finding the place we're going
538 	 * to insert the new entry.
539 	 * If it's going to end up at the end then oldsfep will point there.
540 	 */
541 	for (offset = args->geo->data_first_offset,
542 	      oldsfep = xfs_dir2_sf_firstentry(oldsfp),
543 	      add_datasize = xfs_dir2_data_entsize(mp, args->namelen),
544 	      eof = (char *)oldsfep == &buf[old_isize];
545 	     !eof;
546 	     offset = new_offset + xfs_dir2_data_entsize(mp, oldsfep->namelen),
547 	      oldsfep = xfs_dir2_sf_nextentry(mp, oldsfp, oldsfep),
548 	      eof = (char *)oldsfep == &buf[old_isize]) {
549 		new_offset = xfs_dir2_sf_get_offset(oldsfep);
550 		if (offset + add_datasize <= new_offset)
551 			break;
552 	}
553 	/*
554 	 * Get rid of the old directory, then allocate space for
555 	 * the new one.  We do this so xfs_idata_realloc won't copy
556 	 * the data.
557 	 */
558 	xfs_idata_realloc(dp, -old_isize, XFS_DATA_FORK);
559 	xfs_idata_realloc(dp, new_isize, XFS_DATA_FORK);
560 	/*
561 	 * Reset the pointer since the buffer was reallocated.
562 	 */
563 	sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
564 	/*
565 	 * Copy the first part of the directory, including the header.
566 	 */
567 	nbytes = (int)((char *)oldsfep - (char *)oldsfp);
568 	memcpy(sfp, oldsfp, nbytes);
569 	sfep = (xfs_dir2_sf_entry_t *)((char *)sfp + nbytes);
570 	/*
571 	 * Fill in the new entry, and update the header counts.
572 	 */
573 	sfep->namelen = args->namelen;
574 	xfs_dir2_sf_put_offset(sfep, offset);
575 	memcpy(sfep->name, args->name, sfep->namelen);
576 	xfs_dir2_sf_put_ino(mp, sfp, sfep, args->inumber);
577 	xfs_dir2_sf_put_ftype(mp, sfep, args->filetype);
578 	sfp->count++;
579 	if (args->inumber > XFS_DIR2_MAX_SHORT_INUM && !objchange)
580 		sfp->i8count++;
581 	/*
582 	 * If there's more left to copy, do that.
583 	 */
584 	if (!eof) {
585 		sfep = xfs_dir2_sf_nextentry(mp, sfp, sfep);
586 		memcpy(sfep, oldsfep, old_isize - nbytes);
587 	}
588 	kmem_free(buf);
589 	dp->i_d.di_size = new_isize;
590 	xfs_dir2_sf_check(args);
591 }
592 
593 /*
594  * Decide if the new entry will fit at all.
595  * If it will fit, pick between adding the new entry to the end (easy)
596  * or somewhere else (hard).
597  * Return 0 (won't fit), 1 (easy), 2 (hard).
598  */
599 /*ARGSUSED*/
600 static int					/* pick result */
601 xfs_dir2_sf_addname_pick(
602 	xfs_da_args_t		*args,		/* operation arguments */
603 	int			objchange,	/* inode # size changes */
604 	xfs_dir2_sf_entry_t	**sfepp,	/* out(1): new entry ptr */
605 	xfs_dir2_data_aoff_t	*offsetp)	/* out(1): new offset */
606 {
607 	struct xfs_inode	*dp = args->dp;
608 	struct xfs_mount	*mp = dp->i_mount;
609 	int			holefit;	/* found hole it will fit in */
610 	int			i;		/* entry number */
611 	xfs_dir2_data_aoff_t	offset;		/* data block offset */
612 	xfs_dir2_sf_entry_t	*sfep;		/* shortform entry */
613 	xfs_dir2_sf_hdr_t	*sfp;		/* shortform structure */
614 	int			size;		/* entry's data size */
615 	int			used;		/* data bytes used */
616 
617 	sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
618 	size = xfs_dir2_data_entsize(mp, args->namelen);
619 	offset = args->geo->data_first_offset;
620 	sfep = xfs_dir2_sf_firstentry(sfp);
621 	holefit = 0;
622 	/*
623 	 * Loop over sf entries.
624 	 * Keep track of data offset and whether we've seen a place
625 	 * to insert the new entry.
626 	 */
627 	for (i = 0; i < sfp->count; i++) {
628 		if (!holefit)
629 			holefit = offset + size <= xfs_dir2_sf_get_offset(sfep);
630 		offset = xfs_dir2_sf_get_offset(sfep) +
631 			 xfs_dir2_data_entsize(mp, sfep->namelen);
632 		sfep = xfs_dir2_sf_nextentry(mp, sfp, sfep);
633 	}
634 	/*
635 	 * Calculate data bytes used excluding the new entry, if this
636 	 * was a data block (block form directory).
637 	 */
638 	used = offset +
639 	       (sfp->count + 3) * (uint)sizeof(xfs_dir2_leaf_entry_t) +
640 	       (uint)sizeof(xfs_dir2_block_tail_t);
641 	/*
642 	 * If it won't fit in a block form then we can't insert it,
643 	 * we'll go back, convert to block, then try the insert and convert
644 	 * to leaf.
645 	 */
646 	if (used + (holefit ? 0 : size) > args->geo->blksize)
647 		return 0;
648 	/*
649 	 * If changing the inode number size, do it the hard way.
650 	 */
651 	if (objchange)
652 		return 2;
653 	/*
654 	 * If it won't fit at the end then do it the hard way (use the hole).
655 	 */
656 	if (used + size > args->geo->blksize)
657 		return 2;
658 	/*
659 	 * Do it the easy way.
660 	 */
661 	*sfepp = sfep;
662 	*offsetp = offset;
663 	return 1;
664 }
665 
666 #ifdef DEBUG
667 /*
668  * Check consistency of shortform directory, assert if bad.
669  */
670 static void
671 xfs_dir2_sf_check(
672 	xfs_da_args_t		*args)		/* operation arguments */
673 {
674 	struct xfs_inode	*dp = args->dp;
675 	struct xfs_mount	*mp = dp->i_mount;
676 	int			i;		/* entry number */
677 	int			i8count;	/* number of big inode#s */
678 	xfs_ino_t		ino;		/* entry inode number */
679 	int			offset;		/* data offset */
680 	xfs_dir2_sf_entry_t	*sfep;		/* shortform dir entry */
681 	xfs_dir2_sf_hdr_t	*sfp;		/* shortform structure */
682 
683 	sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
684 	offset = args->geo->data_first_offset;
685 	ino = xfs_dir2_sf_get_parent_ino(sfp);
686 	i8count = ino > XFS_DIR2_MAX_SHORT_INUM;
687 
688 	for (i = 0, sfep = xfs_dir2_sf_firstentry(sfp);
689 	     i < sfp->count;
690 	     i++, sfep = xfs_dir2_sf_nextentry(mp, sfp, sfep)) {
691 		ASSERT(xfs_dir2_sf_get_offset(sfep) >= offset);
692 		ino = xfs_dir2_sf_get_ino(mp, sfp, sfep);
693 		i8count += ino > XFS_DIR2_MAX_SHORT_INUM;
694 		offset =
695 			xfs_dir2_sf_get_offset(sfep) +
696 			xfs_dir2_data_entsize(mp, sfep->namelen);
697 		ASSERT(xfs_dir2_sf_get_ftype(mp, sfep) < XFS_DIR3_FT_MAX);
698 	}
699 	ASSERT(i8count == sfp->i8count);
700 	ASSERT((char *)sfep - (char *)sfp == dp->i_d.di_size);
701 	ASSERT(offset +
702 	       (sfp->count + 2) * (uint)sizeof(xfs_dir2_leaf_entry_t) +
703 	       (uint)sizeof(xfs_dir2_block_tail_t) <= args->geo->blksize);
704 }
705 #endif	/* DEBUG */
706 
707 /* Verify the consistency of an inline directory. */
708 xfs_failaddr_t
709 xfs_dir2_sf_verify(
710 	struct xfs_inode		*ip)
711 {
712 	struct xfs_mount		*mp = ip->i_mount;
713 	struct xfs_dir2_sf_hdr		*sfp;
714 	struct xfs_dir2_sf_entry	*sfep;
715 	struct xfs_dir2_sf_entry	*next_sfep;
716 	char				*endp;
717 	struct xfs_ifork		*ifp;
718 	xfs_ino_t			ino;
719 	int				i;
720 	int				i8count;
721 	int				offset;
722 	int64_t				size;
723 	int				error;
724 	uint8_t				filetype;
725 
726 	ASSERT(ip->i_d.di_format == XFS_DINODE_FMT_LOCAL);
727 
728 	ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
729 	sfp = (struct xfs_dir2_sf_hdr *)ifp->if_u1.if_data;
730 	size = ifp->if_bytes;
731 
732 	/*
733 	 * Give up if the directory is way too short.
734 	 */
735 	if (size <= offsetof(struct xfs_dir2_sf_hdr, parent) ||
736 	    size < xfs_dir2_sf_hdr_size(sfp->i8count))
737 		return __this_address;
738 
739 	endp = (char *)sfp + size;
740 
741 	/* Check .. entry */
742 	ino = xfs_dir2_sf_get_parent_ino(sfp);
743 	i8count = ino > XFS_DIR2_MAX_SHORT_INUM;
744 	error = xfs_dir_ino_validate(mp, ino);
745 	if (error)
746 		return __this_address;
747 	offset = mp->m_dir_geo->data_first_offset;
748 
749 	/* Check all reported entries */
750 	sfep = xfs_dir2_sf_firstentry(sfp);
751 	for (i = 0; i < sfp->count; i++) {
752 		/*
753 		 * struct xfs_dir2_sf_entry has a variable length.
754 		 * Check the fixed-offset parts of the structure are
755 		 * within the data buffer.
756 		 */
757 		if (((char *)sfep + sizeof(*sfep)) >= endp)
758 			return __this_address;
759 
760 		/* Don't allow names with known bad length. */
761 		if (sfep->namelen == 0)
762 			return __this_address;
763 
764 		/*
765 		 * Check that the variable-length part of the structure is
766 		 * within the data buffer.  The next entry starts after the
767 		 * name component, so nextentry is an acceptable test.
768 		 */
769 		next_sfep = xfs_dir2_sf_nextentry(mp, sfp, sfep);
770 		if (endp < (char *)next_sfep)
771 			return __this_address;
772 
773 		/* Check that the offsets always increase. */
774 		if (xfs_dir2_sf_get_offset(sfep) < offset)
775 			return __this_address;
776 
777 		/* Check the inode number. */
778 		ino = xfs_dir2_sf_get_ino(mp, sfp, sfep);
779 		i8count += ino > XFS_DIR2_MAX_SHORT_INUM;
780 		error = xfs_dir_ino_validate(mp, ino);
781 		if (error)
782 			return __this_address;
783 
784 		/* Check the file type. */
785 		filetype = xfs_dir2_sf_get_ftype(mp, sfep);
786 		if (filetype >= XFS_DIR3_FT_MAX)
787 			return __this_address;
788 
789 		offset = xfs_dir2_sf_get_offset(sfep) +
790 				xfs_dir2_data_entsize(mp, sfep->namelen);
791 
792 		sfep = next_sfep;
793 	}
794 	if (i8count != sfp->i8count)
795 		return __this_address;
796 	if ((void *)sfep != (void *)endp)
797 		return __this_address;
798 
799 	/* Make sure this whole thing ought to be in local format. */
800 	if (offset + (sfp->count + 2) * (uint)sizeof(xfs_dir2_leaf_entry_t) +
801 	    (uint)sizeof(xfs_dir2_block_tail_t) > mp->m_dir_geo->blksize)
802 		return __this_address;
803 
804 	return NULL;
805 }
806 
807 /*
808  * Create a new (shortform) directory.
809  */
810 int					/* error, always 0 */
811 xfs_dir2_sf_create(
812 	xfs_da_args_t	*args,		/* operation arguments */
813 	xfs_ino_t	pino)		/* parent inode number */
814 {
815 	xfs_inode_t	*dp;		/* incore directory inode */
816 	int		i8count;	/* parent inode is an 8-byte number */
817 	xfs_dir2_sf_hdr_t *sfp;		/* shortform structure */
818 	int		size;		/* directory size */
819 
820 	trace_xfs_dir2_sf_create(args);
821 
822 	dp = args->dp;
823 
824 	ASSERT(dp != NULL);
825 	ASSERT(dp->i_d.di_size == 0);
826 	/*
827 	 * If it's currently a zero-length extent file,
828 	 * convert it to local format.
829 	 */
830 	if (dp->i_d.di_format == XFS_DINODE_FMT_EXTENTS) {
831 		dp->i_df.if_flags &= ~XFS_IFEXTENTS;	/* just in case */
832 		dp->i_d.di_format = XFS_DINODE_FMT_LOCAL;
833 		xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE);
834 		dp->i_df.if_flags |= XFS_IFINLINE;
835 	}
836 	ASSERT(dp->i_df.if_flags & XFS_IFINLINE);
837 	ASSERT(dp->i_df.if_bytes == 0);
838 	i8count = pino > XFS_DIR2_MAX_SHORT_INUM;
839 	size = xfs_dir2_sf_hdr_size(i8count);
840 	/*
841 	 * Make a buffer for the data.
842 	 */
843 	xfs_idata_realloc(dp, size, XFS_DATA_FORK);
844 	/*
845 	 * Fill in the header,
846 	 */
847 	sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
848 	sfp->i8count = i8count;
849 	/*
850 	 * Now can put in the inode number, since i8count is set.
851 	 */
852 	xfs_dir2_sf_put_parent_ino(sfp, pino);
853 	sfp->count = 0;
854 	dp->i_d.di_size = size;
855 	xfs_dir2_sf_check(args);
856 	xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE | XFS_ILOG_DDATA);
857 	return 0;
858 }
859 
860 /*
861  * Lookup an entry in a shortform directory.
862  * Returns EEXIST if found, ENOENT if not found.
863  */
864 int						/* error */
865 xfs_dir2_sf_lookup(
866 	xfs_da_args_t		*args)		/* operation arguments */
867 {
868 	struct xfs_inode	*dp = args->dp;
869 	struct xfs_mount	*mp = dp->i_mount;
870 	int			i;		/* entry index */
871 	int			error;
872 	xfs_dir2_sf_entry_t	*sfep;		/* shortform directory entry */
873 	xfs_dir2_sf_hdr_t	*sfp;		/* shortform structure */
874 	enum xfs_dacmp		cmp;		/* comparison result */
875 	xfs_dir2_sf_entry_t	*ci_sfep;	/* case-insens. entry */
876 
877 	trace_xfs_dir2_sf_lookup(args);
878 
879 	xfs_dir2_sf_check(args);
880 
881 	ASSERT(dp->i_df.if_flags & XFS_IFINLINE);
882 	ASSERT(dp->i_d.di_size >= offsetof(struct xfs_dir2_sf_hdr, parent));
883 	ASSERT(dp->i_df.if_bytes == dp->i_d.di_size);
884 	ASSERT(dp->i_df.if_u1.if_data != NULL);
885 	sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
886 	ASSERT(dp->i_d.di_size >= xfs_dir2_sf_hdr_size(sfp->i8count));
887 	/*
888 	 * Special case for .
889 	 */
890 	if (args->namelen == 1 && args->name[0] == '.') {
891 		args->inumber = dp->i_ino;
892 		args->cmpresult = XFS_CMP_EXACT;
893 		args->filetype = XFS_DIR3_FT_DIR;
894 		return -EEXIST;
895 	}
896 	/*
897 	 * Special case for ..
898 	 */
899 	if (args->namelen == 2 &&
900 	    args->name[0] == '.' && args->name[1] == '.') {
901 		args->inumber = xfs_dir2_sf_get_parent_ino(sfp);
902 		args->cmpresult = XFS_CMP_EXACT;
903 		args->filetype = XFS_DIR3_FT_DIR;
904 		return -EEXIST;
905 	}
906 	/*
907 	 * Loop over all the entries trying to match ours.
908 	 */
909 	ci_sfep = NULL;
910 	for (i = 0, sfep = xfs_dir2_sf_firstentry(sfp); i < sfp->count;
911 	     i++, sfep = xfs_dir2_sf_nextentry(mp, sfp, sfep)) {
912 		/*
913 		 * Compare name and if it's an exact match, return the inode
914 		 * number. If it's the first case-insensitive match, store the
915 		 * inode number and continue looking for an exact match.
916 		 */
917 		cmp = xfs_dir2_compname(args, sfep->name, sfep->namelen);
918 		if (cmp != XFS_CMP_DIFFERENT && cmp != args->cmpresult) {
919 			args->cmpresult = cmp;
920 			args->inumber = xfs_dir2_sf_get_ino(mp, sfp, sfep);
921 			args->filetype = xfs_dir2_sf_get_ftype(mp, sfep);
922 			if (cmp == XFS_CMP_EXACT)
923 				return -EEXIST;
924 			ci_sfep = sfep;
925 		}
926 	}
927 	ASSERT(args->op_flags & XFS_DA_OP_OKNOENT);
928 	/*
929 	 * Here, we can only be doing a lookup (not a rename or replace).
930 	 * If a case-insensitive match was not found, return -ENOENT.
931 	 */
932 	if (!ci_sfep)
933 		return -ENOENT;
934 	/* otherwise process the CI match as required by the caller */
935 	error = xfs_dir_cilookup_result(args, ci_sfep->name, ci_sfep->namelen);
936 	return error;
937 }
938 
939 /*
940  * Remove an entry from a shortform directory.
941  */
942 int						/* error */
943 xfs_dir2_sf_removename(
944 	xfs_da_args_t		*args)
945 {
946 	struct xfs_inode	*dp = args->dp;
947 	struct xfs_mount	*mp = dp->i_mount;
948 	int			byteoff;	/* offset of removed entry */
949 	int			entsize;	/* this entry's size */
950 	int			i;		/* shortform entry index */
951 	int			newsize;	/* new inode size */
952 	int			oldsize;	/* old inode size */
953 	xfs_dir2_sf_entry_t	*sfep;		/* shortform directory entry */
954 	xfs_dir2_sf_hdr_t	*sfp;		/* shortform structure */
955 
956 	trace_xfs_dir2_sf_removename(args);
957 
958 	ASSERT(dp->i_df.if_flags & XFS_IFINLINE);
959 	oldsize = (int)dp->i_d.di_size;
960 	ASSERT(oldsize >= offsetof(struct xfs_dir2_sf_hdr, parent));
961 	ASSERT(dp->i_df.if_bytes == oldsize);
962 	ASSERT(dp->i_df.if_u1.if_data != NULL);
963 	sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
964 	ASSERT(oldsize >= xfs_dir2_sf_hdr_size(sfp->i8count));
965 	/*
966 	 * Loop over the old directory entries.
967 	 * Find the one we're deleting.
968 	 */
969 	for (i = 0, sfep = xfs_dir2_sf_firstentry(sfp); i < sfp->count;
970 	     i++, sfep = xfs_dir2_sf_nextentry(mp, sfp, sfep)) {
971 		if (xfs_da_compname(args, sfep->name, sfep->namelen) ==
972 								XFS_CMP_EXACT) {
973 			ASSERT(xfs_dir2_sf_get_ino(mp, sfp, sfep) ==
974 			       args->inumber);
975 			break;
976 		}
977 	}
978 	/*
979 	 * Didn't find it.
980 	 */
981 	if (i == sfp->count)
982 		return -ENOENT;
983 	/*
984 	 * Calculate sizes.
985 	 */
986 	byteoff = (int)((char *)sfep - (char *)sfp);
987 	entsize = xfs_dir2_sf_entsize(mp, sfp, args->namelen);
988 	newsize = oldsize - entsize;
989 	/*
990 	 * Copy the part if any after the removed entry, sliding it down.
991 	 */
992 	if (byteoff + entsize < oldsize)
993 		memmove((char *)sfp + byteoff, (char *)sfp + byteoff + entsize,
994 			oldsize - (byteoff + entsize));
995 	/*
996 	 * Fix up the header and file size.
997 	 */
998 	sfp->count--;
999 	dp->i_d.di_size = newsize;
1000 	/*
1001 	 * Reallocate, making it smaller.
1002 	 */
1003 	xfs_idata_realloc(dp, newsize - oldsize, XFS_DATA_FORK);
1004 	sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
1005 	/*
1006 	 * Are we changing inode number size?
1007 	 */
1008 	if (args->inumber > XFS_DIR2_MAX_SHORT_INUM) {
1009 		if (sfp->i8count == 1)
1010 			xfs_dir2_sf_toino4(args);
1011 		else
1012 			sfp->i8count--;
1013 	}
1014 	xfs_dir2_sf_check(args);
1015 	xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE | XFS_ILOG_DDATA);
1016 	return 0;
1017 }
1018 
1019 /*
1020  * Check whether the sf dir replace operation need more blocks.
1021  */
1022 bool
1023 xfs_dir2_sf_replace_needblock(
1024 	struct xfs_inode	*dp,
1025 	xfs_ino_t		inum)
1026 {
1027 	int			newsize;
1028 	struct xfs_dir2_sf_hdr	*sfp;
1029 
1030 	if (dp->i_d.di_format != XFS_DINODE_FMT_LOCAL)
1031 		return false;
1032 
1033 	sfp = (struct xfs_dir2_sf_hdr *)dp->i_df.if_u1.if_data;
1034 	newsize = dp->i_df.if_bytes + (sfp->count + 1) * XFS_INO64_DIFF;
1035 
1036 	return inum > XFS_DIR2_MAX_SHORT_INUM &&
1037 	       sfp->i8count == 0 && newsize > XFS_IFORK_DSIZE(dp);
1038 }
1039 
1040 /*
1041  * Replace the inode number of an entry in a shortform directory.
1042  */
1043 int						/* error */
1044 xfs_dir2_sf_replace(
1045 	xfs_da_args_t		*args)		/* operation arguments */
1046 {
1047 	struct xfs_inode	*dp = args->dp;
1048 	struct xfs_mount	*mp = dp->i_mount;
1049 	int			i;		/* entry index */
1050 	xfs_ino_t		ino=0;		/* entry old inode number */
1051 	int			i8elevated;	/* sf_toino8 set i8count=1 */
1052 	xfs_dir2_sf_entry_t	*sfep;		/* shortform directory entry */
1053 	xfs_dir2_sf_hdr_t	*sfp;		/* shortform structure */
1054 
1055 	trace_xfs_dir2_sf_replace(args);
1056 
1057 	ASSERT(dp->i_df.if_flags & XFS_IFINLINE);
1058 	ASSERT(dp->i_d.di_size >= offsetof(struct xfs_dir2_sf_hdr, parent));
1059 	ASSERT(dp->i_df.if_bytes == dp->i_d.di_size);
1060 	ASSERT(dp->i_df.if_u1.if_data != NULL);
1061 	sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
1062 	ASSERT(dp->i_d.di_size >= xfs_dir2_sf_hdr_size(sfp->i8count));
1063 
1064 	/*
1065 	 * New inode number is large, and need to convert to 8-byte inodes.
1066 	 */
1067 	if (args->inumber > XFS_DIR2_MAX_SHORT_INUM && sfp->i8count == 0) {
1068 		int	error;			/* error return value */
1069 
1070 		/*
1071 		 * Won't fit as shortform, convert to block then do replace.
1072 		 */
1073 		if (xfs_dir2_sf_replace_needblock(dp, args->inumber)) {
1074 			error = xfs_dir2_sf_to_block(args);
1075 			if (error)
1076 				return error;
1077 			return xfs_dir2_block_replace(args);
1078 		}
1079 		/*
1080 		 * Still fits, convert to 8-byte now.
1081 		 */
1082 		xfs_dir2_sf_toino8(args);
1083 		i8elevated = 1;
1084 		sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
1085 	} else
1086 		i8elevated = 0;
1087 
1088 	ASSERT(args->namelen != 1 || args->name[0] != '.');
1089 	/*
1090 	 * Replace ..'s entry.
1091 	 */
1092 	if (args->namelen == 2 &&
1093 	    args->name[0] == '.' && args->name[1] == '.') {
1094 		ino = xfs_dir2_sf_get_parent_ino(sfp);
1095 		ASSERT(args->inumber != ino);
1096 		xfs_dir2_sf_put_parent_ino(sfp, args->inumber);
1097 	}
1098 	/*
1099 	 * Normal entry, look for the name.
1100 	 */
1101 	else {
1102 		for (i = 0, sfep = xfs_dir2_sf_firstentry(sfp); i < sfp->count;
1103 		     i++, sfep = xfs_dir2_sf_nextentry(mp, sfp, sfep)) {
1104 			if (xfs_da_compname(args, sfep->name, sfep->namelen) ==
1105 								XFS_CMP_EXACT) {
1106 				ino = xfs_dir2_sf_get_ino(mp, sfp, sfep);
1107 				ASSERT(args->inumber != ino);
1108 				xfs_dir2_sf_put_ino(mp, sfp, sfep,
1109 						args->inumber);
1110 				xfs_dir2_sf_put_ftype(mp, sfep, args->filetype);
1111 				break;
1112 			}
1113 		}
1114 		/*
1115 		 * Didn't find it.
1116 		 */
1117 		if (i == sfp->count) {
1118 			ASSERT(args->op_flags & XFS_DA_OP_OKNOENT);
1119 			if (i8elevated)
1120 				xfs_dir2_sf_toino4(args);
1121 			return -ENOENT;
1122 		}
1123 	}
1124 	/*
1125 	 * See if the old number was large, the new number is small.
1126 	 */
1127 	if (ino > XFS_DIR2_MAX_SHORT_INUM &&
1128 	    args->inumber <= XFS_DIR2_MAX_SHORT_INUM) {
1129 		/*
1130 		 * And the old count was one, so need to convert to small.
1131 		 */
1132 		if (sfp->i8count == 1)
1133 			xfs_dir2_sf_toino4(args);
1134 		else
1135 			sfp->i8count--;
1136 	}
1137 	/*
1138 	 * See if the old number was small, the new number is large.
1139 	 */
1140 	if (ino <= XFS_DIR2_MAX_SHORT_INUM &&
1141 	    args->inumber > XFS_DIR2_MAX_SHORT_INUM) {
1142 		/*
1143 		 * add to the i8count unless we just converted to 8-byte
1144 		 * inodes (which does an implied i8count = 1)
1145 		 */
1146 		ASSERT(sfp->i8count != 0);
1147 		if (!i8elevated)
1148 			sfp->i8count++;
1149 	}
1150 	xfs_dir2_sf_check(args);
1151 	xfs_trans_log_inode(args->trans, dp, XFS_ILOG_DDATA);
1152 	return 0;
1153 }
1154 
1155 /*
1156  * Convert from 8-byte inode numbers to 4-byte inode numbers.
1157  * The last 8-byte inode number is gone, but the count is still 1.
1158  */
1159 static void
1160 xfs_dir2_sf_toino4(
1161 	xfs_da_args_t		*args)		/* operation arguments */
1162 {
1163 	struct xfs_inode	*dp = args->dp;
1164 	struct xfs_mount	*mp = dp->i_mount;
1165 	char			*buf;		/* old dir's buffer */
1166 	int			i;		/* entry index */
1167 	int			newsize;	/* new inode size */
1168 	xfs_dir2_sf_entry_t	*oldsfep;	/* old sf entry */
1169 	xfs_dir2_sf_hdr_t	*oldsfp;	/* old sf directory */
1170 	int			oldsize;	/* old inode size */
1171 	xfs_dir2_sf_entry_t	*sfep;		/* new sf entry */
1172 	xfs_dir2_sf_hdr_t	*sfp;		/* new sf directory */
1173 
1174 	trace_xfs_dir2_sf_toino4(args);
1175 
1176 	/*
1177 	 * Copy the old directory to the buffer.
1178 	 * Then nuke it from the inode, and add the new buffer to the inode.
1179 	 * Don't want xfs_idata_realloc copying the data here.
1180 	 */
1181 	oldsize = dp->i_df.if_bytes;
1182 	buf = kmem_alloc(oldsize, 0);
1183 	oldsfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
1184 	ASSERT(oldsfp->i8count == 1);
1185 	memcpy(buf, oldsfp, oldsize);
1186 	/*
1187 	 * Compute the new inode size.
1188 	 */
1189 	newsize = oldsize - (oldsfp->count + 1) * XFS_INO64_DIFF;
1190 	xfs_idata_realloc(dp, -oldsize, XFS_DATA_FORK);
1191 	xfs_idata_realloc(dp, newsize, XFS_DATA_FORK);
1192 	/*
1193 	 * Reset our pointers, the data has moved.
1194 	 */
1195 	oldsfp = (xfs_dir2_sf_hdr_t *)buf;
1196 	sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
1197 	/*
1198 	 * Fill in the new header.
1199 	 */
1200 	sfp->count = oldsfp->count;
1201 	sfp->i8count = 0;
1202 	xfs_dir2_sf_put_parent_ino(sfp, xfs_dir2_sf_get_parent_ino(oldsfp));
1203 	/*
1204 	 * Copy the entries field by field.
1205 	 */
1206 	for (i = 0, sfep = xfs_dir2_sf_firstentry(sfp),
1207 		    oldsfep = xfs_dir2_sf_firstentry(oldsfp);
1208 	     i < sfp->count;
1209 	     i++, sfep = xfs_dir2_sf_nextentry(mp, sfp, sfep),
1210 		  oldsfep = xfs_dir2_sf_nextentry(mp, oldsfp, oldsfep)) {
1211 		sfep->namelen = oldsfep->namelen;
1212 		memcpy(sfep->offset, oldsfep->offset, sizeof(sfep->offset));
1213 		memcpy(sfep->name, oldsfep->name, sfep->namelen);
1214 		xfs_dir2_sf_put_ino(mp, sfp, sfep,
1215 				xfs_dir2_sf_get_ino(mp, oldsfp, oldsfep));
1216 		xfs_dir2_sf_put_ftype(mp, sfep,
1217 				xfs_dir2_sf_get_ftype(mp, oldsfep));
1218 	}
1219 	/*
1220 	 * Clean up the inode.
1221 	 */
1222 	kmem_free(buf);
1223 	dp->i_d.di_size = newsize;
1224 	xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE | XFS_ILOG_DDATA);
1225 }
1226 
1227 /*
1228  * Convert existing entries from 4-byte inode numbers to 8-byte inode numbers.
1229  * The new entry w/ an 8-byte inode number is not there yet; we leave with
1230  * i8count set to 1, but no corresponding 8-byte entry.
1231  */
1232 static void
1233 xfs_dir2_sf_toino8(
1234 	xfs_da_args_t		*args)		/* operation arguments */
1235 {
1236 	struct xfs_inode	*dp = args->dp;
1237 	struct xfs_mount	*mp = dp->i_mount;
1238 	char			*buf;		/* old dir's buffer */
1239 	int			i;		/* entry index */
1240 	int			newsize;	/* new inode size */
1241 	xfs_dir2_sf_entry_t	*oldsfep;	/* old sf entry */
1242 	xfs_dir2_sf_hdr_t	*oldsfp;	/* old sf directory */
1243 	int			oldsize;	/* old inode size */
1244 	xfs_dir2_sf_entry_t	*sfep;		/* new sf entry */
1245 	xfs_dir2_sf_hdr_t	*sfp;		/* new sf directory */
1246 
1247 	trace_xfs_dir2_sf_toino8(args);
1248 
1249 	/*
1250 	 * Copy the old directory to the buffer.
1251 	 * Then nuke it from the inode, and add the new buffer to the inode.
1252 	 * Don't want xfs_idata_realloc copying the data here.
1253 	 */
1254 	oldsize = dp->i_df.if_bytes;
1255 	buf = kmem_alloc(oldsize, 0);
1256 	oldsfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
1257 	ASSERT(oldsfp->i8count == 0);
1258 	memcpy(buf, oldsfp, oldsize);
1259 	/*
1260 	 * Compute the new inode size (nb: entry count + 1 for parent)
1261 	 */
1262 	newsize = oldsize + (oldsfp->count + 1) * XFS_INO64_DIFF;
1263 	xfs_idata_realloc(dp, -oldsize, XFS_DATA_FORK);
1264 	xfs_idata_realloc(dp, newsize, XFS_DATA_FORK);
1265 	/*
1266 	 * Reset our pointers, the data has moved.
1267 	 */
1268 	oldsfp = (xfs_dir2_sf_hdr_t *)buf;
1269 	sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
1270 	/*
1271 	 * Fill in the new header.
1272 	 */
1273 	sfp->count = oldsfp->count;
1274 	sfp->i8count = 1;
1275 	xfs_dir2_sf_put_parent_ino(sfp, xfs_dir2_sf_get_parent_ino(oldsfp));
1276 	/*
1277 	 * Copy the entries field by field.
1278 	 */
1279 	for (i = 0, sfep = xfs_dir2_sf_firstentry(sfp),
1280 		    oldsfep = xfs_dir2_sf_firstentry(oldsfp);
1281 	     i < sfp->count;
1282 	     i++, sfep = xfs_dir2_sf_nextentry(mp, sfp, sfep),
1283 		  oldsfep = xfs_dir2_sf_nextentry(mp, oldsfp, oldsfep)) {
1284 		sfep->namelen = oldsfep->namelen;
1285 		memcpy(sfep->offset, oldsfep->offset, sizeof(sfep->offset));
1286 		memcpy(sfep->name, oldsfep->name, sfep->namelen);
1287 		xfs_dir2_sf_put_ino(mp, sfp, sfep,
1288 				xfs_dir2_sf_get_ino(mp, oldsfp, oldsfep));
1289 		xfs_dir2_sf_put_ftype(mp, sfep,
1290 				xfs_dir2_sf_get_ftype(mp, oldsfep));
1291 	}
1292 	/*
1293 	 * Clean up the inode.
1294 	 */
1295 	kmem_free(buf);
1296 	dp->i_d.di_size = newsize;
1297 	xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE | XFS_ILOG_DDATA);
1298 }
1299