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_types.h"
24 #include "xfs_trans.h"
27 #include "xfs_mount.h"
28 #include "xfs_da_btree.h"
29 #include "xfs_bmap_btree.h"
30 #include "xfs_dinode.h"
31 #include "xfs_inode.h"
33 #include "xfs_dir2_format.h"
34 #include "xfs_dir2_priv.h"
35 #include "xfs_error.h"
36 #include "xfs_trace.h"
37 #include "xfs_buf_item.h"
38 #include "xfs_cksum.h"
41 * Local function declarations.
43 static int xfs_dir2_leaf_lookup_int(xfs_da_args_t
*args
, struct xfs_buf
**lbpp
,
44 int *indexp
, struct xfs_buf
**dbpp
);
45 static void xfs_dir3_leaf_log_bests(struct xfs_trans
*tp
, struct xfs_buf
*bp
,
47 static void xfs_dir3_leaf_log_tail(struct xfs_trans
*tp
, struct xfs_buf
*bp
);
50 * Check the internal consistency of a leaf1 block.
51 * Pop an assert if something is wrong.
54 #define xfs_dir3_leaf_check(mp, bp) \
56 if (!xfs_dir3_leaf1_check((mp), (bp))) \
65 struct xfs_dir2_leaf
*leaf
= bp
->b_addr
;
66 struct xfs_dir3_icleaf_hdr leafhdr
;
68 xfs_dir3_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(mp
, &leafhdr
, leaf
);
80 #define xfs_dir3_leaf_check(mp, bp)
84 xfs_dir3_leaf_hdr_from_disk(
85 struct xfs_dir3_icleaf_hdr
*to
,
86 struct xfs_dir2_leaf
*from
)
88 if (from
->hdr
.info
.magic
== cpu_to_be16(XFS_DIR2_LEAF1_MAGIC
) ||
89 from
->hdr
.info
.magic
== cpu_to_be16(XFS_DIR2_LEAFN_MAGIC
)) {
90 to
->forw
= be32_to_cpu(from
->hdr
.info
.forw
);
91 to
->back
= be32_to_cpu(from
->hdr
.info
.back
);
92 to
->magic
= be16_to_cpu(from
->hdr
.info
.magic
);
93 to
->count
= be16_to_cpu(from
->hdr
.count
);
94 to
->stale
= be16_to_cpu(from
->hdr
.stale
);
96 struct xfs_dir3_leaf_hdr
*hdr3
= (struct xfs_dir3_leaf_hdr
*)from
;
98 to
->forw
= be32_to_cpu(hdr3
->info
.hdr
.forw
);
99 to
->back
= be32_to_cpu(hdr3
->info
.hdr
.back
);
100 to
->magic
= be16_to_cpu(hdr3
->info
.hdr
.magic
);
101 to
->count
= be16_to_cpu(hdr3
->count
);
102 to
->stale
= be16_to_cpu(hdr3
->stale
);
105 ASSERT(to
->magic
== XFS_DIR2_LEAF1_MAGIC
||
106 to
->magic
== XFS_DIR3_LEAF1_MAGIC
||
107 to
->magic
== XFS_DIR2_LEAFN_MAGIC
||
108 to
->magic
== XFS_DIR3_LEAFN_MAGIC
);
112 xfs_dir3_leaf_hdr_to_disk(
113 struct xfs_dir2_leaf
*to
,
114 struct xfs_dir3_icleaf_hdr
*from
)
116 ASSERT(from
->magic
== XFS_DIR2_LEAF1_MAGIC
||
117 from
->magic
== XFS_DIR3_LEAF1_MAGIC
||
118 from
->magic
== XFS_DIR2_LEAFN_MAGIC
||
119 from
->magic
== XFS_DIR3_LEAFN_MAGIC
);
121 if (from
->magic
== XFS_DIR2_LEAF1_MAGIC
||
122 from
->magic
== XFS_DIR2_LEAFN_MAGIC
) {
123 to
->hdr
.info
.forw
= cpu_to_be32(from
->forw
);
124 to
->hdr
.info
.back
= cpu_to_be32(from
->back
);
125 to
->hdr
.info
.magic
= cpu_to_be16(from
->magic
);
126 to
->hdr
.count
= cpu_to_be16(from
->count
);
127 to
->hdr
.stale
= cpu_to_be16(from
->stale
);
129 struct xfs_dir3_leaf_hdr
*hdr3
= (struct xfs_dir3_leaf_hdr
*)to
;
131 hdr3
->info
.hdr
.forw
= cpu_to_be32(from
->forw
);
132 hdr3
->info
.hdr
.back
= cpu_to_be32(from
->back
);
133 hdr3
->info
.hdr
.magic
= cpu_to_be16(from
->magic
);
134 hdr3
->count
= cpu_to_be16(from
->count
);
135 hdr3
->stale
= cpu_to_be16(from
->stale
);
140 xfs_dir3_leaf_check_int(
141 struct xfs_mount
*mp
,
142 struct xfs_dir3_icleaf_hdr
*hdr
,
143 struct xfs_dir2_leaf
*leaf
)
145 struct xfs_dir2_leaf_entry
*ents
;
146 xfs_dir2_leaf_tail_t
*ltp
;
150 ents
= xfs_dir3_leaf_ents_p(leaf
);
151 ltp
= xfs_dir2_leaf_tail_p(mp
, leaf
);
154 * XXX (dgc): This value is not restrictive enough.
155 * Should factor in the size of the bests table as well.
156 * We can deduce a value for that from di_size.
158 if (hdr
->count
> xfs_dir3_max_leaf_ents(mp
, leaf
))
161 /* Leaves and bests don't overlap in leaf format. */
162 if ((hdr
->magic
== XFS_DIR2_LEAF1_MAGIC
||
163 hdr
->magic
== XFS_DIR3_LEAF1_MAGIC
) &&
164 (char *)&ents
[hdr
->count
] > (char *)xfs_dir2_leaf_bests_p(ltp
))
167 /* Check hash value order, count stale entries. */
168 for (i
= stale
= 0; i
< hdr
->count
; i
++) {
169 if (i
+ 1 < hdr
->count
) {
170 if (be32_to_cpu(ents
[i
].hashval
) >
171 be32_to_cpu(ents
[i
+ 1].hashval
))
174 if (ents
[i
].address
== cpu_to_be32(XFS_DIR2_NULL_DATAPTR
))
177 if (hdr
->stale
!= stale
)
183 xfs_dir3_leaf_verify(
187 struct xfs_mount
*mp
= bp
->b_target
->bt_mount
;
188 struct xfs_dir2_leaf
*leaf
= bp
->b_addr
;
189 struct xfs_dir3_icleaf_hdr leafhdr
;
191 ASSERT(magic
== XFS_DIR2_LEAF1_MAGIC
|| magic
== XFS_DIR2_LEAFN_MAGIC
);
193 xfs_dir3_leaf_hdr_from_disk(&leafhdr
, leaf
);
194 if (xfs_sb_version_hascrc(&mp
->m_sb
)) {
195 struct xfs_dir3_leaf_hdr
*leaf3
= bp
->b_addr
;
197 if ((magic
== XFS_DIR2_LEAF1_MAGIC
&&
198 leafhdr
.magic
!= XFS_DIR3_LEAF1_MAGIC
) ||
199 (magic
== XFS_DIR2_LEAFN_MAGIC
&&
200 leafhdr
.magic
!= XFS_DIR3_LEAFN_MAGIC
))
203 if (!uuid_equal(&leaf3
->info
.uuid
, &mp
->m_sb
.sb_uuid
))
205 if (be64_to_cpu(leaf3
->info
.blkno
) != bp
->b_bn
)
208 if (leafhdr
.magic
!= magic
)
211 return xfs_dir3_leaf_check_int(mp
, &leafhdr
, leaf
);
219 struct xfs_mount
*mp
= bp
->b_target
->bt_mount
;
221 if ((xfs_sb_version_hascrc(&mp
->m_sb
) &&
222 !xfs_verify_cksum(bp
->b_addr
, BBTOB(bp
->b_length
),
223 XFS_DIR3_LEAF_CRC_OFF
)) ||
224 !xfs_dir3_leaf_verify(bp
, magic
)) {
225 XFS_CORRUPTION_ERROR(__func__
, XFS_ERRLEVEL_LOW
, mp
, bp
->b_addr
);
226 xfs_buf_ioerror(bp
, EFSCORRUPTED
);
235 struct xfs_mount
*mp
= bp
->b_target
->bt_mount
;
236 struct xfs_buf_log_item
*bip
= bp
->b_fspriv
;
237 struct xfs_dir3_leaf_hdr
*hdr3
= bp
->b_addr
;
239 if (!xfs_dir3_leaf_verify(bp
, magic
)) {
240 XFS_CORRUPTION_ERROR(__func__
, XFS_ERRLEVEL_LOW
, mp
, bp
->b_addr
);
241 xfs_buf_ioerror(bp
, EFSCORRUPTED
);
245 if (!xfs_sb_version_hascrc(&mp
->m_sb
))
249 hdr3
->info
.lsn
= cpu_to_be64(bip
->bli_item
.li_lsn
);
251 xfs_update_cksum(bp
->b_addr
, BBTOB(bp
->b_length
), XFS_DIR3_LEAF_CRC_OFF
);
255 xfs_dir3_leaf1_read_verify(
258 __read_verify(bp
, XFS_DIR2_LEAF1_MAGIC
);
262 xfs_dir3_leaf1_write_verify(
265 __write_verify(bp
, XFS_DIR2_LEAF1_MAGIC
);
269 xfs_dir3_leafn_read_verify(
272 __read_verify(bp
, XFS_DIR2_LEAFN_MAGIC
);
276 xfs_dir3_leafn_write_verify(
279 __write_verify(bp
, XFS_DIR2_LEAFN_MAGIC
);
282 const struct xfs_buf_ops xfs_dir3_leaf1_buf_ops
= {
283 .verify_read
= xfs_dir3_leaf1_read_verify
,
284 .verify_write
= xfs_dir3_leaf1_write_verify
,
287 const struct xfs_buf_ops xfs_dir3_leafn_buf_ops
= {
288 .verify_read
= xfs_dir3_leafn_read_verify
,
289 .verify_write
= xfs_dir3_leafn_write_verify
,
294 struct xfs_trans
*tp
,
295 struct xfs_inode
*dp
,
297 xfs_daddr_t mappedbno
,
298 struct xfs_buf
**bpp
)
302 err
= xfs_da_read_buf(tp
, dp
, fbno
, mappedbno
, bpp
,
303 XFS_DATA_FORK
, &xfs_dir3_leaf1_buf_ops
);
305 xfs_trans_buf_set_type(tp
, *bpp
, XFS_BLFT_DIR_LEAF1_BUF
);
311 struct xfs_trans
*tp
,
312 struct xfs_inode
*dp
,
314 xfs_daddr_t mappedbno
,
315 struct xfs_buf
**bpp
)
319 err
= xfs_da_read_buf(tp
, dp
, fbno
, mappedbno
, bpp
,
320 XFS_DATA_FORK
, &xfs_dir3_leafn_buf_ops
);
322 xfs_trans_buf_set_type(tp
, *bpp
, XFS_BLFT_DIR_LEAFN_BUF
);
327 * Initialize a new leaf block, leaf1 or leafn magic accepted.
331 struct xfs_mount
*mp
,
332 struct xfs_trans
*tp
,
337 struct xfs_dir2_leaf
*leaf
= bp
->b_addr
;
339 ASSERT(type
== XFS_DIR2_LEAF1_MAGIC
|| type
== XFS_DIR2_LEAFN_MAGIC
);
341 if (xfs_sb_version_hascrc(&mp
->m_sb
)) {
342 struct xfs_dir3_leaf_hdr
*leaf3
= bp
->b_addr
;
344 memset(leaf3
, 0, sizeof(*leaf3
));
346 leaf3
->info
.hdr
.magic
= (type
== XFS_DIR2_LEAF1_MAGIC
)
347 ? cpu_to_be16(XFS_DIR3_LEAF1_MAGIC
)
348 : cpu_to_be16(XFS_DIR3_LEAFN_MAGIC
);
349 leaf3
->info
.blkno
= cpu_to_be64(bp
->b_bn
);
350 leaf3
->info
.owner
= cpu_to_be64(owner
);
351 uuid_copy(&leaf3
->info
.uuid
, &mp
->m_sb
.sb_uuid
);
353 memset(leaf
, 0, sizeof(*leaf
));
354 leaf
->hdr
.info
.magic
= cpu_to_be16(type
);
358 * If it's a leaf-format directory initialize the tail.
359 * Caller is responsible for initialising the bests table.
361 if (type
== XFS_DIR2_LEAF1_MAGIC
) {
362 struct xfs_dir2_leaf_tail
*ltp
;
364 ltp
= xfs_dir2_leaf_tail_p(mp
, leaf
);
366 bp
->b_ops
= &xfs_dir3_leaf1_buf_ops
;
367 xfs_trans_buf_set_type(tp
, bp
, XFS_BLFT_DIR_LEAF1_BUF
);
369 bp
->b_ops
= &xfs_dir3_leafn_buf_ops
;
370 xfs_trans_buf_set_type(tp
, bp
, XFS_BLFT_DIR_LEAFN_BUF
);
375 xfs_dir3_leaf_get_buf(
378 struct xfs_buf
**bpp
,
381 struct xfs_inode
*dp
= args
->dp
;
382 struct xfs_trans
*tp
= args
->trans
;
383 struct xfs_mount
*mp
= dp
->i_mount
;
387 ASSERT(magic
== XFS_DIR2_LEAF1_MAGIC
|| magic
== XFS_DIR2_LEAFN_MAGIC
);
388 ASSERT(bno
>= XFS_DIR2_LEAF_FIRSTDB(mp
) &&
389 bno
< XFS_DIR2_FREE_FIRSTDB(mp
));
391 error
= xfs_da_get_buf(tp
, dp
, xfs_dir2_db_to_da(mp
, bno
), -1, &bp
,
396 xfs_dir3_leaf_init(mp
, tp
, bp
, dp
->i_ino
, magic
);
397 xfs_dir3_leaf_log_header(tp
, bp
);
398 if (magic
== XFS_DIR2_LEAF1_MAGIC
)
399 xfs_dir3_leaf_log_tail(tp
, bp
);
405 * Convert a block form directory to a leaf form directory.
408 xfs_dir2_block_to_leaf(
409 xfs_da_args_t
*args
, /* operation arguments */
410 struct xfs_buf
*dbp
) /* input block's buffer */
412 __be16
*bestsp
; /* leaf's bestsp entries */
413 xfs_dablk_t blkno
; /* leaf block's bno */
414 xfs_dir2_data_hdr_t
*hdr
; /* block header */
415 xfs_dir2_leaf_entry_t
*blp
; /* block's leaf entries */
416 xfs_dir2_block_tail_t
*btp
; /* block's tail */
417 xfs_inode_t
*dp
; /* incore directory inode */
418 int error
; /* error return code */
419 struct xfs_buf
*lbp
; /* leaf block's buffer */
420 xfs_dir2_db_t ldb
; /* leaf block's bno */
421 xfs_dir2_leaf_t
*leaf
; /* leaf structure */
422 xfs_dir2_leaf_tail_t
*ltp
; /* leaf's tail */
423 xfs_mount_t
*mp
; /* filesystem mount point */
424 int needlog
; /* need to log block header */
425 int needscan
; /* need to rescan bestfree */
426 xfs_trans_t
*tp
; /* transaction pointer */
427 struct xfs_dir2_data_free
*bf
;
428 struct xfs_dir2_leaf_entry
*ents
;
429 struct xfs_dir3_icleaf_hdr leafhdr
;
431 trace_xfs_dir2_block_to_leaf(args
);
437 * Add the leaf block to the inode.
438 * This interface will only put blocks in the leaf/node range.
439 * Since that's empty now, we'll get the root (block 0 in range).
441 if ((error
= xfs_da_grow_inode(args
, &blkno
))) {
444 ldb
= xfs_dir2_da_to_db(mp
, blkno
);
445 ASSERT(ldb
== XFS_DIR2_LEAF_FIRSTDB(mp
));
447 * Initialize the leaf block, get a buffer for it.
449 error
= xfs_dir3_leaf_get_buf(args
, ldb
, &lbp
, XFS_DIR2_LEAF1_MAGIC
);
455 xfs_dir3_data_check(dp
, dbp
);
456 btp
= xfs_dir2_block_tail_p(mp
, hdr
);
457 blp
= xfs_dir2_block_leaf_p(btp
);
458 bf
= xfs_dir3_data_bestfree_p(hdr
);
459 ents
= xfs_dir3_leaf_ents_p(leaf
);
462 * Set the counts in the leaf header.
464 xfs_dir3_leaf_hdr_from_disk(&leafhdr
, leaf
);
465 leafhdr
.count
= be32_to_cpu(btp
->count
);
466 leafhdr
.stale
= be32_to_cpu(btp
->stale
);
467 xfs_dir3_leaf_hdr_to_disk(leaf
, &leafhdr
);
468 xfs_dir3_leaf_log_header(tp
, lbp
);
471 * Could compact these but I think we always do the conversion
472 * after squeezing out stale entries.
474 memcpy(ents
, blp
, be32_to_cpu(btp
->count
) * sizeof(xfs_dir2_leaf_entry_t
));
475 xfs_dir3_leaf_log_ents(tp
, lbp
, 0, leafhdr
.count
- 1);
479 * Make the space formerly occupied by the leaf entries and block
482 xfs_dir2_data_make_free(tp
, dbp
,
483 (xfs_dir2_data_aoff_t
)((char *)blp
- (char *)hdr
),
484 (xfs_dir2_data_aoff_t
)((char *)hdr
+ mp
->m_dirblksize
-
486 &needlog
, &needscan
);
488 * Fix up the block header, make it a data block.
490 dbp
->b_ops
= &xfs_dir3_data_buf_ops
;
491 xfs_trans_buf_set_type(tp
, dbp
, XFS_BLFT_DIR_DATA_BUF
);
492 if (hdr
->magic
== cpu_to_be32(XFS_DIR2_BLOCK_MAGIC
))
493 hdr
->magic
= cpu_to_be32(XFS_DIR2_DATA_MAGIC
);
495 hdr
->magic
= cpu_to_be32(XFS_DIR3_DATA_MAGIC
);
498 xfs_dir2_data_freescan(mp
, hdr
, &needlog
);
500 * Set up leaf tail and bests table.
502 ltp
= xfs_dir2_leaf_tail_p(mp
, leaf
);
503 ltp
->bestcount
= cpu_to_be32(1);
504 bestsp
= xfs_dir2_leaf_bests_p(ltp
);
505 bestsp
[0] = bf
[0].length
;
507 * Log the data header and leaf bests table.
510 xfs_dir2_data_log_header(tp
, dbp
);
511 xfs_dir3_leaf_check(mp
, lbp
);
512 xfs_dir3_data_check(dp
, dbp
);
513 xfs_dir3_leaf_log_bests(tp
, lbp
, 0, 0);
518 xfs_dir3_leaf_find_stale(
519 struct xfs_dir3_icleaf_hdr
*leafhdr
,
520 struct xfs_dir2_leaf_entry
*ents
,
526 * Find the first stale entry before our index, if any.
528 for (*lowstale
= index
- 1; *lowstale
>= 0; --*lowstale
) {
529 if (ents
[*lowstale
].address
==
530 cpu_to_be32(XFS_DIR2_NULL_DATAPTR
))
535 * Find the first stale entry at or after our index, if any.
536 * Stop if the result would require moving more entries than using
539 for (*highstale
= index
; *highstale
< leafhdr
->count
; ++*highstale
) {
540 if (ents
[*highstale
].address
==
541 cpu_to_be32(XFS_DIR2_NULL_DATAPTR
))
543 if (*lowstale
>= 0 && index
- *lowstale
<= *highstale
- index
)
548 struct xfs_dir2_leaf_entry
*
549 xfs_dir3_leaf_find_entry(
550 struct xfs_dir3_icleaf_hdr
*leafhdr
,
551 struct xfs_dir2_leaf_entry
*ents
,
552 int index
, /* leaf table position */
553 int compact
, /* need to compact leaves */
554 int lowstale
, /* index of prev stale leaf */
555 int highstale
, /* index of next stale leaf */
556 int *lfloglow
, /* low leaf logging index */
557 int *lfloghigh
) /* high leaf logging index */
559 if (!leafhdr
->stale
) {
560 xfs_dir2_leaf_entry_t
*lep
; /* leaf entry table pointer */
563 * Now we need to make room to insert the leaf entry.
565 * If there are no stale entries, just insert a hole at index.
568 if (index
< leafhdr
->count
)
569 memmove(lep
+ 1, lep
,
570 (leafhdr
->count
- index
) * sizeof(*lep
));
573 * Record low and high logging indices for the leaf.
576 *lfloghigh
= leafhdr
->count
++;
581 * There are stale entries.
583 * We will use one of them for the new entry. It's probably not at
584 * the right location, so we'll have to shift some up or down first.
586 * If we didn't compact before, we need to find the nearest stale
587 * entries before and after our insertion point.
590 xfs_dir3_leaf_find_stale(leafhdr
, ents
, index
,
591 &lowstale
, &highstale
);
594 * If the low one is better, use it.
597 (highstale
== leafhdr
->count
||
598 index
- lowstale
- 1 < highstale
- index
)) {
599 ASSERT(index
- lowstale
- 1 >= 0);
600 ASSERT(ents
[lowstale
].address
==
601 cpu_to_be32(XFS_DIR2_NULL_DATAPTR
));
604 * Copy entries up to cover the stale entry and make room
607 if (index
- lowstale
- 1 > 0) {
608 memmove(&ents
[lowstale
], &ents
[lowstale
+ 1],
609 (index
- lowstale
- 1) *
610 sizeof(xfs_dir2_leaf_entry_t
));
612 *lfloglow
= MIN(lowstale
, *lfloglow
);
613 *lfloghigh
= MAX(index
- 1, *lfloghigh
);
615 return &ents
[index
- 1];
619 * The high one is better, so use that one.
621 ASSERT(highstale
- index
>= 0);
622 ASSERT(ents
[highstale
].address
== cpu_to_be32(XFS_DIR2_NULL_DATAPTR
));
625 * Copy entries down to cover the stale entry and make room for the
628 if (highstale
- index
> 0) {
629 memmove(&ents
[index
+ 1], &ents
[index
],
630 (highstale
- index
) * sizeof(xfs_dir2_leaf_entry_t
));
632 *lfloglow
= MIN(index
, *lfloglow
);
633 *lfloghigh
= MAX(highstale
, *lfloghigh
);
639 * Add an entry to a leaf form directory.
642 xfs_dir2_leaf_addname(
643 xfs_da_args_t
*args
) /* operation arguments */
645 __be16
*bestsp
; /* freespace table in leaf */
646 int compact
; /* need to compact leaves */
647 xfs_dir2_data_hdr_t
*hdr
; /* data block header */
648 struct xfs_buf
*dbp
; /* data block buffer */
649 xfs_dir2_data_entry_t
*dep
; /* data block entry */
650 xfs_inode_t
*dp
; /* incore directory inode */
651 xfs_dir2_data_unused_t
*dup
; /* data unused entry */
652 int error
; /* error return value */
653 int grown
; /* allocated new data block */
654 int highstale
; /* index of next stale leaf */
655 int i
; /* temporary, index */
656 int index
; /* leaf table position */
657 struct xfs_buf
*lbp
; /* leaf's buffer */
658 xfs_dir2_leaf_t
*leaf
; /* leaf structure */
659 int length
; /* length of new entry */
660 xfs_dir2_leaf_entry_t
*lep
; /* leaf entry table pointer */
661 int lfloglow
; /* low leaf logging index */
662 int lfloghigh
; /* high leaf logging index */
663 int lowstale
; /* index of prev stale leaf */
664 xfs_dir2_leaf_tail_t
*ltp
; /* leaf tail pointer */
665 xfs_mount_t
*mp
; /* filesystem mount point */
666 int needbytes
; /* leaf block bytes needed */
667 int needlog
; /* need to log data header */
668 int needscan
; /* need to rescan data free */
669 __be16
*tagp
; /* end of data entry */
670 xfs_trans_t
*tp
; /* transaction pointer */
671 xfs_dir2_db_t use_block
; /* data block number */
672 struct xfs_dir2_data_free
*bf
; /* bestfree table */
673 struct xfs_dir2_leaf_entry
*ents
;
674 struct xfs_dir3_icleaf_hdr leafhdr
;
676 trace_xfs_dir2_leaf_addname(args
);
682 error
= xfs_dir3_leaf_read(tp
, dp
, mp
->m_dirleafblk
, -1, &lbp
);
687 * Look up the entry by hash value and name.
688 * We know it's not there, our caller has already done a lookup.
689 * So the index is of the entry to insert in front of.
690 * But if there are dup hash values the index is of the first of those.
692 index
= xfs_dir2_leaf_search_hash(args
, lbp
);
694 ltp
= xfs_dir2_leaf_tail_p(mp
, leaf
);
695 ents
= xfs_dir3_leaf_ents_p(leaf
);
696 xfs_dir3_leaf_hdr_from_disk(&leafhdr
, leaf
);
697 bestsp
= xfs_dir2_leaf_bests_p(ltp
);
698 length
= xfs_dir2_data_entsize(args
->namelen
);
701 * See if there are any entries with the same hash value
702 * and space in their block for the new entry.
703 * This is good because it puts multiple same-hash value entries
704 * in a data block, improving the lookup of those entries.
706 for (use_block
= -1, lep
= &ents
[index
];
707 index
< leafhdr
.count
&& be32_to_cpu(lep
->hashval
) == args
->hashval
;
709 if (be32_to_cpu(lep
->address
) == XFS_DIR2_NULL_DATAPTR
)
711 i
= xfs_dir2_dataptr_to_db(mp
, be32_to_cpu(lep
->address
));
712 ASSERT(i
< be32_to_cpu(ltp
->bestcount
));
713 ASSERT(bestsp
[i
] != cpu_to_be16(NULLDATAOFF
));
714 if (be16_to_cpu(bestsp
[i
]) >= length
) {
720 * Didn't find a block yet, linear search all the data blocks.
722 if (use_block
== -1) {
723 for (i
= 0; i
< be32_to_cpu(ltp
->bestcount
); i
++) {
725 * Remember a block we see that's missing.
727 if (bestsp
[i
] == cpu_to_be16(NULLDATAOFF
) &&
730 else if (be16_to_cpu(bestsp
[i
]) >= length
) {
737 * How many bytes do we need in the leaf block?
741 needbytes
+= sizeof(xfs_dir2_leaf_entry_t
);
743 needbytes
+= sizeof(xfs_dir2_data_off_t
);
746 * Now kill use_block if it refers to a missing block, so we
747 * can use it as an indication of allocation needed.
749 if (use_block
!= -1 && bestsp
[use_block
] == cpu_to_be16(NULLDATAOFF
))
752 * If we don't have enough free bytes but we can make enough
753 * by compacting out stale entries, we'll do that.
755 if ((char *)bestsp
- (char *)&ents
[leafhdr
.count
] < needbytes
&&
760 * Otherwise if we don't have enough free bytes we need to
761 * convert to node form.
763 else if ((char *)bestsp
- (char *)&ents
[leafhdr
.count
] < needbytes
) {
765 * Just checking or no space reservation, give up.
767 if ((args
->op_flags
& XFS_DA_OP_JUSTCHECK
) ||
769 xfs_trans_brelse(tp
, lbp
);
770 return XFS_ERROR(ENOSPC
);
773 * Convert to node form.
775 error
= xfs_dir2_leaf_to_node(args
, lbp
);
779 * Then add the new entry.
781 return xfs_dir2_node_addname(args
);
784 * Otherwise it will fit without compaction.
789 * If just checking, then it will fit unless we needed to allocate
792 if (args
->op_flags
& XFS_DA_OP_JUSTCHECK
) {
793 xfs_trans_brelse(tp
, lbp
);
794 return use_block
== -1 ? XFS_ERROR(ENOSPC
) : 0;
797 * If no allocations are allowed, return now before we've
800 if (args
->total
== 0 && use_block
== -1) {
801 xfs_trans_brelse(tp
, lbp
);
802 return XFS_ERROR(ENOSPC
);
805 * Need to compact the leaf entries, removing stale ones.
806 * Leave one stale entry behind - the one closest to our
807 * insertion index - and we'll shift that one to our insertion
811 xfs_dir3_leaf_compact_x1(&leafhdr
, ents
, &index
, &lowstale
,
812 &highstale
, &lfloglow
, &lfloghigh
);
815 * There are stale entries, so we'll need log-low and log-high
816 * impossibly bad values later.
818 else if (leafhdr
.stale
) {
819 lfloglow
= leafhdr
.count
;
823 * If there was no data block space found, we need to allocate
826 if (use_block
== -1) {
828 * Add the new data block.
830 if ((error
= xfs_dir2_grow_inode(args
, XFS_DIR2_DATA_SPACE
,
832 xfs_trans_brelse(tp
, lbp
);
836 * Initialize the block.
838 if ((error
= xfs_dir3_data_init(args
, use_block
, &dbp
))) {
839 xfs_trans_brelse(tp
, lbp
);
843 * If we're adding a new data block on the end we need to
844 * extend the bests table. Copy it up one entry.
846 if (use_block
>= be32_to_cpu(ltp
->bestcount
)) {
848 memmove(&bestsp
[0], &bestsp
[1],
849 be32_to_cpu(ltp
->bestcount
) * sizeof(bestsp
[0]));
850 be32_add_cpu(<p
->bestcount
, 1);
851 xfs_dir3_leaf_log_tail(tp
, lbp
);
852 xfs_dir3_leaf_log_bests(tp
, lbp
, 0, be32_to_cpu(ltp
->bestcount
) - 1);
855 * If we're filling in a previously empty block just log it.
858 xfs_dir3_leaf_log_bests(tp
, lbp
, use_block
, use_block
);
860 bf
= xfs_dir3_data_bestfree_p(hdr
);
861 bestsp
[use_block
] = bf
[0].length
;
865 * Already had space in some data block.
866 * Just read that one in.
868 error
= xfs_dir3_data_read(tp
, dp
,
869 xfs_dir2_db_to_da(mp
, use_block
),
872 xfs_trans_brelse(tp
, lbp
);
876 bf
= xfs_dir3_data_bestfree_p(hdr
);
880 * Point to the biggest freespace in our data block.
882 dup
= (xfs_dir2_data_unused_t
*)
883 ((char *)hdr
+ be16_to_cpu(bf
[0].offset
));
884 ASSERT(be16_to_cpu(dup
->length
) >= length
);
885 needscan
= needlog
= 0;
887 * Mark the initial part of our freespace in use for the new entry.
889 xfs_dir2_data_use_free(tp
, dbp
, dup
,
890 (xfs_dir2_data_aoff_t
)((char *)dup
- (char *)hdr
), length
,
891 &needlog
, &needscan
);
893 * Initialize our new entry (at last).
895 dep
= (xfs_dir2_data_entry_t
*)dup
;
896 dep
->inumber
= cpu_to_be64(args
->inumber
);
897 dep
->namelen
= args
->namelen
;
898 memcpy(dep
->name
, args
->name
, dep
->namelen
);
899 tagp
= xfs_dir2_data_entry_tag_p(dep
);
900 *tagp
= cpu_to_be16((char *)dep
- (char *)hdr
);
902 * Need to scan fix up the bestfree table.
905 xfs_dir2_data_freescan(mp
, hdr
, &needlog
);
907 * Need to log the data block's header.
910 xfs_dir2_data_log_header(tp
, dbp
);
911 xfs_dir2_data_log_entry(tp
, dbp
, dep
);
913 * If the bests table needs to be changed, do it.
914 * Log the change unless we've already done that.
916 if (be16_to_cpu(bestsp
[use_block
]) != be16_to_cpu(bf
[0].length
)) {
917 bestsp
[use_block
] = bf
[0].length
;
919 xfs_dir3_leaf_log_bests(tp
, lbp
, use_block
, use_block
);
922 lep
= xfs_dir3_leaf_find_entry(&leafhdr
, ents
, index
, compact
, lowstale
,
923 highstale
, &lfloglow
, &lfloghigh
);
926 * Fill in the new leaf entry.
928 lep
->hashval
= cpu_to_be32(args
->hashval
);
929 lep
->address
= cpu_to_be32(xfs_dir2_db_off_to_dataptr(mp
, use_block
,
930 be16_to_cpu(*tagp
)));
932 * Log the leaf fields and give up the buffers.
934 xfs_dir3_leaf_hdr_to_disk(leaf
, &leafhdr
);
935 xfs_dir3_leaf_log_header(tp
, lbp
);
936 xfs_dir3_leaf_log_ents(tp
, lbp
, lfloglow
, lfloghigh
);
937 xfs_dir3_leaf_check(mp
, lbp
);
938 xfs_dir3_data_check(dp
, dbp
);
943 * Compact out any stale entries in the leaf.
944 * Log the header and changed leaf entries, if any.
947 xfs_dir3_leaf_compact(
948 xfs_da_args_t
*args
, /* operation arguments */
949 struct xfs_dir3_icleaf_hdr
*leafhdr
,
950 struct xfs_buf
*bp
) /* leaf buffer */
952 int from
; /* source leaf index */
953 xfs_dir2_leaf_t
*leaf
; /* leaf structure */
954 int loglow
; /* first leaf entry to log */
955 int to
; /* target leaf index */
956 struct xfs_dir2_leaf_entry
*ents
;
963 * Compress out the stale entries in place.
965 ents
= xfs_dir3_leaf_ents_p(leaf
);
966 for (from
= to
= 0, loglow
= -1; from
< leafhdr
->count
; from
++) {
967 if (ents
[from
].address
== cpu_to_be32(XFS_DIR2_NULL_DATAPTR
))
970 * Only actually copy the entries that are different.
975 ents
[to
] = ents
[from
];
980 * Update and log the header, log the leaf entries.
982 ASSERT(leafhdr
->stale
== from
- to
);
983 leafhdr
->count
-= leafhdr
->stale
;
986 xfs_dir3_leaf_hdr_to_disk(leaf
, leafhdr
);
987 xfs_dir3_leaf_log_header(args
->trans
, bp
);
989 xfs_dir3_leaf_log_ents(args
->trans
, bp
, loglow
, to
- 1);
993 * Compact the leaf entries, removing stale ones.
994 * Leave one stale entry behind - the one closest to our
995 * insertion index - and the caller will shift that one to our insertion
997 * Return new insertion index, where the remaining stale entry is,
998 * and leaf logging indices.
1001 xfs_dir3_leaf_compact_x1(
1002 struct xfs_dir3_icleaf_hdr
*leafhdr
,
1003 struct xfs_dir2_leaf_entry
*ents
,
1004 int *indexp
, /* insertion index */
1005 int *lowstalep
, /* out: stale entry before us */
1006 int *highstalep
, /* out: stale entry after us */
1007 int *lowlogp
, /* out: low log index */
1008 int *highlogp
) /* out: high log index */
1010 int from
; /* source copy index */
1011 int highstale
; /* stale entry at/after index */
1012 int index
; /* insertion index */
1013 int keepstale
; /* source index of kept stale */
1014 int lowstale
; /* stale entry before index */
1015 int newindex
=0; /* new insertion index */
1016 int to
; /* destination copy index */
1018 ASSERT(leafhdr
->stale
> 1);
1021 xfs_dir3_leaf_find_stale(leafhdr
, ents
, index
, &lowstale
, &highstale
);
1024 * Pick the better of lowstale and highstale.
1026 if (lowstale
>= 0 &&
1027 (highstale
== leafhdr
->count
||
1028 index
- lowstale
<= highstale
- index
))
1029 keepstale
= lowstale
;
1031 keepstale
= highstale
;
1033 * Copy the entries in place, removing all the stale entries
1036 for (from
= to
= 0; from
< leafhdr
->count
; from
++) {
1038 * Notice the new value of index.
1042 if (from
!= keepstale
&&
1043 ents
[from
].address
== cpu_to_be32(XFS_DIR2_NULL_DATAPTR
)) {
1049 * Record the new keepstale value for the insertion.
1051 if (from
== keepstale
)
1052 lowstale
= highstale
= to
;
1054 * Copy only the entries that have moved.
1057 ents
[to
] = ents
[from
];
1062 * If the insertion point was past the last entry,
1063 * set the new insertion point accordingly.
1069 * Adjust the leaf header values.
1071 leafhdr
->count
-= from
- to
;
1074 * Remember the low/high stale value only in the "right"
1077 if (lowstale
>= newindex
)
1080 highstale
= leafhdr
->count
;
1081 *highlogp
= leafhdr
->count
- 1;
1082 *lowstalep
= lowstale
;
1083 *highstalep
= highstale
;
1087 * Log the bests entries indicated from a leaf1 block.
1090 xfs_dir3_leaf_log_bests(
1091 xfs_trans_t
*tp
, /* transaction pointer */
1092 struct xfs_buf
*bp
, /* leaf buffer */
1093 int first
, /* first entry to log */
1094 int last
) /* last entry to log */
1096 __be16
*firstb
; /* pointer to first entry */
1097 __be16
*lastb
; /* pointer to last entry */
1098 struct xfs_dir2_leaf
*leaf
= bp
->b_addr
;
1099 xfs_dir2_leaf_tail_t
*ltp
; /* leaf tail structure */
1101 ASSERT(leaf
->hdr
.info
.magic
== cpu_to_be16(XFS_DIR2_LEAF1_MAGIC
) ||
1102 leaf
->hdr
.info
.magic
== cpu_to_be16(XFS_DIR3_LEAF1_MAGIC
));
1104 ltp
= xfs_dir2_leaf_tail_p(tp
->t_mountp
, leaf
);
1105 firstb
= xfs_dir2_leaf_bests_p(ltp
) + first
;
1106 lastb
= xfs_dir2_leaf_bests_p(ltp
) + last
;
1107 xfs_trans_log_buf(tp
, bp
, (uint
)((char *)firstb
- (char *)leaf
),
1108 (uint
)((char *)lastb
- (char *)leaf
+ sizeof(*lastb
) - 1));
1112 * Log the leaf entries indicated from a leaf1 or leafn block.
1115 xfs_dir3_leaf_log_ents(
1116 xfs_trans_t
*tp
, /* transaction pointer */
1117 struct xfs_buf
*bp
, /* leaf buffer */
1118 int first
, /* first entry to log */
1119 int last
) /* last entry to log */
1121 xfs_dir2_leaf_entry_t
*firstlep
; /* pointer to first entry */
1122 xfs_dir2_leaf_entry_t
*lastlep
; /* pointer to last entry */
1123 struct xfs_dir2_leaf
*leaf
= bp
->b_addr
;
1124 struct xfs_dir2_leaf_entry
*ents
;
1126 ASSERT(leaf
->hdr
.info
.magic
== cpu_to_be16(XFS_DIR2_LEAF1_MAGIC
) ||
1127 leaf
->hdr
.info
.magic
== cpu_to_be16(XFS_DIR3_LEAF1_MAGIC
) ||
1128 leaf
->hdr
.info
.magic
== cpu_to_be16(XFS_DIR2_LEAFN_MAGIC
) ||
1129 leaf
->hdr
.info
.magic
== cpu_to_be16(XFS_DIR3_LEAFN_MAGIC
));
1131 ents
= xfs_dir3_leaf_ents_p(leaf
);
1132 firstlep
= &ents
[first
];
1133 lastlep
= &ents
[last
];
1134 xfs_trans_log_buf(tp
, bp
, (uint
)((char *)firstlep
- (char *)leaf
),
1135 (uint
)((char *)lastlep
- (char *)leaf
+ sizeof(*lastlep
) - 1));
1139 * Log the header of the leaf1 or leafn block.
1142 xfs_dir3_leaf_log_header(
1143 struct xfs_trans
*tp
,
1146 struct xfs_dir2_leaf
*leaf
= bp
->b_addr
;
1148 ASSERT(leaf
->hdr
.info
.magic
== cpu_to_be16(XFS_DIR2_LEAF1_MAGIC
) ||
1149 leaf
->hdr
.info
.magic
== cpu_to_be16(XFS_DIR3_LEAF1_MAGIC
) ||
1150 leaf
->hdr
.info
.magic
== cpu_to_be16(XFS_DIR2_LEAFN_MAGIC
) ||
1151 leaf
->hdr
.info
.magic
== cpu_to_be16(XFS_DIR3_LEAFN_MAGIC
));
1153 xfs_trans_log_buf(tp
, bp
, (uint
)((char *)&leaf
->hdr
- (char *)leaf
),
1154 xfs_dir3_leaf_hdr_size(leaf
) - 1);
1158 * Log the tail of the leaf1 block.
1161 xfs_dir3_leaf_log_tail(
1162 struct xfs_trans
*tp
,
1165 struct xfs_dir2_leaf
*leaf
= bp
->b_addr
;
1166 xfs_dir2_leaf_tail_t
*ltp
; /* leaf tail structure */
1167 struct xfs_mount
*mp
= tp
->t_mountp
;
1169 ASSERT(leaf
->hdr
.info
.magic
== cpu_to_be16(XFS_DIR2_LEAF1_MAGIC
) ||
1170 leaf
->hdr
.info
.magic
== cpu_to_be16(XFS_DIR3_LEAF1_MAGIC
) ||
1171 leaf
->hdr
.info
.magic
== cpu_to_be16(XFS_DIR2_LEAFN_MAGIC
) ||
1172 leaf
->hdr
.info
.magic
== cpu_to_be16(XFS_DIR3_LEAFN_MAGIC
));
1174 ltp
= xfs_dir2_leaf_tail_p(mp
, leaf
);
1175 xfs_trans_log_buf(tp
, bp
, (uint
)((char *)ltp
- (char *)leaf
),
1176 (uint
)(mp
->m_dirblksize
- 1));
1180 * Look up the entry referred to by args in the leaf format directory.
1181 * Most of the work is done by the xfs_dir2_leaf_lookup_int routine which
1182 * is also used by the node-format code.
1185 xfs_dir2_leaf_lookup(
1186 xfs_da_args_t
*args
) /* operation arguments */
1188 struct xfs_buf
*dbp
; /* data block buffer */
1189 xfs_dir2_data_entry_t
*dep
; /* data block entry */
1190 xfs_inode_t
*dp
; /* incore directory inode */
1191 int error
; /* error return code */
1192 int index
; /* found entry index */
1193 struct xfs_buf
*lbp
; /* leaf buffer */
1194 xfs_dir2_leaf_t
*leaf
; /* leaf structure */
1195 xfs_dir2_leaf_entry_t
*lep
; /* leaf entry */
1196 xfs_trans_t
*tp
; /* transaction pointer */
1197 struct xfs_dir2_leaf_entry
*ents
;
1199 trace_xfs_dir2_leaf_lookup(args
);
1202 * Look up name in the leaf block, returning both buffers and index.
1204 if ((error
= xfs_dir2_leaf_lookup_int(args
, &lbp
, &index
, &dbp
))) {
1209 xfs_dir3_leaf_check(dp
->i_mount
, lbp
);
1211 ents
= xfs_dir3_leaf_ents_p(leaf
);
1213 * Get to the leaf entry and contained data entry address.
1218 * Point to the data entry.
1220 dep
= (xfs_dir2_data_entry_t
*)
1221 ((char *)dbp
->b_addr
+
1222 xfs_dir2_dataptr_to_off(dp
->i_mount
, be32_to_cpu(lep
->address
)));
1224 * Return the found inode number & CI name if appropriate
1226 args
->inumber
= be64_to_cpu(dep
->inumber
);
1227 error
= xfs_dir_cilookup_result(args
, dep
->name
, dep
->namelen
);
1228 xfs_trans_brelse(tp
, dbp
);
1229 xfs_trans_brelse(tp
, lbp
);
1230 return XFS_ERROR(error
);
1234 * Look up name/hash in the leaf block.
1235 * Fill in indexp with the found index, and dbpp with the data buffer.
1236 * If not found dbpp will be NULL, and ENOENT comes back.
1237 * lbpp will always be filled in with the leaf buffer unless there's an error.
1239 static int /* error */
1240 xfs_dir2_leaf_lookup_int(
1241 xfs_da_args_t
*args
, /* operation arguments */
1242 struct xfs_buf
**lbpp
, /* out: leaf buffer */
1243 int *indexp
, /* out: index in leaf block */
1244 struct xfs_buf
**dbpp
) /* out: data buffer */
1246 xfs_dir2_db_t curdb
= -1; /* current data block number */
1247 struct xfs_buf
*dbp
= NULL
; /* data buffer */
1248 xfs_dir2_data_entry_t
*dep
; /* data entry */
1249 xfs_inode_t
*dp
; /* incore directory inode */
1250 int error
; /* error return code */
1251 int index
; /* index in leaf block */
1252 struct xfs_buf
*lbp
; /* leaf buffer */
1253 xfs_dir2_leaf_entry_t
*lep
; /* leaf entry */
1254 xfs_dir2_leaf_t
*leaf
; /* leaf structure */
1255 xfs_mount_t
*mp
; /* filesystem mount point */
1256 xfs_dir2_db_t newdb
; /* new data block number */
1257 xfs_trans_t
*tp
; /* transaction pointer */
1258 xfs_dir2_db_t cidb
= -1; /* case match data block no. */
1259 enum xfs_dacmp cmp
; /* name compare result */
1260 struct xfs_dir2_leaf_entry
*ents
;
1261 struct xfs_dir3_icleaf_hdr leafhdr
;
1267 error
= xfs_dir3_leaf_read(tp
, dp
, mp
->m_dirleafblk
, -1, &lbp
);
1273 xfs_dir3_leaf_check(mp
, lbp
);
1274 ents
= xfs_dir3_leaf_ents_p(leaf
);
1275 xfs_dir3_leaf_hdr_from_disk(&leafhdr
, leaf
);
1278 * Look for the first leaf entry with our hash value.
1280 index
= xfs_dir2_leaf_search_hash(args
, lbp
);
1282 * Loop over all the entries with the right hash value
1283 * looking to match the name.
1285 for (lep
= &ents
[index
];
1286 index
< leafhdr
.count
&& be32_to_cpu(lep
->hashval
) == args
->hashval
;
1289 * Skip over stale leaf entries.
1291 if (be32_to_cpu(lep
->address
) == XFS_DIR2_NULL_DATAPTR
)
1294 * Get the new data block number.
1296 newdb
= xfs_dir2_dataptr_to_db(mp
, be32_to_cpu(lep
->address
));
1298 * If it's not the same as the old data block number,
1299 * need to pitch the old one and read the new one.
1301 if (newdb
!= curdb
) {
1303 xfs_trans_brelse(tp
, dbp
);
1304 error
= xfs_dir3_data_read(tp
, dp
,
1305 xfs_dir2_db_to_da(mp
, newdb
),
1308 xfs_trans_brelse(tp
, lbp
);
1314 * Point to the data entry.
1316 dep
= (xfs_dir2_data_entry_t
*)((char *)dbp
->b_addr
+
1317 xfs_dir2_dataptr_to_off(mp
, be32_to_cpu(lep
->address
)));
1319 * Compare name and if it's an exact match, return the index
1320 * and buffer. If it's the first case-insensitive match, store
1321 * the index and buffer and continue looking for an exact match.
1323 cmp
= mp
->m_dirnameops
->compname(args
, dep
->name
, dep
->namelen
);
1324 if (cmp
!= XFS_CMP_DIFFERENT
&& cmp
!= args
->cmpresult
) {
1325 args
->cmpresult
= cmp
;
1327 /* case exact match: return the current buffer. */
1328 if (cmp
== XFS_CMP_EXACT
) {
1335 ASSERT(args
->op_flags
& XFS_DA_OP_OKNOENT
);
1337 * Here, we can only be doing a lookup (not a rename or remove).
1338 * If a case-insensitive match was found earlier, re-read the
1339 * appropriate data block if required and return it.
1341 if (args
->cmpresult
== XFS_CMP_CASE
) {
1343 if (cidb
!= curdb
) {
1344 xfs_trans_brelse(tp
, dbp
);
1345 error
= xfs_dir3_data_read(tp
, dp
,
1346 xfs_dir2_db_to_da(mp
, cidb
),
1349 xfs_trans_brelse(tp
, lbp
);
1357 * No match found, return ENOENT.
1361 xfs_trans_brelse(tp
, dbp
);
1362 xfs_trans_brelse(tp
, lbp
);
1363 return XFS_ERROR(ENOENT
);
1367 * Remove an entry from a leaf format directory.
1370 xfs_dir2_leaf_removename(
1371 xfs_da_args_t
*args
) /* operation arguments */
1373 __be16
*bestsp
; /* leaf block best freespace */
1374 xfs_dir2_data_hdr_t
*hdr
; /* data block header */
1375 xfs_dir2_db_t db
; /* data block number */
1376 struct xfs_buf
*dbp
; /* data block buffer */
1377 xfs_dir2_data_entry_t
*dep
; /* data entry structure */
1378 xfs_inode_t
*dp
; /* incore directory inode */
1379 int error
; /* error return code */
1380 xfs_dir2_db_t i
; /* temporary data block # */
1381 int index
; /* index into leaf entries */
1382 struct xfs_buf
*lbp
; /* leaf buffer */
1383 xfs_dir2_leaf_t
*leaf
; /* leaf structure */
1384 xfs_dir2_leaf_entry_t
*lep
; /* leaf entry */
1385 xfs_dir2_leaf_tail_t
*ltp
; /* leaf tail structure */
1386 xfs_mount_t
*mp
; /* filesystem mount point */
1387 int needlog
; /* need to log data header */
1388 int needscan
; /* need to rescan data frees */
1389 xfs_dir2_data_off_t oldbest
; /* old value of best free */
1390 xfs_trans_t
*tp
; /* transaction pointer */
1391 struct xfs_dir2_data_free
*bf
; /* bestfree table */
1392 struct xfs_dir2_leaf_entry
*ents
;
1393 struct xfs_dir3_icleaf_hdr leafhdr
;
1395 trace_xfs_dir2_leaf_removename(args
);
1398 * Lookup the leaf entry, get the leaf and data blocks read in.
1400 if ((error
= xfs_dir2_leaf_lookup_int(args
, &lbp
, &index
, &dbp
))) {
1408 xfs_dir3_data_check(dp
, dbp
);
1409 bf
= xfs_dir3_data_bestfree_p(hdr
);
1410 xfs_dir3_leaf_hdr_from_disk(&leafhdr
, leaf
);
1411 ents
= xfs_dir3_leaf_ents_p(leaf
);
1413 * Point to the leaf entry, use that to point to the data entry.
1416 db
= xfs_dir2_dataptr_to_db(mp
, be32_to_cpu(lep
->address
));
1417 dep
= (xfs_dir2_data_entry_t
*)
1418 ((char *)hdr
+ xfs_dir2_dataptr_to_off(mp
, be32_to_cpu(lep
->address
)));
1419 needscan
= needlog
= 0;
1420 oldbest
= be16_to_cpu(bf
[0].length
);
1421 ltp
= xfs_dir2_leaf_tail_p(mp
, leaf
);
1422 bestsp
= xfs_dir2_leaf_bests_p(ltp
);
1423 ASSERT(be16_to_cpu(bestsp
[db
]) == oldbest
);
1425 * Mark the former data entry unused.
1427 xfs_dir2_data_make_free(tp
, dbp
,
1428 (xfs_dir2_data_aoff_t
)((char *)dep
- (char *)hdr
),
1429 xfs_dir2_data_entsize(dep
->namelen
), &needlog
, &needscan
);
1431 * We just mark the leaf entry stale by putting a null in it.
1434 xfs_dir3_leaf_hdr_to_disk(leaf
, &leafhdr
);
1435 xfs_dir3_leaf_log_header(tp
, lbp
);
1437 lep
->address
= cpu_to_be32(XFS_DIR2_NULL_DATAPTR
);
1438 xfs_dir3_leaf_log_ents(tp
, lbp
, index
, index
);
1441 * Scan the freespace in the data block again if necessary,
1442 * log the data block header if necessary.
1445 xfs_dir2_data_freescan(mp
, hdr
, &needlog
);
1447 xfs_dir2_data_log_header(tp
, dbp
);
1449 * If the longest freespace in the data block has changed,
1450 * put the new value in the bests table and log that.
1452 if (be16_to_cpu(bf
[0].length
) != oldbest
) {
1453 bestsp
[db
] = bf
[0].length
;
1454 xfs_dir3_leaf_log_bests(tp
, lbp
, db
, db
);
1456 xfs_dir3_data_check(dp
, dbp
);
1458 * If the data block is now empty then get rid of the data block.
1460 if (be16_to_cpu(bf
[0].length
) ==
1461 mp
->m_dirblksize
- xfs_dir3_data_entry_offset(hdr
)) {
1462 ASSERT(db
!= mp
->m_dirdatablk
);
1463 if ((error
= xfs_dir2_shrink_inode(args
, db
, dbp
))) {
1465 * Nope, can't get rid of it because it caused
1466 * allocation of a bmap btree block to do so.
1467 * Just go on, returning success, leaving the
1468 * empty block in place.
1470 if (error
== ENOSPC
&& args
->total
== 0)
1472 xfs_dir3_leaf_check(mp
, lbp
);
1477 * If this is the last data block then compact the
1478 * bests table by getting rid of entries.
1480 if (db
== be32_to_cpu(ltp
->bestcount
) - 1) {
1482 * Look for the last active entry (i).
1484 for (i
= db
- 1; i
> 0; i
--) {
1485 if (bestsp
[i
] != cpu_to_be16(NULLDATAOFF
))
1489 * Copy the table down so inactive entries at the
1492 memmove(&bestsp
[db
- i
], bestsp
,
1493 (be32_to_cpu(ltp
->bestcount
) - (db
- i
)) * sizeof(*bestsp
));
1494 be32_add_cpu(<p
->bestcount
, -(db
- i
));
1495 xfs_dir3_leaf_log_tail(tp
, lbp
);
1496 xfs_dir3_leaf_log_bests(tp
, lbp
, 0, be32_to_cpu(ltp
->bestcount
) - 1);
1498 bestsp
[db
] = cpu_to_be16(NULLDATAOFF
);
1501 * If the data block was not the first one, drop it.
1503 else if (db
!= mp
->m_dirdatablk
)
1506 xfs_dir3_leaf_check(mp
, lbp
);
1508 * See if we can convert to block form.
1510 return xfs_dir2_leaf_to_block(args
, lbp
, dbp
);
1514 * Replace the inode number in a leaf format directory entry.
1517 xfs_dir2_leaf_replace(
1518 xfs_da_args_t
*args
) /* operation arguments */
1520 struct xfs_buf
*dbp
; /* data block buffer */
1521 xfs_dir2_data_entry_t
*dep
; /* data block entry */
1522 xfs_inode_t
*dp
; /* incore directory inode */
1523 int error
; /* error return code */
1524 int index
; /* index of leaf entry */
1525 struct xfs_buf
*lbp
; /* leaf buffer */
1526 xfs_dir2_leaf_t
*leaf
; /* leaf structure */
1527 xfs_dir2_leaf_entry_t
*lep
; /* leaf entry */
1528 xfs_trans_t
*tp
; /* transaction pointer */
1529 struct xfs_dir2_leaf_entry
*ents
;
1531 trace_xfs_dir2_leaf_replace(args
);
1534 * Look up the entry.
1536 if ((error
= xfs_dir2_leaf_lookup_int(args
, &lbp
, &index
, &dbp
))) {
1541 ents
= xfs_dir3_leaf_ents_p(leaf
);
1543 * Point to the leaf entry, get data address from it.
1547 * Point to the data entry.
1549 dep
= (xfs_dir2_data_entry_t
*)
1550 ((char *)dbp
->b_addr
+
1551 xfs_dir2_dataptr_to_off(dp
->i_mount
, be32_to_cpu(lep
->address
)));
1552 ASSERT(args
->inumber
!= be64_to_cpu(dep
->inumber
));
1554 * Put the new inode number in, log it.
1556 dep
->inumber
= cpu_to_be64(args
->inumber
);
1558 xfs_dir2_data_log_entry(tp
, dbp
, dep
);
1559 xfs_dir3_leaf_check(dp
->i_mount
, lbp
);
1560 xfs_trans_brelse(tp
, lbp
);
1565 * Return index in the leaf block (lbp) which is either the first
1566 * one with this hash value, or if there are none, the insert point
1567 * for that hash value.
1569 int /* index value */
1570 xfs_dir2_leaf_search_hash(
1571 xfs_da_args_t
*args
, /* operation arguments */
1572 struct xfs_buf
*lbp
) /* leaf buffer */
1574 xfs_dahash_t hash
=0; /* hash from this entry */
1575 xfs_dahash_t hashwant
; /* hash value looking for */
1576 int high
; /* high leaf index */
1577 int low
; /* low leaf index */
1578 xfs_dir2_leaf_t
*leaf
; /* leaf structure */
1579 xfs_dir2_leaf_entry_t
*lep
; /* leaf entry */
1580 int mid
=0; /* current leaf index */
1581 struct xfs_dir2_leaf_entry
*ents
;
1582 struct xfs_dir3_icleaf_hdr leafhdr
;
1585 ents
= xfs_dir3_leaf_ents_p(leaf
);
1586 xfs_dir3_leaf_hdr_from_disk(&leafhdr
, leaf
);
1593 * Note, the table cannot be empty, so we have to go through the loop.
1594 * Binary search the leaf entries looking for our hash value.
1596 for (lep
= ents
, low
= 0, high
= leafhdr
.count
- 1,
1597 hashwant
= args
->hashval
;
1599 mid
= (low
+ high
) >> 1;
1600 if ((hash
= be32_to_cpu(lep
[mid
].hashval
)) == hashwant
)
1602 if (hash
< hashwant
)
1608 * Found one, back up through all the equal hash values.
1610 if (hash
== hashwant
) {
1611 while (mid
> 0 && be32_to_cpu(lep
[mid
- 1].hashval
) == hashwant
) {
1616 * Need to point to an entry higher than ours.
1618 else if (hash
< hashwant
)
1624 * Trim off a trailing data block. We know it's empty since the leaf
1625 * freespace table says so.
1628 xfs_dir2_leaf_trim_data(
1629 xfs_da_args_t
*args
, /* operation arguments */
1630 struct xfs_buf
*lbp
, /* leaf buffer */
1631 xfs_dir2_db_t db
) /* data block number */
1633 __be16
*bestsp
; /* leaf bests table */
1634 struct xfs_buf
*dbp
; /* data block buffer */
1635 xfs_inode_t
*dp
; /* incore directory inode */
1636 int error
; /* error return value */
1637 xfs_dir2_leaf_t
*leaf
; /* leaf structure */
1638 xfs_dir2_leaf_tail_t
*ltp
; /* leaf tail structure */
1639 xfs_mount_t
*mp
; /* filesystem mount point */
1640 xfs_trans_t
*tp
; /* transaction pointer */
1646 * Read the offending data block. We need its buffer.
1648 error
= xfs_dir3_data_read(tp
, dp
, xfs_dir2_db_to_da(mp
, db
), -1, &dbp
);
1653 ltp
= xfs_dir2_leaf_tail_p(mp
, leaf
);
1657 struct xfs_dir2_data_hdr
*hdr
= dbp
->b_addr
;
1658 struct xfs_dir2_data_free
*bf
= xfs_dir3_data_bestfree_p(hdr
);
1660 ASSERT(hdr
->magic
== cpu_to_be32(XFS_DIR2_DATA_MAGIC
) ||
1661 hdr
->magic
== cpu_to_be32(XFS_DIR3_DATA_MAGIC
));
1662 ASSERT(be16_to_cpu(bf
[0].length
) ==
1663 mp
->m_dirblksize
- xfs_dir3_data_entry_offset(hdr
));
1664 ASSERT(db
== be32_to_cpu(ltp
->bestcount
) - 1);
1669 * Get rid of the data block.
1671 if ((error
= xfs_dir2_shrink_inode(args
, db
, dbp
))) {
1672 ASSERT(error
!= ENOSPC
);
1673 xfs_trans_brelse(tp
, dbp
);
1677 * Eliminate the last bests entry from the table.
1679 bestsp
= xfs_dir2_leaf_bests_p(ltp
);
1680 be32_add_cpu(<p
->bestcount
, -1);
1681 memmove(&bestsp
[1], &bestsp
[0], be32_to_cpu(ltp
->bestcount
) * sizeof(*bestsp
));
1682 xfs_dir3_leaf_log_tail(tp
, lbp
);
1683 xfs_dir3_leaf_log_bests(tp
, lbp
, 0, be32_to_cpu(ltp
->bestcount
) - 1);
1687 static inline size_t
1689 struct xfs_dir3_icleaf_hdr
*hdr
,
1695 entries
= hdr
->count
- hdr
->stale
;
1696 if (hdr
->magic
== XFS_DIR2_LEAF1_MAGIC
||
1697 hdr
->magic
== XFS_DIR2_LEAFN_MAGIC
)
1698 hdrsize
= sizeof(struct xfs_dir2_leaf_hdr
);
1700 hdrsize
= sizeof(struct xfs_dir3_leaf_hdr
);
1702 return hdrsize
+ entries
* sizeof(xfs_dir2_leaf_entry_t
)
1703 + counts
* sizeof(xfs_dir2_data_off_t
)
1704 + sizeof(xfs_dir2_leaf_tail_t
);
1708 * Convert node form directory to leaf form directory.
1709 * The root of the node form dir needs to already be a LEAFN block.
1710 * Just return if we can't do anything.
1713 xfs_dir2_node_to_leaf(
1714 xfs_da_state_t
*state
) /* directory operation state */
1716 xfs_da_args_t
*args
; /* operation arguments */
1717 xfs_inode_t
*dp
; /* incore directory inode */
1718 int error
; /* error return code */
1719 struct xfs_buf
*fbp
; /* buffer for freespace block */
1720 xfs_fileoff_t fo
; /* freespace file offset */
1721 xfs_dir2_free_t
*free
; /* freespace structure */
1722 struct xfs_buf
*lbp
; /* buffer for leaf block */
1723 xfs_dir2_leaf_tail_t
*ltp
; /* tail of leaf structure */
1724 xfs_dir2_leaf_t
*leaf
; /* leaf structure */
1725 xfs_mount_t
*mp
; /* filesystem mount point */
1726 int rval
; /* successful free trim? */
1727 xfs_trans_t
*tp
; /* transaction pointer */
1728 struct xfs_dir3_icleaf_hdr leafhdr
;
1729 struct xfs_dir3_icfree_hdr freehdr
;
1732 * There's more than a leaf level in the btree, so there must
1733 * be multiple leafn blocks. Give up.
1735 if (state
->path
.active
> 1)
1739 trace_xfs_dir2_node_to_leaf(args
);
1745 * Get the last offset in the file.
1747 if ((error
= xfs_bmap_last_offset(tp
, dp
, &fo
, XFS_DATA_FORK
))) {
1750 fo
-= mp
->m_dirblkfsbs
;
1752 * If there are freespace blocks other than the first one,
1753 * take this opportunity to remove trailing empty freespace blocks
1754 * that may have been left behind during no-space-reservation
1757 while (fo
> mp
->m_dirfreeblk
) {
1758 if ((error
= xfs_dir2_node_trim_free(args
, fo
, &rval
))) {
1762 fo
-= mp
->m_dirblkfsbs
;
1767 * Now find the block just before the freespace block.
1769 if ((error
= xfs_bmap_last_before(tp
, dp
, &fo
, XFS_DATA_FORK
))) {
1773 * If it's not the single leaf block, give up.
1775 if (XFS_FSB_TO_B(mp
, fo
) > XFS_DIR2_LEAF_OFFSET
+ mp
->m_dirblksize
)
1777 lbp
= state
->path
.blk
[0].bp
;
1779 xfs_dir3_leaf_hdr_from_disk(&leafhdr
, leaf
);
1781 ASSERT(leafhdr
.magic
== XFS_DIR2_LEAFN_MAGIC
||
1782 leafhdr
.magic
== XFS_DIR3_LEAFN_MAGIC
);
1785 * Read the freespace block.
1787 error
= xfs_dir2_free_read(tp
, dp
, mp
->m_dirfreeblk
, &fbp
);
1791 xfs_dir3_free_hdr_from_disk(&freehdr
, free
);
1793 ASSERT(!freehdr
.firstdb
);
1796 * Now see if the leafn and free data will fit in a leaf1.
1797 * If not, release the buffer and give up.
1799 if (xfs_dir3_leaf_size(&leafhdr
, freehdr
.nvalid
) > mp
->m_dirblksize
) {
1800 xfs_trans_brelse(tp
, fbp
);
1805 * If the leaf has any stale entries in it, compress them out.
1808 xfs_dir3_leaf_compact(args
, &leafhdr
, lbp
);
1810 lbp
->b_ops
= &xfs_dir3_leaf1_buf_ops
;
1811 xfs_trans_buf_set_type(tp
, lbp
, XFS_BLFT_DIR_LEAF1_BUF
);
1812 leafhdr
.magic
= (leafhdr
.magic
== XFS_DIR2_LEAFN_MAGIC
)
1813 ? XFS_DIR2_LEAF1_MAGIC
1814 : XFS_DIR3_LEAF1_MAGIC
;
1817 * Set up the leaf tail from the freespace block.
1819 ltp
= xfs_dir2_leaf_tail_p(mp
, leaf
);
1820 ltp
->bestcount
= cpu_to_be32(freehdr
.nvalid
);
1823 * Set up the leaf bests table.
1825 memcpy(xfs_dir2_leaf_bests_p(ltp
), xfs_dir3_free_bests_p(mp
, free
),
1826 freehdr
.nvalid
* sizeof(xfs_dir2_data_off_t
));
1828 xfs_dir3_leaf_hdr_to_disk(leaf
, &leafhdr
);
1829 xfs_dir3_leaf_log_header(tp
, lbp
);
1830 xfs_dir3_leaf_log_bests(tp
, lbp
, 0, be32_to_cpu(ltp
->bestcount
) - 1);
1831 xfs_dir3_leaf_log_tail(tp
, lbp
);
1832 xfs_dir3_leaf_check(mp
, lbp
);
1835 * Get rid of the freespace block.
1837 error
= xfs_dir2_shrink_inode(args
, XFS_DIR2_FREE_FIRSTDB(mp
), fbp
);
1840 * This can't fail here because it can only happen when
1841 * punching out the middle of an extent, and this is an
1844 ASSERT(error
!= ENOSPC
);
1849 * Now see if we can convert the single-leaf directory
1850 * down to a block form directory.
1851 * This routine always kills the dabuf for the leaf, so
1852 * eliminate it from the path.
1854 error
= xfs_dir2_leaf_to_block(args
, lbp
, NULL
);
1855 state
->path
.blk
[0].bp
= NULL
;