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_inode.h"
16 #include "xfs_dir2_priv.h"
17 #include "xfs_error.h"
18 #include "xfs_trans.h"
19 #include "xfs_buf_item.h"
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);
27 struct xfs_dir2_data_free *
28 xfs_dir2_data_bestfree_p(
30 struct xfs_dir2_data_hdr *hdr)
33 return ((struct xfs_dir3_data_hdr *)hdr)->best_free;
38 * Pointer to an entry's tag word.
41 xfs_dir2_data_entry_tag_p(
43 struct xfs_dir2_data_entry *dep)
45 return (__be16 *)((char *)dep +
46 xfs_dir2_data_entsize(mp, dep->namelen) - sizeof(__be16));
50 xfs_dir2_data_get_ftype(
52 struct xfs_dir2_data_entry *dep)
54 if (xfs_has_ftype(mp)) {
55 uint8_t ftype = dep->name[dep->namelen];
57 if (likely(ftype < XFS_DIR3_FT_MAX))
61 return XFS_DIR3_FT_UNKNOWN;
65 xfs_dir2_data_put_ftype(
67 struct xfs_dir2_data_entry *dep,
70 ASSERT(ftype < XFS_DIR3_FT_MAX);
71 ASSERT(dep->namelen != 0);
73 if (xfs_has_ftype(mp))
74 dep->name[dep->namelen] = ftype;
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.
83 static inline unsigned int
84 xfs_dir2_data_max_leaf_entries(
85 struct xfs_da_geometry *geo)
87 return (geo->blksize - sizeof(struct xfs_dir2_block_tail) -
88 geo->data_entry_offset) /
89 sizeof(struct xfs_dir2_leaf_entry);
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.
98 __xfs_dir3_data_check(
99 struct xfs_inode *dp, /* incore inode pointer */
100 struct xfs_buf *bp) /* data block's buffer */
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;
118 struct xfs_da_geometry *geo = mp->m_dir_geo;
121 * If this isn't a directory, something is seriously wrong. Bail out.
123 if (dp && !S_ISDIR(VFS_I(dp)->i_mode))
124 return __this_address;
127 offset = geo->data_entry_offset;
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);
135 if (be32_to_cpu(btp->count) >=
136 xfs_dir2_data_max_leaf_entries(geo))
137 return __this_address;
139 case cpu_to_be32(XFS_DIR3_DATA_MAGIC):
140 case cpu_to_be32(XFS_DIR2_DATA_MAGIC):
143 return __this_address;
145 end = xfs_dir3_data_end_offset(geo, hdr);
147 return __this_address;
150 * Account for zero bestfree entries.
152 bf = xfs_dir2_data_bestfree_p(mp, hdr);
153 count = lastfree = freeseen = 0;
156 return __this_address;
161 return __this_address;
166 return __this_address;
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;
175 * Loop over the data/unused entries.
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;
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.
186 if (be16_to_cpu(dup->freetag) == XFS_DIR2_DATA_FREE_TAG) {
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)) !=
195 return __this_address;
196 fa = xfs_dir2_data_freefind_verify(hdr, bf, dup, &dfp);
201 if ((freeseen & (1 << i)) != 0)
202 return __this_address;
205 if (be16_to_cpu(dup->length) >
206 be16_to_cpu(bf[2].length))
207 return __this_address;
209 offset += be16_to_cpu(dup->length);
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.
219 if (dep->namelen == 0)
220 return __this_address;
221 if (!xfs_verify_dir_ino(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;
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)
244 if (i >= be32_to_cpu(btp->count))
245 return __this_address;
247 offset += xfs_dir2_data_entsize(mp, dep->namelen);
250 * Need to have seen all the entries and all the bestfree slots.
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))
260 if (i > 0 && be32_to_cpu(lep[i].hashval) <
261 be32_to_cpu(lep[i - 1].hashval))
262 return __this_address;
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;
275 struct xfs_inode *dp,
280 fa = __xfs_dir3_data_check(dp, bp);
283 xfs_corruption_error(__func__, XFS_ERRLEVEL_LOW, dp->i_mount,
284 bp->b_addr, BBTOB(bp->b_length), __FILE__, __LINE__,
290 static xfs_failaddr_t
291 xfs_dir3_data_verify(
294 struct xfs_mount *mp = bp->b_mount;
295 struct xfs_dir3_blk_hdr *hdr3 = bp->b_addr;
297 if (!xfs_verify_magic(bp, hdr3->magic))
298 return __this_address;
300 if (xfs_has_crc(mp)) {
301 if (!uuid_equal(&hdr3->uuid, &mp->m_sb.sb_meta_uuid))
302 return __this_address;
303 if (be64_to_cpu(hdr3->blkno) != xfs_buf_daddr(bp))
304 return __this_address;
305 if (!xfs_log_check_lsn(mp, be64_to_cpu(hdr3->lsn)))
306 return __this_address;
308 return __xfs_dir3_data_check(NULL, bp);
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.
317 xfs_dir3_data_reada_verify(
320 struct xfs_dir2_data_hdr *hdr = bp->b_addr;
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);
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);
334 xfs_verifier_error(bp, -EFSCORRUPTED, __this_address);
340 xfs_dir3_data_read_verify(
343 struct xfs_mount *mp = bp->b_mount;
346 if (xfs_has_crc(mp) &&
347 !xfs_buf_verify_cksum(bp, XFS_DIR3_DATA_CRC_OFF))
348 xfs_verifier_error(bp, -EFSBADCRC, __this_address);
350 fa = xfs_dir3_data_verify(bp);
352 xfs_verifier_error(bp, -EFSCORRUPTED, fa);
357 xfs_dir3_data_write_verify(
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;
365 fa = xfs_dir3_data_verify(bp);
367 xfs_verifier_error(bp, -EFSCORRUPTED, fa);
371 if (!xfs_has_crc(mp))
375 hdr3->lsn = cpu_to_be64(bip->bli_item.li_lsn);
377 xfs_buf_update_cksum(bp, XFS_DIR3_DATA_CRC_OFF);
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,
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,
397 static xfs_failaddr_t
398 xfs_dir3_data_header_check(
399 struct xfs_inode *dp,
402 struct xfs_mount *mp = dp->i_mount;
404 if (xfs_has_crc(mp)) {
405 struct xfs_dir3_data_hdr *hdr3 = bp->b_addr;
407 if (be64_to_cpu(hdr3->hdr.owner) != dp->i_ino)
408 return __this_address;
416 struct xfs_trans *tp,
417 struct xfs_inode *dp,
420 struct xfs_buf **bpp)
425 err = xfs_da_read_buf(tp, dp, bno, flags, bpp, XFS_DATA_FORK,
426 &xfs_dir3_data_buf_ops);
430 /* Check things that we can't do in the verifier. */
431 fa = xfs_dir3_data_header_check(dp, *bpp);
433 __xfs_buf_mark_corrupt(*bpp, fa);
434 xfs_trans_brelse(tp, *bpp);
436 return -EFSCORRUPTED;
439 xfs_trans_buf_set_type(tp, *bpp, XFS_BLFT_DIR_DATA_BUF);
444 xfs_dir3_data_readahead(
445 struct xfs_inode *dp,
449 return xfs_da_reada_buf(dp, bno, flags, XFS_DATA_FORK,
450 &xfs_dir3_data_reada_buf_ops);
454 * Find the bestfree entry that exactly coincides with unused directory space
455 * or a verifier error because the bestfree data are bad.
457 static xfs_failaddr_t
458 xfs_dir2_data_freefind_verify(
459 struct xfs_dir2_data_hdr *hdr,
460 struct xfs_dir2_data_free *bf,
461 struct xfs_dir2_data_unused *dup,
462 struct xfs_dir2_data_free **bf_ent)
464 struct xfs_dir2_data_free *dfp;
465 xfs_dir2_data_aoff_t off;
466 bool matched = false;
467 bool seenzero = false;
470 off = (xfs_dir2_data_aoff_t)((char *)dup - (char *)hdr);
473 * Validate some consistency in the bestfree table.
474 * Check order, non-overlapping entries, and if we find the
475 * one we're looking for it has to be exact.
477 for (dfp = &bf[0]; dfp < &bf[XFS_DIR2_DATA_FD_COUNT]; dfp++) {
480 return __this_address;
485 return __this_address;
486 if (be16_to_cpu(dfp->offset) == off) {
488 if (dfp->length != dup->length)
489 return __this_address;
490 } else if (be16_to_cpu(dfp->offset) > off) {
491 if (off + be16_to_cpu(dup->length) >
492 be16_to_cpu(dfp->offset))
493 return __this_address;
495 if (be16_to_cpu(dfp->offset) +
496 be16_to_cpu(dfp->length) > off)
497 return __this_address;
500 be16_to_cpu(dfp->length) < be16_to_cpu(dup->length))
501 return __this_address;
503 be16_to_cpu(dfp[-1].length) < be16_to_cpu(dfp[0].length))
504 return __this_address;
507 /* Looks ok so far; now try to match up with a bestfree entry. */
508 *bf_ent = xfs_dir2_data_freefind(hdr, bf, dup);
513 * Given a data block and an unused entry from that block,
514 * return the bestfree entry if any that corresponds to it.
516 xfs_dir2_data_free_t *
517 xfs_dir2_data_freefind(
518 struct xfs_dir2_data_hdr *hdr, /* data block header */
519 struct xfs_dir2_data_free *bf, /* bestfree table pointer */
520 struct xfs_dir2_data_unused *dup) /* unused space */
522 xfs_dir2_data_free_t *dfp; /* bestfree entry */
523 xfs_dir2_data_aoff_t off; /* offset value needed */
525 off = (xfs_dir2_data_aoff_t)((char *)dup - (char *)hdr);
528 * If this is smaller than the smallest bestfree entry,
529 * it can't be there since they're sorted.
531 if (be16_to_cpu(dup->length) <
532 be16_to_cpu(bf[XFS_DIR2_DATA_FD_COUNT - 1].length))
535 * Look at the three bestfree entries for our guy.
537 for (dfp = &bf[0]; dfp < &bf[XFS_DIR2_DATA_FD_COUNT]; dfp++) {
540 if (be16_to_cpu(dfp->offset) == off)
544 * Didn't find it. This only happens if there are duplicate lengths.
550 * Insert an unused-space entry into the bestfree table.
552 xfs_dir2_data_free_t * /* entry inserted */
553 xfs_dir2_data_freeinsert(
554 struct xfs_dir2_data_hdr *hdr, /* data block pointer */
555 struct xfs_dir2_data_free *dfp, /* bestfree table pointer */
556 struct xfs_dir2_data_unused *dup, /* unused space */
557 int *loghead) /* log the data header (out) */
559 xfs_dir2_data_free_t new; /* new bestfree entry */
561 ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
562 hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) ||
563 hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC) ||
564 hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC));
566 new.length = dup->length;
567 new.offset = cpu_to_be16((char *)dup - (char *)hdr);
570 * Insert at position 0, 1, or 2; or not at all.
572 if (be16_to_cpu(new.length) > be16_to_cpu(dfp[0].length)) {
579 if (be16_to_cpu(new.length) > be16_to_cpu(dfp[1].length)) {
585 if (be16_to_cpu(new.length) > be16_to_cpu(dfp[2].length)) {
594 * Remove a bestfree entry from the table.
597 xfs_dir2_data_freeremove(
598 struct xfs_dir2_data_hdr *hdr, /* data block header */
599 struct xfs_dir2_data_free *bf, /* bestfree table pointer */
600 struct xfs_dir2_data_free *dfp, /* bestfree entry pointer */
601 int *loghead) /* out: log data header */
604 ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
605 hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) ||
606 hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC) ||
607 hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC));
610 * It's the first entry, slide the next 2 up.
617 * It's the second entry, slide the 3rd entry up.
619 else if (dfp == &bf[1])
622 * Must be the last entry.
625 ASSERT(dfp == &bf[2]);
627 * Clear the 3rd entry, must be zero now.
635 * Given a data block, reconstruct its bestfree map.
638 xfs_dir2_data_freescan(
639 struct xfs_mount *mp,
640 struct xfs_dir2_data_hdr *hdr,
643 struct xfs_da_geometry *geo = mp->m_dir_geo;
644 struct xfs_dir2_data_free *bf = xfs_dir2_data_bestfree_p(mp, hdr);
646 unsigned int offset = geo->data_entry_offset;
649 ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
650 hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC) ||
651 hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) ||
652 hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC));
655 * Start by clearing the table.
657 memset(bf, 0, sizeof(*bf) * XFS_DIR2_DATA_FD_COUNT);
660 end = xfs_dir3_data_end_offset(geo, addr);
661 while (offset < end) {
662 struct xfs_dir2_data_unused *dup = addr + offset;
663 struct xfs_dir2_data_entry *dep = addr + offset;
666 * If it's a free entry, insert it.
668 if (be16_to_cpu(dup->freetag) == XFS_DIR2_DATA_FREE_TAG) {
670 be16_to_cpu(*xfs_dir2_data_unused_tag_p(dup)));
671 xfs_dir2_data_freeinsert(hdr, bf, dup, loghead);
672 offset += be16_to_cpu(dup->length);
677 * For active entries, check their tags and skip them.
680 be16_to_cpu(*xfs_dir2_data_entry_tag_p(mp, dep)));
681 offset += xfs_dir2_data_entsize(mp, dep->namelen);
686 * Initialize a data block at the given block number in the directory.
687 * Give back the buffer for the created block.
691 struct xfs_da_args *args, /* directory operation args */
692 xfs_dir2_db_t blkno, /* logical dir block number */
693 struct xfs_buf **bpp) /* output block buffer */
695 struct xfs_trans *tp = args->trans;
696 struct xfs_inode *dp = args->dp;
697 struct xfs_mount *mp = dp->i_mount;
698 struct xfs_da_geometry *geo = args->geo;
700 struct xfs_dir2_data_hdr *hdr;
701 struct xfs_dir2_data_unused *dup;
702 struct xfs_dir2_data_free *bf;
707 * Get the buffer set up for the block.
709 error = xfs_da_get_buf(tp, dp, xfs_dir2_db_to_da(args->geo, blkno),
713 bp->b_ops = &xfs_dir3_data_buf_ops;
714 xfs_trans_buf_set_type(tp, bp, XFS_BLFT_DIR_DATA_BUF);
717 * Initialize the header.
720 if (xfs_has_crc(mp)) {
721 struct xfs_dir3_blk_hdr *hdr3 = bp->b_addr;
723 memset(hdr3, 0, sizeof(*hdr3));
724 hdr3->magic = cpu_to_be32(XFS_DIR3_DATA_MAGIC);
725 hdr3->blkno = cpu_to_be64(xfs_buf_daddr(bp));
726 hdr3->owner = cpu_to_be64(dp->i_ino);
727 uuid_copy(&hdr3->uuid, &mp->m_sb.sb_meta_uuid);
730 hdr->magic = cpu_to_be32(XFS_DIR2_DATA_MAGIC);
732 bf = xfs_dir2_data_bestfree_p(mp, hdr);
733 bf[0].offset = cpu_to_be16(geo->data_entry_offset);
734 bf[0].length = cpu_to_be16(geo->blksize - geo->data_entry_offset);
735 for (i = 1; i < XFS_DIR2_DATA_FD_COUNT; i++) {
741 * Set up an unused entry for the block's body.
743 dup = bp->b_addr + geo->data_entry_offset;
744 dup->freetag = cpu_to_be16(XFS_DIR2_DATA_FREE_TAG);
745 dup->length = bf[0].length;
746 *xfs_dir2_data_unused_tag_p(dup) = cpu_to_be16((char *)dup - (char *)hdr);
749 * Log it and return it.
751 xfs_dir2_data_log_header(args, bp);
752 xfs_dir2_data_log_unused(args, bp, dup);
758 * Log an active data entry from the block.
761 xfs_dir2_data_log_entry(
762 struct xfs_da_args *args,
764 xfs_dir2_data_entry_t *dep) /* data entry pointer */
766 struct xfs_mount *mp = bp->b_mount;
767 struct xfs_dir2_data_hdr *hdr = bp->b_addr;
769 ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
770 hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC) ||
771 hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) ||
772 hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC));
774 xfs_trans_log_buf(args->trans, bp, (uint)((char *)dep - (char *)hdr),
775 (uint)((char *)(xfs_dir2_data_entry_tag_p(mp, dep) + 1) -
780 * Log a data block header.
783 xfs_dir2_data_log_header(
784 struct xfs_da_args *args,
788 struct xfs_dir2_data_hdr *hdr = bp->b_addr;
790 ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
791 hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC) ||
792 hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) ||
793 hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC));
796 xfs_trans_log_buf(args->trans, bp, 0, args->geo->data_entry_offset - 1);
800 * Log a data unused entry.
803 xfs_dir2_data_log_unused(
804 struct xfs_da_args *args,
806 xfs_dir2_data_unused_t *dup) /* data unused pointer */
808 xfs_dir2_data_hdr_t *hdr = bp->b_addr;
810 ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
811 hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC) ||
812 hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) ||
813 hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC));
816 * Log the first part of the unused entry.
818 xfs_trans_log_buf(args->trans, bp, (uint)((char *)dup - (char *)hdr),
819 (uint)((char *)&dup->length + sizeof(dup->length) -
822 * Log the end (tag) of the unused entry.
824 xfs_trans_log_buf(args->trans, bp,
825 (uint)((char *)xfs_dir2_data_unused_tag_p(dup) - (char *)hdr),
826 (uint)((char *)xfs_dir2_data_unused_tag_p(dup) - (char *)hdr +
827 sizeof(xfs_dir2_data_off_t) - 1));
831 * Make a byte range in the data block unused.
832 * Its current contents are unimportant.
835 xfs_dir2_data_make_free(
836 struct xfs_da_args *args,
838 xfs_dir2_data_aoff_t offset, /* starting byte offset */
839 xfs_dir2_data_aoff_t len, /* length in bytes */
840 int *needlogp, /* out: log header */
841 int *needscanp) /* out: regen bestfree */
843 xfs_dir2_data_hdr_t *hdr; /* data block pointer */
844 xfs_dir2_data_free_t *dfp; /* bestfree pointer */
845 int needscan; /* need to regen bestfree */
846 xfs_dir2_data_unused_t *newdup; /* new unused entry */
847 xfs_dir2_data_unused_t *postdup; /* unused entry after us */
848 xfs_dir2_data_unused_t *prevdup; /* unused entry before us */
850 struct xfs_dir2_data_free *bf;
855 * Figure out where the end of the data area is.
857 end = xfs_dir3_data_end_offset(args->geo, hdr);
861 * If this isn't the start of the block, then back up to
862 * the previous entry and see if it's free.
864 if (offset > args->geo->data_entry_offset) {
865 __be16 *tagp; /* tag just before us */
867 tagp = (__be16 *)((char *)hdr + offset) - 1;
868 prevdup = (xfs_dir2_data_unused_t *)((char *)hdr + be16_to_cpu(*tagp));
869 if (be16_to_cpu(prevdup->freetag) != XFS_DIR2_DATA_FREE_TAG)
874 * If this isn't the end of the block, see if the entry after
877 if (offset + len < end) {
879 (xfs_dir2_data_unused_t *)((char *)hdr + offset + len);
880 if (be16_to_cpu(postdup->freetag) != XFS_DIR2_DATA_FREE_TAG)
884 ASSERT(*needscanp == 0);
887 * Previous and following entries are both free,
888 * merge everything into a single free entry.
890 bf = xfs_dir2_data_bestfree_p(args->dp->i_mount, hdr);
891 if (prevdup && postdup) {
892 xfs_dir2_data_free_t *dfp2; /* another bestfree pointer */
895 * See if prevdup and/or postdup are in bestfree table.
897 dfp = xfs_dir2_data_freefind(hdr, bf, prevdup);
898 dfp2 = xfs_dir2_data_freefind(hdr, bf, postdup);
900 * We need a rescan unless there are exactly 2 free entries
901 * namely our two. Then we know what's happening, otherwise
902 * since the third bestfree is there, there might be more
905 needscan = (bf[2].length != 0);
907 * Fix up the new big freespace.
909 be16_add_cpu(&prevdup->length, len + be16_to_cpu(postdup->length));
910 *xfs_dir2_data_unused_tag_p(prevdup) =
911 cpu_to_be16((char *)prevdup - (char *)hdr);
912 xfs_dir2_data_log_unused(args, bp, prevdup);
915 * Has to be the case that entries 0 and 1 are
916 * dfp and dfp2 (don't know which is which), and
918 * Remove entry 1 first then entry 0.
926 xfs_dir2_data_freeremove(hdr, bf, dfp2, needlogp);
927 xfs_dir2_data_freeremove(hdr, bf, dfp, needlogp);
929 * Now insert the new entry.
931 dfp = xfs_dir2_data_freeinsert(hdr, bf, prevdup,
933 ASSERT(dfp == &bf[0]);
934 ASSERT(dfp->length == prevdup->length);
935 ASSERT(!dfp[1].length);
936 ASSERT(!dfp[2].length);
940 * The entry before us is free, merge with it.
943 dfp = xfs_dir2_data_freefind(hdr, bf, prevdup);
944 be16_add_cpu(&prevdup->length, len);
945 *xfs_dir2_data_unused_tag_p(prevdup) =
946 cpu_to_be16((char *)prevdup - (char *)hdr);
947 xfs_dir2_data_log_unused(args, bp, prevdup);
949 * If the previous 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.
954 xfs_dir2_data_freeremove(hdr, bf, dfp, needlogp);
955 xfs_dir2_data_freeinsert(hdr, bf, prevdup, needlogp);
958 * Otherwise we need a scan if the new entry is big enough.
961 needscan = be16_to_cpu(prevdup->length) >
962 be16_to_cpu(bf[2].length);
966 * The following entry is free, merge with it.
969 dfp = xfs_dir2_data_freefind(hdr, bf, postdup);
970 newdup = (xfs_dir2_data_unused_t *)((char *)hdr + offset);
971 newdup->freetag = cpu_to_be16(XFS_DIR2_DATA_FREE_TAG);
972 newdup->length = cpu_to_be16(len + be16_to_cpu(postdup->length));
973 *xfs_dir2_data_unused_tag_p(newdup) =
974 cpu_to_be16((char *)newdup - (char *)hdr);
975 xfs_dir2_data_log_unused(args, bp, newdup);
977 * If the following entry was in the table, the new entry
978 * is longer, so it will be in the table too. Remove
979 * the old one and add the new one.
982 xfs_dir2_data_freeremove(hdr, bf, dfp, needlogp);
983 xfs_dir2_data_freeinsert(hdr, bf, newdup, needlogp);
986 * Otherwise we need a scan if the new entry is big enough.
989 needscan = be16_to_cpu(newdup->length) >
990 be16_to_cpu(bf[2].length);
994 * Neither neighbor is free. Make a new entry.
997 newdup = (xfs_dir2_data_unused_t *)((char *)hdr + offset);
998 newdup->freetag = cpu_to_be16(XFS_DIR2_DATA_FREE_TAG);
999 newdup->length = cpu_to_be16(len);
1000 *xfs_dir2_data_unused_tag_p(newdup) =
1001 cpu_to_be16((char *)newdup - (char *)hdr);
1002 xfs_dir2_data_log_unused(args, bp, newdup);
1003 xfs_dir2_data_freeinsert(hdr, bf, newdup, needlogp);
1005 *needscanp = needscan;
1008 /* Check our free data for obvious signs of corruption. */
1009 static inline xfs_failaddr_t
1010 xfs_dir2_data_check_free(
1011 struct xfs_dir2_data_hdr *hdr,
1012 struct xfs_dir2_data_unused *dup,
1013 xfs_dir2_data_aoff_t offset,
1014 xfs_dir2_data_aoff_t len)
1016 if (hdr->magic != cpu_to_be32(XFS_DIR2_DATA_MAGIC) &&
1017 hdr->magic != cpu_to_be32(XFS_DIR3_DATA_MAGIC) &&
1018 hdr->magic != cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) &&
1019 hdr->magic != cpu_to_be32(XFS_DIR3_BLOCK_MAGIC))
1020 return __this_address;
1021 if (be16_to_cpu(dup->freetag) != XFS_DIR2_DATA_FREE_TAG)
1022 return __this_address;
1023 if (offset < (char *)dup - (char *)hdr)
1024 return __this_address;
1025 if (offset + len > (char *)dup + be16_to_cpu(dup->length) - (char *)hdr)
1026 return __this_address;
1027 if ((char *)dup - (char *)hdr !=
1028 be16_to_cpu(*xfs_dir2_data_unused_tag_p(dup)))
1029 return __this_address;
1033 /* Sanity-check a new bestfree entry. */
1034 static inline xfs_failaddr_t
1035 xfs_dir2_data_check_new_free(
1036 struct xfs_dir2_data_hdr *hdr,
1037 struct xfs_dir2_data_free *dfp,
1038 struct xfs_dir2_data_unused *newdup)
1041 return __this_address;
1042 if (dfp->length != newdup->length)
1043 return __this_address;
1044 if (be16_to_cpu(dfp->offset) != (char *)newdup - (char *)hdr)
1045 return __this_address;
1050 * Take a byte range out of an existing unused space and make it un-free.
1053 xfs_dir2_data_use_free(
1054 struct xfs_da_args *args,
1056 xfs_dir2_data_unused_t *dup, /* unused entry */
1057 xfs_dir2_data_aoff_t offset, /* starting offset to use */
1058 xfs_dir2_data_aoff_t len, /* length to use */
1059 int *needlogp, /* out: need to log header */
1060 int *needscanp) /* out: need regen bestfree */
1062 xfs_dir2_data_hdr_t *hdr; /* data block header */
1063 xfs_dir2_data_free_t *dfp; /* bestfree pointer */
1064 xfs_dir2_data_unused_t *newdup; /* new unused entry */
1065 xfs_dir2_data_unused_t *newdup2; /* another new unused entry */
1066 struct xfs_dir2_data_free *bf;
1068 int matchback; /* matches end of freespace */
1069 int matchfront; /* matches start of freespace */
1070 int needscan; /* need to regen bestfree */
1071 int oldlen; /* old unused entry's length */
1074 fa = xfs_dir2_data_check_free(hdr, dup, offset, len);
1078 * Look up the entry in the bestfree table.
1080 oldlen = be16_to_cpu(dup->length);
1081 bf = xfs_dir2_data_bestfree_p(args->dp->i_mount, hdr);
1082 dfp = xfs_dir2_data_freefind(hdr, bf, dup);
1083 ASSERT(dfp || oldlen <= be16_to_cpu(bf[2].length));
1085 * Check for alignment with front and back of the entry.
1087 matchfront = (char *)dup - (char *)hdr == offset;
1088 matchback = (char *)dup + oldlen - (char *)hdr == offset + len;
1089 ASSERT(*needscanp == 0);
1092 * If we matched it exactly we just need to get rid of it from
1093 * the bestfree table.
1095 if (matchfront && matchback) {
1097 needscan = (bf[2].offset != 0);
1099 xfs_dir2_data_freeremove(hdr, bf, dfp,
1104 * We match the first part of the entry.
1105 * Make a new entry with the remaining freespace.
1107 else if (matchfront) {
1108 newdup = (xfs_dir2_data_unused_t *)((char *)hdr + offset + len);
1109 newdup->freetag = cpu_to_be16(XFS_DIR2_DATA_FREE_TAG);
1110 newdup->length = cpu_to_be16(oldlen - len);
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);
1115 * If it was in the table, remove it and add the new one.
1118 xfs_dir2_data_freeremove(hdr, bf, dfp, needlogp);
1119 dfp = xfs_dir2_data_freeinsert(hdr, bf, newdup,
1121 fa = xfs_dir2_data_check_new_free(hdr, dfp, newdup);
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.
1129 needscan = dfp == &bf[2];
1133 * We match the last part of the entry.
1134 * Trim the allocated space off the tail of the entry.
1136 else if (matchback) {
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);
1143 * If it was in the table, remove it and add the new one.
1146 xfs_dir2_data_freeremove(hdr, bf, dfp, needlogp);
1147 dfp = xfs_dir2_data_freeinsert(hdr, bf, newdup,
1149 fa = xfs_dir2_data_check_new_free(hdr, dfp, newdup);
1153 * If we got inserted at the last slot,
1154 * that means we don't know if there was a better
1155 * choice for the last slot, or not. Rescan.
1157 needscan = dfp == &bf[2];
1161 * Poking out the middle of an entry.
1162 * Make two new entries.
1166 newdup->length = cpu_to_be16(((char *)hdr + offset) - (char *)newdup);
1167 *xfs_dir2_data_unused_tag_p(newdup) =
1168 cpu_to_be16((char *)newdup - (char *)hdr);
1169 xfs_dir2_data_log_unused(args, bp, newdup);
1170 newdup2 = (xfs_dir2_data_unused_t *)((char *)hdr + offset + len);
1171 newdup2->freetag = cpu_to_be16(XFS_DIR2_DATA_FREE_TAG);
1172 newdup2->length = cpu_to_be16(oldlen - len - be16_to_cpu(newdup->length));
1173 *xfs_dir2_data_unused_tag_p(newdup2) =
1174 cpu_to_be16((char *)newdup2 - (char *)hdr);
1175 xfs_dir2_data_log_unused(args, bp, newdup2);
1177 * If the old entry was in the table, we need to scan
1178 * if the 3rd entry was valid, since these entries
1179 * are smaller than the old one.
1180 * If we don't need to scan that means there were 1 or 2
1181 * entries in the table, and removing the old and adding
1182 * the 2 new will work.
1185 needscan = (bf[2].length != 0);
1187 xfs_dir2_data_freeremove(hdr, bf, dfp,
1189 xfs_dir2_data_freeinsert(hdr, bf, newdup,
1191 xfs_dir2_data_freeinsert(hdr, bf, newdup2,
1196 *needscanp = needscan;
1199 xfs_corruption_error(__func__, XFS_ERRLEVEL_LOW, args->dp->i_mount,
1200 hdr, sizeof(*hdr), __FILE__, __LINE__, fa);
1201 return -EFSCORRUPTED;
1204 /* Find the end of the entry data in a data/block format dir block. */
1206 xfs_dir3_data_end_offset(
1207 struct xfs_da_geometry *geo,
1208 struct xfs_dir2_data_hdr *hdr)
1212 switch (hdr->magic) {
1213 case cpu_to_be32(XFS_DIR3_BLOCK_MAGIC):
1214 case cpu_to_be32(XFS_DIR2_BLOCK_MAGIC):
1215 p = xfs_dir2_block_leaf_p(xfs_dir2_block_tail_p(geo, hdr));
1216 return p - (void *)hdr;
1217 case cpu_to_be32(XFS_DIR3_DATA_MAGIC):
1218 case cpu_to_be32(XFS_DIR2_DATA_MAGIC):
1219 return geo->blksize;