xref: /linux/fs/xfs/libxfs/xfs_dir2_leaf.c (revision 71dfa617ea9f18e4585fe78364217cd32b1fc382)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (c) 2000-2003,2005 Silicon Graphics, Inc.
4  * Copyright (c) 2013 Red Hat, Inc.
5  * All Rights Reserved.
6  */
7 #include "xfs.h"
8 #include "xfs_fs.h"
9 #include "xfs_shared.h"
10 #include "xfs_format.h"
11 #include "xfs_log_format.h"
12 #include "xfs_trans_resv.h"
13 #include "xfs_mount.h"
14 #include "xfs_inode.h"
15 #include "xfs_bmap.h"
16 #include "xfs_dir2.h"
17 #include "xfs_dir2_priv.h"
18 #include "xfs_error.h"
19 #include "xfs_trace.h"
20 #include "xfs_trans.h"
21 #include "xfs_buf_item.h"
22 #include "xfs_health.h"
23 
24 /*
25  * Local function declarations.
26  */
27 static int xfs_dir2_leaf_lookup_int(xfs_da_args_t *args, struct xfs_buf **lbpp,
28 				    int *indexp, struct xfs_buf **dbpp,
29 				    struct xfs_dir3_icleaf_hdr *leafhdr);
30 static void xfs_dir3_leaf_log_bests(struct xfs_da_args *args,
31 				    struct xfs_buf *bp, int first, int last);
32 static void xfs_dir3_leaf_log_tail(struct xfs_da_args *args,
33 				   struct xfs_buf *bp);
34 
35 void
36 xfs_dir2_leaf_hdr_from_disk(
37 	struct xfs_mount		*mp,
38 	struct xfs_dir3_icleaf_hdr	*to,
39 	struct xfs_dir2_leaf		*from)
40 {
41 	if (xfs_has_crc(mp)) {
42 		struct xfs_dir3_leaf *from3 = (struct xfs_dir3_leaf *)from;
43 
44 		to->forw = be32_to_cpu(from3->hdr.info.hdr.forw);
45 		to->back = be32_to_cpu(from3->hdr.info.hdr.back);
46 		to->magic = be16_to_cpu(from3->hdr.info.hdr.magic);
47 		to->count = be16_to_cpu(from3->hdr.count);
48 		to->stale = be16_to_cpu(from3->hdr.stale);
49 		to->ents = from3->__ents;
50 
51 		ASSERT(to->magic == XFS_DIR3_LEAF1_MAGIC ||
52 		       to->magic == XFS_DIR3_LEAFN_MAGIC);
53 	} else {
54 		to->forw = be32_to_cpu(from->hdr.info.forw);
55 		to->back = be32_to_cpu(from->hdr.info.back);
56 		to->magic = be16_to_cpu(from->hdr.info.magic);
57 		to->count = be16_to_cpu(from->hdr.count);
58 		to->stale = be16_to_cpu(from->hdr.stale);
59 		to->ents = from->__ents;
60 
61 		ASSERT(to->magic == XFS_DIR2_LEAF1_MAGIC ||
62 		       to->magic == XFS_DIR2_LEAFN_MAGIC);
63 	}
64 }
65 
66 void
67 xfs_dir2_leaf_hdr_to_disk(
68 	struct xfs_mount		*mp,
69 	struct xfs_dir2_leaf		*to,
70 	struct xfs_dir3_icleaf_hdr	*from)
71 {
72 	if (xfs_has_crc(mp)) {
73 		struct xfs_dir3_leaf *to3 = (struct xfs_dir3_leaf *)to;
74 
75 		ASSERT(from->magic == XFS_DIR3_LEAF1_MAGIC ||
76 		       from->magic == XFS_DIR3_LEAFN_MAGIC);
77 
78 		to3->hdr.info.hdr.forw = cpu_to_be32(from->forw);
79 		to3->hdr.info.hdr.back = cpu_to_be32(from->back);
80 		to3->hdr.info.hdr.magic = cpu_to_be16(from->magic);
81 		to3->hdr.count = cpu_to_be16(from->count);
82 		to3->hdr.stale = cpu_to_be16(from->stale);
83 	} else {
84 		ASSERT(from->magic == XFS_DIR2_LEAF1_MAGIC ||
85 		       from->magic == XFS_DIR2_LEAFN_MAGIC);
86 
87 		to->hdr.info.forw = cpu_to_be32(from->forw);
88 		to->hdr.info.back = cpu_to_be32(from->back);
89 		to->hdr.info.magic = cpu_to_be16(from->magic);
90 		to->hdr.count = cpu_to_be16(from->count);
91 		to->hdr.stale = cpu_to_be16(from->stale);
92 	}
93 }
94 
95 /*
96  * Check the internal consistency of a leaf1 block.
97  * Pop an assert if something is wrong.
98  */
99 #ifdef DEBUG
100 static xfs_failaddr_t
101 xfs_dir3_leaf1_check(
102 	struct xfs_inode	*dp,
103 	struct xfs_buf		*bp)
104 {
105 	struct xfs_dir2_leaf	*leaf = bp->b_addr;
106 	struct xfs_dir3_icleaf_hdr leafhdr;
107 
108 	xfs_dir2_leaf_hdr_from_disk(dp->i_mount, &leafhdr, leaf);
109 
110 	if (leafhdr.magic == XFS_DIR3_LEAF1_MAGIC) {
111 		struct xfs_dir3_leaf_hdr *leaf3 = bp->b_addr;
112 		if (be64_to_cpu(leaf3->info.blkno) != xfs_buf_daddr(bp))
113 			return __this_address;
114 	} else if (leafhdr.magic != XFS_DIR2_LEAF1_MAGIC)
115 		return __this_address;
116 
117 	return xfs_dir3_leaf_check_int(dp->i_mount, &leafhdr, leaf, false);
118 }
119 
120 static inline void
121 xfs_dir3_leaf_check(
122 	struct xfs_inode	*dp,
123 	struct xfs_buf		*bp)
124 {
125 	xfs_failaddr_t		fa;
126 
127 	fa = xfs_dir3_leaf1_check(dp, bp);
128 	if (!fa)
129 		return;
130 	xfs_corruption_error(__func__, XFS_ERRLEVEL_LOW, dp->i_mount,
131 			bp->b_addr, BBTOB(bp->b_length), __FILE__, __LINE__,
132 			fa);
133 	ASSERT(0);
134 }
135 #else
136 #define	xfs_dir3_leaf_check(dp, bp)
137 #endif
138 
139 xfs_failaddr_t
140 xfs_dir3_leaf_check_int(
141 	struct xfs_mount		*mp,
142 	struct xfs_dir3_icleaf_hdr	*hdr,
143 	struct xfs_dir2_leaf		*leaf,
144 	bool				expensive_checking)
145 {
146 	struct xfs_da_geometry		*geo = mp->m_dir_geo;
147 	xfs_dir2_leaf_tail_t		*ltp;
148 	int				stale;
149 	int				i;
150 	bool				isleaf1 = (hdr->magic == XFS_DIR2_LEAF1_MAGIC ||
151 						   hdr->magic == XFS_DIR3_LEAF1_MAGIC);
152 
153 	ltp = xfs_dir2_leaf_tail_p(geo, leaf);
154 
155 	/*
156 	 * XXX (dgc): This value is not restrictive enough.
157 	 * Should factor in the size of the bests table as well.
158 	 * We can deduce a value for that from i_disk_size.
159 	 */
160 	if (hdr->count > geo->leaf_max_ents)
161 		return __this_address;
162 
163 	/* Leaves and bests don't overlap in leaf format. */
164 	if (isleaf1 &&
165 	    (char *)&hdr->ents[hdr->count] > (char *)xfs_dir2_leaf_bests_p(ltp))
166 		return __this_address;
167 
168 	if (!expensive_checking)
169 		return NULL;
170 
171 	/* Check hash value order, count stale entries.  */
172 	for (i = stale = 0; i < hdr->count; i++) {
173 		if (i + 1 < hdr->count) {
174 			if (be32_to_cpu(hdr->ents[i].hashval) >
175 					be32_to_cpu(hdr->ents[i + 1].hashval))
176 				return __this_address;
177 		}
178 		if (hdr->ents[i].address == cpu_to_be32(XFS_DIR2_NULL_DATAPTR))
179 			stale++;
180 		if (isleaf1 && xfs_dir2_dataptr_to_db(geo,
181 				be32_to_cpu(hdr->ents[i].address)) >=
182 				be32_to_cpu(ltp->bestcount))
183 			return __this_address;
184 	}
185 	if (hdr->stale != stale)
186 		return __this_address;
187 	return NULL;
188 }
189 
190 /*
191  * We verify the magic numbers before decoding the leaf header so that on debug
192  * kernels we don't get assertion failures in xfs_dir3_leaf_hdr_from_disk() due
193  * to incorrect magic numbers.
194  */
195 static xfs_failaddr_t
196 xfs_dir3_leaf_verify(
197 	struct xfs_buf			*bp)
198 {
199 	struct xfs_mount		*mp = bp->b_mount;
200 	struct xfs_dir3_icleaf_hdr	leafhdr;
201 	xfs_failaddr_t			fa;
202 
203 	fa = xfs_da3_blkinfo_verify(bp, bp->b_addr);
204 	if (fa)
205 		return fa;
206 
207 	xfs_dir2_leaf_hdr_from_disk(mp, &leafhdr, bp->b_addr);
208 	return xfs_dir3_leaf_check_int(mp, &leafhdr, bp->b_addr, true);
209 }
210 
211 static void
212 xfs_dir3_leaf_read_verify(
213 	struct xfs_buf  *bp)
214 {
215 	struct xfs_mount	*mp = bp->b_mount;
216 	xfs_failaddr_t		fa;
217 
218 	if (xfs_has_crc(mp) &&
219 	     !xfs_buf_verify_cksum(bp, XFS_DIR3_LEAF_CRC_OFF))
220 		xfs_verifier_error(bp, -EFSBADCRC, __this_address);
221 	else {
222 		fa = xfs_dir3_leaf_verify(bp);
223 		if (fa)
224 			xfs_verifier_error(bp, -EFSCORRUPTED, fa);
225 	}
226 }
227 
228 static void
229 xfs_dir3_leaf_write_verify(
230 	struct xfs_buf  *bp)
231 {
232 	struct xfs_mount	*mp = bp->b_mount;
233 	struct xfs_buf_log_item	*bip = bp->b_log_item;
234 	struct xfs_dir3_leaf_hdr *hdr3 = bp->b_addr;
235 	xfs_failaddr_t		fa;
236 
237 	fa = xfs_dir3_leaf_verify(bp);
238 	if (fa) {
239 		xfs_verifier_error(bp, -EFSCORRUPTED, fa);
240 		return;
241 	}
242 
243 	if (!xfs_has_crc(mp))
244 		return;
245 
246 	if (bip)
247 		hdr3->info.lsn = cpu_to_be64(bip->bli_item.li_lsn);
248 
249 	xfs_buf_update_cksum(bp, XFS_DIR3_LEAF_CRC_OFF);
250 }
251 
252 const struct xfs_buf_ops xfs_dir3_leaf1_buf_ops = {
253 	.name = "xfs_dir3_leaf1",
254 	.magic16 = { cpu_to_be16(XFS_DIR2_LEAF1_MAGIC),
255 		     cpu_to_be16(XFS_DIR3_LEAF1_MAGIC) },
256 	.verify_read = xfs_dir3_leaf_read_verify,
257 	.verify_write = xfs_dir3_leaf_write_verify,
258 	.verify_struct = xfs_dir3_leaf_verify,
259 };
260 
261 const struct xfs_buf_ops xfs_dir3_leafn_buf_ops = {
262 	.name = "xfs_dir3_leafn",
263 	.magic16 = { cpu_to_be16(XFS_DIR2_LEAFN_MAGIC),
264 		     cpu_to_be16(XFS_DIR3_LEAFN_MAGIC) },
265 	.verify_read = xfs_dir3_leaf_read_verify,
266 	.verify_write = xfs_dir3_leaf_write_verify,
267 	.verify_struct = xfs_dir3_leaf_verify,
268 };
269 
270 int
271 xfs_dir3_leaf_read(
272 	struct xfs_trans	*tp,
273 	struct xfs_inode	*dp,
274 	xfs_dablk_t		fbno,
275 	struct xfs_buf		**bpp)
276 {
277 	int			err;
278 
279 	err = xfs_da_read_buf(tp, dp, fbno, 0, bpp, XFS_DATA_FORK,
280 			&xfs_dir3_leaf1_buf_ops);
281 	if (!err && tp && *bpp)
282 		xfs_trans_buf_set_type(tp, *bpp, XFS_BLFT_DIR_LEAF1_BUF);
283 	return err;
284 }
285 
286 int
287 xfs_dir3_leafn_read(
288 	struct xfs_trans	*tp,
289 	struct xfs_inode	*dp,
290 	xfs_dablk_t		fbno,
291 	struct xfs_buf		**bpp)
292 {
293 	int			err;
294 
295 	err = xfs_da_read_buf(tp, dp, fbno, 0, bpp, XFS_DATA_FORK,
296 			&xfs_dir3_leafn_buf_ops);
297 	if (!err && tp && *bpp)
298 		xfs_trans_buf_set_type(tp, *bpp, XFS_BLFT_DIR_LEAFN_BUF);
299 	return err;
300 }
301 
302 /*
303  * Initialize a new leaf block, leaf1 or leafn magic accepted.
304  */
305 static void
306 xfs_dir3_leaf_init(
307 	struct xfs_mount	*mp,
308 	struct xfs_trans	*tp,
309 	struct xfs_buf		*bp,
310 	xfs_ino_t		owner,
311 	uint16_t		type)
312 {
313 	struct xfs_dir2_leaf	*leaf = bp->b_addr;
314 
315 	ASSERT(type == XFS_DIR2_LEAF1_MAGIC || type == XFS_DIR2_LEAFN_MAGIC);
316 
317 	if (xfs_has_crc(mp)) {
318 		struct xfs_dir3_leaf_hdr *leaf3 = bp->b_addr;
319 
320 		memset(leaf3, 0, sizeof(*leaf3));
321 
322 		leaf3->info.hdr.magic = (type == XFS_DIR2_LEAF1_MAGIC)
323 					 ? cpu_to_be16(XFS_DIR3_LEAF1_MAGIC)
324 					 : cpu_to_be16(XFS_DIR3_LEAFN_MAGIC);
325 		leaf3->info.blkno = cpu_to_be64(xfs_buf_daddr(bp));
326 		leaf3->info.owner = cpu_to_be64(owner);
327 		uuid_copy(&leaf3->info.uuid, &mp->m_sb.sb_meta_uuid);
328 	} else {
329 		memset(leaf, 0, sizeof(*leaf));
330 		leaf->hdr.info.magic = cpu_to_be16(type);
331 	}
332 
333 	/*
334 	 * If it's a leaf-format directory initialize the tail.
335 	 * Caller is responsible for initialising the bests table.
336 	 */
337 	if (type == XFS_DIR2_LEAF1_MAGIC) {
338 		struct xfs_dir2_leaf_tail *ltp;
339 
340 		ltp = xfs_dir2_leaf_tail_p(mp->m_dir_geo, leaf);
341 		ltp->bestcount = 0;
342 		bp->b_ops = &xfs_dir3_leaf1_buf_ops;
343 		xfs_trans_buf_set_type(tp, bp, XFS_BLFT_DIR_LEAF1_BUF);
344 	} else {
345 		bp->b_ops = &xfs_dir3_leafn_buf_ops;
346 		xfs_trans_buf_set_type(tp, bp, XFS_BLFT_DIR_LEAFN_BUF);
347 	}
348 }
349 
350 int
351 xfs_dir3_leaf_get_buf(
352 	xfs_da_args_t		*args,
353 	xfs_dir2_db_t		bno,
354 	struct xfs_buf		**bpp,
355 	uint16_t		magic)
356 {
357 	struct xfs_inode	*dp = args->dp;
358 	struct xfs_trans	*tp = args->trans;
359 	struct xfs_mount	*mp = dp->i_mount;
360 	struct xfs_buf		*bp;
361 	int			error;
362 
363 	ASSERT(magic == XFS_DIR2_LEAF1_MAGIC || magic == XFS_DIR2_LEAFN_MAGIC);
364 	ASSERT(bno >= xfs_dir2_byte_to_db(args->geo, XFS_DIR2_LEAF_OFFSET) &&
365 	       bno < xfs_dir2_byte_to_db(args->geo, XFS_DIR2_FREE_OFFSET));
366 
367 	error = xfs_da_get_buf(tp, dp, xfs_dir2_db_to_da(args->geo, bno),
368 			       &bp, XFS_DATA_FORK);
369 	if (error)
370 		return error;
371 
372 	xfs_dir3_leaf_init(mp, tp, bp, dp->i_ino, magic);
373 	xfs_dir3_leaf_log_header(args, bp);
374 	if (magic == XFS_DIR2_LEAF1_MAGIC)
375 		xfs_dir3_leaf_log_tail(args, bp);
376 	*bpp = bp;
377 	return 0;
378 }
379 
380 /*
381  * Convert a block form directory to a leaf form directory.
382  */
383 int						/* error */
384 xfs_dir2_block_to_leaf(
385 	xfs_da_args_t		*args,		/* operation arguments */
386 	struct xfs_buf		*dbp)		/* input block's buffer */
387 {
388 	__be16			*bestsp;	/* leaf's bestsp entries */
389 	xfs_dablk_t		blkno;		/* leaf block's bno */
390 	xfs_dir2_data_hdr_t	*hdr;		/* block header */
391 	xfs_dir2_leaf_entry_t	*blp;		/* block's leaf entries */
392 	xfs_dir2_block_tail_t	*btp;		/* block's tail */
393 	xfs_inode_t		*dp;		/* incore directory inode */
394 	int			error;		/* error return code */
395 	struct xfs_buf		*lbp;		/* leaf block's buffer */
396 	xfs_dir2_db_t		ldb;		/* leaf block's bno */
397 	xfs_dir2_leaf_t		*leaf;		/* leaf structure */
398 	xfs_dir2_leaf_tail_t	*ltp;		/* leaf's tail */
399 	int			needlog;	/* need to log block header */
400 	int			needscan;	/* need to rescan bestfree */
401 	xfs_trans_t		*tp;		/* transaction pointer */
402 	struct xfs_dir2_data_free *bf;
403 	struct xfs_dir3_icleaf_hdr leafhdr;
404 
405 	trace_xfs_dir2_block_to_leaf(args);
406 
407 	dp = args->dp;
408 	tp = args->trans;
409 	/*
410 	 * Add the leaf block to the inode.
411 	 * This interface will only put blocks in the leaf/node range.
412 	 * Since that's empty now, we'll get the root (block 0 in range).
413 	 */
414 	if ((error = xfs_da_grow_inode(args, &blkno))) {
415 		return error;
416 	}
417 	ldb = xfs_dir2_da_to_db(args->geo, blkno);
418 	ASSERT(ldb == xfs_dir2_byte_to_db(args->geo, XFS_DIR2_LEAF_OFFSET));
419 	/*
420 	 * Initialize the leaf block, get a buffer for it.
421 	 */
422 	error = xfs_dir3_leaf_get_buf(args, ldb, &lbp, XFS_DIR2_LEAF1_MAGIC);
423 	if (error)
424 		return error;
425 
426 	leaf = lbp->b_addr;
427 	hdr = dbp->b_addr;
428 	xfs_dir3_data_check(dp, dbp);
429 	btp = xfs_dir2_block_tail_p(args->geo, hdr);
430 	blp = xfs_dir2_block_leaf_p(btp);
431 	bf = xfs_dir2_data_bestfree_p(dp->i_mount, hdr);
432 
433 	/*
434 	 * Set the counts in the leaf header.
435 	 */
436 	xfs_dir2_leaf_hdr_from_disk(dp->i_mount, &leafhdr, leaf);
437 	leafhdr.count = be32_to_cpu(btp->count);
438 	leafhdr.stale = be32_to_cpu(btp->stale);
439 	xfs_dir2_leaf_hdr_to_disk(dp->i_mount, leaf, &leafhdr);
440 	xfs_dir3_leaf_log_header(args, lbp);
441 
442 	/*
443 	 * Could compact these but I think we always do the conversion
444 	 * after squeezing out stale entries.
445 	 */
446 	memcpy(leafhdr.ents, blp,
447 		be32_to_cpu(btp->count) * sizeof(struct xfs_dir2_leaf_entry));
448 	xfs_dir3_leaf_log_ents(args, &leafhdr, lbp, 0, leafhdr.count - 1);
449 	needscan = 0;
450 	needlog = 1;
451 	/*
452 	 * Make the space formerly occupied by the leaf entries and block
453 	 * tail be free.
454 	 */
455 	xfs_dir2_data_make_free(args, dbp,
456 		(xfs_dir2_data_aoff_t)((char *)blp - (char *)hdr),
457 		(xfs_dir2_data_aoff_t)((char *)hdr + args->geo->blksize -
458 				       (char *)blp),
459 		&needlog, &needscan);
460 	/*
461 	 * Fix up the block header, make it a data block.
462 	 */
463 	dbp->b_ops = &xfs_dir3_data_buf_ops;
464 	xfs_trans_buf_set_type(tp, dbp, XFS_BLFT_DIR_DATA_BUF);
465 	if (hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC))
466 		hdr->magic = cpu_to_be32(XFS_DIR2_DATA_MAGIC);
467 	else
468 		hdr->magic = cpu_to_be32(XFS_DIR3_DATA_MAGIC);
469 
470 	if (needscan)
471 		xfs_dir2_data_freescan(dp->i_mount, hdr, &needlog);
472 	/*
473 	 * Set up leaf tail and bests table.
474 	 */
475 	ltp = xfs_dir2_leaf_tail_p(args->geo, leaf);
476 	ltp->bestcount = cpu_to_be32(1);
477 	bestsp = xfs_dir2_leaf_bests_p(ltp);
478 	bestsp[0] =  bf[0].length;
479 	/*
480 	 * Log the data header and leaf bests table.
481 	 */
482 	if (needlog)
483 		xfs_dir2_data_log_header(args, dbp);
484 	xfs_dir3_leaf_check(dp, lbp);
485 	xfs_dir3_data_check(dp, dbp);
486 	xfs_dir3_leaf_log_bests(args, lbp, 0, 0);
487 	return 0;
488 }
489 
490 STATIC void
491 xfs_dir3_leaf_find_stale(
492 	struct xfs_dir3_icleaf_hdr *leafhdr,
493 	struct xfs_dir2_leaf_entry *ents,
494 	int			index,
495 	int			*lowstale,
496 	int			*highstale)
497 {
498 	/*
499 	 * Find the first stale entry before our index, if any.
500 	 */
501 	for (*lowstale = index - 1; *lowstale >= 0; --*lowstale) {
502 		if (ents[*lowstale].address ==
503 		    cpu_to_be32(XFS_DIR2_NULL_DATAPTR))
504 			break;
505 	}
506 
507 	/*
508 	 * Find the first stale entry at or after our index, if any.
509 	 * Stop if the result would require moving more entries than using
510 	 * lowstale.
511 	 */
512 	for (*highstale = index; *highstale < leafhdr->count; ++*highstale) {
513 		if (ents[*highstale].address ==
514 		    cpu_to_be32(XFS_DIR2_NULL_DATAPTR))
515 			break;
516 		if (*lowstale >= 0 && index - *lowstale <= *highstale - index)
517 			break;
518 	}
519 }
520 
521 struct xfs_dir2_leaf_entry *
522 xfs_dir3_leaf_find_entry(
523 	struct xfs_dir3_icleaf_hdr *leafhdr,
524 	struct xfs_dir2_leaf_entry *ents,
525 	int			index,		/* leaf table position */
526 	int			compact,	/* need to compact leaves */
527 	int			lowstale,	/* index of prev stale leaf */
528 	int			highstale,	/* index of next stale leaf */
529 	int			*lfloglow,	/* low leaf logging index */
530 	int			*lfloghigh)	/* high leaf logging index */
531 {
532 	if (!leafhdr->stale) {
533 		xfs_dir2_leaf_entry_t	*lep;	/* leaf entry table pointer */
534 
535 		/*
536 		 * Now we need to make room to insert the leaf entry.
537 		 *
538 		 * If there are no stale entries, just insert a hole at index.
539 		 */
540 		lep = &ents[index];
541 		if (index < leafhdr->count)
542 			memmove(lep + 1, lep,
543 				(leafhdr->count - index) * sizeof(*lep));
544 
545 		/*
546 		 * Record low and high logging indices for the leaf.
547 		 */
548 		*lfloglow = index;
549 		*lfloghigh = leafhdr->count++;
550 		return lep;
551 	}
552 
553 	/*
554 	 * There are stale entries.
555 	 *
556 	 * We will use one of them for the new entry.  It's probably not at
557 	 * the right location, so we'll have to shift some up or down first.
558 	 *
559 	 * If we didn't compact before, we need to find the nearest stale
560 	 * entries before and after our insertion point.
561 	 */
562 	if (compact == 0)
563 		xfs_dir3_leaf_find_stale(leafhdr, ents, index,
564 					 &lowstale, &highstale);
565 
566 	/*
567 	 * If the low one is better, use it.
568 	 */
569 	if (lowstale >= 0 &&
570 	    (highstale == leafhdr->count ||
571 	     index - lowstale - 1 < highstale - index)) {
572 		ASSERT(index - lowstale - 1 >= 0);
573 		ASSERT(ents[lowstale].address ==
574 		       cpu_to_be32(XFS_DIR2_NULL_DATAPTR));
575 
576 		/*
577 		 * Copy entries up to cover the stale entry and make room
578 		 * for the new entry.
579 		 */
580 		if (index - lowstale - 1 > 0) {
581 			memmove(&ents[lowstale], &ents[lowstale + 1],
582 				(index - lowstale - 1) *
583 					sizeof(xfs_dir2_leaf_entry_t));
584 		}
585 		*lfloglow = min(lowstale, *lfloglow);
586 		*lfloghigh = max(index - 1, *lfloghigh);
587 		leafhdr->stale--;
588 		return &ents[index - 1];
589 	}
590 
591 	/*
592 	 * The high one is better, so use that one.
593 	 */
594 	ASSERT(highstale - index >= 0);
595 	ASSERT(ents[highstale].address == cpu_to_be32(XFS_DIR2_NULL_DATAPTR));
596 
597 	/*
598 	 * Copy entries down to cover the stale entry and make room for the
599 	 * new entry.
600 	 */
601 	if (highstale - index > 0) {
602 		memmove(&ents[index + 1], &ents[index],
603 			(highstale - index) * sizeof(xfs_dir2_leaf_entry_t));
604 	}
605 	*lfloglow = min(index, *lfloglow);
606 	*lfloghigh = max(highstale, *lfloghigh);
607 	leafhdr->stale--;
608 	return &ents[index];
609 }
610 
611 /*
612  * Add an entry to a leaf form directory.
613  */
614 int						/* error */
615 xfs_dir2_leaf_addname(
616 	struct xfs_da_args	*args)		/* operation arguments */
617 {
618 	struct xfs_dir3_icleaf_hdr leafhdr;
619 	struct xfs_trans	*tp = args->trans;
620 	__be16			*bestsp;	/* freespace table in leaf */
621 	__be16			*tagp;		/* end of data entry */
622 	struct xfs_buf		*dbp;		/* data block buffer */
623 	struct xfs_buf		*lbp;		/* leaf's buffer */
624 	struct xfs_dir2_leaf	*leaf;		/* leaf structure */
625 	struct xfs_inode	*dp = args->dp;	/* incore directory inode */
626 	struct xfs_dir2_data_hdr *hdr;		/* data block header */
627 	struct xfs_dir2_data_entry *dep;	/* data block entry */
628 	struct xfs_dir2_leaf_entry *lep;	/* leaf entry table pointer */
629 	struct xfs_dir2_leaf_entry *ents;
630 	struct xfs_dir2_data_unused *dup;	/* data unused entry */
631 	struct xfs_dir2_leaf_tail *ltp;		/* leaf tail pointer */
632 	struct xfs_dir2_data_free *bf;		/* bestfree table */
633 	int			compact;	/* need to compact leaves */
634 	int			error;		/* error return value */
635 	int			grown;		/* allocated new data block */
636 	int			highstale = 0;	/* index of next stale leaf */
637 	int			i;		/* temporary, index */
638 	int			index;		/* leaf table position */
639 	int			length;		/* length of new entry */
640 	int			lfloglow;	/* low leaf logging index */
641 	int			lfloghigh;	/* high leaf logging index */
642 	int			lowstale = 0;	/* index of prev stale leaf */
643 	int			needbytes;	/* leaf block bytes needed */
644 	int			needlog;	/* need to log data header */
645 	int			needscan;	/* need to rescan data free */
646 	xfs_dir2_db_t		use_block;	/* data block number */
647 
648 	trace_xfs_dir2_leaf_addname(args);
649 
650 	error = xfs_dir3_leaf_read(tp, dp, args->geo->leafblk, &lbp);
651 	if (error)
652 		return error;
653 
654 	/*
655 	 * Look up the entry by hash value and name.
656 	 * We know it's not there, our caller has already done a lookup.
657 	 * So the index is of the entry to insert in front of.
658 	 * But if there are dup hash values the index is of the first of those.
659 	 */
660 	index = xfs_dir2_leaf_search_hash(args, lbp);
661 	leaf = lbp->b_addr;
662 	ltp = xfs_dir2_leaf_tail_p(args->geo, leaf);
663 	xfs_dir2_leaf_hdr_from_disk(dp->i_mount, &leafhdr, leaf);
664 	ents = leafhdr.ents;
665 	bestsp = xfs_dir2_leaf_bests_p(ltp);
666 	length = xfs_dir2_data_entsize(dp->i_mount, args->namelen);
667 
668 	/*
669 	 * See if there are any entries with the same hash value
670 	 * and space in their block for the new entry.
671 	 * This is good because it puts multiple same-hash value entries
672 	 * in a data block, improving the lookup of those entries.
673 	 */
674 	for (use_block = -1, lep = &ents[index];
675 	     index < leafhdr.count && be32_to_cpu(lep->hashval) == args->hashval;
676 	     index++, lep++) {
677 		if (be32_to_cpu(lep->address) == XFS_DIR2_NULL_DATAPTR)
678 			continue;
679 		i = xfs_dir2_dataptr_to_db(args->geo, be32_to_cpu(lep->address));
680 		ASSERT(i < be32_to_cpu(ltp->bestcount));
681 		ASSERT(bestsp[i] != cpu_to_be16(NULLDATAOFF));
682 		if (be16_to_cpu(bestsp[i]) >= length) {
683 			use_block = i;
684 			break;
685 		}
686 	}
687 	/*
688 	 * Didn't find a block yet, linear search all the data blocks.
689 	 */
690 	if (use_block == -1) {
691 		for (i = 0; i < be32_to_cpu(ltp->bestcount); i++) {
692 			/*
693 			 * Remember a block we see that's missing.
694 			 */
695 			if (bestsp[i] == cpu_to_be16(NULLDATAOFF) &&
696 			    use_block == -1)
697 				use_block = i;
698 			else if (be16_to_cpu(bestsp[i]) >= length) {
699 				use_block = i;
700 				break;
701 			}
702 		}
703 	}
704 	/*
705 	 * How many bytes do we need in the leaf block?
706 	 */
707 	needbytes = 0;
708 	if (!leafhdr.stale)
709 		needbytes += sizeof(xfs_dir2_leaf_entry_t);
710 	if (use_block == -1)
711 		needbytes += sizeof(xfs_dir2_data_off_t);
712 
713 	/*
714 	 * Now kill use_block if it refers to a missing block, so we
715 	 * can use it as an indication of allocation needed.
716 	 */
717 	if (use_block != -1 && bestsp[use_block] == cpu_to_be16(NULLDATAOFF))
718 		use_block = -1;
719 	/*
720 	 * If we don't have enough free bytes but we can make enough
721 	 * by compacting out stale entries, we'll do that.
722 	 */
723 	if ((char *)bestsp - (char *)&ents[leafhdr.count] < needbytes &&
724 	    leafhdr.stale > 1)
725 		compact = 1;
726 
727 	/*
728 	 * Otherwise if we don't have enough free bytes we need to
729 	 * convert to node form.
730 	 */
731 	else if ((char *)bestsp - (char *)&ents[leafhdr.count] < needbytes) {
732 		/*
733 		 * Just checking or no space reservation, give up.
734 		 */
735 		if ((args->op_flags & XFS_DA_OP_JUSTCHECK) ||
736 							args->total == 0) {
737 			xfs_trans_brelse(tp, lbp);
738 			return -ENOSPC;
739 		}
740 		/*
741 		 * Convert to node form.
742 		 */
743 		error = xfs_dir2_leaf_to_node(args, lbp);
744 		if (error)
745 			return error;
746 		/*
747 		 * Then add the new entry.
748 		 */
749 		return xfs_dir2_node_addname(args);
750 	}
751 	/*
752 	 * Otherwise it will fit without compaction.
753 	 */
754 	else
755 		compact = 0;
756 	/*
757 	 * If just checking, then it will fit unless we needed to allocate
758 	 * a new data block.
759 	 */
760 	if (args->op_flags & XFS_DA_OP_JUSTCHECK) {
761 		xfs_trans_brelse(tp, lbp);
762 		return use_block == -1 ? -ENOSPC : 0;
763 	}
764 	/*
765 	 * If no allocations are allowed, return now before we've
766 	 * changed anything.
767 	 */
768 	if (args->total == 0 && use_block == -1) {
769 		xfs_trans_brelse(tp, lbp);
770 		return -ENOSPC;
771 	}
772 	/*
773 	 * Need to compact the leaf entries, removing stale ones.
774 	 * Leave one stale entry behind - the one closest to our
775 	 * insertion index - and we'll shift that one to our insertion
776 	 * point later.
777 	 */
778 	if (compact) {
779 		xfs_dir3_leaf_compact_x1(&leafhdr, ents, &index, &lowstale,
780 			&highstale, &lfloglow, &lfloghigh);
781 	}
782 	/*
783 	 * There are stale entries, so we'll need log-low and log-high
784 	 * impossibly bad values later.
785 	 */
786 	else if (leafhdr.stale) {
787 		lfloglow = leafhdr.count;
788 		lfloghigh = -1;
789 	}
790 	/*
791 	 * If there was no data block space found, we need to allocate
792 	 * a new one.
793 	 */
794 	if (use_block == -1) {
795 		/*
796 		 * Add the new data block.
797 		 */
798 		if ((error = xfs_dir2_grow_inode(args, XFS_DIR2_DATA_SPACE,
799 				&use_block))) {
800 			xfs_trans_brelse(tp, lbp);
801 			return error;
802 		}
803 		/*
804 		 * Initialize the block.
805 		 */
806 		if ((error = xfs_dir3_data_init(args, use_block, &dbp))) {
807 			xfs_trans_brelse(tp, lbp);
808 			return error;
809 		}
810 		/*
811 		 * If we're adding a new data block on the end we need to
812 		 * extend the bests table.  Copy it up one entry.
813 		 */
814 		if (use_block >= be32_to_cpu(ltp->bestcount)) {
815 			bestsp--;
816 			memmove(&bestsp[0], &bestsp[1],
817 				be32_to_cpu(ltp->bestcount) * sizeof(bestsp[0]));
818 			be32_add_cpu(&ltp->bestcount, 1);
819 			xfs_dir3_leaf_log_tail(args, lbp);
820 			xfs_dir3_leaf_log_bests(args, lbp, 0,
821 						be32_to_cpu(ltp->bestcount) - 1);
822 		}
823 		/*
824 		 * If we're filling in a previously empty block just log it.
825 		 */
826 		else
827 			xfs_dir3_leaf_log_bests(args, lbp, use_block, use_block);
828 		hdr = dbp->b_addr;
829 		bf = xfs_dir2_data_bestfree_p(dp->i_mount, hdr);
830 		bestsp[use_block] = bf[0].length;
831 		grown = 1;
832 	} else {
833 		/*
834 		 * Already had space in some data block.
835 		 * Just read that one in.
836 		 */
837 		error = xfs_dir3_data_read(tp, dp,
838 				   xfs_dir2_db_to_da(args->geo, use_block),
839 				   0, &dbp);
840 		if (error) {
841 			xfs_trans_brelse(tp, lbp);
842 			return error;
843 		}
844 		hdr = dbp->b_addr;
845 		bf = xfs_dir2_data_bestfree_p(dp->i_mount, hdr);
846 		grown = 0;
847 	}
848 	/*
849 	 * Point to the biggest freespace in our data block.
850 	 */
851 	dup = (xfs_dir2_data_unused_t *)
852 	      ((char *)hdr + be16_to_cpu(bf[0].offset));
853 	needscan = needlog = 0;
854 	/*
855 	 * Mark the initial part of our freespace in use for the new entry.
856 	 */
857 	error = xfs_dir2_data_use_free(args, dbp, dup,
858 			(xfs_dir2_data_aoff_t)((char *)dup - (char *)hdr),
859 			length, &needlog, &needscan);
860 	if (error) {
861 		xfs_trans_brelse(tp, lbp);
862 		return error;
863 	}
864 	/*
865 	 * Initialize our new entry (at last).
866 	 */
867 	dep = (xfs_dir2_data_entry_t *)dup;
868 	dep->inumber = cpu_to_be64(args->inumber);
869 	dep->namelen = args->namelen;
870 	memcpy(dep->name, args->name, dep->namelen);
871 	xfs_dir2_data_put_ftype(dp->i_mount, dep, args->filetype);
872 	tagp = xfs_dir2_data_entry_tag_p(dp->i_mount, dep);
873 	*tagp = cpu_to_be16((char *)dep - (char *)hdr);
874 	/*
875 	 * Need to scan fix up the bestfree table.
876 	 */
877 	if (needscan)
878 		xfs_dir2_data_freescan(dp->i_mount, hdr, &needlog);
879 	/*
880 	 * Need to log the data block's header.
881 	 */
882 	if (needlog)
883 		xfs_dir2_data_log_header(args, dbp);
884 	xfs_dir2_data_log_entry(args, dbp, dep);
885 	/*
886 	 * If the bests table needs to be changed, do it.
887 	 * Log the change unless we've already done that.
888 	 */
889 	if (be16_to_cpu(bestsp[use_block]) != be16_to_cpu(bf[0].length)) {
890 		bestsp[use_block] = bf[0].length;
891 		if (!grown)
892 			xfs_dir3_leaf_log_bests(args, lbp, use_block, use_block);
893 	}
894 
895 	lep = xfs_dir3_leaf_find_entry(&leafhdr, ents, index, compact, lowstale,
896 				       highstale, &lfloglow, &lfloghigh);
897 
898 	/*
899 	 * Fill in the new leaf entry.
900 	 */
901 	lep->hashval = cpu_to_be32(args->hashval);
902 	lep->address = cpu_to_be32(
903 				xfs_dir2_db_off_to_dataptr(args->geo, use_block,
904 				be16_to_cpu(*tagp)));
905 	/*
906 	 * Log the leaf fields and give up the buffers.
907 	 */
908 	xfs_dir2_leaf_hdr_to_disk(dp->i_mount, leaf, &leafhdr);
909 	xfs_dir3_leaf_log_header(args, lbp);
910 	xfs_dir3_leaf_log_ents(args, &leafhdr, lbp, lfloglow, lfloghigh);
911 	xfs_dir3_leaf_check(dp, lbp);
912 	xfs_dir3_data_check(dp, dbp);
913 	return 0;
914 }
915 
916 /*
917  * Compact out any stale entries in the leaf.
918  * Log the header and changed leaf entries, if any.
919  */
920 void
921 xfs_dir3_leaf_compact(
922 	xfs_da_args_t	*args,		/* operation arguments */
923 	struct xfs_dir3_icleaf_hdr *leafhdr,
924 	struct xfs_buf	*bp)		/* leaf buffer */
925 {
926 	int		from;		/* source leaf index */
927 	xfs_dir2_leaf_t	*leaf;		/* leaf structure */
928 	int		loglow;		/* first leaf entry to log */
929 	int		to;		/* target leaf index */
930 	struct xfs_inode *dp = args->dp;
931 
932 	leaf = bp->b_addr;
933 	if (!leafhdr->stale)
934 		return;
935 
936 	/*
937 	 * Compress out the stale entries in place.
938 	 */
939 	for (from = to = 0, loglow = -1; from < leafhdr->count; from++) {
940 		if (leafhdr->ents[from].address ==
941 		    cpu_to_be32(XFS_DIR2_NULL_DATAPTR))
942 			continue;
943 		/*
944 		 * Only actually copy the entries that are different.
945 		 */
946 		if (from > to) {
947 			if (loglow == -1)
948 				loglow = to;
949 			leafhdr->ents[to] = leafhdr->ents[from];
950 		}
951 		to++;
952 	}
953 	/*
954 	 * Update and log the header, log the leaf entries.
955 	 */
956 	ASSERT(leafhdr->stale == from - to);
957 	leafhdr->count -= leafhdr->stale;
958 	leafhdr->stale = 0;
959 
960 	xfs_dir2_leaf_hdr_to_disk(dp->i_mount, leaf, leafhdr);
961 	xfs_dir3_leaf_log_header(args, bp);
962 	if (loglow != -1)
963 		xfs_dir3_leaf_log_ents(args, leafhdr, bp, loglow, to - 1);
964 }
965 
966 /*
967  * Compact the leaf entries, removing stale ones.
968  * Leave one stale entry behind - the one closest to our
969  * insertion index - and the caller will shift that one to our insertion
970  * point later.
971  * Return new insertion index, where the remaining stale entry is,
972  * and leaf logging indices.
973  */
974 void
975 xfs_dir3_leaf_compact_x1(
976 	struct xfs_dir3_icleaf_hdr *leafhdr,
977 	struct xfs_dir2_leaf_entry *ents,
978 	int		*indexp,	/* insertion index */
979 	int		*lowstalep,	/* out: stale entry before us */
980 	int		*highstalep,	/* out: stale entry after us */
981 	int		*lowlogp,	/* out: low log index */
982 	int		*highlogp)	/* out: high log index */
983 {
984 	int		from;		/* source copy index */
985 	int		highstale;	/* stale entry at/after index */
986 	int		index;		/* insertion index */
987 	int		keepstale;	/* source index of kept stale */
988 	int		lowstale;	/* stale entry before index */
989 	int		newindex=0;	/* new insertion index */
990 	int		to;		/* destination copy index */
991 
992 	ASSERT(leafhdr->stale > 1);
993 	index = *indexp;
994 
995 	xfs_dir3_leaf_find_stale(leafhdr, ents, index, &lowstale, &highstale);
996 
997 	/*
998 	 * Pick the better of lowstale and highstale.
999 	 */
1000 	if (lowstale >= 0 &&
1001 	    (highstale == leafhdr->count ||
1002 	     index - lowstale <= highstale - index))
1003 		keepstale = lowstale;
1004 	else
1005 		keepstale = highstale;
1006 	/*
1007 	 * Copy the entries in place, removing all the stale entries
1008 	 * except keepstale.
1009 	 */
1010 	for (from = to = 0; from < leafhdr->count; from++) {
1011 		/*
1012 		 * Notice the new value of index.
1013 		 */
1014 		if (index == from)
1015 			newindex = to;
1016 		if (from != keepstale &&
1017 		    ents[from].address == cpu_to_be32(XFS_DIR2_NULL_DATAPTR)) {
1018 			if (from == to)
1019 				*lowlogp = to;
1020 			continue;
1021 		}
1022 		/*
1023 		 * Record the new keepstale value for the insertion.
1024 		 */
1025 		if (from == keepstale)
1026 			lowstale = highstale = to;
1027 		/*
1028 		 * Copy only the entries that have moved.
1029 		 */
1030 		if (from > to)
1031 			ents[to] = ents[from];
1032 		to++;
1033 	}
1034 	ASSERT(from > to);
1035 	/*
1036 	 * If the insertion point was past the last entry,
1037 	 * set the new insertion point accordingly.
1038 	 */
1039 	if (index == from)
1040 		newindex = to;
1041 	*indexp = newindex;
1042 	/*
1043 	 * Adjust the leaf header values.
1044 	 */
1045 	leafhdr->count -= from - to;
1046 	leafhdr->stale = 1;
1047 	/*
1048 	 * Remember the low/high stale value only in the "right"
1049 	 * direction.
1050 	 */
1051 	if (lowstale >= newindex)
1052 		lowstale = -1;
1053 	else
1054 		highstale = leafhdr->count;
1055 	*highlogp = leafhdr->count - 1;
1056 	*lowstalep = lowstale;
1057 	*highstalep = highstale;
1058 }
1059 
1060 /*
1061  * Log the bests entries indicated from a leaf1 block.
1062  */
1063 static void
1064 xfs_dir3_leaf_log_bests(
1065 	struct xfs_da_args	*args,
1066 	struct xfs_buf		*bp,		/* leaf buffer */
1067 	int			first,		/* first entry to log */
1068 	int			last)		/* last entry to log */
1069 {
1070 	__be16			*firstb;	/* pointer to first entry */
1071 	__be16			*lastb;		/* pointer to last entry */
1072 	struct xfs_dir2_leaf	*leaf = bp->b_addr;
1073 	xfs_dir2_leaf_tail_t	*ltp;		/* leaf tail structure */
1074 
1075 	ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAF1_MAGIC) ||
1076 	       leaf->hdr.info.magic == cpu_to_be16(XFS_DIR3_LEAF1_MAGIC));
1077 
1078 	ltp = xfs_dir2_leaf_tail_p(args->geo, leaf);
1079 	firstb = xfs_dir2_leaf_bests_p(ltp) + first;
1080 	lastb = xfs_dir2_leaf_bests_p(ltp) + last;
1081 	xfs_trans_log_buf(args->trans, bp,
1082 		(uint)((char *)firstb - (char *)leaf),
1083 		(uint)((char *)lastb - (char *)leaf + sizeof(*lastb) - 1));
1084 }
1085 
1086 /*
1087  * Log the leaf entries indicated from a leaf1 or leafn block.
1088  */
1089 void
1090 xfs_dir3_leaf_log_ents(
1091 	struct xfs_da_args	*args,
1092 	struct xfs_dir3_icleaf_hdr *hdr,
1093 	struct xfs_buf		*bp,
1094 	int			first,
1095 	int			last)
1096 {
1097 	xfs_dir2_leaf_entry_t	*firstlep;	/* pointer to first entry */
1098 	xfs_dir2_leaf_entry_t	*lastlep;	/* pointer to last entry */
1099 	struct xfs_dir2_leaf	*leaf = bp->b_addr;
1100 
1101 	ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAF1_MAGIC) ||
1102 	       leaf->hdr.info.magic == cpu_to_be16(XFS_DIR3_LEAF1_MAGIC) ||
1103 	       leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAFN_MAGIC) ||
1104 	       leaf->hdr.info.magic == cpu_to_be16(XFS_DIR3_LEAFN_MAGIC));
1105 
1106 	firstlep = &hdr->ents[first];
1107 	lastlep = &hdr->ents[last];
1108 	xfs_trans_log_buf(args->trans, bp,
1109 		(uint)((char *)firstlep - (char *)leaf),
1110 		(uint)((char *)lastlep - (char *)leaf + sizeof(*lastlep) - 1));
1111 }
1112 
1113 /*
1114  * Log the header of the leaf1 or leafn block.
1115  */
1116 void
1117 xfs_dir3_leaf_log_header(
1118 	struct xfs_da_args	*args,
1119 	struct xfs_buf		*bp)
1120 {
1121 	struct xfs_dir2_leaf	*leaf = bp->b_addr;
1122 
1123 	ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAF1_MAGIC) ||
1124 	       leaf->hdr.info.magic == cpu_to_be16(XFS_DIR3_LEAF1_MAGIC) ||
1125 	       leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAFN_MAGIC) ||
1126 	       leaf->hdr.info.magic == cpu_to_be16(XFS_DIR3_LEAFN_MAGIC));
1127 
1128 	xfs_trans_log_buf(args->trans, bp,
1129 			  (uint)((char *)&leaf->hdr - (char *)leaf),
1130 			  args->geo->leaf_hdr_size - 1);
1131 }
1132 
1133 /*
1134  * Log the tail of the leaf1 block.
1135  */
1136 STATIC void
1137 xfs_dir3_leaf_log_tail(
1138 	struct xfs_da_args	*args,
1139 	struct xfs_buf		*bp)
1140 {
1141 	struct xfs_dir2_leaf	*leaf = bp->b_addr;
1142 	xfs_dir2_leaf_tail_t	*ltp;		/* leaf tail structure */
1143 
1144 	ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAF1_MAGIC) ||
1145 	       leaf->hdr.info.magic == cpu_to_be16(XFS_DIR3_LEAF1_MAGIC) ||
1146 	       leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAFN_MAGIC) ||
1147 	       leaf->hdr.info.magic == cpu_to_be16(XFS_DIR3_LEAFN_MAGIC));
1148 
1149 	ltp = xfs_dir2_leaf_tail_p(args->geo, leaf);
1150 	xfs_trans_log_buf(args->trans, bp, (uint)((char *)ltp - (char *)leaf),
1151 		(uint)(args->geo->blksize - 1));
1152 }
1153 
1154 /*
1155  * Look up the entry referred to by args in the leaf format directory.
1156  * Most of the work is done by the xfs_dir2_leaf_lookup_int routine which
1157  * is also used by the node-format code.
1158  */
1159 int
1160 xfs_dir2_leaf_lookup(
1161 	xfs_da_args_t		*args)		/* operation arguments */
1162 {
1163 	struct xfs_buf		*dbp;		/* data block buffer */
1164 	xfs_dir2_data_entry_t	*dep;		/* data block entry */
1165 	xfs_inode_t		*dp;		/* incore directory inode */
1166 	int			error;		/* error return code */
1167 	int			index;		/* found entry index */
1168 	struct xfs_buf		*lbp;		/* leaf buffer */
1169 	xfs_dir2_leaf_entry_t	*lep;		/* leaf entry */
1170 	xfs_trans_t		*tp;		/* transaction pointer */
1171 	struct xfs_dir3_icleaf_hdr leafhdr;
1172 
1173 	trace_xfs_dir2_leaf_lookup(args);
1174 
1175 	/*
1176 	 * Look up name in the leaf block, returning both buffers and index.
1177 	 */
1178 	error = xfs_dir2_leaf_lookup_int(args, &lbp, &index, &dbp, &leafhdr);
1179 	if (error)
1180 		return error;
1181 
1182 	tp = args->trans;
1183 	dp = args->dp;
1184 	xfs_dir3_leaf_check(dp, lbp);
1185 
1186 	/*
1187 	 * Get to the leaf entry and contained data entry address.
1188 	 */
1189 	lep = &leafhdr.ents[index];
1190 
1191 	/*
1192 	 * Point to the data entry.
1193 	 */
1194 	dep = (xfs_dir2_data_entry_t *)
1195 	      ((char *)dbp->b_addr +
1196 	       xfs_dir2_dataptr_to_off(args->geo, be32_to_cpu(lep->address)));
1197 	/*
1198 	 * Return the found inode number & CI name if appropriate
1199 	 */
1200 	args->inumber = be64_to_cpu(dep->inumber);
1201 	args->filetype = xfs_dir2_data_get_ftype(dp->i_mount, dep);
1202 	error = xfs_dir_cilookup_result(args, dep->name, dep->namelen);
1203 	xfs_trans_brelse(tp, dbp);
1204 	xfs_trans_brelse(tp, lbp);
1205 	return error;
1206 }
1207 
1208 /*
1209  * Look up name/hash in the leaf block.
1210  * Fill in indexp with the found index, and dbpp with the data buffer.
1211  * If not found dbpp will be NULL, and ENOENT comes back.
1212  * lbpp will always be filled in with the leaf buffer unless there's an error.
1213  */
1214 static int					/* error */
1215 xfs_dir2_leaf_lookup_int(
1216 	xfs_da_args_t		*args,		/* operation arguments */
1217 	struct xfs_buf		**lbpp,		/* out: leaf buffer */
1218 	int			*indexp,	/* out: index in leaf block */
1219 	struct xfs_buf		**dbpp,		/* out: data buffer */
1220 	struct xfs_dir3_icleaf_hdr *leafhdr)
1221 {
1222 	xfs_dir2_db_t		curdb = -1;	/* current data block number */
1223 	struct xfs_buf		*dbp = NULL;	/* data buffer */
1224 	xfs_dir2_data_entry_t	*dep;		/* data entry */
1225 	xfs_inode_t		*dp;		/* incore directory inode */
1226 	int			error;		/* error return code */
1227 	int			index;		/* index in leaf block */
1228 	struct xfs_buf		*lbp;		/* leaf buffer */
1229 	xfs_dir2_leaf_entry_t	*lep;		/* leaf entry */
1230 	xfs_dir2_leaf_t		*leaf;		/* leaf structure */
1231 	xfs_mount_t		*mp;		/* filesystem mount point */
1232 	xfs_dir2_db_t		newdb;		/* new data block number */
1233 	xfs_trans_t		*tp;		/* transaction pointer */
1234 	xfs_dir2_db_t		cidb = -1;	/* case match data block no. */
1235 	enum xfs_dacmp		cmp;		/* name compare result */
1236 
1237 	dp = args->dp;
1238 	tp = args->trans;
1239 	mp = dp->i_mount;
1240 
1241 	error = xfs_dir3_leaf_read(tp, dp, args->geo->leafblk, &lbp);
1242 	if (error)
1243 		return error;
1244 
1245 	*lbpp = lbp;
1246 	leaf = lbp->b_addr;
1247 	xfs_dir3_leaf_check(dp, lbp);
1248 	xfs_dir2_leaf_hdr_from_disk(mp, leafhdr, leaf);
1249 
1250 	/*
1251 	 * Look for the first leaf entry with our hash value.
1252 	 */
1253 	index = xfs_dir2_leaf_search_hash(args, lbp);
1254 	/*
1255 	 * Loop over all the entries with the right hash value
1256 	 * looking to match the name.
1257 	 */
1258 	for (lep = &leafhdr->ents[index];
1259 	     index < leafhdr->count &&
1260 			be32_to_cpu(lep->hashval) == args->hashval;
1261 	     lep++, index++) {
1262 		/*
1263 		 * Skip over stale leaf entries.
1264 		 */
1265 		if (be32_to_cpu(lep->address) == XFS_DIR2_NULL_DATAPTR)
1266 			continue;
1267 		/*
1268 		 * Get the new data block number.
1269 		 */
1270 		newdb = xfs_dir2_dataptr_to_db(args->geo,
1271 					       be32_to_cpu(lep->address));
1272 		/*
1273 		 * If it's not the same as the old data block number,
1274 		 * need to pitch the old one and read the new one.
1275 		 */
1276 		if (newdb != curdb) {
1277 			if (dbp)
1278 				xfs_trans_brelse(tp, dbp);
1279 			error = xfs_dir3_data_read(tp, dp,
1280 					   xfs_dir2_db_to_da(args->geo, newdb),
1281 					   0, &dbp);
1282 			if (error) {
1283 				xfs_trans_brelse(tp, lbp);
1284 				return error;
1285 			}
1286 			curdb = newdb;
1287 		}
1288 		/*
1289 		 * Point to the data entry.
1290 		 */
1291 		dep = (xfs_dir2_data_entry_t *)((char *)dbp->b_addr +
1292 			xfs_dir2_dataptr_to_off(args->geo,
1293 						be32_to_cpu(lep->address)));
1294 		/*
1295 		 * Compare name and if it's an exact match, return the index
1296 		 * and buffer. If it's the first case-insensitive match, store
1297 		 * the index and buffer and continue looking for an exact match.
1298 		 */
1299 		cmp = xfs_dir2_compname(args, dep->name, dep->namelen);
1300 		if (cmp != XFS_CMP_DIFFERENT && cmp != args->cmpresult) {
1301 			args->cmpresult = cmp;
1302 			*indexp = index;
1303 			/* case exact match: return the current buffer. */
1304 			if (cmp == XFS_CMP_EXACT) {
1305 				*dbpp = dbp;
1306 				return 0;
1307 			}
1308 			cidb = curdb;
1309 		}
1310 	}
1311 	ASSERT(args->op_flags & XFS_DA_OP_OKNOENT);
1312 	/*
1313 	 * Here, we can only be doing a lookup (not a rename or remove).
1314 	 * If a case-insensitive match was found earlier, re-read the
1315 	 * appropriate data block if required and return it.
1316 	 */
1317 	if (args->cmpresult == XFS_CMP_CASE) {
1318 		ASSERT(cidb != -1);
1319 		if (cidb != curdb) {
1320 			xfs_trans_brelse(tp, dbp);
1321 			error = xfs_dir3_data_read(tp, dp,
1322 					   xfs_dir2_db_to_da(args->geo, cidb),
1323 					   0, &dbp);
1324 			if (error) {
1325 				xfs_trans_brelse(tp, lbp);
1326 				return error;
1327 			}
1328 		}
1329 		*dbpp = dbp;
1330 		return 0;
1331 	}
1332 	/*
1333 	 * No match found, return -ENOENT.
1334 	 */
1335 	ASSERT(cidb == -1);
1336 	if (dbp)
1337 		xfs_trans_brelse(tp, dbp);
1338 	xfs_trans_brelse(tp, lbp);
1339 	return -ENOENT;
1340 }
1341 
1342 /*
1343  * Remove an entry from a leaf format directory.
1344  */
1345 int						/* error */
1346 xfs_dir2_leaf_removename(
1347 	xfs_da_args_t		*args)		/* operation arguments */
1348 {
1349 	struct xfs_da_geometry	*geo = args->geo;
1350 	__be16			*bestsp;	/* leaf block best freespace */
1351 	xfs_dir2_data_hdr_t	*hdr;		/* data block header */
1352 	xfs_dir2_db_t		db;		/* data block number */
1353 	struct xfs_buf		*dbp;		/* data block buffer */
1354 	xfs_dir2_data_entry_t	*dep;		/* data entry structure */
1355 	xfs_inode_t		*dp;		/* incore directory inode */
1356 	int			error;		/* error return code */
1357 	xfs_dir2_db_t		i;		/* temporary data block # */
1358 	int			index;		/* index into leaf entries */
1359 	struct xfs_buf		*lbp;		/* leaf buffer */
1360 	xfs_dir2_leaf_t		*leaf;		/* leaf structure */
1361 	xfs_dir2_leaf_entry_t	*lep;		/* leaf entry */
1362 	xfs_dir2_leaf_tail_t	*ltp;		/* leaf tail structure */
1363 	int			needlog;	/* need to log data header */
1364 	int			needscan;	/* need to rescan data frees */
1365 	xfs_dir2_data_off_t	oldbest;	/* old value of best free */
1366 	struct xfs_dir2_data_free *bf;		/* bestfree table */
1367 	struct xfs_dir3_icleaf_hdr leafhdr;
1368 
1369 	trace_xfs_dir2_leaf_removename(args);
1370 
1371 	/*
1372 	 * Lookup the leaf entry, get the leaf and data blocks read in.
1373 	 */
1374 	error = xfs_dir2_leaf_lookup_int(args, &lbp, &index, &dbp, &leafhdr);
1375 	if (error)
1376 		return error;
1377 
1378 	dp = args->dp;
1379 	leaf = lbp->b_addr;
1380 	hdr = dbp->b_addr;
1381 	xfs_dir3_data_check(dp, dbp);
1382 	bf = xfs_dir2_data_bestfree_p(dp->i_mount, hdr);
1383 
1384 	/*
1385 	 * Point to the leaf entry, use that to point to the data entry.
1386 	 */
1387 	lep = &leafhdr.ents[index];
1388 	db = xfs_dir2_dataptr_to_db(geo, be32_to_cpu(lep->address));
1389 	dep = (xfs_dir2_data_entry_t *)((char *)hdr +
1390 		xfs_dir2_dataptr_to_off(geo, be32_to_cpu(lep->address)));
1391 	needscan = needlog = 0;
1392 	oldbest = be16_to_cpu(bf[0].length);
1393 	ltp = xfs_dir2_leaf_tail_p(geo, leaf);
1394 	bestsp = xfs_dir2_leaf_bests_p(ltp);
1395 	if (be16_to_cpu(bestsp[db]) != oldbest) {
1396 		xfs_buf_mark_corrupt(lbp);
1397 		xfs_da_mark_sick(args);
1398 		return -EFSCORRUPTED;
1399 	}
1400 
1401 	/*
1402 	 * Mark the former data entry unused.
1403 	 */
1404 	xfs_dir2_data_make_free(args, dbp,
1405 		(xfs_dir2_data_aoff_t)((char *)dep - (char *)hdr),
1406 		xfs_dir2_data_entsize(dp->i_mount, dep->namelen), &needlog,
1407 		&needscan);
1408 	/*
1409 	 * We just mark the leaf entry stale by putting a null in it.
1410 	 */
1411 	leafhdr.stale++;
1412 	xfs_dir2_leaf_hdr_to_disk(dp->i_mount, leaf, &leafhdr);
1413 	xfs_dir3_leaf_log_header(args, lbp);
1414 
1415 	lep->address = cpu_to_be32(XFS_DIR2_NULL_DATAPTR);
1416 	xfs_dir3_leaf_log_ents(args, &leafhdr, lbp, index, index);
1417 
1418 	/*
1419 	 * Scan the freespace in the data block again if necessary,
1420 	 * log the data block header if necessary.
1421 	 */
1422 	if (needscan)
1423 		xfs_dir2_data_freescan(dp->i_mount, hdr, &needlog);
1424 	if (needlog)
1425 		xfs_dir2_data_log_header(args, dbp);
1426 	/*
1427 	 * If the longest freespace in the data block has changed,
1428 	 * put the new value in the bests table and log that.
1429 	 */
1430 	if (be16_to_cpu(bf[0].length) != oldbest) {
1431 		bestsp[db] = bf[0].length;
1432 		xfs_dir3_leaf_log_bests(args, lbp, db, db);
1433 	}
1434 	xfs_dir3_data_check(dp, dbp);
1435 	/*
1436 	 * If the data block is now empty then get rid of the data block.
1437 	 */
1438 	if (be16_to_cpu(bf[0].length) ==
1439 	    geo->blksize - geo->data_entry_offset) {
1440 		ASSERT(db != geo->datablk);
1441 		if ((error = xfs_dir2_shrink_inode(args, db, dbp))) {
1442 			/*
1443 			 * Nope, can't get rid of it because it caused
1444 			 * allocation of a bmap btree block to do so.
1445 			 * Just go on, returning success, leaving the
1446 			 * empty block in place.
1447 			 */
1448 			if (error == -ENOSPC && args->total == 0)
1449 				error = 0;
1450 			xfs_dir3_leaf_check(dp, lbp);
1451 			return error;
1452 		}
1453 		dbp = NULL;
1454 		/*
1455 		 * If this is the last data block then compact the
1456 		 * bests table by getting rid of entries.
1457 		 */
1458 		if (db == be32_to_cpu(ltp->bestcount) - 1) {
1459 			/*
1460 			 * Look for the last active entry (i).
1461 			 */
1462 			for (i = db - 1; i > 0; i--) {
1463 				if (bestsp[i] != cpu_to_be16(NULLDATAOFF))
1464 					break;
1465 			}
1466 			/*
1467 			 * Copy the table down so inactive entries at the
1468 			 * end are removed.
1469 			 */
1470 			memmove(&bestsp[db - i], bestsp,
1471 				(be32_to_cpu(ltp->bestcount) - (db - i)) * sizeof(*bestsp));
1472 			be32_add_cpu(&ltp->bestcount, -(db - i));
1473 			xfs_dir3_leaf_log_tail(args, lbp);
1474 			xfs_dir3_leaf_log_bests(args, lbp, 0,
1475 						be32_to_cpu(ltp->bestcount) - 1);
1476 		} else
1477 			bestsp[db] = cpu_to_be16(NULLDATAOFF);
1478 	}
1479 	/*
1480 	 * If the data block was not the first one, drop it.
1481 	 */
1482 	else if (db != geo->datablk)
1483 		dbp = NULL;
1484 
1485 	xfs_dir3_leaf_check(dp, lbp);
1486 	/*
1487 	 * See if we can convert to block form.
1488 	 */
1489 	return xfs_dir2_leaf_to_block(args, lbp, dbp);
1490 }
1491 
1492 /*
1493  * Replace the inode number in a leaf format directory entry.
1494  */
1495 int						/* error */
1496 xfs_dir2_leaf_replace(
1497 	xfs_da_args_t		*args)		/* operation arguments */
1498 {
1499 	struct xfs_buf		*dbp;		/* data block buffer */
1500 	xfs_dir2_data_entry_t	*dep;		/* data block entry */
1501 	xfs_inode_t		*dp;		/* incore directory inode */
1502 	int			error;		/* error return code */
1503 	int			index;		/* index of leaf entry */
1504 	struct xfs_buf		*lbp;		/* leaf buffer */
1505 	xfs_dir2_leaf_entry_t	*lep;		/* leaf entry */
1506 	xfs_trans_t		*tp;		/* transaction pointer */
1507 	struct xfs_dir3_icleaf_hdr leafhdr;
1508 
1509 	trace_xfs_dir2_leaf_replace(args);
1510 
1511 	/*
1512 	 * Look up the entry.
1513 	 */
1514 	error = xfs_dir2_leaf_lookup_int(args, &lbp, &index, &dbp, &leafhdr);
1515 	if (error)
1516 		return error;
1517 
1518 	dp = args->dp;
1519 	/*
1520 	 * Point to the leaf entry, get data address from it.
1521 	 */
1522 	lep = &leafhdr.ents[index];
1523 	/*
1524 	 * Point to the data entry.
1525 	 */
1526 	dep = (xfs_dir2_data_entry_t *)
1527 	      ((char *)dbp->b_addr +
1528 	       xfs_dir2_dataptr_to_off(args->geo, be32_to_cpu(lep->address)));
1529 	ASSERT(args->inumber != be64_to_cpu(dep->inumber));
1530 	/*
1531 	 * Put the new inode number in, log it.
1532 	 */
1533 	dep->inumber = cpu_to_be64(args->inumber);
1534 	xfs_dir2_data_put_ftype(dp->i_mount, dep, args->filetype);
1535 	tp = args->trans;
1536 	xfs_dir2_data_log_entry(args, dbp, dep);
1537 	xfs_dir3_leaf_check(dp, lbp);
1538 	xfs_trans_brelse(tp, lbp);
1539 	return 0;
1540 }
1541 
1542 /*
1543  * Return index in the leaf block (lbp) which is either the first
1544  * one with this hash value, or if there are none, the insert point
1545  * for that hash value.
1546  */
1547 int						/* index value */
1548 xfs_dir2_leaf_search_hash(
1549 	xfs_da_args_t		*args,		/* operation arguments */
1550 	struct xfs_buf		*lbp)		/* leaf buffer */
1551 {
1552 	xfs_dahash_t		hash=0;		/* hash from this entry */
1553 	xfs_dahash_t		hashwant;	/* hash value looking for */
1554 	int			high;		/* high leaf index */
1555 	int			low;		/* low leaf index */
1556 	xfs_dir2_leaf_entry_t	*lep;		/* leaf entry */
1557 	int			mid=0;		/* current leaf index */
1558 	struct xfs_dir3_icleaf_hdr leafhdr;
1559 
1560 	xfs_dir2_leaf_hdr_from_disk(args->dp->i_mount, &leafhdr, lbp->b_addr);
1561 
1562 	/*
1563 	 * Note, the table cannot be empty, so we have to go through the loop.
1564 	 * Binary search the leaf entries looking for our hash value.
1565 	 */
1566 	for (lep = leafhdr.ents, low = 0, high = leafhdr.count - 1,
1567 		hashwant = args->hashval;
1568 	     low <= high; ) {
1569 		mid = (low + high) >> 1;
1570 		if ((hash = be32_to_cpu(lep[mid].hashval)) == hashwant)
1571 			break;
1572 		if (hash < hashwant)
1573 			low = mid + 1;
1574 		else
1575 			high = mid - 1;
1576 	}
1577 	/*
1578 	 * Found one, back up through all the equal hash values.
1579 	 */
1580 	if (hash == hashwant) {
1581 		while (mid > 0 && be32_to_cpu(lep[mid - 1].hashval) == hashwant) {
1582 			mid--;
1583 		}
1584 	}
1585 	/*
1586 	 * Need to point to an entry higher than ours.
1587 	 */
1588 	else if (hash < hashwant)
1589 		mid++;
1590 	return mid;
1591 }
1592 
1593 /*
1594  * Trim off a trailing data block.  We know it's empty since the leaf
1595  * freespace table says so.
1596  */
1597 int						/* error */
1598 xfs_dir2_leaf_trim_data(
1599 	xfs_da_args_t		*args,		/* operation arguments */
1600 	struct xfs_buf		*lbp,		/* leaf buffer */
1601 	xfs_dir2_db_t		db)		/* data block number */
1602 {
1603 	struct xfs_da_geometry	*geo = args->geo;
1604 	__be16			*bestsp;	/* leaf bests table */
1605 	struct xfs_buf		*dbp;		/* data block buffer */
1606 	xfs_inode_t		*dp;		/* incore directory inode */
1607 	int			error;		/* error return value */
1608 	xfs_dir2_leaf_t		*leaf;		/* leaf structure */
1609 	xfs_dir2_leaf_tail_t	*ltp;		/* leaf tail structure */
1610 	xfs_trans_t		*tp;		/* transaction pointer */
1611 
1612 	dp = args->dp;
1613 	tp = args->trans;
1614 	/*
1615 	 * Read the offending data block.  We need its buffer.
1616 	 */
1617 	error = xfs_dir3_data_read(tp, dp, xfs_dir2_db_to_da(geo, db), 0, &dbp);
1618 	if (error)
1619 		return error;
1620 
1621 	leaf = lbp->b_addr;
1622 	ltp = xfs_dir2_leaf_tail_p(geo, leaf);
1623 
1624 #ifdef DEBUG
1625 {
1626 	struct xfs_dir2_data_hdr *hdr = dbp->b_addr;
1627 	struct xfs_dir2_data_free *bf =
1628 		xfs_dir2_data_bestfree_p(dp->i_mount, hdr);
1629 
1630 	ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
1631 	       hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC));
1632 	ASSERT(be16_to_cpu(bf[0].length) ==
1633 	       geo->blksize - geo->data_entry_offset);
1634 	ASSERT(db == be32_to_cpu(ltp->bestcount) - 1);
1635 }
1636 #endif
1637 
1638 	/*
1639 	 * Get rid of the data block.
1640 	 */
1641 	if ((error = xfs_dir2_shrink_inode(args, db, dbp))) {
1642 		ASSERT(error != -ENOSPC);
1643 		xfs_trans_brelse(tp, dbp);
1644 		return error;
1645 	}
1646 	/*
1647 	 * Eliminate the last bests entry from the table.
1648 	 */
1649 	bestsp = xfs_dir2_leaf_bests_p(ltp);
1650 	be32_add_cpu(&ltp->bestcount, -1);
1651 	memmove(&bestsp[1], &bestsp[0], be32_to_cpu(ltp->bestcount) * sizeof(*bestsp));
1652 	xfs_dir3_leaf_log_tail(args, lbp);
1653 	xfs_dir3_leaf_log_bests(args, lbp, 0, be32_to_cpu(ltp->bestcount) - 1);
1654 	return 0;
1655 }
1656 
1657 static inline size_t
1658 xfs_dir3_leaf_size(
1659 	struct xfs_dir3_icleaf_hdr	*hdr,
1660 	int				counts)
1661 {
1662 	int	entries;
1663 	int	hdrsize;
1664 
1665 	entries = hdr->count - hdr->stale;
1666 	if (hdr->magic == XFS_DIR2_LEAF1_MAGIC ||
1667 	    hdr->magic == XFS_DIR2_LEAFN_MAGIC)
1668 		hdrsize = sizeof(struct xfs_dir2_leaf_hdr);
1669 	else
1670 		hdrsize = sizeof(struct xfs_dir3_leaf_hdr);
1671 
1672 	return hdrsize + entries * sizeof(xfs_dir2_leaf_entry_t)
1673 	               + counts * sizeof(xfs_dir2_data_off_t)
1674 		       + sizeof(xfs_dir2_leaf_tail_t);
1675 }
1676 
1677 /*
1678  * Convert node form directory to leaf form directory.
1679  * The root of the node form dir needs to already be a LEAFN block.
1680  * Just return if we can't do anything.
1681  */
1682 int						/* error */
1683 xfs_dir2_node_to_leaf(
1684 	xfs_da_state_t		*state)		/* directory operation state */
1685 {
1686 	xfs_da_args_t		*args;		/* operation arguments */
1687 	xfs_inode_t		*dp;		/* incore directory inode */
1688 	int			error;		/* error return code */
1689 	struct xfs_buf		*fbp;		/* buffer for freespace block */
1690 	xfs_fileoff_t		fo;		/* freespace file offset */
1691 	struct xfs_buf		*lbp;		/* buffer for leaf block */
1692 	xfs_dir2_leaf_tail_t	*ltp;		/* tail of leaf structure */
1693 	xfs_dir2_leaf_t		*leaf;		/* leaf structure */
1694 	xfs_mount_t		*mp;		/* filesystem mount point */
1695 	int			rval;		/* successful free trim? */
1696 	xfs_trans_t		*tp;		/* transaction pointer */
1697 	struct xfs_dir3_icleaf_hdr leafhdr;
1698 	struct xfs_dir3_icfree_hdr freehdr;
1699 
1700 	/*
1701 	 * There's more than a leaf level in the btree, so there must
1702 	 * be multiple leafn blocks.  Give up.
1703 	 */
1704 	if (state->path.active > 1)
1705 		return 0;
1706 	args = state->args;
1707 
1708 	trace_xfs_dir2_node_to_leaf(args);
1709 
1710 	mp = state->mp;
1711 	dp = args->dp;
1712 	tp = args->trans;
1713 	/*
1714 	 * Get the last offset in the file.
1715 	 */
1716 	if ((error = xfs_bmap_last_offset(dp, &fo, XFS_DATA_FORK))) {
1717 		return error;
1718 	}
1719 	fo -= args->geo->fsbcount;
1720 	/*
1721 	 * If there are freespace blocks other than the first one,
1722 	 * take this opportunity to remove trailing empty freespace blocks
1723 	 * that may have been left behind during no-space-reservation
1724 	 * operations.
1725 	 */
1726 	while (fo > args->geo->freeblk) {
1727 		if ((error = xfs_dir2_node_trim_free(args, fo, &rval))) {
1728 			return error;
1729 		}
1730 		if (rval)
1731 			fo -= args->geo->fsbcount;
1732 		else
1733 			return 0;
1734 	}
1735 	/*
1736 	 * Now find the block just before the freespace block.
1737 	 */
1738 	if ((error = xfs_bmap_last_before(tp, dp, &fo, XFS_DATA_FORK))) {
1739 		return error;
1740 	}
1741 	/*
1742 	 * If it's not the single leaf block, give up.
1743 	 */
1744 	if (XFS_FSB_TO_B(mp, fo) > XFS_DIR2_LEAF_OFFSET + args->geo->blksize)
1745 		return 0;
1746 	lbp = state->path.blk[0].bp;
1747 	leaf = lbp->b_addr;
1748 	xfs_dir2_leaf_hdr_from_disk(mp, &leafhdr, leaf);
1749 
1750 	ASSERT(leafhdr.magic == XFS_DIR2_LEAFN_MAGIC ||
1751 	       leafhdr.magic == XFS_DIR3_LEAFN_MAGIC);
1752 
1753 	/*
1754 	 * Read the freespace block.
1755 	 */
1756 	error = xfs_dir2_free_read(tp, dp,  args->geo->freeblk, &fbp);
1757 	if (error)
1758 		return error;
1759 	xfs_dir2_free_hdr_from_disk(mp, &freehdr, fbp->b_addr);
1760 
1761 	ASSERT(!freehdr.firstdb);
1762 
1763 	/*
1764 	 * Now see if the leafn and free data will fit in a leaf1.
1765 	 * If not, release the buffer and give up.
1766 	 */
1767 	if (xfs_dir3_leaf_size(&leafhdr, freehdr.nvalid) > args->geo->blksize) {
1768 		xfs_trans_brelse(tp, fbp);
1769 		return 0;
1770 	}
1771 
1772 	/*
1773 	 * If the leaf has any stale entries in it, compress them out.
1774 	 */
1775 	if (leafhdr.stale)
1776 		xfs_dir3_leaf_compact(args, &leafhdr, lbp);
1777 
1778 	lbp->b_ops = &xfs_dir3_leaf1_buf_ops;
1779 	xfs_trans_buf_set_type(tp, lbp, XFS_BLFT_DIR_LEAF1_BUF);
1780 	leafhdr.magic = (leafhdr.magic == XFS_DIR2_LEAFN_MAGIC)
1781 					? XFS_DIR2_LEAF1_MAGIC
1782 					: XFS_DIR3_LEAF1_MAGIC;
1783 
1784 	/*
1785 	 * Set up the leaf tail from the freespace block.
1786 	 */
1787 	ltp = xfs_dir2_leaf_tail_p(args->geo, leaf);
1788 	ltp->bestcount = cpu_to_be32(freehdr.nvalid);
1789 
1790 	/*
1791 	 * Set up the leaf bests table.
1792 	 */
1793 	memcpy(xfs_dir2_leaf_bests_p(ltp), freehdr.bests,
1794 		freehdr.nvalid * sizeof(xfs_dir2_data_off_t));
1795 
1796 	xfs_dir2_leaf_hdr_to_disk(mp, leaf, &leafhdr);
1797 	xfs_dir3_leaf_log_header(args, lbp);
1798 	xfs_dir3_leaf_log_bests(args, lbp, 0, be32_to_cpu(ltp->bestcount) - 1);
1799 	xfs_dir3_leaf_log_tail(args, lbp);
1800 	xfs_dir3_leaf_check(dp, lbp);
1801 
1802 	/*
1803 	 * Get rid of the freespace block.
1804 	 */
1805 	error = xfs_dir2_shrink_inode(args,
1806 			xfs_dir2_byte_to_db(args->geo, XFS_DIR2_FREE_OFFSET),
1807 			fbp);
1808 	if (error) {
1809 		/*
1810 		 * This can't fail here because it can only happen when
1811 		 * punching out the middle of an extent, and this is an
1812 		 * isolated block.
1813 		 */
1814 		ASSERT(error != -ENOSPC);
1815 		return error;
1816 	}
1817 	fbp = NULL;
1818 	/*
1819 	 * Now see if we can convert the single-leaf directory
1820 	 * down to a block form directory.
1821 	 * This routine always kills the dabuf for the leaf, so
1822 	 * eliminate it from the path.
1823 	 */
1824 	error = xfs_dir2_leaf_to_block(args, lbp, NULL);
1825 	state->path.blk[0].bp = NULL;
1826 	return error;
1827 }
1828