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
23 * Local function declarations.
25 static int xfs_dir2_leaf_lookup_int(xfs_da_args_t
*args
, struct xfs_buf
**lbpp
,
26 int *indexp
, struct xfs_buf
**dbpp
);
27 static void xfs_dir3_leaf_log_bests(struct xfs_trans
*tp
, struct xfs_buf
*bp
,
29 static void xfs_dir3_leaf_log_tail(struct xfs_trans
*tp
, struct xfs_buf
*bp
);
32 * Check the internal consistency of a leaf1 block.
33 * Pop an assert if something is wrong.
36 #define xfs_dir3_leaf_check(mp, bp) \
38 if (!xfs_dir3_leaf1_check((mp), (bp))) \
47 struct xfs_dir2_leaf
*leaf
= bp
->b_addr
;
48 struct xfs_dir3_icleaf_hdr leafhdr
;
50 xfs_dir3_leaf_hdr_from_disk(&leafhdr
, leaf
);
52 if (leafhdr
.magic
== XFS_DIR3_LEAF1_MAGIC
) {
53 struct xfs_dir3_leaf_hdr
*leaf3
= bp
->b_addr
;
54 if (be64_to_cpu(leaf3
->info
.blkno
) != bp
->b_bn
)
56 } else if (leafhdr
.magic
!= XFS_DIR2_LEAF1_MAGIC
)
59 return xfs_dir3_leaf_check_int(mp
, &leafhdr
, leaf
);
62 #define xfs_dir3_leaf_check(mp, bp)
66 xfs_dir3_leaf_hdr_from_disk(
67 struct xfs_dir3_icleaf_hdr
*to
,
68 struct xfs_dir2_leaf
*from
)
70 if (from
->hdr
.info
.magic
== cpu_to_be16(XFS_DIR2_LEAF1_MAGIC
) ||
71 from
->hdr
.info
.magic
== cpu_to_be16(XFS_DIR2_LEAFN_MAGIC
)) {
72 to
->forw
= be32_to_cpu(from
->hdr
.info
.forw
);
73 to
->back
= be32_to_cpu(from
->hdr
.info
.back
);
74 to
->magic
= be16_to_cpu(from
->hdr
.info
.magic
);
75 to
->count
= be16_to_cpu(from
->hdr
.count
);
76 to
->stale
= be16_to_cpu(from
->hdr
.stale
);
78 struct xfs_dir3_leaf_hdr
*hdr3
= (struct xfs_dir3_leaf_hdr
*)from
;
80 to
->forw
= be32_to_cpu(hdr3
->info
.hdr
.forw
);
81 to
->back
= be32_to_cpu(hdr3
->info
.hdr
.back
);
82 to
->magic
= be16_to_cpu(hdr3
->info
.hdr
.magic
);
83 to
->count
= be16_to_cpu(hdr3
->count
);
84 to
->stale
= be16_to_cpu(hdr3
->stale
);
87 ASSERT(to
->magic
== XFS_DIR2_LEAF1_MAGIC
||
88 to
->magic
== XFS_DIR3_LEAF1_MAGIC
||
89 to
->magic
== XFS_DIR2_LEAFN_MAGIC
||
90 to
->magic
== XFS_DIR3_LEAFN_MAGIC
);
94 xfs_dir3_leaf_hdr_to_disk(
95 struct xfs_dir2_leaf
*to
,
96 struct xfs_dir3_icleaf_hdr
*from
)
98 ASSERT(from
->magic
== XFS_DIR2_LEAF1_MAGIC
||
99 from
->magic
== XFS_DIR3_LEAF1_MAGIC
||
100 from
->magic
== XFS_DIR2_LEAFN_MAGIC
||
101 from
->magic
== XFS_DIR3_LEAFN_MAGIC
);
103 if (from
->magic
== XFS_DIR2_LEAF1_MAGIC
||
104 from
->magic
== XFS_DIR2_LEAFN_MAGIC
) {
105 to
->hdr
.info
.forw
= cpu_to_be32(from
->forw
);
106 to
->hdr
.info
.back
= cpu_to_be32(from
->back
);
107 to
->hdr
.info
.magic
= cpu_to_be16(from
->magic
);
108 to
->hdr
.count
= cpu_to_be16(from
->count
);
109 to
->hdr
.stale
= cpu_to_be16(from
->stale
);
111 struct xfs_dir3_leaf_hdr
*hdr3
= (struct xfs_dir3_leaf_hdr
*)to
;
113 hdr3
->info
.hdr
.forw
= cpu_to_be32(from
->forw
);
114 hdr3
->info
.hdr
.back
= cpu_to_be32(from
->back
);
115 hdr3
->info
.hdr
.magic
= cpu_to_be16(from
->magic
);
116 hdr3
->count
= cpu_to_be16(from
->count
);
117 hdr3
->stale
= cpu_to_be16(from
->stale
);
122 xfs_dir3_leaf_check_int(
123 struct xfs_mount
*mp
,
124 struct xfs_dir3_icleaf_hdr
*hdr
,
125 struct xfs_dir2_leaf
*leaf
)
127 struct xfs_dir2_leaf_entry
*ents
;
128 xfs_dir2_leaf_tail_t
*ltp
;
132 ents
= xfs_dir3_leaf_ents_p(leaf
);
133 ltp
= xfs_dir2_leaf_tail_p(mp
, leaf
);
136 * XXX (dgc): This value is not restrictive enough.
137 * Should factor in the size of the bests table as well.
138 * We can deduce a value for that from di_size.
140 if (hdr
->count
> xfs_dir3_max_leaf_ents(mp
, leaf
))
143 /* Leaves and bests don't overlap in leaf format. */
144 if ((hdr
->magic
== XFS_DIR2_LEAF1_MAGIC
||
145 hdr
->magic
== XFS_DIR3_LEAF1_MAGIC
) &&
146 (char *)&ents
[hdr
->count
] > (char *)xfs_dir2_leaf_bests_p(ltp
))
149 /* Check hash value order, count stale entries. */
150 for (i
= stale
= 0; i
< hdr
->count
; i
++) {
151 if (i
+ 1 < hdr
->count
) {
152 if (be32_to_cpu(ents
[i
].hashval
) >
153 be32_to_cpu(ents
[i
+ 1].hashval
))
156 if (ents
[i
].address
== cpu_to_be32(XFS_DIR2_NULL_DATAPTR
))
159 if (hdr
->stale
!= stale
)
165 xfs_dir3_leaf_verify(
169 struct xfs_mount
*mp
= bp
->b_target
->bt_mount
;
170 struct xfs_dir2_leaf
*leaf
= bp
->b_addr
;
171 struct xfs_dir3_icleaf_hdr leafhdr
;
173 ASSERT(magic
== XFS_DIR2_LEAF1_MAGIC
|| magic
== XFS_DIR2_LEAFN_MAGIC
);
175 xfs_dir3_leaf_hdr_from_disk(&leafhdr
, leaf
);
176 if (xfs_sb_version_hascrc(&mp
->m_sb
)) {
177 struct xfs_dir3_leaf_hdr
*leaf3
= bp
->b_addr
;
179 if ((magic
== XFS_DIR2_LEAF1_MAGIC
&&
180 leafhdr
.magic
!= XFS_DIR3_LEAF1_MAGIC
) ||
181 (magic
== XFS_DIR2_LEAFN_MAGIC
&&
182 leafhdr
.magic
!= XFS_DIR3_LEAFN_MAGIC
))
185 if (!uuid_equal(&leaf3
->info
.uuid
, &mp
->m_sb
.sb_uuid
))
187 if (be64_to_cpu(leaf3
->info
.blkno
) != bp
->b_bn
)
190 if (leafhdr
.magic
!= magic
)
193 return xfs_dir3_leaf_check_int(mp
, &leafhdr
, leaf
);
201 struct xfs_mount
*mp
= bp
->b_target
->bt_mount
;
203 if ((xfs_sb_version_hascrc(&mp
->m_sb
) &&
204 !xfs_verify_cksum(bp
->b_addr
, BBTOB(bp
->b_length
),
205 XFS_DIR3_LEAF_CRC_OFF
)) ||
206 !xfs_dir3_leaf_verify(bp
, magic
)) {
207 XFS_CORRUPTION_ERROR(__func__
, XFS_ERRLEVEL_LOW
, mp
, bp
->b_addr
);
208 xfs_buf_ioerror(bp
, EFSCORRUPTED
);
217 struct xfs_mount
*mp
= bp
->b_target
->bt_mount
;
218 struct xfs_buf_log_item
*bip
= bp
->b_fspriv
;
219 struct xfs_dir3_leaf_hdr
*hdr3
= bp
->b_addr
;
221 if (!xfs_dir3_leaf_verify(bp
, magic
)) {
222 XFS_CORRUPTION_ERROR(__func__
, XFS_ERRLEVEL_LOW
, mp
, bp
->b_addr
);
223 xfs_buf_ioerror(bp
, EFSCORRUPTED
);
227 if (!xfs_sb_version_hascrc(&mp
->m_sb
))
231 hdr3
->info
.lsn
= cpu_to_be64(bip
->bli_item
.li_lsn
);
233 xfs_update_cksum(bp
->b_addr
, BBTOB(bp
->b_length
), XFS_DIR3_LEAF_CRC_OFF
);
237 xfs_dir3_leaf1_read_verify(
240 __read_verify(bp
, XFS_DIR2_LEAF1_MAGIC
);
244 xfs_dir3_leaf1_write_verify(
247 __write_verify(bp
, XFS_DIR2_LEAF1_MAGIC
);
251 xfs_dir3_leafn_read_verify(
254 __read_verify(bp
, XFS_DIR2_LEAFN_MAGIC
);
258 xfs_dir3_leafn_write_verify(
261 __write_verify(bp
, XFS_DIR2_LEAFN_MAGIC
);
264 const struct xfs_buf_ops xfs_dir3_leaf1_buf_ops
= {
265 .verify_read
= xfs_dir3_leaf1_read_verify
,
266 .verify_write
= xfs_dir3_leaf1_write_verify
,
269 const struct xfs_buf_ops xfs_dir3_leafn_buf_ops
= {
270 .verify_read
= xfs_dir3_leafn_read_verify
,
271 .verify_write
= xfs_dir3_leafn_write_verify
,
276 struct xfs_trans
*tp
,
277 struct xfs_inode
*dp
,
279 xfs_daddr_t mappedbno
,
280 struct xfs_buf
**bpp
)
282 return xfs_da_read_buf(tp
, dp
, fbno
, mappedbno
, bpp
,
283 XFS_DATA_FORK
, &xfs_dir3_leaf1_buf_ops
);
288 struct xfs_trans
*tp
,
289 struct xfs_inode
*dp
,
291 xfs_daddr_t mappedbno
,
292 struct xfs_buf
**bpp
)
294 return xfs_da_read_buf(tp
, dp
, fbno
, mappedbno
, bpp
,
295 XFS_DATA_FORK
, &xfs_dir3_leafn_buf_ops
);
299 * Initialize a new leaf block, leaf1 or leafn magic accepted.
303 struct xfs_mount
*mp
,
308 struct xfs_dir2_leaf
*leaf
= bp
->b_addr
;
310 ASSERT(type
== XFS_DIR2_LEAF1_MAGIC
|| type
== XFS_DIR2_LEAFN_MAGIC
);
312 if (xfs_sb_version_hascrc(&mp
->m_sb
)) {
313 struct xfs_dir3_leaf_hdr
*leaf3
= bp
->b_addr
;
315 memset(leaf3
, 0, sizeof(*leaf3
));
317 leaf3
->info
.hdr
.magic
= (type
== XFS_DIR2_LEAF1_MAGIC
)
318 ? cpu_to_be16(XFS_DIR3_LEAF1_MAGIC
)
319 : cpu_to_be16(XFS_DIR3_LEAFN_MAGIC
);
320 leaf3
->info
.blkno
= cpu_to_be64(bp
->b_bn
);
321 leaf3
->info
.owner
= cpu_to_be64(owner
);
322 uuid_copy(&leaf3
->info
.uuid
, &mp
->m_sb
.sb_uuid
);
324 memset(leaf
, 0, sizeof(*leaf
));
325 leaf
->hdr
.info
.magic
= cpu_to_be16(type
);
329 * If it's a leaf-format directory initialize the tail.
330 * Caller is responsible for initialising the bests table.
332 if (type
== XFS_DIR2_LEAF1_MAGIC
) {
333 struct xfs_dir2_leaf_tail
*ltp
;
335 ltp
= xfs_dir2_leaf_tail_p(mp
, leaf
);
337 bp
->b_ops
= &xfs_dir3_leaf1_buf_ops
;
339 bp
->b_ops
= &xfs_dir3_leafn_buf_ops
;
343 xfs_dir3_leaf_get_buf(
346 struct xfs_buf
**bpp
,
349 struct xfs_inode
*dp
= args
->dp
;
350 struct xfs_trans
*tp
= args
->trans
;
351 struct xfs_mount
*mp
= dp
->i_mount
;
355 ASSERT(magic
== XFS_DIR2_LEAF1_MAGIC
|| magic
== XFS_DIR2_LEAFN_MAGIC
);
356 ASSERT(bno
>= XFS_DIR2_LEAF_FIRSTDB(mp
) &&
357 bno
< XFS_DIR2_FREE_FIRSTDB(mp
));
359 error
= xfs_da_get_buf(tp
, dp
, xfs_dir2_db_to_da(mp
, bno
), -1, &bp
,
364 xfs_dir3_leaf_init(mp
, bp
, dp
->i_ino
, magic
);
365 xfs_dir3_leaf_log_header(tp
, bp
);
366 if (magic
== XFS_DIR2_LEAF1_MAGIC
)
367 xfs_dir3_leaf_log_tail(tp
, bp
);
373 * Convert a block form directory to a leaf form directory.
376 xfs_dir2_block_to_leaf(
377 xfs_da_args_t
*args
, /* operation arguments */
378 struct xfs_buf
*dbp
) /* input block's buffer */
380 __be16
*bestsp
; /* leaf's bestsp entries */
381 xfs_dablk_t blkno
; /* leaf block's bno */
382 xfs_dir2_data_hdr_t
*hdr
; /* block header */
383 xfs_dir2_leaf_entry_t
*blp
; /* block's leaf entries */
384 xfs_dir2_block_tail_t
*btp
; /* block's tail */
385 xfs_inode_t
*dp
; /* incore directory inode */
386 int error
; /* error return code */
387 struct xfs_buf
*lbp
; /* leaf block's buffer */
388 xfs_dir2_db_t ldb
; /* leaf block's bno */
389 xfs_dir2_leaf_t
*leaf
; /* leaf structure */
390 xfs_dir2_leaf_tail_t
*ltp
; /* leaf's tail */
391 xfs_mount_t
*mp
; /* filesystem mount point */
392 int needlog
; /* need to log block header */
393 int needscan
; /* need to rescan bestfree */
394 xfs_trans_t
*tp
; /* transaction pointer */
395 struct xfs_dir2_data_free
*bf
;
396 struct xfs_dir2_leaf_entry
*ents
;
397 struct xfs_dir3_icleaf_hdr leafhdr
;
399 trace_xfs_dir2_block_to_leaf(args
);
405 * Add the leaf block to the inode.
406 * This interface will only put blocks in the leaf/node range.
407 * Since that's empty now, we'll get the root (block 0 in range).
409 if ((error
= xfs_da_grow_inode(args
, &blkno
))) {
412 ldb
= xfs_dir2_da_to_db(mp
, blkno
);
413 ASSERT(ldb
== XFS_DIR2_LEAF_FIRSTDB(mp
));
415 * Initialize the leaf block, get a buffer for it.
417 error
= xfs_dir3_leaf_get_buf(args
, ldb
, &lbp
, XFS_DIR2_LEAF1_MAGIC
);
423 xfs_dir3_data_check(dp
, dbp
);
424 btp
= xfs_dir2_block_tail_p(mp
, hdr
);
425 blp
= xfs_dir2_block_leaf_p(btp
);
426 bf
= xfs_dir3_data_bestfree_p(hdr
);
427 ents
= xfs_dir3_leaf_ents_p(leaf
);
430 * Set the counts in the leaf header.
432 xfs_dir3_leaf_hdr_from_disk(&leafhdr
, leaf
);
433 leafhdr
.count
= be32_to_cpu(btp
->count
);
434 leafhdr
.stale
= be32_to_cpu(btp
->stale
);
435 xfs_dir3_leaf_hdr_to_disk(leaf
, &leafhdr
);
436 xfs_dir3_leaf_log_header(tp
, lbp
);
439 * Could compact these but I think we always do the conversion
440 * after squeezing out stale entries.
442 memcpy(ents
, blp
, be32_to_cpu(btp
->count
) * sizeof(xfs_dir2_leaf_entry_t
));
443 xfs_dir3_leaf_log_ents(tp
, lbp
, 0, leafhdr
.count
- 1);
447 * Make the space formerly occupied by the leaf entries and block
450 xfs_dir2_data_make_free(tp
, dbp
,
451 (xfs_dir2_data_aoff_t
)((char *)blp
- (char *)hdr
),
452 (xfs_dir2_data_aoff_t
)((char *)hdr
+ mp
->m_dirblksize
-
454 &needlog
, &needscan
);
456 * Fix up the block header, make it a data block.
458 dbp
->b_ops
= &xfs_dir3_data_buf_ops
;
459 if (hdr
->magic
== cpu_to_be32(XFS_DIR2_BLOCK_MAGIC
))
460 hdr
->magic
= cpu_to_be32(XFS_DIR2_DATA_MAGIC
);
462 hdr
->magic
= cpu_to_be32(XFS_DIR3_DATA_MAGIC
);
465 xfs_dir2_data_freescan(mp
, hdr
, &needlog
);
467 * Set up leaf tail and bests table.
469 ltp
= xfs_dir2_leaf_tail_p(mp
, leaf
);
470 ltp
->bestcount
= cpu_to_be32(1);
471 bestsp
= xfs_dir2_leaf_bests_p(ltp
);
472 bestsp
[0] = bf
[0].length
;
474 * Log the data header and leaf bests table.
477 xfs_dir2_data_log_header(tp
, dbp
);
478 xfs_dir3_leaf_check(mp
, lbp
);
479 xfs_dir3_data_check(dp
, dbp
);
480 xfs_dir3_leaf_log_bests(tp
, lbp
, 0, 0);
485 xfs_dir3_leaf_find_stale(
486 struct xfs_dir3_icleaf_hdr
*leafhdr
,
487 struct xfs_dir2_leaf_entry
*ents
,
493 * Find the first stale entry before our index, if any.
495 for (*lowstale
= index
- 1; *lowstale
>= 0; --*lowstale
) {
496 if (ents
[*lowstale
].address
==
497 cpu_to_be32(XFS_DIR2_NULL_DATAPTR
))
502 * Find the first stale entry at or after our index, if any.
503 * Stop if the result would require moving more entries than using
506 for (*highstale
= index
; *highstale
< leafhdr
->count
; ++*highstale
) {
507 if (ents
[*highstale
].address
==
508 cpu_to_be32(XFS_DIR2_NULL_DATAPTR
))
510 if (*lowstale
>= 0 && index
- *lowstale
<= *highstale
- index
)
515 struct xfs_dir2_leaf_entry
*
516 xfs_dir3_leaf_find_entry(
517 struct xfs_dir3_icleaf_hdr
*leafhdr
,
518 struct xfs_dir2_leaf_entry
*ents
,
519 int index
, /* leaf table position */
520 int compact
, /* need to compact leaves */
521 int lowstale
, /* index of prev stale leaf */
522 int highstale
, /* index of next stale leaf */
523 int *lfloglow
, /* low leaf logging index */
524 int *lfloghigh
) /* high leaf logging index */
526 if (!leafhdr
->stale
) {
527 xfs_dir2_leaf_entry_t
*lep
; /* leaf entry table pointer */
530 * Now we need to make room to insert the leaf entry.
532 * If there are no stale entries, just insert a hole at index.
535 if (index
< leafhdr
->count
)
536 memmove(lep
+ 1, lep
,
537 (leafhdr
->count
- index
) * sizeof(*lep
));
540 * Record low and high logging indices for the leaf.
543 *lfloghigh
= leafhdr
->count
++;
548 * There are stale entries.
550 * We will use one of them for the new entry. It's probably not at
551 * the right location, so we'll have to shift some up or down first.
553 * If we didn't compact before, we need to find the nearest stale
554 * entries before and after our insertion point.
557 xfs_dir3_leaf_find_stale(leafhdr
, ents
, index
,
558 &lowstale
, &highstale
);
561 * If the low one is better, use it.
564 (highstale
== leafhdr
->count
||
565 index
- lowstale
- 1 < highstale
- index
)) {
566 ASSERT(index
- lowstale
- 1 >= 0);
567 ASSERT(ents
[lowstale
].address
==
568 cpu_to_be32(XFS_DIR2_NULL_DATAPTR
));
571 * Copy entries up to cover the stale entry and make room
574 if (index
- lowstale
- 1 > 0) {
575 memmove(&ents
[lowstale
], &ents
[lowstale
+ 1],
576 (index
- lowstale
- 1) *
577 sizeof(xfs_dir2_leaf_entry_t
));
579 *lfloglow
= MIN(lowstale
, *lfloglow
);
580 *lfloghigh
= MAX(index
- 1, *lfloghigh
);
582 return &ents
[index
- 1];
586 * The high one is better, so use that one.
588 ASSERT(highstale
- index
>= 0);
589 ASSERT(ents
[highstale
].address
== cpu_to_be32(XFS_DIR2_NULL_DATAPTR
));
592 * Copy entries down to cover the stale entry and make room for the
595 if (highstale
- index
> 0) {
596 memmove(&ents
[index
+ 1], &ents
[index
],
597 (highstale
- index
) * sizeof(xfs_dir2_leaf_entry_t
));
599 *lfloglow
= MIN(index
, *lfloglow
);
600 *lfloghigh
= MAX(highstale
, *lfloghigh
);
606 * Add an entry to a leaf form directory.
609 xfs_dir2_leaf_addname(
610 xfs_da_args_t
*args
) /* operation arguments */
612 __be16
*bestsp
; /* freespace table in leaf */
613 int compact
; /* need to compact leaves */
614 xfs_dir2_data_hdr_t
*hdr
; /* data block header */
615 struct xfs_buf
*dbp
; /* data block buffer */
616 xfs_dir2_data_entry_t
*dep
; /* data block entry */
617 xfs_inode_t
*dp
; /* incore directory inode */
618 xfs_dir2_data_unused_t
*dup
; /* data unused entry */
619 int error
; /* error return value */
620 int grown
; /* allocated new data block */
621 int highstale
; /* index of next stale leaf */
622 int i
; /* temporary, index */
623 int index
; /* leaf table position */
624 struct xfs_buf
*lbp
; /* leaf's buffer */
625 xfs_dir2_leaf_t
*leaf
; /* leaf structure */
626 int length
; /* length of new entry */
627 xfs_dir2_leaf_entry_t
*lep
; /* leaf entry table pointer */
628 int lfloglow
; /* low leaf logging index */
629 int lfloghigh
; /* high leaf logging index */
630 int lowstale
; /* index of prev stale leaf */
631 xfs_dir2_leaf_tail_t
*ltp
; /* leaf tail pointer */
632 xfs_mount_t
*mp
; /* filesystem mount point */
633 int needbytes
; /* leaf block bytes needed */
634 int needlog
; /* need to log data header */
635 int needscan
; /* need to rescan data free */
636 __be16
*tagp
; /* end of data entry */
637 xfs_trans_t
*tp
; /* transaction pointer */
638 xfs_dir2_db_t use_block
; /* data block number */
639 struct xfs_dir2_data_free
*bf
; /* bestfree table */
640 struct xfs_dir2_leaf_entry
*ents
;
641 struct xfs_dir3_icleaf_hdr leafhdr
;
643 trace_xfs_dir2_leaf_addname(args
);
649 error
= xfs_dir3_leaf_read(tp
, dp
, mp
->m_dirleafblk
, -1, &lbp
);
654 * Look up the entry by hash value and name.
655 * We know it's not there, our caller has already done a lookup.
656 * So the index is of the entry to insert in front of.
657 * But if there are dup hash values the index is of the first of those.
659 index
= xfs_dir2_leaf_search_hash(args
, lbp
);
661 ltp
= xfs_dir2_leaf_tail_p(mp
, leaf
);
662 ents
= xfs_dir3_leaf_ents_p(leaf
);
663 xfs_dir3_leaf_hdr_from_disk(&leafhdr
, leaf
);
664 bestsp
= xfs_dir2_leaf_bests_p(ltp
);
665 length
= xfs_dir2_data_entsize(args
->namelen
);
668 * See if there are any entries with the same hash value
669 * and space in their block for the new entry.
670 * This is good because it puts multiple same-hash value entries
671 * in a data block, improving the lookup of those entries.
673 for (use_block
= -1, lep
= &ents
[index
];
674 index
< leafhdr
.count
&& be32_to_cpu(lep
->hashval
) == args
->hashval
;
676 if (be32_to_cpu(lep
->address
) == XFS_DIR2_NULL_DATAPTR
)
678 i
= xfs_dir2_dataptr_to_db(mp
, be32_to_cpu(lep
->address
));
679 ASSERT(i
< be32_to_cpu(ltp
->bestcount
));
680 ASSERT(bestsp
[i
] != cpu_to_be16(NULLDATAOFF
));
681 if (be16_to_cpu(bestsp
[i
]) >= length
) {
687 * Didn't find a block yet, linear search all the data blocks.
689 if (use_block
== -1) {
690 for (i
= 0; i
< be32_to_cpu(ltp
->bestcount
); i
++) {
692 * Remember a block we see that's missing.
694 if (bestsp
[i
] == cpu_to_be16(NULLDATAOFF
) &&
697 else if (be16_to_cpu(bestsp
[i
]) >= length
) {
704 * How many bytes do we need in the leaf block?
708 needbytes
+= sizeof(xfs_dir2_leaf_entry_t
);
710 needbytes
+= sizeof(xfs_dir2_data_off_t
);
713 * Now kill use_block if it refers to a missing block, so we
714 * can use it as an indication of allocation needed.
716 if (use_block
!= -1 && bestsp
[use_block
] == cpu_to_be16(NULLDATAOFF
))
719 * If we don't have enough free bytes but we can make enough
720 * by compacting out stale entries, we'll do that.
722 if ((char *)bestsp
- (char *)&ents
[leafhdr
.count
] < needbytes
&&
727 * Otherwise if we don't have enough free bytes we need to
728 * convert to node form.
730 else if ((char *)bestsp
- (char *)&ents
[leafhdr
.count
] < needbytes
) {
732 * Just checking or no space reservation, give up.
734 if ((args
->op_flags
& XFS_DA_OP_JUSTCHECK
) ||
736 xfs_trans_brelse(tp
, lbp
);
737 return XFS_ERROR(ENOSPC
);
740 * Convert to node form.
742 error
= xfs_dir2_leaf_to_node(args
, lbp
);
746 * Then add the new entry.
748 return xfs_dir2_node_addname(args
);
751 * Otherwise it will fit without compaction.
756 * If just checking, then it will fit unless we needed to allocate
759 if (args
->op_flags
& XFS_DA_OP_JUSTCHECK
) {
760 xfs_trans_brelse(tp
, lbp
);
761 return use_block
== -1 ? XFS_ERROR(ENOSPC
) : 0;
764 * If no allocations are allowed, return now before we've
767 if (args
->total
== 0 && use_block
== -1) {
768 xfs_trans_brelse(tp
, lbp
);
769 return XFS_ERROR(ENOSPC
);
772 * Need to compact the leaf entries, removing stale ones.
773 * Leave one stale entry behind - the one closest to our
774 * insertion index - and we'll shift that one to our insertion
778 xfs_dir3_leaf_compact_x1(&leafhdr
, ents
, &index
, &lowstale
,
779 &highstale
, &lfloglow
, &lfloghigh
);
782 * There are stale entries, so we'll need log-low and log-high
783 * impossibly bad values later.
785 else if (leafhdr
.stale
) {
786 lfloglow
= leafhdr
.count
;
790 * If there was no data block space found, we need to allocate
793 if (use_block
== -1) {
795 * Add the new data block.
797 if ((error
= xfs_dir2_grow_inode(args
, XFS_DIR2_DATA_SPACE
,
799 xfs_trans_brelse(tp
, lbp
);
803 * Initialize the block.
805 if ((error
= xfs_dir3_data_init(args
, use_block
, &dbp
))) {
806 xfs_trans_brelse(tp
, lbp
);
810 * If we're adding a new data block on the end we need to
811 * extend the bests table. Copy it up one entry.
813 if (use_block
>= be32_to_cpu(ltp
->bestcount
)) {
815 memmove(&bestsp
[0], &bestsp
[1],
816 be32_to_cpu(ltp
->bestcount
) * sizeof(bestsp
[0]));
817 be32_add_cpu(<p
->bestcount
, 1);
818 xfs_dir3_leaf_log_tail(tp
, lbp
);
819 xfs_dir3_leaf_log_bests(tp
, lbp
, 0, be32_to_cpu(ltp
->bestcount
) - 1);
822 * If we're filling in a previously empty block just log it.
825 xfs_dir3_leaf_log_bests(tp
, lbp
, use_block
, use_block
);
827 bf
= xfs_dir3_data_bestfree_p(hdr
);
828 bestsp
[use_block
] = bf
[0].length
;
832 * Already had space in some data block.
833 * Just read that one in.
835 error
= xfs_dir3_data_read(tp
, dp
,
836 xfs_dir2_db_to_da(mp
, use_block
),
839 xfs_trans_brelse(tp
, lbp
);
843 bf
= xfs_dir3_data_bestfree_p(hdr
);
847 * Point to the biggest freespace in our data block.
849 dup
= (xfs_dir2_data_unused_t
*)
850 ((char *)hdr
+ be16_to_cpu(bf
[0].offset
));
851 ASSERT(be16_to_cpu(dup
->length
) >= length
);
852 needscan
= needlog
= 0;
854 * Mark the initial part of our freespace in use for the new entry.
856 xfs_dir2_data_use_free(tp
, dbp
, dup
,
857 (xfs_dir2_data_aoff_t
)((char *)dup
- (char *)hdr
), length
,
858 &needlog
, &needscan
);
860 * Initialize our new entry (at last).
862 dep
= (xfs_dir2_data_entry_t
*)dup
;
863 dep
->inumber
= cpu_to_be64(args
->inumber
);
864 dep
->namelen
= args
->namelen
;
865 memcpy(dep
->name
, args
->name
, dep
->namelen
);
866 tagp
= xfs_dir2_data_entry_tag_p(dep
);
867 *tagp
= cpu_to_be16((char *)dep
- (char *)hdr
);
869 * Need to scan fix up the bestfree table.
872 xfs_dir2_data_freescan(mp
, hdr
, &needlog
);
874 * Need to log the data block's header.
877 xfs_dir2_data_log_header(tp
, dbp
);
878 xfs_dir2_data_log_entry(tp
, dbp
, dep
);
880 * If the bests table needs to be changed, do it.
881 * Log the change unless we've already done that.
883 if (be16_to_cpu(bestsp
[use_block
]) != be16_to_cpu(bf
[0].length
)) {
884 bestsp
[use_block
] = bf
[0].length
;
886 xfs_dir3_leaf_log_bests(tp
, lbp
, use_block
, use_block
);
889 lep
= xfs_dir3_leaf_find_entry(&leafhdr
, ents
, index
, compact
, lowstale
,
890 highstale
, &lfloglow
, &lfloghigh
);
893 * Fill in the new leaf entry.
895 lep
->hashval
= cpu_to_be32(args
->hashval
);
896 lep
->address
= cpu_to_be32(xfs_dir2_db_off_to_dataptr(mp
, use_block
,
897 be16_to_cpu(*tagp
)));
899 * Log the leaf fields and give up the buffers.
901 xfs_dir3_leaf_hdr_to_disk(leaf
, &leafhdr
);
902 xfs_dir3_leaf_log_header(tp
, lbp
);
903 xfs_dir3_leaf_log_ents(tp
, lbp
, lfloglow
, lfloghigh
);
904 xfs_dir3_leaf_check(mp
, 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
;
930 * Compress out the stale entries in place.
932 ents
= xfs_dir3_leaf_ents_p(leaf
);
933 for (from
= to
= 0, loglow
= -1; from
< leafhdr
->count
; from
++) {
934 if (ents
[from
].address
== cpu_to_be32(XFS_DIR2_NULL_DATAPTR
))
937 * Only actually copy the entries that are different.
942 ents
[to
] = ents
[from
];
947 * Update and log the header, log the leaf entries.
949 ASSERT(leafhdr
->stale
== from
- to
);
950 leafhdr
->count
-= leafhdr
->stale
;
953 xfs_dir3_leaf_hdr_to_disk(leaf
, leafhdr
);
954 xfs_dir3_leaf_log_header(args
->trans
, bp
);
956 xfs_dir3_leaf_log_ents(args
->trans
, bp
, loglow
, to
- 1);
960 * Compact the leaf entries, removing stale ones.
961 * Leave one stale entry behind - the one closest to our
962 * insertion index - and the caller will shift that one to our insertion
964 * Return new insertion index, where the remaining stale entry is,
965 * and leaf logging indices.
968 xfs_dir3_leaf_compact_x1(
969 struct xfs_dir3_icleaf_hdr
*leafhdr
,
970 struct xfs_dir2_leaf_entry
*ents
,
971 int *indexp
, /* insertion index */
972 int *lowstalep
, /* out: stale entry before us */
973 int *highstalep
, /* out: stale entry after us */
974 int *lowlogp
, /* out: low log index */
975 int *highlogp
) /* out: high log index */
977 int from
; /* source copy index */
978 int highstale
; /* stale entry at/after index */
979 int index
; /* insertion index */
980 int keepstale
; /* source index of kept stale */
981 int lowstale
; /* stale entry before index */
982 int newindex
=0; /* new insertion index */
983 int to
; /* destination copy index */
985 ASSERT(leafhdr
->stale
> 1);
988 xfs_dir3_leaf_find_stale(leafhdr
, ents
, index
, &lowstale
, &highstale
);
991 * Pick the better of lowstale and highstale.
994 (highstale
== leafhdr
->count
||
995 index
- lowstale
<= highstale
- index
))
996 keepstale
= lowstale
;
998 keepstale
= highstale
;
1000 * Copy the entries in place, removing all the stale entries
1003 for (from
= to
= 0; from
< leafhdr
->count
; from
++) {
1005 * Notice the new value of index.
1009 if (from
!= keepstale
&&
1010 ents
[from
].address
== cpu_to_be32(XFS_DIR2_NULL_DATAPTR
)) {
1016 * Record the new keepstale value for the insertion.
1018 if (from
== keepstale
)
1019 lowstale
= highstale
= to
;
1021 * Copy only the entries that have moved.
1024 ents
[to
] = ents
[from
];
1029 * If the insertion point was past the last entry,
1030 * set the new insertion point accordingly.
1036 * Adjust the leaf header values.
1038 leafhdr
->count
-= from
- to
;
1041 * Remember the low/high stale value only in the "right"
1044 if (lowstale
>= newindex
)
1047 highstale
= leafhdr
->count
;
1048 *highlogp
= leafhdr
->count
- 1;
1049 *lowstalep
= lowstale
;
1050 *highstalep
= highstale
;
1055 * Log the bests entries indicated from a leaf1 block.
1058 xfs_dir3_leaf_log_bests(
1059 xfs_trans_t
*tp
, /* transaction pointer */
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(tp
->t_mountp
, leaf
);
1073 firstb
= xfs_dir2_leaf_bests_p(ltp
) + first
;
1074 lastb
= xfs_dir2_leaf_bests_p(ltp
) + last
;
1075 xfs_trans_log_buf(tp
, bp
, (uint
)((char *)firstb
- (char *)leaf
),
1076 (uint
)((char *)lastb
- (char *)leaf
+ sizeof(*lastb
) - 1));
1080 * Log the leaf entries indicated from a leaf1 or leafn block.
1083 xfs_dir3_leaf_log_ents(
1084 xfs_trans_t
*tp
, /* transaction pointer */
1085 struct xfs_buf
*bp
, /* leaf buffer */
1086 int first
, /* first entry to log */
1087 int last
) /* last entry to log */
1089 xfs_dir2_leaf_entry_t
*firstlep
; /* pointer to first entry */
1090 xfs_dir2_leaf_entry_t
*lastlep
; /* pointer to last entry */
1091 struct xfs_dir2_leaf
*leaf
= bp
->b_addr
;
1092 struct xfs_dir2_leaf_entry
*ents
;
1094 ASSERT(leaf
->hdr
.info
.magic
== cpu_to_be16(XFS_DIR2_LEAF1_MAGIC
) ||
1095 leaf
->hdr
.info
.magic
== cpu_to_be16(XFS_DIR3_LEAF1_MAGIC
) ||
1096 leaf
->hdr
.info
.magic
== cpu_to_be16(XFS_DIR2_LEAFN_MAGIC
) ||
1097 leaf
->hdr
.info
.magic
== cpu_to_be16(XFS_DIR3_LEAFN_MAGIC
));
1099 ents
= xfs_dir3_leaf_ents_p(leaf
);
1100 firstlep
= &ents
[first
];
1101 lastlep
= &ents
[last
];
1102 xfs_trans_log_buf(tp
, bp
, (uint
)((char *)firstlep
- (char *)leaf
),
1103 (uint
)((char *)lastlep
- (char *)leaf
+ sizeof(*lastlep
) - 1));
1107 * Log the header of the leaf1 or leafn block.
1110 xfs_dir3_leaf_log_header(
1111 struct xfs_trans
*tp
,
1114 struct xfs_dir2_leaf
*leaf
= bp
->b_addr
;
1116 ASSERT(leaf
->hdr
.info
.magic
== cpu_to_be16(XFS_DIR2_LEAF1_MAGIC
) ||
1117 leaf
->hdr
.info
.magic
== cpu_to_be16(XFS_DIR3_LEAF1_MAGIC
) ||
1118 leaf
->hdr
.info
.magic
== cpu_to_be16(XFS_DIR2_LEAFN_MAGIC
) ||
1119 leaf
->hdr
.info
.magic
== cpu_to_be16(XFS_DIR3_LEAFN_MAGIC
));
1121 xfs_trans_log_buf(tp
, bp
, (uint
)((char *)&leaf
->hdr
- (char *)leaf
),
1122 xfs_dir3_leaf_hdr_size(leaf
) - 1);
1126 * Log the tail of the leaf1 block.
1129 xfs_dir3_leaf_log_tail(
1130 struct xfs_trans
*tp
,
1133 struct xfs_dir2_leaf
*leaf
= bp
->b_addr
;
1134 xfs_dir2_leaf_tail_t
*ltp
; /* leaf tail structure */
1135 struct xfs_mount
*mp
= tp
->t_mountp
;
1137 ASSERT(leaf
->hdr
.info
.magic
== cpu_to_be16(XFS_DIR2_LEAF1_MAGIC
) ||
1138 leaf
->hdr
.info
.magic
== cpu_to_be16(XFS_DIR3_LEAF1_MAGIC
) ||
1139 leaf
->hdr
.info
.magic
== cpu_to_be16(XFS_DIR2_LEAFN_MAGIC
) ||
1140 leaf
->hdr
.info
.magic
== cpu_to_be16(XFS_DIR3_LEAFN_MAGIC
));
1142 ltp
= xfs_dir2_leaf_tail_p(mp
, leaf
);
1143 xfs_trans_log_buf(tp
, bp
, (uint
)((char *)ltp
- (char *)leaf
),
1144 (uint
)(mp
->m_dirblksize
- 1));
1148 * Look up the entry referred to by args in the leaf format directory.
1149 * Most of the work is done by the xfs_dir2_leaf_lookup_int routine which
1150 * is also used by the node-format code.
1153 xfs_dir2_leaf_lookup(
1154 xfs_da_args_t
*args
) /* operation arguments */
1156 struct xfs_buf
*dbp
; /* data block buffer */
1157 xfs_dir2_data_entry_t
*dep
; /* data block entry */
1158 xfs_inode_t
*dp
; /* incore directory inode */
1159 int error
; /* error return code */
1160 int index
; /* found entry index */
1161 struct xfs_buf
*lbp
; /* leaf buffer */
1162 xfs_dir2_leaf_t
*leaf
; /* leaf structure */
1163 xfs_dir2_leaf_entry_t
*lep
; /* leaf entry */
1164 xfs_trans_t
*tp
; /* transaction pointer */
1165 struct xfs_dir2_leaf_entry
*ents
;
1167 trace_xfs_dir2_leaf_lookup(args
);
1170 * Look up name in the leaf block, returning both buffers and index.
1172 if ((error
= xfs_dir2_leaf_lookup_int(args
, &lbp
, &index
, &dbp
))) {
1177 xfs_dir3_leaf_check(dp
->i_mount
, lbp
);
1179 ents
= xfs_dir3_leaf_ents_p(leaf
);
1181 * Get to the leaf entry and contained data entry address.
1186 * Point to the data entry.
1188 dep
= (xfs_dir2_data_entry_t
*)
1189 ((char *)dbp
->b_addr
+
1190 xfs_dir2_dataptr_to_off(dp
->i_mount
, be32_to_cpu(lep
->address
)));
1192 * Return the found inode number & CI name if appropriate
1194 args
->inumber
= be64_to_cpu(dep
->inumber
);
1195 error
= xfs_dir_cilookup_result(args
, dep
->name
, dep
->namelen
);
1196 xfs_trans_brelse(tp
, dbp
);
1197 xfs_trans_brelse(tp
, lbp
);
1198 return XFS_ERROR(error
);
1202 * Look up name/hash in the leaf block.
1203 * Fill in indexp with the found index, and dbpp with the data buffer.
1204 * If not found dbpp will be NULL, and ENOENT comes back.
1205 * lbpp will always be filled in with the leaf buffer unless there's an error.
1207 static int /* error */
1208 xfs_dir2_leaf_lookup_int(
1209 xfs_da_args_t
*args
, /* operation arguments */
1210 struct xfs_buf
**lbpp
, /* out: leaf buffer */
1211 int *indexp
, /* out: index in leaf block */
1212 struct xfs_buf
**dbpp
) /* out: data buffer */
1214 xfs_dir2_db_t curdb
= -1; /* current data block number */
1215 struct xfs_buf
*dbp
= NULL
; /* data buffer */
1216 xfs_dir2_data_entry_t
*dep
; /* data entry */
1217 xfs_inode_t
*dp
; /* incore directory inode */
1218 int error
; /* error return code */
1219 int index
; /* index in leaf block */
1220 struct xfs_buf
*lbp
; /* leaf buffer */
1221 xfs_dir2_leaf_entry_t
*lep
; /* leaf entry */
1222 xfs_dir2_leaf_t
*leaf
; /* leaf structure */
1223 xfs_mount_t
*mp
; /* filesystem mount point */
1224 xfs_dir2_db_t newdb
; /* new data block number */
1225 xfs_trans_t
*tp
; /* transaction pointer */
1226 xfs_dir2_db_t cidb
= -1; /* case match data block no. */
1227 enum xfs_dacmp cmp
; /* name compare result */
1228 struct xfs_dir2_leaf_entry
*ents
;
1229 struct xfs_dir3_icleaf_hdr leafhdr
;
1235 error
= xfs_dir3_leaf_read(tp
, dp
, mp
->m_dirleafblk
, -1, &lbp
);
1241 xfs_dir3_leaf_check(mp
, lbp
);
1242 ents
= xfs_dir3_leaf_ents_p(leaf
);
1243 xfs_dir3_leaf_hdr_from_disk(&leafhdr
, leaf
);
1246 * Look for the first leaf entry with our hash value.
1248 index
= xfs_dir2_leaf_search_hash(args
, lbp
);
1250 * Loop over all the entries with the right hash value
1251 * looking to match the name.
1253 for (lep
= &ents
[index
];
1254 index
< leafhdr
.count
&& be32_to_cpu(lep
->hashval
) == args
->hashval
;
1257 * Skip over stale leaf entries.
1259 if (be32_to_cpu(lep
->address
) == XFS_DIR2_NULL_DATAPTR
)
1262 * Get the new data block number.
1264 newdb
= xfs_dir2_dataptr_to_db(mp
, be32_to_cpu(lep
->address
));
1266 * If it's not the same as the old data block number,
1267 * need to pitch the old one and read the new one.
1269 if (newdb
!= curdb
) {
1271 xfs_trans_brelse(tp
, dbp
);
1272 error
= xfs_dir3_data_read(tp
, dp
,
1273 xfs_dir2_db_to_da(mp
, newdb
),
1276 xfs_trans_brelse(tp
, lbp
);
1282 * Point to the data entry.
1284 dep
= (xfs_dir2_data_entry_t
*)((char *)dbp
->b_addr
+
1285 xfs_dir2_dataptr_to_off(mp
, be32_to_cpu(lep
->address
)));
1287 * Compare name and if it's an exact match, return the index
1288 * and buffer. If it's the first case-insensitive match, store
1289 * the index and buffer and continue looking for an exact match.
1291 cmp
= mp
->m_dirnameops
->compname(args
, dep
->name
, dep
->namelen
);
1292 if (cmp
!= XFS_CMP_DIFFERENT
&& cmp
!= args
->cmpresult
) {
1293 args
->cmpresult
= cmp
;
1295 /* case exact match: return the current buffer. */
1296 if (cmp
== XFS_CMP_EXACT
) {
1303 ASSERT(args
->op_flags
& XFS_DA_OP_OKNOENT
);
1305 * Here, we can only be doing a lookup (not a rename or remove).
1306 * If a case-insensitive match was found earlier, re-read the
1307 * appropriate data block if required and return it.
1309 if (args
->cmpresult
== XFS_CMP_CASE
) {
1311 if (cidb
!= curdb
) {
1312 xfs_trans_brelse(tp
, dbp
);
1313 error
= xfs_dir3_data_read(tp
, dp
,
1314 xfs_dir2_db_to_da(mp
, cidb
),
1317 xfs_trans_brelse(tp
, lbp
);
1325 * No match found, return ENOENT.
1329 xfs_trans_brelse(tp
, dbp
);
1330 xfs_trans_brelse(tp
, lbp
);
1331 return XFS_ERROR(ENOENT
);
1335 * Remove an entry from a leaf format directory.
1338 xfs_dir2_leaf_removename(
1339 xfs_da_args_t
*args
) /* operation arguments */
1341 __be16
*bestsp
; /* leaf block best freespace */
1342 xfs_dir2_data_hdr_t
*hdr
; /* data block header */
1343 xfs_dir2_db_t db
; /* data block number */
1344 struct xfs_buf
*dbp
; /* data block buffer */
1345 xfs_dir2_data_entry_t
*dep
; /* data entry structure */
1346 xfs_inode_t
*dp
; /* incore directory inode */
1347 int error
; /* error return code */
1348 xfs_dir2_db_t i
; /* temporary data block # */
1349 int index
; /* index into leaf entries */
1350 struct xfs_buf
*lbp
; /* leaf buffer */
1351 xfs_dir2_leaf_t
*leaf
; /* leaf structure */
1352 xfs_dir2_leaf_entry_t
*lep
; /* leaf entry */
1353 xfs_dir2_leaf_tail_t
*ltp
; /* leaf tail structure */
1354 xfs_mount_t
*mp
; /* filesystem mount point */
1355 int needlog
; /* need to log data header */
1356 int needscan
; /* need to rescan data frees */
1357 xfs_dir2_data_off_t oldbest
; /* old value of best free */
1358 xfs_trans_t
*tp
; /* transaction pointer */
1359 struct xfs_dir2_data_free
*bf
; /* bestfree table */
1360 struct xfs_dir2_leaf_entry
*ents
;
1361 struct xfs_dir3_icleaf_hdr leafhdr
;
1363 trace_xfs_dir2_leaf_removename(args
);
1366 * Lookup the leaf entry, get the leaf and data blocks read in.
1368 if ((error
= xfs_dir2_leaf_lookup_int(args
, &lbp
, &index
, &dbp
))) {
1376 xfs_dir3_data_check(dp
, dbp
);
1377 bf
= xfs_dir3_data_bestfree_p(hdr
);
1378 xfs_dir3_leaf_hdr_from_disk(&leafhdr
, leaf
);
1379 ents
= xfs_dir3_leaf_ents_p(leaf
);
1381 * Point to the leaf entry, use that to point to the data entry.
1384 db
= xfs_dir2_dataptr_to_db(mp
, be32_to_cpu(lep
->address
));
1385 dep
= (xfs_dir2_data_entry_t
*)
1386 ((char *)hdr
+ xfs_dir2_dataptr_to_off(mp
, be32_to_cpu(lep
->address
)));
1387 needscan
= needlog
= 0;
1388 oldbest
= be16_to_cpu(bf
[0].length
);
1389 ltp
= xfs_dir2_leaf_tail_p(mp
, leaf
);
1390 bestsp
= xfs_dir2_leaf_bests_p(ltp
);
1391 ASSERT(be16_to_cpu(bestsp
[db
]) == oldbest
);
1393 * Mark the former data entry unused.
1395 xfs_dir2_data_make_free(tp
, dbp
,
1396 (xfs_dir2_data_aoff_t
)((char *)dep
- (char *)hdr
),
1397 xfs_dir2_data_entsize(dep
->namelen
), &needlog
, &needscan
);
1399 * We just mark the leaf entry stale by putting a null in it.
1402 xfs_dir3_leaf_hdr_to_disk(leaf
, &leafhdr
);
1403 xfs_dir3_leaf_log_header(tp
, lbp
);
1405 lep
->address
= cpu_to_be32(XFS_DIR2_NULL_DATAPTR
);
1406 xfs_dir3_leaf_log_ents(tp
, lbp
, index
, index
);
1409 * Scan the freespace in the data block again if necessary,
1410 * log the data block header if necessary.
1413 xfs_dir2_data_freescan(mp
, hdr
, &needlog
);
1415 xfs_dir2_data_log_header(tp
, dbp
);
1417 * If the longest freespace in the data block has changed,
1418 * put the new value in the bests table and log that.
1420 if (be16_to_cpu(bf
[0].length
) != oldbest
) {
1421 bestsp
[db
] = bf
[0].length
;
1422 xfs_dir3_leaf_log_bests(tp
, lbp
, db
, db
);
1424 xfs_dir3_data_check(dp
, dbp
);
1426 * If the data block is now empty then get rid of the data block.
1428 if (be16_to_cpu(bf
[0].length
) ==
1429 mp
->m_dirblksize
- xfs_dir3_data_entry_offset(hdr
)) {
1430 ASSERT(db
!= mp
->m_dirdatablk
);
1431 if ((error
= xfs_dir2_shrink_inode(args
, db
, dbp
))) {
1433 * Nope, can't get rid of it because it caused
1434 * allocation of a bmap btree block to do so.
1435 * Just go on, returning success, leaving the
1436 * empty block in place.
1438 if (error
== ENOSPC
&& args
->total
== 0)
1440 xfs_dir3_leaf_check(mp
, lbp
);
1445 * If this is the last data block then compact the
1446 * bests table by getting rid of entries.
1448 if (db
== be32_to_cpu(ltp
->bestcount
) - 1) {
1450 * Look for the last active entry (i).
1452 for (i
= db
- 1; i
> 0; i
--) {
1453 if (bestsp
[i
] != cpu_to_be16(NULLDATAOFF
))
1457 * Copy the table down so inactive entries at the
1460 memmove(&bestsp
[db
- i
], bestsp
,
1461 (be32_to_cpu(ltp
->bestcount
) - (db
- i
)) * sizeof(*bestsp
));
1462 be32_add_cpu(<p
->bestcount
, -(db
- i
));
1463 xfs_dir3_leaf_log_tail(tp
, lbp
);
1464 xfs_dir3_leaf_log_bests(tp
, lbp
, 0, be32_to_cpu(ltp
->bestcount
) - 1);
1466 bestsp
[db
] = cpu_to_be16(NULLDATAOFF
);
1469 * If the data block was not the first one, drop it.
1471 else if (db
!= mp
->m_dirdatablk
)
1474 xfs_dir3_leaf_check(mp
, lbp
);
1476 * See if we can convert to block form.
1478 return xfs_dir2_leaf_to_block(args
, lbp
, dbp
);
1482 * Replace the inode number in a leaf format directory entry.
1485 xfs_dir2_leaf_replace(
1486 xfs_da_args_t
*args
) /* operation arguments */
1488 struct xfs_buf
*dbp
; /* data block buffer */
1489 xfs_dir2_data_entry_t
*dep
; /* data block entry */
1490 xfs_inode_t
*dp
; /* incore directory inode */
1491 int error
; /* error return code */
1492 int index
; /* index of leaf entry */
1493 struct xfs_buf
*lbp
; /* leaf buffer */
1494 xfs_dir2_leaf_t
*leaf
; /* leaf structure */
1495 xfs_dir2_leaf_entry_t
*lep
; /* leaf entry */
1496 xfs_trans_t
*tp
; /* transaction pointer */
1497 struct xfs_dir2_leaf_entry
*ents
;
1499 trace_xfs_dir2_leaf_replace(args
);
1502 * Look up the entry.
1504 if ((error
= xfs_dir2_leaf_lookup_int(args
, &lbp
, &index
, &dbp
))) {
1509 ents
= xfs_dir3_leaf_ents_p(leaf
);
1511 * Point to the leaf entry, get data address from it.
1515 * Point to the data entry.
1517 dep
= (xfs_dir2_data_entry_t
*)
1518 ((char *)dbp
->b_addr
+
1519 xfs_dir2_dataptr_to_off(dp
->i_mount
, be32_to_cpu(lep
->address
)));
1520 ASSERT(args
->inumber
!= be64_to_cpu(dep
->inumber
));
1522 * Put the new inode number in, log it.
1524 dep
->inumber
= cpu_to_be64(args
->inumber
);
1526 xfs_dir2_data_log_entry(tp
, dbp
, dep
);
1527 xfs_dir3_leaf_check(dp
->i_mount
, lbp
);
1528 xfs_trans_brelse(tp
, lbp
);
1533 * Return index in the leaf block (lbp) which is either the first
1534 * one with this hash value, or if there are none, the insert point
1535 * for that hash value.
1537 int /* index value */
1538 xfs_dir2_leaf_search_hash(
1539 xfs_da_args_t
*args
, /* operation arguments */
1540 struct xfs_buf
*lbp
) /* leaf buffer */
1542 xfs_dahash_t hash
=0; /* hash from this entry */
1543 xfs_dahash_t hashwant
; /* hash value looking for */
1544 int high
; /* high leaf index */
1545 int low
; /* low leaf index */
1546 xfs_dir2_leaf_t
*leaf
; /* leaf structure */
1547 xfs_dir2_leaf_entry_t
*lep
; /* leaf entry */
1548 int mid
=0; /* current leaf index */
1549 struct xfs_dir2_leaf_entry
*ents
;
1550 struct xfs_dir3_icleaf_hdr leafhdr
;
1553 ents
= xfs_dir3_leaf_ents_p(leaf
);
1554 xfs_dir3_leaf_hdr_from_disk(&leafhdr
, leaf
);
1561 * Note, the table cannot be empty, so we have to go through the loop.
1562 * Binary search the leaf entries looking for our hash value.
1564 for (lep
= ents
, low
= 0, high
= leafhdr
.count
- 1,
1565 hashwant
= args
->hashval
;
1567 mid
= (low
+ high
) >> 1;
1568 if ((hash
= be32_to_cpu(lep
[mid
].hashval
)) == hashwant
)
1570 if (hash
< hashwant
)
1576 * Found one, back up through all the equal hash values.
1578 if (hash
== hashwant
) {
1579 while (mid
> 0 && be32_to_cpu(lep
[mid
- 1].hashval
) == hashwant
) {
1584 * Need to point to an entry higher than ours.
1586 else if (hash
< hashwant
)
1592 * Trim off a trailing data block. We know it's empty since the leaf
1593 * freespace table says so.
1596 xfs_dir2_leaf_trim_data(
1597 xfs_da_args_t
*args
, /* operation arguments */
1598 struct xfs_buf
*lbp
, /* leaf buffer */
1599 xfs_dir2_db_t db
) /* data block number */
1601 __be16
*bestsp
; /* leaf bests table */
1602 struct xfs_buf
*dbp
; /* data block buffer */
1603 xfs_inode_t
*dp
; /* incore directory inode */
1604 int error
; /* error return value */
1605 xfs_dir2_leaf_t
*leaf
; /* leaf structure */
1606 xfs_dir2_leaf_tail_t
*ltp
; /* leaf tail structure */
1607 xfs_mount_t
*mp
; /* filesystem mount point */
1608 xfs_trans_t
*tp
; /* transaction pointer */
1614 * Read the offending data block. We need its buffer.
1616 error
= xfs_dir3_data_read(tp
, dp
, xfs_dir2_db_to_da(mp
, db
), -1, &dbp
);
1621 ltp
= xfs_dir2_leaf_tail_p(mp
, leaf
);
1625 struct xfs_dir2_data_hdr
*hdr
= dbp
->b_addr
;
1626 struct xfs_dir2_data_free
*bf
= xfs_dir3_data_bestfree_p(hdr
);
1628 ASSERT(hdr
->magic
== cpu_to_be32(XFS_DIR2_DATA_MAGIC
) ||
1629 hdr
->magic
== cpu_to_be32(XFS_DIR3_DATA_MAGIC
));
1630 ASSERT(be16_to_cpu(bf
[0].length
) ==
1631 mp
->m_dirblksize
- xfs_dir3_data_entry_offset(hdr
));
1632 ASSERT(db
== be32_to_cpu(ltp
->bestcount
) - 1);
1637 * Get rid of the data block.
1639 if ((error
= xfs_dir2_shrink_inode(args
, db
, dbp
))) {
1640 ASSERT(error
!= ENOSPC
);
1641 xfs_trans_brelse(tp
, dbp
);
1645 * Eliminate the last bests entry from the table.
1647 bestsp
= xfs_dir2_leaf_bests_p(ltp
);
1648 be32_add_cpu(<p
->bestcount
, -1);
1649 memmove(&bestsp
[1], &bestsp
[0], be32_to_cpu(ltp
->bestcount
) * sizeof(*bestsp
));
1650 xfs_dir3_leaf_log_tail(tp
, lbp
);
1651 xfs_dir3_leaf_log_bests(tp
, lbp
, 0, be32_to_cpu(ltp
->bestcount
) - 1);
1655 static inline size_t
1657 struct xfs_dir3_icleaf_hdr
*hdr
,
1663 entries
= hdr
->count
- hdr
->stale
;
1664 if (hdr
->magic
== XFS_DIR2_LEAF1_MAGIC
||
1665 hdr
->magic
== XFS_DIR2_LEAFN_MAGIC
)
1666 hdrsize
= sizeof(struct xfs_dir2_leaf_hdr
);
1668 hdrsize
= sizeof(struct xfs_dir3_leaf_hdr
);
1670 return hdrsize
+ entries
* sizeof(xfs_dir2_leaf_entry_t
)
1671 + counts
* sizeof(xfs_dir2_data_off_t
)
1672 + sizeof(xfs_dir2_leaf_tail_t
);
1676 * Convert node form directory to leaf form directory.
1677 * The root of the node form dir needs to already be a LEAFN block.
1678 * Just return if we can't do anything.
1681 xfs_dir2_node_to_leaf(
1682 xfs_da_state_t
*state
) /* directory operation state */
1684 xfs_da_args_t
*args
; /* operation arguments */
1685 xfs_inode_t
*dp
; /* incore directory inode */
1686 int error
; /* error return code */
1687 struct xfs_buf
*fbp
; /* buffer for freespace block */
1688 xfs_fileoff_t fo
; /* freespace file offset */
1689 xfs_dir2_free_t
*free
; /* freespace structure */
1690 struct xfs_buf
*lbp
; /* buffer for leaf block */
1691 xfs_dir2_leaf_tail_t
*ltp
; /* tail of leaf structure */
1692 xfs_dir2_leaf_t
*leaf
; /* leaf structure */
1693 xfs_mount_t
*mp
; /* filesystem mount point */
1694 int rval
; /* successful free trim? */
1695 xfs_trans_t
*tp
; /* transaction pointer */
1696 struct xfs_dir3_icleaf_hdr leafhdr
;
1697 struct xfs_dir3_icfree_hdr freehdr
;
1700 * There's more than a leaf level in the btree, so there must
1701 * be multiple leafn blocks. Give up.
1703 if (state
->path
.active
> 1)
1707 trace_xfs_dir2_node_to_leaf(args
);
1713 * Get the last offset in the file.
1715 if ((error
= xfs_bmap_last_offset(tp
, dp
, &fo
, XFS_DATA_FORK
))) {
1718 fo
-= mp
->m_dirblkfsbs
;
1720 * If there are freespace blocks other than the first one,
1721 * take this opportunity to remove trailing empty freespace blocks
1722 * that may have been left behind during no-space-reservation
1725 while (fo
> mp
->m_dirfreeblk
) {
1726 if ((error
= xfs_dir2_node_trim_free(args
, fo
, &rval
))) {
1730 fo
-= mp
->m_dirblkfsbs
;
1735 * Now find the block just before the freespace block.
1737 if ((error
= xfs_bmap_last_before(tp
, dp
, &fo
, XFS_DATA_FORK
))) {
1741 * If it's not the single leaf block, give up.
1743 if (XFS_FSB_TO_B(mp
, fo
) > XFS_DIR2_LEAF_OFFSET
+ mp
->m_dirblksize
)
1745 lbp
= state
->path
.blk
[0].bp
;
1747 xfs_dir3_leaf_hdr_from_disk(&leafhdr
, leaf
);
1749 ASSERT(leafhdr
.magic
== XFS_DIR2_LEAFN_MAGIC
||
1750 leafhdr
.magic
== XFS_DIR3_LEAFN_MAGIC
);
1753 * Read the freespace block.
1755 error
= xfs_dir2_free_read(tp
, dp
, mp
->m_dirfreeblk
, &fbp
);
1759 xfs_dir3_free_hdr_from_disk(&freehdr
, free
);
1761 ASSERT(!freehdr
.firstdb
);
1764 * Now see if the leafn and free data will fit in a leaf1.
1765 * If not, release the buffer and give up.
1767 if (xfs_dir3_leaf_size(&leafhdr
, freehdr
.nvalid
) > mp
->m_dirblksize
) {
1768 xfs_trans_brelse(tp
, fbp
);
1773 * If the leaf has any stale entries in it, compress them out.
1776 xfs_dir3_leaf_compact(args
, &leafhdr
, lbp
);
1778 lbp
->b_ops
= &xfs_dir3_leaf1_buf_ops
;
1779 leafhdr
.magic
= (leafhdr
.magic
== XFS_DIR2_LEAFN_MAGIC
)
1780 ? XFS_DIR2_LEAF1_MAGIC
1781 : XFS_DIR3_LEAF1_MAGIC
;
1784 * Set up the leaf tail from the freespace block.
1786 ltp
= xfs_dir2_leaf_tail_p(mp
, leaf
);
1787 ltp
->bestcount
= cpu_to_be32(freehdr
.nvalid
);
1790 * Set up the leaf bests table.
1792 memcpy(xfs_dir2_leaf_bests_p(ltp
), xfs_dir3_free_bests_p(mp
, free
),
1793 freehdr
.nvalid
* sizeof(xfs_dir2_data_off_t
));
1795 xfs_dir3_leaf_hdr_to_disk(leaf
, &leafhdr
);
1796 xfs_dir3_leaf_log_header(tp
, lbp
);
1797 xfs_dir3_leaf_log_bests(tp
, lbp
, 0, be32_to_cpu(ltp
->bestcount
) - 1);
1798 xfs_dir3_leaf_log_tail(tp
, lbp
);
1799 xfs_dir3_leaf_check(mp
, lbp
);
1802 * Get rid of the freespace block.
1804 error
= xfs_dir2_shrink_inode(args
, XFS_DIR2_FREE_FIRSTDB(mp
), fbp
);
1807 * This can't fail here because it can only happen when
1808 * punching out the middle of an extent, and this is an
1811 ASSERT(error
!= ENOSPC
);
1816 * Now see if we can convert the single-leaf directory
1817 * down to a block form directory.
1818 * This routine always kills the dabuf for the leaf, so
1819 * eliminate it from the path.
1821 error
= xfs_dir2_leaf_to_block(args
, lbp
, NULL
);
1822 state
->path
.blk
[0].bp
= NULL
;