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_verify_cksum(bp
->b_addr
, BBTOB(bp
->b_length
),
75 XFS_DIR3_DATA_CRC_OFF
)) ||
76 !xfs_dir3_block_verify(bp
)) {
77 XFS_CORRUPTION_ERROR(__func__
, XFS_ERRLEVEL_LOW
, mp
, bp
->b_addr
);
78 xfs_buf_ioerror(bp
, EFSCORRUPTED
);
83 xfs_dir3_block_write_verify(
86 struct xfs_mount
*mp
= bp
->b_target
->bt_mount
;
87 struct xfs_buf_log_item
*bip
= bp
->b_fspriv
;
88 struct xfs_dir3_blk_hdr
*hdr3
= bp
->b_addr
;
90 if (!xfs_dir3_block_verify(bp
)) {
91 XFS_CORRUPTION_ERROR(__func__
, XFS_ERRLEVEL_LOW
, mp
, bp
->b_addr
);
92 xfs_buf_ioerror(bp
, EFSCORRUPTED
);
96 if (!xfs_sb_version_hascrc(&mp
->m_sb
))
100 hdr3
->lsn
= cpu_to_be64(bip
->bli_item
.li_lsn
);
102 xfs_update_cksum(bp
->b_addr
, BBTOB(bp
->b_length
), XFS_DIR3_DATA_CRC_OFF
);
105 const struct xfs_buf_ops xfs_dir3_block_buf_ops
= {
106 .verify_read
= xfs_dir3_block_read_verify
,
107 .verify_write
= xfs_dir3_block_write_verify
,
112 struct xfs_trans
*tp
,
113 struct xfs_inode
*dp
,
114 struct xfs_buf
**bpp
)
116 struct xfs_mount
*mp
= dp
->i_mount
;
119 err
= xfs_da_read_buf(tp
, dp
, mp
->m_dirdatablk
, -1, bpp
,
120 XFS_DATA_FORK
, &xfs_dir3_block_buf_ops
);
122 xfs_trans_buf_set_type(tp
, *bpp
, XFS_BLF_DIR_BLOCK_BUF
);
128 struct xfs_mount
*mp
,
129 struct xfs_trans
*tp
,
131 struct xfs_inode
*dp
)
133 struct xfs_dir3_blk_hdr
*hdr3
= bp
->b_addr
;
135 bp
->b_ops
= &xfs_dir3_block_buf_ops
;
136 xfs_trans_buf_set_type(tp
, bp
, XFS_BLF_DIR_BLOCK_BUF
);
138 if (xfs_sb_version_hascrc(&mp
->m_sb
)) {
139 memset(hdr3
, 0, sizeof(*hdr3
));
140 hdr3
->magic
= cpu_to_be32(XFS_DIR3_BLOCK_MAGIC
);
141 hdr3
->blkno
= cpu_to_be64(bp
->b_bn
);
142 hdr3
->owner
= cpu_to_be64(dp
->i_ino
);
143 uuid_copy(&hdr3
->uuid
, &mp
->m_sb
.sb_uuid
);
147 hdr3
->magic
= cpu_to_be32(XFS_DIR2_BLOCK_MAGIC
);
151 xfs_dir2_block_need_space(
152 struct xfs_dir2_data_hdr
*hdr
,
153 struct xfs_dir2_block_tail
*btp
,
154 struct xfs_dir2_leaf_entry
*blp
,
156 struct xfs_dir2_data_unused
**dupp
,
157 struct xfs_dir2_data_unused
**enddupp
,
161 struct xfs_dir2_data_free
*bf
;
163 struct xfs_dir2_data_unused
*dup
= NULL
;
164 struct xfs_dir2_data_unused
*enddup
= NULL
;
167 bf
= xfs_dir3_data_bestfree_p(hdr
);
170 * If there are stale entries we'll use one for the leaf.
173 if (be16_to_cpu(bf
[0].length
) >= len
) {
175 * The biggest entry enough to avoid compaction.
177 dup
= (xfs_dir2_data_unused_t
*)
178 ((char *)hdr
+ be16_to_cpu(bf
[0].offset
));
183 * Will need to compact to make this work.
184 * Tag just before the first leaf entry.
187 tagp
= (__be16
*)blp
- 1;
189 /* Data object just before the first leaf entry. */
190 dup
= (xfs_dir2_data_unused_t
*)((char *)hdr
+ be16_to_cpu(*tagp
));
193 * If it's not free then the data will go where the
194 * leaf data starts now, if it works at all.
196 if (be16_to_cpu(dup
->freetag
) == XFS_DIR2_DATA_FREE_TAG
) {
197 if (be16_to_cpu(dup
->length
) + (be32_to_cpu(btp
->stale
) - 1) *
198 (uint
)sizeof(*blp
) < len
)
200 } else if ((be32_to_cpu(btp
->stale
) - 1) * (uint
)sizeof(*blp
) < len
)
203 dup
= (xfs_dir2_data_unused_t
*)blp
;
208 * no stale entries, so just use free space.
209 * Tag just before the first leaf entry.
211 tagp
= (__be16
*)blp
- 1;
213 /* Data object just before the first leaf entry. */
214 enddup
= (xfs_dir2_data_unused_t
*)((char *)hdr
+ be16_to_cpu(*tagp
));
217 * If it's not free then can't do this add without cleaning up:
218 * the space before the first leaf entry needs to be free so it
219 * can be expanded to hold the pointer to the new entry.
221 if (be16_to_cpu(enddup
->freetag
) == XFS_DIR2_DATA_FREE_TAG
) {
223 * Check out the biggest freespace and see if it's the same one.
225 dup
= (xfs_dir2_data_unused_t
*)
226 ((char *)hdr
+ be16_to_cpu(bf
[0].offset
));
229 * Not the same free entry, just check its length.
231 if (be16_to_cpu(dup
->length
) < len
)
237 * It is the biggest freespace, can it hold the leaf too?
239 if (be16_to_cpu(dup
->length
) < len
+ (uint
)sizeof(*blp
)) {
241 * Yes, use the second-largest entry instead if it works.
243 if (be16_to_cpu(bf
[1].length
) >= len
)
244 dup
= (xfs_dir2_data_unused_t
*)
245 ((char *)hdr
+ be16_to_cpu(bf
[1].offset
));
257 * compact the leaf entries.
258 * Leave the highest-numbered stale entry stale.
259 * XXX should be the one closest to mid but mid is not yet computed.
262 xfs_dir2_block_compact(
263 struct xfs_trans
*tp
,
265 struct xfs_dir2_data_hdr
*hdr
,
266 struct xfs_dir2_block_tail
*btp
,
267 struct xfs_dir2_leaf_entry
*blp
,
272 int fromidx
; /* source leaf index */
273 int toidx
; /* target leaf index */
275 int highstale
; /* high stale index */
277 fromidx
= toidx
= be32_to_cpu(btp
->count
) - 1;
278 highstale
= *lfloghigh
= -1;
279 for (; fromidx
>= 0; fromidx
--) {
280 if (blp
[fromidx
].address
== cpu_to_be32(XFS_DIR2_NULL_DATAPTR
)) {
284 if (*lfloghigh
== -1)
290 blp
[toidx
] = blp
[fromidx
];
293 *lfloglow
= toidx
+ 1 - (be32_to_cpu(btp
->stale
) - 1);
294 *lfloghigh
-= be32_to_cpu(btp
->stale
) - 1;
295 be32_add_cpu(&btp
->count
, -(be32_to_cpu(btp
->stale
) - 1));
296 xfs_dir2_data_make_free(tp
, bp
,
297 (xfs_dir2_data_aoff_t
)((char *)blp
- (char *)hdr
),
298 (xfs_dir2_data_aoff_t
)((be32_to_cpu(btp
->stale
) - 1) * sizeof(*blp
)),
300 blp
+= be32_to_cpu(btp
->stale
) - 1;
301 btp
->stale
= cpu_to_be32(1);
303 * If we now need to rebuild the bestfree map, do so.
304 * This needs to happen before the next call to use_free.
307 xfs_dir2_data_freescan(tp
->t_mountp
, hdr
, needlog
);
311 * Add an entry to a block directory.
314 xfs_dir2_block_addname(
315 xfs_da_args_t
*args
) /* directory op arguments */
317 xfs_dir2_data_hdr_t
*hdr
; /* block header */
318 xfs_dir2_leaf_entry_t
*blp
; /* block leaf entries */
319 struct xfs_buf
*bp
; /* buffer for block */
320 xfs_dir2_block_tail_t
*btp
; /* block tail */
321 int compact
; /* need to compact leaf ents */
322 xfs_dir2_data_entry_t
*dep
; /* block data entry */
323 xfs_inode_t
*dp
; /* directory inode */
324 xfs_dir2_data_unused_t
*dup
; /* block unused entry */
325 int error
; /* error return value */
326 xfs_dir2_data_unused_t
*enddup
=NULL
; /* unused at end of data */
327 xfs_dahash_t hash
; /* hash value of found entry */
328 int high
; /* high index for binary srch */
329 int highstale
; /* high stale index */
330 int lfloghigh
=0; /* last final leaf to log */
331 int lfloglow
=0; /* first final leaf to log */
332 int len
; /* length of the new entry */
333 int low
; /* low index for binary srch */
334 int lowstale
; /* low stale index */
335 int mid
=0; /* midpoint for binary srch */
336 xfs_mount_t
*mp
; /* filesystem mount point */
337 int needlog
; /* need to log header */
338 int needscan
; /* need to rescan freespace */
339 __be16
*tagp
; /* pointer to tag value */
340 xfs_trans_t
*tp
; /* transaction structure */
342 trace_xfs_dir2_block_addname(args
);
348 /* Read the (one and only) directory block into bp. */
349 error
= xfs_dir3_block_read(tp
, dp
, &bp
);
353 len
= xfs_dir2_data_entsize(args
->namelen
);
356 * Set up pointers to parts of the block.
359 btp
= xfs_dir2_block_tail_p(mp
, hdr
);
360 blp
= xfs_dir2_block_leaf_p(btp
);
363 * Find out if we can reuse stale entries or whether we need extra
364 * space for entry and new leaf.
366 xfs_dir2_block_need_space(hdr
, btp
, blp
, &tagp
, &dup
,
367 &enddup
, &compact
, len
);
370 * Done everything we need for a space check now.
372 if (args
->op_flags
& XFS_DA_OP_JUSTCHECK
) {
373 xfs_trans_brelse(tp
, bp
);
375 return XFS_ERROR(ENOSPC
);
380 * If we don't have space for the new entry & leaf ...
383 /* Don't have a space reservation: return no-space. */
384 if (args
->total
== 0)
385 return XFS_ERROR(ENOSPC
);
387 * Convert to the next larger format.
388 * Then add the new entry in that format.
390 error
= xfs_dir2_block_to_leaf(args
, bp
);
393 return xfs_dir2_leaf_addname(args
);
396 needlog
= needscan
= 0;
399 * If need to compact the leaf entries, do it now.
402 xfs_dir2_block_compact(tp
, bp
, hdr
, btp
, blp
, &needlog
,
403 &lfloghigh
, &lfloglow
);
404 /* recalculate blp post-compaction */
405 blp
= xfs_dir2_block_leaf_p(btp
);
406 } else if (btp
->stale
) {
408 * Set leaf logging boundaries to impossible state.
409 * For the no-stale case they're set explicitly.
411 lfloglow
= be32_to_cpu(btp
->count
);
416 * Find the slot that's first lower than our hash value, -1 if none.
418 for (low
= 0, high
= be32_to_cpu(btp
->count
) - 1; low
<= high
; ) {
419 mid
= (low
+ high
) >> 1;
420 if ((hash
= be32_to_cpu(blp
[mid
].hashval
)) == args
->hashval
)
422 if (hash
< args
->hashval
)
427 while (mid
>= 0 && be32_to_cpu(blp
[mid
].hashval
) >= args
->hashval
) {
431 * No stale entries, will use enddup space to hold new leaf.
435 * Mark the space needed for the new leaf entry, now in use.
437 xfs_dir2_data_use_free(tp
, bp
, enddup
,
438 (xfs_dir2_data_aoff_t
)
439 ((char *)enddup
- (char *)hdr
+ be16_to_cpu(enddup
->length
) -
441 (xfs_dir2_data_aoff_t
)sizeof(*blp
),
442 &needlog
, &needscan
);
444 * Update the tail (entry count).
446 be32_add_cpu(&btp
->count
, 1);
448 * If we now need to rebuild the bestfree map, do so.
449 * This needs to happen before the next call to use_free.
452 xfs_dir2_data_freescan(mp
, hdr
, &needlog
);
456 * Adjust pointer to the first leaf entry, we're about to move
457 * the table up one to open up space for the new leaf entry.
458 * Then adjust our index to match.
463 memmove(blp
, &blp
[1], mid
* sizeof(*blp
));
468 * Use a stale leaf for our new entry.
473 blp
[lowstale
].address
!=
474 cpu_to_be32(XFS_DIR2_NULL_DATAPTR
);
477 for (highstale
= mid
+ 1;
478 highstale
< be32_to_cpu(btp
->count
) &&
479 blp
[highstale
].address
!=
480 cpu_to_be32(XFS_DIR2_NULL_DATAPTR
) &&
481 (lowstale
< 0 || mid
- lowstale
> highstale
- mid
);
485 * Move entries toward the low-numbered stale entry.
488 (highstale
== be32_to_cpu(btp
->count
) ||
489 mid
- lowstale
<= highstale
- mid
)) {
491 memmove(&blp
[lowstale
], &blp
[lowstale
+ 1],
492 (mid
- lowstale
) * sizeof(*blp
));
493 lfloglow
= MIN(lowstale
, lfloglow
);
494 lfloghigh
= MAX(mid
, lfloghigh
);
497 * Move entries toward the high-numbered stale entry.
500 ASSERT(highstale
< be32_to_cpu(btp
->count
));
503 memmove(&blp
[mid
+ 1], &blp
[mid
],
504 (highstale
- mid
) * sizeof(*blp
));
505 lfloglow
= MIN(mid
, lfloglow
);
506 lfloghigh
= MAX(highstale
, lfloghigh
);
508 be32_add_cpu(&btp
->stale
, -1);
511 * Point to the new data entry.
513 dep
= (xfs_dir2_data_entry_t
*)dup
;
515 * Fill in the leaf entry.
517 blp
[mid
].hashval
= cpu_to_be32(args
->hashval
);
518 blp
[mid
].address
= cpu_to_be32(xfs_dir2_byte_to_dataptr(mp
,
519 (char *)dep
- (char *)hdr
));
520 xfs_dir2_block_log_leaf(tp
, bp
, lfloglow
, lfloghigh
);
522 * Mark space for the data entry used.
524 xfs_dir2_data_use_free(tp
, bp
, dup
,
525 (xfs_dir2_data_aoff_t
)((char *)dup
- (char *)hdr
),
526 (xfs_dir2_data_aoff_t
)len
, &needlog
, &needscan
);
528 * Create the new data entry.
530 dep
->inumber
= cpu_to_be64(args
->inumber
);
531 dep
->namelen
= args
->namelen
;
532 memcpy(dep
->name
, args
->name
, args
->namelen
);
533 tagp
= xfs_dir2_data_entry_tag_p(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 error
= xfs_dir_cilookup_result(args
, dep
->name
, dep
->namelen
);
626 xfs_trans_brelse(args
->trans
, bp
);
627 return XFS_ERROR(error
);
631 * Internal block lookup routine.
633 static int /* error */
634 xfs_dir2_block_lookup_int(
635 xfs_da_args_t
*args
, /* dir lookup arguments */
636 struct xfs_buf
**bpp
, /* returned block buffer */
637 int *entno
) /* returned entry number */
639 xfs_dir2_dataptr_t addr
; /* data entry address */
640 xfs_dir2_data_hdr_t
*hdr
; /* block header */
641 xfs_dir2_leaf_entry_t
*blp
; /* block leaf entries */
642 struct xfs_buf
*bp
; /* block buffer */
643 xfs_dir2_block_tail_t
*btp
; /* block tail */
644 xfs_dir2_data_entry_t
*dep
; /* block data entry */
645 xfs_inode_t
*dp
; /* incore inode */
646 int error
; /* error return value */
647 xfs_dahash_t hash
; /* found hash value */
648 int high
; /* binary search high index */
649 int low
; /* binary search low index */
650 int mid
; /* binary search current idx */
651 xfs_mount_t
*mp
; /* filesystem mount point */
652 xfs_trans_t
*tp
; /* transaction pointer */
653 enum xfs_dacmp cmp
; /* comparison result */
659 error
= xfs_dir3_block_read(tp
, dp
, &bp
);
664 xfs_dir3_data_check(dp
, bp
);
665 btp
= xfs_dir2_block_tail_p(mp
, hdr
);
666 blp
= xfs_dir2_block_leaf_p(btp
);
668 * Loop doing a binary search for our hash value.
669 * Find our entry, ENOENT if it's not there.
671 for (low
= 0, high
= be32_to_cpu(btp
->count
) - 1; ; ) {
673 mid
= (low
+ high
) >> 1;
674 if ((hash
= be32_to_cpu(blp
[mid
].hashval
)) == args
->hashval
)
676 if (hash
< args
->hashval
)
681 ASSERT(args
->op_flags
& XFS_DA_OP_OKNOENT
);
682 xfs_trans_brelse(tp
, bp
);
683 return XFS_ERROR(ENOENT
);
687 * Back up to the first one with the right hash value.
689 while (mid
> 0 && be32_to_cpu(blp
[mid
- 1].hashval
) == args
->hashval
) {
693 * Now loop forward through all the entries with the
694 * right hash value looking for our name.
697 if ((addr
= be32_to_cpu(blp
[mid
].address
)) == XFS_DIR2_NULL_DATAPTR
)
700 * Get pointer to the entry from the leaf.
702 dep
= (xfs_dir2_data_entry_t
*)
703 ((char *)hdr
+ xfs_dir2_dataptr_to_off(mp
, addr
));
705 * Compare name and if it's an exact match, return the index
706 * and buffer. If it's the first case-insensitive match, store
707 * the index and buffer and continue looking for an exact match.
709 cmp
= mp
->m_dirnameops
->compname(args
, dep
->name
, dep
->namelen
);
710 if (cmp
!= XFS_CMP_DIFFERENT
&& cmp
!= args
->cmpresult
) {
711 args
->cmpresult
= cmp
;
714 if (cmp
== XFS_CMP_EXACT
)
717 } while (++mid
< be32_to_cpu(btp
->count
) &&
718 be32_to_cpu(blp
[mid
].hashval
) == hash
);
720 ASSERT(args
->op_flags
& XFS_DA_OP_OKNOENT
);
722 * Here, we can only be doing a lookup (not a rename or replace).
723 * If a case-insensitive match was found earlier, return success.
725 if (args
->cmpresult
== XFS_CMP_CASE
)
728 * No match, release the buffer and return ENOENT.
730 xfs_trans_brelse(tp
, bp
);
731 return XFS_ERROR(ENOENT
);
735 * Remove an entry from a block format directory.
736 * If that makes the block small enough to fit in shortform, transform it.
739 xfs_dir2_block_removename(
740 xfs_da_args_t
*args
) /* directory operation args */
742 xfs_dir2_data_hdr_t
*hdr
; /* block header */
743 xfs_dir2_leaf_entry_t
*blp
; /* block leaf pointer */
744 struct xfs_buf
*bp
; /* block buffer */
745 xfs_dir2_block_tail_t
*btp
; /* block tail */
746 xfs_dir2_data_entry_t
*dep
; /* block data entry */
747 xfs_inode_t
*dp
; /* incore inode */
748 int ent
; /* block leaf entry index */
749 int error
; /* error return value */
750 xfs_mount_t
*mp
; /* filesystem mount point */
751 int needlog
; /* need to log block header */
752 int needscan
; /* need to fixup bestfree */
753 xfs_dir2_sf_hdr_t sfh
; /* shortform header */
754 int size
; /* shortform size */
755 xfs_trans_t
*tp
; /* transaction pointer */
757 trace_xfs_dir2_block_removename(args
);
760 * Look up the entry in the block. Gets the buffer and entry index.
761 * It will always be there, the vnodeops level does a lookup first.
763 if ((error
= xfs_dir2_block_lookup_int(args
, &bp
, &ent
))) {
770 btp
= xfs_dir2_block_tail_p(mp
, hdr
);
771 blp
= xfs_dir2_block_leaf_p(btp
);
773 * Point to the data entry using the leaf entry.
775 dep
= (xfs_dir2_data_entry_t
*)
776 ((char *)hdr
+ xfs_dir2_dataptr_to_off(mp
, be32_to_cpu(blp
[ent
].address
)));
778 * Mark the data entry's space free.
780 needlog
= needscan
= 0;
781 xfs_dir2_data_make_free(tp
, bp
,
782 (xfs_dir2_data_aoff_t
)((char *)dep
- (char *)hdr
),
783 xfs_dir2_data_entsize(dep
->namelen
), &needlog
, &needscan
);
785 * Fix up the block tail.
787 be32_add_cpu(&btp
->stale
, 1);
788 xfs_dir2_block_log_tail(tp
, bp
);
790 * Remove the leaf entry by marking it stale.
792 blp
[ent
].address
= cpu_to_be32(XFS_DIR2_NULL_DATAPTR
);
793 xfs_dir2_block_log_leaf(tp
, bp
, ent
, ent
);
795 * Fix up bestfree, log the header if necessary.
798 xfs_dir2_data_freescan(mp
, hdr
, &needlog
);
800 xfs_dir2_data_log_header(tp
, bp
);
801 xfs_dir3_data_check(dp
, bp
);
803 * See if the size as a shortform is good enough.
805 size
= xfs_dir2_block_sfsize(dp
, hdr
, &sfh
);
806 if (size
> XFS_IFORK_DSIZE(dp
))
810 * If it works, do the conversion.
812 return xfs_dir2_block_to_sf(args
, bp
, size
, &sfh
);
816 * Replace an entry in a V2 block directory.
817 * Change the inode number to the new value.
820 xfs_dir2_block_replace(
821 xfs_da_args_t
*args
) /* directory operation args */
823 xfs_dir2_data_hdr_t
*hdr
; /* block header */
824 xfs_dir2_leaf_entry_t
*blp
; /* block leaf entries */
825 struct xfs_buf
*bp
; /* block buffer */
826 xfs_dir2_block_tail_t
*btp
; /* block tail */
827 xfs_dir2_data_entry_t
*dep
; /* block data entry */
828 xfs_inode_t
*dp
; /* incore inode */
829 int ent
; /* leaf entry index */
830 int error
; /* error return value */
831 xfs_mount_t
*mp
; /* filesystem mount point */
833 trace_xfs_dir2_block_replace(args
);
836 * Lookup the entry in the directory. Get buffer and entry index.
837 * This will always succeed since the caller has already done a lookup.
839 if ((error
= xfs_dir2_block_lookup_int(args
, &bp
, &ent
))) {
845 btp
= xfs_dir2_block_tail_p(mp
, hdr
);
846 blp
= xfs_dir2_block_leaf_p(btp
);
848 * Point to the data entry we need to change.
850 dep
= (xfs_dir2_data_entry_t
*)
851 ((char *)hdr
+ xfs_dir2_dataptr_to_off(mp
, be32_to_cpu(blp
[ent
].address
)));
852 ASSERT(be64_to_cpu(dep
->inumber
) != args
->inumber
);
854 * Change the inode number to the new value.
856 dep
->inumber
= cpu_to_be64(args
->inumber
);
857 xfs_dir2_data_log_entry(args
->trans
, bp
, dep
);
858 xfs_dir3_data_check(dp
, bp
);
863 * Qsort comparison routine for the block leaf entries.
865 static int /* sort order */
867 const void *a
, /* first leaf entry */
868 const void *b
) /* second leaf entry */
870 const xfs_dir2_leaf_entry_t
*la
; /* first leaf entry */
871 const xfs_dir2_leaf_entry_t
*lb
; /* second leaf entry */
875 return be32_to_cpu(la
->hashval
) < be32_to_cpu(lb
->hashval
) ? -1 :
876 (be32_to_cpu(la
->hashval
) > be32_to_cpu(lb
->hashval
) ? 1 : 0);
880 * Convert a V2 leaf directory to a V2 block directory if possible.
883 xfs_dir2_leaf_to_block(
884 xfs_da_args_t
*args
, /* operation arguments */
885 struct xfs_buf
*lbp
, /* leaf buffer */
886 struct xfs_buf
*dbp
) /* data buffer */
888 __be16
*bestsp
; /* leaf bests table */
889 xfs_dir2_data_hdr_t
*hdr
; /* block header */
890 xfs_dir2_block_tail_t
*btp
; /* block tail */
891 xfs_inode_t
*dp
; /* incore directory inode */
892 xfs_dir2_data_unused_t
*dup
; /* unused data entry */
893 int error
; /* error return value */
894 int from
; /* leaf from index */
895 xfs_dir2_leaf_t
*leaf
; /* leaf structure */
896 xfs_dir2_leaf_entry_t
*lep
; /* leaf entry */
897 xfs_dir2_leaf_tail_t
*ltp
; /* leaf tail structure */
898 xfs_mount_t
*mp
; /* file system mount point */
899 int needlog
; /* need to log data header */
900 int needscan
; /* need to scan for bestfree */
901 xfs_dir2_sf_hdr_t sfh
; /* shortform header */
902 int size
; /* bytes used */
903 __be16
*tagp
; /* end of entry (tag) */
904 int to
; /* block/leaf to index */
905 xfs_trans_t
*tp
; /* transaction pointer */
906 struct xfs_dir2_leaf_entry
*ents
;
907 struct xfs_dir3_icleaf_hdr leafhdr
;
909 trace_xfs_dir2_leaf_to_block(args
);
915 xfs_dir3_leaf_hdr_from_disk(&leafhdr
, leaf
);
916 ents
= xfs_dir3_leaf_ents_p(leaf
);
917 ltp
= xfs_dir2_leaf_tail_p(mp
, leaf
);
919 ASSERT(leafhdr
.magic
== XFS_DIR2_LEAF1_MAGIC
||
920 leafhdr
.magic
== XFS_DIR3_LEAF1_MAGIC
);
922 * If there are data blocks other than the first one, take this
923 * opportunity to remove trailing empty data blocks that may have
924 * been left behind during no-space-reservation operations.
925 * These will show up in the leaf bests table.
927 while (dp
->i_d
.di_size
> mp
->m_dirblksize
) {
930 hdrsz
= xfs_dir3_data_hdr_size(xfs_sb_version_hascrc(&mp
->m_sb
));
931 bestsp
= xfs_dir2_leaf_bests_p(ltp
);
932 if (be16_to_cpu(bestsp
[be32_to_cpu(ltp
->bestcount
) - 1]) ==
933 mp
->m_dirblksize
- hdrsz
) {
935 xfs_dir2_leaf_trim_data(args
, lbp
,
936 (xfs_dir2_db_t
)(be32_to_cpu(ltp
->bestcount
) - 1))))
942 * Read the data block if we don't already have it, give up if it fails.
945 error
= xfs_dir3_data_read(tp
, dp
, mp
->m_dirdatablk
, -1, &dbp
);
950 ASSERT(hdr
->magic
== cpu_to_be32(XFS_DIR2_DATA_MAGIC
) ||
951 hdr
->magic
== cpu_to_be32(XFS_DIR3_DATA_MAGIC
));
954 * Size of the "leaf" area in the block.
956 size
= (uint
)sizeof(xfs_dir2_block_tail_t
) +
957 (uint
)sizeof(*lep
) * (leafhdr
.count
- leafhdr
.stale
);
959 * Look at the last data entry.
961 tagp
= (__be16
*)((char *)hdr
+ mp
->m_dirblksize
) - 1;
962 dup
= (xfs_dir2_data_unused_t
*)((char *)hdr
+ be16_to_cpu(*tagp
));
964 * If it's not free or is too short we can't do it.
966 if (be16_to_cpu(dup
->freetag
) != XFS_DIR2_DATA_FREE_TAG
||
967 be16_to_cpu(dup
->length
) < size
)
971 * Start converting it to block form.
973 xfs_dir3_block_init(mp
, tp
, dbp
, dp
);
978 * Use up the space at the end of the block (blp/btp).
980 xfs_dir2_data_use_free(tp
, dbp
, dup
, mp
->m_dirblksize
- size
, size
,
981 &needlog
, &needscan
);
983 * Initialize the block tail.
985 btp
= xfs_dir2_block_tail_p(mp
, hdr
);
986 btp
->count
= cpu_to_be32(leafhdr
.count
- leafhdr
.stale
);
988 xfs_dir2_block_log_tail(tp
, dbp
);
990 * Initialize the block leaf area. We compact out stale entries.
992 lep
= xfs_dir2_block_leaf_p(btp
);
993 for (from
= to
= 0; from
< leafhdr
.count
; from
++) {
994 if (ents
[from
].address
== cpu_to_be32(XFS_DIR2_NULL_DATAPTR
))
996 lep
[to
++] = ents
[from
];
998 ASSERT(to
== be32_to_cpu(btp
->count
));
999 xfs_dir2_block_log_leaf(tp
, dbp
, 0, be32_to_cpu(btp
->count
) - 1);
1001 * Scan the bestfree if we need it and log the data block header.
1004 xfs_dir2_data_freescan(mp
, hdr
, &needlog
);
1006 xfs_dir2_data_log_header(tp
, dbp
);
1008 * Pitch the old leaf block.
1010 error
= xfs_da_shrink_inode(args
, mp
->m_dirleafblk
, lbp
);
1015 * Now see if the resulting block can be shrunken to shortform.
1017 size
= xfs_dir2_block_sfsize(dp
, hdr
, &sfh
);
1018 if (size
> XFS_IFORK_DSIZE(dp
))
1021 return xfs_dir2_block_to_sf(args
, dbp
, size
, &sfh
);
1025 * Convert the shortform directory to block form.
1028 xfs_dir2_sf_to_block(
1029 xfs_da_args_t
*args
) /* operation arguments */
1031 xfs_dir2_db_t blkno
; /* dir-relative block # (0) */
1032 xfs_dir2_data_hdr_t
*hdr
; /* block header */
1033 xfs_dir2_leaf_entry_t
*blp
; /* block leaf entries */
1034 struct xfs_buf
*bp
; /* block buffer */
1035 xfs_dir2_block_tail_t
*btp
; /* block tail pointer */
1036 xfs_dir2_data_entry_t
*dep
; /* data entry pointer */
1037 xfs_inode_t
*dp
; /* incore directory inode */
1038 int dummy
; /* trash */
1039 xfs_dir2_data_unused_t
*dup
; /* unused entry pointer */
1040 int endoffset
; /* end of data objects */
1041 int error
; /* error return value */
1043 xfs_mount_t
*mp
; /* filesystem mount point */
1044 int needlog
; /* need to log block header */
1045 int needscan
; /* need to scan block freespc */
1046 int newoffset
; /* offset from current entry */
1047 int offset
; /* target block offset */
1048 xfs_dir2_sf_entry_t
*sfep
; /* sf entry pointer */
1049 xfs_dir2_sf_hdr_t
*oldsfp
; /* old shortform header */
1050 xfs_dir2_sf_hdr_t
*sfp
; /* shortform header */
1051 __be16
*tagp
; /* end of data entry */
1052 xfs_trans_t
*tp
; /* transaction pointer */
1053 struct xfs_name name
;
1055 trace_xfs_dir2_sf_to_block(args
);
1060 ASSERT(dp
->i_df
.if_flags
& XFS_IFINLINE
);
1062 * Bomb out if the shortform directory is way too short.
1064 if (dp
->i_d
.di_size
< offsetof(xfs_dir2_sf_hdr_t
, parent
)) {
1065 ASSERT(XFS_FORCED_SHUTDOWN(mp
));
1066 return XFS_ERROR(EIO
);
1069 oldsfp
= (xfs_dir2_sf_hdr_t
*)dp
->i_df
.if_u1
.if_data
;
1071 ASSERT(dp
->i_df
.if_bytes
== dp
->i_d
.di_size
);
1072 ASSERT(dp
->i_df
.if_u1
.if_data
!= NULL
);
1073 ASSERT(dp
->i_d
.di_size
>= xfs_dir2_sf_hdr_size(oldsfp
->i8count
));
1076 * Copy the directory into a temporary buffer.
1077 * Then pitch the incore inode data so we can make extents.
1079 sfp
= kmem_alloc(dp
->i_df
.if_bytes
, KM_SLEEP
);
1080 memcpy(sfp
, oldsfp
, dp
->i_df
.if_bytes
);
1082 xfs_idata_realloc(dp
, -dp
->i_df
.if_bytes
, XFS_DATA_FORK
);
1083 dp
->i_d
.di_size
= 0;
1084 xfs_trans_log_inode(tp
, dp
, XFS_ILOG_CORE
);
1087 * Add block 0 to the inode.
1089 error
= xfs_dir2_grow_inode(args
, XFS_DIR2_DATA_SPACE
, &blkno
);
1095 * Initialize the data block, then convert it to block format.
1097 error
= xfs_dir3_data_init(args
, blkno
, &bp
);
1102 xfs_dir3_block_init(mp
, tp
, bp
, dp
);
1106 * Compute size of block "tail" area.
1108 i
= (uint
)sizeof(*btp
) +
1109 (sfp
->count
+ 2) * (uint
)sizeof(xfs_dir2_leaf_entry_t
);
1111 * The whole thing is initialized to free by the init routine.
1112 * Say we're using the leaf and tail area.
1114 dup
= xfs_dir3_data_unused_p(hdr
);
1115 needlog
= needscan
= 0;
1116 xfs_dir2_data_use_free(tp
, bp
, dup
, mp
->m_dirblksize
- i
, i
, &needlog
,
1118 ASSERT(needscan
== 0);
1122 btp
= xfs_dir2_block_tail_p(mp
, hdr
);
1123 btp
->count
= cpu_to_be32(sfp
->count
+ 2); /* ., .. */
1125 blp
= xfs_dir2_block_leaf_p(btp
);
1126 endoffset
= (uint
)((char *)blp
- (char *)hdr
);
1128 * Remove the freespace, we'll manage it.
1130 xfs_dir2_data_use_free(tp
, bp
, dup
,
1131 (xfs_dir2_data_aoff_t
)((char *)dup
- (char *)hdr
),
1132 be16_to_cpu(dup
->length
), &needlog
, &needscan
);
1134 * Create entry for .
1136 dep
= xfs_dir3_data_dot_entry_p(hdr
);
1137 dep
->inumber
= cpu_to_be64(dp
->i_ino
);
1140 tagp
= xfs_dir2_data_entry_tag_p(dep
);
1141 *tagp
= cpu_to_be16((char *)dep
- (char *)hdr
);
1142 xfs_dir2_data_log_entry(tp
, bp
, dep
);
1143 blp
[0].hashval
= cpu_to_be32(xfs_dir_hash_dot
);
1144 blp
[0].address
= cpu_to_be32(xfs_dir2_byte_to_dataptr(mp
,
1145 (char *)dep
- (char *)hdr
));
1147 * Create entry for ..
1149 dep
= xfs_dir3_data_dotdot_entry_p(hdr
);
1150 dep
->inumber
= cpu_to_be64(xfs_dir2_sf_get_parent_ino(sfp
));
1152 dep
->name
[0] = dep
->name
[1] = '.';
1153 tagp
= xfs_dir2_data_entry_tag_p(dep
);
1154 *tagp
= cpu_to_be16((char *)dep
- (char *)hdr
);
1155 xfs_dir2_data_log_entry(tp
, bp
, dep
);
1156 blp
[1].hashval
= cpu_to_be32(xfs_dir_hash_dotdot
);
1157 blp
[1].address
= cpu_to_be32(xfs_dir2_byte_to_dataptr(mp
,
1158 (char *)dep
- (char *)hdr
));
1159 offset
= xfs_dir3_data_first_offset(hdr
);
1161 * Loop over existing entries, stuff them in.
1167 sfep
= xfs_dir2_sf_firstentry(sfp
);
1169 * Need to preserve the existing offset values in the sf directory.
1170 * Insert holes (unused entries) where necessary.
1172 while (offset
< endoffset
) {
1174 * sfep is null when we reach the end of the list.
1177 newoffset
= endoffset
;
1179 newoffset
= xfs_dir2_sf_get_offset(sfep
);
1181 * There should be a hole here, make one.
1183 if (offset
< newoffset
) {
1184 dup
= (xfs_dir2_data_unused_t
*)((char *)hdr
+ offset
);
1185 dup
->freetag
= cpu_to_be16(XFS_DIR2_DATA_FREE_TAG
);
1186 dup
->length
= cpu_to_be16(newoffset
- offset
);
1187 *xfs_dir2_data_unused_tag_p(dup
) = cpu_to_be16(
1188 ((char *)dup
- (char *)hdr
));
1189 xfs_dir2_data_log_unused(tp
, bp
, dup
);
1190 xfs_dir2_data_freeinsert(hdr
, dup
, &dummy
);
1191 offset
+= be16_to_cpu(dup
->length
);
1195 * Copy a real entry.
1197 dep
= (xfs_dir2_data_entry_t
*)((char *)hdr
+ newoffset
);
1198 dep
->inumber
= cpu_to_be64(xfs_dir2_sfe_get_ino(sfp
, sfep
));
1199 dep
->namelen
= sfep
->namelen
;
1200 memcpy(dep
->name
, sfep
->name
, dep
->namelen
);
1201 tagp
= xfs_dir2_data_entry_tag_p(dep
);
1202 *tagp
= cpu_to_be16((char *)dep
- (char *)hdr
);
1203 xfs_dir2_data_log_entry(tp
, bp
, dep
);
1204 name
.name
= sfep
->name
;
1205 name
.len
= sfep
->namelen
;
1206 blp
[2 + i
].hashval
= cpu_to_be32(mp
->m_dirnameops
->
1208 blp
[2 + i
].address
= cpu_to_be32(xfs_dir2_byte_to_dataptr(mp
,
1209 (char *)dep
- (char *)hdr
));
1210 offset
= (int)((char *)(tagp
+ 1) - (char *)hdr
);
1211 if (++i
== sfp
->count
)
1214 sfep
= xfs_dir2_sf_nextentry(sfp
, sfep
);
1216 /* Done with the temporary buffer */
1219 * Sort the leaf entries by hash value.
1221 xfs_sort(blp
, be32_to_cpu(btp
->count
), sizeof(*blp
), xfs_dir2_block_sort
);
1223 * Log the leaf entry area and tail.
1224 * Already logged the header in data_init, ignore needlog.
1226 ASSERT(needscan
== 0);
1227 xfs_dir2_block_log_leaf(tp
, bp
, 0, be32_to_cpu(btp
->count
) - 1);
1228 xfs_dir2_block_log_tail(tp
, bp
);
1229 xfs_dir3_data_check(dp
, bp
);