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
19 #include "libxfs_priv.h"
21 #include "xfs_format.h"
22 #include "xfs_log_format.h"
23 #include "xfs_trans_resv.h"
24 #include "xfs_mount.h"
25 #include "xfs_da_format.h"
26 #include "xfs_da_btree.h"
27 #include "xfs_inode.h"
29 #include "xfs_dir2_priv.h"
30 #include "xfs_trans.h"
31 #include "xfs_cksum.h"
33 static xfs_failaddr_t
xfs_dir2_data_freefind_verify(
34 struct xfs_dir2_data_hdr
*hdr
, struct xfs_dir2_data_free
*bf
,
35 struct xfs_dir2_data_unused
*dup
,
36 struct xfs_dir2_data_free
**bf_ent
);
39 * Check the consistency of the data block.
40 * The input can also be a block-format directory.
41 * Return NULL if the buffer is good, otherwise the address of the error.
44 __xfs_dir3_data_check(
45 struct xfs_inode
*dp
, /* incore inode pointer */
46 struct xfs_buf
*bp
) /* data block's buffer */
48 xfs_dir2_dataptr_t addr
; /* addr for leaf lookup */
49 xfs_dir2_data_free_t
*bf
; /* bestfree table */
50 xfs_dir2_block_tail_t
*btp
=NULL
; /* block tail */
51 int count
; /* count of entries found */
52 xfs_dir2_data_hdr_t
*hdr
; /* data block header */
53 xfs_dir2_data_entry_t
*dep
; /* data entry */
54 xfs_dir2_data_free_t
*dfp
; /* bestfree entry */
55 xfs_dir2_data_unused_t
*dup
; /* unused entry */
56 char *endp
; /* end of useful data */
57 int freeseen
; /* mask of bestfrees seen */
58 xfs_dahash_t hash
; /* hash of current name */
59 int i
; /* leaf index */
60 int lastfree
; /* last entry was unused */
61 xfs_dir2_leaf_entry_t
*lep
=NULL
; /* block leaf entries */
62 xfs_mount_t
*mp
; /* filesystem mount point */
63 char *p
; /* current data position */
64 int stale
; /* count of stale leaves */
66 const struct xfs_dir_ops
*ops
;
67 struct xfs_da_geometry
*geo
;
69 mp
= bp
->b_target
->bt_mount
;
73 * We can be passed a null dp here from a verifier, so we need to go the
74 * hard way to get them.
76 ops
= xfs_dir_get_ops(mp
, dp
);
79 * If this isn't a directory, or we don't get handed the dir ops,
80 * something is seriously wrong. Bail out.
82 if ((dp
&& !S_ISDIR(VFS_I(dp
)->i_mode
)) ||
83 ops
!= xfs_dir_get_ops(mp
, NULL
))
84 return __this_address
;
87 p
= (char *)ops
->data_entry_p(hdr
);
90 case cpu_to_be32(XFS_DIR3_BLOCK_MAGIC
):
91 case cpu_to_be32(XFS_DIR2_BLOCK_MAGIC
):
92 btp
= xfs_dir2_block_tail_p(geo
, hdr
);
93 lep
= xfs_dir2_block_leaf_p(btp
);
96 * The number of leaf entries is limited by the size of the
97 * block and the amount of space used by the data entries.
98 * We don't know how much space is used by the data entries yet,
99 * so just ensure that the count falls somewhere inside the
102 if (be32_to_cpu(btp
->count
) >=
103 ((char *)btp
- p
) / sizeof(struct xfs_dir2_leaf_entry
))
104 return __this_address
;
106 case cpu_to_be32(XFS_DIR3_DATA_MAGIC
):
107 case cpu_to_be32(XFS_DIR2_DATA_MAGIC
):
110 return __this_address
;
112 endp
= xfs_dir3_data_endp(geo
, hdr
);
114 return __this_address
;
117 * Account for zero bestfree entries.
119 bf
= ops
->data_bestfree_p(hdr
);
120 count
= lastfree
= freeseen
= 0;
123 return __this_address
;
128 return __this_address
;
133 return __this_address
;
137 if (be16_to_cpu(bf
[0].length
) < be16_to_cpu(bf
[1].length
))
138 return __this_address
;
139 if (be16_to_cpu(bf
[1].length
) < be16_to_cpu(bf
[2].length
))
140 return __this_address
;
142 * Loop over the data/unused entries.
145 dup
= (xfs_dir2_data_unused_t
*)p
;
147 * If it's unused, look for the space in the bestfree table.
148 * If we find it, account for that, else make sure it
149 * doesn't need to be there.
151 if (be16_to_cpu(dup
->freetag
) == XFS_DIR2_DATA_FREE_TAG
) {
155 return __this_address
;
156 if (endp
< p
+ be16_to_cpu(dup
->length
))
157 return __this_address
;
158 if (be16_to_cpu(*xfs_dir2_data_unused_tag_p(dup
)) !=
159 (char *)dup
- (char *)hdr
)
160 return __this_address
;
161 fa
= xfs_dir2_data_freefind_verify(hdr
, bf
, dup
, &dfp
);
166 if ((freeseen
& (1 << i
)) != 0)
167 return __this_address
;
170 if (be16_to_cpu(dup
->length
) >
171 be16_to_cpu(bf
[2].length
))
172 return __this_address
;
174 p
+= be16_to_cpu(dup
->length
);
179 * It's a real entry. Validate the fields.
180 * If this is a block directory then make sure it's
181 * in the leaf section of the block.
182 * The linear search is crude but this is DEBUG code.
184 dep
= (xfs_dir2_data_entry_t
*)p
;
185 if (dep
->namelen
== 0)
186 return __this_address
;
187 if (xfs_dir_ino_validate(mp
, be64_to_cpu(dep
->inumber
)))
188 return __this_address
;
189 if (endp
< p
+ ops
->data_entsize(dep
->namelen
))
190 return __this_address
;
191 if (be16_to_cpu(*ops
->data_entry_tag_p(dep
)) !=
192 (char *)dep
- (char *)hdr
)
193 return __this_address
;
194 if (ops
->data_get_ftype(dep
) >= XFS_DIR3_FT_MAX
)
195 return __this_address
;
198 if (hdr
->magic
== cpu_to_be32(XFS_DIR2_BLOCK_MAGIC
) ||
199 hdr
->magic
== cpu_to_be32(XFS_DIR3_BLOCK_MAGIC
)) {
200 addr
= xfs_dir2_db_off_to_dataptr(geo
, geo
->datablk
,
201 (xfs_dir2_data_aoff_t
)
202 ((char *)dep
- (char *)hdr
));
203 name
.name
= dep
->name
;
204 name
.len
= dep
->namelen
;
205 hash
= mp
->m_dirnameops
->hashname(&name
);
206 for (i
= 0; i
< be32_to_cpu(btp
->count
); i
++) {
207 if (be32_to_cpu(lep
[i
].address
) == addr
&&
208 be32_to_cpu(lep
[i
].hashval
) == hash
)
211 if (i
>= be32_to_cpu(btp
->count
))
212 return __this_address
;
214 p
+= ops
->data_entsize(dep
->namelen
);
217 * Need to have seen all the entries and all the bestfree slots.
220 return __this_address
;
221 if (hdr
->magic
== cpu_to_be32(XFS_DIR2_BLOCK_MAGIC
) ||
222 hdr
->magic
== cpu_to_be32(XFS_DIR3_BLOCK_MAGIC
)) {
223 for (i
= stale
= 0; i
< be32_to_cpu(btp
->count
); i
++) {
224 if (lep
[i
].address
==
225 cpu_to_be32(XFS_DIR2_NULL_DATAPTR
))
227 if (i
> 0 && be32_to_cpu(lep
[i
].hashval
) <
228 be32_to_cpu(lep
[i
- 1].hashval
))
229 return __this_address
;
231 if (count
!= be32_to_cpu(btp
->count
) - be32_to_cpu(btp
->stale
))
232 return __this_address
;
233 if (stale
!= be32_to_cpu(btp
->stale
))
234 return __this_address
;
242 struct xfs_inode
*dp
,
247 fa
= __xfs_dir3_data_check(dp
, bp
);
250 xfs_corruption_error(__func__
, XFS_ERRLEVEL_LOW
, dp
->i_mount
,
251 bp
->b_addr
, BBTOB(bp
->b_length
), __FILE__
, __LINE__
,
257 static xfs_failaddr_t
258 xfs_dir3_data_verify(
261 struct xfs_mount
*mp
= bp
->b_target
->bt_mount
;
262 struct xfs_dir3_blk_hdr
*hdr3
= bp
->b_addr
;
264 if (xfs_sb_version_hascrc(&mp
->m_sb
)) {
265 if (hdr3
->magic
!= cpu_to_be32(XFS_DIR3_DATA_MAGIC
))
266 return __this_address
;
267 if (!uuid_equal(&hdr3
->uuid
, &mp
->m_sb
.sb_meta_uuid
))
268 return __this_address
;
269 if (be64_to_cpu(hdr3
->blkno
) != bp
->b_bn
)
270 return __this_address
;
271 if (!xfs_log_check_lsn(mp
, be64_to_cpu(hdr3
->lsn
)))
272 return __this_address
;
274 if (hdr3
->magic
!= cpu_to_be32(XFS_DIR2_DATA_MAGIC
))
275 return __this_address
;
277 return __xfs_dir3_data_check(NULL
, bp
);
281 * Readahead of the first block of the directory when it is opened is completely
282 * oblivious to the format of the directory. Hence we can either get a block
283 * format buffer or a data format buffer on readahead.
286 xfs_dir3_data_reada_verify(
289 struct xfs_dir2_data_hdr
*hdr
= bp
->b_addr
;
291 switch (hdr
->magic
) {
292 case cpu_to_be32(XFS_DIR2_BLOCK_MAGIC
):
293 case cpu_to_be32(XFS_DIR3_BLOCK_MAGIC
):
294 bp
->b_ops
= &xfs_dir3_block_buf_ops
;
295 bp
->b_ops
->verify_read(bp
);
297 case cpu_to_be32(XFS_DIR2_DATA_MAGIC
):
298 case cpu_to_be32(XFS_DIR3_DATA_MAGIC
):
299 bp
->b_ops
= &xfs_dir3_data_buf_ops
;
300 bp
->b_ops
->verify_read(bp
);
303 xfs_verifier_error(bp
, -EFSCORRUPTED
, __this_address
);
309 xfs_dir3_data_read_verify(
312 struct xfs_mount
*mp
= bp
->b_target
->bt_mount
;
315 if (xfs_sb_version_hascrc(&mp
->m_sb
) &&
316 !xfs_buf_verify_cksum(bp
, XFS_DIR3_DATA_CRC_OFF
))
317 xfs_verifier_error(bp
, -EFSBADCRC
, __this_address
);
319 fa
= xfs_dir3_data_verify(bp
);
321 xfs_verifier_error(bp
, -EFSCORRUPTED
, fa
);
326 xfs_dir3_data_write_verify(
329 struct xfs_mount
*mp
= bp
->b_target
->bt_mount
;
330 struct xfs_buf_log_item
*bip
= bp
->b_log_item
;
331 struct xfs_dir3_blk_hdr
*hdr3
= bp
->b_addr
;
334 fa
= xfs_dir3_data_verify(bp
);
336 xfs_verifier_error(bp
, -EFSCORRUPTED
, fa
);
340 if (!xfs_sb_version_hascrc(&mp
->m_sb
))
344 hdr3
->lsn
= cpu_to_be64(bip
->bli_item
.li_lsn
);
346 xfs_buf_update_cksum(bp
, XFS_DIR3_DATA_CRC_OFF
);
349 const struct xfs_buf_ops xfs_dir3_data_buf_ops
= {
350 .name
= "xfs_dir3_data",
351 .verify_read
= xfs_dir3_data_read_verify
,
352 .verify_write
= xfs_dir3_data_write_verify
,
353 .verify_struct
= xfs_dir3_data_verify
,
356 static const struct xfs_buf_ops xfs_dir3_data_reada_buf_ops
= {
357 .name
= "xfs_dir3_data_reada",
358 .verify_read
= xfs_dir3_data_reada_verify
,
359 .verify_write
= xfs_dir3_data_write_verify
,
365 struct xfs_trans
*tp
,
366 struct xfs_inode
*dp
,
368 xfs_daddr_t mapped_bno
,
369 struct xfs_buf
**bpp
)
373 err
= xfs_da_read_buf(tp
, dp
, bno
, mapped_bno
, bpp
,
374 XFS_DATA_FORK
, &xfs_dir3_data_buf_ops
);
375 if (!err
&& tp
&& *bpp
)
376 xfs_trans_buf_set_type(tp
, *bpp
, XFS_BLFT_DIR_DATA_BUF
);
381 xfs_dir3_data_readahead(
382 struct xfs_inode
*dp
,
384 xfs_daddr_t mapped_bno
)
386 return xfs_da_reada_buf(dp
, bno
, mapped_bno
,
387 XFS_DATA_FORK
, &xfs_dir3_data_reada_buf_ops
);
391 * Find the bestfree entry that exactly coincides with unused directory space
392 * or a verifier error because the bestfree data are bad.
394 static xfs_failaddr_t
395 xfs_dir2_data_freefind_verify(
396 struct xfs_dir2_data_hdr
*hdr
,
397 struct xfs_dir2_data_free
*bf
,
398 struct xfs_dir2_data_unused
*dup
,
399 struct xfs_dir2_data_free
**bf_ent
)
401 struct xfs_dir2_data_free
*dfp
;
402 xfs_dir2_data_aoff_t off
;
403 bool matched
= false;
404 bool seenzero
= false;
407 off
= (xfs_dir2_data_aoff_t
)((char *)dup
- (char *)hdr
);
410 * Validate some consistency in the bestfree table.
411 * Check order, non-overlapping entries, and if we find the
412 * one we're looking for it has to be exact.
414 for (dfp
= &bf
[0]; dfp
< &bf
[XFS_DIR2_DATA_FD_COUNT
]; dfp
++) {
417 return __this_address
;
422 return __this_address
;
423 if (be16_to_cpu(dfp
->offset
) == off
) {
425 if (dfp
->length
!= dup
->length
)
426 return __this_address
;
427 } else if (be16_to_cpu(dfp
->offset
) > off
) {
428 if (off
+ be16_to_cpu(dup
->length
) >
429 be16_to_cpu(dfp
->offset
))
430 return __this_address
;
432 if (be16_to_cpu(dfp
->offset
) +
433 be16_to_cpu(dfp
->length
) > off
)
434 return __this_address
;
437 be16_to_cpu(dfp
->length
) < be16_to_cpu(dup
->length
))
438 return __this_address
;
440 be16_to_cpu(dfp
[-1].length
) < be16_to_cpu(dfp
[0].length
))
441 return __this_address
;
444 /* Looks ok so far; now try to match up with a bestfree entry. */
445 *bf_ent
= xfs_dir2_data_freefind(hdr
, bf
, dup
);
450 * Given a data block and an unused entry from that block,
451 * return the bestfree entry if any that corresponds to it.
453 xfs_dir2_data_free_t
*
454 xfs_dir2_data_freefind(
455 struct xfs_dir2_data_hdr
*hdr
, /* data block header */
456 struct xfs_dir2_data_free
*bf
, /* bestfree table pointer */
457 struct xfs_dir2_data_unused
*dup
) /* unused space */
459 xfs_dir2_data_free_t
*dfp
; /* bestfree entry */
460 xfs_dir2_data_aoff_t off
; /* offset value needed */
462 off
= (xfs_dir2_data_aoff_t
)((char *)dup
- (char *)hdr
);
465 * If this is smaller than the smallest bestfree entry,
466 * it can't be there since they're sorted.
468 if (be16_to_cpu(dup
->length
) <
469 be16_to_cpu(bf
[XFS_DIR2_DATA_FD_COUNT
- 1].length
))
472 * Look at the three bestfree entries for our guy.
474 for (dfp
= &bf
[0]; dfp
< &bf
[XFS_DIR2_DATA_FD_COUNT
]; dfp
++) {
477 if (be16_to_cpu(dfp
->offset
) == off
)
481 * Didn't find it. This only happens if there are duplicate lengths.
487 * Insert an unused-space entry into the bestfree table.
489 xfs_dir2_data_free_t
* /* entry inserted */
490 xfs_dir2_data_freeinsert(
491 struct xfs_dir2_data_hdr
*hdr
, /* data block pointer */
492 struct xfs_dir2_data_free
*dfp
, /* bestfree table pointer */
493 struct xfs_dir2_data_unused
*dup
, /* unused space */
494 int *loghead
) /* log the data header (out) */
496 xfs_dir2_data_free_t
new; /* new bestfree entry */
498 ASSERT(hdr
->magic
== cpu_to_be32(XFS_DIR2_DATA_MAGIC
) ||
499 hdr
->magic
== cpu_to_be32(XFS_DIR2_BLOCK_MAGIC
) ||
500 hdr
->magic
== cpu_to_be32(XFS_DIR3_DATA_MAGIC
) ||
501 hdr
->magic
== cpu_to_be32(XFS_DIR3_BLOCK_MAGIC
));
503 new.length
= dup
->length
;
504 new.offset
= cpu_to_be16((char *)dup
- (char *)hdr
);
507 * Insert at position 0, 1, or 2; or not at all.
509 if (be16_to_cpu(new.length
) > be16_to_cpu(dfp
[0].length
)) {
516 if (be16_to_cpu(new.length
) > be16_to_cpu(dfp
[1].length
)) {
522 if (be16_to_cpu(new.length
) > be16_to_cpu(dfp
[2].length
)) {
531 * Remove a bestfree entry from the table.
534 xfs_dir2_data_freeremove(
535 struct xfs_dir2_data_hdr
*hdr
, /* data block header */
536 struct xfs_dir2_data_free
*bf
, /* bestfree table pointer */
537 struct xfs_dir2_data_free
*dfp
, /* bestfree entry pointer */
538 int *loghead
) /* out: log data header */
541 ASSERT(hdr
->magic
== cpu_to_be32(XFS_DIR2_DATA_MAGIC
) ||
542 hdr
->magic
== cpu_to_be32(XFS_DIR2_BLOCK_MAGIC
) ||
543 hdr
->magic
== cpu_to_be32(XFS_DIR3_DATA_MAGIC
) ||
544 hdr
->magic
== cpu_to_be32(XFS_DIR3_BLOCK_MAGIC
));
547 * It's the first entry, slide the next 2 up.
554 * It's the second entry, slide the 3rd entry up.
556 else if (dfp
== &bf
[1])
559 * Must be the last entry.
562 ASSERT(dfp
== &bf
[2]);
564 * Clear the 3rd entry, must be zero now.
572 * Given a data block, reconstruct its bestfree map.
575 xfs_dir2_data_freescan_int(
576 struct xfs_da_geometry
*geo
,
577 const struct xfs_dir_ops
*ops
,
578 struct xfs_dir2_data_hdr
*hdr
,
581 xfs_dir2_data_entry_t
*dep
; /* active data entry */
582 xfs_dir2_data_unused_t
*dup
; /* unused data entry */
583 struct xfs_dir2_data_free
*bf
;
584 char *endp
; /* end of block's data */
585 char *p
; /* current entry pointer */
587 ASSERT(hdr
->magic
== cpu_to_be32(XFS_DIR2_DATA_MAGIC
) ||
588 hdr
->magic
== cpu_to_be32(XFS_DIR3_DATA_MAGIC
) ||
589 hdr
->magic
== cpu_to_be32(XFS_DIR2_BLOCK_MAGIC
) ||
590 hdr
->magic
== cpu_to_be32(XFS_DIR3_BLOCK_MAGIC
));
593 * Start by clearing the table.
595 bf
= ops
->data_bestfree_p(hdr
);
596 memset(bf
, 0, sizeof(*bf
) * XFS_DIR2_DATA_FD_COUNT
);
601 p
= (char *)ops
->data_entry_p(hdr
);
602 endp
= xfs_dir3_data_endp(geo
, hdr
);
604 * Loop over the block's entries.
607 dup
= (xfs_dir2_data_unused_t
*)p
;
609 * If it's a free entry, insert it.
611 if (be16_to_cpu(dup
->freetag
) == XFS_DIR2_DATA_FREE_TAG
) {
612 ASSERT((char *)dup
- (char *)hdr
==
613 be16_to_cpu(*xfs_dir2_data_unused_tag_p(dup
)));
614 xfs_dir2_data_freeinsert(hdr
, bf
, dup
, loghead
);
615 p
+= be16_to_cpu(dup
->length
);
618 * For active entries, check their tags and skip them.
621 dep
= (xfs_dir2_data_entry_t
*)p
;
622 ASSERT((char *)dep
- (char *)hdr
==
623 be16_to_cpu(*ops
->data_entry_tag_p(dep
)));
624 p
+= ops
->data_entsize(dep
->namelen
);
630 xfs_dir2_data_freescan(
631 struct xfs_inode
*dp
,
632 struct xfs_dir2_data_hdr
*hdr
,
635 return xfs_dir2_data_freescan_int(dp
->i_mount
->m_dir_geo
, dp
->d_ops
,
640 * Initialize a data block at the given block number in the directory.
641 * Give back the buffer for the created block.
645 xfs_da_args_t
*args
, /* directory operation args */
646 xfs_dir2_db_t blkno
, /* logical dir block number */
647 struct xfs_buf
**bpp
) /* output block buffer */
649 struct xfs_buf
*bp
; /* block buffer */
650 xfs_dir2_data_hdr_t
*hdr
; /* data block header */
651 xfs_inode_t
*dp
; /* incore directory inode */
652 xfs_dir2_data_unused_t
*dup
; /* unused entry pointer */
653 struct xfs_dir2_data_free
*bf
;
654 int error
; /* error return value */
655 int i
; /* bestfree index */
656 xfs_mount_t
*mp
; /* filesystem mount point */
657 xfs_trans_t
*tp
; /* transaction pointer */
664 * Get the buffer set up for the block.
666 error
= xfs_da_get_buf(tp
, dp
, xfs_dir2_db_to_da(args
->geo
, blkno
),
667 -1, &bp
, XFS_DATA_FORK
);
670 bp
->b_ops
= &xfs_dir3_data_buf_ops
;
671 xfs_trans_buf_set_type(tp
, bp
, XFS_BLFT_DIR_DATA_BUF
);
674 * Initialize the header.
677 if (xfs_sb_version_hascrc(&mp
->m_sb
)) {
678 struct xfs_dir3_blk_hdr
*hdr3
= bp
->b_addr
;
680 memset(hdr3
, 0, sizeof(*hdr3
));
681 hdr3
->magic
= cpu_to_be32(XFS_DIR3_DATA_MAGIC
);
682 hdr3
->blkno
= cpu_to_be64(bp
->b_bn
);
683 hdr3
->owner
= cpu_to_be64(dp
->i_ino
);
684 uuid_copy(&hdr3
->uuid
, &mp
->m_sb
.sb_meta_uuid
);
687 hdr
->magic
= cpu_to_be32(XFS_DIR2_DATA_MAGIC
);
689 bf
= dp
->d_ops
->data_bestfree_p(hdr
);
690 bf
[0].offset
= cpu_to_be16(dp
->d_ops
->data_entry_offset
);
691 for (i
= 1; i
< XFS_DIR2_DATA_FD_COUNT
; i
++) {
697 * Set up an unused entry for the block's body.
699 dup
= dp
->d_ops
->data_unused_p(hdr
);
700 dup
->freetag
= cpu_to_be16(XFS_DIR2_DATA_FREE_TAG
);
702 t
= args
->geo
->blksize
- (uint
)dp
->d_ops
->data_entry_offset
;
703 bf
[0].length
= cpu_to_be16(t
);
704 dup
->length
= cpu_to_be16(t
);
705 *xfs_dir2_data_unused_tag_p(dup
) = cpu_to_be16((char *)dup
- (char *)hdr
);
707 * Log it and return it.
709 xfs_dir2_data_log_header(args
, bp
);
710 xfs_dir2_data_log_unused(args
, bp
, dup
);
716 * Log an active data entry from the block.
719 xfs_dir2_data_log_entry(
720 struct xfs_da_args
*args
,
722 xfs_dir2_data_entry_t
*dep
) /* data entry pointer */
724 struct xfs_dir2_data_hdr
*hdr
= bp
->b_addr
;
726 ASSERT(hdr
->magic
== cpu_to_be32(XFS_DIR2_DATA_MAGIC
) ||
727 hdr
->magic
== cpu_to_be32(XFS_DIR3_DATA_MAGIC
) ||
728 hdr
->magic
== cpu_to_be32(XFS_DIR2_BLOCK_MAGIC
) ||
729 hdr
->magic
== cpu_to_be32(XFS_DIR3_BLOCK_MAGIC
));
731 xfs_trans_log_buf(args
->trans
, bp
, (uint
)((char *)dep
- (char *)hdr
),
732 (uint
)((char *)(args
->dp
->d_ops
->data_entry_tag_p(dep
) + 1) -
737 * Log a data block header.
740 xfs_dir2_data_log_header(
741 struct xfs_da_args
*args
,
745 struct xfs_dir2_data_hdr
*hdr
= bp
->b_addr
;
747 ASSERT(hdr
->magic
== cpu_to_be32(XFS_DIR2_DATA_MAGIC
) ||
748 hdr
->magic
== cpu_to_be32(XFS_DIR3_DATA_MAGIC
) ||
749 hdr
->magic
== cpu_to_be32(XFS_DIR2_BLOCK_MAGIC
) ||
750 hdr
->magic
== cpu_to_be32(XFS_DIR3_BLOCK_MAGIC
));
753 xfs_trans_log_buf(args
->trans
, bp
, 0,
754 args
->dp
->d_ops
->data_entry_offset
- 1);
758 * Log a data unused entry.
761 xfs_dir2_data_log_unused(
762 struct xfs_da_args
*args
,
764 xfs_dir2_data_unused_t
*dup
) /* data unused pointer */
766 xfs_dir2_data_hdr_t
*hdr
= bp
->b_addr
;
768 ASSERT(hdr
->magic
== cpu_to_be32(XFS_DIR2_DATA_MAGIC
) ||
769 hdr
->magic
== cpu_to_be32(XFS_DIR3_DATA_MAGIC
) ||
770 hdr
->magic
== cpu_to_be32(XFS_DIR2_BLOCK_MAGIC
) ||
771 hdr
->magic
== cpu_to_be32(XFS_DIR3_BLOCK_MAGIC
));
774 * Log the first part of the unused entry.
776 xfs_trans_log_buf(args
->trans
, bp
, (uint
)((char *)dup
- (char *)hdr
),
777 (uint
)((char *)&dup
->length
+ sizeof(dup
->length
) -
780 * Log the end (tag) of the unused entry.
782 xfs_trans_log_buf(args
->trans
, bp
,
783 (uint
)((char *)xfs_dir2_data_unused_tag_p(dup
) - (char *)hdr
),
784 (uint
)((char *)xfs_dir2_data_unused_tag_p(dup
) - (char *)hdr
+
785 sizeof(xfs_dir2_data_off_t
) - 1));
789 * Make a byte range in the data block unused.
790 * Its current contents are unimportant.
793 xfs_dir2_data_make_free(
794 struct xfs_da_args
*args
,
796 xfs_dir2_data_aoff_t offset
, /* starting byte offset */
797 xfs_dir2_data_aoff_t len
, /* length in bytes */
798 int *needlogp
, /* out: log header */
799 int *needscanp
) /* out: regen bestfree */
801 xfs_dir2_data_hdr_t
*hdr
; /* data block pointer */
802 xfs_dir2_data_free_t
*dfp
; /* bestfree pointer */
803 char *endptr
; /* end of data area */
804 int needscan
; /* need to regen bestfree */
805 xfs_dir2_data_unused_t
*newdup
; /* new unused entry */
806 xfs_dir2_data_unused_t
*postdup
; /* unused entry after us */
807 xfs_dir2_data_unused_t
*prevdup
; /* unused entry before us */
808 struct xfs_dir2_data_free
*bf
;
813 * Figure out where the end of the data area is.
815 endptr
= xfs_dir3_data_endp(args
->geo
, hdr
);
816 ASSERT(endptr
!= NULL
);
819 * If this isn't the start of the block, then back up to
820 * the previous entry and see if it's free.
822 if (offset
> args
->dp
->d_ops
->data_entry_offset
) {
823 __be16
*tagp
; /* tag just before us */
825 tagp
= (__be16
*)((char *)hdr
+ offset
) - 1;
826 prevdup
= (xfs_dir2_data_unused_t
*)((char *)hdr
+ be16_to_cpu(*tagp
));
827 if (be16_to_cpu(prevdup
->freetag
) != XFS_DIR2_DATA_FREE_TAG
)
832 * If this isn't the end of the block, see if the entry after
835 if ((char *)hdr
+ offset
+ len
< endptr
) {
837 (xfs_dir2_data_unused_t
*)((char *)hdr
+ offset
+ len
);
838 if (be16_to_cpu(postdup
->freetag
) != XFS_DIR2_DATA_FREE_TAG
)
842 ASSERT(*needscanp
== 0);
845 * Previous and following entries are both free,
846 * merge everything into a single free entry.
848 bf
= args
->dp
->d_ops
->data_bestfree_p(hdr
);
849 if (prevdup
&& postdup
) {
850 xfs_dir2_data_free_t
*dfp2
; /* another bestfree pointer */
853 * See if prevdup and/or postdup are in bestfree table.
855 dfp
= xfs_dir2_data_freefind(hdr
, bf
, prevdup
);
856 dfp2
= xfs_dir2_data_freefind(hdr
, bf
, postdup
);
858 * We need a rescan unless there are exactly 2 free entries
859 * namely our two. Then we know what's happening, otherwise
860 * since the third bestfree is there, there might be more
863 needscan
= (bf
[2].length
!= 0);
865 * Fix up the new big freespace.
867 be16_add_cpu(&prevdup
->length
, len
+ be16_to_cpu(postdup
->length
));
868 *xfs_dir2_data_unused_tag_p(prevdup
) =
869 cpu_to_be16((char *)prevdup
- (char *)hdr
);
870 xfs_dir2_data_log_unused(args
, bp
, prevdup
);
873 * Has to be the case that entries 0 and 1 are
874 * dfp and dfp2 (don't know which is which), and
876 * Remove entry 1 first then entry 0.
884 xfs_dir2_data_freeremove(hdr
, bf
, dfp2
, needlogp
);
885 xfs_dir2_data_freeremove(hdr
, bf
, dfp
, needlogp
);
887 * Now insert the new entry.
889 dfp
= xfs_dir2_data_freeinsert(hdr
, bf
, prevdup
,
891 ASSERT(dfp
== &bf
[0]);
892 ASSERT(dfp
->length
== prevdup
->length
);
893 ASSERT(!dfp
[1].length
);
894 ASSERT(!dfp
[2].length
);
898 * The entry before us is free, merge with it.
901 dfp
= xfs_dir2_data_freefind(hdr
, bf
, prevdup
);
902 be16_add_cpu(&prevdup
->length
, len
);
903 *xfs_dir2_data_unused_tag_p(prevdup
) =
904 cpu_to_be16((char *)prevdup
- (char *)hdr
);
905 xfs_dir2_data_log_unused(args
, bp
, prevdup
);
907 * If the previous entry was in the table, the new entry
908 * is longer, so it will be in the table too. Remove
909 * the old one and add the new one.
912 xfs_dir2_data_freeremove(hdr
, bf
, dfp
, needlogp
);
913 xfs_dir2_data_freeinsert(hdr
, bf
, prevdup
, needlogp
);
916 * Otherwise we need a scan if the new entry is big enough.
919 needscan
= be16_to_cpu(prevdup
->length
) >
920 be16_to_cpu(bf
[2].length
);
924 * The following entry is free, merge with it.
927 dfp
= xfs_dir2_data_freefind(hdr
, bf
, postdup
);
928 newdup
= (xfs_dir2_data_unused_t
*)((char *)hdr
+ offset
);
929 newdup
->freetag
= cpu_to_be16(XFS_DIR2_DATA_FREE_TAG
);
930 newdup
->length
= cpu_to_be16(len
+ be16_to_cpu(postdup
->length
));
931 *xfs_dir2_data_unused_tag_p(newdup
) =
932 cpu_to_be16((char *)newdup
- (char *)hdr
);
933 xfs_dir2_data_log_unused(args
, bp
, newdup
);
935 * If the following entry was in the table, the new entry
936 * is longer, so it will be in the table too. Remove
937 * the old one and add the new one.
940 xfs_dir2_data_freeremove(hdr
, bf
, dfp
, needlogp
);
941 xfs_dir2_data_freeinsert(hdr
, bf
, newdup
, needlogp
);
944 * Otherwise we need a scan if the new entry is big enough.
947 needscan
= be16_to_cpu(newdup
->length
) >
948 be16_to_cpu(bf
[2].length
);
952 * Neither neighbor is free. Make a new entry.
955 newdup
= (xfs_dir2_data_unused_t
*)((char *)hdr
+ offset
);
956 newdup
->freetag
= cpu_to_be16(XFS_DIR2_DATA_FREE_TAG
);
957 newdup
->length
= cpu_to_be16(len
);
958 *xfs_dir2_data_unused_tag_p(newdup
) =
959 cpu_to_be16((char *)newdup
- (char *)hdr
);
960 xfs_dir2_data_log_unused(args
, bp
, newdup
);
961 xfs_dir2_data_freeinsert(hdr
, bf
, newdup
, needlogp
);
963 *needscanp
= needscan
;
966 /* Check our free data for obvious signs of corruption. */
967 static inline xfs_failaddr_t
968 xfs_dir2_data_check_free(
969 struct xfs_dir2_data_hdr
*hdr
,
970 struct xfs_dir2_data_unused
*dup
,
971 xfs_dir2_data_aoff_t offset
,
972 xfs_dir2_data_aoff_t len
)
974 if (hdr
->magic
!= cpu_to_be32(XFS_DIR2_DATA_MAGIC
) &&
975 hdr
->magic
!= cpu_to_be32(XFS_DIR3_DATA_MAGIC
) &&
976 hdr
->magic
!= cpu_to_be32(XFS_DIR2_BLOCK_MAGIC
) &&
977 hdr
->magic
!= cpu_to_be32(XFS_DIR3_BLOCK_MAGIC
))
978 return __this_address
;
979 if (be16_to_cpu(dup
->freetag
) != XFS_DIR2_DATA_FREE_TAG
)
980 return __this_address
;
981 if (offset
< (char *)dup
- (char *)hdr
)
982 return __this_address
;
983 if (offset
+ len
> (char *)dup
+ be16_to_cpu(dup
->length
) - (char *)hdr
)
984 return __this_address
;
985 if ((char *)dup
- (char *)hdr
!=
986 be16_to_cpu(*xfs_dir2_data_unused_tag_p(dup
)))
987 return __this_address
;
991 /* Sanity-check a new bestfree entry. */
992 static inline xfs_failaddr_t
993 xfs_dir2_data_check_new_free(
994 struct xfs_dir2_data_hdr
*hdr
,
995 struct xfs_dir2_data_free
*dfp
,
996 struct xfs_dir2_data_unused
*newdup
)
999 return __this_address
;
1000 if (dfp
->length
!= newdup
->length
)
1001 return __this_address
;
1002 if (be16_to_cpu(dfp
->offset
) != (char *)newdup
- (char *)hdr
)
1003 return __this_address
;
1008 * Take a byte range out of an existing unused space and make it un-free.
1011 xfs_dir2_data_use_free(
1012 struct xfs_da_args
*args
,
1014 xfs_dir2_data_unused_t
*dup
, /* unused entry */
1015 xfs_dir2_data_aoff_t offset
, /* starting offset to use */
1016 xfs_dir2_data_aoff_t len
, /* length to use */
1017 int *needlogp
, /* out: need to log header */
1018 int *needscanp
) /* out: need regen bestfree */
1020 xfs_dir2_data_hdr_t
*hdr
; /* data block header */
1021 xfs_dir2_data_free_t
*dfp
; /* bestfree pointer */
1022 xfs_dir2_data_unused_t
*newdup
; /* new unused entry */
1023 xfs_dir2_data_unused_t
*newdup2
; /* another new unused entry */
1024 struct xfs_dir2_data_free
*bf
;
1026 int matchback
; /* matches end of freespace */
1027 int matchfront
; /* matches start of freespace */
1028 int needscan
; /* need to regen bestfree */
1029 int oldlen
; /* old unused entry's length */
1032 fa
= xfs_dir2_data_check_free(hdr
, dup
, offset
, len
);
1036 * Look up the entry in the bestfree table.
1038 oldlen
= be16_to_cpu(dup
->length
);
1039 bf
= args
->dp
->d_ops
->data_bestfree_p(hdr
);
1040 dfp
= xfs_dir2_data_freefind(hdr
, bf
, dup
);
1041 ASSERT(dfp
|| oldlen
<= be16_to_cpu(bf
[2].length
));
1043 * Check for alignment with front and back of the entry.
1045 matchfront
= (char *)dup
- (char *)hdr
== offset
;
1046 matchback
= (char *)dup
+ oldlen
- (char *)hdr
== offset
+ len
;
1047 ASSERT(*needscanp
== 0);
1050 * If we matched it exactly we just need to get rid of it from
1051 * the bestfree table.
1053 if (matchfront
&& matchback
) {
1055 needscan
= (bf
[2].offset
!= 0);
1057 xfs_dir2_data_freeremove(hdr
, bf
, dfp
,
1062 * We match the first part of the entry.
1063 * Make a new entry with the remaining freespace.
1065 else if (matchfront
) {
1066 newdup
= (xfs_dir2_data_unused_t
*)((char *)hdr
+ offset
+ len
);
1067 newdup
->freetag
= cpu_to_be16(XFS_DIR2_DATA_FREE_TAG
);
1068 newdup
->length
= cpu_to_be16(oldlen
- len
);
1069 *xfs_dir2_data_unused_tag_p(newdup
) =
1070 cpu_to_be16((char *)newdup
- (char *)hdr
);
1071 xfs_dir2_data_log_unused(args
, bp
, newdup
);
1073 * If it was in the table, remove it and add the new one.
1076 xfs_dir2_data_freeremove(hdr
, bf
, dfp
, needlogp
);
1077 dfp
= xfs_dir2_data_freeinsert(hdr
, bf
, newdup
,
1079 fa
= xfs_dir2_data_check_new_free(hdr
, dfp
, newdup
);
1083 * If we got inserted at the last slot,
1084 * that means we don't know if there was a better
1085 * choice for the last slot, or not. Rescan.
1087 needscan
= dfp
== &bf
[2];
1091 * We match the last part of the entry.
1092 * Trim the allocated space off the tail of the entry.
1094 else if (matchback
) {
1096 newdup
->length
= cpu_to_be16(((char *)hdr
+ offset
) - (char *)newdup
);
1097 *xfs_dir2_data_unused_tag_p(newdup
) =
1098 cpu_to_be16((char *)newdup
- (char *)hdr
);
1099 xfs_dir2_data_log_unused(args
, bp
, newdup
);
1101 * If it was in the table, remove it and add the new one.
1104 xfs_dir2_data_freeremove(hdr
, bf
, dfp
, needlogp
);
1105 dfp
= xfs_dir2_data_freeinsert(hdr
, bf
, newdup
,
1107 fa
= xfs_dir2_data_check_new_free(hdr
, dfp
, newdup
);
1111 * If we got inserted at the last slot,
1112 * that means we don't know if there was a better
1113 * choice for the last slot, or not. Rescan.
1115 needscan
= dfp
== &bf
[2];
1119 * Poking out the middle of an entry.
1120 * Make two new entries.
1124 newdup
->length
= cpu_to_be16(((char *)hdr
+ offset
) - (char *)newdup
);
1125 *xfs_dir2_data_unused_tag_p(newdup
) =
1126 cpu_to_be16((char *)newdup
- (char *)hdr
);
1127 xfs_dir2_data_log_unused(args
, bp
, newdup
);
1128 newdup2
= (xfs_dir2_data_unused_t
*)((char *)hdr
+ offset
+ len
);
1129 newdup2
->freetag
= cpu_to_be16(XFS_DIR2_DATA_FREE_TAG
);
1130 newdup2
->length
= cpu_to_be16(oldlen
- len
- be16_to_cpu(newdup
->length
));
1131 *xfs_dir2_data_unused_tag_p(newdup2
) =
1132 cpu_to_be16((char *)newdup2
- (char *)hdr
);
1133 xfs_dir2_data_log_unused(args
, bp
, newdup2
);
1135 * If the old entry was in the table, we need to scan
1136 * if the 3rd entry was valid, since these entries
1137 * are smaller than the old one.
1138 * If we don't need to scan that means there were 1 or 2
1139 * entries in the table, and removing the old and adding
1140 * the 2 new will work.
1143 needscan
= (bf
[2].length
!= 0);
1145 xfs_dir2_data_freeremove(hdr
, bf
, dfp
,
1147 xfs_dir2_data_freeinsert(hdr
, bf
, newdup
,
1149 xfs_dir2_data_freeinsert(hdr
, bf
, newdup2
,
1154 *needscanp
= needscan
;
1157 xfs_corruption_error(__func__
, XFS_ERRLEVEL_LOW
, args
->dp
->i_mount
,
1158 hdr
, sizeof(*hdr
), __FILE__
, __LINE__
, fa
);
1159 return -EFSCORRUPTED
;
1162 /* Find the end of the entry data in a data/block format dir block. */
1165 struct xfs_da_geometry
*geo
,
1166 struct xfs_dir2_data_hdr
*hdr
)
1168 switch (hdr
->magic
) {
1169 case cpu_to_be32(XFS_DIR3_BLOCK_MAGIC
):
1170 case cpu_to_be32(XFS_DIR2_BLOCK_MAGIC
):
1171 return xfs_dir2_block_leaf_p(xfs_dir2_block_tail_p(geo
, hdr
));
1172 case cpu_to_be32(XFS_DIR3_DATA_MAGIC
):
1173 case cpu_to_be32(XFS_DIR2_DATA_MAGIC
):
1174 return (char *)hdr
+ geo
->blksize
;