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_mount.h"
28 #include "xfs_da_btree.h"
29 #include "xfs_bmap_btree.h"
30 #include "xfs_dir2_sf.h"
31 #include "xfs_dinode.h"
32 #include "xfs_inode.h"
33 #include "xfs_inode_item.h"
34 #include "xfs_dir2_data.h"
35 #include "xfs_dir2_leaf.h"
36 #include "xfs_dir2_block.h"
37 #include "xfs_error.h"
38 #include "xfs_trace.h"
41 * Local function prototypes.
43 static void xfs_dir2_block_log_leaf(xfs_trans_t *tp, xfs_dabuf_t *bp, int first,
45 static void xfs_dir2_block_log_tail(xfs_trans_t *tp, xfs_dabuf_t *bp);
46 static int xfs_dir2_block_lookup_int(xfs_da_args_t *args, xfs_dabuf_t **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 * Add an entry to a block directory.
66 xfs_dir2_block_addname(
67 xfs_da_args_t *args) /* directory op arguments */
69 xfs_dir2_data_free_t *bf; /* bestfree table in block */
70 xfs_dir2_data_hdr_t *hdr; /* block header */
71 xfs_dir2_leaf_entry_t *blp; /* block leaf entries */
72 xfs_dabuf_t *bp; /* buffer for block */
73 xfs_dir2_block_tail_t *btp; /* block tail */
74 int compact; /* need to compact leaf ents */
75 xfs_dir2_data_entry_t *dep; /* block data entry */
76 xfs_inode_t *dp; /* directory inode */
77 xfs_dir2_data_unused_t *dup; /* block unused entry */
78 int error; /* error return value */
79 xfs_dir2_data_unused_t *enddup=NULL; /* unused at end of data */
80 xfs_dahash_t hash; /* hash value of found entry */
81 int high; /* high index for binary srch */
82 int highstale; /* high stale index */
83 int lfloghigh=0; /* last final leaf to log */
84 int lfloglow=0; /* first final leaf to log */
85 int len; /* length of the new entry */
86 int low; /* low index for binary srch */
87 int lowstale; /* low stale index */
88 int mid=0; /* midpoint for binary srch */
89 xfs_mount_t *mp; /* filesystem mount point */
90 int needlog; /* need to log header */
91 int needscan; /* need to rescan freespace */
92 __be16 *tagp; /* pointer to tag value */
93 xfs_trans_t *tp; /* transaction structure */
95 trace_xfs_dir2_block_addname(args);
101 * Read the (one and only) directory block into dabuf bp.
104 xfs_da_read_buf(tp, dp, mp->m_dirdatablk, -1, &bp, XFS_DATA_FORK))) {
110 * Check the magic number, corrupted if wrong.
112 if (unlikely(be32_to_cpu(hdr->magic) != XFS_DIR2_BLOCK_MAGIC)) {
113 XFS_CORRUPTION_ERROR("xfs_dir2_block_addname",
114 XFS_ERRLEVEL_LOW, mp, hdr);
115 xfs_da_brelse(tp, bp);
116 return XFS_ERROR(EFSCORRUPTED);
118 len = xfs_dir2_data_entsize(args->namelen);
120 * Set up pointers to parts of the block.
123 btp = xfs_dir2_block_tail_p(mp, hdr);
124 blp = xfs_dir2_block_leaf_p(btp);
126 * No stale entries? Need space for entry and new leaf.
130 * Tag just before the first leaf entry.
132 tagp = (__be16 *)blp - 1;
134 * Data object just before the first leaf entry.
136 enddup = (xfs_dir2_data_unused_t *)((char *)hdr + be16_to_cpu(*tagp));
138 * If it's not free then can't do this add without cleaning up:
139 * the space before the first leaf entry needs to be free so it
140 * can be expanded to hold the pointer to the new entry.
142 if (be16_to_cpu(enddup->freetag) != XFS_DIR2_DATA_FREE_TAG)
145 * Check out the biggest freespace and see if it's the same one.
148 dup = (xfs_dir2_data_unused_t *)
149 ((char *)hdr + be16_to_cpu(bf[0].offset));
152 * It is the biggest freespace, is it too small
153 * to hold the new leaf too?
155 if (be16_to_cpu(dup->length) < len + (uint)sizeof(*blp)) {
157 * Yes, we use the second-largest
158 * entry instead if it works.
160 if (be16_to_cpu(bf[1].length) >= len)
161 dup = (xfs_dir2_data_unused_t *)
163 be16_to_cpu(bf[1].offset));
169 * Not the same free entry,
170 * just check its length.
172 if (be16_to_cpu(dup->length) < len) {
180 * If there are stale entries we'll use one for the leaf.
181 * Is the biggest entry enough to avoid compaction?
183 else if (be16_to_cpu(bf[0].length) >= len) {
184 dup = (xfs_dir2_data_unused_t *)
185 ((char *)hdr + be16_to_cpu(bf[0].offset));
189 * Will need to compact to make this work.
193 * Tag just before the first leaf entry.
195 tagp = (__be16 *)blp - 1;
197 * Data object just before the first leaf entry.
199 dup = (xfs_dir2_data_unused_t *)((char *)hdr + be16_to_cpu(*tagp));
201 * If it's not free then the data will go where the
202 * leaf data starts now, if it works at all.
204 if (be16_to_cpu(dup->freetag) == XFS_DIR2_DATA_FREE_TAG) {
205 if (be16_to_cpu(dup->length) + (be32_to_cpu(btp->stale) - 1) *
206 (uint)sizeof(*blp) < len)
208 } else if ((be32_to_cpu(btp->stale) - 1) * (uint)sizeof(*blp) < len)
211 dup = (xfs_dir2_data_unused_t *)blp;
215 * If this isn't a real add, we're done with the buffer.
217 if (args->op_flags & XFS_DA_OP_JUSTCHECK)
218 xfs_da_brelse(tp, bp);
220 * If we don't have space for the new entry & leaf ...
224 * Not trying to actually do anything, or don't have
225 * a space reservation: return no-space.
227 if ((args->op_flags & XFS_DA_OP_JUSTCHECK) || args->total == 0)
228 return XFS_ERROR(ENOSPC);
230 * Convert to the next larger format.
231 * Then add the new entry in that format.
233 error = xfs_dir2_block_to_leaf(args, bp);
237 return xfs_dir2_leaf_addname(args);
240 * Just checking, and it would work, so say so.
242 if (args->op_flags & XFS_DA_OP_JUSTCHECK)
244 needlog = needscan = 0;
246 * If need to compact the leaf entries, do it now.
247 * Leave the highest-numbered stale entry stale.
248 * XXX should be the one closest to mid but mid is not yet computed.
251 int fromidx; /* source leaf index */
252 int toidx; /* target leaf index */
254 for (fromidx = toidx = be32_to_cpu(btp->count) - 1,
255 highstale = lfloghigh = -1;
258 if (be32_to_cpu(blp[fromidx].address) == XFS_DIR2_NULL_DATAPTR) {
268 blp[toidx] = blp[fromidx];
271 lfloglow = toidx + 1 - (be32_to_cpu(btp->stale) - 1);
272 lfloghigh -= be32_to_cpu(btp->stale) - 1;
273 be32_add_cpu(&btp->count, -(be32_to_cpu(btp->stale) - 1));
274 xfs_dir2_data_make_free(tp, bp,
275 (xfs_dir2_data_aoff_t)((char *)blp - (char *)hdr),
276 (xfs_dir2_data_aoff_t)((be32_to_cpu(btp->stale) - 1) * sizeof(*blp)),
277 &needlog, &needscan);
278 blp += be32_to_cpu(btp->stale) - 1;
279 btp->stale = cpu_to_be32(1);
281 * If we now need to rebuild the bestfree map, do so.
282 * This needs to happen before the next call to use_free.
285 xfs_dir2_data_freescan(mp, (xfs_dir2_data_t *)hdr, &needlog);
290 * Set leaf logging boundaries to impossible state.
291 * For the no-stale case they're set explicitly.
293 else if (btp->stale) {
294 lfloglow = be32_to_cpu(btp->count);
298 * Find the slot that's first lower than our hash value, -1 if none.
300 for (low = 0, high = be32_to_cpu(btp->count) - 1; low <= high; ) {
301 mid = (low + high) >> 1;
302 if ((hash = be32_to_cpu(blp[mid].hashval)) == args->hashval)
304 if (hash < args->hashval)
309 while (mid >= 0 && be32_to_cpu(blp[mid].hashval) >= args->hashval) {
313 * No stale entries, will use enddup space to hold new leaf.
317 * Mark the space needed for the new leaf entry, now in use.
319 xfs_dir2_data_use_free(tp, bp, enddup,
320 (xfs_dir2_data_aoff_t)
321 ((char *)enddup - (char *)hdr + be16_to_cpu(enddup->length) -
323 (xfs_dir2_data_aoff_t)sizeof(*blp),
324 &needlog, &needscan);
326 * Update the tail (entry count).
328 be32_add_cpu(&btp->count, 1);
330 * If we now need to rebuild the bestfree map, do so.
331 * This needs to happen before the next call to use_free.
334 xfs_dir2_data_freescan(mp, (xfs_dir2_data_t *)hdr,
339 * Adjust pointer to the first leaf entry, we're about to move
340 * the table up one to open up space for the new leaf entry.
341 * Then adjust our index to match.
346 memmove(blp, &blp[1], mid * sizeof(*blp));
351 * Use a stale leaf for our new entry.
356 be32_to_cpu(blp[lowstale].address) != XFS_DIR2_NULL_DATAPTR;
359 for (highstale = mid + 1;
360 highstale < be32_to_cpu(btp->count) &&
361 be32_to_cpu(blp[highstale].address) != XFS_DIR2_NULL_DATAPTR &&
362 (lowstale < 0 || mid - lowstale > highstale - mid);
366 * Move entries toward the low-numbered stale entry.
369 (highstale == be32_to_cpu(btp->count) ||
370 mid - lowstale <= highstale - mid)) {
372 memmove(&blp[lowstale], &blp[lowstale + 1],
373 (mid - lowstale) * sizeof(*blp));
374 lfloglow = MIN(lowstale, lfloglow);
375 lfloghigh = MAX(mid, lfloghigh);
378 * Move entries toward the high-numbered stale entry.
381 ASSERT(highstale < be32_to_cpu(btp->count));
384 memmove(&blp[mid + 1], &blp[mid],
385 (highstale - mid) * sizeof(*blp));
386 lfloglow = MIN(mid, lfloglow);
387 lfloghigh = MAX(highstale, lfloghigh);
389 be32_add_cpu(&btp->stale, -1);
392 * Point to the new data entry.
394 dep = (xfs_dir2_data_entry_t *)dup;
396 * Fill in the leaf entry.
398 blp[mid].hashval = cpu_to_be32(args->hashval);
399 blp[mid].address = cpu_to_be32(xfs_dir2_byte_to_dataptr(mp,
400 (char *)dep - (char *)hdr));
401 xfs_dir2_block_log_leaf(tp, bp, lfloglow, lfloghigh);
403 * Mark space for the data entry used.
405 xfs_dir2_data_use_free(tp, bp, dup,
406 (xfs_dir2_data_aoff_t)((char *)dup - (char *)hdr),
407 (xfs_dir2_data_aoff_t)len, &needlog, &needscan);
409 * Create the new data entry.
411 dep->inumber = cpu_to_be64(args->inumber);
412 dep->namelen = args->namelen;
413 memcpy(dep->name, args->name, args->namelen);
414 tagp = xfs_dir2_data_entry_tag_p(dep);
415 *tagp = cpu_to_be16((char *)dep - (char *)hdr);
417 * Clean up the bestfree array and log the header, tail, and entry.
420 xfs_dir2_data_freescan(mp, (xfs_dir2_data_t *)hdr, &needlog);
422 xfs_dir2_data_log_header(tp, bp);
423 xfs_dir2_block_log_tail(tp, bp);
424 xfs_dir2_data_log_entry(tp, bp, dep);
425 xfs_dir2_data_check(dp, bp);
431 * Readdir for block directories.
434 xfs_dir2_block_getdents(
435 xfs_inode_t *dp, /* incore inode */
440 xfs_dir2_data_hdr_t *hdr; /* block header */
441 xfs_dabuf_t *bp; /* buffer for block */
442 xfs_dir2_block_tail_t *btp; /* block tail */
443 xfs_dir2_data_entry_t *dep; /* block data entry */
444 xfs_dir2_data_unused_t *dup; /* block unused entry */
445 char *endptr; /* end of the data entries */
446 int error; /* error return value */
447 xfs_mount_t *mp; /* filesystem mount point */
448 char *ptr; /* current data entry */
449 int wantoff; /* starting block offset */
454 * If the block number in the offset is out of range, we're done.
456 if (xfs_dir2_dataptr_to_db(mp, *offset) > mp->m_dirdatablk) {
460 * Can't read the block, give up, else get dabuf in bp.
462 error = xfs_da_read_buf(NULL, dp, mp->m_dirdatablk, -1,
469 * Extract the byte offset we start at from the seek pointer.
470 * We'll skip entries before this.
472 wantoff = xfs_dir2_dataptr_to_off(mp, *offset);
474 xfs_dir2_data_check(dp, bp);
476 * Set up values for the loop.
478 btp = xfs_dir2_block_tail_p(mp, hdr);
479 ptr = (char *)(hdr + 1);
480 endptr = (char *)xfs_dir2_block_leaf_p(btp);
483 * Loop over the data portion of the block.
484 * Each object is a real entry (dep) or an unused one (dup).
486 while (ptr < endptr) {
487 dup = (xfs_dir2_data_unused_t *)ptr;
491 if (be16_to_cpu(dup->freetag) == XFS_DIR2_DATA_FREE_TAG) {
492 ptr += be16_to_cpu(dup->length);
496 dep = (xfs_dir2_data_entry_t *)ptr;
499 * Bump pointer for the next iteration.
501 ptr += xfs_dir2_data_entsize(dep->namelen);
503 * The entry is before the desired starting point, skip it.
505 if ((char *)dep - (char *)hdr < wantoff)
508 cook = xfs_dir2_db_off_to_dataptr(mp, mp->m_dirdatablk,
509 (char *)dep - (char *)hdr);
512 * If it didn't fit, set the final offset to here & return.
514 if (filldir(dirent, (char *)dep->name, dep->namelen,
515 cook & 0x7fffffff, be64_to_cpu(dep->inumber),
517 *offset = cook & 0x7fffffff;
518 xfs_da_brelse(NULL, bp);
524 * Reached the end of the block.
525 * Set the offset to a non-existent block 1 and return.
527 *offset = xfs_dir2_db_off_to_dataptr(mp, mp->m_dirdatablk + 1, 0) &
529 xfs_da_brelse(NULL, bp);
534 * Log leaf entries from the block.
537 xfs_dir2_block_log_leaf(
538 xfs_trans_t *tp, /* transaction structure */
539 xfs_dabuf_t *bp, /* block buffer */
540 int first, /* index of first logged leaf */
541 int last) /* index of last logged leaf */
543 xfs_dir2_data_hdr_t *hdr = bp->data;
544 xfs_dir2_leaf_entry_t *blp;
545 xfs_dir2_block_tail_t *btp;
547 btp = xfs_dir2_block_tail_p(tp->t_mountp, hdr);
548 blp = xfs_dir2_block_leaf_p(btp);
549 xfs_da_log_buf(tp, bp, (uint)((char *)&blp[first] - (char *)hdr),
550 (uint)((char *)&blp[last + 1] - (char *)hdr - 1));
554 * Log the block tail.
557 xfs_dir2_block_log_tail(
558 xfs_trans_t *tp, /* transaction structure */
559 xfs_dabuf_t *bp) /* block buffer */
561 xfs_dir2_data_hdr_t *hdr = bp->data;
562 xfs_dir2_block_tail_t *btp;
564 btp = xfs_dir2_block_tail_p(tp->t_mountp, hdr);
565 xfs_da_log_buf(tp, bp, (uint)((char *)btp - (char *)hdr),
566 (uint)((char *)(btp + 1) - (char *)hdr - 1));
570 * Look up an entry in the block. This is the external routine,
571 * xfs_dir2_block_lookup_int does the real work.
574 xfs_dir2_block_lookup(
575 xfs_da_args_t *args) /* dir lookup arguments */
577 xfs_dir2_data_hdr_t *hdr; /* block header */
578 xfs_dir2_leaf_entry_t *blp; /* block leaf entries */
579 xfs_dabuf_t *bp; /* block buffer */
580 xfs_dir2_block_tail_t *btp; /* block tail */
581 xfs_dir2_data_entry_t *dep; /* block data entry */
582 xfs_inode_t *dp; /* incore inode */
583 int ent; /* entry index */
584 int error; /* error return value */
585 xfs_mount_t *mp; /* filesystem mount point */
587 trace_xfs_dir2_block_lookup(args);
590 * Get the buffer, look up the entry.
591 * If not found (ENOENT) then return, have no buffer.
593 if ((error = xfs_dir2_block_lookup_int(args, &bp, &ent)))
598 xfs_dir2_data_check(dp, bp);
599 btp = xfs_dir2_block_tail_p(mp, hdr);
600 blp = xfs_dir2_block_leaf_p(btp);
602 * Get the offset from the leaf entry, to point to the data.
604 dep = (xfs_dir2_data_entry_t *)((char *)hdr +
605 xfs_dir2_dataptr_to_off(mp, be32_to_cpu(blp[ent].address)));
607 * Fill in inode number, CI name if appropriate, release the block.
609 args->inumber = be64_to_cpu(dep->inumber);
610 error = xfs_dir_cilookup_result(args, dep->name, dep->namelen);
611 xfs_da_brelse(args->trans, bp);
612 return XFS_ERROR(error);
616 * Internal block lookup routine.
618 static int /* error */
619 xfs_dir2_block_lookup_int(
620 xfs_da_args_t *args, /* dir lookup arguments */
621 xfs_dabuf_t **bpp, /* returned block buffer */
622 int *entno) /* returned entry number */
624 xfs_dir2_dataptr_t addr; /* data entry address */
625 xfs_dir2_data_hdr_t *hdr; /* block header */
626 xfs_dir2_leaf_entry_t *blp; /* block leaf entries */
627 xfs_dabuf_t *bp; /* block buffer */
628 xfs_dir2_block_tail_t *btp; /* block tail */
629 xfs_dir2_data_entry_t *dep; /* block data entry */
630 xfs_inode_t *dp; /* incore inode */
631 int error; /* error return value */
632 xfs_dahash_t hash; /* found hash value */
633 int high; /* binary search high index */
634 int low; /* binary search low index */
635 int mid; /* binary search current idx */
636 xfs_mount_t *mp; /* filesystem mount point */
637 xfs_trans_t *tp; /* transaction pointer */
638 enum xfs_dacmp cmp; /* comparison result */
644 * Read the buffer, return error if we can't get it.
647 xfs_da_read_buf(tp, dp, mp->m_dirdatablk, -1, &bp, XFS_DATA_FORK))) {
652 xfs_dir2_data_check(dp, bp);
653 btp = xfs_dir2_block_tail_p(mp, hdr);
654 blp = xfs_dir2_block_leaf_p(btp);
656 * Loop doing a binary search for our hash value.
657 * Find our entry, ENOENT if it's not there.
659 for (low = 0, high = be32_to_cpu(btp->count) - 1; ; ) {
661 mid = (low + high) >> 1;
662 if ((hash = be32_to_cpu(blp[mid].hashval)) == args->hashval)
664 if (hash < args->hashval)
669 ASSERT(args->op_flags & XFS_DA_OP_OKNOENT);
670 xfs_da_brelse(tp, bp);
671 return XFS_ERROR(ENOENT);
675 * Back up to the first one with the right hash value.
677 while (mid > 0 && be32_to_cpu(blp[mid - 1].hashval) == args->hashval) {
681 * Now loop forward through all the entries with the
682 * right hash value looking for our name.
685 if ((addr = be32_to_cpu(blp[mid].address)) == XFS_DIR2_NULL_DATAPTR)
688 * Get pointer to the entry from the leaf.
690 dep = (xfs_dir2_data_entry_t *)
691 ((char *)hdr + xfs_dir2_dataptr_to_off(mp, addr));
693 * Compare name and if it's an exact match, return the index
694 * and buffer. If it's the first case-insensitive match, store
695 * the index and buffer and continue looking for an exact match.
697 cmp = mp->m_dirnameops->compname(args, dep->name, dep->namelen);
698 if (cmp != XFS_CMP_DIFFERENT && cmp != args->cmpresult) {
699 args->cmpresult = cmp;
702 if (cmp == XFS_CMP_EXACT)
705 } while (++mid < be32_to_cpu(btp->count) &&
706 be32_to_cpu(blp[mid].hashval) == hash);
708 ASSERT(args->op_flags & XFS_DA_OP_OKNOENT);
710 * Here, we can only be doing a lookup (not a rename or replace).
711 * If a case-insensitive match was found earlier, return success.
713 if (args->cmpresult == XFS_CMP_CASE)
716 * No match, release the buffer and return ENOENT.
718 xfs_da_brelse(tp, bp);
719 return XFS_ERROR(ENOENT);
723 * Remove an entry from a block format directory.
724 * If that makes the block small enough to fit in shortform, transform it.
727 xfs_dir2_block_removename(
728 xfs_da_args_t *args) /* directory operation args */
730 xfs_dir2_data_hdr_t *hdr; /* block header */
731 xfs_dir2_leaf_entry_t *blp; /* block leaf pointer */
732 xfs_dabuf_t *bp; /* block buffer */
733 xfs_dir2_block_tail_t *btp; /* block tail */
734 xfs_dir2_data_entry_t *dep; /* block data entry */
735 xfs_inode_t *dp; /* incore inode */
736 int ent; /* block leaf entry index */
737 int error; /* error return value */
738 xfs_mount_t *mp; /* filesystem mount point */
739 int needlog; /* need to log block header */
740 int needscan; /* need to fixup bestfree */
741 xfs_dir2_sf_hdr_t sfh; /* shortform header */
742 int size; /* shortform size */
743 xfs_trans_t *tp; /* transaction pointer */
745 trace_xfs_dir2_block_removename(args);
748 * Look up the entry in the block. Gets the buffer and entry index.
749 * It will always be there, the vnodeops level does a lookup first.
751 if ((error = xfs_dir2_block_lookup_int(args, &bp, &ent))) {
758 btp = xfs_dir2_block_tail_p(mp, hdr);
759 blp = xfs_dir2_block_leaf_p(btp);
761 * Point to the data entry using the leaf entry.
763 dep = (xfs_dir2_data_entry_t *)
764 ((char *)hdr + xfs_dir2_dataptr_to_off(mp, be32_to_cpu(blp[ent].address)));
766 * Mark the data entry's space free.
768 needlog = needscan = 0;
769 xfs_dir2_data_make_free(tp, bp,
770 (xfs_dir2_data_aoff_t)((char *)dep - (char *)hdr),
771 xfs_dir2_data_entsize(dep->namelen), &needlog, &needscan);
773 * Fix up the block tail.
775 be32_add_cpu(&btp->stale, 1);
776 xfs_dir2_block_log_tail(tp, bp);
778 * Remove the leaf entry by marking it stale.
780 blp[ent].address = cpu_to_be32(XFS_DIR2_NULL_DATAPTR);
781 xfs_dir2_block_log_leaf(tp, bp, ent, ent);
783 * Fix up bestfree, log the header if necessary.
786 xfs_dir2_data_freescan(mp, (xfs_dir2_data_t *)hdr, &needlog);
788 xfs_dir2_data_log_header(tp, bp);
789 xfs_dir2_data_check(dp, bp);
791 * See if the size as a shortform is good enough.
793 size = xfs_dir2_block_sfsize(dp, hdr, &sfh);
794 if (size > XFS_IFORK_DSIZE(dp)) {
799 * If it works, do the conversion.
801 return xfs_dir2_block_to_sf(args, bp, size, &sfh);
805 * Replace an entry in a V2 block directory.
806 * Change the inode number to the new value.
809 xfs_dir2_block_replace(
810 xfs_da_args_t *args) /* directory operation args */
812 xfs_dir2_data_hdr_t *hdr; /* block header */
813 xfs_dir2_leaf_entry_t *blp; /* block leaf entries */
814 xfs_dabuf_t *bp; /* block buffer */
815 xfs_dir2_block_tail_t *btp; /* block tail */
816 xfs_dir2_data_entry_t *dep; /* block data entry */
817 xfs_inode_t *dp; /* incore inode */
818 int ent; /* leaf entry index */
819 int error; /* error return value */
820 xfs_mount_t *mp; /* filesystem mount point */
822 trace_xfs_dir2_block_replace(args);
825 * Lookup the entry in the directory. Get buffer and entry index.
826 * This will always succeed since the caller has already done a lookup.
828 if ((error = xfs_dir2_block_lookup_int(args, &bp, &ent))) {
834 btp = xfs_dir2_block_tail_p(mp, hdr);
835 blp = xfs_dir2_block_leaf_p(btp);
837 * Point to the data entry we need to change.
839 dep = (xfs_dir2_data_entry_t *)
840 ((char *)hdr + xfs_dir2_dataptr_to_off(mp, be32_to_cpu(blp[ent].address)));
841 ASSERT(be64_to_cpu(dep->inumber) != args->inumber);
843 * Change the inode number to the new value.
845 dep->inumber = cpu_to_be64(args->inumber);
846 xfs_dir2_data_log_entry(args->trans, bp, dep);
847 xfs_dir2_data_check(dp, bp);
853 * Qsort comparison routine for the block leaf entries.
855 static int /* sort order */
857 const void *a, /* first leaf entry */
858 const void *b) /* second leaf entry */
860 const xfs_dir2_leaf_entry_t *la; /* first leaf entry */
861 const xfs_dir2_leaf_entry_t *lb; /* second leaf entry */
865 return be32_to_cpu(la->hashval) < be32_to_cpu(lb->hashval) ? -1 :
866 (be32_to_cpu(la->hashval) > be32_to_cpu(lb->hashval) ? 1 : 0);
870 * Convert a V2 leaf directory to a V2 block directory if possible.
873 xfs_dir2_leaf_to_block(
874 xfs_da_args_t *args, /* operation arguments */
875 xfs_dabuf_t *lbp, /* leaf buffer */
876 xfs_dabuf_t *dbp) /* data buffer */
878 __be16 *bestsp; /* leaf bests table */
879 xfs_dir2_data_hdr_t *hdr; /* block header */
880 xfs_dir2_block_tail_t *btp; /* block tail */
881 xfs_inode_t *dp; /* incore directory inode */
882 xfs_dir2_data_unused_t *dup; /* unused data entry */
883 int error; /* error return value */
884 int from; /* leaf from index */
885 xfs_dir2_leaf_t *leaf; /* leaf structure */
886 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
887 xfs_dir2_leaf_tail_t *ltp; /* leaf tail structure */
888 xfs_mount_t *mp; /* file system mount point */
889 int needlog; /* need to log data header */
890 int needscan; /* need to scan for bestfree */
891 xfs_dir2_sf_hdr_t sfh; /* shortform header */
892 int size; /* bytes used */
893 __be16 *tagp; /* end of entry (tag) */
894 int to; /* block/leaf to index */
895 xfs_trans_t *tp; /* transaction pointer */
897 trace_xfs_dir2_leaf_to_block(args);
903 ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_DIR2_LEAF1_MAGIC);
904 ltp = xfs_dir2_leaf_tail_p(mp, leaf);
906 * If there are data blocks other than the first one, take this
907 * opportunity to remove trailing empty data blocks that may have
908 * been left behind during no-space-reservation operations.
909 * These will show up in the leaf bests table.
911 while (dp->i_d.di_size > mp->m_dirblksize) {
912 bestsp = xfs_dir2_leaf_bests_p(ltp);
913 if (be16_to_cpu(bestsp[be32_to_cpu(ltp->bestcount) - 1]) ==
914 mp->m_dirblksize - (uint)sizeof(*hdr)) {
916 xfs_dir2_leaf_trim_data(args, lbp,
917 (xfs_dir2_db_t)(be32_to_cpu(ltp->bestcount) - 1))))
925 * Read the data block if we don't already have it, give up if it fails.
928 (error = xfs_da_read_buf(tp, dp, mp->m_dirdatablk, -1, &dbp,
933 ASSERT(be32_to_cpu(hdr->magic) == XFS_DIR2_DATA_MAGIC);
935 * Size of the "leaf" area in the block.
937 size = (uint)sizeof(xfs_dir2_block_tail_t) +
938 (uint)sizeof(*lep) * (be16_to_cpu(leaf->hdr.count) - be16_to_cpu(leaf->hdr.stale));
940 * Look at the last data entry.
942 tagp = (__be16 *)((char *)hdr + mp->m_dirblksize) - 1;
943 dup = (xfs_dir2_data_unused_t *)((char *)hdr + be16_to_cpu(*tagp));
945 * If it's not free or is too short we can't do it.
947 if (be16_to_cpu(dup->freetag) != XFS_DIR2_DATA_FREE_TAG ||
948 be16_to_cpu(dup->length) < size) {
953 * Start converting it to block form.
955 hdr->magic = cpu_to_be32(XFS_DIR2_BLOCK_MAGIC);
959 * Use up the space at the end of the block (blp/btp).
961 xfs_dir2_data_use_free(tp, dbp, dup, mp->m_dirblksize - size, size,
962 &needlog, &needscan);
964 * Initialize the block tail.
966 btp = xfs_dir2_block_tail_p(mp, hdr);
967 btp->count = cpu_to_be32(be16_to_cpu(leaf->hdr.count) - be16_to_cpu(leaf->hdr.stale));
969 xfs_dir2_block_log_tail(tp, dbp);
971 * Initialize the block leaf area. We compact out stale entries.
973 lep = xfs_dir2_block_leaf_p(btp);
974 for (from = to = 0; from < be16_to_cpu(leaf->hdr.count); from++) {
975 if (be32_to_cpu(leaf->ents[from].address) == XFS_DIR2_NULL_DATAPTR)
977 lep[to++] = leaf->ents[from];
979 ASSERT(to == be32_to_cpu(btp->count));
980 xfs_dir2_block_log_leaf(tp, dbp, 0, be32_to_cpu(btp->count) - 1);
982 * Scan the bestfree if we need it and log the data block header.
985 xfs_dir2_data_freescan(mp, (xfs_dir2_data_t *)hdr, &needlog);
987 xfs_dir2_data_log_header(tp, dbp);
989 * Pitch the old leaf block.
991 error = xfs_da_shrink_inode(args, mp->m_dirleafblk, lbp);
997 * Now see if the resulting block can be shrunken to shortform.
999 size = xfs_dir2_block_sfsize(dp, hdr, &sfh);
1000 if (size > XFS_IFORK_DSIZE(dp)) {
1004 return xfs_dir2_block_to_sf(args, dbp, size, &sfh);
1007 xfs_da_buf_done(lbp);
1009 xfs_da_buf_done(dbp);
1014 * Convert the shortform directory to block form.
1017 xfs_dir2_sf_to_block(
1018 xfs_da_args_t *args) /* operation arguments */
1020 xfs_dir2_db_t blkno; /* dir-relative block # (0) */
1021 xfs_dir2_data_hdr_t *hdr; /* block header */
1022 xfs_dir2_leaf_entry_t *blp; /* block leaf entries */
1023 xfs_dabuf_t *bp; /* block buffer */
1024 xfs_dir2_block_tail_t *btp; /* block tail pointer */
1025 xfs_dir2_data_entry_t *dep; /* data entry pointer */
1026 xfs_inode_t *dp; /* incore directory inode */
1027 int dummy; /* trash */
1028 xfs_dir2_data_unused_t *dup; /* unused entry pointer */
1029 int endoffset; /* end of data objects */
1030 int error; /* error return value */
1032 xfs_mount_t *mp; /* filesystem mount point */
1033 int needlog; /* need to log block header */
1034 int needscan; /* need to scan block freespc */
1035 int newoffset; /* offset from current entry */
1036 int offset; /* target block offset */
1037 xfs_dir2_sf_entry_t *sfep; /* sf entry pointer */
1038 xfs_dir2_sf_hdr_t *oldsfp; /* old shortform header */
1039 xfs_dir2_sf_hdr_t *sfp; /* shortform header */
1040 __be16 *tagp; /* end of data entry */
1041 xfs_trans_t *tp; /* transaction pointer */
1042 struct xfs_name name;
1044 trace_xfs_dir2_sf_to_block(args);
1049 ASSERT(dp->i_df.if_flags & XFS_IFINLINE);
1051 * Bomb out if the shortform directory is way too short.
1053 if (dp->i_d.di_size < offsetof(xfs_dir2_sf_hdr_t, parent)) {
1054 ASSERT(XFS_FORCED_SHUTDOWN(mp));
1055 return XFS_ERROR(EIO);
1058 oldsfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
1060 ASSERT(dp->i_df.if_bytes == dp->i_d.di_size);
1061 ASSERT(dp->i_df.if_u1.if_data != NULL);
1062 ASSERT(dp->i_d.di_size >= xfs_dir2_sf_hdr_size(oldsfp->i8count));
1065 * Copy the directory into a temporary buffer.
1066 * Then pitch the incore inode data so we can make extents.
1068 sfp = kmem_alloc(dp->i_df.if_bytes, KM_SLEEP);
1069 memcpy(sfp, oldsfp, dp->i_df.if_bytes);
1071 xfs_idata_realloc(dp, -dp->i_df.if_bytes, XFS_DATA_FORK);
1072 dp->i_d.di_size = 0;
1073 xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
1076 * Add block 0 to the inode.
1078 error = xfs_dir2_grow_inode(args, XFS_DIR2_DATA_SPACE, &blkno);
1084 * Initialize the data block.
1086 error = xfs_dir2_data_init(args, blkno, &bp);
1092 hdr->magic = cpu_to_be32(XFS_DIR2_BLOCK_MAGIC);
1094 * Compute size of block "tail" area.
1096 i = (uint)sizeof(*btp) +
1097 (sfp->count + 2) * (uint)sizeof(xfs_dir2_leaf_entry_t);
1099 * The whole thing is initialized to free by the init routine.
1100 * Say we're using the leaf and tail area.
1102 dup = (xfs_dir2_data_unused_t *)(hdr + 1);
1103 needlog = needscan = 0;
1104 xfs_dir2_data_use_free(tp, bp, dup, mp->m_dirblksize - i, i, &needlog,
1106 ASSERT(needscan == 0);
1110 btp = xfs_dir2_block_tail_p(mp, hdr);
1111 btp->count = cpu_to_be32(sfp->count + 2); /* ., .. */
1113 blp = xfs_dir2_block_leaf_p(btp);
1114 endoffset = (uint)((char *)blp - (char *)hdr);
1116 * Remove the freespace, we'll manage it.
1118 xfs_dir2_data_use_free(tp, bp, dup,
1119 (xfs_dir2_data_aoff_t)((char *)dup - (char *)hdr),
1120 be16_to_cpu(dup->length), &needlog, &needscan);
1122 * Create entry for .
1124 dep = (xfs_dir2_data_entry_t *)
1125 ((char *)hdr + XFS_DIR2_DATA_DOT_OFFSET);
1126 dep->inumber = cpu_to_be64(dp->i_ino);
1129 tagp = xfs_dir2_data_entry_tag_p(dep);
1130 *tagp = cpu_to_be16((char *)dep - (char *)hdr);
1131 xfs_dir2_data_log_entry(tp, bp, dep);
1132 blp[0].hashval = cpu_to_be32(xfs_dir_hash_dot);
1133 blp[0].address = cpu_to_be32(xfs_dir2_byte_to_dataptr(mp,
1134 (char *)dep - (char *)hdr));
1136 * Create entry for ..
1138 dep = (xfs_dir2_data_entry_t *)
1139 ((char *)hdr + XFS_DIR2_DATA_DOTDOT_OFFSET);
1140 dep->inumber = cpu_to_be64(xfs_dir2_sf_get_parent_ino(sfp));
1142 dep->name[0] = dep->name[1] = '.';
1143 tagp = xfs_dir2_data_entry_tag_p(dep);
1144 *tagp = cpu_to_be16((char *)dep - (char *)hdr);
1145 xfs_dir2_data_log_entry(tp, bp, dep);
1146 blp[1].hashval = cpu_to_be32(xfs_dir_hash_dotdot);
1147 blp[1].address = cpu_to_be32(xfs_dir2_byte_to_dataptr(mp,
1148 (char *)dep - (char *)hdr));
1149 offset = XFS_DIR2_DATA_FIRST_OFFSET;
1151 * Loop over existing entries, stuff them in.
1157 sfep = xfs_dir2_sf_firstentry(sfp);
1159 * Need to preserve the existing offset values in the sf directory.
1160 * Insert holes (unused entries) where necessary.
1162 while (offset < endoffset) {
1164 * sfep is null when we reach the end of the list.
1167 newoffset = endoffset;
1169 newoffset = xfs_dir2_sf_get_offset(sfep);
1171 * There should be a hole here, make one.
1173 if (offset < newoffset) {
1174 dup = (xfs_dir2_data_unused_t *)((char *)hdr + offset);
1175 dup->freetag = cpu_to_be16(XFS_DIR2_DATA_FREE_TAG);
1176 dup->length = cpu_to_be16(newoffset - offset);
1177 *xfs_dir2_data_unused_tag_p(dup) = cpu_to_be16(
1178 ((char *)dup - (char *)hdr));
1179 xfs_dir2_data_log_unused(tp, bp, dup);
1180 (void)xfs_dir2_data_freeinsert((xfs_dir2_data_t *)hdr,
1182 offset += be16_to_cpu(dup->length);
1186 * Copy a real entry.
1188 dep = (xfs_dir2_data_entry_t *)((char *)hdr + newoffset);
1189 dep->inumber = cpu_to_be64(xfs_dir2_sfe_get_ino(sfp, sfep));
1190 dep->namelen = sfep->namelen;
1191 memcpy(dep->name, sfep->name, dep->namelen);
1192 tagp = xfs_dir2_data_entry_tag_p(dep);
1193 *tagp = cpu_to_be16((char *)dep - (char *)hdr);
1194 xfs_dir2_data_log_entry(tp, bp, dep);
1195 name.name = sfep->name;
1196 name.len = sfep->namelen;
1197 blp[2 + i].hashval = cpu_to_be32(mp->m_dirnameops->
1199 blp[2 + i].address = cpu_to_be32(xfs_dir2_byte_to_dataptr(mp,
1200 (char *)dep - (char *)hdr));
1201 offset = (int)((char *)(tagp + 1) - (char *)hdr);
1202 if (++i == sfp->count)
1205 sfep = xfs_dir2_sf_nextentry(sfp, sfep);
1207 /* Done with the temporary buffer */
1210 * Sort the leaf entries by hash value.
1212 xfs_sort(blp, be32_to_cpu(btp->count), sizeof(*blp), xfs_dir2_block_sort);
1214 * Log the leaf entry area and tail.
1215 * Already logged the header in data_init, ignore needlog.
1217 ASSERT(needscan == 0);
1218 xfs_dir2_block_log_leaf(tp, bp, 0, be32_to_cpu(btp->count) - 1);
1219 xfs_dir2_block_log_tail(tp, bp);
1220 xfs_dir2_data_check(dp, bp);
1221 xfs_da_buf_done(bp);