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"
26 #include "xfs_mount.h"
27 #include "xfs_da_btree.h"
28 #include "xfs_bmap_btree.h"
29 #include "xfs_dinode.h"
30 #include "xfs_inode.h"
31 #include "xfs_dir2_format.h"
32 #include "xfs_dir2_priv.h"
33 #include "xfs_error.h"
35 STATIC xfs_dir2_data_free_t
*
36 xfs_dir2_data_freefind(xfs_dir2_data_hdr_t
*hdr
, xfs_dir2_data_unused_t
*dup
);
40 * Check the consistency of the data block.
41 * The input can also be a block-format directory.
42 * Pop an assert if we find anything bad.
46 xfs_inode_t
*dp
, /* incore inode pointer */
47 xfs_dabuf_t
*bp
) /* data block's buffer */
49 xfs_dir2_dataptr_t addr
; /* addr for leaf lookup */
50 xfs_dir2_data_free_t
*bf
; /* bestfree table */
51 xfs_dir2_block_tail_t
*btp
=NULL
; /* block tail */
52 int count
; /* count of entries found */
53 xfs_dir2_data_hdr_t
*hdr
; /* data block header */
54 xfs_dir2_data_entry_t
*dep
; /* data entry */
55 xfs_dir2_data_free_t
*dfp
; /* bestfree entry */
56 xfs_dir2_data_unused_t
*dup
; /* unused entry */
57 char *endp
; /* end of useful data */
58 int freeseen
; /* mask of bestfrees seen */
59 xfs_dahash_t hash
; /* hash of current name */
60 int i
; /* leaf index */
61 int lastfree
; /* last entry was unused */
62 xfs_dir2_leaf_entry_t
*lep
=NULL
; /* block leaf entries */
63 xfs_mount_t
*mp
; /* filesystem mount point */
64 char *p
; /* current data position */
65 int stale
; /* count of stale leaves */
71 p
= (char *)(hdr
+ 1);
73 if (hdr
->magic
== cpu_to_be32(XFS_DIR2_BLOCK_MAGIC
)) {
74 btp
= xfs_dir2_block_tail_p(mp
, hdr
);
75 lep
= xfs_dir2_block_leaf_p(btp
);
78 ASSERT(hdr
->magic
== cpu_to_be32(XFS_DIR2_DATA_MAGIC
));
79 endp
= (char *)hdr
+ mp
->m_dirblksize
;
82 count
= lastfree
= freeseen
= 0;
84 * Account for zero bestfree entries.
87 ASSERT(!bf
[0].offset
);
91 ASSERT(!bf
[1].offset
);
95 ASSERT(!bf
[2].offset
);
98 ASSERT(be16_to_cpu(bf
[0].length
) >= be16_to_cpu(bf
[1].length
));
99 ASSERT(be16_to_cpu(bf
[1].length
) >= be16_to_cpu(bf
[2].length
));
101 * Loop over the data/unused entries.
104 dup
= (xfs_dir2_data_unused_t
*)p
;
106 * If it's unused, look for the space in the bestfree table.
107 * If we find it, account for that, else make sure it
108 * doesn't need to be there.
110 if (be16_to_cpu(dup
->freetag
) == XFS_DIR2_DATA_FREE_TAG
) {
111 ASSERT(lastfree
== 0);
112 ASSERT(be16_to_cpu(*xfs_dir2_data_unused_tag_p(dup
)) ==
113 (char *)dup
- (char *)hdr
);
114 dfp
= xfs_dir2_data_freefind(hdr
, dup
);
117 ASSERT((freeseen
& (1 << i
)) == 0);
120 ASSERT(be16_to_cpu(dup
->length
) <=
121 be16_to_cpu(bf
[2].length
));
123 p
+= be16_to_cpu(dup
->length
);
128 * It's a real entry. Validate the fields.
129 * If this is a block directory then make sure it's
130 * in the leaf section of the block.
131 * The linear search is crude but this is DEBUG code.
133 dep
= (xfs_dir2_data_entry_t
*)p
;
134 ASSERT(dep
->namelen
!= 0);
135 ASSERT(xfs_dir_ino_validate(mp
, be64_to_cpu(dep
->inumber
)) == 0);
136 ASSERT(be16_to_cpu(*xfs_dir2_data_entry_tag_p(dep
)) ==
137 (char *)dep
- (char *)hdr
);
140 if (hdr
->magic
== cpu_to_be32(XFS_DIR2_BLOCK_MAGIC
)) {
141 addr
= xfs_dir2_db_off_to_dataptr(mp
, mp
->m_dirdatablk
,
142 (xfs_dir2_data_aoff_t
)
143 ((char *)dep
- (char *)hdr
));
144 name
.name
= dep
->name
;
145 name
.len
= dep
->namelen
;
146 hash
= mp
->m_dirnameops
->hashname(&name
);
147 for (i
= 0; i
< be32_to_cpu(btp
->count
); i
++) {
148 if (be32_to_cpu(lep
[i
].address
) == addr
&&
149 be32_to_cpu(lep
[i
].hashval
) == hash
)
152 ASSERT(i
< be32_to_cpu(btp
->count
));
154 p
+= xfs_dir2_data_entsize(dep
->namelen
);
157 * Need to have seen all the entries and all the bestfree slots.
159 ASSERT(freeseen
== 7);
160 if (hdr
->magic
== cpu_to_be32(XFS_DIR2_BLOCK_MAGIC
)) {
161 for (i
= stale
= 0; i
< be32_to_cpu(btp
->count
); i
++) {
162 if (lep
[i
].address
==
163 cpu_to_be32(XFS_DIR2_NULL_DATAPTR
))
166 ASSERT(be32_to_cpu(lep
[i
].hashval
) >= be32_to_cpu(lep
[i
- 1].hashval
));
168 ASSERT(count
== be32_to_cpu(btp
->count
) - be32_to_cpu(btp
->stale
));
169 ASSERT(stale
== be32_to_cpu(btp
->stale
));
175 * Given a data block and an unused entry from that block,
176 * return the bestfree entry if any that corresponds to it.
178 STATIC xfs_dir2_data_free_t
*
179 xfs_dir2_data_freefind(
180 xfs_dir2_data_hdr_t
*hdr
, /* data block */
181 xfs_dir2_data_unused_t
*dup
) /* data unused entry */
183 xfs_dir2_data_free_t
*dfp
; /* bestfree entry */
184 xfs_dir2_data_aoff_t off
; /* offset value needed */
185 #if defined(DEBUG) && defined(__KERNEL__)
186 int matched
; /* matched the value */
187 int seenzero
; /* saw a 0 bestfree entry */
190 off
= (xfs_dir2_data_aoff_t
)((char *)dup
- (char *)hdr
);
191 #if defined(DEBUG) && defined(__KERNEL__)
193 * Validate some consistency in the bestfree table.
194 * Check order, non-overlapping entries, and if we find the
195 * one we're looking for it has to be exact.
197 ASSERT(hdr
->magic
== cpu_to_be32(XFS_DIR2_DATA_MAGIC
) ||
198 hdr
->magic
== cpu_to_be32(XFS_DIR2_BLOCK_MAGIC
));
199 for (dfp
= &hdr
->bestfree
[0], seenzero
= matched
= 0;
200 dfp
< &hdr
->bestfree
[XFS_DIR2_DATA_FD_COUNT
];
203 ASSERT(!dfp
->length
);
207 ASSERT(seenzero
== 0);
208 if (be16_to_cpu(dfp
->offset
) == off
) {
210 ASSERT(dfp
->length
== dup
->length
);
211 } else if (off
< be16_to_cpu(dfp
->offset
))
212 ASSERT(off
+ be16_to_cpu(dup
->length
) <= be16_to_cpu(dfp
->offset
));
214 ASSERT(be16_to_cpu(dfp
->offset
) + be16_to_cpu(dfp
->length
) <= off
);
215 ASSERT(matched
|| be16_to_cpu(dfp
->length
) >= be16_to_cpu(dup
->length
));
216 if (dfp
> &hdr
->bestfree
[0])
217 ASSERT(be16_to_cpu(dfp
[-1].length
) >= be16_to_cpu(dfp
[0].length
));
221 * If this is smaller than the smallest bestfree entry,
222 * it can't be there since they're sorted.
224 if (be16_to_cpu(dup
->length
) <
225 be16_to_cpu(hdr
->bestfree
[XFS_DIR2_DATA_FD_COUNT
- 1].length
))
228 * Look at the three bestfree entries for our guy.
230 for (dfp
= &hdr
->bestfree
[0];
231 dfp
< &hdr
->bestfree
[XFS_DIR2_DATA_FD_COUNT
];
235 if (be16_to_cpu(dfp
->offset
) == off
)
239 * Didn't find it. This only happens if there are duplicate lengths.
245 * Insert an unused-space entry into the bestfree table.
247 xfs_dir2_data_free_t
* /* entry inserted */
248 xfs_dir2_data_freeinsert(
249 xfs_dir2_data_hdr_t
*hdr
, /* data block pointer */
250 xfs_dir2_data_unused_t
*dup
, /* unused space */
251 int *loghead
) /* log the data header (out) */
253 xfs_dir2_data_free_t
*dfp
; /* bestfree table pointer */
254 xfs_dir2_data_free_t
new; /* new bestfree entry */
257 ASSERT(hdr
->magic
== cpu_to_be32(XFS_DIR2_DATA_MAGIC
) ||
258 hdr
->magic
== cpu_to_be32(XFS_DIR2_BLOCK_MAGIC
));
261 new.length
= dup
->length
;
262 new.offset
= cpu_to_be16((char *)dup
- (char *)hdr
);
265 * Insert at position 0, 1, or 2; or not at all.
267 if (be16_to_cpu(new.length
) > be16_to_cpu(dfp
[0].length
)) {
274 if (be16_to_cpu(new.length
) > be16_to_cpu(dfp
[1].length
)) {
280 if (be16_to_cpu(new.length
) > be16_to_cpu(dfp
[2].length
)) {
289 * Remove a bestfree entry from the table.
292 xfs_dir2_data_freeremove(
293 xfs_dir2_data_hdr_t
*hdr
, /* data block header */
294 xfs_dir2_data_free_t
*dfp
, /* bestfree entry pointer */
295 int *loghead
) /* out: log data header */
298 ASSERT(hdr
->magic
== cpu_to_be32(XFS_DIR2_DATA_MAGIC
) ||
299 hdr
->magic
== cpu_to_be32(XFS_DIR2_BLOCK_MAGIC
));
302 * It's the first entry, slide the next 2 up.
304 if (dfp
== &hdr
->bestfree
[0]) {
305 hdr
->bestfree
[0] = hdr
->bestfree
[1];
306 hdr
->bestfree
[1] = hdr
->bestfree
[2];
309 * It's the second entry, slide the 3rd entry up.
311 else if (dfp
== &hdr
->bestfree
[1])
312 hdr
->bestfree
[1] = hdr
->bestfree
[2];
314 * Must be the last entry.
317 ASSERT(dfp
== &hdr
->bestfree
[2]);
319 * Clear the 3rd entry, must be zero now.
321 hdr
->bestfree
[2].length
= 0;
322 hdr
->bestfree
[2].offset
= 0;
327 * Given a data block, reconstruct its bestfree map.
330 xfs_dir2_data_freescan(
331 xfs_mount_t
*mp
, /* filesystem mount point */
332 xfs_dir2_data_hdr_t
*hdr
, /* data block header */
333 int *loghead
) /* out: log data header */
335 xfs_dir2_block_tail_t
*btp
; /* block tail */
336 xfs_dir2_data_entry_t
*dep
; /* active data entry */
337 xfs_dir2_data_unused_t
*dup
; /* unused data entry */
338 char *endp
; /* end of block's data */
339 char *p
; /* current entry pointer */
342 ASSERT(hdr
->magic
== cpu_to_be32(XFS_DIR2_DATA_MAGIC
) ||
343 hdr
->magic
== cpu_to_be32(XFS_DIR2_BLOCK_MAGIC
));
346 * Start by clearing the table.
348 memset(hdr
->bestfree
, 0, sizeof(hdr
->bestfree
));
353 p
= (char *)(hdr
+ 1);
354 if (hdr
->magic
== cpu_to_be32(XFS_DIR2_BLOCK_MAGIC
)) {
355 btp
= xfs_dir2_block_tail_p(mp
, hdr
);
356 endp
= (char *)xfs_dir2_block_leaf_p(btp
);
358 endp
= (char *)hdr
+ mp
->m_dirblksize
;
360 * Loop over the block's entries.
363 dup
= (xfs_dir2_data_unused_t
*)p
;
365 * If it's a free entry, insert it.
367 if (be16_to_cpu(dup
->freetag
) == XFS_DIR2_DATA_FREE_TAG
) {
368 ASSERT((char *)dup
- (char *)hdr
==
369 be16_to_cpu(*xfs_dir2_data_unused_tag_p(dup
)));
370 xfs_dir2_data_freeinsert(hdr
, dup
, loghead
);
371 p
+= be16_to_cpu(dup
->length
);
374 * For active entries, check their tags and skip them.
377 dep
= (xfs_dir2_data_entry_t
*)p
;
378 ASSERT((char *)dep
- (char *)hdr
==
379 be16_to_cpu(*xfs_dir2_data_entry_tag_p(dep
)));
380 p
+= xfs_dir2_data_entsize(dep
->namelen
);
386 * Initialize a data block at the given block number in the directory.
387 * Give back the buffer for the created block.
391 xfs_da_args_t
*args
, /* directory operation args */
392 xfs_dir2_db_t blkno
, /* logical dir block number */
393 xfs_dabuf_t
**bpp
) /* output block buffer */
395 xfs_dabuf_t
*bp
; /* block buffer */
396 xfs_dir2_data_hdr_t
*hdr
; /* data block header */
397 xfs_inode_t
*dp
; /* incore directory inode */
398 xfs_dir2_data_unused_t
*dup
; /* unused entry pointer */
399 int error
; /* error return value */
400 int i
; /* bestfree index */
401 xfs_mount_t
*mp
; /* filesystem mount point */
402 xfs_trans_t
*tp
; /* transaction pointer */
409 * Get the buffer set up for the block.
411 error
= xfs_da_get_buf(tp
, dp
, xfs_dir2_db_to_da(mp
, blkno
), -1, &bp
,
419 * Initialize the header.
422 hdr
->magic
= cpu_to_be32(XFS_DIR2_DATA_MAGIC
);
423 hdr
->bestfree
[0].offset
= cpu_to_be16(sizeof(*hdr
));
424 for (i
= 1; i
< XFS_DIR2_DATA_FD_COUNT
; i
++) {
425 hdr
->bestfree
[i
].length
= 0;
426 hdr
->bestfree
[i
].offset
= 0;
430 * Set up an unused entry for the block's body.
432 dup
= (xfs_dir2_data_unused_t
*)(hdr
+ 1);
433 dup
->freetag
= cpu_to_be16(XFS_DIR2_DATA_FREE_TAG
);
435 t
= mp
->m_dirblksize
- (uint
)sizeof(*hdr
);
436 hdr
->bestfree
[0].length
= cpu_to_be16(t
);
437 dup
->length
= cpu_to_be16(t
);
438 *xfs_dir2_data_unused_tag_p(dup
) = cpu_to_be16((char *)dup
- (char *)hdr
);
440 * Log it and return it.
442 xfs_dir2_data_log_header(tp
, bp
);
443 xfs_dir2_data_log_unused(tp
, bp
, dup
);
449 * Log an active data entry from the block.
452 xfs_dir2_data_log_entry(
453 xfs_trans_t
*tp
, /* transaction pointer */
454 xfs_dabuf_t
*bp
, /* block buffer */
455 xfs_dir2_data_entry_t
*dep
) /* data entry pointer */
457 xfs_dir2_data_hdr_t
*hdr
= bp
->data
;
459 ASSERT(hdr
->magic
== cpu_to_be32(XFS_DIR2_DATA_MAGIC
) ||
460 hdr
->magic
== cpu_to_be32(XFS_DIR2_BLOCK_MAGIC
));
462 xfs_da_log_buf(tp
, bp
, (uint
)((char *)dep
- (char *)hdr
),
463 (uint
)((char *)(xfs_dir2_data_entry_tag_p(dep
) + 1) -
468 * Log a data block header.
471 xfs_dir2_data_log_header(
472 xfs_trans_t
*tp
, /* transaction pointer */
473 xfs_dabuf_t
*bp
) /* block buffer */
475 xfs_dir2_data_hdr_t
*hdr
= bp
->data
;
477 ASSERT(hdr
->magic
== cpu_to_be32(XFS_DIR2_DATA_MAGIC
) ||
478 hdr
->magic
== cpu_to_be32(XFS_DIR2_BLOCK_MAGIC
));
480 xfs_da_log_buf(tp
, bp
, 0, sizeof(*hdr
) - 1);
484 * Log a data unused entry.
487 xfs_dir2_data_log_unused(
488 xfs_trans_t
*tp
, /* transaction pointer */
489 xfs_dabuf_t
*bp
, /* block buffer */
490 xfs_dir2_data_unused_t
*dup
) /* data unused pointer */
492 xfs_dir2_data_hdr_t
*hdr
= bp
->data
;
494 ASSERT(hdr
->magic
== cpu_to_be32(XFS_DIR2_DATA_MAGIC
) ||
495 hdr
->magic
== cpu_to_be32(XFS_DIR2_BLOCK_MAGIC
));
498 * Log the first part of the unused entry.
500 xfs_da_log_buf(tp
, bp
, (uint
)((char *)dup
- (char *)hdr
),
501 (uint
)((char *)&dup
->length
+ sizeof(dup
->length
) -
504 * Log the end (tag) of the unused entry.
506 xfs_da_log_buf(tp
, bp
,
507 (uint
)((char *)xfs_dir2_data_unused_tag_p(dup
) - (char *)hdr
),
508 (uint
)((char *)xfs_dir2_data_unused_tag_p(dup
) - (char *)hdr
+
509 sizeof(xfs_dir2_data_off_t
) - 1));
513 * Make a byte range in the data block unused.
514 * Its current contents are unimportant.
517 xfs_dir2_data_make_free(
518 xfs_trans_t
*tp
, /* transaction pointer */
519 xfs_dabuf_t
*bp
, /* block buffer */
520 xfs_dir2_data_aoff_t offset
, /* starting byte offset */
521 xfs_dir2_data_aoff_t len
, /* length in bytes */
522 int *needlogp
, /* out: log header */
523 int *needscanp
) /* out: regen bestfree */
525 xfs_dir2_data_hdr_t
*hdr
; /* data block pointer */
526 xfs_dir2_data_free_t
*dfp
; /* bestfree pointer */
527 char *endptr
; /* end of data area */
528 xfs_mount_t
*mp
; /* filesystem mount point */
529 int needscan
; /* need to regen bestfree */
530 xfs_dir2_data_unused_t
*newdup
; /* new unused entry */
531 xfs_dir2_data_unused_t
*postdup
; /* unused entry after us */
532 xfs_dir2_data_unused_t
*prevdup
; /* unused entry before us */
538 * Figure out where the end of the data area is.
540 if (hdr
->magic
== cpu_to_be32(XFS_DIR2_DATA_MAGIC
))
541 endptr
= (char *)hdr
+ mp
->m_dirblksize
;
543 xfs_dir2_block_tail_t
*btp
; /* block tail */
545 ASSERT(hdr
->magic
== cpu_to_be32(XFS_DIR2_BLOCK_MAGIC
));
546 btp
= xfs_dir2_block_tail_p(mp
, hdr
);
547 endptr
= (char *)xfs_dir2_block_leaf_p(btp
);
550 * If this isn't the start of the block, then back up to
551 * the previous entry and see if it's free.
553 if (offset
> sizeof(*hdr
)) {
554 __be16
*tagp
; /* tag just before us */
556 tagp
= (__be16
*)((char *)hdr
+ offset
) - 1;
557 prevdup
= (xfs_dir2_data_unused_t
*)((char *)hdr
+ be16_to_cpu(*tagp
));
558 if (be16_to_cpu(prevdup
->freetag
) != XFS_DIR2_DATA_FREE_TAG
)
563 * If this isn't the end of the block, see if the entry after
566 if ((char *)hdr
+ offset
+ len
< endptr
) {
568 (xfs_dir2_data_unused_t
*)((char *)hdr
+ offset
+ len
);
569 if (be16_to_cpu(postdup
->freetag
) != XFS_DIR2_DATA_FREE_TAG
)
573 ASSERT(*needscanp
== 0);
576 * Previous and following entries are both free,
577 * merge everything into a single free entry.
579 if (prevdup
&& postdup
) {
580 xfs_dir2_data_free_t
*dfp2
; /* another bestfree pointer */
583 * See if prevdup and/or postdup are in bestfree table.
585 dfp
= xfs_dir2_data_freefind(hdr
, prevdup
);
586 dfp2
= xfs_dir2_data_freefind(hdr
, postdup
);
588 * We need a rescan unless there are exactly 2 free entries
589 * namely our two. Then we know what's happening, otherwise
590 * since the third bestfree is there, there might be more
593 needscan
= (hdr
->bestfree
[2].length
!= 0);
595 * Fix up the new big freespace.
597 be16_add_cpu(&prevdup
->length
, len
+ be16_to_cpu(postdup
->length
));
598 *xfs_dir2_data_unused_tag_p(prevdup
) =
599 cpu_to_be16((char *)prevdup
- (char *)hdr
);
600 xfs_dir2_data_log_unused(tp
, bp
, prevdup
);
603 * Has to be the case that entries 0 and 1 are
604 * dfp and dfp2 (don't know which is which), and
606 * Remove entry 1 first then entry 0.
609 if (dfp
== &hdr
->bestfree
[1]) {
610 dfp
= &hdr
->bestfree
[0];
612 dfp2
= &hdr
->bestfree
[1];
614 xfs_dir2_data_freeremove(hdr
, dfp2
, needlogp
);
615 xfs_dir2_data_freeremove(hdr
, dfp
, needlogp
);
617 * Now insert the new entry.
619 dfp
= xfs_dir2_data_freeinsert(hdr
, prevdup
, needlogp
);
620 ASSERT(dfp
== &hdr
->bestfree
[0]);
621 ASSERT(dfp
->length
== prevdup
->length
);
622 ASSERT(!dfp
[1].length
);
623 ASSERT(!dfp
[2].length
);
627 * The entry before us is free, merge with it.
630 dfp
= xfs_dir2_data_freefind(hdr
, prevdup
);
631 be16_add_cpu(&prevdup
->length
, len
);
632 *xfs_dir2_data_unused_tag_p(prevdup
) =
633 cpu_to_be16((char *)prevdup
- (char *)hdr
);
634 xfs_dir2_data_log_unused(tp
, bp
, prevdup
);
636 * If the previous entry was in the table, the new entry
637 * is longer, so it will be in the table too. Remove
638 * the old one and add the new one.
641 xfs_dir2_data_freeremove(hdr
, dfp
, needlogp
);
642 xfs_dir2_data_freeinsert(hdr
, prevdup
, needlogp
);
645 * Otherwise we need a scan if the new entry is big enough.
648 needscan
= be16_to_cpu(prevdup
->length
) >
649 be16_to_cpu(hdr
->bestfree
[2].length
);
653 * The following entry is free, merge with it.
656 dfp
= xfs_dir2_data_freefind(hdr
, postdup
);
657 newdup
= (xfs_dir2_data_unused_t
*)((char *)hdr
+ offset
);
658 newdup
->freetag
= cpu_to_be16(XFS_DIR2_DATA_FREE_TAG
);
659 newdup
->length
= cpu_to_be16(len
+ be16_to_cpu(postdup
->length
));
660 *xfs_dir2_data_unused_tag_p(newdup
) =
661 cpu_to_be16((char *)newdup
- (char *)hdr
);
662 xfs_dir2_data_log_unused(tp
, bp
, newdup
);
664 * If the following entry was in the table, the new entry
665 * is longer, so it will be in the table too. Remove
666 * the old one and add the new one.
669 xfs_dir2_data_freeremove(hdr
, dfp
, needlogp
);
670 xfs_dir2_data_freeinsert(hdr
, newdup
, needlogp
);
673 * Otherwise we need a scan if the new entry is big enough.
676 needscan
= be16_to_cpu(newdup
->length
) >
677 be16_to_cpu(hdr
->bestfree
[2].length
);
681 * Neither neighbor is free. Make a new entry.
684 newdup
= (xfs_dir2_data_unused_t
*)((char *)hdr
+ offset
);
685 newdup
->freetag
= cpu_to_be16(XFS_DIR2_DATA_FREE_TAG
);
686 newdup
->length
= cpu_to_be16(len
);
687 *xfs_dir2_data_unused_tag_p(newdup
) =
688 cpu_to_be16((char *)newdup
- (char *)hdr
);
689 xfs_dir2_data_log_unused(tp
, bp
, newdup
);
690 xfs_dir2_data_freeinsert(hdr
, newdup
, needlogp
);
692 *needscanp
= needscan
;
696 * Take a byte range out of an existing unused space and make it un-free.
699 xfs_dir2_data_use_free(
700 xfs_trans_t
*tp
, /* transaction pointer */
701 xfs_dabuf_t
*bp
, /* data block buffer */
702 xfs_dir2_data_unused_t
*dup
, /* unused entry */
703 xfs_dir2_data_aoff_t offset
, /* starting offset to use */
704 xfs_dir2_data_aoff_t len
, /* length to use */
705 int *needlogp
, /* out: need to log header */
706 int *needscanp
) /* out: need regen bestfree */
708 xfs_dir2_data_hdr_t
*hdr
; /* data block header */
709 xfs_dir2_data_free_t
*dfp
; /* bestfree pointer */
710 int matchback
; /* matches end of freespace */
711 int matchfront
; /* matches start of freespace */
712 int needscan
; /* need to regen bestfree */
713 xfs_dir2_data_unused_t
*newdup
; /* new unused entry */
714 xfs_dir2_data_unused_t
*newdup2
; /* another new unused entry */
715 int oldlen
; /* old unused entry's length */
718 ASSERT(hdr
->magic
== cpu_to_be32(XFS_DIR2_DATA_MAGIC
) ||
719 hdr
->magic
== cpu_to_be32(XFS_DIR2_BLOCK_MAGIC
));
720 ASSERT(be16_to_cpu(dup
->freetag
) == XFS_DIR2_DATA_FREE_TAG
);
721 ASSERT(offset
>= (char *)dup
- (char *)hdr
);
722 ASSERT(offset
+ len
<= (char *)dup
+ be16_to_cpu(dup
->length
) - (char *)hdr
);
723 ASSERT((char *)dup
- (char *)hdr
== be16_to_cpu(*xfs_dir2_data_unused_tag_p(dup
)));
725 * Look up the entry in the bestfree table.
727 dfp
= xfs_dir2_data_freefind(hdr
, dup
);
728 oldlen
= be16_to_cpu(dup
->length
);
729 ASSERT(dfp
|| oldlen
<= be16_to_cpu(hdr
->bestfree
[2].length
));
731 * Check for alignment with front and back of the entry.
733 matchfront
= (char *)dup
- (char *)hdr
== offset
;
734 matchback
= (char *)dup
+ oldlen
- (char *)hdr
== offset
+ len
;
735 ASSERT(*needscanp
== 0);
738 * If we matched it exactly we just need to get rid of it from
739 * the bestfree table.
741 if (matchfront
&& matchback
) {
743 needscan
= (hdr
->bestfree
[2].offset
!= 0);
745 xfs_dir2_data_freeremove(hdr
, dfp
, needlogp
);
749 * We match the first part of the entry.
750 * Make a new entry with the remaining freespace.
752 else if (matchfront
) {
753 newdup
= (xfs_dir2_data_unused_t
*)((char *)hdr
+ offset
+ len
);
754 newdup
->freetag
= cpu_to_be16(XFS_DIR2_DATA_FREE_TAG
);
755 newdup
->length
= cpu_to_be16(oldlen
- len
);
756 *xfs_dir2_data_unused_tag_p(newdup
) =
757 cpu_to_be16((char *)newdup
- (char *)hdr
);
758 xfs_dir2_data_log_unused(tp
, bp
, newdup
);
760 * If it was in the table, remove it and add the new one.
763 xfs_dir2_data_freeremove(hdr
, dfp
, needlogp
);
764 dfp
= xfs_dir2_data_freeinsert(hdr
, newdup
, needlogp
);
766 ASSERT(dfp
->length
== newdup
->length
);
767 ASSERT(be16_to_cpu(dfp
->offset
) == (char *)newdup
- (char *)hdr
);
769 * If we got inserted at the last slot,
770 * that means we don't know if there was a better
771 * choice for the last slot, or not. Rescan.
773 needscan
= dfp
== &hdr
->bestfree
[2];
777 * We match the last part of the entry.
778 * Trim the allocated space off the tail of the entry.
780 else if (matchback
) {
782 newdup
->length
= cpu_to_be16(((char *)hdr
+ offset
) - (char *)newdup
);
783 *xfs_dir2_data_unused_tag_p(newdup
) =
784 cpu_to_be16((char *)newdup
- (char *)hdr
);
785 xfs_dir2_data_log_unused(tp
, bp
, newdup
);
787 * If it was in the table, remove it and add the new one.
790 xfs_dir2_data_freeremove(hdr
, dfp
, needlogp
);
791 dfp
= xfs_dir2_data_freeinsert(hdr
, newdup
, needlogp
);
793 ASSERT(dfp
->length
== newdup
->length
);
794 ASSERT(be16_to_cpu(dfp
->offset
) == (char *)newdup
- (char *)hdr
);
796 * If we got inserted at the last slot,
797 * that means we don't know if there was a better
798 * choice for the last slot, or not. Rescan.
800 needscan
= dfp
== &hdr
->bestfree
[2];
804 * Poking out the middle of an entry.
805 * Make two new entries.
809 newdup
->length
= cpu_to_be16(((char *)hdr
+ offset
) - (char *)newdup
);
810 *xfs_dir2_data_unused_tag_p(newdup
) =
811 cpu_to_be16((char *)newdup
- (char *)hdr
);
812 xfs_dir2_data_log_unused(tp
, bp
, newdup
);
813 newdup2
= (xfs_dir2_data_unused_t
*)((char *)hdr
+ offset
+ len
);
814 newdup2
->freetag
= cpu_to_be16(XFS_DIR2_DATA_FREE_TAG
);
815 newdup2
->length
= cpu_to_be16(oldlen
- len
- be16_to_cpu(newdup
->length
));
816 *xfs_dir2_data_unused_tag_p(newdup2
) =
817 cpu_to_be16((char *)newdup2
- (char *)hdr
);
818 xfs_dir2_data_log_unused(tp
, bp
, newdup2
);
820 * If the old entry was in the table, we need to scan
821 * if the 3rd entry was valid, since these entries
822 * are smaller than the old one.
823 * If we don't need to scan that means there were 1 or 2
824 * entries in the table, and removing the old and adding
825 * the 2 new will work.
828 needscan
= (hdr
->bestfree
[2].length
!= 0);
830 xfs_dir2_data_freeremove(hdr
, dfp
, needlogp
);
831 xfs_dir2_data_freeinsert(hdr
, newdup
, needlogp
);
832 xfs_dir2_data_freeinsert(hdr
, newdup2
,
837 *needscanp
= needscan
;