2 * Copyright (c) 2000-2003 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/
34 #include "xfs_types.h"
37 #include "xfs_trans.h"
41 #include "xfs_dmapi.h"
42 #include "xfs_mount.h"
43 #include "xfs_da_btree.h"
44 #include "xfs_bmap_btree.h"
45 #include "xfs_dir_sf.h"
46 #include "xfs_dir2_sf.h"
47 #include "xfs_attr_sf.h"
48 #include "xfs_dinode.h"
49 #include "xfs_inode.h"
50 #include "xfs_inode_item.h"
51 #include "xfs_dir_leaf.h"
52 #include "xfs_dir2_data.h"
53 #include "xfs_dir2_leaf.h"
54 #include "xfs_dir2_block.h"
55 #include "xfs_dir2_trace.h"
56 #include "xfs_error.h"
59 * Local function prototypes.
61 static void xfs_dir2_block_log_leaf(xfs_trans_t
*tp
, xfs_dabuf_t
*bp
, int first
,
63 static void xfs_dir2_block_log_tail(xfs_trans_t
*tp
, xfs_dabuf_t
*bp
);
64 static int xfs_dir2_block_lookup_int(xfs_da_args_t
*args
, xfs_dabuf_t
**bpp
,
66 static int xfs_dir2_block_sort(const void *a
, const void *b
);
69 * Add an entry to a block directory.
72 xfs_dir2_block_addname(
73 xfs_da_args_t
*args
) /* directory op arguments */
75 xfs_dir2_data_free_t
*bf
; /* bestfree table in block */
76 xfs_dir2_block_t
*block
; /* directory block structure */
77 xfs_dir2_leaf_entry_t
*blp
; /* block leaf entries */
78 xfs_dabuf_t
*bp
; /* buffer for block */
79 xfs_dir2_block_tail_t
*btp
; /* block tail */
80 int compact
; /* need to compact leaf ents */
81 xfs_dir2_data_entry_t
*dep
; /* block data entry */
82 xfs_inode_t
*dp
; /* directory inode */
83 xfs_dir2_data_unused_t
*dup
; /* block unused entry */
84 int error
; /* error return value */
85 xfs_dir2_data_unused_t
*enddup
=NULL
; /* unused at end of data */
86 xfs_dahash_t hash
; /* hash value of found entry */
87 int high
; /* high index for binary srch */
88 int highstale
; /* high stale index */
89 int lfloghigh
=0; /* last final leaf to log */
90 int lfloglow
=0; /* first final leaf to log */
91 int len
; /* length of the new entry */
92 int low
; /* low index for binary srch */
93 int lowstale
; /* low stale index */
94 int mid
=0; /* midpoint for binary srch */
95 xfs_mount_t
*mp
; /* filesystem mount point */
96 int needlog
; /* need to log header */
97 int needscan
; /* need to rescan freespace */
98 xfs_dir2_data_off_t
*tagp
; /* pointer to tag value */
99 xfs_trans_t
*tp
; /* transaction structure */
101 xfs_dir2_trace_args("block_addname", args
);
106 * Read the (one and only) directory block into dabuf bp.
109 xfs_da_read_buf(tp
, dp
, mp
->m_dirdatablk
, -1, &bp
, XFS_DATA_FORK
))) {
115 * Check the magic number, corrupted if wrong.
117 if (unlikely(INT_GET(block
->hdr
.magic
, ARCH_CONVERT
)
118 != XFS_DIR2_BLOCK_MAGIC
)) {
119 XFS_CORRUPTION_ERROR("xfs_dir2_block_addname",
120 XFS_ERRLEVEL_LOW
, mp
, block
);
121 xfs_da_brelse(tp
, bp
);
122 return XFS_ERROR(EFSCORRUPTED
);
124 len
= XFS_DIR2_DATA_ENTSIZE(args
->namelen
);
126 * Set up pointers to parts of the block.
128 bf
= block
->hdr
.bestfree
;
129 btp
= XFS_DIR2_BLOCK_TAIL_P(mp
, block
);
130 blp
= XFS_DIR2_BLOCK_LEAF_P(btp
);
132 * No stale entries? Need space for entry and new leaf.
136 * Tag just before the first leaf entry.
138 tagp
= (xfs_dir2_data_off_t
*)blp
- 1;
140 * Data object just before the first leaf entry.
142 enddup
= (xfs_dir2_data_unused_t
*)((char *)block
+ INT_GET(*tagp
, ARCH_CONVERT
));
144 * If it's not free then can't do this add without cleaning up:
145 * the space before the first leaf entry needs to be free so it
146 * can be expanded to hold the pointer to the new entry.
148 if (INT_GET(enddup
->freetag
, ARCH_CONVERT
) != XFS_DIR2_DATA_FREE_TAG
)
151 * Check out the biggest freespace and see if it's the same one.
154 dup
= (xfs_dir2_data_unused_t
*)
155 ((char *)block
+ INT_GET(bf
[0].offset
, ARCH_CONVERT
));
158 * It is the biggest freespace, is it too small
159 * to hold the new leaf too?
161 if (INT_GET(dup
->length
, ARCH_CONVERT
) < len
+ (uint
)sizeof(*blp
)) {
163 * Yes, we use the second-largest
164 * entry instead if it works.
166 if (INT_GET(bf
[1].length
, ARCH_CONVERT
) >= len
)
167 dup
= (xfs_dir2_data_unused_t
*)
169 INT_GET(bf
[1].offset
, ARCH_CONVERT
));
175 * Not the same free entry,
176 * just check its length.
178 if (INT_GET(dup
->length
, ARCH_CONVERT
) < len
) {
186 * If there are stale entries we'll use one for the leaf.
187 * Is the biggest entry enough to avoid compaction?
189 else if (INT_GET(bf
[0].length
, ARCH_CONVERT
) >= len
) {
190 dup
= (xfs_dir2_data_unused_t
*)
191 ((char *)block
+ INT_GET(bf
[0].offset
, ARCH_CONVERT
));
195 * Will need to compact to make this work.
199 * Tag just before the first leaf entry.
201 tagp
= (xfs_dir2_data_off_t
*)blp
- 1;
203 * Data object just before the first leaf entry.
205 dup
= (xfs_dir2_data_unused_t
*)((char *)block
+ INT_GET(*tagp
, ARCH_CONVERT
));
207 * If it's not free then the data will go where the
208 * leaf data starts now, if it works at all.
210 if (INT_GET(dup
->freetag
, ARCH_CONVERT
) == XFS_DIR2_DATA_FREE_TAG
) {
211 if (INT_GET(dup
->length
, ARCH_CONVERT
) + (INT_GET(btp
->stale
, ARCH_CONVERT
) - 1) *
212 (uint
)sizeof(*blp
) < len
)
214 } else if ((INT_GET(btp
->stale
, ARCH_CONVERT
) - 1) * (uint
)sizeof(*blp
) < len
)
217 dup
= (xfs_dir2_data_unused_t
*)blp
;
221 * If this isn't a real add, we're done with the buffer.
224 xfs_da_brelse(tp
, bp
);
226 * If we don't have space for the new entry & leaf ...
230 * Not trying to actually do anything, or don't have
231 * a space reservation: return no-space.
233 if (args
->justcheck
|| args
->total
== 0)
234 return XFS_ERROR(ENOSPC
);
236 * Convert to the next larger format.
237 * Then add the new entry in that format.
239 error
= xfs_dir2_block_to_leaf(args
, bp
);
243 return xfs_dir2_leaf_addname(args
);
246 * Just checking, and it would work, so say so.
250 needlog
= needscan
= 0;
252 * If need to compact the leaf entries, do it now.
253 * Leave the highest-numbered stale entry stale.
254 * XXX should be the one closest to mid but mid is not yet computed.
257 int fromidx
; /* source leaf index */
258 int toidx
; /* target leaf index */
260 for (fromidx
= toidx
= INT_GET(btp
->count
, ARCH_CONVERT
) - 1,
261 highstale
= lfloghigh
= -1;
264 if (INT_GET(blp
[fromidx
].address
, ARCH_CONVERT
) == XFS_DIR2_NULL_DATAPTR
) {
274 blp
[toidx
] = blp
[fromidx
];
277 lfloglow
= toidx
+ 1 - (INT_GET(btp
->stale
, ARCH_CONVERT
) - 1);
278 lfloghigh
-= INT_GET(btp
->stale
, ARCH_CONVERT
) - 1;
279 INT_MOD(btp
->count
, ARCH_CONVERT
, -(INT_GET(btp
->stale
, ARCH_CONVERT
) - 1));
280 xfs_dir2_data_make_free(tp
, bp
,
281 (xfs_dir2_data_aoff_t
)((char *)blp
- (char *)block
),
282 (xfs_dir2_data_aoff_t
)((INT_GET(btp
->stale
, ARCH_CONVERT
) - 1) * sizeof(*blp
)),
283 &needlog
, &needscan
);
284 blp
+= INT_GET(btp
->stale
, ARCH_CONVERT
) - 1;
285 INT_SET(btp
->stale
, ARCH_CONVERT
, 1);
287 * If we now need to rebuild the bestfree map, do so.
288 * This needs to happen before the next call to use_free.
291 xfs_dir2_data_freescan(mp
, (xfs_dir2_data_t
*)block
,
297 * Set leaf logging boundaries to impossible state.
298 * For the no-stale case they're set explicitly.
300 else if (INT_GET(btp
->stale
, ARCH_CONVERT
)) {
301 lfloglow
= INT_GET(btp
->count
, ARCH_CONVERT
);
305 * Find the slot that's first lower than our hash value, -1 if none.
307 for (low
= 0, high
= INT_GET(btp
->count
, ARCH_CONVERT
) - 1; low
<= high
; ) {
308 mid
= (low
+ high
) >> 1;
309 if ((hash
= INT_GET(blp
[mid
].hashval
, ARCH_CONVERT
)) == args
->hashval
)
311 if (hash
< args
->hashval
)
316 while (mid
>= 0 && INT_GET(blp
[mid
].hashval
, ARCH_CONVERT
) >= args
->hashval
) {
320 * No stale entries, will use enddup space to hold new leaf.
324 * Mark the space needed for the new leaf entry, now in use.
326 xfs_dir2_data_use_free(tp
, bp
, enddup
,
327 (xfs_dir2_data_aoff_t
)
328 ((char *)enddup
- (char *)block
+ INT_GET(enddup
->length
, ARCH_CONVERT
) -
330 (xfs_dir2_data_aoff_t
)sizeof(*blp
),
331 &needlog
, &needscan
);
333 * Update the tail (entry count).
335 INT_MOD(btp
->count
, ARCH_CONVERT
, +1);
337 * If we now need to rebuild the bestfree map, do so.
338 * This needs to happen before the next call to use_free.
341 xfs_dir2_data_freescan(mp
, (xfs_dir2_data_t
*)block
,
346 * Adjust pointer to the first leaf entry, we're about to move
347 * the table up one to open up space for the new leaf entry.
348 * Then adjust our index to match.
353 memmove(blp
, &blp
[1], mid
* sizeof(*blp
));
358 * Use a stale leaf for our new entry.
363 INT_GET(blp
[lowstale
].address
, ARCH_CONVERT
) != XFS_DIR2_NULL_DATAPTR
;
366 for (highstale
= mid
+ 1;
367 highstale
< INT_GET(btp
->count
, ARCH_CONVERT
) &&
368 INT_GET(blp
[highstale
].address
, ARCH_CONVERT
) != XFS_DIR2_NULL_DATAPTR
&&
369 (lowstale
< 0 || mid
- lowstale
> highstale
- mid
);
373 * Move entries toward the low-numbered stale entry.
376 (highstale
== INT_GET(btp
->count
, ARCH_CONVERT
) ||
377 mid
- lowstale
<= highstale
- mid
)) {
379 memmove(&blp
[lowstale
], &blp
[lowstale
+ 1],
380 (mid
- lowstale
) * sizeof(*blp
));
381 lfloglow
= MIN(lowstale
, lfloglow
);
382 lfloghigh
= MAX(mid
, lfloghigh
);
385 * Move entries toward the high-numbered stale entry.
388 ASSERT(highstale
< INT_GET(btp
->count
, ARCH_CONVERT
));
391 memmove(&blp
[mid
+ 1], &blp
[mid
],
392 (highstale
- mid
) * sizeof(*blp
));
393 lfloglow
= MIN(mid
, lfloglow
);
394 lfloghigh
= MAX(highstale
, lfloghigh
);
396 INT_MOD(btp
->stale
, ARCH_CONVERT
, -1);
399 * Point to the new data entry.
401 dep
= (xfs_dir2_data_entry_t
*)dup
;
403 * Fill in the leaf entry.
405 INT_SET(blp
[mid
].hashval
, ARCH_CONVERT
, args
->hashval
);
406 INT_SET(blp
[mid
].address
, ARCH_CONVERT
, XFS_DIR2_BYTE_TO_DATAPTR(mp
, (char *)dep
- (char *)block
));
407 xfs_dir2_block_log_leaf(tp
, bp
, lfloglow
, lfloghigh
);
409 * Mark space for the data entry used.
411 xfs_dir2_data_use_free(tp
, bp
, dup
,
412 (xfs_dir2_data_aoff_t
)((char *)dup
- (char *)block
),
413 (xfs_dir2_data_aoff_t
)len
, &needlog
, &needscan
);
415 * Create the new data entry.
417 INT_SET(dep
->inumber
, ARCH_CONVERT
, args
->inumber
);
418 dep
->namelen
= args
->namelen
;
419 memcpy(dep
->name
, args
->name
, args
->namelen
);
420 tagp
= XFS_DIR2_DATA_ENTRY_TAG_P(dep
);
421 INT_SET(*tagp
, ARCH_CONVERT
, (xfs_dir2_data_off_t
)((char *)dep
- (char *)block
));
423 * Clean up the bestfree array and log the header, tail, and entry.
426 xfs_dir2_data_freescan(mp
, (xfs_dir2_data_t
*)block
, &needlog
,
429 xfs_dir2_data_log_header(tp
, bp
);
430 xfs_dir2_block_log_tail(tp
, bp
);
431 xfs_dir2_data_log_entry(tp
, bp
, dep
);
432 xfs_dir2_data_check(dp
, bp
);
438 * Readdir for block directories.
441 xfs_dir2_block_getdents(
442 xfs_trans_t
*tp
, /* transaction (NULL) */
443 xfs_inode_t
*dp
, /* incore inode */
444 uio_t
*uio
, /* caller's buffer control */
445 int *eofp
, /* eof reached? (out) */
446 xfs_dirent_t
*dbp
, /* caller's buffer */
447 xfs_dir2_put_t put
) /* abi's formatting function */
449 xfs_dir2_block_t
*block
; /* directory block structure */
450 xfs_dabuf_t
*bp
; /* buffer for block */
451 xfs_dir2_block_tail_t
*btp
; /* block tail */
452 xfs_dir2_data_entry_t
*dep
; /* block data entry */
453 xfs_dir2_data_unused_t
*dup
; /* block unused entry */
454 char *endptr
; /* end of the data entries */
455 int error
; /* error return value */
456 xfs_mount_t
*mp
; /* filesystem mount point */
457 xfs_dir2_put_args_t p
; /* arg package for put rtn */
458 char *ptr
; /* current data entry */
459 int wantoff
; /* starting block offset */
463 * If the block number in the offset is out of range, we're done.
465 if (XFS_DIR2_DATAPTR_TO_DB(mp
, uio
->uio_offset
) > mp
->m_dirdatablk
) {
470 * Can't read the block, give up, else get dabuf in bp.
473 xfs_da_read_buf(tp
, dp
, mp
->m_dirdatablk
, -1, &bp
, XFS_DATA_FORK
))) {
478 * Extract the byte offset we start at from the seek pointer.
479 * We'll skip entries before this.
481 wantoff
= XFS_DIR2_DATAPTR_TO_OFF(mp
, uio
->uio_offset
);
483 xfs_dir2_data_check(dp
, bp
);
485 * Set up values for the loop.
487 btp
= XFS_DIR2_BLOCK_TAIL_P(mp
, block
);
488 ptr
= (char *)block
->u
;
489 endptr
= (char *)XFS_DIR2_BLOCK_LEAF_P(btp
);
494 * Loop over the data portion of the block.
495 * Each object is a real entry (dep) or an unused one (dup).
497 while (ptr
< endptr
) {
498 dup
= (xfs_dir2_data_unused_t
*)ptr
;
502 if (INT_GET(dup
->freetag
, ARCH_CONVERT
) == XFS_DIR2_DATA_FREE_TAG
) {
503 ptr
+= INT_GET(dup
->length
, ARCH_CONVERT
);
507 dep
= (xfs_dir2_data_entry_t
*)ptr
;
510 * Bump pointer for the next iteration.
512 ptr
+= XFS_DIR2_DATA_ENTSIZE(dep
->namelen
);
514 * The entry is before the desired starting point, skip it.
516 if ((char *)dep
- (char *)block
< wantoff
)
519 * Set up argument structure for put routine.
521 p
.namelen
= dep
->namelen
;
523 p
.cook
= XFS_DIR2_DB_OFF_TO_DATAPTR(mp
, mp
->m_dirdatablk
,
524 ptr
- (char *)block
);
525 p
.ino
= INT_GET(dep
->inumber
, ARCH_CONVERT
);
527 p
.ino
+= mp
->m_inoadd
;
529 p
.name
= (char *)dep
->name
;
532 * Put the entry in the caller's buffer.
537 * If it didn't fit, set the final offset to here & return.
541 XFS_DIR2_DB_OFF_TO_DATAPTR(mp
, mp
->m_dirdatablk
,
542 (char *)dep
- (char *)block
);
543 xfs_da_brelse(tp
, bp
);
549 * Reached the end of the block.
550 * Set the offset to a nonexistent block 1 and return.
555 XFS_DIR2_DB_OFF_TO_DATAPTR(mp
, mp
->m_dirdatablk
+ 1, 0);
557 xfs_da_brelse(tp
, bp
);
563 * Log leaf entries from the block.
566 xfs_dir2_block_log_leaf(
567 xfs_trans_t
*tp
, /* transaction structure */
568 xfs_dabuf_t
*bp
, /* block buffer */
569 int first
, /* index of first logged leaf */
570 int last
) /* index of last logged leaf */
572 xfs_dir2_block_t
*block
; /* directory block structure */
573 xfs_dir2_leaf_entry_t
*blp
; /* block leaf entries */
574 xfs_dir2_block_tail_t
*btp
; /* block tail */
575 xfs_mount_t
*mp
; /* filesystem mount point */
579 btp
= XFS_DIR2_BLOCK_TAIL_P(mp
, block
);
580 blp
= XFS_DIR2_BLOCK_LEAF_P(btp
);
581 xfs_da_log_buf(tp
, bp
, (uint
)((char *)&blp
[first
] - (char *)block
),
582 (uint
)((char *)&blp
[last
+ 1] - (char *)block
- 1));
586 * Log the block tail.
589 xfs_dir2_block_log_tail(
590 xfs_trans_t
*tp
, /* transaction structure */
591 xfs_dabuf_t
*bp
) /* block buffer */
593 xfs_dir2_block_t
*block
; /* directory block structure */
594 xfs_dir2_block_tail_t
*btp
; /* block tail */
595 xfs_mount_t
*mp
; /* filesystem mount point */
599 btp
= XFS_DIR2_BLOCK_TAIL_P(mp
, block
);
600 xfs_da_log_buf(tp
, bp
, (uint
)((char *)btp
- (char *)block
),
601 (uint
)((char *)(btp
+ 1) - (char *)block
- 1));
605 * Look up an entry in the block. This is the external routine,
606 * xfs_dir2_block_lookup_int does the real work.
609 xfs_dir2_block_lookup(
610 xfs_da_args_t
*args
) /* dir lookup arguments */
612 xfs_dir2_block_t
*block
; /* block structure */
613 xfs_dir2_leaf_entry_t
*blp
; /* block leaf entries */
614 xfs_dabuf_t
*bp
; /* block buffer */
615 xfs_dir2_block_tail_t
*btp
; /* block tail */
616 xfs_dir2_data_entry_t
*dep
; /* block data entry */
617 xfs_inode_t
*dp
; /* incore inode */
618 int ent
; /* entry index */
619 int error
; /* error return value */
620 xfs_mount_t
*mp
; /* filesystem mount point */
622 xfs_dir2_trace_args("block_lookup", args
);
624 * Get the buffer, look up the entry.
625 * If not found (ENOENT) then return, have no buffer.
627 if ((error
= xfs_dir2_block_lookup_int(args
, &bp
, &ent
)))
632 xfs_dir2_data_check(dp
, bp
);
633 btp
= XFS_DIR2_BLOCK_TAIL_P(mp
, block
);
634 blp
= XFS_DIR2_BLOCK_LEAF_P(btp
);
636 * Get the offset from the leaf entry, to point to the data.
638 dep
= (xfs_dir2_data_entry_t
*)
639 ((char *)block
+ XFS_DIR2_DATAPTR_TO_OFF(mp
, INT_GET(blp
[ent
].address
, ARCH_CONVERT
)));
641 * Fill in inode number, release the block.
643 args
->inumber
= INT_GET(dep
->inumber
, ARCH_CONVERT
);
644 xfs_da_brelse(args
->trans
, bp
);
645 return XFS_ERROR(EEXIST
);
649 * Internal block lookup routine.
651 static int /* error */
652 xfs_dir2_block_lookup_int(
653 xfs_da_args_t
*args
, /* dir lookup arguments */
654 xfs_dabuf_t
**bpp
, /* returned block buffer */
655 int *entno
) /* returned entry number */
657 xfs_dir2_dataptr_t addr
; /* data entry address */
658 xfs_dir2_block_t
*block
; /* block structure */
659 xfs_dir2_leaf_entry_t
*blp
; /* block leaf entries */
660 xfs_dabuf_t
*bp
; /* block buffer */
661 xfs_dir2_block_tail_t
*btp
; /* block tail */
662 xfs_dir2_data_entry_t
*dep
; /* block data entry */
663 xfs_inode_t
*dp
; /* incore inode */
664 int error
; /* error return value */
665 xfs_dahash_t hash
; /* found hash value */
666 int high
; /* binary search high index */
667 int low
; /* binary search low index */
668 int mid
; /* binary search current idx */
669 xfs_mount_t
*mp
; /* filesystem mount point */
670 xfs_trans_t
*tp
; /* transaction pointer */
676 * Read the buffer, return error if we can't get it.
679 xfs_da_read_buf(tp
, dp
, mp
->m_dirdatablk
, -1, &bp
, XFS_DATA_FORK
))) {
684 xfs_dir2_data_check(dp
, bp
);
685 btp
= XFS_DIR2_BLOCK_TAIL_P(mp
, block
);
686 blp
= XFS_DIR2_BLOCK_LEAF_P(btp
);
688 * Loop doing a binary search for our hash value.
689 * Find our entry, ENOENT if it's not there.
691 for (low
= 0, high
= INT_GET(btp
->count
, ARCH_CONVERT
) - 1; ; ) {
693 mid
= (low
+ high
) >> 1;
694 if ((hash
= INT_GET(blp
[mid
].hashval
, ARCH_CONVERT
)) == args
->hashval
)
696 if (hash
< args
->hashval
)
701 ASSERT(args
->oknoent
);
702 xfs_da_brelse(tp
, bp
);
703 return XFS_ERROR(ENOENT
);
707 * Back up to the first one with the right hash value.
709 while (mid
> 0 && INT_GET(blp
[mid
- 1].hashval
, ARCH_CONVERT
) == args
->hashval
) {
713 * Now loop forward through all the entries with the
714 * right hash value looking for our name.
717 if ((addr
= INT_GET(blp
[mid
].address
, ARCH_CONVERT
)) == XFS_DIR2_NULL_DATAPTR
)
720 * Get pointer to the entry from the leaf.
722 dep
= (xfs_dir2_data_entry_t
*)
723 ((char *)block
+ XFS_DIR2_DATAPTR_TO_OFF(mp
, addr
));
725 * Compare, if it's right give back buffer & entry number.
727 if (dep
->namelen
== args
->namelen
&&
728 dep
->name
[0] == args
->name
[0] &&
729 memcmp(dep
->name
, args
->name
, args
->namelen
) == 0) {
734 } while (++mid
< INT_GET(btp
->count
, ARCH_CONVERT
) && INT_GET(blp
[mid
].hashval
, ARCH_CONVERT
) == hash
);
736 * No match, release the buffer and return ENOENT.
738 ASSERT(args
->oknoent
);
739 xfs_da_brelse(tp
, bp
);
740 return XFS_ERROR(ENOENT
);
744 * Remove an entry from a block format directory.
745 * If that makes the block small enough to fit in shortform, transform it.
748 xfs_dir2_block_removename(
749 xfs_da_args_t
*args
) /* directory operation args */
751 xfs_dir2_block_t
*block
; /* block structure */
752 xfs_dir2_leaf_entry_t
*blp
; /* block leaf pointer */
753 xfs_dabuf_t
*bp
; /* block buffer */
754 xfs_dir2_block_tail_t
*btp
; /* block tail */
755 xfs_dir2_data_entry_t
*dep
; /* block data entry */
756 xfs_inode_t
*dp
; /* incore inode */
757 int ent
; /* block leaf entry index */
758 int error
; /* error return value */
759 xfs_mount_t
*mp
; /* filesystem mount point */
760 int needlog
; /* need to log block header */
761 int needscan
; /* need to fixup bestfree */
762 xfs_dir2_sf_hdr_t sfh
; /* shortform header */
763 int size
; /* shortform size */
764 xfs_trans_t
*tp
; /* transaction pointer */
766 xfs_dir2_trace_args("block_removename", args
);
768 * Look up the entry in the block. Gets the buffer and entry index.
769 * It will always be there, the vnodeops level does a lookup first.
771 if ((error
= xfs_dir2_block_lookup_int(args
, &bp
, &ent
))) {
778 btp
= XFS_DIR2_BLOCK_TAIL_P(mp
, block
);
779 blp
= XFS_DIR2_BLOCK_LEAF_P(btp
);
781 * Point to the data entry using the leaf entry.
783 dep
= (xfs_dir2_data_entry_t
*)
784 ((char *)block
+ XFS_DIR2_DATAPTR_TO_OFF(mp
, INT_GET(blp
[ent
].address
, ARCH_CONVERT
)));
786 * Mark the data entry's space free.
788 needlog
= needscan
= 0;
789 xfs_dir2_data_make_free(tp
, bp
,
790 (xfs_dir2_data_aoff_t
)((char *)dep
- (char *)block
),
791 XFS_DIR2_DATA_ENTSIZE(dep
->namelen
), &needlog
, &needscan
);
793 * Fix up the block tail.
795 INT_MOD(btp
->stale
, ARCH_CONVERT
, +1);
796 xfs_dir2_block_log_tail(tp
, bp
);
798 * Remove the leaf entry by marking it stale.
800 INT_SET(blp
[ent
].address
, ARCH_CONVERT
, XFS_DIR2_NULL_DATAPTR
);
801 xfs_dir2_block_log_leaf(tp
, bp
, ent
, ent
);
803 * Fix up bestfree, log the header if necessary.
806 xfs_dir2_data_freescan(mp
, (xfs_dir2_data_t
*)block
, &needlog
,
809 xfs_dir2_data_log_header(tp
, bp
);
810 xfs_dir2_data_check(dp
, bp
);
812 * See if the size as a shortform is good enough.
814 if ((size
= xfs_dir2_block_sfsize(dp
, block
, &sfh
)) >
815 XFS_IFORK_DSIZE(dp
)) {
820 * If it works, do the conversion.
822 return xfs_dir2_block_to_sf(args
, bp
, size
, &sfh
);
826 * Replace an entry in a V2 block directory.
827 * Change the inode number to the new value.
830 xfs_dir2_block_replace(
831 xfs_da_args_t
*args
) /* directory operation args */
833 xfs_dir2_block_t
*block
; /* block structure */
834 xfs_dir2_leaf_entry_t
*blp
; /* block leaf entries */
835 xfs_dabuf_t
*bp
; /* block buffer */
836 xfs_dir2_block_tail_t
*btp
; /* block tail */
837 xfs_dir2_data_entry_t
*dep
; /* block data entry */
838 xfs_inode_t
*dp
; /* incore inode */
839 int ent
; /* leaf entry index */
840 int error
; /* error return value */
841 xfs_mount_t
*mp
; /* filesystem mount point */
843 xfs_dir2_trace_args("block_replace", args
);
845 * Lookup the entry in the directory. Get buffer and entry index.
846 * This will always succeed since the caller has already done a lookup.
848 if ((error
= xfs_dir2_block_lookup_int(args
, &bp
, &ent
))) {
854 btp
= XFS_DIR2_BLOCK_TAIL_P(mp
, block
);
855 blp
= XFS_DIR2_BLOCK_LEAF_P(btp
);
857 * Point to the data entry we need to change.
859 dep
= (xfs_dir2_data_entry_t
*)
860 ((char *)block
+ XFS_DIR2_DATAPTR_TO_OFF(mp
, INT_GET(blp
[ent
].address
, ARCH_CONVERT
)));
861 ASSERT(INT_GET(dep
->inumber
, ARCH_CONVERT
) != args
->inumber
);
863 * Change the inode number to the new value.
865 INT_SET(dep
->inumber
, ARCH_CONVERT
, args
->inumber
);
866 xfs_dir2_data_log_entry(args
->trans
, bp
, dep
);
867 xfs_dir2_data_check(dp
, bp
);
873 * Qsort comparison routine for the block leaf entries.
875 static int /* sort order */
877 const void *a
, /* first leaf entry */
878 const void *b
) /* second leaf entry */
880 const xfs_dir2_leaf_entry_t
*la
; /* first leaf entry */
881 const xfs_dir2_leaf_entry_t
*lb
; /* second leaf entry */
885 return INT_GET(la
->hashval
, ARCH_CONVERT
) < INT_GET(lb
->hashval
, ARCH_CONVERT
) ? -1 :
886 (INT_GET(la
->hashval
, ARCH_CONVERT
) > INT_GET(lb
->hashval
, ARCH_CONVERT
) ? 1 : 0);
890 * Convert a V2 leaf directory to a V2 block directory if possible.
893 xfs_dir2_leaf_to_block(
894 xfs_da_args_t
*args
, /* operation arguments */
895 xfs_dabuf_t
*lbp
, /* leaf buffer */
896 xfs_dabuf_t
*dbp
) /* data buffer */
898 xfs_dir2_data_off_t
*bestsp
; /* leaf bests table */
899 xfs_dir2_block_t
*block
; /* block structure */
900 xfs_dir2_block_tail_t
*btp
; /* block tail */
901 xfs_inode_t
*dp
; /* incore directory inode */
902 xfs_dir2_data_unused_t
*dup
; /* unused data entry */
903 int error
; /* error return value */
904 int from
; /* leaf from index */
905 xfs_dir2_leaf_t
*leaf
; /* leaf structure */
906 xfs_dir2_leaf_entry_t
*lep
; /* leaf entry */
907 xfs_dir2_leaf_tail_t
*ltp
; /* leaf tail structure */
908 xfs_mount_t
*mp
; /* file system mount point */
909 int needlog
; /* need to log data header */
910 int needscan
; /* need to scan for bestfree */
911 xfs_dir2_sf_hdr_t sfh
; /* shortform header */
912 int size
; /* bytes used */
913 xfs_dir2_data_off_t
*tagp
; /* end of entry (tag) */
914 int to
; /* block/leaf to index */
915 xfs_trans_t
*tp
; /* transaction pointer */
917 xfs_dir2_trace_args_bb("leaf_to_block", args
, lbp
, dbp
);
922 ASSERT(INT_GET(leaf
->hdr
.info
.magic
, ARCH_CONVERT
) == XFS_DIR2_LEAF1_MAGIC
);
923 ltp
= XFS_DIR2_LEAF_TAIL_P(mp
, leaf
);
925 * If there are data blocks other than the first one, take this
926 * opportunity to remove trailing empty data blocks that may have
927 * been left behind during no-space-reservation operations.
928 * These will show up in the leaf bests table.
930 while (dp
->i_d
.di_size
> mp
->m_dirblksize
) {
931 bestsp
= XFS_DIR2_LEAF_BESTS_P(ltp
);
932 if (INT_GET(bestsp
[INT_GET(ltp
->bestcount
, ARCH_CONVERT
) - 1], ARCH_CONVERT
) ==
933 mp
->m_dirblksize
- (uint
)sizeof(block
->hdr
)) {
935 xfs_dir2_leaf_trim_data(args
, lbp
,
936 (xfs_dir2_db_t
)(INT_GET(ltp
->bestcount
, ARCH_CONVERT
) - 1))))
944 * Read the data block if we don't already have it, give up if it fails.
947 (error
= xfs_da_read_buf(tp
, dp
, mp
->m_dirdatablk
, -1, &dbp
,
952 ASSERT(INT_GET(block
->hdr
.magic
, ARCH_CONVERT
) == XFS_DIR2_DATA_MAGIC
);
954 * Size of the "leaf" area in the block.
956 size
= (uint
)sizeof(block
->tail
) +
957 (uint
)sizeof(*lep
) * (INT_GET(leaf
->hdr
.count
, ARCH_CONVERT
) - INT_GET(leaf
->hdr
.stale
, ARCH_CONVERT
));
959 * Look at the last data entry.
961 tagp
= (xfs_dir2_data_off_t
*)((char *)block
+ mp
->m_dirblksize
) - 1;
962 dup
= (xfs_dir2_data_unused_t
*)((char *)block
+ INT_GET(*tagp
, ARCH_CONVERT
));
964 * If it's not free or is too short we can't do it.
966 if (INT_GET(dup
->freetag
, ARCH_CONVERT
) != XFS_DIR2_DATA_FREE_TAG
|| INT_GET(dup
->length
, ARCH_CONVERT
) < size
) {
971 * Start converting it to block form.
973 INT_SET(block
->hdr
.magic
, ARCH_CONVERT
, XFS_DIR2_BLOCK_MAGIC
);
977 * Use up the space at the end of the block (blp/btp).
979 xfs_dir2_data_use_free(tp
, dbp
, dup
, mp
->m_dirblksize
- size
, size
,
980 &needlog
, &needscan
);
982 * Initialize the block tail.
984 btp
= XFS_DIR2_BLOCK_TAIL_P(mp
, block
);
985 INT_SET(btp
->count
, ARCH_CONVERT
, INT_GET(leaf
->hdr
.count
, ARCH_CONVERT
) - INT_GET(leaf
->hdr
.stale
, ARCH_CONVERT
));
987 xfs_dir2_block_log_tail(tp
, dbp
);
989 * Initialize the block leaf area. We compact out stale entries.
991 lep
= XFS_DIR2_BLOCK_LEAF_P(btp
);
992 for (from
= to
= 0; from
< INT_GET(leaf
->hdr
.count
, ARCH_CONVERT
); from
++) {
993 if (INT_GET(leaf
->ents
[from
].address
, ARCH_CONVERT
) == XFS_DIR2_NULL_DATAPTR
)
995 lep
[to
++] = leaf
->ents
[from
];
997 ASSERT(to
== INT_GET(btp
->count
, ARCH_CONVERT
));
998 xfs_dir2_block_log_leaf(tp
, dbp
, 0, INT_GET(btp
->count
, ARCH_CONVERT
) - 1);
1000 * Scan the bestfree if we need it and log the data block header.
1003 xfs_dir2_data_freescan(mp
, (xfs_dir2_data_t
*)block
, &needlog
,
1006 xfs_dir2_data_log_header(tp
, dbp
);
1008 * Pitch the old leaf block.
1010 error
= xfs_da_shrink_inode(args
, mp
->m_dirleafblk
, lbp
);
1016 * Now see if the resulting block can be shrunken to shortform.
1018 if ((size
= xfs_dir2_block_sfsize(dp
, block
, &sfh
)) >
1019 XFS_IFORK_DSIZE(dp
)) {
1023 return xfs_dir2_block_to_sf(args
, dbp
, size
, &sfh
);
1026 xfs_da_buf_done(lbp
);
1028 xfs_da_buf_done(dbp
);
1033 * Convert the shortform directory to block form.
1036 xfs_dir2_sf_to_block(
1037 xfs_da_args_t
*args
) /* operation arguments */
1039 xfs_dir2_db_t blkno
; /* dir-relative block # (0) */
1040 xfs_dir2_block_t
*block
; /* block structure */
1041 xfs_dir2_leaf_entry_t
*blp
; /* block leaf entries */
1042 xfs_dabuf_t
*bp
; /* block buffer */
1043 xfs_dir2_block_tail_t
*btp
; /* block tail pointer */
1044 char *buf
; /* sf buffer */
1046 xfs_dir2_data_entry_t
*dep
; /* data entry pointer */
1047 xfs_inode_t
*dp
; /* incore directory inode */
1048 int dummy
; /* trash */
1049 xfs_dir2_data_unused_t
*dup
; /* unused entry pointer */
1050 int endoffset
; /* end of data objects */
1051 int error
; /* error return value */
1053 xfs_mount_t
*mp
; /* filesystem mount point */
1054 int needlog
; /* need to log block header */
1055 int needscan
; /* need to scan block freespc */
1056 int newoffset
; /* offset from current entry */
1057 int offset
; /* target block offset */
1058 xfs_dir2_sf_entry_t
*sfep
; /* sf entry pointer */
1059 xfs_dir2_sf_t
*sfp
; /* shortform structure */
1060 xfs_dir2_data_off_t
*tagp
; /* end of data entry */
1061 xfs_trans_t
*tp
; /* transaction pointer */
1063 xfs_dir2_trace_args("sf_to_block", args
);
1067 ASSERT(dp
->i_df
.if_flags
& XFS_IFINLINE
);
1069 * Bomb out if the shortform directory is way too short.
1071 if (dp
->i_d
.di_size
< offsetof(xfs_dir2_sf_hdr_t
, parent
)) {
1072 ASSERT(XFS_FORCED_SHUTDOWN(mp
));
1073 return XFS_ERROR(EIO
);
1075 ASSERT(dp
->i_df
.if_bytes
== dp
->i_d
.di_size
);
1076 ASSERT(dp
->i_df
.if_u1
.if_data
!= NULL
);
1077 sfp
= (xfs_dir2_sf_t
*)dp
->i_df
.if_u1
.if_data
;
1078 ASSERT(dp
->i_d
.di_size
>= XFS_DIR2_SF_HDR_SIZE(sfp
->hdr
.i8count
));
1080 * Copy the directory into the stack buffer.
1081 * Then pitch the incore inode data so we can make extents.
1084 buf_len
= dp
->i_df
.if_bytes
;
1085 buf
= kmem_alloc(dp
->i_df
.if_bytes
, KM_SLEEP
);
1087 memcpy(buf
, sfp
, dp
->i_df
.if_bytes
);
1088 xfs_idata_realloc(dp
, -dp
->i_df
.if_bytes
, XFS_DATA_FORK
);
1089 dp
->i_d
.di_size
= 0;
1090 xfs_trans_log_inode(tp
, dp
, XFS_ILOG_CORE
);
1092 * Reset pointer - old sfp is gone.
1094 sfp
= (xfs_dir2_sf_t
*)buf
;
1096 * Add block 0 to the inode.
1098 error
= xfs_dir2_grow_inode(args
, XFS_DIR2_DATA_SPACE
, &blkno
);
1100 kmem_free(buf
, buf_len
);
1104 * Initialize the data block.
1106 error
= xfs_dir2_data_init(args
, blkno
, &bp
);
1108 kmem_free(buf
, buf_len
);
1112 INT_SET(block
->hdr
.magic
, ARCH_CONVERT
, XFS_DIR2_BLOCK_MAGIC
);
1114 * Compute size of block "tail" area.
1116 i
= (uint
)sizeof(*btp
) +
1117 (INT_GET(sfp
->hdr
.count
, ARCH_CONVERT
) + 2) * (uint
)sizeof(xfs_dir2_leaf_entry_t
);
1119 * The whole thing is initialized to free by the init routine.
1120 * Say we're using the leaf and tail area.
1122 dup
= (xfs_dir2_data_unused_t
*)block
->u
;
1123 needlog
= needscan
= 0;
1124 xfs_dir2_data_use_free(tp
, bp
, dup
, mp
->m_dirblksize
- i
, i
, &needlog
,
1126 ASSERT(needscan
== 0);
1130 btp
= XFS_DIR2_BLOCK_TAIL_P(mp
, block
);
1131 INT_SET(btp
->count
, ARCH_CONVERT
, INT_GET(sfp
->hdr
.count
, ARCH_CONVERT
) + 2); /* ., .. */
1133 blp
= XFS_DIR2_BLOCK_LEAF_P(btp
);
1134 endoffset
= (uint
)((char *)blp
- (char *)block
);
1136 * Remove the freespace, we'll manage it.
1138 xfs_dir2_data_use_free(tp
, bp
, dup
,
1139 (xfs_dir2_data_aoff_t
)((char *)dup
- (char *)block
),
1140 INT_GET(dup
->length
, ARCH_CONVERT
), &needlog
, &needscan
);
1142 * Create entry for .
1144 dep
= (xfs_dir2_data_entry_t
*)
1145 ((char *)block
+ XFS_DIR2_DATA_DOT_OFFSET
);
1146 INT_SET(dep
->inumber
, ARCH_CONVERT
, dp
->i_ino
);
1149 tagp
= XFS_DIR2_DATA_ENTRY_TAG_P(dep
);
1150 INT_SET(*tagp
, ARCH_CONVERT
, (xfs_dir2_data_off_t
)((char *)dep
- (char *)block
));
1151 xfs_dir2_data_log_entry(tp
, bp
, dep
);
1152 INT_SET(blp
[0].hashval
, ARCH_CONVERT
, xfs_dir_hash_dot
);
1153 INT_SET(blp
[0].address
, ARCH_CONVERT
, XFS_DIR2_BYTE_TO_DATAPTR(mp
, (char *)dep
- (char *)block
));
1155 * Create entry for ..
1157 dep
= (xfs_dir2_data_entry_t
*)
1158 ((char *)block
+ XFS_DIR2_DATA_DOTDOT_OFFSET
);
1159 INT_SET(dep
->inumber
, ARCH_CONVERT
, XFS_DIR2_SF_GET_INUMBER(sfp
, &sfp
->hdr
.parent
));
1161 dep
->name
[0] = dep
->name
[1] = '.';
1162 tagp
= XFS_DIR2_DATA_ENTRY_TAG_P(dep
);
1163 INT_SET(*tagp
, ARCH_CONVERT
, (xfs_dir2_data_off_t
)((char *)dep
- (char *)block
));
1164 xfs_dir2_data_log_entry(tp
, bp
, dep
);
1165 INT_SET(blp
[1].hashval
, ARCH_CONVERT
, xfs_dir_hash_dotdot
);
1166 INT_SET(blp
[1].address
, ARCH_CONVERT
, XFS_DIR2_BYTE_TO_DATAPTR(mp
, (char *)dep
- (char *)block
));
1167 offset
= XFS_DIR2_DATA_FIRST_OFFSET
;
1169 * Loop over existing entries, stuff them in.
1171 if ((i
= 0) == INT_GET(sfp
->hdr
.count
, ARCH_CONVERT
))
1174 sfep
= XFS_DIR2_SF_FIRSTENTRY(sfp
);
1176 * Need to preserve the existing offset values in the sf directory.
1177 * Insert holes (unused entries) where necessary.
1179 while (offset
< endoffset
) {
1181 * sfep is null when we reach the end of the list.
1184 newoffset
= endoffset
;
1186 newoffset
= XFS_DIR2_SF_GET_OFFSET(sfep
);
1188 * There should be a hole here, make one.
1190 if (offset
< newoffset
) {
1191 dup
= (xfs_dir2_data_unused_t
*)
1192 ((char *)block
+ offset
);
1193 INT_SET(dup
->freetag
, ARCH_CONVERT
, XFS_DIR2_DATA_FREE_TAG
);
1194 INT_SET(dup
->length
, ARCH_CONVERT
, newoffset
- offset
);
1195 INT_SET(*XFS_DIR2_DATA_UNUSED_TAG_P(dup
), ARCH_CONVERT
,
1196 (xfs_dir2_data_off_t
)
1197 ((char *)dup
- (char *)block
));
1198 xfs_dir2_data_log_unused(tp
, bp
, dup
);
1199 (void)xfs_dir2_data_freeinsert((xfs_dir2_data_t
*)block
,
1201 offset
+= INT_GET(dup
->length
, ARCH_CONVERT
);
1205 * Copy a real entry.
1207 dep
= (xfs_dir2_data_entry_t
*)((char *)block
+ newoffset
);
1208 INT_SET(dep
->inumber
, ARCH_CONVERT
, XFS_DIR2_SF_GET_INUMBER(sfp
,
1209 XFS_DIR2_SF_INUMBERP(sfep
)));
1210 dep
->namelen
= sfep
->namelen
;
1211 memcpy(dep
->name
, sfep
->name
, dep
->namelen
);
1212 tagp
= XFS_DIR2_DATA_ENTRY_TAG_P(dep
);
1213 INT_SET(*tagp
, ARCH_CONVERT
, (xfs_dir2_data_off_t
)((char *)dep
- (char *)block
));
1214 xfs_dir2_data_log_entry(tp
, bp
, dep
);
1215 INT_SET(blp
[2 + i
].hashval
, ARCH_CONVERT
, xfs_da_hashname((char *)sfep
->name
, sfep
->namelen
));
1216 INT_SET(blp
[2 + i
].address
, ARCH_CONVERT
, XFS_DIR2_BYTE_TO_DATAPTR(mp
,
1217 (char *)dep
- (char *)block
));
1218 offset
= (int)((char *)(tagp
+ 1) - (char *)block
);
1219 if (++i
== INT_GET(sfp
->hdr
.count
, ARCH_CONVERT
))
1222 sfep
= XFS_DIR2_SF_NEXTENTRY(sfp
, sfep
);
1224 /* Done with the temporary buffer */
1225 kmem_free(buf
, buf_len
);
1227 * Sort the leaf entries by hash value.
1229 xfs_sort(blp
, INT_GET(btp
->count
, ARCH_CONVERT
), sizeof(*blp
), xfs_dir2_block_sort
);
1231 * Log the leaf entry area and tail.
1232 * Already logged the header in data_init, ignore needlog.
1234 ASSERT(needscan
== 0);
1235 xfs_dir2_block_log_leaf(tp
, bp
, 0, INT_GET(btp
->count
, ARCH_CONVERT
) - 1);
1236 xfs_dir2_block_log_tail(tp
, bp
);
1237 xfs_dir2_data_check(dp
, bp
);
1238 xfs_da_buf_done(bp
);