2 * Copyright (c) 2000-2003 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 version 2 implementation - single leaf form
36 * see xfs_dir2_leaf.h for data structures.
37 * These directories have multiple XFS_DIR2_DATA blocks and one
38 * XFS_DIR2_LEAF1 block containing the hash table and freespace map.
43 #include "xfs_macros.h"
44 #include "xfs_types.h"
47 #include "xfs_trans.h"
52 #include "xfs_dmapi.h"
53 #include "xfs_mount.h"
54 #include "xfs_bmap_btree.h"
55 #include "xfs_attr_sf.h"
56 #include "xfs_dir_sf.h"
57 #include "xfs_dir2_sf.h"
58 #include "xfs_dinode.h"
59 #include "xfs_inode.h"
61 #include "xfs_da_btree.h"
62 #include "xfs_dir2_data.h"
63 #include "xfs_dir2_leaf.h"
64 #include "xfs_dir2_block.h"
65 #include "xfs_dir2_node.h"
66 #include "xfs_dir2_trace.h"
67 #include "xfs_error.h"
71 * Local function declarations.
74 static void xfs_dir2_leaf_check(xfs_inode_t *dp, xfs_dabuf_t *bp);
76 #define xfs_dir2_leaf_check(dp, bp)
78 static int xfs_dir2_leaf_lookup_int(xfs_da_args_t *args, xfs_dabuf_t **lbpp,
79 int *indexp, xfs_dabuf_t **dbpp);
80 static void xfs_dir2_leaf_log_bests(struct xfs_trans *tp, struct xfs_dabuf *bp,
82 static void xfs_dir2_leaf_log_tail(struct xfs_trans *tp, struct xfs_dabuf *bp);
86 * Convert a block form directory to a leaf form directory.
89 xfs_dir2_block_to_leaf(
90 xfs_da_args_t *args, /* operation arguments */
91 xfs_dabuf_t *dbp) /* input block's buffer */
93 xfs_dir2_data_off_t *bestsp; /* leaf's bestsp entries */
94 xfs_dablk_t blkno; /* leaf block's bno */
95 xfs_dir2_block_t *block; /* block structure */
96 xfs_dir2_leaf_entry_t *blp; /* block's leaf entries */
97 xfs_dir2_block_tail_t *btp; /* block's tail */
98 xfs_inode_t *dp; /* incore directory inode */
99 int error; /* error return code */
100 xfs_dabuf_t *lbp; /* leaf block's buffer */
101 xfs_dir2_db_t ldb; /* leaf block's bno */
102 xfs_dir2_leaf_t *leaf; /* leaf structure */
103 xfs_dir2_leaf_tail_t *ltp; /* leaf's tail */
104 xfs_mount_t *mp; /* filesystem mount point */
105 int needlog; /* need to log block header */
106 int needscan; /* need to rescan bestfree */
107 xfs_trans_t *tp; /* transaction pointer */
109 xfs_dir2_trace_args_b("block_to_leaf", args, dbp);
114 * Add the leaf block to the inode.
115 * This interface will only put blocks in the leaf/node range.
116 * Since that's empty now, we'll get the root (block 0 in range).
118 if ((error = xfs_da_grow_inode(args, &blkno))) {
121 ldb = XFS_DIR2_DA_TO_DB(mp, blkno);
122 ASSERT(ldb == XFS_DIR2_LEAF_FIRSTDB(mp));
124 * Initialize the leaf block, get a buffer for it.
126 if ((error = xfs_dir2_leaf_init(args, ldb, &lbp, XFS_DIR2_LEAF1_MAGIC))) {
132 xfs_dir2_data_check(dp, dbp);
133 btp = XFS_DIR2_BLOCK_TAIL_P(mp, block);
134 blp = XFS_DIR2_BLOCK_LEAF_P(btp);
136 * Set the counts in the leaf header.
138 INT_COPY(leaf->hdr.count, btp->count, ARCH_CONVERT); /* INT_: type change */
139 INT_COPY(leaf->hdr.stale, btp->stale, ARCH_CONVERT); /* INT_: type change */
141 * Could compact these but I think we always do the conversion
142 * after squeezing out stale entries.
144 memcpy(leaf->ents, blp, INT_GET(btp->count, ARCH_CONVERT) * sizeof(xfs_dir2_leaf_entry_t));
145 xfs_dir2_leaf_log_ents(tp, lbp, 0, INT_GET(leaf->hdr.count, ARCH_CONVERT) - 1);
149 * Make the space formerly occupied by the leaf entries and block
152 xfs_dir2_data_make_free(tp, dbp,
153 (xfs_dir2_data_aoff_t)((char *)blp - (char *)block),
154 (xfs_dir2_data_aoff_t)((char *)block + mp->m_dirblksize -
156 &needlog, &needscan);
158 * Fix up the block header, make it a data block.
160 INT_SET(block->hdr.magic, ARCH_CONVERT, XFS_DIR2_DATA_MAGIC);
162 xfs_dir2_data_freescan(mp, (xfs_dir2_data_t *)block, &needlog,
165 * Set up leaf tail and bests table.
167 ltp = XFS_DIR2_LEAF_TAIL_P(mp, leaf);
168 INT_SET(ltp->bestcount, ARCH_CONVERT, 1);
169 bestsp = XFS_DIR2_LEAF_BESTS_P(ltp);
170 INT_COPY(bestsp[0], block->hdr.bestfree[0].length, ARCH_CONVERT);
172 * Log the data header and leaf bests table.
175 xfs_dir2_data_log_header(tp, dbp);
176 xfs_dir2_leaf_check(dp, lbp);
177 xfs_dir2_data_check(dp, dbp);
178 xfs_dir2_leaf_log_bests(tp, lbp, 0, 0);
179 xfs_da_buf_done(lbp);
184 * Add an entry to a leaf form directory.
187 xfs_dir2_leaf_addname(
188 xfs_da_args_t *args) /* operation arguments */
190 xfs_dir2_data_off_t *bestsp; /* freespace table in leaf */
191 int compact; /* need to compact leaves */
192 xfs_dir2_data_t *data; /* data block structure */
193 xfs_dabuf_t *dbp; /* data block buffer */
194 xfs_dir2_data_entry_t *dep; /* data block entry */
195 xfs_inode_t *dp; /* incore directory inode */
196 xfs_dir2_data_unused_t *dup; /* data unused entry */
197 int error; /* error return value */
198 int grown; /* allocated new data block */
199 int highstale; /* index of next stale leaf */
200 int i; /* temporary, index */
201 int index; /* leaf table position */
202 xfs_dabuf_t *lbp; /* leaf's buffer */
203 xfs_dir2_leaf_t *leaf; /* leaf structure */
204 int length; /* length of new entry */
205 xfs_dir2_leaf_entry_t *lep; /* leaf entry table pointer */
206 int lfloglow; /* low leaf logging index */
207 int lfloghigh; /* high leaf logging index */
208 int lowstale; /* index of prev stale leaf */
209 xfs_dir2_leaf_tail_t *ltp; /* leaf tail pointer */
210 xfs_mount_t *mp; /* filesystem mount point */
211 int needbytes; /* leaf block bytes needed */
212 int needlog; /* need to log data header */
213 int needscan; /* need to rescan data free */
214 xfs_dir2_data_off_t *tagp; /* end of data entry */
215 xfs_trans_t *tp; /* transaction pointer */
216 xfs_dir2_db_t use_block; /* data block number */
218 xfs_dir2_trace_args("leaf_addname", args);
223 * Read the leaf block.
225 error = xfs_da_read_buf(tp, dp, mp->m_dirleafblk, -1, &lbp,
232 * Look up the entry by hash value and name.
233 * We know it's not there, our caller has already done a lookup.
234 * So the index is of the entry to insert in front of.
235 * But if there are dup hash values the index is of the first of those.
237 index = xfs_dir2_leaf_search_hash(args, lbp);
239 ltp = XFS_DIR2_LEAF_TAIL_P(mp, leaf);
240 bestsp = XFS_DIR2_LEAF_BESTS_P(ltp);
241 length = XFS_DIR2_DATA_ENTSIZE(args->namelen);
243 * See if there are any entries with the same hash value
244 * and space in their block for the new entry.
245 * This is good because it puts multiple same-hash value entries
246 * in a data block, improving the lookup of those entries.
248 for (use_block = -1, lep = &leaf->ents[index];
249 index < INT_GET(leaf->hdr.count, ARCH_CONVERT) && INT_GET(lep->hashval, ARCH_CONVERT) == args->hashval;
251 if (INT_GET(lep->address, ARCH_CONVERT) == XFS_DIR2_NULL_DATAPTR)
253 i = XFS_DIR2_DATAPTR_TO_DB(mp, INT_GET(lep->address, ARCH_CONVERT));
254 ASSERT(i < INT_GET(ltp->bestcount, ARCH_CONVERT));
255 ASSERT(INT_GET(bestsp[i], ARCH_CONVERT) != NULLDATAOFF);
256 if (INT_GET(bestsp[i], ARCH_CONVERT) >= length) {
262 * Didn't find a block yet, linear search all the data blocks.
264 if (use_block == -1) {
265 for (i = 0; i < INT_GET(ltp->bestcount, ARCH_CONVERT); i++) {
267 * Remember a block we see that's missing.
269 if (INT_GET(bestsp[i], ARCH_CONVERT) == NULLDATAOFF && use_block == -1)
271 else if (INT_GET(bestsp[i], ARCH_CONVERT) >= length) {
278 * How many bytes do we need in the leaf block?
281 (leaf->hdr.stale ? 0 : (uint)sizeof(leaf->ents[0])) +
282 (use_block != -1 ? 0 : (uint)sizeof(leaf->bests[0]));
284 * Now kill use_block if it refers to a missing block, so we
285 * can use it as an indication of allocation needed.
287 if (use_block != -1 && INT_GET(bestsp[use_block], ARCH_CONVERT) == NULLDATAOFF)
290 * If we don't have enough free bytes but we can make enough
291 * by compacting out stale entries, we'll do that.
293 if ((char *)bestsp - (char *)&leaf->ents[INT_GET(leaf->hdr.count, ARCH_CONVERT)] < needbytes &&
294 INT_GET(leaf->hdr.stale, ARCH_CONVERT) > 1) {
298 * Otherwise if we don't have enough free bytes we need to
299 * convert to node form.
301 else if ((char *)bestsp - (char *)&leaf->ents[INT_GET(leaf->hdr.count, ARCH_CONVERT)] <
304 * Just checking or no space reservation, give up.
306 if (args->justcheck || args->total == 0) {
307 xfs_da_brelse(tp, lbp);
308 return XFS_ERROR(ENOSPC);
311 * Convert to node form.
313 error = xfs_dir2_leaf_to_node(args, lbp);
314 xfs_da_buf_done(lbp);
318 * Then add the new entry.
320 return xfs_dir2_node_addname(args);
323 * Otherwise it will fit without compaction.
328 * If just checking, then it will fit unless we needed to allocate
331 if (args->justcheck) {
332 xfs_da_brelse(tp, lbp);
333 return use_block == -1 ? XFS_ERROR(ENOSPC) : 0;
336 * If no allocations are allowed, return now before we've
339 if (args->total == 0 && use_block == -1) {
340 xfs_da_brelse(tp, lbp);
341 return XFS_ERROR(ENOSPC);
344 * Need to compact the leaf entries, removing stale ones.
345 * Leave one stale entry behind - the one closest to our
346 * insertion index - and we'll shift that one to our insertion
350 xfs_dir2_leaf_compact_x1(lbp, &index, &lowstale, &highstale,
351 &lfloglow, &lfloghigh);
354 * There are stale entries, so we'll need log-low and log-high
355 * impossibly bad values later.
357 else if (INT_GET(leaf->hdr.stale, ARCH_CONVERT)) {
358 lfloglow = INT_GET(leaf->hdr.count, ARCH_CONVERT);
362 * If there was no data block space found, we need to allocate
365 if (use_block == -1) {
367 * Add the new data block.
369 if ((error = xfs_dir2_grow_inode(args, XFS_DIR2_DATA_SPACE,
371 xfs_da_brelse(tp, lbp);
375 * Initialize the block.
377 if ((error = xfs_dir2_data_init(args, use_block, &dbp))) {
378 xfs_da_brelse(tp, lbp);
382 * If we're adding a new data block on the end we need to
383 * extend the bests table. Copy it up one entry.
385 if (use_block >= INT_GET(ltp->bestcount, ARCH_CONVERT)) {
387 memmove(&bestsp[0], &bestsp[1],
388 INT_GET(ltp->bestcount, ARCH_CONVERT) * sizeof(bestsp[0]));
389 INT_MOD(ltp->bestcount, ARCH_CONVERT, +1);
390 xfs_dir2_leaf_log_tail(tp, lbp);
391 xfs_dir2_leaf_log_bests(tp, lbp, 0, INT_GET(ltp->bestcount, ARCH_CONVERT) - 1);
394 * If we're filling in a previously empty block just log it.
397 xfs_dir2_leaf_log_bests(tp, lbp, use_block, use_block);
399 INT_COPY(bestsp[use_block], data->hdr.bestfree[0].length, ARCH_CONVERT);
403 * Already had space in some data block.
404 * Just read that one in.
408 xfs_da_read_buf(tp, dp, XFS_DIR2_DB_TO_DA(mp, use_block),
409 -1, &dbp, XFS_DATA_FORK))) {
410 xfs_da_brelse(tp, lbp);
416 xfs_dir2_data_check(dp, dbp);
418 * Point to the biggest freespace in our data block.
420 dup = (xfs_dir2_data_unused_t *)
421 ((char *)data + INT_GET(data->hdr.bestfree[0].offset, ARCH_CONVERT));
422 ASSERT(INT_GET(dup->length, ARCH_CONVERT) >= length);
423 needscan = needlog = 0;
425 * Mark the initial part of our freespace in use for the new entry.
427 xfs_dir2_data_use_free(tp, dbp, dup,
428 (xfs_dir2_data_aoff_t)((char *)dup - (char *)data), length,
429 &needlog, &needscan);
431 * Initialize our new entry (at last).
433 dep = (xfs_dir2_data_entry_t *)dup;
434 INT_SET(dep->inumber, ARCH_CONVERT, args->inumber);
435 dep->namelen = args->namelen;
436 memcpy(dep->name, args->name, dep->namelen);
437 tagp = XFS_DIR2_DATA_ENTRY_TAG_P(dep);
438 INT_SET(*tagp, ARCH_CONVERT, (xfs_dir2_data_off_t)((char *)dep - (char *)data));
440 * Need to scan fix up the bestfree table.
443 xfs_dir2_data_freescan(mp, data, &needlog, NULL);
445 * Need to log the data block's header.
448 xfs_dir2_data_log_header(tp, dbp);
449 xfs_dir2_data_log_entry(tp, dbp, dep);
451 * If the bests table needs to be changed, do it.
452 * Log the change unless we've already done that.
454 if (INT_GET(bestsp[use_block], ARCH_CONVERT) != INT_GET(data->hdr.bestfree[0].length, ARCH_CONVERT)) {
455 INT_COPY(bestsp[use_block], data->hdr.bestfree[0].length, ARCH_CONVERT);
457 xfs_dir2_leaf_log_bests(tp, lbp, use_block, use_block);
460 * Now we need to make room to insert the leaf entry.
461 * If there are no stale entries, we just insert a hole at index.
463 if (!leaf->hdr.stale) {
465 * lep is still good as the index leaf entry.
467 if (index < INT_GET(leaf->hdr.count, ARCH_CONVERT))
468 memmove(lep + 1, lep,
469 (INT_GET(leaf->hdr.count, ARCH_CONVERT) - index) * sizeof(*lep));
471 * Record low and high logging indices for the leaf.
474 lfloghigh = INT_GET(leaf->hdr.count, ARCH_CONVERT);
475 INT_MOD(leaf->hdr.count, ARCH_CONVERT, +1);
478 * There are stale entries.
479 * We will use one of them for the new entry.
480 * It's probably not at the right location, so we'll have to
481 * shift some up or down first.
485 * If we didn't compact before, we need to find the nearest
486 * stale entries before and after our insertion point.
490 * Find the first stale entry before the insertion
493 for (lowstale = index - 1;
495 INT_GET(leaf->ents[lowstale].address, ARCH_CONVERT) !=
496 XFS_DIR2_NULL_DATAPTR;
500 * Find the next stale entry at or after the insertion
501 * point, if any. Stop if we go so far that the
502 * lowstale entry would be better.
504 for (highstale = index;
505 highstale < INT_GET(leaf->hdr.count, ARCH_CONVERT) &&
506 INT_GET(leaf->ents[highstale].address, ARCH_CONVERT) !=
507 XFS_DIR2_NULL_DATAPTR &&
509 index - lowstale - 1 >= highstale - index);
514 * If the low one is better, use it.
517 (highstale == INT_GET(leaf->hdr.count, ARCH_CONVERT) ||
518 index - lowstale - 1 < highstale - index)) {
519 ASSERT(index - lowstale - 1 >= 0);
520 ASSERT(INT_GET(leaf->ents[lowstale].address, ARCH_CONVERT) ==
521 XFS_DIR2_NULL_DATAPTR);
523 * Copy entries up to cover the stale entry
524 * and make room for the new entry.
526 if (index - lowstale - 1 > 0)
527 memmove(&leaf->ents[lowstale],
528 &leaf->ents[lowstale + 1],
529 (index - lowstale - 1) * sizeof(*lep));
530 lep = &leaf->ents[index - 1];
531 lfloglow = MIN(lowstale, lfloglow);
532 lfloghigh = MAX(index - 1, lfloghigh);
535 * The high one is better, so use that one.
538 ASSERT(highstale - index >= 0);
539 ASSERT(INT_GET(leaf->ents[highstale].address, ARCH_CONVERT) ==
540 XFS_DIR2_NULL_DATAPTR);
542 * Copy entries down to copver the stale entry
543 * and make room for the new entry.
545 if (highstale - index > 0)
546 memmove(&leaf->ents[index + 1],
548 (highstale - index) * sizeof(*lep));
549 lep = &leaf->ents[index];
550 lfloglow = MIN(index, lfloglow);
551 lfloghigh = MAX(highstale, lfloghigh);
553 INT_MOD(leaf->hdr.stale, ARCH_CONVERT, -1);
556 * Fill in the new leaf entry.
558 INT_SET(lep->hashval, ARCH_CONVERT, args->hashval);
559 INT_SET(lep->address, ARCH_CONVERT, XFS_DIR2_DB_OFF_TO_DATAPTR(mp, use_block, INT_GET(*tagp, ARCH_CONVERT)));
561 * Log the leaf fields and give up the buffers.
563 xfs_dir2_leaf_log_header(tp, lbp);
564 xfs_dir2_leaf_log_ents(tp, lbp, lfloglow, lfloghigh);
565 xfs_dir2_leaf_check(dp, lbp);
566 xfs_da_buf_done(lbp);
567 xfs_dir2_data_check(dp, dbp);
568 xfs_da_buf_done(dbp);
574 * Check the internal consistency of a leaf1 block.
575 * Pop an assert if something is wrong.
579 xfs_inode_t *dp, /* incore directory inode */
580 xfs_dabuf_t *bp) /* leaf's buffer */
582 int i; /* leaf index */
583 xfs_dir2_leaf_t *leaf; /* leaf structure */
584 xfs_dir2_leaf_tail_t *ltp; /* leaf tail pointer */
585 xfs_mount_t *mp; /* filesystem mount point */
586 int stale; /* count of stale leaves */
590 ASSERT(INT_GET(leaf->hdr.info.magic, ARCH_CONVERT) == XFS_DIR2_LEAF1_MAGIC);
592 * This value is not restrictive enough.
593 * Should factor in the size of the bests table as well.
594 * We can deduce a value for that from di_size.
596 ASSERT(INT_GET(leaf->hdr.count, ARCH_CONVERT) <= XFS_DIR2_MAX_LEAF_ENTS(mp));
597 ltp = XFS_DIR2_LEAF_TAIL_P(mp, leaf);
599 * Leaves and bests don't overlap.
601 ASSERT((char *)&leaf->ents[INT_GET(leaf->hdr.count, ARCH_CONVERT)] <=
602 (char *)XFS_DIR2_LEAF_BESTS_P(ltp));
604 * Check hash value order, count stale entries.
606 for (i = stale = 0; i < INT_GET(leaf->hdr.count, ARCH_CONVERT); i++) {
607 if (i + 1 < INT_GET(leaf->hdr.count, ARCH_CONVERT))
608 ASSERT(INT_GET(leaf->ents[i].hashval, ARCH_CONVERT) <=
609 INT_GET(leaf->ents[i + 1].hashval, ARCH_CONVERT));
610 if (INT_GET(leaf->ents[i].address, ARCH_CONVERT) == XFS_DIR2_NULL_DATAPTR)
613 ASSERT(INT_GET(leaf->hdr.stale, ARCH_CONVERT) == stale);
618 * Compact out any stale entries in the leaf.
619 * Log the header and changed leaf entries, if any.
622 xfs_dir2_leaf_compact(
623 xfs_da_args_t *args, /* operation arguments */
624 xfs_dabuf_t *bp) /* leaf buffer */
626 int from; /* source leaf index */
627 xfs_dir2_leaf_t *leaf; /* leaf structure */
628 int loglow; /* first leaf entry to log */
629 int to; /* target leaf index */
632 if (!leaf->hdr.stale) {
636 * Compress out the stale entries in place.
638 for (from = to = 0, loglow = -1; from < INT_GET(leaf->hdr.count, ARCH_CONVERT); from++) {
639 if (INT_GET(leaf->ents[from].address, ARCH_CONVERT) == XFS_DIR2_NULL_DATAPTR)
642 * Only actually copy the entries that are different.
647 leaf->ents[to] = leaf->ents[from];
652 * Update and log the header, log the leaf entries.
654 ASSERT(INT_GET(leaf->hdr.stale, ARCH_CONVERT) == from - to);
655 INT_MOD(leaf->hdr.count, ARCH_CONVERT, -(INT_GET(leaf->hdr.stale, ARCH_CONVERT)));
657 xfs_dir2_leaf_log_header(args->trans, bp);
659 xfs_dir2_leaf_log_ents(args->trans, bp, loglow, to - 1);
663 * Compact the leaf entries, removing stale ones.
664 * Leave one stale entry behind - the one closest to our
665 * insertion index - and the caller will shift that one to our insertion
667 * Return new insertion index, where the remaining stale entry is,
668 * and leaf logging indices.
671 xfs_dir2_leaf_compact_x1(
672 xfs_dabuf_t *bp, /* leaf buffer */
673 int *indexp, /* insertion index */
674 int *lowstalep, /* out: stale entry before us */
675 int *highstalep, /* out: stale entry after us */
676 int *lowlogp, /* out: low log index */
677 int *highlogp) /* out: high log index */
679 int from; /* source copy index */
680 int highstale; /* stale entry at/after index */
681 int index; /* insertion index */
682 int keepstale; /* source index of kept stale */
683 xfs_dir2_leaf_t *leaf; /* leaf structure */
684 int lowstale; /* stale entry before index */
685 int newindex=0; /* new insertion index */
686 int to; /* destination copy index */
689 ASSERT(INT_GET(leaf->hdr.stale, ARCH_CONVERT) > 1);
692 * Find the first stale entry before our index, if any.
694 for (lowstale = index - 1;
696 INT_GET(leaf->ents[lowstale].address, ARCH_CONVERT) != XFS_DIR2_NULL_DATAPTR;
700 * Find the first stale entry at or after our index, if any.
701 * Stop if the answer would be worse than lowstale.
703 for (highstale = index;
704 highstale < INT_GET(leaf->hdr.count, ARCH_CONVERT) &&
705 INT_GET(leaf->ents[highstale].address, ARCH_CONVERT) != XFS_DIR2_NULL_DATAPTR &&
706 (lowstale < 0 || index - lowstale > highstale - index);
710 * Pick the better of lowstale and highstale.
713 (highstale == INT_GET(leaf->hdr.count, ARCH_CONVERT) ||
714 index - lowstale <= highstale - index))
715 keepstale = lowstale;
717 keepstale = highstale;
719 * Copy the entries in place, removing all the stale entries
722 for (from = to = 0; from < INT_GET(leaf->hdr.count, ARCH_CONVERT); from++) {
724 * Notice the new value of index.
728 if (from != keepstale &&
729 INT_GET(leaf->ents[from].address, ARCH_CONVERT) == XFS_DIR2_NULL_DATAPTR) {
735 * Record the new keepstale value for the insertion.
737 if (from == keepstale)
738 lowstale = highstale = to;
740 * Copy only the entries that have moved.
743 leaf->ents[to] = leaf->ents[from];
748 * If the insertion point was past the last entry,
749 * set the new insertion point accordingly.
755 * Adjust the leaf header values.
757 INT_MOD(leaf->hdr.count, ARCH_CONVERT, -(from - to));
758 INT_SET(leaf->hdr.stale, ARCH_CONVERT, 1);
760 * Remember the low/high stale value only in the "right"
763 if (lowstale >= newindex)
766 highstale = INT_GET(leaf->hdr.count, ARCH_CONVERT);
767 *highlogp = INT_GET(leaf->hdr.count, ARCH_CONVERT) - 1;
768 *lowstalep = lowstale;
769 *highstalep = highstale;
773 * Getdents (readdir) for leaf and node directories.
774 * This reads the data blocks only, so is the same for both forms.
777 xfs_dir2_leaf_getdents(
778 xfs_trans_t *tp, /* transaction pointer */
779 xfs_inode_t *dp, /* incore directory inode */
780 uio_t *uio, /* I/O control & vectors */
781 int *eofp, /* out: reached end of dir */
782 xfs_dirent_t *dbp, /* caller's buffer */
783 xfs_dir2_put_t put) /* ABI formatting routine */
785 xfs_dabuf_t *bp; /* data block buffer */
786 int byteoff; /* offset in current block */
787 xfs_dir2_db_t curdb; /* db for current block */
788 xfs_dir2_off_t curoff; /* current overall offset */
789 xfs_dir2_data_t *data; /* data block structure */
790 xfs_dir2_data_entry_t *dep; /* data entry */
791 xfs_dir2_data_unused_t *dup; /* unused entry */
792 int eof; /* reached end of directory */
793 int error=0; /* error return value */
794 int i; /* temporary loop index */
795 int j; /* temporary loop index */
796 int length; /* temporary length value */
797 xfs_bmbt_irec_t *map; /* map vector for blocks */
798 xfs_extlen_t map_blocks; /* number of fsbs in map */
799 xfs_dablk_t map_off; /* last mapped file offset */
800 int map_size; /* total entries in *map */
801 int map_valid; /* valid entries in *map */
802 xfs_mount_t *mp; /* filesystem mount point */
803 xfs_dir2_off_t newoff; /* new curoff after new blk */
804 int nmap; /* mappings to ask xfs_bmapi */
805 xfs_dir2_put_args_t p; /* formatting arg bundle */
806 char *ptr=NULL; /* pointer to current data */
807 int ra_current; /* number of read-ahead blks */
808 int ra_index; /* *map index for read-ahead */
809 int ra_offset; /* map entry offset for ra */
810 int ra_want; /* readahead count wanted */
813 * If the offset is at or past the largest allowed value,
814 * give up right away, return eof.
816 if (uio->uio_offset >= XFS_DIR2_MAX_DATAPTR) {
822 * Setup formatting arguments.
828 * Set up to bmap a number of blocks based on the caller's
829 * buffer size, the directory block size, and the filesystem
833 howmany(uio->uio_resid + mp->m_dirblksize,
834 mp->m_sb.sb_blocksize);
835 map = kmem_alloc(map_size * sizeof(*map), KM_SLEEP);
836 map_valid = ra_index = ra_offset = ra_current = map_blocks = 0;
840 * Inside the loop we keep the main offset value as a byte offset
841 * in the directory file.
843 curoff = XFS_DIR2_DATAPTR_TO_BYTE(mp, uio->uio_offset);
845 * Force this conversion through db so we truncate the offset
846 * down to get the start of the data block.
848 map_off = XFS_DIR2_DB_TO_DA(mp, XFS_DIR2_BYTE_TO_DB(mp, curoff));
850 * Loop over directory entries until we reach the end offset.
851 * Get more blocks and readahead as necessary.
853 while (curoff < XFS_DIR2_LEAF_OFFSET) {
855 * If we have no buffer, or we're off the end of the
856 * current buffer, need to get another one.
858 if (!bp || ptr >= (char *)bp->data + mp->m_dirblksize) {
860 * If we have a buffer, we need to release it and
861 * take it out of the mapping.
864 xfs_da_brelse(tp, bp);
866 map_blocks -= mp->m_dirblkfsbs;
868 * Loop to get rid of the extents for the
871 for (i = mp->m_dirblkfsbs; i > 0; ) {
872 j = MIN((int)map->br_blockcount, i);
873 map->br_blockcount -= j;
874 map->br_startblock += j;
875 map->br_startoff += j;
877 * If mapping is done, pitch it from
880 if (!map->br_blockcount && --map_valid)
881 memmove(&map[0], &map[1],
888 * Recalculate the readahead blocks wanted.
890 ra_want = howmany(uio->uio_resid + mp->m_dirblksize,
891 mp->m_sb.sb_blocksize) - 1;
893 * If we don't have as many as we want, and we haven't
894 * run out of data blocks, get some more mappings.
896 if (1 + ra_want > map_blocks &&
898 XFS_DIR2_BYTE_TO_DA(mp, XFS_DIR2_LEAF_OFFSET)) {
900 * Get more bmaps, fill in after the ones
901 * we already have in the table.
903 nmap = map_size - map_valid;
904 error = xfs_bmapi(tp, dp,
906 XFS_DIR2_BYTE_TO_DA(mp,
907 XFS_DIR2_LEAF_OFFSET) - map_off,
908 XFS_BMAPI_METADATA, NULL, 0,
909 &map[map_valid], &nmap, NULL);
911 * Don't know if we should ignore this or
912 * try to return an error.
913 * The trouble with returning errors
914 * is that readdir will just stop without
915 * actually passing the error through.
920 * If we got all the mappings we asked for,
921 * set the final map offset based on the
922 * last bmap value received.
923 * Otherwise, we've reached the end.
925 if (nmap == map_size - map_valid)
927 map[map_valid + nmap - 1].br_startoff +
928 map[map_valid + nmap - 1].br_blockcount;
931 XFS_DIR2_BYTE_TO_DA(mp,
932 XFS_DIR2_LEAF_OFFSET);
934 * Look for holes in the mapping, and
935 * eliminate them. Count up the valid blocks.
937 for (i = map_valid; i < map_valid + nmap; ) {
938 if (map[i].br_startblock ==
941 length = map_valid + nmap - i;
949 map[i].br_blockcount;
956 * No valid mappings, so no more data blocks.
959 curoff = XFS_DIR2_DA_TO_BYTE(mp, map_off);
963 * Read the directory block starting at the first
966 curdb = XFS_DIR2_DA_TO_DB(mp, map->br_startoff);
967 error = xfs_da_read_buf(tp, dp, map->br_startoff,
968 map->br_blockcount >= mp->m_dirblkfsbs ?
969 XFS_FSB_TO_DADDR(mp, map->br_startblock) :
973 * Should just skip over the data block instead
979 * Adjust the current amount of read-ahead: we just
980 * read a block that was previously ra.
983 ra_current -= mp->m_dirblkfsbs;
985 * Do we need more readahead?
987 for (ra_index = ra_offset = i = 0;
988 ra_want > ra_current && i < map_blocks;
989 i += mp->m_dirblkfsbs) {
990 ASSERT(ra_index < map_valid);
992 * Read-ahead a contiguous directory block.
994 if (i > ra_current &&
995 map[ra_index].br_blockcount >=
997 xfs_baread(mp->m_ddev_targp,
999 map[ra_index].br_startblock +
1001 (int)BTOBB(mp->m_dirblksize));
1005 * Read-ahead a non-contiguous directory block.
1006 * This doesn't use our mapping, but this
1007 * is a very rare case.
1009 else if (i > ra_current) {
1010 (void)xfs_da_reada_buf(tp, dp,
1011 map[ra_index].br_startoff +
1012 ra_offset, XFS_DATA_FORK);
1016 * Advance offset through the mapping table.
1018 for (j = 0; j < mp->m_dirblkfsbs; j++) {
1020 * The rest of this extent but not
1021 * more than a dir block.
1023 length = MIN(mp->m_dirblkfsbs,
1024 (int)(map[ra_index].br_blockcount -
1027 ra_offset += length;
1029 * Advance to the next mapping if
1030 * this one is used up.
1033 map[ra_index].br_blockcount) {
1040 * Having done a read, we need to set a new offset.
1042 newoff = XFS_DIR2_DB_OFF_TO_BYTE(mp, curdb, 0);
1044 * Start of the current block.
1046 if (curoff < newoff)
1049 * Make sure we're in the right block.
1051 else if (curoff > newoff)
1052 ASSERT(XFS_DIR2_BYTE_TO_DB(mp, curoff) ==
1055 xfs_dir2_data_check(dp, bp);
1057 * Find our position in the block.
1059 ptr = (char *)&data->u;
1060 byteoff = XFS_DIR2_BYTE_TO_OFF(mp, curoff);
1062 * Skip past the header.
1065 curoff += (uint)sizeof(data->hdr);
1067 * Skip past entries until we reach our offset.
1070 while ((char *)ptr - (char *)data < byteoff) {
1071 dup = (xfs_dir2_data_unused_t *)ptr;
1073 if (INT_GET(dup->freetag, ARCH_CONVERT)
1074 == XFS_DIR2_DATA_FREE_TAG) {
1076 length = INT_GET(dup->length,
1081 dep = (xfs_dir2_data_entry_t *)ptr;
1083 XFS_DIR2_DATA_ENTSIZE(dep->namelen);
1087 * Now set our real offset.
1090 XFS_DIR2_DB_OFF_TO_BYTE(mp,
1091 XFS_DIR2_BYTE_TO_DB(mp, curoff),
1092 (char *)ptr - (char *)data);
1093 if (ptr >= (char *)data + mp->m_dirblksize) {
1099 * We have a pointer to an entry.
1102 dup = (xfs_dir2_data_unused_t *)ptr;
1104 * No, it's unused, skip over it.
1106 if (INT_GET(dup->freetag, ARCH_CONVERT)
1107 == XFS_DIR2_DATA_FREE_TAG) {
1108 length = INT_GET(dup->length, ARCH_CONVERT);
1115 * Copy the entry into the putargs, and try formatting it.
1117 dep = (xfs_dir2_data_entry_t *)ptr;
1119 p.namelen = dep->namelen;
1121 length = XFS_DIR2_DATA_ENTSIZE(p.namelen);
1123 p.cook = XFS_DIR2_BYTE_TO_DATAPTR(mp, curoff + length);
1125 p.ino = INT_GET(dep->inumber, ARCH_CONVERT);
1127 p.ino += mp->m_inoadd;
1129 p.name = (char *)dep->name;
1134 * Won't fit. Return to caller.
1141 * Advance to next entry in the block.
1148 * All done. Set output offset value to current offset.
1151 if (curoff > XFS_DIR2_DATAPTR_TO_BYTE(mp, XFS_DIR2_MAX_DATAPTR))
1152 uio->uio_offset = XFS_DIR2_MAX_DATAPTR;
1154 uio->uio_offset = XFS_DIR2_BYTE_TO_DATAPTR(mp, curoff);
1155 kmem_free(map, map_size * sizeof(*map));
1157 xfs_da_brelse(tp, bp);
1162 * Initialize a new leaf block, leaf1 or leafn magic accepted.
1166 xfs_da_args_t *args, /* operation arguments */
1167 xfs_dir2_db_t bno, /* directory block number */
1168 xfs_dabuf_t **bpp, /* out: leaf buffer */
1169 int magic) /* magic number for block */
1171 xfs_dabuf_t *bp; /* leaf buffer */
1172 xfs_inode_t *dp; /* incore directory inode */
1173 int error; /* error return code */
1174 xfs_dir2_leaf_t *leaf; /* leaf structure */
1175 xfs_dir2_leaf_tail_t *ltp; /* leaf tail structure */
1176 xfs_mount_t *mp; /* filesystem mount point */
1177 xfs_trans_t *tp; /* transaction pointer */
1183 ASSERT(bno >= XFS_DIR2_LEAF_FIRSTDB(mp) &&
1184 bno < XFS_DIR2_FREE_FIRSTDB(mp));
1186 * Get the buffer for the block.
1188 error = xfs_da_get_buf(tp, dp, XFS_DIR2_DB_TO_DA(mp, bno), -1, &bp,
1196 * Initialize the header.
1198 INT_SET(leaf->hdr.info.magic, ARCH_CONVERT, magic);
1199 leaf->hdr.info.forw = 0;
1200 leaf->hdr.info.back = 0;
1201 leaf->hdr.count = 0;
1202 leaf->hdr.stale = 0;
1203 xfs_dir2_leaf_log_header(tp, bp);
1205 * If it's a leaf-format directory initialize the tail.
1206 * In this case our caller has the real bests table to copy into
1209 if (magic == XFS_DIR2_LEAF1_MAGIC) {
1210 ltp = XFS_DIR2_LEAF_TAIL_P(mp, leaf);
1212 xfs_dir2_leaf_log_tail(tp, bp);
1219 * Log the bests entries indicated from a leaf1 block.
1222 xfs_dir2_leaf_log_bests(
1223 xfs_trans_t *tp, /* transaction pointer */
1224 xfs_dabuf_t *bp, /* leaf buffer */
1225 int first, /* first entry to log */
1226 int last) /* last entry to log */
1228 xfs_dir2_data_off_t *firstb; /* pointer to first entry */
1229 xfs_dir2_data_off_t *lastb; /* pointer to last entry */
1230 xfs_dir2_leaf_t *leaf; /* leaf structure */
1231 xfs_dir2_leaf_tail_t *ltp; /* leaf tail structure */
1234 ASSERT(INT_GET(leaf->hdr.info.magic, ARCH_CONVERT) == XFS_DIR2_LEAF1_MAGIC);
1235 ltp = XFS_DIR2_LEAF_TAIL_P(tp->t_mountp, leaf);
1236 firstb = XFS_DIR2_LEAF_BESTS_P(ltp) + first;
1237 lastb = XFS_DIR2_LEAF_BESTS_P(ltp) + last;
1238 xfs_da_log_buf(tp, bp, (uint)((char *)firstb - (char *)leaf),
1239 (uint)((char *)lastb - (char *)leaf + sizeof(*lastb) - 1));
1243 * Log the leaf entries indicated from a leaf1 or leafn block.
1246 xfs_dir2_leaf_log_ents(
1247 xfs_trans_t *tp, /* transaction pointer */
1248 xfs_dabuf_t *bp, /* leaf buffer */
1249 int first, /* first entry to log */
1250 int last) /* last entry to log */
1252 xfs_dir2_leaf_entry_t *firstlep; /* pointer to first entry */
1253 xfs_dir2_leaf_entry_t *lastlep; /* pointer to last entry */
1254 xfs_dir2_leaf_t *leaf; /* leaf structure */
1257 ASSERT(INT_GET(leaf->hdr.info.magic, ARCH_CONVERT) == XFS_DIR2_LEAF1_MAGIC ||
1258 INT_GET(leaf->hdr.info.magic, ARCH_CONVERT) == XFS_DIR2_LEAFN_MAGIC);
1259 firstlep = &leaf->ents[first];
1260 lastlep = &leaf->ents[last];
1261 xfs_da_log_buf(tp, bp, (uint)((char *)firstlep - (char *)leaf),
1262 (uint)((char *)lastlep - (char *)leaf + sizeof(*lastlep) - 1));
1266 * Log the header of the leaf1 or leafn block.
1269 xfs_dir2_leaf_log_header(
1270 xfs_trans_t *tp, /* transaction pointer */
1271 xfs_dabuf_t *bp) /* leaf buffer */
1273 xfs_dir2_leaf_t *leaf; /* leaf structure */
1276 ASSERT(INT_GET(leaf->hdr.info.magic, ARCH_CONVERT) == XFS_DIR2_LEAF1_MAGIC ||
1277 INT_GET(leaf->hdr.info.magic, ARCH_CONVERT) == XFS_DIR2_LEAFN_MAGIC);
1278 xfs_da_log_buf(tp, bp, (uint)((char *)&leaf->hdr - (char *)leaf),
1279 (uint)(sizeof(leaf->hdr) - 1));
1283 * Log the tail of the leaf1 block.
1286 xfs_dir2_leaf_log_tail(
1287 xfs_trans_t *tp, /* transaction pointer */
1288 xfs_dabuf_t *bp) /* leaf buffer */
1290 xfs_dir2_leaf_t *leaf; /* leaf structure */
1291 xfs_dir2_leaf_tail_t *ltp; /* leaf tail structure */
1292 xfs_mount_t *mp; /* filesystem mount point */
1296 ASSERT(INT_GET(leaf->hdr.info.magic, ARCH_CONVERT) == XFS_DIR2_LEAF1_MAGIC);
1297 ltp = XFS_DIR2_LEAF_TAIL_P(mp, leaf);
1298 xfs_da_log_buf(tp, bp, (uint)((char *)ltp - (char *)leaf),
1299 (uint)(mp->m_dirblksize - 1));
1303 * Look up the entry referred to by args in the leaf format directory.
1304 * Most of the work is done by the xfs_dir2_leaf_lookup_int routine which
1305 * is also used by the node-format code.
1308 xfs_dir2_leaf_lookup(
1309 xfs_da_args_t *args) /* operation arguments */
1311 xfs_dabuf_t *dbp; /* data block buffer */
1312 xfs_dir2_data_entry_t *dep; /* data block entry */
1313 xfs_inode_t *dp; /* incore directory inode */
1314 int error; /* error return code */
1315 int index; /* found entry index */
1316 xfs_dabuf_t *lbp; /* leaf buffer */
1317 xfs_dir2_leaf_t *leaf; /* leaf structure */
1318 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
1319 xfs_trans_t *tp; /* transaction pointer */
1321 xfs_dir2_trace_args("leaf_lookup", args);
1323 * Look up name in the leaf block, returning both buffers and index.
1325 if ((error = xfs_dir2_leaf_lookup_int(args, &lbp, &index, &dbp))) {
1330 xfs_dir2_leaf_check(dp, lbp);
1333 * Get to the leaf entry and contained data entry address.
1335 lep = &leaf->ents[index];
1337 * Point to the data entry.
1339 dep = (xfs_dir2_data_entry_t *)
1340 ((char *)dbp->data +
1341 XFS_DIR2_DATAPTR_TO_OFF(dp->i_mount, INT_GET(lep->address, ARCH_CONVERT)));
1343 * Return the found inode number.
1345 args->inumber = INT_GET(dep->inumber, ARCH_CONVERT);
1346 xfs_da_brelse(tp, dbp);
1347 xfs_da_brelse(tp, lbp);
1348 return XFS_ERROR(EEXIST);
1352 * Look up name/hash in the leaf block.
1353 * Fill in indexp with the found index, and dbpp with the data buffer.
1354 * If not found dbpp will be NULL, and ENOENT comes back.
1355 * lbpp will always be filled in with the leaf buffer unless there's an error.
1357 static int /* error */
1358 xfs_dir2_leaf_lookup_int(
1359 xfs_da_args_t *args, /* operation arguments */
1360 xfs_dabuf_t **lbpp, /* out: leaf buffer */
1361 int *indexp, /* out: index in leaf block */
1362 xfs_dabuf_t **dbpp) /* out: data buffer */
1364 xfs_dir2_db_t curdb; /* current data block number */
1365 xfs_dabuf_t *dbp; /* data buffer */
1366 xfs_dir2_data_entry_t *dep; /* data entry */
1367 xfs_inode_t *dp; /* incore directory inode */
1368 int error; /* error return code */
1369 int index; /* index in leaf block */
1370 xfs_dabuf_t *lbp; /* leaf buffer */
1371 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
1372 xfs_dir2_leaf_t *leaf; /* leaf structure */
1373 xfs_mount_t *mp; /* filesystem mount point */
1374 xfs_dir2_db_t newdb; /* new data block number */
1375 xfs_trans_t *tp; /* transaction pointer */
1381 * Read the leaf block into the buffer.
1384 xfs_da_read_buf(tp, dp, mp->m_dirleafblk, -1, &lbp,
1390 xfs_dir2_leaf_check(dp, lbp);
1392 * Look for the first leaf entry with our hash value.
1394 index = xfs_dir2_leaf_search_hash(args, lbp);
1396 * Loop over all the entries with the right hash value
1397 * looking to match the name.
1399 for (lep = &leaf->ents[index], dbp = NULL, curdb = -1;
1400 index < INT_GET(leaf->hdr.count, ARCH_CONVERT) && INT_GET(lep->hashval, ARCH_CONVERT) == args->hashval;
1403 * Skip over stale leaf entries.
1405 if (INT_GET(lep->address, ARCH_CONVERT) == XFS_DIR2_NULL_DATAPTR)
1408 * Get the new data block number.
1410 newdb = XFS_DIR2_DATAPTR_TO_DB(mp, INT_GET(lep->address, ARCH_CONVERT));
1412 * If it's not the same as the old data block number,
1413 * need to pitch the old one and read the new one.
1415 if (newdb != curdb) {
1417 xfs_da_brelse(tp, dbp);
1419 xfs_da_read_buf(tp, dp,
1420 XFS_DIR2_DB_TO_DA(mp, newdb), -1, &dbp,
1422 xfs_da_brelse(tp, lbp);
1425 xfs_dir2_data_check(dp, dbp);
1429 * Point to the data entry.
1431 dep = (xfs_dir2_data_entry_t *)
1432 ((char *)dbp->data +
1433 XFS_DIR2_DATAPTR_TO_OFF(mp, INT_GET(lep->address, ARCH_CONVERT)));
1435 * If it matches then return it.
1437 if (dep->namelen == args->namelen &&
1438 dep->name[0] == args->name[0] &&
1439 memcmp(dep->name, args->name, args->namelen) == 0) {
1446 * No match found, return ENOENT.
1448 ASSERT(args->oknoent);
1450 xfs_da_brelse(tp, dbp);
1451 xfs_da_brelse(tp, lbp);
1452 return XFS_ERROR(ENOENT);
1456 * Remove an entry from a leaf format directory.
1459 xfs_dir2_leaf_removename(
1460 xfs_da_args_t *args) /* operation arguments */
1462 xfs_dir2_data_off_t *bestsp; /* leaf block best freespace */
1463 xfs_dir2_data_t *data; /* data block structure */
1464 xfs_dir2_db_t db; /* data block number */
1465 xfs_dabuf_t *dbp; /* data block buffer */
1466 xfs_dir2_data_entry_t *dep; /* data entry structure */
1467 xfs_inode_t *dp; /* incore directory inode */
1468 int error; /* error return code */
1469 xfs_dir2_db_t i; /* temporary data block # */
1470 int index; /* index into leaf entries */
1471 xfs_dabuf_t *lbp; /* leaf buffer */
1472 xfs_dir2_leaf_t *leaf; /* leaf structure */
1473 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
1474 xfs_dir2_leaf_tail_t *ltp; /* leaf tail structure */
1475 xfs_mount_t *mp; /* filesystem mount point */
1476 int needlog; /* need to log data header */
1477 int needscan; /* need to rescan data frees */
1478 xfs_dir2_data_off_t oldbest; /* old value of best free */
1479 xfs_trans_t *tp; /* transaction pointer */
1481 xfs_dir2_trace_args("leaf_removename", args);
1483 * Lookup the leaf entry, get the leaf and data blocks read in.
1485 if ((error = xfs_dir2_leaf_lookup_int(args, &lbp, &index, &dbp))) {
1493 xfs_dir2_data_check(dp, dbp);
1495 * Point to the leaf entry, use that to point to the data entry.
1497 lep = &leaf->ents[index];
1498 db = XFS_DIR2_DATAPTR_TO_DB(mp, INT_GET(lep->address, ARCH_CONVERT));
1499 dep = (xfs_dir2_data_entry_t *)
1500 ((char *)data + XFS_DIR2_DATAPTR_TO_OFF(mp, INT_GET(lep->address, ARCH_CONVERT)));
1501 needscan = needlog = 0;
1502 oldbest = INT_GET(data->hdr.bestfree[0].length, ARCH_CONVERT);
1503 ltp = XFS_DIR2_LEAF_TAIL_P(mp, leaf);
1504 bestsp = XFS_DIR2_LEAF_BESTS_P(ltp);
1505 ASSERT(INT_GET(bestsp[db], ARCH_CONVERT) == oldbest);
1507 * Mark the former data entry unused.
1509 xfs_dir2_data_make_free(tp, dbp,
1510 (xfs_dir2_data_aoff_t)((char *)dep - (char *)data),
1511 XFS_DIR2_DATA_ENTSIZE(dep->namelen), &needlog, &needscan);
1513 * We just mark the leaf entry stale by putting a null in it.
1515 INT_MOD(leaf->hdr.stale, ARCH_CONVERT, +1);
1516 xfs_dir2_leaf_log_header(tp, lbp);
1517 INT_SET(lep->address, ARCH_CONVERT, XFS_DIR2_NULL_DATAPTR);
1518 xfs_dir2_leaf_log_ents(tp, lbp, index, index);
1520 * Scan the freespace in the data block again if necessary,
1521 * log the data block header if necessary.
1524 xfs_dir2_data_freescan(mp, data, &needlog, NULL);
1526 xfs_dir2_data_log_header(tp, dbp);
1528 * If the longest freespace in the data block has changed,
1529 * put the new value in the bests table and log that.
1531 if (INT_GET(data->hdr.bestfree[0].length, ARCH_CONVERT) != oldbest) {
1532 INT_COPY(bestsp[db], data->hdr.bestfree[0].length, ARCH_CONVERT);
1533 xfs_dir2_leaf_log_bests(tp, lbp, db, db);
1535 xfs_dir2_data_check(dp, dbp);
1537 * If the data block is now empty then get rid of the data block.
1539 if (INT_GET(data->hdr.bestfree[0].length, ARCH_CONVERT) ==
1540 mp->m_dirblksize - (uint)sizeof(data->hdr)) {
1541 ASSERT(db != mp->m_dirdatablk);
1542 if ((error = xfs_dir2_shrink_inode(args, db, dbp))) {
1544 * Nope, can't get rid of it because it caused
1545 * allocation of a bmap btree block to do so.
1546 * Just go on, returning success, leaving the
1547 * empty block in place.
1549 if (error == ENOSPC && args->total == 0) {
1550 xfs_da_buf_done(dbp);
1553 xfs_dir2_leaf_check(dp, lbp);
1554 xfs_da_buf_done(lbp);
1559 * If this is the last data block then compact the
1560 * bests table by getting rid of entries.
1562 if (db == INT_GET(ltp->bestcount, ARCH_CONVERT) - 1) {
1564 * Look for the last active entry (i).
1566 for (i = db - 1; i > 0; i--) {
1567 if (INT_GET(bestsp[i], ARCH_CONVERT) != NULLDATAOFF)
1571 * Copy the table down so inactive entries at the
1574 memmove(&bestsp[db - i], bestsp,
1575 (INT_GET(ltp->bestcount, ARCH_CONVERT) - (db - i)) * sizeof(*bestsp));
1576 INT_MOD(ltp->bestcount, ARCH_CONVERT, -(db - i));
1577 xfs_dir2_leaf_log_tail(tp, lbp);
1578 xfs_dir2_leaf_log_bests(tp, lbp, 0, INT_GET(ltp->bestcount, ARCH_CONVERT) - 1);
1580 INT_SET(bestsp[db], ARCH_CONVERT, NULLDATAOFF);
1583 * If the data block was not the first one, drop it.
1585 else if (db != mp->m_dirdatablk && dbp != NULL) {
1586 xfs_da_buf_done(dbp);
1589 xfs_dir2_leaf_check(dp, lbp);
1591 * See if we can convert to block form.
1593 return xfs_dir2_leaf_to_block(args, lbp, dbp);
1597 * Replace the inode number in a leaf format directory entry.
1600 xfs_dir2_leaf_replace(
1601 xfs_da_args_t *args) /* operation arguments */
1603 xfs_dabuf_t *dbp; /* data block buffer */
1604 xfs_dir2_data_entry_t *dep; /* data block entry */
1605 xfs_inode_t *dp; /* incore directory inode */
1606 int error; /* error return code */
1607 int index; /* index of leaf entry */
1608 xfs_dabuf_t *lbp; /* leaf buffer */
1609 xfs_dir2_leaf_t *leaf; /* leaf structure */
1610 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
1611 xfs_trans_t *tp; /* transaction pointer */
1613 xfs_dir2_trace_args("leaf_replace", args);
1615 * Look up the entry.
1617 if ((error = xfs_dir2_leaf_lookup_int(args, &lbp, &index, &dbp))) {
1623 * Point to the leaf entry, get data address from it.
1625 lep = &leaf->ents[index];
1627 * Point to the data entry.
1629 dep = (xfs_dir2_data_entry_t *)
1630 ((char *)dbp->data +
1631 XFS_DIR2_DATAPTR_TO_OFF(dp->i_mount, INT_GET(lep->address, ARCH_CONVERT)));
1632 ASSERT(args->inumber != INT_GET(dep->inumber, ARCH_CONVERT));
1634 * Put the new inode number in, log it.
1636 INT_SET(dep->inumber, ARCH_CONVERT, args->inumber);
1638 xfs_dir2_data_log_entry(tp, dbp, dep);
1639 xfs_da_buf_done(dbp);
1640 xfs_dir2_leaf_check(dp, lbp);
1641 xfs_da_brelse(tp, lbp);
1646 * Return index in the leaf block (lbp) which is either the first
1647 * one with this hash value, or if there are none, the insert point
1648 * for that hash value.
1650 int /* index value */
1651 xfs_dir2_leaf_search_hash(
1652 xfs_da_args_t *args, /* operation arguments */
1653 xfs_dabuf_t *lbp) /* leaf buffer */
1655 xfs_dahash_t hash=0; /* hash from this entry */
1656 xfs_dahash_t hashwant; /* hash value looking for */
1657 int high; /* high leaf index */
1658 int low; /* low leaf index */
1659 xfs_dir2_leaf_t *leaf; /* leaf structure */
1660 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
1661 int mid=0; /* current leaf index */
1665 if (!leaf->hdr.count)
1669 * Note, the table cannot be empty, so we have to go through the loop.
1670 * Binary search the leaf entries looking for our hash value.
1672 for (lep = leaf->ents, low = 0, high = INT_GET(leaf->hdr.count, ARCH_CONVERT) - 1,
1673 hashwant = args->hashval;
1675 mid = (low + high) >> 1;
1676 if ((hash = INT_GET(lep[mid].hashval, ARCH_CONVERT)) == hashwant)
1678 if (hash < hashwant)
1684 * Found one, back up through all the equal hash values.
1686 if (hash == hashwant) {
1687 while (mid > 0 && INT_GET(lep[mid - 1].hashval, ARCH_CONVERT) == hashwant) {
1692 * Need to point to an entry higher than ours.
1694 else if (hash < hashwant)
1700 * Trim off a trailing data block. We know it's empty since the leaf
1701 * freespace table says so.
1704 xfs_dir2_leaf_trim_data(
1705 xfs_da_args_t *args, /* operation arguments */
1706 xfs_dabuf_t *lbp, /* leaf buffer */
1707 xfs_dir2_db_t db) /* data block number */
1709 xfs_dir2_data_off_t *bestsp; /* leaf bests table */
1711 xfs_dir2_data_t *data; /* data block structure */
1713 xfs_dabuf_t *dbp; /* data block buffer */
1714 xfs_inode_t *dp; /* incore directory inode */
1715 int error; /* error return value */
1716 xfs_dir2_leaf_t *leaf; /* leaf structure */
1717 xfs_dir2_leaf_tail_t *ltp; /* leaf tail structure */
1718 xfs_mount_t *mp; /* filesystem mount point */
1719 xfs_trans_t *tp; /* transaction pointer */
1725 * Read the offending data block. We need its buffer.
1727 if ((error = xfs_da_read_buf(tp, dp, XFS_DIR2_DB_TO_DA(mp, db), -1, &dbp,
1733 ASSERT(INT_GET(data->hdr.magic, ARCH_CONVERT) == XFS_DIR2_DATA_MAGIC);
1735 /* this seems to be an error
1736 * data is only valid if DEBUG is defined?
1741 ltp = XFS_DIR2_LEAF_TAIL_P(mp, leaf);
1742 ASSERT(INT_GET(data->hdr.bestfree[0].length, ARCH_CONVERT) ==
1743 mp->m_dirblksize - (uint)sizeof(data->hdr));
1744 ASSERT(db == INT_GET(ltp->bestcount, ARCH_CONVERT) - 1);
1746 * Get rid of the data block.
1748 if ((error = xfs_dir2_shrink_inode(args, db, dbp))) {
1749 ASSERT(error != ENOSPC);
1750 xfs_da_brelse(tp, dbp);
1754 * Eliminate the last bests entry from the table.
1756 bestsp = XFS_DIR2_LEAF_BESTS_P(ltp);
1757 INT_MOD(ltp->bestcount, ARCH_CONVERT, -1);
1758 memmove(&bestsp[1], &bestsp[0], INT_GET(ltp->bestcount, ARCH_CONVERT) * sizeof(*bestsp));
1759 xfs_dir2_leaf_log_tail(tp, lbp);
1760 xfs_dir2_leaf_log_bests(tp, lbp, 0, INT_GET(ltp->bestcount, ARCH_CONVERT) - 1);
1765 * Convert node form directory to leaf form directory.
1766 * The root of the node form dir needs to already be a LEAFN block.
1767 * Just return if we can't do anything.
1770 xfs_dir2_node_to_leaf(
1771 xfs_da_state_t *state) /* directory operation state */
1773 xfs_da_args_t *args; /* operation arguments */
1774 xfs_inode_t *dp; /* incore directory inode */
1775 int error; /* error return code */
1776 xfs_dabuf_t *fbp; /* buffer for freespace block */
1777 xfs_fileoff_t fo; /* freespace file offset */
1778 xfs_dir2_free_t *free; /* freespace structure */
1779 xfs_dabuf_t *lbp; /* buffer for leaf block */
1780 xfs_dir2_leaf_tail_t *ltp; /* tail of leaf structure */
1781 xfs_dir2_leaf_t *leaf; /* leaf structure */
1782 xfs_mount_t *mp; /* filesystem mount point */
1783 int rval; /* successful free trim? */
1784 xfs_trans_t *tp; /* transaction pointer */
1787 * There's more than a leaf level in the btree, so there must
1788 * be multiple leafn blocks. Give up.
1790 if (state->path.active > 1)
1793 xfs_dir2_trace_args("node_to_leaf", args);
1798 * Get the last offset in the file.
1800 if ((error = xfs_bmap_last_offset(tp, dp, &fo, XFS_DATA_FORK))) {
1803 fo -= mp->m_dirblkfsbs;
1805 * If there are freespace blocks other than the first one,
1806 * take this opportunity to remove trailing empty freespace blocks
1807 * that may have been left behind during no-space-reservation
1810 while (fo > mp->m_dirfreeblk) {
1811 if ((error = xfs_dir2_node_trim_free(args, fo, &rval))) {
1815 fo -= mp->m_dirblkfsbs;
1820 * Now find the block just before the freespace block.
1822 if ((error = xfs_bmap_last_before(tp, dp, &fo, XFS_DATA_FORK))) {
1826 * If it's not the single leaf block, give up.
1828 if (XFS_FSB_TO_B(mp, fo) > XFS_DIR2_LEAF_OFFSET + mp->m_dirblksize)
1830 lbp = state->path.blk[0].bp;
1832 ASSERT(INT_GET(leaf->hdr.info.magic, ARCH_CONVERT) == XFS_DIR2_LEAFN_MAGIC);
1834 * Read the freespace block.
1836 if ((error = xfs_da_read_buf(tp, dp, mp->m_dirfreeblk, -1, &fbp,
1841 ASSERT(INT_GET(free->hdr.magic, ARCH_CONVERT) == XFS_DIR2_FREE_MAGIC);
1842 ASSERT(!free->hdr.firstdb);
1844 * Now see if the leafn and free data will fit in a leaf1.
1845 * If not, release the buffer and give up.
1847 if ((uint)sizeof(leaf->hdr) +
1848 (INT_GET(leaf->hdr.count, ARCH_CONVERT) - INT_GET(leaf->hdr.stale, ARCH_CONVERT)) * (uint)sizeof(leaf->ents[0]) +
1849 INT_GET(free->hdr.nvalid, ARCH_CONVERT) * (uint)sizeof(leaf->bests[0]) +
1850 (uint)sizeof(leaf->tail) >
1852 xfs_da_brelse(tp, fbp);
1856 * If the leaf has any stale entries in it, compress them out.
1857 * The compact routine will log the header.
1859 if (INT_GET(leaf->hdr.stale, ARCH_CONVERT))
1860 xfs_dir2_leaf_compact(args, lbp);
1862 xfs_dir2_leaf_log_header(tp, lbp);
1863 INT_SET(leaf->hdr.info.magic, ARCH_CONVERT, XFS_DIR2_LEAF1_MAGIC);
1865 * Set up the leaf tail from the freespace block.
1867 ltp = XFS_DIR2_LEAF_TAIL_P(mp, leaf);
1868 INT_COPY(ltp->bestcount, free->hdr.nvalid, ARCH_CONVERT);
1870 * Set up the leaf bests table.
1872 memcpy(XFS_DIR2_LEAF_BESTS_P(ltp), free->bests,
1873 INT_GET(ltp->bestcount, ARCH_CONVERT) * sizeof(leaf->bests[0]));
1874 xfs_dir2_leaf_log_bests(tp, lbp, 0, INT_GET(ltp->bestcount, ARCH_CONVERT) - 1);
1875 xfs_dir2_leaf_log_tail(tp, lbp);
1876 xfs_dir2_leaf_check(dp, lbp);
1878 * Get rid of the freespace block.
1880 error = xfs_dir2_shrink_inode(args, XFS_DIR2_FREE_FIRSTDB(mp), fbp);
1883 * This can't fail here because it can only happen when
1884 * punching out the middle of an extent, and this is an
1887 ASSERT(error != ENOSPC);
1892 * Now see if we can convert the single-leaf directory
1893 * down to a block form directory.
1894 * This routine always kills the dabuf for the leaf, so
1895 * eliminate it from the path.
1897 error = xfs_dir2_leaf_to_block(args, lbp, NULL);
1898 state->path.blk[0].bp = NULL;