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_format.h"
10 #include "xfs_log_format.h"
11 #include "xfs_trans_resv.h"
12 #include "xfs_mount.h"
13 #include "xfs_da_format.h"
14 #include "xfs_da_btree.h"
15 #include "xfs_inode.h"
18 #include "xfs_dir2_priv.h"
19 #include "xfs_trace.h"
20 #include "xfs_trans.h"
21 #include "xfs_cksum.h"
24 * Function declarations.
26 static int xfs_dir2_leafn_add(struct xfs_buf
*bp
, xfs_da_args_t
*args
,
28 static void xfs_dir2_leafn_rebalance(xfs_da_state_t
*state
,
29 xfs_da_state_blk_t
*blk1
,
30 xfs_da_state_blk_t
*blk2
);
31 static int xfs_dir2_leafn_remove(xfs_da_args_t
*args
, struct xfs_buf
*bp
,
32 int index
, xfs_da_state_blk_t
*dblk
,
34 static int xfs_dir2_node_addname_int(xfs_da_args_t
*args
,
35 xfs_da_state_blk_t
*fblk
);
38 * Check internal consistency of a leafn block.
46 struct xfs_dir2_leaf
*leaf
= bp
->b_addr
;
47 struct xfs_dir3_icleaf_hdr leafhdr
;
49 dp
->d_ops
->leaf_hdr_from_disk(&leafhdr
, leaf
);
51 if (leafhdr
.magic
== XFS_DIR3_LEAFN_MAGIC
) {
52 struct xfs_dir3_leaf_hdr
*leaf3
= bp
->b_addr
;
53 if (be64_to_cpu(leaf3
->info
.blkno
) != bp
->b_bn
)
54 return __this_address
;
55 } else if (leafhdr
.magic
!= XFS_DIR2_LEAFN_MAGIC
)
56 return __this_address
;
58 return xfs_dir3_leaf_check_int(dp
->i_mount
, dp
, &leafhdr
, leaf
);
68 fa
= xfs_dir3_leafn_check(dp
, bp
);
71 xfs_corruption_error(__func__
, XFS_ERRLEVEL_LOW
, dp
->i_mount
,
72 bp
->b_addr
, BBTOB(bp
->b_length
), __FILE__
, __LINE__
,
77 #define xfs_dir3_leaf_check(dp, bp)
84 struct xfs_mount
*mp
= bp
->b_target
->bt_mount
;
85 struct xfs_dir2_free_hdr
*hdr
= bp
->b_addr
;
87 if (xfs_sb_version_hascrc(&mp
->m_sb
)) {
88 struct xfs_dir3_blk_hdr
*hdr3
= bp
->b_addr
;
90 if (hdr3
->magic
!= cpu_to_be32(XFS_DIR3_FREE_MAGIC
))
91 return __this_address
;
92 if (!uuid_equal(&hdr3
->uuid
, &mp
->m_sb
.sb_meta_uuid
))
93 return __this_address
;
94 if (be64_to_cpu(hdr3
->blkno
) != bp
->b_bn
)
95 return __this_address
;
96 if (!xfs_log_check_lsn(mp
, be64_to_cpu(hdr3
->lsn
)))
97 return __this_address
;
99 if (hdr
->magic
!= cpu_to_be32(XFS_DIR2_FREE_MAGIC
))
100 return __this_address
;
103 /* XXX: should bounds check the xfs_dir3_icfree_hdr here */
109 xfs_dir3_free_read_verify(
112 struct xfs_mount
*mp
= bp
->b_target
->bt_mount
;
115 if (xfs_sb_version_hascrc(&mp
->m_sb
) &&
116 !xfs_buf_verify_cksum(bp
, XFS_DIR3_FREE_CRC_OFF
))
117 xfs_verifier_error(bp
, -EFSBADCRC
, __this_address
);
119 fa
= xfs_dir3_free_verify(bp
);
121 xfs_verifier_error(bp
, -EFSCORRUPTED
, fa
);
126 xfs_dir3_free_write_verify(
129 struct xfs_mount
*mp
= bp
->b_target
->bt_mount
;
130 struct xfs_buf_log_item
*bip
= bp
->b_log_item
;
131 struct xfs_dir3_blk_hdr
*hdr3
= bp
->b_addr
;
134 fa
= xfs_dir3_free_verify(bp
);
136 xfs_verifier_error(bp
, -EFSCORRUPTED
, fa
);
140 if (!xfs_sb_version_hascrc(&mp
->m_sb
))
144 hdr3
->lsn
= cpu_to_be64(bip
->bli_item
.li_lsn
);
146 xfs_buf_update_cksum(bp
, XFS_DIR3_FREE_CRC_OFF
);
149 const struct xfs_buf_ops xfs_dir3_free_buf_ops
= {
150 .name
= "xfs_dir3_free",
151 .verify_read
= xfs_dir3_free_read_verify
,
152 .verify_write
= xfs_dir3_free_write_verify
,
153 .verify_struct
= xfs_dir3_free_verify
,
156 /* Everything ok in the free block header? */
157 static xfs_failaddr_t
158 xfs_dir3_free_header_check(
159 struct xfs_inode
*dp
,
163 struct xfs_mount
*mp
= dp
->i_mount
;
164 unsigned int firstdb
;
167 maxbests
= dp
->d_ops
->free_max_bests(mp
->m_dir_geo
);
168 firstdb
= (xfs_dir2_da_to_db(mp
->m_dir_geo
, fbno
) -
169 xfs_dir2_byte_to_db(mp
->m_dir_geo
, XFS_DIR2_FREE_OFFSET
)) *
171 if (xfs_sb_version_hascrc(&mp
->m_sb
)) {
172 struct xfs_dir3_free_hdr
*hdr3
= bp
->b_addr
;
174 if (be32_to_cpu(hdr3
->firstdb
) != firstdb
)
175 return __this_address
;
176 if (be32_to_cpu(hdr3
->nvalid
) > maxbests
)
177 return __this_address
;
178 if (be32_to_cpu(hdr3
->nvalid
) < be32_to_cpu(hdr3
->nused
))
179 return __this_address
;
181 struct xfs_dir2_free_hdr
*hdr
= bp
->b_addr
;
183 if (be32_to_cpu(hdr
->firstdb
) != firstdb
)
184 return __this_address
;
185 if (be32_to_cpu(hdr
->nvalid
) > maxbests
)
186 return __this_address
;
187 if (be32_to_cpu(hdr
->nvalid
) < be32_to_cpu(hdr
->nused
))
188 return __this_address
;
194 __xfs_dir3_free_read(
195 struct xfs_trans
*tp
,
196 struct xfs_inode
*dp
,
198 xfs_daddr_t mappedbno
,
199 struct xfs_buf
**bpp
)
204 err
= xfs_da_read_buf(tp
, dp
, fbno
, mappedbno
, bpp
,
205 XFS_DATA_FORK
, &xfs_dir3_free_buf_ops
);
209 /* Check things that we can't do in the verifier. */
210 fa
= xfs_dir3_free_header_check(dp
, fbno
, *bpp
);
212 xfs_verifier_error(*bpp
, -EFSCORRUPTED
, fa
);
213 xfs_trans_brelse(tp
, *bpp
);
214 return -EFSCORRUPTED
;
217 /* try read returns without an error or *bpp if it lands in a hole */
219 xfs_trans_buf_set_type(tp
, *bpp
, XFS_BLFT_DIR_FREE_BUF
);
226 struct xfs_trans
*tp
,
227 struct xfs_inode
*dp
,
229 struct xfs_buf
**bpp
)
231 return __xfs_dir3_free_read(tp
, dp
, fbno
, -1, bpp
);
235 xfs_dir2_free_try_read(
236 struct xfs_trans
*tp
,
237 struct xfs_inode
*dp
,
239 struct xfs_buf
**bpp
)
241 return __xfs_dir3_free_read(tp
, dp
, fbno
, -2, bpp
);
245 xfs_dir3_free_get_buf(
248 struct xfs_buf
**bpp
)
250 struct xfs_trans
*tp
= args
->trans
;
251 struct xfs_inode
*dp
= args
->dp
;
252 struct xfs_mount
*mp
= dp
->i_mount
;
255 struct xfs_dir3_icfree_hdr hdr
;
257 error
= xfs_da_get_buf(tp
, dp
, xfs_dir2_db_to_da(args
->geo
, fbno
),
258 -1, &bp
, XFS_DATA_FORK
);
262 xfs_trans_buf_set_type(tp
, bp
, XFS_BLFT_DIR_FREE_BUF
);
263 bp
->b_ops
= &xfs_dir3_free_buf_ops
;
266 * Initialize the new block to be empty, and remember
267 * its first slot as our empty slot.
269 memset(bp
->b_addr
, 0, sizeof(struct xfs_dir3_free_hdr
));
270 memset(&hdr
, 0, sizeof(hdr
));
272 if (xfs_sb_version_hascrc(&mp
->m_sb
)) {
273 struct xfs_dir3_free_hdr
*hdr3
= bp
->b_addr
;
275 hdr
.magic
= XFS_DIR3_FREE_MAGIC
;
277 hdr3
->hdr
.blkno
= cpu_to_be64(bp
->b_bn
);
278 hdr3
->hdr
.owner
= cpu_to_be64(dp
->i_ino
);
279 uuid_copy(&hdr3
->hdr
.uuid
, &mp
->m_sb
.sb_meta_uuid
);
281 hdr
.magic
= XFS_DIR2_FREE_MAGIC
;
282 dp
->d_ops
->free_hdr_to_disk(bp
->b_addr
, &hdr
);
288 * Log entries from a freespace block.
291 xfs_dir2_free_log_bests(
292 struct xfs_da_args
*args
,
294 int first
, /* first entry to log */
295 int last
) /* last entry to log */
297 xfs_dir2_free_t
*free
; /* freespace structure */
301 bests
= args
->dp
->d_ops
->free_bests_p(free
);
302 ASSERT(free
->hdr
.magic
== cpu_to_be32(XFS_DIR2_FREE_MAGIC
) ||
303 free
->hdr
.magic
== cpu_to_be32(XFS_DIR3_FREE_MAGIC
));
304 xfs_trans_log_buf(args
->trans
, bp
,
305 (uint
)((char *)&bests
[first
] - (char *)free
),
306 (uint
)((char *)&bests
[last
] - (char *)free
+
307 sizeof(bests
[0]) - 1));
311 * Log header from a freespace block.
314 xfs_dir2_free_log_header(
315 struct xfs_da_args
*args
,
319 xfs_dir2_free_t
*free
; /* freespace structure */
322 ASSERT(free
->hdr
.magic
== cpu_to_be32(XFS_DIR2_FREE_MAGIC
) ||
323 free
->hdr
.magic
== cpu_to_be32(XFS_DIR3_FREE_MAGIC
));
325 xfs_trans_log_buf(args
->trans
, bp
, 0,
326 args
->dp
->d_ops
->free_hdr_size
- 1);
330 * Convert a leaf-format directory to a node-format directory.
331 * We need to change the magic number of the leaf block, and copy
332 * the freespace table out of the leaf block into its own block.
335 xfs_dir2_leaf_to_node(
336 xfs_da_args_t
*args
, /* operation arguments */
337 struct xfs_buf
*lbp
) /* leaf buffer */
339 xfs_inode_t
*dp
; /* incore directory inode */
340 int error
; /* error return value */
341 struct xfs_buf
*fbp
; /* freespace buffer */
342 xfs_dir2_db_t fdb
; /* freespace block number */
343 xfs_dir2_free_t
*free
; /* freespace structure */
344 __be16
*from
; /* pointer to freespace entry */
345 int i
; /* leaf freespace index */
346 xfs_dir2_leaf_t
*leaf
; /* leaf structure */
347 xfs_dir2_leaf_tail_t
*ltp
; /* leaf tail structure */
348 int n
; /* count of live freespc ents */
349 xfs_dir2_data_off_t off
; /* freespace entry value */
350 __be16
*to
; /* pointer to freespace entry */
351 xfs_trans_t
*tp
; /* transaction pointer */
352 struct xfs_dir3_icfree_hdr freehdr
;
354 trace_xfs_dir2_leaf_to_node(args
);
359 * Add a freespace block to the directory.
361 if ((error
= xfs_dir2_grow_inode(args
, XFS_DIR2_FREE_SPACE
, &fdb
))) {
364 ASSERT(fdb
== xfs_dir2_byte_to_db(args
->geo
, XFS_DIR2_FREE_OFFSET
));
366 * Get the buffer for the new freespace block.
368 error
= xfs_dir3_free_get_buf(args
, fdb
, &fbp
);
373 dp
->d_ops
->free_hdr_from_disk(&freehdr
, free
);
375 ltp
= xfs_dir2_leaf_tail_p(args
->geo
, leaf
);
376 if (be32_to_cpu(ltp
->bestcount
) >
377 (uint
)dp
->i_d
.di_size
/ args
->geo
->blksize
)
378 return -EFSCORRUPTED
;
381 * Copy freespace entries from the leaf block to the new block.
382 * Count active entries.
384 from
= xfs_dir2_leaf_bests_p(ltp
);
385 to
= dp
->d_ops
->free_bests_p(free
);
386 for (i
= n
= 0; i
< be32_to_cpu(ltp
->bestcount
); i
++, from
++, to
++) {
387 if ((off
= be16_to_cpu(*from
)) != NULLDATAOFF
)
389 *to
= cpu_to_be16(off
);
393 * Now initialize the freespace block header.
396 freehdr
.nvalid
= be32_to_cpu(ltp
->bestcount
);
398 dp
->d_ops
->free_hdr_to_disk(fbp
->b_addr
, &freehdr
);
399 xfs_dir2_free_log_bests(args
, fbp
, 0, freehdr
.nvalid
- 1);
400 xfs_dir2_free_log_header(args
, fbp
);
403 * Converting the leaf to a leafnode is just a matter of changing the
404 * magic number and the ops. Do the change directly to the buffer as
405 * it's less work (and less code) than decoding the header to host
406 * format and back again.
408 if (leaf
->hdr
.info
.magic
== cpu_to_be16(XFS_DIR2_LEAF1_MAGIC
))
409 leaf
->hdr
.info
.magic
= cpu_to_be16(XFS_DIR2_LEAFN_MAGIC
);
411 leaf
->hdr
.info
.magic
= cpu_to_be16(XFS_DIR3_LEAFN_MAGIC
);
412 lbp
->b_ops
= &xfs_dir3_leafn_buf_ops
;
413 xfs_trans_buf_set_type(tp
, lbp
, XFS_BLFT_DIR_LEAFN_BUF
);
414 xfs_dir3_leaf_log_header(args
, lbp
);
415 xfs_dir3_leaf_check(dp
, lbp
);
420 * Add a leaf entry to a leaf block in a node-form directory.
421 * The other work necessary is done from the caller.
423 static int /* error */
425 struct xfs_buf
*bp
, /* leaf buffer */
426 xfs_da_args_t
*args
, /* operation arguments */
427 int index
) /* insertion pt for new entry */
429 int compact
; /* compacting stale leaves */
430 xfs_inode_t
*dp
; /* incore directory inode */
431 int highstale
; /* next stale entry */
432 xfs_dir2_leaf_t
*leaf
; /* leaf structure */
433 xfs_dir2_leaf_entry_t
*lep
; /* leaf entry */
434 int lfloghigh
; /* high leaf entry logging */
435 int lfloglow
; /* low leaf entry logging */
436 int lowstale
; /* previous stale entry */
437 struct xfs_dir3_icleaf_hdr leafhdr
;
438 struct xfs_dir2_leaf_entry
*ents
;
440 trace_xfs_dir2_leafn_add(args
, index
);
444 dp
->d_ops
->leaf_hdr_from_disk(&leafhdr
, leaf
);
445 ents
= dp
->d_ops
->leaf_ents_p(leaf
);
448 * Quick check just to make sure we are not going to index
449 * into other peoples memory
452 return -EFSCORRUPTED
;
455 * If there are already the maximum number of leaf entries in
456 * the block, if there are no stale entries it won't fit.
457 * Caller will do a split. If there are stale entries we'll do
461 if (leafhdr
.count
== dp
->d_ops
->leaf_max_ents(args
->geo
)) {
464 compact
= leafhdr
.stale
> 1;
467 ASSERT(index
== 0 || be32_to_cpu(ents
[index
- 1].hashval
) <= args
->hashval
);
468 ASSERT(index
== leafhdr
.count
||
469 be32_to_cpu(ents
[index
].hashval
) >= args
->hashval
);
471 if (args
->op_flags
& XFS_DA_OP_JUSTCHECK
)
475 * Compact out all but one stale leaf entry. Leaves behind
476 * the entry closest to index.
479 xfs_dir3_leaf_compact_x1(&leafhdr
, ents
, &index
, &lowstale
,
480 &highstale
, &lfloglow
, &lfloghigh
);
481 else if (leafhdr
.stale
) {
483 * Set impossible logging indices for this case.
485 lfloglow
= leafhdr
.count
;
490 * Insert the new entry, log everything.
492 lep
= xfs_dir3_leaf_find_entry(&leafhdr
, ents
, index
, compact
, lowstale
,
493 highstale
, &lfloglow
, &lfloghigh
);
495 lep
->hashval
= cpu_to_be32(args
->hashval
);
496 lep
->address
= cpu_to_be32(xfs_dir2_db_off_to_dataptr(args
->geo
,
497 args
->blkno
, args
->index
));
499 dp
->d_ops
->leaf_hdr_to_disk(leaf
, &leafhdr
);
500 xfs_dir3_leaf_log_header(args
, bp
);
501 xfs_dir3_leaf_log_ents(args
, bp
, lfloglow
, lfloghigh
);
502 xfs_dir3_leaf_check(dp
, bp
);
508 xfs_dir2_free_hdr_check(
509 struct xfs_inode
*dp
,
513 struct xfs_dir3_icfree_hdr hdr
;
515 dp
->d_ops
->free_hdr_from_disk(&hdr
, bp
->b_addr
);
517 ASSERT((hdr
.firstdb
%
518 dp
->d_ops
->free_max_bests(dp
->i_mount
->m_dir_geo
)) == 0);
519 ASSERT(hdr
.firstdb
<= db
);
520 ASSERT(db
< hdr
.firstdb
+ hdr
.nvalid
);
523 #define xfs_dir2_free_hdr_check(dp, bp, db)
527 * Return the last hash value in the leaf.
528 * Stale entries are ok.
530 xfs_dahash_t
/* hash value */
531 xfs_dir2_leaf_lasthash(
532 struct xfs_inode
*dp
,
533 struct xfs_buf
*bp
, /* leaf buffer */
534 int *count
) /* count of entries in leaf */
536 struct xfs_dir2_leaf
*leaf
= bp
->b_addr
;
537 struct xfs_dir2_leaf_entry
*ents
;
538 struct xfs_dir3_icleaf_hdr leafhdr
;
540 dp
->d_ops
->leaf_hdr_from_disk(&leafhdr
, leaf
);
542 ASSERT(leafhdr
.magic
== XFS_DIR2_LEAFN_MAGIC
||
543 leafhdr
.magic
== XFS_DIR3_LEAFN_MAGIC
||
544 leafhdr
.magic
== XFS_DIR2_LEAF1_MAGIC
||
545 leafhdr
.magic
== XFS_DIR3_LEAF1_MAGIC
);
548 *count
= leafhdr
.count
;
552 ents
= dp
->d_ops
->leaf_ents_p(leaf
);
553 return be32_to_cpu(ents
[leafhdr
.count
- 1].hashval
);
557 * Look up a leaf entry for space to add a name in a node-format leaf block.
558 * The extrablk in state is a freespace block.
561 xfs_dir2_leafn_lookup_for_addname(
562 struct xfs_buf
*bp
, /* leaf buffer */
563 xfs_da_args_t
*args
, /* operation arguments */
564 int *indexp
, /* out: leaf entry index */
565 xfs_da_state_t
*state
) /* state to fill in */
567 struct xfs_buf
*curbp
= NULL
; /* current data/free buffer */
568 xfs_dir2_db_t curdb
= -1; /* current data block number */
569 xfs_dir2_db_t curfdb
= -1; /* current free block number */
570 xfs_inode_t
*dp
; /* incore directory inode */
571 int error
; /* error return value */
572 int fi
; /* free entry index */
573 xfs_dir2_free_t
*free
= NULL
; /* free block structure */
574 int index
; /* leaf entry index */
575 xfs_dir2_leaf_t
*leaf
; /* leaf structure */
576 int length
; /* length of new data entry */
577 xfs_dir2_leaf_entry_t
*lep
; /* leaf entry */
578 xfs_mount_t
*mp
; /* filesystem mount point */
579 xfs_dir2_db_t newdb
; /* new data block number */
580 xfs_dir2_db_t newfdb
; /* new free block number */
581 xfs_trans_t
*tp
; /* transaction pointer */
582 struct xfs_dir2_leaf_entry
*ents
;
583 struct xfs_dir3_icleaf_hdr leafhdr
;
589 dp
->d_ops
->leaf_hdr_from_disk(&leafhdr
, leaf
);
590 ents
= dp
->d_ops
->leaf_ents_p(leaf
);
592 xfs_dir3_leaf_check(dp
, bp
);
593 ASSERT(leafhdr
.count
> 0);
596 * Look up the hash value in the leaf entries.
598 index
= xfs_dir2_leaf_search_hash(args
, bp
);
600 * Do we have a buffer coming in?
602 if (state
->extravalid
) {
603 /* If so, it's a free block buffer, get the block number. */
604 curbp
= state
->extrablk
.bp
;
605 curfdb
= state
->extrablk
.blkno
;
606 free
= curbp
->b_addr
;
607 ASSERT(free
->hdr
.magic
== cpu_to_be32(XFS_DIR2_FREE_MAGIC
) ||
608 free
->hdr
.magic
== cpu_to_be32(XFS_DIR3_FREE_MAGIC
));
610 length
= dp
->d_ops
->data_entsize(args
->namelen
);
612 * Loop over leaf entries with the right hash value.
614 for (lep
= &ents
[index
];
615 index
< leafhdr
.count
&& be32_to_cpu(lep
->hashval
) == args
->hashval
;
618 * Skip stale leaf entries.
620 if (be32_to_cpu(lep
->address
) == XFS_DIR2_NULL_DATAPTR
)
623 * Pull the data block number from the entry.
625 newdb
= xfs_dir2_dataptr_to_db(args
->geo
,
626 be32_to_cpu(lep
->address
));
628 * For addname, we're looking for a place to put the new entry.
629 * We want to use a data block with an entry of equal
630 * hash value to ours if there is one with room.
632 * If this block isn't the data block we already have
633 * in hand, take a look at it.
635 if (newdb
!= curdb
) {
640 * Convert the data block to the free block
641 * holding its freespace information.
643 newfdb
= dp
->d_ops
->db_to_fdb(args
->geo
, newdb
);
645 * If it's not the one we have in hand, read it in.
647 if (newfdb
!= curfdb
) {
649 * If we had one before, drop it.
652 xfs_trans_brelse(tp
, curbp
);
654 error
= xfs_dir2_free_read(tp
, dp
,
655 xfs_dir2_db_to_da(args
->geo
,
660 free
= curbp
->b_addr
;
662 xfs_dir2_free_hdr_check(dp
, curbp
, curdb
);
665 * Get the index for our entry.
667 fi
= dp
->d_ops
->db_to_fdindex(args
->geo
, curdb
);
669 * If it has room, return it.
671 bests
= dp
->d_ops
->free_bests_p(free
);
672 if (unlikely(bests
[fi
] == cpu_to_be16(NULLDATAOFF
))) {
673 XFS_ERROR_REPORT("xfs_dir2_leafn_lookup_int",
674 XFS_ERRLEVEL_LOW
, mp
);
675 if (curfdb
!= newfdb
)
676 xfs_trans_brelse(tp
, curbp
);
677 return -EFSCORRUPTED
;
680 if (be16_to_cpu(bests
[fi
]) >= length
)
684 /* Didn't find any space */
687 ASSERT(args
->op_flags
& XFS_DA_OP_OKNOENT
);
689 /* Giving back a free block. */
690 state
->extravalid
= 1;
691 state
->extrablk
.bp
= curbp
;
692 state
->extrablk
.index
= fi
;
693 state
->extrablk
.blkno
= curfdb
;
696 * Important: this magic number is not in the buffer - it's for
697 * buffer type information and therefore only the free/data type
698 * matters here, not whether CRCs are enabled or not.
700 state
->extrablk
.magic
= XFS_DIR2_FREE_MAGIC
;
702 state
->extravalid
= 0;
705 * Return the index, that will be the insertion point.
712 * Look up a leaf entry in a node-format leaf block.
713 * The extrablk in state a data block.
716 xfs_dir2_leafn_lookup_for_entry(
717 struct xfs_buf
*bp
, /* leaf buffer */
718 xfs_da_args_t
*args
, /* operation arguments */
719 int *indexp
, /* out: leaf entry index */
720 xfs_da_state_t
*state
) /* state to fill in */
722 struct xfs_buf
*curbp
= NULL
; /* current data/free buffer */
723 xfs_dir2_db_t curdb
= -1; /* current data block number */
724 xfs_dir2_data_entry_t
*dep
; /* data block entry */
725 xfs_inode_t
*dp
; /* incore directory inode */
726 int error
; /* error return value */
727 int index
; /* leaf entry index */
728 xfs_dir2_leaf_t
*leaf
; /* leaf structure */
729 xfs_dir2_leaf_entry_t
*lep
; /* leaf entry */
730 xfs_mount_t
*mp
; /* filesystem mount point */
731 xfs_dir2_db_t newdb
; /* new data block number */
732 xfs_trans_t
*tp
; /* transaction pointer */
733 enum xfs_dacmp cmp
; /* comparison result */
734 struct xfs_dir2_leaf_entry
*ents
;
735 struct xfs_dir3_icleaf_hdr leafhdr
;
741 dp
->d_ops
->leaf_hdr_from_disk(&leafhdr
, leaf
);
742 ents
= dp
->d_ops
->leaf_ents_p(leaf
);
744 xfs_dir3_leaf_check(dp
, bp
);
745 ASSERT(leafhdr
.count
> 0);
748 * Look up the hash value in the leaf entries.
750 index
= xfs_dir2_leaf_search_hash(args
, bp
);
752 * Do we have a buffer coming in?
754 if (state
->extravalid
) {
755 curbp
= state
->extrablk
.bp
;
756 curdb
= state
->extrablk
.blkno
;
759 * Loop over leaf entries with the right hash value.
761 for (lep
= &ents
[index
];
762 index
< leafhdr
.count
&& be32_to_cpu(lep
->hashval
) == args
->hashval
;
765 * Skip stale leaf entries.
767 if (be32_to_cpu(lep
->address
) == XFS_DIR2_NULL_DATAPTR
)
770 * Pull the data block number from the entry.
772 newdb
= xfs_dir2_dataptr_to_db(args
->geo
,
773 be32_to_cpu(lep
->address
));
775 * Not adding a new entry, so we really want to find
776 * the name given to us.
778 * If it's a different data block, go get it.
780 if (newdb
!= curdb
) {
782 * If we had a block before that we aren't saving
783 * for a CI name, drop it
785 if (curbp
&& (args
->cmpresult
== XFS_CMP_DIFFERENT
||
786 curdb
!= state
->extrablk
.blkno
))
787 xfs_trans_brelse(tp
, curbp
);
789 * If needing the block that is saved with a CI match,
790 * use it otherwise read in the new data block.
792 if (args
->cmpresult
!= XFS_CMP_DIFFERENT
&&
793 newdb
== state
->extrablk
.blkno
) {
794 ASSERT(state
->extravalid
);
795 curbp
= state
->extrablk
.bp
;
797 error
= xfs_dir3_data_read(tp
, dp
,
798 xfs_dir2_db_to_da(args
->geo
,
804 xfs_dir3_data_check(dp
, curbp
);
808 * Point to the data entry.
810 dep
= (xfs_dir2_data_entry_t
*)((char *)curbp
->b_addr
+
811 xfs_dir2_dataptr_to_off(args
->geo
,
812 be32_to_cpu(lep
->address
)));
814 * Compare the entry and if it's an exact match, return
815 * EEXIST immediately. If it's the first case-insensitive
816 * match, store the block & inode number and continue looking.
818 cmp
= mp
->m_dirnameops
->compname(args
, dep
->name
, dep
->namelen
);
819 if (cmp
!= XFS_CMP_DIFFERENT
&& cmp
!= args
->cmpresult
) {
820 /* If there is a CI match block, drop it */
821 if (args
->cmpresult
!= XFS_CMP_DIFFERENT
&&
822 curdb
!= state
->extrablk
.blkno
)
823 xfs_trans_brelse(tp
, state
->extrablk
.bp
);
824 args
->cmpresult
= cmp
;
825 args
->inumber
= be64_to_cpu(dep
->inumber
);
826 args
->filetype
= dp
->d_ops
->data_get_ftype(dep
);
828 state
->extravalid
= 1;
829 state
->extrablk
.bp
= curbp
;
830 state
->extrablk
.blkno
= curdb
;
831 state
->extrablk
.index
= (int)((char *)dep
-
832 (char *)curbp
->b_addr
);
833 state
->extrablk
.magic
= XFS_DIR2_DATA_MAGIC
;
834 curbp
->b_ops
= &xfs_dir3_data_buf_ops
;
835 xfs_trans_buf_set_type(tp
, curbp
, XFS_BLFT_DIR_DATA_BUF
);
836 if (cmp
== XFS_CMP_EXACT
)
840 ASSERT(index
== leafhdr
.count
|| (args
->op_flags
& XFS_DA_OP_OKNOENT
));
842 if (args
->cmpresult
== XFS_CMP_DIFFERENT
) {
843 /* Giving back last used data block. */
844 state
->extravalid
= 1;
845 state
->extrablk
.bp
= curbp
;
846 state
->extrablk
.index
= -1;
847 state
->extrablk
.blkno
= curdb
;
848 state
->extrablk
.magic
= XFS_DIR2_DATA_MAGIC
;
849 curbp
->b_ops
= &xfs_dir3_data_buf_ops
;
850 xfs_trans_buf_set_type(tp
, curbp
, XFS_BLFT_DIR_DATA_BUF
);
852 /* If the curbp is not the CI match block, drop it */
853 if (state
->extrablk
.bp
!= curbp
)
854 xfs_trans_brelse(tp
, curbp
);
857 state
->extravalid
= 0;
864 * Look up a leaf entry in a node-format leaf block.
865 * If this is an addname then the extrablk in state is a freespace block,
866 * otherwise it's a data block.
869 xfs_dir2_leafn_lookup_int(
870 struct xfs_buf
*bp
, /* leaf buffer */
871 xfs_da_args_t
*args
, /* operation arguments */
872 int *indexp
, /* out: leaf entry index */
873 xfs_da_state_t
*state
) /* state to fill in */
875 if (args
->op_flags
& XFS_DA_OP_ADDNAME
)
876 return xfs_dir2_leafn_lookup_for_addname(bp
, args
, indexp
,
878 return xfs_dir2_leafn_lookup_for_entry(bp
, args
, indexp
, state
);
882 * Move count leaf entries from source to destination leaf.
883 * Log entries and headers. Stale entries are preserved.
886 xfs_dir3_leafn_moveents(
887 xfs_da_args_t
*args
, /* operation arguments */
888 struct xfs_buf
*bp_s
, /* source */
889 struct xfs_dir3_icleaf_hdr
*shdr
,
890 struct xfs_dir2_leaf_entry
*sents
,
891 int start_s
,/* source leaf index */
892 struct xfs_buf
*bp_d
, /* destination */
893 struct xfs_dir3_icleaf_hdr
*dhdr
,
894 struct xfs_dir2_leaf_entry
*dents
,
895 int start_d
,/* destination leaf index */
896 int count
) /* count of leaves to copy */
898 int stale
; /* count stale leaves copied */
900 trace_xfs_dir2_leafn_moveents(args
, start_s
, start_d
, count
);
903 * Silently return if nothing to do.
909 * If the destination index is not the end of the current
910 * destination leaf entries, open up a hole in the destination
911 * to hold the new entries.
913 if (start_d
< dhdr
->count
) {
914 memmove(&dents
[start_d
+ count
], &dents
[start_d
],
915 (dhdr
->count
- start_d
) * sizeof(xfs_dir2_leaf_entry_t
));
916 xfs_dir3_leaf_log_ents(args
, bp_d
, start_d
+ count
,
917 count
+ dhdr
->count
- 1);
920 * If the source has stale leaves, count the ones in the copy range
921 * so we can update the header correctly.
924 int i
; /* temp leaf index */
926 for (i
= start_s
, stale
= 0; i
< start_s
+ count
; i
++) {
927 if (sents
[i
].address
==
928 cpu_to_be32(XFS_DIR2_NULL_DATAPTR
))
934 * Copy the leaf entries from source to destination.
936 memcpy(&dents
[start_d
], &sents
[start_s
],
937 count
* sizeof(xfs_dir2_leaf_entry_t
));
938 xfs_dir3_leaf_log_ents(args
, bp_d
, start_d
, start_d
+ count
- 1);
941 * If there are source entries after the ones we copied,
942 * delete the ones we copied by sliding the next ones down.
944 if (start_s
+ count
< shdr
->count
) {
945 memmove(&sents
[start_s
], &sents
[start_s
+ count
],
946 count
* sizeof(xfs_dir2_leaf_entry_t
));
947 xfs_dir3_leaf_log_ents(args
, bp_s
, start_s
, start_s
+ count
- 1);
951 * Update the headers and log them.
953 shdr
->count
-= count
;
954 shdr
->stale
-= stale
;
955 dhdr
->count
+= count
;
956 dhdr
->stale
+= stale
;
960 * Determine the sort order of two leaf blocks.
961 * Returns 1 if both are valid and leaf2 should be before leaf1, else 0.
964 xfs_dir2_leafn_order(
965 struct xfs_inode
*dp
,
966 struct xfs_buf
*leaf1_bp
, /* leaf1 buffer */
967 struct xfs_buf
*leaf2_bp
) /* leaf2 buffer */
969 struct xfs_dir2_leaf
*leaf1
= leaf1_bp
->b_addr
;
970 struct xfs_dir2_leaf
*leaf2
= leaf2_bp
->b_addr
;
971 struct xfs_dir2_leaf_entry
*ents1
;
972 struct xfs_dir2_leaf_entry
*ents2
;
973 struct xfs_dir3_icleaf_hdr hdr1
;
974 struct xfs_dir3_icleaf_hdr hdr2
;
976 dp
->d_ops
->leaf_hdr_from_disk(&hdr1
, leaf1
);
977 dp
->d_ops
->leaf_hdr_from_disk(&hdr2
, leaf2
);
978 ents1
= dp
->d_ops
->leaf_ents_p(leaf1
);
979 ents2
= dp
->d_ops
->leaf_ents_p(leaf2
);
981 if (hdr1
.count
> 0 && hdr2
.count
> 0 &&
982 (be32_to_cpu(ents2
[0].hashval
) < be32_to_cpu(ents1
[0].hashval
) ||
983 be32_to_cpu(ents2
[hdr2
.count
- 1].hashval
) <
984 be32_to_cpu(ents1
[hdr1
.count
- 1].hashval
)))
990 * Rebalance leaf entries between two leaf blocks.
991 * This is actually only called when the second block is new,
992 * though the code deals with the general case.
993 * A new entry will be inserted in one of the blocks, and that
994 * entry is taken into account when balancing.
997 xfs_dir2_leafn_rebalance(
998 xfs_da_state_t
*state
, /* btree cursor */
999 xfs_da_state_blk_t
*blk1
, /* first btree block */
1000 xfs_da_state_blk_t
*blk2
) /* second btree block */
1002 xfs_da_args_t
*args
; /* operation arguments */
1003 int count
; /* count (& direction) leaves */
1004 int isleft
; /* new goes in left leaf */
1005 xfs_dir2_leaf_t
*leaf1
; /* first leaf structure */
1006 xfs_dir2_leaf_t
*leaf2
; /* second leaf structure */
1007 int mid
; /* midpoint leaf index */
1008 #if defined(DEBUG) || defined(XFS_WARN)
1009 int oldstale
; /* old count of stale leaves */
1011 int oldsum
; /* old total leaf count */
1012 int swap_blocks
; /* swapped leaf blocks */
1013 struct xfs_dir2_leaf_entry
*ents1
;
1014 struct xfs_dir2_leaf_entry
*ents2
;
1015 struct xfs_dir3_icleaf_hdr hdr1
;
1016 struct xfs_dir3_icleaf_hdr hdr2
;
1017 struct xfs_inode
*dp
= state
->args
->dp
;
1021 * If the block order is wrong, swap the arguments.
1023 swap_blocks
= xfs_dir2_leafn_order(dp
, blk1
->bp
, blk2
->bp
);
1027 leaf1
= blk1
->bp
->b_addr
;
1028 leaf2
= blk2
->bp
->b_addr
;
1029 dp
->d_ops
->leaf_hdr_from_disk(&hdr1
, leaf1
);
1030 dp
->d_ops
->leaf_hdr_from_disk(&hdr2
, leaf2
);
1031 ents1
= dp
->d_ops
->leaf_ents_p(leaf1
);
1032 ents2
= dp
->d_ops
->leaf_ents_p(leaf2
);
1034 oldsum
= hdr1
.count
+ hdr2
.count
;
1035 #if defined(DEBUG) || defined(XFS_WARN)
1036 oldstale
= hdr1
.stale
+ hdr2
.stale
;
1041 * If the old leaf count was odd then the new one will be even,
1042 * so we need to divide the new count evenly.
1045 xfs_dahash_t midhash
; /* middle entry hash value */
1047 if (mid
>= hdr1
.count
)
1048 midhash
= be32_to_cpu(ents2
[mid
- hdr1
.count
].hashval
);
1050 midhash
= be32_to_cpu(ents1
[mid
].hashval
);
1051 isleft
= args
->hashval
<= midhash
;
1054 * If the old count is even then the new count is odd, so there's
1055 * no preferred side for the new entry.
1056 * Pick the left one.
1061 * Calculate moved entry count. Positive means left-to-right,
1062 * negative means right-to-left. Then move the entries.
1064 count
= hdr1
.count
- mid
+ (isleft
== 0);
1066 xfs_dir3_leafn_moveents(args
, blk1
->bp
, &hdr1
, ents1
,
1067 hdr1
.count
- count
, blk2
->bp
,
1068 &hdr2
, ents2
, 0, count
);
1070 xfs_dir3_leafn_moveents(args
, blk2
->bp
, &hdr2
, ents2
, 0,
1071 blk1
->bp
, &hdr1
, ents1
,
1074 ASSERT(hdr1
.count
+ hdr2
.count
== oldsum
);
1075 ASSERT(hdr1
.stale
+ hdr2
.stale
== oldstale
);
1077 /* log the changes made when moving the entries */
1078 dp
->d_ops
->leaf_hdr_to_disk(leaf1
, &hdr1
);
1079 dp
->d_ops
->leaf_hdr_to_disk(leaf2
, &hdr2
);
1080 xfs_dir3_leaf_log_header(args
, blk1
->bp
);
1081 xfs_dir3_leaf_log_header(args
, blk2
->bp
);
1083 xfs_dir3_leaf_check(dp
, blk1
->bp
);
1084 xfs_dir3_leaf_check(dp
, blk2
->bp
);
1087 * Mark whether we're inserting into the old or new leaf.
1089 if (hdr1
.count
< hdr2
.count
)
1090 state
->inleaf
= swap_blocks
;
1091 else if (hdr1
.count
> hdr2
.count
)
1092 state
->inleaf
= !swap_blocks
;
1094 state
->inleaf
= swap_blocks
^ (blk1
->index
<= hdr1
.count
);
1096 * Adjust the expected index for insertion.
1099 blk2
->index
= blk1
->index
- hdr1
.count
;
1102 * Finally sanity check just to make sure we are not returning a
1105 if (blk2
->index
< 0) {
1108 xfs_alert(dp
->i_mount
,
1109 "%s: picked the wrong leaf? reverting original leaf: blk1->index %d",
1110 __func__
, blk1
->index
);
1115 xfs_dir3_data_block_free(
1116 xfs_da_args_t
*args
,
1117 struct xfs_dir2_data_hdr
*hdr
,
1118 struct xfs_dir2_free
*free
,
1121 struct xfs_buf
*fbp
,
1126 struct xfs_dir3_icfree_hdr freehdr
;
1127 struct xfs_inode
*dp
= args
->dp
;
1129 dp
->d_ops
->free_hdr_from_disk(&freehdr
, free
);
1130 bests
= dp
->d_ops
->free_bests_p(free
);
1133 * Data block is not empty, just set the free entry to the new
1136 bests
[findex
] = cpu_to_be16(longest
);
1137 xfs_dir2_free_log_bests(args
, fbp
, findex
, findex
);
1141 /* One less used entry in the free table. */
1145 * If this was the last entry in the table, we can trim the table size
1146 * back. There might be other entries at the end referring to
1147 * non-existent data blocks, get those too.
1149 if (findex
== freehdr
.nvalid
- 1) {
1150 int i
; /* free entry index */
1152 for (i
= findex
- 1; i
>= 0; i
--) {
1153 if (bests
[i
] != cpu_to_be16(NULLDATAOFF
))
1156 freehdr
.nvalid
= i
+ 1;
1159 /* Not the last entry, just punch it out. */
1160 bests
[findex
] = cpu_to_be16(NULLDATAOFF
);
1164 dp
->d_ops
->free_hdr_to_disk(free
, &freehdr
);
1165 xfs_dir2_free_log_header(args
, fbp
);
1168 * If there are no useful entries left in the block, get rid of the
1171 if (!freehdr
.nused
) {
1174 error
= xfs_dir2_shrink_inode(args
, fdb
, fbp
);
1178 } else if (error
!= -ENOSPC
|| args
->total
!= 0)
1181 * It's possible to get ENOSPC if there is no
1182 * space reservation. In this case some one
1183 * else will eventually get rid of this block.
1187 /* Log the free entry that changed, unless we got rid of it. */
1189 xfs_dir2_free_log_bests(args
, fbp
, findex
, findex
);
1194 * Remove an entry from a node directory.
1195 * This removes the leaf entry and the data entry,
1196 * and updates the free block if necessary.
1198 static int /* error */
1199 xfs_dir2_leafn_remove(
1200 xfs_da_args_t
*args
, /* operation arguments */
1201 struct xfs_buf
*bp
, /* leaf buffer */
1202 int index
, /* leaf entry index */
1203 xfs_da_state_blk_t
*dblk
, /* data block */
1204 int *rval
) /* resulting block needs join */
1206 xfs_dir2_data_hdr_t
*hdr
; /* data block header */
1207 xfs_dir2_db_t db
; /* data block number */
1208 struct xfs_buf
*dbp
; /* data block buffer */
1209 xfs_dir2_data_entry_t
*dep
; /* data block entry */
1210 xfs_inode_t
*dp
; /* incore directory inode */
1211 xfs_dir2_leaf_t
*leaf
; /* leaf structure */
1212 xfs_dir2_leaf_entry_t
*lep
; /* leaf entry */
1213 int longest
; /* longest data free entry */
1214 int off
; /* data block entry offset */
1215 int needlog
; /* need to log data header */
1216 int needscan
; /* need to rescan data frees */
1217 xfs_trans_t
*tp
; /* transaction pointer */
1218 struct xfs_dir2_data_free
*bf
; /* bestfree table */
1219 struct xfs_dir3_icleaf_hdr leafhdr
;
1220 struct xfs_dir2_leaf_entry
*ents
;
1222 trace_xfs_dir2_leafn_remove(args
, index
);
1227 dp
->d_ops
->leaf_hdr_from_disk(&leafhdr
, leaf
);
1228 ents
= dp
->d_ops
->leaf_ents_p(leaf
);
1231 * Point to the entry we're removing.
1236 * Extract the data block and offset from the entry.
1238 db
= xfs_dir2_dataptr_to_db(args
->geo
, be32_to_cpu(lep
->address
));
1239 ASSERT(dblk
->blkno
== db
);
1240 off
= xfs_dir2_dataptr_to_off(args
->geo
, be32_to_cpu(lep
->address
));
1241 ASSERT(dblk
->index
== off
);
1244 * Kill the leaf entry by marking it stale.
1245 * Log the leaf block changes.
1248 dp
->d_ops
->leaf_hdr_to_disk(leaf
, &leafhdr
);
1249 xfs_dir3_leaf_log_header(args
, bp
);
1251 lep
->address
= cpu_to_be32(XFS_DIR2_NULL_DATAPTR
);
1252 xfs_dir3_leaf_log_ents(args
, bp
, index
, index
);
1255 * Make the data entry free. Keep track of the longest freespace
1256 * in the data block in case it changes.
1260 dep
= (xfs_dir2_data_entry_t
*)((char *)hdr
+ off
);
1261 bf
= dp
->d_ops
->data_bestfree_p(hdr
);
1262 longest
= be16_to_cpu(bf
[0].length
);
1263 needlog
= needscan
= 0;
1264 xfs_dir2_data_make_free(args
, dbp
, off
,
1265 dp
->d_ops
->data_entsize(dep
->namelen
), &needlog
, &needscan
);
1267 * Rescan the data block freespaces for bestfree.
1268 * Log the data block header if needed.
1271 xfs_dir2_data_freescan(dp
, hdr
, &needlog
);
1273 xfs_dir2_data_log_header(args
, dbp
);
1274 xfs_dir3_data_check(dp
, dbp
);
1276 * If the longest data block freespace changes, need to update
1277 * the corresponding freeblock entry.
1279 if (longest
< be16_to_cpu(bf
[0].length
)) {
1280 int error
; /* error return value */
1281 struct xfs_buf
*fbp
; /* freeblock buffer */
1282 xfs_dir2_db_t fdb
; /* freeblock block number */
1283 int findex
; /* index in freeblock entries */
1284 xfs_dir2_free_t
*free
; /* freeblock structure */
1287 * Convert the data block number to a free block,
1288 * read in the free block.
1290 fdb
= dp
->d_ops
->db_to_fdb(args
->geo
, db
);
1291 error
= xfs_dir2_free_read(tp
, dp
,
1292 xfs_dir2_db_to_da(args
->geo
, fdb
),
1299 struct xfs_dir3_icfree_hdr freehdr
;
1300 dp
->d_ops
->free_hdr_from_disk(&freehdr
, free
);
1301 ASSERT(freehdr
.firstdb
== dp
->d_ops
->free_max_bests(args
->geo
) *
1302 (fdb
- xfs_dir2_byte_to_db(args
->geo
,
1303 XFS_DIR2_FREE_OFFSET
)));
1307 * Calculate which entry we need to fix.
1309 findex
= dp
->d_ops
->db_to_fdindex(args
->geo
, db
);
1310 longest
= be16_to_cpu(bf
[0].length
);
1312 * If the data block is now empty we can get rid of it
1315 if (longest
== args
->geo
->blksize
-
1316 dp
->d_ops
->data_entry_offset
) {
1318 * Try to punch out the data block.
1320 error
= xfs_dir2_shrink_inode(args
, db
, dbp
);
1326 * We can get ENOSPC if there's no space reservation.
1327 * In this case just drop the buffer and some one else
1328 * will eventually get rid of the empty block.
1330 else if (!(error
== -ENOSPC
&& args
->total
== 0))
1334 * If we got rid of the data block, we can eliminate that entry
1335 * in the free block.
1337 error
= xfs_dir3_data_block_free(args
, hdr
, free
,
1338 fdb
, findex
, fbp
, longest
);
1343 xfs_dir3_leaf_check(dp
, bp
);
1345 * Return indication of whether this leaf block is empty enough
1346 * to justify trying to join it with a neighbor.
1348 *rval
= (dp
->d_ops
->leaf_hdr_size
+
1349 (uint
)sizeof(ents
[0]) * (leafhdr
.count
- leafhdr
.stale
)) <
1350 args
->geo
->magicpct
;
1355 * Split the leaf entries in the old block into old and new blocks.
1358 xfs_dir2_leafn_split(
1359 xfs_da_state_t
*state
, /* btree cursor */
1360 xfs_da_state_blk_t
*oldblk
, /* original block */
1361 xfs_da_state_blk_t
*newblk
) /* newly created block */
1363 xfs_da_args_t
*args
; /* operation arguments */
1364 xfs_dablk_t blkno
; /* new leaf block number */
1365 int error
; /* error return value */
1366 struct xfs_inode
*dp
;
1369 * Allocate space for a new leaf node.
1373 ASSERT(oldblk
->magic
== XFS_DIR2_LEAFN_MAGIC
);
1374 error
= xfs_da_grow_inode(args
, &blkno
);
1379 * Initialize the new leaf block.
1381 error
= xfs_dir3_leaf_get_buf(args
, xfs_dir2_da_to_db(args
->geo
, blkno
),
1382 &newblk
->bp
, XFS_DIR2_LEAFN_MAGIC
);
1386 newblk
->blkno
= blkno
;
1387 newblk
->magic
= XFS_DIR2_LEAFN_MAGIC
;
1389 * Rebalance the entries across the two leaves, link the new
1390 * block into the leaves.
1392 xfs_dir2_leafn_rebalance(state
, oldblk
, newblk
);
1393 error
= xfs_da3_blk_link(state
, oldblk
, newblk
);
1398 * Insert the new entry in the correct block.
1401 error
= xfs_dir2_leafn_add(oldblk
->bp
, args
, oldblk
->index
);
1403 error
= xfs_dir2_leafn_add(newblk
->bp
, args
, newblk
->index
);
1405 * Update last hashval in each block since we added the name.
1407 oldblk
->hashval
= xfs_dir2_leaf_lasthash(dp
, oldblk
->bp
, NULL
);
1408 newblk
->hashval
= xfs_dir2_leaf_lasthash(dp
, newblk
->bp
, NULL
);
1409 xfs_dir3_leaf_check(dp
, oldblk
->bp
);
1410 xfs_dir3_leaf_check(dp
, newblk
->bp
);
1415 * Check a leaf block and its neighbors to see if the block should be
1416 * collapsed into one or the other neighbor. Always keep the block
1417 * with the smaller block number.
1418 * If the current block is over 50% full, don't try to join it, return 0.
1419 * If the block is empty, fill in the state structure and return 2.
1420 * If it can be collapsed, fill in the state structure and return 1.
1421 * If nothing can be done, return 0.
1424 xfs_dir2_leafn_toosmall(
1425 xfs_da_state_t
*state
, /* btree cursor */
1426 int *action
) /* resulting action to take */
1428 xfs_da_state_blk_t
*blk
; /* leaf block */
1429 xfs_dablk_t blkno
; /* leaf block number */
1430 struct xfs_buf
*bp
; /* leaf buffer */
1431 int bytes
; /* bytes in use */
1432 int count
; /* leaf live entry count */
1433 int error
; /* error return value */
1434 int forward
; /* sibling block direction */
1435 int i
; /* sibling counter */
1436 xfs_dir2_leaf_t
*leaf
; /* leaf structure */
1437 int rval
; /* result from path_shift */
1438 struct xfs_dir3_icleaf_hdr leafhdr
;
1439 struct xfs_dir2_leaf_entry
*ents
;
1440 struct xfs_inode
*dp
= state
->args
->dp
;
1443 * Check for the degenerate case of the block being over 50% full.
1444 * If so, it's not worth even looking to see if we might be able
1445 * to coalesce with a sibling.
1447 blk
= &state
->path
.blk
[state
->path
.active
- 1];
1448 leaf
= blk
->bp
->b_addr
;
1449 dp
->d_ops
->leaf_hdr_from_disk(&leafhdr
, leaf
);
1450 ents
= dp
->d_ops
->leaf_ents_p(leaf
);
1451 xfs_dir3_leaf_check(dp
, blk
->bp
);
1453 count
= leafhdr
.count
- leafhdr
.stale
;
1454 bytes
= dp
->d_ops
->leaf_hdr_size
+ count
* sizeof(ents
[0]);
1455 if (bytes
> (state
->args
->geo
->blksize
>> 1)) {
1457 * Blk over 50%, don't try to join.
1463 * Check for the degenerate case of the block being empty.
1464 * If the block is empty, we'll simply delete it, no need to
1465 * coalesce it with a sibling block. We choose (arbitrarily)
1466 * to merge with the forward block unless it is NULL.
1470 * Make altpath point to the block we want to keep and
1471 * path point to the block we want to drop (this one).
1473 forward
= (leafhdr
.forw
!= 0);
1474 memcpy(&state
->altpath
, &state
->path
, sizeof(state
->path
));
1475 error
= xfs_da3_path_shift(state
, &state
->altpath
, forward
, 0,
1479 *action
= rval
? 2 : 0;
1483 * Examine each sibling block to see if we can coalesce with
1484 * at least 25% free space to spare. We need to figure out
1485 * whether to merge with the forward or the backward block.
1486 * We prefer coalescing with the lower numbered sibling so as
1487 * to shrink a directory over time.
1489 forward
= leafhdr
.forw
< leafhdr
.back
;
1490 for (i
= 0, bp
= NULL
; i
< 2; forward
= !forward
, i
++) {
1491 struct xfs_dir3_icleaf_hdr hdr2
;
1493 blkno
= forward
? leafhdr
.forw
: leafhdr
.back
;
1497 * Read the sibling leaf block.
1499 error
= xfs_dir3_leafn_read(state
->args
->trans
, dp
,
1505 * Count bytes in the two blocks combined.
1507 count
= leafhdr
.count
- leafhdr
.stale
;
1508 bytes
= state
->args
->geo
->blksize
-
1509 (state
->args
->geo
->blksize
>> 2);
1512 dp
->d_ops
->leaf_hdr_from_disk(&hdr2
, leaf
);
1513 ents
= dp
->d_ops
->leaf_ents_p(leaf
);
1514 count
+= hdr2
.count
- hdr2
.stale
;
1515 bytes
-= count
* sizeof(ents
[0]);
1518 * Fits with at least 25% to spare.
1522 xfs_trans_brelse(state
->args
->trans
, bp
);
1525 * Didn't like either block, give up.
1533 * Make altpath point to the block we want to keep (the lower
1534 * numbered block) and path point to the block we want to drop.
1536 memcpy(&state
->altpath
, &state
->path
, sizeof(state
->path
));
1537 if (blkno
< blk
->blkno
)
1538 error
= xfs_da3_path_shift(state
, &state
->altpath
, forward
, 0,
1541 error
= xfs_da3_path_shift(state
, &state
->path
, forward
, 0,
1546 *action
= rval
? 0 : 1;
1551 * Move all the leaf entries from drop_blk to save_blk.
1552 * This is done as part of a join operation.
1555 xfs_dir2_leafn_unbalance(
1556 xfs_da_state_t
*state
, /* cursor */
1557 xfs_da_state_blk_t
*drop_blk
, /* dead block */
1558 xfs_da_state_blk_t
*save_blk
) /* surviving block */
1560 xfs_da_args_t
*args
; /* operation arguments */
1561 xfs_dir2_leaf_t
*drop_leaf
; /* dead leaf structure */
1562 xfs_dir2_leaf_t
*save_leaf
; /* surviving leaf structure */
1563 struct xfs_dir3_icleaf_hdr savehdr
;
1564 struct xfs_dir3_icleaf_hdr drophdr
;
1565 struct xfs_dir2_leaf_entry
*sents
;
1566 struct xfs_dir2_leaf_entry
*dents
;
1567 struct xfs_inode
*dp
= state
->args
->dp
;
1570 ASSERT(drop_blk
->magic
== XFS_DIR2_LEAFN_MAGIC
);
1571 ASSERT(save_blk
->magic
== XFS_DIR2_LEAFN_MAGIC
);
1572 drop_leaf
= drop_blk
->bp
->b_addr
;
1573 save_leaf
= save_blk
->bp
->b_addr
;
1575 dp
->d_ops
->leaf_hdr_from_disk(&savehdr
, save_leaf
);
1576 dp
->d_ops
->leaf_hdr_from_disk(&drophdr
, drop_leaf
);
1577 sents
= dp
->d_ops
->leaf_ents_p(save_leaf
);
1578 dents
= dp
->d_ops
->leaf_ents_p(drop_leaf
);
1581 * If there are any stale leaf entries, take this opportunity
1585 xfs_dir3_leaf_compact(args
, &drophdr
, drop_blk
->bp
);
1587 xfs_dir3_leaf_compact(args
, &savehdr
, save_blk
->bp
);
1590 * Move the entries from drop to the appropriate end of save.
1592 drop_blk
->hashval
= be32_to_cpu(dents
[drophdr
.count
- 1].hashval
);
1593 if (xfs_dir2_leafn_order(dp
, save_blk
->bp
, drop_blk
->bp
))
1594 xfs_dir3_leafn_moveents(args
, drop_blk
->bp
, &drophdr
, dents
, 0,
1595 save_blk
->bp
, &savehdr
, sents
, 0,
1598 xfs_dir3_leafn_moveents(args
, drop_blk
->bp
, &drophdr
, dents
, 0,
1599 save_blk
->bp
, &savehdr
, sents
,
1600 savehdr
.count
, drophdr
.count
);
1601 save_blk
->hashval
= be32_to_cpu(sents
[savehdr
.count
- 1].hashval
);
1603 /* log the changes made when moving the entries */
1604 dp
->d_ops
->leaf_hdr_to_disk(save_leaf
, &savehdr
);
1605 dp
->d_ops
->leaf_hdr_to_disk(drop_leaf
, &drophdr
);
1606 xfs_dir3_leaf_log_header(args
, save_blk
->bp
);
1607 xfs_dir3_leaf_log_header(args
, drop_blk
->bp
);
1609 xfs_dir3_leaf_check(dp
, save_blk
->bp
);
1610 xfs_dir3_leaf_check(dp
, drop_blk
->bp
);
1614 * Top-level node form directory addname routine.
1617 xfs_dir2_node_addname(
1618 xfs_da_args_t
*args
) /* operation arguments */
1620 xfs_da_state_blk_t
*blk
; /* leaf block for insert */
1621 int error
; /* error return value */
1622 int rval
; /* sub-return value */
1623 xfs_da_state_t
*state
; /* btree cursor */
1625 trace_xfs_dir2_node_addname(args
);
1628 * Allocate and initialize the state (btree cursor).
1630 state
= xfs_da_state_alloc();
1632 state
->mp
= args
->dp
->i_mount
;
1634 * Look up the name. We're not supposed to find it, but
1635 * this gives us the insertion point.
1637 error
= xfs_da3_node_lookup_int(state
, &rval
);
1640 if (rval
!= -ENOENT
) {
1644 * Add the data entry to a data block.
1645 * Extravalid is set to a freeblock found by lookup.
1647 rval
= xfs_dir2_node_addname_int(args
,
1648 state
->extravalid
? &state
->extrablk
: NULL
);
1652 blk
= &state
->path
.blk
[state
->path
.active
- 1];
1653 ASSERT(blk
->magic
== XFS_DIR2_LEAFN_MAGIC
);
1655 * Add the new leaf entry.
1657 rval
= xfs_dir2_leafn_add(blk
->bp
, args
, blk
->index
);
1660 * It worked, fix the hash values up the btree.
1662 if (!(args
->op_flags
& XFS_DA_OP_JUSTCHECK
))
1663 xfs_da3_fixhashpath(state
, &state
->path
);
1666 * It didn't work, we need to split the leaf block.
1668 if (args
->total
== 0) {
1669 ASSERT(rval
== -ENOSPC
);
1673 * Split the leaf block and insert the new entry.
1675 rval
= xfs_da3_split(state
);
1678 xfs_da_state_free(state
);
1683 * Add the data entry for a node-format directory name addition.
1684 * The leaf entry is added in xfs_dir2_leafn_add.
1685 * We may enter with a freespace block that the lookup found.
1687 static int /* error */
1688 xfs_dir2_node_addname_int(
1689 xfs_da_args_t
*args
, /* operation arguments */
1690 xfs_da_state_blk_t
*fblk
) /* optional freespace block */
1692 xfs_dir2_data_hdr_t
*hdr
; /* data block header */
1693 xfs_dir2_db_t dbno
; /* data block number */
1694 struct xfs_buf
*dbp
; /* data block buffer */
1695 xfs_dir2_data_entry_t
*dep
; /* data entry pointer */
1696 xfs_inode_t
*dp
; /* incore directory inode */
1697 xfs_dir2_data_unused_t
*dup
; /* data unused entry pointer */
1698 int error
; /* error return value */
1699 xfs_dir2_db_t fbno
; /* freespace block number */
1700 struct xfs_buf
*fbp
; /* freespace buffer */
1701 int findex
; /* freespace entry index */
1702 xfs_dir2_free_t
*free
=NULL
; /* freespace block structure */
1703 xfs_dir2_db_t ifbno
; /* initial freespace block no */
1704 xfs_dir2_db_t lastfbno
=0; /* highest freespace block no */
1705 int length
; /* length of the new entry */
1706 int logfree
; /* need to log free entry */
1707 xfs_mount_t
*mp
; /* filesystem mount point */
1708 int needlog
; /* need to log data header */
1709 int needscan
; /* need to rescan data frees */
1710 __be16
*tagp
; /* data entry tag pointer */
1711 xfs_trans_t
*tp
; /* transaction pointer */
1713 struct xfs_dir3_icfree_hdr freehdr
;
1714 struct xfs_dir2_data_free
*bf
;
1715 xfs_dir2_data_aoff_t aoff
;
1720 length
= dp
->d_ops
->data_entsize(args
->namelen
);
1722 * If we came in with a freespace block that means that lookup
1723 * found an entry with our hash value. This is the freespace
1724 * block for that data entry.
1729 * Remember initial freespace block number.
1731 ifbno
= fblk
->blkno
;
1733 findex
= fblk
->index
;
1734 bests
= dp
->d_ops
->free_bests_p(free
);
1735 dp
->d_ops
->free_hdr_from_disk(&freehdr
, free
);
1738 * This means the free entry showed that the data block had
1739 * space for our entry, so we remembered it.
1740 * Use that data block.
1743 ASSERT(findex
< freehdr
.nvalid
);
1744 ASSERT(be16_to_cpu(bests
[findex
]) != NULLDATAOFF
);
1745 ASSERT(be16_to_cpu(bests
[findex
]) >= length
);
1746 dbno
= freehdr
.firstdb
+ findex
;
1749 * The data block looked at didn't have enough room.
1750 * We'll start at the beginning of the freespace entries.
1757 * Didn't come in with a freespace block, so no data block.
1765 * If we don't have a data block yet, we're going to scan the
1766 * freespace blocks looking for one. Figure out what the
1767 * highest freespace block number is.
1770 xfs_fileoff_t fo
; /* freespace block number */
1772 if ((error
= xfs_bmap_last_offset(dp
, &fo
, XFS_DATA_FORK
)))
1774 lastfbno
= xfs_dir2_da_to_db(args
->geo
, (xfs_dablk_t
)fo
);
1778 * While we haven't identified a data block, search the freeblock
1779 * data for a good data block. If we find a null freeblock entry,
1780 * indicating a hole in the data blocks, remember that.
1782 while (dbno
== -1) {
1784 * If we don't have a freeblock in hand, get the next one.
1788 * Happens the first time through unless lookup gave
1789 * us a freespace block to start with.
1792 fbno
= xfs_dir2_byte_to_db(args
->geo
,
1793 XFS_DIR2_FREE_OFFSET
);
1795 * If it's ifbno we already looked at it.
1800 * If it's off the end we're done.
1802 if (fbno
>= lastfbno
)
1805 * Read the block. There can be holes in the
1806 * freespace blocks, so this might not succeed.
1807 * This should be really rare, so there's no reason
1810 error
= xfs_dir2_free_try_read(tp
, dp
,
1811 xfs_dir2_db_to_da(args
->geo
, fbno
),
1821 * Look at the current free entry. Is it good enough?
1823 * The bests initialisation should be where the bufer is read in
1824 * the above branch. But gcc is too stupid to realise that bests
1825 * and the freehdr are actually initialised if they are placed
1826 * there, so we have to do it here to avoid warnings. Blech.
1828 bests
= dp
->d_ops
->free_bests_p(free
);
1829 dp
->d_ops
->free_hdr_from_disk(&freehdr
, free
);
1830 if (be16_to_cpu(bests
[findex
]) != NULLDATAOFF
&&
1831 be16_to_cpu(bests
[findex
]) >= length
)
1832 dbno
= freehdr
.firstdb
+ findex
;
1835 * Are we done with the freeblock?
1837 if (++findex
== freehdr
.nvalid
) {
1841 xfs_trans_brelse(tp
, fbp
);
1843 if (fblk
&& fblk
->bp
)
1849 * If we don't have a data block, we need to allocate one and make
1850 * the freespace entries refer to it.
1852 if (unlikely(dbno
== -1)) {
1854 * Not allowed to allocate, return failure.
1856 if ((args
->op_flags
& XFS_DA_OP_JUSTCHECK
) || args
->total
== 0)
1860 * Allocate and initialize the new data block.
1862 if (unlikely((error
= xfs_dir2_grow_inode(args
,
1863 XFS_DIR2_DATA_SPACE
,
1865 (error
= xfs_dir3_data_init(args
, dbno
, &dbp
))))
1869 * If (somehow) we have a freespace block, get rid of it.
1872 xfs_trans_brelse(tp
, fbp
);
1873 if (fblk
&& fblk
->bp
)
1877 * Get the freespace block corresponding to the data block
1878 * that was just allocated.
1880 fbno
= dp
->d_ops
->db_to_fdb(args
->geo
, dbno
);
1881 error
= xfs_dir2_free_try_read(tp
, dp
,
1882 xfs_dir2_db_to_da(args
->geo
, fbno
),
1888 * If there wasn't a freespace block, the read will
1889 * return a NULL fbp. Allocate and initialize a new one.
1892 error
= xfs_dir2_grow_inode(args
, XFS_DIR2_FREE_SPACE
,
1897 if (dp
->d_ops
->db_to_fdb(args
->geo
, dbno
) != fbno
) {
1899 "%s: dir ino %llu needed freesp block %lld for data block %lld, got %lld ifbno %llu lastfbno %d",
1900 __func__
, (unsigned long long)dp
->i_ino
,
1901 (long long)dp
->d_ops
->db_to_fdb(
1903 (long long)dbno
, (long long)fbno
,
1904 (unsigned long long)ifbno
, lastfbno
);
1907 " fblk "PTR_FMT
" blkno %llu index %d magic 0x%x",
1909 (unsigned long long)fblk
->blkno
,
1913 xfs_alert(mp
, " ... fblk is NULL");
1915 XFS_ERROR_REPORT("xfs_dir2_node_addname_int",
1916 XFS_ERRLEVEL_LOW
, mp
);
1917 return -EFSCORRUPTED
;
1921 * Get a buffer for the new block.
1923 error
= xfs_dir3_free_get_buf(args
, fbno
, &fbp
);
1927 bests
= dp
->d_ops
->free_bests_p(free
);
1928 dp
->d_ops
->free_hdr_from_disk(&freehdr
, free
);
1931 * Remember the first slot as our empty slot.
1934 (fbno
- xfs_dir2_byte_to_db(args
->geo
,
1935 XFS_DIR2_FREE_OFFSET
)) *
1936 dp
->d_ops
->free_max_bests(args
->geo
);
1939 bests
= dp
->d_ops
->free_bests_p(free
);
1940 dp
->d_ops
->free_hdr_from_disk(&freehdr
, free
);
1944 * Set the freespace block index from the data block number.
1946 findex
= dp
->d_ops
->db_to_fdindex(args
->geo
, dbno
);
1948 * If it's after the end of the current entries in the
1949 * freespace block, extend that table.
1951 if (findex
>= freehdr
.nvalid
) {
1952 ASSERT(findex
< dp
->d_ops
->free_max_bests(args
->geo
));
1953 freehdr
.nvalid
= findex
+ 1;
1955 * Tag new entry so nused will go up.
1957 bests
[findex
] = cpu_to_be16(NULLDATAOFF
);
1960 * If this entry was for an empty data block
1961 * (this should always be true) then update the header.
1963 if (bests
[findex
] == cpu_to_be16(NULLDATAOFF
)) {
1965 dp
->d_ops
->free_hdr_to_disk(fbp
->b_addr
, &freehdr
);
1966 xfs_dir2_free_log_header(args
, fbp
);
1969 * Update the real value in the table.
1970 * We haven't allocated the data entry yet so this will
1974 bf
= dp
->d_ops
->data_bestfree_p(hdr
);
1975 bests
[findex
] = bf
[0].length
;
1979 * We had a data block so we don't have to make a new one.
1983 * If just checking, we succeeded.
1985 if (args
->op_flags
& XFS_DA_OP_JUSTCHECK
)
1989 * Read the data block in.
1991 error
= xfs_dir3_data_read(tp
, dp
,
1992 xfs_dir2_db_to_da(args
->geo
, dbno
),
1997 bf
= dp
->d_ops
->data_bestfree_p(hdr
);
2000 ASSERT(be16_to_cpu(bf
[0].length
) >= length
);
2002 * Point to the existing unused space.
2004 dup
= (xfs_dir2_data_unused_t
*)
2005 ((char *)hdr
+ be16_to_cpu(bf
[0].offset
));
2006 needscan
= needlog
= 0;
2008 * Mark the first part of the unused space, inuse for us.
2010 aoff
= (xfs_dir2_data_aoff_t
)((char *)dup
- (char *)hdr
);
2011 error
= xfs_dir2_data_use_free(args
, dbp
, dup
, aoff
, length
,
2012 &needlog
, &needscan
);
2014 xfs_trans_brelse(tp
, dbp
);
2018 * Fill in the new entry and log it.
2020 dep
= (xfs_dir2_data_entry_t
*)dup
;
2021 dep
->inumber
= cpu_to_be64(args
->inumber
);
2022 dep
->namelen
= args
->namelen
;
2023 memcpy(dep
->name
, args
->name
, dep
->namelen
);
2024 dp
->d_ops
->data_put_ftype(dep
, args
->filetype
);
2025 tagp
= dp
->d_ops
->data_entry_tag_p(dep
);
2026 *tagp
= cpu_to_be16((char *)dep
- (char *)hdr
);
2027 xfs_dir2_data_log_entry(args
, dbp
, dep
);
2029 * Rescan the block for bestfree if needed.
2032 xfs_dir2_data_freescan(dp
, hdr
, &needlog
);
2034 * Log the data block header if needed.
2037 xfs_dir2_data_log_header(args
, dbp
);
2039 * If the freespace entry is now wrong, update it.
2041 bests
= dp
->d_ops
->free_bests_p(free
); /* gcc is so stupid */
2042 if (be16_to_cpu(bests
[findex
]) != be16_to_cpu(bf
[0].length
)) {
2043 bests
[findex
] = bf
[0].length
;
2047 * Log the freespace entry if needed.
2050 xfs_dir2_free_log_bests(args
, fbp
, findex
, findex
);
2052 * Return the data block and offset in args, then drop the data block.
2054 args
->blkno
= (xfs_dablk_t
)dbno
;
2055 args
->index
= be16_to_cpu(*tagp
);
2060 * Lookup an entry in a node-format directory.
2061 * All the real work happens in xfs_da3_node_lookup_int.
2062 * The only real output is the inode number of the entry.
2065 xfs_dir2_node_lookup(
2066 xfs_da_args_t
*args
) /* operation arguments */
2068 int error
; /* error return value */
2069 int i
; /* btree level */
2070 int rval
; /* operation return value */
2071 xfs_da_state_t
*state
; /* btree cursor */
2073 trace_xfs_dir2_node_lookup(args
);
2076 * Allocate and initialize the btree cursor.
2078 state
= xfs_da_state_alloc();
2080 state
->mp
= args
->dp
->i_mount
;
2082 * Fill in the path to the entry in the cursor.
2084 error
= xfs_da3_node_lookup_int(state
, &rval
);
2087 else if (rval
== -ENOENT
&& args
->cmpresult
== XFS_CMP_CASE
) {
2088 /* If a CI match, dup the actual name and return -EEXIST */
2089 xfs_dir2_data_entry_t
*dep
;
2091 dep
= (xfs_dir2_data_entry_t
*)
2092 ((char *)state
->extrablk
.bp
->b_addr
+
2093 state
->extrablk
.index
);
2094 rval
= xfs_dir_cilookup_result(args
, dep
->name
, dep
->namelen
);
2097 * Release the btree blocks and leaf block.
2099 for (i
= 0; i
< state
->path
.active
; i
++) {
2100 xfs_trans_brelse(args
->trans
, state
->path
.blk
[i
].bp
);
2101 state
->path
.blk
[i
].bp
= NULL
;
2104 * Release the data block if we have it.
2106 if (state
->extravalid
&& state
->extrablk
.bp
) {
2107 xfs_trans_brelse(args
->trans
, state
->extrablk
.bp
);
2108 state
->extrablk
.bp
= NULL
;
2110 xfs_da_state_free(state
);
2115 * Remove an entry from a node-format directory.
2118 xfs_dir2_node_removename(
2119 struct xfs_da_args
*args
) /* operation arguments */
2121 struct xfs_da_state_blk
*blk
; /* leaf block */
2122 int error
; /* error return value */
2123 int rval
; /* operation return value */
2124 struct xfs_da_state
*state
; /* btree cursor */
2126 trace_xfs_dir2_node_removename(args
);
2129 * Allocate and initialize the btree cursor.
2131 state
= xfs_da_state_alloc();
2133 state
->mp
= args
->dp
->i_mount
;
2135 /* Look up the entry we're deleting, set up the cursor. */
2136 error
= xfs_da3_node_lookup_int(state
, &rval
);
2140 /* Didn't find it, upper layer screwed up. */
2141 if (rval
!= -EEXIST
) {
2146 blk
= &state
->path
.blk
[state
->path
.active
- 1];
2147 ASSERT(blk
->magic
== XFS_DIR2_LEAFN_MAGIC
);
2148 ASSERT(state
->extravalid
);
2150 * Remove the leaf and data entries.
2151 * Extrablk refers to the data block.
2153 error
= xfs_dir2_leafn_remove(args
, blk
->bp
, blk
->index
,
2154 &state
->extrablk
, &rval
);
2158 * Fix the hash values up the btree.
2160 xfs_da3_fixhashpath(state
, &state
->path
);
2162 * If we need to join leaf blocks, do it.
2164 if (rval
&& state
->path
.active
> 1)
2165 error
= xfs_da3_join(state
);
2167 * If no errors so far, try conversion to leaf format.
2170 error
= xfs_dir2_node_to_leaf(state
);
2172 xfs_da_state_free(state
);
2177 * Replace an entry's inode number in a node-format directory.
2180 xfs_dir2_node_replace(
2181 xfs_da_args_t
*args
) /* operation arguments */
2183 xfs_da_state_blk_t
*blk
; /* leaf block */
2184 xfs_dir2_data_hdr_t
*hdr
; /* data block header */
2185 xfs_dir2_data_entry_t
*dep
; /* data entry changed */
2186 int error
; /* error return value */
2187 int i
; /* btree level */
2188 xfs_ino_t inum
; /* new inode number */
2189 int ftype
; /* new file type */
2190 xfs_dir2_leaf_t
*leaf
; /* leaf structure */
2191 xfs_dir2_leaf_entry_t
*lep
; /* leaf entry being changed */
2192 int rval
; /* internal return value */
2193 xfs_da_state_t
*state
; /* btree cursor */
2195 trace_xfs_dir2_node_replace(args
);
2198 * Allocate and initialize the btree cursor.
2200 state
= xfs_da_state_alloc();
2202 state
->mp
= args
->dp
->i_mount
;
2205 * We have to save new inode number and ftype since
2206 * xfs_da3_node_lookup_int() is going to overwrite them
2208 inum
= args
->inumber
;
2209 ftype
= args
->filetype
;
2212 * Lookup the entry to change in the btree.
2214 error
= xfs_da3_node_lookup_int(state
, &rval
);
2219 * It should be found, since the vnodeops layer has looked it up
2220 * and locked it. But paranoia is good.
2222 if (rval
== -EEXIST
) {
2223 struct xfs_dir2_leaf_entry
*ents
;
2225 * Find the leaf entry.
2227 blk
= &state
->path
.blk
[state
->path
.active
- 1];
2228 ASSERT(blk
->magic
== XFS_DIR2_LEAFN_MAGIC
);
2229 leaf
= blk
->bp
->b_addr
;
2230 ents
= args
->dp
->d_ops
->leaf_ents_p(leaf
);
2231 lep
= &ents
[blk
->index
];
2232 ASSERT(state
->extravalid
);
2234 * Point to the data entry.
2236 hdr
= state
->extrablk
.bp
->b_addr
;
2237 ASSERT(hdr
->magic
== cpu_to_be32(XFS_DIR2_DATA_MAGIC
) ||
2238 hdr
->magic
== cpu_to_be32(XFS_DIR3_DATA_MAGIC
));
2239 dep
= (xfs_dir2_data_entry_t
*)
2241 xfs_dir2_dataptr_to_off(args
->geo
,
2242 be32_to_cpu(lep
->address
)));
2243 ASSERT(inum
!= be64_to_cpu(dep
->inumber
));
2245 * Fill in the new inode number and log the entry.
2247 dep
->inumber
= cpu_to_be64(inum
);
2248 args
->dp
->d_ops
->data_put_ftype(dep
, ftype
);
2249 xfs_dir2_data_log_entry(args
, state
->extrablk
.bp
, dep
);
2253 * Didn't find it, and we're holding a data block. Drop it.
2255 else if (state
->extravalid
) {
2256 xfs_trans_brelse(args
->trans
, state
->extrablk
.bp
);
2257 state
->extrablk
.bp
= NULL
;
2260 * Release all the buffers in the cursor.
2262 for (i
= 0; i
< state
->path
.active
; i
++) {
2263 xfs_trans_brelse(args
->trans
, state
->path
.blk
[i
].bp
);
2264 state
->path
.blk
[i
].bp
= NULL
;
2266 xfs_da_state_free(state
);
2271 * Trim off a trailing empty freespace block.
2272 * Return (in rvalp) 1 if we did it, 0 if not.
2275 xfs_dir2_node_trim_free(
2276 xfs_da_args_t
*args
, /* operation arguments */
2277 xfs_fileoff_t fo
, /* free block number */
2278 int *rvalp
) /* out: did something */
2280 struct xfs_buf
*bp
; /* freespace buffer */
2281 xfs_inode_t
*dp
; /* incore directory inode */
2282 int error
; /* error return code */
2283 xfs_dir2_free_t
*free
; /* freespace structure */
2284 xfs_trans_t
*tp
; /* transaction pointer */
2285 struct xfs_dir3_icfree_hdr freehdr
;
2293 * Read the freespace block.
2295 error
= xfs_dir2_free_try_read(tp
, dp
, fo
, &bp
);
2299 * There can be holes in freespace. If fo is a hole, there's
2305 dp
->d_ops
->free_hdr_from_disk(&freehdr
, free
);
2308 * If there are used entries, there's nothing to do.
2310 if (freehdr
.nused
> 0) {
2311 xfs_trans_brelse(tp
, bp
);
2315 * Blow the block away.
2317 error
= xfs_dir2_shrink_inode(args
,
2318 xfs_dir2_da_to_db(args
->geo
, (xfs_dablk_t
)fo
), bp
);
2321 * Can't fail with ENOSPC since that only happens with no
2322 * space reservation, when breaking up an extent into two
2323 * pieces. This is the last block of an extent.
2325 ASSERT(error
!= -ENOSPC
);
2326 xfs_trans_brelse(tp
, bp
);
2330 * Return that we succeeded.