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