xref: /openbmc/linux/fs/xfs/libxfs/xfs_dir2_data.c (revision 53809828)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (c) 2000-2002,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_dir2.h"
17 #include "xfs_dir2_priv.h"
18 #include "xfs_error.h"
19 #include "xfs_trans.h"
20 #include "xfs_buf_item.h"
21 #include "xfs_cksum.h"
22 #include "xfs_log.h"
23 
24 static xfs_failaddr_t xfs_dir2_data_freefind_verify(
25 		struct xfs_dir2_data_hdr *hdr, struct xfs_dir2_data_free *bf,
26 		struct xfs_dir2_data_unused *dup,
27 		struct xfs_dir2_data_free **bf_ent);
28 
29 /*
30  * Check the consistency of the data block.
31  * The input can also be a block-format directory.
32  * Return NULL if the buffer is good, otherwise the address of the error.
33  */
34 xfs_failaddr_t
35 __xfs_dir3_data_check(
36 	struct xfs_inode	*dp,		/* incore inode pointer */
37 	struct xfs_buf		*bp)		/* data block's buffer */
38 {
39 	xfs_dir2_dataptr_t	addr;		/* addr for leaf lookup */
40 	xfs_dir2_data_free_t	*bf;		/* bestfree table */
41 	xfs_dir2_block_tail_t	*btp=NULL;	/* block tail */
42 	int			count;		/* count of entries found */
43 	xfs_dir2_data_hdr_t	*hdr;		/* data block header */
44 	xfs_dir2_data_entry_t	*dep;		/* data entry */
45 	xfs_dir2_data_free_t	*dfp;		/* bestfree entry */
46 	xfs_dir2_data_unused_t	*dup;		/* unused entry */
47 	char			*endp;		/* end of useful data */
48 	int			freeseen;	/* mask of bestfrees seen */
49 	xfs_dahash_t		hash;		/* hash of current name */
50 	int			i;		/* leaf index */
51 	int			lastfree;	/* last entry was unused */
52 	xfs_dir2_leaf_entry_t	*lep=NULL;	/* block leaf entries */
53 	xfs_mount_t		*mp;		/* filesystem mount point */
54 	char			*p;		/* current data position */
55 	int			stale;		/* count of stale leaves */
56 	struct xfs_name		name;
57 	const struct xfs_dir_ops *ops;
58 	struct xfs_da_geometry	*geo;
59 
60 	mp = bp->b_target->bt_mount;
61 	geo = mp->m_dir_geo;
62 
63 	/*
64 	 * We can be passed a null dp here from a verifier, so we need to go the
65 	 * hard way to get them.
66 	 */
67 	ops = xfs_dir_get_ops(mp, dp);
68 
69 	/*
70 	 * If this isn't a directory, or we don't get handed the dir ops,
71 	 * something is seriously wrong.  Bail out.
72 	 */
73 	if ((dp && !S_ISDIR(VFS_I(dp)->i_mode)) ||
74 	    ops != xfs_dir_get_ops(mp, NULL))
75 		return __this_address;
76 
77 	hdr = bp->b_addr;
78 	p = (char *)ops->data_entry_p(hdr);
79 
80 	switch (hdr->magic) {
81 	case cpu_to_be32(XFS_DIR3_BLOCK_MAGIC):
82 	case cpu_to_be32(XFS_DIR2_BLOCK_MAGIC):
83 		btp = xfs_dir2_block_tail_p(geo, hdr);
84 		lep = xfs_dir2_block_leaf_p(btp);
85 
86 		/*
87 		 * The number of leaf entries is limited by the size of the
88 		 * block and the amount of space used by the data entries.
89 		 * We don't know how much space is used by the data entries yet,
90 		 * so just ensure that the count falls somewhere inside the
91 		 * block right now.
92 		 */
93 		if (be32_to_cpu(btp->count) >=
94 		    ((char *)btp - p) / sizeof(struct xfs_dir2_leaf_entry))
95 			return __this_address;
96 		break;
97 	case cpu_to_be32(XFS_DIR3_DATA_MAGIC):
98 	case cpu_to_be32(XFS_DIR2_DATA_MAGIC):
99 		break;
100 	default:
101 		return __this_address;
102 	}
103 	endp = xfs_dir3_data_endp(geo, hdr);
104 	if (!endp)
105 		return __this_address;
106 
107 	/*
108 	 * Account for zero bestfree entries.
109 	 */
110 	bf = ops->data_bestfree_p(hdr);
111 	count = lastfree = freeseen = 0;
112 	if (!bf[0].length) {
113 		if (bf[0].offset)
114 			return __this_address;
115 		freeseen |= 1 << 0;
116 	}
117 	if (!bf[1].length) {
118 		if (bf[1].offset)
119 			return __this_address;
120 		freeseen |= 1 << 1;
121 	}
122 	if (!bf[2].length) {
123 		if (bf[2].offset)
124 			return __this_address;
125 		freeseen |= 1 << 2;
126 	}
127 
128 	if (be16_to_cpu(bf[0].length) < be16_to_cpu(bf[1].length))
129 		return __this_address;
130 	if (be16_to_cpu(bf[1].length) < be16_to_cpu(bf[2].length))
131 		return __this_address;
132 	/*
133 	 * Loop over the data/unused entries.
134 	 */
135 	while (p < endp) {
136 		dup = (xfs_dir2_data_unused_t *)p;
137 		/*
138 		 * If it's unused, look for the space in the bestfree table.
139 		 * If we find it, account for that, else make sure it
140 		 * doesn't need to be there.
141 		 */
142 		if (be16_to_cpu(dup->freetag) == XFS_DIR2_DATA_FREE_TAG) {
143 			xfs_failaddr_t	fa;
144 
145 			if (lastfree != 0)
146 				return __this_address;
147 			if (endp < p + be16_to_cpu(dup->length))
148 				return __this_address;
149 			if (be16_to_cpu(*xfs_dir2_data_unused_tag_p(dup)) !=
150 			    (char *)dup - (char *)hdr)
151 				return __this_address;
152 			fa = xfs_dir2_data_freefind_verify(hdr, bf, dup, &dfp);
153 			if (fa)
154 				return fa;
155 			if (dfp) {
156 				i = (int)(dfp - bf);
157 				if ((freeseen & (1 << i)) != 0)
158 					return __this_address;
159 				freeseen |= 1 << i;
160 			} else {
161 				if (be16_to_cpu(dup->length) >
162 				    be16_to_cpu(bf[2].length))
163 					return __this_address;
164 			}
165 			p += be16_to_cpu(dup->length);
166 			lastfree = 1;
167 			continue;
168 		}
169 		/*
170 		 * It's a real entry.  Validate the fields.
171 		 * If this is a block directory then make sure it's
172 		 * in the leaf section of the block.
173 		 * The linear search is crude but this is DEBUG code.
174 		 */
175 		dep = (xfs_dir2_data_entry_t *)p;
176 		if (dep->namelen == 0)
177 			return __this_address;
178 		if (xfs_dir_ino_validate(mp, be64_to_cpu(dep->inumber)))
179 			return __this_address;
180 		if (endp < p + ops->data_entsize(dep->namelen))
181 			return __this_address;
182 		if (be16_to_cpu(*ops->data_entry_tag_p(dep)) !=
183 		    (char *)dep - (char *)hdr)
184 			return __this_address;
185 		if (ops->data_get_ftype(dep) >= XFS_DIR3_FT_MAX)
186 			return __this_address;
187 		count++;
188 		lastfree = 0;
189 		if (hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) ||
190 		    hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC)) {
191 			addr = xfs_dir2_db_off_to_dataptr(geo, geo->datablk,
192 						(xfs_dir2_data_aoff_t)
193 						((char *)dep - (char *)hdr));
194 			name.name = dep->name;
195 			name.len = dep->namelen;
196 			hash = mp->m_dirnameops->hashname(&name);
197 			for (i = 0; i < be32_to_cpu(btp->count); i++) {
198 				if (be32_to_cpu(lep[i].address) == addr &&
199 				    be32_to_cpu(lep[i].hashval) == hash)
200 					break;
201 			}
202 			if (i >= be32_to_cpu(btp->count))
203 				return __this_address;
204 		}
205 		p += ops->data_entsize(dep->namelen);
206 	}
207 	/*
208 	 * Need to have seen all the entries and all the bestfree slots.
209 	 */
210 	if (freeseen != 7)
211 		return __this_address;
212 	if (hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) ||
213 	    hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC)) {
214 		for (i = stale = 0; i < be32_to_cpu(btp->count); i++) {
215 			if (lep[i].address ==
216 			    cpu_to_be32(XFS_DIR2_NULL_DATAPTR))
217 				stale++;
218 			if (i > 0 && be32_to_cpu(lep[i].hashval) <
219 				     be32_to_cpu(lep[i - 1].hashval))
220 				return __this_address;
221 		}
222 		if (count != be32_to_cpu(btp->count) - be32_to_cpu(btp->stale))
223 			return __this_address;
224 		if (stale != be32_to_cpu(btp->stale))
225 			return __this_address;
226 	}
227 	return NULL;
228 }
229 
230 #ifdef DEBUG
231 void
232 xfs_dir3_data_check(
233 	struct xfs_inode	*dp,
234 	struct xfs_buf		*bp)
235 {
236 	xfs_failaddr_t		fa;
237 
238 	fa = __xfs_dir3_data_check(dp, bp);
239 	if (!fa)
240 		return;
241 	xfs_corruption_error(__func__, XFS_ERRLEVEL_LOW, dp->i_mount,
242 			bp->b_addr, BBTOB(bp->b_length), __FILE__, __LINE__,
243 			fa);
244 	ASSERT(0);
245 }
246 #endif
247 
248 static xfs_failaddr_t
249 xfs_dir3_data_verify(
250 	struct xfs_buf		*bp)
251 {
252 	struct xfs_mount	*mp = bp->b_target->bt_mount;
253 	struct xfs_dir3_blk_hdr	*hdr3 = bp->b_addr;
254 
255 	if (xfs_sb_version_hascrc(&mp->m_sb)) {
256 		if (hdr3->magic != cpu_to_be32(XFS_DIR3_DATA_MAGIC))
257 			return __this_address;
258 		if (!uuid_equal(&hdr3->uuid, &mp->m_sb.sb_meta_uuid))
259 			return __this_address;
260 		if (be64_to_cpu(hdr3->blkno) != bp->b_bn)
261 			return __this_address;
262 		if (!xfs_log_check_lsn(mp, be64_to_cpu(hdr3->lsn)))
263 			return __this_address;
264 	} else {
265 		if (hdr3->magic != cpu_to_be32(XFS_DIR2_DATA_MAGIC))
266 			return __this_address;
267 	}
268 	return __xfs_dir3_data_check(NULL, bp);
269 }
270 
271 /*
272  * Readahead of the first block of the directory when it is opened is completely
273  * oblivious to the format of the directory. Hence we can either get a block
274  * format buffer or a data format buffer on readahead.
275  */
276 static void
277 xfs_dir3_data_reada_verify(
278 	struct xfs_buf		*bp)
279 {
280 	struct xfs_dir2_data_hdr *hdr = bp->b_addr;
281 
282 	switch (hdr->magic) {
283 	case cpu_to_be32(XFS_DIR2_BLOCK_MAGIC):
284 	case cpu_to_be32(XFS_DIR3_BLOCK_MAGIC):
285 		bp->b_ops = &xfs_dir3_block_buf_ops;
286 		bp->b_ops->verify_read(bp);
287 		return;
288 	case cpu_to_be32(XFS_DIR2_DATA_MAGIC):
289 	case cpu_to_be32(XFS_DIR3_DATA_MAGIC):
290 		bp->b_ops = &xfs_dir3_data_buf_ops;
291 		bp->b_ops->verify_read(bp);
292 		return;
293 	default:
294 		xfs_verifier_error(bp, -EFSCORRUPTED, __this_address);
295 		break;
296 	}
297 }
298 
299 static void
300 xfs_dir3_data_read_verify(
301 	struct xfs_buf	*bp)
302 {
303 	struct xfs_mount	*mp = bp->b_target->bt_mount;
304 	xfs_failaddr_t		fa;
305 
306 	if (xfs_sb_version_hascrc(&mp->m_sb) &&
307 	    !xfs_buf_verify_cksum(bp, XFS_DIR3_DATA_CRC_OFF))
308 		xfs_verifier_error(bp, -EFSBADCRC, __this_address);
309 	else {
310 		fa = xfs_dir3_data_verify(bp);
311 		if (fa)
312 			xfs_verifier_error(bp, -EFSCORRUPTED, fa);
313 	}
314 }
315 
316 static void
317 xfs_dir3_data_write_verify(
318 	struct xfs_buf	*bp)
319 {
320 	struct xfs_mount	*mp = bp->b_target->bt_mount;
321 	struct xfs_buf_log_item	*bip = bp->b_log_item;
322 	struct xfs_dir3_blk_hdr	*hdr3 = bp->b_addr;
323 	xfs_failaddr_t		fa;
324 
325 	fa = xfs_dir3_data_verify(bp);
326 	if (fa) {
327 		xfs_verifier_error(bp, -EFSCORRUPTED, fa);
328 		return;
329 	}
330 
331 	if (!xfs_sb_version_hascrc(&mp->m_sb))
332 		return;
333 
334 	if (bip)
335 		hdr3->lsn = cpu_to_be64(bip->bli_item.li_lsn);
336 
337 	xfs_buf_update_cksum(bp, XFS_DIR3_DATA_CRC_OFF);
338 }
339 
340 const struct xfs_buf_ops xfs_dir3_data_buf_ops = {
341 	.name = "xfs_dir3_data",
342 	.verify_read = xfs_dir3_data_read_verify,
343 	.verify_write = xfs_dir3_data_write_verify,
344 	.verify_struct = xfs_dir3_data_verify,
345 };
346 
347 static const struct xfs_buf_ops xfs_dir3_data_reada_buf_ops = {
348 	.name = "xfs_dir3_data_reada",
349 	.verify_read = xfs_dir3_data_reada_verify,
350 	.verify_write = xfs_dir3_data_write_verify,
351 };
352 
353 
354 int
355 xfs_dir3_data_read(
356 	struct xfs_trans	*tp,
357 	struct xfs_inode	*dp,
358 	xfs_dablk_t		bno,
359 	xfs_daddr_t		mapped_bno,
360 	struct xfs_buf		**bpp)
361 {
362 	int			err;
363 
364 	err = xfs_da_read_buf(tp, dp, bno, mapped_bno, bpp,
365 				XFS_DATA_FORK, &xfs_dir3_data_buf_ops);
366 	if (!err && tp && *bpp)
367 		xfs_trans_buf_set_type(tp, *bpp, XFS_BLFT_DIR_DATA_BUF);
368 	return err;
369 }
370 
371 int
372 xfs_dir3_data_readahead(
373 	struct xfs_inode	*dp,
374 	xfs_dablk_t		bno,
375 	xfs_daddr_t		mapped_bno)
376 {
377 	return xfs_da_reada_buf(dp, bno, mapped_bno,
378 				XFS_DATA_FORK, &xfs_dir3_data_reada_buf_ops);
379 }
380 
381 /*
382  * Find the bestfree entry that exactly coincides with unused directory space
383  * or a verifier error because the bestfree data are bad.
384  */
385 static xfs_failaddr_t
386 xfs_dir2_data_freefind_verify(
387 	struct xfs_dir2_data_hdr	*hdr,
388 	struct xfs_dir2_data_free	*bf,
389 	struct xfs_dir2_data_unused	*dup,
390 	struct xfs_dir2_data_free	**bf_ent)
391 {
392 	struct xfs_dir2_data_free	*dfp;
393 	xfs_dir2_data_aoff_t		off;
394 	bool				matched = false;
395 	bool				seenzero = false;
396 
397 	*bf_ent = NULL;
398 	off = (xfs_dir2_data_aoff_t)((char *)dup - (char *)hdr);
399 
400 	/*
401 	 * Validate some consistency in the bestfree table.
402 	 * Check order, non-overlapping entries, and if we find the
403 	 * one we're looking for it has to be exact.
404 	 */
405 	for (dfp = &bf[0]; dfp < &bf[XFS_DIR2_DATA_FD_COUNT]; dfp++) {
406 		if (!dfp->offset) {
407 			if (dfp->length)
408 				return __this_address;
409 			seenzero = true;
410 			continue;
411 		}
412 		if (seenzero)
413 			return __this_address;
414 		if (be16_to_cpu(dfp->offset) == off) {
415 			matched = true;
416 			if (dfp->length != dup->length)
417 				return __this_address;
418 		} else if (be16_to_cpu(dfp->offset) > off) {
419 			if (off + be16_to_cpu(dup->length) >
420 					be16_to_cpu(dfp->offset))
421 				return __this_address;
422 		} else {
423 			if (be16_to_cpu(dfp->offset) +
424 					be16_to_cpu(dfp->length) > off)
425 				return __this_address;
426 		}
427 		if (!matched &&
428 		    be16_to_cpu(dfp->length) < be16_to_cpu(dup->length))
429 			return __this_address;
430 		if (dfp > &bf[0] &&
431 		    be16_to_cpu(dfp[-1].length) < be16_to_cpu(dfp[0].length))
432 			return __this_address;
433 	}
434 
435 	/* Looks ok so far; now try to match up with a bestfree entry. */
436 	*bf_ent = xfs_dir2_data_freefind(hdr, bf, dup);
437 	return NULL;
438 }
439 
440 /*
441  * Given a data block and an unused entry from that block,
442  * return the bestfree entry if any that corresponds to it.
443  */
444 xfs_dir2_data_free_t *
445 xfs_dir2_data_freefind(
446 	struct xfs_dir2_data_hdr *hdr,		/* data block header */
447 	struct xfs_dir2_data_free *bf,		/* bestfree table pointer */
448 	struct xfs_dir2_data_unused *dup)	/* unused space */
449 {
450 	xfs_dir2_data_free_t	*dfp;		/* bestfree entry */
451 	xfs_dir2_data_aoff_t	off;		/* offset value needed */
452 
453 	off = (xfs_dir2_data_aoff_t)((char *)dup - (char *)hdr);
454 
455 	/*
456 	 * If this is smaller than the smallest bestfree entry,
457 	 * it can't be there since they're sorted.
458 	 */
459 	if (be16_to_cpu(dup->length) <
460 	    be16_to_cpu(bf[XFS_DIR2_DATA_FD_COUNT - 1].length))
461 		return NULL;
462 	/*
463 	 * Look at the three bestfree entries for our guy.
464 	 */
465 	for (dfp = &bf[0]; dfp < &bf[XFS_DIR2_DATA_FD_COUNT]; dfp++) {
466 		if (!dfp->offset)
467 			return NULL;
468 		if (be16_to_cpu(dfp->offset) == off)
469 			return dfp;
470 	}
471 	/*
472 	 * Didn't find it.  This only happens if there are duplicate lengths.
473 	 */
474 	return NULL;
475 }
476 
477 /*
478  * Insert an unused-space entry into the bestfree table.
479  */
480 xfs_dir2_data_free_t *				/* entry inserted */
481 xfs_dir2_data_freeinsert(
482 	struct xfs_dir2_data_hdr *hdr,		/* data block pointer */
483 	struct xfs_dir2_data_free *dfp,		/* bestfree table pointer */
484 	struct xfs_dir2_data_unused *dup,	/* unused space */
485 	int			*loghead)	/* log the data header (out) */
486 {
487 	xfs_dir2_data_free_t	new;		/* new bestfree entry */
488 
489 	ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
490 	       hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) ||
491 	       hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC) ||
492 	       hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC));
493 
494 	new.length = dup->length;
495 	new.offset = cpu_to_be16((char *)dup - (char *)hdr);
496 
497 	/*
498 	 * Insert at position 0, 1, or 2; or not at all.
499 	 */
500 	if (be16_to_cpu(new.length) > be16_to_cpu(dfp[0].length)) {
501 		dfp[2] = dfp[1];
502 		dfp[1] = dfp[0];
503 		dfp[0] = new;
504 		*loghead = 1;
505 		return &dfp[0];
506 	}
507 	if (be16_to_cpu(new.length) > be16_to_cpu(dfp[1].length)) {
508 		dfp[2] = dfp[1];
509 		dfp[1] = new;
510 		*loghead = 1;
511 		return &dfp[1];
512 	}
513 	if (be16_to_cpu(new.length) > be16_to_cpu(dfp[2].length)) {
514 		dfp[2] = new;
515 		*loghead = 1;
516 		return &dfp[2];
517 	}
518 	return NULL;
519 }
520 
521 /*
522  * Remove a bestfree entry from the table.
523  */
524 STATIC void
525 xfs_dir2_data_freeremove(
526 	struct xfs_dir2_data_hdr *hdr,		/* data block header */
527 	struct xfs_dir2_data_free *bf,		/* bestfree table pointer */
528 	struct xfs_dir2_data_free *dfp,		/* bestfree entry pointer */
529 	int			*loghead)	/* out: log data header */
530 {
531 
532 	ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
533 	       hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) ||
534 	       hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC) ||
535 	       hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC));
536 
537 	/*
538 	 * It's the first entry, slide the next 2 up.
539 	 */
540 	if (dfp == &bf[0]) {
541 		bf[0] = bf[1];
542 		bf[1] = bf[2];
543 	}
544 	/*
545 	 * It's the second entry, slide the 3rd entry up.
546 	 */
547 	else if (dfp == &bf[1])
548 		bf[1] = bf[2];
549 	/*
550 	 * Must be the last entry.
551 	 */
552 	else
553 		ASSERT(dfp == &bf[2]);
554 	/*
555 	 * Clear the 3rd entry, must be zero now.
556 	 */
557 	bf[2].length = 0;
558 	bf[2].offset = 0;
559 	*loghead = 1;
560 }
561 
562 /*
563  * Given a data block, reconstruct its bestfree map.
564  */
565 void
566 xfs_dir2_data_freescan_int(
567 	struct xfs_da_geometry	*geo,
568 	const struct xfs_dir_ops *ops,
569 	struct xfs_dir2_data_hdr *hdr,
570 	int			*loghead)
571 {
572 	xfs_dir2_data_entry_t	*dep;		/* active data entry */
573 	xfs_dir2_data_unused_t	*dup;		/* unused data entry */
574 	struct xfs_dir2_data_free *bf;
575 	char			*endp;		/* end of block's data */
576 	char			*p;		/* current entry pointer */
577 
578 	ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
579 	       hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC) ||
580 	       hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) ||
581 	       hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC));
582 
583 	/*
584 	 * Start by clearing the table.
585 	 */
586 	bf = ops->data_bestfree_p(hdr);
587 	memset(bf, 0, sizeof(*bf) * XFS_DIR2_DATA_FD_COUNT);
588 	*loghead = 1;
589 	/*
590 	 * Set up pointers.
591 	 */
592 	p = (char *)ops->data_entry_p(hdr);
593 	endp = xfs_dir3_data_endp(geo, hdr);
594 	/*
595 	 * Loop over the block's entries.
596 	 */
597 	while (p < endp) {
598 		dup = (xfs_dir2_data_unused_t *)p;
599 		/*
600 		 * If it's a free entry, insert it.
601 		 */
602 		if (be16_to_cpu(dup->freetag) == XFS_DIR2_DATA_FREE_TAG) {
603 			ASSERT((char *)dup - (char *)hdr ==
604 			       be16_to_cpu(*xfs_dir2_data_unused_tag_p(dup)));
605 			xfs_dir2_data_freeinsert(hdr, bf, dup, loghead);
606 			p += be16_to_cpu(dup->length);
607 		}
608 		/*
609 		 * For active entries, check their tags and skip them.
610 		 */
611 		else {
612 			dep = (xfs_dir2_data_entry_t *)p;
613 			ASSERT((char *)dep - (char *)hdr ==
614 			       be16_to_cpu(*ops->data_entry_tag_p(dep)));
615 			p += ops->data_entsize(dep->namelen);
616 		}
617 	}
618 }
619 
620 void
621 xfs_dir2_data_freescan(
622 	struct xfs_inode	*dp,
623 	struct xfs_dir2_data_hdr *hdr,
624 	int			*loghead)
625 {
626 	return xfs_dir2_data_freescan_int(dp->i_mount->m_dir_geo, dp->d_ops,
627 			hdr, loghead);
628 }
629 
630 /*
631  * Initialize a data block at the given block number in the directory.
632  * Give back the buffer for the created block.
633  */
634 int						/* error */
635 xfs_dir3_data_init(
636 	xfs_da_args_t		*args,		/* directory operation args */
637 	xfs_dir2_db_t		blkno,		/* logical dir block number */
638 	struct xfs_buf		**bpp)		/* output block buffer */
639 {
640 	struct xfs_buf		*bp;		/* block buffer */
641 	xfs_dir2_data_hdr_t	*hdr;		/* data block header */
642 	xfs_inode_t		*dp;		/* incore directory inode */
643 	xfs_dir2_data_unused_t	*dup;		/* unused entry pointer */
644 	struct xfs_dir2_data_free *bf;
645 	int			error;		/* error return value */
646 	int			i;		/* bestfree index */
647 	xfs_mount_t		*mp;		/* filesystem mount point */
648 	xfs_trans_t		*tp;		/* transaction pointer */
649 	int                     t;              /* temp */
650 
651 	dp = args->dp;
652 	mp = dp->i_mount;
653 	tp = args->trans;
654 	/*
655 	 * Get the buffer set up for the block.
656 	 */
657 	error = xfs_da_get_buf(tp, dp, xfs_dir2_db_to_da(args->geo, blkno),
658 			       -1, &bp, XFS_DATA_FORK);
659 	if (error)
660 		return error;
661 	bp->b_ops = &xfs_dir3_data_buf_ops;
662 	xfs_trans_buf_set_type(tp, bp, XFS_BLFT_DIR_DATA_BUF);
663 
664 	/*
665 	 * Initialize the header.
666 	 */
667 	hdr = bp->b_addr;
668 	if (xfs_sb_version_hascrc(&mp->m_sb)) {
669 		struct xfs_dir3_blk_hdr *hdr3 = bp->b_addr;
670 
671 		memset(hdr3, 0, sizeof(*hdr3));
672 		hdr3->magic = cpu_to_be32(XFS_DIR3_DATA_MAGIC);
673 		hdr3->blkno = cpu_to_be64(bp->b_bn);
674 		hdr3->owner = cpu_to_be64(dp->i_ino);
675 		uuid_copy(&hdr3->uuid, &mp->m_sb.sb_meta_uuid);
676 
677 	} else
678 		hdr->magic = cpu_to_be32(XFS_DIR2_DATA_MAGIC);
679 
680 	bf = dp->d_ops->data_bestfree_p(hdr);
681 	bf[0].offset = cpu_to_be16(dp->d_ops->data_entry_offset);
682 	for (i = 1; i < XFS_DIR2_DATA_FD_COUNT; i++) {
683 		bf[i].length = 0;
684 		bf[i].offset = 0;
685 	}
686 
687 	/*
688 	 * Set up an unused entry for the block's body.
689 	 */
690 	dup = dp->d_ops->data_unused_p(hdr);
691 	dup->freetag = cpu_to_be16(XFS_DIR2_DATA_FREE_TAG);
692 
693 	t = args->geo->blksize - (uint)dp->d_ops->data_entry_offset;
694 	bf[0].length = cpu_to_be16(t);
695 	dup->length = cpu_to_be16(t);
696 	*xfs_dir2_data_unused_tag_p(dup) = cpu_to_be16((char *)dup - (char *)hdr);
697 	/*
698 	 * Log it and return it.
699 	 */
700 	xfs_dir2_data_log_header(args, bp);
701 	xfs_dir2_data_log_unused(args, bp, dup);
702 	*bpp = bp;
703 	return 0;
704 }
705 
706 /*
707  * Log an active data entry from the block.
708  */
709 void
710 xfs_dir2_data_log_entry(
711 	struct xfs_da_args	*args,
712 	struct xfs_buf		*bp,
713 	xfs_dir2_data_entry_t	*dep)		/* data entry pointer */
714 {
715 	struct xfs_dir2_data_hdr *hdr = bp->b_addr;
716 
717 	ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
718 	       hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC) ||
719 	       hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) ||
720 	       hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC));
721 
722 	xfs_trans_log_buf(args->trans, bp, (uint)((char *)dep - (char *)hdr),
723 		(uint)((char *)(args->dp->d_ops->data_entry_tag_p(dep) + 1) -
724 		       (char *)hdr - 1));
725 }
726 
727 /*
728  * Log a data block header.
729  */
730 void
731 xfs_dir2_data_log_header(
732 	struct xfs_da_args	*args,
733 	struct xfs_buf		*bp)
734 {
735 #ifdef DEBUG
736 	struct xfs_dir2_data_hdr *hdr = bp->b_addr;
737 
738 	ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
739 	       hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC) ||
740 	       hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) ||
741 	       hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC));
742 #endif
743 
744 	xfs_trans_log_buf(args->trans, bp, 0,
745 			  args->dp->d_ops->data_entry_offset - 1);
746 }
747 
748 /*
749  * Log a data unused entry.
750  */
751 void
752 xfs_dir2_data_log_unused(
753 	struct xfs_da_args	*args,
754 	struct xfs_buf		*bp,
755 	xfs_dir2_data_unused_t	*dup)		/* data unused pointer */
756 {
757 	xfs_dir2_data_hdr_t	*hdr = bp->b_addr;
758 
759 	ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
760 	       hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC) ||
761 	       hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) ||
762 	       hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC));
763 
764 	/*
765 	 * Log the first part of the unused entry.
766 	 */
767 	xfs_trans_log_buf(args->trans, bp, (uint)((char *)dup - (char *)hdr),
768 		(uint)((char *)&dup->length + sizeof(dup->length) -
769 		       1 - (char *)hdr));
770 	/*
771 	 * Log the end (tag) of the unused entry.
772 	 */
773 	xfs_trans_log_buf(args->trans, bp,
774 		(uint)((char *)xfs_dir2_data_unused_tag_p(dup) - (char *)hdr),
775 		(uint)((char *)xfs_dir2_data_unused_tag_p(dup) - (char *)hdr +
776 		       sizeof(xfs_dir2_data_off_t) - 1));
777 }
778 
779 /*
780  * Make a byte range in the data block unused.
781  * Its current contents are unimportant.
782  */
783 void
784 xfs_dir2_data_make_free(
785 	struct xfs_da_args	*args,
786 	struct xfs_buf		*bp,
787 	xfs_dir2_data_aoff_t	offset,		/* starting byte offset */
788 	xfs_dir2_data_aoff_t	len,		/* length in bytes */
789 	int			*needlogp,	/* out: log header */
790 	int			*needscanp)	/* out: regen bestfree */
791 {
792 	xfs_dir2_data_hdr_t	*hdr;		/* data block pointer */
793 	xfs_dir2_data_free_t	*dfp;		/* bestfree pointer */
794 	char			*endptr;	/* end of data area */
795 	int			needscan;	/* need to regen bestfree */
796 	xfs_dir2_data_unused_t	*newdup;	/* new unused entry */
797 	xfs_dir2_data_unused_t	*postdup;	/* unused entry after us */
798 	xfs_dir2_data_unused_t	*prevdup;	/* unused entry before us */
799 	struct xfs_dir2_data_free *bf;
800 
801 	hdr = bp->b_addr;
802 
803 	/*
804 	 * Figure out where the end of the data area is.
805 	 */
806 	endptr = xfs_dir3_data_endp(args->geo, hdr);
807 	ASSERT(endptr != NULL);
808 
809 	/*
810 	 * If this isn't the start of the block, then back up to
811 	 * the previous entry and see if it's free.
812 	 */
813 	if (offset > args->dp->d_ops->data_entry_offset) {
814 		__be16			*tagp;	/* tag just before us */
815 
816 		tagp = (__be16 *)((char *)hdr + offset) - 1;
817 		prevdup = (xfs_dir2_data_unused_t *)((char *)hdr + be16_to_cpu(*tagp));
818 		if (be16_to_cpu(prevdup->freetag) != XFS_DIR2_DATA_FREE_TAG)
819 			prevdup = NULL;
820 	} else
821 		prevdup = NULL;
822 	/*
823 	 * If this isn't the end of the block, see if the entry after
824 	 * us is free.
825 	 */
826 	if ((char *)hdr + offset + len < endptr) {
827 		postdup =
828 			(xfs_dir2_data_unused_t *)((char *)hdr + offset + len);
829 		if (be16_to_cpu(postdup->freetag) != XFS_DIR2_DATA_FREE_TAG)
830 			postdup = NULL;
831 	} else
832 		postdup = NULL;
833 	ASSERT(*needscanp == 0);
834 	needscan = 0;
835 	/*
836 	 * Previous and following entries are both free,
837 	 * merge everything into a single free entry.
838 	 */
839 	bf = args->dp->d_ops->data_bestfree_p(hdr);
840 	if (prevdup && postdup) {
841 		xfs_dir2_data_free_t	*dfp2;	/* another bestfree pointer */
842 
843 		/*
844 		 * See if prevdup and/or postdup are in bestfree table.
845 		 */
846 		dfp = xfs_dir2_data_freefind(hdr, bf, prevdup);
847 		dfp2 = xfs_dir2_data_freefind(hdr, bf, postdup);
848 		/*
849 		 * We need a rescan unless there are exactly 2 free entries
850 		 * namely our two.  Then we know what's happening, otherwise
851 		 * since the third bestfree is there, there might be more
852 		 * entries.
853 		 */
854 		needscan = (bf[2].length != 0);
855 		/*
856 		 * Fix up the new big freespace.
857 		 */
858 		be16_add_cpu(&prevdup->length, len + be16_to_cpu(postdup->length));
859 		*xfs_dir2_data_unused_tag_p(prevdup) =
860 			cpu_to_be16((char *)prevdup - (char *)hdr);
861 		xfs_dir2_data_log_unused(args, bp, prevdup);
862 		if (!needscan) {
863 			/*
864 			 * Has to be the case that entries 0 and 1 are
865 			 * dfp and dfp2 (don't know which is which), and
866 			 * entry 2 is empty.
867 			 * Remove entry 1 first then entry 0.
868 			 */
869 			ASSERT(dfp && dfp2);
870 			if (dfp == &bf[1]) {
871 				dfp = &bf[0];
872 				ASSERT(dfp2 == dfp);
873 				dfp2 = &bf[1];
874 			}
875 			xfs_dir2_data_freeremove(hdr, bf, dfp2, needlogp);
876 			xfs_dir2_data_freeremove(hdr, bf, dfp, needlogp);
877 			/*
878 			 * Now insert the new entry.
879 			 */
880 			dfp = xfs_dir2_data_freeinsert(hdr, bf, prevdup,
881 						       needlogp);
882 			ASSERT(dfp == &bf[0]);
883 			ASSERT(dfp->length == prevdup->length);
884 			ASSERT(!dfp[1].length);
885 			ASSERT(!dfp[2].length);
886 		}
887 	}
888 	/*
889 	 * The entry before us is free, merge with it.
890 	 */
891 	else if (prevdup) {
892 		dfp = xfs_dir2_data_freefind(hdr, bf, prevdup);
893 		be16_add_cpu(&prevdup->length, len);
894 		*xfs_dir2_data_unused_tag_p(prevdup) =
895 			cpu_to_be16((char *)prevdup - (char *)hdr);
896 		xfs_dir2_data_log_unused(args, bp, prevdup);
897 		/*
898 		 * If the previous entry was in the table, the new entry
899 		 * is longer, so it will be in the table too.  Remove
900 		 * the old one and add the new one.
901 		 */
902 		if (dfp) {
903 			xfs_dir2_data_freeremove(hdr, bf, dfp, needlogp);
904 			xfs_dir2_data_freeinsert(hdr, bf, prevdup, needlogp);
905 		}
906 		/*
907 		 * Otherwise we need a scan if the new entry is big enough.
908 		 */
909 		else {
910 			needscan = be16_to_cpu(prevdup->length) >
911 				   be16_to_cpu(bf[2].length);
912 		}
913 	}
914 	/*
915 	 * The following entry is free, merge with it.
916 	 */
917 	else if (postdup) {
918 		dfp = xfs_dir2_data_freefind(hdr, bf, postdup);
919 		newdup = (xfs_dir2_data_unused_t *)((char *)hdr + offset);
920 		newdup->freetag = cpu_to_be16(XFS_DIR2_DATA_FREE_TAG);
921 		newdup->length = cpu_to_be16(len + be16_to_cpu(postdup->length));
922 		*xfs_dir2_data_unused_tag_p(newdup) =
923 			cpu_to_be16((char *)newdup - (char *)hdr);
924 		xfs_dir2_data_log_unused(args, bp, newdup);
925 		/*
926 		 * If the following entry was in the table, the new entry
927 		 * is longer, so it will be in the table too.  Remove
928 		 * the old one and add the new one.
929 		 */
930 		if (dfp) {
931 			xfs_dir2_data_freeremove(hdr, bf, dfp, needlogp);
932 			xfs_dir2_data_freeinsert(hdr, bf, newdup, needlogp);
933 		}
934 		/*
935 		 * Otherwise we need a scan if the new entry is big enough.
936 		 */
937 		else {
938 			needscan = be16_to_cpu(newdup->length) >
939 				   be16_to_cpu(bf[2].length);
940 		}
941 	}
942 	/*
943 	 * Neither neighbor is free.  Make a new entry.
944 	 */
945 	else {
946 		newdup = (xfs_dir2_data_unused_t *)((char *)hdr + offset);
947 		newdup->freetag = cpu_to_be16(XFS_DIR2_DATA_FREE_TAG);
948 		newdup->length = cpu_to_be16(len);
949 		*xfs_dir2_data_unused_tag_p(newdup) =
950 			cpu_to_be16((char *)newdup - (char *)hdr);
951 		xfs_dir2_data_log_unused(args, bp, newdup);
952 		xfs_dir2_data_freeinsert(hdr, bf, newdup, needlogp);
953 	}
954 	*needscanp = needscan;
955 }
956 
957 /* Check our free data for obvious signs of corruption. */
958 static inline xfs_failaddr_t
959 xfs_dir2_data_check_free(
960 	struct xfs_dir2_data_hdr	*hdr,
961 	struct xfs_dir2_data_unused	*dup,
962 	xfs_dir2_data_aoff_t		offset,
963 	xfs_dir2_data_aoff_t		len)
964 {
965 	if (hdr->magic != cpu_to_be32(XFS_DIR2_DATA_MAGIC) &&
966 	    hdr->magic != cpu_to_be32(XFS_DIR3_DATA_MAGIC) &&
967 	    hdr->magic != cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) &&
968 	    hdr->magic != cpu_to_be32(XFS_DIR3_BLOCK_MAGIC))
969 		return __this_address;
970 	if (be16_to_cpu(dup->freetag) != XFS_DIR2_DATA_FREE_TAG)
971 		return __this_address;
972 	if (offset < (char *)dup - (char *)hdr)
973 		return __this_address;
974 	if (offset + len > (char *)dup + be16_to_cpu(dup->length) - (char *)hdr)
975 		return __this_address;
976 	if ((char *)dup - (char *)hdr !=
977 			be16_to_cpu(*xfs_dir2_data_unused_tag_p(dup)))
978 		return __this_address;
979 	return NULL;
980 }
981 
982 /* Sanity-check a new bestfree entry. */
983 static inline xfs_failaddr_t
984 xfs_dir2_data_check_new_free(
985 	struct xfs_dir2_data_hdr	*hdr,
986 	struct xfs_dir2_data_free	*dfp,
987 	struct xfs_dir2_data_unused	*newdup)
988 {
989 	if (dfp == NULL)
990 		return __this_address;
991 	if (dfp->length != newdup->length)
992 		return __this_address;
993 	if (be16_to_cpu(dfp->offset) != (char *)newdup - (char *)hdr)
994 		return __this_address;
995 	return NULL;
996 }
997 
998 /*
999  * Take a byte range out of an existing unused space and make it un-free.
1000  */
1001 int
1002 xfs_dir2_data_use_free(
1003 	struct xfs_da_args	*args,
1004 	struct xfs_buf		*bp,
1005 	xfs_dir2_data_unused_t	*dup,		/* unused entry */
1006 	xfs_dir2_data_aoff_t	offset,		/* starting offset to use */
1007 	xfs_dir2_data_aoff_t	len,		/* length to use */
1008 	int			*needlogp,	/* out: need to log header */
1009 	int			*needscanp)	/* out: need regen bestfree */
1010 {
1011 	xfs_dir2_data_hdr_t	*hdr;		/* data block header */
1012 	xfs_dir2_data_free_t	*dfp;		/* bestfree pointer */
1013 	xfs_dir2_data_unused_t	*newdup;	/* new unused entry */
1014 	xfs_dir2_data_unused_t	*newdup2;	/* another new unused entry */
1015 	struct xfs_dir2_data_free *bf;
1016 	xfs_failaddr_t		fa;
1017 	int			matchback;	/* matches end of freespace */
1018 	int			matchfront;	/* matches start of freespace */
1019 	int			needscan;	/* need to regen bestfree */
1020 	int			oldlen;		/* old unused entry's length */
1021 
1022 	hdr = bp->b_addr;
1023 	fa = xfs_dir2_data_check_free(hdr, dup, offset, len);
1024 	if (fa)
1025 		goto corrupt;
1026 	/*
1027 	 * Look up the entry in the bestfree table.
1028 	 */
1029 	oldlen = be16_to_cpu(dup->length);
1030 	bf = args->dp->d_ops->data_bestfree_p(hdr);
1031 	dfp = xfs_dir2_data_freefind(hdr, bf, dup);
1032 	ASSERT(dfp || oldlen <= be16_to_cpu(bf[2].length));
1033 	/*
1034 	 * Check for alignment with front and back of the entry.
1035 	 */
1036 	matchfront = (char *)dup - (char *)hdr == offset;
1037 	matchback = (char *)dup + oldlen - (char *)hdr == offset + len;
1038 	ASSERT(*needscanp == 0);
1039 	needscan = 0;
1040 	/*
1041 	 * If we matched it exactly we just need to get rid of it from
1042 	 * the bestfree table.
1043 	 */
1044 	if (matchfront && matchback) {
1045 		if (dfp) {
1046 			needscan = (bf[2].offset != 0);
1047 			if (!needscan)
1048 				xfs_dir2_data_freeremove(hdr, bf, dfp,
1049 							 needlogp);
1050 		}
1051 	}
1052 	/*
1053 	 * We match the first part of the entry.
1054 	 * Make a new entry with the remaining freespace.
1055 	 */
1056 	else if (matchfront) {
1057 		newdup = (xfs_dir2_data_unused_t *)((char *)hdr + offset + len);
1058 		newdup->freetag = cpu_to_be16(XFS_DIR2_DATA_FREE_TAG);
1059 		newdup->length = cpu_to_be16(oldlen - len);
1060 		*xfs_dir2_data_unused_tag_p(newdup) =
1061 			cpu_to_be16((char *)newdup - (char *)hdr);
1062 		xfs_dir2_data_log_unused(args, bp, newdup);
1063 		/*
1064 		 * If it was in the table, remove it and add the new one.
1065 		 */
1066 		if (dfp) {
1067 			xfs_dir2_data_freeremove(hdr, bf, dfp, needlogp);
1068 			dfp = xfs_dir2_data_freeinsert(hdr, bf, newdup,
1069 						       needlogp);
1070 			fa = xfs_dir2_data_check_new_free(hdr, dfp, newdup);
1071 			if (fa)
1072 				goto corrupt;
1073 			/*
1074 			 * If we got inserted at the last slot,
1075 			 * that means we don't know if there was a better
1076 			 * choice for the last slot, or not.  Rescan.
1077 			 */
1078 			needscan = dfp == &bf[2];
1079 		}
1080 	}
1081 	/*
1082 	 * We match the last part of the entry.
1083 	 * Trim the allocated space off the tail of the entry.
1084 	 */
1085 	else if (matchback) {
1086 		newdup = dup;
1087 		newdup->length = cpu_to_be16(((char *)hdr + offset) - (char *)newdup);
1088 		*xfs_dir2_data_unused_tag_p(newdup) =
1089 			cpu_to_be16((char *)newdup - (char *)hdr);
1090 		xfs_dir2_data_log_unused(args, bp, newdup);
1091 		/*
1092 		 * If it was in the table, remove it and add the new one.
1093 		 */
1094 		if (dfp) {
1095 			xfs_dir2_data_freeremove(hdr, bf, dfp, needlogp);
1096 			dfp = xfs_dir2_data_freeinsert(hdr, bf, newdup,
1097 						       needlogp);
1098 			fa = xfs_dir2_data_check_new_free(hdr, dfp, newdup);
1099 			if (fa)
1100 				goto corrupt;
1101 			/*
1102 			 * If we got inserted at the last slot,
1103 			 * that means we don't know if there was a better
1104 			 * choice for the last slot, or not.  Rescan.
1105 			 */
1106 			needscan = dfp == &bf[2];
1107 		}
1108 	}
1109 	/*
1110 	 * Poking out the middle of an entry.
1111 	 * Make two new entries.
1112 	 */
1113 	else {
1114 		newdup = dup;
1115 		newdup->length = cpu_to_be16(((char *)hdr + offset) - (char *)newdup);
1116 		*xfs_dir2_data_unused_tag_p(newdup) =
1117 			cpu_to_be16((char *)newdup - (char *)hdr);
1118 		xfs_dir2_data_log_unused(args, bp, newdup);
1119 		newdup2 = (xfs_dir2_data_unused_t *)((char *)hdr + offset + len);
1120 		newdup2->freetag = cpu_to_be16(XFS_DIR2_DATA_FREE_TAG);
1121 		newdup2->length = cpu_to_be16(oldlen - len - be16_to_cpu(newdup->length));
1122 		*xfs_dir2_data_unused_tag_p(newdup2) =
1123 			cpu_to_be16((char *)newdup2 - (char *)hdr);
1124 		xfs_dir2_data_log_unused(args, bp, newdup2);
1125 		/*
1126 		 * If the old entry was in the table, we need to scan
1127 		 * if the 3rd entry was valid, since these entries
1128 		 * are smaller than the old one.
1129 		 * If we don't need to scan that means there were 1 or 2
1130 		 * entries in the table, and removing the old and adding
1131 		 * the 2 new will work.
1132 		 */
1133 		if (dfp) {
1134 			needscan = (bf[2].length != 0);
1135 			if (!needscan) {
1136 				xfs_dir2_data_freeremove(hdr, bf, dfp,
1137 							 needlogp);
1138 				xfs_dir2_data_freeinsert(hdr, bf, newdup,
1139 							 needlogp);
1140 				xfs_dir2_data_freeinsert(hdr, bf, newdup2,
1141 							 needlogp);
1142 			}
1143 		}
1144 	}
1145 	*needscanp = needscan;
1146 	return 0;
1147 corrupt:
1148 	xfs_corruption_error(__func__, XFS_ERRLEVEL_LOW, args->dp->i_mount,
1149 			hdr, sizeof(*hdr), __FILE__, __LINE__, fa);
1150 	return -EFSCORRUPTED;
1151 }
1152 
1153 /* Find the end of the entry data in a data/block format dir block. */
1154 void *
1155 xfs_dir3_data_endp(
1156 	struct xfs_da_geometry		*geo,
1157 	struct xfs_dir2_data_hdr	*hdr)
1158 {
1159 	switch (hdr->magic) {
1160 	case cpu_to_be32(XFS_DIR3_BLOCK_MAGIC):
1161 	case cpu_to_be32(XFS_DIR2_BLOCK_MAGIC):
1162 		return xfs_dir2_block_leaf_p(xfs_dir2_block_tail_p(geo, hdr));
1163 	case cpu_to_be32(XFS_DIR3_DATA_MAGIC):
1164 	case cpu_to_be32(XFS_DIR2_DATA_MAGIC):
1165 		return (char *)hdr + geo->blksize;
1166 	default:
1167 		return NULL;
1168 	}
1169 }
1170