1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2000-2003,2005 Silicon Graphics, Inc.
4 * Copyright (c) 2013 Red Hat, Inc.
7 #include "libxfs_priv.h"
9 #include "xfs_format.h"
10 #include "xfs_log_format.h"
11 #include "xfs_trans_resv.h"
12 #include "xfs_mount.h"
13 #include "xfs_da_format.h"
14 #include "xfs_da_btree.h"
15 #include "xfs_inode.h"
18 #include "xfs_dir2_priv.h"
19 #include "xfs_trace.h"
20 #include "xfs_trans.h"
21 #include "xfs_cksum.h"
24 * Local function declarations.
26 static int xfs_dir2_leaf_lookup_int(xfs_da_args_t
*args
, struct xfs_buf
**lbpp
,
27 int *indexp
, struct xfs_buf
**dbpp
);
28 static void xfs_dir3_leaf_log_bests(struct xfs_da_args
*args
,
29 struct xfs_buf
*bp
, int first
, int last
);
30 static void xfs_dir3_leaf_log_tail(struct xfs_da_args
*args
,
34 * Check the internal consistency of a leaf1 block.
35 * Pop an assert if something is wrong.
43 struct xfs_dir2_leaf
*leaf
= bp
->b_addr
;
44 struct xfs_dir3_icleaf_hdr leafhdr
;
46 dp
->d_ops
->leaf_hdr_from_disk(&leafhdr
, leaf
);
48 if (leafhdr
.magic
== XFS_DIR3_LEAF1_MAGIC
) {
49 struct xfs_dir3_leaf_hdr
*leaf3
= bp
->b_addr
;
50 if (be64_to_cpu(leaf3
->info
.blkno
) != bp
->b_bn
)
51 return __this_address
;
52 } else if (leafhdr
.magic
!= XFS_DIR2_LEAF1_MAGIC
)
53 return __this_address
;
55 return xfs_dir3_leaf_check_int(dp
->i_mount
, dp
, &leafhdr
, leaf
);
65 fa
= xfs_dir3_leaf1_check(dp
, bp
);
68 xfs_corruption_error(__func__
, XFS_ERRLEVEL_LOW
, dp
->i_mount
,
69 bp
->b_addr
, BBTOB(bp
->b_length
), __FILE__
, __LINE__
,
74 #define xfs_dir3_leaf_check(dp, bp)
78 xfs_dir3_leaf_check_int(
81 struct xfs_dir3_icleaf_hdr
*hdr
,
82 struct xfs_dir2_leaf
*leaf
)
84 struct xfs_dir2_leaf_entry
*ents
;
85 xfs_dir2_leaf_tail_t
*ltp
;
88 const struct xfs_dir_ops
*ops
;
89 struct xfs_dir3_icleaf_hdr leafhdr
;
90 struct xfs_da_geometry
*geo
= mp
->m_dir_geo
;
93 * we can be passed a null dp here from a verifier, so we need to go the
94 * hard way to get them.
96 ops
= xfs_dir_get_ops(mp
, dp
);
99 ops
->leaf_hdr_from_disk(&leafhdr
, leaf
);
103 ents
= ops
->leaf_ents_p(leaf
);
104 ltp
= xfs_dir2_leaf_tail_p(geo
, leaf
);
107 * XXX (dgc): This value is not restrictive enough.
108 * Should factor in the size of the bests table as well.
109 * We can deduce a value for that from di_size.
111 if (hdr
->count
> ops
->leaf_max_ents(geo
))
112 return __this_address
;
114 /* Leaves and bests don't overlap in leaf format. */
115 if ((hdr
->magic
== XFS_DIR2_LEAF1_MAGIC
||
116 hdr
->magic
== XFS_DIR3_LEAF1_MAGIC
) &&
117 (char *)&ents
[hdr
->count
] > (char *)xfs_dir2_leaf_bests_p(ltp
))
118 return __this_address
;
120 /* Check hash value order, count stale entries. */
121 for (i
= stale
= 0; i
< hdr
->count
; i
++) {
122 if (i
+ 1 < hdr
->count
) {
123 if (be32_to_cpu(ents
[i
].hashval
) >
124 be32_to_cpu(ents
[i
+ 1].hashval
))
125 return __this_address
;
127 if (ents
[i
].address
== cpu_to_be32(XFS_DIR2_NULL_DATAPTR
))
130 if (hdr
->stale
!= stale
)
131 return __this_address
;
136 * We verify the magic numbers before decoding the leaf header so that on debug
137 * kernels we don't get assertion failures in xfs_dir3_leaf_hdr_from_disk() due
138 * to incorrect magic numbers.
140 static xfs_failaddr_t
141 xfs_dir3_leaf_verify(
145 struct xfs_mount
*mp
= bp
->b_target
->bt_mount
;
146 struct xfs_dir2_leaf
*leaf
= bp
->b_addr
;
148 ASSERT(magic
== XFS_DIR2_LEAF1_MAGIC
|| magic
== XFS_DIR2_LEAFN_MAGIC
);
150 if (xfs_sb_version_hascrc(&mp
->m_sb
)) {
151 struct xfs_dir3_leaf_hdr
*leaf3
= bp
->b_addr
;
154 magic3
= (magic
== XFS_DIR2_LEAF1_MAGIC
) ? XFS_DIR3_LEAF1_MAGIC
155 : XFS_DIR3_LEAFN_MAGIC
;
157 if (leaf3
->info
.hdr
.magic
!= cpu_to_be16(magic3
))
158 return __this_address
;
159 if (!uuid_equal(&leaf3
->info
.uuid
, &mp
->m_sb
.sb_meta_uuid
))
160 return __this_address
;
161 if (be64_to_cpu(leaf3
->info
.blkno
) != bp
->b_bn
)
162 return __this_address
;
163 if (!xfs_log_check_lsn(mp
, be64_to_cpu(leaf3
->info
.lsn
)))
164 return __this_address
;
166 if (leaf
->hdr
.info
.magic
!= cpu_to_be16(magic
))
167 return __this_address
;
170 return xfs_dir3_leaf_check_int(mp
, NULL
, NULL
, leaf
);
178 struct xfs_mount
*mp
= bp
->b_target
->bt_mount
;
181 if (xfs_sb_version_hascrc(&mp
->m_sb
) &&
182 !xfs_buf_verify_cksum(bp
, XFS_DIR3_LEAF_CRC_OFF
))
183 xfs_verifier_error(bp
, -EFSBADCRC
, __this_address
);
185 fa
= xfs_dir3_leaf_verify(bp
, magic
);
187 xfs_verifier_error(bp
, -EFSCORRUPTED
, fa
);
196 struct xfs_mount
*mp
= bp
->b_target
->bt_mount
;
197 struct xfs_buf_log_item
*bip
= bp
->b_log_item
;
198 struct xfs_dir3_leaf_hdr
*hdr3
= bp
->b_addr
;
201 fa
= xfs_dir3_leaf_verify(bp
, magic
);
203 xfs_verifier_error(bp
, -EFSCORRUPTED
, fa
);
207 if (!xfs_sb_version_hascrc(&mp
->m_sb
))
211 hdr3
->info
.lsn
= cpu_to_be64(bip
->bli_item
.li_lsn
);
213 xfs_buf_update_cksum(bp
, XFS_DIR3_LEAF_CRC_OFF
);
216 static xfs_failaddr_t
217 xfs_dir3_leaf1_verify(
220 return xfs_dir3_leaf_verify(bp
, XFS_DIR2_LEAF1_MAGIC
);
224 xfs_dir3_leaf1_read_verify(
227 __read_verify(bp
, XFS_DIR2_LEAF1_MAGIC
);
231 xfs_dir3_leaf1_write_verify(
234 __write_verify(bp
, XFS_DIR2_LEAF1_MAGIC
);
237 static xfs_failaddr_t
238 xfs_dir3_leafn_verify(
241 return xfs_dir3_leaf_verify(bp
, XFS_DIR2_LEAFN_MAGIC
);
245 xfs_dir3_leafn_read_verify(
248 __read_verify(bp
, XFS_DIR2_LEAFN_MAGIC
);
252 xfs_dir3_leafn_write_verify(
255 __write_verify(bp
, XFS_DIR2_LEAFN_MAGIC
);
258 const struct xfs_buf_ops xfs_dir3_leaf1_buf_ops
= {
259 .name
= "xfs_dir3_leaf1",
260 .verify_read
= xfs_dir3_leaf1_read_verify
,
261 .verify_write
= xfs_dir3_leaf1_write_verify
,
262 .verify_struct
= xfs_dir3_leaf1_verify
,
265 const struct xfs_buf_ops xfs_dir3_leafn_buf_ops
= {
266 .name
= "xfs_dir3_leafn",
267 .verify_read
= xfs_dir3_leafn_read_verify
,
268 .verify_write
= xfs_dir3_leafn_write_verify
,
269 .verify_struct
= xfs_dir3_leafn_verify
,
274 struct xfs_trans
*tp
,
275 struct xfs_inode
*dp
,
277 xfs_daddr_t mappedbno
,
278 struct xfs_buf
**bpp
)
282 err
= xfs_da_read_buf(tp
, dp
, fbno
, mappedbno
, bpp
,
283 XFS_DATA_FORK
, &xfs_dir3_leaf1_buf_ops
);
284 if (!err
&& tp
&& *bpp
)
285 xfs_trans_buf_set_type(tp
, *bpp
, XFS_BLFT_DIR_LEAF1_BUF
);
291 struct xfs_trans
*tp
,
292 struct xfs_inode
*dp
,
294 xfs_daddr_t mappedbno
,
295 struct xfs_buf
**bpp
)
299 err
= xfs_da_read_buf(tp
, dp
, fbno
, mappedbno
, bpp
,
300 XFS_DATA_FORK
, &xfs_dir3_leafn_buf_ops
);
301 if (!err
&& tp
&& *bpp
)
302 xfs_trans_buf_set_type(tp
, *bpp
, XFS_BLFT_DIR_LEAFN_BUF
);
307 * Initialize a new leaf block, leaf1 or leafn magic accepted.
311 struct xfs_mount
*mp
,
312 struct xfs_trans
*tp
,
317 struct xfs_dir2_leaf
*leaf
= bp
->b_addr
;
319 ASSERT(type
== XFS_DIR2_LEAF1_MAGIC
|| type
== XFS_DIR2_LEAFN_MAGIC
);
321 if (xfs_sb_version_hascrc(&mp
->m_sb
)) {
322 struct xfs_dir3_leaf_hdr
*leaf3
= bp
->b_addr
;
324 memset(leaf3
, 0, sizeof(*leaf3
));
326 leaf3
->info
.hdr
.magic
= (type
== XFS_DIR2_LEAF1_MAGIC
)
327 ? cpu_to_be16(XFS_DIR3_LEAF1_MAGIC
)
328 : cpu_to_be16(XFS_DIR3_LEAFN_MAGIC
);
329 leaf3
->info
.blkno
= cpu_to_be64(bp
->b_bn
);
330 leaf3
->info
.owner
= cpu_to_be64(owner
);
331 uuid_copy(&leaf3
->info
.uuid
, &mp
->m_sb
.sb_meta_uuid
);
333 memset(leaf
, 0, sizeof(*leaf
));
334 leaf
->hdr
.info
.magic
= cpu_to_be16(type
);
338 * If it's a leaf-format directory initialize the tail.
339 * Caller is responsible for initialising the bests table.
341 if (type
== XFS_DIR2_LEAF1_MAGIC
) {
342 struct xfs_dir2_leaf_tail
*ltp
;
344 ltp
= xfs_dir2_leaf_tail_p(mp
->m_dir_geo
, leaf
);
346 bp
->b_ops
= &xfs_dir3_leaf1_buf_ops
;
347 xfs_trans_buf_set_type(tp
, bp
, XFS_BLFT_DIR_LEAF1_BUF
);
349 bp
->b_ops
= &xfs_dir3_leafn_buf_ops
;
350 xfs_trans_buf_set_type(tp
, bp
, XFS_BLFT_DIR_LEAFN_BUF
);
355 xfs_dir3_leaf_get_buf(
358 struct xfs_buf
**bpp
,
361 struct xfs_inode
*dp
= args
->dp
;
362 struct xfs_trans
*tp
= args
->trans
;
363 struct xfs_mount
*mp
= dp
->i_mount
;
367 ASSERT(magic
== XFS_DIR2_LEAF1_MAGIC
|| magic
== XFS_DIR2_LEAFN_MAGIC
);
368 ASSERT(bno
>= xfs_dir2_byte_to_db(args
->geo
, XFS_DIR2_LEAF_OFFSET
) &&
369 bno
< xfs_dir2_byte_to_db(args
->geo
, XFS_DIR2_FREE_OFFSET
));
371 error
= xfs_da_get_buf(tp
, dp
, xfs_dir2_db_to_da(args
->geo
, bno
),
372 -1, &bp
, XFS_DATA_FORK
);
376 xfs_dir3_leaf_init(mp
, tp
, bp
, dp
->i_ino
, magic
);
377 xfs_dir3_leaf_log_header(args
, bp
);
378 if (magic
== XFS_DIR2_LEAF1_MAGIC
)
379 xfs_dir3_leaf_log_tail(args
, bp
);
385 * Convert a block form directory to a leaf form directory.
388 xfs_dir2_block_to_leaf(
389 xfs_da_args_t
*args
, /* operation arguments */
390 struct xfs_buf
*dbp
) /* input block's buffer */
392 __be16
*bestsp
; /* leaf's bestsp entries */
393 xfs_dablk_t blkno
; /* leaf block's bno */
394 xfs_dir2_data_hdr_t
*hdr
; /* block header */
395 xfs_dir2_leaf_entry_t
*blp
; /* block's leaf entries */
396 xfs_dir2_block_tail_t
*btp
; /* block's tail */
397 xfs_inode_t
*dp
; /* incore directory inode */
398 int error
; /* error return code */
399 struct xfs_buf
*lbp
; /* leaf block's buffer */
400 xfs_dir2_db_t ldb
; /* leaf block's bno */
401 xfs_dir2_leaf_t
*leaf
; /* leaf structure */
402 xfs_dir2_leaf_tail_t
*ltp
; /* leaf's tail */
403 int needlog
; /* need to log block header */
404 int needscan
; /* need to rescan bestfree */
405 xfs_trans_t
*tp
; /* transaction pointer */
406 struct xfs_dir2_data_free
*bf
;
407 struct xfs_dir2_leaf_entry
*ents
;
408 struct xfs_dir3_icleaf_hdr leafhdr
;
410 trace_xfs_dir2_block_to_leaf(args
);
415 * Add the leaf block to the inode.
416 * This interface will only put blocks in the leaf/node range.
417 * Since that's empty now, we'll get the root (block 0 in range).
419 if ((error
= xfs_da_grow_inode(args
, &blkno
))) {
422 ldb
= xfs_dir2_da_to_db(args
->geo
, blkno
);
423 ASSERT(ldb
== xfs_dir2_byte_to_db(args
->geo
, XFS_DIR2_LEAF_OFFSET
));
425 * Initialize the leaf block, get a buffer for it.
427 error
= xfs_dir3_leaf_get_buf(args
, ldb
, &lbp
, XFS_DIR2_LEAF1_MAGIC
);
433 xfs_dir3_data_check(dp
, dbp
);
434 btp
= xfs_dir2_block_tail_p(args
->geo
, hdr
);
435 blp
= xfs_dir2_block_leaf_p(btp
);
436 bf
= dp
->d_ops
->data_bestfree_p(hdr
);
437 ents
= dp
->d_ops
->leaf_ents_p(leaf
);
440 * Set the counts in the leaf header.
442 dp
->d_ops
->leaf_hdr_from_disk(&leafhdr
, leaf
);
443 leafhdr
.count
= be32_to_cpu(btp
->count
);
444 leafhdr
.stale
= be32_to_cpu(btp
->stale
);
445 dp
->d_ops
->leaf_hdr_to_disk(leaf
, &leafhdr
);
446 xfs_dir3_leaf_log_header(args
, lbp
);
449 * Could compact these but I think we always do the conversion
450 * after squeezing out stale entries.
452 memcpy(ents
, blp
, be32_to_cpu(btp
->count
) * sizeof(xfs_dir2_leaf_entry_t
));
453 xfs_dir3_leaf_log_ents(args
, lbp
, 0, leafhdr
.count
- 1);
457 * Make the space formerly occupied by the leaf entries and block
460 xfs_dir2_data_make_free(args
, dbp
,
461 (xfs_dir2_data_aoff_t
)((char *)blp
- (char *)hdr
),
462 (xfs_dir2_data_aoff_t
)((char *)hdr
+ args
->geo
->blksize
-
464 &needlog
, &needscan
);
466 * Fix up the block header, make it a data block.
468 dbp
->b_ops
= &xfs_dir3_data_buf_ops
;
469 xfs_trans_buf_set_type(tp
, dbp
, XFS_BLFT_DIR_DATA_BUF
);
470 if (hdr
->magic
== cpu_to_be32(XFS_DIR2_BLOCK_MAGIC
))
471 hdr
->magic
= cpu_to_be32(XFS_DIR2_DATA_MAGIC
);
473 hdr
->magic
= cpu_to_be32(XFS_DIR3_DATA_MAGIC
);
476 xfs_dir2_data_freescan(dp
, hdr
, &needlog
);
478 * Set up leaf tail and bests table.
480 ltp
= xfs_dir2_leaf_tail_p(args
->geo
, leaf
);
481 ltp
->bestcount
= cpu_to_be32(1);
482 bestsp
= xfs_dir2_leaf_bests_p(ltp
);
483 bestsp
[0] = bf
[0].length
;
485 * Log the data header and leaf bests table.
488 xfs_dir2_data_log_header(args
, dbp
);
489 xfs_dir3_leaf_check(dp
, lbp
);
490 xfs_dir3_data_check(dp
, dbp
);
491 xfs_dir3_leaf_log_bests(args
, lbp
, 0, 0);
496 xfs_dir3_leaf_find_stale(
497 struct xfs_dir3_icleaf_hdr
*leafhdr
,
498 struct xfs_dir2_leaf_entry
*ents
,
504 * Find the first stale entry before our index, if any.
506 for (*lowstale
= index
- 1; *lowstale
>= 0; --*lowstale
) {
507 if (ents
[*lowstale
].address
==
508 cpu_to_be32(XFS_DIR2_NULL_DATAPTR
))
513 * Find the first stale entry at or after our index, if any.
514 * Stop if the result would require moving more entries than using
517 for (*highstale
= index
; *highstale
< leafhdr
->count
; ++*highstale
) {
518 if (ents
[*highstale
].address
==
519 cpu_to_be32(XFS_DIR2_NULL_DATAPTR
))
521 if (*lowstale
>= 0 && index
- *lowstale
<= *highstale
- index
)
526 struct xfs_dir2_leaf_entry
*
527 xfs_dir3_leaf_find_entry(
528 struct xfs_dir3_icleaf_hdr
*leafhdr
,
529 struct xfs_dir2_leaf_entry
*ents
,
530 int index
, /* leaf table position */
531 int compact
, /* need to compact leaves */
532 int lowstale
, /* index of prev stale leaf */
533 int highstale
, /* index of next stale leaf */
534 int *lfloglow
, /* low leaf logging index */
535 int *lfloghigh
) /* high leaf logging index */
537 if (!leafhdr
->stale
) {
538 xfs_dir2_leaf_entry_t
*lep
; /* leaf entry table pointer */
541 * Now we need to make room to insert the leaf entry.
543 * If there are no stale entries, just insert a hole at index.
546 if (index
< leafhdr
->count
)
547 memmove(lep
+ 1, lep
,
548 (leafhdr
->count
- index
) * sizeof(*lep
));
551 * Record low and high logging indices for the leaf.
554 *lfloghigh
= leafhdr
->count
++;
559 * There are stale entries.
561 * We will use one of them for the new entry. It's probably not at
562 * the right location, so we'll have to shift some up or down first.
564 * If we didn't compact before, we need to find the nearest stale
565 * entries before and after our insertion point.
568 xfs_dir3_leaf_find_stale(leafhdr
, ents
, index
,
569 &lowstale
, &highstale
);
572 * If the low one is better, use it.
575 (highstale
== leafhdr
->count
||
576 index
- lowstale
- 1 < highstale
- index
)) {
577 ASSERT(index
- lowstale
- 1 >= 0);
578 ASSERT(ents
[lowstale
].address
==
579 cpu_to_be32(XFS_DIR2_NULL_DATAPTR
));
582 * Copy entries up to cover the stale entry and make room
585 if (index
- lowstale
- 1 > 0) {
586 memmove(&ents
[lowstale
], &ents
[lowstale
+ 1],
587 (index
- lowstale
- 1) *
588 sizeof(xfs_dir2_leaf_entry_t
));
590 *lfloglow
= min(lowstale
, *lfloglow
);
591 *lfloghigh
= max(index
- 1, *lfloghigh
);
593 return &ents
[index
- 1];
597 * The high one is better, so use that one.
599 ASSERT(highstale
- index
>= 0);
600 ASSERT(ents
[highstale
].address
== cpu_to_be32(XFS_DIR2_NULL_DATAPTR
));
603 * Copy entries down to cover the stale entry and make room for the
606 if (highstale
- index
> 0) {
607 memmove(&ents
[index
+ 1], &ents
[index
],
608 (highstale
- index
) * sizeof(xfs_dir2_leaf_entry_t
));
610 *lfloglow
= min(index
, *lfloglow
);
611 *lfloghigh
= max(highstale
, *lfloghigh
);
617 * Add an entry to a leaf form directory.
620 xfs_dir2_leaf_addname(
621 xfs_da_args_t
*args
) /* operation arguments */
623 __be16
*bestsp
; /* freespace table in leaf */
624 int compact
; /* need to compact leaves */
625 xfs_dir2_data_hdr_t
*hdr
; /* data block header */
626 struct xfs_buf
*dbp
; /* data block buffer */
627 xfs_dir2_data_entry_t
*dep
; /* data block entry */
628 xfs_inode_t
*dp
; /* incore directory inode */
629 xfs_dir2_data_unused_t
*dup
; /* data unused entry */
630 int error
; /* error return value */
631 int grown
; /* allocated new data block */
632 int highstale
; /* index of next stale leaf */
633 int i
; /* temporary, index */
634 int index
; /* leaf table position */
635 struct xfs_buf
*lbp
; /* leaf's buffer */
636 xfs_dir2_leaf_t
*leaf
; /* leaf structure */
637 int length
; /* length of new entry */
638 xfs_dir2_leaf_entry_t
*lep
; /* leaf entry table pointer */
639 int lfloglow
; /* low leaf logging index */
640 int lfloghigh
; /* high leaf logging index */
641 int lowstale
; /* index of prev stale leaf */
642 xfs_dir2_leaf_tail_t
*ltp
; /* leaf tail pointer */
643 int needbytes
; /* leaf block bytes needed */
644 int needlog
; /* need to log data header */
645 int needscan
; /* need to rescan data free */
646 __be16
*tagp
; /* end of data entry */
647 xfs_trans_t
*tp
; /* transaction pointer */
648 xfs_dir2_db_t use_block
; /* data block number */
649 struct xfs_dir2_data_free
*bf
; /* bestfree table */
650 struct xfs_dir2_leaf_entry
*ents
;
651 struct xfs_dir3_icleaf_hdr leafhdr
;
653 trace_xfs_dir2_leaf_addname(args
);
658 error
= xfs_dir3_leaf_read(tp
, dp
, args
->geo
->leafblk
, -1, &lbp
);
663 * Look up the entry by hash value and name.
664 * We know it's not there, our caller has already done a lookup.
665 * So the index is of the entry to insert in front of.
666 * But if there are dup hash values the index is of the first of those.
668 index
= xfs_dir2_leaf_search_hash(args
, lbp
);
670 ltp
= xfs_dir2_leaf_tail_p(args
->geo
, leaf
);
671 ents
= dp
->d_ops
->leaf_ents_p(leaf
);
672 dp
->d_ops
->leaf_hdr_from_disk(&leafhdr
, leaf
);
673 bestsp
= xfs_dir2_leaf_bests_p(ltp
);
674 length
= dp
->d_ops
->data_entsize(args
->namelen
);
677 * See if there are any entries with the same hash value
678 * and space in their block for the new entry.
679 * This is good because it puts multiple same-hash value entries
680 * in a data block, improving the lookup of those entries.
682 for (use_block
= -1, lep
= &ents
[index
];
683 index
< leafhdr
.count
&& be32_to_cpu(lep
->hashval
) == args
->hashval
;
685 if (be32_to_cpu(lep
->address
) == XFS_DIR2_NULL_DATAPTR
)
687 i
= xfs_dir2_dataptr_to_db(args
->geo
, be32_to_cpu(lep
->address
));
688 ASSERT(i
< be32_to_cpu(ltp
->bestcount
));
689 ASSERT(bestsp
[i
] != cpu_to_be16(NULLDATAOFF
));
690 if (be16_to_cpu(bestsp
[i
]) >= length
) {
696 * Didn't find a block yet, linear search all the data blocks.
698 if (use_block
== -1) {
699 for (i
= 0; i
< be32_to_cpu(ltp
->bestcount
); i
++) {
701 * Remember a block we see that's missing.
703 if (bestsp
[i
] == cpu_to_be16(NULLDATAOFF
) &&
706 else if (be16_to_cpu(bestsp
[i
]) >= length
) {
713 * How many bytes do we need in the leaf block?
717 needbytes
+= sizeof(xfs_dir2_leaf_entry_t
);
719 needbytes
+= sizeof(xfs_dir2_data_off_t
);
722 * Now kill use_block if it refers to a missing block, so we
723 * can use it as an indication of allocation needed.
725 if (use_block
!= -1 && bestsp
[use_block
] == cpu_to_be16(NULLDATAOFF
))
728 * If we don't have enough free bytes but we can make enough
729 * by compacting out stale entries, we'll do that.
731 if ((char *)bestsp
- (char *)&ents
[leafhdr
.count
] < needbytes
&&
736 * Otherwise if we don't have enough free bytes we need to
737 * convert to node form.
739 else if ((char *)bestsp
- (char *)&ents
[leafhdr
.count
] < needbytes
) {
741 * Just checking or no space reservation, give up.
743 if ((args
->op_flags
& XFS_DA_OP_JUSTCHECK
) ||
745 xfs_trans_brelse(tp
, lbp
);
749 * Convert to node form.
751 error
= xfs_dir2_leaf_to_node(args
, lbp
);
755 * Then add the new entry.
757 return xfs_dir2_node_addname(args
);
760 * Otherwise it will fit without compaction.
765 * If just checking, then it will fit unless we needed to allocate
768 if (args
->op_flags
& XFS_DA_OP_JUSTCHECK
) {
769 xfs_trans_brelse(tp
, lbp
);
770 return use_block
== -1 ? -ENOSPC
: 0;
773 * If no allocations are allowed, return now before we've
776 if (args
->total
== 0 && use_block
== -1) {
777 xfs_trans_brelse(tp
, lbp
);
781 * Need to compact the leaf entries, removing stale ones.
782 * Leave one stale entry behind - the one closest to our
783 * insertion index - and we'll shift that one to our insertion
787 xfs_dir3_leaf_compact_x1(&leafhdr
, ents
, &index
, &lowstale
,
788 &highstale
, &lfloglow
, &lfloghigh
);
791 * There are stale entries, so we'll need log-low and log-high
792 * impossibly bad values later.
794 else if (leafhdr
.stale
) {
795 lfloglow
= leafhdr
.count
;
799 * If there was no data block space found, we need to allocate
802 if (use_block
== -1) {
804 * Add the new data block.
806 if ((error
= xfs_dir2_grow_inode(args
, XFS_DIR2_DATA_SPACE
,
808 xfs_trans_brelse(tp
, lbp
);
812 * Initialize the block.
814 if ((error
= xfs_dir3_data_init(args
, use_block
, &dbp
))) {
815 xfs_trans_brelse(tp
, lbp
);
819 * If we're adding a new data block on the end we need to
820 * extend the bests table. Copy it up one entry.
822 if (use_block
>= be32_to_cpu(ltp
->bestcount
)) {
824 memmove(&bestsp
[0], &bestsp
[1],
825 be32_to_cpu(ltp
->bestcount
) * sizeof(bestsp
[0]));
826 be32_add_cpu(<p
->bestcount
, 1);
827 xfs_dir3_leaf_log_tail(args
, lbp
);
828 xfs_dir3_leaf_log_bests(args
, lbp
, 0,
829 be32_to_cpu(ltp
->bestcount
) - 1);
832 * If we're filling in a previously empty block just log it.
835 xfs_dir3_leaf_log_bests(args
, lbp
, use_block
, use_block
);
837 bf
= dp
->d_ops
->data_bestfree_p(hdr
);
838 bestsp
[use_block
] = bf
[0].length
;
842 * Already had space in some data block.
843 * Just read that one in.
845 error
= xfs_dir3_data_read(tp
, dp
,
846 xfs_dir2_db_to_da(args
->geo
, use_block
),
849 xfs_trans_brelse(tp
, lbp
);
853 bf
= dp
->d_ops
->data_bestfree_p(hdr
);
857 * Point to the biggest freespace in our data block.
859 dup
= (xfs_dir2_data_unused_t
*)
860 ((char *)hdr
+ be16_to_cpu(bf
[0].offset
));
861 needscan
= needlog
= 0;
863 * Mark the initial part of our freespace in use for the new entry.
865 error
= xfs_dir2_data_use_free(args
, dbp
, dup
,
866 (xfs_dir2_data_aoff_t
)((char *)dup
- (char *)hdr
),
867 length
, &needlog
, &needscan
);
869 xfs_trans_brelse(tp
, lbp
);
873 * Initialize our new entry (at last).
875 dep
= (xfs_dir2_data_entry_t
*)dup
;
876 dep
->inumber
= cpu_to_be64(args
->inumber
);
877 dep
->namelen
= args
->namelen
;
878 memcpy(dep
->name
, args
->name
, dep
->namelen
);
879 dp
->d_ops
->data_put_ftype(dep
, args
->filetype
);
880 tagp
= dp
->d_ops
->data_entry_tag_p(dep
);
881 *tagp
= cpu_to_be16((char *)dep
- (char *)hdr
);
883 * Need to scan fix up the bestfree table.
886 xfs_dir2_data_freescan(dp
, hdr
, &needlog
);
888 * Need to log the data block's header.
891 xfs_dir2_data_log_header(args
, dbp
);
892 xfs_dir2_data_log_entry(args
, dbp
, dep
);
894 * If the bests table needs to be changed, do it.
895 * Log the change unless we've already done that.
897 if (be16_to_cpu(bestsp
[use_block
]) != be16_to_cpu(bf
[0].length
)) {
898 bestsp
[use_block
] = bf
[0].length
;
900 xfs_dir3_leaf_log_bests(args
, lbp
, use_block
, use_block
);
903 lep
= xfs_dir3_leaf_find_entry(&leafhdr
, ents
, index
, compact
, lowstale
,
904 highstale
, &lfloglow
, &lfloghigh
);
907 * Fill in the new leaf entry.
909 lep
->hashval
= cpu_to_be32(args
->hashval
);
910 lep
->address
= cpu_to_be32(
911 xfs_dir2_db_off_to_dataptr(args
->geo
, use_block
,
912 be16_to_cpu(*tagp
)));
914 * Log the leaf fields and give up the buffers.
916 dp
->d_ops
->leaf_hdr_to_disk(leaf
, &leafhdr
);
917 xfs_dir3_leaf_log_header(args
, lbp
);
918 xfs_dir3_leaf_log_ents(args
, lbp
, lfloglow
, lfloghigh
);
919 xfs_dir3_leaf_check(dp
, lbp
);
920 xfs_dir3_data_check(dp
, dbp
);
925 * Compact out any stale entries in the leaf.
926 * Log the header and changed leaf entries, if any.
929 xfs_dir3_leaf_compact(
930 xfs_da_args_t
*args
, /* operation arguments */
931 struct xfs_dir3_icleaf_hdr
*leafhdr
,
932 struct xfs_buf
*bp
) /* leaf buffer */
934 int from
; /* source leaf index */
935 xfs_dir2_leaf_t
*leaf
; /* leaf structure */
936 int loglow
; /* first leaf entry to log */
937 int to
; /* target leaf index */
938 struct xfs_dir2_leaf_entry
*ents
;
939 struct xfs_inode
*dp
= args
->dp
;
946 * Compress out the stale entries in place.
948 ents
= dp
->d_ops
->leaf_ents_p(leaf
);
949 for (from
= to
= 0, loglow
= -1; from
< leafhdr
->count
; from
++) {
950 if (ents
[from
].address
== cpu_to_be32(XFS_DIR2_NULL_DATAPTR
))
953 * Only actually copy the entries that are different.
958 ents
[to
] = ents
[from
];
963 * Update and log the header, log the leaf entries.
965 ASSERT(leafhdr
->stale
== from
- to
);
966 leafhdr
->count
-= leafhdr
->stale
;
969 dp
->d_ops
->leaf_hdr_to_disk(leaf
, leafhdr
);
970 xfs_dir3_leaf_log_header(args
, bp
);
972 xfs_dir3_leaf_log_ents(args
, bp
, loglow
, to
- 1);
976 * Compact the leaf entries, removing stale ones.
977 * Leave one stale entry behind - the one closest to our
978 * insertion index - and the caller will shift that one to our insertion
980 * Return new insertion index, where the remaining stale entry is,
981 * and leaf logging indices.
984 xfs_dir3_leaf_compact_x1(
985 struct xfs_dir3_icleaf_hdr
*leafhdr
,
986 struct xfs_dir2_leaf_entry
*ents
,
987 int *indexp
, /* insertion index */
988 int *lowstalep
, /* out: stale entry before us */
989 int *highstalep
, /* out: stale entry after us */
990 int *lowlogp
, /* out: low log index */
991 int *highlogp
) /* out: high log index */
993 int from
; /* source copy index */
994 int highstale
; /* stale entry at/after index */
995 int index
; /* insertion index */
996 int keepstale
; /* source index of kept stale */
997 int lowstale
; /* stale entry before index */
998 int newindex
=0; /* new insertion index */
999 int to
; /* destination copy index */
1001 ASSERT(leafhdr
->stale
> 1);
1004 xfs_dir3_leaf_find_stale(leafhdr
, ents
, index
, &lowstale
, &highstale
);
1007 * Pick the better of lowstale and highstale.
1009 if (lowstale
>= 0 &&
1010 (highstale
== leafhdr
->count
||
1011 index
- lowstale
<= highstale
- index
))
1012 keepstale
= lowstale
;
1014 keepstale
= highstale
;
1016 * Copy the entries in place, removing all the stale entries
1019 for (from
= to
= 0; from
< leafhdr
->count
; from
++) {
1021 * Notice the new value of index.
1025 if (from
!= keepstale
&&
1026 ents
[from
].address
== cpu_to_be32(XFS_DIR2_NULL_DATAPTR
)) {
1032 * Record the new keepstale value for the insertion.
1034 if (from
== keepstale
)
1035 lowstale
= highstale
= to
;
1037 * Copy only the entries that have moved.
1040 ents
[to
] = ents
[from
];
1045 * If the insertion point was past the last entry,
1046 * set the new insertion point accordingly.
1052 * Adjust the leaf header values.
1054 leafhdr
->count
-= from
- to
;
1057 * Remember the low/high stale value only in the "right"
1060 if (lowstale
>= newindex
)
1063 highstale
= leafhdr
->count
;
1064 *highlogp
= leafhdr
->count
- 1;
1065 *lowstalep
= lowstale
;
1066 *highstalep
= highstale
;
1070 * Log the bests entries indicated from a leaf1 block.
1073 xfs_dir3_leaf_log_bests(
1074 struct xfs_da_args
*args
,
1075 struct xfs_buf
*bp
, /* leaf buffer */
1076 int first
, /* first entry to log */
1077 int last
) /* last entry to log */
1079 __be16
*firstb
; /* pointer to first entry */
1080 __be16
*lastb
; /* pointer to last entry */
1081 struct xfs_dir2_leaf
*leaf
= bp
->b_addr
;
1082 xfs_dir2_leaf_tail_t
*ltp
; /* leaf tail structure */
1084 ASSERT(leaf
->hdr
.info
.magic
== cpu_to_be16(XFS_DIR2_LEAF1_MAGIC
) ||
1085 leaf
->hdr
.info
.magic
== cpu_to_be16(XFS_DIR3_LEAF1_MAGIC
));
1087 ltp
= xfs_dir2_leaf_tail_p(args
->geo
, leaf
);
1088 firstb
= xfs_dir2_leaf_bests_p(ltp
) + first
;
1089 lastb
= xfs_dir2_leaf_bests_p(ltp
) + last
;
1090 xfs_trans_log_buf(args
->trans
, bp
,
1091 (uint
)((char *)firstb
- (char *)leaf
),
1092 (uint
)((char *)lastb
- (char *)leaf
+ sizeof(*lastb
) - 1));
1096 * Log the leaf entries indicated from a leaf1 or leafn block.
1099 xfs_dir3_leaf_log_ents(
1100 struct xfs_da_args
*args
,
1105 xfs_dir2_leaf_entry_t
*firstlep
; /* pointer to first entry */
1106 xfs_dir2_leaf_entry_t
*lastlep
; /* pointer to last entry */
1107 struct xfs_dir2_leaf
*leaf
= bp
->b_addr
;
1108 struct xfs_dir2_leaf_entry
*ents
;
1110 ASSERT(leaf
->hdr
.info
.magic
== cpu_to_be16(XFS_DIR2_LEAF1_MAGIC
) ||
1111 leaf
->hdr
.info
.magic
== cpu_to_be16(XFS_DIR3_LEAF1_MAGIC
) ||
1112 leaf
->hdr
.info
.magic
== cpu_to_be16(XFS_DIR2_LEAFN_MAGIC
) ||
1113 leaf
->hdr
.info
.magic
== cpu_to_be16(XFS_DIR3_LEAFN_MAGIC
));
1115 ents
= args
->dp
->d_ops
->leaf_ents_p(leaf
);
1116 firstlep
= &ents
[first
];
1117 lastlep
= &ents
[last
];
1118 xfs_trans_log_buf(args
->trans
, bp
,
1119 (uint
)((char *)firstlep
- (char *)leaf
),
1120 (uint
)((char *)lastlep
- (char *)leaf
+ sizeof(*lastlep
) - 1));
1124 * Log the header of the leaf1 or leafn block.
1127 xfs_dir3_leaf_log_header(
1128 struct xfs_da_args
*args
,
1131 struct xfs_dir2_leaf
*leaf
= bp
->b_addr
;
1133 ASSERT(leaf
->hdr
.info
.magic
== cpu_to_be16(XFS_DIR2_LEAF1_MAGIC
) ||
1134 leaf
->hdr
.info
.magic
== cpu_to_be16(XFS_DIR3_LEAF1_MAGIC
) ||
1135 leaf
->hdr
.info
.magic
== cpu_to_be16(XFS_DIR2_LEAFN_MAGIC
) ||
1136 leaf
->hdr
.info
.magic
== cpu_to_be16(XFS_DIR3_LEAFN_MAGIC
));
1138 xfs_trans_log_buf(args
->trans
, bp
,
1139 (uint
)((char *)&leaf
->hdr
- (char *)leaf
),
1140 args
->dp
->d_ops
->leaf_hdr_size
- 1);
1144 * Log the tail of the leaf1 block.
1147 xfs_dir3_leaf_log_tail(
1148 struct xfs_da_args
*args
,
1151 struct xfs_dir2_leaf
*leaf
= bp
->b_addr
;
1152 xfs_dir2_leaf_tail_t
*ltp
; /* leaf tail structure */
1154 ASSERT(leaf
->hdr
.info
.magic
== cpu_to_be16(XFS_DIR2_LEAF1_MAGIC
) ||
1155 leaf
->hdr
.info
.magic
== cpu_to_be16(XFS_DIR3_LEAF1_MAGIC
) ||
1156 leaf
->hdr
.info
.magic
== cpu_to_be16(XFS_DIR2_LEAFN_MAGIC
) ||
1157 leaf
->hdr
.info
.magic
== cpu_to_be16(XFS_DIR3_LEAFN_MAGIC
));
1159 ltp
= xfs_dir2_leaf_tail_p(args
->geo
, leaf
);
1160 xfs_trans_log_buf(args
->trans
, bp
, (uint
)((char *)ltp
- (char *)leaf
),
1161 (uint
)(args
->geo
->blksize
- 1));
1165 * Look up the entry referred to by args in the leaf format directory.
1166 * Most of the work is done by the xfs_dir2_leaf_lookup_int routine which
1167 * is also used by the node-format code.
1170 xfs_dir2_leaf_lookup(
1171 xfs_da_args_t
*args
) /* operation arguments */
1173 struct xfs_buf
*dbp
; /* data block buffer */
1174 xfs_dir2_data_entry_t
*dep
; /* data block entry */
1175 xfs_inode_t
*dp
; /* incore directory inode */
1176 int error
; /* error return code */
1177 int index
; /* found entry index */
1178 struct xfs_buf
*lbp
; /* leaf buffer */
1179 xfs_dir2_leaf_t
*leaf
; /* leaf structure */
1180 xfs_dir2_leaf_entry_t
*lep
; /* leaf entry */
1181 xfs_trans_t
*tp
; /* transaction pointer */
1182 struct xfs_dir2_leaf_entry
*ents
;
1184 trace_xfs_dir2_leaf_lookup(args
);
1187 * Look up name in the leaf block, returning both buffers and index.
1189 if ((error
= xfs_dir2_leaf_lookup_int(args
, &lbp
, &index
, &dbp
))) {
1194 xfs_dir3_leaf_check(dp
, lbp
);
1196 ents
= dp
->d_ops
->leaf_ents_p(leaf
);
1198 * Get to the leaf entry and contained data entry address.
1203 * Point to the data entry.
1205 dep
= (xfs_dir2_data_entry_t
*)
1206 ((char *)dbp
->b_addr
+
1207 xfs_dir2_dataptr_to_off(args
->geo
, be32_to_cpu(lep
->address
)));
1209 * Return the found inode number & CI name if appropriate
1211 args
->inumber
= be64_to_cpu(dep
->inumber
);
1212 args
->filetype
= dp
->d_ops
->data_get_ftype(dep
);
1213 error
= xfs_dir_cilookup_result(args
, dep
->name
, dep
->namelen
);
1214 xfs_trans_brelse(tp
, dbp
);
1215 xfs_trans_brelse(tp
, lbp
);
1220 * Look up name/hash in the leaf block.
1221 * Fill in indexp with the found index, and dbpp with the data buffer.
1222 * If not found dbpp will be NULL, and ENOENT comes back.
1223 * lbpp will always be filled in with the leaf buffer unless there's an error.
1225 static int /* error */
1226 xfs_dir2_leaf_lookup_int(
1227 xfs_da_args_t
*args
, /* operation arguments */
1228 struct xfs_buf
**lbpp
, /* out: leaf buffer */
1229 int *indexp
, /* out: index in leaf block */
1230 struct xfs_buf
**dbpp
) /* out: data buffer */
1232 xfs_dir2_db_t curdb
= -1; /* current data block number */
1233 struct xfs_buf
*dbp
= NULL
; /* data buffer */
1234 xfs_dir2_data_entry_t
*dep
; /* data entry */
1235 xfs_inode_t
*dp
; /* incore directory inode */
1236 int error
; /* error return code */
1237 int index
; /* index in leaf block */
1238 struct xfs_buf
*lbp
; /* leaf buffer */
1239 xfs_dir2_leaf_entry_t
*lep
; /* leaf entry */
1240 xfs_dir2_leaf_t
*leaf
; /* leaf structure */
1241 xfs_mount_t
*mp
; /* filesystem mount point */
1242 xfs_dir2_db_t newdb
; /* new data block number */
1243 xfs_trans_t
*tp
; /* transaction pointer */
1244 xfs_dir2_db_t cidb
= -1; /* case match data block no. */
1245 enum xfs_dacmp cmp
; /* name compare result */
1246 struct xfs_dir2_leaf_entry
*ents
;
1247 struct xfs_dir3_icleaf_hdr leafhdr
;
1253 error
= xfs_dir3_leaf_read(tp
, dp
, args
->geo
->leafblk
, -1, &lbp
);
1259 xfs_dir3_leaf_check(dp
, lbp
);
1260 ents
= dp
->d_ops
->leaf_ents_p(leaf
);
1261 dp
->d_ops
->leaf_hdr_from_disk(&leafhdr
, leaf
);
1264 * Look for the first leaf entry with our hash value.
1266 index
= xfs_dir2_leaf_search_hash(args
, lbp
);
1268 * Loop over all the entries with the right hash value
1269 * looking to match the name.
1271 for (lep
= &ents
[index
];
1272 index
< leafhdr
.count
&& be32_to_cpu(lep
->hashval
) == args
->hashval
;
1275 * Skip over stale leaf entries.
1277 if (be32_to_cpu(lep
->address
) == XFS_DIR2_NULL_DATAPTR
)
1280 * Get the new data block number.
1282 newdb
= xfs_dir2_dataptr_to_db(args
->geo
,
1283 be32_to_cpu(lep
->address
));
1285 * If it's not the same as the old data block number,
1286 * need to pitch the old one and read the new one.
1288 if (newdb
!= curdb
) {
1290 xfs_trans_brelse(tp
, dbp
);
1291 error
= xfs_dir3_data_read(tp
, dp
,
1292 xfs_dir2_db_to_da(args
->geo
, newdb
),
1295 xfs_trans_brelse(tp
, lbp
);
1301 * Point to the data entry.
1303 dep
= (xfs_dir2_data_entry_t
*)((char *)dbp
->b_addr
+
1304 xfs_dir2_dataptr_to_off(args
->geo
,
1305 be32_to_cpu(lep
->address
)));
1307 * Compare name and if it's an exact match, return the index
1308 * and buffer. If it's the first case-insensitive match, store
1309 * the index and buffer and continue looking for an exact match.
1311 cmp
= mp
->m_dirnameops
->compname(args
, dep
->name
, dep
->namelen
);
1312 if (cmp
!= XFS_CMP_DIFFERENT
&& cmp
!= args
->cmpresult
) {
1313 args
->cmpresult
= cmp
;
1315 /* case exact match: return the current buffer. */
1316 if (cmp
== XFS_CMP_EXACT
) {
1323 ASSERT(args
->op_flags
& XFS_DA_OP_OKNOENT
);
1325 * Here, we can only be doing a lookup (not a rename or remove).
1326 * If a case-insensitive match was found earlier, re-read the
1327 * appropriate data block if required and return it.
1329 if (args
->cmpresult
== XFS_CMP_CASE
) {
1331 if (cidb
!= curdb
) {
1332 xfs_trans_brelse(tp
, dbp
);
1333 error
= xfs_dir3_data_read(tp
, dp
,
1334 xfs_dir2_db_to_da(args
->geo
, cidb
),
1337 xfs_trans_brelse(tp
, lbp
);
1345 * No match found, return -ENOENT.
1349 xfs_trans_brelse(tp
, dbp
);
1350 xfs_trans_brelse(tp
, lbp
);
1355 * Remove an entry from a leaf format directory.
1358 xfs_dir2_leaf_removename(
1359 xfs_da_args_t
*args
) /* operation arguments */
1361 __be16
*bestsp
; /* leaf block best freespace */
1362 xfs_dir2_data_hdr_t
*hdr
; /* data block header */
1363 xfs_dir2_db_t db
; /* data block number */
1364 struct xfs_buf
*dbp
; /* data block buffer */
1365 xfs_dir2_data_entry_t
*dep
; /* data entry structure */
1366 xfs_inode_t
*dp
; /* incore directory inode */
1367 int error
; /* error return code */
1368 xfs_dir2_db_t i
; /* temporary data block # */
1369 int index
; /* index into leaf entries */
1370 struct xfs_buf
*lbp
; /* leaf buffer */
1371 xfs_dir2_leaf_t
*leaf
; /* leaf structure */
1372 xfs_dir2_leaf_entry_t
*lep
; /* leaf entry */
1373 xfs_dir2_leaf_tail_t
*ltp
; /* leaf tail structure */
1374 int needlog
; /* need to log data header */
1375 int needscan
; /* need to rescan data frees */
1376 xfs_dir2_data_off_t oldbest
; /* old value of best free */
1377 struct xfs_dir2_data_free
*bf
; /* bestfree table */
1378 struct xfs_dir2_leaf_entry
*ents
;
1379 struct xfs_dir3_icleaf_hdr leafhdr
;
1381 trace_xfs_dir2_leaf_removename(args
);
1384 * Lookup the leaf entry, get the leaf and data blocks read in.
1386 if ((error
= xfs_dir2_leaf_lookup_int(args
, &lbp
, &index
, &dbp
))) {
1392 xfs_dir3_data_check(dp
, dbp
);
1393 bf
= dp
->d_ops
->data_bestfree_p(hdr
);
1394 dp
->d_ops
->leaf_hdr_from_disk(&leafhdr
, leaf
);
1395 ents
= dp
->d_ops
->leaf_ents_p(leaf
);
1397 * Point to the leaf entry, use that to point to the data entry.
1400 db
= xfs_dir2_dataptr_to_db(args
->geo
, be32_to_cpu(lep
->address
));
1401 dep
= (xfs_dir2_data_entry_t
*)((char *)hdr
+
1402 xfs_dir2_dataptr_to_off(args
->geo
, be32_to_cpu(lep
->address
)));
1403 needscan
= needlog
= 0;
1404 oldbest
= be16_to_cpu(bf
[0].length
);
1405 ltp
= xfs_dir2_leaf_tail_p(args
->geo
, leaf
);
1406 bestsp
= xfs_dir2_leaf_bests_p(ltp
);
1407 if (be16_to_cpu(bestsp
[db
]) != oldbest
)
1408 return -EFSCORRUPTED
;
1410 * Mark the former data entry unused.
1412 xfs_dir2_data_make_free(args
, dbp
,
1413 (xfs_dir2_data_aoff_t
)((char *)dep
- (char *)hdr
),
1414 dp
->d_ops
->data_entsize(dep
->namelen
), &needlog
, &needscan
);
1416 * We just mark the leaf entry stale by putting a null in it.
1419 dp
->d_ops
->leaf_hdr_to_disk(leaf
, &leafhdr
);
1420 xfs_dir3_leaf_log_header(args
, lbp
);
1422 lep
->address
= cpu_to_be32(XFS_DIR2_NULL_DATAPTR
);
1423 xfs_dir3_leaf_log_ents(args
, lbp
, index
, index
);
1426 * Scan the freespace in the data block again if necessary,
1427 * log the data block header if necessary.
1430 xfs_dir2_data_freescan(dp
, hdr
, &needlog
);
1432 xfs_dir2_data_log_header(args
, dbp
);
1434 * If the longest freespace in the data block has changed,
1435 * put the new value in the bests table and log that.
1437 if (be16_to_cpu(bf
[0].length
) != oldbest
) {
1438 bestsp
[db
] = bf
[0].length
;
1439 xfs_dir3_leaf_log_bests(args
, lbp
, db
, db
);
1441 xfs_dir3_data_check(dp
, dbp
);
1443 * If the data block is now empty then get rid of the data block.
1445 if (be16_to_cpu(bf
[0].length
) ==
1446 args
->geo
->blksize
- dp
->d_ops
->data_entry_offset
) {
1447 ASSERT(db
!= args
->geo
->datablk
);
1448 if ((error
= xfs_dir2_shrink_inode(args
, db
, dbp
))) {
1450 * Nope, can't get rid of it because it caused
1451 * allocation of a bmap btree block to do so.
1452 * Just go on, returning success, leaving the
1453 * empty block in place.
1455 if (error
== -ENOSPC
&& args
->total
== 0)
1457 xfs_dir3_leaf_check(dp
, lbp
);
1462 * If this is the last data block then compact the
1463 * bests table by getting rid of entries.
1465 if (db
== be32_to_cpu(ltp
->bestcount
) - 1) {
1467 * Look for the last active entry (i).
1469 for (i
= db
- 1; i
> 0; i
--) {
1470 if (bestsp
[i
] != cpu_to_be16(NULLDATAOFF
))
1474 * Copy the table down so inactive entries at the
1477 memmove(&bestsp
[db
- i
], bestsp
,
1478 (be32_to_cpu(ltp
->bestcount
) - (db
- i
)) * sizeof(*bestsp
));
1479 be32_add_cpu(<p
->bestcount
, -(db
- i
));
1480 xfs_dir3_leaf_log_tail(args
, lbp
);
1481 xfs_dir3_leaf_log_bests(args
, lbp
, 0,
1482 be32_to_cpu(ltp
->bestcount
) - 1);
1484 bestsp
[db
] = cpu_to_be16(NULLDATAOFF
);
1487 * If the data block was not the first one, drop it.
1489 else if (db
!= args
->geo
->datablk
)
1492 xfs_dir3_leaf_check(dp
, lbp
);
1494 * See if we can convert to block form.
1496 return xfs_dir2_leaf_to_block(args
, lbp
, dbp
);
1500 * Replace the inode number in a leaf format directory entry.
1503 xfs_dir2_leaf_replace(
1504 xfs_da_args_t
*args
) /* operation arguments */
1506 struct xfs_buf
*dbp
; /* data block buffer */
1507 xfs_dir2_data_entry_t
*dep
; /* data block entry */
1508 xfs_inode_t
*dp
; /* incore directory inode */
1509 int error
; /* error return code */
1510 int index
; /* index of leaf entry */
1511 struct xfs_buf
*lbp
; /* leaf buffer */
1512 xfs_dir2_leaf_t
*leaf
; /* leaf structure */
1513 xfs_dir2_leaf_entry_t
*lep
; /* leaf entry */
1514 xfs_trans_t
*tp
; /* transaction pointer */
1515 struct xfs_dir2_leaf_entry
*ents
;
1517 trace_xfs_dir2_leaf_replace(args
);
1520 * Look up the entry.
1522 if ((error
= xfs_dir2_leaf_lookup_int(args
, &lbp
, &index
, &dbp
))) {
1527 ents
= dp
->d_ops
->leaf_ents_p(leaf
);
1529 * Point to the leaf entry, get data address from it.
1533 * Point to the data entry.
1535 dep
= (xfs_dir2_data_entry_t
*)
1536 ((char *)dbp
->b_addr
+
1537 xfs_dir2_dataptr_to_off(args
->geo
, be32_to_cpu(lep
->address
)));
1538 ASSERT(args
->inumber
!= be64_to_cpu(dep
->inumber
));
1540 * Put the new inode number in, log it.
1542 dep
->inumber
= cpu_to_be64(args
->inumber
);
1543 dp
->d_ops
->data_put_ftype(dep
, args
->filetype
);
1545 xfs_dir2_data_log_entry(args
, dbp
, dep
);
1546 xfs_dir3_leaf_check(dp
, lbp
);
1547 xfs_trans_brelse(tp
, lbp
);
1552 * Return index in the leaf block (lbp) which is either the first
1553 * one with this hash value, or if there are none, the insert point
1554 * for that hash value.
1556 int /* index value */
1557 xfs_dir2_leaf_search_hash(
1558 xfs_da_args_t
*args
, /* operation arguments */
1559 struct xfs_buf
*lbp
) /* leaf buffer */
1561 xfs_dahash_t hash
=0; /* hash from this entry */
1562 xfs_dahash_t hashwant
; /* hash value looking for */
1563 int high
; /* high leaf index */
1564 int low
; /* low leaf index */
1565 xfs_dir2_leaf_t
*leaf
; /* leaf structure */
1566 xfs_dir2_leaf_entry_t
*lep
; /* leaf entry */
1567 int mid
=0; /* current leaf index */
1568 struct xfs_dir2_leaf_entry
*ents
;
1569 struct xfs_dir3_icleaf_hdr leafhdr
;
1572 ents
= args
->dp
->d_ops
->leaf_ents_p(leaf
);
1573 args
->dp
->d_ops
->leaf_hdr_from_disk(&leafhdr
, leaf
);
1576 * Note, the table cannot be empty, so we have to go through the loop.
1577 * Binary search the leaf entries looking for our hash value.
1579 for (lep
= ents
, low
= 0, high
= leafhdr
.count
- 1,
1580 hashwant
= args
->hashval
;
1582 mid
= (low
+ high
) >> 1;
1583 if ((hash
= be32_to_cpu(lep
[mid
].hashval
)) == hashwant
)
1585 if (hash
< hashwant
)
1591 * Found one, back up through all the equal hash values.
1593 if (hash
== hashwant
) {
1594 while (mid
> 0 && be32_to_cpu(lep
[mid
- 1].hashval
) == hashwant
) {
1599 * Need to point to an entry higher than ours.
1601 else if (hash
< hashwant
)
1607 * Trim off a trailing data block. We know it's empty since the leaf
1608 * freespace table says so.
1611 xfs_dir2_leaf_trim_data(
1612 xfs_da_args_t
*args
, /* operation arguments */
1613 struct xfs_buf
*lbp
, /* leaf buffer */
1614 xfs_dir2_db_t db
) /* data block number */
1616 __be16
*bestsp
; /* leaf bests table */
1617 struct xfs_buf
*dbp
; /* data block buffer */
1618 xfs_inode_t
*dp
; /* incore directory inode */
1619 int error
; /* error return value */
1620 xfs_dir2_leaf_t
*leaf
; /* leaf structure */
1621 xfs_dir2_leaf_tail_t
*ltp
; /* leaf tail structure */
1622 xfs_trans_t
*tp
; /* transaction pointer */
1627 * Read the offending data block. We need its buffer.
1629 error
= xfs_dir3_data_read(tp
, dp
, xfs_dir2_db_to_da(args
->geo
, db
),
1635 ltp
= xfs_dir2_leaf_tail_p(args
->geo
, leaf
);
1639 struct xfs_dir2_data_hdr
*hdr
= dbp
->b_addr
;
1640 struct xfs_dir2_data_free
*bf
= dp
->d_ops
->data_bestfree_p(hdr
);
1642 ASSERT(hdr
->magic
== cpu_to_be32(XFS_DIR2_DATA_MAGIC
) ||
1643 hdr
->magic
== cpu_to_be32(XFS_DIR3_DATA_MAGIC
));
1644 ASSERT(be16_to_cpu(bf
[0].length
) ==
1645 args
->geo
->blksize
- dp
->d_ops
->data_entry_offset
);
1646 ASSERT(db
== be32_to_cpu(ltp
->bestcount
) - 1);
1651 * Get rid of the data block.
1653 if ((error
= xfs_dir2_shrink_inode(args
, db
, dbp
))) {
1654 ASSERT(error
!= -ENOSPC
);
1655 xfs_trans_brelse(tp
, dbp
);
1659 * Eliminate the last bests entry from the table.
1661 bestsp
= xfs_dir2_leaf_bests_p(ltp
);
1662 be32_add_cpu(<p
->bestcount
, -1);
1663 memmove(&bestsp
[1], &bestsp
[0], be32_to_cpu(ltp
->bestcount
) * sizeof(*bestsp
));
1664 xfs_dir3_leaf_log_tail(args
, lbp
);
1665 xfs_dir3_leaf_log_bests(args
, lbp
, 0, be32_to_cpu(ltp
->bestcount
) - 1);
1669 static inline size_t
1671 struct xfs_dir3_icleaf_hdr
*hdr
,
1677 entries
= hdr
->count
- hdr
->stale
;
1678 if (hdr
->magic
== XFS_DIR2_LEAF1_MAGIC
||
1679 hdr
->magic
== XFS_DIR2_LEAFN_MAGIC
)
1680 hdrsize
= sizeof(struct xfs_dir2_leaf_hdr
);
1682 hdrsize
= sizeof(struct xfs_dir3_leaf_hdr
);
1684 return hdrsize
+ entries
* sizeof(xfs_dir2_leaf_entry_t
)
1685 + counts
* sizeof(xfs_dir2_data_off_t
)
1686 + sizeof(xfs_dir2_leaf_tail_t
);
1690 * Convert node form directory to leaf form directory.
1691 * The root of the node form dir needs to already be a LEAFN block.
1692 * Just return if we can't do anything.
1695 xfs_dir2_node_to_leaf(
1696 xfs_da_state_t
*state
) /* directory operation state */
1698 xfs_da_args_t
*args
; /* operation arguments */
1699 xfs_inode_t
*dp
; /* incore directory inode */
1700 int error
; /* error return code */
1701 struct xfs_buf
*fbp
; /* buffer for freespace block */
1702 xfs_fileoff_t fo
; /* freespace file offset */
1703 xfs_dir2_free_t
*free
; /* freespace structure */
1704 struct xfs_buf
*lbp
; /* buffer for leaf block */
1705 xfs_dir2_leaf_tail_t
*ltp
; /* tail of leaf structure */
1706 xfs_dir2_leaf_t
*leaf
; /* leaf structure */
1707 xfs_mount_t
*mp
; /* filesystem mount point */
1708 int rval
; /* successful free trim? */
1709 xfs_trans_t
*tp
; /* transaction pointer */
1710 struct xfs_dir3_icleaf_hdr leafhdr
;
1711 struct xfs_dir3_icfree_hdr freehdr
;
1714 * There's more than a leaf level in the btree, so there must
1715 * be multiple leafn blocks. Give up.
1717 if (state
->path
.active
> 1)
1721 trace_xfs_dir2_node_to_leaf(args
);
1727 * Get the last offset in the file.
1729 if ((error
= xfs_bmap_last_offset(dp
, &fo
, XFS_DATA_FORK
))) {
1732 fo
-= args
->geo
->fsbcount
;
1734 * If there are freespace blocks other than the first one,
1735 * take this opportunity to remove trailing empty freespace blocks
1736 * that may have been left behind during no-space-reservation
1739 while (fo
> args
->geo
->freeblk
) {
1740 if ((error
= xfs_dir2_node_trim_free(args
, fo
, &rval
))) {
1744 fo
-= args
->geo
->fsbcount
;
1749 * Now find the block just before the freespace block.
1751 if ((error
= xfs_bmap_last_before(tp
, dp
, &fo
, XFS_DATA_FORK
))) {
1755 * If it's not the single leaf block, give up.
1757 if (XFS_FSB_TO_B(mp
, fo
) > XFS_DIR2_LEAF_OFFSET
+ args
->geo
->blksize
)
1759 lbp
= state
->path
.blk
[0].bp
;
1761 dp
->d_ops
->leaf_hdr_from_disk(&leafhdr
, leaf
);
1763 ASSERT(leafhdr
.magic
== XFS_DIR2_LEAFN_MAGIC
||
1764 leafhdr
.magic
== XFS_DIR3_LEAFN_MAGIC
);
1767 * Read the freespace block.
1769 error
= xfs_dir2_free_read(tp
, dp
, args
->geo
->freeblk
, &fbp
);
1773 dp
->d_ops
->free_hdr_from_disk(&freehdr
, free
);
1775 ASSERT(!freehdr
.firstdb
);
1778 * Now see if the leafn and free data will fit in a leaf1.
1779 * If not, release the buffer and give up.
1781 if (xfs_dir3_leaf_size(&leafhdr
, freehdr
.nvalid
) > args
->geo
->blksize
) {
1782 xfs_trans_brelse(tp
, fbp
);
1787 * If the leaf has any stale entries in it, compress them out.
1790 xfs_dir3_leaf_compact(args
, &leafhdr
, lbp
);
1792 lbp
->b_ops
= &xfs_dir3_leaf1_buf_ops
;
1793 xfs_trans_buf_set_type(tp
, lbp
, XFS_BLFT_DIR_LEAF1_BUF
);
1794 leafhdr
.magic
= (leafhdr
.magic
== XFS_DIR2_LEAFN_MAGIC
)
1795 ? XFS_DIR2_LEAF1_MAGIC
1796 : XFS_DIR3_LEAF1_MAGIC
;
1799 * Set up the leaf tail from the freespace block.
1801 ltp
= xfs_dir2_leaf_tail_p(args
->geo
, leaf
);
1802 ltp
->bestcount
= cpu_to_be32(freehdr
.nvalid
);
1805 * Set up the leaf bests table.
1807 memcpy(xfs_dir2_leaf_bests_p(ltp
), dp
->d_ops
->free_bests_p(free
),
1808 freehdr
.nvalid
* sizeof(xfs_dir2_data_off_t
));
1810 dp
->d_ops
->leaf_hdr_to_disk(leaf
, &leafhdr
);
1811 xfs_dir3_leaf_log_header(args
, lbp
);
1812 xfs_dir3_leaf_log_bests(args
, lbp
, 0, be32_to_cpu(ltp
->bestcount
) - 1);
1813 xfs_dir3_leaf_log_tail(args
, lbp
);
1814 xfs_dir3_leaf_check(dp
, lbp
);
1817 * Get rid of the freespace block.
1819 error
= xfs_dir2_shrink_inode(args
,
1820 xfs_dir2_byte_to_db(args
->geo
, XFS_DIR2_FREE_OFFSET
),
1824 * This can't fail here because it can only happen when
1825 * punching out the middle of an extent, and this is an
1828 ASSERT(error
!= -ENOSPC
);
1833 * Now see if we can convert the single-leaf directory
1834 * down to a block form directory.
1835 * This routine always kills the dabuf for the leaf, so
1836 * eliminate it from the path.
1838 error
= xfs_dir2_leaf_to_block(args
, lbp
, NULL
);
1839 state
->path
.blk
[0].bp
= NULL
;