2 * Copyright (c) 2000-2003,2005 Silicon Graphics, Inc.
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation.
9 * This program is distributed in the hope that it would be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write the Free Software Foundation,
16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 #include "xfs_types.h"
22 #include "xfs_trans.h"
25 #include "xfs_mount.h"
26 #include "xfs_da_btree.h"
27 #include "xfs_bmap_btree.h"
28 #include "xfs_dinode.h"
29 #include "xfs_inode.h"
30 #include "xfs_inode_item.h"
32 #include "xfs_dir2_format.h"
33 #include "xfs_dir2_priv.h"
34 #include "xfs_error.h"
35 #include "xfs_trace.h"
38 * Local function prototypes.
40 static void xfs_dir2_block_log_leaf(xfs_trans_t
*tp
, struct xfs_buf
*bp
,
42 static void xfs_dir2_block_log_tail(xfs_trans_t
*tp
, struct xfs_buf
*bp
);
43 static int xfs_dir2_block_lookup_int(xfs_da_args_t
*args
, struct xfs_buf
**bpp
,
45 static int xfs_dir2_block_sort(const void *a
, const void *b
);
47 static xfs_dahash_t xfs_dir_hash_dot
, xfs_dir_hash_dotdot
;
50 * One-time startup routine called from xfs_init().
55 xfs_dir_hash_dot
= xfs_da_hashname((unsigned char *)".", 1);
56 xfs_dir_hash_dotdot
= xfs_da_hashname((unsigned char *)"..", 2);
65 struct xfs_mount
*mp
= dp
->i_mount
;
67 return xfs_da_read_buf(tp
, dp
, mp
->m_dirdatablk
, -1, bpp
,
72 xfs_dir2_block_need_space(
73 struct xfs_dir2_data_hdr
*hdr
,
74 struct xfs_dir2_block_tail
*btp
,
75 struct xfs_dir2_leaf_entry
*blp
,
77 struct xfs_dir2_data_unused
**dupp
,
78 struct xfs_dir2_data_unused
**enddupp
,
82 struct xfs_dir2_data_free
*bf
;
84 struct xfs_dir2_data_unused
*dup
= NULL
;
85 struct xfs_dir2_data_unused
*enddup
= NULL
;
91 * If there are stale entries we'll use one for the leaf.
94 if (be16_to_cpu(bf
[0].length
) >= len
) {
96 * The biggest entry enough to avoid compaction.
98 dup
= (xfs_dir2_data_unused_t
*)
99 ((char *)hdr
+ be16_to_cpu(bf
[0].offset
));
104 * Will need to compact to make this work.
105 * Tag just before the first leaf entry.
108 tagp
= (__be16
*)blp
- 1;
110 /* Data object just before the first leaf entry. */
111 dup
= (xfs_dir2_data_unused_t
*)((char *)hdr
+ be16_to_cpu(*tagp
));
114 * If it's not free then the data will go where the
115 * leaf data starts now, if it works at all.
117 if (be16_to_cpu(dup
->freetag
) == XFS_DIR2_DATA_FREE_TAG
) {
118 if (be16_to_cpu(dup
->length
) + (be32_to_cpu(btp
->stale
) - 1) *
119 (uint
)sizeof(*blp
) < len
)
121 } else if ((be32_to_cpu(btp
->stale
) - 1) * (uint
)sizeof(*blp
) < len
)
124 dup
= (xfs_dir2_data_unused_t
*)blp
;
129 * no stale entries, so just use free space.
130 * Tag just before the first leaf entry.
132 tagp
= (__be16
*)blp
- 1;
134 /* Data object just before the first leaf entry. */
135 enddup
= (xfs_dir2_data_unused_t
*)((char *)hdr
+ be16_to_cpu(*tagp
));
138 * If it's not free then can't do this add without cleaning up:
139 * the space before the first leaf entry needs to be free so it
140 * can be expanded to hold the pointer to the new entry.
142 if (be16_to_cpu(enddup
->freetag
) == XFS_DIR2_DATA_FREE_TAG
) {
144 * Check out the biggest freespace and see if it's the same one.
146 dup
= (xfs_dir2_data_unused_t
*)
147 ((char *)hdr
+ be16_to_cpu(bf
[0].offset
));
150 * Not the same free entry, just check its length.
152 if (be16_to_cpu(dup
->length
) < len
)
158 * It is the biggest freespace, can it hold the leaf too?
160 if (be16_to_cpu(dup
->length
) < len
+ (uint
)sizeof(*blp
)) {
162 * Yes, use the second-largest entry instead if it works.
164 if (be16_to_cpu(bf
[1].length
) >= len
)
165 dup
= (xfs_dir2_data_unused_t
*)
166 ((char *)hdr
+ be16_to_cpu(bf
[1].offset
));
178 * compact the leaf entries.
179 * Leave the highest-numbered stale entry stale.
180 * XXX should be the one closest to mid but mid is not yet computed.
183 xfs_dir2_block_compact(
184 struct xfs_trans
*tp
,
186 struct xfs_dir2_data_hdr
*hdr
,
187 struct xfs_dir2_block_tail
*btp
,
188 struct xfs_dir2_leaf_entry
*blp
,
193 int fromidx
; /* source leaf index */
194 int toidx
; /* target leaf index */
196 int highstale
; /* high stale index */
198 fromidx
= toidx
= be32_to_cpu(btp
->count
) - 1;
199 highstale
= *lfloghigh
= -1;
200 for (; fromidx
>= 0; fromidx
--) {
201 if (blp
[fromidx
].address
== cpu_to_be32(XFS_DIR2_NULL_DATAPTR
)) {
205 if (*lfloghigh
== -1)
211 blp
[toidx
] = blp
[fromidx
];
214 *lfloglow
= toidx
+ 1 - (be32_to_cpu(btp
->stale
) - 1);
215 *lfloghigh
-= be32_to_cpu(btp
->stale
) - 1;
216 be32_add_cpu(&btp
->count
, -(be32_to_cpu(btp
->stale
) - 1));
217 xfs_dir2_data_make_free(tp
, bp
,
218 (xfs_dir2_data_aoff_t
)((char *)blp
- (char *)hdr
),
219 (xfs_dir2_data_aoff_t
)((be32_to_cpu(btp
->stale
) - 1) * sizeof(*blp
)),
221 blp
+= be32_to_cpu(btp
->stale
) - 1;
222 btp
->stale
= cpu_to_be32(1);
224 * If we now need to rebuild the bestfree map, do so.
225 * This needs to happen before the next call to use_free.
228 xfs_dir2_data_freescan(tp
->t_mountp
, hdr
, needlog
);
232 * Add an entry to a block directory.
235 xfs_dir2_block_addname(
236 xfs_da_args_t
*args
) /* directory op arguments */
238 xfs_dir2_data_hdr_t
*hdr
; /* block header */
239 xfs_dir2_leaf_entry_t
*blp
; /* block leaf entries */
240 struct xfs_buf
*bp
; /* buffer for block */
241 xfs_dir2_block_tail_t
*btp
; /* block tail */
242 int compact
; /* need to compact leaf ents */
243 xfs_dir2_data_entry_t
*dep
; /* block data entry */
244 xfs_inode_t
*dp
; /* directory inode */
245 xfs_dir2_data_unused_t
*dup
; /* block unused entry */
246 int error
; /* error return value */
247 xfs_dir2_data_unused_t
*enddup
=NULL
; /* unused at end of data */
248 xfs_dahash_t hash
; /* hash value of found entry */
249 int high
; /* high index for binary srch */
250 int highstale
; /* high stale index */
251 int lfloghigh
=0; /* last final leaf to log */
252 int lfloglow
=0; /* first final leaf to log */
253 int len
; /* length of the new entry */
254 int low
; /* low index for binary srch */
255 int lowstale
; /* low stale index */
256 int mid
=0; /* midpoint for binary srch */
257 xfs_mount_t
*mp
; /* filesystem mount point */
258 int needlog
; /* need to log header */
259 int needscan
; /* need to rescan freespace */
260 __be16
*tagp
; /* pointer to tag value */
261 xfs_trans_t
*tp
; /* transaction structure */
263 trace_xfs_dir2_block_addname(args
);
269 /* Read the (one and only) directory block into bp. */
270 error
= xfs_dir2_block_read(tp
, dp
, &bp
);
274 len
= xfs_dir2_data_entsize(args
->namelen
);
277 * Set up pointers to parts of the block.
280 btp
= xfs_dir2_block_tail_p(mp
, hdr
);
281 blp
= xfs_dir2_block_leaf_p(btp
);
284 * Find out if we can reuse stale entries or whether we need extra
285 * space for entry and new leaf.
287 xfs_dir2_block_need_space(hdr
, btp
, blp
, &tagp
, &dup
,
288 &enddup
, &compact
, len
);
291 * Done everything we need for a space check now.
293 if (args
->op_flags
& XFS_DA_OP_JUSTCHECK
) {
294 xfs_trans_brelse(tp
, bp
);
296 return XFS_ERROR(ENOSPC
);
301 * If we don't have space for the new entry & leaf ...
304 /* Don't have a space reservation: return no-space. */
305 if (args
->total
== 0)
306 return XFS_ERROR(ENOSPC
);
308 * Convert to the next larger format.
309 * Then add the new entry in that format.
311 error
= xfs_dir2_block_to_leaf(args
, bp
);
314 return xfs_dir2_leaf_addname(args
);
317 needlog
= needscan
= 0;
320 * If need to compact the leaf entries, do it now.
323 xfs_dir2_block_compact(tp
, bp
, hdr
, btp
, blp
, &needlog
,
324 &lfloghigh
, &lfloglow
);
325 else if (btp
->stale
) {
327 * Set leaf logging boundaries to impossible state.
328 * For the no-stale case they're set explicitly.
330 lfloglow
= be32_to_cpu(btp
->count
);
335 * Find the slot that's first lower than our hash value, -1 if none.
337 for (low
= 0, high
= be32_to_cpu(btp
->count
) - 1; low
<= high
; ) {
338 mid
= (low
+ high
) >> 1;
339 if ((hash
= be32_to_cpu(blp
[mid
].hashval
)) == args
->hashval
)
341 if (hash
< args
->hashval
)
346 while (mid
>= 0 && be32_to_cpu(blp
[mid
].hashval
) >= args
->hashval
) {
350 * No stale entries, will use enddup space to hold new leaf.
354 * Mark the space needed for the new leaf entry, now in use.
356 xfs_dir2_data_use_free(tp
, bp
, enddup
,
357 (xfs_dir2_data_aoff_t
)
358 ((char *)enddup
- (char *)hdr
+ be16_to_cpu(enddup
->length
) -
360 (xfs_dir2_data_aoff_t
)sizeof(*blp
),
361 &needlog
, &needscan
);
363 * Update the tail (entry count).
365 be32_add_cpu(&btp
->count
, 1);
367 * If we now need to rebuild the bestfree map, do so.
368 * This needs to happen before the next call to use_free.
371 xfs_dir2_data_freescan(mp
, hdr
, &needlog
);
375 * Adjust pointer to the first leaf entry, we're about to move
376 * the table up one to open up space for the new leaf entry.
377 * Then adjust our index to match.
382 memmove(blp
, &blp
[1], mid
* sizeof(*blp
));
387 * Use a stale leaf for our new entry.
392 blp
[lowstale
].address
!=
393 cpu_to_be32(XFS_DIR2_NULL_DATAPTR
);
396 for (highstale
= mid
+ 1;
397 highstale
< be32_to_cpu(btp
->count
) &&
398 blp
[highstale
].address
!=
399 cpu_to_be32(XFS_DIR2_NULL_DATAPTR
) &&
400 (lowstale
< 0 || mid
- lowstale
> highstale
- mid
);
404 * Move entries toward the low-numbered stale entry.
407 (highstale
== be32_to_cpu(btp
->count
) ||
408 mid
- lowstale
<= highstale
- mid
)) {
410 memmove(&blp
[lowstale
], &blp
[lowstale
+ 1],
411 (mid
- lowstale
) * sizeof(*blp
));
412 lfloglow
= MIN(lowstale
, lfloglow
);
413 lfloghigh
= MAX(mid
, lfloghigh
);
416 * Move entries toward the high-numbered stale entry.
419 ASSERT(highstale
< be32_to_cpu(btp
->count
));
422 memmove(&blp
[mid
+ 1], &blp
[mid
],
423 (highstale
- mid
) * sizeof(*blp
));
424 lfloglow
= MIN(mid
, lfloglow
);
425 lfloghigh
= MAX(highstale
, lfloghigh
);
427 be32_add_cpu(&btp
->stale
, -1);
430 * Point to the new data entry.
432 dep
= (xfs_dir2_data_entry_t
*)dup
;
434 * Fill in the leaf entry.
436 blp
[mid
].hashval
= cpu_to_be32(args
->hashval
);
437 blp
[mid
].address
= cpu_to_be32(xfs_dir2_byte_to_dataptr(mp
,
438 (char *)dep
- (char *)hdr
));
439 xfs_dir2_block_log_leaf(tp
, bp
, lfloglow
, lfloghigh
);
441 * Mark space for the data entry used.
443 xfs_dir2_data_use_free(tp
, bp
, dup
,
444 (xfs_dir2_data_aoff_t
)((char *)dup
- (char *)hdr
),
445 (xfs_dir2_data_aoff_t
)len
, &needlog
, &needscan
);
447 * Create the new data entry.
449 dep
->inumber
= cpu_to_be64(args
->inumber
);
450 dep
->namelen
= args
->namelen
;
451 memcpy(dep
->name
, args
->name
, args
->namelen
);
452 tagp
= xfs_dir2_data_entry_tag_p(dep
);
453 *tagp
= cpu_to_be16((char *)dep
- (char *)hdr
);
455 * Clean up the bestfree array and log the header, tail, and entry.
458 xfs_dir2_data_freescan(mp
, hdr
, &needlog
);
460 xfs_dir2_data_log_header(tp
, bp
);
461 xfs_dir2_block_log_tail(tp
, bp
);
462 xfs_dir2_data_log_entry(tp
, bp
, dep
);
463 xfs_dir2_data_check(dp
, bp
);
468 * Readdir for block directories.
471 xfs_dir2_block_getdents(
472 xfs_inode_t
*dp
, /* incore inode */
477 xfs_dir2_data_hdr_t
*hdr
; /* block header */
478 struct xfs_buf
*bp
; /* buffer for block */
479 xfs_dir2_block_tail_t
*btp
; /* block tail */
480 xfs_dir2_data_entry_t
*dep
; /* block data entry */
481 xfs_dir2_data_unused_t
*dup
; /* block unused entry */
482 char *endptr
; /* end of the data entries */
483 int error
; /* error return value */
484 xfs_mount_t
*mp
; /* filesystem mount point */
485 char *ptr
; /* current data entry */
486 int wantoff
; /* starting block offset */
491 * If the block number in the offset is out of range, we're done.
493 if (xfs_dir2_dataptr_to_db(mp
, *offset
) > mp
->m_dirdatablk
)
496 error
= xfs_dir2_block_read(NULL
, dp
, &bp
);
501 * Extract the byte offset we start at from the seek pointer.
502 * We'll skip entries before this.
504 wantoff
= xfs_dir2_dataptr_to_off(mp
, *offset
);
506 xfs_dir2_data_check(dp
, bp
);
508 * Set up values for the loop.
510 btp
= xfs_dir2_block_tail_p(mp
, hdr
);
511 ptr
= (char *)(hdr
+ 1);
512 endptr
= (char *)xfs_dir2_block_leaf_p(btp
);
515 * Loop over the data portion of the block.
516 * Each object is a real entry (dep) or an unused one (dup).
518 while (ptr
< endptr
) {
519 dup
= (xfs_dir2_data_unused_t
*)ptr
;
523 if (be16_to_cpu(dup
->freetag
) == XFS_DIR2_DATA_FREE_TAG
) {
524 ptr
+= be16_to_cpu(dup
->length
);
528 dep
= (xfs_dir2_data_entry_t
*)ptr
;
531 * Bump pointer for the next iteration.
533 ptr
+= xfs_dir2_data_entsize(dep
->namelen
);
535 * The entry is before the desired starting point, skip it.
537 if ((char *)dep
- (char *)hdr
< wantoff
)
540 cook
= xfs_dir2_db_off_to_dataptr(mp
, mp
->m_dirdatablk
,
541 (char *)dep
- (char *)hdr
);
544 * If it didn't fit, set the final offset to here & return.
546 if (filldir(dirent
, (char *)dep
->name
, dep
->namelen
,
547 cook
& 0x7fffffff, be64_to_cpu(dep
->inumber
),
549 *offset
= cook
& 0x7fffffff;
550 xfs_trans_brelse(NULL
, bp
);
556 * Reached the end of the block.
557 * Set the offset to a non-existent block 1 and return.
559 *offset
= xfs_dir2_db_off_to_dataptr(mp
, mp
->m_dirdatablk
+ 1, 0) &
561 xfs_trans_brelse(NULL
, bp
);
566 * Log leaf entries from the block.
569 xfs_dir2_block_log_leaf(
570 xfs_trans_t
*tp
, /* transaction structure */
571 struct xfs_buf
*bp
, /* block buffer */
572 int first
, /* index of first logged leaf */
573 int last
) /* index of last logged leaf */
575 xfs_dir2_data_hdr_t
*hdr
= bp
->b_addr
;
576 xfs_dir2_leaf_entry_t
*blp
;
577 xfs_dir2_block_tail_t
*btp
;
579 btp
= xfs_dir2_block_tail_p(tp
->t_mountp
, hdr
);
580 blp
= xfs_dir2_block_leaf_p(btp
);
581 xfs_trans_log_buf(tp
, bp
, (uint
)((char *)&blp
[first
] - (char *)hdr
),
582 (uint
)((char *)&blp
[last
+ 1] - (char *)hdr
- 1));
586 * Log the block tail.
589 xfs_dir2_block_log_tail(
590 xfs_trans_t
*tp
, /* transaction structure */
591 struct xfs_buf
*bp
) /* block buffer */
593 xfs_dir2_data_hdr_t
*hdr
= bp
->b_addr
;
594 xfs_dir2_block_tail_t
*btp
;
596 btp
= xfs_dir2_block_tail_p(tp
->t_mountp
, hdr
);
597 xfs_trans_log_buf(tp
, bp
, (uint
)((char *)btp
- (char *)hdr
),
598 (uint
)((char *)(btp
+ 1) - (char *)hdr
- 1));
602 * Look up an entry in the block. This is the external routine,
603 * xfs_dir2_block_lookup_int does the real work.
606 xfs_dir2_block_lookup(
607 xfs_da_args_t
*args
) /* dir lookup arguments */
609 xfs_dir2_data_hdr_t
*hdr
; /* block header */
610 xfs_dir2_leaf_entry_t
*blp
; /* block leaf entries */
611 struct xfs_buf
*bp
; /* block buffer */
612 xfs_dir2_block_tail_t
*btp
; /* block tail */
613 xfs_dir2_data_entry_t
*dep
; /* block data entry */
614 xfs_inode_t
*dp
; /* incore inode */
615 int ent
; /* entry index */
616 int error
; /* error return value */
617 xfs_mount_t
*mp
; /* filesystem mount point */
619 trace_xfs_dir2_block_lookup(args
);
622 * Get the buffer, look up the entry.
623 * If not found (ENOENT) then return, have no buffer.
625 if ((error
= xfs_dir2_block_lookup_int(args
, &bp
, &ent
)))
630 xfs_dir2_data_check(dp
, bp
);
631 btp
= xfs_dir2_block_tail_p(mp
, hdr
);
632 blp
= xfs_dir2_block_leaf_p(btp
);
634 * Get the offset from the leaf entry, to point to the data.
636 dep
= (xfs_dir2_data_entry_t
*)((char *)hdr
+
637 xfs_dir2_dataptr_to_off(mp
, be32_to_cpu(blp
[ent
].address
)));
639 * Fill in inode number, CI name if appropriate, release the block.
641 args
->inumber
= be64_to_cpu(dep
->inumber
);
642 error
= xfs_dir_cilookup_result(args
, dep
->name
, dep
->namelen
);
643 xfs_trans_brelse(args
->trans
, bp
);
644 return XFS_ERROR(error
);
648 * Internal block lookup routine.
650 static int /* error */
651 xfs_dir2_block_lookup_int(
652 xfs_da_args_t
*args
, /* dir lookup arguments */
653 struct xfs_buf
**bpp
, /* returned block buffer */
654 int *entno
) /* returned entry number */
656 xfs_dir2_dataptr_t addr
; /* data entry address */
657 xfs_dir2_data_hdr_t
*hdr
; /* block header */
658 xfs_dir2_leaf_entry_t
*blp
; /* block leaf entries */
659 struct xfs_buf
*bp
; /* block buffer */
660 xfs_dir2_block_tail_t
*btp
; /* block tail */
661 xfs_dir2_data_entry_t
*dep
; /* block data entry */
662 xfs_inode_t
*dp
; /* incore inode */
663 int error
; /* error return value */
664 xfs_dahash_t hash
; /* found hash value */
665 int high
; /* binary search high index */
666 int low
; /* binary search low index */
667 int mid
; /* binary search current idx */
668 xfs_mount_t
*mp
; /* filesystem mount point */
669 xfs_trans_t
*tp
; /* transaction pointer */
670 enum xfs_dacmp cmp
; /* comparison result */
676 error
= xfs_dir2_block_read(tp
, dp
, &bp
);
681 xfs_dir2_data_check(dp
, bp
);
682 btp
= xfs_dir2_block_tail_p(mp
, hdr
);
683 blp
= xfs_dir2_block_leaf_p(btp
);
685 * Loop doing a binary search for our hash value.
686 * Find our entry, ENOENT if it's not there.
688 for (low
= 0, high
= be32_to_cpu(btp
->count
) - 1; ; ) {
690 mid
= (low
+ high
) >> 1;
691 if ((hash
= be32_to_cpu(blp
[mid
].hashval
)) == args
->hashval
)
693 if (hash
< args
->hashval
)
698 ASSERT(args
->op_flags
& XFS_DA_OP_OKNOENT
);
699 xfs_trans_brelse(tp
, bp
);
700 return XFS_ERROR(ENOENT
);
704 * Back up to the first one with the right hash value.
706 while (mid
> 0 && be32_to_cpu(blp
[mid
- 1].hashval
) == args
->hashval
) {
710 * Now loop forward through all the entries with the
711 * right hash value looking for our name.
714 if ((addr
= be32_to_cpu(blp
[mid
].address
)) == XFS_DIR2_NULL_DATAPTR
)
717 * Get pointer to the entry from the leaf.
719 dep
= (xfs_dir2_data_entry_t
*)
720 ((char *)hdr
+ xfs_dir2_dataptr_to_off(mp
, addr
));
722 * Compare name and if it's an exact match, return the index
723 * and buffer. If it's the first case-insensitive match, store
724 * the index and buffer and continue looking for an exact match.
726 cmp
= mp
->m_dirnameops
->compname(args
, dep
->name
, dep
->namelen
);
727 if (cmp
!= XFS_CMP_DIFFERENT
&& cmp
!= args
->cmpresult
) {
728 args
->cmpresult
= cmp
;
731 if (cmp
== XFS_CMP_EXACT
)
734 } while (++mid
< be32_to_cpu(btp
->count
) &&
735 be32_to_cpu(blp
[mid
].hashval
) == hash
);
737 ASSERT(args
->op_flags
& XFS_DA_OP_OKNOENT
);
739 * Here, we can only be doing a lookup (not a rename or replace).
740 * If a case-insensitive match was found earlier, return success.
742 if (args
->cmpresult
== XFS_CMP_CASE
)
745 * No match, release the buffer and return ENOENT.
747 xfs_trans_brelse(tp
, bp
);
748 return XFS_ERROR(ENOENT
);
752 * Remove an entry from a block format directory.
753 * If that makes the block small enough to fit in shortform, transform it.
756 xfs_dir2_block_removename(
757 xfs_da_args_t
*args
) /* directory operation args */
759 xfs_dir2_data_hdr_t
*hdr
; /* block header */
760 xfs_dir2_leaf_entry_t
*blp
; /* block leaf pointer */
761 struct xfs_buf
*bp
; /* block buffer */
762 xfs_dir2_block_tail_t
*btp
; /* block tail */
763 xfs_dir2_data_entry_t
*dep
; /* block data entry */
764 xfs_inode_t
*dp
; /* incore inode */
765 int ent
; /* block leaf entry index */
766 int error
; /* error return value */
767 xfs_mount_t
*mp
; /* filesystem mount point */
768 int needlog
; /* need to log block header */
769 int needscan
; /* need to fixup bestfree */
770 xfs_dir2_sf_hdr_t sfh
; /* shortform header */
771 int size
; /* shortform size */
772 xfs_trans_t
*tp
; /* transaction pointer */
774 trace_xfs_dir2_block_removename(args
);
777 * Look up the entry in the block. Gets the buffer and entry index.
778 * It will always be there, the vnodeops level does a lookup first.
780 if ((error
= xfs_dir2_block_lookup_int(args
, &bp
, &ent
))) {
787 btp
= xfs_dir2_block_tail_p(mp
, hdr
);
788 blp
= xfs_dir2_block_leaf_p(btp
);
790 * Point to the data entry using the leaf entry.
792 dep
= (xfs_dir2_data_entry_t
*)
793 ((char *)hdr
+ xfs_dir2_dataptr_to_off(mp
, be32_to_cpu(blp
[ent
].address
)));
795 * Mark the data entry's space free.
797 needlog
= needscan
= 0;
798 xfs_dir2_data_make_free(tp
, bp
,
799 (xfs_dir2_data_aoff_t
)((char *)dep
- (char *)hdr
),
800 xfs_dir2_data_entsize(dep
->namelen
), &needlog
, &needscan
);
802 * Fix up the block tail.
804 be32_add_cpu(&btp
->stale
, 1);
805 xfs_dir2_block_log_tail(tp
, bp
);
807 * Remove the leaf entry by marking it stale.
809 blp
[ent
].address
= cpu_to_be32(XFS_DIR2_NULL_DATAPTR
);
810 xfs_dir2_block_log_leaf(tp
, bp
, ent
, ent
);
812 * Fix up bestfree, log the header if necessary.
815 xfs_dir2_data_freescan(mp
, hdr
, &needlog
);
817 xfs_dir2_data_log_header(tp
, bp
);
818 xfs_dir2_data_check(dp
, bp
);
820 * See if the size as a shortform is good enough.
822 size
= xfs_dir2_block_sfsize(dp
, hdr
, &sfh
);
823 if (size
> XFS_IFORK_DSIZE(dp
))
827 * If it works, do the conversion.
829 return xfs_dir2_block_to_sf(args
, bp
, size
, &sfh
);
833 * Replace an entry in a V2 block directory.
834 * Change the inode number to the new value.
837 xfs_dir2_block_replace(
838 xfs_da_args_t
*args
) /* directory operation args */
840 xfs_dir2_data_hdr_t
*hdr
; /* block header */
841 xfs_dir2_leaf_entry_t
*blp
; /* block leaf entries */
842 struct xfs_buf
*bp
; /* block buffer */
843 xfs_dir2_block_tail_t
*btp
; /* block tail */
844 xfs_dir2_data_entry_t
*dep
; /* block data entry */
845 xfs_inode_t
*dp
; /* incore inode */
846 int ent
; /* leaf entry index */
847 int error
; /* error return value */
848 xfs_mount_t
*mp
; /* filesystem mount point */
850 trace_xfs_dir2_block_replace(args
);
853 * Lookup the entry in the directory. Get buffer and entry index.
854 * This will always succeed since the caller has already done a lookup.
856 if ((error
= xfs_dir2_block_lookup_int(args
, &bp
, &ent
))) {
862 btp
= xfs_dir2_block_tail_p(mp
, hdr
);
863 blp
= xfs_dir2_block_leaf_p(btp
);
865 * Point to the data entry we need to change.
867 dep
= (xfs_dir2_data_entry_t
*)
868 ((char *)hdr
+ xfs_dir2_dataptr_to_off(mp
, be32_to_cpu(blp
[ent
].address
)));
869 ASSERT(be64_to_cpu(dep
->inumber
) != args
->inumber
);
871 * Change the inode number to the new value.
873 dep
->inumber
= cpu_to_be64(args
->inumber
);
874 xfs_dir2_data_log_entry(args
->trans
, bp
, dep
);
875 xfs_dir2_data_check(dp
, bp
);
880 * Qsort comparison routine for the block leaf entries.
882 static int /* sort order */
884 const void *a
, /* first leaf entry */
885 const void *b
) /* second leaf entry */
887 const xfs_dir2_leaf_entry_t
*la
; /* first leaf entry */
888 const xfs_dir2_leaf_entry_t
*lb
; /* second leaf entry */
892 return be32_to_cpu(la
->hashval
) < be32_to_cpu(lb
->hashval
) ? -1 :
893 (be32_to_cpu(la
->hashval
) > be32_to_cpu(lb
->hashval
) ? 1 : 0);
897 * Convert a V2 leaf directory to a V2 block directory if possible.
900 xfs_dir2_leaf_to_block(
901 xfs_da_args_t
*args
, /* operation arguments */
902 struct xfs_buf
*lbp
, /* leaf buffer */
903 struct xfs_buf
*dbp
) /* data buffer */
905 __be16
*bestsp
; /* leaf bests table */
906 xfs_dir2_data_hdr_t
*hdr
; /* block header */
907 xfs_dir2_block_tail_t
*btp
; /* block tail */
908 xfs_inode_t
*dp
; /* incore directory inode */
909 xfs_dir2_data_unused_t
*dup
; /* unused data entry */
910 int error
; /* error return value */
911 int from
; /* leaf from index */
912 xfs_dir2_leaf_t
*leaf
; /* leaf structure */
913 xfs_dir2_leaf_entry_t
*lep
; /* leaf entry */
914 xfs_dir2_leaf_tail_t
*ltp
; /* leaf tail structure */
915 xfs_mount_t
*mp
; /* file system mount point */
916 int needlog
; /* need to log data header */
917 int needscan
; /* need to scan for bestfree */
918 xfs_dir2_sf_hdr_t sfh
; /* shortform header */
919 int size
; /* bytes used */
920 __be16
*tagp
; /* end of entry (tag) */
921 int to
; /* block/leaf to index */
922 xfs_trans_t
*tp
; /* transaction pointer */
924 trace_xfs_dir2_leaf_to_block(args
);
930 ASSERT(leaf
->hdr
.info
.magic
== cpu_to_be16(XFS_DIR2_LEAF1_MAGIC
));
931 ltp
= xfs_dir2_leaf_tail_p(mp
, leaf
);
933 * If there are data blocks other than the first one, take this
934 * opportunity to remove trailing empty data blocks that may have
935 * been left behind during no-space-reservation operations.
936 * These will show up in the leaf bests table.
938 while (dp
->i_d
.di_size
> mp
->m_dirblksize
) {
939 bestsp
= xfs_dir2_leaf_bests_p(ltp
);
940 if (be16_to_cpu(bestsp
[be32_to_cpu(ltp
->bestcount
) - 1]) ==
941 mp
->m_dirblksize
- (uint
)sizeof(*hdr
)) {
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_da_read_buf(tp
, dp
, mp
->m_dirdatablk
, -1, &dbp
,
954 XFS_DATA_FORK
, NULL
);
959 ASSERT(hdr
->magic
== cpu_to_be32(XFS_DIR2_DATA_MAGIC
));
961 * Size of the "leaf" area in the block.
963 size
= (uint
)sizeof(xfs_dir2_block_tail_t
) +
964 (uint
)sizeof(*lep
) * (be16_to_cpu(leaf
->hdr
.count
) - be16_to_cpu(leaf
->hdr
.stale
));
966 * Look at the last data entry.
968 tagp
= (__be16
*)((char *)hdr
+ mp
->m_dirblksize
) - 1;
969 dup
= (xfs_dir2_data_unused_t
*)((char *)hdr
+ be16_to_cpu(*tagp
));
971 * If it's not free or is too short we can't do it.
973 if (be16_to_cpu(dup
->freetag
) != XFS_DIR2_DATA_FREE_TAG
||
974 be16_to_cpu(dup
->length
) < size
)
978 * Start converting it to block form.
980 hdr
->magic
= cpu_to_be32(XFS_DIR2_BLOCK_MAGIC
);
984 * Use up the space at the end of the block (blp/btp).
986 xfs_dir2_data_use_free(tp
, dbp
, dup
, mp
->m_dirblksize
- size
, size
,
987 &needlog
, &needscan
);
989 * Initialize the block tail.
991 btp
= xfs_dir2_block_tail_p(mp
, hdr
);
992 btp
->count
= cpu_to_be32(be16_to_cpu(leaf
->hdr
.count
) - be16_to_cpu(leaf
->hdr
.stale
));
994 xfs_dir2_block_log_tail(tp
, dbp
);
996 * Initialize the block leaf area. We compact out stale entries.
998 lep
= xfs_dir2_block_leaf_p(btp
);
999 for (from
= to
= 0; from
< be16_to_cpu(leaf
->hdr
.count
); from
++) {
1000 if (leaf
->ents
[from
].address
==
1001 cpu_to_be32(XFS_DIR2_NULL_DATAPTR
))
1003 lep
[to
++] = leaf
->ents
[from
];
1005 ASSERT(to
== be32_to_cpu(btp
->count
));
1006 xfs_dir2_block_log_leaf(tp
, dbp
, 0, be32_to_cpu(btp
->count
) - 1);
1008 * Scan the bestfree if we need it and log the data block header.
1011 xfs_dir2_data_freescan(mp
, hdr
, &needlog
);
1013 xfs_dir2_data_log_header(tp
, dbp
);
1015 * Pitch the old leaf block.
1017 error
= xfs_da_shrink_inode(args
, mp
->m_dirleafblk
, lbp
);
1022 * Now see if the resulting block can be shrunken to shortform.
1024 size
= xfs_dir2_block_sfsize(dp
, hdr
, &sfh
);
1025 if (size
> XFS_IFORK_DSIZE(dp
))
1028 return xfs_dir2_block_to_sf(args
, dbp
, size
, &sfh
);
1032 * Convert the shortform directory to block form.
1035 xfs_dir2_sf_to_block(
1036 xfs_da_args_t
*args
) /* operation arguments */
1038 xfs_dir2_db_t blkno
; /* dir-relative block # (0) */
1039 xfs_dir2_data_hdr_t
*hdr
; /* block header */
1040 xfs_dir2_leaf_entry_t
*blp
; /* block leaf entries */
1041 struct xfs_buf
*bp
; /* block buffer */
1042 xfs_dir2_block_tail_t
*btp
; /* block tail pointer */
1043 xfs_dir2_data_entry_t
*dep
; /* data entry pointer */
1044 xfs_inode_t
*dp
; /* incore directory inode */
1045 int dummy
; /* trash */
1046 xfs_dir2_data_unused_t
*dup
; /* unused entry pointer */
1047 int endoffset
; /* end of data objects */
1048 int error
; /* error return value */
1050 xfs_mount_t
*mp
; /* filesystem mount point */
1051 int needlog
; /* need to log block header */
1052 int needscan
; /* need to scan block freespc */
1053 int newoffset
; /* offset from current entry */
1054 int offset
; /* target block offset */
1055 xfs_dir2_sf_entry_t
*sfep
; /* sf entry pointer */
1056 xfs_dir2_sf_hdr_t
*oldsfp
; /* old shortform header */
1057 xfs_dir2_sf_hdr_t
*sfp
; /* shortform header */
1058 __be16
*tagp
; /* end of data entry */
1059 xfs_trans_t
*tp
; /* transaction pointer */
1060 struct xfs_name name
;
1062 trace_xfs_dir2_sf_to_block(args
);
1067 ASSERT(dp
->i_df
.if_flags
& XFS_IFINLINE
);
1069 * Bomb out if the shortform directory is way too short.
1071 if (dp
->i_d
.di_size
< offsetof(xfs_dir2_sf_hdr_t
, parent
)) {
1072 ASSERT(XFS_FORCED_SHUTDOWN(mp
));
1073 return XFS_ERROR(EIO
);
1076 oldsfp
= (xfs_dir2_sf_hdr_t
*)dp
->i_df
.if_u1
.if_data
;
1078 ASSERT(dp
->i_df
.if_bytes
== dp
->i_d
.di_size
);
1079 ASSERT(dp
->i_df
.if_u1
.if_data
!= NULL
);
1080 ASSERT(dp
->i_d
.di_size
>= xfs_dir2_sf_hdr_size(oldsfp
->i8count
));
1083 * Copy the directory into a temporary buffer.
1084 * Then pitch the incore inode data so we can make extents.
1086 sfp
= kmem_alloc(dp
->i_df
.if_bytes
, KM_SLEEP
);
1087 memcpy(sfp
, oldsfp
, dp
->i_df
.if_bytes
);
1089 xfs_idata_realloc(dp
, -dp
->i_df
.if_bytes
, XFS_DATA_FORK
);
1090 dp
->i_d
.di_size
= 0;
1091 xfs_trans_log_inode(tp
, dp
, XFS_ILOG_CORE
);
1094 * Add block 0 to the inode.
1096 error
= xfs_dir2_grow_inode(args
, XFS_DIR2_DATA_SPACE
, &blkno
);
1102 * Initialize the data block.
1104 error
= xfs_dir2_data_init(args
, blkno
, &bp
);
1110 hdr
->magic
= cpu_to_be32(XFS_DIR2_BLOCK_MAGIC
);
1112 * Compute size of block "tail" area.
1114 i
= (uint
)sizeof(*btp
) +
1115 (sfp
->count
+ 2) * (uint
)sizeof(xfs_dir2_leaf_entry_t
);
1117 * The whole thing is initialized to free by the init routine.
1118 * Say we're using the leaf and tail area.
1120 dup
= (xfs_dir2_data_unused_t
*)(hdr
+ 1);
1121 needlog
= needscan
= 0;
1122 xfs_dir2_data_use_free(tp
, bp
, dup
, mp
->m_dirblksize
- i
, i
, &needlog
,
1124 ASSERT(needscan
== 0);
1128 btp
= xfs_dir2_block_tail_p(mp
, hdr
);
1129 btp
->count
= cpu_to_be32(sfp
->count
+ 2); /* ., .. */
1131 blp
= xfs_dir2_block_leaf_p(btp
);
1132 endoffset
= (uint
)((char *)blp
- (char *)hdr
);
1134 * Remove the freespace, we'll manage it.
1136 xfs_dir2_data_use_free(tp
, bp
, dup
,
1137 (xfs_dir2_data_aoff_t
)((char *)dup
- (char *)hdr
),
1138 be16_to_cpu(dup
->length
), &needlog
, &needscan
);
1140 * Create entry for .
1142 dep
= (xfs_dir2_data_entry_t
*)
1143 ((char *)hdr
+ XFS_DIR2_DATA_DOT_OFFSET
);
1144 dep
->inumber
= cpu_to_be64(dp
->i_ino
);
1147 tagp
= xfs_dir2_data_entry_tag_p(dep
);
1148 *tagp
= cpu_to_be16((char *)dep
- (char *)hdr
);
1149 xfs_dir2_data_log_entry(tp
, bp
, dep
);
1150 blp
[0].hashval
= cpu_to_be32(xfs_dir_hash_dot
);
1151 blp
[0].address
= cpu_to_be32(xfs_dir2_byte_to_dataptr(mp
,
1152 (char *)dep
- (char *)hdr
));
1154 * Create entry for ..
1156 dep
= (xfs_dir2_data_entry_t
*)
1157 ((char *)hdr
+ XFS_DIR2_DATA_DOTDOT_OFFSET
);
1158 dep
->inumber
= cpu_to_be64(xfs_dir2_sf_get_parent_ino(sfp
));
1160 dep
->name
[0] = dep
->name
[1] = '.';
1161 tagp
= xfs_dir2_data_entry_tag_p(dep
);
1162 *tagp
= cpu_to_be16((char *)dep
- (char *)hdr
);
1163 xfs_dir2_data_log_entry(tp
, bp
, dep
);
1164 blp
[1].hashval
= cpu_to_be32(xfs_dir_hash_dotdot
);
1165 blp
[1].address
= cpu_to_be32(xfs_dir2_byte_to_dataptr(mp
,
1166 (char *)dep
- (char *)hdr
));
1167 offset
= XFS_DIR2_DATA_FIRST_OFFSET
;
1169 * Loop over existing entries, stuff them in.
1175 sfep
= xfs_dir2_sf_firstentry(sfp
);
1177 * Need to preserve the existing offset values in the sf directory.
1178 * Insert holes (unused entries) where necessary.
1180 while (offset
< endoffset
) {
1182 * sfep is null when we reach the end of the list.
1185 newoffset
= endoffset
;
1187 newoffset
= xfs_dir2_sf_get_offset(sfep
);
1189 * There should be a hole here, make one.
1191 if (offset
< newoffset
) {
1192 dup
= (xfs_dir2_data_unused_t
*)((char *)hdr
+ offset
);
1193 dup
->freetag
= cpu_to_be16(XFS_DIR2_DATA_FREE_TAG
);
1194 dup
->length
= cpu_to_be16(newoffset
- offset
);
1195 *xfs_dir2_data_unused_tag_p(dup
) = cpu_to_be16(
1196 ((char *)dup
- (char *)hdr
));
1197 xfs_dir2_data_log_unused(tp
, bp
, dup
);
1198 xfs_dir2_data_freeinsert(hdr
, dup
, &dummy
);
1199 offset
+= be16_to_cpu(dup
->length
);
1203 * Copy a real entry.
1205 dep
= (xfs_dir2_data_entry_t
*)((char *)hdr
+ newoffset
);
1206 dep
->inumber
= cpu_to_be64(xfs_dir2_sfe_get_ino(sfp
, sfep
));
1207 dep
->namelen
= sfep
->namelen
;
1208 memcpy(dep
->name
, sfep
->name
, dep
->namelen
);
1209 tagp
= xfs_dir2_data_entry_tag_p(dep
);
1210 *tagp
= cpu_to_be16((char *)dep
- (char *)hdr
);
1211 xfs_dir2_data_log_entry(tp
, bp
, dep
);
1212 name
.name
= sfep
->name
;
1213 name
.len
= sfep
->namelen
;
1214 blp
[2 + i
].hashval
= cpu_to_be32(mp
->m_dirnameops
->
1216 blp
[2 + i
].address
= cpu_to_be32(xfs_dir2_byte_to_dataptr(mp
,
1217 (char *)dep
- (char *)hdr
));
1218 offset
= (int)((char *)(tagp
+ 1) - (char *)hdr
);
1219 if (++i
== sfp
->count
)
1222 sfep
= xfs_dir2_sf_nextentry(sfp
, sfep
);
1224 /* Done with the temporary buffer */
1227 * Sort the leaf entries by hash value.
1229 xfs_sort(blp
, be32_to_cpu(btp
->count
), sizeof(*blp
), xfs_dir2_block_sort
);
1231 * Log the leaf entry area and tail.
1232 * Already logged the header in data_init, ignore needlog.
1234 ASSERT(needscan
== 0);
1235 xfs_dir2_block_log_leaf(tp
, bp
, 0, be32_to_cpu(btp
->count
) - 1);
1236 xfs_dir2_block_log_tail(tp
, bp
);
1237 xfs_dir2_data_check(dp
, bp
);