2 * Copyright (c) 2000-2004 Silicon Graphics, Inc. All Rights Reserved.
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of version 2 of the GNU General Public License as
6 * published by the Free Software Foundation.
8 * This program is distributed in the hope that it would be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12 * Further, this software is distributed without any warranty that it is
13 * free of the rightful claim of any third person regarding infringement
14 * or the like. Any license provided herein, whether implied or
15 * otherwise, applies only to this software file. Patent licenses, if
16 * any, provided herein do not apply to combinations of this program with
17 * other software, or any other product whatsoever.
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write the Free Software Foundation, Inc., 59
21 * Temple Place - Suite 330, Boston MA 02111-1307, USA.
23 * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
24 * Mountain View, CA 94043, or:
28 * For further information regarding this notice, see:
30 * http://oss.sgi.com/projects/GenInfo/SGIGPLNoticeExplan/
34 #include "xfs_types.h"
37 #include "xfs_trans.h"
41 #include "xfs_dmapi.h"
42 #include "xfs_mount.h"
43 #include "xfs_da_btree.h"
44 #include "xfs_bmap_btree.h"
45 #include "xfs_dir_sf.h"
46 #include "xfs_dir2_sf.h"
47 #include "xfs_attr_sf.h"
48 #include "xfs_dinode.h"
49 #include "xfs_inode.h"
51 #include "xfs_dir2_data.h"
52 #include "xfs_dir2_leaf.h"
53 #include "xfs_dir2_block.h"
54 #include "xfs_dir2_node.h"
55 #include "xfs_dir2_trace.h"
56 #include "xfs_error.h"
59 * Function declarations.
61 static void xfs_dir2_free_log_header(xfs_trans_t *tp, xfs_dabuf_t *bp);
62 static int xfs_dir2_leafn_add(xfs_dabuf_t *bp, xfs_da_args_t *args, int index);
64 static void xfs_dir2_leafn_check(xfs_inode_t *dp, xfs_dabuf_t *bp);
66 #define xfs_dir2_leafn_check(dp, bp)
68 static void xfs_dir2_leafn_moveents(xfs_da_args_t *args, xfs_dabuf_t *bp_s,
69 int start_s, xfs_dabuf_t *bp_d, int start_d,
71 static void xfs_dir2_leafn_rebalance(xfs_da_state_t *state,
72 xfs_da_state_blk_t *blk1,
73 xfs_da_state_blk_t *blk2);
74 static int xfs_dir2_leafn_remove(xfs_da_args_t *args, xfs_dabuf_t *bp,
75 int index, xfs_da_state_blk_t *dblk,
77 static int xfs_dir2_node_addname_int(xfs_da_args_t *args,
78 xfs_da_state_blk_t *fblk);
81 * Log entries from a freespace block.
84 xfs_dir2_free_log_bests(
85 xfs_trans_t *tp, /* transaction pointer */
86 xfs_dabuf_t *bp, /* freespace buffer */
87 int first, /* first entry to log */
88 int last) /* last entry to log */
90 xfs_dir2_free_t *free; /* freespace structure */
93 ASSERT(INT_GET(free->hdr.magic, ARCH_CONVERT) == XFS_DIR2_FREE_MAGIC);
94 xfs_da_log_buf(tp, bp,
95 (uint)((char *)&free->bests[first] - (char *)free),
96 (uint)((char *)&free->bests[last] - (char *)free +
97 sizeof(free->bests[0]) - 1));
101 * Log header from a freespace block.
104 xfs_dir2_free_log_header(
105 xfs_trans_t *tp, /* transaction pointer */
106 xfs_dabuf_t *bp) /* freespace buffer */
108 xfs_dir2_free_t *free; /* freespace structure */
111 ASSERT(INT_GET(free->hdr.magic, ARCH_CONVERT) == XFS_DIR2_FREE_MAGIC);
112 xfs_da_log_buf(tp, bp, (uint)((char *)&free->hdr - (char *)free),
113 (uint)(sizeof(xfs_dir2_free_hdr_t) - 1));
117 * Convert a leaf-format directory to a node-format directory.
118 * We need to change the magic number of the leaf block, and copy
119 * the freespace table out of the leaf block into its own block.
122 xfs_dir2_leaf_to_node(
123 xfs_da_args_t *args, /* operation arguments */
124 xfs_dabuf_t *lbp) /* leaf buffer */
126 xfs_inode_t *dp; /* incore directory inode */
127 int error; /* error return value */
128 xfs_dabuf_t *fbp; /* freespace buffer */
129 xfs_dir2_db_t fdb; /* freespace block number */
130 xfs_dir2_free_t *free; /* freespace structure */
131 xfs_dir2_data_off_t *from; /* pointer to freespace entry */
132 int i; /* leaf freespace index */
133 xfs_dir2_leaf_t *leaf; /* leaf structure */
134 xfs_dir2_leaf_tail_t *ltp; /* leaf tail structure */
135 xfs_mount_t *mp; /* filesystem mount point */
136 int n; /* count of live freespc ents */
137 xfs_dir2_data_off_t off; /* freespace entry value */
138 xfs_dir2_data_off_t *to; /* pointer to freespace entry */
139 xfs_trans_t *tp; /* transaction pointer */
141 xfs_dir2_trace_args_b("leaf_to_node", args, lbp);
146 * Add a freespace block to the directory.
148 if ((error = xfs_dir2_grow_inode(args, XFS_DIR2_FREE_SPACE, &fdb))) {
151 ASSERT(fdb == XFS_DIR2_FREE_FIRSTDB(mp));
153 * Get the buffer for the new freespace block.
155 if ((error = xfs_da_get_buf(tp, dp, XFS_DIR2_DB_TO_DA(mp, fdb), -1, &fbp,
162 ltp = XFS_DIR2_LEAF_TAIL_P(mp, leaf);
164 * Initialize the freespace block header.
166 INT_SET(free->hdr.magic, ARCH_CONVERT, XFS_DIR2_FREE_MAGIC);
167 free->hdr.firstdb = 0;
168 ASSERT(INT_GET(ltp->bestcount, ARCH_CONVERT) <= (uint)dp->i_d.di_size / mp->m_dirblksize);
169 INT_COPY(free->hdr.nvalid, ltp->bestcount, ARCH_CONVERT);
171 * Copy freespace entries from the leaf block to the new block.
172 * Count active entries.
174 for (i = n = 0, from = XFS_DIR2_LEAF_BESTS_P(ltp), to = free->bests;
175 i < INT_GET(ltp->bestcount, ARCH_CONVERT); i++, from++, to++) {
176 if ((off = INT_GET(*from, ARCH_CONVERT)) != NULLDATAOFF)
178 INT_SET(*to, ARCH_CONVERT, off);
180 INT_SET(free->hdr.nused, ARCH_CONVERT, n);
181 INT_SET(leaf->hdr.info.magic, ARCH_CONVERT, XFS_DIR2_LEAFN_MAGIC);
185 xfs_dir2_leaf_log_header(tp, lbp);
186 xfs_dir2_free_log_header(tp, fbp);
187 xfs_dir2_free_log_bests(tp, fbp, 0, INT_GET(free->hdr.nvalid, ARCH_CONVERT) - 1);
188 xfs_da_buf_done(fbp);
189 xfs_dir2_leafn_check(dp, lbp);
194 * Add a leaf entry to a leaf block in a node-form directory.
195 * The other work necessary is done from the caller.
197 static int /* error */
199 xfs_dabuf_t *bp, /* leaf buffer */
200 xfs_da_args_t *args, /* operation arguments */
201 int index) /* insertion pt for new entry */
203 int compact; /* compacting stale leaves */
204 xfs_inode_t *dp; /* incore directory inode */
205 int highstale; /* next stale entry */
206 xfs_dir2_leaf_t *leaf; /* leaf structure */
207 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
208 int lfloghigh; /* high leaf entry logging */
209 int lfloglow; /* low leaf entry logging */
210 int lowstale; /* previous stale entry */
211 xfs_mount_t *mp; /* filesystem mount point */
212 xfs_trans_t *tp; /* transaction pointer */
214 xfs_dir2_trace_args_sb("leafn_add", args, index, bp);
221 * Quick check just to make sure we are not going to index
222 * into other peoples memory
225 return XFS_ERROR(EFSCORRUPTED);
228 * If there are already the maximum number of leaf entries in
229 * the block, if there are no stale entries it won't fit.
230 * Caller will do a split. If there are stale entries we'll do
234 if (INT_GET(leaf->hdr.count, ARCH_CONVERT) == XFS_DIR2_MAX_LEAF_ENTS(mp)) {
235 if (!leaf->hdr.stale)
236 return XFS_ERROR(ENOSPC);
237 compact = INT_GET(leaf->hdr.stale, ARCH_CONVERT) > 1;
240 ASSERT(index == 0 || INT_GET(leaf->ents[index - 1].hashval, ARCH_CONVERT) <= args->hashval);
241 ASSERT(index == INT_GET(leaf->hdr.count, ARCH_CONVERT) ||
242 INT_GET(leaf->ents[index].hashval, ARCH_CONVERT) >= args->hashval);
248 * Compact out all but one stale leaf entry. Leaves behind
249 * the entry closest to index.
252 xfs_dir2_leaf_compact_x1(bp, &index, &lowstale, &highstale,
253 &lfloglow, &lfloghigh);
256 * Set impossible logging indices for this case.
258 else if (leaf->hdr.stale) {
259 lfloglow = INT_GET(leaf->hdr.count, ARCH_CONVERT);
263 * No stale entries, just insert a space for the new entry.
265 if (!leaf->hdr.stale) {
266 lep = &leaf->ents[index];
267 if (index < INT_GET(leaf->hdr.count, ARCH_CONVERT))
268 memmove(lep + 1, lep,
269 (INT_GET(leaf->hdr.count, ARCH_CONVERT) - index) * sizeof(*lep));
271 lfloghigh = INT_GET(leaf->hdr.count, ARCH_CONVERT);
272 INT_MOD(leaf->hdr.count, ARCH_CONVERT, +1);
275 * There are stale entries. We'll use one for the new entry.
279 * If we didn't do a compact then we need to figure out
280 * which stale entry will be used.
284 * Find first stale entry before our insertion point.
286 for (lowstale = index - 1;
288 INT_GET(leaf->ents[lowstale].address, ARCH_CONVERT) !=
289 XFS_DIR2_NULL_DATAPTR;
293 * Find next stale entry after insertion point.
294 * Stop looking if the answer would be worse than
295 * lowstale already found.
297 for (highstale = index;
298 highstale < INT_GET(leaf->hdr.count, ARCH_CONVERT) &&
299 INT_GET(leaf->ents[highstale].address, ARCH_CONVERT) !=
300 XFS_DIR2_NULL_DATAPTR &&
302 index - lowstale - 1 >= highstale - index);
307 * Using the low stale entry.
308 * Shift entries up toward the stale slot.
311 (highstale == INT_GET(leaf->hdr.count, ARCH_CONVERT) ||
312 index - lowstale - 1 < highstale - index)) {
313 ASSERT(INT_GET(leaf->ents[lowstale].address, ARCH_CONVERT) ==
314 XFS_DIR2_NULL_DATAPTR);
315 ASSERT(index - lowstale - 1 >= 0);
316 if (index - lowstale - 1 > 0)
317 memmove(&leaf->ents[lowstale],
318 &leaf->ents[lowstale + 1],
319 (index - lowstale - 1) * sizeof(*lep));
320 lep = &leaf->ents[index - 1];
321 lfloglow = MIN(lowstale, lfloglow);
322 lfloghigh = MAX(index - 1, lfloghigh);
325 * Using the high stale entry.
326 * Shift entries down toward the stale slot.
329 ASSERT(INT_GET(leaf->ents[highstale].address, ARCH_CONVERT) ==
330 XFS_DIR2_NULL_DATAPTR);
331 ASSERT(highstale - index >= 0);
332 if (highstale - index > 0)
333 memmove(&leaf->ents[index + 1],
335 (highstale - index) * sizeof(*lep));
336 lep = &leaf->ents[index];
337 lfloglow = MIN(index, lfloglow);
338 lfloghigh = MAX(highstale, lfloghigh);
340 INT_MOD(leaf->hdr.stale, ARCH_CONVERT, -1);
343 * Insert the new entry, log everything.
345 INT_SET(lep->hashval, ARCH_CONVERT, args->hashval);
346 INT_SET(lep->address, ARCH_CONVERT, XFS_DIR2_DB_OFF_TO_DATAPTR(mp, args->blkno, args->index));
347 xfs_dir2_leaf_log_header(tp, bp);
348 xfs_dir2_leaf_log_ents(tp, bp, lfloglow, lfloghigh);
349 xfs_dir2_leafn_check(dp, bp);
355 * Check internal consistency of a leafn block.
358 xfs_dir2_leafn_check(
359 xfs_inode_t *dp, /* incore directory inode */
360 xfs_dabuf_t *bp) /* leaf buffer */
362 int i; /* leaf index */
363 xfs_dir2_leaf_t *leaf; /* leaf structure */
364 xfs_mount_t *mp; /* filesystem mount point */
365 int stale; /* count of stale leaves */
369 ASSERT(INT_GET(leaf->hdr.info.magic, ARCH_CONVERT) == XFS_DIR2_LEAFN_MAGIC);
370 ASSERT(INT_GET(leaf->hdr.count, ARCH_CONVERT) <= XFS_DIR2_MAX_LEAF_ENTS(mp));
371 for (i = stale = 0; i < INT_GET(leaf->hdr.count, ARCH_CONVERT); i++) {
372 if (i + 1 < INT_GET(leaf->hdr.count, ARCH_CONVERT)) {
373 ASSERT(INT_GET(leaf->ents[i].hashval, ARCH_CONVERT) <=
374 INT_GET(leaf->ents[i + 1].hashval, ARCH_CONVERT));
376 if (INT_GET(leaf->ents[i].address, ARCH_CONVERT) == XFS_DIR2_NULL_DATAPTR)
379 ASSERT(INT_GET(leaf->hdr.stale, ARCH_CONVERT) == stale);
384 * Return the last hash value in the leaf.
385 * Stale entries are ok.
387 xfs_dahash_t /* hash value */
388 xfs_dir2_leafn_lasthash(
389 xfs_dabuf_t *bp, /* leaf buffer */
390 int *count) /* count of entries in leaf */
392 xfs_dir2_leaf_t *leaf; /* leaf structure */
395 ASSERT(INT_GET(leaf->hdr.info.magic, ARCH_CONVERT) == XFS_DIR2_LEAFN_MAGIC);
397 *count = INT_GET(leaf->hdr.count, ARCH_CONVERT);
398 if (!leaf->hdr.count)
400 return INT_GET(leaf->ents[INT_GET(leaf->hdr.count, ARCH_CONVERT) - 1].hashval, ARCH_CONVERT);
404 * Look up a leaf entry in a node-format leaf block.
405 * If this is an addname then the extrablk in state is a freespace block,
406 * otherwise it's a data block.
409 xfs_dir2_leafn_lookup_int(
410 xfs_dabuf_t *bp, /* leaf buffer */
411 xfs_da_args_t *args, /* operation arguments */
412 int *indexp, /* out: leaf entry index */
413 xfs_da_state_t *state) /* state to fill in */
415 xfs_dabuf_t *curbp; /* current data/free buffer */
416 xfs_dir2_db_t curdb; /* current data block number */
417 xfs_dir2_db_t curfdb; /* current free block number */
418 xfs_dir2_data_entry_t *dep; /* data block entry */
419 xfs_inode_t *dp; /* incore directory inode */
420 int error; /* error return value */
421 int fi; /* free entry index */
422 xfs_dir2_free_t *free=NULL; /* free block structure */
423 int index; /* leaf entry index */
424 xfs_dir2_leaf_t *leaf; /* leaf structure */
425 int length=0; /* length of new data entry */
426 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
427 xfs_mount_t *mp; /* filesystem mount point */
428 xfs_dir2_db_t newdb; /* new data block number */
429 xfs_dir2_db_t newfdb; /* new free block number */
430 xfs_trans_t *tp; /* transaction pointer */
436 ASSERT(INT_GET(leaf->hdr.info.magic, ARCH_CONVERT) == XFS_DIR2_LEAFN_MAGIC);
438 ASSERT(INT_GET(leaf->hdr.count, ARCH_CONVERT) > 0);
440 xfs_dir2_leafn_check(dp, bp);
442 * Look up the hash value in the leaf entries.
444 index = xfs_dir2_leaf_search_hash(args, bp);
446 * Do we have a buffer coming in?
448 if (state->extravalid)
449 curbp = state->extrablk.bp;
453 * For addname, it's a free block buffer, get the block number.
456 curfdb = curbp ? state->extrablk.blkno : -1;
458 length = XFS_DIR2_DATA_ENTSIZE(args->namelen);
459 if ((free = (curbp ? curbp->data : NULL)))
460 ASSERT(INT_GET(free->hdr.magic, ARCH_CONVERT) == XFS_DIR2_FREE_MAGIC);
463 * For others, it's a data block buffer, get the block number.
467 curdb = curbp ? state->extrablk.blkno : -1;
470 * Loop over leaf entries with the right hash value.
472 for (lep = &leaf->ents[index];
473 index < INT_GET(leaf->hdr.count, ARCH_CONVERT) && INT_GET(lep->hashval, ARCH_CONVERT) == args->hashval;
476 * Skip stale leaf entries.
478 if (INT_GET(lep->address, ARCH_CONVERT) == XFS_DIR2_NULL_DATAPTR)
481 * Pull the data block number from the entry.
483 newdb = XFS_DIR2_DATAPTR_TO_DB(mp, INT_GET(lep->address, ARCH_CONVERT));
485 * For addname, we're looking for a place to put the new entry.
486 * We want to use a data block with an entry of equal
487 * hash value to ours if there is one with room.
491 * If this block isn't the data block we already have
492 * in hand, take a look at it.
494 if (newdb != curdb) {
497 * Convert the data block to the free block
498 * holding its freespace information.
500 newfdb = XFS_DIR2_DB_TO_FDB(mp, newdb);
502 * If it's not the one we have in hand,
505 if (newfdb != curfdb) {
507 * If we had one before, drop it.
510 xfs_da_brelse(tp, curbp);
512 * Read the free block.
514 if ((error = xfs_da_read_buf(tp, dp,
515 XFS_DIR2_DB_TO_DA(mp,
523 ASSERT(INT_GET(free->hdr.magic, ARCH_CONVERT) ==
524 XFS_DIR2_FREE_MAGIC);
525 ASSERT((INT_GET(free->hdr.firstdb, ARCH_CONVERT) %
526 XFS_DIR2_MAX_FREE_BESTS(mp)) ==
528 ASSERT(INT_GET(free->hdr.firstdb, ARCH_CONVERT) <= curdb);
530 INT_GET(free->hdr.firstdb, ARCH_CONVERT) +
531 INT_GET(free->hdr.nvalid, ARCH_CONVERT));
534 * Get the index for our entry.
536 fi = XFS_DIR2_DB_TO_FDINDEX(mp, curdb);
538 * If it has room, return it.
540 if (unlikely(INT_GET(free->bests[fi], ARCH_CONVERT) == NULLDATAOFF)) {
541 XFS_ERROR_REPORT("xfs_dir2_leafn_lookup_int",
542 XFS_ERRLEVEL_LOW, mp);
543 return XFS_ERROR(EFSCORRUPTED);
545 if (INT_GET(free->bests[fi], ARCH_CONVERT) >= length) {
547 state->extravalid = 1;
548 state->extrablk.bp = curbp;
549 state->extrablk.blkno = curfdb;
550 state->extrablk.index = fi;
551 state->extrablk.magic =
553 ASSERT(args->oknoent);
554 return XFS_ERROR(ENOENT);
559 * Not adding a new entry, so we really want to find
560 * the name given to us.
564 * If it's a different data block, go get it.
566 if (newdb != curdb) {
568 * If we had a block before, drop it.
571 xfs_da_brelse(tp, curbp);
573 * Read the data block.
576 xfs_da_read_buf(tp, dp,
577 XFS_DIR2_DB_TO_DA(mp, newdb), -1,
578 &curbp, XFS_DATA_FORK))) {
581 xfs_dir2_data_check(dp, curbp);
585 * Point to the data entry.
587 dep = (xfs_dir2_data_entry_t *)
588 ((char *)curbp->data +
589 XFS_DIR2_DATAPTR_TO_OFF(mp, INT_GET(lep->address, ARCH_CONVERT)));
591 * Compare the entry, return it if it matches.
593 if (dep->namelen == args->namelen &&
594 dep->name[0] == args->name[0] &&
595 memcmp(dep->name, args->name, args->namelen) == 0) {
596 args->inumber = INT_GET(dep->inumber, ARCH_CONVERT);
598 state->extravalid = 1;
599 state->extrablk.bp = curbp;
600 state->extrablk.blkno = curdb;
601 state->extrablk.index =
603 (char *)curbp->data);
604 state->extrablk.magic = XFS_DIR2_DATA_MAGIC;
605 return XFS_ERROR(EEXIST);
610 * Didn't find a match.
611 * If we are holding a buffer, give it back in case our caller
614 if ((state->extravalid = (curbp != NULL))) {
615 state->extrablk.bp = curbp;
616 state->extrablk.index = -1;
618 * For addname, giving back a free block.
621 state->extrablk.blkno = curfdb;
622 state->extrablk.magic = XFS_DIR2_FREE_MAGIC;
625 * For other callers, giving back a data block.
628 state->extrablk.blkno = curdb;
629 state->extrablk.magic = XFS_DIR2_DATA_MAGIC;
633 * Return the final index, that will be the insertion point.
636 ASSERT(index == INT_GET(leaf->hdr.count, ARCH_CONVERT) || args->oknoent);
637 return XFS_ERROR(ENOENT);
641 * Move count leaf entries from source to destination leaf.
642 * Log entries and headers. Stale entries are preserved.
645 xfs_dir2_leafn_moveents(
646 xfs_da_args_t *args, /* operation arguments */
647 xfs_dabuf_t *bp_s, /* source leaf buffer */
648 int start_s, /* source leaf index */
649 xfs_dabuf_t *bp_d, /* destination leaf buffer */
650 int start_d, /* destination leaf index */
651 int count) /* count of leaves to copy */
653 xfs_dir2_leaf_t *leaf_d; /* destination leaf structure */
654 xfs_dir2_leaf_t *leaf_s; /* source leaf structure */
655 int stale; /* count stale leaves copied */
656 xfs_trans_t *tp; /* transaction pointer */
658 xfs_dir2_trace_args_bibii("leafn_moveents", args, bp_s, start_s, bp_d,
661 * Silently return if nothing to do.
670 * If the destination index is not the end of the current
671 * destination leaf entries, open up a hole in the destination
672 * to hold the new entries.
674 if (start_d < INT_GET(leaf_d->hdr.count, ARCH_CONVERT)) {
675 memmove(&leaf_d->ents[start_d + count], &leaf_d->ents[start_d],
676 (INT_GET(leaf_d->hdr.count, ARCH_CONVERT) - start_d) *
677 sizeof(xfs_dir2_leaf_entry_t));
678 xfs_dir2_leaf_log_ents(tp, bp_d, start_d + count,
679 count + INT_GET(leaf_d->hdr.count, ARCH_CONVERT) - 1);
682 * If the source has stale leaves, count the ones in the copy range
683 * so we can update the header correctly.
685 if (leaf_s->hdr.stale) {
686 int i; /* temp leaf index */
688 for (i = start_s, stale = 0; i < start_s + count; i++) {
689 if (INT_GET(leaf_s->ents[i].address, ARCH_CONVERT) == XFS_DIR2_NULL_DATAPTR)
695 * Copy the leaf entries from source to destination.
697 memcpy(&leaf_d->ents[start_d], &leaf_s->ents[start_s],
698 count * sizeof(xfs_dir2_leaf_entry_t));
699 xfs_dir2_leaf_log_ents(tp, bp_d, start_d, start_d + count - 1);
701 * If there are source entries after the ones we copied,
702 * delete the ones we copied by sliding the next ones down.
704 if (start_s + count < INT_GET(leaf_s->hdr.count, ARCH_CONVERT)) {
705 memmove(&leaf_s->ents[start_s], &leaf_s->ents[start_s + count],
706 count * sizeof(xfs_dir2_leaf_entry_t));
707 xfs_dir2_leaf_log_ents(tp, bp_s, start_s, start_s + count - 1);
710 * Update the headers and log them.
712 INT_MOD(leaf_s->hdr.count, ARCH_CONVERT, -(count));
713 INT_MOD(leaf_s->hdr.stale, ARCH_CONVERT, -(stale));
714 INT_MOD(leaf_d->hdr.count, ARCH_CONVERT, count);
715 INT_MOD(leaf_d->hdr.stale, ARCH_CONVERT, stale);
716 xfs_dir2_leaf_log_header(tp, bp_s);
717 xfs_dir2_leaf_log_header(tp, bp_d);
718 xfs_dir2_leafn_check(args->dp, bp_s);
719 xfs_dir2_leafn_check(args->dp, bp_d);
723 * Determine the sort order of two leaf blocks.
724 * Returns 1 if both are valid and leaf2 should be before leaf1, else 0.
727 xfs_dir2_leafn_order(
728 xfs_dabuf_t *leaf1_bp, /* leaf1 buffer */
729 xfs_dabuf_t *leaf2_bp) /* leaf2 buffer */
731 xfs_dir2_leaf_t *leaf1; /* leaf1 structure */
732 xfs_dir2_leaf_t *leaf2; /* leaf2 structure */
734 leaf1 = leaf1_bp->data;
735 leaf2 = leaf2_bp->data;
736 ASSERT(INT_GET(leaf1->hdr.info.magic, ARCH_CONVERT) == XFS_DIR2_LEAFN_MAGIC);
737 ASSERT(INT_GET(leaf2->hdr.info.magic, ARCH_CONVERT) == XFS_DIR2_LEAFN_MAGIC);
738 if (INT_GET(leaf1->hdr.count, ARCH_CONVERT) > 0 &&
739 INT_GET(leaf2->hdr.count, ARCH_CONVERT) > 0 &&
740 (INT_GET(leaf2->ents[0].hashval, ARCH_CONVERT) < INT_GET(leaf1->ents[0].hashval, ARCH_CONVERT) ||
741 INT_GET(leaf2->ents[INT_GET(leaf2->hdr.count, ARCH_CONVERT) - 1].hashval, ARCH_CONVERT) <
742 INT_GET(leaf1->ents[INT_GET(leaf1->hdr.count, ARCH_CONVERT) - 1].hashval, ARCH_CONVERT)))
748 * Rebalance leaf entries between two leaf blocks.
749 * This is actually only called when the second block is new,
750 * though the code deals with the general case.
751 * A new entry will be inserted in one of the blocks, and that
752 * entry is taken into account when balancing.
755 xfs_dir2_leafn_rebalance(
756 xfs_da_state_t *state, /* btree cursor */
757 xfs_da_state_blk_t *blk1, /* first btree block */
758 xfs_da_state_blk_t *blk2) /* second btree block */
760 xfs_da_args_t *args; /* operation arguments */
761 int count; /* count (& direction) leaves */
762 int isleft; /* new goes in left leaf */
763 xfs_dir2_leaf_t *leaf1; /* first leaf structure */
764 xfs_dir2_leaf_t *leaf2; /* second leaf structure */
765 int mid; /* midpoint leaf index */
767 int oldstale; /* old count of stale leaves */
769 int oldsum; /* old total leaf count */
770 int swap; /* swapped leaf blocks */
774 * If the block order is wrong, swap the arguments.
776 if ((swap = xfs_dir2_leafn_order(blk1->bp, blk2->bp))) {
777 xfs_da_state_blk_t *tmp; /* temp for block swap */
783 leaf1 = blk1->bp->data;
784 leaf2 = blk2->bp->data;
785 oldsum = INT_GET(leaf1->hdr.count, ARCH_CONVERT) + INT_GET(leaf2->hdr.count, ARCH_CONVERT);
787 oldstale = INT_GET(leaf1->hdr.stale, ARCH_CONVERT) + INT_GET(leaf2->hdr.stale, ARCH_CONVERT);
791 * If the old leaf count was odd then the new one will be even,
792 * so we need to divide the new count evenly.
795 xfs_dahash_t midhash; /* middle entry hash value */
797 if (mid >= INT_GET(leaf1->hdr.count, ARCH_CONVERT))
798 midhash = INT_GET(leaf2->ents[mid - INT_GET(leaf1->hdr.count, ARCH_CONVERT)].hashval, ARCH_CONVERT);
800 midhash = INT_GET(leaf1->ents[mid].hashval, ARCH_CONVERT);
801 isleft = args->hashval <= midhash;
804 * If the old count is even then the new count is odd, so there's
805 * no preferred side for the new entry.
811 * Calculate moved entry count. Positive means left-to-right,
812 * negative means right-to-left. Then move the entries.
814 count = INT_GET(leaf1->hdr.count, ARCH_CONVERT) - mid + (isleft == 0);
816 xfs_dir2_leafn_moveents(args, blk1->bp,
817 INT_GET(leaf1->hdr.count, ARCH_CONVERT) - count, blk2->bp, 0, count);
819 xfs_dir2_leafn_moveents(args, blk2->bp, 0, blk1->bp,
820 INT_GET(leaf1->hdr.count, ARCH_CONVERT), count);
821 ASSERT(INT_GET(leaf1->hdr.count, ARCH_CONVERT) + INT_GET(leaf2->hdr.count, ARCH_CONVERT) == oldsum);
822 ASSERT(INT_GET(leaf1->hdr.stale, ARCH_CONVERT) + INT_GET(leaf2->hdr.stale, ARCH_CONVERT) == oldstale);
824 * Mark whether we're inserting into the old or new leaf.
826 if (INT_GET(leaf1->hdr.count, ARCH_CONVERT) < INT_GET(leaf2->hdr.count, ARCH_CONVERT))
827 state->inleaf = swap;
828 else if (INT_GET(leaf1->hdr.count, ARCH_CONVERT) > INT_GET(leaf2->hdr.count, ARCH_CONVERT))
829 state->inleaf = !swap;
832 swap ^ (blk1->index <= INT_GET(leaf1->hdr.count, ARCH_CONVERT));
834 * Adjust the expected index for insertion.
837 blk2->index = blk1->index - INT_GET(leaf1->hdr.count, ARCH_CONVERT);
840 * Finally sanity check just to make sure we are not returning a negative index
842 if(blk2->index < 0) {
846 "xfs_dir2_leafn_rebalance: picked the wrong leaf? reverting orignal leaf: "
853 * Remove an entry from a node directory.
854 * This removes the leaf entry and the data entry,
855 * and updates the free block if necessary.
857 static int /* error */
858 xfs_dir2_leafn_remove(
859 xfs_da_args_t *args, /* operation arguments */
860 xfs_dabuf_t *bp, /* leaf buffer */
861 int index, /* leaf entry index */
862 xfs_da_state_blk_t *dblk, /* data block */
863 int *rval) /* resulting block needs join */
865 xfs_dir2_data_t *data; /* data block structure */
866 xfs_dir2_db_t db; /* data block number */
867 xfs_dabuf_t *dbp; /* data block buffer */
868 xfs_dir2_data_entry_t *dep; /* data block entry */
869 xfs_inode_t *dp; /* incore directory inode */
870 xfs_dir2_leaf_t *leaf; /* leaf structure */
871 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
872 int longest; /* longest data free entry */
873 int off; /* data block entry offset */
874 xfs_mount_t *mp; /* filesystem mount point */
875 int needlog; /* need to log data header */
876 int needscan; /* need to rescan data frees */
877 xfs_trans_t *tp; /* transaction pointer */
879 xfs_dir2_trace_args_sb("leafn_remove", args, index, bp);
884 ASSERT(INT_GET(leaf->hdr.info.magic, ARCH_CONVERT) == XFS_DIR2_LEAFN_MAGIC);
886 * Point to the entry we're removing.
888 lep = &leaf->ents[index];
890 * Extract the data block and offset from the entry.
892 db = XFS_DIR2_DATAPTR_TO_DB(mp, INT_GET(lep->address, ARCH_CONVERT));
893 ASSERT(dblk->blkno == db);
894 off = XFS_DIR2_DATAPTR_TO_OFF(mp, INT_GET(lep->address, ARCH_CONVERT));
895 ASSERT(dblk->index == off);
897 * Kill the leaf entry by marking it stale.
898 * Log the leaf block changes.
900 INT_MOD(leaf->hdr.stale, ARCH_CONVERT, +1);
901 xfs_dir2_leaf_log_header(tp, bp);
902 INT_SET(lep->address, ARCH_CONVERT, XFS_DIR2_NULL_DATAPTR);
903 xfs_dir2_leaf_log_ents(tp, bp, index, index);
905 * Make the data entry free. Keep track of the longest freespace
906 * in the data block in case it changes.
910 dep = (xfs_dir2_data_entry_t *)((char *)data + off);
911 longest = INT_GET(data->hdr.bestfree[0].length, ARCH_CONVERT);
912 needlog = needscan = 0;
913 xfs_dir2_data_make_free(tp, dbp, off,
914 XFS_DIR2_DATA_ENTSIZE(dep->namelen), &needlog, &needscan);
916 * Rescan the data block freespaces for bestfree.
917 * Log the data block header if needed.
920 xfs_dir2_data_freescan(mp, data, &needlog, NULL);
922 xfs_dir2_data_log_header(tp, dbp);
923 xfs_dir2_data_check(dp, dbp);
925 * If the longest data block freespace changes, need to update
926 * the corresponding freeblock entry.
928 if (longest < INT_GET(data->hdr.bestfree[0].length, ARCH_CONVERT)) {
929 int error; /* error return value */
930 xfs_dabuf_t *fbp; /* freeblock buffer */
931 xfs_dir2_db_t fdb; /* freeblock block number */
932 int findex; /* index in freeblock entries */
933 xfs_dir2_free_t *free; /* freeblock structure */
934 int logfree; /* need to log free entry */
937 * Convert the data block number to a free block,
938 * read in the free block.
940 fdb = XFS_DIR2_DB_TO_FDB(mp, db);
941 if ((error = xfs_da_read_buf(tp, dp, XFS_DIR2_DB_TO_DA(mp, fdb),
942 -1, &fbp, XFS_DATA_FORK))) {
946 ASSERT(INT_GET(free->hdr.magic, ARCH_CONVERT) == XFS_DIR2_FREE_MAGIC);
947 ASSERT(INT_GET(free->hdr.firstdb, ARCH_CONVERT) ==
948 XFS_DIR2_MAX_FREE_BESTS(mp) *
949 (fdb - XFS_DIR2_FREE_FIRSTDB(mp)));
951 * Calculate which entry we need to fix.
953 findex = XFS_DIR2_DB_TO_FDINDEX(mp, db);
954 longest = INT_GET(data->hdr.bestfree[0].length, ARCH_CONVERT);
956 * If the data block is now empty we can get rid of it
959 if (longest == mp->m_dirblksize - (uint)sizeof(data->hdr)) {
961 * Try to punch out the data block.
963 error = xfs_dir2_shrink_inode(args, db, dbp);
969 * We can get ENOSPC if there's no space reservation.
970 * In this case just drop the buffer and some one else
971 * will eventually get rid of the empty block.
973 else if (error == ENOSPC && args->total == 0)
974 xfs_da_buf_done(dbp);
979 * If we got rid of the data block, we can eliminate that entry
984 * One less used entry in the free table.
986 INT_MOD(free->hdr.nused, ARCH_CONVERT, -1);
987 xfs_dir2_free_log_header(tp, fbp);
989 * If this was the last entry in the table, we can
990 * trim the table size back. There might be other
991 * entries at the end referring to non-existent
992 * data blocks, get those too.
994 if (findex == INT_GET(free->hdr.nvalid, ARCH_CONVERT) - 1) {
995 int i; /* free entry index */
998 i >= 0 && INT_GET(free->bests[i], ARCH_CONVERT) == NULLDATAOFF;
1001 INT_SET(free->hdr.nvalid, ARCH_CONVERT, i + 1);
1005 * Not the last entry, just punch it out.
1008 INT_SET(free->bests[findex], ARCH_CONVERT, NULLDATAOFF);
1012 * If there are no useful entries left in the block,
1013 * get rid of the block if we can.
1015 if (!free->hdr.nused) {
1016 error = xfs_dir2_shrink_inode(args, fdb, fbp);
1020 } else if (error != ENOSPC || args->total != 0)
1023 * It's possible to get ENOSPC if there is no
1024 * space reservation. In this case some one
1025 * else will eventually get rid of this block.
1030 * Data block is not empty, just set the free entry to
1034 INT_SET(free->bests[findex], ARCH_CONVERT, longest);
1038 * Log the free entry that changed, unless we got rid of it.
1041 xfs_dir2_free_log_bests(tp, fbp, findex, findex);
1043 * Drop the buffer if we still have it.
1046 xfs_da_buf_done(fbp);
1048 xfs_dir2_leafn_check(dp, bp);
1050 * Return indication of whether this leaf block is emtpy enough
1051 * to justify trying to join it with a neighbor.
1054 ((uint)sizeof(leaf->hdr) +
1055 (uint)sizeof(leaf->ents[0]) *
1056 (INT_GET(leaf->hdr.count, ARCH_CONVERT) - INT_GET(leaf->hdr.stale, ARCH_CONVERT))) <
1062 * Split the leaf entries in the old block into old and new blocks.
1065 xfs_dir2_leafn_split(
1066 xfs_da_state_t *state, /* btree cursor */
1067 xfs_da_state_blk_t *oldblk, /* original block */
1068 xfs_da_state_blk_t *newblk) /* newly created block */
1070 xfs_da_args_t *args; /* operation arguments */
1071 xfs_dablk_t blkno; /* new leaf block number */
1072 int error; /* error return value */
1073 xfs_mount_t *mp; /* filesystem mount point */
1076 * Allocate space for a new leaf node.
1079 mp = args->dp->i_mount;
1080 ASSERT(args != NULL);
1081 ASSERT(oldblk->magic == XFS_DIR2_LEAFN_MAGIC);
1082 error = xfs_da_grow_inode(args, &blkno);
1087 * Initialize the new leaf block.
1089 error = xfs_dir2_leaf_init(args, XFS_DIR2_DA_TO_DB(mp, blkno),
1090 &newblk->bp, XFS_DIR2_LEAFN_MAGIC);
1094 newblk->blkno = blkno;
1095 newblk->magic = XFS_DIR2_LEAFN_MAGIC;
1097 * Rebalance the entries across the two leaves, link the new
1098 * block into the leaves.
1100 xfs_dir2_leafn_rebalance(state, oldblk, newblk);
1101 error = xfs_da_blk_link(state, oldblk, newblk);
1106 * Insert the new entry in the correct block.
1109 error = xfs_dir2_leafn_add(oldblk->bp, args, oldblk->index);
1111 error = xfs_dir2_leafn_add(newblk->bp, args, newblk->index);
1113 * Update last hashval in each block since we added the name.
1115 oldblk->hashval = xfs_dir2_leafn_lasthash(oldblk->bp, NULL);
1116 newblk->hashval = xfs_dir2_leafn_lasthash(newblk->bp, NULL);
1117 xfs_dir2_leafn_check(args->dp, oldblk->bp);
1118 xfs_dir2_leafn_check(args->dp, newblk->bp);
1123 * Check a leaf block and its neighbors to see if the block should be
1124 * collapsed into one or the other neighbor. Always keep the block
1125 * with the smaller block number.
1126 * If the current block is over 50% full, don't try to join it, return 0.
1127 * If the block is empty, fill in the state structure and return 2.
1128 * If it can be collapsed, fill in the state structure and return 1.
1129 * If nothing can be done, return 0.
1132 xfs_dir2_leafn_toosmall(
1133 xfs_da_state_t *state, /* btree cursor */
1134 int *action) /* resulting action to take */
1136 xfs_da_state_blk_t *blk; /* leaf block */
1137 xfs_dablk_t blkno; /* leaf block number */
1138 xfs_dabuf_t *bp; /* leaf buffer */
1139 int bytes; /* bytes in use */
1140 int count; /* leaf live entry count */
1141 int error; /* error return value */
1142 int forward; /* sibling block direction */
1143 int i; /* sibling counter */
1144 xfs_da_blkinfo_t *info; /* leaf block header */
1145 xfs_dir2_leaf_t *leaf; /* leaf structure */
1146 int rval; /* result from path_shift */
1149 * Check for the degenerate case of the block being over 50% full.
1150 * If so, it's not worth even looking to see if we might be able
1151 * to coalesce with a sibling.
1153 blk = &state->path.blk[state->path.active - 1];
1154 info = blk->bp->data;
1155 ASSERT(INT_GET(info->magic, ARCH_CONVERT) == XFS_DIR2_LEAFN_MAGIC);
1156 leaf = (xfs_dir2_leaf_t *)info;
1157 count = INT_GET(leaf->hdr.count, ARCH_CONVERT) - INT_GET(leaf->hdr.stale, ARCH_CONVERT);
1158 bytes = (uint)sizeof(leaf->hdr) + count * (uint)sizeof(leaf->ents[0]);
1159 if (bytes > (state->blocksize >> 1)) {
1161 * Blk over 50%, don't try to join.
1167 * Check for the degenerate case of the block being empty.
1168 * If the block is empty, we'll simply delete it, no need to
1169 * coalesce it with a sibling block. We choose (arbitrarily)
1170 * to merge with the forward block unless it is NULL.
1174 * Make altpath point to the block we want to keep and
1175 * path point to the block we want to drop (this one).
1177 forward = info->forw;
1178 memcpy(&state->altpath, &state->path, sizeof(state->path));
1179 error = xfs_da_path_shift(state, &state->altpath, forward, 0,
1183 *action = rval ? 2 : 0;
1187 * Examine each sibling block to see if we can coalesce with
1188 * at least 25% free space to spare. We need to figure out
1189 * whether to merge with the forward or the backward block.
1190 * We prefer coalescing with the lower numbered sibling so as
1191 * to shrink a directory over time.
1193 forward = INT_GET(info->forw, ARCH_CONVERT) < INT_GET(info->back, ARCH_CONVERT);
1194 for (i = 0, bp = NULL; i < 2; forward = !forward, i++) {
1195 blkno = forward ?INT_GET( info->forw, ARCH_CONVERT) : INT_GET(info->back, ARCH_CONVERT);
1199 * Read the sibling leaf block.
1202 xfs_da_read_buf(state->args->trans, state->args->dp, blkno,
1203 -1, &bp, XFS_DATA_FORK))) {
1208 * Count bytes in the two blocks combined.
1210 leaf = (xfs_dir2_leaf_t *)info;
1211 count = INT_GET(leaf->hdr.count, ARCH_CONVERT) - INT_GET(leaf->hdr.stale, ARCH_CONVERT);
1212 bytes = state->blocksize - (state->blocksize >> 2);
1214 ASSERT(INT_GET(leaf->hdr.info.magic, ARCH_CONVERT) == XFS_DIR2_LEAFN_MAGIC);
1215 count += INT_GET(leaf->hdr.count, ARCH_CONVERT) - INT_GET(leaf->hdr.stale, ARCH_CONVERT);
1216 bytes -= count * (uint)sizeof(leaf->ents[0]);
1218 * Fits with at least 25% to spare.
1222 xfs_da_brelse(state->args->trans, bp);
1225 * Didn't like either block, give up.
1232 * Done with the sibling leaf block here, drop the dabuf
1233 * so path_shift can get it.
1235 xfs_da_buf_done(bp);
1237 * Make altpath point to the block we want to keep (the lower
1238 * numbered block) and path point to the block we want to drop.
1240 memcpy(&state->altpath, &state->path, sizeof(state->path));
1241 if (blkno < blk->blkno)
1242 error = xfs_da_path_shift(state, &state->altpath, forward, 0,
1245 error = xfs_da_path_shift(state, &state->path, forward, 0,
1250 *action = rval ? 0 : 1;
1255 * Move all the leaf entries from drop_blk to save_blk.
1256 * This is done as part of a join operation.
1259 xfs_dir2_leafn_unbalance(
1260 xfs_da_state_t *state, /* cursor */
1261 xfs_da_state_blk_t *drop_blk, /* dead block */
1262 xfs_da_state_blk_t *save_blk) /* surviving block */
1264 xfs_da_args_t *args; /* operation arguments */
1265 xfs_dir2_leaf_t *drop_leaf; /* dead leaf structure */
1266 xfs_dir2_leaf_t *save_leaf; /* surviving leaf structure */
1269 ASSERT(drop_blk->magic == XFS_DIR2_LEAFN_MAGIC);
1270 ASSERT(save_blk->magic == XFS_DIR2_LEAFN_MAGIC);
1271 drop_leaf = drop_blk->bp->data;
1272 save_leaf = save_blk->bp->data;
1273 ASSERT(INT_GET(drop_leaf->hdr.info.magic, ARCH_CONVERT) == XFS_DIR2_LEAFN_MAGIC);
1274 ASSERT(INT_GET(save_leaf->hdr.info.magic, ARCH_CONVERT) == XFS_DIR2_LEAFN_MAGIC);
1276 * If there are any stale leaf entries, take this opportunity
1279 if (INT_GET(drop_leaf->hdr.stale, ARCH_CONVERT))
1280 xfs_dir2_leaf_compact(args, drop_blk->bp);
1281 if (INT_GET(save_leaf->hdr.stale, ARCH_CONVERT))
1282 xfs_dir2_leaf_compact(args, save_blk->bp);
1284 * Move the entries from drop to the appropriate end of save.
1286 drop_blk->hashval = INT_GET(drop_leaf->ents[INT_GET(drop_leaf->hdr.count, ARCH_CONVERT) - 1].hashval, ARCH_CONVERT);
1287 if (xfs_dir2_leafn_order(save_blk->bp, drop_blk->bp))
1288 xfs_dir2_leafn_moveents(args, drop_blk->bp, 0, save_blk->bp, 0,
1289 INT_GET(drop_leaf->hdr.count, ARCH_CONVERT));
1291 xfs_dir2_leafn_moveents(args, drop_blk->bp, 0, save_blk->bp,
1292 INT_GET(save_leaf->hdr.count, ARCH_CONVERT), INT_GET(drop_leaf->hdr.count, ARCH_CONVERT));
1293 save_blk->hashval = INT_GET(save_leaf->ents[INT_GET(save_leaf->hdr.count, ARCH_CONVERT) - 1].hashval, ARCH_CONVERT);
1294 xfs_dir2_leafn_check(args->dp, save_blk->bp);
1298 * Top-level node form directory addname routine.
1301 xfs_dir2_node_addname(
1302 xfs_da_args_t *args) /* operation arguments */
1304 xfs_da_state_blk_t *blk; /* leaf block for insert */
1305 int error; /* error return value */
1306 int rval; /* sub-return value */
1307 xfs_da_state_t *state; /* btree cursor */
1309 xfs_dir2_trace_args("node_addname", args);
1311 * Allocate and initialize the state (btree cursor).
1313 state = xfs_da_state_alloc();
1315 state->mp = args->dp->i_mount;
1316 state->blocksize = state->mp->m_dirblksize;
1317 state->node_ents = state->mp->m_dir_node_ents;
1319 * Look up the name. We're not supposed to find it, but
1320 * this gives us the insertion point.
1322 error = xfs_da_node_lookup_int(state, &rval);
1325 if (rval != ENOENT) {
1329 * Add the data entry to a data block.
1330 * Extravalid is set to a freeblock found by lookup.
1332 rval = xfs_dir2_node_addname_int(args,
1333 state->extravalid ? &state->extrablk : NULL);
1337 blk = &state->path.blk[state->path.active - 1];
1338 ASSERT(blk->magic == XFS_DIR2_LEAFN_MAGIC);
1340 * Add the new leaf entry.
1342 rval = xfs_dir2_leafn_add(blk->bp, args, blk->index);
1345 * It worked, fix the hash values up the btree.
1347 if (!args->justcheck)
1348 xfs_da_fixhashpath(state, &state->path);
1351 * It didn't work, we need to split the leaf block.
1353 if (args->total == 0) {
1354 ASSERT(rval == ENOSPC);
1358 * Split the leaf block and insert the new entry.
1360 rval = xfs_da_split(state);
1363 xfs_da_state_free(state);
1368 * Add the data entry for a node-format directory name addition.
1369 * The leaf entry is added in xfs_dir2_leafn_add.
1370 * We may enter with a freespace block that the lookup found.
1372 static int /* error */
1373 xfs_dir2_node_addname_int(
1374 xfs_da_args_t *args, /* operation arguments */
1375 xfs_da_state_blk_t *fblk) /* optional freespace block */
1377 xfs_dir2_data_t *data; /* data block structure */
1378 xfs_dir2_db_t dbno; /* data block number */
1379 xfs_dabuf_t *dbp; /* data block buffer */
1380 xfs_dir2_data_entry_t *dep; /* data entry pointer */
1381 xfs_inode_t *dp; /* incore directory inode */
1382 xfs_dir2_data_unused_t *dup; /* data unused entry pointer */
1383 int error; /* error return value */
1384 xfs_dir2_db_t fbno; /* freespace block number */
1385 xfs_dabuf_t *fbp; /* freespace buffer */
1386 int findex; /* freespace entry index */
1387 xfs_dir2_free_t *free=NULL; /* freespace block structure */
1388 xfs_dir2_db_t ifbno; /* initial freespace block no */
1389 xfs_dir2_db_t lastfbno=0; /* highest freespace block no */
1390 int length; /* length of the new entry */
1391 int logfree; /* need to log free entry */
1392 xfs_mount_t *mp; /* filesystem mount point */
1393 int needlog; /* need to log data header */
1394 int needscan; /* need to rescan data frees */
1395 xfs_dir2_data_off_t *tagp; /* data entry tag pointer */
1396 xfs_trans_t *tp; /* transaction pointer */
1401 length = XFS_DIR2_DATA_ENTSIZE(args->namelen);
1403 * If we came in with a freespace block that means that lookup
1404 * found an entry with our hash value. This is the freespace
1405 * block for that data entry.
1410 * Remember initial freespace block number.
1412 ifbno = fblk->blkno;
1414 ASSERT(INT_GET(free->hdr.magic, ARCH_CONVERT) == XFS_DIR2_FREE_MAGIC);
1415 findex = fblk->index;
1417 * This means the free entry showed that the data block had
1418 * space for our entry, so we remembered it.
1419 * Use that data block.
1422 ASSERT(findex < INT_GET(free->hdr.nvalid, ARCH_CONVERT));
1423 ASSERT(INT_GET(free->bests[findex], ARCH_CONVERT) != NULLDATAOFF);
1424 ASSERT(INT_GET(free->bests[findex], ARCH_CONVERT) >= length);
1425 dbno = INT_GET(free->hdr.firstdb, ARCH_CONVERT) + findex;
1428 * The data block looked at didn't have enough room.
1429 * We'll start at the beginning of the freespace entries.
1437 * Didn't come in with a freespace block, so don't have a data block.
1445 * If we don't have a data block yet, we're going to scan the
1446 * freespace blocks looking for one. Figure out what the
1447 * highest freespace block number is.
1450 xfs_fileoff_t fo; /* freespace block number */
1452 if ((error = xfs_bmap_last_offset(tp, dp, &fo, XFS_DATA_FORK)))
1454 lastfbno = XFS_DIR2_DA_TO_DB(mp, (xfs_dablk_t)fo);
1458 * While we haven't identified a data block, search the freeblock
1459 * data for a good data block. If we find a null freeblock entry,
1460 * indicating a hole in the data blocks, remember that.
1462 while (dbno == -1) {
1464 * If we don't have a freeblock in hand, get the next one.
1468 * Happens the first time through unless lookup gave
1469 * us a freespace block to start with.
1472 fbno = XFS_DIR2_FREE_FIRSTDB(mp);
1474 * If it's ifbno we already looked at it.
1479 * If it's off the end we're done.
1481 if (fbno >= lastfbno)
1484 * Read the block. There can be holes in the
1485 * freespace blocks, so this might not succeed.
1486 * This should be really rare, so there's no reason
1489 if ((error = xfs_da_read_buf(tp, dp,
1490 XFS_DIR2_DB_TO_DA(mp, fbno), -2, &fbp,
1494 if (unlikely(fbp == NULL)) {
1498 ASSERT(INT_GET(free->hdr.magic, ARCH_CONVERT) == XFS_DIR2_FREE_MAGIC);
1502 * Look at the current free entry. Is it good enough?
1504 if (INT_GET(free->bests[findex], ARCH_CONVERT) != NULLDATAOFF &&
1505 INT_GET(free->bests[findex], ARCH_CONVERT) >= length)
1506 dbno = INT_GET(free->hdr.firstdb, ARCH_CONVERT) + findex;
1509 * Are we done with the freeblock?
1511 if (++findex == INT_GET(free->hdr.nvalid, ARCH_CONVERT)) {
1515 xfs_da_brelse(tp, fbp);
1517 if (fblk && fblk->bp)
1523 * If we don't have a data block, we need to allocate one and make
1524 * the freespace entries refer to it.
1526 if (unlikely(dbno == -1)) {
1528 * Not allowed to allocate, return failure.
1530 if (args->justcheck || args->total == 0) {
1532 * Drop the freespace buffer unless it came from our
1535 if ((fblk == NULL || fblk->bp == NULL) && fbp != NULL)
1536 xfs_da_buf_done(fbp);
1537 return XFS_ERROR(ENOSPC);
1540 * Allocate and initialize the new data block.
1542 if (unlikely((error = xfs_dir2_grow_inode(args,
1543 XFS_DIR2_DATA_SPACE,
1545 (error = xfs_dir2_data_init(args, dbno, &dbp)))) {
1547 * Drop the freespace buffer unless it came from our
1550 if ((fblk == NULL || fblk->bp == NULL) && fbp != NULL)
1551 xfs_da_buf_done(fbp);
1555 * If (somehow) we have a freespace block, get rid of it.
1558 xfs_da_brelse(tp, fbp);
1559 if (fblk && fblk->bp)
1563 * Get the freespace block corresponding to the data block
1564 * that was just allocated.
1566 fbno = XFS_DIR2_DB_TO_FDB(mp, dbno);
1567 if (unlikely(error = xfs_da_read_buf(tp, dp,
1568 XFS_DIR2_DB_TO_DA(mp, fbno), -2, &fbp,
1570 xfs_da_buf_done(dbp);
1574 * If there wasn't a freespace block, the read will
1575 * return a NULL fbp. Allocate and initialize a new one.
1578 if ((error = xfs_dir2_grow_inode(args, XFS_DIR2_FREE_SPACE,
1583 if (unlikely(XFS_DIR2_DB_TO_FDB(mp, dbno) != fbno)) {
1585 "xfs_dir2_node_addname_int: dir ino "
1586 "%llu needed freesp block %lld for\n"
1587 " data block %lld, got %lld\n"
1588 " ifbno %llu lastfbno %d\n",
1589 (unsigned long long)dp->i_ino,
1590 (long long)XFS_DIR2_DB_TO_FDB(mp, dbno),
1591 (long long)dbno, (long long)fbno,
1592 (unsigned long long)ifbno, lastfbno);
1595 " fblk 0x%p blkno %llu "
1596 "index %d magic 0x%x\n",
1598 (unsigned long long)fblk->blkno,
1603 " ... fblk is NULL\n");
1605 XFS_ERROR_REPORT("xfs_dir2_node_addname_int",
1606 XFS_ERRLEVEL_LOW, mp);
1607 return XFS_ERROR(EFSCORRUPTED);
1611 * Get a buffer for the new block.
1613 if ((error = xfs_da_get_buf(tp, dp,
1614 XFS_DIR2_DB_TO_DA(mp, fbno),
1615 -1, &fbp, XFS_DATA_FORK))) {
1618 ASSERT(fbp != NULL);
1621 * Initialize the new block to be empty, and remember
1622 * its first slot as our empty slot.
1625 INT_SET(free->hdr.magic, ARCH_CONVERT, XFS_DIR2_FREE_MAGIC);
1626 INT_SET(free->hdr.firstdb, ARCH_CONVERT,
1627 (fbno - XFS_DIR2_FREE_FIRSTDB(mp)) *
1628 XFS_DIR2_MAX_FREE_BESTS(mp));
1629 free->hdr.nvalid = 0;
1630 free->hdr.nused = 0;
1633 ASSERT(INT_GET(free->hdr.magic, ARCH_CONVERT) == XFS_DIR2_FREE_MAGIC);
1637 * Set the freespace block index from the data block number.
1639 findex = XFS_DIR2_DB_TO_FDINDEX(mp, dbno);
1641 * If it's after the end of the current entries in the
1642 * freespace block, extend that table.
1644 if (findex >= INT_GET(free->hdr.nvalid, ARCH_CONVERT)) {
1645 ASSERT(findex < XFS_DIR2_MAX_FREE_BESTS(mp));
1646 INT_SET(free->hdr.nvalid, ARCH_CONVERT, findex + 1);
1648 * Tag new entry so nused will go up.
1650 INT_SET(free->bests[findex], ARCH_CONVERT, NULLDATAOFF);
1653 * If this entry was for an empty data block
1654 * (this should always be true) then update the header.
1656 if (INT_GET(free->bests[findex], ARCH_CONVERT) == NULLDATAOFF) {
1657 INT_MOD(free->hdr.nused, ARCH_CONVERT, +1);
1658 xfs_dir2_free_log_header(tp, fbp);
1661 * Update the real value in the table.
1662 * We haven't allocated the data entry yet so this will
1666 INT_COPY(free->bests[findex], data->hdr.bestfree[0].length, ARCH_CONVERT);
1670 * We had a data block so we don't have to make a new one.
1674 * If just checking, we succeeded.
1676 if (args->justcheck) {
1677 if ((fblk == NULL || fblk->bp == NULL) && fbp != NULL)
1678 xfs_da_buf_done(fbp);
1682 * Read the data block in.
1685 error = xfs_da_read_buf(tp, dp, XFS_DIR2_DB_TO_DA(mp, dbno),
1686 -1, &dbp, XFS_DATA_FORK))) {
1687 if ((fblk == NULL || fblk->bp == NULL) && fbp != NULL)
1688 xfs_da_buf_done(fbp);
1694 ASSERT(INT_GET(data->hdr.bestfree[0].length, ARCH_CONVERT) >= length);
1696 * Point to the existing unused space.
1698 dup = (xfs_dir2_data_unused_t *)
1699 ((char *)data + INT_GET(data->hdr.bestfree[0].offset, ARCH_CONVERT));
1700 needscan = needlog = 0;
1702 * Mark the first part of the unused space, inuse for us.
1704 xfs_dir2_data_use_free(tp, dbp, dup,
1705 (xfs_dir2_data_aoff_t)((char *)dup - (char *)data), length,
1706 &needlog, &needscan);
1708 * Fill in the new entry and log it.
1710 dep = (xfs_dir2_data_entry_t *)dup;
1711 INT_SET(dep->inumber, ARCH_CONVERT, args->inumber);
1712 dep->namelen = args->namelen;
1713 memcpy(dep->name, args->name, dep->namelen);
1714 tagp = XFS_DIR2_DATA_ENTRY_TAG_P(dep);
1715 INT_SET(*tagp, ARCH_CONVERT, (xfs_dir2_data_off_t)((char *)dep - (char *)data));
1716 xfs_dir2_data_log_entry(tp, dbp, dep);
1718 * Rescan the block for bestfree if needed.
1721 xfs_dir2_data_freescan(mp, data, &needlog, NULL);
1723 * Log the data block header if needed.
1726 xfs_dir2_data_log_header(tp, dbp);
1728 * If the freespace entry is now wrong, update it.
1730 if (INT_GET(free->bests[findex], ARCH_CONVERT) != INT_GET(data->hdr.bestfree[0].length, ARCH_CONVERT)) {
1731 INT_COPY(free->bests[findex], data->hdr.bestfree[0].length, ARCH_CONVERT);
1735 * Log the freespace entry if needed.
1738 xfs_dir2_free_log_bests(tp, fbp, findex, findex);
1740 * If the caller didn't hand us the freespace block, drop it.
1742 if ((fblk == NULL || fblk->bp == NULL) && fbp != NULL)
1743 xfs_da_buf_done(fbp);
1745 * Return the data block and offset in args, then drop the data block.
1747 args->blkno = (xfs_dablk_t)dbno;
1748 args->index = INT_GET(*tagp, ARCH_CONVERT);
1749 xfs_da_buf_done(dbp);
1754 * Lookup an entry in a node-format directory.
1755 * All the real work happens in xfs_da_node_lookup_int.
1756 * The only real output is the inode number of the entry.
1759 xfs_dir2_node_lookup(
1760 xfs_da_args_t *args) /* operation arguments */
1762 int error; /* error return value */
1763 int i; /* btree level */
1764 int rval; /* operation return value */
1765 xfs_da_state_t *state; /* btree cursor */
1767 xfs_dir2_trace_args("node_lookup", args);
1769 * Allocate and initialize the btree cursor.
1771 state = xfs_da_state_alloc();
1773 state->mp = args->dp->i_mount;
1774 state->blocksize = state->mp->m_dirblksize;
1775 state->node_ents = state->mp->m_dir_node_ents;
1777 * Fill in the path to the entry in the cursor.
1779 error = xfs_da_node_lookup_int(state, &rval);
1783 * Release the btree blocks and leaf block.
1785 for (i = 0; i < state->path.active; i++) {
1786 xfs_da_brelse(args->trans, state->path.blk[i].bp);
1787 state->path.blk[i].bp = NULL;
1790 * Release the data block if we have it.
1792 if (state->extravalid && state->extrablk.bp) {
1793 xfs_da_brelse(args->trans, state->extrablk.bp);
1794 state->extrablk.bp = NULL;
1796 xfs_da_state_free(state);
1801 * Remove an entry from a node-format directory.
1804 xfs_dir2_node_removename(
1805 xfs_da_args_t *args) /* operation arguments */
1807 xfs_da_state_blk_t *blk; /* leaf block */
1808 int error; /* error return value */
1809 int rval; /* operation return value */
1810 xfs_da_state_t *state; /* btree cursor */
1812 xfs_dir2_trace_args("node_removename", args);
1814 * Allocate and initialize the btree cursor.
1816 state = xfs_da_state_alloc();
1818 state->mp = args->dp->i_mount;
1819 state->blocksize = state->mp->m_dirblksize;
1820 state->node_ents = state->mp->m_dir_node_ents;
1822 * Look up the entry we're deleting, set up the cursor.
1824 error = xfs_da_node_lookup_int(state, &rval);
1829 * Didn't find it, upper layer screwed up.
1831 if (rval != EEXIST) {
1832 xfs_da_state_free(state);
1835 blk = &state->path.blk[state->path.active - 1];
1836 ASSERT(blk->magic == XFS_DIR2_LEAFN_MAGIC);
1837 ASSERT(state->extravalid);
1839 * Remove the leaf and data entries.
1840 * Extrablk refers to the data block.
1842 error = xfs_dir2_leafn_remove(args, blk->bp, blk->index,
1843 &state->extrablk, &rval);
1848 * Fix the hash values up the btree.
1850 xfs_da_fixhashpath(state, &state->path);
1852 * If we need to join leaf blocks, do it.
1854 if (rval && state->path.active > 1)
1855 error = xfs_da_join(state);
1857 * If no errors so far, try conversion to leaf format.
1860 error = xfs_dir2_node_to_leaf(state);
1861 xfs_da_state_free(state);
1866 * Replace an entry's inode number in a node-format directory.
1869 xfs_dir2_node_replace(
1870 xfs_da_args_t *args) /* operation arguments */
1872 xfs_da_state_blk_t *blk; /* leaf block */
1873 xfs_dir2_data_t *data; /* data block structure */
1874 xfs_dir2_data_entry_t *dep; /* data entry changed */
1875 int error; /* error return value */
1876 int i; /* btree level */
1877 xfs_ino_t inum; /* new inode number */
1878 xfs_dir2_leaf_t *leaf; /* leaf structure */
1879 xfs_dir2_leaf_entry_t *lep; /* leaf entry being changed */
1880 int rval; /* internal return value */
1881 xfs_da_state_t *state; /* btree cursor */
1883 xfs_dir2_trace_args("node_replace", 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;
1892 inum = args->inumber;
1894 * Lookup the entry to change in the btree.
1896 error = xfs_da_node_lookup_int(state, &rval);
1901 * It should be found, since the vnodeops layer has looked it up
1902 * and locked it. But paranoia is good.
1904 if (rval == EEXIST) {
1906 * Find the leaf entry.
1908 blk = &state->path.blk[state->path.active - 1];
1909 ASSERT(blk->magic == XFS_DIR2_LEAFN_MAGIC);
1910 leaf = blk->bp->data;
1911 lep = &leaf->ents[blk->index];
1912 ASSERT(state->extravalid);
1914 * Point to the data entry.
1916 data = state->extrablk.bp->data;
1917 ASSERT(INT_GET(data->hdr.magic, ARCH_CONVERT) == XFS_DIR2_DATA_MAGIC);
1918 dep = (xfs_dir2_data_entry_t *)
1920 XFS_DIR2_DATAPTR_TO_OFF(state->mp, INT_GET(lep->address, ARCH_CONVERT)));
1921 ASSERT(inum != INT_GET(dep->inumber, ARCH_CONVERT));
1923 * Fill in the new inode number and log the entry.
1925 INT_SET(dep->inumber, ARCH_CONVERT, inum);
1926 xfs_dir2_data_log_entry(args->trans, state->extrablk.bp, dep);
1930 * Didn't find it, and we're holding a data block. Drop it.
1932 else if (state->extravalid) {
1933 xfs_da_brelse(args->trans, state->extrablk.bp);
1934 state->extrablk.bp = NULL;
1937 * Release all the buffers in the cursor.
1939 for (i = 0; i < state->path.active; i++) {
1940 xfs_da_brelse(args->trans, state->path.blk[i].bp);
1941 state->path.blk[i].bp = NULL;
1943 xfs_da_state_free(state);
1948 * Trim off a trailing empty freespace block.
1949 * Return (in rvalp) 1 if we did it, 0 if not.
1952 xfs_dir2_node_trim_free(
1953 xfs_da_args_t *args, /* operation arguments */
1954 xfs_fileoff_t fo, /* free block number */
1955 int *rvalp) /* out: did something */
1957 xfs_dabuf_t *bp; /* freespace buffer */
1958 xfs_inode_t *dp; /* incore directory inode */
1959 int error; /* error return code */
1960 xfs_dir2_free_t *free; /* freespace structure */
1961 xfs_mount_t *mp; /* filesystem mount point */
1962 xfs_trans_t *tp; /* transaction pointer */
1968 * Read the freespace block.
1970 if (unlikely(error = xfs_da_read_buf(tp, dp, (xfs_dablk_t)fo, -2, &bp,
1976 * There can be holes in freespace. If fo is a hole, there's
1983 ASSERT(INT_GET(free->hdr.magic, ARCH_CONVERT) == XFS_DIR2_FREE_MAGIC);
1985 * If there are used entries, there's nothing to do.
1987 if (INT_GET(free->hdr.nused, ARCH_CONVERT) > 0) {
1988 xfs_da_brelse(tp, bp);
1993 * Blow the block away.
1996 xfs_dir2_shrink_inode(args, XFS_DIR2_DA_TO_DB(mp, (xfs_dablk_t)fo),
1999 * Can't fail with ENOSPC since that only happens with no
2000 * space reservation, when breaking up an extent into two
2001 * pieces. This is the last block of an extent.
2003 ASSERT(error != ENOSPC);
2004 xfs_da_brelse(tp, bp);
2008 * Return that we succeeded.