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 (INT_GET(block
->hdr
.magic
, ARCH_CONVERT
) != XFS_DIR2_BLOCK_MAGIC
) {
91 xfs_da_brelse(tp
, bp
);
92 return XFS_ERROR(EFSCORRUPTED
);
94 len
= XFS_DIR2_DATA_ENTSIZE(args
->namelen
);
96 * Set up pointers to parts of the block.
98 bf
= block
->hdr
.bestfree
;
99 btp
= XFS_DIR2_BLOCK_TAIL_P(mp
, block
);
100 blp
= XFS_DIR2_BLOCK_LEAF_P_ARCH(btp
, ARCH_CONVERT
);
102 * No stale entries? Need space for entry and new leaf.
104 if (INT_ISZERO(btp
->stale
, ARCH_CONVERT
)) {
106 * Tag just before the first leaf entry.
108 tagp
= (xfs_dir2_data_off_t
*)blp
- 1;
110 * Data object just before the first leaf entry.
112 enddup
= (xfs_dir2_data_unused_t
*)((char *)block
+ INT_GET(*tagp
, ARCH_CONVERT
));
114 * If it's not free then can't do this add without cleaning up:
115 * the space before the first leaf entry needs to be free so it
116 * can be expanded to hold the pointer to the new entry.
118 if (INT_GET(enddup
->freetag
, ARCH_CONVERT
) != XFS_DIR2_DATA_FREE_TAG
)
121 * Check out the biggest freespace and see if it's the same one.
124 dup
= (xfs_dir2_data_unused_t
*)
125 ((char *)block
+ INT_GET(bf
[0].offset
, ARCH_CONVERT
));
128 * It is the biggest freespace, is it too small
129 * to hold the new leaf too?
131 if (INT_GET(dup
->length
, ARCH_CONVERT
) < len
+ (uint
)sizeof(*blp
)) {
133 * Yes, we use the second-largest
134 * entry instead if it works.
136 if (INT_GET(bf
[1].length
, ARCH_CONVERT
) >= len
)
137 dup
= (xfs_dir2_data_unused_t
*)
139 INT_GET(bf
[1].offset
, ARCH_CONVERT
));
145 * Not the same free entry,
146 * just check its length.
148 if (INT_GET(dup
->length
, ARCH_CONVERT
) < len
) {
156 * If there are stale entries we'll use one for the leaf.
157 * Is the biggest entry enough to avoid compaction?
159 else if (INT_GET(bf
[0].length
, ARCH_CONVERT
) >= len
) {
160 dup
= (xfs_dir2_data_unused_t
*)
161 ((char *)block
+ INT_GET(bf
[0].offset
, ARCH_CONVERT
));
165 * Will need to compact to make this work.
169 * Tag just before the first leaf entry.
171 tagp
= (xfs_dir2_data_off_t
*)blp
- 1;
173 * Data object just before the first leaf entry.
175 dup
= (xfs_dir2_data_unused_t
*)((char *)block
+ INT_GET(*tagp
, ARCH_CONVERT
));
177 * If it's not free then the data will go where the
178 * leaf data starts now, if it works at all.
180 if (INT_GET(dup
->freetag
, ARCH_CONVERT
) == XFS_DIR2_DATA_FREE_TAG
) {
181 if (INT_GET(dup
->length
, ARCH_CONVERT
) + (INT_GET(btp
->stale
, ARCH_CONVERT
) - 1) *
182 (uint
)sizeof(*blp
) < len
)
184 } else if ((INT_GET(btp
->stale
, ARCH_CONVERT
) - 1) * (uint
)sizeof(*blp
) < len
)
187 dup
= (xfs_dir2_data_unused_t
*)blp
;
191 * If this isn't a real add, we're done with the buffer.
194 xfs_da_brelse(tp
, bp
);
196 * If we don't have space for the new entry & leaf ...
200 * Not trying to actually do anything, or don't have
201 * a space reservation: return no-space.
203 if (args
->justcheck
|| args
->total
== 0)
204 return XFS_ERROR(ENOSPC
);
206 * Convert to the next larger format.
207 * Then add the new entry in that format.
209 error
= xfs_dir2_block_to_leaf(args
, bp
);
213 return xfs_dir2_leaf_addname(args
);
216 * Just checking, and it would work, so say so.
220 needlog
= needscan
= 0;
222 * If need to compact the leaf entries, do it now.
223 * Leave the highest-numbered stale entry stale.
224 * XXX should be the one closest to mid but mid is not yet computed.
227 int fromidx
; /* source leaf index */
228 int toidx
; /* target leaf index */
230 for (fromidx
= toidx
= INT_GET(btp
->count
, ARCH_CONVERT
) - 1,
231 highstale
= lfloghigh
= -1;
234 if (INT_GET(blp
[fromidx
].address
, ARCH_CONVERT
) == XFS_DIR2_NULL_DATAPTR
) {
244 blp
[toidx
] = blp
[fromidx
];
247 lfloglow
= toidx
+ 1 - (INT_GET(btp
->stale
, ARCH_CONVERT
) - 1);
248 lfloghigh
-= INT_GET(btp
->stale
, ARCH_CONVERT
) - 1;
249 INT_MOD(btp
->count
, ARCH_CONVERT
, -(INT_GET(btp
->stale
, ARCH_CONVERT
) - 1));
250 xfs_dir2_data_make_free(tp
, bp
,
251 (xfs_dir2_data_aoff_t
)((char *)blp
- (char *)block
),
252 (xfs_dir2_data_aoff_t
)((INT_GET(btp
->stale
, ARCH_CONVERT
) - 1) * sizeof(*blp
)),
253 &needlog
, &needscan
);
254 blp
+= INT_GET(btp
->stale
, ARCH_CONVERT
) - 1;
255 INT_SET(btp
->stale
, ARCH_CONVERT
, 1);
257 * If we now need to rebuild the bestfree map, do so.
258 * This needs to happen before the next call to use_free.
261 xfs_dir2_data_freescan(mp
, (xfs_dir2_data_t
*)block
,
267 * Set leaf logging boundaries to impossible state.
268 * For the no-stale case they're set explicitly.
270 else if (INT_GET(btp
->stale
, ARCH_CONVERT
)) {
271 lfloglow
= INT_GET(btp
->count
, ARCH_CONVERT
);
275 * Find the slot that's first lower than our hash value, -1 if none.
277 for (low
= 0, high
= INT_GET(btp
->count
, ARCH_CONVERT
) - 1; low
<= high
; ) {
278 mid
= (low
+ high
) >> 1;
279 if ((hash
= INT_GET(blp
[mid
].hashval
, ARCH_CONVERT
)) == args
->hashval
)
281 if (hash
< args
->hashval
)
286 while (mid
>= 0 && INT_GET(blp
[mid
].hashval
, ARCH_CONVERT
) >= args
->hashval
) {
290 * No stale entries, will use enddup space to hold new leaf.
292 if (INT_ISZERO(btp
->stale
, ARCH_CONVERT
)) {
294 * Mark the space needed for the new leaf entry, now in use.
296 xfs_dir2_data_use_free(tp
, bp
, enddup
,
297 (xfs_dir2_data_aoff_t
)
298 ((char *)enddup
- (char *)block
+ INT_GET(enddup
->length
, ARCH_CONVERT
) -
300 (xfs_dir2_data_aoff_t
)sizeof(*blp
),
301 &needlog
, &needscan
);
303 * Update the tail (entry count).
305 INT_MOD(btp
->count
, ARCH_CONVERT
, +1);
307 * If we now need to rebuild the bestfree map, do so.
308 * This needs to happen before the next call to use_free.
311 xfs_dir2_data_freescan(mp
, (xfs_dir2_data_t
*)block
,
316 * Adjust pointer to the first leaf entry, we're about to move
317 * the table up one to open up space for the new leaf entry.
318 * Then adjust our index to match.
323 memmove(blp
, &blp
[1], mid
* sizeof(*blp
));
328 * Use a stale leaf for our new entry.
333 INT_GET(blp
[lowstale
].address
, ARCH_CONVERT
) != XFS_DIR2_NULL_DATAPTR
;
336 for (highstale
= mid
+ 1;
337 highstale
< INT_GET(btp
->count
, ARCH_CONVERT
) &&
338 INT_GET(blp
[highstale
].address
, ARCH_CONVERT
) != XFS_DIR2_NULL_DATAPTR
&&
339 (lowstale
< 0 || mid
- lowstale
> highstale
- mid
);
343 * Move entries toward the low-numbered stale entry.
346 (highstale
== INT_GET(btp
->count
, ARCH_CONVERT
) ||
347 mid
- lowstale
<= highstale
- mid
)) {
349 memmove(&blp
[lowstale
], &blp
[lowstale
+ 1],
350 (mid
- lowstale
) * sizeof(*blp
));
351 lfloglow
= MIN(lowstale
, lfloglow
);
352 lfloghigh
= MAX(mid
, lfloghigh
);
355 * Move entries toward the high-numbered stale entry.
358 ASSERT(highstale
< INT_GET(btp
->count
, ARCH_CONVERT
));
361 memmove(&blp
[mid
+ 1], &blp
[mid
],
362 (highstale
- mid
) * sizeof(*blp
));
363 lfloglow
= MIN(mid
, lfloglow
);
364 lfloghigh
= MAX(highstale
, lfloghigh
);
366 INT_MOD(btp
->stale
, ARCH_CONVERT
, -1);
369 * Point to the new data entry.
371 dep
= (xfs_dir2_data_entry_t
*)dup
;
373 * Fill in the leaf entry.
375 INT_SET(blp
[mid
].hashval
, ARCH_CONVERT
, args
->hashval
);
376 INT_SET(blp
[mid
].address
, ARCH_CONVERT
, XFS_DIR2_BYTE_TO_DATAPTR(mp
, (char *)dep
- (char *)block
));
377 xfs_dir2_block_log_leaf(tp
, bp
, lfloglow
, lfloghigh
);
379 * Mark space for the data entry used.
381 xfs_dir2_data_use_free(tp
, bp
, dup
,
382 (xfs_dir2_data_aoff_t
)((char *)dup
- (char *)block
),
383 (xfs_dir2_data_aoff_t
)len
, &needlog
, &needscan
);
385 * Create the new data entry.
387 INT_SET(dep
->inumber
, ARCH_CONVERT
, args
->inumber
);
388 dep
->namelen
= args
->namelen
;
389 memcpy(dep
->name
, args
->name
, args
->namelen
);
390 tagp
= XFS_DIR2_DATA_ENTRY_TAG_P(dep
);
391 INT_SET(*tagp
, ARCH_CONVERT
, (xfs_dir2_data_off_t
)((char *)dep
- (char *)block
));
393 * Clean up the bestfree array and log the header, tail, and entry.
396 xfs_dir2_data_freescan(mp
, (xfs_dir2_data_t
*)block
, &needlog
,
399 xfs_dir2_data_log_header(tp
, bp
);
400 xfs_dir2_block_log_tail(tp
, bp
);
401 xfs_dir2_data_log_entry(tp
, bp
, dep
);
402 xfs_dir2_data_check(dp
, bp
);
408 * Log leaf entries from the block.
411 xfs_dir2_block_log_leaf(
412 xfs_trans_t
*tp
, /* transaction structure */
413 xfs_dabuf_t
*bp
, /* block buffer */
414 int first
, /* index of first logged leaf */
415 int last
) /* index of last logged leaf */
417 xfs_dir2_block_t
*block
; /* directory block structure */
418 xfs_dir2_leaf_entry_t
*blp
; /* block leaf entries */
419 xfs_dir2_block_tail_t
*btp
; /* block tail */
420 xfs_mount_t
*mp
; /* filesystem mount point */
424 btp
= XFS_DIR2_BLOCK_TAIL_P(mp
, block
);
425 blp
= XFS_DIR2_BLOCK_LEAF_P_ARCH(btp
, ARCH_CONVERT
);
426 xfs_da_log_buf(tp
, bp
, (uint
)((char *)&blp
[first
] - (char *)block
),
427 (uint
)((char *)&blp
[last
+ 1] - (char *)block
- 1));
431 * Log the block tail.
434 xfs_dir2_block_log_tail(
435 xfs_trans_t
*tp
, /* transaction structure */
436 xfs_dabuf_t
*bp
) /* block buffer */
438 xfs_dir2_block_t
*block
; /* directory block structure */
439 xfs_dir2_block_tail_t
*btp
; /* block tail */
440 xfs_mount_t
*mp
; /* filesystem mount point */
444 btp
= XFS_DIR2_BLOCK_TAIL_P(mp
, block
);
445 xfs_da_log_buf(tp
, bp
, (uint
)((char *)btp
- (char *)block
),
446 (uint
)((char *)(btp
+ 1) - (char *)block
- 1));
450 * Look up an entry in the block. This is the external routine,
451 * xfs_dir2_block_lookup_int does the real work.
454 xfs_dir2_block_lookup(
455 xfs_da_args_t
*args
) /* dir lookup arguments */
457 xfs_dir2_block_t
*block
; /* block structure */
458 xfs_dir2_leaf_entry_t
*blp
; /* block leaf entries */
459 xfs_dabuf_t
*bp
; /* block buffer */
460 xfs_dir2_block_tail_t
*btp
; /* block tail */
461 xfs_dir2_data_entry_t
*dep
; /* block data entry */
462 xfs_inode_t
*dp
; /* incore inode */
463 int ent
; /* entry index */
464 int error
; /* error return value */
465 xfs_mount_t
*mp
; /* filesystem mount point */
467 xfs_dir2_trace_args("block_lookup", args
);
469 * Get the buffer, look up the entry.
470 * If not found (ENOENT) then return, have no buffer.
472 if ((error
= xfs_dir2_block_lookup_int(args
, &bp
, &ent
)))
477 xfs_dir2_data_check(dp
, bp
);
478 btp
= XFS_DIR2_BLOCK_TAIL_P(mp
, block
);
479 blp
= XFS_DIR2_BLOCK_LEAF_P_ARCH(btp
, ARCH_CONVERT
);
481 * Get the offset from the leaf entry, to point to the data.
483 dep
= (xfs_dir2_data_entry_t
*)
484 ((char *)block
+ XFS_DIR2_DATAPTR_TO_OFF(mp
, INT_GET(blp
[ent
].address
, ARCH_CONVERT
)));
486 * Fill in inode number, release the block.
488 args
->inumber
= INT_GET(dep
->inumber
, ARCH_CONVERT
);
489 xfs_da_brelse(args
->trans
, bp
);
490 return XFS_ERROR(EEXIST
);
494 * Internal block lookup routine.
496 STATIC
int /* error */
497 xfs_dir2_block_lookup_int(
498 xfs_da_args_t
*args
, /* dir lookup arguments */
499 xfs_dabuf_t
**bpp
, /* returned block buffer */
500 int *entno
) /* returned entry number */
502 xfs_dir2_dataptr_t addr
; /* data entry address */
503 xfs_dir2_block_t
*block
; /* block structure */
504 xfs_dir2_leaf_entry_t
*blp
; /* block leaf entries */
505 xfs_dabuf_t
*bp
; /* block buffer */
506 xfs_dir2_block_tail_t
*btp
; /* block tail */
507 xfs_dir2_data_entry_t
*dep
; /* block data entry */
508 xfs_inode_t
*dp
; /* incore inode */
509 int error
; /* error return value */
510 xfs_dahash_t hash
; /* found hash value */
511 int high
; /* binary search high index */
512 int low
; /* binary search low index */
513 int mid
; /* binary search current idx */
514 xfs_mount_t
*mp
; /* filesystem mount point */
515 xfs_trans_t
*tp
; /* transaction pointer */
521 * Read the buffer, return error if we can't get it.
524 xfs_da_read_buf(tp
, dp
, mp
->m_dirdatablk
, -1, &bp
, XFS_DATA_FORK
))) {
529 xfs_dir2_data_check(dp
, bp
);
530 btp
= XFS_DIR2_BLOCK_TAIL_P(mp
, block
);
531 blp
= XFS_DIR2_BLOCK_LEAF_P_ARCH(btp
, ARCH_CONVERT
);
533 * Loop doing a binary search for our hash value.
534 * Find our entry, ENOENT if it's not there.
536 for (low
= 0, high
= INT_GET(btp
->count
, ARCH_CONVERT
) - 1; ; ) {
538 mid
= (low
+ high
) >> 1;
539 if ((hash
= INT_GET(blp
[mid
].hashval
, ARCH_CONVERT
)) == args
->hashval
)
541 if (hash
< args
->hashval
)
546 ASSERT(args
->oknoent
);
547 xfs_da_brelse(tp
, bp
);
548 return XFS_ERROR(ENOENT
);
552 * Back up to the first one with the right hash value.
554 while (mid
> 0 && INT_GET(blp
[mid
- 1].hashval
, ARCH_CONVERT
) == args
->hashval
) {
558 * Now loop forward through all the entries with the
559 * right hash value looking for our name.
562 if ((addr
= INT_GET(blp
[mid
].address
, ARCH_CONVERT
)) == XFS_DIR2_NULL_DATAPTR
)
565 * Get pointer to the entry from the leaf.
567 dep
= (xfs_dir2_data_entry_t
*)
568 ((char *)block
+ XFS_DIR2_DATAPTR_TO_OFF(mp
, addr
));
570 * Compare, if it's right give back buffer & entry number.
572 if (dep
->namelen
== args
->namelen
&&
573 dep
->name
[0] == args
->name
[0] &&
574 memcmp(dep
->name
, args
->name
, args
->namelen
) == 0) {
579 } while (++mid
< INT_GET(btp
->count
, ARCH_CONVERT
) && INT_GET(blp
[mid
].hashval
, ARCH_CONVERT
) == hash
);
581 * No match, release the buffer and return ENOENT.
583 ASSERT(args
->oknoent
);
584 xfs_da_brelse(tp
, bp
);
585 return XFS_ERROR(ENOENT
);
589 * Remove an entry from a block format directory.
590 * If that makes the block small enough to fit in shortform, transform it.
593 xfs_dir2_block_removename(
594 xfs_da_args_t
*args
) /* directory operation args */
596 xfs_dir2_block_t
*block
; /* block structure */
597 xfs_dir2_leaf_entry_t
*blp
; /* block leaf pointer */
598 xfs_dabuf_t
*bp
; /* block buffer */
599 xfs_dir2_block_tail_t
*btp
; /* block tail */
600 xfs_dir2_data_entry_t
*dep
; /* block data entry */
601 xfs_inode_t
*dp
; /* incore inode */
602 int ent
; /* block leaf entry index */
603 int error
; /* error return value */
604 xfs_mount_t
*mp
; /* filesystem mount point */
605 int needlog
; /* need to log block header */
606 int needscan
; /* need to fixup bestfree */
607 xfs_dir2_sf_hdr_t sfh
; /* shortform header */
608 int size
; /* shortform size */
609 xfs_trans_t
*tp
; /* transaction pointer */
611 xfs_dir2_trace_args("block_removename", args
);
613 * Look up the entry in the block. Gets the buffer and entry index.
614 * It will always be there, the vnodeops level does a lookup first.
616 if ((error
= xfs_dir2_block_lookup_int(args
, &bp
, &ent
))) {
623 btp
= XFS_DIR2_BLOCK_TAIL_P(mp
, block
);
624 blp
= XFS_DIR2_BLOCK_LEAF_P_ARCH(btp
, ARCH_CONVERT
);
626 * Point to the data entry using the leaf entry.
628 dep
= (xfs_dir2_data_entry_t
*)
629 ((char *)block
+ XFS_DIR2_DATAPTR_TO_OFF(mp
, INT_GET(blp
[ent
].address
, ARCH_CONVERT
)));
631 * Mark the data entry's space free.
633 needlog
= needscan
= 0;
634 xfs_dir2_data_make_free(tp
, bp
,
635 (xfs_dir2_data_aoff_t
)((char *)dep
- (char *)block
),
636 XFS_DIR2_DATA_ENTSIZE(dep
->namelen
), &needlog
, &needscan
);
638 * Fix up the block tail.
640 INT_MOD(btp
->stale
, ARCH_CONVERT
, +1);
641 xfs_dir2_block_log_tail(tp
, bp
);
643 * Remove the leaf entry by marking it stale.
645 INT_SET(blp
[ent
].address
, ARCH_CONVERT
, XFS_DIR2_NULL_DATAPTR
);
646 xfs_dir2_block_log_leaf(tp
, bp
, ent
, ent
);
648 * Fix up bestfree, log the header if necessary.
651 xfs_dir2_data_freescan(mp
, (xfs_dir2_data_t
*)block
, &needlog
,
654 xfs_dir2_data_log_header(tp
, bp
);
655 xfs_dir2_data_check(dp
, bp
);
657 * See if the size as a shortform is good enough.
659 if ((size
= xfs_dir2_block_sfsize(dp
, block
, &sfh
)) >
660 XFS_IFORK_DSIZE(dp
)) {
665 * If it works, do the conversion.
667 return xfs_dir2_block_to_sf(args
, bp
, size
, &sfh
);
671 * Replace an entry in a V2 block directory.
672 * Change the inode number to the new value.
675 xfs_dir2_block_replace(
676 xfs_da_args_t
*args
) /* directory operation args */
678 xfs_dir2_block_t
*block
; /* block structure */
679 xfs_dir2_leaf_entry_t
*blp
; /* block leaf entries */
680 xfs_dabuf_t
*bp
; /* block buffer */
681 xfs_dir2_block_tail_t
*btp
; /* block tail */
682 xfs_dir2_data_entry_t
*dep
; /* block data entry */
683 xfs_inode_t
*dp
; /* incore inode */
684 int ent
; /* leaf entry index */
685 int error
; /* error return value */
686 xfs_mount_t
*mp
; /* filesystem mount point */
688 xfs_dir2_trace_args("block_replace", args
);
690 * Lookup the entry in the directory. Get buffer and entry index.
691 * This will always succeed since the caller has already done a lookup.
693 if ((error
= xfs_dir2_block_lookup_int(args
, &bp
, &ent
))) {
699 btp
= XFS_DIR2_BLOCK_TAIL_P(mp
, block
);
700 blp
= XFS_DIR2_BLOCK_LEAF_P_ARCH(btp
, ARCH_CONVERT
);
702 * Point to the data entry we need to change.
704 dep
= (xfs_dir2_data_entry_t
*)
705 ((char *)block
+ XFS_DIR2_DATAPTR_TO_OFF(mp
, INT_GET(blp
[ent
].address
, ARCH_CONVERT
)));
706 ASSERT(INT_GET(dep
->inumber
, ARCH_CONVERT
) != args
->inumber
);
708 * Change the inode number to the new value.
710 INT_SET(dep
->inumber
, ARCH_CONVERT
, args
->inumber
);
711 xfs_dir2_data_log_entry(args
->trans
, bp
, dep
);
712 xfs_dir2_data_check(dp
, bp
);
718 * Qsort comparison routine for the block leaf entries.
720 static int /* sort order */
722 const void *a
, /* first leaf entry */
723 const void *b
) /* second leaf entry */
725 const xfs_dir2_leaf_entry_t
*la
; /* first leaf entry */
726 const xfs_dir2_leaf_entry_t
*lb
; /* second leaf entry */
730 return INT_GET(la
->hashval
, ARCH_CONVERT
) < INT_GET(lb
->hashval
, ARCH_CONVERT
) ? -1 :
731 (INT_GET(la
->hashval
, ARCH_CONVERT
) > INT_GET(lb
->hashval
, ARCH_CONVERT
) ? 1 : 0);
735 * Convert a V2 leaf directory to a V2 block directory if possible.
738 xfs_dir2_leaf_to_block(
739 xfs_da_args_t
*args
, /* operation arguments */
740 xfs_dabuf_t
*lbp
, /* leaf buffer */
741 xfs_dabuf_t
*dbp
) /* data buffer */
743 xfs_dir2_data_off_t
*bestsp
; /* leaf bests table */
744 xfs_dir2_block_t
*block
; /* block structure */
745 xfs_dir2_block_tail_t
*btp
; /* block tail */
746 xfs_inode_t
*dp
; /* incore directory inode */
747 xfs_dir2_data_unused_t
*dup
; /* unused data entry */
748 int error
; /* error return value */
749 int from
; /* leaf from index */
750 xfs_dir2_leaf_t
*leaf
; /* leaf structure */
751 xfs_dir2_leaf_entry_t
*lep
; /* leaf entry */
752 xfs_dir2_leaf_tail_t
*ltp
; /* leaf tail structure */
753 xfs_mount_t
*mp
; /* file system mount point */
754 int needlog
; /* need to log data header */
755 int needscan
; /* need to scan for bestfree */
756 xfs_dir2_sf_hdr_t sfh
; /* shortform header */
757 int size
; /* bytes used */
758 xfs_dir2_data_off_t
*tagp
; /* end of entry (tag) */
759 int to
; /* block/leaf to index */
760 xfs_trans_t
*tp
; /* transaction pointer */
762 xfs_dir2_trace_args_bb("leaf_to_block", args
, lbp
, dbp
);
767 ASSERT(INT_GET(leaf
->hdr
.info
.magic
, ARCH_CONVERT
) == XFS_DIR2_LEAF1_MAGIC
);
768 ltp
= XFS_DIR2_LEAF_TAIL_P(mp
, leaf
);
770 * If there are data blocks other than the first one, take this
771 * opportunity to remove trailing empty data blocks that may have
772 * been left behind during no-space-reservation operations.
773 * These will show up in the leaf bests table.
775 while (dp
->i_d
.di_size
> mp
->m_dirblksize
) {
776 bestsp
= XFS_DIR2_LEAF_BESTS_P_ARCH(ltp
, ARCH_CONVERT
);
777 if (INT_GET(bestsp
[INT_GET(ltp
->bestcount
, ARCH_CONVERT
) - 1], ARCH_CONVERT
) ==
778 mp
->m_dirblksize
- (uint
)sizeof(block
->hdr
)) {
780 xfs_dir2_leaf_trim_data(args
, lbp
,
781 (xfs_dir2_db_t
)(INT_GET(ltp
->bestcount
, ARCH_CONVERT
) - 1))))
789 * Read the data block if we don't already have it, give up if it fails.
792 (error
= xfs_da_read_buf(tp
, dp
, mp
->m_dirdatablk
, -1, &dbp
,
797 ASSERT(INT_GET(block
->hdr
.magic
, ARCH_CONVERT
) == XFS_DIR2_DATA_MAGIC
);
799 * Size of the "leaf" area in the block.
801 size
= (uint
)sizeof(block
->tail
) +
802 (uint
)sizeof(*lep
) * (INT_GET(leaf
->hdr
.count
, ARCH_CONVERT
) - INT_GET(leaf
->hdr
.stale
, ARCH_CONVERT
));
804 * Look at the last data entry.
806 tagp
= (xfs_dir2_data_off_t
*)((char *)block
+ mp
->m_dirblksize
) - 1;
807 dup
= (xfs_dir2_data_unused_t
*)((char *)block
+ INT_GET(*tagp
, ARCH_CONVERT
));
809 * If it's not free or is too short we can't do it.
811 if (INT_GET(dup
->freetag
, ARCH_CONVERT
) != XFS_DIR2_DATA_FREE_TAG
|| INT_GET(dup
->length
, ARCH_CONVERT
) < size
) {
816 * Start converting it to block form.
818 INT_SET(block
->hdr
.magic
, ARCH_CONVERT
, XFS_DIR2_BLOCK_MAGIC
);
822 * Use up the space at the end of the block (blp/btp).
824 xfs_dir2_data_use_free(tp
, dbp
, dup
, mp
->m_dirblksize
- size
, size
,
825 &needlog
, &needscan
);
827 * Initialize the block tail.
829 btp
= XFS_DIR2_BLOCK_TAIL_P(mp
, block
);
830 INT_SET(btp
->count
, ARCH_CONVERT
, INT_GET(leaf
->hdr
.count
, ARCH_CONVERT
) - INT_GET(leaf
->hdr
.stale
, ARCH_CONVERT
));
831 INT_ZERO(btp
->stale
, ARCH_CONVERT
);
832 xfs_dir2_block_log_tail(tp
, dbp
);
834 * Initialize the block leaf area. We compact out stale entries.
836 lep
= XFS_DIR2_BLOCK_LEAF_P_ARCH(btp
, ARCH_CONVERT
);
837 for (from
= to
= 0; from
< INT_GET(leaf
->hdr
.count
, ARCH_CONVERT
); from
++) {
838 if (INT_GET(leaf
->ents
[from
].address
, ARCH_CONVERT
) == XFS_DIR2_NULL_DATAPTR
)
840 lep
[to
++] = leaf
->ents
[from
];
842 ASSERT(to
== INT_GET(btp
->count
, ARCH_CONVERT
));
843 xfs_dir2_block_log_leaf(tp
, dbp
, 0, INT_GET(btp
->count
, ARCH_CONVERT
) - 1);
845 * Scan the bestfree if we need it and log the data block header.
848 xfs_dir2_data_freescan(mp
, (xfs_dir2_data_t
*)block
, &needlog
,
851 xfs_dir2_data_log_header(tp
, dbp
);
853 * Pitch the old leaf block.
855 error
= xfs_da_shrink_inode(args
, mp
->m_dirleafblk
, lbp
);
861 * Now see if the resulting block can be shrunken to shortform.
863 if ((size
= xfs_dir2_block_sfsize(dp
, block
, &sfh
)) >
864 XFS_IFORK_DSIZE(dp
)) {
868 return xfs_dir2_block_to_sf(args
, dbp
, size
, &sfh
);
871 xfs_da_buf_done(lbp
);
873 xfs_da_buf_done(dbp
);
878 * Convert the shortform directory to block form.
881 xfs_dir2_sf_to_block(
882 xfs_da_args_t
*args
) /* operation arguments */
884 xfs_dir2_db_t blkno
; /* dir-relative block # (0) */
885 xfs_dir2_block_t
*block
; /* block structure */
886 xfs_dir2_leaf_entry_t
*blp
; /* block leaf entries */
887 xfs_dabuf_t
*bp
; /* block buffer */
888 xfs_dir2_block_tail_t
*btp
; /* block tail pointer */
889 char *buf
; /* sf buffer */
891 xfs_dir2_data_entry_t
*dep
; /* data entry pointer */
892 xfs_inode_t
*dp
; /* incore directory inode */
893 int dummy
; /* trash */
894 xfs_dir2_data_unused_t
*dup
; /* unused entry pointer */
895 int endoffset
; /* end of data objects */
896 int error
; /* error return value */
898 xfs_mount_t
*mp
; /* filesystem mount point */
899 int needlog
; /* need to log block header */
900 int needscan
; /* need to scan block freespc */
901 int newoffset
; /* offset from current entry */
902 int offset
; /* target block offset */
903 xfs_dir2_sf_entry_t
*sfep
; /* sf entry pointer */
904 xfs_dir2_sf_t
*sfp
; /* shortform structure */
905 xfs_dir2_data_off_t
*tagp
; /* end of data entry */
906 xfs_trans_t
*tp
; /* transaction pointer */
908 xfs_dir2_trace_args("sf_to_block", args
);
912 ASSERT(dp
->i_df
.if_flags
& XFS_IFINLINE
);
914 * Bomb out if the shortform directory is way too short.
916 if (dp
->i_d
.di_size
< offsetof(xfs_dir2_sf_hdr_t
, parent
)) {
917 ASSERT(XFS_FORCED_SHUTDOWN(mp
));
918 return XFS_ERROR(EIO
);
920 ASSERT(dp
->i_df
.if_bytes
== dp
->i_d
.di_size
);
921 ASSERT(dp
->i_df
.if_u1
.if_data
!= NULL
);
922 sfp
= (xfs_dir2_sf_t
*)dp
->i_df
.if_u1
.if_data
;
923 ASSERT(dp
->i_d
.di_size
>= XFS_DIR2_SF_HDR_SIZE(sfp
->hdr
.i8count
));
925 * Copy the directory into the stack buffer.
926 * Then pitch the incore inode data so we can make extents.
929 buf_len
= dp
->i_df
.if_bytes
;
930 buf
= kmem_alloc(dp
->i_df
.if_bytes
, KM_SLEEP
);
932 memcpy(buf
, sfp
, dp
->i_df
.if_bytes
);
933 xfs_idata_realloc(dp
, -dp
->i_df
.if_bytes
, XFS_DATA_FORK
);
935 xfs_trans_log_inode(tp
, dp
, XFS_ILOG_CORE
);
937 * Reset pointer - old sfp is gone.
939 sfp
= (xfs_dir2_sf_t
*)buf
;
941 * Add block 0 to the inode.
943 error
= xfs_dir2_grow_inode(args
, XFS_DIR2_DATA_SPACE
, &blkno
);
945 kmem_free(buf
, buf_len
);
949 * Initialize the data block.
951 error
= xfs_dir2_data_init(args
, blkno
, &bp
);
953 kmem_free(buf
, buf_len
);
957 INT_SET(block
->hdr
.magic
, ARCH_CONVERT
, XFS_DIR2_BLOCK_MAGIC
);
959 * Compute size of block "tail" area.
961 i
= (uint
)sizeof(*btp
) +
962 (INT_GET(sfp
->hdr
.count
, ARCH_CONVERT
) + 2) * (uint
)sizeof(xfs_dir2_leaf_entry_t
);
964 * The whole thing is initialized to free by the init routine.
965 * Say we're using the leaf and tail area.
967 dup
= (xfs_dir2_data_unused_t
*)block
->u
;
968 needlog
= needscan
= 0;
969 xfs_dir2_data_use_free(tp
, bp
, dup
, mp
->m_dirblksize
- i
, i
, &needlog
,
971 ASSERT(needscan
== 0);
975 btp
= XFS_DIR2_BLOCK_TAIL_P(mp
, block
);
976 INT_SET(btp
->count
, ARCH_CONVERT
, INT_GET(sfp
->hdr
.count
, ARCH_CONVERT
) + 2); /* ., .. */
977 INT_ZERO(btp
->stale
, ARCH_CONVERT
);
978 blp
= XFS_DIR2_BLOCK_LEAF_P_ARCH(btp
, ARCH_CONVERT
);
979 endoffset
= (uint
)((char *)blp
- (char *)block
);
981 * Remove the freespace, we'll manage it.
983 xfs_dir2_data_use_free(tp
, bp
, dup
,
984 (xfs_dir2_data_aoff_t
)((char *)dup
- (char *)block
),
985 INT_GET(dup
->length
, ARCH_CONVERT
), &needlog
, &needscan
);
989 dep
= (xfs_dir2_data_entry_t
*)
990 ((char *)block
+ XFS_DIR2_DATA_DOT_OFFSET
);
991 INT_SET(dep
->inumber
, ARCH_CONVERT
, dp
->i_ino
);
994 tagp
= XFS_DIR2_DATA_ENTRY_TAG_P(dep
);
995 INT_SET(*tagp
, ARCH_CONVERT
, (xfs_dir2_data_off_t
)((char *)dep
- (char *)block
));
996 xfs_dir2_data_log_entry(tp
, bp
, dep
);
997 INT_SET(blp
[0].hashval
, ARCH_CONVERT
, xfs_dir_hash_dot
);
998 INT_SET(blp
[0].address
, ARCH_CONVERT
, XFS_DIR2_BYTE_TO_DATAPTR(mp
, (char *)dep
- (char *)block
));
1000 * Create entry for ..
1002 dep
= (xfs_dir2_data_entry_t
*)
1003 ((char *)block
+ XFS_DIR2_DATA_DOTDOT_OFFSET
);
1004 INT_SET(dep
->inumber
, ARCH_CONVERT
, XFS_DIR2_SF_GET_INUMBER_ARCH(sfp
, &sfp
->hdr
.parent
, ARCH_CONVERT
));
1006 dep
->name
[0] = dep
->name
[1] = '.';
1007 tagp
= XFS_DIR2_DATA_ENTRY_TAG_P(dep
);
1008 INT_SET(*tagp
, ARCH_CONVERT
, (xfs_dir2_data_off_t
)((char *)dep
- (char *)block
));
1009 xfs_dir2_data_log_entry(tp
, bp
, dep
);
1010 INT_SET(blp
[1].hashval
, ARCH_CONVERT
, xfs_dir_hash_dotdot
);
1011 INT_SET(blp
[1].address
, ARCH_CONVERT
, XFS_DIR2_BYTE_TO_DATAPTR(mp
, (char *)dep
- (char *)block
));
1012 offset
= XFS_DIR2_DATA_FIRST_OFFSET
;
1014 * Loop over existing entries, stuff them in.
1016 if ((i
= 0) == INT_GET(sfp
->hdr
.count
, ARCH_CONVERT
))
1019 sfep
= XFS_DIR2_SF_FIRSTENTRY(sfp
);
1021 * Need to preserve the existing offset values in the sf directory.
1022 * Insert holes (unused entries) where necessary.
1024 while (offset
< endoffset
) {
1026 * sfep is null when we reach the end of the list.
1029 newoffset
= endoffset
;
1031 newoffset
= XFS_DIR2_SF_GET_OFFSET_ARCH(sfep
, ARCH_CONVERT
);
1033 * There should be a hole here, make one.
1035 if (offset
< newoffset
) {
1036 dup
= (xfs_dir2_data_unused_t
*)
1037 ((char *)block
+ offset
);
1038 INT_SET(dup
->freetag
, ARCH_CONVERT
, XFS_DIR2_DATA_FREE_TAG
);
1039 INT_SET(dup
->length
, ARCH_CONVERT
, newoffset
- offset
);
1040 INT_SET(*XFS_DIR2_DATA_UNUSED_TAG_P_ARCH(dup
, ARCH_CONVERT
), ARCH_CONVERT
,
1041 (xfs_dir2_data_off_t
)
1042 ((char *)dup
- (char *)block
));
1043 xfs_dir2_data_log_unused(tp
, bp
, dup
);
1044 (void)xfs_dir2_data_freeinsert((xfs_dir2_data_t
*)block
,
1046 offset
+= INT_GET(dup
->length
, ARCH_CONVERT
);
1050 * Copy a real entry.
1052 dep
= (xfs_dir2_data_entry_t
*)((char *)block
+ newoffset
);
1053 INT_SET(dep
->inumber
, ARCH_CONVERT
, XFS_DIR2_SF_GET_INUMBER_ARCH(sfp
,
1054 XFS_DIR2_SF_INUMBERP(sfep
), ARCH_CONVERT
));
1055 dep
->namelen
= sfep
->namelen
;
1056 memcpy(dep
->name
, sfep
->name
, dep
->namelen
);
1057 tagp
= XFS_DIR2_DATA_ENTRY_TAG_P(dep
);
1058 INT_SET(*tagp
, ARCH_CONVERT
, (xfs_dir2_data_off_t
)((char *)dep
- (char *)block
));
1059 xfs_dir2_data_log_entry(tp
, bp
, dep
);
1060 INT_SET(blp
[2 + i
].hashval
, ARCH_CONVERT
, xfs_da_hashname((char *)sfep
->name
, sfep
->namelen
));
1061 INT_SET(blp
[2 + i
].address
, ARCH_CONVERT
, XFS_DIR2_BYTE_TO_DATAPTR(mp
,
1062 (char *)dep
- (char *)block
));
1063 offset
= (int)((char *)(tagp
+ 1) - (char *)block
);
1064 if (++i
== INT_GET(sfp
->hdr
.count
, ARCH_CONVERT
))
1067 sfep
= XFS_DIR2_SF_NEXTENTRY(sfp
, sfep
);
1069 /* Done with the temporary buffer */
1070 kmem_free(buf
, buf_len
);
1072 * Sort the leaf entries by hash value.
1074 qsort(blp
, INT_GET(btp
->count
, ARCH_CONVERT
), sizeof(*blp
), xfs_dir2_block_sort
);
1076 * Log the leaf entry area and tail.
1077 * Already logged the header in data_init, ignore needlog.
1079 ASSERT(needscan
== 0);
1080 xfs_dir2_block_log_leaf(tp
, bp
, 0, INT_GET(btp
->count
, ARCH_CONVERT
) - 1);
1081 xfs_dir2_block_log_tail(tp
, bp
);
1082 xfs_dir2_data_check(dp
, bp
);
1083 xfs_da_buf_done(bp
);