2 * Copyright (c) 2000-2002 Silicon Graphics, Inc. All Rights Reserved.
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of version 2 of the GNU General Public License as
6 * published by the Free Software Foundation.
8 * This program is distributed in the hope that it would be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12 * Further, this software is distributed without any warranty that it is
13 * free of the rightful claim of any third person regarding infringement
14 * or the like. Any license provided herein, whether implied or
15 * otherwise, applies only to this software file. Patent licenses, if
16 * any, provided herein do not apply to combinations of this program with
17 * other software, or any other product whatsoever.
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write the Free Software Foundation, Inc., 59
21 * Temple Place - Suite 330, Boston MA 02111-1307, USA.
23 * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
24 * Mountain View, CA 94043, or:
28 * For further information regarding this notice, see:
30 * http://oss.sgi.com/projects/GenInfo/SGIGPLNoticeExplan/
34 #include "xfs_types.h"
37 #include "xfs_trans.h"
41 #include "xfs_dmapi.h"
42 #include "xfs_mount.h"
43 #include "xfs_da_btree.h"
44 #include "xfs_bmap_btree.h"
45 #include "xfs_dir_sf.h"
46 #include "xfs_dir2_sf.h"
47 #include "xfs_attr_sf.h"
48 #include "xfs_dinode.h"
49 #include "xfs_inode.h"
50 #include "xfs_dir_leaf.h"
51 #include "xfs_dir2_data.h"
52 #include "xfs_dir2_leaf.h"
53 #include "xfs_dir2_block.h"
54 #include "xfs_error.h"
58 * Check the consistency of the data block.
59 * The input can also be a block-format directory.
60 * Pop an assert if we find anything bad.
64 xfs_inode_t
*dp
, /* incore inode pointer */
65 xfs_dabuf_t
*bp
) /* data block's buffer */
67 xfs_dir2_dataptr_t addr
; /* addr for leaf lookup */
68 xfs_dir2_data_free_t
*bf
; /* bestfree table */
69 xfs_dir2_block_tail_t
*btp
=NULL
; /* block tail */
70 int count
; /* count of entries found */
71 xfs_dir2_data_t
*d
; /* data block pointer */
72 xfs_dir2_data_entry_t
*dep
; /* data entry */
73 xfs_dir2_data_free_t
*dfp
; /* bestfree entry */
74 xfs_dir2_data_unused_t
*dup
; /* unused entry */
75 char *endp
; /* end of useful data */
76 int freeseen
; /* mask of bestfrees seen */
77 xfs_dahash_t hash
; /* hash of current name */
78 int i
; /* leaf index */
79 int lastfree
; /* last entry was unused */
80 xfs_dir2_leaf_entry_t
*lep
=NULL
; /* block leaf entries */
81 xfs_mount_t
*mp
; /* filesystem mount point */
82 char *p
; /* current data position */
83 int stale
; /* count of stale leaves */
87 ASSERT(INT_GET(d
->hdr
.magic
, ARCH_CONVERT
) == XFS_DIR2_DATA_MAGIC
||
88 INT_GET(d
->hdr
.magic
, ARCH_CONVERT
) == XFS_DIR2_BLOCK_MAGIC
);
91 if (INT_GET(d
->hdr
.magic
, ARCH_CONVERT
) == XFS_DIR2_BLOCK_MAGIC
) {
92 btp
= XFS_DIR2_BLOCK_TAIL_P(mp
, (xfs_dir2_block_t
*)d
);
93 lep
= XFS_DIR2_BLOCK_LEAF_P(btp
);
96 endp
= (char *)d
+ mp
->m_dirblksize
;
97 count
= lastfree
= freeseen
= 0;
99 * Account for zero bestfree entries.
102 ASSERT(!bf
[0].offset
);
106 ASSERT(!bf
[1].offset
);
110 ASSERT(!bf
[2].offset
);
113 ASSERT(INT_GET(bf
[0].length
, ARCH_CONVERT
) >= INT_GET(bf
[1].length
, ARCH_CONVERT
));
114 ASSERT(INT_GET(bf
[1].length
, ARCH_CONVERT
) >= INT_GET(bf
[2].length
, ARCH_CONVERT
));
116 * Loop over the data/unused entries.
119 dup
= (xfs_dir2_data_unused_t
*)p
;
121 * If it's unused, look for the space in the bestfree table.
122 * If we find it, account for that, else make sure it
123 * doesn't need to be there.
125 if (INT_GET(dup
->freetag
, ARCH_CONVERT
) == XFS_DIR2_DATA_FREE_TAG
) {
126 ASSERT(lastfree
== 0);
127 ASSERT(INT_GET(*XFS_DIR2_DATA_UNUSED_TAG_P(dup
), ARCH_CONVERT
) ==
128 (char *)dup
- (char *)d
);
129 dfp
= xfs_dir2_data_freefind(d
, dup
);
132 ASSERT((freeseen
& (1 << i
)) == 0);
135 ASSERT(INT_GET(dup
->length
, ARCH_CONVERT
) <= INT_GET(bf
[2].length
, ARCH_CONVERT
));
136 p
+= INT_GET(dup
->length
, ARCH_CONVERT
);
141 * It's a real entry. Validate the fields.
142 * If this is a block directory then make sure it's
143 * in the leaf section of the block.
144 * The linear search is crude but this is DEBUG code.
146 dep
= (xfs_dir2_data_entry_t
*)p
;
147 ASSERT(dep
->namelen
!= 0);
148 ASSERT(xfs_dir_ino_validate(mp
, INT_GET(dep
->inumber
, ARCH_CONVERT
)) == 0);
149 ASSERT(INT_GET(*XFS_DIR2_DATA_ENTRY_TAG_P(dep
), ARCH_CONVERT
) ==
150 (char *)dep
- (char *)d
);
153 if (INT_GET(d
->hdr
.magic
, ARCH_CONVERT
) == XFS_DIR2_BLOCK_MAGIC
) {
154 addr
= XFS_DIR2_DB_OFF_TO_DATAPTR(mp
, mp
->m_dirdatablk
,
155 (xfs_dir2_data_aoff_t
)
156 ((char *)dep
- (char *)d
));
157 hash
= xfs_da_hashname((char *)dep
->name
, dep
->namelen
);
158 for (i
= 0; i
< INT_GET(btp
->count
, ARCH_CONVERT
); i
++) {
159 if (INT_GET(lep
[i
].address
, ARCH_CONVERT
) == addr
&&
160 INT_GET(lep
[i
].hashval
, ARCH_CONVERT
) == hash
)
163 ASSERT(i
< INT_GET(btp
->count
, ARCH_CONVERT
));
165 p
+= XFS_DIR2_DATA_ENTSIZE(dep
->namelen
);
168 * Need to have seen all the entries and all the bestfree slots.
170 ASSERT(freeseen
== 7);
171 if (INT_GET(d
->hdr
.magic
, ARCH_CONVERT
) == XFS_DIR2_BLOCK_MAGIC
) {
172 for (i
= stale
= 0; i
< INT_GET(btp
->count
, ARCH_CONVERT
); i
++) {
173 if (INT_GET(lep
[i
].address
, ARCH_CONVERT
) == XFS_DIR2_NULL_DATAPTR
)
176 ASSERT(INT_GET(lep
[i
].hashval
, ARCH_CONVERT
) >= INT_GET(lep
[i
- 1].hashval
, ARCH_CONVERT
));
178 ASSERT(count
== INT_GET(btp
->count
, ARCH_CONVERT
) - INT_GET(btp
->stale
, ARCH_CONVERT
));
179 ASSERT(stale
== INT_GET(btp
->stale
, ARCH_CONVERT
));
185 * Given a data block and an unused entry from that block,
186 * return the bestfree entry if any that corresponds to it.
188 xfs_dir2_data_free_t
*
189 xfs_dir2_data_freefind(
190 xfs_dir2_data_t
*d
, /* data block */
191 xfs_dir2_data_unused_t
*dup
) /* data unused entry */
193 xfs_dir2_data_free_t
*dfp
; /* bestfree entry */
194 xfs_dir2_data_aoff_t off
; /* offset value needed */
195 #if defined(DEBUG) && defined(__KERNEL__)
196 int matched
; /* matched the value */
197 int seenzero
; /* saw a 0 bestfree entry */
200 off
= (xfs_dir2_data_aoff_t
)((char *)dup
- (char *)d
);
201 #if defined(DEBUG) && defined(__KERNEL__)
203 * Validate some consistency in the bestfree table.
204 * Check order, non-overlapping entries, and if we find the
205 * one we're looking for it has to be exact.
207 ASSERT(INT_GET(d
->hdr
.magic
, ARCH_CONVERT
) == XFS_DIR2_DATA_MAGIC
||
208 INT_GET(d
->hdr
.magic
, ARCH_CONVERT
) == XFS_DIR2_BLOCK_MAGIC
);
209 for (dfp
= &d
->hdr
.bestfree
[0], seenzero
= matched
= 0;
210 dfp
< &d
->hdr
.bestfree
[XFS_DIR2_DATA_FD_COUNT
];
213 ASSERT(!dfp
->length
);
217 ASSERT(seenzero
== 0);
218 if (INT_GET(dfp
->offset
, ARCH_CONVERT
) == off
) {
220 ASSERT(INT_GET(dfp
->length
, ARCH_CONVERT
) == INT_GET(dup
->length
, ARCH_CONVERT
));
221 } else if (off
< INT_GET(dfp
->offset
, ARCH_CONVERT
))
222 ASSERT(off
+ INT_GET(dup
->length
, ARCH_CONVERT
) <= INT_GET(dfp
->offset
, ARCH_CONVERT
));
224 ASSERT(INT_GET(dfp
->offset
, ARCH_CONVERT
) + INT_GET(dfp
->length
, ARCH_CONVERT
) <= off
);
225 ASSERT(matched
|| INT_GET(dfp
->length
, ARCH_CONVERT
) >= INT_GET(dup
->length
, ARCH_CONVERT
));
226 if (dfp
> &d
->hdr
.bestfree
[0])
227 ASSERT(INT_GET(dfp
[-1].length
, ARCH_CONVERT
) >= INT_GET(dfp
[0].length
, ARCH_CONVERT
));
231 * If this is smaller than the smallest bestfree entry,
232 * it can't be there since they're sorted.
234 if (INT_GET(dup
->length
, ARCH_CONVERT
) < INT_GET(d
->hdr
.bestfree
[XFS_DIR2_DATA_FD_COUNT
- 1].length
, ARCH_CONVERT
))
237 * Look at the three bestfree entries for our guy.
239 for (dfp
= &d
->hdr
.bestfree
[0];
240 dfp
< &d
->hdr
.bestfree
[XFS_DIR2_DATA_FD_COUNT
];
244 if (INT_GET(dfp
->offset
, ARCH_CONVERT
) == off
)
248 * Didn't find it. This only happens if there are duplicate lengths.
254 * Insert an unused-space entry into the bestfree table.
256 xfs_dir2_data_free_t
* /* entry inserted */
257 xfs_dir2_data_freeinsert(
258 xfs_dir2_data_t
*d
, /* data block pointer */
259 xfs_dir2_data_unused_t
*dup
, /* unused space */
260 int *loghead
) /* log the data header (out) */
262 xfs_dir2_data_free_t
*dfp
; /* bestfree table pointer */
263 xfs_dir2_data_free_t
new; /* new bestfree entry */
266 ASSERT(INT_GET(d
->hdr
.magic
, ARCH_CONVERT
) == XFS_DIR2_DATA_MAGIC
||
267 INT_GET(d
->hdr
.magic
, ARCH_CONVERT
) == XFS_DIR2_BLOCK_MAGIC
);
269 dfp
= d
->hdr
.bestfree
;
270 INT_COPY(new.length
, dup
->length
, ARCH_CONVERT
);
271 INT_SET(new.offset
, ARCH_CONVERT
, (xfs_dir2_data_off_t
)((char *)dup
- (char *)d
));
273 * Insert at position 0, 1, or 2; or not at all.
275 if (INT_GET(new.length
, ARCH_CONVERT
) > INT_GET(dfp
[0].length
, ARCH_CONVERT
)) {
282 if (INT_GET(new.length
, ARCH_CONVERT
) > INT_GET(dfp
[1].length
, ARCH_CONVERT
)) {
288 if (INT_GET(new.length
, ARCH_CONVERT
) > INT_GET(dfp
[2].length
, ARCH_CONVERT
)) {
297 * Remove a bestfree entry from the table.
300 xfs_dir2_data_freeremove(
301 xfs_dir2_data_t
*d
, /* data block pointer */
302 xfs_dir2_data_free_t
*dfp
, /* bestfree entry pointer */
303 int *loghead
) /* out: log data header */
306 ASSERT(INT_GET(d
->hdr
.magic
, ARCH_CONVERT
) == XFS_DIR2_DATA_MAGIC
||
307 INT_GET(d
->hdr
.magic
, ARCH_CONVERT
) == XFS_DIR2_BLOCK_MAGIC
);
310 * It's the first entry, slide the next 2 up.
312 if (dfp
== &d
->hdr
.bestfree
[0]) {
313 d
->hdr
.bestfree
[0] = d
->hdr
.bestfree
[1];
314 d
->hdr
.bestfree
[1] = d
->hdr
.bestfree
[2];
317 * It's the second entry, slide the 3rd entry up.
319 else if (dfp
== &d
->hdr
.bestfree
[1])
320 d
->hdr
.bestfree
[1] = d
->hdr
.bestfree
[2];
322 * Must be the last entry.
325 ASSERT(dfp
== &d
->hdr
.bestfree
[2]);
327 * Clear the 3rd entry, must be zero now.
329 d
->hdr
.bestfree
[2].length
= 0;
330 d
->hdr
.bestfree
[2].offset
= 0;
335 * Given a data block, reconstruct its bestfree map.
338 xfs_dir2_data_freescan(
339 xfs_mount_t
*mp
, /* filesystem mount point */
340 xfs_dir2_data_t
*d
, /* data block pointer */
341 int *loghead
, /* out: log data header */
342 char *aendp
) /* in: caller's endp */
344 xfs_dir2_block_tail_t
*btp
; /* block tail */
345 xfs_dir2_data_entry_t
*dep
; /* active data entry */
346 xfs_dir2_data_unused_t
*dup
; /* unused data entry */
347 char *endp
; /* end of block's data */
348 char *p
; /* current entry pointer */
351 ASSERT(INT_GET(d
->hdr
.magic
, ARCH_CONVERT
) == XFS_DIR2_DATA_MAGIC
||
352 INT_GET(d
->hdr
.magic
, ARCH_CONVERT
) == XFS_DIR2_BLOCK_MAGIC
);
355 * Start by clearing the table.
357 memset(d
->hdr
.bestfree
, 0, sizeof(d
->hdr
.bestfree
));
365 else if (INT_GET(d
->hdr
.magic
, ARCH_CONVERT
) == XFS_DIR2_BLOCK_MAGIC
) {
366 btp
= XFS_DIR2_BLOCK_TAIL_P(mp
, (xfs_dir2_block_t
*)d
);
367 endp
= (char *)XFS_DIR2_BLOCK_LEAF_P(btp
);
369 endp
= (char *)d
+ mp
->m_dirblksize
;
371 * Loop over the block's entries.
374 dup
= (xfs_dir2_data_unused_t
*)p
;
376 * If it's a free entry, insert it.
378 if (INT_GET(dup
->freetag
, ARCH_CONVERT
) == XFS_DIR2_DATA_FREE_TAG
) {
379 ASSERT((char *)dup
- (char *)d
==
380 INT_GET(*XFS_DIR2_DATA_UNUSED_TAG_P(dup
), ARCH_CONVERT
));
381 xfs_dir2_data_freeinsert(d
, dup
, loghead
);
382 p
+= INT_GET(dup
->length
, ARCH_CONVERT
);
385 * For active entries, check their tags and skip them.
388 dep
= (xfs_dir2_data_entry_t
*)p
;
389 ASSERT((char *)dep
- (char *)d
==
390 INT_GET(*XFS_DIR2_DATA_ENTRY_TAG_P(dep
), ARCH_CONVERT
));
391 p
+= XFS_DIR2_DATA_ENTSIZE(dep
->namelen
);
397 * Initialize a data block at the given block number in the directory.
398 * Give back the buffer for the created block.
402 xfs_da_args_t
*args
, /* directory operation args */
403 xfs_dir2_db_t blkno
, /* logical dir block number */
404 xfs_dabuf_t
**bpp
) /* output block buffer */
406 xfs_dabuf_t
*bp
; /* block buffer */
407 xfs_dir2_data_t
*d
; /* pointer to block */
408 xfs_inode_t
*dp
; /* incore directory inode */
409 xfs_dir2_data_unused_t
*dup
; /* unused entry pointer */
410 int error
; /* error return value */
411 int i
; /* bestfree index */
412 xfs_mount_t
*mp
; /* filesystem mount point */
413 xfs_trans_t
*tp
; /* transaction pointer */
420 * Get the buffer set up for the block.
422 error
= xfs_da_get_buf(tp
, dp
, XFS_DIR2_DB_TO_DA(mp
, blkno
), -1, &bp
,
429 * Initialize the header.
432 INT_SET(d
->hdr
.magic
, ARCH_CONVERT
, XFS_DIR2_DATA_MAGIC
);
433 INT_SET(d
->hdr
.bestfree
[0].offset
, ARCH_CONVERT
, (xfs_dir2_data_off_t
)sizeof(d
->hdr
));
434 for (i
= 1; i
< XFS_DIR2_DATA_FD_COUNT
; i
++) {
435 d
->hdr
.bestfree
[i
].length
= 0;
436 d
->hdr
.bestfree
[i
].offset
= 0;
439 * Set up an unused entry for the block's body.
441 dup
= &d
->u
[0].unused
;
442 INT_SET(dup
->freetag
, ARCH_CONVERT
, XFS_DIR2_DATA_FREE_TAG
);
444 t
=mp
->m_dirblksize
- (uint
)sizeof(d
->hdr
);
445 INT_SET(d
->hdr
.bestfree
[0].length
, ARCH_CONVERT
, t
);
446 INT_SET(dup
->length
, ARCH_CONVERT
, t
);
447 INT_SET(*XFS_DIR2_DATA_UNUSED_TAG_P(dup
), ARCH_CONVERT
,
448 (xfs_dir2_data_off_t
)((char *)dup
- (char *)d
));
450 * Log it and return it.
452 xfs_dir2_data_log_header(tp
, bp
);
453 xfs_dir2_data_log_unused(tp
, bp
, dup
);
459 * Log an active data entry from the block.
462 xfs_dir2_data_log_entry(
463 xfs_trans_t
*tp
, /* transaction pointer */
464 xfs_dabuf_t
*bp
, /* block buffer */
465 xfs_dir2_data_entry_t
*dep
) /* data entry pointer */
467 xfs_dir2_data_t
*d
; /* data block pointer */
470 ASSERT(INT_GET(d
->hdr
.magic
, ARCH_CONVERT
) == XFS_DIR2_DATA_MAGIC
||
471 INT_GET(d
->hdr
.magic
, ARCH_CONVERT
) == XFS_DIR2_BLOCK_MAGIC
);
472 xfs_da_log_buf(tp
, bp
, (uint
)((char *)dep
- (char *)d
),
473 (uint
)((char *)(XFS_DIR2_DATA_ENTRY_TAG_P(dep
) + 1) -
478 * Log a data block header.
481 xfs_dir2_data_log_header(
482 xfs_trans_t
*tp
, /* transaction pointer */
483 xfs_dabuf_t
*bp
) /* block buffer */
485 xfs_dir2_data_t
*d
; /* data block pointer */
488 ASSERT(INT_GET(d
->hdr
.magic
, ARCH_CONVERT
) == XFS_DIR2_DATA_MAGIC
||
489 INT_GET(d
->hdr
.magic
, ARCH_CONVERT
) == XFS_DIR2_BLOCK_MAGIC
);
490 xfs_da_log_buf(tp
, bp
, (uint
)((char *)&d
->hdr
- (char *)d
),
491 (uint
)(sizeof(d
->hdr
) - 1));
495 * Log a data unused entry.
498 xfs_dir2_data_log_unused(
499 xfs_trans_t
*tp
, /* transaction pointer */
500 xfs_dabuf_t
*bp
, /* block buffer */
501 xfs_dir2_data_unused_t
*dup
) /* data unused pointer */
503 xfs_dir2_data_t
*d
; /* data block pointer */
506 ASSERT(INT_GET(d
->hdr
.magic
, ARCH_CONVERT
) == XFS_DIR2_DATA_MAGIC
||
507 INT_GET(d
->hdr
.magic
, ARCH_CONVERT
) == XFS_DIR2_BLOCK_MAGIC
);
509 * Log the first part of the unused entry.
511 xfs_da_log_buf(tp
, bp
, (uint
)((char *)dup
- (char *)d
),
512 (uint
)((char *)&dup
->length
+ sizeof(dup
->length
) -
515 * Log the end (tag) of the unused entry.
517 xfs_da_log_buf(tp
, bp
,
518 (uint
)((char *)XFS_DIR2_DATA_UNUSED_TAG_P(dup
) - (char *)d
),
519 (uint
)((char *)XFS_DIR2_DATA_UNUSED_TAG_P(dup
) - (char *)d
+
520 sizeof(xfs_dir2_data_off_t
) - 1));
524 * Make a byte range in the data block unused.
525 * Its current contents are unimportant.
528 xfs_dir2_data_make_free(
529 xfs_trans_t
*tp
, /* transaction pointer */
530 xfs_dabuf_t
*bp
, /* block buffer */
531 xfs_dir2_data_aoff_t offset
, /* starting byte offset */
532 xfs_dir2_data_aoff_t len
, /* length in bytes */
533 int *needlogp
, /* out: log header */
534 int *needscanp
) /* out: regen bestfree */
536 xfs_dir2_data_t
*d
; /* data block pointer */
537 xfs_dir2_data_free_t
*dfp
; /* bestfree pointer */
538 char *endptr
; /* end of data area */
539 xfs_mount_t
*mp
; /* filesystem mount point */
540 int needscan
; /* need to regen bestfree */
541 xfs_dir2_data_unused_t
*newdup
; /* new unused entry */
542 xfs_dir2_data_unused_t
*postdup
; /* unused entry after us */
543 xfs_dir2_data_unused_t
*prevdup
; /* unused entry before us */
548 * Figure out where the end of the data area is.
550 if (INT_GET(d
->hdr
.magic
, ARCH_CONVERT
) == XFS_DIR2_DATA_MAGIC
)
551 endptr
= (char *)d
+ mp
->m_dirblksize
;
553 xfs_dir2_block_tail_t
*btp
; /* block tail */
555 ASSERT(INT_GET(d
->hdr
.magic
, ARCH_CONVERT
) == XFS_DIR2_BLOCK_MAGIC
);
556 btp
= XFS_DIR2_BLOCK_TAIL_P(mp
, (xfs_dir2_block_t
*)d
);
557 endptr
= (char *)XFS_DIR2_BLOCK_LEAF_P(btp
);
560 * If this isn't the start of the block, then back up to
561 * the previous entry and see if it's free.
563 if (offset
> sizeof(d
->hdr
)) {
564 xfs_dir2_data_off_t
*tagp
; /* tag just before us */
566 tagp
= (xfs_dir2_data_off_t
*)((char *)d
+ offset
) - 1;
567 prevdup
= (xfs_dir2_data_unused_t
*)((char *)d
+ INT_GET(*tagp
, ARCH_CONVERT
));
568 if (INT_GET(prevdup
->freetag
, ARCH_CONVERT
) != XFS_DIR2_DATA_FREE_TAG
)
573 * If this isn't the end of the block, see if the entry after
576 if ((char *)d
+ offset
+ len
< endptr
) {
578 (xfs_dir2_data_unused_t
*)((char *)d
+ offset
+ len
);
579 if (INT_GET(postdup
->freetag
, ARCH_CONVERT
) != XFS_DIR2_DATA_FREE_TAG
)
583 ASSERT(*needscanp
== 0);
586 * Previous and following entries are both free,
587 * merge everything into a single free entry.
589 if (prevdup
&& postdup
) {
590 xfs_dir2_data_free_t
*dfp2
; /* another bestfree pointer */
593 * See if prevdup and/or postdup are in bestfree table.
595 dfp
= xfs_dir2_data_freefind(d
, prevdup
);
596 dfp2
= xfs_dir2_data_freefind(d
, postdup
);
598 * We need a rescan unless there are exactly 2 free entries
599 * namely our two. Then we know what's happening, otherwise
600 * since the third bestfree is there, there might be more
603 needscan
= d
->hdr
.bestfree
[2].length
;
605 * Fix up the new big freespace.
607 INT_MOD(prevdup
->length
, ARCH_CONVERT
, len
+ INT_GET(postdup
->length
, ARCH_CONVERT
));
608 INT_SET(*XFS_DIR2_DATA_UNUSED_TAG_P(prevdup
), ARCH_CONVERT
,
609 (xfs_dir2_data_off_t
)((char *)prevdup
- (char *)d
));
610 xfs_dir2_data_log_unused(tp
, bp
, prevdup
);
613 * Has to be the case that entries 0 and 1 are
614 * dfp and dfp2 (don't know which is which), and
616 * Remove entry 1 first then entry 0.
619 if (dfp
== &d
->hdr
.bestfree
[1]) {
620 dfp
= &d
->hdr
.bestfree
[0];
622 dfp2
= &d
->hdr
.bestfree
[1];
624 xfs_dir2_data_freeremove(d
, dfp2
, needlogp
);
625 xfs_dir2_data_freeremove(d
, dfp
, needlogp
);
627 * Now insert the new entry.
629 dfp
= xfs_dir2_data_freeinsert(d
, prevdup
, needlogp
);
630 ASSERT(dfp
== &d
->hdr
.bestfree
[0]);
631 ASSERT(INT_GET(dfp
->length
, ARCH_CONVERT
) == INT_GET(prevdup
->length
, ARCH_CONVERT
));
632 ASSERT(!dfp
[1].length
);
633 ASSERT(!dfp
[2].length
);
637 * The entry before us is free, merge with it.
640 dfp
= xfs_dir2_data_freefind(d
, prevdup
);
641 INT_MOD(prevdup
->length
, ARCH_CONVERT
, len
);
642 INT_SET(*XFS_DIR2_DATA_UNUSED_TAG_P(prevdup
), ARCH_CONVERT
,
643 (xfs_dir2_data_off_t
)((char *)prevdup
- (char *)d
));
644 xfs_dir2_data_log_unused(tp
, bp
, prevdup
);
646 * If the previous entry was in the table, the new entry
647 * is longer, so it will be in the table too. Remove
648 * the old one and add the new one.
651 xfs_dir2_data_freeremove(d
, dfp
, needlogp
);
652 (void)xfs_dir2_data_freeinsert(d
, prevdup
, needlogp
);
655 * Otherwise we need a scan if the new entry is big enough.
658 needscan
= INT_GET(prevdup
->length
, ARCH_CONVERT
) > INT_GET(d
->hdr
.bestfree
[2].length
, ARCH_CONVERT
);
661 * The following entry is free, merge with it.
664 dfp
= xfs_dir2_data_freefind(d
, postdup
);
665 newdup
= (xfs_dir2_data_unused_t
*)((char *)d
+ offset
);
666 INT_SET(newdup
->freetag
, ARCH_CONVERT
, XFS_DIR2_DATA_FREE_TAG
);
667 INT_SET(newdup
->length
, ARCH_CONVERT
, len
+ INT_GET(postdup
->length
, ARCH_CONVERT
));
668 INT_SET(*XFS_DIR2_DATA_UNUSED_TAG_P(newdup
), ARCH_CONVERT
,
669 (xfs_dir2_data_off_t
)((char *)newdup
- (char *)d
));
670 xfs_dir2_data_log_unused(tp
, bp
, newdup
);
672 * If the following entry was in the table, the new entry
673 * is longer, so it will be in the table too. Remove
674 * the old one and add the new one.
677 xfs_dir2_data_freeremove(d
, dfp
, needlogp
);
678 (void)xfs_dir2_data_freeinsert(d
, newdup
, needlogp
);
681 * Otherwise we need a scan if the new entry is big enough.
684 needscan
= INT_GET(newdup
->length
, ARCH_CONVERT
) > INT_GET(d
->hdr
.bestfree
[2].length
, ARCH_CONVERT
);
687 * Neither neighbor is free. Make a new entry.
690 newdup
= (xfs_dir2_data_unused_t
*)((char *)d
+ offset
);
691 INT_SET(newdup
->freetag
, ARCH_CONVERT
, XFS_DIR2_DATA_FREE_TAG
);
692 INT_SET(newdup
->length
, ARCH_CONVERT
, len
);
693 INT_SET(*XFS_DIR2_DATA_UNUSED_TAG_P(newdup
), ARCH_CONVERT
,
694 (xfs_dir2_data_off_t
)((char *)newdup
- (char *)d
));
695 xfs_dir2_data_log_unused(tp
, bp
, newdup
);
696 (void)xfs_dir2_data_freeinsert(d
, newdup
, needlogp
);
698 *needscanp
= needscan
;
702 * Take a byte range out of an existing unused space and make it un-free.
705 xfs_dir2_data_use_free(
706 xfs_trans_t
*tp
, /* transaction pointer */
707 xfs_dabuf_t
*bp
, /* data block buffer */
708 xfs_dir2_data_unused_t
*dup
, /* unused entry */
709 xfs_dir2_data_aoff_t offset
, /* starting offset to use */
710 xfs_dir2_data_aoff_t len
, /* length to use */
711 int *needlogp
, /* out: need to log header */
712 int *needscanp
) /* out: need regen bestfree */
714 xfs_dir2_data_t
*d
; /* data block */
715 xfs_dir2_data_free_t
*dfp
; /* bestfree pointer */
716 int matchback
; /* matches end of freespace */
717 int matchfront
; /* matches start of freespace */
718 int needscan
; /* need to regen bestfree */
719 xfs_dir2_data_unused_t
*newdup
; /* new unused entry */
720 xfs_dir2_data_unused_t
*newdup2
; /* another new unused entry */
721 int oldlen
; /* old unused entry's length */
724 ASSERT(INT_GET(d
->hdr
.magic
, ARCH_CONVERT
) == XFS_DIR2_DATA_MAGIC
||
725 INT_GET(d
->hdr
.magic
, ARCH_CONVERT
) == XFS_DIR2_BLOCK_MAGIC
);
726 ASSERT(INT_GET(dup
->freetag
, ARCH_CONVERT
) == XFS_DIR2_DATA_FREE_TAG
);
727 ASSERT(offset
>= (char *)dup
- (char *)d
);
728 ASSERT(offset
+ len
<= (char *)dup
+ INT_GET(dup
->length
, ARCH_CONVERT
) - (char *)d
);
729 ASSERT((char *)dup
- (char *)d
== INT_GET(*XFS_DIR2_DATA_UNUSED_TAG_P(dup
), ARCH_CONVERT
));
731 * Look up the entry in the bestfree table.
733 dfp
= xfs_dir2_data_freefind(d
, dup
);
734 oldlen
= INT_GET(dup
->length
, ARCH_CONVERT
);
735 ASSERT(dfp
|| oldlen
<= INT_GET(d
->hdr
.bestfree
[2].length
, ARCH_CONVERT
));
737 * Check for alignment with front and back of the entry.
739 matchfront
= (char *)dup
- (char *)d
== offset
;
740 matchback
= (char *)dup
+ oldlen
- (char *)d
== offset
+ len
;
741 ASSERT(*needscanp
== 0);
744 * If we matched it exactly we just need to get rid of it from
745 * the bestfree table.
747 if (matchfront
&& matchback
) {
749 needscan
= d
->hdr
.bestfree
[2].offset
;
751 xfs_dir2_data_freeremove(d
, dfp
, needlogp
);
755 * We match the first part of the entry.
756 * Make a new entry with the remaining freespace.
758 else if (matchfront
) {
759 newdup
= (xfs_dir2_data_unused_t
*)((char *)d
+ offset
+ len
);
760 INT_SET(newdup
->freetag
, ARCH_CONVERT
, XFS_DIR2_DATA_FREE_TAG
);
761 INT_SET(newdup
->length
, ARCH_CONVERT
, oldlen
- len
);
762 INT_SET(*XFS_DIR2_DATA_UNUSED_TAG_P(newdup
), ARCH_CONVERT
,
763 (xfs_dir2_data_off_t
)((char *)newdup
- (char *)d
));
764 xfs_dir2_data_log_unused(tp
, bp
, newdup
);
766 * If it was in the table, remove it and add the new one.
769 xfs_dir2_data_freeremove(d
, dfp
, needlogp
);
770 dfp
= xfs_dir2_data_freeinsert(d
, newdup
, needlogp
);
772 ASSERT(INT_GET(dfp
->length
, ARCH_CONVERT
) == INT_GET(newdup
->length
, ARCH_CONVERT
));
773 ASSERT(INT_GET(dfp
->offset
, ARCH_CONVERT
) == (char *)newdup
- (char *)d
);
775 * If we got inserted at the last slot,
776 * that means we don't know if there was a better
777 * choice for the last slot, or not. Rescan.
779 needscan
= dfp
== &d
->hdr
.bestfree
[2];
783 * We match the last part of the entry.
784 * Trim the allocated space off the tail of the entry.
786 else if (matchback
) {
788 INT_SET(newdup
->length
, ARCH_CONVERT
, (xfs_dir2_data_off_t
)
789 (((char *)d
+ offset
) - (char *)newdup
));
790 INT_SET(*XFS_DIR2_DATA_UNUSED_TAG_P(newdup
), ARCH_CONVERT
,
791 (xfs_dir2_data_off_t
)((char *)newdup
- (char *)d
));
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(d
, dfp
, needlogp
);
798 dfp
= xfs_dir2_data_freeinsert(d
, newdup
, needlogp
);
800 ASSERT(INT_GET(dfp
->length
, ARCH_CONVERT
) == INT_GET(newdup
->length
, ARCH_CONVERT
));
801 ASSERT(INT_GET(dfp
->offset
, ARCH_CONVERT
) == (char *)newdup
- (char *)d
);
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
== &d
->hdr
.bestfree
[2];
811 * Poking out the middle of an entry.
812 * Make two new entries.
816 INT_SET(newdup
->length
, ARCH_CONVERT
, (xfs_dir2_data_off_t
)
817 (((char *)d
+ offset
) - (char *)newdup
));
818 INT_SET(*XFS_DIR2_DATA_UNUSED_TAG_P(newdup
), ARCH_CONVERT
,
819 (xfs_dir2_data_off_t
)((char *)newdup
- (char *)d
));
820 xfs_dir2_data_log_unused(tp
, bp
, newdup
);
821 newdup2
= (xfs_dir2_data_unused_t
*)((char *)d
+ offset
+ len
);
822 INT_SET(newdup2
->freetag
, ARCH_CONVERT
, XFS_DIR2_DATA_FREE_TAG
);
823 INT_SET(newdup2
->length
, ARCH_CONVERT
, oldlen
- len
- INT_GET(newdup
->length
, ARCH_CONVERT
));
824 INT_SET(*XFS_DIR2_DATA_UNUSED_TAG_P(newdup2
), ARCH_CONVERT
,
825 (xfs_dir2_data_off_t
)((char *)newdup2
- (char *)d
));
826 xfs_dir2_data_log_unused(tp
, bp
, newdup2
);
828 * If the old entry was in the table, we need to scan
829 * if the 3rd entry was valid, since these entries
830 * are smaller than the old one.
831 * If we don't need to scan that means there were 1 or 2
832 * entries in the table, and removing the old and adding
833 * the 2 new will work.
836 needscan
= d
->hdr
.bestfree
[2].length
;
838 xfs_dir2_data_freeremove(d
, dfp
, needlogp
);
839 (void)xfs_dir2_data_freeinsert(d
, newdup
,
841 (void)xfs_dir2_data_freeinsert(d
, newdup2
,
846 *needscanp
= needscan
;