1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2000-2003,2005 Silicon Graphics, Inc.
4 * Copyright (c) 2013 Red Hat, Inc.
7 #include "libxfs_priv.h"
9 #include "xfs_shared.h"
10 #include "xfs_format.h"
11 #include "xfs_log_format.h"
12 #include "xfs_trans_resv.h"
13 #include "xfs_mount.h"
14 #include "xfs_inode.h"
15 #include "xfs_trans.h"
18 #include "xfs_dir2_priv.h"
19 #include "xfs_trace.h"
22 * Local function prototypes.
24 static void xfs_dir2_block_log_leaf(xfs_trans_t
*tp
, struct xfs_buf
*bp
,
26 static void xfs_dir2_block_log_tail(xfs_trans_t
*tp
, struct xfs_buf
*bp
);
27 static int xfs_dir2_block_lookup_int(xfs_da_args_t
*args
, struct xfs_buf
**bpp
,
29 static int xfs_dir2_block_sort(const void *a
, const void *b
);
31 static xfs_dahash_t xfs_dir_hash_dot
, xfs_dir_hash_dotdot
;
34 * One-time startup routine called from xfs_init().
39 xfs_dir_hash_dot
= xfs_da_hashname((unsigned char *)".", 1);
40 xfs_dir_hash_dotdot
= xfs_da_hashname((unsigned char *)"..", 2);
44 xfs_dir3_block_verify(
47 struct xfs_mount
*mp
= bp
->b_mount
;
48 struct xfs_dir3_blk_hdr
*hdr3
= bp
->b_addr
;
50 if (!xfs_verify_magic(bp
, hdr3
->magic
))
51 return __this_address
;
53 if (xfs_has_crc(mp
)) {
54 if (!uuid_equal(&hdr3
->uuid
, &mp
->m_sb
.sb_meta_uuid
))
55 return __this_address
;
56 if (be64_to_cpu(hdr3
->blkno
) != xfs_buf_daddr(bp
))
57 return __this_address
;
58 if (!xfs_log_check_lsn(mp
, be64_to_cpu(hdr3
->lsn
)))
59 return __this_address
;
61 return __xfs_dir3_data_check(NULL
, bp
);
65 xfs_dir3_block_read_verify(
68 struct xfs_mount
*mp
= bp
->b_mount
;
71 if (xfs_has_crc(mp
) &&
72 !xfs_buf_verify_cksum(bp
, XFS_DIR3_DATA_CRC_OFF
))
73 xfs_verifier_error(bp
, -EFSBADCRC
, __this_address
);
75 fa
= xfs_dir3_block_verify(bp
);
77 xfs_verifier_error(bp
, -EFSCORRUPTED
, fa
);
82 xfs_dir3_block_write_verify(
85 struct xfs_mount
*mp
= bp
->b_mount
;
86 struct xfs_buf_log_item
*bip
= bp
->b_log_item
;
87 struct xfs_dir3_blk_hdr
*hdr3
= bp
->b_addr
;
90 fa
= xfs_dir3_block_verify(bp
);
92 xfs_verifier_error(bp
, -EFSCORRUPTED
, fa
);
100 hdr3
->lsn
= cpu_to_be64(bip
->bli_item
.li_lsn
);
102 xfs_buf_update_cksum(bp
, XFS_DIR3_DATA_CRC_OFF
);
105 const struct xfs_buf_ops xfs_dir3_block_buf_ops
= {
106 .name
= "xfs_dir3_block",
107 .magic
= { cpu_to_be32(XFS_DIR2_BLOCK_MAGIC
),
108 cpu_to_be32(XFS_DIR3_BLOCK_MAGIC
) },
109 .verify_read
= xfs_dir3_block_read_verify
,
110 .verify_write
= xfs_dir3_block_write_verify
,
111 .verify_struct
= xfs_dir3_block_verify
,
114 static xfs_failaddr_t
115 xfs_dir3_block_header_check(
116 struct xfs_inode
*dp
,
119 struct xfs_mount
*mp
= dp
->i_mount
;
121 if (xfs_has_crc(mp
)) {
122 struct xfs_dir3_blk_hdr
*hdr3
= bp
->b_addr
;
124 if (be64_to_cpu(hdr3
->owner
) != dp
->i_ino
)
125 return __this_address
;
133 struct xfs_trans
*tp
,
134 struct xfs_inode
*dp
,
135 struct xfs_buf
**bpp
)
137 struct xfs_mount
*mp
= dp
->i_mount
;
141 err
= xfs_da_read_buf(tp
, dp
, mp
->m_dir_geo
->datablk
, 0, bpp
,
142 XFS_DATA_FORK
, &xfs_dir3_block_buf_ops
);
146 /* Check things that we can't do in the verifier. */
147 fa
= xfs_dir3_block_header_check(dp
, *bpp
);
149 __xfs_buf_mark_corrupt(*bpp
, fa
);
150 xfs_trans_brelse(tp
, *bpp
);
152 return -EFSCORRUPTED
;
155 xfs_trans_buf_set_type(tp
, *bpp
, XFS_BLFT_DIR_BLOCK_BUF
);
161 struct xfs_mount
*mp
,
162 struct xfs_trans
*tp
,
164 struct xfs_inode
*dp
)
166 struct xfs_dir3_blk_hdr
*hdr3
= bp
->b_addr
;
168 bp
->b_ops
= &xfs_dir3_block_buf_ops
;
169 xfs_trans_buf_set_type(tp
, bp
, XFS_BLFT_DIR_BLOCK_BUF
);
171 if (xfs_has_crc(mp
)) {
172 memset(hdr3
, 0, sizeof(*hdr3
));
173 hdr3
->magic
= cpu_to_be32(XFS_DIR3_BLOCK_MAGIC
);
174 hdr3
->blkno
= cpu_to_be64(xfs_buf_daddr(bp
));
175 hdr3
->owner
= cpu_to_be64(dp
->i_ino
);
176 uuid_copy(&hdr3
->uuid
, &mp
->m_sb
.sb_meta_uuid
);
180 hdr3
->magic
= cpu_to_be32(XFS_DIR2_BLOCK_MAGIC
);
184 xfs_dir2_block_need_space(
185 struct xfs_inode
*dp
,
186 struct xfs_dir2_data_hdr
*hdr
,
187 struct xfs_dir2_block_tail
*btp
,
188 struct xfs_dir2_leaf_entry
*blp
,
190 struct xfs_dir2_data_unused
**dupp
,
191 struct xfs_dir2_data_unused
**enddupp
,
195 struct xfs_dir2_data_free
*bf
;
197 struct xfs_dir2_data_unused
*dup
= NULL
;
198 struct xfs_dir2_data_unused
*enddup
= NULL
;
201 bf
= xfs_dir2_data_bestfree_p(dp
->i_mount
, hdr
);
204 * If there are stale entries we'll use one for the leaf.
207 if (be16_to_cpu(bf
[0].length
) >= len
) {
209 * The biggest entry enough to avoid compaction.
211 dup
= (xfs_dir2_data_unused_t
*)
212 ((char *)hdr
+ be16_to_cpu(bf
[0].offset
));
217 * Will need to compact to make this work.
218 * Tag just before the first leaf entry.
221 tagp
= (__be16
*)blp
- 1;
223 /* Data object just before the first leaf entry. */
224 dup
= (xfs_dir2_data_unused_t
*)((char *)hdr
+ be16_to_cpu(*tagp
));
227 * If it's not free then the data will go where the
228 * leaf data starts now, if it works at all.
230 if (be16_to_cpu(dup
->freetag
) == XFS_DIR2_DATA_FREE_TAG
) {
231 if (be16_to_cpu(dup
->length
) + (be32_to_cpu(btp
->stale
) - 1) *
232 (uint
)sizeof(*blp
) < len
)
234 } else if ((be32_to_cpu(btp
->stale
) - 1) * (uint
)sizeof(*blp
) < len
)
237 dup
= (xfs_dir2_data_unused_t
*)blp
;
242 * no stale entries, so just use free space.
243 * Tag just before the first leaf entry.
245 tagp
= (__be16
*)blp
- 1;
247 /* Data object just before the first leaf entry. */
248 enddup
= (xfs_dir2_data_unused_t
*)((char *)hdr
+ be16_to_cpu(*tagp
));
251 * If it's not free then can't do this add without cleaning up:
252 * the space before the first leaf entry needs to be free so it
253 * can be expanded to hold the pointer to the new entry.
255 if (be16_to_cpu(enddup
->freetag
) == XFS_DIR2_DATA_FREE_TAG
) {
257 * Check out the biggest freespace and see if it's the same one.
259 dup
= (xfs_dir2_data_unused_t
*)
260 ((char *)hdr
+ be16_to_cpu(bf
[0].offset
));
263 * Not the same free entry, just check its length.
265 if (be16_to_cpu(dup
->length
) < len
)
271 * It is the biggest freespace, can it hold the leaf too?
273 if (be16_to_cpu(dup
->length
) < len
+ (uint
)sizeof(*blp
)) {
275 * Yes, use the second-largest entry instead if it works.
277 if (be16_to_cpu(bf
[1].length
) >= len
)
278 dup
= (xfs_dir2_data_unused_t
*)
279 ((char *)hdr
+ be16_to_cpu(bf
[1].offset
));
291 * compact the leaf entries.
292 * Leave the highest-numbered stale entry stale.
293 * XXX should be the one closest to mid but mid is not yet computed.
296 xfs_dir2_block_compact(
297 struct xfs_da_args
*args
,
299 struct xfs_dir2_data_hdr
*hdr
,
300 struct xfs_dir2_block_tail
*btp
,
301 struct xfs_dir2_leaf_entry
*blp
,
306 int fromidx
; /* source leaf index */
307 int toidx
; /* target leaf index */
309 int highstale
; /* high stale index */
311 fromidx
= toidx
= be32_to_cpu(btp
->count
) - 1;
312 highstale
= *lfloghigh
= -1;
313 for (; fromidx
>= 0; fromidx
--) {
314 if (blp
[fromidx
].address
== cpu_to_be32(XFS_DIR2_NULL_DATAPTR
)) {
318 if (*lfloghigh
== -1)
324 blp
[toidx
] = blp
[fromidx
];
327 *lfloglow
= toidx
+ 1 - (be32_to_cpu(btp
->stale
) - 1);
328 *lfloghigh
-= be32_to_cpu(btp
->stale
) - 1;
329 be32_add_cpu(&btp
->count
, -(be32_to_cpu(btp
->stale
) - 1));
330 xfs_dir2_data_make_free(args
, bp
,
331 (xfs_dir2_data_aoff_t
)((char *)blp
- (char *)hdr
),
332 (xfs_dir2_data_aoff_t
)((be32_to_cpu(btp
->stale
) - 1) * sizeof(*blp
)),
334 btp
->stale
= cpu_to_be32(1);
336 * If we now need to rebuild the bestfree map, do so.
337 * This needs to happen before the next call to use_free.
340 xfs_dir2_data_freescan(args
->dp
->i_mount
, hdr
, needlog
);
344 * Add an entry to a block directory.
347 xfs_dir2_block_addname(
348 xfs_da_args_t
*args
) /* directory op arguments */
350 xfs_dir2_data_hdr_t
*hdr
; /* block header */
351 xfs_dir2_leaf_entry_t
*blp
; /* block leaf entries */
352 struct xfs_buf
*bp
; /* buffer for block */
353 xfs_dir2_block_tail_t
*btp
; /* block tail */
354 int compact
; /* need to compact leaf ents */
355 xfs_dir2_data_entry_t
*dep
; /* block data entry */
356 xfs_inode_t
*dp
; /* directory inode */
357 xfs_dir2_data_unused_t
*dup
; /* block unused entry */
358 int error
; /* error return value */
359 xfs_dir2_data_unused_t
*enddup
=NULL
; /* unused at end of data */
360 xfs_dahash_t hash
; /* hash value of found entry */
361 int high
; /* high index for binary srch */
362 int highstale
; /* high stale index */
363 int lfloghigh
=0; /* last final leaf to log */
364 int lfloglow
=0; /* first final leaf to log */
365 int len
; /* length of the new entry */
366 int low
; /* low index for binary srch */
367 int lowstale
; /* low stale index */
368 int mid
=0; /* midpoint for binary srch */
369 int needlog
; /* need to log header */
370 int needscan
; /* need to rescan freespace */
371 __be16
*tagp
; /* pointer to tag value */
372 xfs_trans_t
*tp
; /* transaction structure */
374 trace_xfs_dir2_block_addname(args
);
379 /* Read the (one and only) directory block into bp. */
380 error
= xfs_dir3_block_read(tp
, dp
, &bp
);
384 len
= xfs_dir2_data_entsize(dp
->i_mount
, args
->namelen
);
387 * Set up pointers to parts of the block.
390 btp
= xfs_dir2_block_tail_p(args
->geo
, hdr
);
391 blp
= xfs_dir2_block_leaf_p(btp
);
394 * Find out if we can reuse stale entries or whether we need extra
395 * space for entry and new leaf.
397 xfs_dir2_block_need_space(dp
, hdr
, btp
, blp
, &tagp
, &dup
,
398 &enddup
, &compact
, len
);
401 * Done everything we need for a space check now.
403 if (args
->op_flags
& XFS_DA_OP_JUSTCHECK
) {
404 xfs_trans_brelse(tp
, bp
);
411 * If we don't have space for the new entry & leaf ...
414 /* Don't have a space reservation: return no-space. */
415 if (args
->total
== 0)
418 * Convert to the next larger format.
419 * Then add the new entry in that format.
421 error
= xfs_dir2_block_to_leaf(args
, bp
);
424 return xfs_dir2_leaf_addname(args
);
427 needlog
= needscan
= 0;
430 * If need to compact the leaf entries, do it now.
433 xfs_dir2_block_compact(args
, bp
, hdr
, btp
, blp
, &needlog
,
434 &lfloghigh
, &lfloglow
);
435 /* recalculate blp post-compaction */
436 blp
= xfs_dir2_block_leaf_p(btp
);
437 } else if (btp
->stale
) {
439 * Set leaf logging boundaries to impossible state.
440 * For the no-stale case they're set explicitly.
442 lfloglow
= be32_to_cpu(btp
->count
);
447 * Find the slot that's first lower than our hash value, -1 if none.
449 for (low
= 0, high
= be32_to_cpu(btp
->count
) - 1; low
<= high
; ) {
450 mid
= (low
+ high
) >> 1;
451 if ((hash
= be32_to_cpu(blp
[mid
].hashval
)) == args
->hashval
)
453 if (hash
< args
->hashval
)
458 while (mid
>= 0 && be32_to_cpu(blp
[mid
].hashval
) >= args
->hashval
) {
462 * No stale entries, will use enddup space to hold new leaf.
465 xfs_dir2_data_aoff_t aoff
;
468 * Mark the space needed for the new leaf entry, now in use.
470 aoff
= (xfs_dir2_data_aoff_t
)((char *)enddup
- (char *)hdr
+
471 be16_to_cpu(enddup
->length
) - sizeof(*blp
));
472 error
= xfs_dir2_data_use_free(args
, bp
, enddup
, aoff
,
473 (xfs_dir2_data_aoff_t
)sizeof(*blp
), &needlog
,
479 * Update the tail (entry count).
481 be32_add_cpu(&btp
->count
, 1);
483 * If we now need to rebuild the bestfree map, do so.
484 * This needs to happen before the next call to use_free.
487 xfs_dir2_data_freescan(dp
->i_mount
, hdr
, &needlog
);
491 * Adjust pointer to the first leaf entry, we're about to move
492 * the table up one to open up space for the new leaf entry.
493 * Then adjust our index to match.
498 memmove(blp
, &blp
[1], mid
* sizeof(*blp
));
503 * Use a stale leaf for our new entry.
508 blp
[lowstale
].address
!=
509 cpu_to_be32(XFS_DIR2_NULL_DATAPTR
);
512 for (highstale
= mid
+ 1;
513 highstale
< be32_to_cpu(btp
->count
) &&
514 blp
[highstale
].address
!=
515 cpu_to_be32(XFS_DIR2_NULL_DATAPTR
) &&
516 (lowstale
< 0 || mid
- lowstale
> highstale
- mid
);
520 * Move entries toward the low-numbered stale entry.
523 (highstale
== be32_to_cpu(btp
->count
) ||
524 mid
- lowstale
<= highstale
- mid
)) {
526 memmove(&blp
[lowstale
], &blp
[lowstale
+ 1],
527 (mid
- lowstale
) * sizeof(*blp
));
528 lfloglow
= min(lowstale
, lfloglow
);
529 lfloghigh
= max(mid
, lfloghigh
);
532 * Move entries toward the high-numbered stale entry.
535 ASSERT(highstale
< be32_to_cpu(btp
->count
));
538 memmove(&blp
[mid
+ 1], &blp
[mid
],
539 (highstale
- mid
) * sizeof(*blp
));
540 lfloglow
= min(mid
, lfloglow
);
541 lfloghigh
= max(highstale
, lfloghigh
);
543 be32_add_cpu(&btp
->stale
, -1);
546 * Point to the new data entry.
548 dep
= (xfs_dir2_data_entry_t
*)dup
;
550 * Fill in the leaf entry.
552 blp
[mid
].hashval
= cpu_to_be32(args
->hashval
);
553 blp
[mid
].address
= cpu_to_be32(xfs_dir2_byte_to_dataptr(
554 (char *)dep
- (char *)hdr
));
555 xfs_dir2_block_log_leaf(tp
, bp
, lfloglow
, lfloghigh
);
557 * Mark space for the data entry used.
559 error
= xfs_dir2_data_use_free(args
, bp
, dup
,
560 (xfs_dir2_data_aoff_t
)((char *)dup
- (char *)hdr
),
561 (xfs_dir2_data_aoff_t
)len
, &needlog
, &needscan
);
565 * Create the new data entry.
567 dep
->inumber
= cpu_to_be64(args
->inumber
);
568 dep
->namelen
= args
->namelen
;
569 memcpy(dep
->name
, args
->name
, args
->namelen
);
570 xfs_dir2_data_put_ftype(dp
->i_mount
, dep
, args
->filetype
);
571 tagp
= xfs_dir2_data_entry_tag_p(dp
->i_mount
, dep
);
572 *tagp
= cpu_to_be16((char *)dep
- (char *)hdr
);
574 * Clean up the bestfree array and log the header, tail, and entry.
577 xfs_dir2_data_freescan(dp
->i_mount
, hdr
, &needlog
);
579 xfs_dir2_data_log_header(args
, bp
);
580 xfs_dir2_block_log_tail(tp
, bp
);
581 xfs_dir2_data_log_entry(args
, bp
, dep
);
582 xfs_dir3_data_check(dp
, bp
);
587 * Log leaf entries from the block.
590 xfs_dir2_block_log_leaf(
591 xfs_trans_t
*tp
, /* transaction structure */
592 struct xfs_buf
*bp
, /* block buffer */
593 int first
, /* index of first logged leaf */
594 int last
) /* index of last logged leaf */
596 xfs_dir2_data_hdr_t
*hdr
= bp
->b_addr
;
597 xfs_dir2_leaf_entry_t
*blp
;
598 xfs_dir2_block_tail_t
*btp
;
600 btp
= xfs_dir2_block_tail_p(tp
->t_mountp
->m_dir_geo
, hdr
);
601 blp
= xfs_dir2_block_leaf_p(btp
);
602 xfs_trans_log_buf(tp
, bp
, (uint
)((char *)&blp
[first
] - (char *)hdr
),
603 (uint
)((char *)&blp
[last
+ 1] - (char *)hdr
- 1));
607 * Log the block tail.
610 xfs_dir2_block_log_tail(
611 xfs_trans_t
*tp
, /* transaction structure */
612 struct xfs_buf
*bp
) /* block buffer */
614 xfs_dir2_data_hdr_t
*hdr
= bp
->b_addr
;
615 xfs_dir2_block_tail_t
*btp
;
617 btp
= xfs_dir2_block_tail_p(tp
->t_mountp
->m_dir_geo
, hdr
);
618 xfs_trans_log_buf(tp
, bp
, (uint
)((char *)btp
- (char *)hdr
),
619 (uint
)((char *)(btp
+ 1) - (char *)hdr
- 1));
623 * Look up an entry in the block. This is the external routine,
624 * xfs_dir2_block_lookup_int does the real work.
627 xfs_dir2_block_lookup(
628 xfs_da_args_t
*args
) /* dir lookup arguments */
630 xfs_dir2_data_hdr_t
*hdr
; /* block header */
631 xfs_dir2_leaf_entry_t
*blp
; /* block leaf entries */
632 struct xfs_buf
*bp
; /* block buffer */
633 xfs_dir2_block_tail_t
*btp
; /* block tail */
634 xfs_dir2_data_entry_t
*dep
; /* block data entry */
635 xfs_inode_t
*dp
; /* incore inode */
636 int ent
; /* entry index */
637 int error
; /* error return value */
639 trace_xfs_dir2_block_lookup(args
);
642 * Get the buffer, look up the entry.
643 * If not found (ENOENT) then return, have no buffer.
645 if ((error
= xfs_dir2_block_lookup_int(args
, &bp
, &ent
)))
649 xfs_dir3_data_check(dp
, bp
);
650 btp
= xfs_dir2_block_tail_p(args
->geo
, hdr
);
651 blp
= xfs_dir2_block_leaf_p(btp
);
653 * Get the offset from the leaf entry, to point to the data.
655 dep
= (xfs_dir2_data_entry_t
*)((char *)hdr
+
656 xfs_dir2_dataptr_to_off(args
->geo
,
657 be32_to_cpu(blp
[ent
].address
)));
659 * Fill in inode number, CI name if appropriate, release the block.
661 args
->inumber
= be64_to_cpu(dep
->inumber
);
662 args
->filetype
= xfs_dir2_data_get_ftype(dp
->i_mount
, dep
);
663 error
= xfs_dir_cilookup_result(args
, dep
->name
, dep
->namelen
);
664 xfs_trans_brelse(args
->trans
, bp
);
669 * Internal block lookup routine.
671 static int /* error */
672 xfs_dir2_block_lookup_int(
673 xfs_da_args_t
*args
, /* dir lookup arguments */
674 struct xfs_buf
**bpp
, /* returned block buffer */
675 int *entno
) /* returned entry number */
677 xfs_dir2_dataptr_t addr
; /* data entry address */
678 xfs_dir2_data_hdr_t
*hdr
; /* block header */
679 xfs_dir2_leaf_entry_t
*blp
; /* block leaf entries */
680 struct xfs_buf
*bp
; /* block buffer */
681 xfs_dir2_block_tail_t
*btp
; /* block tail */
682 xfs_dir2_data_entry_t
*dep
; /* block data entry */
683 xfs_inode_t
*dp
; /* incore inode */
684 int error
; /* error return value */
685 xfs_dahash_t hash
; /* found hash value */
686 int high
; /* binary search high index */
687 int low
; /* binary search low index */
688 int mid
; /* binary search current idx */
689 xfs_trans_t
*tp
; /* transaction pointer */
690 enum xfs_dacmp cmp
; /* comparison result */
695 error
= xfs_dir3_block_read(tp
, dp
, &bp
);
700 xfs_dir3_data_check(dp
, bp
);
701 btp
= xfs_dir2_block_tail_p(args
->geo
, hdr
);
702 blp
= xfs_dir2_block_leaf_p(btp
);
704 * Loop doing a binary search for our hash value.
705 * Find our entry, ENOENT if it's not there.
707 for (low
= 0, high
= be32_to_cpu(btp
->count
) - 1; ; ) {
709 mid
= (low
+ high
) >> 1;
710 if ((hash
= be32_to_cpu(blp
[mid
].hashval
)) == args
->hashval
)
712 if (hash
< args
->hashval
)
717 ASSERT(args
->op_flags
& XFS_DA_OP_OKNOENT
);
718 xfs_trans_brelse(tp
, bp
);
723 * Back up to the first one with the right hash value.
725 while (mid
> 0 && be32_to_cpu(blp
[mid
- 1].hashval
) == args
->hashval
) {
729 * Now loop forward through all the entries with the
730 * right hash value looking for our name.
733 if ((addr
= be32_to_cpu(blp
[mid
].address
)) == XFS_DIR2_NULL_DATAPTR
)
736 * Get pointer to the entry from the leaf.
738 dep
= (xfs_dir2_data_entry_t
*)
739 ((char *)hdr
+ xfs_dir2_dataptr_to_off(args
->geo
, addr
));
741 * Compare name and if it's an exact match, return the index
742 * and buffer. If it's the first case-insensitive match, store
743 * the index and buffer and continue looking for an exact match.
745 cmp
= xfs_dir2_compname(args
, dep
->name
, dep
->namelen
);
746 if (cmp
!= XFS_CMP_DIFFERENT
&& cmp
!= args
->cmpresult
) {
747 args
->cmpresult
= cmp
;
750 if (cmp
== XFS_CMP_EXACT
)
753 } while (++mid
< be32_to_cpu(btp
->count
) &&
754 be32_to_cpu(blp
[mid
].hashval
) == hash
);
756 ASSERT(args
->op_flags
& XFS_DA_OP_OKNOENT
);
758 * Here, we can only be doing a lookup (not a rename or replace).
759 * If a case-insensitive match was found earlier, return success.
761 if (args
->cmpresult
== XFS_CMP_CASE
)
764 * No match, release the buffer and return ENOENT.
766 xfs_trans_brelse(tp
, bp
);
771 * Remove an entry from a block format directory.
772 * If that makes the block small enough to fit in shortform, transform it.
775 xfs_dir2_block_removename(
776 xfs_da_args_t
*args
) /* directory operation args */
778 xfs_dir2_data_hdr_t
*hdr
; /* block header */
779 xfs_dir2_leaf_entry_t
*blp
; /* block leaf pointer */
780 struct xfs_buf
*bp
; /* block buffer */
781 xfs_dir2_block_tail_t
*btp
; /* block tail */
782 xfs_dir2_data_entry_t
*dep
; /* block data entry */
783 xfs_inode_t
*dp
; /* incore inode */
784 int ent
; /* block leaf entry index */
785 int error
; /* error return value */
786 int needlog
; /* need to log block header */
787 int needscan
; /* need to fixup bestfree */
788 xfs_dir2_sf_hdr_t sfh
; /* shortform header */
789 int size
; /* shortform size */
790 xfs_trans_t
*tp
; /* transaction pointer */
792 trace_xfs_dir2_block_removename(args
);
795 * Look up the entry in the block. Gets the buffer and entry index.
796 * It will always be there, the vnodeops level does a lookup first.
798 if ((error
= xfs_dir2_block_lookup_int(args
, &bp
, &ent
))) {
804 btp
= xfs_dir2_block_tail_p(args
->geo
, hdr
);
805 blp
= xfs_dir2_block_leaf_p(btp
);
807 * Point to the data entry using the leaf entry.
809 dep
= (xfs_dir2_data_entry_t
*)((char *)hdr
+
810 xfs_dir2_dataptr_to_off(args
->geo
,
811 be32_to_cpu(blp
[ent
].address
)));
813 * Mark the data entry's space free.
815 needlog
= needscan
= 0;
816 xfs_dir2_data_make_free(args
, bp
,
817 (xfs_dir2_data_aoff_t
)((char *)dep
- (char *)hdr
),
818 xfs_dir2_data_entsize(dp
->i_mount
, dep
->namelen
), &needlog
,
821 * Fix up the block tail.
823 be32_add_cpu(&btp
->stale
, 1);
824 xfs_dir2_block_log_tail(tp
, bp
);
826 * Remove the leaf entry by marking it stale.
828 blp
[ent
].address
= cpu_to_be32(XFS_DIR2_NULL_DATAPTR
);
829 xfs_dir2_block_log_leaf(tp
, bp
, ent
, ent
);
831 * Fix up bestfree, log the header if necessary.
834 xfs_dir2_data_freescan(dp
->i_mount
, hdr
, &needlog
);
836 xfs_dir2_data_log_header(args
, bp
);
837 xfs_dir3_data_check(dp
, bp
);
839 * See if the size as a shortform is good enough.
841 size
= xfs_dir2_block_sfsize(dp
, hdr
, &sfh
);
842 if (size
> xfs_inode_data_fork_size(dp
))
846 * If it works, do the conversion.
848 return xfs_dir2_block_to_sf(args
, bp
, size
, &sfh
);
852 * Replace an entry in a V2 block directory.
853 * Change the inode number to the new value.
856 xfs_dir2_block_replace(
857 xfs_da_args_t
*args
) /* directory operation args */
859 xfs_dir2_data_hdr_t
*hdr
; /* block header */
860 xfs_dir2_leaf_entry_t
*blp
; /* block leaf entries */
861 struct xfs_buf
*bp
; /* block buffer */
862 xfs_dir2_block_tail_t
*btp
; /* block tail */
863 xfs_dir2_data_entry_t
*dep
; /* block data entry */
864 xfs_inode_t
*dp
; /* incore inode */
865 int ent
; /* leaf entry index */
866 int error
; /* error return value */
868 trace_xfs_dir2_block_replace(args
);
871 * Lookup the entry in the directory. Get buffer and entry index.
872 * This will always succeed since the caller has already done a lookup.
874 if ((error
= xfs_dir2_block_lookup_int(args
, &bp
, &ent
))) {
879 btp
= xfs_dir2_block_tail_p(args
->geo
, hdr
);
880 blp
= xfs_dir2_block_leaf_p(btp
);
882 * Point to the data entry we need to change.
884 dep
= (xfs_dir2_data_entry_t
*)((char *)hdr
+
885 xfs_dir2_dataptr_to_off(args
->geo
,
886 be32_to_cpu(blp
[ent
].address
)));
887 ASSERT(be64_to_cpu(dep
->inumber
) != args
->inumber
);
889 * Change the inode number to the new value.
891 dep
->inumber
= cpu_to_be64(args
->inumber
);
892 xfs_dir2_data_put_ftype(dp
->i_mount
, dep
, args
->filetype
);
893 xfs_dir2_data_log_entry(args
, bp
, dep
);
894 xfs_dir3_data_check(dp
, bp
);
899 * Qsort comparison routine for the block leaf entries.
901 static int /* sort order */
903 const void *a
, /* first leaf entry */
904 const void *b
) /* second leaf entry */
906 const xfs_dir2_leaf_entry_t
*la
; /* first leaf entry */
907 const xfs_dir2_leaf_entry_t
*lb
; /* second leaf entry */
911 return be32_to_cpu(la
->hashval
) < be32_to_cpu(lb
->hashval
) ? -1 :
912 (be32_to_cpu(la
->hashval
) > be32_to_cpu(lb
->hashval
) ? 1 : 0);
916 * Convert a V2 leaf directory to a V2 block directory if possible.
919 xfs_dir2_leaf_to_block(
920 xfs_da_args_t
*args
, /* operation arguments */
921 struct xfs_buf
*lbp
, /* leaf buffer */
922 struct xfs_buf
*dbp
) /* data buffer */
924 __be16
*bestsp
; /* leaf bests table */
925 xfs_dir2_data_hdr_t
*hdr
; /* block header */
926 xfs_dir2_block_tail_t
*btp
; /* block tail */
927 xfs_inode_t
*dp
; /* incore directory inode */
928 xfs_dir2_data_unused_t
*dup
; /* unused data entry */
929 int error
; /* error return value */
930 int from
; /* leaf from index */
931 xfs_dir2_leaf_t
*leaf
; /* leaf structure */
932 xfs_dir2_leaf_entry_t
*lep
; /* leaf entry */
933 xfs_dir2_leaf_tail_t
*ltp
; /* leaf tail structure */
934 xfs_mount_t
*mp
; /* file system mount point */
935 int needlog
; /* need to log data header */
936 int needscan
; /* need to scan for bestfree */
937 xfs_dir2_sf_hdr_t sfh
; /* shortform header */
938 int size
; /* bytes used */
939 __be16
*tagp
; /* end of entry (tag) */
940 int to
; /* block/leaf to index */
941 xfs_trans_t
*tp
; /* transaction pointer */
942 struct xfs_dir3_icleaf_hdr leafhdr
;
944 trace_xfs_dir2_leaf_to_block(args
);
950 xfs_dir2_leaf_hdr_from_disk(mp
, &leafhdr
, leaf
);
951 ltp
= xfs_dir2_leaf_tail_p(args
->geo
, leaf
);
953 ASSERT(leafhdr
.magic
== XFS_DIR2_LEAF1_MAGIC
||
954 leafhdr
.magic
== XFS_DIR3_LEAF1_MAGIC
);
956 * If there are data blocks other than the first one, take this
957 * opportunity to remove trailing empty data blocks that may have
958 * been left behind during no-space-reservation operations.
959 * These will show up in the leaf bests table.
961 while (dp
->i_disk_size
> args
->geo
->blksize
) {
964 hdrsz
= args
->geo
->data_entry_offset
;
965 bestsp
= xfs_dir2_leaf_bests_p(ltp
);
966 if (be16_to_cpu(bestsp
[be32_to_cpu(ltp
->bestcount
) - 1]) ==
967 args
->geo
->blksize
- hdrsz
) {
969 xfs_dir2_leaf_trim_data(args
, lbp
,
970 (xfs_dir2_db_t
)(be32_to_cpu(ltp
->bestcount
) - 1))))
976 * Read the data block if we don't already have it, give up if it fails.
979 error
= xfs_dir3_data_read(tp
, dp
, args
->geo
->datablk
, 0, &dbp
);
984 ASSERT(hdr
->magic
== cpu_to_be32(XFS_DIR2_DATA_MAGIC
) ||
985 hdr
->magic
== cpu_to_be32(XFS_DIR3_DATA_MAGIC
));
988 * Size of the "leaf" area in the block.
990 size
= (uint
)sizeof(xfs_dir2_block_tail_t
) +
991 (uint
)sizeof(*lep
) * (leafhdr
.count
- leafhdr
.stale
);
993 * Look at the last data entry.
995 tagp
= (__be16
*)((char *)hdr
+ args
->geo
->blksize
) - 1;
996 dup
= (xfs_dir2_data_unused_t
*)((char *)hdr
+ be16_to_cpu(*tagp
));
998 * If it's not free or is too short we can't do it.
1000 if (be16_to_cpu(dup
->freetag
) != XFS_DIR2_DATA_FREE_TAG
||
1001 be16_to_cpu(dup
->length
) < size
)
1005 * Start converting it to block form.
1007 xfs_dir3_block_init(mp
, tp
, dbp
, dp
);
1012 * Use up the space at the end of the block (blp/btp).
1014 error
= xfs_dir2_data_use_free(args
, dbp
, dup
,
1015 args
->geo
->blksize
- size
, size
, &needlog
, &needscan
);
1019 * Initialize the block tail.
1021 btp
= xfs_dir2_block_tail_p(args
->geo
, hdr
);
1022 btp
->count
= cpu_to_be32(leafhdr
.count
- leafhdr
.stale
);
1024 xfs_dir2_block_log_tail(tp
, dbp
);
1026 * Initialize the block leaf area. We compact out stale entries.
1028 lep
= xfs_dir2_block_leaf_p(btp
);
1029 for (from
= to
= 0; from
< leafhdr
.count
; from
++) {
1030 if (leafhdr
.ents
[from
].address
==
1031 cpu_to_be32(XFS_DIR2_NULL_DATAPTR
))
1033 lep
[to
++] = leafhdr
.ents
[from
];
1035 ASSERT(to
== be32_to_cpu(btp
->count
));
1036 xfs_dir2_block_log_leaf(tp
, dbp
, 0, be32_to_cpu(btp
->count
) - 1);
1038 * Scan the bestfree if we need it and log the data block header.
1041 xfs_dir2_data_freescan(dp
->i_mount
, hdr
, &needlog
);
1043 xfs_dir2_data_log_header(args
, dbp
);
1045 * Pitch the old leaf block.
1047 error
= xfs_da_shrink_inode(args
, args
->geo
->leafblk
, lbp
);
1052 * Now see if the resulting block can be shrunken to shortform.
1054 size
= xfs_dir2_block_sfsize(dp
, hdr
, &sfh
);
1055 if (size
> xfs_inode_data_fork_size(dp
))
1058 return xfs_dir2_block_to_sf(args
, dbp
, size
, &sfh
);
1062 * Convert the shortform directory to block form.
1065 xfs_dir2_sf_to_block(
1066 struct xfs_da_args
*args
)
1068 struct xfs_trans
*tp
= args
->trans
;
1069 struct xfs_inode
*dp
= args
->dp
;
1070 struct xfs_mount
*mp
= dp
->i_mount
;
1071 struct xfs_ifork
*ifp
= xfs_ifork_ptr(dp
, XFS_DATA_FORK
);
1072 struct xfs_da_geometry
*geo
= args
->geo
;
1073 xfs_dir2_db_t blkno
; /* dir-relative block # (0) */
1074 xfs_dir2_data_hdr_t
*hdr
; /* block header */
1075 xfs_dir2_leaf_entry_t
*blp
; /* block leaf entries */
1076 struct xfs_buf
*bp
; /* block buffer */
1077 xfs_dir2_block_tail_t
*btp
; /* block tail pointer */
1078 xfs_dir2_data_entry_t
*dep
; /* data entry pointer */
1079 int dummy
; /* trash */
1080 xfs_dir2_data_unused_t
*dup
; /* unused entry pointer */
1081 int endoffset
; /* end of data objects */
1082 int error
; /* error return value */
1084 int needlog
; /* need to log block header */
1085 int needscan
; /* need to scan block freespc */
1086 int newoffset
; /* offset from current entry */
1087 unsigned int offset
= geo
->data_entry_offset
;
1088 xfs_dir2_sf_entry_t
*sfep
; /* sf entry pointer */
1089 xfs_dir2_sf_hdr_t
*oldsfp
; /* old shortform header */
1090 xfs_dir2_sf_hdr_t
*sfp
; /* shortform header */
1091 __be16
*tagp
; /* end of data entry */
1092 struct xfs_name name
;
1094 trace_xfs_dir2_sf_to_block(args
);
1096 ASSERT(ifp
->if_format
== XFS_DINODE_FMT_LOCAL
);
1097 ASSERT(dp
->i_disk_size
>= offsetof(struct xfs_dir2_sf_hdr
, parent
));
1099 oldsfp
= (xfs_dir2_sf_hdr_t
*)ifp
->if_u1
.if_data
;
1101 ASSERT(ifp
->if_bytes
== dp
->i_disk_size
);
1102 ASSERT(ifp
->if_u1
.if_data
!= NULL
);
1103 ASSERT(dp
->i_disk_size
>= xfs_dir2_sf_hdr_size(oldsfp
->i8count
));
1104 ASSERT(dp
->i_df
.if_nextents
== 0);
1107 * Copy the directory into a temporary buffer.
1108 * Then pitch the incore inode data so we can make extents.
1110 sfp
= kmem_alloc(ifp
->if_bytes
, 0);
1111 memcpy(sfp
, oldsfp
, ifp
->if_bytes
);
1113 xfs_idata_realloc(dp
, -ifp
->if_bytes
, XFS_DATA_FORK
);
1114 xfs_bmap_local_to_extents_empty(tp
, dp
, XFS_DATA_FORK
);
1115 dp
->i_disk_size
= 0;
1118 * Add block 0 to the inode.
1120 error
= xfs_dir2_grow_inode(args
, XFS_DIR2_DATA_SPACE
, &blkno
);
1124 * Initialize the data block, then convert it to block format.
1126 error
= xfs_dir3_data_init(args
, blkno
, &bp
);
1129 xfs_dir3_block_init(mp
, tp
, bp
, dp
);
1133 * Compute size of block "tail" area.
1135 i
= (uint
)sizeof(*btp
) +
1136 (sfp
->count
+ 2) * (uint
)sizeof(xfs_dir2_leaf_entry_t
);
1138 * The whole thing is initialized to free by the init routine.
1139 * Say we're using the leaf and tail area.
1141 dup
= bp
->b_addr
+ offset
;
1142 needlog
= needscan
= 0;
1143 error
= xfs_dir2_data_use_free(args
, bp
, dup
, args
->geo
->blksize
- i
,
1144 i
, &needlog
, &needscan
);
1147 ASSERT(needscan
== 0);
1151 btp
= xfs_dir2_block_tail_p(args
->geo
, hdr
);
1152 btp
->count
= cpu_to_be32(sfp
->count
+ 2); /* ., .. */
1154 blp
= xfs_dir2_block_leaf_p(btp
);
1155 endoffset
= (uint
)((char *)blp
- (char *)hdr
);
1157 * Remove the freespace, we'll manage it.
1159 error
= xfs_dir2_data_use_free(args
, bp
, dup
,
1160 (xfs_dir2_data_aoff_t
)((char *)dup
- (char *)hdr
),
1161 be16_to_cpu(dup
->length
), &needlog
, &needscan
);
1166 * Create entry for .
1168 dep
= bp
->b_addr
+ offset
;
1169 dep
->inumber
= cpu_to_be64(dp
->i_ino
);
1172 xfs_dir2_data_put_ftype(mp
, dep
, XFS_DIR3_FT_DIR
);
1173 tagp
= xfs_dir2_data_entry_tag_p(mp
, dep
);
1174 *tagp
= cpu_to_be16(offset
);
1175 xfs_dir2_data_log_entry(args
, bp
, dep
);
1176 blp
[0].hashval
= cpu_to_be32(xfs_dir_hash_dot
);
1177 blp
[0].address
= cpu_to_be32(xfs_dir2_byte_to_dataptr(offset
));
1178 offset
+= xfs_dir2_data_entsize(mp
, dep
->namelen
);
1181 * Create entry for ..
1183 dep
= bp
->b_addr
+ offset
;
1184 dep
->inumber
= cpu_to_be64(xfs_dir2_sf_get_parent_ino(sfp
));
1186 dep
->name
[0] = dep
->name
[1] = '.';
1187 xfs_dir2_data_put_ftype(mp
, dep
, XFS_DIR3_FT_DIR
);
1188 tagp
= xfs_dir2_data_entry_tag_p(mp
, dep
);
1189 *tagp
= cpu_to_be16(offset
);
1190 xfs_dir2_data_log_entry(args
, bp
, dep
);
1191 blp
[1].hashval
= cpu_to_be32(xfs_dir_hash_dotdot
);
1192 blp
[1].address
= cpu_to_be32(xfs_dir2_byte_to_dataptr(offset
));
1193 offset
+= xfs_dir2_data_entsize(mp
, dep
->namelen
);
1196 * Loop over existing entries, stuff them in.
1202 sfep
= xfs_dir2_sf_firstentry(sfp
);
1205 * Need to preserve the existing offset values in the sf directory.
1206 * Insert holes (unused entries) where necessary.
1208 while (offset
< endoffset
) {
1210 * sfep is null when we reach the end of the list.
1213 newoffset
= endoffset
;
1215 newoffset
= xfs_dir2_sf_get_offset(sfep
);
1217 * There should be a hole here, make one.
1219 if (offset
< newoffset
) {
1220 dup
= bp
->b_addr
+ offset
;
1221 dup
->freetag
= cpu_to_be16(XFS_DIR2_DATA_FREE_TAG
);
1222 dup
->length
= cpu_to_be16(newoffset
- offset
);
1223 *xfs_dir2_data_unused_tag_p(dup
) = cpu_to_be16(offset
);
1224 xfs_dir2_data_log_unused(args
, bp
, dup
);
1225 xfs_dir2_data_freeinsert(hdr
,
1226 xfs_dir2_data_bestfree_p(mp
, hdr
),
1228 offset
+= be16_to_cpu(dup
->length
);
1232 * Copy a real entry.
1234 dep
= bp
->b_addr
+ newoffset
;
1235 dep
->inumber
= cpu_to_be64(xfs_dir2_sf_get_ino(mp
, sfp
, sfep
));
1236 dep
->namelen
= sfep
->namelen
;
1237 xfs_dir2_data_put_ftype(mp
, dep
,
1238 xfs_dir2_sf_get_ftype(mp
, sfep
));
1239 memcpy(dep
->name
, sfep
->name
, dep
->namelen
);
1240 tagp
= xfs_dir2_data_entry_tag_p(mp
, dep
);
1241 *tagp
= cpu_to_be16(newoffset
);
1242 xfs_dir2_data_log_entry(args
, bp
, dep
);
1243 name
.name
= sfep
->name
;
1244 name
.len
= sfep
->namelen
;
1245 blp
[2 + i
].hashval
= cpu_to_be32(xfs_dir2_hashname(mp
, &name
));
1246 blp
[2 + i
].address
=
1247 cpu_to_be32(xfs_dir2_byte_to_dataptr(newoffset
));
1248 offset
= (int)((char *)(tagp
+ 1) - (char *)hdr
);
1249 if (++i
== sfp
->count
)
1252 sfep
= xfs_dir2_sf_nextentry(mp
, sfp
, sfep
);
1254 /* Done with the temporary buffer */
1257 * Sort the leaf entries by hash value.
1259 xfs_sort(blp
, be32_to_cpu(btp
->count
), sizeof(*blp
), xfs_dir2_block_sort
);
1261 * Log the leaf entry area and tail.
1262 * Already logged the header in data_init, ignore needlog.
1264 ASSERT(needscan
== 0);
1265 xfs_dir2_block_log_leaf(tp
, bp
, 0, be32_to_cpu(btp
->count
) - 1);
1266 xfs_dir2_block_log_tail(tp
, bp
);
1267 xfs_dir3_data_check(dp
, bp
);