xfs: reshuffle dir2 headers
[linux-2.6/kvm.git] / fs / xfs / xfs_dir2_leaf.c
blob487036d3751bd782163d0112f57d2dc789cdf8b1
1 /*
2 * Copyright (c) 2000-2003,2005 Silicon Graphics, Inc.
3 * All Rights Reserved.
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
18 #include "xfs.h"
19 #include "xfs_fs.h"
20 #include "xfs_types.h"
21 #include "xfs_bit.h"
22 #include "xfs_log.h"
23 #include "xfs_inum.h"
24 #include "xfs_trans.h"
25 #include "xfs_sb.h"
26 #include "xfs_ag.h"
27 #include "xfs_mount.h"
28 #include "xfs_da_btree.h"
29 #include "xfs_bmap_btree.h"
30 #include "xfs_dinode.h"
31 #include "xfs_inode.h"
32 #include "xfs_bmap.h"
33 #include "xfs_dir2_format.h"
34 #include "xfs_dir2_priv.h"
35 #include "xfs_error.h"
36 #include "xfs_trace.h"
39 * Local function declarations.
41 #ifdef DEBUG
42 static void xfs_dir2_leaf_check(xfs_inode_t *dp, xfs_dabuf_t *bp);
43 #else
44 #define xfs_dir2_leaf_check(dp, bp)
45 #endif
46 static int xfs_dir2_leaf_lookup_int(xfs_da_args_t *args, xfs_dabuf_t **lbpp,
47 int *indexp, xfs_dabuf_t **dbpp);
48 static void xfs_dir2_leaf_log_bests(struct xfs_trans *tp, struct xfs_dabuf *bp,
49 int first, int last);
50 static void xfs_dir2_leaf_log_tail(struct xfs_trans *tp, struct xfs_dabuf *bp);
54 * Convert a block form directory to a leaf form directory.
56 int /* error */
57 xfs_dir2_block_to_leaf(
58 xfs_da_args_t *args, /* operation arguments */
59 xfs_dabuf_t *dbp) /* input block's buffer */
61 __be16 *bestsp; /* leaf's bestsp entries */
62 xfs_dablk_t blkno; /* leaf block's bno */
63 xfs_dir2_data_hdr_t *hdr; /* block header */
64 xfs_dir2_leaf_entry_t *blp; /* block's leaf entries */
65 xfs_dir2_block_tail_t *btp; /* block's tail */
66 xfs_inode_t *dp; /* incore directory inode */
67 int error; /* error return code */
68 xfs_dabuf_t *lbp; /* leaf block's buffer */
69 xfs_dir2_db_t ldb; /* leaf block's bno */
70 xfs_dir2_leaf_t *leaf; /* leaf structure */
71 xfs_dir2_leaf_tail_t *ltp; /* leaf's tail */
72 xfs_mount_t *mp; /* filesystem mount point */
73 int needlog; /* need to log block header */
74 int needscan; /* need to rescan bestfree */
75 xfs_trans_t *tp; /* transaction pointer */
77 trace_xfs_dir2_block_to_leaf(args);
79 dp = args->dp;
80 mp = dp->i_mount;
81 tp = args->trans;
83 * Add the leaf block to the inode.
84 * This interface will only put blocks in the leaf/node range.
85 * Since that's empty now, we'll get the root (block 0 in range).
87 if ((error = xfs_da_grow_inode(args, &blkno))) {
88 return error;
90 ldb = xfs_dir2_da_to_db(mp, blkno);
91 ASSERT(ldb == XFS_DIR2_LEAF_FIRSTDB(mp));
93 * Initialize the leaf block, get a buffer for it.
95 if ((error = xfs_dir2_leaf_init(args, ldb, &lbp, XFS_DIR2_LEAF1_MAGIC))) {
96 return error;
98 ASSERT(lbp != NULL);
99 leaf = lbp->data;
100 hdr = dbp->data;
101 xfs_dir2_data_check(dp, dbp);
102 btp = xfs_dir2_block_tail_p(mp, hdr);
103 blp = xfs_dir2_block_leaf_p(btp);
105 * Set the counts in the leaf header.
107 leaf->hdr.count = cpu_to_be16(be32_to_cpu(btp->count));
108 leaf->hdr.stale = cpu_to_be16(be32_to_cpu(btp->stale));
110 * Could compact these but I think we always do the conversion
111 * after squeezing out stale entries.
113 memcpy(leaf->ents, blp, be32_to_cpu(btp->count) * sizeof(xfs_dir2_leaf_entry_t));
114 xfs_dir2_leaf_log_ents(tp, lbp, 0, be16_to_cpu(leaf->hdr.count) - 1);
115 needscan = 0;
116 needlog = 1;
118 * Make the space formerly occupied by the leaf entries and block
119 * tail be free.
121 xfs_dir2_data_make_free(tp, dbp,
122 (xfs_dir2_data_aoff_t)((char *)blp - (char *)hdr),
123 (xfs_dir2_data_aoff_t)((char *)hdr + mp->m_dirblksize -
124 (char *)blp),
125 &needlog, &needscan);
127 * Fix up the block header, make it a data block.
129 hdr->magic = cpu_to_be32(XFS_DIR2_DATA_MAGIC);
130 if (needscan)
131 xfs_dir2_data_freescan(mp, hdr, &needlog);
133 * Set up leaf tail and bests table.
135 ltp = xfs_dir2_leaf_tail_p(mp, leaf);
136 ltp->bestcount = cpu_to_be32(1);
137 bestsp = xfs_dir2_leaf_bests_p(ltp);
138 bestsp[0] = hdr->bestfree[0].length;
140 * Log the data header and leaf bests table.
142 if (needlog)
143 xfs_dir2_data_log_header(tp, dbp);
144 xfs_dir2_leaf_check(dp, lbp);
145 xfs_dir2_data_check(dp, dbp);
146 xfs_dir2_leaf_log_bests(tp, lbp, 0, 0);
147 xfs_da_buf_done(lbp);
148 return 0;
151 struct xfs_dir2_leaf_entry *
152 xfs_dir2_leaf_find_entry(
153 xfs_dir2_leaf_t *leaf, /* leaf structure */
154 int index, /* leaf table position */
155 int compact, /* need to compact leaves */
156 int lowstale, /* index of prev stale leaf */
157 int highstale, /* index of next stale leaf */
158 int *lfloglow, /* low leaf logging index */
159 int *lfloghigh) /* high leaf logging index */
161 if (!leaf->hdr.stale) {
162 xfs_dir2_leaf_entry_t *lep; /* leaf entry table pointer */
165 * Now we need to make room to insert the leaf entry.
167 * If there are no stale entries, just insert a hole at index.
169 lep = &leaf->ents[index];
170 if (index < be16_to_cpu(leaf->hdr.count))
171 memmove(lep + 1, lep,
172 (be16_to_cpu(leaf->hdr.count) - index) *
173 sizeof(*lep));
176 * Record low and high logging indices for the leaf.
178 *lfloglow = index;
179 *lfloghigh = be16_to_cpu(leaf->hdr.count);
180 be16_add_cpu(&leaf->hdr.count, 1);
181 return lep;
185 * There are stale entries.
187 * We will use one of them for the new entry. It's probably not at
188 * the right location, so we'll have to shift some up or down first.
190 * If we didn't compact before, we need to find the nearest stale
191 * entries before and after our insertion point.
193 if (compact == 0) {
195 * Find the first stale entry before the insertion point,
196 * if any.
198 for (lowstale = index - 1;
199 lowstale >= 0 &&
200 leaf->ents[lowstale].address !=
201 cpu_to_be32(XFS_DIR2_NULL_DATAPTR);
202 lowstale--)
203 continue;
206 * Find the next stale entry at or after the insertion point,
207 * if any. Stop if we go so far that the lowstale entry
208 * would be better.
210 for (highstale = index;
211 highstale < be16_to_cpu(leaf->hdr.count) &&
212 leaf->ents[highstale].address !=
213 cpu_to_be32(XFS_DIR2_NULL_DATAPTR) &&
214 (lowstale < 0 ||
215 index - lowstale - 1 >= highstale - index);
216 highstale++)
217 continue;
221 * If the low one is better, use it.
223 if (lowstale >= 0 &&
224 (highstale == be16_to_cpu(leaf->hdr.count) ||
225 index - lowstale - 1 < highstale - index)) {
226 ASSERT(index - lowstale - 1 >= 0);
227 ASSERT(leaf->ents[lowstale].address ==
228 cpu_to_be32(XFS_DIR2_NULL_DATAPTR));
231 * Copy entries up to cover the stale entry and make room
232 * for the new entry.
234 if (index - lowstale - 1 > 0) {
235 memmove(&leaf->ents[lowstale],
236 &leaf->ents[lowstale + 1],
237 (index - lowstale - 1) *
238 sizeof(xfs_dir2_leaf_entry_t));
240 *lfloglow = MIN(lowstale, *lfloglow);
241 *lfloghigh = MAX(index - 1, *lfloghigh);
242 be16_add_cpu(&leaf->hdr.stale, -1);
243 return &leaf->ents[index - 1];
247 * The high one is better, so use that one.
249 ASSERT(highstale - index >= 0);
250 ASSERT(leaf->ents[highstale].address ==
251 cpu_to_be32(XFS_DIR2_NULL_DATAPTR));
254 * Copy entries down to cover the stale entry and make room for the
255 * new entry.
257 if (highstale - index > 0) {
258 memmove(&leaf->ents[index + 1],
259 &leaf->ents[index],
260 (highstale - index) * sizeof(xfs_dir2_leaf_entry_t));
262 *lfloglow = MIN(index, *lfloglow);
263 *lfloghigh = MAX(highstale, *lfloghigh);
264 be16_add_cpu(&leaf->hdr.stale, -1);
265 return &leaf->ents[index];
269 * Add an entry to a leaf form directory.
271 int /* error */
272 xfs_dir2_leaf_addname(
273 xfs_da_args_t *args) /* operation arguments */
275 __be16 *bestsp; /* freespace table in leaf */
276 int compact; /* need to compact leaves */
277 xfs_dir2_data_hdr_t *hdr; /* data block header */
278 xfs_dabuf_t *dbp; /* data block buffer */
279 xfs_dir2_data_entry_t *dep; /* data block entry */
280 xfs_inode_t *dp; /* incore directory inode */
281 xfs_dir2_data_unused_t *dup; /* data unused entry */
282 int error; /* error return value */
283 int grown; /* allocated new data block */
284 int highstale; /* index of next stale leaf */
285 int i; /* temporary, index */
286 int index; /* leaf table position */
287 xfs_dabuf_t *lbp; /* leaf's buffer */
288 xfs_dir2_leaf_t *leaf; /* leaf structure */
289 int length; /* length of new entry */
290 xfs_dir2_leaf_entry_t *lep; /* leaf entry table pointer */
291 int lfloglow; /* low leaf logging index */
292 int lfloghigh; /* high leaf logging index */
293 int lowstale; /* index of prev stale leaf */
294 xfs_dir2_leaf_tail_t *ltp; /* leaf tail pointer */
295 xfs_mount_t *mp; /* filesystem mount point */
296 int needbytes; /* leaf block bytes needed */
297 int needlog; /* need to log data header */
298 int needscan; /* need to rescan data free */
299 __be16 *tagp; /* end of data entry */
300 xfs_trans_t *tp; /* transaction pointer */
301 xfs_dir2_db_t use_block; /* data block number */
303 trace_xfs_dir2_leaf_addname(args);
305 dp = args->dp;
306 tp = args->trans;
307 mp = dp->i_mount;
309 * Read the leaf block.
311 error = xfs_da_read_buf(tp, dp, mp->m_dirleafblk, -1, &lbp,
312 XFS_DATA_FORK);
313 if (error) {
314 return error;
316 ASSERT(lbp != NULL);
318 * Look up the entry by hash value and name.
319 * We know it's not there, our caller has already done a lookup.
320 * So the index is of the entry to insert in front of.
321 * But if there are dup hash values the index is of the first of those.
323 index = xfs_dir2_leaf_search_hash(args, lbp);
324 leaf = lbp->data;
325 ltp = xfs_dir2_leaf_tail_p(mp, leaf);
326 bestsp = xfs_dir2_leaf_bests_p(ltp);
327 length = xfs_dir2_data_entsize(args->namelen);
329 * See if there are any entries with the same hash value
330 * and space in their block for the new entry.
331 * This is good because it puts multiple same-hash value entries
332 * in a data block, improving the lookup of those entries.
334 for (use_block = -1, lep = &leaf->ents[index];
335 index < be16_to_cpu(leaf->hdr.count) && be32_to_cpu(lep->hashval) == args->hashval;
336 index++, lep++) {
337 if (be32_to_cpu(lep->address) == XFS_DIR2_NULL_DATAPTR)
338 continue;
339 i = xfs_dir2_dataptr_to_db(mp, be32_to_cpu(lep->address));
340 ASSERT(i < be32_to_cpu(ltp->bestcount));
341 ASSERT(bestsp[i] != cpu_to_be16(NULLDATAOFF));
342 if (be16_to_cpu(bestsp[i]) >= length) {
343 use_block = i;
344 break;
348 * Didn't find a block yet, linear search all the data blocks.
350 if (use_block == -1) {
351 for (i = 0; i < be32_to_cpu(ltp->bestcount); i++) {
353 * Remember a block we see that's missing.
355 if (bestsp[i] == cpu_to_be16(NULLDATAOFF) &&
356 use_block == -1)
357 use_block = i;
358 else if (be16_to_cpu(bestsp[i]) >= length) {
359 use_block = i;
360 break;
365 * How many bytes do we need in the leaf block?
367 needbytes = 0;
368 if (!leaf->hdr.stale)
369 needbytes += sizeof(xfs_dir2_leaf_entry_t);
370 if (use_block == -1)
371 needbytes += sizeof(xfs_dir2_data_off_t);
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 && bestsp[use_block] == cpu_to_be16(NULLDATAOFF))
378 use_block = -1;
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) {
385 compact = 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) ||
397 args->total == 0) {
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);
406 if (error)
407 return error;
409 * Then add the new entry.
411 return xfs_dir2_node_addname(args);
414 * Otherwise it will fit without compaction.
416 else
417 compact = 0;
419 * If just checking, then it will fit unless we needed to allocate
420 * a new data block.
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
428 * changed anything.
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
438 * point later.
440 if (compact) {
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);
450 lfloghigh = -1;
453 * If there was no data block space found, we need to allocate
454 * a new one.
456 if (use_block == -1) {
458 * Add the new data block.
460 if ((error = xfs_dir2_grow_inode(args, XFS_DIR2_DATA_SPACE,
461 &use_block))) {
462 xfs_da_brelse(tp, lbp);
463 return error;
466 * Initialize the block.
468 if ((error = xfs_dir2_data_init(args, use_block, &dbp))) {
469 xfs_da_brelse(tp, lbp);
470 return error;
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)) {
477 bestsp--;
478 memmove(&bestsp[0], &bestsp[1],
479 be32_to_cpu(ltp->bestcount) * sizeof(bestsp[0]));
480 be32_add_cpu(&ltp->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.
487 else
488 xfs_dir2_leaf_log_bests(tp, lbp, use_block, use_block);
489 hdr = dbp->data;
490 bestsp[use_block] = hdr->bestfree[0].length;
491 grown = 1;
494 * Already had space in some data block.
495 * Just read that one in.
497 else {
498 if ((error =
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);
502 return error;
504 hdr = dbp->data;
505 grown = 0;
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.
533 if (needscan)
534 xfs_dir2_data_freescan(mp, hdr, &needlog);
536 * Need to log the data block's header.
538 if (needlog)
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;
547 if (!grown)
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);
569 return 0;
572 #ifdef DEBUG
574 * Check the internal consistency of a leaf1 block.
575 * Pop an assert if something is wrong.
577 STATIC void
578 xfs_dir2_leaf_check(
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 */
588 leaf = bp->data;
589 mp = dp->i_mount;
590 ASSERT(leaf->hdr.info.magic == cpu_to_be16(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 (leaf->ents[i].address == cpu_to_be32(XFS_DIR2_NULL_DATAPTR))
611 stale++;
613 ASSERT(be16_to_cpu(leaf->hdr.stale) == stale);
615 #endif /* DEBUG */
618 * Compact out any stale entries in the leaf.
619 * Log the header and changed leaf entries, if any.
621 void
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 */
631 leaf = bp->data;
632 if (!leaf->hdr.stale) {
633 return;
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 (leaf->ents[from].address ==
640 cpu_to_be32(XFS_DIR2_NULL_DATAPTR))
641 continue;
643 * Only actually copy the entries that are different.
645 if (from > to) {
646 if (loglow == -1)
647 loglow = to;
648 leaf->ents[to] = leaf->ents[from];
650 to++;
653 * Update and log the header, log the leaf entries.
655 ASSERT(be16_to_cpu(leaf->hdr.stale) == from - to);
656 be16_add_cpu(&leaf->hdr.count, -(be16_to_cpu(leaf->hdr.stale)));
657 leaf->hdr.stale = 0;
658 xfs_dir2_leaf_log_header(args->trans, bp);
659 if (loglow != -1)
660 xfs_dir2_leaf_log_ents(args->trans, bp, loglow, to - 1);
664 * Compact the leaf entries, removing stale ones.
665 * Leave one stale entry behind - the one closest to our
666 * insertion index - and the caller will shift that one to our insertion
667 * point later.
668 * Return new insertion index, where the remaining stale entry is,
669 * and leaf logging indices.
671 void
672 xfs_dir2_leaf_compact_x1(
673 xfs_dabuf_t *bp, /* leaf buffer */
674 int *indexp, /* insertion index */
675 int *lowstalep, /* out: stale entry before us */
676 int *highstalep, /* out: stale entry after us */
677 int *lowlogp, /* out: low log index */
678 int *highlogp) /* out: high log index */
680 int from; /* source copy index */
681 int highstale; /* stale entry at/after index */
682 int index; /* insertion index */
683 int keepstale; /* source index of kept stale */
684 xfs_dir2_leaf_t *leaf; /* leaf structure */
685 int lowstale; /* stale entry before index */
686 int newindex=0; /* new insertion index */
687 int to; /* destination copy index */
689 leaf = bp->data;
690 ASSERT(be16_to_cpu(leaf->hdr.stale) > 1);
691 index = *indexp;
693 * Find the first stale entry before our index, if any.
695 for (lowstale = index - 1;
696 lowstale >= 0 &&
697 leaf->ents[lowstale].address !=
698 cpu_to_be32(XFS_DIR2_NULL_DATAPTR);
699 lowstale--)
700 continue;
702 * Find the first stale entry at or after our index, if any.
703 * Stop if the answer would be worse than lowstale.
705 for (highstale = index;
706 highstale < be16_to_cpu(leaf->hdr.count) &&
707 leaf->ents[highstale].address !=
708 cpu_to_be32(XFS_DIR2_NULL_DATAPTR) &&
709 (lowstale < 0 || index - lowstale > highstale - index);
710 highstale++)
711 continue;
713 * Pick the better of lowstale and highstale.
715 if (lowstale >= 0 &&
716 (highstale == be16_to_cpu(leaf->hdr.count) ||
717 index - lowstale <= highstale - index))
718 keepstale = lowstale;
719 else
720 keepstale = highstale;
722 * Copy the entries in place, removing all the stale entries
723 * except keepstale.
725 for (from = to = 0; from < be16_to_cpu(leaf->hdr.count); from++) {
727 * Notice the new value of index.
729 if (index == from)
730 newindex = to;
731 if (from != keepstale &&
732 leaf->ents[from].address ==
733 cpu_to_be32(XFS_DIR2_NULL_DATAPTR)) {
734 if (from == to)
735 *lowlogp = to;
736 continue;
739 * Record the new keepstale value for the insertion.
741 if (from == keepstale)
742 lowstale = highstale = to;
744 * Copy only the entries that have moved.
746 if (from > to)
747 leaf->ents[to] = leaf->ents[from];
748 to++;
750 ASSERT(from > to);
752 * If the insertion point was past the last entry,
753 * set the new insertion point accordingly.
755 if (index == from)
756 newindex = to;
757 *indexp = newindex;
759 * Adjust the leaf header values.
761 be16_add_cpu(&leaf->hdr.count, -(from - to));
762 leaf->hdr.stale = cpu_to_be16(1);
764 * Remember the low/high stale value only in the "right"
765 * direction.
767 if (lowstale >= newindex)
768 lowstale = -1;
769 else
770 highstale = be16_to_cpu(leaf->hdr.count);
771 *highlogp = be16_to_cpu(leaf->hdr.count) - 1;
772 *lowstalep = lowstale;
773 *highstalep = highstale;
777 * Getdents (readdir) for leaf and node directories.
778 * This reads the data blocks only, so is the same for both forms.
780 int /* error */
781 xfs_dir2_leaf_getdents(
782 xfs_inode_t *dp, /* incore directory inode */
783 void *dirent,
784 size_t bufsize,
785 xfs_off_t *offset,
786 filldir_t filldir)
788 xfs_dabuf_t *bp; /* data block buffer */
789 int byteoff; /* offset in current block */
790 xfs_dir2_db_t curdb; /* db for current block */
791 xfs_dir2_off_t curoff; /* current overall offset */
792 xfs_dir2_data_hdr_t *hdr; /* data block header */
793 xfs_dir2_data_entry_t *dep; /* data entry */
794 xfs_dir2_data_unused_t *dup; /* unused entry */
795 int error = 0; /* error return value */
796 int i; /* temporary loop index */
797 int j; /* temporary loop index */
798 int length; /* temporary length value */
799 xfs_bmbt_irec_t *map; /* map vector for blocks */
800 xfs_extlen_t map_blocks; /* number of fsbs in map */
801 xfs_dablk_t map_off; /* last mapped file offset */
802 int map_size; /* total entries in *map */
803 int map_valid; /* valid entries in *map */
804 xfs_mount_t *mp; /* filesystem mount point */
805 xfs_dir2_off_t newoff; /* new curoff after new blk */
806 int nmap; /* mappings to ask xfs_bmapi */
807 char *ptr = NULL; /* pointer to current data */
808 int ra_current; /* number of read-ahead blks */
809 int ra_index; /* *map index for read-ahead */
810 int ra_offset; /* map entry offset for ra */
811 int ra_want; /* readahead count wanted */
814 * If the offset is at or past the largest allowed value,
815 * give up right away.
817 if (*offset >= XFS_DIR2_MAX_DATAPTR)
818 return 0;
820 mp = dp->i_mount;
823 * Set up to bmap a number of blocks based on the caller's
824 * buffer size, the directory block size, and the filesystem
825 * block size.
827 map_size = howmany(bufsize + mp->m_dirblksize, mp->m_sb.sb_blocksize);
828 map = kmem_alloc(map_size * sizeof(*map), KM_SLEEP);
829 map_valid = ra_index = ra_offset = ra_current = map_blocks = 0;
830 bp = NULL;
833 * Inside the loop we keep the main offset value as a byte offset
834 * in the directory file.
836 curoff = xfs_dir2_dataptr_to_byte(mp, *offset);
839 * Force this conversion through db so we truncate the offset
840 * down to get the start of the data block.
842 map_off = xfs_dir2_db_to_da(mp, xfs_dir2_byte_to_db(mp, curoff));
844 * Loop over directory entries until we reach the end offset.
845 * Get more blocks and readahead as necessary.
847 while (curoff < XFS_DIR2_LEAF_OFFSET) {
849 * If we have no buffer, or we're off the end of the
850 * current buffer, need to get another one.
852 if (!bp || ptr >= (char *)bp->data + mp->m_dirblksize) {
854 * If we have a buffer, we need to release it and
855 * take it out of the mapping.
857 if (bp) {
858 xfs_da_brelse(NULL, bp);
859 bp = NULL;
860 map_blocks -= mp->m_dirblkfsbs;
862 * Loop to get rid of the extents for the
863 * directory block.
865 for (i = mp->m_dirblkfsbs; i > 0; ) {
866 j = MIN((int)map->br_blockcount, i);
867 map->br_blockcount -= j;
868 map->br_startblock += j;
869 map->br_startoff += j;
871 * If mapping is done, pitch it from
872 * the table.
874 if (!map->br_blockcount && --map_valid)
875 memmove(&map[0], &map[1],
876 sizeof(map[0]) *
877 map_valid);
878 i -= j;
882 * Recalculate the readahead blocks wanted.
884 ra_want = howmany(bufsize + mp->m_dirblksize,
885 mp->m_sb.sb_blocksize) - 1;
886 ASSERT(ra_want >= 0);
889 * If we don't have as many as we want, and we haven't
890 * run out of data blocks, get some more mappings.
892 if (1 + ra_want > map_blocks &&
893 map_off <
894 xfs_dir2_byte_to_da(mp, XFS_DIR2_LEAF_OFFSET)) {
896 * Get more bmaps, fill in after the ones
897 * we already have in the table.
899 nmap = map_size - map_valid;
900 error = xfs_bmapi(NULL, dp,
901 map_off,
902 xfs_dir2_byte_to_da(mp,
903 XFS_DIR2_LEAF_OFFSET) - map_off,
904 XFS_BMAPI_METADATA, NULL, 0,
905 &map[map_valid], &nmap, NULL);
907 * Don't know if we should ignore this or
908 * try to return an error.
909 * The trouble with returning errors
910 * is that readdir will just stop without
911 * actually passing the error through.
913 if (error)
914 break; /* XXX */
916 * If we got all the mappings we asked for,
917 * set the final map offset based on the
918 * last bmap value received.
919 * Otherwise, we've reached the end.
921 if (nmap == map_size - map_valid)
922 map_off =
923 map[map_valid + nmap - 1].br_startoff +
924 map[map_valid + nmap - 1].br_blockcount;
925 else
926 map_off =
927 xfs_dir2_byte_to_da(mp,
928 XFS_DIR2_LEAF_OFFSET);
930 * Look for holes in the mapping, and
931 * eliminate them. Count up the valid blocks.
933 for (i = map_valid; i < map_valid + nmap; ) {
934 if (map[i].br_startblock ==
935 HOLESTARTBLOCK) {
936 nmap--;
937 length = map_valid + nmap - i;
938 if (length)
939 memmove(&map[i],
940 &map[i + 1],
941 sizeof(map[i]) *
942 length);
943 } else {
944 map_blocks +=
945 map[i].br_blockcount;
946 i++;
949 map_valid += nmap;
952 * No valid mappings, so no more data blocks.
954 if (!map_valid) {
955 curoff = xfs_dir2_da_to_byte(mp, map_off);
956 break;
959 * Read the directory block starting at the first
960 * mapping.
962 curdb = xfs_dir2_da_to_db(mp, map->br_startoff);
963 error = xfs_da_read_buf(NULL, dp, map->br_startoff,
964 map->br_blockcount >= mp->m_dirblkfsbs ?
965 XFS_FSB_TO_DADDR(mp, map->br_startblock) :
967 &bp, XFS_DATA_FORK);
969 * Should just skip over the data block instead
970 * of giving up.
972 if (error)
973 break; /* XXX */
975 * Adjust the current amount of read-ahead: we just
976 * read a block that was previously ra.
978 if (ra_current)
979 ra_current -= mp->m_dirblkfsbs;
981 * Do we need more readahead?
983 for (ra_index = ra_offset = i = 0;
984 ra_want > ra_current && i < map_blocks;
985 i += mp->m_dirblkfsbs) {
986 ASSERT(ra_index < map_valid);
988 * Read-ahead a contiguous directory block.
990 if (i > ra_current &&
991 map[ra_index].br_blockcount >=
992 mp->m_dirblkfsbs) {
993 xfs_buf_readahead(mp->m_ddev_targp,
994 XFS_FSB_TO_DADDR(mp,
995 map[ra_index].br_startblock +
996 ra_offset),
997 (int)BTOBB(mp->m_dirblksize));
998 ra_current = i;
1001 * Read-ahead a non-contiguous directory block.
1002 * This doesn't use our mapping, but this
1003 * is a very rare case.
1005 else if (i > ra_current) {
1006 (void)xfs_da_reada_buf(NULL, dp,
1007 map[ra_index].br_startoff +
1008 ra_offset, XFS_DATA_FORK);
1009 ra_current = i;
1012 * Advance offset through the mapping table.
1014 for (j = 0; j < mp->m_dirblkfsbs; j++) {
1016 * The rest of this extent but not
1017 * more than a dir block.
1019 length = MIN(mp->m_dirblkfsbs,
1020 (int)(map[ra_index].br_blockcount -
1021 ra_offset));
1022 j += length;
1023 ra_offset += length;
1025 * Advance to the next mapping if
1026 * this one is used up.
1028 if (ra_offset ==
1029 map[ra_index].br_blockcount) {
1030 ra_offset = 0;
1031 ra_index++;
1036 * Having done a read, we need to set a new offset.
1038 newoff = xfs_dir2_db_off_to_byte(mp, curdb, 0);
1040 * Start of the current block.
1042 if (curoff < newoff)
1043 curoff = newoff;
1045 * Make sure we're in the right block.
1047 else if (curoff > newoff)
1048 ASSERT(xfs_dir2_byte_to_db(mp, curoff) ==
1049 curdb);
1050 hdr = bp->data;
1051 xfs_dir2_data_check(dp, bp);
1053 * Find our position in the block.
1055 ptr = (char *)(hdr + 1);
1056 byteoff = xfs_dir2_byte_to_off(mp, curoff);
1058 * Skip past the header.
1060 if (byteoff == 0)
1061 curoff += (uint)sizeof(*hdr);
1063 * Skip past entries until we reach our offset.
1065 else {
1066 while ((char *)ptr - (char *)hdr < byteoff) {
1067 dup = (xfs_dir2_data_unused_t *)ptr;
1069 if (be16_to_cpu(dup->freetag)
1070 == XFS_DIR2_DATA_FREE_TAG) {
1072 length = be16_to_cpu(dup->length);
1073 ptr += length;
1074 continue;
1076 dep = (xfs_dir2_data_entry_t *)ptr;
1077 length =
1078 xfs_dir2_data_entsize(dep->namelen);
1079 ptr += length;
1082 * Now set our real offset.
1084 curoff =
1085 xfs_dir2_db_off_to_byte(mp,
1086 xfs_dir2_byte_to_db(mp, curoff),
1087 (char *)ptr - (char *)hdr);
1088 if (ptr >= (char *)hdr + mp->m_dirblksize) {
1089 continue;
1094 * We have a pointer to an entry.
1095 * Is it a live one?
1097 dup = (xfs_dir2_data_unused_t *)ptr;
1099 * No, it's unused, skip over it.
1101 if (be16_to_cpu(dup->freetag) == XFS_DIR2_DATA_FREE_TAG) {
1102 length = be16_to_cpu(dup->length);
1103 ptr += length;
1104 curoff += length;
1105 continue;
1108 dep = (xfs_dir2_data_entry_t *)ptr;
1109 length = xfs_dir2_data_entsize(dep->namelen);
1111 if (filldir(dirent, (char *)dep->name, dep->namelen,
1112 xfs_dir2_byte_to_dataptr(mp, curoff) & 0x7fffffff,
1113 be64_to_cpu(dep->inumber), DT_UNKNOWN))
1114 break;
1117 * Advance to next entry in the block.
1119 ptr += length;
1120 curoff += length;
1121 /* bufsize may have just been a guess; don't go negative */
1122 bufsize = bufsize > length ? bufsize - length : 0;
1126 * All done. Set output offset value to current offset.
1128 if (curoff > xfs_dir2_dataptr_to_byte(mp, XFS_DIR2_MAX_DATAPTR))
1129 *offset = XFS_DIR2_MAX_DATAPTR & 0x7fffffff;
1130 else
1131 *offset = xfs_dir2_byte_to_dataptr(mp, curoff) & 0x7fffffff;
1132 kmem_free(map);
1133 if (bp)
1134 xfs_da_brelse(NULL, bp);
1135 return error;
1139 * Initialize a new leaf block, leaf1 or leafn magic accepted.
1142 xfs_dir2_leaf_init(
1143 xfs_da_args_t *args, /* operation arguments */
1144 xfs_dir2_db_t bno, /* directory block number */
1145 xfs_dabuf_t **bpp, /* out: leaf buffer */
1146 int magic) /* magic number for block */
1148 xfs_dabuf_t *bp; /* leaf buffer */
1149 xfs_inode_t *dp; /* incore directory inode */
1150 int error; /* error return code */
1151 xfs_dir2_leaf_t *leaf; /* leaf structure */
1152 xfs_dir2_leaf_tail_t *ltp; /* leaf tail structure */
1153 xfs_mount_t *mp; /* filesystem mount point */
1154 xfs_trans_t *tp; /* transaction pointer */
1156 dp = args->dp;
1157 ASSERT(dp != NULL);
1158 tp = args->trans;
1159 mp = dp->i_mount;
1160 ASSERT(bno >= XFS_DIR2_LEAF_FIRSTDB(mp) &&
1161 bno < XFS_DIR2_FREE_FIRSTDB(mp));
1163 * Get the buffer for the block.
1165 error = xfs_da_get_buf(tp, dp, xfs_dir2_db_to_da(mp, bno), -1, &bp,
1166 XFS_DATA_FORK);
1167 if (error) {
1168 return error;
1170 ASSERT(bp != NULL);
1171 leaf = bp->data;
1173 * Initialize the header.
1175 leaf->hdr.info.magic = cpu_to_be16(magic);
1176 leaf->hdr.info.forw = 0;
1177 leaf->hdr.info.back = 0;
1178 leaf->hdr.count = 0;
1179 leaf->hdr.stale = 0;
1180 xfs_dir2_leaf_log_header(tp, bp);
1182 * If it's a leaf-format directory initialize the tail.
1183 * In this case our caller has the real bests table to copy into
1184 * the block.
1186 if (magic == XFS_DIR2_LEAF1_MAGIC) {
1187 ltp = xfs_dir2_leaf_tail_p(mp, leaf);
1188 ltp->bestcount = 0;
1189 xfs_dir2_leaf_log_tail(tp, bp);
1191 *bpp = bp;
1192 return 0;
1196 * Log the bests entries indicated from a leaf1 block.
1198 static void
1199 xfs_dir2_leaf_log_bests(
1200 xfs_trans_t *tp, /* transaction pointer */
1201 xfs_dabuf_t *bp, /* leaf buffer */
1202 int first, /* first entry to log */
1203 int last) /* last entry to log */
1205 __be16 *firstb; /* pointer to first entry */
1206 __be16 *lastb; /* pointer to last entry */
1207 xfs_dir2_leaf_t *leaf; /* leaf structure */
1208 xfs_dir2_leaf_tail_t *ltp; /* leaf tail structure */
1210 leaf = bp->data;
1211 ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAF1_MAGIC));
1212 ltp = xfs_dir2_leaf_tail_p(tp->t_mountp, leaf);
1213 firstb = xfs_dir2_leaf_bests_p(ltp) + first;
1214 lastb = xfs_dir2_leaf_bests_p(ltp) + last;
1215 xfs_da_log_buf(tp, bp, (uint)((char *)firstb - (char *)leaf),
1216 (uint)((char *)lastb - (char *)leaf + sizeof(*lastb) - 1));
1220 * Log the leaf entries indicated from a leaf1 or leafn block.
1222 void
1223 xfs_dir2_leaf_log_ents(
1224 xfs_trans_t *tp, /* transaction pointer */
1225 xfs_dabuf_t *bp, /* leaf buffer */
1226 int first, /* first entry to log */
1227 int last) /* last entry to log */
1229 xfs_dir2_leaf_entry_t *firstlep; /* pointer to first entry */
1230 xfs_dir2_leaf_entry_t *lastlep; /* pointer to last entry */
1231 xfs_dir2_leaf_t *leaf; /* leaf structure */
1233 leaf = bp->data;
1234 ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAF1_MAGIC) ||
1235 leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAFN_MAGIC));
1236 firstlep = &leaf->ents[first];
1237 lastlep = &leaf->ents[last];
1238 xfs_da_log_buf(tp, bp, (uint)((char *)firstlep - (char *)leaf),
1239 (uint)((char *)lastlep - (char *)leaf + sizeof(*lastlep) - 1));
1243 * Log the header of the leaf1 or leafn block.
1245 void
1246 xfs_dir2_leaf_log_header(
1247 xfs_trans_t *tp, /* transaction pointer */
1248 xfs_dabuf_t *bp) /* leaf buffer */
1250 xfs_dir2_leaf_t *leaf; /* leaf structure */
1252 leaf = bp->data;
1253 ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAF1_MAGIC) ||
1254 leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAFN_MAGIC));
1255 xfs_da_log_buf(tp, bp, (uint)((char *)&leaf->hdr - (char *)leaf),
1256 (uint)(sizeof(leaf->hdr) - 1));
1260 * Log the tail of the leaf1 block.
1262 STATIC void
1263 xfs_dir2_leaf_log_tail(
1264 xfs_trans_t *tp, /* transaction pointer */
1265 xfs_dabuf_t *bp) /* leaf buffer */
1267 xfs_dir2_leaf_t *leaf; /* leaf structure */
1268 xfs_dir2_leaf_tail_t *ltp; /* leaf tail structure */
1269 xfs_mount_t *mp; /* filesystem mount point */
1271 mp = tp->t_mountp;
1272 leaf = bp->data;
1273 ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAF1_MAGIC));
1274 ltp = xfs_dir2_leaf_tail_p(mp, leaf);
1275 xfs_da_log_buf(tp, bp, (uint)((char *)ltp - (char *)leaf),
1276 (uint)(mp->m_dirblksize - 1));
1280 * Look up the entry referred to by args in the leaf format directory.
1281 * Most of the work is done by the xfs_dir2_leaf_lookup_int routine which
1282 * is also used by the node-format code.
1285 xfs_dir2_leaf_lookup(
1286 xfs_da_args_t *args) /* operation arguments */
1288 xfs_dabuf_t *dbp; /* data block buffer */
1289 xfs_dir2_data_entry_t *dep; /* data block entry */
1290 xfs_inode_t *dp; /* incore directory inode */
1291 int error; /* error return code */
1292 int index; /* found entry index */
1293 xfs_dabuf_t *lbp; /* leaf buffer */
1294 xfs_dir2_leaf_t *leaf; /* leaf structure */
1295 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
1296 xfs_trans_t *tp; /* transaction pointer */
1298 trace_xfs_dir2_leaf_lookup(args);
1301 * Look up name in the leaf block, returning both buffers and index.
1303 if ((error = xfs_dir2_leaf_lookup_int(args, &lbp, &index, &dbp))) {
1304 return error;
1306 tp = args->trans;
1307 dp = args->dp;
1308 xfs_dir2_leaf_check(dp, lbp);
1309 leaf = lbp->data;
1311 * Get to the leaf entry and contained data entry address.
1313 lep = &leaf->ents[index];
1315 * Point to the data entry.
1317 dep = (xfs_dir2_data_entry_t *)
1318 ((char *)dbp->data +
1319 xfs_dir2_dataptr_to_off(dp->i_mount, be32_to_cpu(lep->address)));
1321 * Return the found inode number & CI name if appropriate
1323 args->inumber = be64_to_cpu(dep->inumber);
1324 error = xfs_dir_cilookup_result(args, dep->name, dep->namelen);
1325 xfs_da_brelse(tp, dbp);
1326 xfs_da_brelse(tp, lbp);
1327 return XFS_ERROR(error);
1331 * Look up name/hash in the leaf block.
1332 * Fill in indexp with the found index, and dbpp with the data buffer.
1333 * If not found dbpp will be NULL, and ENOENT comes back.
1334 * lbpp will always be filled in with the leaf buffer unless there's an error.
1336 static int /* error */
1337 xfs_dir2_leaf_lookup_int(
1338 xfs_da_args_t *args, /* operation arguments */
1339 xfs_dabuf_t **lbpp, /* out: leaf buffer */
1340 int *indexp, /* out: index in leaf block */
1341 xfs_dabuf_t **dbpp) /* out: data buffer */
1343 xfs_dir2_db_t curdb = -1; /* current data block number */
1344 xfs_dabuf_t *dbp = NULL; /* data buffer */
1345 xfs_dir2_data_entry_t *dep; /* data entry */
1346 xfs_inode_t *dp; /* incore directory inode */
1347 int error; /* error return code */
1348 int index; /* index in leaf block */
1349 xfs_dabuf_t *lbp; /* leaf buffer */
1350 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
1351 xfs_dir2_leaf_t *leaf; /* leaf structure */
1352 xfs_mount_t *mp; /* filesystem mount point */
1353 xfs_dir2_db_t newdb; /* new data block number */
1354 xfs_trans_t *tp; /* transaction pointer */
1355 xfs_dir2_db_t cidb = -1; /* case match data block no. */
1356 enum xfs_dacmp cmp; /* name compare result */
1358 dp = args->dp;
1359 tp = args->trans;
1360 mp = dp->i_mount;
1362 * Read the leaf block into the buffer.
1364 error = xfs_da_read_buf(tp, dp, mp->m_dirleafblk, -1, &lbp,
1365 XFS_DATA_FORK);
1366 if (error)
1367 return error;
1368 *lbpp = lbp;
1369 leaf = lbp->data;
1370 xfs_dir2_leaf_check(dp, lbp);
1372 * Look for the first leaf entry with our hash value.
1374 index = xfs_dir2_leaf_search_hash(args, lbp);
1376 * Loop over all the entries with the right hash value
1377 * looking to match the name.
1379 for (lep = &leaf->ents[index]; index < be16_to_cpu(leaf->hdr.count) &&
1380 be32_to_cpu(lep->hashval) == args->hashval;
1381 lep++, index++) {
1383 * Skip over stale leaf entries.
1385 if (be32_to_cpu(lep->address) == XFS_DIR2_NULL_DATAPTR)
1386 continue;
1388 * Get the new data block number.
1390 newdb = xfs_dir2_dataptr_to_db(mp, be32_to_cpu(lep->address));
1392 * If it's not the same as the old data block number,
1393 * need to pitch the old one and read the new one.
1395 if (newdb != curdb) {
1396 if (dbp)
1397 xfs_da_brelse(tp, dbp);
1398 error = xfs_da_read_buf(tp, dp,
1399 xfs_dir2_db_to_da(mp, newdb),
1400 -1, &dbp, XFS_DATA_FORK);
1401 if (error) {
1402 xfs_da_brelse(tp, lbp);
1403 return error;
1405 xfs_dir2_data_check(dp, dbp);
1406 curdb = newdb;
1409 * Point to the data entry.
1411 dep = (xfs_dir2_data_entry_t *)((char *)dbp->data +
1412 xfs_dir2_dataptr_to_off(mp, be32_to_cpu(lep->address)));
1414 * Compare name and if it's an exact match, return the index
1415 * and buffer. If it's the first case-insensitive match, store
1416 * the index and buffer and continue looking for an exact match.
1418 cmp = mp->m_dirnameops->compname(args, dep->name, dep->namelen);
1419 if (cmp != XFS_CMP_DIFFERENT && cmp != args->cmpresult) {
1420 args->cmpresult = cmp;
1421 *indexp = index;
1422 /* case exact match: return the current buffer. */
1423 if (cmp == XFS_CMP_EXACT) {
1424 *dbpp = dbp;
1425 return 0;
1427 cidb = curdb;
1430 ASSERT(args->op_flags & XFS_DA_OP_OKNOENT);
1432 * Here, we can only be doing a lookup (not a rename or remove).
1433 * If a case-insensitive match was found earlier, re-read the
1434 * appropriate data block if required and return it.
1436 if (args->cmpresult == XFS_CMP_CASE) {
1437 ASSERT(cidb != -1);
1438 if (cidb != curdb) {
1439 xfs_da_brelse(tp, dbp);
1440 error = xfs_da_read_buf(tp, dp,
1441 xfs_dir2_db_to_da(mp, cidb),
1442 -1, &dbp, XFS_DATA_FORK);
1443 if (error) {
1444 xfs_da_brelse(tp, lbp);
1445 return error;
1448 *dbpp = dbp;
1449 return 0;
1452 * No match found, return ENOENT.
1454 ASSERT(cidb == -1);
1455 if (dbp)
1456 xfs_da_brelse(tp, dbp);
1457 xfs_da_brelse(tp, lbp);
1458 return XFS_ERROR(ENOENT);
1462 * Remove an entry from a leaf format directory.
1464 int /* error */
1465 xfs_dir2_leaf_removename(
1466 xfs_da_args_t *args) /* operation arguments */
1468 __be16 *bestsp; /* leaf block best freespace */
1469 xfs_dir2_data_hdr_t *hdr; /* data block header */
1470 xfs_dir2_db_t db; /* data block number */
1471 xfs_dabuf_t *dbp; /* data block buffer */
1472 xfs_dir2_data_entry_t *dep; /* data entry structure */
1473 xfs_inode_t *dp; /* incore directory inode */
1474 int error; /* error return code */
1475 xfs_dir2_db_t i; /* temporary data block # */
1476 int index; /* index into leaf entries */
1477 xfs_dabuf_t *lbp; /* leaf buffer */
1478 xfs_dir2_leaf_t *leaf; /* leaf structure */
1479 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
1480 xfs_dir2_leaf_tail_t *ltp; /* leaf tail structure */
1481 xfs_mount_t *mp; /* filesystem mount point */
1482 int needlog; /* need to log data header */
1483 int needscan; /* need to rescan data frees */
1484 xfs_dir2_data_off_t oldbest; /* old value of best free */
1485 xfs_trans_t *tp; /* transaction pointer */
1487 trace_xfs_dir2_leaf_removename(args);
1490 * Lookup the leaf entry, get the leaf and data blocks read in.
1492 if ((error = xfs_dir2_leaf_lookup_int(args, &lbp, &index, &dbp))) {
1493 return error;
1495 dp = args->dp;
1496 tp = args->trans;
1497 mp = dp->i_mount;
1498 leaf = lbp->data;
1499 hdr = dbp->data;
1500 xfs_dir2_data_check(dp, dbp);
1502 * Point to the leaf entry, use that to point to the data entry.
1504 lep = &leaf->ents[index];
1505 db = xfs_dir2_dataptr_to_db(mp, be32_to_cpu(lep->address));
1506 dep = (xfs_dir2_data_entry_t *)
1507 ((char *)hdr + xfs_dir2_dataptr_to_off(mp, be32_to_cpu(lep->address)));
1508 needscan = needlog = 0;
1509 oldbest = be16_to_cpu(hdr->bestfree[0].length);
1510 ltp = xfs_dir2_leaf_tail_p(mp, leaf);
1511 bestsp = xfs_dir2_leaf_bests_p(ltp);
1512 ASSERT(be16_to_cpu(bestsp[db]) == oldbest);
1514 * Mark the former data entry unused.
1516 xfs_dir2_data_make_free(tp, dbp,
1517 (xfs_dir2_data_aoff_t)((char *)dep - (char *)hdr),
1518 xfs_dir2_data_entsize(dep->namelen), &needlog, &needscan);
1520 * We just mark the leaf entry stale by putting a null in it.
1522 be16_add_cpu(&leaf->hdr.stale, 1);
1523 xfs_dir2_leaf_log_header(tp, lbp);
1524 lep->address = cpu_to_be32(XFS_DIR2_NULL_DATAPTR);
1525 xfs_dir2_leaf_log_ents(tp, lbp, index, index);
1527 * Scan the freespace in the data block again if necessary,
1528 * log the data block header if necessary.
1530 if (needscan)
1531 xfs_dir2_data_freescan(mp, hdr, &needlog);
1532 if (needlog)
1533 xfs_dir2_data_log_header(tp, dbp);
1535 * If the longest freespace in the data block has changed,
1536 * put the new value in the bests table and log that.
1538 if (be16_to_cpu(hdr->bestfree[0].length) != oldbest) {
1539 bestsp[db] = hdr->bestfree[0].length;
1540 xfs_dir2_leaf_log_bests(tp, lbp, db, db);
1542 xfs_dir2_data_check(dp, dbp);
1544 * If the data block is now empty then get rid of the data block.
1546 if (be16_to_cpu(hdr->bestfree[0].length) ==
1547 mp->m_dirblksize - (uint)sizeof(*hdr)) {
1548 ASSERT(db != mp->m_dirdatablk);
1549 if ((error = xfs_dir2_shrink_inode(args, db, dbp))) {
1551 * Nope, can't get rid of it because it caused
1552 * allocation of a bmap btree block to do so.
1553 * Just go on, returning success, leaving the
1554 * empty block in place.
1556 if (error == ENOSPC && args->total == 0) {
1557 xfs_da_buf_done(dbp);
1558 error = 0;
1560 xfs_dir2_leaf_check(dp, lbp);
1561 xfs_da_buf_done(lbp);
1562 return error;
1564 dbp = NULL;
1566 * If this is the last data block then compact the
1567 * bests table by getting rid of entries.
1569 if (db == be32_to_cpu(ltp->bestcount) - 1) {
1571 * Look for the last active entry (i).
1573 for (i = db - 1; i > 0; i--) {
1574 if (bestsp[i] != cpu_to_be16(NULLDATAOFF))
1575 break;
1578 * Copy the table down so inactive entries at the
1579 * end are removed.
1581 memmove(&bestsp[db - i], bestsp,
1582 (be32_to_cpu(ltp->bestcount) - (db - i)) * sizeof(*bestsp));
1583 be32_add_cpu(&ltp->bestcount, -(db - i));
1584 xfs_dir2_leaf_log_tail(tp, lbp);
1585 xfs_dir2_leaf_log_bests(tp, lbp, 0, be32_to_cpu(ltp->bestcount) - 1);
1586 } else
1587 bestsp[db] = cpu_to_be16(NULLDATAOFF);
1590 * If the data block was not the first one, drop it.
1592 else if (db != mp->m_dirdatablk && dbp != NULL) {
1593 xfs_da_buf_done(dbp);
1594 dbp = NULL;
1596 xfs_dir2_leaf_check(dp, lbp);
1598 * See if we can convert to block form.
1600 return xfs_dir2_leaf_to_block(args, lbp, dbp);
1604 * Replace the inode number in a leaf format directory entry.
1606 int /* error */
1607 xfs_dir2_leaf_replace(
1608 xfs_da_args_t *args) /* operation arguments */
1610 xfs_dabuf_t *dbp; /* data block buffer */
1611 xfs_dir2_data_entry_t *dep; /* data block entry */
1612 xfs_inode_t *dp; /* incore directory inode */
1613 int error; /* error return code */
1614 int index; /* index of leaf entry */
1615 xfs_dabuf_t *lbp; /* leaf buffer */
1616 xfs_dir2_leaf_t *leaf; /* leaf structure */
1617 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
1618 xfs_trans_t *tp; /* transaction pointer */
1620 trace_xfs_dir2_leaf_replace(args);
1623 * Look up the entry.
1625 if ((error = xfs_dir2_leaf_lookup_int(args, &lbp, &index, &dbp))) {
1626 return error;
1628 dp = args->dp;
1629 leaf = lbp->data;
1631 * Point to the leaf entry, get data address from it.
1633 lep = &leaf->ents[index];
1635 * Point to the data entry.
1637 dep = (xfs_dir2_data_entry_t *)
1638 ((char *)dbp->data +
1639 xfs_dir2_dataptr_to_off(dp->i_mount, be32_to_cpu(lep->address)));
1640 ASSERT(args->inumber != be64_to_cpu(dep->inumber));
1642 * Put the new inode number in, log it.
1644 dep->inumber = cpu_to_be64(args->inumber);
1645 tp = args->trans;
1646 xfs_dir2_data_log_entry(tp, dbp, dep);
1647 xfs_da_buf_done(dbp);
1648 xfs_dir2_leaf_check(dp, lbp);
1649 xfs_da_brelse(tp, lbp);
1650 return 0;
1654 * Return index in the leaf block (lbp) which is either the first
1655 * one with this hash value, or if there are none, the insert point
1656 * for that hash value.
1658 int /* index value */
1659 xfs_dir2_leaf_search_hash(
1660 xfs_da_args_t *args, /* operation arguments */
1661 xfs_dabuf_t *lbp) /* leaf buffer */
1663 xfs_dahash_t hash=0; /* hash from this entry */
1664 xfs_dahash_t hashwant; /* hash value looking for */
1665 int high; /* high leaf index */
1666 int low; /* low leaf index */
1667 xfs_dir2_leaf_t *leaf; /* leaf structure */
1668 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
1669 int mid=0; /* current leaf index */
1671 leaf = lbp->data;
1672 #ifndef __KERNEL__
1673 if (!leaf->hdr.count)
1674 return 0;
1675 #endif
1677 * Note, the table cannot be empty, so we have to go through the loop.
1678 * Binary search the leaf entries looking for our hash value.
1680 for (lep = leaf->ents, low = 0, high = be16_to_cpu(leaf->hdr.count) - 1,
1681 hashwant = args->hashval;
1682 low <= high; ) {
1683 mid = (low + high) >> 1;
1684 if ((hash = be32_to_cpu(lep[mid].hashval)) == hashwant)
1685 break;
1686 if (hash < hashwant)
1687 low = mid + 1;
1688 else
1689 high = mid - 1;
1692 * Found one, back up through all the equal hash values.
1694 if (hash == hashwant) {
1695 while (mid > 0 && be32_to_cpu(lep[mid - 1].hashval) == hashwant) {
1696 mid--;
1700 * Need to point to an entry higher than ours.
1702 else if (hash < hashwant)
1703 mid++;
1704 return mid;
1708 * Trim off a trailing data block. We know it's empty since the leaf
1709 * freespace table says so.
1711 int /* error */
1712 xfs_dir2_leaf_trim_data(
1713 xfs_da_args_t *args, /* operation arguments */
1714 xfs_dabuf_t *lbp, /* leaf buffer */
1715 xfs_dir2_db_t db) /* data block number */
1717 __be16 *bestsp; /* leaf bests table */
1718 xfs_dabuf_t *dbp; /* data block buffer */
1719 xfs_inode_t *dp; /* incore directory inode */
1720 int error; /* error return value */
1721 xfs_dir2_leaf_t *leaf; /* leaf structure */
1722 xfs_dir2_leaf_tail_t *ltp; /* leaf tail structure */
1723 xfs_mount_t *mp; /* filesystem mount point */
1724 xfs_trans_t *tp; /* transaction pointer */
1726 dp = args->dp;
1727 mp = dp->i_mount;
1728 tp = args->trans;
1730 * Read the offending data block. We need its buffer.
1732 if ((error = xfs_da_read_buf(tp, dp, xfs_dir2_db_to_da(mp, db), -1, &dbp,
1733 XFS_DATA_FORK))) {
1734 return error;
1737 leaf = lbp->data;
1738 ltp = xfs_dir2_leaf_tail_p(mp, leaf);
1740 #ifdef DEBUG
1742 struct xfs_dir2_data_hdr *hdr = dbp->data;
1744 ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC));
1745 ASSERT(be16_to_cpu(hdr->bestfree[0].length) ==
1746 mp->m_dirblksize - (uint)sizeof(*hdr));
1747 ASSERT(db == be32_to_cpu(ltp->bestcount) - 1);
1749 #endif
1752 * Get rid of the data block.
1754 if ((error = xfs_dir2_shrink_inode(args, db, dbp))) {
1755 ASSERT(error != ENOSPC);
1756 xfs_da_brelse(tp, dbp);
1757 return error;
1760 * Eliminate the last bests entry from the table.
1762 bestsp = xfs_dir2_leaf_bests_p(ltp);
1763 be32_add_cpu(&ltp->bestcount, -1);
1764 memmove(&bestsp[1], &bestsp[0], be32_to_cpu(ltp->bestcount) * sizeof(*bestsp));
1765 xfs_dir2_leaf_log_tail(tp, lbp);
1766 xfs_dir2_leaf_log_bests(tp, lbp, 0, be32_to_cpu(ltp->bestcount) - 1);
1767 return 0;
1770 static inline size_t
1771 xfs_dir2_leaf_size(
1772 struct xfs_dir2_leaf_hdr *hdr,
1773 int counts)
1775 int entries;
1777 entries = be16_to_cpu(hdr->count) - be16_to_cpu(hdr->stale);
1778 return sizeof(xfs_dir2_leaf_hdr_t) +
1779 entries * sizeof(xfs_dir2_leaf_entry_t) +
1780 counts * sizeof(xfs_dir2_data_off_t) +
1781 sizeof(xfs_dir2_leaf_tail_t);
1785 * Convert node form directory to leaf form directory.
1786 * The root of the node form dir needs to already be a LEAFN block.
1787 * Just return if we can't do anything.
1789 int /* error */
1790 xfs_dir2_node_to_leaf(
1791 xfs_da_state_t *state) /* directory operation state */
1793 xfs_da_args_t *args; /* operation arguments */
1794 xfs_inode_t *dp; /* incore directory inode */
1795 int error; /* error return code */
1796 xfs_dabuf_t *fbp; /* buffer for freespace block */
1797 xfs_fileoff_t fo; /* freespace file offset */
1798 xfs_dir2_free_t *free; /* freespace structure */
1799 xfs_dabuf_t *lbp; /* buffer for leaf block */
1800 xfs_dir2_leaf_tail_t *ltp; /* tail of leaf structure */
1801 xfs_dir2_leaf_t *leaf; /* leaf structure */
1802 xfs_mount_t *mp; /* filesystem mount point */
1803 int rval; /* successful free trim? */
1804 xfs_trans_t *tp; /* transaction pointer */
1807 * There's more than a leaf level in the btree, so there must
1808 * be multiple leafn blocks. Give up.
1810 if (state->path.active > 1)
1811 return 0;
1812 args = state->args;
1814 trace_xfs_dir2_node_to_leaf(args);
1816 mp = state->mp;
1817 dp = args->dp;
1818 tp = args->trans;
1820 * Get the last offset in the file.
1822 if ((error = xfs_bmap_last_offset(tp, dp, &fo, XFS_DATA_FORK))) {
1823 return error;
1825 fo -= mp->m_dirblkfsbs;
1827 * If there are freespace blocks other than the first one,
1828 * take this opportunity to remove trailing empty freespace blocks
1829 * that may have been left behind during no-space-reservation
1830 * operations.
1832 while (fo > mp->m_dirfreeblk) {
1833 if ((error = xfs_dir2_node_trim_free(args, fo, &rval))) {
1834 return error;
1836 if (rval)
1837 fo -= mp->m_dirblkfsbs;
1838 else
1839 return 0;
1842 * Now find the block just before the freespace block.
1844 if ((error = xfs_bmap_last_before(tp, dp, &fo, XFS_DATA_FORK))) {
1845 return error;
1848 * If it's not the single leaf block, give up.
1850 if (XFS_FSB_TO_B(mp, fo) > XFS_DIR2_LEAF_OFFSET + mp->m_dirblksize)
1851 return 0;
1852 lbp = state->path.blk[0].bp;
1853 leaf = lbp->data;
1854 ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAFN_MAGIC));
1856 * Read the freespace block.
1858 if ((error = xfs_da_read_buf(tp, dp, mp->m_dirfreeblk, -1, &fbp,
1859 XFS_DATA_FORK))) {
1860 return error;
1862 free = fbp->data;
1863 ASSERT(free->hdr.magic == cpu_to_be32(XFS_DIR2_FREE_MAGIC));
1864 ASSERT(!free->hdr.firstdb);
1867 * Now see if the leafn and free data will fit in a leaf1.
1868 * If not, release the buffer and give up.
1870 if (xfs_dir2_leaf_size(&leaf->hdr, be32_to_cpu(free->hdr.nvalid)) >
1871 mp->m_dirblksize) {
1872 xfs_da_brelse(tp, fbp);
1873 return 0;
1877 * If the leaf has any stale entries in it, compress them out.
1878 * The compact routine will log the header.
1880 if (be16_to_cpu(leaf->hdr.stale))
1881 xfs_dir2_leaf_compact(args, lbp);
1882 else
1883 xfs_dir2_leaf_log_header(tp, lbp);
1884 leaf->hdr.info.magic = cpu_to_be16(XFS_DIR2_LEAF1_MAGIC);
1886 * Set up the leaf tail from the freespace block.
1888 ltp = xfs_dir2_leaf_tail_p(mp, leaf);
1889 ltp->bestcount = free->hdr.nvalid;
1891 * Set up the leaf bests table.
1893 memcpy(xfs_dir2_leaf_bests_p(ltp), free->bests,
1894 be32_to_cpu(ltp->bestcount) * sizeof(xfs_dir2_data_off_t));
1895 xfs_dir2_leaf_log_bests(tp, lbp, 0, be32_to_cpu(ltp->bestcount) - 1);
1896 xfs_dir2_leaf_log_tail(tp, lbp);
1897 xfs_dir2_leaf_check(dp, lbp);
1899 * Get rid of the freespace block.
1901 error = xfs_dir2_shrink_inode(args, XFS_DIR2_FREE_FIRSTDB(mp), fbp);
1902 if (error) {
1904 * This can't fail here because it can only happen when
1905 * punching out the middle of an extent, and this is an
1906 * isolated block.
1908 ASSERT(error != ENOSPC);
1909 return error;
1911 fbp = NULL;
1913 * Now see if we can convert the single-leaf directory
1914 * down to a block form directory.
1915 * This routine always kills the dabuf for the leaf, so
1916 * eliminate it from the path.
1918 error = xfs_dir2_leaf_to_block(args, lbp, NULL);
1919 state->path.blk[0].bp = NULL;
1920 return error;