eCryptfs: Remove mmap from directory operations
[linux/fpc-iii.git] / fs / xfs / xfs_dir2_leaf.c
blob29f484c11b3ac11f5f9f89d7e54cf5e332aab2f6
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_dir2.h"
28 #include "xfs_dmapi.h"
29 #include "xfs_mount.h"
30 #include "xfs_da_btree.h"
31 #include "xfs_bmap_btree.h"
32 #include "xfs_attr_sf.h"
33 #include "xfs_dir2_sf.h"
34 #include "xfs_dinode.h"
35 #include "xfs_inode.h"
36 #include "xfs_bmap.h"
37 #include "xfs_dir2_data.h"
38 #include "xfs_dir2_leaf.h"
39 #include "xfs_dir2_block.h"
40 #include "xfs_dir2_node.h"
41 #include "xfs_error.h"
42 #include "xfs_trace.h"
45 * Local function declarations.
47 #ifdef DEBUG
48 static void xfs_dir2_leaf_check(xfs_inode_t *dp, xfs_dabuf_t *bp);
49 #else
50 #define xfs_dir2_leaf_check(dp, bp)
51 #endif
52 static int xfs_dir2_leaf_lookup_int(xfs_da_args_t *args, xfs_dabuf_t **lbpp,
53 int *indexp, xfs_dabuf_t **dbpp);
54 static void xfs_dir2_leaf_log_bests(struct xfs_trans *tp, struct xfs_dabuf *bp,
55 int first, int last);
56 static void xfs_dir2_leaf_log_tail(struct xfs_trans *tp, struct xfs_dabuf *bp);
60 * Convert a block form directory to a leaf form directory.
62 int /* error */
63 xfs_dir2_block_to_leaf(
64 xfs_da_args_t *args, /* operation arguments */
65 xfs_dabuf_t *dbp) /* input block's buffer */
67 __be16 *bestsp; /* leaf's bestsp entries */
68 xfs_dablk_t blkno; /* leaf block's bno */
69 xfs_dir2_block_t *block; /* block structure */
70 xfs_dir2_leaf_entry_t *blp; /* block's leaf entries */
71 xfs_dir2_block_tail_t *btp; /* block's tail */
72 xfs_inode_t *dp; /* incore directory inode */
73 int error; /* error return code */
74 xfs_dabuf_t *lbp; /* leaf block's buffer */
75 xfs_dir2_db_t ldb; /* leaf block's bno */
76 xfs_dir2_leaf_t *leaf; /* leaf structure */
77 xfs_dir2_leaf_tail_t *ltp; /* leaf's tail */
78 xfs_mount_t *mp; /* filesystem mount point */
79 int needlog; /* need to log block header */
80 int needscan; /* need to rescan bestfree */
81 xfs_trans_t *tp; /* transaction pointer */
83 trace_xfs_dir2_block_to_leaf(args);
85 dp = args->dp;
86 mp = dp->i_mount;
87 tp = args->trans;
89 * Add the leaf block to the inode.
90 * This interface will only put blocks in the leaf/node range.
91 * Since that's empty now, we'll get the root (block 0 in range).
93 if ((error = xfs_da_grow_inode(args, &blkno))) {
94 return error;
96 ldb = xfs_dir2_da_to_db(mp, blkno);
97 ASSERT(ldb == XFS_DIR2_LEAF_FIRSTDB(mp));
99 * Initialize the leaf block, get a buffer for it.
101 if ((error = xfs_dir2_leaf_init(args, ldb, &lbp, XFS_DIR2_LEAF1_MAGIC))) {
102 return error;
104 ASSERT(lbp != NULL);
105 leaf = lbp->data;
106 block = dbp->data;
107 xfs_dir2_data_check(dp, dbp);
108 btp = xfs_dir2_block_tail_p(mp, block);
109 blp = xfs_dir2_block_leaf_p(btp);
111 * Set the counts in the leaf header.
113 leaf->hdr.count = cpu_to_be16(be32_to_cpu(btp->count));
114 leaf->hdr.stale = cpu_to_be16(be32_to_cpu(btp->stale));
116 * Could compact these but I think we always do the conversion
117 * after squeezing out stale entries.
119 memcpy(leaf->ents, blp, be32_to_cpu(btp->count) * sizeof(xfs_dir2_leaf_entry_t));
120 xfs_dir2_leaf_log_ents(tp, lbp, 0, be16_to_cpu(leaf->hdr.count) - 1);
121 needscan = 0;
122 needlog = 1;
124 * Make the space formerly occupied by the leaf entries and block
125 * tail be free.
127 xfs_dir2_data_make_free(tp, dbp,
128 (xfs_dir2_data_aoff_t)((char *)blp - (char *)block),
129 (xfs_dir2_data_aoff_t)((char *)block + mp->m_dirblksize -
130 (char *)blp),
131 &needlog, &needscan);
133 * Fix up the block header, make it a data block.
135 block->hdr.magic = cpu_to_be32(XFS_DIR2_DATA_MAGIC);
136 if (needscan)
137 xfs_dir2_data_freescan(mp, (xfs_dir2_data_t *)block, &needlog);
139 * Set up leaf tail and bests table.
141 ltp = xfs_dir2_leaf_tail_p(mp, leaf);
142 ltp->bestcount = cpu_to_be32(1);
143 bestsp = xfs_dir2_leaf_bests_p(ltp);
144 bestsp[0] = block->hdr.bestfree[0].length;
146 * Log the data header and leaf bests table.
148 if (needlog)
149 xfs_dir2_data_log_header(tp, dbp);
150 xfs_dir2_leaf_check(dp, lbp);
151 xfs_dir2_data_check(dp, dbp);
152 xfs_dir2_leaf_log_bests(tp, lbp, 0, 0);
153 xfs_da_buf_done(lbp);
154 return 0;
158 * Add an entry to a leaf form directory.
160 int /* error */
161 xfs_dir2_leaf_addname(
162 xfs_da_args_t *args) /* operation arguments */
164 __be16 *bestsp; /* freespace table in leaf */
165 int compact; /* need to compact leaves */
166 xfs_dir2_data_t *data; /* data block structure */
167 xfs_dabuf_t *dbp; /* data block buffer */
168 xfs_dir2_data_entry_t *dep; /* data block entry */
169 xfs_inode_t *dp; /* incore directory inode */
170 xfs_dir2_data_unused_t *dup; /* data unused entry */
171 int error; /* error return value */
172 int grown; /* allocated new data block */
173 int highstale; /* index of next stale leaf */
174 int i; /* temporary, index */
175 int index; /* leaf table position */
176 xfs_dabuf_t *lbp; /* leaf's buffer */
177 xfs_dir2_leaf_t *leaf; /* leaf structure */
178 int length; /* length of new entry */
179 xfs_dir2_leaf_entry_t *lep; /* leaf entry table pointer */
180 int lfloglow; /* low leaf logging index */
181 int lfloghigh; /* high leaf logging index */
182 int lowstale; /* index of prev stale leaf */
183 xfs_dir2_leaf_tail_t *ltp; /* leaf tail pointer */
184 xfs_mount_t *mp; /* filesystem mount point */
185 int needbytes; /* leaf block bytes needed */
186 int needlog; /* need to log data header */
187 int needscan; /* need to rescan data free */
188 __be16 *tagp; /* end of data entry */
189 xfs_trans_t *tp; /* transaction pointer */
190 xfs_dir2_db_t use_block; /* data block number */
192 trace_xfs_dir2_leaf_addname(args);
194 dp = args->dp;
195 tp = args->trans;
196 mp = dp->i_mount;
198 * Read the leaf block.
200 error = xfs_da_read_buf(tp, dp, mp->m_dirleafblk, -1, &lbp,
201 XFS_DATA_FORK);
202 if (error) {
203 return error;
205 ASSERT(lbp != NULL);
207 * Look up the entry by hash value and name.
208 * We know it's not there, our caller has already done a lookup.
209 * So the index is of the entry to insert in front of.
210 * But if there are dup hash values the index is of the first of those.
212 index = xfs_dir2_leaf_search_hash(args, lbp);
213 leaf = lbp->data;
214 ltp = xfs_dir2_leaf_tail_p(mp, leaf);
215 bestsp = xfs_dir2_leaf_bests_p(ltp);
216 length = xfs_dir2_data_entsize(args->namelen);
218 * See if there are any entries with the same hash value
219 * and space in their block for the new entry.
220 * This is good because it puts multiple same-hash value entries
221 * in a data block, improving the lookup of those entries.
223 for (use_block = -1, lep = &leaf->ents[index];
224 index < be16_to_cpu(leaf->hdr.count) && be32_to_cpu(lep->hashval) == args->hashval;
225 index++, lep++) {
226 if (be32_to_cpu(lep->address) == XFS_DIR2_NULL_DATAPTR)
227 continue;
228 i = xfs_dir2_dataptr_to_db(mp, be32_to_cpu(lep->address));
229 ASSERT(i < be32_to_cpu(ltp->bestcount));
230 ASSERT(be16_to_cpu(bestsp[i]) != NULLDATAOFF);
231 if (be16_to_cpu(bestsp[i]) >= length) {
232 use_block = i;
233 break;
237 * Didn't find a block yet, linear search all the data blocks.
239 if (use_block == -1) {
240 for (i = 0; i < be32_to_cpu(ltp->bestcount); i++) {
242 * Remember a block we see that's missing.
244 if (be16_to_cpu(bestsp[i]) == NULLDATAOFF && use_block == -1)
245 use_block = i;
246 else if (be16_to_cpu(bestsp[i]) >= length) {
247 use_block = i;
248 break;
253 * How many bytes do we need in the leaf block?
255 needbytes =
256 (leaf->hdr.stale ? 0 : (uint)sizeof(leaf->ents[0])) +
257 (use_block != -1 ? 0 : (uint)sizeof(leaf->bests[0]));
259 * Now kill use_block if it refers to a missing block, so we
260 * can use it as an indication of allocation needed.
262 if (use_block != -1 && be16_to_cpu(bestsp[use_block]) == NULLDATAOFF)
263 use_block = -1;
265 * If we don't have enough free bytes but we can make enough
266 * by compacting out stale entries, we'll do that.
268 if ((char *)bestsp - (char *)&leaf->ents[be16_to_cpu(leaf->hdr.count)] <
269 needbytes && be16_to_cpu(leaf->hdr.stale) > 1) {
270 compact = 1;
273 * Otherwise if we don't have enough free bytes we need to
274 * convert to node form.
276 else if ((char *)bestsp - (char *)&leaf->ents[be16_to_cpu(
277 leaf->hdr.count)] < needbytes) {
279 * Just checking or no space reservation, give up.
281 if ((args->op_flags & XFS_DA_OP_JUSTCHECK) ||
282 args->total == 0) {
283 xfs_da_brelse(tp, lbp);
284 return XFS_ERROR(ENOSPC);
287 * Convert to node form.
289 error = xfs_dir2_leaf_to_node(args, lbp);
290 xfs_da_buf_done(lbp);
291 if (error)
292 return error;
294 * Then add the new entry.
296 return xfs_dir2_node_addname(args);
299 * Otherwise it will fit without compaction.
301 else
302 compact = 0;
304 * If just checking, then it will fit unless we needed to allocate
305 * a new data block.
307 if (args->op_flags & XFS_DA_OP_JUSTCHECK) {
308 xfs_da_brelse(tp, lbp);
309 return use_block == -1 ? XFS_ERROR(ENOSPC) : 0;
312 * If no allocations are allowed, return now before we've
313 * changed anything.
315 if (args->total == 0 && use_block == -1) {
316 xfs_da_brelse(tp, lbp);
317 return XFS_ERROR(ENOSPC);
320 * Need to compact the leaf entries, removing stale ones.
321 * Leave one stale entry behind - the one closest to our
322 * insertion index - and we'll shift that one to our insertion
323 * point later.
325 if (compact) {
326 xfs_dir2_leaf_compact_x1(lbp, &index, &lowstale, &highstale,
327 &lfloglow, &lfloghigh);
330 * There are stale entries, so we'll need log-low and log-high
331 * impossibly bad values later.
333 else if (be16_to_cpu(leaf->hdr.stale)) {
334 lfloglow = be16_to_cpu(leaf->hdr.count);
335 lfloghigh = -1;
338 * If there was no data block space found, we need to allocate
339 * a new one.
341 if (use_block == -1) {
343 * Add the new data block.
345 if ((error = xfs_dir2_grow_inode(args, XFS_DIR2_DATA_SPACE,
346 &use_block))) {
347 xfs_da_brelse(tp, lbp);
348 return error;
351 * Initialize the block.
353 if ((error = xfs_dir2_data_init(args, use_block, &dbp))) {
354 xfs_da_brelse(tp, lbp);
355 return error;
358 * If we're adding a new data block on the end we need to
359 * extend the bests table. Copy it up one entry.
361 if (use_block >= be32_to_cpu(ltp->bestcount)) {
362 bestsp--;
363 memmove(&bestsp[0], &bestsp[1],
364 be32_to_cpu(ltp->bestcount) * sizeof(bestsp[0]));
365 be32_add_cpu(&ltp->bestcount, 1);
366 xfs_dir2_leaf_log_tail(tp, lbp);
367 xfs_dir2_leaf_log_bests(tp, lbp, 0, be32_to_cpu(ltp->bestcount) - 1);
370 * If we're filling in a previously empty block just log it.
372 else
373 xfs_dir2_leaf_log_bests(tp, lbp, use_block, use_block);
374 data = dbp->data;
375 bestsp[use_block] = data->hdr.bestfree[0].length;
376 grown = 1;
379 * Already had space in some data block.
380 * Just read that one in.
382 else {
383 if ((error =
384 xfs_da_read_buf(tp, dp, xfs_dir2_db_to_da(mp, use_block),
385 -1, &dbp, XFS_DATA_FORK))) {
386 xfs_da_brelse(tp, lbp);
387 return error;
389 data = dbp->data;
390 grown = 0;
392 xfs_dir2_data_check(dp, dbp);
394 * Point to the biggest freespace in our data block.
396 dup = (xfs_dir2_data_unused_t *)
397 ((char *)data + be16_to_cpu(data->hdr.bestfree[0].offset));
398 ASSERT(be16_to_cpu(dup->length) >= length);
399 needscan = needlog = 0;
401 * Mark the initial part of our freespace in use for the new entry.
403 xfs_dir2_data_use_free(tp, dbp, dup,
404 (xfs_dir2_data_aoff_t)((char *)dup - (char *)data), length,
405 &needlog, &needscan);
407 * Initialize our new entry (at last).
409 dep = (xfs_dir2_data_entry_t *)dup;
410 dep->inumber = cpu_to_be64(args->inumber);
411 dep->namelen = args->namelen;
412 memcpy(dep->name, args->name, dep->namelen);
413 tagp = xfs_dir2_data_entry_tag_p(dep);
414 *tagp = cpu_to_be16((char *)dep - (char *)data);
416 * Need to scan fix up the bestfree table.
418 if (needscan)
419 xfs_dir2_data_freescan(mp, data, &needlog);
421 * Need to log the data block's header.
423 if (needlog)
424 xfs_dir2_data_log_header(tp, dbp);
425 xfs_dir2_data_log_entry(tp, dbp, dep);
427 * If the bests table needs to be changed, do it.
428 * Log the change unless we've already done that.
430 if (be16_to_cpu(bestsp[use_block]) != be16_to_cpu(data->hdr.bestfree[0].length)) {
431 bestsp[use_block] = data->hdr.bestfree[0].length;
432 if (!grown)
433 xfs_dir2_leaf_log_bests(tp, lbp, use_block, use_block);
436 * Now we need to make room to insert the leaf entry.
437 * If there are no stale entries, we just insert a hole at index.
439 if (!leaf->hdr.stale) {
441 * lep is still good as the index leaf entry.
443 if (index < be16_to_cpu(leaf->hdr.count))
444 memmove(lep + 1, lep,
445 (be16_to_cpu(leaf->hdr.count) - index) * sizeof(*lep));
447 * Record low and high logging indices for the leaf.
449 lfloglow = index;
450 lfloghigh = be16_to_cpu(leaf->hdr.count);
451 be16_add_cpu(&leaf->hdr.count, 1);
454 * There are stale entries.
455 * We will use one of them for the new entry.
456 * It's probably not at the right location, so we'll have to
457 * shift some up or down first.
459 else {
461 * If we didn't compact before, we need to find the nearest
462 * stale entries before and after our insertion point.
464 if (compact == 0) {
466 * Find the first stale entry before the insertion
467 * point, if any.
469 for (lowstale = index - 1;
470 lowstale >= 0 &&
471 be32_to_cpu(leaf->ents[lowstale].address) !=
472 XFS_DIR2_NULL_DATAPTR;
473 lowstale--)
474 continue;
476 * Find the next stale entry at or after the insertion
477 * point, if any. Stop if we go so far that the
478 * lowstale entry would be better.
480 for (highstale = index;
481 highstale < be16_to_cpu(leaf->hdr.count) &&
482 be32_to_cpu(leaf->ents[highstale].address) !=
483 XFS_DIR2_NULL_DATAPTR &&
484 (lowstale < 0 ||
485 index - lowstale - 1 >= highstale - index);
486 highstale++)
487 continue;
490 * If the low one is better, use it.
492 if (lowstale >= 0 &&
493 (highstale == be16_to_cpu(leaf->hdr.count) ||
494 index - lowstale - 1 < highstale - index)) {
495 ASSERT(index - lowstale - 1 >= 0);
496 ASSERT(be32_to_cpu(leaf->ents[lowstale].address) ==
497 XFS_DIR2_NULL_DATAPTR);
499 * Copy entries up to cover the stale entry
500 * and make room for the new entry.
502 if (index - lowstale - 1 > 0)
503 memmove(&leaf->ents[lowstale],
504 &leaf->ents[lowstale + 1],
505 (index - lowstale - 1) * sizeof(*lep));
506 lep = &leaf->ents[index - 1];
507 lfloglow = MIN(lowstale, lfloglow);
508 lfloghigh = MAX(index - 1, lfloghigh);
511 * The high one is better, so use that one.
513 else {
514 ASSERT(highstale - index >= 0);
515 ASSERT(be32_to_cpu(leaf->ents[highstale].address) ==
516 XFS_DIR2_NULL_DATAPTR);
518 * Copy entries down to cover the stale entry
519 * and make room for the new entry.
521 if (highstale - index > 0)
522 memmove(&leaf->ents[index + 1],
523 &leaf->ents[index],
524 (highstale - index) * sizeof(*lep));
525 lep = &leaf->ents[index];
526 lfloglow = MIN(index, lfloglow);
527 lfloghigh = MAX(highstale, lfloghigh);
529 be16_add_cpu(&leaf->hdr.stale, -1);
532 * Fill in the new leaf entry.
534 lep->hashval = cpu_to_be32(args->hashval);
535 lep->address = cpu_to_be32(xfs_dir2_db_off_to_dataptr(mp, use_block,
536 be16_to_cpu(*tagp)));
538 * Log the leaf fields and give up the buffers.
540 xfs_dir2_leaf_log_header(tp, lbp);
541 xfs_dir2_leaf_log_ents(tp, lbp, lfloglow, lfloghigh);
542 xfs_dir2_leaf_check(dp, lbp);
543 xfs_da_buf_done(lbp);
544 xfs_dir2_data_check(dp, dbp);
545 xfs_da_buf_done(dbp);
546 return 0;
549 #ifdef DEBUG
551 * Check the internal consistency of a leaf1 block.
552 * Pop an assert if something is wrong.
554 STATIC void
555 xfs_dir2_leaf_check(
556 xfs_inode_t *dp, /* incore directory inode */
557 xfs_dabuf_t *bp) /* leaf's buffer */
559 int i; /* leaf index */
560 xfs_dir2_leaf_t *leaf; /* leaf structure */
561 xfs_dir2_leaf_tail_t *ltp; /* leaf tail pointer */
562 xfs_mount_t *mp; /* filesystem mount point */
563 int stale; /* count of stale leaves */
565 leaf = bp->data;
566 mp = dp->i_mount;
567 ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_DIR2_LEAF1_MAGIC);
569 * This value is not restrictive enough.
570 * Should factor in the size of the bests table as well.
571 * We can deduce a value for that from di_size.
573 ASSERT(be16_to_cpu(leaf->hdr.count) <= xfs_dir2_max_leaf_ents(mp));
574 ltp = xfs_dir2_leaf_tail_p(mp, leaf);
576 * Leaves and bests don't overlap.
578 ASSERT((char *)&leaf->ents[be16_to_cpu(leaf->hdr.count)] <=
579 (char *)xfs_dir2_leaf_bests_p(ltp));
581 * Check hash value order, count stale entries.
583 for (i = stale = 0; i < be16_to_cpu(leaf->hdr.count); i++) {
584 if (i + 1 < be16_to_cpu(leaf->hdr.count))
585 ASSERT(be32_to_cpu(leaf->ents[i].hashval) <=
586 be32_to_cpu(leaf->ents[i + 1].hashval));
587 if (be32_to_cpu(leaf->ents[i].address) == XFS_DIR2_NULL_DATAPTR)
588 stale++;
590 ASSERT(be16_to_cpu(leaf->hdr.stale) == stale);
592 #endif /* DEBUG */
595 * Compact out any stale entries in the leaf.
596 * Log the header and changed leaf entries, if any.
598 void
599 xfs_dir2_leaf_compact(
600 xfs_da_args_t *args, /* operation arguments */
601 xfs_dabuf_t *bp) /* leaf buffer */
603 int from; /* source leaf index */
604 xfs_dir2_leaf_t *leaf; /* leaf structure */
605 int loglow; /* first leaf entry to log */
606 int to; /* target leaf index */
608 leaf = bp->data;
609 if (!leaf->hdr.stale) {
610 return;
613 * Compress out the stale entries in place.
615 for (from = to = 0, loglow = -1; from < be16_to_cpu(leaf->hdr.count); from++) {
616 if (be32_to_cpu(leaf->ents[from].address) == XFS_DIR2_NULL_DATAPTR)
617 continue;
619 * Only actually copy the entries that are different.
621 if (from > to) {
622 if (loglow == -1)
623 loglow = to;
624 leaf->ents[to] = leaf->ents[from];
626 to++;
629 * Update and log the header, log the leaf entries.
631 ASSERT(be16_to_cpu(leaf->hdr.stale) == from - to);
632 be16_add_cpu(&leaf->hdr.count, -(be16_to_cpu(leaf->hdr.stale)));
633 leaf->hdr.stale = 0;
634 xfs_dir2_leaf_log_header(args->trans, bp);
635 if (loglow != -1)
636 xfs_dir2_leaf_log_ents(args->trans, bp, loglow, to - 1);
640 * Compact the leaf entries, removing stale ones.
641 * Leave one stale entry behind - the one closest to our
642 * insertion index - and the caller will shift that one to our insertion
643 * point later.
644 * Return new insertion index, where the remaining stale entry is,
645 * and leaf logging indices.
647 void
648 xfs_dir2_leaf_compact_x1(
649 xfs_dabuf_t *bp, /* leaf buffer */
650 int *indexp, /* insertion index */
651 int *lowstalep, /* out: stale entry before us */
652 int *highstalep, /* out: stale entry after us */
653 int *lowlogp, /* out: low log index */
654 int *highlogp) /* out: high log index */
656 int from; /* source copy index */
657 int highstale; /* stale entry at/after index */
658 int index; /* insertion index */
659 int keepstale; /* source index of kept stale */
660 xfs_dir2_leaf_t *leaf; /* leaf structure */
661 int lowstale; /* stale entry before index */
662 int newindex=0; /* new insertion index */
663 int to; /* destination copy index */
665 leaf = bp->data;
666 ASSERT(be16_to_cpu(leaf->hdr.stale) > 1);
667 index = *indexp;
669 * Find the first stale entry before our index, if any.
671 for (lowstale = index - 1;
672 lowstale >= 0 &&
673 be32_to_cpu(leaf->ents[lowstale].address) != XFS_DIR2_NULL_DATAPTR;
674 lowstale--)
675 continue;
677 * Find the first stale entry at or after our index, if any.
678 * Stop if the answer would be worse than lowstale.
680 for (highstale = index;
681 highstale < be16_to_cpu(leaf->hdr.count) &&
682 be32_to_cpu(leaf->ents[highstale].address) != XFS_DIR2_NULL_DATAPTR &&
683 (lowstale < 0 || index - lowstale > highstale - index);
684 highstale++)
685 continue;
687 * Pick the better of lowstale and highstale.
689 if (lowstale >= 0 &&
690 (highstale == be16_to_cpu(leaf->hdr.count) ||
691 index - lowstale <= highstale - index))
692 keepstale = lowstale;
693 else
694 keepstale = highstale;
696 * Copy the entries in place, removing all the stale entries
697 * except keepstale.
699 for (from = to = 0; from < be16_to_cpu(leaf->hdr.count); from++) {
701 * Notice the new value of index.
703 if (index == from)
704 newindex = to;
705 if (from != keepstale &&
706 be32_to_cpu(leaf->ents[from].address) == XFS_DIR2_NULL_DATAPTR) {
707 if (from == to)
708 *lowlogp = to;
709 continue;
712 * Record the new keepstale value for the insertion.
714 if (from == keepstale)
715 lowstale = highstale = to;
717 * Copy only the entries that have moved.
719 if (from > to)
720 leaf->ents[to] = leaf->ents[from];
721 to++;
723 ASSERT(from > to);
725 * If the insertion point was past the last entry,
726 * set the new insertion point accordingly.
728 if (index == from)
729 newindex = to;
730 *indexp = newindex;
732 * Adjust the leaf header values.
734 be16_add_cpu(&leaf->hdr.count, -(from - to));
735 leaf->hdr.stale = cpu_to_be16(1);
737 * Remember the low/high stale value only in the "right"
738 * direction.
740 if (lowstale >= newindex)
741 lowstale = -1;
742 else
743 highstale = be16_to_cpu(leaf->hdr.count);
744 *highlogp = be16_to_cpu(leaf->hdr.count) - 1;
745 *lowstalep = lowstale;
746 *highstalep = highstale;
750 * Getdents (readdir) for leaf and node directories.
751 * This reads the data blocks only, so is the same for both forms.
753 int /* error */
754 xfs_dir2_leaf_getdents(
755 xfs_inode_t *dp, /* incore directory inode */
756 void *dirent,
757 size_t bufsize,
758 xfs_off_t *offset,
759 filldir_t filldir)
761 xfs_dabuf_t *bp; /* data block buffer */
762 int byteoff; /* offset in current block */
763 xfs_dir2_db_t curdb; /* db for current block */
764 xfs_dir2_off_t curoff; /* current overall offset */
765 xfs_dir2_data_t *data; /* data block structure */
766 xfs_dir2_data_entry_t *dep; /* data entry */
767 xfs_dir2_data_unused_t *dup; /* unused entry */
768 int error = 0; /* error return value */
769 int i; /* temporary loop index */
770 int j; /* temporary loop index */
771 int length; /* temporary length value */
772 xfs_bmbt_irec_t *map; /* map vector for blocks */
773 xfs_extlen_t map_blocks; /* number of fsbs in map */
774 xfs_dablk_t map_off; /* last mapped file offset */
775 int map_size; /* total entries in *map */
776 int map_valid; /* valid entries in *map */
777 xfs_mount_t *mp; /* filesystem mount point */
778 xfs_dir2_off_t newoff; /* new curoff after new blk */
779 int nmap; /* mappings to ask xfs_bmapi */
780 char *ptr = NULL; /* pointer to current data */
781 int ra_current; /* number of read-ahead blks */
782 int ra_index; /* *map index for read-ahead */
783 int ra_offset; /* map entry offset for ra */
784 int ra_want; /* readahead count wanted */
787 * If the offset is at or past the largest allowed value,
788 * give up right away.
790 if (*offset >= XFS_DIR2_MAX_DATAPTR)
791 return 0;
793 mp = dp->i_mount;
796 * Set up to bmap a number of blocks based on the caller's
797 * buffer size, the directory block size, and the filesystem
798 * block size.
800 map_size = howmany(bufsize + mp->m_dirblksize, mp->m_sb.sb_blocksize);
801 map = kmem_alloc(map_size * sizeof(*map), KM_SLEEP);
802 map_valid = ra_index = ra_offset = ra_current = map_blocks = 0;
803 bp = NULL;
806 * Inside the loop we keep the main offset value as a byte offset
807 * in the directory file.
809 curoff = xfs_dir2_dataptr_to_byte(mp, *offset);
812 * Force this conversion through db so we truncate the offset
813 * down to get the start of the data block.
815 map_off = xfs_dir2_db_to_da(mp, xfs_dir2_byte_to_db(mp, curoff));
817 * Loop over directory entries until we reach the end offset.
818 * Get more blocks and readahead as necessary.
820 while (curoff < XFS_DIR2_LEAF_OFFSET) {
822 * If we have no buffer, or we're off the end of the
823 * current buffer, need to get another one.
825 if (!bp || ptr >= (char *)bp->data + mp->m_dirblksize) {
827 * If we have a buffer, we need to release it and
828 * take it out of the mapping.
830 if (bp) {
831 xfs_da_brelse(NULL, bp);
832 bp = NULL;
833 map_blocks -= mp->m_dirblkfsbs;
835 * Loop to get rid of the extents for the
836 * directory block.
838 for (i = mp->m_dirblkfsbs; i > 0; ) {
839 j = MIN((int)map->br_blockcount, i);
840 map->br_blockcount -= j;
841 map->br_startblock += j;
842 map->br_startoff += j;
844 * If mapping is done, pitch it from
845 * the table.
847 if (!map->br_blockcount && --map_valid)
848 memmove(&map[0], &map[1],
849 sizeof(map[0]) *
850 map_valid);
851 i -= j;
855 * Recalculate the readahead blocks wanted.
857 ra_want = howmany(bufsize + mp->m_dirblksize,
858 mp->m_sb.sb_blocksize) - 1;
859 ASSERT(ra_want >= 0);
862 * If we don't have as many as we want, and we haven't
863 * run out of data blocks, get some more mappings.
865 if (1 + ra_want > map_blocks &&
866 map_off <
867 xfs_dir2_byte_to_da(mp, XFS_DIR2_LEAF_OFFSET)) {
869 * Get more bmaps, fill in after the ones
870 * we already have in the table.
872 nmap = map_size - map_valid;
873 error = xfs_bmapi(NULL, dp,
874 map_off,
875 xfs_dir2_byte_to_da(mp,
876 XFS_DIR2_LEAF_OFFSET) - map_off,
877 XFS_BMAPI_METADATA, NULL, 0,
878 &map[map_valid], &nmap, NULL, NULL);
880 * Don't know if we should ignore this or
881 * try to return an error.
882 * The trouble with returning errors
883 * is that readdir will just stop without
884 * actually passing the error through.
886 if (error)
887 break; /* XXX */
889 * If we got all the mappings we asked for,
890 * set the final map offset based on the
891 * last bmap value received.
892 * Otherwise, we've reached the end.
894 if (nmap == map_size - map_valid)
895 map_off =
896 map[map_valid + nmap - 1].br_startoff +
897 map[map_valid + nmap - 1].br_blockcount;
898 else
899 map_off =
900 xfs_dir2_byte_to_da(mp,
901 XFS_DIR2_LEAF_OFFSET);
903 * Look for holes in the mapping, and
904 * eliminate them. Count up the valid blocks.
906 for (i = map_valid; i < map_valid + nmap; ) {
907 if (map[i].br_startblock ==
908 HOLESTARTBLOCK) {
909 nmap--;
910 length = map_valid + nmap - i;
911 if (length)
912 memmove(&map[i],
913 &map[i + 1],
914 sizeof(map[i]) *
915 length);
916 } else {
917 map_blocks +=
918 map[i].br_blockcount;
919 i++;
922 map_valid += nmap;
925 * No valid mappings, so no more data blocks.
927 if (!map_valid) {
928 curoff = xfs_dir2_da_to_byte(mp, map_off);
929 break;
932 * Read the directory block starting at the first
933 * mapping.
935 curdb = xfs_dir2_da_to_db(mp, map->br_startoff);
936 error = xfs_da_read_buf(NULL, dp, map->br_startoff,
937 map->br_blockcount >= mp->m_dirblkfsbs ?
938 XFS_FSB_TO_DADDR(mp, map->br_startblock) :
940 &bp, XFS_DATA_FORK);
942 * Should just skip over the data block instead
943 * of giving up.
945 if (error)
946 break; /* XXX */
948 * Adjust the current amount of read-ahead: we just
949 * read a block that was previously ra.
951 if (ra_current)
952 ra_current -= mp->m_dirblkfsbs;
954 * Do we need more readahead?
956 for (ra_index = ra_offset = i = 0;
957 ra_want > ra_current && i < map_blocks;
958 i += mp->m_dirblkfsbs) {
959 ASSERT(ra_index < map_valid);
961 * Read-ahead a contiguous directory block.
963 if (i > ra_current &&
964 map[ra_index].br_blockcount >=
965 mp->m_dirblkfsbs) {
966 xfs_baread(mp->m_ddev_targp,
967 XFS_FSB_TO_DADDR(mp,
968 map[ra_index].br_startblock +
969 ra_offset),
970 (int)BTOBB(mp->m_dirblksize));
971 ra_current = i;
974 * Read-ahead a non-contiguous directory block.
975 * This doesn't use our mapping, but this
976 * is a very rare case.
978 else if (i > ra_current) {
979 (void)xfs_da_reada_buf(NULL, dp,
980 map[ra_index].br_startoff +
981 ra_offset, XFS_DATA_FORK);
982 ra_current = i;
985 * Advance offset through the mapping table.
987 for (j = 0; j < mp->m_dirblkfsbs; j++) {
989 * The rest of this extent but not
990 * more than a dir block.
992 length = MIN(mp->m_dirblkfsbs,
993 (int)(map[ra_index].br_blockcount -
994 ra_offset));
995 j += length;
996 ra_offset += length;
998 * Advance to the next mapping if
999 * this one is used up.
1001 if (ra_offset ==
1002 map[ra_index].br_blockcount) {
1003 ra_offset = 0;
1004 ra_index++;
1009 * Having done a read, we need to set a new offset.
1011 newoff = xfs_dir2_db_off_to_byte(mp, curdb, 0);
1013 * Start of the current block.
1015 if (curoff < newoff)
1016 curoff = newoff;
1018 * Make sure we're in the right block.
1020 else if (curoff > newoff)
1021 ASSERT(xfs_dir2_byte_to_db(mp, curoff) ==
1022 curdb);
1023 data = bp->data;
1024 xfs_dir2_data_check(dp, bp);
1026 * Find our position in the block.
1028 ptr = (char *)&data->u;
1029 byteoff = xfs_dir2_byte_to_off(mp, curoff);
1031 * Skip past the header.
1033 if (byteoff == 0)
1034 curoff += (uint)sizeof(data->hdr);
1036 * Skip past entries until we reach our offset.
1038 else {
1039 while ((char *)ptr - (char *)data < byteoff) {
1040 dup = (xfs_dir2_data_unused_t *)ptr;
1042 if (be16_to_cpu(dup->freetag)
1043 == XFS_DIR2_DATA_FREE_TAG) {
1045 length = be16_to_cpu(dup->length);
1046 ptr += length;
1047 continue;
1049 dep = (xfs_dir2_data_entry_t *)ptr;
1050 length =
1051 xfs_dir2_data_entsize(dep->namelen);
1052 ptr += length;
1055 * Now set our real offset.
1057 curoff =
1058 xfs_dir2_db_off_to_byte(mp,
1059 xfs_dir2_byte_to_db(mp, curoff),
1060 (char *)ptr - (char *)data);
1061 if (ptr >= (char *)data + mp->m_dirblksize) {
1062 continue;
1067 * We have a pointer to an entry.
1068 * Is it a live one?
1070 dup = (xfs_dir2_data_unused_t *)ptr;
1072 * No, it's unused, skip over it.
1074 if (be16_to_cpu(dup->freetag) == XFS_DIR2_DATA_FREE_TAG) {
1075 length = be16_to_cpu(dup->length);
1076 ptr += length;
1077 curoff += length;
1078 continue;
1081 dep = (xfs_dir2_data_entry_t *)ptr;
1082 length = xfs_dir2_data_entsize(dep->namelen);
1084 if (filldir(dirent, dep->name, dep->namelen,
1085 xfs_dir2_byte_to_dataptr(mp, curoff) & 0x7fffffff,
1086 be64_to_cpu(dep->inumber), DT_UNKNOWN))
1087 break;
1090 * Advance to next entry in the block.
1092 ptr += length;
1093 curoff += length;
1094 /* bufsize may have just been a guess; don't go negative */
1095 bufsize = bufsize > length ? bufsize - length : 0;
1099 * All done. Set output offset value to current offset.
1101 if (curoff > xfs_dir2_dataptr_to_byte(mp, XFS_DIR2_MAX_DATAPTR))
1102 *offset = XFS_DIR2_MAX_DATAPTR & 0x7fffffff;
1103 else
1104 *offset = xfs_dir2_byte_to_dataptr(mp, curoff) & 0x7fffffff;
1105 kmem_free(map);
1106 if (bp)
1107 xfs_da_brelse(NULL, bp);
1108 return error;
1112 * Initialize a new leaf block, leaf1 or leafn magic accepted.
1115 xfs_dir2_leaf_init(
1116 xfs_da_args_t *args, /* operation arguments */
1117 xfs_dir2_db_t bno, /* directory block number */
1118 xfs_dabuf_t **bpp, /* out: leaf buffer */
1119 int magic) /* magic number for block */
1121 xfs_dabuf_t *bp; /* leaf buffer */
1122 xfs_inode_t *dp; /* incore directory inode */
1123 int error; /* error return code */
1124 xfs_dir2_leaf_t *leaf; /* leaf structure */
1125 xfs_dir2_leaf_tail_t *ltp; /* leaf tail structure */
1126 xfs_mount_t *mp; /* filesystem mount point */
1127 xfs_trans_t *tp; /* transaction pointer */
1129 dp = args->dp;
1130 ASSERT(dp != NULL);
1131 tp = args->trans;
1132 mp = dp->i_mount;
1133 ASSERT(bno >= XFS_DIR2_LEAF_FIRSTDB(mp) &&
1134 bno < XFS_DIR2_FREE_FIRSTDB(mp));
1136 * Get the buffer for the block.
1138 error = xfs_da_get_buf(tp, dp, xfs_dir2_db_to_da(mp, bno), -1, &bp,
1139 XFS_DATA_FORK);
1140 if (error) {
1141 return error;
1143 ASSERT(bp != NULL);
1144 leaf = bp->data;
1146 * Initialize the header.
1148 leaf->hdr.info.magic = cpu_to_be16(magic);
1149 leaf->hdr.info.forw = 0;
1150 leaf->hdr.info.back = 0;
1151 leaf->hdr.count = 0;
1152 leaf->hdr.stale = 0;
1153 xfs_dir2_leaf_log_header(tp, bp);
1155 * If it's a leaf-format directory initialize the tail.
1156 * In this case our caller has the real bests table to copy into
1157 * the block.
1159 if (magic == XFS_DIR2_LEAF1_MAGIC) {
1160 ltp = xfs_dir2_leaf_tail_p(mp, leaf);
1161 ltp->bestcount = 0;
1162 xfs_dir2_leaf_log_tail(tp, bp);
1164 *bpp = bp;
1165 return 0;
1169 * Log the bests entries indicated from a leaf1 block.
1171 static void
1172 xfs_dir2_leaf_log_bests(
1173 xfs_trans_t *tp, /* transaction pointer */
1174 xfs_dabuf_t *bp, /* leaf buffer */
1175 int first, /* first entry to log */
1176 int last) /* last entry to log */
1178 __be16 *firstb; /* pointer to first entry */
1179 __be16 *lastb; /* pointer to last entry */
1180 xfs_dir2_leaf_t *leaf; /* leaf structure */
1181 xfs_dir2_leaf_tail_t *ltp; /* leaf tail structure */
1183 leaf = bp->data;
1184 ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_DIR2_LEAF1_MAGIC);
1185 ltp = xfs_dir2_leaf_tail_p(tp->t_mountp, leaf);
1186 firstb = xfs_dir2_leaf_bests_p(ltp) + first;
1187 lastb = xfs_dir2_leaf_bests_p(ltp) + last;
1188 xfs_da_log_buf(tp, bp, (uint)((char *)firstb - (char *)leaf),
1189 (uint)((char *)lastb - (char *)leaf + sizeof(*lastb) - 1));
1193 * Log the leaf entries indicated from a leaf1 or leafn block.
1195 void
1196 xfs_dir2_leaf_log_ents(
1197 xfs_trans_t *tp, /* transaction pointer */
1198 xfs_dabuf_t *bp, /* leaf buffer */
1199 int first, /* first entry to log */
1200 int last) /* last entry to log */
1202 xfs_dir2_leaf_entry_t *firstlep; /* pointer to first entry */
1203 xfs_dir2_leaf_entry_t *lastlep; /* pointer to last entry */
1204 xfs_dir2_leaf_t *leaf; /* leaf structure */
1206 leaf = bp->data;
1207 ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_DIR2_LEAF1_MAGIC ||
1208 be16_to_cpu(leaf->hdr.info.magic) == XFS_DIR2_LEAFN_MAGIC);
1209 firstlep = &leaf->ents[first];
1210 lastlep = &leaf->ents[last];
1211 xfs_da_log_buf(tp, bp, (uint)((char *)firstlep - (char *)leaf),
1212 (uint)((char *)lastlep - (char *)leaf + sizeof(*lastlep) - 1));
1216 * Log the header of the leaf1 or leafn block.
1218 void
1219 xfs_dir2_leaf_log_header(
1220 xfs_trans_t *tp, /* transaction pointer */
1221 xfs_dabuf_t *bp) /* leaf buffer */
1223 xfs_dir2_leaf_t *leaf; /* leaf structure */
1225 leaf = bp->data;
1226 ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_DIR2_LEAF1_MAGIC ||
1227 be16_to_cpu(leaf->hdr.info.magic) == XFS_DIR2_LEAFN_MAGIC);
1228 xfs_da_log_buf(tp, bp, (uint)((char *)&leaf->hdr - (char *)leaf),
1229 (uint)(sizeof(leaf->hdr) - 1));
1233 * Log the tail of the leaf1 block.
1235 STATIC void
1236 xfs_dir2_leaf_log_tail(
1237 xfs_trans_t *tp, /* transaction pointer */
1238 xfs_dabuf_t *bp) /* leaf buffer */
1240 xfs_dir2_leaf_t *leaf; /* leaf structure */
1241 xfs_dir2_leaf_tail_t *ltp; /* leaf tail structure */
1242 xfs_mount_t *mp; /* filesystem mount point */
1244 mp = tp->t_mountp;
1245 leaf = bp->data;
1246 ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_DIR2_LEAF1_MAGIC);
1247 ltp = xfs_dir2_leaf_tail_p(mp, leaf);
1248 xfs_da_log_buf(tp, bp, (uint)((char *)ltp - (char *)leaf),
1249 (uint)(mp->m_dirblksize - 1));
1253 * Look up the entry referred to by args in the leaf format directory.
1254 * Most of the work is done by the xfs_dir2_leaf_lookup_int routine which
1255 * is also used by the node-format code.
1258 xfs_dir2_leaf_lookup(
1259 xfs_da_args_t *args) /* operation arguments */
1261 xfs_dabuf_t *dbp; /* data block buffer */
1262 xfs_dir2_data_entry_t *dep; /* data block entry */
1263 xfs_inode_t *dp; /* incore directory inode */
1264 int error; /* error return code */
1265 int index; /* found entry index */
1266 xfs_dabuf_t *lbp; /* leaf buffer */
1267 xfs_dir2_leaf_t *leaf; /* leaf structure */
1268 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
1269 xfs_trans_t *tp; /* transaction pointer */
1271 trace_xfs_dir2_leaf_lookup(args);
1274 * Look up name in the leaf block, returning both buffers and index.
1276 if ((error = xfs_dir2_leaf_lookup_int(args, &lbp, &index, &dbp))) {
1277 return error;
1279 tp = args->trans;
1280 dp = args->dp;
1281 xfs_dir2_leaf_check(dp, lbp);
1282 leaf = lbp->data;
1284 * Get to the leaf entry and contained data entry address.
1286 lep = &leaf->ents[index];
1288 * Point to the data entry.
1290 dep = (xfs_dir2_data_entry_t *)
1291 ((char *)dbp->data +
1292 xfs_dir2_dataptr_to_off(dp->i_mount, be32_to_cpu(lep->address)));
1294 * Return the found inode number & CI name if appropriate
1296 args->inumber = be64_to_cpu(dep->inumber);
1297 error = xfs_dir_cilookup_result(args, dep->name, dep->namelen);
1298 xfs_da_brelse(tp, dbp);
1299 xfs_da_brelse(tp, lbp);
1300 return XFS_ERROR(error);
1304 * Look up name/hash in the leaf block.
1305 * Fill in indexp with the found index, and dbpp with the data buffer.
1306 * If not found dbpp will be NULL, and ENOENT comes back.
1307 * lbpp will always be filled in with the leaf buffer unless there's an error.
1309 static int /* error */
1310 xfs_dir2_leaf_lookup_int(
1311 xfs_da_args_t *args, /* operation arguments */
1312 xfs_dabuf_t **lbpp, /* out: leaf buffer */
1313 int *indexp, /* out: index in leaf block */
1314 xfs_dabuf_t **dbpp) /* out: data buffer */
1316 xfs_dir2_db_t curdb = -1; /* current data block number */
1317 xfs_dabuf_t *dbp = NULL; /* data buffer */
1318 xfs_dir2_data_entry_t *dep; /* data entry */
1319 xfs_inode_t *dp; /* incore directory inode */
1320 int error; /* error return code */
1321 int index; /* index in leaf block */
1322 xfs_dabuf_t *lbp; /* leaf buffer */
1323 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
1324 xfs_dir2_leaf_t *leaf; /* leaf structure */
1325 xfs_mount_t *mp; /* filesystem mount point */
1326 xfs_dir2_db_t newdb; /* new data block number */
1327 xfs_trans_t *tp; /* transaction pointer */
1328 xfs_dir2_db_t cidb = -1; /* case match data block no. */
1329 enum xfs_dacmp cmp; /* name compare result */
1331 dp = args->dp;
1332 tp = args->trans;
1333 mp = dp->i_mount;
1335 * Read the leaf block into the buffer.
1337 error = xfs_da_read_buf(tp, dp, mp->m_dirleafblk, -1, &lbp,
1338 XFS_DATA_FORK);
1339 if (error)
1340 return error;
1341 *lbpp = lbp;
1342 leaf = lbp->data;
1343 xfs_dir2_leaf_check(dp, lbp);
1345 * Look for the first leaf entry with our hash value.
1347 index = xfs_dir2_leaf_search_hash(args, lbp);
1349 * Loop over all the entries with the right hash value
1350 * looking to match the name.
1352 for (lep = &leaf->ents[index]; index < be16_to_cpu(leaf->hdr.count) &&
1353 be32_to_cpu(lep->hashval) == args->hashval;
1354 lep++, index++) {
1356 * Skip over stale leaf entries.
1358 if (be32_to_cpu(lep->address) == XFS_DIR2_NULL_DATAPTR)
1359 continue;
1361 * Get the new data block number.
1363 newdb = xfs_dir2_dataptr_to_db(mp, be32_to_cpu(lep->address));
1365 * If it's not the same as the old data block number,
1366 * need to pitch the old one and read the new one.
1368 if (newdb != curdb) {
1369 if (dbp)
1370 xfs_da_brelse(tp, dbp);
1371 error = xfs_da_read_buf(tp, dp,
1372 xfs_dir2_db_to_da(mp, newdb),
1373 -1, &dbp, XFS_DATA_FORK);
1374 if (error) {
1375 xfs_da_brelse(tp, lbp);
1376 return error;
1378 xfs_dir2_data_check(dp, dbp);
1379 curdb = newdb;
1382 * Point to the data entry.
1384 dep = (xfs_dir2_data_entry_t *)((char *)dbp->data +
1385 xfs_dir2_dataptr_to_off(mp, be32_to_cpu(lep->address)));
1387 * Compare name and if it's an exact match, return the index
1388 * and buffer. If it's the first case-insensitive match, store
1389 * the index and buffer and continue looking for an exact match.
1391 cmp = mp->m_dirnameops->compname(args, dep->name, dep->namelen);
1392 if (cmp != XFS_CMP_DIFFERENT && cmp != args->cmpresult) {
1393 args->cmpresult = cmp;
1394 *indexp = index;
1395 /* case exact match: return the current buffer. */
1396 if (cmp == XFS_CMP_EXACT) {
1397 *dbpp = dbp;
1398 return 0;
1400 cidb = curdb;
1403 ASSERT(args->op_flags & XFS_DA_OP_OKNOENT);
1405 * Here, we can only be doing a lookup (not a rename or remove).
1406 * If a case-insensitive match was found earlier, re-read the
1407 * appropriate data block if required and return it.
1409 if (args->cmpresult == XFS_CMP_CASE) {
1410 ASSERT(cidb != -1);
1411 if (cidb != curdb) {
1412 xfs_da_brelse(tp, dbp);
1413 error = xfs_da_read_buf(tp, dp,
1414 xfs_dir2_db_to_da(mp, cidb),
1415 -1, &dbp, XFS_DATA_FORK);
1416 if (error) {
1417 xfs_da_brelse(tp, lbp);
1418 return error;
1421 *dbpp = dbp;
1422 return 0;
1425 * No match found, return ENOENT.
1427 ASSERT(cidb == -1);
1428 if (dbp)
1429 xfs_da_brelse(tp, dbp);
1430 xfs_da_brelse(tp, lbp);
1431 return XFS_ERROR(ENOENT);
1435 * Remove an entry from a leaf format directory.
1437 int /* error */
1438 xfs_dir2_leaf_removename(
1439 xfs_da_args_t *args) /* operation arguments */
1441 __be16 *bestsp; /* leaf block best freespace */
1442 xfs_dir2_data_t *data; /* data block structure */
1443 xfs_dir2_db_t db; /* data block number */
1444 xfs_dabuf_t *dbp; /* data block buffer */
1445 xfs_dir2_data_entry_t *dep; /* data entry structure */
1446 xfs_inode_t *dp; /* incore directory inode */
1447 int error; /* error return code */
1448 xfs_dir2_db_t i; /* temporary data block # */
1449 int index; /* index into leaf entries */
1450 xfs_dabuf_t *lbp; /* leaf buffer */
1451 xfs_dir2_leaf_t *leaf; /* leaf structure */
1452 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
1453 xfs_dir2_leaf_tail_t *ltp; /* leaf tail structure */
1454 xfs_mount_t *mp; /* filesystem mount point */
1455 int needlog; /* need to log data header */
1456 int needscan; /* need to rescan data frees */
1457 xfs_dir2_data_off_t oldbest; /* old value of best free */
1458 xfs_trans_t *tp; /* transaction pointer */
1460 trace_xfs_dir2_leaf_removename(args);
1463 * Lookup the leaf entry, get the leaf and data blocks read in.
1465 if ((error = xfs_dir2_leaf_lookup_int(args, &lbp, &index, &dbp))) {
1466 return error;
1468 dp = args->dp;
1469 tp = args->trans;
1470 mp = dp->i_mount;
1471 leaf = lbp->data;
1472 data = dbp->data;
1473 xfs_dir2_data_check(dp, dbp);
1475 * Point to the leaf entry, use that to point to the data entry.
1477 lep = &leaf->ents[index];
1478 db = xfs_dir2_dataptr_to_db(mp, be32_to_cpu(lep->address));
1479 dep = (xfs_dir2_data_entry_t *)
1480 ((char *)data + xfs_dir2_dataptr_to_off(mp, be32_to_cpu(lep->address)));
1481 needscan = needlog = 0;
1482 oldbest = be16_to_cpu(data->hdr.bestfree[0].length);
1483 ltp = xfs_dir2_leaf_tail_p(mp, leaf);
1484 bestsp = xfs_dir2_leaf_bests_p(ltp);
1485 ASSERT(be16_to_cpu(bestsp[db]) == oldbest);
1487 * Mark the former data entry unused.
1489 xfs_dir2_data_make_free(tp, dbp,
1490 (xfs_dir2_data_aoff_t)((char *)dep - (char *)data),
1491 xfs_dir2_data_entsize(dep->namelen), &needlog, &needscan);
1493 * We just mark the leaf entry stale by putting a null in it.
1495 be16_add_cpu(&leaf->hdr.stale, 1);
1496 xfs_dir2_leaf_log_header(tp, lbp);
1497 lep->address = cpu_to_be32(XFS_DIR2_NULL_DATAPTR);
1498 xfs_dir2_leaf_log_ents(tp, lbp, index, index);
1500 * Scan the freespace in the data block again if necessary,
1501 * log the data block header if necessary.
1503 if (needscan)
1504 xfs_dir2_data_freescan(mp, data, &needlog);
1505 if (needlog)
1506 xfs_dir2_data_log_header(tp, dbp);
1508 * If the longest freespace in the data block has changed,
1509 * put the new value in the bests table and log that.
1511 if (be16_to_cpu(data->hdr.bestfree[0].length) != oldbest) {
1512 bestsp[db] = data->hdr.bestfree[0].length;
1513 xfs_dir2_leaf_log_bests(tp, lbp, db, db);
1515 xfs_dir2_data_check(dp, dbp);
1517 * If the data block is now empty then get rid of the data block.
1519 if (be16_to_cpu(data->hdr.bestfree[0].length) ==
1520 mp->m_dirblksize - (uint)sizeof(data->hdr)) {
1521 ASSERT(db != mp->m_dirdatablk);
1522 if ((error = xfs_dir2_shrink_inode(args, db, dbp))) {
1524 * Nope, can't get rid of it because it caused
1525 * allocation of a bmap btree block to do so.
1526 * Just go on, returning success, leaving the
1527 * empty block in place.
1529 if (error == ENOSPC && args->total == 0) {
1530 xfs_da_buf_done(dbp);
1531 error = 0;
1533 xfs_dir2_leaf_check(dp, lbp);
1534 xfs_da_buf_done(lbp);
1535 return error;
1537 dbp = NULL;
1539 * If this is the last data block then compact the
1540 * bests table by getting rid of entries.
1542 if (db == be32_to_cpu(ltp->bestcount) - 1) {
1544 * Look for the last active entry (i).
1546 for (i = db - 1; i > 0; i--) {
1547 if (be16_to_cpu(bestsp[i]) != NULLDATAOFF)
1548 break;
1551 * Copy the table down so inactive entries at the
1552 * end are removed.
1554 memmove(&bestsp[db - i], bestsp,
1555 (be32_to_cpu(ltp->bestcount) - (db - i)) * sizeof(*bestsp));
1556 be32_add_cpu(&ltp->bestcount, -(db - i));
1557 xfs_dir2_leaf_log_tail(tp, lbp);
1558 xfs_dir2_leaf_log_bests(tp, lbp, 0, be32_to_cpu(ltp->bestcount) - 1);
1559 } else
1560 bestsp[db] = cpu_to_be16(NULLDATAOFF);
1563 * If the data block was not the first one, drop it.
1565 else if (db != mp->m_dirdatablk && dbp != NULL) {
1566 xfs_da_buf_done(dbp);
1567 dbp = NULL;
1569 xfs_dir2_leaf_check(dp, lbp);
1571 * See if we can convert to block form.
1573 return xfs_dir2_leaf_to_block(args, lbp, dbp);
1577 * Replace the inode number in a leaf format directory entry.
1579 int /* error */
1580 xfs_dir2_leaf_replace(
1581 xfs_da_args_t *args) /* operation arguments */
1583 xfs_dabuf_t *dbp; /* data block buffer */
1584 xfs_dir2_data_entry_t *dep; /* data block entry */
1585 xfs_inode_t *dp; /* incore directory inode */
1586 int error; /* error return code */
1587 int index; /* index of leaf entry */
1588 xfs_dabuf_t *lbp; /* leaf buffer */
1589 xfs_dir2_leaf_t *leaf; /* leaf structure */
1590 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
1591 xfs_trans_t *tp; /* transaction pointer */
1593 trace_xfs_dir2_leaf_replace(args);
1596 * Look up the entry.
1598 if ((error = xfs_dir2_leaf_lookup_int(args, &lbp, &index, &dbp))) {
1599 return error;
1601 dp = args->dp;
1602 leaf = lbp->data;
1604 * Point to the leaf entry, get data address from it.
1606 lep = &leaf->ents[index];
1608 * Point to the data entry.
1610 dep = (xfs_dir2_data_entry_t *)
1611 ((char *)dbp->data +
1612 xfs_dir2_dataptr_to_off(dp->i_mount, be32_to_cpu(lep->address)));
1613 ASSERT(args->inumber != be64_to_cpu(dep->inumber));
1615 * Put the new inode number in, log it.
1617 dep->inumber = cpu_to_be64(args->inumber);
1618 tp = args->trans;
1619 xfs_dir2_data_log_entry(tp, dbp, dep);
1620 xfs_da_buf_done(dbp);
1621 xfs_dir2_leaf_check(dp, lbp);
1622 xfs_da_brelse(tp, lbp);
1623 return 0;
1627 * Return index in the leaf block (lbp) which is either the first
1628 * one with this hash value, or if there are none, the insert point
1629 * for that hash value.
1631 int /* index value */
1632 xfs_dir2_leaf_search_hash(
1633 xfs_da_args_t *args, /* operation arguments */
1634 xfs_dabuf_t *lbp) /* leaf buffer */
1636 xfs_dahash_t hash=0; /* hash from this entry */
1637 xfs_dahash_t hashwant; /* hash value looking for */
1638 int high; /* high leaf index */
1639 int low; /* low leaf index */
1640 xfs_dir2_leaf_t *leaf; /* leaf structure */
1641 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
1642 int mid=0; /* current leaf index */
1644 leaf = lbp->data;
1645 #ifndef __KERNEL__
1646 if (!leaf->hdr.count)
1647 return 0;
1648 #endif
1650 * Note, the table cannot be empty, so we have to go through the loop.
1651 * Binary search the leaf entries looking for our hash value.
1653 for (lep = leaf->ents, low = 0, high = be16_to_cpu(leaf->hdr.count) - 1,
1654 hashwant = args->hashval;
1655 low <= high; ) {
1656 mid = (low + high) >> 1;
1657 if ((hash = be32_to_cpu(lep[mid].hashval)) == hashwant)
1658 break;
1659 if (hash < hashwant)
1660 low = mid + 1;
1661 else
1662 high = mid - 1;
1665 * Found one, back up through all the equal hash values.
1667 if (hash == hashwant) {
1668 while (mid > 0 && be32_to_cpu(lep[mid - 1].hashval) == hashwant) {
1669 mid--;
1673 * Need to point to an entry higher than ours.
1675 else if (hash < hashwant)
1676 mid++;
1677 return mid;
1681 * Trim off a trailing data block. We know it's empty since the leaf
1682 * freespace table says so.
1684 int /* error */
1685 xfs_dir2_leaf_trim_data(
1686 xfs_da_args_t *args, /* operation arguments */
1687 xfs_dabuf_t *lbp, /* leaf buffer */
1688 xfs_dir2_db_t db) /* data block number */
1690 __be16 *bestsp; /* leaf bests table */
1691 #ifdef DEBUG
1692 xfs_dir2_data_t *data; /* data block structure */
1693 #endif
1694 xfs_dabuf_t *dbp; /* data block buffer */
1695 xfs_inode_t *dp; /* incore directory inode */
1696 int error; /* error return value */
1697 xfs_dir2_leaf_t *leaf; /* leaf structure */
1698 xfs_dir2_leaf_tail_t *ltp; /* leaf tail structure */
1699 xfs_mount_t *mp; /* filesystem mount point */
1700 xfs_trans_t *tp; /* transaction pointer */
1702 dp = args->dp;
1703 mp = dp->i_mount;
1704 tp = args->trans;
1706 * Read the offending data block. We need its buffer.
1708 if ((error = xfs_da_read_buf(tp, dp, xfs_dir2_db_to_da(mp, db), -1, &dbp,
1709 XFS_DATA_FORK))) {
1710 return error;
1712 #ifdef DEBUG
1713 data = dbp->data;
1714 ASSERT(be32_to_cpu(data->hdr.magic) == XFS_DIR2_DATA_MAGIC);
1715 #endif
1716 /* this seems to be an error
1717 * data is only valid if DEBUG is defined?
1718 * RMC 09/08/1999
1721 leaf = lbp->data;
1722 ltp = xfs_dir2_leaf_tail_p(mp, leaf);
1723 ASSERT(be16_to_cpu(data->hdr.bestfree[0].length) ==
1724 mp->m_dirblksize - (uint)sizeof(data->hdr));
1725 ASSERT(db == be32_to_cpu(ltp->bestcount) - 1);
1727 * Get rid of the data block.
1729 if ((error = xfs_dir2_shrink_inode(args, db, dbp))) {
1730 ASSERT(error != ENOSPC);
1731 xfs_da_brelse(tp, dbp);
1732 return error;
1735 * Eliminate the last bests entry from the table.
1737 bestsp = xfs_dir2_leaf_bests_p(ltp);
1738 be32_add_cpu(&ltp->bestcount, -1);
1739 memmove(&bestsp[1], &bestsp[0], be32_to_cpu(ltp->bestcount) * sizeof(*bestsp));
1740 xfs_dir2_leaf_log_tail(tp, lbp);
1741 xfs_dir2_leaf_log_bests(tp, lbp, 0, be32_to_cpu(ltp->bestcount) - 1);
1742 return 0;
1746 * Convert node form directory to leaf form directory.
1747 * The root of the node form dir needs to already be a LEAFN block.
1748 * Just return if we can't do anything.
1750 int /* error */
1751 xfs_dir2_node_to_leaf(
1752 xfs_da_state_t *state) /* directory operation state */
1754 xfs_da_args_t *args; /* operation arguments */
1755 xfs_inode_t *dp; /* incore directory inode */
1756 int error; /* error return code */
1757 xfs_dabuf_t *fbp; /* buffer for freespace block */
1758 xfs_fileoff_t fo; /* freespace file offset */
1759 xfs_dir2_free_t *free; /* freespace structure */
1760 xfs_dabuf_t *lbp; /* buffer for leaf block */
1761 xfs_dir2_leaf_tail_t *ltp; /* tail of leaf structure */
1762 xfs_dir2_leaf_t *leaf; /* leaf structure */
1763 xfs_mount_t *mp; /* filesystem mount point */
1764 int rval; /* successful free trim? */
1765 xfs_trans_t *tp; /* transaction pointer */
1768 * There's more than a leaf level in the btree, so there must
1769 * be multiple leafn blocks. Give up.
1771 if (state->path.active > 1)
1772 return 0;
1773 args = state->args;
1775 trace_xfs_dir2_node_to_leaf(args);
1777 mp = state->mp;
1778 dp = args->dp;
1779 tp = args->trans;
1781 * Get the last offset in the file.
1783 if ((error = xfs_bmap_last_offset(tp, dp, &fo, XFS_DATA_FORK))) {
1784 return error;
1786 fo -= mp->m_dirblkfsbs;
1788 * If there are freespace blocks other than the first one,
1789 * take this opportunity to remove trailing empty freespace blocks
1790 * that may have been left behind during no-space-reservation
1791 * operations.
1793 while (fo > mp->m_dirfreeblk) {
1794 if ((error = xfs_dir2_node_trim_free(args, fo, &rval))) {
1795 return error;
1797 if (rval)
1798 fo -= mp->m_dirblkfsbs;
1799 else
1800 return 0;
1803 * Now find the block just before the freespace block.
1805 if ((error = xfs_bmap_last_before(tp, dp, &fo, XFS_DATA_FORK))) {
1806 return error;
1809 * If it's not the single leaf block, give up.
1811 if (XFS_FSB_TO_B(mp, fo) > XFS_DIR2_LEAF_OFFSET + mp->m_dirblksize)
1812 return 0;
1813 lbp = state->path.blk[0].bp;
1814 leaf = lbp->data;
1815 ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_DIR2_LEAFN_MAGIC);
1817 * Read the freespace block.
1819 if ((error = xfs_da_read_buf(tp, dp, mp->m_dirfreeblk, -1, &fbp,
1820 XFS_DATA_FORK))) {
1821 return error;
1823 free = fbp->data;
1824 ASSERT(be32_to_cpu(free->hdr.magic) == XFS_DIR2_FREE_MAGIC);
1825 ASSERT(!free->hdr.firstdb);
1827 * Now see if the leafn and free data will fit in a leaf1.
1828 * If not, release the buffer and give up.
1830 if ((uint)sizeof(leaf->hdr) +
1831 (be16_to_cpu(leaf->hdr.count) - be16_to_cpu(leaf->hdr.stale)) * (uint)sizeof(leaf->ents[0]) +
1832 be32_to_cpu(free->hdr.nvalid) * (uint)sizeof(leaf->bests[0]) +
1833 (uint)sizeof(leaf->tail) >
1834 mp->m_dirblksize) {
1835 xfs_da_brelse(tp, fbp);
1836 return 0;
1839 * If the leaf has any stale entries in it, compress them out.
1840 * The compact routine will log the header.
1842 if (be16_to_cpu(leaf->hdr.stale))
1843 xfs_dir2_leaf_compact(args, lbp);
1844 else
1845 xfs_dir2_leaf_log_header(tp, lbp);
1846 leaf->hdr.info.magic = cpu_to_be16(XFS_DIR2_LEAF1_MAGIC);
1848 * Set up the leaf tail from the freespace block.
1850 ltp = xfs_dir2_leaf_tail_p(mp, leaf);
1851 ltp->bestcount = free->hdr.nvalid;
1853 * Set up the leaf bests table.
1855 memcpy(xfs_dir2_leaf_bests_p(ltp), free->bests,
1856 be32_to_cpu(ltp->bestcount) * sizeof(leaf->bests[0]));
1857 xfs_dir2_leaf_log_bests(tp, lbp, 0, be32_to_cpu(ltp->bestcount) - 1);
1858 xfs_dir2_leaf_log_tail(tp, lbp);
1859 xfs_dir2_leaf_check(dp, lbp);
1861 * Get rid of the freespace block.
1863 error = xfs_dir2_shrink_inode(args, XFS_DIR2_FREE_FIRSTDB(mp), fbp);
1864 if (error) {
1866 * This can't fail here because it can only happen when
1867 * punching out the middle of an extent, and this is an
1868 * isolated block.
1870 ASSERT(error != ENOSPC);
1871 return error;
1873 fbp = NULL;
1875 * Now see if we can convert the single-leaf directory
1876 * down to a block form directory.
1877 * This routine always kills the dabuf for the leaf, so
1878 * eliminate it from the path.
1880 error = xfs_dir2_leaf_to_block(args, lbp, NULL);
1881 state->path.blk[0].bp = NULL;
1882 return error;