2 * Copyright (c) 2000-2003,2005 Silicon Graphics, Inc.
3 * Copyright (c) 2013 Red Hat, Inc.
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as
8 * published by the Free Software Foundation.
10 * This program is distributed in the hope that it would be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21 #include "xfs_types.h"
23 #include "xfs_trans.h"
26 #include "xfs_mount.h"
27 #include "xfs_da_btree.h"
28 #include "xfs_bmap_btree.h"
29 #include "xfs_dinode.h"
30 #include "xfs_inode.h"
31 #include "xfs_inode_item.h"
32 #include "xfs_buf_item.h"
34 #include "xfs_dir2_format.h"
35 #include "xfs_dir2_priv.h"
36 #include "xfs_error.h"
37 #include "xfs_trace.h"
38 #include "xfs_cksum.h"
41 * Local function prototypes.
43 static void xfs_dir2_block_log_leaf(xfs_trans_t *tp, struct xfs_buf *bp,
45 static void xfs_dir2_block_log_tail(xfs_trans_t *tp, struct xfs_buf *bp);
46 static int xfs_dir2_block_lookup_int(xfs_da_args_t *args, struct xfs_buf **bpp,
48 static int xfs_dir2_block_sort(const void *a, const void *b);
50 static xfs_dahash_t xfs_dir_hash_dot, xfs_dir_hash_dotdot;
53 * One-time startup routine called from xfs_init().
58 xfs_dir_hash_dot = xfs_da_hashname((unsigned char *)".", 1);
59 xfs_dir_hash_dotdot = xfs_da_hashname((unsigned char *)"..", 2);
63 xfs_dir3_block_verify(
66 struct xfs_mount *mp = bp->b_target->bt_mount;
67 struct xfs_dir3_blk_hdr *hdr3 = bp->b_addr;
69 if (xfs_sb_version_hascrc(&mp->m_sb)) {
70 if (hdr3->magic != cpu_to_be32(XFS_DIR3_BLOCK_MAGIC))
72 if (!uuid_equal(&hdr3->uuid, &mp->m_sb.sb_uuid))
74 if (be64_to_cpu(hdr3->blkno) != bp->b_bn)
77 if (hdr3->magic != cpu_to_be32(XFS_DIR2_BLOCK_MAGIC))
80 if (__xfs_dir3_data_check(NULL, bp))
86 xfs_dir3_block_read_verify(
89 struct xfs_mount *mp = bp->b_target->bt_mount;
91 if ((xfs_sb_version_hascrc(&mp->m_sb) &&
92 !xfs_verify_cksum(bp->b_addr, BBTOB(bp->b_length),
93 XFS_DIR3_DATA_CRC_OFF)) ||
94 !xfs_dir3_block_verify(bp)) {
95 XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp, bp->b_addr);
96 xfs_buf_ioerror(bp, EFSCORRUPTED);
101 xfs_dir3_block_write_verify(
104 struct xfs_mount *mp = bp->b_target->bt_mount;
105 struct xfs_buf_log_item *bip = bp->b_fspriv;
106 struct xfs_dir3_blk_hdr *hdr3 = bp->b_addr;
108 if (!xfs_dir3_block_verify(bp)) {
109 XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp, bp->b_addr);
110 xfs_buf_ioerror(bp, EFSCORRUPTED);
114 if (!xfs_sb_version_hascrc(&mp->m_sb))
118 hdr3->lsn = cpu_to_be64(bip->bli_item.li_lsn);
120 xfs_update_cksum(bp->b_addr, BBTOB(bp->b_length), XFS_DIR3_DATA_CRC_OFF);
123 const struct xfs_buf_ops xfs_dir3_block_buf_ops = {
124 .verify_read = xfs_dir3_block_read_verify,
125 .verify_write = xfs_dir3_block_write_verify,
130 struct xfs_trans *tp,
131 struct xfs_inode *dp,
132 struct xfs_buf **bpp)
134 struct xfs_mount *mp = dp->i_mount;
137 err = xfs_da_read_buf(tp, dp, mp->m_dirdatablk, -1, bpp,
138 XFS_DATA_FORK, &xfs_dir3_block_buf_ops);
140 xfs_trans_buf_set_type(tp, *bpp, XFS_BLFT_DIR_BLOCK_BUF);
146 struct xfs_mount *mp,
147 struct xfs_trans *tp,
149 struct xfs_inode *dp)
151 struct xfs_dir3_blk_hdr *hdr3 = bp->b_addr;
153 bp->b_ops = &xfs_dir3_block_buf_ops;
154 xfs_trans_buf_set_type(tp, bp, XFS_BLFT_DIR_BLOCK_BUF);
156 if (xfs_sb_version_hascrc(&mp->m_sb)) {
157 memset(hdr3, 0, sizeof(*hdr3));
158 hdr3->magic = cpu_to_be32(XFS_DIR3_BLOCK_MAGIC);
159 hdr3->blkno = cpu_to_be64(bp->b_bn);
160 hdr3->owner = cpu_to_be64(dp->i_ino);
161 uuid_copy(&hdr3->uuid, &mp->m_sb.sb_uuid);
165 hdr3->magic = cpu_to_be32(XFS_DIR2_BLOCK_MAGIC);
169 xfs_dir2_block_need_space(
170 struct xfs_dir2_data_hdr *hdr,
171 struct xfs_dir2_block_tail *btp,
172 struct xfs_dir2_leaf_entry *blp,
174 struct xfs_dir2_data_unused **dupp,
175 struct xfs_dir2_data_unused **enddupp,
179 struct xfs_dir2_data_free *bf;
181 struct xfs_dir2_data_unused *dup = NULL;
182 struct xfs_dir2_data_unused *enddup = NULL;
185 bf = xfs_dir3_data_bestfree_p(hdr);
188 * If there are stale entries we'll use one for the leaf.
191 if (be16_to_cpu(bf[0].length) >= len) {
193 * The biggest entry enough to avoid compaction.
195 dup = (xfs_dir2_data_unused_t *)
196 ((char *)hdr + be16_to_cpu(bf[0].offset));
201 * Will need to compact to make this work.
202 * Tag just before the first leaf entry.
205 tagp = (__be16 *)blp - 1;
207 /* Data object just before the first leaf entry. */
208 dup = (xfs_dir2_data_unused_t *)((char *)hdr + be16_to_cpu(*tagp));
211 * If it's not free then the data will go where the
212 * leaf data starts now, if it works at all.
214 if (be16_to_cpu(dup->freetag) == XFS_DIR2_DATA_FREE_TAG) {
215 if (be16_to_cpu(dup->length) + (be32_to_cpu(btp->stale) - 1) *
216 (uint)sizeof(*blp) < len)
218 } else if ((be32_to_cpu(btp->stale) - 1) * (uint)sizeof(*blp) < len)
221 dup = (xfs_dir2_data_unused_t *)blp;
226 * no stale entries, so just use free space.
227 * Tag just before the first leaf entry.
229 tagp = (__be16 *)blp - 1;
231 /* Data object just before the first leaf entry. */
232 enddup = (xfs_dir2_data_unused_t *)((char *)hdr + be16_to_cpu(*tagp));
235 * If it's not free then can't do this add without cleaning up:
236 * the space before the first leaf entry needs to be free so it
237 * can be expanded to hold the pointer to the new entry.
239 if (be16_to_cpu(enddup->freetag) == XFS_DIR2_DATA_FREE_TAG) {
241 * Check out the biggest freespace and see if it's the same one.
243 dup = (xfs_dir2_data_unused_t *)
244 ((char *)hdr + be16_to_cpu(bf[0].offset));
247 * Not the same free entry, just check its length.
249 if (be16_to_cpu(dup->length) < len)
255 * It is the biggest freespace, can it hold the leaf too?
257 if (be16_to_cpu(dup->length) < len + (uint)sizeof(*blp)) {
259 * Yes, use the second-largest entry instead if it works.
261 if (be16_to_cpu(bf[1].length) >= len)
262 dup = (xfs_dir2_data_unused_t *)
263 ((char *)hdr + be16_to_cpu(bf[1].offset));
275 * compact the leaf entries.
276 * Leave the highest-numbered stale entry stale.
277 * XXX should be the one closest to mid but mid is not yet computed.
280 xfs_dir2_block_compact(
281 struct xfs_trans *tp,
283 struct xfs_dir2_data_hdr *hdr,
284 struct xfs_dir2_block_tail *btp,
285 struct xfs_dir2_leaf_entry *blp,
290 int fromidx; /* source leaf index */
291 int toidx; /* target leaf index */
293 int highstale; /* high stale index */
295 fromidx = toidx = be32_to_cpu(btp->count) - 1;
296 highstale = *lfloghigh = -1;
297 for (; fromidx >= 0; fromidx--) {
298 if (blp[fromidx].address == cpu_to_be32(XFS_DIR2_NULL_DATAPTR)) {
302 if (*lfloghigh == -1)
308 blp[toidx] = blp[fromidx];
311 *lfloglow = toidx + 1 - (be32_to_cpu(btp->stale) - 1);
312 *lfloghigh -= be32_to_cpu(btp->stale) - 1;
313 be32_add_cpu(&btp->count, -(be32_to_cpu(btp->stale) - 1));
314 xfs_dir2_data_make_free(tp, bp,
315 (xfs_dir2_data_aoff_t)((char *)blp - (char *)hdr),
316 (xfs_dir2_data_aoff_t)((be32_to_cpu(btp->stale) - 1) * sizeof(*blp)),
318 blp += be32_to_cpu(btp->stale) - 1;
319 btp->stale = cpu_to_be32(1);
321 * If we now need to rebuild the bestfree map, do so.
322 * This needs to happen before the next call to use_free.
325 xfs_dir2_data_freescan(tp->t_mountp, hdr, needlog);
329 * Add an entry to a block directory.
332 xfs_dir2_block_addname(
333 xfs_da_args_t *args) /* directory op arguments */
335 xfs_dir2_data_hdr_t *hdr; /* block header */
336 xfs_dir2_leaf_entry_t *blp; /* block leaf entries */
337 struct xfs_buf *bp; /* buffer for block */
338 xfs_dir2_block_tail_t *btp; /* block tail */
339 int compact; /* need to compact leaf ents */
340 xfs_dir2_data_entry_t *dep; /* block data entry */
341 xfs_inode_t *dp; /* directory inode */
342 xfs_dir2_data_unused_t *dup; /* block unused entry */
343 int error; /* error return value */
344 xfs_dir2_data_unused_t *enddup=NULL; /* unused at end of data */
345 xfs_dahash_t hash; /* hash value of found entry */
346 int high; /* high index for binary srch */
347 int highstale; /* high stale index */
348 int lfloghigh=0; /* last final leaf to log */
349 int lfloglow=0; /* first final leaf to log */
350 int len; /* length of the new entry */
351 int low; /* low index for binary srch */
352 int lowstale; /* low stale index */
353 int mid=0; /* midpoint for binary srch */
354 xfs_mount_t *mp; /* filesystem mount point */
355 int needlog; /* need to log header */
356 int needscan; /* need to rescan freespace */
357 __be16 *tagp; /* pointer to tag value */
358 xfs_trans_t *tp; /* transaction structure */
360 trace_xfs_dir2_block_addname(args);
366 /* Read the (one and only) directory block into bp. */
367 error = xfs_dir3_block_read(tp, dp, &bp);
371 len = xfs_dir2_data_entsize(args->namelen);
374 * Set up pointers to parts of the block.
377 btp = xfs_dir2_block_tail_p(mp, hdr);
378 blp = xfs_dir2_block_leaf_p(btp);
381 * Find out if we can reuse stale entries or whether we need extra
382 * space for entry and new leaf.
384 xfs_dir2_block_need_space(hdr, btp, blp, &tagp, &dup,
385 &enddup, &compact, len);
388 * Done everything we need for a space check now.
390 if (args->op_flags & XFS_DA_OP_JUSTCHECK) {
391 xfs_trans_brelse(tp, bp);
393 return XFS_ERROR(ENOSPC);
398 * If we don't have space for the new entry & leaf ...
401 /* Don't have a space reservation: return no-space. */
402 if (args->total == 0)
403 return XFS_ERROR(ENOSPC);
405 * Convert to the next larger format.
406 * Then add the new entry in that format.
408 error = xfs_dir2_block_to_leaf(args, bp);
411 return xfs_dir2_leaf_addname(args);
414 needlog = needscan = 0;
417 * If need to compact the leaf entries, do it now.
420 xfs_dir2_block_compact(tp, bp, hdr, btp, blp, &needlog,
421 &lfloghigh, &lfloglow);
422 /* recalculate blp post-compaction */
423 blp = xfs_dir2_block_leaf_p(btp);
424 } else if (btp->stale) {
426 * Set leaf logging boundaries to impossible state.
427 * For the no-stale case they're set explicitly.
429 lfloglow = be32_to_cpu(btp->count);
434 * Find the slot that's first lower than our hash value, -1 if none.
436 for (low = 0, high = be32_to_cpu(btp->count) - 1; low <= high; ) {
437 mid = (low + high) >> 1;
438 if ((hash = be32_to_cpu(blp[mid].hashval)) == args->hashval)
440 if (hash < args->hashval)
445 while (mid >= 0 && be32_to_cpu(blp[mid].hashval) >= args->hashval) {
449 * No stale entries, will use enddup space to hold new leaf.
453 * Mark the space needed for the new leaf entry, now in use.
455 xfs_dir2_data_use_free(tp, bp, enddup,
456 (xfs_dir2_data_aoff_t)
457 ((char *)enddup - (char *)hdr + be16_to_cpu(enddup->length) -
459 (xfs_dir2_data_aoff_t)sizeof(*blp),
460 &needlog, &needscan);
462 * Update the tail (entry count).
464 be32_add_cpu(&btp->count, 1);
466 * If we now need to rebuild the bestfree map, do so.
467 * This needs to happen before the next call to use_free.
470 xfs_dir2_data_freescan(mp, hdr, &needlog);
474 * Adjust pointer to the first leaf entry, we're about to move
475 * the table up one to open up space for the new leaf entry.
476 * Then adjust our index to match.
481 memmove(blp, &blp[1], mid * sizeof(*blp));
486 * Use a stale leaf for our new entry.
491 blp[lowstale].address !=
492 cpu_to_be32(XFS_DIR2_NULL_DATAPTR);
495 for (highstale = mid + 1;
496 highstale < be32_to_cpu(btp->count) &&
497 blp[highstale].address !=
498 cpu_to_be32(XFS_DIR2_NULL_DATAPTR) &&
499 (lowstale < 0 || mid - lowstale > highstale - mid);
503 * Move entries toward the low-numbered stale entry.
506 (highstale == be32_to_cpu(btp->count) ||
507 mid - lowstale <= highstale - mid)) {
509 memmove(&blp[lowstale], &blp[lowstale + 1],
510 (mid - lowstale) * sizeof(*blp));
511 lfloglow = MIN(lowstale, lfloglow);
512 lfloghigh = MAX(mid, lfloghigh);
515 * Move entries toward the high-numbered stale entry.
518 ASSERT(highstale < be32_to_cpu(btp->count));
521 memmove(&blp[mid + 1], &blp[mid],
522 (highstale - mid) * sizeof(*blp));
523 lfloglow = MIN(mid, lfloglow);
524 lfloghigh = MAX(highstale, lfloghigh);
526 be32_add_cpu(&btp->stale, -1);
529 * Point to the new data entry.
531 dep = (xfs_dir2_data_entry_t *)dup;
533 * Fill in the leaf entry.
535 blp[mid].hashval = cpu_to_be32(args->hashval);
536 blp[mid].address = cpu_to_be32(xfs_dir2_byte_to_dataptr(mp,
537 (char *)dep - (char *)hdr));
538 xfs_dir2_block_log_leaf(tp, bp, lfloglow, lfloghigh);
540 * Mark space for the data entry used.
542 xfs_dir2_data_use_free(tp, bp, dup,
543 (xfs_dir2_data_aoff_t)((char *)dup - (char *)hdr),
544 (xfs_dir2_data_aoff_t)len, &needlog, &needscan);
546 * Create the new data entry.
548 dep->inumber = cpu_to_be64(args->inumber);
549 dep->namelen = args->namelen;
550 memcpy(dep->name, args->name, args->namelen);
551 tagp = xfs_dir2_data_entry_tag_p(dep);
552 *tagp = cpu_to_be16((char *)dep - (char *)hdr);
554 * Clean up the bestfree array and log the header, tail, and entry.
557 xfs_dir2_data_freescan(mp, hdr, &needlog);
559 xfs_dir2_data_log_header(tp, bp);
560 xfs_dir2_block_log_tail(tp, bp);
561 xfs_dir2_data_log_entry(tp, bp, dep);
562 xfs_dir3_data_check(dp, bp);
567 * Readdir for block directories.
570 xfs_dir2_block_getdents(
571 xfs_inode_t *dp, /* incore inode */
572 struct dir_context *ctx)
574 xfs_dir2_data_hdr_t *hdr; /* block header */
575 struct xfs_buf *bp; /* buffer for block */
576 xfs_dir2_block_tail_t *btp; /* block tail */
577 xfs_dir2_data_entry_t *dep; /* block data entry */
578 xfs_dir2_data_unused_t *dup; /* block unused entry */
579 char *endptr; /* end of the data entries */
580 int error; /* error return value */
581 xfs_mount_t *mp; /* filesystem mount point */
582 char *ptr; /* current data entry */
583 int wantoff; /* starting block offset */
588 * If the block number in the offset is out of range, we're done.
590 if (xfs_dir2_dataptr_to_db(mp, ctx->pos) > mp->m_dirdatablk)
593 error = xfs_dir3_block_read(NULL, dp, &bp);
598 * Extract the byte offset we start at from the seek pointer.
599 * We'll skip entries before this.
601 wantoff = xfs_dir2_dataptr_to_off(mp, ctx->pos);
603 xfs_dir3_data_check(dp, bp);
605 * Set up values for the loop.
607 btp = xfs_dir2_block_tail_p(mp, hdr);
608 ptr = (char *)xfs_dir3_data_entry_p(hdr);
609 endptr = (char *)xfs_dir2_block_leaf_p(btp);
612 * Loop over the data portion of the block.
613 * Each object is a real entry (dep) or an unused one (dup).
615 while (ptr < endptr) {
616 dup = (xfs_dir2_data_unused_t *)ptr;
620 if (be16_to_cpu(dup->freetag) == XFS_DIR2_DATA_FREE_TAG) {
621 ptr += be16_to_cpu(dup->length);
625 dep = (xfs_dir2_data_entry_t *)ptr;
628 * Bump pointer for the next iteration.
630 ptr += xfs_dir2_data_entsize(dep->namelen);
632 * The entry is before the desired starting point, skip it.
634 if ((char *)dep - (char *)hdr < wantoff)
637 cook = xfs_dir2_db_off_to_dataptr(mp, mp->m_dirdatablk,
638 (char *)dep - (char *)hdr);
640 ctx->pos = cook & 0x7fffffff;
642 * If it didn't fit, set the final offset to here & return.
644 if (!dir_emit(ctx, (char *)dep->name, dep->namelen,
645 be64_to_cpu(dep->inumber), DT_UNKNOWN)) {
646 xfs_trans_brelse(NULL, bp);
652 * Reached the end of the block.
653 * Set the offset to a non-existent block 1 and return.
655 ctx->pos = xfs_dir2_db_off_to_dataptr(mp, mp->m_dirdatablk + 1, 0) &
657 xfs_trans_brelse(NULL, bp);
662 * Log leaf entries from the block.
665 xfs_dir2_block_log_leaf(
666 xfs_trans_t *tp, /* transaction structure */
667 struct xfs_buf *bp, /* block buffer */
668 int first, /* index of first logged leaf */
669 int last) /* index of last logged leaf */
671 xfs_dir2_data_hdr_t *hdr = bp->b_addr;
672 xfs_dir2_leaf_entry_t *blp;
673 xfs_dir2_block_tail_t *btp;
675 btp = xfs_dir2_block_tail_p(tp->t_mountp, hdr);
676 blp = xfs_dir2_block_leaf_p(btp);
677 xfs_trans_log_buf(tp, bp, (uint)((char *)&blp[first] - (char *)hdr),
678 (uint)((char *)&blp[last + 1] - (char *)hdr - 1));
682 * Log the block tail.
685 xfs_dir2_block_log_tail(
686 xfs_trans_t *tp, /* transaction structure */
687 struct xfs_buf *bp) /* block buffer */
689 xfs_dir2_data_hdr_t *hdr = bp->b_addr;
690 xfs_dir2_block_tail_t *btp;
692 btp = xfs_dir2_block_tail_p(tp->t_mountp, hdr);
693 xfs_trans_log_buf(tp, bp, (uint)((char *)btp - (char *)hdr),
694 (uint)((char *)(btp + 1) - (char *)hdr - 1));
698 * Look up an entry in the block. This is the external routine,
699 * xfs_dir2_block_lookup_int does the real work.
702 xfs_dir2_block_lookup(
703 xfs_da_args_t *args) /* dir lookup arguments */
705 xfs_dir2_data_hdr_t *hdr; /* block header */
706 xfs_dir2_leaf_entry_t *blp; /* block leaf entries */
707 struct xfs_buf *bp; /* block buffer */
708 xfs_dir2_block_tail_t *btp; /* block tail */
709 xfs_dir2_data_entry_t *dep; /* block data entry */
710 xfs_inode_t *dp; /* incore inode */
711 int ent; /* entry index */
712 int error; /* error return value */
713 xfs_mount_t *mp; /* filesystem mount point */
715 trace_xfs_dir2_block_lookup(args);
718 * Get the buffer, look up the entry.
719 * If not found (ENOENT) then return, have no buffer.
721 if ((error = xfs_dir2_block_lookup_int(args, &bp, &ent)))
726 xfs_dir3_data_check(dp, bp);
727 btp = xfs_dir2_block_tail_p(mp, hdr);
728 blp = xfs_dir2_block_leaf_p(btp);
730 * Get the offset from the leaf entry, to point to the data.
732 dep = (xfs_dir2_data_entry_t *)((char *)hdr +
733 xfs_dir2_dataptr_to_off(mp, be32_to_cpu(blp[ent].address)));
735 * Fill in inode number, CI name if appropriate, release the block.
737 args->inumber = be64_to_cpu(dep->inumber);
738 error = xfs_dir_cilookup_result(args, dep->name, dep->namelen);
739 xfs_trans_brelse(args->trans, bp);
740 return XFS_ERROR(error);
744 * Internal block lookup routine.
746 static int /* error */
747 xfs_dir2_block_lookup_int(
748 xfs_da_args_t *args, /* dir lookup arguments */
749 struct xfs_buf **bpp, /* returned block buffer */
750 int *entno) /* returned entry number */
752 xfs_dir2_dataptr_t addr; /* data entry address */
753 xfs_dir2_data_hdr_t *hdr; /* block header */
754 xfs_dir2_leaf_entry_t *blp; /* block leaf entries */
755 struct xfs_buf *bp; /* block buffer */
756 xfs_dir2_block_tail_t *btp; /* block tail */
757 xfs_dir2_data_entry_t *dep; /* block data entry */
758 xfs_inode_t *dp; /* incore inode */
759 int error; /* error return value */
760 xfs_dahash_t hash; /* found hash value */
761 int high; /* binary search high index */
762 int low; /* binary search low index */
763 int mid; /* binary search current idx */
764 xfs_mount_t *mp; /* filesystem mount point */
765 xfs_trans_t *tp; /* transaction pointer */
766 enum xfs_dacmp cmp; /* comparison result */
772 error = xfs_dir3_block_read(tp, dp, &bp);
777 xfs_dir3_data_check(dp, bp);
778 btp = xfs_dir2_block_tail_p(mp, hdr);
779 blp = xfs_dir2_block_leaf_p(btp);
781 * Loop doing a binary search for our hash value.
782 * Find our entry, ENOENT if it's not there.
784 for (low = 0, high = be32_to_cpu(btp->count) - 1; ; ) {
786 mid = (low + high) >> 1;
787 if ((hash = be32_to_cpu(blp[mid].hashval)) == args->hashval)
789 if (hash < args->hashval)
794 ASSERT(args->op_flags & XFS_DA_OP_OKNOENT);
795 xfs_trans_brelse(tp, bp);
796 return XFS_ERROR(ENOENT);
800 * Back up to the first one with the right hash value.
802 while (mid > 0 && be32_to_cpu(blp[mid - 1].hashval) == args->hashval) {
806 * Now loop forward through all the entries with the
807 * right hash value looking for our name.
810 if ((addr = be32_to_cpu(blp[mid].address)) == XFS_DIR2_NULL_DATAPTR)
813 * Get pointer to the entry from the leaf.
815 dep = (xfs_dir2_data_entry_t *)
816 ((char *)hdr + xfs_dir2_dataptr_to_off(mp, addr));
818 * Compare name and if it's an exact match, return the index
819 * and buffer. If it's the first case-insensitive match, store
820 * the index and buffer and continue looking for an exact match.
822 cmp = mp->m_dirnameops->compname(args, dep->name, dep->namelen);
823 if (cmp != XFS_CMP_DIFFERENT && cmp != args->cmpresult) {
824 args->cmpresult = cmp;
827 if (cmp == XFS_CMP_EXACT)
830 } while (++mid < be32_to_cpu(btp->count) &&
831 be32_to_cpu(blp[mid].hashval) == hash);
833 ASSERT(args->op_flags & XFS_DA_OP_OKNOENT);
835 * Here, we can only be doing a lookup (not a rename or replace).
836 * If a case-insensitive match was found earlier, return success.
838 if (args->cmpresult == XFS_CMP_CASE)
841 * No match, release the buffer and return ENOENT.
843 xfs_trans_brelse(tp, bp);
844 return XFS_ERROR(ENOENT);
848 * Remove an entry from a block format directory.
849 * If that makes the block small enough to fit in shortform, transform it.
852 xfs_dir2_block_removename(
853 xfs_da_args_t *args) /* directory operation args */
855 xfs_dir2_data_hdr_t *hdr; /* block header */
856 xfs_dir2_leaf_entry_t *blp; /* block leaf pointer */
857 struct xfs_buf *bp; /* block buffer */
858 xfs_dir2_block_tail_t *btp; /* block tail */
859 xfs_dir2_data_entry_t *dep; /* block data entry */
860 xfs_inode_t *dp; /* incore inode */
861 int ent; /* block leaf entry index */
862 int error; /* error return value */
863 xfs_mount_t *mp; /* filesystem mount point */
864 int needlog; /* need to log block header */
865 int needscan; /* need to fixup bestfree */
866 xfs_dir2_sf_hdr_t sfh; /* shortform header */
867 int size; /* shortform size */
868 xfs_trans_t *tp; /* transaction pointer */
870 trace_xfs_dir2_block_removename(args);
873 * Look up the entry in the block. Gets the buffer and entry index.
874 * It will always be there, the vnodeops level does a lookup first.
876 if ((error = xfs_dir2_block_lookup_int(args, &bp, &ent))) {
883 btp = xfs_dir2_block_tail_p(mp, hdr);
884 blp = xfs_dir2_block_leaf_p(btp);
886 * Point to the data entry using the leaf entry.
888 dep = (xfs_dir2_data_entry_t *)
889 ((char *)hdr + xfs_dir2_dataptr_to_off(mp, be32_to_cpu(blp[ent].address)));
891 * Mark the data entry's space free.
893 needlog = needscan = 0;
894 xfs_dir2_data_make_free(tp, bp,
895 (xfs_dir2_data_aoff_t)((char *)dep - (char *)hdr),
896 xfs_dir2_data_entsize(dep->namelen), &needlog, &needscan);
898 * Fix up the block tail.
900 be32_add_cpu(&btp->stale, 1);
901 xfs_dir2_block_log_tail(tp, bp);
903 * Remove the leaf entry by marking it stale.
905 blp[ent].address = cpu_to_be32(XFS_DIR2_NULL_DATAPTR);
906 xfs_dir2_block_log_leaf(tp, bp, ent, ent);
908 * Fix up bestfree, log the header if necessary.
911 xfs_dir2_data_freescan(mp, hdr, &needlog);
913 xfs_dir2_data_log_header(tp, bp);
914 xfs_dir3_data_check(dp, bp);
916 * See if the size as a shortform is good enough.
918 size = xfs_dir2_block_sfsize(dp, hdr, &sfh);
919 if (size > XFS_IFORK_DSIZE(dp))
923 * If it works, do the conversion.
925 return xfs_dir2_block_to_sf(args, bp, size, &sfh);
929 * Replace an entry in a V2 block directory.
930 * Change the inode number to the new value.
933 xfs_dir2_block_replace(
934 xfs_da_args_t *args) /* directory operation args */
936 xfs_dir2_data_hdr_t *hdr; /* block header */
937 xfs_dir2_leaf_entry_t *blp; /* block leaf entries */
938 struct xfs_buf *bp; /* block buffer */
939 xfs_dir2_block_tail_t *btp; /* block tail */
940 xfs_dir2_data_entry_t *dep; /* block data entry */
941 xfs_inode_t *dp; /* incore inode */
942 int ent; /* leaf entry index */
943 int error; /* error return value */
944 xfs_mount_t *mp; /* filesystem mount point */
946 trace_xfs_dir2_block_replace(args);
949 * Lookup the entry in the directory. Get buffer and entry index.
950 * This will always succeed since the caller has already done a lookup.
952 if ((error = xfs_dir2_block_lookup_int(args, &bp, &ent))) {
958 btp = xfs_dir2_block_tail_p(mp, hdr);
959 blp = xfs_dir2_block_leaf_p(btp);
961 * Point to the data entry we need to change.
963 dep = (xfs_dir2_data_entry_t *)
964 ((char *)hdr + xfs_dir2_dataptr_to_off(mp, be32_to_cpu(blp[ent].address)));
965 ASSERT(be64_to_cpu(dep->inumber) != args->inumber);
967 * Change the inode number to the new value.
969 dep->inumber = cpu_to_be64(args->inumber);
970 xfs_dir2_data_log_entry(args->trans, bp, dep);
971 xfs_dir3_data_check(dp, bp);
976 * Qsort comparison routine for the block leaf entries.
978 static int /* sort order */
980 const void *a, /* first leaf entry */
981 const void *b) /* second leaf entry */
983 const xfs_dir2_leaf_entry_t *la; /* first leaf entry */
984 const xfs_dir2_leaf_entry_t *lb; /* second leaf entry */
988 return be32_to_cpu(la->hashval) < be32_to_cpu(lb->hashval) ? -1 :
989 (be32_to_cpu(la->hashval) > be32_to_cpu(lb->hashval) ? 1 : 0);
993 * Convert a V2 leaf directory to a V2 block directory if possible.
996 xfs_dir2_leaf_to_block(
997 xfs_da_args_t *args, /* operation arguments */
998 struct xfs_buf *lbp, /* leaf buffer */
999 struct xfs_buf *dbp) /* data buffer */
1001 __be16 *bestsp; /* leaf bests table */
1002 xfs_dir2_data_hdr_t *hdr; /* block header */
1003 xfs_dir2_block_tail_t *btp; /* block tail */
1004 xfs_inode_t *dp; /* incore directory inode */
1005 xfs_dir2_data_unused_t *dup; /* unused data entry */
1006 int error; /* error return value */
1007 int from; /* leaf from index */
1008 xfs_dir2_leaf_t *leaf; /* leaf structure */
1009 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
1010 xfs_dir2_leaf_tail_t *ltp; /* leaf tail structure */
1011 xfs_mount_t *mp; /* file system mount point */
1012 int needlog; /* need to log data header */
1013 int needscan; /* need to scan for bestfree */
1014 xfs_dir2_sf_hdr_t sfh; /* shortform header */
1015 int size; /* bytes used */
1016 __be16 *tagp; /* end of entry (tag) */
1017 int to; /* block/leaf to index */
1018 xfs_trans_t *tp; /* transaction pointer */
1019 struct xfs_dir2_leaf_entry *ents;
1020 struct xfs_dir3_icleaf_hdr leafhdr;
1022 trace_xfs_dir2_leaf_to_block(args);
1028 xfs_dir3_leaf_hdr_from_disk(&leafhdr, leaf);
1029 ents = xfs_dir3_leaf_ents_p(leaf);
1030 ltp = xfs_dir2_leaf_tail_p(mp, leaf);
1032 ASSERT(leafhdr.magic == XFS_DIR2_LEAF1_MAGIC ||
1033 leafhdr.magic == XFS_DIR3_LEAF1_MAGIC);
1035 * If there are data blocks other than the first one, take this
1036 * opportunity to remove trailing empty data blocks that may have
1037 * been left behind during no-space-reservation operations.
1038 * These will show up in the leaf bests table.
1040 while (dp->i_d.di_size > mp->m_dirblksize) {
1043 hdrsz = xfs_dir3_data_hdr_size(xfs_sb_version_hascrc(&mp->m_sb));
1044 bestsp = xfs_dir2_leaf_bests_p(ltp);
1045 if (be16_to_cpu(bestsp[be32_to_cpu(ltp->bestcount) - 1]) ==
1046 mp->m_dirblksize - hdrsz) {
1048 xfs_dir2_leaf_trim_data(args, lbp,
1049 (xfs_dir2_db_t)(be32_to_cpu(ltp->bestcount) - 1))))
1055 * Read the data block if we don't already have it, give up if it fails.
1058 error = xfs_dir3_data_read(tp, dp, mp->m_dirdatablk, -1, &dbp);
1063 ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
1064 hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC));
1067 * Size of the "leaf" area in the block.
1069 size = (uint)sizeof(xfs_dir2_block_tail_t) +
1070 (uint)sizeof(*lep) * (leafhdr.count - leafhdr.stale);
1072 * Look at the last data entry.
1074 tagp = (__be16 *)((char *)hdr + mp->m_dirblksize) - 1;
1075 dup = (xfs_dir2_data_unused_t *)((char *)hdr + be16_to_cpu(*tagp));
1077 * If it's not free or is too short we can't do it.
1079 if (be16_to_cpu(dup->freetag) != XFS_DIR2_DATA_FREE_TAG ||
1080 be16_to_cpu(dup->length) < size)
1084 * Start converting it to block form.
1086 xfs_dir3_block_init(mp, tp, dbp, dp);
1091 * Use up the space at the end of the block (blp/btp).
1093 xfs_dir2_data_use_free(tp, dbp, dup, mp->m_dirblksize - size, size,
1094 &needlog, &needscan);
1096 * Initialize the block tail.
1098 btp = xfs_dir2_block_tail_p(mp, hdr);
1099 btp->count = cpu_to_be32(leafhdr.count - leafhdr.stale);
1101 xfs_dir2_block_log_tail(tp, dbp);
1103 * Initialize the block leaf area. We compact out stale entries.
1105 lep = xfs_dir2_block_leaf_p(btp);
1106 for (from = to = 0; from < leafhdr.count; from++) {
1107 if (ents[from].address == cpu_to_be32(XFS_DIR2_NULL_DATAPTR))
1109 lep[to++] = ents[from];
1111 ASSERT(to == be32_to_cpu(btp->count));
1112 xfs_dir2_block_log_leaf(tp, dbp, 0, be32_to_cpu(btp->count) - 1);
1114 * Scan the bestfree if we need it and log the data block header.
1117 xfs_dir2_data_freescan(mp, hdr, &needlog);
1119 xfs_dir2_data_log_header(tp, dbp);
1121 * Pitch the old leaf block.
1123 error = xfs_da_shrink_inode(args, mp->m_dirleafblk, lbp);
1128 * Now see if the resulting block can be shrunken to shortform.
1130 size = xfs_dir2_block_sfsize(dp, hdr, &sfh);
1131 if (size > XFS_IFORK_DSIZE(dp))
1134 return xfs_dir2_block_to_sf(args, dbp, size, &sfh);
1138 * Convert the shortform directory to block form.
1141 xfs_dir2_sf_to_block(
1142 xfs_da_args_t *args) /* operation arguments */
1144 xfs_dir2_db_t blkno; /* dir-relative block # (0) */
1145 xfs_dir2_data_hdr_t *hdr; /* block header */
1146 xfs_dir2_leaf_entry_t *blp; /* block leaf entries */
1147 struct xfs_buf *bp; /* block buffer */
1148 xfs_dir2_block_tail_t *btp; /* block tail pointer */
1149 xfs_dir2_data_entry_t *dep; /* data entry pointer */
1150 xfs_inode_t *dp; /* incore directory inode */
1151 int dummy; /* trash */
1152 xfs_dir2_data_unused_t *dup; /* unused entry pointer */
1153 int endoffset; /* end of data objects */
1154 int error; /* error return value */
1156 xfs_mount_t *mp; /* filesystem mount point */
1157 int needlog; /* need to log block header */
1158 int needscan; /* need to scan block freespc */
1159 int newoffset; /* offset from current entry */
1160 int offset; /* target block offset */
1161 xfs_dir2_sf_entry_t *sfep; /* sf entry pointer */
1162 xfs_dir2_sf_hdr_t *oldsfp; /* old shortform header */
1163 xfs_dir2_sf_hdr_t *sfp; /* shortform header */
1164 __be16 *tagp; /* end of data entry */
1165 xfs_trans_t *tp; /* transaction pointer */
1166 struct xfs_name name;
1168 trace_xfs_dir2_sf_to_block(args);
1173 ASSERT(dp->i_df.if_flags & XFS_IFINLINE);
1175 * Bomb out if the shortform directory is way too short.
1177 if (dp->i_d.di_size < offsetof(xfs_dir2_sf_hdr_t, parent)) {
1178 ASSERT(XFS_FORCED_SHUTDOWN(mp));
1179 return XFS_ERROR(EIO);
1182 oldsfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
1184 ASSERT(dp->i_df.if_bytes == dp->i_d.di_size);
1185 ASSERT(dp->i_df.if_u1.if_data != NULL);
1186 ASSERT(dp->i_d.di_size >= xfs_dir2_sf_hdr_size(oldsfp->i8count));
1189 * Copy the directory into a temporary buffer.
1190 * Then pitch the incore inode data so we can make extents.
1192 sfp = kmem_alloc(dp->i_df.if_bytes, KM_SLEEP);
1193 memcpy(sfp, oldsfp, dp->i_df.if_bytes);
1195 xfs_idata_realloc(dp, -dp->i_df.if_bytes, XFS_DATA_FORK);
1196 dp->i_d.di_size = 0;
1197 xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
1200 * Add block 0 to the inode.
1202 error = xfs_dir2_grow_inode(args, XFS_DIR2_DATA_SPACE, &blkno);
1208 * Initialize the data block, then convert it to block format.
1210 error = xfs_dir3_data_init(args, blkno, &bp);
1215 xfs_dir3_block_init(mp, tp, bp, dp);
1219 * Compute size of block "tail" area.
1221 i = (uint)sizeof(*btp) +
1222 (sfp->count + 2) * (uint)sizeof(xfs_dir2_leaf_entry_t);
1224 * The whole thing is initialized to free by the init routine.
1225 * Say we're using the leaf and tail area.
1227 dup = xfs_dir3_data_unused_p(hdr);
1228 needlog = needscan = 0;
1229 xfs_dir2_data_use_free(tp, bp, dup, mp->m_dirblksize - i, i, &needlog,
1231 ASSERT(needscan == 0);
1235 btp = xfs_dir2_block_tail_p(mp, hdr);
1236 btp->count = cpu_to_be32(sfp->count + 2); /* ., .. */
1238 blp = xfs_dir2_block_leaf_p(btp);
1239 endoffset = (uint)((char *)blp - (char *)hdr);
1241 * Remove the freespace, we'll manage it.
1243 xfs_dir2_data_use_free(tp, bp, dup,
1244 (xfs_dir2_data_aoff_t)((char *)dup - (char *)hdr),
1245 be16_to_cpu(dup->length), &needlog, &needscan);
1247 * Create entry for .
1249 dep = xfs_dir3_data_dot_entry_p(hdr);
1250 dep->inumber = cpu_to_be64(dp->i_ino);
1253 tagp = xfs_dir2_data_entry_tag_p(dep);
1254 *tagp = cpu_to_be16((char *)dep - (char *)hdr);
1255 xfs_dir2_data_log_entry(tp, bp, dep);
1256 blp[0].hashval = cpu_to_be32(xfs_dir_hash_dot);
1257 blp[0].address = cpu_to_be32(xfs_dir2_byte_to_dataptr(mp,
1258 (char *)dep - (char *)hdr));
1260 * Create entry for ..
1262 dep = xfs_dir3_data_dotdot_entry_p(hdr);
1263 dep->inumber = cpu_to_be64(xfs_dir2_sf_get_parent_ino(sfp));
1265 dep->name[0] = dep->name[1] = '.';
1266 tagp = xfs_dir2_data_entry_tag_p(dep);
1267 *tagp = cpu_to_be16((char *)dep - (char *)hdr);
1268 xfs_dir2_data_log_entry(tp, bp, dep);
1269 blp[1].hashval = cpu_to_be32(xfs_dir_hash_dotdot);
1270 blp[1].address = cpu_to_be32(xfs_dir2_byte_to_dataptr(mp,
1271 (char *)dep - (char *)hdr));
1272 offset = xfs_dir3_data_first_offset(hdr);
1274 * Loop over existing entries, stuff them in.
1280 sfep = xfs_dir2_sf_firstentry(sfp);
1282 * Need to preserve the existing offset values in the sf directory.
1283 * Insert holes (unused entries) where necessary.
1285 while (offset < endoffset) {
1287 * sfep is null when we reach the end of the list.
1290 newoffset = endoffset;
1292 newoffset = xfs_dir2_sf_get_offset(sfep);
1294 * There should be a hole here, make one.
1296 if (offset < newoffset) {
1297 dup = (xfs_dir2_data_unused_t *)((char *)hdr + offset);
1298 dup->freetag = cpu_to_be16(XFS_DIR2_DATA_FREE_TAG);
1299 dup->length = cpu_to_be16(newoffset - offset);
1300 *xfs_dir2_data_unused_tag_p(dup) = cpu_to_be16(
1301 ((char *)dup - (char *)hdr));
1302 xfs_dir2_data_log_unused(tp, bp, dup);
1303 xfs_dir2_data_freeinsert(hdr, dup, &dummy);
1304 offset += be16_to_cpu(dup->length);
1308 * Copy a real entry.
1310 dep = (xfs_dir2_data_entry_t *)((char *)hdr + newoffset);
1311 dep->inumber = cpu_to_be64(xfs_dir2_sfe_get_ino(sfp, sfep));
1312 dep->namelen = sfep->namelen;
1313 memcpy(dep->name, sfep->name, dep->namelen);
1314 tagp = xfs_dir2_data_entry_tag_p(dep);
1315 *tagp = cpu_to_be16((char *)dep - (char *)hdr);
1316 xfs_dir2_data_log_entry(tp, bp, dep);
1317 name.name = sfep->name;
1318 name.len = sfep->namelen;
1319 blp[2 + i].hashval = cpu_to_be32(mp->m_dirnameops->
1321 blp[2 + i].address = cpu_to_be32(xfs_dir2_byte_to_dataptr(mp,
1322 (char *)dep - (char *)hdr));
1323 offset = (int)((char *)(tagp + 1) - (char *)hdr);
1324 if (++i == sfp->count)
1327 sfep = xfs_dir2_sf_nextentry(sfp, sfep);
1329 /* Done with the temporary buffer */
1332 * Sort the leaf entries by hash value.
1334 xfs_sort(blp, be32_to_cpu(btp->count), sizeof(*blp), xfs_dir2_block_sort);
1336 * Log the leaf entry area and tail.
1337 * Already logged the header in data_init, ignore needlog.
1339 ASSERT(needscan == 0);
1340 xfs_dir2_block_log_leaf(tp, bp, 0, be32_to_cpu(btp->count) - 1);
1341 xfs_dir2_block_log_tail(tp, bp);
1342 xfs_dir3_data_check(dp, bp);