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
,
34 * Convert data space db to the corresponding free db.
37 xfs_dir2_db_to_fdb(struct xfs_da_geometry
*geo
, xfs_dir2_db_t db
)
39 return xfs_dir2_byte_to_db(geo
, XFS_DIR2_FREE_OFFSET
) +
40 (db
/ geo
->free_max_bests
);
44 * Convert data space db to the corresponding index in a free db.
47 xfs_dir2_db_to_fdindex(struct xfs_da_geometry
*geo
, xfs_dir2_db_t db
)
49 return db
% geo
->free_max_bests
;
53 * Check internal consistency of a leafn block.
61 struct xfs_dir2_leaf
*leaf
= bp
->b_addr
;
62 struct xfs_dir3_icleaf_hdr leafhdr
;
64 xfs_dir2_leaf_hdr_from_disk(dp
->i_mount
, &leafhdr
, leaf
);
66 if (leafhdr
.magic
== XFS_DIR3_LEAFN_MAGIC
) {
67 struct xfs_dir3_leaf_hdr
*leaf3
= bp
->b_addr
;
68 if (be64_to_cpu(leaf3
->info
.blkno
) != bp
->b_bn
)
69 return __this_address
;
70 } else if (leafhdr
.magic
!= XFS_DIR2_LEAFN_MAGIC
)
71 return __this_address
;
73 return xfs_dir3_leaf_check_int(dp
->i_mount
, &leafhdr
, leaf
);
83 fa
= xfs_dir3_leafn_check(dp
, bp
);
86 xfs_corruption_error(__func__
, XFS_ERRLEVEL_LOW
, dp
->i_mount
,
87 bp
->b_addr
, BBTOB(bp
->b_length
), __FILE__
, __LINE__
,
92 #define xfs_dir3_leaf_check(dp, bp)
99 struct xfs_mount
*mp
= bp
->b_mount
;
100 struct xfs_dir2_free_hdr
*hdr
= bp
->b_addr
;
102 if (!xfs_verify_magic(bp
, hdr
->magic
))
103 return __this_address
;
105 if (xfs_sb_version_hascrc(&mp
->m_sb
)) {
106 struct xfs_dir3_blk_hdr
*hdr3
= bp
->b_addr
;
108 if (!uuid_equal(&hdr3
->uuid
, &mp
->m_sb
.sb_meta_uuid
))
109 return __this_address
;
110 if (be64_to_cpu(hdr3
->blkno
) != bp
->b_bn
)
111 return __this_address
;
112 if (!xfs_log_check_lsn(mp
, be64_to_cpu(hdr3
->lsn
)))
113 return __this_address
;
116 /* XXX: should bounds check the xfs_dir3_icfree_hdr here */
122 xfs_dir3_free_read_verify(
125 struct xfs_mount
*mp
= bp
->b_mount
;
128 if (xfs_sb_version_hascrc(&mp
->m_sb
) &&
129 !xfs_buf_verify_cksum(bp
, XFS_DIR3_FREE_CRC_OFF
))
130 xfs_verifier_error(bp
, -EFSBADCRC
, __this_address
);
132 fa
= xfs_dir3_free_verify(bp
);
134 xfs_verifier_error(bp
, -EFSCORRUPTED
, fa
);
139 xfs_dir3_free_write_verify(
142 struct xfs_mount
*mp
= bp
->b_mount
;
143 struct xfs_buf_log_item
*bip
= bp
->b_log_item
;
144 struct xfs_dir3_blk_hdr
*hdr3
= bp
->b_addr
;
147 fa
= xfs_dir3_free_verify(bp
);
149 xfs_verifier_error(bp
, -EFSCORRUPTED
, fa
);
153 if (!xfs_sb_version_hascrc(&mp
->m_sb
))
157 hdr3
->lsn
= cpu_to_be64(bip
->bli_item
.li_lsn
);
159 xfs_buf_update_cksum(bp
, XFS_DIR3_FREE_CRC_OFF
);
162 const struct xfs_buf_ops xfs_dir3_free_buf_ops
= {
163 .name
= "xfs_dir3_free",
164 .magic
= { cpu_to_be32(XFS_DIR2_FREE_MAGIC
),
165 cpu_to_be32(XFS_DIR3_FREE_MAGIC
) },
166 .verify_read
= xfs_dir3_free_read_verify
,
167 .verify_write
= xfs_dir3_free_write_verify
,
168 .verify_struct
= xfs_dir3_free_verify
,
171 /* Everything ok in the free block header? */
172 static xfs_failaddr_t
173 xfs_dir3_free_header_check(
174 struct xfs_inode
*dp
,
178 struct xfs_mount
*mp
= dp
->i_mount
;
179 int maxbests
= mp
->m_dir_geo
->free_max_bests
;
180 unsigned int firstdb
;
182 firstdb
= (xfs_dir2_da_to_db(mp
->m_dir_geo
, fbno
) -
183 xfs_dir2_byte_to_db(mp
->m_dir_geo
, XFS_DIR2_FREE_OFFSET
)) *
185 if (xfs_sb_version_hascrc(&mp
->m_sb
)) {
186 struct xfs_dir3_free_hdr
*hdr3
= bp
->b_addr
;
188 if (be32_to_cpu(hdr3
->firstdb
) != firstdb
)
189 return __this_address
;
190 if (be32_to_cpu(hdr3
->nvalid
) > maxbests
)
191 return __this_address
;
192 if (be32_to_cpu(hdr3
->nvalid
) < be32_to_cpu(hdr3
->nused
))
193 return __this_address
;
195 struct xfs_dir2_free_hdr
*hdr
= bp
->b_addr
;
197 if (be32_to_cpu(hdr
->firstdb
) != firstdb
)
198 return __this_address
;
199 if (be32_to_cpu(hdr
->nvalid
) > maxbests
)
200 return __this_address
;
201 if (be32_to_cpu(hdr
->nvalid
) < be32_to_cpu(hdr
->nused
))
202 return __this_address
;
208 __xfs_dir3_free_read(
209 struct xfs_trans
*tp
,
210 struct xfs_inode
*dp
,
213 struct xfs_buf
**bpp
)
218 err
= xfs_da_read_buf(tp
, dp
, fbno
, flags
, bpp
, XFS_DATA_FORK
,
219 &xfs_dir3_free_buf_ops
);
223 /* Check things that we can't do in the verifier. */
224 fa
= xfs_dir3_free_header_check(dp
, fbno
, *bpp
);
226 xfs_verifier_error(*bpp
, -EFSCORRUPTED
, fa
);
227 xfs_trans_brelse(tp
, *bpp
);
228 return -EFSCORRUPTED
;
231 /* try read returns without an error or *bpp if it lands in a hole */
233 xfs_trans_buf_set_type(tp
, *bpp
, XFS_BLFT_DIR_FREE_BUF
);
239 xfs_dir2_free_hdr_from_disk(
240 struct xfs_mount
*mp
,
241 struct xfs_dir3_icfree_hdr
*to
,
242 struct xfs_dir2_free
*from
)
244 if (xfs_sb_version_hascrc(&mp
->m_sb
)) {
245 struct xfs_dir3_free
*from3
= (struct xfs_dir3_free
*)from
;
247 to
->magic
= be32_to_cpu(from3
->hdr
.hdr
.magic
);
248 to
->firstdb
= be32_to_cpu(from3
->hdr
.firstdb
);
249 to
->nvalid
= be32_to_cpu(from3
->hdr
.nvalid
);
250 to
->nused
= be32_to_cpu(from3
->hdr
.nused
);
251 to
->bests
= from3
->bests
;
253 ASSERT(to
->magic
== XFS_DIR3_FREE_MAGIC
);
255 to
->magic
= be32_to_cpu(from
->hdr
.magic
);
256 to
->firstdb
= be32_to_cpu(from
->hdr
.firstdb
);
257 to
->nvalid
= be32_to_cpu(from
->hdr
.nvalid
);
258 to
->nused
= be32_to_cpu(from
->hdr
.nused
);
259 to
->bests
= from
->bests
;
261 ASSERT(to
->magic
== XFS_DIR2_FREE_MAGIC
);
266 xfs_dir2_free_hdr_to_disk(
267 struct xfs_mount
*mp
,
268 struct xfs_dir2_free
*to
,
269 struct xfs_dir3_icfree_hdr
*from
)
271 if (xfs_sb_version_hascrc(&mp
->m_sb
)) {
272 struct xfs_dir3_free
*to3
= (struct xfs_dir3_free
*)to
;
274 ASSERT(from
->magic
== XFS_DIR3_FREE_MAGIC
);
276 to3
->hdr
.hdr
.magic
= cpu_to_be32(from
->magic
);
277 to3
->hdr
.firstdb
= cpu_to_be32(from
->firstdb
);
278 to3
->hdr
.nvalid
= cpu_to_be32(from
->nvalid
);
279 to3
->hdr
.nused
= cpu_to_be32(from
->nused
);
281 ASSERT(from
->magic
== XFS_DIR2_FREE_MAGIC
);
283 to
->hdr
.magic
= cpu_to_be32(from
->magic
);
284 to
->hdr
.firstdb
= cpu_to_be32(from
->firstdb
);
285 to
->hdr
.nvalid
= cpu_to_be32(from
->nvalid
);
286 to
->hdr
.nused
= cpu_to_be32(from
->nused
);
292 struct xfs_trans
*tp
,
293 struct xfs_inode
*dp
,
295 struct xfs_buf
**bpp
)
297 return __xfs_dir3_free_read(tp
, dp
, fbno
, 0, bpp
);
301 xfs_dir2_free_try_read(
302 struct xfs_trans
*tp
,
303 struct xfs_inode
*dp
,
305 struct xfs_buf
**bpp
)
307 return __xfs_dir3_free_read(tp
, dp
, fbno
, XFS_DABUF_MAP_HOLE_OK
, bpp
);
311 xfs_dir3_free_get_buf(
314 struct xfs_buf
**bpp
)
316 struct xfs_trans
*tp
= args
->trans
;
317 struct xfs_inode
*dp
= args
->dp
;
318 struct xfs_mount
*mp
= dp
->i_mount
;
321 struct xfs_dir3_icfree_hdr hdr
;
323 error
= xfs_da_get_buf(tp
, dp
, xfs_dir2_db_to_da(args
->geo
, fbno
),
328 xfs_trans_buf_set_type(tp
, bp
, XFS_BLFT_DIR_FREE_BUF
);
329 bp
->b_ops
= &xfs_dir3_free_buf_ops
;
332 * Initialize the new block to be empty, and remember
333 * its first slot as our empty slot.
335 memset(bp
->b_addr
, 0, sizeof(struct xfs_dir3_free_hdr
));
336 memset(&hdr
, 0, sizeof(hdr
));
338 if (xfs_sb_version_hascrc(&mp
->m_sb
)) {
339 struct xfs_dir3_free_hdr
*hdr3
= bp
->b_addr
;
341 hdr
.magic
= XFS_DIR3_FREE_MAGIC
;
343 hdr3
->hdr
.blkno
= cpu_to_be64(bp
->b_bn
);
344 hdr3
->hdr
.owner
= cpu_to_be64(dp
->i_ino
);
345 uuid_copy(&hdr3
->hdr
.uuid
, &mp
->m_sb
.sb_meta_uuid
);
347 hdr
.magic
= XFS_DIR2_FREE_MAGIC
;
348 xfs_dir2_free_hdr_to_disk(mp
, bp
->b_addr
, &hdr
);
354 * Log entries from a freespace block.
357 xfs_dir2_free_log_bests(
358 struct xfs_da_args
*args
,
359 struct xfs_dir3_icfree_hdr
*hdr
,
361 int first
, /* first entry to log */
362 int last
) /* last entry to log */
364 struct xfs_dir2_free
*free
= bp
->b_addr
;
366 ASSERT(free
->hdr
.magic
== cpu_to_be32(XFS_DIR2_FREE_MAGIC
) ||
367 free
->hdr
.magic
== cpu_to_be32(XFS_DIR3_FREE_MAGIC
));
368 xfs_trans_log_buf(args
->trans
, bp
,
369 (char *)&hdr
->bests
[first
] - (char *)free
,
370 (char *)&hdr
->bests
[last
] - (char *)free
+
371 sizeof(hdr
->bests
[0]) - 1);
375 * Log header from a freespace block.
378 xfs_dir2_free_log_header(
379 struct xfs_da_args
*args
,
383 xfs_dir2_free_t
*free
; /* freespace structure */
386 ASSERT(free
->hdr
.magic
== cpu_to_be32(XFS_DIR2_FREE_MAGIC
) ||
387 free
->hdr
.magic
== cpu_to_be32(XFS_DIR3_FREE_MAGIC
));
389 xfs_trans_log_buf(args
->trans
, bp
, 0,
390 args
->geo
->free_hdr_size
- 1);
394 * Convert a leaf-format directory to a node-format directory.
395 * We need to change the magic number of the leaf block, and copy
396 * the freespace table out of the leaf block into its own block.
399 xfs_dir2_leaf_to_node(
400 xfs_da_args_t
*args
, /* operation arguments */
401 struct xfs_buf
*lbp
) /* leaf buffer */
403 xfs_inode_t
*dp
; /* incore directory inode */
404 int error
; /* error return value */
405 struct xfs_buf
*fbp
; /* freespace buffer */
406 xfs_dir2_db_t fdb
; /* freespace block number */
407 __be16
*from
; /* pointer to freespace entry */
408 int i
; /* leaf freespace index */
409 xfs_dir2_leaf_t
*leaf
; /* leaf structure */
410 xfs_dir2_leaf_tail_t
*ltp
; /* leaf tail structure */
411 int n
; /* count of live freespc ents */
412 xfs_dir2_data_off_t off
; /* freespace entry value */
413 xfs_trans_t
*tp
; /* transaction pointer */
414 struct xfs_dir3_icfree_hdr freehdr
;
416 trace_xfs_dir2_leaf_to_node(args
);
421 * Add a freespace block to the directory.
423 if ((error
= xfs_dir2_grow_inode(args
, XFS_DIR2_FREE_SPACE
, &fdb
))) {
426 ASSERT(fdb
== xfs_dir2_byte_to_db(args
->geo
, XFS_DIR2_FREE_OFFSET
));
428 * Get the buffer for the new freespace block.
430 error
= xfs_dir3_free_get_buf(args
, fdb
, &fbp
);
434 xfs_dir2_free_hdr_from_disk(dp
->i_mount
, &freehdr
, fbp
->b_addr
);
436 ltp
= xfs_dir2_leaf_tail_p(args
->geo
, leaf
);
437 if (be32_to_cpu(ltp
->bestcount
) >
438 (uint
)dp
->i_d
.di_size
/ args
->geo
->blksize
) {
439 xfs_buf_corruption_error(lbp
);
440 return -EFSCORRUPTED
;
444 * Copy freespace entries from the leaf block to the new block.
445 * Count active entries.
447 from
= xfs_dir2_leaf_bests_p(ltp
);
448 for (i
= n
= 0; i
< be32_to_cpu(ltp
->bestcount
); i
++, from
++) {
449 off
= be16_to_cpu(*from
);
450 if (off
!= NULLDATAOFF
)
452 freehdr
.bests
[i
] = cpu_to_be16(off
);
456 * Now initialize the freespace block header.
459 freehdr
.nvalid
= be32_to_cpu(ltp
->bestcount
);
461 xfs_dir2_free_hdr_to_disk(dp
->i_mount
, fbp
->b_addr
, &freehdr
);
462 xfs_dir2_free_log_bests(args
, &freehdr
, fbp
, 0, freehdr
.nvalid
- 1);
463 xfs_dir2_free_log_header(args
, fbp
);
466 * Converting the leaf to a leafnode is just a matter of changing the
467 * magic number and the ops. Do the change directly to the buffer as
468 * it's less work (and less code) than decoding the header to host
469 * format and back again.
471 if (leaf
->hdr
.info
.magic
== cpu_to_be16(XFS_DIR2_LEAF1_MAGIC
))
472 leaf
->hdr
.info
.magic
= cpu_to_be16(XFS_DIR2_LEAFN_MAGIC
);
474 leaf
->hdr
.info
.magic
= cpu_to_be16(XFS_DIR3_LEAFN_MAGIC
);
475 lbp
->b_ops
= &xfs_dir3_leafn_buf_ops
;
476 xfs_trans_buf_set_type(tp
, lbp
, XFS_BLFT_DIR_LEAFN_BUF
);
477 xfs_dir3_leaf_log_header(args
, lbp
);
478 xfs_dir3_leaf_check(dp
, lbp
);
483 * Add a leaf entry to a leaf block in a node-form directory.
484 * The other work necessary is done from the caller.
486 static int /* error */
488 struct xfs_buf
*bp
, /* leaf buffer */
489 struct xfs_da_args
*args
, /* operation arguments */
490 int index
) /* insertion pt for new entry */
492 struct xfs_dir3_icleaf_hdr leafhdr
;
493 struct xfs_inode
*dp
= args
->dp
;
494 struct xfs_dir2_leaf
*leaf
= bp
->b_addr
;
495 struct xfs_dir2_leaf_entry
*lep
;
496 struct xfs_dir2_leaf_entry
*ents
;
497 int compact
; /* compacting stale leaves */
498 int highstale
= 0; /* next stale entry */
499 int lfloghigh
; /* high leaf entry logging */
500 int lfloglow
; /* low leaf entry logging */
501 int lowstale
= 0; /* previous stale entry */
503 trace_xfs_dir2_leafn_add(args
, index
);
505 xfs_dir2_leaf_hdr_from_disk(dp
->i_mount
, &leafhdr
, leaf
);
509 * Quick check just to make sure we are not going to index
510 * into other peoples memory
513 xfs_buf_corruption_error(bp
);
514 return -EFSCORRUPTED
;
518 * If there are already the maximum number of leaf entries in
519 * the block, if there are no stale entries it won't fit.
520 * Caller will do a split. If there are stale entries we'll do
524 if (leafhdr
.count
== args
->geo
->leaf_max_ents
) {
527 compact
= leafhdr
.stale
> 1;
530 ASSERT(index
== 0 || be32_to_cpu(ents
[index
- 1].hashval
) <= args
->hashval
);
531 ASSERT(index
== leafhdr
.count
||
532 be32_to_cpu(ents
[index
].hashval
) >= args
->hashval
);
534 if (args
->op_flags
& XFS_DA_OP_JUSTCHECK
)
538 * Compact out all but one stale leaf entry. Leaves behind
539 * the entry closest to index.
542 xfs_dir3_leaf_compact_x1(&leafhdr
, ents
, &index
, &lowstale
,
543 &highstale
, &lfloglow
, &lfloghigh
);
544 else if (leafhdr
.stale
) {
546 * Set impossible logging indices for this case.
548 lfloglow
= leafhdr
.count
;
553 * Insert the new entry, log everything.
555 lep
= xfs_dir3_leaf_find_entry(&leafhdr
, ents
, index
, compact
, lowstale
,
556 highstale
, &lfloglow
, &lfloghigh
);
558 lep
->hashval
= cpu_to_be32(args
->hashval
);
559 lep
->address
= cpu_to_be32(xfs_dir2_db_off_to_dataptr(args
->geo
,
560 args
->blkno
, args
->index
));
562 xfs_dir2_leaf_hdr_to_disk(dp
->i_mount
, leaf
, &leafhdr
);
563 xfs_dir3_leaf_log_header(args
, bp
);
564 xfs_dir3_leaf_log_ents(args
, &leafhdr
, bp
, lfloglow
, lfloghigh
);
565 xfs_dir3_leaf_check(dp
, bp
);
571 xfs_dir2_free_hdr_check(
572 struct xfs_inode
*dp
,
576 struct xfs_dir3_icfree_hdr hdr
;
578 xfs_dir2_free_hdr_from_disk(dp
->i_mount
, &hdr
, bp
->b_addr
);
580 ASSERT((hdr
.firstdb
% dp
->i_mount
->m_dir_geo
->free_max_bests
) == 0);
581 ASSERT(hdr
.firstdb
<= db
);
582 ASSERT(db
< hdr
.firstdb
+ hdr
.nvalid
);
585 #define xfs_dir2_free_hdr_check(dp, bp, db)
589 * Return the last hash value in the leaf.
590 * Stale entries are ok.
592 xfs_dahash_t
/* hash value */
593 xfs_dir2_leaf_lasthash(
594 struct xfs_inode
*dp
,
595 struct xfs_buf
*bp
, /* leaf buffer */
596 int *count
) /* count of entries in leaf */
598 struct xfs_dir3_icleaf_hdr leafhdr
;
600 xfs_dir2_leaf_hdr_from_disk(dp
->i_mount
, &leafhdr
, bp
->b_addr
);
602 ASSERT(leafhdr
.magic
== XFS_DIR2_LEAFN_MAGIC
||
603 leafhdr
.magic
== XFS_DIR3_LEAFN_MAGIC
||
604 leafhdr
.magic
== XFS_DIR2_LEAF1_MAGIC
||
605 leafhdr
.magic
== XFS_DIR3_LEAF1_MAGIC
);
608 *count
= leafhdr
.count
;
611 return be32_to_cpu(leafhdr
.ents
[leafhdr
.count
- 1].hashval
);
615 * Look up a leaf entry for space to add a name in a node-format leaf block.
616 * The extrablk in state is a freespace block.
619 xfs_dir2_leafn_lookup_for_addname(
620 struct xfs_buf
*bp
, /* leaf buffer */
621 xfs_da_args_t
*args
, /* operation arguments */
622 int *indexp
, /* out: leaf entry index */
623 xfs_da_state_t
*state
) /* state to fill in */
625 struct xfs_buf
*curbp
= NULL
; /* current data/free buffer */
626 xfs_dir2_db_t curdb
= -1; /* current data block number */
627 xfs_dir2_db_t curfdb
= -1; /* current free block number */
628 xfs_inode_t
*dp
; /* incore directory inode */
629 int error
; /* error return value */
630 int fi
; /* free entry index */
631 xfs_dir2_free_t
*free
= NULL
; /* free block structure */
632 int index
; /* leaf entry index */
633 xfs_dir2_leaf_t
*leaf
; /* leaf structure */
634 int length
; /* length of new data entry */
635 xfs_dir2_leaf_entry_t
*lep
; /* leaf entry */
636 xfs_mount_t
*mp
; /* filesystem mount point */
637 xfs_dir2_db_t newdb
; /* new data block number */
638 xfs_dir2_db_t newfdb
; /* new free block number */
639 xfs_trans_t
*tp
; /* transaction pointer */
640 struct xfs_dir3_icleaf_hdr leafhdr
;
646 xfs_dir2_leaf_hdr_from_disk(mp
, &leafhdr
, leaf
);
648 xfs_dir3_leaf_check(dp
, bp
);
649 ASSERT(leafhdr
.count
> 0);
652 * Look up the hash value in the leaf entries.
654 index
= xfs_dir2_leaf_search_hash(args
, bp
);
656 * Do we have a buffer coming in?
658 if (state
->extravalid
) {
659 /* If so, it's a free block buffer, get the block number. */
660 curbp
= state
->extrablk
.bp
;
661 curfdb
= state
->extrablk
.blkno
;
662 free
= curbp
->b_addr
;
663 ASSERT(free
->hdr
.magic
== cpu_to_be32(XFS_DIR2_FREE_MAGIC
) ||
664 free
->hdr
.magic
== cpu_to_be32(XFS_DIR3_FREE_MAGIC
));
666 length
= xfs_dir2_data_entsize(mp
, args
->namelen
);
668 * Loop over leaf entries with the right hash value.
670 for (lep
= &leafhdr
.ents
[index
];
671 index
< leafhdr
.count
&& be32_to_cpu(lep
->hashval
) == args
->hashval
;
674 * Skip stale leaf entries.
676 if (be32_to_cpu(lep
->address
) == XFS_DIR2_NULL_DATAPTR
)
679 * Pull the data block number from the entry.
681 newdb
= xfs_dir2_dataptr_to_db(args
->geo
,
682 be32_to_cpu(lep
->address
));
684 * For addname, we're looking for a place to put the new entry.
685 * We want to use a data block with an entry of equal
686 * hash value to ours if there is one with room.
688 * If this block isn't the data block we already have
689 * in hand, take a look at it.
691 if (newdb
!= curdb
) {
692 struct xfs_dir3_icfree_hdr freehdr
;
696 * Convert the data block to the free block
697 * holding its freespace information.
699 newfdb
= xfs_dir2_db_to_fdb(args
->geo
, newdb
);
701 * If it's not the one we have in hand, read it in.
703 if (newfdb
!= curfdb
) {
705 * If we had one before, drop it.
708 xfs_trans_brelse(tp
, curbp
);
710 error
= xfs_dir2_free_read(tp
, dp
,
711 xfs_dir2_db_to_da(args
->geo
,
716 free
= curbp
->b_addr
;
718 xfs_dir2_free_hdr_check(dp
, curbp
, curdb
);
721 * Get the index for our entry.
723 fi
= xfs_dir2_db_to_fdindex(args
->geo
, curdb
);
725 * If it has room, return it.
727 xfs_dir2_free_hdr_from_disk(mp
, &freehdr
, free
);
728 if (XFS_IS_CORRUPT(mp
,
730 cpu_to_be16(NULLDATAOFF
))) {
731 if (curfdb
!= newfdb
)
732 xfs_trans_brelse(tp
, curbp
);
733 return -EFSCORRUPTED
;
736 if (be16_to_cpu(freehdr
.bests
[fi
]) >= length
)
740 /* Didn't find any space */
743 ASSERT(args
->op_flags
& XFS_DA_OP_OKNOENT
);
745 /* Giving back a free block. */
746 state
->extravalid
= 1;
747 state
->extrablk
.bp
= curbp
;
748 state
->extrablk
.index
= fi
;
749 state
->extrablk
.blkno
= curfdb
;
752 * Important: this magic number is not in the buffer - it's for
753 * buffer type information and therefore only the free/data type
754 * matters here, not whether CRCs are enabled or not.
756 state
->extrablk
.magic
= XFS_DIR2_FREE_MAGIC
;
758 state
->extravalid
= 0;
761 * Return the index, that will be the insertion point.
768 * Look up a leaf entry in a node-format leaf block.
769 * The extrablk in state a data block.
772 xfs_dir2_leafn_lookup_for_entry(
773 struct xfs_buf
*bp
, /* leaf buffer */
774 xfs_da_args_t
*args
, /* operation arguments */
775 int *indexp
, /* out: leaf entry index */
776 xfs_da_state_t
*state
) /* state to fill in */
778 struct xfs_buf
*curbp
= NULL
; /* current data/free buffer */
779 xfs_dir2_db_t curdb
= -1; /* current data block number */
780 xfs_dir2_data_entry_t
*dep
; /* data block entry */
781 xfs_inode_t
*dp
; /* incore directory inode */
782 int error
; /* error return value */
783 int index
; /* leaf entry index */
784 xfs_dir2_leaf_t
*leaf
; /* leaf structure */
785 xfs_dir2_leaf_entry_t
*lep
; /* leaf entry */
786 xfs_mount_t
*mp
; /* filesystem mount point */
787 xfs_dir2_db_t newdb
; /* new data block number */
788 xfs_trans_t
*tp
; /* transaction pointer */
789 enum xfs_dacmp cmp
; /* comparison result */
790 struct xfs_dir3_icleaf_hdr leafhdr
;
796 xfs_dir2_leaf_hdr_from_disk(mp
, &leafhdr
, leaf
);
798 xfs_dir3_leaf_check(dp
, bp
);
799 if (leafhdr
.count
<= 0) {
800 xfs_buf_corruption_error(bp
);
801 return -EFSCORRUPTED
;
805 * Look up the hash value in the leaf entries.
807 index
= xfs_dir2_leaf_search_hash(args
, bp
);
809 * Do we have a buffer coming in?
811 if (state
->extravalid
) {
812 curbp
= state
->extrablk
.bp
;
813 curdb
= state
->extrablk
.blkno
;
816 * Loop over leaf entries with the right hash value.
818 for (lep
= &leafhdr
.ents
[index
];
819 index
< leafhdr
.count
&& be32_to_cpu(lep
->hashval
) == args
->hashval
;
822 * Skip stale leaf entries.
824 if (be32_to_cpu(lep
->address
) == XFS_DIR2_NULL_DATAPTR
)
827 * Pull the data block number from the entry.
829 newdb
= xfs_dir2_dataptr_to_db(args
->geo
,
830 be32_to_cpu(lep
->address
));
832 * Not adding a new entry, so we really want to find
833 * the name given to us.
835 * If it's a different data block, go get it.
837 if (newdb
!= curdb
) {
839 * If we had a block before that we aren't saving
840 * for a CI name, drop it
842 if (curbp
&& (args
->cmpresult
== XFS_CMP_DIFFERENT
||
843 curdb
!= state
->extrablk
.blkno
))
844 xfs_trans_brelse(tp
, curbp
);
846 * If needing the block that is saved with a CI match,
847 * use it otherwise read in the new data block.
849 if (args
->cmpresult
!= XFS_CMP_DIFFERENT
&&
850 newdb
== state
->extrablk
.blkno
) {
851 ASSERT(state
->extravalid
);
852 curbp
= state
->extrablk
.bp
;
854 error
= xfs_dir3_data_read(tp
, dp
,
855 xfs_dir2_db_to_da(args
->geo
,
861 xfs_dir3_data_check(dp
, curbp
);
865 * Point to the data entry.
867 dep
= (xfs_dir2_data_entry_t
*)((char *)curbp
->b_addr
+
868 xfs_dir2_dataptr_to_off(args
->geo
,
869 be32_to_cpu(lep
->address
)));
871 * Compare the entry and if it's an exact match, return
872 * EEXIST immediately. If it's the first case-insensitive
873 * match, store the block & inode number and continue looking.
875 cmp
= xfs_dir2_compname(args
, dep
->name
, dep
->namelen
);
876 if (cmp
!= XFS_CMP_DIFFERENT
&& cmp
!= args
->cmpresult
) {
877 /* If there is a CI match block, drop it */
878 if (args
->cmpresult
!= XFS_CMP_DIFFERENT
&&
879 curdb
!= state
->extrablk
.blkno
)
880 xfs_trans_brelse(tp
, state
->extrablk
.bp
);
881 args
->cmpresult
= cmp
;
882 args
->inumber
= be64_to_cpu(dep
->inumber
);
883 args
->filetype
= xfs_dir2_data_get_ftype(mp
, dep
);
885 state
->extravalid
= 1;
886 state
->extrablk
.bp
= curbp
;
887 state
->extrablk
.blkno
= curdb
;
888 state
->extrablk
.index
= (int)((char *)dep
-
889 (char *)curbp
->b_addr
);
890 state
->extrablk
.magic
= XFS_DIR2_DATA_MAGIC
;
891 curbp
->b_ops
= &xfs_dir3_data_buf_ops
;
892 xfs_trans_buf_set_type(tp
, curbp
, XFS_BLFT_DIR_DATA_BUF
);
893 if (cmp
== XFS_CMP_EXACT
)
897 ASSERT(index
== leafhdr
.count
|| (args
->op_flags
& XFS_DA_OP_OKNOENT
));
899 if (args
->cmpresult
== XFS_CMP_DIFFERENT
) {
900 /* Giving back last used data block. */
901 state
->extravalid
= 1;
902 state
->extrablk
.bp
= curbp
;
903 state
->extrablk
.index
= -1;
904 state
->extrablk
.blkno
= curdb
;
905 state
->extrablk
.magic
= XFS_DIR2_DATA_MAGIC
;
906 curbp
->b_ops
= &xfs_dir3_data_buf_ops
;
907 xfs_trans_buf_set_type(tp
, curbp
, XFS_BLFT_DIR_DATA_BUF
);
909 /* If the curbp is not the CI match block, drop it */
910 if (state
->extrablk
.bp
!= curbp
)
911 xfs_trans_brelse(tp
, curbp
);
914 state
->extravalid
= 0;
921 * Look up a leaf entry in a node-format leaf block.
922 * If this is an addname then the extrablk in state is a freespace block,
923 * otherwise it's a data block.
926 xfs_dir2_leafn_lookup_int(
927 struct xfs_buf
*bp
, /* leaf buffer */
928 xfs_da_args_t
*args
, /* operation arguments */
929 int *indexp
, /* out: leaf entry index */
930 xfs_da_state_t
*state
) /* state to fill in */
932 if (args
->op_flags
& XFS_DA_OP_ADDNAME
)
933 return xfs_dir2_leafn_lookup_for_addname(bp
, args
, indexp
,
935 return xfs_dir2_leafn_lookup_for_entry(bp
, args
, indexp
, state
);
939 * Move count leaf entries from source to destination leaf.
940 * Log entries and headers. Stale entries are preserved.
943 xfs_dir3_leafn_moveents(
944 xfs_da_args_t
*args
, /* operation arguments */
945 struct xfs_buf
*bp_s
, /* source */
946 struct xfs_dir3_icleaf_hdr
*shdr
,
947 struct xfs_dir2_leaf_entry
*sents
,
948 int start_s
,/* source leaf index */
949 struct xfs_buf
*bp_d
, /* destination */
950 struct xfs_dir3_icleaf_hdr
*dhdr
,
951 struct xfs_dir2_leaf_entry
*dents
,
952 int start_d
,/* destination leaf index */
953 int count
) /* count of leaves to copy */
955 int stale
; /* count stale leaves copied */
957 trace_xfs_dir2_leafn_moveents(args
, start_s
, start_d
, count
);
960 * Silently return if nothing to do.
966 * If the destination index is not the end of the current
967 * destination leaf entries, open up a hole in the destination
968 * to hold the new entries.
970 if (start_d
< dhdr
->count
) {
971 memmove(&dents
[start_d
+ count
], &dents
[start_d
],
972 (dhdr
->count
- start_d
) * sizeof(xfs_dir2_leaf_entry_t
));
973 xfs_dir3_leaf_log_ents(args
, dhdr
, bp_d
, start_d
+ count
,
974 count
+ dhdr
->count
- 1);
977 * If the source has stale leaves, count the ones in the copy range
978 * so we can update the header correctly.
981 int i
; /* temp leaf index */
983 for (i
= start_s
, stale
= 0; i
< start_s
+ count
; i
++) {
984 if (sents
[i
].address
==
985 cpu_to_be32(XFS_DIR2_NULL_DATAPTR
))
991 * Copy the leaf entries from source to destination.
993 memcpy(&dents
[start_d
], &sents
[start_s
],
994 count
* sizeof(xfs_dir2_leaf_entry_t
));
995 xfs_dir3_leaf_log_ents(args
, dhdr
, bp_d
, start_d
, start_d
+ count
- 1);
998 * If there are source entries after the ones we copied,
999 * delete the ones we copied by sliding the next ones down.
1001 if (start_s
+ count
< shdr
->count
) {
1002 memmove(&sents
[start_s
], &sents
[start_s
+ count
],
1003 count
* sizeof(xfs_dir2_leaf_entry_t
));
1004 xfs_dir3_leaf_log_ents(args
, shdr
, bp_s
, start_s
,
1005 start_s
+ count
- 1);
1009 * Update the headers and log them.
1011 shdr
->count
-= count
;
1012 shdr
->stale
-= stale
;
1013 dhdr
->count
+= count
;
1014 dhdr
->stale
+= stale
;
1018 * Determine the sort order of two leaf blocks.
1019 * Returns 1 if both are valid and leaf2 should be before leaf1, else 0.
1021 int /* sort order */
1022 xfs_dir2_leafn_order(
1023 struct xfs_inode
*dp
,
1024 struct xfs_buf
*leaf1_bp
, /* leaf1 buffer */
1025 struct xfs_buf
*leaf2_bp
) /* leaf2 buffer */
1027 struct xfs_dir2_leaf
*leaf1
= leaf1_bp
->b_addr
;
1028 struct xfs_dir2_leaf
*leaf2
= leaf2_bp
->b_addr
;
1029 struct xfs_dir2_leaf_entry
*ents1
;
1030 struct xfs_dir2_leaf_entry
*ents2
;
1031 struct xfs_dir3_icleaf_hdr hdr1
;
1032 struct xfs_dir3_icleaf_hdr hdr2
;
1034 xfs_dir2_leaf_hdr_from_disk(dp
->i_mount
, &hdr1
, leaf1
);
1035 xfs_dir2_leaf_hdr_from_disk(dp
->i_mount
, &hdr2
, leaf2
);
1039 if (hdr1
.count
> 0 && hdr2
.count
> 0 &&
1040 (be32_to_cpu(ents2
[0].hashval
) < be32_to_cpu(ents1
[0].hashval
) ||
1041 be32_to_cpu(ents2
[hdr2
.count
- 1].hashval
) <
1042 be32_to_cpu(ents1
[hdr1
.count
- 1].hashval
)))
1048 * Rebalance leaf entries between two leaf blocks.
1049 * This is actually only called when the second block is new,
1050 * though the code deals with the general case.
1051 * A new entry will be inserted in one of the blocks, and that
1052 * entry is taken into account when balancing.
1055 xfs_dir2_leafn_rebalance(
1056 xfs_da_state_t
*state
, /* btree cursor */
1057 xfs_da_state_blk_t
*blk1
, /* first btree block */
1058 xfs_da_state_blk_t
*blk2
) /* second btree block */
1060 xfs_da_args_t
*args
; /* operation arguments */
1061 int count
; /* count (& direction) leaves */
1062 int isleft
; /* new goes in left leaf */
1063 xfs_dir2_leaf_t
*leaf1
; /* first leaf structure */
1064 xfs_dir2_leaf_t
*leaf2
; /* second leaf structure */
1065 int mid
; /* midpoint leaf index */
1066 #if defined(DEBUG) || defined(XFS_WARN)
1067 int oldstale
; /* old count of stale leaves */
1069 int oldsum
; /* old total leaf count */
1070 int swap_blocks
; /* swapped leaf blocks */
1071 struct xfs_dir2_leaf_entry
*ents1
;
1072 struct xfs_dir2_leaf_entry
*ents2
;
1073 struct xfs_dir3_icleaf_hdr hdr1
;
1074 struct xfs_dir3_icleaf_hdr hdr2
;
1075 struct xfs_inode
*dp
= state
->args
->dp
;
1079 * If the block order is wrong, swap the arguments.
1081 swap_blocks
= xfs_dir2_leafn_order(dp
, blk1
->bp
, blk2
->bp
);
1085 leaf1
= blk1
->bp
->b_addr
;
1086 leaf2
= blk2
->bp
->b_addr
;
1087 xfs_dir2_leaf_hdr_from_disk(dp
->i_mount
, &hdr1
, leaf1
);
1088 xfs_dir2_leaf_hdr_from_disk(dp
->i_mount
, &hdr2
, leaf2
);
1092 oldsum
= hdr1
.count
+ hdr2
.count
;
1093 #if defined(DEBUG) || defined(XFS_WARN)
1094 oldstale
= hdr1
.stale
+ hdr2
.stale
;
1099 * If the old leaf count was odd then the new one will be even,
1100 * so we need to divide the new count evenly.
1103 xfs_dahash_t midhash
; /* middle entry hash value */
1105 if (mid
>= hdr1
.count
)
1106 midhash
= be32_to_cpu(ents2
[mid
- hdr1
.count
].hashval
);
1108 midhash
= be32_to_cpu(ents1
[mid
].hashval
);
1109 isleft
= args
->hashval
<= midhash
;
1112 * If the old count is even then the new count is odd, so there's
1113 * no preferred side for the new entry.
1114 * Pick the left one.
1119 * Calculate moved entry count. Positive means left-to-right,
1120 * negative means right-to-left. Then move the entries.
1122 count
= hdr1
.count
- mid
+ (isleft
== 0);
1124 xfs_dir3_leafn_moveents(args
, blk1
->bp
, &hdr1
, ents1
,
1125 hdr1
.count
- count
, blk2
->bp
,
1126 &hdr2
, ents2
, 0, count
);
1128 xfs_dir3_leafn_moveents(args
, blk2
->bp
, &hdr2
, ents2
, 0,
1129 blk1
->bp
, &hdr1
, ents1
,
1132 ASSERT(hdr1
.count
+ hdr2
.count
== oldsum
);
1133 ASSERT(hdr1
.stale
+ hdr2
.stale
== oldstale
);
1135 /* log the changes made when moving the entries */
1136 xfs_dir2_leaf_hdr_to_disk(dp
->i_mount
, leaf1
, &hdr1
);
1137 xfs_dir2_leaf_hdr_to_disk(dp
->i_mount
, leaf2
, &hdr2
);
1138 xfs_dir3_leaf_log_header(args
, blk1
->bp
);
1139 xfs_dir3_leaf_log_header(args
, blk2
->bp
);
1141 xfs_dir3_leaf_check(dp
, blk1
->bp
);
1142 xfs_dir3_leaf_check(dp
, blk2
->bp
);
1145 * Mark whether we're inserting into the old or new leaf.
1147 if (hdr1
.count
< hdr2
.count
)
1148 state
->inleaf
= swap_blocks
;
1149 else if (hdr1
.count
> hdr2
.count
)
1150 state
->inleaf
= !swap_blocks
;
1152 state
->inleaf
= swap_blocks
^ (blk1
->index
<= hdr1
.count
);
1154 * Adjust the expected index for insertion.
1157 blk2
->index
= blk1
->index
- hdr1
.count
;
1160 * Finally sanity check just to make sure we are not returning a
1163 if (blk2
->index
< 0) {
1166 xfs_alert(dp
->i_mount
,
1167 "%s: picked the wrong leaf? reverting original leaf: blk1->index %d",
1168 __func__
, blk1
->index
);
1173 xfs_dir3_data_block_free(
1174 xfs_da_args_t
*args
,
1175 struct xfs_dir2_data_hdr
*hdr
,
1176 struct xfs_dir2_free
*free
,
1179 struct xfs_buf
*fbp
,
1183 struct xfs_dir3_icfree_hdr freehdr
;
1184 struct xfs_inode
*dp
= args
->dp
;
1186 xfs_dir2_free_hdr_from_disk(dp
->i_mount
, &freehdr
, free
);
1189 * Data block is not empty, just set the free entry to the new
1192 freehdr
.bests
[findex
] = cpu_to_be16(longest
);
1193 xfs_dir2_free_log_bests(args
, &freehdr
, fbp
, findex
, findex
);
1197 /* One less used entry in the free table. */
1201 * If this was the last entry in the table, we can trim the table size
1202 * back. There might be other entries at the end referring to
1203 * non-existent data blocks, get those too.
1205 if (findex
== freehdr
.nvalid
- 1) {
1206 int i
; /* free entry index */
1208 for (i
= findex
- 1; i
>= 0; i
--) {
1209 if (freehdr
.bests
[i
] != cpu_to_be16(NULLDATAOFF
))
1212 freehdr
.nvalid
= i
+ 1;
1215 /* Not the last entry, just punch it out. */
1216 freehdr
.bests
[findex
] = cpu_to_be16(NULLDATAOFF
);
1220 xfs_dir2_free_hdr_to_disk(dp
->i_mount
, free
, &freehdr
);
1221 xfs_dir2_free_log_header(args
, fbp
);
1224 * If there are no useful entries left in the block, get rid of the
1227 if (!freehdr
.nused
) {
1230 error
= xfs_dir2_shrink_inode(args
, fdb
, fbp
);
1234 } else if (error
!= -ENOSPC
|| args
->total
!= 0)
1237 * It's possible to get ENOSPC if there is no
1238 * space reservation. In this case some one
1239 * else will eventually get rid of this block.
1243 /* Log the free entry that changed, unless we got rid of it. */
1245 xfs_dir2_free_log_bests(args
, &freehdr
, fbp
, findex
, findex
);
1250 * Remove an entry from a node directory.
1251 * This removes the leaf entry and the data entry,
1252 * and updates the free block if necessary.
1254 static int /* error */
1255 xfs_dir2_leafn_remove(
1256 xfs_da_args_t
*args
, /* operation arguments */
1257 struct xfs_buf
*bp
, /* leaf buffer */
1258 int index
, /* leaf entry index */
1259 xfs_da_state_blk_t
*dblk
, /* data block */
1260 int *rval
) /* resulting block needs join */
1262 struct xfs_da_geometry
*geo
= args
->geo
;
1263 xfs_dir2_data_hdr_t
*hdr
; /* data block header */
1264 xfs_dir2_db_t db
; /* data block number */
1265 struct xfs_buf
*dbp
; /* data block buffer */
1266 xfs_dir2_data_entry_t
*dep
; /* data block entry */
1267 xfs_inode_t
*dp
; /* incore directory inode */
1268 xfs_dir2_leaf_t
*leaf
; /* leaf structure */
1269 xfs_dir2_leaf_entry_t
*lep
; /* leaf entry */
1270 int longest
; /* longest data free entry */
1271 int off
; /* data block entry offset */
1272 int needlog
; /* need to log data header */
1273 int needscan
; /* need to rescan data frees */
1274 xfs_trans_t
*tp
; /* transaction pointer */
1275 struct xfs_dir2_data_free
*bf
; /* bestfree table */
1276 struct xfs_dir3_icleaf_hdr leafhdr
;
1278 trace_xfs_dir2_leafn_remove(args
, index
);
1283 xfs_dir2_leaf_hdr_from_disk(dp
->i_mount
, &leafhdr
, leaf
);
1286 * Point to the entry we're removing.
1288 lep
= &leafhdr
.ents
[index
];
1291 * Extract the data block and offset from the entry.
1293 db
= xfs_dir2_dataptr_to_db(geo
, be32_to_cpu(lep
->address
));
1294 ASSERT(dblk
->blkno
== db
);
1295 off
= xfs_dir2_dataptr_to_off(geo
, be32_to_cpu(lep
->address
));
1296 ASSERT(dblk
->index
== off
);
1299 * Kill the leaf entry by marking it stale.
1300 * Log the leaf block changes.
1303 xfs_dir2_leaf_hdr_to_disk(dp
->i_mount
, leaf
, &leafhdr
);
1304 xfs_dir3_leaf_log_header(args
, bp
);
1306 lep
->address
= cpu_to_be32(XFS_DIR2_NULL_DATAPTR
);
1307 xfs_dir3_leaf_log_ents(args
, &leafhdr
, bp
, index
, index
);
1310 * Make the data entry free. Keep track of the longest freespace
1311 * in the data block in case it changes.
1315 dep
= (xfs_dir2_data_entry_t
*)((char *)hdr
+ off
);
1316 bf
= xfs_dir2_data_bestfree_p(dp
->i_mount
, hdr
);
1317 longest
= be16_to_cpu(bf
[0].length
);
1318 needlog
= needscan
= 0;
1319 xfs_dir2_data_make_free(args
, dbp
, off
,
1320 xfs_dir2_data_entsize(dp
->i_mount
, dep
->namelen
), &needlog
,
1323 * Rescan the data block freespaces for bestfree.
1324 * Log the data block header if needed.
1327 xfs_dir2_data_freescan(dp
->i_mount
, hdr
, &needlog
);
1329 xfs_dir2_data_log_header(args
, dbp
);
1330 xfs_dir3_data_check(dp
, dbp
);
1332 * If the longest data block freespace changes, need to update
1333 * the corresponding freeblock entry.
1335 if (longest
< be16_to_cpu(bf
[0].length
)) {
1336 int error
; /* error return value */
1337 struct xfs_buf
*fbp
; /* freeblock buffer */
1338 xfs_dir2_db_t fdb
; /* freeblock block number */
1339 int findex
; /* index in freeblock entries */
1340 xfs_dir2_free_t
*free
; /* freeblock structure */
1343 * Convert the data block number to a free block,
1344 * read in the free block.
1346 fdb
= xfs_dir2_db_to_fdb(geo
, db
);
1347 error
= xfs_dir2_free_read(tp
, dp
, xfs_dir2_db_to_da(geo
, fdb
),
1354 struct xfs_dir3_icfree_hdr freehdr
;
1356 xfs_dir2_free_hdr_from_disk(dp
->i_mount
, &freehdr
, free
);
1357 ASSERT(freehdr
.firstdb
== geo
->free_max_bests
*
1358 (fdb
- xfs_dir2_byte_to_db(geo
, XFS_DIR2_FREE_OFFSET
)));
1362 * Calculate which entry we need to fix.
1364 findex
= xfs_dir2_db_to_fdindex(geo
, db
);
1365 longest
= be16_to_cpu(bf
[0].length
);
1367 * If the data block is now empty we can get rid of it
1370 if (longest
== geo
->blksize
- geo
->data_entry_offset
) {
1372 * Try to punch out the data block.
1374 error
= xfs_dir2_shrink_inode(args
, db
, dbp
);
1380 * We can get ENOSPC if there's no space reservation.
1381 * In this case just drop the buffer and some one else
1382 * will eventually get rid of the empty block.
1384 else if (!(error
== -ENOSPC
&& args
->total
== 0))
1388 * If we got rid of the data block, we can eliminate that entry
1389 * in the free block.
1391 error
= xfs_dir3_data_block_free(args
, hdr
, free
,
1392 fdb
, findex
, fbp
, longest
);
1397 xfs_dir3_leaf_check(dp
, bp
);
1399 * Return indication of whether this leaf block is empty enough
1400 * to justify trying to join it with a neighbor.
1402 *rval
= (geo
->leaf_hdr_size
+
1403 (uint
)sizeof(leafhdr
.ents
) * (leafhdr
.count
- leafhdr
.stale
)) <
1409 * Split the leaf entries in the old block into old and new blocks.
1412 xfs_dir2_leafn_split(
1413 xfs_da_state_t
*state
, /* btree cursor */
1414 xfs_da_state_blk_t
*oldblk
, /* original block */
1415 xfs_da_state_blk_t
*newblk
) /* newly created block */
1417 xfs_da_args_t
*args
; /* operation arguments */
1418 xfs_dablk_t blkno
; /* new leaf block number */
1419 int error
; /* error return value */
1420 struct xfs_inode
*dp
;
1423 * Allocate space for a new leaf node.
1427 ASSERT(oldblk
->magic
== XFS_DIR2_LEAFN_MAGIC
);
1428 error
= xfs_da_grow_inode(args
, &blkno
);
1433 * Initialize the new leaf block.
1435 error
= xfs_dir3_leaf_get_buf(args
, xfs_dir2_da_to_db(args
->geo
, blkno
),
1436 &newblk
->bp
, XFS_DIR2_LEAFN_MAGIC
);
1440 newblk
->blkno
= blkno
;
1441 newblk
->magic
= XFS_DIR2_LEAFN_MAGIC
;
1443 * Rebalance the entries across the two leaves, link the new
1444 * block into the leaves.
1446 xfs_dir2_leafn_rebalance(state
, oldblk
, newblk
);
1447 error
= xfs_da3_blk_link(state
, oldblk
, newblk
);
1452 * Insert the new entry in the correct block.
1455 error
= xfs_dir2_leafn_add(oldblk
->bp
, args
, oldblk
->index
);
1457 error
= xfs_dir2_leafn_add(newblk
->bp
, args
, newblk
->index
);
1459 * Update last hashval in each block since we added the name.
1461 oldblk
->hashval
= xfs_dir2_leaf_lasthash(dp
, oldblk
->bp
, NULL
);
1462 newblk
->hashval
= xfs_dir2_leaf_lasthash(dp
, newblk
->bp
, NULL
);
1463 xfs_dir3_leaf_check(dp
, oldblk
->bp
);
1464 xfs_dir3_leaf_check(dp
, newblk
->bp
);
1469 * Check a leaf block and its neighbors to see if the block should be
1470 * collapsed into one or the other neighbor. Always keep the block
1471 * with the smaller block number.
1472 * If the current block is over 50% full, don't try to join it, return 0.
1473 * If the block is empty, fill in the state structure and return 2.
1474 * If it can be collapsed, fill in the state structure and return 1.
1475 * If nothing can be done, return 0.
1478 xfs_dir2_leafn_toosmall(
1479 xfs_da_state_t
*state
, /* btree cursor */
1480 int *action
) /* resulting action to take */
1482 xfs_da_state_blk_t
*blk
; /* leaf block */
1483 xfs_dablk_t blkno
; /* leaf block number */
1484 struct xfs_buf
*bp
; /* leaf buffer */
1485 int bytes
; /* bytes in use */
1486 int count
; /* leaf live entry count */
1487 int error
; /* error return value */
1488 int forward
; /* sibling block direction */
1489 int i
; /* sibling counter */
1490 xfs_dir2_leaf_t
*leaf
; /* leaf structure */
1491 int rval
; /* result from path_shift */
1492 struct xfs_dir3_icleaf_hdr leafhdr
;
1493 struct xfs_dir2_leaf_entry
*ents
;
1494 struct xfs_inode
*dp
= state
->args
->dp
;
1497 * Check for the degenerate case of the block being over 50% full.
1498 * If so, it's not worth even looking to see if we might be able
1499 * to coalesce with a sibling.
1501 blk
= &state
->path
.blk
[state
->path
.active
- 1];
1502 leaf
= blk
->bp
->b_addr
;
1503 xfs_dir2_leaf_hdr_from_disk(dp
->i_mount
, &leafhdr
, leaf
);
1504 ents
= leafhdr
.ents
;
1505 xfs_dir3_leaf_check(dp
, blk
->bp
);
1507 count
= leafhdr
.count
- leafhdr
.stale
;
1508 bytes
= state
->args
->geo
->leaf_hdr_size
+ count
* sizeof(ents
[0]);
1509 if (bytes
> (state
->args
->geo
->blksize
>> 1)) {
1511 * Blk over 50%, don't try to join.
1517 * Check for the degenerate case of the block being empty.
1518 * If the block is empty, we'll simply delete it, no need to
1519 * coalesce it with a sibling block. We choose (arbitrarily)
1520 * to merge with the forward block unless it is NULL.
1524 * Make altpath point to the block we want to keep and
1525 * path point to the block we want to drop (this one).
1527 forward
= (leafhdr
.forw
!= 0);
1528 memcpy(&state
->altpath
, &state
->path
, sizeof(state
->path
));
1529 error
= xfs_da3_path_shift(state
, &state
->altpath
, forward
, 0,
1533 *action
= rval
? 2 : 0;
1537 * Examine each sibling block to see if we can coalesce with
1538 * at least 25% free space to spare. We need to figure out
1539 * whether to merge with the forward or the backward block.
1540 * We prefer coalescing with the lower numbered sibling so as
1541 * to shrink a directory over time.
1543 forward
= leafhdr
.forw
< leafhdr
.back
;
1544 for (i
= 0, bp
= NULL
; i
< 2; forward
= !forward
, i
++) {
1545 struct xfs_dir3_icleaf_hdr hdr2
;
1547 blkno
= forward
? leafhdr
.forw
: leafhdr
.back
;
1551 * Read the sibling leaf block.
1553 error
= xfs_dir3_leafn_read(state
->args
->trans
, dp
, blkno
, &bp
);
1558 * Count bytes in the two blocks combined.
1560 count
= leafhdr
.count
- leafhdr
.stale
;
1561 bytes
= state
->args
->geo
->blksize
-
1562 (state
->args
->geo
->blksize
>> 2);
1565 xfs_dir2_leaf_hdr_from_disk(dp
->i_mount
, &hdr2
, leaf
);
1567 count
+= hdr2
.count
- hdr2
.stale
;
1568 bytes
-= count
* sizeof(ents
[0]);
1571 * Fits with at least 25% to spare.
1575 xfs_trans_brelse(state
->args
->trans
, bp
);
1578 * Didn't like either block, give up.
1586 * Make altpath point to the block we want to keep (the lower
1587 * numbered block) and path point to the block we want to drop.
1589 memcpy(&state
->altpath
, &state
->path
, sizeof(state
->path
));
1590 if (blkno
< blk
->blkno
)
1591 error
= xfs_da3_path_shift(state
, &state
->altpath
, forward
, 0,
1594 error
= xfs_da3_path_shift(state
, &state
->path
, forward
, 0,
1599 *action
= rval
? 0 : 1;
1604 * Move all the leaf entries from drop_blk to save_blk.
1605 * This is done as part of a join operation.
1608 xfs_dir2_leafn_unbalance(
1609 xfs_da_state_t
*state
, /* cursor */
1610 xfs_da_state_blk_t
*drop_blk
, /* dead block */
1611 xfs_da_state_blk_t
*save_blk
) /* surviving block */
1613 xfs_da_args_t
*args
; /* operation arguments */
1614 xfs_dir2_leaf_t
*drop_leaf
; /* dead leaf structure */
1615 xfs_dir2_leaf_t
*save_leaf
; /* surviving leaf structure */
1616 struct xfs_dir3_icleaf_hdr savehdr
;
1617 struct xfs_dir3_icleaf_hdr drophdr
;
1618 struct xfs_dir2_leaf_entry
*sents
;
1619 struct xfs_dir2_leaf_entry
*dents
;
1620 struct xfs_inode
*dp
= state
->args
->dp
;
1623 ASSERT(drop_blk
->magic
== XFS_DIR2_LEAFN_MAGIC
);
1624 ASSERT(save_blk
->magic
== XFS_DIR2_LEAFN_MAGIC
);
1625 drop_leaf
= drop_blk
->bp
->b_addr
;
1626 save_leaf
= save_blk
->bp
->b_addr
;
1628 xfs_dir2_leaf_hdr_from_disk(dp
->i_mount
, &savehdr
, save_leaf
);
1629 xfs_dir2_leaf_hdr_from_disk(dp
->i_mount
, &drophdr
, drop_leaf
);
1630 sents
= savehdr
.ents
;
1631 dents
= drophdr
.ents
;
1634 * If there are any stale leaf entries, take this opportunity
1638 xfs_dir3_leaf_compact(args
, &drophdr
, drop_blk
->bp
);
1640 xfs_dir3_leaf_compact(args
, &savehdr
, save_blk
->bp
);
1643 * Move the entries from drop to the appropriate end of save.
1645 drop_blk
->hashval
= be32_to_cpu(dents
[drophdr
.count
- 1].hashval
);
1646 if (xfs_dir2_leafn_order(dp
, save_blk
->bp
, drop_blk
->bp
))
1647 xfs_dir3_leafn_moveents(args
, drop_blk
->bp
, &drophdr
, dents
, 0,
1648 save_blk
->bp
, &savehdr
, sents
, 0,
1651 xfs_dir3_leafn_moveents(args
, drop_blk
->bp
, &drophdr
, dents
, 0,
1652 save_blk
->bp
, &savehdr
, sents
,
1653 savehdr
.count
, drophdr
.count
);
1654 save_blk
->hashval
= be32_to_cpu(sents
[savehdr
.count
- 1].hashval
);
1656 /* log the changes made when moving the entries */
1657 xfs_dir2_leaf_hdr_to_disk(dp
->i_mount
, save_leaf
, &savehdr
);
1658 xfs_dir2_leaf_hdr_to_disk(dp
->i_mount
, drop_leaf
, &drophdr
);
1659 xfs_dir3_leaf_log_header(args
, save_blk
->bp
);
1660 xfs_dir3_leaf_log_header(args
, drop_blk
->bp
);
1662 xfs_dir3_leaf_check(dp
, save_blk
->bp
);
1663 xfs_dir3_leaf_check(dp
, drop_blk
->bp
);
1667 * Add a new data block to the directory at the free space index that the caller
1671 xfs_dir2_node_add_datablk(
1672 struct xfs_da_args
*args
,
1673 struct xfs_da_state_blk
*fblk
,
1674 xfs_dir2_db_t
*dbno
,
1675 struct xfs_buf
**dbpp
,
1676 struct xfs_buf
**fbpp
,
1677 struct xfs_dir3_icfree_hdr
*hdr
,
1680 struct xfs_inode
*dp
= args
->dp
;
1681 struct xfs_trans
*tp
= args
->trans
;
1682 struct xfs_mount
*mp
= dp
->i_mount
;
1683 struct xfs_dir2_data_free
*bf
;
1685 struct xfs_buf
*fbp
;
1686 struct xfs_buf
*dbp
;
1689 /* Not allowed to allocate, return failure. */
1690 if (args
->total
== 0)
1693 /* Allocate and initialize the new data block. */
1694 error
= xfs_dir2_grow_inode(args
, XFS_DIR2_DATA_SPACE
, dbno
);
1697 error
= xfs_dir3_data_init(args
, *dbno
, &dbp
);
1702 * Get the freespace block corresponding to the data block
1703 * that was just allocated.
1705 fbno
= xfs_dir2_db_to_fdb(args
->geo
, *dbno
);
1706 error
= xfs_dir2_free_try_read(tp
, dp
,
1707 xfs_dir2_db_to_da(args
->geo
, fbno
), &fbp
);
1712 * If there wasn't a freespace block, the read will
1713 * return a NULL fbp. Allocate and initialize a new one.
1716 error
= xfs_dir2_grow_inode(args
, XFS_DIR2_FREE_SPACE
, &fbno
);
1720 if (XFS_IS_CORRUPT(mp
,
1721 xfs_dir2_db_to_fdb(args
->geo
, *dbno
) !=
1724 "%s: dir ino %llu needed freesp block %lld for data block %lld, got %lld",
1725 __func__
, (unsigned long long)dp
->i_ino
,
1726 (long long)xfs_dir2_db_to_fdb(args
->geo
, *dbno
),
1727 (long long)*dbno
, (long long)fbno
);
1730 " fblk "PTR_FMT
" blkno %llu index %d magic 0x%x",
1731 fblk
, (unsigned long long)fblk
->blkno
,
1732 fblk
->index
, fblk
->magic
);
1734 xfs_alert(mp
, " ... fblk is NULL");
1736 return -EFSCORRUPTED
;
1739 /* Get a buffer for the new block. */
1740 error
= xfs_dir3_free_get_buf(args
, fbno
, &fbp
);
1743 xfs_dir2_free_hdr_from_disk(mp
, hdr
, fbp
->b_addr
);
1745 /* Remember the first slot as our empty slot. */
1746 hdr
->firstdb
= (fbno
- xfs_dir2_byte_to_db(args
->geo
,
1747 XFS_DIR2_FREE_OFFSET
)) *
1748 args
->geo
->free_max_bests
;
1750 xfs_dir2_free_hdr_from_disk(mp
, hdr
, fbp
->b_addr
);
1753 /* Set the freespace block index from the data block number. */
1754 *findex
= xfs_dir2_db_to_fdindex(args
->geo
, *dbno
);
1756 /* Extend the freespace table if the new data block is off the end. */
1757 if (*findex
>= hdr
->nvalid
) {
1758 ASSERT(*findex
< args
->geo
->free_max_bests
);
1759 hdr
->nvalid
= *findex
+ 1;
1760 hdr
->bests
[*findex
] = cpu_to_be16(NULLDATAOFF
);
1764 * If this entry was for an empty data block (this should always be
1765 * true) then update the header.
1767 if (hdr
->bests
[*findex
] == cpu_to_be16(NULLDATAOFF
)) {
1769 xfs_dir2_free_hdr_to_disk(mp
, fbp
->b_addr
, hdr
);
1770 xfs_dir2_free_log_header(args
, fbp
);
1773 /* Update the freespace value for the new block in the table. */
1774 bf
= xfs_dir2_data_bestfree_p(mp
, dbp
->b_addr
);
1775 hdr
->bests
[*findex
] = bf
[0].length
;
1783 xfs_dir2_node_find_freeblk(
1784 struct xfs_da_args
*args
,
1785 struct xfs_da_state_blk
*fblk
,
1786 xfs_dir2_db_t
*dbnop
,
1787 struct xfs_buf
**fbpp
,
1788 struct xfs_dir3_icfree_hdr
*hdr
,
1792 struct xfs_inode
*dp
= args
->dp
;
1793 struct xfs_trans
*tp
= args
->trans
;
1794 struct xfs_buf
*fbp
= NULL
;
1795 xfs_dir2_db_t firstfbno
;
1796 xfs_dir2_db_t lastfbno
;
1797 xfs_dir2_db_t ifbno
= -1;
1798 xfs_dir2_db_t dbno
= -1;
1805 * If we came in with a freespace block that means that lookup
1806 * found an entry with our hash value. This is the freespace
1807 * block for that data entry.
1811 findex
= fblk
->index
;
1812 xfs_dir2_free_hdr_from_disk(dp
->i_mount
, hdr
, fbp
->b_addr
);
1814 /* caller already found the freespace for us. */
1815 ASSERT(findex
< hdr
->nvalid
);
1816 ASSERT(be16_to_cpu(hdr
->bests
[findex
]) != NULLDATAOFF
);
1817 ASSERT(be16_to_cpu(hdr
->bests
[findex
]) >= length
);
1818 dbno
= hdr
->firstdb
+ findex
;
1823 * The data block looked at didn't have enough room.
1824 * We'll start at the beginning of the freespace entries.
1826 ifbno
= fblk
->blkno
;
1827 xfs_trans_brelse(tp
, fbp
);
1833 * If we don't have a data block yet, we're going to scan the freespace
1834 * data for a data block with enough free space in it.
1836 error
= xfs_bmap_last_offset(dp
, &fo
, XFS_DATA_FORK
);
1839 lastfbno
= xfs_dir2_da_to_db(args
->geo
, (xfs_dablk_t
)fo
);
1840 firstfbno
= xfs_dir2_byte_to_db(args
->geo
, XFS_DIR2_FREE_OFFSET
);
1842 for (fbno
= lastfbno
- 1; fbno
>= firstfbno
; fbno
--) {
1843 /* If it's ifbno we already looked at it. */
1848 * Read the block. There can be holes in the freespace blocks,
1849 * so this might not succeed. This should be really rare, so
1850 * there's no reason to avoid it.
1852 error
= xfs_dir2_free_try_read(tp
, dp
,
1853 xfs_dir2_db_to_da(args
->geo
, fbno
),
1860 xfs_dir2_free_hdr_from_disk(dp
->i_mount
, hdr
, fbp
->b_addr
);
1862 /* Scan the free entry array for a large enough free space. */
1863 for (findex
= hdr
->nvalid
- 1; findex
>= 0; findex
--) {
1864 if (be16_to_cpu(hdr
->bests
[findex
]) != NULLDATAOFF
&&
1865 be16_to_cpu(hdr
->bests
[findex
]) >= length
) {
1866 dbno
= hdr
->firstdb
+ findex
;
1871 /* Didn't find free space, go on to next free block */
1872 xfs_trans_brelse(tp
, fbp
);
1883 * Add the data entry for a node-format directory name addition.
1884 * The leaf entry is added in xfs_dir2_leafn_add.
1885 * We may enter with a freespace block that the lookup found.
1888 xfs_dir2_node_addname_int(
1889 struct xfs_da_args
*args
, /* operation arguments */
1890 struct xfs_da_state_blk
*fblk
) /* optional freespace block */
1892 struct xfs_dir2_data_unused
*dup
; /* data unused entry pointer */
1893 struct xfs_dir2_data_entry
*dep
; /* data entry pointer */
1894 struct xfs_dir2_data_hdr
*hdr
; /* data block header */
1895 struct xfs_dir2_data_free
*bf
;
1896 struct xfs_trans
*tp
= args
->trans
;
1897 struct xfs_inode
*dp
= args
->dp
;
1898 struct xfs_dir3_icfree_hdr freehdr
;
1899 struct xfs_buf
*dbp
; /* data block buffer */
1900 struct xfs_buf
*fbp
; /* freespace buffer */
1901 xfs_dir2_data_aoff_t aoff
;
1902 xfs_dir2_db_t dbno
; /* data block number */
1903 int error
; /* error return value */
1904 int findex
; /* freespace entry index */
1905 int length
; /* length of the new entry */
1906 int logfree
= 0; /* need to log free entry */
1907 int needlog
= 0; /* need to log data header */
1908 int needscan
= 0; /* need to rescan data frees */
1909 __be16
*tagp
; /* data entry tag pointer */
1911 length
= xfs_dir2_data_entsize(dp
->i_mount
, args
->namelen
);
1912 error
= xfs_dir2_node_find_freeblk(args
, fblk
, &dbno
, &fbp
, &freehdr
,
1918 * Now we know if we must allocate blocks, so if we are checking whether
1919 * we can insert without allocation then we can return now.
1921 if (args
->op_flags
& XFS_DA_OP_JUSTCHECK
) {
1928 * If we don't have a data block, we need to allocate one and make
1929 * the freespace entries refer to it.
1932 /* we're going to have to log the free block index later */
1934 error
= xfs_dir2_node_add_datablk(args
, fblk
, &dbno
, &dbp
, &fbp
,
1937 /* Read the data block in. */
1938 error
= xfs_dir3_data_read(tp
, dp
,
1939 xfs_dir2_db_to_da(args
->geo
, dbno
),
1945 /* setup for data block up now */
1947 bf
= xfs_dir2_data_bestfree_p(dp
->i_mount
, hdr
);
1948 ASSERT(be16_to_cpu(bf
[0].length
) >= length
);
1950 /* Point to the existing unused space. */
1951 dup
= (xfs_dir2_data_unused_t
*)
1952 ((char *)hdr
+ be16_to_cpu(bf
[0].offset
));
1954 /* Mark the first part of the unused space, inuse for us. */
1955 aoff
= (xfs_dir2_data_aoff_t
)((char *)dup
- (char *)hdr
);
1956 error
= xfs_dir2_data_use_free(args
, dbp
, dup
, aoff
, length
,
1957 &needlog
, &needscan
);
1959 xfs_trans_brelse(tp
, dbp
);
1963 /* Fill in the new entry and log it. */
1964 dep
= (xfs_dir2_data_entry_t
*)dup
;
1965 dep
->inumber
= cpu_to_be64(args
->inumber
);
1966 dep
->namelen
= args
->namelen
;
1967 memcpy(dep
->name
, args
->name
, dep
->namelen
);
1968 xfs_dir2_data_put_ftype(dp
->i_mount
, dep
, args
->filetype
);
1969 tagp
= xfs_dir2_data_entry_tag_p(dp
->i_mount
, dep
);
1970 *tagp
= cpu_to_be16((char *)dep
- (char *)hdr
);
1971 xfs_dir2_data_log_entry(args
, dbp
, dep
);
1973 /* Rescan the freespace and log the data block if needed. */
1975 xfs_dir2_data_freescan(dp
->i_mount
, hdr
, &needlog
);
1977 xfs_dir2_data_log_header(args
, dbp
);
1979 /* If the freespace block entry is now wrong, update it. */
1980 if (freehdr
.bests
[findex
] != bf
[0].length
) {
1981 freehdr
.bests
[findex
] = bf
[0].length
;
1985 /* Log the freespace entry if needed. */
1987 xfs_dir2_free_log_bests(args
, &freehdr
, fbp
, findex
, findex
);
1989 /* Return the data block and offset in args. */
1990 args
->blkno
= (xfs_dablk_t
)dbno
;
1991 args
->index
= be16_to_cpu(*tagp
);
1996 * Top-level node form directory addname routine.
1999 xfs_dir2_node_addname(
2000 xfs_da_args_t
*args
) /* operation arguments */
2002 xfs_da_state_blk_t
*blk
; /* leaf block for insert */
2003 int error
; /* error return value */
2004 int rval
; /* sub-return value */
2005 xfs_da_state_t
*state
; /* btree cursor */
2007 trace_xfs_dir2_node_addname(args
);
2010 * Allocate and initialize the state (btree cursor).
2012 state
= xfs_da_state_alloc();
2014 state
->mp
= args
->dp
->i_mount
;
2016 * Look up the name. We're not supposed to find it, but
2017 * this gives us the insertion point.
2019 error
= xfs_da3_node_lookup_int(state
, &rval
);
2022 if (rval
!= -ENOENT
) {
2026 * Add the data entry to a data block.
2027 * Extravalid is set to a freeblock found by lookup.
2029 rval
= xfs_dir2_node_addname_int(args
,
2030 state
->extravalid
? &state
->extrablk
: NULL
);
2034 blk
= &state
->path
.blk
[state
->path
.active
- 1];
2035 ASSERT(blk
->magic
== XFS_DIR2_LEAFN_MAGIC
);
2037 * Add the new leaf entry.
2039 rval
= xfs_dir2_leafn_add(blk
->bp
, args
, blk
->index
);
2042 * It worked, fix the hash values up the btree.
2044 if (!(args
->op_flags
& XFS_DA_OP_JUSTCHECK
))
2045 xfs_da3_fixhashpath(state
, &state
->path
);
2048 * It didn't work, we need to split the leaf block.
2050 if (args
->total
== 0) {
2051 ASSERT(rval
== -ENOSPC
);
2055 * Split the leaf block and insert the new entry.
2057 rval
= xfs_da3_split(state
);
2060 xfs_da_state_free(state
);
2065 * Lookup an entry in a node-format directory.
2066 * All the real work happens in xfs_da3_node_lookup_int.
2067 * The only real output is the inode number of the entry.
2070 xfs_dir2_node_lookup(
2071 xfs_da_args_t
*args
) /* operation arguments */
2073 int error
; /* error return value */
2074 int i
; /* btree level */
2075 int rval
; /* operation return value */
2076 xfs_da_state_t
*state
; /* btree cursor */
2078 trace_xfs_dir2_node_lookup(args
);
2081 * Allocate and initialize the btree cursor.
2083 state
= xfs_da_state_alloc();
2085 state
->mp
= args
->dp
->i_mount
;
2087 * Fill in the path to the entry in the cursor.
2089 error
= xfs_da3_node_lookup_int(state
, &rval
);
2092 else if (rval
== -ENOENT
&& args
->cmpresult
== XFS_CMP_CASE
) {
2093 /* If a CI match, dup the actual name and return -EEXIST */
2094 xfs_dir2_data_entry_t
*dep
;
2096 dep
= (xfs_dir2_data_entry_t
*)
2097 ((char *)state
->extrablk
.bp
->b_addr
+
2098 state
->extrablk
.index
);
2099 rval
= xfs_dir_cilookup_result(args
, dep
->name
, dep
->namelen
);
2102 * Release the btree blocks and leaf block.
2104 for (i
= 0; i
< state
->path
.active
; i
++) {
2105 xfs_trans_brelse(args
->trans
, state
->path
.blk
[i
].bp
);
2106 state
->path
.blk
[i
].bp
= NULL
;
2109 * Release the data block if we have it.
2111 if (state
->extravalid
&& state
->extrablk
.bp
) {
2112 xfs_trans_brelse(args
->trans
, state
->extrablk
.bp
);
2113 state
->extrablk
.bp
= NULL
;
2115 xfs_da_state_free(state
);
2120 * Remove an entry from a node-format directory.
2123 xfs_dir2_node_removename(
2124 struct xfs_da_args
*args
) /* operation arguments */
2126 struct xfs_da_state_blk
*blk
; /* leaf block */
2127 int error
; /* error return value */
2128 int rval
; /* operation return value */
2129 struct xfs_da_state
*state
; /* btree cursor */
2131 trace_xfs_dir2_node_removename(args
);
2134 * Allocate and initialize the btree cursor.
2136 state
= xfs_da_state_alloc();
2138 state
->mp
= args
->dp
->i_mount
;
2140 /* Look up the entry we're deleting, set up the cursor. */
2141 error
= xfs_da3_node_lookup_int(state
, &rval
);
2145 /* Didn't find it, upper layer screwed up. */
2146 if (rval
!= -EEXIST
) {
2151 blk
= &state
->path
.blk
[state
->path
.active
- 1];
2152 ASSERT(blk
->magic
== XFS_DIR2_LEAFN_MAGIC
);
2153 ASSERT(state
->extravalid
);
2155 * Remove the leaf and data entries.
2156 * Extrablk refers to the data block.
2158 error
= xfs_dir2_leafn_remove(args
, blk
->bp
, blk
->index
,
2159 &state
->extrablk
, &rval
);
2163 * Fix the hash values up the btree.
2165 xfs_da3_fixhashpath(state
, &state
->path
);
2167 * If we need to join leaf blocks, do it.
2169 if (rval
&& state
->path
.active
> 1)
2170 error
= xfs_da3_join(state
);
2172 * If no errors so far, try conversion to leaf format.
2175 error
= xfs_dir2_node_to_leaf(state
);
2177 xfs_da_state_free(state
);
2182 * Replace an entry's inode number in a node-format directory.
2185 xfs_dir2_node_replace(
2186 xfs_da_args_t
*args
) /* operation arguments */
2188 xfs_da_state_blk_t
*blk
; /* leaf block */
2189 xfs_dir2_data_hdr_t
*hdr
; /* data block header */
2190 xfs_dir2_data_entry_t
*dep
; /* data entry changed */
2191 int error
; /* error return value */
2192 int i
; /* btree level */
2193 xfs_ino_t inum
; /* new inode number */
2194 int ftype
; /* new file type */
2195 int rval
; /* internal return value */
2196 xfs_da_state_t
*state
; /* btree cursor */
2198 trace_xfs_dir2_node_replace(args
);
2201 * Allocate and initialize the btree cursor.
2203 state
= xfs_da_state_alloc();
2205 state
->mp
= args
->dp
->i_mount
;
2208 * We have to save new inode number and ftype since
2209 * xfs_da3_node_lookup_int() is going to overwrite them
2211 inum
= args
->inumber
;
2212 ftype
= args
->filetype
;
2215 * Lookup the entry to change in the btree.
2217 error
= xfs_da3_node_lookup_int(state
, &rval
);
2222 * It should be found, since the vnodeops layer has looked it up
2223 * and locked it. But paranoia is good.
2225 if (rval
== -EEXIST
) {
2226 struct xfs_dir3_icleaf_hdr leafhdr
;
2229 * Find the leaf entry.
2231 blk
= &state
->path
.blk
[state
->path
.active
- 1];
2232 ASSERT(blk
->magic
== XFS_DIR2_LEAFN_MAGIC
);
2233 ASSERT(state
->extravalid
);
2235 xfs_dir2_leaf_hdr_from_disk(state
->mp
, &leafhdr
,
2238 * Point to the data entry.
2240 hdr
= state
->extrablk
.bp
->b_addr
;
2241 ASSERT(hdr
->magic
== cpu_to_be32(XFS_DIR2_DATA_MAGIC
) ||
2242 hdr
->magic
== cpu_to_be32(XFS_DIR3_DATA_MAGIC
));
2243 dep
= (xfs_dir2_data_entry_t
*)
2245 xfs_dir2_dataptr_to_off(args
->geo
,
2246 be32_to_cpu(leafhdr
.ents
[blk
->index
].address
)));
2247 ASSERT(inum
!= be64_to_cpu(dep
->inumber
));
2249 * Fill in the new inode number and log the entry.
2251 dep
->inumber
= cpu_to_be64(inum
);
2252 xfs_dir2_data_put_ftype(state
->mp
, dep
, ftype
);
2253 xfs_dir2_data_log_entry(args
, state
->extrablk
.bp
, dep
);
2257 * Didn't find it, and we're holding a data block. Drop it.
2259 else if (state
->extravalid
) {
2260 xfs_trans_brelse(args
->trans
, state
->extrablk
.bp
);
2261 state
->extrablk
.bp
= NULL
;
2264 * Release all the buffers in the cursor.
2266 for (i
= 0; i
< state
->path
.active
; i
++) {
2267 xfs_trans_brelse(args
->trans
, state
->path
.blk
[i
].bp
);
2268 state
->path
.blk
[i
].bp
= NULL
;
2270 xfs_da_state_free(state
);
2275 * Trim off a trailing empty freespace block.
2276 * Return (in rvalp) 1 if we did it, 0 if not.
2279 xfs_dir2_node_trim_free(
2280 xfs_da_args_t
*args
, /* operation arguments */
2281 xfs_fileoff_t fo
, /* free block number */
2282 int *rvalp
) /* out: did something */
2284 struct xfs_buf
*bp
; /* freespace buffer */
2285 xfs_inode_t
*dp
; /* incore directory inode */
2286 int error
; /* error return code */
2287 xfs_dir2_free_t
*free
; /* freespace structure */
2288 xfs_trans_t
*tp
; /* transaction pointer */
2289 struct xfs_dir3_icfree_hdr freehdr
;
2297 * Read the freespace block.
2299 error
= xfs_dir2_free_try_read(tp
, dp
, fo
, &bp
);
2303 * There can be holes in freespace. If fo is a hole, there's
2309 xfs_dir2_free_hdr_from_disk(dp
->i_mount
, &freehdr
, free
);
2312 * If there are used entries, there's nothing to do.
2314 if (freehdr
.nused
> 0) {
2315 xfs_trans_brelse(tp
, bp
);
2319 * Blow the block away.
2321 error
= xfs_dir2_shrink_inode(args
,
2322 xfs_dir2_da_to_db(args
->geo
, (xfs_dablk_t
)fo
), bp
);
2325 * Can't fail with ENOSPC since that only happens with no
2326 * space reservation, when breaking up an extent into two
2327 * pieces. This is the last block of an extent.
2329 ASSERT(error
!= -ENOSPC
);
2330 xfs_trans_brelse(tp
, bp
);
2334 * Return that we succeeded.