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/
35 * XFS V2 directory implementation, single-block form.
36 * See xfs_dir2_block.h for the format.
41 #include "xfs_macros.h"
42 #include "xfs_types.h"
45 #include "xfs_trans.h"
49 #include "xfs_dmapi.h"
50 #include "xfs_mount.h"
51 #include "xfs_bmap_btree.h"
52 #include "xfs_attr_sf.h"
53 #include "xfs_dir_sf.h"
54 #include "xfs_dir2_sf.h"
55 #include "xfs_dinode.h"
56 #include "xfs_inode_item.h"
57 #include "xfs_inode.h"
58 #include "xfs_da_btree.h"
59 #include "xfs_dir_leaf.h"
60 #include "xfs_dir2_data.h"
61 #include "xfs_dir2_leaf.h"
62 #include "xfs_dir2_block.h"
63 #include "xfs_dir2_trace.h"
64 #include "xfs_error.h"
67 * Local function prototypes.
69 static void xfs_dir2_block_log_leaf(xfs_trans_t
*tp
, xfs_dabuf_t
*bp
, int first
,
71 static void xfs_dir2_block_log_tail(xfs_trans_t
*tp
, xfs_dabuf_t
*bp
);
72 static int xfs_dir2_block_lookup_int(xfs_da_args_t
*args
, xfs_dabuf_t
**bpp
,
74 static int xfs_dir2_block_sort(const void *a
, const void *b
);
77 * Add an entry to a block directory.
80 xfs_dir2_block_addname(
81 xfs_da_args_t
*args
) /* directory op arguments */
83 xfs_dir2_data_free_t
*bf
; /* bestfree table in block */
84 xfs_dir2_block_t
*block
; /* directory block structure */
85 xfs_dir2_leaf_entry_t
*blp
; /* block leaf entries */
86 xfs_dabuf_t
*bp
; /* buffer for block */
87 xfs_dir2_block_tail_t
*btp
; /* block tail */
88 int compact
; /* need to compact leaf ents */
89 xfs_dir2_data_entry_t
*dep
; /* block data entry */
90 xfs_inode_t
*dp
; /* directory inode */
91 xfs_dir2_data_unused_t
*dup
; /* block unused entry */
92 int error
; /* error return value */
93 xfs_dir2_data_unused_t
*enddup
=NULL
; /* unused at end of data */
94 xfs_dahash_t hash
; /* hash value of found entry */
95 int high
; /* high index for binary srch */
96 int highstale
; /* high stale index */
97 int lfloghigh
=0; /* last final leaf to log */
98 int lfloglow
=0; /* first final leaf to log */
99 int len
; /* length of the new entry */
100 int low
; /* low index for binary srch */
101 int lowstale
; /* low stale index */
102 int mid
=0; /* midpoint for binary srch */
103 xfs_mount_t
*mp
; /* filesystem mount point */
104 int needlog
; /* need to log header */
105 int needscan
; /* need to rescan freespace */
106 xfs_dir2_data_off_t
*tagp
; /* pointer to tag value */
107 xfs_trans_t
*tp
; /* transaction structure */
109 xfs_dir2_trace_args("block_addname", args
);
114 * Read the (one and only) directory block into dabuf bp.
117 xfs_da_read_buf(tp
, dp
, mp
->m_dirdatablk
, -1, &bp
, XFS_DATA_FORK
))) {
123 * Check the magic number, corrupted if wrong.
125 if (unlikely(INT_GET(block
->hdr
.magic
, ARCH_CONVERT
)
126 != XFS_DIR2_BLOCK_MAGIC
)) {
127 XFS_CORRUPTION_ERROR("xfs_dir2_block_addname",
128 XFS_ERRLEVEL_LOW
, mp
, block
);
129 xfs_da_brelse(tp
, bp
);
130 return XFS_ERROR(EFSCORRUPTED
);
132 len
= XFS_DIR2_DATA_ENTSIZE(args
->namelen
);
134 * Set up pointers to parts of the block.
136 bf
= block
->hdr
.bestfree
;
137 btp
= XFS_DIR2_BLOCK_TAIL_P(mp
, block
);
138 blp
= XFS_DIR2_BLOCK_LEAF_P(btp
);
140 * No stale entries? Need space for entry and new leaf.
144 * Tag just before the first leaf entry.
146 tagp
= (xfs_dir2_data_off_t
*)blp
- 1;
148 * Data object just before the first leaf entry.
150 enddup
= (xfs_dir2_data_unused_t
*)((char *)block
+ INT_GET(*tagp
, ARCH_CONVERT
));
152 * If it's not free then can't do this add without cleaning up:
153 * the space before the first leaf entry needs to be free so it
154 * can be expanded to hold the pointer to the new entry.
156 if (INT_GET(enddup
->freetag
, ARCH_CONVERT
) != XFS_DIR2_DATA_FREE_TAG
)
159 * Check out the biggest freespace and see if it's the same one.
162 dup
= (xfs_dir2_data_unused_t
*)
163 ((char *)block
+ INT_GET(bf
[0].offset
, ARCH_CONVERT
));
166 * It is the biggest freespace, is it too small
167 * to hold the new leaf too?
169 if (INT_GET(dup
->length
, ARCH_CONVERT
) < len
+ (uint
)sizeof(*blp
)) {
171 * Yes, we use the second-largest
172 * entry instead if it works.
174 if (INT_GET(bf
[1].length
, ARCH_CONVERT
) >= len
)
175 dup
= (xfs_dir2_data_unused_t
*)
177 INT_GET(bf
[1].offset
, ARCH_CONVERT
));
183 * Not the same free entry,
184 * just check its length.
186 if (INT_GET(dup
->length
, ARCH_CONVERT
) < len
) {
194 * If there are stale entries we'll use one for the leaf.
195 * Is the biggest entry enough to avoid compaction?
197 else if (INT_GET(bf
[0].length
, ARCH_CONVERT
) >= len
) {
198 dup
= (xfs_dir2_data_unused_t
*)
199 ((char *)block
+ INT_GET(bf
[0].offset
, ARCH_CONVERT
));
203 * Will need to compact to make this work.
207 * Tag just before the first leaf entry.
209 tagp
= (xfs_dir2_data_off_t
*)blp
- 1;
211 * Data object just before the first leaf entry.
213 dup
= (xfs_dir2_data_unused_t
*)((char *)block
+ INT_GET(*tagp
, ARCH_CONVERT
));
215 * If it's not free then the data will go where the
216 * leaf data starts now, if it works at all.
218 if (INT_GET(dup
->freetag
, ARCH_CONVERT
) == XFS_DIR2_DATA_FREE_TAG
) {
219 if (INT_GET(dup
->length
, ARCH_CONVERT
) + (INT_GET(btp
->stale
, ARCH_CONVERT
) - 1) *
220 (uint
)sizeof(*blp
) < len
)
222 } else if ((INT_GET(btp
->stale
, ARCH_CONVERT
) - 1) * (uint
)sizeof(*blp
) < len
)
225 dup
= (xfs_dir2_data_unused_t
*)blp
;
229 * If this isn't a real add, we're done with the buffer.
232 xfs_da_brelse(tp
, bp
);
234 * If we don't have space for the new entry & leaf ...
238 * Not trying to actually do anything, or don't have
239 * a space reservation: return no-space.
241 if (args
->justcheck
|| args
->total
== 0)
242 return XFS_ERROR(ENOSPC
);
244 * Convert to the next larger format.
245 * Then add the new entry in that format.
247 error
= xfs_dir2_block_to_leaf(args
, bp
);
251 return xfs_dir2_leaf_addname(args
);
254 * Just checking, and it would work, so say so.
258 needlog
= needscan
= 0;
260 * If need to compact the leaf entries, do it now.
261 * Leave the highest-numbered stale entry stale.
262 * XXX should be the one closest to mid but mid is not yet computed.
265 int fromidx
; /* source leaf index */
266 int toidx
; /* target leaf index */
268 for (fromidx
= toidx
= INT_GET(btp
->count
, ARCH_CONVERT
) - 1,
269 highstale
= lfloghigh
= -1;
272 if (INT_GET(blp
[fromidx
].address
, ARCH_CONVERT
) == XFS_DIR2_NULL_DATAPTR
) {
282 blp
[toidx
] = blp
[fromidx
];
285 lfloglow
= toidx
+ 1 - (INT_GET(btp
->stale
, ARCH_CONVERT
) - 1);
286 lfloghigh
-= INT_GET(btp
->stale
, ARCH_CONVERT
) - 1;
287 INT_MOD(btp
->count
, ARCH_CONVERT
, -(INT_GET(btp
->stale
, ARCH_CONVERT
) - 1));
288 xfs_dir2_data_make_free(tp
, bp
,
289 (xfs_dir2_data_aoff_t
)((char *)blp
- (char *)block
),
290 (xfs_dir2_data_aoff_t
)((INT_GET(btp
->stale
, ARCH_CONVERT
) - 1) * sizeof(*blp
)),
291 &needlog
, &needscan
);
292 blp
+= INT_GET(btp
->stale
, ARCH_CONVERT
) - 1;
293 INT_SET(btp
->stale
, ARCH_CONVERT
, 1);
295 * If we now need to rebuild the bestfree map, do so.
296 * This needs to happen before the next call to use_free.
299 xfs_dir2_data_freescan(mp
, (xfs_dir2_data_t
*)block
,
305 * Set leaf logging boundaries to impossible state.
306 * For the no-stale case they're set explicitly.
308 else if (INT_GET(btp
->stale
, ARCH_CONVERT
)) {
309 lfloglow
= INT_GET(btp
->count
, ARCH_CONVERT
);
313 * Find the slot that's first lower than our hash value, -1 if none.
315 for (low
= 0, high
= INT_GET(btp
->count
, ARCH_CONVERT
) - 1; low
<= high
; ) {
316 mid
= (low
+ high
) >> 1;
317 if ((hash
= INT_GET(blp
[mid
].hashval
, ARCH_CONVERT
)) == args
->hashval
)
319 if (hash
< args
->hashval
)
324 while (mid
>= 0 && INT_GET(blp
[mid
].hashval
, ARCH_CONVERT
) >= args
->hashval
) {
328 * No stale entries, will use enddup space to hold new leaf.
332 * Mark the space needed for the new leaf entry, now in use.
334 xfs_dir2_data_use_free(tp
, bp
, enddup
,
335 (xfs_dir2_data_aoff_t
)
336 ((char *)enddup
- (char *)block
+ INT_GET(enddup
->length
, ARCH_CONVERT
) -
338 (xfs_dir2_data_aoff_t
)sizeof(*blp
),
339 &needlog
, &needscan
);
341 * Update the tail (entry count).
343 INT_MOD(btp
->count
, ARCH_CONVERT
, +1);
345 * If we now need to rebuild the bestfree map, do so.
346 * This needs to happen before the next call to use_free.
349 xfs_dir2_data_freescan(mp
, (xfs_dir2_data_t
*)block
,
354 * Adjust pointer to the first leaf entry, we're about to move
355 * the table up one to open up space for the new leaf entry.
356 * Then adjust our index to match.
361 memmove(blp
, &blp
[1], mid
* sizeof(*blp
));
366 * Use a stale leaf for our new entry.
371 INT_GET(blp
[lowstale
].address
, ARCH_CONVERT
) != XFS_DIR2_NULL_DATAPTR
;
374 for (highstale
= mid
+ 1;
375 highstale
< INT_GET(btp
->count
, ARCH_CONVERT
) &&
376 INT_GET(blp
[highstale
].address
, ARCH_CONVERT
) != XFS_DIR2_NULL_DATAPTR
&&
377 (lowstale
< 0 || mid
- lowstale
> highstale
- mid
);
381 * Move entries toward the low-numbered stale entry.
384 (highstale
== INT_GET(btp
->count
, ARCH_CONVERT
) ||
385 mid
- lowstale
<= highstale
- mid
)) {
387 memmove(&blp
[lowstale
], &blp
[lowstale
+ 1],
388 (mid
- lowstale
) * sizeof(*blp
));
389 lfloglow
= MIN(lowstale
, lfloglow
);
390 lfloghigh
= MAX(mid
, lfloghigh
);
393 * Move entries toward the high-numbered stale entry.
396 ASSERT(highstale
< INT_GET(btp
->count
, ARCH_CONVERT
));
399 memmove(&blp
[mid
+ 1], &blp
[mid
],
400 (highstale
- mid
) * sizeof(*blp
));
401 lfloglow
= MIN(mid
, lfloglow
);
402 lfloghigh
= MAX(highstale
, lfloghigh
);
404 INT_MOD(btp
->stale
, ARCH_CONVERT
, -1);
407 * Point to the new data entry.
409 dep
= (xfs_dir2_data_entry_t
*)dup
;
411 * Fill in the leaf entry.
413 INT_SET(blp
[mid
].hashval
, ARCH_CONVERT
, args
->hashval
);
414 INT_SET(blp
[mid
].address
, ARCH_CONVERT
, XFS_DIR2_BYTE_TO_DATAPTR(mp
, (char *)dep
- (char *)block
));
415 xfs_dir2_block_log_leaf(tp
, bp
, lfloglow
, lfloghigh
);
417 * Mark space for the data entry used.
419 xfs_dir2_data_use_free(tp
, bp
, dup
,
420 (xfs_dir2_data_aoff_t
)((char *)dup
- (char *)block
),
421 (xfs_dir2_data_aoff_t
)len
, &needlog
, &needscan
);
423 * Create the new data entry.
425 INT_SET(dep
->inumber
, ARCH_CONVERT
, args
->inumber
);
426 dep
->namelen
= args
->namelen
;
427 memcpy(dep
->name
, args
->name
, args
->namelen
);
428 tagp
= XFS_DIR2_DATA_ENTRY_TAG_P(dep
);
429 INT_SET(*tagp
, ARCH_CONVERT
, (xfs_dir2_data_off_t
)((char *)dep
- (char *)block
));
431 * Clean up the bestfree array and log the header, tail, and entry.
434 xfs_dir2_data_freescan(mp
, (xfs_dir2_data_t
*)block
, &needlog
,
437 xfs_dir2_data_log_header(tp
, bp
);
438 xfs_dir2_block_log_tail(tp
, bp
);
439 xfs_dir2_data_log_entry(tp
, bp
, dep
);
440 xfs_dir2_data_check(dp
, bp
);
446 * Readdir for block directories.
449 xfs_dir2_block_getdents(
450 xfs_trans_t
*tp
, /* transaction (NULL) */
451 xfs_inode_t
*dp
, /* incore inode */
452 uio_t
*uio
, /* caller's buffer control */
453 int *eofp
, /* eof reached? (out) */
454 xfs_dirent_t
*dbp
, /* caller's buffer */
455 xfs_dir2_put_t put
) /* abi's formatting function */
457 xfs_dir2_block_t
*block
; /* directory block structure */
458 xfs_dabuf_t
*bp
; /* buffer for block */
459 xfs_dir2_block_tail_t
*btp
; /* block tail */
460 xfs_dir2_data_entry_t
*dep
; /* block data entry */
461 xfs_dir2_data_unused_t
*dup
; /* block unused entry */
462 char *endptr
; /* end of the data entries */
463 int error
; /* error return value */
464 xfs_mount_t
*mp
; /* filesystem mount point */
465 xfs_dir2_put_args_t p
; /* arg package for put rtn */
466 char *ptr
; /* current data entry */
467 int wantoff
; /* starting block offset */
471 * If the block number in the offset is out of range, we're done.
473 if (XFS_DIR2_DATAPTR_TO_DB(mp
, uio
->uio_offset
) > mp
->m_dirdatablk
) {
478 * Can't read the block, give up, else get dabuf in bp.
481 xfs_da_read_buf(tp
, dp
, mp
->m_dirdatablk
, -1, &bp
, XFS_DATA_FORK
))) {
486 * Extract the byte offset we start at from the seek pointer.
487 * We'll skip entries before this.
489 wantoff
= XFS_DIR2_DATAPTR_TO_OFF(mp
, uio
->uio_offset
);
491 xfs_dir2_data_check(dp
, bp
);
493 * Set up values for the loop.
495 btp
= XFS_DIR2_BLOCK_TAIL_P(mp
, block
);
496 ptr
= (char *)block
->u
;
497 endptr
= (char *)XFS_DIR2_BLOCK_LEAF_P(btp
);
502 * Loop over the data portion of the block.
503 * Each object is a real entry (dep) or an unused one (dup).
505 while (ptr
< endptr
) {
506 dup
= (xfs_dir2_data_unused_t
*)ptr
;
510 if (INT_GET(dup
->freetag
, ARCH_CONVERT
) == XFS_DIR2_DATA_FREE_TAG
) {
511 ptr
+= INT_GET(dup
->length
, ARCH_CONVERT
);
515 dep
= (xfs_dir2_data_entry_t
*)ptr
;
518 * Bump pointer for the next iteration.
520 ptr
+= XFS_DIR2_DATA_ENTSIZE(dep
->namelen
);
522 * The entry is before the desired starting point, skip it.
524 if ((char *)dep
- (char *)block
< wantoff
)
527 * Set up argument structure for put routine.
529 p
.namelen
= dep
->namelen
;
531 p
.cook
= XFS_DIR2_DB_OFF_TO_DATAPTR(mp
, mp
->m_dirdatablk
,
532 ptr
- (char *)block
);
533 p
.ino
= INT_GET(dep
->inumber
, ARCH_CONVERT
);
535 p
.ino
+= mp
->m_inoadd
;
537 p
.name
= (char *)dep
->name
;
540 * Put the entry in the caller's buffer.
545 * If it didn't fit, set the final offset to here & return.
549 XFS_DIR2_DB_OFF_TO_DATAPTR(mp
, mp
->m_dirdatablk
,
550 (char *)dep
- (char *)block
);
551 xfs_da_brelse(tp
, bp
);
557 * Reached the end of the block.
558 * Set the offset to a nonexistent block 1 and return.
563 XFS_DIR2_DB_OFF_TO_DATAPTR(mp
, mp
->m_dirdatablk
+ 1, 0);
565 xfs_da_brelse(tp
, bp
);
571 * Log leaf entries from the block.
574 xfs_dir2_block_log_leaf(
575 xfs_trans_t
*tp
, /* transaction structure */
576 xfs_dabuf_t
*bp
, /* block buffer */
577 int first
, /* index of first logged leaf */
578 int last
) /* index of last logged leaf */
580 xfs_dir2_block_t
*block
; /* directory block structure */
581 xfs_dir2_leaf_entry_t
*blp
; /* block leaf entries */
582 xfs_dir2_block_tail_t
*btp
; /* block tail */
583 xfs_mount_t
*mp
; /* filesystem mount point */
587 btp
= XFS_DIR2_BLOCK_TAIL_P(mp
, block
);
588 blp
= XFS_DIR2_BLOCK_LEAF_P(btp
);
589 xfs_da_log_buf(tp
, bp
, (uint
)((char *)&blp
[first
] - (char *)block
),
590 (uint
)((char *)&blp
[last
+ 1] - (char *)block
- 1));
594 * Log the block tail.
597 xfs_dir2_block_log_tail(
598 xfs_trans_t
*tp
, /* transaction structure */
599 xfs_dabuf_t
*bp
) /* block buffer */
601 xfs_dir2_block_t
*block
; /* directory block structure */
602 xfs_dir2_block_tail_t
*btp
; /* block tail */
603 xfs_mount_t
*mp
; /* filesystem mount point */
607 btp
= XFS_DIR2_BLOCK_TAIL_P(mp
, block
);
608 xfs_da_log_buf(tp
, bp
, (uint
)((char *)btp
- (char *)block
),
609 (uint
)((char *)(btp
+ 1) - (char *)block
- 1));
613 * Look up an entry in the block. This is the external routine,
614 * xfs_dir2_block_lookup_int does the real work.
617 xfs_dir2_block_lookup(
618 xfs_da_args_t
*args
) /* dir lookup arguments */
620 xfs_dir2_block_t
*block
; /* block structure */
621 xfs_dir2_leaf_entry_t
*blp
; /* block leaf entries */
622 xfs_dabuf_t
*bp
; /* block buffer */
623 xfs_dir2_block_tail_t
*btp
; /* block tail */
624 xfs_dir2_data_entry_t
*dep
; /* block data entry */
625 xfs_inode_t
*dp
; /* incore inode */
626 int ent
; /* entry index */
627 int error
; /* error return value */
628 xfs_mount_t
*mp
; /* filesystem mount point */
630 xfs_dir2_trace_args("block_lookup", args
);
632 * Get the buffer, look up the entry.
633 * If not found (ENOENT) then return, have no buffer.
635 if ((error
= xfs_dir2_block_lookup_int(args
, &bp
, &ent
)))
640 xfs_dir2_data_check(dp
, bp
);
641 btp
= XFS_DIR2_BLOCK_TAIL_P(mp
, block
);
642 blp
= XFS_DIR2_BLOCK_LEAF_P(btp
);
644 * Get the offset from the leaf entry, to point to the data.
646 dep
= (xfs_dir2_data_entry_t
*)
647 ((char *)block
+ XFS_DIR2_DATAPTR_TO_OFF(mp
, INT_GET(blp
[ent
].address
, ARCH_CONVERT
)));
649 * Fill in inode number, release the block.
651 args
->inumber
= INT_GET(dep
->inumber
, ARCH_CONVERT
);
652 xfs_da_brelse(args
->trans
, bp
);
653 return XFS_ERROR(EEXIST
);
657 * Internal block lookup routine.
659 static int /* error */
660 xfs_dir2_block_lookup_int(
661 xfs_da_args_t
*args
, /* dir lookup arguments */
662 xfs_dabuf_t
**bpp
, /* returned block buffer */
663 int *entno
) /* returned entry number */
665 xfs_dir2_dataptr_t addr
; /* data entry address */
666 xfs_dir2_block_t
*block
; /* block structure */
667 xfs_dir2_leaf_entry_t
*blp
; /* block leaf entries */
668 xfs_dabuf_t
*bp
; /* block buffer */
669 xfs_dir2_block_tail_t
*btp
; /* block tail */
670 xfs_dir2_data_entry_t
*dep
; /* block data entry */
671 xfs_inode_t
*dp
; /* incore inode */
672 int error
; /* error return value */
673 xfs_dahash_t hash
; /* found hash value */
674 int high
; /* binary search high index */
675 int low
; /* binary search low index */
676 int mid
; /* binary search current idx */
677 xfs_mount_t
*mp
; /* filesystem mount point */
678 xfs_trans_t
*tp
; /* transaction pointer */
684 * Read the buffer, return error if we can't get it.
687 xfs_da_read_buf(tp
, dp
, mp
->m_dirdatablk
, -1, &bp
, XFS_DATA_FORK
))) {
692 xfs_dir2_data_check(dp
, bp
);
693 btp
= XFS_DIR2_BLOCK_TAIL_P(mp
, block
);
694 blp
= XFS_DIR2_BLOCK_LEAF_P(btp
);
696 * Loop doing a binary search for our hash value.
697 * Find our entry, ENOENT if it's not there.
699 for (low
= 0, high
= INT_GET(btp
->count
, ARCH_CONVERT
) - 1; ; ) {
701 mid
= (low
+ high
) >> 1;
702 if ((hash
= INT_GET(blp
[mid
].hashval
, ARCH_CONVERT
)) == args
->hashval
)
704 if (hash
< args
->hashval
)
709 ASSERT(args
->oknoent
);
710 xfs_da_brelse(tp
, bp
);
711 return XFS_ERROR(ENOENT
);
715 * Back up to the first one with the right hash value.
717 while (mid
> 0 && INT_GET(blp
[mid
- 1].hashval
, ARCH_CONVERT
) == args
->hashval
) {
721 * Now loop forward through all the entries with the
722 * right hash value looking for our name.
725 if ((addr
= INT_GET(blp
[mid
].address
, ARCH_CONVERT
)) == XFS_DIR2_NULL_DATAPTR
)
728 * Get pointer to the entry from the leaf.
730 dep
= (xfs_dir2_data_entry_t
*)
731 ((char *)block
+ XFS_DIR2_DATAPTR_TO_OFF(mp
, addr
));
733 * Compare, if it's right give back buffer & entry number.
735 if (dep
->namelen
== args
->namelen
&&
736 dep
->name
[0] == args
->name
[0] &&
737 memcmp(dep
->name
, args
->name
, args
->namelen
) == 0) {
742 } while (++mid
< INT_GET(btp
->count
, ARCH_CONVERT
) && INT_GET(blp
[mid
].hashval
, ARCH_CONVERT
) == hash
);
744 * No match, release the buffer and return ENOENT.
746 ASSERT(args
->oknoent
);
747 xfs_da_brelse(tp
, bp
);
748 return XFS_ERROR(ENOENT
);
752 * Remove an entry from a block format directory.
753 * If that makes the block small enough to fit in shortform, transform it.
756 xfs_dir2_block_removename(
757 xfs_da_args_t
*args
) /* directory operation args */
759 xfs_dir2_block_t
*block
; /* block structure */
760 xfs_dir2_leaf_entry_t
*blp
; /* block leaf pointer */
761 xfs_dabuf_t
*bp
; /* block buffer */
762 xfs_dir2_block_tail_t
*btp
; /* block tail */
763 xfs_dir2_data_entry_t
*dep
; /* block data entry */
764 xfs_inode_t
*dp
; /* incore inode */
765 int ent
; /* block leaf entry index */
766 int error
; /* error return value */
767 xfs_mount_t
*mp
; /* filesystem mount point */
768 int needlog
; /* need to log block header */
769 int needscan
; /* need to fixup bestfree */
770 xfs_dir2_sf_hdr_t sfh
; /* shortform header */
771 int size
; /* shortform size */
772 xfs_trans_t
*tp
; /* transaction pointer */
774 xfs_dir2_trace_args("block_removename", args
);
776 * Look up the entry in the block. Gets the buffer and entry index.
777 * It will always be there, the vnodeops level does a lookup first.
779 if ((error
= xfs_dir2_block_lookup_int(args
, &bp
, &ent
))) {
786 btp
= XFS_DIR2_BLOCK_TAIL_P(mp
, block
);
787 blp
= XFS_DIR2_BLOCK_LEAF_P(btp
);
789 * Point to the data entry using the leaf entry.
791 dep
= (xfs_dir2_data_entry_t
*)
792 ((char *)block
+ XFS_DIR2_DATAPTR_TO_OFF(mp
, INT_GET(blp
[ent
].address
, ARCH_CONVERT
)));
794 * Mark the data entry's space free.
796 needlog
= needscan
= 0;
797 xfs_dir2_data_make_free(tp
, bp
,
798 (xfs_dir2_data_aoff_t
)((char *)dep
- (char *)block
),
799 XFS_DIR2_DATA_ENTSIZE(dep
->namelen
), &needlog
, &needscan
);
801 * Fix up the block tail.
803 INT_MOD(btp
->stale
, ARCH_CONVERT
, +1);
804 xfs_dir2_block_log_tail(tp
, bp
);
806 * Remove the leaf entry by marking it stale.
808 INT_SET(blp
[ent
].address
, ARCH_CONVERT
, XFS_DIR2_NULL_DATAPTR
);
809 xfs_dir2_block_log_leaf(tp
, bp
, ent
, ent
);
811 * Fix up bestfree, log the header if necessary.
814 xfs_dir2_data_freescan(mp
, (xfs_dir2_data_t
*)block
, &needlog
,
817 xfs_dir2_data_log_header(tp
, bp
);
818 xfs_dir2_data_check(dp
, bp
);
820 * See if the size as a shortform is good enough.
822 if ((size
= xfs_dir2_block_sfsize(dp
, block
, &sfh
)) >
823 XFS_IFORK_DSIZE(dp
)) {
828 * If it works, do the conversion.
830 return xfs_dir2_block_to_sf(args
, bp
, size
, &sfh
);
834 * Replace an entry in a V2 block directory.
835 * Change the inode number to the new value.
838 xfs_dir2_block_replace(
839 xfs_da_args_t
*args
) /* directory operation args */
841 xfs_dir2_block_t
*block
; /* block structure */
842 xfs_dir2_leaf_entry_t
*blp
; /* block leaf entries */
843 xfs_dabuf_t
*bp
; /* block buffer */
844 xfs_dir2_block_tail_t
*btp
; /* block tail */
845 xfs_dir2_data_entry_t
*dep
; /* block data entry */
846 xfs_inode_t
*dp
; /* incore inode */
847 int ent
; /* leaf entry index */
848 int error
; /* error return value */
849 xfs_mount_t
*mp
; /* filesystem mount point */
851 xfs_dir2_trace_args("block_replace", args
);
853 * Lookup the entry in the directory. Get buffer and entry index.
854 * This will always succeed since the caller has already done a lookup.
856 if ((error
= xfs_dir2_block_lookup_int(args
, &bp
, &ent
))) {
862 btp
= XFS_DIR2_BLOCK_TAIL_P(mp
, block
);
863 blp
= XFS_DIR2_BLOCK_LEAF_P(btp
);
865 * Point to the data entry we need to change.
867 dep
= (xfs_dir2_data_entry_t
*)
868 ((char *)block
+ XFS_DIR2_DATAPTR_TO_OFF(mp
, INT_GET(blp
[ent
].address
, ARCH_CONVERT
)));
869 ASSERT(INT_GET(dep
->inumber
, ARCH_CONVERT
) != args
->inumber
);
871 * Change the inode number to the new value.
873 INT_SET(dep
->inumber
, ARCH_CONVERT
, args
->inumber
);
874 xfs_dir2_data_log_entry(args
->trans
, bp
, dep
);
875 xfs_dir2_data_check(dp
, bp
);
881 * Qsort comparison routine for the block leaf entries.
883 static int /* sort order */
885 const void *a
, /* first leaf entry */
886 const void *b
) /* second leaf entry */
888 const xfs_dir2_leaf_entry_t
*la
; /* first leaf entry */
889 const xfs_dir2_leaf_entry_t
*lb
; /* second leaf entry */
893 return INT_GET(la
->hashval
, ARCH_CONVERT
) < INT_GET(lb
->hashval
, ARCH_CONVERT
) ? -1 :
894 (INT_GET(la
->hashval
, ARCH_CONVERT
) > INT_GET(lb
->hashval
, ARCH_CONVERT
) ? 1 : 0);
898 * Convert a V2 leaf directory to a V2 block directory if possible.
901 xfs_dir2_leaf_to_block(
902 xfs_da_args_t
*args
, /* operation arguments */
903 xfs_dabuf_t
*lbp
, /* leaf buffer */
904 xfs_dabuf_t
*dbp
) /* data buffer */
906 xfs_dir2_data_off_t
*bestsp
; /* leaf bests table */
907 xfs_dir2_block_t
*block
; /* block structure */
908 xfs_dir2_block_tail_t
*btp
; /* block tail */
909 xfs_inode_t
*dp
; /* incore directory inode */
910 xfs_dir2_data_unused_t
*dup
; /* unused data entry */
911 int error
; /* error return value */
912 int from
; /* leaf from index */
913 xfs_dir2_leaf_t
*leaf
; /* leaf structure */
914 xfs_dir2_leaf_entry_t
*lep
; /* leaf entry */
915 xfs_dir2_leaf_tail_t
*ltp
; /* leaf tail structure */
916 xfs_mount_t
*mp
; /* file system mount point */
917 int needlog
; /* need to log data header */
918 int needscan
; /* need to scan for bestfree */
919 xfs_dir2_sf_hdr_t sfh
; /* shortform header */
920 int size
; /* bytes used */
921 xfs_dir2_data_off_t
*tagp
; /* end of entry (tag) */
922 int to
; /* block/leaf to index */
923 xfs_trans_t
*tp
; /* transaction pointer */
925 xfs_dir2_trace_args_bb("leaf_to_block", args
, lbp
, dbp
);
930 ASSERT(INT_GET(leaf
->hdr
.info
.magic
, ARCH_CONVERT
) == XFS_DIR2_LEAF1_MAGIC
);
931 ltp
= XFS_DIR2_LEAF_TAIL_P(mp
, leaf
);
933 * If there are data blocks other than the first one, take this
934 * opportunity to remove trailing empty data blocks that may have
935 * been left behind during no-space-reservation operations.
936 * These will show up in the leaf bests table.
938 while (dp
->i_d
.di_size
> mp
->m_dirblksize
) {
939 bestsp
= XFS_DIR2_LEAF_BESTS_P(ltp
);
940 if (INT_GET(bestsp
[INT_GET(ltp
->bestcount
, ARCH_CONVERT
) - 1], ARCH_CONVERT
) ==
941 mp
->m_dirblksize
- (uint
)sizeof(block
->hdr
)) {
943 xfs_dir2_leaf_trim_data(args
, lbp
,
944 (xfs_dir2_db_t
)(INT_GET(ltp
->bestcount
, ARCH_CONVERT
) - 1))))
952 * Read the data block if we don't already have it, give up if it fails.
955 (error
= xfs_da_read_buf(tp
, dp
, mp
->m_dirdatablk
, -1, &dbp
,
960 ASSERT(INT_GET(block
->hdr
.magic
, ARCH_CONVERT
) == XFS_DIR2_DATA_MAGIC
);
962 * Size of the "leaf" area in the block.
964 size
= (uint
)sizeof(block
->tail
) +
965 (uint
)sizeof(*lep
) * (INT_GET(leaf
->hdr
.count
, ARCH_CONVERT
) - INT_GET(leaf
->hdr
.stale
, ARCH_CONVERT
));
967 * Look at the last data entry.
969 tagp
= (xfs_dir2_data_off_t
*)((char *)block
+ mp
->m_dirblksize
) - 1;
970 dup
= (xfs_dir2_data_unused_t
*)((char *)block
+ INT_GET(*tagp
, ARCH_CONVERT
));
972 * If it's not free or is too short we can't do it.
974 if (INT_GET(dup
->freetag
, ARCH_CONVERT
) != XFS_DIR2_DATA_FREE_TAG
|| INT_GET(dup
->length
, ARCH_CONVERT
) < size
) {
979 * Start converting it to block form.
981 INT_SET(block
->hdr
.magic
, ARCH_CONVERT
, XFS_DIR2_BLOCK_MAGIC
);
985 * Use up the space at the end of the block (blp/btp).
987 xfs_dir2_data_use_free(tp
, dbp
, dup
, mp
->m_dirblksize
- size
, size
,
988 &needlog
, &needscan
);
990 * Initialize the block tail.
992 btp
= XFS_DIR2_BLOCK_TAIL_P(mp
, block
);
993 INT_SET(btp
->count
, ARCH_CONVERT
, INT_GET(leaf
->hdr
.count
, ARCH_CONVERT
) - INT_GET(leaf
->hdr
.stale
, ARCH_CONVERT
));
995 xfs_dir2_block_log_tail(tp
, dbp
);
997 * Initialize the block leaf area. We compact out stale entries.
999 lep
= XFS_DIR2_BLOCK_LEAF_P(btp
);
1000 for (from
= to
= 0; from
< INT_GET(leaf
->hdr
.count
, ARCH_CONVERT
); from
++) {
1001 if (INT_GET(leaf
->ents
[from
].address
, ARCH_CONVERT
) == XFS_DIR2_NULL_DATAPTR
)
1003 lep
[to
++] = leaf
->ents
[from
];
1005 ASSERT(to
== INT_GET(btp
->count
, ARCH_CONVERT
));
1006 xfs_dir2_block_log_leaf(tp
, dbp
, 0, INT_GET(btp
->count
, ARCH_CONVERT
) - 1);
1008 * Scan the bestfree if we need it and log the data block header.
1011 xfs_dir2_data_freescan(mp
, (xfs_dir2_data_t
*)block
, &needlog
,
1014 xfs_dir2_data_log_header(tp
, dbp
);
1016 * Pitch the old leaf block.
1018 error
= xfs_da_shrink_inode(args
, mp
->m_dirleafblk
, lbp
);
1024 * Now see if the resulting block can be shrunken to shortform.
1026 if ((size
= xfs_dir2_block_sfsize(dp
, block
, &sfh
)) >
1027 XFS_IFORK_DSIZE(dp
)) {
1031 return xfs_dir2_block_to_sf(args
, dbp
, size
, &sfh
);
1034 xfs_da_buf_done(lbp
);
1036 xfs_da_buf_done(dbp
);
1041 * Convert the shortform directory to block form.
1044 xfs_dir2_sf_to_block(
1045 xfs_da_args_t
*args
) /* operation arguments */
1047 xfs_dir2_db_t blkno
; /* dir-relative block # (0) */
1048 xfs_dir2_block_t
*block
; /* block structure */
1049 xfs_dir2_leaf_entry_t
*blp
; /* block leaf entries */
1050 xfs_dabuf_t
*bp
; /* block buffer */
1051 xfs_dir2_block_tail_t
*btp
; /* block tail pointer */
1052 char *buf
; /* sf buffer */
1054 xfs_dir2_data_entry_t
*dep
; /* data entry pointer */
1055 xfs_inode_t
*dp
; /* incore directory inode */
1056 int dummy
; /* trash */
1057 xfs_dir2_data_unused_t
*dup
; /* unused entry pointer */
1058 int endoffset
; /* end of data objects */
1059 int error
; /* error return value */
1061 xfs_mount_t
*mp
; /* filesystem mount point */
1062 int needlog
; /* need to log block header */
1063 int needscan
; /* need to scan block freespc */
1064 int newoffset
; /* offset from current entry */
1065 int offset
; /* target block offset */
1066 xfs_dir2_sf_entry_t
*sfep
; /* sf entry pointer */
1067 xfs_dir2_sf_t
*sfp
; /* shortform structure */
1068 xfs_dir2_data_off_t
*tagp
; /* end of data entry */
1069 xfs_trans_t
*tp
; /* transaction pointer */
1071 xfs_dir2_trace_args("sf_to_block", args
);
1075 ASSERT(dp
->i_df
.if_flags
& XFS_IFINLINE
);
1077 * Bomb out if the shortform directory is way too short.
1079 if (dp
->i_d
.di_size
< offsetof(xfs_dir2_sf_hdr_t
, parent
)) {
1080 ASSERT(XFS_FORCED_SHUTDOWN(mp
));
1081 return XFS_ERROR(EIO
);
1083 ASSERT(dp
->i_df
.if_bytes
== dp
->i_d
.di_size
);
1084 ASSERT(dp
->i_df
.if_u1
.if_data
!= NULL
);
1085 sfp
= (xfs_dir2_sf_t
*)dp
->i_df
.if_u1
.if_data
;
1086 ASSERT(dp
->i_d
.di_size
>= XFS_DIR2_SF_HDR_SIZE(sfp
->hdr
.i8count
));
1088 * Copy the directory into the stack buffer.
1089 * Then pitch the incore inode data so we can make extents.
1092 buf_len
= dp
->i_df
.if_bytes
;
1093 buf
= kmem_alloc(dp
->i_df
.if_bytes
, KM_SLEEP
);
1095 memcpy(buf
, sfp
, dp
->i_df
.if_bytes
);
1096 xfs_idata_realloc(dp
, -dp
->i_df
.if_bytes
, XFS_DATA_FORK
);
1097 dp
->i_d
.di_size
= 0;
1098 xfs_trans_log_inode(tp
, dp
, XFS_ILOG_CORE
);
1100 * Reset pointer - old sfp is gone.
1102 sfp
= (xfs_dir2_sf_t
*)buf
;
1104 * Add block 0 to the inode.
1106 error
= xfs_dir2_grow_inode(args
, XFS_DIR2_DATA_SPACE
, &blkno
);
1108 kmem_free(buf
, buf_len
);
1112 * Initialize the data block.
1114 error
= xfs_dir2_data_init(args
, blkno
, &bp
);
1116 kmem_free(buf
, buf_len
);
1120 INT_SET(block
->hdr
.magic
, ARCH_CONVERT
, XFS_DIR2_BLOCK_MAGIC
);
1122 * Compute size of block "tail" area.
1124 i
= (uint
)sizeof(*btp
) +
1125 (INT_GET(sfp
->hdr
.count
, ARCH_CONVERT
) + 2) * (uint
)sizeof(xfs_dir2_leaf_entry_t
);
1127 * The whole thing is initialized to free by the init routine.
1128 * Say we're using the leaf and tail area.
1130 dup
= (xfs_dir2_data_unused_t
*)block
->u
;
1131 needlog
= needscan
= 0;
1132 xfs_dir2_data_use_free(tp
, bp
, dup
, mp
->m_dirblksize
- i
, i
, &needlog
,
1134 ASSERT(needscan
== 0);
1138 btp
= XFS_DIR2_BLOCK_TAIL_P(mp
, block
);
1139 INT_SET(btp
->count
, ARCH_CONVERT
, INT_GET(sfp
->hdr
.count
, ARCH_CONVERT
) + 2); /* ., .. */
1141 blp
= XFS_DIR2_BLOCK_LEAF_P(btp
);
1142 endoffset
= (uint
)((char *)blp
- (char *)block
);
1144 * Remove the freespace, we'll manage it.
1146 xfs_dir2_data_use_free(tp
, bp
, dup
,
1147 (xfs_dir2_data_aoff_t
)((char *)dup
- (char *)block
),
1148 INT_GET(dup
->length
, ARCH_CONVERT
), &needlog
, &needscan
);
1150 * Create entry for .
1152 dep
= (xfs_dir2_data_entry_t
*)
1153 ((char *)block
+ XFS_DIR2_DATA_DOT_OFFSET
);
1154 INT_SET(dep
->inumber
, ARCH_CONVERT
, dp
->i_ino
);
1157 tagp
= XFS_DIR2_DATA_ENTRY_TAG_P(dep
);
1158 INT_SET(*tagp
, ARCH_CONVERT
, (xfs_dir2_data_off_t
)((char *)dep
- (char *)block
));
1159 xfs_dir2_data_log_entry(tp
, bp
, dep
);
1160 INT_SET(blp
[0].hashval
, ARCH_CONVERT
, xfs_dir_hash_dot
);
1161 INT_SET(blp
[0].address
, ARCH_CONVERT
, XFS_DIR2_BYTE_TO_DATAPTR(mp
, (char *)dep
- (char *)block
));
1163 * Create entry for ..
1165 dep
= (xfs_dir2_data_entry_t
*)
1166 ((char *)block
+ XFS_DIR2_DATA_DOTDOT_OFFSET
);
1167 INT_SET(dep
->inumber
, ARCH_CONVERT
, XFS_DIR2_SF_GET_INUMBER(sfp
, &sfp
->hdr
.parent
));
1169 dep
->name
[0] = dep
->name
[1] = '.';
1170 tagp
= XFS_DIR2_DATA_ENTRY_TAG_P(dep
);
1171 INT_SET(*tagp
, ARCH_CONVERT
, (xfs_dir2_data_off_t
)((char *)dep
- (char *)block
));
1172 xfs_dir2_data_log_entry(tp
, bp
, dep
);
1173 INT_SET(blp
[1].hashval
, ARCH_CONVERT
, xfs_dir_hash_dotdot
);
1174 INT_SET(blp
[1].address
, ARCH_CONVERT
, XFS_DIR2_BYTE_TO_DATAPTR(mp
, (char *)dep
- (char *)block
));
1175 offset
= XFS_DIR2_DATA_FIRST_OFFSET
;
1177 * Loop over existing entries, stuff them in.
1179 if ((i
= 0) == INT_GET(sfp
->hdr
.count
, ARCH_CONVERT
))
1182 sfep
= XFS_DIR2_SF_FIRSTENTRY(sfp
);
1184 * Need to preserve the existing offset values in the sf directory.
1185 * Insert holes (unused entries) where necessary.
1187 while (offset
< endoffset
) {
1189 * sfep is null when we reach the end of the list.
1192 newoffset
= endoffset
;
1194 newoffset
= XFS_DIR2_SF_GET_OFFSET(sfep
);
1196 * There should be a hole here, make one.
1198 if (offset
< newoffset
) {
1199 dup
= (xfs_dir2_data_unused_t
*)
1200 ((char *)block
+ offset
);
1201 INT_SET(dup
->freetag
, ARCH_CONVERT
, XFS_DIR2_DATA_FREE_TAG
);
1202 INT_SET(dup
->length
, ARCH_CONVERT
, newoffset
- offset
);
1203 INT_SET(*XFS_DIR2_DATA_UNUSED_TAG_P(dup
), ARCH_CONVERT
,
1204 (xfs_dir2_data_off_t
)
1205 ((char *)dup
- (char *)block
));
1206 xfs_dir2_data_log_unused(tp
, bp
, dup
);
1207 (void)xfs_dir2_data_freeinsert((xfs_dir2_data_t
*)block
,
1209 offset
+= INT_GET(dup
->length
, ARCH_CONVERT
);
1213 * Copy a real entry.
1215 dep
= (xfs_dir2_data_entry_t
*)((char *)block
+ newoffset
);
1216 INT_SET(dep
->inumber
, ARCH_CONVERT
, XFS_DIR2_SF_GET_INUMBER(sfp
,
1217 XFS_DIR2_SF_INUMBERP(sfep
)));
1218 dep
->namelen
= sfep
->namelen
;
1219 memcpy(dep
->name
, sfep
->name
, dep
->namelen
);
1220 tagp
= XFS_DIR2_DATA_ENTRY_TAG_P(dep
);
1221 INT_SET(*tagp
, ARCH_CONVERT
, (xfs_dir2_data_off_t
)((char *)dep
- (char *)block
));
1222 xfs_dir2_data_log_entry(tp
, bp
, dep
);
1223 INT_SET(blp
[2 + i
].hashval
, ARCH_CONVERT
, xfs_da_hashname((char *)sfep
->name
, sfep
->namelen
));
1224 INT_SET(blp
[2 + i
].address
, ARCH_CONVERT
, XFS_DIR2_BYTE_TO_DATAPTR(mp
,
1225 (char *)dep
- (char *)block
));
1226 offset
= (int)((char *)(tagp
+ 1) - (char *)block
);
1227 if (++i
== INT_GET(sfp
->hdr
.count
, ARCH_CONVERT
))
1230 sfep
= XFS_DIR2_SF_NEXTENTRY(sfp
, sfep
);
1232 /* Done with the temporary buffer */
1233 kmem_free(buf
, buf_len
);
1235 * Sort the leaf entries by hash value.
1237 qsort(blp
, INT_GET(btp
->count
, ARCH_CONVERT
), sizeof(*blp
), xfs_dir2_block_sort
);
1239 * Log the leaf entry area and tail.
1240 * Already logged the header in data_init, ignore needlog.
1242 ASSERT(needscan
== 0);
1243 xfs_dir2_block_log_leaf(tp
, bp
, 0, INT_GET(btp
->count
, ARCH_CONVERT
) - 1);
1244 xfs_dir2_block_log_tail(tp
, bp
);
1245 xfs_dir2_data_check(dp
, bp
);
1246 xfs_da_buf_done(bp
);