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