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_shared.h"
10 #include "xfs_format.h"
11 #include "xfs_log_format.h"
12 #include "xfs_trans_resv.h"
13 #include "xfs_mount.h"
14 #include "xfs_inode.h"
17 #include "xfs_dir2_priv.h"
18 #include "xfs_trace.h"
19 #include "xfs_trans.h"
20 #include "xfs_health.h"
23 * Local function declarations.
25 static int xfs_dir2_leaf_lookup_int(xfs_da_args_t
*args
, struct xfs_buf
**lbpp
,
26 int *indexp
, struct xfs_buf
**dbpp
,
27 struct xfs_dir3_icleaf_hdr
*leafhdr
);
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 xfs_dir2_leaf_hdr_from_disk(
36 struct xfs_dir3_icleaf_hdr
*to
,
37 struct xfs_dir2_leaf
*from
)
39 if (xfs_has_crc(mp
)) {
40 struct xfs_dir3_leaf
*from3
= (struct xfs_dir3_leaf
*)from
;
42 to
->forw
= be32_to_cpu(from3
->hdr
.info
.hdr
.forw
);
43 to
->back
= be32_to_cpu(from3
->hdr
.info
.hdr
.back
);
44 to
->magic
= be16_to_cpu(from3
->hdr
.info
.hdr
.magic
);
45 to
->count
= be16_to_cpu(from3
->hdr
.count
);
46 to
->stale
= be16_to_cpu(from3
->hdr
.stale
);
47 to
->ents
= from3
->__ents
;
49 ASSERT(to
->magic
== XFS_DIR3_LEAF1_MAGIC
||
50 to
->magic
== XFS_DIR3_LEAFN_MAGIC
);
52 to
->forw
= be32_to_cpu(from
->hdr
.info
.forw
);
53 to
->back
= be32_to_cpu(from
->hdr
.info
.back
);
54 to
->magic
= be16_to_cpu(from
->hdr
.info
.magic
);
55 to
->count
= be16_to_cpu(from
->hdr
.count
);
56 to
->stale
= be16_to_cpu(from
->hdr
.stale
);
57 to
->ents
= from
->__ents
;
59 ASSERT(to
->magic
== XFS_DIR2_LEAF1_MAGIC
||
60 to
->magic
== XFS_DIR2_LEAFN_MAGIC
);
65 xfs_dir2_leaf_hdr_to_disk(
67 struct xfs_dir2_leaf
*to
,
68 struct xfs_dir3_icleaf_hdr
*from
)
70 if (xfs_has_crc(mp
)) {
71 struct xfs_dir3_leaf
*to3
= (struct xfs_dir3_leaf
*)to
;
73 ASSERT(from
->magic
== XFS_DIR3_LEAF1_MAGIC
||
74 from
->magic
== XFS_DIR3_LEAFN_MAGIC
);
76 to3
->hdr
.info
.hdr
.forw
= cpu_to_be32(from
->forw
);
77 to3
->hdr
.info
.hdr
.back
= cpu_to_be32(from
->back
);
78 to3
->hdr
.info
.hdr
.magic
= cpu_to_be16(from
->magic
);
79 to3
->hdr
.count
= cpu_to_be16(from
->count
);
80 to3
->hdr
.stale
= cpu_to_be16(from
->stale
);
82 ASSERT(from
->magic
== XFS_DIR2_LEAF1_MAGIC
||
83 from
->magic
== XFS_DIR2_LEAFN_MAGIC
);
85 to
->hdr
.info
.forw
= cpu_to_be32(from
->forw
);
86 to
->hdr
.info
.back
= cpu_to_be32(from
->back
);
87 to
->hdr
.info
.magic
= cpu_to_be16(from
->magic
);
88 to
->hdr
.count
= cpu_to_be16(from
->count
);
89 to
->hdr
.stale
= cpu_to_be16(from
->stale
);
94 * Check the internal consistency of a leaf1 block.
95 * Pop an assert if something is wrong.
100 struct xfs_inode
*dp
,
103 struct xfs_dir2_leaf
*leaf
= bp
->b_addr
;
104 struct xfs_dir3_icleaf_hdr leafhdr
;
106 xfs_dir2_leaf_hdr_from_disk(dp
->i_mount
, &leafhdr
, leaf
);
108 if (leafhdr
.magic
== XFS_DIR3_LEAF1_MAGIC
) {
109 struct xfs_dir3_leaf_hdr
*leaf3
= bp
->b_addr
;
110 if (be64_to_cpu(leaf3
->info
.blkno
) != xfs_buf_daddr(bp
))
111 return __this_address
;
112 } else if (leafhdr
.magic
!= XFS_DIR2_LEAF1_MAGIC
)
113 return __this_address
;
115 return xfs_dir3_leaf_check_int(dp
->i_mount
, &leafhdr
, leaf
, false);
120 struct xfs_inode
*dp
,
125 fa
= xfs_dir3_leaf1_check(dp
, bp
);
128 xfs_corruption_error(__func__
, XFS_ERRLEVEL_LOW
, dp
->i_mount
,
129 bp
->b_addr
, BBTOB(bp
->b_length
), __FILE__
, __LINE__
,
134 #define xfs_dir3_leaf_check(dp, bp)
138 xfs_dir3_leaf_check_int(
139 struct xfs_mount
*mp
,
140 struct xfs_dir3_icleaf_hdr
*hdr
,
141 struct xfs_dir2_leaf
*leaf
,
142 bool expensive_checking
)
144 struct xfs_da_geometry
*geo
= mp
->m_dir_geo
;
145 xfs_dir2_leaf_tail_t
*ltp
;
148 bool isleaf1
= (hdr
->magic
== XFS_DIR2_LEAF1_MAGIC
||
149 hdr
->magic
== XFS_DIR3_LEAF1_MAGIC
);
151 ltp
= xfs_dir2_leaf_tail_p(geo
, leaf
);
154 * XXX (dgc): This value is not restrictive enough.
155 * Should factor in the size of the bests table as well.
156 * We can deduce a value for that from i_disk_size.
158 if (hdr
->count
> geo
->leaf_max_ents
)
159 return __this_address
;
161 /* Leaves and bests don't overlap in leaf format. */
163 (char *)&hdr
->ents
[hdr
->count
] > (char *)xfs_dir2_leaf_bests_p(ltp
))
164 return __this_address
;
166 if (!expensive_checking
)
169 /* Check hash value order, count stale entries. */
170 for (i
= stale
= 0; i
< hdr
->count
; i
++) {
171 if (i
+ 1 < hdr
->count
) {
172 if (be32_to_cpu(hdr
->ents
[i
].hashval
) >
173 be32_to_cpu(hdr
->ents
[i
+ 1].hashval
))
174 return __this_address
;
176 if (hdr
->ents
[i
].address
== cpu_to_be32(XFS_DIR2_NULL_DATAPTR
))
178 if (isleaf1
&& xfs_dir2_dataptr_to_db(geo
,
179 be32_to_cpu(hdr
->ents
[i
].address
)) >=
180 be32_to_cpu(ltp
->bestcount
))
181 return __this_address
;
183 if (hdr
->stale
!= stale
)
184 return __this_address
;
189 * We verify the magic numbers before decoding the leaf header so that on debug
190 * kernels we don't get assertion failures in xfs_dir3_leaf_hdr_from_disk() due
191 * to incorrect magic numbers.
193 static xfs_failaddr_t
194 xfs_dir3_leaf_verify(
197 struct xfs_mount
*mp
= bp
->b_mount
;
198 struct xfs_dir3_icleaf_hdr leafhdr
;
201 fa
= xfs_da3_blkinfo_verify(bp
, bp
->b_addr
);
205 xfs_dir2_leaf_hdr_from_disk(mp
, &leafhdr
, bp
->b_addr
);
206 return xfs_dir3_leaf_check_int(mp
, &leafhdr
, bp
->b_addr
, true);
210 xfs_dir3_leaf_header_check(
214 struct xfs_mount
*mp
= bp
->b_mount
;
216 if (xfs_has_crc(mp
)) {
217 struct xfs_dir3_leaf
*hdr3
= bp
->b_addr
;
219 if (hdr3
->hdr
.info
.hdr
.magic
!=
220 cpu_to_be16(XFS_DIR3_LEAF1_MAGIC
) &&
221 hdr3
->hdr
.info
.hdr
.magic
!=
222 cpu_to_be16(XFS_DIR3_LEAFN_MAGIC
))
223 return __this_address
;
225 if (be64_to_cpu(hdr3
->hdr
.info
.owner
) != owner
)
226 return __this_address
;
233 xfs_dir3_leaf_read_verify(
236 struct xfs_mount
*mp
= bp
->b_mount
;
239 if (xfs_has_crc(mp
) &&
240 !xfs_buf_verify_cksum(bp
, XFS_DIR3_LEAF_CRC_OFF
))
241 xfs_verifier_error(bp
, -EFSBADCRC
, __this_address
);
243 fa
= xfs_dir3_leaf_verify(bp
);
245 xfs_verifier_error(bp
, -EFSCORRUPTED
, fa
);
250 xfs_dir3_leaf_write_verify(
253 struct xfs_mount
*mp
= bp
->b_mount
;
254 struct xfs_buf_log_item
*bip
= bp
->b_log_item
;
255 struct xfs_dir3_leaf_hdr
*hdr3
= bp
->b_addr
;
258 fa
= xfs_dir3_leaf_verify(bp
);
260 xfs_verifier_error(bp
, -EFSCORRUPTED
, fa
);
264 if (!xfs_has_crc(mp
))
268 hdr3
->info
.lsn
= cpu_to_be64(bip
->bli_item
.li_lsn
);
270 xfs_buf_update_cksum(bp
, XFS_DIR3_LEAF_CRC_OFF
);
273 const struct xfs_buf_ops xfs_dir3_leaf1_buf_ops
= {
274 .name
= "xfs_dir3_leaf1",
275 .magic16
= { cpu_to_be16(XFS_DIR2_LEAF1_MAGIC
),
276 cpu_to_be16(XFS_DIR3_LEAF1_MAGIC
) },
277 .verify_read
= xfs_dir3_leaf_read_verify
,
278 .verify_write
= xfs_dir3_leaf_write_verify
,
279 .verify_struct
= xfs_dir3_leaf_verify
,
282 const struct xfs_buf_ops xfs_dir3_leafn_buf_ops
= {
283 .name
= "xfs_dir3_leafn",
284 .magic16
= { cpu_to_be16(XFS_DIR2_LEAFN_MAGIC
),
285 cpu_to_be16(XFS_DIR3_LEAFN_MAGIC
) },
286 .verify_read
= xfs_dir3_leaf_read_verify
,
287 .verify_write
= xfs_dir3_leaf_write_verify
,
288 .verify_struct
= xfs_dir3_leaf_verify
,
293 struct xfs_trans
*tp
,
294 struct xfs_inode
*dp
,
297 struct xfs_buf
**bpp
)
302 err
= xfs_da_read_buf(tp
, dp
, fbno
, 0, bpp
, XFS_DATA_FORK
,
303 &xfs_dir3_leaf1_buf_ops
);
307 fa
= xfs_dir3_leaf_header_check(*bpp
, owner
);
309 __xfs_buf_mark_corrupt(*bpp
, fa
);
310 xfs_trans_brelse(tp
, *bpp
);
312 xfs_dirattr_mark_sick(dp
, XFS_DATA_FORK
);
313 return -EFSCORRUPTED
;
317 xfs_trans_buf_set_type(tp
, *bpp
, XFS_BLFT_DIR_LEAF1_BUF
);
323 struct xfs_trans
*tp
,
324 struct xfs_inode
*dp
,
327 struct xfs_buf
**bpp
)
332 err
= xfs_da_read_buf(tp
, dp
, fbno
, 0, bpp
, XFS_DATA_FORK
,
333 &xfs_dir3_leafn_buf_ops
);
337 fa
= xfs_dir3_leaf_header_check(*bpp
, owner
);
339 __xfs_buf_mark_corrupt(*bpp
, fa
);
340 xfs_trans_brelse(tp
, *bpp
);
342 xfs_dirattr_mark_sick(dp
, XFS_DATA_FORK
);
343 return -EFSCORRUPTED
;
347 xfs_trans_buf_set_type(tp
, *bpp
, XFS_BLFT_DIR_LEAFN_BUF
);
352 * Initialize a new leaf block, leaf1 or leafn magic accepted.
356 struct xfs_da_args
*args
,
360 struct xfs_mount
*mp
= args
->dp
->i_mount
;
361 struct xfs_trans
*tp
= args
->trans
;
362 struct xfs_dir2_leaf
*leaf
= bp
->b_addr
;
364 ASSERT(type
== XFS_DIR2_LEAF1_MAGIC
|| type
== XFS_DIR2_LEAFN_MAGIC
);
366 if (xfs_has_crc(mp
)) {
367 struct xfs_dir3_leaf_hdr
*leaf3
= bp
->b_addr
;
369 memset(leaf3
, 0, sizeof(*leaf3
));
371 leaf3
->info
.hdr
.magic
= (type
== XFS_DIR2_LEAF1_MAGIC
)
372 ? cpu_to_be16(XFS_DIR3_LEAF1_MAGIC
)
373 : cpu_to_be16(XFS_DIR3_LEAFN_MAGIC
);
374 leaf3
->info
.blkno
= cpu_to_be64(xfs_buf_daddr(bp
));
375 leaf3
->info
.owner
= cpu_to_be64(args
->owner
);
376 uuid_copy(&leaf3
->info
.uuid
, &mp
->m_sb
.sb_meta_uuid
);
378 memset(leaf
, 0, sizeof(*leaf
));
379 leaf
->hdr
.info
.magic
= cpu_to_be16(type
);
383 * If it's a leaf-format directory initialize the tail.
384 * Caller is responsible for initialising the bests table.
386 if (type
== XFS_DIR2_LEAF1_MAGIC
) {
387 struct xfs_dir2_leaf_tail
*ltp
;
389 ltp
= xfs_dir2_leaf_tail_p(mp
->m_dir_geo
, leaf
);
391 bp
->b_ops
= &xfs_dir3_leaf1_buf_ops
;
392 xfs_trans_buf_set_type(tp
, bp
, XFS_BLFT_DIR_LEAF1_BUF
);
394 bp
->b_ops
= &xfs_dir3_leafn_buf_ops
;
395 xfs_trans_buf_set_type(tp
, bp
, XFS_BLFT_DIR_LEAFN_BUF
);
400 xfs_dir3_leaf_get_buf(
403 struct xfs_buf
**bpp
,
406 struct xfs_inode
*dp
= args
->dp
;
407 struct xfs_trans
*tp
= args
->trans
;
411 ASSERT(magic
== XFS_DIR2_LEAF1_MAGIC
|| magic
== XFS_DIR2_LEAFN_MAGIC
);
412 ASSERT(bno
>= xfs_dir2_byte_to_db(args
->geo
, XFS_DIR2_LEAF_OFFSET
) &&
413 bno
< xfs_dir2_byte_to_db(args
->geo
, XFS_DIR2_FREE_OFFSET
));
415 error
= xfs_da_get_buf(tp
, dp
, xfs_dir2_db_to_da(args
->geo
, bno
),
420 xfs_dir3_leaf_init(args
, bp
, magic
);
421 xfs_dir3_leaf_log_header(args
, bp
);
422 if (magic
== XFS_DIR2_LEAF1_MAGIC
)
423 xfs_dir3_leaf_log_tail(args
, bp
);
429 * Convert a block form directory to a leaf form directory.
432 xfs_dir2_block_to_leaf(
433 xfs_da_args_t
*args
, /* operation arguments */
434 struct xfs_buf
*dbp
) /* input block's buffer */
436 __be16
*bestsp
; /* leaf's bestsp entries */
437 xfs_dablk_t blkno
; /* leaf block's bno */
438 xfs_dir2_data_hdr_t
*hdr
; /* block header */
439 xfs_dir2_leaf_entry_t
*blp
; /* block's leaf entries */
440 xfs_dir2_block_tail_t
*btp
; /* block's tail */
441 xfs_inode_t
*dp
; /* incore directory inode */
442 int error
; /* error return code */
443 struct xfs_buf
*lbp
; /* leaf block's buffer */
444 xfs_dir2_db_t ldb
; /* leaf block's bno */
445 xfs_dir2_leaf_t
*leaf
; /* leaf structure */
446 xfs_dir2_leaf_tail_t
*ltp
; /* leaf's tail */
447 int needlog
; /* need to log block header */
448 int needscan
; /* need to rescan bestfree */
449 xfs_trans_t
*tp
; /* transaction pointer */
450 struct xfs_dir2_data_free
*bf
;
451 struct xfs_dir3_icleaf_hdr leafhdr
;
453 trace_xfs_dir2_block_to_leaf(args
);
458 * Add the leaf block to the inode.
459 * This interface will only put blocks in the leaf/node range.
460 * Since that's empty now, we'll get the root (block 0 in range).
462 if ((error
= xfs_da_grow_inode(args
, &blkno
))) {
465 ldb
= xfs_dir2_da_to_db(args
->geo
, blkno
);
466 ASSERT(ldb
== xfs_dir2_byte_to_db(args
->geo
, XFS_DIR2_LEAF_OFFSET
));
468 * Initialize the leaf block, get a buffer for it.
470 error
= xfs_dir3_leaf_get_buf(args
, ldb
, &lbp
, XFS_DIR2_LEAF1_MAGIC
);
476 xfs_dir3_data_check(dp
, dbp
);
477 btp
= xfs_dir2_block_tail_p(args
->geo
, hdr
);
478 blp
= xfs_dir2_block_leaf_p(btp
);
479 bf
= xfs_dir2_data_bestfree_p(dp
->i_mount
, hdr
);
482 * Set the counts in the leaf header.
484 xfs_dir2_leaf_hdr_from_disk(dp
->i_mount
, &leafhdr
, leaf
);
485 leafhdr
.count
= be32_to_cpu(btp
->count
);
486 leafhdr
.stale
= be32_to_cpu(btp
->stale
);
487 xfs_dir2_leaf_hdr_to_disk(dp
->i_mount
, leaf
, &leafhdr
);
488 xfs_dir3_leaf_log_header(args
, lbp
);
491 * Could compact these but I think we always do the conversion
492 * after squeezing out stale entries.
494 memcpy(leafhdr
.ents
, blp
,
495 be32_to_cpu(btp
->count
) * sizeof(struct xfs_dir2_leaf_entry
));
496 xfs_dir3_leaf_log_ents(args
, &leafhdr
, lbp
, 0, leafhdr
.count
- 1);
500 * Make the space formerly occupied by the leaf entries and block
503 xfs_dir2_data_make_free(args
, dbp
,
504 (xfs_dir2_data_aoff_t
)((char *)blp
- (char *)hdr
),
505 (xfs_dir2_data_aoff_t
)((char *)hdr
+ args
->geo
->blksize
-
507 &needlog
, &needscan
);
509 * Fix up the block header, make it a data block.
511 dbp
->b_ops
= &xfs_dir3_data_buf_ops
;
512 xfs_trans_buf_set_type(tp
, dbp
, XFS_BLFT_DIR_DATA_BUF
);
513 if (hdr
->magic
== cpu_to_be32(XFS_DIR2_BLOCK_MAGIC
))
514 hdr
->magic
= cpu_to_be32(XFS_DIR2_DATA_MAGIC
);
516 hdr
->magic
= cpu_to_be32(XFS_DIR3_DATA_MAGIC
);
519 xfs_dir2_data_freescan(dp
->i_mount
, hdr
, &needlog
);
521 * Set up leaf tail and bests table.
523 ltp
= xfs_dir2_leaf_tail_p(args
->geo
, leaf
);
524 ltp
->bestcount
= cpu_to_be32(1);
525 bestsp
= xfs_dir2_leaf_bests_p(ltp
);
526 bestsp
[0] = bf
[0].length
;
528 * Log the data header and leaf bests table.
531 xfs_dir2_data_log_header(args
, dbp
);
532 xfs_dir3_leaf_check(dp
, lbp
);
533 xfs_dir3_data_check(dp
, dbp
);
534 xfs_dir3_leaf_log_bests(args
, lbp
, 0, 0);
539 xfs_dir3_leaf_find_stale(
540 struct xfs_dir3_icleaf_hdr
*leafhdr
,
541 struct xfs_dir2_leaf_entry
*ents
,
547 * Find the first stale entry before our index, if any.
549 for (*lowstale
= index
- 1; *lowstale
>= 0; --*lowstale
) {
550 if (ents
[*lowstale
].address
==
551 cpu_to_be32(XFS_DIR2_NULL_DATAPTR
))
556 * Find the first stale entry at or after our index, if any.
557 * Stop if the result would require moving more entries than using
560 for (*highstale
= index
; *highstale
< leafhdr
->count
; ++*highstale
) {
561 if (ents
[*highstale
].address
==
562 cpu_to_be32(XFS_DIR2_NULL_DATAPTR
))
564 if (*lowstale
>= 0 && index
- *lowstale
<= *highstale
- index
)
569 struct xfs_dir2_leaf_entry
*
570 xfs_dir3_leaf_find_entry(
571 struct xfs_dir3_icleaf_hdr
*leafhdr
,
572 struct xfs_dir2_leaf_entry
*ents
,
573 int index
, /* leaf table position */
574 int compact
, /* need to compact leaves */
575 int lowstale
, /* index of prev stale leaf */
576 int highstale
, /* index of next stale leaf */
577 int *lfloglow
, /* low leaf logging index */
578 int *lfloghigh
) /* high leaf logging index */
580 if (!leafhdr
->stale
) {
581 xfs_dir2_leaf_entry_t
*lep
; /* leaf entry table pointer */
584 * Now we need to make room to insert the leaf entry.
586 * If there are no stale entries, just insert a hole at index.
589 if (index
< leafhdr
->count
)
590 memmove(lep
+ 1, lep
,
591 (leafhdr
->count
- index
) * sizeof(*lep
));
594 * Record low and high logging indices for the leaf.
597 *lfloghigh
= leafhdr
->count
++;
602 * There are stale entries.
604 * We will use one of them for the new entry. It's probably not at
605 * the right location, so we'll have to shift some up or down first.
607 * If we didn't compact before, we need to find the nearest stale
608 * entries before and after our insertion point.
611 xfs_dir3_leaf_find_stale(leafhdr
, ents
, index
,
612 &lowstale
, &highstale
);
615 * If the low one is better, use it.
618 (highstale
== leafhdr
->count
||
619 index
- lowstale
- 1 < highstale
- index
)) {
620 ASSERT(index
- lowstale
- 1 >= 0);
621 ASSERT(ents
[lowstale
].address
==
622 cpu_to_be32(XFS_DIR2_NULL_DATAPTR
));
625 * Copy entries up to cover the stale entry and make room
628 if (index
- lowstale
- 1 > 0) {
629 memmove(&ents
[lowstale
], &ents
[lowstale
+ 1],
630 (index
- lowstale
- 1) *
631 sizeof(xfs_dir2_leaf_entry_t
));
633 *lfloglow
= min(lowstale
, *lfloglow
);
634 *lfloghigh
= max(index
- 1, *lfloghigh
);
636 return &ents
[index
- 1];
640 * The high one is better, so use that one.
642 ASSERT(highstale
- index
>= 0);
643 ASSERT(ents
[highstale
].address
== cpu_to_be32(XFS_DIR2_NULL_DATAPTR
));
646 * Copy entries down to cover the stale entry and make room for the
649 if (highstale
- index
> 0) {
650 memmove(&ents
[index
+ 1], &ents
[index
],
651 (highstale
- index
) * sizeof(xfs_dir2_leaf_entry_t
));
653 *lfloglow
= min(index
, *lfloglow
);
654 *lfloghigh
= max(highstale
, *lfloghigh
);
660 * Add an entry to a leaf form directory.
663 xfs_dir2_leaf_addname(
664 struct xfs_da_args
*args
) /* operation arguments */
666 struct xfs_dir3_icleaf_hdr leafhdr
;
667 struct xfs_trans
*tp
= args
->trans
;
668 __be16
*bestsp
; /* freespace table in leaf */
669 __be16
*tagp
; /* end of data entry */
670 struct xfs_buf
*dbp
; /* data block buffer */
671 struct xfs_buf
*lbp
; /* leaf's buffer */
672 struct xfs_dir2_leaf
*leaf
; /* leaf structure */
673 struct xfs_inode
*dp
= args
->dp
; /* incore directory inode */
674 struct xfs_dir2_data_hdr
*hdr
; /* data block header */
675 struct xfs_dir2_data_entry
*dep
; /* data block entry */
676 struct xfs_dir2_leaf_entry
*lep
; /* leaf entry table pointer */
677 struct xfs_dir2_leaf_entry
*ents
;
678 struct xfs_dir2_data_unused
*dup
; /* data unused entry */
679 struct xfs_dir2_leaf_tail
*ltp
; /* leaf tail pointer */
680 struct xfs_dir2_data_free
*bf
; /* bestfree table */
681 int compact
; /* need to compact leaves */
682 int error
; /* error return value */
683 int grown
; /* allocated new data block */
684 int highstale
= 0; /* index of next stale leaf */
685 int i
; /* temporary, index */
686 int index
; /* leaf table position */
687 int length
; /* length of new entry */
688 int lfloglow
; /* low leaf logging index */
689 int lfloghigh
; /* high leaf logging index */
690 int lowstale
= 0; /* index of prev stale leaf */
691 int needbytes
; /* leaf block bytes needed */
692 int needlog
; /* need to log data header */
693 int needscan
; /* need to rescan data free */
694 xfs_dir2_db_t use_block
; /* data block number */
696 trace_xfs_dir2_leaf_addname(args
);
698 error
= xfs_dir3_leaf_read(tp
, dp
, args
->owner
, args
->geo
->leafblk
,
704 * Look up the entry by hash value and name.
705 * We know it's not there, our caller has already done a lookup.
706 * So the index is of the entry to insert in front of.
707 * But if there are dup hash values the index is of the first of those.
709 index
= xfs_dir2_leaf_search_hash(args
, lbp
);
711 ltp
= xfs_dir2_leaf_tail_p(args
->geo
, leaf
);
712 xfs_dir2_leaf_hdr_from_disk(dp
->i_mount
, &leafhdr
, leaf
);
714 bestsp
= xfs_dir2_leaf_bests_p(ltp
);
715 length
= xfs_dir2_data_entsize(dp
->i_mount
, args
->namelen
);
718 * See if there are any entries with the same hash value
719 * and space in their block for the new entry.
720 * This is good because it puts multiple same-hash value entries
721 * in a data block, improving the lookup of those entries.
723 for (use_block
= -1, lep
= &ents
[index
];
724 index
< leafhdr
.count
&& be32_to_cpu(lep
->hashval
) == args
->hashval
;
726 if (be32_to_cpu(lep
->address
) == XFS_DIR2_NULL_DATAPTR
)
728 i
= xfs_dir2_dataptr_to_db(args
->geo
, be32_to_cpu(lep
->address
));
729 ASSERT(i
< be32_to_cpu(ltp
->bestcount
));
730 ASSERT(bestsp
[i
] != cpu_to_be16(NULLDATAOFF
));
731 if (be16_to_cpu(bestsp
[i
]) >= length
) {
737 * Didn't find a block yet, linear search all the data blocks.
739 if (use_block
== -1) {
740 for (i
= 0; i
< be32_to_cpu(ltp
->bestcount
); i
++) {
742 * Remember a block we see that's missing.
744 if (bestsp
[i
] == cpu_to_be16(NULLDATAOFF
) &&
747 else if (be16_to_cpu(bestsp
[i
]) >= length
) {
754 * How many bytes do we need in the leaf block?
758 needbytes
+= sizeof(xfs_dir2_leaf_entry_t
);
760 needbytes
+= sizeof(xfs_dir2_data_off_t
);
763 * Now kill use_block if it refers to a missing block, so we
764 * can use it as an indication of allocation needed.
766 if (use_block
!= -1 && bestsp
[use_block
] == cpu_to_be16(NULLDATAOFF
))
769 * If we don't have enough free bytes but we can make enough
770 * by compacting out stale entries, we'll do that.
772 if ((char *)bestsp
- (char *)&ents
[leafhdr
.count
] < needbytes
&&
777 * Otherwise if we don't have enough free bytes we need to
778 * convert to node form.
780 else if ((char *)bestsp
- (char *)&ents
[leafhdr
.count
] < needbytes
) {
782 * Just checking or no space reservation, give up.
784 if ((args
->op_flags
& XFS_DA_OP_JUSTCHECK
) ||
786 xfs_trans_brelse(tp
, lbp
);
790 * Convert to node form.
792 error
= xfs_dir2_leaf_to_node(args
, lbp
);
796 * Then add the new entry.
798 return xfs_dir2_node_addname(args
);
801 * Otherwise it will fit without compaction.
806 * If just checking, then it will fit unless we needed to allocate
809 if (args
->op_flags
& XFS_DA_OP_JUSTCHECK
) {
810 xfs_trans_brelse(tp
, lbp
);
811 return use_block
== -1 ? -ENOSPC
: 0;
814 * If no allocations are allowed, return now before we've
817 if (args
->total
== 0 && use_block
== -1) {
818 xfs_trans_brelse(tp
, lbp
);
822 * Need to compact the leaf entries, removing stale ones.
823 * Leave one stale entry behind - the one closest to our
824 * insertion index - and we'll shift that one to our insertion
828 xfs_dir3_leaf_compact_x1(&leafhdr
, ents
, &index
, &lowstale
,
829 &highstale
, &lfloglow
, &lfloghigh
);
832 * There are stale entries, so we'll need log-low and log-high
833 * impossibly bad values later.
835 else if (leafhdr
.stale
) {
836 lfloglow
= leafhdr
.count
;
840 * If there was no data block space found, we need to allocate
843 if (use_block
== -1) {
845 * Add the new data block.
847 if ((error
= xfs_dir2_grow_inode(args
, XFS_DIR2_DATA_SPACE
,
849 xfs_trans_brelse(tp
, lbp
);
853 * Initialize the block.
855 if ((error
= xfs_dir3_data_init(args
, use_block
, &dbp
))) {
856 xfs_trans_brelse(tp
, lbp
);
860 * If we're adding a new data block on the end we need to
861 * extend the bests table. Copy it up one entry.
863 if (use_block
>= be32_to_cpu(ltp
->bestcount
)) {
865 memmove(&bestsp
[0], &bestsp
[1],
866 be32_to_cpu(ltp
->bestcount
) * sizeof(bestsp
[0]));
867 be32_add_cpu(<p
->bestcount
, 1);
868 xfs_dir3_leaf_log_tail(args
, lbp
);
869 xfs_dir3_leaf_log_bests(args
, lbp
, 0,
870 be32_to_cpu(ltp
->bestcount
) - 1);
873 * If we're filling in a previously empty block just log it.
876 xfs_dir3_leaf_log_bests(args
, lbp
, use_block
, use_block
);
878 bf
= xfs_dir2_data_bestfree_p(dp
->i_mount
, hdr
);
879 bestsp
[use_block
] = bf
[0].length
;
883 * Already had space in some data block.
884 * Just read that one in.
886 error
= xfs_dir3_data_read(tp
, dp
, args
->owner
,
887 xfs_dir2_db_to_da(args
->geo
, use_block
), 0,
890 xfs_trans_brelse(tp
, lbp
);
894 bf
= xfs_dir2_data_bestfree_p(dp
->i_mount
, hdr
);
898 * Point to the biggest freespace in our data block.
900 dup
= (xfs_dir2_data_unused_t
*)
901 ((char *)hdr
+ be16_to_cpu(bf
[0].offset
));
902 needscan
= needlog
= 0;
904 * Mark the initial part of our freespace in use for the new entry.
906 error
= xfs_dir2_data_use_free(args
, dbp
, dup
,
907 (xfs_dir2_data_aoff_t
)((char *)dup
- (char *)hdr
),
908 length
, &needlog
, &needscan
);
910 xfs_trans_brelse(tp
, lbp
);
914 * Initialize our new entry (at last).
916 dep
= (xfs_dir2_data_entry_t
*)dup
;
917 dep
->inumber
= cpu_to_be64(args
->inumber
);
918 dep
->namelen
= args
->namelen
;
919 memcpy(dep
->name
, args
->name
, dep
->namelen
);
920 xfs_dir2_data_put_ftype(dp
->i_mount
, dep
, args
->filetype
);
921 tagp
= xfs_dir2_data_entry_tag_p(dp
->i_mount
, dep
);
922 *tagp
= cpu_to_be16((char *)dep
- (char *)hdr
);
924 * Need to scan fix up the bestfree table.
927 xfs_dir2_data_freescan(dp
->i_mount
, hdr
, &needlog
);
929 * Need to log the data block's header.
932 xfs_dir2_data_log_header(args
, dbp
);
933 xfs_dir2_data_log_entry(args
, dbp
, dep
);
935 * If the bests table needs to be changed, do it.
936 * Log the change unless we've already done that.
938 if (be16_to_cpu(bestsp
[use_block
]) != be16_to_cpu(bf
[0].length
)) {
939 bestsp
[use_block
] = bf
[0].length
;
941 xfs_dir3_leaf_log_bests(args
, lbp
, use_block
, use_block
);
944 lep
= xfs_dir3_leaf_find_entry(&leafhdr
, ents
, index
, compact
, lowstale
,
945 highstale
, &lfloglow
, &lfloghigh
);
948 * Fill in the new leaf entry.
950 lep
->hashval
= cpu_to_be32(args
->hashval
);
951 lep
->address
= cpu_to_be32(
952 xfs_dir2_db_off_to_dataptr(args
->geo
, use_block
,
953 be16_to_cpu(*tagp
)));
955 * Log the leaf fields and give up the buffers.
957 xfs_dir2_leaf_hdr_to_disk(dp
->i_mount
, leaf
, &leafhdr
);
958 xfs_dir3_leaf_log_header(args
, lbp
);
959 xfs_dir3_leaf_log_ents(args
, &leafhdr
, lbp
, lfloglow
, lfloghigh
);
960 xfs_dir3_leaf_check(dp
, lbp
);
961 xfs_dir3_data_check(dp
, dbp
);
966 * Compact out any stale entries in the leaf.
967 * Log the header and changed leaf entries, if any.
970 xfs_dir3_leaf_compact(
971 xfs_da_args_t
*args
, /* operation arguments */
972 struct xfs_dir3_icleaf_hdr
*leafhdr
,
973 struct xfs_buf
*bp
) /* leaf buffer */
975 int from
; /* source leaf index */
976 xfs_dir2_leaf_t
*leaf
; /* leaf structure */
977 int loglow
; /* first leaf entry to log */
978 int to
; /* target leaf index */
979 struct xfs_inode
*dp
= args
->dp
;
986 * Compress out the stale entries in place.
988 for (from
= to
= 0, loglow
= -1; from
< leafhdr
->count
; from
++) {
989 if (leafhdr
->ents
[from
].address
==
990 cpu_to_be32(XFS_DIR2_NULL_DATAPTR
))
993 * Only actually copy the entries that are different.
998 leafhdr
->ents
[to
] = leafhdr
->ents
[from
];
1003 * Update and log the header, log the leaf entries.
1005 ASSERT(leafhdr
->stale
== from
- to
);
1006 leafhdr
->count
-= leafhdr
->stale
;
1009 xfs_dir2_leaf_hdr_to_disk(dp
->i_mount
, leaf
, leafhdr
);
1010 xfs_dir3_leaf_log_header(args
, bp
);
1012 xfs_dir3_leaf_log_ents(args
, leafhdr
, bp
, loglow
, to
- 1);
1016 * Compact the leaf entries, removing stale ones.
1017 * Leave one stale entry behind - the one closest to our
1018 * insertion index - and the caller will shift that one to our insertion
1020 * Return new insertion index, where the remaining stale entry is,
1021 * and leaf logging indices.
1024 xfs_dir3_leaf_compact_x1(
1025 struct xfs_dir3_icleaf_hdr
*leafhdr
,
1026 struct xfs_dir2_leaf_entry
*ents
,
1027 int *indexp
, /* insertion index */
1028 int *lowstalep
, /* out: stale entry before us */
1029 int *highstalep
, /* out: stale entry after us */
1030 int *lowlogp
, /* out: low log index */
1031 int *highlogp
) /* out: high log index */
1033 int from
; /* source copy index */
1034 int highstale
; /* stale entry at/after index */
1035 int index
; /* insertion index */
1036 int keepstale
; /* source index of kept stale */
1037 int lowstale
; /* stale entry before index */
1038 int newindex
=0; /* new insertion index */
1039 int to
; /* destination copy index */
1041 ASSERT(leafhdr
->stale
> 1);
1044 xfs_dir3_leaf_find_stale(leafhdr
, ents
, index
, &lowstale
, &highstale
);
1047 * Pick the better of lowstale and highstale.
1049 if (lowstale
>= 0 &&
1050 (highstale
== leafhdr
->count
||
1051 index
- lowstale
<= highstale
- index
))
1052 keepstale
= lowstale
;
1054 keepstale
= highstale
;
1056 * Copy the entries in place, removing all the stale entries
1059 for (from
= to
= 0; from
< leafhdr
->count
; from
++) {
1061 * Notice the new value of index.
1065 if (from
!= keepstale
&&
1066 ents
[from
].address
== cpu_to_be32(XFS_DIR2_NULL_DATAPTR
)) {
1072 * Record the new keepstale value for the insertion.
1074 if (from
== keepstale
)
1075 lowstale
= highstale
= to
;
1077 * Copy only the entries that have moved.
1080 ents
[to
] = ents
[from
];
1085 * If the insertion point was past the last entry,
1086 * set the new insertion point accordingly.
1092 * Adjust the leaf header values.
1094 leafhdr
->count
-= from
- to
;
1097 * Remember the low/high stale value only in the "right"
1100 if (lowstale
>= newindex
)
1103 highstale
= leafhdr
->count
;
1104 *highlogp
= leafhdr
->count
- 1;
1105 *lowstalep
= lowstale
;
1106 *highstalep
= highstale
;
1110 * Log the bests entries indicated from a leaf1 block.
1113 xfs_dir3_leaf_log_bests(
1114 struct xfs_da_args
*args
,
1115 struct xfs_buf
*bp
, /* leaf buffer */
1116 int first
, /* first entry to log */
1117 int last
) /* last entry to log */
1119 __be16
*firstb
; /* pointer to first entry */
1120 __be16
*lastb
; /* pointer to last entry */
1121 struct xfs_dir2_leaf
*leaf
= bp
->b_addr
;
1122 xfs_dir2_leaf_tail_t
*ltp
; /* leaf tail structure */
1124 ASSERT(leaf
->hdr
.info
.magic
== cpu_to_be16(XFS_DIR2_LEAF1_MAGIC
) ||
1125 leaf
->hdr
.info
.magic
== cpu_to_be16(XFS_DIR3_LEAF1_MAGIC
));
1127 ltp
= xfs_dir2_leaf_tail_p(args
->geo
, leaf
);
1128 firstb
= xfs_dir2_leaf_bests_p(ltp
) + first
;
1129 lastb
= xfs_dir2_leaf_bests_p(ltp
) + last
;
1130 xfs_trans_log_buf(args
->trans
, bp
,
1131 (uint
)((char *)firstb
- (char *)leaf
),
1132 (uint
)((char *)lastb
- (char *)leaf
+ sizeof(*lastb
) - 1));
1136 * Log the leaf entries indicated from a leaf1 or leafn block.
1139 xfs_dir3_leaf_log_ents(
1140 struct xfs_da_args
*args
,
1141 struct xfs_dir3_icleaf_hdr
*hdr
,
1146 xfs_dir2_leaf_entry_t
*firstlep
; /* pointer to first entry */
1147 xfs_dir2_leaf_entry_t
*lastlep
; /* pointer to last entry */
1148 struct xfs_dir2_leaf
*leaf
= bp
->b_addr
;
1150 ASSERT(leaf
->hdr
.info
.magic
== cpu_to_be16(XFS_DIR2_LEAF1_MAGIC
) ||
1151 leaf
->hdr
.info
.magic
== cpu_to_be16(XFS_DIR3_LEAF1_MAGIC
) ||
1152 leaf
->hdr
.info
.magic
== cpu_to_be16(XFS_DIR2_LEAFN_MAGIC
) ||
1153 leaf
->hdr
.info
.magic
== cpu_to_be16(XFS_DIR3_LEAFN_MAGIC
));
1155 firstlep
= &hdr
->ents
[first
];
1156 lastlep
= &hdr
->ents
[last
];
1157 xfs_trans_log_buf(args
->trans
, bp
,
1158 (uint
)((char *)firstlep
- (char *)leaf
),
1159 (uint
)((char *)lastlep
- (char *)leaf
+ sizeof(*lastlep
) - 1));
1163 * Log the header of the leaf1 or leafn block.
1166 xfs_dir3_leaf_log_header(
1167 struct xfs_da_args
*args
,
1170 struct xfs_dir2_leaf
*leaf
= bp
->b_addr
;
1172 ASSERT(leaf
->hdr
.info
.magic
== cpu_to_be16(XFS_DIR2_LEAF1_MAGIC
) ||
1173 leaf
->hdr
.info
.magic
== cpu_to_be16(XFS_DIR3_LEAF1_MAGIC
) ||
1174 leaf
->hdr
.info
.magic
== cpu_to_be16(XFS_DIR2_LEAFN_MAGIC
) ||
1175 leaf
->hdr
.info
.magic
== cpu_to_be16(XFS_DIR3_LEAFN_MAGIC
));
1177 xfs_trans_log_buf(args
->trans
, bp
,
1178 (uint
)((char *)&leaf
->hdr
- (char *)leaf
),
1179 args
->geo
->leaf_hdr_size
- 1);
1183 * Log the tail of the leaf1 block.
1186 xfs_dir3_leaf_log_tail(
1187 struct xfs_da_args
*args
,
1190 struct xfs_dir2_leaf
*leaf
= bp
->b_addr
;
1191 xfs_dir2_leaf_tail_t
*ltp
; /* leaf tail structure */
1193 ASSERT(leaf
->hdr
.info
.magic
== cpu_to_be16(XFS_DIR2_LEAF1_MAGIC
) ||
1194 leaf
->hdr
.info
.magic
== cpu_to_be16(XFS_DIR3_LEAF1_MAGIC
) ||
1195 leaf
->hdr
.info
.magic
== cpu_to_be16(XFS_DIR2_LEAFN_MAGIC
) ||
1196 leaf
->hdr
.info
.magic
== cpu_to_be16(XFS_DIR3_LEAFN_MAGIC
));
1198 ltp
= xfs_dir2_leaf_tail_p(args
->geo
, leaf
);
1199 xfs_trans_log_buf(args
->trans
, bp
, (uint
)((char *)ltp
- (char *)leaf
),
1200 (uint
)(args
->geo
->blksize
- 1));
1204 * Look up the entry referred to by args in the leaf format directory.
1205 * Most of the work is done by the xfs_dir2_leaf_lookup_int routine which
1206 * is also used by the node-format code.
1209 xfs_dir2_leaf_lookup(
1210 xfs_da_args_t
*args
) /* operation arguments */
1212 struct xfs_buf
*dbp
; /* data block buffer */
1213 xfs_dir2_data_entry_t
*dep
; /* data block entry */
1214 xfs_inode_t
*dp
; /* incore directory inode */
1215 int error
; /* error return code */
1216 int index
; /* found entry index */
1217 struct xfs_buf
*lbp
; /* leaf buffer */
1218 xfs_dir2_leaf_entry_t
*lep
; /* leaf entry */
1219 xfs_trans_t
*tp
; /* transaction pointer */
1220 struct xfs_dir3_icleaf_hdr leafhdr
;
1222 trace_xfs_dir2_leaf_lookup(args
);
1225 * Look up name in the leaf block, returning both buffers and index.
1227 error
= xfs_dir2_leaf_lookup_int(args
, &lbp
, &index
, &dbp
, &leafhdr
);
1233 xfs_dir3_leaf_check(dp
, lbp
);
1236 * Get to the leaf entry and contained data entry address.
1238 lep
= &leafhdr
.ents
[index
];
1241 * Point to the data entry.
1243 dep
= (xfs_dir2_data_entry_t
*)
1244 ((char *)dbp
->b_addr
+
1245 xfs_dir2_dataptr_to_off(args
->geo
, be32_to_cpu(lep
->address
)));
1247 * Return the found inode number & CI name if appropriate
1249 args
->inumber
= be64_to_cpu(dep
->inumber
);
1250 args
->filetype
= xfs_dir2_data_get_ftype(dp
->i_mount
, dep
);
1251 error
= xfs_dir_cilookup_result(args
, dep
->name
, dep
->namelen
);
1252 xfs_trans_brelse(tp
, dbp
);
1253 xfs_trans_brelse(tp
, lbp
);
1258 * Look up name/hash in the leaf block.
1259 * Fill in indexp with the found index, and dbpp with the data buffer.
1260 * If not found dbpp will be NULL, and ENOENT comes back.
1261 * lbpp will always be filled in with the leaf buffer unless there's an error.
1263 static int /* error */
1264 xfs_dir2_leaf_lookup_int(
1265 xfs_da_args_t
*args
, /* operation arguments */
1266 struct xfs_buf
**lbpp
, /* out: leaf buffer */
1267 int *indexp
, /* out: index in leaf block */
1268 struct xfs_buf
**dbpp
, /* out: data buffer */
1269 struct xfs_dir3_icleaf_hdr
*leafhdr
)
1271 xfs_dir2_db_t curdb
= -1; /* current data block number */
1272 struct xfs_buf
*dbp
= NULL
; /* data buffer */
1273 xfs_dir2_data_entry_t
*dep
; /* data entry */
1274 xfs_inode_t
*dp
; /* incore directory inode */
1275 int error
; /* error return code */
1276 int index
; /* index in leaf block */
1277 struct xfs_buf
*lbp
; /* leaf buffer */
1278 xfs_dir2_leaf_entry_t
*lep
; /* leaf entry */
1279 xfs_dir2_leaf_t
*leaf
; /* leaf structure */
1280 xfs_mount_t
*mp
; /* filesystem mount point */
1281 xfs_dir2_db_t newdb
; /* new data block number */
1282 xfs_trans_t
*tp
; /* transaction pointer */
1283 xfs_dir2_db_t cidb
= -1; /* case match data block no. */
1284 enum xfs_dacmp cmp
; /* name compare result */
1290 error
= xfs_dir3_leaf_read(tp
, dp
, args
->owner
, args
->geo
->leafblk
,
1297 xfs_dir3_leaf_check(dp
, lbp
);
1298 xfs_dir2_leaf_hdr_from_disk(mp
, leafhdr
, leaf
);
1301 * Look for the first leaf entry with our hash value.
1303 index
= xfs_dir2_leaf_search_hash(args
, lbp
);
1305 * Loop over all the entries with the right hash value
1306 * looking to match the name.
1308 for (lep
= &leafhdr
->ents
[index
];
1309 index
< leafhdr
->count
&&
1310 be32_to_cpu(lep
->hashval
) == args
->hashval
;
1313 * Skip over stale leaf entries.
1315 if (be32_to_cpu(lep
->address
) == XFS_DIR2_NULL_DATAPTR
)
1318 * Get the new data block number.
1320 newdb
= xfs_dir2_dataptr_to_db(args
->geo
,
1321 be32_to_cpu(lep
->address
));
1323 * If it's not the same as the old data block number,
1324 * need to pitch the old one and read the new one.
1326 if (newdb
!= curdb
) {
1328 xfs_trans_brelse(tp
, dbp
);
1329 error
= xfs_dir3_data_read(tp
, dp
, args
->owner
,
1330 xfs_dir2_db_to_da(args
->geo
, newdb
), 0,
1333 xfs_trans_brelse(tp
, lbp
);
1339 * Point to the data entry.
1341 dep
= (xfs_dir2_data_entry_t
*)((char *)dbp
->b_addr
+
1342 xfs_dir2_dataptr_to_off(args
->geo
,
1343 be32_to_cpu(lep
->address
)));
1345 * Compare name and if it's an exact match, return the index
1346 * and buffer. If it's the first case-insensitive match, store
1347 * the index and buffer and continue looking for an exact match.
1349 cmp
= xfs_dir2_compname(args
, dep
->name
, dep
->namelen
);
1350 if (cmp
!= XFS_CMP_DIFFERENT
&& cmp
!= args
->cmpresult
) {
1351 args
->cmpresult
= cmp
;
1353 /* case exact match: return the current buffer. */
1354 if (cmp
== XFS_CMP_EXACT
) {
1361 ASSERT(args
->op_flags
& XFS_DA_OP_OKNOENT
);
1363 * Here, we can only be doing a lookup (not a rename or remove).
1364 * If a case-insensitive match was found earlier, re-read the
1365 * appropriate data block if required and return it.
1367 if (args
->cmpresult
== XFS_CMP_CASE
) {
1369 if (cidb
!= curdb
) {
1370 xfs_trans_brelse(tp
, dbp
);
1371 error
= xfs_dir3_data_read(tp
, dp
, args
->owner
,
1372 xfs_dir2_db_to_da(args
->geo
, cidb
), 0,
1375 xfs_trans_brelse(tp
, lbp
);
1383 * No match found, return -ENOENT.
1387 xfs_trans_brelse(tp
, dbp
);
1388 xfs_trans_brelse(tp
, lbp
);
1393 * Remove an entry from a leaf format directory.
1396 xfs_dir2_leaf_removename(
1397 xfs_da_args_t
*args
) /* operation arguments */
1399 struct xfs_da_geometry
*geo
= args
->geo
;
1400 __be16
*bestsp
; /* leaf block best freespace */
1401 xfs_dir2_data_hdr_t
*hdr
; /* data block header */
1402 xfs_dir2_db_t db
; /* data block number */
1403 struct xfs_buf
*dbp
; /* data block buffer */
1404 xfs_dir2_data_entry_t
*dep
; /* data entry structure */
1405 xfs_inode_t
*dp
; /* incore directory inode */
1406 int error
; /* error return code */
1407 xfs_dir2_db_t i
; /* temporary data block # */
1408 int index
; /* index into leaf entries */
1409 struct xfs_buf
*lbp
; /* leaf buffer */
1410 xfs_dir2_leaf_t
*leaf
; /* leaf structure */
1411 xfs_dir2_leaf_entry_t
*lep
; /* leaf entry */
1412 xfs_dir2_leaf_tail_t
*ltp
; /* leaf tail structure */
1413 int needlog
; /* need to log data header */
1414 int needscan
; /* need to rescan data frees */
1415 xfs_dir2_data_off_t oldbest
; /* old value of best free */
1416 struct xfs_dir2_data_free
*bf
; /* bestfree table */
1417 struct xfs_dir3_icleaf_hdr leafhdr
;
1419 trace_xfs_dir2_leaf_removename(args
);
1422 * Lookup the leaf entry, get the leaf and data blocks read in.
1424 error
= xfs_dir2_leaf_lookup_int(args
, &lbp
, &index
, &dbp
, &leafhdr
);
1431 xfs_dir3_data_check(dp
, dbp
);
1432 bf
= xfs_dir2_data_bestfree_p(dp
->i_mount
, hdr
);
1435 * Point to the leaf entry, use that to point to the data entry.
1437 lep
= &leafhdr
.ents
[index
];
1438 db
= xfs_dir2_dataptr_to_db(geo
, be32_to_cpu(lep
->address
));
1439 dep
= (xfs_dir2_data_entry_t
*)((char *)hdr
+
1440 xfs_dir2_dataptr_to_off(geo
, be32_to_cpu(lep
->address
)));
1441 needscan
= needlog
= 0;
1442 oldbest
= be16_to_cpu(bf
[0].length
);
1443 ltp
= xfs_dir2_leaf_tail_p(geo
, leaf
);
1444 bestsp
= xfs_dir2_leaf_bests_p(ltp
);
1445 if (be16_to_cpu(bestsp
[db
]) != oldbest
) {
1446 xfs_buf_mark_corrupt(lbp
);
1447 xfs_da_mark_sick(args
);
1448 return -EFSCORRUPTED
;
1452 * Mark the former data entry unused.
1454 xfs_dir2_data_make_free(args
, dbp
,
1455 (xfs_dir2_data_aoff_t
)((char *)dep
- (char *)hdr
),
1456 xfs_dir2_data_entsize(dp
->i_mount
, dep
->namelen
), &needlog
,
1459 * We just mark the leaf entry stale by putting a null in it.
1462 xfs_dir2_leaf_hdr_to_disk(dp
->i_mount
, leaf
, &leafhdr
);
1463 xfs_dir3_leaf_log_header(args
, lbp
);
1465 lep
->address
= cpu_to_be32(XFS_DIR2_NULL_DATAPTR
);
1466 xfs_dir3_leaf_log_ents(args
, &leafhdr
, lbp
, index
, index
);
1469 * Scan the freespace in the data block again if necessary,
1470 * log the data block header if necessary.
1473 xfs_dir2_data_freescan(dp
->i_mount
, hdr
, &needlog
);
1475 xfs_dir2_data_log_header(args
, dbp
);
1477 * If the longest freespace in the data block has changed,
1478 * put the new value in the bests table and log that.
1480 if (be16_to_cpu(bf
[0].length
) != oldbest
) {
1481 bestsp
[db
] = bf
[0].length
;
1482 xfs_dir3_leaf_log_bests(args
, lbp
, db
, db
);
1484 xfs_dir3_data_check(dp
, dbp
);
1486 * If the data block is now empty then get rid of the data block.
1488 if (be16_to_cpu(bf
[0].length
) ==
1489 geo
->blksize
- geo
->data_entry_offset
) {
1490 ASSERT(db
!= geo
->datablk
);
1491 if ((error
= xfs_dir2_shrink_inode(args
, db
, dbp
))) {
1493 * Nope, can't get rid of it because it caused
1494 * allocation of a bmap btree block to do so.
1495 * Just go on, returning success, leaving the
1496 * empty block in place.
1498 if (error
== -ENOSPC
&& args
->total
== 0)
1500 xfs_dir3_leaf_check(dp
, lbp
);
1505 * If this is the last data block then compact the
1506 * bests table by getting rid of entries.
1508 if (db
== be32_to_cpu(ltp
->bestcount
) - 1) {
1510 * Look for the last active entry (i).
1512 for (i
= db
- 1; i
> 0; i
--) {
1513 if (bestsp
[i
] != cpu_to_be16(NULLDATAOFF
))
1517 * Copy the table down so inactive entries at the
1520 memmove(&bestsp
[db
- i
], bestsp
,
1521 (be32_to_cpu(ltp
->bestcount
) - (db
- i
)) * sizeof(*bestsp
));
1522 be32_add_cpu(<p
->bestcount
, -(db
- i
));
1523 xfs_dir3_leaf_log_tail(args
, lbp
);
1524 xfs_dir3_leaf_log_bests(args
, lbp
, 0,
1525 be32_to_cpu(ltp
->bestcount
) - 1);
1527 bestsp
[db
] = cpu_to_be16(NULLDATAOFF
);
1530 * If the data block was not the first one, drop it.
1532 else if (db
!= geo
->datablk
)
1535 xfs_dir3_leaf_check(dp
, lbp
);
1537 * See if we can convert to block form.
1539 return xfs_dir2_leaf_to_block(args
, lbp
, dbp
);
1543 * Replace the inode number in a leaf format directory entry.
1546 xfs_dir2_leaf_replace(
1547 xfs_da_args_t
*args
) /* operation arguments */
1549 struct xfs_buf
*dbp
; /* data block buffer */
1550 xfs_dir2_data_entry_t
*dep
; /* data block entry */
1551 xfs_inode_t
*dp
; /* incore directory inode */
1552 int error
; /* error return code */
1553 int index
; /* index of leaf entry */
1554 struct xfs_buf
*lbp
; /* leaf buffer */
1555 xfs_dir2_leaf_entry_t
*lep
; /* leaf entry */
1556 xfs_trans_t
*tp
; /* transaction pointer */
1557 struct xfs_dir3_icleaf_hdr leafhdr
;
1559 trace_xfs_dir2_leaf_replace(args
);
1562 * Look up the entry.
1564 error
= xfs_dir2_leaf_lookup_int(args
, &lbp
, &index
, &dbp
, &leafhdr
);
1570 * Point to the leaf entry, get data address from it.
1572 lep
= &leafhdr
.ents
[index
];
1574 * Point to the data entry.
1576 dep
= (xfs_dir2_data_entry_t
*)
1577 ((char *)dbp
->b_addr
+
1578 xfs_dir2_dataptr_to_off(args
->geo
, be32_to_cpu(lep
->address
)));
1579 ASSERT(args
->inumber
!= be64_to_cpu(dep
->inumber
));
1581 * Put the new inode number in, log it.
1583 dep
->inumber
= cpu_to_be64(args
->inumber
);
1584 xfs_dir2_data_put_ftype(dp
->i_mount
, dep
, args
->filetype
);
1586 xfs_dir2_data_log_entry(args
, dbp
, dep
);
1587 xfs_dir3_leaf_check(dp
, lbp
);
1588 xfs_trans_brelse(tp
, lbp
);
1593 * Return index in the leaf block (lbp) which is either the first
1594 * one with this hash value, or if there are none, the insert point
1595 * for that hash value.
1597 int /* index value */
1598 xfs_dir2_leaf_search_hash(
1599 xfs_da_args_t
*args
, /* operation arguments */
1600 struct xfs_buf
*lbp
) /* leaf buffer */
1602 xfs_dahash_t hash
=0; /* hash from this entry */
1603 xfs_dahash_t hashwant
; /* hash value looking for */
1604 int high
; /* high leaf index */
1605 int low
; /* low leaf index */
1606 xfs_dir2_leaf_entry_t
*lep
; /* leaf entry */
1607 int mid
=0; /* current leaf index */
1608 struct xfs_dir3_icleaf_hdr leafhdr
;
1610 xfs_dir2_leaf_hdr_from_disk(args
->dp
->i_mount
, &leafhdr
, lbp
->b_addr
);
1613 * Note, the table cannot be empty, so we have to go through the loop.
1614 * Binary search the leaf entries looking for our hash value.
1616 for (lep
= leafhdr
.ents
, low
= 0, high
= leafhdr
.count
- 1,
1617 hashwant
= args
->hashval
;
1619 mid
= (low
+ high
) >> 1;
1620 if ((hash
= be32_to_cpu(lep
[mid
].hashval
)) == hashwant
)
1622 if (hash
< hashwant
)
1628 * Found one, back up through all the equal hash values.
1630 if (hash
== hashwant
) {
1631 while (mid
> 0 && be32_to_cpu(lep
[mid
- 1].hashval
) == hashwant
) {
1636 * Need to point to an entry higher than ours.
1638 else if (hash
< hashwant
)
1644 * Trim off a trailing data block. We know it's empty since the leaf
1645 * freespace table says so.
1648 xfs_dir2_leaf_trim_data(
1649 xfs_da_args_t
*args
, /* operation arguments */
1650 struct xfs_buf
*lbp
, /* leaf buffer */
1651 xfs_dir2_db_t db
) /* data block number */
1653 struct xfs_da_geometry
*geo
= args
->geo
;
1654 __be16
*bestsp
; /* leaf bests table */
1655 struct xfs_buf
*dbp
; /* data block buffer */
1656 xfs_inode_t
*dp
; /* incore directory inode */
1657 int error
; /* error return value */
1658 xfs_dir2_leaf_t
*leaf
; /* leaf structure */
1659 xfs_dir2_leaf_tail_t
*ltp
; /* leaf tail structure */
1660 xfs_trans_t
*tp
; /* transaction pointer */
1665 * Read the offending data block. We need its buffer.
1667 error
= xfs_dir3_data_read(tp
, dp
, args
->owner
,
1668 xfs_dir2_db_to_da(geo
, db
), 0, &dbp
);
1673 ltp
= xfs_dir2_leaf_tail_p(geo
, leaf
);
1677 struct xfs_dir2_data_hdr
*hdr
= dbp
->b_addr
;
1678 struct xfs_dir2_data_free
*bf
=
1679 xfs_dir2_data_bestfree_p(dp
->i_mount
, hdr
);
1681 ASSERT(hdr
->magic
== cpu_to_be32(XFS_DIR2_DATA_MAGIC
) ||
1682 hdr
->magic
== cpu_to_be32(XFS_DIR3_DATA_MAGIC
));
1683 ASSERT(be16_to_cpu(bf
[0].length
) ==
1684 geo
->blksize
- geo
->data_entry_offset
);
1685 ASSERT(db
== be32_to_cpu(ltp
->bestcount
) - 1);
1690 * Get rid of the data block.
1692 if ((error
= xfs_dir2_shrink_inode(args
, db
, dbp
))) {
1693 ASSERT(error
!= -ENOSPC
);
1694 xfs_trans_brelse(tp
, dbp
);
1698 * Eliminate the last bests entry from the table.
1700 bestsp
= xfs_dir2_leaf_bests_p(ltp
);
1701 be32_add_cpu(<p
->bestcount
, -1);
1702 memmove(&bestsp
[1], &bestsp
[0], be32_to_cpu(ltp
->bestcount
) * sizeof(*bestsp
));
1703 xfs_dir3_leaf_log_tail(args
, lbp
);
1704 xfs_dir3_leaf_log_bests(args
, lbp
, 0, be32_to_cpu(ltp
->bestcount
) - 1);
1708 static inline size_t
1710 struct xfs_dir3_icleaf_hdr
*hdr
,
1716 entries
= hdr
->count
- hdr
->stale
;
1717 if (hdr
->magic
== XFS_DIR2_LEAF1_MAGIC
||
1718 hdr
->magic
== XFS_DIR2_LEAFN_MAGIC
)
1719 hdrsize
= sizeof(struct xfs_dir2_leaf_hdr
);
1721 hdrsize
= sizeof(struct xfs_dir3_leaf_hdr
);
1723 return hdrsize
+ entries
* sizeof(xfs_dir2_leaf_entry_t
)
1724 + counts
* sizeof(xfs_dir2_data_off_t
)
1725 + sizeof(xfs_dir2_leaf_tail_t
);
1729 * Convert node form directory to leaf form directory.
1730 * The root of the node form dir needs to already be a LEAFN block.
1731 * Just return if we can't do anything.
1734 xfs_dir2_node_to_leaf(
1735 xfs_da_state_t
*state
) /* directory operation state */
1737 xfs_da_args_t
*args
; /* operation arguments */
1738 xfs_inode_t
*dp
; /* incore directory inode */
1739 int error
; /* error return code */
1740 struct xfs_buf
*fbp
; /* buffer for freespace block */
1741 xfs_fileoff_t fo
; /* freespace file offset */
1742 struct xfs_buf
*lbp
; /* buffer for leaf block */
1743 xfs_dir2_leaf_tail_t
*ltp
; /* tail of leaf structure */
1744 xfs_dir2_leaf_t
*leaf
; /* leaf structure */
1745 xfs_mount_t
*mp
; /* filesystem mount point */
1746 int rval
; /* successful free trim? */
1747 xfs_trans_t
*tp
; /* transaction pointer */
1748 struct xfs_dir3_icleaf_hdr leafhdr
;
1749 struct xfs_dir3_icfree_hdr freehdr
;
1752 * There's more than a leaf level in the btree, so there must
1753 * be multiple leafn blocks. Give up.
1755 if (state
->path
.active
> 1)
1759 trace_xfs_dir2_node_to_leaf(args
);
1765 * Get the last offset in the file.
1767 if ((error
= xfs_bmap_last_offset(dp
, &fo
, XFS_DATA_FORK
))) {
1770 fo
-= args
->geo
->fsbcount
;
1772 * If there are freespace blocks other than the first one,
1773 * take this opportunity to remove trailing empty freespace blocks
1774 * that may have been left behind during no-space-reservation
1777 while (fo
> args
->geo
->freeblk
) {
1778 if ((error
= xfs_dir2_node_trim_free(args
, fo
, &rval
))) {
1782 fo
-= args
->geo
->fsbcount
;
1787 * Now find the block just before the freespace block.
1789 if ((error
= xfs_bmap_last_before(tp
, dp
, &fo
, XFS_DATA_FORK
))) {
1793 * If it's not the single leaf block, give up.
1795 if (XFS_FSB_TO_B(mp
, fo
) > XFS_DIR2_LEAF_OFFSET
+ args
->geo
->blksize
)
1797 lbp
= state
->path
.blk
[0].bp
;
1799 xfs_dir2_leaf_hdr_from_disk(mp
, &leafhdr
, leaf
);
1801 ASSERT(leafhdr
.magic
== XFS_DIR2_LEAFN_MAGIC
||
1802 leafhdr
.magic
== XFS_DIR3_LEAFN_MAGIC
);
1805 * Read the freespace block.
1807 error
= xfs_dir2_free_read(tp
, dp
, args
->owner
, args
->geo
->freeblk
,
1811 xfs_dir2_free_hdr_from_disk(mp
, &freehdr
, fbp
->b_addr
);
1813 ASSERT(!freehdr
.firstdb
);
1816 * Now see if the leafn and free data will fit in a leaf1.
1817 * If not, release the buffer and give up.
1819 if (xfs_dir3_leaf_size(&leafhdr
, freehdr
.nvalid
) > args
->geo
->blksize
) {
1820 xfs_trans_brelse(tp
, fbp
);
1825 * If the leaf has any stale entries in it, compress them out.
1828 xfs_dir3_leaf_compact(args
, &leafhdr
, lbp
);
1830 lbp
->b_ops
= &xfs_dir3_leaf1_buf_ops
;
1831 xfs_trans_buf_set_type(tp
, lbp
, XFS_BLFT_DIR_LEAF1_BUF
);
1832 leafhdr
.magic
= (leafhdr
.magic
== XFS_DIR2_LEAFN_MAGIC
)
1833 ? XFS_DIR2_LEAF1_MAGIC
1834 : XFS_DIR3_LEAF1_MAGIC
;
1837 * Set up the leaf tail from the freespace block.
1839 ltp
= xfs_dir2_leaf_tail_p(args
->geo
, leaf
);
1840 ltp
->bestcount
= cpu_to_be32(freehdr
.nvalid
);
1843 * Set up the leaf bests table.
1845 memcpy(xfs_dir2_leaf_bests_p(ltp
), freehdr
.bests
,
1846 freehdr
.nvalid
* sizeof(xfs_dir2_data_off_t
));
1848 xfs_dir2_leaf_hdr_to_disk(mp
, leaf
, &leafhdr
);
1849 xfs_dir3_leaf_log_header(args
, lbp
);
1850 xfs_dir3_leaf_log_bests(args
, lbp
, 0, be32_to_cpu(ltp
->bestcount
) - 1);
1851 xfs_dir3_leaf_log_tail(args
, lbp
);
1852 xfs_dir3_leaf_check(dp
, lbp
);
1855 * Get rid of the freespace block.
1857 error
= xfs_dir2_shrink_inode(args
,
1858 xfs_dir2_byte_to_db(args
->geo
, XFS_DIR2_FREE_OFFSET
),
1862 * This can't fail here because it can only happen when
1863 * punching out the middle of an extent, and this is an
1866 ASSERT(error
!= -ENOSPC
);
1871 * Now see if we can convert the single-leaf directory
1872 * down to a block form directory.
1873 * This routine always kills the dabuf for the leaf, so
1874 * eliminate it from the path.
1876 error
= xfs_dir2_leaf_to_block(args
, lbp
, NULL
);
1877 state
->path
.blk
[0].bp
= NULL
;