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_dinode.h"
32 #include "xfs_inode.h"
33 #include "xfs_dir2_data.h"
34 #include "xfs_dir2_leaf.h"
35 #include "xfs_dir2_block.h"
36 #include "xfs_error.h"
38 STATIC xfs_dir2_data_free_t *
39 xfs_dir2_data_freefind(xfs_dir2_data_hdr_t *hdr, xfs_dir2_data_unused_t *dup);
43 * Check the consistency of the data block.
44 * The input can also be a block-format directory.
45 * Pop an assert if we find anything bad.
49 xfs_inode_t *dp, /* incore inode pointer */
50 xfs_dabuf_t *bp) /* data block's buffer */
52 xfs_dir2_dataptr_t addr; /* addr for leaf lookup */
53 xfs_dir2_data_free_t *bf; /* bestfree table */
54 xfs_dir2_block_tail_t *btp=NULL; /* block tail */
55 int count; /* count of entries found */
56 xfs_dir2_data_hdr_t *hdr; /* data block header */
57 xfs_dir2_data_entry_t *dep; /* data entry */
58 xfs_dir2_data_free_t *dfp; /* bestfree entry */
59 xfs_dir2_data_unused_t *dup; /* unused entry */
60 char *endp; /* end of useful data */
61 int freeseen; /* mask of bestfrees seen */
62 xfs_dahash_t hash; /* hash of current name */
63 int i; /* leaf index */
64 int lastfree; /* last entry was unused */
65 xfs_dir2_leaf_entry_t *lep=NULL; /* block leaf entries */
66 xfs_mount_t *mp; /* filesystem mount point */
67 char *p; /* current data position */
68 int stale; /* count of stale leaves */
74 p = (char *)(hdr + 1);
76 if (be32_to_cpu(hdr->magic) == XFS_DIR2_BLOCK_MAGIC) {
77 btp = xfs_dir2_block_tail_p(mp, hdr);
78 lep = xfs_dir2_block_leaf_p(btp);
81 ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC));
82 endp = (char *)hdr + mp->m_dirblksize;
85 count = lastfree = freeseen = 0;
87 * Account for zero bestfree entries.
90 ASSERT(!bf[0].offset);
94 ASSERT(!bf[1].offset);
98 ASSERT(!bf[2].offset);
101 ASSERT(be16_to_cpu(bf[0].length) >= be16_to_cpu(bf[1].length));
102 ASSERT(be16_to_cpu(bf[1].length) >= be16_to_cpu(bf[2].length));
104 * Loop over the data/unused entries.
107 dup = (xfs_dir2_data_unused_t *)p;
109 * If it's unused, look for the space in the bestfree table.
110 * If we find it, account for that, else make sure it
111 * doesn't need to be there.
113 if (be16_to_cpu(dup->freetag) == XFS_DIR2_DATA_FREE_TAG) {
114 ASSERT(lastfree == 0);
115 ASSERT(be16_to_cpu(*xfs_dir2_data_unused_tag_p(dup)) ==
116 (char *)dup - (char *)hdr);
117 dfp = xfs_dir2_data_freefind(hdr, dup);
120 ASSERT((freeseen & (1 << i)) == 0);
123 ASSERT(be16_to_cpu(dup->length) <=
124 be16_to_cpu(bf[2].length));
126 p += be16_to_cpu(dup->length);
131 * It's a real entry. Validate the fields.
132 * If this is a block directory then make sure it's
133 * in the leaf section of the block.
134 * The linear search is crude but this is DEBUG code.
136 dep = (xfs_dir2_data_entry_t *)p;
137 ASSERT(dep->namelen != 0);
138 ASSERT(xfs_dir_ino_validate(mp, be64_to_cpu(dep->inumber)) == 0);
139 ASSERT(be16_to_cpu(*xfs_dir2_data_entry_tag_p(dep)) ==
140 (char *)dep - (char *)hdr);
143 if (be32_to_cpu(hdr->magic) == XFS_DIR2_BLOCK_MAGIC) {
144 addr = xfs_dir2_db_off_to_dataptr(mp, mp->m_dirdatablk,
145 (xfs_dir2_data_aoff_t)
146 ((char *)dep - (char *)hdr));
147 name.name = dep->name;
148 name.len = dep->namelen;
149 hash = mp->m_dirnameops->hashname(&name);
150 for (i = 0; i < be32_to_cpu(btp->count); i++) {
151 if (be32_to_cpu(lep[i].address) == addr &&
152 be32_to_cpu(lep[i].hashval) == hash)
155 ASSERT(i < be32_to_cpu(btp->count));
157 p += xfs_dir2_data_entsize(dep->namelen);
160 * Need to have seen all the entries and all the bestfree slots.
162 ASSERT(freeseen == 7);
163 if (be32_to_cpu(hdr->magic) == XFS_DIR2_BLOCK_MAGIC) {
164 for (i = stale = 0; i < be32_to_cpu(btp->count); i++) {
165 if (be32_to_cpu(lep[i].address) == XFS_DIR2_NULL_DATAPTR)
168 ASSERT(be32_to_cpu(lep[i].hashval) >= be32_to_cpu(lep[i - 1].hashval));
170 ASSERT(count == be32_to_cpu(btp->count) - be32_to_cpu(btp->stale));
171 ASSERT(stale == be32_to_cpu(btp->stale));
177 * Given a data block and an unused entry from that block,
178 * return the bestfree entry if any that corresponds to it.
180 STATIC xfs_dir2_data_free_t *
181 xfs_dir2_data_freefind(
182 xfs_dir2_data_hdr_t *hdr, /* data block */
183 xfs_dir2_data_unused_t *dup) /* data unused entry */
185 xfs_dir2_data_free_t *dfp; /* bestfree entry */
186 xfs_dir2_data_aoff_t off; /* offset value needed */
187 #if defined(DEBUG) && defined(__KERNEL__)
188 int matched; /* matched the value */
189 int seenzero; /* saw a 0 bestfree entry */
192 off = (xfs_dir2_data_aoff_t)((char *)dup - (char *)hdr);
193 #if defined(DEBUG) && defined(__KERNEL__)
195 * Validate some consistency in the bestfree table.
196 * Check order, non-overlapping entries, and if we find the
197 * one we're looking for it has to be exact.
199 ASSERT(be32_to_cpu(hdr->magic) == XFS_DIR2_DATA_MAGIC ||
200 be32_to_cpu(hdr->magic) == XFS_DIR2_BLOCK_MAGIC);
201 for (dfp = &hdr->bestfree[0], seenzero = matched = 0;
202 dfp < &hdr->bestfree[XFS_DIR2_DATA_FD_COUNT];
205 ASSERT(!dfp->length);
209 ASSERT(seenzero == 0);
210 if (be16_to_cpu(dfp->offset) == off) {
212 ASSERT(dfp->length == dup->length);
213 } else if (off < be16_to_cpu(dfp->offset))
214 ASSERT(off + be16_to_cpu(dup->length) <= be16_to_cpu(dfp->offset));
216 ASSERT(be16_to_cpu(dfp->offset) + be16_to_cpu(dfp->length) <= off);
217 ASSERT(matched || be16_to_cpu(dfp->length) >= be16_to_cpu(dup->length));
218 if (dfp > &hdr->bestfree[0])
219 ASSERT(be16_to_cpu(dfp[-1].length) >= be16_to_cpu(dfp[0].length));
223 * If this is smaller than the smallest bestfree entry,
224 * it can't be there since they're sorted.
226 if (be16_to_cpu(dup->length) <
227 be16_to_cpu(hdr->bestfree[XFS_DIR2_DATA_FD_COUNT - 1].length))
230 * Look at the three bestfree entries for our guy.
232 for (dfp = &hdr->bestfree[0];
233 dfp < &hdr->bestfree[XFS_DIR2_DATA_FD_COUNT];
237 if (be16_to_cpu(dfp->offset) == off)
241 * Didn't find it. This only happens if there are duplicate lengths.
247 * Insert an unused-space entry into the bestfree table.
249 xfs_dir2_data_free_t * /* entry inserted */
250 xfs_dir2_data_freeinsert(
251 xfs_dir2_data_hdr_t *hdr, /* data block pointer */
252 xfs_dir2_data_unused_t *dup, /* unused space */
253 int *loghead) /* log the data header (out) */
255 xfs_dir2_data_free_t *dfp; /* bestfree table pointer */
256 xfs_dir2_data_free_t new; /* new bestfree entry */
259 ASSERT(be32_to_cpu(hdr->magic) == XFS_DIR2_DATA_MAGIC ||
260 be32_to_cpu(hdr->magic) == XFS_DIR2_BLOCK_MAGIC);
263 new.length = dup->length;
264 new.offset = cpu_to_be16((char *)dup - (char *)hdr);
267 * Insert at position 0, 1, or 2; or not at all.
269 if (be16_to_cpu(new.length) > be16_to_cpu(dfp[0].length)) {
276 if (be16_to_cpu(new.length) > be16_to_cpu(dfp[1].length)) {
282 if (be16_to_cpu(new.length) > be16_to_cpu(dfp[2].length)) {
291 * Remove a bestfree entry from the table.
294 xfs_dir2_data_freeremove(
295 xfs_dir2_data_hdr_t *hdr, /* data block header */
296 xfs_dir2_data_free_t *dfp, /* bestfree entry pointer */
297 int *loghead) /* out: log data header */
300 ASSERT(be32_to_cpu(hdr->magic) == XFS_DIR2_DATA_MAGIC ||
301 be32_to_cpu(hdr->magic) == XFS_DIR2_BLOCK_MAGIC);
304 * It's the first entry, slide the next 2 up.
306 if (dfp == &hdr->bestfree[0]) {
307 hdr->bestfree[0] = hdr->bestfree[1];
308 hdr->bestfree[1] = hdr->bestfree[2];
311 * It's the second entry, slide the 3rd entry up.
313 else if (dfp == &hdr->bestfree[1])
314 hdr->bestfree[1] = hdr->bestfree[2];
316 * Must be the last entry.
319 ASSERT(dfp == &hdr->bestfree[2]);
321 * Clear the 3rd entry, must be zero now.
323 hdr->bestfree[2].length = 0;
324 hdr->bestfree[2].offset = 0;
329 * Given a data block, reconstruct its bestfree map.
332 xfs_dir2_data_freescan(
333 xfs_mount_t *mp, /* filesystem mount point */
334 xfs_dir2_data_hdr_t *hdr, /* data block header */
335 int *loghead) /* out: log data header */
337 xfs_dir2_block_tail_t *btp; /* block tail */
338 xfs_dir2_data_entry_t *dep; /* active data entry */
339 xfs_dir2_data_unused_t *dup; /* unused data entry */
340 char *endp; /* end of block's data */
341 char *p; /* current entry pointer */
344 ASSERT(be32_to_cpu(hdr->magic) == XFS_DIR2_DATA_MAGIC ||
345 be32_to_cpu(hdr->magic) == XFS_DIR2_BLOCK_MAGIC);
348 * Start by clearing the table.
350 memset(hdr->bestfree, 0, sizeof(hdr->bestfree));
355 p = (char *)(hdr + 1);
356 if (be32_to_cpu(hdr->magic) == XFS_DIR2_BLOCK_MAGIC) {
357 btp = xfs_dir2_block_tail_p(mp, hdr);
358 endp = (char *)xfs_dir2_block_leaf_p(btp);
360 endp = (char *)hdr + mp->m_dirblksize;
362 * Loop over the block's entries.
365 dup = (xfs_dir2_data_unused_t *)p;
367 * If it's a free entry, insert it.
369 if (be16_to_cpu(dup->freetag) == XFS_DIR2_DATA_FREE_TAG) {
370 ASSERT((char *)dup - (char *)hdr ==
371 be16_to_cpu(*xfs_dir2_data_unused_tag_p(dup)));
372 xfs_dir2_data_freeinsert(hdr, dup, loghead);
373 p += be16_to_cpu(dup->length);
376 * For active entries, check their tags and skip them.
379 dep = (xfs_dir2_data_entry_t *)p;
380 ASSERT((char *)dep - (char *)hdr ==
381 be16_to_cpu(*xfs_dir2_data_entry_tag_p(dep)));
382 p += xfs_dir2_data_entsize(dep->namelen);
388 * Initialize a data block at the given block number in the directory.
389 * Give back the buffer for the created block.
393 xfs_da_args_t *args, /* directory operation args */
394 xfs_dir2_db_t blkno, /* logical dir block number */
395 xfs_dabuf_t **bpp) /* output block buffer */
397 xfs_dabuf_t *bp; /* block buffer */
398 xfs_dir2_data_hdr_t *hdr; /* data block header */
399 xfs_inode_t *dp; /* incore directory inode */
400 xfs_dir2_data_unused_t *dup; /* unused entry pointer */
401 int error; /* error return value */
402 int i; /* bestfree index */
403 xfs_mount_t *mp; /* filesystem mount point */
404 xfs_trans_t *tp; /* transaction pointer */
411 * Get the buffer set up for the block.
413 error = xfs_da_get_buf(tp, dp, xfs_dir2_db_to_da(mp, blkno), -1, &bp,
421 * Initialize the header.
424 hdr->magic = cpu_to_be32(XFS_DIR2_DATA_MAGIC);
425 hdr->bestfree[0].offset = cpu_to_be16(sizeof(*hdr));
426 for (i = 1; i < XFS_DIR2_DATA_FD_COUNT; i++) {
427 hdr->bestfree[i].length = 0;
428 hdr->bestfree[i].offset = 0;
432 * Set up an unused entry for the block's body.
434 dup = (xfs_dir2_data_unused_t *)(hdr + 1);
435 dup->freetag = cpu_to_be16(XFS_DIR2_DATA_FREE_TAG);
437 t = mp->m_dirblksize - (uint)sizeof(*hdr);
438 hdr->bestfree[0].length = cpu_to_be16(t);
439 dup->length = cpu_to_be16(t);
440 *xfs_dir2_data_unused_tag_p(dup) = cpu_to_be16((char *)dup - (char *)hdr);
442 * Log it and return it.
444 xfs_dir2_data_log_header(tp, bp);
445 xfs_dir2_data_log_unused(tp, bp, dup);
451 * Log an active data entry from the block.
454 xfs_dir2_data_log_entry(
455 xfs_trans_t *tp, /* transaction pointer */
456 xfs_dabuf_t *bp, /* block buffer */
457 xfs_dir2_data_entry_t *dep) /* data entry pointer */
459 xfs_dir2_data_hdr_t *hdr = bp->data;
461 ASSERT(be32_to_cpu(hdr->magic) == XFS_DIR2_DATA_MAGIC ||
462 be32_to_cpu(hdr->magic) == XFS_DIR2_BLOCK_MAGIC);
464 xfs_da_log_buf(tp, bp, (uint)((char *)dep - (char *)hdr),
465 (uint)((char *)(xfs_dir2_data_entry_tag_p(dep) + 1) -
470 * Log a data block header.
473 xfs_dir2_data_log_header(
474 xfs_trans_t *tp, /* transaction pointer */
475 xfs_dabuf_t *bp) /* block buffer */
477 xfs_dir2_data_hdr_t *hdr = bp->data;
479 ASSERT(be32_to_cpu(hdr->magic) == XFS_DIR2_DATA_MAGIC ||
480 be32_to_cpu(hdr->magic) == XFS_DIR2_BLOCK_MAGIC);
482 xfs_da_log_buf(tp, bp, 0, sizeof(*hdr) - 1);
486 * Log a data unused entry.
489 xfs_dir2_data_log_unused(
490 xfs_trans_t *tp, /* transaction pointer */
491 xfs_dabuf_t *bp, /* block buffer */
492 xfs_dir2_data_unused_t *dup) /* data unused pointer */
494 xfs_dir2_data_hdr_t *hdr = bp->data;
496 ASSERT(be32_to_cpu(hdr->magic) == XFS_DIR2_DATA_MAGIC ||
497 be32_to_cpu(hdr->magic) == XFS_DIR2_BLOCK_MAGIC);
500 * Log the first part of the unused entry.
502 xfs_da_log_buf(tp, bp, (uint)((char *)dup - (char *)hdr),
503 (uint)((char *)&dup->length + sizeof(dup->length) -
506 * Log the end (tag) of the unused entry.
508 xfs_da_log_buf(tp, bp,
509 (uint)((char *)xfs_dir2_data_unused_tag_p(dup) - (char *)hdr),
510 (uint)((char *)xfs_dir2_data_unused_tag_p(dup) - (char *)hdr +
511 sizeof(xfs_dir2_data_off_t) - 1));
515 * Make a byte range in the data block unused.
516 * Its current contents are unimportant.
519 xfs_dir2_data_make_free(
520 xfs_trans_t *tp, /* transaction pointer */
521 xfs_dabuf_t *bp, /* block buffer */
522 xfs_dir2_data_aoff_t offset, /* starting byte offset */
523 xfs_dir2_data_aoff_t len, /* length in bytes */
524 int *needlogp, /* out: log header */
525 int *needscanp) /* out: regen bestfree */
527 xfs_dir2_data_hdr_t *hdr; /* data block pointer */
528 xfs_dir2_data_free_t *dfp; /* bestfree pointer */
529 char *endptr; /* end of data area */
530 xfs_mount_t *mp; /* filesystem mount point */
531 int needscan; /* need to regen bestfree */
532 xfs_dir2_data_unused_t *newdup; /* new unused entry */
533 xfs_dir2_data_unused_t *postdup; /* unused entry after us */
534 xfs_dir2_data_unused_t *prevdup; /* unused entry before us */
540 * Figure out where the end of the data area is.
542 if (be32_to_cpu(hdr->magic) == XFS_DIR2_DATA_MAGIC)
543 endptr = (char *)hdr + mp->m_dirblksize;
545 xfs_dir2_block_tail_t *btp; /* block tail */
547 ASSERT(be32_to_cpu(hdr->magic) == XFS_DIR2_BLOCK_MAGIC);
548 btp = xfs_dir2_block_tail_p(mp, hdr);
549 endptr = (char *)xfs_dir2_block_leaf_p(btp);
552 * If this isn't the start of the block, then back up to
553 * the previous entry and see if it's free.
555 if (offset > sizeof(*hdr)) {
556 __be16 *tagp; /* tag just before us */
558 tagp = (__be16 *)((char *)hdr + offset) - 1;
559 prevdup = (xfs_dir2_data_unused_t *)((char *)hdr + be16_to_cpu(*tagp));
560 if (be16_to_cpu(prevdup->freetag) != XFS_DIR2_DATA_FREE_TAG)
565 * If this isn't the end of the block, see if the entry after
568 if ((char *)hdr + offset + len < endptr) {
570 (xfs_dir2_data_unused_t *)((char *)hdr + offset + len);
571 if (be16_to_cpu(postdup->freetag) != XFS_DIR2_DATA_FREE_TAG)
575 ASSERT(*needscanp == 0);
578 * Previous and following entries are both free,
579 * merge everything into a single free entry.
581 if (prevdup && postdup) {
582 xfs_dir2_data_free_t *dfp2; /* another bestfree pointer */
585 * See if prevdup and/or postdup are in bestfree table.
587 dfp = xfs_dir2_data_freefind(hdr, prevdup);
588 dfp2 = xfs_dir2_data_freefind(hdr, postdup);
590 * We need a rescan unless there are exactly 2 free entries
591 * namely our two. Then we know what's happening, otherwise
592 * since the third bestfree is there, there might be more
595 needscan = (hdr->bestfree[2].length != 0);
597 * Fix up the new big freespace.
599 be16_add_cpu(&prevdup->length, len + be16_to_cpu(postdup->length));
600 *xfs_dir2_data_unused_tag_p(prevdup) =
601 cpu_to_be16((char *)prevdup - (char *)hdr);
602 xfs_dir2_data_log_unused(tp, bp, prevdup);
605 * Has to be the case that entries 0 and 1 are
606 * dfp and dfp2 (don't know which is which), and
608 * Remove entry 1 first then entry 0.
611 if (dfp == &hdr->bestfree[1]) {
612 dfp = &hdr->bestfree[0];
614 dfp2 = &hdr->bestfree[1];
616 xfs_dir2_data_freeremove(hdr, dfp2, needlogp);
617 xfs_dir2_data_freeremove(hdr, dfp, needlogp);
619 * Now insert the new entry.
621 dfp = xfs_dir2_data_freeinsert(hdr, prevdup, needlogp);
622 ASSERT(dfp == &hdr->bestfree[0]);
623 ASSERT(dfp->length == prevdup->length);
624 ASSERT(!dfp[1].length);
625 ASSERT(!dfp[2].length);
629 * The entry before us is free, merge with it.
632 dfp = xfs_dir2_data_freefind(hdr, prevdup);
633 be16_add_cpu(&prevdup->length, len);
634 *xfs_dir2_data_unused_tag_p(prevdup) =
635 cpu_to_be16((char *)prevdup - (char *)hdr);
636 xfs_dir2_data_log_unused(tp, bp, prevdup);
638 * If the previous entry was in the table, the new entry
639 * is longer, so it will be in the table too. Remove
640 * the old one and add the new one.
643 xfs_dir2_data_freeremove(hdr, dfp, needlogp);
644 xfs_dir2_data_freeinsert(hdr, prevdup, needlogp);
647 * Otherwise we need a scan if the new entry is big enough.
650 needscan = be16_to_cpu(prevdup->length) >
651 be16_to_cpu(hdr->bestfree[2].length);
655 * The following entry is free, merge with it.
658 dfp = xfs_dir2_data_freefind(hdr, postdup);
659 newdup = (xfs_dir2_data_unused_t *)((char *)hdr + offset);
660 newdup->freetag = cpu_to_be16(XFS_DIR2_DATA_FREE_TAG);
661 newdup->length = cpu_to_be16(len + be16_to_cpu(postdup->length));
662 *xfs_dir2_data_unused_tag_p(newdup) =
663 cpu_to_be16((char *)newdup - (char *)hdr);
664 xfs_dir2_data_log_unused(tp, bp, newdup);
666 * If the following entry was in the table, the new entry
667 * is longer, so it will be in the table too. Remove
668 * the old one and add the new one.
671 xfs_dir2_data_freeremove(hdr, dfp, needlogp);
672 xfs_dir2_data_freeinsert(hdr, newdup, needlogp);
675 * Otherwise we need a scan if the new entry is big enough.
678 needscan = be16_to_cpu(newdup->length) >
679 be16_to_cpu(hdr->bestfree[2].length);
683 * Neither neighbor is free. Make a new entry.
686 newdup = (xfs_dir2_data_unused_t *)((char *)hdr + offset);
687 newdup->freetag = cpu_to_be16(XFS_DIR2_DATA_FREE_TAG);
688 newdup->length = cpu_to_be16(len);
689 *xfs_dir2_data_unused_tag_p(newdup) =
690 cpu_to_be16((char *)newdup - (char *)hdr);
691 xfs_dir2_data_log_unused(tp, bp, newdup);
692 xfs_dir2_data_freeinsert(hdr, newdup, needlogp);
694 *needscanp = needscan;
698 * Take a byte range out of an existing unused space and make it un-free.
701 xfs_dir2_data_use_free(
702 xfs_trans_t *tp, /* transaction pointer */
703 xfs_dabuf_t *bp, /* data block buffer */
704 xfs_dir2_data_unused_t *dup, /* unused entry */
705 xfs_dir2_data_aoff_t offset, /* starting offset to use */
706 xfs_dir2_data_aoff_t len, /* length to use */
707 int *needlogp, /* out: need to log header */
708 int *needscanp) /* out: need regen bestfree */
710 xfs_dir2_data_hdr_t *hdr; /* data block header */
711 xfs_dir2_data_free_t *dfp; /* bestfree pointer */
712 int matchback; /* matches end of freespace */
713 int matchfront; /* matches start of freespace */
714 int needscan; /* need to regen bestfree */
715 xfs_dir2_data_unused_t *newdup; /* new unused entry */
716 xfs_dir2_data_unused_t *newdup2; /* another new unused entry */
717 int oldlen; /* old unused entry's length */
720 ASSERT(be32_to_cpu(hdr->magic) == XFS_DIR2_DATA_MAGIC ||
721 be32_to_cpu(hdr->magic) == XFS_DIR2_BLOCK_MAGIC);
722 ASSERT(be16_to_cpu(dup->freetag) == XFS_DIR2_DATA_FREE_TAG);
723 ASSERT(offset >= (char *)dup - (char *)hdr);
724 ASSERT(offset + len <= (char *)dup + be16_to_cpu(dup->length) - (char *)hdr);
725 ASSERT((char *)dup - (char *)hdr == be16_to_cpu(*xfs_dir2_data_unused_tag_p(dup)));
727 * Look up the entry in the bestfree table.
729 dfp = xfs_dir2_data_freefind(hdr, dup);
730 oldlen = be16_to_cpu(dup->length);
731 ASSERT(dfp || oldlen <= be16_to_cpu(hdr->bestfree[2].length));
733 * Check for alignment with front and back of the entry.
735 matchfront = (char *)dup - (char *)hdr == offset;
736 matchback = (char *)dup + oldlen - (char *)hdr == offset + len;
737 ASSERT(*needscanp == 0);
740 * If we matched it exactly we just need to get rid of it from
741 * the bestfree table.
743 if (matchfront && matchback) {
745 needscan = (hdr->bestfree[2].offset != 0);
747 xfs_dir2_data_freeremove(hdr, dfp, needlogp);
751 * We match the first part of the entry.
752 * Make a new entry with the remaining freespace.
754 else if (matchfront) {
755 newdup = (xfs_dir2_data_unused_t *)((char *)hdr + offset + len);
756 newdup->freetag = cpu_to_be16(XFS_DIR2_DATA_FREE_TAG);
757 newdup->length = cpu_to_be16(oldlen - len);
758 *xfs_dir2_data_unused_tag_p(newdup) =
759 cpu_to_be16((char *)newdup - (char *)hdr);
760 xfs_dir2_data_log_unused(tp, bp, newdup);
762 * If it was in the table, remove it and add the new one.
765 xfs_dir2_data_freeremove(hdr, dfp, needlogp);
766 dfp = xfs_dir2_data_freeinsert(hdr, newdup, needlogp);
768 ASSERT(dfp->length == newdup->length);
769 ASSERT(be16_to_cpu(dfp->offset) == (char *)newdup - (char *)hdr);
771 * If we got inserted at the last slot,
772 * that means we don't know if there was a better
773 * choice for the last slot, or not. Rescan.
775 needscan = dfp == &hdr->bestfree[2];
779 * We match the last part of the entry.
780 * Trim the allocated space off the tail of the entry.
782 else if (matchback) {
784 newdup->length = cpu_to_be16(((char *)hdr + offset) - (char *)newdup);
785 *xfs_dir2_data_unused_tag_p(newdup) =
786 cpu_to_be16((char *)newdup - (char *)hdr);
787 xfs_dir2_data_log_unused(tp, bp, newdup);
789 * If it was in the table, remove it and add the new one.
792 xfs_dir2_data_freeremove(hdr, dfp, needlogp);
793 dfp = xfs_dir2_data_freeinsert(hdr, newdup, needlogp);
795 ASSERT(dfp->length == newdup->length);
796 ASSERT(be16_to_cpu(dfp->offset) == (char *)newdup - (char *)hdr);
798 * If we got inserted at the last slot,
799 * that means we don't know if there was a better
800 * choice for the last slot, or not. Rescan.
802 needscan = dfp == &hdr->bestfree[2];
806 * Poking out the middle of an entry.
807 * Make two new entries.
811 newdup->length = cpu_to_be16(((char *)hdr + offset) - (char *)newdup);
812 *xfs_dir2_data_unused_tag_p(newdup) =
813 cpu_to_be16((char *)newdup - (char *)hdr);
814 xfs_dir2_data_log_unused(tp, bp, newdup);
815 newdup2 = (xfs_dir2_data_unused_t *)((char *)hdr + offset + len);
816 newdup2->freetag = cpu_to_be16(XFS_DIR2_DATA_FREE_TAG);
817 newdup2->length = cpu_to_be16(oldlen - len - be16_to_cpu(newdup->length));
818 *xfs_dir2_data_unused_tag_p(newdup2) =
819 cpu_to_be16((char *)newdup2 - (char *)hdr);
820 xfs_dir2_data_log_unused(tp, bp, newdup2);
822 * If the old entry was in the table, we need to scan
823 * if the 3rd entry was valid, since these entries
824 * are smaller than the old one.
825 * If we don't need to scan that means there were 1 or 2
826 * entries in the table, and removing the old and adding
827 * the 2 new will work.
830 needscan = (hdr->bestfree[2].length != 0);
832 xfs_dir2_data_freeremove(hdr, dfp, needlogp);
833 xfs_dir2_data_freeinsert(hdr, newdup, needlogp);
834 xfs_dir2_data_freeinsert(hdr, newdup2,
839 *needscanp = needscan;