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
);
59 * Log entries from a freespace block.
62 xfs_dir2_free_log_bests(
65 int first
, /* first entry to log */
66 int last
) /* last entry to log */
68 xfs_dir2_free_t
*free
; /* freespace structure */
71 ASSERT(free
->hdr
.magic
== cpu_to_be32(XFS_DIR2_FREE_MAGIC
));
72 xfs_trans_log_buf(tp
, bp
,
73 (uint
)((char *)&free
->bests
[first
] - (char *)free
),
74 (uint
)((char *)&free
->bests
[last
] - (char *)free
+
75 sizeof(free
->bests
[0]) - 1));
79 * Log header from a freespace block.
82 xfs_dir2_free_log_header(
86 xfs_dir2_free_t
*free
; /* freespace structure */
89 ASSERT(free
->hdr
.magic
== cpu_to_be32(XFS_DIR2_FREE_MAGIC
));
90 xfs_trans_log_buf(tp
, bp
, (uint
)((char *)&free
->hdr
- (char *)free
),
91 (uint
)(sizeof(xfs_dir2_free_hdr_t
) - 1));
95 * Convert a leaf-format directory to a node-format directory.
96 * We need to change the magic number of the leaf block, and copy
97 * the freespace table out of the leaf block into its own block.
100 xfs_dir2_leaf_to_node(
101 xfs_da_args_t
*args
, /* operation arguments */
102 struct xfs_buf
*lbp
) /* leaf buffer */
104 xfs_inode_t
*dp
; /* incore directory inode */
105 int error
; /* error return value */
106 struct xfs_buf
*fbp
; /* freespace buffer */
107 xfs_dir2_db_t fdb
; /* freespace block number */
108 xfs_dir2_free_t
*free
; /* freespace structure */
109 __be16
*from
; /* pointer to freespace entry */
110 int i
; /* leaf freespace index */
111 xfs_dir2_leaf_t
*leaf
; /* leaf structure */
112 xfs_dir2_leaf_tail_t
*ltp
; /* leaf tail structure */
113 xfs_mount_t
*mp
; /* filesystem mount point */
114 int n
; /* count of live freespc ents */
115 xfs_dir2_data_off_t off
; /* freespace entry value */
116 __be16
*to
; /* pointer to freespace entry */
117 xfs_trans_t
*tp
; /* transaction pointer */
119 trace_xfs_dir2_leaf_to_node(args
);
125 * Add a freespace block to the directory.
127 if ((error
= xfs_dir2_grow_inode(args
, XFS_DIR2_FREE_SPACE
, &fdb
))) {
130 ASSERT(fdb
== XFS_DIR2_FREE_FIRSTDB(mp
));
132 * Get the buffer for the new freespace block.
134 if ((error
= xfs_da_get_buf(tp
, dp
, xfs_dir2_db_to_da(mp
, fdb
), -1, &fbp
,
141 ltp
= xfs_dir2_leaf_tail_p(mp
, leaf
);
143 * Initialize the freespace block header.
145 free
->hdr
.magic
= cpu_to_be32(XFS_DIR2_FREE_MAGIC
);
146 free
->hdr
.firstdb
= 0;
147 ASSERT(be32_to_cpu(ltp
->bestcount
) <= (uint
)dp
->i_d
.di_size
/ mp
->m_dirblksize
);
148 free
->hdr
.nvalid
= ltp
->bestcount
;
150 * Copy freespace entries from the leaf block to the new block.
151 * Count active entries.
153 for (i
= n
= 0, from
= xfs_dir2_leaf_bests_p(ltp
), to
= free
->bests
;
154 i
< be32_to_cpu(ltp
->bestcount
); i
++, from
++, to
++) {
155 if ((off
= be16_to_cpu(*from
)) != NULLDATAOFF
)
157 *to
= cpu_to_be16(off
);
159 free
->hdr
.nused
= cpu_to_be32(n
);
160 leaf
->hdr
.info
.magic
= cpu_to_be16(XFS_DIR2_LEAFN_MAGIC
);
164 xfs_dir2_leaf_log_header(tp
, lbp
);
165 xfs_dir2_free_log_header(tp
, fbp
);
166 xfs_dir2_free_log_bests(tp
, fbp
, 0, be32_to_cpu(free
->hdr
.nvalid
) - 1);
167 xfs_dir2_leafn_check(dp
, lbp
);
172 * Add a leaf entry to a leaf block in a node-form directory.
173 * The other work necessary is done from the caller.
175 static int /* error */
177 struct xfs_buf
*bp
, /* leaf buffer */
178 xfs_da_args_t
*args
, /* operation arguments */
179 int index
) /* insertion pt for new entry */
181 int compact
; /* compacting stale leaves */
182 xfs_inode_t
*dp
; /* incore directory inode */
183 int highstale
; /* next stale entry */
184 xfs_dir2_leaf_t
*leaf
; /* leaf structure */
185 xfs_dir2_leaf_entry_t
*lep
; /* leaf entry */
186 int lfloghigh
; /* high leaf entry logging */
187 int lfloglow
; /* low leaf entry logging */
188 int lowstale
; /* previous stale entry */
189 xfs_mount_t
*mp
; /* filesystem mount point */
190 xfs_trans_t
*tp
; /* transaction pointer */
192 trace_xfs_dir2_leafn_add(args
, index
);
200 * Quick check just to make sure we are not going to index
201 * into other peoples memory
204 return XFS_ERROR(EFSCORRUPTED
);
207 * If there are already the maximum number of leaf entries in
208 * the block, if there are no stale entries it won't fit.
209 * Caller will do a split. If there are stale entries we'll do
213 if (be16_to_cpu(leaf
->hdr
.count
) == xfs_dir2_max_leaf_ents(mp
)) {
214 if (!leaf
->hdr
.stale
)
215 return XFS_ERROR(ENOSPC
);
216 compact
= be16_to_cpu(leaf
->hdr
.stale
) > 1;
219 ASSERT(index
== 0 || be32_to_cpu(leaf
->ents
[index
- 1].hashval
) <= args
->hashval
);
220 ASSERT(index
== be16_to_cpu(leaf
->hdr
.count
) ||
221 be32_to_cpu(leaf
->ents
[index
].hashval
) >= args
->hashval
);
223 if (args
->op_flags
& XFS_DA_OP_JUSTCHECK
)
227 * Compact out all but one stale leaf entry. Leaves behind
228 * the entry closest to index.
231 xfs_dir2_leaf_compact_x1(bp
, &index
, &lowstale
, &highstale
,
232 &lfloglow
, &lfloghigh
);
235 * Set impossible logging indices for this case.
237 else if (leaf
->hdr
.stale
) {
238 lfloglow
= be16_to_cpu(leaf
->hdr
.count
);
243 * Insert the new entry, log everything.
245 lep
= xfs_dir2_leaf_find_entry(leaf
, index
, compact
, lowstale
,
246 highstale
, &lfloglow
, &lfloghigh
);
248 lep
->hashval
= cpu_to_be32(args
->hashval
);
249 lep
->address
= cpu_to_be32(xfs_dir2_db_off_to_dataptr(mp
,
250 args
->blkno
, args
->index
));
251 xfs_dir2_leaf_log_header(tp
, bp
);
252 xfs_dir2_leaf_log_ents(tp
, bp
, lfloglow
, lfloghigh
);
253 xfs_dir2_leafn_check(dp
, bp
);
259 * Check internal consistency of a leafn block.
262 xfs_dir2_leafn_check(
263 struct xfs_inode
*dp
,
266 int i
; /* leaf index */
267 xfs_dir2_leaf_t
*leaf
; /* leaf structure */
268 xfs_mount_t
*mp
; /* filesystem mount point */
269 int stale
; /* count of stale leaves */
273 ASSERT(leaf
->hdr
.info
.magic
== cpu_to_be16(XFS_DIR2_LEAFN_MAGIC
));
274 ASSERT(be16_to_cpu(leaf
->hdr
.count
) <= xfs_dir2_max_leaf_ents(mp
));
275 for (i
= stale
= 0; i
< be16_to_cpu(leaf
->hdr
.count
); i
++) {
276 if (i
+ 1 < be16_to_cpu(leaf
->hdr
.count
)) {
277 ASSERT(be32_to_cpu(leaf
->ents
[i
].hashval
) <=
278 be32_to_cpu(leaf
->ents
[i
+ 1].hashval
));
280 if (leaf
->ents
[i
].address
== cpu_to_be32(XFS_DIR2_NULL_DATAPTR
))
283 ASSERT(be16_to_cpu(leaf
->hdr
.stale
) == stale
);
288 * Return the last hash value in the leaf.
289 * Stale entries are ok.
291 xfs_dahash_t
/* hash value */
292 xfs_dir2_leafn_lasthash(
293 struct xfs_buf
*bp
, /* leaf buffer */
294 int *count
) /* count of entries in leaf */
296 xfs_dir2_leaf_t
*leaf
; /* leaf structure */
299 ASSERT(leaf
->hdr
.info
.magic
== cpu_to_be16(XFS_DIR2_LEAFN_MAGIC
));
301 *count
= be16_to_cpu(leaf
->hdr
.count
);
302 if (!leaf
->hdr
.count
)
304 return be32_to_cpu(leaf
->ents
[be16_to_cpu(leaf
->hdr
.count
) - 1].hashval
);
308 * Look up a leaf entry for space to add a name in a node-format leaf block.
309 * The extrablk in state is a freespace block.
312 xfs_dir2_leafn_lookup_for_addname(
313 struct xfs_buf
*bp
, /* leaf buffer */
314 xfs_da_args_t
*args
, /* operation arguments */
315 int *indexp
, /* out: leaf entry index */
316 xfs_da_state_t
*state
) /* state to fill in */
318 struct xfs_buf
*curbp
= NULL
; /* current data/free buffer */
319 xfs_dir2_db_t curdb
= -1; /* current data block number */
320 xfs_dir2_db_t curfdb
= -1; /* current free block number */
321 xfs_inode_t
*dp
; /* incore directory inode */
322 int error
; /* error return value */
323 int fi
; /* free entry index */
324 xfs_dir2_free_t
*free
= NULL
; /* free block structure */
325 int index
; /* leaf entry index */
326 xfs_dir2_leaf_t
*leaf
; /* leaf structure */
327 int length
; /* length of new data entry */
328 xfs_dir2_leaf_entry_t
*lep
; /* leaf entry */
329 xfs_mount_t
*mp
; /* filesystem mount point */
330 xfs_dir2_db_t newdb
; /* new data block number */
331 xfs_dir2_db_t newfdb
; /* new free block number */
332 xfs_trans_t
*tp
; /* transaction pointer */
338 ASSERT(leaf
->hdr
.info
.magic
== cpu_to_be16(XFS_DIR2_LEAFN_MAGIC
));
340 ASSERT(be16_to_cpu(leaf
->hdr
.count
) > 0);
342 xfs_dir2_leafn_check(dp
, bp
);
344 * Look up the hash value in the leaf entries.
346 index
= xfs_dir2_leaf_search_hash(args
, bp
);
348 * Do we have a buffer coming in?
350 if (state
->extravalid
) {
351 /* If so, it's a free block buffer, get the block number. */
352 curbp
= state
->extrablk
.bp
;
353 curfdb
= state
->extrablk
.blkno
;
354 free
= curbp
->b_addr
;
355 ASSERT(free
->hdr
.magic
== cpu_to_be32(XFS_DIR2_FREE_MAGIC
));
357 length
= xfs_dir2_data_entsize(args
->namelen
);
359 * Loop over leaf entries with the right hash value.
361 for (lep
= &leaf
->ents
[index
]; index
< be16_to_cpu(leaf
->hdr
.count
) &&
362 be32_to_cpu(lep
->hashval
) == args
->hashval
;
365 * Skip stale leaf entries.
367 if (be32_to_cpu(lep
->address
) == XFS_DIR2_NULL_DATAPTR
)
370 * Pull the data block number from the entry.
372 newdb
= xfs_dir2_dataptr_to_db(mp
, be32_to_cpu(lep
->address
));
374 * For addname, we're looking for a place to put the new entry.
375 * We want to use a data block with an entry of equal
376 * hash value to ours if there is one with room.
378 * If this block isn't the data block we already have
379 * in hand, take a look at it.
381 if (newdb
!= curdb
) {
384 * Convert the data block to the free block
385 * holding its freespace information.
387 newfdb
= xfs_dir2_db_to_fdb(mp
, newdb
);
389 * If it's not the one we have in hand, read it in.
391 if (newfdb
!= curfdb
) {
393 * If we had one before, drop it.
396 xfs_trans_brelse(tp
, curbp
);
398 * Read the free block.
400 error
= xfs_da_read_buf(tp
, dp
,
401 xfs_dir2_db_to_da(mp
, newfdb
),
402 -1, &curbp
, XFS_DATA_FORK
);
405 free
= curbp
->b_addr
;
406 ASSERT(be32_to_cpu(free
->hdr
.magic
) ==
407 XFS_DIR2_FREE_MAGIC
);
408 ASSERT((be32_to_cpu(free
->hdr
.firstdb
) %
409 xfs_dir2_free_max_bests(mp
)) == 0);
410 ASSERT(be32_to_cpu(free
->hdr
.firstdb
) <= curdb
);
411 ASSERT(curdb
< be32_to_cpu(free
->hdr
.firstdb
) +
412 be32_to_cpu(free
->hdr
.nvalid
));
415 * Get the index for our entry.
417 fi
= xfs_dir2_db_to_fdindex(mp
, curdb
);
419 * If it has room, return it.
421 if (unlikely(free
->bests
[fi
] ==
422 cpu_to_be16(NULLDATAOFF
))) {
423 XFS_ERROR_REPORT("xfs_dir2_leafn_lookup_int",
424 XFS_ERRLEVEL_LOW
, mp
);
425 if (curfdb
!= newfdb
)
426 xfs_trans_brelse(tp
, curbp
);
427 return XFS_ERROR(EFSCORRUPTED
);
430 if (be16_to_cpu(free
->bests
[fi
]) >= length
)
434 /* Didn't find any space */
437 ASSERT(args
->op_flags
& XFS_DA_OP_OKNOENT
);
439 /* Giving back a free block. */
440 state
->extravalid
= 1;
441 state
->extrablk
.bp
= curbp
;
442 state
->extrablk
.index
= fi
;
443 state
->extrablk
.blkno
= curfdb
;
444 state
->extrablk
.magic
= XFS_DIR2_FREE_MAGIC
;
446 state
->extravalid
= 0;
449 * Return the index, that will be the insertion point.
452 return XFS_ERROR(ENOENT
);
456 * Look up a leaf entry in a node-format leaf block.
457 * The extrablk in state a data block.
460 xfs_dir2_leafn_lookup_for_entry(
461 struct xfs_buf
*bp
, /* leaf buffer */
462 xfs_da_args_t
*args
, /* operation arguments */
463 int *indexp
, /* out: leaf entry index */
464 xfs_da_state_t
*state
) /* state to fill in */
466 struct xfs_buf
*curbp
= NULL
; /* current data/free buffer */
467 xfs_dir2_db_t curdb
= -1; /* current data block number */
468 xfs_dir2_data_entry_t
*dep
; /* data block entry */
469 xfs_inode_t
*dp
; /* incore directory inode */
470 int error
; /* error return value */
471 int index
; /* leaf entry index */
472 xfs_dir2_leaf_t
*leaf
; /* leaf structure */
473 xfs_dir2_leaf_entry_t
*lep
; /* leaf entry */
474 xfs_mount_t
*mp
; /* filesystem mount point */
475 xfs_dir2_db_t newdb
; /* new data block number */
476 xfs_trans_t
*tp
; /* transaction pointer */
477 enum xfs_dacmp cmp
; /* comparison result */
483 ASSERT(leaf
->hdr
.info
.magic
== cpu_to_be16(XFS_DIR2_LEAFN_MAGIC
));
485 ASSERT(be16_to_cpu(leaf
->hdr
.count
) > 0);
487 xfs_dir2_leafn_check(dp
, bp
);
489 * Look up the hash value in the leaf entries.
491 index
= xfs_dir2_leaf_search_hash(args
, bp
);
493 * Do we have a buffer coming in?
495 if (state
->extravalid
) {
496 curbp
= state
->extrablk
.bp
;
497 curdb
= state
->extrablk
.blkno
;
500 * Loop over leaf entries with the right hash value.
502 for (lep
= &leaf
->ents
[index
]; index
< be16_to_cpu(leaf
->hdr
.count
) &&
503 be32_to_cpu(lep
->hashval
) == args
->hashval
;
506 * Skip stale leaf entries.
508 if (be32_to_cpu(lep
->address
) == XFS_DIR2_NULL_DATAPTR
)
511 * Pull the data block number from the entry.
513 newdb
= xfs_dir2_dataptr_to_db(mp
, be32_to_cpu(lep
->address
));
515 * Not adding a new entry, so we really want to find
516 * the name given to us.
518 * If it's a different data block, go get it.
520 if (newdb
!= curdb
) {
522 * If we had a block before that we aren't saving
523 * for a CI name, drop it
525 if (curbp
&& (args
->cmpresult
== XFS_CMP_DIFFERENT
||
526 curdb
!= state
->extrablk
.blkno
))
527 xfs_trans_brelse(tp
, curbp
);
529 * If needing the block that is saved with a CI match,
530 * use it otherwise read in the new data block.
532 if (args
->cmpresult
!= XFS_CMP_DIFFERENT
&&
533 newdb
== state
->extrablk
.blkno
) {
534 ASSERT(state
->extravalid
);
535 curbp
= state
->extrablk
.bp
;
537 error
= xfs_da_read_buf(tp
, dp
,
538 xfs_dir2_db_to_da(mp
, newdb
),
539 -1, &curbp
, XFS_DATA_FORK
);
543 xfs_dir2_data_check(dp
, curbp
);
547 * Point to the data entry.
549 dep
= (xfs_dir2_data_entry_t
*)((char *)curbp
->b_addr
+
550 xfs_dir2_dataptr_to_off(mp
, be32_to_cpu(lep
->address
)));
552 * Compare the entry and if it's an exact match, return
553 * EEXIST immediately. If it's the first case-insensitive
554 * match, store the block & inode number and continue looking.
556 cmp
= mp
->m_dirnameops
->compname(args
, dep
->name
, dep
->namelen
);
557 if (cmp
!= XFS_CMP_DIFFERENT
&& cmp
!= args
->cmpresult
) {
558 /* If there is a CI match block, drop it */
559 if (args
->cmpresult
!= XFS_CMP_DIFFERENT
&&
560 curdb
!= state
->extrablk
.blkno
)
561 xfs_trans_brelse(tp
, state
->extrablk
.bp
);
562 args
->cmpresult
= cmp
;
563 args
->inumber
= be64_to_cpu(dep
->inumber
);
565 state
->extravalid
= 1;
566 state
->extrablk
.bp
= curbp
;
567 state
->extrablk
.blkno
= curdb
;
568 state
->extrablk
.index
= (int)((char *)dep
-
569 (char *)curbp
->b_addr
);
570 state
->extrablk
.magic
= XFS_DIR2_DATA_MAGIC
;
571 if (cmp
== XFS_CMP_EXACT
)
572 return XFS_ERROR(EEXIST
);
575 ASSERT(index
== be16_to_cpu(leaf
->hdr
.count
) ||
576 (args
->op_flags
& XFS_DA_OP_OKNOENT
));
578 if (args
->cmpresult
== XFS_CMP_DIFFERENT
) {
579 /* Giving back last used data block. */
580 state
->extravalid
= 1;
581 state
->extrablk
.bp
= curbp
;
582 state
->extrablk
.index
= -1;
583 state
->extrablk
.blkno
= curdb
;
584 state
->extrablk
.magic
= XFS_DIR2_DATA_MAGIC
;
586 /* If the curbp is not the CI match block, drop it */
587 if (state
->extrablk
.bp
!= curbp
)
588 xfs_trans_brelse(tp
, curbp
);
591 state
->extravalid
= 0;
594 return XFS_ERROR(ENOENT
);
598 * Look up a leaf entry in a node-format leaf block.
599 * If this is an addname then the extrablk in state is a freespace block,
600 * otherwise it's a data block.
603 xfs_dir2_leafn_lookup_int(
604 struct xfs_buf
*bp
, /* leaf buffer */
605 xfs_da_args_t
*args
, /* operation arguments */
606 int *indexp
, /* out: leaf entry index */
607 xfs_da_state_t
*state
) /* state to fill in */
609 if (args
->op_flags
& XFS_DA_OP_ADDNAME
)
610 return xfs_dir2_leafn_lookup_for_addname(bp
, args
, indexp
,
612 return xfs_dir2_leafn_lookup_for_entry(bp
, args
, indexp
, state
);
616 * Move count leaf entries from source to destination leaf.
617 * Log entries and headers. Stale entries are preserved.
620 xfs_dir2_leafn_moveents(
621 xfs_da_args_t
*args
, /* operation arguments */
622 struct xfs_buf
*bp_s
, /* source leaf buffer */
623 int start_s
, /* source leaf index */
624 struct xfs_buf
*bp_d
, /* destination leaf buffer */
625 int start_d
, /* destination leaf index */
626 int count
) /* count of leaves to copy */
628 xfs_dir2_leaf_t
*leaf_d
; /* destination leaf structure */
629 xfs_dir2_leaf_t
*leaf_s
; /* source leaf structure */
630 int stale
; /* count stale leaves copied */
631 xfs_trans_t
*tp
; /* transaction pointer */
633 trace_xfs_dir2_leafn_moveents(args
, start_s
, start_d
, count
);
636 * Silently return if nothing to do.
642 leaf_s
= bp_s
->b_addr
;
643 leaf_d
= bp_d
->b_addr
;
645 * If the destination index is not the end of the current
646 * destination leaf entries, open up a hole in the destination
647 * to hold the new entries.
649 if (start_d
< be16_to_cpu(leaf_d
->hdr
.count
)) {
650 memmove(&leaf_d
->ents
[start_d
+ count
], &leaf_d
->ents
[start_d
],
651 (be16_to_cpu(leaf_d
->hdr
.count
) - start_d
) *
652 sizeof(xfs_dir2_leaf_entry_t
));
653 xfs_dir2_leaf_log_ents(tp
, bp_d
, start_d
+ count
,
654 count
+ be16_to_cpu(leaf_d
->hdr
.count
) - 1);
657 * If the source has stale leaves, count the ones in the copy range
658 * so we can update the header correctly.
660 if (leaf_s
->hdr
.stale
) {
661 int i
; /* temp leaf index */
663 for (i
= start_s
, stale
= 0; i
< start_s
+ count
; i
++) {
664 if (leaf_s
->ents
[i
].address
==
665 cpu_to_be32(XFS_DIR2_NULL_DATAPTR
))
671 * Copy the leaf entries from source to destination.
673 memcpy(&leaf_d
->ents
[start_d
], &leaf_s
->ents
[start_s
],
674 count
* sizeof(xfs_dir2_leaf_entry_t
));
675 xfs_dir2_leaf_log_ents(tp
, bp_d
, start_d
, start_d
+ count
- 1);
677 * If there are source entries after the ones we copied,
678 * delete the ones we copied by sliding the next ones down.
680 if (start_s
+ count
< be16_to_cpu(leaf_s
->hdr
.count
)) {
681 memmove(&leaf_s
->ents
[start_s
], &leaf_s
->ents
[start_s
+ count
],
682 count
* sizeof(xfs_dir2_leaf_entry_t
));
683 xfs_dir2_leaf_log_ents(tp
, bp_s
, start_s
, start_s
+ count
- 1);
686 * Update the headers and log them.
688 be16_add_cpu(&leaf_s
->hdr
.count
, -(count
));
689 be16_add_cpu(&leaf_s
->hdr
.stale
, -(stale
));
690 be16_add_cpu(&leaf_d
->hdr
.count
, count
);
691 be16_add_cpu(&leaf_d
->hdr
.stale
, stale
);
692 xfs_dir2_leaf_log_header(tp
, bp_s
);
693 xfs_dir2_leaf_log_header(tp
, bp_d
);
694 xfs_dir2_leafn_check(args
->dp
, bp_s
);
695 xfs_dir2_leafn_check(args
->dp
, bp_d
);
699 * Determine the sort order of two leaf blocks.
700 * Returns 1 if both are valid and leaf2 should be before leaf1, else 0.
703 xfs_dir2_leafn_order(
704 struct xfs_buf
*leaf1_bp
, /* leaf1 buffer */
705 struct xfs_buf
*leaf2_bp
) /* leaf2 buffer */
707 xfs_dir2_leaf_t
*leaf1
; /* leaf1 structure */
708 xfs_dir2_leaf_t
*leaf2
; /* leaf2 structure */
710 leaf1
= leaf1_bp
->b_addr
;
711 leaf2
= leaf2_bp
->b_addr
;
712 ASSERT(leaf1
->hdr
.info
.magic
== cpu_to_be16(XFS_DIR2_LEAFN_MAGIC
));
713 ASSERT(leaf2
->hdr
.info
.magic
== cpu_to_be16(XFS_DIR2_LEAFN_MAGIC
));
714 if (be16_to_cpu(leaf1
->hdr
.count
) > 0 &&
715 be16_to_cpu(leaf2
->hdr
.count
) > 0 &&
716 (be32_to_cpu(leaf2
->ents
[0].hashval
) < be32_to_cpu(leaf1
->ents
[0].hashval
) ||
717 be32_to_cpu(leaf2
->ents
[be16_to_cpu(leaf2
->hdr
.count
) - 1].hashval
) <
718 be32_to_cpu(leaf1
->ents
[be16_to_cpu(leaf1
->hdr
.count
) - 1].hashval
)))
724 * Rebalance leaf entries between two leaf blocks.
725 * This is actually only called when the second block is new,
726 * though the code deals with the general case.
727 * A new entry will be inserted in one of the blocks, and that
728 * entry is taken into account when balancing.
731 xfs_dir2_leafn_rebalance(
732 xfs_da_state_t
*state
, /* btree cursor */
733 xfs_da_state_blk_t
*blk1
, /* first btree block */
734 xfs_da_state_blk_t
*blk2
) /* second btree block */
736 xfs_da_args_t
*args
; /* operation arguments */
737 int count
; /* count (& direction) leaves */
738 int isleft
; /* new goes in left leaf */
739 xfs_dir2_leaf_t
*leaf1
; /* first leaf structure */
740 xfs_dir2_leaf_t
*leaf2
; /* second leaf structure */
741 int mid
; /* midpoint leaf index */
743 int oldstale
; /* old count of stale leaves */
745 int oldsum
; /* old total leaf count */
746 int swap
; /* swapped leaf blocks */
750 * If the block order is wrong, swap the arguments.
752 if ((swap
= xfs_dir2_leafn_order(blk1
->bp
, blk2
->bp
))) {
753 xfs_da_state_blk_t
*tmp
; /* temp for block swap */
759 leaf1
= blk1
->bp
->b_addr
;
760 leaf2
= blk2
->bp
->b_addr
;
761 oldsum
= be16_to_cpu(leaf1
->hdr
.count
) + be16_to_cpu(leaf2
->hdr
.count
);
763 oldstale
= be16_to_cpu(leaf1
->hdr
.stale
) + be16_to_cpu(leaf2
->hdr
.stale
);
767 * If the old leaf count was odd then the new one will be even,
768 * so we need to divide the new count evenly.
771 xfs_dahash_t midhash
; /* middle entry hash value */
773 if (mid
>= be16_to_cpu(leaf1
->hdr
.count
))
774 midhash
= be32_to_cpu(leaf2
->ents
[mid
- be16_to_cpu(leaf1
->hdr
.count
)].hashval
);
776 midhash
= be32_to_cpu(leaf1
->ents
[mid
].hashval
);
777 isleft
= args
->hashval
<= midhash
;
780 * If the old count is even then the new count is odd, so there's
781 * no preferred side for the new entry.
787 * Calculate moved entry count. Positive means left-to-right,
788 * negative means right-to-left. Then move the entries.
790 count
= be16_to_cpu(leaf1
->hdr
.count
) - mid
+ (isleft
== 0);
792 xfs_dir2_leafn_moveents(args
, blk1
->bp
,
793 be16_to_cpu(leaf1
->hdr
.count
) - count
, blk2
->bp
, 0, count
);
795 xfs_dir2_leafn_moveents(args
, blk2
->bp
, 0, blk1
->bp
,
796 be16_to_cpu(leaf1
->hdr
.count
), count
);
797 ASSERT(be16_to_cpu(leaf1
->hdr
.count
) + be16_to_cpu(leaf2
->hdr
.count
) == oldsum
);
798 ASSERT(be16_to_cpu(leaf1
->hdr
.stale
) + be16_to_cpu(leaf2
->hdr
.stale
) == oldstale
);
800 * Mark whether we're inserting into the old or new leaf.
802 if (be16_to_cpu(leaf1
->hdr
.count
) < be16_to_cpu(leaf2
->hdr
.count
))
803 state
->inleaf
= swap
;
804 else if (be16_to_cpu(leaf1
->hdr
.count
) > be16_to_cpu(leaf2
->hdr
.count
))
805 state
->inleaf
= !swap
;
808 swap
^ (blk1
->index
<= be16_to_cpu(leaf1
->hdr
.count
));
810 * Adjust the expected index for insertion.
813 blk2
->index
= blk1
->index
- be16_to_cpu(leaf1
->hdr
.count
);
816 * Finally sanity check just to make sure we are not returning a
819 if(blk2
->index
< 0) {
822 xfs_alert(args
->dp
->i_mount
,
823 "%s: picked the wrong leaf? reverting original leaf: blk1->index %d\n",
824 __func__
, blk1
->index
);
829 * Remove an entry from a node directory.
830 * This removes the leaf entry and the data entry,
831 * and updates the free block if necessary.
833 static int /* error */
834 xfs_dir2_leafn_remove(
835 xfs_da_args_t
*args
, /* operation arguments */
836 struct xfs_buf
*bp
, /* leaf buffer */
837 int index
, /* leaf entry index */
838 xfs_da_state_blk_t
*dblk
, /* data block */
839 int *rval
) /* resulting block needs join */
841 xfs_dir2_data_hdr_t
*hdr
; /* data block header */
842 xfs_dir2_db_t db
; /* data block number */
843 struct xfs_buf
*dbp
; /* data block buffer */
844 xfs_dir2_data_entry_t
*dep
; /* data block entry */
845 xfs_inode_t
*dp
; /* incore directory inode */
846 xfs_dir2_leaf_t
*leaf
; /* leaf structure */
847 xfs_dir2_leaf_entry_t
*lep
; /* leaf entry */
848 int longest
; /* longest data free entry */
849 int off
; /* data block entry offset */
850 xfs_mount_t
*mp
; /* filesystem mount point */
851 int needlog
; /* need to log data header */
852 int needscan
; /* need to rescan data frees */
853 xfs_trans_t
*tp
; /* transaction pointer */
855 trace_xfs_dir2_leafn_remove(args
, index
);
861 ASSERT(leaf
->hdr
.info
.magic
== cpu_to_be16(XFS_DIR2_LEAFN_MAGIC
));
863 * Point to the entry we're removing.
865 lep
= &leaf
->ents
[index
];
867 * Extract the data block and offset from the entry.
869 db
= xfs_dir2_dataptr_to_db(mp
, be32_to_cpu(lep
->address
));
870 ASSERT(dblk
->blkno
== db
);
871 off
= xfs_dir2_dataptr_to_off(mp
, be32_to_cpu(lep
->address
));
872 ASSERT(dblk
->index
== off
);
874 * Kill the leaf entry by marking it stale.
875 * Log the leaf block changes.
877 be16_add_cpu(&leaf
->hdr
.stale
, 1);
878 xfs_dir2_leaf_log_header(tp
, bp
);
879 lep
->address
= cpu_to_be32(XFS_DIR2_NULL_DATAPTR
);
880 xfs_dir2_leaf_log_ents(tp
, bp
, index
, index
);
882 * Make the data entry free. Keep track of the longest freespace
883 * in the data block in case it changes.
887 dep
= (xfs_dir2_data_entry_t
*)((char *)hdr
+ off
);
888 longest
= be16_to_cpu(hdr
->bestfree
[0].length
);
889 needlog
= needscan
= 0;
890 xfs_dir2_data_make_free(tp
, dbp
, off
,
891 xfs_dir2_data_entsize(dep
->namelen
), &needlog
, &needscan
);
893 * Rescan the data block freespaces for bestfree.
894 * Log the data block header if needed.
897 xfs_dir2_data_freescan(mp
, hdr
, &needlog
);
899 xfs_dir2_data_log_header(tp
, dbp
);
900 xfs_dir2_data_check(dp
, dbp
);
902 * If the longest data block freespace changes, need to update
903 * the corresponding freeblock entry.
905 if (longest
< be16_to_cpu(hdr
->bestfree
[0].length
)) {
906 int error
; /* error return value */
907 struct xfs_buf
*fbp
; /* freeblock buffer */
908 xfs_dir2_db_t fdb
; /* freeblock block number */
909 int findex
; /* index in freeblock entries */
910 xfs_dir2_free_t
*free
; /* freeblock structure */
911 int logfree
; /* need to log free entry */
914 * Convert the data block number to a free block,
915 * read in the free block.
917 fdb
= xfs_dir2_db_to_fdb(mp
, db
);
918 if ((error
= xfs_da_read_buf(tp
, dp
, xfs_dir2_db_to_da(mp
, fdb
),
919 -1, &fbp
, XFS_DATA_FORK
))) {
923 ASSERT(free
->hdr
.magic
== cpu_to_be32(XFS_DIR2_FREE_MAGIC
));
924 ASSERT(be32_to_cpu(free
->hdr
.firstdb
) ==
925 xfs_dir2_free_max_bests(mp
) *
926 (fdb
- XFS_DIR2_FREE_FIRSTDB(mp
)));
928 * Calculate which entry we need to fix.
930 findex
= xfs_dir2_db_to_fdindex(mp
, db
);
931 longest
= be16_to_cpu(hdr
->bestfree
[0].length
);
933 * If the data block is now empty we can get rid of it
936 if (longest
== mp
->m_dirblksize
- (uint
)sizeof(*hdr
)) {
938 * Try to punch out the data block.
940 error
= xfs_dir2_shrink_inode(args
, db
, dbp
);
946 * We can get ENOSPC if there's no space reservation.
947 * In this case just drop the buffer and some one else
948 * will eventually get rid of the empty block.
950 else if (!(error
== ENOSPC
&& args
->total
== 0))
954 * If we got rid of the data block, we can eliminate that entry
959 * One less used entry in the free table.
961 be32_add_cpu(&free
->hdr
.nused
, -1);
962 xfs_dir2_free_log_header(tp
, fbp
);
964 * If this was the last entry in the table, we can
965 * trim the table size back. There might be other
966 * entries at the end referring to non-existent
967 * data blocks, get those too.
969 if (findex
== be32_to_cpu(free
->hdr
.nvalid
) - 1) {
970 int i
; /* free entry index */
974 free
->bests
[i
] == cpu_to_be16(NULLDATAOFF
);
977 free
->hdr
.nvalid
= cpu_to_be32(i
+ 1);
981 * Not the last entry, just punch it out.
984 free
->bests
[findex
] = cpu_to_be16(NULLDATAOFF
);
988 * If there are no useful entries left in the block,
989 * get rid of the block if we can.
991 if (!free
->hdr
.nused
) {
992 error
= xfs_dir2_shrink_inode(args
, fdb
, fbp
);
996 } else if (error
!= ENOSPC
|| args
->total
!= 0)
999 * It's possible to get ENOSPC if there is no
1000 * space reservation. In this case some one
1001 * else will eventually get rid of this block.
1006 * Data block is not empty, just set the free entry to
1010 free
->bests
[findex
] = cpu_to_be16(longest
);
1014 * Log the free entry that changed, unless we got rid of it.
1017 xfs_dir2_free_log_bests(tp
, fbp
, findex
, findex
);
1019 xfs_dir2_leafn_check(dp
, bp
);
1021 * Return indication of whether this leaf block is empty enough
1022 * to justify trying to join it with a neighbor.
1025 ((uint
)sizeof(leaf
->hdr
) +
1026 (uint
)sizeof(leaf
->ents
[0]) *
1027 (be16_to_cpu(leaf
->hdr
.count
) - be16_to_cpu(leaf
->hdr
.stale
))) <
1033 * Split the leaf entries in the old block into old and new blocks.
1036 xfs_dir2_leafn_split(
1037 xfs_da_state_t
*state
, /* btree cursor */
1038 xfs_da_state_blk_t
*oldblk
, /* original block */
1039 xfs_da_state_blk_t
*newblk
) /* newly created block */
1041 xfs_da_args_t
*args
; /* operation arguments */
1042 xfs_dablk_t blkno
; /* new leaf block number */
1043 int error
; /* error return value */
1044 xfs_mount_t
*mp
; /* filesystem mount point */
1047 * Allocate space for a new leaf node.
1050 mp
= args
->dp
->i_mount
;
1051 ASSERT(args
!= NULL
);
1052 ASSERT(oldblk
->magic
== XFS_DIR2_LEAFN_MAGIC
);
1053 error
= xfs_da_grow_inode(args
, &blkno
);
1058 * Initialize the new leaf block.
1060 error
= xfs_dir2_leaf_init(args
, xfs_dir2_da_to_db(mp
, blkno
),
1061 &newblk
->bp
, XFS_DIR2_LEAFN_MAGIC
);
1065 newblk
->blkno
= blkno
;
1066 newblk
->magic
= XFS_DIR2_LEAFN_MAGIC
;
1068 * Rebalance the entries across the two leaves, link the new
1069 * block into the leaves.
1071 xfs_dir2_leafn_rebalance(state
, oldblk
, newblk
);
1072 error
= xfs_da_blk_link(state
, oldblk
, newblk
);
1077 * Insert the new entry in the correct block.
1080 error
= xfs_dir2_leafn_add(oldblk
->bp
, args
, oldblk
->index
);
1082 error
= xfs_dir2_leafn_add(newblk
->bp
, args
, newblk
->index
);
1084 * Update last hashval in each block since we added the name.
1086 oldblk
->hashval
= xfs_dir2_leafn_lasthash(oldblk
->bp
, NULL
);
1087 newblk
->hashval
= xfs_dir2_leafn_lasthash(newblk
->bp
, NULL
);
1088 xfs_dir2_leafn_check(args
->dp
, oldblk
->bp
);
1089 xfs_dir2_leafn_check(args
->dp
, newblk
->bp
);
1094 * Check a leaf block and its neighbors to see if the block should be
1095 * collapsed into one or the other neighbor. Always keep the block
1096 * with the smaller block number.
1097 * If the current block is over 50% full, don't try to join it, return 0.
1098 * If the block is empty, fill in the state structure and return 2.
1099 * If it can be collapsed, fill in the state structure and return 1.
1100 * If nothing can be done, return 0.
1103 xfs_dir2_leafn_toosmall(
1104 xfs_da_state_t
*state
, /* btree cursor */
1105 int *action
) /* resulting action to take */
1107 xfs_da_state_blk_t
*blk
; /* leaf block */
1108 xfs_dablk_t blkno
; /* leaf block number */
1109 struct xfs_buf
*bp
; /* leaf buffer */
1110 int bytes
; /* bytes in use */
1111 int count
; /* leaf live entry count */
1112 int error
; /* error return value */
1113 int forward
; /* sibling block direction */
1114 int i
; /* sibling counter */
1115 xfs_da_blkinfo_t
*info
; /* leaf block header */
1116 xfs_dir2_leaf_t
*leaf
; /* leaf structure */
1117 int rval
; /* result from path_shift */
1120 * Check for the degenerate case of the block being over 50% full.
1121 * If so, it's not worth even looking to see if we might be able
1122 * to coalesce with a sibling.
1124 blk
= &state
->path
.blk
[state
->path
.active
- 1];
1125 info
= blk
->bp
->b_addr
;
1126 ASSERT(info
->magic
== cpu_to_be16(XFS_DIR2_LEAFN_MAGIC
));
1127 leaf
= (xfs_dir2_leaf_t
*)info
;
1128 count
= be16_to_cpu(leaf
->hdr
.count
) - be16_to_cpu(leaf
->hdr
.stale
);
1129 bytes
= (uint
)sizeof(leaf
->hdr
) + count
* (uint
)sizeof(leaf
->ents
[0]);
1130 if (bytes
> (state
->blocksize
>> 1)) {
1132 * Blk over 50%, don't try to join.
1138 * Check for the degenerate case of the block being empty.
1139 * If the block is empty, we'll simply delete it, no need to
1140 * coalesce it with a sibling block. We choose (arbitrarily)
1141 * to merge with the forward block unless it is NULL.
1145 * Make altpath point to the block we want to keep and
1146 * path point to the block we want to drop (this one).
1148 forward
= (info
->forw
!= 0);
1149 memcpy(&state
->altpath
, &state
->path
, sizeof(state
->path
));
1150 error
= xfs_da_path_shift(state
, &state
->altpath
, forward
, 0,
1154 *action
= rval
? 2 : 0;
1158 * Examine each sibling block to see if we can coalesce with
1159 * at least 25% free space to spare. We need to figure out
1160 * whether to merge with the forward or the backward block.
1161 * We prefer coalescing with the lower numbered sibling so as
1162 * to shrink a directory over time.
1164 forward
= be32_to_cpu(info
->forw
) < be32_to_cpu(info
->back
);
1165 for (i
= 0, bp
= NULL
; i
< 2; forward
= !forward
, i
++) {
1166 blkno
= forward
? be32_to_cpu(info
->forw
) : be32_to_cpu(info
->back
);
1170 * Read the sibling leaf block.
1173 xfs_da_read_buf(state
->args
->trans
, state
->args
->dp
, blkno
,
1174 -1, &bp
, XFS_DATA_FORK
))) {
1179 * Count bytes in the two blocks combined.
1181 leaf
= (xfs_dir2_leaf_t
*)info
;
1182 count
= be16_to_cpu(leaf
->hdr
.count
) - be16_to_cpu(leaf
->hdr
.stale
);
1183 bytes
= state
->blocksize
- (state
->blocksize
>> 2);
1185 ASSERT(leaf
->hdr
.info
.magic
== cpu_to_be16(XFS_DIR2_LEAFN_MAGIC
));
1186 count
+= be16_to_cpu(leaf
->hdr
.count
) - be16_to_cpu(leaf
->hdr
.stale
);
1187 bytes
-= count
* (uint
)sizeof(leaf
->ents
[0]);
1189 * Fits with at least 25% to spare.
1193 xfs_trans_brelse(state
->args
->trans
, bp
);
1196 * Didn't like either block, give up.
1204 * Make altpath point to the block we want to keep (the lower
1205 * numbered block) and path point to the block we want to drop.
1207 memcpy(&state
->altpath
, &state
->path
, sizeof(state
->path
));
1208 if (blkno
< blk
->blkno
)
1209 error
= xfs_da_path_shift(state
, &state
->altpath
, forward
, 0,
1212 error
= xfs_da_path_shift(state
, &state
->path
, forward
, 0,
1217 *action
= rval
? 0 : 1;
1222 * Move all the leaf entries from drop_blk to save_blk.
1223 * This is done as part of a join operation.
1226 xfs_dir2_leafn_unbalance(
1227 xfs_da_state_t
*state
, /* cursor */
1228 xfs_da_state_blk_t
*drop_blk
, /* dead block */
1229 xfs_da_state_blk_t
*save_blk
) /* surviving block */
1231 xfs_da_args_t
*args
; /* operation arguments */
1232 xfs_dir2_leaf_t
*drop_leaf
; /* dead leaf structure */
1233 xfs_dir2_leaf_t
*save_leaf
; /* surviving leaf structure */
1236 ASSERT(drop_blk
->magic
== XFS_DIR2_LEAFN_MAGIC
);
1237 ASSERT(save_blk
->magic
== XFS_DIR2_LEAFN_MAGIC
);
1238 drop_leaf
= drop_blk
->bp
->b_addr
;
1239 save_leaf
= save_blk
->bp
->b_addr
;
1240 ASSERT(drop_leaf
->hdr
.info
.magic
== cpu_to_be16(XFS_DIR2_LEAFN_MAGIC
));
1241 ASSERT(save_leaf
->hdr
.info
.magic
== cpu_to_be16(XFS_DIR2_LEAFN_MAGIC
));
1243 * If there are any stale leaf entries, take this opportunity
1246 if (drop_leaf
->hdr
.stale
)
1247 xfs_dir2_leaf_compact(args
, drop_blk
->bp
);
1248 if (save_leaf
->hdr
.stale
)
1249 xfs_dir2_leaf_compact(args
, save_blk
->bp
);
1251 * Move the entries from drop to the appropriate end of save.
1253 drop_blk
->hashval
= be32_to_cpu(drop_leaf
->ents
[be16_to_cpu(drop_leaf
->hdr
.count
) - 1].hashval
);
1254 if (xfs_dir2_leafn_order(save_blk
->bp
, drop_blk
->bp
))
1255 xfs_dir2_leafn_moveents(args
, drop_blk
->bp
, 0, save_blk
->bp
, 0,
1256 be16_to_cpu(drop_leaf
->hdr
.count
));
1258 xfs_dir2_leafn_moveents(args
, drop_blk
->bp
, 0, save_blk
->bp
,
1259 be16_to_cpu(save_leaf
->hdr
.count
), be16_to_cpu(drop_leaf
->hdr
.count
));
1260 save_blk
->hashval
= be32_to_cpu(save_leaf
->ents
[be16_to_cpu(save_leaf
->hdr
.count
) - 1].hashval
);
1261 xfs_dir2_leafn_check(args
->dp
, save_blk
->bp
);
1265 * Top-level node form directory addname routine.
1268 xfs_dir2_node_addname(
1269 xfs_da_args_t
*args
) /* operation arguments */
1271 xfs_da_state_blk_t
*blk
; /* leaf block for insert */
1272 int error
; /* error return value */
1273 int rval
; /* sub-return value */
1274 xfs_da_state_t
*state
; /* btree cursor */
1276 trace_xfs_dir2_node_addname(args
);
1279 * Allocate and initialize the state (btree cursor).
1281 state
= xfs_da_state_alloc();
1283 state
->mp
= args
->dp
->i_mount
;
1284 state
->blocksize
= state
->mp
->m_dirblksize
;
1285 state
->node_ents
= state
->mp
->m_dir_node_ents
;
1287 * Look up the name. We're not supposed to find it, but
1288 * this gives us the insertion point.
1290 error
= xfs_da_node_lookup_int(state
, &rval
);
1293 if (rval
!= ENOENT
) {
1297 * Add the data entry to a data block.
1298 * Extravalid is set to a freeblock found by lookup.
1300 rval
= xfs_dir2_node_addname_int(args
,
1301 state
->extravalid
? &state
->extrablk
: NULL
);
1305 blk
= &state
->path
.blk
[state
->path
.active
- 1];
1306 ASSERT(blk
->magic
== XFS_DIR2_LEAFN_MAGIC
);
1308 * Add the new leaf entry.
1310 rval
= xfs_dir2_leafn_add(blk
->bp
, args
, blk
->index
);
1313 * It worked, fix the hash values up the btree.
1315 if (!(args
->op_flags
& XFS_DA_OP_JUSTCHECK
))
1316 xfs_da_fixhashpath(state
, &state
->path
);
1319 * It didn't work, we need to split the leaf block.
1321 if (args
->total
== 0) {
1322 ASSERT(rval
== ENOSPC
);
1326 * Split the leaf block and insert the new entry.
1328 rval
= xfs_da_split(state
);
1331 xfs_da_state_free(state
);
1336 * Add the data entry for a node-format directory name addition.
1337 * The leaf entry is added in xfs_dir2_leafn_add.
1338 * We may enter with a freespace block that the lookup found.
1340 static int /* error */
1341 xfs_dir2_node_addname_int(
1342 xfs_da_args_t
*args
, /* operation arguments */
1343 xfs_da_state_blk_t
*fblk
) /* optional freespace block */
1345 xfs_dir2_data_hdr_t
*hdr
; /* data block header */
1346 xfs_dir2_db_t dbno
; /* data block number */
1347 struct xfs_buf
*dbp
; /* data block buffer */
1348 xfs_dir2_data_entry_t
*dep
; /* data entry pointer */
1349 xfs_inode_t
*dp
; /* incore directory inode */
1350 xfs_dir2_data_unused_t
*dup
; /* data unused entry pointer */
1351 int error
; /* error return value */
1352 xfs_dir2_db_t fbno
; /* freespace block number */
1353 struct xfs_buf
*fbp
; /* freespace buffer */
1354 int findex
; /* freespace entry index */
1355 xfs_dir2_free_t
*free
=NULL
; /* freespace block structure */
1356 xfs_dir2_db_t ifbno
; /* initial freespace block no */
1357 xfs_dir2_db_t lastfbno
=0; /* highest freespace block no */
1358 int length
; /* length of the new entry */
1359 int logfree
; /* need to log free entry */
1360 xfs_mount_t
*mp
; /* filesystem mount point */
1361 int needlog
; /* need to log data header */
1362 int needscan
; /* need to rescan data frees */
1363 __be16
*tagp
; /* data entry tag pointer */
1364 xfs_trans_t
*tp
; /* transaction pointer */
1369 length
= xfs_dir2_data_entsize(args
->namelen
);
1371 * If we came in with a freespace block that means that lookup
1372 * found an entry with our hash value. This is the freespace
1373 * block for that data entry.
1378 * Remember initial freespace block number.
1380 ifbno
= fblk
->blkno
;
1382 ASSERT(free
->hdr
.magic
== cpu_to_be32(XFS_DIR2_FREE_MAGIC
));
1383 findex
= fblk
->index
;
1385 * This means the free entry showed that the data block had
1386 * space for our entry, so we remembered it.
1387 * Use that data block.
1390 ASSERT(findex
< be32_to_cpu(free
->hdr
.nvalid
));
1391 ASSERT(be16_to_cpu(free
->bests
[findex
]) != NULLDATAOFF
);
1392 ASSERT(be16_to_cpu(free
->bests
[findex
]) >= length
);
1393 dbno
= be32_to_cpu(free
->hdr
.firstdb
) + findex
;
1396 * The data block looked at didn't have enough room.
1397 * We'll start at the beginning of the freespace entries.
1405 * Didn't come in with a freespace block, so don't have a data block.
1413 * If we don't have a data block yet, we're going to scan the
1414 * freespace blocks looking for one. Figure out what the
1415 * highest freespace block number is.
1418 xfs_fileoff_t fo
; /* freespace block number */
1420 if ((error
= xfs_bmap_last_offset(tp
, dp
, &fo
, XFS_DATA_FORK
)))
1422 lastfbno
= xfs_dir2_da_to_db(mp
, (xfs_dablk_t
)fo
);
1426 * While we haven't identified a data block, search the freeblock
1427 * data for a good data block. If we find a null freeblock entry,
1428 * indicating a hole in the data blocks, remember that.
1430 while (dbno
== -1) {
1432 * If we don't have a freeblock in hand, get the next one.
1436 * Happens the first time through unless lookup gave
1437 * us a freespace block to start with.
1440 fbno
= XFS_DIR2_FREE_FIRSTDB(mp
);
1442 * If it's ifbno we already looked at it.
1447 * If it's off the end we're done.
1449 if (fbno
>= lastfbno
)
1452 * Read the block. There can be holes in the
1453 * freespace blocks, so this might not succeed.
1454 * This should be really rare, so there's no reason
1457 if ((error
= xfs_da_read_buf(tp
, dp
,
1458 xfs_dir2_db_to_da(mp
, fbno
), -2, &fbp
,
1462 if (unlikely(fbp
== NULL
)) {
1466 ASSERT(free
->hdr
.magic
== cpu_to_be32(XFS_DIR2_FREE_MAGIC
));
1470 * Look at the current free entry. Is it good enough?
1472 if (be16_to_cpu(free
->bests
[findex
]) != NULLDATAOFF
&&
1473 be16_to_cpu(free
->bests
[findex
]) >= length
)
1474 dbno
= be32_to_cpu(free
->hdr
.firstdb
) + findex
;
1477 * Are we done with the freeblock?
1479 if (++findex
== be32_to_cpu(free
->hdr
.nvalid
)) {
1483 xfs_trans_brelse(tp
, fbp
);
1485 if (fblk
&& fblk
->bp
)
1491 * If we don't have a data block, we need to allocate one and make
1492 * the freespace entries refer to it.
1494 if (unlikely(dbno
== -1)) {
1496 * Not allowed to allocate, return failure.
1498 if ((args
->op_flags
& XFS_DA_OP_JUSTCHECK
) || args
->total
== 0)
1499 return XFS_ERROR(ENOSPC
);
1502 * Allocate and initialize the new data block.
1504 if (unlikely((error
= xfs_dir2_grow_inode(args
,
1505 XFS_DIR2_DATA_SPACE
,
1507 (error
= xfs_dir2_data_init(args
, dbno
, &dbp
))))
1511 * If (somehow) we have a freespace block, get rid of it.
1514 xfs_trans_brelse(tp
, fbp
);
1515 if (fblk
&& fblk
->bp
)
1519 * Get the freespace block corresponding to the data block
1520 * that was just allocated.
1522 fbno
= xfs_dir2_db_to_fdb(mp
, dbno
);
1523 if (unlikely(error
= xfs_da_read_buf(tp
, dp
,
1524 xfs_dir2_db_to_da(mp
, fbno
), -2, &fbp
,
1529 * If there wasn't a freespace block, the read will
1530 * return a NULL fbp. Allocate and initialize a new one.
1533 if ((error
= xfs_dir2_grow_inode(args
, XFS_DIR2_FREE_SPACE
,
1538 if (unlikely(xfs_dir2_db_to_fdb(mp
, dbno
) != fbno
)) {
1540 "%s: dir ino %llu needed freesp block %lld for\n"
1541 " data block %lld, got %lld ifbno %llu lastfbno %d",
1542 __func__
, (unsigned long long)dp
->i_ino
,
1543 (long long)xfs_dir2_db_to_fdb(mp
, dbno
),
1544 (long long)dbno
, (long long)fbno
,
1545 (unsigned long long)ifbno
, lastfbno
);
1548 " fblk 0x%p blkno %llu index %d magic 0x%x",
1550 (unsigned long long)fblk
->blkno
,
1554 xfs_alert(mp
, " ... fblk is NULL");
1556 XFS_ERROR_REPORT("xfs_dir2_node_addname_int",
1557 XFS_ERRLEVEL_LOW
, mp
);
1558 return XFS_ERROR(EFSCORRUPTED
);
1562 * Get a buffer for the new block.
1564 if ((error
= xfs_da_get_buf(tp
, dp
,
1565 xfs_dir2_db_to_da(mp
, fbno
),
1566 -1, &fbp
, XFS_DATA_FORK
))) {
1569 ASSERT(fbp
!= NULL
);
1572 * Initialize the new block to be empty, and remember
1573 * its first slot as our empty slot.
1576 free
->hdr
.magic
= cpu_to_be32(XFS_DIR2_FREE_MAGIC
);
1577 free
->hdr
.firstdb
= cpu_to_be32(
1578 (fbno
- XFS_DIR2_FREE_FIRSTDB(mp
)) *
1579 xfs_dir2_free_max_bests(mp
));
1580 free
->hdr
.nvalid
= 0;
1581 free
->hdr
.nused
= 0;
1584 ASSERT(free
->hdr
.magic
== cpu_to_be32(XFS_DIR2_FREE_MAGIC
));
1588 * Set the freespace block index from the data block number.
1590 findex
= xfs_dir2_db_to_fdindex(mp
, dbno
);
1592 * If it's after the end of the current entries in the
1593 * freespace block, extend that table.
1595 if (findex
>= be32_to_cpu(free
->hdr
.nvalid
)) {
1596 ASSERT(findex
< xfs_dir2_free_max_bests(mp
));
1597 free
->hdr
.nvalid
= cpu_to_be32(findex
+ 1);
1599 * Tag new entry so nused will go up.
1601 free
->bests
[findex
] = cpu_to_be16(NULLDATAOFF
);
1604 * If this entry was for an empty data block
1605 * (this should always be true) then update the header.
1607 if (free
->bests
[findex
] == cpu_to_be16(NULLDATAOFF
)) {
1608 be32_add_cpu(&free
->hdr
.nused
, 1);
1609 xfs_dir2_free_log_header(tp
, fbp
);
1612 * Update the real value in the table.
1613 * We haven't allocated the data entry yet so this will
1617 free
->bests
[findex
] = hdr
->bestfree
[0].length
;
1621 * We had a data block so we don't have to make a new one.
1625 * If just checking, we succeeded.
1627 if (args
->op_flags
& XFS_DA_OP_JUSTCHECK
)
1631 * Read the data block in.
1633 error
= xfs_da_read_buf(tp
, dp
, xfs_dir2_db_to_da(mp
, dbno
),
1634 -1, &dbp
, XFS_DATA_FORK
);
1640 ASSERT(be16_to_cpu(hdr
->bestfree
[0].length
) >= length
);
1642 * Point to the existing unused space.
1644 dup
= (xfs_dir2_data_unused_t
*)
1645 ((char *)hdr
+ be16_to_cpu(hdr
->bestfree
[0].offset
));
1646 needscan
= needlog
= 0;
1648 * Mark the first part of the unused space, inuse for us.
1650 xfs_dir2_data_use_free(tp
, dbp
, dup
,
1651 (xfs_dir2_data_aoff_t
)((char *)dup
- (char *)hdr
), length
,
1652 &needlog
, &needscan
);
1654 * Fill in the new entry and log it.
1656 dep
= (xfs_dir2_data_entry_t
*)dup
;
1657 dep
->inumber
= cpu_to_be64(args
->inumber
);
1658 dep
->namelen
= args
->namelen
;
1659 memcpy(dep
->name
, args
->name
, dep
->namelen
);
1660 tagp
= xfs_dir2_data_entry_tag_p(dep
);
1661 *tagp
= cpu_to_be16((char *)dep
- (char *)hdr
);
1662 xfs_dir2_data_log_entry(tp
, dbp
, dep
);
1664 * Rescan the block for bestfree if needed.
1667 xfs_dir2_data_freescan(mp
, hdr
, &needlog
);
1669 * Log the data block header if needed.
1672 xfs_dir2_data_log_header(tp
, dbp
);
1674 * If the freespace entry is now wrong, update it.
1676 if (be16_to_cpu(free
->bests
[findex
]) != be16_to_cpu(hdr
->bestfree
[0].length
)) {
1677 free
->bests
[findex
] = hdr
->bestfree
[0].length
;
1681 * Log the freespace entry if needed.
1684 xfs_dir2_free_log_bests(tp
, fbp
, findex
, findex
);
1686 * Return the data block and offset in args, then drop the data block.
1688 args
->blkno
= (xfs_dablk_t
)dbno
;
1689 args
->index
= be16_to_cpu(*tagp
);
1694 * Lookup an entry in a node-format directory.
1695 * All the real work happens in xfs_da_node_lookup_int.
1696 * The only real output is the inode number of the entry.
1699 xfs_dir2_node_lookup(
1700 xfs_da_args_t
*args
) /* operation arguments */
1702 int error
; /* error return value */
1703 int i
; /* btree level */
1704 int rval
; /* operation return value */
1705 xfs_da_state_t
*state
; /* btree cursor */
1707 trace_xfs_dir2_node_lookup(args
);
1710 * Allocate and initialize the btree cursor.
1712 state
= xfs_da_state_alloc();
1714 state
->mp
= args
->dp
->i_mount
;
1715 state
->blocksize
= state
->mp
->m_dirblksize
;
1716 state
->node_ents
= state
->mp
->m_dir_node_ents
;
1718 * Fill in the path to the entry in the cursor.
1720 error
= xfs_da_node_lookup_int(state
, &rval
);
1723 else if (rval
== ENOENT
&& args
->cmpresult
== XFS_CMP_CASE
) {
1724 /* If a CI match, dup the actual name and return EEXIST */
1725 xfs_dir2_data_entry_t
*dep
;
1727 dep
= (xfs_dir2_data_entry_t
*)
1728 ((char *)state
->extrablk
.bp
->b_addr
+
1729 state
->extrablk
.index
);
1730 rval
= xfs_dir_cilookup_result(args
, dep
->name
, dep
->namelen
);
1733 * Release the btree blocks and leaf block.
1735 for (i
= 0; i
< state
->path
.active
; i
++) {
1736 xfs_trans_brelse(args
->trans
, state
->path
.blk
[i
].bp
);
1737 state
->path
.blk
[i
].bp
= NULL
;
1740 * Release the data block if we have it.
1742 if (state
->extravalid
&& state
->extrablk
.bp
) {
1743 xfs_trans_brelse(args
->trans
, state
->extrablk
.bp
);
1744 state
->extrablk
.bp
= NULL
;
1746 xfs_da_state_free(state
);
1751 * Remove an entry from a node-format directory.
1754 xfs_dir2_node_removename(
1755 xfs_da_args_t
*args
) /* operation arguments */
1757 xfs_da_state_blk_t
*blk
; /* leaf block */
1758 int error
; /* error return value */
1759 int rval
; /* operation return value */
1760 xfs_da_state_t
*state
; /* btree cursor */
1762 trace_xfs_dir2_node_removename(args
);
1765 * Allocate and initialize the btree cursor.
1767 state
= xfs_da_state_alloc();
1769 state
->mp
= args
->dp
->i_mount
;
1770 state
->blocksize
= state
->mp
->m_dirblksize
;
1771 state
->node_ents
= state
->mp
->m_dir_node_ents
;
1773 * Look up the entry we're deleting, set up the cursor.
1775 error
= xfs_da_node_lookup_int(state
, &rval
);
1779 * Didn't find it, upper layer screwed up.
1781 if (rval
!= EEXIST
) {
1782 xfs_da_state_free(state
);
1785 blk
= &state
->path
.blk
[state
->path
.active
- 1];
1786 ASSERT(blk
->magic
== XFS_DIR2_LEAFN_MAGIC
);
1787 ASSERT(state
->extravalid
);
1789 * Remove the leaf and data entries.
1790 * Extrablk refers to the data block.
1792 error
= xfs_dir2_leafn_remove(args
, blk
->bp
, blk
->index
,
1793 &state
->extrablk
, &rval
);
1797 * Fix the hash values up the btree.
1799 xfs_da_fixhashpath(state
, &state
->path
);
1801 * If we need to join leaf blocks, do it.
1803 if (rval
&& state
->path
.active
> 1)
1804 error
= xfs_da_join(state
);
1806 * If no errors so far, try conversion to leaf format.
1809 error
= xfs_dir2_node_to_leaf(state
);
1810 xfs_da_state_free(state
);
1815 * Replace an entry's inode number in a node-format directory.
1818 xfs_dir2_node_replace(
1819 xfs_da_args_t
*args
) /* operation arguments */
1821 xfs_da_state_blk_t
*blk
; /* leaf block */
1822 xfs_dir2_data_hdr_t
*hdr
; /* data block header */
1823 xfs_dir2_data_entry_t
*dep
; /* data entry changed */
1824 int error
; /* error return value */
1825 int i
; /* btree level */
1826 xfs_ino_t inum
; /* new inode number */
1827 xfs_dir2_leaf_t
*leaf
; /* leaf structure */
1828 xfs_dir2_leaf_entry_t
*lep
; /* leaf entry being changed */
1829 int rval
; /* internal return value */
1830 xfs_da_state_t
*state
; /* btree cursor */
1832 trace_xfs_dir2_node_replace(args
);
1835 * Allocate and initialize the btree cursor.
1837 state
= xfs_da_state_alloc();
1839 state
->mp
= args
->dp
->i_mount
;
1840 state
->blocksize
= state
->mp
->m_dirblksize
;
1841 state
->node_ents
= state
->mp
->m_dir_node_ents
;
1842 inum
= args
->inumber
;
1844 * Lookup the entry to change in the btree.
1846 error
= xfs_da_node_lookup_int(state
, &rval
);
1851 * It should be found, since the vnodeops layer has looked it up
1852 * and locked it. But paranoia is good.
1854 if (rval
== EEXIST
) {
1856 * Find the leaf entry.
1858 blk
= &state
->path
.blk
[state
->path
.active
- 1];
1859 ASSERT(blk
->magic
== XFS_DIR2_LEAFN_MAGIC
);
1860 leaf
= blk
->bp
->b_addr
;
1861 lep
= &leaf
->ents
[blk
->index
];
1862 ASSERT(state
->extravalid
);
1864 * Point to the data entry.
1866 hdr
= state
->extrablk
.bp
->b_addr
;
1867 ASSERT(hdr
->magic
== cpu_to_be32(XFS_DIR2_DATA_MAGIC
));
1868 dep
= (xfs_dir2_data_entry_t
*)
1870 xfs_dir2_dataptr_to_off(state
->mp
, be32_to_cpu(lep
->address
)));
1871 ASSERT(inum
!= be64_to_cpu(dep
->inumber
));
1873 * Fill in the new inode number and log the entry.
1875 dep
->inumber
= cpu_to_be64(inum
);
1876 xfs_dir2_data_log_entry(args
->trans
, state
->extrablk
.bp
, dep
);
1880 * Didn't find it, and we're holding a data block. Drop it.
1882 else if (state
->extravalid
) {
1883 xfs_trans_brelse(args
->trans
, state
->extrablk
.bp
);
1884 state
->extrablk
.bp
= NULL
;
1887 * Release all the buffers in the cursor.
1889 for (i
= 0; i
< state
->path
.active
; i
++) {
1890 xfs_trans_brelse(args
->trans
, state
->path
.blk
[i
].bp
);
1891 state
->path
.blk
[i
].bp
= NULL
;
1893 xfs_da_state_free(state
);
1898 * Trim off a trailing empty freespace block.
1899 * Return (in rvalp) 1 if we did it, 0 if not.
1902 xfs_dir2_node_trim_free(
1903 xfs_da_args_t
*args
, /* operation arguments */
1904 xfs_fileoff_t fo
, /* free block number */
1905 int *rvalp
) /* out: did something */
1907 struct xfs_buf
*bp
; /* freespace buffer */
1908 xfs_inode_t
*dp
; /* incore directory inode */
1909 int error
; /* error return code */
1910 xfs_dir2_free_t
*free
; /* freespace structure */
1911 xfs_mount_t
*mp
; /* filesystem mount point */
1912 xfs_trans_t
*tp
; /* transaction pointer */
1918 * Read the freespace block.
1920 if (unlikely(error
= xfs_da_read_buf(tp
, dp
, (xfs_dablk_t
)fo
, -2, &bp
,
1926 * There can be holes in freespace. If fo is a hole, there's
1933 ASSERT(free
->hdr
.magic
== cpu_to_be32(XFS_DIR2_FREE_MAGIC
));
1935 * If there are used entries, there's nothing to do.
1937 if (be32_to_cpu(free
->hdr
.nused
) > 0) {
1938 xfs_trans_brelse(tp
, bp
);
1943 * Blow the block away.
1946 xfs_dir2_shrink_inode(args
, xfs_dir2_da_to_db(mp
, (xfs_dablk_t
)fo
),
1949 * Can't fail with ENOSPC since that only happens with no
1950 * space reservation, when breaking up an extent into two
1951 * pieces. This is the last block of an extent.
1953 ASSERT(error
!= ENOSPC
);
1954 xfs_trans_brelse(tp
, bp
);
1958 * Return that we succeeded.
This page took 0.091099 seconds and 6 git commands to generate.