2 * Copyright (c) 2000-2003,2005 Silicon Graphics, Inc.
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation.
9 * This program is distributed in the hope that it would be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write the Free Software Foundation,
16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 #include "xfs_types.h"
22 #include "xfs_trans.h"
25 #include "xfs_mount.h"
26 #include "xfs_da_btree.h"
27 #include "xfs_bmap_btree.h"
28 #include "xfs_dinode.h"
29 #include "xfs_inode.h"
30 #include "xfs_inode_item.h"
32 #include "xfs_dir2_format.h"
33 #include "xfs_dir2_priv.h"
34 #include "xfs_error.h"
35 #include "xfs_trace.h"
38 * Local function prototypes.
40 static void xfs_dir2_block_log_leaf(xfs_trans_t
*tp
, struct xfs_buf
*bp
,
42 static void xfs_dir2_block_log_tail(xfs_trans_t
*tp
, struct xfs_buf
*bp
);
43 static int xfs_dir2_block_lookup_int(xfs_da_args_t
*args
, struct xfs_buf
**bpp
,
45 static int xfs_dir2_block_sort(const void *a
, const void *b
);
47 static xfs_dahash_t xfs_dir_hash_dot
, xfs_dir_hash_dotdot
;
50 * One-time startup routine called from xfs_init().
55 xfs_dir_hash_dot
= xfs_da_hashname((unsigned char *)".", 1);
56 xfs_dir_hash_dotdot
= xfs_da_hashname((unsigned char *)"..", 2);
60 xfs_dir2_block_verify(
63 struct xfs_mount
*mp
= bp
->b_target
->bt_mount
;
64 struct xfs_dir2_data_hdr
*hdr
= bp
->b_addr
;
67 block_ok
= hdr
->magic
== cpu_to_be32(XFS_DIR2_BLOCK_MAGIC
);
68 block_ok
= block_ok
&& __xfs_dir2_data_check(NULL
, bp
) == 0;
71 XFS_CORRUPTION_ERROR(__func__
, XFS_ERRLEVEL_LOW
, mp
, hdr
);
72 xfs_buf_ioerror(bp
, EFSCORRUPTED
);
77 xfs_dir2_block_read_verify(
80 xfs_dir2_block_verify(bp
);
84 xfs_dir2_block_write_verify(
87 xfs_dir2_block_verify(bp
);
90 const struct xfs_buf_ops xfs_dir2_block_buf_ops
= {
91 .verify_read
= xfs_dir2_block_read_verify
,
92 .verify_write
= xfs_dir2_block_write_verify
,
101 struct xfs_mount
*mp
= dp
->i_mount
;
103 return xfs_da_read_buf(tp
, dp
, mp
->m_dirdatablk
, -1, bpp
,
104 XFS_DATA_FORK
, &xfs_dir2_block_buf_ops
);
108 xfs_dir2_block_need_space(
109 struct xfs_dir2_data_hdr
*hdr
,
110 struct xfs_dir2_block_tail
*btp
,
111 struct xfs_dir2_leaf_entry
*blp
,
113 struct xfs_dir2_data_unused
**dupp
,
114 struct xfs_dir2_data_unused
**enddupp
,
118 struct xfs_dir2_data_free
*bf
;
120 struct xfs_dir2_data_unused
*dup
= NULL
;
121 struct xfs_dir2_data_unused
*enddup
= NULL
;
127 * If there are stale entries we'll use one for the leaf.
130 if (be16_to_cpu(bf
[0].length
) >= len
) {
132 * The biggest entry enough to avoid compaction.
134 dup
= (xfs_dir2_data_unused_t
*)
135 ((char *)hdr
+ be16_to_cpu(bf
[0].offset
));
140 * Will need to compact to make this work.
141 * Tag just before the first leaf entry.
144 tagp
= (__be16
*)blp
- 1;
146 /* Data object just before the first leaf entry. */
147 dup
= (xfs_dir2_data_unused_t
*)((char *)hdr
+ be16_to_cpu(*tagp
));
150 * If it's not free then the data will go where the
151 * leaf data starts now, if it works at all.
153 if (be16_to_cpu(dup
->freetag
) == XFS_DIR2_DATA_FREE_TAG
) {
154 if (be16_to_cpu(dup
->length
) + (be32_to_cpu(btp
->stale
) - 1) *
155 (uint
)sizeof(*blp
) < len
)
157 } else if ((be32_to_cpu(btp
->stale
) - 1) * (uint
)sizeof(*blp
) < len
)
160 dup
= (xfs_dir2_data_unused_t
*)blp
;
165 * no stale entries, so just use free space.
166 * Tag just before the first leaf entry.
168 tagp
= (__be16
*)blp
- 1;
170 /* Data object just before the first leaf entry. */
171 enddup
= (xfs_dir2_data_unused_t
*)((char *)hdr
+ be16_to_cpu(*tagp
));
174 * If it's not free then can't do this add without cleaning up:
175 * the space before the first leaf entry needs to be free so it
176 * can be expanded to hold the pointer to the new entry.
178 if (be16_to_cpu(enddup
->freetag
) == XFS_DIR2_DATA_FREE_TAG
) {
180 * Check out the biggest freespace and see if it's the same one.
182 dup
= (xfs_dir2_data_unused_t
*)
183 ((char *)hdr
+ be16_to_cpu(bf
[0].offset
));
186 * Not the same free entry, just check its length.
188 if (be16_to_cpu(dup
->length
) < len
)
194 * It is the biggest freespace, can it hold the leaf too?
196 if (be16_to_cpu(dup
->length
) < len
+ (uint
)sizeof(*blp
)) {
198 * Yes, use the second-largest entry instead if it works.
200 if (be16_to_cpu(bf
[1].length
) >= len
)
201 dup
= (xfs_dir2_data_unused_t
*)
202 ((char *)hdr
+ be16_to_cpu(bf
[1].offset
));
214 * compact the leaf entries.
215 * Leave the highest-numbered stale entry stale.
216 * XXX should be the one closest to mid but mid is not yet computed.
219 xfs_dir2_block_compact(
220 struct xfs_trans
*tp
,
222 struct xfs_dir2_data_hdr
*hdr
,
223 struct xfs_dir2_block_tail
*btp
,
224 struct xfs_dir2_leaf_entry
*blp
,
229 int fromidx
; /* source leaf index */
230 int toidx
; /* target leaf index */
232 int highstale
; /* high stale index */
234 fromidx
= toidx
= be32_to_cpu(btp
->count
) - 1;
235 highstale
= *lfloghigh
= -1;
236 for (; fromidx
>= 0; fromidx
--) {
237 if (blp
[fromidx
].address
== cpu_to_be32(XFS_DIR2_NULL_DATAPTR
)) {
241 if (*lfloghigh
== -1)
247 blp
[toidx
] = blp
[fromidx
];
250 *lfloglow
= toidx
+ 1 - (be32_to_cpu(btp
->stale
) - 1);
251 *lfloghigh
-= be32_to_cpu(btp
->stale
) - 1;
252 be32_add_cpu(&btp
->count
, -(be32_to_cpu(btp
->stale
) - 1));
253 xfs_dir2_data_make_free(tp
, bp
,
254 (xfs_dir2_data_aoff_t
)((char *)blp
- (char *)hdr
),
255 (xfs_dir2_data_aoff_t
)((be32_to_cpu(btp
->stale
) - 1) * sizeof(*blp
)),
257 blp
+= be32_to_cpu(btp
->stale
) - 1;
258 btp
->stale
= cpu_to_be32(1);
260 * If we now need to rebuild the bestfree map, do so.
261 * This needs to happen before the next call to use_free.
264 xfs_dir2_data_freescan(tp
->t_mountp
, hdr
, needlog
);
268 * Add an entry to a block directory.
271 xfs_dir2_block_addname(
272 xfs_da_args_t
*args
) /* directory op arguments */
274 xfs_dir2_data_hdr_t
*hdr
; /* block header */
275 xfs_dir2_leaf_entry_t
*blp
; /* block leaf entries */
276 struct xfs_buf
*bp
; /* buffer for block */
277 xfs_dir2_block_tail_t
*btp
; /* block tail */
278 int compact
; /* need to compact leaf ents */
279 xfs_dir2_data_entry_t
*dep
; /* block data entry */
280 xfs_inode_t
*dp
; /* directory inode */
281 xfs_dir2_data_unused_t
*dup
; /* block unused entry */
282 int error
; /* error return value */
283 xfs_dir2_data_unused_t
*enddup
=NULL
; /* unused at end of data */
284 xfs_dahash_t hash
; /* hash value of found entry */
285 int high
; /* high index for binary srch */
286 int highstale
; /* high stale index */
287 int lfloghigh
=0; /* last final leaf to log */
288 int lfloglow
=0; /* first final leaf to log */
289 int len
; /* length of the new entry */
290 int low
; /* low index for binary srch */
291 int lowstale
; /* low stale index */
292 int mid
=0; /* midpoint for binary srch */
293 xfs_mount_t
*mp
; /* filesystem mount point */
294 int needlog
; /* need to log header */
295 int needscan
; /* need to rescan freespace */
296 __be16
*tagp
; /* pointer to tag value */
297 xfs_trans_t
*tp
; /* transaction structure */
299 trace_xfs_dir2_block_addname(args
);
305 /* Read the (one and only) directory block into bp. */
306 error
= xfs_dir2_block_read(tp
, dp
, &bp
);
310 len
= xfs_dir2_data_entsize(args
->namelen
);
313 * Set up pointers to parts of the block.
316 btp
= xfs_dir2_block_tail_p(mp
, hdr
);
317 blp
= xfs_dir2_block_leaf_p(btp
);
320 * Find out if we can reuse stale entries or whether we need extra
321 * space for entry and new leaf.
323 xfs_dir2_block_need_space(hdr
, btp
, blp
, &tagp
, &dup
,
324 &enddup
, &compact
, len
);
327 * Done everything we need for a space check now.
329 if (args
->op_flags
& XFS_DA_OP_JUSTCHECK
) {
330 xfs_trans_brelse(tp
, bp
);
332 return XFS_ERROR(ENOSPC
);
337 * If we don't have space for the new entry & leaf ...
340 /* Don't have a space reservation: return no-space. */
341 if (args
->total
== 0)
342 return XFS_ERROR(ENOSPC
);
344 * Convert to the next larger format.
345 * Then add the new entry in that format.
347 error
= xfs_dir2_block_to_leaf(args
, bp
);
350 return xfs_dir2_leaf_addname(args
);
353 needlog
= needscan
= 0;
356 * If need to compact the leaf entries, do it now.
359 xfs_dir2_block_compact(tp
, bp
, hdr
, btp
, blp
, &needlog
,
360 &lfloghigh
, &lfloglow
);
361 else if (btp
->stale
) {
363 * Set leaf logging boundaries to impossible state.
364 * For the no-stale case they're set explicitly.
366 lfloglow
= be32_to_cpu(btp
->count
);
371 * Find the slot that's first lower than our hash value, -1 if none.
373 for (low
= 0, high
= be32_to_cpu(btp
->count
) - 1; low
<= high
; ) {
374 mid
= (low
+ high
) >> 1;
375 if ((hash
= be32_to_cpu(blp
[mid
].hashval
)) == args
->hashval
)
377 if (hash
< args
->hashval
)
382 while (mid
>= 0 && be32_to_cpu(blp
[mid
].hashval
) >= args
->hashval
) {
386 * No stale entries, will use enddup space to hold new leaf.
390 * Mark the space needed for the new leaf entry, now in use.
392 xfs_dir2_data_use_free(tp
, bp
, enddup
,
393 (xfs_dir2_data_aoff_t
)
394 ((char *)enddup
- (char *)hdr
+ be16_to_cpu(enddup
->length
) -
396 (xfs_dir2_data_aoff_t
)sizeof(*blp
),
397 &needlog
, &needscan
);
399 * Update the tail (entry count).
401 be32_add_cpu(&btp
->count
, 1);
403 * If we now need to rebuild the bestfree map, do so.
404 * This needs to happen before the next call to use_free.
407 xfs_dir2_data_freescan(mp
, hdr
, &needlog
);
411 * Adjust pointer to the first leaf entry, we're about to move
412 * the table up one to open up space for the new leaf entry.
413 * Then adjust our index to match.
418 memmove(blp
, &blp
[1], mid
* sizeof(*blp
));
423 * Use a stale leaf for our new entry.
428 blp
[lowstale
].address
!=
429 cpu_to_be32(XFS_DIR2_NULL_DATAPTR
);
432 for (highstale
= mid
+ 1;
433 highstale
< be32_to_cpu(btp
->count
) &&
434 blp
[highstale
].address
!=
435 cpu_to_be32(XFS_DIR2_NULL_DATAPTR
) &&
436 (lowstale
< 0 || mid
- lowstale
> highstale
- mid
);
440 * Move entries toward the low-numbered stale entry.
443 (highstale
== be32_to_cpu(btp
->count
) ||
444 mid
- lowstale
<= highstale
- mid
)) {
446 memmove(&blp
[lowstale
], &blp
[lowstale
+ 1],
447 (mid
- lowstale
) * sizeof(*blp
));
448 lfloglow
= MIN(lowstale
, lfloglow
);
449 lfloghigh
= MAX(mid
, lfloghigh
);
452 * Move entries toward the high-numbered stale entry.
455 ASSERT(highstale
< be32_to_cpu(btp
->count
));
458 memmove(&blp
[mid
+ 1], &blp
[mid
],
459 (highstale
- mid
) * sizeof(*blp
));
460 lfloglow
= MIN(mid
, lfloglow
);
461 lfloghigh
= MAX(highstale
, lfloghigh
);
463 be32_add_cpu(&btp
->stale
, -1);
466 * Point to the new data entry.
468 dep
= (xfs_dir2_data_entry_t
*)dup
;
470 * Fill in the leaf entry.
472 blp
[mid
].hashval
= cpu_to_be32(args
->hashval
);
473 blp
[mid
].address
= cpu_to_be32(xfs_dir2_byte_to_dataptr(mp
,
474 (char *)dep
- (char *)hdr
));
475 xfs_dir2_block_log_leaf(tp
, bp
, lfloglow
, lfloghigh
);
477 * Mark space for the data entry used.
479 xfs_dir2_data_use_free(tp
, bp
, dup
,
480 (xfs_dir2_data_aoff_t
)((char *)dup
- (char *)hdr
),
481 (xfs_dir2_data_aoff_t
)len
, &needlog
, &needscan
);
483 * Create the new data entry.
485 dep
->inumber
= cpu_to_be64(args
->inumber
);
486 dep
->namelen
= args
->namelen
;
487 memcpy(dep
->name
, args
->name
, args
->namelen
);
488 tagp
= xfs_dir2_data_entry_tag_p(dep
);
489 *tagp
= cpu_to_be16((char *)dep
- (char *)hdr
);
491 * Clean up the bestfree array and log the header, tail, and entry.
494 xfs_dir2_data_freescan(mp
, hdr
, &needlog
);
496 xfs_dir2_data_log_header(tp
, bp
);
497 xfs_dir2_block_log_tail(tp
, bp
);
498 xfs_dir2_data_log_entry(tp
, bp
, dep
);
499 xfs_dir2_data_check(dp
, bp
);
504 * Readdir for block directories.
507 xfs_dir2_block_getdents(
508 xfs_inode_t
*dp
, /* incore inode */
513 xfs_dir2_data_hdr_t
*hdr
; /* block header */
514 struct xfs_buf
*bp
; /* buffer for block */
515 xfs_dir2_block_tail_t
*btp
; /* block tail */
516 xfs_dir2_data_entry_t
*dep
; /* block data entry */
517 xfs_dir2_data_unused_t
*dup
; /* block unused entry */
518 char *endptr
; /* end of the data entries */
519 int error
; /* error return value */
520 xfs_mount_t
*mp
; /* filesystem mount point */
521 char *ptr
; /* current data entry */
522 int wantoff
; /* starting block offset */
527 * If the block number in the offset is out of range, we're done.
529 if (xfs_dir2_dataptr_to_db(mp
, *offset
) > mp
->m_dirdatablk
)
532 error
= xfs_dir2_block_read(NULL
, dp
, &bp
);
537 * Extract the byte offset we start at from the seek pointer.
538 * We'll skip entries before this.
540 wantoff
= xfs_dir2_dataptr_to_off(mp
, *offset
);
542 xfs_dir2_data_check(dp
, bp
);
544 * Set up values for the loop.
546 btp
= xfs_dir2_block_tail_p(mp
, hdr
);
547 ptr
= (char *)(hdr
+ 1);
548 endptr
= (char *)xfs_dir2_block_leaf_p(btp
);
551 * Loop over the data portion of the block.
552 * Each object is a real entry (dep) or an unused one (dup).
554 while (ptr
< endptr
) {
555 dup
= (xfs_dir2_data_unused_t
*)ptr
;
559 if (be16_to_cpu(dup
->freetag
) == XFS_DIR2_DATA_FREE_TAG
) {
560 ptr
+= be16_to_cpu(dup
->length
);
564 dep
= (xfs_dir2_data_entry_t
*)ptr
;
567 * Bump pointer for the next iteration.
569 ptr
+= xfs_dir2_data_entsize(dep
->namelen
);
571 * The entry is before the desired starting point, skip it.
573 if ((char *)dep
- (char *)hdr
< wantoff
)
576 cook
= xfs_dir2_db_off_to_dataptr(mp
, mp
->m_dirdatablk
,
577 (char *)dep
- (char *)hdr
);
580 * If it didn't fit, set the final offset to here & return.
582 if (filldir(dirent
, (char *)dep
->name
, dep
->namelen
,
583 cook
& 0x7fffffff, be64_to_cpu(dep
->inumber
),
585 *offset
= cook
& 0x7fffffff;
586 xfs_trans_brelse(NULL
, bp
);
592 * Reached the end of the block.
593 * Set the offset to a non-existent block 1 and return.
595 *offset
= xfs_dir2_db_off_to_dataptr(mp
, mp
->m_dirdatablk
+ 1, 0) &
597 xfs_trans_brelse(NULL
, bp
);
602 * Log leaf entries from the block.
605 xfs_dir2_block_log_leaf(
606 xfs_trans_t
*tp
, /* transaction structure */
607 struct xfs_buf
*bp
, /* block buffer */
608 int first
, /* index of first logged leaf */
609 int last
) /* index of last logged leaf */
611 xfs_dir2_data_hdr_t
*hdr
= bp
->b_addr
;
612 xfs_dir2_leaf_entry_t
*blp
;
613 xfs_dir2_block_tail_t
*btp
;
615 btp
= xfs_dir2_block_tail_p(tp
->t_mountp
, hdr
);
616 blp
= xfs_dir2_block_leaf_p(btp
);
617 xfs_trans_log_buf(tp
, bp
, (uint
)((char *)&blp
[first
] - (char *)hdr
),
618 (uint
)((char *)&blp
[last
+ 1] - (char *)hdr
- 1));
622 * Log the block tail.
625 xfs_dir2_block_log_tail(
626 xfs_trans_t
*tp
, /* transaction structure */
627 struct xfs_buf
*bp
) /* block buffer */
629 xfs_dir2_data_hdr_t
*hdr
= bp
->b_addr
;
630 xfs_dir2_block_tail_t
*btp
;
632 btp
= xfs_dir2_block_tail_p(tp
->t_mountp
, hdr
);
633 xfs_trans_log_buf(tp
, bp
, (uint
)((char *)btp
- (char *)hdr
),
634 (uint
)((char *)(btp
+ 1) - (char *)hdr
- 1));
638 * Look up an entry in the block. This is the external routine,
639 * xfs_dir2_block_lookup_int does the real work.
642 xfs_dir2_block_lookup(
643 xfs_da_args_t
*args
) /* dir lookup arguments */
645 xfs_dir2_data_hdr_t
*hdr
; /* block header */
646 xfs_dir2_leaf_entry_t
*blp
; /* block leaf entries */
647 struct xfs_buf
*bp
; /* block buffer */
648 xfs_dir2_block_tail_t
*btp
; /* block tail */
649 xfs_dir2_data_entry_t
*dep
; /* block data entry */
650 xfs_inode_t
*dp
; /* incore inode */
651 int ent
; /* entry index */
652 int error
; /* error return value */
653 xfs_mount_t
*mp
; /* filesystem mount point */
655 trace_xfs_dir2_block_lookup(args
);
658 * Get the buffer, look up the entry.
659 * If not found (ENOENT) then return, have no buffer.
661 if ((error
= xfs_dir2_block_lookup_int(args
, &bp
, &ent
)))
666 xfs_dir2_data_check(dp
, bp
);
667 btp
= xfs_dir2_block_tail_p(mp
, hdr
);
668 blp
= xfs_dir2_block_leaf_p(btp
);
670 * Get the offset from the leaf entry, to point to the data.
672 dep
= (xfs_dir2_data_entry_t
*)((char *)hdr
+
673 xfs_dir2_dataptr_to_off(mp
, be32_to_cpu(blp
[ent
].address
)));
675 * Fill in inode number, CI name if appropriate, release the block.
677 args
->inumber
= be64_to_cpu(dep
->inumber
);
678 error
= xfs_dir_cilookup_result(args
, dep
->name
, dep
->namelen
);
679 xfs_trans_brelse(args
->trans
, bp
);
680 return XFS_ERROR(error
);
684 * Internal block lookup routine.
686 static int /* error */
687 xfs_dir2_block_lookup_int(
688 xfs_da_args_t
*args
, /* dir lookup arguments */
689 struct xfs_buf
**bpp
, /* returned block buffer */
690 int *entno
) /* returned entry number */
692 xfs_dir2_dataptr_t addr
; /* data entry address */
693 xfs_dir2_data_hdr_t
*hdr
; /* block header */
694 xfs_dir2_leaf_entry_t
*blp
; /* block leaf entries */
695 struct xfs_buf
*bp
; /* block buffer */
696 xfs_dir2_block_tail_t
*btp
; /* block tail */
697 xfs_dir2_data_entry_t
*dep
; /* block data entry */
698 xfs_inode_t
*dp
; /* incore inode */
699 int error
; /* error return value */
700 xfs_dahash_t hash
; /* found hash value */
701 int high
; /* binary search high index */
702 int low
; /* binary search low index */
703 int mid
; /* binary search current idx */
704 xfs_mount_t
*mp
; /* filesystem mount point */
705 xfs_trans_t
*tp
; /* transaction pointer */
706 enum xfs_dacmp cmp
; /* comparison result */
712 error
= xfs_dir2_block_read(tp
, dp
, &bp
);
717 xfs_dir2_data_check(dp
, bp
);
718 btp
= xfs_dir2_block_tail_p(mp
, hdr
);
719 blp
= xfs_dir2_block_leaf_p(btp
);
721 * Loop doing a binary search for our hash value.
722 * Find our entry, ENOENT if it's not there.
724 for (low
= 0, high
= be32_to_cpu(btp
->count
) - 1; ; ) {
726 mid
= (low
+ high
) >> 1;
727 if ((hash
= be32_to_cpu(blp
[mid
].hashval
)) == args
->hashval
)
729 if (hash
< args
->hashval
)
734 ASSERT(args
->op_flags
& XFS_DA_OP_OKNOENT
);
735 xfs_trans_brelse(tp
, bp
);
736 return XFS_ERROR(ENOENT
);
740 * Back up to the first one with the right hash value.
742 while (mid
> 0 && be32_to_cpu(blp
[mid
- 1].hashval
) == args
->hashval
) {
746 * Now loop forward through all the entries with the
747 * right hash value looking for our name.
750 if ((addr
= be32_to_cpu(blp
[mid
].address
)) == XFS_DIR2_NULL_DATAPTR
)
753 * Get pointer to the entry from the leaf.
755 dep
= (xfs_dir2_data_entry_t
*)
756 ((char *)hdr
+ xfs_dir2_dataptr_to_off(mp
, addr
));
758 * Compare name and if it's an exact match, return the index
759 * and buffer. If it's the first case-insensitive match, store
760 * the index and buffer and continue looking for an exact match.
762 cmp
= mp
->m_dirnameops
->compname(args
, dep
->name
, dep
->namelen
);
763 if (cmp
!= XFS_CMP_DIFFERENT
&& cmp
!= args
->cmpresult
) {
764 args
->cmpresult
= cmp
;
767 if (cmp
== XFS_CMP_EXACT
)
770 } while (++mid
< be32_to_cpu(btp
->count
) &&
771 be32_to_cpu(blp
[mid
].hashval
) == hash
);
773 ASSERT(args
->op_flags
& XFS_DA_OP_OKNOENT
);
775 * Here, we can only be doing a lookup (not a rename or replace).
776 * If a case-insensitive match was found earlier, return success.
778 if (args
->cmpresult
== XFS_CMP_CASE
)
781 * No match, release the buffer and return ENOENT.
783 xfs_trans_brelse(tp
, bp
);
784 return XFS_ERROR(ENOENT
);
788 * Remove an entry from a block format directory.
789 * If that makes the block small enough to fit in shortform, transform it.
792 xfs_dir2_block_removename(
793 xfs_da_args_t
*args
) /* directory operation args */
795 xfs_dir2_data_hdr_t
*hdr
; /* block header */
796 xfs_dir2_leaf_entry_t
*blp
; /* block leaf pointer */
797 struct xfs_buf
*bp
; /* block buffer */
798 xfs_dir2_block_tail_t
*btp
; /* block tail */
799 xfs_dir2_data_entry_t
*dep
; /* block data entry */
800 xfs_inode_t
*dp
; /* incore inode */
801 int ent
; /* block leaf entry index */
802 int error
; /* error return value */
803 xfs_mount_t
*mp
; /* filesystem mount point */
804 int needlog
; /* need to log block header */
805 int needscan
; /* need to fixup bestfree */
806 xfs_dir2_sf_hdr_t sfh
; /* shortform header */
807 int size
; /* shortform size */
808 xfs_trans_t
*tp
; /* transaction pointer */
810 trace_xfs_dir2_block_removename(args
);
813 * Look up the entry in the block. Gets the buffer and entry index.
814 * It will always be there, the vnodeops level does a lookup first.
816 if ((error
= xfs_dir2_block_lookup_int(args
, &bp
, &ent
))) {
823 btp
= xfs_dir2_block_tail_p(mp
, hdr
);
824 blp
= xfs_dir2_block_leaf_p(btp
);
826 * Point to the data entry using the leaf entry.
828 dep
= (xfs_dir2_data_entry_t
*)
829 ((char *)hdr
+ xfs_dir2_dataptr_to_off(mp
, be32_to_cpu(blp
[ent
].address
)));
831 * Mark the data entry's space free.
833 needlog
= needscan
= 0;
834 xfs_dir2_data_make_free(tp
, bp
,
835 (xfs_dir2_data_aoff_t
)((char *)dep
- (char *)hdr
),
836 xfs_dir2_data_entsize(dep
->namelen
), &needlog
, &needscan
);
838 * Fix up the block tail.
840 be32_add_cpu(&btp
->stale
, 1);
841 xfs_dir2_block_log_tail(tp
, bp
);
843 * Remove the leaf entry by marking it stale.
845 blp
[ent
].address
= cpu_to_be32(XFS_DIR2_NULL_DATAPTR
);
846 xfs_dir2_block_log_leaf(tp
, bp
, ent
, ent
);
848 * Fix up bestfree, log the header if necessary.
851 xfs_dir2_data_freescan(mp
, hdr
, &needlog
);
853 xfs_dir2_data_log_header(tp
, bp
);
854 xfs_dir2_data_check(dp
, bp
);
856 * See if the size as a shortform is good enough.
858 size
= xfs_dir2_block_sfsize(dp
, hdr
, &sfh
);
859 if (size
> XFS_IFORK_DSIZE(dp
))
863 * If it works, do the conversion.
865 return xfs_dir2_block_to_sf(args
, bp
, size
, &sfh
);
869 * Replace an entry in a V2 block directory.
870 * Change the inode number to the new value.
873 xfs_dir2_block_replace(
874 xfs_da_args_t
*args
) /* directory operation args */
876 xfs_dir2_data_hdr_t
*hdr
; /* block header */
877 xfs_dir2_leaf_entry_t
*blp
; /* block leaf entries */
878 struct xfs_buf
*bp
; /* block buffer */
879 xfs_dir2_block_tail_t
*btp
; /* block tail */
880 xfs_dir2_data_entry_t
*dep
; /* block data entry */
881 xfs_inode_t
*dp
; /* incore inode */
882 int ent
; /* leaf entry index */
883 int error
; /* error return value */
884 xfs_mount_t
*mp
; /* filesystem mount point */
886 trace_xfs_dir2_block_replace(args
);
889 * Lookup the entry in the directory. Get buffer and entry index.
890 * This will always succeed since the caller has already done a lookup.
892 if ((error
= xfs_dir2_block_lookup_int(args
, &bp
, &ent
))) {
898 btp
= xfs_dir2_block_tail_p(mp
, hdr
);
899 blp
= xfs_dir2_block_leaf_p(btp
);
901 * Point to the data entry we need to change.
903 dep
= (xfs_dir2_data_entry_t
*)
904 ((char *)hdr
+ xfs_dir2_dataptr_to_off(mp
, be32_to_cpu(blp
[ent
].address
)));
905 ASSERT(be64_to_cpu(dep
->inumber
) != args
->inumber
);
907 * Change the inode number to the new value.
909 dep
->inumber
= cpu_to_be64(args
->inumber
);
910 xfs_dir2_data_log_entry(args
->trans
, bp
, dep
);
911 xfs_dir2_data_check(dp
, bp
);
916 * Qsort comparison routine for the block leaf entries.
918 static int /* sort order */
920 const void *a
, /* first leaf entry */
921 const void *b
) /* second leaf entry */
923 const xfs_dir2_leaf_entry_t
*la
; /* first leaf entry */
924 const xfs_dir2_leaf_entry_t
*lb
; /* second leaf entry */
928 return be32_to_cpu(la
->hashval
) < be32_to_cpu(lb
->hashval
) ? -1 :
929 (be32_to_cpu(la
->hashval
) > be32_to_cpu(lb
->hashval
) ? 1 : 0);
933 * Convert a V2 leaf directory to a V2 block directory if possible.
936 xfs_dir2_leaf_to_block(
937 xfs_da_args_t
*args
, /* operation arguments */
938 struct xfs_buf
*lbp
, /* leaf buffer */
939 struct xfs_buf
*dbp
) /* data buffer */
941 __be16
*bestsp
; /* leaf bests table */
942 xfs_dir2_data_hdr_t
*hdr
; /* block header */
943 xfs_dir2_block_tail_t
*btp
; /* block tail */
944 xfs_inode_t
*dp
; /* incore directory inode */
945 xfs_dir2_data_unused_t
*dup
; /* unused data entry */
946 int error
; /* error return value */
947 int from
; /* leaf from index */
948 xfs_dir2_leaf_t
*leaf
; /* leaf structure */
949 xfs_dir2_leaf_entry_t
*lep
; /* leaf entry */
950 xfs_dir2_leaf_tail_t
*ltp
; /* leaf tail structure */
951 xfs_mount_t
*mp
; /* file system mount point */
952 int needlog
; /* need to log data header */
953 int needscan
; /* need to scan for bestfree */
954 xfs_dir2_sf_hdr_t sfh
; /* shortform header */
955 int size
; /* bytes used */
956 __be16
*tagp
; /* end of entry (tag) */
957 int to
; /* block/leaf to index */
958 xfs_trans_t
*tp
; /* transaction pointer */
960 trace_xfs_dir2_leaf_to_block(args
);
966 ASSERT(leaf
->hdr
.info
.magic
== cpu_to_be16(XFS_DIR2_LEAF1_MAGIC
));
967 ltp
= xfs_dir2_leaf_tail_p(mp
, leaf
);
969 * If there are data blocks other than the first one, take this
970 * opportunity to remove trailing empty data blocks that may have
971 * been left behind during no-space-reservation operations.
972 * These will show up in the leaf bests table.
974 while (dp
->i_d
.di_size
> mp
->m_dirblksize
) {
975 bestsp
= xfs_dir2_leaf_bests_p(ltp
);
976 if (be16_to_cpu(bestsp
[be32_to_cpu(ltp
->bestcount
) - 1]) ==
977 mp
->m_dirblksize
- (uint
)sizeof(*hdr
)) {
979 xfs_dir2_leaf_trim_data(args
, lbp
,
980 (xfs_dir2_db_t
)(be32_to_cpu(ltp
->bestcount
) - 1))))
986 * Read the data block if we don't already have it, give up if it fails.
989 error
= xfs_dir2_data_read(tp
, dp
, mp
->m_dirdatablk
, -1, &dbp
);
994 ASSERT(hdr
->magic
== cpu_to_be32(XFS_DIR2_DATA_MAGIC
));
996 * Size of the "leaf" area in the block.
998 size
= (uint
)sizeof(xfs_dir2_block_tail_t
) +
999 (uint
)sizeof(*lep
) * (be16_to_cpu(leaf
->hdr
.count
) - be16_to_cpu(leaf
->hdr
.stale
));
1001 * Look at the last data entry.
1003 tagp
= (__be16
*)((char *)hdr
+ mp
->m_dirblksize
) - 1;
1004 dup
= (xfs_dir2_data_unused_t
*)((char *)hdr
+ be16_to_cpu(*tagp
));
1006 * If it's not free or is too short we can't do it.
1008 if (be16_to_cpu(dup
->freetag
) != XFS_DIR2_DATA_FREE_TAG
||
1009 be16_to_cpu(dup
->length
) < size
)
1013 * Start converting it to block form.
1015 dbp
->b_ops
= &xfs_dir2_block_buf_ops
;
1016 hdr
->magic
= cpu_to_be32(XFS_DIR2_BLOCK_MAGIC
);
1020 * Use up the space at the end of the block (blp/btp).
1022 xfs_dir2_data_use_free(tp
, dbp
, dup
, mp
->m_dirblksize
- size
, size
,
1023 &needlog
, &needscan
);
1025 * Initialize the block tail.
1027 btp
= xfs_dir2_block_tail_p(mp
, hdr
);
1028 btp
->count
= cpu_to_be32(be16_to_cpu(leaf
->hdr
.count
) - be16_to_cpu(leaf
->hdr
.stale
));
1030 xfs_dir2_block_log_tail(tp
, dbp
);
1032 * Initialize the block leaf area. We compact out stale entries.
1034 lep
= xfs_dir2_block_leaf_p(btp
);
1035 for (from
= to
= 0; from
< be16_to_cpu(leaf
->hdr
.count
); from
++) {
1036 if (leaf
->ents
[from
].address
==
1037 cpu_to_be32(XFS_DIR2_NULL_DATAPTR
))
1039 lep
[to
++] = leaf
->ents
[from
];
1041 ASSERT(to
== be32_to_cpu(btp
->count
));
1042 xfs_dir2_block_log_leaf(tp
, dbp
, 0, be32_to_cpu(btp
->count
) - 1);
1044 * Scan the bestfree if we need it and log the data block header.
1047 xfs_dir2_data_freescan(mp
, hdr
, &needlog
);
1049 xfs_dir2_data_log_header(tp
, dbp
);
1051 * Pitch the old leaf block.
1053 error
= xfs_da_shrink_inode(args
, mp
->m_dirleafblk
, lbp
);
1058 * Now see if the resulting block can be shrunken to shortform.
1060 size
= xfs_dir2_block_sfsize(dp
, hdr
, &sfh
);
1061 if (size
> XFS_IFORK_DSIZE(dp
))
1064 return xfs_dir2_block_to_sf(args
, dbp
, size
, &sfh
);
1068 * Convert the shortform directory to block form.
1071 xfs_dir2_sf_to_block(
1072 xfs_da_args_t
*args
) /* operation arguments */
1074 xfs_dir2_db_t blkno
; /* dir-relative block # (0) */
1075 xfs_dir2_data_hdr_t
*hdr
; /* block header */
1076 xfs_dir2_leaf_entry_t
*blp
; /* block leaf entries */
1077 struct xfs_buf
*bp
; /* block buffer */
1078 xfs_dir2_block_tail_t
*btp
; /* block tail pointer */
1079 xfs_dir2_data_entry_t
*dep
; /* data entry pointer */
1080 xfs_inode_t
*dp
; /* incore directory inode */
1081 int dummy
; /* trash */
1082 xfs_dir2_data_unused_t
*dup
; /* unused entry pointer */
1083 int endoffset
; /* end of data objects */
1084 int error
; /* error return value */
1086 xfs_mount_t
*mp
; /* filesystem mount point */
1087 int needlog
; /* need to log block header */
1088 int needscan
; /* need to scan block freespc */
1089 int newoffset
; /* offset from current entry */
1090 int offset
; /* target block offset */
1091 xfs_dir2_sf_entry_t
*sfep
; /* sf entry pointer */
1092 xfs_dir2_sf_hdr_t
*oldsfp
; /* old shortform header */
1093 xfs_dir2_sf_hdr_t
*sfp
; /* shortform header */
1094 __be16
*tagp
; /* end of data entry */
1095 xfs_trans_t
*tp
; /* transaction pointer */
1096 struct xfs_name name
;
1098 trace_xfs_dir2_sf_to_block(args
);
1103 ASSERT(dp
->i_df
.if_flags
& XFS_IFINLINE
);
1105 * Bomb out if the shortform directory is way too short.
1107 if (dp
->i_d
.di_size
< offsetof(xfs_dir2_sf_hdr_t
, parent
)) {
1108 ASSERT(XFS_FORCED_SHUTDOWN(mp
));
1109 return XFS_ERROR(EIO
);
1112 oldsfp
= (xfs_dir2_sf_hdr_t
*)dp
->i_df
.if_u1
.if_data
;
1114 ASSERT(dp
->i_df
.if_bytes
== dp
->i_d
.di_size
);
1115 ASSERT(dp
->i_df
.if_u1
.if_data
!= NULL
);
1116 ASSERT(dp
->i_d
.di_size
>= xfs_dir2_sf_hdr_size(oldsfp
->i8count
));
1119 * Copy the directory into a temporary buffer.
1120 * Then pitch the incore inode data so we can make extents.
1122 sfp
= kmem_alloc(dp
->i_df
.if_bytes
, KM_SLEEP
);
1123 memcpy(sfp
, oldsfp
, dp
->i_df
.if_bytes
);
1125 xfs_idata_realloc(dp
, -dp
->i_df
.if_bytes
, XFS_DATA_FORK
);
1126 dp
->i_d
.di_size
= 0;
1127 xfs_trans_log_inode(tp
, dp
, XFS_ILOG_CORE
);
1130 * Add block 0 to the inode.
1132 error
= xfs_dir2_grow_inode(args
, XFS_DIR2_DATA_SPACE
, &blkno
);
1138 * Initialize the data block.
1140 error
= xfs_dir2_data_init(args
, blkno
, &bp
);
1145 bp
->b_ops
= &xfs_dir2_block_buf_ops
;
1147 hdr
->magic
= cpu_to_be32(XFS_DIR2_BLOCK_MAGIC
);
1149 * Compute size of block "tail" area.
1151 i
= (uint
)sizeof(*btp
) +
1152 (sfp
->count
+ 2) * (uint
)sizeof(xfs_dir2_leaf_entry_t
);
1154 * The whole thing is initialized to free by the init routine.
1155 * Say we're using the leaf and tail area.
1157 dup
= (xfs_dir2_data_unused_t
*)(hdr
+ 1);
1158 needlog
= needscan
= 0;
1159 xfs_dir2_data_use_free(tp
, bp
, dup
, mp
->m_dirblksize
- i
, i
, &needlog
,
1161 ASSERT(needscan
== 0);
1165 btp
= xfs_dir2_block_tail_p(mp
, hdr
);
1166 btp
->count
= cpu_to_be32(sfp
->count
+ 2); /* ., .. */
1168 blp
= xfs_dir2_block_leaf_p(btp
);
1169 endoffset
= (uint
)((char *)blp
- (char *)hdr
);
1171 * Remove the freespace, we'll manage it.
1173 xfs_dir2_data_use_free(tp
, bp
, dup
,
1174 (xfs_dir2_data_aoff_t
)((char *)dup
- (char *)hdr
),
1175 be16_to_cpu(dup
->length
), &needlog
, &needscan
);
1177 * Create entry for .
1179 dep
= (xfs_dir2_data_entry_t
*)
1180 ((char *)hdr
+ XFS_DIR2_DATA_DOT_OFFSET
);
1181 dep
->inumber
= cpu_to_be64(dp
->i_ino
);
1184 tagp
= xfs_dir2_data_entry_tag_p(dep
);
1185 *tagp
= cpu_to_be16((char *)dep
- (char *)hdr
);
1186 xfs_dir2_data_log_entry(tp
, bp
, dep
);
1187 blp
[0].hashval
= cpu_to_be32(xfs_dir_hash_dot
);
1188 blp
[0].address
= cpu_to_be32(xfs_dir2_byte_to_dataptr(mp
,
1189 (char *)dep
- (char *)hdr
));
1191 * Create entry for ..
1193 dep
= (xfs_dir2_data_entry_t
*)
1194 ((char *)hdr
+ XFS_DIR2_DATA_DOTDOT_OFFSET
);
1195 dep
->inumber
= cpu_to_be64(xfs_dir2_sf_get_parent_ino(sfp
));
1197 dep
->name
[0] = dep
->name
[1] = '.';
1198 tagp
= xfs_dir2_data_entry_tag_p(dep
);
1199 *tagp
= cpu_to_be16((char *)dep
- (char *)hdr
);
1200 xfs_dir2_data_log_entry(tp
, bp
, dep
);
1201 blp
[1].hashval
= cpu_to_be32(xfs_dir_hash_dotdot
);
1202 blp
[1].address
= cpu_to_be32(xfs_dir2_byte_to_dataptr(mp
,
1203 (char *)dep
- (char *)hdr
));
1204 offset
= XFS_DIR2_DATA_FIRST_OFFSET
;
1206 * Loop over existing entries, stuff them in.
1212 sfep
= xfs_dir2_sf_firstentry(sfp
);
1214 * Need to preserve the existing offset values in the sf directory.
1215 * Insert holes (unused entries) where necessary.
1217 while (offset
< endoffset
) {
1219 * sfep is null when we reach the end of the list.
1222 newoffset
= endoffset
;
1224 newoffset
= xfs_dir2_sf_get_offset(sfep
);
1226 * There should be a hole here, make one.
1228 if (offset
< newoffset
) {
1229 dup
= (xfs_dir2_data_unused_t
*)((char *)hdr
+ offset
);
1230 dup
->freetag
= cpu_to_be16(XFS_DIR2_DATA_FREE_TAG
);
1231 dup
->length
= cpu_to_be16(newoffset
- offset
);
1232 *xfs_dir2_data_unused_tag_p(dup
) = cpu_to_be16(
1233 ((char *)dup
- (char *)hdr
));
1234 xfs_dir2_data_log_unused(tp
, bp
, dup
);
1235 xfs_dir2_data_freeinsert(hdr
, dup
, &dummy
);
1236 offset
+= be16_to_cpu(dup
->length
);
1240 * Copy a real entry.
1242 dep
= (xfs_dir2_data_entry_t
*)((char *)hdr
+ newoffset
);
1243 dep
->inumber
= cpu_to_be64(xfs_dir2_sfe_get_ino(sfp
, sfep
));
1244 dep
->namelen
= sfep
->namelen
;
1245 memcpy(dep
->name
, sfep
->name
, dep
->namelen
);
1246 tagp
= xfs_dir2_data_entry_tag_p(dep
);
1247 *tagp
= cpu_to_be16((char *)dep
- (char *)hdr
);
1248 xfs_dir2_data_log_entry(tp
, bp
, dep
);
1249 name
.name
= sfep
->name
;
1250 name
.len
= sfep
->namelen
;
1251 blp
[2 + i
].hashval
= cpu_to_be32(mp
->m_dirnameops
->
1253 blp
[2 + i
].address
= cpu_to_be32(xfs_dir2_byte_to_dataptr(mp
,
1254 (char *)dep
- (char *)hdr
));
1255 offset
= (int)((char *)(tagp
+ 1) - (char *)hdr
);
1256 if (++i
== sfp
->count
)
1259 sfep
= xfs_dir2_sf_nextentry(sfp
, sfep
);
1261 /* Done with the temporary buffer */
1264 * Sort the leaf entries by hash value.
1266 xfs_sort(blp
, be32_to_cpu(btp
->count
), sizeof(*blp
), xfs_dir2_block_sort
);
1268 * Log the leaf entry area and tail.
1269 * Already logged the header in data_init, ignore needlog.
1271 ASSERT(needscan
== 0);
1272 xfs_dir2_block_log_leaf(tp
, bp
, 0, be32_to_cpu(btp
->count
) - 1);
1273 xfs_dir2_block_log_tail(tp
, bp
);
1274 xfs_dir2_data_check(dp
, bp
);