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_t
*d
; /* data block pointer */
57 xfs_dir2_data_hdr_t
*hdr
; /* data block header */
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 */
78 if (be32_to_cpu(hdr
->magic
) == XFS_DIR2_BLOCK_MAGIC
) {
79 btp
= xfs_dir2_block_tail_p(mp
, hdr
);
80 lep
= xfs_dir2_block_leaf_p(btp
);
83 ASSERT(hdr
->magic
== cpu_to_be32(XFS_DIR2_DATA_MAGIC
));
84 endp
= (char *)hdr
+ mp
->m_dirblksize
;
87 count
= lastfree
= freeseen
= 0;
89 * Account for zero bestfree entries.
92 ASSERT(!bf
[0].offset
);
96 ASSERT(!bf
[1].offset
);
100 ASSERT(!bf
[2].offset
);
103 ASSERT(be16_to_cpu(bf
[0].length
) >= be16_to_cpu(bf
[1].length
));
104 ASSERT(be16_to_cpu(bf
[1].length
) >= be16_to_cpu(bf
[2].length
));
106 * Loop over the data/unused entries.
109 dup
= (xfs_dir2_data_unused_t
*)p
;
111 * If it's unused, look for the space in the bestfree table.
112 * If we find it, account for that, else make sure it
113 * doesn't need to be there.
115 if (be16_to_cpu(dup
->freetag
) == XFS_DIR2_DATA_FREE_TAG
) {
116 ASSERT(lastfree
== 0);
117 ASSERT(be16_to_cpu(*xfs_dir2_data_unused_tag_p(dup
)) ==
118 (char *)dup
- (char *)hdr
);
119 dfp
= xfs_dir2_data_freefind(hdr
, dup
);
122 ASSERT((freeseen
& (1 << i
)) == 0);
125 ASSERT(be16_to_cpu(dup
->length
) <=
126 be16_to_cpu(bf
[2].length
));
128 p
+= be16_to_cpu(dup
->length
);
133 * It's a real entry. Validate the fields.
134 * If this is a block directory then make sure it's
135 * in the leaf section of the block.
136 * The linear search is crude but this is DEBUG code.
138 dep
= (xfs_dir2_data_entry_t
*)p
;
139 ASSERT(dep
->namelen
!= 0);
140 ASSERT(xfs_dir_ino_validate(mp
, be64_to_cpu(dep
->inumber
)) == 0);
141 ASSERT(be16_to_cpu(*xfs_dir2_data_entry_tag_p(dep
)) ==
142 (char *)dep
- (char *)hdr
);
145 if (be32_to_cpu(hdr
->magic
) == XFS_DIR2_BLOCK_MAGIC
) {
146 addr
= xfs_dir2_db_off_to_dataptr(mp
, mp
->m_dirdatablk
,
147 (xfs_dir2_data_aoff_t
)
148 ((char *)dep
- (char *)hdr
));
149 name
.name
= dep
->name
;
150 name
.len
= dep
->namelen
;
151 hash
= mp
->m_dirnameops
->hashname(&name
);
152 for (i
= 0; i
< be32_to_cpu(btp
->count
); i
++) {
153 if (be32_to_cpu(lep
[i
].address
) == addr
&&
154 be32_to_cpu(lep
[i
].hashval
) == hash
)
157 ASSERT(i
< be32_to_cpu(btp
->count
));
159 p
+= xfs_dir2_data_entsize(dep
->namelen
);
162 * Need to have seen all the entries and all the bestfree slots.
164 ASSERT(freeseen
== 7);
165 if (be32_to_cpu(hdr
->magic
) == XFS_DIR2_BLOCK_MAGIC
) {
166 for (i
= stale
= 0; i
< be32_to_cpu(btp
->count
); i
++) {
167 if (be32_to_cpu(lep
[i
].address
) == XFS_DIR2_NULL_DATAPTR
)
170 ASSERT(be32_to_cpu(lep
[i
].hashval
) >= be32_to_cpu(lep
[i
- 1].hashval
));
172 ASSERT(count
== be32_to_cpu(btp
->count
) - be32_to_cpu(btp
->stale
));
173 ASSERT(stale
== be32_to_cpu(btp
->stale
));
179 * Given a data block and an unused entry from that block,
180 * return the bestfree entry if any that corresponds to it.
182 STATIC xfs_dir2_data_free_t
*
183 xfs_dir2_data_freefind(
184 xfs_dir2_data_hdr_t
*hdr
, /* data block */
185 xfs_dir2_data_unused_t
*dup
) /* data unused entry */
187 xfs_dir2_data_free_t
*dfp
; /* bestfree entry */
188 xfs_dir2_data_aoff_t off
; /* offset value needed */
189 #if defined(DEBUG) && defined(__KERNEL__)
190 int matched
; /* matched the value */
191 int seenzero
; /* saw a 0 bestfree entry */
194 off
= (xfs_dir2_data_aoff_t
)((char *)dup
- (char *)hdr
);
195 #if defined(DEBUG) && defined(__KERNEL__)
197 * Validate some consistency in the bestfree table.
198 * Check order, non-overlapping entries, and if we find the
199 * one we're looking for it has to be exact.
201 ASSERT(be32_to_cpu(hdr
->magic
) == XFS_DIR2_DATA_MAGIC
||
202 be32_to_cpu(hdr
->magic
) == XFS_DIR2_BLOCK_MAGIC
);
203 for (dfp
= &hdr
->bestfree
[0], seenzero
= matched
= 0;
204 dfp
< &hdr
->bestfree
[XFS_DIR2_DATA_FD_COUNT
];
207 ASSERT(!dfp
->length
);
211 ASSERT(seenzero
== 0);
212 if (be16_to_cpu(dfp
->offset
) == off
) {
214 ASSERT(dfp
->length
== dup
->length
);
215 } else if (off
< be16_to_cpu(dfp
->offset
))
216 ASSERT(off
+ be16_to_cpu(dup
->length
) <= be16_to_cpu(dfp
->offset
));
218 ASSERT(be16_to_cpu(dfp
->offset
) + be16_to_cpu(dfp
->length
) <= off
);
219 ASSERT(matched
|| be16_to_cpu(dfp
->length
) >= be16_to_cpu(dup
->length
));
220 if (dfp
> &hdr
->bestfree
[0])
221 ASSERT(be16_to_cpu(dfp
[-1].length
) >= be16_to_cpu(dfp
[0].length
));
225 * If this is smaller than the smallest bestfree entry,
226 * it can't be there since they're sorted.
228 if (be16_to_cpu(dup
->length
) <
229 be16_to_cpu(hdr
->bestfree
[XFS_DIR2_DATA_FD_COUNT
- 1].length
))
232 * Look at the three bestfree entries for our guy.
234 for (dfp
= &hdr
->bestfree
[0];
235 dfp
< &hdr
->bestfree
[XFS_DIR2_DATA_FD_COUNT
];
239 if (be16_to_cpu(dfp
->offset
) == off
)
243 * Didn't find it. This only happens if there are duplicate lengths.
249 * Insert an unused-space entry into the bestfree table.
251 xfs_dir2_data_free_t
* /* entry inserted */
252 xfs_dir2_data_freeinsert(
253 xfs_dir2_data_hdr_t
*hdr
, /* data block pointer */
254 xfs_dir2_data_unused_t
*dup
, /* unused space */
255 int *loghead
) /* log the data header (out) */
257 xfs_dir2_data_free_t
*dfp
; /* bestfree table pointer */
258 xfs_dir2_data_free_t
new; /* new bestfree entry */
261 ASSERT(be32_to_cpu(hdr
->magic
) == XFS_DIR2_DATA_MAGIC
||
262 be32_to_cpu(hdr
->magic
) == XFS_DIR2_BLOCK_MAGIC
);
265 new.length
= dup
->length
;
266 new.offset
= cpu_to_be16((char *)dup
- (char *)hdr
);
269 * Insert at position 0, 1, or 2; or not at all.
271 if (be16_to_cpu(new.length
) > be16_to_cpu(dfp
[0].length
)) {
278 if (be16_to_cpu(new.length
) > be16_to_cpu(dfp
[1].length
)) {
284 if (be16_to_cpu(new.length
) > be16_to_cpu(dfp
[2].length
)) {
293 * Remove a bestfree entry from the table.
296 xfs_dir2_data_freeremove(
297 xfs_dir2_data_hdr_t
*hdr
, /* data block header */
298 xfs_dir2_data_free_t
*dfp
, /* bestfree entry pointer */
299 int *loghead
) /* out: log data header */
302 ASSERT(be32_to_cpu(hdr
->magic
) == XFS_DIR2_DATA_MAGIC
||
303 be32_to_cpu(hdr
->magic
) == XFS_DIR2_BLOCK_MAGIC
);
306 * It's the first entry, slide the next 2 up.
308 if (dfp
== &hdr
->bestfree
[0]) {
309 hdr
->bestfree
[0] = hdr
->bestfree
[1];
310 hdr
->bestfree
[1] = hdr
->bestfree
[2];
313 * It's the second entry, slide the 3rd entry up.
315 else if (dfp
== &hdr
->bestfree
[1])
316 hdr
->bestfree
[1] = hdr
->bestfree
[2];
318 * Must be the last entry.
321 ASSERT(dfp
== &hdr
->bestfree
[2]);
323 * Clear the 3rd entry, must be zero now.
325 hdr
->bestfree
[2].length
= 0;
326 hdr
->bestfree
[2].offset
= 0;
331 * Given a data block, reconstruct its bestfree map.
334 xfs_dir2_data_freescan(
335 xfs_mount_t
*mp
, /* filesystem mount point */
336 xfs_dir2_data_hdr_t
*hdr
, /* data block header */
337 int *loghead
) /* out: log data header */
339 xfs_dir2_data_t
*d
= (xfs_dir2_data_t
*)hdr
;
340 xfs_dir2_block_tail_t
*btp
; /* block tail */
341 xfs_dir2_data_entry_t
*dep
; /* active data entry */
342 xfs_dir2_data_unused_t
*dup
; /* unused data entry */
343 char *endp
; /* end of block's data */
344 char *p
; /* current entry pointer */
347 ASSERT(be32_to_cpu(hdr
->magic
) == XFS_DIR2_DATA_MAGIC
||
348 be32_to_cpu(hdr
->magic
) == XFS_DIR2_BLOCK_MAGIC
);
351 * Start by clearing the table.
353 memset(hdr
->bestfree
, 0, sizeof(hdr
->bestfree
));
359 if (be32_to_cpu(hdr
->magic
) == XFS_DIR2_BLOCK_MAGIC
) {
360 btp
= xfs_dir2_block_tail_p(mp
, hdr
);
361 endp
= (char *)xfs_dir2_block_leaf_p(btp
);
363 endp
= (char *)hdr
+ mp
->m_dirblksize
;
365 * Loop over the block's entries.
368 dup
= (xfs_dir2_data_unused_t
*)p
;
370 * If it's a free entry, insert it.
372 if (be16_to_cpu(dup
->freetag
) == XFS_DIR2_DATA_FREE_TAG
) {
373 ASSERT((char *)dup
- (char *)hdr
==
374 be16_to_cpu(*xfs_dir2_data_unused_tag_p(dup
)));
375 xfs_dir2_data_freeinsert(hdr
, dup
, loghead
);
376 p
+= be16_to_cpu(dup
->length
);
379 * For active entries, check their tags and skip them.
382 dep
= (xfs_dir2_data_entry_t
*)p
;
383 ASSERT((char *)dep
- (char *)hdr
==
384 be16_to_cpu(*xfs_dir2_data_entry_tag_p(dep
)));
385 p
+= xfs_dir2_data_entsize(dep
->namelen
);
391 * Initialize a data block at the given block number in the directory.
392 * Give back the buffer for the created block.
396 xfs_da_args_t
*args
, /* directory operation args */
397 xfs_dir2_db_t blkno
, /* logical dir block number */
398 xfs_dabuf_t
**bpp
) /* output block buffer */
400 xfs_dabuf_t
*bp
; /* block buffer */
401 xfs_dir2_data_t
*d
; /* pointer to block */
402 xfs_dir2_data_hdr_t
*hdr
; /* data block header */
403 xfs_inode_t
*dp
; /* incore directory inode */
404 xfs_dir2_data_unused_t
*dup
; /* unused entry pointer */
405 int error
; /* error return value */
406 int i
; /* bestfree index */
407 xfs_mount_t
*mp
; /* filesystem mount point */
408 xfs_trans_t
*tp
; /* transaction pointer */
415 * Get the buffer set up for the block.
417 error
= xfs_da_get_buf(tp
, dp
, xfs_dir2_db_to_da(mp
, blkno
), -1, &bp
,
425 * Initialize the header.
429 hdr
->magic
= cpu_to_be32(XFS_DIR2_DATA_MAGIC
);
430 hdr
->bestfree
[0].offset
= cpu_to_be16(sizeof(*hdr
));
431 for (i
= 1; i
< XFS_DIR2_DATA_FD_COUNT
; i
++) {
432 hdr
->bestfree
[i
].length
= 0;
433 hdr
->bestfree
[i
].offset
= 0;
437 * Set up an unused entry for the block's body.
439 dup
= &d
->u
[0].unused
;
440 dup
->freetag
= cpu_to_be16(XFS_DIR2_DATA_FREE_TAG
);
442 t
= mp
->m_dirblksize
- (uint
)sizeof(*hdr
);
443 hdr
->bestfree
[0].length
= cpu_to_be16(t
);
444 dup
->length
= cpu_to_be16(t
);
445 *xfs_dir2_data_unused_tag_p(dup
) = cpu_to_be16((char *)dup
- (char *)hdr
);
447 * Log it and return it.
449 xfs_dir2_data_log_header(tp
, bp
);
450 xfs_dir2_data_log_unused(tp
, bp
, dup
);
456 * Log an active data entry from the block.
459 xfs_dir2_data_log_entry(
460 xfs_trans_t
*tp
, /* transaction pointer */
461 xfs_dabuf_t
*bp
, /* block buffer */
462 xfs_dir2_data_entry_t
*dep
) /* data entry pointer */
464 xfs_dir2_data_hdr_t
*hdr
= bp
->data
;
466 ASSERT(be32_to_cpu(hdr
->magic
) == XFS_DIR2_DATA_MAGIC
||
467 be32_to_cpu(hdr
->magic
) == XFS_DIR2_BLOCK_MAGIC
);
469 xfs_da_log_buf(tp
, bp
, (uint
)((char *)dep
- (char *)hdr
),
470 (uint
)((char *)(xfs_dir2_data_entry_tag_p(dep
) + 1) -
475 * Log a data block header.
478 xfs_dir2_data_log_header(
479 xfs_trans_t
*tp
, /* transaction pointer */
480 xfs_dabuf_t
*bp
) /* block buffer */
482 xfs_dir2_data_hdr_t
*hdr
= bp
->data
;
484 ASSERT(be32_to_cpu(hdr
->magic
) == XFS_DIR2_DATA_MAGIC
||
485 be32_to_cpu(hdr
->magic
) == XFS_DIR2_BLOCK_MAGIC
);
487 xfs_da_log_buf(tp
, bp
, 0, sizeof(*hdr
) - 1);
491 * Log a data unused entry.
494 xfs_dir2_data_log_unused(
495 xfs_trans_t
*tp
, /* transaction pointer */
496 xfs_dabuf_t
*bp
, /* block buffer */
497 xfs_dir2_data_unused_t
*dup
) /* data unused pointer */
499 xfs_dir2_data_hdr_t
*hdr
= bp
->data
;
501 ASSERT(be32_to_cpu(hdr
->magic
) == XFS_DIR2_DATA_MAGIC
||
502 be32_to_cpu(hdr
->magic
) == XFS_DIR2_BLOCK_MAGIC
);
505 * Log the first part of the unused entry.
507 xfs_da_log_buf(tp
, bp
, (uint
)((char *)dup
- (char *)hdr
),
508 (uint
)((char *)&dup
->length
+ sizeof(dup
->length
) -
511 * Log the end (tag) of the unused entry.
513 xfs_da_log_buf(tp
, bp
,
514 (uint
)((char *)xfs_dir2_data_unused_tag_p(dup
) - (char *)hdr
),
515 (uint
)((char *)xfs_dir2_data_unused_tag_p(dup
) - (char *)hdr
+
516 sizeof(xfs_dir2_data_off_t
) - 1));
520 * Make a byte range in the data block unused.
521 * Its current contents are unimportant.
524 xfs_dir2_data_make_free(
525 xfs_trans_t
*tp
, /* transaction pointer */
526 xfs_dabuf_t
*bp
, /* block buffer */
527 xfs_dir2_data_aoff_t offset
, /* starting byte offset */
528 xfs_dir2_data_aoff_t len
, /* length in bytes */
529 int *needlogp
, /* out: log header */
530 int *needscanp
) /* out: regen bestfree */
532 xfs_dir2_data_hdr_t
*hdr
; /* data block pointer */
533 xfs_dir2_data_free_t
*dfp
; /* bestfree pointer */
534 char *endptr
; /* end of data area */
535 xfs_mount_t
*mp
; /* filesystem mount point */
536 int needscan
; /* need to regen bestfree */
537 xfs_dir2_data_unused_t
*newdup
; /* new unused entry */
538 xfs_dir2_data_unused_t
*postdup
; /* unused entry after us */
539 xfs_dir2_data_unused_t
*prevdup
; /* unused entry before us */
545 * Figure out where the end of the data area is.
547 if (be32_to_cpu(hdr
->magic
) == XFS_DIR2_DATA_MAGIC
)
548 endptr
= (char *)hdr
+ mp
->m_dirblksize
;
550 xfs_dir2_block_tail_t
*btp
; /* block tail */
552 ASSERT(be32_to_cpu(hdr
->magic
) == XFS_DIR2_BLOCK_MAGIC
);
553 btp
= xfs_dir2_block_tail_p(mp
, hdr
);
554 endptr
= (char *)xfs_dir2_block_leaf_p(btp
);
557 * If this isn't the start of the block, then back up to
558 * the previous entry and see if it's free.
560 if (offset
> sizeof(*hdr
)) {
561 __be16
*tagp
; /* tag just before us */
563 tagp
= (__be16
*)((char *)hdr
+ offset
) - 1;
564 prevdup
= (xfs_dir2_data_unused_t
*)((char *)hdr
+ be16_to_cpu(*tagp
));
565 if (be16_to_cpu(prevdup
->freetag
) != XFS_DIR2_DATA_FREE_TAG
)
570 * If this isn't the end of the block, see if the entry after
573 if ((char *)hdr
+ offset
+ len
< endptr
) {
575 (xfs_dir2_data_unused_t
*)((char *)hdr
+ offset
+ len
);
576 if (be16_to_cpu(postdup
->freetag
) != XFS_DIR2_DATA_FREE_TAG
)
580 ASSERT(*needscanp
== 0);
583 * Previous and following entries are both free,
584 * merge everything into a single free entry.
586 if (prevdup
&& postdup
) {
587 xfs_dir2_data_free_t
*dfp2
; /* another bestfree pointer */
590 * See if prevdup and/or postdup are in bestfree table.
592 dfp
= xfs_dir2_data_freefind(hdr
, prevdup
);
593 dfp2
= xfs_dir2_data_freefind(hdr
, postdup
);
595 * We need a rescan unless there are exactly 2 free entries
596 * namely our two. Then we know what's happening, otherwise
597 * since the third bestfree is there, there might be more
600 needscan
= (hdr
->bestfree
[2].length
!= 0);
602 * Fix up the new big freespace.
604 be16_add_cpu(&prevdup
->length
, len
+ be16_to_cpu(postdup
->length
));
605 *xfs_dir2_data_unused_tag_p(prevdup
) =
606 cpu_to_be16((char *)prevdup
- (char *)hdr
);
607 xfs_dir2_data_log_unused(tp
, bp
, prevdup
);
610 * Has to be the case that entries 0 and 1 are
611 * dfp and dfp2 (don't know which is which), and
613 * Remove entry 1 first then entry 0.
616 if (dfp
== &hdr
->bestfree
[1]) {
617 dfp
= &hdr
->bestfree
[0];
619 dfp2
= &hdr
->bestfree
[1];
621 xfs_dir2_data_freeremove(hdr
, dfp2
, needlogp
);
622 xfs_dir2_data_freeremove(hdr
, dfp
, needlogp
);
624 * Now insert the new entry.
626 dfp
= xfs_dir2_data_freeinsert(hdr
, prevdup
, needlogp
);
627 ASSERT(dfp
== &hdr
->bestfree
[0]);
628 ASSERT(dfp
->length
== prevdup
->length
);
629 ASSERT(!dfp
[1].length
);
630 ASSERT(!dfp
[2].length
);
634 * The entry before us is free, merge with it.
637 dfp
= xfs_dir2_data_freefind(hdr
, prevdup
);
638 be16_add_cpu(&prevdup
->length
, len
);
639 *xfs_dir2_data_unused_tag_p(prevdup
) =
640 cpu_to_be16((char *)prevdup
- (char *)hdr
);
641 xfs_dir2_data_log_unused(tp
, bp
, prevdup
);
643 * If the previous entry was in the table, the new entry
644 * is longer, so it will be in the table too. Remove
645 * the old one and add the new one.
648 xfs_dir2_data_freeremove(hdr
, dfp
, needlogp
);
649 xfs_dir2_data_freeinsert(hdr
, prevdup
, needlogp
);
652 * Otherwise we need a scan if the new entry is big enough.
655 needscan
= be16_to_cpu(prevdup
->length
) >
656 be16_to_cpu(hdr
->bestfree
[2].length
);
660 * The following entry is free, merge with it.
663 dfp
= xfs_dir2_data_freefind(hdr
, postdup
);
664 newdup
= (xfs_dir2_data_unused_t
*)((char *)hdr
+ offset
);
665 newdup
->freetag
= cpu_to_be16(XFS_DIR2_DATA_FREE_TAG
);
666 newdup
->length
= cpu_to_be16(len
+ be16_to_cpu(postdup
->length
));
667 *xfs_dir2_data_unused_tag_p(newdup
) =
668 cpu_to_be16((char *)newdup
- (char *)hdr
);
669 xfs_dir2_data_log_unused(tp
, bp
, newdup
);
671 * If the following entry was in the table, the new entry
672 * is longer, so it will be in the table too. Remove
673 * the old one and add the new one.
676 xfs_dir2_data_freeremove(hdr
, dfp
, needlogp
);
677 xfs_dir2_data_freeinsert(hdr
, newdup
, needlogp
);
680 * Otherwise we need a scan if the new entry is big enough.
683 needscan
= be16_to_cpu(newdup
->length
) >
684 be16_to_cpu(hdr
->bestfree
[2].length
);
688 * Neither neighbor is free. Make a new entry.
691 newdup
= (xfs_dir2_data_unused_t
*)((char *)hdr
+ offset
);
692 newdup
->freetag
= cpu_to_be16(XFS_DIR2_DATA_FREE_TAG
);
693 newdup
->length
= cpu_to_be16(len
);
694 *xfs_dir2_data_unused_tag_p(newdup
) =
695 cpu_to_be16((char *)newdup
- (char *)hdr
);
696 xfs_dir2_data_log_unused(tp
, bp
, newdup
);
697 xfs_dir2_data_freeinsert(hdr
, newdup
, needlogp
);
699 *needscanp
= needscan
;
703 * Take a byte range out of an existing unused space and make it un-free.
706 xfs_dir2_data_use_free(
707 xfs_trans_t
*tp
, /* transaction pointer */
708 xfs_dabuf_t
*bp
, /* data block buffer */
709 xfs_dir2_data_unused_t
*dup
, /* unused entry */
710 xfs_dir2_data_aoff_t offset
, /* starting offset to use */
711 xfs_dir2_data_aoff_t len
, /* length to use */
712 int *needlogp
, /* out: need to log header */
713 int *needscanp
) /* out: need regen bestfree */
715 xfs_dir2_data_hdr_t
*hdr
; /* data block header */
716 xfs_dir2_data_free_t
*dfp
; /* bestfree pointer */
717 int matchback
; /* matches end of freespace */
718 int matchfront
; /* matches start of freespace */
719 int needscan
; /* need to regen bestfree */
720 xfs_dir2_data_unused_t
*newdup
; /* new unused entry */
721 xfs_dir2_data_unused_t
*newdup2
; /* another new unused entry */
722 int oldlen
; /* old unused entry's length */
725 ASSERT(be32_to_cpu(hdr
->magic
) == XFS_DIR2_DATA_MAGIC
||
726 be32_to_cpu(hdr
->magic
) == XFS_DIR2_BLOCK_MAGIC
);
727 ASSERT(be16_to_cpu(dup
->freetag
) == XFS_DIR2_DATA_FREE_TAG
);
728 ASSERT(offset
>= (char *)dup
- (char *)hdr
);
729 ASSERT(offset
+ len
<= (char *)dup
+ be16_to_cpu(dup
->length
) - (char *)hdr
);
730 ASSERT((char *)dup
- (char *)hdr
== be16_to_cpu(*xfs_dir2_data_unused_tag_p(dup
)));
732 * Look up the entry in the bestfree table.
734 dfp
= xfs_dir2_data_freefind(hdr
, dup
);
735 oldlen
= be16_to_cpu(dup
->length
);
736 ASSERT(dfp
|| oldlen
<= be16_to_cpu(hdr
->bestfree
[2].length
));
738 * Check for alignment with front and back of the entry.
740 matchfront
= (char *)dup
- (char *)hdr
== offset
;
741 matchback
= (char *)dup
+ oldlen
- (char *)hdr
== offset
+ len
;
742 ASSERT(*needscanp
== 0);
745 * If we matched it exactly we just need to get rid of it from
746 * the bestfree table.
748 if (matchfront
&& matchback
) {
750 needscan
= (hdr
->bestfree
[2].offset
!= 0);
752 xfs_dir2_data_freeremove(hdr
, dfp
, needlogp
);
756 * We match the first part of the entry.
757 * Make a new entry with the remaining freespace.
759 else if (matchfront
) {
760 newdup
= (xfs_dir2_data_unused_t
*)((char *)hdr
+ offset
+ len
);
761 newdup
->freetag
= cpu_to_be16(XFS_DIR2_DATA_FREE_TAG
);
762 newdup
->length
= cpu_to_be16(oldlen
- len
);
763 *xfs_dir2_data_unused_tag_p(newdup
) =
764 cpu_to_be16((char *)newdup
- (char *)hdr
);
765 xfs_dir2_data_log_unused(tp
, bp
, newdup
);
767 * If it was in the table, remove it and add the new one.
770 xfs_dir2_data_freeremove(hdr
, dfp
, needlogp
);
771 dfp
= xfs_dir2_data_freeinsert(hdr
, newdup
, needlogp
);
773 ASSERT(dfp
->length
== newdup
->length
);
774 ASSERT(be16_to_cpu(dfp
->offset
) == (char *)newdup
- (char *)hdr
);
776 * If we got inserted at the last slot,
777 * that means we don't know if there was a better
778 * choice for the last slot, or not. Rescan.
780 needscan
= dfp
== &hdr
->bestfree
[2];
784 * We match the last part of the entry.
785 * Trim the allocated space off the tail of the entry.
787 else if (matchback
) {
789 newdup
->length
= cpu_to_be16(((char *)hdr
+ offset
) - (char *)newdup
);
790 *xfs_dir2_data_unused_tag_p(newdup
) =
791 cpu_to_be16((char *)newdup
- (char *)hdr
);
792 xfs_dir2_data_log_unused(tp
, bp
, newdup
);
794 * If it was in the table, remove it and add the new one.
797 xfs_dir2_data_freeremove(hdr
, dfp
, needlogp
);
798 dfp
= xfs_dir2_data_freeinsert(hdr
, newdup
, needlogp
);
800 ASSERT(dfp
->length
== newdup
->length
);
801 ASSERT(be16_to_cpu(dfp
->offset
) == (char *)newdup
- (char *)hdr
);
803 * If we got inserted at the last slot,
804 * that means we don't know if there was a better
805 * choice for the last slot, or not. Rescan.
807 needscan
= dfp
== &hdr
->bestfree
[2];
811 * Poking out the middle of an entry.
812 * Make two new entries.
816 newdup
->length
= cpu_to_be16(((char *)hdr
+ offset
) - (char *)newdup
);
817 *xfs_dir2_data_unused_tag_p(newdup
) =
818 cpu_to_be16((char *)newdup
- (char *)hdr
);
819 xfs_dir2_data_log_unused(tp
, bp
, newdup
);
820 newdup2
= (xfs_dir2_data_unused_t
*)((char *)hdr
+ offset
+ len
);
821 newdup2
->freetag
= cpu_to_be16(XFS_DIR2_DATA_FREE_TAG
);
822 newdup2
->length
= cpu_to_be16(oldlen
- len
- be16_to_cpu(newdup
->length
));
823 *xfs_dir2_data_unused_tag_p(newdup2
) =
824 cpu_to_be16((char *)newdup2
- (char *)hdr
);
825 xfs_dir2_data_log_unused(tp
, bp
, newdup2
);
827 * If the old entry was in the table, we need to scan
828 * if the 3rd entry was valid, since these entries
829 * are smaller than the old one.
830 * If we don't need to scan that means there were 1 or 2
831 * entries in the table, and removing the old and adding
832 * the 2 new will work.
835 needscan
= (hdr
->bestfree
[2].length
!= 0);
837 xfs_dir2_data_freeremove(hdr
, dfp
, needlogp
);
838 xfs_dir2_data_freeinsert(hdr
, newdup
, needlogp
);
839 xfs_dir2_data_freeinsert(hdr
, newdup2
,
844 *needscanp
= needscan
;