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_types.h"
23 #include "xfs_trans.h"
26 #include "xfs_mount.h"
27 #include "xfs_da_btree.h"
28 #include "xfs_bmap_btree.h"
29 #include "xfs_dinode.h"
30 #include "xfs_inode.h"
31 #include "xfs_dir2_format.h"
32 #include "xfs_dir2_priv.h"
33 #include "xfs_error.h"
34 #include "xfs_buf_item.h"
35 #include "xfs_cksum.h"
37 STATIC xfs_dir2_data_free_t *
38 xfs_dir2_data_freefind(xfs_dir2_data_hdr_t *hdr, xfs_dir2_data_unused_t *dup);
41 * Check the consistency of the data block.
42 * The input can also be a block-format directory.
43 * Return 0 is the buffer is good, otherwise an error.
46 __xfs_dir3_data_check(
47 struct xfs_inode *dp, /* incore inode pointer */
48 struct xfs_buf *bp) /* data block's buffer */
50 xfs_dir2_dataptr_t addr; /* addr for leaf lookup */
51 xfs_dir2_data_free_t *bf; /* bestfree table */
52 xfs_dir2_block_tail_t *btp=NULL; /* block tail */
53 int count; /* count of entries found */
54 xfs_dir2_data_hdr_t *hdr; /* data block header */
55 xfs_dir2_data_entry_t *dep; /* data entry */
56 xfs_dir2_data_free_t *dfp; /* bestfree entry */
57 xfs_dir2_data_unused_t *dup; /* unused entry */
58 char *endp; /* end of useful data */
59 int freeseen; /* mask of bestfrees seen */
60 xfs_dahash_t hash; /* hash of current name */
61 int i; /* leaf index */
62 int lastfree; /* last entry was unused */
63 xfs_dir2_leaf_entry_t *lep=NULL; /* block leaf entries */
64 xfs_mount_t *mp; /* filesystem mount point */
65 char *p; /* current data position */
66 int stale; /* count of stale leaves */
69 mp = bp->b_target->bt_mount;
71 bf = xfs_dir3_data_bestfree_p(hdr);
72 p = (char *)xfs_dir3_data_entry_p(hdr);
75 case cpu_to_be32(XFS_DIR3_BLOCK_MAGIC):
76 case cpu_to_be32(XFS_DIR2_BLOCK_MAGIC):
77 btp = xfs_dir2_block_tail_p(mp, hdr);
78 lep = xfs_dir2_block_leaf_p(btp);
81 case cpu_to_be32(XFS_DIR3_DATA_MAGIC):
82 case cpu_to_be32(XFS_DIR2_DATA_MAGIC):
83 endp = (char *)hdr + mp->m_dirblksize;
86 XFS_ERROR_REPORT("Bad Magic", XFS_ERRLEVEL_LOW, mp);
90 count = lastfree = freeseen = 0;
92 * Account for zero bestfree entries.
95 XFS_WANT_CORRUPTED_RETURN(!bf[0].offset);
99 XFS_WANT_CORRUPTED_RETURN(!bf[1].offset);
103 XFS_WANT_CORRUPTED_RETURN(!bf[2].offset);
107 XFS_WANT_CORRUPTED_RETURN(be16_to_cpu(bf[0].length) >=
108 be16_to_cpu(bf[1].length));
109 XFS_WANT_CORRUPTED_RETURN(be16_to_cpu(bf[1].length) >=
110 be16_to_cpu(bf[2].length));
112 * Loop over the data/unused entries.
115 dup = (xfs_dir2_data_unused_t *)p;
117 * If it's unused, look for the space in the bestfree table.
118 * If we find it, account for that, else make sure it
119 * doesn't need to be there.
121 if (be16_to_cpu(dup->freetag) == XFS_DIR2_DATA_FREE_TAG) {
122 XFS_WANT_CORRUPTED_RETURN(lastfree == 0);
123 XFS_WANT_CORRUPTED_RETURN(
124 be16_to_cpu(*xfs_dir2_data_unused_tag_p(dup)) ==
125 (char *)dup - (char *)hdr);
126 dfp = xfs_dir2_data_freefind(hdr, dup);
129 XFS_WANT_CORRUPTED_RETURN(
130 (freeseen & (1 << i)) == 0);
133 XFS_WANT_CORRUPTED_RETURN(
134 be16_to_cpu(dup->length) <=
135 be16_to_cpu(bf[2].length));
137 p += be16_to_cpu(dup->length);
142 * It's a real entry. Validate the fields.
143 * If this is a block directory then make sure it's
144 * in the leaf section of the block.
145 * The linear search is crude but this is DEBUG code.
147 dep = (xfs_dir2_data_entry_t *)p;
148 XFS_WANT_CORRUPTED_RETURN(dep->namelen != 0);
149 XFS_WANT_CORRUPTED_RETURN(
150 !xfs_dir_ino_validate(mp, be64_to_cpu(dep->inumber)));
151 XFS_WANT_CORRUPTED_RETURN(
152 be16_to_cpu(*xfs_dir2_data_entry_tag_p(dep)) ==
153 (char *)dep - (char *)hdr);
156 if (hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) ||
157 hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC)) {
158 addr = xfs_dir2_db_off_to_dataptr(mp, mp->m_dirdatablk,
159 (xfs_dir2_data_aoff_t)
160 ((char *)dep - (char *)hdr));
161 name.name = dep->name;
162 name.len = dep->namelen;
163 hash = mp->m_dirnameops->hashname(&name);
164 for (i = 0; i < be32_to_cpu(btp->count); i++) {
165 if (be32_to_cpu(lep[i].address) == addr &&
166 be32_to_cpu(lep[i].hashval) == hash)
169 XFS_WANT_CORRUPTED_RETURN(i < be32_to_cpu(btp->count));
171 p += xfs_dir2_data_entsize(dep->namelen);
174 * Need to have seen all the entries and all the bestfree slots.
176 XFS_WANT_CORRUPTED_RETURN(freeseen == 7);
177 if (hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) ||
178 hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC)) {
179 for (i = stale = 0; i < be32_to_cpu(btp->count); i++) {
180 if (lep[i].address ==
181 cpu_to_be32(XFS_DIR2_NULL_DATAPTR))
184 XFS_WANT_CORRUPTED_RETURN(
185 be32_to_cpu(lep[i].hashval) >=
186 be32_to_cpu(lep[i - 1].hashval));
188 XFS_WANT_CORRUPTED_RETURN(count ==
189 be32_to_cpu(btp->count) - be32_to_cpu(btp->stale));
190 XFS_WANT_CORRUPTED_RETURN(stale == be32_to_cpu(btp->stale));
196 xfs_dir3_data_verify(
199 struct xfs_mount *mp = bp->b_target->bt_mount;
200 struct xfs_dir3_blk_hdr *hdr3 = bp->b_addr;
202 if (xfs_sb_version_hascrc(&mp->m_sb)) {
203 if (hdr3->magic != cpu_to_be32(XFS_DIR3_DATA_MAGIC))
205 if (!uuid_equal(&hdr3->uuid, &mp->m_sb.sb_uuid))
207 if (be64_to_cpu(hdr3->blkno) != bp->b_bn)
210 if (hdr3->magic != cpu_to_be32(XFS_DIR2_DATA_MAGIC))
213 if (__xfs_dir3_data_check(NULL, bp))
219 * Readahead of the first block of the directory when it is opened is completely
220 * oblivious to the format of the directory. Hence we can either get a block
221 * format buffer or a data format buffer on readahead.
224 xfs_dir3_data_reada_verify(
227 struct xfs_mount *mp = bp->b_target->bt_mount;
228 struct xfs_dir2_data_hdr *hdr = bp->b_addr;
230 switch (hdr->magic) {
231 case cpu_to_be32(XFS_DIR2_BLOCK_MAGIC):
232 case cpu_to_be32(XFS_DIR3_BLOCK_MAGIC):
233 bp->b_ops = &xfs_dir3_block_buf_ops;
234 bp->b_ops->verify_read(bp);
236 case cpu_to_be32(XFS_DIR2_DATA_MAGIC):
237 case cpu_to_be32(XFS_DIR3_DATA_MAGIC):
238 xfs_dir3_data_verify(bp);
241 XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp, hdr);
242 xfs_buf_ioerror(bp, EFSCORRUPTED);
248 xfs_dir3_data_read_verify(
251 struct xfs_mount *mp = bp->b_target->bt_mount;
253 if ((xfs_sb_version_hascrc(&mp->m_sb) &&
254 !xfs_verify_cksum(bp->b_addr, BBTOB(bp->b_length),
255 XFS_DIR3_DATA_CRC_OFF)) ||
256 !xfs_dir3_data_verify(bp)) {
257 XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp, bp->b_addr);
258 xfs_buf_ioerror(bp, EFSCORRUPTED);
263 xfs_dir3_data_write_verify(
266 struct xfs_mount *mp = bp->b_target->bt_mount;
267 struct xfs_buf_log_item *bip = bp->b_fspriv;
268 struct xfs_dir3_blk_hdr *hdr3 = bp->b_addr;
270 if (!xfs_dir3_data_verify(bp)) {
271 XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp, bp->b_addr);
272 xfs_buf_ioerror(bp, EFSCORRUPTED);
276 if (!xfs_sb_version_hascrc(&mp->m_sb))
280 hdr3->lsn = cpu_to_be64(bip->bli_item.li_lsn);
282 xfs_update_cksum(bp->b_addr, BBTOB(bp->b_length), XFS_DIR3_DATA_CRC_OFF);
285 const struct xfs_buf_ops xfs_dir3_data_buf_ops = {
286 .verify_read = xfs_dir3_data_read_verify,
287 .verify_write = xfs_dir3_data_write_verify,
290 static const struct xfs_buf_ops xfs_dir3_data_reada_buf_ops = {
291 .verify_read = xfs_dir3_data_reada_verify,
292 .verify_write = xfs_dir3_data_write_verify,
298 struct xfs_trans *tp,
299 struct xfs_inode *dp,
301 xfs_daddr_t mapped_bno,
302 struct xfs_buf **bpp)
304 return xfs_da_read_buf(tp, dp, bno, mapped_bno, bpp,
305 XFS_DATA_FORK, &xfs_dir3_data_buf_ops);
309 xfs_dir3_data_readahead(
310 struct xfs_trans *tp,
311 struct xfs_inode *dp,
313 xfs_daddr_t mapped_bno)
315 return xfs_da_reada_buf(tp, dp, bno, mapped_bno,
316 XFS_DATA_FORK, &xfs_dir3_data_reada_buf_ops);
320 * Given a data block and an unused entry from that block,
321 * return the bestfree entry if any that corresponds to it.
323 STATIC xfs_dir2_data_free_t *
324 xfs_dir2_data_freefind(
325 xfs_dir2_data_hdr_t *hdr, /* data block */
326 xfs_dir2_data_unused_t *dup) /* data unused entry */
328 xfs_dir2_data_free_t *dfp; /* bestfree entry */
329 xfs_dir2_data_aoff_t off; /* offset value needed */
330 struct xfs_dir2_data_free *bf;
331 #if defined(DEBUG) && defined(__KERNEL__)
332 int matched; /* matched the value */
333 int seenzero; /* saw a 0 bestfree entry */
336 off = (xfs_dir2_data_aoff_t)((char *)dup - (char *)hdr);
337 bf = xfs_dir3_data_bestfree_p(hdr);
339 #if defined(DEBUG) && defined(__KERNEL__)
341 * Validate some consistency in the bestfree table.
342 * Check order, non-overlapping entries, and if we find the
343 * one we're looking for it has to be exact.
345 ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
346 hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC) ||
347 hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) ||
348 hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC));
349 for (dfp = &bf[0], seenzero = matched = 0;
350 dfp < &bf[XFS_DIR2_DATA_FD_COUNT];
353 ASSERT(!dfp->length);
357 ASSERT(seenzero == 0);
358 if (be16_to_cpu(dfp->offset) == off) {
360 ASSERT(dfp->length == dup->length);
361 } else if (off < be16_to_cpu(dfp->offset))
362 ASSERT(off + be16_to_cpu(dup->length) <= be16_to_cpu(dfp->offset));
364 ASSERT(be16_to_cpu(dfp->offset) + be16_to_cpu(dfp->length) <= off);
365 ASSERT(matched || be16_to_cpu(dfp->length) >= be16_to_cpu(dup->length));
367 ASSERT(be16_to_cpu(dfp[-1].length) >= be16_to_cpu(dfp[0].length));
371 * If this is smaller than the smallest bestfree entry,
372 * it can't be there since they're sorted.
374 if (be16_to_cpu(dup->length) <
375 be16_to_cpu(bf[XFS_DIR2_DATA_FD_COUNT - 1].length))
378 * Look at the three bestfree entries for our guy.
380 for (dfp = &bf[0]; dfp < &bf[XFS_DIR2_DATA_FD_COUNT]; dfp++) {
383 if (be16_to_cpu(dfp->offset) == off)
387 * Didn't find it. This only happens if there are duplicate lengths.
393 * Insert an unused-space entry into the bestfree table.
395 xfs_dir2_data_free_t * /* entry inserted */
396 xfs_dir2_data_freeinsert(
397 xfs_dir2_data_hdr_t *hdr, /* data block pointer */
398 xfs_dir2_data_unused_t *dup, /* unused space */
399 int *loghead) /* log the data header (out) */
401 xfs_dir2_data_free_t *dfp; /* bestfree table pointer */
402 xfs_dir2_data_free_t new; /* new bestfree entry */
404 ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
405 hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) ||
406 hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC) ||
407 hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC));
409 dfp = xfs_dir3_data_bestfree_p(hdr);
410 new.length = dup->length;
411 new.offset = cpu_to_be16((char *)dup - (char *)hdr);
414 * Insert at position 0, 1, or 2; or not at all.
416 if (be16_to_cpu(new.length) > be16_to_cpu(dfp[0].length)) {
423 if (be16_to_cpu(new.length) > be16_to_cpu(dfp[1].length)) {
429 if (be16_to_cpu(new.length) > be16_to_cpu(dfp[2].length)) {
438 * Remove a bestfree entry from the table.
441 xfs_dir2_data_freeremove(
442 xfs_dir2_data_hdr_t *hdr, /* data block header */
443 xfs_dir2_data_free_t *dfp, /* bestfree entry pointer */
444 int *loghead) /* out: log data header */
446 struct xfs_dir2_data_free *bf;
448 ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
449 hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) ||
450 hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC) ||
451 hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC));
454 * It's the first entry, slide the next 2 up.
456 bf = xfs_dir3_data_bestfree_p(hdr);
462 * It's the second entry, slide the 3rd entry up.
464 else if (dfp == &bf[1])
467 * Must be the last entry.
470 ASSERT(dfp == &bf[2]);
472 * Clear the 3rd entry, must be zero now.
480 * Given a data block, reconstruct its bestfree map.
483 xfs_dir2_data_freescan(
484 xfs_mount_t *mp, /* filesystem mount point */
485 xfs_dir2_data_hdr_t *hdr, /* data block header */
486 int *loghead) /* out: log data header */
488 xfs_dir2_block_tail_t *btp; /* block tail */
489 xfs_dir2_data_entry_t *dep; /* active data entry */
490 xfs_dir2_data_unused_t *dup; /* unused data entry */
491 struct xfs_dir2_data_free *bf;
492 char *endp; /* end of block's data */
493 char *p; /* current entry pointer */
495 ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
496 hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC) ||
497 hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) ||
498 hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC));
501 * Start by clearing the table.
503 bf = xfs_dir3_data_bestfree_p(hdr);
504 memset(bf, 0, sizeof(*bf) * XFS_DIR2_DATA_FD_COUNT);
509 p = (char *)xfs_dir3_data_entry_p(hdr);
510 if (hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) ||
511 hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC)) {
512 btp = xfs_dir2_block_tail_p(mp, hdr);
513 endp = (char *)xfs_dir2_block_leaf_p(btp);
515 endp = (char *)hdr + mp->m_dirblksize;
517 * Loop over the block's entries.
520 dup = (xfs_dir2_data_unused_t *)p;
522 * If it's a free entry, insert it.
524 if (be16_to_cpu(dup->freetag) == XFS_DIR2_DATA_FREE_TAG) {
525 ASSERT((char *)dup - (char *)hdr ==
526 be16_to_cpu(*xfs_dir2_data_unused_tag_p(dup)));
527 xfs_dir2_data_freeinsert(hdr, dup, loghead);
528 p += be16_to_cpu(dup->length);
531 * For active entries, check their tags and skip them.
534 dep = (xfs_dir2_data_entry_t *)p;
535 ASSERT((char *)dep - (char *)hdr ==
536 be16_to_cpu(*xfs_dir2_data_entry_tag_p(dep)));
537 p += xfs_dir2_data_entsize(dep->namelen);
543 * Initialize a data block at the given block number in the directory.
544 * Give back the buffer for the created block.
548 xfs_da_args_t *args, /* directory operation args */
549 xfs_dir2_db_t blkno, /* logical dir block number */
550 struct xfs_buf **bpp) /* output block buffer */
552 struct xfs_buf *bp; /* block buffer */
553 xfs_dir2_data_hdr_t *hdr; /* data block header */
554 xfs_inode_t *dp; /* incore directory inode */
555 xfs_dir2_data_unused_t *dup; /* unused entry pointer */
556 struct xfs_dir2_data_free *bf;
557 int error; /* error return value */
558 int i; /* bestfree index */
559 xfs_mount_t *mp; /* filesystem mount point */
560 xfs_trans_t *tp; /* transaction pointer */
567 * Get the buffer set up for the block.
569 error = xfs_da_get_buf(tp, dp, xfs_dir2_db_to_da(mp, blkno), -1, &bp,
573 bp->b_ops = &xfs_dir3_data_buf_ops;
576 * Initialize the header.
579 if (xfs_sb_version_hascrc(&mp->m_sb)) {
580 struct xfs_dir3_blk_hdr *hdr3 = bp->b_addr;
582 memset(hdr3, 0, sizeof(*hdr3));
583 hdr3->magic = cpu_to_be32(XFS_DIR3_DATA_MAGIC);
584 hdr3->blkno = cpu_to_be64(bp->b_bn);
585 hdr3->owner = cpu_to_be64(dp->i_ino);
586 uuid_copy(&hdr3->uuid, &mp->m_sb.sb_uuid);
589 hdr->magic = cpu_to_be32(XFS_DIR2_DATA_MAGIC);
591 bf = xfs_dir3_data_bestfree_p(hdr);
592 bf[0].offset = cpu_to_be16(xfs_dir3_data_entry_offset(hdr));
593 for (i = 1; i < XFS_DIR2_DATA_FD_COUNT; i++) {
599 * Set up an unused entry for the block's body.
601 dup = xfs_dir3_data_unused_p(hdr);
602 dup->freetag = cpu_to_be16(XFS_DIR2_DATA_FREE_TAG);
604 t = mp->m_dirblksize - (uint)xfs_dir3_data_entry_offset(hdr);
605 bf[0].length = cpu_to_be16(t);
606 dup->length = cpu_to_be16(t);
607 *xfs_dir2_data_unused_tag_p(dup) = cpu_to_be16((char *)dup - (char *)hdr);
609 * Log it and return it.
611 xfs_dir2_data_log_header(tp, bp);
612 xfs_dir2_data_log_unused(tp, bp, dup);
618 * Log an active data entry from the block.
621 xfs_dir2_data_log_entry(
622 struct xfs_trans *tp,
624 xfs_dir2_data_entry_t *dep) /* data entry pointer */
626 xfs_dir2_data_hdr_t *hdr = bp->b_addr;
628 ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
629 hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC) ||
630 hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) ||
631 hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC));
633 xfs_trans_log_buf(tp, bp, (uint)((char *)dep - (char *)hdr),
634 (uint)((char *)(xfs_dir2_data_entry_tag_p(dep) + 1) -
639 * Log a data block header.
642 xfs_dir2_data_log_header(
643 struct xfs_trans *tp,
646 xfs_dir2_data_hdr_t *hdr = bp->b_addr;
648 ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
649 hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC) ||
650 hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) ||
651 hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC));
653 xfs_trans_log_buf(tp, bp, 0, xfs_dir3_data_entry_offset(hdr) - 1);
657 * Log a data unused entry.
660 xfs_dir2_data_log_unused(
661 struct xfs_trans *tp,
663 xfs_dir2_data_unused_t *dup) /* data unused pointer */
665 xfs_dir2_data_hdr_t *hdr = bp->b_addr;
667 ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
668 hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC) ||
669 hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) ||
670 hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC));
673 * Log the first part of the unused entry.
675 xfs_trans_log_buf(tp, bp, (uint)((char *)dup - (char *)hdr),
676 (uint)((char *)&dup->length + sizeof(dup->length) -
679 * Log the end (tag) of the unused entry.
681 xfs_trans_log_buf(tp, bp,
682 (uint)((char *)xfs_dir2_data_unused_tag_p(dup) - (char *)hdr),
683 (uint)((char *)xfs_dir2_data_unused_tag_p(dup) - (char *)hdr +
684 sizeof(xfs_dir2_data_off_t) - 1));
688 * Make a byte range in the data block unused.
689 * Its current contents are unimportant.
692 xfs_dir2_data_make_free(
693 struct xfs_trans *tp,
695 xfs_dir2_data_aoff_t offset, /* starting byte offset */
696 xfs_dir2_data_aoff_t len, /* length in bytes */
697 int *needlogp, /* out: log header */
698 int *needscanp) /* out: regen bestfree */
700 xfs_dir2_data_hdr_t *hdr; /* data block pointer */
701 xfs_dir2_data_free_t *dfp; /* bestfree pointer */
702 char *endptr; /* end of data area */
703 xfs_mount_t *mp; /* filesystem mount point */
704 int needscan; /* need to regen bestfree */
705 xfs_dir2_data_unused_t *newdup; /* new unused entry */
706 xfs_dir2_data_unused_t *postdup; /* unused entry after us */
707 xfs_dir2_data_unused_t *prevdup; /* unused entry before us */
708 struct xfs_dir2_data_free *bf;
714 * Figure out where the end of the data area is.
716 if (hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
717 hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC))
718 endptr = (char *)hdr + mp->m_dirblksize;
720 xfs_dir2_block_tail_t *btp; /* block tail */
722 ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) ||
723 hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC));
724 btp = xfs_dir2_block_tail_p(mp, hdr);
725 endptr = (char *)xfs_dir2_block_leaf_p(btp);
728 * If this isn't the start of the block, then back up to
729 * the previous entry and see if it's free.
731 if (offset > xfs_dir3_data_entry_offset(hdr)) {
732 __be16 *tagp; /* tag just before us */
734 tagp = (__be16 *)((char *)hdr + offset) - 1;
735 prevdup = (xfs_dir2_data_unused_t *)((char *)hdr + be16_to_cpu(*tagp));
736 if (be16_to_cpu(prevdup->freetag) != XFS_DIR2_DATA_FREE_TAG)
741 * If this isn't the end of the block, see if the entry after
744 if ((char *)hdr + offset + len < endptr) {
746 (xfs_dir2_data_unused_t *)((char *)hdr + offset + len);
747 if (be16_to_cpu(postdup->freetag) != XFS_DIR2_DATA_FREE_TAG)
751 ASSERT(*needscanp == 0);
754 * Previous and following entries are both free,
755 * merge everything into a single free entry.
757 bf = xfs_dir3_data_bestfree_p(hdr);
758 if (prevdup && postdup) {
759 xfs_dir2_data_free_t *dfp2; /* another bestfree pointer */
762 * See if prevdup and/or postdup are in bestfree table.
764 dfp = xfs_dir2_data_freefind(hdr, prevdup);
765 dfp2 = xfs_dir2_data_freefind(hdr, postdup);
767 * We need a rescan unless there are exactly 2 free entries
768 * namely our two. Then we know what's happening, otherwise
769 * since the third bestfree is there, there might be more
772 needscan = (bf[2].length != 0);
774 * Fix up the new big freespace.
776 be16_add_cpu(&prevdup->length, len + be16_to_cpu(postdup->length));
777 *xfs_dir2_data_unused_tag_p(prevdup) =
778 cpu_to_be16((char *)prevdup - (char *)hdr);
779 xfs_dir2_data_log_unused(tp, bp, prevdup);
782 * Has to be the case that entries 0 and 1 are
783 * dfp and dfp2 (don't know which is which), and
785 * Remove entry 1 first then entry 0.
793 xfs_dir2_data_freeremove(hdr, dfp2, needlogp);
794 xfs_dir2_data_freeremove(hdr, dfp, needlogp);
796 * Now insert the new entry.
798 dfp = xfs_dir2_data_freeinsert(hdr, prevdup, needlogp);
799 ASSERT(dfp == &bf[0]);
800 ASSERT(dfp->length == prevdup->length);
801 ASSERT(!dfp[1].length);
802 ASSERT(!dfp[2].length);
806 * The entry before us is free, merge with it.
809 dfp = xfs_dir2_data_freefind(hdr, prevdup);
810 be16_add_cpu(&prevdup->length, len);
811 *xfs_dir2_data_unused_tag_p(prevdup) =
812 cpu_to_be16((char *)prevdup - (char *)hdr);
813 xfs_dir2_data_log_unused(tp, bp, prevdup);
815 * If the previous entry was in the table, the new entry
816 * is longer, so it will be in the table too. Remove
817 * the old one and add the new one.
820 xfs_dir2_data_freeremove(hdr, dfp, needlogp);
821 xfs_dir2_data_freeinsert(hdr, prevdup, needlogp);
824 * Otherwise we need a scan if the new entry is big enough.
827 needscan = be16_to_cpu(prevdup->length) >
828 be16_to_cpu(bf[2].length);
832 * The following entry is free, merge with it.
835 dfp = xfs_dir2_data_freefind(hdr, postdup);
836 newdup = (xfs_dir2_data_unused_t *)((char *)hdr + offset);
837 newdup->freetag = cpu_to_be16(XFS_DIR2_DATA_FREE_TAG);
838 newdup->length = cpu_to_be16(len + be16_to_cpu(postdup->length));
839 *xfs_dir2_data_unused_tag_p(newdup) =
840 cpu_to_be16((char *)newdup - (char *)hdr);
841 xfs_dir2_data_log_unused(tp, bp, newdup);
843 * If the following entry was in the table, the new entry
844 * is longer, so it will be in the table too. Remove
845 * the old one and add the new one.
848 xfs_dir2_data_freeremove(hdr, dfp, needlogp);
849 xfs_dir2_data_freeinsert(hdr, newdup, needlogp);
852 * Otherwise we need a scan if the new entry is big enough.
855 needscan = be16_to_cpu(newdup->length) >
856 be16_to_cpu(bf[2].length);
860 * Neither neighbor is free. Make a new entry.
863 newdup = (xfs_dir2_data_unused_t *)((char *)hdr + offset);
864 newdup->freetag = cpu_to_be16(XFS_DIR2_DATA_FREE_TAG);
865 newdup->length = cpu_to_be16(len);
866 *xfs_dir2_data_unused_tag_p(newdup) =
867 cpu_to_be16((char *)newdup - (char *)hdr);
868 xfs_dir2_data_log_unused(tp, bp, newdup);
869 xfs_dir2_data_freeinsert(hdr, newdup, needlogp);
871 *needscanp = needscan;
875 * Take a byte range out of an existing unused space and make it un-free.
878 xfs_dir2_data_use_free(
879 struct xfs_trans *tp,
881 xfs_dir2_data_unused_t *dup, /* unused entry */
882 xfs_dir2_data_aoff_t offset, /* starting offset to use */
883 xfs_dir2_data_aoff_t len, /* length to use */
884 int *needlogp, /* out: need to log header */
885 int *needscanp) /* out: need regen bestfree */
887 xfs_dir2_data_hdr_t *hdr; /* data block header */
888 xfs_dir2_data_free_t *dfp; /* bestfree pointer */
889 int matchback; /* matches end of freespace */
890 int matchfront; /* matches start of freespace */
891 int needscan; /* need to regen bestfree */
892 xfs_dir2_data_unused_t *newdup; /* new unused entry */
893 xfs_dir2_data_unused_t *newdup2; /* another new unused entry */
894 int oldlen; /* old unused entry's length */
895 struct xfs_dir2_data_free *bf;
898 ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
899 hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC) ||
900 hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) ||
901 hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC));
902 ASSERT(be16_to_cpu(dup->freetag) == XFS_DIR2_DATA_FREE_TAG);
903 ASSERT(offset >= (char *)dup - (char *)hdr);
904 ASSERT(offset + len <= (char *)dup + be16_to_cpu(dup->length) - (char *)hdr);
905 ASSERT((char *)dup - (char *)hdr == be16_to_cpu(*xfs_dir2_data_unused_tag_p(dup)));
907 * Look up the entry in the bestfree table.
909 dfp = xfs_dir2_data_freefind(hdr, dup);
910 oldlen = be16_to_cpu(dup->length);
911 bf = xfs_dir3_data_bestfree_p(hdr);
912 ASSERT(dfp || oldlen <= be16_to_cpu(bf[2].length));
914 * Check for alignment with front and back of the entry.
916 matchfront = (char *)dup - (char *)hdr == offset;
917 matchback = (char *)dup + oldlen - (char *)hdr == offset + len;
918 ASSERT(*needscanp == 0);
921 * If we matched it exactly we just need to get rid of it from
922 * the bestfree table.
924 if (matchfront && matchback) {
926 needscan = (bf[2].offset != 0);
928 xfs_dir2_data_freeremove(hdr, dfp, needlogp);
932 * We match the first part of the entry.
933 * Make a new entry with the remaining freespace.
935 else if (matchfront) {
936 newdup = (xfs_dir2_data_unused_t *)((char *)hdr + offset + len);
937 newdup->freetag = cpu_to_be16(XFS_DIR2_DATA_FREE_TAG);
938 newdup->length = cpu_to_be16(oldlen - len);
939 *xfs_dir2_data_unused_tag_p(newdup) =
940 cpu_to_be16((char *)newdup - (char *)hdr);
941 xfs_dir2_data_log_unused(tp, bp, newdup);
943 * If it was in the table, remove it and add the new one.
946 xfs_dir2_data_freeremove(hdr, dfp, needlogp);
947 dfp = xfs_dir2_data_freeinsert(hdr, newdup, needlogp);
949 ASSERT(dfp->length == newdup->length);
950 ASSERT(be16_to_cpu(dfp->offset) == (char *)newdup - (char *)hdr);
952 * If we got inserted at the last slot,
953 * that means we don't know if there was a better
954 * choice for the last slot, or not. Rescan.
956 needscan = dfp == &bf[2];
960 * We match the last part of the entry.
961 * Trim the allocated space off the tail of the entry.
963 else if (matchback) {
965 newdup->length = cpu_to_be16(((char *)hdr + offset) - (char *)newdup);
966 *xfs_dir2_data_unused_tag_p(newdup) =
967 cpu_to_be16((char *)newdup - (char *)hdr);
968 xfs_dir2_data_log_unused(tp, bp, newdup);
970 * If it was in the table, remove it and add the new one.
973 xfs_dir2_data_freeremove(hdr, dfp, needlogp);
974 dfp = xfs_dir2_data_freeinsert(hdr, newdup, needlogp);
976 ASSERT(dfp->length == newdup->length);
977 ASSERT(be16_to_cpu(dfp->offset) == (char *)newdup - (char *)hdr);
979 * If we got inserted at the last slot,
980 * that means we don't know if there was a better
981 * choice for the last slot, or not. Rescan.
983 needscan = dfp == &bf[2];
987 * Poking out the middle of an entry.
988 * Make two new entries.
992 newdup->length = cpu_to_be16(((char *)hdr + offset) - (char *)newdup);
993 *xfs_dir2_data_unused_tag_p(newdup) =
994 cpu_to_be16((char *)newdup - (char *)hdr);
995 xfs_dir2_data_log_unused(tp, bp, newdup);
996 newdup2 = (xfs_dir2_data_unused_t *)((char *)hdr + offset + len);
997 newdup2->freetag = cpu_to_be16(XFS_DIR2_DATA_FREE_TAG);
998 newdup2->length = cpu_to_be16(oldlen - len - be16_to_cpu(newdup->length));
999 *xfs_dir2_data_unused_tag_p(newdup2) =
1000 cpu_to_be16((char *)newdup2 - (char *)hdr);
1001 xfs_dir2_data_log_unused(tp, bp, newdup2);
1003 * If the old entry was in the table, we need to scan
1004 * if the 3rd entry was valid, since these entries
1005 * are smaller than the old one.
1006 * If we don't need to scan that means there were 1 or 2
1007 * entries in the table, and removing the old and adding
1008 * the 2 new will work.
1011 needscan = (bf[2].length != 0);
1013 xfs_dir2_data_freeremove(hdr, dfp, needlogp);
1014 xfs_dir2_data_freeinsert(hdr, newdup, needlogp);
1015 xfs_dir2_data_freeinsert(hdr, newdup2,
1020 *needscanp = needscan;