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"
33 #include "xfs_dir2_priv.h"
34 #include "xfs_error.h"
35 #include "xfs_buf_item.h"
36 #include "xfs_cksum.h"
39 * Check the consistency of the data block.
40 * The input can also be a block-format directory.
41 * Return 0 is the buffer is good, otherwise an error.
44 __xfs_dir3_data_check(
45 struct xfs_inode *dp, /* incore inode pointer */
46 struct xfs_buf *bp) /* data block's buffer */
48 xfs_dir2_dataptr_t addr; /* addr for leaf lookup */
49 xfs_dir2_data_free_t *bf; /* bestfree table */
50 xfs_dir2_block_tail_t *btp=NULL; /* block tail */
51 int count; /* count of entries found */
52 xfs_dir2_data_hdr_t *hdr; /* data block header */
53 xfs_dir2_data_entry_t *dep; /* data entry */
54 xfs_dir2_data_free_t *dfp; /* bestfree entry */
55 xfs_dir2_data_unused_t *dup; /* unused entry */
56 char *endp; /* end of useful data */
57 int freeseen; /* mask of bestfrees seen */
58 xfs_dahash_t hash; /* hash of current name */
59 int i; /* leaf index */
60 int lastfree; /* last entry was unused */
61 xfs_dir2_leaf_entry_t *lep=NULL; /* block leaf entries */
62 xfs_mount_t *mp; /* filesystem mount point */
63 char *p; /* current data position */
64 int stale; /* count of stale leaves */
67 mp = bp->b_target->bt_mount;
69 bf = xfs_dir3_data_bestfree_p(hdr);
70 p = (char *)xfs_dir3_data_entry_p(hdr);
73 case cpu_to_be32(XFS_DIR3_BLOCK_MAGIC):
74 case cpu_to_be32(XFS_DIR2_BLOCK_MAGIC):
75 btp = xfs_dir2_block_tail_p(mp, hdr);
76 lep = xfs_dir2_block_leaf_p(btp);
79 case cpu_to_be32(XFS_DIR3_DATA_MAGIC):
80 case cpu_to_be32(XFS_DIR2_DATA_MAGIC):
81 endp = (char *)hdr + mp->m_dirblksize;
84 XFS_ERROR_REPORT("Bad Magic", XFS_ERRLEVEL_LOW, mp);
88 count = lastfree = freeseen = 0;
90 * Account for zero bestfree entries.
93 XFS_WANT_CORRUPTED_RETURN(!bf[0].offset);
97 XFS_WANT_CORRUPTED_RETURN(!bf[1].offset);
101 XFS_WANT_CORRUPTED_RETURN(!bf[2].offset);
105 XFS_WANT_CORRUPTED_RETURN(be16_to_cpu(bf[0].length) >=
106 be16_to_cpu(bf[1].length));
107 XFS_WANT_CORRUPTED_RETURN(be16_to_cpu(bf[1].length) >=
108 be16_to_cpu(bf[2].length));
110 * Loop over the data/unused entries.
113 dup = (xfs_dir2_data_unused_t *)p;
115 * If it's unused, look for the space in the bestfree table.
116 * If we find it, account for that, else make sure it
117 * doesn't need to be there.
119 if (be16_to_cpu(dup->freetag) == XFS_DIR2_DATA_FREE_TAG) {
120 XFS_WANT_CORRUPTED_RETURN(lastfree == 0);
121 XFS_WANT_CORRUPTED_RETURN(
122 be16_to_cpu(*xfs_dir2_data_unused_tag_p(dup)) ==
123 (char *)dup - (char *)hdr);
124 dfp = xfs_dir2_data_freefind(hdr, dup);
127 XFS_WANT_CORRUPTED_RETURN(
128 (freeseen & (1 << i)) == 0);
131 XFS_WANT_CORRUPTED_RETURN(
132 be16_to_cpu(dup->length) <=
133 be16_to_cpu(bf[2].length));
135 p += be16_to_cpu(dup->length);
140 * It's a real entry. Validate the fields.
141 * If this is a block directory then make sure it's
142 * in the leaf section of the block.
143 * The linear search is crude but this is DEBUG code.
145 dep = (xfs_dir2_data_entry_t *)p;
146 XFS_WANT_CORRUPTED_RETURN(dep->namelen != 0);
147 XFS_WANT_CORRUPTED_RETURN(
148 !xfs_dir_ino_validate(mp, be64_to_cpu(dep->inumber)));
149 XFS_WANT_CORRUPTED_RETURN(
150 be16_to_cpu(*xfs_dir2_data_entry_tag_p(dep)) ==
151 (char *)dep - (char *)hdr);
154 if (hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) ||
155 hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC)) {
156 addr = xfs_dir2_db_off_to_dataptr(mp, mp->m_dirdatablk,
157 (xfs_dir2_data_aoff_t)
158 ((char *)dep - (char *)hdr));
159 name.name = dep->name;
160 name.len = dep->namelen;
161 hash = mp->m_dirnameops->hashname(&name);
162 for (i = 0; i < be32_to_cpu(btp->count); i++) {
163 if (be32_to_cpu(lep[i].address) == addr &&
164 be32_to_cpu(lep[i].hashval) == hash)
167 XFS_WANT_CORRUPTED_RETURN(i < be32_to_cpu(btp->count));
169 p += xfs_dir2_data_entsize(dep->namelen);
172 * Need to have seen all the entries and all the bestfree slots.
174 XFS_WANT_CORRUPTED_RETURN(freeseen == 7);
175 if (hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) ||
176 hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC)) {
177 for (i = stale = 0; i < be32_to_cpu(btp->count); i++) {
178 if (lep[i].address ==
179 cpu_to_be32(XFS_DIR2_NULL_DATAPTR))
182 XFS_WANT_CORRUPTED_RETURN(
183 be32_to_cpu(lep[i].hashval) >=
184 be32_to_cpu(lep[i - 1].hashval));
186 XFS_WANT_CORRUPTED_RETURN(count ==
187 be32_to_cpu(btp->count) - be32_to_cpu(btp->stale));
188 XFS_WANT_CORRUPTED_RETURN(stale == be32_to_cpu(btp->stale));
194 xfs_dir3_data_verify(
197 struct xfs_mount *mp = bp->b_target->bt_mount;
198 struct xfs_dir3_blk_hdr *hdr3 = bp->b_addr;
200 if (xfs_sb_version_hascrc(&mp->m_sb)) {
201 if (hdr3->magic != cpu_to_be32(XFS_DIR3_DATA_MAGIC))
203 if (!uuid_equal(&hdr3->uuid, &mp->m_sb.sb_uuid))
205 if (be64_to_cpu(hdr3->blkno) != bp->b_bn)
208 if (hdr3->magic != cpu_to_be32(XFS_DIR2_DATA_MAGIC))
211 if (__xfs_dir3_data_check(NULL, bp))
217 * Readahead of the first block of the directory when it is opened is completely
218 * oblivious to the format of the directory. Hence we can either get a block
219 * format buffer or a data format buffer on readahead.
222 xfs_dir3_data_reada_verify(
225 struct xfs_mount *mp = bp->b_target->bt_mount;
226 struct xfs_dir2_data_hdr *hdr = bp->b_addr;
228 switch (hdr->magic) {
229 case cpu_to_be32(XFS_DIR2_BLOCK_MAGIC):
230 case cpu_to_be32(XFS_DIR3_BLOCK_MAGIC):
231 bp->b_ops = &xfs_dir3_block_buf_ops;
232 bp->b_ops->verify_read(bp);
234 case cpu_to_be32(XFS_DIR2_DATA_MAGIC):
235 case cpu_to_be32(XFS_DIR3_DATA_MAGIC):
236 xfs_dir3_data_verify(bp);
239 XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp, hdr);
240 xfs_buf_ioerror(bp, EFSCORRUPTED);
246 xfs_dir3_data_read_verify(
249 struct xfs_mount *mp = bp->b_target->bt_mount;
251 if ((xfs_sb_version_hascrc(&mp->m_sb) &&
252 !xfs_verify_cksum(bp->b_addr, BBTOB(bp->b_length),
253 XFS_DIR3_DATA_CRC_OFF)) ||
254 !xfs_dir3_data_verify(bp)) {
255 XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp, bp->b_addr);
256 xfs_buf_ioerror(bp, EFSCORRUPTED);
261 xfs_dir3_data_write_verify(
264 struct xfs_mount *mp = bp->b_target->bt_mount;
265 struct xfs_buf_log_item *bip = bp->b_fspriv;
266 struct xfs_dir3_blk_hdr *hdr3 = bp->b_addr;
268 if (!xfs_dir3_data_verify(bp)) {
269 XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp, bp->b_addr);
270 xfs_buf_ioerror(bp, EFSCORRUPTED);
274 if (!xfs_sb_version_hascrc(&mp->m_sb))
278 hdr3->lsn = cpu_to_be64(bip->bli_item.li_lsn);
280 xfs_update_cksum(bp->b_addr, BBTOB(bp->b_length), XFS_DIR3_DATA_CRC_OFF);
283 const struct xfs_buf_ops xfs_dir3_data_buf_ops = {
284 .verify_read = xfs_dir3_data_read_verify,
285 .verify_write = xfs_dir3_data_write_verify,
288 static const struct xfs_buf_ops xfs_dir3_data_reada_buf_ops = {
289 .verify_read = xfs_dir3_data_reada_verify,
290 .verify_write = xfs_dir3_data_write_verify,
296 struct xfs_trans *tp,
297 struct xfs_inode *dp,
299 xfs_daddr_t mapped_bno,
300 struct xfs_buf **bpp)
304 err = xfs_da_read_buf(tp, dp, bno, mapped_bno, bpp,
305 XFS_DATA_FORK, &xfs_dir3_data_buf_ops);
307 xfs_trans_buf_set_type(tp, *bpp, XFS_BLFT_DIR_DATA_BUF);
312 xfs_dir3_data_readahead(
313 struct xfs_trans *tp,
314 struct xfs_inode *dp,
316 xfs_daddr_t mapped_bno)
318 return xfs_da_reada_buf(tp, dp, bno, mapped_bno,
319 XFS_DATA_FORK, &xfs_dir3_data_reada_buf_ops);
323 * Given a data block and an unused entry from that block,
324 * return the bestfree entry if any that corresponds to it.
326 xfs_dir2_data_free_t *
327 xfs_dir2_data_freefind(
328 xfs_dir2_data_hdr_t *hdr, /* data block */
329 xfs_dir2_data_unused_t *dup) /* data unused entry */
331 xfs_dir2_data_free_t *dfp; /* bestfree entry */
332 xfs_dir2_data_aoff_t off; /* offset value needed */
333 struct xfs_dir2_data_free *bf;
334 #if defined(DEBUG) && defined(__KERNEL__)
335 int matched; /* matched the value */
336 int seenzero; /* saw a 0 bestfree entry */
339 off = (xfs_dir2_data_aoff_t)((char *)dup - (char *)hdr);
340 bf = xfs_dir3_data_bestfree_p(hdr);
342 #if defined(DEBUG) && defined(__KERNEL__)
344 * Validate some consistency in the bestfree table.
345 * Check order, non-overlapping entries, and if we find the
346 * one we're looking for it has to be exact.
348 ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
349 hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC) ||
350 hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) ||
351 hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC));
352 for (dfp = &bf[0], seenzero = matched = 0;
353 dfp < &bf[XFS_DIR2_DATA_FD_COUNT];
356 ASSERT(!dfp->length);
360 ASSERT(seenzero == 0);
361 if (be16_to_cpu(dfp->offset) == off) {
363 ASSERT(dfp->length == dup->length);
364 } else if (off < be16_to_cpu(dfp->offset))
365 ASSERT(off + be16_to_cpu(dup->length) <= be16_to_cpu(dfp->offset));
367 ASSERT(be16_to_cpu(dfp->offset) + be16_to_cpu(dfp->length) <= off);
368 ASSERT(matched || be16_to_cpu(dfp->length) >= be16_to_cpu(dup->length));
370 ASSERT(be16_to_cpu(dfp[-1].length) >= be16_to_cpu(dfp[0].length));
374 * If this is smaller than the smallest bestfree entry,
375 * it can't be there since they're sorted.
377 if (be16_to_cpu(dup->length) <
378 be16_to_cpu(bf[XFS_DIR2_DATA_FD_COUNT - 1].length))
381 * Look at the three bestfree entries for our guy.
383 for (dfp = &bf[0]; dfp < &bf[XFS_DIR2_DATA_FD_COUNT]; dfp++) {
386 if (be16_to_cpu(dfp->offset) == off)
390 * Didn't find it. This only happens if there are duplicate lengths.
396 * Insert an unused-space entry into the bestfree table.
398 xfs_dir2_data_free_t * /* entry inserted */
399 xfs_dir2_data_freeinsert(
400 xfs_dir2_data_hdr_t *hdr, /* data block pointer */
401 xfs_dir2_data_unused_t *dup, /* unused space */
402 int *loghead) /* log the data header (out) */
404 xfs_dir2_data_free_t *dfp; /* bestfree table pointer */
405 xfs_dir2_data_free_t new; /* new bestfree entry */
407 ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
408 hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) ||
409 hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC) ||
410 hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC));
412 dfp = xfs_dir3_data_bestfree_p(hdr);
413 new.length = dup->length;
414 new.offset = cpu_to_be16((char *)dup - (char *)hdr);
417 * Insert at position 0, 1, or 2; or not at all.
419 if (be16_to_cpu(new.length) > be16_to_cpu(dfp[0].length)) {
426 if (be16_to_cpu(new.length) > be16_to_cpu(dfp[1].length)) {
432 if (be16_to_cpu(new.length) > be16_to_cpu(dfp[2].length)) {
441 * Remove a bestfree entry from the table.
444 xfs_dir2_data_freeremove(
445 xfs_dir2_data_hdr_t *hdr, /* data block header */
446 xfs_dir2_data_free_t *dfp, /* bestfree entry pointer */
447 int *loghead) /* out: log data header */
449 struct xfs_dir2_data_free *bf;
451 ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
452 hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) ||
453 hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC) ||
454 hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC));
457 * It's the first entry, slide the next 2 up.
459 bf = xfs_dir3_data_bestfree_p(hdr);
465 * It's the second entry, slide the 3rd entry up.
467 else if (dfp == &bf[1])
470 * Must be the last entry.
473 ASSERT(dfp == &bf[2]);
475 * Clear the 3rd entry, must be zero now.
483 * Given a data block, reconstruct its bestfree map.
486 xfs_dir2_data_freescan(
487 xfs_mount_t *mp, /* filesystem mount point */
488 xfs_dir2_data_hdr_t *hdr, /* data block header */
489 int *loghead) /* out: log data header */
491 xfs_dir2_block_tail_t *btp; /* block tail */
492 xfs_dir2_data_entry_t *dep; /* active data entry */
493 xfs_dir2_data_unused_t *dup; /* unused data entry */
494 struct xfs_dir2_data_free *bf;
495 char *endp; /* end of block's data */
496 char *p; /* current entry pointer */
498 ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
499 hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC) ||
500 hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) ||
501 hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC));
504 * Start by clearing the table.
506 bf = xfs_dir3_data_bestfree_p(hdr);
507 memset(bf, 0, sizeof(*bf) * XFS_DIR2_DATA_FD_COUNT);
512 p = (char *)xfs_dir3_data_entry_p(hdr);
513 if (hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) ||
514 hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC)) {
515 btp = xfs_dir2_block_tail_p(mp, hdr);
516 endp = (char *)xfs_dir2_block_leaf_p(btp);
518 endp = (char *)hdr + mp->m_dirblksize;
520 * Loop over the block's entries.
523 dup = (xfs_dir2_data_unused_t *)p;
525 * If it's a free entry, insert it.
527 if (be16_to_cpu(dup->freetag) == XFS_DIR2_DATA_FREE_TAG) {
528 ASSERT((char *)dup - (char *)hdr ==
529 be16_to_cpu(*xfs_dir2_data_unused_tag_p(dup)));
530 xfs_dir2_data_freeinsert(hdr, dup, loghead);
531 p += be16_to_cpu(dup->length);
534 * For active entries, check their tags and skip them.
537 dep = (xfs_dir2_data_entry_t *)p;
538 ASSERT((char *)dep - (char *)hdr ==
539 be16_to_cpu(*xfs_dir2_data_entry_tag_p(dep)));
540 p += xfs_dir2_data_entsize(dep->namelen);
546 * Initialize a data block at the given block number in the directory.
547 * Give back the buffer for the created block.
551 xfs_da_args_t *args, /* directory operation args */
552 xfs_dir2_db_t blkno, /* logical dir block number */
553 struct xfs_buf **bpp) /* output block buffer */
555 struct xfs_buf *bp; /* block buffer */
556 xfs_dir2_data_hdr_t *hdr; /* data block header */
557 xfs_inode_t *dp; /* incore directory inode */
558 xfs_dir2_data_unused_t *dup; /* unused entry pointer */
559 struct xfs_dir2_data_free *bf;
560 int error; /* error return value */
561 int i; /* bestfree index */
562 xfs_mount_t *mp; /* filesystem mount point */
563 xfs_trans_t *tp; /* transaction pointer */
570 * Get the buffer set up for the block.
572 error = xfs_da_get_buf(tp, dp, xfs_dir2_db_to_da(mp, blkno), -1, &bp,
576 bp->b_ops = &xfs_dir3_data_buf_ops;
577 xfs_trans_buf_set_type(tp, bp, XFS_BLFT_DIR_DATA_BUF);
580 * Initialize the header.
583 if (xfs_sb_version_hascrc(&mp->m_sb)) {
584 struct xfs_dir3_blk_hdr *hdr3 = bp->b_addr;
586 memset(hdr3, 0, sizeof(*hdr3));
587 hdr3->magic = cpu_to_be32(XFS_DIR3_DATA_MAGIC);
588 hdr3->blkno = cpu_to_be64(bp->b_bn);
589 hdr3->owner = cpu_to_be64(dp->i_ino);
590 uuid_copy(&hdr3->uuid, &mp->m_sb.sb_uuid);
593 hdr->magic = cpu_to_be32(XFS_DIR2_DATA_MAGIC);
595 bf = xfs_dir3_data_bestfree_p(hdr);
596 bf[0].offset = cpu_to_be16(xfs_dir3_data_entry_offset(hdr));
597 for (i = 1; i < XFS_DIR2_DATA_FD_COUNT; i++) {
603 * Set up an unused entry for the block's body.
605 dup = xfs_dir3_data_unused_p(hdr);
606 dup->freetag = cpu_to_be16(XFS_DIR2_DATA_FREE_TAG);
608 t = mp->m_dirblksize - (uint)xfs_dir3_data_entry_offset(hdr);
609 bf[0].length = cpu_to_be16(t);
610 dup->length = cpu_to_be16(t);
611 *xfs_dir2_data_unused_tag_p(dup) = cpu_to_be16((char *)dup - (char *)hdr);
613 * Log it and return it.
615 xfs_dir2_data_log_header(tp, bp);
616 xfs_dir2_data_log_unused(tp, bp, dup);
622 * Log an active data entry from the block.
625 xfs_dir2_data_log_entry(
626 struct xfs_trans *tp,
628 xfs_dir2_data_entry_t *dep) /* data entry pointer */
630 xfs_dir2_data_hdr_t *hdr = bp->b_addr;
632 ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
633 hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC) ||
634 hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) ||
635 hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC));
637 xfs_trans_log_buf(tp, bp, (uint)((char *)dep - (char *)hdr),
638 (uint)((char *)(xfs_dir2_data_entry_tag_p(dep) + 1) -
643 * Log a data block header.
646 xfs_dir2_data_log_header(
647 struct xfs_trans *tp,
650 xfs_dir2_data_hdr_t *hdr = bp->b_addr;
652 ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
653 hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC) ||
654 hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) ||
655 hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC));
657 xfs_trans_log_buf(tp, bp, 0, xfs_dir3_data_entry_offset(hdr) - 1);
661 * Log a data unused entry.
664 xfs_dir2_data_log_unused(
665 struct xfs_trans *tp,
667 xfs_dir2_data_unused_t *dup) /* data unused pointer */
669 xfs_dir2_data_hdr_t *hdr = bp->b_addr;
671 ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
672 hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC) ||
673 hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) ||
674 hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC));
677 * Log the first part of the unused entry.
679 xfs_trans_log_buf(tp, bp, (uint)((char *)dup - (char *)hdr),
680 (uint)((char *)&dup->length + sizeof(dup->length) -
683 * Log the end (tag) of the unused entry.
685 xfs_trans_log_buf(tp, bp,
686 (uint)((char *)xfs_dir2_data_unused_tag_p(dup) - (char *)hdr),
687 (uint)((char *)xfs_dir2_data_unused_tag_p(dup) - (char *)hdr +
688 sizeof(xfs_dir2_data_off_t) - 1));
692 * Make a byte range in the data block unused.
693 * Its current contents are unimportant.
696 xfs_dir2_data_make_free(
697 struct xfs_trans *tp,
699 xfs_dir2_data_aoff_t offset, /* starting byte offset */
700 xfs_dir2_data_aoff_t len, /* length in bytes */
701 int *needlogp, /* out: log header */
702 int *needscanp) /* out: regen bestfree */
704 xfs_dir2_data_hdr_t *hdr; /* data block pointer */
705 xfs_dir2_data_free_t *dfp; /* bestfree pointer */
706 char *endptr; /* end of data area */
707 xfs_mount_t *mp; /* filesystem mount point */
708 int needscan; /* need to regen bestfree */
709 xfs_dir2_data_unused_t *newdup; /* new unused entry */
710 xfs_dir2_data_unused_t *postdup; /* unused entry after us */
711 xfs_dir2_data_unused_t *prevdup; /* unused entry before us */
712 struct xfs_dir2_data_free *bf;
718 * Figure out where the end of the data area is.
720 if (hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
721 hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC))
722 endptr = (char *)hdr + mp->m_dirblksize;
724 xfs_dir2_block_tail_t *btp; /* block tail */
726 ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) ||
727 hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC));
728 btp = xfs_dir2_block_tail_p(mp, hdr);
729 endptr = (char *)xfs_dir2_block_leaf_p(btp);
732 * If this isn't the start of the block, then back up to
733 * the previous entry and see if it's free.
735 if (offset > xfs_dir3_data_entry_offset(hdr)) {
736 __be16 *tagp; /* tag just before us */
738 tagp = (__be16 *)((char *)hdr + offset) - 1;
739 prevdup = (xfs_dir2_data_unused_t *)((char *)hdr + be16_to_cpu(*tagp));
740 if (be16_to_cpu(prevdup->freetag) != XFS_DIR2_DATA_FREE_TAG)
745 * If this isn't the end of the block, see if the entry after
748 if ((char *)hdr + offset + len < endptr) {
750 (xfs_dir2_data_unused_t *)((char *)hdr + offset + len);
751 if (be16_to_cpu(postdup->freetag) != XFS_DIR2_DATA_FREE_TAG)
755 ASSERT(*needscanp == 0);
758 * Previous and following entries are both free,
759 * merge everything into a single free entry.
761 bf = xfs_dir3_data_bestfree_p(hdr);
762 if (prevdup && postdup) {
763 xfs_dir2_data_free_t *dfp2; /* another bestfree pointer */
766 * See if prevdup and/or postdup are in bestfree table.
768 dfp = xfs_dir2_data_freefind(hdr, prevdup);
769 dfp2 = xfs_dir2_data_freefind(hdr, postdup);
771 * We need a rescan unless there are exactly 2 free entries
772 * namely our two. Then we know what's happening, otherwise
773 * since the third bestfree is there, there might be more
776 needscan = (bf[2].length != 0);
778 * Fix up the new big freespace.
780 be16_add_cpu(&prevdup->length, len + be16_to_cpu(postdup->length));
781 *xfs_dir2_data_unused_tag_p(prevdup) =
782 cpu_to_be16((char *)prevdup - (char *)hdr);
783 xfs_dir2_data_log_unused(tp, bp, prevdup);
786 * Has to be the case that entries 0 and 1 are
787 * dfp and dfp2 (don't know which is which), and
789 * Remove entry 1 first then entry 0.
797 xfs_dir2_data_freeremove(hdr, dfp2, needlogp);
798 xfs_dir2_data_freeremove(hdr, dfp, needlogp);
800 * Now insert the new entry.
802 dfp = xfs_dir2_data_freeinsert(hdr, prevdup, needlogp);
803 ASSERT(dfp == &bf[0]);
804 ASSERT(dfp->length == prevdup->length);
805 ASSERT(!dfp[1].length);
806 ASSERT(!dfp[2].length);
810 * The entry before us is free, merge with it.
813 dfp = xfs_dir2_data_freefind(hdr, prevdup);
814 be16_add_cpu(&prevdup->length, len);
815 *xfs_dir2_data_unused_tag_p(prevdup) =
816 cpu_to_be16((char *)prevdup - (char *)hdr);
817 xfs_dir2_data_log_unused(tp, bp, prevdup);
819 * If the previous entry was in the table, the new entry
820 * is longer, so it will be in the table too. Remove
821 * the old one and add the new one.
824 xfs_dir2_data_freeremove(hdr, dfp, needlogp);
825 xfs_dir2_data_freeinsert(hdr, prevdup, needlogp);
828 * Otherwise we need a scan if the new entry is big enough.
831 needscan = be16_to_cpu(prevdup->length) >
832 be16_to_cpu(bf[2].length);
836 * The following entry is free, merge with it.
839 dfp = xfs_dir2_data_freefind(hdr, postdup);
840 newdup = (xfs_dir2_data_unused_t *)((char *)hdr + offset);
841 newdup->freetag = cpu_to_be16(XFS_DIR2_DATA_FREE_TAG);
842 newdup->length = cpu_to_be16(len + be16_to_cpu(postdup->length));
843 *xfs_dir2_data_unused_tag_p(newdup) =
844 cpu_to_be16((char *)newdup - (char *)hdr);
845 xfs_dir2_data_log_unused(tp, bp, newdup);
847 * If the following entry was in the table, the new entry
848 * is longer, so it will be in the table too. Remove
849 * the old one and add the new one.
852 xfs_dir2_data_freeremove(hdr, dfp, needlogp);
853 xfs_dir2_data_freeinsert(hdr, newdup, needlogp);
856 * Otherwise we need a scan if the new entry is big enough.
859 needscan = be16_to_cpu(newdup->length) >
860 be16_to_cpu(bf[2].length);
864 * Neither neighbor is free. Make a new entry.
867 newdup = (xfs_dir2_data_unused_t *)((char *)hdr + offset);
868 newdup->freetag = cpu_to_be16(XFS_DIR2_DATA_FREE_TAG);
869 newdup->length = cpu_to_be16(len);
870 *xfs_dir2_data_unused_tag_p(newdup) =
871 cpu_to_be16((char *)newdup - (char *)hdr);
872 xfs_dir2_data_log_unused(tp, bp, newdup);
873 xfs_dir2_data_freeinsert(hdr, newdup, needlogp);
875 *needscanp = needscan;
879 * Take a byte range out of an existing unused space and make it un-free.
882 xfs_dir2_data_use_free(
883 struct xfs_trans *tp,
885 xfs_dir2_data_unused_t *dup, /* unused entry */
886 xfs_dir2_data_aoff_t offset, /* starting offset to use */
887 xfs_dir2_data_aoff_t len, /* length to use */
888 int *needlogp, /* out: need to log header */
889 int *needscanp) /* out: need regen bestfree */
891 xfs_dir2_data_hdr_t *hdr; /* data block header */
892 xfs_dir2_data_free_t *dfp; /* bestfree pointer */
893 int matchback; /* matches end of freespace */
894 int matchfront; /* matches start of freespace */
895 int needscan; /* need to regen bestfree */
896 xfs_dir2_data_unused_t *newdup; /* new unused entry */
897 xfs_dir2_data_unused_t *newdup2; /* another new unused entry */
898 int oldlen; /* old unused entry's length */
899 struct xfs_dir2_data_free *bf;
902 ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
903 hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC) ||
904 hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) ||
905 hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC));
906 ASSERT(be16_to_cpu(dup->freetag) == XFS_DIR2_DATA_FREE_TAG);
907 ASSERT(offset >= (char *)dup - (char *)hdr);
908 ASSERT(offset + len <= (char *)dup + be16_to_cpu(dup->length) - (char *)hdr);
909 ASSERT((char *)dup - (char *)hdr == be16_to_cpu(*xfs_dir2_data_unused_tag_p(dup)));
911 * Look up the entry in the bestfree table.
913 dfp = xfs_dir2_data_freefind(hdr, dup);
914 oldlen = be16_to_cpu(dup->length);
915 bf = xfs_dir3_data_bestfree_p(hdr);
916 ASSERT(dfp || oldlen <= be16_to_cpu(bf[2].length));
918 * Check for alignment with front and back of the entry.
920 matchfront = (char *)dup - (char *)hdr == offset;
921 matchback = (char *)dup + oldlen - (char *)hdr == offset + len;
922 ASSERT(*needscanp == 0);
925 * If we matched it exactly we just need to get rid of it from
926 * the bestfree table.
928 if (matchfront && matchback) {
930 needscan = (bf[2].offset != 0);
932 xfs_dir2_data_freeremove(hdr, dfp, needlogp);
936 * We match the first part of the entry.
937 * Make a new entry with the remaining freespace.
939 else if (matchfront) {
940 newdup = (xfs_dir2_data_unused_t *)((char *)hdr + offset + len);
941 newdup->freetag = cpu_to_be16(XFS_DIR2_DATA_FREE_TAG);
942 newdup->length = cpu_to_be16(oldlen - len);
943 *xfs_dir2_data_unused_tag_p(newdup) =
944 cpu_to_be16((char *)newdup - (char *)hdr);
945 xfs_dir2_data_log_unused(tp, bp, newdup);
947 * If it was in the table, remove it and add the new one.
950 xfs_dir2_data_freeremove(hdr, dfp, needlogp);
951 dfp = xfs_dir2_data_freeinsert(hdr, newdup, needlogp);
953 ASSERT(dfp->length == newdup->length);
954 ASSERT(be16_to_cpu(dfp->offset) == (char *)newdup - (char *)hdr);
956 * If we got inserted at the last slot,
957 * that means we don't know if there was a better
958 * choice for the last slot, or not. Rescan.
960 needscan = dfp == &bf[2];
964 * We match the last part of the entry.
965 * Trim the allocated space off the tail of the entry.
967 else if (matchback) {
969 newdup->length = cpu_to_be16(((char *)hdr + offset) - (char *)newdup);
970 *xfs_dir2_data_unused_tag_p(newdup) =
971 cpu_to_be16((char *)newdup - (char *)hdr);
972 xfs_dir2_data_log_unused(tp, bp, newdup);
974 * If it was in the table, remove it and add the new one.
977 xfs_dir2_data_freeremove(hdr, dfp, needlogp);
978 dfp = xfs_dir2_data_freeinsert(hdr, newdup, needlogp);
980 ASSERT(dfp->length == newdup->length);
981 ASSERT(be16_to_cpu(dfp->offset) == (char *)newdup - (char *)hdr);
983 * If we got inserted at the last slot,
984 * that means we don't know if there was a better
985 * choice for the last slot, or not. Rescan.
987 needscan = dfp == &bf[2];
991 * Poking out the middle of an entry.
992 * Make two new entries.
996 newdup->length = cpu_to_be16(((char *)hdr + offset) - (char *)newdup);
997 *xfs_dir2_data_unused_tag_p(newdup) =
998 cpu_to_be16((char *)newdup - (char *)hdr);
999 xfs_dir2_data_log_unused(tp, bp, newdup);
1000 newdup2 = (xfs_dir2_data_unused_t *)((char *)hdr + offset + len);
1001 newdup2->freetag = cpu_to_be16(XFS_DIR2_DATA_FREE_TAG);
1002 newdup2->length = cpu_to_be16(oldlen - len - be16_to_cpu(newdup->length));
1003 *xfs_dir2_data_unused_tag_p(newdup2) =
1004 cpu_to_be16((char *)newdup2 - (char *)hdr);
1005 xfs_dir2_data_log_unused(tp, bp, newdup2);
1007 * If the old entry was in the table, we need to scan
1008 * if the 3rd entry was valid, since these entries
1009 * are smaller than the old one.
1010 * If we don't need to scan that means there were 1 or 2
1011 * entries in the table, and removing the old and adding
1012 * the 2 new will work.
1015 needscan = (bf[2].length != 0);
1017 xfs_dir2_data_freeremove(hdr, dfp, needlogp);
1018 xfs_dir2_data_freeinsert(hdr, newdup, needlogp);
1019 xfs_dir2_data_freeinsert(hdr, newdup2,
1024 *needscanp = needscan;