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