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
21 * Core data block handling routines for XFS V2 directories.
22 * See xfs_dir2_data.h for data structures.
30 * Check the consistency of the data block.
31 * The input can also be a block-format directory.
32 * Pop an assert if we find anything bad.
36 xfs_inode_t
*dp
, /* incore inode pointer */
37 xfs_dabuf_t
*bp
) /* data block's buffer */
39 xfs_dir2_dataptr_t addr
; /* addr for leaf lookup */
40 xfs_dir2_data_free_t
*bf
; /* bestfree table */
41 xfs_dir2_block_tail_t
*btp
=NULL
; /* block tail */
42 int count
; /* count of entries found */
43 xfs_dir2_data_t
*d
; /* data block pointer */
44 xfs_dir2_data_entry_t
*dep
; /* data entry */
45 xfs_dir2_data_free_t
*dfp
; /* bestfree entry */
46 xfs_dir2_data_unused_t
*dup
; /* unused entry */
47 char *endp
; /* end of useful data */
48 int freeseen
; /* mask of bestfrees seen */
49 xfs_dahash_t hash
; /* hash of current name */
50 int i
; /* leaf index */
51 int lastfree
; /* last entry was unused */
52 xfs_dir2_leaf_entry_t
*lep
=NULL
; /* block leaf entries */
53 xfs_mount_t
*mp
; /* filesystem mount point */
54 char *p
; /* current data position */
55 int stale
; /* count of stale leaves */
59 ASSERT(INT_GET(d
->hdr
.magic
, ARCH_CONVERT
) == XFS_DIR2_DATA_MAGIC
||
60 INT_GET(d
->hdr
.magic
, ARCH_CONVERT
) == XFS_DIR2_BLOCK_MAGIC
);
63 if (INT_GET(d
->hdr
.magic
, ARCH_CONVERT
) == XFS_DIR2_BLOCK_MAGIC
) {
64 btp
= XFS_DIR2_BLOCK_TAIL_P(mp
, (xfs_dir2_block_t
*)d
);
65 lep
= XFS_DIR2_BLOCK_LEAF_P(btp
);
68 endp
= (char *)d
+ mp
->m_dirblksize
;
69 count
= lastfree
= freeseen
= 0;
71 * Account for zero bestfree entries.
74 ASSERT(!bf
[0].offset
);
78 ASSERT(!bf
[1].offset
);
82 ASSERT(!bf
[2].offset
);
85 ASSERT(INT_GET(bf
[0].length
, ARCH_CONVERT
) >= INT_GET(bf
[1].length
, ARCH_CONVERT
));
86 ASSERT(INT_GET(bf
[1].length
, ARCH_CONVERT
) >= INT_GET(bf
[2].length
, ARCH_CONVERT
));
88 * Loop over the data/unused entries.
91 dup
= (xfs_dir2_data_unused_t
*)p
;
93 * If it's unused, look for the space in the bestfree table.
94 * If we find it, account for that, else make sure it
95 * doesn't need to be there.
97 if (INT_GET(dup
->freetag
, ARCH_CONVERT
) == XFS_DIR2_DATA_FREE_TAG
) {
98 ASSERT(lastfree
== 0);
99 ASSERT(INT_GET(*XFS_DIR2_DATA_UNUSED_TAG_P(dup
), ARCH_CONVERT
) ==
100 (char *)dup
- (char *)d
);
101 dfp
= xfs_dir2_data_freefind(d
, dup
);
104 ASSERT((freeseen
& (1 << i
)) == 0);
107 ASSERT(INT_GET(dup
->length
, ARCH_CONVERT
) <= INT_GET(bf
[2].length
, ARCH_CONVERT
));
108 p
+= INT_GET(dup
->length
, ARCH_CONVERT
);
113 * It's a real entry. Validate the fields.
114 * If this is a block directory then make sure it's
115 * in the leaf section of the block.
116 * The linear search is crude but this is DEBUG code.
118 dep
= (xfs_dir2_data_entry_t
*)p
;
119 ASSERT(dep
->namelen
!= 0);
120 ASSERT(xfs_dir_ino_validate(mp
, INT_GET(dep
->inumber
, ARCH_CONVERT
)) == 0);
121 ASSERT(INT_GET(*XFS_DIR2_DATA_ENTRY_TAG_P(dep
), ARCH_CONVERT
) ==
122 (char *)dep
- (char *)d
);
125 if (INT_GET(d
->hdr
.magic
, ARCH_CONVERT
) == XFS_DIR2_BLOCK_MAGIC
) {
126 addr
= XFS_DIR2_DB_OFF_TO_DATAPTR(mp
, mp
->m_dirdatablk
,
127 (xfs_dir2_data_aoff_t
)
128 ((char *)dep
- (char *)d
));
129 hash
= xfs_da_hashname((char *)dep
->name
, dep
->namelen
);
130 for (i
= 0; i
< INT_GET(btp
->count
, ARCH_CONVERT
); i
++) {
131 if (INT_GET(lep
[i
].address
, ARCH_CONVERT
) == addr
&&
132 INT_GET(lep
[i
].hashval
, ARCH_CONVERT
) == hash
)
135 ASSERT(i
< INT_GET(btp
->count
, ARCH_CONVERT
));
137 p
+= XFS_DIR2_DATA_ENTSIZE(dep
->namelen
);
140 * Need to have seen all the entries and all the bestfree slots.
142 ASSERT(freeseen
== 7);
143 if (INT_GET(d
->hdr
.magic
, ARCH_CONVERT
) == XFS_DIR2_BLOCK_MAGIC
) {
144 for (i
= stale
= 0; i
< INT_GET(btp
->count
, ARCH_CONVERT
); i
++) {
145 if (INT_GET(lep
[i
].address
, ARCH_CONVERT
) == XFS_DIR2_NULL_DATAPTR
)
148 ASSERT(INT_GET(lep
[i
].hashval
, ARCH_CONVERT
) >= INT_GET(lep
[i
- 1].hashval
, ARCH_CONVERT
));
150 ASSERT(count
== INT_GET(btp
->count
, ARCH_CONVERT
) - INT_GET(btp
->stale
, ARCH_CONVERT
));
151 ASSERT(stale
== INT_GET(btp
->stale
, ARCH_CONVERT
));
157 * Given a data block and an unused entry from that block,
158 * return the bestfree entry if any that corresponds to it.
160 xfs_dir2_data_free_t
*
161 xfs_dir2_data_freefind(
162 xfs_dir2_data_t
*d
, /* data block */
163 xfs_dir2_data_unused_t
*dup
) /* data unused entry */
165 xfs_dir2_data_free_t
*dfp
; /* bestfree entry */
166 xfs_dir2_data_aoff_t off
; /* offset value needed */
167 #if defined(DEBUG) && defined(__KERNEL__)
168 int matched
; /* matched the value */
169 int seenzero
; /* saw a 0 bestfree entry */
172 off
= (xfs_dir2_data_aoff_t
)((char *)dup
- (char *)d
);
173 #if defined(DEBUG) && defined(__KERNEL__)
175 * Validate some consistency in the bestfree table.
176 * Check order, non-overlapping entries, and if we find the
177 * one we're looking for it has to be exact.
179 ASSERT(INT_GET(d
->hdr
.magic
, ARCH_CONVERT
) == XFS_DIR2_DATA_MAGIC
||
180 INT_GET(d
->hdr
.magic
, ARCH_CONVERT
) == XFS_DIR2_BLOCK_MAGIC
);
181 for (dfp
= &d
->hdr
.bestfree
[0], seenzero
= matched
= 0;
182 dfp
< &d
->hdr
.bestfree
[XFS_DIR2_DATA_FD_COUNT
];
185 ASSERT(!dfp
->length
);
189 ASSERT(seenzero
== 0);
190 if (INT_GET(dfp
->offset
, ARCH_CONVERT
) == off
) {
192 ASSERT(INT_GET(dfp
->length
, ARCH_CONVERT
) == INT_GET(dup
->length
, ARCH_CONVERT
));
193 } else if (off
< INT_GET(dfp
->offset
, ARCH_CONVERT
))
194 ASSERT(off
+ INT_GET(dup
->length
, ARCH_CONVERT
) <= INT_GET(dfp
->offset
, ARCH_CONVERT
));
196 ASSERT(INT_GET(dfp
->offset
, ARCH_CONVERT
) + INT_GET(dfp
->length
, ARCH_CONVERT
) <= off
);
197 ASSERT(matched
|| INT_GET(dfp
->length
, ARCH_CONVERT
) >= INT_GET(dup
->length
, ARCH_CONVERT
));
198 if (dfp
> &d
->hdr
.bestfree
[0])
199 ASSERT(INT_GET(dfp
[-1].length
, ARCH_CONVERT
) >= INT_GET(dfp
[0].length
, ARCH_CONVERT
));
203 * If this is smaller than the smallest bestfree entry,
204 * it can't be there since they're sorted.
206 if (INT_GET(dup
->length
, ARCH_CONVERT
) < INT_GET(d
->hdr
.bestfree
[XFS_DIR2_DATA_FD_COUNT
- 1].length
, ARCH_CONVERT
))
209 * Look at the three bestfree entries for our guy.
211 for (dfp
= &d
->hdr
.bestfree
[0];
212 dfp
< &d
->hdr
.bestfree
[XFS_DIR2_DATA_FD_COUNT
];
216 if (INT_GET(dfp
->offset
, ARCH_CONVERT
) == off
)
220 * Didn't find it. This only happens if there are duplicate lengths.
226 * Insert an unused-space entry into the bestfree table.
228 xfs_dir2_data_free_t
* /* entry inserted */
229 xfs_dir2_data_freeinsert(
230 xfs_dir2_data_t
*d
, /* data block pointer */
231 xfs_dir2_data_unused_t
*dup
, /* unused space */
232 int *loghead
) /* log the data header (out) */
234 xfs_dir2_data_free_t
*dfp
; /* bestfree table pointer */
235 xfs_dir2_data_free_t
new; /* new bestfree entry */
238 ASSERT(INT_GET(d
->hdr
.magic
, ARCH_CONVERT
) == XFS_DIR2_DATA_MAGIC
||
239 INT_GET(d
->hdr
.magic
, ARCH_CONVERT
) == XFS_DIR2_BLOCK_MAGIC
);
241 dfp
= d
->hdr
.bestfree
;
242 INT_COPY(new.length
, dup
->length
, ARCH_CONVERT
);
243 INT_SET(new.offset
, ARCH_CONVERT
, (xfs_dir2_data_off_t
)((char *)dup
- (char *)d
));
245 * Insert at position 0, 1, or 2; or not at all.
247 if (INT_GET(new.length
, ARCH_CONVERT
) > INT_GET(dfp
[0].length
, ARCH_CONVERT
)) {
254 if (INT_GET(new.length
, ARCH_CONVERT
) > INT_GET(dfp
[1].length
, ARCH_CONVERT
)) {
260 if (INT_GET(new.length
, ARCH_CONVERT
) > INT_GET(dfp
[2].length
, ARCH_CONVERT
)) {
269 * Remove a bestfree entry from the table.
272 xfs_dir2_data_freeremove(
273 xfs_dir2_data_t
*d
, /* data block pointer */
274 xfs_dir2_data_free_t
*dfp
, /* bestfree entry pointer */
275 int *loghead
) /* out: log data header */
278 ASSERT(INT_GET(d
->hdr
.magic
, ARCH_CONVERT
) == XFS_DIR2_DATA_MAGIC
||
279 INT_GET(d
->hdr
.magic
, ARCH_CONVERT
) == XFS_DIR2_BLOCK_MAGIC
);
282 * It's the first entry, slide the next 2 up.
284 if (dfp
== &d
->hdr
.bestfree
[0]) {
285 d
->hdr
.bestfree
[0] = d
->hdr
.bestfree
[1];
286 d
->hdr
.bestfree
[1] = d
->hdr
.bestfree
[2];
289 * It's the second entry, slide the 3rd entry up.
291 else if (dfp
== &d
->hdr
.bestfree
[1])
292 d
->hdr
.bestfree
[1] = d
->hdr
.bestfree
[2];
294 * Must be the last entry.
297 ASSERT(dfp
== &d
->hdr
.bestfree
[2]);
299 * Clear the 3rd entry, must be zero now.
301 d
->hdr
.bestfree
[2].length
= 0;
302 d
->hdr
.bestfree
[2].offset
= 0;
307 * Given a data block, reconstruct its bestfree map.
310 xfs_dir2_data_freescan(
311 xfs_mount_t
*mp
, /* filesystem mount point */
312 xfs_dir2_data_t
*d
, /* data block pointer */
313 int *loghead
, /* out: log data header */
314 char *aendp
) /* in: caller's endp */
316 xfs_dir2_block_tail_t
*btp
; /* block tail */
317 xfs_dir2_data_entry_t
*dep
; /* active data entry */
318 xfs_dir2_data_unused_t
*dup
; /* unused data entry */
319 char *endp
; /* end of block's data */
320 char *p
; /* current entry pointer */
323 ASSERT(INT_GET(d
->hdr
.magic
, ARCH_CONVERT
) == XFS_DIR2_DATA_MAGIC
||
324 INT_GET(d
->hdr
.magic
, ARCH_CONVERT
) == XFS_DIR2_BLOCK_MAGIC
);
327 * Start by clearing the table.
329 memset(d
->hdr
.bestfree
, 0, sizeof(d
->hdr
.bestfree
));
337 else if (INT_GET(d
->hdr
.magic
, ARCH_CONVERT
) == XFS_DIR2_BLOCK_MAGIC
) {
338 btp
= XFS_DIR2_BLOCK_TAIL_P(mp
, (xfs_dir2_block_t
*)d
);
339 endp
= (char *)XFS_DIR2_BLOCK_LEAF_P(btp
);
341 endp
= (char *)d
+ mp
->m_dirblksize
;
343 * Loop over the block's entries.
346 dup
= (xfs_dir2_data_unused_t
*)p
;
348 * If it's a free entry, insert it.
350 if (INT_GET(dup
->freetag
, ARCH_CONVERT
) == XFS_DIR2_DATA_FREE_TAG
) {
351 ASSERT((char *)dup
- (char *)d
==
352 INT_GET(*XFS_DIR2_DATA_UNUSED_TAG_P(dup
), ARCH_CONVERT
));
353 xfs_dir2_data_freeinsert(d
, dup
, loghead
);
354 p
+= INT_GET(dup
->length
, ARCH_CONVERT
);
357 * For active entries, check their tags and skip them.
360 dep
= (xfs_dir2_data_entry_t
*)p
;
361 ASSERT((char *)dep
- (char *)d
==
362 INT_GET(*XFS_DIR2_DATA_ENTRY_TAG_P(dep
), ARCH_CONVERT
));
363 p
+= XFS_DIR2_DATA_ENTSIZE(dep
->namelen
);
369 * Initialize a data block at the given block number in the directory.
370 * Give back the buffer for the created block.
374 xfs_da_args_t
*args
, /* directory operation args */
375 xfs_dir2_db_t blkno
, /* logical dir block number */
376 xfs_dabuf_t
**bpp
) /* output block buffer */
378 xfs_dabuf_t
*bp
; /* block buffer */
379 xfs_dir2_data_t
*d
; /* pointer to block */
380 xfs_inode_t
*dp
; /* incore directory inode */
381 xfs_dir2_data_unused_t
*dup
; /* unused entry pointer */
382 int error
; /* error return value */
383 int i
; /* bestfree index */
384 xfs_mount_t
*mp
; /* filesystem mount point */
385 xfs_trans_t
*tp
; /* transaction pointer */
392 * Get the buffer set up for the block.
394 error
= xfs_da_get_buf(tp
, dp
, XFS_DIR2_DB_TO_DA(mp
, blkno
), -1, &bp
,
401 * Initialize the header.
404 INT_SET(d
->hdr
.magic
, ARCH_CONVERT
, XFS_DIR2_DATA_MAGIC
);
405 INT_SET(d
->hdr
.bestfree
[0].offset
, ARCH_CONVERT
, (xfs_dir2_data_off_t
)sizeof(d
->hdr
));
406 for (i
= 1; i
< XFS_DIR2_DATA_FD_COUNT
; i
++) {
407 d
->hdr
.bestfree
[i
].length
= 0;
408 d
->hdr
.bestfree
[i
].offset
= 0;
411 * Set up an unused entry for the block's body.
413 dup
= &d
->u
[0].unused
;
414 INT_SET(dup
->freetag
, ARCH_CONVERT
, XFS_DIR2_DATA_FREE_TAG
);
416 t
=mp
->m_dirblksize
- (uint
)sizeof(d
->hdr
);
417 INT_SET(d
->hdr
.bestfree
[0].length
, ARCH_CONVERT
, t
);
418 INT_SET(dup
->length
, ARCH_CONVERT
, t
);
419 INT_SET(*XFS_DIR2_DATA_UNUSED_TAG_P(dup
), ARCH_CONVERT
,
420 (xfs_dir2_data_off_t
)((char *)dup
- (char *)d
));
422 * Log it and return it.
424 xfs_dir2_data_log_header(tp
, bp
);
425 xfs_dir2_data_log_unused(tp
, bp
, dup
);
431 * Log an active data entry from the block.
434 xfs_dir2_data_log_entry(
435 xfs_trans_t
*tp
, /* transaction pointer */
436 xfs_dabuf_t
*bp
, /* block buffer */
437 xfs_dir2_data_entry_t
*dep
) /* data entry pointer */
439 xfs_dir2_data_t
*d
; /* data block pointer */
442 ASSERT(INT_GET(d
->hdr
.magic
, ARCH_CONVERT
) == XFS_DIR2_DATA_MAGIC
||
443 INT_GET(d
->hdr
.magic
, ARCH_CONVERT
) == XFS_DIR2_BLOCK_MAGIC
);
444 xfs_da_log_buf(tp
, bp
, (uint
)((char *)dep
- (char *)d
),
445 (uint
)((char *)(XFS_DIR2_DATA_ENTRY_TAG_P(dep
) + 1) -
450 * Log a data block header.
453 xfs_dir2_data_log_header(
454 xfs_trans_t
*tp
, /* transaction pointer */
455 xfs_dabuf_t
*bp
) /* block buffer */
457 xfs_dir2_data_t
*d
; /* data block pointer */
460 ASSERT(INT_GET(d
->hdr
.magic
, ARCH_CONVERT
) == XFS_DIR2_DATA_MAGIC
||
461 INT_GET(d
->hdr
.magic
, ARCH_CONVERT
) == XFS_DIR2_BLOCK_MAGIC
);
462 xfs_da_log_buf(tp
, bp
, (uint
)((char *)&d
->hdr
- (char *)d
),
463 (uint
)(sizeof(d
->hdr
) - 1));
467 * Log a data unused entry.
470 xfs_dir2_data_log_unused(
471 xfs_trans_t
*tp
, /* transaction pointer */
472 xfs_dabuf_t
*bp
, /* block buffer */
473 xfs_dir2_data_unused_t
*dup
) /* data unused pointer */
475 xfs_dir2_data_t
*d
; /* data block pointer */
478 ASSERT(INT_GET(d
->hdr
.magic
, ARCH_CONVERT
) == XFS_DIR2_DATA_MAGIC
||
479 INT_GET(d
->hdr
.magic
, ARCH_CONVERT
) == XFS_DIR2_BLOCK_MAGIC
);
481 * Log the first part of the unused entry.
483 xfs_da_log_buf(tp
, bp
, (uint
)((char *)dup
- (char *)d
),
484 (uint
)((char *)&dup
->length
+ sizeof(dup
->length
) -
487 * Log the end (tag) of the unused entry.
489 xfs_da_log_buf(tp
, bp
,
490 (uint
)((char *)XFS_DIR2_DATA_UNUSED_TAG_P(dup
) - (char *)d
),
491 (uint
)((char *)XFS_DIR2_DATA_UNUSED_TAG_P(dup
) - (char *)d
+
492 sizeof(xfs_dir2_data_off_t
) - 1));
496 * Make a byte range in the data block unused.
497 * Its current contents are unimportant.
500 xfs_dir2_data_make_free(
501 xfs_trans_t
*tp
, /* transaction pointer */
502 xfs_dabuf_t
*bp
, /* block buffer */
503 xfs_dir2_data_aoff_t offset
, /* starting byte offset */
504 xfs_dir2_data_aoff_t len
, /* length in bytes */
505 int *needlogp
, /* out: log header */
506 int *needscanp
) /* out: regen bestfree */
508 xfs_dir2_data_t
*d
; /* data block pointer */
509 xfs_dir2_data_free_t
*dfp
; /* bestfree pointer */
510 char *endptr
; /* end of data area */
511 xfs_mount_t
*mp
; /* filesystem mount point */
512 int needscan
; /* need to regen bestfree */
513 xfs_dir2_data_unused_t
*newdup
; /* new unused entry */
514 xfs_dir2_data_unused_t
*postdup
; /* unused entry after us */
515 xfs_dir2_data_unused_t
*prevdup
; /* unused entry before us */
520 * Figure out where the end of the data area is.
522 if (INT_GET(d
->hdr
.magic
, ARCH_CONVERT
) == XFS_DIR2_DATA_MAGIC
)
523 endptr
= (char *)d
+ mp
->m_dirblksize
;
525 xfs_dir2_block_tail_t
*btp
; /* block tail */
527 ASSERT(INT_GET(d
->hdr
.magic
, ARCH_CONVERT
) == XFS_DIR2_BLOCK_MAGIC
);
528 btp
= XFS_DIR2_BLOCK_TAIL_P(mp
, (xfs_dir2_block_t
*)d
);
529 endptr
= (char *)XFS_DIR2_BLOCK_LEAF_P(btp
);
532 * If this isn't the start of the block, then back up to
533 * the previous entry and see if it's free.
535 if (offset
> sizeof(d
->hdr
)) {
536 xfs_dir2_data_off_t
*tagp
; /* tag just before us */
538 tagp
= (xfs_dir2_data_off_t
*)((char *)d
+ offset
) - 1;
539 prevdup
= (xfs_dir2_data_unused_t
*)((char *)d
+ INT_GET(*tagp
, ARCH_CONVERT
));
540 if (INT_GET(prevdup
->freetag
, ARCH_CONVERT
) != XFS_DIR2_DATA_FREE_TAG
)
545 * If this isn't the end of the block, see if the entry after
548 if ((char *)d
+ offset
+ len
< endptr
) {
550 (xfs_dir2_data_unused_t
*)((char *)d
+ offset
+ len
);
551 if (INT_GET(postdup
->freetag
, ARCH_CONVERT
) != XFS_DIR2_DATA_FREE_TAG
)
555 ASSERT(*needscanp
== 0);
558 * Previous and following entries are both free,
559 * merge everything into a single free entry.
561 if (prevdup
&& postdup
) {
562 xfs_dir2_data_free_t
*dfp2
; /* another bestfree pointer */
565 * See if prevdup and/or postdup are in bestfree table.
567 dfp
= xfs_dir2_data_freefind(d
, prevdup
);
568 dfp2
= xfs_dir2_data_freefind(d
, postdup
);
570 * We need a rescan unless there are exactly 2 free entries
571 * namely our two. Then we know what's happening, otherwise
572 * since the third bestfree is there, there might be more
575 needscan
= d
->hdr
.bestfree
[2].length
;
577 * Fix up the new big freespace.
579 INT_MOD(prevdup
->length
, ARCH_CONVERT
, len
+ INT_GET(postdup
->length
, ARCH_CONVERT
));
580 INT_SET(*XFS_DIR2_DATA_UNUSED_TAG_P(prevdup
), ARCH_CONVERT
,
581 (xfs_dir2_data_off_t
)((char *)prevdup
- (char *)d
));
582 xfs_dir2_data_log_unused(tp
, bp
, prevdup
);
585 * Has to be the case that entries 0 and 1 are
586 * dfp and dfp2 (don't know which is which), and
588 * Remove entry 1 first then entry 0.
591 if (dfp
== &d
->hdr
.bestfree
[1]) {
592 dfp
= &d
->hdr
.bestfree
[0];
594 dfp2
= &d
->hdr
.bestfree
[1];
596 xfs_dir2_data_freeremove(d
, dfp2
, needlogp
);
597 xfs_dir2_data_freeremove(d
, dfp
, needlogp
);
599 * Now insert the new entry.
601 dfp
= xfs_dir2_data_freeinsert(d
, prevdup
, needlogp
);
602 ASSERT(dfp
== &d
->hdr
.bestfree
[0]);
603 ASSERT(INT_GET(dfp
->length
, ARCH_CONVERT
) == INT_GET(prevdup
->length
, ARCH_CONVERT
));
604 ASSERT(!dfp
[1].length
);
605 ASSERT(!dfp
[2].length
);
609 * The entry before us is free, merge with it.
612 dfp
= xfs_dir2_data_freefind(d
, prevdup
);
613 INT_MOD(prevdup
->length
, ARCH_CONVERT
, len
);
614 INT_SET(*XFS_DIR2_DATA_UNUSED_TAG_P(prevdup
), ARCH_CONVERT
,
615 (xfs_dir2_data_off_t
)((char *)prevdup
- (char *)d
));
616 xfs_dir2_data_log_unused(tp
, bp
, prevdup
);
618 * If the previous entry was in the table, the new entry
619 * is longer, so it will be in the table too. Remove
620 * the old one and add the new one.
623 xfs_dir2_data_freeremove(d
, dfp
, needlogp
);
624 (void)xfs_dir2_data_freeinsert(d
, prevdup
, needlogp
);
627 * Otherwise we need a scan if the new entry is big enough.
630 needscan
= INT_GET(prevdup
->length
, ARCH_CONVERT
) > INT_GET(d
->hdr
.bestfree
[2].length
, ARCH_CONVERT
);
633 * The following entry is free, merge with it.
636 dfp
= xfs_dir2_data_freefind(d
, postdup
);
637 newdup
= (xfs_dir2_data_unused_t
*)((char *)d
+ offset
);
638 INT_SET(newdup
->freetag
, ARCH_CONVERT
, XFS_DIR2_DATA_FREE_TAG
);
639 INT_SET(newdup
->length
, ARCH_CONVERT
, len
+ INT_GET(postdup
->length
, ARCH_CONVERT
));
640 INT_SET(*XFS_DIR2_DATA_UNUSED_TAG_P(newdup
), ARCH_CONVERT
,
641 (xfs_dir2_data_off_t
)((char *)newdup
- (char *)d
));
642 xfs_dir2_data_log_unused(tp
, bp
, newdup
);
644 * If the following entry was in the table, the new entry
645 * is longer, so it will be in the table too. Remove
646 * the old one and add the new one.
649 xfs_dir2_data_freeremove(d
, dfp
, needlogp
);
650 (void)xfs_dir2_data_freeinsert(d
, newdup
, needlogp
);
653 * Otherwise we need a scan if the new entry is big enough.
656 needscan
= INT_GET(newdup
->length
, ARCH_CONVERT
) > INT_GET(d
->hdr
.bestfree
[2].length
, ARCH_CONVERT
);
659 * Neither neighbor is free. Make a new entry.
662 newdup
= (xfs_dir2_data_unused_t
*)((char *)d
+ offset
);
663 INT_SET(newdup
->freetag
, ARCH_CONVERT
, XFS_DIR2_DATA_FREE_TAG
);
664 INT_SET(newdup
->length
, ARCH_CONVERT
, len
);
665 INT_SET(*XFS_DIR2_DATA_UNUSED_TAG_P(newdup
), ARCH_CONVERT
,
666 (xfs_dir2_data_off_t
)((char *)newdup
- (char *)d
));
667 xfs_dir2_data_log_unused(tp
, bp
, newdup
);
668 (void)xfs_dir2_data_freeinsert(d
, newdup
, needlogp
);
670 *needscanp
= needscan
;
674 * Take a byte range out of an existing unused space and make it un-free.
677 xfs_dir2_data_use_free(
678 xfs_trans_t
*tp
, /* transaction pointer */
679 xfs_dabuf_t
*bp
, /* data block buffer */
680 xfs_dir2_data_unused_t
*dup
, /* unused entry */
681 xfs_dir2_data_aoff_t offset
, /* starting offset to use */
682 xfs_dir2_data_aoff_t len
, /* length to use */
683 int *needlogp
, /* out: need to log header */
684 int *needscanp
) /* out: need regen bestfree */
686 xfs_dir2_data_t
*d
; /* data block */
687 xfs_dir2_data_free_t
*dfp
; /* bestfree pointer */
688 int matchback
; /* matches end of freespace */
689 int matchfront
; /* matches start of freespace */
690 int needscan
; /* need to regen bestfree */
691 xfs_dir2_data_unused_t
*newdup
; /* new unused entry */
692 xfs_dir2_data_unused_t
*newdup2
; /* another new unused entry */
693 int oldlen
; /* old unused entry's length */
696 ASSERT(INT_GET(d
->hdr
.magic
, ARCH_CONVERT
) == XFS_DIR2_DATA_MAGIC
||
697 INT_GET(d
->hdr
.magic
, ARCH_CONVERT
) == XFS_DIR2_BLOCK_MAGIC
);
698 ASSERT(INT_GET(dup
->freetag
, ARCH_CONVERT
) == XFS_DIR2_DATA_FREE_TAG
);
699 ASSERT(offset
>= (char *)dup
- (char *)d
);
700 ASSERT(offset
+ len
<= (char *)dup
+ INT_GET(dup
->length
, ARCH_CONVERT
) - (char *)d
);
701 ASSERT((char *)dup
- (char *)d
== INT_GET(*XFS_DIR2_DATA_UNUSED_TAG_P(dup
), ARCH_CONVERT
));
703 * Look up the entry in the bestfree table.
705 dfp
= xfs_dir2_data_freefind(d
, dup
);
706 oldlen
= INT_GET(dup
->length
, ARCH_CONVERT
);
707 ASSERT(dfp
|| oldlen
<= INT_GET(d
->hdr
.bestfree
[2].length
, ARCH_CONVERT
));
709 * Check for alignment with front and back of the entry.
711 matchfront
= (char *)dup
- (char *)d
== offset
;
712 matchback
= (char *)dup
+ oldlen
- (char *)d
== offset
+ len
;
713 ASSERT(*needscanp
== 0);
716 * If we matched it exactly we just need to get rid of it from
717 * the bestfree table.
719 if (matchfront
&& matchback
) {
721 needscan
= d
->hdr
.bestfree
[2].offset
;
723 xfs_dir2_data_freeremove(d
, dfp
, needlogp
);
727 * We match the first part of the entry.
728 * Make a new entry with the remaining freespace.
730 else if (matchfront
) {
731 newdup
= (xfs_dir2_data_unused_t
*)((char *)d
+ offset
+ len
);
732 INT_SET(newdup
->freetag
, ARCH_CONVERT
, XFS_DIR2_DATA_FREE_TAG
);
733 INT_SET(newdup
->length
, ARCH_CONVERT
, oldlen
- len
);
734 INT_SET(*XFS_DIR2_DATA_UNUSED_TAG_P(newdup
), ARCH_CONVERT
,
735 (xfs_dir2_data_off_t
)((char *)newdup
- (char *)d
));
736 xfs_dir2_data_log_unused(tp
, bp
, newdup
);
738 * If it was in the table, remove it and add the new one.
741 xfs_dir2_data_freeremove(d
, dfp
, needlogp
);
742 dfp
= xfs_dir2_data_freeinsert(d
, newdup
, needlogp
);
744 ASSERT(INT_GET(dfp
->length
, ARCH_CONVERT
) == INT_GET(newdup
->length
, ARCH_CONVERT
));
745 ASSERT(INT_GET(dfp
->offset
, ARCH_CONVERT
) == (char *)newdup
- (char *)d
);
747 * If we got inserted at the last slot,
748 * that means we don't know if there was a better
749 * choice for the last slot, or not. Rescan.
751 needscan
= dfp
== &d
->hdr
.bestfree
[2];
755 * We match the last part of the entry.
756 * Trim the allocated space off the tail of the entry.
758 else if (matchback
) {
760 INT_SET(newdup
->length
, ARCH_CONVERT
, (xfs_dir2_data_off_t
)
761 (((char *)d
+ offset
) - (char *)newdup
));
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 * Poking out the middle of an entry.
784 * Make two new entries.
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
);
793 newdup2
= (xfs_dir2_data_unused_t
*)((char *)d
+ offset
+ len
);
794 INT_SET(newdup2
->freetag
, ARCH_CONVERT
, XFS_DIR2_DATA_FREE_TAG
);
795 INT_SET(newdup2
->length
, ARCH_CONVERT
, oldlen
- len
- INT_GET(newdup
->length
, ARCH_CONVERT
));
796 INT_SET(*XFS_DIR2_DATA_UNUSED_TAG_P(newdup2
), ARCH_CONVERT
,
797 (xfs_dir2_data_off_t
)((char *)newdup2
- (char *)d
));
798 xfs_dir2_data_log_unused(tp
, bp
, newdup2
);
800 * If the old entry was in the table, we need to scan
801 * if the 3rd entry was valid, since these entries
802 * are smaller than the old one.
803 * If we don't need to scan that means there were 1 or 2
804 * entries in the table, and removing the old and adding
805 * the 2 new will work.
808 needscan
= d
->hdr
.bestfree
[2].length
;
810 xfs_dir2_data_freeremove(d
, dfp
, needlogp
);
811 (void)xfs_dir2_data_freeinsert(d
, newdup
,
813 (void)xfs_dir2_data_freeinsert(d
, newdup2
,
818 *needscanp
= needscan
;