1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2000-2003,2005 Silicon Graphics, Inc.
4 * Copyright (c) 2013 Red Hat, Inc.
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"
17 #include "xfs_buf_item.h"
19 #include "xfs_dir2_priv.h"
20 #include "xfs_error.h"
21 #include "xfs_trace.h"
25 * Local function prototypes.
27 static void xfs_dir2_block_log_leaf(xfs_trans_t
*tp
, struct xfs_buf
*bp
,
29 static void xfs_dir2_block_log_tail(xfs_trans_t
*tp
, struct xfs_buf
*bp
);
30 static int xfs_dir2_block_lookup_int(xfs_da_args_t
*args
, struct xfs_buf
**bpp
,
32 static int xfs_dir2_block_sort(const void *a
, const void *b
);
34 static xfs_dahash_t xfs_dir_hash_dot
, xfs_dir_hash_dotdot
;
37 * One-time startup routine called from xfs_init().
42 xfs_dir_hash_dot
= xfs_da_hashname((unsigned char *)".", 1);
43 xfs_dir_hash_dotdot
= xfs_da_hashname((unsigned char *)"..", 2);
47 xfs_dir3_block_verify(
50 struct xfs_mount
*mp
= bp
->b_mount
;
51 struct xfs_dir3_blk_hdr
*hdr3
= bp
->b_addr
;
53 if (!xfs_verify_magic(bp
, hdr3
->magic
))
54 return __this_address
;
56 if (xfs_sb_version_hascrc(&mp
->m_sb
)) {
57 if (!uuid_equal(&hdr3
->uuid
, &mp
->m_sb
.sb_meta_uuid
))
58 return __this_address
;
59 if (be64_to_cpu(hdr3
->blkno
) != bp
->b_bn
)
60 return __this_address
;
61 if (!xfs_log_check_lsn(mp
, be64_to_cpu(hdr3
->lsn
)))
62 return __this_address
;
64 return __xfs_dir3_data_check(NULL
, bp
);
68 xfs_dir3_block_read_verify(
71 struct xfs_mount
*mp
= bp
->b_mount
;
74 if (xfs_sb_version_hascrc(&mp
->m_sb
) &&
75 !xfs_buf_verify_cksum(bp
, XFS_DIR3_DATA_CRC_OFF
))
76 xfs_verifier_error(bp
, -EFSBADCRC
, __this_address
);
78 fa
= xfs_dir3_block_verify(bp
);
80 xfs_verifier_error(bp
, -EFSCORRUPTED
, fa
);
85 xfs_dir3_block_write_verify(
88 struct xfs_mount
*mp
= bp
->b_mount
;
89 struct xfs_buf_log_item
*bip
= bp
->b_log_item
;
90 struct xfs_dir3_blk_hdr
*hdr3
= bp
->b_addr
;
93 fa
= xfs_dir3_block_verify(bp
);
95 xfs_verifier_error(bp
, -EFSCORRUPTED
, fa
);
99 if (!xfs_sb_version_hascrc(&mp
->m_sb
))
103 hdr3
->lsn
= cpu_to_be64(bip
->bli_item
.li_lsn
);
105 xfs_buf_update_cksum(bp
, XFS_DIR3_DATA_CRC_OFF
);
108 const struct xfs_buf_ops xfs_dir3_block_buf_ops
= {
109 .name
= "xfs_dir3_block",
110 .magic
= { cpu_to_be32(XFS_DIR2_BLOCK_MAGIC
),
111 cpu_to_be32(XFS_DIR3_BLOCK_MAGIC
) },
112 .verify_read
= xfs_dir3_block_read_verify
,
113 .verify_write
= xfs_dir3_block_write_verify
,
114 .verify_struct
= xfs_dir3_block_verify
,
117 static xfs_failaddr_t
118 xfs_dir3_block_header_check(
119 struct xfs_inode
*dp
,
122 struct xfs_mount
*mp
= dp
->i_mount
;
124 if (xfs_sb_version_hascrc(&mp
->m_sb
)) {
125 struct xfs_dir3_blk_hdr
*hdr3
= bp
->b_addr
;
127 if (be64_to_cpu(hdr3
->owner
) != dp
->i_ino
)
128 return __this_address
;
136 struct xfs_trans
*tp
,
137 struct xfs_inode
*dp
,
138 struct xfs_buf
**bpp
)
140 struct xfs_mount
*mp
= dp
->i_mount
;
144 err
= xfs_da_read_buf(tp
, dp
, mp
->m_dir_geo
->datablk
, 0, bpp
,
145 XFS_DATA_FORK
, &xfs_dir3_block_buf_ops
);
149 /* Check things that we can't do in the verifier. */
150 fa
= xfs_dir3_block_header_check(dp
, *bpp
);
152 __xfs_buf_mark_corrupt(*bpp
, fa
);
153 xfs_trans_brelse(tp
, *bpp
);
155 return -EFSCORRUPTED
;
158 xfs_trans_buf_set_type(tp
, *bpp
, XFS_BLFT_DIR_BLOCK_BUF
);
164 struct xfs_mount
*mp
,
165 struct xfs_trans
*tp
,
167 struct xfs_inode
*dp
)
169 struct xfs_dir3_blk_hdr
*hdr3
= bp
->b_addr
;
171 bp
->b_ops
= &xfs_dir3_block_buf_ops
;
172 xfs_trans_buf_set_type(tp
, bp
, XFS_BLFT_DIR_BLOCK_BUF
);
174 if (xfs_sb_version_hascrc(&mp
->m_sb
)) {
175 memset(hdr3
, 0, sizeof(*hdr3
));
176 hdr3
->magic
= cpu_to_be32(XFS_DIR3_BLOCK_MAGIC
);
177 hdr3
->blkno
= cpu_to_be64(bp
->b_bn
);
178 hdr3
->owner
= cpu_to_be64(dp
->i_ino
);
179 uuid_copy(&hdr3
->uuid
, &mp
->m_sb
.sb_meta_uuid
);
183 hdr3
->magic
= cpu_to_be32(XFS_DIR2_BLOCK_MAGIC
);
187 xfs_dir2_block_need_space(
188 struct xfs_inode
*dp
,
189 struct xfs_dir2_data_hdr
*hdr
,
190 struct xfs_dir2_block_tail
*btp
,
191 struct xfs_dir2_leaf_entry
*blp
,
193 struct xfs_dir2_data_unused
**dupp
,
194 struct xfs_dir2_data_unused
**enddupp
,
198 struct xfs_dir2_data_free
*bf
;
200 struct xfs_dir2_data_unused
*dup
= NULL
;
201 struct xfs_dir2_data_unused
*enddup
= NULL
;
204 bf
= xfs_dir2_data_bestfree_p(dp
->i_mount
, hdr
);
207 * If there are stale entries we'll use one for the leaf.
210 if (be16_to_cpu(bf
[0].length
) >= len
) {
212 * The biggest entry enough to avoid compaction.
214 dup
= (xfs_dir2_data_unused_t
*)
215 ((char *)hdr
+ be16_to_cpu(bf
[0].offset
));
220 * Will need to compact to make this work.
221 * Tag just before the first leaf entry.
224 tagp
= (__be16
*)blp
- 1;
226 /* Data object just before the first leaf entry. */
227 dup
= (xfs_dir2_data_unused_t
*)((char *)hdr
+ be16_to_cpu(*tagp
));
230 * If it's not free then the data will go where the
231 * leaf data starts now, if it works at all.
233 if (be16_to_cpu(dup
->freetag
) == XFS_DIR2_DATA_FREE_TAG
) {
234 if (be16_to_cpu(dup
->length
) + (be32_to_cpu(btp
->stale
) - 1) *
235 (uint
)sizeof(*blp
) < len
)
237 } else if ((be32_to_cpu(btp
->stale
) - 1) * (uint
)sizeof(*blp
) < len
)
240 dup
= (xfs_dir2_data_unused_t
*)blp
;
245 * no stale entries, so just use free space.
246 * Tag just before the first leaf entry.
248 tagp
= (__be16
*)blp
- 1;
250 /* Data object just before the first leaf entry. */
251 enddup
= (xfs_dir2_data_unused_t
*)((char *)hdr
+ be16_to_cpu(*tagp
));
254 * If it's not free then can't do this add without cleaning up:
255 * the space before the first leaf entry needs to be free so it
256 * can be expanded to hold the pointer to the new entry.
258 if (be16_to_cpu(enddup
->freetag
) == XFS_DIR2_DATA_FREE_TAG
) {
260 * Check out the biggest freespace and see if it's the same one.
262 dup
= (xfs_dir2_data_unused_t
*)
263 ((char *)hdr
+ be16_to_cpu(bf
[0].offset
));
266 * Not the same free entry, just check its length.
268 if (be16_to_cpu(dup
->length
) < len
)
274 * It is the biggest freespace, can it hold the leaf too?
276 if (be16_to_cpu(dup
->length
) < len
+ (uint
)sizeof(*blp
)) {
278 * Yes, use the second-largest entry instead if it works.
280 if (be16_to_cpu(bf
[1].length
) >= len
)
281 dup
= (xfs_dir2_data_unused_t
*)
282 ((char *)hdr
+ be16_to_cpu(bf
[1].offset
));
294 * compact the leaf entries.
295 * Leave the highest-numbered stale entry stale.
296 * XXX should be the one closest to mid but mid is not yet computed.
299 xfs_dir2_block_compact(
300 struct xfs_da_args
*args
,
302 struct xfs_dir2_data_hdr
*hdr
,
303 struct xfs_dir2_block_tail
*btp
,
304 struct xfs_dir2_leaf_entry
*blp
,
309 int fromidx
; /* source leaf index */
310 int toidx
; /* target leaf index */
312 int highstale
; /* high stale index */
314 fromidx
= toidx
= be32_to_cpu(btp
->count
) - 1;
315 highstale
= *lfloghigh
= -1;
316 for (; fromidx
>= 0; fromidx
--) {
317 if (blp
[fromidx
].address
== cpu_to_be32(XFS_DIR2_NULL_DATAPTR
)) {
321 if (*lfloghigh
== -1)
327 blp
[toidx
] = blp
[fromidx
];
330 *lfloglow
= toidx
+ 1 - (be32_to_cpu(btp
->stale
) - 1);
331 *lfloghigh
-= be32_to_cpu(btp
->stale
) - 1;
332 be32_add_cpu(&btp
->count
, -(be32_to_cpu(btp
->stale
) - 1));
333 xfs_dir2_data_make_free(args
, bp
,
334 (xfs_dir2_data_aoff_t
)((char *)blp
- (char *)hdr
),
335 (xfs_dir2_data_aoff_t
)((be32_to_cpu(btp
->stale
) - 1) * sizeof(*blp
)),
337 btp
->stale
= cpu_to_be32(1);
339 * If we now need to rebuild the bestfree map, do so.
340 * This needs to happen before the next call to use_free.
343 xfs_dir2_data_freescan(args
->dp
->i_mount
, hdr
, needlog
);
347 * Add an entry to a block directory.
350 xfs_dir2_block_addname(
351 xfs_da_args_t
*args
) /* directory op arguments */
353 xfs_dir2_data_hdr_t
*hdr
; /* block header */
354 xfs_dir2_leaf_entry_t
*blp
; /* block leaf entries */
355 struct xfs_buf
*bp
; /* buffer for block */
356 xfs_dir2_block_tail_t
*btp
; /* block tail */
357 int compact
; /* need to compact leaf ents */
358 xfs_dir2_data_entry_t
*dep
; /* block data entry */
359 xfs_inode_t
*dp
; /* directory inode */
360 xfs_dir2_data_unused_t
*dup
; /* block unused entry */
361 int error
; /* error return value */
362 xfs_dir2_data_unused_t
*enddup
=NULL
; /* unused at end of data */
363 xfs_dahash_t hash
; /* hash value of found entry */
364 int high
; /* high index for binary srch */
365 int highstale
; /* high stale index */
366 int lfloghigh
=0; /* last final leaf to log */
367 int lfloglow
=0; /* first final leaf to log */
368 int len
; /* length of the new entry */
369 int low
; /* low index for binary srch */
370 int lowstale
; /* low stale index */
371 int mid
=0; /* midpoint for binary srch */
372 int needlog
; /* need to log header */
373 int needscan
; /* need to rescan freespace */
374 __be16
*tagp
; /* pointer to tag value */
375 xfs_trans_t
*tp
; /* transaction structure */
377 trace_xfs_dir2_block_addname(args
);
382 /* Read the (one and only) directory block into bp. */
383 error
= xfs_dir3_block_read(tp
, dp
, &bp
);
387 len
= xfs_dir2_data_entsize(dp
->i_mount
, args
->namelen
);
390 * Set up pointers to parts of the block.
393 btp
= xfs_dir2_block_tail_p(args
->geo
, hdr
);
394 blp
= xfs_dir2_block_leaf_p(btp
);
397 * Find out if we can reuse stale entries or whether we need extra
398 * space for entry and new leaf.
400 xfs_dir2_block_need_space(dp
, hdr
, btp
, blp
, &tagp
, &dup
,
401 &enddup
, &compact
, len
);
404 * Done everything we need for a space check now.
406 if (args
->op_flags
& XFS_DA_OP_JUSTCHECK
) {
407 xfs_trans_brelse(tp
, bp
);
414 * If we don't have space for the new entry & leaf ...
417 /* Don't have a space reservation: return no-space. */
418 if (args
->total
== 0)
421 * Convert to the next larger format.
422 * Then add the new entry in that format.
424 error
= xfs_dir2_block_to_leaf(args
, bp
);
427 return xfs_dir2_leaf_addname(args
);
430 needlog
= needscan
= 0;
433 * If need to compact the leaf entries, do it now.
436 xfs_dir2_block_compact(args
, bp
, hdr
, btp
, blp
, &needlog
,
437 &lfloghigh
, &lfloglow
);
438 /* recalculate blp post-compaction */
439 blp
= xfs_dir2_block_leaf_p(btp
);
440 } else if (btp
->stale
) {
442 * Set leaf logging boundaries to impossible state.
443 * For the no-stale case they're set explicitly.
445 lfloglow
= be32_to_cpu(btp
->count
);
450 * Find the slot that's first lower than our hash value, -1 if none.
452 for (low
= 0, high
= be32_to_cpu(btp
->count
) - 1; low
<= high
; ) {
453 mid
= (low
+ high
) >> 1;
454 if ((hash
= be32_to_cpu(blp
[mid
].hashval
)) == args
->hashval
)
456 if (hash
< args
->hashval
)
461 while (mid
>= 0 && be32_to_cpu(blp
[mid
].hashval
) >= args
->hashval
) {
465 * No stale entries, will use enddup space to hold new leaf.
468 xfs_dir2_data_aoff_t aoff
;
471 * Mark the space needed for the new leaf entry, now in use.
473 aoff
= (xfs_dir2_data_aoff_t
)((char *)enddup
- (char *)hdr
+
474 be16_to_cpu(enddup
->length
) - sizeof(*blp
));
475 error
= xfs_dir2_data_use_free(args
, bp
, enddup
, aoff
,
476 (xfs_dir2_data_aoff_t
)sizeof(*blp
), &needlog
,
482 * Update the tail (entry count).
484 be32_add_cpu(&btp
->count
, 1);
486 * If we now need to rebuild the bestfree map, do so.
487 * This needs to happen before the next call to use_free.
490 xfs_dir2_data_freescan(dp
->i_mount
, hdr
, &needlog
);
494 * Adjust pointer to the first leaf entry, we're about to move
495 * the table up one to open up space for the new leaf entry.
496 * Then adjust our index to match.
501 memmove(blp
, &blp
[1], mid
* sizeof(*blp
));
506 * Use a stale leaf for our new entry.
511 blp
[lowstale
].address
!=
512 cpu_to_be32(XFS_DIR2_NULL_DATAPTR
);
515 for (highstale
= mid
+ 1;
516 highstale
< be32_to_cpu(btp
->count
) &&
517 blp
[highstale
].address
!=
518 cpu_to_be32(XFS_DIR2_NULL_DATAPTR
) &&
519 (lowstale
< 0 || mid
- lowstale
> highstale
- mid
);
523 * Move entries toward the low-numbered stale entry.
526 (highstale
== be32_to_cpu(btp
->count
) ||
527 mid
- lowstale
<= highstale
- mid
)) {
529 memmove(&blp
[lowstale
], &blp
[lowstale
+ 1],
530 (mid
- lowstale
) * sizeof(*blp
));
531 lfloglow
= min(lowstale
, lfloglow
);
532 lfloghigh
= max(mid
, lfloghigh
);
535 * Move entries toward the high-numbered stale entry.
538 ASSERT(highstale
< be32_to_cpu(btp
->count
));
541 memmove(&blp
[mid
+ 1], &blp
[mid
],
542 (highstale
- mid
) * sizeof(*blp
));
543 lfloglow
= min(mid
, lfloglow
);
544 lfloghigh
= max(highstale
, lfloghigh
);
546 be32_add_cpu(&btp
->stale
, -1);
549 * Point to the new data entry.
551 dep
= (xfs_dir2_data_entry_t
*)dup
;
553 * Fill in the leaf entry.
555 blp
[mid
].hashval
= cpu_to_be32(args
->hashval
);
556 blp
[mid
].address
= cpu_to_be32(xfs_dir2_byte_to_dataptr(
557 (char *)dep
- (char *)hdr
));
558 xfs_dir2_block_log_leaf(tp
, bp
, lfloglow
, lfloghigh
);
560 * Mark space for the data entry used.
562 error
= xfs_dir2_data_use_free(args
, bp
, dup
,
563 (xfs_dir2_data_aoff_t
)((char *)dup
- (char *)hdr
),
564 (xfs_dir2_data_aoff_t
)len
, &needlog
, &needscan
);
568 * Create the new data entry.
570 dep
->inumber
= cpu_to_be64(args
->inumber
);
571 dep
->namelen
= args
->namelen
;
572 memcpy(dep
->name
, args
->name
, args
->namelen
);
573 xfs_dir2_data_put_ftype(dp
->i_mount
, dep
, args
->filetype
);
574 tagp
= xfs_dir2_data_entry_tag_p(dp
->i_mount
, dep
);
575 *tagp
= cpu_to_be16((char *)dep
- (char *)hdr
);
577 * Clean up the bestfree array and log the header, tail, and entry.
580 xfs_dir2_data_freescan(dp
->i_mount
, hdr
, &needlog
);
582 xfs_dir2_data_log_header(args
, bp
);
583 xfs_dir2_block_log_tail(tp
, bp
);
584 xfs_dir2_data_log_entry(args
, bp
, dep
);
585 xfs_dir3_data_check(dp
, bp
);
590 * Log leaf entries from the block.
593 xfs_dir2_block_log_leaf(
594 xfs_trans_t
*tp
, /* transaction structure */
595 struct xfs_buf
*bp
, /* block buffer */
596 int first
, /* index of first logged leaf */
597 int last
) /* index of last logged leaf */
599 xfs_dir2_data_hdr_t
*hdr
= bp
->b_addr
;
600 xfs_dir2_leaf_entry_t
*blp
;
601 xfs_dir2_block_tail_t
*btp
;
603 btp
= xfs_dir2_block_tail_p(tp
->t_mountp
->m_dir_geo
, hdr
);
604 blp
= xfs_dir2_block_leaf_p(btp
);
605 xfs_trans_log_buf(tp
, bp
, (uint
)((char *)&blp
[first
] - (char *)hdr
),
606 (uint
)((char *)&blp
[last
+ 1] - (char *)hdr
- 1));
610 * Log the block tail.
613 xfs_dir2_block_log_tail(
614 xfs_trans_t
*tp
, /* transaction structure */
615 struct xfs_buf
*bp
) /* block buffer */
617 xfs_dir2_data_hdr_t
*hdr
= bp
->b_addr
;
618 xfs_dir2_block_tail_t
*btp
;
620 btp
= xfs_dir2_block_tail_p(tp
->t_mountp
->m_dir_geo
, hdr
);
621 xfs_trans_log_buf(tp
, bp
, (uint
)((char *)btp
- (char *)hdr
),
622 (uint
)((char *)(btp
+ 1) - (char *)hdr
- 1));
626 * Look up an entry in the block. This is the external routine,
627 * xfs_dir2_block_lookup_int does the real work.
630 xfs_dir2_block_lookup(
631 xfs_da_args_t
*args
) /* dir lookup arguments */
633 xfs_dir2_data_hdr_t
*hdr
; /* block header */
634 xfs_dir2_leaf_entry_t
*blp
; /* block leaf entries */
635 struct xfs_buf
*bp
; /* block buffer */
636 xfs_dir2_block_tail_t
*btp
; /* block tail */
637 xfs_dir2_data_entry_t
*dep
; /* block data entry */
638 xfs_inode_t
*dp
; /* incore inode */
639 int ent
; /* entry index */
640 int error
; /* error return value */
642 trace_xfs_dir2_block_lookup(args
);
645 * Get the buffer, look up the entry.
646 * If not found (ENOENT) then return, have no buffer.
648 if ((error
= xfs_dir2_block_lookup_int(args
, &bp
, &ent
)))
652 xfs_dir3_data_check(dp
, bp
);
653 btp
= xfs_dir2_block_tail_p(args
->geo
, hdr
);
654 blp
= xfs_dir2_block_leaf_p(btp
);
656 * Get the offset from the leaf entry, to point to the data.
658 dep
= (xfs_dir2_data_entry_t
*)((char *)hdr
+
659 xfs_dir2_dataptr_to_off(args
->geo
,
660 be32_to_cpu(blp
[ent
].address
)));
662 * Fill in inode number, CI name if appropriate, release the block.
664 args
->inumber
= be64_to_cpu(dep
->inumber
);
665 args
->filetype
= xfs_dir2_data_get_ftype(dp
->i_mount
, dep
);
666 error
= xfs_dir_cilookup_result(args
, dep
->name
, dep
->namelen
);
667 xfs_trans_brelse(args
->trans
, bp
);
672 * Internal block lookup routine.
674 static int /* error */
675 xfs_dir2_block_lookup_int(
676 xfs_da_args_t
*args
, /* dir lookup arguments */
677 struct xfs_buf
**bpp
, /* returned block buffer */
678 int *entno
) /* returned entry number */
680 xfs_dir2_dataptr_t addr
; /* data entry address */
681 xfs_dir2_data_hdr_t
*hdr
; /* block header */
682 xfs_dir2_leaf_entry_t
*blp
; /* block leaf entries */
683 struct xfs_buf
*bp
; /* block buffer */
684 xfs_dir2_block_tail_t
*btp
; /* block tail */
685 xfs_dir2_data_entry_t
*dep
; /* block data entry */
686 xfs_inode_t
*dp
; /* incore inode */
687 int error
; /* error return value */
688 xfs_dahash_t hash
; /* found hash value */
689 int high
; /* binary search high index */
690 int low
; /* binary search low index */
691 int mid
; /* binary search current idx */
692 xfs_trans_t
*tp
; /* transaction pointer */
693 enum xfs_dacmp cmp
; /* comparison result */
698 error
= xfs_dir3_block_read(tp
, dp
, &bp
);
703 xfs_dir3_data_check(dp
, bp
);
704 btp
= xfs_dir2_block_tail_p(args
->geo
, hdr
);
705 blp
= xfs_dir2_block_leaf_p(btp
);
707 * Loop doing a binary search for our hash value.
708 * Find our entry, ENOENT if it's not there.
710 for (low
= 0, high
= be32_to_cpu(btp
->count
) - 1; ; ) {
712 mid
= (low
+ high
) >> 1;
713 if ((hash
= be32_to_cpu(blp
[mid
].hashval
)) == args
->hashval
)
715 if (hash
< args
->hashval
)
720 ASSERT(args
->op_flags
& XFS_DA_OP_OKNOENT
);
721 xfs_trans_brelse(tp
, bp
);
726 * Back up to the first one with the right hash value.
728 while (mid
> 0 && be32_to_cpu(blp
[mid
- 1].hashval
) == args
->hashval
) {
732 * Now loop forward through all the entries with the
733 * right hash value looking for our name.
736 if ((addr
= be32_to_cpu(blp
[mid
].address
)) == XFS_DIR2_NULL_DATAPTR
)
739 * Get pointer to the entry from the leaf.
741 dep
= (xfs_dir2_data_entry_t
*)
742 ((char *)hdr
+ xfs_dir2_dataptr_to_off(args
->geo
, addr
));
744 * Compare name and if it's an exact match, return the index
745 * and buffer. If it's the first case-insensitive match, store
746 * the index and buffer and continue looking for an exact match.
748 cmp
= xfs_dir2_compname(args
, dep
->name
, dep
->namelen
);
749 if (cmp
!= XFS_CMP_DIFFERENT
&& cmp
!= args
->cmpresult
) {
750 args
->cmpresult
= cmp
;
753 if (cmp
== XFS_CMP_EXACT
)
756 } while (++mid
< be32_to_cpu(btp
->count
) &&
757 be32_to_cpu(blp
[mid
].hashval
) == hash
);
759 ASSERT(args
->op_flags
& XFS_DA_OP_OKNOENT
);
761 * Here, we can only be doing a lookup (not a rename or replace).
762 * If a case-insensitive match was found earlier, return success.
764 if (args
->cmpresult
== XFS_CMP_CASE
)
767 * No match, release the buffer and return ENOENT.
769 xfs_trans_brelse(tp
, bp
);
774 * Remove an entry from a block format directory.
775 * If that makes the block small enough to fit in shortform, transform it.
778 xfs_dir2_block_removename(
779 xfs_da_args_t
*args
) /* directory operation args */
781 xfs_dir2_data_hdr_t
*hdr
; /* block header */
782 xfs_dir2_leaf_entry_t
*blp
; /* block leaf pointer */
783 struct xfs_buf
*bp
; /* block buffer */
784 xfs_dir2_block_tail_t
*btp
; /* block tail */
785 xfs_dir2_data_entry_t
*dep
; /* block data entry */
786 xfs_inode_t
*dp
; /* incore inode */
787 int ent
; /* block leaf entry index */
788 int error
; /* error return value */
789 int needlog
; /* need to log block header */
790 int needscan
; /* need to fixup bestfree */
791 xfs_dir2_sf_hdr_t sfh
; /* shortform header */
792 int size
; /* shortform size */
793 xfs_trans_t
*tp
; /* transaction pointer */
795 trace_xfs_dir2_block_removename(args
);
798 * Look up the entry in the block. Gets the buffer and entry index.
799 * It will always be there, the vnodeops level does a lookup first.
801 if ((error
= xfs_dir2_block_lookup_int(args
, &bp
, &ent
))) {
807 btp
= xfs_dir2_block_tail_p(args
->geo
, hdr
);
808 blp
= xfs_dir2_block_leaf_p(btp
);
810 * Point to the data entry using the leaf entry.
812 dep
= (xfs_dir2_data_entry_t
*)((char *)hdr
+
813 xfs_dir2_dataptr_to_off(args
->geo
,
814 be32_to_cpu(blp
[ent
].address
)));
816 * Mark the data entry's space free.
818 needlog
= needscan
= 0;
819 xfs_dir2_data_make_free(args
, bp
,
820 (xfs_dir2_data_aoff_t
)((char *)dep
- (char *)hdr
),
821 xfs_dir2_data_entsize(dp
->i_mount
, dep
->namelen
), &needlog
,
824 * Fix up the block tail.
826 be32_add_cpu(&btp
->stale
, 1);
827 xfs_dir2_block_log_tail(tp
, bp
);
829 * Remove the leaf entry by marking it stale.
831 blp
[ent
].address
= cpu_to_be32(XFS_DIR2_NULL_DATAPTR
);
832 xfs_dir2_block_log_leaf(tp
, bp
, ent
, ent
);
834 * Fix up bestfree, log the header if necessary.
837 xfs_dir2_data_freescan(dp
->i_mount
, hdr
, &needlog
);
839 xfs_dir2_data_log_header(args
, bp
);
840 xfs_dir3_data_check(dp
, bp
);
842 * See if the size as a shortform is good enough.
844 size
= xfs_dir2_block_sfsize(dp
, hdr
, &sfh
);
845 if (size
> XFS_IFORK_DSIZE(dp
))
849 * If it works, do the conversion.
851 return xfs_dir2_block_to_sf(args
, bp
, size
, &sfh
);
855 * Replace an entry in a V2 block directory.
856 * Change the inode number to the new value.
859 xfs_dir2_block_replace(
860 xfs_da_args_t
*args
) /* directory operation args */
862 xfs_dir2_data_hdr_t
*hdr
; /* block header */
863 xfs_dir2_leaf_entry_t
*blp
; /* block leaf entries */
864 struct xfs_buf
*bp
; /* block buffer */
865 xfs_dir2_block_tail_t
*btp
; /* block tail */
866 xfs_dir2_data_entry_t
*dep
; /* block data entry */
867 xfs_inode_t
*dp
; /* incore inode */
868 int ent
; /* leaf entry index */
869 int error
; /* error return value */
871 trace_xfs_dir2_block_replace(args
);
874 * Lookup the entry in the directory. Get buffer and entry index.
875 * This will always succeed since the caller has already done a lookup.
877 if ((error
= xfs_dir2_block_lookup_int(args
, &bp
, &ent
))) {
882 btp
= xfs_dir2_block_tail_p(args
->geo
, hdr
);
883 blp
= xfs_dir2_block_leaf_p(btp
);
885 * Point to the data entry we need to change.
887 dep
= (xfs_dir2_data_entry_t
*)((char *)hdr
+
888 xfs_dir2_dataptr_to_off(args
->geo
,
889 be32_to_cpu(blp
[ent
].address
)));
890 ASSERT(be64_to_cpu(dep
->inumber
) != args
->inumber
);
892 * Change the inode number to the new value.
894 dep
->inumber
= cpu_to_be64(args
->inumber
);
895 xfs_dir2_data_put_ftype(dp
->i_mount
, dep
, args
->filetype
);
896 xfs_dir2_data_log_entry(args
, bp
, dep
);
897 xfs_dir3_data_check(dp
, bp
);
902 * Qsort comparison routine for the block leaf entries.
904 static int /* sort order */
906 const void *a
, /* first leaf entry */
907 const void *b
) /* second leaf entry */
909 const xfs_dir2_leaf_entry_t
*la
; /* first leaf entry */
910 const xfs_dir2_leaf_entry_t
*lb
; /* second leaf entry */
914 return be32_to_cpu(la
->hashval
) < be32_to_cpu(lb
->hashval
) ? -1 :
915 (be32_to_cpu(la
->hashval
) > be32_to_cpu(lb
->hashval
) ? 1 : 0);
919 * Convert a V2 leaf directory to a V2 block directory if possible.
922 xfs_dir2_leaf_to_block(
923 xfs_da_args_t
*args
, /* operation arguments */
924 struct xfs_buf
*lbp
, /* leaf buffer */
925 struct xfs_buf
*dbp
) /* data buffer */
927 __be16
*bestsp
; /* leaf bests table */
928 xfs_dir2_data_hdr_t
*hdr
; /* block header */
929 xfs_dir2_block_tail_t
*btp
; /* block tail */
930 xfs_inode_t
*dp
; /* incore directory inode */
931 xfs_dir2_data_unused_t
*dup
; /* unused data entry */
932 int error
; /* error return value */
933 int from
; /* leaf from index */
934 xfs_dir2_leaf_t
*leaf
; /* leaf structure */
935 xfs_dir2_leaf_entry_t
*lep
; /* leaf entry */
936 xfs_dir2_leaf_tail_t
*ltp
; /* leaf tail structure */
937 xfs_mount_t
*mp
; /* file system mount point */
938 int needlog
; /* need to log data header */
939 int needscan
; /* need to scan for bestfree */
940 xfs_dir2_sf_hdr_t sfh
; /* shortform header */
941 int size
; /* bytes used */
942 __be16
*tagp
; /* end of entry (tag) */
943 int to
; /* block/leaf to index */
944 xfs_trans_t
*tp
; /* transaction pointer */
945 struct xfs_dir3_icleaf_hdr leafhdr
;
947 trace_xfs_dir2_leaf_to_block(args
);
953 xfs_dir2_leaf_hdr_from_disk(mp
, &leafhdr
, leaf
);
954 ltp
= xfs_dir2_leaf_tail_p(args
->geo
, leaf
);
956 ASSERT(leafhdr
.magic
== XFS_DIR2_LEAF1_MAGIC
||
957 leafhdr
.magic
== XFS_DIR3_LEAF1_MAGIC
);
959 * If there are data blocks other than the first one, take this
960 * opportunity to remove trailing empty data blocks that may have
961 * been left behind during no-space-reservation operations.
962 * These will show up in the leaf bests table.
964 while (dp
->i_d
.di_size
> args
->geo
->blksize
) {
967 hdrsz
= args
->geo
->data_entry_offset
;
968 bestsp
= xfs_dir2_leaf_bests_p(ltp
);
969 if (be16_to_cpu(bestsp
[be32_to_cpu(ltp
->bestcount
) - 1]) ==
970 args
->geo
->blksize
- hdrsz
) {
972 xfs_dir2_leaf_trim_data(args
, lbp
,
973 (xfs_dir2_db_t
)(be32_to_cpu(ltp
->bestcount
) - 1))))
979 * Read the data block if we don't already have it, give up if it fails.
982 error
= xfs_dir3_data_read(tp
, dp
, args
->geo
->datablk
, 0, &dbp
);
987 ASSERT(hdr
->magic
== cpu_to_be32(XFS_DIR2_DATA_MAGIC
) ||
988 hdr
->magic
== cpu_to_be32(XFS_DIR3_DATA_MAGIC
));
991 * Size of the "leaf" area in the block.
993 size
= (uint
)sizeof(xfs_dir2_block_tail_t
) +
994 (uint
)sizeof(*lep
) * (leafhdr
.count
- leafhdr
.stale
);
996 * Look at the last data entry.
998 tagp
= (__be16
*)((char *)hdr
+ args
->geo
->blksize
) - 1;
999 dup
= (xfs_dir2_data_unused_t
*)((char *)hdr
+ be16_to_cpu(*tagp
));
1001 * If it's not free or is too short we can't do it.
1003 if (be16_to_cpu(dup
->freetag
) != XFS_DIR2_DATA_FREE_TAG
||
1004 be16_to_cpu(dup
->length
) < size
)
1008 * Start converting it to block form.
1010 xfs_dir3_block_init(mp
, tp
, dbp
, dp
);
1015 * Use up the space at the end of the block (blp/btp).
1017 error
= xfs_dir2_data_use_free(args
, dbp
, dup
,
1018 args
->geo
->blksize
- size
, size
, &needlog
, &needscan
);
1022 * Initialize the block tail.
1024 btp
= xfs_dir2_block_tail_p(args
->geo
, hdr
);
1025 btp
->count
= cpu_to_be32(leafhdr
.count
- leafhdr
.stale
);
1027 xfs_dir2_block_log_tail(tp
, dbp
);
1029 * Initialize the block leaf area. We compact out stale entries.
1031 lep
= xfs_dir2_block_leaf_p(btp
);
1032 for (from
= to
= 0; from
< leafhdr
.count
; from
++) {
1033 if (leafhdr
.ents
[from
].address
==
1034 cpu_to_be32(XFS_DIR2_NULL_DATAPTR
))
1036 lep
[to
++] = leafhdr
.ents
[from
];
1038 ASSERT(to
== be32_to_cpu(btp
->count
));
1039 xfs_dir2_block_log_leaf(tp
, dbp
, 0, be32_to_cpu(btp
->count
) - 1);
1041 * Scan the bestfree if we need it and log the data block header.
1044 xfs_dir2_data_freescan(dp
->i_mount
, hdr
, &needlog
);
1046 xfs_dir2_data_log_header(args
, dbp
);
1048 * Pitch the old leaf block.
1050 error
= xfs_da_shrink_inode(args
, args
->geo
->leafblk
, lbp
);
1055 * Now see if the resulting block can be shrunken to shortform.
1057 size
= xfs_dir2_block_sfsize(dp
, hdr
, &sfh
);
1058 if (size
> XFS_IFORK_DSIZE(dp
))
1061 return xfs_dir2_block_to_sf(args
, dbp
, size
, &sfh
);
1065 * Convert the shortform directory to block form.
1068 xfs_dir2_sf_to_block(
1069 struct xfs_da_args
*args
)
1071 struct xfs_trans
*tp
= args
->trans
;
1072 struct xfs_inode
*dp
= args
->dp
;
1073 struct xfs_mount
*mp
= dp
->i_mount
;
1074 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(dp
, XFS_DATA_FORK
);
1075 struct xfs_da_geometry
*geo
= args
->geo
;
1076 xfs_dir2_db_t blkno
; /* dir-relative block # (0) */
1077 xfs_dir2_data_hdr_t
*hdr
; /* block header */
1078 xfs_dir2_leaf_entry_t
*blp
; /* block leaf entries */
1079 struct xfs_buf
*bp
; /* block buffer */
1080 xfs_dir2_block_tail_t
*btp
; /* block tail pointer */
1081 xfs_dir2_data_entry_t
*dep
; /* data entry pointer */
1082 int dummy
; /* trash */
1083 xfs_dir2_data_unused_t
*dup
; /* unused entry pointer */
1084 int endoffset
; /* end of data objects */
1085 int error
; /* error return value */
1087 int needlog
; /* need to log block header */
1088 int needscan
; /* need to scan block freespc */
1089 int newoffset
; /* offset from current entry */
1090 unsigned int offset
= geo
->data_entry_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 struct xfs_name name
;
1097 trace_xfs_dir2_sf_to_block(args
);
1099 ASSERT(ifp
->if_flags
& XFS_IFINLINE
);
1100 ASSERT(dp
->i_d
.di_size
>= offsetof(struct xfs_dir2_sf_hdr
, parent
));
1102 oldsfp
= (xfs_dir2_sf_hdr_t
*)ifp
->if_u1
.if_data
;
1104 ASSERT(ifp
->if_bytes
== dp
->i_d
.di_size
);
1105 ASSERT(ifp
->if_u1
.if_data
!= NULL
);
1106 ASSERT(dp
->i_d
.di_size
>= xfs_dir2_sf_hdr_size(oldsfp
->i8count
));
1107 ASSERT(dp
->i_df
.if_nextents
== 0);
1110 * Copy the directory into a temporary buffer.
1111 * Then pitch the incore inode data so we can make extents.
1113 sfp
= kmem_alloc(ifp
->if_bytes
, 0);
1114 memcpy(sfp
, oldsfp
, ifp
->if_bytes
);
1116 xfs_idata_realloc(dp
, -ifp
->if_bytes
, XFS_DATA_FORK
);
1117 xfs_bmap_local_to_extents_empty(tp
, dp
, XFS_DATA_FORK
);
1118 dp
->i_d
.di_size
= 0;
1121 * Add block 0 to the inode.
1123 error
= xfs_dir2_grow_inode(args
, XFS_DIR2_DATA_SPACE
, &blkno
);
1127 * Initialize the data block, then convert it to block format.
1129 error
= xfs_dir3_data_init(args
, blkno
, &bp
);
1132 xfs_dir3_block_init(mp
, tp
, bp
, dp
);
1136 * Compute size of block "tail" area.
1138 i
= (uint
)sizeof(*btp
) +
1139 (sfp
->count
+ 2) * (uint
)sizeof(xfs_dir2_leaf_entry_t
);
1141 * The whole thing is initialized to free by the init routine.
1142 * Say we're using the leaf and tail area.
1144 dup
= bp
->b_addr
+ offset
;
1145 needlog
= needscan
= 0;
1146 error
= xfs_dir2_data_use_free(args
, bp
, dup
, args
->geo
->blksize
- i
,
1147 i
, &needlog
, &needscan
);
1150 ASSERT(needscan
== 0);
1154 btp
= xfs_dir2_block_tail_p(args
->geo
, hdr
);
1155 btp
->count
= cpu_to_be32(sfp
->count
+ 2); /* ., .. */
1157 blp
= xfs_dir2_block_leaf_p(btp
);
1158 endoffset
= (uint
)((char *)blp
- (char *)hdr
);
1160 * Remove the freespace, we'll manage it.
1162 error
= xfs_dir2_data_use_free(args
, bp
, dup
,
1163 (xfs_dir2_data_aoff_t
)((char *)dup
- (char *)hdr
),
1164 be16_to_cpu(dup
->length
), &needlog
, &needscan
);
1169 * Create entry for .
1171 dep
= bp
->b_addr
+ offset
;
1172 dep
->inumber
= cpu_to_be64(dp
->i_ino
);
1175 xfs_dir2_data_put_ftype(mp
, dep
, XFS_DIR3_FT_DIR
);
1176 tagp
= xfs_dir2_data_entry_tag_p(mp
, dep
);
1177 *tagp
= cpu_to_be16(offset
);
1178 xfs_dir2_data_log_entry(args
, bp
, dep
);
1179 blp
[0].hashval
= cpu_to_be32(xfs_dir_hash_dot
);
1180 blp
[0].address
= cpu_to_be32(xfs_dir2_byte_to_dataptr(offset
));
1181 offset
+= xfs_dir2_data_entsize(mp
, dep
->namelen
);
1184 * Create entry for ..
1186 dep
= bp
->b_addr
+ offset
;
1187 dep
->inumber
= cpu_to_be64(xfs_dir2_sf_get_parent_ino(sfp
));
1189 dep
->name
[0] = dep
->name
[1] = '.';
1190 xfs_dir2_data_put_ftype(mp
, dep
, XFS_DIR3_FT_DIR
);
1191 tagp
= xfs_dir2_data_entry_tag_p(mp
, dep
);
1192 *tagp
= cpu_to_be16(offset
);
1193 xfs_dir2_data_log_entry(args
, bp
, dep
);
1194 blp
[1].hashval
= cpu_to_be32(xfs_dir_hash_dotdot
);
1195 blp
[1].address
= cpu_to_be32(xfs_dir2_byte_to_dataptr(offset
));
1196 offset
+= xfs_dir2_data_entsize(mp
, dep
->namelen
);
1199 * Loop over existing entries, stuff them in.
1205 sfep
= xfs_dir2_sf_firstentry(sfp
);
1208 * Need to preserve the existing offset values in the sf directory.
1209 * Insert holes (unused entries) where necessary.
1211 while (offset
< endoffset
) {
1213 * sfep is null when we reach the end of the list.
1216 newoffset
= endoffset
;
1218 newoffset
= xfs_dir2_sf_get_offset(sfep
);
1220 * There should be a hole here, make one.
1222 if (offset
< newoffset
) {
1223 dup
= bp
->b_addr
+ offset
;
1224 dup
->freetag
= cpu_to_be16(XFS_DIR2_DATA_FREE_TAG
);
1225 dup
->length
= cpu_to_be16(newoffset
- offset
);
1226 *xfs_dir2_data_unused_tag_p(dup
) = cpu_to_be16(offset
);
1227 xfs_dir2_data_log_unused(args
, bp
, dup
);
1228 xfs_dir2_data_freeinsert(hdr
,
1229 xfs_dir2_data_bestfree_p(mp
, hdr
),
1231 offset
+= be16_to_cpu(dup
->length
);
1235 * Copy a real entry.
1237 dep
= bp
->b_addr
+ newoffset
;
1238 dep
->inumber
= cpu_to_be64(xfs_dir2_sf_get_ino(mp
, sfp
, sfep
));
1239 dep
->namelen
= sfep
->namelen
;
1240 xfs_dir2_data_put_ftype(mp
, dep
,
1241 xfs_dir2_sf_get_ftype(mp
, sfep
));
1242 memcpy(dep
->name
, sfep
->name
, dep
->namelen
);
1243 tagp
= xfs_dir2_data_entry_tag_p(mp
, dep
);
1244 *tagp
= cpu_to_be16(newoffset
);
1245 xfs_dir2_data_log_entry(args
, bp
, dep
);
1246 name
.name
= sfep
->name
;
1247 name
.len
= sfep
->namelen
;
1248 blp
[2 + i
].hashval
= cpu_to_be32(xfs_dir2_hashname(mp
, &name
));
1249 blp
[2 + i
].address
=
1250 cpu_to_be32(xfs_dir2_byte_to_dataptr(newoffset
));
1251 offset
= (int)((char *)(tagp
+ 1) - (char *)hdr
);
1252 if (++i
== sfp
->count
)
1255 sfep
= xfs_dir2_sf_nextentry(mp
, sfp
, sfep
);
1257 /* Done with the temporary buffer */
1260 * Sort the leaf entries by hash value.
1262 xfs_sort(blp
, be32_to_cpu(btp
->count
), sizeof(*blp
), xfs_dir2_block_sort
);
1264 * Log the leaf entry area and tail.
1265 * Already logged the header in data_init, ignore needlog.
1267 ASSERT(needscan
== 0);
1268 xfs_dir2_block_log_leaf(tp
, bp
, 0, be32_to_cpu(btp
->count
) - 1);
1269 xfs_dir2_block_log_tail(tp
, bp
);
1270 xfs_dir3_data_check(dp
, bp
);