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"
20 #include "xfs_health.h"
23 * Local function prototypes.
25 static void xfs_dir2_block_log_leaf(xfs_trans_t
*tp
, struct xfs_buf
*bp
,
27 static void xfs_dir2_block_log_tail(xfs_trans_t
*tp
, struct xfs_buf
*bp
);
28 static int xfs_dir2_block_lookup_int(xfs_da_args_t
*args
, struct xfs_buf
**bpp
,
30 static int xfs_dir2_block_sort(const void *a
, const void *b
);
32 static xfs_dahash_t xfs_dir_hash_dot
, xfs_dir_hash_dotdot
;
35 * One-time startup routine called from xfs_init().
40 xfs_dir_hash_dot
= xfs_da_hashname((unsigned char *)".", 1);
41 xfs_dir_hash_dotdot
= xfs_da_hashname((unsigned char *)"..", 2);
45 xfs_dir3_block_verify(
48 struct xfs_mount
*mp
= bp
->b_mount
;
49 struct xfs_dir3_blk_hdr
*hdr3
= bp
->b_addr
;
51 if (!xfs_verify_magic(bp
, hdr3
->magic
))
52 return __this_address
;
54 if (xfs_has_crc(mp
)) {
55 if (!uuid_equal(&hdr3
->uuid
, &mp
->m_sb
.sb_meta_uuid
))
56 return __this_address
;
57 if (be64_to_cpu(hdr3
->blkno
) != xfs_buf_daddr(bp
))
58 return __this_address
;
59 if (!xfs_log_check_lsn(mp
, be64_to_cpu(hdr3
->lsn
)))
60 return __this_address
;
62 return __xfs_dir3_data_check(NULL
, bp
);
66 xfs_dir3_block_read_verify(
69 struct xfs_mount
*mp
= bp
->b_mount
;
72 if (xfs_has_crc(mp
) &&
73 !xfs_buf_verify_cksum(bp
, XFS_DIR3_DATA_CRC_OFF
))
74 xfs_verifier_error(bp
, -EFSBADCRC
, __this_address
);
76 fa
= xfs_dir3_block_verify(bp
);
78 xfs_verifier_error(bp
, -EFSCORRUPTED
, fa
);
83 xfs_dir3_block_write_verify(
86 struct xfs_mount
*mp
= bp
->b_mount
;
87 struct xfs_buf_log_item
*bip
= bp
->b_log_item
;
88 struct xfs_dir3_blk_hdr
*hdr3
= bp
->b_addr
;
91 fa
= xfs_dir3_block_verify(bp
);
93 xfs_verifier_error(bp
, -EFSCORRUPTED
, fa
);
101 hdr3
->lsn
= cpu_to_be64(bip
->bli_item
.li_lsn
);
103 xfs_buf_update_cksum(bp
, XFS_DIR3_DATA_CRC_OFF
);
106 const struct xfs_buf_ops xfs_dir3_block_buf_ops
= {
107 .name
= "xfs_dir3_block",
108 .magic
= { cpu_to_be32(XFS_DIR2_BLOCK_MAGIC
),
109 cpu_to_be32(XFS_DIR3_BLOCK_MAGIC
) },
110 .verify_read
= xfs_dir3_block_read_verify
,
111 .verify_write
= xfs_dir3_block_write_verify
,
112 .verify_struct
= xfs_dir3_block_verify
,
116 xfs_dir3_block_header_check(
120 struct xfs_mount
*mp
= bp
->b_mount
;
122 if (xfs_has_crc(mp
)) {
123 struct xfs_dir3_blk_hdr
*hdr3
= bp
->b_addr
;
125 if (hdr3
->magic
!= cpu_to_be32(XFS_DIR3_BLOCK_MAGIC
))
126 return __this_address
;
128 if (be64_to_cpu(hdr3
->owner
) != owner
)
129 return __this_address
;
137 struct xfs_trans
*tp
,
138 struct xfs_inode
*dp
,
140 struct xfs_buf
**bpp
)
142 struct xfs_mount
*mp
= dp
->i_mount
;
146 err
= xfs_da_read_buf(tp
, dp
, mp
->m_dir_geo
->datablk
, 0, bpp
,
147 XFS_DATA_FORK
, &xfs_dir3_block_buf_ops
);
151 /* Check things that we can't do in the verifier. */
152 fa
= xfs_dir3_block_header_check(*bpp
, owner
);
154 __xfs_buf_mark_corrupt(*bpp
, fa
);
155 xfs_trans_brelse(tp
, *bpp
);
157 xfs_dirattr_mark_sick(dp
, XFS_DATA_FORK
);
158 return -EFSCORRUPTED
;
161 xfs_trans_buf_set_type(tp
, *bpp
, XFS_BLFT_DIR_BLOCK_BUF
);
167 struct xfs_da_args
*args
,
170 struct xfs_trans
*tp
= args
->trans
;
171 struct xfs_inode
*dp
= args
->dp
;
172 struct xfs_mount
*mp
= dp
->i_mount
;
173 struct xfs_dir3_blk_hdr
*hdr3
= bp
->b_addr
;
175 bp
->b_ops
= &xfs_dir3_block_buf_ops
;
176 xfs_trans_buf_set_type(tp
, bp
, XFS_BLFT_DIR_BLOCK_BUF
);
178 if (xfs_has_crc(mp
)) {
179 memset(hdr3
, 0, sizeof(*hdr3
));
180 hdr3
->magic
= cpu_to_be32(XFS_DIR3_BLOCK_MAGIC
);
181 hdr3
->blkno
= cpu_to_be64(xfs_buf_daddr(bp
));
182 hdr3
->owner
= cpu_to_be64(args
->owner
);
183 uuid_copy(&hdr3
->uuid
, &mp
->m_sb
.sb_meta_uuid
);
187 hdr3
->magic
= cpu_to_be32(XFS_DIR2_BLOCK_MAGIC
);
191 xfs_dir2_block_need_space(
192 struct xfs_inode
*dp
,
193 struct xfs_dir2_data_hdr
*hdr
,
194 struct xfs_dir2_block_tail
*btp
,
195 struct xfs_dir2_leaf_entry
*blp
,
197 struct xfs_dir2_data_unused
**dupp
,
198 struct xfs_dir2_data_unused
**enddupp
,
202 struct xfs_dir2_data_free
*bf
;
204 struct xfs_dir2_data_unused
*dup
= NULL
;
205 struct xfs_dir2_data_unused
*enddup
= NULL
;
208 bf
= xfs_dir2_data_bestfree_p(dp
->i_mount
, hdr
);
211 * If there are stale entries we'll use one for the leaf.
214 if (be16_to_cpu(bf
[0].length
) >= len
) {
216 * The biggest entry enough to avoid compaction.
218 dup
= (xfs_dir2_data_unused_t
*)
219 ((char *)hdr
+ be16_to_cpu(bf
[0].offset
));
224 * Will need to compact to make this work.
225 * Tag just before the first leaf entry.
228 tagp
= (__be16
*)blp
- 1;
230 /* Data object just before the first leaf entry. */
231 dup
= (xfs_dir2_data_unused_t
*)((char *)hdr
+ be16_to_cpu(*tagp
));
234 * If it's not free then the data will go where the
235 * leaf data starts now, if it works at all.
237 if (be16_to_cpu(dup
->freetag
) == XFS_DIR2_DATA_FREE_TAG
) {
238 if (be16_to_cpu(dup
->length
) + (be32_to_cpu(btp
->stale
) - 1) *
239 (uint
)sizeof(*blp
) < len
)
241 } else if ((be32_to_cpu(btp
->stale
) - 1) * (uint
)sizeof(*blp
) < len
)
244 dup
= (xfs_dir2_data_unused_t
*)blp
;
249 * no stale entries, so just use free space.
250 * Tag just before the first leaf entry.
252 tagp
= (__be16
*)blp
- 1;
254 /* Data object just before the first leaf entry. */
255 enddup
= (xfs_dir2_data_unused_t
*)((char *)hdr
+ be16_to_cpu(*tagp
));
258 * If it's not free then can't do this add without cleaning up:
259 * the space before the first leaf entry needs to be free so it
260 * can be expanded to hold the pointer to the new entry.
262 if (be16_to_cpu(enddup
->freetag
) == XFS_DIR2_DATA_FREE_TAG
) {
264 * Check out the biggest freespace and see if it's the same one.
266 dup
= (xfs_dir2_data_unused_t
*)
267 ((char *)hdr
+ be16_to_cpu(bf
[0].offset
));
270 * Not the same free entry, just check its length.
272 if (be16_to_cpu(dup
->length
) < len
)
278 * It is the biggest freespace, can it hold the leaf too?
280 if (be16_to_cpu(dup
->length
) < len
+ (uint
)sizeof(*blp
)) {
282 * Yes, use the second-largest entry instead if it works.
284 if (be16_to_cpu(bf
[1].length
) >= len
)
285 dup
= (xfs_dir2_data_unused_t
*)
286 ((char *)hdr
+ be16_to_cpu(bf
[1].offset
));
298 * compact the leaf entries.
299 * Leave the highest-numbered stale entry stale.
300 * XXX should be the one closest to mid but mid is not yet computed.
303 xfs_dir2_block_compact(
304 struct xfs_da_args
*args
,
306 struct xfs_dir2_data_hdr
*hdr
,
307 struct xfs_dir2_block_tail
*btp
,
308 struct xfs_dir2_leaf_entry
*blp
,
313 int fromidx
; /* source leaf index */
314 int toidx
; /* target leaf index */
316 int highstale
; /* high stale index */
318 fromidx
= toidx
= be32_to_cpu(btp
->count
) - 1;
319 highstale
= *lfloghigh
= -1;
320 for (; fromidx
>= 0; fromidx
--) {
321 if (blp
[fromidx
].address
== cpu_to_be32(XFS_DIR2_NULL_DATAPTR
)) {
325 if (*lfloghigh
== -1)
331 blp
[toidx
] = blp
[fromidx
];
334 *lfloglow
= toidx
+ 1 - (be32_to_cpu(btp
->stale
) - 1);
335 *lfloghigh
-= be32_to_cpu(btp
->stale
) - 1;
336 be32_add_cpu(&btp
->count
, -(be32_to_cpu(btp
->stale
) - 1));
337 xfs_dir2_data_make_free(args
, bp
,
338 (xfs_dir2_data_aoff_t
)((char *)blp
- (char *)hdr
),
339 (xfs_dir2_data_aoff_t
)((be32_to_cpu(btp
->stale
) - 1) * sizeof(*blp
)),
341 btp
->stale
= cpu_to_be32(1);
343 * If we now need to rebuild the bestfree map, do so.
344 * This needs to happen before the next call to use_free.
347 xfs_dir2_data_freescan(args
->dp
->i_mount
, hdr
, needlog
);
351 * Add an entry to a block directory.
354 xfs_dir2_block_addname(
355 xfs_da_args_t
*args
) /* directory op arguments */
357 xfs_dir2_data_hdr_t
*hdr
; /* block header */
358 xfs_dir2_leaf_entry_t
*blp
; /* block leaf entries */
359 struct xfs_buf
*bp
; /* buffer for block */
360 xfs_dir2_block_tail_t
*btp
; /* block tail */
361 int compact
; /* need to compact leaf ents */
362 xfs_dir2_data_entry_t
*dep
; /* block data entry */
363 xfs_inode_t
*dp
; /* directory inode */
364 xfs_dir2_data_unused_t
*dup
; /* block unused entry */
365 int error
; /* error return value */
366 xfs_dir2_data_unused_t
*enddup
=NULL
; /* unused at end of data */
367 xfs_dahash_t hash
; /* hash value of found entry */
368 int high
; /* high index for binary srch */
369 int highstale
; /* high stale index */
370 int lfloghigh
=0; /* last final leaf to log */
371 int lfloglow
=0; /* first final leaf to log */
372 int len
; /* length of the new entry */
373 int low
; /* low index for binary srch */
374 int lowstale
; /* low stale index */
375 int mid
=0; /* midpoint for binary srch */
376 int needlog
; /* need to log header */
377 int needscan
; /* need to rescan freespace */
378 __be16
*tagp
; /* pointer to tag value */
379 xfs_trans_t
*tp
; /* transaction structure */
381 trace_xfs_dir2_block_addname(args
);
386 /* Read the (one and only) directory block into bp. */
387 error
= xfs_dir3_block_read(tp
, dp
, args
->owner
, &bp
);
391 len
= xfs_dir2_data_entsize(dp
->i_mount
, args
->namelen
);
394 * Set up pointers to parts of the block.
397 btp
= xfs_dir2_block_tail_p(args
->geo
, hdr
);
398 blp
= xfs_dir2_block_leaf_p(btp
);
401 * Find out if we can reuse stale entries or whether we need extra
402 * space for entry and new leaf.
404 xfs_dir2_block_need_space(dp
, hdr
, btp
, blp
, &tagp
, &dup
,
405 &enddup
, &compact
, len
);
408 * Done everything we need for a space check now.
410 if (args
->op_flags
& XFS_DA_OP_JUSTCHECK
) {
411 xfs_trans_brelse(tp
, bp
);
418 * If we don't have space for the new entry & leaf ...
421 /* Don't have a space reservation: return no-space. */
422 if (args
->total
== 0)
425 * Convert to the next larger format.
426 * Then add the new entry in that format.
428 error
= xfs_dir2_block_to_leaf(args
, bp
);
431 return xfs_dir2_leaf_addname(args
);
434 needlog
= needscan
= 0;
437 * If need to compact the leaf entries, do it now.
440 xfs_dir2_block_compact(args
, bp
, hdr
, btp
, blp
, &needlog
,
441 &lfloghigh
, &lfloglow
);
442 /* recalculate blp post-compaction */
443 blp
= xfs_dir2_block_leaf_p(btp
);
444 } else if (btp
->stale
) {
446 * Set leaf logging boundaries to impossible state.
447 * For the no-stale case they're set explicitly.
449 lfloglow
= be32_to_cpu(btp
->count
);
454 * Find the slot that's first lower than our hash value, -1 if none.
456 for (low
= 0, high
= be32_to_cpu(btp
->count
) - 1; low
<= high
; ) {
457 mid
= (low
+ high
) >> 1;
458 if ((hash
= be32_to_cpu(blp
[mid
].hashval
)) == args
->hashval
)
460 if (hash
< args
->hashval
)
465 while (mid
>= 0 && be32_to_cpu(blp
[mid
].hashval
) >= args
->hashval
) {
469 * No stale entries, will use enddup space to hold new leaf.
472 xfs_dir2_data_aoff_t aoff
;
475 * Mark the space needed for the new leaf entry, now in use.
477 aoff
= (xfs_dir2_data_aoff_t
)((char *)enddup
- (char *)hdr
+
478 be16_to_cpu(enddup
->length
) - sizeof(*blp
));
479 error
= xfs_dir2_data_use_free(args
, bp
, enddup
, aoff
,
480 (xfs_dir2_data_aoff_t
)sizeof(*blp
), &needlog
,
486 * Update the tail (entry count).
488 be32_add_cpu(&btp
->count
, 1);
490 * If we now need to rebuild the bestfree map, do so.
491 * This needs to happen before the next call to use_free.
494 xfs_dir2_data_freescan(dp
->i_mount
, hdr
, &needlog
);
498 * Adjust pointer to the first leaf entry, we're about to move
499 * the table up one to open up space for the new leaf entry.
500 * Then adjust our index to match.
505 memmove(blp
, &blp
[1], mid
* sizeof(*blp
));
510 * Use a stale leaf for our new entry.
515 blp
[lowstale
].address
!=
516 cpu_to_be32(XFS_DIR2_NULL_DATAPTR
);
519 for (highstale
= mid
+ 1;
520 highstale
< be32_to_cpu(btp
->count
) &&
521 blp
[highstale
].address
!=
522 cpu_to_be32(XFS_DIR2_NULL_DATAPTR
) &&
523 (lowstale
< 0 || mid
- lowstale
> highstale
- mid
);
527 * Move entries toward the low-numbered stale entry.
530 (highstale
== be32_to_cpu(btp
->count
) ||
531 mid
- lowstale
<= highstale
- mid
)) {
533 memmove(&blp
[lowstale
], &blp
[lowstale
+ 1],
534 (mid
- lowstale
) * sizeof(*blp
));
535 lfloglow
= min(lowstale
, lfloglow
);
536 lfloghigh
= max(mid
, lfloghigh
);
539 * Move entries toward the high-numbered stale entry.
542 ASSERT(highstale
< be32_to_cpu(btp
->count
));
545 memmove(&blp
[mid
+ 1], &blp
[mid
],
546 (highstale
- mid
) * sizeof(*blp
));
547 lfloglow
= min(mid
, lfloglow
);
548 lfloghigh
= max(highstale
, lfloghigh
);
550 be32_add_cpu(&btp
->stale
, -1);
553 * Point to the new data entry.
555 dep
= (xfs_dir2_data_entry_t
*)dup
;
557 * Fill in the leaf entry.
559 blp
[mid
].hashval
= cpu_to_be32(args
->hashval
);
560 blp
[mid
].address
= cpu_to_be32(xfs_dir2_byte_to_dataptr(
561 (char *)dep
- (char *)hdr
));
562 xfs_dir2_block_log_leaf(tp
, bp
, lfloglow
, lfloghigh
);
564 * Mark space for the data entry used.
566 error
= xfs_dir2_data_use_free(args
, bp
, dup
,
567 (xfs_dir2_data_aoff_t
)((char *)dup
- (char *)hdr
),
568 (xfs_dir2_data_aoff_t
)len
, &needlog
, &needscan
);
572 * Create the new data entry.
574 dep
->inumber
= cpu_to_be64(args
->inumber
);
575 dep
->namelen
= args
->namelen
;
576 memcpy(dep
->name
, args
->name
, args
->namelen
);
577 xfs_dir2_data_put_ftype(dp
->i_mount
, dep
, args
->filetype
);
578 tagp
= xfs_dir2_data_entry_tag_p(dp
->i_mount
, dep
);
579 *tagp
= cpu_to_be16((char *)dep
- (char *)hdr
);
581 * Clean up the bestfree array and log the header, tail, and entry.
584 xfs_dir2_data_freescan(dp
->i_mount
, hdr
, &needlog
);
586 xfs_dir2_data_log_header(args
, bp
);
587 xfs_dir2_block_log_tail(tp
, bp
);
588 xfs_dir2_data_log_entry(args
, bp
, dep
);
589 xfs_dir3_data_check(dp
, bp
);
594 * Log leaf entries from the block.
597 xfs_dir2_block_log_leaf(
598 xfs_trans_t
*tp
, /* transaction structure */
599 struct xfs_buf
*bp
, /* block buffer */
600 int first
, /* index of first logged leaf */
601 int last
) /* index of last logged leaf */
603 xfs_dir2_data_hdr_t
*hdr
= bp
->b_addr
;
604 xfs_dir2_leaf_entry_t
*blp
;
605 xfs_dir2_block_tail_t
*btp
;
607 btp
= xfs_dir2_block_tail_p(tp
->t_mountp
->m_dir_geo
, hdr
);
608 blp
= xfs_dir2_block_leaf_p(btp
);
609 xfs_trans_log_buf(tp
, bp
, (uint
)((char *)&blp
[first
] - (char *)hdr
),
610 (uint
)((char *)&blp
[last
+ 1] - (char *)hdr
- 1));
614 * Log the block tail.
617 xfs_dir2_block_log_tail(
618 xfs_trans_t
*tp
, /* transaction structure */
619 struct xfs_buf
*bp
) /* block buffer */
621 xfs_dir2_data_hdr_t
*hdr
= bp
->b_addr
;
622 xfs_dir2_block_tail_t
*btp
;
624 btp
= xfs_dir2_block_tail_p(tp
->t_mountp
->m_dir_geo
, hdr
);
625 xfs_trans_log_buf(tp
, bp
, (uint
)((char *)btp
- (char *)hdr
),
626 (uint
)((char *)(btp
+ 1) - (char *)hdr
- 1));
630 * Look up an entry in the block. This is the external routine,
631 * xfs_dir2_block_lookup_int does the real work.
634 xfs_dir2_block_lookup(
635 xfs_da_args_t
*args
) /* dir lookup arguments */
637 xfs_dir2_data_hdr_t
*hdr
; /* block header */
638 xfs_dir2_leaf_entry_t
*blp
; /* block leaf entries */
639 struct xfs_buf
*bp
; /* block buffer */
640 xfs_dir2_block_tail_t
*btp
; /* block tail */
641 xfs_dir2_data_entry_t
*dep
; /* block data entry */
642 xfs_inode_t
*dp
; /* incore inode */
643 int ent
; /* entry index */
644 int error
; /* error return value */
646 trace_xfs_dir2_block_lookup(args
);
649 * Get the buffer, look up the entry.
650 * If not found (ENOENT) then return, have no buffer.
652 if ((error
= xfs_dir2_block_lookup_int(args
, &bp
, &ent
)))
656 xfs_dir3_data_check(dp
, bp
);
657 btp
= xfs_dir2_block_tail_p(args
->geo
, hdr
);
658 blp
= xfs_dir2_block_leaf_p(btp
);
660 * Get the offset from the leaf entry, to point to the data.
662 dep
= (xfs_dir2_data_entry_t
*)((char *)hdr
+
663 xfs_dir2_dataptr_to_off(args
->geo
,
664 be32_to_cpu(blp
[ent
].address
)));
666 * Fill in inode number, CI name if appropriate, release the block.
668 args
->inumber
= be64_to_cpu(dep
->inumber
);
669 args
->filetype
= xfs_dir2_data_get_ftype(dp
->i_mount
, dep
);
670 error
= xfs_dir_cilookup_result(args
, dep
->name
, dep
->namelen
);
671 xfs_trans_brelse(args
->trans
, bp
);
676 * Internal block lookup routine.
678 static int /* error */
679 xfs_dir2_block_lookup_int(
680 xfs_da_args_t
*args
, /* dir lookup arguments */
681 struct xfs_buf
**bpp
, /* returned block buffer */
682 int *entno
) /* returned entry number */
684 xfs_dir2_dataptr_t addr
; /* data entry address */
685 xfs_dir2_data_hdr_t
*hdr
; /* block header */
686 xfs_dir2_leaf_entry_t
*blp
; /* block leaf entries */
687 struct xfs_buf
*bp
; /* block buffer */
688 xfs_dir2_block_tail_t
*btp
; /* block tail */
689 xfs_dir2_data_entry_t
*dep
; /* block data entry */
690 xfs_inode_t
*dp
; /* incore inode */
691 int error
; /* error return value */
692 xfs_dahash_t hash
; /* found hash value */
693 int high
; /* binary search high index */
694 int low
; /* binary search low index */
695 int mid
; /* binary search current idx */
696 xfs_trans_t
*tp
; /* transaction pointer */
697 enum xfs_dacmp cmp
; /* comparison result */
702 error
= xfs_dir3_block_read(tp
, dp
, args
->owner
, &bp
);
707 xfs_dir3_data_check(dp
, bp
);
708 btp
= xfs_dir2_block_tail_p(args
->geo
, hdr
);
709 blp
= xfs_dir2_block_leaf_p(btp
);
711 * Loop doing a binary search for our hash value.
712 * Find our entry, ENOENT if it's not there.
714 for (low
= 0, high
= be32_to_cpu(btp
->count
) - 1; ; ) {
716 mid
= (low
+ high
) >> 1;
717 if ((hash
= be32_to_cpu(blp
[mid
].hashval
)) == args
->hashval
)
719 if (hash
< args
->hashval
)
724 ASSERT(args
->op_flags
& XFS_DA_OP_OKNOENT
);
725 xfs_trans_brelse(tp
, bp
);
730 * Back up to the first one with the right hash value.
732 while (mid
> 0 && be32_to_cpu(blp
[mid
- 1].hashval
) == args
->hashval
) {
736 * Now loop forward through all the entries with the
737 * right hash value looking for our name.
740 if ((addr
= be32_to_cpu(blp
[mid
].address
)) == XFS_DIR2_NULL_DATAPTR
)
743 * Get pointer to the entry from the leaf.
745 dep
= (xfs_dir2_data_entry_t
*)
746 ((char *)hdr
+ xfs_dir2_dataptr_to_off(args
->geo
, addr
));
748 * Compare name and if it's an exact match, return the index
749 * and buffer. If it's the first case-insensitive match, store
750 * the index and buffer and continue looking for an exact match.
752 cmp
= xfs_dir2_compname(args
, dep
->name
, dep
->namelen
);
753 if (cmp
!= XFS_CMP_DIFFERENT
&& cmp
!= args
->cmpresult
) {
754 args
->cmpresult
= cmp
;
757 if (cmp
== XFS_CMP_EXACT
)
760 } while (++mid
< be32_to_cpu(btp
->count
) &&
761 be32_to_cpu(blp
[mid
].hashval
) == hash
);
763 ASSERT(args
->op_flags
& XFS_DA_OP_OKNOENT
);
765 * Here, we can only be doing a lookup (not a rename or replace).
766 * If a case-insensitive match was found earlier, return success.
768 if (args
->cmpresult
== XFS_CMP_CASE
)
771 * No match, release the buffer and return ENOENT.
773 xfs_trans_brelse(tp
, bp
);
778 * Remove an entry from a block format directory.
779 * If that makes the block small enough to fit in shortform, transform it.
782 xfs_dir2_block_removename(
783 xfs_da_args_t
*args
) /* directory operation args */
785 xfs_dir2_data_hdr_t
*hdr
; /* block header */
786 xfs_dir2_leaf_entry_t
*blp
; /* block leaf pointer */
787 struct xfs_buf
*bp
; /* block buffer */
788 xfs_dir2_block_tail_t
*btp
; /* block tail */
789 xfs_dir2_data_entry_t
*dep
; /* block data entry */
790 xfs_inode_t
*dp
; /* incore inode */
791 int ent
; /* block leaf entry index */
792 int error
; /* error return value */
793 int needlog
; /* need to log block header */
794 int needscan
; /* need to fixup bestfree */
795 xfs_dir2_sf_hdr_t sfh
; /* shortform header */
796 int size
; /* shortform size */
797 xfs_trans_t
*tp
; /* transaction pointer */
799 trace_xfs_dir2_block_removename(args
);
802 * Look up the entry in the block. Gets the buffer and entry index.
803 * It will always be there, the vnodeops level does a lookup first.
805 if ((error
= xfs_dir2_block_lookup_int(args
, &bp
, &ent
))) {
811 btp
= xfs_dir2_block_tail_p(args
->geo
, hdr
);
812 blp
= xfs_dir2_block_leaf_p(btp
);
814 * Point to the data entry using the leaf entry.
816 dep
= (xfs_dir2_data_entry_t
*)((char *)hdr
+
817 xfs_dir2_dataptr_to_off(args
->geo
,
818 be32_to_cpu(blp
[ent
].address
)));
820 * Mark the data entry's space free.
822 needlog
= needscan
= 0;
823 xfs_dir2_data_make_free(args
, bp
,
824 (xfs_dir2_data_aoff_t
)((char *)dep
- (char *)hdr
),
825 xfs_dir2_data_entsize(dp
->i_mount
, dep
->namelen
), &needlog
,
828 * Fix up the block tail.
830 be32_add_cpu(&btp
->stale
, 1);
831 xfs_dir2_block_log_tail(tp
, bp
);
833 * Remove the leaf entry by marking it stale.
835 blp
[ent
].address
= cpu_to_be32(XFS_DIR2_NULL_DATAPTR
);
836 xfs_dir2_block_log_leaf(tp
, bp
, ent
, ent
);
838 * Fix up bestfree, log the header if necessary.
841 xfs_dir2_data_freescan(dp
->i_mount
, hdr
, &needlog
);
843 xfs_dir2_data_log_header(args
, bp
);
844 xfs_dir3_data_check(dp
, bp
);
846 * See if the size as a shortform is good enough.
848 size
= xfs_dir2_block_sfsize(dp
, hdr
, &sfh
);
849 if (size
> xfs_inode_data_fork_size(dp
))
853 * If it works, do the conversion.
855 return xfs_dir2_block_to_sf(args
, bp
, size
, &sfh
);
859 * Replace an entry in a V2 block directory.
860 * Change the inode number to the new value.
863 xfs_dir2_block_replace(
864 xfs_da_args_t
*args
) /* directory operation args */
866 xfs_dir2_data_hdr_t
*hdr
; /* block header */
867 xfs_dir2_leaf_entry_t
*blp
; /* block leaf entries */
868 struct xfs_buf
*bp
; /* block buffer */
869 xfs_dir2_block_tail_t
*btp
; /* block tail */
870 xfs_dir2_data_entry_t
*dep
; /* block data entry */
871 xfs_inode_t
*dp
; /* incore inode */
872 int ent
; /* leaf entry index */
873 int error
; /* error return value */
875 trace_xfs_dir2_block_replace(args
);
878 * Lookup the entry in the directory. Get buffer and entry index.
879 * This will always succeed since the caller has already done a lookup.
881 if ((error
= xfs_dir2_block_lookup_int(args
, &bp
, &ent
))) {
886 btp
= xfs_dir2_block_tail_p(args
->geo
, hdr
);
887 blp
= xfs_dir2_block_leaf_p(btp
);
889 * Point to the data entry we need to change.
891 dep
= (xfs_dir2_data_entry_t
*)((char *)hdr
+
892 xfs_dir2_dataptr_to_off(args
->geo
,
893 be32_to_cpu(blp
[ent
].address
)));
894 ASSERT(be64_to_cpu(dep
->inumber
) != args
->inumber
);
896 * Change the inode number to the new value.
898 dep
->inumber
= cpu_to_be64(args
->inumber
);
899 xfs_dir2_data_put_ftype(dp
->i_mount
, dep
, args
->filetype
);
900 xfs_dir2_data_log_entry(args
, bp
, dep
);
901 xfs_dir3_data_check(dp
, bp
);
906 * Qsort comparison routine for the block leaf entries.
908 static int /* sort order */
910 const void *a
, /* first leaf entry */
911 const void *b
) /* second leaf entry */
913 const xfs_dir2_leaf_entry_t
*la
; /* first leaf entry */
914 const xfs_dir2_leaf_entry_t
*lb
; /* second leaf entry */
918 return be32_to_cpu(la
->hashval
) < be32_to_cpu(lb
->hashval
) ? -1 :
919 (be32_to_cpu(la
->hashval
) > be32_to_cpu(lb
->hashval
) ? 1 : 0);
923 * Convert a V2 leaf directory to a V2 block directory if possible.
926 xfs_dir2_leaf_to_block(
927 xfs_da_args_t
*args
, /* operation arguments */
928 struct xfs_buf
*lbp
, /* leaf buffer */
929 struct xfs_buf
*dbp
) /* data buffer */
931 __be16
*bestsp
; /* leaf bests table */
932 xfs_dir2_data_hdr_t
*hdr
; /* block header */
933 xfs_dir2_block_tail_t
*btp
; /* block tail */
934 xfs_inode_t
*dp
; /* incore directory inode */
935 xfs_dir2_data_unused_t
*dup
; /* unused data entry */
936 int error
; /* error return value */
937 int from
; /* leaf from index */
938 xfs_dir2_leaf_t
*leaf
; /* leaf structure */
939 xfs_dir2_leaf_entry_t
*lep
; /* leaf entry */
940 xfs_dir2_leaf_tail_t
*ltp
; /* leaf tail structure */
941 xfs_mount_t
*mp
; /* file system mount point */
942 int needlog
; /* need to log data header */
943 int needscan
; /* need to scan for bestfree */
944 xfs_dir2_sf_hdr_t sfh
; /* shortform header */
945 int size
; /* bytes used */
946 __be16
*tagp
; /* end of entry (tag) */
947 int to
; /* block/leaf to index */
948 xfs_trans_t
*tp
; /* transaction pointer */
949 struct xfs_dir3_icleaf_hdr leafhdr
;
951 trace_xfs_dir2_leaf_to_block(args
);
957 xfs_dir2_leaf_hdr_from_disk(mp
, &leafhdr
, leaf
);
958 ltp
= xfs_dir2_leaf_tail_p(args
->geo
, leaf
);
960 ASSERT(leafhdr
.magic
== XFS_DIR2_LEAF1_MAGIC
||
961 leafhdr
.magic
== XFS_DIR3_LEAF1_MAGIC
);
963 * If there are data blocks other than the first one, take this
964 * opportunity to remove trailing empty data blocks that may have
965 * been left behind during no-space-reservation operations.
966 * These will show up in the leaf bests table.
968 while (dp
->i_disk_size
> args
->geo
->blksize
) {
971 hdrsz
= args
->geo
->data_entry_offset
;
972 bestsp
= xfs_dir2_leaf_bests_p(ltp
);
973 if (be16_to_cpu(bestsp
[be32_to_cpu(ltp
->bestcount
) - 1]) ==
974 args
->geo
->blksize
- hdrsz
) {
976 xfs_dir2_leaf_trim_data(args
, lbp
,
977 (xfs_dir2_db_t
)(be32_to_cpu(ltp
->bestcount
) - 1))))
983 * Read the data block if we don't already have it, give up if it fails.
986 error
= xfs_dir3_data_read(tp
, dp
, args
->owner
,
987 args
->geo
->datablk
, 0, &dbp
);
992 ASSERT(hdr
->magic
== cpu_to_be32(XFS_DIR2_DATA_MAGIC
) ||
993 hdr
->magic
== cpu_to_be32(XFS_DIR3_DATA_MAGIC
));
996 * Size of the "leaf" area in the block.
998 size
= (uint
)sizeof(xfs_dir2_block_tail_t
) +
999 (uint
)sizeof(*lep
) * (leafhdr
.count
- leafhdr
.stale
);
1001 * Look at the last data entry.
1003 tagp
= (__be16
*)((char *)hdr
+ args
->geo
->blksize
) - 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 xfs_dir3_block_init(args
, dbp
);
1020 * Use up the space at the end of the block (blp/btp).
1022 error
= xfs_dir2_data_use_free(args
, dbp
, dup
,
1023 args
->geo
->blksize
- size
, size
, &needlog
, &needscan
);
1027 * Initialize the block tail.
1029 btp
= xfs_dir2_block_tail_p(args
->geo
, hdr
);
1030 btp
->count
= cpu_to_be32(leafhdr
.count
- leafhdr
.stale
);
1032 xfs_dir2_block_log_tail(tp
, dbp
);
1034 * Initialize the block leaf area. We compact out stale entries.
1036 lep
= xfs_dir2_block_leaf_p(btp
);
1037 for (from
= to
= 0; from
< leafhdr
.count
; from
++) {
1038 if (leafhdr
.ents
[from
].address
==
1039 cpu_to_be32(XFS_DIR2_NULL_DATAPTR
))
1041 lep
[to
++] = leafhdr
.ents
[from
];
1043 ASSERT(to
== be32_to_cpu(btp
->count
));
1044 xfs_dir2_block_log_leaf(tp
, dbp
, 0, be32_to_cpu(btp
->count
) - 1);
1046 * Scan the bestfree if we need it and log the data block header.
1049 xfs_dir2_data_freescan(dp
->i_mount
, hdr
, &needlog
);
1051 xfs_dir2_data_log_header(args
, dbp
);
1053 * Pitch the old leaf block.
1055 error
= xfs_da_shrink_inode(args
, args
->geo
->leafblk
, lbp
);
1060 * Now see if the resulting block can be shrunken to shortform.
1062 size
= xfs_dir2_block_sfsize(dp
, hdr
, &sfh
);
1063 if (size
> xfs_inode_data_fork_size(dp
))
1066 return xfs_dir2_block_to_sf(args
, dbp
, size
, &sfh
);
1070 * Convert the shortform directory to block form.
1073 xfs_dir2_sf_to_block(
1074 struct xfs_da_args
*args
)
1076 struct xfs_trans
*tp
= args
->trans
;
1077 struct xfs_inode
*dp
= args
->dp
;
1078 struct xfs_mount
*mp
= dp
->i_mount
;
1079 struct xfs_ifork
*ifp
= xfs_ifork_ptr(dp
, XFS_DATA_FORK
);
1080 struct xfs_da_geometry
*geo
= args
->geo
;
1081 xfs_dir2_db_t blkno
; /* dir-relative block # (0) */
1082 xfs_dir2_data_hdr_t
*hdr
; /* block header */
1083 xfs_dir2_leaf_entry_t
*blp
; /* block leaf entries */
1084 struct xfs_buf
*bp
; /* block buffer */
1085 xfs_dir2_block_tail_t
*btp
; /* block tail pointer */
1086 xfs_dir2_data_entry_t
*dep
; /* data entry pointer */
1087 int dummy
; /* trash */
1088 xfs_dir2_data_unused_t
*dup
; /* unused entry pointer */
1089 int endoffset
; /* end of data objects */
1090 int error
; /* error return value */
1092 int needlog
; /* need to log block header */
1093 int needscan
; /* need to scan block freespc */
1094 int newoffset
; /* offset from current entry */
1095 unsigned int offset
= geo
->data_entry_offset
;
1096 xfs_dir2_sf_entry_t
*sfep
; /* sf entry pointer */
1097 struct xfs_dir2_sf_hdr
*oldsfp
= ifp
->if_data
;
1098 xfs_dir2_sf_hdr_t
*sfp
; /* shortform header */
1099 __be16
*tagp
; /* end of data entry */
1100 struct xfs_name name
;
1102 trace_xfs_dir2_sf_to_block(args
);
1104 ASSERT(ifp
->if_format
== XFS_DINODE_FMT_LOCAL
);
1105 ASSERT(dp
->i_disk_size
>= offsetof(struct xfs_dir2_sf_hdr
, parent
));
1107 ASSERT(ifp
->if_bytes
== dp
->i_disk_size
);
1108 ASSERT(oldsfp
!= NULL
);
1109 ASSERT(dp
->i_disk_size
>= xfs_dir2_sf_hdr_size(oldsfp
->i8count
));
1110 ASSERT(dp
->i_df
.if_nextents
== 0);
1113 * Copy the directory into a temporary buffer.
1114 * Then pitch the incore inode data so we can make extents.
1116 sfp
= kmalloc(ifp
->if_bytes
, GFP_KERNEL
| __GFP_NOFAIL
);
1117 memcpy(sfp
, oldsfp
, ifp
->if_bytes
);
1119 xfs_idata_realloc(dp
, -ifp
->if_bytes
, XFS_DATA_FORK
);
1120 xfs_bmap_local_to_extents_empty(tp
, dp
, XFS_DATA_FORK
);
1121 dp
->i_disk_size
= 0;
1124 * Add block 0 to the inode.
1126 error
= xfs_dir2_grow_inode(args
, XFS_DIR2_DATA_SPACE
, &blkno
);
1130 * Initialize the data block, then convert it to block format.
1132 error
= xfs_dir3_data_init(args
, blkno
, &bp
);
1135 xfs_dir3_block_init(args
, bp
);
1139 * Compute size of block "tail" area.
1141 i
= (uint
)sizeof(*btp
) +
1142 (sfp
->count
+ 2) * (uint
)sizeof(xfs_dir2_leaf_entry_t
);
1144 * The whole thing is initialized to free by the init routine.
1145 * Say we're using the leaf and tail area.
1147 dup
= bp
->b_addr
+ offset
;
1148 needlog
= needscan
= 0;
1149 error
= xfs_dir2_data_use_free(args
, bp
, dup
, args
->geo
->blksize
- i
,
1150 i
, &needlog
, &needscan
);
1153 ASSERT(needscan
== 0);
1157 btp
= xfs_dir2_block_tail_p(args
->geo
, hdr
);
1158 btp
->count
= cpu_to_be32(sfp
->count
+ 2); /* ., .. */
1160 blp
= xfs_dir2_block_leaf_p(btp
);
1161 endoffset
= (uint
)((char *)blp
- (char *)hdr
);
1163 * Remove the freespace, we'll manage it.
1165 error
= xfs_dir2_data_use_free(args
, bp
, dup
,
1166 (xfs_dir2_data_aoff_t
)((char *)dup
- (char *)hdr
),
1167 be16_to_cpu(dup
->length
), &needlog
, &needscan
);
1172 * Create entry for .
1174 dep
= bp
->b_addr
+ offset
;
1175 dep
->inumber
= cpu_to_be64(args
->owner
);
1178 xfs_dir2_data_put_ftype(mp
, dep
, XFS_DIR3_FT_DIR
);
1179 tagp
= xfs_dir2_data_entry_tag_p(mp
, dep
);
1180 *tagp
= cpu_to_be16(offset
);
1181 xfs_dir2_data_log_entry(args
, bp
, dep
);
1182 blp
[0].hashval
= cpu_to_be32(xfs_dir_hash_dot
);
1183 blp
[0].address
= cpu_to_be32(xfs_dir2_byte_to_dataptr(offset
));
1184 offset
+= xfs_dir2_data_entsize(mp
, dep
->namelen
);
1187 * Create entry for ..
1189 dep
= bp
->b_addr
+ offset
;
1190 dep
->inumber
= cpu_to_be64(xfs_dir2_sf_get_parent_ino(sfp
));
1192 dep
->name
[0] = dep
->name
[1] = '.';
1193 xfs_dir2_data_put_ftype(mp
, dep
, XFS_DIR3_FT_DIR
);
1194 tagp
= xfs_dir2_data_entry_tag_p(mp
, dep
);
1195 *tagp
= cpu_to_be16(offset
);
1196 xfs_dir2_data_log_entry(args
, bp
, dep
);
1197 blp
[1].hashval
= cpu_to_be32(xfs_dir_hash_dotdot
);
1198 blp
[1].address
= cpu_to_be32(xfs_dir2_byte_to_dataptr(offset
));
1199 offset
+= xfs_dir2_data_entsize(mp
, dep
->namelen
);
1202 * Loop over existing entries, stuff them in.
1208 sfep
= xfs_dir2_sf_firstentry(sfp
);
1211 * Need to preserve the existing offset values in the sf directory.
1212 * Insert holes (unused entries) where necessary.
1214 while (offset
< endoffset
) {
1216 * sfep is null when we reach the end of the list.
1219 newoffset
= endoffset
;
1221 newoffset
= xfs_dir2_sf_get_offset(sfep
);
1223 * There should be a hole here, make one.
1225 if (offset
< newoffset
) {
1226 dup
= bp
->b_addr
+ offset
;
1227 dup
->freetag
= cpu_to_be16(XFS_DIR2_DATA_FREE_TAG
);
1228 dup
->length
= cpu_to_be16(newoffset
- offset
);
1229 *xfs_dir2_data_unused_tag_p(dup
) = cpu_to_be16(offset
);
1230 xfs_dir2_data_log_unused(args
, bp
, dup
);
1231 xfs_dir2_data_freeinsert(hdr
,
1232 xfs_dir2_data_bestfree_p(mp
, hdr
),
1234 offset
+= be16_to_cpu(dup
->length
);
1238 * Copy a real entry.
1240 dep
= bp
->b_addr
+ newoffset
;
1241 dep
->inumber
= cpu_to_be64(xfs_dir2_sf_get_ino(mp
, sfp
, sfep
));
1242 dep
->namelen
= sfep
->namelen
;
1243 xfs_dir2_data_put_ftype(mp
, dep
,
1244 xfs_dir2_sf_get_ftype(mp
, sfep
));
1245 memcpy(dep
->name
, sfep
->name
, dep
->namelen
);
1246 tagp
= xfs_dir2_data_entry_tag_p(mp
, dep
);
1247 *tagp
= cpu_to_be16(newoffset
);
1248 xfs_dir2_data_log_entry(args
, bp
, dep
);
1249 name
.name
= sfep
->name
;
1250 name
.len
= sfep
->namelen
;
1251 blp
[2 + i
].hashval
= cpu_to_be32(xfs_dir2_hashname(mp
, &name
));
1252 blp
[2 + i
].address
=
1253 cpu_to_be32(xfs_dir2_byte_to_dataptr(newoffset
));
1254 offset
= (int)((char *)(tagp
+ 1) - (char *)hdr
);
1255 if (++i
== sfp
->count
)
1258 sfep
= xfs_dir2_sf_nextentry(mp
, sfp
, sfep
);
1260 /* Done with the temporary buffer */
1263 * Sort the leaf entries by hash value.
1265 xfs_sort(blp
, be32_to_cpu(btp
->count
), sizeof(*blp
), xfs_dir2_block_sort
);
1267 * Log the leaf entry area and tail.
1268 * Already logged the header in data_init, ignore needlog.
1270 ASSERT(needscan
== 0);
1271 xfs_dir2_block_log_leaf(tp
, bp
, 0, be32_to_cpu(btp
->count
) - 1);
1272 xfs_dir2_block_log_tail(tp
, bp
);
1273 xfs_dir3_data_check(dp
, bp
);