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"
34 * Check the consistency of the data block.
35 * The input can also be a block-format directory.
36 * Return 0 is the buffer is good, otherwise an error.
39 __xfs_dir3_data_check(
40 struct xfs_inode
*dp
, /* incore inode pointer */
41 struct xfs_buf
*bp
) /* data block's buffer */
43 xfs_dir2_dataptr_t addr
; /* addr for leaf lookup */
44 xfs_dir2_data_free_t
*bf
; /* bestfree table */
45 xfs_dir2_block_tail_t
*btp
=NULL
; /* block tail */
46 int count
; /* count of entries found */
47 xfs_dir2_data_hdr_t
*hdr
; /* data block header */
48 xfs_dir2_data_entry_t
*dep
; /* data entry */
49 xfs_dir2_data_free_t
*dfp
; /* bestfree entry */
50 xfs_dir2_data_unused_t
*dup
; /* unused entry */
51 char *endp
; /* end of useful data */
52 int freeseen
; /* mask of bestfrees seen */
53 xfs_dahash_t hash
; /* hash of current name */
54 int i
; /* leaf index */
55 int lastfree
; /* last entry was unused */
56 xfs_dir2_leaf_entry_t
*lep
=NULL
; /* block leaf entries */
57 xfs_mount_t
*mp
; /* filesystem mount point */
58 char *p
; /* current data position */
59 int stale
; /* count of stale leaves */
61 const struct xfs_dir_ops
*ops
;
62 struct xfs_da_geometry
*geo
;
64 mp
= bp
->b_target
->bt_mount
;
68 * We can be passed a null dp here from a verifier, so we need to go the
69 * hard way to get them.
71 ops
= xfs_dir_get_ops(mp
, dp
);
74 p
= (char *)ops
->data_entry_p(hdr
);
77 case cpu_to_be32(XFS_DIR3_BLOCK_MAGIC
):
78 case cpu_to_be32(XFS_DIR2_BLOCK_MAGIC
):
79 btp
= xfs_dir2_block_tail_p(geo
, hdr
);
80 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 XFS_WANT_CORRUPTED_RETURN(mp
, be32_to_cpu(btp
->count
) <
91 ((char *)btp
- p
) / sizeof(struct xfs_dir2_leaf_entry
));
93 case cpu_to_be32(XFS_DIR3_DATA_MAGIC
):
94 case cpu_to_be32(XFS_DIR2_DATA_MAGIC
):
95 endp
= (char *)hdr
+ geo
->blksize
;
98 XFS_ERROR_REPORT("Bad Magic", XFS_ERRLEVEL_LOW
, mp
);
103 * Account for zero bestfree entries.
105 bf
= ops
->data_bestfree_p(hdr
);
106 count
= lastfree
= freeseen
= 0;
108 XFS_WANT_CORRUPTED_RETURN(mp
, !bf
[0].offset
);
112 XFS_WANT_CORRUPTED_RETURN(mp
, !bf
[1].offset
);
116 XFS_WANT_CORRUPTED_RETURN(mp
, !bf
[2].offset
);
120 XFS_WANT_CORRUPTED_RETURN(mp
, be16_to_cpu(bf
[0].length
) >=
121 be16_to_cpu(bf
[1].length
));
122 XFS_WANT_CORRUPTED_RETURN(mp
, be16_to_cpu(bf
[1].length
) >=
123 be16_to_cpu(bf
[2].length
));
125 * Loop over the data/unused entries.
128 dup
= (xfs_dir2_data_unused_t
*)p
;
130 * If it's unused, look for the space in the bestfree table.
131 * If we find it, account for that, else make sure it
132 * doesn't need to be there.
134 if (be16_to_cpu(dup
->freetag
) == XFS_DIR2_DATA_FREE_TAG
) {
135 XFS_WANT_CORRUPTED_RETURN(mp
, lastfree
== 0);
136 XFS_WANT_CORRUPTED_RETURN(mp
, endp
>=
137 p
+ be16_to_cpu(dup
->length
));
138 XFS_WANT_CORRUPTED_RETURN(mp
,
139 be16_to_cpu(*xfs_dir2_data_unused_tag_p(dup
)) ==
140 (char *)dup
- (char *)hdr
);
141 dfp
= xfs_dir2_data_freefind(hdr
, bf
, dup
);
144 XFS_WANT_CORRUPTED_RETURN(mp
,
145 (freeseen
& (1 << i
)) == 0);
148 XFS_WANT_CORRUPTED_RETURN(mp
,
149 be16_to_cpu(dup
->length
) <=
150 be16_to_cpu(bf
[2].length
));
152 p
+= be16_to_cpu(dup
->length
);
157 * It's a real entry. Validate the fields.
158 * If this is a block directory then make sure it's
159 * in the leaf section of the block.
160 * The linear search is crude but this is DEBUG code.
162 dep
= (xfs_dir2_data_entry_t
*)p
;
163 XFS_WANT_CORRUPTED_RETURN(mp
, dep
->namelen
!= 0);
164 XFS_WANT_CORRUPTED_RETURN(mp
,
165 !xfs_dir_ino_validate(mp
, be64_to_cpu(dep
->inumber
)));
166 XFS_WANT_CORRUPTED_RETURN(mp
, endp
>=
167 p
+ ops
->data_entsize(dep
->namelen
));
168 XFS_WANT_CORRUPTED_RETURN(mp
,
169 be16_to_cpu(*ops
->data_entry_tag_p(dep
)) ==
170 (char *)dep
- (char *)hdr
);
171 XFS_WANT_CORRUPTED_RETURN(mp
,
172 ops
->data_get_ftype(dep
) < XFS_DIR3_FT_MAX
);
175 if (hdr
->magic
== cpu_to_be32(XFS_DIR2_BLOCK_MAGIC
) ||
176 hdr
->magic
== cpu_to_be32(XFS_DIR3_BLOCK_MAGIC
)) {
177 addr
= xfs_dir2_db_off_to_dataptr(geo
, geo
->datablk
,
178 (xfs_dir2_data_aoff_t
)
179 ((char *)dep
- (char *)hdr
));
180 name
.name
= dep
->name
;
181 name
.len
= dep
->namelen
;
182 hash
= mp
->m_dirnameops
->hashname(&name
);
183 for (i
= 0; i
< be32_to_cpu(btp
->count
); i
++) {
184 if (be32_to_cpu(lep
[i
].address
) == addr
&&
185 be32_to_cpu(lep
[i
].hashval
) == hash
)
188 XFS_WANT_CORRUPTED_RETURN(mp
,
189 i
< be32_to_cpu(btp
->count
));
191 p
+= ops
->data_entsize(dep
->namelen
);
194 * Need to have seen all the entries and all the bestfree slots.
196 XFS_WANT_CORRUPTED_RETURN(mp
, freeseen
== 7);
197 if (hdr
->magic
== cpu_to_be32(XFS_DIR2_BLOCK_MAGIC
) ||
198 hdr
->magic
== cpu_to_be32(XFS_DIR3_BLOCK_MAGIC
)) {
199 for (i
= stale
= 0; i
< be32_to_cpu(btp
->count
); i
++) {
200 if (lep
[i
].address
==
201 cpu_to_be32(XFS_DIR2_NULL_DATAPTR
))
204 XFS_WANT_CORRUPTED_RETURN(mp
,
205 be32_to_cpu(lep
[i
].hashval
) >=
206 be32_to_cpu(lep
[i
- 1].hashval
));
208 XFS_WANT_CORRUPTED_RETURN(mp
, count
==
209 be32_to_cpu(btp
->count
) - be32_to_cpu(btp
->stale
));
210 XFS_WANT_CORRUPTED_RETURN(mp
, stale
== be32_to_cpu(btp
->stale
));
216 xfs_dir3_data_verify(
219 struct xfs_mount
*mp
= bp
->b_target
->bt_mount
;
220 struct xfs_dir3_blk_hdr
*hdr3
= bp
->b_addr
;
222 if (xfs_sb_version_hascrc(&mp
->m_sb
)) {
223 if (hdr3
->magic
!= cpu_to_be32(XFS_DIR3_DATA_MAGIC
))
225 if (!uuid_equal(&hdr3
->uuid
, &mp
->m_sb
.sb_meta_uuid
))
227 if (be64_to_cpu(hdr3
->blkno
) != bp
->b_bn
)
229 if (!xfs_log_check_lsn(mp
, be64_to_cpu(hdr3
->lsn
)))
232 if (hdr3
->magic
!= cpu_to_be32(XFS_DIR2_DATA_MAGIC
))
235 if (__xfs_dir3_data_check(NULL
, bp
))
241 * Readahead of the first block of the directory when it is opened is completely
242 * oblivious to the format of the directory. Hence we can either get a block
243 * format buffer or a data format buffer on readahead.
246 xfs_dir3_data_reada_verify(
249 struct xfs_dir2_data_hdr
*hdr
= bp
->b_addr
;
251 switch (hdr
->magic
) {
252 case cpu_to_be32(XFS_DIR2_BLOCK_MAGIC
):
253 case cpu_to_be32(XFS_DIR3_BLOCK_MAGIC
):
254 bp
->b_ops
= &xfs_dir3_block_buf_ops
;
255 bp
->b_ops
->verify_read(bp
);
257 case cpu_to_be32(XFS_DIR2_DATA_MAGIC
):
258 case cpu_to_be32(XFS_DIR3_DATA_MAGIC
):
259 bp
->b_ops
= &xfs_dir3_data_buf_ops
;
260 bp
->b_ops
->verify_read(bp
);
263 xfs_buf_ioerror(bp
, -EFSCORRUPTED
);
264 xfs_verifier_error(bp
);
270 xfs_dir3_data_read_verify(
273 struct xfs_mount
*mp
= bp
->b_target
->bt_mount
;
275 if (xfs_sb_version_hascrc(&mp
->m_sb
) &&
276 !xfs_buf_verify_cksum(bp
, XFS_DIR3_DATA_CRC_OFF
))
277 xfs_buf_ioerror(bp
, -EFSBADCRC
);
278 else if (!xfs_dir3_data_verify(bp
))
279 xfs_buf_ioerror(bp
, -EFSCORRUPTED
);
282 xfs_verifier_error(bp
);
286 xfs_dir3_data_write_verify(
289 struct xfs_mount
*mp
= bp
->b_target
->bt_mount
;
290 struct xfs_buf_log_item
*bip
= bp
->b_fspriv
;
291 struct xfs_dir3_blk_hdr
*hdr3
= bp
->b_addr
;
293 if (!xfs_dir3_data_verify(bp
)) {
294 xfs_buf_ioerror(bp
, -EFSCORRUPTED
);
295 xfs_verifier_error(bp
);
299 if (!xfs_sb_version_hascrc(&mp
->m_sb
))
303 hdr3
->lsn
= cpu_to_be64(bip
->bli_item
.li_lsn
);
305 xfs_buf_update_cksum(bp
, XFS_DIR3_DATA_CRC_OFF
);
308 const struct xfs_buf_ops xfs_dir3_data_buf_ops
= {
309 .name
= "xfs_dir3_data",
310 .verify_read
= xfs_dir3_data_read_verify
,
311 .verify_write
= xfs_dir3_data_write_verify
,
314 static const struct xfs_buf_ops xfs_dir3_data_reada_buf_ops
= {
315 .name
= "xfs_dir3_data_reada",
316 .verify_read
= xfs_dir3_data_reada_verify
,
317 .verify_write
= xfs_dir3_data_write_verify
,
323 struct xfs_trans
*tp
,
324 struct xfs_inode
*dp
,
326 xfs_daddr_t mapped_bno
,
327 struct xfs_buf
**bpp
)
331 err
= xfs_da_read_buf(tp
, dp
, bno
, mapped_bno
, bpp
,
332 XFS_DATA_FORK
, &xfs_dir3_data_buf_ops
);
333 if (!err
&& tp
&& *bpp
)
334 xfs_trans_buf_set_type(tp
, *bpp
, XFS_BLFT_DIR_DATA_BUF
);
339 xfs_dir3_data_readahead(
340 struct xfs_inode
*dp
,
342 xfs_daddr_t mapped_bno
)
344 return xfs_da_reada_buf(dp
, bno
, mapped_bno
,
345 XFS_DATA_FORK
, &xfs_dir3_data_reada_buf_ops
);
349 * Given a data block and an unused entry from that block,
350 * return the bestfree entry if any that corresponds to it.
352 xfs_dir2_data_free_t
*
353 xfs_dir2_data_freefind(
354 struct xfs_dir2_data_hdr
*hdr
, /* data block header */
355 struct xfs_dir2_data_free
*bf
, /* bestfree table pointer */
356 struct xfs_dir2_data_unused
*dup
) /* unused space */
358 xfs_dir2_data_free_t
*dfp
; /* bestfree entry */
359 xfs_dir2_data_aoff_t off
; /* offset value needed */
361 int matched
; /* matched the value */
362 int seenzero
; /* saw a 0 bestfree entry */
365 off
= (xfs_dir2_data_aoff_t
)((char *)dup
- (char *)hdr
);
369 * Validate some consistency in the bestfree table.
370 * Check order, non-overlapping entries, and if we find the
371 * one we're looking for it has to be exact.
373 ASSERT(hdr
->magic
== cpu_to_be32(XFS_DIR2_DATA_MAGIC
) ||
374 hdr
->magic
== cpu_to_be32(XFS_DIR3_DATA_MAGIC
) ||
375 hdr
->magic
== cpu_to_be32(XFS_DIR2_BLOCK_MAGIC
) ||
376 hdr
->magic
== cpu_to_be32(XFS_DIR3_BLOCK_MAGIC
));
377 for (dfp
= &bf
[0], seenzero
= matched
= 0;
378 dfp
< &bf
[XFS_DIR2_DATA_FD_COUNT
];
381 ASSERT(!dfp
->length
);
385 ASSERT(seenzero
== 0);
386 if (be16_to_cpu(dfp
->offset
) == off
) {
388 ASSERT(dfp
->length
== dup
->length
);
389 } else if (off
< be16_to_cpu(dfp
->offset
))
390 ASSERT(off
+ be16_to_cpu(dup
->length
) <= be16_to_cpu(dfp
->offset
));
392 ASSERT(be16_to_cpu(dfp
->offset
) + be16_to_cpu(dfp
->length
) <= off
);
393 ASSERT(matched
|| be16_to_cpu(dfp
->length
) >= be16_to_cpu(dup
->length
));
395 ASSERT(be16_to_cpu(dfp
[-1].length
) >= be16_to_cpu(dfp
[0].length
));
399 * If this is smaller than the smallest bestfree entry,
400 * it can't be there since they're sorted.
402 if (be16_to_cpu(dup
->length
) <
403 be16_to_cpu(bf
[XFS_DIR2_DATA_FD_COUNT
- 1].length
))
406 * Look at the three bestfree entries for our guy.
408 for (dfp
= &bf
[0]; dfp
< &bf
[XFS_DIR2_DATA_FD_COUNT
]; dfp
++) {
411 if (be16_to_cpu(dfp
->offset
) == off
)
415 * Didn't find it. This only happens if there are duplicate lengths.
421 * Insert an unused-space entry into the bestfree table.
423 xfs_dir2_data_free_t
* /* entry inserted */
424 xfs_dir2_data_freeinsert(
425 struct xfs_dir2_data_hdr
*hdr
, /* data block pointer */
426 struct xfs_dir2_data_free
*dfp
, /* bestfree table pointer */
427 struct xfs_dir2_data_unused
*dup
, /* unused space */
428 int *loghead
) /* log the data header (out) */
430 xfs_dir2_data_free_t
new; /* new bestfree entry */
432 ASSERT(hdr
->magic
== cpu_to_be32(XFS_DIR2_DATA_MAGIC
) ||
433 hdr
->magic
== cpu_to_be32(XFS_DIR2_BLOCK_MAGIC
) ||
434 hdr
->magic
== cpu_to_be32(XFS_DIR3_DATA_MAGIC
) ||
435 hdr
->magic
== cpu_to_be32(XFS_DIR3_BLOCK_MAGIC
));
437 new.length
= dup
->length
;
438 new.offset
= cpu_to_be16((char *)dup
- (char *)hdr
);
441 * Insert at position 0, 1, or 2; or not at all.
443 if (be16_to_cpu(new.length
) > be16_to_cpu(dfp
[0].length
)) {
450 if (be16_to_cpu(new.length
) > be16_to_cpu(dfp
[1].length
)) {
456 if (be16_to_cpu(new.length
) > be16_to_cpu(dfp
[2].length
)) {
465 * Remove a bestfree entry from the table.
468 xfs_dir2_data_freeremove(
469 struct xfs_dir2_data_hdr
*hdr
, /* data block header */
470 struct xfs_dir2_data_free
*bf
, /* bestfree table pointer */
471 struct xfs_dir2_data_free
*dfp
, /* bestfree entry pointer */
472 int *loghead
) /* out: log data header */
475 ASSERT(hdr
->magic
== cpu_to_be32(XFS_DIR2_DATA_MAGIC
) ||
476 hdr
->magic
== cpu_to_be32(XFS_DIR2_BLOCK_MAGIC
) ||
477 hdr
->magic
== cpu_to_be32(XFS_DIR3_DATA_MAGIC
) ||
478 hdr
->magic
== cpu_to_be32(XFS_DIR3_BLOCK_MAGIC
));
481 * It's the first entry, slide the next 2 up.
488 * It's the second entry, slide the 3rd entry up.
490 else if (dfp
== &bf
[1])
493 * Must be the last entry.
496 ASSERT(dfp
== &bf
[2]);
498 * Clear the 3rd entry, must be zero now.
506 * Given a data block, reconstruct its bestfree map.
509 xfs_dir2_data_freescan_int(
510 struct xfs_da_geometry
*geo
,
511 const struct xfs_dir_ops
*ops
,
512 struct xfs_dir2_data_hdr
*hdr
,
515 xfs_dir2_block_tail_t
*btp
; /* block tail */
516 xfs_dir2_data_entry_t
*dep
; /* active data entry */
517 xfs_dir2_data_unused_t
*dup
; /* unused data entry */
518 struct xfs_dir2_data_free
*bf
;
519 char *endp
; /* end of block's data */
520 char *p
; /* current entry pointer */
522 ASSERT(hdr
->magic
== cpu_to_be32(XFS_DIR2_DATA_MAGIC
) ||
523 hdr
->magic
== cpu_to_be32(XFS_DIR3_DATA_MAGIC
) ||
524 hdr
->magic
== cpu_to_be32(XFS_DIR2_BLOCK_MAGIC
) ||
525 hdr
->magic
== cpu_to_be32(XFS_DIR3_BLOCK_MAGIC
));
528 * Start by clearing the table.
530 bf
= ops
->data_bestfree_p(hdr
);
531 memset(bf
, 0, sizeof(*bf
) * XFS_DIR2_DATA_FD_COUNT
);
536 p
= (char *)ops
->data_entry_p(hdr
);
537 if (hdr
->magic
== cpu_to_be32(XFS_DIR2_BLOCK_MAGIC
) ||
538 hdr
->magic
== cpu_to_be32(XFS_DIR3_BLOCK_MAGIC
)) {
539 btp
= xfs_dir2_block_tail_p(geo
, hdr
);
540 endp
= (char *)xfs_dir2_block_leaf_p(btp
);
542 endp
= (char *)hdr
+ geo
->blksize
;
544 * Loop over the block's entries.
547 dup
= (xfs_dir2_data_unused_t
*)p
;
549 * If it's a free entry, insert it.
551 if (be16_to_cpu(dup
->freetag
) == XFS_DIR2_DATA_FREE_TAG
) {
552 ASSERT((char *)dup
- (char *)hdr
==
553 be16_to_cpu(*xfs_dir2_data_unused_tag_p(dup
)));
554 xfs_dir2_data_freeinsert(hdr
, bf
, dup
, loghead
);
555 p
+= be16_to_cpu(dup
->length
);
558 * For active entries, check their tags and skip them.
561 dep
= (xfs_dir2_data_entry_t
*)p
;
562 ASSERT((char *)dep
- (char *)hdr
==
563 be16_to_cpu(*ops
->data_entry_tag_p(dep
)));
564 p
+= ops
->data_entsize(dep
->namelen
);
570 xfs_dir2_data_freescan(
571 struct xfs_inode
*dp
,
572 struct xfs_dir2_data_hdr
*hdr
,
575 return xfs_dir2_data_freescan_int(dp
->i_mount
->m_dir_geo
, dp
->d_ops
,
580 * Initialize a data block at the given block number in the directory.
581 * Give back the buffer for the created block.
585 xfs_da_args_t
*args
, /* directory operation args */
586 xfs_dir2_db_t blkno
, /* logical dir block number */
587 struct xfs_buf
**bpp
) /* output block buffer */
589 struct xfs_buf
*bp
; /* block buffer */
590 xfs_dir2_data_hdr_t
*hdr
; /* data block header */
591 xfs_inode_t
*dp
; /* incore directory inode */
592 xfs_dir2_data_unused_t
*dup
; /* unused entry pointer */
593 struct xfs_dir2_data_free
*bf
;
594 int error
; /* error return value */
595 int i
; /* bestfree index */
596 xfs_mount_t
*mp
; /* filesystem mount point */
597 xfs_trans_t
*tp
; /* transaction pointer */
604 * Get the buffer set up for the block.
606 error
= xfs_da_get_buf(tp
, dp
, xfs_dir2_db_to_da(args
->geo
, blkno
),
607 -1, &bp
, XFS_DATA_FORK
);
610 bp
->b_ops
= &xfs_dir3_data_buf_ops
;
611 xfs_trans_buf_set_type(tp
, bp
, XFS_BLFT_DIR_DATA_BUF
);
614 * Initialize the header.
617 if (xfs_sb_version_hascrc(&mp
->m_sb
)) {
618 struct xfs_dir3_blk_hdr
*hdr3
= bp
->b_addr
;
620 memset(hdr3
, 0, sizeof(*hdr3
));
621 hdr3
->magic
= cpu_to_be32(XFS_DIR3_DATA_MAGIC
);
622 hdr3
->blkno
= cpu_to_be64(bp
->b_bn
);
623 hdr3
->owner
= cpu_to_be64(dp
->i_ino
);
624 uuid_copy(&hdr3
->uuid
, &mp
->m_sb
.sb_meta_uuid
);
627 hdr
->magic
= cpu_to_be32(XFS_DIR2_DATA_MAGIC
);
629 bf
= dp
->d_ops
->data_bestfree_p(hdr
);
630 bf
[0].offset
= cpu_to_be16(dp
->d_ops
->data_entry_offset
);
631 for (i
= 1; i
< XFS_DIR2_DATA_FD_COUNT
; i
++) {
637 * Set up an unused entry for the block's body.
639 dup
= dp
->d_ops
->data_unused_p(hdr
);
640 dup
->freetag
= cpu_to_be16(XFS_DIR2_DATA_FREE_TAG
);
642 t
= args
->geo
->blksize
- (uint
)dp
->d_ops
->data_entry_offset
;
643 bf
[0].length
= cpu_to_be16(t
);
644 dup
->length
= cpu_to_be16(t
);
645 *xfs_dir2_data_unused_tag_p(dup
) = cpu_to_be16((char *)dup
- (char *)hdr
);
647 * Log it and return it.
649 xfs_dir2_data_log_header(args
, bp
);
650 xfs_dir2_data_log_unused(args
, bp
, dup
);
656 * Log an active data entry from the block.
659 xfs_dir2_data_log_entry(
660 struct xfs_da_args
*args
,
662 xfs_dir2_data_entry_t
*dep
) /* data entry pointer */
664 struct xfs_dir2_data_hdr
*hdr
= bp
->b_addr
;
666 ASSERT(hdr
->magic
== cpu_to_be32(XFS_DIR2_DATA_MAGIC
) ||
667 hdr
->magic
== cpu_to_be32(XFS_DIR3_DATA_MAGIC
) ||
668 hdr
->magic
== cpu_to_be32(XFS_DIR2_BLOCK_MAGIC
) ||
669 hdr
->magic
== cpu_to_be32(XFS_DIR3_BLOCK_MAGIC
));
671 xfs_trans_log_buf(args
->trans
, bp
, (uint
)((char *)dep
- (char *)hdr
),
672 (uint
)((char *)(args
->dp
->d_ops
->data_entry_tag_p(dep
) + 1) -
677 * Log a data block header.
680 xfs_dir2_data_log_header(
681 struct xfs_da_args
*args
,
685 struct xfs_dir2_data_hdr
*hdr
= bp
->b_addr
;
687 ASSERT(hdr
->magic
== cpu_to_be32(XFS_DIR2_DATA_MAGIC
) ||
688 hdr
->magic
== cpu_to_be32(XFS_DIR3_DATA_MAGIC
) ||
689 hdr
->magic
== cpu_to_be32(XFS_DIR2_BLOCK_MAGIC
) ||
690 hdr
->magic
== cpu_to_be32(XFS_DIR3_BLOCK_MAGIC
));
693 xfs_trans_log_buf(args
->trans
, bp
, 0,
694 args
->dp
->d_ops
->data_entry_offset
- 1);
698 * Log a data unused entry.
701 xfs_dir2_data_log_unused(
702 struct xfs_da_args
*args
,
704 xfs_dir2_data_unused_t
*dup
) /* data unused pointer */
706 xfs_dir2_data_hdr_t
*hdr
= bp
->b_addr
;
708 ASSERT(hdr
->magic
== cpu_to_be32(XFS_DIR2_DATA_MAGIC
) ||
709 hdr
->magic
== cpu_to_be32(XFS_DIR3_DATA_MAGIC
) ||
710 hdr
->magic
== cpu_to_be32(XFS_DIR2_BLOCK_MAGIC
) ||
711 hdr
->magic
== cpu_to_be32(XFS_DIR3_BLOCK_MAGIC
));
714 * Log the first part of the unused entry.
716 xfs_trans_log_buf(args
->trans
, bp
, (uint
)((char *)dup
- (char *)hdr
),
717 (uint
)((char *)&dup
->length
+ sizeof(dup
->length
) -
720 * Log the end (tag) of the unused entry.
722 xfs_trans_log_buf(args
->trans
, bp
,
723 (uint
)((char *)xfs_dir2_data_unused_tag_p(dup
) - (char *)hdr
),
724 (uint
)((char *)xfs_dir2_data_unused_tag_p(dup
) - (char *)hdr
+
725 sizeof(xfs_dir2_data_off_t
) - 1));
729 * Make a byte range in the data block unused.
730 * Its current contents are unimportant.
733 xfs_dir2_data_make_free(
734 struct xfs_da_args
*args
,
736 xfs_dir2_data_aoff_t offset
, /* starting byte offset */
737 xfs_dir2_data_aoff_t len
, /* length in bytes */
738 int *needlogp
, /* out: log header */
739 int *needscanp
) /* out: regen bestfree */
741 xfs_dir2_data_hdr_t
*hdr
; /* data block pointer */
742 xfs_dir2_data_free_t
*dfp
; /* bestfree pointer */
743 char *endptr
; /* end of data area */
744 int needscan
; /* need to regen bestfree */
745 xfs_dir2_data_unused_t
*newdup
; /* new unused entry */
746 xfs_dir2_data_unused_t
*postdup
; /* unused entry after us */
747 xfs_dir2_data_unused_t
*prevdup
; /* unused entry before us */
748 struct xfs_dir2_data_free
*bf
;
753 * Figure out where the end of the data area is.
755 if (hdr
->magic
== cpu_to_be32(XFS_DIR2_DATA_MAGIC
) ||
756 hdr
->magic
== cpu_to_be32(XFS_DIR3_DATA_MAGIC
))
757 endptr
= (char *)hdr
+ args
->geo
->blksize
;
759 xfs_dir2_block_tail_t
*btp
; /* block tail */
761 ASSERT(hdr
->magic
== cpu_to_be32(XFS_DIR2_BLOCK_MAGIC
) ||
762 hdr
->magic
== cpu_to_be32(XFS_DIR3_BLOCK_MAGIC
));
763 btp
= xfs_dir2_block_tail_p(args
->geo
, hdr
);
764 endptr
= (char *)xfs_dir2_block_leaf_p(btp
);
767 * If this isn't the start of the block, then back up to
768 * the previous entry and see if it's free.
770 if (offset
> args
->dp
->d_ops
->data_entry_offset
) {
771 __be16
*tagp
; /* tag just before us */
773 tagp
= (__be16
*)((char *)hdr
+ offset
) - 1;
774 prevdup
= (xfs_dir2_data_unused_t
*)((char *)hdr
+ be16_to_cpu(*tagp
));
775 if (be16_to_cpu(prevdup
->freetag
) != XFS_DIR2_DATA_FREE_TAG
)
780 * If this isn't the end of the block, see if the entry after
783 if ((char *)hdr
+ offset
+ len
< endptr
) {
785 (xfs_dir2_data_unused_t
*)((char *)hdr
+ offset
+ len
);
786 if (be16_to_cpu(postdup
->freetag
) != XFS_DIR2_DATA_FREE_TAG
)
790 ASSERT(*needscanp
== 0);
793 * Previous and following entries are both free,
794 * merge everything into a single free entry.
796 bf
= args
->dp
->d_ops
->data_bestfree_p(hdr
);
797 if (prevdup
&& postdup
) {
798 xfs_dir2_data_free_t
*dfp2
; /* another bestfree pointer */
801 * See if prevdup and/or postdup are in bestfree table.
803 dfp
= xfs_dir2_data_freefind(hdr
, bf
, prevdup
);
804 dfp2
= xfs_dir2_data_freefind(hdr
, bf
, postdup
);
806 * We need a rescan unless there are exactly 2 free entries
807 * namely our two. Then we know what's happening, otherwise
808 * since the third bestfree is there, there might be more
811 needscan
= (bf
[2].length
!= 0);
813 * Fix up the new big freespace.
815 be16_add_cpu(&prevdup
->length
, len
+ be16_to_cpu(postdup
->length
));
816 *xfs_dir2_data_unused_tag_p(prevdup
) =
817 cpu_to_be16((char *)prevdup
- (char *)hdr
);
818 xfs_dir2_data_log_unused(args
, bp
, prevdup
);
821 * Has to be the case that entries 0 and 1 are
822 * dfp and dfp2 (don't know which is which), and
824 * Remove entry 1 first then entry 0.
832 xfs_dir2_data_freeremove(hdr
, bf
, dfp2
, needlogp
);
833 xfs_dir2_data_freeremove(hdr
, bf
, dfp
, needlogp
);
835 * Now insert the new entry.
837 dfp
= xfs_dir2_data_freeinsert(hdr
, bf
, prevdup
,
839 ASSERT(dfp
== &bf
[0]);
840 ASSERT(dfp
->length
== prevdup
->length
);
841 ASSERT(!dfp
[1].length
);
842 ASSERT(!dfp
[2].length
);
846 * The entry before us is free, merge with it.
849 dfp
= xfs_dir2_data_freefind(hdr
, bf
, prevdup
);
850 be16_add_cpu(&prevdup
->length
, len
);
851 *xfs_dir2_data_unused_tag_p(prevdup
) =
852 cpu_to_be16((char *)prevdup
- (char *)hdr
);
853 xfs_dir2_data_log_unused(args
, bp
, prevdup
);
855 * If the previous entry was in the table, the new entry
856 * is longer, so it will be in the table too. Remove
857 * the old one and add the new one.
860 xfs_dir2_data_freeremove(hdr
, bf
, dfp
, needlogp
);
861 xfs_dir2_data_freeinsert(hdr
, bf
, prevdup
, needlogp
);
864 * Otherwise we need a scan if the new entry is big enough.
867 needscan
= be16_to_cpu(prevdup
->length
) >
868 be16_to_cpu(bf
[2].length
);
872 * The following entry is free, merge with it.
875 dfp
= xfs_dir2_data_freefind(hdr
, bf
, postdup
);
876 newdup
= (xfs_dir2_data_unused_t
*)((char *)hdr
+ offset
);
877 newdup
->freetag
= cpu_to_be16(XFS_DIR2_DATA_FREE_TAG
);
878 newdup
->length
= cpu_to_be16(len
+ be16_to_cpu(postdup
->length
));
879 *xfs_dir2_data_unused_tag_p(newdup
) =
880 cpu_to_be16((char *)newdup
- (char *)hdr
);
881 xfs_dir2_data_log_unused(args
, bp
, newdup
);
883 * If the following entry was in the table, the new entry
884 * is longer, so it will be in the table too. Remove
885 * the old one and add the new one.
888 xfs_dir2_data_freeremove(hdr
, bf
, dfp
, needlogp
);
889 xfs_dir2_data_freeinsert(hdr
, bf
, newdup
, needlogp
);
892 * Otherwise we need a scan if the new entry is big enough.
895 needscan
= be16_to_cpu(newdup
->length
) >
896 be16_to_cpu(bf
[2].length
);
900 * Neither neighbor is free. Make a new entry.
903 newdup
= (xfs_dir2_data_unused_t
*)((char *)hdr
+ offset
);
904 newdup
->freetag
= cpu_to_be16(XFS_DIR2_DATA_FREE_TAG
);
905 newdup
->length
= cpu_to_be16(len
);
906 *xfs_dir2_data_unused_tag_p(newdup
) =
907 cpu_to_be16((char *)newdup
- (char *)hdr
);
908 xfs_dir2_data_log_unused(args
, bp
, newdup
);
909 xfs_dir2_data_freeinsert(hdr
, bf
, newdup
, needlogp
);
911 *needscanp
= needscan
;
915 * Take a byte range out of an existing unused space and make it un-free.
918 xfs_dir2_data_use_free(
919 struct xfs_da_args
*args
,
921 xfs_dir2_data_unused_t
*dup
, /* unused entry */
922 xfs_dir2_data_aoff_t offset
, /* starting offset to use */
923 xfs_dir2_data_aoff_t len
, /* length to use */
924 int *needlogp
, /* out: need to log header */
925 int *needscanp
) /* out: need regen bestfree */
927 xfs_dir2_data_hdr_t
*hdr
; /* data block header */
928 xfs_dir2_data_free_t
*dfp
; /* bestfree pointer */
929 int matchback
; /* matches end of freespace */
930 int matchfront
; /* matches start of freespace */
931 int needscan
; /* need to regen bestfree */
932 xfs_dir2_data_unused_t
*newdup
; /* new unused entry */
933 xfs_dir2_data_unused_t
*newdup2
; /* another new unused entry */
934 int oldlen
; /* old unused entry's length */
935 struct xfs_dir2_data_free
*bf
;
938 ASSERT(hdr
->magic
== cpu_to_be32(XFS_DIR2_DATA_MAGIC
) ||
939 hdr
->magic
== cpu_to_be32(XFS_DIR3_DATA_MAGIC
) ||
940 hdr
->magic
== cpu_to_be32(XFS_DIR2_BLOCK_MAGIC
) ||
941 hdr
->magic
== cpu_to_be32(XFS_DIR3_BLOCK_MAGIC
));
942 ASSERT(be16_to_cpu(dup
->freetag
) == XFS_DIR2_DATA_FREE_TAG
);
943 ASSERT(offset
>= (char *)dup
- (char *)hdr
);
944 ASSERT(offset
+ len
<= (char *)dup
+ be16_to_cpu(dup
->length
) - (char *)hdr
);
945 ASSERT((char *)dup
- (char *)hdr
== be16_to_cpu(*xfs_dir2_data_unused_tag_p(dup
)));
947 * Look up the entry in the bestfree table.
949 oldlen
= be16_to_cpu(dup
->length
);
950 bf
= args
->dp
->d_ops
->data_bestfree_p(hdr
);
951 dfp
= xfs_dir2_data_freefind(hdr
, bf
, dup
);
952 ASSERT(dfp
|| oldlen
<= be16_to_cpu(bf
[2].length
));
954 * Check for alignment with front and back of the entry.
956 matchfront
= (char *)dup
- (char *)hdr
== offset
;
957 matchback
= (char *)dup
+ oldlen
- (char *)hdr
== offset
+ len
;
958 ASSERT(*needscanp
== 0);
961 * If we matched it exactly we just need to get rid of it from
962 * the bestfree table.
964 if (matchfront
&& matchback
) {
966 needscan
= (bf
[2].offset
!= 0);
968 xfs_dir2_data_freeremove(hdr
, bf
, dfp
,
973 * We match the first part of the entry.
974 * Make a new entry with the remaining freespace.
976 else if (matchfront
) {
977 newdup
= (xfs_dir2_data_unused_t
*)((char *)hdr
+ offset
+ len
);
978 newdup
->freetag
= cpu_to_be16(XFS_DIR2_DATA_FREE_TAG
);
979 newdup
->length
= cpu_to_be16(oldlen
- len
);
980 *xfs_dir2_data_unused_tag_p(newdup
) =
981 cpu_to_be16((char *)newdup
- (char *)hdr
);
982 xfs_dir2_data_log_unused(args
, bp
, newdup
);
984 * If it was in the table, remove it and add the new one.
987 xfs_dir2_data_freeremove(hdr
, bf
, dfp
, needlogp
);
988 dfp
= xfs_dir2_data_freeinsert(hdr
, bf
, newdup
,
991 ASSERT(dfp
->length
== newdup
->length
);
992 ASSERT(be16_to_cpu(dfp
->offset
) == (char *)newdup
- (char *)hdr
);
994 * If we got inserted at the last slot,
995 * that means we don't know if there was a better
996 * choice for the last slot, or not. Rescan.
998 needscan
= dfp
== &bf
[2];
1002 * We match the last part of the entry.
1003 * Trim the allocated space off the tail of the entry.
1005 else if (matchback
) {
1007 newdup
->length
= cpu_to_be16(((char *)hdr
+ offset
) - (char *)newdup
);
1008 *xfs_dir2_data_unused_tag_p(newdup
) =
1009 cpu_to_be16((char *)newdup
- (char *)hdr
);
1010 xfs_dir2_data_log_unused(args
, bp
, newdup
);
1012 * If it was in the table, remove it and add the new one.
1015 xfs_dir2_data_freeremove(hdr
, bf
, dfp
, needlogp
);
1016 dfp
= xfs_dir2_data_freeinsert(hdr
, bf
, newdup
,
1018 ASSERT(dfp
!= NULL
);
1019 ASSERT(dfp
->length
== newdup
->length
);
1020 ASSERT(be16_to_cpu(dfp
->offset
) == (char *)newdup
- (char *)hdr
);
1022 * If we got inserted at the last slot,
1023 * that means we don't know if there was a better
1024 * choice for the last slot, or not. Rescan.
1026 needscan
= dfp
== &bf
[2];
1030 * Poking out the middle of an entry.
1031 * Make two new entries.
1035 newdup
->length
= cpu_to_be16(((char *)hdr
+ offset
) - (char *)newdup
);
1036 *xfs_dir2_data_unused_tag_p(newdup
) =
1037 cpu_to_be16((char *)newdup
- (char *)hdr
);
1038 xfs_dir2_data_log_unused(args
, bp
, newdup
);
1039 newdup2
= (xfs_dir2_data_unused_t
*)((char *)hdr
+ offset
+ len
);
1040 newdup2
->freetag
= cpu_to_be16(XFS_DIR2_DATA_FREE_TAG
);
1041 newdup2
->length
= cpu_to_be16(oldlen
- len
- be16_to_cpu(newdup
->length
));
1042 *xfs_dir2_data_unused_tag_p(newdup2
) =
1043 cpu_to_be16((char *)newdup2
- (char *)hdr
);
1044 xfs_dir2_data_log_unused(args
, bp
, newdup2
);
1046 * If the old entry was in the table, we need to scan
1047 * if the 3rd entry was valid, since these entries
1048 * are smaller than the old one.
1049 * If we don't need to scan that means there were 1 or 2
1050 * entries in the table, and removing the old and adding
1051 * the 2 new will work.
1054 needscan
= (bf
[2].length
!= 0);
1056 xfs_dir2_data_freeremove(hdr
, bf
, dfp
,
1058 xfs_dir2_data_freeinsert(hdr
, bf
, newdup
,
1060 xfs_dir2_data_freeinsert(hdr
, bf
, newdup2
,
1065 *needscanp
= needscan
;