1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2000-2002,2005 Silicon Graphics, Inc.
4 * Copyright (c) 2013 Red Hat, Inc.
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_da_format.h"
15 #include "xfs_da_btree.h"
16 #include "xfs_inode.h"
18 #include "xfs_dir2_priv.h"
19 #include "xfs_error.h"
20 #include "xfs_trans.h"
21 #include "xfs_buf_item.h"
22 #include "xfs_cksum.h"
25 static xfs_failaddr_t xfs_dir2_data_freefind_verify(
26 struct xfs_dir2_data_hdr *hdr, struct xfs_dir2_data_free *bf,
27 struct xfs_dir2_data_unused *dup,
28 struct xfs_dir2_data_free **bf_ent);
31 * Check the consistency of the data block.
32 * The input can also be a block-format directory.
33 * Return NULL if the buffer is good, otherwise the address of the error.
36 __xfs_dir3_data_check(
37 struct xfs_inode *dp, /* incore inode pointer */
38 struct xfs_buf *bp) /* data block's buffer */
40 xfs_dir2_dataptr_t addr; /* addr for leaf lookup */
41 xfs_dir2_data_free_t *bf; /* bestfree table */
42 xfs_dir2_block_tail_t *btp=NULL; /* block tail */
43 int count; /* count of entries found */
44 xfs_dir2_data_hdr_t *hdr; /* data block header */
45 xfs_dir2_data_entry_t *dep; /* data entry */
46 xfs_dir2_data_free_t *dfp; /* bestfree entry */
47 xfs_dir2_data_unused_t *dup; /* unused entry */
48 char *endp; /* end of useful data */
49 int freeseen; /* mask of bestfrees seen */
50 xfs_dahash_t hash; /* hash of current name */
51 int i; /* leaf index */
52 int lastfree; /* last entry was unused */
53 xfs_dir2_leaf_entry_t *lep=NULL; /* block leaf entries */
54 struct xfs_mount *mp = bp->b_mount;
55 char *p; /* current data position */
56 int stale; /* count of stale leaves */
58 const struct xfs_dir_ops *ops;
59 struct xfs_da_geometry *geo;
64 * We can be passed a null dp here from a verifier, so we need to go the
65 * hard way to get them.
67 ops = xfs_dir_get_ops(mp, dp);
70 * If this isn't a directory, or we don't get handed the dir ops,
71 * something is seriously wrong. Bail out.
73 if ((dp && !S_ISDIR(VFS_I(dp)->i_mode)) ||
74 ops != xfs_dir_get_ops(mp, NULL))
75 return __this_address;
78 p = (char *)ops->data_entry_p(hdr);
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);
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
93 if (be32_to_cpu(btp->count) >=
94 ((char *)btp - p) / sizeof(struct xfs_dir2_leaf_entry))
95 return __this_address;
97 case cpu_to_be32(XFS_DIR3_DATA_MAGIC):
98 case cpu_to_be32(XFS_DIR2_DATA_MAGIC):
101 return __this_address;
103 endp = xfs_dir3_data_endp(geo, hdr);
105 return __this_address;
108 * Account for zero bestfree entries.
110 bf = ops->data_bestfree_p(hdr);
111 count = lastfree = freeseen = 0;
114 return __this_address;
119 return __this_address;
124 return __this_address;
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;
133 * Loop over the data/unused entries.
136 dup = (xfs_dir2_data_unused_t *)p;
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.
142 if (be16_to_cpu(dup->freetag) == XFS_DIR2_DATA_FREE_TAG) {
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);
157 if ((freeseen & (1 << i)) != 0)
158 return __this_address;
161 if (be16_to_cpu(dup->length) >
162 be16_to_cpu(bf[2].length))
163 return __this_address;
165 p += be16_to_cpu(dup->length);
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.
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;
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)
202 if (i >= be32_to_cpu(btp->count))
203 return __this_address;
205 p += ops->data_entsize(dep->namelen);
208 * Need to have seen all the entries and all the bestfree slots.
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))
218 if (i > 0 && be32_to_cpu(lep[i].hashval) <
219 be32_to_cpu(lep[i - 1].hashval))
220 return __this_address;
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;
233 struct xfs_inode *dp,
238 fa = __xfs_dir3_data_check(dp, bp);
241 xfs_corruption_error(__func__, XFS_ERRLEVEL_LOW, dp->i_mount,
242 bp->b_addr, BBTOB(bp->b_length), __FILE__, __LINE__,
248 static xfs_failaddr_t
249 xfs_dir3_data_verify(
252 struct xfs_mount *mp = bp->b_mount;
253 struct xfs_dir3_blk_hdr *hdr3 = bp->b_addr;
255 if (!xfs_verify_magic(bp, hdr3->magic))
256 return __this_address;
258 if (xfs_sb_version_hascrc(&mp->m_sb)) {
259 if (!uuid_equal(&hdr3->uuid, &mp->m_sb.sb_meta_uuid))
260 return __this_address;
261 if (be64_to_cpu(hdr3->blkno) != bp->b_bn)
262 return __this_address;
263 if (!xfs_log_check_lsn(mp, be64_to_cpu(hdr3->lsn)))
264 return __this_address;
266 return __xfs_dir3_data_check(NULL, bp);
270 * Readahead of the first block of the directory when it is opened is completely
271 * oblivious to the format of the directory. Hence we can either get a block
272 * format buffer or a data format buffer on readahead.
275 xfs_dir3_data_reada_verify(
278 struct xfs_dir2_data_hdr *hdr = bp->b_addr;
280 switch (hdr->magic) {
281 case cpu_to_be32(XFS_DIR2_BLOCK_MAGIC):
282 case cpu_to_be32(XFS_DIR3_BLOCK_MAGIC):
283 bp->b_ops = &xfs_dir3_block_buf_ops;
284 bp->b_ops->verify_read(bp);
286 case cpu_to_be32(XFS_DIR2_DATA_MAGIC):
287 case cpu_to_be32(XFS_DIR3_DATA_MAGIC):
288 bp->b_ops = &xfs_dir3_data_buf_ops;
289 bp->b_ops->verify_read(bp);
292 xfs_verifier_error(bp, -EFSCORRUPTED, __this_address);
298 xfs_dir3_data_read_verify(
301 struct xfs_mount *mp = bp->b_mount;
304 if (xfs_sb_version_hascrc(&mp->m_sb) &&
305 !xfs_buf_verify_cksum(bp, XFS_DIR3_DATA_CRC_OFF))
306 xfs_verifier_error(bp, -EFSBADCRC, __this_address);
308 fa = xfs_dir3_data_verify(bp);
310 xfs_verifier_error(bp, -EFSCORRUPTED, fa);
315 xfs_dir3_data_write_verify(
318 struct xfs_mount *mp = bp->b_mount;
319 struct xfs_buf_log_item *bip = bp->b_log_item;
320 struct xfs_dir3_blk_hdr *hdr3 = bp->b_addr;
323 fa = xfs_dir3_data_verify(bp);
325 xfs_verifier_error(bp, -EFSCORRUPTED, fa);
329 if (!xfs_sb_version_hascrc(&mp->m_sb))
333 hdr3->lsn = cpu_to_be64(bip->bli_item.li_lsn);
335 xfs_buf_update_cksum(bp, XFS_DIR3_DATA_CRC_OFF);
338 const struct xfs_buf_ops xfs_dir3_data_buf_ops = {
339 .name = "xfs_dir3_data",
340 .magic = { cpu_to_be32(XFS_DIR2_DATA_MAGIC),
341 cpu_to_be32(XFS_DIR3_DATA_MAGIC) },
342 .verify_read = xfs_dir3_data_read_verify,
343 .verify_write = xfs_dir3_data_write_verify,
344 .verify_struct = xfs_dir3_data_verify,
347 static const struct xfs_buf_ops xfs_dir3_data_reada_buf_ops = {
348 .name = "xfs_dir3_data_reada",
349 .magic = { cpu_to_be32(XFS_DIR2_DATA_MAGIC),
350 cpu_to_be32(XFS_DIR3_DATA_MAGIC) },
351 .verify_read = xfs_dir3_data_reada_verify,
352 .verify_write = xfs_dir3_data_write_verify,
358 struct xfs_trans *tp,
359 struct xfs_inode *dp,
361 xfs_daddr_t mapped_bno,
362 struct xfs_buf **bpp)
366 err = xfs_da_read_buf(tp, dp, bno, mapped_bno, bpp,
367 XFS_DATA_FORK, &xfs_dir3_data_buf_ops);
368 if (!err && tp && *bpp)
369 xfs_trans_buf_set_type(tp, *bpp, XFS_BLFT_DIR_DATA_BUF);
374 xfs_dir3_data_readahead(
375 struct xfs_inode *dp,
377 xfs_daddr_t mapped_bno)
379 return xfs_da_reada_buf(dp, bno, mapped_bno,
380 XFS_DATA_FORK, &xfs_dir3_data_reada_buf_ops);
384 * Find the bestfree entry that exactly coincides with unused directory space
385 * or a verifier error because the bestfree data are bad.
387 static xfs_failaddr_t
388 xfs_dir2_data_freefind_verify(
389 struct xfs_dir2_data_hdr *hdr,
390 struct xfs_dir2_data_free *bf,
391 struct xfs_dir2_data_unused *dup,
392 struct xfs_dir2_data_free **bf_ent)
394 struct xfs_dir2_data_free *dfp;
395 xfs_dir2_data_aoff_t off;
396 bool matched = false;
397 bool seenzero = false;
400 off = (xfs_dir2_data_aoff_t)((char *)dup - (char *)hdr);
403 * Validate some consistency in the bestfree table.
404 * Check order, non-overlapping entries, and if we find the
405 * one we're looking for it has to be exact.
407 for (dfp = &bf[0]; dfp < &bf[XFS_DIR2_DATA_FD_COUNT]; dfp++) {
410 return __this_address;
415 return __this_address;
416 if (be16_to_cpu(dfp->offset) == off) {
418 if (dfp->length != dup->length)
419 return __this_address;
420 } else if (be16_to_cpu(dfp->offset) > off) {
421 if (off + be16_to_cpu(dup->length) >
422 be16_to_cpu(dfp->offset))
423 return __this_address;
425 if (be16_to_cpu(dfp->offset) +
426 be16_to_cpu(dfp->length) > off)
427 return __this_address;
430 be16_to_cpu(dfp->length) < be16_to_cpu(dup->length))
431 return __this_address;
433 be16_to_cpu(dfp[-1].length) < be16_to_cpu(dfp[0].length))
434 return __this_address;
437 /* Looks ok so far; now try to match up with a bestfree entry. */
438 *bf_ent = xfs_dir2_data_freefind(hdr, bf, dup);
443 * Given a data block and an unused entry from that block,
444 * return the bestfree entry if any that corresponds to it.
446 xfs_dir2_data_free_t *
447 xfs_dir2_data_freefind(
448 struct xfs_dir2_data_hdr *hdr, /* data block header */
449 struct xfs_dir2_data_free *bf, /* bestfree table pointer */
450 struct xfs_dir2_data_unused *dup) /* unused space */
452 xfs_dir2_data_free_t *dfp; /* bestfree entry */
453 xfs_dir2_data_aoff_t off; /* offset value needed */
455 off = (xfs_dir2_data_aoff_t)((char *)dup - (char *)hdr);
458 * If this is smaller than the smallest bestfree entry,
459 * it can't be there since they're sorted.
461 if (be16_to_cpu(dup->length) <
462 be16_to_cpu(bf[XFS_DIR2_DATA_FD_COUNT - 1].length))
465 * Look at the three bestfree entries for our guy.
467 for (dfp = &bf[0]; dfp < &bf[XFS_DIR2_DATA_FD_COUNT]; dfp++) {
470 if (be16_to_cpu(dfp->offset) == off)
474 * Didn't find it. This only happens if there are duplicate lengths.
480 * Insert an unused-space entry into the bestfree table.
482 xfs_dir2_data_free_t * /* entry inserted */
483 xfs_dir2_data_freeinsert(
484 struct xfs_dir2_data_hdr *hdr, /* data block pointer */
485 struct xfs_dir2_data_free *dfp, /* bestfree table pointer */
486 struct xfs_dir2_data_unused *dup, /* unused space */
487 int *loghead) /* log the data header (out) */
489 xfs_dir2_data_free_t new; /* new bestfree entry */
491 ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
492 hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) ||
493 hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC) ||
494 hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC));
496 new.length = dup->length;
497 new.offset = cpu_to_be16((char *)dup - (char *)hdr);
500 * Insert at position 0, 1, or 2; or not at all.
502 if (be16_to_cpu(new.length) > be16_to_cpu(dfp[0].length)) {
509 if (be16_to_cpu(new.length) > be16_to_cpu(dfp[1].length)) {
515 if (be16_to_cpu(new.length) > be16_to_cpu(dfp[2].length)) {
524 * Remove a bestfree entry from the table.
527 xfs_dir2_data_freeremove(
528 struct xfs_dir2_data_hdr *hdr, /* data block header */
529 struct xfs_dir2_data_free *bf, /* bestfree table pointer */
530 struct xfs_dir2_data_free *dfp, /* bestfree entry pointer */
531 int *loghead) /* out: log data header */
534 ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
535 hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) ||
536 hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC) ||
537 hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC));
540 * It's the first entry, slide the next 2 up.
547 * It's the second entry, slide the 3rd entry up.
549 else if (dfp == &bf[1])
552 * Must be the last entry.
555 ASSERT(dfp == &bf[2]);
557 * Clear the 3rd entry, must be zero now.
565 * Given a data block, reconstruct its bestfree map.
568 xfs_dir2_data_freescan_int(
569 struct xfs_da_geometry *geo,
570 const struct xfs_dir_ops *ops,
571 struct xfs_dir2_data_hdr *hdr,
574 xfs_dir2_data_entry_t *dep; /* active data entry */
575 xfs_dir2_data_unused_t *dup; /* unused data entry */
576 struct xfs_dir2_data_free *bf;
577 char *endp; /* end of block's data */
578 char *p; /* current entry pointer */
580 ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
581 hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC) ||
582 hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) ||
583 hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC));
586 * Start by clearing the table.
588 bf = ops->data_bestfree_p(hdr);
589 memset(bf, 0, sizeof(*bf) * XFS_DIR2_DATA_FD_COUNT);
594 p = (char *)ops->data_entry_p(hdr);
595 endp = xfs_dir3_data_endp(geo, hdr);
597 * Loop over the block's entries.
600 dup = (xfs_dir2_data_unused_t *)p;
602 * If it's a free entry, insert it.
604 if (be16_to_cpu(dup->freetag) == XFS_DIR2_DATA_FREE_TAG) {
605 ASSERT((char *)dup - (char *)hdr ==
606 be16_to_cpu(*xfs_dir2_data_unused_tag_p(dup)));
607 xfs_dir2_data_freeinsert(hdr, bf, dup, loghead);
608 p += be16_to_cpu(dup->length);
611 * For active entries, check their tags and skip them.
614 dep = (xfs_dir2_data_entry_t *)p;
615 ASSERT((char *)dep - (char *)hdr ==
616 be16_to_cpu(*ops->data_entry_tag_p(dep)));
617 p += ops->data_entsize(dep->namelen);
623 xfs_dir2_data_freescan(
624 struct xfs_inode *dp,
625 struct xfs_dir2_data_hdr *hdr,
628 return xfs_dir2_data_freescan_int(dp->i_mount->m_dir_geo, dp->d_ops,
633 * Initialize a data block at the given block number in the directory.
634 * Give back the buffer for the created block.
638 xfs_da_args_t *args, /* directory operation args */
639 xfs_dir2_db_t blkno, /* logical dir block number */
640 struct xfs_buf **bpp) /* output block buffer */
642 struct xfs_buf *bp; /* block buffer */
643 xfs_dir2_data_hdr_t *hdr; /* data block header */
644 xfs_inode_t *dp; /* incore directory inode */
645 xfs_dir2_data_unused_t *dup; /* unused entry pointer */
646 struct xfs_dir2_data_free *bf;
647 int error; /* error return value */
648 int i; /* bestfree index */
649 xfs_mount_t *mp; /* filesystem mount point */
650 xfs_trans_t *tp; /* transaction pointer */
657 * Get the buffer set up for the block.
659 error = xfs_da_get_buf(tp, dp, xfs_dir2_db_to_da(args->geo, blkno),
660 -1, &bp, XFS_DATA_FORK);
663 bp->b_ops = &xfs_dir3_data_buf_ops;
664 xfs_trans_buf_set_type(tp, bp, XFS_BLFT_DIR_DATA_BUF);
667 * Initialize the header.
670 if (xfs_sb_version_hascrc(&mp->m_sb)) {
671 struct xfs_dir3_blk_hdr *hdr3 = bp->b_addr;
673 memset(hdr3, 0, sizeof(*hdr3));
674 hdr3->magic = cpu_to_be32(XFS_DIR3_DATA_MAGIC);
675 hdr3->blkno = cpu_to_be64(bp->b_bn);
676 hdr3->owner = cpu_to_be64(dp->i_ino);
677 uuid_copy(&hdr3->uuid, &mp->m_sb.sb_meta_uuid);
680 hdr->magic = cpu_to_be32(XFS_DIR2_DATA_MAGIC);
682 bf = dp->d_ops->data_bestfree_p(hdr);
683 bf[0].offset = cpu_to_be16(dp->d_ops->data_entry_offset);
684 for (i = 1; i < XFS_DIR2_DATA_FD_COUNT; i++) {
690 * Set up an unused entry for the block's body.
692 dup = dp->d_ops->data_unused_p(hdr);
693 dup->freetag = cpu_to_be16(XFS_DIR2_DATA_FREE_TAG);
695 t = args->geo->blksize - (uint)dp->d_ops->data_entry_offset;
696 bf[0].length = cpu_to_be16(t);
697 dup->length = cpu_to_be16(t);
698 *xfs_dir2_data_unused_tag_p(dup) = cpu_to_be16((char *)dup - (char *)hdr);
700 * Log it and return it.
702 xfs_dir2_data_log_header(args, bp);
703 xfs_dir2_data_log_unused(args, bp, dup);
709 * Log an active data entry from the block.
712 xfs_dir2_data_log_entry(
713 struct xfs_da_args *args,
715 xfs_dir2_data_entry_t *dep) /* data entry pointer */
717 struct xfs_dir2_data_hdr *hdr = bp->b_addr;
719 ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
720 hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC) ||
721 hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) ||
722 hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC));
724 xfs_trans_log_buf(args->trans, bp, (uint)((char *)dep - (char *)hdr),
725 (uint)((char *)(args->dp->d_ops->data_entry_tag_p(dep) + 1) -
730 * Log a data block header.
733 xfs_dir2_data_log_header(
734 struct xfs_da_args *args,
738 struct xfs_dir2_data_hdr *hdr = bp->b_addr;
740 ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
741 hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC) ||
742 hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) ||
743 hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC));
746 xfs_trans_log_buf(args->trans, bp, 0,
747 args->dp->d_ops->data_entry_offset - 1);
751 * Log a data unused entry.
754 xfs_dir2_data_log_unused(
755 struct xfs_da_args *args,
757 xfs_dir2_data_unused_t *dup) /* data unused pointer */
759 xfs_dir2_data_hdr_t *hdr = bp->b_addr;
761 ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
762 hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC) ||
763 hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) ||
764 hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC));
767 * Log the first part of the unused entry.
769 xfs_trans_log_buf(args->trans, bp, (uint)((char *)dup - (char *)hdr),
770 (uint)((char *)&dup->length + sizeof(dup->length) -
773 * Log the end (tag) of the unused entry.
775 xfs_trans_log_buf(args->trans, bp,
776 (uint)((char *)xfs_dir2_data_unused_tag_p(dup) - (char *)hdr),
777 (uint)((char *)xfs_dir2_data_unused_tag_p(dup) - (char *)hdr +
778 sizeof(xfs_dir2_data_off_t) - 1));
782 * Make a byte range in the data block unused.
783 * Its current contents are unimportant.
786 xfs_dir2_data_make_free(
787 struct xfs_da_args *args,
789 xfs_dir2_data_aoff_t offset, /* starting byte offset */
790 xfs_dir2_data_aoff_t len, /* length in bytes */
791 int *needlogp, /* out: log header */
792 int *needscanp) /* out: regen bestfree */
794 xfs_dir2_data_hdr_t *hdr; /* data block pointer */
795 xfs_dir2_data_free_t *dfp; /* bestfree pointer */
796 char *endptr; /* end of data area */
797 int needscan; /* need to regen bestfree */
798 xfs_dir2_data_unused_t *newdup; /* new unused entry */
799 xfs_dir2_data_unused_t *postdup; /* unused entry after us */
800 xfs_dir2_data_unused_t *prevdup; /* unused entry before us */
801 struct xfs_dir2_data_free *bf;
806 * Figure out where the end of the data area is.
808 endptr = xfs_dir3_data_endp(args->geo, hdr);
809 ASSERT(endptr != NULL);
812 * If this isn't the start of the block, then back up to
813 * the previous entry and see if it's free.
815 if (offset > args->dp->d_ops->data_entry_offset) {
816 __be16 *tagp; /* tag just before us */
818 tagp = (__be16 *)((char *)hdr + offset) - 1;
819 prevdup = (xfs_dir2_data_unused_t *)((char *)hdr + be16_to_cpu(*tagp));
820 if (be16_to_cpu(prevdup->freetag) != XFS_DIR2_DATA_FREE_TAG)
825 * If this isn't the end of the block, see if the entry after
828 if ((char *)hdr + offset + len < endptr) {
830 (xfs_dir2_data_unused_t *)((char *)hdr + offset + len);
831 if (be16_to_cpu(postdup->freetag) != XFS_DIR2_DATA_FREE_TAG)
835 ASSERT(*needscanp == 0);
838 * Previous and following entries are both free,
839 * merge everything into a single free entry.
841 bf = args->dp->d_ops->data_bestfree_p(hdr);
842 if (prevdup && postdup) {
843 xfs_dir2_data_free_t *dfp2; /* another bestfree pointer */
846 * See if prevdup and/or postdup are in bestfree table.
848 dfp = xfs_dir2_data_freefind(hdr, bf, prevdup);
849 dfp2 = xfs_dir2_data_freefind(hdr, bf, postdup);
851 * We need a rescan unless there are exactly 2 free entries
852 * namely our two. Then we know what's happening, otherwise
853 * since the third bestfree is there, there might be more
856 needscan = (bf[2].length != 0);
858 * Fix up the new big freespace.
860 be16_add_cpu(&prevdup->length, len + be16_to_cpu(postdup->length));
861 *xfs_dir2_data_unused_tag_p(prevdup) =
862 cpu_to_be16((char *)prevdup - (char *)hdr);
863 xfs_dir2_data_log_unused(args, bp, prevdup);
866 * Has to be the case that entries 0 and 1 are
867 * dfp and dfp2 (don't know which is which), and
869 * Remove entry 1 first then entry 0.
877 xfs_dir2_data_freeremove(hdr, bf, dfp2, needlogp);
878 xfs_dir2_data_freeremove(hdr, bf, dfp, needlogp);
880 * Now insert the new entry.
882 dfp = xfs_dir2_data_freeinsert(hdr, bf, prevdup,
884 ASSERT(dfp == &bf[0]);
885 ASSERT(dfp->length == prevdup->length);
886 ASSERT(!dfp[1].length);
887 ASSERT(!dfp[2].length);
891 * The entry before us is free, merge with it.
894 dfp = xfs_dir2_data_freefind(hdr, bf, prevdup);
895 be16_add_cpu(&prevdup->length, len);
896 *xfs_dir2_data_unused_tag_p(prevdup) =
897 cpu_to_be16((char *)prevdup - (char *)hdr);
898 xfs_dir2_data_log_unused(args, bp, prevdup);
900 * If the previous entry was in the table, the new entry
901 * is longer, so it will be in the table too. Remove
902 * the old one and add the new one.
905 xfs_dir2_data_freeremove(hdr, bf, dfp, needlogp);
906 xfs_dir2_data_freeinsert(hdr, bf, prevdup, needlogp);
909 * Otherwise we need a scan if the new entry is big enough.
912 needscan = be16_to_cpu(prevdup->length) >
913 be16_to_cpu(bf[2].length);
917 * The following entry is free, merge with it.
920 dfp = xfs_dir2_data_freefind(hdr, bf, postdup);
921 newdup = (xfs_dir2_data_unused_t *)((char *)hdr + offset);
922 newdup->freetag = cpu_to_be16(XFS_DIR2_DATA_FREE_TAG);
923 newdup->length = cpu_to_be16(len + be16_to_cpu(postdup->length));
924 *xfs_dir2_data_unused_tag_p(newdup) =
925 cpu_to_be16((char *)newdup - (char *)hdr);
926 xfs_dir2_data_log_unused(args, bp, newdup);
928 * If the following entry was in the table, the new entry
929 * is longer, so it will be in the table too. Remove
930 * the old one and add the new one.
933 xfs_dir2_data_freeremove(hdr, bf, dfp, needlogp);
934 xfs_dir2_data_freeinsert(hdr, bf, newdup, needlogp);
937 * Otherwise we need a scan if the new entry is big enough.
940 needscan = be16_to_cpu(newdup->length) >
941 be16_to_cpu(bf[2].length);
945 * Neither neighbor is free. Make a new entry.
948 newdup = (xfs_dir2_data_unused_t *)((char *)hdr + offset);
949 newdup->freetag = cpu_to_be16(XFS_DIR2_DATA_FREE_TAG);
950 newdup->length = cpu_to_be16(len);
951 *xfs_dir2_data_unused_tag_p(newdup) =
952 cpu_to_be16((char *)newdup - (char *)hdr);
953 xfs_dir2_data_log_unused(args, bp, newdup);
954 xfs_dir2_data_freeinsert(hdr, bf, newdup, needlogp);
956 *needscanp = needscan;
959 /* Check our free data for obvious signs of corruption. */
960 static inline xfs_failaddr_t
961 xfs_dir2_data_check_free(
962 struct xfs_dir2_data_hdr *hdr,
963 struct xfs_dir2_data_unused *dup,
964 xfs_dir2_data_aoff_t offset,
965 xfs_dir2_data_aoff_t len)
967 if (hdr->magic != cpu_to_be32(XFS_DIR2_DATA_MAGIC) &&
968 hdr->magic != cpu_to_be32(XFS_DIR3_DATA_MAGIC) &&
969 hdr->magic != cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) &&
970 hdr->magic != cpu_to_be32(XFS_DIR3_BLOCK_MAGIC))
971 return __this_address;
972 if (be16_to_cpu(dup->freetag) != XFS_DIR2_DATA_FREE_TAG)
973 return __this_address;
974 if (offset < (char *)dup - (char *)hdr)
975 return __this_address;
976 if (offset + len > (char *)dup + be16_to_cpu(dup->length) - (char *)hdr)
977 return __this_address;
978 if ((char *)dup - (char *)hdr !=
979 be16_to_cpu(*xfs_dir2_data_unused_tag_p(dup)))
980 return __this_address;
984 /* Sanity-check a new bestfree entry. */
985 static inline xfs_failaddr_t
986 xfs_dir2_data_check_new_free(
987 struct xfs_dir2_data_hdr *hdr,
988 struct xfs_dir2_data_free *dfp,
989 struct xfs_dir2_data_unused *newdup)
992 return __this_address;
993 if (dfp->length != newdup->length)
994 return __this_address;
995 if (be16_to_cpu(dfp->offset) != (char *)newdup - (char *)hdr)
996 return __this_address;
1001 * Take a byte range out of an existing unused space and make it un-free.
1004 xfs_dir2_data_use_free(
1005 struct xfs_da_args *args,
1007 xfs_dir2_data_unused_t *dup, /* unused entry */
1008 xfs_dir2_data_aoff_t offset, /* starting offset to use */
1009 xfs_dir2_data_aoff_t len, /* length to use */
1010 int *needlogp, /* out: need to log header */
1011 int *needscanp) /* out: need regen bestfree */
1013 xfs_dir2_data_hdr_t *hdr; /* data block header */
1014 xfs_dir2_data_free_t *dfp; /* bestfree pointer */
1015 xfs_dir2_data_unused_t *newdup; /* new unused entry */
1016 xfs_dir2_data_unused_t *newdup2; /* another new unused entry */
1017 struct xfs_dir2_data_free *bf;
1019 int matchback; /* matches end of freespace */
1020 int matchfront; /* matches start of freespace */
1021 int needscan; /* need to regen bestfree */
1022 int oldlen; /* old unused entry's length */
1025 fa = xfs_dir2_data_check_free(hdr, dup, offset, len);
1029 * Look up the entry in the bestfree table.
1031 oldlen = be16_to_cpu(dup->length);
1032 bf = args->dp->d_ops->data_bestfree_p(hdr);
1033 dfp = xfs_dir2_data_freefind(hdr, bf, dup);
1034 ASSERT(dfp || oldlen <= be16_to_cpu(bf[2].length));
1036 * Check for alignment with front and back of the entry.
1038 matchfront = (char *)dup - (char *)hdr == offset;
1039 matchback = (char *)dup + oldlen - (char *)hdr == offset + len;
1040 ASSERT(*needscanp == 0);
1043 * If we matched it exactly we just need to get rid of it from
1044 * the bestfree table.
1046 if (matchfront && matchback) {
1048 needscan = (bf[2].offset != 0);
1050 xfs_dir2_data_freeremove(hdr, bf, dfp,
1055 * We match the first part of the entry.
1056 * Make a new entry with the remaining freespace.
1058 else if (matchfront) {
1059 newdup = (xfs_dir2_data_unused_t *)((char *)hdr + offset + len);
1060 newdup->freetag = cpu_to_be16(XFS_DIR2_DATA_FREE_TAG);
1061 newdup->length = cpu_to_be16(oldlen - len);
1062 *xfs_dir2_data_unused_tag_p(newdup) =
1063 cpu_to_be16((char *)newdup - (char *)hdr);
1064 xfs_dir2_data_log_unused(args, bp, newdup);
1066 * If it was in the table, remove it and add the new one.
1069 xfs_dir2_data_freeremove(hdr, bf, dfp, needlogp);
1070 dfp = xfs_dir2_data_freeinsert(hdr, bf, newdup,
1072 fa = xfs_dir2_data_check_new_free(hdr, dfp, newdup);
1076 * If we got inserted at the last slot,
1077 * that means we don't know if there was a better
1078 * choice for the last slot, or not. Rescan.
1080 needscan = dfp == &bf[2];
1084 * We match the last part of the entry.
1085 * Trim the allocated space off the tail of the entry.
1087 else if (matchback) {
1089 newdup->length = cpu_to_be16(((char *)hdr + offset) - (char *)newdup);
1090 *xfs_dir2_data_unused_tag_p(newdup) =
1091 cpu_to_be16((char *)newdup - (char *)hdr);
1092 xfs_dir2_data_log_unused(args, bp, newdup);
1094 * If it was in the table, remove it and add the new one.
1097 xfs_dir2_data_freeremove(hdr, bf, dfp, needlogp);
1098 dfp = xfs_dir2_data_freeinsert(hdr, bf, newdup,
1100 fa = xfs_dir2_data_check_new_free(hdr, dfp, newdup);
1104 * If we got inserted at the last slot,
1105 * that means we don't know if there was a better
1106 * choice for the last slot, or not. Rescan.
1108 needscan = dfp == &bf[2];
1112 * Poking out the middle of an entry.
1113 * Make two new entries.
1117 newdup->length = cpu_to_be16(((char *)hdr + offset) - (char *)newdup);
1118 *xfs_dir2_data_unused_tag_p(newdup) =
1119 cpu_to_be16((char *)newdup - (char *)hdr);
1120 xfs_dir2_data_log_unused(args, bp, newdup);
1121 newdup2 = (xfs_dir2_data_unused_t *)((char *)hdr + offset + len);
1122 newdup2->freetag = cpu_to_be16(XFS_DIR2_DATA_FREE_TAG);
1123 newdup2->length = cpu_to_be16(oldlen - len - be16_to_cpu(newdup->length));
1124 *xfs_dir2_data_unused_tag_p(newdup2) =
1125 cpu_to_be16((char *)newdup2 - (char *)hdr);
1126 xfs_dir2_data_log_unused(args, bp, newdup2);
1128 * If the old entry was in the table, we need to scan
1129 * if the 3rd entry was valid, since these entries
1130 * are smaller than the old one.
1131 * If we don't need to scan that means there were 1 or 2
1132 * entries in the table, and removing the old and adding
1133 * the 2 new will work.
1136 needscan = (bf[2].length != 0);
1138 xfs_dir2_data_freeremove(hdr, bf, dfp,
1140 xfs_dir2_data_freeinsert(hdr, bf, newdup,
1142 xfs_dir2_data_freeinsert(hdr, bf, newdup2,
1147 *needscanp = needscan;
1150 xfs_corruption_error(__func__, XFS_ERRLEVEL_LOW, args->dp->i_mount,
1151 hdr, sizeof(*hdr), __FILE__, __LINE__, fa);
1152 return -EFSCORRUPTED;
1155 /* Find the end of the entry data in a data/block format dir block. */
1158 struct xfs_da_geometry *geo,
1159 struct xfs_dir2_data_hdr *hdr)
1161 switch (hdr->magic) {
1162 case cpu_to_be32(XFS_DIR3_BLOCK_MAGIC):
1163 case cpu_to_be32(XFS_DIR2_BLOCK_MAGIC):
1164 return xfs_dir2_block_leaf_p(xfs_dir2_block_tail_p(geo, hdr));
1165 case cpu_to_be32(XFS_DIR3_DATA_MAGIC):
1166 case cpu_to_be32(XFS_DIR2_DATA_MAGIC):
1167 return (char *)hdr + geo->blksize;