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_mount.h"
28 #include "xfs_da_btree.h"
29 #include "xfs_bmap_btree.h"
30 #include "xfs_dir2_sf.h"
31 #include "xfs_attr_sf.h"
32 #include "xfs_dinode.h"
33 #include "xfs_inode.h"
34 #include "xfs_dir2_data.h"
35 #include "xfs_dir2_leaf.h"
36 #include "xfs_dir2_block.h"
37 #include "xfs_error.h"
41 * Check the consistency of the data block.
42 * The input can also be a block-format directory.
43 * Pop an assert if we find anything bad.
47 xfs_inode_t *dp, /* incore inode pointer */
48 xfs_dabuf_t *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_t *d; /* data block pointer */
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 */
71 ASSERT(be32_to_cpu(d->hdr.magic) == XFS_DIR2_DATA_MAGIC ||
72 be32_to_cpu(d->hdr.magic) == XFS_DIR2_BLOCK_MAGIC);
75 if (be32_to_cpu(d->hdr.magic) == XFS_DIR2_BLOCK_MAGIC) {
76 btp = xfs_dir2_block_tail_p(mp, (xfs_dir2_block_t *)d);
77 lep = xfs_dir2_block_leaf_p(btp);
80 endp = (char *)d + mp->m_dirblksize;
81 count = lastfree = freeseen = 0;
83 * Account for zero bestfree entries.
86 ASSERT(!bf[0].offset);
90 ASSERT(!bf[1].offset);
94 ASSERT(!bf[2].offset);
97 ASSERT(be16_to_cpu(bf[0].length) >= be16_to_cpu(bf[1].length));
98 ASSERT(be16_to_cpu(bf[1].length) >= be16_to_cpu(bf[2].length));
100 * Loop over the data/unused entries.
103 dup = (xfs_dir2_data_unused_t *)p;
105 * If it's unused, look for the space in the bestfree table.
106 * If we find it, account for that, else make sure it
107 * doesn't need to be there.
109 if (be16_to_cpu(dup->freetag) == XFS_DIR2_DATA_FREE_TAG) {
110 ASSERT(lastfree == 0);
111 ASSERT(be16_to_cpu(*xfs_dir2_data_unused_tag_p(dup)) ==
112 (char *)dup - (char *)d);
113 dfp = xfs_dir2_data_freefind(d, dup);
116 ASSERT((freeseen & (1 << i)) == 0);
119 ASSERT(be16_to_cpu(dup->length) <=
120 be16_to_cpu(bf[2].length));
122 p += be16_to_cpu(dup->length);
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, be64_to_cpu(dep->inumber)) == 0);
135 ASSERT(be16_to_cpu(*xfs_dir2_data_entry_tag_p(dep)) ==
136 (char *)dep - (char *)d);
139 if (be32_to_cpu(d->hdr.magic) == 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 name.name = dep->name;
144 name.len = dep->namelen;
145 hash = mp->m_dirnameops->hashname(&name);
146 for (i = 0; i < be32_to_cpu(btp->count); i++) {
147 if (be32_to_cpu(lep[i].address) == addr &&
148 be32_to_cpu(lep[i].hashval) == hash)
151 ASSERT(i < be32_to_cpu(btp->count));
153 p += xfs_dir2_data_entsize(dep->namelen);
156 * Need to have seen all the entries and all the bestfree slots.
158 ASSERT(freeseen == 7);
159 if (be32_to_cpu(d->hdr.magic) == XFS_DIR2_BLOCK_MAGIC) {
160 for (i = stale = 0; i < be32_to_cpu(btp->count); i++) {
161 if (be32_to_cpu(lep[i].address) == XFS_DIR2_NULL_DATAPTR)
164 ASSERT(be32_to_cpu(lep[i].hashval) >= be32_to_cpu(lep[i - 1].hashval));
166 ASSERT(count == be32_to_cpu(btp->count) - be32_to_cpu(btp->stale));
167 ASSERT(stale == be32_to_cpu(btp->stale));
173 * Given a data block and an unused entry from that block,
174 * return the bestfree entry if any that corresponds to it.
176 xfs_dir2_data_free_t *
177 xfs_dir2_data_freefind(
178 xfs_dir2_data_t *d, /* data block */
179 xfs_dir2_data_unused_t *dup) /* data unused entry */
181 xfs_dir2_data_free_t *dfp; /* bestfree entry */
182 xfs_dir2_data_aoff_t off; /* offset value needed */
183 #if defined(DEBUG) && defined(__KERNEL__)
184 int matched; /* matched the value */
185 int seenzero; /* saw a 0 bestfree entry */
188 off = (xfs_dir2_data_aoff_t)((char *)dup - (char *)d);
189 #if defined(DEBUG) && defined(__KERNEL__)
191 * Validate some consistency in the bestfree table.
192 * Check order, non-overlapping entries, and if we find the
193 * one we're looking for it has to be exact.
195 ASSERT(be32_to_cpu(d->hdr.magic) == XFS_DIR2_DATA_MAGIC ||
196 be32_to_cpu(d->hdr.magic) == XFS_DIR2_BLOCK_MAGIC);
197 for (dfp = &d->hdr.bestfree[0], seenzero = matched = 0;
198 dfp < &d->hdr.bestfree[XFS_DIR2_DATA_FD_COUNT];
201 ASSERT(!dfp->length);
205 ASSERT(seenzero == 0);
206 if (be16_to_cpu(dfp->offset) == off) {
208 ASSERT(dfp->length == dup->length);
209 } else if (off < be16_to_cpu(dfp->offset))
210 ASSERT(off + be16_to_cpu(dup->length) <= be16_to_cpu(dfp->offset));
212 ASSERT(be16_to_cpu(dfp->offset) + be16_to_cpu(dfp->length) <= off);
213 ASSERT(matched || be16_to_cpu(dfp->length) >= be16_to_cpu(dup->length));
214 if (dfp > &d->hdr.bestfree[0])
215 ASSERT(be16_to_cpu(dfp[-1].length) >= be16_to_cpu(dfp[0].length));
219 * If this is smaller than the smallest bestfree entry,
220 * it can't be there since they're sorted.
222 if (be16_to_cpu(dup->length) <
223 be16_to_cpu(d->hdr.bestfree[XFS_DIR2_DATA_FD_COUNT - 1].length))
226 * Look at the three bestfree entries for our guy.
228 for (dfp = &d->hdr.bestfree[0];
229 dfp < &d->hdr.bestfree[XFS_DIR2_DATA_FD_COUNT];
233 if (be16_to_cpu(dfp->offset) == off)
237 * Didn't find it. This only happens if there are duplicate lengths.
243 * Insert an unused-space entry into the bestfree table.
245 xfs_dir2_data_free_t * /* entry inserted */
246 xfs_dir2_data_freeinsert(
247 xfs_dir2_data_t *d, /* data block pointer */
248 xfs_dir2_data_unused_t *dup, /* unused space */
249 int *loghead) /* log the data header (out) */
251 xfs_dir2_data_free_t *dfp; /* bestfree table pointer */
252 xfs_dir2_data_free_t new; /* new bestfree entry */
255 ASSERT(be32_to_cpu(d->hdr.magic) == XFS_DIR2_DATA_MAGIC ||
256 be32_to_cpu(d->hdr.magic) == XFS_DIR2_BLOCK_MAGIC);
258 dfp = d->hdr.bestfree;
259 new.length = dup->length;
260 new.offset = cpu_to_be16((char *)dup - (char *)d);
262 * Insert at position 0, 1, or 2; or not at all.
264 if (be16_to_cpu(new.length) > be16_to_cpu(dfp[0].length)) {
271 if (be16_to_cpu(new.length) > be16_to_cpu(dfp[1].length)) {
277 if (be16_to_cpu(new.length) > be16_to_cpu(dfp[2].length)) {
286 * Remove a bestfree entry from the table.
289 xfs_dir2_data_freeremove(
290 xfs_dir2_data_t *d, /* data block pointer */
291 xfs_dir2_data_free_t *dfp, /* bestfree entry pointer */
292 int *loghead) /* out: log data header */
295 ASSERT(be32_to_cpu(d->hdr.magic) == XFS_DIR2_DATA_MAGIC ||
296 be32_to_cpu(d->hdr.magic) == XFS_DIR2_BLOCK_MAGIC);
299 * It's the first entry, slide the next 2 up.
301 if (dfp == &d->hdr.bestfree[0]) {
302 d->hdr.bestfree[0] = d->hdr.bestfree[1];
303 d->hdr.bestfree[1] = d->hdr.bestfree[2];
306 * It's the second entry, slide the 3rd entry up.
308 else if (dfp == &d->hdr.bestfree[1])
309 d->hdr.bestfree[1] = d->hdr.bestfree[2];
311 * Must be the last entry.
314 ASSERT(dfp == &d->hdr.bestfree[2]);
316 * Clear the 3rd entry, must be zero now.
318 d->hdr.bestfree[2].length = 0;
319 d->hdr.bestfree[2].offset = 0;
324 * Given a data block, reconstruct its bestfree map.
327 xfs_dir2_data_freescan(
328 xfs_mount_t *mp, /* filesystem mount point */
329 xfs_dir2_data_t *d, /* data block pointer */
330 int *loghead) /* out: log data header */
332 xfs_dir2_block_tail_t *btp; /* block tail */
333 xfs_dir2_data_entry_t *dep; /* active data entry */
334 xfs_dir2_data_unused_t *dup; /* unused data entry */
335 char *endp; /* end of block's data */
336 char *p; /* current entry pointer */
339 ASSERT(be32_to_cpu(d->hdr.magic) == XFS_DIR2_DATA_MAGIC ||
340 be32_to_cpu(d->hdr.magic) == XFS_DIR2_BLOCK_MAGIC);
343 * Start by clearing the table.
345 memset(d->hdr.bestfree, 0, sizeof(d->hdr.bestfree));
351 if (be32_to_cpu(d->hdr.magic) == 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 (be16_to_cpu(dup->freetag) == XFS_DIR2_DATA_FREE_TAG) {
365 ASSERT((char *)dup - (char *)d ==
366 be16_to_cpu(*xfs_dir2_data_unused_tag_p(dup)));
367 xfs_dir2_data_freeinsert(d, dup, loghead);
368 p += be16_to_cpu(dup->length);
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 be16_to_cpu(*xfs_dir2_data_entry_tag_p(dep)));
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 d->hdr.magic = cpu_to_be32(XFS_DIR2_DATA_MAGIC);
419 d->hdr.bestfree[0].offset = cpu_to_be16(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 dup->freetag = cpu_to_be16(XFS_DIR2_DATA_FREE_TAG);
430 t=mp->m_dirblksize - (uint)sizeof(d->hdr);
431 d->hdr.bestfree[0].length = cpu_to_be16(t);
432 dup->length = cpu_to_be16(t);
433 *xfs_dir2_data_unused_tag_p(dup) = cpu_to_be16((char *)dup - (char *)d);
435 * Log it and return it.
437 xfs_dir2_data_log_header(tp, bp);
438 xfs_dir2_data_log_unused(tp, bp, dup);
444 * Log an active data entry from the block.
447 xfs_dir2_data_log_entry(
448 xfs_trans_t *tp, /* transaction pointer */
449 xfs_dabuf_t *bp, /* block buffer */
450 xfs_dir2_data_entry_t *dep) /* data entry pointer */
452 xfs_dir2_data_t *d; /* data block pointer */
455 ASSERT(be32_to_cpu(d->hdr.magic) == XFS_DIR2_DATA_MAGIC ||
456 be32_to_cpu(d->hdr.magic) == XFS_DIR2_BLOCK_MAGIC);
457 xfs_da_log_buf(tp, bp, (uint)((char *)dep - (char *)d),
458 (uint)((char *)(xfs_dir2_data_entry_tag_p(dep) + 1) -
463 * Log a data block header.
466 xfs_dir2_data_log_header(
467 xfs_trans_t *tp, /* transaction pointer */
468 xfs_dabuf_t *bp) /* block buffer */
470 xfs_dir2_data_t *d; /* data block pointer */
473 ASSERT(be32_to_cpu(d->hdr.magic) == XFS_DIR2_DATA_MAGIC ||
474 be32_to_cpu(d->hdr.magic) == XFS_DIR2_BLOCK_MAGIC);
475 xfs_da_log_buf(tp, bp, (uint)((char *)&d->hdr - (char *)d),
476 (uint)(sizeof(d->hdr) - 1));
480 * Log a data unused entry.
483 xfs_dir2_data_log_unused(
484 xfs_trans_t *tp, /* transaction pointer */
485 xfs_dabuf_t *bp, /* block buffer */
486 xfs_dir2_data_unused_t *dup) /* data unused pointer */
488 xfs_dir2_data_t *d; /* data block pointer */
491 ASSERT(be32_to_cpu(d->hdr.magic) == XFS_DIR2_DATA_MAGIC ||
492 be32_to_cpu(d->hdr.magic) == XFS_DIR2_BLOCK_MAGIC);
494 * Log the first part of the unused entry.
496 xfs_da_log_buf(tp, bp, (uint)((char *)dup - (char *)d),
497 (uint)((char *)&dup->length + sizeof(dup->length) -
500 * Log the end (tag) of the unused entry.
502 xfs_da_log_buf(tp, bp,
503 (uint)((char *)xfs_dir2_data_unused_tag_p(dup) - (char *)d),
504 (uint)((char *)xfs_dir2_data_unused_tag_p(dup) - (char *)d +
505 sizeof(xfs_dir2_data_off_t) - 1));
509 * Make a byte range in the data block unused.
510 * Its current contents are unimportant.
513 xfs_dir2_data_make_free(
514 xfs_trans_t *tp, /* transaction pointer */
515 xfs_dabuf_t *bp, /* block buffer */
516 xfs_dir2_data_aoff_t offset, /* starting byte offset */
517 xfs_dir2_data_aoff_t len, /* length in bytes */
518 int *needlogp, /* out: log header */
519 int *needscanp) /* out: regen bestfree */
521 xfs_dir2_data_t *d; /* data block pointer */
522 xfs_dir2_data_free_t *dfp; /* bestfree pointer */
523 char *endptr; /* end of data area */
524 xfs_mount_t *mp; /* filesystem mount point */
525 int needscan; /* need to regen bestfree */
526 xfs_dir2_data_unused_t *newdup; /* new unused entry */
527 xfs_dir2_data_unused_t *postdup; /* unused entry after us */
528 xfs_dir2_data_unused_t *prevdup; /* unused entry before us */
533 * Figure out where the end of the data area is.
535 if (be32_to_cpu(d->hdr.magic) == XFS_DIR2_DATA_MAGIC)
536 endptr = (char *)d + mp->m_dirblksize;
538 xfs_dir2_block_tail_t *btp; /* block tail */
540 ASSERT(be32_to_cpu(d->hdr.magic) == XFS_DIR2_BLOCK_MAGIC);
541 btp = xfs_dir2_block_tail_p(mp, (xfs_dir2_block_t *)d);
542 endptr = (char *)xfs_dir2_block_leaf_p(btp);
545 * If this isn't the start of the block, then back up to
546 * the previous entry and see if it's free.
548 if (offset > sizeof(d->hdr)) {
549 __be16 *tagp; /* tag just before us */
551 tagp = (__be16 *)((char *)d + offset) - 1;
552 prevdup = (xfs_dir2_data_unused_t *)((char *)d + be16_to_cpu(*tagp));
553 if (be16_to_cpu(prevdup->freetag) != XFS_DIR2_DATA_FREE_TAG)
558 * If this isn't the end of the block, see if the entry after
561 if ((char *)d + offset + len < endptr) {
563 (xfs_dir2_data_unused_t *)((char *)d + offset + len);
564 if (be16_to_cpu(postdup->freetag) != XFS_DIR2_DATA_FREE_TAG)
568 ASSERT(*needscanp == 0);
571 * Previous and following entries are both free,
572 * merge everything into a single free entry.
574 if (prevdup && postdup) {
575 xfs_dir2_data_free_t *dfp2; /* another bestfree pointer */
578 * See if prevdup and/or postdup are in bestfree table.
580 dfp = xfs_dir2_data_freefind(d, prevdup);
581 dfp2 = xfs_dir2_data_freefind(d, postdup);
583 * We need a rescan unless there are exactly 2 free entries
584 * namely our two. Then we know what's happening, otherwise
585 * since the third bestfree is there, there might be more
588 needscan = (d->hdr.bestfree[2].length != 0);
590 * Fix up the new big freespace.
592 be16_add_cpu(&prevdup->length, len + be16_to_cpu(postdup->length));
593 *xfs_dir2_data_unused_tag_p(prevdup) =
594 cpu_to_be16((char *)prevdup - (char *)d);
595 xfs_dir2_data_log_unused(tp, bp, prevdup);
598 * Has to be the case that entries 0 and 1 are
599 * dfp and dfp2 (don't know which is which), and
601 * Remove entry 1 first then entry 0.
604 if (dfp == &d->hdr.bestfree[1]) {
605 dfp = &d->hdr.bestfree[0];
607 dfp2 = &d->hdr.bestfree[1];
609 xfs_dir2_data_freeremove(d, dfp2, needlogp);
610 xfs_dir2_data_freeremove(d, dfp, needlogp);
612 * Now insert the new entry.
614 dfp = xfs_dir2_data_freeinsert(d, prevdup, needlogp);
615 ASSERT(dfp == &d->hdr.bestfree[0]);
616 ASSERT(dfp->length == prevdup->length);
617 ASSERT(!dfp[1].length);
618 ASSERT(!dfp[2].length);
622 * The entry before us is free, merge with it.
625 dfp = xfs_dir2_data_freefind(d, prevdup);
626 be16_add_cpu(&prevdup->length, len);
627 *xfs_dir2_data_unused_tag_p(prevdup) =
628 cpu_to_be16((char *)prevdup - (char *)d);
629 xfs_dir2_data_log_unused(tp, bp, prevdup);
631 * If the previous entry was in the table, the new entry
632 * is longer, so it will be in the table too. Remove
633 * the old one and add the new one.
636 xfs_dir2_data_freeremove(d, dfp, needlogp);
637 (void)xfs_dir2_data_freeinsert(d, prevdup, needlogp);
640 * Otherwise we need a scan if the new entry is big enough.
643 needscan = be16_to_cpu(prevdup->length) >
644 be16_to_cpu(d->hdr.bestfree[2].length);
648 * The following entry is free, merge with it.
651 dfp = xfs_dir2_data_freefind(d, postdup);
652 newdup = (xfs_dir2_data_unused_t *)((char *)d + offset);
653 newdup->freetag = cpu_to_be16(XFS_DIR2_DATA_FREE_TAG);
654 newdup->length = cpu_to_be16(len + be16_to_cpu(postdup->length));
655 *xfs_dir2_data_unused_tag_p(newdup) =
656 cpu_to_be16((char *)newdup - (char *)d);
657 xfs_dir2_data_log_unused(tp, bp, newdup);
659 * If the following entry was in the table, the new entry
660 * is longer, so it will be in the table too. Remove
661 * the old one and add the new one.
664 xfs_dir2_data_freeremove(d, dfp, needlogp);
665 (void)xfs_dir2_data_freeinsert(d, newdup, needlogp);
668 * Otherwise we need a scan if the new entry is big enough.
671 needscan = be16_to_cpu(newdup->length) >
672 be16_to_cpu(d->hdr.bestfree[2].length);
676 * Neither neighbor is free. Make a new entry.
679 newdup = (xfs_dir2_data_unused_t *)((char *)d + offset);
680 newdup->freetag = cpu_to_be16(XFS_DIR2_DATA_FREE_TAG);
681 newdup->length = cpu_to_be16(len);
682 *xfs_dir2_data_unused_tag_p(newdup) =
683 cpu_to_be16((char *)newdup - (char *)d);
684 xfs_dir2_data_log_unused(tp, bp, newdup);
685 (void)xfs_dir2_data_freeinsert(d, newdup, needlogp);
687 *needscanp = needscan;
691 * Take a byte range out of an existing unused space and make it un-free.
694 xfs_dir2_data_use_free(
695 xfs_trans_t *tp, /* transaction pointer */
696 xfs_dabuf_t *bp, /* data block buffer */
697 xfs_dir2_data_unused_t *dup, /* unused entry */
698 xfs_dir2_data_aoff_t offset, /* starting offset to use */
699 xfs_dir2_data_aoff_t len, /* length to use */
700 int *needlogp, /* out: need to log header */
701 int *needscanp) /* out: need regen bestfree */
703 xfs_dir2_data_t *d; /* data block */
704 xfs_dir2_data_free_t *dfp; /* bestfree pointer */
705 int matchback; /* matches end of freespace */
706 int matchfront; /* matches start of freespace */
707 int needscan; /* need to regen bestfree */
708 xfs_dir2_data_unused_t *newdup; /* new unused entry */
709 xfs_dir2_data_unused_t *newdup2; /* another new unused entry */
710 int oldlen; /* old unused entry's length */
713 ASSERT(be32_to_cpu(d->hdr.magic) == XFS_DIR2_DATA_MAGIC ||
714 be32_to_cpu(d->hdr.magic) == XFS_DIR2_BLOCK_MAGIC);
715 ASSERT(be16_to_cpu(dup->freetag) == XFS_DIR2_DATA_FREE_TAG);
716 ASSERT(offset >= (char *)dup - (char *)d);
717 ASSERT(offset + len <= (char *)dup + be16_to_cpu(dup->length) - (char *)d);
718 ASSERT((char *)dup - (char *)d == be16_to_cpu(*xfs_dir2_data_unused_tag_p(dup)));
720 * Look up the entry in the bestfree table.
722 dfp = xfs_dir2_data_freefind(d, dup);
723 oldlen = be16_to_cpu(dup->length);
724 ASSERT(dfp || oldlen <= be16_to_cpu(d->hdr.bestfree[2].length));
726 * Check for alignment with front and back of the entry.
728 matchfront = (char *)dup - (char *)d == offset;
729 matchback = (char *)dup + oldlen - (char *)d == offset + len;
730 ASSERT(*needscanp == 0);
733 * If we matched it exactly we just need to get rid of it from
734 * the bestfree table.
736 if (matchfront && matchback) {
738 needscan = (d->hdr.bestfree[2].offset != 0);
740 xfs_dir2_data_freeremove(d, dfp, needlogp);
744 * We match the first part of the entry.
745 * Make a new entry with the remaining freespace.
747 else if (matchfront) {
748 newdup = (xfs_dir2_data_unused_t *)((char *)d + offset + len);
749 newdup->freetag = cpu_to_be16(XFS_DIR2_DATA_FREE_TAG);
750 newdup->length = cpu_to_be16(oldlen - len);
751 *xfs_dir2_data_unused_tag_p(newdup) =
752 cpu_to_be16((char *)newdup - (char *)d);
753 xfs_dir2_data_log_unused(tp, bp, newdup);
755 * If it was in the table, remove it and add the new one.
758 xfs_dir2_data_freeremove(d, dfp, needlogp);
759 dfp = xfs_dir2_data_freeinsert(d, newdup, needlogp);
761 ASSERT(dfp->length == newdup->length);
762 ASSERT(be16_to_cpu(dfp->offset) == (char *)newdup - (char *)d);
764 * If we got inserted at the last slot,
765 * that means we don't know if there was a better
766 * choice for the last slot, or not. Rescan.
768 needscan = dfp == &d->hdr.bestfree[2];
772 * We match the last part of the entry.
773 * Trim the allocated space off the tail of the entry.
775 else if (matchback) {
777 newdup->length = cpu_to_be16(((char *)d + offset) - (char *)newdup);
778 *xfs_dir2_data_unused_tag_p(newdup) =
779 cpu_to_be16((char *)newdup - (char *)d);
780 xfs_dir2_data_log_unused(tp, bp, newdup);
782 * If it was in the table, remove it and add the new one.
785 xfs_dir2_data_freeremove(d, dfp, needlogp);
786 dfp = xfs_dir2_data_freeinsert(d, newdup, needlogp);
788 ASSERT(dfp->length == newdup->length);
789 ASSERT(be16_to_cpu(dfp->offset) == (char *)newdup - (char *)d);
791 * If we got inserted at the last slot,
792 * that means we don't know if there was a better
793 * choice for the last slot, or not. Rescan.
795 needscan = dfp == &d->hdr.bestfree[2];
799 * Poking out the middle of an entry.
800 * Make two new entries.
804 newdup->length = cpu_to_be16(((char *)d + offset) - (char *)newdup);
805 *xfs_dir2_data_unused_tag_p(newdup) =
806 cpu_to_be16((char *)newdup - (char *)d);
807 xfs_dir2_data_log_unused(tp, bp, newdup);
808 newdup2 = (xfs_dir2_data_unused_t *)((char *)d + offset + len);
809 newdup2->freetag = cpu_to_be16(XFS_DIR2_DATA_FREE_TAG);
810 newdup2->length = cpu_to_be16(oldlen - len - be16_to_cpu(newdup->length));
811 *xfs_dir2_data_unused_tag_p(newdup2) =
812 cpu_to_be16((char *)newdup2 - (char *)d);
813 xfs_dir2_data_log_unused(tp, bp, newdup2);
815 * If the old entry was in the table, we need to scan
816 * if the 3rd entry was valid, since these entries
817 * are smaller than the old one.
818 * If we don't need to scan that means there were 1 or 2
819 * entries in the table, and removing the old and adding
820 * the 2 new will work.
823 needscan = (d->hdr.bestfree[2].length != 0);
825 xfs_dir2_data_freeremove(d, dfp, needlogp);
826 (void)xfs_dir2_data_freeinsert(d, newdup,
828 (void)xfs_dir2_data_freeinsert(d, newdup2,
833 *needscanp = needscan;