2 * Copyright (c) 2000-2002,2005 Silicon Graphics, Inc.
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation.
9 * This program is distributed in the hope that it would be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write the Free Software Foundation,
16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 #include "xfs_types.h"
23 #include "xfs_trans.h"
27 #include "xfs_dmapi.h"
28 #include "xfs_mount.h"
29 #include "xfs_da_btree.h"
30 #include "xfs_bmap_btree.h"
31 #include "xfs_dir_sf.h"
32 #include "xfs_dir2_sf.h"
33 #include "xfs_attr_sf.h"
34 #include "xfs_dinode.h"
35 #include "xfs_inode.h"
36 #include "xfs_dir_leaf.h"
37 #include "xfs_dir2_data.h"
38 #include "xfs_dir2_leaf.h"
39 #include "xfs_dir2_block.h"
40 #include "xfs_error.h"
44 * Check the consistency of the data block.
45 * The input can also be a block-format directory.
46 * Pop an assert if we find anything bad.
50 xfs_inode_t *dp, /* incore inode pointer */
51 xfs_dabuf_t *bp) /* data block's buffer */
53 xfs_dir2_dataptr_t addr; /* addr for leaf lookup */
54 xfs_dir2_data_free_t *bf; /* bestfree table */
55 xfs_dir2_block_tail_t *btp=NULL; /* block tail */
56 int count; /* count of entries found */
57 xfs_dir2_data_t *d; /* data block pointer */
58 xfs_dir2_data_entry_t *dep; /* data entry */
59 xfs_dir2_data_free_t *dfp; /* bestfree entry */
60 xfs_dir2_data_unused_t *dup; /* unused entry */
61 char *endp; /* end of useful data */
62 int freeseen; /* mask of bestfrees seen */
63 xfs_dahash_t hash; /* hash of current name */
64 int i; /* leaf index */
65 int lastfree; /* last entry was unused */
66 xfs_dir2_leaf_entry_t *lep=NULL; /* block leaf entries */
67 xfs_mount_t *mp; /* filesystem mount point */
68 char *p; /* current data position */
69 int stale; /* count of stale leaves */
73 ASSERT(INT_GET(d->hdr.magic, ARCH_CONVERT) == XFS_DIR2_DATA_MAGIC ||
74 INT_GET(d->hdr.magic, ARCH_CONVERT) == XFS_DIR2_BLOCK_MAGIC);
77 if (INT_GET(d->hdr.magic, ARCH_CONVERT) == XFS_DIR2_BLOCK_MAGIC) {
78 btp = XFS_DIR2_BLOCK_TAIL_P(mp, (xfs_dir2_block_t *)d);
79 lep = XFS_DIR2_BLOCK_LEAF_P(btp);
82 endp = (char *)d + mp->m_dirblksize;
83 count = lastfree = freeseen = 0;
85 * Account for zero bestfree entries.
88 ASSERT(!bf[0].offset);
92 ASSERT(!bf[1].offset);
96 ASSERT(!bf[2].offset);
99 ASSERT(INT_GET(bf[0].length, ARCH_CONVERT) >= INT_GET(bf[1].length, ARCH_CONVERT));
100 ASSERT(INT_GET(bf[1].length, ARCH_CONVERT) >= INT_GET(bf[2].length, ARCH_CONVERT));
102 * Loop over the data/unused entries.
105 dup = (xfs_dir2_data_unused_t *)p;
107 * If it's unused, look for the space in the bestfree table.
108 * If we find it, account for that, else make sure it
109 * doesn't need to be there.
111 if (INT_GET(dup->freetag, ARCH_CONVERT) == XFS_DIR2_DATA_FREE_TAG) {
112 ASSERT(lastfree == 0);
113 ASSERT(INT_GET(*XFS_DIR2_DATA_UNUSED_TAG_P(dup), ARCH_CONVERT) ==
114 (char *)dup - (char *)d);
115 dfp = xfs_dir2_data_freefind(d, dup);
118 ASSERT((freeseen & (1 << i)) == 0);
121 ASSERT(INT_GET(dup->length, ARCH_CONVERT) <= INT_GET(bf[2].length, ARCH_CONVERT));
122 p += INT_GET(dup->length, ARCH_CONVERT);
127 * It's a real entry. Validate the fields.
128 * If this is a block directory then make sure it's
129 * in the leaf section of the block.
130 * The linear search is crude but this is DEBUG code.
132 dep = (xfs_dir2_data_entry_t *)p;
133 ASSERT(dep->namelen != 0);
134 ASSERT(xfs_dir_ino_validate(mp, INT_GET(dep->inumber, ARCH_CONVERT)) == 0);
135 ASSERT(INT_GET(*XFS_DIR2_DATA_ENTRY_TAG_P(dep), ARCH_CONVERT) ==
136 (char *)dep - (char *)d);
139 if (INT_GET(d->hdr.magic, ARCH_CONVERT) == XFS_DIR2_BLOCK_MAGIC) {
140 addr = XFS_DIR2_DB_OFF_TO_DATAPTR(mp, mp->m_dirdatablk,
141 (xfs_dir2_data_aoff_t)
142 ((char *)dep - (char *)d));
143 hash = xfs_da_hashname((char *)dep->name, dep->namelen);
144 for (i = 0; i < INT_GET(btp->count, ARCH_CONVERT); i++) {
145 if (INT_GET(lep[i].address, ARCH_CONVERT) == addr &&
146 INT_GET(lep[i].hashval, ARCH_CONVERT) == hash)
149 ASSERT(i < INT_GET(btp->count, ARCH_CONVERT));
151 p += XFS_DIR2_DATA_ENTSIZE(dep->namelen);
154 * Need to have seen all the entries and all the bestfree slots.
156 ASSERT(freeseen == 7);
157 if (INT_GET(d->hdr.magic, ARCH_CONVERT) == XFS_DIR2_BLOCK_MAGIC) {
158 for (i = stale = 0; i < INT_GET(btp->count, ARCH_CONVERT); i++) {
159 if (INT_GET(lep[i].address, ARCH_CONVERT) == XFS_DIR2_NULL_DATAPTR)
162 ASSERT(INT_GET(lep[i].hashval, ARCH_CONVERT) >= INT_GET(lep[i - 1].hashval, ARCH_CONVERT));
164 ASSERT(count == INT_GET(btp->count, ARCH_CONVERT) - INT_GET(btp->stale, ARCH_CONVERT));
165 ASSERT(stale == INT_GET(btp->stale, ARCH_CONVERT));
171 * Given a data block and an unused entry from that block,
172 * return the bestfree entry if any that corresponds to it.
174 xfs_dir2_data_free_t *
175 xfs_dir2_data_freefind(
176 xfs_dir2_data_t *d, /* data block */
177 xfs_dir2_data_unused_t *dup) /* data unused entry */
179 xfs_dir2_data_free_t *dfp; /* bestfree entry */
180 xfs_dir2_data_aoff_t off; /* offset value needed */
181 #if defined(DEBUG) && defined(__KERNEL__)
182 int matched; /* matched the value */
183 int seenzero; /* saw a 0 bestfree entry */
186 off = (xfs_dir2_data_aoff_t)((char *)dup - (char *)d);
187 #if defined(DEBUG) && defined(__KERNEL__)
189 * Validate some consistency in the bestfree table.
190 * Check order, non-overlapping entries, and if we find the
191 * one we're looking for it has to be exact.
193 ASSERT(INT_GET(d->hdr.magic, ARCH_CONVERT) == XFS_DIR2_DATA_MAGIC ||
194 INT_GET(d->hdr.magic, ARCH_CONVERT) == XFS_DIR2_BLOCK_MAGIC);
195 for (dfp = &d->hdr.bestfree[0], seenzero = matched = 0;
196 dfp < &d->hdr.bestfree[XFS_DIR2_DATA_FD_COUNT];
199 ASSERT(!dfp->length);
203 ASSERT(seenzero == 0);
204 if (INT_GET(dfp->offset, ARCH_CONVERT) == off) {
206 ASSERT(INT_GET(dfp->length, ARCH_CONVERT) == INT_GET(dup->length, ARCH_CONVERT));
207 } else if (off < INT_GET(dfp->offset, ARCH_CONVERT))
208 ASSERT(off + INT_GET(dup->length, ARCH_CONVERT) <= INT_GET(dfp->offset, ARCH_CONVERT));
210 ASSERT(INT_GET(dfp->offset, ARCH_CONVERT) + INT_GET(dfp->length, ARCH_CONVERT) <= off);
211 ASSERT(matched || INT_GET(dfp->length, ARCH_CONVERT) >= INT_GET(dup->length, ARCH_CONVERT));
212 if (dfp > &d->hdr.bestfree[0])
213 ASSERT(INT_GET(dfp[-1].length, ARCH_CONVERT) >= INT_GET(dfp[0].length, ARCH_CONVERT));
217 * If this is smaller than the smallest bestfree entry,
218 * it can't be there since they're sorted.
220 if (INT_GET(dup->length, ARCH_CONVERT) < INT_GET(d->hdr.bestfree[XFS_DIR2_DATA_FD_COUNT - 1].length, ARCH_CONVERT))
223 * Look at the three bestfree entries for our guy.
225 for (dfp = &d->hdr.bestfree[0];
226 dfp < &d->hdr.bestfree[XFS_DIR2_DATA_FD_COUNT];
230 if (INT_GET(dfp->offset, ARCH_CONVERT) == off)
234 * Didn't find it. This only happens if there are duplicate lengths.
240 * Insert an unused-space entry into the bestfree table.
242 xfs_dir2_data_free_t * /* entry inserted */
243 xfs_dir2_data_freeinsert(
244 xfs_dir2_data_t *d, /* data block pointer */
245 xfs_dir2_data_unused_t *dup, /* unused space */
246 int *loghead) /* log the data header (out) */
248 xfs_dir2_data_free_t *dfp; /* bestfree table pointer */
249 xfs_dir2_data_free_t new; /* new bestfree entry */
252 ASSERT(INT_GET(d->hdr.magic, ARCH_CONVERT) == XFS_DIR2_DATA_MAGIC ||
253 INT_GET(d->hdr.magic, ARCH_CONVERT) == XFS_DIR2_BLOCK_MAGIC);
255 dfp = d->hdr.bestfree;
256 INT_COPY(new.length, dup->length, ARCH_CONVERT);
257 INT_SET(new.offset, ARCH_CONVERT, (xfs_dir2_data_off_t)((char *)dup - (char *)d));
259 * Insert at position 0, 1, or 2; or not at all.
261 if (INT_GET(new.length, ARCH_CONVERT) > INT_GET(dfp[0].length, ARCH_CONVERT)) {
268 if (INT_GET(new.length, ARCH_CONVERT) > INT_GET(dfp[1].length, ARCH_CONVERT)) {
274 if (INT_GET(new.length, ARCH_CONVERT) > INT_GET(dfp[2].length, ARCH_CONVERT)) {
283 * Remove a bestfree entry from the table.
286 xfs_dir2_data_freeremove(
287 xfs_dir2_data_t *d, /* data block pointer */
288 xfs_dir2_data_free_t *dfp, /* bestfree entry pointer */
289 int *loghead) /* out: log data header */
292 ASSERT(INT_GET(d->hdr.magic, ARCH_CONVERT) == XFS_DIR2_DATA_MAGIC ||
293 INT_GET(d->hdr.magic, ARCH_CONVERT) == XFS_DIR2_BLOCK_MAGIC);
296 * It's the first entry, slide the next 2 up.
298 if (dfp == &d->hdr.bestfree[0]) {
299 d->hdr.bestfree[0] = d->hdr.bestfree[1];
300 d->hdr.bestfree[1] = d->hdr.bestfree[2];
303 * It's the second entry, slide the 3rd entry up.
305 else if (dfp == &d->hdr.bestfree[1])
306 d->hdr.bestfree[1] = d->hdr.bestfree[2];
308 * Must be the last entry.
311 ASSERT(dfp == &d->hdr.bestfree[2]);
313 * Clear the 3rd entry, must be zero now.
315 d->hdr.bestfree[2].length = 0;
316 d->hdr.bestfree[2].offset = 0;
321 * Given a data block, reconstruct its bestfree map.
324 xfs_dir2_data_freescan(
325 xfs_mount_t *mp, /* filesystem mount point */
326 xfs_dir2_data_t *d, /* data block pointer */
327 int *loghead, /* out: log data header */
328 char *aendp) /* in: caller's endp */
330 xfs_dir2_block_tail_t *btp; /* block tail */
331 xfs_dir2_data_entry_t *dep; /* active data entry */
332 xfs_dir2_data_unused_t *dup; /* unused data entry */
333 char *endp; /* end of block's data */
334 char *p; /* current entry pointer */
337 ASSERT(INT_GET(d->hdr.magic, ARCH_CONVERT) == XFS_DIR2_DATA_MAGIC ||
338 INT_GET(d->hdr.magic, ARCH_CONVERT) == XFS_DIR2_BLOCK_MAGIC);
341 * Start by clearing the table.
343 memset(d->hdr.bestfree, 0, sizeof(d->hdr.bestfree));
351 else if (INT_GET(d->hdr.magic, ARCH_CONVERT) == XFS_DIR2_BLOCK_MAGIC) {
352 btp = XFS_DIR2_BLOCK_TAIL_P(mp, (xfs_dir2_block_t *)d);
353 endp = (char *)XFS_DIR2_BLOCK_LEAF_P(btp);
355 endp = (char *)d + mp->m_dirblksize;
357 * Loop over the block's entries.
360 dup = (xfs_dir2_data_unused_t *)p;
362 * If it's a free entry, insert it.
364 if (INT_GET(dup->freetag, ARCH_CONVERT) == XFS_DIR2_DATA_FREE_TAG) {
365 ASSERT((char *)dup - (char *)d ==
366 INT_GET(*XFS_DIR2_DATA_UNUSED_TAG_P(dup), ARCH_CONVERT));
367 xfs_dir2_data_freeinsert(d, dup, loghead);
368 p += INT_GET(dup->length, ARCH_CONVERT);
371 * For active entries, check their tags and skip them.
374 dep = (xfs_dir2_data_entry_t *)p;
375 ASSERT((char *)dep - (char *)d ==
376 INT_GET(*XFS_DIR2_DATA_ENTRY_TAG_P(dep), ARCH_CONVERT));
377 p += XFS_DIR2_DATA_ENTSIZE(dep->namelen);
383 * Initialize a data block at the given block number in the directory.
384 * Give back the buffer for the created block.
388 xfs_da_args_t *args, /* directory operation args */
389 xfs_dir2_db_t blkno, /* logical dir block number */
390 xfs_dabuf_t **bpp) /* output block buffer */
392 xfs_dabuf_t *bp; /* block buffer */
393 xfs_dir2_data_t *d; /* pointer to block */
394 xfs_inode_t *dp; /* incore directory inode */
395 xfs_dir2_data_unused_t *dup; /* unused entry pointer */
396 int error; /* error return value */
397 int i; /* bestfree index */
398 xfs_mount_t *mp; /* filesystem mount point */
399 xfs_trans_t *tp; /* transaction pointer */
406 * Get the buffer set up for the block.
408 error = xfs_da_get_buf(tp, dp, XFS_DIR2_DB_TO_DA(mp, blkno), -1, &bp,
415 * Initialize the header.
418 INT_SET(d->hdr.magic, ARCH_CONVERT, XFS_DIR2_DATA_MAGIC);
419 INT_SET(d->hdr.bestfree[0].offset, ARCH_CONVERT, (xfs_dir2_data_off_t)sizeof(d->hdr));
420 for (i = 1; i < XFS_DIR2_DATA_FD_COUNT; i++) {
421 d->hdr.bestfree[i].length = 0;
422 d->hdr.bestfree[i].offset = 0;
425 * Set up an unused entry for the block's body.
427 dup = &d->u[0].unused;
428 INT_SET(dup->freetag, ARCH_CONVERT, XFS_DIR2_DATA_FREE_TAG);
430 t=mp->m_dirblksize - (uint)sizeof(d->hdr);
431 INT_SET(d->hdr.bestfree[0].length, ARCH_CONVERT, t);
432 INT_SET(dup->length, ARCH_CONVERT, t);
433 INT_SET(*XFS_DIR2_DATA_UNUSED_TAG_P(dup), ARCH_CONVERT,
434 (xfs_dir2_data_off_t)((char *)dup - (char *)d));
436 * Log it and return it.
438 xfs_dir2_data_log_header(tp, bp);
439 xfs_dir2_data_log_unused(tp, bp, dup);
445 * Log an active data entry from the block.
448 xfs_dir2_data_log_entry(
449 xfs_trans_t *tp, /* transaction pointer */
450 xfs_dabuf_t *bp, /* block buffer */
451 xfs_dir2_data_entry_t *dep) /* data entry pointer */
453 xfs_dir2_data_t *d; /* data block pointer */
456 ASSERT(INT_GET(d->hdr.magic, ARCH_CONVERT) == XFS_DIR2_DATA_MAGIC ||
457 INT_GET(d->hdr.magic, ARCH_CONVERT) == XFS_DIR2_BLOCK_MAGIC);
458 xfs_da_log_buf(tp, bp, (uint)((char *)dep - (char *)d),
459 (uint)((char *)(XFS_DIR2_DATA_ENTRY_TAG_P(dep) + 1) -
464 * Log a data block header.
467 xfs_dir2_data_log_header(
468 xfs_trans_t *tp, /* transaction pointer */
469 xfs_dabuf_t *bp) /* block buffer */
471 xfs_dir2_data_t *d; /* data block pointer */
474 ASSERT(INT_GET(d->hdr.magic, ARCH_CONVERT) == XFS_DIR2_DATA_MAGIC ||
475 INT_GET(d->hdr.magic, ARCH_CONVERT) == XFS_DIR2_BLOCK_MAGIC);
476 xfs_da_log_buf(tp, bp, (uint)((char *)&d->hdr - (char *)d),
477 (uint)(sizeof(d->hdr) - 1));
481 * Log a data unused entry.
484 xfs_dir2_data_log_unused(
485 xfs_trans_t *tp, /* transaction pointer */
486 xfs_dabuf_t *bp, /* block buffer */
487 xfs_dir2_data_unused_t *dup) /* data unused pointer */
489 xfs_dir2_data_t *d; /* data block pointer */
492 ASSERT(INT_GET(d->hdr.magic, ARCH_CONVERT) == XFS_DIR2_DATA_MAGIC ||
493 INT_GET(d->hdr.magic, ARCH_CONVERT) == XFS_DIR2_BLOCK_MAGIC);
495 * Log the first part of the unused entry.
497 xfs_da_log_buf(tp, bp, (uint)((char *)dup - (char *)d),
498 (uint)((char *)&dup->length + sizeof(dup->length) -
501 * Log the end (tag) of the unused entry.
503 xfs_da_log_buf(tp, bp,
504 (uint)((char *)XFS_DIR2_DATA_UNUSED_TAG_P(dup) - (char *)d),
505 (uint)((char *)XFS_DIR2_DATA_UNUSED_TAG_P(dup) - (char *)d +
506 sizeof(xfs_dir2_data_off_t) - 1));
510 * Make a byte range in the data block unused.
511 * Its current contents are unimportant.
514 xfs_dir2_data_make_free(
515 xfs_trans_t *tp, /* transaction pointer */
516 xfs_dabuf_t *bp, /* block buffer */
517 xfs_dir2_data_aoff_t offset, /* starting byte offset */
518 xfs_dir2_data_aoff_t len, /* length in bytes */
519 int *needlogp, /* out: log header */
520 int *needscanp) /* out: regen bestfree */
522 xfs_dir2_data_t *d; /* data block pointer */
523 xfs_dir2_data_free_t *dfp; /* bestfree pointer */
524 char *endptr; /* end of data area */
525 xfs_mount_t *mp; /* filesystem mount point */
526 int needscan; /* need to regen bestfree */
527 xfs_dir2_data_unused_t *newdup; /* new unused entry */
528 xfs_dir2_data_unused_t *postdup; /* unused entry after us */
529 xfs_dir2_data_unused_t *prevdup; /* unused entry before us */
534 * Figure out where the end of the data area is.
536 if (INT_GET(d->hdr.magic, ARCH_CONVERT) == XFS_DIR2_DATA_MAGIC)
537 endptr = (char *)d + mp->m_dirblksize;
539 xfs_dir2_block_tail_t *btp; /* block tail */
541 ASSERT(INT_GET(d->hdr.magic, ARCH_CONVERT) == XFS_DIR2_BLOCK_MAGIC);
542 btp = XFS_DIR2_BLOCK_TAIL_P(mp, (xfs_dir2_block_t *)d);
543 endptr = (char *)XFS_DIR2_BLOCK_LEAF_P(btp);
546 * If this isn't the start of the block, then back up to
547 * the previous entry and see if it's free.
549 if (offset > sizeof(d->hdr)) {
550 xfs_dir2_data_off_t *tagp; /* tag just before us */
552 tagp = (xfs_dir2_data_off_t *)((char *)d + offset) - 1;
553 prevdup = (xfs_dir2_data_unused_t *)((char *)d + INT_GET(*tagp, ARCH_CONVERT));
554 if (INT_GET(prevdup->freetag, ARCH_CONVERT) != XFS_DIR2_DATA_FREE_TAG)
559 * If this isn't the end of the block, see if the entry after
562 if ((char *)d + offset + len < endptr) {
564 (xfs_dir2_data_unused_t *)((char *)d + offset + len);
565 if (INT_GET(postdup->freetag, ARCH_CONVERT) != XFS_DIR2_DATA_FREE_TAG)
569 ASSERT(*needscanp == 0);
572 * Previous and following entries are both free,
573 * merge everything into a single free entry.
575 if (prevdup && postdup) {
576 xfs_dir2_data_free_t *dfp2; /* another bestfree pointer */
579 * See if prevdup and/or postdup are in bestfree table.
581 dfp = xfs_dir2_data_freefind(d, prevdup);
582 dfp2 = xfs_dir2_data_freefind(d, postdup);
584 * We need a rescan unless there are exactly 2 free entries
585 * namely our two. Then we know what's happening, otherwise
586 * since the third bestfree is there, there might be more
589 needscan = d->hdr.bestfree[2].length;
591 * Fix up the new big freespace.
593 INT_MOD(prevdup->length, ARCH_CONVERT, len + INT_GET(postdup->length, ARCH_CONVERT));
594 INT_SET(*XFS_DIR2_DATA_UNUSED_TAG_P(prevdup), ARCH_CONVERT,
595 (xfs_dir2_data_off_t)((char *)prevdup - (char *)d));
596 xfs_dir2_data_log_unused(tp, bp, prevdup);
599 * Has to be the case that entries 0 and 1 are
600 * dfp and dfp2 (don't know which is which), and
602 * Remove entry 1 first then entry 0.
605 if (dfp == &d->hdr.bestfree[1]) {
606 dfp = &d->hdr.bestfree[0];
608 dfp2 = &d->hdr.bestfree[1];
610 xfs_dir2_data_freeremove(d, dfp2, needlogp);
611 xfs_dir2_data_freeremove(d, dfp, needlogp);
613 * Now insert the new entry.
615 dfp = xfs_dir2_data_freeinsert(d, prevdup, needlogp);
616 ASSERT(dfp == &d->hdr.bestfree[0]);
617 ASSERT(INT_GET(dfp->length, ARCH_CONVERT) == INT_GET(prevdup->length, ARCH_CONVERT));
618 ASSERT(!dfp[1].length);
619 ASSERT(!dfp[2].length);
623 * The entry before us is free, merge with it.
626 dfp = xfs_dir2_data_freefind(d, prevdup);
627 INT_MOD(prevdup->length, ARCH_CONVERT, len);
628 INT_SET(*XFS_DIR2_DATA_UNUSED_TAG_P(prevdup), ARCH_CONVERT,
629 (xfs_dir2_data_off_t)((char *)prevdup - (char *)d));
630 xfs_dir2_data_log_unused(tp, bp, prevdup);
632 * If the previous entry was in the table, the new entry
633 * is longer, so it will be in the table too. Remove
634 * the old one and add the new one.
637 xfs_dir2_data_freeremove(d, dfp, needlogp);
638 (void)xfs_dir2_data_freeinsert(d, prevdup, needlogp);
641 * Otherwise we need a scan if the new entry is big enough.
644 needscan = INT_GET(prevdup->length, ARCH_CONVERT) > INT_GET(d->hdr.bestfree[2].length, ARCH_CONVERT);
647 * The following entry is free, merge with it.
650 dfp = xfs_dir2_data_freefind(d, postdup);
651 newdup = (xfs_dir2_data_unused_t *)((char *)d + offset);
652 INT_SET(newdup->freetag, ARCH_CONVERT, XFS_DIR2_DATA_FREE_TAG);
653 INT_SET(newdup->length, ARCH_CONVERT, len + INT_GET(postdup->length, ARCH_CONVERT));
654 INT_SET(*XFS_DIR2_DATA_UNUSED_TAG_P(newdup), ARCH_CONVERT,
655 (xfs_dir2_data_off_t)((char *)newdup - (char *)d));
656 xfs_dir2_data_log_unused(tp, bp, newdup);
658 * If the following entry was in the table, the new entry
659 * is longer, so it will be in the table too. Remove
660 * the old one and add the new one.
663 xfs_dir2_data_freeremove(d, dfp, needlogp);
664 (void)xfs_dir2_data_freeinsert(d, newdup, needlogp);
667 * Otherwise we need a scan if the new entry is big enough.
670 needscan = INT_GET(newdup->length, ARCH_CONVERT) > INT_GET(d->hdr.bestfree[2].length, ARCH_CONVERT);
673 * Neither neighbor is free. Make a new entry.
676 newdup = (xfs_dir2_data_unused_t *)((char *)d + offset);
677 INT_SET(newdup->freetag, ARCH_CONVERT, XFS_DIR2_DATA_FREE_TAG);
678 INT_SET(newdup->length, ARCH_CONVERT, len);
679 INT_SET(*XFS_DIR2_DATA_UNUSED_TAG_P(newdup), ARCH_CONVERT,
680 (xfs_dir2_data_off_t)((char *)newdup - (char *)d));
681 xfs_dir2_data_log_unused(tp, bp, newdup);
682 (void)xfs_dir2_data_freeinsert(d, newdup, needlogp);
684 *needscanp = needscan;
688 * Take a byte range out of an existing unused space and make it un-free.
691 xfs_dir2_data_use_free(
692 xfs_trans_t *tp, /* transaction pointer */
693 xfs_dabuf_t *bp, /* data block buffer */
694 xfs_dir2_data_unused_t *dup, /* unused entry */
695 xfs_dir2_data_aoff_t offset, /* starting offset to use */
696 xfs_dir2_data_aoff_t len, /* length to use */
697 int *needlogp, /* out: need to log header */
698 int *needscanp) /* out: need regen bestfree */
700 xfs_dir2_data_t *d; /* data block */
701 xfs_dir2_data_free_t *dfp; /* bestfree pointer */
702 int matchback; /* matches end of freespace */
703 int matchfront; /* matches start of freespace */
704 int needscan; /* need to regen bestfree */
705 xfs_dir2_data_unused_t *newdup; /* new unused entry */
706 xfs_dir2_data_unused_t *newdup2; /* another new unused entry */
707 int oldlen; /* old unused entry's length */
710 ASSERT(INT_GET(d->hdr.magic, ARCH_CONVERT) == XFS_DIR2_DATA_MAGIC ||
711 INT_GET(d->hdr.magic, ARCH_CONVERT) == XFS_DIR2_BLOCK_MAGIC);
712 ASSERT(INT_GET(dup->freetag, ARCH_CONVERT) == XFS_DIR2_DATA_FREE_TAG);
713 ASSERT(offset >= (char *)dup - (char *)d);
714 ASSERT(offset + len <= (char *)dup + INT_GET(dup->length, ARCH_CONVERT) - (char *)d);
715 ASSERT((char *)dup - (char *)d == INT_GET(*XFS_DIR2_DATA_UNUSED_TAG_P(dup), ARCH_CONVERT));
717 * Look up the entry in the bestfree table.
719 dfp = xfs_dir2_data_freefind(d, dup);
720 oldlen = INT_GET(dup->length, ARCH_CONVERT);
721 ASSERT(dfp || oldlen <= INT_GET(d->hdr.bestfree[2].length, ARCH_CONVERT));
723 * Check for alignment with front and back of the entry.
725 matchfront = (char *)dup - (char *)d == offset;
726 matchback = (char *)dup + oldlen - (char *)d == offset + len;
727 ASSERT(*needscanp == 0);
730 * If we matched it exactly we just need to get rid of it from
731 * the bestfree table.
733 if (matchfront && matchback) {
735 needscan = d->hdr.bestfree[2].offset;
737 xfs_dir2_data_freeremove(d, dfp, needlogp);
741 * We match the first part of the entry.
742 * Make a new entry with the remaining freespace.
744 else if (matchfront) {
745 newdup = (xfs_dir2_data_unused_t *)((char *)d + offset + len);
746 INT_SET(newdup->freetag, ARCH_CONVERT, XFS_DIR2_DATA_FREE_TAG);
747 INT_SET(newdup->length, ARCH_CONVERT, oldlen - len);
748 INT_SET(*XFS_DIR2_DATA_UNUSED_TAG_P(newdup), ARCH_CONVERT,
749 (xfs_dir2_data_off_t)((char *)newdup - (char *)d));
750 xfs_dir2_data_log_unused(tp, bp, newdup);
752 * If it was in the table, remove it and add the new one.
755 xfs_dir2_data_freeremove(d, dfp, needlogp);
756 dfp = xfs_dir2_data_freeinsert(d, newdup, needlogp);
758 ASSERT(INT_GET(dfp->length, ARCH_CONVERT) == INT_GET(newdup->length, ARCH_CONVERT));
759 ASSERT(INT_GET(dfp->offset, ARCH_CONVERT) == (char *)newdup - (char *)d);
761 * If we got inserted at the last slot,
762 * that means we don't know if there was a better
763 * choice for the last slot, or not. Rescan.
765 needscan = dfp == &d->hdr.bestfree[2];
769 * We match the last part of the entry.
770 * Trim the allocated space off the tail of the entry.
772 else if (matchback) {
774 INT_SET(newdup->length, ARCH_CONVERT, (xfs_dir2_data_off_t)
775 (((char *)d + offset) - (char *)newdup));
776 INT_SET(*XFS_DIR2_DATA_UNUSED_TAG_P(newdup), ARCH_CONVERT,
777 (xfs_dir2_data_off_t)((char *)newdup - (char *)d));
778 xfs_dir2_data_log_unused(tp, bp, newdup);
780 * If it was in the table, remove it and add the new one.
783 xfs_dir2_data_freeremove(d, dfp, needlogp);
784 dfp = xfs_dir2_data_freeinsert(d, newdup, needlogp);
786 ASSERT(INT_GET(dfp->length, ARCH_CONVERT) == INT_GET(newdup->length, ARCH_CONVERT));
787 ASSERT(INT_GET(dfp->offset, ARCH_CONVERT) == (char *)newdup - (char *)d);
789 * If we got inserted at the last slot,
790 * that means we don't know if there was a better
791 * choice for the last slot, or not. Rescan.
793 needscan = dfp == &d->hdr.bestfree[2];
797 * Poking out the middle of an entry.
798 * Make two new entries.
802 INT_SET(newdup->length, ARCH_CONVERT, (xfs_dir2_data_off_t)
803 (((char *)d + offset) - (char *)newdup));
804 INT_SET(*XFS_DIR2_DATA_UNUSED_TAG_P(newdup), ARCH_CONVERT,
805 (xfs_dir2_data_off_t)((char *)newdup - (char *)d));
806 xfs_dir2_data_log_unused(tp, bp, newdup);
807 newdup2 = (xfs_dir2_data_unused_t *)((char *)d + offset + len);
808 INT_SET(newdup2->freetag, ARCH_CONVERT, XFS_DIR2_DATA_FREE_TAG);
809 INT_SET(newdup2->length, ARCH_CONVERT, oldlen - len - INT_GET(newdup->length, ARCH_CONVERT));
810 INT_SET(*XFS_DIR2_DATA_UNUSED_TAG_P(newdup2), ARCH_CONVERT,
811 (xfs_dir2_data_off_t)((char *)newdup2 - (char *)d));
812 xfs_dir2_data_log_unused(tp, bp, newdup2);
814 * If the old entry was in the table, we need to scan
815 * if the 3rd entry was valid, since these entries
816 * are smaller than the old one.
817 * If we don't need to scan that means there were 1 or 2
818 * entries in the table, and removing the old and adding
819 * the 2 new will work.
822 needscan = d->hdr.bestfree[2].length;
824 xfs_dir2_data_freeremove(d, dfp, needlogp);
825 (void)xfs_dir2_data_freeinsert(d, newdup,
827 (void)xfs_dir2_data_freeinsert(d, newdup2,
832 *needscanp = needscan;