2 * Copyright (c) 2000-2002 Silicon Graphics, Inc. All Rights Reserved.
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of version 2 of the GNU General Public License as
6 * published by the Free Software Foundation.
8 * This program is distributed in the hope that it would be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12 * Further, this software is distributed without any warranty that it is
13 * free of the rightful claim of any third person regarding infringement
14 * or the like. Any license provided herein, whether implied or
15 * otherwise, applies only to this software file. Patent licenses, if
16 * any, provided herein do not apply to combinations of this program with
17 * other software, or any other product whatsoever.
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write the Free Software Foundation, Inc., 59
21 * Temple Place - Suite 330, Boston MA 02111-1307, USA.
23 * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
24 * Mountain View, CA 94043, or:
28 * For further information regarding this notice, see:
30 * http://oss.sgi.com/projects/GenInfo/SGIGPLNoticeExplan/
35 * XFS V2 directory implementation, single-block form.
36 * See xfs_dir2_block.h for the format.
42 * Add an entry to a block directory.
45 xfs_dir2_block_addname(
46 xfs_da_args_t
*args
) /* directory op arguments */
48 xfs_dir2_data_free_t
*bf
; /* bestfree table in block */
49 xfs_dir2_block_t
*block
; /* directory block structure */
50 xfs_dir2_leaf_entry_t
*blp
; /* block leaf entries */
51 xfs_dabuf_t
*bp
; /* buffer for block */
52 xfs_dir2_block_tail_t
*btp
; /* block tail */
53 int compact
; /* need to compact leaf ents */
54 xfs_dir2_data_entry_t
*dep
; /* block data entry */
55 xfs_inode_t
*dp
; /* directory inode */
56 xfs_dir2_data_unused_t
*dup
; /* block unused entry */
57 int error
; /* error return value */
58 xfs_dir2_data_unused_t
*enddup
=NULL
; /* unused at end of data */
59 xfs_dahash_t hash
; /* hash value of found entry */
60 int high
; /* high index for binary srch */
61 int highstale
; /* high stale index */
62 int lfloghigh
=0; /* last final leaf to log */
63 int lfloglow
=0; /* first final leaf to log */
64 int len
; /* length of the new entry */
65 int low
; /* low index for binary srch */
66 int lowstale
; /* low stale index */
67 int mid
=0; /* midpoint for binary srch */
68 xfs_mount_t
*mp
; /* filesystem mount point */
69 int needlog
; /* need to log header */
70 int needscan
; /* need to rescan freespace */
71 xfs_dir2_data_off_t
*tagp
; /* pointer to tag value */
72 xfs_trans_t
*tp
; /* transaction structure */
74 xfs_dir2_trace_args("block_addname", args
);
79 * Read the (one and only) directory block into dabuf bp.
82 xfs_da_read_buf(tp
, dp
, mp
->m_dirdatablk
, -1, &bp
, XFS_DATA_FORK
))) {
88 * Check the magic number, corrupted if wrong.
90 if (unlikely(INT_GET(block
->hdr
.magic
, ARCH_CONVERT
)
91 != XFS_DIR2_BLOCK_MAGIC
)) {
92 XFS_CORRUPTION_ERROR("xfs_dir2_block_addname",
93 XFS_ERRLEVEL_LOW
, mp
, block
);
94 xfs_da_brelse(tp
, bp
);
95 return XFS_ERROR(EFSCORRUPTED
);
97 len
= XFS_DIR2_DATA_ENTSIZE(args
->namelen
);
99 * Set up pointers to parts of the block.
101 bf
= block
->hdr
.bestfree
;
102 btp
= XFS_DIR2_BLOCK_TAIL_P(mp
, block
);
103 blp
= XFS_DIR2_BLOCK_LEAF_P_ARCH(btp
, ARCH_CONVERT
);
105 * No stale entries? Need space for entry and new leaf.
107 if (INT_ISZERO(btp
->stale
, ARCH_CONVERT
)) {
109 * Tag just before the first leaf entry.
111 tagp
= (xfs_dir2_data_off_t
*)blp
- 1;
113 * Data object just before the first leaf entry.
115 enddup
= (xfs_dir2_data_unused_t
*)((char *)block
+ INT_GET(*tagp
, ARCH_CONVERT
));
117 * If it's not free then can't do this add without cleaning up:
118 * the space before the first leaf entry needs to be free so it
119 * can be expanded to hold the pointer to the new entry.
121 if (INT_GET(enddup
->freetag
, ARCH_CONVERT
) != XFS_DIR2_DATA_FREE_TAG
)
124 * Check out the biggest freespace and see if it's the same one.
127 dup
= (xfs_dir2_data_unused_t
*)
128 ((char *)block
+ INT_GET(bf
[0].offset
, ARCH_CONVERT
));
131 * It is the biggest freespace, is it too small
132 * to hold the new leaf too?
134 if (INT_GET(dup
->length
, ARCH_CONVERT
) < len
+ (uint
)sizeof(*blp
)) {
136 * Yes, we use the second-largest
137 * entry instead if it works.
139 if (INT_GET(bf
[1].length
, ARCH_CONVERT
) >= len
)
140 dup
= (xfs_dir2_data_unused_t
*)
142 INT_GET(bf
[1].offset
, ARCH_CONVERT
));
148 * Not the same free entry,
149 * just check its length.
151 if (INT_GET(dup
->length
, ARCH_CONVERT
) < len
) {
159 * If there are stale entries we'll use one for the leaf.
160 * Is the biggest entry enough to avoid compaction?
162 else if (INT_GET(bf
[0].length
, ARCH_CONVERT
) >= len
) {
163 dup
= (xfs_dir2_data_unused_t
*)
164 ((char *)block
+ INT_GET(bf
[0].offset
, ARCH_CONVERT
));
168 * Will need to compact to make this work.
172 * Tag just before the first leaf entry.
174 tagp
= (xfs_dir2_data_off_t
*)blp
- 1;
176 * Data object just before the first leaf entry.
178 dup
= (xfs_dir2_data_unused_t
*)((char *)block
+ INT_GET(*tagp
, ARCH_CONVERT
));
180 * If it's not free then the data will go where the
181 * leaf data starts now, if it works at all.
183 if (INT_GET(dup
->freetag
, ARCH_CONVERT
) == XFS_DIR2_DATA_FREE_TAG
) {
184 if (INT_GET(dup
->length
, ARCH_CONVERT
) + (INT_GET(btp
->stale
, ARCH_CONVERT
) - 1) *
185 (uint
)sizeof(*blp
) < len
)
187 } else if ((INT_GET(btp
->stale
, ARCH_CONVERT
) - 1) * (uint
)sizeof(*blp
) < len
)
190 dup
= (xfs_dir2_data_unused_t
*)blp
;
194 * If this isn't a real add, we're done with the buffer.
197 xfs_da_brelse(tp
, bp
);
199 * If we don't have space for the new entry & leaf ...
203 * Not trying to actually do anything, or don't have
204 * a space reservation: return no-space.
206 if (args
->justcheck
|| args
->total
== 0)
207 return XFS_ERROR(ENOSPC
);
209 * Convert to the next larger format.
210 * Then add the new entry in that format.
212 error
= xfs_dir2_block_to_leaf(args
, bp
);
216 return xfs_dir2_leaf_addname(args
);
219 * Just checking, and it would work, so say so.
223 needlog
= needscan
= 0;
225 * If need to compact the leaf entries, do it now.
226 * Leave the highest-numbered stale entry stale.
227 * XXX should be the one closest to mid but mid is not yet computed.
230 int fromidx
; /* source leaf index */
231 int toidx
; /* target leaf index */
233 for (fromidx
= toidx
= INT_GET(btp
->count
, ARCH_CONVERT
) - 1,
234 highstale
= lfloghigh
= -1;
237 if (INT_GET(blp
[fromidx
].address
, ARCH_CONVERT
) == XFS_DIR2_NULL_DATAPTR
) {
247 blp
[toidx
] = blp
[fromidx
];
250 lfloglow
= toidx
+ 1 - (INT_GET(btp
->stale
, ARCH_CONVERT
) - 1);
251 lfloghigh
-= INT_GET(btp
->stale
, ARCH_CONVERT
) - 1;
252 INT_MOD(btp
->count
, ARCH_CONVERT
, -(INT_GET(btp
->stale
, ARCH_CONVERT
) - 1));
253 xfs_dir2_data_make_free(tp
, bp
,
254 (xfs_dir2_data_aoff_t
)((char *)blp
- (char *)block
),
255 (xfs_dir2_data_aoff_t
)((INT_GET(btp
->stale
, ARCH_CONVERT
) - 1) * sizeof(*blp
)),
256 &needlog
, &needscan
);
257 blp
+= INT_GET(btp
->stale
, ARCH_CONVERT
) - 1;
258 INT_SET(btp
->stale
, ARCH_CONVERT
, 1);
260 * If we now need to rebuild the bestfree map, do so.
261 * This needs to happen before the next call to use_free.
264 xfs_dir2_data_freescan(mp
, (xfs_dir2_data_t
*)block
,
270 * Set leaf logging boundaries to impossible state.
271 * For the no-stale case they're set explicitly.
273 else if (INT_GET(btp
->stale
, ARCH_CONVERT
)) {
274 lfloglow
= INT_GET(btp
->count
, ARCH_CONVERT
);
278 * Find the slot that's first lower than our hash value, -1 if none.
280 for (low
= 0, high
= INT_GET(btp
->count
, ARCH_CONVERT
) - 1; low
<= high
; ) {
281 mid
= (low
+ high
) >> 1;
282 if ((hash
= INT_GET(blp
[mid
].hashval
, ARCH_CONVERT
)) == args
->hashval
)
284 if (hash
< args
->hashval
)
289 while (mid
>= 0 && INT_GET(blp
[mid
].hashval
, ARCH_CONVERT
) >= args
->hashval
) {
293 * No stale entries, will use enddup space to hold new leaf.
295 if (INT_ISZERO(btp
->stale
, ARCH_CONVERT
)) {
297 * Mark the space needed for the new leaf entry, now in use.
299 xfs_dir2_data_use_free(tp
, bp
, enddup
,
300 (xfs_dir2_data_aoff_t
)
301 ((char *)enddup
- (char *)block
+ INT_GET(enddup
->length
, ARCH_CONVERT
) -
303 (xfs_dir2_data_aoff_t
)sizeof(*blp
),
304 &needlog
, &needscan
);
306 * Update the tail (entry count).
308 INT_MOD(btp
->count
, ARCH_CONVERT
, +1);
310 * If we now need to rebuild the bestfree map, do so.
311 * This needs to happen before the next call to use_free.
314 xfs_dir2_data_freescan(mp
, (xfs_dir2_data_t
*)block
,
319 * Adjust pointer to the first leaf entry, we're about to move
320 * the table up one to open up space for the new leaf entry.
321 * Then adjust our index to match.
326 memmove(blp
, &blp
[1], mid
* sizeof(*blp
));
331 * Use a stale leaf for our new entry.
336 INT_GET(blp
[lowstale
].address
, ARCH_CONVERT
) != XFS_DIR2_NULL_DATAPTR
;
339 for (highstale
= mid
+ 1;
340 highstale
< INT_GET(btp
->count
, ARCH_CONVERT
) &&
341 INT_GET(blp
[highstale
].address
, ARCH_CONVERT
) != XFS_DIR2_NULL_DATAPTR
&&
342 (lowstale
< 0 || mid
- lowstale
> highstale
- mid
);
346 * Move entries toward the low-numbered stale entry.
349 (highstale
== INT_GET(btp
->count
, ARCH_CONVERT
) ||
350 mid
- lowstale
<= highstale
- mid
)) {
352 memmove(&blp
[lowstale
], &blp
[lowstale
+ 1],
353 (mid
- lowstale
) * sizeof(*blp
));
354 lfloglow
= MIN(lowstale
, lfloglow
);
355 lfloghigh
= MAX(mid
, lfloghigh
);
358 * Move entries toward the high-numbered stale entry.
361 ASSERT(highstale
< INT_GET(btp
->count
, ARCH_CONVERT
));
364 memmove(&blp
[mid
+ 1], &blp
[mid
],
365 (highstale
- mid
) * sizeof(*blp
));
366 lfloglow
= MIN(mid
, lfloglow
);
367 lfloghigh
= MAX(highstale
, lfloghigh
);
369 INT_MOD(btp
->stale
, ARCH_CONVERT
, -1);
372 * Point to the new data entry.
374 dep
= (xfs_dir2_data_entry_t
*)dup
;
376 * Fill in the leaf entry.
378 INT_SET(blp
[mid
].hashval
, ARCH_CONVERT
, args
->hashval
);
379 INT_SET(blp
[mid
].address
, ARCH_CONVERT
, XFS_DIR2_BYTE_TO_DATAPTR(mp
, (char *)dep
- (char *)block
));
380 xfs_dir2_block_log_leaf(tp
, bp
, lfloglow
, lfloghigh
);
382 * Mark space for the data entry used.
384 xfs_dir2_data_use_free(tp
, bp
, dup
,
385 (xfs_dir2_data_aoff_t
)((char *)dup
- (char *)block
),
386 (xfs_dir2_data_aoff_t
)len
, &needlog
, &needscan
);
388 * Create the new data entry.
390 INT_SET(dep
->inumber
, ARCH_CONVERT
, args
->inumber
);
391 dep
->namelen
= args
->namelen
;
392 memcpy(dep
->name
, args
->name
, args
->namelen
);
393 tagp
= XFS_DIR2_DATA_ENTRY_TAG_P(dep
);
394 INT_SET(*tagp
, ARCH_CONVERT
, (xfs_dir2_data_off_t
)((char *)dep
- (char *)block
));
396 * Clean up the bestfree array and log the header, tail, and entry.
399 xfs_dir2_data_freescan(mp
, (xfs_dir2_data_t
*)block
, &needlog
,
402 xfs_dir2_data_log_header(tp
, bp
);
403 xfs_dir2_block_log_tail(tp
, bp
);
404 xfs_dir2_data_log_entry(tp
, bp
, dep
);
405 xfs_dir2_data_check(dp
, bp
);
411 * Log leaf entries from the block.
414 xfs_dir2_block_log_leaf(
415 xfs_trans_t
*tp
, /* transaction structure */
416 xfs_dabuf_t
*bp
, /* block buffer */
417 int first
, /* index of first logged leaf */
418 int last
) /* index of last logged leaf */
420 xfs_dir2_block_t
*block
; /* directory block structure */
421 xfs_dir2_leaf_entry_t
*blp
; /* block leaf entries */
422 xfs_dir2_block_tail_t
*btp
; /* block tail */
423 xfs_mount_t
*mp
; /* filesystem mount point */
427 btp
= XFS_DIR2_BLOCK_TAIL_P(mp
, block
);
428 blp
= XFS_DIR2_BLOCK_LEAF_P_ARCH(btp
, ARCH_CONVERT
);
429 xfs_da_log_buf(tp
, bp
, (uint
)((char *)&blp
[first
] - (char *)block
),
430 (uint
)((char *)&blp
[last
+ 1] - (char *)block
- 1));
434 * Log the block tail.
437 xfs_dir2_block_log_tail(
438 xfs_trans_t
*tp
, /* transaction structure */
439 xfs_dabuf_t
*bp
) /* block buffer */
441 xfs_dir2_block_t
*block
; /* directory block structure */
442 xfs_dir2_block_tail_t
*btp
; /* block tail */
443 xfs_mount_t
*mp
; /* filesystem mount point */
447 btp
= XFS_DIR2_BLOCK_TAIL_P(mp
, block
);
448 xfs_da_log_buf(tp
, bp
, (uint
)((char *)btp
- (char *)block
),
449 (uint
)((char *)(btp
+ 1) - (char *)block
- 1));
453 * Look up an entry in the block. This is the external routine,
454 * xfs_dir2_block_lookup_int does the real work.
457 xfs_dir2_block_lookup(
458 xfs_da_args_t
*args
) /* dir lookup arguments */
460 xfs_dir2_block_t
*block
; /* block structure */
461 xfs_dir2_leaf_entry_t
*blp
; /* block leaf entries */
462 xfs_dabuf_t
*bp
; /* block buffer */
463 xfs_dir2_block_tail_t
*btp
; /* block tail */
464 xfs_dir2_data_entry_t
*dep
; /* block data entry */
465 xfs_inode_t
*dp
; /* incore inode */
466 int ent
; /* entry index */
467 int error
; /* error return value */
468 xfs_mount_t
*mp
; /* filesystem mount point */
470 xfs_dir2_trace_args("block_lookup", args
);
472 * Get the buffer, look up the entry.
473 * If not found (ENOENT) then return, have no buffer.
475 if ((error
= xfs_dir2_block_lookup_int(args
, &bp
, &ent
)))
480 xfs_dir2_data_check(dp
, bp
);
481 btp
= XFS_DIR2_BLOCK_TAIL_P(mp
, block
);
482 blp
= XFS_DIR2_BLOCK_LEAF_P_ARCH(btp
, ARCH_CONVERT
);
484 * Get the offset from the leaf entry, to point to the data.
486 dep
= (xfs_dir2_data_entry_t
*)
487 ((char *)block
+ XFS_DIR2_DATAPTR_TO_OFF(mp
, INT_GET(blp
[ent
].address
, ARCH_CONVERT
)));
489 * Fill in inode number, release the block.
491 args
->inumber
= INT_GET(dep
->inumber
, ARCH_CONVERT
);
492 xfs_da_brelse(args
->trans
, bp
);
493 return XFS_ERROR(EEXIST
);
497 * Internal block lookup routine.
499 STATIC
int /* error */
500 xfs_dir2_block_lookup_int(
501 xfs_da_args_t
*args
, /* dir lookup arguments */
502 xfs_dabuf_t
**bpp
, /* returned block buffer */
503 int *entno
) /* returned entry number */
505 xfs_dir2_dataptr_t addr
; /* data entry address */
506 xfs_dir2_block_t
*block
; /* block structure */
507 xfs_dir2_leaf_entry_t
*blp
; /* block leaf entries */
508 xfs_dabuf_t
*bp
; /* block buffer */
509 xfs_dir2_block_tail_t
*btp
; /* block tail */
510 xfs_dir2_data_entry_t
*dep
; /* block data entry */
511 xfs_inode_t
*dp
; /* incore inode */
512 int error
; /* error return value */
513 xfs_dahash_t hash
; /* found hash value */
514 int high
; /* binary search high index */
515 int low
; /* binary search low index */
516 int mid
; /* binary search current idx */
517 xfs_mount_t
*mp
; /* filesystem mount point */
518 xfs_trans_t
*tp
; /* transaction pointer */
524 * Read the buffer, return error if we can't get it.
527 xfs_da_read_buf(tp
, dp
, mp
->m_dirdatablk
, -1, &bp
, XFS_DATA_FORK
))) {
532 xfs_dir2_data_check(dp
, bp
);
533 btp
= XFS_DIR2_BLOCK_TAIL_P(mp
, block
);
534 blp
= XFS_DIR2_BLOCK_LEAF_P_ARCH(btp
, ARCH_CONVERT
);
536 * Loop doing a binary search for our hash value.
537 * Find our entry, ENOENT if it's not there.
539 for (low
= 0, high
= INT_GET(btp
->count
, ARCH_CONVERT
) - 1; ; ) {
541 mid
= (low
+ high
) >> 1;
542 if ((hash
= INT_GET(blp
[mid
].hashval
, ARCH_CONVERT
)) == args
->hashval
)
544 if (hash
< args
->hashval
)
549 ASSERT(args
->oknoent
);
550 xfs_da_brelse(tp
, bp
);
551 return XFS_ERROR(ENOENT
);
555 * Back up to the first one with the right hash value.
557 while (mid
> 0 && INT_GET(blp
[mid
- 1].hashval
, ARCH_CONVERT
) == args
->hashval
) {
561 * Now loop forward through all the entries with the
562 * right hash value looking for our name.
565 if ((addr
= INT_GET(blp
[mid
].address
, ARCH_CONVERT
)) == XFS_DIR2_NULL_DATAPTR
)
568 * Get pointer to the entry from the leaf.
570 dep
= (xfs_dir2_data_entry_t
*)
571 ((char *)block
+ XFS_DIR2_DATAPTR_TO_OFF(mp
, addr
));
573 * Compare, if it's right give back buffer & entry number.
575 if (dep
->namelen
== args
->namelen
&&
576 dep
->name
[0] == args
->name
[0] &&
577 memcmp(dep
->name
, args
->name
, args
->namelen
) == 0) {
582 } while (++mid
< INT_GET(btp
->count
, ARCH_CONVERT
) && INT_GET(blp
[mid
].hashval
, ARCH_CONVERT
) == hash
);
584 * No match, release the buffer and return ENOENT.
586 ASSERT(args
->oknoent
);
587 xfs_da_brelse(tp
, bp
);
588 return XFS_ERROR(ENOENT
);
592 * Remove an entry from a block format directory.
593 * If that makes the block small enough to fit in shortform, transform it.
596 xfs_dir2_block_removename(
597 xfs_da_args_t
*args
) /* directory operation args */
599 xfs_dir2_block_t
*block
; /* block structure */
600 xfs_dir2_leaf_entry_t
*blp
; /* block leaf pointer */
601 xfs_dabuf_t
*bp
; /* block buffer */
602 xfs_dir2_block_tail_t
*btp
; /* block tail */
603 xfs_dir2_data_entry_t
*dep
; /* block data entry */
604 xfs_inode_t
*dp
; /* incore inode */
605 int ent
; /* block leaf entry index */
606 int error
; /* error return value */
607 xfs_mount_t
*mp
; /* filesystem mount point */
608 int needlog
; /* need to log block header */
609 int needscan
; /* need to fixup bestfree */
610 xfs_dir2_sf_hdr_t sfh
; /* shortform header */
611 int size
; /* shortform size */
612 xfs_trans_t
*tp
; /* transaction pointer */
614 xfs_dir2_trace_args("block_removename", args
);
616 * Look up the entry in the block. Gets the buffer and entry index.
617 * It will always be there, the vnodeops level does a lookup first.
619 if ((error
= xfs_dir2_block_lookup_int(args
, &bp
, &ent
))) {
626 btp
= XFS_DIR2_BLOCK_TAIL_P(mp
, block
);
627 blp
= XFS_DIR2_BLOCK_LEAF_P_ARCH(btp
, ARCH_CONVERT
);
629 * Point to the data entry using the leaf entry.
631 dep
= (xfs_dir2_data_entry_t
*)
632 ((char *)block
+ XFS_DIR2_DATAPTR_TO_OFF(mp
, INT_GET(blp
[ent
].address
, ARCH_CONVERT
)));
634 * Mark the data entry's space free.
636 needlog
= needscan
= 0;
637 xfs_dir2_data_make_free(tp
, bp
,
638 (xfs_dir2_data_aoff_t
)((char *)dep
- (char *)block
),
639 XFS_DIR2_DATA_ENTSIZE(dep
->namelen
), &needlog
, &needscan
);
641 * Fix up the block tail.
643 INT_MOD(btp
->stale
, ARCH_CONVERT
, +1);
644 xfs_dir2_block_log_tail(tp
, bp
);
646 * Remove the leaf entry by marking it stale.
648 INT_SET(blp
[ent
].address
, ARCH_CONVERT
, XFS_DIR2_NULL_DATAPTR
);
649 xfs_dir2_block_log_leaf(tp
, bp
, ent
, ent
);
651 * Fix up bestfree, log the header if necessary.
654 xfs_dir2_data_freescan(mp
, (xfs_dir2_data_t
*)block
, &needlog
,
657 xfs_dir2_data_log_header(tp
, bp
);
658 xfs_dir2_data_check(dp
, bp
);
660 * See if the size as a shortform is good enough.
662 if ((size
= xfs_dir2_block_sfsize(dp
, block
, &sfh
)) >
663 XFS_IFORK_DSIZE(dp
)) {
668 * If it works, do the conversion.
670 return xfs_dir2_block_to_sf(args
, bp
, size
, &sfh
);
674 * Replace an entry in a V2 block directory.
675 * Change the inode number to the new value.
678 xfs_dir2_block_replace(
679 xfs_da_args_t
*args
) /* directory operation args */
681 xfs_dir2_block_t
*block
; /* block structure */
682 xfs_dir2_leaf_entry_t
*blp
; /* block leaf entries */
683 xfs_dabuf_t
*bp
; /* block buffer */
684 xfs_dir2_block_tail_t
*btp
; /* block tail */
685 xfs_dir2_data_entry_t
*dep
; /* block data entry */
686 xfs_inode_t
*dp
; /* incore inode */
687 int ent
; /* leaf entry index */
688 int error
; /* error return value */
689 xfs_mount_t
*mp
; /* filesystem mount point */
691 xfs_dir2_trace_args("block_replace", args
);
693 * Lookup the entry in the directory. Get buffer and entry index.
694 * This will always succeed since the caller has already done a lookup.
696 if ((error
= xfs_dir2_block_lookup_int(args
, &bp
, &ent
))) {
702 btp
= XFS_DIR2_BLOCK_TAIL_P(mp
, block
);
703 blp
= XFS_DIR2_BLOCK_LEAF_P_ARCH(btp
, ARCH_CONVERT
);
705 * Point to the data entry we need to change.
707 dep
= (xfs_dir2_data_entry_t
*)
708 ((char *)block
+ XFS_DIR2_DATAPTR_TO_OFF(mp
, INT_GET(blp
[ent
].address
, ARCH_CONVERT
)));
709 ASSERT(INT_GET(dep
->inumber
, ARCH_CONVERT
) != args
->inumber
);
711 * Change the inode number to the new value.
713 INT_SET(dep
->inumber
, ARCH_CONVERT
, args
->inumber
);
714 xfs_dir2_data_log_entry(args
->trans
, bp
, dep
);
715 xfs_dir2_data_check(dp
, bp
);
721 * Qsort comparison routine for the block leaf entries.
723 static int /* sort order */
725 const void *a
, /* first leaf entry */
726 const void *b
) /* second leaf entry */
728 const xfs_dir2_leaf_entry_t
*la
; /* first leaf entry */
729 const xfs_dir2_leaf_entry_t
*lb
; /* second leaf entry */
733 return INT_GET(la
->hashval
, ARCH_CONVERT
) < INT_GET(lb
->hashval
, ARCH_CONVERT
) ? -1 :
734 (INT_GET(la
->hashval
, ARCH_CONVERT
) > INT_GET(lb
->hashval
, ARCH_CONVERT
) ? 1 : 0);
738 * Convert a V2 leaf directory to a V2 block directory if possible.
741 xfs_dir2_leaf_to_block(
742 xfs_da_args_t
*args
, /* operation arguments */
743 xfs_dabuf_t
*lbp
, /* leaf buffer */
744 xfs_dabuf_t
*dbp
) /* data buffer */
746 xfs_dir2_data_off_t
*bestsp
; /* leaf bests table */
747 xfs_dir2_block_t
*block
; /* block structure */
748 xfs_dir2_block_tail_t
*btp
; /* block tail */
749 xfs_inode_t
*dp
; /* incore directory inode */
750 xfs_dir2_data_unused_t
*dup
; /* unused data entry */
751 int error
; /* error return value */
752 int from
; /* leaf from index */
753 xfs_dir2_leaf_t
*leaf
; /* leaf structure */
754 xfs_dir2_leaf_entry_t
*lep
; /* leaf entry */
755 xfs_dir2_leaf_tail_t
*ltp
; /* leaf tail structure */
756 xfs_mount_t
*mp
; /* file system mount point */
757 int needlog
; /* need to log data header */
758 int needscan
; /* need to scan for bestfree */
759 xfs_dir2_sf_hdr_t sfh
; /* shortform header */
760 int size
; /* bytes used */
761 xfs_dir2_data_off_t
*tagp
; /* end of entry (tag) */
762 int to
; /* block/leaf to index */
763 xfs_trans_t
*tp
; /* transaction pointer */
765 xfs_dir2_trace_args_bb("leaf_to_block", args
, lbp
, dbp
);
770 ASSERT(INT_GET(leaf
->hdr
.info
.magic
, ARCH_CONVERT
) == XFS_DIR2_LEAF1_MAGIC
);
771 ltp
= XFS_DIR2_LEAF_TAIL_P(mp
, leaf
);
773 * If there are data blocks other than the first one, take this
774 * opportunity to remove trailing empty data blocks that may have
775 * been left behind during no-space-reservation operations.
776 * These will show up in the leaf bests table.
778 while (dp
->i_d
.di_size
> mp
->m_dirblksize
) {
779 bestsp
= XFS_DIR2_LEAF_BESTS_P_ARCH(ltp
, ARCH_CONVERT
);
780 if (INT_GET(bestsp
[INT_GET(ltp
->bestcount
, ARCH_CONVERT
) - 1], ARCH_CONVERT
) ==
781 mp
->m_dirblksize
- (uint
)sizeof(block
->hdr
)) {
783 xfs_dir2_leaf_trim_data(args
, lbp
,
784 (xfs_dir2_db_t
)(INT_GET(ltp
->bestcount
, ARCH_CONVERT
) - 1))))
792 * Read the data block if we don't already have it, give up if it fails.
795 (error
= xfs_da_read_buf(tp
, dp
, mp
->m_dirdatablk
, -1, &dbp
,
800 ASSERT(INT_GET(block
->hdr
.magic
, ARCH_CONVERT
) == XFS_DIR2_DATA_MAGIC
);
802 * Size of the "leaf" area in the block.
804 size
= (uint
)sizeof(block
->tail
) +
805 (uint
)sizeof(*lep
) * (INT_GET(leaf
->hdr
.count
, ARCH_CONVERT
) - INT_GET(leaf
->hdr
.stale
, ARCH_CONVERT
));
807 * Look at the last data entry.
809 tagp
= (xfs_dir2_data_off_t
*)((char *)block
+ mp
->m_dirblksize
) - 1;
810 dup
= (xfs_dir2_data_unused_t
*)((char *)block
+ INT_GET(*tagp
, ARCH_CONVERT
));
812 * If it's not free or is too short we can't do it.
814 if (INT_GET(dup
->freetag
, ARCH_CONVERT
) != XFS_DIR2_DATA_FREE_TAG
|| INT_GET(dup
->length
, ARCH_CONVERT
) < size
) {
819 * Start converting it to block form.
821 INT_SET(block
->hdr
.magic
, ARCH_CONVERT
, XFS_DIR2_BLOCK_MAGIC
);
825 * Use up the space at the end of the block (blp/btp).
827 xfs_dir2_data_use_free(tp
, dbp
, dup
, mp
->m_dirblksize
- size
, size
,
828 &needlog
, &needscan
);
830 * Initialize the block tail.
832 btp
= XFS_DIR2_BLOCK_TAIL_P(mp
, block
);
833 INT_SET(btp
->count
, ARCH_CONVERT
, INT_GET(leaf
->hdr
.count
, ARCH_CONVERT
) - INT_GET(leaf
->hdr
.stale
, ARCH_CONVERT
));
834 INT_ZERO(btp
->stale
, ARCH_CONVERT
);
835 xfs_dir2_block_log_tail(tp
, dbp
);
837 * Initialize the block leaf area. We compact out stale entries.
839 lep
= XFS_DIR2_BLOCK_LEAF_P_ARCH(btp
, ARCH_CONVERT
);
840 for (from
= to
= 0; from
< INT_GET(leaf
->hdr
.count
, ARCH_CONVERT
); from
++) {
841 if (INT_GET(leaf
->ents
[from
].address
, ARCH_CONVERT
) == XFS_DIR2_NULL_DATAPTR
)
843 lep
[to
++] = leaf
->ents
[from
];
845 ASSERT(to
== INT_GET(btp
->count
, ARCH_CONVERT
));
846 xfs_dir2_block_log_leaf(tp
, dbp
, 0, INT_GET(btp
->count
, ARCH_CONVERT
) - 1);
848 * Scan the bestfree if we need it and log the data block header.
851 xfs_dir2_data_freescan(mp
, (xfs_dir2_data_t
*)block
, &needlog
,
854 xfs_dir2_data_log_header(tp
, dbp
);
856 * Pitch the old leaf block.
858 error
= xfs_da_shrink_inode(args
, mp
->m_dirleafblk
, lbp
);
864 * Now see if the resulting block can be shrunken to shortform.
866 if ((size
= xfs_dir2_block_sfsize(dp
, block
, &sfh
)) >
867 XFS_IFORK_DSIZE(dp
)) {
871 return xfs_dir2_block_to_sf(args
, dbp
, size
, &sfh
);
874 xfs_da_buf_done(lbp
);
876 xfs_da_buf_done(dbp
);
881 * Convert the shortform directory to block form.
884 xfs_dir2_sf_to_block(
885 xfs_da_args_t
*args
) /* operation arguments */
887 xfs_dir2_db_t blkno
; /* dir-relative block # (0) */
888 xfs_dir2_block_t
*block
; /* block structure */
889 xfs_dir2_leaf_entry_t
*blp
; /* block leaf entries */
890 xfs_dabuf_t
*bp
; /* block buffer */
891 xfs_dir2_block_tail_t
*btp
; /* block tail pointer */
892 char *buf
; /* sf buffer */
894 xfs_dir2_data_entry_t
*dep
; /* data entry pointer */
895 xfs_inode_t
*dp
; /* incore directory inode */
896 int dummy
; /* trash */
897 xfs_dir2_data_unused_t
*dup
; /* unused entry pointer */
898 int endoffset
; /* end of data objects */
899 int error
; /* error return value */
901 xfs_mount_t
*mp
; /* filesystem mount point */
902 int needlog
; /* need to log block header */
903 int needscan
; /* need to scan block freespc */
904 int newoffset
; /* offset from current entry */
905 int offset
; /* target block offset */
906 xfs_dir2_sf_entry_t
*sfep
; /* sf entry pointer */
907 xfs_dir2_sf_t
*sfp
; /* shortform structure */
908 xfs_dir2_data_off_t
*tagp
; /* end of data entry */
909 xfs_trans_t
*tp
; /* transaction pointer */
911 xfs_dir2_trace_args("sf_to_block", args
);
915 ASSERT(dp
->i_df
.if_flags
& XFS_IFINLINE
);
917 * Bomb out if the shortform directory is way too short.
919 if (dp
->i_d
.di_size
< offsetof(xfs_dir2_sf_hdr_t
, parent
)) {
920 ASSERT(XFS_FORCED_SHUTDOWN(mp
));
921 return XFS_ERROR(EIO
);
923 ASSERT(dp
->i_df
.if_bytes
== dp
->i_d
.di_size
);
924 ASSERT(dp
->i_df
.if_u1
.if_data
!= NULL
);
925 sfp
= (xfs_dir2_sf_t
*)dp
->i_df
.if_u1
.if_data
;
926 ASSERT(dp
->i_d
.di_size
>= XFS_DIR2_SF_HDR_SIZE(sfp
->hdr
.i8count
));
928 * Copy the directory into the stack buffer.
929 * Then pitch the incore inode data so we can make extents.
932 buf_len
= dp
->i_df
.if_bytes
;
933 buf
= kmem_alloc(dp
->i_df
.if_bytes
, KM_SLEEP
);
935 memcpy(buf
, sfp
, dp
->i_df
.if_bytes
);
936 xfs_idata_realloc(dp
, -dp
->i_df
.if_bytes
, XFS_DATA_FORK
);
938 xfs_trans_log_inode(tp
, dp
, XFS_ILOG_CORE
);
940 * Reset pointer - old sfp is gone.
942 sfp
= (xfs_dir2_sf_t
*)buf
;
944 * Add block 0 to the inode.
946 error
= xfs_dir2_grow_inode(args
, XFS_DIR2_DATA_SPACE
, &blkno
);
948 kmem_free(buf
, buf_len
);
952 * Initialize the data block.
954 error
= xfs_dir2_data_init(args
, blkno
, &bp
);
956 kmem_free(buf
, buf_len
);
960 INT_SET(block
->hdr
.magic
, ARCH_CONVERT
, XFS_DIR2_BLOCK_MAGIC
);
962 * Compute size of block "tail" area.
964 i
= (uint
)sizeof(*btp
) +
965 (INT_GET(sfp
->hdr
.count
, ARCH_CONVERT
) + 2) * (uint
)sizeof(xfs_dir2_leaf_entry_t
);
967 * The whole thing is initialized to free by the init routine.
968 * Say we're using the leaf and tail area.
970 dup
= (xfs_dir2_data_unused_t
*)block
->u
;
971 needlog
= needscan
= 0;
972 xfs_dir2_data_use_free(tp
, bp
, dup
, mp
->m_dirblksize
- i
, i
, &needlog
,
974 ASSERT(needscan
== 0);
978 btp
= XFS_DIR2_BLOCK_TAIL_P(mp
, block
);
979 INT_SET(btp
->count
, ARCH_CONVERT
, INT_GET(sfp
->hdr
.count
, ARCH_CONVERT
) + 2); /* ., .. */
980 INT_ZERO(btp
->stale
, ARCH_CONVERT
);
981 blp
= XFS_DIR2_BLOCK_LEAF_P_ARCH(btp
, ARCH_CONVERT
);
982 endoffset
= (uint
)((char *)blp
- (char *)block
);
984 * Remove the freespace, we'll manage it.
986 xfs_dir2_data_use_free(tp
, bp
, dup
,
987 (xfs_dir2_data_aoff_t
)((char *)dup
- (char *)block
),
988 INT_GET(dup
->length
, ARCH_CONVERT
), &needlog
, &needscan
);
992 dep
= (xfs_dir2_data_entry_t
*)
993 ((char *)block
+ XFS_DIR2_DATA_DOT_OFFSET
);
994 INT_SET(dep
->inumber
, ARCH_CONVERT
, dp
->i_ino
);
997 tagp
= XFS_DIR2_DATA_ENTRY_TAG_P(dep
);
998 INT_SET(*tagp
, ARCH_CONVERT
, (xfs_dir2_data_off_t
)((char *)dep
- (char *)block
));
999 xfs_dir2_data_log_entry(tp
, bp
, dep
);
1000 INT_SET(blp
[0].hashval
, ARCH_CONVERT
, xfs_dir_hash_dot
);
1001 INT_SET(blp
[0].address
, ARCH_CONVERT
, XFS_DIR2_BYTE_TO_DATAPTR(mp
, (char *)dep
- (char *)block
));
1003 * Create entry for ..
1005 dep
= (xfs_dir2_data_entry_t
*)
1006 ((char *)block
+ XFS_DIR2_DATA_DOTDOT_OFFSET
);
1007 INT_SET(dep
->inumber
, ARCH_CONVERT
, XFS_DIR2_SF_GET_INUMBER_ARCH(sfp
, &sfp
->hdr
.parent
, ARCH_CONVERT
));
1009 dep
->name
[0] = dep
->name
[1] = '.';
1010 tagp
= XFS_DIR2_DATA_ENTRY_TAG_P(dep
);
1011 INT_SET(*tagp
, ARCH_CONVERT
, (xfs_dir2_data_off_t
)((char *)dep
- (char *)block
));
1012 xfs_dir2_data_log_entry(tp
, bp
, dep
);
1013 INT_SET(blp
[1].hashval
, ARCH_CONVERT
, xfs_dir_hash_dotdot
);
1014 INT_SET(blp
[1].address
, ARCH_CONVERT
, XFS_DIR2_BYTE_TO_DATAPTR(mp
, (char *)dep
- (char *)block
));
1015 offset
= XFS_DIR2_DATA_FIRST_OFFSET
;
1017 * Loop over existing entries, stuff them in.
1019 if ((i
= 0) == INT_GET(sfp
->hdr
.count
, ARCH_CONVERT
))
1022 sfep
= XFS_DIR2_SF_FIRSTENTRY(sfp
);
1024 * Need to preserve the existing offset values in the sf directory.
1025 * Insert holes (unused entries) where necessary.
1027 while (offset
< endoffset
) {
1029 * sfep is null when we reach the end of the list.
1032 newoffset
= endoffset
;
1034 newoffset
= XFS_DIR2_SF_GET_OFFSET_ARCH(sfep
, ARCH_CONVERT
);
1036 * There should be a hole here, make one.
1038 if (offset
< newoffset
) {
1039 dup
= (xfs_dir2_data_unused_t
*)
1040 ((char *)block
+ offset
);
1041 INT_SET(dup
->freetag
, ARCH_CONVERT
, XFS_DIR2_DATA_FREE_TAG
);
1042 INT_SET(dup
->length
, ARCH_CONVERT
, newoffset
- offset
);
1043 INT_SET(*XFS_DIR2_DATA_UNUSED_TAG_P_ARCH(dup
, ARCH_CONVERT
), ARCH_CONVERT
,
1044 (xfs_dir2_data_off_t
)
1045 ((char *)dup
- (char *)block
));
1046 xfs_dir2_data_log_unused(tp
, bp
, dup
);
1047 (void)xfs_dir2_data_freeinsert((xfs_dir2_data_t
*)block
,
1049 offset
+= INT_GET(dup
->length
, ARCH_CONVERT
);
1053 * Copy a real entry.
1055 dep
= (xfs_dir2_data_entry_t
*)((char *)block
+ newoffset
);
1056 INT_SET(dep
->inumber
, ARCH_CONVERT
, XFS_DIR2_SF_GET_INUMBER_ARCH(sfp
,
1057 XFS_DIR2_SF_INUMBERP(sfep
), ARCH_CONVERT
));
1058 dep
->namelen
= sfep
->namelen
;
1059 memcpy(dep
->name
, sfep
->name
, dep
->namelen
);
1060 tagp
= XFS_DIR2_DATA_ENTRY_TAG_P(dep
);
1061 INT_SET(*tagp
, ARCH_CONVERT
, (xfs_dir2_data_off_t
)((char *)dep
- (char *)block
));
1062 xfs_dir2_data_log_entry(tp
, bp
, dep
);
1063 INT_SET(blp
[2 + i
].hashval
, ARCH_CONVERT
, xfs_da_hashname((char *)sfep
->name
, sfep
->namelen
));
1064 INT_SET(blp
[2 + i
].address
, ARCH_CONVERT
, XFS_DIR2_BYTE_TO_DATAPTR(mp
,
1065 (char *)dep
- (char *)block
));
1066 offset
= (int)((char *)(tagp
+ 1) - (char *)block
);
1067 if (++i
== INT_GET(sfp
->hdr
.count
, ARCH_CONVERT
))
1070 sfep
= XFS_DIR2_SF_NEXTENTRY(sfp
, sfep
);
1072 /* Done with the temporary buffer */
1073 kmem_free(buf
, buf_len
);
1075 * Sort the leaf entries by hash value.
1077 qsort(blp
, INT_GET(btp
->count
, ARCH_CONVERT
), sizeof(*blp
), xfs_dir2_block_sort
);
1079 * Log the leaf entry area and tail.
1080 * Already logged the header in data_init, ignore needlog.
1082 ASSERT(needscan
== 0);
1083 xfs_dir2_block_log_leaf(tp
, bp
, 0, INT_GET(btp
->count
, ARCH_CONVERT
) - 1);
1084 xfs_dir2_block_log_tail(tp
, bp
);
1085 xfs_dir2_data_check(dp
, bp
);
1086 xfs_da_buf_done(bp
);