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"
22 #include "xfs_trans.h"
25 #include "xfs_mount.h"
26 #include "xfs_da_btree.h"
27 #include "xfs_bmap_btree.h"
28 #include "xfs_dinode.h"
29 #include "xfs_inode.h"
30 #include "xfs_dir2_format.h"
31 #include "xfs_dir2_priv.h"
32 #include "xfs_error.h"
34 STATIC xfs_dir2_data_free_t
*
35 xfs_dir2_data_freefind(xfs_dir2_data_hdr_t
*hdr
, xfs_dir2_data_unused_t
*dup
);
38 * Check the consistency of the data block.
39 * The input can also be a block-format directory.
40 * Return 0 is the buffer is good, otherwise an error.
43 __xfs_dir2_data_check(
44 struct xfs_inode
*dp
, /* incore inode pointer */
45 struct xfs_buf
*bp
) /* data block's buffer */
47 xfs_dir2_dataptr_t addr
; /* addr for leaf lookup */
48 xfs_dir2_data_free_t
*bf
; /* bestfree table */
49 xfs_dir2_block_tail_t
*btp
=NULL
; /* block tail */
50 int count
; /* count of entries found */
51 xfs_dir2_data_hdr_t
*hdr
; /* data block header */
52 xfs_dir2_data_entry_t
*dep
; /* data entry */
53 xfs_dir2_data_free_t
*dfp
; /* bestfree entry */
54 xfs_dir2_data_unused_t
*dup
; /* unused entry */
55 char *endp
; /* end of useful data */
56 int freeseen
; /* mask of bestfrees seen */
57 xfs_dahash_t hash
; /* hash of current name */
58 int i
; /* leaf index */
59 int lastfree
; /* last entry was unused */
60 xfs_dir2_leaf_entry_t
*lep
=NULL
; /* block leaf entries */
61 xfs_mount_t
*mp
; /* filesystem mount point */
62 char *p
; /* current data position */
63 int stale
; /* count of stale leaves */
66 mp
= bp
->b_target
->bt_mount
;
69 p
= (char *)(hdr
+ 1);
72 case cpu_to_be32(XFS_DIR2_BLOCK_MAGIC
):
73 btp
= xfs_dir2_block_tail_p(mp
, hdr
);
74 lep
= xfs_dir2_block_leaf_p(btp
);
77 case cpu_to_be32(XFS_DIR2_DATA_MAGIC
):
78 endp
= (char *)hdr
+ mp
->m_dirblksize
;
81 XFS_ERROR_REPORT("Bad Magic", XFS_ERRLEVEL_LOW
, mp
);
85 count
= lastfree
= freeseen
= 0;
87 * Account for zero bestfree entries.
90 XFS_WANT_CORRUPTED_RETURN(!bf
[0].offset
);
94 XFS_WANT_CORRUPTED_RETURN(!bf
[1].offset
);
98 XFS_WANT_CORRUPTED_RETURN(!bf
[2].offset
);
102 XFS_WANT_CORRUPTED_RETURN(be16_to_cpu(bf
[0].length
) >=
103 be16_to_cpu(bf
[1].length
));
104 XFS_WANT_CORRUPTED_RETURN(be16_to_cpu(bf
[1].length
) >=
105 be16_to_cpu(bf
[2].length
));
107 * Loop over the data/unused entries.
110 dup
= (xfs_dir2_data_unused_t
*)p
;
112 * If it's unused, look for the space in the bestfree table.
113 * If we find it, account for that, else make sure it
114 * doesn't need to be there.
116 if (be16_to_cpu(dup
->freetag
) == XFS_DIR2_DATA_FREE_TAG
) {
117 XFS_WANT_CORRUPTED_RETURN(lastfree
== 0);
118 XFS_WANT_CORRUPTED_RETURN(
119 be16_to_cpu(*xfs_dir2_data_unused_tag_p(dup
)) ==
120 (char *)dup
- (char *)hdr
);
121 dfp
= xfs_dir2_data_freefind(hdr
, dup
);
124 XFS_WANT_CORRUPTED_RETURN(
125 (freeseen
& (1 << i
)) == 0);
128 XFS_WANT_CORRUPTED_RETURN(
129 be16_to_cpu(dup
->length
) <=
130 be16_to_cpu(bf
[2].length
));
132 p
+= be16_to_cpu(dup
->length
);
137 * It's a real entry. Validate the fields.
138 * If this is a block directory then make sure it's
139 * in the leaf section of the block.
140 * The linear search is crude but this is DEBUG code.
142 dep
= (xfs_dir2_data_entry_t
*)p
;
143 XFS_WANT_CORRUPTED_RETURN(dep
->namelen
!= 0);
144 XFS_WANT_CORRUPTED_RETURN(
145 !xfs_dir_ino_validate(mp
, be64_to_cpu(dep
->inumber
)));
146 XFS_WANT_CORRUPTED_RETURN(
147 be16_to_cpu(*xfs_dir2_data_entry_tag_p(dep
)) ==
148 (char *)dep
- (char *)hdr
);
151 if (hdr
->magic
== cpu_to_be32(XFS_DIR2_BLOCK_MAGIC
)) {
152 addr
= xfs_dir2_db_off_to_dataptr(mp
, mp
->m_dirdatablk
,
153 (xfs_dir2_data_aoff_t
)
154 ((char *)dep
- (char *)hdr
));
155 name
.name
= dep
->name
;
156 name
.len
= dep
->namelen
;
157 hash
= mp
->m_dirnameops
->hashname(&name
);
158 for (i
= 0; i
< be32_to_cpu(btp
->count
); i
++) {
159 if (be32_to_cpu(lep
[i
].address
) == addr
&&
160 be32_to_cpu(lep
[i
].hashval
) == hash
)
163 XFS_WANT_CORRUPTED_RETURN(i
< be32_to_cpu(btp
->count
));
165 p
+= xfs_dir2_data_entsize(dep
->namelen
);
168 * Need to have seen all the entries and all the bestfree slots.
170 XFS_WANT_CORRUPTED_RETURN(freeseen
== 7);
171 if (hdr
->magic
== cpu_to_be32(XFS_DIR2_BLOCK_MAGIC
)) {
172 for (i
= stale
= 0; i
< be32_to_cpu(btp
->count
); i
++) {
173 if (lep
[i
].address
==
174 cpu_to_be32(XFS_DIR2_NULL_DATAPTR
))
177 XFS_WANT_CORRUPTED_RETURN(
178 be32_to_cpu(lep
[i
].hashval
) >=
179 be32_to_cpu(lep
[i
- 1].hashval
));
181 XFS_WANT_CORRUPTED_RETURN(count
==
182 be32_to_cpu(btp
->count
) - be32_to_cpu(btp
->stale
));
183 XFS_WANT_CORRUPTED_RETURN(stale
== be32_to_cpu(btp
->stale
));
189 xfs_dir2_data_verify(
192 struct xfs_mount
*mp
= bp
->b_target
->bt_mount
;
193 struct xfs_dir2_data_hdr
*hdr
= bp
->b_addr
;
196 block_ok
= hdr
->magic
== cpu_to_be32(XFS_DIR2_DATA_MAGIC
);
197 block_ok
= block_ok
&& __xfs_dir2_data_check(NULL
, bp
) == 0;
200 XFS_CORRUPTION_ERROR(__func__
, XFS_ERRLEVEL_LOW
, mp
, hdr
);
201 xfs_buf_ioerror(bp
, EFSCORRUPTED
);
206 xfs_dir2_data_write_verify(
209 xfs_dir2_data_verify(bp
);
213 xfs_dir2_data_read_verify(
216 xfs_dir2_data_verify(bp
);
217 bp
->b_pre_io
= xfs_dir2_data_write_verify
;
219 xfs_buf_ioend(bp
, 0);
225 struct xfs_trans
*tp
,
226 struct xfs_inode
*dp
,
228 xfs_daddr_t mapped_bno
,
229 struct xfs_buf
**bpp
)
231 return xfs_da_read_buf(tp
, dp
, bno
, mapped_bno
, bpp
,
232 XFS_DATA_FORK
, xfs_dir2_data_read_verify
);
236 xfs_dir2_data_readahead(
237 struct xfs_trans
*tp
,
238 struct xfs_inode
*dp
,
240 xfs_daddr_t mapped_bno
)
242 return xfs_da_reada_buf(tp
, dp
, bno
, mapped_bno
,
243 XFS_DATA_FORK
, xfs_dir2_data_read_verify
);
247 * Given a data block and an unused entry from that block,
248 * return the bestfree entry if any that corresponds to it.
250 STATIC xfs_dir2_data_free_t
*
251 xfs_dir2_data_freefind(
252 xfs_dir2_data_hdr_t
*hdr
, /* data block */
253 xfs_dir2_data_unused_t
*dup
) /* data unused entry */
255 xfs_dir2_data_free_t
*dfp
; /* bestfree entry */
256 xfs_dir2_data_aoff_t off
; /* offset value needed */
257 #if defined(DEBUG) && defined(__KERNEL__)
258 int matched
; /* matched the value */
259 int seenzero
; /* saw a 0 bestfree entry */
262 off
= (xfs_dir2_data_aoff_t
)((char *)dup
- (char *)hdr
);
263 #if defined(DEBUG) && defined(__KERNEL__)
265 * Validate some consistency in the bestfree table.
266 * Check order, non-overlapping entries, and if we find the
267 * one we're looking for it has to be exact.
269 ASSERT(hdr
->magic
== cpu_to_be32(XFS_DIR2_DATA_MAGIC
) ||
270 hdr
->magic
== cpu_to_be32(XFS_DIR2_BLOCK_MAGIC
));
271 for (dfp
= &hdr
->bestfree
[0], seenzero
= matched
= 0;
272 dfp
< &hdr
->bestfree
[XFS_DIR2_DATA_FD_COUNT
];
275 ASSERT(!dfp
->length
);
279 ASSERT(seenzero
== 0);
280 if (be16_to_cpu(dfp
->offset
) == off
) {
282 ASSERT(dfp
->length
== dup
->length
);
283 } else if (off
< be16_to_cpu(dfp
->offset
))
284 ASSERT(off
+ be16_to_cpu(dup
->length
) <= be16_to_cpu(dfp
->offset
));
286 ASSERT(be16_to_cpu(dfp
->offset
) + be16_to_cpu(dfp
->length
) <= off
);
287 ASSERT(matched
|| be16_to_cpu(dfp
->length
) >= be16_to_cpu(dup
->length
));
288 if (dfp
> &hdr
->bestfree
[0])
289 ASSERT(be16_to_cpu(dfp
[-1].length
) >= be16_to_cpu(dfp
[0].length
));
293 * If this is smaller than the smallest bestfree entry,
294 * it can't be there since they're sorted.
296 if (be16_to_cpu(dup
->length
) <
297 be16_to_cpu(hdr
->bestfree
[XFS_DIR2_DATA_FD_COUNT
- 1].length
))
300 * Look at the three bestfree entries for our guy.
302 for (dfp
= &hdr
->bestfree
[0];
303 dfp
< &hdr
->bestfree
[XFS_DIR2_DATA_FD_COUNT
];
307 if (be16_to_cpu(dfp
->offset
) == off
)
311 * Didn't find it. This only happens if there are duplicate lengths.
317 * Insert an unused-space entry into the bestfree table.
319 xfs_dir2_data_free_t
* /* entry inserted */
320 xfs_dir2_data_freeinsert(
321 xfs_dir2_data_hdr_t
*hdr
, /* data block pointer */
322 xfs_dir2_data_unused_t
*dup
, /* unused space */
323 int *loghead
) /* log the data header (out) */
325 xfs_dir2_data_free_t
*dfp
; /* bestfree table pointer */
326 xfs_dir2_data_free_t
new; /* new bestfree entry */
329 ASSERT(hdr
->magic
== cpu_to_be32(XFS_DIR2_DATA_MAGIC
) ||
330 hdr
->magic
== cpu_to_be32(XFS_DIR2_BLOCK_MAGIC
));
333 new.length
= dup
->length
;
334 new.offset
= cpu_to_be16((char *)dup
- (char *)hdr
);
337 * Insert at position 0, 1, or 2; or not at all.
339 if (be16_to_cpu(new.length
) > be16_to_cpu(dfp
[0].length
)) {
346 if (be16_to_cpu(new.length
) > be16_to_cpu(dfp
[1].length
)) {
352 if (be16_to_cpu(new.length
) > be16_to_cpu(dfp
[2].length
)) {
361 * Remove a bestfree entry from the table.
364 xfs_dir2_data_freeremove(
365 xfs_dir2_data_hdr_t
*hdr
, /* data block header */
366 xfs_dir2_data_free_t
*dfp
, /* bestfree entry pointer */
367 int *loghead
) /* out: log data header */
370 ASSERT(hdr
->magic
== cpu_to_be32(XFS_DIR2_DATA_MAGIC
) ||
371 hdr
->magic
== cpu_to_be32(XFS_DIR2_BLOCK_MAGIC
));
374 * It's the first entry, slide the next 2 up.
376 if (dfp
== &hdr
->bestfree
[0]) {
377 hdr
->bestfree
[0] = hdr
->bestfree
[1];
378 hdr
->bestfree
[1] = hdr
->bestfree
[2];
381 * It's the second entry, slide the 3rd entry up.
383 else if (dfp
== &hdr
->bestfree
[1])
384 hdr
->bestfree
[1] = hdr
->bestfree
[2];
386 * Must be the last entry.
389 ASSERT(dfp
== &hdr
->bestfree
[2]);
391 * Clear the 3rd entry, must be zero now.
393 hdr
->bestfree
[2].length
= 0;
394 hdr
->bestfree
[2].offset
= 0;
399 * Given a data block, reconstruct its bestfree map.
402 xfs_dir2_data_freescan(
403 xfs_mount_t
*mp
, /* filesystem mount point */
404 xfs_dir2_data_hdr_t
*hdr
, /* data block header */
405 int *loghead
) /* out: log data header */
407 xfs_dir2_block_tail_t
*btp
; /* block tail */
408 xfs_dir2_data_entry_t
*dep
; /* active data entry */
409 xfs_dir2_data_unused_t
*dup
; /* unused data entry */
410 char *endp
; /* end of block's data */
411 char *p
; /* current entry pointer */
414 ASSERT(hdr
->magic
== cpu_to_be32(XFS_DIR2_DATA_MAGIC
) ||
415 hdr
->magic
== cpu_to_be32(XFS_DIR2_BLOCK_MAGIC
));
418 * Start by clearing the table.
420 memset(hdr
->bestfree
, 0, sizeof(hdr
->bestfree
));
425 p
= (char *)(hdr
+ 1);
426 if (hdr
->magic
== cpu_to_be32(XFS_DIR2_BLOCK_MAGIC
)) {
427 btp
= xfs_dir2_block_tail_p(mp
, hdr
);
428 endp
= (char *)xfs_dir2_block_leaf_p(btp
);
430 endp
= (char *)hdr
+ mp
->m_dirblksize
;
432 * Loop over the block's entries.
435 dup
= (xfs_dir2_data_unused_t
*)p
;
437 * If it's a free entry, insert it.
439 if (be16_to_cpu(dup
->freetag
) == XFS_DIR2_DATA_FREE_TAG
) {
440 ASSERT((char *)dup
- (char *)hdr
==
441 be16_to_cpu(*xfs_dir2_data_unused_tag_p(dup
)));
442 xfs_dir2_data_freeinsert(hdr
, dup
, loghead
);
443 p
+= be16_to_cpu(dup
->length
);
446 * For active entries, check their tags and skip them.
449 dep
= (xfs_dir2_data_entry_t
*)p
;
450 ASSERT((char *)dep
- (char *)hdr
==
451 be16_to_cpu(*xfs_dir2_data_entry_tag_p(dep
)));
452 p
+= xfs_dir2_data_entsize(dep
->namelen
);
458 * Initialize a data block at the given block number in the directory.
459 * Give back the buffer for the created block.
463 xfs_da_args_t
*args
, /* directory operation args */
464 xfs_dir2_db_t blkno
, /* logical dir block number */
465 struct xfs_buf
**bpp
) /* output block buffer */
467 struct xfs_buf
*bp
; /* block buffer */
468 xfs_dir2_data_hdr_t
*hdr
; /* data block header */
469 xfs_inode_t
*dp
; /* incore directory inode */
470 xfs_dir2_data_unused_t
*dup
; /* unused entry pointer */
471 int error
; /* error return value */
472 int i
; /* bestfree index */
473 xfs_mount_t
*mp
; /* filesystem mount point */
474 xfs_trans_t
*tp
; /* transaction pointer */
481 * Get the buffer set up for the block.
483 error
= xfs_da_get_buf(tp
, dp
, xfs_dir2_db_to_da(mp
, blkno
), -1, &bp
,
487 bp
->b_pre_io
= xfs_dir2_data_write_verify
;
490 * Initialize the header.
493 hdr
->magic
= cpu_to_be32(XFS_DIR2_DATA_MAGIC
);
494 hdr
->bestfree
[0].offset
= cpu_to_be16(sizeof(*hdr
));
495 for (i
= 1; i
< XFS_DIR2_DATA_FD_COUNT
; i
++) {
496 hdr
->bestfree
[i
].length
= 0;
497 hdr
->bestfree
[i
].offset
= 0;
501 * Set up an unused entry for the block's body.
503 dup
= (xfs_dir2_data_unused_t
*)(hdr
+ 1);
504 dup
->freetag
= cpu_to_be16(XFS_DIR2_DATA_FREE_TAG
);
506 t
= mp
->m_dirblksize
- (uint
)sizeof(*hdr
);
507 hdr
->bestfree
[0].length
= cpu_to_be16(t
);
508 dup
->length
= cpu_to_be16(t
);
509 *xfs_dir2_data_unused_tag_p(dup
) = cpu_to_be16((char *)dup
- (char *)hdr
);
511 * Log it and return it.
513 xfs_dir2_data_log_header(tp
, bp
);
514 xfs_dir2_data_log_unused(tp
, bp
, dup
);
520 * Log an active data entry from the block.
523 xfs_dir2_data_log_entry(
524 struct xfs_trans
*tp
,
526 xfs_dir2_data_entry_t
*dep
) /* data entry pointer */
528 xfs_dir2_data_hdr_t
*hdr
= bp
->b_addr
;
530 ASSERT(hdr
->magic
== cpu_to_be32(XFS_DIR2_DATA_MAGIC
) ||
531 hdr
->magic
== cpu_to_be32(XFS_DIR2_BLOCK_MAGIC
));
533 xfs_trans_log_buf(tp
, bp
, (uint
)((char *)dep
- (char *)hdr
),
534 (uint
)((char *)(xfs_dir2_data_entry_tag_p(dep
) + 1) -
539 * Log a data block header.
542 xfs_dir2_data_log_header(
543 struct xfs_trans
*tp
,
546 xfs_dir2_data_hdr_t
*hdr
= bp
->b_addr
;
548 ASSERT(hdr
->magic
== cpu_to_be32(XFS_DIR2_DATA_MAGIC
) ||
549 hdr
->magic
== cpu_to_be32(XFS_DIR2_BLOCK_MAGIC
));
551 xfs_trans_log_buf(tp
, bp
, 0, sizeof(*hdr
) - 1);
555 * Log a data unused entry.
558 xfs_dir2_data_log_unused(
559 struct xfs_trans
*tp
,
561 xfs_dir2_data_unused_t
*dup
) /* data unused pointer */
563 xfs_dir2_data_hdr_t
*hdr
= bp
->b_addr
;
565 ASSERT(hdr
->magic
== cpu_to_be32(XFS_DIR2_DATA_MAGIC
) ||
566 hdr
->magic
== cpu_to_be32(XFS_DIR2_BLOCK_MAGIC
));
569 * Log the first part of the unused entry.
571 xfs_trans_log_buf(tp
, bp
, (uint
)((char *)dup
- (char *)hdr
),
572 (uint
)((char *)&dup
->length
+ sizeof(dup
->length
) -
575 * Log the end (tag) of the unused entry.
577 xfs_trans_log_buf(tp
, bp
,
578 (uint
)((char *)xfs_dir2_data_unused_tag_p(dup
) - (char *)hdr
),
579 (uint
)((char *)xfs_dir2_data_unused_tag_p(dup
) - (char *)hdr
+
580 sizeof(xfs_dir2_data_off_t
) - 1));
584 * Make a byte range in the data block unused.
585 * Its current contents are unimportant.
588 xfs_dir2_data_make_free(
589 struct xfs_trans
*tp
,
591 xfs_dir2_data_aoff_t offset
, /* starting byte offset */
592 xfs_dir2_data_aoff_t len
, /* length in bytes */
593 int *needlogp
, /* out: log header */
594 int *needscanp
) /* out: regen bestfree */
596 xfs_dir2_data_hdr_t
*hdr
; /* data block pointer */
597 xfs_dir2_data_free_t
*dfp
; /* bestfree pointer */
598 char *endptr
; /* end of data area */
599 xfs_mount_t
*mp
; /* filesystem mount point */
600 int needscan
; /* need to regen bestfree */
601 xfs_dir2_data_unused_t
*newdup
; /* new unused entry */
602 xfs_dir2_data_unused_t
*postdup
; /* unused entry after us */
603 xfs_dir2_data_unused_t
*prevdup
; /* unused entry before us */
609 * Figure out where the end of the data area is.
611 if (hdr
->magic
== cpu_to_be32(XFS_DIR2_DATA_MAGIC
))
612 endptr
= (char *)hdr
+ mp
->m_dirblksize
;
614 xfs_dir2_block_tail_t
*btp
; /* block tail */
616 ASSERT(hdr
->magic
== cpu_to_be32(XFS_DIR2_BLOCK_MAGIC
));
617 btp
= xfs_dir2_block_tail_p(mp
, hdr
);
618 endptr
= (char *)xfs_dir2_block_leaf_p(btp
);
621 * If this isn't the start of the block, then back up to
622 * the previous entry and see if it's free.
624 if (offset
> sizeof(*hdr
)) {
625 __be16
*tagp
; /* tag just before us */
627 tagp
= (__be16
*)((char *)hdr
+ offset
) - 1;
628 prevdup
= (xfs_dir2_data_unused_t
*)((char *)hdr
+ be16_to_cpu(*tagp
));
629 if (be16_to_cpu(prevdup
->freetag
) != XFS_DIR2_DATA_FREE_TAG
)
634 * If this isn't the end of the block, see if the entry after
637 if ((char *)hdr
+ offset
+ len
< endptr
) {
639 (xfs_dir2_data_unused_t
*)((char *)hdr
+ offset
+ len
);
640 if (be16_to_cpu(postdup
->freetag
) != XFS_DIR2_DATA_FREE_TAG
)
644 ASSERT(*needscanp
== 0);
647 * Previous and following entries are both free,
648 * merge everything into a single free entry.
650 if (prevdup
&& postdup
) {
651 xfs_dir2_data_free_t
*dfp2
; /* another bestfree pointer */
654 * See if prevdup and/or postdup are in bestfree table.
656 dfp
= xfs_dir2_data_freefind(hdr
, prevdup
);
657 dfp2
= xfs_dir2_data_freefind(hdr
, postdup
);
659 * We need a rescan unless there are exactly 2 free entries
660 * namely our two. Then we know what's happening, otherwise
661 * since the third bestfree is there, there might be more
664 needscan
= (hdr
->bestfree
[2].length
!= 0);
666 * Fix up the new big freespace.
668 be16_add_cpu(&prevdup
->length
, len
+ be16_to_cpu(postdup
->length
));
669 *xfs_dir2_data_unused_tag_p(prevdup
) =
670 cpu_to_be16((char *)prevdup
- (char *)hdr
);
671 xfs_dir2_data_log_unused(tp
, bp
, prevdup
);
674 * Has to be the case that entries 0 and 1 are
675 * dfp and dfp2 (don't know which is which), and
677 * Remove entry 1 first then entry 0.
680 if (dfp
== &hdr
->bestfree
[1]) {
681 dfp
= &hdr
->bestfree
[0];
683 dfp2
= &hdr
->bestfree
[1];
685 xfs_dir2_data_freeremove(hdr
, dfp2
, needlogp
);
686 xfs_dir2_data_freeremove(hdr
, dfp
, needlogp
);
688 * Now insert the new entry.
690 dfp
= xfs_dir2_data_freeinsert(hdr
, prevdup
, needlogp
);
691 ASSERT(dfp
== &hdr
->bestfree
[0]);
692 ASSERT(dfp
->length
== prevdup
->length
);
693 ASSERT(!dfp
[1].length
);
694 ASSERT(!dfp
[2].length
);
698 * The entry before us is free, merge with it.
701 dfp
= xfs_dir2_data_freefind(hdr
, prevdup
);
702 be16_add_cpu(&prevdup
->length
, len
);
703 *xfs_dir2_data_unused_tag_p(prevdup
) =
704 cpu_to_be16((char *)prevdup
- (char *)hdr
);
705 xfs_dir2_data_log_unused(tp
, bp
, prevdup
);
707 * If the previous entry was in the table, the new entry
708 * is longer, so it will be in the table too. Remove
709 * the old one and add the new one.
712 xfs_dir2_data_freeremove(hdr
, dfp
, needlogp
);
713 xfs_dir2_data_freeinsert(hdr
, prevdup
, needlogp
);
716 * Otherwise we need a scan if the new entry is big enough.
719 needscan
= be16_to_cpu(prevdup
->length
) >
720 be16_to_cpu(hdr
->bestfree
[2].length
);
724 * The following entry is free, merge with it.
727 dfp
= xfs_dir2_data_freefind(hdr
, postdup
);
728 newdup
= (xfs_dir2_data_unused_t
*)((char *)hdr
+ offset
);
729 newdup
->freetag
= cpu_to_be16(XFS_DIR2_DATA_FREE_TAG
);
730 newdup
->length
= cpu_to_be16(len
+ be16_to_cpu(postdup
->length
));
731 *xfs_dir2_data_unused_tag_p(newdup
) =
732 cpu_to_be16((char *)newdup
- (char *)hdr
);
733 xfs_dir2_data_log_unused(tp
, bp
, newdup
);
735 * If the following entry was in the table, the new entry
736 * is longer, so it will be in the table too. Remove
737 * the old one and add the new one.
740 xfs_dir2_data_freeremove(hdr
, dfp
, needlogp
);
741 xfs_dir2_data_freeinsert(hdr
, newdup
, needlogp
);
744 * Otherwise we need a scan if the new entry is big enough.
747 needscan
= be16_to_cpu(newdup
->length
) >
748 be16_to_cpu(hdr
->bestfree
[2].length
);
752 * Neither neighbor is free. Make a new entry.
755 newdup
= (xfs_dir2_data_unused_t
*)((char *)hdr
+ offset
);
756 newdup
->freetag
= cpu_to_be16(XFS_DIR2_DATA_FREE_TAG
);
757 newdup
->length
= cpu_to_be16(len
);
758 *xfs_dir2_data_unused_tag_p(newdup
) =
759 cpu_to_be16((char *)newdup
- (char *)hdr
);
760 xfs_dir2_data_log_unused(tp
, bp
, newdup
);
761 xfs_dir2_data_freeinsert(hdr
, newdup
, needlogp
);
763 *needscanp
= needscan
;
767 * Take a byte range out of an existing unused space and make it un-free.
770 xfs_dir2_data_use_free(
771 struct xfs_trans
*tp
,
773 xfs_dir2_data_unused_t
*dup
, /* unused entry */
774 xfs_dir2_data_aoff_t offset
, /* starting offset to use */
775 xfs_dir2_data_aoff_t len
, /* length to use */
776 int *needlogp
, /* out: need to log header */
777 int *needscanp
) /* out: need regen bestfree */
779 xfs_dir2_data_hdr_t
*hdr
; /* data block header */
780 xfs_dir2_data_free_t
*dfp
; /* bestfree pointer */
781 int matchback
; /* matches end of freespace */
782 int matchfront
; /* matches start of freespace */
783 int needscan
; /* need to regen bestfree */
784 xfs_dir2_data_unused_t
*newdup
; /* new unused entry */
785 xfs_dir2_data_unused_t
*newdup2
; /* another new unused entry */
786 int oldlen
; /* old unused entry's length */
789 ASSERT(hdr
->magic
== cpu_to_be32(XFS_DIR2_DATA_MAGIC
) ||
790 hdr
->magic
== cpu_to_be32(XFS_DIR2_BLOCK_MAGIC
));
791 ASSERT(be16_to_cpu(dup
->freetag
) == XFS_DIR2_DATA_FREE_TAG
);
792 ASSERT(offset
>= (char *)dup
- (char *)hdr
);
793 ASSERT(offset
+ len
<= (char *)dup
+ be16_to_cpu(dup
->length
) - (char *)hdr
);
794 ASSERT((char *)dup
- (char *)hdr
== be16_to_cpu(*xfs_dir2_data_unused_tag_p(dup
)));
796 * Look up the entry in the bestfree table.
798 dfp
= xfs_dir2_data_freefind(hdr
, dup
);
799 oldlen
= be16_to_cpu(dup
->length
);
800 ASSERT(dfp
|| oldlen
<= be16_to_cpu(hdr
->bestfree
[2].length
));
802 * Check for alignment with front and back of the entry.
804 matchfront
= (char *)dup
- (char *)hdr
== offset
;
805 matchback
= (char *)dup
+ oldlen
- (char *)hdr
== offset
+ len
;
806 ASSERT(*needscanp
== 0);
809 * If we matched it exactly we just need to get rid of it from
810 * the bestfree table.
812 if (matchfront
&& matchback
) {
814 needscan
= (hdr
->bestfree
[2].offset
!= 0);
816 xfs_dir2_data_freeremove(hdr
, dfp
, needlogp
);
820 * We match the first part of the entry.
821 * Make a new entry with the remaining freespace.
823 else if (matchfront
) {
824 newdup
= (xfs_dir2_data_unused_t
*)((char *)hdr
+ offset
+ len
);
825 newdup
->freetag
= cpu_to_be16(XFS_DIR2_DATA_FREE_TAG
);
826 newdup
->length
= cpu_to_be16(oldlen
- len
);
827 *xfs_dir2_data_unused_tag_p(newdup
) =
828 cpu_to_be16((char *)newdup
- (char *)hdr
);
829 xfs_dir2_data_log_unused(tp
, bp
, newdup
);
831 * If it was in the table, remove it and add the new one.
834 xfs_dir2_data_freeremove(hdr
, dfp
, needlogp
);
835 dfp
= xfs_dir2_data_freeinsert(hdr
, newdup
, needlogp
);
837 ASSERT(dfp
->length
== newdup
->length
);
838 ASSERT(be16_to_cpu(dfp
->offset
) == (char *)newdup
- (char *)hdr
);
840 * If we got inserted at the last slot,
841 * that means we don't know if there was a better
842 * choice for the last slot, or not. Rescan.
844 needscan
= dfp
== &hdr
->bestfree
[2];
848 * We match the last part of the entry.
849 * Trim the allocated space off the tail of the entry.
851 else if (matchback
) {
853 newdup
->length
= cpu_to_be16(((char *)hdr
+ offset
) - (char *)newdup
);
854 *xfs_dir2_data_unused_tag_p(newdup
) =
855 cpu_to_be16((char *)newdup
- (char *)hdr
);
856 xfs_dir2_data_log_unused(tp
, bp
, newdup
);
858 * If it was in the table, remove it and add the new one.
861 xfs_dir2_data_freeremove(hdr
, dfp
, needlogp
);
862 dfp
= xfs_dir2_data_freeinsert(hdr
, newdup
, needlogp
);
864 ASSERT(dfp
->length
== newdup
->length
);
865 ASSERT(be16_to_cpu(dfp
->offset
) == (char *)newdup
- (char *)hdr
);
867 * If we got inserted at the last slot,
868 * that means we don't know if there was a better
869 * choice for the last slot, or not. Rescan.
871 needscan
= dfp
== &hdr
->bestfree
[2];
875 * Poking out the middle of an entry.
876 * Make two new entries.
880 newdup
->length
= cpu_to_be16(((char *)hdr
+ offset
) - (char *)newdup
);
881 *xfs_dir2_data_unused_tag_p(newdup
) =
882 cpu_to_be16((char *)newdup
- (char *)hdr
);
883 xfs_dir2_data_log_unused(tp
, bp
, newdup
);
884 newdup2
= (xfs_dir2_data_unused_t
*)((char *)hdr
+ offset
+ len
);
885 newdup2
->freetag
= cpu_to_be16(XFS_DIR2_DATA_FREE_TAG
);
886 newdup2
->length
= cpu_to_be16(oldlen
- len
- be16_to_cpu(newdup
->length
));
887 *xfs_dir2_data_unused_tag_p(newdup2
) =
888 cpu_to_be16((char *)newdup2
- (char *)hdr
);
889 xfs_dir2_data_log_unused(tp
, bp
, newdup2
);
891 * If the old entry was in the table, we need to scan
892 * if the 3rd entry was valid, since these entries
893 * are smaller than the old one.
894 * If we don't need to scan that means there were 1 or 2
895 * entries in the table, and removing the old and adding
896 * the 2 new will work.
899 needscan
= (hdr
->bestfree
[2].length
!= 0);
901 xfs_dir2_data_freeremove(hdr
, dfp
, needlogp
);
902 xfs_dir2_data_freeinsert(hdr
, newdup
, needlogp
);
903 xfs_dir2_data_freeinsert(hdr
, newdup2
,
908 *needscanp
= needscan
;