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"
27 #include "xfs_mount.h"
28 #include "xfs_da_btree.h"
29 #include "xfs_bmap_btree.h"
30 #include "xfs_dinode.h"
31 #include "xfs_inode.h"
33 #include "xfs_dir2_format.h"
34 #include "xfs_dir2_priv.h"
35 #include "xfs_error.h"
36 #include "xfs_trace.h"
39 * Local function declarations.
42 static void xfs_dir2_leaf_check(xfs_inode_t
*dp
, xfs_dabuf_t
*bp
);
44 #define xfs_dir2_leaf_check(dp, bp)
46 static int xfs_dir2_leaf_lookup_int(xfs_da_args_t
*args
, xfs_dabuf_t
**lbpp
,
47 int *indexp
, xfs_dabuf_t
**dbpp
);
48 static void xfs_dir2_leaf_log_bests(struct xfs_trans
*tp
, struct xfs_dabuf
*bp
,
50 static void xfs_dir2_leaf_log_tail(struct xfs_trans
*tp
, struct xfs_dabuf
*bp
);
54 * Convert a block form directory to a leaf form directory.
57 xfs_dir2_block_to_leaf(
58 xfs_da_args_t
*args
, /* operation arguments */
59 xfs_dabuf_t
*dbp
) /* input block's buffer */
61 __be16
*bestsp
; /* leaf's bestsp entries */
62 xfs_dablk_t blkno
; /* leaf block's bno */
63 xfs_dir2_data_hdr_t
*hdr
; /* block header */
64 xfs_dir2_leaf_entry_t
*blp
; /* block's leaf entries */
65 xfs_dir2_block_tail_t
*btp
; /* block's tail */
66 xfs_inode_t
*dp
; /* incore directory inode */
67 int error
; /* error return code */
68 xfs_dabuf_t
*lbp
; /* leaf block's buffer */
69 xfs_dir2_db_t ldb
; /* leaf block's bno */
70 xfs_dir2_leaf_t
*leaf
; /* leaf structure */
71 xfs_dir2_leaf_tail_t
*ltp
; /* leaf's tail */
72 xfs_mount_t
*mp
; /* filesystem mount point */
73 int needlog
; /* need to log block header */
74 int needscan
; /* need to rescan bestfree */
75 xfs_trans_t
*tp
; /* transaction pointer */
77 trace_xfs_dir2_block_to_leaf(args
);
83 * Add the leaf block to the inode.
84 * This interface will only put blocks in the leaf/node range.
85 * Since that's empty now, we'll get the root (block 0 in range).
87 if ((error
= xfs_da_grow_inode(args
, &blkno
))) {
90 ldb
= xfs_dir2_da_to_db(mp
, blkno
);
91 ASSERT(ldb
== XFS_DIR2_LEAF_FIRSTDB(mp
));
93 * Initialize the leaf block, get a buffer for it.
95 if ((error
= xfs_dir2_leaf_init(args
, ldb
, &lbp
, XFS_DIR2_LEAF1_MAGIC
))) {
101 xfs_dir2_data_check(dp
, dbp
);
102 btp
= xfs_dir2_block_tail_p(mp
, hdr
);
103 blp
= xfs_dir2_block_leaf_p(btp
);
105 * Set the counts in the leaf header.
107 leaf
->hdr
.count
= cpu_to_be16(be32_to_cpu(btp
->count
));
108 leaf
->hdr
.stale
= cpu_to_be16(be32_to_cpu(btp
->stale
));
110 * Could compact these but I think we always do the conversion
111 * after squeezing out stale entries.
113 memcpy(leaf
->ents
, blp
, be32_to_cpu(btp
->count
) * sizeof(xfs_dir2_leaf_entry_t
));
114 xfs_dir2_leaf_log_ents(tp
, lbp
, 0, be16_to_cpu(leaf
->hdr
.count
) - 1);
118 * Make the space formerly occupied by the leaf entries and block
121 xfs_dir2_data_make_free(tp
, dbp
,
122 (xfs_dir2_data_aoff_t
)((char *)blp
- (char *)hdr
),
123 (xfs_dir2_data_aoff_t
)((char *)hdr
+ mp
->m_dirblksize
-
125 &needlog
, &needscan
);
127 * Fix up the block header, make it a data block.
129 hdr
->magic
= cpu_to_be32(XFS_DIR2_DATA_MAGIC
);
131 xfs_dir2_data_freescan(mp
, hdr
, &needlog
);
133 * Set up leaf tail and bests table.
135 ltp
= xfs_dir2_leaf_tail_p(mp
, leaf
);
136 ltp
->bestcount
= cpu_to_be32(1);
137 bestsp
= xfs_dir2_leaf_bests_p(ltp
);
138 bestsp
[0] = hdr
->bestfree
[0].length
;
140 * Log the data header and leaf bests table.
143 xfs_dir2_data_log_header(tp
, dbp
);
144 xfs_dir2_leaf_check(dp
, lbp
);
145 xfs_dir2_data_check(dp
, dbp
);
146 xfs_dir2_leaf_log_bests(tp
, lbp
, 0, 0);
147 xfs_da_buf_done(lbp
);
151 struct xfs_dir2_leaf_entry
*
152 xfs_dir2_leaf_find_entry(
153 xfs_dir2_leaf_t
*leaf
, /* leaf structure */
154 int index
, /* leaf table position */
155 int compact
, /* need to compact leaves */
156 int lowstale
, /* index of prev stale leaf */
157 int highstale
, /* index of next stale leaf */
158 int *lfloglow
, /* low leaf logging index */
159 int *lfloghigh
) /* high leaf logging index */
161 if (!leaf
->hdr
.stale
) {
162 xfs_dir2_leaf_entry_t
*lep
; /* leaf entry table pointer */
165 * Now we need to make room to insert the leaf entry.
167 * If there are no stale entries, just insert a hole at index.
169 lep
= &leaf
->ents
[index
];
170 if (index
< be16_to_cpu(leaf
->hdr
.count
))
171 memmove(lep
+ 1, lep
,
172 (be16_to_cpu(leaf
->hdr
.count
) - index
) *
176 * Record low and high logging indices for the leaf.
179 *lfloghigh
= be16_to_cpu(leaf
->hdr
.count
);
180 be16_add_cpu(&leaf
->hdr
.count
, 1);
185 * There are stale entries.
187 * We will use one of them for the new entry. It's probably not at
188 * the right location, so we'll have to shift some up or down first.
190 * If we didn't compact before, we need to find the nearest stale
191 * entries before and after our insertion point.
195 * Find the first stale entry before the insertion point,
198 for (lowstale
= index
- 1;
200 leaf
->ents
[lowstale
].address
!=
201 cpu_to_be32(XFS_DIR2_NULL_DATAPTR
);
206 * Find the next stale entry at or after the insertion point,
207 * if any. Stop if we go so far that the lowstale entry
210 for (highstale
= index
;
211 highstale
< be16_to_cpu(leaf
->hdr
.count
) &&
212 leaf
->ents
[highstale
].address
!=
213 cpu_to_be32(XFS_DIR2_NULL_DATAPTR
) &&
215 index
- lowstale
- 1 >= highstale
- index
);
221 * If the low one is better, use it.
224 (highstale
== be16_to_cpu(leaf
->hdr
.count
) ||
225 index
- lowstale
- 1 < highstale
- index
)) {
226 ASSERT(index
- lowstale
- 1 >= 0);
227 ASSERT(leaf
->ents
[lowstale
].address
==
228 cpu_to_be32(XFS_DIR2_NULL_DATAPTR
));
231 * Copy entries up to cover the stale entry and make room
234 if (index
- lowstale
- 1 > 0) {
235 memmove(&leaf
->ents
[lowstale
],
236 &leaf
->ents
[lowstale
+ 1],
237 (index
- lowstale
- 1) *
238 sizeof(xfs_dir2_leaf_entry_t
));
240 *lfloglow
= MIN(lowstale
, *lfloglow
);
241 *lfloghigh
= MAX(index
- 1, *lfloghigh
);
242 be16_add_cpu(&leaf
->hdr
.stale
, -1);
243 return &leaf
->ents
[index
- 1];
247 * The high one is better, so use that one.
249 ASSERT(highstale
- index
>= 0);
250 ASSERT(leaf
->ents
[highstale
].address
==
251 cpu_to_be32(XFS_DIR2_NULL_DATAPTR
));
254 * Copy entries down to cover the stale entry and make room for the
257 if (highstale
- index
> 0) {
258 memmove(&leaf
->ents
[index
+ 1],
260 (highstale
- index
) * sizeof(xfs_dir2_leaf_entry_t
));
262 *lfloglow
= MIN(index
, *lfloglow
);
263 *lfloghigh
= MAX(highstale
, *lfloghigh
);
264 be16_add_cpu(&leaf
->hdr
.stale
, -1);
265 return &leaf
->ents
[index
];
269 * Add an entry to a leaf form directory.
272 xfs_dir2_leaf_addname(
273 xfs_da_args_t
*args
) /* operation arguments */
275 __be16
*bestsp
; /* freespace table in leaf */
276 int compact
; /* need to compact leaves */
277 xfs_dir2_data_hdr_t
*hdr
; /* data block header */
278 xfs_dabuf_t
*dbp
; /* data block buffer */
279 xfs_dir2_data_entry_t
*dep
; /* data block entry */
280 xfs_inode_t
*dp
; /* incore directory inode */
281 xfs_dir2_data_unused_t
*dup
; /* data unused entry */
282 int error
; /* error return value */
283 int grown
; /* allocated new data block */
284 int highstale
; /* index of next stale leaf */
285 int i
; /* temporary, index */
286 int index
; /* leaf table position */
287 xfs_dabuf_t
*lbp
; /* leaf's buffer */
288 xfs_dir2_leaf_t
*leaf
; /* leaf structure */
289 int length
; /* length of new entry */
290 xfs_dir2_leaf_entry_t
*lep
; /* leaf entry table pointer */
291 int lfloglow
; /* low leaf logging index */
292 int lfloghigh
; /* high leaf logging index */
293 int lowstale
; /* index of prev stale leaf */
294 xfs_dir2_leaf_tail_t
*ltp
; /* leaf tail pointer */
295 xfs_mount_t
*mp
; /* filesystem mount point */
296 int needbytes
; /* leaf block bytes needed */
297 int needlog
; /* need to log data header */
298 int needscan
; /* need to rescan data free */
299 __be16
*tagp
; /* end of data entry */
300 xfs_trans_t
*tp
; /* transaction pointer */
301 xfs_dir2_db_t use_block
; /* data block number */
303 trace_xfs_dir2_leaf_addname(args
);
309 * Read the leaf block.
311 error
= xfs_da_read_buf(tp
, dp
, mp
->m_dirleafblk
, -1, &lbp
,
318 * Look up the entry by hash value and name.
319 * We know it's not there, our caller has already done a lookup.
320 * So the index is of the entry to insert in front of.
321 * But if there are dup hash values the index is of the first of those.
323 index
= xfs_dir2_leaf_search_hash(args
, lbp
);
325 ltp
= xfs_dir2_leaf_tail_p(mp
, leaf
);
326 bestsp
= xfs_dir2_leaf_bests_p(ltp
);
327 length
= xfs_dir2_data_entsize(args
->namelen
);
329 * See if there are any entries with the same hash value
330 * and space in their block for the new entry.
331 * This is good because it puts multiple same-hash value entries
332 * in a data block, improving the lookup of those entries.
334 for (use_block
= -1, lep
= &leaf
->ents
[index
];
335 index
< be16_to_cpu(leaf
->hdr
.count
) && be32_to_cpu(lep
->hashval
) == args
->hashval
;
337 if (be32_to_cpu(lep
->address
) == XFS_DIR2_NULL_DATAPTR
)
339 i
= xfs_dir2_dataptr_to_db(mp
, be32_to_cpu(lep
->address
));
340 ASSERT(i
< be32_to_cpu(ltp
->bestcount
));
341 ASSERT(bestsp
[i
] != cpu_to_be16(NULLDATAOFF
));
342 if (be16_to_cpu(bestsp
[i
]) >= length
) {
348 * Didn't find a block yet, linear search all the data blocks.
350 if (use_block
== -1) {
351 for (i
= 0; i
< be32_to_cpu(ltp
->bestcount
); i
++) {
353 * Remember a block we see that's missing.
355 if (bestsp
[i
] == cpu_to_be16(NULLDATAOFF
) &&
358 else if (be16_to_cpu(bestsp
[i
]) >= length
) {
365 * How many bytes do we need in the leaf block?
368 if (!leaf
->hdr
.stale
)
369 needbytes
+= sizeof(xfs_dir2_leaf_entry_t
);
371 needbytes
+= sizeof(xfs_dir2_data_off_t
);
374 * Now kill use_block if it refers to a missing block, so we
375 * can use it as an indication of allocation needed.
377 if (use_block
!= -1 && bestsp
[use_block
] == cpu_to_be16(NULLDATAOFF
))
380 * If we don't have enough free bytes but we can make enough
381 * by compacting out stale entries, we'll do that.
383 if ((char *)bestsp
- (char *)&leaf
->ents
[be16_to_cpu(leaf
->hdr
.count
)] <
384 needbytes
&& be16_to_cpu(leaf
->hdr
.stale
) > 1) {
388 * Otherwise if we don't have enough free bytes we need to
389 * convert to node form.
391 else if ((char *)bestsp
- (char *)&leaf
->ents
[be16_to_cpu(
392 leaf
->hdr
.count
)] < needbytes
) {
394 * Just checking or no space reservation, give up.
396 if ((args
->op_flags
& XFS_DA_OP_JUSTCHECK
) ||
398 xfs_da_brelse(tp
, lbp
);
399 return XFS_ERROR(ENOSPC
);
402 * Convert to node form.
404 error
= xfs_dir2_leaf_to_node(args
, lbp
);
405 xfs_da_buf_done(lbp
);
409 * Then add the new entry.
411 return xfs_dir2_node_addname(args
);
414 * Otherwise it will fit without compaction.
419 * If just checking, then it will fit unless we needed to allocate
422 if (args
->op_flags
& XFS_DA_OP_JUSTCHECK
) {
423 xfs_da_brelse(tp
, lbp
);
424 return use_block
== -1 ? XFS_ERROR(ENOSPC
) : 0;
427 * If no allocations are allowed, return now before we've
430 if (args
->total
== 0 && use_block
== -1) {
431 xfs_da_brelse(tp
, lbp
);
432 return XFS_ERROR(ENOSPC
);
435 * Need to compact the leaf entries, removing stale ones.
436 * Leave one stale entry behind - the one closest to our
437 * insertion index - and we'll shift that one to our insertion
441 xfs_dir2_leaf_compact_x1(lbp
, &index
, &lowstale
, &highstale
,
442 &lfloglow
, &lfloghigh
);
445 * There are stale entries, so we'll need log-low and log-high
446 * impossibly bad values later.
448 else if (be16_to_cpu(leaf
->hdr
.stale
)) {
449 lfloglow
= be16_to_cpu(leaf
->hdr
.count
);
453 * If there was no data block space found, we need to allocate
456 if (use_block
== -1) {
458 * Add the new data block.
460 if ((error
= xfs_dir2_grow_inode(args
, XFS_DIR2_DATA_SPACE
,
462 xfs_da_brelse(tp
, lbp
);
466 * Initialize the block.
468 if ((error
= xfs_dir2_data_init(args
, use_block
, &dbp
))) {
469 xfs_da_brelse(tp
, lbp
);
473 * If we're adding a new data block on the end we need to
474 * extend the bests table. Copy it up one entry.
476 if (use_block
>= be32_to_cpu(ltp
->bestcount
)) {
478 memmove(&bestsp
[0], &bestsp
[1],
479 be32_to_cpu(ltp
->bestcount
) * sizeof(bestsp
[0]));
480 be32_add_cpu(<p
->bestcount
, 1);
481 xfs_dir2_leaf_log_tail(tp
, lbp
);
482 xfs_dir2_leaf_log_bests(tp
, lbp
, 0, be32_to_cpu(ltp
->bestcount
) - 1);
485 * If we're filling in a previously empty block just log it.
488 xfs_dir2_leaf_log_bests(tp
, lbp
, use_block
, use_block
);
490 bestsp
[use_block
] = hdr
->bestfree
[0].length
;
494 * Already had space in some data block.
495 * Just read that one in.
499 xfs_da_read_buf(tp
, dp
, xfs_dir2_db_to_da(mp
, use_block
),
500 -1, &dbp
, XFS_DATA_FORK
))) {
501 xfs_da_brelse(tp
, lbp
);
507 xfs_dir2_data_check(dp
, dbp
);
509 * Point to the biggest freespace in our data block.
511 dup
= (xfs_dir2_data_unused_t
*)
512 ((char *)hdr
+ be16_to_cpu(hdr
->bestfree
[0].offset
));
513 ASSERT(be16_to_cpu(dup
->length
) >= length
);
514 needscan
= needlog
= 0;
516 * Mark the initial part of our freespace in use for the new entry.
518 xfs_dir2_data_use_free(tp
, dbp
, dup
,
519 (xfs_dir2_data_aoff_t
)((char *)dup
- (char *)hdr
), length
,
520 &needlog
, &needscan
);
522 * Initialize our new entry (at last).
524 dep
= (xfs_dir2_data_entry_t
*)dup
;
525 dep
->inumber
= cpu_to_be64(args
->inumber
);
526 dep
->namelen
= args
->namelen
;
527 memcpy(dep
->name
, args
->name
, dep
->namelen
);
528 tagp
= xfs_dir2_data_entry_tag_p(dep
);
529 *tagp
= cpu_to_be16((char *)dep
- (char *)hdr
);
531 * Need to scan fix up the bestfree table.
534 xfs_dir2_data_freescan(mp
, hdr
, &needlog
);
536 * Need to log the data block's header.
539 xfs_dir2_data_log_header(tp
, dbp
);
540 xfs_dir2_data_log_entry(tp
, dbp
, dep
);
542 * If the bests table needs to be changed, do it.
543 * Log the change unless we've already done that.
545 if (be16_to_cpu(bestsp
[use_block
]) != be16_to_cpu(hdr
->bestfree
[0].length
)) {
546 bestsp
[use_block
] = hdr
->bestfree
[0].length
;
548 xfs_dir2_leaf_log_bests(tp
, lbp
, use_block
, use_block
);
551 lep
= xfs_dir2_leaf_find_entry(leaf
, index
, compact
, lowstale
,
552 highstale
, &lfloglow
, &lfloghigh
);
555 * Fill in the new leaf entry.
557 lep
->hashval
= cpu_to_be32(args
->hashval
);
558 lep
->address
= cpu_to_be32(xfs_dir2_db_off_to_dataptr(mp
, use_block
,
559 be16_to_cpu(*tagp
)));
561 * Log the leaf fields and give up the buffers.
563 xfs_dir2_leaf_log_header(tp
, lbp
);
564 xfs_dir2_leaf_log_ents(tp
, lbp
, lfloglow
, lfloghigh
);
565 xfs_dir2_leaf_check(dp
, lbp
);
566 xfs_da_buf_done(lbp
);
567 xfs_dir2_data_check(dp
, dbp
);
568 xfs_da_buf_done(dbp
);
574 * Check the internal consistency of a leaf1 block.
575 * Pop an assert if something is wrong.
579 xfs_inode_t
*dp
, /* incore directory inode */
580 xfs_dabuf_t
*bp
) /* leaf's buffer */
582 int i
; /* leaf index */
583 xfs_dir2_leaf_t
*leaf
; /* leaf structure */
584 xfs_dir2_leaf_tail_t
*ltp
; /* leaf tail pointer */
585 xfs_mount_t
*mp
; /* filesystem mount point */
586 int stale
; /* count of stale leaves */
590 ASSERT(leaf
->hdr
.info
.magic
== cpu_to_be16(XFS_DIR2_LEAF1_MAGIC
));
592 * This value is not restrictive enough.
593 * Should factor in the size of the bests table as well.
594 * We can deduce a value for that from di_size.
596 ASSERT(be16_to_cpu(leaf
->hdr
.count
) <= xfs_dir2_max_leaf_ents(mp
));
597 ltp
= xfs_dir2_leaf_tail_p(mp
, leaf
);
599 * Leaves and bests don't overlap.
601 ASSERT((char *)&leaf
->ents
[be16_to_cpu(leaf
->hdr
.count
)] <=
602 (char *)xfs_dir2_leaf_bests_p(ltp
));
604 * Check hash value order, count stale entries.
606 for (i
= stale
= 0; i
< be16_to_cpu(leaf
->hdr
.count
); i
++) {
607 if (i
+ 1 < be16_to_cpu(leaf
->hdr
.count
))
608 ASSERT(be32_to_cpu(leaf
->ents
[i
].hashval
) <=
609 be32_to_cpu(leaf
->ents
[i
+ 1].hashval
));
610 if (leaf
->ents
[i
].address
== cpu_to_be32(XFS_DIR2_NULL_DATAPTR
))
613 ASSERT(be16_to_cpu(leaf
->hdr
.stale
) == stale
);
618 * Compact out any stale entries in the leaf.
619 * Log the header and changed leaf entries, if any.
622 xfs_dir2_leaf_compact(
623 xfs_da_args_t
*args
, /* operation arguments */
624 xfs_dabuf_t
*bp
) /* leaf buffer */
626 int from
; /* source leaf index */
627 xfs_dir2_leaf_t
*leaf
; /* leaf structure */
628 int loglow
; /* first leaf entry to log */
629 int to
; /* target leaf index */
632 if (!leaf
->hdr
.stale
) {
636 * Compress out the stale entries in place.
638 for (from
= to
= 0, loglow
= -1; from
< be16_to_cpu(leaf
->hdr
.count
); from
++) {
639 if (leaf
->ents
[from
].address
==
640 cpu_to_be32(XFS_DIR2_NULL_DATAPTR
))
643 * Only actually copy the entries that are different.
648 leaf
->ents
[to
] = leaf
->ents
[from
];
653 * Update and log the header, log the leaf entries.
655 ASSERT(be16_to_cpu(leaf
->hdr
.stale
) == from
- to
);
656 be16_add_cpu(&leaf
->hdr
.count
, -(be16_to_cpu(leaf
->hdr
.stale
)));
658 xfs_dir2_leaf_log_header(args
->trans
, bp
);
660 xfs_dir2_leaf_log_ents(args
->trans
, bp
, loglow
, to
- 1);
664 * Compact the leaf entries, removing stale ones.
665 * Leave one stale entry behind - the one closest to our
666 * insertion index - and the caller will shift that one to our insertion
668 * Return new insertion index, where the remaining stale entry is,
669 * and leaf logging indices.
672 xfs_dir2_leaf_compact_x1(
673 xfs_dabuf_t
*bp
, /* leaf buffer */
674 int *indexp
, /* insertion index */
675 int *lowstalep
, /* out: stale entry before us */
676 int *highstalep
, /* out: stale entry after us */
677 int *lowlogp
, /* out: low log index */
678 int *highlogp
) /* out: high log index */
680 int from
; /* source copy index */
681 int highstale
; /* stale entry at/after index */
682 int index
; /* insertion index */
683 int keepstale
; /* source index of kept stale */
684 xfs_dir2_leaf_t
*leaf
; /* leaf structure */
685 int lowstale
; /* stale entry before index */
686 int newindex
=0; /* new insertion index */
687 int to
; /* destination copy index */
690 ASSERT(be16_to_cpu(leaf
->hdr
.stale
) > 1);
693 * Find the first stale entry before our index, if any.
695 for (lowstale
= index
- 1;
697 leaf
->ents
[lowstale
].address
!=
698 cpu_to_be32(XFS_DIR2_NULL_DATAPTR
);
702 * Find the first stale entry at or after our index, if any.
703 * Stop if the answer would be worse than lowstale.
705 for (highstale
= index
;
706 highstale
< be16_to_cpu(leaf
->hdr
.count
) &&
707 leaf
->ents
[highstale
].address
!=
708 cpu_to_be32(XFS_DIR2_NULL_DATAPTR
) &&
709 (lowstale
< 0 || index
- lowstale
> highstale
- index
);
713 * Pick the better of lowstale and highstale.
716 (highstale
== be16_to_cpu(leaf
->hdr
.count
) ||
717 index
- lowstale
<= highstale
- index
))
718 keepstale
= lowstale
;
720 keepstale
= highstale
;
722 * Copy the entries in place, removing all the stale entries
725 for (from
= to
= 0; from
< be16_to_cpu(leaf
->hdr
.count
); from
++) {
727 * Notice the new value of index.
731 if (from
!= keepstale
&&
732 leaf
->ents
[from
].address
==
733 cpu_to_be32(XFS_DIR2_NULL_DATAPTR
)) {
739 * Record the new keepstale value for the insertion.
741 if (from
== keepstale
)
742 lowstale
= highstale
= to
;
744 * Copy only the entries that have moved.
747 leaf
->ents
[to
] = leaf
->ents
[from
];
752 * If the insertion point was past the last entry,
753 * set the new insertion point accordingly.
759 * Adjust the leaf header values.
761 be16_add_cpu(&leaf
->hdr
.count
, -(from
- to
));
762 leaf
->hdr
.stale
= cpu_to_be16(1);
764 * Remember the low/high stale value only in the "right"
767 if (lowstale
>= newindex
)
770 highstale
= be16_to_cpu(leaf
->hdr
.count
);
771 *highlogp
= be16_to_cpu(leaf
->hdr
.count
) - 1;
772 *lowstalep
= lowstale
;
773 *highstalep
= highstale
;
777 * Getdents (readdir) for leaf and node directories.
778 * This reads the data blocks only, so is the same for both forms.
781 xfs_dir2_leaf_getdents(
782 xfs_inode_t
*dp
, /* incore directory inode */
788 xfs_dabuf_t
*bp
; /* data block buffer */
789 int byteoff
; /* offset in current block */
790 xfs_dir2_db_t curdb
; /* db for current block */
791 xfs_dir2_off_t curoff
; /* current overall offset */
792 xfs_dir2_data_hdr_t
*hdr
; /* data block header */
793 xfs_dir2_data_entry_t
*dep
; /* data entry */
794 xfs_dir2_data_unused_t
*dup
; /* unused entry */
795 int error
= 0; /* error return value */
796 int i
; /* temporary loop index */
797 int j
; /* temporary loop index */
798 int length
; /* temporary length value */
799 xfs_bmbt_irec_t
*map
; /* map vector for blocks */
800 xfs_extlen_t map_blocks
; /* number of fsbs in map */
801 xfs_dablk_t map_off
; /* last mapped file offset */
802 int map_size
; /* total entries in *map */
803 int map_valid
; /* valid entries in *map */
804 xfs_mount_t
*mp
; /* filesystem mount point */
805 xfs_dir2_off_t newoff
; /* new curoff after new blk */
806 int nmap
; /* mappings to ask xfs_bmapi */
807 char *ptr
= NULL
; /* pointer to current data */
808 int ra_current
; /* number of read-ahead blks */
809 int ra_index
; /* *map index for read-ahead */
810 int ra_offset
; /* map entry offset for ra */
811 int ra_want
; /* readahead count wanted */
814 * If the offset is at or past the largest allowed value,
815 * give up right away.
817 if (*offset
>= XFS_DIR2_MAX_DATAPTR
)
823 * Set up to bmap a number of blocks based on the caller's
824 * buffer size, the directory block size, and the filesystem
827 map_size
= howmany(bufsize
+ mp
->m_dirblksize
, mp
->m_sb
.sb_blocksize
);
828 map
= kmem_alloc(map_size
* sizeof(*map
), KM_SLEEP
);
829 map_valid
= ra_index
= ra_offset
= ra_current
= map_blocks
= 0;
833 * Inside the loop we keep the main offset value as a byte offset
834 * in the directory file.
836 curoff
= xfs_dir2_dataptr_to_byte(mp
, *offset
);
839 * Force this conversion through db so we truncate the offset
840 * down to get the start of the data block.
842 map_off
= xfs_dir2_db_to_da(mp
, xfs_dir2_byte_to_db(mp
, curoff
));
844 * Loop over directory entries until we reach the end offset.
845 * Get more blocks and readahead as necessary.
847 while (curoff
< XFS_DIR2_LEAF_OFFSET
) {
849 * If we have no buffer, or we're off the end of the
850 * current buffer, need to get another one.
852 if (!bp
|| ptr
>= (char *)bp
->data
+ mp
->m_dirblksize
) {
854 * If we have a buffer, we need to release it and
855 * take it out of the mapping.
858 xfs_da_brelse(NULL
, bp
);
860 map_blocks
-= mp
->m_dirblkfsbs
;
862 * Loop to get rid of the extents for the
865 for (i
= mp
->m_dirblkfsbs
; i
> 0; ) {
866 j
= MIN((int)map
->br_blockcount
, i
);
867 map
->br_blockcount
-= j
;
868 map
->br_startblock
+= j
;
869 map
->br_startoff
+= j
;
871 * If mapping is done, pitch it from
874 if (!map
->br_blockcount
&& --map_valid
)
875 memmove(&map
[0], &map
[1],
882 * Recalculate the readahead blocks wanted.
884 ra_want
= howmany(bufsize
+ mp
->m_dirblksize
,
885 mp
->m_sb
.sb_blocksize
) - 1;
886 ASSERT(ra_want
>= 0);
889 * If we don't have as many as we want, and we haven't
890 * run out of data blocks, get some more mappings.
892 if (1 + ra_want
> map_blocks
&&
894 xfs_dir2_byte_to_da(mp
, XFS_DIR2_LEAF_OFFSET
)) {
896 * Get more bmaps, fill in after the ones
897 * we already have in the table.
899 nmap
= map_size
- map_valid
;
900 error
= xfs_bmapi(NULL
, dp
,
902 xfs_dir2_byte_to_da(mp
,
903 XFS_DIR2_LEAF_OFFSET
) - map_off
,
904 XFS_BMAPI_METADATA
, NULL
, 0,
905 &map
[map_valid
], &nmap
, NULL
);
907 * Don't know if we should ignore this or
908 * try to return an error.
909 * The trouble with returning errors
910 * is that readdir will just stop without
911 * actually passing the error through.
916 * If we got all the mappings we asked for,
917 * set the final map offset based on the
918 * last bmap value received.
919 * Otherwise, we've reached the end.
921 if (nmap
== map_size
- map_valid
)
923 map
[map_valid
+ nmap
- 1].br_startoff
+
924 map
[map_valid
+ nmap
- 1].br_blockcount
;
927 xfs_dir2_byte_to_da(mp
,
928 XFS_DIR2_LEAF_OFFSET
);
930 * Look for holes in the mapping, and
931 * eliminate them. Count up the valid blocks.
933 for (i
= map_valid
; i
< map_valid
+ nmap
; ) {
934 if (map
[i
].br_startblock
==
937 length
= map_valid
+ nmap
- i
;
945 map
[i
].br_blockcount
;
952 * No valid mappings, so no more data blocks.
955 curoff
= xfs_dir2_da_to_byte(mp
, map_off
);
959 * Read the directory block starting at the first
962 curdb
= xfs_dir2_da_to_db(mp
, map
->br_startoff
);
963 error
= xfs_da_read_buf(NULL
, dp
, map
->br_startoff
,
964 map
->br_blockcount
>= mp
->m_dirblkfsbs
?
965 XFS_FSB_TO_DADDR(mp
, map
->br_startblock
) :
969 * Should just skip over the data block instead
975 * Adjust the current amount of read-ahead: we just
976 * read a block that was previously ra.
979 ra_current
-= mp
->m_dirblkfsbs
;
981 * Do we need more readahead?
983 for (ra_index
= ra_offset
= i
= 0;
984 ra_want
> ra_current
&& i
< map_blocks
;
985 i
+= mp
->m_dirblkfsbs
) {
986 ASSERT(ra_index
< map_valid
);
988 * Read-ahead a contiguous directory block.
990 if (i
> ra_current
&&
991 map
[ra_index
].br_blockcount
>=
993 xfs_buf_readahead(mp
->m_ddev_targp
,
995 map
[ra_index
].br_startblock
+
997 (int)BTOBB(mp
->m_dirblksize
));
1001 * Read-ahead a non-contiguous directory block.
1002 * This doesn't use our mapping, but this
1003 * is a very rare case.
1005 else if (i
> ra_current
) {
1006 (void)xfs_da_reada_buf(NULL
, dp
,
1007 map
[ra_index
].br_startoff
+
1008 ra_offset
, XFS_DATA_FORK
);
1012 * Advance offset through the mapping table.
1014 for (j
= 0; j
< mp
->m_dirblkfsbs
; j
++) {
1016 * The rest of this extent but not
1017 * more than a dir block.
1019 length
= MIN(mp
->m_dirblkfsbs
,
1020 (int)(map
[ra_index
].br_blockcount
-
1023 ra_offset
+= length
;
1025 * Advance to the next mapping if
1026 * this one is used up.
1029 map
[ra_index
].br_blockcount
) {
1036 * Having done a read, we need to set a new offset.
1038 newoff
= xfs_dir2_db_off_to_byte(mp
, curdb
, 0);
1040 * Start of the current block.
1042 if (curoff
< newoff
)
1045 * Make sure we're in the right block.
1047 else if (curoff
> newoff
)
1048 ASSERT(xfs_dir2_byte_to_db(mp
, curoff
) ==
1051 xfs_dir2_data_check(dp
, bp
);
1053 * Find our position in the block.
1055 ptr
= (char *)(hdr
+ 1);
1056 byteoff
= xfs_dir2_byte_to_off(mp
, curoff
);
1058 * Skip past the header.
1061 curoff
+= (uint
)sizeof(*hdr
);
1063 * Skip past entries until we reach our offset.
1066 while ((char *)ptr
- (char *)hdr
< byteoff
) {
1067 dup
= (xfs_dir2_data_unused_t
*)ptr
;
1069 if (be16_to_cpu(dup
->freetag
)
1070 == XFS_DIR2_DATA_FREE_TAG
) {
1072 length
= be16_to_cpu(dup
->length
);
1076 dep
= (xfs_dir2_data_entry_t
*)ptr
;
1078 xfs_dir2_data_entsize(dep
->namelen
);
1082 * Now set our real offset.
1085 xfs_dir2_db_off_to_byte(mp
,
1086 xfs_dir2_byte_to_db(mp
, curoff
),
1087 (char *)ptr
- (char *)hdr
);
1088 if (ptr
>= (char *)hdr
+ mp
->m_dirblksize
) {
1094 * We have a pointer to an entry.
1097 dup
= (xfs_dir2_data_unused_t
*)ptr
;
1099 * No, it's unused, skip over it.
1101 if (be16_to_cpu(dup
->freetag
) == XFS_DIR2_DATA_FREE_TAG
) {
1102 length
= be16_to_cpu(dup
->length
);
1108 dep
= (xfs_dir2_data_entry_t
*)ptr
;
1109 length
= xfs_dir2_data_entsize(dep
->namelen
);
1111 if (filldir(dirent
, (char *)dep
->name
, dep
->namelen
,
1112 xfs_dir2_byte_to_dataptr(mp
, curoff
) & 0x7fffffff,
1113 be64_to_cpu(dep
->inumber
), DT_UNKNOWN
))
1117 * Advance to next entry in the block.
1121 /* bufsize may have just been a guess; don't go negative */
1122 bufsize
= bufsize
> length
? bufsize
- length
: 0;
1126 * All done. Set output offset value to current offset.
1128 if (curoff
> xfs_dir2_dataptr_to_byte(mp
, XFS_DIR2_MAX_DATAPTR
))
1129 *offset
= XFS_DIR2_MAX_DATAPTR
& 0x7fffffff;
1131 *offset
= xfs_dir2_byte_to_dataptr(mp
, curoff
) & 0x7fffffff;
1134 xfs_da_brelse(NULL
, bp
);
1139 * Initialize a new leaf block, leaf1 or leafn magic accepted.
1143 xfs_da_args_t
*args
, /* operation arguments */
1144 xfs_dir2_db_t bno
, /* directory block number */
1145 xfs_dabuf_t
**bpp
, /* out: leaf buffer */
1146 int magic
) /* magic number for block */
1148 xfs_dabuf_t
*bp
; /* leaf buffer */
1149 xfs_inode_t
*dp
; /* incore directory inode */
1150 int error
; /* error return code */
1151 xfs_dir2_leaf_t
*leaf
; /* leaf structure */
1152 xfs_dir2_leaf_tail_t
*ltp
; /* leaf tail structure */
1153 xfs_mount_t
*mp
; /* filesystem mount point */
1154 xfs_trans_t
*tp
; /* transaction pointer */
1160 ASSERT(bno
>= XFS_DIR2_LEAF_FIRSTDB(mp
) &&
1161 bno
< XFS_DIR2_FREE_FIRSTDB(mp
));
1163 * Get the buffer for the block.
1165 error
= xfs_da_get_buf(tp
, dp
, xfs_dir2_db_to_da(mp
, bno
), -1, &bp
,
1173 * Initialize the header.
1175 leaf
->hdr
.info
.magic
= cpu_to_be16(magic
);
1176 leaf
->hdr
.info
.forw
= 0;
1177 leaf
->hdr
.info
.back
= 0;
1178 leaf
->hdr
.count
= 0;
1179 leaf
->hdr
.stale
= 0;
1180 xfs_dir2_leaf_log_header(tp
, bp
);
1182 * If it's a leaf-format directory initialize the tail.
1183 * In this case our caller has the real bests table to copy into
1186 if (magic
== XFS_DIR2_LEAF1_MAGIC
) {
1187 ltp
= xfs_dir2_leaf_tail_p(mp
, leaf
);
1189 xfs_dir2_leaf_log_tail(tp
, bp
);
1196 * Log the bests entries indicated from a leaf1 block.
1199 xfs_dir2_leaf_log_bests(
1200 xfs_trans_t
*tp
, /* transaction pointer */
1201 xfs_dabuf_t
*bp
, /* leaf buffer */
1202 int first
, /* first entry to log */
1203 int last
) /* last entry to log */
1205 __be16
*firstb
; /* pointer to first entry */
1206 __be16
*lastb
; /* pointer to last entry */
1207 xfs_dir2_leaf_t
*leaf
; /* leaf structure */
1208 xfs_dir2_leaf_tail_t
*ltp
; /* leaf tail structure */
1211 ASSERT(leaf
->hdr
.info
.magic
== cpu_to_be16(XFS_DIR2_LEAF1_MAGIC
));
1212 ltp
= xfs_dir2_leaf_tail_p(tp
->t_mountp
, leaf
);
1213 firstb
= xfs_dir2_leaf_bests_p(ltp
) + first
;
1214 lastb
= xfs_dir2_leaf_bests_p(ltp
) + last
;
1215 xfs_da_log_buf(tp
, bp
, (uint
)((char *)firstb
- (char *)leaf
),
1216 (uint
)((char *)lastb
- (char *)leaf
+ sizeof(*lastb
) - 1));
1220 * Log the leaf entries indicated from a leaf1 or leafn block.
1223 xfs_dir2_leaf_log_ents(
1224 xfs_trans_t
*tp
, /* transaction pointer */
1225 xfs_dabuf_t
*bp
, /* leaf buffer */
1226 int first
, /* first entry to log */
1227 int last
) /* last entry to log */
1229 xfs_dir2_leaf_entry_t
*firstlep
; /* pointer to first entry */
1230 xfs_dir2_leaf_entry_t
*lastlep
; /* pointer to last entry */
1231 xfs_dir2_leaf_t
*leaf
; /* leaf structure */
1234 ASSERT(leaf
->hdr
.info
.magic
== cpu_to_be16(XFS_DIR2_LEAF1_MAGIC
) ||
1235 leaf
->hdr
.info
.magic
== cpu_to_be16(XFS_DIR2_LEAFN_MAGIC
));
1236 firstlep
= &leaf
->ents
[first
];
1237 lastlep
= &leaf
->ents
[last
];
1238 xfs_da_log_buf(tp
, bp
, (uint
)((char *)firstlep
- (char *)leaf
),
1239 (uint
)((char *)lastlep
- (char *)leaf
+ sizeof(*lastlep
) - 1));
1243 * Log the header of the leaf1 or leafn block.
1246 xfs_dir2_leaf_log_header(
1247 xfs_trans_t
*tp
, /* transaction pointer */
1248 xfs_dabuf_t
*bp
) /* leaf buffer */
1250 xfs_dir2_leaf_t
*leaf
; /* leaf structure */
1253 ASSERT(leaf
->hdr
.info
.magic
== cpu_to_be16(XFS_DIR2_LEAF1_MAGIC
) ||
1254 leaf
->hdr
.info
.magic
== cpu_to_be16(XFS_DIR2_LEAFN_MAGIC
));
1255 xfs_da_log_buf(tp
, bp
, (uint
)((char *)&leaf
->hdr
- (char *)leaf
),
1256 (uint
)(sizeof(leaf
->hdr
) - 1));
1260 * Log the tail of the leaf1 block.
1263 xfs_dir2_leaf_log_tail(
1264 xfs_trans_t
*tp
, /* transaction pointer */
1265 xfs_dabuf_t
*bp
) /* leaf buffer */
1267 xfs_dir2_leaf_t
*leaf
; /* leaf structure */
1268 xfs_dir2_leaf_tail_t
*ltp
; /* leaf tail structure */
1269 xfs_mount_t
*mp
; /* filesystem mount point */
1273 ASSERT(leaf
->hdr
.info
.magic
== cpu_to_be16(XFS_DIR2_LEAF1_MAGIC
));
1274 ltp
= xfs_dir2_leaf_tail_p(mp
, leaf
);
1275 xfs_da_log_buf(tp
, bp
, (uint
)((char *)ltp
- (char *)leaf
),
1276 (uint
)(mp
->m_dirblksize
- 1));
1280 * Look up the entry referred to by args in the leaf format directory.
1281 * Most of the work is done by the xfs_dir2_leaf_lookup_int routine which
1282 * is also used by the node-format code.
1285 xfs_dir2_leaf_lookup(
1286 xfs_da_args_t
*args
) /* operation arguments */
1288 xfs_dabuf_t
*dbp
; /* data block buffer */
1289 xfs_dir2_data_entry_t
*dep
; /* data block entry */
1290 xfs_inode_t
*dp
; /* incore directory inode */
1291 int error
; /* error return code */
1292 int index
; /* found entry index */
1293 xfs_dabuf_t
*lbp
; /* leaf buffer */
1294 xfs_dir2_leaf_t
*leaf
; /* leaf structure */
1295 xfs_dir2_leaf_entry_t
*lep
; /* leaf entry */
1296 xfs_trans_t
*tp
; /* transaction pointer */
1298 trace_xfs_dir2_leaf_lookup(args
);
1301 * Look up name in the leaf block, returning both buffers and index.
1303 if ((error
= xfs_dir2_leaf_lookup_int(args
, &lbp
, &index
, &dbp
))) {
1308 xfs_dir2_leaf_check(dp
, lbp
);
1311 * Get to the leaf entry and contained data entry address.
1313 lep
= &leaf
->ents
[index
];
1315 * Point to the data entry.
1317 dep
= (xfs_dir2_data_entry_t
*)
1318 ((char *)dbp
->data
+
1319 xfs_dir2_dataptr_to_off(dp
->i_mount
, be32_to_cpu(lep
->address
)));
1321 * Return the found inode number & CI name if appropriate
1323 args
->inumber
= be64_to_cpu(dep
->inumber
);
1324 error
= xfs_dir_cilookup_result(args
, dep
->name
, dep
->namelen
);
1325 xfs_da_brelse(tp
, dbp
);
1326 xfs_da_brelse(tp
, lbp
);
1327 return XFS_ERROR(error
);
1331 * Look up name/hash in the leaf block.
1332 * Fill in indexp with the found index, and dbpp with the data buffer.
1333 * If not found dbpp will be NULL, and ENOENT comes back.
1334 * lbpp will always be filled in with the leaf buffer unless there's an error.
1336 static int /* error */
1337 xfs_dir2_leaf_lookup_int(
1338 xfs_da_args_t
*args
, /* operation arguments */
1339 xfs_dabuf_t
**lbpp
, /* out: leaf buffer */
1340 int *indexp
, /* out: index in leaf block */
1341 xfs_dabuf_t
**dbpp
) /* out: data buffer */
1343 xfs_dir2_db_t curdb
= -1; /* current data block number */
1344 xfs_dabuf_t
*dbp
= NULL
; /* data buffer */
1345 xfs_dir2_data_entry_t
*dep
; /* data entry */
1346 xfs_inode_t
*dp
; /* incore directory inode */
1347 int error
; /* error return code */
1348 int index
; /* index in leaf block */
1349 xfs_dabuf_t
*lbp
; /* leaf buffer */
1350 xfs_dir2_leaf_entry_t
*lep
; /* leaf entry */
1351 xfs_dir2_leaf_t
*leaf
; /* leaf structure */
1352 xfs_mount_t
*mp
; /* filesystem mount point */
1353 xfs_dir2_db_t newdb
; /* new data block number */
1354 xfs_trans_t
*tp
; /* transaction pointer */
1355 xfs_dir2_db_t cidb
= -1; /* case match data block no. */
1356 enum xfs_dacmp cmp
; /* name compare result */
1362 * Read the leaf block into the buffer.
1364 error
= xfs_da_read_buf(tp
, dp
, mp
->m_dirleafblk
, -1, &lbp
,
1370 xfs_dir2_leaf_check(dp
, lbp
);
1372 * Look for the first leaf entry with our hash value.
1374 index
= xfs_dir2_leaf_search_hash(args
, lbp
);
1376 * Loop over all the entries with the right hash value
1377 * looking to match the name.
1379 for (lep
= &leaf
->ents
[index
]; index
< be16_to_cpu(leaf
->hdr
.count
) &&
1380 be32_to_cpu(lep
->hashval
) == args
->hashval
;
1383 * Skip over stale leaf entries.
1385 if (be32_to_cpu(lep
->address
) == XFS_DIR2_NULL_DATAPTR
)
1388 * Get the new data block number.
1390 newdb
= xfs_dir2_dataptr_to_db(mp
, be32_to_cpu(lep
->address
));
1392 * If it's not the same as the old data block number,
1393 * need to pitch the old one and read the new one.
1395 if (newdb
!= curdb
) {
1397 xfs_da_brelse(tp
, dbp
);
1398 error
= xfs_da_read_buf(tp
, dp
,
1399 xfs_dir2_db_to_da(mp
, newdb
),
1400 -1, &dbp
, XFS_DATA_FORK
);
1402 xfs_da_brelse(tp
, lbp
);
1405 xfs_dir2_data_check(dp
, dbp
);
1409 * Point to the data entry.
1411 dep
= (xfs_dir2_data_entry_t
*)((char *)dbp
->data
+
1412 xfs_dir2_dataptr_to_off(mp
, be32_to_cpu(lep
->address
)));
1414 * Compare name and if it's an exact match, return the index
1415 * and buffer. If it's the first case-insensitive match, store
1416 * the index and buffer and continue looking for an exact match.
1418 cmp
= mp
->m_dirnameops
->compname(args
, dep
->name
, dep
->namelen
);
1419 if (cmp
!= XFS_CMP_DIFFERENT
&& cmp
!= args
->cmpresult
) {
1420 args
->cmpresult
= cmp
;
1422 /* case exact match: return the current buffer. */
1423 if (cmp
== XFS_CMP_EXACT
) {
1430 ASSERT(args
->op_flags
& XFS_DA_OP_OKNOENT
);
1432 * Here, we can only be doing a lookup (not a rename or remove).
1433 * If a case-insensitive match was found earlier, re-read the
1434 * appropriate data block if required and return it.
1436 if (args
->cmpresult
== XFS_CMP_CASE
) {
1438 if (cidb
!= curdb
) {
1439 xfs_da_brelse(tp
, dbp
);
1440 error
= xfs_da_read_buf(tp
, dp
,
1441 xfs_dir2_db_to_da(mp
, cidb
),
1442 -1, &dbp
, XFS_DATA_FORK
);
1444 xfs_da_brelse(tp
, lbp
);
1452 * No match found, return ENOENT.
1456 xfs_da_brelse(tp
, dbp
);
1457 xfs_da_brelse(tp
, lbp
);
1458 return XFS_ERROR(ENOENT
);
1462 * Remove an entry from a leaf format directory.
1465 xfs_dir2_leaf_removename(
1466 xfs_da_args_t
*args
) /* operation arguments */
1468 __be16
*bestsp
; /* leaf block best freespace */
1469 xfs_dir2_data_hdr_t
*hdr
; /* data block header */
1470 xfs_dir2_db_t db
; /* data block number */
1471 xfs_dabuf_t
*dbp
; /* data block buffer */
1472 xfs_dir2_data_entry_t
*dep
; /* data entry structure */
1473 xfs_inode_t
*dp
; /* incore directory inode */
1474 int error
; /* error return code */
1475 xfs_dir2_db_t i
; /* temporary data block # */
1476 int index
; /* index into leaf entries */
1477 xfs_dabuf_t
*lbp
; /* leaf buffer */
1478 xfs_dir2_leaf_t
*leaf
; /* leaf structure */
1479 xfs_dir2_leaf_entry_t
*lep
; /* leaf entry */
1480 xfs_dir2_leaf_tail_t
*ltp
; /* leaf tail structure */
1481 xfs_mount_t
*mp
; /* filesystem mount point */
1482 int needlog
; /* need to log data header */
1483 int needscan
; /* need to rescan data frees */
1484 xfs_dir2_data_off_t oldbest
; /* old value of best free */
1485 xfs_trans_t
*tp
; /* transaction pointer */
1487 trace_xfs_dir2_leaf_removename(args
);
1490 * Lookup the leaf entry, get the leaf and data blocks read in.
1492 if ((error
= xfs_dir2_leaf_lookup_int(args
, &lbp
, &index
, &dbp
))) {
1500 xfs_dir2_data_check(dp
, dbp
);
1502 * Point to the leaf entry, use that to point to the data entry.
1504 lep
= &leaf
->ents
[index
];
1505 db
= xfs_dir2_dataptr_to_db(mp
, be32_to_cpu(lep
->address
));
1506 dep
= (xfs_dir2_data_entry_t
*)
1507 ((char *)hdr
+ xfs_dir2_dataptr_to_off(mp
, be32_to_cpu(lep
->address
)));
1508 needscan
= needlog
= 0;
1509 oldbest
= be16_to_cpu(hdr
->bestfree
[0].length
);
1510 ltp
= xfs_dir2_leaf_tail_p(mp
, leaf
);
1511 bestsp
= xfs_dir2_leaf_bests_p(ltp
);
1512 ASSERT(be16_to_cpu(bestsp
[db
]) == oldbest
);
1514 * Mark the former data entry unused.
1516 xfs_dir2_data_make_free(tp
, dbp
,
1517 (xfs_dir2_data_aoff_t
)((char *)dep
- (char *)hdr
),
1518 xfs_dir2_data_entsize(dep
->namelen
), &needlog
, &needscan
);
1520 * We just mark the leaf entry stale by putting a null in it.
1522 be16_add_cpu(&leaf
->hdr
.stale
, 1);
1523 xfs_dir2_leaf_log_header(tp
, lbp
);
1524 lep
->address
= cpu_to_be32(XFS_DIR2_NULL_DATAPTR
);
1525 xfs_dir2_leaf_log_ents(tp
, lbp
, index
, index
);
1527 * Scan the freespace in the data block again if necessary,
1528 * log the data block header if necessary.
1531 xfs_dir2_data_freescan(mp
, hdr
, &needlog
);
1533 xfs_dir2_data_log_header(tp
, dbp
);
1535 * If the longest freespace in the data block has changed,
1536 * put the new value in the bests table and log that.
1538 if (be16_to_cpu(hdr
->bestfree
[0].length
) != oldbest
) {
1539 bestsp
[db
] = hdr
->bestfree
[0].length
;
1540 xfs_dir2_leaf_log_bests(tp
, lbp
, db
, db
);
1542 xfs_dir2_data_check(dp
, dbp
);
1544 * If the data block is now empty then get rid of the data block.
1546 if (be16_to_cpu(hdr
->bestfree
[0].length
) ==
1547 mp
->m_dirblksize
- (uint
)sizeof(*hdr
)) {
1548 ASSERT(db
!= mp
->m_dirdatablk
);
1549 if ((error
= xfs_dir2_shrink_inode(args
, db
, dbp
))) {
1551 * Nope, can't get rid of it because it caused
1552 * allocation of a bmap btree block to do so.
1553 * Just go on, returning success, leaving the
1554 * empty block in place.
1556 if (error
== ENOSPC
&& args
->total
== 0) {
1557 xfs_da_buf_done(dbp
);
1560 xfs_dir2_leaf_check(dp
, lbp
);
1561 xfs_da_buf_done(lbp
);
1566 * If this is the last data block then compact the
1567 * bests table by getting rid of entries.
1569 if (db
== be32_to_cpu(ltp
->bestcount
) - 1) {
1571 * Look for the last active entry (i).
1573 for (i
= db
- 1; i
> 0; i
--) {
1574 if (bestsp
[i
] != cpu_to_be16(NULLDATAOFF
))
1578 * Copy the table down so inactive entries at the
1581 memmove(&bestsp
[db
- i
], bestsp
,
1582 (be32_to_cpu(ltp
->bestcount
) - (db
- i
)) * sizeof(*bestsp
));
1583 be32_add_cpu(<p
->bestcount
, -(db
- i
));
1584 xfs_dir2_leaf_log_tail(tp
, lbp
);
1585 xfs_dir2_leaf_log_bests(tp
, lbp
, 0, be32_to_cpu(ltp
->bestcount
) - 1);
1587 bestsp
[db
] = cpu_to_be16(NULLDATAOFF
);
1590 * If the data block was not the first one, drop it.
1592 else if (db
!= mp
->m_dirdatablk
&& dbp
!= NULL
) {
1593 xfs_da_buf_done(dbp
);
1596 xfs_dir2_leaf_check(dp
, lbp
);
1598 * See if we can convert to block form.
1600 return xfs_dir2_leaf_to_block(args
, lbp
, dbp
);
1604 * Replace the inode number in a leaf format directory entry.
1607 xfs_dir2_leaf_replace(
1608 xfs_da_args_t
*args
) /* operation arguments */
1610 xfs_dabuf_t
*dbp
; /* data block buffer */
1611 xfs_dir2_data_entry_t
*dep
; /* data block entry */
1612 xfs_inode_t
*dp
; /* incore directory inode */
1613 int error
; /* error return code */
1614 int index
; /* index of leaf entry */
1615 xfs_dabuf_t
*lbp
; /* leaf buffer */
1616 xfs_dir2_leaf_t
*leaf
; /* leaf structure */
1617 xfs_dir2_leaf_entry_t
*lep
; /* leaf entry */
1618 xfs_trans_t
*tp
; /* transaction pointer */
1620 trace_xfs_dir2_leaf_replace(args
);
1623 * Look up the entry.
1625 if ((error
= xfs_dir2_leaf_lookup_int(args
, &lbp
, &index
, &dbp
))) {
1631 * Point to the leaf entry, get data address from it.
1633 lep
= &leaf
->ents
[index
];
1635 * Point to the data entry.
1637 dep
= (xfs_dir2_data_entry_t
*)
1638 ((char *)dbp
->data
+
1639 xfs_dir2_dataptr_to_off(dp
->i_mount
, be32_to_cpu(lep
->address
)));
1640 ASSERT(args
->inumber
!= be64_to_cpu(dep
->inumber
));
1642 * Put the new inode number in, log it.
1644 dep
->inumber
= cpu_to_be64(args
->inumber
);
1646 xfs_dir2_data_log_entry(tp
, dbp
, dep
);
1647 xfs_da_buf_done(dbp
);
1648 xfs_dir2_leaf_check(dp
, lbp
);
1649 xfs_da_brelse(tp
, lbp
);
1654 * Return index in the leaf block (lbp) which is either the first
1655 * one with this hash value, or if there are none, the insert point
1656 * for that hash value.
1658 int /* index value */
1659 xfs_dir2_leaf_search_hash(
1660 xfs_da_args_t
*args
, /* operation arguments */
1661 xfs_dabuf_t
*lbp
) /* leaf buffer */
1663 xfs_dahash_t hash
=0; /* hash from this entry */
1664 xfs_dahash_t hashwant
; /* hash value looking for */
1665 int high
; /* high leaf index */
1666 int low
; /* low leaf index */
1667 xfs_dir2_leaf_t
*leaf
; /* leaf structure */
1668 xfs_dir2_leaf_entry_t
*lep
; /* leaf entry */
1669 int mid
=0; /* current leaf index */
1673 if (!leaf
->hdr
.count
)
1677 * Note, the table cannot be empty, so we have to go through the loop.
1678 * Binary search the leaf entries looking for our hash value.
1680 for (lep
= leaf
->ents
, low
= 0, high
= be16_to_cpu(leaf
->hdr
.count
) - 1,
1681 hashwant
= args
->hashval
;
1683 mid
= (low
+ high
) >> 1;
1684 if ((hash
= be32_to_cpu(lep
[mid
].hashval
)) == hashwant
)
1686 if (hash
< hashwant
)
1692 * Found one, back up through all the equal hash values.
1694 if (hash
== hashwant
) {
1695 while (mid
> 0 && be32_to_cpu(lep
[mid
- 1].hashval
) == hashwant
) {
1700 * Need to point to an entry higher than ours.
1702 else if (hash
< hashwant
)
1708 * Trim off a trailing data block. We know it's empty since the leaf
1709 * freespace table says so.
1712 xfs_dir2_leaf_trim_data(
1713 xfs_da_args_t
*args
, /* operation arguments */
1714 xfs_dabuf_t
*lbp
, /* leaf buffer */
1715 xfs_dir2_db_t db
) /* data block number */
1717 __be16
*bestsp
; /* leaf bests table */
1718 xfs_dabuf_t
*dbp
; /* data block buffer */
1719 xfs_inode_t
*dp
; /* incore directory inode */
1720 int error
; /* error return value */
1721 xfs_dir2_leaf_t
*leaf
; /* leaf structure */
1722 xfs_dir2_leaf_tail_t
*ltp
; /* leaf tail structure */
1723 xfs_mount_t
*mp
; /* filesystem mount point */
1724 xfs_trans_t
*tp
; /* transaction pointer */
1730 * Read the offending data block. We need its buffer.
1732 if ((error
= xfs_da_read_buf(tp
, dp
, xfs_dir2_db_to_da(mp
, db
), -1, &dbp
,
1738 ltp
= xfs_dir2_leaf_tail_p(mp
, leaf
);
1742 struct xfs_dir2_data_hdr
*hdr
= dbp
->data
;
1744 ASSERT(hdr
->magic
== cpu_to_be32(XFS_DIR2_DATA_MAGIC
));
1745 ASSERT(be16_to_cpu(hdr
->bestfree
[0].length
) ==
1746 mp
->m_dirblksize
- (uint
)sizeof(*hdr
));
1747 ASSERT(db
== be32_to_cpu(ltp
->bestcount
) - 1);
1752 * Get rid of the data block.
1754 if ((error
= xfs_dir2_shrink_inode(args
, db
, dbp
))) {
1755 ASSERT(error
!= ENOSPC
);
1756 xfs_da_brelse(tp
, dbp
);
1760 * Eliminate the last bests entry from the table.
1762 bestsp
= xfs_dir2_leaf_bests_p(ltp
);
1763 be32_add_cpu(<p
->bestcount
, -1);
1764 memmove(&bestsp
[1], &bestsp
[0], be32_to_cpu(ltp
->bestcount
) * sizeof(*bestsp
));
1765 xfs_dir2_leaf_log_tail(tp
, lbp
);
1766 xfs_dir2_leaf_log_bests(tp
, lbp
, 0, be32_to_cpu(ltp
->bestcount
) - 1);
1770 static inline size_t
1772 struct xfs_dir2_leaf_hdr
*hdr
,
1777 entries
= be16_to_cpu(hdr
->count
) - be16_to_cpu(hdr
->stale
);
1778 return sizeof(xfs_dir2_leaf_hdr_t
) +
1779 entries
* sizeof(xfs_dir2_leaf_entry_t
) +
1780 counts
* sizeof(xfs_dir2_data_off_t
) +
1781 sizeof(xfs_dir2_leaf_tail_t
);
1785 * Convert node form directory to leaf form directory.
1786 * The root of the node form dir needs to already be a LEAFN block.
1787 * Just return if we can't do anything.
1790 xfs_dir2_node_to_leaf(
1791 xfs_da_state_t
*state
) /* directory operation state */
1793 xfs_da_args_t
*args
; /* operation arguments */
1794 xfs_inode_t
*dp
; /* incore directory inode */
1795 int error
; /* error return code */
1796 xfs_dabuf_t
*fbp
; /* buffer for freespace block */
1797 xfs_fileoff_t fo
; /* freespace file offset */
1798 xfs_dir2_free_t
*free
; /* freespace structure */
1799 xfs_dabuf_t
*lbp
; /* buffer for leaf block */
1800 xfs_dir2_leaf_tail_t
*ltp
; /* tail of leaf structure */
1801 xfs_dir2_leaf_t
*leaf
; /* leaf structure */
1802 xfs_mount_t
*mp
; /* filesystem mount point */
1803 int rval
; /* successful free trim? */
1804 xfs_trans_t
*tp
; /* transaction pointer */
1807 * There's more than a leaf level in the btree, so there must
1808 * be multiple leafn blocks. Give up.
1810 if (state
->path
.active
> 1)
1814 trace_xfs_dir2_node_to_leaf(args
);
1820 * Get the last offset in the file.
1822 if ((error
= xfs_bmap_last_offset(tp
, dp
, &fo
, XFS_DATA_FORK
))) {
1825 fo
-= mp
->m_dirblkfsbs
;
1827 * If there are freespace blocks other than the first one,
1828 * take this opportunity to remove trailing empty freespace blocks
1829 * that may have been left behind during no-space-reservation
1832 while (fo
> mp
->m_dirfreeblk
) {
1833 if ((error
= xfs_dir2_node_trim_free(args
, fo
, &rval
))) {
1837 fo
-= mp
->m_dirblkfsbs
;
1842 * Now find the block just before the freespace block.
1844 if ((error
= xfs_bmap_last_before(tp
, dp
, &fo
, XFS_DATA_FORK
))) {
1848 * If it's not the single leaf block, give up.
1850 if (XFS_FSB_TO_B(mp
, fo
) > XFS_DIR2_LEAF_OFFSET
+ mp
->m_dirblksize
)
1852 lbp
= state
->path
.blk
[0].bp
;
1854 ASSERT(leaf
->hdr
.info
.magic
== cpu_to_be16(XFS_DIR2_LEAFN_MAGIC
));
1856 * Read the freespace block.
1858 if ((error
= xfs_da_read_buf(tp
, dp
, mp
->m_dirfreeblk
, -1, &fbp
,
1863 ASSERT(free
->hdr
.magic
== cpu_to_be32(XFS_DIR2_FREE_MAGIC
));
1864 ASSERT(!free
->hdr
.firstdb
);
1867 * Now see if the leafn and free data will fit in a leaf1.
1868 * If not, release the buffer and give up.
1870 if (xfs_dir2_leaf_size(&leaf
->hdr
, be32_to_cpu(free
->hdr
.nvalid
)) >
1872 xfs_da_brelse(tp
, fbp
);
1877 * If the leaf has any stale entries in it, compress them out.
1878 * The compact routine will log the header.
1880 if (be16_to_cpu(leaf
->hdr
.stale
))
1881 xfs_dir2_leaf_compact(args
, lbp
);
1883 xfs_dir2_leaf_log_header(tp
, lbp
);
1884 leaf
->hdr
.info
.magic
= cpu_to_be16(XFS_DIR2_LEAF1_MAGIC
);
1886 * Set up the leaf tail from the freespace block.
1888 ltp
= xfs_dir2_leaf_tail_p(mp
, leaf
);
1889 ltp
->bestcount
= free
->hdr
.nvalid
;
1891 * Set up the leaf bests table.
1893 memcpy(xfs_dir2_leaf_bests_p(ltp
), free
->bests
,
1894 be32_to_cpu(ltp
->bestcount
) * sizeof(xfs_dir2_data_off_t
));
1895 xfs_dir2_leaf_log_bests(tp
, lbp
, 0, be32_to_cpu(ltp
->bestcount
) - 1);
1896 xfs_dir2_leaf_log_tail(tp
, lbp
);
1897 xfs_dir2_leaf_check(dp
, lbp
);
1899 * Get rid of the freespace block.
1901 error
= xfs_dir2_shrink_inode(args
, XFS_DIR2_FREE_FIRSTDB(mp
), fbp
);
1904 * This can't fail here because it can only happen when
1905 * punching out the middle of an extent, and this is an
1908 ASSERT(error
!= ENOSPC
);
1913 * Now see if we can convert the single-leaf directory
1914 * down to a block form directory.
1915 * This routine always kills the dabuf for the leaf, so
1916 * eliminate it from the path.
1918 error
= xfs_dir2_leaf_to_block(args
, lbp
, NULL
);
1919 state
->path
.blk
[0].bp
= NULL
;