2 * Copyright (c) 2000-2003,2005 Silicon Graphics, Inc.
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation.
9 * This program is distributed in the hope that it would be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write the Free Software Foundation,
16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 #include "xfs_types.h"
24 #include "xfs_trans.h"
28 #include "xfs_mount.h"
29 #include "xfs_da_btree.h"
30 #include "xfs_bmap_btree.h"
31 #include "xfs_dir2_sf.h"
32 #include "xfs_dinode.h"
33 #include "xfs_inode.h"
35 #include "xfs_dir2_data.h"
36 #include "xfs_dir2_leaf.h"
37 #include "xfs_dir2_block.h"
38 #include "xfs_dir2_node.h"
39 #include "xfs_error.h"
40 #include "xfs_trace.h"
43 * Local function declarations.
46 static void xfs_dir2_leaf_check(xfs_inode_t *dp, xfs_dabuf_t *bp);
48 #define xfs_dir2_leaf_check(dp, bp)
50 static int xfs_dir2_leaf_lookup_int(xfs_da_args_t *args, xfs_dabuf_t **lbpp,
51 int *indexp, xfs_dabuf_t **dbpp);
52 static void xfs_dir2_leaf_log_bests(struct xfs_trans *tp, struct xfs_dabuf *bp,
54 static void xfs_dir2_leaf_log_tail(struct xfs_trans *tp, struct xfs_dabuf *bp);
58 * Convert a block form directory to a leaf form directory.
61 xfs_dir2_block_to_leaf(
62 xfs_da_args_t *args, /* operation arguments */
63 xfs_dabuf_t *dbp) /* input block's buffer */
65 __be16 *bestsp; /* leaf's bestsp entries */
66 xfs_dablk_t blkno; /* leaf block's bno */
67 xfs_dir2_data_hdr_t *hdr; /* block header */
68 xfs_dir2_leaf_entry_t *blp; /* block's leaf entries */
69 xfs_dir2_block_tail_t *btp; /* block's tail */
70 xfs_inode_t *dp; /* incore directory inode */
71 int error; /* error return code */
72 xfs_dabuf_t *lbp; /* leaf block's buffer */
73 xfs_dir2_db_t ldb; /* leaf block's bno */
74 xfs_dir2_leaf_t *leaf; /* leaf structure */
75 xfs_dir2_leaf_tail_t *ltp; /* leaf's tail */
76 xfs_mount_t *mp; /* filesystem mount point */
77 int needlog; /* need to log block header */
78 int needscan; /* need to rescan bestfree */
79 xfs_trans_t *tp; /* transaction pointer */
81 trace_xfs_dir2_block_to_leaf(args);
87 * Add the leaf block to the inode.
88 * This interface will only put blocks in the leaf/node range.
89 * Since that's empty now, we'll get the root (block 0 in range).
91 if ((error = xfs_da_grow_inode(args, &blkno))) {
94 ldb = xfs_dir2_da_to_db(mp, blkno);
95 ASSERT(ldb == XFS_DIR2_LEAF_FIRSTDB(mp));
97 * Initialize the leaf block, get a buffer for it.
99 if ((error = xfs_dir2_leaf_init(args, ldb, &lbp, XFS_DIR2_LEAF1_MAGIC))) {
105 xfs_dir2_data_check(dp, dbp);
106 btp = xfs_dir2_block_tail_p(mp, hdr);
107 blp = xfs_dir2_block_leaf_p(btp);
109 * Set the counts in the leaf header.
111 leaf->hdr.count = cpu_to_be16(be32_to_cpu(btp->count));
112 leaf->hdr.stale = cpu_to_be16(be32_to_cpu(btp->stale));
114 * Could compact these but I think we always do the conversion
115 * after squeezing out stale entries.
117 memcpy(leaf->ents, blp, be32_to_cpu(btp->count) * sizeof(xfs_dir2_leaf_entry_t));
118 xfs_dir2_leaf_log_ents(tp, lbp, 0, be16_to_cpu(leaf->hdr.count) - 1);
122 * Make the space formerly occupied by the leaf entries and block
125 xfs_dir2_data_make_free(tp, dbp,
126 (xfs_dir2_data_aoff_t)((char *)blp - (char *)hdr),
127 (xfs_dir2_data_aoff_t)((char *)hdr + mp->m_dirblksize -
129 &needlog, &needscan);
131 * Fix up the block header, make it a data block.
133 hdr->magic = cpu_to_be32(XFS_DIR2_DATA_MAGIC);
135 xfs_dir2_data_freescan(mp, hdr, &needlog);
137 * Set up leaf tail and bests table.
139 ltp = xfs_dir2_leaf_tail_p(mp, leaf);
140 ltp->bestcount = cpu_to_be32(1);
141 bestsp = xfs_dir2_leaf_bests_p(ltp);
142 bestsp[0] = hdr->bestfree[0].length;
144 * Log the data header and leaf bests table.
147 xfs_dir2_data_log_header(tp, dbp);
148 xfs_dir2_leaf_check(dp, lbp);
149 xfs_dir2_data_check(dp, dbp);
150 xfs_dir2_leaf_log_bests(tp, lbp, 0, 0);
151 xfs_da_buf_done(lbp);
155 struct xfs_dir2_leaf_entry *
156 xfs_dir2_leaf_find_entry(
157 xfs_dir2_leaf_t *leaf, /* leaf structure */
158 int index, /* leaf table position */
159 int compact, /* need to compact leaves */
160 int lowstale, /* index of prev stale leaf */
161 int highstale, /* index of next stale leaf */
162 int *lfloglow, /* low leaf logging index */
163 int *lfloghigh) /* high leaf logging index */
165 if (!leaf->hdr.stale) {
166 xfs_dir2_leaf_entry_t *lep; /* leaf entry table pointer */
169 * Now we need to make room to insert the leaf entry.
171 * If there are no stale entries, just insert a hole at index.
173 lep = &leaf->ents[index];
174 if (index < be16_to_cpu(leaf->hdr.count))
175 memmove(lep + 1, lep,
176 (be16_to_cpu(leaf->hdr.count) - index) *
180 * Record low and high logging indices for the leaf.
183 *lfloghigh = be16_to_cpu(leaf->hdr.count);
184 be16_add_cpu(&leaf->hdr.count, 1);
189 * There are stale entries.
191 * We will use one of them for the new entry. It's probably not at
192 * the right location, so we'll have to shift some up or down first.
194 * If we didn't compact before, we need to find the nearest stale
195 * entries before and after our insertion point.
199 * Find the first stale entry before the insertion point,
202 for (lowstale = index - 1;
204 be32_to_cpu(leaf->ents[lowstale].address) !=
205 XFS_DIR2_NULL_DATAPTR;
210 * Find the next stale entry at or after the insertion point,
211 * if any. Stop if we go so far that the lowstale entry
214 for (highstale = index;
215 highstale < be16_to_cpu(leaf->hdr.count) &&
216 be32_to_cpu(leaf->ents[highstale].address) !=
217 XFS_DIR2_NULL_DATAPTR &&
219 index - lowstale - 1 >= highstale - index);
225 * If the low one is better, use it.
228 (highstale == be16_to_cpu(leaf->hdr.count) ||
229 index - lowstale - 1 < highstale - index)) {
230 ASSERT(index - lowstale - 1 >= 0);
231 ASSERT(be32_to_cpu(leaf->ents[lowstale].address) ==
232 XFS_DIR2_NULL_DATAPTR);
235 * Copy entries up to cover the stale entry and make room
238 if (index - lowstale - 1 > 0) {
239 memmove(&leaf->ents[lowstale],
240 &leaf->ents[lowstale + 1],
241 (index - lowstale - 1) *
242 sizeof(xfs_dir2_leaf_entry_t));
244 *lfloglow = MIN(lowstale, *lfloglow);
245 *lfloghigh = MAX(index - 1, *lfloghigh);
246 be16_add_cpu(&leaf->hdr.stale, -1);
247 return &leaf->ents[index - 1];
251 * The high one is better, so use that one.
253 ASSERT(highstale - index >= 0);
254 ASSERT(be32_to_cpu(leaf->ents[highstale].address) ==
255 XFS_DIR2_NULL_DATAPTR);
258 * Copy entries down to cover the stale entry and make room for the
261 if (highstale - index > 0) {
262 memmove(&leaf->ents[index + 1],
264 (highstale - index) * sizeof(xfs_dir2_leaf_entry_t));
266 *lfloglow = MIN(index, *lfloglow);
267 *lfloghigh = MAX(highstale, *lfloghigh);
268 be16_add_cpu(&leaf->hdr.stale, -1);
269 return &leaf->ents[index];
273 * Add an entry to a leaf form directory.
276 xfs_dir2_leaf_addname(
277 xfs_da_args_t *args) /* operation arguments */
279 __be16 *bestsp; /* freespace table in leaf */
280 int compact; /* need to compact leaves */
281 xfs_dir2_data_hdr_t *hdr; /* data block header */
282 xfs_dabuf_t *dbp; /* data block buffer */
283 xfs_dir2_data_entry_t *dep; /* data block entry */
284 xfs_inode_t *dp; /* incore directory inode */
285 xfs_dir2_data_unused_t *dup; /* data unused entry */
286 int error; /* error return value */
287 int grown; /* allocated new data block */
288 int highstale; /* index of next stale leaf */
289 int i; /* temporary, index */
290 int index; /* leaf table position */
291 xfs_dabuf_t *lbp; /* leaf's buffer */
292 xfs_dir2_leaf_t *leaf; /* leaf structure */
293 int length; /* length of new entry */
294 xfs_dir2_leaf_entry_t *lep; /* leaf entry table pointer */
295 int lfloglow; /* low leaf logging index */
296 int lfloghigh; /* high leaf logging index */
297 int lowstale; /* index of prev stale leaf */
298 xfs_dir2_leaf_tail_t *ltp; /* leaf tail pointer */
299 xfs_mount_t *mp; /* filesystem mount point */
300 int needbytes; /* leaf block bytes needed */
301 int needlog; /* need to log data header */
302 int needscan; /* need to rescan data free */
303 __be16 *tagp; /* end of data entry */
304 xfs_trans_t *tp; /* transaction pointer */
305 xfs_dir2_db_t use_block; /* data block number */
307 trace_xfs_dir2_leaf_addname(args);
313 * Read the leaf block.
315 error = xfs_da_read_buf(tp, dp, mp->m_dirleafblk, -1, &lbp,
322 * Look up the entry by hash value and name.
323 * We know it's not there, our caller has already done a lookup.
324 * So the index is of the entry to insert in front of.
325 * But if there are dup hash values the index is of the first of those.
327 index = xfs_dir2_leaf_search_hash(args, lbp);
329 ltp = xfs_dir2_leaf_tail_p(mp, leaf);
330 bestsp = xfs_dir2_leaf_bests_p(ltp);
331 length = xfs_dir2_data_entsize(args->namelen);
333 * See if there are any entries with the same hash value
334 * and space in their block for the new entry.
335 * This is good because it puts multiple same-hash value entries
336 * in a data block, improving the lookup of those entries.
338 for (use_block = -1, lep = &leaf->ents[index];
339 index < be16_to_cpu(leaf->hdr.count) && be32_to_cpu(lep->hashval) == args->hashval;
341 if (be32_to_cpu(lep->address) == XFS_DIR2_NULL_DATAPTR)
343 i = xfs_dir2_dataptr_to_db(mp, be32_to_cpu(lep->address));
344 ASSERT(i < be32_to_cpu(ltp->bestcount));
345 ASSERT(be16_to_cpu(bestsp[i]) != NULLDATAOFF);
346 if (be16_to_cpu(bestsp[i]) >= length) {
352 * Didn't find a block yet, linear search all the data blocks.
354 if (use_block == -1) {
355 for (i = 0; i < be32_to_cpu(ltp->bestcount); i++) {
357 * Remember a block we see that's missing.
359 if (be16_to_cpu(bestsp[i]) == NULLDATAOFF && use_block == -1)
361 else if (be16_to_cpu(bestsp[i]) >= length) {
368 * How many bytes do we need in the leaf block?
371 (leaf->hdr.stale ? 0 : (uint)sizeof(leaf->ents[0])) +
372 (use_block != -1 ? 0 : (uint)sizeof(leaf->bests[0]));
374 * Now kill use_block if it refers to a missing block, so we
375 * can use it as an indication of allocation needed.
377 if (use_block != -1 && be16_to_cpu(bestsp[use_block]) == NULLDATAOFF)
380 * If we don't have enough free bytes but we can make enough
381 * by compacting out stale entries, we'll do that.
383 if ((char *)bestsp - (char *)&leaf->ents[be16_to_cpu(leaf->hdr.count)] <
384 needbytes && be16_to_cpu(leaf->hdr.stale) > 1) {
388 * Otherwise if we don't have enough free bytes we need to
389 * convert to node form.
391 else if ((char *)bestsp - (char *)&leaf->ents[be16_to_cpu(
392 leaf->hdr.count)] < needbytes) {
394 * Just checking or no space reservation, give up.
396 if ((args->op_flags & XFS_DA_OP_JUSTCHECK) ||
398 xfs_da_brelse(tp, lbp);
399 return XFS_ERROR(ENOSPC);
402 * Convert to node form.
404 error = xfs_dir2_leaf_to_node(args, lbp);
405 xfs_da_buf_done(lbp);
409 * Then add the new entry.
411 return xfs_dir2_node_addname(args);
414 * Otherwise it will fit without compaction.
419 * If just checking, then it will fit unless we needed to allocate
422 if (args->op_flags & XFS_DA_OP_JUSTCHECK) {
423 xfs_da_brelse(tp, lbp);
424 return use_block == -1 ? XFS_ERROR(ENOSPC) : 0;
427 * If no allocations are allowed, return now before we've
430 if (args->total == 0 && use_block == -1) {
431 xfs_da_brelse(tp, lbp);
432 return XFS_ERROR(ENOSPC);
435 * Need to compact the leaf entries, removing stale ones.
436 * Leave one stale entry behind - the one closest to our
437 * insertion index - and we'll shift that one to our insertion
441 xfs_dir2_leaf_compact_x1(lbp, &index, &lowstale, &highstale,
442 &lfloglow, &lfloghigh);
445 * There are stale entries, so we'll need log-low and log-high
446 * impossibly bad values later.
448 else if (be16_to_cpu(leaf->hdr.stale)) {
449 lfloglow = be16_to_cpu(leaf->hdr.count);
453 * If there was no data block space found, we need to allocate
456 if (use_block == -1) {
458 * Add the new data block.
460 if ((error = xfs_dir2_grow_inode(args, XFS_DIR2_DATA_SPACE,
462 xfs_da_brelse(tp, lbp);
466 * Initialize the block.
468 if ((error = xfs_dir2_data_init(args, use_block, &dbp))) {
469 xfs_da_brelse(tp, lbp);
473 * If we're adding a new data block on the end we need to
474 * extend the bests table. Copy it up one entry.
476 if (use_block >= be32_to_cpu(ltp->bestcount)) {
478 memmove(&bestsp[0], &bestsp[1],
479 be32_to_cpu(ltp->bestcount) * sizeof(bestsp[0]));
480 be32_add_cpu(<p->bestcount, 1);
481 xfs_dir2_leaf_log_tail(tp, lbp);
482 xfs_dir2_leaf_log_bests(tp, lbp, 0, be32_to_cpu(ltp->bestcount) - 1);
485 * If we're filling in a previously empty block just log it.
488 xfs_dir2_leaf_log_bests(tp, lbp, use_block, use_block);
490 bestsp[use_block] = hdr->bestfree[0].length;
494 * Already had space in some data block.
495 * Just read that one in.
499 xfs_da_read_buf(tp, dp, xfs_dir2_db_to_da(mp, use_block),
500 -1, &dbp, XFS_DATA_FORK))) {
501 xfs_da_brelse(tp, lbp);
507 xfs_dir2_data_check(dp, dbp);
509 * Point to the biggest freespace in our data block.
511 dup = (xfs_dir2_data_unused_t *)
512 ((char *)hdr + be16_to_cpu(hdr->bestfree[0].offset));
513 ASSERT(be16_to_cpu(dup->length) >= length);
514 needscan = needlog = 0;
516 * Mark the initial part of our freespace in use for the new entry.
518 xfs_dir2_data_use_free(tp, dbp, dup,
519 (xfs_dir2_data_aoff_t)((char *)dup - (char *)hdr), length,
520 &needlog, &needscan);
522 * Initialize our new entry (at last).
524 dep = (xfs_dir2_data_entry_t *)dup;
525 dep->inumber = cpu_to_be64(args->inumber);
526 dep->namelen = args->namelen;
527 memcpy(dep->name, args->name, dep->namelen);
528 tagp = xfs_dir2_data_entry_tag_p(dep);
529 *tagp = cpu_to_be16((char *)dep - (char *)hdr);
531 * Need to scan fix up the bestfree table.
534 xfs_dir2_data_freescan(mp, hdr, &needlog);
536 * Need to log the data block's header.
539 xfs_dir2_data_log_header(tp, dbp);
540 xfs_dir2_data_log_entry(tp, dbp, dep);
542 * If the bests table needs to be changed, do it.
543 * Log the change unless we've already done that.
545 if (be16_to_cpu(bestsp[use_block]) != be16_to_cpu(hdr->bestfree[0].length)) {
546 bestsp[use_block] = hdr->bestfree[0].length;
548 xfs_dir2_leaf_log_bests(tp, lbp, use_block, use_block);
551 lep = xfs_dir2_leaf_find_entry(leaf, index, compact, lowstale,
552 highstale, &lfloglow, &lfloghigh);
555 * Fill in the new leaf entry.
557 lep->hashval = cpu_to_be32(args->hashval);
558 lep->address = cpu_to_be32(xfs_dir2_db_off_to_dataptr(mp, use_block,
559 be16_to_cpu(*tagp)));
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(be16_to_cpu(leaf->hdr.info.magic) == 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(be16_to_cpu(leaf->hdr.count) <= 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[be16_to_cpu(leaf->hdr.count)] <=
602 (char *)xfs_dir2_leaf_bests_p(ltp));
604 * Check hash value order, count stale entries.
606 for (i = stale = 0; i < be16_to_cpu(leaf->hdr.count); i++) {
607 if (i + 1 < be16_to_cpu(leaf->hdr.count))
608 ASSERT(be32_to_cpu(leaf->ents[i].hashval) <=
609 be32_to_cpu(leaf->ents[i + 1].hashval));
610 if (be32_to_cpu(leaf->ents[i].address) == XFS_DIR2_NULL_DATAPTR)
613 ASSERT(be16_to_cpu(leaf->hdr.stale) == 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 < be16_to_cpu(leaf->hdr.count); from++) {
639 if (be32_to_cpu(leaf->ents[from].address) == 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(be16_to_cpu(leaf->hdr.stale) == from - to);
655 be16_add_cpu(&leaf->hdr.count, -(be16_to_cpu(leaf->hdr.stale)));
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(be16_to_cpu(leaf->hdr.stale) > 1);
692 * Find the first stale entry before our index, if any.
694 for (lowstale = index - 1;
696 be32_to_cpu(leaf->ents[lowstale].address) != 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 < be16_to_cpu(leaf->hdr.count) &&
705 be32_to_cpu(leaf->ents[highstale].address) != XFS_DIR2_NULL_DATAPTR &&
706 (lowstale < 0 || index - lowstale > highstale - index);
710 * Pick the better of lowstale and highstale.
713 (highstale == be16_to_cpu(leaf->hdr.count) ||
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 < be16_to_cpu(leaf->hdr.count); from++) {
724 * Notice the new value of index.
728 if (from != keepstale &&
729 be32_to_cpu(leaf->ents[from].address) == 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 be16_add_cpu(&leaf->hdr.count, -(from - to));
758 leaf->hdr.stale = cpu_to_be16(1);
760 * Remember the low/high stale value only in the "right"
763 if (lowstale >= newindex)
766 highstale = be16_to_cpu(leaf->hdr.count);
767 *highlogp = be16_to_cpu(leaf->hdr.count) - 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_inode_t *dp, /* incore directory inode */
784 xfs_dabuf_t *bp; /* data block buffer */
785 int byteoff; /* offset in current block */
786 xfs_dir2_db_t curdb; /* db for current block */
787 xfs_dir2_off_t curoff; /* current overall offset */
788 xfs_dir2_data_hdr_t *hdr; /* data block header */
789 xfs_dir2_data_entry_t *dep; /* data entry */
790 xfs_dir2_data_unused_t *dup; /* unused entry */
791 int error = 0; /* error return value */
792 int i; /* temporary loop index */
793 int j; /* temporary loop index */
794 int length; /* temporary length value */
795 xfs_bmbt_irec_t *map; /* map vector for blocks */
796 xfs_extlen_t map_blocks; /* number of fsbs in map */
797 xfs_dablk_t map_off; /* last mapped file offset */
798 int map_size; /* total entries in *map */
799 int map_valid; /* valid entries in *map */
800 xfs_mount_t *mp; /* filesystem mount point */
801 xfs_dir2_off_t newoff; /* new curoff after new blk */
802 int nmap; /* mappings to ask xfs_bmapi */
803 char *ptr = NULL; /* pointer to current data */
804 int ra_current; /* number of read-ahead blks */
805 int ra_index; /* *map index for read-ahead */
806 int ra_offset; /* map entry offset for ra */
807 int ra_want; /* readahead count wanted */
810 * If the offset is at or past the largest allowed value,
811 * give up right away.
813 if (*offset >= XFS_DIR2_MAX_DATAPTR)
819 * Set up to bmap a number of blocks based on the caller's
820 * buffer size, the directory block size, and the filesystem
823 map_size = howmany(bufsize + mp->m_dirblksize, mp->m_sb.sb_blocksize);
824 map = kmem_alloc(map_size * sizeof(*map), KM_SLEEP);
825 map_valid = ra_index = ra_offset = ra_current = map_blocks = 0;
829 * Inside the loop we keep the main offset value as a byte offset
830 * in the directory file.
832 curoff = xfs_dir2_dataptr_to_byte(mp, *offset);
835 * Force this conversion through db so we truncate the offset
836 * down to get the start of the data block.
838 map_off = xfs_dir2_db_to_da(mp, xfs_dir2_byte_to_db(mp, curoff));
840 * Loop over directory entries until we reach the end offset.
841 * Get more blocks and readahead as necessary.
843 while (curoff < XFS_DIR2_LEAF_OFFSET) {
845 * If we have no buffer, or we're off the end of the
846 * current buffer, need to get another one.
848 if (!bp || ptr >= (char *)bp->data + mp->m_dirblksize) {
850 * If we have a buffer, we need to release it and
851 * take it out of the mapping.
854 xfs_da_brelse(NULL, bp);
856 map_blocks -= mp->m_dirblkfsbs;
858 * Loop to get rid of the extents for the
861 for (i = mp->m_dirblkfsbs; i > 0; ) {
862 j = MIN((int)map->br_blockcount, i);
863 map->br_blockcount -= j;
864 map->br_startblock += j;
865 map->br_startoff += j;
867 * If mapping is done, pitch it from
870 if (!map->br_blockcount && --map_valid)
871 memmove(&map[0], &map[1],
878 * Recalculate the readahead blocks wanted.
880 ra_want = howmany(bufsize + mp->m_dirblksize,
881 mp->m_sb.sb_blocksize) - 1;
882 ASSERT(ra_want >= 0);
885 * If we don't have as many as we want, and we haven't
886 * run out of data blocks, get some more mappings.
888 if (1 + ra_want > map_blocks &&
890 xfs_dir2_byte_to_da(mp, XFS_DIR2_LEAF_OFFSET)) {
892 * Get more bmaps, fill in after the ones
893 * we already have in the table.
895 nmap = map_size - map_valid;
896 error = xfs_bmapi(NULL, dp,
898 xfs_dir2_byte_to_da(mp,
899 XFS_DIR2_LEAF_OFFSET) - map_off,
900 XFS_BMAPI_METADATA, NULL, 0,
901 &map[map_valid], &nmap, NULL);
903 * Don't know if we should ignore this or
904 * try to return an error.
905 * The trouble with returning errors
906 * is that readdir will just stop without
907 * actually passing the error through.
912 * If we got all the mappings we asked for,
913 * set the final map offset based on the
914 * last bmap value received.
915 * Otherwise, we've reached the end.
917 if (nmap == map_size - map_valid)
919 map[map_valid + nmap - 1].br_startoff +
920 map[map_valid + nmap - 1].br_blockcount;
923 xfs_dir2_byte_to_da(mp,
924 XFS_DIR2_LEAF_OFFSET);
926 * Look for holes in the mapping, and
927 * eliminate them. Count up the valid blocks.
929 for (i = map_valid; i < map_valid + nmap; ) {
930 if (map[i].br_startblock ==
933 length = map_valid + nmap - i;
941 map[i].br_blockcount;
948 * No valid mappings, so no more data blocks.
951 curoff = xfs_dir2_da_to_byte(mp, map_off);
955 * Read the directory block starting at the first
958 curdb = xfs_dir2_da_to_db(mp, map->br_startoff);
959 error = xfs_da_read_buf(NULL, dp, map->br_startoff,
960 map->br_blockcount >= mp->m_dirblkfsbs ?
961 XFS_FSB_TO_DADDR(mp, map->br_startblock) :
965 * Should just skip over the data block instead
971 * Adjust the current amount of read-ahead: we just
972 * read a block that was previously ra.
975 ra_current -= mp->m_dirblkfsbs;
977 * Do we need more readahead?
979 for (ra_index = ra_offset = i = 0;
980 ra_want > ra_current && i < map_blocks;
981 i += mp->m_dirblkfsbs) {
982 ASSERT(ra_index < map_valid);
984 * Read-ahead a contiguous directory block.
986 if (i > ra_current &&
987 map[ra_index].br_blockcount >=
989 xfs_buf_readahead(mp->m_ddev_targp,
991 map[ra_index].br_startblock +
993 (int)BTOBB(mp->m_dirblksize));
997 * Read-ahead a non-contiguous directory block.
998 * This doesn't use our mapping, but this
999 * is a very rare case.
1001 else if (i > ra_current) {
1002 (void)xfs_da_reada_buf(NULL, dp,
1003 map[ra_index].br_startoff +
1004 ra_offset, XFS_DATA_FORK);
1008 * Advance offset through the mapping table.
1010 for (j = 0; j < mp->m_dirblkfsbs; j++) {
1012 * The rest of this extent but not
1013 * more than a dir block.
1015 length = MIN(mp->m_dirblkfsbs,
1016 (int)(map[ra_index].br_blockcount -
1019 ra_offset += length;
1021 * Advance to the next mapping if
1022 * this one is used up.
1025 map[ra_index].br_blockcount) {
1032 * Having done a read, we need to set a new offset.
1034 newoff = xfs_dir2_db_off_to_byte(mp, curdb, 0);
1036 * Start of the current block.
1038 if (curoff < newoff)
1041 * Make sure we're in the right block.
1043 else if (curoff > newoff)
1044 ASSERT(xfs_dir2_byte_to_db(mp, curoff) ==
1047 xfs_dir2_data_check(dp, bp);
1049 * Find our position in the block.
1051 ptr = (char *)(hdr + 1);
1052 byteoff = xfs_dir2_byte_to_off(mp, curoff);
1054 * Skip past the header.
1057 curoff += (uint)sizeof(*hdr);
1059 * Skip past entries until we reach our offset.
1062 while ((char *)ptr - (char *)hdr < byteoff) {
1063 dup = (xfs_dir2_data_unused_t *)ptr;
1065 if (be16_to_cpu(dup->freetag)
1066 == XFS_DIR2_DATA_FREE_TAG) {
1068 length = be16_to_cpu(dup->length);
1072 dep = (xfs_dir2_data_entry_t *)ptr;
1074 xfs_dir2_data_entsize(dep->namelen);
1078 * Now set our real offset.
1081 xfs_dir2_db_off_to_byte(mp,
1082 xfs_dir2_byte_to_db(mp, curoff),
1083 (char *)ptr - (char *)hdr);
1084 if (ptr >= (char *)hdr + mp->m_dirblksize) {
1090 * We have a pointer to an entry.
1093 dup = (xfs_dir2_data_unused_t *)ptr;
1095 * No, it's unused, skip over it.
1097 if (be16_to_cpu(dup->freetag) == XFS_DIR2_DATA_FREE_TAG) {
1098 length = be16_to_cpu(dup->length);
1104 dep = (xfs_dir2_data_entry_t *)ptr;
1105 length = xfs_dir2_data_entsize(dep->namelen);
1107 if (filldir(dirent, (char *)dep->name, dep->namelen,
1108 xfs_dir2_byte_to_dataptr(mp, curoff) & 0x7fffffff,
1109 be64_to_cpu(dep->inumber), DT_UNKNOWN))
1113 * Advance to next entry in the block.
1117 /* bufsize may have just been a guess; don't go negative */
1118 bufsize = bufsize > length ? bufsize - length : 0;
1122 * All done. Set output offset value to current offset.
1124 if (curoff > xfs_dir2_dataptr_to_byte(mp, XFS_DIR2_MAX_DATAPTR))
1125 *offset = XFS_DIR2_MAX_DATAPTR & 0x7fffffff;
1127 *offset = xfs_dir2_byte_to_dataptr(mp, curoff) & 0x7fffffff;
1130 xfs_da_brelse(NULL, bp);
1135 * Initialize a new leaf block, leaf1 or leafn magic accepted.
1139 xfs_da_args_t *args, /* operation arguments */
1140 xfs_dir2_db_t bno, /* directory block number */
1141 xfs_dabuf_t **bpp, /* out: leaf buffer */
1142 int magic) /* magic number for block */
1144 xfs_dabuf_t *bp; /* leaf buffer */
1145 xfs_inode_t *dp; /* incore directory inode */
1146 int error; /* error return code */
1147 xfs_dir2_leaf_t *leaf; /* leaf structure */
1148 xfs_dir2_leaf_tail_t *ltp; /* leaf tail structure */
1149 xfs_mount_t *mp; /* filesystem mount point */
1150 xfs_trans_t *tp; /* transaction pointer */
1156 ASSERT(bno >= XFS_DIR2_LEAF_FIRSTDB(mp) &&
1157 bno < XFS_DIR2_FREE_FIRSTDB(mp));
1159 * Get the buffer for the block.
1161 error = xfs_da_get_buf(tp, dp, xfs_dir2_db_to_da(mp, bno), -1, &bp,
1169 * Initialize the header.
1171 leaf->hdr.info.magic = cpu_to_be16(magic);
1172 leaf->hdr.info.forw = 0;
1173 leaf->hdr.info.back = 0;
1174 leaf->hdr.count = 0;
1175 leaf->hdr.stale = 0;
1176 xfs_dir2_leaf_log_header(tp, bp);
1178 * If it's a leaf-format directory initialize the tail.
1179 * In this case our caller has the real bests table to copy into
1182 if (magic == XFS_DIR2_LEAF1_MAGIC) {
1183 ltp = xfs_dir2_leaf_tail_p(mp, leaf);
1185 xfs_dir2_leaf_log_tail(tp, bp);
1192 * Log the bests entries indicated from a leaf1 block.
1195 xfs_dir2_leaf_log_bests(
1196 xfs_trans_t *tp, /* transaction pointer */
1197 xfs_dabuf_t *bp, /* leaf buffer */
1198 int first, /* first entry to log */
1199 int last) /* last entry to log */
1201 __be16 *firstb; /* pointer to first entry */
1202 __be16 *lastb; /* pointer to last entry */
1203 xfs_dir2_leaf_t *leaf; /* leaf structure */
1204 xfs_dir2_leaf_tail_t *ltp; /* leaf tail structure */
1207 ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_DIR2_LEAF1_MAGIC);
1208 ltp = xfs_dir2_leaf_tail_p(tp->t_mountp, leaf);
1209 firstb = xfs_dir2_leaf_bests_p(ltp) + first;
1210 lastb = xfs_dir2_leaf_bests_p(ltp) + last;
1211 xfs_da_log_buf(tp, bp, (uint)((char *)firstb - (char *)leaf),
1212 (uint)((char *)lastb - (char *)leaf + sizeof(*lastb) - 1));
1216 * Log the leaf entries indicated from a leaf1 or leafn block.
1219 xfs_dir2_leaf_log_ents(
1220 xfs_trans_t *tp, /* transaction pointer */
1221 xfs_dabuf_t *bp, /* leaf buffer */
1222 int first, /* first entry to log */
1223 int last) /* last entry to log */
1225 xfs_dir2_leaf_entry_t *firstlep; /* pointer to first entry */
1226 xfs_dir2_leaf_entry_t *lastlep; /* pointer to last entry */
1227 xfs_dir2_leaf_t *leaf; /* leaf structure */
1230 ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_DIR2_LEAF1_MAGIC ||
1231 be16_to_cpu(leaf->hdr.info.magic) == XFS_DIR2_LEAFN_MAGIC);
1232 firstlep = &leaf->ents[first];
1233 lastlep = &leaf->ents[last];
1234 xfs_da_log_buf(tp, bp, (uint)((char *)firstlep - (char *)leaf),
1235 (uint)((char *)lastlep - (char *)leaf + sizeof(*lastlep) - 1));
1239 * Log the header of the leaf1 or leafn block.
1242 xfs_dir2_leaf_log_header(
1243 xfs_trans_t *tp, /* transaction pointer */
1244 xfs_dabuf_t *bp) /* leaf buffer */
1246 xfs_dir2_leaf_t *leaf; /* leaf structure */
1249 ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_DIR2_LEAF1_MAGIC ||
1250 be16_to_cpu(leaf->hdr.info.magic) == XFS_DIR2_LEAFN_MAGIC);
1251 xfs_da_log_buf(tp, bp, (uint)((char *)&leaf->hdr - (char *)leaf),
1252 (uint)(sizeof(leaf->hdr) - 1));
1256 * Log the tail of the leaf1 block.
1259 xfs_dir2_leaf_log_tail(
1260 xfs_trans_t *tp, /* transaction pointer */
1261 xfs_dabuf_t *bp) /* leaf buffer */
1263 xfs_dir2_leaf_t *leaf; /* leaf structure */
1264 xfs_dir2_leaf_tail_t *ltp; /* leaf tail structure */
1265 xfs_mount_t *mp; /* filesystem mount point */
1269 ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_DIR2_LEAF1_MAGIC);
1270 ltp = xfs_dir2_leaf_tail_p(mp, leaf);
1271 xfs_da_log_buf(tp, bp, (uint)((char *)ltp - (char *)leaf),
1272 (uint)(mp->m_dirblksize - 1));
1276 * Look up the entry referred to by args in the leaf format directory.
1277 * Most of the work is done by the xfs_dir2_leaf_lookup_int routine which
1278 * is also used by the node-format code.
1281 xfs_dir2_leaf_lookup(
1282 xfs_da_args_t *args) /* operation arguments */
1284 xfs_dabuf_t *dbp; /* data block buffer */
1285 xfs_dir2_data_entry_t *dep; /* data block entry */
1286 xfs_inode_t *dp; /* incore directory inode */
1287 int error; /* error return code */
1288 int index; /* found entry index */
1289 xfs_dabuf_t *lbp; /* leaf buffer */
1290 xfs_dir2_leaf_t *leaf; /* leaf structure */
1291 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
1292 xfs_trans_t *tp; /* transaction pointer */
1294 trace_xfs_dir2_leaf_lookup(args);
1297 * Look up name in the leaf block, returning both buffers and index.
1299 if ((error = xfs_dir2_leaf_lookup_int(args, &lbp, &index, &dbp))) {
1304 xfs_dir2_leaf_check(dp, lbp);
1307 * Get to the leaf entry and contained data entry address.
1309 lep = &leaf->ents[index];
1311 * Point to the data entry.
1313 dep = (xfs_dir2_data_entry_t *)
1314 ((char *)dbp->data +
1315 xfs_dir2_dataptr_to_off(dp->i_mount, be32_to_cpu(lep->address)));
1317 * Return the found inode number & CI name if appropriate
1319 args->inumber = be64_to_cpu(dep->inumber);
1320 error = xfs_dir_cilookup_result(args, dep->name, dep->namelen);
1321 xfs_da_brelse(tp, dbp);
1322 xfs_da_brelse(tp, lbp);
1323 return XFS_ERROR(error);
1327 * Look up name/hash in the leaf block.
1328 * Fill in indexp with the found index, and dbpp with the data buffer.
1329 * If not found dbpp will be NULL, and ENOENT comes back.
1330 * lbpp will always be filled in with the leaf buffer unless there's an error.
1332 static int /* error */
1333 xfs_dir2_leaf_lookup_int(
1334 xfs_da_args_t *args, /* operation arguments */
1335 xfs_dabuf_t **lbpp, /* out: leaf buffer */
1336 int *indexp, /* out: index in leaf block */
1337 xfs_dabuf_t **dbpp) /* out: data buffer */
1339 xfs_dir2_db_t curdb = -1; /* current data block number */
1340 xfs_dabuf_t *dbp = NULL; /* data buffer */
1341 xfs_dir2_data_entry_t *dep; /* data entry */
1342 xfs_inode_t *dp; /* incore directory inode */
1343 int error; /* error return code */
1344 int index; /* index in leaf block */
1345 xfs_dabuf_t *lbp; /* leaf buffer */
1346 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
1347 xfs_dir2_leaf_t *leaf; /* leaf structure */
1348 xfs_mount_t *mp; /* filesystem mount point */
1349 xfs_dir2_db_t newdb; /* new data block number */
1350 xfs_trans_t *tp; /* transaction pointer */
1351 xfs_dir2_db_t cidb = -1; /* case match data block no. */
1352 enum xfs_dacmp cmp; /* name compare result */
1358 * Read the leaf block into the buffer.
1360 error = xfs_da_read_buf(tp, dp, mp->m_dirleafblk, -1, &lbp,
1366 xfs_dir2_leaf_check(dp, lbp);
1368 * Look for the first leaf entry with our hash value.
1370 index = xfs_dir2_leaf_search_hash(args, lbp);
1372 * Loop over all the entries with the right hash value
1373 * looking to match the name.
1375 for (lep = &leaf->ents[index]; index < be16_to_cpu(leaf->hdr.count) &&
1376 be32_to_cpu(lep->hashval) == args->hashval;
1379 * Skip over stale leaf entries.
1381 if (be32_to_cpu(lep->address) == XFS_DIR2_NULL_DATAPTR)
1384 * Get the new data block number.
1386 newdb = xfs_dir2_dataptr_to_db(mp, be32_to_cpu(lep->address));
1388 * If it's not the same as the old data block number,
1389 * need to pitch the old one and read the new one.
1391 if (newdb != curdb) {
1393 xfs_da_brelse(tp, dbp);
1394 error = xfs_da_read_buf(tp, dp,
1395 xfs_dir2_db_to_da(mp, newdb),
1396 -1, &dbp, XFS_DATA_FORK);
1398 xfs_da_brelse(tp, lbp);
1401 xfs_dir2_data_check(dp, dbp);
1405 * Point to the data entry.
1407 dep = (xfs_dir2_data_entry_t *)((char *)dbp->data +
1408 xfs_dir2_dataptr_to_off(mp, be32_to_cpu(lep->address)));
1410 * Compare name and if it's an exact match, return the index
1411 * and buffer. If it's the first case-insensitive match, store
1412 * the index and buffer and continue looking for an exact match.
1414 cmp = mp->m_dirnameops->compname(args, dep->name, dep->namelen);
1415 if (cmp != XFS_CMP_DIFFERENT && cmp != args->cmpresult) {
1416 args->cmpresult = cmp;
1418 /* case exact match: return the current buffer. */
1419 if (cmp == XFS_CMP_EXACT) {
1426 ASSERT(args->op_flags & XFS_DA_OP_OKNOENT);
1428 * Here, we can only be doing a lookup (not a rename or remove).
1429 * If a case-insensitive match was found earlier, re-read the
1430 * appropriate data block if required and return it.
1432 if (args->cmpresult == XFS_CMP_CASE) {
1434 if (cidb != curdb) {
1435 xfs_da_brelse(tp, dbp);
1436 error = xfs_da_read_buf(tp, dp,
1437 xfs_dir2_db_to_da(mp, cidb),
1438 -1, &dbp, XFS_DATA_FORK);
1440 xfs_da_brelse(tp, lbp);
1448 * No match found, return ENOENT.
1452 xfs_da_brelse(tp, dbp);
1453 xfs_da_brelse(tp, lbp);
1454 return XFS_ERROR(ENOENT);
1458 * Remove an entry from a leaf format directory.
1461 xfs_dir2_leaf_removename(
1462 xfs_da_args_t *args) /* operation arguments */
1464 __be16 *bestsp; /* leaf block best freespace */
1465 xfs_dir2_data_hdr_t *hdr; /* data block header */
1466 xfs_dir2_db_t db; /* data block number */
1467 xfs_dabuf_t *dbp; /* data block buffer */
1468 xfs_dir2_data_entry_t *dep; /* data entry structure */
1469 xfs_inode_t *dp; /* incore directory inode */
1470 int error; /* error return code */
1471 xfs_dir2_db_t i; /* temporary data block # */
1472 int index; /* index into leaf entries */
1473 xfs_dabuf_t *lbp; /* leaf buffer */
1474 xfs_dir2_leaf_t *leaf; /* leaf structure */
1475 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
1476 xfs_dir2_leaf_tail_t *ltp; /* leaf tail structure */
1477 xfs_mount_t *mp; /* filesystem mount point */
1478 int needlog; /* need to log data header */
1479 int needscan; /* need to rescan data frees */
1480 xfs_dir2_data_off_t oldbest; /* old value of best free */
1481 xfs_trans_t *tp; /* transaction pointer */
1483 trace_xfs_dir2_leaf_removename(args);
1486 * Lookup the leaf entry, get the leaf and data blocks read in.
1488 if ((error = xfs_dir2_leaf_lookup_int(args, &lbp, &index, &dbp))) {
1496 xfs_dir2_data_check(dp, dbp);
1498 * Point to the leaf entry, use that to point to the data entry.
1500 lep = &leaf->ents[index];
1501 db = xfs_dir2_dataptr_to_db(mp, be32_to_cpu(lep->address));
1502 dep = (xfs_dir2_data_entry_t *)
1503 ((char *)hdr + xfs_dir2_dataptr_to_off(mp, be32_to_cpu(lep->address)));
1504 needscan = needlog = 0;
1505 oldbest = be16_to_cpu(hdr->bestfree[0].length);
1506 ltp = xfs_dir2_leaf_tail_p(mp, leaf);
1507 bestsp = xfs_dir2_leaf_bests_p(ltp);
1508 ASSERT(be16_to_cpu(bestsp[db]) == oldbest);
1510 * Mark the former data entry unused.
1512 xfs_dir2_data_make_free(tp, dbp,
1513 (xfs_dir2_data_aoff_t)((char *)dep - (char *)hdr),
1514 xfs_dir2_data_entsize(dep->namelen), &needlog, &needscan);
1516 * We just mark the leaf entry stale by putting a null in it.
1518 be16_add_cpu(&leaf->hdr.stale, 1);
1519 xfs_dir2_leaf_log_header(tp, lbp);
1520 lep->address = cpu_to_be32(XFS_DIR2_NULL_DATAPTR);
1521 xfs_dir2_leaf_log_ents(tp, lbp, index, index);
1523 * Scan the freespace in the data block again if necessary,
1524 * log the data block header if necessary.
1527 xfs_dir2_data_freescan(mp, hdr, &needlog);
1529 xfs_dir2_data_log_header(tp, dbp);
1531 * If the longest freespace in the data block has changed,
1532 * put the new value in the bests table and log that.
1534 if (be16_to_cpu(hdr->bestfree[0].length) != oldbest) {
1535 bestsp[db] = hdr->bestfree[0].length;
1536 xfs_dir2_leaf_log_bests(tp, lbp, db, db);
1538 xfs_dir2_data_check(dp, dbp);
1540 * If the data block is now empty then get rid of the data block.
1542 if (be16_to_cpu(hdr->bestfree[0].length) ==
1543 mp->m_dirblksize - (uint)sizeof(*hdr)) {
1544 ASSERT(db != mp->m_dirdatablk);
1545 if ((error = xfs_dir2_shrink_inode(args, db, dbp))) {
1547 * Nope, can't get rid of it because it caused
1548 * allocation of a bmap btree block to do so.
1549 * Just go on, returning success, leaving the
1550 * empty block in place.
1552 if (error == ENOSPC && args->total == 0) {
1553 xfs_da_buf_done(dbp);
1556 xfs_dir2_leaf_check(dp, lbp);
1557 xfs_da_buf_done(lbp);
1562 * If this is the last data block then compact the
1563 * bests table by getting rid of entries.
1565 if (db == be32_to_cpu(ltp->bestcount) - 1) {
1567 * Look for the last active entry (i).
1569 for (i = db - 1; i > 0; i--) {
1570 if (be16_to_cpu(bestsp[i]) != NULLDATAOFF)
1574 * Copy the table down so inactive entries at the
1577 memmove(&bestsp[db - i], bestsp,
1578 (be32_to_cpu(ltp->bestcount) - (db - i)) * sizeof(*bestsp));
1579 be32_add_cpu(<p->bestcount, -(db - i));
1580 xfs_dir2_leaf_log_tail(tp, lbp);
1581 xfs_dir2_leaf_log_bests(tp, lbp, 0, be32_to_cpu(ltp->bestcount) - 1);
1583 bestsp[db] = cpu_to_be16(NULLDATAOFF);
1586 * If the data block was not the first one, drop it.
1588 else if (db != mp->m_dirdatablk && dbp != NULL) {
1589 xfs_da_buf_done(dbp);
1592 xfs_dir2_leaf_check(dp, lbp);
1594 * See if we can convert to block form.
1596 return xfs_dir2_leaf_to_block(args, lbp, dbp);
1600 * Replace the inode number in a leaf format directory entry.
1603 xfs_dir2_leaf_replace(
1604 xfs_da_args_t *args) /* operation arguments */
1606 xfs_dabuf_t *dbp; /* data block buffer */
1607 xfs_dir2_data_entry_t *dep; /* data block entry */
1608 xfs_inode_t *dp; /* incore directory inode */
1609 int error; /* error return code */
1610 int index; /* index of leaf entry */
1611 xfs_dabuf_t *lbp; /* leaf buffer */
1612 xfs_dir2_leaf_t *leaf; /* leaf structure */
1613 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
1614 xfs_trans_t *tp; /* transaction pointer */
1616 trace_xfs_dir2_leaf_replace(args);
1619 * Look up the entry.
1621 if ((error = xfs_dir2_leaf_lookup_int(args, &lbp, &index, &dbp))) {
1627 * Point to the leaf entry, get data address from it.
1629 lep = &leaf->ents[index];
1631 * Point to the data entry.
1633 dep = (xfs_dir2_data_entry_t *)
1634 ((char *)dbp->data +
1635 xfs_dir2_dataptr_to_off(dp->i_mount, be32_to_cpu(lep->address)));
1636 ASSERT(args->inumber != be64_to_cpu(dep->inumber));
1638 * Put the new inode number in, log it.
1640 dep->inumber = cpu_to_be64(args->inumber);
1642 xfs_dir2_data_log_entry(tp, dbp, dep);
1643 xfs_da_buf_done(dbp);
1644 xfs_dir2_leaf_check(dp, lbp);
1645 xfs_da_brelse(tp, lbp);
1650 * Return index in the leaf block (lbp) which is either the first
1651 * one with this hash value, or if there are none, the insert point
1652 * for that hash value.
1654 int /* index value */
1655 xfs_dir2_leaf_search_hash(
1656 xfs_da_args_t *args, /* operation arguments */
1657 xfs_dabuf_t *lbp) /* leaf buffer */
1659 xfs_dahash_t hash=0; /* hash from this entry */
1660 xfs_dahash_t hashwant; /* hash value looking for */
1661 int high; /* high leaf index */
1662 int low; /* low leaf index */
1663 xfs_dir2_leaf_t *leaf; /* leaf structure */
1664 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
1665 int mid=0; /* current leaf index */
1669 if (!leaf->hdr.count)
1673 * Note, the table cannot be empty, so we have to go through the loop.
1674 * Binary search the leaf entries looking for our hash value.
1676 for (lep = leaf->ents, low = 0, high = be16_to_cpu(leaf->hdr.count) - 1,
1677 hashwant = args->hashval;
1679 mid = (low + high) >> 1;
1680 if ((hash = be32_to_cpu(lep[mid].hashval)) == hashwant)
1682 if (hash < hashwant)
1688 * Found one, back up through all the equal hash values.
1690 if (hash == hashwant) {
1691 while (mid > 0 && be32_to_cpu(lep[mid - 1].hashval) == hashwant) {
1696 * Need to point to an entry higher than ours.
1698 else if (hash < hashwant)
1704 * Trim off a trailing data block. We know it's empty since the leaf
1705 * freespace table says so.
1708 xfs_dir2_leaf_trim_data(
1709 xfs_da_args_t *args, /* operation arguments */
1710 xfs_dabuf_t *lbp, /* leaf buffer */
1711 xfs_dir2_db_t db) /* data block number */
1713 __be16 *bestsp; /* leaf bests table */
1714 xfs_dabuf_t *dbp; /* data block buffer */
1715 xfs_inode_t *dp; /* incore directory inode */
1716 int error; /* error return value */
1717 xfs_dir2_leaf_t *leaf; /* leaf structure */
1718 xfs_dir2_leaf_tail_t *ltp; /* leaf tail structure */
1719 xfs_mount_t *mp; /* filesystem mount point */
1720 xfs_trans_t *tp; /* transaction pointer */
1726 * Read the offending data block. We need its buffer.
1728 if ((error = xfs_da_read_buf(tp, dp, xfs_dir2_db_to_da(mp, db), -1, &dbp,
1734 ltp = xfs_dir2_leaf_tail_p(mp, leaf);
1738 struct xfs_dir2_data_hdr *hdr = dbp->data;
1740 ASSERT(be32_to_cpu(hdr->magic) == XFS_DIR2_DATA_MAGIC);
1741 ASSERT(be16_to_cpu(hdr->bestfree[0].length) ==
1742 mp->m_dirblksize - (uint)sizeof(*hdr));
1743 ASSERT(db == be32_to_cpu(ltp->bestcount) - 1);
1748 * Get rid of the data block.
1750 if ((error = xfs_dir2_shrink_inode(args, db, dbp))) {
1751 ASSERT(error != ENOSPC);
1752 xfs_da_brelse(tp, dbp);
1756 * Eliminate the last bests entry from the table.
1758 bestsp = xfs_dir2_leaf_bests_p(ltp);
1759 be32_add_cpu(<p->bestcount, -1);
1760 memmove(&bestsp[1], &bestsp[0], be32_to_cpu(ltp->bestcount) * sizeof(*bestsp));
1761 xfs_dir2_leaf_log_tail(tp, lbp);
1762 xfs_dir2_leaf_log_bests(tp, lbp, 0, be32_to_cpu(ltp->bestcount) - 1);
1767 * Convert node form directory to leaf form directory.
1768 * The root of the node form dir needs to already be a LEAFN block.
1769 * Just return if we can't do anything.
1772 xfs_dir2_node_to_leaf(
1773 xfs_da_state_t *state) /* directory operation state */
1775 xfs_da_args_t *args; /* operation arguments */
1776 xfs_inode_t *dp; /* incore directory inode */
1777 int error; /* error return code */
1778 xfs_dabuf_t *fbp; /* buffer for freespace block */
1779 xfs_fileoff_t fo; /* freespace file offset */
1780 xfs_dir2_free_t *free; /* freespace structure */
1781 xfs_dabuf_t *lbp; /* buffer for leaf block */
1782 xfs_dir2_leaf_tail_t *ltp; /* tail of leaf structure */
1783 xfs_dir2_leaf_t *leaf; /* leaf structure */
1784 xfs_mount_t *mp; /* filesystem mount point */
1785 int rval; /* successful free trim? */
1786 xfs_trans_t *tp; /* transaction pointer */
1789 * There's more than a leaf level in the btree, so there must
1790 * be multiple leafn blocks. Give up.
1792 if (state->path.active > 1)
1796 trace_xfs_dir2_node_to_leaf(args);
1802 * Get the last offset in the file.
1804 if ((error = xfs_bmap_last_offset(tp, dp, &fo, XFS_DATA_FORK))) {
1807 fo -= mp->m_dirblkfsbs;
1809 * If there are freespace blocks other than the first one,
1810 * take this opportunity to remove trailing empty freespace blocks
1811 * that may have been left behind during no-space-reservation
1814 while (fo > mp->m_dirfreeblk) {
1815 if ((error = xfs_dir2_node_trim_free(args, fo, &rval))) {
1819 fo -= mp->m_dirblkfsbs;
1824 * Now find the block just before the freespace block.
1826 if ((error = xfs_bmap_last_before(tp, dp, &fo, XFS_DATA_FORK))) {
1830 * If it's not the single leaf block, give up.
1832 if (XFS_FSB_TO_B(mp, fo) > XFS_DIR2_LEAF_OFFSET + mp->m_dirblksize)
1834 lbp = state->path.blk[0].bp;
1836 ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_DIR2_LEAFN_MAGIC);
1838 * Read the freespace block.
1840 if ((error = xfs_da_read_buf(tp, dp, mp->m_dirfreeblk, -1, &fbp,
1845 ASSERT(be32_to_cpu(free->hdr.magic) == XFS_DIR2_FREE_MAGIC);
1846 ASSERT(!free->hdr.firstdb);
1848 * Now see if the leafn and free data will fit in a leaf1.
1849 * If not, release the buffer and give up.
1851 if ((uint)sizeof(leaf->hdr) +
1852 (be16_to_cpu(leaf->hdr.count) - be16_to_cpu(leaf->hdr.stale)) * (uint)sizeof(leaf->ents[0]) +
1853 be32_to_cpu(free->hdr.nvalid) * (uint)sizeof(leaf->bests[0]) +
1854 (uint)sizeof(leaf->tail) >
1856 xfs_da_brelse(tp, fbp);
1860 * If the leaf has any stale entries in it, compress them out.
1861 * The compact routine will log the header.
1863 if (be16_to_cpu(leaf->hdr.stale))
1864 xfs_dir2_leaf_compact(args, lbp);
1866 xfs_dir2_leaf_log_header(tp, lbp);
1867 leaf->hdr.info.magic = cpu_to_be16(XFS_DIR2_LEAF1_MAGIC);
1869 * Set up the leaf tail from the freespace block.
1871 ltp = xfs_dir2_leaf_tail_p(mp, leaf);
1872 ltp->bestcount = free->hdr.nvalid;
1874 * Set up the leaf bests table.
1876 memcpy(xfs_dir2_leaf_bests_p(ltp), free->bests,
1877 be32_to_cpu(ltp->bestcount) * sizeof(leaf->bests[0]));
1878 xfs_dir2_leaf_log_bests(tp, lbp, 0, be32_to_cpu(ltp->bestcount) - 1);
1879 xfs_dir2_leaf_log_tail(tp, lbp);
1880 xfs_dir2_leaf_check(dp, lbp);
1882 * Get rid of the freespace block.
1884 error = xfs_dir2_shrink_inode(args, XFS_DIR2_FREE_FIRSTDB(mp), fbp);
1887 * This can't fail here because it can only happen when
1888 * punching out the middle of an extent, and this is an
1891 ASSERT(error != ENOSPC);
1896 * Now see if we can convert the single-leaf directory
1897 * down to a block form directory.
1898 * This routine always kills the dabuf for the leaf, so
1899 * eliminate it from the path.
1901 error = xfs_dir2_leaf_to_block(args, lbp, NULL);
1902 state->path.blk[0].bp = NULL;