1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2000-2005 Silicon Graphics, Inc.
4 * Copyright (c) 2013 Red Hat, Inc.
7 #include "libxfs_priv.h"
9 #include "xfs_shared.h"
10 #include "xfs_format.h"
11 #include "xfs_log_format.h"
12 #include "xfs_trans_resv.h"
13 #include "xfs_mount.h"
14 #include "xfs_inode.h"
17 #include "xfs_dir2_priv.h"
18 #include "xfs_trace.h"
19 #include "xfs_trans.h"
22 * Function declarations.
24 static int xfs_dir2_leafn_add(struct xfs_buf
*bp
, xfs_da_args_t
*args
,
26 static void xfs_dir2_leafn_rebalance(xfs_da_state_t
*state
,
27 xfs_da_state_blk_t
*blk1
,
28 xfs_da_state_blk_t
*blk2
);
29 static int xfs_dir2_leafn_remove(xfs_da_args_t
*args
, struct xfs_buf
*bp
,
30 int index
, xfs_da_state_blk_t
*dblk
,
32 static int xfs_dir2_node_addname_int(xfs_da_args_t
*args
,
33 xfs_da_state_blk_t
*fblk
);
36 * Check internal consistency of a leafn block.
44 struct xfs_dir2_leaf
*leaf
= bp
->b_addr
;
45 struct xfs_dir3_icleaf_hdr leafhdr
;
47 dp
->d_ops
->leaf_hdr_from_disk(&leafhdr
, leaf
);
49 if (leafhdr
.magic
== XFS_DIR3_LEAFN_MAGIC
) {
50 struct xfs_dir3_leaf_hdr
*leaf3
= bp
->b_addr
;
51 if (be64_to_cpu(leaf3
->info
.blkno
) != bp
->b_bn
)
52 return __this_address
;
53 } else if (leafhdr
.magic
!= XFS_DIR2_LEAFN_MAGIC
)
54 return __this_address
;
56 return xfs_dir3_leaf_check_int(dp
->i_mount
, dp
, &leafhdr
, leaf
);
66 fa
= xfs_dir3_leafn_check(dp
, bp
);
69 xfs_corruption_error(__func__
, XFS_ERRLEVEL_LOW
, dp
->i_mount
,
70 bp
->b_addr
, BBTOB(bp
->b_length
), __FILE__
, __LINE__
,
75 #define xfs_dir3_leaf_check(dp, bp)
82 struct xfs_mount
*mp
= bp
->b_mount
;
83 struct xfs_dir2_free_hdr
*hdr
= bp
->b_addr
;
85 if (!xfs_verify_magic(bp
, hdr
->magic
))
86 return __this_address
;
88 if (xfs_sb_version_hascrc(&mp
->m_sb
)) {
89 struct xfs_dir3_blk_hdr
*hdr3
= bp
->b_addr
;
91 if (!uuid_equal(&hdr3
->uuid
, &mp
->m_sb
.sb_meta_uuid
))
92 return __this_address
;
93 if (be64_to_cpu(hdr3
->blkno
) != bp
->b_bn
)
94 return __this_address
;
95 if (!xfs_log_check_lsn(mp
, be64_to_cpu(hdr3
->lsn
)))
96 return __this_address
;
99 /* XXX: should bounds check the xfs_dir3_icfree_hdr here */
105 xfs_dir3_free_read_verify(
108 struct xfs_mount
*mp
= bp
->b_mount
;
111 if (xfs_sb_version_hascrc(&mp
->m_sb
) &&
112 !xfs_buf_verify_cksum(bp
, XFS_DIR3_FREE_CRC_OFF
))
113 xfs_verifier_error(bp
, -EFSBADCRC
, __this_address
);
115 fa
= xfs_dir3_free_verify(bp
);
117 xfs_verifier_error(bp
, -EFSCORRUPTED
, fa
);
122 xfs_dir3_free_write_verify(
125 struct xfs_mount
*mp
= bp
->b_mount
;
126 struct xfs_buf_log_item
*bip
= bp
->b_log_item
;
127 struct xfs_dir3_blk_hdr
*hdr3
= bp
->b_addr
;
130 fa
= xfs_dir3_free_verify(bp
);
132 xfs_verifier_error(bp
, -EFSCORRUPTED
, fa
);
136 if (!xfs_sb_version_hascrc(&mp
->m_sb
))
140 hdr3
->lsn
= cpu_to_be64(bip
->bli_item
.li_lsn
);
142 xfs_buf_update_cksum(bp
, XFS_DIR3_FREE_CRC_OFF
);
145 const struct xfs_buf_ops xfs_dir3_free_buf_ops
= {
146 .name
= "xfs_dir3_free",
147 .magic
= { cpu_to_be32(XFS_DIR2_FREE_MAGIC
),
148 cpu_to_be32(XFS_DIR3_FREE_MAGIC
) },
149 .verify_read
= xfs_dir3_free_read_verify
,
150 .verify_write
= xfs_dir3_free_write_verify
,
151 .verify_struct
= xfs_dir3_free_verify
,
154 /* Everything ok in the free block header? */
155 static xfs_failaddr_t
156 xfs_dir3_free_header_check(
157 struct xfs_inode
*dp
,
161 struct xfs_mount
*mp
= dp
->i_mount
;
162 unsigned int firstdb
;
165 maxbests
= dp
->d_ops
->free_max_bests(mp
->m_dir_geo
);
166 firstdb
= (xfs_dir2_da_to_db(mp
->m_dir_geo
, fbno
) -
167 xfs_dir2_byte_to_db(mp
->m_dir_geo
, XFS_DIR2_FREE_OFFSET
)) *
169 if (xfs_sb_version_hascrc(&mp
->m_sb
)) {
170 struct xfs_dir3_free_hdr
*hdr3
= bp
->b_addr
;
172 if (be32_to_cpu(hdr3
->firstdb
) != firstdb
)
173 return __this_address
;
174 if (be32_to_cpu(hdr3
->nvalid
) > maxbests
)
175 return __this_address
;
176 if (be32_to_cpu(hdr3
->nvalid
) < be32_to_cpu(hdr3
->nused
))
177 return __this_address
;
179 struct xfs_dir2_free_hdr
*hdr
= bp
->b_addr
;
181 if (be32_to_cpu(hdr
->firstdb
) != firstdb
)
182 return __this_address
;
183 if (be32_to_cpu(hdr
->nvalid
) > maxbests
)
184 return __this_address
;
185 if (be32_to_cpu(hdr
->nvalid
) < be32_to_cpu(hdr
->nused
))
186 return __this_address
;
192 __xfs_dir3_free_read(
193 struct xfs_trans
*tp
,
194 struct xfs_inode
*dp
,
196 xfs_daddr_t mappedbno
,
197 struct xfs_buf
**bpp
)
202 err
= xfs_da_read_buf(tp
, dp
, fbno
, mappedbno
, bpp
,
203 XFS_DATA_FORK
, &xfs_dir3_free_buf_ops
);
207 /* Check things that we can't do in the verifier. */
208 fa
= xfs_dir3_free_header_check(dp
, fbno
, *bpp
);
210 xfs_verifier_error(*bpp
, -EFSCORRUPTED
, fa
);
211 xfs_trans_brelse(tp
, *bpp
);
212 return -EFSCORRUPTED
;
215 /* try read returns without an error or *bpp if it lands in a hole */
217 xfs_trans_buf_set_type(tp
, *bpp
, XFS_BLFT_DIR_FREE_BUF
);
224 struct xfs_trans
*tp
,
225 struct xfs_inode
*dp
,
227 struct xfs_buf
**bpp
)
229 return __xfs_dir3_free_read(tp
, dp
, fbno
, -1, bpp
);
233 xfs_dir2_free_try_read(
234 struct xfs_trans
*tp
,
235 struct xfs_inode
*dp
,
237 struct xfs_buf
**bpp
)
239 return __xfs_dir3_free_read(tp
, dp
, fbno
, -2, bpp
);
243 xfs_dir3_free_get_buf(
246 struct xfs_buf
**bpp
)
248 struct xfs_trans
*tp
= args
->trans
;
249 struct xfs_inode
*dp
= args
->dp
;
250 struct xfs_mount
*mp
= dp
->i_mount
;
253 struct xfs_dir3_icfree_hdr hdr
;
255 error
= xfs_da_get_buf(tp
, dp
, xfs_dir2_db_to_da(args
->geo
, fbno
),
256 -1, &bp
, XFS_DATA_FORK
);
260 xfs_trans_buf_set_type(tp
, bp
, XFS_BLFT_DIR_FREE_BUF
);
261 bp
->b_ops
= &xfs_dir3_free_buf_ops
;
264 * Initialize the new block to be empty, and remember
265 * its first slot as our empty slot.
267 memset(bp
->b_addr
, 0, sizeof(struct xfs_dir3_free_hdr
));
268 memset(&hdr
, 0, sizeof(hdr
));
270 if (xfs_sb_version_hascrc(&mp
->m_sb
)) {
271 struct xfs_dir3_free_hdr
*hdr3
= bp
->b_addr
;
273 hdr
.magic
= XFS_DIR3_FREE_MAGIC
;
275 hdr3
->hdr
.blkno
= cpu_to_be64(bp
->b_bn
);
276 hdr3
->hdr
.owner
= cpu_to_be64(dp
->i_ino
);
277 uuid_copy(&hdr3
->hdr
.uuid
, &mp
->m_sb
.sb_meta_uuid
);
279 hdr
.magic
= XFS_DIR2_FREE_MAGIC
;
280 dp
->d_ops
->free_hdr_to_disk(bp
->b_addr
, &hdr
);
286 * Log entries from a freespace block.
289 xfs_dir2_free_log_bests(
290 struct xfs_da_args
*args
,
292 int first
, /* first entry to log */
293 int last
) /* last entry to log */
295 xfs_dir2_free_t
*free
; /* freespace structure */
299 bests
= args
->dp
->d_ops
->free_bests_p(free
);
300 ASSERT(free
->hdr
.magic
== cpu_to_be32(XFS_DIR2_FREE_MAGIC
) ||
301 free
->hdr
.magic
== cpu_to_be32(XFS_DIR3_FREE_MAGIC
));
302 xfs_trans_log_buf(args
->trans
, bp
,
303 (uint
)((char *)&bests
[first
] - (char *)free
),
304 (uint
)((char *)&bests
[last
] - (char *)free
+
305 sizeof(bests
[0]) - 1));
309 * Log header from a freespace block.
312 xfs_dir2_free_log_header(
313 struct xfs_da_args
*args
,
317 xfs_dir2_free_t
*free
; /* freespace structure */
320 ASSERT(free
->hdr
.magic
== cpu_to_be32(XFS_DIR2_FREE_MAGIC
) ||
321 free
->hdr
.magic
== cpu_to_be32(XFS_DIR3_FREE_MAGIC
));
323 xfs_trans_log_buf(args
->trans
, bp
, 0,
324 args
->dp
->d_ops
->free_hdr_size
- 1);
328 * Convert a leaf-format directory to a node-format directory.
329 * We need to change the magic number of the leaf block, and copy
330 * the freespace table out of the leaf block into its own block.
333 xfs_dir2_leaf_to_node(
334 xfs_da_args_t
*args
, /* operation arguments */
335 struct xfs_buf
*lbp
) /* leaf buffer */
337 xfs_inode_t
*dp
; /* incore directory inode */
338 int error
; /* error return value */
339 struct xfs_buf
*fbp
; /* freespace buffer */
340 xfs_dir2_db_t fdb
; /* freespace block number */
341 xfs_dir2_free_t
*free
; /* freespace structure */
342 __be16
*from
; /* pointer to freespace entry */
343 int i
; /* leaf freespace index */
344 xfs_dir2_leaf_t
*leaf
; /* leaf structure */
345 xfs_dir2_leaf_tail_t
*ltp
; /* leaf tail structure */
346 int n
; /* count of live freespc ents */
347 xfs_dir2_data_off_t off
; /* freespace entry value */
348 __be16
*to
; /* pointer to freespace entry */
349 xfs_trans_t
*tp
; /* transaction pointer */
350 struct xfs_dir3_icfree_hdr freehdr
;
352 trace_xfs_dir2_leaf_to_node(args
);
357 * Add a freespace block to the directory.
359 if ((error
= xfs_dir2_grow_inode(args
, XFS_DIR2_FREE_SPACE
, &fdb
))) {
362 ASSERT(fdb
== xfs_dir2_byte_to_db(args
->geo
, XFS_DIR2_FREE_OFFSET
));
364 * Get the buffer for the new freespace block.
366 error
= xfs_dir3_free_get_buf(args
, fdb
, &fbp
);
371 dp
->d_ops
->free_hdr_from_disk(&freehdr
, free
);
373 ltp
= xfs_dir2_leaf_tail_p(args
->geo
, leaf
);
374 if (be32_to_cpu(ltp
->bestcount
) >
375 (uint
)dp
->i_d
.di_size
/ args
->geo
->blksize
)
376 return -EFSCORRUPTED
;
379 * Copy freespace entries from the leaf block to the new block.
380 * Count active entries.
382 from
= xfs_dir2_leaf_bests_p(ltp
);
383 to
= dp
->d_ops
->free_bests_p(free
);
384 for (i
= n
= 0; i
< be32_to_cpu(ltp
->bestcount
); i
++, from
++, to
++) {
385 if ((off
= be16_to_cpu(*from
)) != NULLDATAOFF
)
387 *to
= cpu_to_be16(off
);
391 * Now initialize the freespace block header.
394 freehdr
.nvalid
= be32_to_cpu(ltp
->bestcount
);
396 dp
->d_ops
->free_hdr_to_disk(fbp
->b_addr
, &freehdr
);
397 xfs_dir2_free_log_bests(args
, fbp
, 0, freehdr
.nvalid
- 1);
398 xfs_dir2_free_log_header(args
, fbp
);
401 * Converting the leaf to a leafnode is just a matter of changing the
402 * magic number and the ops. Do the change directly to the buffer as
403 * it's less work (and less code) than decoding the header to host
404 * format and back again.
406 if (leaf
->hdr
.info
.magic
== cpu_to_be16(XFS_DIR2_LEAF1_MAGIC
))
407 leaf
->hdr
.info
.magic
= cpu_to_be16(XFS_DIR2_LEAFN_MAGIC
);
409 leaf
->hdr
.info
.magic
= cpu_to_be16(XFS_DIR3_LEAFN_MAGIC
);
410 lbp
->b_ops
= &xfs_dir3_leafn_buf_ops
;
411 xfs_trans_buf_set_type(tp
, lbp
, XFS_BLFT_DIR_LEAFN_BUF
);
412 xfs_dir3_leaf_log_header(args
, lbp
);
413 xfs_dir3_leaf_check(dp
, lbp
);
418 * Add a leaf entry to a leaf block in a node-form directory.
419 * The other work necessary is done from the caller.
421 static int /* error */
423 struct xfs_buf
*bp
, /* leaf buffer */
424 struct xfs_da_args
*args
, /* operation arguments */
425 int index
) /* insertion pt for new entry */
427 struct xfs_dir3_icleaf_hdr leafhdr
;
428 struct xfs_inode
*dp
= args
->dp
;
429 struct xfs_dir2_leaf
*leaf
= bp
->b_addr
;
430 struct xfs_dir2_leaf_entry
*lep
;
431 struct xfs_dir2_leaf_entry
*ents
;
432 int compact
; /* compacting stale leaves */
433 int highstale
= 0; /* next stale entry */
434 int lfloghigh
; /* high leaf entry logging */
435 int lfloglow
; /* low leaf entry logging */
436 int lowstale
= 0; /* previous stale entry */
438 trace_xfs_dir2_leafn_add(args
, index
);
440 dp
->d_ops
->leaf_hdr_from_disk(&leafhdr
, leaf
);
441 ents
= dp
->d_ops
->leaf_ents_p(leaf
);
444 * Quick check just to make sure we are not going to index
445 * into other peoples memory
448 return -EFSCORRUPTED
;
451 * If there are already the maximum number of leaf entries in
452 * the block, if there are no stale entries it won't fit.
453 * Caller will do a split. If there are stale entries we'll do
457 if (leafhdr
.count
== dp
->d_ops
->leaf_max_ents(args
->geo
)) {
460 compact
= leafhdr
.stale
> 1;
463 ASSERT(index
== 0 || be32_to_cpu(ents
[index
- 1].hashval
) <= args
->hashval
);
464 ASSERT(index
== leafhdr
.count
||
465 be32_to_cpu(ents
[index
].hashval
) >= args
->hashval
);
467 if (args
->op_flags
& XFS_DA_OP_JUSTCHECK
)
471 * Compact out all but one stale leaf entry. Leaves behind
472 * the entry closest to index.
475 xfs_dir3_leaf_compact_x1(&leafhdr
, ents
, &index
, &lowstale
,
476 &highstale
, &lfloglow
, &lfloghigh
);
477 else if (leafhdr
.stale
) {
479 * Set impossible logging indices for this case.
481 lfloglow
= leafhdr
.count
;
486 * Insert the new entry, log everything.
488 lep
= xfs_dir3_leaf_find_entry(&leafhdr
, ents
, index
, compact
, lowstale
,
489 highstale
, &lfloglow
, &lfloghigh
);
491 lep
->hashval
= cpu_to_be32(args
->hashval
);
492 lep
->address
= cpu_to_be32(xfs_dir2_db_off_to_dataptr(args
->geo
,
493 args
->blkno
, args
->index
));
495 dp
->d_ops
->leaf_hdr_to_disk(leaf
, &leafhdr
);
496 xfs_dir3_leaf_log_header(args
, bp
);
497 xfs_dir3_leaf_log_ents(args
, bp
, lfloglow
, lfloghigh
);
498 xfs_dir3_leaf_check(dp
, bp
);
504 xfs_dir2_free_hdr_check(
505 struct xfs_inode
*dp
,
509 struct xfs_dir3_icfree_hdr hdr
;
511 dp
->d_ops
->free_hdr_from_disk(&hdr
, bp
->b_addr
);
513 ASSERT((hdr
.firstdb
%
514 dp
->d_ops
->free_max_bests(dp
->i_mount
->m_dir_geo
)) == 0);
515 ASSERT(hdr
.firstdb
<= db
);
516 ASSERT(db
< hdr
.firstdb
+ hdr
.nvalid
);
519 #define xfs_dir2_free_hdr_check(dp, bp, db)
523 * Return the last hash value in the leaf.
524 * Stale entries are ok.
526 xfs_dahash_t
/* hash value */
527 xfs_dir2_leaf_lasthash(
528 struct xfs_inode
*dp
,
529 struct xfs_buf
*bp
, /* leaf buffer */
530 int *count
) /* count of entries in leaf */
532 struct xfs_dir2_leaf
*leaf
= bp
->b_addr
;
533 struct xfs_dir2_leaf_entry
*ents
;
534 struct xfs_dir3_icleaf_hdr leafhdr
;
536 dp
->d_ops
->leaf_hdr_from_disk(&leafhdr
, leaf
);
538 ASSERT(leafhdr
.magic
== XFS_DIR2_LEAFN_MAGIC
||
539 leafhdr
.magic
== XFS_DIR3_LEAFN_MAGIC
||
540 leafhdr
.magic
== XFS_DIR2_LEAF1_MAGIC
||
541 leafhdr
.magic
== XFS_DIR3_LEAF1_MAGIC
);
544 *count
= leafhdr
.count
;
548 ents
= dp
->d_ops
->leaf_ents_p(leaf
);
549 return be32_to_cpu(ents
[leafhdr
.count
- 1].hashval
);
553 * Look up a leaf entry for space to add a name in a node-format leaf block.
554 * The extrablk in state is a freespace block.
557 xfs_dir2_leafn_lookup_for_addname(
558 struct xfs_buf
*bp
, /* leaf buffer */
559 xfs_da_args_t
*args
, /* operation arguments */
560 int *indexp
, /* out: leaf entry index */
561 xfs_da_state_t
*state
) /* state to fill in */
563 struct xfs_buf
*curbp
= NULL
; /* current data/free buffer */
564 xfs_dir2_db_t curdb
= -1; /* current data block number */
565 xfs_dir2_db_t curfdb
= -1; /* current free block number */
566 xfs_inode_t
*dp
; /* incore directory inode */
567 int error
; /* error return value */
568 int fi
; /* free entry index */
569 xfs_dir2_free_t
*free
= NULL
; /* free block structure */
570 int index
; /* leaf entry index */
571 xfs_dir2_leaf_t
*leaf
; /* leaf structure */
572 int length
; /* length of new data entry */
573 xfs_dir2_leaf_entry_t
*lep
; /* leaf entry */
574 xfs_mount_t
*mp
; /* filesystem mount point */
575 xfs_dir2_db_t newdb
; /* new data block number */
576 xfs_dir2_db_t newfdb
; /* new free block number */
577 xfs_trans_t
*tp
; /* transaction pointer */
578 struct xfs_dir2_leaf_entry
*ents
;
579 struct xfs_dir3_icleaf_hdr leafhdr
;
585 dp
->d_ops
->leaf_hdr_from_disk(&leafhdr
, leaf
);
586 ents
= dp
->d_ops
->leaf_ents_p(leaf
);
588 xfs_dir3_leaf_check(dp
, bp
);
589 ASSERT(leafhdr
.count
> 0);
592 * Look up the hash value in the leaf entries.
594 index
= xfs_dir2_leaf_search_hash(args
, bp
);
596 * Do we have a buffer coming in?
598 if (state
->extravalid
) {
599 /* If so, it's a free block buffer, get the block number. */
600 curbp
= state
->extrablk
.bp
;
601 curfdb
= state
->extrablk
.blkno
;
602 free
= curbp
->b_addr
;
603 ASSERT(free
->hdr
.magic
== cpu_to_be32(XFS_DIR2_FREE_MAGIC
) ||
604 free
->hdr
.magic
== cpu_to_be32(XFS_DIR3_FREE_MAGIC
));
606 length
= dp
->d_ops
->data_entsize(args
->namelen
);
608 * Loop over leaf entries with the right hash value.
610 for (lep
= &ents
[index
];
611 index
< leafhdr
.count
&& be32_to_cpu(lep
->hashval
) == args
->hashval
;
614 * Skip stale leaf entries.
616 if (be32_to_cpu(lep
->address
) == XFS_DIR2_NULL_DATAPTR
)
619 * Pull the data block number from the entry.
621 newdb
= xfs_dir2_dataptr_to_db(args
->geo
,
622 be32_to_cpu(lep
->address
));
624 * For addname, we're looking for a place to put the new entry.
625 * We want to use a data block with an entry of equal
626 * hash value to ours if there is one with room.
628 * If this block isn't the data block we already have
629 * in hand, take a look at it.
631 if (newdb
!= curdb
) {
636 * Convert the data block to the free block
637 * holding its freespace information.
639 newfdb
= dp
->d_ops
->db_to_fdb(args
->geo
, newdb
);
641 * If it's not the one we have in hand, read it in.
643 if (newfdb
!= curfdb
) {
645 * If we had one before, drop it.
648 xfs_trans_brelse(tp
, curbp
);
650 error
= xfs_dir2_free_read(tp
, dp
,
651 xfs_dir2_db_to_da(args
->geo
,
656 free
= curbp
->b_addr
;
658 xfs_dir2_free_hdr_check(dp
, curbp
, curdb
);
661 * Get the index for our entry.
663 fi
= dp
->d_ops
->db_to_fdindex(args
->geo
, curdb
);
665 * If it has room, return it.
667 bests
= dp
->d_ops
->free_bests_p(free
);
668 if (unlikely(bests
[fi
] == cpu_to_be16(NULLDATAOFF
))) {
669 XFS_ERROR_REPORT("xfs_dir2_leafn_lookup_int",
670 XFS_ERRLEVEL_LOW
, mp
);
671 if (curfdb
!= newfdb
)
672 xfs_trans_brelse(tp
, curbp
);
673 return -EFSCORRUPTED
;
676 if (be16_to_cpu(bests
[fi
]) >= length
)
680 /* Didn't find any space */
683 ASSERT(args
->op_flags
& XFS_DA_OP_OKNOENT
);
685 /* Giving back a free block. */
686 state
->extravalid
= 1;
687 state
->extrablk
.bp
= curbp
;
688 state
->extrablk
.index
= fi
;
689 state
->extrablk
.blkno
= curfdb
;
692 * Important: this magic number is not in the buffer - it's for
693 * buffer type information and therefore only the free/data type
694 * matters here, not whether CRCs are enabled or not.
696 state
->extrablk
.magic
= XFS_DIR2_FREE_MAGIC
;
698 state
->extravalid
= 0;
701 * Return the index, that will be the insertion point.
708 * Look up a leaf entry in a node-format leaf block.
709 * The extrablk in state a data block.
712 xfs_dir2_leafn_lookup_for_entry(
713 struct xfs_buf
*bp
, /* leaf buffer */
714 xfs_da_args_t
*args
, /* operation arguments */
715 int *indexp
, /* out: leaf entry index */
716 xfs_da_state_t
*state
) /* state to fill in */
718 struct xfs_buf
*curbp
= NULL
; /* current data/free buffer */
719 xfs_dir2_db_t curdb
= -1; /* current data block number */
720 xfs_dir2_data_entry_t
*dep
; /* data block entry */
721 xfs_inode_t
*dp
; /* incore directory inode */
722 int error
; /* error return value */
723 int index
; /* leaf entry index */
724 xfs_dir2_leaf_t
*leaf
; /* leaf structure */
725 xfs_dir2_leaf_entry_t
*lep
; /* leaf entry */
726 xfs_mount_t
*mp
; /* filesystem mount point */
727 xfs_dir2_db_t newdb
; /* new data block number */
728 xfs_trans_t
*tp
; /* transaction pointer */
729 enum xfs_dacmp cmp
; /* comparison result */
730 struct xfs_dir2_leaf_entry
*ents
;
731 struct xfs_dir3_icleaf_hdr leafhdr
;
737 dp
->d_ops
->leaf_hdr_from_disk(&leafhdr
, leaf
);
738 ents
= dp
->d_ops
->leaf_ents_p(leaf
);
740 xfs_dir3_leaf_check(dp
, bp
);
741 if (leafhdr
.count
<= 0)
742 return -EFSCORRUPTED
;
745 * Look up the hash value in the leaf entries.
747 index
= xfs_dir2_leaf_search_hash(args
, bp
);
749 * Do we have a buffer coming in?
751 if (state
->extravalid
) {
752 curbp
= state
->extrablk
.bp
;
753 curdb
= state
->extrablk
.blkno
;
756 * Loop over leaf entries with the right hash value.
758 for (lep
= &ents
[index
];
759 index
< leafhdr
.count
&& be32_to_cpu(lep
->hashval
) == args
->hashval
;
762 * Skip stale leaf entries.
764 if (be32_to_cpu(lep
->address
) == XFS_DIR2_NULL_DATAPTR
)
767 * Pull the data block number from the entry.
769 newdb
= xfs_dir2_dataptr_to_db(args
->geo
,
770 be32_to_cpu(lep
->address
));
772 * Not adding a new entry, so we really want to find
773 * the name given to us.
775 * If it's a different data block, go get it.
777 if (newdb
!= curdb
) {
779 * If we had a block before that we aren't saving
780 * for a CI name, drop it
782 if (curbp
&& (args
->cmpresult
== XFS_CMP_DIFFERENT
||
783 curdb
!= state
->extrablk
.blkno
))
784 xfs_trans_brelse(tp
, curbp
);
786 * If needing the block that is saved with a CI match,
787 * use it otherwise read in the new data block.
789 if (args
->cmpresult
!= XFS_CMP_DIFFERENT
&&
790 newdb
== state
->extrablk
.blkno
) {
791 ASSERT(state
->extravalid
);
792 curbp
= state
->extrablk
.bp
;
794 error
= xfs_dir3_data_read(tp
, dp
,
795 xfs_dir2_db_to_da(args
->geo
,
801 xfs_dir3_data_check(dp
, curbp
);
805 * Point to the data entry.
807 dep
= (xfs_dir2_data_entry_t
*)((char *)curbp
->b_addr
+
808 xfs_dir2_dataptr_to_off(args
->geo
,
809 be32_to_cpu(lep
->address
)));
811 * Compare the entry and if it's an exact match, return
812 * EEXIST immediately. If it's the first case-insensitive
813 * match, store the block & inode number and continue looking.
815 cmp
= mp
->m_dirnameops
->compname(args
, dep
->name
, dep
->namelen
);
816 if (cmp
!= XFS_CMP_DIFFERENT
&& cmp
!= args
->cmpresult
) {
817 /* If there is a CI match block, drop it */
818 if (args
->cmpresult
!= XFS_CMP_DIFFERENT
&&
819 curdb
!= state
->extrablk
.blkno
)
820 xfs_trans_brelse(tp
, state
->extrablk
.bp
);
821 args
->cmpresult
= cmp
;
822 args
->inumber
= be64_to_cpu(dep
->inumber
);
823 args
->filetype
= dp
->d_ops
->data_get_ftype(dep
);
825 state
->extravalid
= 1;
826 state
->extrablk
.bp
= curbp
;
827 state
->extrablk
.blkno
= curdb
;
828 state
->extrablk
.index
= (int)((char *)dep
-
829 (char *)curbp
->b_addr
);
830 state
->extrablk
.magic
= XFS_DIR2_DATA_MAGIC
;
831 curbp
->b_ops
= &xfs_dir3_data_buf_ops
;
832 xfs_trans_buf_set_type(tp
, curbp
, XFS_BLFT_DIR_DATA_BUF
);
833 if (cmp
== XFS_CMP_EXACT
)
837 ASSERT(index
== leafhdr
.count
|| (args
->op_flags
& XFS_DA_OP_OKNOENT
));
839 if (args
->cmpresult
== XFS_CMP_DIFFERENT
) {
840 /* Giving back last used data block. */
841 state
->extravalid
= 1;
842 state
->extrablk
.bp
= curbp
;
843 state
->extrablk
.index
= -1;
844 state
->extrablk
.blkno
= curdb
;
845 state
->extrablk
.magic
= XFS_DIR2_DATA_MAGIC
;
846 curbp
->b_ops
= &xfs_dir3_data_buf_ops
;
847 xfs_trans_buf_set_type(tp
, curbp
, XFS_BLFT_DIR_DATA_BUF
);
849 /* If the curbp is not the CI match block, drop it */
850 if (state
->extrablk
.bp
!= curbp
)
851 xfs_trans_brelse(tp
, curbp
);
854 state
->extravalid
= 0;
861 * Look up a leaf entry in a node-format leaf block.
862 * If this is an addname then the extrablk in state is a freespace block,
863 * otherwise it's a data block.
866 xfs_dir2_leafn_lookup_int(
867 struct xfs_buf
*bp
, /* leaf buffer */
868 xfs_da_args_t
*args
, /* operation arguments */
869 int *indexp
, /* out: leaf entry index */
870 xfs_da_state_t
*state
) /* state to fill in */
872 if (args
->op_flags
& XFS_DA_OP_ADDNAME
)
873 return xfs_dir2_leafn_lookup_for_addname(bp
, args
, indexp
,
875 return xfs_dir2_leafn_lookup_for_entry(bp
, args
, indexp
, state
);
879 * Move count leaf entries from source to destination leaf.
880 * Log entries and headers. Stale entries are preserved.
883 xfs_dir3_leafn_moveents(
884 xfs_da_args_t
*args
, /* operation arguments */
885 struct xfs_buf
*bp_s
, /* source */
886 struct xfs_dir3_icleaf_hdr
*shdr
,
887 struct xfs_dir2_leaf_entry
*sents
,
888 int start_s
,/* source leaf index */
889 struct xfs_buf
*bp_d
, /* destination */
890 struct xfs_dir3_icleaf_hdr
*dhdr
,
891 struct xfs_dir2_leaf_entry
*dents
,
892 int start_d
,/* destination leaf index */
893 int count
) /* count of leaves to copy */
895 int stale
; /* count stale leaves copied */
897 trace_xfs_dir2_leafn_moveents(args
, start_s
, start_d
, count
);
900 * Silently return if nothing to do.
906 * If the destination index is not the end of the current
907 * destination leaf entries, open up a hole in the destination
908 * to hold the new entries.
910 if (start_d
< dhdr
->count
) {
911 memmove(&dents
[start_d
+ count
], &dents
[start_d
],
912 (dhdr
->count
- start_d
) * sizeof(xfs_dir2_leaf_entry_t
));
913 xfs_dir3_leaf_log_ents(args
, bp_d
, start_d
+ count
,
914 count
+ dhdr
->count
- 1);
917 * If the source has stale leaves, count the ones in the copy range
918 * so we can update the header correctly.
921 int i
; /* temp leaf index */
923 for (i
= start_s
, stale
= 0; i
< start_s
+ count
; i
++) {
924 if (sents
[i
].address
==
925 cpu_to_be32(XFS_DIR2_NULL_DATAPTR
))
931 * Copy the leaf entries from source to destination.
933 memcpy(&dents
[start_d
], &sents
[start_s
],
934 count
* sizeof(xfs_dir2_leaf_entry_t
));
935 xfs_dir3_leaf_log_ents(args
, bp_d
, start_d
, start_d
+ count
- 1);
938 * If there are source entries after the ones we copied,
939 * delete the ones we copied by sliding the next ones down.
941 if (start_s
+ count
< shdr
->count
) {
942 memmove(&sents
[start_s
], &sents
[start_s
+ count
],
943 count
* sizeof(xfs_dir2_leaf_entry_t
));
944 xfs_dir3_leaf_log_ents(args
, bp_s
, start_s
, start_s
+ count
- 1);
948 * Update the headers and log them.
950 shdr
->count
-= count
;
951 shdr
->stale
-= stale
;
952 dhdr
->count
+= count
;
953 dhdr
->stale
+= stale
;
957 * Determine the sort order of two leaf blocks.
958 * Returns 1 if both are valid and leaf2 should be before leaf1, else 0.
961 xfs_dir2_leafn_order(
962 struct xfs_inode
*dp
,
963 struct xfs_buf
*leaf1_bp
, /* leaf1 buffer */
964 struct xfs_buf
*leaf2_bp
) /* leaf2 buffer */
966 struct xfs_dir2_leaf
*leaf1
= leaf1_bp
->b_addr
;
967 struct xfs_dir2_leaf
*leaf2
= leaf2_bp
->b_addr
;
968 struct xfs_dir2_leaf_entry
*ents1
;
969 struct xfs_dir2_leaf_entry
*ents2
;
970 struct xfs_dir3_icleaf_hdr hdr1
;
971 struct xfs_dir3_icleaf_hdr hdr2
;
973 dp
->d_ops
->leaf_hdr_from_disk(&hdr1
, leaf1
);
974 dp
->d_ops
->leaf_hdr_from_disk(&hdr2
, leaf2
);
975 ents1
= dp
->d_ops
->leaf_ents_p(leaf1
);
976 ents2
= dp
->d_ops
->leaf_ents_p(leaf2
);
978 if (hdr1
.count
> 0 && hdr2
.count
> 0 &&
979 (be32_to_cpu(ents2
[0].hashval
) < be32_to_cpu(ents1
[0].hashval
) ||
980 be32_to_cpu(ents2
[hdr2
.count
- 1].hashval
) <
981 be32_to_cpu(ents1
[hdr1
.count
- 1].hashval
)))
987 * Rebalance leaf entries between two leaf blocks.
988 * This is actually only called when the second block is new,
989 * though the code deals with the general case.
990 * A new entry will be inserted in one of the blocks, and that
991 * entry is taken into account when balancing.
994 xfs_dir2_leafn_rebalance(
995 xfs_da_state_t
*state
, /* btree cursor */
996 xfs_da_state_blk_t
*blk1
, /* first btree block */
997 xfs_da_state_blk_t
*blk2
) /* second btree block */
999 xfs_da_args_t
*args
; /* operation arguments */
1000 int count
; /* count (& direction) leaves */
1001 int isleft
; /* new goes in left leaf */
1002 xfs_dir2_leaf_t
*leaf1
; /* first leaf structure */
1003 xfs_dir2_leaf_t
*leaf2
; /* second leaf structure */
1004 int mid
; /* midpoint leaf index */
1005 #if defined(DEBUG) || defined(XFS_WARN)
1006 int oldstale
; /* old count of stale leaves */
1008 int oldsum
; /* old total leaf count */
1009 int swap_blocks
; /* swapped leaf blocks */
1010 struct xfs_dir2_leaf_entry
*ents1
;
1011 struct xfs_dir2_leaf_entry
*ents2
;
1012 struct xfs_dir3_icleaf_hdr hdr1
;
1013 struct xfs_dir3_icleaf_hdr hdr2
;
1014 struct xfs_inode
*dp
= state
->args
->dp
;
1018 * If the block order is wrong, swap the arguments.
1020 swap_blocks
= xfs_dir2_leafn_order(dp
, blk1
->bp
, blk2
->bp
);
1024 leaf1
= blk1
->bp
->b_addr
;
1025 leaf2
= blk2
->bp
->b_addr
;
1026 dp
->d_ops
->leaf_hdr_from_disk(&hdr1
, leaf1
);
1027 dp
->d_ops
->leaf_hdr_from_disk(&hdr2
, leaf2
);
1028 ents1
= dp
->d_ops
->leaf_ents_p(leaf1
);
1029 ents2
= dp
->d_ops
->leaf_ents_p(leaf2
);
1031 oldsum
= hdr1
.count
+ hdr2
.count
;
1032 #if defined(DEBUG) || defined(XFS_WARN)
1033 oldstale
= hdr1
.stale
+ hdr2
.stale
;
1038 * If the old leaf count was odd then the new one will be even,
1039 * so we need to divide the new count evenly.
1042 xfs_dahash_t midhash
; /* middle entry hash value */
1044 if (mid
>= hdr1
.count
)
1045 midhash
= be32_to_cpu(ents2
[mid
- hdr1
.count
].hashval
);
1047 midhash
= be32_to_cpu(ents1
[mid
].hashval
);
1048 isleft
= args
->hashval
<= midhash
;
1051 * If the old count is even then the new count is odd, so there's
1052 * no preferred side for the new entry.
1053 * Pick the left one.
1058 * Calculate moved entry count. Positive means left-to-right,
1059 * negative means right-to-left. Then move the entries.
1061 count
= hdr1
.count
- mid
+ (isleft
== 0);
1063 xfs_dir3_leafn_moveents(args
, blk1
->bp
, &hdr1
, ents1
,
1064 hdr1
.count
- count
, blk2
->bp
,
1065 &hdr2
, ents2
, 0, count
);
1067 xfs_dir3_leafn_moveents(args
, blk2
->bp
, &hdr2
, ents2
, 0,
1068 blk1
->bp
, &hdr1
, ents1
,
1071 ASSERT(hdr1
.count
+ hdr2
.count
== oldsum
);
1072 ASSERT(hdr1
.stale
+ hdr2
.stale
== oldstale
);
1074 /* log the changes made when moving the entries */
1075 dp
->d_ops
->leaf_hdr_to_disk(leaf1
, &hdr1
);
1076 dp
->d_ops
->leaf_hdr_to_disk(leaf2
, &hdr2
);
1077 xfs_dir3_leaf_log_header(args
, blk1
->bp
);
1078 xfs_dir3_leaf_log_header(args
, blk2
->bp
);
1080 xfs_dir3_leaf_check(dp
, blk1
->bp
);
1081 xfs_dir3_leaf_check(dp
, blk2
->bp
);
1084 * Mark whether we're inserting into the old or new leaf.
1086 if (hdr1
.count
< hdr2
.count
)
1087 state
->inleaf
= swap_blocks
;
1088 else if (hdr1
.count
> hdr2
.count
)
1089 state
->inleaf
= !swap_blocks
;
1091 state
->inleaf
= swap_blocks
^ (blk1
->index
<= hdr1
.count
);
1093 * Adjust the expected index for insertion.
1096 blk2
->index
= blk1
->index
- hdr1
.count
;
1099 * Finally sanity check just to make sure we are not returning a
1102 if (blk2
->index
< 0) {
1105 xfs_alert(dp
->i_mount
,
1106 "%s: picked the wrong leaf? reverting original leaf: blk1->index %d",
1107 __func__
, blk1
->index
);
1112 xfs_dir3_data_block_free(
1113 xfs_da_args_t
*args
,
1114 struct xfs_dir2_data_hdr
*hdr
,
1115 struct xfs_dir2_free
*free
,
1118 struct xfs_buf
*fbp
,
1123 struct xfs_dir3_icfree_hdr freehdr
;
1124 struct xfs_inode
*dp
= args
->dp
;
1126 dp
->d_ops
->free_hdr_from_disk(&freehdr
, free
);
1127 bests
= dp
->d_ops
->free_bests_p(free
);
1130 * Data block is not empty, just set the free entry to the new
1133 bests
[findex
] = cpu_to_be16(longest
);
1134 xfs_dir2_free_log_bests(args
, fbp
, findex
, findex
);
1138 /* One less used entry in the free table. */
1142 * If this was the last entry in the table, we can trim the table size
1143 * back. There might be other entries at the end referring to
1144 * non-existent data blocks, get those too.
1146 if (findex
== freehdr
.nvalid
- 1) {
1147 int i
; /* free entry index */
1149 for (i
= findex
- 1; i
>= 0; i
--) {
1150 if (bests
[i
] != cpu_to_be16(NULLDATAOFF
))
1153 freehdr
.nvalid
= i
+ 1;
1156 /* Not the last entry, just punch it out. */
1157 bests
[findex
] = cpu_to_be16(NULLDATAOFF
);
1161 dp
->d_ops
->free_hdr_to_disk(free
, &freehdr
);
1162 xfs_dir2_free_log_header(args
, fbp
);
1165 * If there are no useful entries left in the block, get rid of the
1168 if (!freehdr
.nused
) {
1171 error
= xfs_dir2_shrink_inode(args
, fdb
, fbp
);
1175 } else if (error
!= -ENOSPC
|| args
->total
!= 0)
1178 * It's possible to get ENOSPC if there is no
1179 * space reservation. In this case some one
1180 * else will eventually get rid of this block.
1184 /* Log the free entry that changed, unless we got rid of it. */
1186 xfs_dir2_free_log_bests(args
, fbp
, findex
, findex
);
1191 * Remove an entry from a node directory.
1192 * This removes the leaf entry and the data entry,
1193 * and updates the free block if necessary.
1195 static int /* error */
1196 xfs_dir2_leafn_remove(
1197 xfs_da_args_t
*args
, /* operation arguments */
1198 struct xfs_buf
*bp
, /* leaf buffer */
1199 int index
, /* leaf entry index */
1200 xfs_da_state_blk_t
*dblk
, /* data block */
1201 int *rval
) /* resulting block needs join */
1203 xfs_dir2_data_hdr_t
*hdr
; /* data block header */
1204 xfs_dir2_db_t db
; /* data block number */
1205 struct xfs_buf
*dbp
; /* data block buffer */
1206 xfs_dir2_data_entry_t
*dep
; /* data block entry */
1207 xfs_inode_t
*dp
; /* incore directory inode */
1208 xfs_dir2_leaf_t
*leaf
; /* leaf structure */
1209 xfs_dir2_leaf_entry_t
*lep
; /* leaf entry */
1210 int longest
; /* longest data free entry */
1211 int off
; /* data block entry offset */
1212 int needlog
; /* need to log data header */
1213 int needscan
; /* need to rescan data frees */
1214 xfs_trans_t
*tp
; /* transaction pointer */
1215 struct xfs_dir2_data_free
*bf
; /* bestfree table */
1216 struct xfs_dir3_icleaf_hdr leafhdr
;
1217 struct xfs_dir2_leaf_entry
*ents
;
1219 trace_xfs_dir2_leafn_remove(args
, index
);
1224 dp
->d_ops
->leaf_hdr_from_disk(&leafhdr
, leaf
);
1225 ents
= dp
->d_ops
->leaf_ents_p(leaf
);
1228 * Point to the entry we're removing.
1233 * Extract the data block and offset from the entry.
1235 db
= xfs_dir2_dataptr_to_db(args
->geo
, be32_to_cpu(lep
->address
));
1236 ASSERT(dblk
->blkno
== db
);
1237 off
= xfs_dir2_dataptr_to_off(args
->geo
, be32_to_cpu(lep
->address
));
1238 ASSERT(dblk
->index
== off
);
1241 * Kill the leaf entry by marking it stale.
1242 * Log the leaf block changes.
1245 dp
->d_ops
->leaf_hdr_to_disk(leaf
, &leafhdr
);
1246 xfs_dir3_leaf_log_header(args
, bp
);
1248 lep
->address
= cpu_to_be32(XFS_DIR2_NULL_DATAPTR
);
1249 xfs_dir3_leaf_log_ents(args
, bp
, index
, index
);
1252 * Make the data entry free. Keep track of the longest freespace
1253 * in the data block in case it changes.
1257 dep
= (xfs_dir2_data_entry_t
*)((char *)hdr
+ off
);
1258 bf
= dp
->d_ops
->data_bestfree_p(hdr
);
1259 longest
= be16_to_cpu(bf
[0].length
);
1260 needlog
= needscan
= 0;
1261 xfs_dir2_data_make_free(args
, dbp
, off
,
1262 dp
->d_ops
->data_entsize(dep
->namelen
), &needlog
, &needscan
);
1264 * Rescan the data block freespaces for bestfree.
1265 * Log the data block header if needed.
1268 xfs_dir2_data_freescan(dp
, hdr
, &needlog
);
1270 xfs_dir2_data_log_header(args
, dbp
);
1271 xfs_dir3_data_check(dp
, dbp
);
1273 * If the longest data block freespace changes, need to update
1274 * the corresponding freeblock entry.
1276 if (longest
< be16_to_cpu(bf
[0].length
)) {
1277 int error
; /* error return value */
1278 struct xfs_buf
*fbp
; /* freeblock buffer */
1279 xfs_dir2_db_t fdb
; /* freeblock block number */
1280 int findex
; /* index in freeblock entries */
1281 xfs_dir2_free_t
*free
; /* freeblock structure */
1284 * Convert the data block number to a free block,
1285 * read in the free block.
1287 fdb
= dp
->d_ops
->db_to_fdb(args
->geo
, db
);
1288 error
= xfs_dir2_free_read(tp
, dp
,
1289 xfs_dir2_db_to_da(args
->geo
, fdb
),
1296 struct xfs_dir3_icfree_hdr freehdr
;
1297 dp
->d_ops
->free_hdr_from_disk(&freehdr
, free
);
1298 ASSERT(freehdr
.firstdb
== dp
->d_ops
->free_max_bests(args
->geo
) *
1299 (fdb
- xfs_dir2_byte_to_db(args
->geo
,
1300 XFS_DIR2_FREE_OFFSET
)));
1304 * Calculate which entry we need to fix.
1306 findex
= dp
->d_ops
->db_to_fdindex(args
->geo
, db
);
1307 longest
= be16_to_cpu(bf
[0].length
);
1309 * If the data block is now empty we can get rid of it
1312 if (longest
== args
->geo
->blksize
-
1313 dp
->d_ops
->data_entry_offset
) {
1315 * Try to punch out the data block.
1317 error
= xfs_dir2_shrink_inode(args
, db
, dbp
);
1323 * We can get ENOSPC if there's no space reservation.
1324 * In this case just drop the buffer and some one else
1325 * will eventually get rid of the empty block.
1327 else if (!(error
== -ENOSPC
&& args
->total
== 0))
1331 * If we got rid of the data block, we can eliminate that entry
1332 * in the free block.
1334 error
= xfs_dir3_data_block_free(args
, hdr
, free
,
1335 fdb
, findex
, fbp
, longest
);
1340 xfs_dir3_leaf_check(dp
, bp
);
1342 * Return indication of whether this leaf block is empty enough
1343 * to justify trying to join it with a neighbor.
1345 *rval
= (dp
->d_ops
->leaf_hdr_size
+
1346 (uint
)sizeof(ents
[0]) * (leafhdr
.count
- leafhdr
.stale
)) <
1347 args
->geo
->magicpct
;
1352 * Split the leaf entries in the old block into old and new blocks.
1355 xfs_dir2_leafn_split(
1356 xfs_da_state_t
*state
, /* btree cursor */
1357 xfs_da_state_blk_t
*oldblk
, /* original block */
1358 xfs_da_state_blk_t
*newblk
) /* newly created block */
1360 xfs_da_args_t
*args
; /* operation arguments */
1361 xfs_dablk_t blkno
; /* new leaf block number */
1362 int error
; /* error return value */
1363 struct xfs_inode
*dp
;
1366 * Allocate space for a new leaf node.
1370 ASSERT(oldblk
->magic
== XFS_DIR2_LEAFN_MAGIC
);
1371 error
= xfs_da_grow_inode(args
, &blkno
);
1376 * Initialize the new leaf block.
1378 error
= xfs_dir3_leaf_get_buf(args
, xfs_dir2_da_to_db(args
->geo
, blkno
),
1379 &newblk
->bp
, XFS_DIR2_LEAFN_MAGIC
);
1383 newblk
->blkno
= blkno
;
1384 newblk
->magic
= XFS_DIR2_LEAFN_MAGIC
;
1386 * Rebalance the entries across the two leaves, link the new
1387 * block into the leaves.
1389 xfs_dir2_leafn_rebalance(state
, oldblk
, newblk
);
1390 error
= xfs_da3_blk_link(state
, oldblk
, newblk
);
1395 * Insert the new entry in the correct block.
1398 error
= xfs_dir2_leafn_add(oldblk
->bp
, args
, oldblk
->index
);
1400 error
= xfs_dir2_leafn_add(newblk
->bp
, args
, newblk
->index
);
1402 * Update last hashval in each block since we added the name.
1404 oldblk
->hashval
= xfs_dir2_leaf_lasthash(dp
, oldblk
->bp
, NULL
);
1405 newblk
->hashval
= xfs_dir2_leaf_lasthash(dp
, newblk
->bp
, NULL
);
1406 xfs_dir3_leaf_check(dp
, oldblk
->bp
);
1407 xfs_dir3_leaf_check(dp
, newblk
->bp
);
1412 * Check a leaf block and its neighbors to see if the block should be
1413 * collapsed into one or the other neighbor. Always keep the block
1414 * with the smaller block number.
1415 * If the current block is over 50% full, don't try to join it, return 0.
1416 * If the block is empty, fill in the state structure and return 2.
1417 * If it can be collapsed, fill in the state structure and return 1.
1418 * If nothing can be done, return 0.
1421 xfs_dir2_leafn_toosmall(
1422 xfs_da_state_t
*state
, /* btree cursor */
1423 int *action
) /* resulting action to take */
1425 xfs_da_state_blk_t
*blk
; /* leaf block */
1426 xfs_dablk_t blkno
; /* leaf block number */
1427 struct xfs_buf
*bp
; /* leaf buffer */
1428 int bytes
; /* bytes in use */
1429 int count
; /* leaf live entry count */
1430 int error
; /* error return value */
1431 int forward
; /* sibling block direction */
1432 int i
; /* sibling counter */
1433 xfs_dir2_leaf_t
*leaf
; /* leaf structure */
1434 int rval
; /* result from path_shift */
1435 struct xfs_dir3_icleaf_hdr leafhdr
;
1436 struct xfs_dir2_leaf_entry
*ents
;
1437 struct xfs_inode
*dp
= state
->args
->dp
;
1440 * Check for the degenerate case of the block being over 50% full.
1441 * If so, it's not worth even looking to see if we might be able
1442 * to coalesce with a sibling.
1444 blk
= &state
->path
.blk
[state
->path
.active
- 1];
1445 leaf
= blk
->bp
->b_addr
;
1446 dp
->d_ops
->leaf_hdr_from_disk(&leafhdr
, leaf
);
1447 ents
= dp
->d_ops
->leaf_ents_p(leaf
);
1448 xfs_dir3_leaf_check(dp
, blk
->bp
);
1450 count
= leafhdr
.count
- leafhdr
.stale
;
1451 bytes
= dp
->d_ops
->leaf_hdr_size
+ count
* sizeof(ents
[0]);
1452 if (bytes
> (state
->args
->geo
->blksize
>> 1)) {
1454 * Blk over 50%, don't try to join.
1460 * Check for the degenerate case of the block being empty.
1461 * If the block is empty, we'll simply delete it, no need to
1462 * coalesce it with a sibling block. We choose (arbitrarily)
1463 * to merge with the forward block unless it is NULL.
1467 * Make altpath point to the block we want to keep and
1468 * path point to the block we want to drop (this one).
1470 forward
= (leafhdr
.forw
!= 0);
1471 memcpy(&state
->altpath
, &state
->path
, sizeof(state
->path
));
1472 error
= xfs_da3_path_shift(state
, &state
->altpath
, forward
, 0,
1476 *action
= rval
? 2 : 0;
1480 * Examine each sibling block to see if we can coalesce with
1481 * at least 25% free space to spare. We need to figure out
1482 * whether to merge with the forward or the backward block.
1483 * We prefer coalescing with the lower numbered sibling so as
1484 * to shrink a directory over time.
1486 forward
= leafhdr
.forw
< leafhdr
.back
;
1487 for (i
= 0, bp
= NULL
; i
< 2; forward
= !forward
, i
++) {
1488 struct xfs_dir3_icleaf_hdr hdr2
;
1490 blkno
= forward
? leafhdr
.forw
: leafhdr
.back
;
1494 * Read the sibling leaf block.
1496 error
= xfs_dir3_leafn_read(state
->args
->trans
, dp
,
1502 * Count bytes in the two blocks combined.
1504 count
= leafhdr
.count
- leafhdr
.stale
;
1505 bytes
= state
->args
->geo
->blksize
-
1506 (state
->args
->geo
->blksize
>> 2);
1509 dp
->d_ops
->leaf_hdr_from_disk(&hdr2
, leaf
);
1510 ents
= dp
->d_ops
->leaf_ents_p(leaf
);
1511 count
+= hdr2
.count
- hdr2
.stale
;
1512 bytes
-= count
* sizeof(ents
[0]);
1515 * Fits with at least 25% to spare.
1519 xfs_trans_brelse(state
->args
->trans
, bp
);
1522 * Didn't like either block, give up.
1530 * Make altpath point to the block we want to keep (the lower
1531 * numbered block) and path point to the block we want to drop.
1533 memcpy(&state
->altpath
, &state
->path
, sizeof(state
->path
));
1534 if (blkno
< blk
->blkno
)
1535 error
= xfs_da3_path_shift(state
, &state
->altpath
, forward
, 0,
1538 error
= xfs_da3_path_shift(state
, &state
->path
, forward
, 0,
1543 *action
= rval
? 0 : 1;
1548 * Move all the leaf entries from drop_blk to save_blk.
1549 * This is done as part of a join operation.
1552 xfs_dir2_leafn_unbalance(
1553 xfs_da_state_t
*state
, /* cursor */
1554 xfs_da_state_blk_t
*drop_blk
, /* dead block */
1555 xfs_da_state_blk_t
*save_blk
) /* surviving block */
1557 xfs_da_args_t
*args
; /* operation arguments */
1558 xfs_dir2_leaf_t
*drop_leaf
; /* dead leaf structure */
1559 xfs_dir2_leaf_t
*save_leaf
; /* surviving leaf structure */
1560 struct xfs_dir3_icleaf_hdr savehdr
;
1561 struct xfs_dir3_icleaf_hdr drophdr
;
1562 struct xfs_dir2_leaf_entry
*sents
;
1563 struct xfs_dir2_leaf_entry
*dents
;
1564 struct xfs_inode
*dp
= state
->args
->dp
;
1567 ASSERT(drop_blk
->magic
== XFS_DIR2_LEAFN_MAGIC
);
1568 ASSERT(save_blk
->magic
== XFS_DIR2_LEAFN_MAGIC
);
1569 drop_leaf
= drop_blk
->bp
->b_addr
;
1570 save_leaf
= save_blk
->bp
->b_addr
;
1572 dp
->d_ops
->leaf_hdr_from_disk(&savehdr
, save_leaf
);
1573 dp
->d_ops
->leaf_hdr_from_disk(&drophdr
, drop_leaf
);
1574 sents
= dp
->d_ops
->leaf_ents_p(save_leaf
);
1575 dents
= dp
->d_ops
->leaf_ents_p(drop_leaf
);
1578 * If there are any stale leaf entries, take this opportunity
1582 xfs_dir3_leaf_compact(args
, &drophdr
, drop_blk
->bp
);
1584 xfs_dir3_leaf_compact(args
, &savehdr
, save_blk
->bp
);
1587 * Move the entries from drop to the appropriate end of save.
1589 drop_blk
->hashval
= be32_to_cpu(dents
[drophdr
.count
- 1].hashval
);
1590 if (xfs_dir2_leafn_order(dp
, save_blk
->bp
, drop_blk
->bp
))
1591 xfs_dir3_leafn_moveents(args
, drop_blk
->bp
, &drophdr
, dents
, 0,
1592 save_blk
->bp
, &savehdr
, sents
, 0,
1595 xfs_dir3_leafn_moveents(args
, drop_blk
->bp
, &drophdr
, dents
, 0,
1596 save_blk
->bp
, &savehdr
, sents
,
1597 savehdr
.count
, drophdr
.count
);
1598 save_blk
->hashval
= be32_to_cpu(sents
[savehdr
.count
- 1].hashval
);
1600 /* log the changes made when moving the entries */
1601 dp
->d_ops
->leaf_hdr_to_disk(save_leaf
, &savehdr
);
1602 dp
->d_ops
->leaf_hdr_to_disk(drop_leaf
, &drophdr
);
1603 xfs_dir3_leaf_log_header(args
, save_blk
->bp
);
1604 xfs_dir3_leaf_log_header(args
, drop_blk
->bp
);
1606 xfs_dir3_leaf_check(dp
, save_blk
->bp
);
1607 xfs_dir3_leaf_check(dp
, drop_blk
->bp
);
1611 * Top-level node form directory addname routine.
1614 xfs_dir2_node_addname(
1615 xfs_da_args_t
*args
) /* operation arguments */
1617 xfs_da_state_blk_t
*blk
; /* leaf block for insert */
1618 int error
; /* error return value */
1619 int rval
; /* sub-return value */
1620 xfs_da_state_t
*state
; /* btree cursor */
1622 trace_xfs_dir2_node_addname(args
);
1625 * Allocate and initialize the state (btree cursor).
1627 state
= xfs_da_state_alloc();
1629 state
->mp
= args
->dp
->i_mount
;
1631 * Look up the name. We're not supposed to find it, but
1632 * this gives us the insertion point.
1634 error
= xfs_da3_node_lookup_int(state
, &rval
);
1637 if (rval
!= -ENOENT
) {
1641 * Add the data entry to a data block.
1642 * Extravalid is set to a freeblock found by lookup.
1644 rval
= xfs_dir2_node_addname_int(args
,
1645 state
->extravalid
? &state
->extrablk
: NULL
);
1649 blk
= &state
->path
.blk
[state
->path
.active
- 1];
1650 ASSERT(blk
->magic
== XFS_DIR2_LEAFN_MAGIC
);
1652 * Add the new leaf entry.
1654 rval
= xfs_dir2_leafn_add(blk
->bp
, args
, blk
->index
);
1657 * It worked, fix the hash values up the btree.
1659 if (!(args
->op_flags
& XFS_DA_OP_JUSTCHECK
))
1660 xfs_da3_fixhashpath(state
, &state
->path
);
1663 * It didn't work, we need to split the leaf block.
1665 if (args
->total
== 0) {
1666 ASSERT(rval
== -ENOSPC
);
1670 * Split the leaf block and insert the new entry.
1672 rval
= xfs_da3_split(state
);
1675 xfs_da_state_free(state
);
1680 * Add the data entry for a node-format directory name addition.
1681 * The leaf entry is added in xfs_dir2_leafn_add.
1682 * We may enter with a freespace block that the lookup found.
1684 static int /* error */
1685 xfs_dir2_node_addname_int(
1686 xfs_da_args_t
*args
, /* operation arguments */
1687 xfs_da_state_blk_t
*fblk
) /* optional freespace block */
1689 xfs_dir2_data_hdr_t
*hdr
; /* data block header */
1690 xfs_dir2_db_t dbno
; /* data block number */
1691 struct xfs_buf
*dbp
; /* data block buffer */
1692 xfs_dir2_data_entry_t
*dep
; /* data entry pointer */
1693 xfs_inode_t
*dp
; /* incore directory inode */
1694 xfs_dir2_data_unused_t
*dup
; /* data unused entry pointer */
1695 int error
; /* error return value */
1696 xfs_dir2_db_t fbno
; /* freespace block number */
1697 struct xfs_buf
*fbp
; /* freespace buffer */
1698 int findex
; /* freespace entry index */
1699 xfs_dir2_free_t
*free
=NULL
; /* freespace block structure */
1700 xfs_dir2_db_t ifbno
; /* initial freespace block no */
1701 xfs_dir2_db_t lastfbno
=0; /* highest freespace block no */
1702 int length
; /* length of the new entry */
1703 int logfree
; /* need to log free entry */
1704 xfs_mount_t
*mp
; /* filesystem mount point */
1705 int needlog
; /* need to log data header */
1706 int needscan
; /* need to rescan data frees */
1707 __be16
*tagp
; /* data entry tag pointer */
1708 xfs_trans_t
*tp
; /* transaction pointer */
1710 struct xfs_dir3_icfree_hdr freehdr
;
1711 struct xfs_dir2_data_free
*bf
;
1712 xfs_dir2_data_aoff_t aoff
;
1717 length
= dp
->d_ops
->data_entsize(args
->namelen
);
1719 * If we came in with a freespace block that means that lookup
1720 * found an entry with our hash value. This is the freespace
1721 * block for that data entry.
1726 * Remember initial freespace block number.
1728 ifbno
= fblk
->blkno
;
1730 findex
= fblk
->index
;
1731 bests
= dp
->d_ops
->free_bests_p(free
);
1732 dp
->d_ops
->free_hdr_from_disk(&freehdr
, free
);
1735 * This means the free entry showed that the data block had
1736 * space for our entry, so we remembered it.
1737 * Use that data block.
1740 ASSERT(findex
< freehdr
.nvalid
);
1741 ASSERT(be16_to_cpu(bests
[findex
]) != NULLDATAOFF
);
1742 ASSERT(be16_to_cpu(bests
[findex
]) >= length
);
1743 dbno
= freehdr
.firstdb
+ findex
;
1746 * The data block looked at didn't have enough room.
1747 * We'll start at the beginning of the freespace entries.
1754 * Didn't come in with a freespace block, so no data block.
1762 * If we don't have a data block yet, we're going to scan the
1763 * freespace blocks looking for one. Figure out what the
1764 * highest freespace block number is.
1767 xfs_fileoff_t fo
; /* freespace block number */
1769 if ((error
= xfs_bmap_last_offset(dp
, &fo
, XFS_DATA_FORK
)))
1771 lastfbno
= xfs_dir2_da_to_db(args
->geo
, (xfs_dablk_t
)fo
);
1775 * While we haven't identified a data block, search the freeblock
1776 * data for a good data block. If we find a null freeblock entry,
1777 * indicating a hole in the data blocks, remember that.
1779 while (dbno
== -1) {
1781 * If we don't have a freeblock in hand, get the next one.
1785 * Happens the first time through unless lookup gave
1786 * us a freespace block to start with.
1789 fbno
= xfs_dir2_byte_to_db(args
->geo
,
1790 XFS_DIR2_FREE_OFFSET
);
1792 * If it's ifbno we already looked at it.
1797 * If it's off the end we're done.
1799 if (fbno
>= lastfbno
)
1802 * Read the block. There can be holes in the
1803 * freespace blocks, so this might not succeed.
1804 * This should be really rare, so there's no reason
1807 error
= xfs_dir2_free_try_read(tp
, dp
,
1808 xfs_dir2_db_to_da(args
->geo
, fbno
),
1818 * Look at the current free entry. Is it good enough?
1820 * The bests initialisation should be where the bufer is read in
1821 * the above branch. But gcc is too stupid to realise that bests
1822 * and the freehdr are actually initialised if they are placed
1823 * there, so we have to do it here to avoid warnings. Blech.
1825 bests
= dp
->d_ops
->free_bests_p(free
);
1826 dp
->d_ops
->free_hdr_from_disk(&freehdr
, free
);
1827 if (be16_to_cpu(bests
[findex
]) != NULLDATAOFF
&&
1828 be16_to_cpu(bests
[findex
]) >= length
)
1829 dbno
= freehdr
.firstdb
+ findex
;
1832 * Are we done with the freeblock?
1834 if (++findex
== freehdr
.nvalid
) {
1838 xfs_trans_brelse(tp
, fbp
);
1840 if (fblk
&& fblk
->bp
)
1846 * If we don't have a data block, we need to allocate one and make
1847 * the freespace entries refer to it.
1849 if (unlikely(dbno
== -1)) {
1851 * Not allowed to allocate, return failure.
1853 if ((args
->op_flags
& XFS_DA_OP_JUSTCHECK
) || args
->total
== 0)
1857 * Allocate and initialize the new data block.
1859 if (unlikely((error
= xfs_dir2_grow_inode(args
,
1860 XFS_DIR2_DATA_SPACE
,
1862 (error
= xfs_dir3_data_init(args
, dbno
, &dbp
))))
1866 * If (somehow) we have a freespace block, get rid of it.
1869 xfs_trans_brelse(tp
, fbp
);
1870 if (fblk
&& fblk
->bp
)
1874 * Get the freespace block corresponding to the data block
1875 * that was just allocated.
1877 fbno
= dp
->d_ops
->db_to_fdb(args
->geo
, dbno
);
1878 error
= xfs_dir2_free_try_read(tp
, dp
,
1879 xfs_dir2_db_to_da(args
->geo
, fbno
),
1885 * If there wasn't a freespace block, the read will
1886 * return a NULL fbp. Allocate and initialize a new one.
1889 error
= xfs_dir2_grow_inode(args
, XFS_DIR2_FREE_SPACE
,
1894 if (dp
->d_ops
->db_to_fdb(args
->geo
, dbno
) != fbno
) {
1896 "%s: dir ino %llu needed freesp block %lld for data block %lld, got %lld ifbno %llu lastfbno %d",
1897 __func__
, (unsigned long long)dp
->i_ino
,
1898 (long long)dp
->d_ops
->db_to_fdb(
1900 (long long)dbno
, (long long)fbno
,
1901 (unsigned long long)ifbno
, lastfbno
);
1904 " fblk "PTR_FMT
" blkno %llu index %d magic 0x%x",
1906 (unsigned long long)fblk
->blkno
,
1910 xfs_alert(mp
, " ... fblk is NULL");
1912 XFS_ERROR_REPORT("xfs_dir2_node_addname_int",
1913 XFS_ERRLEVEL_LOW
, mp
);
1914 return -EFSCORRUPTED
;
1918 * Get a buffer for the new block.
1920 error
= xfs_dir3_free_get_buf(args
, fbno
, &fbp
);
1924 bests
= dp
->d_ops
->free_bests_p(free
);
1925 dp
->d_ops
->free_hdr_from_disk(&freehdr
, free
);
1928 * Remember the first slot as our empty slot.
1931 (fbno
- xfs_dir2_byte_to_db(args
->geo
,
1932 XFS_DIR2_FREE_OFFSET
)) *
1933 dp
->d_ops
->free_max_bests(args
->geo
);
1936 bests
= dp
->d_ops
->free_bests_p(free
);
1937 dp
->d_ops
->free_hdr_from_disk(&freehdr
, free
);
1941 * Set the freespace block index from the data block number.
1943 findex
= dp
->d_ops
->db_to_fdindex(args
->geo
, dbno
);
1945 * If it's after the end of the current entries in the
1946 * freespace block, extend that table.
1948 if (findex
>= freehdr
.nvalid
) {
1949 ASSERT(findex
< dp
->d_ops
->free_max_bests(args
->geo
));
1950 freehdr
.nvalid
= findex
+ 1;
1952 * Tag new entry so nused will go up.
1954 bests
[findex
] = cpu_to_be16(NULLDATAOFF
);
1957 * If this entry was for an empty data block
1958 * (this should always be true) then update the header.
1960 if (bests
[findex
] == cpu_to_be16(NULLDATAOFF
)) {
1962 dp
->d_ops
->free_hdr_to_disk(fbp
->b_addr
, &freehdr
);
1963 xfs_dir2_free_log_header(args
, fbp
);
1966 * Update the real value in the table.
1967 * We haven't allocated the data entry yet so this will
1971 bf
= dp
->d_ops
->data_bestfree_p(hdr
);
1972 bests
[findex
] = bf
[0].length
;
1976 * We had a data block so we don't have to make a new one.
1980 * If just checking, we succeeded.
1982 if (args
->op_flags
& XFS_DA_OP_JUSTCHECK
)
1986 * Read the data block in.
1988 error
= xfs_dir3_data_read(tp
, dp
,
1989 xfs_dir2_db_to_da(args
->geo
, dbno
),
1994 bf
= dp
->d_ops
->data_bestfree_p(hdr
);
1997 ASSERT(be16_to_cpu(bf
[0].length
) >= length
);
1999 * Point to the existing unused space.
2001 dup
= (xfs_dir2_data_unused_t
*)
2002 ((char *)hdr
+ be16_to_cpu(bf
[0].offset
));
2003 needscan
= needlog
= 0;
2005 * Mark the first part of the unused space, inuse for us.
2007 aoff
= (xfs_dir2_data_aoff_t
)((char *)dup
- (char *)hdr
);
2008 error
= xfs_dir2_data_use_free(args
, dbp
, dup
, aoff
, length
,
2009 &needlog
, &needscan
);
2011 xfs_trans_brelse(tp
, dbp
);
2015 * Fill in the new entry and log it.
2017 dep
= (xfs_dir2_data_entry_t
*)dup
;
2018 dep
->inumber
= cpu_to_be64(args
->inumber
);
2019 dep
->namelen
= args
->namelen
;
2020 memcpy(dep
->name
, args
->name
, dep
->namelen
);
2021 dp
->d_ops
->data_put_ftype(dep
, args
->filetype
);
2022 tagp
= dp
->d_ops
->data_entry_tag_p(dep
);
2023 *tagp
= cpu_to_be16((char *)dep
- (char *)hdr
);
2024 xfs_dir2_data_log_entry(args
, dbp
, dep
);
2026 * Rescan the block for bestfree if needed.
2029 xfs_dir2_data_freescan(dp
, hdr
, &needlog
);
2031 * Log the data block header if needed.
2034 xfs_dir2_data_log_header(args
, dbp
);
2036 * If the freespace entry is now wrong, update it.
2038 bests
= dp
->d_ops
->free_bests_p(free
); /* gcc is so stupid */
2039 if (be16_to_cpu(bests
[findex
]) != be16_to_cpu(bf
[0].length
)) {
2040 bests
[findex
] = bf
[0].length
;
2044 * Log the freespace entry if needed.
2047 xfs_dir2_free_log_bests(args
, fbp
, findex
, findex
);
2049 * Return the data block and offset in args, then drop the data block.
2051 args
->blkno
= (xfs_dablk_t
)dbno
;
2052 args
->index
= be16_to_cpu(*tagp
);
2057 * Lookup an entry in a node-format directory.
2058 * All the real work happens in xfs_da3_node_lookup_int.
2059 * The only real output is the inode number of the entry.
2062 xfs_dir2_node_lookup(
2063 xfs_da_args_t
*args
) /* operation arguments */
2065 int error
; /* error return value */
2066 int i
; /* btree level */
2067 int rval
; /* operation return value */
2068 xfs_da_state_t
*state
; /* btree cursor */
2070 trace_xfs_dir2_node_lookup(args
);
2073 * Allocate and initialize the btree cursor.
2075 state
= xfs_da_state_alloc();
2077 state
->mp
= args
->dp
->i_mount
;
2079 * Fill in the path to the entry in the cursor.
2081 error
= xfs_da3_node_lookup_int(state
, &rval
);
2084 else if (rval
== -ENOENT
&& args
->cmpresult
== XFS_CMP_CASE
) {
2085 /* If a CI match, dup the actual name and return -EEXIST */
2086 xfs_dir2_data_entry_t
*dep
;
2088 dep
= (xfs_dir2_data_entry_t
*)
2089 ((char *)state
->extrablk
.bp
->b_addr
+
2090 state
->extrablk
.index
);
2091 rval
= xfs_dir_cilookup_result(args
, dep
->name
, dep
->namelen
);
2094 * Release the btree blocks and leaf block.
2096 for (i
= 0; i
< state
->path
.active
; i
++) {
2097 xfs_trans_brelse(args
->trans
, state
->path
.blk
[i
].bp
);
2098 state
->path
.blk
[i
].bp
= NULL
;
2101 * Release the data block if we have it.
2103 if (state
->extravalid
&& state
->extrablk
.bp
) {
2104 xfs_trans_brelse(args
->trans
, state
->extrablk
.bp
);
2105 state
->extrablk
.bp
= NULL
;
2107 xfs_da_state_free(state
);
2112 * Remove an entry from a node-format directory.
2115 xfs_dir2_node_removename(
2116 struct xfs_da_args
*args
) /* operation arguments */
2118 struct xfs_da_state_blk
*blk
; /* leaf block */
2119 int error
; /* error return value */
2120 int rval
; /* operation return value */
2121 struct xfs_da_state
*state
; /* btree cursor */
2123 trace_xfs_dir2_node_removename(args
);
2126 * Allocate and initialize the btree cursor.
2128 state
= xfs_da_state_alloc();
2130 state
->mp
= args
->dp
->i_mount
;
2132 /* Look up the entry we're deleting, set up the cursor. */
2133 error
= xfs_da3_node_lookup_int(state
, &rval
);
2137 /* Didn't find it, upper layer screwed up. */
2138 if (rval
!= -EEXIST
) {
2143 blk
= &state
->path
.blk
[state
->path
.active
- 1];
2144 ASSERT(blk
->magic
== XFS_DIR2_LEAFN_MAGIC
);
2145 ASSERT(state
->extravalid
);
2147 * Remove the leaf and data entries.
2148 * Extrablk refers to the data block.
2150 error
= xfs_dir2_leafn_remove(args
, blk
->bp
, blk
->index
,
2151 &state
->extrablk
, &rval
);
2155 * Fix the hash values up the btree.
2157 xfs_da3_fixhashpath(state
, &state
->path
);
2159 * If we need to join leaf blocks, do it.
2161 if (rval
&& state
->path
.active
> 1)
2162 error
= xfs_da3_join(state
);
2164 * If no errors so far, try conversion to leaf format.
2167 error
= xfs_dir2_node_to_leaf(state
);
2169 xfs_da_state_free(state
);
2174 * Replace an entry's inode number in a node-format directory.
2177 xfs_dir2_node_replace(
2178 xfs_da_args_t
*args
) /* operation arguments */
2180 xfs_da_state_blk_t
*blk
; /* leaf block */
2181 xfs_dir2_data_hdr_t
*hdr
; /* data block header */
2182 xfs_dir2_data_entry_t
*dep
; /* data entry changed */
2183 int error
; /* error return value */
2184 int i
; /* btree level */
2185 xfs_ino_t inum
; /* new inode number */
2186 int ftype
; /* new file type */
2187 xfs_dir2_leaf_t
*leaf
; /* leaf structure */
2188 xfs_dir2_leaf_entry_t
*lep
; /* leaf entry being changed */
2189 int rval
; /* internal return value */
2190 xfs_da_state_t
*state
; /* btree cursor */
2192 trace_xfs_dir2_node_replace(args
);
2195 * Allocate and initialize the btree cursor.
2197 state
= xfs_da_state_alloc();
2199 state
->mp
= args
->dp
->i_mount
;
2202 * We have to save new inode number and ftype since
2203 * xfs_da3_node_lookup_int() is going to overwrite them
2205 inum
= args
->inumber
;
2206 ftype
= args
->filetype
;
2209 * Lookup the entry to change in the btree.
2211 error
= xfs_da3_node_lookup_int(state
, &rval
);
2216 * It should be found, since the vnodeops layer has looked it up
2217 * and locked it. But paranoia is good.
2219 if (rval
== -EEXIST
) {
2220 struct xfs_dir2_leaf_entry
*ents
;
2222 * Find the leaf entry.
2224 blk
= &state
->path
.blk
[state
->path
.active
- 1];
2225 ASSERT(blk
->magic
== XFS_DIR2_LEAFN_MAGIC
);
2226 leaf
= blk
->bp
->b_addr
;
2227 ents
= args
->dp
->d_ops
->leaf_ents_p(leaf
);
2228 lep
= &ents
[blk
->index
];
2229 ASSERT(state
->extravalid
);
2231 * Point to the data entry.
2233 hdr
= state
->extrablk
.bp
->b_addr
;
2234 ASSERT(hdr
->magic
== cpu_to_be32(XFS_DIR2_DATA_MAGIC
) ||
2235 hdr
->magic
== cpu_to_be32(XFS_DIR3_DATA_MAGIC
));
2236 dep
= (xfs_dir2_data_entry_t
*)
2238 xfs_dir2_dataptr_to_off(args
->geo
,
2239 be32_to_cpu(lep
->address
)));
2240 ASSERT(inum
!= be64_to_cpu(dep
->inumber
));
2242 * Fill in the new inode number and log the entry.
2244 dep
->inumber
= cpu_to_be64(inum
);
2245 args
->dp
->d_ops
->data_put_ftype(dep
, ftype
);
2246 xfs_dir2_data_log_entry(args
, state
->extrablk
.bp
, dep
);
2250 * Didn't find it, and we're holding a data block. Drop it.
2252 else if (state
->extravalid
) {
2253 xfs_trans_brelse(args
->trans
, state
->extrablk
.bp
);
2254 state
->extrablk
.bp
= NULL
;
2257 * Release all the buffers in the cursor.
2259 for (i
= 0; i
< state
->path
.active
; i
++) {
2260 xfs_trans_brelse(args
->trans
, state
->path
.blk
[i
].bp
);
2261 state
->path
.blk
[i
].bp
= NULL
;
2263 xfs_da_state_free(state
);
2268 * Trim off a trailing empty freespace block.
2269 * Return (in rvalp) 1 if we did it, 0 if not.
2272 xfs_dir2_node_trim_free(
2273 xfs_da_args_t
*args
, /* operation arguments */
2274 xfs_fileoff_t fo
, /* free block number */
2275 int *rvalp
) /* out: did something */
2277 struct xfs_buf
*bp
; /* freespace buffer */
2278 xfs_inode_t
*dp
; /* incore directory inode */
2279 int error
; /* error return code */
2280 xfs_dir2_free_t
*free
; /* freespace structure */
2281 xfs_trans_t
*tp
; /* transaction pointer */
2282 struct xfs_dir3_icfree_hdr freehdr
;
2290 * Read the freespace block.
2292 error
= xfs_dir2_free_try_read(tp
, dp
, fo
, &bp
);
2296 * There can be holes in freespace. If fo is a hole, there's
2302 dp
->d_ops
->free_hdr_from_disk(&freehdr
, free
);
2305 * If there are used entries, there's nothing to do.
2307 if (freehdr
.nused
> 0) {
2308 xfs_trans_brelse(tp
, bp
);
2312 * Blow the block away.
2314 error
= xfs_dir2_shrink_inode(args
,
2315 xfs_dir2_da_to_db(args
->geo
, (xfs_dablk_t
)fo
), bp
);
2318 * Can't fail with ENOSPC since that only happens with no
2319 * space reservation, when breaking up an extent into two
2320 * pieces. This is the last block of an extent.
2322 ASSERT(error
!= -ENOSPC
);
2323 xfs_trans_brelse(tp
, bp
);
2327 * Return that we succeeded.