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"
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_error.h"
31 #include "xfs_trans.h"
32 #include "xfs_buf_item.h"
33 #include "xfs_cksum.h"
37 * Check the consistency of the data block.
38 * The input can also be a block-format directory.
39 * Return 0 is the buffer is good, otherwise an error.
42 __xfs_dir3_data_check(
43 struct xfs_inode
*dp
, /* incore inode pointer */
44 struct xfs_buf
*bp
) /* data block's buffer */
46 xfs_dir2_dataptr_t addr
; /* addr for leaf lookup */
47 xfs_dir2_data_free_t
*bf
; /* bestfree table */
48 xfs_dir2_block_tail_t
*btp
=NULL
; /* block tail */
49 int count
; /* count of entries found */
50 xfs_dir2_data_hdr_t
*hdr
; /* data block header */
51 xfs_dir2_data_entry_t
*dep
; /* data entry */
52 xfs_dir2_data_free_t
*dfp
; /* bestfree entry */
53 xfs_dir2_data_unused_t
*dup
; /* unused entry */
54 char *endp
; /* end of useful data */
55 int freeseen
; /* mask of bestfrees seen */
56 xfs_dahash_t hash
; /* hash of current name */
57 int i
; /* leaf index */
58 int lastfree
; /* last entry was unused */
59 xfs_dir2_leaf_entry_t
*lep
=NULL
; /* block leaf entries */
60 xfs_mount_t
*mp
; /* filesystem mount point */
61 char *p
; /* current data position */
62 int stale
; /* count of stale leaves */
64 const struct xfs_dir_ops
*ops
;
65 struct xfs_da_geometry
*geo
;
67 mp
= bp
->b_target
->bt_mount
;
71 * We can be passed a null dp here from a verifier, so we need to go the
72 * hard way to get them.
74 ops
= xfs_dir_get_ops(mp
, dp
);
77 p
= (char *)ops
->data_entry_p(hdr
);
80 case cpu_to_be32(XFS_DIR3_BLOCK_MAGIC
):
81 case cpu_to_be32(XFS_DIR2_BLOCK_MAGIC
):
82 btp
= xfs_dir2_block_tail_p(geo
, hdr
);
83 lep
= xfs_dir2_block_leaf_p(btp
);
87 * The number of leaf entries is limited by the size of the
88 * block and the amount of space used by the data entries.
89 * We don't know how much space is used by the data entries yet,
90 * so just ensure that the count falls somewhere inside the
93 XFS_WANT_CORRUPTED_RETURN(mp
, be32_to_cpu(btp
->count
) <
94 ((char *)btp
- p
) / sizeof(struct xfs_dir2_leaf_entry
));
96 case cpu_to_be32(XFS_DIR3_DATA_MAGIC
):
97 case cpu_to_be32(XFS_DIR2_DATA_MAGIC
):
98 endp
= (char *)hdr
+ geo
->blksize
;
101 XFS_ERROR_REPORT("Bad Magic", XFS_ERRLEVEL_LOW
, mp
);
102 return -EFSCORRUPTED
;
106 * Account for zero bestfree entries.
108 bf
= ops
->data_bestfree_p(hdr
);
109 count
= lastfree
= freeseen
= 0;
111 XFS_WANT_CORRUPTED_RETURN(mp
, !bf
[0].offset
);
115 XFS_WANT_CORRUPTED_RETURN(mp
, !bf
[1].offset
);
119 XFS_WANT_CORRUPTED_RETURN(mp
, !bf
[2].offset
);
123 XFS_WANT_CORRUPTED_RETURN(mp
, be16_to_cpu(bf
[0].length
) >=
124 be16_to_cpu(bf
[1].length
));
125 XFS_WANT_CORRUPTED_RETURN(mp
, be16_to_cpu(bf
[1].length
) >=
126 be16_to_cpu(bf
[2].length
));
128 * Loop over the data/unused entries.
131 dup
= (xfs_dir2_data_unused_t
*)p
;
133 * If it's unused, look for the space in the bestfree table.
134 * If we find it, account for that, else make sure it
135 * doesn't need to be there.
137 if (be16_to_cpu(dup
->freetag
) == XFS_DIR2_DATA_FREE_TAG
) {
138 XFS_WANT_CORRUPTED_RETURN(mp
, lastfree
== 0);
139 XFS_WANT_CORRUPTED_RETURN(mp
,
140 be16_to_cpu(*xfs_dir2_data_unused_tag_p(dup
)) ==
141 (char *)dup
- (char *)hdr
);
142 dfp
= xfs_dir2_data_freefind(hdr
, bf
, dup
);
145 XFS_WANT_CORRUPTED_RETURN(mp
,
146 (freeseen
& (1 << i
)) == 0);
149 XFS_WANT_CORRUPTED_RETURN(mp
,
150 be16_to_cpu(dup
->length
) <=
151 be16_to_cpu(bf
[2].length
));
153 p
+= be16_to_cpu(dup
->length
);
158 * It's a real entry. Validate the fields.
159 * If this is a block directory then make sure it's
160 * in the leaf section of the block.
161 * The linear search is crude but this is DEBUG code.
163 dep
= (xfs_dir2_data_entry_t
*)p
;
164 XFS_WANT_CORRUPTED_RETURN(mp
, dep
->namelen
!= 0);
165 XFS_WANT_CORRUPTED_RETURN(mp
,
166 !xfs_dir_ino_validate(mp
, be64_to_cpu(dep
->inumber
)));
167 XFS_WANT_CORRUPTED_RETURN(mp
,
168 be16_to_cpu(*ops
->data_entry_tag_p(dep
)) ==
169 (char *)dep
- (char *)hdr
);
170 XFS_WANT_CORRUPTED_RETURN(mp
,
171 ops
->data_get_ftype(dep
) < XFS_DIR3_FT_MAX
);
174 if (hdr
->magic
== cpu_to_be32(XFS_DIR2_BLOCK_MAGIC
) ||
175 hdr
->magic
== cpu_to_be32(XFS_DIR3_BLOCK_MAGIC
)) {
176 addr
= xfs_dir2_db_off_to_dataptr(geo
, geo
->datablk
,
177 (xfs_dir2_data_aoff_t
)
178 ((char *)dep
- (char *)hdr
));
179 name
.name
= dep
->name
;
180 name
.len
= dep
->namelen
;
181 hash
= mp
->m_dirnameops
->hashname(&name
);
182 for (i
= 0; i
< be32_to_cpu(btp
->count
); i
++) {
183 if (be32_to_cpu(lep
[i
].address
) == addr
&&
184 be32_to_cpu(lep
[i
].hashval
) == hash
)
187 XFS_WANT_CORRUPTED_RETURN(mp
,
188 i
< be32_to_cpu(btp
->count
));
190 p
+= ops
->data_entsize(dep
->namelen
);
193 * Need to have seen all the entries and all the bestfree slots.
195 XFS_WANT_CORRUPTED_RETURN(mp
, freeseen
== 7);
196 if (hdr
->magic
== cpu_to_be32(XFS_DIR2_BLOCK_MAGIC
) ||
197 hdr
->magic
== cpu_to_be32(XFS_DIR3_BLOCK_MAGIC
)) {
198 for (i
= stale
= 0; i
< be32_to_cpu(btp
->count
); i
++) {
199 if (lep
[i
].address
==
200 cpu_to_be32(XFS_DIR2_NULL_DATAPTR
))
203 XFS_WANT_CORRUPTED_RETURN(mp
,
204 be32_to_cpu(lep
[i
].hashval
) >=
205 be32_to_cpu(lep
[i
- 1].hashval
));
207 XFS_WANT_CORRUPTED_RETURN(mp
, count
==
208 be32_to_cpu(btp
->count
) - be32_to_cpu(btp
->stale
));
209 XFS_WANT_CORRUPTED_RETURN(mp
, stale
== be32_to_cpu(btp
->stale
));
215 xfs_dir3_data_verify(
218 struct xfs_mount
*mp
= bp
->b_target
->bt_mount
;
219 struct xfs_dir3_blk_hdr
*hdr3
= bp
->b_addr
;
221 if (xfs_sb_version_hascrc(&mp
->m_sb
)) {
222 if (hdr3
->magic
!= cpu_to_be32(XFS_DIR3_DATA_MAGIC
))
224 if (!uuid_equal(&hdr3
->uuid
, &mp
->m_sb
.sb_meta_uuid
))
226 if (be64_to_cpu(hdr3
->blkno
) != bp
->b_bn
)
228 if (!xfs_log_check_lsn(mp
, be64_to_cpu(hdr3
->lsn
)))
231 if (hdr3
->magic
!= cpu_to_be32(XFS_DIR2_DATA_MAGIC
))
234 if (__xfs_dir3_data_check(NULL
, bp
))
240 * Readahead of the first block of the directory when it is opened is completely
241 * oblivious to the format of the directory. Hence we can either get a block
242 * format buffer or a data format buffer on readahead.
245 xfs_dir3_data_reada_verify(
248 struct xfs_dir2_data_hdr
*hdr
= bp
->b_addr
;
250 switch (hdr
->magic
) {
251 case cpu_to_be32(XFS_DIR2_BLOCK_MAGIC
):
252 case cpu_to_be32(XFS_DIR3_BLOCK_MAGIC
):
253 bp
->b_ops
= &xfs_dir3_block_buf_ops
;
254 bp
->b_ops
->verify_read(bp
);
256 case cpu_to_be32(XFS_DIR2_DATA_MAGIC
):
257 case cpu_to_be32(XFS_DIR3_DATA_MAGIC
):
258 bp
->b_ops
= &xfs_dir3_data_buf_ops
;
259 bp
->b_ops
->verify_read(bp
);
262 xfs_buf_ioerror(bp
, -EFSCORRUPTED
);
263 xfs_verifier_error(bp
);
269 xfs_dir3_data_read_verify(
272 struct xfs_mount
*mp
= bp
->b_target
->bt_mount
;
274 if (xfs_sb_version_hascrc(&mp
->m_sb
) &&
275 !xfs_buf_verify_cksum(bp
, XFS_DIR3_DATA_CRC_OFF
))
276 xfs_buf_ioerror(bp
, -EFSBADCRC
);
277 else if (!xfs_dir3_data_verify(bp
))
278 xfs_buf_ioerror(bp
, -EFSCORRUPTED
);
281 xfs_verifier_error(bp
);
285 xfs_dir3_data_write_verify(
288 struct xfs_mount
*mp
= bp
->b_target
->bt_mount
;
289 struct xfs_buf_log_item
*bip
= bp
->b_fspriv
;
290 struct xfs_dir3_blk_hdr
*hdr3
= bp
->b_addr
;
292 if (!xfs_dir3_data_verify(bp
)) {
293 xfs_buf_ioerror(bp
, -EFSCORRUPTED
);
294 xfs_verifier_error(bp
);
298 if (!xfs_sb_version_hascrc(&mp
->m_sb
))
302 hdr3
->lsn
= cpu_to_be64(bip
->bli_item
.li_lsn
);
304 xfs_buf_update_cksum(bp
, XFS_DIR3_DATA_CRC_OFF
);
307 const struct xfs_buf_ops xfs_dir3_data_buf_ops
= {
308 .verify_read
= xfs_dir3_data_read_verify
,
309 .verify_write
= xfs_dir3_data_write_verify
,
312 static const struct xfs_buf_ops xfs_dir3_data_reada_buf_ops
= {
313 .verify_read
= xfs_dir3_data_reada_verify
,
314 .verify_write
= xfs_dir3_data_write_verify
,
320 struct xfs_trans
*tp
,
321 struct xfs_inode
*dp
,
323 xfs_daddr_t mapped_bno
,
324 struct xfs_buf
**bpp
)
328 err
= xfs_da_read_buf(tp
, dp
, bno
, mapped_bno
, bpp
,
329 XFS_DATA_FORK
, &xfs_dir3_data_buf_ops
);
331 xfs_trans_buf_set_type(tp
, *bpp
, XFS_BLFT_DIR_DATA_BUF
);
336 xfs_dir3_data_readahead(
337 struct xfs_inode
*dp
,
339 xfs_daddr_t mapped_bno
)
341 return xfs_da_reada_buf(dp
, bno
, mapped_bno
,
342 XFS_DATA_FORK
, &xfs_dir3_data_reada_buf_ops
);
346 * Given a data block and an unused entry from that block,
347 * return the bestfree entry if any that corresponds to it.
349 xfs_dir2_data_free_t
*
350 xfs_dir2_data_freefind(
351 struct xfs_dir2_data_hdr
*hdr
, /* data block header */
352 struct xfs_dir2_data_free
*bf
, /* bestfree table pointer */
353 struct xfs_dir2_data_unused
*dup
) /* unused space */
355 xfs_dir2_data_free_t
*dfp
; /* bestfree entry */
356 xfs_dir2_data_aoff_t off
; /* offset value needed */
358 int matched
; /* matched the value */
359 int seenzero
; /* saw a 0 bestfree entry */
362 off
= (xfs_dir2_data_aoff_t
)((char *)dup
- (char *)hdr
);
366 * Validate some consistency in the bestfree table.
367 * Check order, non-overlapping entries, and if we find the
368 * one we're looking for it has to be exact.
370 ASSERT(hdr
->magic
== cpu_to_be32(XFS_DIR2_DATA_MAGIC
) ||
371 hdr
->magic
== cpu_to_be32(XFS_DIR3_DATA_MAGIC
) ||
372 hdr
->magic
== cpu_to_be32(XFS_DIR2_BLOCK_MAGIC
) ||
373 hdr
->magic
== cpu_to_be32(XFS_DIR3_BLOCK_MAGIC
));
374 for (dfp
= &bf
[0], seenzero
= matched
= 0;
375 dfp
< &bf
[XFS_DIR2_DATA_FD_COUNT
];
378 ASSERT(!dfp
->length
);
382 ASSERT(seenzero
== 0);
383 if (be16_to_cpu(dfp
->offset
) == off
) {
385 ASSERT(dfp
->length
== dup
->length
);
386 } else if (off
< be16_to_cpu(dfp
->offset
))
387 ASSERT(off
+ be16_to_cpu(dup
->length
) <= be16_to_cpu(dfp
->offset
));
389 ASSERT(be16_to_cpu(dfp
->offset
) + be16_to_cpu(dfp
->length
) <= off
);
390 ASSERT(matched
|| be16_to_cpu(dfp
->length
) >= be16_to_cpu(dup
->length
));
392 ASSERT(be16_to_cpu(dfp
[-1].length
) >= be16_to_cpu(dfp
[0].length
));
396 * If this is smaller than the smallest bestfree entry,
397 * it can't be there since they're sorted.
399 if (be16_to_cpu(dup
->length
) <
400 be16_to_cpu(bf
[XFS_DIR2_DATA_FD_COUNT
- 1].length
))
403 * Look at the three bestfree entries for our guy.
405 for (dfp
= &bf
[0]; dfp
< &bf
[XFS_DIR2_DATA_FD_COUNT
]; dfp
++) {
408 if (be16_to_cpu(dfp
->offset
) == off
)
412 * Didn't find it. This only happens if there are duplicate lengths.
418 * Insert an unused-space entry into the bestfree table.
420 xfs_dir2_data_free_t
* /* entry inserted */
421 xfs_dir2_data_freeinsert(
422 struct xfs_dir2_data_hdr
*hdr
, /* data block pointer */
423 struct xfs_dir2_data_free
*dfp
, /* bestfree table pointer */
424 struct xfs_dir2_data_unused
*dup
, /* unused space */
425 int *loghead
) /* log the data header (out) */
427 xfs_dir2_data_free_t
new; /* new bestfree entry */
429 ASSERT(hdr
->magic
== cpu_to_be32(XFS_DIR2_DATA_MAGIC
) ||
430 hdr
->magic
== cpu_to_be32(XFS_DIR2_BLOCK_MAGIC
) ||
431 hdr
->magic
== cpu_to_be32(XFS_DIR3_DATA_MAGIC
) ||
432 hdr
->magic
== cpu_to_be32(XFS_DIR3_BLOCK_MAGIC
));
434 new.length
= dup
->length
;
435 new.offset
= cpu_to_be16((char *)dup
- (char *)hdr
);
438 * Insert at position 0, 1, or 2; or not at all.
440 if (be16_to_cpu(new.length
) > be16_to_cpu(dfp
[0].length
)) {
447 if (be16_to_cpu(new.length
) > be16_to_cpu(dfp
[1].length
)) {
453 if (be16_to_cpu(new.length
) > be16_to_cpu(dfp
[2].length
)) {
462 * Remove a bestfree entry from the table.
465 xfs_dir2_data_freeremove(
466 struct xfs_dir2_data_hdr
*hdr
, /* data block header */
467 struct xfs_dir2_data_free
*bf
, /* bestfree table pointer */
468 struct xfs_dir2_data_free
*dfp
, /* bestfree entry pointer */
469 int *loghead
) /* out: log data header */
472 ASSERT(hdr
->magic
== cpu_to_be32(XFS_DIR2_DATA_MAGIC
) ||
473 hdr
->magic
== cpu_to_be32(XFS_DIR2_BLOCK_MAGIC
) ||
474 hdr
->magic
== cpu_to_be32(XFS_DIR3_DATA_MAGIC
) ||
475 hdr
->magic
== cpu_to_be32(XFS_DIR3_BLOCK_MAGIC
));
478 * It's the first entry, slide the next 2 up.
485 * It's the second entry, slide the 3rd entry up.
487 else if (dfp
== &bf
[1])
490 * Must be the last entry.
493 ASSERT(dfp
== &bf
[2]);
495 * Clear the 3rd entry, must be zero now.
503 * Given a data block, reconstruct its bestfree map.
506 xfs_dir2_data_freescan(
507 struct xfs_inode
*dp
,
508 struct xfs_dir2_data_hdr
*hdr
,
511 xfs_dir2_block_tail_t
*btp
; /* block tail */
512 xfs_dir2_data_entry_t
*dep
; /* active data entry */
513 xfs_dir2_data_unused_t
*dup
; /* unused data entry */
514 struct xfs_dir2_data_free
*bf
;
515 char *endp
; /* end of block's data */
516 char *p
; /* current entry pointer */
517 struct xfs_da_geometry
*geo
= dp
->i_mount
->m_dir_geo
;
519 ASSERT(hdr
->magic
== cpu_to_be32(XFS_DIR2_DATA_MAGIC
) ||
520 hdr
->magic
== cpu_to_be32(XFS_DIR3_DATA_MAGIC
) ||
521 hdr
->magic
== cpu_to_be32(XFS_DIR2_BLOCK_MAGIC
) ||
522 hdr
->magic
== cpu_to_be32(XFS_DIR3_BLOCK_MAGIC
));
525 * Start by clearing the table.
527 bf
= dp
->d_ops
->data_bestfree_p(hdr
);
528 memset(bf
, 0, sizeof(*bf
) * XFS_DIR2_DATA_FD_COUNT
);
533 p
= (char *)dp
->d_ops
->data_entry_p(hdr
);
534 if (hdr
->magic
== cpu_to_be32(XFS_DIR2_BLOCK_MAGIC
) ||
535 hdr
->magic
== cpu_to_be32(XFS_DIR3_BLOCK_MAGIC
)) {
536 btp
= xfs_dir2_block_tail_p(geo
, hdr
);
537 endp
= (char *)xfs_dir2_block_leaf_p(btp
);
539 endp
= (char *)hdr
+ geo
->blksize
;
541 * Loop over the block's entries.
544 dup
= (xfs_dir2_data_unused_t
*)p
;
546 * If it's a free entry, insert it.
548 if (be16_to_cpu(dup
->freetag
) == XFS_DIR2_DATA_FREE_TAG
) {
549 ASSERT((char *)dup
- (char *)hdr
==
550 be16_to_cpu(*xfs_dir2_data_unused_tag_p(dup
)));
551 xfs_dir2_data_freeinsert(hdr
, bf
, dup
, loghead
);
552 p
+= be16_to_cpu(dup
->length
);
555 * For active entries, check their tags and skip them.
558 dep
= (xfs_dir2_data_entry_t
*)p
;
559 ASSERT((char *)dep
- (char *)hdr
==
560 be16_to_cpu(*dp
->d_ops
->data_entry_tag_p(dep
)));
561 p
+= dp
->d_ops
->data_entsize(dep
->namelen
);
567 * Initialize a data block at the given block number in the directory.
568 * Give back the buffer for the created block.
572 xfs_da_args_t
*args
, /* directory operation args */
573 xfs_dir2_db_t blkno
, /* logical dir block number */
574 struct xfs_buf
**bpp
) /* output block buffer */
576 struct xfs_buf
*bp
; /* block buffer */
577 xfs_dir2_data_hdr_t
*hdr
; /* data block header */
578 xfs_inode_t
*dp
; /* incore directory inode */
579 xfs_dir2_data_unused_t
*dup
; /* unused entry pointer */
580 struct xfs_dir2_data_free
*bf
;
581 int error
; /* error return value */
582 int i
; /* bestfree index */
583 xfs_mount_t
*mp
; /* filesystem mount point */
584 xfs_trans_t
*tp
; /* transaction pointer */
591 * Get the buffer set up for the block.
593 error
= xfs_da_get_buf(tp
, dp
, xfs_dir2_db_to_da(args
->geo
, blkno
),
594 -1, &bp
, XFS_DATA_FORK
);
597 bp
->b_ops
= &xfs_dir3_data_buf_ops
;
598 xfs_trans_buf_set_type(tp
, bp
, XFS_BLFT_DIR_DATA_BUF
);
601 * Initialize the header.
604 if (xfs_sb_version_hascrc(&mp
->m_sb
)) {
605 struct xfs_dir3_blk_hdr
*hdr3
= bp
->b_addr
;
607 memset(hdr3
, 0, sizeof(*hdr3
));
608 hdr3
->magic
= cpu_to_be32(XFS_DIR3_DATA_MAGIC
);
609 hdr3
->blkno
= cpu_to_be64(bp
->b_bn
);
610 hdr3
->owner
= cpu_to_be64(dp
->i_ino
);
611 uuid_copy(&hdr3
->uuid
, &mp
->m_sb
.sb_meta_uuid
);
614 hdr
->magic
= cpu_to_be32(XFS_DIR2_DATA_MAGIC
);
616 bf
= dp
->d_ops
->data_bestfree_p(hdr
);
617 bf
[0].offset
= cpu_to_be16(dp
->d_ops
->data_entry_offset
);
618 for (i
= 1; i
< XFS_DIR2_DATA_FD_COUNT
; i
++) {
624 * Set up an unused entry for the block's body.
626 dup
= dp
->d_ops
->data_unused_p(hdr
);
627 dup
->freetag
= cpu_to_be16(XFS_DIR2_DATA_FREE_TAG
);
629 t
= args
->geo
->blksize
- (uint
)dp
->d_ops
->data_entry_offset
;
630 bf
[0].length
= cpu_to_be16(t
);
631 dup
->length
= cpu_to_be16(t
);
632 *xfs_dir2_data_unused_tag_p(dup
) = cpu_to_be16((char *)dup
- (char *)hdr
);
634 * Log it and return it.
636 xfs_dir2_data_log_header(args
, bp
);
637 xfs_dir2_data_log_unused(args
, bp
, dup
);
643 * Log an active data entry from the block.
646 xfs_dir2_data_log_entry(
647 struct xfs_da_args
*args
,
649 xfs_dir2_data_entry_t
*dep
) /* data entry pointer */
651 struct xfs_dir2_data_hdr
*hdr
= bp
->b_addr
;
653 ASSERT(hdr
->magic
== cpu_to_be32(XFS_DIR2_DATA_MAGIC
) ||
654 hdr
->magic
== cpu_to_be32(XFS_DIR3_DATA_MAGIC
) ||
655 hdr
->magic
== cpu_to_be32(XFS_DIR2_BLOCK_MAGIC
) ||
656 hdr
->magic
== cpu_to_be32(XFS_DIR3_BLOCK_MAGIC
));
658 xfs_trans_log_buf(args
->trans
, bp
, (uint
)((char *)dep
- (char *)hdr
),
659 (uint
)((char *)(args
->dp
->d_ops
->data_entry_tag_p(dep
) + 1) -
664 * Log a data block header.
667 xfs_dir2_data_log_header(
668 struct xfs_da_args
*args
,
672 struct xfs_dir2_data_hdr
*hdr
= bp
->b_addr
;
674 ASSERT(hdr
->magic
== cpu_to_be32(XFS_DIR2_DATA_MAGIC
) ||
675 hdr
->magic
== cpu_to_be32(XFS_DIR3_DATA_MAGIC
) ||
676 hdr
->magic
== cpu_to_be32(XFS_DIR2_BLOCK_MAGIC
) ||
677 hdr
->magic
== cpu_to_be32(XFS_DIR3_BLOCK_MAGIC
));
680 xfs_trans_log_buf(args
->trans
, bp
, 0,
681 args
->dp
->d_ops
->data_entry_offset
- 1);
685 * Log a data unused entry.
688 xfs_dir2_data_log_unused(
689 struct xfs_da_args
*args
,
691 xfs_dir2_data_unused_t
*dup
) /* data unused pointer */
693 xfs_dir2_data_hdr_t
*hdr
= bp
->b_addr
;
695 ASSERT(hdr
->magic
== cpu_to_be32(XFS_DIR2_DATA_MAGIC
) ||
696 hdr
->magic
== cpu_to_be32(XFS_DIR3_DATA_MAGIC
) ||
697 hdr
->magic
== cpu_to_be32(XFS_DIR2_BLOCK_MAGIC
) ||
698 hdr
->magic
== cpu_to_be32(XFS_DIR3_BLOCK_MAGIC
));
701 * Log the first part of the unused entry.
703 xfs_trans_log_buf(args
->trans
, bp
, (uint
)((char *)dup
- (char *)hdr
),
704 (uint
)((char *)&dup
->length
+ sizeof(dup
->length
) -
707 * Log the end (tag) of the unused entry.
709 xfs_trans_log_buf(args
->trans
, bp
,
710 (uint
)((char *)xfs_dir2_data_unused_tag_p(dup
) - (char *)hdr
),
711 (uint
)((char *)xfs_dir2_data_unused_tag_p(dup
) - (char *)hdr
+
712 sizeof(xfs_dir2_data_off_t
) - 1));
716 * Make a byte range in the data block unused.
717 * Its current contents are unimportant.
720 xfs_dir2_data_make_free(
721 struct xfs_da_args
*args
,
723 xfs_dir2_data_aoff_t offset
, /* starting byte offset */
724 xfs_dir2_data_aoff_t len
, /* length in bytes */
725 int *needlogp
, /* out: log header */
726 int *needscanp
) /* out: regen bestfree */
728 xfs_dir2_data_hdr_t
*hdr
; /* data block pointer */
729 xfs_dir2_data_free_t
*dfp
; /* bestfree pointer */
730 char *endptr
; /* end of data area */
731 int needscan
; /* need to regen bestfree */
732 xfs_dir2_data_unused_t
*newdup
; /* new unused entry */
733 xfs_dir2_data_unused_t
*postdup
; /* unused entry after us */
734 xfs_dir2_data_unused_t
*prevdup
; /* unused entry before us */
735 struct xfs_dir2_data_free
*bf
;
740 * Figure out where the end of the data area is.
742 if (hdr
->magic
== cpu_to_be32(XFS_DIR2_DATA_MAGIC
) ||
743 hdr
->magic
== cpu_to_be32(XFS_DIR3_DATA_MAGIC
))
744 endptr
= (char *)hdr
+ args
->geo
->blksize
;
746 xfs_dir2_block_tail_t
*btp
; /* block tail */
748 ASSERT(hdr
->magic
== cpu_to_be32(XFS_DIR2_BLOCK_MAGIC
) ||
749 hdr
->magic
== cpu_to_be32(XFS_DIR3_BLOCK_MAGIC
));
750 btp
= xfs_dir2_block_tail_p(args
->geo
, hdr
);
751 endptr
= (char *)xfs_dir2_block_leaf_p(btp
);
754 * If this isn't the start of the block, then back up to
755 * the previous entry and see if it's free.
757 if (offset
> args
->dp
->d_ops
->data_entry_offset
) {
758 __be16
*tagp
; /* tag just before us */
760 tagp
= (__be16
*)((char *)hdr
+ offset
) - 1;
761 prevdup
= (xfs_dir2_data_unused_t
*)((char *)hdr
+ be16_to_cpu(*tagp
));
762 if (be16_to_cpu(prevdup
->freetag
) != XFS_DIR2_DATA_FREE_TAG
)
767 * If this isn't the end of the block, see if the entry after
770 if ((char *)hdr
+ offset
+ len
< endptr
) {
772 (xfs_dir2_data_unused_t
*)((char *)hdr
+ offset
+ len
);
773 if (be16_to_cpu(postdup
->freetag
) != XFS_DIR2_DATA_FREE_TAG
)
777 ASSERT(*needscanp
== 0);
780 * Previous and following entries are both free,
781 * merge everything into a single free entry.
783 bf
= args
->dp
->d_ops
->data_bestfree_p(hdr
);
784 if (prevdup
&& postdup
) {
785 xfs_dir2_data_free_t
*dfp2
; /* another bestfree pointer */
788 * See if prevdup and/or postdup are in bestfree table.
790 dfp
= xfs_dir2_data_freefind(hdr
, bf
, prevdup
);
791 dfp2
= xfs_dir2_data_freefind(hdr
, bf
, postdup
);
793 * We need a rescan unless there are exactly 2 free entries
794 * namely our two. Then we know what's happening, otherwise
795 * since the third bestfree is there, there might be more
798 needscan
= (bf
[2].length
!= 0);
800 * Fix up the new big freespace.
802 be16_add_cpu(&prevdup
->length
, len
+ be16_to_cpu(postdup
->length
));
803 *xfs_dir2_data_unused_tag_p(prevdup
) =
804 cpu_to_be16((char *)prevdup
- (char *)hdr
);
805 xfs_dir2_data_log_unused(args
, bp
, prevdup
);
808 * Has to be the case that entries 0 and 1 are
809 * dfp and dfp2 (don't know which is which), and
811 * Remove entry 1 first then entry 0.
819 xfs_dir2_data_freeremove(hdr
, bf
, dfp2
, needlogp
);
820 xfs_dir2_data_freeremove(hdr
, bf
, dfp
, needlogp
);
822 * Now insert the new entry.
824 dfp
= xfs_dir2_data_freeinsert(hdr
, bf
, prevdup
,
826 ASSERT(dfp
== &bf
[0]);
827 ASSERT(dfp
->length
== prevdup
->length
);
828 ASSERT(!dfp
[1].length
);
829 ASSERT(!dfp
[2].length
);
833 * The entry before us is free, merge with it.
836 dfp
= xfs_dir2_data_freefind(hdr
, bf
, prevdup
);
837 be16_add_cpu(&prevdup
->length
, len
);
838 *xfs_dir2_data_unused_tag_p(prevdup
) =
839 cpu_to_be16((char *)prevdup
- (char *)hdr
);
840 xfs_dir2_data_log_unused(args
, bp
, prevdup
);
842 * If the previous entry was in the table, the new entry
843 * is longer, so it will be in the table too. Remove
844 * the old one and add the new one.
847 xfs_dir2_data_freeremove(hdr
, bf
, dfp
, needlogp
);
848 xfs_dir2_data_freeinsert(hdr
, bf
, prevdup
, needlogp
);
851 * Otherwise we need a scan if the new entry is big enough.
854 needscan
= be16_to_cpu(prevdup
->length
) >
855 be16_to_cpu(bf
[2].length
);
859 * The following entry is free, merge with it.
862 dfp
= xfs_dir2_data_freefind(hdr
, bf
, postdup
);
863 newdup
= (xfs_dir2_data_unused_t
*)((char *)hdr
+ offset
);
864 newdup
->freetag
= cpu_to_be16(XFS_DIR2_DATA_FREE_TAG
);
865 newdup
->length
= cpu_to_be16(len
+ be16_to_cpu(postdup
->length
));
866 *xfs_dir2_data_unused_tag_p(newdup
) =
867 cpu_to_be16((char *)newdup
- (char *)hdr
);
868 xfs_dir2_data_log_unused(args
, bp
, newdup
);
870 * If the following entry was in the table, the new entry
871 * is longer, so it will be in the table too. Remove
872 * the old one and add the new one.
875 xfs_dir2_data_freeremove(hdr
, bf
, dfp
, needlogp
);
876 xfs_dir2_data_freeinsert(hdr
, bf
, newdup
, needlogp
);
879 * Otherwise we need a scan if the new entry is big enough.
882 needscan
= be16_to_cpu(newdup
->length
) >
883 be16_to_cpu(bf
[2].length
);
887 * Neither neighbor is free. Make a new entry.
890 newdup
= (xfs_dir2_data_unused_t
*)((char *)hdr
+ offset
);
891 newdup
->freetag
= cpu_to_be16(XFS_DIR2_DATA_FREE_TAG
);
892 newdup
->length
= cpu_to_be16(len
);
893 *xfs_dir2_data_unused_tag_p(newdup
) =
894 cpu_to_be16((char *)newdup
- (char *)hdr
);
895 xfs_dir2_data_log_unused(args
, bp
, newdup
);
896 xfs_dir2_data_freeinsert(hdr
, bf
, newdup
, needlogp
);
898 *needscanp
= needscan
;
902 * Take a byte range out of an existing unused space and make it un-free.
905 xfs_dir2_data_use_free(
906 struct xfs_da_args
*args
,
908 xfs_dir2_data_unused_t
*dup
, /* unused entry */
909 xfs_dir2_data_aoff_t offset
, /* starting offset to use */
910 xfs_dir2_data_aoff_t len
, /* length to use */
911 int *needlogp
, /* out: need to log header */
912 int *needscanp
) /* out: need regen bestfree */
914 xfs_dir2_data_hdr_t
*hdr
; /* data block header */
915 xfs_dir2_data_free_t
*dfp
; /* bestfree pointer */
916 int matchback
; /* matches end of freespace */
917 int matchfront
; /* matches start of freespace */
918 int needscan
; /* need to regen bestfree */
919 xfs_dir2_data_unused_t
*newdup
; /* new unused entry */
920 xfs_dir2_data_unused_t
*newdup2
; /* another new unused entry */
921 int oldlen
; /* old unused entry's length */
922 struct xfs_dir2_data_free
*bf
;
925 ASSERT(hdr
->magic
== cpu_to_be32(XFS_DIR2_DATA_MAGIC
) ||
926 hdr
->magic
== cpu_to_be32(XFS_DIR3_DATA_MAGIC
) ||
927 hdr
->magic
== cpu_to_be32(XFS_DIR2_BLOCK_MAGIC
) ||
928 hdr
->magic
== cpu_to_be32(XFS_DIR3_BLOCK_MAGIC
));
929 ASSERT(be16_to_cpu(dup
->freetag
) == XFS_DIR2_DATA_FREE_TAG
);
930 ASSERT(offset
>= (char *)dup
- (char *)hdr
);
931 ASSERT(offset
+ len
<= (char *)dup
+ be16_to_cpu(dup
->length
) - (char *)hdr
);
932 ASSERT((char *)dup
- (char *)hdr
== be16_to_cpu(*xfs_dir2_data_unused_tag_p(dup
)));
934 * Look up the entry in the bestfree table.
936 oldlen
= be16_to_cpu(dup
->length
);
937 bf
= args
->dp
->d_ops
->data_bestfree_p(hdr
);
938 dfp
= xfs_dir2_data_freefind(hdr
, bf
, dup
);
939 ASSERT(dfp
|| oldlen
<= be16_to_cpu(bf
[2].length
));
941 * Check for alignment with front and back of the entry.
943 matchfront
= (char *)dup
- (char *)hdr
== offset
;
944 matchback
= (char *)dup
+ oldlen
- (char *)hdr
== offset
+ len
;
945 ASSERT(*needscanp
== 0);
948 * If we matched it exactly we just need to get rid of it from
949 * the bestfree table.
951 if (matchfront
&& matchback
) {
953 needscan
= (bf
[2].offset
!= 0);
955 xfs_dir2_data_freeremove(hdr
, bf
, dfp
,
960 * We match the first part of the entry.
961 * Make a new entry with the remaining freespace.
963 else if (matchfront
) {
964 newdup
= (xfs_dir2_data_unused_t
*)((char *)hdr
+ offset
+ len
);
965 newdup
->freetag
= cpu_to_be16(XFS_DIR2_DATA_FREE_TAG
);
966 newdup
->length
= cpu_to_be16(oldlen
- len
);
967 *xfs_dir2_data_unused_tag_p(newdup
) =
968 cpu_to_be16((char *)newdup
- (char *)hdr
);
969 xfs_dir2_data_log_unused(args
, bp
, newdup
);
971 * If it was in the table, remove it and add the new one.
974 xfs_dir2_data_freeremove(hdr
, bf
, dfp
, needlogp
);
975 dfp
= xfs_dir2_data_freeinsert(hdr
, bf
, newdup
,
978 ASSERT(dfp
->length
== newdup
->length
);
979 ASSERT(be16_to_cpu(dfp
->offset
) == (char *)newdup
- (char *)hdr
);
981 * If we got inserted at the last slot,
982 * that means we don't know if there was a better
983 * choice for the last slot, or not. Rescan.
985 needscan
= dfp
== &bf
[2];
989 * We match the last part of the entry.
990 * Trim the allocated space off the tail of the entry.
992 else if (matchback
) {
994 newdup
->length
= cpu_to_be16(((char *)hdr
+ offset
) - (char *)newdup
);
995 *xfs_dir2_data_unused_tag_p(newdup
) =
996 cpu_to_be16((char *)newdup
- (char *)hdr
);
997 xfs_dir2_data_log_unused(args
, bp
, newdup
);
999 * If it was in the table, remove it and add the new one.
1002 xfs_dir2_data_freeremove(hdr
, bf
, dfp
, needlogp
);
1003 dfp
= xfs_dir2_data_freeinsert(hdr
, bf
, newdup
,
1005 ASSERT(dfp
!= NULL
);
1006 ASSERT(dfp
->length
== newdup
->length
);
1007 ASSERT(be16_to_cpu(dfp
->offset
) == (char *)newdup
- (char *)hdr
);
1009 * If we got inserted at the last slot,
1010 * that means we don't know if there was a better
1011 * choice for the last slot, or not. Rescan.
1013 needscan
= dfp
== &bf
[2];
1017 * Poking out the middle of an entry.
1018 * Make two new entries.
1022 newdup
->length
= cpu_to_be16(((char *)hdr
+ offset
) - (char *)newdup
);
1023 *xfs_dir2_data_unused_tag_p(newdup
) =
1024 cpu_to_be16((char *)newdup
- (char *)hdr
);
1025 xfs_dir2_data_log_unused(args
, bp
, newdup
);
1026 newdup2
= (xfs_dir2_data_unused_t
*)((char *)hdr
+ offset
+ len
);
1027 newdup2
->freetag
= cpu_to_be16(XFS_DIR2_DATA_FREE_TAG
);
1028 newdup2
->length
= cpu_to_be16(oldlen
- len
- be16_to_cpu(newdup
->length
));
1029 *xfs_dir2_data_unused_tag_p(newdup2
) =
1030 cpu_to_be16((char *)newdup2
- (char *)hdr
);
1031 xfs_dir2_data_log_unused(args
, bp
, newdup2
);
1033 * If the old entry was in the table, we need to scan
1034 * if the 3rd entry was valid, since these entries
1035 * are smaller than the old one.
1036 * If we don't need to scan that means there were 1 or 2
1037 * entries in the table, and removing the old and adding
1038 * the 2 new will work.
1041 needscan
= (bf
[2].length
!= 0);
1043 xfs_dir2_data_freeremove(hdr
, bf
, dfp
,
1045 xfs_dir2_data_freeinsert(hdr
, bf
, newdup
,
1047 xfs_dir2_data_freeinsert(hdr
, bf
, newdup2
,
1052 *needscanp
= needscan
;