2 * Copyright (c) 2000-2002,2005 Silicon Graphics, Inc.
3 * Copyright (c) 2013 Red Hat, Inc.
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as
8 * published by the Free Software Foundation.
10 * This program is distributed in the hope that it would be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21 #include "xfs_format.h"
22 #include "xfs_log_format.h"
23 #include "xfs_trans_resv.h"
26 #include "xfs_mount.h"
27 #include "xfs_da_format.h"
28 #include "xfs_da_btree.h"
29 #include "xfs_inode.h"
31 #include "xfs_dir2_priv.h"
32 #include "xfs_error.h"
33 #include "xfs_trans.h"
34 #include "xfs_buf_item.h"
35 #include "xfs_cksum.h"
38 * Check the consistency of the data block.
39 * The input can also be a block-format directory.
40 * Return 0 is the buffer is good, otherwise an error.
43 __xfs_dir3_data_check(
44 struct xfs_inode *dp, /* incore inode pointer */
45 struct xfs_buf *bp) /* data block's buffer */
47 xfs_dir2_dataptr_t addr; /* addr for leaf lookup */
48 xfs_dir2_data_free_t *bf; /* bestfree table */
49 xfs_dir2_block_tail_t *btp=NULL; /* block tail */
50 int count; /* count of entries found */
51 xfs_dir2_data_hdr_t *hdr; /* data block header */
52 xfs_dir2_data_entry_t *dep; /* data entry */
53 xfs_dir2_data_free_t *dfp; /* bestfree entry */
54 xfs_dir2_data_unused_t *dup; /* unused entry */
55 char *endp; /* end of useful data */
56 int freeseen; /* mask of bestfrees seen */
57 xfs_dahash_t hash; /* hash of current name */
58 int i; /* leaf index */
59 int lastfree; /* last entry was unused */
60 xfs_dir2_leaf_entry_t *lep=NULL; /* block leaf entries */
61 xfs_mount_t *mp; /* filesystem mount point */
62 char *p; /* current data position */
63 int stale; /* count of stale leaves */
65 const struct xfs_dir_ops *ops;
67 mp = bp->b_target->bt_mount;
71 * We can be passed a null dp here from a verifier, so we need to go the
72 * hard way to get them.
74 ops = xfs_dir_get_ops(mp, dp);
77 case cpu_to_be32(XFS_DIR3_BLOCK_MAGIC):
78 case cpu_to_be32(XFS_DIR2_BLOCK_MAGIC):
79 btp = xfs_dir2_block_tail_p(mp, hdr);
80 lep = xfs_dir2_block_leaf_p(btp);
83 case cpu_to_be32(XFS_DIR3_DATA_MAGIC):
84 case cpu_to_be32(XFS_DIR2_DATA_MAGIC):
85 endp = (char *)hdr + mp->m_dirblksize;
88 XFS_ERROR_REPORT("Bad Magic", XFS_ERRLEVEL_LOW, mp);
91 bf = ops->data_bestfree_p(hdr);
92 p = (char *)ops->data_entry_p(hdr);
94 count = lastfree = freeseen = 0;
96 * Account for zero bestfree entries.
99 XFS_WANT_CORRUPTED_RETURN(!bf[0].offset);
103 XFS_WANT_CORRUPTED_RETURN(!bf[1].offset);
107 XFS_WANT_CORRUPTED_RETURN(!bf[2].offset);
111 XFS_WANT_CORRUPTED_RETURN(be16_to_cpu(bf[0].length) >=
112 be16_to_cpu(bf[1].length));
113 XFS_WANT_CORRUPTED_RETURN(be16_to_cpu(bf[1].length) >=
114 be16_to_cpu(bf[2].length));
116 * Loop over the data/unused entries.
119 dup = (xfs_dir2_data_unused_t *)p;
121 * If it's unused, look for the space in the bestfree table.
122 * If we find it, account for that, else make sure it
123 * doesn't need to be there.
125 if (be16_to_cpu(dup->freetag) == XFS_DIR2_DATA_FREE_TAG) {
126 XFS_WANT_CORRUPTED_RETURN(lastfree == 0);
127 XFS_WANT_CORRUPTED_RETURN(
128 be16_to_cpu(*xfs_dir2_data_unused_tag_p(dup)) ==
129 (char *)dup - (char *)hdr);
130 dfp = xfs_dir2_data_freefind(hdr, bf, dup);
133 XFS_WANT_CORRUPTED_RETURN(
134 (freeseen & (1 << i)) == 0);
137 XFS_WANT_CORRUPTED_RETURN(
138 be16_to_cpu(dup->length) <=
139 be16_to_cpu(bf[2].length));
141 p += be16_to_cpu(dup->length);
146 * It's a real entry. Validate the fields.
147 * If this is a block directory then make sure it's
148 * in the leaf section of the block.
149 * The linear search is crude but this is DEBUG code.
151 dep = (xfs_dir2_data_entry_t *)p;
152 XFS_WANT_CORRUPTED_RETURN(dep->namelen != 0);
153 XFS_WANT_CORRUPTED_RETURN(
154 !xfs_dir_ino_validate(mp, be64_to_cpu(dep->inumber)));
155 XFS_WANT_CORRUPTED_RETURN(
156 be16_to_cpu(*ops->data_entry_tag_p(dep)) ==
157 (char *)dep - (char *)hdr);
158 XFS_WANT_CORRUPTED_RETURN(
159 ops->data_get_ftype(dep) < XFS_DIR3_FT_MAX);
162 if (hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) ||
163 hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC)) {
164 addr = xfs_dir2_db_off_to_dataptr(mp, mp->m_dirdatablk,
165 (xfs_dir2_data_aoff_t)
166 ((char *)dep - (char *)hdr));
167 name.name = dep->name;
168 name.len = dep->namelen;
169 hash = mp->m_dirnameops->hashname(&name);
170 for (i = 0; i < be32_to_cpu(btp->count); i++) {
171 if (be32_to_cpu(lep[i].address) == addr &&
172 be32_to_cpu(lep[i].hashval) == hash)
175 XFS_WANT_CORRUPTED_RETURN(i < be32_to_cpu(btp->count));
177 p += ops->data_entsize(dep->namelen);
180 * Need to have seen all the entries and all the bestfree slots.
182 XFS_WANT_CORRUPTED_RETURN(freeseen == 7);
183 if (hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) ||
184 hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC)) {
185 for (i = stale = 0; i < be32_to_cpu(btp->count); i++) {
186 if (lep[i].address ==
187 cpu_to_be32(XFS_DIR2_NULL_DATAPTR))
190 XFS_WANT_CORRUPTED_RETURN(
191 be32_to_cpu(lep[i].hashval) >=
192 be32_to_cpu(lep[i - 1].hashval));
194 XFS_WANT_CORRUPTED_RETURN(count ==
195 be32_to_cpu(btp->count) - be32_to_cpu(btp->stale));
196 XFS_WANT_CORRUPTED_RETURN(stale == be32_to_cpu(btp->stale));
202 xfs_dir3_data_verify(
205 struct xfs_mount *mp = bp->b_target->bt_mount;
206 struct xfs_dir3_blk_hdr *hdr3 = bp->b_addr;
208 if (xfs_sb_version_hascrc(&mp->m_sb)) {
209 if (hdr3->magic != cpu_to_be32(XFS_DIR3_DATA_MAGIC))
211 if (!uuid_equal(&hdr3->uuid, &mp->m_sb.sb_uuid))
213 if (be64_to_cpu(hdr3->blkno) != bp->b_bn)
216 if (hdr3->magic != cpu_to_be32(XFS_DIR2_DATA_MAGIC))
219 if (__xfs_dir3_data_check(NULL, bp))
225 * Readahead of the first block of the directory when it is opened is completely
226 * oblivious to the format of the directory. Hence we can either get a block
227 * format buffer or a data format buffer on readahead.
230 xfs_dir3_data_reada_verify(
233 struct xfs_mount *mp = bp->b_target->bt_mount;
234 struct xfs_dir2_data_hdr *hdr = bp->b_addr;
236 switch (hdr->magic) {
237 case cpu_to_be32(XFS_DIR2_BLOCK_MAGIC):
238 case cpu_to_be32(XFS_DIR3_BLOCK_MAGIC):
239 bp->b_ops = &xfs_dir3_block_buf_ops;
240 bp->b_ops->verify_read(bp);
242 case cpu_to_be32(XFS_DIR2_DATA_MAGIC):
243 case cpu_to_be32(XFS_DIR3_DATA_MAGIC):
244 xfs_dir3_data_verify(bp);
247 XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp, hdr);
248 xfs_buf_ioerror(bp, EFSCORRUPTED);
254 xfs_dir3_data_read_verify(
257 struct xfs_mount *mp = bp->b_target->bt_mount;
259 if ((xfs_sb_version_hascrc(&mp->m_sb) &&
260 !xfs_verify_cksum(bp->b_addr, BBTOB(bp->b_length),
261 XFS_DIR3_DATA_CRC_OFF)) ||
262 !xfs_dir3_data_verify(bp)) {
263 XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp, bp->b_addr);
264 xfs_buf_ioerror(bp, EFSCORRUPTED);
269 xfs_dir3_data_write_verify(
272 struct xfs_mount *mp = bp->b_target->bt_mount;
273 struct xfs_buf_log_item *bip = bp->b_fspriv;
274 struct xfs_dir3_blk_hdr *hdr3 = bp->b_addr;
276 if (!xfs_dir3_data_verify(bp)) {
277 XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp, bp->b_addr);
278 xfs_buf_ioerror(bp, EFSCORRUPTED);
282 if (!xfs_sb_version_hascrc(&mp->m_sb))
286 hdr3->lsn = cpu_to_be64(bip->bli_item.li_lsn);
288 xfs_update_cksum(bp->b_addr, BBTOB(bp->b_length), XFS_DIR3_DATA_CRC_OFF);
291 const struct xfs_buf_ops xfs_dir3_data_buf_ops = {
292 .verify_read = xfs_dir3_data_read_verify,
293 .verify_write = xfs_dir3_data_write_verify,
296 static const struct xfs_buf_ops xfs_dir3_data_reada_buf_ops = {
297 .verify_read = xfs_dir3_data_reada_verify,
298 .verify_write = xfs_dir3_data_write_verify,
304 struct xfs_trans *tp,
305 struct xfs_inode *dp,
307 xfs_daddr_t mapped_bno,
308 struct xfs_buf **bpp)
312 err = xfs_da_read_buf(tp, dp, bno, mapped_bno, bpp,
313 XFS_DATA_FORK, &xfs_dir3_data_buf_ops);
315 xfs_trans_buf_set_type(tp, *bpp, XFS_BLFT_DIR_DATA_BUF);
320 xfs_dir3_data_readahead(
321 struct xfs_trans *tp,
322 struct xfs_inode *dp,
324 xfs_daddr_t mapped_bno)
326 return xfs_da_reada_buf(tp, dp, bno, mapped_bno,
327 XFS_DATA_FORK, &xfs_dir3_data_reada_buf_ops);
331 * Given a data block and an unused entry from that block,
332 * return the bestfree entry if any that corresponds to it.
334 xfs_dir2_data_free_t *
335 xfs_dir2_data_freefind(
336 struct xfs_dir2_data_hdr *hdr, /* data block header */
337 struct xfs_dir2_data_free *bf, /* bestfree table pointer */
338 struct xfs_dir2_data_unused *dup) /* unused space */
340 xfs_dir2_data_free_t *dfp; /* bestfree entry */
341 xfs_dir2_data_aoff_t off; /* offset value needed */
343 int matched; /* matched the value */
344 int seenzero; /* saw a 0 bestfree entry */
347 off = (xfs_dir2_data_aoff_t)((char *)dup - (char *)hdr);
351 * Validate some consistency in the bestfree table.
352 * Check order, non-overlapping entries, and if we find the
353 * one we're looking for it has to be exact.
355 ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
356 hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC) ||
357 hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) ||
358 hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC));
359 for (dfp = &bf[0], seenzero = matched = 0;
360 dfp < &bf[XFS_DIR2_DATA_FD_COUNT];
363 ASSERT(!dfp->length);
367 ASSERT(seenzero == 0);
368 if (be16_to_cpu(dfp->offset) == off) {
370 ASSERT(dfp->length == dup->length);
371 } else if (off < be16_to_cpu(dfp->offset))
372 ASSERT(off + be16_to_cpu(dup->length) <= be16_to_cpu(dfp->offset));
374 ASSERT(be16_to_cpu(dfp->offset) + be16_to_cpu(dfp->length) <= off);
375 ASSERT(matched || be16_to_cpu(dfp->length) >= be16_to_cpu(dup->length));
377 ASSERT(be16_to_cpu(dfp[-1].length) >= be16_to_cpu(dfp[0].length));
381 * If this is smaller than the smallest bestfree entry,
382 * it can't be there since they're sorted.
384 if (be16_to_cpu(dup->length) <
385 be16_to_cpu(bf[XFS_DIR2_DATA_FD_COUNT - 1].length))
388 * Look at the three bestfree entries for our guy.
390 for (dfp = &bf[0]; dfp < &bf[XFS_DIR2_DATA_FD_COUNT]; dfp++) {
393 if (be16_to_cpu(dfp->offset) == off)
397 * Didn't find it. This only happens if there are duplicate lengths.
403 * Insert an unused-space entry into the bestfree table.
405 xfs_dir2_data_free_t * /* entry inserted */
406 xfs_dir2_data_freeinsert(
407 struct xfs_dir2_data_hdr *hdr, /* data block pointer */
408 struct xfs_dir2_data_free *dfp, /* bestfree table pointer */
409 struct xfs_dir2_data_unused *dup, /* unused space */
410 int *loghead) /* log the data header (out) */
412 xfs_dir2_data_free_t new; /* new bestfree entry */
414 ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
415 hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) ||
416 hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC) ||
417 hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC));
419 new.length = dup->length;
420 new.offset = cpu_to_be16((char *)dup - (char *)hdr);
423 * Insert at position 0, 1, or 2; or not at all.
425 if (be16_to_cpu(new.length) > be16_to_cpu(dfp[0].length)) {
432 if (be16_to_cpu(new.length) > be16_to_cpu(dfp[1].length)) {
438 if (be16_to_cpu(new.length) > be16_to_cpu(dfp[2].length)) {
447 * Remove a bestfree entry from the table.
450 xfs_dir2_data_freeremove(
451 struct xfs_dir2_data_hdr *hdr, /* data block header */
452 struct xfs_dir2_data_free *bf, /* bestfree table pointer */
453 struct xfs_dir2_data_free *dfp, /* bestfree entry pointer */
454 int *loghead) /* out: log data header */
457 ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
458 hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) ||
459 hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC) ||
460 hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC));
463 * It's the first entry, slide the next 2 up.
470 * It's the second entry, slide the 3rd entry up.
472 else if (dfp == &bf[1])
475 * Must be the last entry.
478 ASSERT(dfp == &bf[2]);
480 * Clear the 3rd entry, must be zero now.
488 * Given a data block, reconstruct its bestfree map.
491 xfs_dir2_data_freescan(
492 struct xfs_inode *dp,
493 struct xfs_dir2_data_hdr *hdr,
496 xfs_dir2_block_tail_t *btp; /* block tail */
497 xfs_dir2_data_entry_t *dep; /* active data entry */
498 xfs_dir2_data_unused_t *dup; /* unused data entry */
499 struct xfs_dir2_data_free *bf;
500 char *endp; /* end of block's data */
501 char *p; /* current entry pointer */
503 ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
504 hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC) ||
505 hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) ||
506 hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC));
509 * Start by clearing the table.
511 bf = dp->d_ops->data_bestfree_p(hdr);
512 memset(bf, 0, sizeof(*bf) * XFS_DIR2_DATA_FD_COUNT);
517 p = (char *)dp->d_ops->data_entry_p(hdr);
518 if (hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) ||
519 hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC)) {
520 btp = xfs_dir2_block_tail_p(dp->i_mount, hdr);
521 endp = (char *)xfs_dir2_block_leaf_p(btp);
523 endp = (char *)hdr + dp->i_mount->m_dirblksize;
525 * Loop over the block's entries.
528 dup = (xfs_dir2_data_unused_t *)p;
530 * If it's a free entry, insert it.
532 if (be16_to_cpu(dup->freetag) == XFS_DIR2_DATA_FREE_TAG) {
533 ASSERT((char *)dup - (char *)hdr ==
534 be16_to_cpu(*xfs_dir2_data_unused_tag_p(dup)));
535 xfs_dir2_data_freeinsert(hdr, bf, dup, loghead);
536 p += be16_to_cpu(dup->length);
539 * For active entries, check their tags and skip them.
542 dep = (xfs_dir2_data_entry_t *)p;
543 ASSERT((char *)dep - (char *)hdr ==
544 be16_to_cpu(*dp->d_ops->data_entry_tag_p(dep)));
545 p += dp->d_ops->data_entsize(dep->namelen);
551 * Initialize a data block at the given block number in the directory.
552 * Give back the buffer for the created block.
556 xfs_da_args_t *args, /* directory operation args */
557 xfs_dir2_db_t blkno, /* logical dir block number */
558 struct xfs_buf **bpp) /* output block buffer */
560 struct xfs_buf *bp; /* block buffer */
561 xfs_dir2_data_hdr_t *hdr; /* data block header */
562 xfs_inode_t *dp; /* incore directory inode */
563 xfs_dir2_data_unused_t *dup; /* unused entry pointer */
564 struct xfs_dir2_data_free *bf;
565 int error; /* error return value */
566 int i; /* bestfree index */
567 xfs_mount_t *mp; /* filesystem mount point */
568 xfs_trans_t *tp; /* transaction pointer */
575 * Get the buffer set up for the block.
577 error = xfs_da_get_buf(tp, dp, xfs_dir2_db_to_da(mp, blkno), -1, &bp,
581 bp->b_ops = &xfs_dir3_data_buf_ops;
582 xfs_trans_buf_set_type(tp, bp, XFS_BLFT_DIR_DATA_BUF);
585 * Initialize the header.
588 if (xfs_sb_version_hascrc(&mp->m_sb)) {
589 struct xfs_dir3_blk_hdr *hdr3 = bp->b_addr;
591 memset(hdr3, 0, sizeof(*hdr3));
592 hdr3->magic = cpu_to_be32(XFS_DIR3_DATA_MAGIC);
593 hdr3->blkno = cpu_to_be64(bp->b_bn);
594 hdr3->owner = cpu_to_be64(dp->i_ino);
595 uuid_copy(&hdr3->uuid, &mp->m_sb.sb_uuid);
598 hdr->magic = cpu_to_be32(XFS_DIR2_DATA_MAGIC);
600 bf = dp->d_ops->data_bestfree_p(hdr);
601 bf[0].offset = cpu_to_be16(dp->d_ops->data_entry_offset);
602 for (i = 1; i < XFS_DIR2_DATA_FD_COUNT; i++) {
608 * Set up an unused entry for the block's body.
610 dup = dp->d_ops->data_unused_p(hdr);
611 dup->freetag = cpu_to_be16(XFS_DIR2_DATA_FREE_TAG);
613 t = mp->m_dirblksize - (uint)dp->d_ops->data_entry_offset;
614 bf[0].length = cpu_to_be16(t);
615 dup->length = cpu_to_be16(t);
616 *xfs_dir2_data_unused_tag_p(dup) = cpu_to_be16((char *)dup - (char *)hdr);
618 * Log it and return it.
620 xfs_dir2_data_log_header(tp, dp, bp);
621 xfs_dir2_data_log_unused(tp, bp, dup);
627 * Log an active data entry from the block.
630 xfs_dir2_data_log_entry(
631 struct xfs_trans *tp,
632 struct xfs_inode *dp,
634 xfs_dir2_data_entry_t *dep) /* data entry pointer */
636 struct xfs_dir2_data_hdr *hdr = bp->b_addr;
638 ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
639 hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC) ||
640 hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) ||
641 hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC));
643 xfs_trans_log_buf(tp, bp, (uint)((char *)dep - (char *)hdr),
644 (uint)((char *)(dp->d_ops->data_entry_tag_p(dep) + 1) -
649 * Log a data block header.
652 xfs_dir2_data_log_header(
653 struct xfs_trans *tp,
654 struct xfs_inode *dp,
658 struct xfs_dir2_data_hdr *hdr = bp->b_addr;
660 ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
661 hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC) ||
662 hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) ||
663 hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC));
666 xfs_trans_log_buf(tp, bp, 0, dp->d_ops->data_entry_offset - 1);
670 * Log a data unused entry.
673 xfs_dir2_data_log_unused(
674 struct xfs_trans *tp,
676 xfs_dir2_data_unused_t *dup) /* data unused pointer */
678 xfs_dir2_data_hdr_t *hdr = bp->b_addr;
680 ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
681 hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC) ||
682 hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) ||
683 hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC));
686 * Log the first part of the unused entry.
688 xfs_trans_log_buf(tp, bp, (uint)((char *)dup - (char *)hdr),
689 (uint)((char *)&dup->length + sizeof(dup->length) -
692 * Log the end (tag) of the unused entry.
694 xfs_trans_log_buf(tp, bp,
695 (uint)((char *)xfs_dir2_data_unused_tag_p(dup) - (char *)hdr),
696 (uint)((char *)xfs_dir2_data_unused_tag_p(dup) - (char *)hdr +
697 sizeof(xfs_dir2_data_off_t) - 1));
701 * Make a byte range in the data block unused.
702 * Its current contents are unimportant.
705 xfs_dir2_data_make_free(
706 struct xfs_trans *tp,
707 struct xfs_inode *dp,
709 xfs_dir2_data_aoff_t offset, /* starting byte offset */
710 xfs_dir2_data_aoff_t len, /* length in bytes */
711 int *needlogp, /* out: log header */
712 int *needscanp) /* out: regen bestfree */
714 xfs_dir2_data_hdr_t *hdr; /* data block pointer */
715 xfs_dir2_data_free_t *dfp; /* bestfree pointer */
716 char *endptr; /* end of data area */
717 xfs_mount_t *mp; /* filesystem mount point */
718 int needscan; /* need to regen bestfree */
719 xfs_dir2_data_unused_t *newdup; /* new unused entry */
720 xfs_dir2_data_unused_t *postdup; /* unused entry after us */
721 xfs_dir2_data_unused_t *prevdup; /* unused entry before us */
722 struct xfs_dir2_data_free *bf;
728 * Figure out where the end of the data area is.
730 if (hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
731 hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC))
732 endptr = (char *)hdr + mp->m_dirblksize;
734 xfs_dir2_block_tail_t *btp; /* block tail */
736 ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) ||
737 hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC));
738 btp = xfs_dir2_block_tail_p(mp, hdr);
739 endptr = (char *)xfs_dir2_block_leaf_p(btp);
742 * If this isn't the start of the block, then back up to
743 * the previous entry and see if it's free.
745 if (offset > dp->d_ops->data_entry_offset) {
746 __be16 *tagp; /* tag just before us */
748 tagp = (__be16 *)((char *)hdr + offset) - 1;
749 prevdup = (xfs_dir2_data_unused_t *)((char *)hdr + be16_to_cpu(*tagp));
750 if (be16_to_cpu(prevdup->freetag) != XFS_DIR2_DATA_FREE_TAG)
755 * If this isn't the end of the block, see if the entry after
758 if ((char *)hdr + offset + len < endptr) {
760 (xfs_dir2_data_unused_t *)((char *)hdr + offset + len);
761 if (be16_to_cpu(postdup->freetag) != XFS_DIR2_DATA_FREE_TAG)
765 ASSERT(*needscanp == 0);
768 * Previous and following entries are both free,
769 * merge everything into a single free entry.
771 bf = dp->d_ops->data_bestfree_p(hdr);
772 if (prevdup && postdup) {
773 xfs_dir2_data_free_t *dfp2; /* another bestfree pointer */
776 * See if prevdup and/or postdup are in bestfree table.
778 dfp = xfs_dir2_data_freefind(hdr, bf, prevdup);
779 dfp2 = xfs_dir2_data_freefind(hdr, bf, postdup);
781 * We need a rescan unless there are exactly 2 free entries
782 * namely our two. Then we know what's happening, otherwise
783 * since the third bestfree is there, there might be more
786 needscan = (bf[2].length != 0);
788 * Fix up the new big freespace.
790 be16_add_cpu(&prevdup->length, len + be16_to_cpu(postdup->length));
791 *xfs_dir2_data_unused_tag_p(prevdup) =
792 cpu_to_be16((char *)prevdup - (char *)hdr);
793 xfs_dir2_data_log_unused(tp, bp, prevdup);
796 * Has to be the case that entries 0 and 1 are
797 * dfp and dfp2 (don't know which is which), and
799 * Remove entry 1 first then entry 0.
807 xfs_dir2_data_freeremove(hdr, bf, dfp2, needlogp);
808 xfs_dir2_data_freeremove(hdr, bf, dfp, needlogp);
810 * Now insert the new entry.
812 dfp = xfs_dir2_data_freeinsert(hdr, bf, prevdup,
814 ASSERT(dfp == &bf[0]);
815 ASSERT(dfp->length == prevdup->length);
816 ASSERT(!dfp[1].length);
817 ASSERT(!dfp[2].length);
821 * The entry before us is free, merge with it.
824 dfp = xfs_dir2_data_freefind(hdr, bf, prevdup);
825 be16_add_cpu(&prevdup->length, len);
826 *xfs_dir2_data_unused_tag_p(prevdup) =
827 cpu_to_be16((char *)prevdup - (char *)hdr);
828 xfs_dir2_data_log_unused(tp, bp, prevdup);
830 * If the previous entry was in the table, the new entry
831 * is longer, so it will be in the table too. Remove
832 * the old one and add the new one.
835 xfs_dir2_data_freeremove(hdr, bf, dfp, needlogp);
836 xfs_dir2_data_freeinsert(hdr, bf, prevdup, needlogp);
839 * Otherwise we need a scan if the new entry is big enough.
842 needscan = be16_to_cpu(prevdup->length) >
843 be16_to_cpu(bf[2].length);
847 * The following entry is free, merge with it.
850 dfp = xfs_dir2_data_freefind(hdr, bf, postdup);
851 newdup = (xfs_dir2_data_unused_t *)((char *)hdr + offset);
852 newdup->freetag = cpu_to_be16(XFS_DIR2_DATA_FREE_TAG);
853 newdup->length = cpu_to_be16(len + be16_to_cpu(postdup->length));
854 *xfs_dir2_data_unused_tag_p(newdup) =
855 cpu_to_be16((char *)newdup - (char *)hdr);
856 xfs_dir2_data_log_unused(tp, bp, newdup);
858 * If the following entry was in the table, the new entry
859 * is longer, so it will be in the table too. Remove
860 * the old one and add the new one.
863 xfs_dir2_data_freeremove(hdr, bf, dfp, needlogp);
864 xfs_dir2_data_freeinsert(hdr, bf, newdup, needlogp);
867 * Otherwise we need a scan if the new entry is big enough.
870 needscan = be16_to_cpu(newdup->length) >
871 be16_to_cpu(bf[2].length);
875 * Neither neighbor is free. Make a new entry.
878 newdup = (xfs_dir2_data_unused_t *)((char *)hdr + offset);
879 newdup->freetag = cpu_to_be16(XFS_DIR2_DATA_FREE_TAG);
880 newdup->length = cpu_to_be16(len);
881 *xfs_dir2_data_unused_tag_p(newdup) =
882 cpu_to_be16((char *)newdup - (char *)hdr);
883 xfs_dir2_data_log_unused(tp, bp, newdup);
884 xfs_dir2_data_freeinsert(hdr, bf, newdup, needlogp);
886 *needscanp = needscan;
890 * Take a byte range out of an existing unused space and make it un-free.
893 xfs_dir2_data_use_free(
894 struct xfs_trans *tp,
895 struct xfs_inode *dp,
897 xfs_dir2_data_unused_t *dup, /* unused entry */
898 xfs_dir2_data_aoff_t offset, /* starting offset to use */
899 xfs_dir2_data_aoff_t len, /* length to use */
900 int *needlogp, /* out: need to log header */
901 int *needscanp) /* out: need regen bestfree */
903 xfs_dir2_data_hdr_t *hdr; /* data block header */
904 xfs_dir2_data_free_t *dfp; /* bestfree pointer */
905 int matchback; /* matches end of freespace */
906 int matchfront; /* matches start of freespace */
907 int needscan; /* need to regen bestfree */
908 xfs_dir2_data_unused_t *newdup; /* new unused entry */
909 xfs_dir2_data_unused_t *newdup2; /* another new unused entry */
910 int oldlen; /* old unused entry's length */
911 struct xfs_dir2_data_free *bf;
914 ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
915 hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC) ||
916 hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) ||
917 hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC));
918 ASSERT(be16_to_cpu(dup->freetag) == XFS_DIR2_DATA_FREE_TAG);
919 ASSERT(offset >= (char *)dup - (char *)hdr);
920 ASSERT(offset + len <= (char *)dup + be16_to_cpu(dup->length) - (char *)hdr);
921 ASSERT((char *)dup - (char *)hdr == be16_to_cpu(*xfs_dir2_data_unused_tag_p(dup)));
923 * Look up the entry in the bestfree table.
925 oldlen = be16_to_cpu(dup->length);
926 bf = dp->d_ops->data_bestfree_p(hdr);
927 dfp = xfs_dir2_data_freefind(hdr, bf, dup);
928 ASSERT(dfp || oldlen <= be16_to_cpu(bf[2].length));
930 * Check for alignment with front and back of the entry.
932 matchfront = (char *)dup - (char *)hdr == offset;
933 matchback = (char *)dup + oldlen - (char *)hdr == offset + len;
934 ASSERT(*needscanp == 0);
937 * If we matched it exactly we just need to get rid of it from
938 * the bestfree table.
940 if (matchfront && matchback) {
942 needscan = (bf[2].offset != 0);
944 xfs_dir2_data_freeremove(hdr, bf, dfp,
949 * We match the first part of the entry.
950 * Make a new entry with the remaining freespace.
952 else if (matchfront) {
953 newdup = (xfs_dir2_data_unused_t *)((char *)hdr + offset + len);
954 newdup->freetag = cpu_to_be16(XFS_DIR2_DATA_FREE_TAG);
955 newdup->length = cpu_to_be16(oldlen - len);
956 *xfs_dir2_data_unused_tag_p(newdup) =
957 cpu_to_be16((char *)newdup - (char *)hdr);
958 xfs_dir2_data_log_unused(tp, bp, newdup);
960 * If it was in the table, remove it and add the new one.
963 xfs_dir2_data_freeremove(hdr, bf, dfp, needlogp);
964 dfp = xfs_dir2_data_freeinsert(hdr, bf, newdup,
967 ASSERT(dfp->length == newdup->length);
968 ASSERT(be16_to_cpu(dfp->offset) == (char *)newdup - (char *)hdr);
970 * If we got inserted at the last slot,
971 * that means we don't know if there was a better
972 * choice for the last slot, or not. Rescan.
974 needscan = dfp == &bf[2];
978 * We match the last part of the entry.
979 * Trim the allocated space off the tail of the entry.
981 else if (matchback) {
983 newdup->length = cpu_to_be16(((char *)hdr + offset) - (char *)newdup);
984 *xfs_dir2_data_unused_tag_p(newdup) =
985 cpu_to_be16((char *)newdup - (char *)hdr);
986 xfs_dir2_data_log_unused(tp, bp, newdup);
988 * If it was in the table, remove it and add the new one.
991 xfs_dir2_data_freeremove(hdr, bf, dfp, needlogp);
992 dfp = xfs_dir2_data_freeinsert(hdr, bf, newdup,
995 ASSERT(dfp->length == newdup->length);
996 ASSERT(be16_to_cpu(dfp->offset) == (char *)newdup - (char *)hdr);
998 * If we got inserted at the last slot,
999 * that means we don't know if there was a better
1000 * choice for the last slot, or not. Rescan.
1002 needscan = dfp == &bf[2];
1006 * Poking out the middle of an entry.
1007 * Make two new entries.
1011 newdup->length = cpu_to_be16(((char *)hdr + offset) - (char *)newdup);
1012 *xfs_dir2_data_unused_tag_p(newdup) =
1013 cpu_to_be16((char *)newdup - (char *)hdr);
1014 xfs_dir2_data_log_unused(tp, bp, newdup);
1015 newdup2 = (xfs_dir2_data_unused_t *)((char *)hdr + offset + len);
1016 newdup2->freetag = cpu_to_be16(XFS_DIR2_DATA_FREE_TAG);
1017 newdup2->length = cpu_to_be16(oldlen - len - be16_to_cpu(newdup->length));
1018 *xfs_dir2_data_unused_tag_p(newdup2) =
1019 cpu_to_be16((char *)newdup2 - (char *)hdr);
1020 xfs_dir2_data_log_unused(tp, bp, newdup2);
1022 * If the old entry was in the table, we need to scan
1023 * if the 3rd entry was valid, since these entries
1024 * are smaller than the old one.
1025 * If we don't need to scan that means there were 1 or 2
1026 * entries in the table, and removing the old and adding
1027 * the 2 new will work.
1030 needscan = (bf[2].length != 0);
1032 xfs_dir2_data_freeremove(hdr, bf, dfp,
1034 xfs_dir2_data_freeinsert(hdr, bf, newdup,
1036 xfs_dir2_data_freeinsert(hdr, bf, newdup2,
1041 *needscanp = needscan;