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"
20 #include "xfs_health.h"
23 * Function declarations.
25 static int xfs_dir2_leafn_add(struct xfs_buf
*bp
, xfs_da_args_t
*args
,
27 static void xfs_dir2_leafn_rebalance(xfs_da_state_t
*state
,
28 xfs_da_state_blk_t
*blk1
,
29 xfs_da_state_blk_t
*blk2
);
30 static int xfs_dir2_leafn_remove(xfs_da_args_t
*args
, struct xfs_buf
*bp
,
31 int index
, xfs_da_state_blk_t
*dblk
,
35 * Convert data space db to the corresponding free db.
38 xfs_dir2_db_to_fdb(struct xfs_da_geometry
*geo
, xfs_dir2_db_t db
)
40 return xfs_dir2_byte_to_db(geo
, XFS_DIR2_FREE_OFFSET
) +
41 (db
/ geo
->free_max_bests
);
45 * Convert data space db to the corresponding index in a free db.
48 xfs_dir2_db_to_fdindex(struct xfs_da_geometry
*geo
, xfs_dir2_db_t db
)
50 return db
% geo
->free_max_bests
;
54 * Check internal consistency of a leafn block.
62 struct xfs_dir2_leaf
*leaf
= bp
->b_addr
;
63 struct xfs_dir3_icleaf_hdr leafhdr
;
65 xfs_dir2_leaf_hdr_from_disk(dp
->i_mount
, &leafhdr
, leaf
);
67 if (leafhdr
.magic
== XFS_DIR3_LEAFN_MAGIC
) {
68 struct xfs_dir3_leaf_hdr
*leaf3
= bp
->b_addr
;
69 if (be64_to_cpu(leaf3
->info
.blkno
) != xfs_buf_daddr(bp
))
70 return __this_address
;
71 } else if (leafhdr
.magic
!= XFS_DIR2_LEAFN_MAGIC
)
72 return __this_address
;
74 return xfs_dir3_leaf_check_int(dp
->i_mount
, &leafhdr
, leaf
, false);
84 fa
= xfs_dir3_leafn_check(dp
, bp
);
87 xfs_corruption_error(__func__
, XFS_ERRLEVEL_LOW
, dp
->i_mount
,
88 bp
->b_addr
, BBTOB(bp
->b_length
), __FILE__
, __LINE__
,
93 #define xfs_dir3_leaf_check(dp, bp)
100 struct xfs_mount
*mp
= bp
->b_mount
;
101 struct xfs_dir2_free_hdr
*hdr
= bp
->b_addr
;
103 if (!xfs_verify_magic(bp
, hdr
->magic
))
104 return __this_address
;
106 if (xfs_has_crc(mp
)) {
107 struct xfs_dir3_blk_hdr
*hdr3
= bp
->b_addr
;
109 if (!uuid_equal(&hdr3
->uuid
, &mp
->m_sb
.sb_meta_uuid
))
110 return __this_address
;
111 if (be64_to_cpu(hdr3
->blkno
) != xfs_buf_daddr(bp
))
112 return __this_address
;
113 if (!xfs_log_check_lsn(mp
, be64_to_cpu(hdr3
->lsn
)))
114 return __this_address
;
117 /* XXX: should bounds check the xfs_dir3_icfree_hdr here */
123 xfs_dir3_free_read_verify(
126 struct xfs_mount
*mp
= bp
->b_mount
;
129 if (xfs_has_crc(mp
) &&
130 !xfs_buf_verify_cksum(bp
, XFS_DIR3_FREE_CRC_OFF
))
131 xfs_verifier_error(bp
, -EFSBADCRC
, __this_address
);
133 fa
= xfs_dir3_free_verify(bp
);
135 xfs_verifier_error(bp
, -EFSCORRUPTED
, fa
);
140 xfs_dir3_free_write_verify(
143 struct xfs_mount
*mp
= bp
->b_mount
;
144 struct xfs_buf_log_item
*bip
= bp
->b_log_item
;
145 struct xfs_dir3_blk_hdr
*hdr3
= bp
->b_addr
;
148 fa
= xfs_dir3_free_verify(bp
);
150 xfs_verifier_error(bp
, -EFSCORRUPTED
, fa
);
154 if (!xfs_has_crc(mp
))
158 hdr3
->lsn
= cpu_to_be64(bip
->bli_item
.li_lsn
);
160 xfs_buf_update_cksum(bp
, XFS_DIR3_FREE_CRC_OFF
);
163 const struct xfs_buf_ops xfs_dir3_free_buf_ops
= {
164 .name
= "xfs_dir3_free",
165 .magic
= { cpu_to_be32(XFS_DIR2_FREE_MAGIC
),
166 cpu_to_be32(XFS_DIR3_FREE_MAGIC
) },
167 .verify_read
= xfs_dir3_free_read_verify
,
168 .verify_write
= xfs_dir3_free_write_verify
,
169 .verify_struct
= xfs_dir3_free_verify
,
172 /* Everything ok in the free block header? */
173 static xfs_failaddr_t
174 xfs_dir3_free_header_check(
179 struct xfs_mount
*mp
= bp
->b_mount
;
180 int maxbests
= mp
->m_dir_geo
->free_max_bests
;
181 unsigned int firstdb
;
183 firstdb
= (xfs_dir2_da_to_db(mp
->m_dir_geo
, fbno
) -
184 xfs_dir2_byte_to_db(mp
->m_dir_geo
, XFS_DIR2_FREE_OFFSET
)) *
186 if (xfs_has_crc(mp
)) {
187 struct xfs_dir3_free_hdr
*hdr3
= bp
->b_addr
;
189 if (be32_to_cpu(hdr3
->firstdb
) != firstdb
)
190 return __this_address
;
191 if (be32_to_cpu(hdr3
->nvalid
) > maxbests
)
192 return __this_address
;
193 if (be32_to_cpu(hdr3
->nvalid
) < be32_to_cpu(hdr3
->nused
))
194 return __this_address
;
195 if (be64_to_cpu(hdr3
->hdr
.owner
) != owner
)
196 return __this_address
;
198 struct xfs_dir2_free_hdr
*hdr
= bp
->b_addr
;
200 if (be32_to_cpu(hdr
->firstdb
) != firstdb
)
201 return __this_address
;
202 if (be32_to_cpu(hdr
->nvalid
) > maxbests
)
203 return __this_address
;
204 if (be32_to_cpu(hdr
->nvalid
) < be32_to_cpu(hdr
->nused
))
205 return __this_address
;
211 __xfs_dir3_free_read(
212 struct xfs_trans
*tp
,
213 struct xfs_inode
*dp
,
217 struct xfs_buf
**bpp
)
222 err
= xfs_da_read_buf(tp
, dp
, fbno
, flags
, bpp
, XFS_DATA_FORK
,
223 &xfs_dir3_free_buf_ops
);
227 /* Check things that we can't do in the verifier. */
228 fa
= xfs_dir3_free_header_check(*bpp
, owner
, fbno
);
230 __xfs_buf_mark_corrupt(*bpp
, fa
);
231 xfs_trans_brelse(tp
, *bpp
);
233 xfs_dirattr_mark_sick(dp
, XFS_DATA_FORK
);
234 return -EFSCORRUPTED
;
237 /* try read returns without an error or *bpp if it lands in a hole */
239 xfs_trans_buf_set_type(tp
, *bpp
, XFS_BLFT_DIR_FREE_BUF
);
245 xfs_dir2_free_hdr_from_disk(
246 struct xfs_mount
*mp
,
247 struct xfs_dir3_icfree_hdr
*to
,
248 struct xfs_dir2_free
*from
)
250 if (xfs_has_crc(mp
)) {
251 struct xfs_dir3_free
*from3
= (struct xfs_dir3_free
*)from
;
253 to
->magic
= be32_to_cpu(from3
->hdr
.hdr
.magic
);
254 to
->firstdb
= be32_to_cpu(from3
->hdr
.firstdb
);
255 to
->nvalid
= be32_to_cpu(from3
->hdr
.nvalid
);
256 to
->nused
= be32_to_cpu(from3
->hdr
.nused
);
257 to
->bests
= from3
->bests
;
259 ASSERT(to
->magic
== XFS_DIR3_FREE_MAGIC
);
261 to
->magic
= be32_to_cpu(from
->hdr
.magic
);
262 to
->firstdb
= be32_to_cpu(from
->hdr
.firstdb
);
263 to
->nvalid
= be32_to_cpu(from
->hdr
.nvalid
);
264 to
->nused
= be32_to_cpu(from
->hdr
.nused
);
265 to
->bests
= from
->bests
;
267 ASSERT(to
->magic
== XFS_DIR2_FREE_MAGIC
);
272 xfs_dir2_free_hdr_to_disk(
273 struct xfs_mount
*mp
,
274 struct xfs_dir2_free
*to
,
275 struct xfs_dir3_icfree_hdr
*from
)
277 if (xfs_has_crc(mp
)) {
278 struct xfs_dir3_free
*to3
= (struct xfs_dir3_free
*)to
;
280 ASSERT(from
->magic
== XFS_DIR3_FREE_MAGIC
);
282 to3
->hdr
.hdr
.magic
= cpu_to_be32(from
->magic
);
283 to3
->hdr
.firstdb
= cpu_to_be32(from
->firstdb
);
284 to3
->hdr
.nvalid
= cpu_to_be32(from
->nvalid
);
285 to3
->hdr
.nused
= cpu_to_be32(from
->nused
);
287 ASSERT(from
->magic
== XFS_DIR2_FREE_MAGIC
);
289 to
->hdr
.magic
= cpu_to_be32(from
->magic
);
290 to
->hdr
.firstdb
= cpu_to_be32(from
->firstdb
);
291 to
->hdr
.nvalid
= cpu_to_be32(from
->nvalid
);
292 to
->hdr
.nused
= cpu_to_be32(from
->nused
);
298 struct xfs_trans
*tp
,
299 struct xfs_inode
*dp
,
302 struct xfs_buf
**bpp
)
304 return __xfs_dir3_free_read(tp
, dp
, owner
, fbno
, 0, bpp
);
308 xfs_dir2_free_try_read(
309 struct xfs_trans
*tp
,
310 struct xfs_inode
*dp
,
313 struct xfs_buf
**bpp
)
315 return __xfs_dir3_free_read(tp
, dp
, owner
, fbno
, XFS_DABUF_MAP_HOLE_OK
,
320 xfs_dir3_free_get_buf(
323 struct xfs_buf
**bpp
)
325 struct xfs_trans
*tp
= args
->trans
;
326 struct xfs_inode
*dp
= args
->dp
;
327 struct xfs_mount
*mp
= dp
->i_mount
;
330 struct xfs_dir3_icfree_hdr hdr
;
332 error
= xfs_da_get_buf(tp
, dp
, xfs_dir2_db_to_da(args
->geo
, fbno
),
337 xfs_trans_buf_set_type(tp
, bp
, XFS_BLFT_DIR_FREE_BUF
);
338 bp
->b_ops
= &xfs_dir3_free_buf_ops
;
341 * Initialize the new block to be empty, and remember
342 * its first slot as our empty slot.
344 memset(bp
->b_addr
, 0, sizeof(struct xfs_dir3_free_hdr
));
345 memset(&hdr
, 0, sizeof(hdr
));
347 if (xfs_has_crc(mp
)) {
348 struct xfs_dir3_free_hdr
*hdr3
= bp
->b_addr
;
350 hdr
.magic
= XFS_DIR3_FREE_MAGIC
;
352 hdr3
->hdr
.blkno
= cpu_to_be64(xfs_buf_daddr(bp
));
353 hdr3
->hdr
.owner
= cpu_to_be64(args
->owner
);
354 uuid_copy(&hdr3
->hdr
.uuid
, &mp
->m_sb
.sb_meta_uuid
);
356 hdr
.magic
= XFS_DIR2_FREE_MAGIC
;
357 xfs_dir2_free_hdr_to_disk(mp
, bp
->b_addr
, &hdr
);
363 * Log entries from a freespace block.
366 xfs_dir2_free_log_bests(
367 struct xfs_da_args
*args
,
368 struct xfs_dir3_icfree_hdr
*hdr
,
370 int first
, /* first entry to log */
371 int last
) /* last entry to log */
373 struct xfs_dir2_free
*free
= bp
->b_addr
;
375 ASSERT(free
->hdr
.magic
== cpu_to_be32(XFS_DIR2_FREE_MAGIC
) ||
376 free
->hdr
.magic
== cpu_to_be32(XFS_DIR3_FREE_MAGIC
));
377 xfs_trans_log_buf(args
->trans
, bp
,
378 (char *)&hdr
->bests
[first
] - (char *)free
,
379 (char *)&hdr
->bests
[last
] - (char *)free
+
380 sizeof(hdr
->bests
[0]) - 1);
384 * Log header from a freespace block.
387 xfs_dir2_free_log_header(
388 struct xfs_da_args
*args
,
392 xfs_dir2_free_t
*free
; /* freespace structure */
395 ASSERT(free
->hdr
.magic
== cpu_to_be32(XFS_DIR2_FREE_MAGIC
) ||
396 free
->hdr
.magic
== cpu_to_be32(XFS_DIR3_FREE_MAGIC
));
398 xfs_trans_log_buf(args
->trans
, bp
, 0,
399 args
->geo
->free_hdr_size
- 1);
403 * Convert a leaf-format directory to a node-format directory.
404 * We need to change the magic number of the leaf block, and copy
405 * the freespace table out of the leaf block into its own block.
408 xfs_dir2_leaf_to_node(
409 xfs_da_args_t
*args
, /* operation arguments */
410 struct xfs_buf
*lbp
) /* leaf buffer */
412 xfs_inode_t
*dp
; /* incore directory inode */
413 int error
; /* error return value */
414 struct xfs_buf
*fbp
; /* freespace buffer */
415 xfs_dir2_db_t fdb
; /* freespace block number */
416 __be16
*from
; /* pointer to freespace entry */
417 int i
; /* leaf freespace index */
418 xfs_dir2_leaf_t
*leaf
; /* leaf structure */
419 xfs_dir2_leaf_tail_t
*ltp
; /* leaf tail structure */
420 int n
; /* count of live freespc ents */
421 xfs_dir2_data_off_t off
; /* freespace entry value */
422 xfs_trans_t
*tp
; /* transaction pointer */
423 struct xfs_dir3_icfree_hdr freehdr
;
425 trace_xfs_dir2_leaf_to_node(args
);
430 * Add a freespace block to the directory.
432 if ((error
= xfs_dir2_grow_inode(args
, XFS_DIR2_FREE_SPACE
, &fdb
))) {
435 ASSERT(fdb
== xfs_dir2_byte_to_db(args
->geo
, XFS_DIR2_FREE_OFFSET
));
437 * Get the buffer for the new freespace block.
439 error
= xfs_dir3_free_get_buf(args
, fdb
, &fbp
);
443 xfs_dir2_free_hdr_from_disk(dp
->i_mount
, &freehdr
, fbp
->b_addr
);
445 ltp
= xfs_dir2_leaf_tail_p(args
->geo
, leaf
);
446 if (be32_to_cpu(ltp
->bestcount
) >
447 (uint
)dp
->i_disk_size
/ args
->geo
->blksize
) {
448 xfs_buf_mark_corrupt(lbp
);
449 xfs_da_mark_sick(args
);
450 return -EFSCORRUPTED
;
454 * Copy freespace entries from the leaf block to the new block.
455 * Count active entries.
457 from
= xfs_dir2_leaf_bests_p(ltp
);
458 for (i
= n
= 0; i
< be32_to_cpu(ltp
->bestcount
); i
++, from
++) {
459 off
= be16_to_cpu(*from
);
460 if (off
!= NULLDATAOFF
)
462 freehdr
.bests
[i
] = cpu_to_be16(off
);
466 * Now initialize the freespace block header.
469 freehdr
.nvalid
= be32_to_cpu(ltp
->bestcount
);
471 xfs_dir2_free_hdr_to_disk(dp
->i_mount
, fbp
->b_addr
, &freehdr
);
472 xfs_dir2_free_log_bests(args
, &freehdr
, fbp
, 0, freehdr
.nvalid
- 1);
473 xfs_dir2_free_log_header(args
, fbp
);
476 * Converting the leaf to a leafnode is just a matter of changing the
477 * magic number and the ops. Do the change directly to the buffer as
478 * it's less work (and less code) than decoding the header to host
479 * format and back again.
481 if (leaf
->hdr
.info
.magic
== cpu_to_be16(XFS_DIR2_LEAF1_MAGIC
))
482 leaf
->hdr
.info
.magic
= cpu_to_be16(XFS_DIR2_LEAFN_MAGIC
);
484 leaf
->hdr
.info
.magic
= cpu_to_be16(XFS_DIR3_LEAFN_MAGIC
);
485 lbp
->b_ops
= &xfs_dir3_leafn_buf_ops
;
486 xfs_trans_buf_set_type(tp
, lbp
, XFS_BLFT_DIR_LEAFN_BUF
);
487 xfs_dir3_leaf_log_header(args
, lbp
);
488 xfs_dir3_leaf_check(dp
, lbp
);
493 * Add a leaf entry to a leaf block in a node-form directory.
494 * The other work necessary is done from the caller.
496 static int /* error */
498 struct xfs_buf
*bp
, /* leaf buffer */
499 struct xfs_da_args
*args
, /* operation arguments */
500 int index
) /* insertion pt for new entry */
502 struct xfs_dir3_icleaf_hdr leafhdr
;
503 struct xfs_inode
*dp
= args
->dp
;
504 struct xfs_dir2_leaf
*leaf
= bp
->b_addr
;
505 struct xfs_dir2_leaf_entry
*lep
;
506 struct xfs_dir2_leaf_entry
*ents
;
507 int compact
; /* compacting stale leaves */
508 int highstale
= 0; /* next stale entry */
509 int lfloghigh
; /* high leaf entry logging */
510 int lfloglow
; /* low leaf entry logging */
511 int lowstale
= 0; /* previous stale entry */
513 trace_xfs_dir2_leafn_add(args
, index
);
515 xfs_dir2_leaf_hdr_from_disk(dp
->i_mount
, &leafhdr
, leaf
);
519 * Quick check just to make sure we are not going to index
520 * into other peoples memory
523 xfs_buf_mark_corrupt(bp
);
524 xfs_da_mark_sick(args
);
525 return -EFSCORRUPTED
;
529 * If there are already the maximum number of leaf entries in
530 * the block, if there are no stale entries it won't fit.
531 * Caller will do a split. If there are stale entries we'll do
535 if (leafhdr
.count
== args
->geo
->leaf_max_ents
) {
538 compact
= leafhdr
.stale
> 1;
541 ASSERT(index
== 0 || be32_to_cpu(ents
[index
- 1].hashval
) <= args
->hashval
);
542 ASSERT(index
== leafhdr
.count
||
543 be32_to_cpu(ents
[index
].hashval
) >= args
->hashval
);
545 if (args
->op_flags
& XFS_DA_OP_JUSTCHECK
)
549 * Compact out all but one stale leaf entry. Leaves behind
550 * the entry closest to index.
553 xfs_dir3_leaf_compact_x1(&leafhdr
, ents
, &index
, &lowstale
,
554 &highstale
, &lfloglow
, &lfloghigh
);
555 else if (leafhdr
.stale
) {
557 * Set impossible logging indices for this case.
559 lfloglow
= leafhdr
.count
;
564 * Insert the new entry, log everything.
566 lep
= xfs_dir3_leaf_find_entry(&leafhdr
, ents
, index
, compact
, lowstale
,
567 highstale
, &lfloglow
, &lfloghigh
);
569 lep
->hashval
= cpu_to_be32(args
->hashval
);
570 lep
->address
= cpu_to_be32(xfs_dir2_db_off_to_dataptr(args
->geo
,
571 args
->blkno
, args
->index
));
573 xfs_dir2_leaf_hdr_to_disk(dp
->i_mount
, leaf
, &leafhdr
);
574 xfs_dir3_leaf_log_header(args
, bp
);
575 xfs_dir3_leaf_log_ents(args
, &leafhdr
, bp
, lfloglow
, lfloghigh
);
576 xfs_dir3_leaf_check(dp
, bp
);
582 xfs_dir2_free_hdr_check(
583 struct xfs_inode
*dp
,
587 struct xfs_dir3_icfree_hdr hdr
;
589 xfs_dir2_free_hdr_from_disk(dp
->i_mount
, &hdr
, bp
->b_addr
);
591 ASSERT((hdr
.firstdb
% dp
->i_mount
->m_dir_geo
->free_max_bests
) == 0);
592 ASSERT(hdr
.firstdb
<= db
);
593 ASSERT(db
< hdr
.firstdb
+ hdr
.nvalid
);
596 #define xfs_dir2_free_hdr_check(dp, bp, db)
600 * Return the last hash value in the leaf.
601 * Stale entries are ok.
603 xfs_dahash_t
/* hash value */
604 xfs_dir2_leaf_lasthash(
605 struct xfs_inode
*dp
,
606 struct xfs_buf
*bp
, /* leaf buffer */
607 int *count
) /* count of entries in leaf */
609 struct xfs_dir3_icleaf_hdr leafhdr
;
611 xfs_dir2_leaf_hdr_from_disk(dp
->i_mount
, &leafhdr
, bp
->b_addr
);
613 ASSERT(leafhdr
.magic
== XFS_DIR2_LEAFN_MAGIC
||
614 leafhdr
.magic
== XFS_DIR3_LEAFN_MAGIC
||
615 leafhdr
.magic
== XFS_DIR2_LEAF1_MAGIC
||
616 leafhdr
.magic
== XFS_DIR3_LEAF1_MAGIC
);
619 *count
= leafhdr
.count
;
622 return be32_to_cpu(leafhdr
.ents
[leafhdr
.count
- 1].hashval
);
626 * Look up a leaf entry for space to add a name in a node-format leaf block.
627 * The extrablk in state is a freespace block.
630 xfs_dir2_leafn_lookup_for_addname(
631 struct xfs_buf
*bp
, /* leaf buffer */
632 xfs_da_args_t
*args
, /* operation arguments */
633 int *indexp
, /* out: leaf entry index */
634 xfs_da_state_t
*state
) /* state to fill in */
636 struct xfs_buf
*curbp
= NULL
; /* current data/free buffer */
637 xfs_dir2_db_t curdb
= -1; /* current data block number */
638 xfs_dir2_db_t curfdb
= -1; /* current free block number */
639 xfs_inode_t
*dp
; /* incore directory inode */
640 int error
; /* error return value */
641 int fi
; /* free entry index */
642 xfs_dir2_free_t
*free
= NULL
; /* free block structure */
643 int index
; /* leaf entry index */
644 xfs_dir2_leaf_t
*leaf
; /* leaf structure */
645 int length
; /* length of new data entry */
646 xfs_dir2_leaf_entry_t
*lep
; /* leaf entry */
647 xfs_mount_t
*mp
; /* filesystem mount point */
648 xfs_dir2_db_t newdb
; /* new data block number */
649 xfs_dir2_db_t newfdb
; /* new free block number */
650 xfs_trans_t
*tp
; /* transaction pointer */
651 struct xfs_dir3_icleaf_hdr leafhdr
;
657 xfs_dir2_leaf_hdr_from_disk(mp
, &leafhdr
, leaf
);
659 xfs_dir3_leaf_check(dp
, bp
);
660 ASSERT(leafhdr
.count
> 0);
663 * Look up the hash value in the leaf entries.
665 index
= xfs_dir2_leaf_search_hash(args
, bp
);
667 * Do we have a buffer coming in?
669 if (state
->extravalid
) {
670 /* If so, it's a free block buffer, get the block number. */
671 curbp
= state
->extrablk
.bp
;
672 curfdb
= state
->extrablk
.blkno
;
673 free
= curbp
->b_addr
;
674 ASSERT(free
->hdr
.magic
== cpu_to_be32(XFS_DIR2_FREE_MAGIC
) ||
675 free
->hdr
.magic
== cpu_to_be32(XFS_DIR3_FREE_MAGIC
));
677 length
= xfs_dir2_data_entsize(mp
, args
->namelen
);
679 * Loop over leaf entries with the right hash value.
681 for (lep
= &leafhdr
.ents
[index
];
682 index
< leafhdr
.count
&& be32_to_cpu(lep
->hashval
) == args
->hashval
;
685 * Skip stale leaf entries.
687 if (be32_to_cpu(lep
->address
) == XFS_DIR2_NULL_DATAPTR
)
690 * Pull the data block number from the entry.
692 newdb
= xfs_dir2_dataptr_to_db(args
->geo
,
693 be32_to_cpu(lep
->address
));
695 * For addname, we're looking for a place to put the new entry.
696 * We want to use a data block with an entry of equal
697 * hash value to ours if there is one with room.
699 * If this block isn't the data block we already have
700 * in hand, take a look at it.
702 if (newdb
!= curdb
) {
703 struct xfs_dir3_icfree_hdr freehdr
;
707 * Convert the data block to the free block
708 * holding its freespace information.
710 newfdb
= xfs_dir2_db_to_fdb(args
->geo
, newdb
);
712 * If it's not the one we have in hand, read it in.
714 if (newfdb
!= curfdb
) {
716 * If we had one before, drop it.
719 xfs_trans_brelse(tp
, curbp
);
721 error
= xfs_dir2_free_read(tp
, dp
, args
->owner
,
722 xfs_dir2_db_to_da(args
->geo
,
727 free
= curbp
->b_addr
;
729 xfs_dir2_free_hdr_check(dp
, curbp
, curdb
);
732 * Get the index for our entry.
734 fi
= xfs_dir2_db_to_fdindex(args
->geo
, curdb
);
736 * If it has room, return it.
738 xfs_dir2_free_hdr_from_disk(mp
, &freehdr
, free
);
739 if (XFS_IS_CORRUPT(mp
,
741 cpu_to_be16(NULLDATAOFF
))) {
742 if (curfdb
!= newfdb
)
743 xfs_trans_brelse(tp
, curbp
);
744 xfs_da_mark_sick(args
);
745 return -EFSCORRUPTED
;
748 if (be16_to_cpu(freehdr
.bests
[fi
]) >= length
)
752 /* Didn't find any space */
755 ASSERT(args
->op_flags
& XFS_DA_OP_OKNOENT
);
757 /* Giving back a free block. */
758 state
->extravalid
= 1;
759 state
->extrablk
.bp
= curbp
;
760 state
->extrablk
.index
= fi
;
761 state
->extrablk
.blkno
= curfdb
;
764 * Important: this magic number is not in the buffer - it's for
765 * buffer type information and therefore only the free/data type
766 * matters here, not whether CRCs are enabled or not.
768 state
->extrablk
.magic
= XFS_DIR2_FREE_MAGIC
;
770 state
->extravalid
= 0;
773 * Return the index, that will be the insertion point.
780 * Look up a leaf entry in a node-format leaf block.
781 * The extrablk in state a data block.
784 xfs_dir2_leafn_lookup_for_entry(
785 struct xfs_buf
*bp
, /* leaf buffer */
786 xfs_da_args_t
*args
, /* operation arguments */
787 int *indexp
, /* out: leaf entry index */
788 xfs_da_state_t
*state
) /* state to fill in */
790 struct xfs_buf
*curbp
= NULL
; /* current data/free buffer */
791 xfs_dir2_db_t curdb
= -1; /* current data block number */
792 xfs_dir2_data_entry_t
*dep
; /* data block entry */
793 xfs_inode_t
*dp
; /* incore directory inode */
794 int error
; /* error return value */
795 int index
; /* leaf entry index */
796 xfs_dir2_leaf_t
*leaf
; /* leaf structure */
797 xfs_dir2_leaf_entry_t
*lep
; /* leaf entry */
798 xfs_mount_t
*mp
; /* filesystem mount point */
799 xfs_dir2_db_t newdb
; /* new data block number */
800 xfs_trans_t
*tp
; /* transaction pointer */
801 enum xfs_dacmp cmp
; /* comparison result */
802 struct xfs_dir3_icleaf_hdr leafhdr
;
808 xfs_dir2_leaf_hdr_from_disk(mp
, &leafhdr
, leaf
);
810 xfs_dir3_leaf_check(dp
, bp
);
811 if (leafhdr
.count
<= 0) {
812 xfs_buf_mark_corrupt(bp
);
813 xfs_da_mark_sick(args
);
814 return -EFSCORRUPTED
;
818 * Look up the hash value in the leaf entries.
820 index
= xfs_dir2_leaf_search_hash(args
, bp
);
822 * Do we have a buffer coming in?
824 if (state
->extravalid
) {
825 curbp
= state
->extrablk
.bp
;
826 curdb
= state
->extrablk
.blkno
;
829 * Loop over leaf entries with the right hash value.
831 for (lep
= &leafhdr
.ents
[index
];
832 index
< leafhdr
.count
&& be32_to_cpu(lep
->hashval
) == args
->hashval
;
835 * Skip stale leaf entries.
837 if (be32_to_cpu(lep
->address
) == XFS_DIR2_NULL_DATAPTR
)
840 * Pull the data block number from the entry.
842 newdb
= xfs_dir2_dataptr_to_db(args
->geo
,
843 be32_to_cpu(lep
->address
));
845 * Not adding a new entry, so we really want to find
846 * the name given to us.
848 * If it's a different data block, go get it.
850 if (newdb
!= curdb
) {
852 * If we had a block before that we aren't saving
853 * for a CI name, drop it
855 if (curbp
&& (args
->cmpresult
== XFS_CMP_DIFFERENT
||
856 curdb
!= state
->extrablk
.blkno
))
857 xfs_trans_brelse(tp
, curbp
);
859 * If needing the block that is saved with a CI match,
860 * use it otherwise read in the new data block.
862 if (args
->cmpresult
!= XFS_CMP_DIFFERENT
&&
863 newdb
== state
->extrablk
.blkno
) {
864 ASSERT(state
->extravalid
);
865 curbp
= state
->extrablk
.bp
;
867 error
= xfs_dir3_data_read(tp
, dp
, args
->owner
,
868 xfs_dir2_db_to_da(args
->geo
,
874 xfs_dir3_data_check(dp
, curbp
);
878 * Point to the data entry.
880 dep
= (xfs_dir2_data_entry_t
*)((char *)curbp
->b_addr
+
881 xfs_dir2_dataptr_to_off(args
->geo
,
882 be32_to_cpu(lep
->address
)));
884 * Compare the entry and if it's an exact match, return
885 * EEXIST immediately. If it's the first case-insensitive
886 * match, store the block & inode number and continue looking.
888 cmp
= xfs_dir2_compname(args
, dep
->name
, dep
->namelen
);
889 if (cmp
!= XFS_CMP_DIFFERENT
&& cmp
!= args
->cmpresult
) {
890 /* If there is a CI match block, drop it */
891 if (args
->cmpresult
!= XFS_CMP_DIFFERENT
&&
892 curdb
!= state
->extrablk
.blkno
)
893 xfs_trans_brelse(tp
, state
->extrablk
.bp
);
894 args
->cmpresult
= cmp
;
895 args
->inumber
= be64_to_cpu(dep
->inumber
);
896 args
->filetype
= xfs_dir2_data_get_ftype(mp
, dep
);
898 state
->extravalid
= 1;
899 state
->extrablk
.bp
= curbp
;
900 state
->extrablk
.blkno
= curdb
;
901 state
->extrablk
.index
= (int)((char *)dep
-
902 (char *)curbp
->b_addr
);
903 state
->extrablk
.magic
= XFS_DIR2_DATA_MAGIC
;
904 curbp
->b_ops
= &xfs_dir3_data_buf_ops
;
905 xfs_trans_buf_set_type(tp
, curbp
, XFS_BLFT_DIR_DATA_BUF
);
906 if (cmp
== XFS_CMP_EXACT
)
910 ASSERT(index
== leafhdr
.count
|| (args
->op_flags
& XFS_DA_OP_OKNOENT
));
912 if (args
->cmpresult
== XFS_CMP_DIFFERENT
) {
913 /* Giving back last used data block. */
914 state
->extravalid
= 1;
915 state
->extrablk
.bp
= curbp
;
916 state
->extrablk
.index
= -1;
917 state
->extrablk
.blkno
= curdb
;
918 state
->extrablk
.magic
= XFS_DIR2_DATA_MAGIC
;
919 curbp
->b_ops
= &xfs_dir3_data_buf_ops
;
920 xfs_trans_buf_set_type(tp
, curbp
, XFS_BLFT_DIR_DATA_BUF
);
922 /* If the curbp is not the CI match block, drop it */
923 if (state
->extrablk
.bp
!= curbp
)
924 xfs_trans_brelse(tp
, curbp
);
927 state
->extravalid
= 0;
934 * Look up a leaf entry in a node-format leaf block.
935 * If this is an addname then the extrablk in state is a freespace block,
936 * otherwise it's a data block.
939 xfs_dir2_leafn_lookup_int(
940 struct xfs_buf
*bp
, /* leaf buffer */
941 xfs_da_args_t
*args
, /* operation arguments */
942 int *indexp
, /* out: leaf entry index */
943 xfs_da_state_t
*state
) /* state to fill in */
945 if (args
->op_flags
& XFS_DA_OP_ADDNAME
)
946 return xfs_dir2_leafn_lookup_for_addname(bp
, args
, indexp
,
948 return xfs_dir2_leafn_lookup_for_entry(bp
, args
, indexp
, state
);
952 * Move count leaf entries from source to destination leaf.
953 * Log entries and headers. Stale entries are preserved.
956 xfs_dir3_leafn_moveents(
957 xfs_da_args_t
*args
, /* operation arguments */
958 struct xfs_buf
*bp_s
, /* source */
959 struct xfs_dir3_icleaf_hdr
*shdr
,
960 struct xfs_dir2_leaf_entry
*sents
,
961 int start_s
,/* source leaf index */
962 struct xfs_buf
*bp_d
, /* destination */
963 struct xfs_dir3_icleaf_hdr
*dhdr
,
964 struct xfs_dir2_leaf_entry
*dents
,
965 int start_d
,/* destination leaf index */
966 int count
) /* count of leaves to copy */
968 int stale
; /* count stale leaves copied */
970 trace_xfs_dir2_leafn_moveents(args
, start_s
, start_d
, count
);
973 * Silently return if nothing to do.
979 * If the destination index is not the end of the current
980 * destination leaf entries, open up a hole in the destination
981 * to hold the new entries.
983 if (start_d
< dhdr
->count
) {
984 memmove(&dents
[start_d
+ count
], &dents
[start_d
],
985 (dhdr
->count
- start_d
) * sizeof(xfs_dir2_leaf_entry_t
));
986 xfs_dir3_leaf_log_ents(args
, dhdr
, bp_d
, start_d
+ count
,
987 count
+ dhdr
->count
- 1);
990 * If the source has stale leaves, count the ones in the copy range
991 * so we can update the header correctly.
994 int i
; /* temp leaf index */
996 for (i
= start_s
, stale
= 0; i
< start_s
+ count
; i
++) {
997 if (sents
[i
].address
==
998 cpu_to_be32(XFS_DIR2_NULL_DATAPTR
))
1004 * Copy the leaf entries from source to destination.
1006 memcpy(&dents
[start_d
], &sents
[start_s
],
1007 count
* sizeof(xfs_dir2_leaf_entry_t
));
1008 xfs_dir3_leaf_log_ents(args
, dhdr
, bp_d
, start_d
, start_d
+ count
- 1);
1011 * If there are source entries after the ones we copied,
1012 * delete the ones we copied by sliding the next ones down.
1014 if (start_s
+ count
< shdr
->count
) {
1015 memmove(&sents
[start_s
], &sents
[start_s
+ count
],
1016 count
* sizeof(xfs_dir2_leaf_entry_t
));
1017 xfs_dir3_leaf_log_ents(args
, shdr
, bp_s
, start_s
,
1018 start_s
+ count
- 1);
1022 * Update the headers and log them.
1024 shdr
->count
-= count
;
1025 shdr
->stale
-= stale
;
1026 dhdr
->count
+= count
;
1027 dhdr
->stale
+= stale
;
1031 * Determine the sort order of two leaf blocks.
1032 * Returns 1 if both are valid and leaf2 should be before leaf1, else 0.
1034 int /* sort order */
1035 xfs_dir2_leafn_order(
1036 struct xfs_inode
*dp
,
1037 struct xfs_buf
*leaf1_bp
, /* leaf1 buffer */
1038 struct xfs_buf
*leaf2_bp
) /* leaf2 buffer */
1040 struct xfs_dir2_leaf
*leaf1
= leaf1_bp
->b_addr
;
1041 struct xfs_dir2_leaf
*leaf2
= leaf2_bp
->b_addr
;
1042 struct xfs_dir2_leaf_entry
*ents1
;
1043 struct xfs_dir2_leaf_entry
*ents2
;
1044 struct xfs_dir3_icleaf_hdr hdr1
;
1045 struct xfs_dir3_icleaf_hdr hdr2
;
1047 xfs_dir2_leaf_hdr_from_disk(dp
->i_mount
, &hdr1
, leaf1
);
1048 xfs_dir2_leaf_hdr_from_disk(dp
->i_mount
, &hdr2
, leaf2
);
1052 if (hdr1
.count
> 0 && hdr2
.count
> 0 &&
1053 (be32_to_cpu(ents2
[0].hashval
) < be32_to_cpu(ents1
[0].hashval
) ||
1054 be32_to_cpu(ents2
[hdr2
.count
- 1].hashval
) <
1055 be32_to_cpu(ents1
[hdr1
.count
- 1].hashval
)))
1061 * Rebalance leaf entries between two leaf blocks.
1062 * This is actually only called when the second block is new,
1063 * though the code deals with the general case.
1064 * A new entry will be inserted in one of the blocks, and that
1065 * entry is taken into account when balancing.
1068 xfs_dir2_leafn_rebalance(
1069 xfs_da_state_t
*state
, /* btree cursor */
1070 xfs_da_state_blk_t
*blk1
, /* first btree block */
1071 xfs_da_state_blk_t
*blk2
) /* second btree block */
1073 xfs_da_args_t
*args
; /* operation arguments */
1074 int count
; /* count (& direction) leaves */
1075 int isleft
; /* new goes in left leaf */
1076 xfs_dir2_leaf_t
*leaf1
; /* first leaf structure */
1077 xfs_dir2_leaf_t
*leaf2
; /* second leaf structure */
1078 int mid
; /* midpoint leaf index */
1079 #if defined(DEBUG) || defined(XFS_WARN)
1080 int oldstale
; /* old count of stale leaves */
1082 int oldsum
; /* old total leaf count */
1083 int swap_blocks
; /* swapped leaf blocks */
1084 struct xfs_dir2_leaf_entry
*ents1
;
1085 struct xfs_dir2_leaf_entry
*ents2
;
1086 struct xfs_dir3_icleaf_hdr hdr1
;
1087 struct xfs_dir3_icleaf_hdr hdr2
;
1088 struct xfs_inode
*dp
= state
->args
->dp
;
1092 * If the block order is wrong, swap the arguments.
1094 swap_blocks
= xfs_dir2_leafn_order(dp
, blk1
->bp
, blk2
->bp
);
1098 leaf1
= blk1
->bp
->b_addr
;
1099 leaf2
= blk2
->bp
->b_addr
;
1100 xfs_dir2_leaf_hdr_from_disk(dp
->i_mount
, &hdr1
, leaf1
);
1101 xfs_dir2_leaf_hdr_from_disk(dp
->i_mount
, &hdr2
, leaf2
);
1105 oldsum
= hdr1
.count
+ hdr2
.count
;
1106 #if defined(DEBUG) || defined(XFS_WARN)
1107 oldstale
= hdr1
.stale
+ hdr2
.stale
;
1112 * If the old leaf count was odd then the new one will be even,
1113 * so we need to divide the new count evenly.
1116 xfs_dahash_t midhash
; /* middle entry hash value */
1118 if (mid
>= hdr1
.count
)
1119 midhash
= be32_to_cpu(ents2
[mid
- hdr1
.count
].hashval
);
1121 midhash
= be32_to_cpu(ents1
[mid
].hashval
);
1122 isleft
= args
->hashval
<= midhash
;
1125 * If the old count is even then the new count is odd, so there's
1126 * no preferred side for the new entry.
1127 * Pick the left one.
1132 * Calculate moved entry count. Positive means left-to-right,
1133 * negative means right-to-left. Then move the entries.
1135 count
= hdr1
.count
- mid
+ (isleft
== 0);
1137 xfs_dir3_leafn_moveents(args
, blk1
->bp
, &hdr1
, ents1
,
1138 hdr1
.count
- count
, blk2
->bp
,
1139 &hdr2
, ents2
, 0, count
);
1141 xfs_dir3_leafn_moveents(args
, blk2
->bp
, &hdr2
, ents2
, 0,
1142 blk1
->bp
, &hdr1
, ents1
,
1145 ASSERT(hdr1
.count
+ hdr2
.count
== oldsum
);
1146 ASSERT(hdr1
.stale
+ hdr2
.stale
== oldstale
);
1148 /* log the changes made when moving the entries */
1149 xfs_dir2_leaf_hdr_to_disk(dp
->i_mount
, leaf1
, &hdr1
);
1150 xfs_dir2_leaf_hdr_to_disk(dp
->i_mount
, leaf2
, &hdr2
);
1151 xfs_dir3_leaf_log_header(args
, blk1
->bp
);
1152 xfs_dir3_leaf_log_header(args
, blk2
->bp
);
1154 xfs_dir3_leaf_check(dp
, blk1
->bp
);
1155 xfs_dir3_leaf_check(dp
, blk2
->bp
);
1158 * Mark whether we're inserting into the old or new leaf.
1160 if (hdr1
.count
< hdr2
.count
)
1161 state
->inleaf
= swap_blocks
;
1162 else if (hdr1
.count
> hdr2
.count
)
1163 state
->inleaf
= !swap_blocks
;
1165 state
->inleaf
= swap_blocks
^ (blk1
->index
<= hdr1
.count
);
1167 * Adjust the expected index for insertion.
1170 blk2
->index
= blk1
->index
- hdr1
.count
;
1173 * Finally sanity check just to make sure we are not returning a
1176 if (blk2
->index
< 0) {
1179 xfs_alert(dp
->i_mount
,
1180 "%s: picked the wrong leaf? reverting original leaf: blk1->index %d",
1181 __func__
, blk1
->index
);
1186 xfs_dir3_data_block_free(
1187 xfs_da_args_t
*args
,
1188 struct xfs_dir2_data_hdr
*hdr
,
1189 struct xfs_dir2_free
*free
,
1192 struct xfs_buf
*fbp
,
1196 struct xfs_dir3_icfree_hdr freehdr
;
1197 struct xfs_inode
*dp
= args
->dp
;
1199 xfs_dir2_free_hdr_from_disk(dp
->i_mount
, &freehdr
, free
);
1202 * Data block is not empty, just set the free entry to the new
1205 freehdr
.bests
[findex
] = cpu_to_be16(longest
);
1206 xfs_dir2_free_log_bests(args
, &freehdr
, fbp
, findex
, findex
);
1210 /* One less used entry in the free table. */
1214 * If this was the last entry in the table, we can trim the table size
1215 * back. There might be other entries at the end referring to
1216 * non-existent data blocks, get those too.
1218 if (findex
== freehdr
.nvalid
- 1) {
1219 int i
; /* free entry index */
1221 for (i
= findex
- 1; i
>= 0; i
--) {
1222 if (freehdr
.bests
[i
] != cpu_to_be16(NULLDATAOFF
))
1225 freehdr
.nvalid
= i
+ 1;
1228 /* Not the last entry, just punch it out. */
1229 freehdr
.bests
[findex
] = cpu_to_be16(NULLDATAOFF
);
1233 xfs_dir2_free_hdr_to_disk(dp
->i_mount
, free
, &freehdr
);
1234 xfs_dir2_free_log_header(args
, fbp
);
1237 * If there are no useful entries left in the block, get rid of the
1240 if (!freehdr
.nused
) {
1243 error
= xfs_dir2_shrink_inode(args
, fdb
, fbp
);
1247 } else if (error
!= -ENOSPC
|| args
->total
!= 0)
1250 * It's possible to get ENOSPC if there is no
1251 * space reservation. In this case some one
1252 * else will eventually get rid of this block.
1256 /* Log the free entry that changed, unless we got rid of it. */
1258 xfs_dir2_free_log_bests(args
, &freehdr
, fbp
, findex
, findex
);
1263 * Remove an entry from a node directory.
1264 * This removes the leaf entry and the data entry,
1265 * and updates the free block if necessary.
1267 static int /* error */
1268 xfs_dir2_leafn_remove(
1269 xfs_da_args_t
*args
, /* operation arguments */
1270 struct xfs_buf
*bp
, /* leaf buffer */
1271 int index
, /* leaf entry index */
1272 xfs_da_state_blk_t
*dblk
, /* data block */
1273 int *rval
) /* resulting block needs join */
1275 struct xfs_da_geometry
*geo
= args
->geo
;
1276 xfs_dir2_data_hdr_t
*hdr
; /* data block header */
1277 xfs_dir2_db_t db
; /* data block number */
1278 struct xfs_buf
*dbp
; /* data block buffer */
1279 xfs_dir2_data_entry_t
*dep
; /* data block entry */
1280 xfs_inode_t
*dp
; /* incore directory inode */
1281 xfs_dir2_leaf_t
*leaf
; /* leaf structure */
1282 xfs_dir2_leaf_entry_t
*lep
; /* leaf entry */
1283 int longest
; /* longest data free entry */
1284 int off
; /* data block entry offset */
1285 int needlog
; /* need to log data header */
1286 int needscan
; /* need to rescan data frees */
1287 xfs_trans_t
*tp
; /* transaction pointer */
1288 struct xfs_dir2_data_free
*bf
; /* bestfree table */
1289 struct xfs_dir3_icleaf_hdr leafhdr
;
1291 trace_xfs_dir2_leafn_remove(args
, index
);
1296 xfs_dir2_leaf_hdr_from_disk(dp
->i_mount
, &leafhdr
, leaf
);
1299 * Point to the entry we're removing.
1301 lep
= &leafhdr
.ents
[index
];
1304 * Extract the data block and offset from the entry.
1306 db
= xfs_dir2_dataptr_to_db(geo
, be32_to_cpu(lep
->address
));
1307 ASSERT(dblk
->blkno
== db
);
1308 off
= xfs_dir2_dataptr_to_off(geo
, be32_to_cpu(lep
->address
));
1309 ASSERT(dblk
->index
== off
);
1312 * Kill the leaf entry by marking it stale.
1313 * Log the leaf block changes.
1316 xfs_dir2_leaf_hdr_to_disk(dp
->i_mount
, leaf
, &leafhdr
);
1317 xfs_dir3_leaf_log_header(args
, bp
);
1319 lep
->address
= cpu_to_be32(XFS_DIR2_NULL_DATAPTR
);
1320 xfs_dir3_leaf_log_ents(args
, &leafhdr
, bp
, index
, index
);
1323 * Make the data entry free. Keep track of the longest freespace
1324 * in the data block in case it changes.
1328 dep
= (xfs_dir2_data_entry_t
*)((char *)hdr
+ off
);
1329 bf
= xfs_dir2_data_bestfree_p(dp
->i_mount
, hdr
);
1330 longest
= be16_to_cpu(bf
[0].length
);
1331 needlog
= needscan
= 0;
1332 xfs_dir2_data_make_free(args
, dbp
, off
,
1333 xfs_dir2_data_entsize(dp
->i_mount
, dep
->namelen
), &needlog
,
1336 * Rescan the data block freespaces for bestfree.
1337 * Log the data block header if needed.
1340 xfs_dir2_data_freescan(dp
->i_mount
, hdr
, &needlog
);
1342 xfs_dir2_data_log_header(args
, dbp
);
1343 xfs_dir3_data_check(dp
, dbp
);
1345 * If the longest data block freespace changes, need to update
1346 * the corresponding freeblock entry.
1348 if (longest
< be16_to_cpu(bf
[0].length
)) {
1349 int error
; /* error return value */
1350 struct xfs_buf
*fbp
; /* freeblock buffer */
1351 xfs_dir2_db_t fdb
; /* freeblock block number */
1352 int findex
; /* index in freeblock entries */
1353 xfs_dir2_free_t
*free
; /* freeblock structure */
1356 * Convert the data block number to a free block,
1357 * read in the free block.
1359 fdb
= xfs_dir2_db_to_fdb(geo
, db
);
1360 error
= xfs_dir2_free_read(tp
, dp
, args
->owner
,
1361 xfs_dir2_db_to_da(geo
, fdb
), &fbp
);
1367 struct xfs_dir3_icfree_hdr freehdr
;
1369 xfs_dir2_free_hdr_from_disk(dp
->i_mount
, &freehdr
, free
);
1370 ASSERT(freehdr
.firstdb
== geo
->free_max_bests
*
1371 (fdb
- xfs_dir2_byte_to_db(geo
, XFS_DIR2_FREE_OFFSET
)));
1375 * Calculate which entry we need to fix.
1377 findex
= xfs_dir2_db_to_fdindex(geo
, db
);
1378 longest
= be16_to_cpu(bf
[0].length
);
1380 * If the data block is now empty we can get rid of it
1383 if (longest
== geo
->blksize
- geo
->data_entry_offset
) {
1385 * Try to punch out the data block.
1387 error
= xfs_dir2_shrink_inode(args
, db
, dbp
);
1393 * We can get ENOSPC if there's no space reservation.
1394 * In this case just drop the buffer and some one else
1395 * will eventually get rid of the empty block.
1397 else if (!(error
== -ENOSPC
&& args
->total
== 0))
1401 * If we got rid of the data block, we can eliminate that entry
1402 * in the free block.
1404 error
= xfs_dir3_data_block_free(args
, hdr
, free
,
1405 fdb
, findex
, fbp
, longest
);
1410 xfs_dir3_leaf_check(dp
, bp
);
1412 * Return indication of whether this leaf block is empty enough
1413 * to justify trying to join it with a neighbor.
1415 *rval
= (geo
->leaf_hdr_size
+
1416 (uint
)sizeof(leafhdr
.ents
) * (leafhdr
.count
- leafhdr
.stale
)) <
1422 * Split the leaf entries in the old block into old and new blocks.
1425 xfs_dir2_leafn_split(
1426 xfs_da_state_t
*state
, /* btree cursor */
1427 xfs_da_state_blk_t
*oldblk
, /* original block */
1428 xfs_da_state_blk_t
*newblk
) /* newly created block */
1430 xfs_da_args_t
*args
; /* operation arguments */
1431 xfs_dablk_t blkno
; /* new leaf block number */
1432 int error
; /* error return value */
1433 struct xfs_inode
*dp
;
1436 * Allocate space for a new leaf node.
1440 ASSERT(oldblk
->magic
== XFS_DIR2_LEAFN_MAGIC
);
1441 error
= xfs_da_grow_inode(args
, &blkno
);
1446 * Initialize the new leaf block.
1448 error
= xfs_dir3_leaf_get_buf(args
, xfs_dir2_da_to_db(args
->geo
, blkno
),
1449 &newblk
->bp
, XFS_DIR2_LEAFN_MAGIC
);
1453 newblk
->blkno
= blkno
;
1454 newblk
->magic
= XFS_DIR2_LEAFN_MAGIC
;
1456 * Rebalance the entries across the two leaves, link the new
1457 * block into the leaves.
1459 xfs_dir2_leafn_rebalance(state
, oldblk
, newblk
);
1460 error
= xfs_da3_blk_link(state
, oldblk
, newblk
);
1465 * Insert the new entry in the correct block.
1468 error
= xfs_dir2_leafn_add(oldblk
->bp
, args
, oldblk
->index
);
1470 error
= xfs_dir2_leafn_add(newblk
->bp
, args
, newblk
->index
);
1472 * Update last hashval in each block since we added the name.
1474 oldblk
->hashval
= xfs_dir2_leaf_lasthash(dp
, oldblk
->bp
, NULL
);
1475 newblk
->hashval
= xfs_dir2_leaf_lasthash(dp
, newblk
->bp
, NULL
);
1476 xfs_dir3_leaf_check(dp
, oldblk
->bp
);
1477 xfs_dir3_leaf_check(dp
, newblk
->bp
);
1482 * Check a leaf block and its neighbors to see if the block should be
1483 * collapsed into one or the other neighbor. Always keep the block
1484 * with the smaller block number.
1485 * If the current block is over 50% full, don't try to join it, return 0.
1486 * If the block is empty, fill in the state structure and return 2.
1487 * If it can be collapsed, fill in the state structure and return 1.
1488 * If nothing can be done, return 0.
1491 xfs_dir2_leafn_toosmall(
1492 xfs_da_state_t
*state
, /* btree cursor */
1493 int *action
) /* resulting action to take */
1495 xfs_da_state_blk_t
*blk
; /* leaf block */
1496 xfs_dablk_t blkno
; /* leaf block number */
1497 struct xfs_buf
*bp
; /* leaf buffer */
1498 int bytes
; /* bytes in use */
1499 int count
; /* leaf live entry count */
1500 int error
; /* error return value */
1501 int forward
; /* sibling block direction */
1502 int i
; /* sibling counter */
1503 xfs_dir2_leaf_t
*leaf
; /* leaf structure */
1504 int rval
; /* result from path_shift */
1505 struct xfs_dir3_icleaf_hdr leafhdr
;
1506 struct xfs_dir2_leaf_entry
*ents
;
1507 struct xfs_inode
*dp
= state
->args
->dp
;
1510 * Check for the degenerate case of the block being over 50% full.
1511 * If so, it's not worth even looking to see if we might be able
1512 * to coalesce with a sibling.
1514 blk
= &state
->path
.blk
[state
->path
.active
- 1];
1515 leaf
= blk
->bp
->b_addr
;
1516 xfs_dir2_leaf_hdr_from_disk(dp
->i_mount
, &leafhdr
, leaf
);
1517 ents
= leafhdr
.ents
;
1518 xfs_dir3_leaf_check(dp
, blk
->bp
);
1520 count
= leafhdr
.count
- leafhdr
.stale
;
1521 bytes
= state
->args
->geo
->leaf_hdr_size
+ count
* sizeof(ents
[0]);
1522 if (bytes
> (state
->args
->geo
->blksize
>> 1)) {
1524 * Blk over 50%, don't try to join.
1530 * Check for the degenerate case of the block being empty.
1531 * If the block is empty, we'll simply delete it, no need to
1532 * coalesce it with a sibling block. We choose (arbitrarily)
1533 * to merge with the forward block unless it is NULL.
1537 * Make altpath point to the block we want to keep and
1538 * path point to the block we want to drop (this one).
1540 forward
= (leafhdr
.forw
!= 0);
1541 memcpy(&state
->altpath
, &state
->path
, sizeof(state
->path
));
1542 error
= xfs_da3_path_shift(state
, &state
->altpath
, forward
, 0,
1546 *action
= rval
? 2 : 0;
1550 * Examine each sibling block to see if we can coalesce with
1551 * at least 25% free space to spare. We need to figure out
1552 * whether to merge with the forward or the backward block.
1553 * We prefer coalescing with the lower numbered sibling so as
1554 * to shrink a directory over time.
1556 forward
= leafhdr
.forw
< leafhdr
.back
;
1557 for (i
= 0, bp
= NULL
; i
< 2; forward
= !forward
, i
++) {
1558 struct xfs_dir3_icleaf_hdr hdr2
;
1560 blkno
= forward
? leafhdr
.forw
: leafhdr
.back
;
1564 * Read the sibling leaf block.
1566 error
= xfs_dir3_leafn_read(state
->args
->trans
, dp
,
1567 state
->args
->owner
, blkno
, &bp
);
1572 * Count bytes in the two blocks combined.
1574 count
= leafhdr
.count
- leafhdr
.stale
;
1575 bytes
= state
->args
->geo
->blksize
-
1576 (state
->args
->geo
->blksize
>> 2);
1579 xfs_dir2_leaf_hdr_from_disk(dp
->i_mount
, &hdr2
, leaf
);
1581 count
+= hdr2
.count
- hdr2
.stale
;
1582 bytes
-= count
* sizeof(ents
[0]);
1585 * Fits with at least 25% to spare.
1589 xfs_trans_brelse(state
->args
->trans
, bp
);
1592 * Didn't like either block, give up.
1600 * Make altpath point to the block we want to keep (the lower
1601 * numbered block) and path point to the block we want to drop.
1603 memcpy(&state
->altpath
, &state
->path
, sizeof(state
->path
));
1604 if (blkno
< blk
->blkno
)
1605 error
= xfs_da3_path_shift(state
, &state
->altpath
, forward
, 0,
1608 error
= xfs_da3_path_shift(state
, &state
->path
, forward
, 0,
1613 *action
= rval
? 0 : 1;
1618 * Move all the leaf entries from drop_blk to save_blk.
1619 * This is done as part of a join operation.
1622 xfs_dir2_leafn_unbalance(
1623 xfs_da_state_t
*state
, /* cursor */
1624 xfs_da_state_blk_t
*drop_blk
, /* dead block */
1625 xfs_da_state_blk_t
*save_blk
) /* surviving block */
1627 xfs_da_args_t
*args
; /* operation arguments */
1628 xfs_dir2_leaf_t
*drop_leaf
; /* dead leaf structure */
1629 xfs_dir2_leaf_t
*save_leaf
; /* surviving leaf structure */
1630 struct xfs_dir3_icleaf_hdr savehdr
;
1631 struct xfs_dir3_icleaf_hdr drophdr
;
1632 struct xfs_dir2_leaf_entry
*sents
;
1633 struct xfs_dir2_leaf_entry
*dents
;
1634 struct xfs_inode
*dp
= state
->args
->dp
;
1637 ASSERT(drop_blk
->magic
== XFS_DIR2_LEAFN_MAGIC
);
1638 ASSERT(save_blk
->magic
== XFS_DIR2_LEAFN_MAGIC
);
1639 drop_leaf
= drop_blk
->bp
->b_addr
;
1640 save_leaf
= save_blk
->bp
->b_addr
;
1642 xfs_dir2_leaf_hdr_from_disk(dp
->i_mount
, &savehdr
, save_leaf
);
1643 xfs_dir2_leaf_hdr_from_disk(dp
->i_mount
, &drophdr
, drop_leaf
);
1644 sents
= savehdr
.ents
;
1645 dents
= drophdr
.ents
;
1648 * If there are any stale leaf entries, take this opportunity
1652 xfs_dir3_leaf_compact(args
, &drophdr
, drop_blk
->bp
);
1654 xfs_dir3_leaf_compact(args
, &savehdr
, save_blk
->bp
);
1657 * Move the entries from drop to the appropriate end of save.
1659 drop_blk
->hashval
= be32_to_cpu(dents
[drophdr
.count
- 1].hashval
);
1660 if (xfs_dir2_leafn_order(dp
, save_blk
->bp
, drop_blk
->bp
))
1661 xfs_dir3_leafn_moveents(args
, drop_blk
->bp
, &drophdr
, dents
, 0,
1662 save_blk
->bp
, &savehdr
, sents
, 0,
1665 xfs_dir3_leafn_moveents(args
, drop_blk
->bp
, &drophdr
, dents
, 0,
1666 save_blk
->bp
, &savehdr
, sents
,
1667 savehdr
.count
, drophdr
.count
);
1668 save_blk
->hashval
= be32_to_cpu(sents
[savehdr
.count
- 1].hashval
);
1670 /* log the changes made when moving the entries */
1671 xfs_dir2_leaf_hdr_to_disk(dp
->i_mount
, save_leaf
, &savehdr
);
1672 xfs_dir2_leaf_hdr_to_disk(dp
->i_mount
, drop_leaf
, &drophdr
);
1673 xfs_dir3_leaf_log_header(args
, save_blk
->bp
);
1674 xfs_dir3_leaf_log_header(args
, drop_blk
->bp
);
1676 xfs_dir3_leaf_check(dp
, save_blk
->bp
);
1677 xfs_dir3_leaf_check(dp
, drop_blk
->bp
);
1681 * Add a new data block to the directory at the free space index that the caller
1685 xfs_dir2_node_add_datablk(
1686 struct xfs_da_args
*args
,
1687 struct xfs_da_state_blk
*fblk
,
1688 xfs_dir2_db_t
*dbno
,
1689 struct xfs_buf
**dbpp
,
1690 struct xfs_buf
**fbpp
,
1691 struct xfs_dir3_icfree_hdr
*hdr
,
1694 struct xfs_inode
*dp
= args
->dp
;
1695 struct xfs_trans
*tp
= args
->trans
;
1696 struct xfs_mount
*mp
= dp
->i_mount
;
1697 struct xfs_dir2_data_free
*bf
;
1699 struct xfs_buf
*fbp
;
1700 struct xfs_buf
*dbp
;
1703 /* Not allowed to allocate, return failure. */
1704 if (args
->total
== 0)
1707 /* Allocate and initialize the new data block. */
1708 error
= xfs_dir2_grow_inode(args
, XFS_DIR2_DATA_SPACE
, dbno
);
1711 error
= xfs_dir3_data_init(args
, *dbno
, &dbp
);
1716 * Get the freespace block corresponding to the data block
1717 * that was just allocated.
1719 fbno
= xfs_dir2_db_to_fdb(args
->geo
, *dbno
);
1720 error
= xfs_dir2_free_try_read(tp
, dp
, args
->owner
,
1721 xfs_dir2_db_to_da(args
->geo
, fbno
), &fbp
);
1726 * If there wasn't a freespace block, the read will
1727 * return a NULL fbp. Allocate and initialize a new one.
1730 error
= xfs_dir2_grow_inode(args
, XFS_DIR2_FREE_SPACE
, &fbno
);
1734 if (XFS_IS_CORRUPT(mp
,
1735 xfs_dir2_db_to_fdb(args
->geo
, *dbno
) !=
1738 "%s: dir ino %llu needed freesp block %lld for data block %lld, got %lld",
1739 __func__
, (unsigned long long)dp
->i_ino
,
1740 (long long)xfs_dir2_db_to_fdb(args
->geo
, *dbno
),
1741 (long long)*dbno
, (long long)fbno
);
1744 " fblk "PTR_FMT
" blkno %llu index %d magic 0x%x",
1745 fblk
, (unsigned long long)fblk
->blkno
,
1746 fblk
->index
, fblk
->magic
);
1748 xfs_alert(mp
, " ... fblk is NULL");
1750 xfs_da_mark_sick(args
);
1751 return -EFSCORRUPTED
;
1754 /* Get a buffer for the new block. */
1755 error
= xfs_dir3_free_get_buf(args
, fbno
, &fbp
);
1758 xfs_dir2_free_hdr_from_disk(mp
, hdr
, fbp
->b_addr
);
1760 /* Remember the first slot as our empty slot. */
1761 hdr
->firstdb
= (fbno
- xfs_dir2_byte_to_db(args
->geo
,
1762 XFS_DIR2_FREE_OFFSET
)) *
1763 args
->geo
->free_max_bests
;
1765 xfs_dir2_free_hdr_from_disk(mp
, hdr
, fbp
->b_addr
);
1768 /* Set the freespace block index from the data block number. */
1769 *findex
= xfs_dir2_db_to_fdindex(args
->geo
, *dbno
);
1771 /* Extend the freespace table if the new data block is off the end. */
1772 if (*findex
>= hdr
->nvalid
) {
1773 ASSERT(*findex
< args
->geo
->free_max_bests
);
1774 hdr
->nvalid
= *findex
+ 1;
1775 hdr
->bests
[*findex
] = cpu_to_be16(NULLDATAOFF
);
1779 * If this entry was for an empty data block (this should always be
1780 * true) then update the header.
1782 if (hdr
->bests
[*findex
] == cpu_to_be16(NULLDATAOFF
)) {
1784 xfs_dir2_free_hdr_to_disk(mp
, fbp
->b_addr
, hdr
);
1785 xfs_dir2_free_log_header(args
, fbp
);
1788 /* Update the freespace value for the new block in the table. */
1789 bf
= xfs_dir2_data_bestfree_p(mp
, dbp
->b_addr
);
1790 hdr
->bests
[*findex
] = bf
[0].length
;
1798 xfs_dir2_node_find_freeblk(
1799 struct xfs_da_args
*args
,
1800 struct xfs_da_state_blk
*fblk
,
1801 xfs_dir2_db_t
*dbnop
,
1802 struct xfs_buf
**fbpp
,
1803 struct xfs_dir3_icfree_hdr
*hdr
,
1807 struct xfs_inode
*dp
= args
->dp
;
1808 struct xfs_trans
*tp
= args
->trans
;
1809 struct xfs_buf
*fbp
= NULL
;
1810 xfs_dir2_db_t firstfbno
;
1811 xfs_dir2_db_t lastfbno
;
1812 xfs_dir2_db_t ifbno
= -1;
1813 xfs_dir2_db_t dbno
= -1;
1820 * If we came in with a freespace block that means that lookup
1821 * found an entry with our hash value. This is the freespace
1822 * block for that data entry.
1826 findex
= fblk
->index
;
1827 xfs_dir2_free_hdr_from_disk(dp
->i_mount
, hdr
, fbp
->b_addr
);
1829 /* caller already found the freespace for us. */
1830 ASSERT(findex
< hdr
->nvalid
);
1831 ASSERT(be16_to_cpu(hdr
->bests
[findex
]) != NULLDATAOFF
);
1832 ASSERT(be16_to_cpu(hdr
->bests
[findex
]) >= length
);
1833 dbno
= hdr
->firstdb
+ findex
;
1838 * The data block looked at didn't have enough room.
1839 * We'll start at the beginning of the freespace entries.
1841 ifbno
= fblk
->blkno
;
1842 xfs_trans_brelse(tp
, fbp
);
1848 * If we don't have a data block yet, we're going to scan the freespace
1849 * data for a data block with enough free space in it.
1851 error
= xfs_bmap_last_offset(dp
, &fo
, XFS_DATA_FORK
);
1854 lastfbno
= xfs_dir2_da_to_db(args
->geo
, (xfs_dablk_t
)fo
);
1855 firstfbno
= xfs_dir2_byte_to_db(args
->geo
, XFS_DIR2_FREE_OFFSET
);
1857 for (fbno
= lastfbno
- 1; fbno
>= firstfbno
; fbno
--) {
1858 /* If it's ifbno we already looked at it. */
1863 * Read the block. There can be holes in the freespace blocks,
1864 * so this might not succeed. This should be really rare, so
1865 * there's no reason to avoid it.
1867 error
= xfs_dir2_free_try_read(tp
, dp
, args
->owner
,
1868 xfs_dir2_db_to_da(args
->geo
, fbno
),
1875 xfs_dir2_free_hdr_from_disk(dp
->i_mount
, hdr
, fbp
->b_addr
);
1877 /* Scan the free entry array for a large enough free space. */
1878 for (findex
= hdr
->nvalid
- 1; findex
>= 0; findex
--) {
1879 if (be16_to_cpu(hdr
->bests
[findex
]) != NULLDATAOFF
&&
1880 be16_to_cpu(hdr
->bests
[findex
]) >= length
) {
1881 dbno
= hdr
->firstdb
+ findex
;
1886 /* Didn't find free space, go on to next free block */
1887 xfs_trans_brelse(tp
, fbp
);
1898 * Add the data entry for a node-format directory name addition.
1899 * The leaf entry is added in xfs_dir2_leafn_add.
1900 * We may enter with a freespace block that the lookup found.
1903 xfs_dir2_node_addname_int(
1904 struct xfs_da_args
*args
, /* operation arguments */
1905 struct xfs_da_state_blk
*fblk
) /* optional freespace block */
1907 struct xfs_dir2_data_unused
*dup
; /* data unused entry pointer */
1908 struct xfs_dir2_data_entry
*dep
; /* data entry pointer */
1909 struct xfs_dir2_data_hdr
*hdr
; /* data block header */
1910 struct xfs_dir2_data_free
*bf
;
1911 struct xfs_trans
*tp
= args
->trans
;
1912 struct xfs_inode
*dp
= args
->dp
;
1913 struct xfs_dir3_icfree_hdr freehdr
;
1914 struct xfs_buf
*dbp
; /* data block buffer */
1915 struct xfs_buf
*fbp
; /* freespace buffer */
1916 xfs_dir2_data_aoff_t aoff
;
1917 xfs_dir2_db_t dbno
; /* data block number */
1918 int error
; /* error return value */
1919 int findex
; /* freespace entry index */
1920 int length
; /* length of the new entry */
1921 int logfree
= 0; /* need to log free entry */
1922 int needlog
= 0; /* need to log data header */
1923 int needscan
= 0; /* need to rescan data frees */
1924 __be16
*tagp
; /* data entry tag pointer */
1926 length
= xfs_dir2_data_entsize(dp
->i_mount
, args
->namelen
);
1927 error
= xfs_dir2_node_find_freeblk(args
, fblk
, &dbno
, &fbp
, &freehdr
,
1933 * Now we know if we must allocate blocks, so if we are checking whether
1934 * we can insert without allocation then we can return now.
1936 if (args
->op_flags
& XFS_DA_OP_JUSTCHECK
) {
1943 * If we don't have a data block, we need to allocate one and make
1944 * the freespace entries refer to it.
1947 /* we're going to have to log the free block index later */
1949 error
= xfs_dir2_node_add_datablk(args
, fblk
, &dbno
, &dbp
, &fbp
,
1952 /* Read the data block in. */
1953 error
= xfs_dir3_data_read(tp
, dp
, args
->owner
,
1954 xfs_dir2_db_to_da(args
->geo
, dbno
), 0, &dbp
);
1959 /* setup for data block up now */
1961 bf
= xfs_dir2_data_bestfree_p(dp
->i_mount
, hdr
);
1962 ASSERT(be16_to_cpu(bf
[0].length
) >= length
);
1964 /* Point to the existing unused space. */
1965 dup
= (xfs_dir2_data_unused_t
*)
1966 ((char *)hdr
+ be16_to_cpu(bf
[0].offset
));
1968 /* Mark the first part of the unused space, inuse for us. */
1969 aoff
= (xfs_dir2_data_aoff_t
)((char *)dup
- (char *)hdr
);
1970 error
= xfs_dir2_data_use_free(args
, dbp
, dup
, aoff
, length
,
1971 &needlog
, &needscan
);
1973 xfs_trans_brelse(tp
, dbp
);
1977 /* Fill in the new entry and log it. */
1978 dep
= (xfs_dir2_data_entry_t
*)dup
;
1979 dep
->inumber
= cpu_to_be64(args
->inumber
);
1980 dep
->namelen
= args
->namelen
;
1981 memcpy(dep
->name
, args
->name
, dep
->namelen
);
1982 xfs_dir2_data_put_ftype(dp
->i_mount
, dep
, args
->filetype
);
1983 tagp
= xfs_dir2_data_entry_tag_p(dp
->i_mount
, dep
);
1984 *tagp
= cpu_to_be16((char *)dep
- (char *)hdr
);
1985 xfs_dir2_data_log_entry(args
, dbp
, dep
);
1987 /* Rescan the freespace and log the data block if needed. */
1989 xfs_dir2_data_freescan(dp
->i_mount
, hdr
, &needlog
);
1991 xfs_dir2_data_log_header(args
, dbp
);
1993 /* If the freespace block entry is now wrong, update it. */
1994 if (freehdr
.bests
[findex
] != bf
[0].length
) {
1995 freehdr
.bests
[findex
] = bf
[0].length
;
1999 /* Log the freespace entry if needed. */
2001 xfs_dir2_free_log_bests(args
, &freehdr
, fbp
, findex
, findex
);
2003 /* Return the data block and offset in args. */
2004 args
->blkno
= (xfs_dablk_t
)dbno
;
2005 args
->index
= be16_to_cpu(*tagp
);
2010 * Top-level node form directory addname routine.
2013 xfs_dir2_node_addname(
2014 xfs_da_args_t
*args
) /* operation arguments */
2016 xfs_da_state_blk_t
*blk
; /* leaf block for insert */
2017 int error
; /* error return value */
2018 int rval
; /* sub-return value */
2019 xfs_da_state_t
*state
; /* btree cursor */
2021 trace_xfs_dir2_node_addname(args
);
2024 * Allocate and initialize the state (btree cursor).
2026 state
= xfs_da_state_alloc(args
);
2028 * Look up the name. We're not supposed to find it, but
2029 * this gives us the insertion point.
2031 error
= xfs_da3_node_lookup_int(state
, &rval
);
2034 if (rval
!= -ENOENT
) {
2038 * Add the data entry to a data block.
2039 * Extravalid is set to a freeblock found by lookup.
2041 rval
= xfs_dir2_node_addname_int(args
,
2042 state
->extravalid
? &state
->extrablk
: NULL
);
2046 blk
= &state
->path
.blk
[state
->path
.active
- 1];
2047 ASSERT(blk
->magic
== XFS_DIR2_LEAFN_MAGIC
);
2049 * Add the new leaf entry.
2051 rval
= xfs_dir2_leafn_add(blk
->bp
, args
, blk
->index
);
2054 * It worked, fix the hash values up the btree.
2056 if (!(args
->op_flags
& XFS_DA_OP_JUSTCHECK
))
2057 xfs_da3_fixhashpath(state
, &state
->path
);
2060 * It didn't work, we need to split the leaf block.
2062 if (args
->total
== 0) {
2063 ASSERT(rval
== -ENOSPC
);
2067 * Split the leaf block and insert the new entry.
2069 rval
= xfs_da3_split(state
);
2072 xfs_da_state_free(state
);
2077 * Lookup an entry in a node-format directory.
2078 * All the real work happens in xfs_da3_node_lookup_int.
2079 * The only real output is the inode number of the entry.
2082 xfs_dir2_node_lookup(
2083 xfs_da_args_t
*args
) /* operation arguments */
2085 int error
; /* error return value */
2086 int i
; /* btree level */
2087 int rval
; /* operation return value */
2088 xfs_da_state_t
*state
; /* btree cursor */
2090 trace_xfs_dir2_node_lookup(args
);
2093 * Allocate and initialize the btree cursor.
2095 state
= xfs_da_state_alloc(args
);
2098 * Fill in the path to the entry in the cursor.
2100 error
= xfs_da3_node_lookup_int(state
, &rval
);
2103 else if (rval
== -ENOENT
&& args
->cmpresult
== XFS_CMP_CASE
) {
2104 /* If a CI match, dup the actual name and return -EEXIST */
2105 xfs_dir2_data_entry_t
*dep
;
2107 dep
= (xfs_dir2_data_entry_t
*)
2108 ((char *)state
->extrablk
.bp
->b_addr
+
2109 state
->extrablk
.index
);
2110 rval
= xfs_dir_cilookup_result(args
, dep
->name
, dep
->namelen
);
2113 * Release the btree blocks and leaf block.
2115 for (i
= 0; i
< state
->path
.active
; i
++) {
2116 xfs_trans_brelse(args
->trans
, state
->path
.blk
[i
].bp
);
2117 state
->path
.blk
[i
].bp
= NULL
;
2120 * Release the data block if we have it.
2122 if (state
->extravalid
&& state
->extrablk
.bp
) {
2123 xfs_trans_brelse(args
->trans
, state
->extrablk
.bp
);
2124 state
->extrablk
.bp
= NULL
;
2126 xfs_da_state_free(state
);
2131 * Remove an entry from a node-format directory.
2134 xfs_dir2_node_removename(
2135 struct xfs_da_args
*args
) /* operation arguments */
2137 struct xfs_da_state_blk
*blk
; /* leaf block */
2138 int error
; /* error return value */
2139 int rval
; /* operation return value */
2140 struct xfs_da_state
*state
; /* btree cursor */
2142 trace_xfs_dir2_node_removename(args
);
2145 * Allocate and initialize the btree cursor.
2147 state
= xfs_da_state_alloc(args
);
2149 /* Look up the entry we're deleting, set up the cursor. */
2150 error
= xfs_da3_node_lookup_int(state
, &rval
);
2154 /* Didn't find it, upper layer screwed up. */
2155 if (rval
!= -EEXIST
) {
2160 blk
= &state
->path
.blk
[state
->path
.active
- 1];
2161 ASSERT(blk
->magic
== XFS_DIR2_LEAFN_MAGIC
);
2162 ASSERT(state
->extravalid
);
2164 * Remove the leaf and data entries.
2165 * Extrablk refers to the data block.
2167 error
= xfs_dir2_leafn_remove(args
, blk
->bp
, blk
->index
,
2168 &state
->extrablk
, &rval
);
2172 * Fix the hash values up the btree.
2174 xfs_da3_fixhashpath(state
, &state
->path
);
2176 * If we need to join leaf blocks, do it.
2178 if (rval
&& state
->path
.active
> 1)
2179 error
= xfs_da3_join(state
);
2181 * If no errors so far, try conversion to leaf format.
2184 error
= xfs_dir2_node_to_leaf(state
);
2186 xfs_da_state_free(state
);
2191 * Replace an entry's inode number in a node-format directory.
2194 xfs_dir2_node_replace(
2195 xfs_da_args_t
*args
) /* operation arguments */
2197 xfs_da_state_blk_t
*blk
; /* leaf block */
2198 xfs_dir2_data_hdr_t
*hdr
; /* data block header */
2199 xfs_dir2_data_entry_t
*dep
; /* data entry changed */
2200 int error
; /* error return value */
2201 int i
; /* btree level */
2202 xfs_ino_t inum
; /* new inode number */
2203 int ftype
; /* new file type */
2204 int rval
; /* internal return value */
2205 xfs_da_state_t
*state
; /* btree cursor */
2207 trace_xfs_dir2_node_replace(args
);
2210 * Allocate and initialize the btree cursor.
2212 state
= xfs_da_state_alloc(args
);
2215 * We have to save new inode number and ftype since
2216 * xfs_da3_node_lookup_int() is going to overwrite them
2218 inum
= args
->inumber
;
2219 ftype
= args
->filetype
;
2222 * Lookup the entry to change in the btree.
2224 error
= xfs_da3_node_lookup_int(state
, &rval
);
2229 * It should be found, since the vnodeops layer has looked it up
2230 * and locked it. But paranoia is good.
2232 if (rval
== -EEXIST
) {
2233 struct xfs_dir3_icleaf_hdr leafhdr
;
2236 * Find the leaf entry.
2238 blk
= &state
->path
.blk
[state
->path
.active
- 1];
2239 ASSERT(blk
->magic
== XFS_DIR2_LEAFN_MAGIC
);
2240 ASSERT(state
->extravalid
);
2242 xfs_dir2_leaf_hdr_from_disk(state
->mp
, &leafhdr
,
2245 * Point to the data entry.
2247 hdr
= state
->extrablk
.bp
->b_addr
;
2248 ASSERT(hdr
->magic
== cpu_to_be32(XFS_DIR2_DATA_MAGIC
) ||
2249 hdr
->magic
== cpu_to_be32(XFS_DIR3_DATA_MAGIC
));
2250 dep
= (xfs_dir2_data_entry_t
*)
2252 xfs_dir2_dataptr_to_off(args
->geo
,
2253 be32_to_cpu(leafhdr
.ents
[blk
->index
].address
)));
2254 ASSERT(inum
!= be64_to_cpu(dep
->inumber
));
2256 * Fill in the new inode number and log the entry.
2258 dep
->inumber
= cpu_to_be64(inum
);
2259 xfs_dir2_data_put_ftype(state
->mp
, dep
, ftype
);
2260 xfs_dir2_data_log_entry(args
, state
->extrablk
.bp
, dep
);
2264 * Didn't find it, and we're holding a data block. Drop it.
2266 else if (state
->extravalid
) {
2267 xfs_trans_brelse(args
->trans
, state
->extrablk
.bp
);
2268 state
->extrablk
.bp
= NULL
;
2271 * Release all the buffers in the cursor.
2273 for (i
= 0; i
< state
->path
.active
; i
++) {
2274 xfs_trans_brelse(args
->trans
, state
->path
.blk
[i
].bp
);
2275 state
->path
.blk
[i
].bp
= NULL
;
2277 xfs_da_state_free(state
);
2282 * Trim off a trailing empty freespace block.
2283 * Return (in rvalp) 1 if we did it, 0 if not.
2286 xfs_dir2_node_trim_free(
2287 xfs_da_args_t
*args
, /* operation arguments */
2288 xfs_fileoff_t fo
, /* free block number */
2289 int *rvalp
) /* out: did something */
2291 struct xfs_buf
*bp
; /* freespace buffer */
2292 xfs_inode_t
*dp
; /* incore directory inode */
2293 int error
; /* error return code */
2294 xfs_dir2_free_t
*free
; /* freespace structure */
2295 xfs_trans_t
*tp
; /* transaction pointer */
2296 struct xfs_dir3_icfree_hdr freehdr
;
2304 * Read the freespace block.
2306 error
= xfs_dir2_free_try_read(tp
, dp
, args
->owner
, fo
, &bp
);
2310 * There can be holes in freespace. If fo is a hole, there's
2316 xfs_dir2_free_hdr_from_disk(dp
->i_mount
, &freehdr
, free
);
2319 * If there are used entries, there's nothing to do.
2321 if (freehdr
.nused
> 0) {
2322 xfs_trans_brelse(tp
, bp
);
2326 * Blow the block away.
2328 error
= xfs_dir2_shrink_inode(args
,
2329 xfs_dir2_da_to_db(args
->geo
, (xfs_dablk_t
)fo
), bp
);
2332 * Can't fail with ENOSPC since that only happens with no
2333 * space reservation, when breaking up an extent into two
2334 * pieces. This is the last block of an extent.
2336 ASSERT(error
!= -ENOSPC
);
2337 xfs_trans_brelse(tp
, bp
);
2341 * Return that we succeeded.