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
);
205 xfs_buf_ioend(bp
, 0);
210 struct xfs_trans
*tp
,
211 struct xfs_inode
*dp
,
213 xfs_daddr_t mapped_bno
,
214 struct xfs_buf
**bpp
)
216 return xfs_da_read_buf(tp
, dp
, bno
, mapped_bno
, bpp
,
217 XFS_DATA_FORK
, xfs_dir2_data_verify
);
221 xfs_dir2_data_readahead(
222 struct xfs_trans
*tp
,
223 struct xfs_inode
*dp
,
225 xfs_daddr_t mapped_bno
)
227 return xfs_da_reada_buf(tp
, dp
, bno
, mapped_bno
,
228 XFS_DATA_FORK
, xfs_dir2_data_verify
);
232 * Given a data block and an unused entry from that block,
233 * return the bestfree entry if any that corresponds to it.
235 STATIC xfs_dir2_data_free_t
*
236 xfs_dir2_data_freefind(
237 xfs_dir2_data_hdr_t
*hdr
, /* data block */
238 xfs_dir2_data_unused_t
*dup
) /* data unused entry */
240 xfs_dir2_data_free_t
*dfp
; /* bestfree entry */
241 xfs_dir2_data_aoff_t off
; /* offset value needed */
242 #if defined(DEBUG) && defined(__KERNEL__)
243 int matched
; /* matched the value */
244 int seenzero
; /* saw a 0 bestfree entry */
247 off
= (xfs_dir2_data_aoff_t
)((char *)dup
- (char *)hdr
);
248 #if defined(DEBUG) && defined(__KERNEL__)
250 * Validate some consistency in the bestfree table.
251 * Check order, non-overlapping entries, and if we find the
252 * one we're looking for it has to be exact.
254 ASSERT(hdr
->magic
== cpu_to_be32(XFS_DIR2_DATA_MAGIC
) ||
255 hdr
->magic
== cpu_to_be32(XFS_DIR2_BLOCK_MAGIC
));
256 for (dfp
= &hdr
->bestfree
[0], seenzero
= matched
= 0;
257 dfp
< &hdr
->bestfree
[XFS_DIR2_DATA_FD_COUNT
];
260 ASSERT(!dfp
->length
);
264 ASSERT(seenzero
== 0);
265 if (be16_to_cpu(dfp
->offset
) == off
) {
267 ASSERT(dfp
->length
== dup
->length
);
268 } else if (off
< be16_to_cpu(dfp
->offset
))
269 ASSERT(off
+ be16_to_cpu(dup
->length
) <= be16_to_cpu(dfp
->offset
));
271 ASSERT(be16_to_cpu(dfp
->offset
) + be16_to_cpu(dfp
->length
) <= off
);
272 ASSERT(matched
|| be16_to_cpu(dfp
->length
) >= be16_to_cpu(dup
->length
));
273 if (dfp
> &hdr
->bestfree
[0])
274 ASSERT(be16_to_cpu(dfp
[-1].length
) >= be16_to_cpu(dfp
[0].length
));
278 * If this is smaller than the smallest bestfree entry,
279 * it can't be there since they're sorted.
281 if (be16_to_cpu(dup
->length
) <
282 be16_to_cpu(hdr
->bestfree
[XFS_DIR2_DATA_FD_COUNT
- 1].length
))
285 * Look at the three bestfree entries for our guy.
287 for (dfp
= &hdr
->bestfree
[0];
288 dfp
< &hdr
->bestfree
[XFS_DIR2_DATA_FD_COUNT
];
292 if (be16_to_cpu(dfp
->offset
) == off
)
296 * Didn't find it. This only happens if there are duplicate lengths.
302 * Insert an unused-space entry into the bestfree table.
304 xfs_dir2_data_free_t
* /* entry inserted */
305 xfs_dir2_data_freeinsert(
306 xfs_dir2_data_hdr_t
*hdr
, /* data block pointer */
307 xfs_dir2_data_unused_t
*dup
, /* unused space */
308 int *loghead
) /* log the data header (out) */
310 xfs_dir2_data_free_t
*dfp
; /* bestfree table pointer */
311 xfs_dir2_data_free_t
new; /* new bestfree entry */
314 ASSERT(hdr
->magic
== cpu_to_be32(XFS_DIR2_DATA_MAGIC
) ||
315 hdr
->magic
== cpu_to_be32(XFS_DIR2_BLOCK_MAGIC
));
318 new.length
= dup
->length
;
319 new.offset
= cpu_to_be16((char *)dup
- (char *)hdr
);
322 * Insert at position 0, 1, or 2; or not at all.
324 if (be16_to_cpu(new.length
) > be16_to_cpu(dfp
[0].length
)) {
331 if (be16_to_cpu(new.length
) > be16_to_cpu(dfp
[1].length
)) {
337 if (be16_to_cpu(new.length
) > be16_to_cpu(dfp
[2].length
)) {
346 * Remove a bestfree entry from the table.
349 xfs_dir2_data_freeremove(
350 xfs_dir2_data_hdr_t
*hdr
, /* data block header */
351 xfs_dir2_data_free_t
*dfp
, /* bestfree entry pointer */
352 int *loghead
) /* out: log data header */
355 ASSERT(hdr
->magic
== cpu_to_be32(XFS_DIR2_DATA_MAGIC
) ||
356 hdr
->magic
== cpu_to_be32(XFS_DIR2_BLOCK_MAGIC
));
359 * It's the first entry, slide the next 2 up.
361 if (dfp
== &hdr
->bestfree
[0]) {
362 hdr
->bestfree
[0] = hdr
->bestfree
[1];
363 hdr
->bestfree
[1] = hdr
->bestfree
[2];
366 * It's the second entry, slide the 3rd entry up.
368 else if (dfp
== &hdr
->bestfree
[1])
369 hdr
->bestfree
[1] = hdr
->bestfree
[2];
371 * Must be the last entry.
374 ASSERT(dfp
== &hdr
->bestfree
[2]);
376 * Clear the 3rd entry, must be zero now.
378 hdr
->bestfree
[2].length
= 0;
379 hdr
->bestfree
[2].offset
= 0;
384 * Given a data block, reconstruct its bestfree map.
387 xfs_dir2_data_freescan(
388 xfs_mount_t
*mp
, /* filesystem mount point */
389 xfs_dir2_data_hdr_t
*hdr
, /* data block header */
390 int *loghead
) /* out: log data header */
392 xfs_dir2_block_tail_t
*btp
; /* block tail */
393 xfs_dir2_data_entry_t
*dep
; /* active data entry */
394 xfs_dir2_data_unused_t
*dup
; /* unused data entry */
395 char *endp
; /* end of block's data */
396 char *p
; /* current entry pointer */
399 ASSERT(hdr
->magic
== cpu_to_be32(XFS_DIR2_DATA_MAGIC
) ||
400 hdr
->magic
== cpu_to_be32(XFS_DIR2_BLOCK_MAGIC
));
403 * Start by clearing the table.
405 memset(hdr
->bestfree
, 0, sizeof(hdr
->bestfree
));
410 p
= (char *)(hdr
+ 1);
411 if (hdr
->magic
== cpu_to_be32(XFS_DIR2_BLOCK_MAGIC
)) {
412 btp
= xfs_dir2_block_tail_p(mp
, hdr
);
413 endp
= (char *)xfs_dir2_block_leaf_p(btp
);
415 endp
= (char *)hdr
+ mp
->m_dirblksize
;
417 * Loop over the block's entries.
420 dup
= (xfs_dir2_data_unused_t
*)p
;
422 * If it's a free entry, insert it.
424 if (be16_to_cpu(dup
->freetag
) == XFS_DIR2_DATA_FREE_TAG
) {
425 ASSERT((char *)dup
- (char *)hdr
==
426 be16_to_cpu(*xfs_dir2_data_unused_tag_p(dup
)));
427 xfs_dir2_data_freeinsert(hdr
, dup
, loghead
);
428 p
+= be16_to_cpu(dup
->length
);
431 * For active entries, check their tags and skip them.
434 dep
= (xfs_dir2_data_entry_t
*)p
;
435 ASSERT((char *)dep
- (char *)hdr
==
436 be16_to_cpu(*xfs_dir2_data_entry_tag_p(dep
)));
437 p
+= xfs_dir2_data_entsize(dep
->namelen
);
443 * Initialize a data block at the given block number in the directory.
444 * Give back the buffer for the created block.
448 xfs_da_args_t
*args
, /* directory operation args */
449 xfs_dir2_db_t blkno
, /* logical dir block number */
450 struct xfs_buf
**bpp
) /* output block buffer */
452 struct xfs_buf
*bp
; /* block buffer */
453 xfs_dir2_data_hdr_t
*hdr
; /* data block header */
454 xfs_inode_t
*dp
; /* incore directory inode */
455 xfs_dir2_data_unused_t
*dup
; /* unused entry pointer */
456 int error
; /* error return value */
457 int i
; /* bestfree index */
458 xfs_mount_t
*mp
; /* filesystem mount point */
459 xfs_trans_t
*tp
; /* transaction pointer */
466 * Get the buffer set up for the block.
468 error
= xfs_da_get_buf(tp
, dp
, xfs_dir2_db_to_da(mp
, blkno
), -1, &bp
,
476 * Initialize the header.
479 hdr
->magic
= cpu_to_be32(XFS_DIR2_DATA_MAGIC
);
480 hdr
->bestfree
[0].offset
= cpu_to_be16(sizeof(*hdr
));
481 for (i
= 1; i
< XFS_DIR2_DATA_FD_COUNT
; i
++) {
482 hdr
->bestfree
[i
].length
= 0;
483 hdr
->bestfree
[i
].offset
= 0;
487 * Set up an unused entry for the block's body.
489 dup
= (xfs_dir2_data_unused_t
*)(hdr
+ 1);
490 dup
->freetag
= cpu_to_be16(XFS_DIR2_DATA_FREE_TAG
);
492 t
= mp
->m_dirblksize
- (uint
)sizeof(*hdr
);
493 hdr
->bestfree
[0].length
= cpu_to_be16(t
);
494 dup
->length
= cpu_to_be16(t
);
495 *xfs_dir2_data_unused_tag_p(dup
) = cpu_to_be16((char *)dup
- (char *)hdr
);
497 * Log it and return it.
499 xfs_dir2_data_log_header(tp
, bp
);
500 xfs_dir2_data_log_unused(tp
, bp
, dup
);
506 * Log an active data entry from the block.
509 xfs_dir2_data_log_entry(
510 struct xfs_trans
*tp
,
512 xfs_dir2_data_entry_t
*dep
) /* data entry pointer */
514 xfs_dir2_data_hdr_t
*hdr
= bp
->b_addr
;
516 ASSERT(hdr
->magic
== cpu_to_be32(XFS_DIR2_DATA_MAGIC
) ||
517 hdr
->magic
== cpu_to_be32(XFS_DIR2_BLOCK_MAGIC
));
519 xfs_trans_log_buf(tp
, bp
, (uint
)((char *)dep
- (char *)hdr
),
520 (uint
)((char *)(xfs_dir2_data_entry_tag_p(dep
) + 1) -
525 * Log a data block header.
528 xfs_dir2_data_log_header(
529 struct xfs_trans
*tp
,
532 xfs_dir2_data_hdr_t
*hdr
= bp
->b_addr
;
534 ASSERT(hdr
->magic
== cpu_to_be32(XFS_DIR2_DATA_MAGIC
) ||
535 hdr
->magic
== cpu_to_be32(XFS_DIR2_BLOCK_MAGIC
));
537 xfs_trans_log_buf(tp
, bp
, 0, sizeof(*hdr
) - 1);
541 * Log a data unused entry.
544 xfs_dir2_data_log_unused(
545 struct xfs_trans
*tp
,
547 xfs_dir2_data_unused_t
*dup
) /* data unused pointer */
549 xfs_dir2_data_hdr_t
*hdr
= bp
->b_addr
;
551 ASSERT(hdr
->magic
== cpu_to_be32(XFS_DIR2_DATA_MAGIC
) ||
552 hdr
->magic
== cpu_to_be32(XFS_DIR2_BLOCK_MAGIC
));
555 * Log the first part of the unused entry.
557 xfs_trans_log_buf(tp
, bp
, (uint
)((char *)dup
- (char *)hdr
),
558 (uint
)((char *)&dup
->length
+ sizeof(dup
->length
) -
561 * Log the end (tag) of the unused entry.
563 xfs_trans_log_buf(tp
, bp
,
564 (uint
)((char *)xfs_dir2_data_unused_tag_p(dup
) - (char *)hdr
),
565 (uint
)((char *)xfs_dir2_data_unused_tag_p(dup
) - (char *)hdr
+
566 sizeof(xfs_dir2_data_off_t
) - 1));
570 * Make a byte range in the data block unused.
571 * Its current contents are unimportant.
574 xfs_dir2_data_make_free(
575 struct xfs_trans
*tp
,
577 xfs_dir2_data_aoff_t offset
, /* starting byte offset */
578 xfs_dir2_data_aoff_t len
, /* length in bytes */
579 int *needlogp
, /* out: log header */
580 int *needscanp
) /* out: regen bestfree */
582 xfs_dir2_data_hdr_t
*hdr
; /* data block pointer */
583 xfs_dir2_data_free_t
*dfp
; /* bestfree pointer */
584 char *endptr
; /* end of data area */
585 xfs_mount_t
*mp
; /* filesystem mount point */
586 int needscan
; /* need to regen bestfree */
587 xfs_dir2_data_unused_t
*newdup
; /* new unused entry */
588 xfs_dir2_data_unused_t
*postdup
; /* unused entry after us */
589 xfs_dir2_data_unused_t
*prevdup
; /* unused entry before us */
595 * Figure out where the end of the data area is.
597 if (hdr
->magic
== cpu_to_be32(XFS_DIR2_DATA_MAGIC
))
598 endptr
= (char *)hdr
+ mp
->m_dirblksize
;
600 xfs_dir2_block_tail_t
*btp
; /* block tail */
602 ASSERT(hdr
->magic
== cpu_to_be32(XFS_DIR2_BLOCK_MAGIC
));
603 btp
= xfs_dir2_block_tail_p(mp
, hdr
);
604 endptr
= (char *)xfs_dir2_block_leaf_p(btp
);
607 * If this isn't the start of the block, then back up to
608 * the previous entry and see if it's free.
610 if (offset
> sizeof(*hdr
)) {
611 __be16
*tagp
; /* tag just before us */
613 tagp
= (__be16
*)((char *)hdr
+ offset
) - 1;
614 prevdup
= (xfs_dir2_data_unused_t
*)((char *)hdr
+ be16_to_cpu(*tagp
));
615 if (be16_to_cpu(prevdup
->freetag
) != XFS_DIR2_DATA_FREE_TAG
)
620 * If this isn't the end of the block, see if the entry after
623 if ((char *)hdr
+ offset
+ len
< endptr
) {
625 (xfs_dir2_data_unused_t
*)((char *)hdr
+ offset
+ len
);
626 if (be16_to_cpu(postdup
->freetag
) != XFS_DIR2_DATA_FREE_TAG
)
630 ASSERT(*needscanp
== 0);
633 * Previous and following entries are both free,
634 * merge everything into a single free entry.
636 if (prevdup
&& postdup
) {
637 xfs_dir2_data_free_t
*dfp2
; /* another bestfree pointer */
640 * See if prevdup and/or postdup are in bestfree table.
642 dfp
= xfs_dir2_data_freefind(hdr
, prevdup
);
643 dfp2
= xfs_dir2_data_freefind(hdr
, postdup
);
645 * We need a rescan unless there are exactly 2 free entries
646 * namely our two. Then we know what's happening, otherwise
647 * since the third bestfree is there, there might be more
650 needscan
= (hdr
->bestfree
[2].length
!= 0);
652 * Fix up the new big freespace.
654 be16_add_cpu(&prevdup
->length
, len
+ be16_to_cpu(postdup
->length
));
655 *xfs_dir2_data_unused_tag_p(prevdup
) =
656 cpu_to_be16((char *)prevdup
- (char *)hdr
);
657 xfs_dir2_data_log_unused(tp
, bp
, prevdup
);
660 * Has to be the case that entries 0 and 1 are
661 * dfp and dfp2 (don't know which is which), and
663 * Remove entry 1 first then entry 0.
666 if (dfp
== &hdr
->bestfree
[1]) {
667 dfp
= &hdr
->bestfree
[0];
669 dfp2
= &hdr
->bestfree
[1];
671 xfs_dir2_data_freeremove(hdr
, dfp2
, needlogp
);
672 xfs_dir2_data_freeremove(hdr
, dfp
, needlogp
);
674 * Now insert the new entry.
676 dfp
= xfs_dir2_data_freeinsert(hdr
, prevdup
, needlogp
);
677 ASSERT(dfp
== &hdr
->bestfree
[0]);
678 ASSERT(dfp
->length
== prevdup
->length
);
679 ASSERT(!dfp
[1].length
);
680 ASSERT(!dfp
[2].length
);
684 * The entry before us is free, merge with it.
687 dfp
= xfs_dir2_data_freefind(hdr
, prevdup
);
688 be16_add_cpu(&prevdup
->length
, len
);
689 *xfs_dir2_data_unused_tag_p(prevdup
) =
690 cpu_to_be16((char *)prevdup
- (char *)hdr
);
691 xfs_dir2_data_log_unused(tp
, bp
, prevdup
);
693 * If the previous entry was in the table, the new entry
694 * is longer, so it will be in the table too. Remove
695 * the old one and add the new one.
698 xfs_dir2_data_freeremove(hdr
, dfp
, needlogp
);
699 xfs_dir2_data_freeinsert(hdr
, prevdup
, needlogp
);
702 * Otherwise we need a scan if the new entry is big enough.
705 needscan
= be16_to_cpu(prevdup
->length
) >
706 be16_to_cpu(hdr
->bestfree
[2].length
);
710 * The following entry is free, merge with it.
713 dfp
= xfs_dir2_data_freefind(hdr
, postdup
);
714 newdup
= (xfs_dir2_data_unused_t
*)((char *)hdr
+ offset
);
715 newdup
->freetag
= cpu_to_be16(XFS_DIR2_DATA_FREE_TAG
);
716 newdup
->length
= cpu_to_be16(len
+ be16_to_cpu(postdup
->length
));
717 *xfs_dir2_data_unused_tag_p(newdup
) =
718 cpu_to_be16((char *)newdup
- (char *)hdr
);
719 xfs_dir2_data_log_unused(tp
, bp
, newdup
);
721 * If the following entry was in the table, the new entry
722 * is longer, so it will be in the table too. Remove
723 * the old one and add the new one.
726 xfs_dir2_data_freeremove(hdr
, dfp
, needlogp
);
727 xfs_dir2_data_freeinsert(hdr
, newdup
, needlogp
);
730 * Otherwise we need a scan if the new entry is big enough.
733 needscan
= be16_to_cpu(newdup
->length
) >
734 be16_to_cpu(hdr
->bestfree
[2].length
);
738 * Neither neighbor is free. Make a new entry.
741 newdup
= (xfs_dir2_data_unused_t
*)((char *)hdr
+ offset
);
742 newdup
->freetag
= cpu_to_be16(XFS_DIR2_DATA_FREE_TAG
);
743 newdup
->length
= cpu_to_be16(len
);
744 *xfs_dir2_data_unused_tag_p(newdup
) =
745 cpu_to_be16((char *)newdup
- (char *)hdr
);
746 xfs_dir2_data_log_unused(tp
, bp
, newdup
);
747 xfs_dir2_data_freeinsert(hdr
, newdup
, needlogp
);
749 *needscanp
= needscan
;
753 * Take a byte range out of an existing unused space and make it un-free.
756 xfs_dir2_data_use_free(
757 struct xfs_trans
*tp
,
759 xfs_dir2_data_unused_t
*dup
, /* unused entry */
760 xfs_dir2_data_aoff_t offset
, /* starting offset to use */
761 xfs_dir2_data_aoff_t len
, /* length to use */
762 int *needlogp
, /* out: need to log header */
763 int *needscanp
) /* out: need regen bestfree */
765 xfs_dir2_data_hdr_t
*hdr
; /* data block header */
766 xfs_dir2_data_free_t
*dfp
; /* bestfree pointer */
767 int matchback
; /* matches end of freespace */
768 int matchfront
; /* matches start of freespace */
769 int needscan
; /* need to regen bestfree */
770 xfs_dir2_data_unused_t
*newdup
; /* new unused entry */
771 xfs_dir2_data_unused_t
*newdup2
; /* another new unused entry */
772 int oldlen
; /* old unused entry's length */
775 ASSERT(hdr
->magic
== cpu_to_be32(XFS_DIR2_DATA_MAGIC
) ||
776 hdr
->magic
== cpu_to_be32(XFS_DIR2_BLOCK_MAGIC
));
777 ASSERT(be16_to_cpu(dup
->freetag
) == XFS_DIR2_DATA_FREE_TAG
);
778 ASSERT(offset
>= (char *)dup
- (char *)hdr
);
779 ASSERT(offset
+ len
<= (char *)dup
+ be16_to_cpu(dup
->length
) - (char *)hdr
);
780 ASSERT((char *)dup
- (char *)hdr
== be16_to_cpu(*xfs_dir2_data_unused_tag_p(dup
)));
782 * Look up the entry in the bestfree table.
784 dfp
= xfs_dir2_data_freefind(hdr
, dup
);
785 oldlen
= be16_to_cpu(dup
->length
);
786 ASSERT(dfp
|| oldlen
<= be16_to_cpu(hdr
->bestfree
[2].length
));
788 * Check for alignment with front and back of the entry.
790 matchfront
= (char *)dup
- (char *)hdr
== offset
;
791 matchback
= (char *)dup
+ oldlen
- (char *)hdr
== offset
+ len
;
792 ASSERT(*needscanp
== 0);
795 * If we matched it exactly we just need to get rid of it from
796 * the bestfree table.
798 if (matchfront
&& matchback
) {
800 needscan
= (hdr
->bestfree
[2].offset
!= 0);
802 xfs_dir2_data_freeremove(hdr
, dfp
, needlogp
);
806 * We match the first part of the entry.
807 * Make a new entry with the remaining freespace.
809 else if (matchfront
) {
810 newdup
= (xfs_dir2_data_unused_t
*)((char *)hdr
+ offset
+ len
);
811 newdup
->freetag
= cpu_to_be16(XFS_DIR2_DATA_FREE_TAG
);
812 newdup
->length
= cpu_to_be16(oldlen
- len
);
813 *xfs_dir2_data_unused_tag_p(newdup
) =
814 cpu_to_be16((char *)newdup
- (char *)hdr
);
815 xfs_dir2_data_log_unused(tp
, bp
, newdup
);
817 * If it was in the table, remove it and add the new one.
820 xfs_dir2_data_freeremove(hdr
, dfp
, needlogp
);
821 dfp
= xfs_dir2_data_freeinsert(hdr
, newdup
, needlogp
);
823 ASSERT(dfp
->length
== newdup
->length
);
824 ASSERT(be16_to_cpu(dfp
->offset
) == (char *)newdup
- (char *)hdr
);
826 * If we got inserted at the last slot,
827 * that means we don't know if there was a better
828 * choice for the last slot, or not. Rescan.
830 needscan
= dfp
== &hdr
->bestfree
[2];
834 * We match the last part of the entry.
835 * Trim the allocated space off the tail of the entry.
837 else if (matchback
) {
839 newdup
->length
= cpu_to_be16(((char *)hdr
+ offset
) - (char *)newdup
);
840 *xfs_dir2_data_unused_tag_p(newdup
) =
841 cpu_to_be16((char *)newdup
- (char *)hdr
);
842 xfs_dir2_data_log_unused(tp
, bp
, newdup
);
844 * If it was in the table, remove it and add the new one.
847 xfs_dir2_data_freeremove(hdr
, dfp
, needlogp
);
848 dfp
= xfs_dir2_data_freeinsert(hdr
, newdup
, needlogp
);
850 ASSERT(dfp
->length
== newdup
->length
);
851 ASSERT(be16_to_cpu(dfp
->offset
) == (char *)newdup
- (char *)hdr
);
853 * If we got inserted at the last slot,
854 * that means we don't know if there was a better
855 * choice for the last slot, or not. Rescan.
857 needscan
= dfp
== &hdr
->bestfree
[2];
861 * Poking out the middle of an entry.
862 * Make two new entries.
866 newdup
->length
= cpu_to_be16(((char *)hdr
+ offset
) - (char *)newdup
);
867 *xfs_dir2_data_unused_tag_p(newdup
) =
868 cpu_to_be16((char *)newdup
- (char *)hdr
);
869 xfs_dir2_data_log_unused(tp
, bp
, newdup
);
870 newdup2
= (xfs_dir2_data_unused_t
*)((char *)hdr
+ offset
+ len
);
871 newdup2
->freetag
= cpu_to_be16(XFS_DIR2_DATA_FREE_TAG
);
872 newdup2
->length
= cpu_to_be16(oldlen
- len
- be16_to_cpu(newdup
->length
));
873 *xfs_dir2_data_unused_tag_p(newdup2
) =
874 cpu_to_be16((char *)newdup2
- (char *)hdr
);
875 xfs_dir2_data_log_unused(tp
, bp
, newdup2
);
877 * If the old entry was in the table, we need to scan
878 * if the 3rd entry was valid, since these entries
879 * are smaller than the old one.
880 * If we don't need to scan that means there were 1 or 2
881 * entries in the table, and removing the old and adding
882 * the 2 new will work.
885 needscan
= (hdr
->bestfree
[2].length
!= 0);
887 xfs_dir2_data_freeremove(hdr
, dfp
, needlogp
);
888 xfs_dir2_data_freeinsert(hdr
, newdup
, needlogp
);
889 xfs_dir2_data_freeinsert(hdr
, newdup2
,
894 *needscanp
= needscan
;