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_sb_version_hascrc(&mp
->m_sb
)) {
54 if (!uuid_equal(&hdr3
->uuid
, &mp
->m_sb
.sb_meta_uuid
))
55 return __this_address
;
56 if (be64_to_cpu(hdr3
->blkno
) != bp
->b_bn
)
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_sb_version_hascrc(&mp
->m_sb
) &&
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
);
96 if (!xfs_sb_version_hascrc(&mp
->m_sb
))
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
,
116 struct xfs_trans
*tp
,
117 struct xfs_inode
*dp
,
118 struct xfs_buf
**bpp
)
120 struct xfs_mount
*mp
= dp
->i_mount
;
123 err
= xfs_da_read_buf(tp
, dp
, mp
->m_dir_geo
->datablk
, -1, bpp
,
124 XFS_DATA_FORK
, &xfs_dir3_block_buf_ops
);
125 if (!err
&& tp
&& *bpp
)
126 xfs_trans_buf_set_type(tp
, *bpp
, XFS_BLFT_DIR_BLOCK_BUF
);
132 struct xfs_mount
*mp
,
133 struct xfs_trans
*tp
,
135 struct xfs_inode
*dp
)
137 struct xfs_dir3_blk_hdr
*hdr3
= bp
->b_addr
;
139 bp
->b_ops
= &xfs_dir3_block_buf_ops
;
140 xfs_trans_buf_set_type(tp
, bp
, XFS_BLFT_DIR_BLOCK_BUF
);
142 if (xfs_sb_version_hascrc(&mp
->m_sb
)) {
143 memset(hdr3
, 0, sizeof(*hdr3
));
144 hdr3
->magic
= cpu_to_be32(XFS_DIR3_BLOCK_MAGIC
);
145 hdr3
->blkno
= cpu_to_be64(bp
->b_bn
);
146 hdr3
->owner
= cpu_to_be64(dp
->i_ino
);
147 uuid_copy(&hdr3
->uuid
, &mp
->m_sb
.sb_meta_uuid
);
151 hdr3
->magic
= cpu_to_be32(XFS_DIR2_BLOCK_MAGIC
);
155 xfs_dir2_block_need_space(
156 struct xfs_inode
*dp
,
157 struct xfs_dir2_data_hdr
*hdr
,
158 struct xfs_dir2_block_tail
*btp
,
159 struct xfs_dir2_leaf_entry
*blp
,
161 struct xfs_dir2_data_unused
**dupp
,
162 struct xfs_dir2_data_unused
**enddupp
,
166 struct xfs_dir2_data_free
*bf
;
168 struct xfs_dir2_data_unused
*dup
= NULL
;
169 struct xfs_dir2_data_unused
*enddup
= NULL
;
172 bf
= dp
->d_ops
->data_bestfree_p(hdr
);
175 * If there are stale entries we'll use one for the leaf.
178 if (be16_to_cpu(bf
[0].length
) >= len
) {
180 * The biggest entry enough to avoid compaction.
182 dup
= (xfs_dir2_data_unused_t
*)
183 ((char *)hdr
+ be16_to_cpu(bf
[0].offset
));
188 * Will need to compact to make this work.
189 * Tag just before the first leaf entry.
192 tagp
= (__be16
*)blp
- 1;
194 /* Data object just before the first leaf entry. */
195 dup
= (xfs_dir2_data_unused_t
*)((char *)hdr
+ be16_to_cpu(*tagp
));
198 * If it's not free then the data will go where the
199 * leaf data starts now, if it works at all.
201 if (be16_to_cpu(dup
->freetag
) == XFS_DIR2_DATA_FREE_TAG
) {
202 if (be16_to_cpu(dup
->length
) + (be32_to_cpu(btp
->stale
) - 1) *
203 (uint
)sizeof(*blp
) < len
)
205 } else if ((be32_to_cpu(btp
->stale
) - 1) * (uint
)sizeof(*blp
) < len
)
208 dup
= (xfs_dir2_data_unused_t
*)blp
;
213 * no stale entries, so just use free space.
214 * Tag just before the first leaf entry.
216 tagp
= (__be16
*)blp
- 1;
218 /* Data object just before the first leaf entry. */
219 enddup
= (xfs_dir2_data_unused_t
*)((char *)hdr
+ be16_to_cpu(*tagp
));
222 * If it's not free then can't do this add without cleaning up:
223 * the space before the first leaf entry needs to be free so it
224 * can be expanded to hold the pointer to the new entry.
226 if (be16_to_cpu(enddup
->freetag
) == XFS_DIR2_DATA_FREE_TAG
) {
228 * Check out the biggest freespace and see if it's the same one.
230 dup
= (xfs_dir2_data_unused_t
*)
231 ((char *)hdr
+ be16_to_cpu(bf
[0].offset
));
234 * Not the same free entry, just check its length.
236 if (be16_to_cpu(dup
->length
) < len
)
242 * It is the biggest freespace, can it hold the leaf too?
244 if (be16_to_cpu(dup
->length
) < len
+ (uint
)sizeof(*blp
)) {
246 * Yes, use the second-largest entry instead if it works.
248 if (be16_to_cpu(bf
[1].length
) >= len
)
249 dup
= (xfs_dir2_data_unused_t
*)
250 ((char *)hdr
+ be16_to_cpu(bf
[1].offset
));
262 * compact the leaf entries.
263 * Leave the highest-numbered stale entry stale.
264 * XXX should be the one closest to mid but mid is not yet computed.
267 xfs_dir2_block_compact(
268 struct xfs_da_args
*args
,
270 struct xfs_dir2_data_hdr
*hdr
,
271 struct xfs_dir2_block_tail
*btp
,
272 struct xfs_dir2_leaf_entry
*blp
,
277 int fromidx
; /* source leaf index */
278 int toidx
; /* target leaf index */
280 int highstale
; /* high stale index */
282 fromidx
= toidx
= be32_to_cpu(btp
->count
) - 1;
283 highstale
= *lfloghigh
= -1;
284 for (; fromidx
>= 0; fromidx
--) {
285 if (blp
[fromidx
].address
== cpu_to_be32(XFS_DIR2_NULL_DATAPTR
)) {
289 if (*lfloghigh
== -1)
295 blp
[toidx
] = blp
[fromidx
];
298 *lfloglow
= toidx
+ 1 - (be32_to_cpu(btp
->stale
) - 1);
299 *lfloghigh
-= be32_to_cpu(btp
->stale
) - 1;
300 be32_add_cpu(&btp
->count
, -(be32_to_cpu(btp
->stale
) - 1));
301 xfs_dir2_data_make_free(args
, bp
,
302 (xfs_dir2_data_aoff_t
)((char *)blp
- (char *)hdr
),
303 (xfs_dir2_data_aoff_t
)((be32_to_cpu(btp
->stale
) - 1) * sizeof(*blp
)),
305 btp
->stale
= cpu_to_be32(1);
307 * If we now need to rebuild the bestfree map, do so.
308 * This needs to happen before the next call to use_free.
311 xfs_dir2_data_freescan(args
->dp
, hdr
, needlog
);
315 * Add an entry to a block directory.
318 xfs_dir2_block_addname(
319 xfs_da_args_t
*args
) /* directory op arguments */
321 xfs_dir2_data_hdr_t
*hdr
; /* block header */
322 xfs_dir2_leaf_entry_t
*blp
; /* block leaf entries */
323 struct xfs_buf
*bp
; /* buffer for block */
324 xfs_dir2_block_tail_t
*btp
; /* block tail */
325 int compact
; /* need to compact leaf ents */
326 xfs_dir2_data_entry_t
*dep
; /* block data entry */
327 xfs_inode_t
*dp
; /* directory inode */
328 xfs_dir2_data_unused_t
*dup
; /* block unused entry */
329 int error
; /* error return value */
330 xfs_dir2_data_unused_t
*enddup
=NULL
; /* unused at end of data */
331 xfs_dahash_t hash
; /* hash value of found entry */
332 int high
; /* high index for binary srch */
333 int highstale
; /* high stale index */
334 int lfloghigh
=0; /* last final leaf to log */
335 int lfloglow
=0; /* first final leaf to log */
336 int len
; /* length of the new entry */
337 int low
; /* low index for binary srch */
338 int lowstale
; /* low stale index */
339 int mid
=0; /* midpoint for binary srch */
340 int needlog
; /* need to log header */
341 int needscan
; /* need to rescan freespace */
342 __be16
*tagp
; /* pointer to tag value */
343 xfs_trans_t
*tp
; /* transaction structure */
345 trace_xfs_dir2_block_addname(args
);
350 /* Read the (one and only) directory block into bp. */
351 error
= xfs_dir3_block_read(tp
, dp
, &bp
);
355 len
= dp
->d_ops
->data_entsize(args
->namelen
);
358 * Set up pointers to parts of the block.
361 btp
= xfs_dir2_block_tail_p(args
->geo
, hdr
);
362 blp
= xfs_dir2_block_leaf_p(btp
);
365 * Find out if we can reuse stale entries or whether we need extra
366 * space for entry and new leaf.
368 xfs_dir2_block_need_space(dp
, hdr
, btp
, blp
, &tagp
, &dup
,
369 &enddup
, &compact
, len
);
372 * Done everything we need for a space check now.
374 if (args
->op_flags
& XFS_DA_OP_JUSTCHECK
) {
375 xfs_trans_brelse(tp
, bp
);
382 * If we don't have space for the new entry & leaf ...
385 /* Don't have a space reservation: return no-space. */
386 if (args
->total
== 0)
389 * Convert to the next larger format.
390 * Then add the new entry in that format.
392 error
= xfs_dir2_block_to_leaf(args
, bp
);
395 return xfs_dir2_leaf_addname(args
);
398 needlog
= needscan
= 0;
401 * If need to compact the leaf entries, do it now.
404 xfs_dir2_block_compact(args
, bp
, hdr
, btp
, blp
, &needlog
,
405 &lfloghigh
, &lfloglow
);
406 /* recalculate blp post-compaction */
407 blp
= xfs_dir2_block_leaf_p(btp
);
408 } else if (btp
->stale
) {
410 * Set leaf logging boundaries to impossible state.
411 * For the no-stale case they're set explicitly.
413 lfloglow
= be32_to_cpu(btp
->count
);
418 * Find the slot that's first lower than our hash value, -1 if none.
420 for (low
= 0, high
= be32_to_cpu(btp
->count
) - 1; low
<= high
; ) {
421 mid
= (low
+ high
) >> 1;
422 if ((hash
= be32_to_cpu(blp
[mid
].hashval
)) == args
->hashval
)
424 if (hash
< args
->hashval
)
429 while (mid
>= 0 && be32_to_cpu(blp
[mid
].hashval
) >= args
->hashval
) {
433 * No stale entries, will use enddup space to hold new leaf.
436 xfs_dir2_data_aoff_t aoff
;
439 * Mark the space needed for the new leaf entry, now in use.
441 aoff
= (xfs_dir2_data_aoff_t
)((char *)enddup
- (char *)hdr
+
442 be16_to_cpu(enddup
->length
) - sizeof(*blp
));
443 error
= xfs_dir2_data_use_free(args
, bp
, enddup
, aoff
,
444 (xfs_dir2_data_aoff_t
)sizeof(*blp
), &needlog
,
450 * Update the tail (entry count).
452 be32_add_cpu(&btp
->count
, 1);
454 * If we now need to rebuild the bestfree map, do so.
455 * This needs to happen before the next call to use_free.
458 xfs_dir2_data_freescan(dp
, hdr
, &needlog
);
462 * Adjust pointer to the first leaf entry, we're about to move
463 * the table up one to open up space for the new leaf entry.
464 * Then adjust our index to match.
469 memmove(blp
, &blp
[1], mid
* sizeof(*blp
));
474 * Use a stale leaf for our new entry.
479 blp
[lowstale
].address
!=
480 cpu_to_be32(XFS_DIR2_NULL_DATAPTR
);
483 for (highstale
= mid
+ 1;
484 highstale
< be32_to_cpu(btp
->count
) &&
485 blp
[highstale
].address
!=
486 cpu_to_be32(XFS_DIR2_NULL_DATAPTR
) &&
487 (lowstale
< 0 || mid
- lowstale
> highstale
- mid
);
491 * Move entries toward the low-numbered stale entry.
494 (highstale
== be32_to_cpu(btp
->count
) ||
495 mid
- lowstale
<= highstale
- mid
)) {
497 memmove(&blp
[lowstale
], &blp
[lowstale
+ 1],
498 (mid
- lowstale
) * sizeof(*blp
));
499 lfloglow
= min(lowstale
, lfloglow
);
500 lfloghigh
= max(mid
, lfloghigh
);
503 * Move entries toward the high-numbered stale entry.
506 ASSERT(highstale
< be32_to_cpu(btp
->count
));
509 memmove(&blp
[mid
+ 1], &blp
[mid
],
510 (highstale
- mid
) * sizeof(*blp
));
511 lfloglow
= min(mid
, lfloglow
);
512 lfloghigh
= max(highstale
, lfloghigh
);
514 be32_add_cpu(&btp
->stale
, -1);
517 * Point to the new data entry.
519 dep
= (xfs_dir2_data_entry_t
*)dup
;
521 * Fill in the leaf entry.
523 blp
[mid
].hashval
= cpu_to_be32(args
->hashval
);
524 blp
[mid
].address
= cpu_to_be32(xfs_dir2_byte_to_dataptr(
525 (char *)dep
- (char *)hdr
));
526 xfs_dir2_block_log_leaf(tp
, bp
, lfloglow
, lfloghigh
);
528 * Mark space for the data entry used.
530 error
= xfs_dir2_data_use_free(args
, bp
, dup
,
531 (xfs_dir2_data_aoff_t
)((char *)dup
- (char *)hdr
),
532 (xfs_dir2_data_aoff_t
)len
, &needlog
, &needscan
);
536 * Create the new data entry.
538 dep
->inumber
= cpu_to_be64(args
->inumber
);
539 dep
->namelen
= args
->namelen
;
540 memcpy(dep
->name
, args
->name
, args
->namelen
);
541 dp
->d_ops
->data_put_ftype(dep
, args
->filetype
);
542 tagp
= dp
->d_ops
->data_entry_tag_p(dep
);
543 *tagp
= cpu_to_be16((char *)dep
- (char *)hdr
);
545 * Clean up the bestfree array and log the header, tail, and entry.
548 xfs_dir2_data_freescan(dp
, hdr
, &needlog
);
550 xfs_dir2_data_log_header(args
, bp
);
551 xfs_dir2_block_log_tail(tp
, bp
);
552 xfs_dir2_data_log_entry(args
, bp
, dep
);
553 xfs_dir3_data_check(dp
, bp
);
558 * Log leaf entries from the block.
561 xfs_dir2_block_log_leaf(
562 xfs_trans_t
*tp
, /* transaction structure */
563 struct xfs_buf
*bp
, /* block buffer */
564 int first
, /* index of first logged leaf */
565 int last
) /* index of last logged leaf */
567 xfs_dir2_data_hdr_t
*hdr
= bp
->b_addr
;
568 xfs_dir2_leaf_entry_t
*blp
;
569 xfs_dir2_block_tail_t
*btp
;
571 btp
= xfs_dir2_block_tail_p(tp
->t_mountp
->m_dir_geo
, hdr
);
572 blp
= xfs_dir2_block_leaf_p(btp
);
573 xfs_trans_log_buf(tp
, bp
, (uint
)((char *)&blp
[first
] - (char *)hdr
),
574 (uint
)((char *)&blp
[last
+ 1] - (char *)hdr
- 1));
578 * Log the block tail.
581 xfs_dir2_block_log_tail(
582 xfs_trans_t
*tp
, /* transaction structure */
583 struct xfs_buf
*bp
) /* block buffer */
585 xfs_dir2_data_hdr_t
*hdr
= bp
->b_addr
;
586 xfs_dir2_block_tail_t
*btp
;
588 btp
= xfs_dir2_block_tail_p(tp
->t_mountp
->m_dir_geo
, hdr
);
589 xfs_trans_log_buf(tp
, bp
, (uint
)((char *)btp
- (char *)hdr
),
590 (uint
)((char *)(btp
+ 1) - (char *)hdr
- 1));
594 * Look up an entry in the block. This is the external routine,
595 * xfs_dir2_block_lookup_int does the real work.
598 xfs_dir2_block_lookup(
599 xfs_da_args_t
*args
) /* dir lookup arguments */
601 xfs_dir2_data_hdr_t
*hdr
; /* block header */
602 xfs_dir2_leaf_entry_t
*blp
; /* block leaf entries */
603 struct xfs_buf
*bp
; /* block buffer */
604 xfs_dir2_block_tail_t
*btp
; /* block tail */
605 xfs_dir2_data_entry_t
*dep
; /* block data entry */
606 xfs_inode_t
*dp
; /* incore inode */
607 int ent
; /* entry index */
608 int error
; /* error return value */
610 trace_xfs_dir2_block_lookup(args
);
613 * Get the buffer, look up the entry.
614 * If not found (ENOENT) then return, have no buffer.
616 if ((error
= xfs_dir2_block_lookup_int(args
, &bp
, &ent
)))
620 xfs_dir3_data_check(dp
, bp
);
621 btp
= xfs_dir2_block_tail_p(args
->geo
, hdr
);
622 blp
= xfs_dir2_block_leaf_p(btp
);
624 * Get the offset from the leaf entry, to point to the data.
626 dep
= (xfs_dir2_data_entry_t
*)((char *)hdr
+
627 xfs_dir2_dataptr_to_off(args
->geo
,
628 be32_to_cpu(blp
[ent
].address
)));
630 * Fill in inode number, CI name if appropriate, release the block.
632 args
->inumber
= be64_to_cpu(dep
->inumber
);
633 args
->filetype
= dp
->d_ops
->data_get_ftype(dep
);
634 error
= xfs_dir_cilookup_result(args
, dep
->name
, dep
->namelen
);
635 xfs_trans_brelse(args
->trans
, bp
);
640 * Internal block lookup routine.
642 static int /* error */
643 xfs_dir2_block_lookup_int(
644 xfs_da_args_t
*args
, /* dir lookup arguments */
645 struct xfs_buf
**bpp
, /* returned block buffer */
646 int *entno
) /* returned entry number */
648 xfs_dir2_dataptr_t addr
; /* data entry address */
649 xfs_dir2_data_hdr_t
*hdr
; /* block header */
650 xfs_dir2_leaf_entry_t
*blp
; /* block leaf entries */
651 struct xfs_buf
*bp
; /* block buffer */
652 xfs_dir2_block_tail_t
*btp
; /* block tail */
653 xfs_dir2_data_entry_t
*dep
; /* block data entry */
654 xfs_inode_t
*dp
; /* incore inode */
655 int error
; /* error return value */
656 xfs_dahash_t hash
; /* found hash value */
657 int high
; /* binary search high index */
658 int low
; /* binary search low index */
659 int mid
; /* binary search current idx */
660 xfs_mount_t
*mp
; /* filesystem mount point */
661 xfs_trans_t
*tp
; /* transaction pointer */
662 enum xfs_dacmp cmp
; /* comparison result */
668 error
= xfs_dir3_block_read(tp
, dp
, &bp
);
673 xfs_dir3_data_check(dp
, bp
);
674 btp
= xfs_dir2_block_tail_p(args
->geo
, hdr
);
675 blp
= xfs_dir2_block_leaf_p(btp
);
677 * Loop doing a binary search for our hash value.
678 * Find our entry, ENOENT if it's not there.
680 for (low
= 0, high
= be32_to_cpu(btp
->count
) - 1; ; ) {
682 mid
= (low
+ high
) >> 1;
683 if ((hash
= be32_to_cpu(blp
[mid
].hashval
)) == args
->hashval
)
685 if (hash
< args
->hashval
)
690 ASSERT(args
->op_flags
& XFS_DA_OP_OKNOENT
);
691 xfs_trans_brelse(tp
, bp
);
696 * Back up to the first one with the right hash value.
698 while (mid
> 0 && be32_to_cpu(blp
[mid
- 1].hashval
) == args
->hashval
) {
702 * Now loop forward through all the entries with the
703 * right hash value looking for our name.
706 if ((addr
= be32_to_cpu(blp
[mid
].address
)) == XFS_DIR2_NULL_DATAPTR
)
709 * Get pointer to the entry from the leaf.
711 dep
= (xfs_dir2_data_entry_t
*)
712 ((char *)hdr
+ xfs_dir2_dataptr_to_off(args
->geo
, addr
));
714 * Compare name and if it's an exact match, return the index
715 * and buffer. If it's the first case-insensitive match, store
716 * the index and buffer and continue looking for an exact match.
718 cmp
= mp
->m_dirnameops
->compname(args
, dep
->name
, dep
->namelen
);
719 if (cmp
!= XFS_CMP_DIFFERENT
&& cmp
!= args
->cmpresult
) {
720 args
->cmpresult
= cmp
;
723 if (cmp
== XFS_CMP_EXACT
)
726 } while (++mid
< be32_to_cpu(btp
->count
) &&
727 be32_to_cpu(blp
[mid
].hashval
) == hash
);
729 ASSERT(args
->op_flags
& XFS_DA_OP_OKNOENT
);
731 * Here, we can only be doing a lookup (not a rename or replace).
732 * If a case-insensitive match was found earlier, return success.
734 if (args
->cmpresult
== XFS_CMP_CASE
)
737 * No match, release the buffer and return ENOENT.
739 xfs_trans_brelse(tp
, bp
);
744 * Remove an entry from a block format directory.
745 * If that makes the block small enough to fit in shortform, transform it.
748 xfs_dir2_block_removename(
749 xfs_da_args_t
*args
) /* directory operation args */
751 xfs_dir2_data_hdr_t
*hdr
; /* block header */
752 xfs_dir2_leaf_entry_t
*blp
; /* block leaf pointer */
753 struct xfs_buf
*bp
; /* block buffer */
754 xfs_dir2_block_tail_t
*btp
; /* block tail */
755 xfs_dir2_data_entry_t
*dep
; /* block data entry */
756 xfs_inode_t
*dp
; /* incore inode */
757 int ent
; /* block leaf entry index */
758 int error
; /* error return value */
759 int needlog
; /* need to log block header */
760 int needscan
; /* need to fixup bestfree */
761 xfs_dir2_sf_hdr_t sfh
; /* shortform header */
762 int size
; /* shortform size */
763 xfs_trans_t
*tp
; /* transaction pointer */
765 trace_xfs_dir2_block_removename(args
);
768 * Look up the entry in the block. Gets the buffer and entry index.
769 * It will always be there, the vnodeops level does a lookup first.
771 if ((error
= xfs_dir2_block_lookup_int(args
, &bp
, &ent
))) {
777 btp
= xfs_dir2_block_tail_p(args
->geo
, hdr
);
778 blp
= xfs_dir2_block_leaf_p(btp
);
780 * Point to the data entry using the leaf entry.
782 dep
= (xfs_dir2_data_entry_t
*)((char *)hdr
+
783 xfs_dir2_dataptr_to_off(args
->geo
,
784 be32_to_cpu(blp
[ent
].address
)));
786 * Mark the data entry's space free.
788 needlog
= needscan
= 0;
789 xfs_dir2_data_make_free(args
, bp
,
790 (xfs_dir2_data_aoff_t
)((char *)dep
- (char *)hdr
),
791 dp
->d_ops
->data_entsize(dep
->namelen
), &needlog
, &needscan
);
793 * Fix up the block tail.
795 be32_add_cpu(&btp
->stale
, 1);
796 xfs_dir2_block_log_tail(tp
, bp
);
798 * Remove the leaf entry by marking it stale.
800 blp
[ent
].address
= cpu_to_be32(XFS_DIR2_NULL_DATAPTR
);
801 xfs_dir2_block_log_leaf(tp
, bp
, ent
, ent
);
803 * Fix up bestfree, log the header if necessary.
806 xfs_dir2_data_freescan(dp
, hdr
, &needlog
);
808 xfs_dir2_data_log_header(args
, bp
);
809 xfs_dir3_data_check(dp
, bp
);
811 * See if the size as a shortform is good enough.
813 size
= xfs_dir2_block_sfsize(dp
, hdr
, &sfh
);
814 if (size
> XFS_IFORK_DSIZE(dp
))
818 * If it works, do the conversion.
820 return xfs_dir2_block_to_sf(args
, bp
, size
, &sfh
);
824 * Replace an entry in a V2 block directory.
825 * Change the inode number to the new value.
828 xfs_dir2_block_replace(
829 xfs_da_args_t
*args
) /* directory operation args */
831 xfs_dir2_data_hdr_t
*hdr
; /* block header */
832 xfs_dir2_leaf_entry_t
*blp
; /* block leaf entries */
833 struct xfs_buf
*bp
; /* block buffer */
834 xfs_dir2_block_tail_t
*btp
; /* block tail */
835 xfs_dir2_data_entry_t
*dep
; /* block data entry */
836 xfs_inode_t
*dp
; /* incore inode */
837 int ent
; /* leaf entry index */
838 int error
; /* error return value */
840 trace_xfs_dir2_block_replace(args
);
843 * Lookup the entry in the directory. Get buffer and entry index.
844 * This will always succeed since the caller has already done a lookup.
846 if ((error
= xfs_dir2_block_lookup_int(args
, &bp
, &ent
))) {
851 btp
= xfs_dir2_block_tail_p(args
->geo
, hdr
);
852 blp
= xfs_dir2_block_leaf_p(btp
);
854 * Point to the data entry we need to change.
856 dep
= (xfs_dir2_data_entry_t
*)((char *)hdr
+
857 xfs_dir2_dataptr_to_off(args
->geo
,
858 be32_to_cpu(blp
[ent
].address
)));
859 ASSERT(be64_to_cpu(dep
->inumber
) != args
->inumber
);
861 * Change the inode number to the new value.
863 dep
->inumber
= cpu_to_be64(args
->inumber
);
864 dp
->d_ops
->data_put_ftype(dep
, args
->filetype
);
865 xfs_dir2_data_log_entry(args
, bp
, dep
);
866 xfs_dir3_data_check(dp
, bp
);
871 * Qsort comparison routine for the block leaf entries.
873 static int /* sort order */
875 const void *a
, /* first leaf entry */
876 const void *b
) /* second leaf entry */
878 const xfs_dir2_leaf_entry_t
*la
; /* first leaf entry */
879 const xfs_dir2_leaf_entry_t
*lb
; /* second leaf entry */
883 return be32_to_cpu(la
->hashval
) < be32_to_cpu(lb
->hashval
) ? -1 :
884 (be32_to_cpu(la
->hashval
) > be32_to_cpu(lb
->hashval
) ? 1 : 0);
888 * Convert a V2 leaf directory to a V2 block directory if possible.
891 xfs_dir2_leaf_to_block(
892 xfs_da_args_t
*args
, /* operation arguments */
893 struct xfs_buf
*lbp
, /* leaf buffer */
894 struct xfs_buf
*dbp
) /* data buffer */
896 __be16
*bestsp
; /* leaf bests table */
897 xfs_dir2_data_hdr_t
*hdr
; /* block header */
898 xfs_dir2_block_tail_t
*btp
; /* block tail */
899 xfs_inode_t
*dp
; /* incore directory inode */
900 xfs_dir2_data_unused_t
*dup
; /* unused data entry */
901 int error
; /* error return value */
902 int from
; /* leaf from index */
903 xfs_dir2_leaf_t
*leaf
; /* leaf structure */
904 xfs_dir2_leaf_entry_t
*lep
; /* leaf entry */
905 xfs_dir2_leaf_tail_t
*ltp
; /* leaf tail structure */
906 xfs_mount_t
*mp
; /* file system mount point */
907 int needlog
; /* need to log data header */
908 int needscan
; /* need to scan for bestfree */
909 xfs_dir2_sf_hdr_t sfh
; /* shortform header */
910 int size
; /* bytes used */
911 __be16
*tagp
; /* end of entry (tag) */
912 int to
; /* block/leaf to index */
913 xfs_trans_t
*tp
; /* transaction pointer */
914 struct xfs_dir2_leaf_entry
*ents
;
915 struct xfs_dir3_icleaf_hdr leafhdr
;
917 trace_xfs_dir2_leaf_to_block(args
);
923 dp
->d_ops
->leaf_hdr_from_disk(&leafhdr
, leaf
);
924 ents
= dp
->d_ops
->leaf_ents_p(leaf
);
925 ltp
= xfs_dir2_leaf_tail_p(args
->geo
, leaf
);
927 ASSERT(leafhdr
.magic
== XFS_DIR2_LEAF1_MAGIC
||
928 leafhdr
.magic
== XFS_DIR3_LEAF1_MAGIC
);
930 * If there are data blocks other than the first one, take this
931 * opportunity to remove trailing empty data blocks that may have
932 * been left behind during no-space-reservation operations.
933 * These will show up in the leaf bests table.
935 while (dp
->i_d
.di_size
> args
->geo
->blksize
) {
938 hdrsz
= dp
->d_ops
->data_entry_offset
;
939 bestsp
= xfs_dir2_leaf_bests_p(ltp
);
940 if (be16_to_cpu(bestsp
[be32_to_cpu(ltp
->bestcount
) - 1]) ==
941 args
->geo
->blksize
- hdrsz
) {
943 xfs_dir2_leaf_trim_data(args
, lbp
,
944 (xfs_dir2_db_t
)(be32_to_cpu(ltp
->bestcount
) - 1))))
950 * Read the data block if we don't already have it, give up if it fails.
953 error
= xfs_dir3_data_read(tp
, dp
, args
->geo
->datablk
, -1, &dbp
);
958 ASSERT(hdr
->magic
== cpu_to_be32(XFS_DIR2_DATA_MAGIC
) ||
959 hdr
->magic
== cpu_to_be32(XFS_DIR3_DATA_MAGIC
));
962 * Size of the "leaf" area in the block.
964 size
= (uint
)sizeof(xfs_dir2_block_tail_t
) +
965 (uint
)sizeof(*lep
) * (leafhdr
.count
- leafhdr
.stale
);
967 * Look at the last data entry.
969 tagp
= (__be16
*)((char *)hdr
+ args
->geo
->blksize
) - 1;
970 dup
= (xfs_dir2_data_unused_t
*)((char *)hdr
+ be16_to_cpu(*tagp
));
972 * If it's not free or is too short we can't do it.
974 if (be16_to_cpu(dup
->freetag
) != XFS_DIR2_DATA_FREE_TAG
||
975 be16_to_cpu(dup
->length
) < size
)
979 * Start converting it to block form.
981 xfs_dir3_block_init(mp
, tp
, dbp
, dp
);
986 * Use up the space at the end of the block (blp/btp).
988 error
= xfs_dir2_data_use_free(args
, dbp
, dup
,
989 args
->geo
->blksize
- size
, size
, &needlog
, &needscan
);
993 * Initialize the block tail.
995 btp
= xfs_dir2_block_tail_p(args
->geo
, hdr
);
996 btp
->count
= cpu_to_be32(leafhdr
.count
- leafhdr
.stale
);
998 xfs_dir2_block_log_tail(tp
, dbp
);
1000 * Initialize the block leaf area. We compact out stale entries.
1002 lep
= xfs_dir2_block_leaf_p(btp
);
1003 for (from
= to
= 0; from
< leafhdr
.count
; from
++) {
1004 if (ents
[from
].address
== cpu_to_be32(XFS_DIR2_NULL_DATAPTR
))
1006 lep
[to
++] = ents
[from
];
1008 ASSERT(to
== be32_to_cpu(btp
->count
));
1009 xfs_dir2_block_log_leaf(tp
, dbp
, 0, be32_to_cpu(btp
->count
) - 1);
1011 * Scan the bestfree if we need it and log the data block header.
1014 xfs_dir2_data_freescan(dp
, hdr
, &needlog
);
1016 xfs_dir2_data_log_header(args
, dbp
);
1018 * Pitch the old leaf block.
1020 error
= xfs_da_shrink_inode(args
, args
->geo
->leafblk
, lbp
);
1025 * Now see if the resulting block can be shrunken to shortform.
1027 size
= xfs_dir2_block_sfsize(dp
, hdr
, &sfh
);
1028 if (size
> XFS_IFORK_DSIZE(dp
))
1031 return xfs_dir2_block_to_sf(args
, dbp
, size
, &sfh
);
1035 * Convert the shortform directory to block form.
1038 xfs_dir2_sf_to_block(
1039 xfs_da_args_t
*args
) /* operation arguments */
1041 xfs_dir2_db_t blkno
; /* dir-relative block # (0) */
1042 xfs_dir2_data_hdr_t
*hdr
; /* block header */
1043 xfs_dir2_leaf_entry_t
*blp
; /* block leaf entries */
1044 struct xfs_buf
*bp
; /* block buffer */
1045 xfs_dir2_block_tail_t
*btp
; /* block tail pointer */
1046 xfs_dir2_data_entry_t
*dep
; /* data entry pointer */
1047 xfs_inode_t
*dp
; /* incore directory inode */
1048 int dummy
; /* trash */
1049 xfs_dir2_data_unused_t
*dup
; /* unused entry pointer */
1050 int endoffset
; /* end of data objects */
1051 int error
; /* error return value */
1053 xfs_mount_t
*mp
; /* filesystem mount point */
1054 int needlog
; /* need to log block header */
1055 int needscan
; /* need to scan block freespc */
1056 int newoffset
; /* offset from current entry */
1057 int offset
; /* target block offset */
1058 xfs_dir2_sf_entry_t
*sfep
; /* sf entry pointer */
1059 xfs_dir2_sf_hdr_t
*oldsfp
; /* old shortform header */
1060 xfs_dir2_sf_hdr_t
*sfp
; /* shortform header */
1061 __be16
*tagp
; /* end of data entry */
1062 xfs_trans_t
*tp
; /* transaction pointer */
1063 struct xfs_name name
;
1064 struct xfs_ifork
*ifp
;
1066 trace_xfs_dir2_sf_to_block(args
);
1071 ifp
= XFS_IFORK_PTR(dp
, XFS_DATA_FORK
);
1072 ASSERT(ifp
->if_flags
& XFS_IFINLINE
);
1074 * Bomb out if the shortform directory is way too short.
1076 if (dp
->i_d
.di_size
< offsetof(xfs_dir2_sf_hdr_t
, parent
)) {
1077 ASSERT(XFS_FORCED_SHUTDOWN(mp
));
1081 oldsfp
= (xfs_dir2_sf_hdr_t
*)ifp
->if_u1
.if_data
;
1083 ASSERT(ifp
->if_bytes
== dp
->i_d
.di_size
);
1084 ASSERT(ifp
->if_u1
.if_data
!= NULL
);
1085 ASSERT(dp
->i_d
.di_size
>= xfs_dir2_sf_hdr_size(oldsfp
->i8count
));
1086 ASSERT(dp
->i_d
.di_nextents
== 0);
1089 * Copy the directory into a temporary buffer.
1090 * Then pitch the incore inode data so we can make extents.
1092 sfp
= kmem_alloc(ifp
->if_bytes
, 0);
1093 memcpy(sfp
, oldsfp
, ifp
->if_bytes
);
1095 xfs_idata_realloc(dp
, -ifp
->if_bytes
, XFS_DATA_FORK
);
1096 xfs_bmap_local_to_extents_empty(tp
, dp
, XFS_DATA_FORK
);
1097 dp
->i_d
.di_size
= 0;
1100 * Add block 0 to the inode.
1102 error
= xfs_dir2_grow_inode(args
, XFS_DIR2_DATA_SPACE
, &blkno
);
1106 * Initialize the data block, then convert it to block format.
1108 error
= xfs_dir3_data_init(args
, blkno
, &bp
);
1111 xfs_dir3_block_init(mp
, tp
, bp
, dp
);
1115 * Compute size of block "tail" area.
1117 i
= (uint
)sizeof(*btp
) +
1118 (sfp
->count
+ 2) * (uint
)sizeof(xfs_dir2_leaf_entry_t
);
1120 * The whole thing is initialized to free by the init routine.
1121 * Say we're using the leaf and tail area.
1123 dup
= dp
->d_ops
->data_unused_p(hdr
);
1124 needlog
= needscan
= 0;
1125 error
= xfs_dir2_data_use_free(args
, bp
, dup
, args
->geo
->blksize
- i
,
1126 i
, &needlog
, &needscan
);
1129 ASSERT(needscan
== 0);
1133 btp
= xfs_dir2_block_tail_p(args
->geo
, hdr
);
1134 btp
->count
= cpu_to_be32(sfp
->count
+ 2); /* ., .. */
1136 blp
= xfs_dir2_block_leaf_p(btp
);
1137 endoffset
= (uint
)((char *)blp
- (char *)hdr
);
1139 * Remove the freespace, we'll manage it.
1141 error
= xfs_dir2_data_use_free(args
, bp
, dup
,
1142 (xfs_dir2_data_aoff_t
)((char *)dup
- (char *)hdr
),
1143 be16_to_cpu(dup
->length
), &needlog
, &needscan
);
1147 * Create entry for .
1149 dep
= dp
->d_ops
->data_dot_entry_p(hdr
);
1150 dep
->inumber
= cpu_to_be64(dp
->i_ino
);
1153 dp
->d_ops
->data_put_ftype(dep
, XFS_DIR3_FT_DIR
);
1154 tagp
= dp
->d_ops
->data_entry_tag_p(dep
);
1155 *tagp
= cpu_to_be16((char *)dep
- (char *)hdr
);
1156 xfs_dir2_data_log_entry(args
, bp
, dep
);
1157 blp
[0].hashval
= cpu_to_be32(xfs_dir_hash_dot
);
1158 blp
[0].address
= cpu_to_be32(xfs_dir2_byte_to_dataptr(
1159 (char *)dep
- (char *)hdr
));
1161 * Create entry for ..
1163 dep
= dp
->d_ops
->data_dotdot_entry_p(hdr
);
1164 dep
->inumber
= cpu_to_be64(dp
->d_ops
->sf_get_parent_ino(sfp
));
1166 dep
->name
[0] = dep
->name
[1] = '.';
1167 dp
->d_ops
->data_put_ftype(dep
, XFS_DIR3_FT_DIR
);
1168 tagp
= dp
->d_ops
->data_entry_tag_p(dep
);
1169 *tagp
= cpu_to_be16((char *)dep
- (char *)hdr
);
1170 xfs_dir2_data_log_entry(args
, bp
, dep
);
1171 blp
[1].hashval
= cpu_to_be32(xfs_dir_hash_dotdot
);
1172 blp
[1].address
= cpu_to_be32(xfs_dir2_byte_to_dataptr(
1173 (char *)dep
- (char *)hdr
));
1174 offset
= dp
->d_ops
->data_first_offset
;
1176 * Loop over existing entries, stuff them in.
1182 sfep
= xfs_dir2_sf_firstentry(sfp
);
1184 * Need to preserve the existing offset values in the sf directory.
1185 * Insert holes (unused entries) where necessary.
1187 while (offset
< endoffset
) {
1189 * sfep is null when we reach the end of the list.
1192 newoffset
= endoffset
;
1194 newoffset
= xfs_dir2_sf_get_offset(sfep
);
1196 * There should be a hole here, make one.
1198 if (offset
< newoffset
) {
1199 dup
= (xfs_dir2_data_unused_t
*)((char *)hdr
+ offset
);
1200 dup
->freetag
= cpu_to_be16(XFS_DIR2_DATA_FREE_TAG
);
1201 dup
->length
= cpu_to_be16(newoffset
- offset
);
1202 *xfs_dir2_data_unused_tag_p(dup
) = cpu_to_be16(
1203 ((char *)dup
- (char *)hdr
));
1204 xfs_dir2_data_log_unused(args
, bp
, dup
);
1205 xfs_dir2_data_freeinsert(hdr
,
1206 dp
->d_ops
->data_bestfree_p(hdr
),
1208 offset
+= be16_to_cpu(dup
->length
);
1212 * Copy a real entry.
1214 dep
= (xfs_dir2_data_entry_t
*)((char *)hdr
+ newoffset
);
1215 dep
->inumber
= cpu_to_be64(dp
->d_ops
->sf_get_ino(sfp
, sfep
));
1216 dep
->namelen
= sfep
->namelen
;
1217 dp
->d_ops
->data_put_ftype(dep
, dp
->d_ops
->sf_get_ftype(sfep
));
1218 memcpy(dep
->name
, sfep
->name
, dep
->namelen
);
1219 tagp
= dp
->d_ops
->data_entry_tag_p(dep
);
1220 *tagp
= cpu_to_be16((char *)dep
- (char *)hdr
);
1221 xfs_dir2_data_log_entry(args
, bp
, dep
);
1222 name
.name
= sfep
->name
;
1223 name
.len
= sfep
->namelen
;
1224 blp
[2 + i
].hashval
= cpu_to_be32(mp
->m_dirnameops
->
1226 blp
[2 + i
].address
= cpu_to_be32(xfs_dir2_byte_to_dataptr(
1227 (char *)dep
- (char *)hdr
));
1228 offset
= (int)((char *)(tagp
+ 1) - (char *)hdr
);
1229 if (++i
== sfp
->count
)
1232 sfep
= dp
->d_ops
->sf_nextentry(sfp
, sfep
);
1234 /* Done with the temporary buffer */
1237 * Sort the leaf entries by hash value.
1239 xfs_sort(blp
, be32_to_cpu(btp
->count
), sizeof(*blp
), xfs_dir2_block_sort
);
1241 * Log the leaf entry area and tail.
1242 * Already logged the header in data_init, ignore needlog.
1244 ASSERT(needscan
== 0);
1245 xfs_dir2_block_log_leaf(tp
, bp
, 0, be32_to_cpu(btp
->count
) - 1);
1246 xfs_dir2_block_log_tail(tp
, bp
);
1247 xfs_dir3_data_check(dp
, bp
);