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"
23 #include "xfs_trans.h"
26 #include "xfs_mount.h"
27 #include "xfs_da_btree.h"
28 #include "xfs_bmap_btree.h"
29 #include "xfs_dinode.h"
30 #include "xfs_inode.h"
32 #include "xfs_dir2_format.h"
33 #include "xfs_dir2_priv.h"
34 #include "xfs_error.h"
35 #include "xfs_trace.h"
38 * Local function declarations.
41 static void xfs_dir2_leaf_check(struct xfs_inode *dp, struct xfs_buf *bp);
43 #define xfs_dir2_leaf_check(dp, bp)
45 static int xfs_dir2_leaf_lookup_int(xfs_da_args_t *args, struct xfs_buf **lbpp,
46 int *indexp, struct xfs_buf **dbpp);
47 static void xfs_dir2_leaf_log_bests(struct xfs_trans *tp, struct xfs_buf *bp,
49 static void xfs_dir2_leaf_log_tail(struct xfs_trans *tp, struct xfs_buf *bp);
53 * Convert a block form directory to a leaf form directory.
56 xfs_dir2_block_to_leaf(
57 xfs_da_args_t *args, /* operation arguments */
58 struct xfs_buf *dbp) /* input block's buffer */
60 __be16 *bestsp; /* leaf's bestsp entries */
61 xfs_dablk_t blkno; /* leaf block's bno */
62 xfs_dir2_data_hdr_t *hdr; /* block header */
63 xfs_dir2_leaf_entry_t *blp; /* block's leaf entries */
64 xfs_dir2_block_tail_t *btp; /* block's tail */
65 xfs_inode_t *dp; /* incore directory inode */
66 int error; /* error return code */
67 struct xfs_buf *lbp; /* leaf block's buffer */
68 xfs_dir2_db_t ldb; /* leaf block's bno */
69 xfs_dir2_leaf_t *leaf; /* leaf structure */
70 xfs_dir2_leaf_tail_t *ltp; /* leaf's tail */
71 xfs_mount_t *mp; /* filesystem mount point */
72 int needlog; /* need to log block header */
73 int needscan; /* need to rescan bestfree */
74 xfs_trans_t *tp; /* transaction pointer */
76 trace_xfs_dir2_block_to_leaf(args);
82 * Add the leaf block to the inode.
83 * This interface will only put blocks in the leaf/node range.
84 * Since that's empty now, we'll get the root (block 0 in range).
86 if ((error = xfs_da_grow_inode(args, &blkno))) {
89 ldb = xfs_dir2_da_to_db(mp, blkno);
90 ASSERT(ldb == XFS_DIR2_LEAF_FIRSTDB(mp));
92 * Initialize the leaf block, get a buffer for it.
94 if ((error = xfs_dir2_leaf_init(args, ldb, &lbp, XFS_DIR2_LEAF1_MAGIC))) {
100 xfs_dir2_data_check(dp, dbp);
101 btp = xfs_dir2_block_tail_p(mp, hdr);
102 blp = xfs_dir2_block_leaf_p(btp);
104 * Set the counts in the leaf header.
106 leaf->hdr.count = cpu_to_be16(be32_to_cpu(btp->count));
107 leaf->hdr.stale = cpu_to_be16(be32_to_cpu(btp->stale));
109 * Could compact these but I think we always do the conversion
110 * after squeezing out stale entries.
112 memcpy(leaf->ents, blp, be32_to_cpu(btp->count) * sizeof(xfs_dir2_leaf_entry_t));
113 xfs_dir2_leaf_log_ents(tp, lbp, 0, be16_to_cpu(leaf->hdr.count) - 1);
117 * Make the space formerly occupied by the leaf entries and block
120 xfs_dir2_data_make_free(tp, dbp,
121 (xfs_dir2_data_aoff_t)((char *)blp - (char *)hdr),
122 (xfs_dir2_data_aoff_t)((char *)hdr + mp->m_dirblksize -
124 &needlog, &needscan);
126 * Fix up the block header, make it a data block.
128 hdr->magic = cpu_to_be32(XFS_DIR2_DATA_MAGIC);
130 xfs_dir2_data_freescan(mp, hdr, &needlog);
132 * Set up leaf tail and bests table.
134 ltp = xfs_dir2_leaf_tail_p(mp, leaf);
135 ltp->bestcount = cpu_to_be32(1);
136 bestsp = xfs_dir2_leaf_bests_p(ltp);
137 bestsp[0] = hdr->bestfree[0].length;
139 * Log the data header and leaf bests table.
142 xfs_dir2_data_log_header(tp, dbp);
143 xfs_dir2_leaf_check(dp, lbp);
144 xfs_dir2_data_check(dp, dbp);
145 xfs_dir2_leaf_log_bests(tp, lbp, 0, 0);
150 xfs_dir2_leaf_find_stale(
151 struct xfs_dir2_leaf *leaf,
157 * Find the first stale entry before our index, if any.
159 for (*lowstale = index - 1; *lowstale >= 0; --*lowstale) {
160 if (leaf->ents[*lowstale].address ==
161 cpu_to_be32(XFS_DIR2_NULL_DATAPTR))
166 * Find the first stale entry at or after our index, if any.
167 * Stop if the result would require moving more entries than using
170 for (*highstale = index;
171 *highstale < be16_to_cpu(leaf->hdr.count);
173 if (leaf->ents[*highstale].address ==
174 cpu_to_be32(XFS_DIR2_NULL_DATAPTR))
176 if (*lowstale >= 0 && index - *lowstale <= *highstale - index)
181 struct xfs_dir2_leaf_entry *
182 xfs_dir2_leaf_find_entry(
183 xfs_dir2_leaf_t *leaf, /* leaf structure */
184 int index, /* leaf table position */
185 int compact, /* need to compact leaves */
186 int lowstale, /* index of prev stale leaf */
187 int highstale, /* index of next stale leaf */
188 int *lfloglow, /* low leaf logging index */
189 int *lfloghigh) /* high leaf logging index */
191 if (!leaf->hdr.stale) {
192 xfs_dir2_leaf_entry_t *lep; /* leaf entry table pointer */
195 * Now we need to make room to insert the leaf entry.
197 * If there are no stale entries, just insert a hole at index.
199 lep = &leaf->ents[index];
200 if (index < be16_to_cpu(leaf->hdr.count))
201 memmove(lep + 1, lep,
202 (be16_to_cpu(leaf->hdr.count) - index) *
206 * Record low and high logging indices for the leaf.
209 *lfloghigh = be16_to_cpu(leaf->hdr.count);
210 be16_add_cpu(&leaf->hdr.count, 1);
215 * There are stale entries.
217 * We will use one of them for the new entry. It's probably not at
218 * the right location, so we'll have to shift some up or down first.
220 * If we didn't compact before, we need to find the nearest stale
221 * entries before and after our insertion point.
224 xfs_dir2_leaf_find_stale(leaf, index, &lowstale, &highstale);
227 * If the low one is better, use it.
230 (highstale == be16_to_cpu(leaf->hdr.count) ||
231 index - lowstale - 1 < highstale - index)) {
232 ASSERT(index - lowstale - 1 >= 0);
233 ASSERT(leaf->ents[lowstale].address ==
234 cpu_to_be32(XFS_DIR2_NULL_DATAPTR));
237 * Copy entries up to cover the stale entry and make room
240 if (index - lowstale - 1 > 0) {
241 memmove(&leaf->ents[lowstale],
242 &leaf->ents[lowstale + 1],
243 (index - lowstale - 1) *
244 sizeof(xfs_dir2_leaf_entry_t));
246 *lfloglow = MIN(lowstale, *lfloglow);
247 *lfloghigh = MAX(index - 1, *lfloghigh);
248 be16_add_cpu(&leaf->hdr.stale, -1);
249 return &leaf->ents[index - 1];
253 * The high one is better, so use that one.
255 ASSERT(highstale - index >= 0);
256 ASSERT(leaf->ents[highstale].address ==
257 cpu_to_be32(XFS_DIR2_NULL_DATAPTR));
260 * Copy entries down to cover the stale entry and make room for the
263 if (highstale - index > 0) {
264 memmove(&leaf->ents[index + 1],
266 (highstale - index) * sizeof(xfs_dir2_leaf_entry_t));
268 *lfloglow = MIN(index, *lfloglow);
269 *lfloghigh = MAX(highstale, *lfloghigh);
270 be16_add_cpu(&leaf->hdr.stale, -1);
271 return &leaf->ents[index];
275 * Add an entry to a leaf form directory.
278 xfs_dir2_leaf_addname(
279 xfs_da_args_t *args) /* operation arguments */
281 __be16 *bestsp; /* freespace table in leaf */
282 int compact; /* need to compact leaves */
283 xfs_dir2_data_hdr_t *hdr; /* data block header */
284 struct xfs_buf *dbp; /* data block buffer */
285 xfs_dir2_data_entry_t *dep; /* data block entry */
286 xfs_inode_t *dp; /* incore directory inode */
287 xfs_dir2_data_unused_t *dup; /* data unused entry */
288 int error; /* error return value */
289 int grown; /* allocated new data block */
290 int highstale; /* index of next stale leaf */
291 int i; /* temporary, index */
292 int index; /* leaf table position */
293 struct xfs_buf *lbp; /* leaf's buffer */
294 xfs_dir2_leaf_t *leaf; /* leaf structure */
295 int length; /* length of new entry */
296 xfs_dir2_leaf_entry_t *lep; /* leaf entry table pointer */
297 int lfloglow; /* low leaf logging index */
298 int lfloghigh; /* high leaf logging index */
299 int lowstale; /* index of prev stale leaf */
300 xfs_dir2_leaf_tail_t *ltp; /* leaf tail pointer */
301 xfs_mount_t *mp; /* filesystem mount point */
302 int needbytes; /* leaf block bytes needed */
303 int needlog; /* need to log data header */
304 int needscan; /* need to rescan data free */
305 __be16 *tagp; /* end of data entry */
306 xfs_trans_t *tp; /* transaction pointer */
307 xfs_dir2_db_t use_block; /* data block number */
309 trace_xfs_dir2_leaf_addname(args);
315 * Read the leaf block.
317 error = xfs_da_read_buf(tp, dp, mp->m_dirleafblk, -1, &lbp,
318 XFS_DATA_FORK, NULL);
323 * Look up the entry by hash value and name.
324 * We know it's not there, our caller has already done a lookup.
325 * So the index is of the entry to insert in front of.
326 * But if there are dup hash values the index is of the first of those.
328 index = xfs_dir2_leaf_search_hash(args, lbp);
330 ltp = xfs_dir2_leaf_tail_p(mp, leaf);
331 bestsp = xfs_dir2_leaf_bests_p(ltp);
332 length = xfs_dir2_data_entsize(args->namelen);
334 * See if there are any entries with the same hash value
335 * and space in their block for the new entry.
336 * This is good because it puts multiple same-hash value entries
337 * in a data block, improving the lookup of those entries.
339 for (use_block = -1, lep = &leaf->ents[index];
340 index < be16_to_cpu(leaf->hdr.count) && be32_to_cpu(lep->hashval) == args->hashval;
342 if (be32_to_cpu(lep->address) == XFS_DIR2_NULL_DATAPTR)
344 i = xfs_dir2_dataptr_to_db(mp, be32_to_cpu(lep->address));
345 ASSERT(i < be32_to_cpu(ltp->bestcount));
346 ASSERT(bestsp[i] != cpu_to_be16(NULLDATAOFF));
347 if (be16_to_cpu(bestsp[i]) >= length) {
353 * Didn't find a block yet, linear search all the data blocks.
355 if (use_block == -1) {
356 for (i = 0; i < be32_to_cpu(ltp->bestcount); i++) {
358 * Remember a block we see that's missing.
360 if (bestsp[i] == cpu_to_be16(NULLDATAOFF) &&
363 else if (be16_to_cpu(bestsp[i]) >= length) {
370 * How many bytes do we need in the leaf block?
373 if (!leaf->hdr.stale)
374 needbytes += sizeof(xfs_dir2_leaf_entry_t);
376 needbytes += sizeof(xfs_dir2_data_off_t);
379 * Now kill use_block if it refers to a missing block, so we
380 * can use it as an indication of allocation needed.
382 if (use_block != -1 && bestsp[use_block] == cpu_to_be16(NULLDATAOFF))
385 * If we don't have enough free bytes but we can make enough
386 * by compacting out stale entries, we'll do that.
388 if ((char *)bestsp - (char *)&leaf->ents[be16_to_cpu(leaf->hdr.count)] <
389 needbytes && be16_to_cpu(leaf->hdr.stale) > 1) {
393 * Otherwise if we don't have enough free bytes we need to
394 * convert to node form.
396 else if ((char *)bestsp - (char *)&leaf->ents[be16_to_cpu(
397 leaf->hdr.count)] < needbytes) {
399 * Just checking or no space reservation, give up.
401 if ((args->op_flags & XFS_DA_OP_JUSTCHECK) ||
403 xfs_trans_brelse(tp, lbp);
404 return XFS_ERROR(ENOSPC);
407 * Convert to node form.
409 error = xfs_dir2_leaf_to_node(args, lbp);
413 * Then add the new entry.
415 return xfs_dir2_node_addname(args);
418 * Otherwise it will fit without compaction.
423 * If just checking, then it will fit unless we needed to allocate
426 if (args->op_flags & XFS_DA_OP_JUSTCHECK) {
427 xfs_trans_brelse(tp, lbp);
428 return use_block == -1 ? XFS_ERROR(ENOSPC) : 0;
431 * If no allocations are allowed, return now before we've
434 if (args->total == 0 && use_block == -1) {
435 xfs_trans_brelse(tp, lbp);
436 return XFS_ERROR(ENOSPC);
439 * Need to compact the leaf entries, removing stale ones.
440 * Leave one stale entry behind - the one closest to our
441 * insertion index - and we'll shift that one to our insertion
445 xfs_dir2_leaf_compact_x1(lbp, &index, &lowstale, &highstale,
446 &lfloglow, &lfloghigh);
449 * There are stale entries, so we'll need log-low and log-high
450 * impossibly bad values later.
452 else if (be16_to_cpu(leaf->hdr.stale)) {
453 lfloglow = be16_to_cpu(leaf->hdr.count);
457 * If there was no data block space found, we need to allocate
460 if (use_block == -1) {
462 * Add the new data block.
464 if ((error = xfs_dir2_grow_inode(args, XFS_DIR2_DATA_SPACE,
466 xfs_trans_brelse(tp, lbp);
470 * Initialize the block.
472 if ((error = xfs_dir2_data_init(args, use_block, &dbp))) {
473 xfs_trans_brelse(tp, lbp);
477 * If we're adding a new data block on the end we need to
478 * extend the bests table. Copy it up one entry.
480 if (use_block >= be32_to_cpu(ltp->bestcount)) {
482 memmove(&bestsp[0], &bestsp[1],
483 be32_to_cpu(ltp->bestcount) * sizeof(bestsp[0]));
484 be32_add_cpu(<p->bestcount, 1);
485 xfs_dir2_leaf_log_tail(tp, lbp);
486 xfs_dir2_leaf_log_bests(tp, lbp, 0, be32_to_cpu(ltp->bestcount) - 1);
489 * If we're filling in a previously empty block just log it.
492 xfs_dir2_leaf_log_bests(tp, lbp, use_block, use_block);
494 bestsp[use_block] = hdr->bestfree[0].length;
498 * Already had space in some data block.
499 * Just read that one in.
501 error = xfs_dir2_data_read(tp, dp,
502 xfs_dir2_db_to_da(mp, use_block),
505 xfs_trans_brelse(tp, lbp);
512 * Point to the biggest freespace in our data block.
514 dup = (xfs_dir2_data_unused_t *)
515 ((char *)hdr + be16_to_cpu(hdr->bestfree[0].offset));
516 ASSERT(be16_to_cpu(dup->length) >= length);
517 needscan = needlog = 0;
519 * Mark the initial part of our freespace in use for the new entry.
521 xfs_dir2_data_use_free(tp, dbp, dup,
522 (xfs_dir2_data_aoff_t)((char *)dup - (char *)hdr), length,
523 &needlog, &needscan);
525 * Initialize our new entry (at last).
527 dep = (xfs_dir2_data_entry_t *)dup;
528 dep->inumber = cpu_to_be64(args->inumber);
529 dep->namelen = args->namelen;
530 memcpy(dep->name, args->name, dep->namelen);
531 tagp = xfs_dir2_data_entry_tag_p(dep);
532 *tagp = cpu_to_be16((char *)dep - (char *)hdr);
534 * Need to scan fix up the bestfree table.
537 xfs_dir2_data_freescan(mp, hdr, &needlog);
539 * Need to log the data block's header.
542 xfs_dir2_data_log_header(tp, dbp);
543 xfs_dir2_data_log_entry(tp, dbp, dep);
545 * If the bests table needs to be changed, do it.
546 * Log the change unless we've already done that.
548 if (be16_to_cpu(bestsp[use_block]) != be16_to_cpu(hdr->bestfree[0].length)) {
549 bestsp[use_block] = hdr->bestfree[0].length;
551 xfs_dir2_leaf_log_bests(tp, lbp, use_block, use_block);
554 lep = xfs_dir2_leaf_find_entry(leaf, index, compact, lowstale,
555 highstale, &lfloglow, &lfloghigh);
558 * Fill in the new leaf entry.
560 lep->hashval = cpu_to_be32(args->hashval);
561 lep->address = cpu_to_be32(xfs_dir2_db_off_to_dataptr(mp, use_block,
562 be16_to_cpu(*tagp)));
564 * Log the leaf fields and give up the buffers.
566 xfs_dir2_leaf_log_header(tp, lbp);
567 xfs_dir2_leaf_log_ents(tp, lbp, lfloglow, lfloghigh);
568 xfs_dir2_leaf_check(dp, lbp);
569 xfs_dir2_data_check(dp, dbp);
575 * Check the internal consistency of a leaf1 block.
576 * Pop an assert if something is wrong.
580 struct xfs_inode *dp, /* incore directory inode */
581 struct xfs_buf *bp) /* leaf's buffer */
583 int i; /* leaf index */
584 xfs_dir2_leaf_t *leaf; /* leaf structure */
585 xfs_dir2_leaf_tail_t *ltp; /* leaf tail pointer */
586 xfs_mount_t *mp; /* filesystem mount point */
587 int stale; /* count of stale leaves */
591 ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAF1_MAGIC));
593 * This value is not restrictive enough.
594 * Should factor in the size of the bests table as well.
595 * We can deduce a value for that from di_size.
597 ASSERT(be16_to_cpu(leaf->hdr.count) <= xfs_dir2_max_leaf_ents(mp));
598 ltp = xfs_dir2_leaf_tail_p(mp, leaf);
600 * Leaves and bests don't overlap.
602 ASSERT((char *)&leaf->ents[be16_to_cpu(leaf->hdr.count)] <=
603 (char *)xfs_dir2_leaf_bests_p(ltp));
605 * Check hash value order, count stale entries.
607 for (i = stale = 0; i < be16_to_cpu(leaf->hdr.count); i++) {
608 if (i + 1 < be16_to_cpu(leaf->hdr.count))
609 ASSERT(be32_to_cpu(leaf->ents[i].hashval) <=
610 be32_to_cpu(leaf->ents[i + 1].hashval));
611 if (leaf->ents[i].address == cpu_to_be32(XFS_DIR2_NULL_DATAPTR))
614 ASSERT(be16_to_cpu(leaf->hdr.stale) == stale);
619 * Compact out any stale entries in the leaf.
620 * Log the header and changed leaf entries, if any.
623 xfs_dir2_leaf_compact(
624 xfs_da_args_t *args, /* operation arguments */
625 struct xfs_buf *bp) /* leaf buffer */
627 int from; /* source leaf index */
628 xfs_dir2_leaf_t *leaf; /* leaf structure */
629 int loglow; /* first leaf entry to log */
630 int to; /* target leaf index */
633 if (!leaf->hdr.stale) {
637 * Compress out the stale entries in place.
639 for (from = to = 0, loglow = -1; from < be16_to_cpu(leaf->hdr.count); from++) {
640 if (leaf->ents[from].address ==
641 cpu_to_be32(XFS_DIR2_NULL_DATAPTR))
644 * Only actually copy the entries that are different.
649 leaf->ents[to] = leaf->ents[from];
654 * Update and log the header, log the leaf entries.
656 ASSERT(be16_to_cpu(leaf->hdr.stale) == from - to);
657 be16_add_cpu(&leaf->hdr.count, -(be16_to_cpu(leaf->hdr.stale)));
659 xfs_dir2_leaf_log_header(args->trans, bp);
661 xfs_dir2_leaf_log_ents(args->trans, bp, loglow, to - 1);
665 * Compact the leaf entries, removing stale ones.
666 * Leave one stale entry behind - the one closest to our
667 * insertion index - and the caller will shift that one to our insertion
669 * Return new insertion index, where the remaining stale entry is,
670 * and leaf logging indices.
673 xfs_dir2_leaf_compact_x1(
674 struct xfs_buf *bp, /* leaf buffer */
675 int *indexp, /* insertion index */
676 int *lowstalep, /* out: stale entry before us */
677 int *highstalep, /* out: stale entry after us */
678 int *lowlogp, /* out: low log index */
679 int *highlogp) /* out: high log index */
681 int from; /* source copy index */
682 int highstale; /* stale entry at/after index */
683 int index; /* insertion index */
684 int keepstale; /* source index of kept stale */
685 xfs_dir2_leaf_t *leaf; /* leaf structure */
686 int lowstale; /* stale entry before index */
687 int newindex=0; /* new insertion index */
688 int to; /* destination copy index */
691 ASSERT(be16_to_cpu(leaf->hdr.stale) > 1);
694 xfs_dir2_leaf_find_stale(leaf, index, &lowstale, &highstale);
697 * Pick the better of lowstale and highstale.
700 (highstale == be16_to_cpu(leaf->hdr.count) ||
701 index - lowstale <= highstale - index))
702 keepstale = lowstale;
704 keepstale = highstale;
706 * Copy the entries in place, removing all the stale entries
709 for (from = to = 0; from < be16_to_cpu(leaf->hdr.count); from++) {
711 * Notice the new value of index.
715 if (from != keepstale &&
716 leaf->ents[from].address ==
717 cpu_to_be32(XFS_DIR2_NULL_DATAPTR)) {
723 * Record the new keepstale value for the insertion.
725 if (from == keepstale)
726 lowstale = highstale = to;
728 * Copy only the entries that have moved.
731 leaf->ents[to] = leaf->ents[from];
736 * If the insertion point was past the last entry,
737 * set the new insertion point accordingly.
743 * Adjust the leaf header values.
745 be16_add_cpu(&leaf->hdr.count, -(from - to));
746 leaf->hdr.stale = cpu_to_be16(1);
748 * Remember the low/high stale value only in the "right"
751 if (lowstale >= newindex)
754 highstale = be16_to_cpu(leaf->hdr.count);
755 *highlogp = be16_to_cpu(leaf->hdr.count) - 1;
756 *lowstalep = lowstale;
757 *highstalep = highstale;
760 struct xfs_dir2_leaf_map_info {
761 xfs_extlen_t map_blocks; /* number of fsbs in map */
762 xfs_dablk_t map_off; /* last mapped file offset */
763 int map_size; /* total entries in *map */
764 int map_valid; /* valid entries in *map */
765 int nmap; /* mappings to ask xfs_bmapi */
766 xfs_dir2_db_t curdb; /* db for current block */
767 int ra_current; /* number of read-ahead blks */
768 int ra_index; /* *map index for read-ahead */
769 int ra_offset; /* map entry offset for ra */
770 int ra_want; /* readahead count wanted */
771 struct xfs_bmbt_irec map[]; /* map vector for blocks */
775 xfs_dir2_leaf_readbuf(
776 struct xfs_inode *dp,
778 struct xfs_dir2_leaf_map_info *mip,
779 xfs_dir2_off_t *curoff,
780 struct xfs_buf **bpp)
782 struct xfs_mount *mp = dp->i_mount;
783 struct xfs_buf *bp = *bpp;
784 struct xfs_bmbt_irec *map = mip->map;
791 * If we have a buffer, we need to release it and
792 * take it out of the mapping.
796 xfs_trans_brelse(NULL, bp);
798 mip->map_blocks -= mp->m_dirblkfsbs;
800 * Loop to get rid of the extents for the
803 for (i = mp->m_dirblkfsbs; i > 0; ) {
804 j = min_t(int, map->br_blockcount, i);
805 map->br_blockcount -= j;
806 map->br_startblock += j;
807 map->br_startoff += j;
809 * If mapping is done, pitch it from
812 if (!map->br_blockcount && --mip->map_valid)
813 memmove(&map[0], &map[1],
814 sizeof(map[0]) * mip->map_valid);
820 * Recalculate the readahead blocks wanted.
822 mip->ra_want = howmany(bufsize + mp->m_dirblksize,
823 mp->m_sb.sb_blocksize) - 1;
824 ASSERT(mip->ra_want >= 0);
827 * If we don't have as many as we want, and we haven't
828 * run out of data blocks, get some more mappings.
830 if (1 + mip->ra_want > mip->map_blocks &&
831 mip->map_off < xfs_dir2_byte_to_da(mp, XFS_DIR2_LEAF_OFFSET)) {
833 * Get more bmaps, fill in after the ones
834 * we already have in the table.
836 mip->nmap = mip->map_size - mip->map_valid;
837 error = xfs_bmapi_read(dp, mip->map_off,
838 xfs_dir2_byte_to_da(mp, XFS_DIR2_LEAF_OFFSET) -
840 &map[mip->map_valid], &mip->nmap, 0);
843 * Don't know if we should ignore this or try to return an
844 * error. The trouble with returning errors is that readdir
845 * will just stop without actually passing the error through.
851 * If we got all the mappings we asked for, set the final map
852 * offset based on the last bmap value received. Otherwise,
853 * we've reached the end.
855 if (mip->nmap == mip->map_size - mip->map_valid) {
856 i = mip->map_valid + mip->nmap - 1;
857 mip->map_off = map[i].br_startoff + map[i].br_blockcount;
859 mip->map_off = xfs_dir2_byte_to_da(mp,
860 XFS_DIR2_LEAF_OFFSET);
863 * Look for holes in the mapping, and eliminate them. Count up
866 for (i = mip->map_valid; i < mip->map_valid + mip->nmap; ) {
867 if (map[i].br_startblock == HOLESTARTBLOCK) {
869 length = mip->map_valid + mip->nmap - i;
871 memmove(&map[i], &map[i + 1],
872 sizeof(map[i]) * length);
874 mip->map_blocks += map[i].br_blockcount;
878 mip->map_valid += mip->nmap;
882 * No valid mappings, so no more data blocks.
884 if (!mip->map_valid) {
885 *curoff = xfs_dir2_da_to_byte(mp, mip->map_off);
890 * Read the directory block starting at the first mapping.
892 mip->curdb = xfs_dir2_da_to_db(mp, map->br_startoff);
893 error = xfs_dir2_data_read(NULL, dp, map->br_startoff,
894 map->br_blockcount >= mp->m_dirblkfsbs ?
895 XFS_FSB_TO_DADDR(mp, map->br_startblock) : -1, &bp);
898 * Should just skip over the data block instead of giving up.
904 * Adjust the current amount of read-ahead: we just read a block that
908 mip->ra_current -= mp->m_dirblkfsbs;
911 * Do we need more readahead?
913 for (mip->ra_index = mip->ra_offset = i = 0;
914 mip->ra_want > mip->ra_current && i < mip->map_blocks;
915 i += mp->m_dirblkfsbs) {
916 ASSERT(mip->ra_index < mip->map_valid);
918 * Read-ahead a contiguous directory block.
920 if (i > mip->ra_current &&
921 map[mip->ra_index].br_blockcount >= mp->m_dirblkfsbs) {
922 xfs_buf_readahead(mp->m_ddev_targp,
924 map[mip->ra_index].br_startblock +
926 (int)BTOBB(mp->m_dirblksize), NULL);
931 * Read-ahead a non-contiguous directory block. This doesn't
932 * use our mapping, but this is a very rare case.
934 else if (i > mip->ra_current) {
935 xfs_da_reada_buf(NULL, dp,
936 map[mip->ra_index].br_startoff +
938 XFS_DATA_FORK, NULL);
943 * Advance offset through the mapping table.
945 for (j = 0; j < mp->m_dirblkfsbs; j++) {
947 * The rest of this extent but not more than a dir
950 length = min_t(int, mp->m_dirblkfsbs,
951 map[mip->ra_index].br_blockcount -
954 mip->ra_offset += length;
957 * Advance to the next mapping if this one is used up.
959 if (mip->ra_offset == map[mip->ra_index].br_blockcount) {
972 * Getdents (readdir) for leaf and node directories.
973 * This reads the data blocks only, so is the same for both forms.
976 xfs_dir2_leaf_getdents(
977 xfs_inode_t *dp, /* incore directory inode */
983 struct xfs_buf *bp = NULL; /* data block buffer */
984 xfs_dir2_data_hdr_t *hdr; /* data block header */
985 xfs_dir2_data_entry_t *dep; /* data entry */
986 xfs_dir2_data_unused_t *dup; /* unused entry */
987 int error = 0; /* error return value */
988 int length; /* temporary length value */
989 xfs_mount_t *mp; /* filesystem mount point */
990 int byteoff; /* offset in current block */
991 xfs_dir2_off_t curoff; /* current overall offset */
992 xfs_dir2_off_t newoff; /* new curoff after new blk */
993 char *ptr = NULL; /* pointer to current data */
994 struct xfs_dir2_leaf_map_info *map_info;
997 * If the offset is at or past the largest allowed value,
998 * give up right away.
1000 if (*offset >= XFS_DIR2_MAX_DATAPTR)
1006 * Set up to bmap a number of blocks based on the caller's
1007 * buffer size, the directory block size, and the filesystem
1010 length = howmany(bufsize + mp->m_dirblksize,
1011 mp->m_sb.sb_blocksize);
1012 map_info = kmem_zalloc(offsetof(struct xfs_dir2_leaf_map_info, map) +
1013 (length * sizeof(struct xfs_bmbt_irec)),
1015 map_info->map_size = length;
1018 * Inside the loop we keep the main offset value as a byte offset
1019 * in the directory file.
1021 curoff = xfs_dir2_dataptr_to_byte(mp, *offset);
1024 * Force this conversion through db so we truncate the offset
1025 * down to get the start of the data block.
1027 map_info->map_off = xfs_dir2_db_to_da(mp,
1028 xfs_dir2_byte_to_db(mp, curoff));
1031 * Loop over directory entries until we reach the end offset.
1032 * Get more blocks and readahead as necessary.
1034 while (curoff < XFS_DIR2_LEAF_OFFSET) {
1036 * If we have no buffer, or we're off the end of the
1037 * current buffer, need to get another one.
1039 if (!bp || ptr >= (char *)bp->b_addr + mp->m_dirblksize) {
1041 error = xfs_dir2_leaf_readbuf(dp, bufsize, map_info,
1043 if (error || !map_info->map_valid)
1047 * Having done a read, we need to set a new offset.
1049 newoff = xfs_dir2_db_off_to_byte(mp, map_info->curdb, 0);
1051 * Start of the current block.
1053 if (curoff < newoff)
1056 * Make sure we're in the right block.
1058 else if (curoff > newoff)
1059 ASSERT(xfs_dir2_byte_to_db(mp, curoff) ==
1062 xfs_dir2_data_check(dp, bp);
1064 * Find our position in the block.
1066 ptr = (char *)(hdr + 1);
1067 byteoff = xfs_dir2_byte_to_off(mp, curoff);
1069 * Skip past the header.
1072 curoff += (uint)sizeof(*hdr);
1074 * Skip past entries until we reach our offset.
1077 while ((char *)ptr - (char *)hdr < byteoff) {
1078 dup = (xfs_dir2_data_unused_t *)ptr;
1080 if (be16_to_cpu(dup->freetag)
1081 == XFS_DIR2_DATA_FREE_TAG) {
1083 length = be16_to_cpu(dup->length);
1087 dep = (xfs_dir2_data_entry_t *)ptr;
1089 xfs_dir2_data_entsize(dep->namelen);
1093 * Now set our real offset.
1096 xfs_dir2_db_off_to_byte(mp,
1097 xfs_dir2_byte_to_db(mp, curoff),
1098 (char *)ptr - (char *)hdr);
1099 if (ptr >= (char *)hdr + mp->m_dirblksize) {
1105 * We have a pointer to an entry.
1108 dup = (xfs_dir2_data_unused_t *)ptr;
1110 * No, it's unused, skip over it.
1112 if (be16_to_cpu(dup->freetag) == XFS_DIR2_DATA_FREE_TAG) {
1113 length = be16_to_cpu(dup->length);
1119 dep = (xfs_dir2_data_entry_t *)ptr;
1120 length = xfs_dir2_data_entsize(dep->namelen);
1122 if (filldir(dirent, (char *)dep->name, dep->namelen,
1123 xfs_dir2_byte_to_dataptr(mp, curoff) & 0x7fffffff,
1124 be64_to_cpu(dep->inumber), DT_UNKNOWN))
1128 * Advance to next entry in the block.
1132 /* bufsize may have just been a guess; don't go negative */
1133 bufsize = bufsize > length ? bufsize - length : 0;
1137 * All done. Set output offset value to current offset.
1139 if (curoff > xfs_dir2_dataptr_to_byte(mp, XFS_DIR2_MAX_DATAPTR))
1140 *offset = XFS_DIR2_MAX_DATAPTR & 0x7fffffff;
1142 *offset = xfs_dir2_byte_to_dataptr(mp, curoff) & 0x7fffffff;
1143 kmem_free(map_info);
1145 xfs_trans_brelse(NULL, bp);
1150 * Initialize a new leaf block, leaf1 or leafn magic accepted.
1154 xfs_da_args_t *args, /* operation arguments */
1155 xfs_dir2_db_t bno, /* directory block number */
1156 struct xfs_buf **bpp, /* out: leaf buffer */
1157 int magic) /* magic number for block */
1159 struct xfs_buf *bp; /* leaf buffer */
1160 xfs_inode_t *dp; /* incore directory inode */
1161 int error; /* error return code */
1162 xfs_dir2_leaf_t *leaf; /* leaf structure */
1163 xfs_dir2_leaf_tail_t *ltp; /* leaf tail structure */
1164 xfs_mount_t *mp; /* filesystem mount point */
1165 xfs_trans_t *tp; /* transaction pointer */
1171 ASSERT(bno >= XFS_DIR2_LEAF_FIRSTDB(mp) &&
1172 bno < XFS_DIR2_FREE_FIRSTDB(mp));
1174 * Get the buffer for the block.
1176 error = xfs_da_get_buf(tp, dp, xfs_dir2_db_to_da(mp, bno), -1, &bp,
1184 * Initialize the header.
1186 leaf->hdr.info.magic = cpu_to_be16(magic);
1187 leaf->hdr.info.forw = 0;
1188 leaf->hdr.info.back = 0;
1189 leaf->hdr.count = 0;
1190 leaf->hdr.stale = 0;
1191 xfs_dir2_leaf_log_header(tp, bp);
1193 * If it's a leaf-format directory initialize the tail.
1194 * In this case our caller has the real bests table to copy into
1197 if (magic == XFS_DIR2_LEAF1_MAGIC) {
1198 ltp = xfs_dir2_leaf_tail_p(mp, leaf);
1200 xfs_dir2_leaf_log_tail(tp, bp);
1207 * Log the bests entries indicated from a leaf1 block.
1210 xfs_dir2_leaf_log_bests(
1211 xfs_trans_t *tp, /* transaction pointer */
1212 struct xfs_buf *bp, /* leaf buffer */
1213 int first, /* first entry to log */
1214 int last) /* last entry to log */
1216 __be16 *firstb; /* pointer to first entry */
1217 __be16 *lastb; /* pointer to last entry */
1218 xfs_dir2_leaf_t *leaf; /* leaf structure */
1219 xfs_dir2_leaf_tail_t *ltp; /* leaf tail structure */
1222 ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAF1_MAGIC));
1223 ltp = xfs_dir2_leaf_tail_p(tp->t_mountp, leaf);
1224 firstb = xfs_dir2_leaf_bests_p(ltp) + first;
1225 lastb = xfs_dir2_leaf_bests_p(ltp) + last;
1226 xfs_trans_log_buf(tp, bp, (uint)((char *)firstb - (char *)leaf),
1227 (uint)((char *)lastb - (char *)leaf + sizeof(*lastb) - 1));
1231 * Log the leaf entries indicated from a leaf1 or leafn block.
1234 xfs_dir2_leaf_log_ents(
1235 xfs_trans_t *tp, /* transaction pointer */
1236 struct xfs_buf *bp, /* leaf buffer */
1237 int first, /* first entry to log */
1238 int last) /* last entry to log */
1240 xfs_dir2_leaf_entry_t *firstlep; /* pointer to first entry */
1241 xfs_dir2_leaf_entry_t *lastlep; /* pointer to last entry */
1242 xfs_dir2_leaf_t *leaf; /* leaf structure */
1245 ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAF1_MAGIC) ||
1246 leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAFN_MAGIC));
1247 firstlep = &leaf->ents[first];
1248 lastlep = &leaf->ents[last];
1249 xfs_trans_log_buf(tp, bp, (uint)((char *)firstlep - (char *)leaf),
1250 (uint)((char *)lastlep - (char *)leaf + sizeof(*lastlep) - 1));
1254 * Log the header of the leaf1 or leafn block.
1257 xfs_dir2_leaf_log_header(
1258 struct xfs_trans *tp,
1261 xfs_dir2_leaf_t *leaf; /* leaf structure */
1264 ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAF1_MAGIC) ||
1265 leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAFN_MAGIC));
1266 xfs_trans_log_buf(tp, bp, (uint)((char *)&leaf->hdr - (char *)leaf),
1267 (uint)(sizeof(leaf->hdr) - 1));
1271 * Log the tail of the leaf1 block.
1274 xfs_dir2_leaf_log_tail(
1275 struct xfs_trans *tp,
1278 xfs_dir2_leaf_t *leaf; /* leaf structure */
1279 xfs_dir2_leaf_tail_t *ltp; /* leaf tail structure */
1280 xfs_mount_t *mp; /* filesystem mount point */
1284 ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAF1_MAGIC));
1285 ltp = xfs_dir2_leaf_tail_p(mp, leaf);
1286 xfs_trans_log_buf(tp, bp, (uint)((char *)ltp - (char *)leaf),
1287 (uint)(mp->m_dirblksize - 1));
1291 * Look up the entry referred to by args in the leaf format directory.
1292 * Most of the work is done by the xfs_dir2_leaf_lookup_int routine which
1293 * is also used by the node-format code.
1296 xfs_dir2_leaf_lookup(
1297 xfs_da_args_t *args) /* operation arguments */
1299 struct xfs_buf *dbp; /* data block buffer */
1300 xfs_dir2_data_entry_t *dep; /* data block entry */
1301 xfs_inode_t *dp; /* incore directory inode */
1302 int error; /* error return code */
1303 int index; /* found entry index */
1304 struct xfs_buf *lbp; /* leaf buffer */
1305 xfs_dir2_leaf_t *leaf; /* leaf structure */
1306 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
1307 xfs_trans_t *tp; /* transaction pointer */
1309 trace_xfs_dir2_leaf_lookup(args);
1312 * Look up name in the leaf block, returning both buffers and index.
1314 if ((error = xfs_dir2_leaf_lookup_int(args, &lbp, &index, &dbp))) {
1319 xfs_dir2_leaf_check(dp, lbp);
1322 * Get to the leaf entry and contained data entry address.
1324 lep = &leaf->ents[index];
1326 * Point to the data entry.
1328 dep = (xfs_dir2_data_entry_t *)
1329 ((char *)dbp->b_addr +
1330 xfs_dir2_dataptr_to_off(dp->i_mount, be32_to_cpu(lep->address)));
1332 * Return the found inode number & CI name if appropriate
1334 args->inumber = be64_to_cpu(dep->inumber);
1335 error = xfs_dir_cilookup_result(args, dep->name, dep->namelen);
1336 xfs_trans_brelse(tp, dbp);
1337 xfs_trans_brelse(tp, lbp);
1338 return XFS_ERROR(error);
1342 * Look up name/hash in the leaf block.
1343 * Fill in indexp with the found index, and dbpp with the data buffer.
1344 * If not found dbpp will be NULL, and ENOENT comes back.
1345 * lbpp will always be filled in with the leaf buffer unless there's an error.
1347 static int /* error */
1348 xfs_dir2_leaf_lookup_int(
1349 xfs_da_args_t *args, /* operation arguments */
1350 struct xfs_buf **lbpp, /* out: leaf buffer */
1351 int *indexp, /* out: index in leaf block */
1352 struct xfs_buf **dbpp) /* out: data buffer */
1354 xfs_dir2_db_t curdb = -1; /* current data block number */
1355 struct xfs_buf *dbp = NULL; /* data buffer */
1356 xfs_dir2_data_entry_t *dep; /* data entry */
1357 xfs_inode_t *dp; /* incore directory inode */
1358 int error; /* error return code */
1359 int index; /* index in leaf block */
1360 struct xfs_buf *lbp; /* leaf buffer */
1361 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
1362 xfs_dir2_leaf_t *leaf; /* leaf structure */
1363 xfs_mount_t *mp; /* filesystem mount point */
1364 xfs_dir2_db_t newdb; /* new data block number */
1365 xfs_trans_t *tp; /* transaction pointer */
1366 xfs_dir2_db_t cidb = -1; /* case match data block no. */
1367 enum xfs_dacmp cmp; /* name compare result */
1373 * Read the leaf block into the buffer.
1375 error = xfs_da_read_buf(tp, dp, mp->m_dirleafblk, -1, &lbp,
1376 XFS_DATA_FORK, NULL);
1381 xfs_dir2_leaf_check(dp, lbp);
1383 * Look for the first leaf entry with our hash value.
1385 index = xfs_dir2_leaf_search_hash(args, lbp);
1387 * Loop over all the entries with the right hash value
1388 * looking to match the name.
1390 for (lep = &leaf->ents[index]; index < be16_to_cpu(leaf->hdr.count) &&
1391 be32_to_cpu(lep->hashval) == args->hashval;
1394 * Skip over stale leaf entries.
1396 if (be32_to_cpu(lep->address) == XFS_DIR2_NULL_DATAPTR)
1399 * Get the new data block number.
1401 newdb = xfs_dir2_dataptr_to_db(mp, be32_to_cpu(lep->address));
1403 * If it's not the same as the old data block number,
1404 * need to pitch the old one and read the new one.
1406 if (newdb != curdb) {
1408 xfs_trans_brelse(tp, dbp);
1409 error = xfs_dir2_data_read(tp, dp,
1410 xfs_dir2_db_to_da(mp, newdb),
1413 xfs_trans_brelse(tp, lbp);
1419 * Point to the data entry.
1421 dep = (xfs_dir2_data_entry_t *)((char *)dbp->b_addr +
1422 xfs_dir2_dataptr_to_off(mp, be32_to_cpu(lep->address)));
1424 * Compare name and if it's an exact match, return the index
1425 * and buffer. If it's the first case-insensitive match, store
1426 * the index and buffer and continue looking for an exact match.
1428 cmp = mp->m_dirnameops->compname(args, dep->name, dep->namelen);
1429 if (cmp != XFS_CMP_DIFFERENT && cmp != args->cmpresult) {
1430 args->cmpresult = cmp;
1432 /* case exact match: return the current buffer. */
1433 if (cmp == XFS_CMP_EXACT) {
1440 ASSERT(args->op_flags & XFS_DA_OP_OKNOENT);
1442 * Here, we can only be doing a lookup (not a rename or remove).
1443 * If a case-insensitive match was found earlier, re-read the
1444 * appropriate data block if required and return it.
1446 if (args->cmpresult == XFS_CMP_CASE) {
1448 if (cidb != curdb) {
1449 xfs_trans_brelse(tp, dbp);
1450 error = xfs_dir2_data_read(tp, dp,
1451 xfs_dir2_db_to_da(mp, cidb),
1454 xfs_trans_brelse(tp, lbp);
1462 * No match found, return ENOENT.
1466 xfs_trans_brelse(tp, dbp);
1467 xfs_trans_brelse(tp, lbp);
1468 return XFS_ERROR(ENOENT);
1472 * Remove an entry from a leaf format directory.
1475 xfs_dir2_leaf_removename(
1476 xfs_da_args_t *args) /* operation arguments */
1478 __be16 *bestsp; /* leaf block best freespace */
1479 xfs_dir2_data_hdr_t *hdr; /* data block header */
1480 xfs_dir2_db_t db; /* data block number */
1481 struct xfs_buf *dbp; /* data block buffer */
1482 xfs_dir2_data_entry_t *dep; /* data entry structure */
1483 xfs_inode_t *dp; /* incore directory inode */
1484 int error; /* error return code */
1485 xfs_dir2_db_t i; /* temporary data block # */
1486 int index; /* index into leaf entries */
1487 struct xfs_buf *lbp; /* leaf buffer */
1488 xfs_dir2_leaf_t *leaf; /* leaf structure */
1489 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
1490 xfs_dir2_leaf_tail_t *ltp; /* leaf tail structure */
1491 xfs_mount_t *mp; /* filesystem mount point */
1492 int needlog; /* need to log data header */
1493 int needscan; /* need to rescan data frees */
1494 xfs_dir2_data_off_t oldbest; /* old value of best free */
1495 xfs_trans_t *tp; /* transaction pointer */
1497 trace_xfs_dir2_leaf_removename(args);
1500 * Lookup the leaf entry, get the leaf and data blocks read in.
1502 if ((error = xfs_dir2_leaf_lookup_int(args, &lbp, &index, &dbp))) {
1510 xfs_dir2_data_check(dp, dbp);
1512 * Point to the leaf entry, use that to point to the data entry.
1514 lep = &leaf->ents[index];
1515 db = xfs_dir2_dataptr_to_db(mp, be32_to_cpu(lep->address));
1516 dep = (xfs_dir2_data_entry_t *)
1517 ((char *)hdr + xfs_dir2_dataptr_to_off(mp, be32_to_cpu(lep->address)));
1518 needscan = needlog = 0;
1519 oldbest = be16_to_cpu(hdr->bestfree[0].length);
1520 ltp = xfs_dir2_leaf_tail_p(mp, leaf);
1521 bestsp = xfs_dir2_leaf_bests_p(ltp);
1522 ASSERT(be16_to_cpu(bestsp[db]) == oldbest);
1524 * Mark the former data entry unused.
1526 xfs_dir2_data_make_free(tp, dbp,
1527 (xfs_dir2_data_aoff_t)((char *)dep - (char *)hdr),
1528 xfs_dir2_data_entsize(dep->namelen), &needlog, &needscan);
1530 * We just mark the leaf entry stale by putting a null in it.
1532 be16_add_cpu(&leaf->hdr.stale, 1);
1533 xfs_dir2_leaf_log_header(tp, lbp);
1534 lep->address = cpu_to_be32(XFS_DIR2_NULL_DATAPTR);
1535 xfs_dir2_leaf_log_ents(tp, lbp, index, index);
1537 * Scan the freespace in the data block again if necessary,
1538 * log the data block header if necessary.
1541 xfs_dir2_data_freescan(mp, hdr, &needlog);
1543 xfs_dir2_data_log_header(tp, dbp);
1545 * If the longest freespace in the data block has changed,
1546 * put the new value in the bests table and log that.
1548 if (be16_to_cpu(hdr->bestfree[0].length) != oldbest) {
1549 bestsp[db] = hdr->bestfree[0].length;
1550 xfs_dir2_leaf_log_bests(tp, lbp, db, db);
1552 xfs_dir2_data_check(dp, dbp);
1554 * If the data block is now empty then get rid of the data block.
1556 if (be16_to_cpu(hdr->bestfree[0].length) ==
1557 mp->m_dirblksize - (uint)sizeof(*hdr)) {
1558 ASSERT(db != mp->m_dirdatablk);
1559 if ((error = xfs_dir2_shrink_inode(args, db, dbp))) {
1561 * Nope, can't get rid of it because it caused
1562 * allocation of a bmap btree block to do so.
1563 * Just go on, returning success, leaving the
1564 * empty block in place.
1566 if (error == ENOSPC && args->total == 0)
1568 xfs_dir2_leaf_check(dp, lbp);
1573 * If this is the last data block then compact the
1574 * bests table by getting rid of entries.
1576 if (db == be32_to_cpu(ltp->bestcount) - 1) {
1578 * Look for the last active entry (i).
1580 for (i = db - 1; i > 0; i--) {
1581 if (bestsp[i] != cpu_to_be16(NULLDATAOFF))
1585 * Copy the table down so inactive entries at the
1588 memmove(&bestsp[db - i], bestsp,
1589 (be32_to_cpu(ltp->bestcount) - (db - i)) * sizeof(*bestsp));
1590 be32_add_cpu(<p->bestcount, -(db - i));
1591 xfs_dir2_leaf_log_tail(tp, lbp);
1592 xfs_dir2_leaf_log_bests(tp, lbp, 0, be32_to_cpu(ltp->bestcount) - 1);
1594 bestsp[db] = cpu_to_be16(NULLDATAOFF);
1597 * If the data block was not the first one, drop it.
1599 else if (db != mp->m_dirdatablk)
1602 xfs_dir2_leaf_check(dp, lbp);
1604 * See if we can convert to block form.
1606 return xfs_dir2_leaf_to_block(args, lbp, dbp);
1610 * Replace the inode number in a leaf format directory entry.
1613 xfs_dir2_leaf_replace(
1614 xfs_da_args_t *args) /* operation arguments */
1616 struct xfs_buf *dbp; /* data block buffer */
1617 xfs_dir2_data_entry_t *dep; /* data block entry */
1618 xfs_inode_t *dp; /* incore directory inode */
1619 int error; /* error return code */
1620 int index; /* index of leaf entry */
1621 struct xfs_buf *lbp; /* leaf buffer */
1622 xfs_dir2_leaf_t *leaf; /* leaf structure */
1623 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
1624 xfs_trans_t *tp; /* transaction pointer */
1626 trace_xfs_dir2_leaf_replace(args);
1629 * Look up the entry.
1631 if ((error = xfs_dir2_leaf_lookup_int(args, &lbp, &index, &dbp))) {
1637 * Point to the leaf entry, get data address from it.
1639 lep = &leaf->ents[index];
1641 * Point to the data entry.
1643 dep = (xfs_dir2_data_entry_t *)
1644 ((char *)dbp->b_addr +
1645 xfs_dir2_dataptr_to_off(dp->i_mount, be32_to_cpu(lep->address)));
1646 ASSERT(args->inumber != be64_to_cpu(dep->inumber));
1648 * Put the new inode number in, log it.
1650 dep->inumber = cpu_to_be64(args->inumber);
1652 xfs_dir2_data_log_entry(tp, dbp, dep);
1653 xfs_dir2_leaf_check(dp, lbp);
1654 xfs_trans_brelse(tp, lbp);
1659 * Return index in the leaf block (lbp) which is either the first
1660 * one with this hash value, or if there are none, the insert point
1661 * for that hash value.
1663 int /* index value */
1664 xfs_dir2_leaf_search_hash(
1665 xfs_da_args_t *args, /* operation arguments */
1666 struct xfs_buf *lbp) /* leaf buffer */
1668 xfs_dahash_t hash=0; /* hash from this entry */
1669 xfs_dahash_t hashwant; /* hash value looking for */
1670 int high; /* high leaf index */
1671 int low; /* low leaf index */
1672 xfs_dir2_leaf_t *leaf; /* leaf structure */
1673 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
1674 int mid=0; /* current leaf index */
1678 if (!leaf->hdr.count)
1682 * Note, the table cannot be empty, so we have to go through the loop.
1683 * Binary search the leaf entries looking for our hash value.
1685 for (lep = leaf->ents, low = 0, high = be16_to_cpu(leaf->hdr.count) - 1,
1686 hashwant = args->hashval;
1688 mid = (low + high) >> 1;
1689 if ((hash = be32_to_cpu(lep[mid].hashval)) == hashwant)
1691 if (hash < hashwant)
1697 * Found one, back up through all the equal hash values.
1699 if (hash == hashwant) {
1700 while (mid > 0 && be32_to_cpu(lep[mid - 1].hashval) == hashwant) {
1705 * Need to point to an entry higher than ours.
1707 else if (hash < hashwant)
1713 * Trim off a trailing data block. We know it's empty since the leaf
1714 * freespace table says so.
1717 xfs_dir2_leaf_trim_data(
1718 xfs_da_args_t *args, /* operation arguments */
1719 struct xfs_buf *lbp, /* leaf buffer */
1720 xfs_dir2_db_t db) /* data block number */
1722 __be16 *bestsp; /* leaf bests table */
1723 struct xfs_buf *dbp; /* data block buffer */
1724 xfs_inode_t *dp; /* incore directory inode */
1725 int error; /* error return value */
1726 xfs_dir2_leaf_t *leaf; /* leaf structure */
1727 xfs_dir2_leaf_tail_t *ltp; /* leaf tail structure */
1728 xfs_mount_t *mp; /* filesystem mount point */
1729 xfs_trans_t *tp; /* transaction pointer */
1735 * Read the offending data block. We need its buffer.
1737 error = xfs_dir2_data_read(tp, dp, xfs_dir2_db_to_da(mp, db), -1, &dbp);
1742 ltp = xfs_dir2_leaf_tail_p(mp, leaf);
1746 struct xfs_dir2_data_hdr *hdr = dbp->b_addr;
1748 ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC));
1749 ASSERT(be16_to_cpu(hdr->bestfree[0].length) ==
1750 mp->m_dirblksize - (uint)sizeof(*hdr));
1751 ASSERT(db == be32_to_cpu(ltp->bestcount) - 1);
1756 * Get rid of the data block.
1758 if ((error = xfs_dir2_shrink_inode(args, db, dbp))) {
1759 ASSERT(error != ENOSPC);
1760 xfs_trans_brelse(tp, dbp);
1764 * Eliminate the last bests entry from the table.
1766 bestsp = xfs_dir2_leaf_bests_p(ltp);
1767 be32_add_cpu(<p->bestcount, -1);
1768 memmove(&bestsp[1], &bestsp[0], be32_to_cpu(ltp->bestcount) * sizeof(*bestsp));
1769 xfs_dir2_leaf_log_tail(tp, lbp);
1770 xfs_dir2_leaf_log_bests(tp, lbp, 0, be32_to_cpu(ltp->bestcount) - 1);
1774 static inline size_t
1776 struct xfs_dir2_leaf_hdr *hdr,
1781 entries = be16_to_cpu(hdr->count) - be16_to_cpu(hdr->stale);
1782 return sizeof(xfs_dir2_leaf_hdr_t) +
1783 entries * sizeof(xfs_dir2_leaf_entry_t) +
1784 counts * sizeof(xfs_dir2_data_off_t) +
1785 sizeof(xfs_dir2_leaf_tail_t);
1789 * Convert node form directory to leaf form directory.
1790 * The root of the node form dir needs to already be a LEAFN block.
1791 * Just return if we can't do anything.
1794 xfs_dir2_node_to_leaf(
1795 xfs_da_state_t *state) /* directory operation state */
1797 xfs_da_args_t *args; /* operation arguments */
1798 xfs_inode_t *dp; /* incore directory inode */
1799 int error; /* error return code */
1800 struct xfs_buf *fbp; /* buffer for freespace block */
1801 xfs_fileoff_t fo; /* freespace file offset */
1802 xfs_dir2_free_t *free; /* freespace structure */
1803 struct xfs_buf *lbp; /* buffer for leaf block */
1804 xfs_dir2_leaf_tail_t *ltp; /* tail of leaf structure */
1805 xfs_dir2_leaf_t *leaf; /* leaf structure */
1806 xfs_mount_t *mp; /* filesystem mount point */
1807 int rval; /* successful free trim? */
1808 xfs_trans_t *tp; /* transaction pointer */
1811 * There's more than a leaf level in the btree, so there must
1812 * be multiple leafn blocks. Give up.
1814 if (state->path.active > 1)
1818 trace_xfs_dir2_node_to_leaf(args);
1824 * Get the last offset in the file.
1826 if ((error = xfs_bmap_last_offset(tp, dp, &fo, XFS_DATA_FORK))) {
1829 fo -= mp->m_dirblkfsbs;
1831 * If there are freespace blocks other than the first one,
1832 * take this opportunity to remove trailing empty freespace blocks
1833 * that may have been left behind during no-space-reservation
1836 while (fo > mp->m_dirfreeblk) {
1837 if ((error = xfs_dir2_node_trim_free(args, fo, &rval))) {
1841 fo -= mp->m_dirblkfsbs;
1846 * Now find the block just before the freespace block.
1848 if ((error = xfs_bmap_last_before(tp, dp, &fo, XFS_DATA_FORK))) {
1852 * If it's not the single leaf block, give up.
1854 if (XFS_FSB_TO_B(mp, fo) > XFS_DIR2_LEAF_OFFSET + mp->m_dirblksize)
1856 lbp = state->path.blk[0].bp;
1858 ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAFN_MAGIC));
1860 * Read the freespace block.
1862 error = xfs_dir2_free_read(tp, dp, mp->m_dirfreeblk, &fbp);
1866 ASSERT(free->hdr.magic == cpu_to_be32(XFS_DIR2_FREE_MAGIC));
1867 ASSERT(!free->hdr.firstdb);
1870 * Now see if the leafn and free data will fit in a leaf1.
1871 * If not, release the buffer and give up.
1873 if (xfs_dir2_leaf_size(&leaf->hdr, be32_to_cpu(free->hdr.nvalid)) >
1875 xfs_trans_brelse(tp, fbp);
1880 * If the leaf has any stale entries in it, compress them out.
1881 * The compact routine will log the header.
1883 if (be16_to_cpu(leaf->hdr.stale))
1884 xfs_dir2_leaf_compact(args, lbp);
1886 xfs_dir2_leaf_log_header(tp, lbp);
1887 leaf->hdr.info.magic = cpu_to_be16(XFS_DIR2_LEAF1_MAGIC);
1889 * Set up the leaf tail from the freespace block.
1891 ltp = xfs_dir2_leaf_tail_p(mp, leaf);
1892 ltp->bestcount = free->hdr.nvalid;
1894 * Set up the leaf bests table.
1896 memcpy(xfs_dir2_leaf_bests_p(ltp), free->bests,
1897 be32_to_cpu(ltp->bestcount) * sizeof(xfs_dir2_data_off_t));
1898 xfs_dir2_leaf_log_bests(tp, lbp, 0, be32_to_cpu(ltp->bestcount) - 1);
1899 xfs_dir2_leaf_log_tail(tp, lbp);
1900 xfs_dir2_leaf_check(dp, lbp);
1902 * Get rid of the freespace block.
1904 error = xfs_dir2_shrink_inode(args, XFS_DIR2_FREE_FIRSTDB(mp), fbp);
1907 * This can't fail here because it can only happen when
1908 * punching out the middle of an extent, and this is an
1911 ASSERT(error != ENOSPC);
1916 * Now see if we can convert the single-leaf directory
1917 * down to a block form directory.
1918 * This routine always kills the dabuf for the leaf, so
1919 * eliminate it from the path.
1921 error = xfs_dir2_leaf_to_block(args, lbp, NULL);
1922 state->path.blk[0].bp = NULL;