2 * Copyright (c) 2000-2003,2005 Silicon Graphics, Inc.
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation.
9 * This program is distributed in the hope that it would be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write the Free Software Foundation,
16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 #include "xfs_types.h"
24 #include "xfs_trans.h"
28 #include "xfs_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"
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.
48 static void xfs_dir2_leaf_check(xfs_inode_t
*dp
, xfs_dabuf_t
*bp
);
50 #define xfs_dir2_leaf_check(dp, bp)
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
,
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.
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
);
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
))) {
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
))) {
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);
123 * Make the space formerly occupied by the leaf entries and block
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
-
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
);
136 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.
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
);
158 * Add an entry to a leaf form directory.
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 xfs_dir2_trace_args("leaf_addname", args
);
197 * Read the leaf block.
199 error
= xfs_da_read_buf(tp
, dp
, mp
->m_dirleafblk
, -1, &lbp
,
206 * Look up the entry by hash value and name.
207 * We know it's not there, our caller has already done a lookup.
208 * So the index is of the entry to insert in front of.
209 * But if there are dup hash values the index is of the first of those.
211 index
= xfs_dir2_leaf_search_hash(args
, lbp
);
213 ltp
= XFS_DIR2_LEAF_TAIL_P(mp
, leaf
);
214 bestsp
= XFS_DIR2_LEAF_BESTS_P(ltp
);
215 length
= XFS_DIR2_DATA_ENTSIZE(args
->namelen
);
217 * See if there are any entries with the same hash value
218 * and space in their block for the new entry.
219 * This is good because it puts multiple same-hash value entries
220 * in a data block, improving the lookup of those entries.
222 for (use_block
= -1, lep
= &leaf
->ents
[index
];
223 index
< be16_to_cpu(leaf
->hdr
.count
) && be32_to_cpu(lep
->hashval
) == args
->hashval
;
225 if (be32_to_cpu(lep
->address
) == XFS_DIR2_NULL_DATAPTR
)
227 i
= XFS_DIR2_DATAPTR_TO_DB(mp
, be32_to_cpu(lep
->address
));
228 ASSERT(i
< be32_to_cpu(ltp
->bestcount
));
229 ASSERT(be16_to_cpu(bestsp
[i
]) != NULLDATAOFF
);
230 if (be16_to_cpu(bestsp
[i
]) >= length
) {
236 * Didn't find a block yet, linear search all the data blocks.
238 if (use_block
== -1) {
239 for (i
= 0; i
< be32_to_cpu(ltp
->bestcount
); i
++) {
241 * Remember a block we see that's missing.
243 if (be16_to_cpu(bestsp
[i
]) == NULLDATAOFF
&& use_block
== -1)
245 else if (be16_to_cpu(bestsp
[i
]) >= length
) {
252 * How many bytes do we need in the leaf block?
255 (leaf
->hdr
.stale
? 0 : (uint
)sizeof(leaf
->ents
[0])) +
256 (use_block
!= -1 ? 0 : (uint
)sizeof(leaf
->bests
[0]));
258 * Now kill use_block if it refers to a missing block, so we
259 * can use it as an indication of allocation needed.
261 if (use_block
!= -1 && be16_to_cpu(bestsp
[use_block
]) == NULLDATAOFF
)
264 * If we don't have enough free bytes but we can make enough
265 * by compacting out stale entries, we'll do that.
267 if ((char *)bestsp
- (char *)&leaf
->ents
[be16_to_cpu(leaf
->hdr
.count
)] < needbytes
&&
268 be16_to_cpu(leaf
->hdr
.stale
) > 1) {
272 * Otherwise if we don't have enough free bytes we need to
273 * convert to node form.
275 else if ((char *)bestsp
- (char *)&leaf
->ents
[be16_to_cpu(leaf
->hdr
.count
)] <
278 * Just checking or no space reservation, give up.
280 if (args
->justcheck
|| 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
);
292 * Then add the new entry.
294 return xfs_dir2_node_addname(args
);
297 * Otherwise it will fit without compaction.
302 * If just checking, then it will fit unless we needed to allocate
305 if (args
->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
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
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
);
336 * If there was no data block space found, we need to allocate
339 if (use_block
== -1) {
341 * Add the new data block.
343 if ((error
= xfs_dir2_grow_inode(args
, XFS_DIR2_DATA_SPACE
,
345 xfs_da_brelse(tp
, lbp
);
349 * Initialize the block.
351 if ((error
= xfs_dir2_data_init(args
, use_block
, &dbp
))) {
352 xfs_da_brelse(tp
, lbp
);
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
)) {
361 memmove(&bestsp
[0], &bestsp
[1],
362 be32_to_cpu(ltp
->bestcount
) * sizeof(bestsp
[0]));
363 be32_add(<p
->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.
371 xfs_dir2_leaf_log_bests(tp
, lbp
, use_block
, use_block
);
373 bestsp
[use_block
] = data
->hdr
.bestfree
[0].length
;
377 * Already had space in some data block.
378 * Just read that one in.
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
);
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.
417 xfs_dir2_data_freescan(mp
, data
, &needlog
, NULL
);
419 * Need to log the data block's header.
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
;
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.
448 lfloghigh
= be16_to_cpu(leaf
->hdr
.count
);
449 be16_add(&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.
459 * If we didn't compact before, we need to find the nearest
460 * stale entries before and after our insertion point.
464 * Find the first stale entry before the insertion
467 for (lowstale
= index
- 1;
469 be32_to_cpu(leaf
->ents
[lowstale
].address
) !=
470 XFS_DIR2_NULL_DATAPTR
;
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
&&
483 index
- lowstale
- 1 >= highstale
- index
);
488 * If the low one is better, use it.
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.
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],
522 (highstale
- index
) * sizeof(*lep
));
523 lep
= &leaf
->ents
[index
];
524 lfloglow
= MIN(index
, lfloglow
);
525 lfloghigh
= MAX(highstale
, lfloghigh
);
527 be16_add(&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
);
549 * Check the internal consistency of a leaf1 block.
550 * Pop an assert if something is wrong.
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 */
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
)
588 ASSERT(be16_to_cpu(leaf
->hdr
.stale
) == stale
);
593 * Compact out any stale entries in the leaf.
594 * Log the header and changed leaf entries, if any.
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 */
607 if (!leaf
->hdr
.stale
) {
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
)
617 * Only actually copy the entries that are different.
622 leaf
->ents
[to
] = leaf
->ents
[from
];
627 * Update and log the header, log the leaf entries.
629 ASSERT(be16_to_cpu(leaf
->hdr
.stale
) == from
- to
);
630 be16_add(&leaf
->hdr
.count
, -(be16_to_cpu(leaf
->hdr
.stale
)));
632 xfs_dir2_leaf_log_header(args
->trans
, bp
);
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
642 * Return new insertion index, where the remaining stale entry is,
643 * and leaf logging indices.
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 */
664 ASSERT(be16_to_cpu(leaf
->hdr
.stale
) > 1);
667 * Find the first stale entry before our index, if any.
669 for (lowstale
= index
- 1;
671 be32_to_cpu(leaf
->ents
[lowstale
].address
) != XFS_DIR2_NULL_DATAPTR
;
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
);
685 * Pick the better of lowstale and highstale.
688 (highstale
== be16_to_cpu(leaf
->hdr
.count
) ||
689 index
- lowstale
<= highstale
- index
))
690 keepstale
= lowstale
;
692 keepstale
= highstale
;
694 * Copy the entries in place, removing all the stale entries
697 for (from
= to
= 0; from
< be16_to_cpu(leaf
->hdr
.count
); from
++) {
699 * Notice the new value of index.
703 if (from
!= keepstale
&&
704 be32_to_cpu(leaf
->ents
[from
].address
) == XFS_DIR2_NULL_DATAPTR
) {
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.
718 leaf
->ents
[to
] = leaf
->ents
[from
];
723 * If the insertion point was past the last entry,
724 * set the new insertion point accordingly.
730 * Adjust the leaf header values.
732 be16_add(&leaf
->hdr
.count
, -(from
- to
));
733 leaf
->hdr
.stale
= cpu_to_be16(1);
735 * Remember the low/high stale value only in the "right"
738 if (lowstale
>= newindex
)
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.
752 xfs_dir2_leaf_getdents(
753 xfs_trans_t
*tp
, /* transaction pointer */
754 xfs_inode_t
*dp
, /* incore directory inode */
755 uio_t
*uio
, /* I/O control & vectors */
756 int *eofp
, /* out: reached end of dir */
757 xfs_dirent_t
*dbp
, /* caller's buffer */
758 xfs_dir2_put_t put
) /* ABI formatting routine */
760 xfs_dabuf_t
*bp
; /* data block buffer */
761 int byteoff
; /* offset in current block */
762 xfs_dir2_db_t curdb
; /* db for current block */
763 xfs_dir2_off_t curoff
; /* current overall offset */
764 xfs_dir2_data_t
*data
; /* data block structure */
765 xfs_dir2_data_entry_t
*dep
; /* data entry */
766 xfs_dir2_data_unused_t
*dup
; /* unused entry */
767 int eof
; /* reached end of directory */
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 xfs_dir2_put_args_t
*p
; /* formatting arg bundle */
781 char *ptr
= NULL
; /* pointer to current data */
782 int ra_current
; /* number of read-ahead blks */
783 int ra_index
; /* *map index for read-ahead */
784 int ra_offset
; /* map entry offset for ra */
785 int ra_want
; /* readahead count wanted */
788 * If the offset is at or past the largest allowed value,
789 * give up right away, return eof.
791 if (uio
->uio_offset
>= XFS_DIR2_MAX_DATAPTR
) {
797 * Setup formatting arguments.
799 p
= kmem_alloc(sizeof(*p
), KM_SLEEP
);
804 * Set up to bmap a number of blocks based on the caller's
805 * buffer size, the directory block size, and the filesystem
809 howmany(uio
->uio_resid
+ mp
->m_dirblksize
,
810 mp
->m_sb
.sb_blocksize
);
811 map
= kmem_alloc(map_size
* sizeof(*map
), KM_SLEEP
);
812 map_valid
= ra_index
= ra_offset
= ra_current
= map_blocks
= 0;
816 * Inside the loop we keep the main offset value as a byte offset
817 * in the directory file.
819 curoff
= XFS_DIR2_DATAPTR_TO_BYTE(mp
, uio
->uio_offset
);
821 * Force this conversion through db so we truncate the offset
822 * down to get the start of the data block.
824 map_off
= XFS_DIR2_DB_TO_DA(mp
, XFS_DIR2_BYTE_TO_DB(mp
, curoff
));
826 * Loop over directory entries until we reach the end offset.
827 * Get more blocks and readahead as necessary.
829 while (curoff
< XFS_DIR2_LEAF_OFFSET
) {
831 * If we have no buffer, or we're off the end of the
832 * current buffer, need to get another one.
834 if (!bp
|| ptr
>= (char *)bp
->data
+ mp
->m_dirblksize
) {
836 * If we have a buffer, we need to release it and
837 * take it out of the mapping.
840 xfs_da_brelse(tp
, bp
);
842 map_blocks
-= mp
->m_dirblkfsbs
;
844 * Loop to get rid of the extents for the
847 for (i
= mp
->m_dirblkfsbs
; i
> 0; ) {
848 j
= MIN((int)map
->br_blockcount
, i
);
849 map
->br_blockcount
-= j
;
850 map
->br_startblock
+= j
;
851 map
->br_startoff
+= j
;
853 * If mapping is done, pitch it from
856 if (!map
->br_blockcount
&& --map_valid
)
857 memmove(&map
[0], &map
[1],
864 * Recalculate the readahead blocks wanted.
866 ra_want
= howmany(uio
->uio_resid
+ mp
->m_dirblksize
,
867 mp
->m_sb
.sb_blocksize
) - 1;
869 * If we don't have as many as we want, and we haven't
870 * run out of data blocks, get some more mappings.
872 if (1 + ra_want
> map_blocks
&&
874 XFS_DIR2_BYTE_TO_DA(mp
, XFS_DIR2_LEAF_OFFSET
)) {
876 * Get more bmaps, fill in after the ones
877 * we already have in the table.
879 nmap
= map_size
- map_valid
;
880 error
= xfs_bmapi(tp
, dp
,
882 XFS_DIR2_BYTE_TO_DA(mp
,
883 XFS_DIR2_LEAF_OFFSET
) - map_off
,
884 XFS_BMAPI_METADATA
, NULL
, 0,
885 &map
[map_valid
], &nmap
, NULL
, NULL
);
887 * Don't know if we should ignore this or
888 * try to return an error.
889 * The trouble with returning errors
890 * is that readdir will just stop without
891 * actually passing the error through.
896 * If we got all the mappings we asked for,
897 * set the final map offset based on the
898 * last bmap value received.
899 * Otherwise, we've reached the end.
901 if (nmap
== map_size
- map_valid
)
903 map
[map_valid
+ nmap
- 1].br_startoff
+
904 map
[map_valid
+ nmap
- 1].br_blockcount
;
907 XFS_DIR2_BYTE_TO_DA(mp
,
908 XFS_DIR2_LEAF_OFFSET
);
910 * Look for holes in the mapping, and
911 * eliminate them. Count up the valid blocks.
913 for (i
= map_valid
; i
< map_valid
+ nmap
; ) {
914 if (map
[i
].br_startblock
==
917 length
= map_valid
+ nmap
- i
;
925 map
[i
].br_blockcount
;
932 * No valid mappings, so no more data blocks.
935 curoff
= XFS_DIR2_DA_TO_BYTE(mp
, map_off
);
939 * Read the directory block starting at the first
942 curdb
= XFS_DIR2_DA_TO_DB(mp
, map
->br_startoff
);
943 error
= xfs_da_read_buf(tp
, dp
, map
->br_startoff
,
944 map
->br_blockcount
>= mp
->m_dirblkfsbs
?
945 XFS_FSB_TO_DADDR(mp
, map
->br_startblock
) :
949 * Should just skip over the data block instead
955 * Adjust the current amount of read-ahead: we just
956 * read a block that was previously ra.
959 ra_current
-= mp
->m_dirblkfsbs
;
961 * Do we need more readahead?
963 for (ra_index
= ra_offset
= i
= 0;
964 ra_want
> ra_current
&& i
< map_blocks
;
965 i
+= mp
->m_dirblkfsbs
) {
966 ASSERT(ra_index
< map_valid
);
968 * Read-ahead a contiguous directory block.
970 if (i
> ra_current
&&
971 map
[ra_index
].br_blockcount
>=
973 xfs_baread(mp
->m_ddev_targp
,
975 map
[ra_index
].br_startblock
+
977 (int)BTOBB(mp
->m_dirblksize
));
981 * Read-ahead a non-contiguous directory block.
982 * This doesn't use our mapping, but this
983 * is a very rare case.
985 else if (i
> ra_current
) {
986 (void)xfs_da_reada_buf(tp
, dp
,
987 map
[ra_index
].br_startoff
+
988 ra_offset
, XFS_DATA_FORK
);
992 * Advance offset through the mapping table.
994 for (j
= 0; j
< mp
->m_dirblkfsbs
; j
++) {
996 * The rest of this extent but not
997 * more than a dir block.
999 length
= MIN(mp
->m_dirblkfsbs
,
1000 (int)(map
[ra_index
].br_blockcount
-
1003 ra_offset
+= length
;
1005 * Advance to the next mapping if
1006 * this one is used up.
1009 map
[ra_index
].br_blockcount
) {
1016 * Having done a read, we need to set a new offset.
1018 newoff
= XFS_DIR2_DB_OFF_TO_BYTE(mp
, curdb
, 0);
1020 * Start of the current block.
1022 if (curoff
< newoff
)
1025 * Make sure we're in the right block.
1027 else if (curoff
> newoff
)
1028 ASSERT(XFS_DIR2_BYTE_TO_DB(mp
, curoff
) ==
1031 xfs_dir2_data_check(dp
, bp
);
1033 * Find our position in the block.
1035 ptr
= (char *)&data
->u
;
1036 byteoff
= XFS_DIR2_BYTE_TO_OFF(mp
, curoff
);
1038 * Skip past the header.
1041 curoff
+= (uint
)sizeof(data
->hdr
);
1043 * Skip past entries until we reach our offset.
1046 while ((char *)ptr
- (char *)data
< byteoff
) {
1047 dup
= (xfs_dir2_data_unused_t
*)ptr
;
1049 if (be16_to_cpu(dup
->freetag
)
1050 == XFS_DIR2_DATA_FREE_TAG
) {
1052 length
= be16_to_cpu(dup
->length
);
1056 dep
= (xfs_dir2_data_entry_t
*)ptr
;
1058 XFS_DIR2_DATA_ENTSIZE(dep
->namelen
);
1062 * Now set our real offset.
1065 XFS_DIR2_DB_OFF_TO_BYTE(mp
,
1066 XFS_DIR2_BYTE_TO_DB(mp
, curoff
),
1067 (char *)ptr
- (char *)data
);
1068 if (ptr
>= (char *)data
+ mp
->m_dirblksize
) {
1074 * We have a pointer to an entry.
1077 dup
= (xfs_dir2_data_unused_t
*)ptr
;
1079 * No, it's unused, skip over it.
1081 if (be16_to_cpu(dup
->freetag
) == XFS_DIR2_DATA_FREE_TAG
) {
1082 length
= be16_to_cpu(dup
->length
);
1089 * Copy the entry into the putargs, and try formatting it.
1091 dep
= (xfs_dir2_data_entry_t
*)ptr
;
1093 p
->namelen
= dep
->namelen
;
1095 length
= XFS_DIR2_DATA_ENTSIZE(p
->namelen
);
1097 p
->cook
= XFS_DIR2_BYTE_TO_DATAPTR(mp
, curoff
+ length
);
1099 p
->ino
= be64_to_cpu(dep
->inumber
);
1101 p
->ino
+= mp
->m_inoadd
;
1103 p
->name
= (char *)dep
->name
;
1108 * Won't fit. Return to caller.
1115 * Advance to next entry in the block.
1122 * All done. Set output offset value to current offset.
1125 if (curoff
> XFS_DIR2_DATAPTR_TO_BYTE(mp
, XFS_DIR2_MAX_DATAPTR
))
1126 uio
->uio_offset
= XFS_DIR2_MAX_DATAPTR
;
1128 uio
->uio_offset
= XFS_DIR2_BYTE_TO_DATAPTR(mp
, curoff
);
1129 kmem_free(map
, map_size
* sizeof(*map
));
1130 kmem_free(p
, sizeof(*p
));
1132 xfs_da_brelse(tp
, bp
);
1137 * Initialize a new leaf block, leaf1 or leafn magic accepted.
1141 xfs_da_args_t
*args
, /* operation arguments */
1142 xfs_dir2_db_t bno
, /* directory block number */
1143 xfs_dabuf_t
**bpp
, /* out: leaf buffer */
1144 int magic
) /* magic number for block */
1146 xfs_dabuf_t
*bp
; /* leaf buffer */
1147 xfs_inode_t
*dp
; /* incore directory inode */
1148 int error
; /* error return code */
1149 xfs_dir2_leaf_t
*leaf
; /* leaf structure */
1150 xfs_dir2_leaf_tail_t
*ltp
; /* leaf tail structure */
1151 xfs_mount_t
*mp
; /* filesystem mount point */
1152 xfs_trans_t
*tp
; /* transaction pointer */
1158 ASSERT(bno
>= XFS_DIR2_LEAF_FIRSTDB(mp
) &&
1159 bno
< XFS_DIR2_FREE_FIRSTDB(mp
));
1161 * Get the buffer for the block.
1163 error
= xfs_da_get_buf(tp
, dp
, XFS_DIR2_DB_TO_DA(mp
, bno
), -1, &bp
,
1171 * Initialize the header.
1173 leaf
->hdr
.info
.magic
= cpu_to_be16(magic
);
1174 leaf
->hdr
.info
.forw
= 0;
1175 leaf
->hdr
.info
.back
= 0;
1176 leaf
->hdr
.count
= 0;
1177 leaf
->hdr
.stale
= 0;
1178 xfs_dir2_leaf_log_header(tp
, bp
);
1180 * If it's a leaf-format directory initialize the tail.
1181 * In this case our caller has the real bests table to copy into
1184 if (magic
== XFS_DIR2_LEAF1_MAGIC
) {
1185 ltp
= XFS_DIR2_LEAF_TAIL_P(mp
, leaf
);
1187 xfs_dir2_leaf_log_tail(tp
, bp
);
1194 * Log the bests entries indicated from a leaf1 block.
1197 xfs_dir2_leaf_log_bests(
1198 xfs_trans_t
*tp
, /* transaction pointer */
1199 xfs_dabuf_t
*bp
, /* leaf buffer */
1200 int first
, /* first entry to log */
1201 int last
) /* last entry to log */
1203 __be16
*firstb
; /* pointer to first entry */
1204 __be16
*lastb
; /* pointer to last entry */
1205 xfs_dir2_leaf_t
*leaf
; /* leaf structure */
1206 xfs_dir2_leaf_tail_t
*ltp
; /* leaf tail structure */
1209 ASSERT(be16_to_cpu(leaf
->hdr
.info
.magic
) == XFS_DIR2_LEAF1_MAGIC
);
1210 ltp
= XFS_DIR2_LEAF_TAIL_P(tp
->t_mountp
, leaf
);
1211 firstb
= XFS_DIR2_LEAF_BESTS_P(ltp
) + first
;
1212 lastb
= XFS_DIR2_LEAF_BESTS_P(ltp
) + last
;
1213 xfs_da_log_buf(tp
, bp
, (uint
)((char *)firstb
- (char *)leaf
),
1214 (uint
)((char *)lastb
- (char *)leaf
+ sizeof(*lastb
) - 1));
1218 * Log the leaf entries indicated from a leaf1 or leafn block.
1221 xfs_dir2_leaf_log_ents(
1222 xfs_trans_t
*tp
, /* transaction pointer */
1223 xfs_dabuf_t
*bp
, /* leaf buffer */
1224 int first
, /* first entry to log */
1225 int last
) /* last entry to log */
1227 xfs_dir2_leaf_entry_t
*firstlep
; /* pointer to first entry */
1228 xfs_dir2_leaf_entry_t
*lastlep
; /* pointer to last entry */
1229 xfs_dir2_leaf_t
*leaf
; /* leaf structure */
1232 ASSERT(be16_to_cpu(leaf
->hdr
.info
.magic
) == XFS_DIR2_LEAF1_MAGIC
||
1233 be16_to_cpu(leaf
->hdr
.info
.magic
) == XFS_DIR2_LEAFN_MAGIC
);
1234 firstlep
= &leaf
->ents
[first
];
1235 lastlep
= &leaf
->ents
[last
];
1236 xfs_da_log_buf(tp
, bp
, (uint
)((char *)firstlep
- (char *)leaf
),
1237 (uint
)((char *)lastlep
- (char *)leaf
+ sizeof(*lastlep
) - 1));
1241 * Log the header of the leaf1 or leafn block.
1244 xfs_dir2_leaf_log_header(
1245 xfs_trans_t
*tp
, /* transaction pointer */
1246 xfs_dabuf_t
*bp
) /* leaf buffer */
1248 xfs_dir2_leaf_t
*leaf
; /* leaf structure */
1251 ASSERT(be16_to_cpu(leaf
->hdr
.info
.magic
) == XFS_DIR2_LEAF1_MAGIC
||
1252 be16_to_cpu(leaf
->hdr
.info
.magic
) == XFS_DIR2_LEAFN_MAGIC
);
1253 xfs_da_log_buf(tp
, bp
, (uint
)((char *)&leaf
->hdr
- (char *)leaf
),
1254 (uint
)(sizeof(leaf
->hdr
) - 1));
1258 * Log the tail of the leaf1 block.
1261 xfs_dir2_leaf_log_tail(
1262 xfs_trans_t
*tp
, /* transaction pointer */
1263 xfs_dabuf_t
*bp
) /* leaf buffer */
1265 xfs_dir2_leaf_t
*leaf
; /* leaf structure */
1266 xfs_dir2_leaf_tail_t
*ltp
; /* leaf tail structure */
1267 xfs_mount_t
*mp
; /* filesystem mount point */
1271 ASSERT(be16_to_cpu(leaf
->hdr
.info
.magic
) == XFS_DIR2_LEAF1_MAGIC
);
1272 ltp
= XFS_DIR2_LEAF_TAIL_P(mp
, leaf
);
1273 xfs_da_log_buf(tp
, bp
, (uint
)((char *)ltp
- (char *)leaf
),
1274 (uint
)(mp
->m_dirblksize
- 1));
1278 * Look up the entry referred to by args in the leaf format directory.
1279 * Most of the work is done by the xfs_dir2_leaf_lookup_int routine which
1280 * is also used by the node-format code.
1283 xfs_dir2_leaf_lookup(
1284 xfs_da_args_t
*args
) /* operation arguments */
1286 xfs_dabuf_t
*dbp
; /* data block buffer */
1287 xfs_dir2_data_entry_t
*dep
; /* data block entry */
1288 xfs_inode_t
*dp
; /* incore directory inode */
1289 int error
; /* error return code */
1290 int index
; /* found entry index */
1291 xfs_dabuf_t
*lbp
; /* leaf buffer */
1292 xfs_dir2_leaf_t
*leaf
; /* leaf structure */
1293 xfs_dir2_leaf_entry_t
*lep
; /* leaf entry */
1294 xfs_trans_t
*tp
; /* transaction pointer */
1296 xfs_dir2_trace_args("leaf_lookup", args
);
1298 * Look up name in the leaf block, returning both buffers and index.
1300 if ((error
= xfs_dir2_leaf_lookup_int(args
, &lbp
, &index
, &dbp
))) {
1305 xfs_dir2_leaf_check(dp
, lbp
);
1308 * Get to the leaf entry and contained data entry address.
1310 lep
= &leaf
->ents
[index
];
1312 * Point to the data entry.
1314 dep
= (xfs_dir2_data_entry_t
*)
1315 ((char *)dbp
->data
+
1316 XFS_DIR2_DATAPTR_TO_OFF(dp
->i_mount
, be32_to_cpu(lep
->address
)));
1318 * Return the found inode number.
1320 args
->inumber
= be64_to_cpu(dep
->inumber
);
1321 xfs_da_brelse(tp
, dbp
);
1322 xfs_da_brelse(tp
, lbp
);
1323 return XFS_ERROR(EEXIST
);
1327 * Look up name/hash in the leaf block.
1328 * Fill in indexp with the found index, and dbpp with the data buffer.
1329 * If not found dbpp will be NULL, and ENOENT comes back.
1330 * lbpp will always be filled in with the leaf buffer unless there's an error.
1332 static int /* error */
1333 xfs_dir2_leaf_lookup_int(
1334 xfs_da_args_t
*args
, /* operation arguments */
1335 xfs_dabuf_t
**lbpp
, /* out: leaf buffer */
1336 int *indexp
, /* out: index in leaf block */
1337 xfs_dabuf_t
**dbpp
) /* out: data buffer */
1339 xfs_dir2_db_t curdb
; /* current data block number */
1340 xfs_dabuf_t
*dbp
; /* data buffer */
1341 xfs_dir2_data_entry_t
*dep
; /* data entry */
1342 xfs_inode_t
*dp
; /* incore directory inode */
1343 int error
; /* error return code */
1344 int index
; /* index in leaf block */
1345 xfs_dabuf_t
*lbp
; /* leaf buffer */
1346 xfs_dir2_leaf_entry_t
*lep
; /* leaf entry */
1347 xfs_dir2_leaf_t
*leaf
; /* leaf structure */
1348 xfs_mount_t
*mp
; /* filesystem mount point */
1349 xfs_dir2_db_t newdb
; /* new data block number */
1350 xfs_trans_t
*tp
; /* transaction pointer */
1356 * Read the leaf block into the buffer.
1359 xfs_da_read_buf(tp
, dp
, mp
->m_dirleafblk
, -1, &lbp
,
1365 xfs_dir2_leaf_check(dp
, lbp
);
1367 * Look for the first leaf entry with our hash value.
1369 index
= xfs_dir2_leaf_search_hash(args
, lbp
);
1371 * Loop over all the entries with the right hash value
1372 * looking to match the name.
1374 for (lep
= &leaf
->ents
[index
], dbp
= NULL
, curdb
= -1;
1375 index
< be16_to_cpu(leaf
->hdr
.count
) && be32_to_cpu(lep
->hashval
) == args
->hashval
;
1378 * Skip over stale leaf entries.
1380 if (be32_to_cpu(lep
->address
) == XFS_DIR2_NULL_DATAPTR
)
1383 * Get the new data block number.
1385 newdb
= XFS_DIR2_DATAPTR_TO_DB(mp
, be32_to_cpu(lep
->address
));
1387 * If it's not the same as the old data block number,
1388 * need to pitch the old one and read the new one.
1390 if (newdb
!= curdb
) {
1392 xfs_da_brelse(tp
, dbp
);
1394 xfs_da_read_buf(tp
, dp
,
1395 XFS_DIR2_DB_TO_DA(mp
, newdb
), -1, &dbp
,
1397 xfs_da_brelse(tp
, lbp
);
1400 xfs_dir2_data_check(dp
, dbp
);
1404 * Point to the data entry.
1406 dep
= (xfs_dir2_data_entry_t
*)
1407 ((char *)dbp
->data
+
1408 XFS_DIR2_DATAPTR_TO_OFF(mp
, be32_to_cpu(lep
->address
)));
1410 * If it matches then return it.
1412 if (dep
->namelen
== args
->namelen
&&
1413 dep
->name
[0] == args
->name
[0] &&
1414 memcmp(dep
->name
, args
->name
, args
->namelen
) == 0) {
1421 * No match found, return ENOENT.
1423 ASSERT(args
->oknoent
);
1425 xfs_da_brelse(tp
, dbp
);
1426 xfs_da_brelse(tp
, lbp
);
1427 return XFS_ERROR(ENOENT
);
1431 * Remove an entry from a leaf format directory.
1434 xfs_dir2_leaf_removename(
1435 xfs_da_args_t
*args
) /* operation arguments */
1437 __be16
*bestsp
; /* leaf block best freespace */
1438 xfs_dir2_data_t
*data
; /* data block structure */
1439 xfs_dir2_db_t db
; /* data block number */
1440 xfs_dabuf_t
*dbp
; /* data block buffer */
1441 xfs_dir2_data_entry_t
*dep
; /* data entry structure */
1442 xfs_inode_t
*dp
; /* incore directory inode */
1443 int error
; /* error return code */
1444 xfs_dir2_db_t i
; /* temporary data block # */
1445 int index
; /* index into leaf entries */
1446 xfs_dabuf_t
*lbp
; /* leaf buffer */
1447 xfs_dir2_leaf_t
*leaf
; /* leaf structure */
1448 xfs_dir2_leaf_entry_t
*lep
; /* leaf entry */
1449 xfs_dir2_leaf_tail_t
*ltp
; /* leaf tail structure */
1450 xfs_mount_t
*mp
; /* filesystem mount point */
1451 int needlog
; /* need to log data header */
1452 int needscan
; /* need to rescan data frees */
1453 xfs_dir2_data_off_t oldbest
; /* old value of best free */
1454 xfs_trans_t
*tp
; /* transaction pointer */
1456 xfs_dir2_trace_args("leaf_removename", args
);
1458 * Lookup the leaf entry, get the leaf and data blocks read in.
1460 if ((error
= xfs_dir2_leaf_lookup_int(args
, &lbp
, &index
, &dbp
))) {
1468 xfs_dir2_data_check(dp
, dbp
);
1470 * Point to the leaf entry, use that to point to the data entry.
1472 lep
= &leaf
->ents
[index
];
1473 db
= XFS_DIR2_DATAPTR_TO_DB(mp
, be32_to_cpu(lep
->address
));
1474 dep
= (xfs_dir2_data_entry_t
*)
1475 ((char *)data
+ XFS_DIR2_DATAPTR_TO_OFF(mp
, be32_to_cpu(lep
->address
)));
1476 needscan
= needlog
= 0;
1477 oldbest
= be16_to_cpu(data
->hdr
.bestfree
[0].length
);
1478 ltp
= XFS_DIR2_LEAF_TAIL_P(mp
, leaf
);
1479 bestsp
= XFS_DIR2_LEAF_BESTS_P(ltp
);
1480 ASSERT(be16_to_cpu(bestsp
[db
]) == oldbest
);
1482 * Mark the former data entry unused.
1484 xfs_dir2_data_make_free(tp
, dbp
,
1485 (xfs_dir2_data_aoff_t
)((char *)dep
- (char *)data
),
1486 XFS_DIR2_DATA_ENTSIZE(dep
->namelen
), &needlog
, &needscan
);
1488 * We just mark the leaf entry stale by putting a null in it.
1490 be16_add(&leaf
->hdr
.stale
, 1);
1491 xfs_dir2_leaf_log_header(tp
, lbp
);
1492 lep
->address
= cpu_to_be32(XFS_DIR2_NULL_DATAPTR
);
1493 xfs_dir2_leaf_log_ents(tp
, lbp
, index
, index
);
1495 * Scan the freespace in the data block again if necessary,
1496 * log the data block header if necessary.
1499 xfs_dir2_data_freescan(mp
, data
, &needlog
, NULL
);
1501 xfs_dir2_data_log_header(tp
, dbp
);
1503 * If the longest freespace in the data block has changed,
1504 * put the new value in the bests table and log that.
1506 if (be16_to_cpu(data
->hdr
.bestfree
[0].length
) != oldbest
) {
1507 bestsp
[db
] = data
->hdr
.bestfree
[0].length
;
1508 xfs_dir2_leaf_log_bests(tp
, lbp
, db
, db
);
1510 xfs_dir2_data_check(dp
, dbp
);
1512 * If the data block is now empty then get rid of the data block.
1514 if (be16_to_cpu(data
->hdr
.bestfree
[0].length
) ==
1515 mp
->m_dirblksize
- (uint
)sizeof(data
->hdr
)) {
1516 ASSERT(db
!= mp
->m_dirdatablk
);
1517 if ((error
= xfs_dir2_shrink_inode(args
, db
, dbp
))) {
1519 * Nope, can't get rid of it because it caused
1520 * allocation of a bmap btree block to do so.
1521 * Just go on, returning success, leaving the
1522 * empty block in place.
1524 if (error
== ENOSPC
&& args
->total
== 0) {
1525 xfs_da_buf_done(dbp
);
1528 xfs_dir2_leaf_check(dp
, lbp
);
1529 xfs_da_buf_done(lbp
);
1534 * If this is the last data block then compact the
1535 * bests table by getting rid of entries.
1537 if (db
== be32_to_cpu(ltp
->bestcount
) - 1) {
1539 * Look for the last active entry (i).
1541 for (i
= db
- 1; i
> 0; i
--) {
1542 if (be16_to_cpu(bestsp
[i
]) != NULLDATAOFF
)
1546 * Copy the table down so inactive entries at the
1549 memmove(&bestsp
[db
- i
], bestsp
,
1550 (be32_to_cpu(ltp
->bestcount
) - (db
- i
)) * sizeof(*bestsp
));
1551 be32_add(<p
->bestcount
, -(db
- i
));
1552 xfs_dir2_leaf_log_tail(tp
, lbp
);
1553 xfs_dir2_leaf_log_bests(tp
, lbp
, 0, be32_to_cpu(ltp
->bestcount
) - 1);
1555 bestsp
[db
] = cpu_to_be16(NULLDATAOFF
);
1558 * If the data block was not the first one, drop it.
1560 else if (db
!= mp
->m_dirdatablk
&& dbp
!= NULL
) {
1561 xfs_da_buf_done(dbp
);
1564 xfs_dir2_leaf_check(dp
, lbp
);
1566 * See if we can convert to block form.
1568 return xfs_dir2_leaf_to_block(args
, lbp
, dbp
);
1572 * Replace the inode number in a leaf format directory entry.
1575 xfs_dir2_leaf_replace(
1576 xfs_da_args_t
*args
) /* operation arguments */
1578 xfs_dabuf_t
*dbp
; /* data block buffer */
1579 xfs_dir2_data_entry_t
*dep
; /* data block entry */
1580 xfs_inode_t
*dp
; /* incore directory inode */
1581 int error
; /* error return code */
1582 int index
; /* index of leaf entry */
1583 xfs_dabuf_t
*lbp
; /* leaf buffer */
1584 xfs_dir2_leaf_t
*leaf
; /* leaf structure */
1585 xfs_dir2_leaf_entry_t
*lep
; /* leaf entry */
1586 xfs_trans_t
*tp
; /* transaction pointer */
1588 xfs_dir2_trace_args("leaf_replace", args
);
1590 * Look up the entry.
1592 if ((error
= xfs_dir2_leaf_lookup_int(args
, &lbp
, &index
, &dbp
))) {
1598 * Point to the leaf entry, get data address from it.
1600 lep
= &leaf
->ents
[index
];
1602 * Point to the data entry.
1604 dep
= (xfs_dir2_data_entry_t
*)
1605 ((char *)dbp
->data
+
1606 XFS_DIR2_DATAPTR_TO_OFF(dp
->i_mount
, be32_to_cpu(lep
->address
)));
1607 ASSERT(args
->inumber
!= be64_to_cpu(dep
->inumber
));
1609 * Put the new inode number in, log it.
1611 dep
->inumber
= cpu_to_be64(args
->inumber
);
1613 xfs_dir2_data_log_entry(tp
, dbp
, dep
);
1614 xfs_da_buf_done(dbp
);
1615 xfs_dir2_leaf_check(dp
, lbp
);
1616 xfs_da_brelse(tp
, lbp
);
1621 * Return index in the leaf block (lbp) which is either the first
1622 * one with this hash value, or if there are none, the insert point
1623 * for that hash value.
1625 int /* index value */
1626 xfs_dir2_leaf_search_hash(
1627 xfs_da_args_t
*args
, /* operation arguments */
1628 xfs_dabuf_t
*lbp
) /* leaf buffer */
1630 xfs_dahash_t hash
=0; /* hash from this entry */
1631 xfs_dahash_t hashwant
; /* hash value looking for */
1632 int high
; /* high leaf index */
1633 int low
; /* low leaf index */
1634 xfs_dir2_leaf_t
*leaf
; /* leaf structure */
1635 xfs_dir2_leaf_entry_t
*lep
; /* leaf entry */
1636 int mid
=0; /* current leaf index */
1640 if (!leaf
->hdr
.count
)
1644 * Note, the table cannot be empty, so we have to go through the loop.
1645 * Binary search the leaf entries looking for our hash value.
1647 for (lep
= leaf
->ents
, low
= 0, high
= be16_to_cpu(leaf
->hdr
.count
) - 1,
1648 hashwant
= args
->hashval
;
1650 mid
= (low
+ high
) >> 1;
1651 if ((hash
= be32_to_cpu(lep
[mid
].hashval
)) == hashwant
)
1653 if (hash
< hashwant
)
1659 * Found one, back up through all the equal hash values.
1661 if (hash
== hashwant
) {
1662 while (mid
> 0 && be32_to_cpu(lep
[mid
- 1].hashval
) == hashwant
) {
1667 * Need to point to an entry higher than ours.
1669 else if (hash
< hashwant
)
1675 * Trim off a trailing data block. We know it's empty since the leaf
1676 * freespace table says so.
1679 xfs_dir2_leaf_trim_data(
1680 xfs_da_args_t
*args
, /* operation arguments */
1681 xfs_dabuf_t
*lbp
, /* leaf buffer */
1682 xfs_dir2_db_t db
) /* data block number */
1684 __be16
*bestsp
; /* leaf bests table */
1686 xfs_dir2_data_t
*data
; /* data block structure */
1688 xfs_dabuf_t
*dbp
; /* data block buffer */
1689 xfs_inode_t
*dp
; /* incore directory inode */
1690 int error
; /* error return value */
1691 xfs_dir2_leaf_t
*leaf
; /* leaf structure */
1692 xfs_dir2_leaf_tail_t
*ltp
; /* leaf tail structure */
1693 xfs_mount_t
*mp
; /* filesystem mount point */
1694 xfs_trans_t
*tp
; /* transaction pointer */
1700 * Read the offending data block. We need its buffer.
1702 if ((error
= xfs_da_read_buf(tp
, dp
, XFS_DIR2_DB_TO_DA(mp
, db
), -1, &dbp
,
1708 ASSERT(be32_to_cpu(data
->hdr
.magic
) == XFS_DIR2_DATA_MAGIC
);
1710 /* this seems to be an error
1711 * data is only valid if DEBUG is defined?
1716 ltp
= XFS_DIR2_LEAF_TAIL_P(mp
, leaf
);
1717 ASSERT(be16_to_cpu(data
->hdr
.bestfree
[0].length
) ==
1718 mp
->m_dirblksize
- (uint
)sizeof(data
->hdr
));
1719 ASSERT(db
== be32_to_cpu(ltp
->bestcount
) - 1);
1721 * Get rid of the data block.
1723 if ((error
= xfs_dir2_shrink_inode(args
, db
, dbp
))) {
1724 ASSERT(error
!= ENOSPC
);
1725 xfs_da_brelse(tp
, dbp
);
1729 * Eliminate the last bests entry from the table.
1731 bestsp
= XFS_DIR2_LEAF_BESTS_P(ltp
);
1732 be32_add(<p
->bestcount
, -1);
1733 memmove(&bestsp
[1], &bestsp
[0], be32_to_cpu(ltp
->bestcount
) * sizeof(*bestsp
));
1734 xfs_dir2_leaf_log_tail(tp
, lbp
);
1735 xfs_dir2_leaf_log_bests(tp
, lbp
, 0, be32_to_cpu(ltp
->bestcount
) - 1);
1740 * Convert node form directory to leaf form directory.
1741 * The root of the node form dir needs to already be a LEAFN block.
1742 * Just return if we can't do anything.
1745 xfs_dir2_node_to_leaf(
1746 xfs_da_state_t
*state
) /* directory operation state */
1748 xfs_da_args_t
*args
; /* operation arguments */
1749 xfs_inode_t
*dp
; /* incore directory inode */
1750 int error
; /* error return code */
1751 xfs_dabuf_t
*fbp
; /* buffer for freespace block */
1752 xfs_fileoff_t fo
; /* freespace file offset */
1753 xfs_dir2_free_t
*free
; /* freespace structure */
1754 xfs_dabuf_t
*lbp
; /* buffer for leaf block */
1755 xfs_dir2_leaf_tail_t
*ltp
; /* tail of leaf structure */
1756 xfs_dir2_leaf_t
*leaf
; /* leaf structure */
1757 xfs_mount_t
*mp
; /* filesystem mount point */
1758 int rval
; /* successful free trim? */
1759 xfs_trans_t
*tp
; /* transaction pointer */
1762 * There's more than a leaf level in the btree, so there must
1763 * be multiple leafn blocks. Give up.
1765 if (state
->path
.active
> 1)
1768 xfs_dir2_trace_args("node_to_leaf", args
);
1773 * Get the last offset in the file.
1775 if ((error
= xfs_bmap_last_offset(tp
, dp
, &fo
, XFS_DATA_FORK
))) {
1778 fo
-= mp
->m_dirblkfsbs
;
1780 * If there are freespace blocks other than the first one,
1781 * take this opportunity to remove trailing empty freespace blocks
1782 * that may have been left behind during no-space-reservation
1785 while (fo
> mp
->m_dirfreeblk
) {
1786 if ((error
= xfs_dir2_node_trim_free(args
, fo
, &rval
))) {
1790 fo
-= mp
->m_dirblkfsbs
;
1795 * Now find the block just before the freespace block.
1797 if ((error
= xfs_bmap_last_before(tp
, dp
, &fo
, XFS_DATA_FORK
))) {
1801 * If it's not the single leaf block, give up.
1803 if (XFS_FSB_TO_B(mp
, fo
) > XFS_DIR2_LEAF_OFFSET
+ mp
->m_dirblksize
)
1805 lbp
= state
->path
.blk
[0].bp
;
1807 ASSERT(be16_to_cpu(leaf
->hdr
.info
.magic
) == XFS_DIR2_LEAFN_MAGIC
);
1809 * Read the freespace block.
1811 if ((error
= xfs_da_read_buf(tp
, dp
, mp
->m_dirfreeblk
, -1, &fbp
,
1816 ASSERT(be32_to_cpu(free
->hdr
.magic
) == XFS_DIR2_FREE_MAGIC
);
1817 ASSERT(!free
->hdr
.firstdb
);
1819 * Now see if the leafn and free data will fit in a leaf1.
1820 * If not, release the buffer and give up.
1822 if ((uint
)sizeof(leaf
->hdr
) +
1823 (be16_to_cpu(leaf
->hdr
.count
) - be16_to_cpu(leaf
->hdr
.stale
)) * (uint
)sizeof(leaf
->ents
[0]) +
1824 be32_to_cpu(free
->hdr
.nvalid
) * (uint
)sizeof(leaf
->bests
[0]) +
1825 (uint
)sizeof(leaf
->tail
) >
1827 xfs_da_brelse(tp
, fbp
);
1831 * If the leaf has any stale entries in it, compress them out.
1832 * The compact routine will log the header.
1834 if (be16_to_cpu(leaf
->hdr
.stale
))
1835 xfs_dir2_leaf_compact(args
, lbp
);
1837 xfs_dir2_leaf_log_header(tp
, lbp
);
1838 leaf
->hdr
.info
.magic
= cpu_to_be16(XFS_DIR2_LEAF1_MAGIC
);
1840 * Set up the leaf tail from the freespace block.
1842 ltp
= XFS_DIR2_LEAF_TAIL_P(mp
, leaf
);
1843 ltp
->bestcount
= free
->hdr
.nvalid
;
1845 * Set up the leaf bests table.
1847 memcpy(XFS_DIR2_LEAF_BESTS_P(ltp
), free
->bests
,
1848 be32_to_cpu(ltp
->bestcount
) * sizeof(leaf
->bests
[0]));
1849 xfs_dir2_leaf_log_bests(tp
, lbp
, 0, be32_to_cpu(ltp
->bestcount
) - 1);
1850 xfs_dir2_leaf_log_tail(tp
, lbp
);
1851 xfs_dir2_leaf_check(dp
, lbp
);
1853 * Get rid of the freespace block.
1855 error
= xfs_dir2_shrink_inode(args
, XFS_DIR2_FREE_FIRSTDB(mp
), fbp
);
1858 * This can't fail here because it can only happen when
1859 * punching out the middle of an extent, and this is an
1862 ASSERT(error
!= ENOSPC
);
1867 * Now see if we can convert the single-leaf directory
1868 * down to a block form directory.
1869 * This routine always kills the dabuf for the leaf, so
1870 * eliminate it from the path.
1872 error
= xfs_dir2_leaf_to_block(args
, lbp
, NULL
);
1873 state
->path
.blk
[0].bp
= NULL
;