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 * Check internal consistency of a leafn block.
42 struct xfs_dir2_leaf
*leaf
= bp
->b_addr
;
43 struct xfs_dir3_icleaf_hdr leafhdr
;
45 dp
->d_ops
->leaf_hdr_from_disk(&leafhdr
, leaf
);
47 if (leafhdr
.magic
== XFS_DIR3_LEAFN_MAGIC
) {
48 struct xfs_dir3_leaf_hdr
*leaf3
= bp
->b_addr
;
49 if (be64_to_cpu(leaf3
->info
.blkno
) != bp
->b_bn
)
50 return __this_address
;
51 } else if (leafhdr
.magic
!= XFS_DIR2_LEAFN_MAGIC
)
52 return __this_address
;
54 return xfs_dir3_leaf_check_int(dp
->i_mount
, dp
, &leafhdr
, leaf
);
64 fa
= xfs_dir3_leafn_check(dp
, bp
);
67 xfs_corruption_error(__func__
, XFS_ERRLEVEL_LOW
, dp
->i_mount
,
68 bp
->b_addr
, BBTOB(bp
->b_length
), __FILE__
, __LINE__
,
73 #define xfs_dir3_leaf_check(dp, bp)
80 struct xfs_mount
*mp
= bp
->b_mount
;
81 struct xfs_dir2_free_hdr
*hdr
= bp
->b_addr
;
83 if (!xfs_verify_magic(bp
, hdr
->magic
))
84 return __this_address
;
86 if (xfs_sb_version_hascrc(&mp
->m_sb
)) {
87 struct xfs_dir3_blk_hdr
*hdr3
= bp
->b_addr
;
89 if (!uuid_equal(&hdr3
->uuid
, &mp
->m_sb
.sb_meta_uuid
))
90 return __this_address
;
91 if (be64_to_cpu(hdr3
->blkno
) != bp
->b_bn
)
92 return __this_address
;
93 if (!xfs_log_check_lsn(mp
, be64_to_cpu(hdr3
->lsn
)))
94 return __this_address
;
97 /* XXX: should bounds check the xfs_dir3_icfree_hdr here */
103 xfs_dir3_free_read_verify(
106 struct xfs_mount
*mp
= bp
->b_mount
;
109 if (xfs_sb_version_hascrc(&mp
->m_sb
) &&
110 !xfs_buf_verify_cksum(bp
, XFS_DIR3_FREE_CRC_OFF
))
111 xfs_verifier_error(bp
, -EFSBADCRC
, __this_address
);
113 fa
= xfs_dir3_free_verify(bp
);
115 xfs_verifier_error(bp
, -EFSCORRUPTED
, fa
);
120 xfs_dir3_free_write_verify(
123 struct xfs_mount
*mp
= bp
->b_mount
;
124 struct xfs_buf_log_item
*bip
= bp
->b_log_item
;
125 struct xfs_dir3_blk_hdr
*hdr3
= bp
->b_addr
;
128 fa
= xfs_dir3_free_verify(bp
);
130 xfs_verifier_error(bp
, -EFSCORRUPTED
, fa
);
134 if (!xfs_sb_version_hascrc(&mp
->m_sb
))
138 hdr3
->lsn
= cpu_to_be64(bip
->bli_item
.li_lsn
);
140 xfs_buf_update_cksum(bp
, XFS_DIR3_FREE_CRC_OFF
);
143 const struct xfs_buf_ops xfs_dir3_free_buf_ops
= {
144 .name
= "xfs_dir3_free",
145 .magic
= { cpu_to_be32(XFS_DIR2_FREE_MAGIC
),
146 cpu_to_be32(XFS_DIR3_FREE_MAGIC
) },
147 .verify_read
= xfs_dir3_free_read_verify
,
148 .verify_write
= xfs_dir3_free_write_verify
,
149 .verify_struct
= xfs_dir3_free_verify
,
152 /* Everything ok in the free block header? */
153 static xfs_failaddr_t
154 xfs_dir3_free_header_check(
155 struct xfs_inode
*dp
,
159 struct xfs_mount
*mp
= dp
->i_mount
;
160 unsigned int firstdb
;
163 maxbests
= dp
->d_ops
->free_max_bests(mp
->m_dir_geo
);
164 firstdb
= (xfs_dir2_da_to_db(mp
->m_dir_geo
, fbno
) -
165 xfs_dir2_byte_to_db(mp
->m_dir_geo
, XFS_DIR2_FREE_OFFSET
)) *
167 if (xfs_sb_version_hascrc(&mp
->m_sb
)) {
168 struct xfs_dir3_free_hdr
*hdr3
= bp
->b_addr
;
170 if (be32_to_cpu(hdr3
->firstdb
) != firstdb
)
171 return __this_address
;
172 if (be32_to_cpu(hdr3
->nvalid
) > maxbests
)
173 return __this_address
;
174 if (be32_to_cpu(hdr3
->nvalid
) < be32_to_cpu(hdr3
->nused
))
175 return __this_address
;
177 struct xfs_dir2_free_hdr
*hdr
= bp
->b_addr
;
179 if (be32_to_cpu(hdr
->firstdb
) != firstdb
)
180 return __this_address
;
181 if (be32_to_cpu(hdr
->nvalid
) > maxbests
)
182 return __this_address
;
183 if (be32_to_cpu(hdr
->nvalid
) < be32_to_cpu(hdr
->nused
))
184 return __this_address
;
190 __xfs_dir3_free_read(
191 struct xfs_trans
*tp
,
192 struct xfs_inode
*dp
,
194 xfs_daddr_t mappedbno
,
195 struct xfs_buf
**bpp
)
200 err
= xfs_da_read_buf(tp
, dp
, fbno
, mappedbno
, bpp
,
201 XFS_DATA_FORK
, &xfs_dir3_free_buf_ops
);
205 /* Check things that we can't do in the verifier. */
206 fa
= xfs_dir3_free_header_check(dp
, fbno
, *bpp
);
208 xfs_verifier_error(*bpp
, -EFSCORRUPTED
, fa
);
209 xfs_trans_brelse(tp
, *bpp
);
210 return -EFSCORRUPTED
;
213 /* try read returns without an error or *bpp if it lands in a hole */
215 xfs_trans_buf_set_type(tp
, *bpp
, XFS_BLFT_DIR_FREE_BUF
);
222 struct xfs_trans
*tp
,
223 struct xfs_inode
*dp
,
225 struct xfs_buf
**bpp
)
227 return __xfs_dir3_free_read(tp
, dp
, fbno
, -1, bpp
);
231 xfs_dir2_free_try_read(
232 struct xfs_trans
*tp
,
233 struct xfs_inode
*dp
,
235 struct xfs_buf
**bpp
)
237 return __xfs_dir3_free_read(tp
, dp
, fbno
, -2, bpp
);
241 xfs_dir3_free_get_buf(
244 struct xfs_buf
**bpp
)
246 struct xfs_trans
*tp
= args
->trans
;
247 struct xfs_inode
*dp
= args
->dp
;
248 struct xfs_mount
*mp
= dp
->i_mount
;
251 struct xfs_dir3_icfree_hdr hdr
;
253 error
= xfs_da_get_buf(tp
, dp
, xfs_dir2_db_to_da(args
->geo
, fbno
),
254 -1, &bp
, XFS_DATA_FORK
);
258 xfs_trans_buf_set_type(tp
, bp
, XFS_BLFT_DIR_FREE_BUF
);
259 bp
->b_ops
= &xfs_dir3_free_buf_ops
;
262 * Initialize the new block to be empty, and remember
263 * its first slot as our empty slot.
265 memset(bp
->b_addr
, 0, sizeof(struct xfs_dir3_free_hdr
));
266 memset(&hdr
, 0, sizeof(hdr
));
268 if (xfs_sb_version_hascrc(&mp
->m_sb
)) {
269 struct xfs_dir3_free_hdr
*hdr3
= bp
->b_addr
;
271 hdr
.magic
= XFS_DIR3_FREE_MAGIC
;
273 hdr3
->hdr
.blkno
= cpu_to_be64(bp
->b_bn
);
274 hdr3
->hdr
.owner
= cpu_to_be64(dp
->i_ino
);
275 uuid_copy(&hdr3
->hdr
.uuid
, &mp
->m_sb
.sb_meta_uuid
);
277 hdr
.magic
= XFS_DIR2_FREE_MAGIC
;
278 dp
->d_ops
->free_hdr_to_disk(bp
->b_addr
, &hdr
);
284 * Log entries from a freespace block.
287 xfs_dir2_free_log_bests(
288 struct xfs_da_args
*args
,
290 int first
, /* first entry to log */
291 int last
) /* last entry to log */
293 xfs_dir2_free_t
*free
; /* freespace structure */
297 bests
= args
->dp
->d_ops
->free_bests_p(free
);
298 ASSERT(free
->hdr
.magic
== cpu_to_be32(XFS_DIR2_FREE_MAGIC
) ||
299 free
->hdr
.magic
== cpu_to_be32(XFS_DIR3_FREE_MAGIC
));
300 xfs_trans_log_buf(args
->trans
, bp
,
301 (uint
)((char *)&bests
[first
] - (char *)free
),
302 (uint
)((char *)&bests
[last
] - (char *)free
+
303 sizeof(bests
[0]) - 1));
307 * Log header from a freespace block.
310 xfs_dir2_free_log_header(
311 struct xfs_da_args
*args
,
315 xfs_dir2_free_t
*free
; /* freespace structure */
318 ASSERT(free
->hdr
.magic
== cpu_to_be32(XFS_DIR2_FREE_MAGIC
) ||
319 free
->hdr
.magic
== cpu_to_be32(XFS_DIR3_FREE_MAGIC
));
321 xfs_trans_log_buf(args
->trans
, bp
, 0,
322 args
->dp
->d_ops
->free_hdr_size
- 1);
326 * Convert a leaf-format directory to a node-format directory.
327 * We need to change the magic number of the leaf block, and copy
328 * the freespace table out of the leaf block into its own block.
331 xfs_dir2_leaf_to_node(
332 xfs_da_args_t
*args
, /* operation arguments */
333 struct xfs_buf
*lbp
) /* leaf buffer */
335 xfs_inode_t
*dp
; /* incore directory inode */
336 int error
; /* error return value */
337 struct xfs_buf
*fbp
; /* freespace buffer */
338 xfs_dir2_db_t fdb
; /* freespace block number */
339 xfs_dir2_free_t
*free
; /* freespace structure */
340 __be16
*from
; /* pointer to freespace entry */
341 int i
; /* leaf freespace index */
342 xfs_dir2_leaf_t
*leaf
; /* leaf structure */
343 xfs_dir2_leaf_tail_t
*ltp
; /* leaf tail structure */
344 int n
; /* count of live freespc ents */
345 xfs_dir2_data_off_t off
; /* freespace entry value */
346 __be16
*to
; /* pointer to freespace entry */
347 xfs_trans_t
*tp
; /* transaction pointer */
348 struct xfs_dir3_icfree_hdr freehdr
;
350 trace_xfs_dir2_leaf_to_node(args
);
355 * Add a freespace block to the directory.
357 if ((error
= xfs_dir2_grow_inode(args
, XFS_DIR2_FREE_SPACE
, &fdb
))) {
360 ASSERT(fdb
== xfs_dir2_byte_to_db(args
->geo
, XFS_DIR2_FREE_OFFSET
));
362 * Get the buffer for the new freespace block.
364 error
= xfs_dir3_free_get_buf(args
, fdb
, &fbp
);
369 dp
->d_ops
->free_hdr_from_disk(&freehdr
, free
);
371 ltp
= xfs_dir2_leaf_tail_p(args
->geo
, leaf
);
372 if (be32_to_cpu(ltp
->bestcount
) >
373 (uint
)dp
->i_d
.di_size
/ args
->geo
->blksize
)
374 return -EFSCORRUPTED
;
377 * Copy freespace entries from the leaf block to the new block.
378 * Count active entries.
380 from
= xfs_dir2_leaf_bests_p(ltp
);
381 to
= dp
->d_ops
->free_bests_p(free
);
382 for (i
= n
= 0; i
< be32_to_cpu(ltp
->bestcount
); i
++, from
++, to
++) {
383 if ((off
= be16_to_cpu(*from
)) != NULLDATAOFF
)
385 *to
= cpu_to_be16(off
);
389 * Now initialize the freespace block header.
392 freehdr
.nvalid
= be32_to_cpu(ltp
->bestcount
);
394 dp
->d_ops
->free_hdr_to_disk(fbp
->b_addr
, &freehdr
);
395 xfs_dir2_free_log_bests(args
, fbp
, 0, freehdr
.nvalid
- 1);
396 xfs_dir2_free_log_header(args
, fbp
);
399 * Converting the leaf to a leafnode is just a matter of changing the
400 * magic number and the ops. Do the change directly to the buffer as
401 * it's less work (and less code) than decoding the header to host
402 * format and back again.
404 if (leaf
->hdr
.info
.magic
== cpu_to_be16(XFS_DIR2_LEAF1_MAGIC
))
405 leaf
->hdr
.info
.magic
= cpu_to_be16(XFS_DIR2_LEAFN_MAGIC
);
407 leaf
->hdr
.info
.magic
= cpu_to_be16(XFS_DIR3_LEAFN_MAGIC
);
408 lbp
->b_ops
= &xfs_dir3_leafn_buf_ops
;
409 xfs_trans_buf_set_type(tp
, lbp
, XFS_BLFT_DIR_LEAFN_BUF
);
410 xfs_dir3_leaf_log_header(args
, lbp
);
411 xfs_dir3_leaf_check(dp
, lbp
);
416 * Add a leaf entry to a leaf block in a node-form directory.
417 * The other work necessary is done from the caller.
419 static int /* error */
421 struct xfs_buf
*bp
, /* leaf buffer */
422 struct xfs_da_args
*args
, /* operation arguments */
423 int index
) /* insertion pt for new entry */
425 struct xfs_dir3_icleaf_hdr leafhdr
;
426 struct xfs_inode
*dp
= args
->dp
;
427 struct xfs_dir2_leaf
*leaf
= bp
->b_addr
;
428 struct xfs_dir2_leaf_entry
*lep
;
429 struct xfs_dir2_leaf_entry
*ents
;
430 int compact
; /* compacting stale leaves */
431 int highstale
= 0; /* next stale entry */
432 int lfloghigh
; /* high leaf entry logging */
433 int lfloglow
; /* low leaf entry logging */
434 int lowstale
= 0; /* previous stale entry */
436 trace_xfs_dir2_leafn_add(args
, index
);
438 dp
->d_ops
->leaf_hdr_from_disk(&leafhdr
, leaf
);
439 ents
= dp
->d_ops
->leaf_ents_p(leaf
);
442 * Quick check just to make sure we are not going to index
443 * into other peoples memory
446 return -EFSCORRUPTED
;
449 * If there are already the maximum number of leaf entries in
450 * the block, if there are no stale entries it won't fit.
451 * Caller will do a split. If there are stale entries we'll do
455 if (leafhdr
.count
== dp
->d_ops
->leaf_max_ents(args
->geo
)) {
458 compact
= leafhdr
.stale
> 1;
461 ASSERT(index
== 0 || be32_to_cpu(ents
[index
- 1].hashval
) <= args
->hashval
);
462 ASSERT(index
== leafhdr
.count
||
463 be32_to_cpu(ents
[index
].hashval
) >= args
->hashval
);
465 if (args
->op_flags
& XFS_DA_OP_JUSTCHECK
)
469 * Compact out all but one stale leaf entry. Leaves behind
470 * the entry closest to index.
473 xfs_dir3_leaf_compact_x1(&leafhdr
, ents
, &index
, &lowstale
,
474 &highstale
, &lfloglow
, &lfloghigh
);
475 else if (leafhdr
.stale
) {
477 * Set impossible logging indices for this case.
479 lfloglow
= leafhdr
.count
;
484 * Insert the new entry, log everything.
486 lep
= xfs_dir3_leaf_find_entry(&leafhdr
, ents
, index
, compact
, lowstale
,
487 highstale
, &lfloglow
, &lfloghigh
);
489 lep
->hashval
= cpu_to_be32(args
->hashval
);
490 lep
->address
= cpu_to_be32(xfs_dir2_db_off_to_dataptr(args
->geo
,
491 args
->blkno
, args
->index
));
493 dp
->d_ops
->leaf_hdr_to_disk(leaf
, &leafhdr
);
494 xfs_dir3_leaf_log_header(args
, bp
);
495 xfs_dir3_leaf_log_ents(args
, bp
, lfloglow
, lfloghigh
);
496 xfs_dir3_leaf_check(dp
, bp
);
502 xfs_dir2_free_hdr_check(
503 struct xfs_inode
*dp
,
507 struct xfs_dir3_icfree_hdr hdr
;
509 dp
->d_ops
->free_hdr_from_disk(&hdr
, bp
->b_addr
);
511 ASSERT((hdr
.firstdb
%
512 dp
->d_ops
->free_max_bests(dp
->i_mount
->m_dir_geo
)) == 0);
513 ASSERT(hdr
.firstdb
<= db
);
514 ASSERT(db
< hdr
.firstdb
+ hdr
.nvalid
);
517 #define xfs_dir2_free_hdr_check(dp, bp, db)
521 * Return the last hash value in the leaf.
522 * Stale entries are ok.
524 xfs_dahash_t
/* hash value */
525 xfs_dir2_leaf_lasthash(
526 struct xfs_inode
*dp
,
527 struct xfs_buf
*bp
, /* leaf buffer */
528 int *count
) /* count of entries in leaf */
530 struct xfs_dir2_leaf
*leaf
= bp
->b_addr
;
531 struct xfs_dir2_leaf_entry
*ents
;
532 struct xfs_dir3_icleaf_hdr leafhdr
;
534 dp
->d_ops
->leaf_hdr_from_disk(&leafhdr
, leaf
);
536 ASSERT(leafhdr
.magic
== XFS_DIR2_LEAFN_MAGIC
||
537 leafhdr
.magic
== XFS_DIR3_LEAFN_MAGIC
||
538 leafhdr
.magic
== XFS_DIR2_LEAF1_MAGIC
||
539 leafhdr
.magic
== XFS_DIR3_LEAF1_MAGIC
);
542 *count
= leafhdr
.count
;
546 ents
= dp
->d_ops
->leaf_ents_p(leaf
);
547 return be32_to_cpu(ents
[leafhdr
.count
- 1].hashval
);
551 * Look up a leaf entry for space to add a name in a node-format leaf block.
552 * The extrablk in state is a freespace block.
555 xfs_dir2_leafn_lookup_for_addname(
556 struct xfs_buf
*bp
, /* leaf buffer */
557 xfs_da_args_t
*args
, /* operation arguments */
558 int *indexp
, /* out: leaf entry index */
559 xfs_da_state_t
*state
) /* state to fill in */
561 struct xfs_buf
*curbp
= NULL
; /* current data/free buffer */
562 xfs_dir2_db_t curdb
= -1; /* current data block number */
563 xfs_dir2_db_t curfdb
= -1; /* current free block number */
564 xfs_inode_t
*dp
; /* incore directory inode */
565 int error
; /* error return value */
566 int fi
; /* free entry index */
567 xfs_dir2_free_t
*free
= NULL
; /* free block structure */
568 int index
; /* leaf entry index */
569 xfs_dir2_leaf_t
*leaf
; /* leaf structure */
570 int length
; /* length of new data entry */
571 xfs_dir2_leaf_entry_t
*lep
; /* leaf entry */
572 xfs_mount_t
*mp
; /* filesystem mount point */
573 xfs_dir2_db_t newdb
; /* new data block number */
574 xfs_dir2_db_t newfdb
; /* new free block number */
575 xfs_trans_t
*tp
; /* transaction pointer */
576 struct xfs_dir2_leaf_entry
*ents
;
577 struct xfs_dir3_icleaf_hdr leafhdr
;
583 dp
->d_ops
->leaf_hdr_from_disk(&leafhdr
, leaf
);
584 ents
= dp
->d_ops
->leaf_ents_p(leaf
);
586 xfs_dir3_leaf_check(dp
, bp
);
587 ASSERT(leafhdr
.count
> 0);
590 * Look up the hash value in the leaf entries.
592 index
= xfs_dir2_leaf_search_hash(args
, bp
);
594 * Do we have a buffer coming in?
596 if (state
->extravalid
) {
597 /* If so, it's a free block buffer, get the block number. */
598 curbp
= state
->extrablk
.bp
;
599 curfdb
= state
->extrablk
.blkno
;
600 free
= curbp
->b_addr
;
601 ASSERT(free
->hdr
.magic
== cpu_to_be32(XFS_DIR2_FREE_MAGIC
) ||
602 free
->hdr
.magic
== cpu_to_be32(XFS_DIR3_FREE_MAGIC
));
604 length
= dp
->d_ops
->data_entsize(args
->namelen
);
606 * Loop over leaf entries with the right hash value.
608 for (lep
= &ents
[index
];
609 index
< leafhdr
.count
&& be32_to_cpu(lep
->hashval
) == args
->hashval
;
612 * Skip stale leaf entries.
614 if (be32_to_cpu(lep
->address
) == XFS_DIR2_NULL_DATAPTR
)
617 * Pull the data block number from the entry.
619 newdb
= xfs_dir2_dataptr_to_db(args
->geo
,
620 be32_to_cpu(lep
->address
));
622 * For addname, we're looking for a place to put the new entry.
623 * We want to use a data block with an entry of equal
624 * hash value to ours if there is one with room.
626 * If this block isn't the data block we already have
627 * in hand, take a look at it.
629 if (newdb
!= curdb
) {
634 * Convert the data block to the free block
635 * holding its freespace information.
637 newfdb
= dp
->d_ops
->db_to_fdb(args
->geo
, newdb
);
639 * If it's not the one we have in hand, read it in.
641 if (newfdb
!= curfdb
) {
643 * If we had one before, drop it.
646 xfs_trans_brelse(tp
, curbp
);
648 error
= xfs_dir2_free_read(tp
, dp
,
649 xfs_dir2_db_to_da(args
->geo
,
654 free
= curbp
->b_addr
;
656 xfs_dir2_free_hdr_check(dp
, curbp
, curdb
);
659 * Get the index for our entry.
661 fi
= dp
->d_ops
->db_to_fdindex(args
->geo
, curdb
);
663 * If it has room, return it.
665 bests
= dp
->d_ops
->free_bests_p(free
);
666 if (unlikely(bests
[fi
] == cpu_to_be16(NULLDATAOFF
))) {
667 XFS_ERROR_REPORT("xfs_dir2_leafn_lookup_int",
668 XFS_ERRLEVEL_LOW
, mp
);
669 if (curfdb
!= newfdb
)
670 xfs_trans_brelse(tp
, curbp
);
671 return -EFSCORRUPTED
;
674 if (be16_to_cpu(bests
[fi
]) >= length
)
678 /* Didn't find any space */
681 ASSERT(args
->op_flags
& XFS_DA_OP_OKNOENT
);
683 /* Giving back a free block. */
684 state
->extravalid
= 1;
685 state
->extrablk
.bp
= curbp
;
686 state
->extrablk
.index
= fi
;
687 state
->extrablk
.blkno
= curfdb
;
690 * Important: this magic number is not in the buffer - it's for
691 * buffer type information and therefore only the free/data type
692 * matters here, not whether CRCs are enabled or not.
694 state
->extrablk
.magic
= XFS_DIR2_FREE_MAGIC
;
696 state
->extravalid
= 0;
699 * Return the index, that will be the insertion point.
706 * Look up a leaf entry in a node-format leaf block.
707 * The extrablk in state a data block.
710 xfs_dir2_leafn_lookup_for_entry(
711 struct xfs_buf
*bp
, /* leaf buffer */
712 xfs_da_args_t
*args
, /* operation arguments */
713 int *indexp
, /* out: leaf entry index */
714 xfs_da_state_t
*state
) /* state to fill in */
716 struct xfs_buf
*curbp
= NULL
; /* current data/free buffer */
717 xfs_dir2_db_t curdb
= -1; /* current data block number */
718 xfs_dir2_data_entry_t
*dep
; /* data block entry */
719 xfs_inode_t
*dp
; /* incore directory inode */
720 int error
; /* error return value */
721 int index
; /* leaf entry index */
722 xfs_dir2_leaf_t
*leaf
; /* leaf structure */
723 xfs_dir2_leaf_entry_t
*lep
; /* leaf entry */
724 xfs_mount_t
*mp
; /* filesystem mount point */
725 xfs_dir2_db_t newdb
; /* new data block number */
726 xfs_trans_t
*tp
; /* transaction pointer */
727 enum xfs_dacmp cmp
; /* comparison result */
728 struct xfs_dir2_leaf_entry
*ents
;
729 struct xfs_dir3_icleaf_hdr leafhdr
;
735 dp
->d_ops
->leaf_hdr_from_disk(&leafhdr
, leaf
);
736 ents
= dp
->d_ops
->leaf_ents_p(leaf
);
738 xfs_dir3_leaf_check(dp
, bp
);
739 if (leafhdr
.count
<= 0)
740 return -EFSCORRUPTED
;
743 * Look up the hash value in the leaf entries.
745 index
= xfs_dir2_leaf_search_hash(args
, bp
);
747 * Do we have a buffer coming in?
749 if (state
->extravalid
) {
750 curbp
= state
->extrablk
.bp
;
751 curdb
= state
->extrablk
.blkno
;
754 * Loop over leaf entries with the right hash value.
756 for (lep
= &ents
[index
];
757 index
< leafhdr
.count
&& be32_to_cpu(lep
->hashval
) == args
->hashval
;
760 * Skip stale leaf entries.
762 if (be32_to_cpu(lep
->address
) == XFS_DIR2_NULL_DATAPTR
)
765 * Pull the data block number from the entry.
767 newdb
= xfs_dir2_dataptr_to_db(args
->geo
,
768 be32_to_cpu(lep
->address
));
770 * Not adding a new entry, so we really want to find
771 * the name given to us.
773 * If it's a different data block, go get it.
775 if (newdb
!= curdb
) {
777 * If we had a block before that we aren't saving
778 * for a CI name, drop it
780 if (curbp
&& (args
->cmpresult
== XFS_CMP_DIFFERENT
||
781 curdb
!= state
->extrablk
.blkno
))
782 xfs_trans_brelse(tp
, curbp
);
784 * If needing the block that is saved with a CI match,
785 * use it otherwise read in the new data block.
787 if (args
->cmpresult
!= XFS_CMP_DIFFERENT
&&
788 newdb
== state
->extrablk
.blkno
) {
789 ASSERT(state
->extravalid
);
790 curbp
= state
->extrablk
.bp
;
792 error
= xfs_dir3_data_read(tp
, dp
,
793 xfs_dir2_db_to_da(args
->geo
,
799 xfs_dir3_data_check(dp
, curbp
);
803 * Point to the data entry.
805 dep
= (xfs_dir2_data_entry_t
*)((char *)curbp
->b_addr
+
806 xfs_dir2_dataptr_to_off(args
->geo
,
807 be32_to_cpu(lep
->address
)));
809 * Compare the entry and if it's an exact match, return
810 * EEXIST immediately. If it's the first case-insensitive
811 * match, store the block & inode number and continue looking.
813 cmp
= mp
->m_dirnameops
->compname(args
, dep
->name
, dep
->namelen
);
814 if (cmp
!= XFS_CMP_DIFFERENT
&& cmp
!= args
->cmpresult
) {
815 /* If there is a CI match block, drop it */
816 if (args
->cmpresult
!= XFS_CMP_DIFFERENT
&&
817 curdb
!= state
->extrablk
.blkno
)
818 xfs_trans_brelse(tp
, state
->extrablk
.bp
);
819 args
->cmpresult
= cmp
;
820 args
->inumber
= be64_to_cpu(dep
->inumber
);
821 args
->filetype
= dp
->d_ops
->data_get_ftype(dep
);
823 state
->extravalid
= 1;
824 state
->extrablk
.bp
= curbp
;
825 state
->extrablk
.blkno
= curdb
;
826 state
->extrablk
.index
= (int)((char *)dep
-
827 (char *)curbp
->b_addr
);
828 state
->extrablk
.magic
= XFS_DIR2_DATA_MAGIC
;
829 curbp
->b_ops
= &xfs_dir3_data_buf_ops
;
830 xfs_trans_buf_set_type(tp
, curbp
, XFS_BLFT_DIR_DATA_BUF
);
831 if (cmp
== XFS_CMP_EXACT
)
835 ASSERT(index
== leafhdr
.count
|| (args
->op_flags
& XFS_DA_OP_OKNOENT
));
837 if (args
->cmpresult
== XFS_CMP_DIFFERENT
) {
838 /* Giving back last used data block. */
839 state
->extravalid
= 1;
840 state
->extrablk
.bp
= curbp
;
841 state
->extrablk
.index
= -1;
842 state
->extrablk
.blkno
= curdb
;
843 state
->extrablk
.magic
= XFS_DIR2_DATA_MAGIC
;
844 curbp
->b_ops
= &xfs_dir3_data_buf_ops
;
845 xfs_trans_buf_set_type(tp
, curbp
, XFS_BLFT_DIR_DATA_BUF
);
847 /* If the curbp is not the CI match block, drop it */
848 if (state
->extrablk
.bp
!= curbp
)
849 xfs_trans_brelse(tp
, curbp
);
852 state
->extravalid
= 0;
859 * Look up a leaf entry in a node-format leaf block.
860 * If this is an addname then the extrablk in state is a freespace block,
861 * otherwise it's a data block.
864 xfs_dir2_leafn_lookup_int(
865 struct xfs_buf
*bp
, /* leaf buffer */
866 xfs_da_args_t
*args
, /* operation arguments */
867 int *indexp
, /* out: leaf entry index */
868 xfs_da_state_t
*state
) /* state to fill in */
870 if (args
->op_flags
& XFS_DA_OP_ADDNAME
)
871 return xfs_dir2_leafn_lookup_for_addname(bp
, args
, indexp
,
873 return xfs_dir2_leafn_lookup_for_entry(bp
, args
, indexp
, state
);
877 * Move count leaf entries from source to destination leaf.
878 * Log entries and headers. Stale entries are preserved.
881 xfs_dir3_leafn_moveents(
882 xfs_da_args_t
*args
, /* operation arguments */
883 struct xfs_buf
*bp_s
, /* source */
884 struct xfs_dir3_icleaf_hdr
*shdr
,
885 struct xfs_dir2_leaf_entry
*sents
,
886 int start_s
,/* source leaf index */
887 struct xfs_buf
*bp_d
, /* destination */
888 struct xfs_dir3_icleaf_hdr
*dhdr
,
889 struct xfs_dir2_leaf_entry
*dents
,
890 int start_d
,/* destination leaf index */
891 int count
) /* count of leaves to copy */
893 int stale
; /* count stale leaves copied */
895 trace_xfs_dir2_leafn_moveents(args
, start_s
, start_d
, count
);
898 * Silently return if nothing to do.
904 * If the destination index is not the end of the current
905 * destination leaf entries, open up a hole in the destination
906 * to hold the new entries.
908 if (start_d
< dhdr
->count
) {
909 memmove(&dents
[start_d
+ count
], &dents
[start_d
],
910 (dhdr
->count
- start_d
) * sizeof(xfs_dir2_leaf_entry_t
));
911 xfs_dir3_leaf_log_ents(args
, bp_d
, start_d
+ count
,
912 count
+ dhdr
->count
- 1);
915 * If the source has stale leaves, count the ones in the copy range
916 * so we can update the header correctly.
919 int i
; /* temp leaf index */
921 for (i
= start_s
, stale
= 0; i
< start_s
+ count
; i
++) {
922 if (sents
[i
].address
==
923 cpu_to_be32(XFS_DIR2_NULL_DATAPTR
))
929 * Copy the leaf entries from source to destination.
931 memcpy(&dents
[start_d
], &sents
[start_s
],
932 count
* sizeof(xfs_dir2_leaf_entry_t
));
933 xfs_dir3_leaf_log_ents(args
, bp_d
, start_d
, start_d
+ count
- 1);
936 * If there are source entries after the ones we copied,
937 * delete the ones we copied by sliding the next ones down.
939 if (start_s
+ count
< shdr
->count
) {
940 memmove(&sents
[start_s
], &sents
[start_s
+ count
],
941 count
* sizeof(xfs_dir2_leaf_entry_t
));
942 xfs_dir3_leaf_log_ents(args
, bp_s
, start_s
, start_s
+ count
- 1);
946 * Update the headers and log them.
948 shdr
->count
-= count
;
949 shdr
->stale
-= stale
;
950 dhdr
->count
+= count
;
951 dhdr
->stale
+= stale
;
955 * Determine the sort order of two leaf blocks.
956 * Returns 1 if both are valid and leaf2 should be before leaf1, else 0.
959 xfs_dir2_leafn_order(
960 struct xfs_inode
*dp
,
961 struct xfs_buf
*leaf1_bp
, /* leaf1 buffer */
962 struct xfs_buf
*leaf2_bp
) /* leaf2 buffer */
964 struct xfs_dir2_leaf
*leaf1
= leaf1_bp
->b_addr
;
965 struct xfs_dir2_leaf
*leaf2
= leaf2_bp
->b_addr
;
966 struct xfs_dir2_leaf_entry
*ents1
;
967 struct xfs_dir2_leaf_entry
*ents2
;
968 struct xfs_dir3_icleaf_hdr hdr1
;
969 struct xfs_dir3_icleaf_hdr hdr2
;
971 dp
->d_ops
->leaf_hdr_from_disk(&hdr1
, leaf1
);
972 dp
->d_ops
->leaf_hdr_from_disk(&hdr2
, leaf2
);
973 ents1
= dp
->d_ops
->leaf_ents_p(leaf1
);
974 ents2
= dp
->d_ops
->leaf_ents_p(leaf2
);
976 if (hdr1
.count
> 0 && hdr2
.count
> 0 &&
977 (be32_to_cpu(ents2
[0].hashval
) < be32_to_cpu(ents1
[0].hashval
) ||
978 be32_to_cpu(ents2
[hdr2
.count
- 1].hashval
) <
979 be32_to_cpu(ents1
[hdr1
.count
- 1].hashval
)))
985 * Rebalance leaf entries between two leaf blocks.
986 * This is actually only called when the second block is new,
987 * though the code deals with the general case.
988 * A new entry will be inserted in one of the blocks, and that
989 * entry is taken into account when balancing.
992 xfs_dir2_leafn_rebalance(
993 xfs_da_state_t
*state
, /* btree cursor */
994 xfs_da_state_blk_t
*blk1
, /* first btree block */
995 xfs_da_state_blk_t
*blk2
) /* second btree block */
997 xfs_da_args_t
*args
; /* operation arguments */
998 int count
; /* count (& direction) leaves */
999 int isleft
; /* new goes in left leaf */
1000 xfs_dir2_leaf_t
*leaf1
; /* first leaf structure */
1001 xfs_dir2_leaf_t
*leaf2
; /* second leaf structure */
1002 int mid
; /* midpoint leaf index */
1003 #if defined(DEBUG) || defined(XFS_WARN)
1004 int oldstale
; /* old count of stale leaves */
1006 int oldsum
; /* old total leaf count */
1007 int swap_blocks
; /* swapped leaf blocks */
1008 struct xfs_dir2_leaf_entry
*ents1
;
1009 struct xfs_dir2_leaf_entry
*ents2
;
1010 struct xfs_dir3_icleaf_hdr hdr1
;
1011 struct xfs_dir3_icleaf_hdr hdr2
;
1012 struct xfs_inode
*dp
= state
->args
->dp
;
1016 * If the block order is wrong, swap the arguments.
1018 swap_blocks
= xfs_dir2_leafn_order(dp
, blk1
->bp
, blk2
->bp
);
1022 leaf1
= blk1
->bp
->b_addr
;
1023 leaf2
= blk2
->bp
->b_addr
;
1024 dp
->d_ops
->leaf_hdr_from_disk(&hdr1
, leaf1
);
1025 dp
->d_ops
->leaf_hdr_from_disk(&hdr2
, leaf2
);
1026 ents1
= dp
->d_ops
->leaf_ents_p(leaf1
);
1027 ents2
= dp
->d_ops
->leaf_ents_p(leaf2
);
1029 oldsum
= hdr1
.count
+ hdr2
.count
;
1030 #if defined(DEBUG) || defined(XFS_WARN)
1031 oldstale
= hdr1
.stale
+ hdr2
.stale
;
1036 * If the old leaf count was odd then the new one will be even,
1037 * so we need to divide the new count evenly.
1040 xfs_dahash_t midhash
; /* middle entry hash value */
1042 if (mid
>= hdr1
.count
)
1043 midhash
= be32_to_cpu(ents2
[mid
- hdr1
.count
].hashval
);
1045 midhash
= be32_to_cpu(ents1
[mid
].hashval
);
1046 isleft
= args
->hashval
<= midhash
;
1049 * If the old count is even then the new count is odd, so there's
1050 * no preferred side for the new entry.
1051 * Pick the left one.
1056 * Calculate moved entry count. Positive means left-to-right,
1057 * negative means right-to-left. Then move the entries.
1059 count
= hdr1
.count
- mid
+ (isleft
== 0);
1061 xfs_dir3_leafn_moveents(args
, blk1
->bp
, &hdr1
, ents1
,
1062 hdr1
.count
- count
, blk2
->bp
,
1063 &hdr2
, ents2
, 0, count
);
1065 xfs_dir3_leafn_moveents(args
, blk2
->bp
, &hdr2
, ents2
, 0,
1066 blk1
->bp
, &hdr1
, ents1
,
1069 ASSERT(hdr1
.count
+ hdr2
.count
== oldsum
);
1070 ASSERT(hdr1
.stale
+ hdr2
.stale
== oldstale
);
1072 /* log the changes made when moving the entries */
1073 dp
->d_ops
->leaf_hdr_to_disk(leaf1
, &hdr1
);
1074 dp
->d_ops
->leaf_hdr_to_disk(leaf2
, &hdr2
);
1075 xfs_dir3_leaf_log_header(args
, blk1
->bp
);
1076 xfs_dir3_leaf_log_header(args
, blk2
->bp
);
1078 xfs_dir3_leaf_check(dp
, blk1
->bp
);
1079 xfs_dir3_leaf_check(dp
, blk2
->bp
);
1082 * Mark whether we're inserting into the old or new leaf.
1084 if (hdr1
.count
< hdr2
.count
)
1085 state
->inleaf
= swap_blocks
;
1086 else if (hdr1
.count
> hdr2
.count
)
1087 state
->inleaf
= !swap_blocks
;
1089 state
->inleaf
= swap_blocks
^ (blk1
->index
<= hdr1
.count
);
1091 * Adjust the expected index for insertion.
1094 blk2
->index
= blk1
->index
- hdr1
.count
;
1097 * Finally sanity check just to make sure we are not returning a
1100 if (blk2
->index
< 0) {
1103 xfs_alert(dp
->i_mount
,
1104 "%s: picked the wrong leaf? reverting original leaf: blk1->index %d",
1105 __func__
, blk1
->index
);
1110 xfs_dir3_data_block_free(
1111 xfs_da_args_t
*args
,
1112 struct xfs_dir2_data_hdr
*hdr
,
1113 struct xfs_dir2_free
*free
,
1116 struct xfs_buf
*fbp
,
1121 struct xfs_dir3_icfree_hdr freehdr
;
1122 struct xfs_inode
*dp
= args
->dp
;
1124 dp
->d_ops
->free_hdr_from_disk(&freehdr
, free
);
1125 bests
= dp
->d_ops
->free_bests_p(free
);
1128 * Data block is not empty, just set the free entry to the new
1131 bests
[findex
] = cpu_to_be16(longest
);
1132 xfs_dir2_free_log_bests(args
, fbp
, findex
, findex
);
1136 /* One less used entry in the free table. */
1140 * If this was the last entry in the table, we can trim the table size
1141 * back. There might be other entries at the end referring to
1142 * non-existent data blocks, get those too.
1144 if (findex
== freehdr
.nvalid
- 1) {
1145 int i
; /* free entry index */
1147 for (i
= findex
- 1; i
>= 0; i
--) {
1148 if (bests
[i
] != cpu_to_be16(NULLDATAOFF
))
1151 freehdr
.nvalid
= i
+ 1;
1154 /* Not the last entry, just punch it out. */
1155 bests
[findex
] = cpu_to_be16(NULLDATAOFF
);
1159 dp
->d_ops
->free_hdr_to_disk(free
, &freehdr
);
1160 xfs_dir2_free_log_header(args
, fbp
);
1163 * If there are no useful entries left in the block, get rid of the
1166 if (!freehdr
.nused
) {
1169 error
= xfs_dir2_shrink_inode(args
, fdb
, fbp
);
1173 } else if (error
!= -ENOSPC
|| args
->total
!= 0)
1176 * It's possible to get ENOSPC if there is no
1177 * space reservation. In this case some one
1178 * else will eventually get rid of this block.
1182 /* Log the free entry that changed, unless we got rid of it. */
1184 xfs_dir2_free_log_bests(args
, fbp
, findex
, findex
);
1189 * Remove an entry from a node directory.
1190 * This removes the leaf entry and the data entry,
1191 * and updates the free block if necessary.
1193 static int /* error */
1194 xfs_dir2_leafn_remove(
1195 xfs_da_args_t
*args
, /* operation arguments */
1196 struct xfs_buf
*bp
, /* leaf buffer */
1197 int index
, /* leaf entry index */
1198 xfs_da_state_blk_t
*dblk
, /* data block */
1199 int *rval
) /* resulting block needs join */
1201 xfs_dir2_data_hdr_t
*hdr
; /* data block header */
1202 xfs_dir2_db_t db
; /* data block number */
1203 struct xfs_buf
*dbp
; /* data block buffer */
1204 xfs_dir2_data_entry_t
*dep
; /* data block entry */
1205 xfs_inode_t
*dp
; /* incore directory inode */
1206 xfs_dir2_leaf_t
*leaf
; /* leaf structure */
1207 xfs_dir2_leaf_entry_t
*lep
; /* leaf entry */
1208 int longest
; /* longest data free entry */
1209 int off
; /* data block entry offset */
1210 int needlog
; /* need to log data header */
1211 int needscan
; /* need to rescan data frees */
1212 xfs_trans_t
*tp
; /* transaction pointer */
1213 struct xfs_dir2_data_free
*bf
; /* bestfree table */
1214 struct xfs_dir3_icleaf_hdr leafhdr
;
1215 struct xfs_dir2_leaf_entry
*ents
;
1217 trace_xfs_dir2_leafn_remove(args
, index
);
1222 dp
->d_ops
->leaf_hdr_from_disk(&leafhdr
, leaf
);
1223 ents
= dp
->d_ops
->leaf_ents_p(leaf
);
1226 * Point to the entry we're removing.
1231 * Extract the data block and offset from the entry.
1233 db
= xfs_dir2_dataptr_to_db(args
->geo
, be32_to_cpu(lep
->address
));
1234 ASSERT(dblk
->blkno
== db
);
1235 off
= xfs_dir2_dataptr_to_off(args
->geo
, be32_to_cpu(lep
->address
));
1236 ASSERT(dblk
->index
== off
);
1239 * Kill the leaf entry by marking it stale.
1240 * Log the leaf block changes.
1243 dp
->d_ops
->leaf_hdr_to_disk(leaf
, &leafhdr
);
1244 xfs_dir3_leaf_log_header(args
, bp
);
1246 lep
->address
= cpu_to_be32(XFS_DIR2_NULL_DATAPTR
);
1247 xfs_dir3_leaf_log_ents(args
, bp
, index
, index
);
1250 * Make the data entry free. Keep track of the longest freespace
1251 * in the data block in case it changes.
1255 dep
= (xfs_dir2_data_entry_t
*)((char *)hdr
+ off
);
1256 bf
= dp
->d_ops
->data_bestfree_p(hdr
);
1257 longest
= be16_to_cpu(bf
[0].length
);
1258 needlog
= needscan
= 0;
1259 xfs_dir2_data_make_free(args
, dbp
, off
,
1260 dp
->d_ops
->data_entsize(dep
->namelen
), &needlog
, &needscan
);
1262 * Rescan the data block freespaces for bestfree.
1263 * Log the data block header if needed.
1266 xfs_dir2_data_freescan(dp
, hdr
, &needlog
);
1268 xfs_dir2_data_log_header(args
, dbp
);
1269 xfs_dir3_data_check(dp
, dbp
);
1271 * If the longest data block freespace changes, need to update
1272 * the corresponding freeblock entry.
1274 if (longest
< be16_to_cpu(bf
[0].length
)) {
1275 int error
; /* error return value */
1276 struct xfs_buf
*fbp
; /* freeblock buffer */
1277 xfs_dir2_db_t fdb
; /* freeblock block number */
1278 int findex
; /* index in freeblock entries */
1279 xfs_dir2_free_t
*free
; /* freeblock structure */
1282 * Convert the data block number to a free block,
1283 * read in the free block.
1285 fdb
= dp
->d_ops
->db_to_fdb(args
->geo
, db
);
1286 error
= xfs_dir2_free_read(tp
, dp
,
1287 xfs_dir2_db_to_da(args
->geo
, fdb
),
1294 struct xfs_dir3_icfree_hdr freehdr
;
1295 dp
->d_ops
->free_hdr_from_disk(&freehdr
, free
);
1296 ASSERT(freehdr
.firstdb
== dp
->d_ops
->free_max_bests(args
->geo
) *
1297 (fdb
- xfs_dir2_byte_to_db(args
->geo
,
1298 XFS_DIR2_FREE_OFFSET
)));
1302 * Calculate which entry we need to fix.
1304 findex
= dp
->d_ops
->db_to_fdindex(args
->geo
, db
);
1305 longest
= be16_to_cpu(bf
[0].length
);
1307 * If the data block is now empty we can get rid of it
1310 if (longest
== args
->geo
->blksize
-
1311 dp
->d_ops
->data_entry_offset
) {
1313 * Try to punch out the data block.
1315 error
= xfs_dir2_shrink_inode(args
, db
, dbp
);
1321 * We can get ENOSPC if there's no space reservation.
1322 * In this case just drop the buffer and some one else
1323 * will eventually get rid of the empty block.
1325 else if (!(error
== -ENOSPC
&& args
->total
== 0))
1329 * If we got rid of the data block, we can eliminate that entry
1330 * in the free block.
1332 error
= xfs_dir3_data_block_free(args
, hdr
, free
,
1333 fdb
, findex
, fbp
, longest
);
1338 xfs_dir3_leaf_check(dp
, bp
);
1340 * Return indication of whether this leaf block is empty enough
1341 * to justify trying to join it with a neighbor.
1343 *rval
= (dp
->d_ops
->leaf_hdr_size
+
1344 (uint
)sizeof(ents
[0]) * (leafhdr
.count
- leafhdr
.stale
)) <
1345 args
->geo
->magicpct
;
1350 * Split the leaf entries in the old block into old and new blocks.
1353 xfs_dir2_leafn_split(
1354 xfs_da_state_t
*state
, /* btree cursor */
1355 xfs_da_state_blk_t
*oldblk
, /* original block */
1356 xfs_da_state_blk_t
*newblk
) /* newly created block */
1358 xfs_da_args_t
*args
; /* operation arguments */
1359 xfs_dablk_t blkno
; /* new leaf block number */
1360 int error
; /* error return value */
1361 struct xfs_inode
*dp
;
1364 * Allocate space for a new leaf node.
1368 ASSERT(oldblk
->magic
== XFS_DIR2_LEAFN_MAGIC
);
1369 error
= xfs_da_grow_inode(args
, &blkno
);
1374 * Initialize the new leaf block.
1376 error
= xfs_dir3_leaf_get_buf(args
, xfs_dir2_da_to_db(args
->geo
, blkno
),
1377 &newblk
->bp
, XFS_DIR2_LEAFN_MAGIC
);
1381 newblk
->blkno
= blkno
;
1382 newblk
->magic
= XFS_DIR2_LEAFN_MAGIC
;
1384 * Rebalance the entries across the two leaves, link the new
1385 * block into the leaves.
1387 xfs_dir2_leafn_rebalance(state
, oldblk
, newblk
);
1388 error
= xfs_da3_blk_link(state
, oldblk
, newblk
);
1393 * Insert the new entry in the correct block.
1396 error
= xfs_dir2_leafn_add(oldblk
->bp
, args
, oldblk
->index
);
1398 error
= xfs_dir2_leafn_add(newblk
->bp
, args
, newblk
->index
);
1400 * Update last hashval in each block since we added the name.
1402 oldblk
->hashval
= xfs_dir2_leaf_lasthash(dp
, oldblk
->bp
, NULL
);
1403 newblk
->hashval
= xfs_dir2_leaf_lasthash(dp
, newblk
->bp
, NULL
);
1404 xfs_dir3_leaf_check(dp
, oldblk
->bp
);
1405 xfs_dir3_leaf_check(dp
, newblk
->bp
);
1410 * Check a leaf block and its neighbors to see if the block should be
1411 * collapsed into one or the other neighbor. Always keep the block
1412 * with the smaller block number.
1413 * If the current block is over 50% full, don't try to join it, return 0.
1414 * If the block is empty, fill in the state structure and return 2.
1415 * If it can be collapsed, fill in the state structure and return 1.
1416 * If nothing can be done, return 0.
1419 xfs_dir2_leafn_toosmall(
1420 xfs_da_state_t
*state
, /* btree cursor */
1421 int *action
) /* resulting action to take */
1423 xfs_da_state_blk_t
*blk
; /* leaf block */
1424 xfs_dablk_t blkno
; /* leaf block number */
1425 struct xfs_buf
*bp
; /* leaf buffer */
1426 int bytes
; /* bytes in use */
1427 int count
; /* leaf live entry count */
1428 int error
; /* error return value */
1429 int forward
; /* sibling block direction */
1430 int i
; /* sibling counter */
1431 xfs_dir2_leaf_t
*leaf
; /* leaf structure */
1432 int rval
; /* result from path_shift */
1433 struct xfs_dir3_icleaf_hdr leafhdr
;
1434 struct xfs_dir2_leaf_entry
*ents
;
1435 struct xfs_inode
*dp
= state
->args
->dp
;
1438 * Check for the degenerate case of the block being over 50% full.
1439 * If so, it's not worth even looking to see if we might be able
1440 * to coalesce with a sibling.
1442 blk
= &state
->path
.blk
[state
->path
.active
- 1];
1443 leaf
= blk
->bp
->b_addr
;
1444 dp
->d_ops
->leaf_hdr_from_disk(&leafhdr
, leaf
);
1445 ents
= dp
->d_ops
->leaf_ents_p(leaf
);
1446 xfs_dir3_leaf_check(dp
, blk
->bp
);
1448 count
= leafhdr
.count
- leafhdr
.stale
;
1449 bytes
= dp
->d_ops
->leaf_hdr_size
+ count
* sizeof(ents
[0]);
1450 if (bytes
> (state
->args
->geo
->blksize
>> 1)) {
1452 * Blk over 50%, don't try to join.
1458 * Check for the degenerate case of the block being empty.
1459 * If the block is empty, we'll simply delete it, no need to
1460 * coalesce it with a sibling block. We choose (arbitrarily)
1461 * to merge with the forward block unless it is NULL.
1465 * Make altpath point to the block we want to keep and
1466 * path point to the block we want to drop (this one).
1468 forward
= (leafhdr
.forw
!= 0);
1469 memcpy(&state
->altpath
, &state
->path
, sizeof(state
->path
));
1470 error
= xfs_da3_path_shift(state
, &state
->altpath
, forward
, 0,
1474 *action
= rval
? 2 : 0;
1478 * Examine each sibling block to see if we can coalesce with
1479 * at least 25% free space to spare. We need to figure out
1480 * whether to merge with the forward or the backward block.
1481 * We prefer coalescing with the lower numbered sibling so as
1482 * to shrink a directory over time.
1484 forward
= leafhdr
.forw
< leafhdr
.back
;
1485 for (i
= 0, bp
= NULL
; i
< 2; forward
= !forward
, i
++) {
1486 struct xfs_dir3_icleaf_hdr hdr2
;
1488 blkno
= forward
? leafhdr
.forw
: leafhdr
.back
;
1492 * Read the sibling leaf block.
1494 error
= xfs_dir3_leafn_read(state
->args
->trans
, dp
,
1500 * Count bytes in the two blocks combined.
1502 count
= leafhdr
.count
- leafhdr
.stale
;
1503 bytes
= state
->args
->geo
->blksize
-
1504 (state
->args
->geo
->blksize
>> 2);
1507 dp
->d_ops
->leaf_hdr_from_disk(&hdr2
, leaf
);
1508 ents
= dp
->d_ops
->leaf_ents_p(leaf
);
1509 count
+= hdr2
.count
- hdr2
.stale
;
1510 bytes
-= count
* sizeof(ents
[0]);
1513 * Fits with at least 25% to spare.
1517 xfs_trans_brelse(state
->args
->trans
, bp
);
1520 * Didn't like either block, give up.
1528 * Make altpath point to the block we want to keep (the lower
1529 * numbered block) and path point to the block we want to drop.
1531 memcpy(&state
->altpath
, &state
->path
, sizeof(state
->path
));
1532 if (blkno
< blk
->blkno
)
1533 error
= xfs_da3_path_shift(state
, &state
->altpath
, forward
, 0,
1536 error
= xfs_da3_path_shift(state
, &state
->path
, forward
, 0,
1541 *action
= rval
? 0 : 1;
1546 * Move all the leaf entries from drop_blk to save_blk.
1547 * This is done as part of a join operation.
1550 xfs_dir2_leafn_unbalance(
1551 xfs_da_state_t
*state
, /* cursor */
1552 xfs_da_state_blk_t
*drop_blk
, /* dead block */
1553 xfs_da_state_blk_t
*save_blk
) /* surviving block */
1555 xfs_da_args_t
*args
; /* operation arguments */
1556 xfs_dir2_leaf_t
*drop_leaf
; /* dead leaf structure */
1557 xfs_dir2_leaf_t
*save_leaf
; /* surviving leaf structure */
1558 struct xfs_dir3_icleaf_hdr savehdr
;
1559 struct xfs_dir3_icleaf_hdr drophdr
;
1560 struct xfs_dir2_leaf_entry
*sents
;
1561 struct xfs_dir2_leaf_entry
*dents
;
1562 struct xfs_inode
*dp
= state
->args
->dp
;
1565 ASSERT(drop_blk
->magic
== XFS_DIR2_LEAFN_MAGIC
);
1566 ASSERT(save_blk
->magic
== XFS_DIR2_LEAFN_MAGIC
);
1567 drop_leaf
= drop_blk
->bp
->b_addr
;
1568 save_leaf
= save_blk
->bp
->b_addr
;
1570 dp
->d_ops
->leaf_hdr_from_disk(&savehdr
, save_leaf
);
1571 dp
->d_ops
->leaf_hdr_from_disk(&drophdr
, drop_leaf
);
1572 sents
= dp
->d_ops
->leaf_ents_p(save_leaf
);
1573 dents
= dp
->d_ops
->leaf_ents_p(drop_leaf
);
1576 * If there are any stale leaf entries, take this opportunity
1580 xfs_dir3_leaf_compact(args
, &drophdr
, drop_blk
->bp
);
1582 xfs_dir3_leaf_compact(args
, &savehdr
, save_blk
->bp
);
1585 * Move the entries from drop to the appropriate end of save.
1587 drop_blk
->hashval
= be32_to_cpu(dents
[drophdr
.count
- 1].hashval
);
1588 if (xfs_dir2_leafn_order(dp
, save_blk
->bp
, drop_blk
->bp
))
1589 xfs_dir3_leafn_moveents(args
, drop_blk
->bp
, &drophdr
, dents
, 0,
1590 save_blk
->bp
, &savehdr
, sents
, 0,
1593 xfs_dir3_leafn_moveents(args
, drop_blk
->bp
, &drophdr
, dents
, 0,
1594 save_blk
->bp
, &savehdr
, sents
,
1595 savehdr
.count
, drophdr
.count
);
1596 save_blk
->hashval
= be32_to_cpu(sents
[savehdr
.count
- 1].hashval
);
1598 /* log the changes made when moving the entries */
1599 dp
->d_ops
->leaf_hdr_to_disk(save_leaf
, &savehdr
);
1600 dp
->d_ops
->leaf_hdr_to_disk(drop_leaf
, &drophdr
);
1601 xfs_dir3_leaf_log_header(args
, save_blk
->bp
);
1602 xfs_dir3_leaf_log_header(args
, drop_blk
->bp
);
1604 xfs_dir3_leaf_check(dp
, save_blk
->bp
);
1605 xfs_dir3_leaf_check(dp
, drop_blk
->bp
);
1609 * Add a new data block to the directory at the free space index that the caller
1613 xfs_dir2_node_add_datablk(
1614 struct xfs_da_args
*args
,
1615 struct xfs_da_state_blk
*fblk
,
1616 xfs_dir2_db_t
*dbno
,
1617 struct xfs_buf
**dbpp
,
1618 struct xfs_buf
**fbpp
,
1621 struct xfs_inode
*dp
= args
->dp
;
1622 struct xfs_trans
*tp
= args
->trans
;
1623 struct xfs_mount
*mp
= dp
->i_mount
;
1624 struct xfs_dir3_icfree_hdr freehdr
;
1625 struct xfs_dir2_data_free
*bf
;
1626 struct xfs_dir2_data_hdr
*hdr
;
1627 struct xfs_dir2_free
*free
= NULL
;
1629 struct xfs_buf
*fbp
;
1630 struct xfs_buf
*dbp
;
1631 __be16
*bests
= NULL
;
1634 /* Not allowed to allocate, return failure. */
1635 if (args
->total
== 0)
1638 /* Allocate and initialize the new data block. */
1639 error
= xfs_dir2_grow_inode(args
, XFS_DIR2_DATA_SPACE
, dbno
);
1642 error
= xfs_dir3_data_init(args
, *dbno
, &dbp
);
1647 * Get the freespace block corresponding to the data block
1648 * that was just allocated.
1650 fbno
= dp
->d_ops
->db_to_fdb(args
->geo
, *dbno
);
1651 error
= xfs_dir2_free_try_read(tp
, dp
,
1652 xfs_dir2_db_to_da(args
->geo
, fbno
), &fbp
);
1657 * If there wasn't a freespace block, the read will
1658 * return a NULL fbp. Allocate and initialize a new one.
1661 error
= xfs_dir2_grow_inode(args
, XFS_DIR2_FREE_SPACE
, &fbno
);
1665 if (dp
->d_ops
->db_to_fdb(args
->geo
, *dbno
) != fbno
) {
1667 "%s: dir ino %llu needed freesp block %lld for data block %lld, got %lld",
1668 __func__
, (unsigned long long)dp
->i_ino
,
1669 (long long)dp
->d_ops
->db_to_fdb(args
->geo
, *dbno
),
1670 (long long)*dbno
, (long long)fbno
);
1673 " fblk "PTR_FMT
" blkno %llu index %d magic 0x%x",
1674 fblk
, (unsigned long long)fblk
->blkno
,
1675 fblk
->index
, fblk
->magic
);
1677 xfs_alert(mp
, " ... fblk is NULL");
1679 XFS_ERROR_REPORT(__func__
, XFS_ERRLEVEL_LOW
, mp
);
1680 return -EFSCORRUPTED
;
1683 /* Get a buffer for the new block. */
1684 error
= xfs_dir3_free_get_buf(args
, fbno
, &fbp
);
1688 bests
= dp
->d_ops
->free_bests_p(free
);
1689 dp
->d_ops
->free_hdr_from_disk(&freehdr
, free
);
1691 /* Remember the first slot as our empty slot. */
1692 freehdr
.firstdb
= (fbno
- xfs_dir2_byte_to_db(args
->geo
,
1693 XFS_DIR2_FREE_OFFSET
)) *
1694 dp
->d_ops
->free_max_bests(args
->geo
);
1697 bests
= dp
->d_ops
->free_bests_p(free
);
1698 dp
->d_ops
->free_hdr_from_disk(&freehdr
, free
);
1701 /* Set the freespace block index from the data block number. */
1702 *findex
= dp
->d_ops
->db_to_fdindex(args
->geo
, *dbno
);
1704 /* Extend the freespace table if the new data block is off the end. */
1705 if (*findex
>= freehdr
.nvalid
) {
1706 ASSERT(*findex
< dp
->d_ops
->free_max_bests(args
->geo
));
1707 freehdr
.nvalid
= *findex
+ 1;
1708 bests
[*findex
] = cpu_to_be16(NULLDATAOFF
);
1712 * If this entry was for an empty data block (this should always be
1713 * true) then update the header.
1715 if (bests
[*findex
] == cpu_to_be16(NULLDATAOFF
)) {
1717 dp
->d_ops
->free_hdr_to_disk(fbp
->b_addr
, &freehdr
);
1718 xfs_dir2_free_log_header(args
, fbp
);
1721 /* Update the freespace value for the new block in the table. */
1723 bf
= dp
->d_ops
->data_bestfree_p(hdr
);
1724 bests
[*findex
] = bf
[0].length
;
1732 xfs_dir2_node_find_freeblk(
1733 struct xfs_da_args
*args
,
1734 struct xfs_da_state_blk
*fblk
,
1735 xfs_dir2_db_t
*dbnop
,
1736 struct xfs_buf
**fbpp
,
1740 struct xfs_dir3_icfree_hdr freehdr
;
1741 struct xfs_dir2_free
*free
= NULL
;
1742 struct xfs_inode
*dp
= args
->dp
;
1743 struct xfs_trans
*tp
= args
->trans
;
1744 struct xfs_buf
*fbp
= NULL
;
1745 xfs_dir2_db_t lastfbno
;
1746 xfs_dir2_db_t ifbno
= -1;
1747 xfs_dir2_db_t dbno
= -1;
1748 xfs_dir2_db_t fbno
= -1;
1755 * If we came in with a freespace block that means that lookup
1756 * found an entry with our hash value. This is the freespace
1757 * block for that data entry.
1762 findex
= fblk
->index
;
1764 /* caller already found the freespace for us. */
1765 bests
= dp
->d_ops
->free_bests_p(free
);
1766 dp
->d_ops
->free_hdr_from_disk(&freehdr
, free
);
1768 ASSERT(findex
< freehdr
.nvalid
);
1769 ASSERT(be16_to_cpu(bests
[findex
]) != NULLDATAOFF
);
1770 ASSERT(be16_to_cpu(bests
[findex
]) >= length
);
1771 dbno
= freehdr
.firstdb
+ findex
;
1776 * The data block looked at didn't have enough room.
1777 * We'll start at the beginning of the freespace entries.
1779 ifbno
= fblk
->blkno
;
1786 * If we don't have a data block yet, we're going to scan the freespace
1787 * blocks looking for one. Figure out what the highest freespace block
1790 error
= xfs_bmap_last_offset(dp
, &fo
, XFS_DATA_FORK
);
1793 lastfbno
= xfs_dir2_da_to_db(args
->geo
, (xfs_dablk_t
)fo
);
1795 /* If we haven't get a search start block, set it now */
1797 fbno
= xfs_dir2_byte_to_db(args
->geo
, XFS_DIR2_FREE_OFFSET
);
1800 * While we haven't identified a data block, search the freeblock
1801 * data for a good data block. If we find a null freeblock entry,
1802 * indicating a hole in the data blocks, remember that.
1804 while (dbno
== -1) {
1806 * If we don't have a freeblock in hand, get the next one.
1810 * If it's ifbno we already looked at it.
1812 if (++fbno
== ifbno
)
1815 * If it's off the end we're done.
1817 if (fbno
>= lastfbno
)
1820 * Read the block. There can be holes in the
1821 * freespace blocks, so this might not succeed.
1822 * This should be really rare, so there's no reason
1825 error
= xfs_dir2_free_try_read(tp
, dp
,
1826 xfs_dir2_db_to_da(args
->geo
, fbno
),
1836 * Look at the current free entry. Is it good enough?
1838 * The bests initialisation should be where the bufer is read in
1839 * the above branch. But gcc is too stupid to realise that bests
1840 * and the freehdr are actually initialised if they are placed
1841 * there, so we have to do it here to avoid warnings. Blech.
1843 bests
= dp
->d_ops
->free_bests_p(free
);
1844 dp
->d_ops
->free_hdr_from_disk(&freehdr
, free
);
1845 if (be16_to_cpu(bests
[findex
]) != NULLDATAOFF
&&
1846 be16_to_cpu(bests
[findex
]) >= length
)
1847 dbno
= freehdr
.firstdb
+ findex
;
1850 * Are we done with the freeblock?
1852 if (++findex
== freehdr
.nvalid
) {
1856 xfs_trans_brelse(tp
, fbp
);
1858 if (fblk
&& fblk
->bp
)
1872 * Add the data entry for a node-format directory name addition.
1873 * The leaf entry is added in xfs_dir2_leafn_add.
1874 * We may enter with a freespace block that the lookup found.
1877 xfs_dir2_node_addname_int(
1878 struct xfs_da_args
*args
, /* operation arguments */
1879 struct xfs_da_state_blk
*fblk
) /* optional freespace block */
1881 struct xfs_dir2_data_unused
*dup
; /* data unused entry pointer */
1882 struct xfs_dir2_data_entry
*dep
; /* data entry pointer */
1883 struct xfs_dir2_data_hdr
*hdr
; /* data block header */
1884 struct xfs_dir2_data_free
*bf
;
1885 struct xfs_dir2_free
*free
= NULL
; /* freespace block structure */
1886 struct xfs_trans
*tp
= args
->trans
;
1887 struct xfs_inode
*dp
= args
->dp
;
1888 struct xfs_buf
*dbp
; /* data block buffer */
1889 struct xfs_buf
*fbp
; /* freespace buffer */
1890 xfs_dir2_data_aoff_t aoff
;
1891 xfs_dir2_db_t dbno
; /* data block number */
1892 int error
; /* error return value */
1893 int findex
; /* freespace entry index */
1894 int length
; /* length of the new entry */
1895 int logfree
= 0; /* need to log free entry */
1896 int needlog
= 0; /* need to log data header */
1897 int needscan
= 0; /* need to rescan data frees */
1898 __be16
*tagp
; /* data entry tag pointer */
1901 length
= dp
->d_ops
->data_entsize(args
->namelen
);
1902 error
= xfs_dir2_node_find_freeblk(args
, fblk
, &dbno
, &fbp
, &findex
,
1908 * Now we know if we must allocate blocks, so if we are checking whether
1909 * we can insert without allocation then we can return now.
1911 if (args
->op_flags
& XFS_DA_OP_JUSTCHECK
) {
1918 * If we don't have a data block, we need to allocate one and make
1919 * the freespace entries refer to it.
1922 /* we're going to have to log the free block index later */
1924 error
= xfs_dir2_node_add_datablk(args
, fblk
, &dbno
, &dbp
, &fbp
,
1927 /* Read the data block in. */
1928 error
= xfs_dir3_data_read(tp
, dp
,
1929 xfs_dir2_db_to_da(args
->geo
, dbno
),
1935 /* setup for data block up now */
1937 bf
= dp
->d_ops
->data_bestfree_p(hdr
);
1938 ASSERT(be16_to_cpu(bf
[0].length
) >= length
);
1940 /* Point to the existing unused space. */
1941 dup
= (xfs_dir2_data_unused_t
*)
1942 ((char *)hdr
+ be16_to_cpu(bf
[0].offset
));
1944 /* Mark the first part of the unused space, inuse for us. */
1945 aoff
= (xfs_dir2_data_aoff_t
)((char *)dup
- (char *)hdr
);
1946 error
= xfs_dir2_data_use_free(args
, dbp
, dup
, aoff
, length
,
1947 &needlog
, &needscan
);
1949 xfs_trans_brelse(tp
, dbp
);
1953 /* Fill in the new entry and log it. */
1954 dep
= (xfs_dir2_data_entry_t
*)dup
;
1955 dep
->inumber
= cpu_to_be64(args
->inumber
);
1956 dep
->namelen
= args
->namelen
;
1957 memcpy(dep
->name
, args
->name
, dep
->namelen
);
1958 dp
->d_ops
->data_put_ftype(dep
, args
->filetype
);
1959 tagp
= dp
->d_ops
->data_entry_tag_p(dep
);
1960 *tagp
= cpu_to_be16((char *)dep
- (char *)hdr
);
1961 xfs_dir2_data_log_entry(args
, dbp
, dep
);
1963 /* Rescan the freespace and log the data block if needed. */
1965 xfs_dir2_data_freescan(dp
, hdr
, &needlog
);
1967 xfs_dir2_data_log_header(args
, dbp
);
1969 /* If the freespace block entry is now wrong, update it. */
1971 bests
= dp
->d_ops
->free_bests_p(free
);
1972 if (bests
[findex
] != bf
[0].length
) {
1973 bests
[findex
] = bf
[0].length
;
1977 /* Log the freespace entry if needed. */
1979 xfs_dir2_free_log_bests(args
, fbp
, findex
, findex
);
1981 /* Return the data block and offset in args. */
1982 args
->blkno
= (xfs_dablk_t
)dbno
;
1983 args
->index
= be16_to_cpu(*tagp
);
1988 * Top-level node form directory addname routine.
1991 xfs_dir2_node_addname(
1992 xfs_da_args_t
*args
) /* operation arguments */
1994 xfs_da_state_blk_t
*blk
; /* leaf block for insert */
1995 int error
; /* error return value */
1996 int rval
; /* sub-return value */
1997 xfs_da_state_t
*state
; /* btree cursor */
1999 trace_xfs_dir2_node_addname(args
);
2002 * Allocate and initialize the state (btree cursor).
2004 state
= xfs_da_state_alloc();
2006 state
->mp
= args
->dp
->i_mount
;
2008 * Look up the name. We're not supposed to find it, but
2009 * this gives us the insertion point.
2011 error
= xfs_da3_node_lookup_int(state
, &rval
);
2014 if (rval
!= -ENOENT
) {
2018 * Add the data entry to a data block.
2019 * Extravalid is set to a freeblock found by lookup.
2021 rval
= xfs_dir2_node_addname_int(args
,
2022 state
->extravalid
? &state
->extrablk
: NULL
);
2026 blk
= &state
->path
.blk
[state
->path
.active
- 1];
2027 ASSERT(blk
->magic
== XFS_DIR2_LEAFN_MAGIC
);
2029 * Add the new leaf entry.
2031 rval
= xfs_dir2_leafn_add(blk
->bp
, args
, blk
->index
);
2034 * It worked, fix the hash values up the btree.
2036 if (!(args
->op_flags
& XFS_DA_OP_JUSTCHECK
))
2037 xfs_da3_fixhashpath(state
, &state
->path
);
2040 * It didn't work, we need to split the leaf block.
2042 if (args
->total
== 0) {
2043 ASSERT(rval
== -ENOSPC
);
2047 * Split the leaf block and insert the new entry.
2049 rval
= xfs_da3_split(state
);
2052 xfs_da_state_free(state
);
2057 * Lookup an entry in a node-format directory.
2058 * All the real work happens in xfs_da3_node_lookup_int.
2059 * The only real output is the inode number of the entry.
2062 xfs_dir2_node_lookup(
2063 xfs_da_args_t
*args
) /* operation arguments */
2065 int error
; /* error return value */
2066 int i
; /* btree level */
2067 int rval
; /* operation return value */
2068 xfs_da_state_t
*state
; /* btree cursor */
2070 trace_xfs_dir2_node_lookup(args
);
2073 * Allocate and initialize the btree cursor.
2075 state
= xfs_da_state_alloc();
2077 state
->mp
= args
->dp
->i_mount
;
2079 * Fill in the path to the entry in the cursor.
2081 error
= xfs_da3_node_lookup_int(state
, &rval
);
2084 else if (rval
== -ENOENT
&& args
->cmpresult
== XFS_CMP_CASE
) {
2085 /* If a CI match, dup the actual name and return -EEXIST */
2086 xfs_dir2_data_entry_t
*dep
;
2088 dep
= (xfs_dir2_data_entry_t
*)
2089 ((char *)state
->extrablk
.bp
->b_addr
+
2090 state
->extrablk
.index
);
2091 rval
= xfs_dir_cilookup_result(args
, dep
->name
, dep
->namelen
);
2094 * Release the btree blocks and leaf block.
2096 for (i
= 0; i
< state
->path
.active
; i
++) {
2097 xfs_trans_brelse(args
->trans
, state
->path
.blk
[i
].bp
);
2098 state
->path
.blk
[i
].bp
= NULL
;
2101 * Release the data block if we have it.
2103 if (state
->extravalid
&& state
->extrablk
.bp
) {
2104 xfs_trans_brelse(args
->trans
, state
->extrablk
.bp
);
2105 state
->extrablk
.bp
= NULL
;
2107 xfs_da_state_free(state
);
2112 * Remove an entry from a node-format directory.
2115 xfs_dir2_node_removename(
2116 struct xfs_da_args
*args
) /* operation arguments */
2118 struct xfs_da_state_blk
*blk
; /* leaf block */
2119 int error
; /* error return value */
2120 int rval
; /* operation return value */
2121 struct xfs_da_state
*state
; /* btree cursor */
2123 trace_xfs_dir2_node_removename(args
);
2126 * Allocate and initialize the btree cursor.
2128 state
= xfs_da_state_alloc();
2130 state
->mp
= args
->dp
->i_mount
;
2132 /* Look up the entry we're deleting, set up the cursor. */
2133 error
= xfs_da3_node_lookup_int(state
, &rval
);
2137 /* Didn't find it, upper layer screwed up. */
2138 if (rval
!= -EEXIST
) {
2143 blk
= &state
->path
.blk
[state
->path
.active
- 1];
2144 ASSERT(blk
->magic
== XFS_DIR2_LEAFN_MAGIC
);
2145 ASSERT(state
->extravalid
);
2147 * Remove the leaf and data entries.
2148 * Extrablk refers to the data block.
2150 error
= xfs_dir2_leafn_remove(args
, blk
->bp
, blk
->index
,
2151 &state
->extrablk
, &rval
);
2155 * Fix the hash values up the btree.
2157 xfs_da3_fixhashpath(state
, &state
->path
);
2159 * If we need to join leaf blocks, do it.
2161 if (rval
&& state
->path
.active
> 1)
2162 error
= xfs_da3_join(state
);
2164 * If no errors so far, try conversion to leaf format.
2167 error
= xfs_dir2_node_to_leaf(state
);
2169 xfs_da_state_free(state
);
2174 * Replace an entry's inode number in a node-format directory.
2177 xfs_dir2_node_replace(
2178 xfs_da_args_t
*args
) /* operation arguments */
2180 xfs_da_state_blk_t
*blk
; /* leaf block */
2181 xfs_dir2_data_hdr_t
*hdr
; /* data block header */
2182 xfs_dir2_data_entry_t
*dep
; /* data entry changed */
2183 int error
; /* error return value */
2184 int i
; /* btree level */
2185 xfs_ino_t inum
; /* new inode number */
2186 int ftype
; /* new file type */
2187 xfs_dir2_leaf_t
*leaf
; /* leaf structure */
2188 xfs_dir2_leaf_entry_t
*lep
; /* leaf entry being changed */
2189 int rval
; /* internal return value */
2190 xfs_da_state_t
*state
; /* btree cursor */
2192 trace_xfs_dir2_node_replace(args
);
2195 * Allocate and initialize the btree cursor.
2197 state
= xfs_da_state_alloc();
2199 state
->mp
= args
->dp
->i_mount
;
2202 * We have to save new inode number and ftype since
2203 * xfs_da3_node_lookup_int() is going to overwrite them
2205 inum
= args
->inumber
;
2206 ftype
= args
->filetype
;
2209 * Lookup the entry to change in the btree.
2211 error
= xfs_da3_node_lookup_int(state
, &rval
);
2216 * It should be found, since the vnodeops layer has looked it up
2217 * and locked it. But paranoia is good.
2219 if (rval
== -EEXIST
) {
2220 struct xfs_dir2_leaf_entry
*ents
;
2222 * Find the leaf entry.
2224 blk
= &state
->path
.blk
[state
->path
.active
- 1];
2225 ASSERT(blk
->magic
== XFS_DIR2_LEAFN_MAGIC
);
2226 leaf
= blk
->bp
->b_addr
;
2227 ents
= args
->dp
->d_ops
->leaf_ents_p(leaf
);
2228 lep
= &ents
[blk
->index
];
2229 ASSERT(state
->extravalid
);
2231 * Point to the data entry.
2233 hdr
= state
->extrablk
.bp
->b_addr
;
2234 ASSERT(hdr
->magic
== cpu_to_be32(XFS_DIR2_DATA_MAGIC
) ||
2235 hdr
->magic
== cpu_to_be32(XFS_DIR3_DATA_MAGIC
));
2236 dep
= (xfs_dir2_data_entry_t
*)
2238 xfs_dir2_dataptr_to_off(args
->geo
,
2239 be32_to_cpu(lep
->address
)));
2240 ASSERT(inum
!= be64_to_cpu(dep
->inumber
));
2242 * Fill in the new inode number and log the entry.
2244 dep
->inumber
= cpu_to_be64(inum
);
2245 args
->dp
->d_ops
->data_put_ftype(dep
, ftype
);
2246 xfs_dir2_data_log_entry(args
, state
->extrablk
.bp
, dep
);
2250 * Didn't find it, and we're holding a data block. Drop it.
2252 else if (state
->extravalid
) {
2253 xfs_trans_brelse(args
->trans
, state
->extrablk
.bp
);
2254 state
->extrablk
.bp
= NULL
;
2257 * Release all the buffers in the cursor.
2259 for (i
= 0; i
< state
->path
.active
; i
++) {
2260 xfs_trans_brelse(args
->trans
, state
->path
.blk
[i
].bp
);
2261 state
->path
.blk
[i
].bp
= NULL
;
2263 xfs_da_state_free(state
);
2268 * Trim off a trailing empty freespace block.
2269 * Return (in rvalp) 1 if we did it, 0 if not.
2272 xfs_dir2_node_trim_free(
2273 xfs_da_args_t
*args
, /* operation arguments */
2274 xfs_fileoff_t fo
, /* free block number */
2275 int *rvalp
) /* out: did something */
2277 struct xfs_buf
*bp
; /* freespace buffer */
2278 xfs_inode_t
*dp
; /* incore directory inode */
2279 int error
; /* error return code */
2280 xfs_dir2_free_t
*free
; /* freespace structure */
2281 xfs_trans_t
*tp
; /* transaction pointer */
2282 struct xfs_dir3_icfree_hdr freehdr
;
2290 * Read the freespace block.
2292 error
= xfs_dir2_free_try_read(tp
, dp
, fo
, &bp
);
2296 * There can be holes in freespace. If fo is a hole, there's
2302 dp
->d_ops
->free_hdr_from_disk(&freehdr
, free
);
2305 * If there are used entries, there's nothing to do.
2307 if (freehdr
.nused
> 0) {
2308 xfs_trans_brelse(tp
, bp
);
2312 * Blow the block away.
2314 error
= xfs_dir2_shrink_inode(args
,
2315 xfs_dir2_da_to_db(args
->geo
, (xfs_dablk_t
)fo
), bp
);
2318 * Can't fail with ENOSPC since that only happens with no
2319 * space reservation, when breaking up an extent into two
2320 * pieces. This is the last block of an extent.
2322 ASSERT(error
!= -ENOSPC
);
2323 xfs_trans_brelse(tp
, bp
);
2327 * Return that we succeeded.