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