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/
35 * XFS directory implementation, version 2, node form files
36 * See data structures in xfs_dir2_node.h and xfs_da_btree.h.
41 #include "xfs_macros.h"
42 #include "xfs_types.h"
45 #include "xfs_trans.h"
49 #include "xfs_dmapi.h"
50 #include "xfs_mount.h"
51 #include "xfs_bmap_btree.h"
52 #include "xfs_attr_sf.h"
53 #include "xfs_dir_sf.h"
54 #include "xfs_dir2_sf.h"
55 #include "xfs_dinode.h"
56 #include "xfs_inode.h"
58 #include "xfs_da_btree.h"
59 #include "xfs_dir2_data.h"
60 #include "xfs_dir2_leaf.h"
61 #include "xfs_dir2_block.h"
62 #include "xfs_dir2_node.h"
63 #include "xfs_dir2_trace.h"
64 #include "xfs_error.h"
67 * Function declarations.
69 static void xfs_dir2_free_log_header(xfs_trans_t *tp, xfs_dabuf_t *bp);
70 static int xfs_dir2_leafn_add(xfs_dabuf_t *bp, xfs_da_args_t *args, int index);
72 static void xfs_dir2_leafn_check(xfs_inode_t *dp, xfs_dabuf_t *bp);
74 #define xfs_dir2_leafn_check(dp, bp)
76 static void xfs_dir2_leafn_moveents(xfs_da_args_t *args, xfs_dabuf_t *bp_s,
77 int start_s, xfs_dabuf_t *bp_d, int start_d,
79 static void xfs_dir2_leafn_rebalance(xfs_da_state_t *state,
80 xfs_da_state_blk_t *blk1,
81 xfs_da_state_blk_t *blk2);
82 static int xfs_dir2_leafn_remove(xfs_da_args_t *args, xfs_dabuf_t *bp,
83 int index, xfs_da_state_blk_t *dblk,
85 static int xfs_dir2_node_addname_int(xfs_da_args_t *args,
86 xfs_da_state_blk_t *fblk);
89 * Log entries from a freespace block.
92 xfs_dir2_free_log_bests(
93 xfs_trans_t *tp, /* transaction pointer */
94 xfs_dabuf_t *bp, /* freespace buffer */
95 int first, /* first entry to log */
96 int last) /* last entry to log */
98 xfs_dir2_free_t *free; /* freespace structure */
101 ASSERT(INT_GET(free->hdr.magic, ARCH_CONVERT) == XFS_DIR2_FREE_MAGIC);
102 xfs_da_log_buf(tp, bp,
103 (uint)((char *)&free->bests[first] - (char *)free),
104 (uint)((char *)&free->bests[last] - (char *)free +
105 sizeof(free->bests[0]) - 1));
109 * Log header from a freespace block.
112 xfs_dir2_free_log_header(
113 xfs_trans_t *tp, /* transaction pointer */
114 xfs_dabuf_t *bp) /* freespace buffer */
116 xfs_dir2_free_t *free; /* freespace structure */
119 ASSERT(INT_GET(free->hdr.magic, ARCH_CONVERT) == XFS_DIR2_FREE_MAGIC);
120 xfs_da_log_buf(tp, bp, (uint)((char *)&free->hdr - (char *)free),
121 (uint)(sizeof(xfs_dir2_free_hdr_t) - 1));
125 * Convert a leaf-format directory to a node-format directory.
126 * We need to change the magic number of the leaf block, and copy
127 * the freespace table out of the leaf block into its own block.
130 xfs_dir2_leaf_to_node(
131 xfs_da_args_t *args, /* operation arguments */
132 xfs_dabuf_t *lbp) /* leaf buffer */
134 xfs_inode_t *dp; /* incore directory inode */
135 int error; /* error return value */
136 xfs_dabuf_t *fbp; /* freespace buffer */
137 xfs_dir2_db_t fdb; /* freespace block number */
138 xfs_dir2_free_t *free; /* freespace structure */
139 xfs_dir2_data_off_t *from; /* pointer to freespace entry */
140 int i; /* leaf freespace index */
141 xfs_dir2_leaf_t *leaf; /* leaf structure */
142 xfs_dir2_leaf_tail_t *ltp; /* leaf tail structure */
143 xfs_mount_t *mp; /* filesystem mount point */
144 int n; /* count of live freespc ents */
145 xfs_dir2_data_off_t off; /* freespace entry value */
146 xfs_dir2_data_off_t *to; /* pointer to freespace entry */
147 xfs_trans_t *tp; /* transaction pointer */
149 xfs_dir2_trace_args_b("leaf_to_node", args, lbp);
154 * Add a freespace block to the directory.
156 if ((error = xfs_dir2_grow_inode(args, XFS_DIR2_FREE_SPACE, &fdb))) {
159 ASSERT(fdb == XFS_DIR2_FREE_FIRSTDB(mp));
161 * Get the buffer for the new freespace block.
163 if ((error = xfs_da_get_buf(tp, dp, XFS_DIR2_DB_TO_DA(mp, fdb), -1, &fbp,
170 ltp = XFS_DIR2_LEAF_TAIL_P(mp, leaf);
172 * Initialize the freespace block header.
174 INT_SET(free->hdr.magic, ARCH_CONVERT, XFS_DIR2_FREE_MAGIC);
175 free->hdr.firstdb = 0;
176 ASSERT(INT_GET(ltp->bestcount, ARCH_CONVERT) <= (uint)dp->i_d.di_size / mp->m_dirblksize);
177 INT_COPY(free->hdr.nvalid, ltp->bestcount, ARCH_CONVERT);
179 * Copy freespace entries from the leaf block to the new block.
180 * Count active entries.
182 for (i = n = 0, from = XFS_DIR2_LEAF_BESTS_P(ltp), to = free->bests;
183 i < INT_GET(ltp->bestcount, ARCH_CONVERT); i++, from++, to++) {
184 if ((off = INT_GET(*from, ARCH_CONVERT)) != NULLDATAOFF)
186 INT_SET(*to, ARCH_CONVERT, off);
188 INT_SET(free->hdr.nused, ARCH_CONVERT, n);
189 INT_SET(leaf->hdr.info.magic, ARCH_CONVERT, XFS_DIR2_LEAFN_MAGIC);
193 xfs_dir2_leaf_log_header(tp, lbp);
194 xfs_dir2_free_log_header(tp, fbp);
195 xfs_dir2_free_log_bests(tp, fbp, 0, INT_GET(free->hdr.nvalid, ARCH_CONVERT) - 1);
196 xfs_da_buf_done(fbp);
197 xfs_dir2_leafn_check(dp, lbp);
202 * Add a leaf entry to a leaf block in a node-form directory.
203 * The other work necessary is done from the caller.
205 static int /* error */
207 xfs_dabuf_t *bp, /* leaf buffer */
208 xfs_da_args_t *args, /* operation arguments */
209 int index) /* insertion pt for new entry */
211 int compact; /* compacting stale leaves */
212 xfs_inode_t *dp; /* incore directory inode */
213 int highstale; /* next stale entry */
214 xfs_dir2_leaf_t *leaf; /* leaf structure */
215 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
216 int lfloghigh; /* high leaf entry logging */
217 int lfloglow; /* low leaf entry logging */
218 int lowstale; /* previous stale entry */
219 xfs_mount_t *mp; /* filesystem mount point */
220 xfs_trans_t *tp; /* transaction pointer */
222 xfs_dir2_trace_args_sb("leafn_add", args, index, bp);
229 * Quick check just to make sure we are not going to index
230 * into other peoples memory
233 return XFS_ERROR(EFSCORRUPTED);
236 * If there are already the maximum number of leaf entries in
237 * the block, if there are no stale entries it won't fit.
238 * Caller will do a split. If there are stale entries we'll do
242 if (INT_GET(leaf->hdr.count, ARCH_CONVERT) == XFS_DIR2_MAX_LEAF_ENTS(mp)) {
243 if (!leaf->hdr.stale)
244 return XFS_ERROR(ENOSPC);
245 compact = INT_GET(leaf->hdr.stale, ARCH_CONVERT) > 1;
248 ASSERT(index == 0 || INT_GET(leaf->ents[index - 1].hashval, ARCH_CONVERT) <= args->hashval);
249 ASSERT(index == INT_GET(leaf->hdr.count, ARCH_CONVERT) ||
250 INT_GET(leaf->ents[index].hashval, ARCH_CONVERT) >= args->hashval);
256 * Compact out all but one stale leaf entry. Leaves behind
257 * the entry closest to index.
260 xfs_dir2_leaf_compact_x1(bp, &index, &lowstale, &highstale,
261 &lfloglow, &lfloghigh);
264 * Set impossible logging indices for this case.
266 else if (leaf->hdr.stale) {
267 lfloglow = INT_GET(leaf->hdr.count, ARCH_CONVERT);
271 * No stale entries, just insert a space for the new entry.
273 if (!leaf->hdr.stale) {
274 lep = &leaf->ents[index];
275 if (index < INT_GET(leaf->hdr.count, ARCH_CONVERT))
276 memmove(lep + 1, lep,
277 (INT_GET(leaf->hdr.count, ARCH_CONVERT) - index) * sizeof(*lep));
279 lfloghigh = INT_GET(leaf->hdr.count, ARCH_CONVERT);
280 INT_MOD(leaf->hdr.count, ARCH_CONVERT, +1);
283 * There are stale entries. We'll use one for the new entry.
287 * If we didn't do a compact then we need to figure out
288 * which stale entry will be used.
292 * Find first stale entry before our insertion point.
294 for (lowstale = index - 1;
296 INT_GET(leaf->ents[lowstale].address, ARCH_CONVERT) !=
297 XFS_DIR2_NULL_DATAPTR;
301 * Find next stale entry after insertion point.
302 * Stop looking if the answer would be worse than
303 * lowstale already found.
305 for (highstale = index;
306 highstale < INT_GET(leaf->hdr.count, ARCH_CONVERT) &&
307 INT_GET(leaf->ents[highstale].address, ARCH_CONVERT) !=
308 XFS_DIR2_NULL_DATAPTR &&
310 index - lowstale - 1 >= highstale - index);
315 * Using the low stale entry.
316 * Shift entries up toward the stale slot.
319 (highstale == INT_GET(leaf->hdr.count, ARCH_CONVERT) ||
320 index - lowstale - 1 < highstale - index)) {
321 ASSERT(INT_GET(leaf->ents[lowstale].address, ARCH_CONVERT) ==
322 XFS_DIR2_NULL_DATAPTR);
323 ASSERT(index - lowstale - 1 >= 0);
324 if (index - lowstale - 1 > 0)
325 memmove(&leaf->ents[lowstale],
326 &leaf->ents[lowstale + 1],
327 (index - lowstale - 1) * sizeof(*lep));
328 lep = &leaf->ents[index - 1];
329 lfloglow = MIN(lowstale, lfloglow);
330 lfloghigh = MAX(index - 1, lfloghigh);
333 * Using the high stale entry.
334 * Shift entries down toward the stale slot.
337 ASSERT(INT_GET(leaf->ents[highstale].address, ARCH_CONVERT) ==
338 XFS_DIR2_NULL_DATAPTR);
339 ASSERT(highstale - index >= 0);
340 if (highstale - index > 0)
341 memmove(&leaf->ents[index + 1],
343 (highstale - index) * sizeof(*lep));
344 lep = &leaf->ents[index];
345 lfloglow = MIN(index, lfloglow);
346 lfloghigh = MAX(highstale, lfloghigh);
348 INT_MOD(leaf->hdr.stale, ARCH_CONVERT, -1);
351 * Insert the new entry, log everything.
353 INT_SET(lep->hashval, ARCH_CONVERT, args->hashval);
354 INT_SET(lep->address, ARCH_CONVERT, XFS_DIR2_DB_OFF_TO_DATAPTR(mp, args->blkno, args->index));
355 xfs_dir2_leaf_log_header(tp, bp);
356 xfs_dir2_leaf_log_ents(tp, bp, lfloglow, lfloghigh);
357 xfs_dir2_leafn_check(dp, bp);
363 * Check internal consistency of a leafn block.
366 xfs_dir2_leafn_check(
367 xfs_inode_t *dp, /* incore directory inode */
368 xfs_dabuf_t *bp) /* leaf buffer */
370 int i; /* leaf index */
371 xfs_dir2_leaf_t *leaf; /* leaf structure */
372 xfs_mount_t *mp; /* filesystem mount point */
373 int stale; /* count of stale leaves */
377 ASSERT(INT_GET(leaf->hdr.info.magic, ARCH_CONVERT) == XFS_DIR2_LEAFN_MAGIC);
378 ASSERT(INT_GET(leaf->hdr.count, ARCH_CONVERT) <= XFS_DIR2_MAX_LEAF_ENTS(mp));
379 for (i = stale = 0; i < INT_GET(leaf->hdr.count, ARCH_CONVERT); i++) {
380 if (i + 1 < INT_GET(leaf->hdr.count, ARCH_CONVERT)) {
381 ASSERT(INT_GET(leaf->ents[i].hashval, ARCH_CONVERT) <=
382 INT_GET(leaf->ents[i + 1].hashval, ARCH_CONVERT));
384 if (INT_GET(leaf->ents[i].address, ARCH_CONVERT) == XFS_DIR2_NULL_DATAPTR)
387 ASSERT(INT_GET(leaf->hdr.stale, ARCH_CONVERT) == stale);
392 * Return the last hash value in the leaf.
393 * Stale entries are ok.
395 xfs_dahash_t /* hash value */
396 xfs_dir2_leafn_lasthash(
397 xfs_dabuf_t *bp, /* leaf buffer */
398 int *count) /* count of entries in leaf */
400 xfs_dir2_leaf_t *leaf; /* leaf structure */
403 ASSERT(INT_GET(leaf->hdr.info.magic, ARCH_CONVERT) == XFS_DIR2_LEAFN_MAGIC);
405 *count = INT_GET(leaf->hdr.count, ARCH_CONVERT);
406 if (!leaf->hdr.count)
408 return INT_GET(leaf->ents[INT_GET(leaf->hdr.count, ARCH_CONVERT) - 1].hashval, ARCH_CONVERT);
412 * Look up a leaf entry in a node-format leaf block.
413 * If this is an addname then the extrablk in state is a freespace block,
414 * otherwise it's a data block.
417 xfs_dir2_leafn_lookup_int(
418 xfs_dabuf_t *bp, /* leaf buffer */
419 xfs_da_args_t *args, /* operation arguments */
420 int *indexp, /* out: leaf entry index */
421 xfs_da_state_t *state) /* state to fill in */
423 xfs_dabuf_t *curbp; /* current data/free buffer */
424 xfs_dir2_db_t curdb; /* current data block number */
425 xfs_dir2_db_t curfdb; /* current free block number */
426 xfs_dir2_data_entry_t *dep; /* data block entry */
427 xfs_inode_t *dp; /* incore directory inode */
428 int error; /* error return value */
429 int fi; /* free entry index */
430 xfs_dir2_free_t *free=NULL; /* free block structure */
431 int index; /* leaf entry index */
432 xfs_dir2_leaf_t *leaf; /* leaf structure */
433 int length=0; /* length of new data entry */
434 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
435 xfs_mount_t *mp; /* filesystem mount point */
436 xfs_dir2_db_t newdb; /* new data block number */
437 xfs_dir2_db_t newfdb; /* new free block number */
438 xfs_trans_t *tp; /* transaction pointer */
444 ASSERT(INT_GET(leaf->hdr.info.magic, ARCH_CONVERT) == XFS_DIR2_LEAFN_MAGIC);
446 ASSERT(INT_GET(leaf->hdr.count, ARCH_CONVERT) > 0);
448 xfs_dir2_leafn_check(dp, bp);
450 * Look up the hash value in the leaf entries.
452 index = xfs_dir2_leaf_search_hash(args, bp);
454 * Do we have a buffer coming in?
456 if (state->extravalid)
457 curbp = state->extrablk.bp;
461 * For addname, it's a free block buffer, get the block number.
464 curfdb = curbp ? state->extrablk.blkno : -1;
466 length = XFS_DIR2_DATA_ENTSIZE(args->namelen);
467 if ((free = (curbp ? curbp->data : NULL)))
468 ASSERT(INT_GET(free->hdr.magic, ARCH_CONVERT) == XFS_DIR2_FREE_MAGIC);
471 * For others, it's a data block buffer, get the block number.
475 curdb = curbp ? state->extrablk.blkno : -1;
478 * Loop over leaf entries with the right hash value.
480 for (lep = &leaf->ents[index];
481 index < INT_GET(leaf->hdr.count, ARCH_CONVERT) && INT_GET(lep->hashval, ARCH_CONVERT) == args->hashval;
484 * Skip stale leaf entries.
486 if (INT_GET(lep->address, ARCH_CONVERT) == XFS_DIR2_NULL_DATAPTR)
489 * Pull the data block number from the entry.
491 newdb = XFS_DIR2_DATAPTR_TO_DB(mp, INT_GET(lep->address, ARCH_CONVERT));
493 * For addname, we're looking for a place to put the new entry.
494 * We want to use a data block with an entry of equal
495 * hash value to ours if there is one with room.
499 * If this block isn't the data block we already have
500 * in hand, take a look at it.
502 if (newdb != curdb) {
505 * Convert the data block to the free block
506 * holding its freespace information.
508 newfdb = XFS_DIR2_DB_TO_FDB(mp, newdb);
510 * If it's not the one we have in hand,
513 if (newfdb != curfdb) {
515 * If we had one before, drop it.
518 xfs_da_brelse(tp, curbp);
520 * Read the free block.
522 if ((error = xfs_da_read_buf(tp, dp,
523 XFS_DIR2_DB_TO_DA(mp,
531 ASSERT(INT_GET(free->hdr.magic, ARCH_CONVERT) ==
532 XFS_DIR2_FREE_MAGIC);
533 ASSERT((INT_GET(free->hdr.firstdb, ARCH_CONVERT) %
534 XFS_DIR2_MAX_FREE_BESTS(mp)) ==
536 ASSERT(INT_GET(free->hdr.firstdb, ARCH_CONVERT) <= curdb);
538 INT_GET(free->hdr.firstdb, ARCH_CONVERT) +
539 INT_GET(free->hdr.nvalid, ARCH_CONVERT));
542 * Get the index for our entry.
544 fi = XFS_DIR2_DB_TO_FDINDEX(mp, curdb);
546 * If it has room, return it.
548 if (unlikely(INT_GET(free->bests[fi], ARCH_CONVERT) == NULLDATAOFF)) {
549 XFS_ERROR_REPORT("xfs_dir2_leafn_lookup_int",
550 XFS_ERRLEVEL_LOW, mp);
551 return XFS_ERROR(EFSCORRUPTED);
553 if (INT_GET(free->bests[fi], ARCH_CONVERT) >= length) {
555 state->extravalid = 1;
556 state->extrablk.bp = curbp;
557 state->extrablk.blkno = curfdb;
558 state->extrablk.index = fi;
559 state->extrablk.magic =
561 ASSERT(args->oknoent);
562 return XFS_ERROR(ENOENT);
567 * Not adding a new entry, so we really want to find
568 * the name given to us.
572 * If it's a different data block, go get it.
574 if (newdb != curdb) {
576 * If we had a block before, drop it.
579 xfs_da_brelse(tp, curbp);
581 * Read the data block.
584 xfs_da_read_buf(tp, dp,
585 XFS_DIR2_DB_TO_DA(mp, newdb), -1,
586 &curbp, XFS_DATA_FORK))) {
589 xfs_dir2_data_check(dp, curbp);
593 * Point to the data entry.
595 dep = (xfs_dir2_data_entry_t *)
596 ((char *)curbp->data +
597 XFS_DIR2_DATAPTR_TO_OFF(mp, INT_GET(lep->address, ARCH_CONVERT)));
599 * Compare the entry, return it if it matches.
601 if (dep->namelen == args->namelen &&
602 dep->name[0] == args->name[0] &&
603 memcmp(dep->name, args->name, args->namelen) == 0) {
604 args->inumber = INT_GET(dep->inumber, ARCH_CONVERT);
606 state->extravalid = 1;
607 state->extrablk.bp = curbp;
608 state->extrablk.blkno = curdb;
609 state->extrablk.index =
611 (char *)curbp->data);
612 state->extrablk.magic = XFS_DIR2_DATA_MAGIC;
613 return XFS_ERROR(EEXIST);
618 * Didn't find a match.
619 * If we are holding a buffer, give it back in case our caller
622 if ((state->extravalid = (curbp != NULL))) {
623 state->extrablk.bp = curbp;
624 state->extrablk.index = -1;
626 * For addname, giving back a free block.
629 state->extrablk.blkno = curfdb;
630 state->extrablk.magic = XFS_DIR2_FREE_MAGIC;
633 * For other callers, giving back a data block.
636 state->extrablk.blkno = curdb;
637 state->extrablk.magic = XFS_DIR2_DATA_MAGIC;
641 * Return the final index, that will be the insertion point.
644 ASSERT(index == INT_GET(leaf->hdr.count, ARCH_CONVERT) || args->oknoent);
645 return XFS_ERROR(ENOENT);
649 * Move count leaf entries from source to destination leaf.
650 * Log entries and headers. Stale entries are preserved.
653 xfs_dir2_leafn_moveents(
654 xfs_da_args_t *args, /* operation arguments */
655 xfs_dabuf_t *bp_s, /* source leaf buffer */
656 int start_s, /* source leaf index */
657 xfs_dabuf_t *bp_d, /* destination leaf buffer */
658 int start_d, /* destination leaf index */
659 int count) /* count of leaves to copy */
661 xfs_dir2_leaf_t *leaf_d; /* destination leaf structure */
662 xfs_dir2_leaf_t *leaf_s; /* source leaf structure */
663 int stale; /* count stale leaves copied */
664 xfs_trans_t *tp; /* transaction pointer */
666 xfs_dir2_trace_args_bibii("leafn_moveents", args, bp_s, start_s, bp_d,
669 * Silently return if nothing to do.
678 * If the destination index is not the end of the current
679 * destination leaf entries, open up a hole in the destination
680 * to hold the new entries.
682 if (start_d < INT_GET(leaf_d->hdr.count, ARCH_CONVERT)) {
683 memmove(&leaf_d->ents[start_d + count], &leaf_d->ents[start_d],
684 (INT_GET(leaf_d->hdr.count, ARCH_CONVERT) - start_d) *
685 sizeof(xfs_dir2_leaf_entry_t));
686 xfs_dir2_leaf_log_ents(tp, bp_d, start_d + count,
687 count + INT_GET(leaf_d->hdr.count, ARCH_CONVERT) - 1);
690 * If the source has stale leaves, count the ones in the copy range
691 * so we can update the header correctly.
693 if (leaf_s->hdr.stale) {
694 int i; /* temp leaf index */
696 for (i = start_s, stale = 0; i < start_s + count; i++) {
697 if (INT_GET(leaf_s->ents[i].address, ARCH_CONVERT) == XFS_DIR2_NULL_DATAPTR)
703 * Copy the leaf entries from source to destination.
705 memcpy(&leaf_d->ents[start_d], &leaf_s->ents[start_s],
706 count * sizeof(xfs_dir2_leaf_entry_t));
707 xfs_dir2_leaf_log_ents(tp, bp_d, start_d, start_d + count - 1);
709 * If there are source entries after the ones we copied,
710 * delete the ones we copied by sliding the next ones down.
712 if (start_s + count < INT_GET(leaf_s->hdr.count, ARCH_CONVERT)) {
713 memmove(&leaf_s->ents[start_s], &leaf_s->ents[start_s + count],
714 count * sizeof(xfs_dir2_leaf_entry_t));
715 xfs_dir2_leaf_log_ents(tp, bp_s, start_s, start_s + count - 1);
718 * Update the headers and log them.
720 INT_MOD(leaf_s->hdr.count, ARCH_CONVERT, -(count));
721 INT_MOD(leaf_s->hdr.stale, ARCH_CONVERT, -(stale));
722 INT_MOD(leaf_d->hdr.count, ARCH_CONVERT, count);
723 INT_MOD(leaf_d->hdr.stale, ARCH_CONVERT, stale);
724 xfs_dir2_leaf_log_header(tp, bp_s);
725 xfs_dir2_leaf_log_header(tp, bp_d);
726 xfs_dir2_leafn_check(args->dp, bp_s);
727 xfs_dir2_leafn_check(args->dp, bp_d);
731 * Determine the sort order of two leaf blocks.
732 * Returns 1 if both are valid and leaf2 should be before leaf1, else 0.
735 xfs_dir2_leafn_order(
736 xfs_dabuf_t *leaf1_bp, /* leaf1 buffer */
737 xfs_dabuf_t *leaf2_bp) /* leaf2 buffer */
739 xfs_dir2_leaf_t *leaf1; /* leaf1 structure */
740 xfs_dir2_leaf_t *leaf2; /* leaf2 structure */
742 leaf1 = leaf1_bp->data;
743 leaf2 = leaf2_bp->data;
744 ASSERT(INT_GET(leaf1->hdr.info.magic, ARCH_CONVERT) == XFS_DIR2_LEAFN_MAGIC);
745 ASSERT(INT_GET(leaf2->hdr.info.magic, ARCH_CONVERT) == XFS_DIR2_LEAFN_MAGIC);
746 if (INT_GET(leaf1->hdr.count, ARCH_CONVERT) > 0 &&
747 INT_GET(leaf2->hdr.count, ARCH_CONVERT) > 0 &&
748 (INT_GET(leaf2->ents[0].hashval, ARCH_CONVERT) < INT_GET(leaf1->ents[0].hashval, ARCH_CONVERT) ||
749 INT_GET(leaf2->ents[INT_GET(leaf2->hdr.count, ARCH_CONVERT) - 1].hashval, ARCH_CONVERT) <
750 INT_GET(leaf1->ents[INT_GET(leaf1->hdr.count, ARCH_CONVERT) - 1].hashval, ARCH_CONVERT)))
756 * Rebalance leaf entries between two leaf blocks.
757 * This is actually only called when the second block is new,
758 * though the code deals with the general case.
759 * A new entry will be inserted in one of the blocks, and that
760 * entry is taken into account when balancing.
763 xfs_dir2_leafn_rebalance(
764 xfs_da_state_t *state, /* btree cursor */
765 xfs_da_state_blk_t *blk1, /* first btree block */
766 xfs_da_state_blk_t *blk2) /* second btree block */
768 xfs_da_args_t *args; /* operation arguments */
769 int count; /* count (& direction) leaves */
770 int isleft; /* new goes in left leaf */
771 xfs_dir2_leaf_t *leaf1; /* first leaf structure */
772 xfs_dir2_leaf_t *leaf2; /* second leaf structure */
773 int mid; /* midpoint leaf index */
775 int oldstale; /* old count of stale leaves */
777 int oldsum; /* old total leaf count */
778 int swap; /* swapped leaf blocks */
782 * If the block order is wrong, swap the arguments.
784 if ((swap = xfs_dir2_leafn_order(blk1->bp, blk2->bp))) {
785 xfs_da_state_blk_t *tmp; /* temp for block swap */
791 leaf1 = blk1->bp->data;
792 leaf2 = blk2->bp->data;
793 oldsum = INT_GET(leaf1->hdr.count, ARCH_CONVERT) + INT_GET(leaf2->hdr.count, ARCH_CONVERT);
795 oldstale = INT_GET(leaf1->hdr.stale, ARCH_CONVERT) + INT_GET(leaf2->hdr.stale, ARCH_CONVERT);
799 * If the old leaf count was odd then the new one will be even,
800 * so we need to divide the new count evenly.
803 xfs_dahash_t midhash; /* middle entry hash value */
805 if (mid >= INT_GET(leaf1->hdr.count, ARCH_CONVERT))
806 midhash = INT_GET(leaf2->ents[mid - INT_GET(leaf1->hdr.count, ARCH_CONVERT)].hashval, ARCH_CONVERT);
808 midhash = INT_GET(leaf1->ents[mid].hashval, ARCH_CONVERT);
809 isleft = args->hashval <= midhash;
812 * If the old count is even then the new count is odd, so there's
813 * no preferred side for the new entry.
819 * Calculate moved entry count. Positive means left-to-right,
820 * negative means right-to-left. Then move the entries.
822 count = INT_GET(leaf1->hdr.count, ARCH_CONVERT) - mid + (isleft == 0);
824 xfs_dir2_leafn_moveents(args, blk1->bp,
825 INT_GET(leaf1->hdr.count, ARCH_CONVERT) - count, blk2->bp, 0, count);
827 xfs_dir2_leafn_moveents(args, blk2->bp, 0, blk1->bp,
828 INT_GET(leaf1->hdr.count, ARCH_CONVERT), count);
829 ASSERT(INT_GET(leaf1->hdr.count, ARCH_CONVERT) + INT_GET(leaf2->hdr.count, ARCH_CONVERT) == oldsum);
830 ASSERT(INT_GET(leaf1->hdr.stale, ARCH_CONVERT) + INT_GET(leaf2->hdr.stale, ARCH_CONVERT) == oldstale);
832 * Mark whether we're inserting into the old or new leaf.
834 if (INT_GET(leaf1->hdr.count, ARCH_CONVERT) < INT_GET(leaf2->hdr.count, ARCH_CONVERT))
835 state->inleaf = swap;
836 else if (INT_GET(leaf1->hdr.count, ARCH_CONVERT) > INT_GET(leaf2->hdr.count, ARCH_CONVERT))
837 state->inleaf = !swap;
840 swap ^ (blk1->index <= INT_GET(leaf1->hdr.count, ARCH_CONVERT));
842 * Adjust the expected index for insertion.
845 blk2->index = blk1->index - INT_GET(leaf1->hdr.count, ARCH_CONVERT);
848 * Finally sanity check just to make sure we are not returning a negative index
850 if(blk2->index < 0) {
854 "xfs_dir2_leafn_rebalance: picked the wrong leaf? reverting orignal leaf: "
861 * Remove an entry from a node directory.
862 * This removes the leaf entry and the data entry,
863 * and updates the free block if necessary.
865 static int /* error */
866 xfs_dir2_leafn_remove(
867 xfs_da_args_t *args, /* operation arguments */
868 xfs_dabuf_t *bp, /* leaf buffer */
869 int index, /* leaf entry index */
870 xfs_da_state_blk_t *dblk, /* data block */
871 int *rval) /* resulting block needs join */
873 xfs_dir2_data_t *data; /* data block structure */
874 xfs_dir2_db_t db; /* data block number */
875 xfs_dabuf_t *dbp; /* data block buffer */
876 xfs_dir2_data_entry_t *dep; /* data block entry */
877 xfs_inode_t *dp; /* incore directory inode */
878 xfs_dir2_leaf_t *leaf; /* leaf structure */
879 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
880 int longest; /* longest data free entry */
881 int off; /* data block entry offset */
882 xfs_mount_t *mp; /* filesystem mount point */
883 int needlog; /* need to log data header */
884 int needscan; /* need to rescan data frees */
885 xfs_trans_t *tp; /* transaction pointer */
887 xfs_dir2_trace_args_sb("leafn_remove", args, index, bp);
892 ASSERT(INT_GET(leaf->hdr.info.magic, ARCH_CONVERT) == XFS_DIR2_LEAFN_MAGIC);
894 * Point to the entry we're removing.
896 lep = &leaf->ents[index];
898 * Extract the data block and offset from the entry.
900 db = XFS_DIR2_DATAPTR_TO_DB(mp, INT_GET(lep->address, ARCH_CONVERT));
901 ASSERT(dblk->blkno == db);
902 off = XFS_DIR2_DATAPTR_TO_OFF(mp, INT_GET(lep->address, ARCH_CONVERT));
903 ASSERT(dblk->index == off);
905 * Kill the leaf entry by marking it stale.
906 * Log the leaf block changes.
908 INT_MOD(leaf->hdr.stale, ARCH_CONVERT, +1);
909 xfs_dir2_leaf_log_header(tp, bp);
910 INT_SET(lep->address, ARCH_CONVERT, XFS_DIR2_NULL_DATAPTR);
911 xfs_dir2_leaf_log_ents(tp, bp, index, index);
913 * Make the data entry free. Keep track of the longest freespace
914 * in the data block in case it changes.
918 dep = (xfs_dir2_data_entry_t *)((char *)data + off);
919 longest = INT_GET(data->hdr.bestfree[0].length, ARCH_CONVERT);
920 needlog = needscan = 0;
921 xfs_dir2_data_make_free(tp, dbp, off,
922 XFS_DIR2_DATA_ENTSIZE(dep->namelen), &needlog, &needscan);
924 * Rescan the data block freespaces for bestfree.
925 * Log the data block header if needed.
928 xfs_dir2_data_freescan(mp, data, &needlog, NULL);
930 xfs_dir2_data_log_header(tp, dbp);
931 xfs_dir2_data_check(dp, dbp);
933 * If the longest data block freespace changes, need to update
934 * the corresponding freeblock entry.
936 if (longest < INT_GET(data->hdr.bestfree[0].length, ARCH_CONVERT)) {
937 int error; /* error return value */
938 xfs_dabuf_t *fbp; /* freeblock buffer */
939 xfs_dir2_db_t fdb; /* freeblock block number */
940 int findex; /* index in freeblock entries */
941 xfs_dir2_free_t *free; /* freeblock structure */
942 int logfree; /* need to log free entry */
945 * Convert the data block number to a free block,
946 * read in the free block.
948 fdb = XFS_DIR2_DB_TO_FDB(mp, db);
949 if ((error = xfs_da_read_buf(tp, dp, XFS_DIR2_DB_TO_DA(mp, fdb),
950 -1, &fbp, XFS_DATA_FORK))) {
954 ASSERT(INT_GET(free->hdr.magic, ARCH_CONVERT) == XFS_DIR2_FREE_MAGIC);
955 ASSERT(INT_GET(free->hdr.firstdb, ARCH_CONVERT) ==
956 XFS_DIR2_MAX_FREE_BESTS(mp) *
957 (fdb - XFS_DIR2_FREE_FIRSTDB(mp)));
959 * Calculate which entry we need to fix.
961 findex = XFS_DIR2_DB_TO_FDINDEX(mp, db);
962 longest = INT_GET(data->hdr.bestfree[0].length, ARCH_CONVERT);
964 * If the data block is now empty we can get rid of it
967 if (longest == mp->m_dirblksize - (uint)sizeof(data->hdr)) {
969 * Try to punch out the data block.
971 error = xfs_dir2_shrink_inode(args, db, dbp);
977 * We can get ENOSPC if there's no space reservation.
978 * In this case just drop the buffer and some one else
979 * will eventually get rid of the empty block.
981 else if (error == ENOSPC && args->total == 0)
982 xfs_da_buf_done(dbp);
987 * If we got rid of the data block, we can eliminate that entry
992 * One less used entry in the free table.
994 INT_MOD(free->hdr.nused, ARCH_CONVERT, -1);
995 xfs_dir2_free_log_header(tp, fbp);
997 * If this was the last entry in the table, we can
998 * trim the table size back. There might be other
999 * entries at the end referring to non-existent
1000 * data blocks, get those too.
1002 if (findex == INT_GET(free->hdr.nvalid, ARCH_CONVERT) - 1) {
1003 int i; /* free entry index */
1005 for (i = findex - 1;
1006 i >= 0 && INT_GET(free->bests[i], ARCH_CONVERT) == NULLDATAOFF;
1009 INT_SET(free->hdr.nvalid, ARCH_CONVERT, i + 1);
1013 * Not the last entry, just punch it out.
1016 INT_SET(free->bests[findex], ARCH_CONVERT, NULLDATAOFF);
1020 * If there are no useful entries left in the block,
1021 * get rid of the block if we can.
1023 if (!free->hdr.nused) {
1024 error = xfs_dir2_shrink_inode(args, fdb, fbp);
1028 } else if (error != ENOSPC || args->total != 0)
1031 * It's possible to get ENOSPC if there is no
1032 * space reservation. In this case some one
1033 * else will eventually get rid of this block.
1038 * Data block is not empty, just set the free entry to
1042 INT_SET(free->bests[findex], ARCH_CONVERT, longest);
1046 * Log the free entry that changed, unless we got rid of it.
1049 xfs_dir2_free_log_bests(tp, fbp, findex, findex);
1051 * Drop the buffer if we still have it.
1054 xfs_da_buf_done(fbp);
1056 xfs_dir2_leafn_check(dp, bp);
1058 * Return indication of whether this leaf block is emtpy enough
1059 * to justify trying to join it with a neighbor.
1062 ((uint)sizeof(leaf->hdr) +
1063 (uint)sizeof(leaf->ents[0]) *
1064 (INT_GET(leaf->hdr.count, ARCH_CONVERT) - INT_GET(leaf->hdr.stale, ARCH_CONVERT))) <
1070 * Split the leaf entries in the old block into old and new blocks.
1073 xfs_dir2_leafn_split(
1074 xfs_da_state_t *state, /* btree cursor */
1075 xfs_da_state_blk_t *oldblk, /* original block */
1076 xfs_da_state_blk_t *newblk) /* newly created block */
1078 xfs_da_args_t *args; /* operation arguments */
1079 xfs_dablk_t blkno; /* new leaf block number */
1080 int error; /* error return value */
1081 xfs_mount_t *mp; /* filesystem mount point */
1084 * Allocate space for a new leaf node.
1087 mp = args->dp->i_mount;
1088 ASSERT(args != NULL);
1089 ASSERT(oldblk->magic == XFS_DIR2_LEAFN_MAGIC);
1090 error = xfs_da_grow_inode(args, &blkno);
1095 * Initialize the new leaf block.
1097 error = xfs_dir2_leaf_init(args, XFS_DIR2_DA_TO_DB(mp, blkno),
1098 &newblk->bp, XFS_DIR2_LEAFN_MAGIC);
1102 newblk->blkno = blkno;
1103 newblk->magic = XFS_DIR2_LEAFN_MAGIC;
1105 * Rebalance the entries across the two leaves, link the new
1106 * block into the leaves.
1108 xfs_dir2_leafn_rebalance(state, oldblk, newblk);
1109 error = xfs_da_blk_link(state, oldblk, newblk);
1114 * Insert the new entry in the correct block.
1117 error = xfs_dir2_leafn_add(oldblk->bp, args, oldblk->index);
1119 error = xfs_dir2_leafn_add(newblk->bp, args, newblk->index);
1121 * Update last hashval in each block since we added the name.
1123 oldblk->hashval = xfs_dir2_leafn_lasthash(oldblk->bp, NULL);
1124 newblk->hashval = xfs_dir2_leafn_lasthash(newblk->bp, NULL);
1125 xfs_dir2_leafn_check(args->dp, oldblk->bp);
1126 xfs_dir2_leafn_check(args->dp, newblk->bp);
1131 * Check a leaf block and its neighbors to see if the block should be
1132 * collapsed into one or the other neighbor. Always keep the block
1133 * with the smaller block number.
1134 * If the current block is over 50% full, don't try to join it, return 0.
1135 * If the block is empty, fill in the state structure and return 2.
1136 * If it can be collapsed, fill in the state structure and return 1.
1137 * If nothing can be done, return 0.
1140 xfs_dir2_leafn_toosmall(
1141 xfs_da_state_t *state, /* btree cursor */
1142 int *action) /* resulting action to take */
1144 xfs_da_state_blk_t *blk; /* leaf block */
1145 xfs_dablk_t blkno; /* leaf block number */
1146 xfs_dabuf_t *bp; /* leaf buffer */
1147 int bytes; /* bytes in use */
1148 int count; /* leaf live entry count */
1149 int error; /* error return value */
1150 int forward; /* sibling block direction */
1151 int i; /* sibling counter */
1152 xfs_da_blkinfo_t *info; /* leaf block header */
1153 xfs_dir2_leaf_t *leaf; /* leaf structure */
1154 int rval; /* result from path_shift */
1157 * Check for the degenerate case of the block being over 50% full.
1158 * If so, it's not worth even looking to see if we might be able
1159 * to coalesce with a sibling.
1161 blk = &state->path.blk[state->path.active - 1];
1162 info = blk->bp->data;
1163 ASSERT(INT_GET(info->magic, ARCH_CONVERT) == XFS_DIR2_LEAFN_MAGIC);
1164 leaf = (xfs_dir2_leaf_t *)info;
1165 count = INT_GET(leaf->hdr.count, ARCH_CONVERT) - INT_GET(leaf->hdr.stale, ARCH_CONVERT);
1166 bytes = (uint)sizeof(leaf->hdr) + count * (uint)sizeof(leaf->ents[0]);
1167 if (bytes > (state->blocksize >> 1)) {
1169 * Blk over 50%, don't try to join.
1175 * Check for the degenerate case of the block being empty.
1176 * If the block is empty, we'll simply delete it, no need to
1177 * coalesce it with a sibling block. We choose (arbitrarily)
1178 * to merge with the forward block unless it is NULL.
1182 * Make altpath point to the block we want to keep and
1183 * path point to the block we want to drop (this one).
1185 forward = info->forw;
1186 memcpy(&state->altpath, &state->path, sizeof(state->path));
1187 error = xfs_da_path_shift(state, &state->altpath, forward, 0,
1191 *action = rval ? 2 : 0;
1195 * Examine each sibling block to see if we can coalesce with
1196 * at least 25% free space to spare. We need to figure out
1197 * whether to merge with the forward or the backward block.
1198 * We prefer coalescing with the lower numbered sibling so as
1199 * to shrink a directory over time.
1201 forward = INT_GET(info->forw, ARCH_CONVERT) < INT_GET(info->back, ARCH_CONVERT);
1202 for (i = 0, bp = NULL; i < 2; forward = !forward, i++) {
1203 blkno = forward ?INT_GET( info->forw, ARCH_CONVERT) : INT_GET(info->back, ARCH_CONVERT);
1207 * Read the sibling leaf block.
1210 xfs_da_read_buf(state->args->trans, state->args->dp, blkno,
1211 -1, &bp, XFS_DATA_FORK))) {
1216 * Count bytes in the two blocks combined.
1218 leaf = (xfs_dir2_leaf_t *)info;
1219 count = INT_GET(leaf->hdr.count, ARCH_CONVERT) - INT_GET(leaf->hdr.stale, ARCH_CONVERT);
1220 bytes = state->blocksize - (state->blocksize >> 2);
1222 ASSERT(INT_GET(leaf->hdr.info.magic, ARCH_CONVERT) == XFS_DIR2_LEAFN_MAGIC);
1223 count += INT_GET(leaf->hdr.count, ARCH_CONVERT) - INT_GET(leaf->hdr.stale, ARCH_CONVERT);
1224 bytes -= count * (uint)sizeof(leaf->ents[0]);
1226 * Fits with at least 25% to spare.
1230 xfs_da_brelse(state->args->trans, bp);
1233 * Didn't like either block, give up.
1240 * Done with the sibling leaf block here, drop the dabuf
1241 * so path_shift can get it.
1243 xfs_da_buf_done(bp);
1245 * Make altpath point to the block we want to keep (the lower
1246 * numbered block) and path point to the block we want to drop.
1248 memcpy(&state->altpath, &state->path, sizeof(state->path));
1249 if (blkno < blk->blkno)
1250 error = xfs_da_path_shift(state, &state->altpath, forward, 0,
1253 error = xfs_da_path_shift(state, &state->path, forward, 0,
1258 *action = rval ? 0 : 1;
1263 * Move all the leaf entries from drop_blk to save_blk.
1264 * This is done as part of a join operation.
1267 xfs_dir2_leafn_unbalance(
1268 xfs_da_state_t *state, /* cursor */
1269 xfs_da_state_blk_t *drop_blk, /* dead block */
1270 xfs_da_state_blk_t *save_blk) /* surviving block */
1272 xfs_da_args_t *args; /* operation arguments */
1273 xfs_dir2_leaf_t *drop_leaf; /* dead leaf structure */
1274 xfs_dir2_leaf_t *save_leaf; /* surviving leaf structure */
1277 ASSERT(drop_blk->magic == XFS_DIR2_LEAFN_MAGIC);
1278 ASSERT(save_blk->magic == XFS_DIR2_LEAFN_MAGIC);
1279 drop_leaf = drop_blk->bp->data;
1280 save_leaf = save_blk->bp->data;
1281 ASSERT(INT_GET(drop_leaf->hdr.info.magic, ARCH_CONVERT) == XFS_DIR2_LEAFN_MAGIC);
1282 ASSERT(INT_GET(save_leaf->hdr.info.magic, ARCH_CONVERT) == XFS_DIR2_LEAFN_MAGIC);
1284 * If there are any stale leaf entries, take this opportunity
1287 if (INT_GET(drop_leaf->hdr.stale, ARCH_CONVERT))
1288 xfs_dir2_leaf_compact(args, drop_blk->bp);
1289 if (INT_GET(save_leaf->hdr.stale, ARCH_CONVERT))
1290 xfs_dir2_leaf_compact(args, save_blk->bp);
1292 * Move the entries from drop to the appropriate end of save.
1294 drop_blk->hashval = INT_GET(drop_leaf->ents[INT_GET(drop_leaf->hdr.count, ARCH_CONVERT) - 1].hashval, ARCH_CONVERT);
1295 if (xfs_dir2_leafn_order(save_blk->bp, drop_blk->bp))
1296 xfs_dir2_leafn_moveents(args, drop_blk->bp, 0, save_blk->bp, 0,
1297 INT_GET(drop_leaf->hdr.count, ARCH_CONVERT));
1299 xfs_dir2_leafn_moveents(args, drop_blk->bp, 0, save_blk->bp,
1300 INT_GET(save_leaf->hdr.count, ARCH_CONVERT), INT_GET(drop_leaf->hdr.count, ARCH_CONVERT));
1301 save_blk->hashval = INT_GET(save_leaf->ents[INT_GET(save_leaf->hdr.count, ARCH_CONVERT) - 1].hashval, ARCH_CONVERT);
1302 xfs_dir2_leafn_check(args->dp, save_blk->bp);
1306 * Top-level node form directory addname routine.
1309 xfs_dir2_node_addname(
1310 xfs_da_args_t *args) /* operation arguments */
1312 xfs_da_state_blk_t *blk; /* leaf block for insert */
1313 int error; /* error return value */
1314 int rval; /* sub-return value */
1315 xfs_da_state_t *state; /* btree cursor */
1317 xfs_dir2_trace_args("node_addname", args);
1319 * Allocate and initialize the state (btree cursor).
1321 state = xfs_da_state_alloc();
1323 state->mp = args->dp->i_mount;
1324 state->blocksize = state->mp->m_dirblksize;
1325 state->node_ents = state->mp->m_dir_node_ents;
1327 * Look up the name. We're not supposed to find it, but
1328 * this gives us the insertion point.
1330 error = xfs_da_node_lookup_int(state, &rval);
1333 if (rval != ENOENT) {
1337 * Add the data entry to a data block.
1338 * Extravalid is set to a freeblock found by lookup.
1340 rval = xfs_dir2_node_addname_int(args,
1341 state->extravalid ? &state->extrablk : NULL);
1345 blk = &state->path.blk[state->path.active - 1];
1346 ASSERT(blk->magic == XFS_DIR2_LEAFN_MAGIC);
1348 * Add the new leaf entry.
1350 rval = xfs_dir2_leafn_add(blk->bp, args, blk->index);
1353 * It worked, fix the hash values up the btree.
1355 if (!args->justcheck)
1356 xfs_da_fixhashpath(state, &state->path);
1359 * It didn't work, we need to split the leaf block.
1361 if (args->total == 0) {
1362 ASSERT(rval == ENOSPC);
1366 * Split the leaf block and insert the new entry.
1368 rval = xfs_da_split(state);
1371 xfs_da_state_free(state);
1376 * Add the data entry for a node-format directory name addition.
1377 * The leaf entry is added in xfs_dir2_leafn_add.
1378 * We may enter with a freespace block that the lookup found.
1380 static int /* error */
1381 xfs_dir2_node_addname_int(
1382 xfs_da_args_t *args, /* operation arguments */
1383 xfs_da_state_blk_t *fblk) /* optional freespace block */
1385 xfs_dir2_data_t *data; /* data block structure */
1386 xfs_dir2_db_t dbno; /* data block number */
1387 xfs_dabuf_t *dbp; /* data block buffer */
1388 xfs_dir2_data_entry_t *dep; /* data entry pointer */
1389 xfs_inode_t *dp; /* incore directory inode */
1390 xfs_dir2_data_unused_t *dup; /* data unused entry pointer */
1391 int error; /* error return value */
1392 xfs_dir2_db_t fbno; /* freespace block number */
1393 xfs_dabuf_t *fbp; /* freespace buffer */
1394 int findex; /* freespace entry index */
1395 xfs_dir2_free_t *free=NULL; /* freespace block structure */
1396 xfs_dir2_db_t ifbno; /* initial freespace block no */
1397 xfs_dir2_db_t lastfbno=0; /* highest freespace block no */
1398 int length; /* length of the new entry */
1399 int logfree; /* need to log free entry */
1400 xfs_mount_t *mp; /* filesystem mount point */
1401 int needlog; /* need to log data header */
1402 int needscan; /* need to rescan data frees */
1403 xfs_dir2_data_off_t *tagp; /* data entry tag pointer */
1404 xfs_trans_t *tp; /* transaction pointer */
1409 length = XFS_DIR2_DATA_ENTSIZE(args->namelen);
1411 * If we came in with a freespace block that means that lookup
1412 * found an entry with our hash value. This is the freespace
1413 * block for that data entry.
1418 * Remember initial freespace block number.
1420 ifbno = fblk->blkno;
1422 ASSERT(INT_GET(free->hdr.magic, ARCH_CONVERT) == XFS_DIR2_FREE_MAGIC);
1423 findex = fblk->index;
1425 * This means the free entry showed that the data block had
1426 * space for our entry, so we remembered it.
1427 * Use that data block.
1430 ASSERT(findex < INT_GET(free->hdr.nvalid, ARCH_CONVERT));
1431 ASSERT(INT_GET(free->bests[findex], ARCH_CONVERT) != NULLDATAOFF);
1432 ASSERT(INT_GET(free->bests[findex], ARCH_CONVERT) >= length);
1433 dbno = INT_GET(free->hdr.firstdb, ARCH_CONVERT) + findex;
1436 * The data block looked at didn't have enough room.
1437 * We'll start at the beginning of the freespace entries.
1445 * Didn't come in with a freespace block, so don't have a data block.
1453 * If we don't have a data block yet, we're going to scan the
1454 * freespace blocks looking for one. Figure out what the
1455 * highest freespace block number is.
1458 xfs_fileoff_t fo; /* freespace block number */
1460 if ((error = xfs_bmap_last_offset(tp, dp, &fo, XFS_DATA_FORK)))
1462 lastfbno = XFS_DIR2_DA_TO_DB(mp, (xfs_dablk_t)fo);
1466 * While we haven't identified a data block, search the freeblock
1467 * data for a good data block. If we find a null freeblock entry,
1468 * indicating a hole in the data blocks, remember that.
1470 while (dbno == -1) {
1472 * If we don't have a freeblock in hand, get the next one.
1476 * Happens the first time through unless lookup gave
1477 * us a freespace block to start with.
1480 fbno = XFS_DIR2_FREE_FIRSTDB(mp);
1482 * If it's ifbno we already looked at it.
1487 * If it's off the end we're done.
1489 if (fbno >= lastfbno)
1492 * Read the block. There can be holes in the
1493 * freespace blocks, so this might not succeed.
1494 * This should be really rare, so there's no reason
1497 if ((error = xfs_da_read_buf(tp, dp,
1498 XFS_DIR2_DB_TO_DA(mp, fbno), -2, &fbp,
1502 if (unlikely(fbp == NULL)) {
1506 ASSERT(INT_GET(free->hdr.magic, ARCH_CONVERT) == XFS_DIR2_FREE_MAGIC);
1510 * Look at the current free entry. Is it good enough?
1512 if (INT_GET(free->bests[findex], ARCH_CONVERT) != NULLDATAOFF &&
1513 INT_GET(free->bests[findex], ARCH_CONVERT) >= length)
1514 dbno = INT_GET(free->hdr.firstdb, ARCH_CONVERT) + findex;
1517 * Are we done with the freeblock?
1519 if (++findex == INT_GET(free->hdr.nvalid, ARCH_CONVERT)) {
1523 xfs_da_brelse(tp, fbp);
1525 if (fblk && fblk->bp)
1531 * If we don't have a data block, we need to allocate one and make
1532 * the freespace entries refer to it.
1534 if (unlikely(dbno == -1)) {
1536 * Not allowed to allocate, return failure.
1538 if (args->justcheck || args->total == 0) {
1540 * Drop the freespace buffer unless it came from our
1543 if ((fblk == NULL || fblk->bp == NULL) && fbp != NULL)
1544 xfs_da_buf_done(fbp);
1545 return XFS_ERROR(ENOSPC);
1548 * Allocate and initialize the new data block.
1550 if (unlikely((error = xfs_dir2_grow_inode(args,
1551 XFS_DIR2_DATA_SPACE,
1553 (error = xfs_dir2_data_init(args, dbno, &dbp)))) {
1555 * Drop the freespace buffer unless it came from our
1558 if ((fblk == NULL || fblk->bp == NULL) && fbp != NULL)
1559 xfs_da_buf_done(fbp);
1563 * If (somehow) we have a freespace block, get rid of it.
1566 xfs_da_brelse(tp, fbp);
1567 if (fblk && fblk->bp)
1571 * Get the freespace block corresponding to the data block
1572 * that was just allocated.
1574 fbno = XFS_DIR2_DB_TO_FDB(mp, dbno);
1575 if (unlikely(error = xfs_da_read_buf(tp, dp,
1576 XFS_DIR2_DB_TO_DA(mp, fbno), -2, &fbp,
1578 xfs_da_buf_done(dbp);
1582 * If there wasn't a freespace block, the read will
1583 * return a NULL fbp. Allocate and initialize a new one.
1586 if ((error = xfs_dir2_grow_inode(args, XFS_DIR2_FREE_SPACE,
1591 if (unlikely(XFS_DIR2_DB_TO_FDB(mp, dbno) != fbno)) {
1593 "xfs_dir2_node_addname_int: dir ino "
1594 "%llu needed freesp block %lld for\n"
1595 " data block %lld, got %lld\n"
1596 " ifbno %llu lastfbno %d\n",
1597 (unsigned long long)dp->i_ino,
1598 (long long)XFS_DIR2_DB_TO_FDB(mp, dbno),
1599 (long long)dbno, (long long)fbno,
1600 (unsigned long long)ifbno, lastfbno);
1603 " fblk 0x%p blkno %llu "
1604 "index %d magic 0x%x\n",
1606 (unsigned long long)fblk->blkno,
1611 " ... fblk is NULL\n");
1613 XFS_ERROR_REPORT("xfs_dir2_node_addname_int",
1614 XFS_ERRLEVEL_LOW, mp);
1615 return XFS_ERROR(EFSCORRUPTED);
1619 * Get a buffer for the new block.
1621 if ((error = xfs_da_get_buf(tp, dp,
1622 XFS_DIR2_DB_TO_DA(mp, fbno),
1623 -1, &fbp, XFS_DATA_FORK))) {
1626 ASSERT(fbp != NULL);
1629 * Initialize the new block to be empty, and remember
1630 * its first slot as our empty slot.
1633 INT_SET(free->hdr.magic, ARCH_CONVERT, XFS_DIR2_FREE_MAGIC);
1634 INT_SET(free->hdr.firstdb, ARCH_CONVERT,
1635 (fbno - XFS_DIR2_FREE_FIRSTDB(mp)) *
1636 XFS_DIR2_MAX_FREE_BESTS(mp));
1637 free->hdr.nvalid = 0;
1638 free->hdr.nused = 0;
1641 ASSERT(INT_GET(free->hdr.magic, ARCH_CONVERT) == XFS_DIR2_FREE_MAGIC);
1645 * Set the freespace block index from the data block number.
1647 findex = XFS_DIR2_DB_TO_FDINDEX(mp, dbno);
1649 * If it's after the end of the current entries in the
1650 * freespace block, extend that table.
1652 if (findex >= INT_GET(free->hdr.nvalid, ARCH_CONVERT)) {
1653 ASSERT(findex < XFS_DIR2_MAX_FREE_BESTS(mp));
1654 INT_SET(free->hdr.nvalid, ARCH_CONVERT, findex + 1);
1656 * Tag new entry so nused will go up.
1658 INT_SET(free->bests[findex], ARCH_CONVERT, NULLDATAOFF);
1661 * If this entry was for an empty data block
1662 * (this should always be true) then update the header.
1664 if (INT_GET(free->bests[findex], ARCH_CONVERT) == NULLDATAOFF) {
1665 INT_MOD(free->hdr.nused, ARCH_CONVERT, +1);
1666 xfs_dir2_free_log_header(tp, fbp);
1669 * Update the real value in the table.
1670 * We haven't allocated the data entry yet so this will
1674 INT_COPY(free->bests[findex], data->hdr.bestfree[0].length, ARCH_CONVERT);
1678 * We had a data block so we don't have to make a new one.
1682 * If just checking, we succeeded.
1684 if (args->justcheck) {
1685 if ((fblk == NULL || fblk->bp == NULL) && fbp != NULL)
1686 xfs_da_buf_done(fbp);
1690 * Read the data block in.
1693 error = xfs_da_read_buf(tp, dp, XFS_DIR2_DB_TO_DA(mp, dbno),
1694 -1, &dbp, XFS_DATA_FORK))) {
1695 if ((fblk == NULL || fblk->bp == NULL) && fbp != NULL)
1696 xfs_da_buf_done(fbp);
1702 ASSERT(INT_GET(data->hdr.bestfree[0].length, ARCH_CONVERT) >= length);
1704 * Point to the existing unused space.
1706 dup = (xfs_dir2_data_unused_t *)
1707 ((char *)data + INT_GET(data->hdr.bestfree[0].offset, ARCH_CONVERT));
1708 needscan = needlog = 0;
1710 * Mark the first part of the unused space, inuse for us.
1712 xfs_dir2_data_use_free(tp, dbp, dup,
1713 (xfs_dir2_data_aoff_t)((char *)dup - (char *)data), length,
1714 &needlog, &needscan);
1716 * Fill in the new entry and log it.
1718 dep = (xfs_dir2_data_entry_t *)dup;
1719 INT_SET(dep->inumber, ARCH_CONVERT, args->inumber);
1720 dep->namelen = args->namelen;
1721 memcpy(dep->name, args->name, dep->namelen);
1722 tagp = XFS_DIR2_DATA_ENTRY_TAG_P(dep);
1723 INT_SET(*tagp, ARCH_CONVERT, (xfs_dir2_data_off_t)((char *)dep - (char *)data));
1724 xfs_dir2_data_log_entry(tp, dbp, dep);
1726 * Rescan the block for bestfree if needed.
1729 xfs_dir2_data_freescan(mp, data, &needlog, NULL);
1731 * Log the data block header if needed.
1734 xfs_dir2_data_log_header(tp, dbp);
1736 * If the freespace entry is now wrong, update it.
1738 if (INT_GET(free->bests[findex], ARCH_CONVERT) != INT_GET(data->hdr.bestfree[0].length, ARCH_CONVERT)) {
1739 INT_COPY(free->bests[findex], data->hdr.bestfree[0].length, ARCH_CONVERT);
1743 * Log the freespace entry if needed.
1746 xfs_dir2_free_log_bests(tp, fbp, findex, findex);
1748 * If the caller didn't hand us the freespace block, drop it.
1750 if ((fblk == NULL || fblk->bp == NULL) && fbp != NULL)
1751 xfs_da_buf_done(fbp);
1753 * Return the data block and offset in args, then drop the data block.
1755 args->blkno = (xfs_dablk_t)dbno;
1756 args->index = INT_GET(*tagp, ARCH_CONVERT);
1757 xfs_da_buf_done(dbp);
1762 * Lookup an entry in a node-format directory.
1763 * All the real work happens in xfs_da_node_lookup_int.
1764 * The only real output is the inode number of the entry.
1767 xfs_dir2_node_lookup(
1768 xfs_da_args_t *args) /* operation arguments */
1770 int error; /* error return value */
1771 int i; /* btree level */
1772 int rval; /* operation return value */
1773 xfs_da_state_t *state; /* btree cursor */
1775 xfs_dir2_trace_args("node_lookup", args);
1777 * Allocate and initialize the btree cursor.
1779 state = xfs_da_state_alloc();
1781 state->mp = args->dp->i_mount;
1782 state->blocksize = state->mp->m_dirblksize;
1783 state->node_ents = state->mp->m_dir_node_ents;
1785 * Fill in the path to the entry in the cursor.
1787 error = xfs_da_node_lookup_int(state, &rval);
1791 * Release the btree blocks and leaf block.
1793 for (i = 0; i < state->path.active; i++) {
1794 xfs_da_brelse(args->trans, state->path.blk[i].bp);
1795 state->path.blk[i].bp = NULL;
1798 * Release the data block if we have it.
1800 if (state->extravalid && state->extrablk.bp) {
1801 xfs_da_brelse(args->trans, state->extrablk.bp);
1802 state->extrablk.bp = NULL;
1804 xfs_da_state_free(state);
1809 * Remove an entry from a node-format directory.
1812 xfs_dir2_node_removename(
1813 xfs_da_args_t *args) /* operation arguments */
1815 xfs_da_state_blk_t *blk; /* leaf block */
1816 int error; /* error return value */
1817 int rval; /* operation return value */
1818 xfs_da_state_t *state; /* btree cursor */
1820 xfs_dir2_trace_args("node_removename", args);
1822 * Allocate and initialize the btree cursor.
1824 state = xfs_da_state_alloc();
1826 state->mp = args->dp->i_mount;
1827 state->blocksize = state->mp->m_dirblksize;
1828 state->node_ents = state->mp->m_dir_node_ents;
1830 * Look up the entry we're deleting, set up the cursor.
1832 error = xfs_da_node_lookup_int(state, &rval);
1837 * Didn't find it, upper layer screwed up.
1839 if (rval != EEXIST) {
1840 xfs_da_state_free(state);
1843 blk = &state->path.blk[state->path.active - 1];
1844 ASSERT(blk->magic == XFS_DIR2_LEAFN_MAGIC);
1845 ASSERT(state->extravalid);
1847 * Remove the leaf and data entries.
1848 * Extrablk refers to the data block.
1850 error = xfs_dir2_leafn_remove(args, blk->bp, blk->index,
1851 &state->extrablk, &rval);
1856 * Fix the hash values up the btree.
1858 xfs_da_fixhashpath(state, &state->path);
1860 * If we need to join leaf blocks, do it.
1862 if (rval && state->path.active > 1)
1863 error = xfs_da_join(state);
1865 * If no errors so far, try conversion to leaf format.
1868 error = xfs_dir2_node_to_leaf(state);
1869 xfs_da_state_free(state);
1874 * Replace an entry's inode number in a node-format directory.
1877 xfs_dir2_node_replace(
1878 xfs_da_args_t *args) /* operation arguments */
1880 xfs_da_state_blk_t *blk; /* leaf block */
1881 xfs_dir2_data_t *data; /* data block structure */
1882 xfs_dir2_data_entry_t *dep; /* data entry changed */
1883 int error; /* error return value */
1884 int i; /* btree level */
1885 xfs_ino_t inum; /* new inode number */
1886 xfs_dir2_leaf_t *leaf; /* leaf structure */
1887 xfs_dir2_leaf_entry_t *lep; /* leaf entry being changed */
1888 int rval; /* internal return value */
1889 xfs_da_state_t *state; /* btree cursor */
1891 xfs_dir2_trace_args("node_replace", args);
1893 * Allocate and initialize the btree cursor.
1895 state = xfs_da_state_alloc();
1897 state->mp = args->dp->i_mount;
1898 state->blocksize = state->mp->m_dirblksize;
1899 state->node_ents = state->mp->m_dir_node_ents;
1900 inum = args->inumber;
1902 * Lookup the entry to change in the btree.
1904 error = xfs_da_node_lookup_int(state, &rval);
1909 * It should be found, since the vnodeops layer has looked it up
1910 * and locked it. But paranoia is good.
1912 if (rval == EEXIST) {
1914 * Find the leaf entry.
1916 blk = &state->path.blk[state->path.active - 1];
1917 ASSERT(blk->magic == XFS_DIR2_LEAFN_MAGIC);
1918 leaf = blk->bp->data;
1919 lep = &leaf->ents[blk->index];
1920 ASSERT(state->extravalid);
1922 * Point to the data entry.
1924 data = state->extrablk.bp->data;
1925 ASSERT(INT_GET(data->hdr.magic, ARCH_CONVERT) == XFS_DIR2_DATA_MAGIC);
1926 dep = (xfs_dir2_data_entry_t *)
1928 XFS_DIR2_DATAPTR_TO_OFF(state->mp, INT_GET(lep->address, ARCH_CONVERT)));
1929 ASSERT(inum != INT_GET(dep->inumber, ARCH_CONVERT));
1931 * Fill in the new inode number and log the entry.
1933 INT_SET(dep->inumber, ARCH_CONVERT, inum);
1934 xfs_dir2_data_log_entry(args->trans, state->extrablk.bp, dep);
1938 * Didn't find it, and we're holding a data block. Drop it.
1940 else if (state->extravalid) {
1941 xfs_da_brelse(args->trans, state->extrablk.bp);
1942 state->extrablk.bp = NULL;
1945 * Release all the buffers in the cursor.
1947 for (i = 0; i < state->path.active; i++) {
1948 xfs_da_brelse(args->trans, state->path.blk[i].bp);
1949 state->path.blk[i].bp = NULL;
1951 xfs_da_state_free(state);
1956 * Trim off a trailing empty freespace block.
1957 * Return (in rvalp) 1 if we did it, 0 if not.
1960 xfs_dir2_node_trim_free(
1961 xfs_da_args_t *args, /* operation arguments */
1962 xfs_fileoff_t fo, /* free block number */
1963 int *rvalp) /* out: did something */
1965 xfs_dabuf_t *bp; /* freespace buffer */
1966 xfs_inode_t *dp; /* incore directory inode */
1967 int error; /* error return code */
1968 xfs_dir2_free_t *free; /* freespace structure */
1969 xfs_mount_t *mp; /* filesystem mount point */
1970 xfs_trans_t *tp; /* transaction pointer */
1976 * Read the freespace block.
1978 if (unlikely(error = xfs_da_read_buf(tp, dp, (xfs_dablk_t)fo, -2, &bp,
1984 * There can be holes in freespace. If fo is a hole, there's
1991 ASSERT(INT_GET(free->hdr.magic, ARCH_CONVERT) == XFS_DIR2_FREE_MAGIC);
1993 * If there are used entries, there's nothing to do.
1995 if (INT_GET(free->hdr.nused, ARCH_CONVERT) > 0) {
1996 xfs_da_brelse(tp, bp);
2001 * Blow the block away.
2004 xfs_dir2_shrink_inode(args, XFS_DIR2_DA_TO_DB(mp, (xfs_dablk_t)fo),
2007 * Can't fail with ENOSPC since that only happens with no
2008 * space reservation, when breaking up an extent into two
2009 * pieces. This is the last block of an extent.
2011 ASSERT(error != ENOSPC);
2012 xfs_da_brelse(tp, bp);
2016 * Return that we succeeded.