Linux 2.6.16.9
[wandboard.git] / fs / xfs / xfs_dir2_node.c
blob641f8633d25418309f6393c44de69d82c88c98fd
1 /*
2 * Copyright (c) 2000-2005 Silicon Graphics, Inc.
3 * All Rights Reserved.
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
18 #include "xfs.h"
19 #include "xfs_fs.h"
20 #include "xfs_types.h"
21 #include "xfs_log.h"
22 #include "xfs_inum.h"
23 #include "xfs_trans.h"
24 #include "xfs_sb.h"
25 #include "xfs_dir.h"
26 #include "xfs_dir2.h"
27 #include "xfs_dmapi.h"
28 #include "xfs_mount.h"
29 #include "xfs_da_btree.h"
30 #include "xfs_bmap_btree.h"
31 #include "xfs_dir_sf.h"
32 #include "xfs_dir2_sf.h"
33 #include "xfs_attr_sf.h"
34 #include "xfs_dinode.h"
35 #include "xfs_inode.h"
36 #include "xfs_bmap.h"
37 #include "xfs_dir2_data.h"
38 #include "xfs_dir2_leaf.h"
39 #include "xfs_dir2_block.h"
40 #include "xfs_dir2_node.h"
41 #include "xfs_dir2_trace.h"
42 #include "xfs_error.h"
45 * Function declarations.
47 static void xfs_dir2_free_log_header(xfs_trans_t *tp, xfs_dabuf_t *bp);
48 static int xfs_dir2_leafn_add(xfs_dabuf_t *bp, xfs_da_args_t *args, int index);
49 #ifdef DEBUG
50 static void xfs_dir2_leafn_check(xfs_inode_t *dp, xfs_dabuf_t *bp);
51 #else
52 #define xfs_dir2_leafn_check(dp, bp)
53 #endif
54 static void xfs_dir2_leafn_moveents(xfs_da_args_t *args, xfs_dabuf_t *bp_s,
55 int start_s, xfs_dabuf_t *bp_d, int start_d,
56 int count);
57 static void xfs_dir2_leafn_rebalance(xfs_da_state_t *state,
58 xfs_da_state_blk_t *blk1,
59 xfs_da_state_blk_t *blk2);
60 static int xfs_dir2_leafn_remove(xfs_da_args_t *args, xfs_dabuf_t *bp,
61 int index, xfs_da_state_blk_t *dblk,
62 int *rval);
63 static int xfs_dir2_node_addname_int(xfs_da_args_t *args,
64 xfs_da_state_blk_t *fblk);
67 * Log entries from a freespace block.
69 void
70 xfs_dir2_free_log_bests(
71 xfs_trans_t *tp, /* transaction pointer */
72 xfs_dabuf_t *bp, /* freespace buffer */
73 int first, /* first entry to log */
74 int last) /* last entry to log */
76 xfs_dir2_free_t *free; /* freespace structure */
78 free = bp->data;
79 ASSERT(INT_GET(free->hdr.magic, ARCH_CONVERT) == XFS_DIR2_FREE_MAGIC);
80 xfs_da_log_buf(tp, bp,
81 (uint)((char *)&free->bests[first] - (char *)free),
82 (uint)((char *)&free->bests[last] - (char *)free +
83 sizeof(free->bests[0]) - 1));
87 * Log header from a freespace block.
89 static void
90 xfs_dir2_free_log_header(
91 xfs_trans_t *tp, /* transaction pointer */
92 xfs_dabuf_t *bp) /* freespace buffer */
94 xfs_dir2_free_t *free; /* freespace structure */
96 free = bp->data;
97 ASSERT(INT_GET(free->hdr.magic, ARCH_CONVERT) == XFS_DIR2_FREE_MAGIC);
98 xfs_da_log_buf(tp, bp, (uint)((char *)&free->hdr - (char *)free),
99 (uint)(sizeof(xfs_dir2_free_hdr_t) - 1));
103 * Convert a leaf-format directory to a node-format directory.
104 * We need to change the magic number of the leaf block, and copy
105 * the freespace table out of the leaf block into its own block.
107 int /* error */
108 xfs_dir2_leaf_to_node(
109 xfs_da_args_t *args, /* operation arguments */
110 xfs_dabuf_t *lbp) /* leaf buffer */
112 xfs_inode_t *dp; /* incore directory inode */
113 int error; /* error return value */
114 xfs_dabuf_t *fbp; /* freespace buffer */
115 xfs_dir2_db_t fdb; /* freespace block number */
116 xfs_dir2_free_t *free; /* freespace structure */
117 xfs_dir2_data_off_t *from; /* pointer to freespace entry */
118 int i; /* leaf freespace index */
119 xfs_dir2_leaf_t *leaf; /* leaf structure */
120 xfs_dir2_leaf_tail_t *ltp; /* leaf tail structure */
121 xfs_mount_t *mp; /* filesystem mount point */
122 int n; /* count of live freespc ents */
123 xfs_dir2_data_off_t off; /* freespace entry value */
124 xfs_dir2_data_off_t *to; /* pointer to freespace entry */
125 xfs_trans_t *tp; /* transaction pointer */
127 xfs_dir2_trace_args_b("leaf_to_node", args, lbp);
128 dp = args->dp;
129 mp = dp->i_mount;
130 tp = args->trans;
132 * Add a freespace block to the directory.
134 if ((error = xfs_dir2_grow_inode(args, XFS_DIR2_FREE_SPACE, &fdb))) {
135 return error;
137 ASSERT(fdb == XFS_DIR2_FREE_FIRSTDB(mp));
139 * Get the buffer for the new freespace block.
141 if ((error = xfs_da_get_buf(tp, dp, XFS_DIR2_DB_TO_DA(mp, fdb), -1, &fbp,
142 XFS_DATA_FORK))) {
143 return error;
145 ASSERT(fbp != NULL);
146 free = fbp->data;
147 leaf = lbp->data;
148 ltp = XFS_DIR2_LEAF_TAIL_P(mp, leaf);
150 * Initialize the freespace block header.
152 INT_SET(free->hdr.magic, ARCH_CONVERT, XFS_DIR2_FREE_MAGIC);
153 free->hdr.firstdb = 0;
154 ASSERT(INT_GET(ltp->bestcount, ARCH_CONVERT) <= (uint)dp->i_d.di_size / mp->m_dirblksize);
155 INT_COPY(free->hdr.nvalid, ltp->bestcount, ARCH_CONVERT);
157 * Copy freespace entries from the leaf block to the new block.
158 * Count active entries.
160 for (i = n = 0, from = XFS_DIR2_LEAF_BESTS_P(ltp), to = free->bests;
161 i < INT_GET(ltp->bestcount, ARCH_CONVERT); i++, from++, to++) {
162 if ((off = INT_GET(*from, ARCH_CONVERT)) != NULLDATAOFF)
163 n++;
164 INT_SET(*to, ARCH_CONVERT, off);
166 INT_SET(free->hdr.nused, ARCH_CONVERT, n);
167 INT_SET(leaf->hdr.info.magic, ARCH_CONVERT, XFS_DIR2_LEAFN_MAGIC);
169 * Log everything.
171 xfs_dir2_leaf_log_header(tp, lbp);
172 xfs_dir2_free_log_header(tp, fbp);
173 xfs_dir2_free_log_bests(tp, fbp, 0, INT_GET(free->hdr.nvalid, ARCH_CONVERT) - 1);
174 xfs_da_buf_done(fbp);
175 xfs_dir2_leafn_check(dp, lbp);
176 return 0;
180 * Add a leaf entry to a leaf block in a node-form directory.
181 * The other work necessary is done from the caller.
183 static int /* error */
184 xfs_dir2_leafn_add(
185 xfs_dabuf_t *bp, /* leaf buffer */
186 xfs_da_args_t *args, /* operation arguments */
187 int index) /* insertion pt for new entry */
189 int compact; /* compacting stale leaves */
190 xfs_inode_t *dp; /* incore directory inode */
191 int highstale; /* next stale entry */
192 xfs_dir2_leaf_t *leaf; /* leaf structure */
193 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
194 int lfloghigh; /* high leaf entry logging */
195 int lfloglow; /* low leaf entry logging */
196 int lowstale; /* previous stale entry */
197 xfs_mount_t *mp; /* filesystem mount point */
198 xfs_trans_t *tp; /* transaction pointer */
200 xfs_dir2_trace_args_sb("leafn_add", args, index, bp);
201 dp = args->dp;
202 mp = dp->i_mount;
203 tp = args->trans;
204 leaf = bp->data;
207 * Quick check just to make sure we are not going to index
208 * into other peoples memory
210 if (index < 0)
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
217 * a compact.
220 if (INT_GET(leaf->hdr.count, ARCH_CONVERT) == XFS_DIR2_MAX_LEAF_ENTS(mp)) {
221 if (!leaf->hdr.stale)
222 return XFS_ERROR(ENOSPC);
223 compact = INT_GET(leaf->hdr.stale, ARCH_CONVERT) > 1;
224 } else
225 compact = 0;
226 ASSERT(index == 0 || INT_GET(leaf->ents[index - 1].hashval, ARCH_CONVERT) <= args->hashval);
227 ASSERT(index == INT_GET(leaf->hdr.count, ARCH_CONVERT) ||
228 INT_GET(leaf->ents[index].hashval, ARCH_CONVERT) >= args->hashval);
230 if (args->justcheck)
231 return 0;
234 * Compact out all but one stale leaf entry. Leaves behind
235 * the entry closest to index.
237 if (compact) {
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 = INT_GET(leaf->hdr.count, ARCH_CONVERT);
246 lfloghigh = -1;
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 < INT_GET(leaf->hdr.count, ARCH_CONVERT))
254 memmove(lep + 1, lep,
255 (INT_GET(leaf->hdr.count, ARCH_CONVERT) - index) * sizeof(*lep));
256 lfloglow = index;
257 lfloghigh = INT_GET(leaf->hdr.count, ARCH_CONVERT);
258 INT_MOD(leaf->hdr.count, ARCH_CONVERT, +1);
261 * There are stale entries. We'll use one for the new entry.
263 else {
265 * If we didn't do a compact then we need to figure out
266 * which stale entry will be used.
268 if (compact == 0) {
270 * Find first stale entry before our insertion point.
272 for (lowstale = index - 1;
273 lowstale >= 0 &&
274 INT_GET(leaf->ents[lowstale].address, ARCH_CONVERT) !=
275 XFS_DIR2_NULL_DATAPTR;
276 lowstale--)
277 continue;
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 < INT_GET(leaf->hdr.count, ARCH_CONVERT) &&
285 INT_GET(leaf->ents[highstale].address, ARCH_CONVERT) !=
286 XFS_DIR2_NULL_DATAPTR &&
287 (lowstale < 0 ||
288 index - lowstale - 1 >= highstale - index);
289 highstale++)
290 continue;
293 * Using the low stale entry.
294 * Shift entries up toward the stale slot.
296 if (lowstale >= 0 &&
297 (highstale == INT_GET(leaf->hdr.count, ARCH_CONVERT) ||
298 index - lowstale - 1 < highstale - index)) {
299 ASSERT(INT_GET(leaf->ents[lowstale].address, ARCH_CONVERT) ==
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.
314 else {
315 ASSERT(INT_GET(leaf->ents[highstale].address, ARCH_CONVERT) ==
316 XFS_DIR2_NULL_DATAPTR);
317 ASSERT(highstale - index >= 0);
318 if (highstale - index > 0)
319 memmove(&leaf->ents[index + 1],
320 &leaf->ents[index],
321 (highstale - index) * sizeof(*lep));
322 lep = &leaf->ents[index];
323 lfloglow = MIN(index, lfloglow);
324 lfloghigh = MAX(highstale, lfloghigh);
326 INT_MOD(leaf->hdr.stale, ARCH_CONVERT, -1);
329 * Insert the new entry, log everything.
331 INT_SET(lep->hashval, ARCH_CONVERT, args->hashval);
332 INT_SET(lep->address, ARCH_CONVERT, XFS_DIR2_DB_OFF_TO_DATAPTR(mp, args->blkno, args->index));
333 xfs_dir2_leaf_log_header(tp, bp);
334 xfs_dir2_leaf_log_ents(tp, bp, lfloglow, lfloghigh);
335 xfs_dir2_leafn_check(dp, bp);
336 return 0;
339 #ifdef DEBUG
341 * Check internal consistency of a leafn block.
343 void
344 xfs_dir2_leafn_check(
345 xfs_inode_t *dp, /* incore directory inode */
346 xfs_dabuf_t *bp) /* leaf buffer */
348 int i; /* leaf index */
349 xfs_dir2_leaf_t *leaf; /* leaf structure */
350 xfs_mount_t *mp; /* filesystem mount point */
351 int stale; /* count of stale leaves */
353 leaf = bp->data;
354 mp = dp->i_mount;
355 ASSERT(INT_GET(leaf->hdr.info.magic, ARCH_CONVERT) == XFS_DIR2_LEAFN_MAGIC);
356 ASSERT(INT_GET(leaf->hdr.count, ARCH_CONVERT) <= XFS_DIR2_MAX_LEAF_ENTS(mp));
357 for (i = stale = 0; i < INT_GET(leaf->hdr.count, ARCH_CONVERT); i++) {
358 if (i + 1 < INT_GET(leaf->hdr.count, ARCH_CONVERT)) {
359 ASSERT(INT_GET(leaf->ents[i].hashval, ARCH_CONVERT) <=
360 INT_GET(leaf->ents[i + 1].hashval, ARCH_CONVERT));
362 if (INT_GET(leaf->ents[i].address, ARCH_CONVERT) == XFS_DIR2_NULL_DATAPTR)
363 stale++;
365 ASSERT(INT_GET(leaf->hdr.stale, ARCH_CONVERT) == stale);
367 #endif /* DEBUG */
370 * Return the last hash value in the leaf.
371 * Stale entries are ok.
373 xfs_dahash_t /* hash value */
374 xfs_dir2_leafn_lasthash(
375 xfs_dabuf_t *bp, /* leaf buffer */
376 int *count) /* count of entries in leaf */
378 xfs_dir2_leaf_t *leaf; /* leaf structure */
380 leaf = bp->data;
381 ASSERT(INT_GET(leaf->hdr.info.magic, ARCH_CONVERT) == XFS_DIR2_LEAFN_MAGIC);
382 if (count)
383 *count = INT_GET(leaf->hdr.count, ARCH_CONVERT);
384 if (!leaf->hdr.count)
385 return 0;
386 return INT_GET(leaf->ents[INT_GET(leaf->hdr.count, ARCH_CONVERT) - 1].hashval, ARCH_CONVERT);
390 * Look up a leaf entry in a node-format leaf block.
391 * If this is an addname then the extrablk in state is a freespace block,
392 * otherwise it's a data block.
395 xfs_dir2_leafn_lookup_int(
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; /* current data/free buffer */
402 xfs_dir2_db_t curdb; /* current data block number */
403 xfs_dir2_db_t curfdb; /* current free block number */
404 xfs_dir2_data_entry_t *dep; /* data block entry */
405 xfs_inode_t *dp; /* incore directory inode */
406 int error; /* error return value */
407 int fi; /* free entry index */
408 xfs_dir2_free_t *free=NULL; /* free block structure */
409 int index; /* leaf entry index */
410 xfs_dir2_leaf_t *leaf; /* leaf structure */
411 int length=0; /* length of new data entry */
412 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
413 xfs_mount_t *mp; /* filesystem mount point */
414 xfs_dir2_db_t newdb; /* new data block number */
415 xfs_dir2_db_t newfdb; /* new free block number */
416 xfs_trans_t *tp; /* transaction pointer */
418 dp = args->dp;
419 tp = args->trans;
420 mp = dp->i_mount;
421 leaf = bp->data;
422 ASSERT(INT_GET(leaf->hdr.info.magic, ARCH_CONVERT) == XFS_DIR2_LEAFN_MAGIC);
423 #ifdef __KERNEL__
424 ASSERT(INT_GET(leaf->hdr.count, ARCH_CONVERT) > 0);
425 #endif
426 xfs_dir2_leafn_check(dp, bp);
428 * Look up the hash value in the leaf entries.
430 index = xfs_dir2_leaf_search_hash(args, bp);
432 * Do we have a buffer coming in?
434 if (state->extravalid)
435 curbp = state->extrablk.bp;
436 else
437 curbp = NULL;
439 * For addname, it's a free block buffer, get the block number.
441 if (args->addname) {
442 curfdb = curbp ? state->extrablk.blkno : -1;
443 curdb = -1;
444 length = XFS_DIR2_DATA_ENTSIZE(args->namelen);
445 if ((free = (curbp ? curbp->data : NULL)))
446 ASSERT(INT_GET(free->hdr.magic, ARCH_CONVERT) == XFS_DIR2_FREE_MAGIC);
449 * For others, it's a data block buffer, get the block number.
451 else {
452 curfdb = -1;
453 curdb = curbp ? state->extrablk.blkno : -1;
456 * Loop over leaf entries with the right hash value.
458 for (lep = &leaf->ents[index];
459 index < INT_GET(leaf->hdr.count, ARCH_CONVERT) && INT_GET(lep->hashval, ARCH_CONVERT) == args->hashval;
460 lep++, index++) {
462 * Skip stale leaf entries.
464 if (INT_GET(lep->address, ARCH_CONVERT) == XFS_DIR2_NULL_DATAPTR)
465 continue;
467 * Pull the data block number from the entry.
469 newdb = XFS_DIR2_DATAPTR_TO_DB(mp, INT_GET(lep->address, ARCH_CONVERT));
471 * For addname, we're looking for a place to put the new entry.
472 * We want to use a data block with an entry of equal
473 * hash value to ours if there is one with room.
475 if (args->addname) {
477 * If this block isn't the data block we already have
478 * in hand, take a look at it.
480 if (newdb != curdb) {
481 curdb = newdb;
483 * Convert the data block to the free block
484 * holding its freespace information.
486 newfdb = XFS_DIR2_DB_TO_FDB(mp, newdb);
488 * If it's not the one we have in hand,
489 * read it in.
491 if (newfdb != curfdb) {
493 * If we had one before, drop it.
495 if (curbp)
496 xfs_da_brelse(tp, curbp);
498 * Read the free block.
500 if ((error = xfs_da_read_buf(tp, dp,
501 XFS_DIR2_DB_TO_DA(mp,
502 newfdb),
503 -1, &curbp,
504 XFS_DATA_FORK))) {
505 return error;
507 curfdb = newfdb;
508 free = curbp->data;
509 ASSERT(INT_GET(free->hdr.magic, ARCH_CONVERT) ==
510 XFS_DIR2_FREE_MAGIC);
511 ASSERT((INT_GET(free->hdr.firstdb, ARCH_CONVERT) %
512 XFS_DIR2_MAX_FREE_BESTS(mp)) ==
514 ASSERT(INT_GET(free->hdr.firstdb, ARCH_CONVERT) <= curdb);
515 ASSERT(curdb <
516 INT_GET(free->hdr.firstdb, ARCH_CONVERT) +
517 INT_GET(free->hdr.nvalid, ARCH_CONVERT));
520 * Get the index for our entry.
522 fi = XFS_DIR2_DB_TO_FDINDEX(mp, curdb);
524 * If it has room, return it.
526 if (unlikely(INT_GET(free->bests[fi], ARCH_CONVERT) == NULLDATAOFF)) {
527 XFS_ERROR_REPORT("xfs_dir2_leafn_lookup_int",
528 XFS_ERRLEVEL_LOW, mp);
529 return XFS_ERROR(EFSCORRUPTED);
531 if (INT_GET(free->bests[fi], ARCH_CONVERT) >= length) {
532 *indexp = index;
533 state->extravalid = 1;
534 state->extrablk.bp = curbp;
535 state->extrablk.blkno = curfdb;
536 state->extrablk.index = fi;
537 state->extrablk.magic =
538 XFS_DIR2_FREE_MAGIC;
539 ASSERT(args->oknoent);
540 return XFS_ERROR(ENOENT);
545 * Not adding a new entry, so we really want to find
546 * the name given to us.
548 else {
550 * If it's a different data block, go get it.
552 if (newdb != curdb) {
554 * If we had a block before, drop it.
556 if (curbp)
557 xfs_da_brelse(tp, curbp);
559 * Read the data block.
561 if ((error =
562 xfs_da_read_buf(tp, dp,
563 XFS_DIR2_DB_TO_DA(mp, newdb), -1,
564 &curbp, XFS_DATA_FORK))) {
565 return error;
567 xfs_dir2_data_check(dp, curbp);
568 curdb = newdb;
571 * Point to the data entry.
573 dep = (xfs_dir2_data_entry_t *)
574 ((char *)curbp->data +
575 XFS_DIR2_DATAPTR_TO_OFF(mp, INT_GET(lep->address, ARCH_CONVERT)));
577 * Compare the entry, return it if it matches.
579 if (dep->namelen == args->namelen &&
580 dep->name[0] == args->name[0] &&
581 memcmp(dep->name, args->name, args->namelen) == 0) {
582 args->inumber = INT_GET(dep->inumber, ARCH_CONVERT);
583 *indexp = index;
584 state->extravalid = 1;
585 state->extrablk.bp = curbp;
586 state->extrablk.blkno = curdb;
587 state->extrablk.index =
588 (int)((char *)dep -
589 (char *)curbp->data);
590 state->extrablk.magic = XFS_DIR2_DATA_MAGIC;
591 return XFS_ERROR(EEXIST);
596 * Didn't find a match.
597 * If we are holding a buffer, give it back in case our caller
598 * finds it useful.
600 if ((state->extravalid = (curbp != NULL))) {
601 state->extrablk.bp = curbp;
602 state->extrablk.index = -1;
604 * For addname, giving back a free block.
606 if (args->addname) {
607 state->extrablk.blkno = curfdb;
608 state->extrablk.magic = XFS_DIR2_FREE_MAGIC;
611 * For other callers, giving back a data block.
613 else {
614 state->extrablk.blkno = curdb;
615 state->extrablk.magic = XFS_DIR2_DATA_MAGIC;
619 * Return the final index, that will be the insertion point.
621 *indexp = index;
622 ASSERT(index == INT_GET(leaf->hdr.count, ARCH_CONVERT) || args->oknoent);
623 return XFS_ERROR(ENOENT);
627 * Move count leaf entries from source to destination leaf.
628 * Log entries and headers. Stale entries are preserved.
630 static void
631 xfs_dir2_leafn_moveents(
632 xfs_da_args_t *args, /* operation arguments */
633 xfs_dabuf_t *bp_s, /* source leaf buffer */
634 int start_s, /* source leaf index */
635 xfs_dabuf_t *bp_d, /* destination leaf buffer */
636 int start_d, /* destination leaf index */
637 int count) /* count of leaves to copy */
639 xfs_dir2_leaf_t *leaf_d; /* destination leaf structure */
640 xfs_dir2_leaf_t *leaf_s; /* source leaf structure */
641 int stale; /* count stale leaves copied */
642 xfs_trans_t *tp; /* transaction pointer */
644 xfs_dir2_trace_args_bibii("leafn_moveents", args, bp_s, start_s, bp_d,
645 start_d, count);
647 * Silently return if nothing to do.
649 if (count == 0) {
650 return;
652 tp = args->trans;
653 leaf_s = bp_s->data;
654 leaf_d = bp_d->data;
656 * If the destination index is not the end of the current
657 * destination leaf entries, open up a hole in the destination
658 * to hold the new entries.
660 if (start_d < INT_GET(leaf_d->hdr.count, ARCH_CONVERT)) {
661 memmove(&leaf_d->ents[start_d + count], &leaf_d->ents[start_d],
662 (INT_GET(leaf_d->hdr.count, ARCH_CONVERT) - start_d) *
663 sizeof(xfs_dir2_leaf_entry_t));
664 xfs_dir2_leaf_log_ents(tp, bp_d, start_d + count,
665 count + INT_GET(leaf_d->hdr.count, ARCH_CONVERT) - 1);
668 * If the source has stale leaves, count the ones in the copy range
669 * so we can update the header correctly.
671 if (leaf_s->hdr.stale) {
672 int i; /* temp leaf index */
674 for (i = start_s, stale = 0; i < start_s + count; i++) {
675 if (INT_GET(leaf_s->ents[i].address, ARCH_CONVERT) == XFS_DIR2_NULL_DATAPTR)
676 stale++;
678 } else
679 stale = 0;
681 * Copy the leaf entries from source to destination.
683 memcpy(&leaf_d->ents[start_d], &leaf_s->ents[start_s],
684 count * sizeof(xfs_dir2_leaf_entry_t));
685 xfs_dir2_leaf_log_ents(tp, bp_d, start_d, start_d + count - 1);
687 * If there are source entries after the ones we copied,
688 * delete the ones we copied by sliding the next ones down.
690 if (start_s + count < INT_GET(leaf_s->hdr.count, ARCH_CONVERT)) {
691 memmove(&leaf_s->ents[start_s], &leaf_s->ents[start_s + count],
692 count * sizeof(xfs_dir2_leaf_entry_t));
693 xfs_dir2_leaf_log_ents(tp, bp_s, start_s, start_s + count - 1);
696 * Update the headers and log them.
698 INT_MOD(leaf_s->hdr.count, ARCH_CONVERT, -(count));
699 INT_MOD(leaf_s->hdr.stale, ARCH_CONVERT, -(stale));
700 INT_MOD(leaf_d->hdr.count, ARCH_CONVERT, count);
701 INT_MOD(leaf_d->hdr.stale, ARCH_CONVERT, stale);
702 xfs_dir2_leaf_log_header(tp, bp_s);
703 xfs_dir2_leaf_log_header(tp, bp_d);
704 xfs_dir2_leafn_check(args->dp, bp_s);
705 xfs_dir2_leafn_check(args->dp, bp_d);
709 * Determine the sort order of two leaf blocks.
710 * Returns 1 if both are valid and leaf2 should be before leaf1, else 0.
712 int /* sort order */
713 xfs_dir2_leafn_order(
714 xfs_dabuf_t *leaf1_bp, /* leaf1 buffer */
715 xfs_dabuf_t *leaf2_bp) /* leaf2 buffer */
717 xfs_dir2_leaf_t *leaf1; /* leaf1 structure */
718 xfs_dir2_leaf_t *leaf2; /* leaf2 structure */
720 leaf1 = leaf1_bp->data;
721 leaf2 = leaf2_bp->data;
722 ASSERT(INT_GET(leaf1->hdr.info.magic, ARCH_CONVERT) == XFS_DIR2_LEAFN_MAGIC);
723 ASSERT(INT_GET(leaf2->hdr.info.magic, ARCH_CONVERT) == XFS_DIR2_LEAFN_MAGIC);
724 if (INT_GET(leaf1->hdr.count, ARCH_CONVERT) > 0 &&
725 INT_GET(leaf2->hdr.count, ARCH_CONVERT) > 0 &&
726 (INT_GET(leaf2->ents[0].hashval, ARCH_CONVERT) < INT_GET(leaf1->ents[0].hashval, ARCH_CONVERT) ||
727 INT_GET(leaf2->ents[INT_GET(leaf2->hdr.count, ARCH_CONVERT) - 1].hashval, ARCH_CONVERT) <
728 INT_GET(leaf1->ents[INT_GET(leaf1->hdr.count, ARCH_CONVERT) - 1].hashval, ARCH_CONVERT)))
729 return 1;
730 return 0;
734 * Rebalance leaf entries between two leaf blocks.
735 * This is actually only called when the second block is new,
736 * though the code deals with the general case.
737 * A new entry will be inserted in one of the blocks, and that
738 * entry is taken into account when balancing.
740 static void
741 xfs_dir2_leafn_rebalance(
742 xfs_da_state_t *state, /* btree cursor */
743 xfs_da_state_blk_t *blk1, /* first btree block */
744 xfs_da_state_blk_t *blk2) /* second btree block */
746 xfs_da_args_t *args; /* operation arguments */
747 int count; /* count (& direction) leaves */
748 int isleft; /* new goes in left leaf */
749 xfs_dir2_leaf_t *leaf1; /* first leaf structure */
750 xfs_dir2_leaf_t *leaf2; /* second leaf structure */
751 int mid; /* midpoint leaf index */
752 #ifdef DEBUG
753 int oldstale; /* old count of stale leaves */
754 #endif
755 int oldsum; /* old total leaf count */
756 int swap; /* swapped leaf blocks */
758 args = state->args;
760 * If the block order is wrong, swap the arguments.
762 if ((swap = xfs_dir2_leafn_order(blk1->bp, blk2->bp))) {
763 xfs_da_state_blk_t *tmp; /* temp for block swap */
765 tmp = blk1;
766 blk1 = blk2;
767 blk2 = tmp;
769 leaf1 = blk1->bp->data;
770 leaf2 = blk2->bp->data;
771 oldsum = INT_GET(leaf1->hdr.count, ARCH_CONVERT) + INT_GET(leaf2->hdr.count, ARCH_CONVERT);
772 #ifdef DEBUG
773 oldstale = INT_GET(leaf1->hdr.stale, ARCH_CONVERT) + INT_GET(leaf2->hdr.stale, ARCH_CONVERT);
774 #endif
775 mid = oldsum >> 1;
777 * If the old leaf count was odd then the new one will be even,
778 * so we need to divide the new count evenly.
780 if (oldsum & 1) {
781 xfs_dahash_t midhash; /* middle entry hash value */
783 if (mid >= INT_GET(leaf1->hdr.count, ARCH_CONVERT))
784 midhash = INT_GET(leaf2->ents[mid - INT_GET(leaf1->hdr.count, ARCH_CONVERT)].hashval, ARCH_CONVERT);
785 else
786 midhash = INT_GET(leaf1->ents[mid].hashval, ARCH_CONVERT);
787 isleft = args->hashval <= midhash;
790 * If the old count is even then the new count is odd, so there's
791 * no preferred side for the new entry.
792 * Pick the left one.
794 else
795 isleft = 1;
797 * Calculate moved entry count. Positive means left-to-right,
798 * negative means right-to-left. Then move the entries.
800 count = INT_GET(leaf1->hdr.count, ARCH_CONVERT) - mid + (isleft == 0);
801 if (count > 0)
802 xfs_dir2_leafn_moveents(args, blk1->bp,
803 INT_GET(leaf1->hdr.count, ARCH_CONVERT) - count, blk2->bp, 0, count);
804 else if (count < 0)
805 xfs_dir2_leafn_moveents(args, blk2->bp, 0, blk1->bp,
806 INT_GET(leaf1->hdr.count, ARCH_CONVERT), count);
807 ASSERT(INT_GET(leaf1->hdr.count, ARCH_CONVERT) + INT_GET(leaf2->hdr.count, ARCH_CONVERT) == oldsum);
808 ASSERT(INT_GET(leaf1->hdr.stale, ARCH_CONVERT) + INT_GET(leaf2->hdr.stale, ARCH_CONVERT) == oldstale);
810 * Mark whether we're inserting into the old or new leaf.
812 if (INT_GET(leaf1->hdr.count, ARCH_CONVERT) < INT_GET(leaf2->hdr.count, ARCH_CONVERT))
813 state->inleaf = swap;
814 else if (INT_GET(leaf1->hdr.count, ARCH_CONVERT) > INT_GET(leaf2->hdr.count, ARCH_CONVERT))
815 state->inleaf = !swap;
816 else
817 state->inleaf =
818 swap ^ (blk1->index <= INT_GET(leaf1->hdr.count, ARCH_CONVERT));
820 * Adjust the expected index for insertion.
822 if (!state->inleaf)
823 blk2->index = blk1->index - INT_GET(leaf1->hdr.count, ARCH_CONVERT);
826 * Finally sanity check just to make sure we are not returning a negative index
828 if(blk2->index < 0) {
829 state->inleaf = 1;
830 blk2->index = 0;
831 cmn_err(CE_ALERT,
832 "xfs_dir2_leafn_rebalance: picked the wrong leaf? reverting orignal leaf: "
833 "blk1->index %d\n",
834 blk1->index);
839 * Remove an entry from a node directory.
840 * This removes the leaf entry and the data entry,
841 * and updates the free block if necessary.
843 static int /* error */
844 xfs_dir2_leafn_remove(
845 xfs_da_args_t *args, /* operation arguments */
846 xfs_dabuf_t *bp, /* leaf buffer */
847 int index, /* leaf entry index */
848 xfs_da_state_blk_t *dblk, /* data block */
849 int *rval) /* resulting block needs join */
851 xfs_dir2_data_t *data; /* data block structure */
852 xfs_dir2_db_t db; /* data block number */
853 xfs_dabuf_t *dbp; /* data block buffer */
854 xfs_dir2_data_entry_t *dep; /* data block entry */
855 xfs_inode_t *dp; /* incore directory inode */
856 xfs_dir2_leaf_t *leaf; /* leaf structure */
857 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
858 int longest; /* longest data free entry */
859 int off; /* data block entry offset */
860 xfs_mount_t *mp; /* filesystem mount point */
861 int needlog; /* need to log data header */
862 int needscan; /* need to rescan data frees */
863 xfs_trans_t *tp; /* transaction pointer */
865 xfs_dir2_trace_args_sb("leafn_remove", args, index, bp);
866 dp = args->dp;
867 tp = args->trans;
868 mp = dp->i_mount;
869 leaf = bp->data;
870 ASSERT(INT_GET(leaf->hdr.info.magic, ARCH_CONVERT) == XFS_DIR2_LEAFN_MAGIC);
872 * Point to the entry we're removing.
874 lep = &leaf->ents[index];
876 * Extract the data block and offset from the entry.
878 db = XFS_DIR2_DATAPTR_TO_DB(mp, INT_GET(lep->address, ARCH_CONVERT));
879 ASSERT(dblk->blkno == db);
880 off = XFS_DIR2_DATAPTR_TO_OFF(mp, INT_GET(lep->address, ARCH_CONVERT));
881 ASSERT(dblk->index == off);
883 * Kill the leaf entry by marking it stale.
884 * Log the leaf block changes.
886 INT_MOD(leaf->hdr.stale, ARCH_CONVERT, +1);
887 xfs_dir2_leaf_log_header(tp, bp);
888 INT_SET(lep->address, ARCH_CONVERT, XFS_DIR2_NULL_DATAPTR);
889 xfs_dir2_leaf_log_ents(tp, bp, index, index);
891 * Make the data entry free. Keep track of the longest freespace
892 * in the data block in case it changes.
894 dbp = dblk->bp;
895 data = dbp->data;
896 dep = (xfs_dir2_data_entry_t *)((char *)data + off);
897 longest = INT_GET(data->hdr.bestfree[0].length, ARCH_CONVERT);
898 needlog = needscan = 0;
899 xfs_dir2_data_make_free(tp, dbp, off,
900 XFS_DIR2_DATA_ENTSIZE(dep->namelen), &needlog, &needscan);
902 * Rescan the data block freespaces for bestfree.
903 * Log the data block header if needed.
905 if (needscan)
906 xfs_dir2_data_freescan(mp, data, &needlog, NULL);
907 if (needlog)
908 xfs_dir2_data_log_header(tp, dbp);
909 xfs_dir2_data_check(dp, dbp);
911 * If the longest data block freespace changes, need to update
912 * the corresponding freeblock entry.
914 if (longest < INT_GET(data->hdr.bestfree[0].length, ARCH_CONVERT)) {
915 int error; /* error return value */
916 xfs_dabuf_t *fbp; /* freeblock buffer */
917 xfs_dir2_db_t fdb; /* freeblock block number */
918 int findex; /* index in freeblock entries */
919 xfs_dir2_free_t *free; /* freeblock structure */
920 int logfree; /* need to log free entry */
923 * Convert the data block number to a free block,
924 * read in the free block.
926 fdb = XFS_DIR2_DB_TO_FDB(mp, db);
927 if ((error = xfs_da_read_buf(tp, dp, XFS_DIR2_DB_TO_DA(mp, fdb),
928 -1, &fbp, XFS_DATA_FORK))) {
929 return error;
931 free = fbp->data;
932 ASSERT(INT_GET(free->hdr.magic, ARCH_CONVERT) == XFS_DIR2_FREE_MAGIC);
933 ASSERT(INT_GET(free->hdr.firstdb, ARCH_CONVERT) ==
934 XFS_DIR2_MAX_FREE_BESTS(mp) *
935 (fdb - XFS_DIR2_FREE_FIRSTDB(mp)));
937 * Calculate which entry we need to fix.
939 findex = XFS_DIR2_DB_TO_FDINDEX(mp, db);
940 longest = INT_GET(data->hdr.bestfree[0].length, ARCH_CONVERT);
942 * If the data block is now empty we can get rid of it
943 * (usually).
945 if (longest == mp->m_dirblksize - (uint)sizeof(data->hdr)) {
947 * Try to punch out the data block.
949 error = xfs_dir2_shrink_inode(args, db, dbp);
950 if (error == 0) {
951 dblk->bp = NULL;
952 data = NULL;
955 * We can get ENOSPC if there's no space reservation.
956 * In this case just drop the buffer and some one else
957 * will eventually get rid of the empty block.
959 else if (error == ENOSPC && args->total == 0)
960 xfs_da_buf_done(dbp);
961 else
962 return error;
965 * If we got rid of the data block, we can eliminate that entry
966 * in the free block.
968 if (data == NULL) {
970 * One less used entry in the free table.
972 INT_MOD(free->hdr.nused, ARCH_CONVERT, -1);
973 xfs_dir2_free_log_header(tp, fbp);
975 * If this was the last entry in the table, we can
976 * trim the table size back. There might be other
977 * entries at the end referring to non-existent
978 * data blocks, get those too.
980 if (findex == INT_GET(free->hdr.nvalid, ARCH_CONVERT) - 1) {
981 int i; /* free entry index */
983 for (i = findex - 1;
984 i >= 0 && INT_GET(free->bests[i], ARCH_CONVERT) == NULLDATAOFF;
985 i--)
986 continue;
987 INT_SET(free->hdr.nvalid, ARCH_CONVERT, i + 1);
988 logfree = 0;
991 * Not the last entry, just punch it out.
993 else {
994 INT_SET(free->bests[findex], ARCH_CONVERT, NULLDATAOFF);
995 logfree = 1;
998 * If there are no useful entries left in the block,
999 * get rid of the block if we can.
1001 if (!free->hdr.nused) {
1002 error = xfs_dir2_shrink_inode(args, fdb, fbp);
1003 if (error == 0) {
1004 fbp = NULL;
1005 logfree = 0;
1006 } else if (error != ENOSPC || args->total != 0)
1007 return error;
1009 * It's possible to get ENOSPC if there is no
1010 * space reservation. In this case some one
1011 * else will eventually get rid of this block.
1016 * Data block is not empty, just set the free entry to
1017 * the new value.
1019 else {
1020 INT_SET(free->bests[findex], ARCH_CONVERT, longest);
1021 logfree = 1;
1024 * Log the free entry that changed, unless we got rid of it.
1026 if (logfree)
1027 xfs_dir2_free_log_bests(tp, fbp, findex, findex);
1029 * Drop the buffer if we still have it.
1031 if (fbp)
1032 xfs_da_buf_done(fbp);
1034 xfs_dir2_leafn_check(dp, bp);
1036 * Return indication of whether this leaf block is emtpy enough
1037 * to justify trying to join it with a neighbor.
1039 *rval =
1040 ((uint)sizeof(leaf->hdr) +
1041 (uint)sizeof(leaf->ents[0]) *
1042 (INT_GET(leaf->hdr.count, ARCH_CONVERT) - INT_GET(leaf->hdr.stale, ARCH_CONVERT))) <
1043 mp->m_dir_magicpct;
1044 return 0;
1048 * Split the leaf entries in the old block into old and new blocks.
1050 int /* error */
1051 xfs_dir2_leafn_split(
1052 xfs_da_state_t *state, /* btree cursor */
1053 xfs_da_state_blk_t *oldblk, /* original block */
1054 xfs_da_state_blk_t *newblk) /* newly created block */
1056 xfs_da_args_t *args; /* operation arguments */
1057 xfs_dablk_t blkno; /* new leaf block number */
1058 int error; /* error return value */
1059 xfs_mount_t *mp; /* filesystem mount point */
1062 * Allocate space for a new leaf node.
1064 args = state->args;
1065 mp = args->dp->i_mount;
1066 ASSERT(args != NULL);
1067 ASSERT(oldblk->magic == XFS_DIR2_LEAFN_MAGIC);
1068 error = xfs_da_grow_inode(args, &blkno);
1069 if (error) {
1070 return error;
1073 * Initialize the new leaf block.
1075 error = xfs_dir2_leaf_init(args, XFS_DIR2_DA_TO_DB(mp, blkno),
1076 &newblk->bp, XFS_DIR2_LEAFN_MAGIC);
1077 if (error) {
1078 return error;
1080 newblk->blkno = blkno;
1081 newblk->magic = XFS_DIR2_LEAFN_MAGIC;
1083 * Rebalance the entries across the two leaves, link the new
1084 * block into the leaves.
1086 xfs_dir2_leafn_rebalance(state, oldblk, newblk);
1087 error = xfs_da_blk_link(state, oldblk, newblk);
1088 if (error) {
1089 return error;
1092 * Insert the new entry in the correct block.
1094 if (state->inleaf)
1095 error = xfs_dir2_leafn_add(oldblk->bp, args, oldblk->index);
1096 else
1097 error = xfs_dir2_leafn_add(newblk->bp, args, newblk->index);
1099 * Update last hashval in each block since we added the name.
1101 oldblk->hashval = xfs_dir2_leafn_lasthash(oldblk->bp, NULL);
1102 newblk->hashval = xfs_dir2_leafn_lasthash(newblk->bp, NULL);
1103 xfs_dir2_leafn_check(args->dp, oldblk->bp);
1104 xfs_dir2_leafn_check(args->dp, newblk->bp);
1105 return error;
1109 * Check a leaf block and its neighbors to see if the block should be
1110 * collapsed into one or the other neighbor. Always keep the block
1111 * with the smaller block number.
1112 * If the current block is over 50% full, don't try to join it, return 0.
1113 * If the block is empty, fill in the state structure and return 2.
1114 * If it can be collapsed, fill in the state structure and return 1.
1115 * If nothing can be done, return 0.
1117 int /* error */
1118 xfs_dir2_leafn_toosmall(
1119 xfs_da_state_t *state, /* btree cursor */
1120 int *action) /* resulting action to take */
1122 xfs_da_state_blk_t *blk; /* leaf block */
1123 xfs_dablk_t blkno; /* leaf block number */
1124 xfs_dabuf_t *bp; /* leaf buffer */
1125 int bytes; /* bytes in use */
1126 int count; /* leaf live entry count */
1127 int error; /* error return value */
1128 int forward; /* sibling block direction */
1129 int i; /* sibling counter */
1130 xfs_da_blkinfo_t *info; /* leaf block header */
1131 xfs_dir2_leaf_t *leaf; /* leaf structure */
1132 int rval; /* result from path_shift */
1135 * Check for the degenerate case of the block being over 50% full.
1136 * If so, it's not worth even looking to see if we might be able
1137 * to coalesce with a sibling.
1139 blk = &state->path.blk[state->path.active - 1];
1140 info = blk->bp->data;
1141 ASSERT(INT_GET(info->magic, ARCH_CONVERT) == XFS_DIR2_LEAFN_MAGIC);
1142 leaf = (xfs_dir2_leaf_t *)info;
1143 count = INT_GET(leaf->hdr.count, ARCH_CONVERT) - INT_GET(leaf->hdr.stale, ARCH_CONVERT);
1144 bytes = (uint)sizeof(leaf->hdr) + count * (uint)sizeof(leaf->ents[0]);
1145 if (bytes > (state->blocksize >> 1)) {
1147 * Blk over 50%, don't try to join.
1149 *action = 0;
1150 return 0;
1153 * Check for the degenerate case of the block being empty.
1154 * If the block is empty, we'll simply delete it, no need to
1155 * coalesce it with a sibling block. We choose (arbitrarily)
1156 * to merge with the forward block unless it is NULL.
1158 if (count == 0) {
1160 * Make altpath point to the block we want to keep and
1161 * path point to the block we want to drop (this one).
1163 forward = info->forw;
1164 memcpy(&state->altpath, &state->path, sizeof(state->path));
1165 error = xfs_da_path_shift(state, &state->altpath, forward, 0,
1166 &rval);
1167 if (error)
1168 return error;
1169 *action = rval ? 2 : 0;
1170 return 0;
1173 * Examine each sibling block to see if we can coalesce with
1174 * at least 25% free space to spare. We need to figure out
1175 * whether to merge with the forward or the backward block.
1176 * We prefer coalescing with the lower numbered sibling so as
1177 * to shrink a directory over time.
1179 forward = INT_GET(info->forw, ARCH_CONVERT) < INT_GET(info->back, ARCH_CONVERT);
1180 for (i = 0, bp = NULL; i < 2; forward = !forward, i++) {
1181 blkno = forward ?INT_GET( info->forw, ARCH_CONVERT) : INT_GET(info->back, ARCH_CONVERT);
1182 if (blkno == 0)
1183 continue;
1185 * Read the sibling leaf block.
1187 if ((error =
1188 xfs_da_read_buf(state->args->trans, state->args->dp, blkno,
1189 -1, &bp, XFS_DATA_FORK))) {
1190 return error;
1192 ASSERT(bp != NULL);
1194 * Count bytes in the two blocks combined.
1196 leaf = (xfs_dir2_leaf_t *)info;
1197 count = INT_GET(leaf->hdr.count, ARCH_CONVERT) - INT_GET(leaf->hdr.stale, ARCH_CONVERT);
1198 bytes = state->blocksize - (state->blocksize >> 2);
1199 leaf = bp->data;
1200 ASSERT(INT_GET(leaf->hdr.info.magic, ARCH_CONVERT) == XFS_DIR2_LEAFN_MAGIC);
1201 count += INT_GET(leaf->hdr.count, ARCH_CONVERT) - INT_GET(leaf->hdr.stale, ARCH_CONVERT);
1202 bytes -= count * (uint)sizeof(leaf->ents[0]);
1204 * Fits with at least 25% to spare.
1206 if (bytes >= 0)
1207 break;
1208 xfs_da_brelse(state->args->trans, bp);
1211 * Didn't like either block, give up.
1213 if (i >= 2) {
1214 *action = 0;
1215 return 0;
1218 * Done with the sibling leaf block here, drop the dabuf
1219 * so path_shift can get it.
1221 xfs_da_buf_done(bp);
1223 * Make altpath point to the block we want to keep (the lower
1224 * numbered block) and path point to the block we want to drop.
1226 memcpy(&state->altpath, &state->path, sizeof(state->path));
1227 if (blkno < blk->blkno)
1228 error = xfs_da_path_shift(state, &state->altpath, forward, 0,
1229 &rval);
1230 else
1231 error = xfs_da_path_shift(state, &state->path, forward, 0,
1232 &rval);
1233 if (error) {
1234 return error;
1236 *action = rval ? 0 : 1;
1237 return 0;
1241 * Move all the leaf entries from drop_blk to save_blk.
1242 * This is done as part of a join operation.
1244 void
1245 xfs_dir2_leafn_unbalance(
1246 xfs_da_state_t *state, /* cursor */
1247 xfs_da_state_blk_t *drop_blk, /* dead block */
1248 xfs_da_state_blk_t *save_blk) /* surviving block */
1250 xfs_da_args_t *args; /* operation arguments */
1251 xfs_dir2_leaf_t *drop_leaf; /* dead leaf structure */
1252 xfs_dir2_leaf_t *save_leaf; /* surviving leaf structure */
1254 args = state->args;
1255 ASSERT(drop_blk->magic == XFS_DIR2_LEAFN_MAGIC);
1256 ASSERT(save_blk->magic == XFS_DIR2_LEAFN_MAGIC);
1257 drop_leaf = drop_blk->bp->data;
1258 save_leaf = save_blk->bp->data;
1259 ASSERT(INT_GET(drop_leaf->hdr.info.magic, ARCH_CONVERT) == XFS_DIR2_LEAFN_MAGIC);
1260 ASSERT(INT_GET(save_leaf->hdr.info.magic, ARCH_CONVERT) == XFS_DIR2_LEAFN_MAGIC);
1262 * If there are any stale leaf entries, take this opportunity
1263 * to purge them.
1265 if (INT_GET(drop_leaf->hdr.stale, ARCH_CONVERT))
1266 xfs_dir2_leaf_compact(args, drop_blk->bp);
1267 if (INT_GET(save_leaf->hdr.stale, ARCH_CONVERT))
1268 xfs_dir2_leaf_compact(args, save_blk->bp);
1270 * Move the entries from drop to the appropriate end of save.
1272 drop_blk->hashval = INT_GET(drop_leaf->ents[INT_GET(drop_leaf->hdr.count, ARCH_CONVERT) - 1].hashval, ARCH_CONVERT);
1273 if (xfs_dir2_leafn_order(save_blk->bp, drop_blk->bp))
1274 xfs_dir2_leafn_moveents(args, drop_blk->bp, 0, save_blk->bp, 0,
1275 INT_GET(drop_leaf->hdr.count, ARCH_CONVERT));
1276 else
1277 xfs_dir2_leafn_moveents(args, drop_blk->bp, 0, save_blk->bp,
1278 INT_GET(save_leaf->hdr.count, ARCH_CONVERT), INT_GET(drop_leaf->hdr.count, ARCH_CONVERT));
1279 save_blk->hashval = INT_GET(save_leaf->ents[INT_GET(save_leaf->hdr.count, ARCH_CONVERT) - 1].hashval, ARCH_CONVERT);
1280 xfs_dir2_leafn_check(args->dp, save_blk->bp);
1284 * Top-level node form directory addname routine.
1286 int /* error */
1287 xfs_dir2_node_addname(
1288 xfs_da_args_t *args) /* operation arguments */
1290 xfs_da_state_blk_t *blk; /* leaf block for insert */
1291 int error; /* error return value */
1292 int rval; /* sub-return value */
1293 xfs_da_state_t *state; /* btree cursor */
1295 xfs_dir2_trace_args("node_addname", args);
1297 * Allocate and initialize the state (btree cursor).
1299 state = xfs_da_state_alloc();
1300 state->args = args;
1301 state->mp = args->dp->i_mount;
1302 state->blocksize = state->mp->m_dirblksize;
1303 state->node_ents = state->mp->m_dir_node_ents;
1305 * Look up the name. We're not supposed to find it, but
1306 * this gives us the insertion point.
1308 error = xfs_da_node_lookup_int(state, &rval);
1309 if (error)
1310 rval = error;
1311 if (rval != ENOENT) {
1312 goto done;
1315 * Add the data entry to a data block.
1316 * Extravalid is set to a freeblock found by lookup.
1318 rval = xfs_dir2_node_addname_int(args,
1319 state->extravalid ? &state->extrablk : NULL);
1320 if (rval) {
1321 goto done;
1323 blk = &state->path.blk[state->path.active - 1];
1324 ASSERT(blk->magic == XFS_DIR2_LEAFN_MAGIC);
1326 * Add the new leaf entry.
1328 rval = xfs_dir2_leafn_add(blk->bp, args, blk->index);
1329 if (rval == 0) {
1331 * It worked, fix the hash values up the btree.
1333 if (!args->justcheck)
1334 xfs_da_fixhashpath(state, &state->path);
1335 } else {
1337 * It didn't work, we need to split the leaf block.
1339 if (args->total == 0) {
1340 ASSERT(rval == ENOSPC);
1341 goto done;
1344 * Split the leaf block and insert the new entry.
1346 rval = xfs_da_split(state);
1348 done:
1349 xfs_da_state_free(state);
1350 return rval;
1354 * Add the data entry for a node-format directory name addition.
1355 * The leaf entry is added in xfs_dir2_leafn_add.
1356 * We may enter with a freespace block that the lookup found.
1358 static int /* error */
1359 xfs_dir2_node_addname_int(
1360 xfs_da_args_t *args, /* operation arguments */
1361 xfs_da_state_blk_t *fblk) /* optional freespace block */
1363 xfs_dir2_data_t *data; /* data block structure */
1364 xfs_dir2_db_t dbno; /* data block number */
1365 xfs_dabuf_t *dbp; /* data block buffer */
1366 xfs_dir2_data_entry_t *dep; /* data entry pointer */
1367 xfs_inode_t *dp; /* incore directory inode */
1368 xfs_dir2_data_unused_t *dup; /* data unused entry pointer */
1369 int error; /* error return value */
1370 xfs_dir2_db_t fbno; /* freespace block number */
1371 xfs_dabuf_t *fbp; /* freespace buffer */
1372 int findex; /* freespace entry index */
1373 xfs_dir2_free_t *free=NULL; /* freespace block structure */
1374 xfs_dir2_db_t ifbno; /* initial freespace block no */
1375 xfs_dir2_db_t lastfbno=0; /* highest freespace block no */
1376 int length; /* length of the new entry */
1377 int logfree; /* need to log free entry */
1378 xfs_mount_t *mp; /* filesystem mount point */
1379 int needlog; /* need to log data header */
1380 int needscan; /* need to rescan data frees */
1381 xfs_dir2_data_off_t *tagp; /* data entry tag pointer */
1382 xfs_trans_t *tp; /* transaction pointer */
1384 dp = args->dp;
1385 mp = dp->i_mount;
1386 tp = args->trans;
1387 length = XFS_DIR2_DATA_ENTSIZE(args->namelen);
1389 * If we came in with a freespace block that means that lookup
1390 * found an entry with our hash value. This is the freespace
1391 * block for that data entry.
1393 if (fblk) {
1394 fbp = fblk->bp;
1396 * Remember initial freespace block number.
1398 ifbno = fblk->blkno;
1399 free = fbp->data;
1400 ASSERT(INT_GET(free->hdr.magic, ARCH_CONVERT) == XFS_DIR2_FREE_MAGIC);
1401 findex = fblk->index;
1403 * This means the free entry showed that the data block had
1404 * space for our entry, so we remembered it.
1405 * Use that data block.
1407 if (findex >= 0) {
1408 ASSERT(findex < INT_GET(free->hdr.nvalid, ARCH_CONVERT));
1409 ASSERT(INT_GET(free->bests[findex], ARCH_CONVERT) != NULLDATAOFF);
1410 ASSERT(INT_GET(free->bests[findex], ARCH_CONVERT) >= length);
1411 dbno = INT_GET(free->hdr.firstdb, ARCH_CONVERT) + findex;
1414 * The data block looked at didn't have enough room.
1415 * We'll start at the beginning of the freespace entries.
1417 else {
1418 dbno = -1;
1419 findex = 0;
1423 * Didn't come in with a freespace block, so don't have a data block.
1425 else {
1426 ifbno = dbno = -1;
1427 fbp = NULL;
1428 findex = 0;
1431 * If we don't have a data block yet, we're going to scan the
1432 * freespace blocks looking for one. Figure out what the
1433 * highest freespace block number is.
1435 if (dbno == -1) {
1436 xfs_fileoff_t fo; /* freespace block number */
1438 if ((error = xfs_bmap_last_offset(tp, dp, &fo, XFS_DATA_FORK)))
1439 return error;
1440 lastfbno = XFS_DIR2_DA_TO_DB(mp, (xfs_dablk_t)fo);
1441 fbno = ifbno;
1444 * While we haven't identified a data block, search the freeblock
1445 * data for a good data block. If we find a null freeblock entry,
1446 * indicating a hole in the data blocks, remember that.
1448 while (dbno == -1) {
1450 * If we don't have a freeblock in hand, get the next one.
1452 if (fbp == NULL) {
1454 * Happens the first time through unless lookup gave
1455 * us a freespace block to start with.
1457 if (++fbno == 0)
1458 fbno = XFS_DIR2_FREE_FIRSTDB(mp);
1460 * If it's ifbno we already looked at it.
1462 if (fbno == ifbno)
1463 fbno++;
1465 * If it's off the end we're done.
1467 if (fbno >= lastfbno)
1468 break;
1470 * Read the block. There can be holes in the
1471 * freespace blocks, so this might not succeed.
1472 * This should be really rare, so there's no reason
1473 * to avoid it.
1475 if ((error = xfs_da_read_buf(tp, dp,
1476 XFS_DIR2_DB_TO_DA(mp, fbno), -2, &fbp,
1477 XFS_DATA_FORK))) {
1478 return error;
1480 if (unlikely(fbp == NULL)) {
1481 continue;
1483 free = fbp->data;
1484 ASSERT(INT_GET(free->hdr.magic, ARCH_CONVERT) == XFS_DIR2_FREE_MAGIC);
1485 findex = 0;
1488 * Look at the current free entry. Is it good enough?
1490 if (INT_GET(free->bests[findex], ARCH_CONVERT) != NULLDATAOFF &&
1491 INT_GET(free->bests[findex], ARCH_CONVERT) >= length)
1492 dbno = INT_GET(free->hdr.firstdb, ARCH_CONVERT) + findex;
1493 else {
1495 * Are we done with the freeblock?
1497 if (++findex == INT_GET(free->hdr.nvalid, ARCH_CONVERT)) {
1499 * Drop the block.
1501 xfs_da_brelse(tp, fbp);
1502 fbp = NULL;
1503 if (fblk && fblk->bp)
1504 fblk->bp = NULL;
1509 * If we don't have a data block, we need to allocate one and make
1510 * the freespace entries refer to it.
1512 if (unlikely(dbno == -1)) {
1514 * Not allowed to allocate, return failure.
1516 if (args->justcheck || args->total == 0) {
1518 * Drop the freespace buffer unless it came from our
1519 * caller.
1521 if ((fblk == NULL || fblk->bp == NULL) && fbp != NULL)
1522 xfs_da_buf_done(fbp);
1523 return XFS_ERROR(ENOSPC);
1526 * Allocate and initialize the new data block.
1528 if (unlikely((error = xfs_dir2_grow_inode(args,
1529 XFS_DIR2_DATA_SPACE,
1530 &dbno)) ||
1531 (error = xfs_dir2_data_init(args, dbno, &dbp)))) {
1533 * Drop the freespace buffer unless it came from our
1534 * caller.
1536 if ((fblk == NULL || fblk->bp == NULL) && fbp != NULL)
1537 xfs_da_buf_done(fbp);
1538 return error;
1541 * If (somehow) we have a freespace block, get rid of it.
1543 if (fbp)
1544 xfs_da_brelse(tp, fbp);
1545 if (fblk && fblk->bp)
1546 fblk->bp = NULL;
1549 * Get the freespace block corresponding to the data block
1550 * that was just allocated.
1552 fbno = XFS_DIR2_DB_TO_FDB(mp, dbno);
1553 if (unlikely(error = xfs_da_read_buf(tp, dp,
1554 XFS_DIR2_DB_TO_DA(mp, fbno), -2, &fbp,
1555 XFS_DATA_FORK))) {
1556 xfs_da_buf_done(dbp);
1557 return error;
1560 * If there wasn't a freespace block, the read will
1561 * return a NULL fbp. Allocate and initialize a new one.
1563 if( fbp == NULL ) {
1564 if ((error = xfs_dir2_grow_inode(args, XFS_DIR2_FREE_SPACE,
1565 &fbno))) {
1566 return error;
1569 if (unlikely(XFS_DIR2_DB_TO_FDB(mp, dbno) != fbno)) {
1570 cmn_err(CE_ALERT,
1571 "xfs_dir2_node_addname_int: dir ino "
1572 "%llu needed freesp block %lld for\n"
1573 " data block %lld, got %lld\n"
1574 " ifbno %llu lastfbno %d\n",
1575 (unsigned long long)dp->i_ino,
1576 (long long)XFS_DIR2_DB_TO_FDB(mp, dbno),
1577 (long long)dbno, (long long)fbno,
1578 (unsigned long long)ifbno, lastfbno);
1579 if (fblk) {
1580 cmn_err(CE_ALERT,
1581 " fblk 0x%p blkno %llu "
1582 "index %d magic 0x%x\n",
1583 fblk,
1584 (unsigned long long)fblk->blkno,
1585 fblk->index,
1586 fblk->magic);
1587 } else {
1588 cmn_err(CE_ALERT,
1589 " ... fblk is NULL\n");
1591 XFS_ERROR_REPORT("xfs_dir2_node_addname_int",
1592 XFS_ERRLEVEL_LOW, mp);
1593 return XFS_ERROR(EFSCORRUPTED);
1597 * Get a buffer for the new block.
1599 if ((error = xfs_da_get_buf(tp, dp,
1600 XFS_DIR2_DB_TO_DA(mp, fbno),
1601 -1, &fbp, XFS_DATA_FORK))) {
1602 return error;
1604 ASSERT(fbp != NULL);
1607 * Initialize the new block to be empty, and remember
1608 * its first slot as our empty slot.
1610 free = fbp->data;
1611 INT_SET(free->hdr.magic, ARCH_CONVERT, XFS_DIR2_FREE_MAGIC);
1612 INT_SET(free->hdr.firstdb, ARCH_CONVERT,
1613 (fbno - XFS_DIR2_FREE_FIRSTDB(mp)) *
1614 XFS_DIR2_MAX_FREE_BESTS(mp));
1615 free->hdr.nvalid = 0;
1616 free->hdr.nused = 0;
1617 } else {
1618 free = fbp->data;
1619 ASSERT(INT_GET(free->hdr.magic, ARCH_CONVERT) == XFS_DIR2_FREE_MAGIC);
1623 * Set the freespace block index from the data block number.
1625 findex = XFS_DIR2_DB_TO_FDINDEX(mp, dbno);
1627 * If it's after the end of the current entries in the
1628 * freespace block, extend that table.
1630 if (findex >= INT_GET(free->hdr.nvalid, ARCH_CONVERT)) {
1631 ASSERT(findex < XFS_DIR2_MAX_FREE_BESTS(mp));
1632 INT_SET(free->hdr.nvalid, ARCH_CONVERT, findex + 1);
1634 * Tag new entry so nused will go up.
1636 INT_SET(free->bests[findex], ARCH_CONVERT, NULLDATAOFF);
1639 * If this entry was for an empty data block
1640 * (this should always be true) then update the header.
1642 if (INT_GET(free->bests[findex], ARCH_CONVERT) == NULLDATAOFF) {
1643 INT_MOD(free->hdr.nused, ARCH_CONVERT, +1);
1644 xfs_dir2_free_log_header(tp, fbp);
1647 * Update the real value in the table.
1648 * We haven't allocated the data entry yet so this will
1649 * change again.
1651 data = dbp->data;
1652 INT_COPY(free->bests[findex], data->hdr.bestfree[0].length, ARCH_CONVERT);
1653 logfree = 1;
1656 * We had a data block so we don't have to make a new one.
1658 else {
1660 * If just checking, we succeeded.
1662 if (args->justcheck) {
1663 if ((fblk == NULL || fblk->bp == NULL) && fbp != NULL)
1664 xfs_da_buf_done(fbp);
1665 return 0;
1668 * Read the data block in.
1670 if (unlikely(
1671 error = xfs_da_read_buf(tp, dp, XFS_DIR2_DB_TO_DA(mp, dbno),
1672 -1, &dbp, XFS_DATA_FORK))) {
1673 if ((fblk == NULL || fblk->bp == NULL) && fbp != NULL)
1674 xfs_da_buf_done(fbp);
1675 return error;
1677 data = dbp->data;
1678 logfree = 0;
1680 ASSERT(INT_GET(data->hdr.bestfree[0].length, ARCH_CONVERT) >= length);
1682 * Point to the existing unused space.
1684 dup = (xfs_dir2_data_unused_t *)
1685 ((char *)data + INT_GET(data->hdr.bestfree[0].offset, ARCH_CONVERT));
1686 needscan = needlog = 0;
1688 * Mark the first part of the unused space, inuse for us.
1690 xfs_dir2_data_use_free(tp, dbp, dup,
1691 (xfs_dir2_data_aoff_t)((char *)dup - (char *)data), length,
1692 &needlog, &needscan);
1694 * Fill in the new entry and log it.
1696 dep = (xfs_dir2_data_entry_t *)dup;
1697 INT_SET(dep->inumber, ARCH_CONVERT, args->inumber);
1698 dep->namelen = args->namelen;
1699 memcpy(dep->name, args->name, dep->namelen);
1700 tagp = XFS_DIR2_DATA_ENTRY_TAG_P(dep);
1701 INT_SET(*tagp, ARCH_CONVERT, (xfs_dir2_data_off_t)((char *)dep - (char *)data));
1702 xfs_dir2_data_log_entry(tp, dbp, dep);
1704 * Rescan the block for bestfree if needed.
1706 if (needscan)
1707 xfs_dir2_data_freescan(mp, data, &needlog, NULL);
1709 * Log the data block header if needed.
1711 if (needlog)
1712 xfs_dir2_data_log_header(tp, dbp);
1714 * If the freespace entry is now wrong, update it.
1716 if (INT_GET(free->bests[findex], ARCH_CONVERT) != INT_GET(data->hdr.bestfree[0].length, ARCH_CONVERT)) {
1717 INT_COPY(free->bests[findex], data->hdr.bestfree[0].length, ARCH_CONVERT);
1718 logfree = 1;
1721 * Log the freespace entry if needed.
1723 if (logfree)
1724 xfs_dir2_free_log_bests(tp, fbp, findex, findex);
1726 * If the caller didn't hand us the freespace block, drop it.
1728 if ((fblk == NULL || fblk->bp == NULL) && fbp != NULL)
1729 xfs_da_buf_done(fbp);
1731 * Return the data block and offset in args, then drop the data block.
1733 args->blkno = (xfs_dablk_t)dbno;
1734 args->index = INT_GET(*tagp, ARCH_CONVERT);
1735 xfs_da_buf_done(dbp);
1736 return 0;
1740 * Lookup an entry in a node-format directory.
1741 * All the real work happens in xfs_da_node_lookup_int.
1742 * The only real output is the inode number of the entry.
1744 int /* error */
1745 xfs_dir2_node_lookup(
1746 xfs_da_args_t *args) /* operation arguments */
1748 int error; /* error return value */
1749 int i; /* btree level */
1750 int rval; /* operation return value */
1751 xfs_da_state_t *state; /* btree cursor */
1753 xfs_dir2_trace_args("node_lookup", args);
1755 * Allocate and initialize the btree cursor.
1757 state = xfs_da_state_alloc();
1758 state->args = args;
1759 state->mp = args->dp->i_mount;
1760 state->blocksize = state->mp->m_dirblksize;
1761 state->node_ents = state->mp->m_dir_node_ents;
1763 * Fill in the path to the entry in the cursor.
1765 error = xfs_da_node_lookup_int(state, &rval);
1766 if (error)
1767 rval = error;
1769 * Release the btree blocks and leaf block.
1771 for (i = 0; i < state->path.active; i++) {
1772 xfs_da_brelse(args->trans, state->path.blk[i].bp);
1773 state->path.blk[i].bp = NULL;
1776 * Release the data block if we have it.
1778 if (state->extravalid && state->extrablk.bp) {
1779 xfs_da_brelse(args->trans, state->extrablk.bp);
1780 state->extrablk.bp = NULL;
1782 xfs_da_state_free(state);
1783 return rval;
1787 * Remove an entry from a node-format directory.
1789 int /* error */
1790 xfs_dir2_node_removename(
1791 xfs_da_args_t *args) /* operation arguments */
1793 xfs_da_state_blk_t *blk; /* leaf block */
1794 int error; /* error return value */
1795 int rval; /* operation return value */
1796 xfs_da_state_t *state; /* btree cursor */
1798 xfs_dir2_trace_args("node_removename", args);
1800 * Allocate and initialize the btree cursor.
1802 state = xfs_da_state_alloc();
1803 state->args = args;
1804 state->mp = args->dp->i_mount;
1805 state->blocksize = state->mp->m_dirblksize;
1806 state->node_ents = state->mp->m_dir_node_ents;
1808 * Look up the entry we're deleting, set up the cursor.
1810 error = xfs_da_node_lookup_int(state, &rval);
1811 if (error) {
1812 rval = error;
1815 * Didn't find it, upper layer screwed up.
1817 if (rval != EEXIST) {
1818 xfs_da_state_free(state);
1819 return rval;
1821 blk = &state->path.blk[state->path.active - 1];
1822 ASSERT(blk->magic == XFS_DIR2_LEAFN_MAGIC);
1823 ASSERT(state->extravalid);
1825 * Remove the leaf and data entries.
1826 * Extrablk refers to the data block.
1828 error = xfs_dir2_leafn_remove(args, blk->bp, blk->index,
1829 &state->extrablk, &rval);
1830 if (error) {
1831 return error;
1834 * Fix the hash values up the btree.
1836 xfs_da_fixhashpath(state, &state->path);
1838 * If we need to join leaf blocks, do it.
1840 if (rval && state->path.active > 1)
1841 error = xfs_da_join(state);
1843 * If no errors so far, try conversion to leaf format.
1845 if (!error)
1846 error = xfs_dir2_node_to_leaf(state);
1847 xfs_da_state_free(state);
1848 return error;
1852 * Replace an entry's inode number in a node-format directory.
1854 int /* error */
1855 xfs_dir2_node_replace(
1856 xfs_da_args_t *args) /* operation arguments */
1858 xfs_da_state_blk_t *blk; /* leaf block */
1859 xfs_dir2_data_t *data; /* data block structure */
1860 xfs_dir2_data_entry_t *dep; /* data entry changed */
1861 int error; /* error return value */
1862 int i; /* btree level */
1863 xfs_ino_t inum; /* new inode number */
1864 xfs_dir2_leaf_t *leaf; /* leaf structure */
1865 xfs_dir2_leaf_entry_t *lep; /* leaf entry being changed */
1866 int rval; /* internal return value */
1867 xfs_da_state_t *state; /* btree cursor */
1869 xfs_dir2_trace_args("node_replace", args);
1871 * Allocate and initialize the btree cursor.
1873 state = xfs_da_state_alloc();
1874 state->args = args;
1875 state->mp = args->dp->i_mount;
1876 state->blocksize = state->mp->m_dirblksize;
1877 state->node_ents = state->mp->m_dir_node_ents;
1878 inum = args->inumber;
1880 * Lookup the entry to change in the btree.
1882 error = xfs_da_node_lookup_int(state, &rval);
1883 if (error) {
1884 rval = error;
1887 * It should be found, since the vnodeops layer has looked it up
1888 * and locked it. But paranoia is good.
1890 if (rval == EEXIST) {
1892 * Find the leaf entry.
1894 blk = &state->path.blk[state->path.active - 1];
1895 ASSERT(blk->magic == XFS_DIR2_LEAFN_MAGIC);
1896 leaf = blk->bp->data;
1897 lep = &leaf->ents[blk->index];
1898 ASSERT(state->extravalid);
1900 * Point to the data entry.
1902 data = state->extrablk.bp->data;
1903 ASSERT(INT_GET(data->hdr.magic, ARCH_CONVERT) == XFS_DIR2_DATA_MAGIC);
1904 dep = (xfs_dir2_data_entry_t *)
1905 ((char *)data +
1906 XFS_DIR2_DATAPTR_TO_OFF(state->mp, INT_GET(lep->address, ARCH_CONVERT)));
1907 ASSERT(inum != INT_GET(dep->inumber, ARCH_CONVERT));
1909 * Fill in the new inode number and log the entry.
1911 INT_SET(dep->inumber, ARCH_CONVERT, inum);
1912 xfs_dir2_data_log_entry(args->trans, state->extrablk.bp, dep);
1913 rval = 0;
1916 * Didn't find it, and we're holding a data block. Drop it.
1918 else if (state->extravalid) {
1919 xfs_da_brelse(args->trans, state->extrablk.bp);
1920 state->extrablk.bp = NULL;
1923 * Release all the buffers in the cursor.
1925 for (i = 0; i < state->path.active; i++) {
1926 xfs_da_brelse(args->trans, state->path.blk[i].bp);
1927 state->path.blk[i].bp = NULL;
1929 xfs_da_state_free(state);
1930 return rval;
1934 * Trim off a trailing empty freespace block.
1935 * Return (in rvalp) 1 if we did it, 0 if not.
1937 int /* error */
1938 xfs_dir2_node_trim_free(
1939 xfs_da_args_t *args, /* operation arguments */
1940 xfs_fileoff_t fo, /* free block number */
1941 int *rvalp) /* out: did something */
1943 xfs_dabuf_t *bp; /* freespace buffer */
1944 xfs_inode_t *dp; /* incore directory inode */
1945 int error; /* error return code */
1946 xfs_dir2_free_t *free; /* freespace structure */
1947 xfs_mount_t *mp; /* filesystem mount point */
1948 xfs_trans_t *tp; /* transaction pointer */
1950 dp = args->dp;
1951 mp = dp->i_mount;
1952 tp = args->trans;
1954 * Read the freespace block.
1956 if (unlikely(error = xfs_da_read_buf(tp, dp, (xfs_dablk_t)fo, -2, &bp,
1957 XFS_DATA_FORK))) {
1958 return error;
1962 * There can be holes in freespace. If fo is a hole, there's
1963 * nothing to do.
1965 if (bp == NULL) {
1966 return 0;
1968 free = bp->data;
1969 ASSERT(INT_GET(free->hdr.magic, ARCH_CONVERT) == XFS_DIR2_FREE_MAGIC);
1971 * If there are used entries, there's nothing to do.
1973 if (INT_GET(free->hdr.nused, ARCH_CONVERT) > 0) {
1974 xfs_da_brelse(tp, bp);
1975 *rvalp = 0;
1976 return 0;
1979 * Blow the block away.
1981 if ((error =
1982 xfs_dir2_shrink_inode(args, XFS_DIR2_DA_TO_DB(mp, (xfs_dablk_t)fo),
1983 bp))) {
1985 * Can't fail with ENOSPC since that only happens with no
1986 * space reservation, when breaking up an extent into two
1987 * pieces. This is the last block of an extent.
1989 ASSERT(error != ENOSPC);
1990 xfs_da_brelse(tp, bp);
1991 return error;
1994 * Return that we succeeded.
1996 *rvalp = 1;
1997 return 0;