iwlwifi: use iwl_poll_direct_bit in EEPROM reading
[linux-2.6/verdex.git] / fs / xfs / xfs_dir2_leaf.c
blob93535992cb60e79491b490f07e85a9338dfc0faf
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_dir2_trace.h"
42 #include "xfs_error.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 xfs_dir2_trace_args_b("block_to_leaf", args, dbp);
84 dp = args->dp;
85 mp = dp->i_mount;
86 tp = args->trans;
88 * Add the leaf block to the inode.
89 * This interface will only put blocks in the leaf/node range.
90 * Since that's empty now, we'll get the root (block 0 in range).
92 if ((error = xfs_da_grow_inode(args, &blkno))) {
93 return error;
95 ldb = xfs_dir2_da_to_db(mp, blkno);
96 ASSERT(ldb == XFS_DIR2_LEAF_FIRSTDB(mp));
98 * Initialize the leaf block, get a buffer for it.
100 if ((error = xfs_dir2_leaf_init(args, ldb, &lbp, XFS_DIR2_LEAF1_MAGIC))) {
101 return error;
103 ASSERT(lbp != NULL);
104 leaf = lbp->data;
105 block = dbp->data;
106 xfs_dir2_data_check(dp, dbp);
107 btp = xfs_dir2_block_tail_p(mp, block);
108 blp = xfs_dir2_block_leaf_p(btp);
110 * Set the counts in the leaf header.
112 leaf->hdr.count = cpu_to_be16(be32_to_cpu(btp->count));
113 leaf->hdr.stale = cpu_to_be16(be32_to_cpu(btp->stale));
115 * Could compact these but I think we always do the conversion
116 * after squeezing out stale entries.
118 memcpy(leaf->ents, blp, be32_to_cpu(btp->count) * sizeof(xfs_dir2_leaf_entry_t));
119 xfs_dir2_leaf_log_ents(tp, lbp, 0, be16_to_cpu(leaf->hdr.count) - 1);
120 needscan = 0;
121 needlog = 1;
123 * Make the space formerly occupied by the leaf entries and block
124 * tail be free.
126 xfs_dir2_data_make_free(tp, dbp,
127 (xfs_dir2_data_aoff_t)((char *)blp - (char *)block),
128 (xfs_dir2_data_aoff_t)((char *)block + mp->m_dirblksize -
129 (char *)blp),
130 &needlog, &needscan);
132 * Fix up the block header, make it a data block.
134 block->hdr.magic = cpu_to_be32(XFS_DIR2_DATA_MAGIC);
135 if (needscan)
136 xfs_dir2_data_freescan(mp, (xfs_dir2_data_t *)block, &needlog);
138 * Set up leaf tail and bests table.
140 ltp = xfs_dir2_leaf_tail_p(mp, leaf);
141 ltp->bestcount = cpu_to_be32(1);
142 bestsp = xfs_dir2_leaf_bests_p(ltp);
143 bestsp[0] = block->hdr.bestfree[0].length;
145 * Log the data header and leaf bests table.
147 if (needlog)
148 xfs_dir2_data_log_header(tp, dbp);
149 xfs_dir2_leaf_check(dp, lbp);
150 xfs_dir2_data_check(dp, dbp);
151 xfs_dir2_leaf_log_bests(tp, lbp, 0, 0);
152 xfs_da_buf_done(lbp);
153 return 0;
157 * Add an entry to a leaf form directory.
159 int /* error */
160 xfs_dir2_leaf_addname(
161 xfs_da_args_t *args) /* operation arguments */
163 __be16 *bestsp; /* freespace table in leaf */
164 int compact; /* need to compact leaves */
165 xfs_dir2_data_t *data; /* data block structure */
166 xfs_dabuf_t *dbp; /* data block buffer */
167 xfs_dir2_data_entry_t *dep; /* data block entry */
168 xfs_inode_t *dp; /* incore directory inode */
169 xfs_dir2_data_unused_t *dup; /* data unused entry */
170 int error; /* error return value */
171 int grown; /* allocated new data block */
172 int highstale; /* index of next stale leaf */
173 int i; /* temporary, index */
174 int index; /* leaf table position */
175 xfs_dabuf_t *lbp; /* leaf's buffer */
176 xfs_dir2_leaf_t *leaf; /* leaf structure */
177 int length; /* length of new entry */
178 xfs_dir2_leaf_entry_t *lep; /* leaf entry table pointer */
179 int lfloglow; /* low leaf logging index */
180 int lfloghigh; /* high leaf logging index */
181 int lowstale; /* index of prev stale leaf */
182 xfs_dir2_leaf_tail_t *ltp; /* leaf tail pointer */
183 xfs_mount_t *mp; /* filesystem mount point */
184 int needbytes; /* leaf block bytes needed */
185 int needlog; /* need to log data header */
186 int needscan; /* need to rescan data free */
187 __be16 *tagp; /* end of data entry */
188 xfs_trans_t *tp; /* transaction pointer */
189 xfs_dir2_db_t use_block; /* data block number */
191 xfs_dir2_trace_args("leaf_addname", args);
192 dp = args->dp;
193 tp = args->trans;
194 mp = dp->i_mount;
196 * Read the leaf block.
198 error = xfs_da_read_buf(tp, dp, mp->m_dirleafblk, -1, &lbp,
199 XFS_DATA_FORK);
200 if (error) {
201 return error;
203 ASSERT(lbp != NULL);
205 * Look up the entry by hash value and name.
206 * We know it's not there, our caller has already done a lookup.
207 * So the index is of the entry to insert in front of.
208 * But if there are dup hash values the index is of the first of those.
210 index = xfs_dir2_leaf_search_hash(args, lbp);
211 leaf = lbp->data;
212 ltp = xfs_dir2_leaf_tail_p(mp, leaf);
213 bestsp = xfs_dir2_leaf_bests_p(ltp);
214 length = xfs_dir2_data_entsize(args->namelen);
216 * See if there are any entries with the same hash value
217 * and space in their block for the new entry.
218 * This is good because it puts multiple same-hash value entries
219 * in a data block, improving the lookup of those entries.
221 for (use_block = -1, lep = &leaf->ents[index];
222 index < be16_to_cpu(leaf->hdr.count) && be32_to_cpu(lep->hashval) == args->hashval;
223 index++, lep++) {
224 if (be32_to_cpu(lep->address) == XFS_DIR2_NULL_DATAPTR)
225 continue;
226 i = xfs_dir2_dataptr_to_db(mp, be32_to_cpu(lep->address));
227 ASSERT(i < be32_to_cpu(ltp->bestcount));
228 ASSERT(be16_to_cpu(bestsp[i]) != NULLDATAOFF);
229 if (be16_to_cpu(bestsp[i]) >= length) {
230 use_block = i;
231 break;
235 * Didn't find a block yet, linear search all the data blocks.
237 if (use_block == -1) {
238 for (i = 0; i < be32_to_cpu(ltp->bestcount); i++) {
240 * Remember a block we see that's missing.
242 if (be16_to_cpu(bestsp[i]) == NULLDATAOFF && use_block == -1)
243 use_block = i;
244 else if (be16_to_cpu(bestsp[i]) >= length) {
245 use_block = i;
246 break;
251 * How many bytes do we need in the leaf block?
253 needbytes =
254 (leaf->hdr.stale ? 0 : (uint)sizeof(leaf->ents[0])) +
255 (use_block != -1 ? 0 : (uint)sizeof(leaf->bests[0]));
257 * Now kill use_block if it refers to a missing block, so we
258 * can use it as an indication of allocation needed.
260 if (use_block != -1 && be16_to_cpu(bestsp[use_block]) == NULLDATAOFF)
261 use_block = -1;
263 * If we don't have enough free bytes but we can make enough
264 * by compacting out stale entries, we'll do that.
266 if ((char *)bestsp - (char *)&leaf->ents[be16_to_cpu(leaf->hdr.count)] <
267 needbytes && be16_to_cpu(leaf->hdr.stale) > 1) {
268 compact = 1;
271 * Otherwise if we don't have enough free bytes we need to
272 * convert to node form.
274 else if ((char *)bestsp - (char *)&leaf->ents[be16_to_cpu(
275 leaf->hdr.count)] < needbytes) {
277 * Just checking or no space reservation, give up.
279 if ((args->op_flags & XFS_DA_OP_JUSTCHECK) ||
280 args->total == 0) {
281 xfs_da_brelse(tp, lbp);
282 return XFS_ERROR(ENOSPC);
285 * Convert to node form.
287 error = xfs_dir2_leaf_to_node(args, lbp);
288 xfs_da_buf_done(lbp);
289 if (error)
290 return error;
292 * Then add the new entry.
294 return xfs_dir2_node_addname(args);
297 * Otherwise it will fit without compaction.
299 else
300 compact = 0;
302 * If just checking, then it will fit unless we needed to allocate
303 * a new data block.
305 if (args->op_flags & XFS_DA_OP_JUSTCHECK) {
306 xfs_da_brelse(tp, lbp);
307 return use_block == -1 ? XFS_ERROR(ENOSPC) : 0;
310 * If no allocations are allowed, return now before we've
311 * changed anything.
313 if (args->total == 0 && use_block == -1) {
314 xfs_da_brelse(tp, lbp);
315 return XFS_ERROR(ENOSPC);
318 * Need to compact the leaf entries, removing stale ones.
319 * Leave one stale entry behind - the one closest to our
320 * insertion index - and we'll shift that one to our insertion
321 * point later.
323 if (compact) {
324 xfs_dir2_leaf_compact_x1(lbp, &index, &lowstale, &highstale,
325 &lfloglow, &lfloghigh);
328 * There are stale entries, so we'll need log-low and log-high
329 * impossibly bad values later.
331 else if (be16_to_cpu(leaf->hdr.stale)) {
332 lfloglow = be16_to_cpu(leaf->hdr.count);
333 lfloghigh = -1;
336 * If there was no data block space found, we need to allocate
337 * a new one.
339 if (use_block == -1) {
341 * Add the new data block.
343 if ((error = xfs_dir2_grow_inode(args, XFS_DIR2_DATA_SPACE,
344 &use_block))) {
345 xfs_da_brelse(tp, lbp);
346 return error;
349 * Initialize the block.
351 if ((error = xfs_dir2_data_init(args, use_block, &dbp))) {
352 xfs_da_brelse(tp, lbp);
353 return error;
356 * If we're adding a new data block on the end we need to
357 * extend the bests table. Copy it up one entry.
359 if (use_block >= be32_to_cpu(ltp->bestcount)) {
360 bestsp--;
361 memmove(&bestsp[0], &bestsp[1],
362 be32_to_cpu(ltp->bestcount) * sizeof(bestsp[0]));
363 be32_add_cpu(&ltp->bestcount, 1);
364 xfs_dir2_leaf_log_tail(tp, lbp);
365 xfs_dir2_leaf_log_bests(tp, lbp, 0, be32_to_cpu(ltp->bestcount) - 1);
368 * If we're filling in a previously empty block just log it.
370 else
371 xfs_dir2_leaf_log_bests(tp, lbp, use_block, use_block);
372 data = dbp->data;
373 bestsp[use_block] = data->hdr.bestfree[0].length;
374 grown = 1;
377 * Already had space in some data block.
378 * Just read that one in.
380 else {
381 if ((error =
382 xfs_da_read_buf(tp, dp, xfs_dir2_db_to_da(mp, use_block),
383 -1, &dbp, XFS_DATA_FORK))) {
384 xfs_da_brelse(tp, lbp);
385 return error;
387 data = dbp->data;
388 grown = 0;
390 xfs_dir2_data_check(dp, dbp);
392 * Point to the biggest freespace in our data block.
394 dup = (xfs_dir2_data_unused_t *)
395 ((char *)data + be16_to_cpu(data->hdr.bestfree[0].offset));
396 ASSERT(be16_to_cpu(dup->length) >= length);
397 needscan = needlog = 0;
399 * Mark the initial part of our freespace in use for the new entry.
401 xfs_dir2_data_use_free(tp, dbp, dup,
402 (xfs_dir2_data_aoff_t)((char *)dup - (char *)data), length,
403 &needlog, &needscan);
405 * Initialize our new entry (at last).
407 dep = (xfs_dir2_data_entry_t *)dup;
408 dep->inumber = cpu_to_be64(args->inumber);
409 dep->namelen = args->namelen;
410 memcpy(dep->name, args->name, dep->namelen);
411 tagp = xfs_dir2_data_entry_tag_p(dep);
412 *tagp = cpu_to_be16((char *)dep - (char *)data);
414 * Need to scan fix up the bestfree table.
416 if (needscan)
417 xfs_dir2_data_freescan(mp, data, &needlog);
419 * Need to log the data block's header.
421 if (needlog)
422 xfs_dir2_data_log_header(tp, dbp);
423 xfs_dir2_data_log_entry(tp, dbp, dep);
425 * If the bests table needs to be changed, do it.
426 * Log the change unless we've already done that.
428 if (be16_to_cpu(bestsp[use_block]) != be16_to_cpu(data->hdr.bestfree[0].length)) {
429 bestsp[use_block] = data->hdr.bestfree[0].length;
430 if (!grown)
431 xfs_dir2_leaf_log_bests(tp, lbp, use_block, use_block);
434 * Now we need to make room to insert the leaf entry.
435 * If there are no stale entries, we just insert a hole at index.
437 if (!leaf->hdr.stale) {
439 * lep is still good as the index leaf entry.
441 if (index < be16_to_cpu(leaf->hdr.count))
442 memmove(lep + 1, lep,
443 (be16_to_cpu(leaf->hdr.count) - index) * sizeof(*lep));
445 * Record low and high logging indices for the leaf.
447 lfloglow = index;
448 lfloghigh = be16_to_cpu(leaf->hdr.count);
449 be16_add_cpu(&leaf->hdr.count, 1);
452 * There are stale entries.
453 * We will use one of them for the new entry.
454 * It's probably not at the right location, so we'll have to
455 * shift some up or down first.
457 else {
459 * If we didn't compact before, we need to find the nearest
460 * stale entries before and after our insertion point.
462 if (compact == 0) {
464 * Find the first stale entry before the insertion
465 * point, if any.
467 for (lowstale = index - 1;
468 lowstale >= 0 &&
469 be32_to_cpu(leaf->ents[lowstale].address) !=
470 XFS_DIR2_NULL_DATAPTR;
471 lowstale--)
472 continue;
474 * Find the next stale entry at or after the insertion
475 * point, if any. Stop if we go so far that the
476 * lowstale entry would be better.
478 for (highstale = index;
479 highstale < be16_to_cpu(leaf->hdr.count) &&
480 be32_to_cpu(leaf->ents[highstale].address) !=
481 XFS_DIR2_NULL_DATAPTR &&
482 (lowstale < 0 ||
483 index - lowstale - 1 >= highstale - index);
484 highstale++)
485 continue;
488 * If the low one is better, use it.
490 if (lowstale >= 0 &&
491 (highstale == be16_to_cpu(leaf->hdr.count) ||
492 index - lowstale - 1 < highstale - index)) {
493 ASSERT(index - lowstale - 1 >= 0);
494 ASSERT(be32_to_cpu(leaf->ents[lowstale].address) ==
495 XFS_DIR2_NULL_DATAPTR);
497 * Copy entries up to cover the stale entry
498 * and make room for the new entry.
500 if (index - lowstale - 1 > 0)
501 memmove(&leaf->ents[lowstale],
502 &leaf->ents[lowstale + 1],
503 (index - lowstale - 1) * sizeof(*lep));
504 lep = &leaf->ents[index - 1];
505 lfloglow = MIN(lowstale, lfloglow);
506 lfloghigh = MAX(index - 1, lfloghigh);
509 * The high one is better, so use that one.
511 else {
512 ASSERT(highstale - index >= 0);
513 ASSERT(be32_to_cpu(leaf->ents[highstale].address) ==
514 XFS_DIR2_NULL_DATAPTR);
516 * Copy entries down to cover the stale entry
517 * and make room for the new entry.
519 if (highstale - index > 0)
520 memmove(&leaf->ents[index + 1],
521 &leaf->ents[index],
522 (highstale - index) * sizeof(*lep));
523 lep = &leaf->ents[index];
524 lfloglow = MIN(index, lfloglow);
525 lfloghigh = MAX(highstale, lfloghigh);
527 be16_add_cpu(&leaf->hdr.stale, -1);
530 * Fill in the new leaf entry.
532 lep->hashval = cpu_to_be32(args->hashval);
533 lep->address = cpu_to_be32(xfs_dir2_db_off_to_dataptr(mp, use_block,
534 be16_to_cpu(*tagp)));
536 * Log the leaf fields and give up the buffers.
538 xfs_dir2_leaf_log_header(tp, lbp);
539 xfs_dir2_leaf_log_ents(tp, lbp, lfloglow, lfloghigh);
540 xfs_dir2_leaf_check(dp, lbp);
541 xfs_da_buf_done(lbp);
542 xfs_dir2_data_check(dp, dbp);
543 xfs_da_buf_done(dbp);
544 return 0;
547 #ifdef DEBUG
549 * Check the internal consistency of a leaf1 block.
550 * Pop an assert if something is wrong.
552 void
553 xfs_dir2_leaf_check(
554 xfs_inode_t *dp, /* incore directory inode */
555 xfs_dabuf_t *bp) /* leaf's buffer */
557 int i; /* leaf index */
558 xfs_dir2_leaf_t *leaf; /* leaf structure */
559 xfs_dir2_leaf_tail_t *ltp; /* leaf tail pointer */
560 xfs_mount_t *mp; /* filesystem mount point */
561 int stale; /* count of stale leaves */
563 leaf = bp->data;
564 mp = dp->i_mount;
565 ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_DIR2_LEAF1_MAGIC);
567 * This value is not restrictive enough.
568 * Should factor in the size of the bests table as well.
569 * We can deduce a value for that from di_size.
571 ASSERT(be16_to_cpu(leaf->hdr.count) <= xfs_dir2_max_leaf_ents(mp));
572 ltp = xfs_dir2_leaf_tail_p(mp, leaf);
574 * Leaves and bests don't overlap.
576 ASSERT((char *)&leaf->ents[be16_to_cpu(leaf->hdr.count)] <=
577 (char *)xfs_dir2_leaf_bests_p(ltp));
579 * Check hash value order, count stale entries.
581 for (i = stale = 0; i < be16_to_cpu(leaf->hdr.count); i++) {
582 if (i + 1 < be16_to_cpu(leaf->hdr.count))
583 ASSERT(be32_to_cpu(leaf->ents[i].hashval) <=
584 be32_to_cpu(leaf->ents[i + 1].hashval));
585 if (be32_to_cpu(leaf->ents[i].address) == XFS_DIR2_NULL_DATAPTR)
586 stale++;
588 ASSERT(be16_to_cpu(leaf->hdr.stale) == stale);
590 #endif /* DEBUG */
593 * Compact out any stale entries in the leaf.
594 * Log the header and changed leaf entries, if any.
596 void
597 xfs_dir2_leaf_compact(
598 xfs_da_args_t *args, /* operation arguments */
599 xfs_dabuf_t *bp) /* leaf buffer */
601 int from; /* source leaf index */
602 xfs_dir2_leaf_t *leaf; /* leaf structure */
603 int loglow; /* first leaf entry to log */
604 int to; /* target leaf index */
606 leaf = bp->data;
607 if (!leaf->hdr.stale) {
608 return;
611 * Compress out the stale entries in place.
613 for (from = to = 0, loglow = -1; from < be16_to_cpu(leaf->hdr.count); from++) {
614 if (be32_to_cpu(leaf->ents[from].address) == XFS_DIR2_NULL_DATAPTR)
615 continue;
617 * Only actually copy the entries that are different.
619 if (from > to) {
620 if (loglow == -1)
621 loglow = to;
622 leaf->ents[to] = leaf->ents[from];
624 to++;
627 * Update and log the header, log the leaf entries.
629 ASSERT(be16_to_cpu(leaf->hdr.stale) == from - to);
630 be16_add_cpu(&leaf->hdr.count, -(be16_to_cpu(leaf->hdr.stale)));
631 leaf->hdr.stale = 0;
632 xfs_dir2_leaf_log_header(args->trans, bp);
633 if (loglow != -1)
634 xfs_dir2_leaf_log_ents(args->trans, bp, loglow, to - 1);
638 * Compact the leaf entries, removing stale ones.
639 * Leave one stale entry behind - the one closest to our
640 * insertion index - and the caller will shift that one to our insertion
641 * point later.
642 * Return new insertion index, where the remaining stale entry is,
643 * and leaf logging indices.
645 void
646 xfs_dir2_leaf_compact_x1(
647 xfs_dabuf_t *bp, /* leaf buffer */
648 int *indexp, /* insertion index */
649 int *lowstalep, /* out: stale entry before us */
650 int *highstalep, /* out: stale entry after us */
651 int *lowlogp, /* out: low log index */
652 int *highlogp) /* out: high log index */
654 int from; /* source copy index */
655 int highstale; /* stale entry at/after index */
656 int index; /* insertion index */
657 int keepstale; /* source index of kept stale */
658 xfs_dir2_leaf_t *leaf; /* leaf structure */
659 int lowstale; /* stale entry before index */
660 int newindex=0; /* new insertion index */
661 int to; /* destination copy index */
663 leaf = bp->data;
664 ASSERT(be16_to_cpu(leaf->hdr.stale) > 1);
665 index = *indexp;
667 * Find the first stale entry before our index, if any.
669 for (lowstale = index - 1;
670 lowstale >= 0 &&
671 be32_to_cpu(leaf->ents[lowstale].address) != XFS_DIR2_NULL_DATAPTR;
672 lowstale--)
673 continue;
675 * Find the first stale entry at or after our index, if any.
676 * Stop if the answer would be worse than lowstale.
678 for (highstale = index;
679 highstale < be16_to_cpu(leaf->hdr.count) &&
680 be32_to_cpu(leaf->ents[highstale].address) != XFS_DIR2_NULL_DATAPTR &&
681 (lowstale < 0 || index - lowstale > highstale - index);
682 highstale++)
683 continue;
685 * Pick the better of lowstale and highstale.
687 if (lowstale >= 0 &&
688 (highstale == be16_to_cpu(leaf->hdr.count) ||
689 index - lowstale <= highstale - index))
690 keepstale = lowstale;
691 else
692 keepstale = highstale;
694 * Copy the entries in place, removing all the stale entries
695 * except keepstale.
697 for (from = to = 0; from < be16_to_cpu(leaf->hdr.count); from++) {
699 * Notice the new value of index.
701 if (index == from)
702 newindex = to;
703 if (from != keepstale &&
704 be32_to_cpu(leaf->ents[from].address) == XFS_DIR2_NULL_DATAPTR) {
705 if (from == to)
706 *lowlogp = to;
707 continue;
710 * Record the new keepstale value for the insertion.
712 if (from == keepstale)
713 lowstale = highstale = to;
715 * Copy only the entries that have moved.
717 if (from > to)
718 leaf->ents[to] = leaf->ents[from];
719 to++;
721 ASSERT(from > to);
723 * If the insertion point was past the last entry,
724 * set the new insertion point accordingly.
726 if (index == from)
727 newindex = to;
728 *indexp = newindex;
730 * Adjust the leaf header values.
732 be16_add_cpu(&leaf->hdr.count, -(from - to));
733 leaf->hdr.stale = cpu_to_be16(1);
735 * Remember the low/high stale value only in the "right"
736 * direction.
738 if (lowstale >= newindex)
739 lowstale = -1;
740 else
741 highstale = be16_to_cpu(leaf->hdr.count);
742 *highlogp = be16_to_cpu(leaf->hdr.count) - 1;
743 *lowstalep = lowstale;
744 *highstalep = highstale;
748 * Getdents (readdir) for leaf and node directories.
749 * This reads the data blocks only, so is the same for both forms.
751 int /* error */
752 xfs_dir2_leaf_getdents(
753 xfs_inode_t *dp, /* incore directory inode */
754 void *dirent,
755 size_t bufsize,
756 xfs_off_t *offset,
757 filldir_t filldir)
759 xfs_dabuf_t *bp; /* data block buffer */
760 int byteoff; /* offset in current block */
761 xfs_dir2_db_t curdb; /* db for current block */
762 xfs_dir2_off_t curoff; /* current overall offset */
763 xfs_dir2_data_t *data; /* data block structure */
764 xfs_dir2_data_entry_t *dep; /* data entry */
765 xfs_dir2_data_unused_t *dup; /* unused entry */
766 int error = 0; /* error return value */
767 int i; /* temporary loop index */
768 int j; /* temporary loop index */
769 int length; /* temporary length value */
770 xfs_bmbt_irec_t *map; /* map vector for blocks */
771 xfs_extlen_t map_blocks; /* number of fsbs in map */
772 xfs_dablk_t map_off; /* last mapped file offset */
773 int map_size; /* total entries in *map */
774 int map_valid; /* valid entries in *map */
775 xfs_mount_t *mp; /* filesystem mount point */
776 xfs_dir2_off_t newoff; /* new curoff after new blk */
777 int nmap; /* mappings to ask xfs_bmapi */
778 char *ptr = NULL; /* pointer to current data */
779 int ra_current; /* number of read-ahead blks */
780 int ra_index; /* *map index for read-ahead */
781 int ra_offset; /* map entry offset for ra */
782 int ra_want; /* readahead count wanted */
783 xfs_ino_t ino;
786 * If the offset is at or past the largest allowed value,
787 * give up right away.
789 if (*offset >= XFS_DIR2_MAX_DATAPTR)
790 return 0;
792 mp = dp->i_mount;
795 * Set up to bmap a number of blocks based on the caller's
796 * buffer size, the directory block size, and the filesystem
797 * block size.
799 map_size = howmany(bufsize + mp->m_dirblksize, mp->m_sb.sb_blocksize);
800 map = kmem_alloc(map_size * sizeof(*map), KM_SLEEP);
801 map_valid = ra_index = ra_offset = ra_current = map_blocks = 0;
802 bp = NULL;
805 * Inside the loop we keep the main offset value as a byte offset
806 * in the directory file.
808 curoff = xfs_dir2_dataptr_to_byte(mp, *offset);
811 * Force this conversion through db so we truncate the offset
812 * down to get the start of the data block.
814 map_off = xfs_dir2_db_to_da(mp, xfs_dir2_byte_to_db(mp, curoff));
816 * Loop over directory entries until we reach the end offset.
817 * Get more blocks and readahead as necessary.
819 while (curoff < XFS_DIR2_LEAF_OFFSET) {
821 * If we have no buffer, or we're off the end of the
822 * current buffer, need to get another one.
824 if (!bp || ptr >= (char *)bp->data + mp->m_dirblksize) {
826 * If we have a buffer, we need to release it and
827 * take it out of the mapping.
829 if (bp) {
830 xfs_da_brelse(NULL, bp);
831 bp = NULL;
832 map_blocks -= mp->m_dirblkfsbs;
834 * Loop to get rid of the extents for the
835 * directory block.
837 for (i = mp->m_dirblkfsbs; i > 0; ) {
838 j = MIN((int)map->br_blockcount, i);
839 map->br_blockcount -= j;
840 map->br_startblock += j;
841 map->br_startoff += j;
843 * If mapping is done, pitch it from
844 * the table.
846 if (!map->br_blockcount && --map_valid)
847 memmove(&map[0], &map[1],
848 sizeof(map[0]) *
849 map_valid);
850 i -= j;
854 * Recalculate the readahead blocks wanted.
856 ra_want = howmany(bufsize + mp->m_dirblksize,
857 mp->m_sb.sb_blocksize) - 1;
860 * If we don't have as many as we want, and we haven't
861 * run out of data blocks, get some more mappings.
863 if (1 + ra_want > map_blocks &&
864 map_off <
865 xfs_dir2_byte_to_da(mp, XFS_DIR2_LEAF_OFFSET)) {
867 * Get more bmaps, fill in after the ones
868 * we already have in the table.
870 nmap = map_size - map_valid;
871 error = xfs_bmapi(NULL, dp,
872 map_off,
873 xfs_dir2_byte_to_da(mp,
874 XFS_DIR2_LEAF_OFFSET) - map_off,
875 XFS_BMAPI_METADATA, NULL, 0,
876 &map[map_valid], &nmap, NULL, NULL);
878 * Don't know if we should ignore this or
879 * try to return an error.
880 * The trouble with returning errors
881 * is that readdir will just stop without
882 * actually passing the error through.
884 if (error)
885 break; /* XXX */
887 * If we got all the mappings we asked for,
888 * set the final map offset based on the
889 * last bmap value received.
890 * Otherwise, we've reached the end.
892 if (nmap == map_size - map_valid)
893 map_off =
894 map[map_valid + nmap - 1].br_startoff +
895 map[map_valid + nmap - 1].br_blockcount;
896 else
897 map_off =
898 xfs_dir2_byte_to_da(mp,
899 XFS_DIR2_LEAF_OFFSET);
901 * Look for holes in the mapping, and
902 * eliminate them. Count up the valid blocks.
904 for (i = map_valid; i < map_valid + nmap; ) {
905 if (map[i].br_startblock ==
906 HOLESTARTBLOCK) {
907 nmap--;
908 length = map_valid + nmap - i;
909 if (length)
910 memmove(&map[i],
911 &map[i + 1],
912 sizeof(map[i]) *
913 length);
914 } else {
915 map_blocks +=
916 map[i].br_blockcount;
917 i++;
920 map_valid += nmap;
923 * No valid mappings, so no more data blocks.
925 if (!map_valid) {
926 curoff = xfs_dir2_da_to_byte(mp, map_off);
927 break;
930 * Read the directory block starting at the first
931 * mapping.
933 curdb = xfs_dir2_da_to_db(mp, map->br_startoff);
934 error = xfs_da_read_buf(NULL, dp, map->br_startoff,
935 map->br_blockcount >= mp->m_dirblkfsbs ?
936 XFS_FSB_TO_DADDR(mp, map->br_startblock) :
938 &bp, XFS_DATA_FORK);
940 * Should just skip over the data block instead
941 * of giving up.
943 if (error)
944 break; /* XXX */
946 * Adjust the current amount of read-ahead: we just
947 * read a block that was previously ra.
949 if (ra_current)
950 ra_current -= mp->m_dirblkfsbs;
952 * Do we need more readahead?
954 for (ra_index = ra_offset = i = 0;
955 ra_want > ra_current && i < map_blocks;
956 i += mp->m_dirblkfsbs) {
957 ASSERT(ra_index < map_valid);
959 * Read-ahead a contiguous directory block.
961 if (i > ra_current &&
962 map[ra_index].br_blockcount >=
963 mp->m_dirblkfsbs) {
964 xfs_baread(mp->m_ddev_targp,
965 XFS_FSB_TO_DADDR(mp,
966 map[ra_index].br_startblock +
967 ra_offset),
968 (int)BTOBB(mp->m_dirblksize));
969 ra_current = i;
972 * Read-ahead a non-contiguous directory block.
973 * This doesn't use our mapping, but this
974 * is a very rare case.
976 else if (i > ra_current) {
977 (void)xfs_da_reada_buf(NULL, dp,
978 map[ra_index].br_startoff +
979 ra_offset, XFS_DATA_FORK);
980 ra_current = i;
983 * Advance offset through the mapping table.
985 for (j = 0; j < mp->m_dirblkfsbs; j++) {
987 * The rest of this extent but not
988 * more than a dir block.
990 length = MIN(mp->m_dirblkfsbs,
991 (int)(map[ra_index].br_blockcount -
992 ra_offset));
993 j += length;
994 ra_offset += length;
996 * Advance to the next mapping if
997 * this one is used up.
999 if (ra_offset ==
1000 map[ra_index].br_blockcount) {
1001 ra_offset = 0;
1002 ra_index++;
1007 * Having done a read, we need to set a new offset.
1009 newoff = xfs_dir2_db_off_to_byte(mp, curdb, 0);
1011 * Start of the current block.
1013 if (curoff < newoff)
1014 curoff = newoff;
1016 * Make sure we're in the right block.
1018 else if (curoff > newoff)
1019 ASSERT(xfs_dir2_byte_to_db(mp, curoff) ==
1020 curdb);
1021 data = bp->data;
1022 xfs_dir2_data_check(dp, bp);
1024 * Find our position in the block.
1026 ptr = (char *)&data->u;
1027 byteoff = xfs_dir2_byte_to_off(mp, curoff);
1029 * Skip past the header.
1031 if (byteoff == 0)
1032 curoff += (uint)sizeof(data->hdr);
1034 * Skip past entries until we reach our offset.
1036 else {
1037 while ((char *)ptr - (char *)data < byteoff) {
1038 dup = (xfs_dir2_data_unused_t *)ptr;
1040 if (be16_to_cpu(dup->freetag)
1041 == XFS_DIR2_DATA_FREE_TAG) {
1043 length = be16_to_cpu(dup->length);
1044 ptr += length;
1045 continue;
1047 dep = (xfs_dir2_data_entry_t *)ptr;
1048 length =
1049 xfs_dir2_data_entsize(dep->namelen);
1050 ptr += length;
1053 * Now set our real offset.
1055 curoff =
1056 xfs_dir2_db_off_to_byte(mp,
1057 xfs_dir2_byte_to_db(mp, curoff),
1058 (char *)ptr - (char *)data);
1059 if (ptr >= (char *)data + mp->m_dirblksize) {
1060 continue;
1065 * We have a pointer to an entry.
1066 * Is it a live one?
1068 dup = (xfs_dir2_data_unused_t *)ptr;
1070 * No, it's unused, skip over it.
1072 if (be16_to_cpu(dup->freetag) == XFS_DIR2_DATA_FREE_TAG) {
1073 length = be16_to_cpu(dup->length);
1074 ptr += length;
1075 curoff += length;
1076 continue;
1080 * Copy the entry into the putargs, and try formatting it.
1082 dep = (xfs_dir2_data_entry_t *)ptr;
1084 length = xfs_dir2_data_entsize(dep->namelen);
1086 ino = be64_to_cpu(dep->inumber);
1087 #if XFS_BIG_INUMS
1088 ino += mp->m_inoadd;
1089 #endif
1092 * Won't fit. Return to caller.
1094 if (filldir(dirent, dep->name, dep->namelen,
1095 xfs_dir2_byte_to_dataptr(mp, curoff),
1096 ino, DT_UNKNOWN))
1097 break;
1100 * Advance to next entry in the block.
1102 ptr += length;
1103 curoff += length;
1104 bufsize -= length;
1108 * All done. Set output offset value to current offset.
1110 if (curoff > xfs_dir2_dataptr_to_byte(mp, XFS_DIR2_MAX_DATAPTR))
1111 *offset = XFS_DIR2_MAX_DATAPTR;
1112 else
1113 *offset = xfs_dir2_byte_to_dataptr(mp, curoff);
1114 kmem_free(map);
1115 if (bp)
1116 xfs_da_brelse(NULL, bp);
1117 return error;
1121 * Initialize a new leaf block, leaf1 or leafn magic accepted.
1124 xfs_dir2_leaf_init(
1125 xfs_da_args_t *args, /* operation arguments */
1126 xfs_dir2_db_t bno, /* directory block number */
1127 xfs_dabuf_t **bpp, /* out: leaf buffer */
1128 int magic) /* magic number for block */
1130 xfs_dabuf_t *bp; /* leaf buffer */
1131 xfs_inode_t *dp; /* incore directory inode */
1132 int error; /* error return code */
1133 xfs_dir2_leaf_t *leaf; /* leaf structure */
1134 xfs_dir2_leaf_tail_t *ltp; /* leaf tail structure */
1135 xfs_mount_t *mp; /* filesystem mount point */
1136 xfs_trans_t *tp; /* transaction pointer */
1138 dp = args->dp;
1139 ASSERT(dp != NULL);
1140 tp = args->trans;
1141 mp = dp->i_mount;
1142 ASSERT(bno >= XFS_DIR2_LEAF_FIRSTDB(mp) &&
1143 bno < XFS_DIR2_FREE_FIRSTDB(mp));
1145 * Get the buffer for the block.
1147 error = xfs_da_get_buf(tp, dp, xfs_dir2_db_to_da(mp, bno), -1, &bp,
1148 XFS_DATA_FORK);
1149 if (error) {
1150 return error;
1152 ASSERT(bp != NULL);
1153 leaf = bp->data;
1155 * Initialize the header.
1157 leaf->hdr.info.magic = cpu_to_be16(magic);
1158 leaf->hdr.info.forw = 0;
1159 leaf->hdr.info.back = 0;
1160 leaf->hdr.count = 0;
1161 leaf->hdr.stale = 0;
1162 xfs_dir2_leaf_log_header(tp, bp);
1164 * If it's a leaf-format directory initialize the tail.
1165 * In this case our caller has the real bests table to copy into
1166 * the block.
1168 if (magic == XFS_DIR2_LEAF1_MAGIC) {
1169 ltp = xfs_dir2_leaf_tail_p(mp, leaf);
1170 ltp->bestcount = 0;
1171 xfs_dir2_leaf_log_tail(tp, bp);
1173 *bpp = bp;
1174 return 0;
1178 * Log the bests entries indicated from a leaf1 block.
1180 static void
1181 xfs_dir2_leaf_log_bests(
1182 xfs_trans_t *tp, /* transaction pointer */
1183 xfs_dabuf_t *bp, /* leaf buffer */
1184 int first, /* first entry to log */
1185 int last) /* last entry to log */
1187 __be16 *firstb; /* pointer to first entry */
1188 __be16 *lastb; /* pointer to last entry */
1189 xfs_dir2_leaf_t *leaf; /* leaf structure */
1190 xfs_dir2_leaf_tail_t *ltp; /* leaf tail structure */
1192 leaf = bp->data;
1193 ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_DIR2_LEAF1_MAGIC);
1194 ltp = xfs_dir2_leaf_tail_p(tp->t_mountp, leaf);
1195 firstb = xfs_dir2_leaf_bests_p(ltp) + first;
1196 lastb = xfs_dir2_leaf_bests_p(ltp) + last;
1197 xfs_da_log_buf(tp, bp, (uint)((char *)firstb - (char *)leaf),
1198 (uint)((char *)lastb - (char *)leaf + sizeof(*lastb) - 1));
1202 * Log the leaf entries indicated from a leaf1 or leafn block.
1204 void
1205 xfs_dir2_leaf_log_ents(
1206 xfs_trans_t *tp, /* transaction pointer */
1207 xfs_dabuf_t *bp, /* leaf buffer */
1208 int first, /* first entry to log */
1209 int last) /* last entry to log */
1211 xfs_dir2_leaf_entry_t *firstlep; /* pointer to first entry */
1212 xfs_dir2_leaf_entry_t *lastlep; /* pointer to last entry */
1213 xfs_dir2_leaf_t *leaf; /* leaf structure */
1215 leaf = bp->data;
1216 ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_DIR2_LEAF1_MAGIC ||
1217 be16_to_cpu(leaf->hdr.info.magic) == XFS_DIR2_LEAFN_MAGIC);
1218 firstlep = &leaf->ents[first];
1219 lastlep = &leaf->ents[last];
1220 xfs_da_log_buf(tp, bp, (uint)((char *)firstlep - (char *)leaf),
1221 (uint)((char *)lastlep - (char *)leaf + sizeof(*lastlep) - 1));
1225 * Log the header of the leaf1 or leafn block.
1227 void
1228 xfs_dir2_leaf_log_header(
1229 xfs_trans_t *tp, /* transaction pointer */
1230 xfs_dabuf_t *bp) /* leaf buffer */
1232 xfs_dir2_leaf_t *leaf; /* leaf structure */
1234 leaf = bp->data;
1235 ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_DIR2_LEAF1_MAGIC ||
1236 be16_to_cpu(leaf->hdr.info.magic) == XFS_DIR2_LEAFN_MAGIC);
1237 xfs_da_log_buf(tp, bp, (uint)((char *)&leaf->hdr - (char *)leaf),
1238 (uint)(sizeof(leaf->hdr) - 1));
1242 * Log the tail of the leaf1 block.
1244 STATIC void
1245 xfs_dir2_leaf_log_tail(
1246 xfs_trans_t *tp, /* transaction pointer */
1247 xfs_dabuf_t *bp) /* leaf buffer */
1249 xfs_dir2_leaf_t *leaf; /* leaf structure */
1250 xfs_dir2_leaf_tail_t *ltp; /* leaf tail structure */
1251 xfs_mount_t *mp; /* filesystem mount point */
1253 mp = tp->t_mountp;
1254 leaf = bp->data;
1255 ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_DIR2_LEAF1_MAGIC);
1256 ltp = xfs_dir2_leaf_tail_p(mp, leaf);
1257 xfs_da_log_buf(tp, bp, (uint)((char *)ltp - (char *)leaf),
1258 (uint)(mp->m_dirblksize - 1));
1262 * Look up the entry referred to by args in the leaf format directory.
1263 * Most of the work is done by the xfs_dir2_leaf_lookup_int routine which
1264 * is also used by the node-format code.
1267 xfs_dir2_leaf_lookup(
1268 xfs_da_args_t *args) /* operation arguments */
1270 xfs_dabuf_t *dbp; /* data block buffer */
1271 xfs_dir2_data_entry_t *dep; /* data block entry */
1272 xfs_inode_t *dp; /* incore directory inode */
1273 int error; /* error return code */
1274 int index; /* found entry index */
1275 xfs_dabuf_t *lbp; /* leaf buffer */
1276 xfs_dir2_leaf_t *leaf; /* leaf structure */
1277 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
1278 xfs_trans_t *tp; /* transaction pointer */
1280 xfs_dir2_trace_args("leaf_lookup", args);
1282 * Look up name in the leaf block, returning both buffers and index.
1284 if ((error = xfs_dir2_leaf_lookup_int(args, &lbp, &index, &dbp))) {
1285 return error;
1287 tp = args->trans;
1288 dp = args->dp;
1289 xfs_dir2_leaf_check(dp, lbp);
1290 leaf = lbp->data;
1292 * Get to the leaf entry and contained data entry address.
1294 lep = &leaf->ents[index];
1296 * Point to the data entry.
1298 dep = (xfs_dir2_data_entry_t *)
1299 ((char *)dbp->data +
1300 xfs_dir2_dataptr_to_off(dp->i_mount, be32_to_cpu(lep->address)));
1302 * Return the found inode number & CI name if appropriate
1304 args->inumber = be64_to_cpu(dep->inumber);
1305 error = xfs_dir_cilookup_result(args, dep->name, dep->namelen);
1306 xfs_da_brelse(tp, dbp);
1307 xfs_da_brelse(tp, lbp);
1308 return XFS_ERROR(error);
1312 * Look up name/hash in the leaf block.
1313 * Fill in indexp with the found index, and dbpp with the data buffer.
1314 * If not found dbpp will be NULL, and ENOENT comes back.
1315 * lbpp will always be filled in with the leaf buffer unless there's an error.
1317 static int /* error */
1318 xfs_dir2_leaf_lookup_int(
1319 xfs_da_args_t *args, /* operation arguments */
1320 xfs_dabuf_t **lbpp, /* out: leaf buffer */
1321 int *indexp, /* out: index in leaf block */
1322 xfs_dabuf_t **dbpp) /* out: data buffer */
1324 xfs_dir2_db_t curdb = -1; /* current data block number */
1325 xfs_dabuf_t *dbp = NULL; /* data buffer */
1326 xfs_dir2_data_entry_t *dep; /* data entry */
1327 xfs_inode_t *dp; /* incore directory inode */
1328 int error; /* error return code */
1329 int index; /* index in leaf block */
1330 xfs_dabuf_t *lbp; /* leaf buffer */
1331 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
1332 xfs_dir2_leaf_t *leaf; /* leaf structure */
1333 xfs_mount_t *mp; /* filesystem mount point */
1334 xfs_dir2_db_t newdb; /* new data block number */
1335 xfs_trans_t *tp; /* transaction pointer */
1336 xfs_dir2_db_t cidb = -1; /* case match data block no. */
1337 enum xfs_dacmp cmp; /* name compare result */
1339 dp = args->dp;
1340 tp = args->trans;
1341 mp = dp->i_mount;
1343 * Read the leaf block into the buffer.
1345 error = xfs_da_read_buf(tp, dp, mp->m_dirleafblk, -1, &lbp,
1346 XFS_DATA_FORK);
1347 if (error)
1348 return error;
1349 *lbpp = lbp;
1350 leaf = lbp->data;
1351 xfs_dir2_leaf_check(dp, lbp);
1353 * Look for the first leaf entry with our hash value.
1355 index = xfs_dir2_leaf_search_hash(args, lbp);
1357 * Loop over all the entries with the right hash value
1358 * looking to match the name.
1360 for (lep = &leaf->ents[index]; index < be16_to_cpu(leaf->hdr.count) &&
1361 be32_to_cpu(lep->hashval) == args->hashval;
1362 lep++, index++) {
1364 * Skip over stale leaf entries.
1366 if (be32_to_cpu(lep->address) == XFS_DIR2_NULL_DATAPTR)
1367 continue;
1369 * Get the new data block number.
1371 newdb = xfs_dir2_dataptr_to_db(mp, be32_to_cpu(lep->address));
1373 * If it's not the same as the old data block number,
1374 * need to pitch the old one and read the new one.
1376 if (newdb != curdb) {
1377 if (dbp)
1378 xfs_da_brelse(tp, dbp);
1379 error = xfs_da_read_buf(tp, dp,
1380 xfs_dir2_db_to_da(mp, newdb),
1381 -1, &dbp, XFS_DATA_FORK);
1382 if (error) {
1383 xfs_da_brelse(tp, lbp);
1384 return error;
1386 xfs_dir2_data_check(dp, dbp);
1387 curdb = newdb;
1390 * Point to the data entry.
1392 dep = (xfs_dir2_data_entry_t *)((char *)dbp->data +
1393 xfs_dir2_dataptr_to_off(mp, be32_to_cpu(lep->address)));
1395 * Compare name and if it's an exact match, return the index
1396 * and buffer. If it's the first case-insensitive match, store
1397 * the index and buffer and continue looking for an exact match.
1399 cmp = mp->m_dirnameops->compname(args, dep->name, dep->namelen);
1400 if (cmp != XFS_CMP_DIFFERENT && cmp != args->cmpresult) {
1401 args->cmpresult = cmp;
1402 *indexp = index;
1403 /* case exact match: return the current buffer. */
1404 if (cmp == XFS_CMP_EXACT) {
1405 *dbpp = dbp;
1406 return 0;
1408 cidb = curdb;
1411 ASSERT(args->op_flags & XFS_DA_OP_OKNOENT);
1413 * Here, we can only be doing a lookup (not a rename or remove).
1414 * If a case-insensitive match was found earlier, re-read the
1415 * appropriate data block if required and return it.
1417 if (args->cmpresult == XFS_CMP_CASE) {
1418 ASSERT(cidb != -1);
1419 if (cidb != curdb) {
1420 xfs_da_brelse(tp, dbp);
1421 error = xfs_da_read_buf(tp, dp,
1422 xfs_dir2_db_to_da(mp, cidb),
1423 -1, &dbp, XFS_DATA_FORK);
1424 if (error) {
1425 xfs_da_brelse(tp, lbp);
1426 return error;
1429 *dbpp = dbp;
1430 return 0;
1433 * No match found, return ENOENT.
1435 ASSERT(cidb == -1);
1436 if (dbp)
1437 xfs_da_brelse(tp, dbp);
1438 xfs_da_brelse(tp, lbp);
1439 return XFS_ERROR(ENOENT);
1443 * Remove an entry from a leaf format directory.
1445 int /* error */
1446 xfs_dir2_leaf_removename(
1447 xfs_da_args_t *args) /* operation arguments */
1449 __be16 *bestsp; /* leaf block best freespace */
1450 xfs_dir2_data_t *data; /* data block structure */
1451 xfs_dir2_db_t db; /* data block number */
1452 xfs_dabuf_t *dbp; /* data block buffer */
1453 xfs_dir2_data_entry_t *dep; /* data entry structure */
1454 xfs_inode_t *dp; /* incore directory inode */
1455 int error; /* error return code */
1456 xfs_dir2_db_t i; /* temporary data block # */
1457 int index; /* index into leaf entries */
1458 xfs_dabuf_t *lbp; /* leaf buffer */
1459 xfs_dir2_leaf_t *leaf; /* leaf structure */
1460 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
1461 xfs_dir2_leaf_tail_t *ltp; /* leaf tail structure */
1462 xfs_mount_t *mp; /* filesystem mount point */
1463 int needlog; /* need to log data header */
1464 int needscan; /* need to rescan data frees */
1465 xfs_dir2_data_off_t oldbest; /* old value of best free */
1466 xfs_trans_t *tp; /* transaction pointer */
1468 xfs_dir2_trace_args("leaf_removename", args);
1470 * Lookup the leaf entry, get the leaf and data blocks read in.
1472 if ((error = xfs_dir2_leaf_lookup_int(args, &lbp, &index, &dbp))) {
1473 return error;
1475 dp = args->dp;
1476 tp = args->trans;
1477 mp = dp->i_mount;
1478 leaf = lbp->data;
1479 data = dbp->data;
1480 xfs_dir2_data_check(dp, dbp);
1482 * Point to the leaf entry, use that to point to the data entry.
1484 lep = &leaf->ents[index];
1485 db = xfs_dir2_dataptr_to_db(mp, be32_to_cpu(lep->address));
1486 dep = (xfs_dir2_data_entry_t *)
1487 ((char *)data + xfs_dir2_dataptr_to_off(mp, be32_to_cpu(lep->address)));
1488 needscan = needlog = 0;
1489 oldbest = be16_to_cpu(data->hdr.bestfree[0].length);
1490 ltp = xfs_dir2_leaf_tail_p(mp, leaf);
1491 bestsp = xfs_dir2_leaf_bests_p(ltp);
1492 ASSERT(be16_to_cpu(bestsp[db]) == oldbest);
1494 * Mark the former data entry unused.
1496 xfs_dir2_data_make_free(tp, dbp,
1497 (xfs_dir2_data_aoff_t)((char *)dep - (char *)data),
1498 xfs_dir2_data_entsize(dep->namelen), &needlog, &needscan);
1500 * We just mark the leaf entry stale by putting a null in it.
1502 be16_add_cpu(&leaf->hdr.stale, 1);
1503 xfs_dir2_leaf_log_header(tp, lbp);
1504 lep->address = cpu_to_be32(XFS_DIR2_NULL_DATAPTR);
1505 xfs_dir2_leaf_log_ents(tp, lbp, index, index);
1507 * Scan the freespace in the data block again if necessary,
1508 * log the data block header if necessary.
1510 if (needscan)
1511 xfs_dir2_data_freescan(mp, data, &needlog);
1512 if (needlog)
1513 xfs_dir2_data_log_header(tp, dbp);
1515 * If the longest freespace in the data block has changed,
1516 * put the new value in the bests table and log that.
1518 if (be16_to_cpu(data->hdr.bestfree[0].length) != oldbest) {
1519 bestsp[db] = data->hdr.bestfree[0].length;
1520 xfs_dir2_leaf_log_bests(tp, lbp, db, db);
1522 xfs_dir2_data_check(dp, dbp);
1524 * If the data block is now empty then get rid of the data block.
1526 if (be16_to_cpu(data->hdr.bestfree[0].length) ==
1527 mp->m_dirblksize - (uint)sizeof(data->hdr)) {
1528 ASSERT(db != mp->m_dirdatablk);
1529 if ((error = xfs_dir2_shrink_inode(args, db, dbp))) {
1531 * Nope, can't get rid of it because it caused
1532 * allocation of a bmap btree block to do so.
1533 * Just go on, returning success, leaving the
1534 * empty block in place.
1536 if (error == ENOSPC && args->total == 0) {
1537 xfs_da_buf_done(dbp);
1538 error = 0;
1540 xfs_dir2_leaf_check(dp, lbp);
1541 xfs_da_buf_done(lbp);
1542 return error;
1544 dbp = NULL;
1546 * If this is the last data block then compact the
1547 * bests table by getting rid of entries.
1549 if (db == be32_to_cpu(ltp->bestcount) - 1) {
1551 * Look for the last active entry (i).
1553 for (i = db - 1; i > 0; i--) {
1554 if (be16_to_cpu(bestsp[i]) != NULLDATAOFF)
1555 break;
1558 * Copy the table down so inactive entries at the
1559 * end are removed.
1561 memmove(&bestsp[db - i], bestsp,
1562 (be32_to_cpu(ltp->bestcount) - (db - i)) * sizeof(*bestsp));
1563 be32_add_cpu(&ltp->bestcount, -(db - i));
1564 xfs_dir2_leaf_log_tail(tp, lbp);
1565 xfs_dir2_leaf_log_bests(tp, lbp, 0, be32_to_cpu(ltp->bestcount) - 1);
1566 } else
1567 bestsp[db] = cpu_to_be16(NULLDATAOFF);
1570 * If the data block was not the first one, drop it.
1572 else if (db != mp->m_dirdatablk && dbp != NULL) {
1573 xfs_da_buf_done(dbp);
1574 dbp = NULL;
1576 xfs_dir2_leaf_check(dp, lbp);
1578 * See if we can convert to block form.
1580 return xfs_dir2_leaf_to_block(args, lbp, dbp);
1584 * Replace the inode number in a leaf format directory entry.
1586 int /* error */
1587 xfs_dir2_leaf_replace(
1588 xfs_da_args_t *args) /* operation arguments */
1590 xfs_dabuf_t *dbp; /* data block buffer */
1591 xfs_dir2_data_entry_t *dep; /* data block entry */
1592 xfs_inode_t *dp; /* incore directory inode */
1593 int error; /* error return code */
1594 int index; /* index of leaf entry */
1595 xfs_dabuf_t *lbp; /* leaf buffer */
1596 xfs_dir2_leaf_t *leaf; /* leaf structure */
1597 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
1598 xfs_trans_t *tp; /* transaction pointer */
1600 xfs_dir2_trace_args("leaf_replace", args);
1602 * Look up the entry.
1604 if ((error = xfs_dir2_leaf_lookup_int(args, &lbp, &index, &dbp))) {
1605 return error;
1607 dp = args->dp;
1608 leaf = lbp->data;
1610 * Point to the leaf entry, get data address from it.
1612 lep = &leaf->ents[index];
1614 * Point to the data entry.
1616 dep = (xfs_dir2_data_entry_t *)
1617 ((char *)dbp->data +
1618 xfs_dir2_dataptr_to_off(dp->i_mount, be32_to_cpu(lep->address)));
1619 ASSERT(args->inumber != be64_to_cpu(dep->inumber));
1621 * Put the new inode number in, log it.
1623 dep->inumber = cpu_to_be64(args->inumber);
1624 tp = args->trans;
1625 xfs_dir2_data_log_entry(tp, dbp, dep);
1626 xfs_da_buf_done(dbp);
1627 xfs_dir2_leaf_check(dp, lbp);
1628 xfs_da_brelse(tp, lbp);
1629 return 0;
1633 * Return index in the leaf block (lbp) which is either the first
1634 * one with this hash value, or if there are none, the insert point
1635 * for that hash value.
1637 int /* index value */
1638 xfs_dir2_leaf_search_hash(
1639 xfs_da_args_t *args, /* operation arguments */
1640 xfs_dabuf_t *lbp) /* leaf buffer */
1642 xfs_dahash_t hash=0; /* hash from this entry */
1643 xfs_dahash_t hashwant; /* hash value looking for */
1644 int high; /* high leaf index */
1645 int low; /* low leaf index */
1646 xfs_dir2_leaf_t *leaf; /* leaf structure */
1647 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
1648 int mid=0; /* current leaf index */
1650 leaf = lbp->data;
1651 #ifndef __KERNEL__
1652 if (!leaf->hdr.count)
1653 return 0;
1654 #endif
1656 * Note, the table cannot be empty, so we have to go through the loop.
1657 * Binary search the leaf entries looking for our hash value.
1659 for (lep = leaf->ents, low = 0, high = be16_to_cpu(leaf->hdr.count) - 1,
1660 hashwant = args->hashval;
1661 low <= high; ) {
1662 mid = (low + high) >> 1;
1663 if ((hash = be32_to_cpu(lep[mid].hashval)) == hashwant)
1664 break;
1665 if (hash < hashwant)
1666 low = mid + 1;
1667 else
1668 high = mid - 1;
1671 * Found one, back up through all the equal hash values.
1673 if (hash == hashwant) {
1674 while (mid > 0 && be32_to_cpu(lep[mid - 1].hashval) == hashwant) {
1675 mid--;
1679 * Need to point to an entry higher than ours.
1681 else if (hash < hashwant)
1682 mid++;
1683 return mid;
1687 * Trim off a trailing data block. We know it's empty since the leaf
1688 * freespace table says so.
1690 int /* error */
1691 xfs_dir2_leaf_trim_data(
1692 xfs_da_args_t *args, /* operation arguments */
1693 xfs_dabuf_t *lbp, /* leaf buffer */
1694 xfs_dir2_db_t db) /* data block number */
1696 __be16 *bestsp; /* leaf bests table */
1697 #ifdef DEBUG
1698 xfs_dir2_data_t *data; /* data block structure */
1699 #endif
1700 xfs_dabuf_t *dbp; /* data block buffer */
1701 xfs_inode_t *dp; /* incore directory inode */
1702 int error; /* error return value */
1703 xfs_dir2_leaf_t *leaf; /* leaf structure */
1704 xfs_dir2_leaf_tail_t *ltp; /* leaf tail structure */
1705 xfs_mount_t *mp; /* filesystem mount point */
1706 xfs_trans_t *tp; /* transaction pointer */
1708 dp = args->dp;
1709 mp = dp->i_mount;
1710 tp = args->trans;
1712 * Read the offending data block. We need its buffer.
1714 if ((error = xfs_da_read_buf(tp, dp, xfs_dir2_db_to_da(mp, db), -1, &dbp,
1715 XFS_DATA_FORK))) {
1716 return error;
1718 #ifdef DEBUG
1719 data = dbp->data;
1720 ASSERT(be32_to_cpu(data->hdr.magic) == XFS_DIR2_DATA_MAGIC);
1721 #endif
1722 /* this seems to be an error
1723 * data is only valid if DEBUG is defined?
1724 * RMC 09/08/1999
1727 leaf = lbp->data;
1728 ltp = xfs_dir2_leaf_tail_p(mp, leaf);
1729 ASSERT(be16_to_cpu(data->hdr.bestfree[0].length) ==
1730 mp->m_dirblksize - (uint)sizeof(data->hdr));
1731 ASSERT(db == be32_to_cpu(ltp->bestcount) - 1);
1733 * Get rid of the data block.
1735 if ((error = xfs_dir2_shrink_inode(args, db, dbp))) {
1736 ASSERT(error != ENOSPC);
1737 xfs_da_brelse(tp, dbp);
1738 return error;
1741 * Eliminate the last bests entry from the table.
1743 bestsp = xfs_dir2_leaf_bests_p(ltp);
1744 be32_add_cpu(&ltp->bestcount, -1);
1745 memmove(&bestsp[1], &bestsp[0], be32_to_cpu(ltp->bestcount) * sizeof(*bestsp));
1746 xfs_dir2_leaf_log_tail(tp, lbp);
1747 xfs_dir2_leaf_log_bests(tp, lbp, 0, be32_to_cpu(ltp->bestcount) - 1);
1748 return 0;
1752 * Convert node form directory to leaf form directory.
1753 * The root of the node form dir needs to already be a LEAFN block.
1754 * Just return if we can't do anything.
1756 int /* error */
1757 xfs_dir2_node_to_leaf(
1758 xfs_da_state_t *state) /* directory operation state */
1760 xfs_da_args_t *args; /* operation arguments */
1761 xfs_inode_t *dp; /* incore directory inode */
1762 int error; /* error return code */
1763 xfs_dabuf_t *fbp; /* buffer for freespace block */
1764 xfs_fileoff_t fo; /* freespace file offset */
1765 xfs_dir2_free_t *free; /* freespace structure */
1766 xfs_dabuf_t *lbp; /* buffer for leaf block */
1767 xfs_dir2_leaf_tail_t *ltp; /* tail of leaf structure */
1768 xfs_dir2_leaf_t *leaf; /* leaf structure */
1769 xfs_mount_t *mp; /* filesystem mount point */
1770 int rval; /* successful free trim? */
1771 xfs_trans_t *tp; /* transaction pointer */
1774 * There's more than a leaf level in the btree, so there must
1775 * be multiple leafn blocks. Give up.
1777 if (state->path.active > 1)
1778 return 0;
1779 args = state->args;
1780 xfs_dir2_trace_args("node_to_leaf", args);
1781 mp = state->mp;
1782 dp = args->dp;
1783 tp = args->trans;
1785 * Get the last offset in the file.
1787 if ((error = xfs_bmap_last_offset(tp, dp, &fo, XFS_DATA_FORK))) {
1788 return error;
1790 fo -= mp->m_dirblkfsbs;
1792 * If there are freespace blocks other than the first one,
1793 * take this opportunity to remove trailing empty freespace blocks
1794 * that may have been left behind during no-space-reservation
1795 * operations.
1797 while (fo > mp->m_dirfreeblk) {
1798 if ((error = xfs_dir2_node_trim_free(args, fo, &rval))) {
1799 return error;
1801 if (rval)
1802 fo -= mp->m_dirblkfsbs;
1803 else
1804 return 0;
1807 * Now find the block just before the freespace block.
1809 if ((error = xfs_bmap_last_before(tp, dp, &fo, XFS_DATA_FORK))) {
1810 return error;
1813 * If it's not the single leaf block, give up.
1815 if (XFS_FSB_TO_B(mp, fo) > XFS_DIR2_LEAF_OFFSET + mp->m_dirblksize)
1816 return 0;
1817 lbp = state->path.blk[0].bp;
1818 leaf = lbp->data;
1819 ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_DIR2_LEAFN_MAGIC);
1821 * Read the freespace block.
1823 if ((error = xfs_da_read_buf(tp, dp, mp->m_dirfreeblk, -1, &fbp,
1824 XFS_DATA_FORK))) {
1825 return error;
1827 free = fbp->data;
1828 ASSERT(be32_to_cpu(free->hdr.magic) == XFS_DIR2_FREE_MAGIC);
1829 ASSERT(!free->hdr.firstdb);
1831 * Now see if the leafn and free data will fit in a leaf1.
1832 * If not, release the buffer and give up.
1834 if ((uint)sizeof(leaf->hdr) +
1835 (be16_to_cpu(leaf->hdr.count) - be16_to_cpu(leaf->hdr.stale)) * (uint)sizeof(leaf->ents[0]) +
1836 be32_to_cpu(free->hdr.nvalid) * (uint)sizeof(leaf->bests[0]) +
1837 (uint)sizeof(leaf->tail) >
1838 mp->m_dirblksize) {
1839 xfs_da_brelse(tp, fbp);
1840 return 0;
1843 * If the leaf has any stale entries in it, compress them out.
1844 * The compact routine will log the header.
1846 if (be16_to_cpu(leaf->hdr.stale))
1847 xfs_dir2_leaf_compact(args, lbp);
1848 else
1849 xfs_dir2_leaf_log_header(tp, lbp);
1850 leaf->hdr.info.magic = cpu_to_be16(XFS_DIR2_LEAF1_MAGIC);
1852 * Set up the leaf tail from the freespace block.
1854 ltp = xfs_dir2_leaf_tail_p(mp, leaf);
1855 ltp->bestcount = free->hdr.nvalid;
1857 * Set up the leaf bests table.
1859 memcpy(xfs_dir2_leaf_bests_p(ltp), free->bests,
1860 be32_to_cpu(ltp->bestcount) * sizeof(leaf->bests[0]));
1861 xfs_dir2_leaf_log_bests(tp, lbp, 0, be32_to_cpu(ltp->bestcount) - 1);
1862 xfs_dir2_leaf_log_tail(tp, lbp);
1863 xfs_dir2_leaf_check(dp, lbp);
1865 * Get rid of the freespace block.
1867 error = xfs_dir2_shrink_inode(args, XFS_DIR2_FREE_FIRSTDB(mp), fbp);
1868 if (error) {
1870 * This can't fail here because it can only happen when
1871 * punching out the middle of an extent, and this is an
1872 * isolated block.
1874 ASSERT(error != ENOSPC);
1875 return error;
1877 fbp = NULL;
1879 * Now see if we can convert the single-leaf directory
1880 * down to a block form directory.
1881 * This routine always kills the dabuf for the leaf, so
1882 * eliminate it from the path.
1884 error = xfs_dir2_leaf_to_block(args, lbp, NULL);
1885 state->path.blk[0].bp = NULL;
1886 return error;