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"
29 #include "xfs_dmapi.h"
30 #include "xfs_mount.h"
31 #include "xfs_da_btree.h"
32 #include "xfs_bmap_btree.h"
33 #include "xfs_attr_sf.h"
34 #include "xfs_dir_sf.h"
35 #include "xfs_dir2_sf.h"
36 #include "xfs_dinode.h"
37 #include "xfs_inode.h"
39 #include "xfs_dir2_data.h"
40 #include "xfs_dir2_leaf.h"
41 #include "xfs_dir2_block.h"
42 #include "xfs_dir2_node.h"
43 #include "xfs_dir2_trace.h"
44 #include "xfs_error.h"
47 * Local function declarations.
50 static void xfs_dir2_leaf_check(xfs_inode_t
*dp
, xfs_dabuf_t
*bp
);
52 #define xfs_dir2_leaf_check(dp, bp)
54 static int xfs_dir2_leaf_lookup_int(xfs_da_args_t
*args
, xfs_dabuf_t
**lbpp
,
55 int *indexp
, xfs_dabuf_t
**dbpp
);
56 static void xfs_dir2_leaf_log_bests(struct xfs_trans
*tp
, struct xfs_dabuf
*bp
,
58 static void xfs_dir2_leaf_log_tail(struct xfs_trans
*tp
, struct xfs_dabuf
*bp
);
62 * Convert a block form directory to a leaf form directory.
65 xfs_dir2_block_to_leaf(
66 xfs_da_args_t
*args
, /* operation arguments */
67 xfs_dabuf_t
*dbp
) /* input block's buffer */
69 xfs_dir2_data_off_t
*bestsp
; /* leaf's bestsp entries */
70 xfs_dablk_t blkno
; /* leaf block's bno */
71 xfs_dir2_block_t
*block
; /* block structure */
72 xfs_dir2_leaf_entry_t
*blp
; /* block's leaf entries */
73 xfs_dir2_block_tail_t
*btp
; /* block's tail */
74 xfs_inode_t
*dp
; /* incore directory inode */
75 int error
; /* error return code */
76 xfs_dabuf_t
*lbp
; /* leaf block's buffer */
77 xfs_dir2_db_t ldb
; /* leaf block's bno */
78 xfs_dir2_leaf_t
*leaf
; /* leaf structure */
79 xfs_dir2_leaf_tail_t
*ltp
; /* leaf's tail */
80 xfs_mount_t
*mp
; /* filesystem mount point */
81 int needlog
; /* need to log block header */
82 int needscan
; /* need to rescan bestfree */
83 xfs_trans_t
*tp
; /* transaction pointer */
85 xfs_dir2_trace_args_b("block_to_leaf", args
, dbp
);
90 * Add the leaf block to the inode.
91 * This interface will only put blocks in the leaf/node range.
92 * Since that's empty now, we'll get the root (block 0 in range).
94 if ((error
= xfs_da_grow_inode(args
, &blkno
))) {
97 ldb
= XFS_DIR2_DA_TO_DB(mp
, blkno
);
98 ASSERT(ldb
== XFS_DIR2_LEAF_FIRSTDB(mp
));
100 * Initialize the leaf block, get a buffer for it.
102 if ((error
= xfs_dir2_leaf_init(args
, ldb
, &lbp
, XFS_DIR2_LEAF1_MAGIC
))) {
108 xfs_dir2_data_check(dp
, dbp
);
109 btp
= XFS_DIR2_BLOCK_TAIL_P(mp
, block
);
110 blp
= XFS_DIR2_BLOCK_LEAF_P(btp
);
112 * Set the counts in the leaf header.
114 INT_COPY(leaf
->hdr
.count
, btp
->count
, ARCH_CONVERT
); /* INT_: type change */
115 INT_COPY(leaf
->hdr
.stale
, btp
->stale
, ARCH_CONVERT
); /* INT_: type change */
117 * Could compact these but I think we always do the conversion
118 * after squeezing out stale entries.
120 memcpy(leaf
->ents
, blp
, INT_GET(btp
->count
, ARCH_CONVERT
) * sizeof(xfs_dir2_leaf_entry_t
));
121 xfs_dir2_leaf_log_ents(tp
, lbp
, 0, INT_GET(leaf
->hdr
.count
, ARCH_CONVERT
) - 1);
125 * Make the space formerly occupied by the leaf entries and block
128 xfs_dir2_data_make_free(tp
, dbp
,
129 (xfs_dir2_data_aoff_t
)((char *)blp
- (char *)block
),
130 (xfs_dir2_data_aoff_t
)((char *)block
+ mp
->m_dirblksize
-
132 &needlog
, &needscan
);
134 * Fix up the block header, make it a data block.
136 INT_SET(block
->hdr
.magic
, ARCH_CONVERT
, XFS_DIR2_DATA_MAGIC
);
138 xfs_dir2_data_freescan(mp
, (xfs_dir2_data_t
*)block
, &needlog
,
141 * Set up leaf tail and bests table.
143 ltp
= XFS_DIR2_LEAF_TAIL_P(mp
, leaf
);
144 INT_SET(ltp
->bestcount
, ARCH_CONVERT
, 1);
145 bestsp
= XFS_DIR2_LEAF_BESTS_P(ltp
);
146 INT_COPY(bestsp
[0], block
->hdr
.bestfree
[0].length
, ARCH_CONVERT
);
148 * Log the data header and leaf bests table.
151 xfs_dir2_data_log_header(tp
, dbp
);
152 xfs_dir2_leaf_check(dp
, lbp
);
153 xfs_dir2_data_check(dp
, dbp
);
154 xfs_dir2_leaf_log_bests(tp
, lbp
, 0, 0);
155 xfs_da_buf_done(lbp
);
160 * Add an entry to a leaf form directory.
163 xfs_dir2_leaf_addname(
164 xfs_da_args_t
*args
) /* operation arguments */
166 xfs_dir2_data_off_t
*bestsp
; /* freespace table in leaf */
167 int compact
; /* need to compact leaves */
168 xfs_dir2_data_t
*data
; /* data block structure */
169 xfs_dabuf_t
*dbp
; /* data block buffer */
170 xfs_dir2_data_entry_t
*dep
; /* data block entry */
171 xfs_inode_t
*dp
; /* incore directory inode */
172 xfs_dir2_data_unused_t
*dup
; /* data unused entry */
173 int error
; /* error return value */
174 int grown
; /* allocated new data block */
175 int highstale
; /* index of next stale leaf */
176 int i
; /* temporary, index */
177 int index
; /* leaf table position */
178 xfs_dabuf_t
*lbp
; /* leaf's buffer */
179 xfs_dir2_leaf_t
*leaf
; /* leaf structure */
180 int length
; /* length of new entry */
181 xfs_dir2_leaf_entry_t
*lep
; /* leaf entry table pointer */
182 int lfloglow
; /* low leaf logging index */
183 int lfloghigh
; /* high leaf logging index */
184 int lowstale
; /* index of prev stale leaf */
185 xfs_dir2_leaf_tail_t
*ltp
; /* leaf tail pointer */
186 xfs_mount_t
*mp
; /* filesystem mount point */
187 int needbytes
; /* leaf block bytes needed */
188 int needlog
; /* need to log data header */
189 int needscan
; /* need to rescan data free */
190 xfs_dir2_data_off_t
*tagp
; /* end of data entry */
191 xfs_trans_t
*tp
; /* transaction pointer */
192 xfs_dir2_db_t use_block
; /* data block number */
194 xfs_dir2_trace_args("leaf_addname", args
);
199 * Read the leaf block.
201 error
= xfs_da_read_buf(tp
, dp
, mp
->m_dirleafblk
, -1, &lbp
,
208 * Look up the entry by hash value and name.
209 * We know it's not there, our caller has already done a lookup.
210 * So the index is of the entry to insert in front of.
211 * But if there are dup hash values the index is of the first of those.
213 index
= xfs_dir2_leaf_search_hash(args
, lbp
);
215 ltp
= XFS_DIR2_LEAF_TAIL_P(mp
, leaf
);
216 bestsp
= XFS_DIR2_LEAF_BESTS_P(ltp
);
217 length
= XFS_DIR2_DATA_ENTSIZE(args
->namelen
);
219 * See if there are any entries with the same hash value
220 * and space in their block for the new entry.
221 * This is good because it puts multiple same-hash value entries
222 * in a data block, improving the lookup of those entries.
224 for (use_block
= -1, lep
= &leaf
->ents
[index
];
225 index
< INT_GET(leaf
->hdr
.count
, ARCH_CONVERT
) && INT_GET(lep
->hashval
, ARCH_CONVERT
) == args
->hashval
;
227 if (INT_GET(lep
->address
, ARCH_CONVERT
) == XFS_DIR2_NULL_DATAPTR
)
229 i
= XFS_DIR2_DATAPTR_TO_DB(mp
, INT_GET(lep
->address
, ARCH_CONVERT
));
230 ASSERT(i
< INT_GET(ltp
->bestcount
, ARCH_CONVERT
));
231 ASSERT(INT_GET(bestsp
[i
], ARCH_CONVERT
) != NULLDATAOFF
);
232 if (INT_GET(bestsp
[i
], ARCH_CONVERT
) >= length
) {
238 * Didn't find a block yet, linear search all the data blocks.
240 if (use_block
== -1) {
241 for (i
= 0; i
< INT_GET(ltp
->bestcount
, ARCH_CONVERT
); i
++) {
243 * Remember a block we see that's missing.
245 if (INT_GET(bestsp
[i
], ARCH_CONVERT
) == NULLDATAOFF
&& use_block
== -1)
247 else if (INT_GET(bestsp
[i
], ARCH_CONVERT
) >= length
) {
254 * How many bytes do we need in the leaf block?
257 (leaf
->hdr
.stale
? 0 : (uint
)sizeof(leaf
->ents
[0])) +
258 (use_block
!= -1 ? 0 : (uint
)sizeof(leaf
->bests
[0]));
260 * Now kill use_block if it refers to a missing block, so we
261 * can use it as an indication of allocation needed.
263 if (use_block
!= -1 && INT_GET(bestsp
[use_block
], ARCH_CONVERT
) == NULLDATAOFF
)
266 * If we don't have enough free bytes but we can make enough
267 * by compacting out stale entries, we'll do that.
269 if ((char *)bestsp
- (char *)&leaf
->ents
[INT_GET(leaf
->hdr
.count
, ARCH_CONVERT
)] < needbytes
&&
270 INT_GET(leaf
->hdr
.stale
, ARCH_CONVERT
) > 1) {
274 * Otherwise if we don't have enough free bytes we need to
275 * convert to node form.
277 else if ((char *)bestsp
- (char *)&leaf
->ents
[INT_GET(leaf
->hdr
.count
, ARCH_CONVERT
)] <
280 * Just checking or no space reservation, give up.
282 if (args
->justcheck
|| args
->total
== 0) {
283 xfs_da_brelse(tp
, lbp
);
284 return XFS_ERROR(ENOSPC
);
287 * Convert to node form.
289 error
= xfs_dir2_leaf_to_node(args
, lbp
);
290 xfs_da_buf_done(lbp
);
294 * Then add the new entry.
296 return xfs_dir2_node_addname(args
);
299 * Otherwise it will fit without compaction.
304 * If just checking, then it will fit unless we needed to allocate
307 if (args
->justcheck
) {
308 xfs_da_brelse(tp
, lbp
);
309 return use_block
== -1 ? XFS_ERROR(ENOSPC
) : 0;
312 * If no allocations are allowed, return now before we've
315 if (args
->total
== 0 && use_block
== -1) {
316 xfs_da_brelse(tp
, lbp
);
317 return XFS_ERROR(ENOSPC
);
320 * Need to compact the leaf entries, removing stale ones.
321 * Leave one stale entry behind - the one closest to our
322 * insertion index - and we'll shift that one to our insertion
326 xfs_dir2_leaf_compact_x1(lbp
, &index
, &lowstale
, &highstale
,
327 &lfloglow
, &lfloghigh
);
330 * There are stale entries, so we'll need log-low and log-high
331 * impossibly bad values later.
333 else if (INT_GET(leaf
->hdr
.stale
, ARCH_CONVERT
)) {
334 lfloglow
= INT_GET(leaf
->hdr
.count
, ARCH_CONVERT
);
338 * If there was no data block space found, we need to allocate
341 if (use_block
== -1) {
343 * Add the new data block.
345 if ((error
= xfs_dir2_grow_inode(args
, XFS_DIR2_DATA_SPACE
,
347 xfs_da_brelse(tp
, lbp
);
351 * Initialize the block.
353 if ((error
= xfs_dir2_data_init(args
, use_block
, &dbp
))) {
354 xfs_da_brelse(tp
, lbp
);
358 * If we're adding a new data block on the end we need to
359 * extend the bests table. Copy it up one entry.
361 if (use_block
>= INT_GET(ltp
->bestcount
, ARCH_CONVERT
)) {
363 memmove(&bestsp
[0], &bestsp
[1],
364 INT_GET(ltp
->bestcount
, ARCH_CONVERT
) * sizeof(bestsp
[0]));
365 INT_MOD(ltp
->bestcount
, ARCH_CONVERT
, +1);
366 xfs_dir2_leaf_log_tail(tp
, lbp
);
367 xfs_dir2_leaf_log_bests(tp
, lbp
, 0, INT_GET(ltp
->bestcount
, ARCH_CONVERT
) - 1);
370 * If we're filling in a previously empty block just log it.
373 xfs_dir2_leaf_log_bests(tp
, lbp
, use_block
, use_block
);
375 INT_COPY(bestsp
[use_block
], data
->hdr
.bestfree
[0].length
, ARCH_CONVERT
);
379 * Already had space in some data block.
380 * Just read that one in.
384 xfs_da_read_buf(tp
, dp
, XFS_DIR2_DB_TO_DA(mp
, use_block
),
385 -1, &dbp
, XFS_DATA_FORK
))) {
386 xfs_da_brelse(tp
, lbp
);
392 xfs_dir2_data_check(dp
, dbp
);
394 * Point to the biggest freespace in our data block.
396 dup
= (xfs_dir2_data_unused_t
*)
397 ((char *)data
+ INT_GET(data
->hdr
.bestfree
[0].offset
, ARCH_CONVERT
));
398 ASSERT(INT_GET(dup
->length
, ARCH_CONVERT
) >= length
);
399 needscan
= needlog
= 0;
401 * Mark the initial part of our freespace in use for the new entry.
403 xfs_dir2_data_use_free(tp
, dbp
, dup
,
404 (xfs_dir2_data_aoff_t
)((char *)dup
- (char *)data
), length
,
405 &needlog
, &needscan
);
407 * Initialize our new entry (at last).
409 dep
= (xfs_dir2_data_entry_t
*)dup
;
410 INT_SET(dep
->inumber
, ARCH_CONVERT
, args
->inumber
);
411 dep
->namelen
= args
->namelen
;
412 memcpy(dep
->name
, args
->name
, dep
->namelen
);
413 tagp
= XFS_DIR2_DATA_ENTRY_TAG_P(dep
);
414 INT_SET(*tagp
, ARCH_CONVERT
, (xfs_dir2_data_off_t
)((char *)dep
- (char *)data
));
416 * Need to scan fix up the bestfree table.
419 xfs_dir2_data_freescan(mp
, data
, &needlog
, NULL
);
421 * Need to log the data block's header.
424 xfs_dir2_data_log_header(tp
, dbp
);
425 xfs_dir2_data_log_entry(tp
, dbp
, dep
);
427 * If the bests table needs to be changed, do it.
428 * Log the change unless we've already done that.
430 if (INT_GET(bestsp
[use_block
], ARCH_CONVERT
) != INT_GET(data
->hdr
.bestfree
[0].length
, ARCH_CONVERT
)) {
431 INT_COPY(bestsp
[use_block
], data
->hdr
.bestfree
[0].length
, ARCH_CONVERT
);
433 xfs_dir2_leaf_log_bests(tp
, lbp
, use_block
, use_block
);
436 * Now we need to make room to insert the leaf entry.
437 * If there are no stale entries, we just insert a hole at index.
439 if (!leaf
->hdr
.stale
) {
441 * lep is still good as the index leaf entry.
443 if (index
< INT_GET(leaf
->hdr
.count
, ARCH_CONVERT
))
444 memmove(lep
+ 1, lep
,
445 (INT_GET(leaf
->hdr
.count
, ARCH_CONVERT
) - index
) * sizeof(*lep
));
447 * Record low and high logging indices for the leaf.
450 lfloghigh
= INT_GET(leaf
->hdr
.count
, ARCH_CONVERT
);
451 INT_MOD(leaf
->hdr
.count
, ARCH_CONVERT
, +1);
454 * There are stale entries.
455 * We will use one of them for the new entry.
456 * It's probably not at the right location, so we'll have to
457 * shift some up or down first.
461 * If we didn't compact before, we need to find the nearest
462 * stale entries before and after our insertion point.
466 * Find the first stale entry before the insertion
469 for (lowstale
= index
- 1;
471 INT_GET(leaf
->ents
[lowstale
].address
, ARCH_CONVERT
) !=
472 XFS_DIR2_NULL_DATAPTR
;
476 * Find the next stale entry at or after the insertion
477 * point, if any. Stop if we go so far that the
478 * lowstale entry would be better.
480 for (highstale
= index
;
481 highstale
< INT_GET(leaf
->hdr
.count
, ARCH_CONVERT
) &&
482 INT_GET(leaf
->ents
[highstale
].address
, ARCH_CONVERT
) !=
483 XFS_DIR2_NULL_DATAPTR
&&
485 index
- lowstale
- 1 >= highstale
- index
);
490 * If the low one is better, use it.
493 (highstale
== INT_GET(leaf
->hdr
.count
, ARCH_CONVERT
) ||
494 index
- lowstale
- 1 < highstale
- index
)) {
495 ASSERT(index
- lowstale
- 1 >= 0);
496 ASSERT(INT_GET(leaf
->ents
[lowstale
].address
, ARCH_CONVERT
) ==
497 XFS_DIR2_NULL_DATAPTR
);
499 * Copy entries up to cover the stale entry
500 * and make room for the new entry.
502 if (index
- lowstale
- 1 > 0)
503 memmove(&leaf
->ents
[lowstale
],
504 &leaf
->ents
[lowstale
+ 1],
505 (index
- lowstale
- 1) * sizeof(*lep
));
506 lep
= &leaf
->ents
[index
- 1];
507 lfloglow
= MIN(lowstale
, lfloglow
);
508 lfloghigh
= MAX(index
- 1, lfloghigh
);
511 * The high one is better, so use that one.
514 ASSERT(highstale
- index
>= 0);
515 ASSERT(INT_GET(leaf
->ents
[highstale
].address
, ARCH_CONVERT
) ==
516 XFS_DIR2_NULL_DATAPTR
);
518 * Copy entries down to copver the stale entry
519 * and make room for the new entry.
521 if (highstale
- index
> 0)
522 memmove(&leaf
->ents
[index
+ 1],
524 (highstale
- index
) * sizeof(*lep
));
525 lep
= &leaf
->ents
[index
];
526 lfloglow
= MIN(index
, lfloglow
);
527 lfloghigh
= MAX(highstale
, lfloghigh
);
529 INT_MOD(leaf
->hdr
.stale
, ARCH_CONVERT
, -1);
532 * Fill in the new leaf entry.
534 INT_SET(lep
->hashval
, ARCH_CONVERT
, args
->hashval
);
535 INT_SET(lep
->address
, ARCH_CONVERT
, XFS_DIR2_DB_OFF_TO_DATAPTR(mp
, use_block
, INT_GET(*tagp
, ARCH_CONVERT
)));
537 * Log the leaf fields and give up the buffers.
539 xfs_dir2_leaf_log_header(tp
, lbp
);
540 xfs_dir2_leaf_log_ents(tp
, lbp
, lfloglow
, lfloghigh
);
541 xfs_dir2_leaf_check(dp
, lbp
);
542 xfs_da_buf_done(lbp
);
543 xfs_dir2_data_check(dp
, dbp
);
544 xfs_da_buf_done(dbp
);
550 * Check the internal consistency of a leaf1 block.
551 * Pop an assert if something is wrong.
555 xfs_inode_t
*dp
, /* incore directory inode */
556 xfs_dabuf_t
*bp
) /* leaf's buffer */
558 int i
; /* leaf index */
559 xfs_dir2_leaf_t
*leaf
; /* leaf structure */
560 xfs_dir2_leaf_tail_t
*ltp
; /* leaf tail pointer */
561 xfs_mount_t
*mp
; /* filesystem mount point */
562 int stale
; /* count of stale leaves */
566 ASSERT(INT_GET(leaf
->hdr
.info
.magic
, ARCH_CONVERT
) == XFS_DIR2_LEAF1_MAGIC
);
568 * This value is not restrictive enough.
569 * Should factor in the size of the bests table as well.
570 * We can deduce a value for that from di_size.
572 ASSERT(INT_GET(leaf
->hdr
.count
, ARCH_CONVERT
) <= XFS_DIR2_MAX_LEAF_ENTS(mp
));
573 ltp
= XFS_DIR2_LEAF_TAIL_P(mp
, leaf
);
575 * Leaves and bests don't overlap.
577 ASSERT((char *)&leaf
->ents
[INT_GET(leaf
->hdr
.count
, ARCH_CONVERT
)] <=
578 (char *)XFS_DIR2_LEAF_BESTS_P(ltp
));
580 * Check hash value order, count stale entries.
582 for (i
= stale
= 0; i
< INT_GET(leaf
->hdr
.count
, ARCH_CONVERT
); i
++) {
583 if (i
+ 1 < INT_GET(leaf
->hdr
.count
, ARCH_CONVERT
))
584 ASSERT(INT_GET(leaf
->ents
[i
].hashval
, ARCH_CONVERT
) <=
585 INT_GET(leaf
->ents
[i
+ 1].hashval
, ARCH_CONVERT
));
586 if (INT_GET(leaf
->ents
[i
].address
, ARCH_CONVERT
) == XFS_DIR2_NULL_DATAPTR
)
589 ASSERT(INT_GET(leaf
->hdr
.stale
, ARCH_CONVERT
) == stale
);
594 * Compact out any stale entries in the leaf.
595 * Log the header and changed leaf entries, if any.
598 xfs_dir2_leaf_compact(
599 xfs_da_args_t
*args
, /* operation arguments */
600 xfs_dabuf_t
*bp
) /* leaf buffer */
602 int from
; /* source leaf index */
603 xfs_dir2_leaf_t
*leaf
; /* leaf structure */
604 int loglow
; /* first leaf entry to log */
605 int to
; /* target leaf index */
608 if (!leaf
->hdr
.stale
) {
612 * Compress out the stale entries in place.
614 for (from
= to
= 0, loglow
= -1; from
< INT_GET(leaf
->hdr
.count
, ARCH_CONVERT
); from
++) {
615 if (INT_GET(leaf
->ents
[from
].address
, ARCH_CONVERT
) == XFS_DIR2_NULL_DATAPTR
)
618 * Only actually copy the entries that are different.
623 leaf
->ents
[to
] = leaf
->ents
[from
];
628 * Update and log the header, log the leaf entries.
630 ASSERT(INT_GET(leaf
->hdr
.stale
, ARCH_CONVERT
) == from
- to
);
631 INT_MOD(leaf
->hdr
.count
, ARCH_CONVERT
, -(INT_GET(leaf
->hdr
.stale
, ARCH_CONVERT
)));
633 xfs_dir2_leaf_log_header(args
->trans
, bp
);
635 xfs_dir2_leaf_log_ents(args
->trans
, bp
, loglow
, to
- 1);
639 * Compact the leaf entries, removing stale ones.
640 * Leave one stale entry behind - the one closest to our
641 * insertion index - and the caller will shift that one to our insertion
643 * Return new insertion index, where the remaining stale entry is,
644 * and leaf logging indices.
647 xfs_dir2_leaf_compact_x1(
648 xfs_dabuf_t
*bp
, /* leaf buffer */
649 int *indexp
, /* insertion index */
650 int *lowstalep
, /* out: stale entry before us */
651 int *highstalep
, /* out: stale entry after us */
652 int *lowlogp
, /* out: low log index */
653 int *highlogp
) /* out: high log index */
655 int from
; /* source copy index */
656 int highstale
; /* stale entry at/after index */
657 int index
; /* insertion index */
658 int keepstale
; /* source index of kept stale */
659 xfs_dir2_leaf_t
*leaf
; /* leaf structure */
660 int lowstale
; /* stale entry before index */
661 int newindex
=0; /* new insertion index */
662 int to
; /* destination copy index */
665 ASSERT(INT_GET(leaf
->hdr
.stale
, ARCH_CONVERT
) > 1);
668 * Find the first stale entry before our index, if any.
670 for (lowstale
= index
- 1;
672 INT_GET(leaf
->ents
[lowstale
].address
, ARCH_CONVERT
) != XFS_DIR2_NULL_DATAPTR
;
676 * Find the first stale entry at or after our index, if any.
677 * Stop if the answer would be worse than lowstale.
679 for (highstale
= index
;
680 highstale
< INT_GET(leaf
->hdr
.count
, ARCH_CONVERT
) &&
681 INT_GET(leaf
->ents
[highstale
].address
, ARCH_CONVERT
) != XFS_DIR2_NULL_DATAPTR
&&
682 (lowstale
< 0 || index
- lowstale
> highstale
- index
);
686 * Pick the better of lowstale and highstale.
689 (highstale
== INT_GET(leaf
->hdr
.count
, ARCH_CONVERT
) ||
690 index
- lowstale
<= highstale
- index
))
691 keepstale
= lowstale
;
693 keepstale
= highstale
;
695 * Copy the entries in place, removing all the stale entries
698 for (from
= to
= 0; from
< INT_GET(leaf
->hdr
.count
, ARCH_CONVERT
); from
++) {
700 * Notice the new value of index.
704 if (from
!= keepstale
&&
705 INT_GET(leaf
->ents
[from
].address
, ARCH_CONVERT
) == XFS_DIR2_NULL_DATAPTR
) {
711 * Record the new keepstale value for the insertion.
713 if (from
== keepstale
)
714 lowstale
= highstale
= to
;
716 * Copy only the entries that have moved.
719 leaf
->ents
[to
] = leaf
->ents
[from
];
724 * If the insertion point was past the last entry,
725 * set the new insertion point accordingly.
731 * Adjust the leaf header values.
733 INT_MOD(leaf
->hdr
.count
, ARCH_CONVERT
, -(from
- to
));
734 INT_SET(leaf
->hdr
.stale
, ARCH_CONVERT
, 1);
736 * Remember the low/high stale value only in the "right"
739 if (lowstale
>= newindex
)
742 highstale
= INT_GET(leaf
->hdr
.count
, ARCH_CONVERT
);
743 *highlogp
= INT_GET(leaf
->hdr
.count
, ARCH_CONVERT
) - 1;
744 *lowstalep
= lowstale
;
745 *highstalep
= highstale
;
749 * Getdents (readdir) for leaf and node directories.
750 * This reads the data blocks only, so is the same for both forms.
753 xfs_dir2_leaf_getdents(
754 xfs_trans_t
*tp
, /* transaction pointer */
755 xfs_inode_t
*dp
, /* incore directory inode */
756 uio_t
*uio
, /* I/O control & vectors */
757 int *eofp
, /* out: reached end of dir */
758 xfs_dirent_t
*dbp
, /* caller's buffer */
759 xfs_dir2_put_t put
) /* ABI formatting routine */
761 xfs_dabuf_t
*bp
; /* data block buffer */
762 int byteoff
; /* offset in current block */
763 xfs_dir2_db_t curdb
; /* db for current block */
764 xfs_dir2_off_t curoff
; /* current overall offset */
765 xfs_dir2_data_t
*data
; /* data block structure */
766 xfs_dir2_data_entry_t
*dep
; /* data entry */
767 xfs_dir2_data_unused_t
*dup
; /* unused entry */
768 int eof
; /* reached end of directory */
769 int error
=0; /* error return value */
770 int i
; /* temporary loop index */
771 int j
; /* temporary loop index */
772 int length
; /* temporary length value */
773 xfs_bmbt_irec_t
*map
; /* map vector for blocks */
774 xfs_extlen_t map_blocks
; /* number of fsbs in map */
775 xfs_dablk_t map_off
; /* last mapped file offset */
776 int map_size
; /* total entries in *map */
777 int map_valid
; /* valid entries in *map */
778 xfs_mount_t
*mp
; /* filesystem mount point */
779 xfs_dir2_off_t newoff
; /* new curoff after new blk */
780 int nmap
; /* mappings to ask xfs_bmapi */
781 xfs_dir2_put_args_t p
; /* formatting arg bundle */
782 char *ptr
=NULL
; /* pointer to current data */
783 int ra_current
; /* number of read-ahead blks */
784 int ra_index
; /* *map index for read-ahead */
785 int ra_offset
; /* map entry offset for ra */
786 int ra_want
; /* readahead count wanted */
789 * If the offset is at or past the largest allowed value,
790 * give up right away, return eof.
792 if (uio
->uio_offset
>= XFS_DIR2_MAX_DATAPTR
) {
798 * Setup formatting arguments.
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
);
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 (INT_GET(dup
->freetag
, ARCH_CONVERT
)
1050 == XFS_DIR2_DATA_FREE_TAG
) {
1052 length
= INT_GET(dup
->length
,
1057 dep
= (xfs_dir2_data_entry_t
*)ptr
;
1059 XFS_DIR2_DATA_ENTSIZE(dep
->namelen
);
1063 * Now set our real offset.
1066 XFS_DIR2_DB_OFF_TO_BYTE(mp
,
1067 XFS_DIR2_BYTE_TO_DB(mp
, curoff
),
1068 (char *)ptr
- (char *)data
);
1069 if (ptr
>= (char *)data
+ mp
->m_dirblksize
) {
1075 * We have a pointer to an entry.
1078 dup
= (xfs_dir2_data_unused_t
*)ptr
;
1080 * No, it's unused, skip over it.
1082 if (INT_GET(dup
->freetag
, ARCH_CONVERT
)
1083 == XFS_DIR2_DATA_FREE_TAG
) {
1084 length
= INT_GET(dup
->length
, ARCH_CONVERT
);
1091 * Copy the entry into the putargs, and try formatting it.
1093 dep
= (xfs_dir2_data_entry_t
*)ptr
;
1095 p
.namelen
= dep
->namelen
;
1097 length
= XFS_DIR2_DATA_ENTSIZE(p
.namelen
);
1099 p
.cook
= XFS_DIR2_BYTE_TO_DATAPTR(mp
, curoff
+ length
);
1101 p
.ino
= INT_GET(dep
->inumber
, ARCH_CONVERT
);
1103 p
.ino
+= mp
->m_inoadd
;
1105 p
.name
= (char *)dep
->name
;
1110 * Won't fit. Return to caller.
1117 * Advance to next entry in the block.
1124 * All done. Set output offset value to current offset.
1127 if (curoff
> XFS_DIR2_DATAPTR_TO_BYTE(mp
, XFS_DIR2_MAX_DATAPTR
))
1128 uio
->uio_offset
= XFS_DIR2_MAX_DATAPTR
;
1130 uio
->uio_offset
= XFS_DIR2_BYTE_TO_DATAPTR(mp
, curoff
);
1131 kmem_free(map
, map_size
* sizeof(*map
));
1133 xfs_da_brelse(tp
, bp
);
1138 * Initialize a new leaf block, leaf1 or leafn magic accepted.
1142 xfs_da_args_t
*args
, /* operation arguments */
1143 xfs_dir2_db_t bno
, /* directory block number */
1144 xfs_dabuf_t
**bpp
, /* out: leaf buffer */
1145 int magic
) /* magic number for block */
1147 xfs_dabuf_t
*bp
; /* leaf buffer */
1148 xfs_inode_t
*dp
; /* incore directory inode */
1149 int error
; /* error return code */
1150 xfs_dir2_leaf_t
*leaf
; /* leaf structure */
1151 xfs_dir2_leaf_tail_t
*ltp
; /* leaf tail structure */
1152 xfs_mount_t
*mp
; /* filesystem mount point */
1153 xfs_trans_t
*tp
; /* transaction pointer */
1159 ASSERT(bno
>= XFS_DIR2_LEAF_FIRSTDB(mp
) &&
1160 bno
< XFS_DIR2_FREE_FIRSTDB(mp
));
1162 * Get the buffer for the block.
1164 error
= xfs_da_get_buf(tp
, dp
, XFS_DIR2_DB_TO_DA(mp
, bno
), -1, &bp
,
1172 * Initialize the header.
1174 INT_SET(leaf
->hdr
.info
.magic
, ARCH_CONVERT
, magic
);
1175 leaf
->hdr
.info
.forw
= 0;
1176 leaf
->hdr
.info
.back
= 0;
1177 leaf
->hdr
.count
= 0;
1178 leaf
->hdr
.stale
= 0;
1179 xfs_dir2_leaf_log_header(tp
, bp
);
1181 * If it's a leaf-format directory initialize the tail.
1182 * In this case our caller has the real bests table to copy into
1185 if (magic
== XFS_DIR2_LEAF1_MAGIC
) {
1186 ltp
= XFS_DIR2_LEAF_TAIL_P(mp
, leaf
);
1188 xfs_dir2_leaf_log_tail(tp
, bp
);
1195 * Log the bests entries indicated from a leaf1 block.
1198 xfs_dir2_leaf_log_bests(
1199 xfs_trans_t
*tp
, /* transaction pointer */
1200 xfs_dabuf_t
*bp
, /* leaf buffer */
1201 int first
, /* first entry to log */
1202 int last
) /* last entry to log */
1204 xfs_dir2_data_off_t
*firstb
; /* pointer to first entry */
1205 xfs_dir2_data_off_t
*lastb
; /* pointer to last entry */
1206 xfs_dir2_leaf_t
*leaf
; /* leaf structure */
1207 xfs_dir2_leaf_tail_t
*ltp
; /* leaf tail structure */
1210 ASSERT(INT_GET(leaf
->hdr
.info
.magic
, ARCH_CONVERT
) == XFS_DIR2_LEAF1_MAGIC
);
1211 ltp
= XFS_DIR2_LEAF_TAIL_P(tp
->t_mountp
, leaf
);
1212 firstb
= XFS_DIR2_LEAF_BESTS_P(ltp
) + first
;
1213 lastb
= XFS_DIR2_LEAF_BESTS_P(ltp
) + last
;
1214 xfs_da_log_buf(tp
, bp
, (uint
)((char *)firstb
- (char *)leaf
),
1215 (uint
)((char *)lastb
- (char *)leaf
+ sizeof(*lastb
) - 1));
1219 * Log the leaf entries indicated from a leaf1 or leafn block.
1222 xfs_dir2_leaf_log_ents(
1223 xfs_trans_t
*tp
, /* transaction pointer */
1224 xfs_dabuf_t
*bp
, /* leaf buffer */
1225 int first
, /* first entry to log */
1226 int last
) /* last entry to log */
1228 xfs_dir2_leaf_entry_t
*firstlep
; /* pointer to first entry */
1229 xfs_dir2_leaf_entry_t
*lastlep
; /* pointer to last entry */
1230 xfs_dir2_leaf_t
*leaf
; /* leaf structure */
1233 ASSERT(INT_GET(leaf
->hdr
.info
.magic
, ARCH_CONVERT
) == XFS_DIR2_LEAF1_MAGIC
||
1234 INT_GET(leaf
->hdr
.info
.magic
, ARCH_CONVERT
) == XFS_DIR2_LEAFN_MAGIC
);
1235 firstlep
= &leaf
->ents
[first
];
1236 lastlep
= &leaf
->ents
[last
];
1237 xfs_da_log_buf(tp
, bp
, (uint
)((char *)firstlep
- (char *)leaf
),
1238 (uint
)((char *)lastlep
- (char *)leaf
+ sizeof(*lastlep
) - 1));
1242 * Log the header of the leaf1 or leafn block.
1245 xfs_dir2_leaf_log_header(
1246 xfs_trans_t
*tp
, /* transaction pointer */
1247 xfs_dabuf_t
*bp
) /* leaf buffer */
1249 xfs_dir2_leaf_t
*leaf
; /* leaf structure */
1252 ASSERT(INT_GET(leaf
->hdr
.info
.magic
, ARCH_CONVERT
) == XFS_DIR2_LEAF1_MAGIC
||
1253 INT_GET(leaf
->hdr
.info
.magic
, ARCH_CONVERT
) == XFS_DIR2_LEAFN_MAGIC
);
1254 xfs_da_log_buf(tp
, bp
, (uint
)((char *)&leaf
->hdr
- (char *)leaf
),
1255 (uint
)(sizeof(leaf
->hdr
) - 1));
1259 * Log the tail of the leaf1 block.
1262 xfs_dir2_leaf_log_tail(
1263 xfs_trans_t
*tp
, /* transaction pointer */
1264 xfs_dabuf_t
*bp
) /* leaf buffer */
1266 xfs_dir2_leaf_t
*leaf
; /* leaf structure */
1267 xfs_dir2_leaf_tail_t
*ltp
; /* leaf tail structure */
1268 xfs_mount_t
*mp
; /* filesystem mount point */
1272 ASSERT(INT_GET(leaf
->hdr
.info
.magic
, ARCH_CONVERT
) == XFS_DIR2_LEAF1_MAGIC
);
1273 ltp
= XFS_DIR2_LEAF_TAIL_P(mp
, leaf
);
1274 xfs_da_log_buf(tp
, bp
, (uint
)((char *)ltp
- (char *)leaf
),
1275 (uint
)(mp
->m_dirblksize
- 1));
1279 * Look up the entry referred to by args in the leaf format directory.
1280 * Most of the work is done by the xfs_dir2_leaf_lookup_int routine which
1281 * is also used by the node-format code.
1284 xfs_dir2_leaf_lookup(
1285 xfs_da_args_t
*args
) /* operation arguments */
1287 xfs_dabuf_t
*dbp
; /* data block buffer */
1288 xfs_dir2_data_entry_t
*dep
; /* data block entry */
1289 xfs_inode_t
*dp
; /* incore directory inode */
1290 int error
; /* error return code */
1291 int index
; /* found entry index */
1292 xfs_dabuf_t
*lbp
; /* leaf buffer */
1293 xfs_dir2_leaf_t
*leaf
; /* leaf structure */
1294 xfs_dir2_leaf_entry_t
*lep
; /* leaf entry */
1295 xfs_trans_t
*tp
; /* transaction pointer */
1297 xfs_dir2_trace_args("leaf_lookup", args
);
1299 * Look up name in the leaf block, returning both buffers and index.
1301 if ((error
= xfs_dir2_leaf_lookup_int(args
, &lbp
, &index
, &dbp
))) {
1306 xfs_dir2_leaf_check(dp
, lbp
);
1309 * Get to the leaf entry and contained data entry address.
1311 lep
= &leaf
->ents
[index
];
1313 * Point to the data entry.
1315 dep
= (xfs_dir2_data_entry_t
*)
1316 ((char *)dbp
->data
+
1317 XFS_DIR2_DATAPTR_TO_OFF(dp
->i_mount
, INT_GET(lep
->address
, ARCH_CONVERT
)));
1319 * Return the found inode number.
1321 args
->inumber
= INT_GET(dep
->inumber
, ARCH_CONVERT
);
1322 xfs_da_brelse(tp
, dbp
);
1323 xfs_da_brelse(tp
, lbp
);
1324 return XFS_ERROR(EEXIST
);
1328 * Look up name/hash in the leaf block.
1329 * Fill in indexp with the found index, and dbpp with the data buffer.
1330 * If not found dbpp will be NULL, and ENOENT comes back.
1331 * lbpp will always be filled in with the leaf buffer unless there's an error.
1333 static int /* error */
1334 xfs_dir2_leaf_lookup_int(
1335 xfs_da_args_t
*args
, /* operation arguments */
1336 xfs_dabuf_t
**lbpp
, /* out: leaf buffer */
1337 int *indexp
, /* out: index in leaf block */
1338 xfs_dabuf_t
**dbpp
) /* out: data buffer */
1340 xfs_dir2_db_t curdb
; /* current data block number */
1341 xfs_dabuf_t
*dbp
; /* data buffer */
1342 xfs_dir2_data_entry_t
*dep
; /* data entry */
1343 xfs_inode_t
*dp
; /* incore directory inode */
1344 int error
; /* error return code */
1345 int index
; /* index in leaf block */
1346 xfs_dabuf_t
*lbp
; /* leaf buffer */
1347 xfs_dir2_leaf_entry_t
*lep
; /* leaf entry */
1348 xfs_dir2_leaf_t
*leaf
; /* leaf structure */
1349 xfs_mount_t
*mp
; /* filesystem mount point */
1350 xfs_dir2_db_t newdb
; /* new data block number */
1351 xfs_trans_t
*tp
; /* transaction pointer */
1357 * Read the leaf block into the buffer.
1360 xfs_da_read_buf(tp
, dp
, mp
->m_dirleafblk
, -1, &lbp
,
1366 xfs_dir2_leaf_check(dp
, lbp
);
1368 * Look for the first leaf entry with our hash value.
1370 index
= xfs_dir2_leaf_search_hash(args
, lbp
);
1372 * Loop over all the entries with the right hash value
1373 * looking to match the name.
1375 for (lep
= &leaf
->ents
[index
], dbp
= NULL
, curdb
= -1;
1376 index
< INT_GET(leaf
->hdr
.count
, ARCH_CONVERT
) && INT_GET(lep
->hashval
, ARCH_CONVERT
) == args
->hashval
;
1379 * Skip over stale leaf entries.
1381 if (INT_GET(lep
->address
, ARCH_CONVERT
) == XFS_DIR2_NULL_DATAPTR
)
1384 * Get the new data block number.
1386 newdb
= XFS_DIR2_DATAPTR_TO_DB(mp
, INT_GET(lep
->address
, ARCH_CONVERT
));
1388 * If it's not the same as the old data block number,
1389 * need to pitch the old one and read the new one.
1391 if (newdb
!= curdb
) {
1393 xfs_da_brelse(tp
, dbp
);
1395 xfs_da_read_buf(tp
, dp
,
1396 XFS_DIR2_DB_TO_DA(mp
, newdb
), -1, &dbp
,
1398 xfs_da_brelse(tp
, lbp
);
1401 xfs_dir2_data_check(dp
, dbp
);
1405 * Point to the data entry.
1407 dep
= (xfs_dir2_data_entry_t
*)
1408 ((char *)dbp
->data
+
1409 XFS_DIR2_DATAPTR_TO_OFF(mp
, INT_GET(lep
->address
, ARCH_CONVERT
)));
1411 * If it matches then return it.
1413 if (dep
->namelen
== args
->namelen
&&
1414 dep
->name
[0] == args
->name
[0] &&
1415 memcmp(dep
->name
, args
->name
, args
->namelen
) == 0) {
1422 * No match found, return ENOENT.
1424 ASSERT(args
->oknoent
);
1426 xfs_da_brelse(tp
, dbp
);
1427 xfs_da_brelse(tp
, lbp
);
1428 return XFS_ERROR(ENOENT
);
1432 * Remove an entry from a leaf format directory.
1435 xfs_dir2_leaf_removename(
1436 xfs_da_args_t
*args
) /* operation arguments */
1438 xfs_dir2_data_off_t
*bestsp
; /* leaf block best freespace */
1439 xfs_dir2_data_t
*data
; /* data block structure */
1440 xfs_dir2_db_t db
; /* data block number */
1441 xfs_dabuf_t
*dbp
; /* data block buffer */
1442 xfs_dir2_data_entry_t
*dep
; /* data entry structure */
1443 xfs_inode_t
*dp
; /* incore directory inode */
1444 int error
; /* error return code */
1445 xfs_dir2_db_t i
; /* temporary data block # */
1446 int index
; /* index into leaf entries */
1447 xfs_dabuf_t
*lbp
; /* leaf buffer */
1448 xfs_dir2_leaf_t
*leaf
; /* leaf structure */
1449 xfs_dir2_leaf_entry_t
*lep
; /* leaf entry */
1450 xfs_dir2_leaf_tail_t
*ltp
; /* leaf tail structure */
1451 xfs_mount_t
*mp
; /* filesystem mount point */
1452 int needlog
; /* need to log data header */
1453 int needscan
; /* need to rescan data frees */
1454 xfs_dir2_data_off_t oldbest
; /* old value of best free */
1455 xfs_trans_t
*tp
; /* transaction pointer */
1457 xfs_dir2_trace_args("leaf_removename", args
);
1459 * Lookup the leaf entry, get the leaf and data blocks read in.
1461 if ((error
= xfs_dir2_leaf_lookup_int(args
, &lbp
, &index
, &dbp
))) {
1469 xfs_dir2_data_check(dp
, dbp
);
1471 * Point to the leaf entry, use that to point to the data entry.
1473 lep
= &leaf
->ents
[index
];
1474 db
= XFS_DIR2_DATAPTR_TO_DB(mp
, INT_GET(lep
->address
, ARCH_CONVERT
));
1475 dep
= (xfs_dir2_data_entry_t
*)
1476 ((char *)data
+ XFS_DIR2_DATAPTR_TO_OFF(mp
, INT_GET(lep
->address
, ARCH_CONVERT
)));
1477 needscan
= needlog
= 0;
1478 oldbest
= INT_GET(data
->hdr
.bestfree
[0].length
, ARCH_CONVERT
);
1479 ltp
= XFS_DIR2_LEAF_TAIL_P(mp
, leaf
);
1480 bestsp
= XFS_DIR2_LEAF_BESTS_P(ltp
);
1481 ASSERT(INT_GET(bestsp
[db
], ARCH_CONVERT
) == oldbest
);
1483 * Mark the former data entry unused.
1485 xfs_dir2_data_make_free(tp
, dbp
,
1486 (xfs_dir2_data_aoff_t
)((char *)dep
- (char *)data
),
1487 XFS_DIR2_DATA_ENTSIZE(dep
->namelen
), &needlog
, &needscan
);
1489 * We just mark the leaf entry stale by putting a null in it.
1491 INT_MOD(leaf
->hdr
.stale
, ARCH_CONVERT
, +1);
1492 xfs_dir2_leaf_log_header(tp
, lbp
);
1493 INT_SET(lep
->address
, ARCH_CONVERT
, XFS_DIR2_NULL_DATAPTR
);
1494 xfs_dir2_leaf_log_ents(tp
, lbp
, index
, index
);
1496 * Scan the freespace in the data block again if necessary,
1497 * log the data block header if necessary.
1500 xfs_dir2_data_freescan(mp
, data
, &needlog
, NULL
);
1502 xfs_dir2_data_log_header(tp
, dbp
);
1504 * If the longest freespace in the data block has changed,
1505 * put the new value in the bests table and log that.
1507 if (INT_GET(data
->hdr
.bestfree
[0].length
, ARCH_CONVERT
) != oldbest
) {
1508 INT_COPY(bestsp
[db
], data
->hdr
.bestfree
[0].length
, ARCH_CONVERT
);
1509 xfs_dir2_leaf_log_bests(tp
, lbp
, db
, db
);
1511 xfs_dir2_data_check(dp
, dbp
);
1513 * If the data block is now empty then get rid of the data block.
1515 if (INT_GET(data
->hdr
.bestfree
[0].length
, ARCH_CONVERT
) ==
1516 mp
->m_dirblksize
- (uint
)sizeof(data
->hdr
)) {
1517 ASSERT(db
!= mp
->m_dirdatablk
);
1518 if ((error
= xfs_dir2_shrink_inode(args
, db
, dbp
))) {
1520 * Nope, can't get rid of it because it caused
1521 * allocation of a bmap btree block to do so.
1522 * Just go on, returning success, leaving the
1523 * empty block in place.
1525 if (error
== ENOSPC
&& args
->total
== 0) {
1526 xfs_da_buf_done(dbp
);
1529 xfs_dir2_leaf_check(dp
, lbp
);
1530 xfs_da_buf_done(lbp
);
1535 * If this is the last data block then compact the
1536 * bests table by getting rid of entries.
1538 if (db
== INT_GET(ltp
->bestcount
, ARCH_CONVERT
) - 1) {
1540 * Look for the last active entry (i).
1542 for (i
= db
- 1; i
> 0; i
--) {
1543 if (INT_GET(bestsp
[i
], ARCH_CONVERT
) != NULLDATAOFF
)
1547 * Copy the table down so inactive entries at the
1550 memmove(&bestsp
[db
- i
], bestsp
,
1551 (INT_GET(ltp
->bestcount
, ARCH_CONVERT
) - (db
- i
)) * sizeof(*bestsp
));
1552 INT_MOD(ltp
->bestcount
, ARCH_CONVERT
, -(db
- i
));
1553 xfs_dir2_leaf_log_tail(tp
, lbp
);
1554 xfs_dir2_leaf_log_bests(tp
, lbp
, 0, INT_GET(ltp
->bestcount
, ARCH_CONVERT
) - 1);
1556 INT_SET(bestsp
[db
], ARCH_CONVERT
, NULLDATAOFF
);
1559 * If the data block was not the first one, drop it.
1561 else if (db
!= mp
->m_dirdatablk
&& dbp
!= NULL
) {
1562 xfs_da_buf_done(dbp
);
1565 xfs_dir2_leaf_check(dp
, lbp
);
1567 * See if we can convert to block form.
1569 return xfs_dir2_leaf_to_block(args
, lbp
, dbp
);
1573 * Replace the inode number in a leaf format directory entry.
1576 xfs_dir2_leaf_replace(
1577 xfs_da_args_t
*args
) /* operation arguments */
1579 xfs_dabuf_t
*dbp
; /* data block buffer */
1580 xfs_dir2_data_entry_t
*dep
; /* data block entry */
1581 xfs_inode_t
*dp
; /* incore directory inode */
1582 int error
; /* error return code */
1583 int index
; /* index of leaf entry */
1584 xfs_dabuf_t
*lbp
; /* leaf buffer */
1585 xfs_dir2_leaf_t
*leaf
; /* leaf structure */
1586 xfs_dir2_leaf_entry_t
*lep
; /* leaf entry */
1587 xfs_trans_t
*tp
; /* transaction pointer */
1589 xfs_dir2_trace_args("leaf_replace", args
);
1591 * Look up the entry.
1593 if ((error
= xfs_dir2_leaf_lookup_int(args
, &lbp
, &index
, &dbp
))) {
1599 * Point to the leaf entry, get data address from it.
1601 lep
= &leaf
->ents
[index
];
1603 * Point to the data entry.
1605 dep
= (xfs_dir2_data_entry_t
*)
1606 ((char *)dbp
->data
+
1607 XFS_DIR2_DATAPTR_TO_OFF(dp
->i_mount
, INT_GET(lep
->address
, ARCH_CONVERT
)));
1608 ASSERT(args
->inumber
!= INT_GET(dep
->inumber
, ARCH_CONVERT
));
1610 * Put the new inode number in, log it.
1612 INT_SET(dep
->inumber
, ARCH_CONVERT
, args
->inumber
);
1614 xfs_dir2_data_log_entry(tp
, dbp
, dep
);
1615 xfs_da_buf_done(dbp
);
1616 xfs_dir2_leaf_check(dp
, lbp
);
1617 xfs_da_brelse(tp
, lbp
);
1622 * Return index in the leaf block (lbp) which is either the first
1623 * one with this hash value, or if there are none, the insert point
1624 * for that hash value.
1626 int /* index value */
1627 xfs_dir2_leaf_search_hash(
1628 xfs_da_args_t
*args
, /* operation arguments */
1629 xfs_dabuf_t
*lbp
) /* leaf buffer */
1631 xfs_dahash_t hash
=0; /* hash from this entry */
1632 xfs_dahash_t hashwant
; /* hash value looking for */
1633 int high
; /* high leaf index */
1634 int low
; /* low leaf index */
1635 xfs_dir2_leaf_t
*leaf
; /* leaf structure */
1636 xfs_dir2_leaf_entry_t
*lep
; /* leaf entry */
1637 int mid
=0; /* current leaf index */
1641 if (!leaf
->hdr
.count
)
1645 * Note, the table cannot be empty, so we have to go through the loop.
1646 * Binary search the leaf entries looking for our hash value.
1648 for (lep
= leaf
->ents
, low
= 0, high
= INT_GET(leaf
->hdr
.count
, ARCH_CONVERT
) - 1,
1649 hashwant
= args
->hashval
;
1651 mid
= (low
+ high
) >> 1;
1652 if ((hash
= INT_GET(lep
[mid
].hashval
, ARCH_CONVERT
)) == hashwant
)
1654 if (hash
< hashwant
)
1660 * Found one, back up through all the equal hash values.
1662 if (hash
== hashwant
) {
1663 while (mid
> 0 && INT_GET(lep
[mid
- 1].hashval
, ARCH_CONVERT
) == hashwant
) {
1668 * Need to point to an entry higher than ours.
1670 else if (hash
< hashwant
)
1676 * Trim off a trailing data block. We know it's empty since the leaf
1677 * freespace table says so.
1680 xfs_dir2_leaf_trim_data(
1681 xfs_da_args_t
*args
, /* operation arguments */
1682 xfs_dabuf_t
*lbp
, /* leaf buffer */
1683 xfs_dir2_db_t db
) /* data block number */
1685 xfs_dir2_data_off_t
*bestsp
; /* leaf bests table */
1687 xfs_dir2_data_t
*data
; /* data block structure */
1689 xfs_dabuf_t
*dbp
; /* data block buffer */
1690 xfs_inode_t
*dp
; /* incore directory inode */
1691 int error
; /* error return value */
1692 xfs_dir2_leaf_t
*leaf
; /* leaf structure */
1693 xfs_dir2_leaf_tail_t
*ltp
; /* leaf tail structure */
1694 xfs_mount_t
*mp
; /* filesystem mount point */
1695 xfs_trans_t
*tp
; /* transaction pointer */
1701 * Read the offending data block. We need its buffer.
1703 if ((error
= xfs_da_read_buf(tp
, dp
, XFS_DIR2_DB_TO_DA(mp
, db
), -1, &dbp
,
1709 ASSERT(INT_GET(data
->hdr
.magic
, ARCH_CONVERT
) == XFS_DIR2_DATA_MAGIC
);
1711 /* this seems to be an error
1712 * data is only valid if DEBUG is defined?
1717 ltp
= XFS_DIR2_LEAF_TAIL_P(mp
, leaf
);
1718 ASSERT(INT_GET(data
->hdr
.bestfree
[0].length
, ARCH_CONVERT
) ==
1719 mp
->m_dirblksize
- (uint
)sizeof(data
->hdr
));
1720 ASSERT(db
== INT_GET(ltp
->bestcount
, ARCH_CONVERT
) - 1);
1722 * Get rid of the data block.
1724 if ((error
= xfs_dir2_shrink_inode(args
, db
, dbp
))) {
1725 ASSERT(error
!= ENOSPC
);
1726 xfs_da_brelse(tp
, dbp
);
1730 * Eliminate the last bests entry from the table.
1732 bestsp
= XFS_DIR2_LEAF_BESTS_P(ltp
);
1733 INT_MOD(ltp
->bestcount
, ARCH_CONVERT
, -1);
1734 memmove(&bestsp
[1], &bestsp
[0], INT_GET(ltp
->bestcount
, ARCH_CONVERT
) * sizeof(*bestsp
));
1735 xfs_dir2_leaf_log_tail(tp
, lbp
);
1736 xfs_dir2_leaf_log_bests(tp
, lbp
, 0, INT_GET(ltp
->bestcount
, ARCH_CONVERT
) - 1);
1741 * Convert node form directory to leaf form directory.
1742 * The root of the node form dir needs to already be a LEAFN block.
1743 * Just return if we can't do anything.
1746 xfs_dir2_node_to_leaf(
1747 xfs_da_state_t
*state
) /* directory operation state */
1749 xfs_da_args_t
*args
; /* operation arguments */
1750 xfs_inode_t
*dp
; /* incore directory inode */
1751 int error
; /* error return code */
1752 xfs_dabuf_t
*fbp
; /* buffer for freespace block */
1753 xfs_fileoff_t fo
; /* freespace file offset */
1754 xfs_dir2_free_t
*free
; /* freespace structure */
1755 xfs_dabuf_t
*lbp
; /* buffer for leaf block */
1756 xfs_dir2_leaf_tail_t
*ltp
; /* tail of leaf structure */
1757 xfs_dir2_leaf_t
*leaf
; /* leaf structure */
1758 xfs_mount_t
*mp
; /* filesystem mount point */
1759 int rval
; /* successful free trim? */
1760 xfs_trans_t
*tp
; /* transaction pointer */
1763 * There's more than a leaf level in the btree, so there must
1764 * be multiple leafn blocks. Give up.
1766 if (state
->path
.active
> 1)
1769 xfs_dir2_trace_args("node_to_leaf", args
);
1774 * Get the last offset in the file.
1776 if ((error
= xfs_bmap_last_offset(tp
, dp
, &fo
, XFS_DATA_FORK
))) {
1779 fo
-= mp
->m_dirblkfsbs
;
1781 * If there are freespace blocks other than the first one,
1782 * take this opportunity to remove trailing empty freespace blocks
1783 * that may have been left behind during no-space-reservation
1786 while (fo
> mp
->m_dirfreeblk
) {
1787 if ((error
= xfs_dir2_node_trim_free(args
, fo
, &rval
))) {
1791 fo
-= mp
->m_dirblkfsbs
;
1796 * Now find the block just before the freespace block.
1798 if ((error
= xfs_bmap_last_before(tp
, dp
, &fo
, XFS_DATA_FORK
))) {
1802 * If it's not the single leaf block, give up.
1804 if (XFS_FSB_TO_B(mp
, fo
) > XFS_DIR2_LEAF_OFFSET
+ mp
->m_dirblksize
)
1806 lbp
= state
->path
.blk
[0].bp
;
1808 ASSERT(INT_GET(leaf
->hdr
.info
.magic
, ARCH_CONVERT
) == XFS_DIR2_LEAFN_MAGIC
);
1810 * Read the freespace block.
1812 if ((error
= xfs_da_read_buf(tp
, dp
, mp
->m_dirfreeblk
, -1, &fbp
,
1817 ASSERT(INT_GET(free
->hdr
.magic
, ARCH_CONVERT
) == XFS_DIR2_FREE_MAGIC
);
1818 ASSERT(!free
->hdr
.firstdb
);
1820 * Now see if the leafn and free data will fit in a leaf1.
1821 * If not, release the buffer and give up.
1823 if ((uint
)sizeof(leaf
->hdr
) +
1824 (INT_GET(leaf
->hdr
.count
, ARCH_CONVERT
) - INT_GET(leaf
->hdr
.stale
, ARCH_CONVERT
)) * (uint
)sizeof(leaf
->ents
[0]) +
1825 INT_GET(free
->hdr
.nvalid
, ARCH_CONVERT
) * (uint
)sizeof(leaf
->bests
[0]) +
1826 (uint
)sizeof(leaf
->tail
) >
1828 xfs_da_brelse(tp
, fbp
);
1832 * If the leaf has any stale entries in it, compress them out.
1833 * The compact routine will log the header.
1835 if (INT_GET(leaf
->hdr
.stale
, ARCH_CONVERT
))
1836 xfs_dir2_leaf_compact(args
, lbp
);
1838 xfs_dir2_leaf_log_header(tp
, lbp
);
1839 INT_SET(leaf
->hdr
.info
.magic
, ARCH_CONVERT
, XFS_DIR2_LEAF1_MAGIC
);
1841 * Set up the leaf tail from the freespace block.
1843 ltp
= XFS_DIR2_LEAF_TAIL_P(mp
, leaf
);
1844 INT_COPY(ltp
->bestcount
, free
->hdr
.nvalid
, ARCH_CONVERT
);
1846 * Set up the leaf bests table.
1848 memcpy(XFS_DIR2_LEAF_BESTS_P(ltp
), free
->bests
,
1849 INT_GET(ltp
->bestcount
, ARCH_CONVERT
) * sizeof(leaf
->bests
[0]));
1850 xfs_dir2_leaf_log_bests(tp
, lbp
, 0, INT_GET(ltp
->bestcount
, ARCH_CONVERT
) - 1);
1851 xfs_dir2_leaf_log_tail(tp
, lbp
);
1852 xfs_dir2_leaf_check(dp
, lbp
);
1854 * Get rid of the freespace block.
1856 error
= xfs_dir2_shrink_inode(args
, XFS_DIR2_FREE_FIRSTDB(mp
), fbp
);
1859 * This can't fail here because it can only happen when
1860 * punching out the middle of an extent, and this is an
1863 ASSERT(error
!= ENOSPC
);
1868 * Now see if we can convert the single-leaf directory
1869 * down to a block form directory.
1870 * This routine always kills the dabuf for the leaf, so
1871 * eliminate it from the path.
1873 error
= xfs_dir2_leaf_to_block(args
, lbp
, NULL
);
1874 state
->path
.blk
[0].bp
= NULL
;