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);
60 xfs_dir2_block_verify(
63 struct xfs_mount *mp = bp->b_target->bt_mount;
64 struct xfs_dir2_data_hdr *hdr = bp->b_addr;
67 block_ok = hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC);
68 block_ok = block_ok && __xfs_dir2_data_check(NULL, bp) == 0;
71 XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp, hdr);
72 xfs_buf_ioerror(bp, EFSCORRUPTED);
85 struct xfs_mount *mp = dp->i_mount;
87 return xfs_da_read_buf(tp, dp, mp->m_dirdatablk, -1, bpp,
88 XFS_DATA_FORK, xfs_dir2_block_verify);
92 xfs_dir2_block_need_space(
93 struct xfs_dir2_data_hdr *hdr,
94 struct xfs_dir2_block_tail *btp,
95 struct xfs_dir2_leaf_entry *blp,
97 struct xfs_dir2_data_unused **dupp,
98 struct xfs_dir2_data_unused **enddupp,
102 struct xfs_dir2_data_free *bf;
104 struct xfs_dir2_data_unused *dup = NULL;
105 struct xfs_dir2_data_unused *enddup = NULL;
111 * If there are stale entries we'll use one for the leaf.
114 if (be16_to_cpu(bf[0].length) >= len) {
116 * The biggest entry enough to avoid compaction.
118 dup = (xfs_dir2_data_unused_t *)
119 ((char *)hdr + be16_to_cpu(bf[0].offset));
124 * Will need to compact to make this work.
125 * Tag just before the first leaf entry.
128 tagp = (__be16 *)blp - 1;
130 /* Data object just before the first leaf entry. */
131 dup = (xfs_dir2_data_unused_t *)((char *)hdr + be16_to_cpu(*tagp));
134 * If it's not free then the data will go where the
135 * leaf data starts now, if it works at all.
137 if (be16_to_cpu(dup->freetag) == XFS_DIR2_DATA_FREE_TAG) {
138 if (be16_to_cpu(dup->length) + (be32_to_cpu(btp->stale) - 1) *
139 (uint)sizeof(*blp) < len)
141 } else if ((be32_to_cpu(btp->stale) - 1) * (uint)sizeof(*blp) < len)
144 dup = (xfs_dir2_data_unused_t *)blp;
149 * no stale entries, so just use free space.
150 * Tag just before the first leaf entry.
152 tagp = (__be16 *)blp - 1;
154 /* Data object just before the first leaf entry. */
155 enddup = (xfs_dir2_data_unused_t *)((char *)hdr + be16_to_cpu(*tagp));
158 * If it's not free then can't do this add without cleaning up:
159 * the space before the first leaf entry needs to be free so it
160 * can be expanded to hold the pointer to the new entry.
162 if (be16_to_cpu(enddup->freetag) == XFS_DIR2_DATA_FREE_TAG) {
164 * Check out the biggest freespace and see if it's the same one.
166 dup = (xfs_dir2_data_unused_t *)
167 ((char *)hdr + be16_to_cpu(bf[0].offset));
170 * Not the same free entry, just check its length.
172 if (be16_to_cpu(dup->length) < len)
178 * It is the biggest freespace, can it hold the leaf too?
180 if (be16_to_cpu(dup->length) < len + (uint)sizeof(*blp)) {
182 * Yes, use the second-largest entry instead if it works.
184 if (be16_to_cpu(bf[1].length) >= len)
185 dup = (xfs_dir2_data_unused_t *)
186 ((char *)hdr + be16_to_cpu(bf[1].offset));
198 * compact the leaf entries.
199 * Leave the highest-numbered stale entry stale.
200 * XXX should be the one closest to mid but mid is not yet computed.
203 xfs_dir2_block_compact(
204 struct xfs_trans *tp,
206 struct xfs_dir2_data_hdr *hdr,
207 struct xfs_dir2_block_tail *btp,
208 struct xfs_dir2_leaf_entry *blp,
213 int fromidx; /* source leaf index */
214 int toidx; /* target leaf index */
216 int highstale; /* high stale index */
218 fromidx = toidx = be32_to_cpu(btp->count) - 1;
219 highstale = *lfloghigh = -1;
220 for (; fromidx >= 0; fromidx--) {
221 if (blp[fromidx].address == cpu_to_be32(XFS_DIR2_NULL_DATAPTR)) {
225 if (*lfloghigh == -1)
231 blp[toidx] = blp[fromidx];
234 *lfloglow = toidx + 1 - (be32_to_cpu(btp->stale) - 1);
235 *lfloghigh -= be32_to_cpu(btp->stale) - 1;
236 be32_add_cpu(&btp->count, -(be32_to_cpu(btp->stale) - 1));
237 xfs_dir2_data_make_free(tp, bp,
238 (xfs_dir2_data_aoff_t)((char *)blp - (char *)hdr),
239 (xfs_dir2_data_aoff_t)((be32_to_cpu(btp->stale) - 1) * sizeof(*blp)),
241 blp += be32_to_cpu(btp->stale) - 1;
242 btp->stale = cpu_to_be32(1);
244 * If we now need to rebuild the bestfree map, do so.
245 * This needs to happen before the next call to use_free.
248 xfs_dir2_data_freescan(tp->t_mountp, hdr, needlog);
252 * Add an entry to a block directory.
255 xfs_dir2_block_addname(
256 xfs_da_args_t *args) /* directory op arguments */
258 xfs_dir2_data_hdr_t *hdr; /* block header */
259 xfs_dir2_leaf_entry_t *blp; /* block leaf entries */
260 struct xfs_buf *bp; /* buffer for block */
261 xfs_dir2_block_tail_t *btp; /* block tail */
262 int compact; /* need to compact leaf ents */
263 xfs_dir2_data_entry_t *dep; /* block data entry */
264 xfs_inode_t *dp; /* directory inode */
265 xfs_dir2_data_unused_t *dup; /* block unused entry */
266 int error; /* error return value */
267 xfs_dir2_data_unused_t *enddup=NULL; /* unused at end of data */
268 xfs_dahash_t hash; /* hash value of found entry */
269 int high; /* high index for binary srch */
270 int highstale; /* high stale index */
271 int lfloghigh=0; /* last final leaf to log */
272 int lfloglow=0; /* first final leaf to log */
273 int len; /* length of the new entry */
274 int low; /* low index for binary srch */
275 int lowstale; /* low stale index */
276 int mid=0; /* midpoint for binary srch */
277 xfs_mount_t *mp; /* filesystem mount point */
278 int needlog; /* need to log header */
279 int needscan; /* need to rescan freespace */
280 __be16 *tagp; /* pointer to tag value */
281 xfs_trans_t *tp; /* transaction structure */
283 trace_xfs_dir2_block_addname(args);
289 /* Read the (one and only) directory block into bp. */
290 error = xfs_dir2_block_read(tp, dp, &bp);
294 len = xfs_dir2_data_entsize(args->namelen);
297 * Set up pointers to parts of the block.
300 btp = xfs_dir2_block_tail_p(mp, hdr);
301 blp = xfs_dir2_block_leaf_p(btp);
304 * Find out if we can reuse stale entries or whether we need extra
305 * space for entry and new leaf.
307 xfs_dir2_block_need_space(hdr, btp, blp, &tagp, &dup,
308 &enddup, &compact, len);
311 * Done everything we need for a space check now.
313 if (args->op_flags & XFS_DA_OP_JUSTCHECK) {
314 xfs_trans_brelse(tp, bp);
316 return XFS_ERROR(ENOSPC);
321 * If we don't have space for the new entry & leaf ...
324 /* Don't have a space reservation: return no-space. */
325 if (args->total == 0)
326 return XFS_ERROR(ENOSPC);
328 * Convert to the next larger format.
329 * Then add the new entry in that format.
331 error = xfs_dir2_block_to_leaf(args, bp);
334 return xfs_dir2_leaf_addname(args);
337 needlog = needscan = 0;
340 * If need to compact the leaf entries, do it now.
343 xfs_dir2_block_compact(tp, bp, hdr, btp, blp, &needlog,
344 &lfloghigh, &lfloglow);
345 else if (btp->stale) {
347 * Set leaf logging boundaries to impossible state.
348 * For the no-stale case they're set explicitly.
350 lfloglow = be32_to_cpu(btp->count);
355 * Find the slot that's first lower than our hash value, -1 if none.
357 for (low = 0, high = be32_to_cpu(btp->count) - 1; low <= high; ) {
358 mid = (low + high) >> 1;
359 if ((hash = be32_to_cpu(blp[mid].hashval)) == args->hashval)
361 if (hash < args->hashval)
366 while (mid >= 0 && be32_to_cpu(blp[mid].hashval) >= args->hashval) {
370 * No stale entries, will use enddup space to hold new leaf.
374 * Mark the space needed for the new leaf entry, now in use.
376 xfs_dir2_data_use_free(tp, bp, enddup,
377 (xfs_dir2_data_aoff_t)
378 ((char *)enddup - (char *)hdr + be16_to_cpu(enddup->length) -
380 (xfs_dir2_data_aoff_t)sizeof(*blp),
381 &needlog, &needscan);
383 * Update the tail (entry count).
385 be32_add_cpu(&btp->count, 1);
387 * If we now need to rebuild the bestfree map, do so.
388 * This needs to happen before the next call to use_free.
391 xfs_dir2_data_freescan(mp, hdr, &needlog);
395 * Adjust pointer to the first leaf entry, we're about to move
396 * the table up one to open up space for the new leaf entry.
397 * Then adjust our index to match.
402 memmove(blp, &blp[1], mid * sizeof(*blp));
407 * Use a stale leaf for our new entry.
412 blp[lowstale].address !=
413 cpu_to_be32(XFS_DIR2_NULL_DATAPTR);
416 for (highstale = mid + 1;
417 highstale < be32_to_cpu(btp->count) &&
418 blp[highstale].address !=
419 cpu_to_be32(XFS_DIR2_NULL_DATAPTR) &&
420 (lowstale < 0 || mid - lowstale > highstale - mid);
424 * Move entries toward the low-numbered stale entry.
427 (highstale == be32_to_cpu(btp->count) ||
428 mid - lowstale <= highstale - mid)) {
430 memmove(&blp[lowstale], &blp[lowstale + 1],
431 (mid - lowstale) * sizeof(*blp));
432 lfloglow = MIN(lowstale, lfloglow);
433 lfloghigh = MAX(mid, lfloghigh);
436 * Move entries toward the high-numbered stale entry.
439 ASSERT(highstale < be32_to_cpu(btp->count));
442 memmove(&blp[mid + 1], &blp[mid],
443 (highstale - mid) * sizeof(*blp));
444 lfloglow = MIN(mid, lfloglow);
445 lfloghigh = MAX(highstale, lfloghigh);
447 be32_add_cpu(&btp->stale, -1);
450 * Point to the new data entry.
452 dep = (xfs_dir2_data_entry_t *)dup;
454 * Fill in the leaf entry.
456 blp[mid].hashval = cpu_to_be32(args->hashval);
457 blp[mid].address = cpu_to_be32(xfs_dir2_byte_to_dataptr(mp,
458 (char *)dep - (char *)hdr));
459 xfs_dir2_block_log_leaf(tp, bp, lfloglow, lfloghigh);
461 * Mark space for the data entry used.
463 xfs_dir2_data_use_free(tp, bp, dup,
464 (xfs_dir2_data_aoff_t)((char *)dup - (char *)hdr),
465 (xfs_dir2_data_aoff_t)len, &needlog, &needscan);
467 * Create the new data entry.
469 dep->inumber = cpu_to_be64(args->inumber);
470 dep->namelen = args->namelen;
471 memcpy(dep->name, args->name, args->namelen);
472 tagp = xfs_dir2_data_entry_tag_p(dep);
473 *tagp = cpu_to_be16((char *)dep - (char *)hdr);
475 * Clean up the bestfree array and log the header, tail, and entry.
478 xfs_dir2_data_freescan(mp, hdr, &needlog);
480 xfs_dir2_data_log_header(tp, bp);
481 xfs_dir2_block_log_tail(tp, bp);
482 xfs_dir2_data_log_entry(tp, bp, dep);
483 xfs_dir2_data_check(dp, bp);
488 * Readdir for block directories.
491 xfs_dir2_block_getdents(
492 xfs_inode_t *dp, /* incore inode */
497 xfs_dir2_data_hdr_t *hdr; /* block header */
498 struct xfs_buf *bp; /* buffer for block */
499 xfs_dir2_block_tail_t *btp; /* block tail */
500 xfs_dir2_data_entry_t *dep; /* block data entry */
501 xfs_dir2_data_unused_t *dup; /* block unused entry */
502 char *endptr; /* end of the data entries */
503 int error; /* error return value */
504 xfs_mount_t *mp; /* filesystem mount point */
505 char *ptr; /* current data entry */
506 int wantoff; /* starting block offset */
511 * If the block number in the offset is out of range, we're done.
513 if (xfs_dir2_dataptr_to_db(mp, *offset) > mp->m_dirdatablk)
516 error = xfs_dir2_block_read(NULL, dp, &bp);
521 * Extract the byte offset we start at from the seek pointer.
522 * We'll skip entries before this.
524 wantoff = xfs_dir2_dataptr_to_off(mp, *offset);
526 xfs_dir2_data_check(dp, bp);
528 * Set up values for the loop.
530 btp = xfs_dir2_block_tail_p(mp, hdr);
531 ptr = (char *)(hdr + 1);
532 endptr = (char *)xfs_dir2_block_leaf_p(btp);
535 * Loop over the data portion of the block.
536 * Each object is a real entry (dep) or an unused one (dup).
538 while (ptr < endptr) {
539 dup = (xfs_dir2_data_unused_t *)ptr;
543 if (be16_to_cpu(dup->freetag) == XFS_DIR2_DATA_FREE_TAG) {
544 ptr += be16_to_cpu(dup->length);
548 dep = (xfs_dir2_data_entry_t *)ptr;
551 * Bump pointer for the next iteration.
553 ptr += xfs_dir2_data_entsize(dep->namelen);
555 * The entry is before the desired starting point, skip it.
557 if ((char *)dep - (char *)hdr < wantoff)
560 cook = xfs_dir2_db_off_to_dataptr(mp, mp->m_dirdatablk,
561 (char *)dep - (char *)hdr);
564 * If it didn't fit, set the final offset to here & return.
566 if (filldir(dirent, (char *)dep->name, dep->namelen,
567 cook & 0x7fffffff, be64_to_cpu(dep->inumber),
569 *offset = cook & 0x7fffffff;
570 xfs_trans_brelse(NULL, bp);
576 * Reached the end of the block.
577 * Set the offset to a non-existent block 1 and return.
579 *offset = xfs_dir2_db_off_to_dataptr(mp, mp->m_dirdatablk + 1, 0) &
581 xfs_trans_brelse(NULL, bp);
586 * Log leaf entries from the block.
589 xfs_dir2_block_log_leaf(
590 xfs_trans_t *tp, /* transaction structure */
591 struct xfs_buf *bp, /* block buffer */
592 int first, /* index of first logged leaf */
593 int last) /* index of last logged leaf */
595 xfs_dir2_data_hdr_t *hdr = bp->b_addr;
596 xfs_dir2_leaf_entry_t *blp;
597 xfs_dir2_block_tail_t *btp;
599 btp = xfs_dir2_block_tail_p(tp->t_mountp, hdr);
600 blp = xfs_dir2_block_leaf_p(btp);
601 xfs_trans_log_buf(tp, bp, (uint)((char *)&blp[first] - (char *)hdr),
602 (uint)((char *)&blp[last + 1] - (char *)hdr - 1));
606 * Log the block tail.
609 xfs_dir2_block_log_tail(
610 xfs_trans_t *tp, /* transaction structure */
611 struct xfs_buf *bp) /* block buffer */
613 xfs_dir2_data_hdr_t *hdr = bp->b_addr;
614 xfs_dir2_block_tail_t *btp;
616 btp = xfs_dir2_block_tail_p(tp->t_mountp, hdr);
617 xfs_trans_log_buf(tp, bp, (uint)((char *)btp - (char *)hdr),
618 (uint)((char *)(btp + 1) - (char *)hdr - 1));
622 * Look up an entry in the block. This is the external routine,
623 * xfs_dir2_block_lookup_int does the real work.
626 xfs_dir2_block_lookup(
627 xfs_da_args_t *args) /* dir lookup arguments */
629 xfs_dir2_data_hdr_t *hdr; /* block header */
630 xfs_dir2_leaf_entry_t *blp; /* block leaf entries */
631 struct xfs_buf *bp; /* block buffer */
632 xfs_dir2_block_tail_t *btp; /* block tail */
633 xfs_dir2_data_entry_t *dep; /* block data entry */
634 xfs_inode_t *dp; /* incore inode */
635 int ent; /* entry index */
636 int error; /* error return value */
637 xfs_mount_t *mp; /* filesystem mount point */
639 trace_xfs_dir2_block_lookup(args);
642 * Get the buffer, look up the entry.
643 * If not found (ENOENT) then return, have no buffer.
645 if ((error = xfs_dir2_block_lookup_int(args, &bp, &ent)))
650 xfs_dir2_data_check(dp, bp);
651 btp = xfs_dir2_block_tail_p(mp, hdr);
652 blp = xfs_dir2_block_leaf_p(btp);
654 * Get the offset from the leaf entry, to point to the data.
656 dep = (xfs_dir2_data_entry_t *)((char *)hdr +
657 xfs_dir2_dataptr_to_off(mp, be32_to_cpu(blp[ent].address)));
659 * Fill in inode number, CI name if appropriate, release the block.
661 args->inumber = be64_to_cpu(dep->inumber);
662 error = xfs_dir_cilookup_result(args, dep->name, dep->namelen);
663 xfs_trans_brelse(args->trans, bp);
664 return XFS_ERROR(error);
668 * Internal block lookup routine.
670 static int /* error */
671 xfs_dir2_block_lookup_int(
672 xfs_da_args_t *args, /* dir lookup arguments */
673 struct xfs_buf **bpp, /* returned block buffer */
674 int *entno) /* returned entry number */
676 xfs_dir2_dataptr_t addr; /* data entry address */
677 xfs_dir2_data_hdr_t *hdr; /* block header */
678 xfs_dir2_leaf_entry_t *blp; /* block leaf entries */
679 struct xfs_buf *bp; /* block buffer */
680 xfs_dir2_block_tail_t *btp; /* block tail */
681 xfs_dir2_data_entry_t *dep; /* block data entry */
682 xfs_inode_t *dp; /* incore inode */
683 int error; /* error return value */
684 xfs_dahash_t hash; /* found hash value */
685 int high; /* binary search high index */
686 int low; /* binary search low index */
687 int mid; /* binary search current idx */
688 xfs_mount_t *mp; /* filesystem mount point */
689 xfs_trans_t *tp; /* transaction pointer */
690 enum xfs_dacmp cmp; /* comparison result */
696 error = xfs_dir2_block_read(tp, dp, &bp);
701 xfs_dir2_data_check(dp, bp);
702 btp = xfs_dir2_block_tail_p(mp, hdr);
703 blp = xfs_dir2_block_leaf_p(btp);
705 * Loop doing a binary search for our hash value.
706 * Find our entry, ENOENT if it's not there.
708 for (low = 0, high = be32_to_cpu(btp->count) - 1; ; ) {
710 mid = (low + high) >> 1;
711 if ((hash = be32_to_cpu(blp[mid].hashval)) == args->hashval)
713 if (hash < args->hashval)
718 ASSERT(args->op_flags & XFS_DA_OP_OKNOENT);
719 xfs_trans_brelse(tp, bp);
720 return XFS_ERROR(ENOENT);
724 * Back up to the first one with the right hash value.
726 while (mid > 0 && be32_to_cpu(blp[mid - 1].hashval) == args->hashval) {
730 * Now loop forward through all the entries with the
731 * right hash value looking for our name.
734 if ((addr = be32_to_cpu(blp[mid].address)) == XFS_DIR2_NULL_DATAPTR)
737 * Get pointer to the entry from the leaf.
739 dep = (xfs_dir2_data_entry_t *)
740 ((char *)hdr + xfs_dir2_dataptr_to_off(mp, addr));
742 * Compare name and if it's an exact match, return the index
743 * and buffer. If it's the first case-insensitive match, store
744 * the index and buffer and continue looking for an exact match.
746 cmp = mp->m_dirnameops->compname(args, dep->name, dep->namelen);
747 if (cmp != XFS_CMP_DIFFERENT && cmp != args->cmpresult) {
748 args->cmpresult = cmp;
751 if (cmp == XFS_CMP_EXACT)
754 } while (++mid < be32_to_cpu(btp->count) &&
755 be32_to_cpu(blp[mid].hashval) == hash);
757 ASSERT(args->op_flags & XFS_DA_OP_OKNOENT);
759 * Here, we can only be doing a lookup (not a rename or replace).
760 * If a case-insensitive match was found earlier, return success.
762 if (args->cmpresult == XFS_CMP_CASE)
765 * No match, release the buffer and return ENOENT.
767 xfs_trans_brelse(tp, bp);
768 return XFS_ERROR(ENOENT);
772 * Remove an entry from a block format directory.
773 * If that makes the block small enough to fit in shortform, transform it.
776 xfs_dir2_block_removename(
777 xfs_da_args_t *args) /* directory operation args */
779 xfs_dir2_data_hdr_t *hdr; /* block header */
780 xfs_dir2_leaf_entry_t *blp; /* block leaf pointer */
781 struct xfs_buf *bp; /* block buffer */
782 xfs_dir2_block_tail_t *btp; /* block tail */
783 xfs_dir2_data_entry_t *dep; /* block data entry */
784 xfs_inode_t *dp; /* incore inode */
785 int ent; /* block leaf entry index */
786 int error; /* error return value */
787 xfs_mount_t *mp; /* filesystem mount point */
788 int needlog; /* need to log block header */
789 int needscan; /* need to fixup bestfree */
790 xfs_dir2_sf_hdr_t sfh; /* shortform header */
791 int size; /* shortform size */
792 xfs_trans_t *tp; /* transaction pointer */
794 trace_xfs_dir2_block_removename(args);
797 * Look up the entry in the block. Gets the buffer and entry index.
798 * It will always be there, the vnodeops level does a lookup first.
800 if ((error = xfs_dir2_block_lookup_int(args, &bp, &ent))) {
807 btp = xfs_dir2_block_tail_p(mp, hdr);
808 blp = xfs_dir2_block_leaf_p(btp);
810 * Point to the data entry using the leaf entry.
812 dep = (xfs_dir2_data_entry_t *)
813 ((char *)hdr + xfs_dir2_dataptr_to_off(mp, be32_to_cpu(blp[ent].address)));
815 * Mark the data entry's space free.
817 needlog = needscan = 0;
818 xfs_dir2_data_make_free(tp, bp,
819 (xfs_dir2_data_aoff_t)((char *)dep - (char *)hdr),
820 xfs_dir2_data_entsize(dep->namelen), &needlog, &needscan);
822 * Fix up the block tail.
824 be32_add_cpu(&btp->stale, 1);
825 xfs_dir2_block_log_tail(tp, bp);
827 * Remove the leaf entry by marking it stale.
829 blp[ent].address = cpu_to_be32(XFS_DIR2_NULL_DATAPTR);
830 xfs_dir2_block_log_leaf(tp, bp, ent, ent);
832 * Fix up bestfree, log the header if necessary.
835 xfs_dir2_data_freescan(mp, hdr, &needlog);
837 xfs_dir2_data_log_header(tp, bp);
838 xfs_dir2_data_check(dp, bp);
840 * See if the size as a shortform is good enough.
842 size = xfs_dir2_block_sfsize(dp, hdr, &sfh);
843 if (size > XFS_IFORK_DSIZE(dp))
847 * If it works, do the conversion.
849 return xfs_dir2_block_to_sf(args, bp, size, &sfh);
853 * Replace an entry in a V2 block directory.
854 * Change the inode number to the new value.
857 xfs_dir2_block_replace(
858 xfs_da_args_t *args) /* directory operation args */
860 xfs_dir2_data_hdr_t *hdr; /* block header */
861 xfs_dir2_leaf_entry_t *blp; /* block leaf entries */
862 struct xfs_buf *bp; /* block buffer */
863 xfs_dir2_block_tail_t *btp; /* block tail */
864 xfs_dir2_data_entry_t *dep; /* block data entry */
865 xfs_inode_t *dp; /* incore inode */
866 int ent; /* leaf entry index */
867 int error; /* error return value */
868 xfs_mount_t *mp; /* filesystem mount point */
870 trace_xfs_dir2_block_replace(args);
873 * Lookup the entry in the directory. Get buffer and entry index.
874 * This will always succeed since the caller has already done a lookup.
876 if ((error = xfs_dir2_block_lookup_int(args, &bp, &ent))) {
882 btp = xfs_dir2_block_tail_p(mp, hdr);
883 blp = xfs_dir2_block_leaf_p(btp);
885 * Point to the data entry we need to change.
887 dep = (xfs_dir2_data_entry_t *)
888 ((char *)hdr + xfs_dir2_dataptr_to_off(mp, be32_to_cpu(blp[ent].address)));
889 ASSERT(be64_to_cpu(dep->inumber) != args->inumber);
891 * Change the inode number to the new value.
893 dep->inumber = cpu_to_be64(args->inumber);
894 xfs_dir2_data_log_entry(args->trans, bp, dep);
895 xfs_dir2_data_check(dp, bp);
900 * Qsort comparison routine for the block leaf entries.
902 static int /* sort order */
904 const void *a, /* first leaf entry */
905 const void *b) /* second leaf entry */
907 const xfs_dir2_leaf_entry_t *la; /* first leaf entry */
908 const xfs_dir2_leaf_entry_t *lb; /* second leaf entry */
912 return be32_to_cpu(la->hashval) < be32_to_cpu(lb->hashval) ? -1 :
913 (be32_to_cpu(la->hashval) > be32_to_cpu(lb->hashval) ? 1 : 0);
917 * Convert a V2 leaf directory to a V2 block directory if possible.
920 xfs_dir2_leaf_to_block(
921 xfs_da_args_t *args, /* operation arguments */
922 struct xfs_buf *lbp, /* leaf buffer */
923 struct xfs_buf *dbp) /* data buffer */
925 __be16 *bestsp; /* leaf bests table */
926 xfs_dir2_data_hdr_t *hdr; /* block header */
927 xfs_dir2_block_tail_t *btp; /* block tail */
928 xfs_inode_t *dp; /* incore directory inode */
929 xfs_dir2_data_unused_t *dup; /* unused data entry */
930 int error; /* error return value */
931 int from; /* leaf from index */
932 xfs_dir2_leaf_t *leaf; /* leaf structure */
933 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
934 xfs_dir2_leaf_tail_t *ltp; /* leaf tail structure */
935 xfs_mount_t *mp; /* file system mount point */
936 int needlog; /* need to log data header */
937 int needscan; /* need to scan for bestfree */
938 xfs_dir2_sf_hdr_t sfh; /* shortform header */
939 int size; /* bytes used */
940 __be16 *tagp; /* end of entry (tag) */
941 int to; /* block/leaf to index */
942 xfs_trans_t *tp; /* transaction pointer */
944 trace_xfs_dir2_leaf_to_block(args);
950 ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAF1_MAGIC));
951 ltp = xfs_dir2_leaf_tail_p(mp, leaf);
953 * If there are data blocks other than the first one, take this
954 * opportunity to remove trailing empty data blocks that may have
955 * been left behind during no-space-reservation operations.
956 * These will show up in the leaf bests table.
958 while (dp->i_d.di_size > mp->m_dirblksize) {
959 bestsp = xfs_dir2_leaf_bests_p(ltp);
960 if (be16_to_cpu(bestsp[be32_to_cpu(ltp->bestcount) - 1]) ==
961 mp->m_dirblksize - (uint)sizeof(*hdr)) {
963 xfs_dir2_leaf_trim_data(args, lbp,
964 (xfs_dir2_db_t)(be32_to_cpu(ltp->bestcount) - 1))))
970 * Read the data block if we don't already have it, give up if it fails.
973 error = xfs_da_read_buf(tp, dp, mp->m_dirdatablk, -1, &dbp,
974 XFS_DATA_FORK, NULL);
979 ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC));
981 * Size of the "leaf" area in the block.
983 size = (uint)sizeof(xfs_dir2_block_tail_t) +
984 (uint)sizeof(*lep) * (be16_to_cpu(leaf->hdr.count) - be16_to_cpu(leaf->hdr.stale));
986 * Look at the last data entry.
988 tagp = (__be16 *)((char *)hdr + mp->m_dirblksize) - 1;
989 dup = (xfs_dir2_data_unused_t *)((char *)hdr + be16_to_cpu(*tagp));
991 * If it's not free or is too short we can't do it.
993 if (be16_to_cpu(dup->freetag) != XFS_DIR2_DATA_FREE_TAG ||
994 be16_to_cpu(dup->length) < size)
998 * Start converting it to block form.
1000 hdr->magic = cpu_to_be32(XFS_DIR2_BLOCK_MAGIC);
1004 * Use up the space at the end of the block (blp/btp).
1006 xfs_dir2_data_use_free(tp, dbp, dup, mp->m_dirblksize - size, size,
1007 &needlog, &needscan);
1009 * Initialize the block tail.
1011 btp = xfs_dir2_block_tail_p(mp, hdr);
1012 btp->count = cpu_to_be32(be16_to_cpu(leaf->hdr.count) - be16_to_cpu(leaf->hdr.stale));
1014 xfs_dir2_block_log_tail(tp, dbp);
1016 * Initialize the block leaf area. We compact out stale entries.
1018 lep = xfs_dir2_block_leaf_p(btp);
1019 for (from = to = 0; from < be16_to_cpu(leaf->hdr.count); from++) {
1020 if (leaf->ents[from].address ==
1021 cpu_to_be32(XFS_DIR2_NULL_DATAPTR))
1023 lep[to++] = leaf->ents[from];
1025 ASSERT(to == be32_to_cpu(btp->count));
1026 xfs_dir2_block_log_leaf(tp, dbp, 0, be32_to_cpu(btp->count) - 1);
1028 * Scan the bestfree if we need it and log the data block header.
1031 xfs_dir2_data_freescan(mp, hdr, &needlog);
1033 xfs_dir2_data_log_header(tp, dbp);
1035 * Pitch the old leaf block.
1037 error = xfs_da_shrink_inode(args, mp->m_dirleafblk, lbp);
1042 * Now see if the resulting block can be shrunken to shortform.
1044 size = xfs_dir2_block_sfsize(dp, hdr, &sfh);
1045 if (size > XFS_IFORK_DSIZE(dp))
1048 return xfs_dir2_block_to_sf(args, dbp, size, &sfh);
1052 * Convert the shortform directory to block form.
1055 xfs_dir2_sf_to_block(
1056 xfs_da_args_t *args) /* operation arguments */
1058 xfs_dir2_db_t blkno; /* dir-relative block # (0) */
1059 xfs_dir2_data_hdr_t *hdr; /* block header */
1060 xfs_dir2_leaf_entry_t *blp; /* block leaf entries */
1061 struct xfs_buf *bp; /* block buffer */
1062 xfs_dir2_block_tail_t *btp; /* block tail pointer */
1063 xfs_dir2_data_entry_t *dep; /* data entry pointer */
1064 xfs_inode_t *dp; /* incore directory inode */
1065 int dummy; /* trash */
1066 xfs_dir2_data_unused_t *dup; /* unused entry pointer */
1067 int endoffset; /* end of data objects */
1068 int error; /* error return value */
1070 xfs_mount_t *mp; /* filesystem mount point */
1071 int needlog; /* need to log block header */
1072 int needscan; /* need to scan block freespc */
1073 int newoffset; /* offset from current entry */
1074 int offset; /* target block offset */
1075 xfs_dir2_sf_entry_t *sfep; /* sf entry pointer */
1076 xfs_dir2_sf_hdr_t *oldsfp; /* old shortform header */
1077 xfs_dir2_sf_hdr_t *sfp; /* shortform header */
1078 __be16 *tagp; /* end of data entry */
1079 xfs_trans_t *tp; /* transaction pointer */
1080 struct xfs_name name;
1082 trace_xfs_dir2_sf_to_block(args);
1087 ASSERT(dp->i_df.if_flags & XFS_IFINLINE);
1089 * Bomb out if the shortform directory is way too short.
1091 if (dp->i_d.di_size < offsetof(xfs_dir2_sf_hdr_t, parent)) {
1092 ASSERT(XFS_FORCED_SHUTDOWN(mp));
1093 return XFS_ERROR(EIO);
1096 oldsfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
1098 ASSERT(dp->i_df.if_bytes == dp->i_d.di_size);
1099 ASSERT(dp->i_df.if_u1.if_data != NULL);
1100 ASSERT(dp->i_d.di_size >= xfs_dir2_sf_hdr_size(oldsfp->i8count));
1103 * Copy the directory into a temporary buffer.
1104 * Then pitch the incore inode data so we can make extents.
1106 sfp = kmem_alloc(dp->i_df.if_bytes, KM_SLEEP);
1107 memcpy(sfp, oldsfp, dp->i_df.if_bytes);
1109 xfs_idata_realloc(dp, -dp->i_df.if_bytes, XFS_DATA_FORK);
1110 dp->i_d.di_size = 0;
1111 xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
1114 * Add block 0 to the inode.
1116 error = xfs_dir2_grow_inode(args, XFS_DIR2_DATA_SPACE, &blkno);
1122 * Initialize the data block.
1124 error = xfs_dir2_data_init(args, blkno, &bp);
1130 hdr->magic = cpu_to_be32(XFS_DIR2_BLOCK_MAGIC);
1132 * Compute size of block "tail" area.
1134 i = (uint)sizeof(*btp) +
1135 (sfp->count + 2) * (uint)sizeof(xfs_dir2_leaf_entry_t);
1137 * The whole thing is initialized to free by the init routine.
1138 * Say we're using the leaf and tail area.
1140 dup = (xfs_dir2_data_unused_t *)(hdr + 1);
1141 needlog = needscan = 0;
1142 xfs_dir2_data_use_free(tp, bp, dup, mp->m_dirblksize - i, i, &needlog,
1144 ASSERT(needscan == 0);
1148 btp = xfs_dir2_block_tail_p(mp, hdr);
1149 btp->count = cpu_to_be32(sfp->count + 2); /* ., .. */
1151 blp = xfs_dir2_block_leaf_p(btp);
1152 endoffset = (uint)((char *)blp - (char *)hdr);
1154 * Remove the freespace, we'll manage it.
1156 xfs_dir2_data_use_free(tp, bp, dup,
1157 (xfs_dir2_data_aoff_t)((char *)dup - (char *)hdr),
1158 be16_to_cpu(dup->length), &needlog, &needscan);
1160 * Create entry for .
1162 dep = (xfs_dir2_data_entry_t *)
1163 ((char *)hdr + XFS_DIR2_DATA_DOT_OFFSET);
1164 dep->inumber = cpu_to_be64(dp->i_ino);
1167 tagp = xfs_dir2_data_entry_tag_p(dep);
1168 *tagp = cpu_to_be16((char *)dep - (char *)hdr);
1169 xfs_dir2_data_log_entry(tp, bp, dep);
1170 blp[0].hashval = cpu_to_be32(xfs_dir_hash_dot);
1171 blp[0].address = cpu_to_be32(xfs_dir2_byte_to_dataptr(mp,
1172 (char *)dep - (char *)hdr));
1174 * Create entry for ..
1176 dep = (xfs_dir2_data_entry_t *)
1177 ((char *)hdr + XFS_DIR2_DATA_DOTDOT_OFFSET);
1178 dep->inumber = cpu_to_be64(xfs_dir2_sf_get_parent_ino(sfp));
1180 dep->name[0] = dep->name[1] = '.';
1181 tagp = xfs_dir2_data_entry_tag_p(dep);
1182 *tagp = cpu_to_be16((char *)dep - (char *)hdr);
1183 xfs_dir2_data_log_entry(tp, bp, dep);
1184 blp[1].hashval = cpu_to_be32(xfs_dir_hash_dotdot);
1185 blp[1].address = cpu_to_be32(xfs_dir2_byte_to_dataptr(mp,
1186 (char *)dep - (char *)hdr));
1187 offset = XFS_DIR2_DATA_FIRST_OFFSET;
1189 * Loop over existing entries, stuff them in.
1195 sfep = xfs_dir2_sf_firstentry(sfp);
1197 * Need to preserve the existing offset values in the sf directory.
1198 * Insert holes (unused entries) where necessary.
1200 while (offset < endoffset) {
1202 * sfep is null when we reach the end of the list.
1205 newoffset = endoffset;
1207 newoffset = xfs_dir2_sf_get_offset(sfep);
1209 * There should be a hole here, make one.
1211 if (offset < newoffset) {
1212 dup = (xfs_dir2_data_unused_t *)((char *)hdr + offset);
1213 dup->freetag = cpu_to_be16(XFS_DIR2_DATA_FREE_TAG);
1214 dup->length = cpu_to_be16(newoffset - offset);
1215 *xfs_dir2_data_unused_tag_p(dup) = cpu_to_be16(
1216 ((char *)dup - (char *)hdr));
1217 xfs_dir2_data_log_unused(tp, bp, dup);
1218 xfs_dir2_data_freeinsert(hdr, dup, &dummy);
1219 offset += be16_to_cpu(dup->length);
1223 * Copy a real entry.
1225 dep = (xfs_dir2_data_entry_t *)((char *)hdr + newoffset);
1226 dep->inumber = cpu_to_be64(xfs_dir2_sfe_get_ino(sfp, sfep));
1227 dep->namelen = sfep->namelen;
1228 memcpy(dep->name, sfep->name, dep->namelen);
1229 tagp = xfs_dir2_data_entry_tag_p(dep);
1230 *tagp = cpu_to_be16((char *)dep - (char *)hdr);
1231 xfs_dir2_data_log_entry(tp, bp, dep);
1232 name.name = sfep->name;
1233 name.len = sfep->namelen;
1234 blp[2 + i].hashval = cpu_to_be32(mp->m_dirnameops->
1236 blp[2 + i].address = cpu_to_be32(xfs_dir2_byte_to_dataptr(mp,
1237 (char *)dep - (char *)hdr));
1238 offset = (int)((char *)(tagp + 1) - (char *)hdr);
1239 if (++i == sfp->count)
1242 sfep = xfs_dir2_sf_nextentry(sfp, sfep);
1244 /* Done with the temporary buffer */
1247 * Sort the leaf entries by hash value.
1249 xfs_sort(blp, be32_to_cpu(btp->count), sizeof(*blp), xfs_dir2_block_sort);
1251 * Log the leaf entry area and tail.
1252 * Already logged the header in data_init, ignore needlog.
1254 ASSERT(needscan == 0);
1255 xfs_dir2_block_log_leaf(tp, bp, 0, be32_to_cpu(btp->count) - 1);
1256 xfs_dir2_block_log_tail(tp, bp);
1257 xfs_dir2_data_check(dp, bp);