1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2000-2002,2005 Silicon Graphics, Inc.
4 * Copyright (c) 2013 Red Hat, Inc.
7 #include "libxfs_priv.h"
9 #include "xfs_format.h"
10 #include "xfs_log_format.h"
11 #include "xfs_trans_resv.h"
12 #include "xfs_mount.h"
13 #include "xfs_da_format.h"
14 #include "xfs_da_btree.h"
15 #include "xfs_inode.h"
17 #include "xfs_dir2_priv.h"
18 #include "xfs_trans.h"
19 #include "xfs_cksum.h"
21 static xfs_failaddr_t
xfs_dir2_data_freefind_verify(
22 struct xfs_dir2_data_hdr
*hdr
, struct xfs_dir2_data_free
*bf
,
23 struct xfs_dir2_data_unused
*dup
,
24 struct xfs_dir2_data_free
**bf_ent
);
27 * Check the consistency of the data block.
28 * The input can also be a block-format directory.
29 * Return NULL if the buffer is good, otherwise the address of the error.
32 __xfs_dir3_data_check(
33 struct xfs_inode
*dp
, /* incore inode pointer */
34 struct xfs_buf
*bp
) /* data block's buffer */
36 xfs_dir2_dataptr_t addr
; /* addr for leaf lookup */
37 xfs_dir2_data_free_t
*bf
; /* bestfree table */
38 xfs_dir2_block_tail_t
*btp
=NULL
; /* block tail */
39 int count
; /* count of entries found */
40 xfs_dir2_data_hdr_t
*hdr
; /* data block header */
41 xfs_dir2_data_entry_t
*dep
; /* data entry */
42 xfs_dir2_data_free_t
*dfp
; /* bestfree entry */
43 xfs_dir2_data_unused_t
*dup
; /* unused entry */
44 char *endp
; /* end of useful data */
45 int freeseen
; /* mask of bestfrees seen */
46 xfs_dahash_t hash
; /* hash of current name */
47 int i
; /* leaf index */
48 int lastfree
; /* last entry was unused */
49 xfs_dir2_leaf_entry_t
*lep
=NULL
; /* block leaf entries */
50 xfs_mount_t
*mp
; /* filesystem mount point */
51 char *p
; /* current data position */
52 int stale
; /* count of stale leaves */
54 const struct xfs_dir_ops
*ops
;
55 struct xfs_da_geometry
*geo
;
57 mp
= bp
->b_target
->bt_mount
;
61 * We can be passed a null dp here from a verifier, so we need to go the
62 * hard way to get them.
64 ops
= xfs_dir_get_ops(mp
, dp
);
67 * If this isn't a directory, or we don't get handed the dir ops,
68 * something is seriously wrong. Bail out.
70 if ((dp
&& !S_ISDIR(VFS_I(dp
)->i_mode
)) ||
71 ops
!= xfs_dir_get_ops(mp
, NULL
))
72 return __this_address
;
75 p
= (char *)ops
->data_entry_p(hdr
);
78 case cpu_to_be32(XFS_DIR3_BLOCK_MAGIC
):
79 case cpu_to_be32(XFS_DIR2_BLOCK_MAGIC
):
80 btp
= xfs_dir2_block_tail_p(geo
, hdr
);
81 lep
= xfs_dir2_block_leaf_p(btp
);
84 * The number of leaf entries is limited by the size of the
85 * block and the amount of space used by the data entries.
86 * We don't know how much space is used by the data entries yet,
87 * so just ensure that the count falls somewhere inside the
90 if (be32_to_cpu(btp
->count
) >=
91 ((char *)btp
- p
) / sizeof(struct xfs_dir2_leaf_entry
))
92 return __this_address
;
94 case cpu_to_be32(XFS_DIR3_DATA_MAGIC
):
95 case cpu_to_be32(XFS_DIR2_DATA_MAGIC
):
98 return __this_address
;
100 endp
= xfs_dir3_data_endp(geo
, hdr
);
102 return __this_address
;
105 * Account for zero bestfree entries.
107 bf
= ops
->data_bestfree_p(hdr
);
108 count
= lastfree
= freeseen
= 0;
111 return __this_address
;
116 return __this_address
;
121 return __this_address
;
125 if (be16_to_cpu(bf
[0].length
) < be16_to_cpu(bf
[1].length
))
126 return __this_address
;
127 if (be16_to_cpu(bf
[1].length
) < be16_to_cpu(bf
[2].length
))
128 return __this_address
;
130 * Loop over the data/unused entries.
133 dup
= (xfs_dir2_data_unused_t
*)p
;
135 * If it's unused, look for the space in the bestfree table.
136 * If we find it, account for that, else make sure it
137 * doesn't need to be there.
139 if (be16_to_cpu(dup
->freetag
) == XFS_DIR2_DATA_FREE_TAG
) {
143 return __this_address
;
144 if (endp
< p
+ be16_to_cpu(dup
->length
))
145 return __this_address
;
146 if (be16_to_cpu(*xfs_dir2_data_unused_tag_p(dup
)) !=
147 (char *)dup
- (char *)hdr
)
148 return __this_address
;
149 fa
= xfs_dir2_data_freefind_verify(hdr
, bf
, dup
, &dfp
);
154 if ((freeseen
& (1 << i
)) != 0)
155 return __this_address
;
158 if (be16_to_cpu(dup
->length
) >
159 be16_to_cpu(bf
[2].length
))
160 return __this_address
;
162 p
+= be16_to_cpu(dup
->length
);
167 * It's a real entry. Validate the fields.
168 * If this is a block directory then make sure it's
169 * in the leaf section of the block.
170 * The linear search is crude but this is DEBUG code.
172 dep
= (xfs_dir2_data_entry_t
*)p
;
173 if (dep
->namelen
== 0)
174 return __this_address
;
175 if (xfs_dir_ino_validate(mp
, be64_to_cpu(dep
->inumber
)))
176 return __this_address
;
177 if (endp
< p
+ ops
->data_entsize(dep
->namelen
))
178 return __this_address
;
179 if (be16_to_cpu(*ops
->data_entry_tag_p(dep
)) !=
180 (char *)dep
- (char *)hdr
)
181 return __this_address
;
182 if (ops
->data_get_ftype(dep
) >= XFS_DIR3_FT_MAX
)
183 return __this_address
;
186 if (hdr
->magic
== cpu_to_be32(XFS_DIR2_BLOCK_MAGIC
) ||
187 hdr
->magic
== cpu_to_be32(XFS_DIR3_BLOCK_MAGIC
)) {
188 addr
= xfs_dir2_db_off_to_dataptr(geo
, geo
->datablk
,
189 (xfs_dir2_data_aoff_t
)
190 ((char *)dep
- (char *)hdr
));
191 name
.name
= dep
->name
;
192 name
.len
= dep
->namelen
;
193 hash
= mp
->m_dirnameops
->hashname(&name
);
194 for (i
= 0; i
< be32_to_cpu(btp
->count
); i
++) {
195 if (be32_to_cpu(lep
[i
].address
) == addr
&&
196 be32_to_cpu(lep
[i
].hashval
) == hash
)
199 if (i
>= be32_to_cpu(btp
->count
))
200 return __this_address
;
202 p
+= ops
->data_entsize(dep
->namelen
);
205 * Need to have seen all the entries and all the bestfree slots.
208 return __this_address
;
209 if (hdr
->magic
== cpu_to_be32(XFS_DIR2_BLOCK_MAGIC
) ||
210 hdr
->magic
== cpu_to_be32(XFS_DIR3_BLOCK_MAGIC
)) {
211 for (i
= stale
= 0; i
< be32_to_cpu(btp
->count
); i
++) {
212 if (lep
[i
].address
==
213 cpu_to_be32(XFS_DIR2_NULL_DATAPTR
))
215 if (i
> 0 && be32_to_cpu(lep
[i
].hashval
) <
216 be32_to_cpu(lep
[i
- 1].hashval
))
217 return __this_address
;
219 if (count
!= be32_to_cpu(btp
->count
) - be32_to_cpu(btp
->stale
))
220 return __this_address
;
221 if (stale
!= be32_to_cpu(btp
->stale
))
222 return __this_address
;
230 struct xfs_inode
*dp
,
235 fa
= __xfs_dir3_data_check(dp
, bp
);
238 xfs_corruption_error(__func__
, XFS_ERRLEVEL_LOW
, dp
->i_mount
,
239 bp
->b_addr
, BBTOB(bp
->b_length
), __FILE__
, __LINE__
,
245 static xfs_failaddr_t
246 xfs_dir3_data_verify(
249 struct xfs_mount
*mp
= bp
->b_target
->bt_mount
;
250 struct xfs_dir3_blk_hdr
*hdr3
= bp
->b_addr
;
252 if (xfs_sb_version_hascrc(&mp
->m_sb
)) {
253 if (hdr3
->magic
!= cpu_to_be32(XFS_DIR3_DATA_MAGIC
))
254 return __this_address
;
255 if (!uuid_equal(&hdr3
->uuid
, &mp
->m_sb
.sb_meta_uuid
))
256 return __this_address
;
257 if (be64_to_cpu(hdr3
->blkno
) != bp
->b_bn
)
258 return __this_address
;
259 if (!xfs_log_check_lsn(mp
, be64_to_cpu(hdr3
->lsn
)))
260 return __this_address
;
262 if (hdr3
->magic
!= cpu_to_be32(XFS_DIR2_DATA_MAGIC
))
263 return __this_address
;
265 return __xfs_dir3_data_check(NULL
, bp
);
269 * Readahead of the first block of the directory when it is opened is completely
270 * oblivious to the format of the directory. Hence we can either get a block
271 * format buffer or a data format buffer on readahead.
274 xfs_dir3_data_reada_verify(
277 struct xfs_dir2_data_hdr
*hdr
= bp
->b_addr
;
279 switch (hdr
->magic
) {
280 case cpu_to_be32(XFS_DIR2_BLOCK_MAGIC
):
281 case cpu_to_be32(XFS_DIR3_BLOCK_MAGIC
):
282 bp
->b_ops
= &xfs_dir3_block_buf_ops
;
283 bp
->b_ops
->verify_read(bp
);
285 case cpu_to_be32(XFS_DIR2_DATA_MAGIC
):
286 case cpu_to_be32(XFS_DIR3_DATA_MAGIC
):
287 bp
->b_ops
= &xfs_dir3_data_buf_ops
;
288 bp
->b_ops
->verify_read(bp
);
291 xfs_verifier_error(bp
, -EFSCORRUPTED
, __this_address
);
297 xfs_dir3_data_read_verify(
300 struct xfs_mount
*mp
= bp
->b_target
->bt_mount
;
303 if (xfs_sb_version_hascrc(&mp
->m_sb
) &&
304 !xfs_buf_verify_cksum(bp
, XFS_DIR3_DATA_CRC_OFF
))
305 xfs_verifier_error(bp
, -EFSBADCRC
, __this_address
);
307 fa
= xfs_dir3_data_verify(bp
);
309 xfs_verifier_error(bp
, -EFSCORRUPTED
, fa
);
314 xfs_dir3_data_write_verify(
317 struct xfs_mount
*mp
= bp
->b_target
->bt_mount
;
318 struct xfs_buf_log_item
*bip
= bp
->b_log_item
;
319 struct xfs_dir3_blk_hdr
*hdr3
= bp
->b_addr
;
322 fa
= xfs_dir3_data_verify(bp
);
324 xfs_verifier_error(bp
, -EFSCORRUPTED
, fa
);
328 if (!xfs_sb_version_hascrc(&mp
->m_sb
))
332 hdr3
->lsn
= cpu_to_be64(bip
->bli_item
.li_lsn
);
334 xfs_buf_update_cksum(bp
, XFS_DIR3_DATA_CRC_OFF
);
337 const struct xfs_buf_ops xfs_dir3_data_buf_ops
= {
338 .name
= "xfs_dir3_data",
339 .verify_read
= xfs_dir3_data_read_verify
,
340 .verify_write
= xfs_dir3_data_write_verify
,
341 .verify_struct
= xfs_dir3_data_verify
,
344 static const struct xfs_buf_ops xfs_dir3_data_reada_buf_ops
= {
345 .name
= "xfs_dir3_data_reada",
346 .verify_read
= xfs_dir3_data_reada_verify
,
347 .verify_write
= xfs_dir3_data_write_verify
,
353 struct xfs_trans
*tp
,
354 struct xfs_inode
*dp
,
356 xfs_daddr_t mapped_bno
,
357 struct xfs_buf
**bpp
)
361 err
= xfs_da_read_buf(tp
, dp
, bno
, mapped_bno
, bpp
,
362 XFS_DATA_FORK
, &xfs_dir3_data_buf_ops
);
363 if (!err
&& tp
&& *bpp
)
364 xfs_trans_buf_set_type(tp
, *bpp
, XFS_BLFT_DIR_DATA_BUF
);
369 xfs_dir3_data_readahead(
370 struct xfs_inode
*dp
,
372 xfs_daddr_t mapped_bno
)
374 return xfs_da_reada_buf(dp
, bno
, mapped_bno
,
375 XFS_DATA_FORK
, &xfs_dir3_data_reada_buf_ops
);
379 * Find the bestfree entry that exactly coincides with unused directory space
380 * or a verifier error because the bestfree data are bad.
382 static xfs_failaddr_t
383 xfs_dir2_data_freefind_verify(
384 struct xfs_dir2_data_hdr
*hdr
,
385 struct xfs_dir2_data_free
*bf
,
386 struct xfs_dir2_data_unused
*dup
,
387 struct xfs_dir2_data_free
**bf_ent
)
389 struct xfs_dir2_data_free
*dfp
;
390 xfs_dir2_data_aoff_t off
;
391 bool matched
= false;
392 bool seenzero
= false;
395 off
= (xfs_dir2_data_aoff_t
)((char *)dup
- (char *)hdr
);
398 * Validate some consistency in the bestfree table.
399 * Check order, non-overlapping entries, and if we find the
400 * one we're looking for it has to be exact.
402 for (dfp
= &bf
[0]; dfp
< &bf
[XFS_DIR2_DATA_FD_COUNT
]; dfp
++) {
405 return __this_address
;
410 return __this_address
;
411 if (be16_to_cpu(dfp
->offset
) == off
) {
413 if (dfp
->length
!= dup
->length
)
414 return __this_address
;
415 } else if (be16_to_cpu(dfp
->offset
) > off
) {
416 if (off
+ be16_to_cpu(dup
->length
) >
417 be16_to_cpu(dfp
->offset
))
418 return __this_address
;
420 if (be16_to_cpu(dfp
->offset
) +
421 be16_to_cpu(dfp
->length
) > off
)
422 return __this_address
;
425 be16_to_cpu(dfp
->length
) < be16_to_cpu(dup
->length
))
426 return __this_address
;
428 be16_to_cpu(dfp
[-1].length
) < be16_to_cpu(dfp
[0].length
))
429 return __this_address
;
432 /* Looks ok so far; now try to match up with a bestfree entry. */
433 *bf_ent
= xfs_dir2_data_freefind(hdr
, bf
, dup
);
438 * Given a data block and an unused entry from that block,
439 * return the bestfree entry if any that corresponds to it.
441 xfs_dir2_data_free_t
*
442 xfs_dir2_data_freefind(
443 struct xfs_dir2_data_hdr
*hdr
, /* data block header */
444 struct xfs_dir2_data_free
*bf
, /* bestfree table pointer */
445 struct xfs_dir2_data_unused
*dup
) /* unused space */
447 xfs_dir2_data_free_t
*dfp
; /* bestfree entry */
448 xfs_dir2_data_aoff_t off
; /* offset value needed */
450 off
= (xfs_dir2_data_aoff_t
)((char *)dup
- (char *)hdr
);
453 * If this is smaller than the smallest bestfree entry,
454 * it can't be there since they're sorted.
456 if (be16_to_cpu(dup
->length
) <
457 be16_to_cpu(bf
[XFS_DIR2_DATA_FD_COUNT
- 1].length
))
460 * Look at the three bestfree entries for our guy.
462 for (dfp
= &bf
[0]; dfp
< &bf
[XFS_DIR2_DATA_FD_COUNT
]; dfp
++) {
465 if (be16_to_cpu(dfp
->offset
) == off
)
469 * Didn't find it. This only happens if there are duplicate lengths.
475 * Insert an unused-space entry into the bestfree table.
477 xfs_dir2_data_free_t
* /* entry inserted */
478 xfs_dir2_data_freeinsert(
479 struct xfs_dir2_data_hdr
*hdr
, /* data block pointer */
480 struct xfs_dir2_data_free
*dfp
, /* bestfree table pointer */
481 struct xfs_dir2_data_unused
*dup
, /* unused space */
482 int *loghead
) /* log the data header (out) */
484 xfs_dir2_data_free_t
new; /* new bestfree entry */
486 ASSERT(hdr
->magic
== cpu_to_be32(XFS_DIR2_DATA_MAGIC
) ||
487 hdr
->magic
== cpu_to_be32(XFS_DIR2_BLOCK_MAGIC
) ||
488 hdr
->magic
== cpu_to_be32(XFS_DIR3_DATA_MAGIC
) ||
489 hdr
->magic
== cpu_to_be32(XFS_DIR3_BLOCK_MAGIC
));
491 new.length
= dup
->length
;
492 new.offset
= cpu_to_be16((char *)dup
- (char *)hdr
);
495 * Insert at position 0, 1, or 2; or not at all.
497 if (be16_to_cpu(new.length
) > be16_to_cpu(dfp
[0].length
)) {
504 if (be16_to_cpu(new.length
) > be16_to_cpu(dfp
[1].length
)) {
510 if (be16_to_cpu(new.length
) > be16_to_cpu(dfp
[2].length
)) {
519 * Remove a bestfree entry from the table.
522 xfs_dir2_data_freeremove(
523 struct xfs_dir2_data_hdr
*hdr
, /* data block header */
524 struct xfs_dir2_data_free
*bf
, /* bestfree table pointer */
525 struct xfs_dir2_data_free
*dfp
, /* bestfree entry pointer */
526 int *loghead
) /* out: log data header */
529 ASSERT(hdr
->magic
== cpu_to_be32(XFS_DIR2_DATA_MAGIC
) ||
530 hdr
->magic
== cpu_to_be32(XFS_DIR2_BLOCK_MAGIC
) ||
531 hdr
->magic
== cpu_to_be32(XFS_DIR3_DATA_MAGIC
) ||
532 hdr
->magic
== cpu_to_be32(XFS_DIR3_BLOCK_MAGIC
));
535 * It's the first entry, slide the next 2 up.
542 * It's the second entry, slide the 3rd entry up.
544 else if (dfp
== &bf
[1])
547 * Must be the last entry.
550 ASSERT(dfp
== &bf
[2]);
552 * Clear the 3rd entry, must be zero now.
560 * Given a data block, reconstruct its bestfree map.
563 xfs_dir2_data_freescan_int(
564 struct xfs_da_geometry
*geo
,
565 const struct xfs_dir_ops
*ops
,
566 struct xfs_dir2_data_hdr
*hdr
,
569 xfs_dir2_data_entry_t
*dep
; /* active data entry */
570 xfs_dir2_data_unused_t
*dup
; /* unused data entry */
571 struct xfs_dir2_data_free
*bf
;
572 char *endp
; /* end of block's data */
573 char *p
; /* current entry pointer */
575 ASSERT(hdr
->magic
== cpu_to_be32(XFS_DIR2_DATA_MAGIC
) ||
576 hdr
->magic
== cpu_to_be32(XFS_DIR3_DATA_MAGIC
) ||
577 hdr
->magic
== cpu_to_be32(XFS_DIR2_BLOCK_MAGIC
) ||
578 hdr
->magic
== cpu_to_be32(XFS_DIR3_BLOCK_MAGIC
));
581 * Start by clearing the table.
583 bf
= ops
->data_bestfree_p(hdr
);
584 memset(bf
, 0, sizeof(*bf
) * XFS_DIR2_DATA_FD_COUNT
);
589 p
= (char *)ops
->data_entry_p(hdr
);
590 endp
= xfs_dir3_data_endp(geo
, hdr
);
592 * Loop over the block's entries.
595 dup
= (xfs_dir2_data_unused_t
*)p
;
597 * If it's a free entry, insert it.
599 if (be16_to_cpu(dup
->freetag
) == XFS_DIR2_DATA_FREE_TAG
) {
600 ASSERT((char *)dup
- (char *)hdr
==
601 be16_to_cpu(*xfs_dir2_data_unused_tag_p(dup
)));
602 xfs_dir2_data_freeinsert(hdr
, bf
, dup
, loghead
);
603 p
+= be16_to_cpu(dup
->length
);
606 * For active entries, check their tags and skip them.
609 dep
= (xfs_dir2_data_entry_t
*)p
;
610 ASSERT((char *)dep
- (char *)hdr
==
611 be16_to_cpu(*ops
->data_entry_tag_p(dep
)));
612 p
+= ops
->data_entsize(dep
->namelen
);
618 xfs_dir2_data_freescan(
619 struct xfs_inode
*dp
,
620 struct xfs_dir2_data_hdr
*hdr
,
623 return xfs_dir2_data_freescan_int(dp
->i_mount
->m_dir_geo
, dp
->d_ops
,
628 * Initialize a data block at the given block number in the directory.
629 * Give back the buffer for the created block.
633 xfs_da_args_t
*args
, /* directory operation args */
634 xfs_dir2_db_t blkno
, /* logical dir block number */
635 struct xfs_buf
**bpp
) /* output block buffer */
637 struct xfs_buf
*bp
; /* block buffer */
638 xfs_dir2_data_hdr_t
*hdr
; /* data block header */
639 xfs_inode_t
*dp
; /* incore directory inode */
640 xfs_dir2_data_unused_t
*dup
; /* unused entry pointer */
641 struct xfs_dir2_data_free
*bf
;
642 int error
; /* error return value */
643 int i
; /* bestfree index */
644 xfs_mount_t
*mp
; /* filesystem mount point */
645 xfs_trans_t
*tp
; /* transaction pointer */
652 * Get the buffer set up for the block.
654 error
= xfs_da_get_buf(tp
, dp
, xfs_dir2_db_to_da(args
->geo
, blkno
),
655 -1, &bp
, XFS_DATA_FORK
);
658 bp
->b_ops
= &xfs_dir3_data_buf_ops
;
659 xfs_trans_buf_set_type(tp
, bp
, XFS_BLFT_DIR_DATA_BUF
);
662 * Initialize the header.
665 if (xfs_sb_version_hascrc(&mp
->m_sb
)) {
666 struct xfs_dir3_blk_hdr
*hdr3
= bp
->b_addr
;
668 memset(hdr3
, 0, sizeof(*hdr3
));
669 hdr3
->magic
= cpu_to_be32(XFS_DIR3_DATA_MAGIC
);
670 hdr3
->blkno
= cpu_to_be64(bp
->b_bn
);
671 hdr3
->owner
= cpu_to_be64(dp
->i_ino
);
672 uuid_copy(&hdr3
->uuid
, &mp
->m_sb
.sb_meta_uuid
);
675 hdr
->magic
= cpu_to_be32(XFS_DIR2_DATA_MAGIC
);
677 bf
= dp
->d_ops
->data_bestfree_p(hdr
);
678 bf
[0].offset
= cpu_to_be16(dp
->d_ops
->data_entry_offset
);
679 for (i
= 1; i
< XFS_DIR2_DATA_FD_COUNT
; i
++) {
685 * Set up an unused entry for the block's body.
687 dup
= dp
->d_ops
->data_unused_p(hdr
);
688 dup
->freetag
= cpu_to_be16(XFS_DIR2_DATA_FREE_TAG
);
690 t
= args
->geo
->blksize
- (uint
)dp
->d_ops
->data_entry_offset
;
691 bf
[0].length
= cpu_to_be16(t
);
692 dup
->length
= cpu_to_be16(t
);
693 *xfs_dir2_data_unused_tag_p(dup
) = cpu_to_be16((char *)dup
- (char *)hdr
);
695 * Log it and return it.
697 xfs_dir2_data_log_header(args
, bp
);
698 xfs_dir2_data_log_unused(args
, bp
, dup
);
704 * Log an active data entry from the block.
707 xfs_dir2_data_log_entry(
708 struct xfs_da_args
*args
,
710 xfs_dir2_data_entry_t
*dep
) /* data entry pointer */
712 struct xfs_dir2_data_hdr
*hdr
= bp
->b_addr
;
714 ASSERT(hdr
->magic
== cpu_to_be32(XFS_DIR2_DATA_MAGIC
) ||
715 hdr
->magic
== cpu_to_be32(XFS_DIR3_DATA_MAGIC
) ||
716 hdr
->magic
== cpu_to_be32(XFS_DIR2_BLOCK_MAGIC
) ||
717 hdr
->magic
== cpu_to_be32(XFS_DIR3_BLOCK_MAGIC
));
719 xfs_trans_log_buf(args
->trans
, bp
, (uint
)((char *)dep
- (char *)hdr
),
720 (uint
)((char *)(args
->dp
->d_ops
->data_entry_tag_p(dep
) + 1) -
725 * Log a data block header.
728 xfs_dir2_data_log_header(
729 struct xfs_da_args
*args
,
733 struct xfs_dir2_data_hdr
*hdr
= bp
->b_addr
;
735 ASSERT(hdr
->magic
== cpu_to_be32(XFS_DIR2_DATA_MAGIC
) ||
736 hdr
->magic
== cpu_to_be32(XFS_DIR3_DATA_MAGIC
) ||
737 hdr
->magic
== cpu_to_be32(XFS_DIR2_BLOCK_MAGIC
) ||
738 hdr
->magic
== cpu_to_be32(XFS_DIR3_BLOCK_MAGIC
));
741 xfs_trans_log_buf(args
->trans
, bp
, 0,
742 args
->dp
->d_ops
->data_entry_offset
- 1);
746 * Log a data unused entry.
749 xfs_dir2_data_log_unused(
750 struct xfs_da_args
*args
,
752 xfs_dir2_data_unused_t
*dup
) /* data unused pointer */
754 xfs_dir2_data_hdr_t
*hdr
= bp
->b_addr
;
756 ASSERT(hdr
->magic
== cpu_to_be32(XFS_DIR2_DATA_MAGIC
) ||
757 hdr
->magic
== cpu_to_be32(XFS_DIR3_DATA_MAGIC
) ||
758 hdr
->magic
== cpu_to_be32(XFS_DIR2_BLOCK_MAGIC
) ||
759 hdr
->magic
== cpu_to_be32(XFS_DIR3_BLOCK_MAGIC
));
762 * Log the first part of the unused entry.
764 xfs_trans_log_buf(args
->trans
, bp
, (uint
)((char *)dup
- (char *)hdr
),
765 (uint
)((char *)&dup
->length
+ sizeof(dup
->length
) -
768 * Log the end (tag) of the unused entry.
770 xfs_trans_log_buf(args
->trans
, bp
,
771 (uint
)((char *)xfs_dir2_data_unused_tag_p(dup
) - (char *)hdr
),
772 (uint
)((char *)xfs_dir2_data_unused_tag_p(dup
) - (char *)hdr
+
773 sizeof(xfs_dir2_data_off_t
) - 1));
777 * Make a byte range in the data block unused.
778 * Its current contents are unimportant.
781 xfs_dir2_data_make_free(
782 struct xfs_da_args
*args
,
784 xfs_dir2_data_aoff_t offset
, /* starting byte offset */
785 xfs_dir2_data_aoff_t len
, /* length in bytes */
786 int *needlogp
, /* out: log header */
787 int *needscanp
) /* out: regen bestfree */
789 xfs_dir2_data_hdr_t
*hdr
; /* data block pointer */
790 xfs_dir2_data_free_t
*dfp
; /* bestfree pointer */
791 char *endptr
; /* end of data area */
792 int needscan
; /* need to regen bestfree */
793 xfs_dir2_data_unused_t
*newdup
; /* new unused entry */
794 xfs_dir2_data_unused_t
*postdup
; /* unused entry after us */
795 xfs_dir2_data_unused_t
*prevdup
; /* unused entry before us */
796 struct xfs_dir2_data_free
*bf
;
801 * Figure out where the end of the data area is.
803 endptr
= xfs_dir3_data_endp(args
->geo
, hdr
);
804 ASSERT(endptr
!= NULL
);
807 * If this isn't the start of the block, then back up to
808 * the previous entry and see if it's free.
810 if (offset
> args
->dp
->d_ops
->data_entry_offset
) {
811 __be16
*tagp
; /* tag just before us */
813 tagp
= (__be16
*)((char *)hdr
+ offset
) - 1;
814 prevdup
= (xfs_dir2_data_unused_t
*)((char *)hdr
+ be16_to_cpu(*tagp
));
815 if (be16_to_cpu(prevdup
->freetag
) != XFS_DIR2_DATA_FREE_TAG
)
820 * If this isn't the end of the block, see if the entry after
823 if ((char *)hdr
+ offset
+ len
< endptr
) {
825 (xfs_dir2_data_unused_t
*)((char *)hdr
+ offset
+ len
);
826 if (be16_to_cpu(postdup
->freetag
) != XFS_DIR2_DATA_FREE_TAG
)
830 ASSERT(*needscanp
== 0);
833 * Previous and following entries are both free,
834 * merge everything into a single free entry.
836 bf
= args
->dp
->d_ops
->data_bestfree_p(hdr
);
837 if (prevdup
&& postdup
) {
838 xfs_dir2_data_free_t
*dfp2
; /* another bestfree pointer */
841 * See if prevdup and/or postdup are in bestfree table.
843 dfp
= xfs_dir2_data_freefind(hdr
, bf
, prevdup
);
844 dfp2
= xfs_dir2_data_freefind(hdr
, bf
, postdup
);
846 * We need a rescan unless there are exactly 2 free entries
847 * namely our two. Then we know what's happening, otherwise
848 * since the third bestfree is there, there might be more
851 needscan
= (bf
[2].length
!= 0);
853 * Fix up the new big freespace.
855 be16_add_cpu(&prevdup
->length
, len
+ be16_to_cpu(postdup
->length
));
856 *xfs_dir2_data_unused_tag_p(prevdup
) =
857 cpu_to_be16((char *)prevdup
- (char *)hdr
);
858 xfs_dir2_data_log_unused(args
, bp
, prevdup
);
861 * Has to be the case that entries 0 and 1 are
862 * dfp and dfp2 (don't know which is which), and
864 * Remove entry 1 first then entry 0.
872 xfs_dir2_data_freeremove(hdr
, bf
, dfp2
, needlogp
);
873 xfs_dir2_data_freeremove(hdr
, bf
, dfp
, needlogp
);
875 * Now insert the new entry.
877 dfp
= xfs_dir2_data_freeinsert(hdr
, bf
, prevdup
,
879 ASSERT(dfp
== &bf
[0]);
880 ASSERT(dfp
->length
== prevdup
->length
);
881 ASSERT(!dfp
[1].length
);
882 ASSERT(!dfp
[2].length
);
886 * The entry before us is free, merge with it.
889 dfp
= xfs_dir2_data_freefind(hdr
, bf
, prevdup
);
890 be16_add_cpu(&prevdup
->length
, len
);
891 *xfs_dir2_data_unused_tag_p(prevdup
) =
892 cpu_to_be16((char *)prevdup
- (char *)hdr
);
893 xfs_dir2_data_log_unused(args
, bp
, prevdup
);
895 * If the previous entry was in the table, the new entry
896 * is longer, so it will be in the table too. Remove
897 * the old one and add the new one.
900 xfs_dir2_data_freeremove(hdr
, bf
, dfp
, needlogp
);
901 xfs_dir2_data_freeinsert(hdr
, bf
, prevdup
, needlogp
);
904 * Otherwise we need a scan if the new entry is big enough.
907 needscan
= be16_to_cpu(prevdup
->length
) >
908 be16_to_cpu(bf
[2].length
);
912 * The following entry is free, merge with it.
915 dfp
= xfs_dir2_data_freefind(hdr
, bf
, postdup
);
916 newdup
= (xfs_dir2_data_unused_t
*)((char *)hdr
+ offset
);
917 newdup
->freetag
= cpu_to_be16(XFS_DIR2_DATA_FREE_TAG
);
918 newdup
->length
= cpu_to_be16(len
+ be16_to_cpu(postdup
->length
));
919 *xfs_dir2_data_unused_tag_p(newdup
) =
920 cpu_to_be16((char *)newdup
- (char *)hdr
);
921 xfs_dir2_data_log_unused(args
, bp
, newdup
);
923 * If the following entry was in the table, the new entry
924 * is longer, so it will be in the table too. Remove
925 * the old one and add the new one.
928 xfs_dir2_data_freeremove(hdr
, bf
, dfp
, needlogp
);
929 xfs_dir2_data_freeinsert(hdr
, bf
, newdup
, needlogp
);
932 * Otherwise we need a scan if the new entry is big enough.
935 needscan
= be16_to_cpu(newdup
->length
) >
936 be16_to_cpu(bf
[2].length
);
940 * Neither neighbor is free. Make a new entry.
943 newdup
= (xfs_dir2_data_unused_t
*)((char *)hdr
+ offset
);
944 newdup
->freetag
= cpu_to_be16(XFS_DIR2_DATA_FREE_TAG
);
945 newdup
->length
= cpu_to_be16(len
);
946 *xfs_dir2_data_unused_tag_p(newdup
) =
947 cpu_to_be16((char *)newdup
- (char *)hdr
);
948 xfs_dir2_data_log_unused(args
, bp
, newdup
);
949 xfs_dir2_data_freeinsert(hdr
, bf
, newdup
, needlogp
);
951 *needscanp
= needscan
;
954 /* Check our free data for obvious signs of corruption. */
955 static inline xfs_failaddr_t
956 xfs_dir2_data_check_free(
957 struct xfs_dir2_data_hdr
*hdr
,
958 struct xfs_dir2_data_unused
*dup
,
959 xfs_dir2_data_aoff_t offset
,
960 xfs_dir2_data_aoff_t len
)
962 if (hdr
->magic
!= cpu_to_be32(XFS_DIR2_DATA_MAGIC
) &&
963 hdr
->magic
!= cpu_to_be32(XFS_DIR3_DATA_MAGIC
) &&
964 hdr
->magic
!= cpu_to_be32(XFS_DIR2_BLOCK_MAGIC
) &&
965 hdr
->magic
!= cpu_to_be32(XFS_DIR3_BLOCK_MAGIC
))
966 return __this_address
;
967 if (be16_to_cpu(dup
->freetag
) != XFS_DIR2_DATA_FREE_TAG
)
968 return __this_address
;
969 if (offset
< (char *)dup
- (char *)hdr
)
970 return __this_address
;
971 if (offset
+ len
> (char *)dup
+ be16_to_cpu(dup
->length
) - (char *)hdr
)
972 return __this_address
;
973 if ((char *)dup
- (char *)hdr
!=
974 be16_to_cpu(*xfs_dir2_data_unused_tag_p(dup
)))
975 return __this_address
;
979 /* Sanity-check a new bestfree entry. */
980 static inline xfs_failaddr_t
981 xfs_dir2_data_check_new_free(
982 struct xfs_dir2_data_hdr
*hdr
,
983 struct xfs_dir2_data_free
*dfp
,
984 struct xfs_dir2_data_unused
*newdup
)
987 return __this_address
;
988 if (dfp
->length
!= newdup
->length
)
989 return __this_address
;
990 if (be16_to_cpu(dfp
->offset
) != (char *)newdup
- (char *)hdr
)
991 return __this_address
;
996 * Take a byte range out of an existing unused space and make it un-free.
999 xfs_dir2_data_use_free(
1000 struct xfs_da_args
*args
,
1002 xfs_dir2_data_unused_t
*dup
, /* unused entry */
1003 xfs_dir2_data_aoff_t offset
, /* starting offset to use */
1004 xfs_dir2_data_aoff_t len
, /* length to use */
1005 int *needlogp
, /* out: need to log header */
1006 int *needscanp
) /* out: need regen bestfree */
1008 xfs_dir2_data_hdr_t
*hdr
; /* data block header */
1009 xfs_dir2_data_free_t
*dfp
; /* bestfree pointer */
1010 xfs_dir2_data_unused_t
*newdup
; /* new unused entry */
1011 xfs_dir2_data_unused_t
*newdup2
; /* another new unused entry */
1012 struct xfs_dir2_data_free
*bf
;
1014 int matchback
; /* matches end of freespace */
1015 int matchfront
; /* matches start of freespace */
1016 int needscan
; /* need to regen bestfree */
1017 int oldlen
; /* old unused entry's length */
1020 fa
= xfs_dir2_data_check_free(hdr
, dup
, offset
, len
);
1024 * Look up the entry in the bestfree table.
1026 oldlen
= be16_to_cpu(dup
->length
);
1027 bf
= args
->dp
->d_ops
->data_bestfree_p(hdr
);
1028 dfp
= xfs_dir2_data_freefind(hdr
, bf
, dup
);
1029 ASSERT(dfp
|| oldlen
<= be16_to_cpu(bf
[2].length
));
1031 * Check for alignment with front and back of the entry.
1033 matchfront
= (char *)dup
- (char *)hdr
== offset
;
1034 matchback
= (char *)dup
+ oldlen
- (char *)hdr
== offset
+ len
;
1035 ASSERT(*needscanp
== 0);
1038 * If we matched it exactly we just need to get rid of it from
1039 * the bestfree table.
1041 if (matchfront
&& matchback
) {
1043 needscan
= (bf
[2].offset
!= 0);
1045 xfs_dir2_data_freeremove(hdr
, bf
, dfp
,
1050 * We match the first part of the entry.
1051 * Make a new entry with the remaining freespace.
1053 else if (matchfront
) {
1054 newdup
= (xfs_dir2_data_unused_t
*)((char *)hdr
+ offset
+ len
);
1055 newdup
->freetag
= cpu_to_be16(XFS_DIR2_DATA_FREE_TAG
);
1056 newdup
->length
= cpu_to_be16(oldlen
- len
);
1057 *xfs_dir2_data_unused_tag_p(newdup
) =
1058 cpu_to_be16((char *)newdup
- (char *)hdr
);
1059 xfs_dir2_data_log_unused(args
, bp
, newdup
);
1061 * If it was in the table, remove it and add the new one.
1064 xfs_dir2_data_freeremove(hdr
, bf
, dfp
, needlogp
);
1065 dfp
= xfs_dir2_data_freeinsert(hdr
, bf
, newdup
,
1067 fa
= xfs_dir2_data_check_new_free(hdr
, dfp
, newdup
);
1071 * If we got inserted at the last slot,
1072 * that means we don't know if there was a better
1073 * choice for the last slot, or not. Rescan.
1075 needscan
= dfp
== &bf
[2];
1079 * We match the last part of the entry.
1080 * Trim the allocated space off the tail of the entry.
1082 else if (matchback
) {
1084 newdup
->length
= cpu_to_be16(((char *)hdr
+ offset
) - (char *)newdup
);
1085 *xfs_dir2_data_unused_tag_p(newdup
) =
1086 cpu_to_be16((char *)newdup
- (char *)hdr
);
1087 xfs_dir2_data_log_unused(args
, bp
, newdup
);
1089 * If it was in the table, remove it and add the new one.
1092 xfs_dir2_data_freeremove(hdr
, bf
, dfp
, needlogp
);
1093 dfp
= xfs_dir2_data_freeinsert(hdr
, bf
, newdup
,
1095 fa
= xfs_dir2_data_check_new_free(hdr
, dfp
, newdup
);
1099 * If we got inserted at the last slot,
1100 * that means we don't know if there was a better
1101 * choice for the last slot, or not. Rescan.
1103 needscan
= dfp
== &bf
[2];
1107 * Poking out the middle of an entry.
1108 * Make two new entries.
1112 newdup
->length
= cpu_to_be16(((char *)hdr
+ offset
) - (char *)newdup
);
1113 *xfs_dir2_data_unused_tag_p(newdup
) =
1114 cpu_to_be16((char *)newdup
- (char *)hdr
);
1115 xfs_dir2_data_log_unused(args
, bp
, newdup
);
1116 newdup2
= (xfs_dir2_data_unused_t
*)((char *)hdr
+ offset
+ len
);
1117 newdup2
->freetag
= cpu_to_be16(XFS_DIR2_DATA_FREE_TAG
);
1118 newdup2
->length
= cpu_to_be16(oldlen
- len
- be16_to_cpu(newdup
->length
));
1119 *xfs_dir2_data_unused_tag_p(newdup2
) =
1120 cpu_to_be16((char *)newdup2
- (char *)hdr
);
1121 xfs_dir2_data_log_unused(args
, bp
, newdup2
);
1123 * If the old entry was in the table, we need to scan
1124 * if the 3rd entry was valid, since these entries
1125 * are smaller than the old one.
1126 * If we don't need to scan that means there were 1 or 2
1127 * entries in the table, and removing the old and adding
1128 * the 2 new will work.
1131 needscan
= (bf
[2].length
!= 0);
1133 xfs_dir2_data_freeremove(hdr
, bf
, dfp
,
1135 xfs_dir2_data_freeinsert(hdr
, bf
, newdup
,
1137 xfs_dir2_data_freeinsert(hdr
, bf
, newdup2
,
1142 *needscanp
= needscan
;
1145 xfs_corruption_error(__func__
, XFS_ERRLEVEL_LOW
, args
->dp
->i_mount
,
1146 hdr
, sizeof(*hdr
), __FILE__
, __LINE__
, fa
);
1147 return -EFSCORRUPTED
;
1150 /* Find the end of the entry data in a data/block format dir block. */
1153 struct xfs_da_geometry
*geo
,
1154 struct xfs_dir2_data_hdr
*hdr
)
1156 switch (hdr
->magic
) {
1157 case cpu_to_be32(XFS_DIR3_BLOCK_MAGIC
):
1158 case cpu_to_be32(XFS_DIR2_BLOCK_MAGIC
):
1159 return xfs_dir2_block_leaf_p(xfs_dir2_block_tail_p(geo
, hdr
));
1160 case cpu_to_be32(XFS_DIR3_DATA_MAGIC
):
1161 case cpu_to_be32(XFS_DIR2_DATA_MAGIC
):
1162 return (char *)hdr
+ geo
->blksize
;