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"
23 #include "xfs_trans.h"
27 #include "xfs_dmapi.h"
28 #include "xfs_mount.h"
29 #include "xfs_da_btree.h"
30 #include "xfs_bmap_btree.h"
31 #include "xfs_dir_sf.h"
32 #include "xfs_dir2_sf.h"
33 #include "xfs_attr_sf.h"
34 #include "xfs_dinode.h"
35 #include "xfs_inode.h"
36 #include "xfs_inode_item.h"
37 #include "xfs_dir_leaf.h"
38 #include "xfs_dir2_data.h"
39 #include "xfs_dir2_leaf.h"
40 #include "xfs_dir2_block.h"
41 #include "xfs_dir2_trace.h"
42 #include "xfs_error.h"
45 * Local function prototypes.
47 static void xfs_dir2_block_log_leaf(xfs_trans_t *tp, xfs_dabuf_t *bp, int first,
49 static void xfs_dir2_block_log_tail(xfs_trans_t *tp, xfs_dabuf_t *bp);
50 static int xfs_dir2_block_lookup_int(xfs_da_args_t *args, xfs_dabuf_t **bpp,
52 static int xfs_dir2_block_sort(const void *a, const void *b);
55 * Add an entry to a block directory.
58 xfs_dir2_block_addname(
59 xfs_da_args_t *args) /* directory op arguments */
61 xfs_dir2_data_free_t *bf; /* bestfree table in block */
62 xfs_dir2_block_t *block; /* directory block structure */
63 xfs_dir2_leaf_entry_t *blp; /* block leaf entries */
64 xfs_dabuf_t *bp; /* buffer for block */
65 xfs_dir2_block_tail_t *btp; /* block tail */
66 int compact; /* need to compact leaf ents */
67 xfs_dir2_data_entry_t *dep; /* block data entry */
68 xfs_inode_t *dp; /* directory inode */
69 xfs_dir2_data_unused_t *dup; /* block unused entry */
70 int error; /* error return value */
71 xfs_dir2_data_unused_t *enddup=NULL; /* unused at end of data */
72 xfs_dahash_t hash; /* hash value of found entry */
73 int high; /* high index for binary srch */
74 int highstale; /* high stale index */
75 int lfloghigh=0; /* last final leaf to log */
76 int lfloglow=0; /* first final leaf to log */
77 int len; /* length of the new entry */
78 int low; /* low index for binary srch */
79 int lowstale; /* low stale index */
80 int mid=0; /* midpoint for binary srch */
81 xfs_mount_t *mp; /* filesystem mount point */
82 int needlog; /* need to log header */
83 int needscan; /* need to rescan freespace */
84 xfs_dir2_data_off_t *tagp; /* pointer to tag value */
85 xfs_trans_t *tp; /* transaction structure */
87 xfs_dir2_trace_args("block_addname", args);
92 * Read the (one and only) directory block into dabuf bp.
95 xfs_da_read_buf(tp, dp, mp->m_dirdatablk, -1, &bp, XFS_DATA_FORK))) {
101 * Check the magic number, corrupted if wrong.
103 if (unlikely(INT_GET(block->hdr.magic, ARCH_CONVERT)
104 != XFS_DIR2_BLOCK_MAGIC)) {
105 XFS_CORRUPTION_ERROR("xfs_dir2_block_addname",
106 XFS_ERRLEVEL_LOW, mp, block);
107 xfs_da_brelse(tp, bp);
108 return XFS_ERROR(EFSCORRUPTED);
110 len = XFS_DIR2_DATA_ENTSIZE(args->namelen);
112 * Set up pointers to parts of the block.
114 bf = block->hdr.bestfree;
115 btp = XFS_DIR2_BLOCK_TAIL_P(mp, block);
116 blp = XFS_DIR2_BLOCK_LEAF_P(btp);
118 * No stale entries? Need space for entry and new leaf.
122 * Tag just before the first leaf entry.
124 tagp = (xfs_dir2_data_off_t *)blp - 1;
126 * Data object just before the first leaf entry.
128 enddup = (xfs_dir2_data_unused_t *)((char *)block + INT_GET(*tagp, ARCH_CONVERT));
130 * If it's not free then can't do this add without cleaning up:
131 * the space before the first leaf entry needs to be free so it
132 * can be expanded to hold the pointer to the new entry.
134 if (INT_GET(enddup->freetag, ARCH_CONVERT) != XFS_DIR2_DATA_FREE_TAG)
137 * Check out the biggest freespace and see if it's the same one.
140 dup = (xfs_dir2_data_unused_t *)
141 ((char *)block + INT_GET(bf[0].offset, ARCH_CONVERT));
144 * It is the biggest freespace, is it too small
145 * to hold the new leaf too?
147 if (INT_GET(dup->length, ARCH_CONVERT) < len + (uint)sizeof(*blp)) {
149 * Yes, we use the second-largest
150 * entry instead if it works.
152 if (INT_GET(bf[1].length, ARCH_CONVERT) >= len)
153 dup = (xfs_dir2_data_unused_t *)
155 INT_GET(bf[1].offset, ARCH_CONVERT));
161 * Not the same free entry,
162 * just check its length.
164 if (INT_GET(dup->length, ARCH_CONVERT) < len) {
172 * If there are stale entries we'll use one for the leaf.
173 * Is the biggest entry enough to avoid compaction?
175 else if (INT_GET(bf[0].length, ARCH_CONVERT) >= len) {
176 dup = (xfs_dir2_data_unused_t *)
177 ((char *)block + INT_GET(bf[0].offset, ARCH_CONVERT));
181 * Will need to compact to make this work.
185 * Tag just before the first leaf entry.
187 tagp = (xfs_dir2_data_off_t *)blp - 1;
189 * Data object just before the first leaf entry.
191 dup = (xfs_dir2_data_unused_t *)((char *)block + INT_GET(*tagp, ARCH_CONVERT));
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 (INT_GET(dup->freetag, ARCH_CONVERT) == XFS_DIR2_DATA_FREE_TAG) {
197 if (INT_GET(dup->length, ARCH_CONVERT) + (INT_GET(btp->stale, ARCH_CONVERT) - 1) *
198 (uint)sizeof(*blp) < len)
200 } else if ((INT_GET(btp->stale, ARCH_CONVERT) - 1) * (uint)sizeof(*blp) < len)
203 dup = (xfs_dir2_data_unused_t *)blp;
207 * If this isn't a real add, we're done with the buffer.
210 xfs_da_brelse(tp, bp);
212 * If we don't have space for the new entry & leaf ...
216 * Not trying to actually do anything, or don't have
217 * a space reservation: return no-space.
219 if (args->justcheck || args->total == 0)
220 return XFS_ERROR(ENOSPC);
222 * Convert to the next larger format.
223 * Then add the new entry in that format.
225 error = xfs_dir2_block_to_leaf(args, bp);
229 return xfs_dir2_leaf_addname(args);
232 * Just checking, and it would work, so say so.
236 needlog = needscan = 0;
238 * If need to compact the leaf entries, do it now.
239 * Leave the highest-numbered stale entry stale.
240 * XXX should be the one closest to mid but mid is not yet computed.
243 int fromidx; /* source leaf index */
244 int toidx; /* target leaf index */
246 for (fromidx = toidx = INT_GET(btp->count, ARCH_CONVERT) - 1,
247 highstale = lfloghigh = -1;
250 if (INT_GET(blp[fromidx].address, ARCH_CONVERT) == XFS_DIR2_NULL_DATAPTR) {
260 blp[toidx] = blp[fromidx];
263 lfloglow = toidx + 1 - (INT_GET(btp->stale, ARCH_CONVERT) - 1);
264 lfloghigh -= INT_GET(btp->stale, ARCH_CONVERT) - 1;
265 INT_MOD(btp->count, ARCH_CONVERT, -(INT_GET(btp->stale, ARCH_CONVERT) - 1));
266 xfs_dir2_data_make_free(tp, bp,
267 (xfs_dir2_data_aoff_t)((char *)blp - (char *)block),
268 (xfs_dir2_data_aoff_t)((INT_GET(btp->stale, ARCH_CONVERT) - 1) * sizeof(*blp)),
269 &needlog, &needscan);
270 blp += INT_GET(btp->stale, ARCH_CONVERT) - 1;
271 INT_SET(btp->stale, ARCH_CONVERT, 1);
273 * If we now need to rebuild the bestfree map, do so.
274 * This needs to happen before the next call to use_free.
277 xfs_dir2_data_freescan(mp, (xfs_dir2_data_t *)block,
283 * Set leaf logging boundaries to impossible state.
284 * For the no-stale case they're set explicitly.
286 else if (INT_GET(btp->stale, ARCH_CONVERT)) {
287 lfloglow = INT_GET(btp->count, ARCH_CONVERT);
291 * Find the slot that's first lower than our hash value, -1 if none.
293 for (low = 0, high = INT_GET(btp->count, ARCH_CONVERT) - 1; low <= high; ) {
294 mid = (low + high) >> 1;
295 if ((hash = INT_GET(blp[mid].hashval, ARCH_CONVERT)) == args->hashval)
297 if (hash < args->hashval)
302 while (mid >= 0 && INT_GET(blp[mid].hashval, ARCH_CONVERT) >= args->hashval) {
306 * No stale entries, will use enddup space to hold new leaf.
310 * Mark the space needed for the new leaf entry, now in use.
312 xfs_dir2_data_use_free(tp, bp, enddup,
313 (xfs_dir2_data_aoff_t)
314 ((char *)enddup - (char *)block + INT_GET(enddup->length, ARCH_CONVERT) -
316 (xfs_dir2_data_aoff_t)sizeof(*blp),
317 &needlog, &needscan);
319 * Update the tail (entry count).
321 INT_MOD(btp->count, ARCH_CONVERT, +1);
323 * If we now need to rebuild the bestfree map, do so.
324 * This needs to happen before the next call to use_free.
327 xfs_dir2_data_freescan(mp, (xfs_dir2_data_t *)block,
332 * Adjust pointer to the first leaf entry, we're about to move
333 * the table up one to open up space for the new leaf entry.
334 * Then adjust our index to match.
339 memmove(blp, &blp[1], mid * sizeof(*blp));
344 * Use a stale leaf for our new entry.
349 INT_GET(blp[lowstale].address, ARCH_CONVERT) != XFS_DIR2_NULL_DATAPTR;
352 for (highstale = mid + 1;
353 highstale < INT_GET(btp->count, ARCH_CONVERT) &&
354 INT_GET(blp[highstale].address, ARCH_CONVERT) != XFS_DIR2_NULL_DATAPTR &&
355 (lowstale < 0 || mid - lowstale > highstale - mid);
359 * Move entries toward the low-numbered stale entry.
362 (highstale == INT_GET(btp->count, ARCH_CONVERT) ||
363 mid - lowstale <= highstale - mid)) {
365 memmove(&blp[lowstale], &blp[lowstale + 1],
366 (mid - lowstale) * sizeof(*blp));
367 lfloglow = MIN(lowstale, lfloglow);
368 lfloghigh = MAX(mid, lfloghigh);
371 * Move entries toward the high-numbered stale entry.
374 ASSERT(highstale < INT_GET(btp->count, ARCH_CONVERT));
377 memmove(&blp[mid + 1], &blp[mid],
378 (highstale - mid) * sizeof(*blp));
379 lfloglow = MIN(mid, lfloglow);
380 lfloghigh = MAX(highstale, lfloghigh);
382 INT_MOD(btp->stale, ARCH_CONVERT, -1);
385 * Point to the new data entry.
387 dep = (xfs_dir2_data_entry_t *)dup;
389 * Fill in the leaf entry.
391 INT_SET(blp[mid].hashval, ARCH_CONVERT, args->hashval);
392 INT_SET(blp[mid].address, ARCH_CONVERT, XFS_DIR2_BYTE_TO_DATAPTR(mp, (char *)dep - (char *)block));
393 xfs_dir2_block_log_leaf(tp, bp, lfloglow, lfloghigh);
395 * Mark space for the data entry used.
397 xfs_dir2_data_use_free(tp, bp, dup,
398 (xfs_dir2_data_aoff_t)((char *)dup - (char *)block),
399 (xfs_dir2_data_aoff_t)len, &needlog, &needscan);
401 * Create the new data entry.
403 INT_SET(dep->inumber, ARCH_CONVERT, args->inumber);
404 dep->namelen = args->namelen;
405 memcpy(dep->name, args->name, args->namelen);
406 tagp = XFS_DIR2_DATA_ENTRY_TAG_P(dep);
407 INT_SET(*tagp, ARCH_CONVERT, (xfs_dir2_data_off_t)((char *)dep - (char *)block));
409 * Clean up the bestfree array and log the header, tail, and entry.
412 xfs_dir2_data_freescan(mp, (xfs_dir2_data_t *)block, &needlog,
415 xfs_dir2_data_log_header(tp, bp);
416 xfs_dir2_block_log_tail(tp, bp);
417 xfs_dir2_data_log_entry(tp, bp, dep);
418 xfs_dir2_data_check(dp, bp);
424 * Readdir for block directories.
427 xfs_dir2_block_getdents(
428 xfs_trans_t *tp, /* transaction (NULL) */
429 xfs_inode_t *dp, /* incore inode */
430 uio_t *uio, /* caller's buffer control */
431 int *eofp, /* eof reached? (out) */
432 xfs_dirent_t *dbp, /* caller's buffer */
433 xfs_dir2_put_t put) /* abi's formatting function */
435 xfs_dir2_block_t *block; /* directory block structure */
436 xfs_dabuf_t *bp; /* buffer for block */
437 xfs_dir2_block_tail_t *btp; /* block tail */
438 xfs_dir2_data_entry_t *dep; /* block data entry */
439 xfs_dir2_data_unused_t *dup; /* block unused entry */
440 char *endptr; /* end of the data entries */
441 int error; /* error return value */
442 xfs_mount_t *mp; /* filesystem mount point */
443 xfs_dir2_put_args_t p; /* arg package for put rtn */
444 char *ptr; /* current data entry */
445 int wantoff; /* starting block offset */
449 * If the block number in the offset is out of range, we're done.
451 if (XFS_DIR2_DATAPTR_TO_DB(mp, uio->uio_offset) > mp->m_dirdatablk) {
456 * Can't read the block, give up, else get dabuf in bp.
459 xfs_da_read_buf(tp, dp, mp->m_dirdatablk, -1, &bp, XFS_DATA_FORK))) {
464 * Extract the byte offset we start at from the seek pointer.
465 * We'll skip entries before this.
467 wantoff = XFS_DIR2_DATAPTR_TO_OFF(mp, uio->uio_offset);
469 xfs_dir2_data_check(dp, bp);
471 * Set up values for the loop.
473 btp = XFS_DIR2_BLOCK_TAIL_P(mp, block);
474 ptr = (char *)block->u;
475 endptr = (char *)XFS_DIR2_BLOCK_LEAF_P(btp);
480 * Loop over the data portion of the block.
481 * Each object is a real entry (dep) or an unused one (dup).
483 while (ptr < endptr) {
484 dup = (xfs_dir2_data_unused_t *)ptr;
488 if (INT_GET(dup->freetag, ARCH_CONVERT) == XFS_DIR2_DATA_FREE_TAG) {
489 ptr += INT_GET(dup->length, ARCH_CONVERT);
493 dep = (xfs_dir2_data_entry_t *)ptr;
496 * Bump pointer for the next iteration.
498 ptr += XFS_DIR2_DATA_ENTSIZE(dep->namelen);
500 * The entry is before the desired starting point, skip it.
502 if ((char *)dep - (char *)block < wantoff)
505 * Set up argument structure for put routine.
507 p.namelen = dep->namelen;
509 p.cook = XFS_DIR2_DB_OFF_TO_DATAPTR(mp, mp->m_dirdatablk,
510 ptr - (char *)block);
511 p.ino = INT_GET(dep->inumber, ARCH_CONVERT);
513 p.ino += mp->m_inoadd;
515 p.name = (char *)dep->name;
518 * Put the entry in the caller's buffer.
523 * If it didn't fit, set the final offset to here & return.
527 XFS_DIR2_DB_OFF_TO_DATAPTR(mp, mp->m_dirdatablk,
528 (char *)dep - (char *)block);
529 xfs_da_brelse(tp, bp);
535 * Reached the end of the block.
536 * Set the offset to a nonexistent block 1 and return.
541 XFS_DIR2_DB_OFF_TO_DATAPTR(mp, mp->m_dirdatablk + 1, 0);
543 xfs_da_brelse(tp, bp);
549 * Log leaf entries from the block.
552 xfs_dir2_block_log_leaf(
553 xfs_trans_t *tp, /* transaction structure */
554 xfs_dabuf_t *bp, /* block buffer */
555 int first, /* index of first logged leaf */
556 int last) /* index of last logged leaf */
558 xfs_dir2_block_t *block; /* directory block structure */
559 xfs_dir2_leaf_entry_t *blp; /* block leaf entries */
560 xfs_dir2_block_tail_t *btp; /* block tail */
561 xfs_mount_t *mp; /* filesystem mount point */
565 btp = XFS_DIR2_BLOCK_TAIL_P(mp, block);
566 blp = XFS_DIR2_BLOCK_LEAF_P(btp);
567 xfs_da_log_buf(tp, bp, (uint)((char *)&blp[first] - (char *)block),
568 (uint)((char *)&blp[last + 1] - (char *)block - 1));
572 * Log the block tail.
575 xfs_dir2_block_log_tail(
576 xfs_trans_t *tp, /* transaction structure */
577 xfs_dabuf_t *bp) /* block buffer */
579 xfs_dir2_block_t *block; /* directory block structure */
580 xfs_dir2_block_tail_t *btp; /* block tail */
581 xfs_mount_t *mp; /* filesystem mount point */
585 btp = XFS_DIR2_BLOCK_TAIL_P(mp, block);
586 xfs_da_log_buf(tp, bp, (uint)((char *)btp - (char *)block),
587 (uint)((char *)(btp + 1) - (char *)block - 1));
591 * Look up an entry in the block. This is the external routine,
592 * xfs_dir2_block_lookup_int does the real work.
595 xfs_dir2_block_lookup(
596 xfs_da_args_t *args) /* dir lookup arguments */
598 xfs_dir2_block_t *block; /* block structure */
599 xfs_dir2_leaf_entry_t *blp; /* block leaf entries */
600 xfs_dabuf_t *bp; /* block buffer */
601 xfs_dir2_block_tail_t *btp; /* block tail */
602 xfs_dir2_data_entry_t *dep; /* block data entry */
603 xfs_inode_t *dp; /* incore inode */
604 int ent; /* entry index */
605 int error; /* error return value */
606 xfs_mount_t *mp; /* filesystem mount point */
608 xfs_dir2_trace_args("block_lookup", args);
610 * Get the buffer, look up the entry.
611 * If not found (ENOENT) then return, have no buffer.
613 if ((error = xfs_dir2_block_lookup_int(args, &bp, &ent)))
618 xfs_dir2_data_check(dp, bp);
619 btp = XFS_DIR2_BLOCK_TAIL_P(mp, block);
620 blp = XFS_DIR2_BLOCK_LEAF_P(btp);
622 * Get the offset from the leaf entry, to point to the data.
624 dep = (xfs_dir2_data_entry_t *)
625 ((char *)block + XFS_DIR2_DATAPTR_TO_OFF(mp, INT_GET(blp[ent].address, ARCH_CONVERT)));
627 * Fill in inode number, release the block.
629 args->inumber = INT_GET(dep->inumber, ARCH_CONVERT);
630 xfs_da_brelse(args->trans, bp);
631 return XFS_ERROR(EEXIST);
635 * Internal block lookup routine.
637 static int /* error */
638 xfs_dir2_block_lookup_int(
639 xfs_da_args_t *args, /* dir lookup arguments */
640 xfs_dabuf_t **bpp, /* returned block buffer */
641 int *entno) /* returned entry number */
643 xfs_dir2_dataptr_t addr; /* data entry address */
644 xfs_dir2_block_t *block; /* block structure */
645 xfs_dir2_leaf_entry_t *blp; /* block leaf entries */
646 xfs_dabuf_t *bp; /* block buffer */
647 xfs_dir2_block_tail_t *btp; /* block tail */
648 xfs_dir2_data_entry_t *dep; /* block data entry */
649 xfs_inode_t *dp; /* incore inode */
650 int error; /* error return value */
651 xfs_dahash_t hash; /* found hash value */
652 int high; /* binary search high index */
653 int low; /* binary search low index */
654 int mid; /* binary search current idx */
655 xfs_mount_t *mp; /* filesystem mount point */
656 xfs_trans_t *tp; /* transaction pointer */
662 * Read the buffer, return error if we can't get it.
665 xfs_da_read_buf(tp, dp, mp->m_dirdatablk, -1, &bp, XFS_DATA_FORK))) {
670 xfs_dir2_data_check(dp, bp);
671 btp = XFS_DIR2_BLOCK_TAIL_P(mp, block);
672 blp = XFS_DIR2_BLOCK_LEAF_P(btp);
674 * Loop doing a binary search for our hash value.
675 * Find our entry, ENOENT if it's not there.
677 for (low = 0, high = INT_GET(btp->count, ARCH_CONVERT) - 1; ; ) {
679 mid = (low + high) >> 1;
680 if ((hash = INT_GET(blp[mid].hashval, ARCH_CONVERT)) == args->hashval)
682 if (hash < args->hashval)
687 ASSERT(args->oknoent);
688 xfs_da_brelse(tp, bp);
689 return XFS_ERROR(ENOENT);
693 * Back up to the first one with the right hash value.
695 while (mid > 0 && INT_GET(blp[mid - 1].hashval, ARCH_CONVERT) == args->hashval) {
699 * Now loop forward through all the entries with the
700 * right hash value looking for our name.
703 if ((addr = INT_GET(blp[mid].address, ARCH_CONVERT)) == XFS_DIR2_NULL_DATAPTR)
706 * Get pointer to the entry from the leaf.
708 dep = (xfs_dir2_data_entry_t *)
709 ((char *)block + XFS_DIR2_DATAPTR_TO_OFF(mp, addr));
711 * Compare, if it's right give back buffer & entry number.
713 if (dep->namelen == args->namelen &&
714 dep->name[0] == args->name[0] &&
715 memcmp(dep->name, args->name, args->namelen) == 0) {
720 } while (++mid < INT_GET(btp->count, ARCH_CONVERT) && INT_GET(blp[mid].hashval, ARCH_CONVERT) == hash);
722 * No match, release the buffer and return ENOENT.
724 ASSERT(args->oknoent);
725 xfs_da_brelse(tp, bp);
726 return XFS_ERROR(ENOENT);
730 * Remove an entry from a block format directory.
731 * If that makes the block small enough to fit in shortform, transform it.
734 xfs_dir2_block_removename(
735 xfs_da_args_t *args) /* directory operation args */
737 xfs_dir2_block_t *block; /* block structure */
738 xfs_dir2_leaf_entry_t *blp; /* block leaf pointer */
739 xfs_dabuf_t *bp; /* block buffer */
740 xfs_dir2_block_tail_t *btp; /* block tail */
741 xfs_dir2_data_entry_t *dep; /* block data entry */
742 xfs_inode_t *dp; /* incore inode */
743 int ent; /* block leaf entry index */
744 int error; /* error return value */
745 xfs_mount_t *mp; /* filesystem mount point */
746 int needlog; /* need to log block header */
747 int needscan; /* need to fixup bestfree */
748 xfs_dir2_sf_hdr_t sfh; /* shortform header */
749 int size; /* shortform size */
750 xfs_trans_t *tp; /* transaction pointer */
752 xfs_dir2_trace_args("block_removename", args);
754 * Look up the entry in the block. Gets the buffer and entry index.
755 * It will always be there, the vnodeops level does a lookup first.
757 if ((error = xfs_dir2_block_lookup_int(args, &bp, &ent))) {
764 btp = XFS_DIR2_BLOCK_TAIL_P(mp, block);
765 blp = XFS_DIR2_BLOCK_LEAF_P(btp);
767 * Point to the data entry using the leaf entry.
769 dep = (xfs_dir2_data_entry_t *)
770 ((char *)block + XFS_DIR2_DATAPTR_TO_OFF(mp, INT_GET(blp[ent].address, ARCH_CONVERT)));
772 * Mark the data entry's space free.
774 needlog = needscan = 0;
775 xfs_dir2_data_make_free(tp, bp,
776 (xfs_dir2_data_aoff_t)((char *)dep - (char *)block),
777 XFS_DIR2_DATA_ENTSIZE(dep->namelen), &needlog, &needscan);
779 * Fix up the block tail.
781 INT_MOD(btp->stale, ARCH_CONVERT, +1);
782 xfs_dir2_block_log_tail(tp, bp);
784 * Remove the leaf entry by marking it stale.
786 INT_SET(blp[ent].address, ARCH_CONVERT, XFS_DIR2_NULL_DATAPTR);
787 xfs_dir2_block_log_leaf(tp, bp, ent, ent);
789 * Fix up bestfree, log the header if necessary.
792 xfs_dir2_data_freescan(mp, (xfs_dir2_data_t *)block, &needlog,
795 xfs_dir2_data_log_header(tp, bp);
796 xfs_dir2_data_check(dp, bp);
798 * See if the size as a shortform is good enough.
800 if ((size = xfs_dir2_block_sfsize(dp, block, &sfh)) >
801 XFS_IFORK_DSIZE(dp)) {
806 * If it works, do the conversion.
808 return xfs_dir2_block_to_sf(args, bp, size, &sfh);
812 * Replace an entry in a V2 block directory.
813 * Change the inode number to the new value.
816 xfs_dir2_block_replace(
817 xfs_da_args_t *args) /* directory operation args */
819 xfs_dir2_block_t *block; /* block structure */
820 xfs_dir2_leaf_entry_t *blp; /* block leaf entries */
821 xfs_dabuf_t *bp; /* block buffer */
822 xfs_dir2_block_tail_t *btp; /* block tail */
823 xfs_dir2_data_entry_t *dep; /* block data entry */
824 xfs_inode_t *dp; /* incore inode */
825 int ent; /* leaf entry index */
826 int error; /* error return value */
827 xfs_mount_t *mp; /* filesystem mount point */
829 xfs_dir2_trace_args("block_replace", args);
831 * Lookup the entry in the directory. Get buffer and entry index.
832 * This will always succeed since the caller has already done a lookup.
834 if ((error = xfs_dir2_block_lookup_int(args, &bp, &ent))) {
840 btp = XFS_DIR2_BLOCK_TAIL_P(mp, block);
841 blp = XFS_DIR2_BLOCK_LEAF_P(btp);
843 * Point to the data entry we need to change.
845 dep = (xfs_dir2_data_entry_t *)
846 ((char *)block + XFS_DIR2_DATAPTR_TO_OFF(mp, INT_GET(blp[ent].address, ARCH_CONVERT)));
847 ASSERT(INT_GET(dep->inumber, ARCH_CONVERT) != args->inumber);
849 * Change the inode number to the new value.
851 INT_SET(dep->inumber, ARCH_CONVERT, args->inumber);
852 xfs_dir2_data_log_entry(args->trans, bp, dep);
853 xfs_dir2_data_check(dp, bp);
859 * Qsort comparison routine for the block leaf entries.
861 static int /* sort order */
863 const void *a, /* first leaf entry */
864 const void *b) /* second leaf entry */
866 const xfs_dir2_leaf_entry_t *la; /* first leaf entry */
867 const xfs_dir2_leaf_entry_t *lb; /* second leaf entry */
871 return INT_GET(la->hashval, ARCH_CONVERT) < INT_GET(lb->hashval, ARCH_CONVERT) ? -1 :
872 (INT_GET(la->hashval, ARCH_CONVERT) > INT_GET(lb->hashval, ARCH_CONVERT) ? 1 : 0);
876 * Convert a V2 leaf directory to a V2 block directory if possible.
879 xfs_dir2_leaf_to_block(
880 xfs_da_args_t *args, /* operation arguments */
881 xfs_dabuf_t *lbp, /* leaf buffer */
882 xfs_dabuf_t *dbp) /* data buffer */
884 xfs_dir2_data_off_t *bestsp; /* leaf bests table */
885 xfs_dir2_block_t *block; /* block structure */
886 xfs_dir2_block_tail_t *btp; /* block tail */
887 xfs_inode_t *dp; /* incore directory inode */
888 xfs_dir2_data_unused_t *dup; /* unused data entry */
889 int error; /* error return value */
890 int from; /* leaf from index */
891 xfs_dir2_leaf_t *leaf; /* leaf structure */
892 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
893 xfs_dir2_leaf_tail_t *ltp; /* leaf tail structure */
894 xfs_mount_t *mp; /* file system mount point */
895 int needlog; /* need to log data header */
896 int needscan; /* need to scan for bestfree */
897 xfs_dir2_sf_hdr_t sfh; /* shortform header */
898 int size; /* bytes used */
899 xfs_dir2_data_off_t *tagp; /* end of entry (tag) */
900 int to; /* block/leaf to index */
901 xfs_trans_t *tp; /* transaction pointer */
903 xfs_dir2_trace_args_bb("leaf_to_block", args, lbp, dbp);
908 ASSERT(INT_GET(leaf->hdr.info.magic, ARCH_CONVERT) == XFS_DIR2_LEAF1_MAGIC);
909 ltp = XFS_DIR2_LEAF_TAIL_P(mp, leaf);
911 * If there are data blocks other than the first one, take this
912 * opportunity to remove trailing empty data blocks that may have
913 * been left behind during no-space-reservation operations.
914 * These will show up in the leaf bests table.
916 while (dp->i_d.di_size > mp->m_dirblksize) {
917 bestsp = XFS_DIR2_LEAF_BESTS_P(ltp);
918 if (INT_GET(bestsp[INT_GET(ltp->bestcount, ARCH_CONVERT) - 1], ARCH_CONVERT) ==
919 mp->m_dirblksize - (uint)sizeof(block->hdr)) {
921 xfs_dir2_leaf_trim_data(args, lbp,
922 (xfs_dir2_db_t)(INT_GET(ltp->bestcount, ARCH_CONVERT) - 1))))
930 * Read the data block if we don't already have it, give up if it fails.
933 (error = xfs_da_read_buf(tp, dp, mp->m_dirdatablk, -1, &dbp,
938 ASSERT(INT_GET(block->hdr.magic, ARCH_CONVERT) == XFS_DIR2_DATA_MAGIC);
940 * Size of the "leaf" area in the block.
942 size = (uint)sizeof(block->tail) +
943 (uint)sizeof(*lep) * (INT_GET(leaf->hdr.count, ARCH_CONVERT) - INT_GET(leaf->hdr.stale, ARCH_CONVERT));
945 * Look at the last data entry.
947 tagp = (xfs_dir2_data_off_t *)((char *)block + mp->m_dirblksize) - 1;
948 dup = (xfs_dir2_data_unused_t *)((char *)block + INT_GET(*tagp, ARCH_CONVERT));
950 * If it's not free or is too short we can't do it.
952 if (INT_GET(dup->freetag, ARCH_CONVERT) != XFS_DIR2_DATA_FREE_TAG || INT_GET(dup->length, ARCH_CONVERT) < size) {
957 * Start converting it to block form.
959 INT_SET(block->hdr.magic, ARCH_CONVERT, XFS_DIR2_BLOCK_MAGIC);
963 * Use up the space at the end of the block (blp/btp).
965 xfs_dir2_data_use_free(tp, dbp, dup, mp->m_dirblksize - size, size,
966 &needlog, &needscan);
968 * Initialize the block tail.
970 btp = XFS_DIR2_BLOCK_TAIL_P(mp, block);
971 INT_SET(btp->count, ARCH_CONVERT, INT_GET(leaf->hdr.count, ARCH_CONVERT) - INT_GET(leaf->hdr.stale, ARCH_CONVERT));
973 xfs_dir2_block_log_tail(tp, dbp);
975 * Initialize the block leaf area. We compact out stale entries.
977 lep = XFS_DIR2_BLOCK_LEAF_P(btp);
978 for (from = to = 0; from < INT_GET(leaf->hdr.count, ARCH_CONVERT); from++) {
979 if (INT_GET(leaf->ents[from].address, ARCH_CONVERT) == XFS_DIR2_NULL_DATAPTR)
981 lep[to++] = leaf->ents[from];
983 ASSERT(to == INT_GET(btp->count, ARCH_CONVERT));
984 xfs_dir2_block_log_leaf(tp, dbp, 0, INT_GET(btp->count, ARCH_CONVERT) - 1);
986 * Scan the bestfree if we need it and log the data block header.
989 xfs_dir2_data_freescan(mp, (xfs_dir2_data_t *)block, &needlog,
992 xfs_dir2_data_log_header(tp, dbp);
994 * Pitch the old leaf block.
996 error = xfs_da_shrink_inode(args, mp->m_dirleafblk, lbp);
1002 * Now see if the resulting block can be shrunken to shortform.
1004 if ((size = xfs_dir2_block_sfsize(dp, block, &sfh)) >
1005 XFS_IFORK_DSIZE(dp)) {
1009 return xfs_dir2_block_to_sf(args, dbp, size, &sfh);
1012 xfs_da_buf_done(lbp);
1014 xfs_da_buf_done(dbp);
1019 * Convert the shortform directory to block form.
1022 xfs_dir2_sf_to_block(
1023 xfs_da_args_t *args) /* operation arguments */
1025 xfs_dir2_db_t blkno; /* dir-relative block # (0) */
1026 xfs_dir2_block_t *block; /* block structure */
1027 xfs_dir2_leaf_entry_t *blp; /* block leaf entries */
1028 xfs_dabuf_t *bp; /* block buffer */
1029 xfs_dir2_block_tail_t *btp; /* block tail pointer */
1030 char *buf; /* sf buffer */
1032 xfs_dir2_data_entry_t *dep; /* data entry pointer */
1033 xfs_inode_t *dp; /* incore directory inode */
1034 int dummy; /* trash */
1035 xfs_dir2_data_unused_t *dup; /* unused entry pointer */
1036 int endoffset; /* end of data objects */
1037 int error; /* error return value */
1039 xfs_mount_t *mp; /* filesystem mount point */
1040 int needlog; /* need to log block header */
1041 int needscan; /* need to scan block freespc */
1042 int newoffset; /* offset from current entry */
1043 int offset; /* target block offset */
1044 xfs_dir2_sf_entry_t *sfep; /* sf entry pointer */
1045 xfs_dir2_sf_t *sfp; /* shortform structure */
1046 xfs_dir2_data_off_t *tagp; /* end of data entry */
1047 xfs_trans_t *tp; /* transaction pointer */
1049 xfs_dir2_trace_args("sf_to_block", args);
1053 ASSERT(dp->i_df.if_flags & XFS_IFINLINE);
1055 * Bomb out if the shortform directory is way too short.
1057 if (dp->i_d.di_size < offsetof(xfs_dir2_sf_hdr_t, parent)) {
1058 ASSERT(XFS_FORCED_SHUTDOWN(mp));
1059 return XFS_ERROR(EIO);
1061 ASSERT(dp->i_df.if_bytes == dp->i_d.di_size);
1062 ASSERT(dp->i_df.if_u1.if_data != NULL);
1063 sfp = (xfs_dir2_sf_t *)dp->i_df.if_u1.if_data;
1064 ASSERT(dp->i_d.di_size >= XFS_DIR2_SF_HDR_SIZE(sfp->hdr.i8count));
1066 * Copy the directory into the stack buffer.
1067 * Then pitch the incore inode data so we can make extents.
1070 buf_len = dp->i_df.if_bytes;
1071 buf = kmem_alloc(dp->i_df.if_bytes, KM_SLEEP);
1073 memcpy(buf, sfp, dp->i_df.if_bytes);
1074 xfs_idata_realloc(dp, -dp->i_df.if_bytes, XFS_DATA_FORK);
1075 dp->i_d.di_size = 0;
1076 xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
1078 * Reset pointer - old sfp is gone.
1080 sfp = (xfs_dir2_sf_t *)buf;
1082 * Add block 0 to the inode.
1084 error = xfs_dir2_grow_inode(args, XFS_DIR2_DATA_SPACE, &blkno);
1086 kmem_free(buf, buf_len);
1090 * Initialize the data block.
1092 error = xfs_dir2_data_init(args, blkno, &bp);
1094 kmem_free(buf, buf_len);
1098 INT_SET(block->hdr.magic, ARCH_CONVERT, XFS_DIR2_BLOCK_MAGIC);
1100 * Compute size of block "tail" area.
1102 i = (uint)sizeof(*btp) +
1103 (INT_GET(sfp->hdr.count, ARCH_CONVERT) + 2) * (uint)sizeof(xfs_dir2_leaf_entry_t);
1105 * The whole thing is initialized to free by the init routine.
1106 * Say we're using the leaf and tail area.
1108 dup = (xfs_dir2_data_unused_t *)block->u;
1109 needlog = needscan = 0;
1110 xfs_dir2_data_use_free(tp, bp, dup, mp->m_dirblksize - i, i, &needlog,
1112 ASSERT(needscan == 0);
1116 btp = XFS_DIR2_BLOCK_TAIL_P(mp, block);
1117 INT_SET(btp->count, ARCH_CONVERT, INT_GET(sfp->hdr.count, ARCH_CONVERT) + 2); /* ., .. */
1119 blp = XFS_DIR2_BLOCK_LEAF_P(btp);
1120 endoffset = (uint)((char *)blp - (char *)block);
1122 * Remove the freespace, we'll manage it.
1124 xfs_dir2_data_use_free(tp, bp, dup,
1125 (xfs_dir2_data_aoff_t)((char *)dup - (char *)block),
1126 INT_GET(dup->length, ARCH_CONVERT), &needlog, &needscan);
1128 * Create entry for .
1130 dep = (xfs_dir2_data_entry_t *)
1131 ((char *)block + XFS_DIR2_DATA_DOT_OFFSET);
1132 INT_SET(dep->inumber, ARCH_CONVERT, dp->i_ino);
1135 tagp = XFS_DIR2_DATA_ENTRY_TAG_P(dep);
1136 INT_SET(*tagp, ARCH_CONVERT, (xfs_dir2_data_off_t)((char *)dep - (char *)block));
1137 xfs_dir2_data_log_entry(tp, bp, dep);
1138 INT_SET(blp[0].hashval, ARCH_CONVERT, xfs_dir_hash_dot);
1139 INT_SET(blp[0].address, ARCH_CONVERT, XFS_DIR2_BYTE_TO_DATAPTR(mp, (char *)dep - (char *)block));
1141 * Create entry for ..
1143 dep = (xfs_dir2_data_entry_t *)
1144 ((char *)block + XFS_DIR2_DATA_DOTDOT_OFFSET);
1145 INT_SET(dep->inumber, ARCH_CONVERT, XFS_DIR2_SF_GET_INUMBER(sfp, &sfp->hdr.parent));
1147 dep->name[0] = dep->name[1] = '.';
1148 tagp = XFS_DIR2_DATA_ENTRY_TAG_P(dep);
1149 INT_SET(*tagp, ARCH_CONVERT, (xfs_dir2_data_off_t)((char *)dep - (char *)block));
1150 xfs_dir2_data_log_entry(tp, bp, dep);
1151 INT_SET(blp[1].hashval, ARCH_CONVERT, xfs_dir_hash_dotdot);
1152 INT_SET(blp[1].address, ARCH_CONVERT, XFS_DIR2_BYTE_TO_DATAPTR(mp, (char *)dep - (char *)block));
1153 offset = XFS_DIR2_DATA_FIRST_OFFSET;
1155 * Loop over existing entries, stuff them in.
1157 if ((i = 0) == INT_GET(sfp->hdr.count, ARCH_CONVERT))
1160 sfep = XFS_DIR2_SF_FIRSTENTRY(sfp);
1162 * Need to preserve the existing offset values in the sf directory.
1163 * Insert holes (unused entries) where necessary.
1165 while (offset < endoffset) {
1167 * sfep is null when we reach the end of the list.
1170 newoffset = endoffset;
1172 newoffset = XFS_DIR2_SF_GET_OFFSET(sfep);
1174 * There should be a hole here, make one.
1176 if (offset < newoffset) {
1177 dup = (xfs_dir2_data_unused_t *)
1178 ((char *)block + offset);
1179 INT_SET(dup->freetag, ARCH_CONVERT, XFS_DIR2_DATA_FREE_TAG);
1180 INT_SET(dup->length, ARCH_CONVERT, newoffset - offset);
1181 INT_SET(*XFS_DIR2_DATA_UNUSED_TAG_P(dup), ARCH_CONVERT,
1182 (xfs_dir2_data_off_t)
1183 ((char *)dup - (char *)block));
1184 xfs_dir2_data_log_unused(tp, bp, dup);
1185 (void)xfs_dir2_data_freeinsert((xfs_dir2_data_t *)block,
1187 offset += INT_GET(dup->length, ARCH_CONVERT);
1191 * Copy a real entry.
1193 dep = (xfs_dir2_data_entry_t *)((char *)block + newoffset);
1194 INT_SET(dep->inumber, ARCH_CONVERT, XFS_DIR2_SF_GET_INUMBER(sfp,
1195 XFS_DIR2_SF_INUMBERP(sfep)));
1196 dep->namelen = sfep->namelen;
1197 memcpy(dep->name, sfep->name, dep->namelen);
1198 tagp = XFS_DIR2_DATA_ENTRY_TAG_P(dep);
1199 INT_SET(*tagp, ARCH_CONVERT, (xfs_dir2_data_off_t)((char *)dep - (char *)block));
1200 xfs_dir2_data_log_entry(tp, bp, dep);
1201 INT_SET(blp[2 + i].hashval, ARCH_CONVERT, xfs_da_hashname((char *)sfep->name, sfep->namelen));
1202 INT_SET(blp[2 + i].address, ARCH_CONVERT, XFS_DIR2_BYTE_TO_DATAPTR(mp,
1203 (char *)dep - (char *)block));
1204 offset = (int)((char *)(tagp + 1) - (char *)block);
1205 if (++i == INT_GET(sfp->hdr.count, ARCH_CONVERT))
1208 sfep = XFS_DIR2_SF_NEXTENTRY(sfp, sfep);
1210 /* Done with the temporary buffer */
1211 kmem_free(buf, buf_len);
1213 * Sort the leaf entries by hash value.
1215 xfs_sort(blp, INT_GET(btp->count, ARCH_CONVERT), sizeof(*blp), xfs_dir2_block_sort);
1217 * Log the leaf entry area and tail.
1218 * Already logged the header in data_init, ignore needlog.
1220 ASSERT(needscan == 0);
1221 xfs_dir2_block_log_leaf(tp, bp, 0, INT_GET(btp->count, ARCH_CONVERT) - 1);
1222 xfs_dir2_block_log_tail(tp, bp);
1223 xfs_dir2_data_check(dp, bp);
1224 xfs_da_buf_done(bp);