2 * Copyright (c) 2000-2003,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"
30 #include "xfs_dir2_priv.h"
31 #include "xfs_trace.h"
32 #include "xfs_trans.h"
33 #include "xfs_cksum.h"
36 * Local function declarations.
38 static int xfs_dir2_leaf_lookup_int(xfs_da_args_t
*args
, struct xfs_buf
**lbpp
,
39 int *indexp
, struct xfs_buf
**dbpp
);
40 static void xfs_dir3_leaf_log_bests(struct xfs_da_args
*args
,
41 struct xfs_buf
*bp
, int first
, int last
);
42 static void xfs_dir3_leaf_log_tail(struct xfs_da_args
*args
,
46 * Check the internal consistency of a leaf1 block.
47 * Pop an assert if something is wrong.
55 struct xfs_dir2_leaf
*leaf
= bp
->b_addr
;
56 struct xfs_dir3_icleaf_hdr leafhdr
;
58 dp
->d_ops
->leaf_hdr_from_disk(&leafhdr
, leaf
);
60 if (leafhdr
.magic
== XFS_DIR3_LEAF1_MAGIC
) {
61 struct xfs_dir3_leaf_hdr
*leaf3
= bp
->b_addr
;
62 if (be64_to_cpu(leaf3
->info
.blkno
) != bp
->b_bn
)
63 return __this_address
;
64 } else if (leafhdr
.magic
!= XFS_DIR2_LEAF1_MAGIC
)
65 return __this_address
;
67 return xfs_dir3_leaf_check_int(dp
->i_mount
, dp
, &leafhdr
, leaf
);
77 fa
= xfs_dir3_leaf1_check(dp
, bp
);
80 xfs_corruption_error(__func__
, XFS_ERRLEVEL_LOW
, dp
->i_mount
,
81 bp
->b_addr
, __FILE__
, __LINE__
, fa
);
85 #define xfs_dir3_leaf_check(dp, bp)
89 xfs_dir3_leaf_check_int(
92 struct xfs_dir3_icleaf_hdr
*hdr
,
93 struct xfs_dir2_leaf
*leaf
)
95 struct xfs_dir2_leaf_entry
*ents
;
96 xfs_dir2_leaf_tail_t
*ltp
;
99 const struct xfs_dir_ops
*ops
;
100 struct xfs_dir3_icleaf_hdr leafhdr
;
101 struct xfs_da_geometry
*geo
= mp
->m_dir_geo
;
104 * we can be passed a null dp here from a verifier, so we need to go the
105 * hard way to get them.
107 ops
= xfs_dir_get_ops(mp
, dp
);
110 ops
->leaf_hdr_from_disk(&leafhdr
, leaf
);
114 ents
= ops
->leaf_ents_p(leaf
);
115 ltp
= xfs_dir2_leaf_tail_p(geo
, leaf
);
118 * XXX (dgc): This value is not restrictive enough.
119 * Should factor in the size of the bests table as well.
120 * We can deduce a value for that from di_size.
122 if (hdr
->count
> ops
->leaf_max_ents(geo
))
123 return __this_address
;
125 /* Leaves and bests don't overlap in leaf format. */
126 if ((hdr
->magic
== XFS_DIR2_LEAF1_MAGIC
||
127 hdr
->magic
== XFS_DIR3_LEAF1_MAGIC
) &&
128 (char *)&ents
[hdr
->count
] > (char *)xfs_dir2_leaf_bests_p(ltp
))
129 return __this_address
;
131 /* Check hash value order, count stale entries. */
132 for (i
= stale
= 0; i
< hdr
->count
; i
++) {
133 if (i
+ 1 < hdr
->count
) {
134 if (be32_to_cpu(ents
[i
].hashval
) >
135 be32_to_cpu(ents
[i
+ 1].hashval
))
136 return __this_address
;
138 if (ents
[i
].address
== cpu_to_be32(XFS_DIR2_NULL_DATAPTR
))
141 if (hdr
->stale
!= stale
)
142 return __this_address
;
147 * We verify the magic numbers before decoding the leaf header so that on debug
148 * kernels we don't get assertion failures in xfs_dir3_leaf_hdr_from_disk() due
149 * to incorrect magic numbers.
151 static xfs_failaddr_t
152 xfs_dir3_leaf_verify(
156 struct xfs_mount
*mp
= bp
->b_target
->bt_mount
;
157 struct xfs_dir2_leaf
*leaf
= bp
->b_addr
;
159 ASSERT(magic
== XFS_DIR2_LEAF1_MAGIC
|| magic
== XFS_DIR2_LEAFN_MAGIC
);
161 if (xfs_sb_version_hascrc(&mp
->m_sb
)) {
162 struct xfs_dir3_leaf_hdr
*leaf3
= bp
->b_addr
;
165 magic3
= (magic
== XFS_DIR2_LEAF1_MAGIC
) ? XFS_DIR3_LEAF1_MAGIC
166 : XFS_DIR3_LEAFN_MAGIC
;
168 if (leaf3
->info
.hdr
.magic
!= cpu_to_be16(magic3
))
169 return __this_address
;
170 if (!uuid_equal(&leaf3
->info
.uuid
, &mp
->m_sb
.sb_meta_uuid
))
171 return __this_address
;
172 if (be64_to_cpu(leaf3
->info
.blkno
) != bp
->b_bn
)
173 return __this_address
;
174 if (!xfs_log_check_lsn(mp
, be64_to_cpu(leaf3
->info
.lsn
)))
175 return __this_address
;
177 if (leaf
->hdr
.info
.magic
!= cpu_to_be16(magic
))
178 return __this_address
;
181 return xfs_dir3_leaf_check_int(mp
, NULL
, NULL
, leaf
);
189 struct xfs_mount
*mp
= bp
->b_target
->bt_mount
;
192 if (xfs_sb_version_hascrc(&mp
->m_sb
) &&
193 !xfs_buf_verify_cksum(bp
, XFS_DIR3_LEAF_CRC_OFF
))
194 xfs_verifier_error(bp
, -EFSBADCRC
, __this_address
);
196 fa
= xfs_dir3_leaf_verify(bp
, magic
);
198 xfs_verifier_error(bp
, -EFSCORRUPTED
, fa
);
207 struct xfs_mount
*mp
= bp
->b_target
->bt_mount
;
208 struct xfs_buf_log_item
*bip
= bp
->b_log_item
;
209 struct xfs_dir3_leaf_hdr
*hdr3
= bp
->b_addr
;
212 fa
= xfs_dir3_leaf_verify(bp
, magic
);
214 xfs_verifier_error(bp
, -EFSCORRUPTED
, fa
);
218 if (!xfs_sb_version_hascrc(&mp
->m_sb
))
222 hdr3
->info
.lsn
= cpu_to_be64(bip
->bli_item
.li_lsn
);
224 xfs_buf_update_cksum(bp
, XFS_DIR3_LEAF_CRC_OFF
);
227 static xfs_failaddr_t
228 xfs_dir3_leaf1_verify(
231 return xfs_dir3_leaf_verify(bp
, XFS_DIR2_LEAF1_MAGIC
);
235 xfs_dir3_leaf1_read_verify(
238 __read_verify(bp
, XFS_DIR2_LEAF1_MAGIC
);
242 xfs_dir3_leaf1_write_verify(
245 __write_verify(bp
, XFS_DIR2_LEAF1_MAGIC
);
248 static xfs_failaddr_t
249 xfs_dir3_leafn_verify(
252 return xfs_dir3_leaf_verify(bp
, XFS_DIR2_LEAFN_MAGIC
);
256 xfs_dir3_leafn_read_verify(
259 __read_verify(bp
, XFS_DIR2_LEAFN_MAGIC
);
263 xfs_dir3_leafn_write_verify(
266 __write_verify(bp
, XFS_DIR2_LEAFN_MAGIC
);
269 const struct xfs_buf_ops xfs_dir3_leaf1_buf_ops
= {
270 .name
= "xfs_dir3_leaf1",
271 .verify_read
= xfs_dir3_leaf1_read_verify
,
272 .verify_write
= xfs_dir3_leaf1_write_verify
,
273 .verify_struct
= xfs_dir3_leaf1_verify
,
276 const struct xfs_buf_ops xfs_dir3_leafn_buf_ops
= {
277 .name
= "xfs_dir3_leafn",
278 .verify_read
= xfs_dir3_leafn_read_verify
,
279 .verify_write
= xfs_dir3_leafn_write_verify
,
280 .verify_struct
= xfs_dir3_leafn_verify
,
285 struct xfs_trans
*tp
,
286 struct xfs_inode
*dp
,
288 xfs_daddr_t mappedbno
,
289 struct xfs_buf
**bpp
)
293 err
= xfs_da_read_buf(tp
, dp
, fbno
, mappedbno
, bpp
,
294 XFS_DATA_FORK
, &xfs_dir3_leaf1_buf_ops
);
295 if (!err
&& tp
&& *bpp
)
296 xfs_trans_buf_set_type(tp
, *bpp
, XFS_BLFT_DIR_LEAF1_BUF
);
302 struct xfs_trans
*tp
,
303 struct xfs_inode
*dp
,
305 xfs_daddr_t mappedbno
,
306 struct xfs_buf
**bpp
)
310 err
= xfs_da_read_buf(tp
, dp
, fbno
, mappedbno
, bpp
,
311 XFS_DATA_FORK
, &xfs_dir3_leafn_buf_ops
);
312 if (!err
&& tp
&& *bpp
)
313 xfs_trans_buf_set_type(tp
, *bpp
, XFS_BLFT_DIR_LEAFN_BUF
);
318 * Initialize a new leaf block, leaf1 or leafn magic accepted.
322 struct xfs_mount
*mp
,
323 struct xfs_trans
*tp
,
328 struct xfs_dir2_leaf
*leaf
= bp
->b_addr
;
330 ASSERT(type
== XFS_DIR2_LEAF1_MAGIC
|| type
== XFS_DIR2_LEAFN_MAGIC
);
332 if (xfs_sb_version_hascrc(&mp
->m_sb
)) {
333 struct xfs_dir3_leaf_hdr
*leaf3
= bp
->b_addr
;
335 memset(leaf3
, 0, sizeof(*leaf3
));
337 leaf3
->info
.hdr
.magic
= (type
== XFS_DIR2_LEAF1_MAGIC
)
338 ? cpu_to_be16(XFS_DIR3_LEAF1_MAGIC
)
339 : cpu_to_be16(XFS_DIR3_LEAFN_MAGIC
);
340 leaf3
->info
.blkno
= cpu_to_be64(bp
->b_bn
);
341 leaf3
->info
.owner
= cpu_to_be64(owner
);
342 uuid_copy(&leaf3
->info
.uuid
, &mp
->m_sb
.sb_meta_uuid
);
344 memset(leaf
, 0, sizeof(*leaf
));
345 leaf
->hdr
.info
.magic
= cpu_to_be16(type
);
349 * If it's a leaf-format directory initialize the tail.
350 * Caller is responsible for initialising the bests table.
352 if (type
== XFS_DIR2_LEAF1_MAGIC
) {
353 struct xfs_dir2_leaf_tail
*ltp
;
355 ltp
= xfs_dir2_leaf_tail_p(mp
->m_dir_geo
, leaf
);
357 bp
->b_ops
= &xfs_dir3_leaf1_buf_ops
;
358 xfs_trans_buf_set_type(tp
, bp
, XFS_BLFT_DIR_LEAF1_BUF
);
360 bp
->b_ops
= &xfs_dir3_leafn_buf_ops
;
361 xfs_trans_buf_set_type(tp
, bp
, XFS_BLFT_DIR_LEAFN_BUF
);
366 xfs_dir3_leaf_get_buf(
369 struct xfs_buf
**bpp
,
372 struct xfs_inode
*dp
= args
->dp
;
373 struct xfs_trans
*tp
= args
->trans
;
374 struct xfs_mount
*mp
= dp
->i_mount
;
378 ASSERT(magic
== XFS_DIR2_LEAF1_MAGIC
|| magic
== XFS_DIR2_LEAFN_MAGIC
);
379 ASSERT(bno
>= xfs_dir2_byte_to_db(args
->geo
, XFS_DIR2_LEAF_OFFSET
) &&
380 bno
< xfs_dir2_byte_to_db(args
->geo
, XFS_DIR2_FREE_OFFSET
));
382 error
= xfs_da_get_buf(tp
, dp
, xfs_dir2_db_to_da(args
->geo
, bno
),
383 -1, &bp
, XFS_DATA_FORK
);
387 xfs_dir3_leaf_init(mp
, tp
, bp
, dp
->i_ino
, magic
);
388 xfs_dir3_leaf_log_header(args
, bp
);
389 if (magic
== XFS_DIR2_LEAF1_MAGIC
)
390 xfs_dir3_leaf_log_tail(args
, bp
);
396 * Convert a block form directory to a leaf form directory.
399 xfs_dir2_block_to_leaf(
400 xfs_da_args_t
*args
, /* operation arguments */
401 struct xfs_buf
*dbp
) /* input block's buffer */
403 __be16
*bestsp
; /* leaf's bestsp entries */
404 xfs_dablk_t blkno
; /* leaf block's bno */
405 xfs_dir2_data_hdr_t
*hdr
; /* block header */
406 xfs_dir2_leaf_entry_t
*blp
; /* block's leaf entries */
407 xfs_dir2_block_tail_t
*btp
; /* block's tail */
408 xfs_inode_t
*dp
; /* incore directory inode */
409 int error
; /* error return code */
410 struct xfs_buf
*lbp
; /* leaf block's buffer */
411 xfs_dir2_db_t ldb
; /* leaf block's bno */
412 xfs_dir2_leaf_t
*leaf
; /* leaf structure */
413 xfs_dir2_leaf_tail_t
*ltp
; /* leaf's tail */
414 int needlog
; /* need to log block header */
415 int needscan
; /* need to rescan bestfree */
416 xfs_trans_t
*tp
; /* transaction pointer */
417 struct xfs_dir2_data_free
*bf
;
418 struct xfs_dir2_leaf_entry
*ents
;
419 struct xfs_dir3_icleaf_hdr leafhdr
;
421 trace_xfs_dir2_block_to_leaf(args
);
426 * Add the leaf block to the inode.
427 * This interface will only put blocks in the leaf/node range.
428 * Since that's empty now, we'll get the root (block 0 in range).
430 if ((error
= xfs_da_grow_inode(args
, &blkno
))) {
433 ldb
= xfs_dir2_da_to_db(args
->geo
, blkno
);
434 ASSERT(ldb
== xfs_dir2_byte_to_db(args
->geo
, XFS_DIR2_LEAF_OFFSET
));
436 * Initialize the leaf block, get a buffer for it.
438 error
= xfs_dir3_leaf_get_buf(args
, ldb
, &lbp
, XFS_DIR2_LEAF1_MAGIC
);
444 xfs_dir3_data_check(dp
, dbp
);
445 btp
= xfs_dir2_block_tail_p(args
->geo
, hdr
);
446 blp
= xfs_dir2_block_leaf_p(btp
);
447 bf
= dp
->d_ops
->data_bestfree_p(hdr
);
448 ents
= dp
->d_ops
->leaf_ents_p(leaf
);
451 * Set the counts in the leaf header.
453 dp
->d_ops
->leaf_hdr_from_disk(&leafhdr
, leaf
);
454 leafhdr
.count
= be32_to_cpu(btp
->count
);
455 leafhdr
.stale
= be32_to_cpu(btp
->stale
);
456 dp
->d_ops
->leaf_hdr_to_disk(leaf
, &leafhdr
);
457 xfs_dir3_leaf_log_header(args
, lbp
);
460 * Could compact these but I think we always do the conversion
461 * after squeezing out stale entries.
463 memcpy(ents
, blp
, be32_to_cpu(btp
->count
) * sizeof(xfs_dir2_leaf_entry_t
));
464 xfs_dir3_leaf_log_ents(args
, lbp
, 0, leafhdr
.count
- 1);
468 * Make the space formerly occupied by the leaf entries and block
471 xfs_dir2_data_make_free(args
, dbp
,
472 (xfs_dir2_data_aoff_t
)((char *)blp
- (char *)hdr
),
473 (xfs_dir2_data_aoff_t
)((char *)hdr
+ args
->geo
->blksize
-
475 &needlog
, &needscan
);
477 * Fix up the block header, make it a data block.
479 dbp
->b_ops
= &xfs_dir3_data_buf_ops
;
480 xfs_trans_buf_set_type(tp
, dbp
, XFS_BLFT_DIR_DATA_BUF
);
481 if (hdr
->magic
== cpu_to_be32(XFS_DIR2_BLOCK_MAGIC
))
482 hdr
->magic
= cpu_to_be32(XFS_DIR2_DATA_MAGIC
);
484 hdr
->magic
= cpu_to_be32(XFS_DIR3_DATA_MAGIC
);
487 xfs_dir2_data_freescan(dp
, hdr
, &needlog
);
489 * Set up leaf tail and bests table.
491 ltp
= xfs_dir2_leaf_tail_p(args
->geo
, leaf
);
492 ltp
->bestcount
= cpu_to_be32(1);
493 bestsp
= xfs_dir2_leaf_bests_p(ltp
);
494 bestsp
[0] = bf
[0].length
;
496 * Log the data header and leaf bests table.
499 xfs_dir2_data_log_header(args
, dbp
);
500 xfs_dir3_leaf_check(dp
, lbp
);
501 xfs_dir3_data_check(dp
, dbp
);
502 xfs_dir3_leaf_log_bests(args
, lbp
, 0, 0);
507 xfs_dir3_leaf_find_stale(
508 struct xfs_dir3_icleaf_hdr
*leafhdr
,
509 struct xfs_dir2_leaf_entry
*ents
,
515 * Find the first stale entry before our index, if any.
517 for (*lowstale
= index
- 1; *lowstale
>= 0; --*lowstale
) {
518 if (ents
[*lowstale
].address
==
519 cpu_to_be32(XFS_DIR2_NULL_DATAPTR
))
524 * Find the first stale entry at or after our index, if any.
525 * Stop if the result would require moving more entries than using
528 for (*highstale
= index
; *highstale
< leafhdr
->count
; ++*highstale
) {
529 if (ents
[*highstale
].address
==
530 cpu_to_be32(XFS_DIR2_NULL_DATAPTR
))
532 if (*lowstale
>= 0 && index
- *lowstale
<= *highstale
- index
)
537 struct xfs_dir2_leaf_entry
*
538 xfs_dir3_leaf_find_entry(
539 struct xfs_dir3_icleaf_hdr
*leafhdr
,
540 struct xfs_dir2_leaf_entry
*ents
,
541 int index
, /* leaf table position */
542 int compact
, /* need to compact leaves */
543 int lowstale
, /* index of prev stale leaf */
544 int highstale
, /* index of next stale leaf */
545 int *lfloglow
, /* low leaf logging index */
546 int *lfloghigh
) /* high leaf logging index */
548 if (!leafhdr
->stale
) {
549 xfs_dir2_leaf_entry_t
*lep
; /* leaf entry table pointer */
552 * Now we need to make room to insert the leaf entry.
554 * If there are no stale entries, just insert a hole at index.
557 if (index
< leafhdr
->count
)
558 memmove(lep
+ 1, lep
,
559 (leafhdr
->count
- index
) * sizeof(*lep
));
562 * Record low and high logging indices for the leaf.
565 *lfloghigh
= leafhdr
->count
++;
570 * There are stale entries.
572 * We will use one of them for the new entry. It's probably not at
573 * the right location, so we'll have to shift some up or down first.
575 * If we didn't compact before, we need to find the nearest stale
576 * entries before and after our insertion point.
579 xfs_dir3_leaf_find_stale(leafhdr
, ents
, index
,
580 &lowstale
, &highstale
);
583 * If the low one is better, use it.
586 (highstale
== leafhdr
->count
||
587 index
- lowstale
- 1 < highstale
- index
)) {
588 ASSERT(index
- lowstale
- 1 >= 0);
589 ASSERT(ents
[lowstale
].address
==
590 cpu_to_be32(XFS_DIR2_NULL_DATAPTR
));
593 * Copy entries up to cover the stale entry and make room
596 if (index
- lowstale
- 1 > 0) {
597 memmove(&ents
[lowstale
], &ents
[lowstale
+ 1],
598 (index
- lowstale
- 1) *
599 sizeof(xfs_dir2_leaf_entry_t
));
601 *lfloglow
= MIN(lowstale
, *lfloglow
);
602 *lfloghigh
= MAX(index
- 1, *lfloghigh
);
604 return &ents
[index
- 1];
608 * The high one is better, so use that one.
610 ASSERT(highstale
- index
>= 0);
611 ASSERT(ents
[highstale
].address
== cpu_to_be32(XFS_DIR2_NULL_DATAPTR
));
614 * Copy entries down to cover the stale entry and make room for the
617 if (highstale
- index
> 0) {
618 memmove(&ents
[index
+ 1], &ents
[index
],
619 (highstale
- index
) * sizeof(xfs_dir2_leaf_entry_t
));
621 *lfloglow
= MIN(index
, *lfloglow
);
622 *lfloghigh
= MAX(highstale
, *lfloghigh
);
628 * Add an entry to a leaf form directory.
631 xfs_dir2_leaf_addname(
632 xfs_da_args_t
*args
) /* operation arguments */
634 __be16
*bestsp
; /* freespace table in leaf */
635 int compact
; /* need to compact leaves */
636 xfs_dir2_data_hdr_t
*hdr
; /* data block header */
637 struct xfs_buf
*dbp
; /* data block buffer */
638 xfs_dir2_data_entry_t
*dep
; /* data block entry */
639 xfs_inode_t
*dp
; /* incore directory inode */
640 xfs_dir2_data_unused_t
*dup
; /* data unused entry */
641 int error
; /* error return value */
642 int grown
; /* allocated new data block */
643 int highstale
; /* index of next stale leaf */
644 int i
; /* temporary, index */
645 int index
; /* leaf table position */
646 struct xfs_buf
*lbp
; /* leaf's buffer */
647 xfs_dir2_leaf_t
*leaf
; /* leaf structure */
648 int length
; /* length of new entry */
649 xfs_dir2_leaf_entry_t
*lep
; /* leaf entry table pointer */
650 int lfloglow
; /* low leaf logging index */
651 int lfloghigh
; /* high leaf logging index */
652 int lowstale
; /* index of prev stale leaf */
653 xfs_dir2_leaf_tail_t
*ltp
; /* leaf tail pointer */
654 int needbytes
; /* leaf block bytes needed */
655 int needlog
; /* need to log data header */
656 int needscan
; /* need to rescan data free */
657 __be16
*tagp
; /* end of data entry */
658 xfs_trans_t
*tp
; /* transaction pointer */
659 xfs_dir2_db_t use_block
; /* data block number */
660 struct xfs_dir2_data_free
*bf
; /* bestfree table */
661 struct xfs_dir2_leaf_entry
*ents
;
662 struct xfs_dir3_icleaf_hdr leafhdr
;
664 trace_xfs_dir2_leaf_addname(args
);
669 error
= xfs_dir3_leaf_read(tp
, dp
, args
->geo
->leafblk
, -1, &lbp
);
674 * Look up the entry by hash value and name.
675 * We know it's not there, our caller has already done a lookup.
676 * So the index is of the entry to insert in front of.
677 * But if there are dup hash values the index is of the first of those.
679 index
= xfs_dir2_leaf_search_hash(args
, lbp
);
681 ltp
= xfs_dir2_leaf_tail_p(args
->geo
, leaf
);
682 ents
= dp
->d_ops
->leaf_ents_p(leaf
);
683 dp
->d_ops
->leaf_hdr_from_disk(&leafhdr
, leaf
);
684 bestsp
= xfs_dir2_leaf_bests_p(ltp
);
685 length
= dp
->d_ops
->data_entsize(args
->namelen
);
688 * See if there are any entries with the same hash value
689 * and space in their block for the new entry.
690 * This is good because it puts multiple same-hash value entries
691 * in a data block, improving the lookup of those entries.
693 for (use_block
= -1, lep
= &ents
[index
];
694 index
< leafhdr
.count
&& be32_to_cpu(lep
->hashval
) == args
->hashval
;
696 if (be32_to_cpu(lep
->address
) == XFS_DIR2_NULL_DATAPTR
)
698 i
= xfs_dir2_dataptr_to_db(args
->geo
, be32_to_cpu(lep
->address
));
699 ASSERT(i
< be32_to_cpu(ltp
->bestcount
));
700 ASSERT(bestsp
[i
] != cpu_to_be16(NULLDATAOFF
));
701 if (be16_to_cpu(bestsp
[i
]) >= length
) {
707 * Didn't find a block yet, linear search all the data blocks.
709 if (use_block
== -1) {
710 for (i
= 0; i
< be32_to_cpu(ltp
->bestcount
); i
++) {
712 * Remember a block we see that's missing.
714 if (bestsp
[i
] == cpu_to_be16(NULLDATAOFF
) &&
717 else if (be16_to_cpu(bestsp
[i
]) >= length
) {
724 * How many bytes do we need in the leaf block?
728 needbytes
+= sizeof(xfs_dir2_leaf_entry_t
);
730 needbytes
+= sizeof(xfs_dir2_data_off_t
);
733 * Now kill use_block if it refers to a missing block, so we
734 * can use it as an indication of allocation needed.
736 if (use_block
!= -1 && bestsp
[use_block
] == cpu_to_be16(NULLDATAOFF
))
739 * If we don't have enough free bytes but we can make enough
740 * by compacting out stale entries, we'll do that.
742 if ((char *)bestsp
- (char *)&ents
[leafhdr
.count
] < needbytes
&&
747 * Otherwise if we don't have enough free bytes we need to
748 * convert to node form.
750 else if ((char *)bestsp
- (char *)&ents
[leafhdr
.count
] < needbytes
) {
752 * Just checking or no space reservation, give up.
754 if ((args
->op_flags
& XFS_DA_OP_JUSTCHECK
) ||
756 xfs_trans_brelse(tp
, lbp
);
760 * Convert to node form.
762 error
= xfs_dir2_leaf_to_node(args
, lbp
);
766 * Then add the new entry.
768 return xfs_dir2_node_addname(args
);
771 * Otherwise it will fit without compaction.
776 * If just checking, then it will fit unless we needed to allocate
779 if (args
->op_flags
& XFS_DA_OP_JUSTCHECK
) {
780 xfs_trans_brelse(tp
, lbp
);
781 return use_block
== -1 ? -ENOSPC
: 0;
784 * If no allocations are allowed, return now before we've
787 if (args
->total
== 0 && use_block
== -1) {
788 xfs_trans_brelse(tp
, lbp
);
792 * Need to compact the leaf entries, removing stale ones.
793 * Leave one stale entry behind - the one closest to our
794 * insertion index - and we'll shift that one to our insertion
798 xfs_dir3_leaf_compact_x1(&leafhdr
, ents
, &index
, &lowstale
,
799 &highstale
, &lfloglow
, &lfloghigh
);
802 * There are stale entries, so we'll need log-low and log-high
803 * impossibly bad values later.
805 else if (leafhdr
.stale
) {
806 lfloglow
= leafhdr
.count
;
810 * If there was no data block space found, we need to allocate
813 if (use_block
== -1) {
815 * Add the new data block.
817 if ((error
= xfs_dir2_grow_inode(args
, XFS_DIR2_DATA_SPACE
,
819 xfs_trans_brelse(tp
, lbp
);
823 * Initialize the block.
825 if ((error
= xfs_dir3_data_init(args
, use_block
, &dbp
))) {
826 xfs_trans_brelse(tp
, lbp
);
830 * If we're adding a new data block on the end we need to
831 * extend the bests table. Copy it up one entry.
833 if (use_block
>= be32_to_cpu(ltp
->bestcount
)) {
835 memmove(&bestsp
[0], &bestsp
[1],
836 be32_to_cpu(ltp
->bestcount
) * sizeof(bestsp
[0]));
837 be32_add_cpu(<p
->bestcount
, 1);
838 xfs_dir3_leaf_log_tail(args
, lbp
);
839 xfs_dir3_leaf_log_bests(args
, lbp
, 0,
840 be32_to_cpu(ltp
->bestcount
) - 1);
843 * If we're filling in a previously empty block just log it.
846 xfs_dir3_leaf_log_bests(args
, lbp
, use_block
, use_block
);
848 bf
= dp
->d_ops
->data_bestfree_p(hdr
);
849 bestsp
[use_block
] = bf
[0].length
;
853 * Already had space in some data block.
854 * Just read that one in.
856 error
= xfs_dir3_data_read(tp
, dp
,
857 xfs_dir2_db_to_da(args
->geo
, use_block
),
860 xfs_trans_brelse(tp
, lbp
);
864 bf
= dp
->d_ops
->data_bestfree_p(hdr
);
868 * Point to the biggest freespace in our data block.
870 dup
= (xfs_dir2_data_unused_t
*)
871 ((char *)hdr
+ be16_to_cpu(bf
[0].offset
));
872 ASSERT(be16_to_cpu(dup
->length
) >= length
);
873 needscan
= needlog
= 0;
875 * Mark the initial part of our freespace in use for the new entry.
877 error
= xfs_dir2_data_use_free(args
, dbp
, dup
,
878 (xfs_dir2_data_aoff_t
)((char *)dup
- (char *)hdr
),
879 length
, &needlog
, &needscan
);
881 xfs_trans_brelse(tp
, lbp
);
885 * Initialize our new entry (at last).
887 dep
= (xfs_dir2_data_entry_t
*)dup
;
888 dep
->inumber
= cpu_to_be64(args
->inumber
);
889 dep
->namelen
= args
->namelen
;
890 memcpy(dep
->name
, args
->name
, dep
->namelen
);
891 dp
->d_ops
->data_put_ftype(dep
, args
->filetype
);
892 tagp
= dp
->d_ops
->data_entry_tag_p(dep
);
893 *tagp
= cpu_to_be16((char *)dep
- (char *)hdr
);
895 * Need to scan fix up the bestfree table.
898 xfs_dir2_data_freescan(dp
, hdr
, &needlog
);
900 * Need to log the data block's header.
903 xfs_dir2_data_log_header(args
, dbp
);
904 xfs_dir2_data_log_entry(args
, dbp
, dep
);
906 * If the bests table needs to be changed, do it.
907 * Log the change unless we've already done that.
909 if (be16_to_cpu(bestsp
[use_block
]) != be16_to_cpu(bf
[0].length
)) {
910 bestsp
[use_block
] = bf
[0].length
;
912 xfs_dir3_leaf_log_bests(args
, lbp
, use_block
, use_block
);
915 lep
= xfs_dir3_leaf_find_entry(&leafhdr
, ents
, index
, compact
, lowstale
,
916 highstale
, &lfloglow
, &lfloghigh
);
919 * Fill in the new leaf entry.
921 lep
->hashval
= cpu_to_be32(args
->hashval
);
922 lep
->address
= cpu_to_be32(
923 xfs_dir2_db_off_to_dataptr(args
->geo
, use_block
,
924 be16_to_cpu(*tagp
)));
926 * Log the leaf fields and give up the buffers.
928 dp
->d_ops
->leaf_hdr_to_disk(leaf
, &leafhdr
);
929 xfs_dir3_leaf_log_header(args
, lbp
);
930 xfs_dir3_leaf_log_ents(args
, lbp
, lfloglow
, lfloghigh
);
931 xfs_dir3_leaf_check(dp
, lbp
);
932 xfs_dir3_data_check(dp
, dbp
);
937 * Compact out any stale entries in the leaf.
938 * Log the header and changed leaf entries, if any.
941 xfs_dir3_leaf_compact(
942 xfs_da_args_t
*args
, /* operation arguments */
943 struct xfs_dir3_icleaf_hdr
*leafhdr
,
944 struct xfs_buf
*bp
) /* leaf buffer */
946 int from
; /* source leaf index */
947 xfs_dir2_leaf_t
*leaf
; /* leaf structure */
948 int loglow
; /* first leaf entry to log */
949 int to
; /* target leaf index */
950 struct xfs_dir2_leaf_entry
*ents
;
951 struct xfs_inode
*dp
= args
->dp
;
958 * Compress out the stale entries in place.
960 ents
= dp
->d_ops
->leaf_ents_p(leaf
);
961 for (from
= to
= 0, loglow
= -1; from
< leafhdr
->count
; from
++) {
962 if (ents
[from
].address
== cpu_to_be32(XFS_DIR2_NULL_DATAPTR
))
965 * Only actually copy the entries that are different.
970 ents
[to
] = ents
[from
];
975 * Update and log the header, log the leaf entries.
977 ASSERT(leafhdr
->stale
== from
- to
);
978 leafhdr
->count
-= leafhdr
->stale
;
981 dp
->d_ops
->leaf_hdr_to_disk(leaf
, leafhdr
);
982 xfs_dir3_leaf_log_header(args
, bp
);
984 xfs_dir3_leaf_log_ents(args
, bp
, loglow
, to
- 1);
988 * Compact the leaf entries, removing stale ones.
989 * Leave one stale entry behind - the one closest to our
990 * insertion index - and the caller will shift that one to our insertion
992 * Return new insertion index, where the remaining stale entry is,
993 * and leaf logging indices.
996 xfs_dir3_leaf_compact_x1(
997 struct xfs_dir3_icleaf_hdr
*leafhdr
,
998 struct xfs_dir2_leaf_entry
*ents
,
999 int *indexp
, /* insertion index */
1000 int *lowstalep
, /* out: stale entry before us */
1001 int *highstalep
, /* out: stale entry after us */
1002 int *lowlogp
, /* out: low log index */
1003 int *highlogp
) /* out: high log index */
1005 int from
; /* source copy index */
1006 int highstale
; /* stale entry at/after index */
1007 int index
; /* insertion index */
1008 int keepstale
; /* source index of kept stale */
1009 int lowstale
; /* stale entry before index */
1010 int newindex
=0; /* new insertion index */
1011 int to
; /* destination copy index */
1013 ASSERT(leafhdr
->stale
> 1);
1016 xfs_dir3_leaf_find_stale(leafhdr
, ents
, index
, &lowstale
, &highstale
);
1019 * Pick the better of lowstale and highstale.
1021 if (lowstale
>= 0 &&
1022 (highstale
== leafhdr
->count
||
1023 index
- lowstale
<= highstale
- index
))
1024 keepstale
= lowstale
;
1026 keepstale
= highstale
;
1028 * Copy the entries in place, removing all the stale entries
1031 for (from
= to
= 0; from
< leafhdr
->count
; from
++) {
1033 * Notice the new value of index.
1037 if (from
!= keepstale
&&
1038 ents
[from
].address
== cpu_to_be32(XFS_DIR2_NULL_DATAPTR
)) {
1044 * Record the new keepstale value for the insertion.
1046 if (from
== keepstale
)
1047 lowstale
= highstale
= to
;
1049 * Copy only the entries that have moved.
1052 ents
[to
] = ents
[from
];
1057 * If the insertion point was past the last entry,
1058 * set the new insertion point accordingly.
1064 * Adjust the leaf header values.
1066 leafhdr
->count
-= from
- to
;
1069 * Remember the low/high stale value only in the "right"
1072 if (lowstale
>= newindex
)
1075 highstale
= leafhdr
->count
;
1076 *highlogp
= leafhdr
->count
- 1;
1077 *lowstalep
= lowstale
;
1078 *highstalep
= highstale
;
1082 * Log the bests entries indicated from a leaf1 block.
1085 xfs_dir3_leaf_log_bests(
1086 struct xfs_da_args
*args
,
1087 struct xfs_buf
*bp
, /* leaf buffer */
1088 int first
, /* first entry to log */
1089 int last
) /* last entry to log */
1091 __be16
*firstb
; /* pointer to first entry */
1092 __be16
*lastb
; /* pointer to last entry */
1093 struct xfs_dir2_leaf
*leaf
= bp
->b_addr
;
1094 xfs_dir2_leaf_tail_t
*ltp
; /* leaf tail structure */
1096 ASSERT(leaf
->hdr
.info
.magic
== cpu_to_be16(XFS_DIR2_LEAF1_MAGIC
) ||
1097 leaf
->hdr
.info
.magic
== cpu_to_be16(XFS_DIR3_LEAF1_MAGIC
));
1099 ltp
= xfs_dir2_leaf_tail_p(args
->geo
, leaf
);
1100 firstb
= xfs_dir2_leaf_bests_p(ltp
) + first
;
1101 lastb
= xfs_dir2_leaf_bests_p(ltp
) + last
;
1102 xfs_trans_log_buf(args
->trans
, bp
,
1103 (uint
)((char *)firstb
- (char *)leaf
),
1104 (uint
)((char *)lastb
- (char *)leaf
+ sizeof(*lastb
) - 1));
1108 * Log the leaf entries indicated from a leaf1 or leafn block.
1111 xfs_dir3_leaf_log_ents(
1112 struct xfs_da_args
*args
,
1117 xfs_dir2_leaf_entry_t
*firstlep
; /* pointer to first entry */
1118 xfs_dir2_leaf_entry_t
*lastlep
; /* pointer to last entry */
1119 struct xfs_dir2_leaf
*leaf
= bp
->b_addr
;
1120 struct xfs_dir2_leaf_entry
*ents
;
1122 ASSERT(leaf
->hdr
.info
.magic
== cpu_to_be16(XFS_DIR2_LEAF1_MAGIC
) ||
1123 leaf
->hdr
.info
.magic
== cpu_to_be16(XFS_DIR3_LEAF1_MAGIC
) ||
1124 leaf
->hdr
.info
.magic
== cpu_to_be16(XFS_DIR2_LEAFN_MAGIC
) ||
1125 leaf
->hdr
.info
.magic
== cpu_to_be16(XFS_DIR3_LEAFN_MAGIC
));
1127 ents
= args
->dp
->d_ops
->leaf_ents_p(leaf
);
1128 firstlep
= &ents
[first
];
1129 lastlep
= &ents
[last
];
1130 xfs_trans_log_buf(args
->trans
, bp
,
1131 (uint
)((char *)firstlep
- (char *)leaf
),
1132 (uint
)((char *)lastlep
- (char *)leaf
+ sizeof(*lastlep
) - 1));
1136 * Log the header of the leaf1 or leafn block.
1139 xfs_dir3_leaf_log_header(
1140 struct xfs_da_args
*args
,
1143 struct xfs_dir2_leaf
*leaf
= bp
->b_addr
;
1145 ASSERT(leaf
->hdr
.info
.magic
== cpu_to_be16(XFS_DIR2_LEAF1_MAGIC
) ||
1146 leaf
->hdr
.info
.magic
== cpu_to_be16(XFS_DIR3_LEAF1_MAGIC
) ||
1147 leaf
->hdr
.info
.magic
== cpu_to_be16(XFS_DIR2_LEAFN_MAGIC
) ||
1148 leaf
->hdr
.info
.magic
== cpu_to_be16(XFS_DIR3_LEAFN_MAGIC
));
1150 xfs_trans_log_buf(args
->trans
, bp
,
1151 (uint
)((char *)&leaf
->hdr
- (char *)leaf
),
1152 args
->dp
->d_ops
->leaf_hdr_size
- 1);
1156 * Log the tail of the leaf1 block.
1159 xfs_dir3_leaf_log_tail(
1160 struct xfs_da_args
*args
,
1163 struct xfs_dir2_leaf
*leaf
= bp
->b_addr
;
1164 xfs_dir2_leaf_tail_t
*ltp
; /* leaf tail structure */
1166 ASSERT(leaf
->hdr
.info
.magic
== cpu_to_be16(XFS_DIR2_LEAF1_MAGIC
) ||
1167 leaf
->hdr
.info
.magic
== cpu_to_be16(XFS_DIR3_LEAF1_MAGIC
) ||
1168 leaf
->hdr
.info
.magic
== cpu_to_be16(XFS_DIR2_LEAFN_MAGIC
) ||
1169 leaf
->hdr
.info
.magic
== cpu_to_be16(XFS_DIR3_LEAFN_MAGIC
));
1171 ltp
= xfs_dir2_leaf_tail_p(args
->geo
, leaf
);
1172 xfs_trans_log_buf(args
->trans
, bp
, (uint
)((char *)ltp
- (char *)leaf
),
1173 (uint
)(args
->geo
->blksize
- 1));
1177 * Look up the entry referred to by args in the leaf format directory.
1178 * Most of the work is done by the xfs_dir2_leaf_lookup_int routine which
1179 * is also used by the node-format code.
1182 xfs_dir2_leaf_lookup(
1183 xfs_da_args_t
*args
) /* operation arguments */
1185 struct xfs_buf
*dbp
; /* data block buffer */
1186 xfs_dir2_data_entry_t
*dep
; /* data block entry */
1187 xfs_inode_t
*dp
; /* incore directory inode */
1188 int error
; /* error return code */
1189 int index
; /* found entry index */
1190 struct xfs_buf
*lbp
; /* leaf buffer */
1191 xfs_dir2_leaf_t
*leaf
; /* leaf structure */
1192 xfs_dir2_leaf_entry_t
*lep
; /* leaf entry */
1193 xfs_trans_t
*tp
; /* transaction pointer */
1194 struct xfs_dir2_leaf_entry
*ents
;
1196 trace_xfs_dir2_leaf_lookup(args
);
1199 * Look up name in the leaf block, returning both buffers and index.
1201 if ((error
= xfs_dir2_leaf_lookup_int(args
, &lbp
, &index
, &dbp
))) {
1206 xfs_dir3_leaf_check(dp
, lbp
);
1208 ents
= dp
->d_ops
->leaf_ents_p(leaf
);
1210 * Get to the leaf entry and contained data entry address.
1215 * Point to the data entry.
1217 dep
= (xfs_dir2_data_entry_t
*)
1218 ((char *)dbp
->b_addr
+
1219 xfs_dir2_dataptr_to_off(args
->geo
, be32_to_cpu(lep
->address
)));
1221 * Return the found inode number & CI name if appropriate
1223 args
->inumber
= be64_to_cpu(dep
->inumber
);
1224 args
->filetype
= dp
->d_ops
->data_get_ftype(dep
);
1225 error
= xfs_dir_cilookup_result(args
, dep
->name
, dep
->namelen
);
1226 xfs_trans_brelse(tp
, dbp
);
1227 xfs_trans_brelse(tp
, lbp
);
1232 * Look up name/hash in the leaf block.
1233 * Fill in indexp with the found index, and dbpp with the data buffer.
1234 * If not found dbpp will be NULL, and ENOENT comes back.
1235 * lbpp will always be filled in with the leaf buffer unless there's an error.
1237 static int /* error */
1238 xfs_dir2_leaf_lookup_int(
1239 xfs_da_args_t
*args
, /* operation arguments */
1240 struct xfs_buf
**lbpp
, /* out: leaf buffer */
1241 int *indexp
, /* out: index in leaf block */
1242 struct xfs_buf
**dbpp
) /* out: data buffer */
1244 xfs_dir2_db_t curdb
= -1; /* current data block number */
1245 struct xfs_buf
*dbp
= NULL
; /* data buffer */
1246 xfs_dir2_data_entry_t
*dep
; /* data entry */
1247 xfs_inode_t
*dp
; /* incore directory inode */
1248 int error
; /* error return code */
1249 int index
; /* index in leaf block */
1250 struct xfs_buf
*lbp
; /* leaf buffer */
1251 xfs_dir2_leaf_entry_t
*lep
; /* leaf entry */
1252 xfs_dir2_leaf_t
*leaf
; /* leaf structure */
1253 xfs_mount_t
*mp
; /* filesystem mount point */
1254 xfs_dir2_db_t newdb
; /* new data block number */
1255 xfs_trans_t
*tp
; /* transaction pointer */
1256 xfs_dir2_db_t cidb
= -1; /* case match data block no. */
1257 enum xfs_dacmp cmp
; /* name compare result */
1258 struct xfs_dir2_leaf_entry
*ents
;
1259 struct xfs_dir3_icleaf_hdr leafhdr
;
1265 error
= xfs_dir3_leaf_read(tp
, dp
, args
->geo
->leafblk
, -1, &lbp
);
1271 xfs_dir3_leaf_check(dp
, lbp
);
1272 ents
= dp
->d_ops
->leaf_ents_p(leaf
);
1273 dp
->d_ops
->leaf_hdr_from_disk(&leafhdr
, leaf
);
1276 * Look for the first leaf entry with our hash value.
1278 index
= xfs_dir2_leaf_search_hash(args
, lbp
);
1280 * Loop over all the entries with the right hash value
1281 * looking to match the name.
1283 for (lep
= &ents
[index
];
1284 index
< leafhdr
.count
&& be32_to_cpu(lep
->hashval
) == args
->hashval
;
1287 * Skip over stale leaf entries.
1289 if (be32_to_cpu(lep
->address
) == XFS_DIR2_NULL_DATAPTR
)
1292 * Get the new data block number.
1294 newdb
= xfs_dir2_dataptr_to_db(args
->geo
,
1295 be32_to_cpu(lep
->address
));
1297 * If it's not the same as the old data block number,
1298 * need to pitch the old one and read the new one.
1300 if (newdb
!= curdb
) {
1302 xfs_trans_brelse(tp
, dbp
);
1303 error
= xfs_dir3_data_read(tp
, dp
,
1304 xfs_dir2_db_to_da(args
->geo
, newdb
),
1307 xfs_trans_brelse(tp
, lbp
);
1313 * Point to the data entry.
1315 dep
= (xfs_dir2_data_entry_t
*)((char *)dbp
->b_addr
+
1316 xfs_dir2_dataptr_to_off(args
->geo
,
1317 be32_to_cpu(lep
->address
)));
1319 * Compare name and if it's an exact match, return the index
1320 * and buffer. If it's the first case-insensitive match, store
1321 * the index and buffer and continue looking for an exact match.
1323 cmp
= mp
->m_dirnameops
->compname(args
, dep
->name
, dep
->namelen
);
1324 if (cmp
!= XFS_CMP_DIFFERENT
&& cmp
!= args
->cmpresult
) {
1325 args
->cmpresult
= cmp
;
1327 /* case exact match: return the current buffer. */
1328 if (cmp
== XFS_CMP_EXACT
) {
1335 ASSERT(args
->op_flags
& XFS_DA_OP_OKNOENT
);
1337 * Here, we can only be doing a lookup (not a rename or remove).
1338 * If a case-insensitive match was found earlier, re-read the
1339 * appropriate data block if required and return it.
1341 if (args
->cmpresult
== XFS_CMP_CASE
) {
1343 if (cidb
!= curdb
) {
1344 xfs_trans_brelse(tp
, dbp
);
1345 error
= xfs_dir3_data_read(tp
, dp
,
1346 xfs_dir2_db_to_da(args
->geo
, cidb
),
1349 xfs_trans_brelse(tp
, lbp
);
1357 * No match found, return -ENOENT.
1361 xfs_trans_brelse(tp
, dbp
);
1362 xfs_trans_brelse(tp
, lbp
);
1367 * Remove an entry from a leaf format directory.
1370 xfs_dir2_leaf_removename(
1371 xfs_da_args_t
*args
) /* operation arguments */
1373 __be16
*bestsp
; /* leaf block best freespace */
1374 xfs_dir2_data_hdr_t
*hdr
; /* data block header */
1375 xfs_dir2_db_t db
; /* data block number */
1376 struct xfs_buf
*dbp
; /* data block buffer */
1377 xfs_dir2_data_entry_t
*dep
; /* data entry structure */
1378 xfs_inode_t
*dp
; /* incore directory inode */
1379 int error
; /* error return code */
1380 xfs_dir2_db_t i
; /* temporary data block # */
1381 int index
; /* index into leaf entries */
1382 struct xfs_buf
*lbp
; /* leaf buffer */
1383 xfs_dir2_leaf_t
*leaf
; /* leaf structure */
1384 xfs_dir2_leaf_entry_t
*lep
; /* leaf entry */
1385 xfs_dir2_leaf_tail_t
*ltp
; /* leaf tail structure */
1386 int needlog
; /* need to log data header */
1387 int needscan
; /* need to rescan data frees */
1388 xfs_dir2_data_off_t oldbest
; /* old value of best free */
1389 struct xfs_dir2_data_free
*bf
; /* bestfree table */
1390 struct xfs_dir2_leaf_entry
*ents
;
1391 struct xfs_dir3_icleaf_hdr leafhdr
;
1393 trace_xfs_dir2_leaf_removename(args
);
1396 * Lookup the leaf entry, get the leaf and data blocks read in.
1398 if ((error
= xfs_dir2_leaf_lookup_int(args
, &lbp
, &index
, &dbp
))) {
1404 xfs_dir3_data_check(dp
, dbp
);
1405 bf
= dp
->d_ops
->data_bestfree_p(hdr
);
1406 dp
->d_ops
->leaf_hdr_from_disk(&leafhdr
, leaf
);
1407 ents
= dp
->d_ops
->leaf_ents_p(leaf
);
1409 * Point to the leaf entry, use that to point to the data entry.
1412 db
= xfs_dir2_dataptr_to_db(args
->geo
, be32_to_cpu(lep
->address
));
1413 dep
= (xfs_dir2_data_entry_t
*)((char *)hdr
+
1414 xfs_dir2_dataptr_to_off(args
->geo
, be32_to_cpu(lep
->address
)));
1415 needscan
= needlog
= 0;
1416 oldbest
= be16_to_cpu(bf
[0].length
);
1417 ltp
= xfs_dir2_leaf_tail_p(args
->geo
, leaf
);
1418 bestsp
= xfs_dir2_leaf_bests_p(ltp
);
1419 if (be16_to_cpu(bestsp
[db
]) != oldbest
)
1420 return -EFSCORRUPTED
;
1422 * Mark the former data entry unused.
1424 xfs_dir2_data_make_free(args
, dbp
,
1425 (xfs_dir2_data_aoff_t
)((char *)dep
- (char *)hdr
),
1426 dp
->d_ops
->data_entsize(dep
->namelen
), &needlog
, &needscan
);
1428 * We just mark the leaf entry stale by putting a null in it.
1431 dp
->d_ops
->leaf_hdr_to_disk(leaf
, &leafhdr
);
1432 xfs_dir3_leaf_log_header(args
, lbp
);
1434 lep
->address
= cpu_to_be32(XFS_DIR2_NULL_DATAPTR
);
1435 xfs_dir3_leaf_log_ents(args
, lbp
, index
, index
);
1438 * Scan the freespace in the data block again if necessary,
1439 * log the data block header if necessary.
1442 xfs_dir2_data_freescan(dp
, hdr
, &needlog
);
1444 xfs_dir2_data_log_header(args
, dbp
);
1446 * If the longest freespace in the data block has changed,
1447 * put the new value in the bests table and log that.
1449 if (be16_to_cpu(bf
[0].length
) != oldbest
) {
1450 bestsp
[db
] = bf
[0].length
;
1451 xfs_dir3_leaf_log_bests(args
, lbp
, db
, db
);
1453 xfs_dir3_data_check(dp
, dbp
);
1455 * If the data block is now empty then get rid of the data block.
1457 if (be16_to_cpu(bf
[0].length
) ==
1458 args
->geo
->blksize
- dp
->d_ops
->data_entry_offset
) {
1459 ASSERT(db
!= args
->geo
->datablk
);
1460 if ((error
= xfs_dir2_shrink_inode(args
, db
, dbp
))) {
1462 * Nope, can't get rid of it because it caused
1463 * allocation of a bmap btree block to do so.
1464 * Just go on, returning success, leaving the
1465 * empty block in place.
1467 if (error
== -ENOSPC
&& args
->total
== 0)
1469 xfs_dir3_leaf_check(dp
, lbp
);
1474 * If this is the last data block then compact the
1475 * bests table by getting rid of entries.
1477 if (db
== be32_to_cpu(ltp
->bestcount
) - 1) {
1479 * Look for the last active entry (i).
1481 for (i
= db
- 1; i
> 0; i
--) {
1482 if (bestsp
[i
] != cpu_to_be16(NULLDATAOFF
))
1486 * Copy the table down so inactive entries at the
1489 memmove(&bestsp
[db
- i
], bestsp
,
1490 (be32_to_cpu(ltp
->bestcount
) - (db
- i
)) * sizeof(*bestsp
));
1491 be32_add_cpu(<p
->bestcount
, -(db
- i
));
1492 xfs_dir3_leaf_log_tail(args
, lbp
);
1493 xfs_dir3_leaf_log_bests(args
, lbp
, 0,
1494 be32_to_cpu(ltp
->bestcount
) - 1);
1496 bestsp
[db
] = cpu_to_be16(NULLDATAOFF
);
1499 * If the data block was not the first one, drop it.
1501 else if (db
!= args
->geo
->datablk
)
1504 xfs_dir3_leaf_check(dp
, lbp
);
1506 * See if we can convert to block form.
1508 return xfs_dir2_leaf_to_block(args
, lbp
, dbp
);
1512 * Replace the inode number in a leaf format directory entry.
1515 xfs_dir2_leaf_replace(
1516 xfs_da_args_t
*args
) /* operation arguments */
1518 struct xfs_buf
*dbp
; /* data block buffer */
1519 xfs_dir2_data_entry_t
*dep
; /* data block entry */
1520 xfs_inode_t
*dp
; /* incore directory inode */
1521 int error
; /* error return code */
1522 int index
; /* index of leaf entry */
1523 struct xfs_buf
*lbp
; /* leaf buffer */
1524 xfs_dir2_leaf_t
*leaf
; /* leaf structure */
1525 xfs_dir2_leaf_entry_t
*lep
; /* leaf entry */
1526 xfs_trans_t
*tp
; /* transaction pointer */
1527 struct xfs_dir2_leaf_entry
*ents
;
1529 trace_xfs_dir2_leaf_replace(args
);
1532 * Look up the entry.
1534 if ((error
= xfs_dir2_leaf_lookup_int(args
, &lbp
, &index
, &dbp
))) {
1539 ents
= dp
->d_ops
->leaf_ents_p(leaf
);
1541 * Point to the leaf entry, get data address from it.
1545 * Point to the data entry.
1547 dep
= (xfs_dir2_data_entry_t
*)
1548 ((char *)dbp
->b_addr
+
1549 xfs_dir2_dataptr_to_off(args
->geo
, be32_to_cpu(lep
->address
)));
1550 ASSERT(args
->inumber
!= be64_to_cpu(dep
->inumber
));
1552 * Put the new inode number in, log it.
1554 dep
->inumber
= cpu_to_be64(args
->inumber
);
1555 dp
->d_ops
->data_put_ftype(dep
, args
->filetype
);
1557 xfs_dir2_data_log_entry(args
, dbp
, dep
);
1558 xfs_dir3_leaf_check(dp
, lbp
);
1559 xfs_trans_brelse(tp
, lbp
);
1564 * Return index in the leaf block (lbp) which is either the first
1565 * one with this hash value, or if there are none, the insert point
1566 * for that hash value.
1568 int /* index value */
1569 xfs_dir2_leaf_search_hash(
1570 xfs_da_args_t
*args
, /* operation arguments */
1571 struct xfs_buf
*lbp
) /* leaf buffer */
1573 xfs_dahash_t hash
=0; /* hash from this entry */
1574 xfs_dahash_t hashwant
; /* hash value looking for */
1575 int high
; /* high leaf index */
1576 int low
; /* low leaf index */
1577 xfs_dir2_leaf_t
*leaf
; /* leaf structure */
1578 xfs_dir2_leaf_entry_t
*lep
; /* leaf entry */
1579 int mid
=0; /* current leaf index */
1580 struct xfs_dir2_leaf_entry
*ents
;
1581 struct xfs_dir3_icleaf_hdr leafhdr
;
1584 ents
= args
->dp
->d_ops
->leaf_ents_p(leaf
);
1585 args
->dp
->d_ops
->leaf_hdr_from_disk(&leafhdr
, leaf
);
1588 * Note, the table cannot be empty, so we have to go through the loop.
1589 * Binary search the leaf entries looking for our hash value.
1591 for (lep
= ents
, low
= 0, high
= leafhdr
.count
- 1,
1592 hashwant
= args
->hashval
;
1594 mid
= (low
+ high
) >> 1;
1595 if ((hash
= be32_to_cpu(lep
[mid
].hashval
)) == hashwant
)
1597 if (hash
< hashwant
)
1603 * Found one, back up through all the equal hash values.
1605 if (hash
== hashwant
) {
1606 while (mid
> 0 && be32_to_cpu(lep
[mid
- 1].hashval
) == hashwant
) {
1611 * Need to point to an entry higher than ours.
1613 else if (hash
< hashwant
)
1619 * Trim off a trailing data block. We know it's empty since the leaf
1620 * freespace table says so.
1623 xfs_dir2_leaf_trim_data(
1624 xfs_da_args_t
*args
, /* operation arguments */
1625 struct xfs_buf
*lbp
, /* leaf buffer */
1626 xfs_dir2_db_t db
) /* data block number */
1628 __be16
*bestsp
; /* leaf bests table */
1629 struct xfs_buf
*dbp
; /* data block buffer */
1630 xfs_inode_t
*dp
; /* incore directory inode */
1631 int error
; /* error return value */
1632 xfs_dir2_leaf_t
*leaf
; /* leaf structure */
1633 xfs_dir2_leaf_tail_t
*ltp
; /* leaf tail structure */
1634 xfs_trans_t
*tp
; /* transaction pointer */
1639 * Read the offending data block. We need its buffer.
1641 error
= xfs_dir3_data_read(tp
, dp
, xfs_dir2_db_to_da(args
->geo
, db
),
1647 ltp
= xfs_dir2_leaf_tail_p(args
->geo
, leaf
);
1651 struct xfs_dir2_data_hdr
*hdr
= dbp
->b_addr
;
1652 struct xfs_dir2_data_free
*bf
= dp
->d_ops
->data_bestfree_p(hdr
);
1654 ASSERT(hdr
->magic
== cpu_to_be32(XFS_DIR2_DATA_MAGIC
) ||
1655 hdr
->magic
== cpu_to_be32(XFS_DIR3_DATA_MAGIC
));
1656 ASSERT(be16_to_cpu(bf
[0].length
) ==
1657 args
->geo
->blksize
- dp
->d_ops
->data_entry_offset
);
1658 ASSERT(db
== be32_to_cpu(ltp
->bestcount
) - 1);
1663 * Get rid of the data block.
1665 if ((error
= xfs_dir2_shrink_inode(args
, db
, dbp
))) {
1666 ASSERT(error
!= -ENOSPC
);
1667 xfs_trans_brelse(tp
, dbp
);
1671 * Eliminate the last bests entry from the table.
1673 bestsp
= xfs_dir2_leaf_bests_p(ltp
);
1674 be32_add_cpu(<p
->bestcount
, -1);
1675 memmove(&bestsp
[1], &bestsp
[0], be32_to_cpu(ltp
->bestcount
) * sizeof(*bestsp
));
1676 xfs_dir3_leaf_log_tail(args
, lbp
);
1677 xfs_dir3_leaf_log_bests(args
, lbp
, 0, be32_to_cpu(ltp
->bestcount
) - 1);
1681 static inline size_t
1683 struct xfs_dir3_icleaf_hdr
*hdr
,
1689 entries
= hdr
->count
- hdr
->stale
;
1690 if (hdr
->magic
== XFS_DIR2_LEAF1_MAGIC
||
1691 hdr
->magic
== XFS_DIR2_LEAFN_MAGIC
)
1692 hdrsize
= sizeof(struct xfs_dir2_leaf_hdr
);
1694 hdrsize
= sizeof(struct xfs_dir3_leaf_hdr
);
1696 return hdrsize
+ entries
* sizeof(xfs_dir2_leaf_entry_t
)
1697 + counts
* sizeof(xfs_dir2_data_off_t
)
1698 + sizeof(xfs_dir2_leaf_tail_t
);
1702 * Convert node form directory to leaf form directory.
1703 * The root of the node form dir needs to already be a LEAFN block.
1704 * Just return if we can't do anything.
1707 xfs_dir2_node_to_leaf(
1708 xfs_da_state_t
*state
) /* directory operation state */
1710 xfs_da_args_t
*args
; /* operation arguments */
1711 xfs_inode_t
*dp
; /* incore directory inode */
1712 int error
; /* error return code */
1713 struct xfs_buf
*fbp
; /* buffer for freespace block */
1714 xfs_fileoff_t fo
; /* freespace file offset */
1715 xfs_dir2_free_t
*free
; /* freespace structure */
1716 struct xfs_buf
*lbp
; /* buffer for leaf block */
1717 xfs_dir2_leaf_tail_t
*ltp
; /* tail of leaf structure */
1718 xfs_dir2_leaf_t
*leaf
; /* leaf structure */
1719 xfs_mount_t
*mp
; /* filesystem mount point */
1720 int rval
; /* successful free trim? */
1721 xfs_trans_t
*tp
; /* transaction pointer */
1722 struct xfs_dir3_icleaf_hdr leafhdr
;
1723 struct xfs_dir3_icfree_hdr freehdr
;
1726 * There's more than a leaf level in the btree, so there must
1727 * be multiple leafn blocks. Give up.
1729 if (state
->path
.active
> 1)
1733 trace_xfs_dir2_node_to_leaf(args
);
1739 * Get the last offset in the file.
1741 if ((error
= xfs_bmap_last_offset(dp
, &fo
, XFS_DATA_FORK
))) {
1744 fo
-= args
->geo
->fsbcount
;
1746 * If there are freespace blocks other than the first one,
1747 * take this opportunity to remove trailing empty freespace blocks
1748 * that may have been left behind during no-space-reservation
1751 while (fo
> args
->geo
->freeblk
) {
1752 if ((error
= xfs_dir2_node_trim_free(args
, fo
, &rval
))) {
1756 fo
-= args
->geo
->fsbcount
;
1761 * Now find the block just before the freespace block.
1763 if ((error
= xfs_bmap_last_before(tp
, dp
, &fo
, XFS_DATA_FORK
))) {
1767 * If it's not the single leaf block, give up.
1769 if (XFS_FSB_TO_B(mp
, fo
) > XFS_DIR2_LEAF_OFFSET
+ args
->geo
->blksize
)
1771 lbp
= state
->path
.blk
[0].bp
;
1773 dp
->d_ops
->leaf_hdr_from_disk(&leafhdr
, leaf
);
1775 ASSERT(leafhdr
.magic
== XFS_DIR2_LEAFN_MAGIC
||
1776 leafhdr
.magic
== XFS_DIR3_LEAFN_MAGIC
);
1779 * Read the freespace block.
1781 error
= xfs_dir2_free_read(tp
, dp
, args
->geo
->freeblk
, &fbp
);
1785 dp
->d_ops
->free_hdr_from_disk(&freehdr
, free
);
1787 ASSERT(!freehdr
.firstdb
);
1790 * Now see if the leafn and free data will fit in a leaf1.
1791 * If not, release the buffer and give up.
1793 if (xfs_dir3_leaf_size(&leafhdr
, freehdr
.nvalid
) > args
->geo
->blksize
) {
1794 xfs_trans_brelse(tp
, fbp
);
1799 * If the leaf has any stale entries in it, compress them out.
1802 xfs_dir3_leaf_compact(args
, &leafhdr
, lbp
);
1804 lbp
->b_ops
= &xfs_dir3_leaf1_buf_ops
;
1805 xfs_trans_buf_set_type(tp
, lbp
, XFS_BLFT_DIR_LEAF1_BUF
);
1806 leafhdr
.magic
= (leafhdr
.magic
== XFS_DIR2_LEAFN_MAGIC
)
1807 ? XFS_DIR2_LEAF1_MAGIC
1808 : XFS_DIR3_LEAF1_MAGIC
;
1811 * Set up the leaf tail from the freespace block.
1813 ltp
= xfs_dir2_leaf_tail_p(args
->geo
, leaf
);
1814 ltp
->bestcount
= cpu_to_be32(freehdr
.nvalid
);
1817 * Set up the leaf bests table.
1819 memcpy(xfs_dir2_leaf_bests_p(ltp
), dp
->d_ops
->free_bests_p(free
),
1820 freehdr
.nvalid
* sizeof(xfs_dir2_data_off_t
));
1822 dp
->d_ops
->leaf_hdr_to_disk(leaf
, &leafhdr
);
1823 xfs_dir3_leaf_log_header(args
, lbp
);
1824 xfs_dir3_leaf_log_bests(args
, lbp
, 0, be32_to_cpu(ltp
->bestcount
) - 1);
1825 xfs_dir3_leaf_log_tail(args
, lbp
);
1826 xfs_dir3_leaf_check(dp
, lbp
);
1829 * Get rid of the freespace block.
1831 error
= xfs_dir2_shrink_inode(args
,
1832 xfs_dir2_byte_to_db(args
->geo
, XFS_DIR2_FREE_OFFSET
),
1836 * This can't fail here because it can only happen when
1837 * punching out the middle of an extent, and this is an
1840 ASSERT(error
!= -ENOSPC
);
1845 * Now see if we can convert the single-leaf directory
1846 * down to a block form directory.
1847 * This routine always kills the dabuf for the leaf, so
1848 * eliminate it from the path.
1850 error
= xfs_dir2_leaf_to_block(args
, lbp
, NULL
);
1851 state
->path
.blk
[0].bp
= NULL
;