2 * Copyright (c) 2000-2002,2005 Silicon Graphics, Inc.
3 * Copyright (c) 2013 Red Hat, Inc.
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as
8 * published by the Free Software Foundation.
10 * This program is distributed in the hope that it would be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21 #include "xfs_format.h"
22 #include "xfs_log_format.h"
23 #include "xfs_trans_resv.h"
26 #include "xfs_mount.h"
27 #include "xfs_da_format.h"
28 #include "xfs_da_btree.h"
29 #include "xfs_inode.h"
31 #include "xfs_dir2_priv.h"
32 #include "xfs_error.h"
33 #include "xfs_trans.h"
34 #include "xfs_buf_item.h"
35 #include "xfs_cksum.h"
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_dir3_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
;
68 bf
= xfs_dir3_data_bestfree_p(hdr
);
69 p
= (char *)xfs_dir3_data_entry_p(hdr
);
72 case cpu_to_be32(XFS_DIR3_BLOCK_MAGIC
):
73 case cpu_to_be32(XFS_DIR2_BLOCK_MAGIC
):
74 btp
= xfs_dir2_block_tail_p(mp
, hdr
);
75 lep
= xfs_dir2_block_leaf_p(btp
);
78 case cpu_to_be32(XFS_DIR3_DATA_MAGIC
):
79 case cpu_to_be32(XFS_DIR2_DATA_MAGIC
):
80 endp
= (char *)hdr
+ mp
->m_dirblksize
;
83 XFS_ERROR_REPORT("Bad Magic", XFS_ERRLEVEL_LOW
, mp
);
87 count
= lastfree
= freeseen
= 0;
89 * Account for zero bestfree entries.
92 XFS_WANT_CORRUPTED_RETURN(!bf
[0].offset
);
96 XFS_WANT_CORRUPTED_RETURN(!bf
[1].offset
);
100 XFS_WANT_CORRUPTED_RETURN(!bf
[2].offset
);
104 XFS_WANT_CORRUPTED_RETURN(be16_to_cpu(bf
[0].length
) >=
105 be16_to_cpu(bf
[1].length
));
106 XFS_WANT_CORRUPTED_RETURN(be16_to_cpu(bf
[1].length
) >=
107 be16_to_cpu(bf
[2].length
));
109 * Loop over the data/unused entries.
112 dup
= (xfs_dir2_data_unused_t
*)p
;
114 * If it's unused, look for the space in the bestfree table.
115 * If we find it, account for that, else make sure it
116 * doesn't need to be there.
118 if (be16_to_cpu(dup
->freetag
) == XFS_DIR2_DATA_FREE_TAG
) {
119 XFS_WANT_CORRUPTED_RETURN(lastfree
== 0);
120 XFS_WANT_CORRUPTED_RETURN(
121 be16_to_cpu(*xfs_dir2_data_unused_tag_p(dup
)) ==
122 (char *)dup
- (char *)hdr
);
123 dfp
= xfs_dir2_data_freefind(hdr
, dup
);
126 XFS_WANT_CORRUPTED_RETURN(
127 (freeseen
& (1 << i
)) == 0);
130 XFS_WANT_CORRUPTED_RETURN(
131 be16_to_cpu(dup
->length
) <=
132 be16_to_cpu(bf
[2].length
));
134 p
+= be16_to_cpu(dup
->length
);
139 * It's a real entry. Validate the fields.
140 * If this is a block directory then make sure it's
141 * in the leaf section of the block.
142 * The linear search is crude but this is DEBUG code.
144 dep
= (xfs_dir2_data_entry_t
*)p
;
145 XFS_WANT_CORRUPTED_RETURN(dep
->namelen
!= 0);
146 XFS_WANT_CORRUPTED_RETURN(
147 !xfs_dir_ino_validate(mp
, be64_to_cpu(dep
->inumber
)));
148 XFS_WANT_CORRUPTED_RETURN(
149 be16_to_cpu(*xfs_dir3_data_entry_tag_p(mp
, dep
)) ==
150 (char *)dep
- (char *)hdr
);
151 XFS_WANT_CORRUPTED_RETURN(
152 xfs_dir3_dirent_get_ftype(mp
, dep
) < XFS_DIR3_FT_MAX
);
155 if (hdr
->magic
== cpu_to_be32(XFS_DIR2_BLOCK_MAGIC
) ||
156 hdr
->magic
== cpu_to_be32(XFS_DIR3_BLOCK_MAGIC
)) {
157 addr
= xfs_dir2_db_off_to_dataptr(mp
, mp
->m_dirdatablk
,
158 (xfs_dir2_data_aoff_t
)
159 ((char *)dep
- (char *)hdr
));
160 name
.name
= dep
->name
;
161 name
.len
= dep
->namelen
;
162 hash
= mp
->m_dirnameops
->hashname(&name
);
163 for (i
= 0; i
< be32_to_cpu(btp
->count
); i
++) {
164 if (be32_to_cpu(lep
[i
].address
) == addr
&&
165 be32_to_cpu(lep
[i
].hashval
) == hash
)
168 XFS_WANT_CORRUPTED_RETURN(i
< be32_to_cpu(btp
->count
));
170 p
+= xfs_dir3_data_entsize(mp
, dep
->namelen
);
173 * Need to have seen all the entries and all the bestfree slots.
175 XFS_WANT_CORRUPTED_RETURN(freeseen
== 7);
176 if (hdr
->magic
== cpu_to_be32(XFS_DIR2_BLOCK_MAGIC
) ||
177 hdr
->magic
== cpu_to_be32(XFS_DIR3_BLOCK_MAGIC
)) {
178 for (i
= stale
= 0; i
< be32_to_cpu(btp
->count
); i
++) {
179 if (lep
[i
].address
==
180 cpu_to_be32(XFS_DIR2_NULL_DATAPTR
))
183 XFS_WANT_CORRUPTED_RETURN(
184 be32_to_cpu(lep
[i
].hashval
) >=
185 be32_to_cpu(lep
[i
- 1].hashval
));
187 XFS_WANT_CORRUPTED_RETURN(count
==
188 be32_to_cpu(btp
->count
) - be32_to_cpu(btp
->stale
));
189 XFS_WANT_CORRUPTED_RETURN(stale
== be32_to_cpu(btp
->stale
));
195 xfs_dir3_data_verify(
198 struct xfs_mount
*mp
= bp
->b_target
->bt_mount
;
199 struct xfs_dir3_blk_hdr
*hdr3
= bp
->b_addr
;
201 if (xfs_sb_version_hascrc(&mp
->m_sb
)) {
202 if (hdr3
->magic
!= cpu_to_be32(XFS_DIR3_DATA_MAGIC
))
204 if (!uuid_equal(&hdr3
->uuid
, &mp
->m_sb
.sb_uuid
))
206 if (be64_to_cpu(hdr3
->blkno
) != bp
->b_bn
)
209 if (hdr3
->magic
!= cpu_to_be32(XFS_DIR2_DATA_MAGIC
))
212 if (__xfs_dir3_data_check(NULL
, bp
))
218 * Readahead of the first block of the directory when it is opened is completely
219 * oblivious to the format of the directory. Hence we can either get a block
220 * format buffer or a data format buffer on readahead.
223 xfs_dir3_data_reada_verify(
226 struct xfs_mount
*mp
= bp
->b_target
->bt_mount
;
227 struct xfs_dir2_data_hdr
*hdr
= bp
->b_addr
;
229 switch (hdr
->magic
) {
230 case cpu_to_be32(XFS_DIR2_BLOCK_MAGIC
):
231 case cpu_to_be32(XFS_DIR3_BLOCK_MAGIC
):
232 bp
->b_ops
= &xfs_dir3_block_buf_ops
;
233 bp
->b_ops
->verify_read(bp
);
235 case cpu_to_be32(XFS_DIR2_DATA_MAGIC
):
236 case cpu_to_be32(XFS_DIR3_DATA_MAGIC
):
237 xfs_dir3_data_verify(bp
);
240 XFS_CORRUPTION_ERROR(__func__
, XFS_ERRLEVEL_LOW
, mp
, hdr
);
241 xfs_buf_ioerror(bp
, EFSCORRUPTED
);
247 xfs_dir3_data_read_verify(
250 struct xfs_mount
*mp
= bp
->b_target
->bt_mount
;
252 if ((xfs_sb_version_hascrc(&mp
->m_sb
) &&
253 !xfs_verify_cksum(bp
->b_addr
, BBTOB(bp
->b_length
),
254 XFS_DIR3_DATA_CRC_OFF
)) ||
255 !xfs_dir3_data_verify(bp
)) {
256 XFS_CORRUPTION_ERROR(__func__
, XFS_ERRLEVEL_LOW
, mp
, bp
->b_addr
);
257 xfs_buf_ioerror(bp
, EFSCORRUPTED
);
262 xfs_dir3_data_write_verify(
265 struct xfs_mount
*mp
= bp
->b_target
->bt_mount
;
266 struct xfs_buf_log_item
*bip
= bp
->b_fspriv
;
267 struct xfs_dir3_blk_hdr
*hdr3
= bp
->b_addr
;
269 if (!xfs_dir3_data_verify(bp
)) {
270 XFS_CORRUPTION_ERROR(__func__
, XFS_ERRLEVEL_LOW
, mp
, bp
->b_addr
);
271 xfs_buf_ioerror(bp
, EFSCORRUPTED
);
275 if (!xfs_sb_version_hascrc(&mp
->m_sb
))
279 hdr3
->lsn
= cpu_to_be64(bip
->bli_item
.li_lsn
);
281 xfs_update_cksum(bp
->b_addr
, BBTOB(bp
->b_length
), XFS_DIR3_DATA_CRC_OFF
);
284 const struct xfs_buf_ops xfs_dir3_data_buf_ops
= {
285 .verify_read
= xfs_dir3_data_read_verify
,
286 .verify_write
= xfs_dir3_data_write_verify
,
289 static const struct xfs_buf_ops xfs_dir3_data_reada_buf_ops
= {
290 .verify_read
= xfs_dir3_data_reada_verify
,
291 .verify_write
= xfs_dir3_data_write_verify
,
297 struct xfs_trans
*tp
,
298 struct xfs_inode
*dp
,
300 xfs_daddr_t mapped_bno
,
301 struct xfs_buf
**bpp
)
305 err
= xfs_da_read_buf(tp
, dp
, bno
, mapped_bno
, bpp
,
306 XFS_DATA_FORK
, &xfs_dir3_data_buf_ops
);
308 xfs_trans_buf_set_type(tp
, *bpp
, XFS_BLFT_DIR_DATA_BUF
);
313 xfs_dir3_data_readahead(
314 struct xfs_trans
*tp
,
315 struct xfs_inode
*dp
,
317 xfs_daddr_t mapped_bno
)
319 return xfs_da_reada_buf(tp
, dp
, bno
, mapped_bno
,
320 XFS_DATA_FORK
, &xfs_dir3_data_reada_buf_ops
);
324 * Given a data block and an unused entry from that block,
325 * return the bestfree entry if any that corresponds to it.
327 xfs_dir2_data_free_t
*
328 xfs_dir2_data_freefind(
329 xfs_dir2_data_hdr_t
*hdr
, /* data block */
330 xfs_dir2_data_unused_t
*dup
) /* data unused entry */
332 xfs_dir2_data_free_t
*dfp
; /* bestfree entry */
333 xfs_dir2_data_aoff_t off
; /* offset value needed */
334 struct xfs_dir2_data_free
*bf
;
336 int matched
; /* matched the value */
337 int seenzero
; /* saw a 0 bestfree entry */
340 off
= (xfs_dir2_data_aoff_t
)((char *)dup
- (char *)hdr
);
341 bf
= xfs_dir3_data_bestfree_p(hdr
);
345 * Validate some consistency in the bestfree table.
346 * Check order, non-overlapping entries, and if we find the
347 * one we're looking for it has to be exact.
349 ASSERT(hdr
->magic
== cpu_to_be32(XFS_DIR2_DATA_MAGIC
) ||
350 hdr
->magic
== cpu_to_be32(XFS_DIR3_DATA_MAGIC
) ||
351 hdr
->magic
== cpu_to_be32(XFS_DIR2_BLOCK_MAGIC
) ||
352 hdr
->magic
== cpu_to_be32(XFS_DIR3_BLOCK_MAGIC
));
353 for (dfp
= &bf
[0], seenzero
= matched
= 0;
354 dfp
< &bf
[XFS_DIR2_DATA_FD_COUNT
];
357 ASSERT(!dfp
->length
);
361 ASSERT(seenzero
== 0);
362 if (be16_to_cpu(dfp
->offset
) == off
) {
364 ASSERT(dfp
->length
== dup
->length
);
365 } else if (off
< be16_to_cpu(dfp
->offset
))
366 ASSERT(off
+ be16_to_cpu(dup
->length
) <= be16_to_cpu(dfp
->offset
));
368 ASSERT(be16_to_cpu(dfp
->offset
) + be16_to_cpu(dfp
->length
) <= off
);
369 ASSERT(matched
|| be16_to_cpu(dfp
->length
) >= be16_to_cpu(dup
->length
));
371 ASSERT(be16_to_cpu(dfp
[-1].length
) >= be16_to_cpu(dfp
[0].length
));
375 * If this is smaller than the smallest bestfree entry,
376 * it can't be there since they're sorted.
378 if (be16_to_cpu(dup
->length
) <
379 be16_to_cpu(bf
[XFS_DIR2_DATA_FD_COUNT
- 1].length
))
382 * Look at the three bestfree entries for our guy.
384 for (dfp
= &bf
[0]; dfp
< &bf
[XFS_DIR2_DATA_FD_COUNT
]; dfp
++) {
387 if (be16_to_cpu(dfp
->offset
) == off
)
391 * Didn't find it. This only happens if there are duplicate lengths.
397 * Insert an unused-space entry into the bestfree table.
399 xfs_dir2_data_free_t
* /* entry inserted */
400 xfs_dir2_data_freeinsert(
401 xfs_dir2_data_hdr_t
*hdr
, /* data block pointer */
402 xfs_dir2_data_unused_t
*dup
, /* unused space */
403 int *loghead
) /* log the data header (out) */
405 xfs_dir2_data_free_t
*dfp
; /* bestfree table pointer */
406 xfs_dir2_data_free_t
new; /* new bestfree entry */
408 ASSERT(hdr
->magic
== cpu_to_be32(XFS_DIR2_DATA_MAGIC
) ||
409 hdr
->magic
== cpu_to_be32(XFS_DIR2_BLOCK_MAGIC
) ||
410 hdr
->magic
== cpu_to_be32(XFS_DIR3_DATA_MAGIC
) ||
411 hdr
->magic
== cpu_to_be32(XFS_DIR3_BLOCK_MAGIC
));
413 dfp
= xfs_dir3_data_bestfree_p(hdr
);
414 new.length
= dup
->length
;
415 new.offset
= cpu_to_be16((char *)dup
- (char *)hdr
);
418 * Insert at position 0, 1, or 2; or not at all.
420 if (be16_to_cpu(new.length
) > be16_to_cpu(dfp
[0].length
)) {
427 if (be16_to_cpu(new.length
) > be16_to_cpu(dfp
[1].length
)) {
433 if (be16_to_cpu(new.length
) > be16_to_cpu(dfp
[2].length
)) {
442 * Remove a bestfree entry from the table.
445 xfs_dir2_data_freeremove(
446 xfs_dir2_data_hdr_t
*hdr
, /* data block header */
447 xfs_dir2_data_free_t
*dfp
, /* bestfree entry pointer */
448 int *loghead
) /* out: log data header */
450 struct xfs_dir2_data_free
*bf
;
452 ASSERT(hdr
->magic
== cpu_to_be32(XFS_DIR2_DATA_MAGIC
) ||
453 hdr
->magic
== cpu_to_be32(XFS_DIR2_BLOCK_MAGIC
) ||
454 hdr
->magic
== cpu_to_be32(XFS_DIR3_DATA_MAGIC
) ||
455 hdr
->magic
== cpu_to_be32(XFS_DIR3_BLOCK_MAGIC
));
458 * It's the first entry, slide the next 2 up.
460 bf
= xfs_dir3_data_bestfree_p(hdr
);
466 * It's the second entry, slide the 3rd entry up.
468 else if (dfp
== &bf
[1])
471 * Must be the last entry.
474 ASSERT(dfp
== &bf
[2]);
476 * Clear the 3rd entry, must be zero now.
484 * Given a data block, reconstruct its bestfree map.
487 xfs_dir2_data_freescan(
488 xfs_mount_t
*mp
, /* filesystem mount point */
489 xfs_dir2_data_hdr_t
*hdr
, /* data block header */
490 int *loghead
) /* out: log data header */
492 xfs_dir2_block_tail_t
*btp
; /* block tail */
493 xfs_dir2_data_entry_t
*dep
; /* active data entry */
494 xfs_dir2_data_unused_t
*dup
; /* unused data entry */
495 struct xfs_dir2_data_free
*bf
;
496 char *endp
; /* end of block's data */
497 char *p
; /* current entry pointer */
499 ASSERT(hdr
->magic
== cpu_to_be32(XFS_DIR2_DATA_MAGIC
) ||
500 hdr
->magic
== cpu_to_be32(XFS_DIR3_DATA_MAGIC
) ||
501 hdr
->magic
== cpu_to_be32(XFS_DIR2_BLOCK_MAGIC
) ||
502 hdr
->magic
== cpu_to_be32(XFS_DIR3_BLOCK_MAGIC
));
505 * Start by clearing the table.
507 bf
= xfs_dir3_data_bestfree_p(hdr
);
508 memset(bf
, 0, sizeof(*bf
) * XFS_DIR2_DATA_FD_COUNT
);
513 p
= (char *)xfs_dir3_data_entry_p(hdr
);
514 if (hdr
->magic
== cpu_to_be32(XFS_DIR2_BLOCK_MAGIC
) ||
515 hdr
->magic
== cpu_to_be32(XFS_DIR3_BLOCK_MAGIC
)) {
516 btp
= xfs_dir2_block_tail_p(mp
, hdr
);
517 endp
= (char *)xfs_dir2_block_leaf_p(btp
);
519 endp
= (char *)hdr
+ mp
->m_dirblksize
;
521 * Loop over the block's entries.
524 dup
= (xfs_dir2_data_unused_t
*)p
;
526 * If it's a free entry, insert it.
528 if (be16_to_cpu(dup
->freetag
) == XFS_DIR2_DATA_FREE_TAG
) {
529 ASSERT((char *)dup
- (char *)hdr
==
530 be16_to_cpu(*xfs_dir2_data_unused_tag_p(dup
)));
531 xfs_dir2_data_freeinsert(hdr
, dup
, loghead
);
532 p
+= be16_to_cpu(dup
->length
);
535 * For active entries, check their tags and skip them.
538 dep
= (xfs_dir2_data_entry_t
*)p
;
539 ASSERT((char *)dep
- (char *)hdr
==
540 be16_to_cpu(*xfs_dir3_data_entry_tag_p(mp
, dep
)));
541 p
+= xfs_dir3_data_entsize(mp
, dep
->namelen
);
547 * Initialize a data block at the given block number in the directory.
548 * Give back the buffer for the created block.
552 xfs_da_args_t
*args
, /* directory operation args */
553 xfs_dir2_db_t blkno
, /* logical dir block number */
554 struct xfs_buf
**bpp
) /* output block buffer */
556 struct xfs_buf
*bp
; /* block buffer */
557 xfs_dir2_data_hdr_t
*hdr
; /* data block header */
558 xfs_inode_t
*dp
; /* incore directory inode */
559 xfs_dir2_data_unused_t
*dup
; /* unused entry pointer */
560 struct xfs_dir2_data_free
*bf
;
561 int error
; /* error return value */
562 int i
; /* bestfree index */
563 xfs_mount_t
*mp
; /* filesystem mount point */
564 xfs_trans_t
*tp
; /* transaction pointer */
571 * Get the buffer set up for the block.
573 error
= xfs_da_get_buf(tp
, dp
, xfs_dir2_db_to_da(mp
, blkno
), -1, &bp
,
577 bp
->b_ops
= &xfs_dir3_data_buf_ops
;
578 xfs_trans_buf_set_type(tp
, bp
, XFS_BLFT_DIR_DATA_BUF
);
581 * Initialize the header.
584 if (xfs_sb_version_hascrc(&mp
->m_sb
)) {
585 struct xfs_dir3_blk_hdr
*hdr3
= bp
->b_addr
;
587 memset(hdr3
, 0, sizeof(*hdr3
));
588 hdr3
->magic
= cpu_to_be32(XFS_DIR3_DATA_MAGIC
);
589 hdr3
->blkno
= cpu_to_be64(bp
->b_bn
);
590 hdr3
->owner
= cpu_to_be64(dp
->i_ino
);
591 uuid_copy(&hdr3
->uuid
, &mp
->m_sb
.sb_uuid
);
594 hdr
->magic
= cpu_to_be32(XFS_DIR2_DATA_MAGIC
);
596 bf
= xfs_dir3_data_bestfree_p(hdr
);
597 bf
[0].offset
= cpu_to_be16(xfs_dir3_data_entry_offset(hdr
));
598 for (i
= 1; i
< XFS_DIR2_DATA_FD_COUNT
; i
++) {
604 * Set up an unused entry for the block's body.
606 dup
= xfs_dir3_data_unused_p(hdr
);
607 dup
->freetag
= cpu_to_be16(XFS_DIR2_DATA_FREE_TAG
);
609 t
= mp
->m_dirblksize
- (uint
)xfs_dir3_data_entry_offset(hdr
);
610 bf
[0].length
= cpu_to_be16(t
);
611 dup
->length
= cpu_to_be16(t
);
612 *xfs_dir2_data_unused_tag_p(dup
) = cpu_to_be16((char *)dup
- (char *)hdr
);
614 * Log it and return it.
616 xfs_dir2_data_log_header(tp
, bp
);
617 xfs_dir2_data_log_unused(tp
, bp
, dup
);
623 * Log an active data entry from the block.
626 xfs_dir2_data_log_entry(
627 struct xfs_trans
*tp
,
629 xfs_dir2_data_entry_t
*dep
) /* data entry pointer */
631 struct xfs_dir2_data_hdr
*hdr
= bp
->b_addr
;
632 struct xfs_mount
*mp
= tp
->t_mountp
;
634 ASSERT(hdr
->magic
== cpu_to_be32(XFS_DIR2_DATA_MAGIC
) ||
635 hdr
->magic
== cpu_to_be32(XFS_DIR3_DATA_MAGIC
) ||
636 hdr
->magic
== cpu_to_be32(XFS_DIR2_BLOCK_MAGIC
) ||
637 hdr
->magic
== cpu_to_be32(XFS_DIR3_BLOCK_MAGIC
));
639 xfs_trans_log_buf(tp
, bp
, (uint
)((char *)dep
- (char *)hdr
),
640 (uint
)((char *)(xfs_dir3_data_entry_tag_p(mp
, dep
) + 1) -
645 * Log a data block header.
648 xfs_dir2_data_log_header(
649 struct xfs_trans
*tp
,
652 xfs_dir2_data_hdr_t
*hdr
= bp
->b_addr
;
654 ASSERT(hdr
->magic
== cpu_to_be32(XFS_DIR2_DATA_MAGIC
) ||
655 hdr
->magic
== cpu_to_be32(XFS_DIR3_DATA_MAGIC
) ||
656 hdr
->magic
== cpu_to_be32(XFS_DIR2_BLOCK_MAGIC
) ||
657 hdr
->magic
== cpu_to_be32(XFS_DIR3_BLOCK_MAGIC
));
659 xfs_trans_log_buf(tp
, bp
, 0, xfs_dir3_data_entry_offset(hdr
) - 1);
663 * Log a data unused entry.
666 xfs_dir2_data_log_unused(
667 struct xfs_trans
*tp
,
669 xfs_dir2_data_unused_t
*dup
) /* data unused pointer */
671 xfs_dir2_data_hdr_t
*hdr
= bp
->b_addr
;
673 ASSERT(hdr
->magic
== cpu_to_be32(XFS_DIR2_DATA_MAGIC
) ||
674 hdr
->magic
== cpu_to_be32(XFS_DIR3_DATA_MAGIC
) ||
675 hdr
->magic
== cpu_to_be32(XFS_DIR2_BLOCK_MAGIC
) ||
676 hdr
->magic
== cpu_to_be32(XFS_DIR3_BLOCK_MAGIC
));
679 * Log the first part of the unused entry.
681 xfs_trans_log_buf(tp
, bp
, (uint
)((char *)dup
- (char *)hdr
),
682 (uint
)((char *)&dup
->length
+ sizeof(dup
->length
) -
685 * Log the end (tag) of the unused entry.
687 xfs_trans_log_buf(tp
, bp
,
688 (uint
)((char *)xfs_dir2_data_unused_tag_p(dup
) - (char *)hdr
),
689 (uint
)((char *)xfs_dir2_data_unused_tag_p(dup
) - (char *)hdr
+
690 sizeof(xfs_dir2_data_off_t
) - 1));
694 * Make a byte range in the data block unused.
695 * Its current contents are unimportant.
698 xfs_dir2_data_make_free(
699 struct xfs_trans
*tp
,
701 xfs_dir2_data_aoff_t offset
, /* starting byte offset */
702 xfs_dir2_data_aoff_t len
, /* length in bytes */
703 int *needlogp
, /* out: log header */
704 int *needscanp
) /* out: regen bestfree */
706 xfs_dir2_data_hdr_t
*hdr
; /* data block pointer */
707 xfs_dir2_data_free_t
*dfp
; /* bestfree pointer */
708 char *endptr
; /* end of data area */
709 xfs_mount_t
*mp
; /* filesystem mount point */
710 int needscan
; /* need to regen bestfree */
711 xfs_dir2_data_unused_t
*newdup
; /* new unused entry */
712 xfs_dir2_data_unused_t
*postdup
; /* unused entry after us */
713 xfs_dir2_data_unused_t
*prevdup
; /* unused entry before us */
714 struct xfs_dir2_data_free
*bf
;
720 * Figure out where the end of the data area is.
722 if (hdr
->magic
== cpu_to_be32(XFS_DIR2_DATA_MAGIC
) ||
723 hdr
->magic
== cpu_to_be32(XFS_DIR3_DATA_MAGIC
))
724 endptr
= (char *)hdr
+ mp
->m_dirblksize
;
726 xfs_dir2_block_tail_t
*btp
; /* block tail */
728 ASSERT(hdr
->magic
== cpu_to_be32(XFS_DIR2_BLOCK_MAGIC
) ||
729 hdr
->magic
== cpu_to_be32(XFS_DIR3_BLOCK_MAGIC
));
730 btp
= xfs_dir2_block_tail_p(mp
, hdr
);
731 endptr
= (char *)xfs_dir2_block_leaf_p(btp
);
734 * If this isn't the start of the block, then back up to
735 * the previous entry and see if it's free.
737 if (offset
> xfs_dir3_data_entry_offset(hdr
)) {
738 __be16
*tagp
; /* tag just before us */
740 tagp
= (__be16
*)((char *)hdr
+ offset
) - 1;
741 prevdup
= (xfs_dir2_data_unused_t
*)((char *)hdr
+ be16_to_cpu(*tagp
));
742 if (be16_to_cpu(prevdup
->freetag
) != XFS_DIR2_DATA_FREE_TAG
)
747 * If this isn't the end of the block, see if the entry after
750 if ((char *)hdr
+ offset
+ len
< endptr
) {
752 (xfs_dir2_data_unused_t
*)((char *)hdr
+ offset
+ len
);
753 if (be16_to_cpu(postdup
->freetag
) != XFS_DIR2_DATA_FREE_TAG
)
757 ASSERT(*needscanp
== 0);
760 * Previous and following entries are both free,
761 * merge everything into a single free entry.
763 bf
= xfs_dir3_data_bestfree_p(hdr
);
764 if (prevdup
&& postdup
) {
765 xfs_dir2_data_free_t
*dfp2
; /* another bestfree pointer */
768 * See if prevdup and/or postdup are in bestfree table.
770 dfp
= xfs_dir2_data_freefind(hdr
, prevdup
);
771 dfp2
= xfs_dir2_data_freefind(hdr
, postdup
);
773 * We need a rescan unless there are exactly 2 free entries
774 * namely our two. Then we know what's happening, otherwise
775 * since the third bestfree is there, there might be more
778 needscan
= (bf
[2].length
!= 0);
780 * Fix up the new big freespace.
782 be16_add_cpu(&prevdup
->length
, len
+ be16_to_cpu(postdup
->length
));
783 *xfs_dir2_data_unused_tag_p(prevdup
) =
784 cpu_to_be16((char *)prevdup
- (char *)hdr
);
785 xfs_dir2_data_log_unused(tp
, bp
, prevdup
);
788 * Has to be the case that entries 0 and 1 are
789 * dfp and dfp2 (don't know which is which), and
791 * Remove entry 1 first then entry 0.
799 xfs_dir2_data_freeremove(hdr
, dfp2
, needlogp
);
800 xfs_dir2_data_freeremove(hdr
, dfp
, needlogp
);
802 * Now insert the new entry.
804 dfp
= xfs_dir2_data_freeinsert(hdr
, prevdup
, needlogp
);
805 ASSERT(dfp
== &bf
[0]);
806 ASSERT(dfp
->length
== prevdup
->length
);
807 ASSERT(!dfp
[1].length
);
808 ASSERT(!dfp
[2].length
);
812 * The entry before us is free, merge with it.
815 dfp
= xfs_dir2_data_freefind(hdr
, prevdup
);
816 be16_add_cpu(&prevdup
->length
, len
);
817 *xfs_dir2_data_unused_tag_p(prevdup
) =
818 cpu_to_be16((char *)prevdup
- (char *)hdr
);
819 xfs_dir2_data_log_unused(tp
, bp
, prevdup
);
821 * If the previous entry was in the table, the new entry
822 * is longer, so it will be in the table too. Remove
823 * the old one and add the new one.
826 xfs_dir2_data_freeremove(hdr
, dfp
, needlogp
);
827 xfs_dir2_data_freeinsert(hdr
, prevdup
, needlogp
);
830 * Otherwise we need a scan if the new entry is big enough.
833 needscan
= be16_to_cpu(prevdup
->length
) >
834 be16_to_cpu(bf
[2].length
);
838 * The following entry is free, merge with it.
841 dfp
= xfs_dir2_data_freefind(hdr
, postdup
);
842 newdup
= (xfs_dir2_data_unused_t
*)((char *)hdr
+ offset
);
843 newdup
->freetag
= cpu_to_be16(XFS_DIR2_DATA_FREE_TAG
);
844 newdup
->length
= cpu_to_be16(len
+ be16_to_cpu(postdup
->length
));
845 *xfs_dir2_data_unused_tag_p(newdup
) =
846 cpu_to_be16((char *)newdup
- (char *)hdr
);
847 xfs_dir2_data_log_unused(tp
, bp
, newdup
);
849 * If the following entry was in the table, the new entry
850 * is longer, so it will be in the table too. Remove
851 * the old one and add the new one.
854 xfs_dir2_data_freeremove(hdr
, dfp
, needlogp
);
855 xfs_dir2_data_freeinsert(hdr
, newdup
, needlogp
);
858 * Otherwise we need a scan if the new entry is big enough.
861 needscan
= be16_to_cpu(newdup
->length
) >
862 be16_to_cpu(bf
[2].length
);
866 * Neither neighbor is free. Make a new entry.
869 newdup
= (xfs_dir2_data_unused_t
*)((char *)hdr
+ offset
);
870 newdup
->freetag
= cpu_to_be16(XFS_DIR2_DATA_FREE_TAG
);
871 newdup
->length
= cpu_to_be16(len
);
872 *xfs_dir2_data_unused_tag_p(newdup
) =
873 cpu_to_be16((char *)newdup
- (char *)hdr
);
874 xfs_dir2_data_log_unused(tp
, bp
, newdup
);
875 xfs_dir2_data_freeinsert(hdr
, newdup
, needlogp
);
877 *needscanp
= needscan
;
881 * Take a byte range out of an existing unused space and make it un-free.
884 xfs_dir2_data_use_free(
885 struct xfs_trans
*tp
,
887 xfs_dir2_data_unused_t
*dup
, /* unused entry */
888 xfs_dir2_data_aoff_t offset
, /* starting offset to use */
889 xfs_dir2_data_aoff_t len
, /* length to use */
890 int *needlogp
, /* out: need to log header */
891 int *needscanp
) /* out: need regen bestfree */
893 xfs_dir2_data_hdr_t
*hdr
; /* data block header */
894 xfs_dir2_data_free_t
*dfp
; /* bestfree pointer */
895 int matchback
; /* matches end of freespace */
896 int matchfront
; /* matches start of freespace */
897 int needscan
; /* need to regen bestfree */
898 xfs_dir2_data_unused_t
*newdup
; /* new unused entry */
899 xfs_dir2_data_unused_t
*newdup2
; /* another new unused entry */
900 int oldlen
; /* old unused entry's length */
901 struct xfs_dir2_data_free
*bf
;
904 ASSERT(hdr
->magic
== cpu_to_be32(XFS_DIR2_DATA_MAGIC
) ||
905 hdr
->magic
== cpu_to_be32(XFS_DIR3_DATA_MAGIC
) ||
906 hdr
->magic
== cpu_to_be32(XFS_DIR2_BLOCK_MAGIC
) ||
907 hdr
->magic
== cpu_to_be32(XFS_DIR3_BLOCK_MAGIC
));
908 ASSERT(be16_to_cpu(dup
->freetag
) == XFS_DIR2_DATA_FREE_TAG
);
909 ASSERT(offset
>= (char *)dup
- (char *)hdr
);
910 ASSERT(offset
+ len
<= (char *)dup
+ be16_to_cpu(dup
->length
) - (char *)hdr
);
911 ASSERT((char *)dup
- (char *)hdr
== be16_to_cpu(*xfs_dir2_data_unused_tag_p(dup
)));
913 * Look up the entry in the bestfree table.
915 dfp
= xfs_dir2_data_freefind(hdr
, dup
);
916 oldlen
= be16_to_cpu(dup
->length
);
917 bf
= xfs_dir3_data_bestfree_p(hdr
);
918 ASSERT(dfp
|| oldlen
<= be16_to_cpu(bf
[2].length
));
920 * Check for alignment with front and back of the entry.
922 matchfront
= (char *)dup
- (char *)hdr
== offset
;
923 matchback
= (char *)dup
+ oldlen
- (char *)hdr
== offset
+ len
;
924 ASSERT(*needscanp
== 0);
927 * If we matched it exactly we just need to get rid of it from
928 * the bestfree table.
930 if (matchfront
&& matchback
) {
932 needscan
= (bf
[2].offset
!= 0);
934 xfs_dir2_data_freeremove(hdr
, dfp
, needlogp
);
938 * We match the first part of the entry.
939 * Make a new entry with the remaining freespace.
941 else if (matchfront
) {
942 newdup
= (xfs_dir2_data_unused_t
*)((char *)hdr
+ offset
+ len
);
943 newdup
->freetag
= cpu_to_be16(XFS_DIR2_DATA_FREE_TAG
);
944 newdup
->length
= cpu_to_be16(oldlen
- len
);
945 *xfs_dir2_data_unused_tag_p(newdup
) =
946 cpu_to_be16((char *)newdup
- (char *)hdr
);
947 xfs_dir2_data_log_unused(tp
, bp
, newdup
);
949 * If it was in the table, remove it and add the new one.
952 xfs_dir2_data_freeremove(hdr
, dfp
, needlogp
);
953 dfp
= xfs_dir2_data_freeinsert(hdr
, newdup
, needlogp
);
955 ASSERT(dfp
->length
== newdup
->length
);
956 ASSERT(be16_to_cpu(dfp
->offset
) == (char *)newdup
- (char *)hdr
);
958 * If we got inserted at the last slot,
959 * that means we don't know if there was a better
960 * choice for the last slot, or not. Rescan.
962 needscan
= dfp
== &bf
[2];
966 * We match the last part of the entry.
967 * Trim the allocated space off the tail of the entry.
969 else if (matchback
) {
971 newdup
->length
= cpu_to_be16(((char *)hdr
+ offset
) - (char *)newdup
);
972 *xfs_dir2_data_unused_tag_p(newdup
) =
973 cpu_to_be16((char *)newdup
- (char *)hdr
);
974 xfs_dir2_data_log_unused(tp
, bp
, newdup
);
976 * If it was in the table, remove it and add the new one.
979 xfs_dir2_data_freeremove(hdr
, dfp
, needlogp
);
980 dfp
= xfs_dir2_data_freeinsert(hdr
, newdup
, needlogp
);
982 ASSERT(dfp
->length
== newdup
->length
);
983 ASSERT(be16_to_cpu(dfp
->offset
) == (char *)newdup
- (char *)hdr
);
985 * If we got inserted at the last slot,
986 * that means we don't know if there was a better
987 * choice for the last slot, or not. Rescan.
989 needscan
= dfp
== &bf
[2];
993 * Poking out the middle of an entry.
994 * Make two new entries.
998 newdup
->length
= cpu_to_be16(((char *)hdr
+ offset
) - (char *)newdup
);
999 *xfs_dir2_data_unused_tag_p(newdup
) =
1000 cpu_to_be16((char *)newdup
- (char *)hdr
);
1001 xfs_dir2_data_log_unused(tp
, bp
, newdup
);
1002 newdup2
= (xfs_dir2_data_unused_t
*)((char *)hdr
+ offset
+ len
);
1003 newdup2
->freetag
= cpu_to_be16(XFS_DIR2_DATA_FREE_TAG
);
1004 newdup2
->length
= cpu_to_be16(oldlen
- len
- be16_to_cpu(newdup
->length
));
1005 *xfs_dir2_data_unused_tag_p(newdup2
) =
1006 cpu_to_be16((char *)newdup2
- (char *)hdr
);
1007 xfs_dir2_data_log_unused(tp
, bp
, newdup2
);
1009 * If the old entry was in the table, we need to scan
1010 * if the 3rd entry was valid, since these entries
1011 * are smaller than the old one.
1012 * If we don't need to scan that means there were 1 or 2
1013 * entries in the table, and removing the old and adding
1014 * the 2 new will work.
1017 needscan
= (bf
[2].length
!= 0);
1019 xfs_dir2_data_freeremove(hdr
, dfp
, needlogp
);
1020 xfs_dir2_data_freeinsert(hdr
, newdup
, needlogp
);
1021 xfs_dir2_data_freeinsert(hdr
, newdup2
,
1026 *needscanp
= needscan
;