2 * Copyright (c) 2000-2003,2005 Silicon Graphics, Inc.
3 * Copyright (c) 2013 Red Hat, Inc.
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as
8 * published by the Free Software Foundation.
10 * This program is distributed in the hope that it would be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
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_target
->bt_mount
;
49 struct xfs_dir3_blk_hdr
*hdr3
= bp
->b_addr
;
51 if (xfs_sb_version_hascrc(&mp
->m_sb
)) {
52 if (hdr3
->magic
!= cpu_to_be32(XFS_DIR3_BLOCK_MAGIC
))
54 if (!uuid_equal(&hdr3
->uuid
, &mp
->m_sb
.sb_uuid
))
56 if (be64_to_cpu(hdr3
->blkno
) != bp
->b_bn
)
59 if (hdr3
->magic
!= cpu_to_be32(XFS_DIR2_BLOCK_MAGIC
))
62 if (__xfs_dir3_data_check(NULL
, bp
))
68 xfs_dir3_block_read_verify(
71 struct xfs_mount
*mp
= bp
->b_target
->bt_mount
;
73 if ((xfs_sb_version_hascrc(&mp
->m_sb
) &&
74 !xfs_buf_verify_cksum(bp
, XFS_DIR3_DATA_CRC_OFF
)) ||
75 !xfs_dir3_block_verify(bp
)) {
76 XFS_CORRUPTION_ERROR(__func__
, XFS_ERRLEVEL_LOW
, mp
, bp
->b_addr
);
77 xfs_buf_ioerror(bp
, EFSCORRUPTED
);
82 xfs_dir3_block_write_verify(
85 struct xfs_mount
*mp
= bp
->b_target
->bt_mount
;
86 struct xfs_buf_log_item
*bip
= bp
->b_fspriv
;
87 struct xfs_dir3_blk_hdr
*hdr3
= bp
->b_addr
;
89 if (!xfs_dir3_block_verify(bp
)) {
90 XFS_CORRUPTION_ERROR(__func__
, XFS_ERRLEVEL_LOW
, mp
, bp
->b_addr
);
91 xfs_buf_ioerror(bp
, EFSCORRUPTED
);
95 if (!xfs_sb_version_hascrc(&mp
->m_sb
))
99 hdr3
->lsn
= cpu_to_be64(bip
->bli_item
.li_lsn
);
101 xfs_buf_update_cksum(bp
, XFS_DIR3_DATA_CRC_OFF
);
104 const struct xfs_buf_ops xfs_dir3_block_buf_ops
= {
105 .verify_read
= xfs_dir3_block_read_verify
,
106 .verify_write
= xfs_dir3_block_write_verify
,
111 struct xfs_trans
*tp
,
112 struct xfs_inode
*dp
,
113 struct xfs_buf
**bpp
)
115 struct xfs_mount
*mp
= dp
->i_mount
;
118 err
= xfs_da_read_buf(tp
, dp
, mp
->m_dirdatablk
, -1, bpp
,
119 XFS_DATA_FORK
, &xfs_dir3_block_buf_ops
);
121 xfs_trans_buf_set_type(tp
, *bpp
, XFS_BLFT_DIR_BLOCK_BUF
);
127 struct xfs_mount
*mp
,
128 struct xfs_trans
*tp
,
130 struct xfs_inode
*dp
)
132 struct xfs_dir3_blk_hdr
*hdr3
= bp
->b_addr
;
134 bp
->b_ops
= &xfs_dir3_block_buf_ops
;
135 xfs_trans_buf_set_type(tp
, bp
, XFS_BLFT_DIR_BLOCK_BUF
);
137 if (xfs_sb_version_hascrc(&mp
->m_sb
)) {
138 memset(hdr3
, 0, sizeof(*hdr3
));
139 hdr3
->magic
= cpu_to_be32(XFS_DIR3_BLOCK_MAGIC
);
140 hdr3
->blkno
= cpu_to_be64(bp
->b_bn
);
141 hdr3
->owner
= cpu_to_be64(dp
->i_ino
);
142 uuid_copy(&hdr3
->uuid
, &mp
->m_sb
.sb_uuid
);
146 hdr3
->magic
= cpu_to_be32(XFS_DIR2_BLOCK_MAGIC
);
150 xfs_dir2_block_need_space(
151 struct xfs_dir2_data_hdr
*hdr
,
152 struct xfs_dir2_block_tail
*btp
,
153 struct xfs_dir2_leaf_entry
*blp
,
155 struct xfs_dir2_data_unused
**dupp
,
156 struct xfs_dir2_data_unused
**enddupp
,
160 struct xfs_dir2_data_free
*bf
;
162 struct xfs_dir2_data_unused
*dup
= NULL
;
163 struct xfs_dir2_data_unused
*enddup
= NULL
;
166 bf
= xfs_dir3_data_bestfree_p(hdr
);
169 * If there are stale entries we'll use one for the leaf.
172 if (be16_to_cpu(bf
[0].length
) >= len
) {
174 * The biggest entry enough to avoid compaction.
176 dup
= (xfs_dir2_data_unused_t
*)
177 ((char *)hdr
+ be16_to_cpu(bf
[0].offset
));
182 * Will need to compact to make this work.
183 * Tag just before the first leaf entry.
186 tagp
= (__be16
*)blp
- 1;
188 /* Data object just before the first leaf entry. */
189 dup
= (xfs_dir2_data_unused_t
*)((char *)hdr
+ be16_to_cpu(*tagp
));
192 * If it's not free then the data will go where the
193 * leaf data starts now, if it works at all.
195 if (be16_to_cpu(dup
->freetag
) == XFS_DIR2_DATA_FREE_TAG
) {
196 if (be16_to_cpu(dup
->length
) + (be32_to_cpu(btp
->stale
) - 1) *
197 (uint
)sizeof(*blp
) < len
)
199 } else if ((be32_to_cpu(btp
->stale
) - 1) * (uint
)sizeof(*blp
) < len
)
202 dup
= (xfs_dir2_data_unused_t
*)blp
;
207 * no stale entries, so just use free space.
208 * Tag just before the first leaf entry.
210 tagp
= (__be16
*)blp
- 1;
212 /* Data object just before the first leaf entry. */
213 enddup
= (xfs_dir2_data_unused_t
*)((char *)hdr
+ be16_to_cpu(*tagp
));
216 * If it's not free then can't do this add without cleaning up:
217 * the space before the first leaf entry needs to be free so it
218 * can be expanded to hold the pointer to the new entry.
220 if (be16_to_cpu(enddup
->freetag
) == XFS_DIR2_DATA_FREE_TAG
) {
222 * Check out the biggest freespace and see if it's the same one.
224 dup
= (xfs_dir2_data_unused_t
*)
225 ((char *)hdr
+ be16_to_cpu(bf
[0].offset
));
228 * Not the same free entry, just check its length.
230 if (be16_to_cpu(dup
->length
) < len
)
236 * It is the biggest freespace, can it hold the leaf too?
238 if (be16_to_cpu(dup
->length
) < len
+ (uint
)sizeof(*blp
)) {
240 * Yes, use the second-largest entry instead if it works.
242 if (be16_to_cpu(bf
[1].length
) >= len
)
243 dup
= (xfs_dir2_data_unused_t
*)
244 ((char *)hdr
+ be16_to_cpu(bf
[1].offset
));
256 * compact the leaf entries.
257 * Leave the highest-numbered stale entry stale.
258 * XXX should be the one closest to mid but mid is not yet computed.
261 xfs_dir2_block_compact(
262 struct xfs_trans
*tp
,
264 struct xfs_dir2_data_hdr
*hdr
,
265 struct xfs_dir2_block_tail
*btp
,
266 struct xfs_dir2_leaf_entry
*blp
,
271 int fromidx
; /* source leaf index */
272 int toidx
; /* target leaf index */
274 int highstale
; /* high stale index */
276 fromidx
= toidx
= be32_to_cpu(btp
->count
) - 1;
277 highstale
= *lfloghigh
= -1;
278 for (; fromidx
>= 0; fromidx
--) {
279 if (blp
[fromidx
].address
== cpu_to_be32(XFS_DIR2_NULL_DATAPTR
)) {
283 if (*lfloghigh
== -1)
289 blp
[toidx
] = blp
[fromidx
];
292 *lfloglow
= toidx
+ 1 - (be32_to_cpu(btp
->stale
) - 1);
293 *lfloghigh
-= be32_to_cpu(btp
->stale
) - 1;
294 be32_add_cpu(&btp
->count
, -(be32_to_cpu(btp
->stale
) - 1));
295 xfs_dir2_data_make_free(tp
, bp
,
296 (xfs_dir2_data_aoff_t
)((char *)blp
- (char *)hdr
),
297 (xfs_dir2_data_aoff_t
)((be32_to_cpu(btp
->stale
) - 1) * sizeof(*blp
)),
299 blp
+= be32_to_cpu(btp
->stale
) - 1;
300 btp
->stale
= cpu_to_be32(1);
302 * If we now need to rebuild the bestfree map, do so.
303 * This needs to happen before the next call to use_free.
306 xfs_dir2_data_freescan(tp
->t_mountp
, hdr
, needlog
);
310 * Add an entry to a block directory.
313 xfs_dir2_block_addname(
314 xfs_da_args_t
*args
) /* directory op arguments */
316 xfs_dir2_data_hdr_t
*hdr
; /* block header */
317 xfs_dir2_leaf_entry_t
*blp
; /* block leaf entries */
318 struct xfs_buf
*bp
; /* buffer for block */
319 xfs_dir2_block_tail_t
*btp
; /* block tail */
320 int compact
; /* need to compact leaf ents */
321 xfs_dir2_data_entry_t
*dep
; /* block data entry */
322 xfs_inode_t
*dp
; /* directory inode */
323 xfs_dir2_data_unused_t
*dup
; /* block unused entry */
324 int error
; /* error return value */
325 xfs_dir2_data_unused_t
*enddup
=NULL
; /* unused at end of data */
326 xfs_dahash_t hash
; /* hash value of found entry */
327 int high
; /* high index for binary srch */
328 int highstale
; /* high stale index */
329 int lfloghigh
=0; /* last final leaf to log */
330 int lfloglow
=0; /* first final leaf to log */
331 int len
; /* length of the new entry */
332 int low
; /* low index for binary srch */
333 int lowstale
; /* low stale index */
334 int mid
=0; /* midpoint for binary srch */
335 xfs_mount_t
*mp
; /* filesystem mount point */
336 int needlog
; /* need to log header */
337 int needscan
; /* need to rescan freespace */
338 __be16
*tagp
; /* pointer to tag value */
339 xfs_trans_t
*tp
; /* transaction structure */
341 trace_xfs_dir2_block_addname(args
);
347 /* Read the (one and only) directory block into bp. */
348 error
= xfs_dir3_block_read(tp
, dp
, &bp
);
352 len
= xfs_dir3_data_entsize(mp
, args
->namelen
);
355 * Set up pointers to parts of the block.
358 btp
= xfs_dir2_block_tail_p(mp
, hdr
);
359 blp
= xfs_dir2_block_leaf_p(btp
);
362 * Find out if we can reuse stale entries or whether we need extra
363 * space for entry and new leaf.
365 xfs_dir2_block_need_space(hdr
, btp
, blp
, &tagp
, &dup
,
366 &enddup
, &compact
, len
);
369 * Done everything we need for a space check now.
371 if (args
->op_flags
& XFS_DA_OP_JUSTCHECK
) {
372 xfs_trans_brelse(tp
, bp
);
374 return XFS_ERROR(ENOSPC
);
379 * If we don't have space for the new entry & leaf ...
382 /* Don't have a space reservation: return no-space. */
383 if (args
->total
== 0)
384 return XFS_ERROR(ENOSPC
);
386 * Convert to the next larger format.
387 * Then add the new entry in that format.
389 error
= xfs_dir2_block_to_leaf(args
, bp
);
392 return xfs_dir2_leaf_addname(args
);
395 needlog
= needscan
= 0;
398 * If need to compact the leaf entries, do it now.
401 xfs_dir2_block_compact(tp
, bp
, hdr
, btp
, blp
, &needlog
,
402 &lfloghigh
, &lfloglow
);
403 /* recalculate blp post-compaction */
404 blp
= xfs_dir2_block_leaf_p(btp
);
405 } else if (btp
->stale
) {
407 * Set leaf logging boundaries to impossible state.
408 * For the no-stale case they're set explicitly.
410 lfloglow
= be32_to_cpu(btp
->count
);
415 * Find the slot that's first lower than our hash value, -1 if none.
417 for (low
= 0, high
= be32_to_cpu(btp
->count
) - 1; low
<= high
; ) {
418 mid
= (low
+ high
) >> 1;
419 if ((hash
= be32_to_cpu(blp
[mid
].hashval
)) == args
->hashval
)
421 if (hash
< args
->hashval
)
426 while (mid
>= 0 && be32_to_cpu(blp
[mid
].hashval
) >= args
->hashval
) {
430 * No stale entries, will use enddup space to hold new leaf.
434 * Mark the space needed for the new leaf entry, now in use.
436 xfs_dir2_data_use_free(tp
, bp
, enddup
,
437 (xfs_dir2_data_aoff_t
)
438 ((char *)enddup
- (char *)hdr
+ be16_to_cpu(enddup
->length
) -
440 (xfs_dir2_data_aoff_t
)sizeof(*blp
),
441 &needlog
, &needscan
);
443 * Update the tail (entry count).
445 be32_add_cpu(&btp
->count
, 1);
447 * If we now need to rebuild the bestfree map, do so.
448 * This needs to happen before the next call to use_free.
451 xfs_dir2_data_freescan(mp
, hdr
, &needlog
);
455 * Adjust pointer to the first leaf entry, we're about to move
456 * the table up one to open up space for the new leaf entry.
457 * Then adjust our index to match.
462 memmove(blp
, &blp
[1], mid
* sizeof(*blp
));
467 * Use a stale leaf for our new entry.
472 blp
[lowstale
].address
!=
473 cpu_to_be32(XFS_DIR2_NULL_DATAPTR
);
476 for (highstale
= mid
+ 1;
477 highstale
< be32_to_cpu(btp
->count
) &&
478 blp
[highstale
].address
!=
479 cpu_to_be32(XFS_DIR2_NULL_DATAPTR
) &&
480 (lowstale
< 0 || mid
- lowstale
> highstale
- mid
);
484 * Move entries toward the low-numbered stale entry.
487 (highstale
== be32_to_cpu(btp
->count
) ||
488 mid
- lowstale
<= highstale
- mid
)) {
490 memmove(&blp
[lowstale
], &blp
[lowstale
+ 1],
491 (mid
- lowstale
) * sizeof(*blp
));
492 lfloglow
= MIN(lowstale
, lfloglow
);
493 lfloghigh
= MAX(mid
, lfloghigh
);
496 * Move entries toward the high-numbered stale entry.
499 ASSERT(highstale
< be32_to_cpu(btp
->count
));
502 memmove(&blp
[mid
+ 1], &blp
[mid
],
503 (highstale
- mid
) * sizeof(*blp
));
504 lfloglow
= MIN(mid
, lfloglow
);
505 lfloghigh
= MAX(highstale
, lfloghigh
);
507 be32_add_cpu(&btp
->stale
, -1);
510 * Point to the new data entry.
512 dep
= (xfs_dir2_data_entry_t
*)dup
;
514 * Fill in the leaf entry.
516 blp
[mid
].hashval
= cpu_to_be32(args
->hashval
);
517 blp
[mid
].address
= cpu_to_be32(xfs_dir2_byte_to_dataptr(mp
,
518 (char *)dep
- (char *)hdr
));
519 xfs_dir2_block_log_leaf(tp
, bp
, lfloglow
, lfloghigh
);
521 * Mark space for the data entry used.
523 xfs_dir2_data_use_free(tp
, bp
, dup
,
524 (xfs_dir2_data_aoff_t
)((char *)dup
- (char *)hdr
),
525 (xfs_dir2_data_aoff_t
)len
, &needlog
, &needscan
);
527 * Create the new data entry.
529 dep
->inumber
= cpu_to_be64(args
->inumber
);
530 dep
->namelen
= args
->namelen
;
531 memcpy(dep
->name
, args
->name
, args
->namelen
);
532 xfs_dir3_dirent_put_ftype(mp
, dep
, args
->filetype
);
533 tagp
= xfs_dir3_data_entry_tag_p(mp
, dep
);
534 *tagp
= cpu_to_be16((char *)dep
- (char *)hdr
);
536 * Clean up the bestfree array and log the header, tail, and entry.
539 xfs_dir2_data_freescan(mp
, hdr
, &needlog
);
541 xfs_dir2_data_log_header(tp
, bp
);
542 xfs_dir2_block_log_tail(tp
, bp
);
543 xfs_dir2_data_log_entry(tp
, bp
, dep
);
544 xfs_dir3_data_check(dp
, bp
);
549 * Log leaf entries from the block.
552 xfs_dir2_block_log_leaf(
553 xfs_trans_t
*tp
, /* transaction structure */
554 struct xfs_buf
*bp
, /* block buffer */
555 int first
, /* index of first logged leaf */
556 int last
) /* index of last logged leaf */
558 xfs_dir2_data_hdr_t
*hdr
= bp
->b_addr
;
559 xfs_dir2_leaf_entry_t
*blp
;
560 xfs_dir2_block_tail_t
*btp
;
562 btp
= xfs_dir2_block_tail_p(tp
->t_mountp
, hdr
);
563 blp
= xfs_dir2_block_leaf_p(btp
);
564 xfs_trans_log_buf(tp
, bp
, (uint
)((char *)&blp
[first
] - (char *)hdr
),
565 (uint
)((char *)&blp
[last
+ 1] - (char *)hdr
- 1));
569 * Log the block tail.
572 xfs_dir2_block_log_tail(
573 xfs_trans_t
*tp
, /* transaction structure */
574 struct xfs_buf
*bp
) /* block buffer */
576 xfs_dir2_data_hdr_t
*hdr
= bp
->b_addr
;
577 xfs_dir2_block_tail_t
*btp
;
579 btp
= xfs_dir2_block_tail_p(tp
->t_mountp
, hdr
);
580 xfs_trans_log_buf(tp
, bp
, (uint
)((char *)btp
- (char *)hdr
),
581 (uint
)((char *)(btp
+ 1) - (char *)hdr
- 1));
585 * Look up an entry in the block. This is the external routine,
586 * xfs_dir2_block_lookup_int does the real work.
589 xfs_dir2_block_lookup(
590 xfs_da_args_t
*args
) /* dir lookup arguments */
592 xfs_dir2_data_hdr_t
*hdr
; /* block header */
593 xfs_dir2_leaf_entry_t
*blp
; /* block leaf entries */
594 struct xfs_buf
*bp
; /* block buffer */
595 xfs_dir2_block_tail_t
*btp
; /* block tail */
596 xfs_dir2_data_entry_t
*dep
; /* block data entry */
597 xfs_inode_t
*dp
; /* incore inode */
598 int ent
; /* entry index */
599 int error
; /* error return value */
600 xfs_mount_t
*mp
; /* filesystem mount point */
602 trace_xfs_dir2_block_lookup(args
);
605 * Get the buffer, look up the entry.
606 * If not found (ENOENT) then return, have no buffer.
608 if ((error
= xfs_dir2_block_lookup_int(args
, &bp
, &ent
)))
613 xfs_dir3_data_check(dp
, bp
);
614 btp
= xfs_dir2_block_tail_p(mp
, hdr
);
615 blp
= xfs_dir2_block_leaf_p(btp
);
617 * Get the offset from the leaf entry, to point to the data.
619 dep
= (xfs_dir2_data_entry_t
*)((char *)hdr
+
620 xfs_dir2_dataptr_to_off(mp
, be32_to_cpu(blp
[ent
].address
)));
622 * Fill in inode number, CI name if appropriate, release the block.
624 args
->inumber
= be64_to_cpu(dep
->inumber
);
625 args
->filetype
= xfs_dir3_dirent_get_ftype(mp
, dep
);
626 error
= xfs_dir_cilookup_result(args
, dep
->name
, dep
->namelen
);
627 xfs_trans_brelse(args
->trans
, bp
);
628 return XFS_ERROR(error
);
632 * Internal block lookup routine.
634 static int /* error */
635 xfs_dir2_block_lookup_int(
636 xfs_da_args_t
*args
, /* dir lookup arguments */
637 struct xfs_buf
**bpp
, /* returned block buffer */
638 int *entno
) /* returned entry number */
640 xfs_dir2_dataptr_t addr
; /* data entry address */
641 xfs_dir2_data_hdr_t
*hdr
; /* block header */
642 xfs_dir2_leaf_entry_t
*blp
; /* block leaf entries */
643 struct xfs_buf
*bp
; /* block buffer */
644 xfs_dir2_block_tail_t
*btp
; /* block tail */
645 xfs_dir2_data_entry_t
*dep
; /* block data entry */
646 xfs_inode_t
*dp
; /* incore inode */
647 int error
; /* error return value */
648 xfs_dahash_t hash
; /* found hash value */
649 int high
; /* binary search high index */
650 int low
; /* binary search low index */
651 int mid
; /* binary search current idx */
652 xfs_mount_t
*mp
; /* filesystem mount point */
653 xfs_trans_t
*tp
; /* transaction pointer */
654 enum xfs_dacmp cmp
; /* comparison result */
660 error
= xfs_dir3_block_read(tp
, dp
, &bp
);
665 xfs_dir3_data_check(dp
, bp
);
666 btp
= xfs_dir2_block_tail_p(mp
, hdr
);
667 blp
= xfs_dir2_block_leaf_p(btp
);
669 * Loop doing a binary search for our hash value.
670 * Find our entry, ENOENT if it's not there.
672 for (low
= 0, high
= be32_to_cpu(btp
->count
) - 1; ; ) {
674 mid
= (low
+ high
) >> 1;
675 if ((hash
= be32_to_cpu(blp
[mid
].hashval
)) == args
->hashval
)
677 if (hash
< args
->hashval
)
682 ASSERT(args
->op_flags
& XFS_DA_OP_OKNOENT
);
683 xfs_trans_brelse(tp
, bp
);
684 return XFS_ERROR(ENOENT
);
688 * Back up to the first one with the right hash value.
690 while (mid
> 0 && be32_to_cpu(blp
[mid
- 1].hashval
) == args
->hashval
) {
694 * Now loop forward through all the entries with the
695 * right hash value looking for our name.
698 if ((addr
= be32_to_cpu(blp
[mid
].address
)) == XFS_DIR2_NULL_DATAPTR
)
701 * Get pointer to the entry from the leaf.
703 dep
= (xfs_dir2_data_entry_t
*)
704 ((char *)hdr
+ xfs_dir2_dataptr_to_off(mp
, addr
));
706 * Compare name and if it's an exact match, return the index
707 * and buffer. If it's the first case-insensitive match, store
708 * the index and buffer and continue looking for an exact match.
710 cmp
= mp
->m_dirnameops
->compname(args
, dep
->name
, dep
->namelen
);
711 if (cmp
!= XFS_CMP_DIFFERENT
&& cmp
!= args
->cmpresult
) {
712 args
->cmpresult
= cmp
;
715 if (cmp
== XFS_CMP_EXACT
)
718 } while (++mid
< be32_to_cpu(btp
->count
) &&
719 be32_to_cpu(blp
[mid
].hashval
) == hash
);
721 ASSERT(args
->op_flags
& XFS_DA_OP_OKNOENT
);
723 * Here, we can only be doing a lookup (not a rename or replace).
724 * If a case-insensitive match was found earlier, return success.
726 if (args
->cmpresult
== XFS_CMP_CASE
)
729 * No match, release the buffer and return ENOENT.
731 xfs_trans_brelse(tp
, bp
);
732 return XFS_ERROR(ENOENT
);
736 * Remove an entry from a block format directory.
737 * If that makes the block small enough to fit in shortform, transform it.
740 xfs_dir2_block_removename(
741 xfs_da_args_t
*args
) /* directory operation args */
743 xfs_dir2_data_hdr_t
*hdr
; /* block header */
744 xfs_dir2_leaf_entry_t
*blp
; /* block leaf pointer */
745 struct xfs_buf
*bp
; /* block buffer */
746 xfs_dir2_block_tail_t
*btp
; /* block tail */
747 xfs_dir2_data_entry_t
*dep
; /* block data entry */
748 xfs_inode_t
*dp
; /* incore inode */
749 int ent
; /* block leaf entry index */
750 int error
; /* error return value */
751 xfs_mount_t
*mp
; /* filesystem mount point */
752 int needlog
; /* need to log block header */
753 int needscan
; /* need to fixup bestfree */
754 xfs_dir2_sf_hdr_t sfh
; /* shortform header */
755 int size
; /* shortform size */
756 xfs_trans_t
*tp
; /* transaction pointer */
758 trace_xfs_dir2_block_removename(args
);
761 * Look up the entry in the block. Gets the buffer and entry index.
762 * It will always be there, the vnodeops level does a lookup first.
764 if ((error
= xfs_dir2_block_lookup_int(args
, &bp
, &ent
))) {
771 btp
= xfs_dir2_block_tail_p(mp
, hdr
);
772 blp
= xfs_dir2_block_leaf_p(btp
);
774 * Point to the data entry using the leaf entry.
776 dep
= (xfs_dir2_data_entry_t
*)
777 ((char *)hdr
+ xfs_dir2_dataptr_to_off(mp
, be32_to_cpu(blp
[ent
].address
)));
779 * Mark the data entry's space free.
781 needlog
= needscan
= 0;
782 xfs_dir2_data_make_free(tp
, bp
,
783 (xfs_dir2_data_aoff_t
)((char *)dep
- (char *)hdr
),
784 xfs_dir3_data_entsize(mp
, dep
->namelen
), &needlog
, &needscan
);
786 * Fix up the block tail.
788 be32_add_cpu(&btp
->stale
, 1);
789 xfs_dir2_block_log_tail(tp
, bp
);
791 * Remove the leaf entry by marking it stale.
793 blp
[ent
].address
= cpu_to_be32(XFS_DIR2_NULL_DATAPTR
);
794 xfs_dir2_block_log_leaf(tp
, bp
, ent
, ent
);
796 * Fix up bestfree, log the header if necessary.
799 xfs_dir2_data_freescan(mp
, hdr
, &needlog
);
801 xfs_dir2_data_log_header(tp
, bp
);
802 xfs_dir3_data_check(dp
, bp
);
804 * See if the size as a shortform is good enough.
806 size
= xfs_dir2_block_sfsize(dp
, hdr
, &sfh
);
807 if (size
> XFS_IFORK_DSIZE(dp
))
811 * If it works, do the conversion.
813 return xfs_dir2_block_to_sf(args
, bp
, size
, &sfh
);
817 * Replace an entry in a V2 block directory.
818 * Change the inode number to the new value.
821 xfs_dir2_block_replace(
822 xfs_da_args_t
*args
) /* directory operation args */
824 xfs_dir2_data_hdr_t
*hdr
; /* block header */
825 xfs_dir2_leaf_entry_t
*blp
; /* block leaf entries */
826 struct xfs_buf
*bp
; /* block buffer */
827 xfs_dir2_block_tail_t
*btp
; /* block tail */
828 xfs_dir2_data_entry_t
*dep
; /* block data entry */
829 xfs_inode_t
*dp
; /* incore inode */
830 int ent
; /* leaf entry index */
831 int error
; /* error return value */
832 xfs_mount_t
*mp
; /* filesystem mount point */
834 trace_xfs_dir2_block_replace(args
);
837 * Lookup the entry in the directory. Get buffer and entry index.
838 * This will always succeed since the caller has already done a lookup.
840 if ((error
= xfs_dir2_block_lookup_int(args
, &bp
, &ent
))) {
846 btp
= xfs_dir2_block_tail_p(mp
, hdr
);
847 blp
= xfs_dir2_block_leaf_p(btp
);
849 * Point to the data entry we need to change.
851 dep
= (xfs_dir2_data_entry_t
*)
852 ((char *)hdr
+ xfs_dir2_dataptr_to_off(mp
, be32_to_cpu(blp
[ent
].address
)));
853 ASSERT(be64_to_cpu(dep
->inumber
) != args
->inumber
);
855 * Change the inode number to the new value.
857 dep
->inumber
= cpu_to_be64(args
->inumber
);
858 xfs_dir3_dirent_put_ftype(mp
, dep
, args
->filetype
);
859 xfs_dir2_data_log_entry(args
->trans
, bp
, dep
);
860 xfs_dir3_data_check(dp
, bp
);
865 * Qsort comparison routine for the block leaf entries.
867 static int /* sort order */
869 const void *a
, /* first leaf entry */
870 const void *b
) /* second leaf entry */
872 const xfs_dir2_leaf_entry_t
*la
; /* first leaf entry */
873 const xfs_dir2_leaf_entry_t
*lb
; /* second leaf entry */
877 return be32_to_cpu(la
->hashval
) < be32_to_cpu(lb
->hashval
) ? -1 :
878 (be32_to_cpu(la
->hashval
) > be32_to_cpu(lb
->hashval
) ? 1 : 0);
882 * Convert a V2 leaf directory to a V2 block directory if possible.
885 xfs_dir2_leaf_to_block(
886 xfs_da_args_t
*args
, /* operation arguments */
887 struct xfs_buf
*lbp
, /* leaf buffer */
888 struct xfs_buf
*dbp
) /* data buffer */
890 __be16
*bestsp
; /* leaf bests table */
891 xfs_dir2_data_hdr_t
*hdr
; /* block header */
892 xfs_dir2_block_tail_t
*btp
; /* block tail */
893 xfs_inode_t
*dp
; /* incore directory inode */
894 xfs_dir2_data_unused_t
*dup
; /* unused data entry */
895 int error
; /* error return value */
896 int from
; /* leaf from index */
897 xfs_dir2_leaf_t
*leaf
; /* leaf structure */
898 xfs_dir2_leaf_entry_t
*lep
; /* leaf entry */
899 xfs_dir2_leaf_tail_t
*ltp
; /* leaf tail structure */
900 xfs_mount_t
*mp
; /* file system mount point */
901 int needlog
; /* need to log data header */
902 int needscan
; /* need to scan for bestfree */
903 xfs_dir2_sf_hdr_t sfh
; /* shortform header */
904 int size
; /* bytes used */
905 __be16
*tagp
; /* end of entry (tag) */
906 int to
; /* block/leaf to index */
907 xfs_trans_t
*tp
; /* transaction pointer */
908 struct xfs_dir2_leaf_entry
*ents
;
909 struct xfs_dir3_icleaf_hdr leafhdr
;
911 trace_xfs_dir2_leaf_to_block(args
);
917 xfs_dir3_leaf_hdr_from_disk(&leafhdr
, leaf
);
918 ents
= xfs_dir3_leaf_ents_p(leaf
);
919 ltp
= xfs_dir2_leaf_tail_p(mp
, leaf
);
921 ASSERT(leafhdr
.magic
== XFS_DIR2_LEAF1_MAGIC
||
922 leafhdr
.magic
== XFS_DIR3_LEAF1_MAGIC
);
924 * If there are data blocks other than the first one, take this
925 * opportunity to remove trailing empty data blocks that may have
926 * been left behind during no-space-reservation operations.
927 * These will show up in the leaf bests table.
929 while (dp
->i_d
.di_size
> mp
->m_dirblksize
) {
932 hdrsz
= xfs_dir3_data_hdr_size(xfs_sb_version_hascrc(&mp
->m_sb
));
933 bestsp
= xfs_dir2_leaf_bests_p(ltp
);
934 if (be16_to_cpu(bestsp
[be32_to_cpu(ltp
->bestcount
) - 1]) ==
935 mp
->m_dirblksize
- hdrsz
) {
937 xfs_dir2_leaf_trim_data(args
, lbp
,
938 (xfs_dir2_db_t
)(be32_to_cpu(ltp
->bestcount
) - 1))))
944 * Read the data block if we don't already have it, give up if it fails.
947 error
= xfs_dir3_data_read(tp
, dp
, mp
->m_dirdatablk
, -1, &dbp
);
952 ASSERT(hdr
->magic
== cpu_to_be32(XFS_DIR2_DATA_MAGIC
) ||
953 hdr
->magic
== cpu_to_be32(XFS_DIR3_DATA_MAGIC
));
956 * Size of the "leaf" area in the block.
958 size
= (uint
)sizeof(xfs_dir2_block_tail_t
) +
959 (uint
)sizeof(*lep
) * (leafhdr
.count
- leafhdr
.stale
);
961 * Look at the last data entry.
963 tagp
= (__be16
*)((char *)hdr
+ mp
->m_dirblksize
) - 1;
964 dup
= (xfs_dir2_data_unused_t
*)((char *)hdr
+ be16_to_cpu(*tagp
));
966 * If it's not free or is too short we can't do it.
968 if (be16_to_cpu(dup
->freetag
) != XFS_DIR2_DATA_FREE_TAG
||
969 be16_to_cpu(dup
->length
) < size
)
973 * Start converting it to block form.
975 xfs_dir3_block_init(mp
, tp
, dbp
, dp
);
980 * Use up the space at the end of the block (blp/btp).
982 xfs_dir2_data_use_free(tp
, dbp
, dup
, mp
->m_dirblksize
- size
, size
,
983 &needlog
, &needscan
);
985 * Initialize the block tail.
987 btp
= xfs_dir2_block_tail_p(mp
, hdr
);
988 btp
->count
= cpu_to_be32(leafhdr
.count
- leafhdr
.stale
);
990 xfs_dir2_block_log_tail(tp
, dbp
);
992 * Initialize the block leaf area. We compact out stale entries.
994 lep
= xfs_dir2_block_leaf_p(btp
);
995 for (from
= to
= 0; from
< leafhdr
.count
; from
++) {
996 if (ents
[from
].address
== cpu_to_be32(XFS_DIR2_NULL_DATAPTR
))
998 lep
[to
++] = ents
[from
];
1000 ASSERT(to
== be32_to_cpu(btp
->count
));
1001 xfs_dir2_block_log_leaf(tp
, dbp
, 0, be32_to_cpu(btp
->count
) - 1);
1003 * Scan the bestfree if we need it and log the data block header.
1006 xfs_dir2_data_freescan(mp
, hdr
, &needlog
);
1008 xfs_dir2_data_log_header(tp
, dbp
);
1010 * Pitch the old leaf block.
1012 error
= xfs_da_shrink_inode(args
, mp
->m_dirleafblk
, lbp
);
1017 * Now see if the resulting block can be shrunken to shortform.
1019 size
= xfs_dir2_block_sfsize(dp
, hdr
, &sfh
);
1020 if (size
> XFS_IFORK_DSIZE(dp
))
1023 return xfs_dir2_block_to_sf(args
, dbp
, size
, &sfh
);
1027 * Convert the shortform directory to block form.
1030 xfs_dir2_sf_to_block(
1031 xfs_da_args_t
*args
) /* operation arguments */
1033 xfs_dir2_db_t blkno
; /* dir-relative block # (0) */
1034 xfs_dir2_data_hdr_t
*hdr
; /* block header */
1035 xfs_dir2_leaf_entry_t
*blp
; /* block leaf entries */
1036 struct xfs_buf
*bp
; /* block buffer */
1037 xfs_dir2_block_tail_t
*btp
; /* block tail pointer */
1038 xfs_dir2_data_entry_t
*dep
; /* data entry pointer */
1039 xfs_inode_t
*dp
; /* incore directory inode */
1040 int dummy
; /* trash */
1041 xfs_dir2_data_unused_t
*dup
; /* unused entry pointer */
1042 int endoffset
; /* end of data objects */
1043 int error
; /* error return value */
1045 xfs_mount_t
*mp
; /* filesystem mount point */
1046 int needlog
; /* need to log block header */
1047 int needscan
; /* need to scan block freespc */
1048 int newoffset
; /* offset from current entry */
1049 int offset
; /* target block offset */
1050 xfs_dir2_sf_entry_t
*sfep
; /* sf entry pointer */
1051 xfs_dir2_sf_hdr_t
*oldsfp
; /* old shortform header */
1052 xfs_dir2_sf_hdr_t
*sfp
; /* shortform header */
1053 __be16
*tagp
; /* end of data entry */
1054 xfs_trans_t
*tp
; /* transaction pointer */
1055 struct xfs_name name
;
1056 struct xfs_ifork
*ifp
;
1058 trace_xfs_dir2_sf_to_block(args
);
1063 ifp
= XFS_IFORK_PTR(dp
, XFS_DATA_FORK
);
1064 ASSERT(ifp
->if_flags
& XFS_IFINLINE
);
1066 * Bomb out if the shortform directory is way too short.
1068 if (dp
->i_d
.di_size
< offsetof(xfs_dir2_sf_hdr_t
, parent
)) {
1069 ASSERT(XFS_FORCED_SHUTDOWN(mp
));
1070 return XFS_ERROR(EIO
);
1073 oldsfp
= (xfs_dir2_sf_hdr_t
*)ifp
->if_u1
.if_data
;
1075 ASSERT(ifp
->if_bytes
== dp
->i_d
.di_size
);
1076 ASSERT(ifp
->if_u1
.if_data
!= NULL
);
1077 ASSERT(dp
->i_d
.di_size
>= xfs_dir2_sf_hdr_size(oldsfp
->i8count
));
1078 ASSERT(dp
->i_d
.di_nextents
== 0);
1081 * Copy the directory into a temporary buffer.
1082 * Then pitch the incore inode data so we can make extents.
1084 sfp
= kmem_alloc(ifp
->if_bytes
, KM_SLEEP
);
1085 memcpy(sfp
, oldsfp
, ifp
->if_bytes
);
1087 xfs_idata_realloc(dp
, -ifp
->if_bytes
, XFS_DATA_FORK
);
1088 xfs_bmap_local_to_extents_empty(dp
, XFS_DATA_FORK
);
1089 dp
->i_d
.di_size
= 0;
1092 * Add block 0 to the inode.
1094 error
= xfs_dir2_grow_inode(args
, XFS_DIR2_DATA_SPACE
, &blkno
);
1100 * Initialize the data block, then convert it to block format.
1102 error
= xfs_dir3_data_init(args
, blkno
, &bp
);
1107 xfs_dir3_block_init(mp
, tp
, bp
, dp
);
1111 * Compute size of block "tail" area.
1113 i
= (uint
)sizeof(*btp
) +
1114 (sfp
->count
+ 2) * (uint
)sizeof(xfs_dir2_leaf_entry_t
);
1116 * The whole thing is initialized to free by the init routine.
1117 * Say we're using the leaf and tail area.
1119 dup
= xfs_dir3_data_unused_p(hdr
);
1120 needlog
= needscan
= 0;
1121 xfs_dir2_data_use_free(tp
, bp
, dup
, mp
->m_dirblksize
- i
, i
, &needlog
,
1123 ASSERT(needscan
== 0);
1127 btp
= xfs_dir2_block_tail_p(mp
, hdr
);
1128 btp
->count
= cpu_to_be32(sfp
->count
+ 2); /* ., .. */
1130 blp
= xfs_dir2_block_leaf_p(btp
);
1131 endoffset
= (uint
)((char *)blp
- (char *)hdr
);
1133 * Remove the freespace, we'll manage it.
1135 xfs_dir2_data_use_free(tp
, bp
, dup
,
1136 (xfs_dir2_data_aoff_t
)((char *)dup
- (char *)hdr
),
1137 be16_to_cpu(dup
->length
), &needlog
, &needscan
);
1139 * Create entry for .
1141 dep
= xfs_dir3_data_dot_entry_p(mp
, hdr
);
1142 dep
->inumber
= cpu_to_be64(dp
->i_ino
);
1145 xfs_dir3_dirent_put_ftype(mp
, dep
, XFS_DIR3_FT_DIR
);
1146 tagp
= xfs_dir3_data_entry_tag_p(mp
, dep
);
1147 *tagp
= cpu_to_be16((char *)dep
- (char *)hdr
);
1148 xfs_dir2_data_log_entry(tp
, bp
, dep
);
1149 blp
[0].hashval
= cpu_to_be32(xfs_dir_hash_dot
);
1150 blp
[0].address
= cpu_to_be32(xfs_dir2_byte_to_dataptr(mp
,
1151 (char *)dep
- (char *)hdr
));
1153 * Create entry for ..
1155 dep
= xfs_dir3_data_dotdot_entry_p(mp
, hdr
);
1156 dep
->inumber
= cpu_to_be64(xfs_dir2_sf_get_parent_ino(sfp
));
1158 dep
->name
[0] = dep
->name
[1] = '.';
1159 xfs_dir3_dirent_put_ftype(mp
, dep
, XFS_DIR3_FT_DIR
);
1160 tagp
= xfs_dir3_data_entry_tag_p(mp
, dep
);
1161 *tagp
= cpu_to_be16((char *)dep
- (char *)hdr
);
1162 xfs_dir2_data_log_entry(tp
, bp
, dep
);
1163 blp
[1].hashval
= cpu_to_be32(xfs_dir_hash_dotdot
);
1164 blp
[1].address
= cpu_to_be32(xfs_dir2_byte_to_dataptr(mp
,
1165 (char *)dep
- (char *)hdr
));
1166 offset
= xfs_dir3_data_first_offset(mp
);
1168 * Loop over existing entries, stuff them in.
1174 sfep
= xfs_dir2_sf_firstentry(sfp
);
1176 * Need to preserve the existing offset values in the sf directory.
1177 * Insert holes (unused entries) where necessary.
1179 while (offset
< endoffset
) {
1181 * sfep is null when we reach the end of the list.
1184 newoffset
= endoffset
;
1186 newoffset
= xfs_dir2_sf_get_offset(sfep
);
1188 * There should be a hole here, make one.
1190 if (offset
< newoffset
) {
1191 dup
= (xfs_dir2_data_unused_t
*)((char *)hdr
+ offset
);
1192 dup
->freetag
= cpu_to_be16(XFS_DIR2_DATA_FREE_TAG
);
1193 dup
->length
= cpu_to_be16(newoffset
- offset
);
1194 *xfs_dir2_data_unused_tag_p(dup
) = cpu_to_be16(
1195 ((char *)dup
- (char *)hdr
));
1196 xfs_dir2_data_log_unused(tp
, bp
, dup
);
1197 xfs_dir2_data_freeinsert(hdr
, dup
, &dummy
);
1198 offset
+= be16_to_cpu(dup
->length
);
1202 * Copy a real entry.
1204 dep
= (xfs_dir2_data_entry_t
*)((char *)hdr
+ newoffset
);
1205 dep
->inumber
= cpu_to_be64(xfs_dir3_sfe_get_ino(mp
, sfp
, sfep
));
1206 dep
->namelen
= sfep
->namelen
;
1207 xfs_dir3_dirent_put_ftype(mp
, dep
,
1208 xfs_dir3_sfe_get_ftype(mp
, sfp
, sfep
));
1209 memcpy(dep
->name
, sfep
->name
, dep
->namelen
);
1210 tagp
= xfs_dir3_data_entry_tag_p(mp
, dep
);
1211 *tagp
= cpu_to_be16((char *)dep
- (char *)hdr
);
1212 xfs_dir2_data_log_entry(tp
, bp
, dep
);
1213 name
.name
= sfep
->name
;
1214 name
.len
= sfep
->namelen
;
1215 blp
[2 + i
].hashval
= cpu_to_be32(mp
->m_dirnameops
->
1217 blp
[2 + i
].address
= cpu_to_be32(xfs_dir2_byte_to_dataptr(mp
,
1218 (char *)dep
- (char *)hdr
));
1219 offset
= (int)((char *)(tagp
+ 1) - (char *)hdr
);
1220 if (++i
== sfp
->count
)
1223 sfep
= xfs_dir3_sf_nextentry(mp
, sfp
, sfep
);
1225 /* Done with the temporary buffer */
1228 * Sort the leaf entries by hash value.
1230 xfs_sort(blp
, be32_to_cpu(btp
->count
), sizeof(*blp
), xfs_dir2_block_sort
);
1232 * Log the leaf entry area and tail.
1233 * Already logged the header in data_init, ignore needlog.
1235 ASSERT(needscan
== 0);
1236 xfs_dir2_block_log_leaf(tp
, bp
, 0, be32_to_cpu(btp
->count
) - 1);
1237 xfs_dir2_block_log_tail(tp
, bp
);
1238 xfs_dir3_data_check(dp
, bp
);