2 * Copyright (c) 2000-2003,2005 Silicon Graphics, Inc.
3 * Copyright (c) 2013 Red Hat, Inc.
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as
8 * published by the Free Software Foundation.
10 * This program is distributed in the hope that it would be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 #include "libxfs_priv.h"
21 #include "xfs_format.h"
22 #include "xfs_log_format.h"
23 #include "xfs_trans_resv.h"
24 #include "xfs_mount.h"
25 #include "xfs_da_format.h"
26 #include "xfs_da_btree.h"
27 #include "xfs_inode.h"
30 #include "xfs_dir2_priv.h"
31 #include "xfs_trace.h"
32 #include "xfs_trans.h"
33 #include "xfs_cksum.h"
36 * Local function declarations.
38 static int xfs_dir2_leaf_lookup_int(xfs_da_args_t
*args
, struct xfs_buf
**lbpp
,
39 int *indexp
, struct xfs_buf
**dbpp
);
40 static void xfs_dir3_leaf_log_bests(struct xfs_da_args
*args
,
41 struct xfs_buf
*bp
, int first
, int last
);
42 static void xfs_dir3_leaf_log_tail(struct xfs_da_args
*args
,
46 * Check the internal consistency of a leaf1 block.
47 * Pop an assert if something is wrong.
55 struct xfs_dir2_leaf
*leaf
= bp
->b_addr
;
56 struct xfs_dir3_icleaf_hdr leafhdr
;
58 dp
->d_ops
->leaf_hdr_from_disk(&leafhdr
, leaf
);
60 if (leafhdr
.magic
== XFS_DIR3_LEAF1_MAGIC
) {
61 struct xfs_dir3_leaf_hdr
*leaf3
= bp
->b_addr
;
62 if (be64_to_cpu(leaf3
->info
.blkno
) != bp
->b_bn
)
63 return __this_address
;
64 } else if (leafhdr
.magic
!= XFS_DIR2_LEAF1_MAGIC
)
65 return __this_address
;
67 return xfs_dir3_leaf_check_int(dp
->i_mount
, dp
, &leafhdr
, leaf
);
77 fa
= xfs_dir3_leaf1_check(dp
, bp
);
80 xfs_corruption_error(__func__
, XFS_ERRLEVEL_LOW
, dp
->i_mount
,
81 bp
->b_addr
, __FILE__
, __LINE__
, fa
);
85 #define xfs_dir3_leaf_check(dp, bp)
89 xfs_dir3_leaf_check_int(
92 struct xfs_dir3_icleaf_hdr
*hdr
,
93 struct xfs_dir2_leaf
*leaf
)
95 struct xfs_dir2_leaf_entry
*ents
;
96 xfs_dir2_leaf_tail_t
*ltp
;
99 const struct xfs_dir_ops
*ops
;
100 struct xfs_dir3_icleaf_hdr leafhdr
;
101 struct xfs_da_geometry
*geo
= mp
->m_dir_geo
;
104 * we can be passed a null dp here from a verifier, so we need to go the
105 * hard way to get them.
107 ops
= xfs_dir_get_ops(mp
, dp
);
110 ops
->leaf_hdr_from_disk(&leafhdr
, leaf
);
114 ents
= ops
->leaf_ents_p(leaf
);
115 ltp
= xfs_dir2_leaf_tail_p(geo
, leaf
);
118 * XXX (dgc): This value is not restrictive enough.
119 * Should factor in the size of the bests table as well.
120 * We can deduce a value for that from di_size.
122 if (hdr
->count
> ops
->leaf_max_ents(geo
))
123 return __this_address
;
125 /* Leaves and bests don't overlap in leaf format. */
126 if ((hdr
->magic
== XFS_DIR2_LEAF1_MAGIC
||
127 hdr
->magic
== XFS_DIR3_LEAF1_MAGIC
) &&
128 (char *)&ents
[hdr
->count
] > (char *)xfs_dir2_leaf_bests_p(ltp
))
129 return __this_address
;
131 /* Check hash value order, count stale entries. */
132 for (i
= stale
= 0; i
< hdr
->count
; i
++) {
133 if (i
+ 1 < hdr
->count
) {
134 if (be32_to_cpu(ents
[i
].hashval
) >
135 be32_to_cpu(ents
[i
+ 1].hashval
))
136 return __this_address
;
138 if (ents
[i
].address
== cpu_to_be32(XFS_DIR2_NULL_DATAPTR
))
141 if (hdr
->stale
!= stale
)
142 return __this_address
;
147 * We verify the magic numbers before decoding the leaf header so that on debug
148 * kernels we don't get assertion failures in xfs_dir3_leaf_hdr_from_disk() due
149 * to incorrect magic numbers.
151 static xfs_failaddr_t
152 xfs_dir3_leaf_verify(
156 struct xfs_mount
*mp
= bp
->b_target
->bt_mount
;
157 struct xfs_dir2_leaf
*leaf
= bp
->b_addr
;
159 ASSERT(magic
== XFS_DIR2_LEAF1_MAGIC
|| magic
== XFS_DIR2_LEAFN_MAGIC
);
161 if (xfs_sb_version_hascrc(&mp
->m_sb
)) {
162 struct xfs_dir3_leaf_hdr
*leaf3
= bp
->b_addr
;
165 magic3
= (magic
== XFS_DIR2_LEAF1_MAGIC
) ? XFS_DIR3_LEAF1_MAGIC
166 : XFS_DIR3_LEAFN_MAGIC
;
168 if (leaf3
->info
.hdr
.magic
!= cpu_to_be16(magic3
))
169 return __this_address
;
170 if (!uuid_equal(&leaf3
->info
.uuid
, &mp
->m_sb
.sb_meta_uuid
))
171 return __this_address
;
172 if (be64_to_cpu(leaf3
->info
.blkno
) != bp
->b_bn
)
173 return __this_address
;
174 if (!xfs_log_check_lsn(mp
, be64_to_cpu(leaf3
->info
.lsn
)))
175 return __this_address
;
177 if (leaf
->hdr
.info
.magic
!= cpu_to_be16(magic
))
178 return __this_address
;
181 return xfs_dir3_leaf_check_int(mp
, NULL
, NULL
, leaf
);
189 struct xfs_mount
*mp
= bp
->b_target
->bt_mount
;
192 if (xfs_sb_version_hascrc(&mp
->m_sb
) &&
193 !xfs_buf_verify_cksum(bp
, XFS_DIR3_LEAF_CRC_OFF
))
194 xfs_verifier_error(bp
, -EFSBADCRC
, __this_address
);
196 fa
= xfs_dir3_leaf_verify(bp
, magic
);
198 xfs_verifier_error(bp
, -EFSCORRUPTED
, fa
);
207 struct xfs_mount
*mp
= bp
->b_target
->bt_mount
;
208 struct xfs_buf_log_item
*bip
= bp
->b_fspriv
;
209 struct xfs_dir3_leaf_hdr
*hdr3
= bp
->b_addr
;
212 fa
= xfs_dir3_leaf_verify(bp
, magic
);
214 xfs_verifier_error(bp
, -EFSCORRUPTED
, fa
);
218 if (!xfs_sb_version_hascrc(&mp
->m_sb
))
222 hdr3
->info
.lsn
= cpu_to_be64(bip
->bli_item
.li_lsn
);
224 xfs_buf_update_cksum(bp
, XFS_DIR3_LEAF_CRC_OFF
);
227 static xfs_failaddr_t
228 xfs_dir3_leaf1_verify(
231 return xfs_dir3_leaf_verify(bp
, XFS_DIR2_LEAF1_MAGIC
);
235 xfs_dir3_leaf1_read_verify(
238 __read_verify(bp
, XFS_DIR2_LEAF1_MAGIC
);
242 xfs_dir3_leaf1_write_verify(
245 __write_verify(bp
, XFS_DIR2_LEAF1_MAGIC
);
248 static xfs_failaddr_t
249 xfs_dir3_leafn_verify(
252 return xfs_dir3_leaf_verify(bp
, XFS_DIR2_LEAFN_MAGIC
);
256 xfs_dir3_leafn_read_verify(
259 __read_verify(bp
, XFS_DIR2_LEAFN_MAGIC
);
263 xfs_dir3_leafn_write_verify(
266 __write_verify(bp
, XFS_DIR2_LEAFN_MAGIC
);
269 const struct xfs_buf_ops xfs_dir3_leaf1_buf_ops
= {
270 .name
= "xfs_dir3_leaf1",
271 .verify_read
= xfs_dir3_leaf1_read_verify
,
272 .verify_write
= xfs_dir3_leaf1_write_verify
,
273 .verify_struct
= xfs_dir3_leaf1_verify
,
276 const struct xfs_buf_ops xfs_dir3_leafn_buf_ops
= {
277 .name
= "xfs_dir3_leafn",
278 .verify_read
= xfs_dir3_leafn_read_verify
,
279 .verify_write
= xfs_dir3_leafn_write_verify
,
280 .verify_struct
= xfs_dir3_leafn_verify
,
285 struct xfs_trans
*tp
,
286 struct xfs_inode
*dp
,
288 xfs_daddr_t mappedbno
,
289 struct xfs_buf
**bpp
)
293 err
= xfs_da_read_buf(tp
, dp
, fbno
, mappedbno
, bpp
,
294 XFS_DATA_FORK
, &xfs_dir3_leaf1_buf_ops
);
295 if (!err
&& tp
&& *bpp
)
296 xfs_trans_buf_set_type(tp
, *bpp
, XFS_BLFT_DIR_LEAF1_BUF
);
302 struct xfs_trans
*tp
,
303 struct xfs_inode
*dp
,
305 xfs_daddr_t mappedbno
,
306 struct xfs_buf
**bpp
)
310 err
= xfs_da_read_buf(tp
, dp
, fbno
, mappedbno
, bpp
,
311 XFS_DATA_FORK
, &xfs_dir3_leafn_buf_ops
);
312 if (!err
&& tp
&& *bpp
)
313 xfs_trans_buf_set_type(tp
, *bpp
, XFS_BLFT_DIR_LEAFN_BUF
);
318 * Initialize a new leaf block, leaf1 or leafn magic accepted.
322 struct xfs_mount
*mp
,
323 struct xfs_trans
*tp
,
328 struct xfs_dir2_leaf
*leaf
= bp
->b_addr
;
330 ASSERT(type
== XFS_DIR2_LEAF1_MAGIC
|| type
== XFS_DIR2_LEAFN_MAGIC
);
332 if (xfs_sb_version_hascrc(&mp
->m_sb
)) {
333 struct xfs_dir3_leaf_hdr
*leaf3
= bp
->b_addr
;
335 memset(leaf3
, 0, sizeof(*leaf3
));
337 leaf3
->info
.hdr
.magic
= (type
== XFS_DIR2_LEAF1_MAGIC
)
338 ? cpu_to_be16(XFS_DIR3_LEAF1_MAGIC
)
339 : cpu_to_be16(XFS_DIR3_LEAFN_MAGIC
);
340 leaf3
->info
.blkno
= cpu_to_be64(bp
->b_bn
);
341 leaf3
->info
.owner
= cpu_to_be64(owner
);
342 uuid_copy(&leaf3
->info
.uuid
, &mp
->m_sb
.sb_meta_uuid
);
344 memset(leaf
, 0, sizeof(*leaf
));
345 leaf
->hdr
.info
.magic
= cpu_to_be16(type
);
349 * If it's a leaf-format directory initialize the tail.
350 * Caller is responsible for initialising the bests table.
352 if (type
== XFS_DIR2_LEAF1_MAGIC
) {
353 struct xfs_dir2_leaf_tail
*ltp
;
355 ltp
= xfs_dir2_leaf_tail_p(mp
->m_dir_geo
, leaf
);
357 bp
->b_ops
= &xfs_dir3_leaf1_buf_ops
;
358 xfs_trans_buf_set_type(tp
, bp
, XFS_BLFT_DIR_LEAF1_BUF
);
360 bp
->b_ops
= &xfs_dir3_leafn_buf_ops
;
361 xfs_trans_buf_set_type(tp
, bp
, XFS_BLFT_DIR_LEAFN_BUF
);
366 xfs_dir3_leaf_get_buf(
369 struct xfs_buf
**bpp
,
372 struct xfs_inode
*dp
= args
->dp
;
373 struct xfs_trans
*tp
= args
->trans
;
374 struct xfs_mount
*mp
= dp
->i_mount
;
378 ASSERT(magic
== XFS_DIR2_LEAF1_MAGIC
|| magic
== XFS_DIR2_LEAFN_MAGIC
);
379 ASSERT(bno
>= xfs_dir2_byte_to_db(args
->geo
, XFS_DIR2_LEAF_OFFSET
) &&
380 bno
< xfs_dir2_byte_to_db(args
->geo
, XFS_DIR2_FREE_OFFSET
));
382 error
= xfs_da_get_buf(tp
, dp
, xfs_dir2_db_to_da(args
->geo
, bno
),
383 -1, &bp
, XFS_DATA_FORK
);
387 xfs_dir3_leaf_init(mp
, tp
, bp
, dp
->i_ino
, magic
);
388 xfs_dir3_leaf_log_header(args
, bp
);
389 if (magic
== XFS_DIR2_LEAF1_MAGIC
)
390 xfs_dir3_leaf_log_tail(args
, bp
);
396 * Convert a block form directory to a leaf form directory.
399 xfs_dir2_block_to_leaf(
400 xfs_da_args_t
*args
, /* operation arguments */
401 struct xfs_buf
*dbp
) /* input block's buffer */
403 __be16
*bestsp
; /* leaf's bestsp entries */
404 xfs_dablk_t blkno
; /* leaf block's bno */
405 xfs_dir2_data_hdr_t
*hdr
; /* block header */
406 xfs_dir2_leaf_entry_t
*blp
; /* block's leaf entries */
407 xfs_dir2_block_tail_t
*btp
; /* block's tail */
408 xfs_inode_t
*dp
; /* incore directory inode */
409 int error
; /* error return code */
410 struct xfs_buf
*lbp
; /* leaf block's buffer */
411 xfs_dir2_db_t ldb
; /* leaf block's bno */
412 xfs_dir2_leaf_t
*leaf
; /* leaf structure */
413 xfs_dir2_leaf_tail_t
*ltp
; /* leaf's tail */
414 int needlog
; /* need to log block header */
415 int needscan
; /* need to rescan bestfree */
416 xfs_trans_t
*tp
; /* transaction pointer */
417 struct xfs_dir2_data_free
*bf
;
418 struct xfs_dir2_leaf_entry
*ents
;
419 struct xfs_dir3_icleaf_hdr leafhdr
;
421 trace_xfs_dir2_block_to_leaf(args
);
426 * Add the leaf block to the inode.
427 * This interface will only put blocks in the leaf/node range.
428 * Since that's empty now, we'll get the root (block 0 in range).
430 if ((error
= xfs_da_grow_inode(args
, &blkno
))) {
433 ldb
= xfs_dir2_da_to_db(args
->geo
, blkno
);
434 ASSERT(ldb
== xfs_dir2_byte_to_db(args
->geo
, XFS_DIR2_LEAF_OFFSET
));
436 * Initialize the leaf block, get a buffer for it.
438 error
= xfs_dir3_leaf_get_buf(args
, ldb
, &lbp
, XFS_DIR2_LEAF1_MAGIC
);
444 xfs_dir3_data_check(dp
, dbp
);
445 btp
= xfs_dir2_block_tail_p(args
->geo
, hdr
);
446 blp
= xfs_dir2_block_leaf_p(btp
);
447 bf
= dp
->d_ops
->data_bestfree_p(hdr
);
448 ents
= dp
->d_ops
->leaf_ents_p(leaf
);
451 * Set the counts in the leaf header.
453 dp
->d_ops
->leaf_hdr_from_disk(&leafhdr
, leaf
);
454 leafhdr
.count
= be32_to_cpu(btp
->count
);
455 leafhdr
.stale
= be32_to_cpu(btp
->stale
);
456 dp
->d_ops
->leaf_hdr_to_disk(leaf
, &leafhdr
);
457 xfs_dir3_leaf_log_header(args
, lbp
);
460 * Could compact these but I think we always do the conversion
461 * after squeezing out stale entries.
463 memcpy(ents
, blp
, be32_to_cpu(btp
->count
) * sizeof(xfs_dir2_leaf_entry_t
));
464 xfs_dir3_leaf_log_ents(args
, lbp
, 0, leafhdr
.count
- 1);
468 * Make the space formerly occupied by the leaf entries and block
471 xfs_dir2_data_make_free(args
, dbp
,
472 (xfs_dir2_data_aoff_t
)((char *)blp
- (char *)hdr
),
473 (xfs_dir2_data_aoff_t
)((char *)hdr
+ args
->geo
->blksize
-
475 &needlog
, &needscan
);
477 * Fix up the block header, make it a data block.
479 dbp
->b_ops
= &xfs_dir3_data_buf_ops
;
480 xfs_trans_buf_set_type(tp
, dbp
, XFS_BLFT_DIR_DATA_BUF
);
481 if (hdr
->magic
== cpu_to_be32(XFS_DIR2_BLOCK_MAGIC
))
482 hdr
->magic
= cpu_to_be32(XFS_DIR2_DATA_MAGIC
);
484 hdr
->magic
= cpu_to_be32(XFS_DIR3_DATA_MAGIC
);
487 xfs_dir2_data_freescan(dp
, hdr
, &needlog
);
489 * Set up leaf tail and bests table.
491 ltp
= xfs_dir2_leaf_tail_p(args
->geo
, leaf
);
492 ltp
->bestcount
= cpu_to_be32(1);
493 bestsp
= xfs_dir2_leaf_bests_p(ltp
);
494 bestsp
[0] = bf
[0].length
;
496 * Log the data header and leaf bests table.
499 xfs_dir2_data_log_header(args
, dbp
);
500 xfs_dir3_leaf_check(dp
, lbp
);
501 xfs_dir3_data_check(dp
, dbp
);
502 xfs_dir3_leaf_log_bests(args
, lbp
, 0, 0);
507 xfs_dir3_leaf_find_stale(
508 struct xfs_dir3_icleaf_hdr
*leafhdr
,
509 struct xfs_dir2_leaf_entry
*ents
,
515 * Find the first stale entry before our index, if any.
517 for (*lowstale
= index
- 1; *lowstale
>= 0; --*lowstale
) {
518 if (ents
[*lowstale
].address
==
519 cpu_to_be32(XFS_DIR2_NULL_DATAPTR
))
524 * Find the first stale entry at or after our index, if any.
525 * Stop if the result would require moving more entries than using
528 for (*highstale
= index
; *highstale
< leafhdr
->count
; ++*highstale
) {
529 if (ents
[*highstale
].address
==
530 cpu_to_be32(XFS_DIR2_NULL_DATAPTR
))
532 if (*lowstale
>= 0 && index
- *lowstale
<= *highstale
- index
)
537 struct xfs_dir2_leaf_entry
*
538 xfs_dir3_leaf_find_entry(
539 struct xfs_dir3_icleaf_hdr
*leafhdr
,
540 struct xfs_dir2_leaf_entry
*ents
,
541 int index
, /* leaf table position */
542 int compact
, /* need to compact leaves */
543 int lowstale
, /* index of prev stale leaf */
544 int highstale
, /* index of next stale leaf */
545 int *lfloglow
, /* low leaf logging index */
546 int *lfloghigh
) /* high leaf logging index */
548 if (!leafhdr
->stale
) {
549 xfs_dir2_leaf_entry_t
*lep
; /* leaf entry table pointer */
552 * Now we need to make room to insert the leaf entry.
554 * If there are no stale entries, just insert a hole at index.
557 if (index
< leafhdr
->count
)
558 memmove(lep
+ 1, lep
,
559 (leafhdr
->count
- index
) * sizeof(*lep
));
562 * Record low and high logging indices for the leaf.
565 *lfloghigh
= leafhdr
->count
++;
570 * There are stale entries.
572 * We will use one of them for the new entry. It's probably not at
573 * the right location, so we'll have to shift some up or down first.
575 * If we didn't compact before, we need to find the nearest stale
576 * entries before and after our insertion point.
579 xfs_dir3_leaf_find_stale(leafhdr
, ents
, index
,
580 &lowstale
, &highstale
);
583 * If the low one is better, use it.
586 (highstale
== leafhdr
->count
||
587 index
- lowstale
- 1 < highstale
- index
)) {
588 ASSERT(index
- lowstale
- 1 >= 0);
589 ASSERT(ents
[lowstale
].address
==
590 cpu_to_be32(XFS_DIR2_NULL_DATAPTR
));
593 * Copy entries up to cover the stale entry and make room
596 if (index
- lowstale
- 1 > 0) {
597 memmove(&ents
[lowstale
], &ents
[lowstale
+ 1],
598 (index
- lowstale
- 1) *
599 sizeof(xfs_dir2_leaf_entry_t
));
601 *lfloglow
= MIN(lowstale
, *lfloglow
);
602 *lfloghigh
= MAX(index
- 1, *lfloghigh
);
604 return &ents
[index
- 1];
608 * The high one is better, so use that one.
610 ASSERT(highstale
- index
>= 0);
611 ASSERT(ents
[highstale
].address
== cpu_to_be32(XFS_DIR2_NULL_DATAPTR
));
614 * Copy entries down to cover the stale entry and make room for the
617 if (highstale
- index
> 0) {
618 memmove(&ents
[index
+ 1], &ents
[index
],
619 (highstale
- index
) * sizeof(xfs_dir2_leaf_entry_t
));
621 *lfloglow
= MIN(index
, *lfloglow
);
622 *lfloghigh
= MAX(highstale
, *lfloghigh
);
628 * Add an entry to a leaf form directory.
631 xfs_dir2_leaf_addname(
632 xfs_da_args_t
*args
) /* operation arguments */
634 __be16
*bestsp
; /* freespace table in leaf */
635 int compact
; /* need to compact leaves */
636 xfs_dir2_data_hdr_t
*hdr
; /* data block header */
637 struct xfs_buf
*dbp
; /* data block buffer */
638 xfs_dir2_data_entry_t
*dep
; /* data block entry */
639 xfs_inode_t
*dp
; /* incore directory inode */
640 xfs_dir2_data_unused_t
*dup
; /* data unused entry */
641 int error
; /* error return value */
642 int grown
; /* allocated new data block */
643 int highstale
; /* index of next stale leaf */
644 int i
; /* temporary, index */
645 int index
; /* leaf table position */
646 struct xfs_buf
*lbp
; /* leaf's buffer */
647 xfs_dir2_leaf_t
*leaf
; /* leaf structure */
648 int length
; /* length of new entry */
649 xfs_dir2_leaf_entry_t
*lep
; /* leaf entry table pointer */
650 int lfloglow
; /* low leaf logging index */
651 int lfloghigh
; /* high leaf logging index */
652 int lowstale
; /* index of prev stale leaf */
653 xfs_dir2_leaf_tail_t
*ltp
; /* leaf tail pointer */
654 int needbytes
; /* leaf block bytes needed */
655 int needlog
; /* need to log data header */
656 int needscan
; /* need to rescan data free */
657 __be16
*tagp
; /* end of data entry */
658 xfs_trans_t
*tp
; /* transaction pointer */
659 xfs_dir2_db_t use_block
; /* data block number */
660 struct xfs_dir2_data_free
*bf
; /* bestfree table */
661 struct xfs_dir2_leaf_entry
*ents
;
662 struct xfs_dir3_icleaf_hdr leafhdr
;
664 trace_xfs_dir2_leaf_addname(args
);
669 error
= xfs_dir3_leaf_read(tp
, dp
, args
->geo
->leafblk
, -1, &lbp
);
674 * Look up the entry by hash value and name.
675 * We know it's not there, our caller has already done a lookup.
676 * So the index is of the entry to insert in front of.
677 * But if there are dup hash values the index is of the first of those.
679 index
= xfs_dir2_leaf_search_hash(args
, lbp
);
681 ltp
= xfs_dir2_leaf_tail_p(args
->geo
, leaf
);
682 ents
= dp
->d_ops
->leaf_ents_p(leaf
);
683 dp
->d_ops
->leaf_hdr_from_disk(&leafhdr
, leaf
);
684 bestsp
= xfs_dir2_leaf_bests_p(ltp
);
685 length
= dp
->d_ops
->data_entsize(args
->namelen
);
688 * See if there are any entries with the same hash value
689 * and space in their block for the new entry.
690 * This is good because it puts multiple same-hash value entries
691 * in a data block, improving the lookup of those entries.
693 for (use_block
= -1, lep
= &ents
[index
];
694 index
< leafhdr
.count
&& be32_to_cpu(lep
->hashval
) == args
->hashval
;
696 if (be32_to_cpu(lep
->address
) == XFS_DIR2_NULL_DATAPTR
)
698 i
= xfs_dir2_dataptr_to_db(args
->geo
, be32_to_cpu(lep
->address
));
699 ASSERT(i
< be32_to_cpu(ltp
->bestcount
));
700 ASSERT(bestsp
[i
] != cpu_to_be16(NULLDATAOFF
));
701 if (be16_to_cpu(bestsp
[i
]) >= length
) {
707 * Didn't find a block yet, linear search all the data blocks.
709 if (use_block
== -1) {
710 for (i
= 0; i
< be32_to_cpu(ltp
->bestcount
); i
++) {
712 * Remember a block we see that's missing.
714 if (bestsp
[i
] == cpu_to_be16(NULLDATAOFF
) &&
717 else if (be16_to_cpu(bestsp
[i
]) >= length
) {
724 * How many bytes do we need in the leaf block?
728 needbytes
+= sizeof(xfs_dir2_leaf_entry_t
);
730 needbytes
+= sizeof(xfs_dir2_data_off_t
);
733 * Now kill use_block if it refers to a missing block, so we
734 * can use it as an indication of allocation needed.
736 if (use_block
!= -1 && bestsp
[use_block
] == cpu_to_be16(NULLDATAOFF
))
739 * If we don't have enough free bytes but we can make enough
740 * by compacting out stale entries, we'll do that.
742 if ((char *)bestsp
- (char *)&ents
[leafhdr
.count
] < needbytes
&&
747 * Otherwise if we don't have enough free bytes we need to
748 * convert to node form.
750 else if ((char *)bestsp
- (char *)&ents
[leafhdr
.count
] < needbytes
) {
752 * Just checking or no space reservation, give up.
754 if ((args
->op_flags
& XFS_DA_OP_JUSTCHECK
) ||
756 xfs_trans_brelse(tp
, lbp
);
760 * Convert to node form.
762 error
= xfs_dir2_leaf_to_node(args
, lbp
);
766 * Then add the new entry.
768 return xfs_dir2_node_addname(args
);
771 * Otherwise it will fit without compaction.
776 * If just checking, then it will fit unless we needed to allocate
779 if (args
->op_flags
& XFS_DA_OP_JUSTCHECK
) {
780 xfs_trans_brelse(tp
, lbp
);
781 return use_block
== -1 ? -ENOSPC
: 0;
784 * If no allocations are allowed, return now before we've
787 if (args
->total
== 0 && use_block
== -1) {
788 xfs_trans_brelse(tp
, lbp
);
792 * Need to compact the leaf entries, removing stale ones.
793 * Leave one stale entry behind - the one closest to our
794 * insertion index - and we'll shift that one to our insertion
798 xfs_dir3_leaf_compact_x1(&leafhdr
, ents
, &index
, &lowstale
,
799 &highstale
, &lfloglow
, &lfloghigh
);
802 * There are stale entries, so we'll need log-low and log-high
803 * impossibly bad values later.
805 else if (leafhdr
.stale
) {
806 lfloglow
= leafhdr
.count
;
810 * If there was no data block space found, we need to allocate
813 if (use_block
== -1) {
815 * Add the new data block.
817 if ((error
= xfs_dir2_grow_inode(args
, XFS_DIR2_DATA_SPACE
,
819 xfs_trans_brelse(tp
, lbp
);
823 * Initialize the block.
825 if ((error
= xfs_dir3_data_init(args
, use_block
, &dbp
))) {
826 xfs_trans_brelse(tp
, lbp
);
830 * If we're adding a new data block on the end we need to
831 * extend the bests table. Copy it up one entry.
833 if (use_block
>= be32_to_cpu(ltp
->bestcount
)) {
835 memmove(&bestsp
[0], &bestsp
[1],
836 be32_to_cpu(ltp
->bestcount
) * sizeof(bestsp
[0]));
837 be32_add_cpu(<p
->bestcount
, 1);
838 xfs_dir3_leaf_log_tail(args
, lbp
);
839 xfs_dir3_leaf_log_bests(args
, lbp
, 0,
840 be32_to_cpu(ltp
->bestcount
) - 1);
843 * If we're filling in a previously empty block just log it.
846 xfs_dir3_leaf_log_bests(args
, lbp
, use_block
, use_block
);
848 bf
= dp
->d_ops
->data_bestfree_p(hdr
);
849 bestsp
[use_block
] = bf
[0].length
;
853 * Already had space in some data block.
854 * Just read that one in.
856 error
= xfs_dir3_data_read(tp
, dp
,
857 xfs_dir2_db_to_da(args
->geo
, use_block
),
860 xfs_trans_brelse(tp
, lbp
);
864 bf
= dp
->d_ops
->data_bestfree_p(hdr
);
868 * Point to the biggest freespace in our data block.
870 dup
= (xfs_dir2_data_unused_t
*)
871 ((char *)hdr
+ be16_to_cpu(bf
[0].offset
));
872 ASSERT(be16_to_cpu(dup
->length
) >= length
);
873 needscan
= needlog
= 0;
875 * Mark the initial part of our freespace in use for the new entry.
877 xfs_dir2_data_use_free(args
, dbp
, dup
,
878 (xfs_dir2_data_aoff_t
)((char *)dup
- (char *)hdr
), length
,
879 &needlog
, &needscan
);
881 * Initialize our new entry (at last).
883 dep
= (xfs_dir2_data_entry_t
*)dup
;
884 dep
->inumber
= cpu_to_be64(args
->inumber
);
885 dep
->namelen
= args
->namelen
;
886 memcpy(dep
->name
, args
->name
, dep
->namelen
);
887 dp
->d_ops
->data_put_ftype(dep
, args
->filetype
);
888 tagp
= dp
->d_ops
->data_entry_tag_p(dep
);
889 *tagp
= cpu_to_be16((char *)dep
- (char *)hdr
);
891 * Need to scan fix up the bestfree table.
894 xfs_dir2_data_freescan(dp
, hdr
, &needlog
);
896 * Need to log the data block's header.
899 xfs_dir2_data_log_header(args
, dbp
);
900 xfs_dir2_data_log_entry(args
, dbp
, dep
);
902 * If the bests table needs to be changed, do it.
903 * Log the change unless we've already done that.
905 if (be16_to_cpu(bestsp
[use_block
]) != be16_to_cpu(bf
[0].length
)) {
906 bestsp
[use_block
] = bf
[0].length
;
908 xfs_dir3_leaf_log_bests(args
, lbp
, use_block
, use_block
);
911 lep
= xfs_dir3_leaf_find_entry(&leafhdr
, ents
, index
, compact
, lowstale
,
912 highstale
, &lfloglow
, &lfloghigh
);
915 * Fill in the new leaf entry.
917 lep
->hashval
= cpu_to_be32(args
->hashval
);
918 lep
->address
= cpu_to_be32(
919 xfs_dir2_db_off_to_dataptr(args
->geo
, use_block
,
920 be16_to_cpu(*tagp
)));
922 * Log the leaf fields and give up the buffers.
924 dp
->d_ops
->leaf_hdr_to_disk(leaf
, &leafhdr
);
925 xfs_dir3_leaf_log_header(args
, lbp
);
926 xfs_dir3_leaf_log_ents(args
, lbp
, lfloglow
, lfloghigh
);
927 xfs_dir3_leaf_check(dp
, lbp
);
928 xfs_dir3_data_check(dp
, dbp
);
933 * Compact out any stale entries in the leaf.
934 * Log the header and changed leaf entries, if any.
937 xfs_dir3_leaf_compact(
938 xfs_da_args_t
*args
, /* operation arguments */
939 struct xfs_dir3_icleaf_hdr
*leafhdr
,
940 struct xfs_buf
*bp
) /* leaf buffer */
942 int from
; /* source leaf index */
943 xfs_dir2_leaf_t
*leaf
; /* leaf structure */
944 int loglow
; /* first leaf entry to log */
945 int to
; /* target leaf index */
946 struct xfs_dir2_leaf_entry
*ents
;
947 struct xfs_inode
*dp
= args
->dp
;
954 * Compress out the stale entries in place.
956 ents
= dp
->d_ops
->leaf_ents_p(leaf
);
957 for (from
= to
= 0, loglow
= -1; from
< leafhdr
->count
; from
++) {
958 if (ents
[from
].address
== cpu_to_be32(XFS_DIR2_NULL_DATAPTR
))
961 * Only actually copy the entries that are different.
966 ents
[to
] = ents
[from
];
971 * Update and log the header, log the leaf entries.
973 ASSERT(leafhdr
->stale
== from
- to
);
974 leafhdr
->count
-= leafhdr
->stale
;
977 dp
->d_ops
->leaf_hdr_to_disk(leaf
, leafhdr
);
978 xfs_dir3_leaf_log_header(args
, bp
);
980 xfs_dir3_leaf_log_ents(args
, bp
, loglow
, to
- 1);
984 * Compact the leaf entries, removing stale ones.
985 * Leave one stale entry behind - the one closest to our
986 * insertion index - and the caller will shift that one to our insertion
988 * Return new insertion index, where the remaining stale entry is,
989 * and leaf logging indices.
992 xfs_dir3_leaf_compact_x1(
993 struct xfs_dir3_icleaf_hdr
*leafhdr
,
994 struct xfs_dir2_leaf_entry
*ents
,
995 int *indexp
, /* insertion index */
996 int *lowstalep
, /* out: stale entry before us */
997 int *highstalep
, /* out: stale entry after us */
998 int *lowlogp
, /* out: low log index */
999 int *highlogp
) /* out: high log index */
1001 int from
; /* source copy index */
1002 int highstale
; /* stale entry at/after index */
1003 int index
; /* insertion index */
1004 int keepstale
; /* source index of kept stale */
1005 int lowstale
; /* stale entry before index */
1006 int newindex
=0; /* new insertion index */
1007 int to
; /* destination copy index */
1009 ASSERT(leafhdr
->stale
> 1);
1012 xfs_dir3_leaf_find_stale(leafhdr
, ents
, index
, &lowstale
, &highstale
);
1015 * Pick the better of lowstale and highstale.
1017 if (lowstale
>= 0 &&
1018 (highstale
== leafhdr
->count
||
1019 index
- lowstale
<= highstale
- index
))
1020 keepstale
= lowstale
;
1022 keepstale
= highstale
;
1024 * Copy the entries in place, removing all the stale entries
1027 for (from
= to
= 0; from
< leafhdr
->count
; from
++) {
1029 * Notice the new value of index.
1033 if (from
!= keepstale
&&
1034 ents
[from
].address
== cpu_to_be32(XFS_DIR2_NULL_DATAPTR
)) {
1040 * Record the new keepstale value for the insertion.
1042 if (from
== keepstale
)
1043 lowstale
= highstale
= to
;
1045 * Copy only the entries that have moved.
1048 ents
[to
] = ents
[from
];
1053 * If the insertion point was past the last entry,
1054 * set the new insertion point accordingly.
1060 * Adjust the leaf header values.
1062 leafhdr
->count
-= from
- to
;
1065 * Remember the low/high stale value only in the "right"
1068 if (lowstale
>= newindex
)
1071 highstale
= leafhdr
->count
;
1072 *highlogp
= leafhdr
->count
- 1;
1073 *lowstalep
= lowstale
;
1074 *highstalep
= highstale
;
1078 * Log the bests entries indicated from a leaf1 block.
1081 xfs_dir3_leaf_log_bests(
1082 struct xfs_da_args
*args
,
1083 struct xfs_buf
*bp
, /* leaf buffer */
1084 int first
, /* first entry to log */
1085 int last
) /* last entry to log */
1087 __be16
*firstb
; /* pointer to first entry */
1088 __be16
*lastb
; /* pointer to last entry */
1089 struct xfs_dir2_leaf
*leaf
= bp
->b_addr
;
1090 xfs_dir2_leaf_tail_t
*ltp
; /* leaf tail structure */
1092 ASSERT(leaf
->hdr
.info
.magic
== cpu_to_be16(XFS_DIR2_LEAF1_MAGIC
) ||
1093 leaf
->hdr
.info
.magic
== cpu_to_be16(XFS_DIR3_LEAF1_MAGIC
));
1095 ltp
= xfs_dir2_leaf_tail_p(args
->geo
, leaf
);
1096 firstb
= xfs_dir2_leaf_bests_p(ltp
) + first
;
1097 lastb
= xfs_dir2_leaf_bests_p(ltp
) + last
;
1098 xfs_trans_log_buf(args
->trans
, bp
,
1099 (uint
)((char *)firstb
- (char *)leaf
),
1100 (uint
)((char *)lastb
- (char *)leaf
+ sizeof(*lastb
) - 1));
1104 * Log the leaf entries indicated from a leaf1 or leafn block.
1107 xfs_dir3_leaf_log_ents(
1108 struct xfs_da_args
*args
,
1113 xfs_dir2_leaf_entry_t
*firstlep
; /* pointer to first entry */
1114 xfs_dir2_leaf_entry_t
*lastlep
; /* pointer to last entry */
1115 struct xfs_dir2_leaf
*leaf
= bp
->b_addr
;
1116 struct xfs_dir2_leaf_entry
*ents
;
1118 ASSERT(leaf
->hdr
.info
.magic
== cpu_to_be16(XFS_DIR2_LEAF1_MAGIC
) ||
1119 leaf
->hdr
.info
.magic
== cpu_to_be16(XFS_DIR3_LEAF1_MAGIC
) ||
1120 leaf
->hdr
.info
.magic
== cpu_to_be16(XFS_DIR2_LEAFN_MAGIC
) ||
1121 leaf
->hdr
.info
.magic
== cpu_to_be16(XFS_DIR3_LEAFN_MAGIC
));
1123 ents
= args
->dp
->d_ops
->leaf_ents_p(leaf
);
1124 firstlep
= &ents
[first
];
1125 lastlep
= &ents
[last
];
1126 xfs_trans_log_buf(args
->trans
, bp
,
1127 (uint
)((char *)firstlep
- (char *)leaf
),
1128 (uint
)((char *)lastlep
- (char *)leaf
+ sizeof(*lastlep
) - 1));
1132 * Log the header of the leaf1 or leafn block.
1135 xfs_dir3_leaf_log_header(
1136 struct xfs_da_args
*args
,
1139 struct xfs_dir2_leaf
*leaf
= bp
->b_addr
;
1141 ASSERT(leaf
->hdr
.info
.magic
== cpu_to_be16(XFS_DIR2_LEAF1_MAGIC
) ||
1142 leaf
->hdr
.info
.magic
== cpu_to_be16(XFS_DIR3_LEAF1_MAGIC
) ||
1143 leaf
->hdr
.info
.magic
== cpu_to_be16(XFS_DIR2_LEAFN_MAGIC
) ||
1144 leaf
->hdr
.info
.magic
== cpu_to_be16(XFS_DIR3_LEAFN_MAGIC
));
1146 xfs_trans_log_buf(args
->trans
, bp
,
1147 (uint
)((char *)&leaf
->hdr
- (char *)leaf
),
1148 args
->dp
->d_ops
->leaf_hdr_size
- 1);
1152 * Log the tail of the leaf1 block.
1155 xfs_dir3_leaf_log_tail(
1156 struct xfs_da_args
*args
,
1159 struct xfs_dir2_leaf
*leaf
= bp
->b_addr
;
1160 xfs_dir2_leaf_tail_t
*ltp
; /* leaf tail structure */
1162 ASSERT(leaf
->hdr
.info
.magic
== cpu_to_be16(XFS_DIR2_LEAF1_MAGIC
) ||
1163 leaf
->hdr
.info
.magic
== cpu_to_be16(XFS_DIR3_LEAF1_MAGIC
) ||
1164 leaf
->hdr
.info
.magic
== cpu_to_be16(XFS_DIR2_LEAFN_MAGIC
) ||
1165 leaf
->hdr
.info
.magic
== cpu_to_be16(XFS_DIR3_LEAFN_MAGIC
));
1167 ltp
= xfs_dir2_leaf_tail_p(args
->geo
, leaf
);
1168 xfs_trans_log_buf(args
->trans
, bp
, (uint
)((char *)ltp
- (char *)leaf
),
1169 (uint
)(args
->geo
->blksize
- 1));
1173 * Look up the entry referred to by args in the leaf format directory.
1174 * Most of the work is done by the xfs_dir2_leaf_lookup_int routine which
1175 * is also used by the node-format code.
1178 xfs_dir2_leaf_lookup(
1179 xfs_da_args_t
*args
) /* operation arguments */
1181 struct xfs_buf
*dbp
; /* data block buffer */
1182 xfs_dir2_data_entry_t
*dep
; /* data block entry */
1183 xfs_inode_t
*dp
; /* incore directory inode */
1184 int error
; /* error return code */
1185 int index
; /* found entry index */
1186 struct xfs_buf
*lbp
; /* leaf buffer */
1187 xfs_dir2_leaf_t
*leaf
; /* leaf structure */
1188 xfs_dir2_leaf_entry_t
*lep
; /* leaf entry */
1189 xfs_trans_t
*tp
; /* transaction pointer */
1190 struct xfs_dir2_leaf_entry
*ents
;
1192 trace_xfs_dir2_leaf_lookup(args
);
1195 * Look up name in the leaf block, returning both buffers and index.
1197 if ((error
= xfs_dir2_leaf_lookup_int(args
, &lbp
, &index
, &dbp
))) {
1202 xfs_dir3_leaf_check(dp
, lbp
);
1204 ents
= dp
->d_ops
->leaf_ents_p(leaf
);
1206 * Get to the leaf entry and contained data entry address.
1211 * Point to the data entry.
1213 dep
= (xfs_dir2_data_entry_t
*)
1214 ((char *)dbp
->b_addr
+
1215 xfs_dir2_dataptr_to_off(args
->geo
, be32_to_cpu(lep
->address
)));
1217 * Return the found inode number & CI name if appropriate
1219 args
->inumber
= be64_to_cpu(dep
->inumber
);
1220 args
->filetype
= dp
->d_ops
->data_get_ftype(dep
);
1221 error
= xfs_dir_cilookup_result(args
, dep
->name
, dep
->namelen
);
1222 xfs_trans_brelse(tp
, dbp
);
1223 xfs_trans_brelse(tp
, lbp
);
1228 * Look up name/hash in the leaf block.
1229 * Fill in indexp with the found index, and dbpp with the data buffer.
1230 * If not found dbpp will be NULL, and ENOENT comes back.
1231 * lbpp will always be filled in with the leaf buffer unless there's an error.
1233 static int /* error */
1234 xfs_dir2_leaf_lookup_int(
1235 xfs_da_args_t
*args
, /* operation arguments */
1236 struct xfs_buf
**lbpp
, /* out: leaf buffer */
1237 int *indexp
, /* out: index in leaf block */
1238 struct xfs_buf
**dbpp
) /* out: data buffer */
1240 xfs_dir2_db_t curdb
= -1; /* current data block number */
1241 struct xfs_buf
*dbp
= NULL
; /* data buffer */
1242 xfs_dir2_data_entry_t
*dep
; /* data entry */
1243 xfs_inode_t
*dp
; /* incore directory inode */
1244 int error
; /* error return code */
1245 int index
; /* index in leaf block */
1246 struct xfs_buf
*lbp
; /* leaf buffer */
1247 xfs_dir2_leaf_entry_t
*lep
; /* leaf entry */
1248 xfs_dir2_leaf_t
*leaf
; /* leaf structure */
1249 xfs_mount_t
*mp
; /* filesystem mount point */
1250 xfs_dir2_db_t newdb
; /* new data block number */
1251 xfs_trans_t
*tp
; /* transaction pointer */
1252 xfs_dir2_db_t cidb
= -1; /* case match data block no. */
1253 enum xfs_dacmp cmp
; /* name compare result */
1254 struct xfs_dir2_leaf_entry
*ents
;
1255 struct xfs_dir3_icleaf_hdr leafhdr
;
1261 error
= xfs_dir3_leaf_read(tp
, dp
, args
->geo
->leafblk
, -1, &lbp
);
1267 xfs_dir3_leaf_check(dp
, lbp
);
1268 ents
= dp
->d_ops
->leaf_ents_p(leaf
);
1269 dp
->d_ops
->leaf_hdr_from_disk(&leafhdr
, leaf
);
1272 * Look for the first leaf entry with our hash value.
1274 index
= xfs_dir2_leaf_search_hash(args
, lbp
);
1276 * Loop over all the entries with the right hash value
1277 * looking to match the name.
1279 for (lep
= &ents
[index
];
1280 index
< leafhdr
.count
&& be32_to_cpu(lep
->hashval
) == args
->hashval
;
1283 * Skip over stale leaf entries.
1285 if (be32_to_cpu(lep
->address
) == XFS_DIR2_NULL_DATAPTR
)
1288 * Get the new data block number.
1290 newdb
= xfs_dir2_dataptr_to_db(args
->geo
,
1291 be32_to_cpu(lep
->address
));
1293 * If it's not the same as the old data block number,
1294 * need to pitch the old one and read the new one.
1296 if (newdb
!= curdb
) {
1298 xfs_trans_brelse(tp
, dbp
);
1299 error
= xfs_dir3_data_read(tp
, dp
,
1300 xfs_dir2_db_to_da(args
->geo
, newdb
),
1303 xfs_trans_brelse(tp
, lbp
);
1309 * Point to the data entry.
1311 dep
= (xfs_dir2_data_entry_t
*)((char *)dbp
->b_addr
+
1312 xfs_dir2_dataptr_to_off(args
->geo
,
1313 be32_to_cpu(lep
->address
)));
1315 * Compare name and if it's an exact match, return the index
1316 * and buffer. If it's the first case-insensitive match, store
1317 * the index and buffer and continue looking for an exact match.
1319 cmp
= mp
->m_dirnameops
->compname(args
, dep
->name
, dep
->namelen
);
1320 if (cmp
!= XFS_CMP_DIFFERENT
&& cmp
!= args
->cmpresult
) {
1321 args
->cmpresult
= cmp
;
1323 /* case exact match: return the current buffer. */
1324 if (cmp
== XFS_CMP_EXACT
) {
1331 ASSERT(args
->op_flags
& XFS_DA_OP_OKNOENT
);
1333 * Here, we can only be doing a lookup (not a rename or remove).
1334 * If a case-insensitive match was found earlier, re-read the
1335 * appropriate data block if required and return it.
1337 if (args
->cmpresult
== XFS_CMP_CASE
) {
1339 if (cidb
!= curdb
) {
1340 xfs_trans_brelse(tp
, dbp
);
1341 error
= xfs_dir3_data_read(tp
, dp
,
1342 xfs_dir2_db_to_da(args
->geo
, cidb
),
1345 xfs_trans_brelse(tp
, lbp
);
1353 * No match found, return -ENOENT.
1357 xfs_trans_brelse(tp
, dbp
);
1358 xfs_trans_brelse(tp
, lbp
);
1363 * Remove an entry from a leaf format directory.
1366 xfs_dir2_leaf_removename(
1367 xfs_da_args_t
*args
) /* operation arguments */
1369 __be16
*bestsp
; /* leaf block best freespace */
1370 xfs_dir2_data_hdr_t
*hdr
; /* data block header */
1371 xfs_dir2_db_t db
; /* data block number */
1372 struct xfs_buf
*dbp
; /* data block buffer */
1373 xfs_dir2_data_entry_t
*dep
; /* data entry structure */
1374 xfs_inode_t
*dp
; /* incore directory inode */
1375 int error
; /* error return code */
1376 xfs_dir2_db_t i
; /* temporary data block # */
1377 int index
; /* index into leaf entries */
1378 struct xfs_buf
*lbp
; /* leaf buffer */
1379 xfs_dir2_leaf_t
*leaf
; /* leaf structure */
1380 xfs_dir2_leaf_entry_t
*lep
; /* leaf entry */
1381 xfs_dir2_leaf_tail_t
*ltp
; /* leaf tail structure */
1382 int needlog
; /* need to log data header */
1383 int needscan
; /* need to rescan data frees */
1384 xfs_dir2_data_off_t oldbest
; /* old value of best free */
1385 struct xfs_dir2_data_free
*bf
; /* bestfree table */
1386 struct xfs_dir2_leaf_entry
*ents
;
1387 struct xfs_dir3_icleaf_hdr leafhdr
;
1389 trace_xfs_dir2_leaf_removename(args
);
1392 * Lookup the leaf entry, get the leaf and data blocks read in.
1394 if ((error
= xfs_dir2_leaf_lookup_int(args
, &lbp
, &index
, &dbp
))) {
1400 xfs_dir3_data_check(dp
, dbp
);
1401 bf
= dp
->d_ops
->data_bestfree_p(hdr
);
1402 dp
->d_ops
->leaf_hdr_from_disk(&leafhdr
, leaf
);
1403 ents
= dp
->d_ops
->leaf_ents_p(leaf
);
1405 * Point to the leaf entry, use that to point to the data entry.
1408 db
= xfs_dir2_dataptr_to_db(args
->geo
, be32_to_cpu(lep
->address
));
1409 dep
= (xfs_dir2_data_entry_t
*)((char *)hdr
+
1410 xfs_dir2_dataptr_to_off(args
->geo
, be32_to_cpu(lep
->address
)));
1411 needscan
= needlog
= 0;
1412 oldbest
= be16_to_cpu(bf
[0].length
);
1413 ltp
= xfs_dir2_leaf_tail_p(args
->geo
, leaf
);
1414 bestsp
= xfs_dir2_leaf_bests_p(ltp
);
1415 ASSERT(be16_to_cpu(bestsp
[db
]) == oldbest
);
1417 * Mark the former data entry unused.
1419 xfs_dir2_data_make_free(args
, dbp
,
1420 (xfs_dir2_data_aoff_t
)((char *)dep
- (char *)hdr
),
1421 dp
->d_ops
->data_entsize(dep
->namelen
), &needlog
, &needscan
);
1423 * We just mark the leaf entry stale by putting a null in it.
1426 dp
->d_ops
->leaf_hdr_to_disk(leaf
, &leafhdr
);
1427 xfs_dir3_leaf_log_header(args
, lbp
);
1429 lep
->address
= cpu_to_be32(XFS_DIR2_NULL_DATAPTR
);
1430 xfs_dir3_leaf_log_ents(args
, lbp
, index
, index
);
1433 * Scan the freespace in the data block again if necessary,
1434 * log the data block header if necessary.
1437 xfs_dir2_data_freescan(dp
, hdr
, &needlog
);
1439 xfs_dir2_data_log_header(args
, dbp
);
1441 * If the longest freespace in the data block has changed,
1442 * put the new value in the bests table and log that.
1444 if (be16_to_cpu(bf
[0].length
) != oldbest
) {
1445 bestsp
[db
] = bf
[0].length
;
1446 xfs_dir3_leaf_log_bests(args
, lbp
, db
, db
);
1448 xfs_dir3_data_check(dp
, dbp
);
1450 * If the data block is now empty then get rid of the data block.
1452 if (be16_to_cpu(bf
[0].length
) ==
1453 args
->geo
->blksize
- dp
->d_ops
->data_entry_offset
) {
1454 ASSERT(db
!= args
->geo
->datablk
);
1455 if ((error
= xfs_dir2_shrink_inode(args
, db
, dbp
))) {
1457 * Nope, can't get rid of it because it caused
1458 * allocation of a bmap btree block to do so.
1459 * Just go on, returning success, leaving the
1460 * empty block in place.
1462 if (error
== -ENOSPC
&& args
->total
== 0)
1464 xfs_dir3_leaf_check(dp
, lbp
);
1469 * If this is the last data block then compact the
1470 * bests table by getting rid of entries.
1472 if (db
== be32_to_cpu(ltp
->bestcount
) - 1) {
1474 * Look for the last active entry (i).
1476 for (i
= db
- 1; i
> 0; i
--) {
1477 if (bestsp
[i
] != cpu_to_be16(NULLDATAOFF
))
1481 * Copy the table down so inactive entries at the
1484 memmove(&bestsp
[db
- i
], bestsp
,
1485 (be32_to_cpu(ltp
->bestcount
) - (db
- i
)) * sizeof(*bestsp
));
1486 be32_add_cpu(<p
->bestcount
, -(db
- i
));
1487 xfs_dir3_leaf_log_tail(args
, lbp
);
1488 xfs_dir3_leaf_log_bests(args
, lbp
, 0,
1489 be32_to_cpu(ltp
->bestcount
) - 1);
1491 bestsp
[db
] = cpu_to_be16(NULLDATAOFF
);
1494 * If the data block was not the first one, drop it.
1496 else if (db
!= args
->geo
->datablk
)
1499 xfs_dir3_leaf_check(dp
, lbp
);
1501 * See if we can convert to block form.
1503 return xfs_dir2_leaf_to_block(args
, lbp
, dbp
);
1507 * Replace the inode number in a leaf format directory entry.
1510 xfs_dir2_leaf_replace(
1511 xfs_da_args_t
*args
) /* operation arguments */
1513 struct xfs_buf
*dbp
; /* data block buffer */
1514 xfs_dir2_data_entry_t
*dep
; /* data block entry */
1515 xfs_inode_t
*dp
; /* incore directory inode */
1516 int error
; /* error return code */
1517 int index
; /* index of leaf entry */
1518 struct xfs_buf
*lbp
; /* leaf buffer */
1519 xfs_dir2_leaf_t
*leaf
; /* leaf structure */
1520 xfs_dir2_leaf_entry_t
*lep
; /* leaf entry */
1521 xfs_trans_t
*tp
; /* transaction pointer */
1522 struct xfs_dir2_leaf_entry
*ents
;
1524 trace_xfs_dir2_leaf_replace(args
);
1527 * Look up the entry.
1529 if ((error
= xfs_dir2_leaf_lookup_int(args
, &lbp
, &index
, &dbp
))) {
1534 ents
= dp
->d_ops
->leaf_ents_p(leaf
);
1536 * Point to the leaf entry, get data address from it.
1540 * Point to the data entry.
1542 dep
= (xfs_dir2_data_entry_t
*)
1543 ((char *)dbp
->b_addr
+
1544 xfs_dir2_dataptr_to_off(args
->geo
, be32_to_cpu(lep
->address
)));
1545 ASSERT(args
->inumber
!= be64_to_cpu(dep
->inumber
));
1547 * Put the new inode number in, log it.
1549 dep
->inumber
= cpu_to_be64(args
->inumber
);
1550 dp
->d_ops
->data_put_ftype(dep
, args
->filetype
);
1552 xfs_dir2_data_log_entry(args
, dbp
, dep
);
1553 xfs_dir3_leaf_check(dp
, lbp
);
1554 xfs_trans_brelse(tp
, lbp
);
1559 * Return index in the leaf block (lbp) which is either the first
1560 * one with this hash value, or if there are none, the insert point
1561 * for that hash value.
1563 int /* index value */
1564 xfs_dir2_leaf_search_hash(
1565 xfs_da_args_t
*args
, /* operation arguments */
1566 struct xfs_buf
*lbp
) /* leaf buffer */
1568 xfs_dahash_t hash
=0; /* hash from this entry */
1569 xfs_dahash_t hashwant
; /* hash value looking for */
1570 int high
; /* high leaf index */
1571 int low
; /* low leaf index */
1572 xfs_dir2_leaf_t
*leaf
; /* leaf structure */
1573 xfs_dir2_leaf_entry_t
*lep
; /* leaf entry */
1574 int mid
=0; /* current leaf index */
1575 struct xfs_dir2_leaf_entry
*ents
;
1576 struct xfs_dir3_icleaf_hdr leafhdr
;
1579 ents
= args
->dp
->d_ops
->leaf_ents_p(leaf
);
1580 args
->dp
->d_ops
->leaf_hdr_from_disk(&leafhdr
, leaf
);
1583 * Note, the table cannot be empty, so we have to go through the loop.
1584 * Binary search the leaf entries looking for our hash value.
1586 for (lep
= ents
, low
= 0, high
= leafhdr
.count
- 1,
1587 hashwant
= args
->hashval
;
1589 mid
= (low
+ high
) >> 1;
1590 if ((hash
= be32_to_cpu(lep
[mid
].hashval
)) == hashwant
)
1592 if (hash
< hashwant
)
1598 * Found one, back up through all the equal hash values.
1600 if (hash
== hashwant
) {
1601 while (mid
> 0 && be32_to_cpu(lep
[mid
- 1].hashval
) == hashwant
) {
1606 * Need to point to an entry higher than ours.
1608 else if (hash
< hashwant
)
1614 * Trim off a trailing data block. We know it's empty since the leaf
1615 * freespace table says so.
1618 xfs_dir2_leaf_trim_data(
1619 xfs_da_args_t
*args
, /* operation arguments */
1620 struct xfs_buf
*lbp
, /* leaf buffer */
1621 xfs_dir2_db_t db
) /* data block number */
1623 __be16
*bestsp
; /* leaf bests table */
1624 struct xfs_buf
*dbp
; /* data block buffer */
1625 xfs_inode_t
*dp
; /* incore directory inode */
1626 int error
; /* error return value */
1627 xfs_dir2_leaf_t
*leaf
; /* leaf structure */
1628 xfs_dir2_leaf_tail_t
*ltp
; /* leaf tail structure */
1629 xfs_trans_t
*tp
; /* transaction pointer */
1634 * Read the offending data block. We need its buffer.
1636 error
= xfs_dir3_data_read(tp
, dp
, xfs_dir2_db_to_da(args
->geo
, db
),
1642 ltp
= xfs_dir2_leaf_tail_p(args
->geo
, leaf
);
1646 struct xfs_dir2_data_hdr
*hdr
= dbp
->b_addr
;
1647 struct xfs_dir2_data_free
*bf
= dp
->d_ops
->data_bestfree_p(hdr
);
1649 ASSERT(hdr
->magic
== cpu_to_be32(XFS_DIR2_DATA_MAGIC
) ||
1650 hdr
->magic
== cpu_to_be32(XFS_DIR3_DATA_MAGIC
));
1651 ASSERT(be16_to_cpu(bf
[0].length
) ==
1652 args
->geo
->blksize
- dp
->d_ops
->data_entry_offset
);
1653 ASSERT(db
== be32_to_cpu(ltp
->bestcount
) - 1);
1658 * Get rid of the data block.
1660 if ((error
= xfs_dir2_shrink_inode(args
, db
, dbp
))) {
1661 ASSERT(error
!= -ENOSPC
);
1662 xfs_trans_brelse(tp
, dbp
);
1666 * Eliminate the last bests entry from the table.
1668 bestsp
= xfs_dir2_leaf_bests_p(ltp
);
1669 be32_add_cpu(<p
->bestcount
, -1);
1670 memmove(&bestsp
[1], &bestsp
[0], be32_to_cpu(ltp
->bestcount
) * sizeof(*bestsp
));
1671 xfs_dir3_leaf_log_tail(args
, lbp
);
1672 xfs_dir3_leaf_log_bests(args
, lbp
, 0, be32_to_cpu(ltp
->bestcount
) - 1);
1676 static inline size_t
1678 struct xfs_dir3_icleaf_hdr
*hdr
,
1684 entries
= hdr
->count
- hdr
->stale
;
1685 if (hdr
->magic
== XFS_DIR2_LEAF1_MAGIC
||
1686 hdr
->magic
== XFS_DIR2_LEAFN_MAGIC
)
1687 hdrsize
= sizeof(struct xfs_dir2_leaf_hdr
);
1689 hdrsize
= sizeof(struct xfs_dir3_leaf_hdr
);
1691 return hdrsize
+ entries
* sizeof(xfs_dir2_leaf_entry_t
)
1692 + counts
* sizeof(xfs_dir2_data_off_t
)
1693 + sizeof(xfs_dir2_leaf_tail_t
);
1697 * Convert node form directory to leaf form directory.
1698 * The root of the node form dir needs to already be a LEAFN block.
1699 * Just return if we can't do anything.
1702 xfs_dir2_node_to_leaf(
1703 xfs_da_state_t
*state
) /* directory operation state */
1705 xfs_da_args_t
*args
; /* operation arguments */
1706 xfs_inode_t
*dp
; /* incore directory inode */
1707 int error
; /* error return code */
1708 struct xfs_buf
*fbp
; /* buffer for freespace block */
1709 xfs_fileoff_t fo
; /* freespace file offset */
1710 xfs_dir2_free_t
*free
; /* freespace structure */
1711 struct xfs_buf
*lbp
; /* buffer for leaf block */
1712 xfs_dir2_leaf_tail_t
*ltp
; /* tail of leaf structure */
1713 xfs_dir2_leaf_t
*leaf
; /* leaf structure */
1714 xfs_mount_t
*mp
; /* filesystem mount point */
1715 int rval
; /* successful free trim? */
1716 xfs_trans_t
*tp
; /* transaction pointer */
1717 struct xfs_dir3_icleaf_hdr leafhdr
;
1718 struct xfs_dir3_icfree_hdr freehdr
;
1721 * There's more than a leaf level in the btree, so there must
1722 * be multiple leafn blocks. Give up.
1724 if (state
->path
.active
> 1)
1728 trace_xfs_dir2_node_to_leaf(args
);
1734 * Get the last offset in the file.
1736 if ((error
= xfs_bmap_last_offset(dp
, &fo
, XFS_DATA_FORK
))) {
1739 fo
-= args
->geo
->fsbcount
;
1741 * If there are freespace blocks other than the first one,
1742 * take this opportunity to remove trailing empty freespace blocks
1743 * that may have been left behind during no-space-reservation
1746 while (fo
> args
->geo
->freeblk
) {
1747 if ((error
= xfs_dir2_node_trim_free(args
, fo
, &rval
))) {
1751 fo
-= args
->geo
->fsbcount
;
1756 * Now find the block just before the freespace block.
1758 if ((error
= xfs_bmap_last_before(tp
, dp
, &fo
, XFS_DATA_FORK
))) {
1762 * If it's not the single leaf block, give up.
1764 if (XFS_FSB_TO_B(mp
, fo
) > XFS_DIR2_LEAF_OFFSET
+ args
->geo
->blksize
)
1766 lbp
= state
->path
.blk
[0].bp
;
1768 dp
->d_ops
->leaf_hdr_from_disk(&leafhdr
, leaf
);
1770 ASSERT(leafhdr
.magic
== XFS_DIR2_LEAFN_MAGIC
||
1771 leafhdr
.magic
== XFS_DIR3_LEAFN_MAGIC
);
1774 * Read the freespace block.
1776 error
= xfs_dir2_free_read(tp
, dp
, args
->geo
->freeblk
, &fbp
);
1780 dp
->d_ops
->free_hdr_from_disk(&freehdr
, free
);
1782 ASSERT(!freehdr
.firstdb
);
1785 * Now see if the leafn and free data will fit in a leaf1.
1786 * If not, release the buffer and give up.
1788 if (xfs_dir3_leaf_size(&leafhdr
, freehdr
.nvalid
) > args
->geo
->blksize
) {
1789 xfs_trans_brelse(tp
, fbp
);
1794 * If the leaf has any stale entries in it, compress them out.
1797 xfs_dir3_leaf_compact(args
, &leafhdr
, lbp
);
1799 lbp
->b_ops
= &xfs_dir3_leaf1_buf_ops
;
1800 xfs_trans_buf_set_type(tp
, lbp
, XFS_BLFT_DIR_LEAF1_BUF
);
1801 leafhdr
.magic
= (leafhdr
.magic
== XFS_DIR2_LEAFN_MAGIC
)
1802 ? XFS_DIR2_LEAF1_MAGIC
1803 : XFS_DIR3_LEAF1_MAGIC
;
1806 * Set up the leaf tail from the freespace block.
1808 ltp
= xfs_dir2_leaf_tail_p(args
->geo
, leaf
);
1809 ltp
->bestcount
= cpu_to_be32(freehdr
.nvalid
);
1812 * Set up the leaf bests table.
1814 memcpy(xfs_dir2_leaf_bests_p(ltp
), dp
->d_ops
->free_bests_p(free
),
1815 freehdr
.nvalid
* sizeof(xfs_dir2_data_off_t
));
1817 dp
->d_ops
->leaf_hdr_to_disk(leaf
, &leafhdr
);
1818 xfs_dir3_leaf_log_header(args
, lbp
);
1819 xfs_dir3_leaf_log_bests(args
, lbp
, 0, be32_to_cpu(ltp
->bestcount
) - 1);
1820 xfs_dir3_leaf_log_tail(args
, lbp
);
1821 xfs_dir3_leaf_check(dp
, lbp
);
1824 * Get rid of the freespace block.
1826 error
= xfs_dir2_shrink_inode(args
,
1827 xfs_dir2_byte_to_db(args
->geo
, XFS_DIR2_FREE_OFFSET
),
1831 * This can't fail here because it can only happen when
1832 * punching out the middle of an extent, and this is an
1835 ASSERT(error
!= -ENOSPC
);
1840 * Now see if we can convert the single-leaf directory
1841 * down to a block form directory.
1842 * This routine always kills the dabuf for the leaf, so
1843 * eliminate it from the path.
1845 error
= xfs_dir2_leaf_to_block(args
, lbp
, NULL
);
1846 state
->path
.blk
[0].bp
= NULL
;