xref: /linux/fs/xfs/libxfs/xfs_dir2_node.c (revision f3539c12d8196ce0a1993364d30b3a18908470d1)
1 /*
2  * Copyright (c) 2000-2005 Silicon Graphics, Inc.
3  * Copyright (c) 2013 Red Hat, Inc.
4  * All Rights Reserved.
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License as
8  * published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it would be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write the Free Software Foundation,
17  * Inc.,  51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18  */
19 #include "xfs.h"
20 #include "xfs_fs.h"
21 #include "xfs_format.h"
22 #include "xfs_log_format.h"
23 #include "xfs_trans_resv.h"
24 #include "xfs_mount.h"
25 #include "xfs_da_format.h"
26 #include "xfs_da_btree.h"
27 #include "xfs_inode.h"
28 #include "xfs_bmap.h"
29 #include "xfs_dir2.h"
30 #include "xfs_dir2_priv.h"
31 #include "xfs_error.h"
32 #include "xfs_trace.h"
33 #include "xfs_trans.h"
34 #include "xfs_buf_item.h"
35 #include "xfs_cksum.h"
36 #include "xfs_log.h"
37 
38 /*
39  * Function declarations.
40  */
41 static int xfs_dir2_leafn_add(struct xfs_buf *bp, xfs_da_args_t *args,
42 			      int index);
43 static void xfs_dir2_leafn_rebalance(xfs_da_state_t *state,
44 				     xfs_da_state_blk_t *blk1,
45 				     xfs_da_state_blk_t *blk2);
46 static int xfs_dir2_leafn_remove(xfs_da_args_t *args, struct xfs_buf *bp,
47 				 int index, xfs_da_state_blk_t *dblk,
48 				 int *rval);
49 static int xfs_dir2_node_addname_int(xfs_da_args_t *args,
50 				     xfs_da_state_blk_t *fblk);
51 
52 /*
53  * Check internal consistency of a leafn block.
54  */
55 #ifdef DEBUG
56 #define	xfs_dir3_leaf_check(dp, bp) \
57 do { \
58 	if (!xfs_dir3_leafn_check((dp), (bp))) \
59 		ASSERT(0); \
60 } while (0);
61 
62 static bool
63 xfs_dir3_leafn_check(
64 	struct xfs_inode	*dp,
65 	struct xfs_buf		*bp)
66 {
67 	struct xfs_dir2_leaf	*leaf = bp->b_addr;
68 	struct xfs_dir3_icleaf_hdr leafhdr;
69 
70 	dp->d_ops->leaf_hdr_from_disk(&leafhdr, leaf);
71 
72 	if (leafhdr.magic == XFS_DIR3_LEAFN_MAGIC) {
73 		struct xfs_dir3_leaf_hdr *leaf3 = bp->b_addr;
74 		if (be64_to_cpu(leaf3->info.blkno) != bp->b_bn)
75 			return false;
76 	} else if (leafhdr.magic != XFS_DIR2_LEAFN_MAGIC)
77 		return false;
78 
79 	return xfs_dir3_leaf_check_int(dp->i_mount, dp, &leafhdr, leaf);
80 }
81 #else
82 #define	xfs_dir3_leaf_check(dp, bp)
83 #endif
84 
85 static bool
86 xfs_dir3_free_verify(
87 	struct xfs_buf		*bp)
88 {
89 	struct xfs_mount	*mp = bp->b_target->bt_mount;
90 	struct xfs_dir2_free_hdr *hdr = bp->b_addr;
91 
92 	if (xfs_sb_version_hascrc(&mp->m_sb)) {
93 		struct xfs_dir3_blk_hdr *hdr3 = bp->b_addr;
94 
95 		if (hdr3->magic != cpu_to_be32(XFS_DIR3_FREE_MAGIC))
96 			return false;
97 		if (!uuid_equal(&hdr3->uuid, &mp->m_sb.sb_meta_uuid))
98 			return false;
99 		if (be64_to_cpu(hdr3->blkno) != bp->b_bn)
100 			return false;
101 		if (!xfs_log_check_lsn(mp, be64_to_cpu(hdr3->lsn)))
102 			return false;
103 	} else {
104 		if (hdr->magic != cpu_to_be32(XFS_DIR2_FREE_MAGIC))
105 			return false;
106 	}
107 
108 	/* XXX: should bounds check the xfs_dir3_icfree_hdr here */
109 
110 	return true;
111 }
112 
113 static void
114 xfs_dir3_free_read_verify(
115 	struct xfs_buf	*bp)
116 {
117 	struct xfs_mount	*mp = bp->b_target->bt_mount;
118 
119 	if (xfs_sb_version_hascrc(&mp->m_sb) &&
120 	    !xfs_buf_verify_cksum(bp, XFS_DIR3_FREE_CRC_OFF))
121 		xfs_buf_ioerror(bp, -EFSBADCRC);
122 	else if (!xfs_dir3_free_verify(bp))
123 		xfs_buf_ioerror(bp, -EFSCORRUPTED);
124 
125 	if (bp->b_error)
126 		xfs_verifier_error(bp);
127 }
128 
129 static void
130 xfs_dir3_free_write_verify(
131 	struct xfs_buf	*bp)
132 {
133 	struct xfs_mount	*mp = bp->b_target->bt_mount;
134 	struct xfs_buf_log_item	*bip = bp->b_fspriv;
135 	struct xfs_dir3_blk_hdr	*hdr3 = bp->b_addr;
136 
137 	if (!xfs_dir3_free_verify(bp)) {
138 		xfs_buf_ioerror(bp, -EFSCORRUPTED);
139 		xfs_verifier_error(bp);
140 		return;
141 	}
142 
143 	if (!xfs_sb_version_hascrc(&mp->m_sb))
144 		return;
145 
146 	if (bip)
147 		hdr3->lsn = cpu_to_be64(bip->bli_item.li_lsn);
148 
149 	xfs_buf_update_cksum(bp, XFS_DIR3_FREE_CRC_OFF);
150 }
151 
152 const struct xfs_buf_ops xfs_dir3_free_buf_ops = {
153 	.name = "xfs_dir3_free",
154 	.verify_read = xfs_dir3_free_read_verify,
155 	.verify_write = xfs_dir3_free_write_verify,
156 };
157 
158 
159 static int
160 __xfs_dir3_free_read(
161 	struct xfs_trans	*tp,
162 	struct xfs_inode	*dp,
163 	xfs_dablk_t		fbno,
164 	xfs_daddr_t		mappedbno,
165 	struct xfs_buf		**bpp)
166 {
167 	int			err;
168 
169 	err = xfs_da_read_buf(tp, dp, fbno, mappedbno, bpp,
170 				XFS_DATA_FORK, &xfs_dir3_free_buf_ops);
171 
172 	/* try read returns without an error or *bpp if it lands in a hole */
173 	if (!err && tp && *bpp)
174 		xfs_trans_buf_set_type(tp, *bpp, XFS_BLFT_DIR_FREE_BUF);
175 	return err;
176 }
177 
178 int
179 xfs_dir2_free_read(
180 	struct xfs_trans	*tp,
181 	struct xfs_inode	*dp,
182 	xfs_dablk_t		fbno,
183 	struct xfs_buf		**bpp)
184 {
185 	return __xfs_dir3_free_read(tp, dp, fbno, -1, bpp);
186 }
187 
188 static int
189 xfs_dir2_free_try_read(
190 	struct xfs_trans	*tp,
191 	struct xfs_inode	*dp,
192 	xfs_dablk_t		fbno,
193 	struct xfs_buf		**bpp)
194 {
195 	return __xfs_dir3_free_read(tp, dp, fbno, -2, bpp);
196 }
197 
198 static int
199 xfs_dir3_free_get_buf(
200 	xfs_da_args_t		*args,
201 	xfs_dir2_db_t		fbno,
202 	struct xfs_buf		**bpp)
203 {
204 	struct xfs_trans	*tp = args->trans;
205 	struct xfs_inode	*dp = args->dp;
206 	struct xfs_mount	*mp = dp->i_mount;
207 	struct xfs_buf		*bp;
208 	int			error;
209 	struct xfs_dir3_icfree_hdr hdr;
210 
211 	error = xfs_da_get_buf(tp, dp, xfs_dir2_db_to_da(args->geo, fbno),
212 				   -1, &bp, XFS_DATA_FORK);
213 	if (error)
214 		return error;
215 
216 	xfs_trans_buf_set_type(tp, bp, XFS_BLFT_DIR_FREE_BUF);
217 	bp->b_ops = &xfs_dir3_free_buf_ops;
218 
219 	/*
220 	 * Initialize the new block to be empty, and remember
221 	 * its first slot as our empty slot.
222 	 */
223 	memset(bp->b_addr, 0, sizeof(struct xfs_dir3_free_hdr));
224 	memset(&hdr, 0, sizeof(hdr));
225 
226 	if (xfs_sb_version_hascrc(&mp->m_sb)) {
227 		struct xfs_dir3_free_hdr *hdr3 = bp->b_addr;
228 
229 		hdr.magic = XFS_DIR3_FREE_MAGIC;
230 
231 		hdr3->hdr.blkno = cpu_to_be64(bp->b_bn);
232 		hdr3->hdr.owner = cpu_to_be64(dp->i_ino);
233 		uuid_copy(&hdr3->hdr.uuid, &mp->m_sb.sb_meta_uuid);
234 	} else
235 		hdr.magic = XFS_DIR2_FREE_MAGIC;
236 	dp->d_ops->free_hdr_to_disk(bp->b_addr, &hdr);
237 	*bpp = bp;
238 	return 0;
239 }
240 
241 /*
242  * Log entries from a freespace block.
243  */
244 STATIC void
245 xfs_dir2_free_log_bests(
246 	struct xfs_da_args	*args,
247 	struct xfs_buf		*bp,
248 	int			first,		/* first entry to log */
249 	int			last)		/* last entry to log */
250 {
251 	xfs_dir2_free_t		*free;		/* freespace structure */
252 	__be16			*bests;
253 
254 	free = bp->b_addr;
255 	bests = args->dp->d_ops->free_bests_p(free);
256 	ASSERT(free->hdr.magic == cpu_to_be32(XFS_DIR2_FREE_MAGIC) ||
257 	       free->hdr.magic == cpu_to_be32(XFS_DIR3_FREE_MAGIC));
258 	xfs_trans_log_buf(args->trans, bp,
259 		(uint)((char *)&bests[first] - (char *)free),
260 		(uint)((char *)&bests[last] - (char *)free +
261 		       sizeof(bests[0]) - 1));
262 }
263 
264 /*
265  * Log header from a freespace block.
266  */
267 static void
268 xfs_dir2_free_log_header(
269 	struct xfs_da_args	*args,
270 	struct xfs_buf		*bp)
271 {
272 #ifdef DEBUG
273 	xfs_dir2_free_t		*free;		/* freespace structure */
274 
275 	free = bp->b_addr;
276 	ASSERT(free->hdr.magic == cpu_to_be32(XFS_DIR2_FREE_MAGIC) ||
277 	       free->hdr.magic == cpu_to_be32(XFS_DIR3_FREE_MAGIC));
278 #endif
279 	xfs_trans_log_buf(args->trans, bp, 0,
280 			  args->dp->d_ops->free_hdr_size - 1);
281 }
282 
283 /*
284  * Convert a leaf-format directory to a node-format directory.
285  * We need to change the magic number of the leaf block, and copy
286  * the freespace table out of the leaf block into its own block.
287  */
288 int						/* error */
289 xfs_dir2_leaf_to_node(
290 	xfs_da_args_t		*args,		/* operation arguments */
291 	struct xfs_buf		*lbp)		/* leaf buffer */
292 {
293 	xfs_inode_t		*dp;		/* incore directory inode */
294 	int			error;		/* error return value */
295 	struct xfs_buf		*fbp;		/* freespace buffer */
296 	xfs_dir2_db_t		fdb;		/* freespace block number */
297 	xfs_dir2_free_t		*free;		/* freespace structure */
298 	__be16			*from;		/* pointer to freespace entry */
299 	int			i;		/* leaf freespace index */
300 	xfs_dir2_leaf_t		*leaf;		/* leaf structure */
301 	xfs_dir2_leaf_tail_t	*ltp;		/* leaf tail structure */
302 	int			n;		/* count of live freespc ents */
303 	xfs_dir2_data_off_t	off;		/* freespace entry value */
304 	__be16			*to;		/* pointer to freespace entry */
305 	xfs_trans_t		*tp;		/* transaction pointer */
306 	struct xfs_dir3_icfree_hdr freehdr;
307 
308 	trace_xfs_dir2_leaf_to_node(args);
309 
310 	dp = args->dp;
311 	tp = args->trans;
312 	/*
313 	 * Add a freespace block to the directory.
314 	 */
315 	if ((error = xfs_dir2_grow_inode(args, XFS_DIR2_FREE_SPACE, &fdb))) {
316 		return error;
317 	}
318 	ASSERT(fdb == xfs_dir2_byte_to_db(args->geo, XFS_DIR2_FREE_OFFSET));
319 	/*
320 	 * Get the buffer for the new freespace block.
321 	 */
322 	error = xfs_dir3_free_get_buf(args, fdb, &fbp);
323 	if (error)
324 		return error;
325 
326 	free = fbp->b_addr;
327 	dp->d_ops->free_hdr_from_disk(&freehdr, free);
328 	leaf = lbp->b_addr;
329 	ltp = xfs_dir2_leaf_tail_p(args->geo, leaf);
330 	ASSERT(be32_to_cpu(ltp->bestcount) <=
331 				(uint)dp->i_d.di_size / args->geo->blksize);
332 
333 	/*
334 	 * Copy freespace entries from the leaf block to the new block.
335 	 * Count active entries.
336 	 */
337 	from = xfs_dir2_leaf_bests_p(ltp);
338 	to = dp->d_ops->free_bests_p(free);
339 	for (i = n = 0; i < be32_to_cpu(ltp->bestcount); i++, from++, to++) {
340 		if ((off = be16_to_cpu(*from)) != NULLDATAOFF)
341 			n++;
342 		*to = cpu_to_be16(off);
343 	}
344 
345 	/*
346 	 * Now initialize the freespace block header.
347 	 */
348 	freehdr.nused = n;
349 	freehdr.nvalid = be32_to_cpu(ltp->bestcount);
350 
351 	dp->d_ops->free_hdr_to_disk(fbp->b_addr, &freehdr);
352 	xfs_dir2_free_log_bests(args, fbp, 0, freehdr.nvalid - 1);
353 	xfs_dir2_free_log_header(args, fbp);
354 
355 	/*
356 	 * Converting the leaf to a leafnode is just a matter of changing the
357 	 * magic number and the ops. Do the change directly to the buffer as
358 	 * it's less work (and less code) than decoding the header to host
359 	 * format and back again.
360 	 */
361 	if (leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAF1_MAGIC))
362 		leaf->hdr.info.magic = cpu_to_be16(XFS_DIR2_LEAFN_MAGIC);
363 	else
364 		leaf->hdr.info.magic = cpu_to_be16(XFS_DIR3_LEAFN_MAGIC);
365 	lbp->b_ops = &xfs_dir3_leafn_buf_ops;
366 	xfs_trans_buf_set_type(tp, lbp, XFS_BLFT_DIR_LEAFN_BUF);
367 	xfs_dir3_leaf_log_header(args, lbp);
368 	xfs_dir3_leaf_check(dp, lbp);
369 	return 0;
370 }
371 
372 /*
373  * Add a leaf entry to a leaf block in a node-form directory.
374  * The other work necessary is done from the caller.
375  */
376 static int					/* error */
377 xfs_dir2_leafn_add(
378 	struct xfs_buf		*bp,		/* leaf buffer */
379 	xfs_da_args_t		*args,		/* operation arguments */
380 	int			index)		/* insertion pt for new entry */
381 {
382 	int			compact;	/* compacting stale leaves */
383 	xfs_inode_t		*dp;		/* incore directory inode */
384 	int			highstale;	/* next stale entry */
385 	xfs_dir2_leaf_t		*leaf;		/* leaf structure */
386 	xfs_dir2_leaf_entry_t	*lep;		/* leaf entry */
387 	int			lfloghigh;	/* high leaf entry logging */
388 	int			lfloglow;	/* low leaf entry logging */
389 	int			lowstale;	/* previous stale entry */
390 	struct xfs_dir3_icleaf_hdr leafhdr;
391 	struct xfs_dir2_leaf_entry *ents;
392 
393 	trace_xfs_dir2_leafn_add(args, index);
394 
395 	dp = args->dp;
396 	leaf = bp->b_addr;
397 	dp->d_ops->leaf_hdr_from_disk(&leafhdr, leaf);
398 	ents = dp->d_ops->leaf_ents_p(leaf);
399 
400 	/*
401 	 * Quick check just to make sure we are not going to index
402 	 * into other peoples memory
403 	 */
404 	if (index < 0)
405 		return -EFSCORRUPTED;
406 
407 	/*
408 	 * If there are already the maximum number of leaf entries in
409 	 * the block, if there are no stale entries it won't fit.
410 	 * Caller will do a split.  If there are stale entries we'll do
411 	 * a compact.
412 	 */
413 
414 	if (leafhdr.count == dp->d_ops->leaf_max_ents(args->geo)) {
415 		if (!leafhdr.stale)
416 			return -ENOSPC;
417 		compact = leafhdr.stale > 1;
418 	} else
419 		compact = 0;
420 	ASSERT(index == 0 || be32_to_cpu(ents[index - 1].hashval) <= args->hashval);
421 	ASSERT(index == leafhdr.count ||
422 	       be32_to_cpu(ents[index].hashval) >= args->hashval);
423 
424 	if (args->op_flags & XFS_DA_OP_JUSTCHECK)
425 		return 0;
426 
427 	/*
428 	 * Compact out all but one stale leaf entry.  Leaves behind
429 	 * the entry closest to index.
430 	 */
431 	if (compact)
432 		xfs_dir3_leaf_compact_x1(&leafhdr, ents, &index, &lowstale,
433 					 &highstale, &lfloglow, &lfloghigh);
434 	else if (leafhdr.stale) {
435 		/*
436 		 * Set impossible logging indices for this case.
437 		 */
438 		lfloglow = leafhdr.count;
439 		lfloghigh = -1;
440 	}
441 
442 	/*
443 	 * Insert the new entry, log everything.
444 	 */
445 	lep = xfs_dir3_leaf_find_entry(&leafhdr, ents, index, compact, lowstale,
446 				       highstale, &lfloglow, &lfloghigh);
447 
448 	lep->hashval = cpu_to_be32(args->hashval);
449 	lep->address = cpu_to_be32(xfs_dir2_db_off_to_dataptr(args->geo,
450 				args->blkno, args->index));
451 
452 	dp->d_ops->leaf_hdr_to_disk(leaf, &leafhdr);
453 	xfs_dir3_leaf_log_header(args, bp);
454 	xfs_dir3_leaf_log_ents(args, bp, lfloglow, lfloghigh);
455 	xfs_dir3_leaf_check(dp, bp);
456 	return 0;
457 }
458 
459 #ifdef DEBUG
460 static void
461 xfs_dir2_free_hdr_check(
462 	struct xfs_inode *dp,
463 	struct xfs_buf	*bp,
464 	xfs_dir2_db_t	db)
465 {
466 	struct xfs_dir3_icfree_hdr hdr;
467 
468 	dp->d_ops->free_hdr_from_disk(&hdr, bp->b_addr);
469 
470 	ASSERT((hdr.firstdb %
471 		dp->d_ops->free_max_bests(dp->i_mount->m_dir_geo)) == 0);
472 	ASSERT(hdr.firstdb <= db);
473 	ASSERT(db < hdr.firstdb + hdr.nvalid);
474 }
475 #else
476 #define xfs_dir2_free_hdr_check(dp, bp, db)
477 #endif	/* DEBUG */
478 
479 /*
480  * Return the last hash value in the leaf.
481  * Stale entries are ok.
482  */
483 xfs_dahash_t					/* hash value */
484 xfs_dir2_leafn_lasthash(
485 	struct xfs_inode *dp,
486 	struct xfs_buf	*bp,			/* leaf buffer */
487 	int		*count)			/* count of entries in leaf */
488 {
489 	struct xfs_dir2_leaf	*leaf = bp->b_addr;
490 	struct xfs_dir2_leaf_entry *ents;
491 	struct xfs_dir3_icleaf_hdr leafhdr;
492 
493 	dp->d_ops->leaf_hdr_from_disk(&leafhdr, leaf);
494 
495 	ASSERT(leafhdr.magic == XFS_DIR2_LEAFN_MAGIC ||
496 	       leafhdr.magic == XFS_DIR3_LEAFN_MAGIC);
497 
498 	if (count)
499 		*count = leafhdr.count;
500 	if (!leafhdr.count)
501 		return 0;
502 
503 	ents = dp->d_ops->leaf_ents_p(leaf);
504 	return be32_to_cpu(ents[leafhdr.count - 1].hashval);
505 }
506 
507 /*
508  * Look up a leaf entry for space to add a name in a node-format leaf block.
509  * The extrablk in state is a freespace block.
510  */
511 STATIC int
512 xfs_dir2_leafn_lookup_for_addname(
513 	struct xfs_buf		*bp,		/* leaf buffer */
514 	xfs_da_args_t		*args,		/* operation arguments */
515 	int			*indexp,	/* out: leaf entry index */
516 	xfs_da_state_t		*state)		/* state to fill in */
517 {
518 	struct xfs_buf		*curbp = NULL;	/* current data/free buffer */
519 	xfs_dir2_db_t		curdb = -1;	/* current data block number */
520 	xfs_dir2_db_t		curfdb = -1;	/* current free block number */
521 	xfs_inode_t		*dp;		/* incore directory inode */
522 	int			error;		/* error return value */
523 	int			fi;		/* free entry index */
524 	xfs_dir2_free_t		*free = NULL;	/* free block structure */
525 	int			index;		/* leaf entry index */
526 	xfs_dir2_leaf_t		*leaf;		/* leaf structure */
527 	int			length;		/* length of new data entry */
528 	xfs_dir2_leaf_entry_t	*lep;		/* leaf entry */
529 	xfs_mount_t		*mp;		/* filesystem mount point */
530 	xfs_dir2_db_t		newdb;		/* new data block number */
531 	xfs_dir2_db_t		newfdb;		/* new free block number */
532 	xfs_trans_t		*tp;		/* transaction pointer */
533 	struct xfs_dir2_leaf_entry *ents;
534 	struct xfs_dir3_icleaf_hdr leafhdr;
535 
536 	dp = args->dp;
537 	tp = args->trans;
538 	mp = dp->i_mount;
539 	leaf = bp->b_addr;
540 	dp->d_ops->leaf_hdr_from_disk(&leafhdr, leaf);
541 	ents = dp->d_ops->leaf_ents_p(leaf);
542 
543 	xfs_dir3_leaf_check(dp, bp);
544 	ASSERT(leafhdr.count > 0);
545 
546 	/*
547 	 * Look up the hash value in the leaf entries.
548 	 */
549 	index = xfs_dir2_leaf_search_hash(args, bp);
550 	/*
551 	 * Do we have a buffer coming in?
552 	 */
553 	if (state->extravalid) {
554 		/* If so, it's a free block buffer, get the block number. */
555 		curbp = state->extrablk.bp;
556 		curfdb = state->extrablk.blkno;
557 		free = curbp->b_addr;
558 		ASSERT(free->hdr.magic == cpu_to_be32(XFS_DIR2_FREE_MAGIC) ||
559 		       free->hdr.magic == cpu_to_be32(XFS_DIR3_FREE_MAGIC));
560 	}
561 	length = dp->d_ops->data_entsize(args->namelen);
562 	/*
563 	 * Loop over leaf entries with the right hash value.
564 	 */
565 	for (lep = &ents[index];
566 	     index < leafhdr.count && be32_to_cpu(lep->hashval) == args->hashval;
567 	     lep++, index++) {
568 		/*
569 		 * Skip stale leaf entries.
570 		 */
571 		if (be32_to_cpu(lep->address) == XFS_DIR2_NULL_DATAPTR)
572 			continue;
573 		/*
574 		 * Pull the data block number from the entry.
575 		 */
576 		newdb = xfs_dir2_dataptr_to_db(args->geo,
577 					       be32_to_cpu(lep->address));
578 		/*
579 		 * For addname, we're looking for a place to put the new entry.
580 		 * We want to use a data block with an entry of equal
581 		 * hash value to ours if there is one with room.
582 		 *
583 		 * If this block isn't the data block we already have
584 		 * in hand, take a look at it.
585 		 */
586 		if (newdb != curdb) {
587 			__be16 *bests;
588 
589 			curdb = newdb;
590 			/*
591 			 * Convert the data block to the free block
592 			 * holding its freespace information.
593 			 */
594 			newfdb = dp->d_ops->db_to_fdb(args->geo, newdb);
595 			/*
596 			 * If it's not the one we have in hand, read it in.
597 			 */
598 			if (newfdb != curfdb) {
599 				/*
600 				 * If we had one before, drop it.
601 				 */
602 				if (curbp)
603 					xfs_trans_brelse(tp, curbp);
604 
605 				error = xfs_dir2_free_read(tp, dp,
606 						xfs_dir2_db_to_da(args->geo,
607 								  newfdb),
608 						&curbp);
609 				if (error)
610 					return error;
611 				free = curbp->b_addr;
612 
613 				xfs_dir2_free_hdr_check(dp, curbp, curdb);
614 			}
615 			/*
616 			 * Get the index for our entry.
617 			 */
618 			fi = dp->d_ops->db_to_fdindex(args->geo, curdb);
619 			/*
620 			 * If it has room, return it.
621 			 */
622 			bests = dp->d_ops->free_bests_p(free);
623 			if (unlikely(bests[fi] == cpu_to_be16(NULLDATAOFF))) {
624 				XFS_ERROR_REPORT("xfs_dir2_leafn_lookup_int",
625 							XFS_ERRLEVEL_LOW, mp);
626 				if (curfdb != newfdb)
627 					xfs_trans_brelse(tp, curbp);
628 				return -EFSCORRUPTED;
629 			}
630 			curfdb = newfdb;
631 			if (be16_to_cpu(bests[fi]) >= length)
632 				goto out;
633 		}
634 	}
635 	/* Didn't find any space */
636 	fi = -1;
637 out:
638 	ASSERT(args->op_flags & XFS_DA_OP_OKNOENT);
639 	if (curbp) {
640 		/* Giving back a free block. */
641 		state->extravalid = 1;
642 		state->extrablk.bp = curbp;
643 		state->extrablk.index = fi;
644 		state->extrablk.blkno = curfdb;
645 
646 		/*
647 		 * Important: this magic number is not in the buffer - it's for
648 		 * buffer type information and therefore only the free/data type
649 		 * matters here, not whether CRCs are enabled or not.
650 		 */
651 		state->extrablk.magic = XFS_DIR2_FREE_MAGIC;
652 	} else {
653 		state->extravalid = 0;
654 	}
655 	/*
656 	 * Return the index, that will be the insertion point.
657 	 */
658 	*indexp = index;
659 	return -ENOENT;
660 }
661 
662 /*
663  * Look up a leaf entry in a node-format leaf block.
664  * The extrablk in state a data block.
665  */
666 STATIC int
667 xfs_dir2_leafn_lookup_for_entry(
668 	struct xfs_buf		*bp,		/* leaf buffer */
669 	xfs_da_args_t		*args,		/* operation arguments */
670 	int			*indexp,	/* out: leaf entry index */
671 	xfs_da_state_t		*state)		/* state to fill in */
672 {
673 	struct xfs_buf		*curbp = NULL;	/* current data/free buffer */
674 	xfs_dir2_db_t		curdb = -1;	/* current data block number */
675 	xfs_dir2_data_entry_t	*dep;		/* data block entry */
676 	xfs_inode_t		*dp;		/* incore directory inode */
677 	int			error;		/* error return value */
678 	int			index;		/* leaf entry index */
679 	xfs_dir2_leaf_t		*leaf;		/* leaf structure */
680 	xfs_dir2_leaf_entry_t	*lep;		/* leaf entry */
681 	xfs_mount_t		*mp;		/* filesystem mount point */
682 	xfs_dir2_db_t		newdb;		/* new data block number */
683 	xfs_trans_t		*tp;		/* transaction pointer */
684 	enum xfs_dacmp		cmp;		/* comparison result */
685 	struct xfs_dir2_leaf_entry *ents;
686 	struct xfs_dir3_icleaf_hdr leafhdr;
687 
688 	dp = args->dp;
689 	tp = args->trans;
690 	mp = dp->i_mount;
691 	leaf = bp->b_addr;
692 	dp->d_ops->leaf_hdr_from_disk(&leafhdr, leaf);
693 	ents = dp->d_ops->leaf_ents_p(leaf);
694 
695 	xfs_dir3_leaf_check(dp, bp);
696 	ASSERT(leafhdr.count > 0);
697 
698 	/*
699 	 * Look up the hash value in the leaf entries.
700 	 */
701 	index = xfs_dir2_leaf_search_hash(args, bp);
702 	/*
703 	 * Do we have a buffer coming in?
704 	 */
705 	if (state->extravalid) {
706 		curbp = state->extrablk.bp;
707 		curdb = state->extrablk.blkno;
708 	}
709 	/*
710 	 * Loop over leaf entries with the right hash value.
711 	 */
712 	for (lep = &ents[index];
713 	     index < leafhdr.count && be32_to_cpu(lep->hashval) == args->hashval;
714 	     lep++, index++) {
715 		/*
716 		 * Skip stale leaf entries.
717 		 */
718 		if (be32_to_cpu(lep->address) == XFS_DIR2_NULL_DATAPTR)
719 			continue;
720 		/*
721 		 * Pull the data block number from the entry.
722 		 */
723 		newdb = xfs_dir2_dataptr_to_db(args->geo,
724 					       be32_to_cpu(lep->address));
725 		/*
726 		 * Not adding a new entry, so we really want to find
727 		 * the name given to us.
728 		 *
729 		 * If it's a different data block, go get it.
730 		 */
731 		if (newdb != curdb) {
732 			/*
733 			 * If we had a block before that we aren't saving
734 			 * for a CI name, drop it
735 			 */
736 			if (curbp && (args->cmpresult == XFS_CMP_DIFFERENT ||
737 						curdb != state->extrablk.blkno))
738 				xfs_trans_brelse(tp, curbp);
739 			/*
740 			 * If needing the block that is saved with a CI match,
741 			 * use it otherwise read in the new data block.
742 			 */
743 			if (args->cmpresult != XFS_CMP_DIFFERENT &&
744 					newdb == state->extrablk.blkno) {
745 				ASSERT(state->extravalid);
746 				curbp = state->extrablk.bp;
747 			} else {
748 				error = xfs_dir3_data_read(tp, dp,
749 						xfs_dir2_db_to_da(args->geo,
750 								  newdb),
751 						-1, &curbp);
752 				if (error)
753 					return error;
754 			}
755 			xfs_dir3_data_check(dp, curbp);
756 			curdb = newdb;
757 		}
758 		/*
759 		 * Point to the data entry.
760 		 */
761 		dep = (xfs_dir2_data_entry_t *)((char *)curbp->b_addr +
762 			xfs_dir2_dataptr_to_off(args->geo,
763 						be32_to_cpu(lep->address)));
764 		/*
765 		 * Compare the entry and if it's an exact match, return
766 		 * EEXIST immediately. If it's the first case-insensitive
767 		 * match, store the block & inode number and continue looking.
768 		 */
769 		cmp = mp->m_dirnameops->compname(args, dep->name, dep->namelen);
770 		if (cmp != XFS_CMP_DIFFERENT && cmp != args->cmpresult) {
771 			/* If there is a CI match block, drop it */
772 			if (args->cmpresult != XFS_CMP_DIFFERENT &&
773 						curdb != state->extrablk.blkno)
774 				xfs_trans_brelse(tp, state->extrablk.bp);
775 			args->cmpresult = cmp;
776 			args->inumber = be64_to_cpu(dep->inumber);
777 			args->filetype = dp->d_ops->data_get_ftype(dep);
778 			*indexp = index;
779 			state->extravalid = 1;
780 			state->extrablk.bp = curbp;
781 			state->extrablk.blkno = curdb;
782 			state->extrablk.index = (int)((char *)dep -
783 							(char *)curbp->b_addr);
784 			state->extrablk.magic = XFS_DIR2_DATA_MAGIC;
785 			curbp->b_ops = &xfs_dir3_data_buf_ops;
786 			xfs_trans_buf_set_type(tp, curbp, XFS_BLFT_DIR_DATA_BUF);
787 			if (cmp == XFS_CMP_EXACT)
788 				return -EEXIST;
789 		}
790 	}
791 	ASSERT(index == leafhdr.count || (args->op_flags & XFS_DA_OP_OKNOENT));
792 	if (curbp) {
793 		if (args->cmpresult == XFS_CMP_DIFFERENT) {
794 			/* Giving back last used data block. */
795 			state->extravalid = 1;
796 			state->extrablk.bp = curbp;
797 			state->extrablk.index = -1;
798 			state->extrablk.blkno = curdb;
799 			state->extrablk.magic = XFS_DIR2_DATA_MAGIC;
800 			curbp->b_ops = &xfs_dir3_data_buf_ops;
801 			xfs_trans_buf_set_type(tp, curbp, XFS_BLFT_DIR_DATA_BUF);
802 		} else {
803 			/* If the curbp is not the CI match block, drop it */
804 			if (state->extrablk.bp != curbp)
805 				xfs_trans_brelse(tp, curbp);
806 		}
807 	} else {
808 		state->extravalid = 0;
809 	}
810 	*indexp = index;
811 	return -ENOENT;
812 }
813 
814 /*
815  * Look up a leaf entry in a node-format leaf block.
816  * If this is an addname then the extrablk in state is a freespace block,
817  * otherwise it's a data block.
818  */
819 int
820 xfs_dir2_leafn_lookup_int(
821 	struct xfs_buf		*bp,		/* leaf buffer */
822 	xfs_da_args_t		*args,		/* operation arguments */
823 	int			*indexp,	/* out: leaf entry index */
824 	xfs_da_state_t		*state)		/* state to fill in */
825 {
826 	if (args->op_flags & XFS_DA_OP_ADDNAME)
827 		return xfs_dir2_leafn_lookup_for_addname(bp, args, indexp,
828 							state);
829 	return xfs_dir2_leafn_lookup_for_entry(bp, args, indexp, state);
830 }
831 
832 /*
833  * Move count leaf entries from source to destination leaf.
834  * Log entries and headers.  Stale entries are preserved.
835  */
836 static void
837 xfs_dir3_leafn_moveents(
838 	xfs_da_args_t			*args,	/* operation arguments */
839 	struct xfs_buf			*bp_s,	/* source */
840 	struct xfs_dir3_icleaf_hdr	*shdr,
841 	struct xfs_dir2_leaf_entry	*sents,
842 	int				start_s,/* source leaf index */
843 	struct xfs_buf			*bp_d,	/* destination */
844 	struct xfs_dir3_icleaf_hdr	*dhdr,
845 	struct xfs_dir2_leaf_entry	*dents,
846 	int				start_d,/* destination leaf index */
847 	int				count)	/* count of leaves to copy */
848 {
849 	int				stale;	/* count stale leaves copied */
850 
851 	trace_xfs_dir2_leafn_moveents(args, start_s, start_d, count);
852 
853 	/*
854 	 * Silently return if nothing to do.
855 	 */
856 	if (count == 0)
857 		return;
858 
859 	/*
860 	 * If the destination index is not the end of the current
861 	 * destination leaf entries, open up a hole in the destination
862 	 * to hold the new entries.
863 	 */
864 	if (start_d < dhdr->count) {
865 		memmove(&dents[start_d + count], &dents[start_d],
866 			(dhdr->count - start_d) * sizeof(xfs_dir2_leaf_entry_t));
867 		xfs_dir3_leaf_log_ents(args, bp_d, start_d + count,
868 				       count + dhdr->count - 1);
869 	}
870 	/*
871 	 * If the source has stale leaves, count the ones in the copy range
872 	 * so we can update the header correctly.
873 	 */
874 	if (shdr->stale) {
875 		int	i;			/* temp leaf index */
876 
877 		for (i = start_s, stale = 0; i < start_s + count; i++) {
878 			if (sents[i].address ==
879 					cpu_to_be32(XFS_DIR2_NULL_DATAPTR))
880 				stale++;
881 		}
882 	} else
883 		stale = 0;
884 	/*
885 	 * Copy the leaf entries from source to destination.
886 	 */
887 	memcpy(&dents[start_d], &sents[start_s],
888 		count * sizeof(xfs_dir2_leaf_entry_t));
889 	xfs_dir3_leaf_log_ents(args, bp_d, start_d, start_d + count - 1);
890 
891 	/*
892 	 * If there are source entries after the ones we copied,
893 	 * delete the ones we copied by sliding the next ones down.
894 	 */
895 	if (start_s + count < shdr->count) {
896 		memmove(&sents[start_s], &sents[start_s + count],
897 			count * sizeof(xfs_dir2_leaf_entry_t));
898 		xfs_dir3_leaf_log_ents(args, bp_s, start_s, start_s + count - 1);
899 	}
900 
901 	/*
902 	 * Update the headers and log them.
903 	 */
904 	shdr->count -= count;
905 	shdr->stale -= stale;
906 	dhdr->count += count;
907 	dhdr->stale += stale;
908 }
909 
910 /*
911  * Determine the sort order of two leaf blocks.
912  * Returns 1 if both are valid and leaf2 should be before leaf1, else 0.
913  */
914 int						/* sort order */
915 xfs_dir2_leafn_order(
916 	struct xfs_inode	*dp,
917 	struct xfs_buf		*leaf1_bp,		/* leaf1 buffer */
918 	struct xfs_buf		*leaf2_bp)		/* leaf2 buffer */
919 {
920 	struct xfs_dir2_leaf	*leaf1 = leaf1_bp->b_addr;
921 	struct xfs_dir2_leaf	*leaf2 = leaf2_bp->b_addr;
922 	struct xfs_dir2_leaf_entry *ents1;
923 	struct xfs_dir2_leaf_entry *ents2;
924 	struct xfs_dir3_icleaf_hdr hdr1;
925 	struct xfs_dir3_icleaf_hdr hdr2;
926 
927 	dp->d_ops->leaf_hdr_from_disk(&hdr1, leaf1);
928 	dp->d_ops->leaf_hdr_from_disk(&hdr2, leaf2);
929 	ents1 = dp->d_ops->leaf_ents_p(leaf1);
930 	ents2 = dp->d_ops->leaf_ents_p(leaf2);
931 
932 	if (hdr1.count > 0 && hdr2.count > 0 &&
933 	    (be32_to_cpu(ents2[0].hashval) < be32_to_cpu(ents1[0].hashval) ||
934 	     be32_to_cpu(ents2[hdr2.count - 1].hashval) <
935 				be32_to_cpu(ents1[hdr1.count - 1].hashval)))
936 		return 1;
937 	return 0;
938 }
939 
940 /*
941  * Rebalance leaf entries between two leaf blocks.
942  * This is actually only called when the second block is new,
943  * though the code deals with the general case.
944  * A new entry will be inserted in one of the blocks, and that
945  * entry is taken into account when balancing.
946  */
947 static void
948 xfs_dir2_leafn_rebalance(
949 	xfs_da_state_t		*state,		/* btree cursor */
950 	xfs_da_state_blk_t	*blk1,		/* first btree block */
951 	xfs_da_state_blk_t	*blk2)		/* second btree block */
952 {
953 	xfs_da_args_t		*args;		/* operation arguments */
954 	int			count;		/* count (& direction) leaves */
955 	int			isleft;		/* new goes in left leaf */
956 	xfs_dir2_leaf_t		*leaf1;		/* first leaf structure */
957 	xfs_dir2_leaf_t		*leaf2;		/* second leaf structure */
958 	int			mid;		/* midpoint leaf index */
959 #if defined(DEBUG) || defined(XFS_WARN)
960 	int			oldstale;	/* old count of stale leaves */
961 #endif
962 	int			oldsum;		/* old total leaf count */
963 	int			swap;		/* swapped leaf blocks */
964 	struct xfs_dir2_leaf_entry *ents1;
965 	struct xfs_dir2_leaf_entry *ents2;
966 	struct xfs_dir3_icleaf_hdr hdr1;
967 	struct xfs_dir3_icleaf_hdr hdr2;
968 	struct xfs_inode	*dp = state->args->dp;
969 
970 	args = state->args;
971 	/*
972 	 * If the block order is wrong, swap the arguments.
973 	 */
974 	if ((swap = xfs_dir2_leafn_order(dp, blk1->bp, blk2->bp))) {
975 		xfs_da_state_blk_t	*tmp;	/* temp for block swap */
976 
977 		tmp = blk1;
978 		blk1 = blk2;
979 		blk2 = tmp;
980 	}
981 	leaf1 = blk1->bp->b_addr;
982 	leaf2 = blk2->bp->b_addr;
983 	dp->d_ops->leaf_hdr_from_disk(&hdr1, leaf1);
984 	dp->d_ops->leaf_hdr_from_disk(&hdr2, leaf2);
985 	ents1 = dp->d_ops->leaf_ents_p(leaf1);
986 	ents2 = dp->d_ops->leaf_ents_p(leaf2);
987 
988 	oldsum = hdr1.count + hdr2.count;
989 #if defined(DEBUG) || defined(XFS_WARN)
990 	oldstale = hdr1.stale + hdr2.stale;
991 #endif
992 	mid = oldsum >> 1;
993 
994 	/*
995 	 * If the old leaf count was odd then the new one will be even,
996 	 * so we need to divide the new count evenly.
997 	 */
998 	if (oldsum & 1) {
999 		xfs_dahash_t	midhash;	/* middle entry hash value */
1000 
1001 		if (mid >= hdr1.count)
1002 			midhash = be32_to_cpu(ents2[mid - hdr1.count].hashval);
1003 		else
1004 			midhash = be32_to_cpu(ents1[mid].hashval);
1005 		isleft = args->hashval <= midhash;
1006 	}
1007 	/*
1008 	 * If the old count is even then the new count is odd, so there's
1009 	 * no preferred side for the new entry.
1010 	 * Pick the left one.
1011 	 */
1012 	else
1013 		isleft = 1;
1014 	/*
1015 	 * Calculate moved entry count.  Positive means left-to-right,
1016 	 * negative means right-to-left.  Then move the entries.
1017 	 */
1018 	count = hdr1.count - mid + (isleft == 0);
1019 	if (count > 0)
1020 		xfs_dir3_leafn_moveents(args, blk1->bp, &hdr1, ents1,
1021 					hdr1.count - count, blk2->bp,
1022 					&hdr2, ents2, 0, count);
1023 	else if (count < 0)
1024 		xfs_dir3_leafn_moveents(args, blk2->bp, &hdr2, ents2, 0,
1025 					blk1->bp, &hdr1, ents1,
1026 					hdr1.count, count);
1027 
1028 	ASSERT(hdr1.count + hdr2.count == oldsum);
1029 	ASSERT(hdr1.stale + hdr2.stale == oldstale);
1030 
1031 	/* log the changes made when moving the entries */
1032 	dp->d_ops->leaf_hdr_to_disk(leaf1, &hdr1);
1033 	dp->d_ops->leaf_hdr_to_disk(leaf2, &hdr2);
1034 	xfs_dir3_leaf_log_header(args, blk1->bp);
1035 	xfs_dir3_leaf_log_header(args, blk2->bp);
1036 
1037 	xfs_dir3_leaf_check(dp, blk1->bp);
1038 	xfs_dir3_leaf_check(dp, blk2->bp);
1039 
1040 	/*
1041 	 * Mark whether we're inserting into the old or new leaf.
1042 	 */
1043 	if (hdr1.count < hdr2.count)
1044 		state->inleaf = swap;
1045 	else if (hdr1.count > hdr2.count)
1046 		state->inleaf = !swap;
1047 	else
1048 		state->inleaf = swap ^ (blk1->index <= hdr1.count);
1049 	/*
1050 	 * Adjust the expected index for insertion.
1051 	 */
1052 	if (!state->inleaf)
1053 		blk2->index = blk1->index - hdr1.count;
1054 
1055 	/*
1056 	 * Finally sanity check just to make sure we are not returning a
1057 	 * negative index
1058 	 */
1059 	if (blk2->index < 0) {
1060 		state->inleaf = 1;
1061 		blk2->index = 0;
1062 		xfs_alert(dp->i_mount,
1063 	"%s: picked the wrong leaf? reverting original leaf: blk1->index %d",
1064 			__func__, blk1->index);
1065 	}
1066 }
1067 
1068 static int
1069 xfs_dir3_data_block_free(
1070 	xfs_da_args_t		*args,
1071 	struct xfs_dir2_data_hdr *hdr,
1072 	struct xfs_dir2_free	*free,
1073 	xfs_dir2_db_t		fdb,
1074 	int			findex,
1075 	struct xfs_buf		*fbp,
1076 	int			longest)
1077 {
1078 	int			logfree = 0;
1079 	__be16			*bests;
1080 	struct xfs_dir3_icfree_hdr freehdr;
1081 	struct xfs_inode	*dp = args->dp;
1082 
1083 	dp->d_ops->free_hdr_from_disk(&freehdr, free);
1084 	bests = dp->d_ops->free_bests_p(free);
1085 	if (hdr) {
1086 		/*
1087 		 * Data block is not empty, just set the free entry to the new
1088 		 * value.
1089 		 */
1090 		bests[findex] = cpu_to_be16(longest);
1091 		xfs_dir2_free_log_bests(args, fbp, findex, findex);
1092 		return 0;
1093 	}
1094 
1095 	/* One less used entry in the free table. */
1096 	freehdr.nused--;
1097 
1098 	/*
1099 	 * If this was the last entry in the table, we can trim the table size
1100 	 * back.  There might be other entries at the end referring to
1101 	 * non-existent data blocks, get those too.
1102 	 */
1103 	if (findex == freehdr.nvalid - 1) {
1104 		int	i;		/* free entry index */
1105 
1106 		for (i = findex - 1; i >= 0; i--) {
1107 			if (bests[i] != cpu_to_be16(NULLDATAOFF))
1108 				break;
1109 		}
1110 		freehdr.nvalid = i + 1;
1111 		logfree = 0;
1112 	} else {
1113 		/* Not the last entry, just punch it out.  */
1114 		bests[findex] = cpu_to_be16(NULLDATAOFF);
1115 		logfree = 1;
1116 	}
1117 
1118 	dp->d_ops->free_hdr_to_disk(free, &freehdr);
1119 	xfs_dir2_free_log_header(args, fbp);
1120 
1121 	/*
1122 	 * If there are no useful entries left in the block, get rid of the
1123 	 * block if we can.
1124 	 */
1125 	if (!freehdr.nused) {
1126 		int error;
1127 
1128 		error = xfs_dir2_shrink_inode(args, fdb, fbp);
1129 		if (error == 0) {
1130 			fbp = NULL;
1131 			logfree = 0;
1132 		} else if (error != -ENOSPC || args->total != 0)
1133 			return error;
1134 		/*
1135 		 * It's possible to get ENOSPC if there is no
1136 		 * space reservation.  In this case some one
1137 		 * else will eventually get rid of this block.
1138 		 */
1139 	}
1140 
1141 	/* Log the free entry that changed, unless we got rid of it.  */
1142 	if (logfree)
1143 		xfs_dir2_free_log_bests(args, fbp, findex, findex);
1144 	return 0;
1145 }
1146 
1147 /*
1148  * Remove an entry from a node directory.
1149  * This removes the leaf entry and the data entry,
1150  * and updates the free block if necessary.
1151  */
1152 static int					/* error */
1153 xfs_dir2_leafn_remove(
1154 	xfs_da_args_t		*args,		/* operation arguments */
1155 	struct xfs_buf		*bp,		/* leaf buffer */
1156 	int			index,		/* leaf entry index */
1157 	xfs_da_state_blk_t	*dblk,		/* data block */
1158 	int			*rval)		/* resulting block needs join */
1159 {
1160 	xfs_dir2_data_hdr_t	*hdr;		/* data block header */
1161 	xfs_dir2_db_t		db;		/* data block number */
1162 	struct xfs_buf		*dbp;		/* data block buffer */
1163 	xfs_dir2_data_entry_t	*dep;		/* data block entry */
1164 	xfs_inode_t		*dp;		/* incore directory inode */
1165 	xfs_dir2_leaf_t		*leaf;		/* leaf structure */
1166 	xfs_dir2_leaf_entry_t	*lep;		/* leaf entry */
1167 	int			longest;	/* longest data free entry */
1168 	int			off;		/* data block entry offset */
1169 	int			needlog;	/* need to log data header */
1170 	int			needscan;	/* need to rescan data frees */
1171 	xfs_trans_t		*tp;		/* transaction pointer */
1172 	struct xfs_dir2_data_free *bf;		/* bestfree table */
1173 	struct xfs_dir3_icleaf_hdr leafhdr;
1174 	struct xfs_dir2_leaf_entry *ents;
1175 
1176 	trace_xfs_dir2_leafn_remove(args, index);
1177 
1178 	dp = args->dp;
1179 	tp = args->trans;
1180 	leaf = bp->b_addr;
1181 	dp->d_ops->leaf_hdr_from_disk(&leafhdr, leaf);
1182 	ents = dp->d_ops->leaf_ents_p(leaf);
1183 
1184 	/*
1185 	 * Point to the entry we're removing.
1186 	 */
1187 	lep = &ents[index];
1188 
1189 	/*
1190 	 * Extract the data block and offset from the entry.
1191 	 */
1192 	db = xfs_dir2_dataptr_to_db(args->geo, be32_to_cpu(lep->address));
1193 	ASSERT(dblk->blkno == db);
1194 	off = xfs_dir2_dataptr_to_off(args->geo, be32_to_cpu(lep->address));
1195 	ASSERT(dblk->index == off);
1196 
1197 	/*
1198 	 * Kill the leaf entry by marking it stale.
1199 	 * Log the leaf block changes.
1200 	 */
1201 	leafhdr.stale++;
1202 	dp->d_ops->leaf_hdr_to_disk(leaf, &leafhdr);
1203 	xfs_dir3_leaf_log_header(args, bp);
1204 
1205 	lep->address = cpu_to_be32(XFS_DIR2_NULL_DATAPTR);
1206 	xfs_dir3_leaf_log_ents(args, bp, index, index);
1207 
1208 	/*
1209 	 * Make the data entry free.  Keep track of the longest freespace
1210 	 * in the data block in case it changes.
1211 	 */
1212 	dbp = dblk->bp;
1213 	hdr = dbp->b_addr;
1214 	dep = (xfs_dir2_data_entry_t *)((char *)hdr + off);
1215 	bf = dp->d_ops->data_bestfree_p(hdr);
1216 	longest = be16_to_cpu(bf[0].length);
1217 	needlog = needscan = 0;
1218 	xfs_dir2_data_make_free(args, dbp, off,
1219 		dp->d_ops->data_entsize(dep->namelen), &needlog, &needscan);
1220 	/*
1221 	 * Rescan the data block freespaces for bestfree.
1222 	 * Log the data block header if needed.
1223 	 */
1224 	if (needscan)
1225 		xfs_dir2_data_freescan(dp, hdr, &needlog);
1226 	if (needlog)
1227 		xfs_dir2_data_log_header(args, dbp);
1228 	xfs_dir3_data_check(dp, dbp);
1229 	/*
1230 	 * If the longest data block freespace changes, need to update
1231 	 * the corresponding freeblock entry.
1232 	 */
1233 	if (longest < be16_to_cpu(bf[0].length)) {
1234 		int		error;		/* error return value */
1235 		struct xfs_buf	*fbp;		/* freeblock buffer */
1236 		xfs_dir2_db_t	fdb;		/* freeblock block number */
1237 		int		findex;		/* index in freeblock entries */
1238 		xfs_dir2_free_t	*free;		/* freeblock structure */
1239 
1240 		/*
1241 		 * Convert the data block number to a free block,
1242 		 * read in the free block.
1243 		 */
1244 		fdb = dp->d_ops->db_to_fdb(args->geo, db);
1245 		error = xfs_dir2_free_read(tp, dp,
1246 					   xfs_dir2_db_to_da(args->geo, fdb),
1247 					   &fbp);
1248 		if (error)
1249 			return error;
1250 		free = fbp->b_addr;
1251 #ifdef DEBUG
1252 	{
1253 		struct xfs_dir3_icfree_hdr freehdr;
1254 		dp->d_ops->free_hdr_from_disk(&freehdr, free);
1255 		ASSERT(freehdr.firstdb == dp->d_ops->free_max_bests(args->geo) *
1256 			(fdb - xfs_dir2_byte_to_db(args->geo,
1257 						   XFS_DIR2_FREE_OFFSET)));
1258 	}
1259 #endif
1260 		/*
1261 		 * Calculate which entry we need to fix.
1262 		 */
1263 		findex = dp->d_ops->db_to_fdindex(args->geo, db);
1264 		longest = be16_to_cpu(bf[0].length);
1265 		/*
1266 		 * If the data block is now empty we can get rid of it
1267 		 * (usually).
1268 		 */
1269 		if (longest == args->geo->blksize -
1270 			       dp->d_ops->data_entry_offset) {
1271 			/*
1272 			 * Try to punch out the data block.
1273 			 */
1274 			error = xfs_dir2_shrink_inode(args, db, dbp);
1275 			if (error == 0) {
1276 				dblk->bp = NULL;
1277 				hdr = NULL;
1278 			}
1279 			/*
1280 			 * We can get ENOSPC if there's no space reservation.
1281 			 * In this case just drop the buffer and some one else
1282 			 * will eventually get rid of the empty block.
1283 			 */
1284 			else if (!(error == -ENOSPC && args->total == 0))
1285 				return error;
1286 		}
1287 		/*
1288 		 * If we got rid of the data block, we can eliminate that entry
1289 		 * in the free block.
1290 		 */
1291 		error = xfs_dir3_data_block_free(args, hdr, free,
1292 						 fdb, findex, fbp, longest);
1293 		if (error)
1294 			return error;
1295 	}
1296 
1297 	xfs_dir3_leaf_check(dp, bp);
1298 	/*
1299 	 * Return indication of whether this leaf block is empty enough
1300 	 * to justify trying to join it with a neighbor.
1301 	 */
1302 	*rval = (dp->d_ops->leaf_hdr_size +
1303 		 (uint)sizeof(ents[0]) * (leafhdr.count - leafhdr.stale)) <
1304 		args->geo->magicpct;
1305 	return 0;
1306 }
1307 
1308 /*
1309  * Split the leaf entries in the old block into old and new blocks.
1310  */
1311 int						/* error */
1312 xfs_dir2_leafn_split(
1313 	xfs_da_state_t		*state,		/* btree cursor */
1314 	xfs_da_state_blk_t	*oldblk,	/* original block */
1315 	xfs_da_state_blk_t	*newblk)	/* newly created block */
1316 {
1317 	xfs_da_args_t		*args;		/* operation arguments */
1318 	xfs_dablk_t		blkno;		/* new leaf block number */
1319 	int			error;		/* error return value */
1320 	struct xfs_inode	*dp;
1321 
1322 	/*
1323 	 * Allocate space for a new leaf node.
1324 	 */
1325 	args = state->args;
1326 	dp = args->dp;
1327 	ASSERT(oldblk->magic == XFS_DIR2_LEAFN_MAGIC);
1328 	error = xfs_da_grow_inode(args, &blkno);
1329 	if (error) {
1330 		return error;
1331 	}
1332 	/*
1333 	 * Initialize the new leaf block.
1334 	 */
1335 	error = xfs_dir3_leaf_get_buf(args, xfs_dir2_da_to_db(args->geo, blkno),
1336 				      &newblk->bp, XFS_DIR2_LEAFN_MAGIC);
1337 	if (error)
1338 		return error;
1339 
1340 	newblk->blkno = blkno;
1341 	newblk->magic = XFS_DIR2_LEAFN_MAGIC;
1342 	/*
1343 	 * Rebalance the entries across the two leaves, link the new
1344 	 * block into the leaves.
1345 	 */
1346 	xfs_dir2_leafn_rebalance(state, oldblk, newblk);
1347 	error = xfs_da3_blk_link(state, oldblk, newblk);
1348 	if (error) {
1349 		return error;
1350 	}
1351 	/*
1352 	 * Insert the new entry in the correct block.
1353 	 */
1354 	if (state->inleaf)
1355 		error = xfs_dir2_leafn_add(oldblk->bp, args, oldblk->index);
1356 	else
1357 		error = xfs_dir2_leafn_add(newblk->bp, args, newblk->index);
1358 	/*
1359 	 * Update last hashval in each block since we added the name.
1360 	 */
1361 	oldblk->hashval = xfs_dir2_leafn_lasthash(dp, oldblk->bp, NULL);
1362 	newblk->hashval = xfs_dir2_leafn_lasthash(dp, newblk->bp, NULL);
1363 	xfs_dir3_leaf_check(dp, oldblk->bp);
1364 	xfs_dir3_leaf_check(dp, newblk->bp);
1365 	return error;
1366 }
1367 
1368 /*
1369  * Check a leaf block and its neighbors to see if the block should be
1370  * collapsed into one or the other neighbor.  Always keep the block
1371  * with the smaller block number.
1372  * If the current block is over 50% full, don't try to join it, return 0.
1373  * If the block is empty, fill in the state structure and return 2.
1374  * If it can be collapsed, fill in the state structure and return 1.
1375  * If nothing can be done, return 0.
1376  */
1377 int						/* error */
1378 xfs_dir2_leafn_toosmall(
1379 	xfs_da_state_t		*state,		/* btree cursor */
1380 	int			*action)	/* resulting action to take */
1381 {
1382 	xfs_da_state_blk_t	*blk;		/* leaf block */
1383 	xfs_dablk_t		blkno;		/* leaf block number */
1384 	struct xfs_buf		*bp;		/* leaf buffer */
1385 	int			bytes;		/* bytes in use */
1386 	int			count;		/* leaf live entry count */
1387 	int			error;		/* error return value */
1388 	int			forward;	/* sibling block direction */
1389 	int			i;		/* sibling counter */
1390 	xfs_dir2_leaf_t		*leaf;		/* leaf structure */
1391 	int			rval;		/* result from path_shift */
1392 	struct xfs_dir3_icleaf_hdr leafhdr;
1393 	struct xfs_dir2_leaf_entry *ents;
1394 	struct xfs_inode	*dp = state->args->dp;
1395 
1396 	/*
1397 	 * Check for the degenerate case of the block being over 50% full.
1398 	 * If so, it's not worth even looking to see if we might be able
1399 	 * to coalesce with a sibling.
1400 	 */
1401 	blk = &state->path.blk[state->path.active - 1];
1402 	leaf = blk->bp->b_addr;
1403 	dp->d_ops->leaf_hdr_from_disk(&leafhdr, leaf);
1404 	ents = dp->d_ops->leaf_ents_p(leaf);
1405 	xfs_dir3_leaf_check(dp, blk->bp);
1406 
1407 	count = leafhdr.count - leafhdr.stale;
1408 	bytes = dp->d_ops->leaf_hdr_size + count * sizeof(ents[0]);
1409 	if (bytes > (state->args->geo->blksize >> 1)) {
1410 		/*
1411 		 * Blk over 50%, don't try to join.
1412 		 */
1413 		*action = 0;
1414 		return 0;
1415 	}
1416 	/*
1417 	 * Check for the degenerate case of the block being empty.
1418 	 * If the block is empty, we'll simply delete it, no need to
1419 	 * coalesce it with a sibling block.  We choose (arbitrarily)
1420 	 * to merge with the forward block unless it is NULL.
1421 	 */
1422 	if (count == 0) {
1423 		/*
1424 		 * Make altpath point to the block we want to keep and
1425 		 * path point to the block we want to drop (this one).
1426 		 */
1427 		forward = (leafhdr.forw != 0);
1428 		memcpy(&state->altpath, &state->path, sizeof(state->path));
1429 		error = xfs_da3_path_shift(state, &state->altpath, forward, 0,
1430 			&rval);
1431 		if (error)
1432 			return error;
1433 		*action = rval ? 2 : 0;
1434 		return 0;
1435 	}
1436 	/*
1437 	 * Examine each sibling block to see if we can coalesce with
1438 	 * at least 25% free space to spare.  We need to figure out
1439 	 * whether to merge with the forward or the backward block.
1440 	 * We prefer coalescing with the lower numbered sibling so as
1441 	 * to shrink a directory over time.
1442 	 */
1443 	forward = leafhdr.forw < leafhdr.back;
1444 	for (i = 0, bp = NULL; i < 2; forward = !forward, i++) {
1445 		struct xfs_dir3_icleaf_hdr hdr2;
1446 
1447 		blkno = forward ? leafhdr.forw : leafhdr.back;
1448 		if (blkno == 0)
1449 			continue;
1450 		/*
1451 		 * Read the sibling leaf block.
1452 		 */
1453 		error = xfs_dir3_leafn_read(state->args->trans, dp,
1454 					    blkno, -1, &bp);
1455 		if (error)
1456 			return error;
1457 
1458 		/*
1459 		 * Count bytes in the two blocks combined.
1460 		 */
1461 		count = leafhdr.count - leafhdr.stale;
1462 		bytes = state->args->geo->blksize -
1463 			(state->args->geo->blksize >> 2);
1464 
1465 		leaf = bp->b_addr;
1466 		dp->d_ops->leaf_hdr_from_disk(&hdr2, leaf);
1467 		ents = dp->d_ops->leaf_ents_p(leaf);
1468 		count += hdr2.count - hdr2.stale;
1469 		bytes -= count * sizeof(ents[0]);
1470 
1471 		/*
1472 		 * Fits with at least 25% to spare.
1473 		 */
1474 		if (bytes >= 0)
1475 			break;
1476 		xfs_trans_brelse(state->args->trans, bp);
1477 	}
1478 	/*
1479 	 * Didn't like either block, give up.
1480 	 */
1481 	if (i >= 2) {
1482 		*action = 0;
1483 		return 0;
1484 	}
1485 
1486 	/*
1487 	 * Make altpath point to the block we want to keep (the lower
1488 	 * numbered block) and path point to the block we want to drop.
1489 	 */
1490 	memcpy(&state->altpath, &state->path, sizeof(state->path));
1491 	if (blkno < blk->blkno)
1492 		error = xfs_da3_path_shift(state, &state->altpath, forward, 0,
1493 			&rval);
1494 	else
1495 		error = xfs_da3_path_shift(state, &state->path, forward, 0,
1496 			&rval);
1497 	if (error) {
1498 		return error;
1499 	}
1500 	*action = rval ? 0 : 1;
1501 	return 0;
1502 }
1503 
1504 /*
1505  * Move all the leaf entries from drop_blk to save_blk.
1506  * This is done as part of a join operation.
1507  */
1508 void
1509 xfs_dir2_leafn_unbalance(
1510 	xfs_da_state_t		*state,		/* cursor */
1511 	xfs_da_state_blk_t	*drop_blk,	/* dead block */
1512 	xfs_da_state_blk_t	*save_blk)	/* surviving block */
1513 {
1514 	xfs_da_args_t		*args;		/* operation arguments */
1515 	xfs_dir2_leaf_t		*drop_leaf;	/* dead leaf structure */
1516 	xfs_dir2_leaf_t		*save_leaf;	/* surviving leaf structure */
1517 	struct xfs_dir3_icleaf_hdr savehdr;
1518 	struct xfs_dir3_icleaf_hdr drophdr;
1519 	struct xfs_dir2_leaf_entry *sents;
1520 	struct xfs_dir2_leaf_entry *dents;
1521 	struct xfs_inode	*dp = state->args->dp;
1522 
1523 	args = state->args;
1524 	ASSERT(drop_blk->magic == XFS_DIR2_LEAFN_MAGIC);
1525 	ASSERT(save_blk->magic == XFS_DIR2_LEAFN_MAGIC);
1526 	drop_leaf = drop_blk->bp->b_addr;
1527 	save_leaf = save_blk->bp->b_addr;
1528 
1529 	dp->d_ops->leaf_hdr_from_disk(&savehdr, save_leaf);
1530 	dp->d_ops->leaf_hdr_from_disk(&drophdr, drop_leaf);
1531 	sents = dp->d_ops->leaf_ents_p(save_leaf);
1532 	dents = dp->d_ops->leaf_ents_p(drop_leaf);
1533 
1534 	/*
1535 	 * If there are any stale leaf entries, take this opportunity
1536 	 * to purge them.
1537 	 */
1538 	if (drophdr.stale)
1539 		xfs_dir3_leaf_compact(args, &drophdr, drop_blk->bp);
1540 	if (savehdr.stale)
1541 		xfs_dir3_leaf_compact(args, &savehdr, save_blk->bp);
1542 
1543 	/*
1544 	 * Move the entries from drop to the appropriate end of save.
1545 	 */
1546 	drop_blk->hashval = be32_to_cpu(dents[drophdr.count - 1].hashval);
1547 	if (xfs_dir2_leafn_order(dp, save_blk->bp, drop_blk->bp))
1548 		xfs_dir3_leafn_moveents(args, drop_blk->bp, &drophdr, dents, 0,
1549 					save_blk->bp, &savehdr, sents, 0,
1550 					drophdr.count);
1551 	else
1552 		xfs_dir3_leafn_moveents(args, drop_blk->bp, &drophdr, dents, 0,
1553 					save_blk->bp, &savehdr, sents,
1554 					savehdr.count, drophdr.count);
1555 	save_blk->hashval = be32_to_cpu(sents[savehdr.count - 1].hashval);
1556 
1557 	/* log the changes made when moving the entries */
1558 	dp->d_ops->leaf_hdr_to_disk(save_leaf, &savehdr);
1559 	dp->d_ops->leaf_hdr_to_disk(drop_leaf, &drophdr);
1560 	xfs_dir3_leaf_log_header(args, save_blk->bp);
1561 	xfs_dir3_leaf_log_header(args, drop_blk->bp);
1562 
1563 	xfs_dir3_leaf_check(dp, save_blk->bp);
1564 	xfs_dir3_leaf_check(dp, drop_blk->bp);
1565 }
1566 
1567 /*
1568  * Top-level node form directory addname routine.
1569  */
1570 int						/* error */
1571 xfs_dir2_node_addname(
1572 	xfs_da_args_t		*args)		/* operation arguments */
1573 {
1574 	xfs_da_state_blk_t	*blk;		/* leaf block for insert */
1575 	int			error;		/* error return value */
1576 	int			rval;		/* sub-return value */
1577 	xfs_da_state_t		*state;		/* btree cursor */
1578 
1579 	trace_xfs_dir2_node_addname(args);
1580 
1581 	/*
1582 	 * Allocate and initialize the state (btree cursor).
1583 	 */
1584 	state = xfs_da_state_alloc();
1585 	state->args = args;
1586 	state->mp = args->dp->i_mount;
1587 	/*
1588 	 * Look up the name.  We're not supposed to find it, but
1589 	 * this gives us the insertion point.
1590 	 */
1591 	error = xfs_da3_node_lookup_int(state, &rval);
1592 	if (error)
1593 		rval = error;
1594 	if (rval != -ENOENT) {
1595 		goto done;
1596 	}
1597 	/*
1598 	 * Add the data entry to a data block.
1599 	 * Extravalid is set to a freeblock found by lookup.
1600 	 */
1601 	rval = xfs_dir2_node_addname_int(args,
1602 		state->extravalid ? &state->extrablk : NULL);
1603 	if (rval) {
1604 		goto done;
1605 	}
1606 	blk = &state->path.blk[state->path.active - 1];
1607 	ASSERT(blk->magic == XFS_DIR2_LEAFN_MAGIC);
1608 	/*
1609 	 * Add the new leaf entry.
1610 	 */
1611 	rval = xfs_dir2_leafn_add(blk->bp, args, blk->index);
1612 	if (rval == 0) {
1613 		/*
1614 		 * It worked, fix the hash values up the btree.
1615 		 */
1616 		if (!(args->op_flags & XFS_DA_OP_JUSTCHECK))
1617 			xfs_da3_fixhashpath(state, &state->path);
1618 	} else {
1619 		/*
1620 		 * It didn't work, we need to split the leaf block.
1621 		 */
1622 		if (args->total == 0) {
1623 			ASSERT(rval == -ENOSPC);
1624 			goto done;
1625 		}
1626 		/*
1627 		 * Split the leaf block and insert the new entry.
1628 		 */
1629 		rval = xfs_da3_split(state);
1630 	}
1631 done:
1632 	xfs_da_state_free(state);
1633 	return rval;
1634 }
1635 
1636 /*
1637  * Add the data entry for a node-format directory name addition.
1638  * The leaf entry is added in xfs_dir2_leafn_add.
1639  * We may enter with a freespace block that the lookup found.
1640  */
1641 static int					/* error */
1642 xfs_dir2_node_addname_int(
1643 	xfs_da_args_t		*args,		/* operation arguments */
1644 	xfs_da_state_blk_t	*fblk)		/* optional freespace block */
1645 {
1646 	xfs_dir2_data_hdr_t	*hdr;		/* data block header */
1647 	xfs_dir2_db_t		dbno;		/* data block number */
1648 	struct xfs_buf		*dbp;		/* data block buffer */
1649 	xfs_dir2_data_entry_t	*dep;		/* data entry pointer */
1650 	xfs_inode_t		*dp;		/* incore directory inode */
1651 	xfs_dir2_data_unused_t	*dup;		/* data unused entry pointer */
1652 	int			error;		/* error return value */
1653 	xfs_dir2_db_t		fbno;		/* freespace block number */
1654 	struct xfs_buf		*fbp;		/* freespace buffer */
1655 	int			findex;		/* freespace entry index */
1656 	xfs_dir2_free_t		*free=NULL;	/* freespace block structure */
1657 	xfs_dir2_db_t		ifbno;		/* initial freespace block no */
1658 	xfs_dir2_db_t		lastfbno=0;	/* highest freespace block no */
1659 	int			length;		/* length of the new entry */
1660 	int			logfree;	/* need to log free entry */
1661 	xfs_mount_t		*mp;		/* filesystem mount point */
1662 	int			needlog;	/* need to log data header */
1663 	int			needscan;	/* need to rescan data frees */
1664 	__be16			*tagp;		/* data entry tag pointer */
1665 	xfs_trans_t		*tp;		/* transaction pointer */
1666 	__be16			*bests;
1667 	struct xfs_dir3_icfree_hdr freehdr;
1668 	struct xfs_dir2_data_free *bf;
1669 
1670 	dp = args->dp;
1671 	mp = dp->i_mount;
1672 	tp = args->trans;
1673 	length = dp->d_ops->data_entsize(args->namelen);
1674 	/*
1675 	 * If we came in with a freespace block that means that lookup
1676 	 * found an entry with our hash value.  This is the freespace
1677 	 * block for that data entry.
1678 	 */
1679 	if (fblk) {
1680 		fbp = fblk->bp;
1681 		/*
1682 		 * Remember initial freespace block number.
1683 		 */
1684 		ifbno = fblk->blkno;
1685 		free = fbp->b_addr;
1686 		findex = fblk->index;
1687 		bests = dp->d_ops->free_bests_p(free);
1688 		dp->d_ops->free_hdr_from_disk(&freehdr, free);
1689 
1690 		/*
1691 		 * This means the free entry showed that the data block had
1692 		 * space for our entry, so we remembered it.
1693 		 * Use that data block.
1694 		 */
1695 		if (findex >= 0) {
1696 			ASSERT(findex < freehdr.nvalid);
1697 			ASSERT(be16_to_cpu(bests[findex]) != NULLDATAOFF);
1698 			ASSERT(be16_to_cpu(bests[findex]) >= length);
1699 			dbno = freehdr.firstdb + findex;
1700 		} else {
1701 			/*
1702 			 * The data block looked at didn't have enough room.
1703 			 * We'll start at the beginning of the freespace entries.
1704 			 */
1705 			dbno = -1;
1706 			findex = 0;
1707 		}
1708 	} else {
1709 		/*
1710 		 * Didn't come in with a freespace block, so no data block.
1711 		 */
1712 		ifbno = dbno = -1;
1713 		fbp = NULL;
1714 		findex = 0;
1715 	}
1716 
1717 	/*
1718 	 * If we don't have a data block yet, we're going to scan the
1719 	 * freespace blocks looking for one.  Figure out what the
1720 	 * highest freespace block number is.
1721 	 */
1722 	if (dbno == -1) {
1723 		xfs_fileoff_t	fo;		/* freespace block number */
1724 
1725 		if ((error = xfs_bmap_last_offset(dp, &fo, XFS_DATA_FORK)))
1726 			return error;
1727 		lastfbno = xfs_dir2_da_to_db(args->geo, (xfs_dablk_t)fo);
1728 		fbno = ifbno;
1729 	}
1730 	/*
1731 	 * While we haven't identified a data block, search the freeblock
1732 	 * data for a good data block.  If we find a null freeblock entry,
1733 	 * indicating a hole in the data blocks, remember that.
1734 	 */
1735 	while (dbno == -1) {
1736 		/*
1737 		 * If we don't have a freeblock in hand, get the next one.
1738 		 */
1739 		if (fbp == NULL) {
1740 			/*
1741 			 * Happens the first time through unless lookup gave
1742 			 * us a freespace block to start with.
1743 			 */
1744 			if (++fbno == 0)
1745 				fbno = xfs_dir2_byte_to_db(args->geo,
1746 							XFS_DIR2_FREE_OFFSET);
1747 			/*
1748 			 * If it's ifbno we already looked at it.
1749 			 */
1750 			if (fbno == ifbno)
1751 				fbno++;
1752 			/*
1753 			 * If it's off the end we're done.
1754 			 */
1755 			if (fbno >= lastfbno)
1756 				break;
1757 			/*
1758 			 * Read the block.  There can be holes in the
1759 			 * freespace blocks, so this might not succeed.
1760 			 * This should be really rare, so there's no reason
1761 			 * to avoid it.
1762 			 */
1763 			error = xfs_dir2_free_try_read(tp, dp,
1764 					xfs_dir2_db_to_da(args->geo, fbno),
1765 					&fbp);
1766 			if (error)
1767 				return error;
1768 			if (!fbp)
1769 				continue;
1770 			free = fbp->b_addr;
1771 			findex = 0;
1772 		}
1773 		/*
1774 		 * Look at the current free entry.  Is it good enough?
1775 		 *
1776 		 * The bests initialisation should be where the bufer is read in
1777 		 * the above branch. But gcc is too stupid to realise that bests
1778 		 * and the freehdr are actually initialised if they are placed
1779 		 * there, so we have to do it here to avoid warnings. Blech.
1780 		 */
1781 		bests = dp->d_ops->free_bests_p(free);
1782 		dp->d_ops->free_hdr_from_disk(&freehdr, free);
1783 		if (be16_to_cpu(bests[findex]) != NULLDATAOFF &&
1784 		    be16_to_cpu(bests[findex]) >= length)
1785 			dbno = freehdr.firstdb + findex;
1786 		else {
1787 			/*
1788 			 * Are we done with the freeblock?
1789 			 */
1790 			if (++findex == freehdr.nvalid) {
1791 				/*
1792 				 * Drop the block.
1793 				 */
1794 				xfs_trans_brelse(tp, fbp);
1795 				fbp = NULL;
1796 				if (fblk && fblk->bp)
1797 					fblk->bp = NULL;
1798 			}
1799 		}
1800 	}
1801 	/*
1802 	 * If we don't have a data block, we need to allocate one and make
1803 	 * the freespace entries refer to it.
1804 	 */
1805 	if (unlikely(dbno == -1)) {
1806 		/*
1807 		 * Not allowed to allocate, return failure.
1808 		 */
1809 		if ((args->op_flags & XFS_DA_OP_JUSTCHECK) || args->total == 0)
1810 			return -ENOSPC;
1811 
1812 		/*
1813 		 * Allocate and initialize the new data block.
1814 		 */
1815 		if (unlikely((error = xfs_dir2_grow_inode(args,
1816 							 XFS_DIR2_DATA_SPACE,
1817 							 &dbno)) ||
1818 		    (error = xfs_dir3_data_init(args, dbno, &dbp))))
1819 			return error;
1820 
1821 		/*
1822 		 * If (somehow) we have a freespace block, get rid of it.
1823 		 */
1824 		if (fbp)
1825 			xfs_trans_brelse(tp, fbp);
1826 		if (fblk && fblk->bp)
1827 			fblk->bp = NULL;
1828 
1829 		/*
1830 		 * Get the freespace block corresponding to the data block
1831 		 * that was just allocated.
1832 		 */
1833 		fbno = dp->d_ops->db_to_fdb(args->geo, dbno);
1834 		error = xfs_dir2_free_try_read(tp, dp,
1835 				       xfs_dir2_db_to_da(args->geo, fbno),
1836 				       &fbp);
1837 		if (error)
1838 			return error;
1839 
1840 		/*
1841 		 * If there wasn't a freespace block, the read will
1842 		 * return a NULL fbp.  Allocate and initialize a new one.
1843 		 */
1844 		if (!fbp) {
1845 			error = xfs_dir2_grow_inode(args, XFS_DIR2_FREE_SPACE,
1846 						    &fbno);
1847 			if (error)
1848 				return error;
1849 
1850 			if (dp->d_ops->db_to_fdb(args->geo, dbno) != fbno) {
1851 				xfs_alert(mp,
1852 "%s: dir ino %llu needed freesp block %lld for data block %lld, got %lld ifbno %llu lastfbno %d",
1853 					__func__, (unsigned long long)dp->i_ino,
1854 					(long long)dp->d_ops->db_to_fdb(
1855 								args->geo, dbno),
1856 					(long long)dbno, (long long)fbno,
1857 					(unsigned long long)ifbno, lastfbno);
1858 				if (fblk) {
1859 					xfs_alert(mp,
1860 				" fblk 0x%p blkno %llu index %d magic 0x%x",
1861 						fblk,
1862 						(unsigned long long)fblk->blkno,
1863 						fblk->index,
1864 						fblk->magic);
1865 				} else {
1866 					xfs_alert(mp, " ... fblk is NULL");
1867 				}
1868 				XFS_ERROR_REPORT("xfs_dir2_node_addname_int",
1869 						 XFS_ERRLEVEL_LOW, mp);
1870 				return -EFSCORRUPTED;
1871 			}
1872 
1873 			/*
1874 			 * Get a buffer for the new block.
1875 			 */
1876 			error = xfs_dir3_free_get_buf(args, fbno, &fbp);
1877 			if (error)
1878 				return error;
1879 			free = fbp->b_addr;
1880 			bests = dp->d_ops->free_bests_p(free);
1881 			dp->d_ops->free_hdr_from_disk(&freehdr, free);
1882 
1883 			/*
1884 			 * Remember the first slot as our empty slot.
1885 			 */
1886 			freehdr.firstdb =
1887 				(fbno - xfs_dir2_byte_to_db(args->geo,
1888 							XFS_DIR2_FREE_OFFSET)) *
1889 					dp->d_ops->free_max_bests(args->geo);
1890 		} else {
1891 			free = fbp->b_addr;
1892 			bests = dp->d_ops->free_bests_p(free);
1893 			dp->d_ops->free_hdr_from_disk(&freehdr, free);
1894 		}
1895 
1896 		/*
1897 		 * Set the freespace block index from the data block number.
1898 		 */
1899 		findex = dp->d_ops->db_to_fdindex(args->geo, dbno);
1900 		/*
1901 		 * If it's after the end of the current entries in the
1902 		 * freespace block, extend that table.
1903 		 */
1904 		if (findex >= freehdr.nvalid) {
1905 			ASSERT(findex < dp->d_ops->free_max_bests(args->geo));
1906 			freehdr.nvalid = findex + 1;
1907 			/*
1908 			 * Tag new entry so nused will go up.
1909 			 */
1910 			bests[findex] = cpu_to_be16(NULLDATAOFF);
1911 		}
1912 		/*
1913 		 * If this entry was for an empty data block
1914 		 * (this should always be true) then update the header.
1915 		 */
1916 		if (bests[findex] == cpu_to_be16(NULLDATAOFF)) {
1917 			freehdr.nused++;
1918 			dp->d_ops->free_hdr_to_disk(fbp->b_addr, &freehdr);
1919 			xfs_dir2_free_log_header(args, fbp);
1920 		}
1921 		/*
1922 		 * Update the real value in the table.
1923 		 * We haven't allocated the data entry yet so this will
1924 		 * change again.
1925 		 */
1926 		hdr = dbp->b_addr;
1927 		bf = dp->d_ops->data_bestfree_p(hdr);
1928 		bests[findex] = bf[0].length;
1929 		logfree = 1;
1930 	}
1931 	/*
1932 	 * We had a data block so we don't have to make a new one.
1933 	 */
1934 	else {
1935 		/*
1936 		 * If just checking, we succeeded.
1937 		 */
1938 		if (args->op_flags & XFS_DA_OP_JUSTCHECK)
1939 			return 0;
1940 
1941 		/*
1942 		 * Read the data block in.
1943 		 */
1944 		error = xfs_dir3_data_read(tp, dp,
1945 					   xfs_dir2_db_to_da(args->geo, dbno),
1946 					   -1, &dbp);
1947 		if (error)
1948 			return error;
1949 		hdr = dbp->b_addr;
1950 		bf = dp->d_ops->data_bestfree_p(hdr);
1951 		logfree = 0;
1952 	}
1953 	ASSERT(be16_to_cpu(bf[0].length) >= length);
1954 	/*
1955 	 * Point to the existing unused space.
1956 	 */
1957 	dup = (xfs_dir2_data_unused_t *)
1958 	      ((char *)hdr + be16_to_cpu(bf[0].offset));
1959 	needscan = needlog = 0;
1960 	/*
1961 	 * Mark the first part of the unused space, inuse for us.
1962 	 */
1963 	xfs_dir2_data_use_free(args, dbp, dup,
1964 		(xfs_dir2_data_aoff_t)((char *)dup - (char *)hdr), length,
1965 		&needlog, &needscan);
1966 	/*
1967 	 * Fill in the new entry and log it.
1968 	 */
1969 	dep = (xfs_dir2_data_entry_t *)dup;
1970 	dep->inumber = cpu_to_be64(args->inumber);
1971 	dep->namelen = args->namelen;
1972 	memcpy(dep->name, args->name, dep->namelen);
1973 	dp->d_ops->data_put_ftype(dep, args->filetype);
1974 	tagp = dp->d_ops->data_entry_tag_p(dep);
1975 	*tagp = cpu_to_be16((char *)dep - (char *)hdr);
1976 	xfs_dir2_data_log_entry(args, dbp, dep);
1977 	/*
1978 	 * Rescan the block for bestfree if needed.
1979 	 */
1980 	if (needscan)
1981 		xfs_dir2_data_freescan(dp, hdr, &needlog);
1982 	/*
1983 	 * Log the data block header if needed.
1984 	 */
1985 	if (needlog)
1986 		xfs_dir2_data_log_header(args, dbp);
1987 	/*
1988 	 * If the freespace entry is now wrong, update it.
1989 	 */
1990 	bests = dp->d_ops->free_bests_p(free); /* gcc is so stupid */
1991 	if (be16_to_cpu(bests[findex]) != be16_to_cpu(bf[0].length)) {
1992 		bests[findex] = bf[0].length;
1993 		logfree = 1;
1994 	}
1995 	/*
1996 	 * Log the freespace entry if needed.
1997 	 */
1998 	if (logfree)
1999 		xfs_dir2_free_log_bests(args, fbp, findex, findex);
2000 	/*
2001 	 * Return the data block and offset in args, then drop the data block.
2002 	 */
2003 	args->blkno = (xfs_dablk_t)dbno;
2004 	args->index = be16_to_cpu(*tagp);
2005 	return 0;
2006 }
2007 
2008 /*
2009  * Lookup an entry in a node-format directory.
2010  * All the real work happens in xfs_da3_node_lookup_int.
2011  * The only real output is the inode number of the entry.
2012  */
2013 int						/* error */
2014 xfs_dir2_node_lookup(
2015 	xfs_da_args_t	*args)			/* operation arguments */
2016 {
2017 	int		error;			/* error return value */
2018 	int		i;			/* btree level */
2019 	int		rval;			/* operation return value */
2020 	xfs_da_state_t	*state;			/* btree cursor */
2021 
2022 	trace_xfs_dir2_node_lookup(args);
2023 
2024 	/*
2025 	 * Allocate and initialize the btree cursor.
2026 	 */
2027 	state = xfs_da_state_alloc();
2028 	state->args = args;
2029 	state->mp = args->dp->i_mount;
2030 	/*
2031 	 * Fill in the path to the entry in the cursor.
2032 	 */
2033 	error = xfs_da3_node_lookup_int(state, &rval);
2034 	if (error)
2035 		rval = error;
2036 	else if (rval == -ENOENT && args->cmpresult == XFS_CMP_CASE) {
2037 		/* If a CI match, dup the actual name and return -EEXIST */
2038 		xfs_dir2_data_entry_t	*dep;
2039 
2040 		dep = (xfs_dir2_data_entry_t *)
2041 			((char *)state->extrablk.bp->b_addr +
2042 						 state->extrablk.index);
2043 		rval = xfs_dir_cilookup_result(args, dep->name, dep->namelen);
2044 	}
2045 	/*
2046 	 * Release the btree blocks and leaf block.
2047 	 */
2048 	for (i = 0; i < state->path.active; i++) {
2049 		xfs_trans_brelse(args->trans, state->path.blk[i].bp);
2050 		state->path.blk[i].bp = NULL;
2051 	}
2052 	/*
2053 	 * Release the data block if we have it.
2054 	 */
2055 	if (state->extravalid && state->extrablk.bp) {
2056 		xfs_trans_brelse(args->trans, state->extrablk.bp);
2057 		state->extrablk.bp = NULL;
2058 	}
2059 	xfs_da_state_free(state);
2060 	return rval;
2061 }
2062 
2063 /*
2064  * Remove an entry from a node-format directory.
2065  */
2066 int						/* error */
2067 xfs_dir2_node_removename(
2068 	struct xfs_da_args	*args)		/* operation arguments */
2069 {
2070 	struct xfs_da_state_blk	*blk;		/* leaf block */
2071 	int			error;		/* error return value */
2072 	int			rval;		/* operation return value */
2073 	struct xfs_da_state	*state;		/* btree cursor */
2074 
2075 	trace_xfs_dir2_node_removename(args);
2076 
2077 	/*
2078 	 * Allocate and initialize the btree cursor.
2079 	 */
2080 	state = xfs_da_state_alloc();
2081 	state->args = args;
2082 	state->mp = args->dp->i_mount;
2083 
2084 	/* Look up the entry we're deleting, set up the cursor. */
2085 	error = xfs_da3_node_lookup_int(state, &rval);
2086 	if (error)
2087 		goto out_free;
2088 
2089 	/* Didn't find it, upper layer screwed up. */
2090 	if (rval != -EEXIST) {
2091 		error = rval;
2092 		goto out_free;
2093 	}
2094 
2095 	blk = &state->path.blk[state->path.active - 1];
2096 	ASSERT(blk->magic == XFS_DIR2_LEAFN_MAGIC);
2097 	ASSERT(state->extravalid);
2098 	/*
2099 	 * Remove the leaf and data entries.
2100 	 * Extrablk refers to the data block.
2101 	 */
2102 	error = xfs_dir2_leafn_remove(args, blk->bp, blk->index,
2103 		&state->extrablk, &rval);
2104 	if (error)
2105 		goto out_free;
2106 	/*
2107 	 * Fix the hash values up the btree.
2108 	 */
2109 	xfs_da3_fixhashpath(state, &state->path);
2110 	/*
2111 	 * If we need to join leaf blocks, do it.
2112 	 */
2113 	if (rval && state->path.active > 1)
2114 		error = xfs_da3_join(state);
2115 	/*
2116 	 * If no errors so far, try conversion to leaf format.
2117 	 */
2118 	if (!error)
2119 		error = xfs_dir2_node_to_leaf(state);
2120 out_free:
2121 	xfs_da_state_free(state);
2122 	return error;
2123 }
2124 
2125 /*
2126  * Replace an entry's inode number in a node-format directory.
2127  */
2128 int						/* error */
2129 xfs_dir2_node_replace(
2130 	xfs_da_args_t		*args)		/* operation arguments */
2131 {
2132 	xfs_da_state_blk_t	*blk;		/* leaf block */
2133 	xfs_dir2_data_hdr_t	*hdr;		/* data block header */
2134 	xfs_dir2_data_entry_t	*dep;		/* data entry changed */
2135 	int			error;		/* error return value */
2136 	int			i;		/* btree level */
2137 	xfs_ino_t		inum;		/* new inode number */
2138 	int			ftype;		/* new file type */
2139 	xfs_dir2_leaf_t		*leaf;		/* leaf structure */
2140 	xfs_dir2_leaf_entry_t	*lep;		/* leaf entry being changed */
2141 	int			rval;		/* internal return value */
2142 	xfs_da_state_t		*state;		/* btree cursor */
2143 
2144 	trace_xfs_dir2_node_replace(args);
2145 
2146 	/*
2147 	 * Allocate and initialize the btree cursor.
2148 	 */
2149 	state = xfs_da_state_alloc();
2150 	state->args = args;
2151 	state->mp = args->dp->i_mount;
2152 
2153 	/*
2154 	 * We have to save new inode number and ftype since
2155 	 * xfs_da3_node_lookup_int() is going to overwrite them
2156 	 */
2157 	inum = args->inumber;
2158 	ftype = args->filetype;
2159 
2160 	/*
2161 	 * Lookup the entry to change in the btree.
2162 	 */
2163 	error = xfs_da3_node_lookup_int(state, &rval);
2164 	if (error) {
2165 		rval = error;
2166 	}
2167 	/*
2168 	 * It should be found, since the vnodeops layer has looked it up
2169 	 * and locked it.  But paranoia is good.
2170 	 */
2171 	if (rval == -EEXIST) {
2172 		struct xfs_dir2_leaf_entry *ents;
2173 		/*
2174 		 * Find the leaf entry.
2175 		 */
2176 		blk = &state->path.blk[state->path.active - 1];
2177 		ASSERT(blk->magic == XFS_DIR2_LEAFN_MAGIC);
2178 		leaf = blk->bp->b_addr;
2179 		ents = args->dp->d_ops->leaf_ents_p(leaf);
2180 		lep = &ents[blk->index];
2181 		ASSERT(state->extravalid);
2182 		/*
2183 		 * Point to the data entry.
2184 		 */
2185 		hdr = state->extrablk.bp->b_addr;
2186 		ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
2187 		       hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC));
2188 		dep = (xfs_dir2_data_entry_t *)
2189 		      ((char *)hdr +
2190 		       xfs_dir2_dataptr_to_off(args->geo,
2191 					       be32_to_cpu(lep->address)));
2192 		ASSERT(inum != be64_to_cpu(dep->inumber));
2193 		/*
2194 		 * Fill in the new inode number and log the entry.
2195 		 */
2196 		dep->inumber = cpu_to_be64(inum);
2197 		args->dp->d_ops->data_put_ftype(dep, ftype);
2198 		xfs_dir2_data_log_entry(args, state->extrablk.bp, dep);
2199 		rval = 0;
2200 	}
2201 	/*
2202 	 * Didn't find it, and we're holding a data block.  Drop it.
2203 	 */
2204 	else if (state->extravalid) {
2205 		xfs_trans_brelse(args->trans, state->extrablk.bp);
2206 		state->extrablk.bp = NULL;
2207 	}
2208 	/*
2209 	 * Release all the buffers in the cursor.
2210 	 */
2211 	for (i = 0; i < state->path.active; i++) {
2212 		xfs_trans_brelse(args->trans, state->path.blk[i].bp);
2213 		state->path.blk[i].bp = NULL;
2214 	}
2215 	xfs_da_state_free(state);
2216 	return rval;
2217 }
2218 
2219 /*
2220  * Trim off a trailing empty freespace block.
2221  * Return (in rvalp) 1 if we did it, 0 if not.
2222  */
2223 int						/* error */
2224 xfs_dir2_node_trim_free(
2225 	xfs_da_args_t		*args,		/* operation arguments */
2226 	xfs_fileoff_t		fo,		/* free block number */
2227 	int			*rvalp)		/* out: did something */
2228 {
2229 	struct xfs_buf		*bp;		/* freespace buffer */
2230 	xfs_inode_t		*dp;		/* incore directory inode */
2231 	int			error;		/* error return code */
2232 	xfs_dir2_free_t		*free;		/* freespace structure */
2233 	xfs_trans_t		*tp;		/* transaction pointer */
2234 	struct xfs_dir3_icfree_hdr freehdr;
2235 
2236 	dp = args->dp;
2237 	tp = args->trans;
2238 
2239 	*rvalp = 0;
2240 
2241 	/*
2242 	 * Read the freespace block.
2243 	 */
2244 	error = xfs_dir2_free_try_read(tp, dp, fo, &bp);
2245 	if (error)
2246 		return error;
2247 	/*
2248 	 * There can be holes in freespace.  If fo is a hole, there's
2249 	 * nothing to do.
2250 	 */
2251 	if (!bp)
2252 		return 0;
2253 	free = bp->b_addr;
2254 	dp->d_ops->free_hdr_from_disk(&freehdr, free);
2255 
2256 	/*
2257 	 * If there are used entries, there's nothing to do.
2258 	 */
2259 	if (freehdr.nused > 0) {
2260 		xfs_trans_brelse(tp, bp);
2261 		return 0;
2262 	}
2263 	/*
2264 	 * Blow the block away.
2265 	 */
2266 	error = xfs_dir2_shrink_inode(args,
2267 			xfs_dir2_da_to_db(args->geo, (xfs_dablk_t)fo), bp);
2268 	if (error) {
2269 		/*
2270 		 * Can't fail with ENOSPC since that only happens with no
2271 		 * space reservation, when breaking up an extent into two
2272 		 * pieces.  This is the last block of an extent.
2273 		 */
2274 		ASSERT(error != -ENOSPC);
2275 		xfs_trans_brelse(tp, bp);
2276 		return error;
2277 	}
2278 	/*
2279 	 * Return that we succeeded.
2280 	 */
2281 	*rvalp = 1;
2282 	return 0;
2283 }
2284