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
21 #include "xfs_format.h"
22 #include "xfs_log_format.h"
23 #include "xfs_trans_resv.h"
26 #include "xfs_mount.h"
27 #include "xfs_da_format.h"
28 #include "xfs_da_btree.h"
29 #include "xfs_inode.h"
32 #include "xfs_dir2_priv.h"
33 #include "xfs_error.h"
34 #include "xfs_trace.h"
35 #include "xfs_trans.h"
36 #include "xfs_buf_item.h"
37 #include "xfs_cksum.h"
40 * Local function declarations.
42 static int xfs_dir2_leaf_lookup_int(xfs_da_args_t
*args
, struct xfs_buf
**lbpp
,
43 int *indexp
, struct xfs_buf
**dbpp
);
44 static void xfs_dir3_leaf_log_bests(struct xfs_da_args
*args
,
45 struct xfs_buf
*bp
, int first
, int last
);
46 static void xfs_dir3_leaf_log_tail(struct xfs_da_args
*args
,
50 * Check the internal consistency of a leaf1 block.
51 * Pop an assert if something is wrong.
54 #define xfs_dir3_leaf_check(dp, bp) \
56 if (!xfs_dir3_leaf1_check((dp), (bp))) \
65 struct xfs_dir2_leaf
*leaf
= bp
->b_addr
;
66 struct xfs_dir3_icleaf_hdr leafhdr
;
68 dp
->d_ops
->leaf_hdr_from_disk(&leafhdr
, leaf
);
70 if (leafhdr
.magic
== XFS_DIR3_LEAF1_MAGIC
) {
71 struct xfs_dir3_leaf_hdr
*leaf3
= bp
->b_addr
;
72 if (be64_to_cpu(leaf3
->info
.blkno
) != bp
->b_bn
)
74 } else if (leafhdr
.magic
!= XFS_DIR2_LEAF1_MAGIC
)
77 return xfs_dir3_leaf_check_int(dp
->i_mount
, dp
, &leafhdr
, leaf
);
80 #define xfs_dir3_leaf_check(dp, bp)
84 xfs_dir3_leaf_check_int(
87 struct xfs_dir3_icleaf_hdr
*hdr
,
88 struct xfs_dir2_leaf
*leaf
)
90 struct xfs_dir2_leaf_entry
*ents
;
91 xfs_dir2_leaf_tail_t
*ltp
;
94 const struct xfs_dir_ops
*ops
;
95 struct xfs_dir3_icleaf_hdr leafhdr
;
96 struct xfs_da_geometry
*geo
= mp
->m_dir_geo
;
99 * we can be passed a null dp here from a verifier, so we need to go the
100 * hard way to get them.
102 ops
= xfs_dir_get_ops(mp
, dp
);
105 ops
->leaf_hdr_from_disk(&leafhdr
, leaf
);
109 ents
= ops
->leaf_ents_p(leaf
);
110 ltp
= xfs_dir2_leaf_tail_p(geo
, leaf
);
113 * XXX (dgc): This value is not restrictive enough.
114 * Should factor in the size of the bests table as well.
115 * We can deduce a value for that from di_size.
117 if (hdr
->count
> ops
->leaf_max_ents(geo
))
120 /* Leaves and bests don't overlap in leaf format. */
121 if ((hdr
->magic
== XFS_DIR2_LEAF1_MAGIC
||
122 hdr
->magic
== XFS_DIR3_LEAF1_MAGIC
) &&
123 (char *)&ents
[hdr
->count
] > (char *)xfs_dir2_leaf_bests_p(ltp
))
126 /* Check hash value order, count stale entries. */
127 for (i
= stale
= 0; i
< hdr
->count
; i
++) {
128 if (i
+ 1 < hdr
->count
) {
129 if (be32_to_cpu(ents
[i
].hashval
) >
130 be32_to_cpu(ents
[i
+ 1].hashval
))
133 if (ents
[i
].address
== cpu_to_be32(XFS_DIR2_NULL_DATAPTR
))
136 if (hdr
->stale
!= stale
)
142 * We verify the magic numbers before decoding the leaf header so that on debug
143 * kernels we don't get assertion failures in xfs_dir3_leaf_hdr_from_disk() due
144 * to incorrect magic numbers.
147 xfs_dir3_leaf_verify(
151 struct xfs_mount
*mp
= bp
->b_target
->bt_mount
;
152 struct xfs_dir2_leaf
*leaf
= bp
->b_addr
;
154 ASSERT(magic
== XFS_DIR2_LEAF1_MAGIC
|| magic
== XFS_DIR2_LEAFN_MAGIC
);
156 if (xfs_sb_version_hascrc(&mp
->m_sb
)) {
157 struct xfs_dir3_leaf_hdr
*leaf3
= bp
->b_addr
;
160 magic3
= (magic
== XFS_DIR2_LEAF1_MAGIC
) ? XFS_DIR3_LEAF1_MAGIC
161 : XFS_DIR3_LEAFN_MAGIC
;
163 if (leaf3
->info
.hdr
.magic
!= cpu_to_be16(magic3
))
165 if (!uuid_equal(&leaf3
->info
.uuid
, &mp
->m_sb
.sb_uuid
))
167 if (be64_to_cpu(leaf3
->info
.blkno
) != bp
->b_bn
)
170 if (leaf
->hdr
.info
.magic
!= cpu_to_be16(magic
))
174 return xfs_dir3_leaf_check_int(mp
, NULL
, NULL
, leaf
);
182 struct xfs_mount
*mp
= bp
->b_target
->bt_mount
;
184 if (xfs_sb_version_hascrc(&mp
->m_sb
) &&
185 !xfs_buf_verify_cksum(bp
, XFS_DIR3_LEAF_CRC_OFF
))
186 xfs_buf_ioerror(bp
, EFSBADCRC
);
187 else if (!xfs_dir3_leaf_verify(bp
, magic
))
188 xfs_buf_ioerror(bp
, EFSCORRUPTED
);
191 xfs_verifier_error(bp
);
199 struct xfs_mount
*mp
= bp
->b_target
->bt_mount
;
200 struct xfs_buf_log_item
*bip
= bp
->b_fspriv
;
201 struct xfs_dir3_leaf_hdr
*hdr3
= bp
->b_addr
;
203 if (!xfs_dir3_leaf_verify(bp
, magic
)) {
204 xfs_buf_ioerror(bp
, EFSCORRUPTED
);
205 xfs_verifier_error(bp
);
209 if (!xfs_sb_version_hascrc(&mp
->m_sb
))
213 hdr3
->info
.lsn
= cpu_to_be64(bip
->bli_item
.li_lsn
);
215 xfs_buf_update_cksum(bp
, XFS_DIR3_LEAF_CRC_OFF
);
219 xfs_dir3_leaf1_read_verify(
222 __read_verify(bp
, XFS_DIR2_LEAF1_MAGIC
);
226 xfs_dir3_leaf1_write_verify(
229 __write_verify(bp
, XFS_DIR2_LEAF1_MAGIC
);
233 xfs_dir3_leafn_read_verify(
236 __read_verify(bp
, XFS_DIR2_LEAFN_MAGIC
);
240 xfs_dir3_leafn_write_verify(
243 __write_verify(bp
, XFS_DIR2_LEAFN_MAGIC
);
246 const struct xfs_buf_ops xfs_dir3_leaf1_buf_ops
= {
247 .verify_read
= xfs_dir3_leaf1_read_verify
,
248 .verify_write
= xfs_dir3_leaf1_write_verify
,
251 const struct xfs_buf_ops xfs_dir3_leafn_buf_ops
= {
252 .verify_read
= xfs_dir3_leafn_read_verify
,
253 .verify_write
= xfs_dir3_leafn_write_verify
,
258 struct xfs_trans
*tp
,
259 struct xfs_inode
*dp
,
261 xfs_daddr_t mappedbno
,
262 struct xfs_buf
**bpp
)
266 err
= xfs_da_read_buf(tp
, dp
, fbno
, mappedbno
, bpp
,
267 XFS_DATA_FORK
, &xfs_dir3_leaf1_buf_ops
);
269 xfs_trans_buf_set_type(tp
, *bpp
, XFS_BLFT_DIR_LEAF1_BUF
);
275 struct xfs_trans
*tp
,
276 struct xfs_inode
*dp
,
278 xfs_daddr_t mappedbno
,
279 struct xfs_buf
**bpp
)
283 err
= xfs_da_read_buf(tp
, dp
, fbno
, mappedbno
, bpp
,
284 XFS_DATA_FORK
, &xfs_dir3_leafn_buf_ops
);
286 xfs_trans_buf_set_type(tp
, *bpp
, XFS_BLFT_DIR_LEAFN_BUF
);
291 * Initialize a new leaf block, leaf1 or leafn magic accepted.
295 struct xfs_mount
*mp
,
296 struct xfs_trans
*tp
,
301 struct xfs_dir2_leaf
*leaf
= bp
->b_addr
;
303 ASSERT(type
== XFS_DIR2_LEAF1_MAGIC
|| type
== XFS_DIR2_LEAFN_MAGIC
);
305 if (xfs_sb_version_hascrc(&mp
->m_sb
)) {
306 struct xfs_dir3_leaf_hdr
*leaf3
= bp
->b_addr
;
308 memset(leaf3
, 0, sizeof(*leaf3
));
310 leaf3
->info
.hdr
.magic
= (type
== XFS_DIR2_LEAF1_MAGIC
)
311 ? cpu_to_be16(XFS_DIR3_LEAF1_MAGIC
)
312 : cpu_to_be16(XFS_DIR3_LEAFN_MAGIC
);
313 leaf3
->info
.blkno
= cpu_to_be64(bp
->b_bn
);
314 leaf3
->info
.owner
= cpu_to_be64(owner
);
315 uuid_copy(&leaf3
->info
.uuid
, &mp
->m_sb
.sb_uuid
);
317 memset(leaf
, 0, sizeof(*leaf
));
318 leaf
->hdr
.info
.magic
= cpu_to_be16(type
);
322 * If it's a leaf-format directory initialize the tail.
323 * Caller is responsible for initialising the bests table.
325 if (type
== XFS_DIR2_LEAF1_MAGIC
) {
326 struct xfs_dir2_leaf_tail
*ltp
;
328 ltp
= xfs_dir2_leaf_tail_p(mp
->m_dir_geo
, leaf
);
330 bp
->b_ops
= &xfs_dir3_leaf1_buf_ops
;
331 xfs_trans_buf_set_type(tp
, bp
, XFS_BLFT_DIR_LEAF1_BUF
);
333 bp
->b_ops
= &xfs_dir3_leafn_buf_ops
;
334 xfs_trans_buf_set_type(tp
, bp
, XFS_BLFT_DIR_LEAFN_BUF
);
339 xfs_dir3_leaf_get_buf(
342 struct xfs_buf
**bpp
,
345 struct xfs_inode
*dp
= args
->dp
;
346 struct xfs_trans
*tp
= args
->trans
;
347 struct xfs_mount
*mp
= dp
->i_mount
;
351 ASSERT(magic
== XFS_DIR2_LEAF1_MAGIC
|| magic
== XFS_DIR2_LEAFN_MAGIC
);
352 ASSERT(bno
>= xfs_dir2_byte_to_db(args
->geo
, XFS_DIR2_LEAF_OFFSET
) &&
353 bno
< xfs_dir2_byte_to_db(args
->geo
, XFS_DIR2_FREE_OFFSET
));
355 error
= xfs_da_get_buf(tp
, dp
, xfs_dir2_db_to_da(args
->geo
, bno
),
356 -1, &bp
, XFS_DATA_FORK
);
360 xfs_dir3_leaf_init(mp
, tp
, bp
, dp
->i_ino
, magic
);
361 xfs_dir3_leaf_log_header(args
, bp
);
362 if (magic
== XFS_DIR2_LEAF1_MAGIC
)
363 xfs_dir3_leaf_log_tail(args
, bp
);
369 * Convert a block form directory to a leaf form directory.
372 xfs_dir2_block_to_leaf(
373 xfs_da_args_t
*args
, /* operation arguments */
374 struct xfs_buf
*dbp
) /* input block's buffer */
376 __be16
*bestsp
; /* leaf's bestsp entries */
377 xfs_dablk_t blkno
; /* leaf block's bno */
378 xfs_dir2_data_hdr_t
*hdr
; /* block header */
379 xfs_dir2_leaf_entry_t
*blp
; /* block's leaf entries */
380 xfs_dir2_block_tail_t
*btp
; /* block's tail */
381 xfs_inode_t
*dp
; /* incore directory inode */
382 int error
; /* error return code */
383 struct xfs_buf
*lbp
; /* leaf block's buffer */
384 xfs_dir2_db_t ldb
; /* leaf block's bno */
385 xfs_dir2_leaf_t
*leaf
; /* leaf structure */
386 xfs_dir2_leaf_tail_t
*ltp
; /* leaf's tail */
387 xfs_mount_t
*mp
; /* filesystem mount point */
388 int needlog
; /* need to log block header */
389 int needscan
; /* need to rescan bestfree */
390 xfs_trans_t
*tp
; /* transaction pointer */
391 struct xfs_dir2_data_free
*bf
;
392 struct xfs_dir2_leaf_entry
*ents
;
393 struct xfs_dir3_icleaf_hdr leafhdr
;
395 trace_xfs_dir2_block_to_leaf(args
);
401 * Add the leaf block to the inode.
402 * This interface will only put blocks in the leaf/node range.
403 * Since that's empty now, we'll get the root (block 0 in range).
405 if ((error
= xfs_da_grow_inode(args
, &blkno
))) {
408 ldb
= xfs_dir2_da_to_db(args
->geo
, blkno
);
409 ASSERT(ldb
== xfs_dir2_byte_to_db(args
->geo
, XFS_DIR2_LEAF_OFFSET
));
411 * Initialize the leaf block, get a buffer for it.
413 error
= xfs_dir3_leaf_get_buf(args
, ldb
, &lbp
, XFS_DIR2_LEAF1_MAGIC
);
419 xfs_dir3_data_check(dp
, dbp
);
420 btp
= xfs_dir2_block_tail_p(args
->geo
, hdr
);
421 blp
= xfs_dir2_block_leaf_p(btp
);
422 bf
= dp
->d_ops
->data_bestfree_p(hdr
);
423 ents
= dp
->d_ops
->leaf_ents_p(leaf
);
426 * Set the counts in the leaf header.
428 dp
->d_ops
->leaf_hdr_from_disk(&leafhdr
, leaf
);
429 leafhdr
.count
= be32_to_cpu(btp
->count
);
430 leafhdr
.stale
= be32_to_cpu(btp
->stale
);
431 dp
->d_ops
->leaf_hdr_to_disk(leaf
, &leafhdr
);
432 xfs_dir3_leaf_log_header(args
, lbp
);
435 * Could compact these but I think we always do the conversion
436 * after squeezing out stale entries.
438 memcpy(ents
, blp
, be32_to_cpu(btp
->count
) * sizeof(xfs_dir2_leaf_entry_t
));
439 xfs_dir3_leaf_log_ents(args
, lbp
, 0, leafhdr
.count
- 1);
443 * Make the space formerly occupied by the leaf entries and block
446 xfs_dir2_data_make_free(args
, dbp
,
447 (xfs_dir2_data_aoff_t
)((char *)blp
- (char *)hdr
),
448 (xfs_dir2_data_aoff_t
)((char *)hdr
+ args
->geo
->blksize
-
450 &needlog
, &needscan
);
452 * Fix up the block header, make it a data block.
454 dbp
->b_ops
= &xfs_dir3_data_buf_ops
;
455 xfs_trans_buf_set_type(tp
, dbp
, XFS_BLFT_DIR_DATA_BUF
);
456 if (hdr
->magic
== cpu_to_be32(XFS_DIR2_BLOCK_MAGIC
))
457 hdr
->magic
= cpu_to_be32(XFS_DIR2_DATA_MAGIC
);
459 hdr
->magic
= cpu_to_be32(XFS_DIR3_DATA_MAGIC
);
462 xfs_dir2_data_freescan(dp
, hdr
, &needlog
);
464 * Set up leaf tail and bests table.
466 ltp
= xfs_dir2_leaf_tail_p(args
->geo
, leaf
);
467 ltp
->bestcount
= cpu_to_be32(1);
468 bestsp
= xfs_dir2_leaf_bests_p(ltp
);
469 bestsp
[0] = bf
[0].length
;
471 * Log the data header and leaf bests table.
474 xfs_dir2_data_log_header(args
, dbp
);
475 xfs_dir3_leaf_check(dp
, lbp
);
476 xfs_dir3_data_check(dp
, dbp
);
477 xfs_dir3_leaf_log_bests(args
, lbp
, 0, 0);
482 xfs_dir3_leaf_find_stale(
483 struct xfs_dir3_icleaf_hdr
*leafhdr
,
484 struct xfs_dir2_leaf_entry
*ents
,
490 * Find the first stale entry before our index, if any.
492 for (*lowstale
= index
- 1; *lowstale
>= 0; --*lowstale
) {
493 if (ents
[*lowstale
].address
==
494 cpu_to_be32(XFS_DIR2_NULL_DATAPTR
))
499 * Find the first stale entry at or after our index, if any.
500 * Stop if the result would require moving more entries than using
503 for (*highstale
= index
; *highstale
< leafhdr
->count
; ++*highstale
) {
504 if (ents
[*highstale
].address
==
505 cpu_to_be32(XFS_DIR2_NULL_DATAPTR
))
507 if (*lowstale
>= 0 && index
- *lowstale
<= *highstale
- index
)
512 struct xfs_dir2_leaf_entry
*
513 xfs_dir3_leaf_find_entry(
514 struct xfs_dir3_icleaf_hdr
*leafhdr
,
515 struct xfs_dir2_leaf_entry
*ents
,
516 int index
, /* leaf table position */
517 int compact
, /* need to compact leaves */
518 int lowstale
, /* index of prev stale leaf */
519 int highstale
, /* index of next stale leaf */
520 int *lfloglow
, /* low leaf logging index */
521 int *lfloghigh
) /* high leaf logging index */
523 if (!leafhdr
->stale
) {
524 xfs_dir2_leaf_entry_t
*lep
; /* leaf entry table pointer */
527 * Now we need to make room to insert the leaf entry.
529 * If there are no stale entries, just insert a hole at index.
532 if (index
< leafhdr
->count
)
533 memmove(lep
+ 1, lep
,
534 (leafhdr
->count
- index
) * sizeof(*lep
));
537 * Record low and high logging indices for the leaf.
540 *lfloghigh
= leafhdr
->count
++;
545 * There are stale entries.
547 * We will use one of them for the new entry. It's probably not at
548 * the right location, so we'll have to shift some up or down first.
550 * If we didn't compact before, we need to find the nearest stale
551 * entries before and after our insertion point.
554 xfs_dir3_leaf_find_stale(leafhdr
, ents
, index
,
555 &lowstale
, &highstale
);
558 * If the low one is better, use it.
561 (highstale
== leafhdr
->count
||
562 index
- lowstale
- 1 < highstale
- index
)) {
563 ASSERT(index
- lowstale
- 1 >= 0);
564 ASSERT(ents
[lowstale
].address
==
565 cpu_to_be32(XFS_DIR2_NULL_DATAPTR
));
568 * Copy entries up to cover the stale entry and make room
571 if (index
- lowstale
- 1 > 0) {
572 memmove(&ents
[lowstale
], &ents
[lowstale
+ 1],
573 (index
- lowstale
- 1) *
574 sizeof(xfs_dir2_leaf_entry_t
));
576 *lfloglow
= MIN(lowstale
, *lfloglow
);
577 *lfloghigh
= MAX(index
- 1, *lfloghigh
);
579 return &ents
[index
- 1];
583 * The high one is better, so use that one.
585 ASSERT(highstale
- index
>= 0);
586 ASSERT(ents
[highstale
].address
== cpu_to_be32(XFS_DIR2_NULL_DATAPTR
));
589 * Copy entries down to cover the stale entry and make room for the
592 if (highstale
- index
> 0) {
593 memmove(&ents
[index
+ 1], &ents
[index
],
594 (highstale
- index
) * sizeof(xfs_dir2_leaf_entry_t
));
596 *lfloglow
= MIN(index
, *lfloglow
);
597 *lfloghigh
= MAX(highstale
, *lfloghigh
);
603 * Add an entry to a leaf form directory.
606 xfs_dir2_leaf_addname(
607 xfs_da_args_t
*args
) /* operation arguments */
609 __be16
*bestsp
; /* freespace table in leaf */
610 int compact
; /* need to compact leaves */
611 xfs_dir2_data_hdr_t
*hdr
; /* data block header */
612 struct xfs_buf
*dbp
; /* data block buffer */
613 xfs_dir2_data_entry_t
*dep
; /* data block entry */
614 xfs_inode_t
*dp
; /* incore directory inode */
615 xfs_dir2_data_unused_t
*dup
; /* data unused entry */
616 int error
; /* error return value */
617 int grown
; /* allocated new data block */
618 int highstale
; /* index of next stale leaf */
619 int i
; /* temporary, index */
620 int index
; /* leaf table position */
621 struct xfs_buf
*lbp
; /* leaf's buffer */
622 xfs_dir2_leaf_t
*leaf
; /* leaf structure */
623 int length
; /* length of new entry */
624 xfs_dir2_leaf_entry_t
*lep
; /* leaf entry table pointer */
625 int lfloglow
; /* low leaf logging index */
626 int lfloghigh
; /* high leaf logging index */
627 int lowstale
; /* index of prev stale leaf */
628 xfs_dir2_leaf_tail_t
*ltp
; /* leaf tail pointer */
629 xfs_mount_t
*mp
; /* filesystem mount point */
630 int needbytes
; /* leaf block bytes needed */
631 int needlog
; /* need to log data header */
632 int needscan
; /* need to rescan data free */
633 __be16
*tagp
; /* end of data entry */
634 xfs_trans_t
*tp
; /* transaction pointer */
635 xfs_dir2_db_t use_block
; /* data block number */
636 struct xfs_dir2_data_free
*bf
; /* bestfree table */
637 struct xfs_dir2_leaf_entry
*ents
;
638 struct xfs_dir3_icleaf_hdr leafhdr
;
640 trace_xfs_dir2_leaf_addname(args
);
646 error
= xfs_dir3_leaf_read(tp
, dp
, args
->geo
->leafblk
, -1, &lbp
);
651 * Look up the entry by hash value and name.
652 * We know it's not there, our caller has already done a lookup.
653 * So the index is of the entry to insert in front of.
654 * But if there are dup hash values the index is of the first of those.
656 index
= xfs_dir2_leaf_search_hash(args
, lbp
);
658 ltp
= xfs_dir2_leaf_tail_p(args
->geo
, leaf
);
659 ents
= dp
->d_ops
->leaf_ents_p(leaf
);
660 dp
->d_ops
->leaf_hdr_from_disk(&leafhdr
, leaf
);
661 bestsp
= xfs_dir2_leaf_bests_p(ltp
);
662 length
= dp
->d_ops
->data_entsize(args
->namelen
);
665 * See if there are any entries with the same hash value
666 * and space in their block for the new entry.
667 * This is good because it puts multiple same-hash value entries
668 * in a data block, improving the lookup of those entries.
670 for (use_block
= -1, lep
= &ents
[index
];
671 index
< leafhdr
.count
&& be32_to_cpu(lep
->hashval
) == args
->hashval
;
673 if (be32_to_cpu(lep
->address
) == XFS_DIR2_NULL_DATAPTR
)
675 i
= xfs_dir2_dataptr_to_db(args
->geo
, be32_to_cpu(lep
->address
));
676 ASSERT(i
< be32_to_cpu(ltp
->bestcount
));
677 ASSERT(bestsp
[i
] != cpu_to_be16(NULLDATAOFF
));
678 if (be16_to_cpu(bestsp
[i
]) >= length
) {
684 * Didn't find a block yet, linear search all the data blocks.
686 if (use_block
== -1) {
687 for (i
= 0; i
< be32_to_cpu(ltp
->bestcount
); i
++) {
689 * Remember a block we see that's missing.
691 if (bestsp
[i
] == cpu_to_be16(NULLDATAOFF
) &&
694 else if (be16_to_cpu(bestsp
[i
]) >= length
) {
701 * How many bytes do we need in the leaf block?
705 needbytes
+= sizeof(xfs_dir2_leaf_entry_t
);
707 needbytes
+= sizeof(xfs_dir2_data_off_t
);
710 * Now kill use_block if it refers to a missing block, so we
711 * can use it as an indication of allocation needed.
713 if (use_block
!= -1 && bestsp
[use_block
] == cpu_to_be16(NULLDATAOFF
))
716 * If we don't have enough free bytes but we can make enough
717 * by compacting out stale entries, we'll do that.
719 if ((char *)bestsp
- (char *)&ents
[leafhdr
.count
] < needbytes
&&
724 * Otherwise if we don't have enough free bytes we need to
725 * convert to node form.
727 else if ((char *)bestsp
- (char *)&ents
[leafhdr
.count
] < needbytes
) {
729 * Just checking or no space reservation, give up.
731 if ((args
->op_flags
& XFS_DA_OP_JUSTCHECK
) ||
733 xfs_trans_brelse(tp
, lbp
);
734 return XFS_ERROR(ENOSPC
);
737 * Convert to node form.
739 error
= xfs_dir2_leaf_to_node(args
, lbp
);
743 * Then add the new entry.
745 return xfs_dir2_node_addname(args
);
748 * Otherwise it will fit without compaction.
753 * If just checking, then it will fit unless we needed to allocate
756 if (args
->op_flags
& XFS_DA_OP_JUSTCHECK
) {
757 xfs_trans_brelse(tp
, lbp
);
758 return use_block
== -1 ? XFS_ERROR(ENOSPC
) : 0;
761 * If no allocations are allowed, return now before we've
764 if (args
->total
== 0 && use_block
== -1) {
765 xfs_trans_brelse(tp
, lbp
);
766 return XFS_ERROR(ENOSPC
);
769 * Need to compact the leaf entries, removing stale ones.
770 * Leave one stale entry behind - the one closest to our
771 * insertion index - and we'll shift that one to our insertion
775 xfs_dir3_leaf_compact_x1(&leafhdr
, ents
, &index
, &lowstale
,
776 &highstale
, &lfloglow
, &lfloghigh
);
779 * There are stale entries, so we'll need log-low and log-high
780 * impossibly bad values later.
782 else if (leafhdr
.stale
) {
783 lfloglow
= leafhdr
.count
;
787 * If there was no data block space found, we need to allocate
790 if (use_block
== -1) {
792 * Add the new data block.
794 if ((error
= xfs_dir2_grow_inode(args
, XFS_DIR2_DATA_SPACE
,
796 xfs_trans_brelse(tp
, lbp
);
800 * Initialize the block.
802 if ((error
= xfs_dir3_data_init(args
, use_block
, &dbp
))) {
803 xfs_trans_brelse(tp
, lbp
);
807 * If we're adding a new data block on the end we need to
808 * extend the bests table. Copy it up one entry.
810 if (use_block
>= be32_to_cpu(ltp
->bestcount
)) {
812 memmove(&bestsp
[0], &bestsp
[1],
813 be32_to_cpu(ltp
->bestcount
) * sizeof(bestsp
[0]));
814 be32_add_cpu(<p
->bestcount
, 1);
815 xfs_dir3_leaf_log_tail(args
, lbp
);
816 xfs_dir3_leaf_log_bests(args
, lbp
, 0,
817 be32_to_cpu(ltp
->bestcount
) - 1);
820 * If we're filling in a previously empty block just log it.
823 xfs_dir3_leaf_log_bests(args
, lbp
, use_block
, use_block
);
825 bf
= dp
->d_ops
->data_bestfree_p(hdr
);
826 bestsp
[use_block
] = bf
[0].length
;
830 * Already had space in some data block.
831 * Just read that one in.
833 error
= xfs_dir3_data_read(tp
, dp
,
834 xfs_dir2_db_to_da(args
->geo
, use_block
),
837 xfs_trans_brelse(tp
, lbp
);
841 bf
= dp
->d_ops
->data_bestfree_p(hdr
);
845 * Point to the biggest freespace in our data block.
847 dup
= (xfs_dir2_data_unused_t
*)
848 ((char *)hdr
+ be16_to_cpu(bf
[0].offset
));
849 ASSERT(be16_to_cpu(dup
->length
) >= length
);
850 needscan
= needlog
= 0;
852 * Mark the initial part of our freespace in use for the new entry.
854 xfs_dir2_data_use_free(args
, dbp
, dup
,
855 (xfs_dir2_data_aoff_t
)((char *)dup
- (char *)hdr
), length
,
856 &needlog
, &needscan
);
858 * Initialize our new entry (at last).
860 dep
= (xfs_dir2_data_entry_t
*)dup
;
861 dep
->inumber
= cpu_to_be64(args
->inumber
);
862 dep
->namelen
= args
->namelen
;
863 memcpy(dep
->name
, args
->name
, dep
->namelen
);
864 dp
->d_ops
->data_put_ftype(dep
, args
->filetype
);
865 tagp
= dp
->d_ops
->data_entry_tag_p(dep
);
866 *tagp
= cpu_to_be16((char *)dep
- (char *)hdr
);
868 * Need to scan fix up the bestfree table.
871 xfs_dir2_data_freescan(dp
, hdr
, &needlog
);
873 * Need to log the data block's header.
876 xfs_dir2_data_log_header(args
, dbp
);
877 xfs_dir2_data_log_entry(args
, dbp
, dep
);
879 * If the bests table needs to be changed, do it.
880 * Log the change unless we've already done that.
882 if (be16_to_cpu(bestsp
[use_block
]) != be16_to_cpu(bf
[0].length
)) {
883 bestsp
[use_block
] = bf
[0].length
;
885 xfs_dir3_leaf_log_bests(args
, lbp
, use_block
, use_block
);
888 lep
= xfs_dir3_leaf_find_entry(&leafhdr
, ents
, index
, compact
, lowstale
,
889 highstale
, &lfloglow
, &lfloghigh
);
892 * Fill in the new leaf entry.
894 lep
->hashval
= cpu_to_be32(args
->hashval
);
895 lep
->address
= cpu_to_be32(
896 xfs_dir2_db_off_to_dataptr(args
->geo
, use_block
,
897 be16_to_cpu(*tagp
)));
899 * Log the leaf fields and give up the buffers.
901 dp
->d_ops
->leaf_hdr_to_disk(leaf
, &leafhdr
);
902 xfs_dir3_leaf_log_header(args
, lbp
);
903 xfs_dir3_leaf_log_ents(args
, lbp
, lfloglow
, lfloghigh
);
904 xfs_dir3_leaf_check(dp
, lbp
);
905 xfs_dir3_data_check(dp
, dbp
);
910 * Compact out any stale entries in the leaf.
911 * Log the header and changed leaf entries, if any.
914 xfs_dir3_leaf_compact(
915 xfs_da_args_t
*args
, /* operation arguments */
916 struct xfs_dir3_icleaf_hdr
*leafhdr
,
917 struct xfs_buf
*bp
) /* leaf buffer */
919 int from
; /* source leaf index */
920 xfs_dir2_leaf_t
*leaf
; /* leaf structure */
921 int loglow
; /* first leaf entry to log */
922 int to
; /* target leaf index */
923 struct xfs_dir2_leaf_entry
*ents
;
924 struct xfs_inode
*dp
= args
->dp
;
931 * Compress out the stale entries in place.
933 ents
= dp
->d_ops
->leaf_ents_p(leaf
);
934 for (from
= to
= 0, loglow
= -1; from
< leafhdr
->count
; from
++) {
935 if (ents
[from
].address
== cpu_to_be32(XFS_DIR2_NULL_DATAPTR
))
938 * Only actually copy the entries that are different.
943 ents
[to
] = ents
[from
];
948 * Update and log the header, log the leaf entries.
950 ASSERT(leafhdr
->stale
== from
- to
);
951 leafhdr
->count
-= leafhdr
->stale
;
954 dp
->d_ops
->leaf_hdr_to_disk(leaf
, leafhdr
);
955 xfs_dir3_leaf_log_header(args
, bp
);
957 xfs_dir3_leaf_log_ents(args
, bp
, loglow
, to
- 1);
961 * Compact the leaf entries, removing stale ones.
962 * Leave one stale entry behind - the one closest to our
963 * insertion index - and the caller will shift that one to our insertion
965 * Return new insertion index, where the remaining stale entry is,
966 * and leaf logging indices.
969 xfs_dir3_leaf_compact_x1(
970 struct xfs_dir3_icleaf_hdr
*leafhdr
,
971 struct xfs_dir2_leaf_entry
*ents
,
972 int *indexp
, /* insertion index */
973 int *lowstalep
, /* out: stale entry before us */
974 int *highstalep
, /* out: stale entry after us */
975 int *lowlogp
, /* out: low log index */
976 int *highlogp
) /* out: high log index */
978 int from
; /* source copy index */
979 int highstale
; /* stale entry at/after index */
980 int index
; /* insertion index */
981 int keepstale
; /* source index of kept stale */
982 int lowstale
; /* stale entry before index */
983 int newindex
=0; /* new insertion index */
984 int to
; /* destination copy index */
986 ASSERT(leafhdr
->stale
> 1);
989 xfs_dir3_leaf_find_stale(leafhdr
, ents
, index
, &lowstale
, &highstale
);
992 * Pick the better of lowstale and highstale.
995 (highstale
== leafhdr
->count
||
996 index
- lowstale
<= highstale
- index
))
997 keepstale
= lowstale
;
999 keepstale
= highstale
;
1001 * Copy the entries in place, removing all the stale entries
1004 for (from
= to
= 0; from
< leafhdr
->count
; from
++) {
1006 * Notice the new value of index.
1010 if (from
!= keepstale
&&
1011 ents
[from
].address
== cpu_to_be32(XFS_DIR2_NULL_DATAPTR
)) {
1017 * Record the new keepstale value for the insertion.
1019 if (from
== keepstale
)
1020 lowstale
= highstale
= to
;
1022 * Copy only the entries that have moved.
1025 ents
[to
] = ents
[from
];
1030 * If the insertion point was past the last entry,
1031 * set the new insertion point accordingly.
1037 * Adjust the leaf header values.
1039 leafhdr
->count
-= from
- to
;
1042 * Remember the low/high stale value only in the "right"
1045 if (lowstale
>= newindex
)
1048 highstale
= leafhdr
->count
;
1049 *highlogp
= leafhdr
->count
- 1;
1050 *lowstalep
= lowstale
;
1051 *highstalep
= highstale
;
1055 * Log the bests entries indicated from a leaf1 block.
1058 xfs_dir3_leaf_log_bests(
1059 struct xfs_da_args
*args
,
1060 struct xfs_buf
*bp
, /* leaf buffer */
1061 int first
, /* first entry to log */
1062 int last
) /* last entry to log */
1064 __be16
*firstb
; /* pointer to first entry */
1065 __be16
*lastb
; /* pointer to last entry */
1066 struct xfs_dir2_leaf
*leaf
= bp
->b_addr
;
1067 xfs_dir2_leaf_tail_t
*ltp
; /* leaf tail structure */
1069 ASSERT(leaf
->hdr
.info
.magic
== cpu_to_be16(XFS_DIR2_LEAF1_MAGIC
) ||
1070 leaf
->hdr
.info
.magic
== cpu_to_be16(XFS_DIR3_LEAF1_MAGIC
));
1072 ltp
= xfs_dir2_leaf_tail_p(args
->geo
, leaf
);
1073 firstb
= xfs_dir2_leaf_bests_p(ltp
) + first
;
1074 lastb
= xfs_dir2_leaf_bests_p(ltp
) + last
;
1075 xfs_trans_log_buf(args
->trans
, bp
,
1076 (uint
)((char *)firstb
- (char *)leaf
),
1077 (uint
)((char *)lastb
- (char *)leaf
+ sizeof(*lastb
) - 1));
1081 * Log the leaf entries indicated from a leaf1 or leafn block.
1084 xfs_dir3_leaf_log_ents(
1085 struct xfs_da_args
*args
,
1090 xfs_dir2_leaf_entry_t
*firstlep
; /* pointer to first entry */
1091 xfs_dir2_leaf_entry_t
*lastlep
; /* pointer to last entry */
1092 struct xfs_dir2_leaf
*leaf
= bp
->b_addr
;
1093 struct xfs_dir2_leaf_entry
*ents
;
1095 ASSERT(leaf
->hdr
.info
.magic
== cpu_to_be16(XFS_DIR2_LEAF1_MAGIC
) ||
1096 leaf
->hdr
.info
.magic
== cpu_to_be16(XFS_DIR3_LEAF1_MAGIC
) ||
1097 leaf
->hdr
.info
.magic
== cpu_to_be16(XFS_DIR2_LEAFN_MAGIC
) ||
1098 leaf
->hdr
.info
.magic
== cpu_to_be16(XFS_DIR3_LEAFN_MAGIC
));
1100 ents
= args
->dp
->d_ops
->leaf_ents_p(leaf
);
1101 firstlep
= &ents
[first
];
1102 lastlep
= &ents
[last
];
1103 xfs_trans_log_buf(args
->trans
, bp
,
1104 (uint
)((char *)firstlep
- (char *)leaf
),
1105 (uint
)((char *)lastlep
- (char *)leaf
+ sizeof(*lastlep
) - 1));
1109 * Log the header of the leaf1 or leafn block.
1112 xfs_dir3_leaf_log_header(
1113 struct xfs_da_args
*args
,
1116 struct xfs_dir2_leaf
*leaf
= bp
->b_addr
;
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 xfs_trans_log_buf(args
->trans
, bp
,
1124 (uint
)((char *)&leaf
->hdr
- (char *)leaf
),
1125 args
->dp
->d_ops
->leaf_hdr_size
- 1);
1129 * Log the tail of the leaf1 block.
1132 xfs_dir3_leaf_log_tail(
1133 struct xfs_da_args
*args
,
1136 struct xfs_dir2_leaf
*leaf
= bp
->b_addr
;
1137 xfs_dir2_leaf_tail_t
*ltp
; /* leaf tail structure */
1139 ASSERT(leaf
->hdr
.info
.magic
== cpu_to_be16(XFS_DIR2_LEAF1_MAGIC
) ||
1140 leaf
->hdr
.info
.magic
== cpu_to_be16(XFS_DIR3_LEAF1_MAGIC
) ||
1141 leaf
->hdr
.info
.magic
== cpu_to_be16(XFS_DIR2_LEAFN_MAGIC
) ||
1142 leaf
->hdr
.info
.magic
== cpu_to_be16(XFS_DIR3_LEAFN_MAGIC
));
1144 ltp
= xfs_dir2_leaf_tail_p(args
->geo
, leaf
);
1145 xfs_trans_log_buf(args
->trans
, bp
, (uint
)((char *)ltp
- (char *)leaf
),
1146 (uint
)(args
->geo
->blksize
- 1));
1150 * Look up the entry referred to by args in the leaf format directory.
1151 * Most of the work is done by the xfs_dir2_leaf_lookup_int routine which
1152 * is also used by the node-format code.
1155 xfs_dir2_leaf_lookup(
1156 xfs_da_args_t
*args
) /* operation arguments */
1158 struct xfs_buf
*dbp
; /* data block buffer */
1159 xfs_dir2_data_entry_t
*dep
; /* data block entry */
1160 xfs_inode_t
*dp
; /* incore directory inode */
1161 int error
; /* error return code */
1162 int index
; /* found entry index */
1163 struct xfs_buf
*lbp
; /* leaf buffer */
1164 xfs_dir2_leaf_t
*leaf
; /* leaf structure */
1165 xfs_dir2_leaf_entry_t
*lep
; /* leaf entry */
1166 xfs_trans_t
*tp
; /* transaction pointer */
1167 struct xfs_dir2_leaf_entry
*ents
;
1169 trace_xfs_dir2_leaf_lookup(args
);
1172 * Look up name in the leaf block, returning both buffers and index.
1174 if ((error
= xfs_dir2_leaf_lookup_int(args
, &lbp
, &index
, &dbp
))) {
1179 xfs_dir3_leaf_check(dp
, lbp
);
1181 ents
= dp
->d_ops
->leaf_ents_p(leaf
);
1183 * Get to the leaf entry and contained data entry address.
1188 * Point to the data entry.
1190 dep
= (xfs_dir2_data_entry_t
*)
1191 ((char *)dbp
->b_addr
+
1192 xfs_dir2_dataptr_to_off(args
->geo
, be32_to_cpu(lep
->address
)));
1194 * Return the found inode number & CI name if appropriate
1196 args
->inumber
= be64_to_cpu(dep
->inumber
);
1197 args
->filetype
= dp
->d_ops
->data_get_ftype(dep
);
1198 error
= xfs_dir_cilookup_result(args
, dep
->name
, dep
->namelen
);
1199 xfs_trans_brelse(tp
, dbp
);
1200 xfs_trans_brelse(tp
, lbp
);
1201 return XFS_ERROR(error
);
1205 * Look up name/hash in the leaf block.
1206 * Fill in indexp with the found index, and dbpp with the data buffer.
1207 * If not found dbpp will be NULL, and ENOENT comes back.
1208 * lbpp will always be filled in with the leaf buffer unless there's an error.
1210 static int /* error */
1211 xfs_dir2_leaf_lookup_int(
1212 xfs_da_args_t
*args
, /* operation arguments */
1213 struct xfs_buf
**lbpp
, /* out: leaf buffer */
1214 int *indexp
, /* out: index in leaf block */
1215 struct xfs_buf
**dbpp
) /* out: data buffer */
1217 xfs_dir2_db_t curdb
= -1; /* current data block number */
1218 struct xfs_buf
*dbp
= NULL
; /* data buffer */
1219 xfs_dir2_data_entry_t
*dep
; /* data entry */
1220 xfs_inode_t
*dp
; /* incore directory inode */
1221 int error
; /* error return code */
1222 int index
; /* index in leaf block */
1223 struct xfs_buf
*lbp
; /* leaf buffer */
1224 xfs_dir2_leaf_entry_t
*lep
; /* leaf entry */
1225 xfs_dir2_leaf_t
*leaf
; /* leaf structure */
1226 xfs_mount_t
*mp
; /* filesystem mount point */
1227 xfs_dir2_db_t newdb
; /* new data block number */
1228 xfs_trans_t
*tp
; /* transaction pointer */
1229 xfs_dir2_db_t cidb
= -1; /* case match data block no. */
1230 enum xfs_dacmp cmp
; /* name compare result */
1231 struct xfs_dir2_leaf_entry
*ents
;
1232 struct xfs_dir3_icleaf_hdr leafhdr
;
1238 error
= xfs_dir3_leaf_read(tp
, dp
, args
->geo
->leafblk
, -1, &lbp
);
1244 xfs_dir3_leaf_check(dp
, lbp
);
1245 ents
= dp
->d_ops
->leaf_ents_p(leaf
);
1246 dp
->d_ops
->leaf_hdr_from_disk(&leafhdr
, leaf
);
1249 * Look for the first leaf entry with our hash value.
1251 index
= xfs_dir2_leaf_search_hash(args
, lbp
);
1253 * Loop over all the entries with the right hash value
1254 * looking to match the name.
1256 for (lep
= &ents
[index
];
1257 index
< leafhdr
.count
&& be32_to_cpu(lep
->hashval
) == args
->hashval
;
1260 * Skip over stale leaf entries.
1262 if (be32_to_cpu(lep
->address
) == XFS_DIR2_NULL_DATAPTR
)
1265 * Get the new data block number.
1267 newdb
= xfs_dir2_dataptr_to_db(args
->geo
,
1268 be32_to_cpu(lep
->address
));
1270 * If it's not the same as the old data block number,
1271 * need to pitch the old one and read the new one.
1273 if (newdb
!= curdb
) {
1275 xfs_trans_brelse(tp
, dbp
);
1276 error
= xfs_dir3_data_read(tp
, dp
,
1277 xfs_dir2_db_to_da(args
->geo
, newdb
),
1280 xfs_trans_brelse(tp
, lbp
);
1286 * Point to the data entry.
1288 dep
= (xfs_dir2_data_entry_t
*)((char *)dbp
->b_addr
+
1289 xfs_dir2_dataptr_to_off(args
->geo
,
1290 be32_to_cpu(lep
->address
)));
1292 * Compare name and if it's an exact match, return the index
1293 * and buffer. If it's the first case-insensitive match, store
1294 * the index and buffer and continue looking for an exact match.
1296 cmp
= mp
->m_dirnameops
->compname(args
, dep
->name
, dep
->namelen
);
1297 if (cmp
!= XFS_CMP_DIFFERENT
&& cmp
!= args
->cmpresult
) {
1298 args
->cmpresult
= cmp
;
1300 /* case exact match: return the current buffer. */
1301 if (cmp
== XFS_CMP_EXACT
) {
1308 ASSERT(args
->op_flags
& XFS_DA_OP_OKNOENT
);
1310 * Here, we can only be doing a lookup (not a rename or remove).
1311 * If a case-insensitive match was found earlier, re-read the
1312 * appropriate data block if required and return it.
1314 if (args
->cmpresult
== XFS_CMP_CASE
) {
1316 if (cidb
!= curdb
) {
1317 xfs_trans_brelse(tp
, dbp
);
1318 error
= xfs_dir3_data_read(tp
, dp
,
1319 xfs_dir2_db_to_da(args
->geo
, cidb
),
1322 xfs_trans_brelse(tp
, lbp
);
1330 * No match found, return ENOENT.
1334 xfs_trans_brelse(tp
, dbp
);
1335 xfs_trans_brelse(tp
, lbp
);
1336 return XFS_ERROR(ENOENT
);
1340 * Remove an entry from a leaf format directory.
1343 xfs_dir2_leaf_removename(
1344 xfs_da_args_t
*args
) /* operation arguments */
1346 __be16
*bestsp
; /* leaf block best freespace */
1347 xfs_dir2_data_hdr_t
*hdr
; /* data block header */
1348 xfs_dir2_db_t db
; /* data block number */
1349 struct xfs_buf
*dbp
; /* data block buffer */
1350 xfs_dir2_data_entry_t
*dep
; /* data entry structure */
1351 xfs_inode_t
*dp
; /* incore directory inode */
1352 int error
; /* error return code */
1353 xfs_dir2_db_t i
; /* temporary data block # */
1354 int index
; /* index into leaf entries */
1355 struct xfs_buf
*lbp
; /* leaf buffer */
1356 xfs_dir2_leaf_t
*leaf
; /* leaf structure */
1357 xfs_dir2_leaf_entry_t
*lep
; /* leaf entry */
1358 xfs_dir2_leaf_tail_t
*ltp
; /* leaf tail structure */
1359 xfs_mount_t
*mp
; /* filesystem mount point */
1360 int needlog
; /* need to log data header */
1361 int needscan
; /* need to rescan data frees */
1362 xfs_dir2_data_off_t oldbest
; /* old value of best free */
1363 xfs_trans_t
*tp
; /* transaction pointer */
1364 struct xfs_dir2_data_free
*bf
; /* bestfree table */
1365 struct xfs_dir2_leaf_entry
*ents
;
1366 struct xfs_dir3_icleaf_hdr leafhdr
;
1368 trace_xfs_dir2_leaf_removename(args
);
1371 * Lookup the leaf entry, get the leaf and data blocks read in.
1373 if ((error
= xfs_dir2_leaf_lookup_int(args
, &lbp
, &index
, &dbp
))) {
1381 xfs_dir3_data_check(dp
, dbp
);
1382 bf
= dp
->d_ops
->data_bestfree_p(hdr
);
1383 dp
->d_ops
->leaf_hdr_from_disk(&leafhdr
, leaf
);
1384 ents
= dp
->d_ops
->leaf_ents_p(leaf
);
1386 * Point to the leaf entry, use that to point to the data entry.
1389 db
= xfs_dir2_dataptr_to_db(args
->geo
, be32_to_cpu(lep
->address
));
1390 dep
= (xfs_dir2_data_entry_t
*)((char *)hdr
+
1391 xfs_dir2_dataptr_to_off(args
->geo
, be32_to_cpu(lep
->address
)));
1392 needscan
= needlog
= 0;
1393 oldbest
= be16_to_cpu(bf
[0].length
);
1394 ltp
= xfs_dir2_leaf_tail_p(args
->geo
, leaf
);
1395 bestsp
= xfs_dir2_leaf_bests_p(ltp
);
1396 ASSERT(be16_to_cpu(bestsp
[db
]) == oldbest
);
1398 * Mark the former data entry unused.
1400 xfs_dir2_data_make_free(args
, dbp
,
1401 (xfs_dir2_data_aoff_t
)((char *)dep
- (char *)hdr
),
1402 dp
->d_ops
->data_entsize(dep
->namelen
), &needlog
, &needscan
);
1404 * We just mark the leaf entry stale by putting a null in it.
1407 dp
->d_ops
->leaf_hdr_to_disk(leaf
, &leafhdr
);
1408 xfs_dir3_leaf_log_header(args
, lbp
);
1410 lep
->address
= cpu_to_be32(XFS_DIR2_NULL_DATAPTR
);
1411 xfs_dir3_leaf_log_ents(args
, lbp
, index
, index
);
1414 * Scan the freespace in the data block again if necessary,
1415 * log the data block header if necessary.
1418 xfs_dir2_data_freescan(dp
, hdr
, &needlog
);
1420 xfs_dir2_data_log_header(args
, dbp
);
1422 * If the longest freespace in the data block has changed,
1423 * put the new value in the bests table and log that.
1425 if (be16_to_cpu(bf
[0].length
) != oldbest
) {
1426 bestsp
[db
] = bf
[0].length
;
1427 xfs_dir3_leaf_log_bests(args
, lbp
, db
, db
);
1429 xfs_dir3_data_check(dp
, dbp
);
1431 * If the data block is now empty then get rid of the data block.
1433 if (be16_to_cpu(bf
[0].length
) ==
1434 args
->geo
->blksize
- dp
->d_ops
->data_entry_offset
) {
1435 ASSERT(db
!= args
->geo
->datablk
);
1436 if ((error
= xfs_dir2_shrink_inode(args
, db
, dbp
))) {
1438 * Nope, can't get rid of it because it caused
1439 * allocation of a bmap btree block to do so.
1440 * Just go on, returning success, leaving the
1441 * empty block in place.
1443 if (error
== ENOSPC
&& args
->total
== 0)
1445 xfs_dir3_leaf_check(dp
, lbp
);
1450 * If this is the last data block then compact the
1451 * bests table by getting rid of entries.
1453 if (db
== be32_to_cpu(ltp
->bestcount
) - 1) {
1455 * Look for the last active entry (i).
1457 for (i
= db
- 1; i
> 0; i
--) {
1458 if (bestsp
[i
] != cpu_to_be16(NULLDATAOFF
))
1462 * Copy the table down so inactive entries at the
1465 memmove(&bestsp
[db
- i
], bestsp
,
1466 (be32_to_cpu(ltp
->bestcount
) - (db
- i
)) * sizeof(*bestsp
));
1467 be32_add_cpu(<p
->bestcount
, -(db
- i
));
1468 xfs_dir3_leaf_log_tail(args
, lbp
);
1469 xfs_dir3_leaf_log_bests(args
, lbp
, 0,
1470 be32_to_cpu(ltp
->bestcount
) - 1);
1472 bestsp
[db
] = cpu_to_be16(NULLDATAOFF
);
1475 * If the data block was not the first one, drop it.
1477 else if (db
!= args
->geo
->datablk
)
1480 xfs_dir3_leaf_check(dp
, lbp
);
1482 * See if we can convert to block form.
1484 return xfs_dir2_leaf_to_block(args
, lbp
, dbp
);
1488 * Replace the inode number in a leaf format directory entry.
1491 xfs_dir2_leaf_replace(
1492 xfs_da_args_t
*args
) /* operation arguments */
1494 struct xfs_buf
*dbp
; /* data block buffer */
1495 xfs_dir2_data_entry_t
*dep
; /* data block entry */
1496 xfs_inode_t
*dp
; /* incore directory inode */
1497 int error
; /* error return code */
1498 int index
; /* index of leaf entry */
1499 struct xfs_buf
*lbp
; /* leaf buffer */
1500 xfs_dir2_leaf_t
*leaf
; /* leaf structure */
1501 xfs_dir2_leaf_entry_t
*lep
; /* leaf entry */
1502 xfs_trans_t
*tp
; /* transaction pointer */
1503 struct xfs_dir2_leaf_entry
*ents
;
1505 trace_xfs_dir2_leaf_replace(args
);
1508 * Look up the entry.
1510 if ((error
= xfs_dir2_leaf_lookup_int(args
, &lbp
, &index
, &dbp
))) {
1515 ents
= dp
->d_ops
->leaf_ents_p(leaf
);
1517 * Point to the leaf entry, get data address from it.
1521 * Point to the data entry.
1523 dep
= (xfs_dir2_data_entry_t
*)
1524 ((char *)dbp
->b_addr
+
1525 xfs_dir2_dataptr_to_off(args
->geo
, be32_to_cpu(lep
->address
)));
1526 ASSERT(args
->inumber
!= be64_to_cpu(dep
->inumber
));
1528 * Put the new inode number in, log it.
1530 dep
->inumber
= cpu_to_be64(args
->inumber
);
1531 dp
->d_ops
->data_put_ftype(dep
, args
->filetype
);
1533 xfs_dir2_data_log_entry(args
, dbp
, dep
);
1534 xfs_dir3_leaf_check(dp
, lbp
);
1535 xfs_trans_brelse(tp
, lbp
);
1540 * Return index in the leaf block (lbp) which is either the first
1541 * one with this hash value, or if there are none, the insert point
1542 * for that hash value.
1544 int /* index value */
1545 xfs_dir2_leaf_search_hash(
1546 xfs_da_args_t
*args
, /* operation arguments */
1547 struct xfs_buf
*lbp
) /* leaf buffer */
1549 xfs_dahash_t hash
=0; /* hash from this entry */
1550 xfs_dahash_t hashwant
; /* hash value looking for */
1551 int high
; /* high leaf index */
1552 int low
; /* low leaf index */
1553 xfs_dir2_leaf_t
*leaf
; /* leaf structure */
1554 xfs_dir2_leaf_entry_t
*lep
; /* leaf entry */
1555 int mid
=0; /* current leaf index */
1556 struct xfs_dir2_leaf_entry
*ents
;
1557 struct xfs_dir3_icleaf_hdr leafhdr
;
1560 ents
= args
->dp
->d_ops
->leaf_ents_p(leaf
);
1561 args
->dp
->d_ops
->leaf_hdr_from_disk(&leafhdr
, leaf
);
1564 * Note, the table cannot be empty, so we have to go through the loop.
1565 * Binary search the leaf entries looking for our hash value.
1567 for (lep
= ents
, low
= 0, high
= leafhdr
.count
- 1,
1568 hashwant
= args
->hashval
;
1570 mid
= (low
+ high
) >> 1;
1571 if ((hash
= be32_to_cpu(lep
[mid
].hashval
)) == hashwant
)
1573 if (hash
< hashwant
)
1579 * Found one, back up through all the equal hash values.
1581 if (hash
== hashwant
) {
1582 while (mid
> 0 && be32_to_cpu(lep
[mid
- 1].hashval
) == hashwant
) {
1587 * Need to point to an entry higher than ours.
1589 else if (hash
< hashwant
)
1595 * Trim off a trailing data block. We know it's empty since the leaf
1596 * freespace table says so.
1599 xfs_dir2_leaf_trim_data(
1600 xfs_da_args_t
*args
, /* operation arguments */
1601 struct xfs_buf
*lbp
, /* leaf buffer */
1602 xfs_dir2_db_t db
) /* data block number */
1604 __be16
*bestsp
; /* leaf bests table */
1605 struct xfs_buf
*dbp
; /* data block buffer */
1606 xfs_inode_t
*dp
; /* incore directory inode */
1607 int error
; /* error return value */
1608 xfs_dir2_leaf_t
*leaf
; /* leaf structure */
1609 xfs_dir2_leaf_tail_t
*ltp
; /* leaf tail structure */
1610 xfs_mount_t
*mp
; /* filesystem mount point */
1611 xfs_trans_t
*tp
; /* transaction pointer */
1617 * Read the offending data block. We need its buffer.
1619 error
= xfs_dir3_data_read(tp
, dp
, xfs_dir2_db_to_da(args
->geo
, db
),
1625 ltp
= xfs_dir2_leaf_tail_p(args
->geo
, leaf
);
1629 struct xfs_dir2_data_hdr
*hdr
= dbp
->b_addr
;
1630 struct xfs_dir2_data_free
*bf
= dp
->d_ops
->data_bestfree_p(hdr
);
1632 ASSERT(hdr
->magic
== cpu_to_be32(XFS_DIR2_DATA_MAGIC
) ||
1633 hdr
->magic
== cpu_to_be32(XFS_DIR3_DATA_MAGIC
));
1634 ASSERT(be16_to_cpu(bf
[0].length
) ==
1635 args
->geo
->blksize
- dp
->d_ops
->data_entry_offset
);
1636 ASSERT(db
== be32_to_cpu(ltp
->bestcount
) - 1);
1641 * Get rid of the data block.
1643 if ((error
= xfs_dir2_shrink_inode(args
, db
, dbp
))) {
1644 ASSERT(error
!= ENOSPC
);
1645 xfs_trans_brelse(tp
, dbp
);
1649 * Eliminate the last bests entry from the table.
1651 bestsp
= xfs_dir2_leaf_bests_p(ltp
);
1652 be32_add_cpu(<p
->bestcount
, -1);
1653 memmove(&bestsp
[1], &bestsp
[0], be32_to_cpu(ltp
->bestcount
) * sizeof(*bestsp
));
1654 xfs_dir3_leaf_log_tail(args
, lbp
);
1655 xfs_dir3_leaf_log_bests(args
, lbp
, 0, be32_to_cpu(ltp
->bestcount
) - 1);
1659 static inline size_t
1661 struct xfs_dir3_icleaf_hdr
*hdr
,
1667 entries
= hdr
->count
- hdr
->stale
;
1668 if (hdr
->magic
== XFS_DIR2_LEAF1_MAGIC
||
1669 hdr
->magic
== XFS_DIR2_LEAFN_MAGIC
)
1670 hdrsize
= sizeof(struct xfs_dir2_leaf_hdr
);
1672 hdrsize
= sizeof(struct xfs_dir3_leaf_hdr
);
1674 return hdrsize
+ entries
* sizeof(xfs_dir2_leaf_entry_t
)
1675 + counts
* sizeof(xfs_dir2_data_off_t
)
1676 + sizeof(xfs_dir2_leaf_tail_t
);
1680 * Convert node form directory to leaf form directory.
1681 * The root of the node form dir needs to already be a LEAFN block.
1682 * Just return if we can't do anything.
1685 xfs_dir2_node_to_leaf(
1686 xfs_da_state_t
*state
) /* directory operation state */
1688 xfs_da_args_t
*args
; /* operation arguments */
1689 xfs_inode_t
*dp
; /* incore directory inode */
1690 int error
; /* error return code */
1691 struct xfs_buf
*fbp
; /* buffer for freespace block */
1692 xfs_fileoff_t fo
; /* freespace file offset */
1693 xfs_dir2_free_t
*free
; /* freespace structure */
1694 struct xfs_buf
*lbp
; /* buffer for leaf block */
1695 xfs_dir2_leaf_tail_t
*ltp
; /* tail of leaf structure */
1696 xfs_dir2_leaf_t
*leaf
; /* leaf structure */
1697 xfs_mount_t
*mp
; /* filesystem mount point */
1698 int rval
; /* successful free trim? */
1699 xfs_trans_t
*tp
; /* transaction pointer */
1700 struct xfs_dir3_icleaf_hdr leafhdr
;
1701 struct xfs_dir3_icfree_hdr freehdr
;
1704 * There's more than a leaf level in the btree, so there must
1705 * be multiple leafn blocks. Give up.
1707 if (state
->path
.active
> 1)
1711 trace_xfs_dir2_node_to_leaf(args
);
1717 * Get the last offset in the file.
1719 if ((error
= xfs_bmap_last_offset(dp
, &fo
, XFS_DATA_FORK
))) {
1722 fo
-= args
->geo
->fsbcount
;
1724 * If there are freespace blocks other than the first one,
1725 * take this opportunity to remove trailing empty freespace blocks
1726 * that may have been left behind during no-space-reservation
1729 while (fo
> args
->geo
->freeblk
) {
1730 if ((error
= xfs_dir2_node_trim_free(args
, fo
, &rval
))) {
1734 fo
-= args
->geo
->fsbcount
;
1739 * Now find the block just before the freespace block.
1741 if ((error
= xfs_bmap_last_before(tp
, dp
, &fo
, XFS_DATA_FORK
))) {
1745 * If it's not the single leaf block, give up.
1747 if (XFS_FSB_TO_B(mp
, fo
) > XFS_DIR2_LEAF_OFFSET
+ args
->geo
->blksize
)
1749 lbp
= state
->path
.blk
[0].bp
;
1751 dp
->d_ops
->leaf_hdr_from_disk(&leafhdr
, leaf
);
1753 ASSERT(leafhdr
.magic
== XFS_DIR2_LEAFN_MAGIC
||
1754 leafhdr
.magic
== XFS_DIR3_LEAFN_MAGIC
);
1757 * Read the freespace block.
1759 error
= xfs_dir2_free_read(tp
, dp
, args
->geo
->freeblk
, &fbp
);
1763 dp
->d_ops
->free_hdr_from_disk(&freehdr
, free
);
1765 ASSERT(!freehdr
.firstdb
);
1768 * Now see if the leafn and free data will fit in a leaf1.
1769 * If not, release the buffer and give up.
1771 if (xfs_dir3_leaf_size(&leafhdr
, freehdr
.nvalid
) > args
->geo
->blksize
) {
1772 xfs_trans_brelse(tp
, fbp
);
1777 * If the leaf has any stale entries in it, compress them out.
1780 xfs_dir3_leaf_compact(args
, &leafhdr
, lbp
);
1782 lbp
->b_ops
= &xfs_dir3_leaf1_buf_ops
;
1783 xfs_trans_buf_set_type(tp
, lbp
, XFS_BLFT_DIR_LEAF1_BUF
);
1784 leafhdr
.magic
= (leafhdr
.magic
== XFS_DIR2_LEAFN_MAGIC
)
1785 ? XFS_DIR2_LEAF1_MAGIC
1786 : XFS_DIR3_LEAF1_MAGIC
;
1789 * Set up the leaf tail from the freespace block.
1791 ltp
= xfs_dir2_leaf_tail_p(args
->geo
, leaf
);
1792 ltp
->bestcount
= cpu_to_be32(freehdr
.nvalid
);
1795 * Set up the leaf bests table.
1797 memcpy(xfs_dir2_leaf_bests_p(ltp
), dp
->d_ops
->free_bests_p(free
),
1798 freehdr
.nvalid
* sizeof(xfs_dir2_data_off_t
));
1800 dp
->d_ops
->leaf_hdr_to_disk(leaf
, &leafhdr
);
1801 xfs_dir3_leaf_log_header(args
, lbp
);
1802 xfs_dir3_leaf_log_bests(args
, lbp
, 0, be32_to_cpu(ltp
->bestcount
) - 1);
1803 xfs_dir3_leaf_log_tail(args
, lbp
);
1804 xfs_dir3_leaf_check(dp
, lbp
);
1807 * Get rid of the freespace block.
1809 error
= xfs_dir2_shrink_inode(args
,
1810 xfs_dir2_byte_to_db(args
->geo
, XFS_DIR2_FREE_OFFSET
),
1814 * This can't fail here because it can only happen when
1815 * punching out the middle of an extent, and this is an
1818 ASSERT(error
!= ENOSPC
);
1823 * Now see if we can convert the single-leaf directory
1824 * down to a block form directory.
1825 * This routine always kills the dabuf for the leaf, so
1826 * eliminate it from the path.
1828 error
= xfs_dir2_leaf_to_block(args
, lbp
, NULL
);
1829 state
->path
.blk
[0].bp
= NULL
;