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"
23 #include "xfs_trans.h"
27 #include "xfs_mount.h"
28 #include "xfs_da_btree.h"
29 #include "xfs_bmap_btree.h"
30 #include "xfs_dir2_sf.h"
31 #include "xfs_attr_sf.h"
32 #include "xfs_dinode.h"
33 #include "xfs_inode.h"
35 #include "xfs_dir2_data.h"
36 #include "xfs_dir2_leaf.h"
37 #include "xfs_dir2_block.h"
38 #include "xfs_dir2_node.h"
39 #include "xfs_error.h"
40 #include "xfs_trace.h"
43 * Function declarations.
45 static void xfs_dir2_free_log_header(xfs_trans_t
*tp
, xfs_dabuf_t
*bp
);
46 static int xfs_dir2_leafn_add(xfs_dabuf_t
*bp
, xfs_da_args_t
*args
, int index
);
48 static void xfs_dir2_leafn_check(xfs_inode_t
*dp
, xfs_dabuf_t
*bp
);
50 #define xfs_dir2_leafn_check(dp, bp)
52 static void xfs_dir2_leafn_moveents(xfs_da_args_t
*args
, xfs_dabuf_t
*bp_s
,
53 int start_s
, xfs_dabuf_t
*bp_d
, int start_d
,
55 static void xfs_dir2_leafn_rebalance(xfs_da_state_t
*state
,
56 xfs_da_state_blk_t
*blk1
,
57 xfs_da_state_blk_t
*blk2
);
58 static int xfs_dir2_leafn_remove(xfs_da_args_t
*args
, xfs_dabuf_t
*bp
,
59 int index
, xfs_da_state_blk_t
*dblk
,
61 static int xfs_dir2_node_addname_int(xfs_da_args_t
*args
,
62 xfs_da_state_blk_t
*fblk
);
65 * Log entries from a freespace block.
68 xfs_dir2_free_log_bests(
69 xfs_trans_t
*tp
, /* transaction pointer */
70 xfs_dabuf_t
*bp
, /* freespace buffer */
71 int first
, /* first entry to log */
72 int last
) /* last entry to log */
74 xfs_dir2_free_t
*free
; /* freespace structure */
77 ASSERT(be32_to_cpu(free
->hdr
.magic
) == XFS_DIR2_FREE_MAGIC
);
78 xfs_da_log_buf(tp
, bp
,
79 (uint
)((char *)&free
->bests
[first
] - (char *)free
),
80 (uint
)((char *)&free
->bests
[last
] - (char *)free
+
81 sizeof(free
->bests
[0]) - 1));
85 * Log header from a freespace block.
88 xfs_dir2_free_log_header(
89 xfs_trans_t
*tp
, /* transaction pointer */
90 xfs_dabuf_t
*bp
) /* freespace buffer */
92 xfs_dir2_free_t
*free
; /* freespace structure */
95 ASSERT(be32_to_cpu(free
->hdr
.magic
) == XFS_DIR2_FREE_MAGIC
);
96 xfs_da_log_buf(tp
, bp
, (uint
)((char *)&free
->hdr
- (char *)free
),
97 (uint
)(sizeof(xfs_dir2_free_hdr_t
) - 1));
101 * Convert a leaf-format directory to a node-format directory.
102 * We need to change the magic number of the leaf block, and copy
103 * the freespace table out of the leaf block into its own block.
106 xfs_dir2_leaf_to_node(
107 xfs_da_args_t
*args
, /* operation arguments */
108 xfs_dabuf_t
*lbp
) /* leaf buffer */
110 xfs_inode_t
*dp
; /* incore directory inode */
111 int error
; /* error return value */
112 xfs_dabuf_t
*fbp
; /* freespace buffer */
113 xfs_dir2_db_t fdb
; /* freespace block number */
114 xfs_dir2_free_t
*free
; /* freespace structure */
115 __be16
*from
; /* pointer to freespace entry */
116 int i
; /* leaf freespace index */
117 xfs_dir2_leaf_t
*leaf
; /* leaf structure */
118 xfs_dir2_leaf_tail_t
*ltp
; /* leaf tail structure */
119 xfs_mount_t
*mp
; /* filesystem mount point */
120 int n
; /* count of live freespc ents */
121 xfs_dir2_data_off_t off
; /* freespace entry value */
122 __be16
*to
; /* pointer to freespace entry */
123 xfs_trans_t
*tp
; /* transaction pointer */
125 trace_xfs_dir2_leaf_to_node(args
);
131 * Add a freespace block to the directory.
133 if ((error
= xfs_dir2_grow_inode(args
, XFS_DIR2_FREE_SPACE
, &fdb
))) {
136 ASSERT(fdb
== XFS_DIR2_FREE_FIRSTDB(mp
));
138 * Get the buffer for the new freespace block.
140 if ((error
= xfs_da_get_buf(tp
, dp
, xfs_dir2_db_to_da(mp
, fdb
), -1, &fbp
,
147 ltp
= xfs_dir2_leaf_tail_p(mp
, leaf
);
149 * Initialize the freespace block header.
151 free
->hdr
.magic
= cpu_to_be32(XFS_DIR2_FREE_MAGIC
);
152 free
->hdr
.firstdb
= 0;
153 ASSERT(be32_to_cpu(ltp
->bestcount
) <= (uint
)dp
->i_d
.di_size
/ mp
->m_dirblksize
);
154 free
->hdr
.nvalid
= ltp
->bestcount
;
156 * Copy freespace entries from the leaf block to the new block.
157 * Count active entries.
159 for (i
= n
= 0, from
= xfs_dir2_leaf_bests_p(ltp
), to
= free
->bests
;
160 i
< be32_to_cpu(ltp
->bestcount
); i
++, from
++, to
++) {
161 if ((off
= be16_to_cpu(*from
)) != NULLDATAOFF
)
163 *to
= cpu_to_be16(off
);
165 free
->hdr
.nused
= cpu_to_be32(n
);
166 leaf
->hdr
.info
.magic
= cpu_to_be16(XFS_DIR2_LEAFN_MAGIC
);
170 xfs_dir2_leaf_log_header(tp
, lbp
);
171 xfs_dir2_free_log_header(tp
, fbp
);
172 xfs_dir2_free_log_bests(tp
, fbp
, 0, be32_to_cpu(free
->hdr
.nvalid
) - 1);
173 xfs_da_buf_done(fbp
);
174 xfs_dir2_leafn_check(dp
, lbp
);
179 * Add a leaf entry to a leaf block in a node-form directory.
180 * The other work necessary is done from the caller.
182 static int /* error */
184 xfs_dabuf_t
*bp
, /* leaf buffer */
185 xfs_da_args_t
*args
, /* operation arguments */
186 int index
) /* insertion pt for new entry */
188 int compact
; /* compacting stale leaves */
189 xfs_inode_t
*dp
; /* incore directory inode */
190 int highstale
; /* next stale entry */
191 xfs_dir2_leaf_t
*leaf
; /* leaf structure */
192 xfs_dir2_leaf_entry_t
*lep
; /* leaf entry */
193 int lfloghigh
; /* high leaf entry logging */
194 int lfloglow
; /* low leaf entry logging */
195 int lowstale
; /* previous stale entry */
196 xfs_mount_t
*mp
; /* filesystem mount point */
197 xfs_trans_t
*tp
; /* transaction pointer */
199 trace_xfs_dir2_leafn_add(args
, index
);
207 * Quick check just to make sure we are not going to index
208 * into other peoples memory
211 return XFS_ERROR(EFSCORRUPTED
);
214 * If there are already the maximum number of leaf entries in
215 * the block, if there are no stale entries it won't fit.
216 * Caller will do a split. If there are stale entries we'll do
220 if (be16_to_cpu(leaf
->hdr
.count
) == xfs_dir2_max_leaf_ents(mp
)) {
221 if (!leaf
->hdr
.stale
)
222 return XFS_ERROR(ENOSPC
);
223 compact
= be16_to_cpu(leaf
->hdr
.stale
) > 1;
226 ASSERT(index
== 0 || be32_to_cpu(leaf
->ents
[index
- 1].hashval
) <= args
->hashval
);
227 ASSERT(index
== be16_to_cpu(leaf
->hdr
.count
) ||
228 be32_to_cpu(leaf
->ents
[index
].hashval
) >= args
->hashval
);
230 if (args
->op_flags
& XFS_DA_OP_JUSTCHECK
)
234 * Compact out all but one stale leaf entry. Leaves behind
235 * the entry closest to index.
238 xfs_dir2_leaf_compact_x1(bp
, &index
, &lowstale
, &highstale
,
239 &lfloglow
, &lfloghigh
);
242 * Set impossible logging indices for this case.
244 else if (leaf
->hdr
.stale
) {
245 lfloglow
= be16_to_cpu(leaf
->hdr
.count
);
249 * No stale entries, just insert a space for the new entry.
251 if (!leaf
->hdr
.stale
) {
252 lep
= &leaf
->ents
[index
];
253 if (index
< be16_to_cpu(leaf
->hdr
.count
))
254 memmove(lep
+ 1, lep
,
255 (be16_to_cpu(leaf
->hdr
.count
) - index
) * sizeof(*lep
));
257 lfloghigh
= be16_to_cpu(leaf
->hdr
.count
);
258 be16_add_cpu(&leaf
->hdr
.count
, 1);
261 * There are stale entries. We'll use one for the new entry.
265 * If we didn't do a compact then we need to figure out
266 * which stale entry will be used.
270 * Find first stale entry before our insertion point.
272 for (lowstale
= index
- 1;
274 be32_to_cpu(leaf
->ents
[lowstale
].address
) !=
275 XFS_DIR2_NULL_DATAPTR
;
279 * Find next stale entry after insertion point.
280 * Stop looking if the answer would be worse than
281 * lowstale already found.
283 for (highstale
= index
;
284 highstale
< be16_to_cpu(leaf
->hdr
.count
) &&
285 be32_to_cpu(leaf
->ents
[highstale
].address
) !=
286 XFS_DIR2_NULL_DATAPTR
&&
288 index
- lowstale
- 1 >= highstale
- index
);
293 * Using the low stale entry.
294 * Shift entries up toward the stale slot.
297 (highstale
== be16_to_cpu(leaf
->hdr
.count
) ||
298 index
- lowstale
- 1 < highstale
- index
)) {
299 ASSERT(be32_to_cpu(leaf
->ents
[lowstale
].address
) ==
300 XFS_DIR2_NULL_DATAPTR
);
301 ASSERT(index
- lowstale
- 1 >= 0);
302 if (index
- lowstale
- 1 > 0)
303 memmove(&leaf
->ents
[lowstale
],
304 &leaf
->ents
[lowstale
+ 1],
305 (index
- lowstale
- 1) * sizeof(*lep
));
306 lep
= &leaf
->ents
[index
- 1];
307 lfloglow
= MIN(lowstale
, lfloglow
);
308 lfloghigh
= MAX(index
- 1, lfloghigh
);
311 * Using the high stale entry.
312 * Shift entries down toward the stale slot.
315 ASSERT(be32_to_cpu(leaf
->ents
[highstale
].address
) ==
316 XFS_DIR2_NULL_DATAPTR
);
317 ASSERT(highstale
- index
>= 0);
318 if (highstale
- index
> 0)
319 memmove(&leaf
->ents
[index
+ 1],
321 (highstale
- index
) * sizeof(*lep
));
322 lep
= &leaf
->ents
[index
];
323 lfloglow
= MIN(index
, lfloglow
);
324 lfloghigh
= MAX(highstale
, lfloghigh
);
326 be16_add_cpu(&leaf
->hdr
.stale
, -1);
329 * Insert the new entry, log everything.
331 lep
->hashval
= cpu_to_be32(args
->hashval
);
332 lep
->address
= cpu_to_be32(xfs_dir2_db_off_to_dataptr(mp
,
333 args
->blkno
, args
->index
));
334 xfs_dir2_leaf_log_header(tp
, bp
);
335 xfs_dir2_leaf_log_ents(tp
, bp
, lfloglow
, lfloghigh
);
336 xfs_dir2_leafn_check(dp
, bp
);
342 * Check internal consistency of a leafn block.
345 xfs_dir2_leafn_check(
346 xfs_inode_t
*dp
, /* incore directory inode */
347 xfs_dabuf_t
*bp
) /* leaf buffer */
349 int i
; /* leaf index */
350 xfs_dir2_leaf_t
*leaf
; /* leaf structure */
351 xfs_mount_t
*mp
; /* filesystem mount point */
352 int stale
; /* count of stale leaves */
356 ASSERT(be16_to_cpu(leaf
->hdr
.info
.magic
) == XFS_DIR2_LEAFN_MAGIC
);
357 ASSERT(be16_to_cpu(leaf
->hdr
.count
) <= xfs_dir2_max_leaf_ents(mp
));
358 for (i
= stale
= 0; i
< be16_to_cpu(leaf
->hdr
.count
); i
++) {
359 if (i
+ 1 < be16_to_cpu(leaf
->hdr
.count
)) {
360 ASSERT(be32_to_cpu(leaf
->ents
[i
].hashval
) <=
361 be32_to_cpu(leaf
->ents
[i
+ 1].hashval
));
363 if (be32_to_cpu(leaf
->ents
[i
].address
) == XFS_DIR2_NULL_DATAPTR
)
366 ASSERT(be16_to_cpu(leaf
->hdr
.stale
) == stale
);
371 * Return the last hash value in the leaf.
372 * Stale entries are ok.
374 xfs_dahash_t
/* hash value */
375 xfs_dir2_leafn_lasthash(
376 xfs_dabuf_t
*bp
, /* leaf buffer */
377 int *count
) /* count of entries in leaf */
379 xfs_dir2_leaf_t
*leaf
; /* leaf structure */
382 ASSERT(be16_to_cpu(leaf
->hdr
.info
.magic
) == XFS_DIR2_LEAFN_MAGIC
);
384 *count
= be16_to_cpu(leaf
->hdr
.count
);
385 if (!leaf
->hdr
.count
)
387 return be32_to_cpu(leaf
->ents
[be16_to_cpu(leaf
->hdr
.count
) - 1].hashval
);
391 * Look up a leaf entry for space to add a name in a node-format leaf block.
392 * The extrablk in state is a freespace block.
395 xfs_dir2_leafn_lookup_for_addname(
396 xfs_dabuf_t
*bp
, /* leaf buffer */
397 xfs_da_args_t
*args
, /* operation arguments */
398 int *indexp
, /* out: leaf entry index */
399 xfs_da_state_t
*state
) /* state to fill in */
401 xfs_dabuf_t
*curbp
= NULL
; /* current data/free buffer */
402 xfs_dir2_db_t curdb
= -1; /* current data block number */
403 xfs_dir2_db_t curfdb
= -1; /* current free block number */
404 xfs_inode_t
*dp
; /* incore directory inode */
405 int error
; /* error return value */
406 int fi
; /* free entry index */
407 xfs_dir2_free_t
*free
= NULL
; /* free block structure */
408 int index
; /* leaf entry index */
409 xfs_dir2_leaf_t
*leaf
; /* leaf structure */
410 int length
; /* length of new data entry */
411 xfs_dir2_leaf_entry_t
*lep
; /* leaf entry */
412 xfs_mount_t
*mp
; /* filesystem mount point */
413 xfs_dir2_db_t newdb
; /* new data block number */
414 xfs_dir2_db_t newfdb
; /* new free block number */
415 xfs_trans_t
*tp
; /* transaction pointer */
421 ASSERT(be16_to_cpu(leaf
->hdr
.info
.magic
) == XFS_DIR2_LEAFN_MAGIC
);
423 ASSERT(be16_to_cpu(leaf
->hdr
.count
) > 0);
425 xfs_dir2_leafn_check(dp
, bp
);
427 * Look up the hash value in the leaf entries.
429 index
= xfs_dir2_leaf_search_hash(args
, bp
);
431 * Do we have a buffer coming in?
433 if (state
->extravalid
) {
434 /* If so, it's a free block buffer, get the block number. */
435 curbp
= state
->extrablk
.bp
;
436 curfdb
= state
->extrablk
.blkno
;
438 ASSERT(be32_to_cpu(free
->hdr
.magic
) == XFS_DIR2_FREE_MAGIC
);
440 length
= xfs_dir2_data_entsize(args
->namelen
);
442 * Loop over leaf entries with the right hash value.
444 for (lep
= &leaf
->ents
[index
]; index
< be16_to_cpu(leaf
->hdr
.count
) &&
445 be32_to_cpu(lep
->hashval
) == args
->hashval
;
448 * Skip stale leaf entries.
450 if (be32_to_cpu(lep
->address
) == XFS_DIR2_NULL_DATAPTR
)
453 * Pull the data block number from the entry.
455 newdb
= xfs_dir2_dataptr_to_db(mp
, be32_to_cpu(lep
->address
));
457 * For addname, we're looking for a place to put the new entry.
458 * We want to use a data block with an entry of equal
459 * hash value to ours if there is one with room.
461 * If this block isn't the data block we already have
462 * in hand, take a look at it.
464 if (newdb
!= curdb
) {
467 * Convert the data block to the free block
468 * holding its freespace information.
470 newfdb
= xfs_dir2_db_to_fdb(mp
, newdb
);
472 * If it's not the one we have in hand, read it in.
474 if (newfdb
!= curfdb
) {
476 * If we had one before, drop it.
479 xfs_da_brelse(tp
, curbp
);
481 * Read the free block.
483 error
= xfs_da_read_buf(tp
, dp
,
484 xfs_dir2_db_to_da(mp
, newfdb
),
485 -1, &curbp
, XFS_DATA_FORK
);
489 ASSERT(be32_to_cpu(free
->hdr
.magic
) ==
490 XFS_DIR2_FREE_MAGIC
);
491 ASSERT((be32_to_cpu(free
->hdr
.firstdb
) %
492 XFS_DIR2_MAX_FREE_BESTS(mp
)) == 0);
493 ASSERT(be32_to_cpu(free
->hdr
.firstdb
) <= curdb
);
494 ASSERT(curdb
< be32_to_cpu(free
->hdr
.firstdb
) +
495 be32_to_cpu(free
->hdr
.nvalid
));
498 * Get the index for our entry.
500 fi
= xfs_dir2_db_to_fdindex(mp
, curdb
);
502 * If it has room, return it.
504 if (unlikely(be16_to_cpu(free
->bests
[fi
]) == NULLDATAOFF
)) {
505 XFS_ERROR_REPORT("xfs_dir2_leafn_lookup_int",
506 XFS_ERRLEVEL_LOW
, mp
);
507 if (curfdb
!= newfdb
)
508 xfs_da_brelse(tp
, curbp
);
509 return XFS_ERROR(EFSCORRUPTED
);
512 if (be16_to_cpu(free
->bests
[fi
]) >= length
)
516 /* Didn't find any space */
519 ASSERT(args
->op_flags
& XFS_DA_OP_OKNOENT
);
521 /* Giving back a free block. */
522 state
->extravalid
= 1;
523 state
->extrablk
.bp
= curbp
;
524 state
->extrablk
.index
= fi
;
525 state
->extrablk
.blkno
= curfdb
;
526 state
->extrablk
.magic
= XFS_DIR2_FREE_MAGIC
;
528 state
->extravalid
= 0;
531 * Return the index, that will be the insertion point.
534 return XFS_ERROR(ENOENT
);
538 * Look up a leaf entry in a node-format leaf block.
539 * The extrablk in state a data block.
542 xfs_dir2_leafn_lookup_for_entry(
543 xfs_dabuf_t
*bp
, /* leaf buffer */
544 xfs_da_args_t
*args
, /* operation arguments */
545 int *indexp
, /* out: leaf entry index */
546 xfs_da_state_t
*state
) /* state to fill in */
548 xfs_dabuf_t
*curbp
= NULL
; /* current data/free buffer */
549 xfs_dir2_db_t curdb
= -1; /* current data block number */
550 xfs_dir2_data_entry_t
*dep
; /* data block entry */
551 xfs_inode_t
*dp
; /* incore directory inode */
552 int error
; /* error return value */
553 int index
; /* leaf entry index */
554 xfs_dir2_leaf_t
*leaf
; /* leaf structure */
555 xfs_dir2_leaf_entry_t
*lep
; /* leaf entry */
556 xfs_mount_t
*mp
; /* filesystem mount point */
557 xfs_dir2_db_t newdb
; /* new data block number */
558 xfs_trans_t
*tp
; /* transaction pointer */
559 enum xfs_dacmp cmp
; /* comparison result */
565 ASSERT(be16_to_cpu(leaf
->hdr
.info
.magic
) == XFS_DIR2_LEAFN_MAGIC
);
567 ASSERT(be16_to_cpu(leaf
->hdr
.count
) > 0);
569 xfs_dir2_leafn_check(dp
, bp
);
571 * Look up the hash value in the leaf entries.
573 index
= xfs_dir2_leaf_search_hash(args
, bp
);
575 * Do we have a buffer coming in?
577 if (state
->extravalid
) {
578 curbp
= state
->extrablk
.bp
;
579 curdb
= state
->extrablk
.blkno
;
582 * Loop over leaf entries with the right hash value.
584 for (lep
= &leaf
->ents
[index
]; index
< be16_to_cpu(leaf
->hdr
.count
) &&
585 be32_to_cpu(lep
->hashval
) == args
->hashval
;
588 * Skip stale leaf entries.
590 if (be32_to_cpu(lep
->address
) == XFS_DIR2_NULL_DATAPTR
)
593 * Pull the data block number from the entry.
595 newdb
= xfs_dir2_dataptr_to_db(mp
, be32_to_cpu(lep
->address
));
597 * Not adding a new entry, so we really want to find
598 * the name given to us.
600 * If it's a different data block, go get it.
602 if (newdb
!= curdb
) {
604 * If we had a block before that we aren't saving
605 * for a CI name, drop it
607 if (curbp
&& (args
->cmpresult
== XFS_CMP_DIFFERENT
||
608 curdb
!= state
->extrablk
.blkno
))
609 xfs_da_brelse(tp
, curbp
);
611 * If needing the block that is saved with a CI match,
612 * use it otherwise read in the new data block.
614 if (args
->cmpresult
!= XFS_CMP_DIFFERENT
&&
615 newdb
== state
->extrablk
.blkno
) {
616 ASSERT(state
->extravalid
);
617 curbp
= state
->extrablk
.bp
;
619 error
= xfs_da_read_buf(tp
, dp
,
620 xfs_dir2_db_to_da(mp
, newdb
),
621 -1, &curbp
, XFS_DATA_FORK
);
625 xfs_dir2_data_check(dp
, curbp
);
629 * Point to the data entry.
631 dep
= (xfs_dir2_data_entry_t
*)((char *)curbp
->data
+
632 xfs_dir2_dataptr_to_off(mp
, be32_to_cpu(lep
->address
)));
634 * Compare the entry and if it's an exact match, return
635 * EEXIST immediately. If it's the first case-insensitive
636 * match, store the block & inode number and continue looking.
638 cmp
= mp
->m_dirnameops
->compname(args
, dep
->name
, dep
->namelen
);
639 if (cmp
!= XFS_CMP_DIFFERENT
&& cmp
!= args
->cmpresult
) {
640 /* If there is a CI match block, drop it */
641 if (args
->cmpresult
!= XFS_CMP_DIFFERENT
&&
642 curdb
!= state
->extrablk
.blkno
)
643 xfs_da_brelse(tp
, state
->extrablk
.bp
);
644 args
->cmpresult
= cmp
;
645 args
->inumber
= be64_to_cpu(dep
->inumber
);
647 state
->extravalid
= 1;
648 state
->extrablk
.bp
= curbp
;
649 state
->extrablk
.blkno
= curdb
;
650 state
->extrablk
.index
= (int)((char *)dep
-
651 (char *)curbp
->data
);
652 state
->extrablk
.magic
= XFS_DIR2_DATA_MAGIC
;
653 if (cmp
== XFS_CMP_EXACT
)
654 return XFS_ERROR(EEXIST
);
657 ASSERT(index
== be16_to_cpu(leaf
->hdr
.count
) ||
658 (args
->op_flags
& XFS_DA_OP_OKNOENT
));
660 if (args
->cmpresult
== XFS_CMP_DIFFERENT
) {
661 /* Giving back last used data block. */
662 state
->extravalid
= 1;
663 state
->extrablk
.bp
= curbp
;
664 state
->extrablk
.index
= -1;
665 state
->extrablk
.blkno
= curdb
;
666 state
->extrablk
.magic
= XFS_DIR2_DATA_MAGIC
;
668 /* If the curbp is not the CI match block, drop it */
669 if (state
->extrablk
.bp
!= curbp
)
670 xfs_da_brelse(tp
, curbp
);
673 state
->extravalid
= 0;
676 return XFS_ERROR(ENOENT
);
680 * Look up a leaf entry in a node-format leaf block.
681 * If this is an addname then the extrablk in state is a freespace block,
682 * otherwise it's a data block.
685 xfs_dir2_leafn_lookup_int(
686 xfs_dabuf_t
*bp
, /* leaf buffer */
687 xfs_da_args_t
*args
, /* operation arguments */
688 int *indexp
, /* out: leaf entry index */
689 xfs_da_state_t
*state
) /* state to fill in */
691 if (args
->op_flags
& XFS_DA_OP_ADDNAME
)
692 return xfs_dir2_leafn_lookup_for_addname(bp
, args
, indexp
,
694 return xfs_dir2_leafn_lookup_for_entry(bp
, args
, indexp
, state
);
698 * Move count leaf entries from source to destination leaf.
699 * Log entries and headers. Stale entries are preserved.
702 xfs_dir2_leafn_moveents(
703 xfs_da_args_t
*args
, /* operation arguments */
704 xfs_dabuf_t
*bp_s
, /* source leaf buffer */
705 int start_s
, /* source leaf index */
706 xfs_dabuf_t
*bp_d
, /* destination leaf buffer */
707 int start_d
, /* destination leaf index */
708 int count
) /* count of leaves to copy */
710 xfs_dir2_leaf_t
*leaf_d
; /* destination leaf structure */
711 xfs_dir2_leaf_t
*leaf_s
; /* source leaf structure */
712 int stale
; /* count stale leaves copied */
713 xfs_trans_t
*tp
; /* transaction pointer */
715 trace_xfs_dir2_leafn_moveents(args
, start_s
, start_d
, count
);
718 * Silently return if nothing to do.
727 * If the destination index is not the end of the current
728 * destination leaf entries, open up a hole in the destination
729 * to hold the new entries.
731 if (start_d
< be16_to_cpu(leaf_d
->hdr
.count
)) {
732 memmove(&leaf_d
->ents
[start_d
+ count
], &leaf_d
->ents
[start_d
],
733 (be16_to_cpu(leaf_d
->hdr
.count
) - start_d
) *
734 sizeof(xfs_dir2_leaf_entry_t
));
735 xfs_dir2_leaf_log_ents(tp
, bp_d
, start_d
+ count
,
736 count
+ be16_to_cpu(leaf_d
->hdr
.count
) - 1);
739 * If the source has stale leaves, count the ones in the copy range
740 * so we can update the header correctly.
742 if (leaf_s
->hdr
.stale
) {
743 int i
; /* temp leaf index */
745 for (i
= start_s
, stale
= 0; i
< start_s
+ count
; i
++) {
746 if (be32_to_cpu(leaf_s
->ents
[i
].address
) == XFS_DIR2_NULL_DATAPTR
)
752 * Copy the leaf entries from source to destination.
754 memcpy(&leaf_d
->ents
[start_d
], &leaf_s
->ents
[start_s
],
755 count
* sizeof(xfs_dir2_leaf_entry_t
));
756 xfs_dir2_leaf_log_ents(tp
, bp_d
, start_d
, start_d
+ count
- 1);
758 * If there are source entries after the ones we copied,
759 * delete the ones we copied by sliding the next ones down.
761 if (start_s
+ count
< be16_to_cpu(leaf_s
->hdr
.count
)) {
762 memmove(&leaf_s
->ents
[start_s
], &leaf_s
->ents
[start_s
+ count
],
763 count
* sizeof(xfs_dir2_leaf_entry_t
));
764 xfs_dir2_leaf_log_ents(tp
, bp_s
, start_s
, start_s
+ count
- 1);
767 * Update the headers and log them.
769 be16_add_cpu(&leaf_s
->hdr
.count
, -(count
));
770 be16_add_cpu(&leaf_s
->hdr
.stale
, -(stale
));
771 be16_add_cpu(&leaf_d
->hdr
.count
, count
);
772 be16_add_cpu(&leaf_d
->hdr
.stale
, stale
);
773 xfs_dir2_leaf_log_header(tp
, bp_s
);
774 xfs_dir2_leaf_log_header(tp
, bp_d
);
775 xfs_dir2_leafn_check(args
->dp
, bp_s
);
776 xfs_dir2_leafn_check(args
->dp
, bp_d
);
780 * Determine the sort order of two leaf blocks.
781 * Returns 1 if both are valid and leaf2 should be before leaf1, else 0.
784 xfs_dir2_leafn_order(
785 xfs_dabuf_t
*leaf1_bp
, /* leaf1 buffer */
786 xfs_dabuf_t
*leaf2_bp
) /* leaf2 buffer */
788 xfs_dir2_leaf_t
*leaf1
; /* leaf1 structure */
789 xfs_dir2_leaf_t
*leaf2
; /* leaf2 structure */
791 leaf1
= leaf1_bp
->data
;
792 leaf2
= leaf2_bp
->data
;
793 ASSERT(be16_to_cpu(leaf1
->hdr
.info
.magic
) == XFS_DIR2_LEAFN_MAGIC
);
794 ASSERT(be16_to_cpu(leaf2
->hdr
.info
.magic
) == XFS_DIR2_LEAFN_MAGIC
);
795 if (be16_to_cpu(leaf1
->hdr
.count
) > 0 &&
796 be16_to_cpu(leaf2
->hdr
.count
) > 0 &&
797 (be32_to_cpu(leaf2
->ents
[0].hashval
) < be32_to_cpu(leaf1
->ents
[0].hashval
) ||
798 be32_to_cpu(leaf2
->ents
[be16_to_cpu(leaf2
->hdr
.count
) - 1].hashval
) <
799 be32_to_cpu(leaf1
->ents
[be16_to_cpu(leaf1
->hdr
.count
) - 1].hashval
)))
805 * Rebalance leaf entries between two leaf blocks.
806 * This is actually only called when the second block is new,
807 * though the code deals with the general case.
808 * A new entry will be inserted in one of the blocks, and that
809 * entry is taken into account when balancing.
812 xfs_dir2_leafn_rebalance(
813 xfs_da_state_t
*state
, /* btree cursor */
814 xfs_da_state_blk_t
*blk1
, /* first btree block */
815 xfs_da_state_blk_t
*blk2
) /* second btree block */
817 xfs_da_args_t
*args
; /* operation arguments */
818 int count
; /* count (& direction) leaves */
819 int isleft
; /* new goes in left leaf */
820 xfs_dir2_leaf_t
*leaf1
; /* first leaf structure */
821 xfs_dir2_leaf_t
*leaf2
; /* second leaf structure */
822 int mid
; /* midpoint leaf index */
824 int oldstale
; /* old count of stale leaves */
826 int oldsum
; /* old total leaf count */
827 int swap
; /* swapped leaf blocks */
831 * If the block order is wrong, swap the arguments.
833 if ((swap
= xfs_dir2_leafn_order(blk1
->bp
, blk2
->bp
))) {
834 xfs_da_state_blk_t
*tmp
; /* temp for block swap */
840 leaf1
= blk1
->bp
->data
;
841 leaf2
= blk2
->bp
->data
;
842 oldsum
= be16_to_cpu(leaf1
->hdr
.count
) + be16_to_cpu(leaf2
->hdr
.count
);
844 oldstale
= be16_to_cpu(leaf1
->hdr
.stale
) + be16_to_cpu(leaf2
->hdr
.stale
);
848 * If the old leaf count was odd then the new one will be even,
849 * so we need to divide the new count evenly.
852 xfs_dahash_t midhash
; /* middle entry hash value */
854 if (mid
>= be16_to_cpu(leaf1
->hdr
.count
))
855 midhash
= be32_to_cpu(leaf2
->ents
[mid
- be16_to_cpu(leaf1
->hdr
.count
)].hashval
);
857 midhash
= be32_to_cpu(leaf1
->ents
[mid
].hashval
);
858 isleft
= args
->hashval
<= midhash
;
861 * If the old count is even then the new count is odd, so there's
862 * no preferred side for the new entry.
868 * Calculate moved entry count. Positive means left-to-right,
869 * negative means right-to-left. Then move the entries.
871 count
= be16_to_cpu(leaf1
->hdr
.count
) - mid
+ (isleft
== 0);
873 xfs_dir2_leafn_moveents(args
, blk1
->bp
,
874 be16_to_cpu(leaf1
->hdr
.count
) - count
, blk2
->bp
, 0, count
);
876 xfs_dir2_leafn_moveents(args
, blk2
->bp
, 0, blk1
->bp
,
877 be16_to_cpu(leaf1
->hdr
.count
), count
);
878 ASSERT(be16_to_cpu(leaf1
->hdr
.count
) + be16_to_cpu(leaf2
->hdr
.count
) == oldsum
);
879 ASSERT(be16_to_cpu(leaf1
->hdr
.stale
) + be16_to_cpu(leaf2
->hdr
.stale
) == oldstale
);
881 * Mark whether we're inserting into the old or new leaf.
883 if (be16_to_cpu(leaf1
->hdr
.count
) < be16_to_cpu(leaf2
->hdr
.count
))
884 state
->inleaf
= swap
;
885 else if (be16_to_cpu(leaf1
->hdr
.count
) > be16_to_cpu(leaf2
->hdr
.count
))
886 state
->inleaf
= !swap
;
889 swap
^ (blk1
->index
<= be16_to_cpu(leaf1
->hdr
.count
));
891 * Adjust the expected index for insertion.
894 blk2
->index
= blk1
->index
- be16_to_cpu(leaf1
->hdr
.count
);
897 * Finally sanity check just to make sure we are not returning a
900 if(blk2
->index
< 0) {
904 "xfs_dir2_leafn_rebalance: picked the wrong leaf? reverting original leaf: "
911 * Remove an entry from a node directory.
912 * This removes the leaf entry and the data entry,
913 * and updates the free block if necessary.
915 static int /* error */
916 xfs_dir2_leafn_remove(
917 xfs_da_args_t
*args
, /* operation arguments */
918 xfs_dabuf_t
*bp
, /* leaf buffer */
919 int index
, /* leaf entry index */
920 xfs_da_state_blk_t
*dblk
, /* data block */
921 int *rval
) /* resulting block needs join */
923 xfs_dir2_data_t
*data
; /* data block structure */
924 xfs_dir2_db_t db
; /* data block number */
925 xfs_dabuf_t
*dbp
; /* data block buffer */
926 xfs_dir2_data_entry_t
*dep
; /* data block entry */
927 xfs_inode_t
*dp
; /* incore directory inode */
928 xfs_dir2_leaf_t
*leaf
; /* leaf structure */
929 xfs_dir2_leaf_entry_t
*lep
; /* leaf entry */
930 int longest
; /* longest data free entry */
931 int off
; /* data block entry offset */
932 xfs_mount_t
*mp
; /* filesystem mount point */
933 int needlog
; /* need to log data header */
934 int needscan
; /* need to rescan data frees */
935 xfs_trans_t
*tp
; /* transaction pointer */
937 trace_xfs_dir2_leafn_remove(args
, index
);
943 ASSERT(be16_to_cpu(leaf
->hdr
.info
.magic
) == XFS_DIR2_LEAFN_MAGIC
);
945 * Point to the entry we're removing.
947 lep
= &leaf
->ents
[index
];
949 * Extract the data block and offset from the entry.
951 db
= xfs_dir2_dataptr_to_db(mp
, be32_to_cpu(lep
->address
));
952 ASSERT(dblk
->blkno
== db
);
953 off
= xfs_dir2_dataptr_to_off(mp
, be32_to_cpu(lep
->address
));
954 ASSERT(dblk
->index
== off
);
956 * Kill the leaf entry by marking it stale.
957 * Log the leaf block changes.
959 be16_add_cpu(&leaf
->hdr
.stale
, 1);
960 xfs_dir2_leaf_log_header(tp
, bp
);
961 lep
->address
= cpu_to_be32(XFS_DIR2_NULL_DATAPTR
);
962 xfs_dir2_leaf_log_ents(tp
, bp
, index
, index
);
964 * Make the data entry free. Keep track of the longest freespace
965 * in the data block in case it changes.
969 dep
= (xfs_dir2_data_entry_t
*)((char *)data
+ off
);
970 longest
= be16_to_cpu(data
->hdr
.bestfree
[0].length
);
971 needlog
= needscan
= 0;
972 xfs_dir2_data_make_free(tp
, dbp
, off
,
973 xfs_dir2_data_entsize(dep
->namelen
), &needlog
, &needscan
);
975 * Rescan the data block freespaces for bestfree.
976 * Log the data block header if needed.
979 xfs_dir2_data_freescan(mp
, data
, &needlog
);
981 xfs_dir2_data_log_header(tp
, dbp
);
982 xfs_dir2_data_check(dp
, dbp
);
984 * If the longest data block freespace changes, need to update
985 * the corresponding freeblock entry.
987 if (longest
< be16_to_cpu(data
->hdr
.bestfree
[0].length
)) {
988 int error
; /* error return value */
989 xfs_dabuf_t
*fbp
; /* freeblock buffer */
990 xfs_dir2_db_t fdb
; /* freeblock block number */
991 int findex
; /* index in freeblock entries */
992 xfs_dir2_free_t
*free
; /* freeblock structure */
993 int logfree
; /* need to log free entry */
996 * Convert the data block number to a free block,
997 * read in the free block.
999 fdb
= xfs_dir2_db_to_fdb(mp
, db
);
1000 if ((error
= xfs_da_read_buf(tp
, dp
, xfs_dir2_db_to_da(mp
, fdb
),
1001 -1, &fbp
, XFS_DATA_FORK
))) {
1005 ASSERT(be32_to_cpu(free
->hdr
.magic
) == XFS_DIR2_FREE_MAGIC
);
1006 ASSERT(be32_to_cpu(free
->hdr
.firstdb
) ==
1007 XFS_DIR2_MAX_FREE_BESTS(mp
) *
1008 (fdb
- XFS_DIR2_FREE_FIRSTDB(mp
)));
1010 * Calculate which entry we need to fix.
1012 findex
= xfs_dir2_db_to_fdindex(mp
, db
);
1013 longest
= be16_to_cpu(data
->hdr
.bestfree
[0].length
);
1015 * If the data block is now empty we can get rid of it
1018 if (longest
== mp
->m_dirblksize
- (uint
)sizeof(data
->hdr
)) {
1020 * Try to punch out the data block.
1022 error
= xfs_dir2_shrink_inode(args
, db
, dbp
);
1028 * We can get ENOSPC if there's no space reservation.
1029 * In this case just drop the buffer and some one else
1030 * will eventually get rid of the empty block.
1032 else if (error
== ENOSPC
&& args
->total
== 0)
1033 xfs_da_buf_done(dbp
);
1038 * If we got rid of the data block, we can eliminate that entry
1039 * in the free block.
1043 * One less used entry in the free table.
1045 be32_add_cpu(&free
->hdr
.nused
, -1);
1046 xfs_dir2_free_log_header(tp
, fbp
);
1048 * If this was the last entry in the table, we can
1049 * trim the table size back. There might be other
1050 * entries at the end referring to non-existent
1051 * data blocks, get those too.
1053 if (findex
== be32_to_cpu(free
->hdr
.nvalid
) - 1) {
1054 int i
; /* free entry index */
1056 for (i
= findex
- 1;
1057 i
>= 0 && be16_to_cpu(free
->bests
[i
]) == NULLDATAOFF
;
1060 free
->hdr
.nvalid
= cpu_to_be32(i
+ 1);
1064 * Not the last entry, just punch it out.
1067 free
->bests
[findex
] = cpu_to_be16(NULLDATAOFF
);
1071 * If there are no useful entries left in the block,
1072 * get rid of the block if we can.
1074 if (!free
->hdr
.nused
) {
1075 error
= xfs_dir2_shrink_inode(args
, fdb
, fbp
);
1079 } else if (error
!= ENOSPC
|| args
->total
!= 0)
1082 * It's possible to get ENOSPC if there is no
1083 * space reservation. In this case some one
1084 * else will eventually get rid of this block.
1089 * Data block is not empty, just set the free entry to
1093 free
->bests
[findex
] = cpu_to_be16(longest
);
1097 * Log the free entry that changed, unless we got rid of it.
1100 xfs_dir2_free_log_bests(tp
, fbp
, findex
, findex
);
1102 * Drop the buffer if we still have it.
1105 xfs_da_buf_done(fbp
);
1107 xfs_dir2_leafn_check(dp
, bp
);
1109 * Return indication of whether this leaf block is empty enough
1110 * to justify trying to join it with a neighbor.
1113 ((uint
)sizeof(leaf
->hdr
) +
1114 (uint
)sizeof(leaf
->ents
[0]) *
1115 (be16_to_cpu(leaf
->hdr
.count
) - be16_to_cpu(leaf
->hdr
.stale
))) <
1121 * Split the leaf entries in the old block into old and new blocks.
1124 xfs_dir2_leafn_split(
1125 xfs_da_state_t
*state
, /* btree cursor */
1126 xfs_da_state_blk_t
*oldblk
, /* original block */
1127 xfs_da_state_blk_t
*newblk
) /* newly created block */
1129 xfs_da_args_t
*args
; /* operation arguments */
1130 xfs_dablk_t blkno
; /* new leaf block number */
1131 int error
; /* error return value */
1132 xfs_mount_t
*mp
; /* filesystem mount point */
1135 * Allocate space for a new leaf node.
1138 mp
= args
->dp
->i_mount
;
1139 ASSERT(args
!= NULL
);
1140 ASSERT(oldblk
->magic
== XFS_DIR2_LEAFN_MAGIC
);
1141 error
= xfs_da_grow_inode(args
, &blkno
);
1146 * Initialize the new leaf block.
1148 error
= xfs_dir2_leaf_init(args
, xfs_dir2_da_to_db(mp
, blkno
),
1149 &newblk
->bp
, XFS_DIR2_LEAFN_MAGIC
);
1153 newblk
->blkno
= blkno
;
1154 newblk
->magic
= XFS_DIR2_LEAFN_MAGIC
;
1156 * Rebalance the entries across the two leaves, link the new
1157 * block into the leaves.
1159 xfs_dir2_leafn_rebalance(state
, oldblk
, newblk
);
1160 error
= xfs_da_blk_link(state
, oldblk
, newblk
);
1165 * Insert the new entry in the correct block.
1168 error
= xfs_dir2_leafn_add(oldblk
->bp
, args
, oldblk
->index
);
1170 error
= xfs_dir2_leafn_add(newblk
->bp
, args
, newblk
->index
);
1172 * Update last hashval in each block since we added the name.
1174 oldblk
->hashval
= xfs_dir2_leafn_lasthash(oldblk
->bp
, NULL
);
1175 newblk
->hashval
= xfs_dir2_leafn_lasthash(newblk
->bp
, NULL
);
1176 xfs_dir2_leafn_check(args
->dp
, oldblk
->bp
);
1177 xfs_dir2_leafn_check(args
->dp
, newblk
->bp
);
1182 * Check a leaf block and its neighbors to see if the block should be
1183 * collapsed into one or the other neighbor. Always keep the block
1184 * with the smaller block number.
1185 * If the current block is over 50% full, don't try to join it, return 0.
1186 * If the block is empty, fill in the state structure and return 2.
1187 * If it can be collapsed, fill in the state structure and return 1.
1188 * If nothing can be done, return 0.
1191 xfs_dir2_leafn_toosmall(
1192 xfs_da_state_t
*state
, /* btree cursor */
1193 int *action
) /* resulting action to take */
1195 xfs_da_state_blk_t
*blk
; /* leaf block */
1196 xfs_dablk_t blkno
; /* leaf block number */
1197 xfs_dabuf_t
*bp
; /* leaf buffer */
1198 int bytes
; /* bytes in use */
1199 int count
; /* leaf live entry count */
1200 int error
; /* error return value */
1201 int forward
; /* sibling block direction */
1202 int i
; /* sibling counter */
1203 xfs_da_blkinfo_t
*info
; /* leaf block header */
1204 xfs_dir2_leaf_t
*leaf
; /* leaf structure */
1205 int rval
; /* result from path_shift */
1208 * Check for the degenerate case of the block being over 50% full.
1209 * If so, it's not worth even looking to see if we might be able
1210 * to coalesce with a sibling.
1212 blk
= &state
->path
.blk
[state
->path
.active
- 1];
1213 info
= blk
->bp
->data
;
1214 ASSERT(be16_to_cpu(info
->magic
) == XFS_DIR2_LEAFN_MAGIC
);
1215 leaf
= (xfs_dir2_leaf_t
*)info
;
1216 count
= be16_to_cpu(leaf
->hdr
.count
) - be16_to_cpu(leaf
->hdr
.stale
);
1217 bytes
= (uint
)sizeof(leaf
->hdr
) + count
* (uint
)sizeof(leaf
->ents
[0]);
1218 if (bytes
> (state
->blocksize
>> 1)) {
1220 * Blk over 50%, don't try to join.
1226 * Check for the degenerate case of the block being empty.
1227 * If the block is empty, we'll simply delete it, no need to
1228 * coalesce it with a sibling block. We choose (arbitrarily)
1229 * to merge with the forward block unless it is NULL.
1233 * Make altpath point to the block we want to keep and
1234 * path point to the block we want to drop (this one).
1236 forward
= (info
->forw
!= 0);
1237 memcpy(&state
->altpath
, &state
->path
, sizeof(state
->path
));
1238 error
= xfs_da_path_shift(state
, &state
->altpath
, forward
, 0,
1242 *action
= rval
? 2 : 0;
1246 * Examine each sibling block to see if we can coalesce with
1247 * at least 25% free space to spare. We need to figure out
1248 * whether to merge with the forward or the backward block.
1249 * We prefer coalescing with the lower numbered sibling so as
1250 * to shrink a directory over time.
1252 forward
= be32_to_cpu(info
->forw
) < be32_to_cpu(info
->back
);
1253 for (i
= 0, bp
= NULL
; i
< 2; forward
= !forward
, i
++) {
1254 blkno
= forward
? be32_to_cpu(info
->forw
) : be32_to_cpu(info
->back
);
1258 * Read the sibling leaf block.
1261 xfs_da_read_buf(state
->args
->trans
, state
->args
->dp
, blkno
,
1262 -1, &bp
, XFS_DATA_FORK
))) {
1267 * Count bytes in the two blocks combined.
1269 leaf
= (xfs_dir2_leaf_t
*)info
;
1270 count
= be16_to_cpu(leaf
->hdr
.count
) - be16_to_cpu(leaf
->hdr
.stale
);
1271 bytes
= state
->blocksize
- (state
->blocksize
>> 2);
1273 ASSERT(be16_to_cpu(leaf
->hdr
.info
.magic
) == XFS_DIR2_LEAFN_MAGIC
);
1274 count
+= be16_to_cpu(leaf
->hdr
.count
) - be16_to_cpu(leaf
->hdr
.stale
);
1275 bytes
-= count
* (uint
)sizeof(leaf
->ents
[0]);
1277 * Fits with at least 25% to spare.
1281 xfs_da_brelse(state
->args
->trans
, bp
);
1284 * Didn't like either block, give up.
1291 * Done with the sibling leaf block here, drop the dabuf
1292 * so path_shift can get it.
1294 xfs_da_buf_done(bp
);
1296 * Make altpath point to the block we want to keep (the lower
1297 * numbered block) and path point to the block we want to drop.
1299 memcpy(&state
->altpath
, &state
->path
, sizeof(state
->path
));
1300 if (blkno
< blk
->blkno
)
1301 error
= xfs_da_path_shift(state
, &state
->altpath
, forward
, 0,
1304 error
= xfs_da_path_shift(state
, &state
->path
, forward
, 0,
1309 *action
= rval
? 0 : 1;
1314 * Move all the leaf entries from drop_blk to save_blk.
1315 * This is done as part of a join operation.
1318 xfs_dir2_leafn_unbalance(
1319 xfs_da_state_t
*state
, /* cursor */
1320 xfs_da_state_blk_t
*drop_blk
, /* dead block */
1321 xfs_da_state_blk_t
*save_blk
) /* surviving block */
1323 xfs_da_args_t
*args
; /* operation arguments */
1324 xfs_dir2_leaf_t
*drop_leaf
; /* dead leaf structure */
1325 xfs_dir2_leaf_t
*save_leaf
; /* surviving leaf structure */
1328 ASSERT(drop_blk
->magic
== XFS_DIR2_LEAFN_MAGIC
);
1329 ASSERT(save_blk
->magic
== XFS_DIR2_LEAFN_MAGIC
);
1330 drop_leaf
= drop_blk
->bp
->data
;
1331 save_leaf
= save_blk
->bp
->data
;
1332 ASSERT(be16_to_cpu(drop_leaf
->hdr
.info
.magic
) == XFS_DIR2_LEAFN_MAGIC
);
1333 ASSERT(be16_to_cpu(save_leaf
->hdr
.info
.magic
) == XFS_DIR2_LEAFN_MAGIC
);
1335 * If there are any stale leaf entries, take this opportunity
1338 if (drop_leaf
->hdr
.stale
)
1339 xfs_dir2_leaf_compact(args
, drop_blk
->bp
);
1340 if (save_leaf
->hdr
.stale
)
1341 xfs_dir2_leaf_compact(args
, save_blk
->bp
);
1343 * Move the entries from drop to the appropriate end of save.
1345 drop_blk
->hashval
= be32_to_cpu(drop_leaf
->ents
[be16_to_cpu(drop_leaf
->hdr
.count
) - 1].hashval
);
1346 if (xfs_dir2_leafn_order(save_blk
->bp
, drop_blk
->bp
))
1347 xfs_dir2_leafn_moveents(args
, drop_blk
->bp
, 0, save_blk
->bp
, 0,
1348 be16_to_cpu(drop_leaf
->hdr
.count
));
1350 xfs_dir2_leafn_moveents(args
, drop_blk
->bp
, 0, save_blk
->bp
,
1351 be16_to_cpu(save_leaf
->hdr
.count
), be16_to_cpu(drop_leaf
->hdr
.count
));
1352 save_blk
->hashval
= be32_to_cpu(save_leaf
->ents
[be16_to_cpu(save_leaf
->hdr
.count
) - 1].hashval
);
1353 xfs_dir2_leafn_check(args
->dp
, save_blk
->bp
);
1357 * Top-level node form directory addname routine.
1360 xfs_dir2_node_addname(
1361 xfs_da_args_t
*args
) /* operation arguments */
1363 xfs_da_state_blk_t
*blk
; /* leaf block for insert */
1364 int error
; /* error return value */
1365 int rval
; /* sub-return value */
1366 xfs_da_state_t
*state
; /* btree cursor */
1368 trace_xfs_dir2_node_addname(args
);
1371 * Allocate and initialize the state (btree cursor).
1373 state
= xfs_da_state_alloc();
1375 state
->mp
= args
->dp
->i_mount
;
1376 state
->blocksize
= state
->mp
->m_dirblksize
;
1377 state
->node_ents
= state
->mp
->m_dir_node_ents
;
1379 * Look up the name. We're not supposed to find it, but
1380 * this gives us the insertion point.
1382 error
= xfs_da_node_lookup_int(state
, &rval
);
1385 if (rval
!= ENOENT
) {
1389 * Add the data entry to a data block.
1390 * Extravalid is set to a freeblock found by lookup.
1392 rval
= xfs_dir2_node_addname_int(args
,
1393 state
->extravalid
? &state
->extrablk
: NULL
);
1397 blk
= &state
->path
.blk
[state
->path
.active
- 1];
1398 ASSERT(blk
->magic
== XFS_DIR2_LEAFN_MAGIC
);
1400 * Add the new leaf entry.
1402 rval
= xfs_dir2_leafn_add(blk
->bp
, args
, blk
->index
);
1405 * It worked, fix the hash values up the btree.
1407 if (!(args
->op_flags
& XFS_DA_OP_JUSTCHECK
))
1408 xfs_da_fixhashpath(state
, &state
->path
);
1411 * It didn't work, we need to split the leaf block.
1413 if (args
->total
== 0) {
1414 ASSERT(rval
== ENOSPC
);
1418 * Split the leaf block and insert the new entry.
1420 rval
= xfs_da_split(state
);
1423 xfs_da_state_free(state
);
1428 * Add the data entry for a node-format directory name addition.
1429 * The leaf entry is added in xfs_dir2_leafn_add.
1430 * We may enter with a freespace block that the lookup found.
1432 static int /* error */
1433 xfs_dir2_node_addname_int(
1434 xfs_da_args_t
*args
, /* operation arguments */
1435 xfs_da_state_blk_t
*fblk
) /* optional freespace block */
1437 xfs_dir2_data_t
*data
; /* data block structure */
1438 xfs_dir2_db_t dbno
; /* data block number */
1439 xfs_dabuf_t
*dbp
; /* data block buffer */
1440 xfs_dir2_data_entry_t
*dep
; /* data entry pointer */
1441 xfs_inode_t
*dp
; /* incore directory inode */
1442 xfs_dir2_data_unused_t
*dup
; /* data unused entry pointer */
1443 int error
; /* error return value */
1444 xfs_dir2_db_t fbno
; /* freespace block number */
1445 xfs_dabuf_t
*fbp
; /* freespace buffer */
1446 int findex
; /* freespace entry index */
1447 xfs_dir2_free_t
*free
=NULL
; /* freespace block structure */
1448 xfs_dir2_db_t ifbno
; /* initial freespace block no */
1449 xfs_dir2_db_t lastfbno
=0; /* highest freespace block no */
1450 int length
; /* length of the new entry */
1451 int logfree
; /* need to log free entry */
1452 xfs_mount_t
*mp
; /* filesystem mount point */
1453 int needlog
; /* need to log data header */
1454 int needscan
; /* need to rescan data frees */
1455 __be16
*tagp
; /* data entry tag pointer */
1456 xfs_trans_t
*tp
; /* transaction pointer */
1461 length
= xfs_dir2_data_entsize(args
->namelen
);
1463 * If we came in with a freespace block that means that lookup
1464 * found an entry with our hash value. This is the freespace
1465 * block for that data entry.
1470 * Remember initial freespace block number.
1472 ifbno
= fblk
->blkno
;
1474 ASSERT(be32_to_cpu(free
->hdr
.magic
) == XFS_DIR2_FREE_MAGIC
);
1475 findex
= fblk
->index
;
1477 * This means the free entry showed that the data block had
1478 * space for our entry, so we remembered it.
1479 * Use that data block.
1482 ASSERT(findex
< be32_to_cpu(free
->hdr
.nvalid
));
1483 ASSERT(be16_to_cpu(free
->bests
[findex
]) != NULLDATAOFF
);
1484 ASSERT(be16_to_cpu(free
->bests
[findex
]) >= length
);
1485 dbno
= be32_to_cpu(free
->hdr
.firstdb
) + findex
;
1488 * The data block looked at didn't have enough room.
1489 * We'll start at the beginning of the freespace entries.
1497 * Didn't come in with a freespace block, so don't have a data block.
1505 * If we don't have a data block yet, we're going to scan the
1506 * freespace blocks looking for one. Figure out what the
1507 * highest freespace block number is.
1510 xfs_fileoff_t fo
; /* freespace block number */
1512 if ((error
= xfs_bmap_last_offset(tp
, dp
, &fo
, XFS_DATA_FORK
)))
1514 lastfbno
= xfs_dir2_da_to_db(mp
, (xfs_dablk_t
)fo
);
1518 * While we haven't identified a data block, search the freeblock
1519 * data for a good data block. If we find a null freeblock entry,
1520 * indicating a hole in the data blocks, remember that.
1522 while (dbno
== -1) {
1524 * If we don't have a freeblock in hand, get the next one.
1528 * Happens the first time through unless lookup gave
1529 * us a freespace block to start with.
1532 fbno
= XFS_DIR2_FREE_FIRSTDB(mp
);
1534 * If it's ifbno we already looked at it.
1539 * If it's off the end we're done.
1541 if (fbno
>= lastfbno
)
1544 * Read the block. There can be holes in the
1545 * freespace blocks, so this might not succeed.
1546 * This should be really rare, so there's no reason
1549 if ((error
= xfs_da_read_buf(tp
, dp
,
1550 xfs_dir2_db_to_da(mp
, fbno
), -2, &fbp
,
1554 if (unlikely(fbp
== NULL
)) {
1558 ASSERT(be32_to_cpu(free
->hdr
.magic
) == XFS_DIR2_FREE_MAGIC
);
1562 * Look at the current free entry. Is it good enough?
1564 if (be16_to_cpu(free
->bests
[findex
]) != NULLDATAOFF
&&
1565 be16_to_cpu(free
->bests
[findex
]) >= length
)
1566 dbno
= be32_to_cpu(free
->hdr
.firstdb
) + findex
;
1569 * Are we done with the freeblock?
1571 if (++findex
== be32_to_cpu(free
->hdr
.nvalid
)) {
1575 xfs_da_brelse(tp
, fbp
);
1577 if (fblk
&& fblk
->bp
)
1583 * If we don't have a data block, we need to allocate one and make
1584 * the freespace entries refer to it.
1586 if (unlikely(dbno
== -1)) {
1588 * Not allowed to allocate, return failure.
1590 if ((args
->op_flags
& XFS_DA_OP_JUSTCHECK
) ||
1593 * Drop the freespace buffer unless it came from our
1596 if ((fblk
== NULL
|| fblk
->bp
== NULL
) && fbp
!= NULL
)
1597 xfs_da_buf_done(fbp
);
1598 return XFS_ERROR(ENOSPC
);
1601 * Allocate and initialize the new data block.
1603 if (unlikely((error
= xfs_dir2_grow_inode(args
,
1604 XFS_DIR2_DATA_SPACE
,
1606 (error
= xfs_dir2_data_init(args
, dbno
, &dbp
)))) {
1608 * Drop the freespace buffer unless it came from our
1611 if ((fblk
== NULL
|| fblk
->bp
== NULL
) && fbp
!= NULL
)
1612 xfs_da_buf_done(fbp
);
1616 * If (somehow) we have a freespace block, get rid of it.
1619 xfs_da_brelse(tp
, fbp
);
1620 if (fblk
&& fblk
->bp
)
1624 * Get the freespace block corresponding to the data block
1625 * that was just allocated.
1627 fbno
= xfs_dir2_db_to_fdb(mp
, dbno
);
1628 if (unlikely(error
= xfs_da_read_buf(tp
, dp
,
1629 xfs_dir2_db_to_da(mp
, fbno
), -2, &fbp
,
1631 xfs_da_buf_done(dbp
);
1635 * If there wasn't a freespace block, the read will
1636 * return a NULL fbp. Allocate and initialize a new one.
1639 if ((error
= xfs_dir2_grow_inode(args
, XFS_DIR2_FREE_SPACE
,
1644 if (unlikely(xfs_dir2_db_to_fdb(mp
, dbno
) != fbno
)) {
1646 "xfs_dir2_node_addname_int: dir ino "
1647 "%llu needed freesp block %lld for\n"
1648 " data block %lld, got %lld\n"
1649 " ifbno %llu lastfbno %d\n",
1650 (unsigned long long)dp
->i_ino
,
1651 (long long)xfs_dir2_db_to_fdb(mp
, dbno
),
1652 (long long)dbno
, (long long)fbno
,
1653 (unsigned long long)ifbno
, lastfbno
);
1656 " fblk 0x%p blkno %llu "
1657 "index %d magic 0x%x\n",
1659 (unsigned long long)fblk
->blkno
,
1664 " ... fblk is NULL\n");
1666 XFS_ERROR_REPORT("xfs_dir2_node_addname_int",
1667 XFS_ERRLEVEL_LOW
, mp
);
1668 return XFS_ERROR(EFSCORRUPTED
);
1672 * Get a buffer for the new block.
1674 if ((error
= xfs_da_get_buf(tp
, dp
,
1675 xfs_dir2_db_to_da(mp
, fbno
),
1676 -1, &fbp
, XFS_DATA_FORK
))) {
1679 ASSERT(fbp
!= NULL
);
1682 * Initialize the new block to be empty, and remember
1683 * its first slot as our empty slot.
1686 free
->hdr
.magic
= cpu_to_be32(XFS_DIR2_FREE_MAGIC
);
1687 free
->hdr
.firstdb
= cpu_to_be32(
1688 (fbno
- XFS_DIR2_FREE_FIRSTDB(mp
)) *
1689 XFS_DIR2_MAX_FREE_BESTS(mp
));
1690 free
->hdr
.nvalid
= 0;
1691 free
->hdr
.nused
= 0;
1694 ASSERT(be32_to_cpu(free
->hdr
.magic
) == XFS_DIR2_FREE_MAGIC
);
1698 * Set the freespace block index from the data block number.
1700 findex
= xfs_dir2_db_to_fdindex(mp
, dbno
);
1702 * If it's after the end of the current entries in the
1703 * freespace block, extend that table.
1705 if (findex
>= be32_to_cpu(free
->hdr
.nvalid
)) {
1706 ASSERT(findex
< XFS_DIR2_MAX_FREE_BESTS(mp
));
1707 free
->hdr
.nvalid
= cpu_to_be32(findex
+ 1);
1709 * Tag new entry so nused will go up.
1711 free
->bests
[findex
] = cpu_to_be16(NULLDATAOFF
);
1714 * If this entry was for an empty data block
1715 * (this should always be true) then update the header.
1717 if (be16_to_cpu(free
->bests
[findex
]) == NULLDATAOFF
) {
1718 be32_add_cpu(&free
->hdr
.nused
, 1);
1719 xfs_dir2_free_log_header(tp
, fbp
);
1722 * Update the real value in the table.
1723 * We haven't allocated the data entry yet so this will
1727 free
->bests
[findex
] = data
->hdr
.bestfree
[0].length
;
1731 * We had a data block so we don't have to make a new one.
1735 * If just checking, we succeeded.
1737 if (args
->op_flags
& XFS_DA_OP_JUSTCHECK
) {
1738 if ((fblk
== NULL
|| fblk
->bp
== NULL
) && fbp
!= NULL
)
1739 xfs_da_buf_done(fbp
);
1743 * Read the data block in.
1746 error
= xfs_da_read_buf(tp
, dp
, xfs_dir2_db_to_da(mp
, dbno
),
1747 -1, &dbp
, XFS_DATA_FORK
))) {
1748 if ((fblk
== NULL
|| fblk
->bp
== NULL
) && fbp
!= NULL
)
1749 xfs_da_buf_done(fbp
);
1755 ASSERT(be16_to_cpu(data
->hdr
.bestfree
[0].length
) >= length
);
1757 * Point to the existing unused space.
1759 dup
= (xfs_dir2_data_unused_t
*)
1760 ((char *)data
+ be16_to_cpu(data
->hdr
.bestfree
[0].offset
));
1761 needscan
= needlog
= 0;
1763 * Mark the first part of the unused space, inuse for us.
1765 xfs_dir2_data_use_free(tp
, dbp
, dup
,
1766 (xfs_dir2_data_aoff_t
)((char *)dup
- (char *)data
), length
,
1767 &needlog
, &needscan
);
1769 * Fill in the new entry and log it.
1771 dep
= (xfs_dir2_data_entry_t
*)dup
;
1772 dep
->inumber
= cpu_to_be64(args
->inumber
);
1773 dep
->namelen
= args
->namelen
;
1774 memcpy(dep
->name
, args
->name
, dep
->namelen
);
1775 tagp
= xfs_dir2_data_entry_tag_p(dep
);
1776 *tagp
= cpu_to_be16((char *)dep
- (char *)data
);
1777 xfs_dir2_data_log_entry(tp
, dbp
, dep
);
1779 * Rescan the block for bestfree if needed.
1782 xfs_dir2_data_freescan(mp
, data
, &needlog
);
1784 * Log the data block header if needed.
1787 xfs_dir2_data_log_header(tp
, dbp
);
1789 * If the freespace entry is now wrong, update it.
1791 if (be16_to_cpu(free
->bests
[findex
]) != be16_to_cpu(data
->hdr
.bestfree
[0].length
)) {
1792 free
->bests
[findex
] = data
->hdr
.bestfree
[0].length
;
1796 * Log the freespace entry if needed.
1799 xfs_dir2_free_log_bests(tp
, fbp
, findex
, findex
);
1801 * If the caller didn't hand us the freespace block, drop it.
1803 if ((fblk
== NULL
|| fblk
->bp
== NULL
) && fbp
!= NULL
)
1804 xfs_da_buf_done(fbp
);
1806 * Return the data block and offset in args, then drop the data block.
1808 args
->blkno
= (xfs_dablk_t
)dbno
;
1809 args
->index
= be16_to_cpu(*tagp
);
1810 xfs_da_buf_done(dbp
);
1815 * Lookup an entry in a node-format directory.
1816 * All the real work happens in xfs_da_node_lookup_int.
1817 * The only real output is the inode number of the entry.
1820 xfs_dir2_node_lookup(
1821 xfs_da_args_t
*args
) /* operation arguments */
1823 int error
; /* error return value */
1824 int i
; /* btree level */
1825 int rval
; /* operation return value */
1826 xfs_da_state_t
*state
; /* btree cursor */
1828 trace_xfs_dir2_node_lookup(args
);
1831 * Allocate and initialize the btree cursor.
1833 state
= xfs_da_state_alloc();
1835 state
->mp
= args
->dp
->i_mount
;
1836 state
->blocksize
= state
->mp
->m_dirblksize
;
1837 state
->node_ents
= state
->mp
->m_dir_node_ents
;
1839 * Fill in the path to the entry in the cursor.
1841 error
= xfs_da_node_lookup_int(state
, &rval
);
1844 else if (rval
== ENOENT
&& args
->cmpresult
== XFS_CMP_CASE
) {
1845 /* If a CI match, dup the actual name and return EEXIST */
1846 xfs_dir2_data_entry_t
*dep
;
1848 dep
= (xfs_dir2_data_entry_t
*)((char *)state
->extrablk
.bp
->
1849 data
+ state
->extrablk
.index
);
1850 rval
= xfs_dir_cilookup_result(args
, dep
->name
, dep
->namelen
);
1853 * Release the btree blocks and leaf block.
1855 for (i
= 0; i
< state
->path
.active
; i
++) {
1856 xfs_da_brelse(args
->trans
, state
->path
.blk
[i
].bp
);
1857 state
->path
.blk
[i
].bp
= NULL
;
1860 * Release the data block if we have it.
1862 if (state
->extravalid
&& state
->extrablk
.bp
) {
1863 xfs_da_brelse(args
->trans
, state
->extrablk
.bp
);
1864 state
->extrablk
.bp
= NULL
;
1866 xfs_da_state_free(state
);
1871 * Remove an entry from a node-format directory.
1874 xfs_dir2_node_removename(
1875 xfs_da_args_t
*args
) /* operation arguments */
1877 xfs_da_state_blk_t
*blk
; /* leaf block */
1878 int error
; /* error return value */
1879 int rval
; /* operation return value */
1880 xfs_da_state_t
*state
; /* btree cursor */
1882 trace_xfs_dir2_node_removename(args
);
1885 * Allocate and initialize the btree cursor.
1887 state
= xfs_da_state_alloc();
1889 state
->mp
= args
->dp
->i_mount
;
1890 state
->blocksize
= state
->mp
->m_dirblksize
;
1891 state
->node_ents
= state
->mp
->m_dir_node_ents
;
1893 * Look up the entry we're deleting, set up the cursor.
1895 error
= xfs_da_node_lookup_int(state
, &rval
);
1899 * Didn't find it, upper layer screwed up.
1901 if (rval
!= EEXIST
) {
1902 xfs_da_state_free(state
);
1905 blk
= &state
->path
.blk
[state
->path
.active
- 1];
1906 ASSERT(blk
->magic
== XFS_DIR2_LEAFN_MAGIC
);
1907 ASSERT(state
->extravalid
);
1909 * Remove the leaf and data entries.
1910 * Extrablk refers to the data block.
1912 error
= xfs_dir2_leafn_remove(args
, blk
->bp
, blk
->index
,
1913 &state
->extrablk
, &rval
);
1917 * Fix the hash values up the btree.
1919 xfs_da_fixhashpath(state
, &state
->path
);
1921 * If we need to join leaf blocks, do it.
1923 if (rval
&& state
->path
.active
> 1)
1924 error
= xfs_da_join(state
);
1926 * If no errors so far, try conversion to leaf format.
1929 error
= xfs_dir2_node_to_leaf(state
);
1930 xfs_da_state_free(state
);
1935 * Replace an entry's inode number in a node-format directory.
1938 xfs_dir2_node_replace(
1939 xfs_da_args_t
*args
) /* operation arguments */
1941 xfs_da_state_blk_t
*blk
; /* leaf block */
1942 xfs_dir2_data_t
*data
; /* data block structure */
1943 xfs_dir2_data_entry_t
*dep
; /* data entry changed */
1944 int error
; /* error return value */
1945 int i
; /* btree level */
1946 xfs_ino_t inum
; /* new inode number */
1947 xfs_dir2_leaf_t
*leaf
; /* leaf structure */
1948 xfs_dir2_leaf_entry_t
*lep
; /* leaf entry being changed */
1949 int rval
; /* internal return value */
1950 xfs_da_state_t
*state
; /* btree cursor */
1952 trace_xfs_dir2_node_replace(args
);
1955 * Allocate and initialize the btree cursor.
1957 state
= xfs_da_state_alloc();
1959 state
->mp
= args
->dp
->i_mount
;
1960 state
->blocksize
= state
->mp
->m_dirblksize
;
1961 state
->node_ents
= state
->mp
->m_dir_node_ents
;
1962 inum
= args
->inumber
;
1964 * Lookup the entry to change in the btree.
1966 error
= xfs_da_node_lookup_int(state
, &rval
);
1971 * It should be found, since the vnodeops layer has looked it up
1972 * and locked it. But paranoia is good.
1974 if (rval
== EEXIST
) {
1976 * Find the leaf entry.
1978 blk
= &state
->path
.blk
[state
->path
.active
- 1];
1979 ASSERT(blk
->magic
== XFS_DIR2_LEAFN_MAGIC
);
1980 leaf
= blk
->bp
->data
;
1981 lep
= &leaf
->ents
[blk
->index
];
1982 ASSERT(state
->extravalid
);
1984 * Point to the data entry.
1986 data
= state
->extrablk
.bp
->data
;
1987 ASSERT(be32_to_cpu(data
->hdr
.magic
) == XFS_DIR2_DATA_MAGIC
);
1988 dep
= (xfs_dir2_data_entry_t
*)
1990 xfs_dir2_dataptr_to_off(state
->mp
, be32_to_cpu(lep
->address
)));
1991 ASSERT(inum
!= be64_to_cpu(dep
->inumber
));
1993 * Fill in the new inode number and log the entry.
1995 dep
->inumber
= cpu_to_be64(inum
);
1996 xfs_dir2_data_log_entry(args
->trans
, state
->extrablk
.bp
, dep
);
2000 * Didn't find it, and we're holding a data block. Drop it.
2002 else if (state
->extravalid
) {
2003 xfs_da_brelse(args
->trans
, state
->extrablk
.bp
);
2004 state
->extrablk
.bp
= NULL
;
2007 * Release all the buffers in the cursor.
2009 for (i
= 0; i
< state
->path
.active
; i
++) {
2010 xfs_da_brelse(args
->trans
, state
->path
.blk
[i
].bp
);
2011 state
->path
.blk
[i
].bp
= NULL
;
2013 xfs_da_state_free(state
);
2018 * Trim off a trailing empty freespace block.
2019 * Return (in rvalp) 1 if we did it, 0 if not.
2022 xfs_dir2_node_trim_free(
2023 xfs_da_args_t
*args
, /* operation arguments */
2024 xfs_fileoff_t fo
, /* free block number */
2025 int *rvalp
) /* out: did something */
2027 xfs_dabuf_t
*bp
; /* freespace buffer */
2028 xfs_inode_t
*dp
; /* incore directory inode */
2029 int error
; /* error return code */
2030 xfs_dir2_free_t
*free
; /* freespace structure */
2031 xfs_mount_t
*mp
; /* filesystem mount point */
2032 xfs_trans_t
*tp
; /* transaction pointer */
2038 * Read the freespace block.
2040 if (unlikely(error
= xfs_da_read_buf(tp
, dp
, (xfs_dablk_t
)fo
, -2, &bp
,
2046 * There can be holes in freespace. If fo is a hole, there's
2053 ASSERT(be32_to_cpu(free
->hdr
.magic
) == XFS_DIR2_FREE_MAGIC
);
2055 * If there are used entries, there's nothing to do.
2057 if (be32_to_cpu(free
->hdr
.nused
) > 0) {
2058 xfs_da_brelse(tp
, bp
);
2063 * Blow the block away.
2066 xfs_dir2_shrink_inode(args
, xfs_dir2_da_to_db(mp
, (xfs_dablk_t
)fo
),
2069 * Can't fail with ENOSPC since that only happens with no
2070 * space reservation, when breaking up an extent into two
2071 * pieces. This is the last block of an extent.
2073 ASSERT(error
!= ENOSPC
);
2074 xfs_da_brelse(tp
, bp
);
2078 * Return that we succeeded.
This page took 0.110058 seconds and 5 git commands to generate.