2 * Copyright (c) 2000-2005 Silicon Graphics, Inc.
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation.
9 * This program is distributed in the hope that it would be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write the Free Software Foundation,
16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 #include "xfs_types.h"
22 #include "xfs_trans.h"
25 #include "xfs_mount.h"
26 #include "xfs_da_btree.h"
27 #include "xfs_bmap_btree.h"
28 #include "xfs_dinode.h"
29 #include "xfs_inode.h"
31 #include "xfs_dir2_format.h"
32 #include "xfs_dir2_priv.h"
33 #include "xfs_error.h"
34 #include "xfs_trace.h"
37 * Function declarations.
39 static int xfs_dir2_leafn_add(struct xfs_buf
*bp
, xfs_da_args_t
*args
,
42 static void xfs_dir2_leafn_check(struct xfs_inode
*dp
, struct xfs_buf
*bp
);
44 #define xfs_dir2_leafn_check(dp, bp)
46 static void xfs_dir2_leafn_moveents(xfs_da_args_t
*args
, struct xfs_buf
*bp_s
,
47 int start_s
, struct xfs_buf
*bp_d
,
48 int start_d
, int count
);
49 static void xfs_dir2_leafn_rebalance(xfs_da_state_t
*state
,
50 xfs_da_state_blk_t
*blk1
,
51 xfs_da_state_blk_t
*blk2
);
52 static int xfs_dir2_leafn_remove(xfs_da_args_t
*args
, struct xfs_buf
*bp
,
53 int index
, xfs_da_state_blk_t
*dblk
,
55 static int xfs_dir2_node_addname_int(xfs_da_args_t
*args
,
56 xfs_da_state_blk_t
*fblk
);
62 struct xfs_mount
*mp
= bp
->b_target
->bt_mount
;
63 struct xfs_dir2_free_hdr
*hdr
= bp
->b_addr
;
66 block_ok
= hdr
->magic
== cpu_to_be32(XFS_DIR2_FREE_MAGIC
);
68 XFS_CORRUPTION_ERROR("xfs_dir2_free_verify magic",
69 XFS_ERRLEVEL_LOW
, mp
, hdr
);
70 xfs_buf_ioerror(bp
, EFSCORRUPTED
);
75 xfs_dir2_free_write_verify(
78 xfs_dir2_free_verify(bp
);
82 xfs_dir2_free_read_verify(
85 xfs_dir2_free_verify(bp
);
86 bp
->b_pre_io
= xfs_dir2_free_write_verify
;
97 xfs_daddr_t mappedbno
,
100 return xfs_da_read_buf(tp
, dp
, fbno
, mappedbno
, bpp
,
101 XFS_DATA_FORK
, xfs_dir2_free_read_verify
);
106 struct xfs_trans
*tp
,
107 struct xfs_inode
*dp
,
109 struct xfs_buf
**bpp
)
111 return __xfs_dir2_free_read(tp
, dp
, fbno
, -1, bpp
);
115 xfs_dir2_free_try_read(
116 struct xfs_trans
*tp
,
117 struct xfs_inode
*dp
,
119 struct xfs_buf
**bpp
)
121 return __xfs_dir2_free_read(tp
, dp
, fbno
, -2, bpp
);
125 * Log entries from a freespace block.
128 xfs_dir2_free_log_bests(
129 struct xfs_trans
*tp
,
131 int first
, /* first entry to log */
132 int last
) /* last entry to log */
134 xfs_dir2_free_t
*free
; /* freespace structure */
137 ASSERT(free
->hdr
.magic
== cpu_to_be32(XFS_DIR2_FREE_MAGIC
));
138 xfs_trans_log_buf(tp
, bp
,
139 (uint
)((char *)&free
->bests
[first
] - (char *)free
),
140 (uint
)((char *)&free
->bests
[last
] - (char *)free
+
141 sizeof(free
->bests
[0]) - 1));
145 * Log header from a freespace block.
148 xfs_dir2_free_log_header(
149 struct xfs_trans
*tp
,
152 xfs_dir2_free_t
*free
; /* freespace structure */
155 ASSERT(free
->hdr
.magic
== cpu_to_be32(XFS_DIR2_FREE_MAGIC
));
156 xfs_trans_log_buf(tp
, bp
, (uint
)((char *)&free
->hdr
- (char *)free
),
157 (uint
)(sizeof(xfs_dir2_free_hdr_t
) - 1));
161 * Convert a leaf-format directory to a node-format directory.
162 * We need to change the magic number of the leaf block, and copy
163 * the freespace table out of the leaf block into its own block.
166 xfs_dir2_leaf_to_node(
167 xfs_da_args_t
*args
, /* operation arguments */
168 struct xfs_buf
*lbp
) /* leaf buffer */
170 xfs_inode_t
*dp
; /* incore directory inode */
171 int error
; /* error return value */
172 struct xfs_buf
*fbp
; /* freespace buffer */
173 xfs_dir2_db_t fdb
; /* freespace block number */
174 xfs_dir2_free_t
*free
; /* freespace structure */
175 __be16
*from
; /* pointer to freespace entry */
176 int i
; /* leaf freespace index */
177 xfs_dir2_leaf_t
*leaf
; /* leaf structure */
178 xfs_dir2_leaf_tail_t
*ltp
; /* leaf tail structure */
179 xfs_mount_t
*mp
; /* filesystem mount point */
180 int n
; /* count of live freespc ents */
181 xfs_dir2_data_off_t off
; /* freespace entry value */
182 __be16
*to
; /* pointer to freespace entry */
183 xfs_trans_t
*tp
; /* transaction pointer */
185 trace_xfs_dir2_leaf_to_node(args
);
191 * Add a freespace block to the directory.
193 if ((error
= xfs_dir2_grow_inode(args
, XFS_DIR2_FREE_SPACE
, &fdb
))) {
196 ASSERT(fdb
== XFS_DIR2_FREE_FIRSTDB(mp
));
198 * Get the buffer for the new freespace block.
200 if ((error
= xfs_da_get_buf(tp
, dp
, xfs_dir2_db_to_da(mp
, fdb
), -1, &fbp
,
207 ltp
= xfs_dir2_leaf_tail_p(mp
, leaf
);
209 * Initialize the freespace block header.
211 free
->hdr
.magic
= cpu_to_be32(XFS_DIR2_FREE_MAGIC
);
212 free
->hdr
.firstdb
= 0;
213 ASSERT(be32_to_cpu(ltp
->bestcount
) <= (uint
)dp
->i_d
.di_size
/ mp
->m_dirblksize
);
214 free
->hdr
.nvalid
= ltp
->bestcount
;
216 * Copy freespace entries from the leaf block to the new block.
217 * Count active entries.
219 for (i
= n
= 0, from
= xfs_dir2_leaf_bests_p(ltp
), to
= free
->bests
;
220 i
< be32_to_cpu(ltp
->bestcount
); i
++, from
++, to
++) {
221 if ((off
= be16_to_cpu(*from
)) != NULLDATAOFF
)
223 *to
= cpu_to_be16(off
);
225 free
->hdr
.nused
= cpu_to_be32(n
);
226 leaf
->hdr
.info
.magic
= cpu_to_be16(XFS_DIR2_LEAFN_MAGIC
);
230 xfs_dir2_leaf_log_header(tp
, lbp
);
231 xfs_dir2_free_log_header(tp
, fbp
);
232 xfs_dir2_free_log_bests(tp
, fbp
, 0, be32_to_cpu(free
->hdr
.nvalid
) - 1);
233 xfs_dir2_leafn_check(dp
, lbp
);
238 * Add a leaf entry to a leaf block in a node-form directory.
239 * The other work necessary is done from the caller.
241 static int /* error */
243 struct xfs_buf
*bp
, /* leaf buffer */
244 xfs_da_args_t
*args
, /* operation arguments */
245 int index
) /* insertion pt for new entry */
247 int compact
; /* compacting stale leaves */
248 xfs_inode_t
*dp
; /* incore directory inode */
249 int highstale
; /* next stale entry */
250 xfs_dir2_leaf_t
*leaf
; /* leaf structure */
251 xfs_dir2_leaf_entry_t
*lep
; /* leaf entry */
252 int lfloghigh
; /* high leaf entry logging */
253 int lfloglow
; /* low leaf entry logging */
254 int lowstale
; /* previous stale entry */
255 xfs_mount_t
*mp
; /* filesystem mount point */
256 xfs_trans_t
*tp
; /* transaction pointer */
258 trace_xfs_dir2_leafn_add(args
, index
);
266 * Quick check just to make sure we are not going to index
267 * into other peoples memory
270 return XFS_ERROR(EFSCORRUPTED
);
273 * If there are already the maximum number of leaf entries in
274 * the block, if there are no stale entries it won't fit.
275 * Caller will do a split. If there are stale entries we'll do
279 if (be16_to_cpu(leaf
->hdr
.count
) == xfs_dir2_max_leaf_ents(mp
)) {
280 if (!leaf
->hdr
.stale
)
281 return XFS_ERROR(ENOSPC
);
282 compact
= be16_to_cpu(leaf
->hdr
.stale
) > 1;
285 ASSERT(index
== 0 || be32_to_cpu(leaf
->ents
[index
- 1].hashval
) <= args
->hashval
);
286 ASSERT(index
== be16_to_cpu(leaf
->hdr
.count
) ||
287 be32_to_cpu(leaf
->ents
[index
].hashval
) >= args
->hashval
);
289 if (args
->op_flags
& XFS_DA_OP_JUSTCHECK
)
293 * Compact out all but one stale leaf entry. Leaves behind
294 * the entry closest to index.
297 xfs_dir2_leaf_compact_x1(bp
, &index
, &lowstale
, &highstale
,
298 &lfloglow
, &lfloghigh
);
301 * Set impossible logging indices for this case.
303 else if (leaf
->hdr
.stale
) {
304 lfloglow
= be16_to_cpu(leaf
->hdr
.count
);
309 * Insert the new entry, log everything.
311 lep
= xfs_dir2_leaf_find_entry(leaf
, index
, compact
, lowstale
,
312 highstale
, &lfloglow
, &lfloghigh
);
314 lep
->hashval
= cpu_to_be32(args
->hashval
);
315 lep
->address
= cpu_to_be32(xfs_dir2_db_off_to_dataptr(mp
,
316 args
->blkno
, args
->index
));
317 xfs_dir2_leaf_log_header(tp
, bp
);
318 xfs_dir2_leaf_log_ents(tp
, bp
, lfloglow
, lfloghigh
);
319 xfs_dir2_leafn_check(dp
, bp
);
325 * Check internal consistency of a leafn block.
328 xfs_dir2_leafn_check(
329 struct xfs_inode
*dp
,
332 int i
; /* leaf index */
333 xfs_dir2_leaf_t
*leaf
; /* leaf structure */
334 xfs_mount_t
*mp
; /* filesystem mount point */
335 int stale
; /* count of stale leaves */
339 ASSERT(leaf
->hdr
.info
.magic
== cpu_to_be16(XFS_DIR2_LEAFN_MAGIC
));
340 ASSERT(be16_to_cpu(leaf
->hdr
.count
) <= xfs_dir2_max_leaf_ents(mp
));
341 for (i
= stale
= 0; i
< be16_to_cpu(leaf
->hdr
.count
); i
++) {
342 if (i
+ 1 < be16_to_cpu(leaf
->hdr
.count
)) {
343 ASSERT(be32_to_cpu(leaf
->ents
[i
].hashval
) <=
344 be32_to_cpu(leaf
->ents
[i
+ 1].hashval
));
346 if (leaf
->ents
[i
].address
== cpu_to_be32(XFS_DIR2_NULL_DATAPTR
))
349 ASSERT(be16_to_cpu(leaf
->hdr
.stale
) == stale
);
354 * Return the last hash value in the leaf.
355 * Stale entries are ok.
357 xfs_dahash_t
/* hash value */
358 xfs_dir2_leafn_lasthash(
359 struct xfs_buf
*bp
, /* leaf buffer */
360 int *count
) /* count of entries in leaf */
362 xfs_dir2_leaf_t
*leaf
; /* leaf structure */
365 ASSERT(leaf
->hdr
.info
.magic
== cpu_to_be16(XFS_DIR2_LEAFN_MAGIC
));
367 *count
= be16_to_cpu(leaf
->hdr
.count
);
368 if (!leaf
->hdr
.count
)
370 return be32_to_cpu(leaf
->ents
[be16_to_cpu(leaf
->hdr
.count
) - 1].hashval
);
374 * Look up a leaf entry for space to add a name in a node-format leaf block.
375 * The extrablk in state is a freespace block.
378 xfs_dir2_leafn_lookup_for_addname(
379 struct xfs_buf
*bp
, /* leaf buffer */
380 xfs_da_args_t
*args
, /* operation arguments */
381 int *indexp
, /* out: leaf entry index */
382 xfs_da_state_t
*state
) /* state to fill in */
384 struct xfs_buf
*curbp
= NULL
; /* current data/free buffer */
385 xfs_dir2_db_t curdb
= -1; /* current data block number */
386 xfs_dir2_db_t curfdb
= -1; /* current free block number */
387 xfs_inode_t
*dp
; /* incore directory inode */
388 int error
; /* error return value */
389 int fi
; /* free entry index */
390 xfs_dir2_free_t
*free
= NULL
; /* free block structure */
391 int index
; /* leaf entry index */
392 xfs_dir2_leaf_t
*leaf
; /* leaf structure */
393 int length
; /* length of new data entry */
394 xfs_dir2_leaf_entry_t
*lep
; /* leaf entry */
395 xfs_mount_t
*mp
; /* filesystem mount point */
396 xfs_dir2_db_t newdb
; /* new data block number */
397 xfs_dir2_db_t newfdb
; /* new free block number */
398 xfs_trans_t
*tp
; /* transaction pointer */
404 ASSERT(leaf
->hdr
.info
.magic
== cpu_to_be16(XFS_DIR2_LEAFN_MAGIC
));
406 ASSERT(be16_to_cpu(leaf
->hdr
.count
) > 0);
408 xfs_dir2_leafn_check(dp
, bp
);
410 * Look up the hash value in the leaf entries.
412 index
= xfs_dir2_leaf_search_hash(args
, bp
);
414 * Do we have a buffer coming in?
416 if (state
->extravalid
) {
417 /* If so, it's a free block buffer, get the block number. */
418 curbp
= state
->extrablk
.bp
;
419 curfdb
= state
->extrablk
.blkno
;
420 free
= curbp
->b_addr
;
421 ASSERT(free
->hdr
.magic
== cpu_to_be32(XFS_DIR2_FREE_MAGIC
));
423 length
= xfs_dir2_data_entsize(args
->namelen
);
425 * Loop over leaf entries with the right hash value.
427 for (lep
= &leaf
->ents
[index
]; index
< be16_to_cpu(leaf
->hdr
.count
) &&
428 be32_to_cpu(lep
->hashval
) == args
->hashval
;
431 * Skip stale leaf entries.
433 if (be32_to_cpu(lep
->address
) == XFS_DIR2_NULL_DATAPTR
)
436 * Pull the data block number from the entry.
438 newdb
= xfs_dir2_dataptr_to_db(mp
, be32_to_cpu(lep
->address
));
440 * For addname, we're looking for a place to put the new entry.
441 * We want to use a data block with an entry of equal
442 * hash value to ours if there is one with room.
444 * If this block isn't the data block we already have
445 * in hand, take a look at it.
447 if (newdb
!= curdb
) {
450 * Convert the data block to the free block
451 * holding its freespace information.
453 newfdb
= xfs_dir2_db_to_fdb(mp
, newdb
);
455 * If it's not the one we have in hand, read it in.
457 if (newfdb
!= curfdb
) {
459 * If we had one before, drop it.
462 xfs_trans_brelse(tp
, curbp
);
464 error
= xfs_dir2_free_read(tp
, dp
,
465 xfs_dir2_db_to_da(mp
, newfdb
),
469 free
= curbp
->b_addr
;
470 ASSERT(be32_to_cpu(free
->hdr
.magic
) ==
471 XFS_DIR2_FREE_MAGIC
);
472 ASSERT((be32_to_cpu(free
->hdr
.firstdb
) %
473 xfs_dir2_free_max_bests(mp
)) == 0);
474 ASSERT(be32_to_cpu(free
->hdr
.firstdb
) <= curdb
);
475 ASSERT(curdb
< be32_to_cpu(free
->hdr
.firstdb
) +
476 be32_to_cpu(free
->hdr
.nvalid
));
479 * Get the index for our entry.
481 fi
= xfs_dir2_db_to_fdindex(mp
, curdb
);
483 * If it has room, return it.
485 if (unlikely(free
->bests
[fi
] ==
486 cpu_to_be16(NULLDATAOFF
))) {
487 XFS_ERROR_REPORT("xfs_dir2_leafn_lookup_int",
488 XFS_ERRLEVEL_LOW
, mp
);
489 if (curfdb
!= newfdb
)
490 xfs_trans_brelse(tp
, curbp
);
491 return XFS_ERROR(EFSCORRUPTED
);
494 if (be16_to_cpu(free
->bests
[fi
]) >= length
)
498 /* Didn't find any space */
501 ASSERT(args
->op_flags
& XFS_DA_OP_OKNOENT
);
503 /* Giving back a free block. */
504 state
->extravalid
= 1;
505 state
->extrablk
.bp
= curbp
;
506 state
->extrablk
.index
= fi
;
507 state
->extrablk
.blkno
= curfdb
;
508 state
->extrablk
.magic
= XFS_DIR2_FREE_MAGIC
;
510 state
->extravalid
= 0;
513 * Return the index, that will be the insertion point.
516 return XFS_ERROR(ENOENT
);
520 * Look up a leaf entry in a node-format leaf block.
521 * The extrablk in state a data block.
524 xfs_dir2_leafn_lookup_for_entry(
525 struct xfs_buf
*bp
, /* leaf buffer */
526 xfs_da_args_t
*args
, /* operation arguments */
527 int *indexp
, /* out: leaf entry index */
528 xfs_da_state_t
*state
) /* state to fill in */
530 struct xfs_buf
*curbp
= NULL
; /* current data/free buffer */
531 xfs_dir2_db_t curdb
= -1; /* current data block number */
532 xfs_dir2_data_entry_t
*dep
; /* data block entry */
533 xfs_inode_t
*dp
; /* incore directory inode */
534 int error
; /* error return value */
535 int index
; /* leaf entry index */
536 xfs_dir2_leaf_t
*leaf
; /* leaf structure */
537 xfs_dir2_leaf_entry_t
*lep
; /* leaf entry */
538 xfs_mount_t
*mp
; /* filesystem mount point */
539 xfs_dir2_db_t newdb
; /* new data block number */
540 xfs_trans_t
*tp
; /* transaction pointer */
541 enum xfs_dacmp cmp
; /* comparison result */
547 ASSERT(leaf
->hdr
.info
.magic
== cpu_to_be16(XFS_DIR2_LEAFN_MAGIC
));
549 ASSERT(be16_to_cpu(leaf
->hdr
.count
) > 0);
551 xfs_dir2_leafn_check(dp
, bp
);
553 * Look up the hash value in the leaf entries.
555 index
= xfs_dir2_leaf_search_hash(args
, bp
);
557 * Do we have a buffer coming in?
559 if (state
->extravalid
) {
560 curbp
= state
->extrablk
.bp
;
561 curdb
= state
->extrablk
.blkno
;
564 * Loop over leaf entries with the right hash value.
566 for (lep
= &leaf
->ents
[index
]; index
< be16_to_cpu(leaf
->hdr
.count
) &&
567 be32_to_cpu(lep
->hashval
) == args
->hashval
;
570 * Skip stale leaf entries.
572 if (be32_to_cpu(lep
->address
) == XFS_DIR2_NULL_DATAPTR
)
575 * Pull the data block number from the entry.
577 newdb
= xfs_dir2_dataptr_to_db(mp
, be32_to_cpu(lep
->address
));
579 * Not adding a new entry, so we really want to find
580 * the name given to us.
582 * If it's a different data block, go get it.
584 if (newdb
!= curdb
) {
586 * If we had a block before that we aren't saving
587 * for a CI name, drop it
589 if (curbp
&& (args
->cmpresult
== XFS_CMP_DIFFERENT
||
590 curdb
!= state
->extrablk
.blkno
))
591 xfs_trans_brelse(tp
, curbp
);
593 * If needing the block that is saved with a CI match,
594 * use it otherwise read in the new data block.
596 if (args
->cmpresult
!= XFS_CMP_DIFFERENT
&&
597 newdb
== state
->extrablk
.blkno
) {
598 ASSERT(state
->extravalid
);
599 curbp
= state
->extrablk
.bp
;
601 error
= xfs_dir2_data_read(tp
, dp
,
602 xfs_dir2_db_to_da(mp
, newdb
),
607 xfs_dir2_data_check(dp
, curbp
);
611 * Point to the data entry.
613 dep
= (xfs_dir2_data_entry_t
*)((char *)curbp
->b_addr
+
614 xfs_dir2_dataptr_to_off(mp
, be32_to_cpu(lep
->address
)));
616 * Compare the entry and if it's an exact match, return
617 * EEXIST immediately. If it's the first case-insensitive
618 * match, store the block & inode number and continue looking.
620 cmp
= mp
->m_dirnameops
->compname(args
, dep
->name
, dep
->namelen
);
621 if (cmp
!= XFS_CMP_DIFFERENT
&& cmp
!= args
->cmpresult
) {
622 /* If there is a CI match block, drop it */
623 if (args
->cmpresult
!= XFS_CMP_DIFFERENT
&&
624 curdb
!= state
->extrablk
.blkno
)
625 xfs_trans_brelse(tp
, state
->extrablk
.bp
);
626 args
->cmpresult
= cmp
;
627 args
->inumber
= be64_to_cpu(dep
->inumber
);
629 state
->extravalid
= 1;
630 state
->extrablk
.bp
= curbp
;
631 state
->extrablk
.blkno
= curdb
;
632 state
->extrablk
.index
= (int)((char *)dep
-
633 (char *)curbp
->b_addr
);
634 state
->extrablk
.magic
= XFS_DIR2_DATA_MAGIC
;
635 if (cmp
== XFS_CMP_EXACT
)
636 return XFS_ERROR(EEXIST
);
639 ASSERT(index
== be16_to_cpu(leaf
->hdr
.count
) ||
640 (args
->op_flags
& XFS_DA_OP_OKNOENT
));
642 if (args
->cmpresult
== XFS_CMP_DIFFERENT
) {
643 /* Giving back last used data block. */
644 state
->extravalid
= 1;
645 state
->extrablk
.bp
= curbp
;
646 state
->extrablk
.index
= -1;
647 state
->extrablk
.blkno
= curdb
;
648 state
->extrablk
.magic
= XFS_DIR2_DATA_MAGIC
;
650 /* If the curbp is not the CI match block, drop it */
651 if (state
->extrablk
.bp
!= curbp
)
652 xfs_trans_brelse(tp
, curbp
);
655 state
->extravalid
= 0;
658 return XFS_ERROR(ENOENT
);
662 * Look up a leaf entry in a node-format leaf block.
663 * If this is an addname then the extrablk in state is a freespace block,
664 * otherwise it's a data block.
667 xfs_dir2_leafn_lookup_int(
668 struct xfs_buf
*bp
, /* leaf buffer */
669 xfs_da_args_t
*args
, /* operation arguments */
670 int *indexp
, /* out: leaf entry index */
671 xfs_da_state_t
*state
) /* state to fill in */
673 if (args
->op_flags
& XFS_DA_OP_ADDNAME
)
674 return xfs_dir2_leafn_lookup_for_addname(bp
, args
, indexp
,
676 return xfs_dir2_leafn_lookup_for_entry(bp
, args
, indexp
, state
);
680 * Move count leaf entries from source to destination leaf.
681 * Log entries and headers. Stale entries are preserved.
684 xfs_dir2_leafn_moveents(
685 xfs_da_args_t
*args
, /* operation arguments */
686 struct xfs_buf
*bp_s
, /* source leaf buffer */
687 int start_s
, /* source leaf index */
688 struct xfs_buf
*bp_d
, /* destination leaf buffer */
689 int start_d
, /* destination leaf index */
690 int count
) /* count of leaves to copy */
692 xfs_dir2_leaf_t
*leaf_d
; /* destination leaf structure */
693 xfs_dir2_leaf_t
*leaf_s
; /* source leaf structure */
694 int stale
; /* count stale leaves copied */
695 xfs_trans_t
*tp
; /* transaction pointer */
697 trace_xfs_dir2_leafn_moveents(args
, start_s
, start_d
, count
);
700 * Silently return if nothing to do.
706 leaf_s
= bp_s
->b_addr
;
707 leaf_d
= bp_d
->b_addr
;
709 * If the destination index is not the end of the current
710 * destination leaf entries, open up a hole in the destination
711 * to hold the new entries.
713 if (start_d
< be16_to_cpu(leaf_d
->hdr
.count
)) {
714 memmove(&leaf_d
->ents
[start_d
+ count
], &leaf_d
->ents
[start_d
],
715 (be16_to_cpu(leaf_d
->hdr
.count
) - start_d
) *
716 sizeof(xfs_dir2_leaf_entry_t
));
717 xfs_dir2_leaf_log_ents(tp
, bp_d
, start_d
+ count
,
718 count
+ be16_to_cpu(leaf_d
->hdr
.count
) - 1);
721 * If the source has stale leaves, count the ones in the copy range
722 * so we can update the header correctly.
724 if (leaf_s
->hdr
.stale
) {
725 int i
; /* temp leaf index */
727 for (i
= start_s
, stale
= 0; i
< start_s
+ count
; i
++) {
728 if (leaf_s
->ents
[i
].address
==
729 cpu_to_be32(XFS_DIR2_NULL_DATAPTR
))
735 * Copy the leaf entries from source to destination.
737 memcpy(&leaf_d
->ents
[start_d
], &leaf_s
->ents
[start_s
],
738 count
* sizeof(xfs_dir2_leaf_entry_t
));
739 xfs_dir2_leaf_log_ents(tp
, bp_d
, start_d
, start_d
+ count
- 1);
741 * If there are source entries after the ones we copied,
742 * delete the ones we copied by sliding the next ones down.
744 if (start_s
+ count
< be16_to_cpu(leaf_s
->hdr
.count
)) {
745 memmove(&leaf_s
->ents
[start_s
], &leaf_s
->ents
[start_s
+ count
],
746 count
* sizeof(xfs_dir2_leaf_entry_t
));
747 xfs_dir2_leaf_log_ents(tp
, bp_s
, start_s
, start_s
+ count
- 1);
750 * Update the headers and log them.
752 be16_add_cpu(&leaf_s
->hdr
.count
, -(count
));
753 be16_add_cpu(&leaf_s
->hdr
.stale
, -(stale
));
754 be16_add_cpu(&leaf_d
->hdr
.count
, count
);
755 be16_add_cpu(&leaf_d
->hdr
.stale
, stale
);
756 xfs_dir2_leaf_log_header(tp
, bp_s
);
757 xfs_dir2_leaf_log_header(tp
, bp_d
);
758 xfs_dir2_leafn_check(args
->dp
, bp_s
);
759 xfs_dir2_leafn_check(args
->dp
, bp_d
);
763 * Determine the sort order of two leaf blocks.
764 * Returns 1 if both are valid and leaf2 should be before leaf1, else 0.
767 xfs_dir2_leafn_order(
768 struct xfs_buf
*leaf1_bp
, /* leaf1 buffer */
769 struct xfs_buf
*leaf2_bp
) /* leaf2 buffer */
771 xfs_dir2_leaf_t
*leaf1
; /* leaf1 structure */
772 xfs_dir2_leaf_t
*leaf2
; /* leaf2 structure */
774 leaf1
= leaf1_bp
->b_addr
;
775 leaf2
= leaf2_bp
->b_addr
;
776 ASSERT(leaf1
->hdr
.info
.magic
== cpu_to_be16(XFS_DIR2_LEAFN_MAGIC
));
777 ASSERT(leaf2
->hdr
.info
.magic
== cpu_to_be16(XFS_DIR2_LEAFN_MAGIC
));
778 if (be16_to_cpu(leaf1
->hdr
.count
) > 0 &&
779 be16_to_cpu(leaf2
->hdr
.count
) > 0 &&
780 (be32_to_cpu(leaf2
->ents
[0].hashval
) < be32_to_cpu(leaf1
->ents
[0].hashval
) ||
781 be32_to_cpu(leaf2
->ents
[be16_to_cpu(leaf2
->hdr
.count
) - 1].hashval
) <
782 be32_to_cpu(leaf1
->ents
[be16_to_cpu(leaf1
->hdr
.count
) - 1].hashval
)))
788 * Rebalance leaf entries between two leaf blocks.
789 * This is actually only called when the second block is new,
790 * though the code deals with the general case.
791 * A new entry will be inserted in one of the blocks, and that
792 * entry is taken into account when balancing.
795 xfs_dir2_leafn_rebalance(
796 xfs_da_state_t
*state
, /* btree cursor */
797 xfs_da_state_blk_t
*blk1
, /* first btree block */
798 xfs_da_state_blk_t
*blk2
) /* second btree block */
800 xfs_da_args_t
*args
; /* operation arguments */
801 int count
; /* count (& direction) leaves */
802 int isleft
; /* new goes in left leaf */
803 xfs_dir2_leaf_t
*leaf1
; /* first leaf structure */
804 xfs_dir2_leaf_t
*leaf2
; /* second leaf structure */
805 int mid
; /* midpoint leaf index */
807 int oldstale
; /* old count of stale leaves */
809 int oldsum
; /* old total leaf count */
810 int swap
; /* swapped leaf blocks */
814 * If the block order is wrong, swap the arguments.
816 if ((swap
= xfs_dir2_leafn_order(blk1
->bp
, blk2
->bp
))) {
817 xfs_da_state_blk_t
*tmp
; /* temp for block swap */
823 leaf1
= blk1
->bp
->b_addr
;
824 leaf2
= blk2
->bp
->b_addr
;
825 oldsum
= be16_to_cpu(leaf1
->hdr
.count
) + be16_to_cpu(leaf2
->hdr
.count
);
827 oldstale
= be16_to_cpu(leaf1
->hdr
.stale
) + be16_to_cpu(leaf2
->hdr
.stale
);
831 * If the old leaf count was odd then the new one will be even,
832 * so we need to divide the new count evenly.
835 xfs_dahash_t midhash
; /* middle entry hash value */
837 if (mid
>= be16_to_cpu(leaf1
->hdr
.count
))
838 midhash
= be32_to_cpu(leaf2
->ents
[mid
- be16_to_cpu(leaf1
->hdr
.count
)].hashval
);
840 midhash
= be32_to_cpu(leaf1
->ents
[mid
].hashval
);
841 isleft
= args
->hashval
<= midhash
;
844 * If the old count is even then the new count is odd, so there's
845 * no preferred side for the new entry.
851 * Calculate moved entry count. Positive means left-to-right,
852 * negative means right-to-left. Then move the entries.
854 count
= be16_to_cpu(leaf1
->hdr
.count
) - mid
+ (isleft
== 0);
856 xfs_dir2_leafn_moveents(args
, blk1
->bp
,
857 be16_to_cpu(leaf1
->hdr
.count
) - count
, blk2
->bp
, 0, count
);
859 xfs_dir2_leafn_moveents(args
, blk2
->bp
, 0, blk1
->bp
,
860 be16_to_cpu(leaf1
->hdr
.count
), count
);
861 ASSERT(be16_to_cpu(leaf1
->hdr
.count
) + be16_to_cpu(leaf2
->hdr
.count
) == oldsum
);
862 ASSERT(be16_to_cpu(leaf1
->hdr
.stale
) + be16_to_cpu(leaf2
->hdr
.stale
) == oldstale
);
864 * Mark whether we're inserting into the old or new leaf.
866 if (be16_to_cpu(leaf1
->hdr
.count
) < be16_to_cpu(leaf2
->hdr
.count
))
867 state
->inleaf
= swap
;
868 else if (be16_to_cpu(leaf1
->hdr
.count
) > be16_to_cpu(leaf2
->hdr
.count
))
869 state
->inleaf
= !swap
;
872 swap
^ (blk1
->index
<= be16_to_cpu(leaf1
->hdr
.count
));
874 * Adjust the expected index for insertion.
877 blk2
->index
= blk1
->index
- be16_to_cpu(leaf1
->hdr
.count
);
880 * Finally sanity check just to make sure we are not returning a
883 if(blk2
->index
< 0) {
886 xfs_alert(args
->dp
->i_mount
,
887 "%s: picked the wrong leaf? reverting original leaf: blk1->index %d\n",
888 __func__
, blk1
->index
);
893 xfs_dir2_data_block_free(
895 struct xfs_dir2_data_hdr
*hdr
,
896 struct xfs_dir2_free
*free
,
902 struct xfs_trans
*tp
= args
->trans
;
906 /* One less used entry in the free table. */
907 be32_add_cpu(&free
->hdr
.nused
, -1);
908 xfs_dir2_free_log_header(tp
, fbp
);
911 * If this was the last entry in the table, we can trim the
912 * table size back. There might be other entries at the end
913 * referring to non-existent data blocks, get those too.
915 if (findex
== be32_to_cpu(free
->hdr
.nvalid
) - 1) {
916 int i
; /* free entry index */
918 for (i
= findex
- 1; i
>= 0; i
--) {
919 if (free
->bests
[i
] != cpu_to_be16(NULLDATAOFF
))
922 free
->hdr
.nvalid
= cpu_to_be32(i
+ 1);
925 /* Not the last entry, just punch it out. */
926 free
->bests
[findex
] = cpu_to_be16(NULLDATAOFF
);
930 * If there are no useful entries left in the block,
931 * get rid of the block if we can.
933 if (!free
->hdr
.nused
) {
936 error
= xfs_dir2_shrink_inode(args
, fdb
, fbp
);
940 } else if (error
!= ENOSPC
|| args
->total
!= 0)
943 * It's possible to get ENOSPC if there is no
944 * space reservation. In this case some one
945 * else will eventually get rid of this block.
950 * Data block is not empty, just set the free entry to the new
953 free
->bests
[findex
] = cpu_to_be16(longest
);
957 /* Log the free entry that changed, unless we got rid of it. */
959 xfs_dir2_free_log_bests(tp
, fbp
, findex
, findex
);
964 * Remove an entry from a node directory.
965 * This removes the leaf entry and the data entry,
966 * and updates the free block if necessary.
968 static int /* error */
969 xfs_dir2_leafn_remove(
970 xfs_da_args_t
*args
, /* operation arguments */
971 struct xfs_buf
*bp
, /* leaf buffer */
972 int index
, /* leaf entry index */
973 xfs_da_state_blk_t
*dblk
, /* data block */
974 int *rval
) /* resulting block needs join */
976 xfs_dir2_data_hdr_t
*hdr
; /* data block header */
977 xfs_dir2_db_t db
; /* data block number */
978 struct xfs_buf
*dbp
; /* data block buffer */
979 xfs_dir2_data_entry_t
*dep
; /* data block entry */
980 xfs_inode_t
*dp
; /* incore directory inode */
981 xfs_dir2_leaf_t
*leaf
; /* leaf structure */
982 xfs_dir2_leaf_entry_t
*lep
; /* leaf entry */
983 int longest
; /* longest data free entry */
984 int off
; /* data block entry offset */
985 xfs_mount_t
*mp
; /* filesystem mount point */
986 int needlog
; /* need to log data header */
987 int needscan
; /* need to rescan data frees */
988 xfs_trans_t
*tp
; /* transaction pointer */
990 trace_xfs_dir2_leafn_remove(args
, index
);
996 ASSERT(leaf
->hdr
.info
.magic
== cpu_to_be16(XFS_DIR2_LEAFN_MAGIC
));
998 * Point to the entry we're removing.
1000 lep
= &leaf
->ents
[index
];
1002 * Extract the data block and offset from the entry.
1004 db
= xfs_dir2_dataptr_to_db(mp
, be32_to_cpu(lep
->address
));
1005 ASSERT(dblk
->blkno
== db
);
1006 off
= xfs_dir2_dataptr_to_off(mp
, be32_to_cpu(lep
->address
));
1007 ASSERT(dblk
->index
== off
);
1009 * Kill the leaf entry by marking it stale.
1010 * Log the leaf block changes.
1012 be16_add_cpu(&leaf
->hdr
.stale
, 1);
1013 xfs_dir2_leaf_log_header(tp
, bp
);
1014 lep
->address
= cpu_to_be32(XFS_DIR2_NULL_DATAPTR
);
1015 xfs_dir2_leaf_log_ents(tp
, bp
, index
, index
);
1017 * Make the data entry free. Keep track of the longest freespace
1018 * in the data block in case it changes.
1022 dep
= (xfs_dir2_data_entry_t
*)((char *)hdr
+ off
);
1023 longest
= be16_to_cpu(hdr
->bestfree
[0].length
);
1024 needlog
= needscan
= 0;
1025 xfs_dir2_data_make_free(tp
, dbp
, off
,
1026 xfs_dir2_data_entsize(dep
->namelen
), &needlog
, &needscan
);
1028 * Rescan the data block freespaces for bestfree.
1029 * Log the data block header if needed.
1032 xfs_dir2_data_freescan(mp
, hdr
, &needlog
);
1034 xfs_dir2_data_log_header(tp
, dbp
);
1035 xfs_dir2_data_check(dp
, dbp
);
1037 * If the longest data block freespace changes, need to update
1038 * the corresponding freeblock entry.
1040 if (longest
< be16_to_cpu(hdr
->bestfree
[0].length
)) {
1041 int error
; /* error return value */
1042 struct xfs_buf
*fbp
; /* freeblock buffer */
1043 xfs_dir2_db_t fdb
; /* freeblock block number */
1044 int findex
; /* index in freeblock entries */
1045 xfs_dir2_free_t
*free
; /* freeblock structure */
1048 * Convert the data block number to a free block,
1049 * read in the free block.
1051 fdb
= xfs_dir2_db_to_fdb(mp
, db
);
1052 error
= xfs_dir2_free_read(tp
, dp
, xfs_dir2_db_to_da(mp
, fdb
),
1057 ASSERT(free
->hdr
.magic
== cpu_to_be32(XFS_DIR2_FREE_MAGIC
));
1058 ASSERT(be32_to_cpu(free
->hdr
.firstdb
) ==
1059 xfs_dir2_free_max_bests(mp
) *
1060 (fdb
- XFS_DIR2_FREE_FIRSTDB(mp
)));
1062 * Calculate which entry we need to fix.
1064 findex
= xfs_dir2_db_to_fdindex(mp
, db
);
1065 longest
= be16_to_cpu(hdr
->bestfree
[0].length
);
1067 * If the data block is now empty we can get rid of it
1070 if (longest
== mp
->m_dirblksize
- (uint
)sizeof(*hdr
)) {
1072 * Try to punch out the data block.
1074 error
= xfs_dir2_shrink_inode(args
, db
, dbp
);
1080 * We can get ENOSPC if there's no space reservation.
1081 * In this case just drop the buffer and some one else
1082 * will eventually get rid of the empty block.
1084 else if (!(error
== ENOSPC
&& args
->total
== 0))
1088 * If we got rid of the data block, we can eliminate that entry
1089 * in the free block.
1091 error
= xfs_dir2_data_block_free(args
, hdr
, free
,
1092 fdb
, findex
, fbp
, longest
);
1097 xfs_dir2_leafn_check(dp
, bp
);
1099 * Return indication of whether this leaf block is empty enough
1100 * to justify trying to join it with a neighbor.
1103 ((uint
)sizeof(leaf
->hdr
) +
1104 (uint
)sizeof(leaf
->ents
[0]) *
1105 (be16_to_cpu(leaf
->hdr
.count
) - be16_to_cpu(leaf
->hdr
.stale
))) <
1111 * Split the leaf entries in the old block into old and new blocks.
1114 xfs_dir2_leafn_split(
1115 xfs_da_state_t
*state
, /* btree cursor */
1116 xfs_da_state_blk_t
*oldblk
, /* original block */
1117 xfs_da_state_blk_t
*newblk
) /* newly created block */
1119 xfs_da_args_t
*args
; /* operation arguments */
1120 xfs_dablk_t blkno
; /* new leaf block number */
1121 int error
; /* error return value */
1122 xfs_mount_t
*mp
; /* filesystem mount point */
1125 * Allocate space for a new leaf node.
1128 mp
= args
->dp
->i_mount
;
1129 ASSERT(args
!= NULL
);
1130 ASSERT(oldblk
->magic
== XFS_DIR2_LEAFN_MAGIC
);
1131 error
= xfs_da_grow_inode(args
, &blkno
);
1136 * Initialize the new leaf block.
1138 error
= xfs_dir2_leaf_init(args
, xfs_dir2_da_to_db(mp
, blkno
),
1139 &newblk
->bp
, XFS_DIR2_LEAFN_MAGIC
);
1143 newblk
->blkno
= blkno
;
1144 newblk
->magic
= XFS_DIR2_LEAFN_MAGIC
;
1146 * Rebalance the entries across the two leaves, link the new
1147 * block into the leaves.
1149 xfs_dir2_leafn_rebalance(state
, oldblk
, newblk
);
1150 error
= xfs_da_blk_link(state
, oldblk
, newblk
);
1155 * Insert the new entry in the correct block.
1158 error
= xfs_dir2_leafn_add(oldblk
->bp
, args
, oldblk
->index
);
1160 error
= xfs_dir2_leafn_add(newblk
->bp
, args
, newblk
->index
);
1162 * Update last hashval in each block since we added the name.
1164 oldblk
->hashval
= xfs_dir2_leafn_lasthash(oldblk
->bp
, NULL
);
1165 newblk
->hashval
= xfs_dir2_leafn_lasthash(newblk
->bp
, NULL
);
1166 xfs_dir2_leafn_check(args
->dp
, oldblk
->bp
);
1167 xfs_dir2_leafn_check(args
->dp
, newblk
->bp
);
1172 * Check a leaf block and its neighbors to see if the block should be
1173 * collapsed into one or the other neighbor. Always keep the block
1174 * with the smaller block number.
1175 * If the current block is over 50% full, don't try to join it, return 0.
1176 * If the block is empty, fill in the state structure and return 2.
1177 * If it can be collapsed, fill in the state structure and return 1.
1178 * If nothing can be done, return 0.
1181 xfs_dir2_leafn_toosmall(
1182 xfs_da_state_t
*state
, /* btree cursor */
1183 int *action
) /* resulting action to take */
1185 xfs_da_state_blk_t
*blk
; /* leaf block */
1186 xfs_dablk_t blkno
; /* leaf block number */
1187 struct xfs_buf
*bp
; /* leaf buffer */
1188 int bytes
; /* bytes in use */
1189 int count
; /* leaf live entry count */
1190 int error
; /* error return value */
1191 int forward
; /* sibling block direction */
1192 int i
; /* sibling counter */
1193 xfs_da_blkinfo_t
*info
; /* leaf block header */
1194 xfs_dir2_leaf_t
*leaf
; /* leaf structure */
1195 int rval
; /* result from path_shift */
1198 * Check for the degenerate case of the block being over 50% full.
1199 * If so, it's not worth even looking to see if we might be able
1200 * to coalesce with a sibling.
1202 blk
= &state
->path
.blk
[state
->path
.active
- 1];
1203 info
= blk
->bp
->b_addr
;
1204 ASSERT(info
->magic
== cpu_to_be16(XFS_DIR2_LEAFN_MAGIC
));
1205 leaf
= (xfs_dir2_leaf_t
*)info
;
1206 count
= be16_to_cpu(leaf
->hdr
.count
) - be16_to_cpu(leaf
->hdr
.stale
);
1207 bytes
= (uint
)sizeof(leaf
->hdr
) + count
* (uint
)sizeof(leaf
->ents
[0]);
1208 if (bytes
> (state
->blocksize
>> 1)) {
1210 * Blk over 50%, don't try to join.
1216 * Check for the degenerate case of the block being empty.
1217 * If the block is empty, we'll simply delete it, no need to
1218 * coalesce it with a sibling block. We choose (arbitrarily)
1219 * to merge with the forward block unless it is NULL.
1223 * Make altpath point to the block we want to keep and
1224 * path point to the block we want to drop (this one).
1226 forward
= (info
->forw
!= 0);
1227 memcpy(&state
->altpath
, &state
->path
, sizeof(state
->path
));
1228 error
= xfs_da_path_shift(state
, &state
->altpath
, forward
, 0,
1232 *action
= rval
? 2 : 0;
1236 * Examine each sibling block to see if we can coalesce with
1237 * at least 25% free space to spare. We need to figure out
1238 * whether to merge with the forward or the backward block.
1239 * We prefer coalescing with the lower numbered sibling so as
1240 * to shrink a directory over time.
1242 forward
= be32_to_cpu(info
->forw
) < be32_to_cpu(info
->back
);
1243 for (i
= 0, bp
= NULL
; i
< 2; forward
= !forward
, i
++) {
1244 blkno
= forward
? be32_to_cpu(info
->forw
) : be32_to_cpu(info
->back
);
1248 * Read the sibling leaf block.
1250 error
= xfs_dir2_leafn_read(state
->args
->trans
, state
->args
->dp
,
1256 * Count bytes in the two blocks combined.
1258 leaf
= (xfs_dir2_leaf_t
*)info
;
1259 count
= be16_to_cpu(leaf
->hdr
.count
) - be16_to_cpu(leaf
->hdr
.stale
);
1260 bytes
= state
->blocksize
- (state
->blocksize
>> 2);
1262 ASSERT(leaf
->hdr
.info
.magic
== cpu_to_be16(XFS_DIR2_LEAFN_MAGIC
));
1263 count
+= be16_to_cpu(leaf
->hdr
.count
) - be16_to_cpu(leaf
->hdr
.stale
);
1264 bytes
-= count
* (uint
)sizeof(leaf
->ents
[0]);
1266 * Fits with at least 25% to spare.
1270 xfs_trans_brelse(state
->args
->trans
, bp
);
1273 * Didn't like either block, give up.
1281 * Make altpath point to the block we want to keep (the lower
1282 * numbered block) and path point to the block we want to drop.
1284 memcpy(&state
->altpath
, &state
->path
, sizeof(state
->path
));
1285 if (blkno
< blk
->blkno
)
1286 error
= xfs_da_path_shift(state
, &state
->altpath
, forward
, 0,
1289 error
= xfs_da_path_shift(state
, &state
->path
, forward
, 0,
1294 *action
= rval
? 0 : 1;
1299 * Move all the leaf entries from drop_blk to save_blk.
1300 * This is done as part of a join operation.
1303 xfs_dir2_leafn_unbalance(
1304 xfs_da_state_t
*state
, /* cursor */
1305 xfs_da_state_blk_t
*drop_blk
, /* dead block */
1306 xfs_da_state_blk_t
*save_blk
) /* surviving block */
1308 xfs_da_args_t
*args
; /* operation arguments */
1309 xfs_dir2_leaf_t
*drop_leaf
; /* dead leaf structure */
1310 xfs_dir2_leaf_t
*save_leaf
; /* surviving leaf structure */
1313 ASSERT(drop_blk
->magic
== XFS_DIR2_LEAFN_MAGIC
);
1314 ASSERT(save_blk
->magic
== XFS_DIR2_LEAFN_MAGIC
);
1315 drop_leaf
= drop_blk
->bp
->b_addr
;
1316 save_leaf
= save_blk
->bp
->b_addr
;
1317 ASSERT(drop_leaf
->hdr
.info
.magic
== cpu_to_be16(XFS_DIR2_LEAFN_MAGIC
));
1318 ASSERT(save_leaf
->hdr
.info
.magic
== cpu_to_be16(XFS_DIR2_LEAFN_MAGIC
));
1320 * If there are any stale leaf entries, take this opportunity
1323 if (drop_leaf
->hdr
.stale
)
1324 xfs_dir2_leaf_compact(args
, drop_blk
->bp
);
1325 if (save_leaf
->hdr
.stale
)
1326 xfs_dir2_leaf_compact(args
, save_blk
->bp
);
1328 * Move the entries from drop to the appropriate end of save.
1330 drop_blk
->hashval
= be32_to_cpu(drop_leaf
->ents
[be16_to_cpu(drop_leaf
->hdr
.count
) - 1].hashval
);
1331 if (xfs_dir2_leafn_order(save_blk
->bp
, drop_blk
->bp
))
1332 xfs_dir2_leafn_moveents(args
, drop_blk
->bp
, 0, save_blk
->bp
, 0,
1333 be16_to_cpu(drop_leaf
->hdr
.count
));
1335 xfs_dir2_leafn_moveents(args
, drop_blk
->bp
, 0, save_blk
->bp
,
1336 be16_to_cpu(save_leaf
->hdr
.count
), be16_to_cpu(drop_leaf
->hdr
.count
));
1337 save_blk
->hashval
= be32_to_cpu(save_leaf
->ents
[be16_to_cpu(save_leaf
->hdr
.count
) - 1].hashval
);
1338 xfs_dir2_leafn_check(args
->dp
, save_blk
->bp
);
1342 * Top-level node form directory addname routine.
1345 xfs_dir2_node_addname(
1346 xfs_da_args_t
*args
) /* operation arguments */
1348 xfs_da_state_blk_t
*blk
; /* leaf block for insert */
1349 int error
; /* error return value */
1350 int rval
; /* sub-return value */
1351 xfs_da_state_t
*state
; /* btree cursor */
1353 trace_xfs_dir2_node_addname(args
);
1356 * Allocate and initialize the state (btree cursor).
1358 state
= xfs_da_state_alloc();
1360 state
->mp
= args
->dp
->i_mount
;
1361 state
->blocksize
= state
->mp
->m_dirblksize
;
1362 state
->node_ents
= state
->mp
->m_dir_node_ents
;
1364 * Look up the name. We're not supposed to find it, but
1365 * this gives us the insertion point.
1367 error
= xfs_da_node_lookup_int(state
, &rval
);
1370 if (rval
!= ENOENT
) {
1374 * Add the data entry to a data block.
1375 * Extravalid is set to a freeblock found by lookup.
1377 rval
= xfs_dir2_node_addname_int(args
,
1378 state
->extravalid
? &state
->extrablk
: NULL
);
1382 blk
= &state
->path
.blk
[state
->path
.active
- 1];
1383 ASSERT(blk
->magic
== XFS_DIR2_LEAFN_MAGIC
);
1385 * Add the new leaf entry.
1387 rval
= xfs_dir2_leafn_add(blk
->bp
, args
, blk
->index
);
1390 * It worked, fix the hash values up the btree.
1392 if (!(args
->op_flags
& XFS_DA_OP_JUSTCHECK
))
1393 xfs_da_fixhashpath(state
, &state
->path
);
1396 * It didn't work, we need to split the leaf block.
1398 if (args
->total
== 0) {
1399 ASSERT(rval
== ENOSPC
);
1403 * Split the leaf block and insert the new entry.
1405 rval
= xfs_da_split(state
);
1408 xfs_da_state_free(state
);
1413 * Add the data entry for a node-format directory name addition.
1414 * The leaf entry is added in xfs_dir2_leafn_add.
1415 * We may enter with a freespace block that the lookup found.
1417 static int /* error */
1418 xfs_dir2_node_addname_int(
1419 xfs_da_args_t
*args
, /* operation arguments */
1420 xfs_da_state_blk_t
*fblk
) /* optional freespace block */
1422 xfs_dir2_data_hdr_t
*hdr
; /* data block header */
1423 xfs_dir2_db_t dbno
; /* data block number */
1424 struct xfs_buf
*dbp
; /* data block buffer */
1425 xfs_dir2_data_entry_t
*dep
; /* data entry pointer */
1426 xfs_inode_t
*dp
; /* incore directory inode */
1427 xfs_dir2_data_unused_t
*dup
; /* data unused entry pointer */
1428 int error
; /* error return value */
1429 xfs_dir2_db_t fbno
; /* freespace block number */
1430 struct xfs_buf
*fbp
; /* freespace buffer */
1431 int findex
; /* freespace entry index */
1432 xfs_dir2_free_t
*free
=NULL
; /* freespace block structure */
1433 xfs_dir2_db_t ifbno
; /* initial freespace block no */
1434 xfs_dir2_db_t lastfbno
=0; /* highest freespace block no */
1435 int length
; /* length of the new entry */
1436 int logfree
; /* need to log free entry */
1437 xfs_mount_t
*mp
; /* filesystem mount point */
1438 int needlog
; /* need to log data header */
1439 int needscan
; /* need to rescan data frees */
1440 __be16
*tagp
; /* data entry tag pointer */
1441 xfs_trans_t
*tp
; /* transaction pointer */
1446 length
= xfs_dir2_data_entsize(args
->namelen
);
1448 * If we came in with a freespace block that means that lookup
1449 * found an entry with our hash value. This is the freespace
1450 * block for that data entry.
1455 * Remember initial freespace block number.
1457 ifbno
= fblk
->blkno
;
1459 ASSERT(free
->hdr
.magic
== cpu_to_be32(XFS_DIR2_FREE_MAGIC
));
1460 findex
= fblk
->index
;
1462 * This means the free entry showed that the data block had
1463 * space for our entry, so we remembered it.
1464 * Use that data block.
1467 ASSERT(findex
< be32_to_cpu(free
->hdr
.nvalid
));
1468 ASSERT(be16_to_cpu(free
->bests
[findex
]) != NULLDATAOFF
);
1469 ASSERT(be16_to_cpu(free
->bests
[findex
]) >= length
);
1470 dbno
= be32_to_cpu(free
->hdr
.firstdb
) + findex
;
1473 * The data block looked at didn't have enough room.
1474 * We'll start at the beginning of the freespace entries.
1482 * Didn't come in with a freespace block, so don't have a data block.
1490 * If we don't have a data block yet, we're going to scan the
1491 * freespace blocks looking for one. Figure out what the
1492 * highest freespace block number is.
1495 xfs_fileoff_t fo
; /* freespace block number */
1497 if ((error
= xfs_bmap_last_offset(tp
, dp
, &fo
, XFS_DATA_FORK
)))
1499 lastfbno
= xfs_dir2_da_to_db(mp
, (xfs_dablk_t
)fo
);
1503 * While we haven't identified a data block, search the freeblock
1504 * data for a good data block. If we find a null freeblock entry,
1505 * indicating a hole in the data blocks, remember that.
1507 while (dbno
== -1) {
1509 * If we don't have a freeblock in hand, get the next one.
1513 * Happens the first time through unless lookup gave
1514 * us a freespace block to start with.
1517 fbno
= XFS_DIR2_FREE_FIRSTDB(mp
);
1519 * If it's ifbno we already looked at it.
1524 * If it's off the end we're done.
1526 if (fbno
>= lastfbno
)
1529 * Read the block. There can be holes in the
1530 * freespace blocks, so this might not succeed.
1531 * This should be really rare, so there's no reason
1534 error
= xfs_dir2_free_try_read(tp
, dp
,
1535 xfs_dir2_db_to_da(mp
, fbno
),
1542 ASSERT(free
->hdr
.magic
== cpu_to_be32(XFS_DIR2_FREE_MAGIC
));
1546 * Look at the current free entry. Is it good enough?
1548 if (be16_to_cpu(free
->bests
[findex
]) != NULLDATAOFF
&&
1549 be16_to_cpu(free
->bests
[findex
]) >= length
)
1550 dbno
= be32_to_cpu(free
->hdr
.firstdb
) + findex
;
1553 * Are we done with the freeblock?
1555 if (++findex
== be32_to_cpu(free
->hdr
.nvalid
)) {
1559 xfs_trans_brelse(tp
, fbp
);
1561 if (fblk
&& fblk
->bp
)
1567 * If we don't have a data block, we need to allocate one and make
1568 * the freespace entries refer to it.
1570 if (unlikely(dbno
== -1)) {
1572 * Not allowed to allocate, return failure.
1574 if ((args
->op_flags
& XFS_DA_OP_JUSTCHECK
) || args
->total
== 0)
1575 return XFS_ERROR(ENOSPC
);
1578 * Allocate and initialize the new data block.
1580 if (unlikely((error
= xfs_dir2_grow_inode(args
,
1581 XFS_DIR2_DATA_SPACE
,
1583 (error
= xfs_dir2_data_init(args
, dbno
, &dbp
))))
1587 * If (somehow) we have a freespace block, get rid of it.
1590 xfs_trans_brelse(tp
, fbp
);
1591 if (fblk
&& fblk
->bp
)
1595 * Get the freespace block corresponding to the data block
1596 * that was just allocated.
1598 fbno
= xfs_dir2_db_to_fdb(mp
, dbno
);
1599 error
= xfs_dir2_free_try_read(tp
, dp
,
1600 xfs_dir2_db_to_da(mp
, fbno
),
1606 * If there wasn't a freespace block, the read will
1607 * return a NULL fbp. Allocate and initialize a new one.
1610 if ((error
= xfs_dir2_grow_inode(args
, XFS_DIR2_FREE_SPACE
,
1615 if (unlikely(xfs_dir2_db_to_fdb(mp
, dbno
) != fbno
)) {
1617 "%s: dir ino %llu needed freesp block %lld for\n"
1618 " data block %lld, got %lld ifbno %llu lastfbno %d",
1619 __func__
, (unsigned long long)dp
->i_ino
,
1620 (long long)xfs_dir2_db_to_fdb(mp
, dbno
),
1621 (long long)dbno
, (long long)fbno
,
1622 (unsigned long long)ifbno
, lastfbno
);
1625 " fblk 0x%p blkno %llu index %d magic 0x%x",
1627 (unsigned long long)fblk
->blkno
,
1631 xfs_alert(mp
, " ... fblk is NULL");
1633 XFS_ERROR_REPORT("xfs_dir2_node_addname_int",
1634 XFS_ERRLEVEL_LOW
, mp
);
1635 return XFS_ERROR(EFSCORRUPTED
);
1639 * Get a buffer for the new block.
1641 if ((error
= xfs_da_get_buf(tp
, dp
,
1642 xfs_dir2_db_to_da(mp
, fbno
),
1643 -1, &fbp
, XFS_DATA_FORK
))) {
1646 ASSERT(fbp
!= NULL
);
1649 * Initialize the new block to be empty, and remember
1650 * its first slot as our empty slot.
1653 free
->hdr
.magic
= cpu_to_be32(XFS_DIR2_FREE_MAGIC
);
1654 free
->hdr
.firstdb
= cpu_to_be32(
1655 (fbno
- XFS_DIR2_FREE_FIRSTDB(mp
)) *
1656 xfs_dir2_free_max_bests(mp
));
1657 free
->hdr
.nvalid
= 0;
1658 free
->hdr
.nused
= 0;
1661 ASSERT(free
->hdr
.magic
== cpu_to_be32(XFS_DIR2_FREE_MAGIC
));
1665 * Set the freespace block index from the data block number.
1667 findex
= xfs_dir2_db_to_fdindex(mp
, dbno
);
1669 * If it's after the end of the current entries in the
1670 * freespace block, extend that table.
1672 if (findex
>= be32_to_cpu(free
->hdr
.nvalid
)) {
1673 ASSERT(findex
< xfs_dir2_free_max_bests(mp
));
1674 free
->hdr
.nvalid
= cpu_to_be32(findex
+ 1);
1676 * Tag new entry so nused will go up.
1678 free
->bests
[findex
] = cpu_to_be16(NULLDATAOFF
);
1681 * If this entry was for an empty data block
1682 * (this should always be true) then update the header.
1684 if (free
->bests
[findex
] == cpu_to_be16(NULLDATAOFF
)) {
1685 be32_add_cpu(&free
->hdr
.nused
, 1);
1686 xfs_dir2_free_log_header(tp
, fbp
);
1689 * Update the real value in the table.
1690 * We haven't allocated the data entry yet so this will
1694 free
->bests
[findex
] = hdr
->bestfree
[0].length
;
1698 * We had a data block so we don't have to make a new one.
1702 * If just checking, we succeeded.
1704 if (args
->op_flags
& XFS_DA_OP_JUSTCHECK
)
1708 * Read the data block in.
1710 error
= xfs_dir2_data_read(tp
, dp
, xfs_dir2_db_to_da(mp
, dbno
),
1717 ASSERT(be16_to_cpu(hdr
->bestfree
[0].length
) >= length
);
1719 * Point to the existing unused space.
1721 dup
= (xfs_dir2_data_unused_t
*)
1722 ((char *)hdr
+ be16_to_cpu(hdr
->bestfree
[0].offset
));
1723 needscan
= needlog
= 0;
1725 * Mark the first part of the unused space, inuse for us.
1727 xfs_dir2_data_use_free(tp
, dbp
, dup
,
1728 (xfs_dir2_data_aoff_t
)((char *)dup
- (char *)hdr
), length
,
1729 &needlog
, &needscan
);
1731 * Fill in the new entry and log it.
1733 dep
= (xfs_dir2_data_entry_t
*)dup
;
1734 dep
->inumber
= cpu_to_be64(args
->inumber
);
1735 dep
->namelen
= args
->namelen
;
1736 memcpy(dep
->name
, args
->name
, dep
->namelen
);
1737 tagp
= xfs_dir2_data_entry_tag_p(dep
);
1738 *tagp
= cpu_to_be16((char *)dep
- (char *)hdr
);
1739 xfs_dir2_data_log_entry(tp
, dbp
, dep
);
1741 * Rescan the block for bestfree if needed.
1744 xfs_dir2_data_freescan(mp
, hdr
, &needlog
);
1746 * Log the data block header if needed.
1749 xfs_dir2_data_log_header(tp
, dbp
);
1751 * If the freespace entry is now wrong, update it.
1753 if (be16_to_cpu(free
->bests
[findex
]) != be16_to_cpu(hdr
->bestfree
[0].length
)) {
1754 free
->bests
[findex
] = hdr
->bestfree
[0].length
;
1758 * Log the freespace entry if needed.
1761 xfs_dir2_free_log_bests(tp
, fbp
, findex
, findex
);
1763 * Return the data block and offset in args, then drop the data block.
1765 args
->blkno
= (xfs_dablk_t
)dbno
;
1766 args
->index
= be16_to_cpu(*tagp
);
1771 * Lookup an entry in a node-format directory.
1772 * All the real work happens in xfs_da_node_lookup_int.
1773 * The only real output is the inode number of the entry.
1776 xfs_dir2_node_lookup(
1777 xfs_da_args_t
*args
) /* operation arguments */
1779 int error
; /* error return value */
1780 int i
; /* btree level */
1781 int rval
; /* operation return value */
1782 xfs_da_state_t
*state
; /* btree cursor */
1784 trace_xfs_dir2_node_lookup(args
);
1787 * Allocate and initialize the btree cursor.
1789 state
= xfs_da_state_alloc();
1791 state
->mp
= args
->dp
->i_mount
;
1792 state
->blocksize
= state
->mp
->m_dirblksize
;
1793 state
->node_ents
= state
->mp
->m_dir_node_ents
;
1795 * Fill in the path to the entry in the cursor.
1797 error
= xfs_da_node_lookup_int(state
, &rval
);
1800 else if (rval
== ENOENT
&& args
->cmpresult
== XFS_CMP_CASE
) {
1801 /* If a CI match, dup the actual name and return EEXIST */
1802 xfs_dir2_data_entry_t
*dep
;
1804 dep
= (xfs_dir2_data_entry_t
*)
1805 ((char *)state
->extrablk
.bp
->b_addr
+
1806 state
->extrablk
.index
);
1807 rval
= xfs_dir_cilookup_result(args
, dep
->name
, dep
->namelen
);
1810 * Release the btree blocks and leaf block.
1812 for (i
= 0; i
< state
->path
.active
; i
++) {
1813 xfs_trans_brelse(args
->trans
, state
->path
.blk
[i
].bp
);
1814 state
->path
.blk
[i
].bp
= NULL
;
1817 * Release the data block if we have it.
1819 if (state
->extravalid
&& state
->extrablk
.bp
) {
1820 xfs_trans_brelse(args
->trans
, state
->extrablk
.bp
);
1821 state
->extrablk
.bp
= NULL
;
1823 xfs_da_state_free(state
);
1828 * Remove an entry from a node-format directory.
1831 xfs_dir2_node_removename(
1832 xfs_da_args_t
*args
) /* operation arguments */
1834 xfs_da_state_blk_t
*blk
; /* leaf block */
1835 int error
; /* error return value */
1836 int rval
; /* operation return value */
1837 xfs_da_state_t
*state
; /* btree cursor */
1839 trace_xfs_dir2_node_removename(args
);
1842 * Allocate and initialize the btree cursor.
1844 state
= xfs_da_state_alloc();
1846 state
->mp
= args
->dp
->i_mount
;
1847 state
->blocksize
= state
->mp
->m_dirblksize
;
1848 state
->node_ents
= state
->mp
->m_dir_node_ents
;
1850 * Look up the entry we're deleting, set up the cursor.
1852 error
= xfs_da_node_lookup_int(state
, &rval
);
1856 * Didn't find it, upper layer screwed up.
1858 if (rval
!= EEXIST
) {
1859 xfs_da_state_free(state
);
1862 blk
= &state
->path
.blk
[state
->path
.active
- 1];
1863 ASSERT(blk
->magic
== XFS_DIR2_LEAFN_MAGIC
);
1864 ASSERT(state
->extravalid
);
1866 * Remove the leaf and data entries.
1867 * Extrablk refers to the data block.
1869 error
= xfs_dir2_leafn_remove(args
, blk
->bp
, blk
->index
,
1870 &state
->extrablk
, &rval
);
1874 * Fix the hash values up the btree.
1876 xfs_da_fixhashpath(state
, &state
->path
);
1878 * If we need to join leaf blocks, do it.
1880 if (rval
&& state
->path
.active
> 1)
1881 error
= xfs_da_join(state
);
1883 * If no errors so far, try conversion to leaf format.
1886 error
= xfs_dir2_node_to_leaf(state
);
1887 xfs_da_state_free(state
);
1892 * Replace an entry's inode number in a node-format directory.
1895 xfs_dir2_node_replace(
1896 xfs_da_args_t
*args
) /* operation arguments */
1898 xfs_da_state_blk_t
*blk
; /* leaf block */
1899 xfs_dir2_data_hdr_t
*hdr
; /* data block header */
1900 xfs_dir2_data_entry_t
*dep
; /* data entry changed */
1901 int error
; /* error return value */
1902 int i
; /* btree level */
1903 xfs_ino_t inum
; /* new inode number */
1904 xfs_dir2_leaf_t
*leaf
; /* leaf structure */
1905 xfs_dir2_leaf_entry_t
*lep
; /* leaf entry being changed */
1906 int rval
; /* internal return value */
1907 xfs_da_state_t
*state
; /* btree cursor */
1909 trace_xfs_dir2_node_replace(args
);
1912 * Allocate and initialize the btree cursor.
1914 state
= xfs_da_state_alloc();
1916 state
->mp
= args
->dp
->i_mount
;
1917 state
->blocksize
= state
->mp
->m_dirblksize
;
1918 state
->node_ents
= state
->mp
->m_dir_node_ents
;
1919 inum
= args
->inumber
;
1921 * Lookup the entry to change in the btree.
1923 error
= xfs_da_node_lookup_int(state
, &rval
);
1928 * It should be found, since the vnodeops layer has looked it up
1929 * and locked it. But paranoia is good.
1931 if (rval
== EEXIST
) {
1933 * Find the leaf entry.
1935 blk
= &state
->path
.blk
[state
->path
.active
- 1];
1936 ASSERT(blk
->magic
== XFS_DIR2_LEAFN_MAGIC
);
1937 leaf
= blk
->bp
->b_addr
;
1938 lep
= &leaf
->ents
[blk
->index
];
1939 ASSERT(state
->extravalid
);
1941 * Point to the data entry.
1943 hdr
= state
->extrablk
.bp
->b_addr
;
1944 ASSERT(hdr
->magic
== cpu_to_be32(XFS_DIR2_DATA_MAGIC
));
1945 dep
= (xfs_dir2_data_entry_t
*)
1947 xfs_dir2_dataptr_to_off(state
->mp
, be32_to_cpu(lep
->address
)));
1948 ASSERT(inum
!= be64_to_cpu(dep
->inumber
));
1950 * Fill in the new inode number and log the entry.
1952 dep
->inumber
= cpu_to_be64(inum
);
1953 xfs_dir2_data_log_entry(args
->trans
, state
->extrablk
.bp
, dep
);
1957 * Didn't find it, and we're holding a data block. Drop it.
1959 else if (state
->extravalid
) {
1960 xfs_trans_brelse(args
->trans
, state
->extrablk
.bp
);
1961 state
->extrablk
.bp
= NULL
;
1964 * Release all the buffers in the cursor.
1966 for (i
= 0; i
< state
->path
.active
; i
++) {
1967 xfs_trans_brelse(args
->trans
, state
->path
.blk
[i
].bp
);
1968 state
->path
.blk
[i
].bp
= NULL
;
1970 xfs_da_state_free(state
);
1975 * Trim off a trailing empty freespace block.
1976 * Return (in rvalp) 1 if we did it, 0 if not.
1979 xfs_dir2_node_trim_free(
1980 xfs_da_args_t
*args
, /* operation arguments */
1981 xfs_fileoff_t fo
, /* free block number */
1982 int *rvalp
) /* out: did something */
1984 struct xfs_buf
*bp
; /* freespace buffer */
1985 xfs_inode_t
*dp
; /* incore directory inode */
1986 int error
; /* error return code */
1987 xfs_dir2_free_t
*free
; /* freespace structure */
1988 xfs_mount_t
*mp
; /* filesystem mount point */
1989 xfs_trans_t
*tp
; /* transaction pointer */
1995 * Read the freespace block.
1997 error
= xfs_dir2_free_try_read(tp
, dp
, fo
, &bp
);
2001 * There can be holes in freespace. If fo is a hole, there's
2007 ASSERT(free
->hdr
.magic
== cpu_to_be32(XFS_DIR2_FREE_MAGIC
));
2009 * If there are used entries, there's nothing to do.
2011 if (be32_to_cpu(free
->hdr
.nused
) > 0) {
2012 xfs_trans_brelse(tp
, bp
);
2017 * Blow the block away.
2020 xfs_dir2_shrink_inode(args
, xfs_dir2_da_to_db(mp
, (xfs_dablk_t
)fo
),
2023 * Can't fail with ENOSPC since that only happens with no
2024 * space reservation, when breaking up an extent into two
2025 * pieces. This is the last block of an extent.
2027 ASSERT(error
!= ENOSPC
);
2028 xfs_trans_brelse(tp
, bp
);
2032 * Return that we succeeded.