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_format.h"
21 #include "xfs_log_format.h"
22 #include "xfs_trans_resv.h"
23 #include "xfs_mount.h"
24 #include "xfs_da_format.h"
25 #include "xfs_da_btree.h"
26 #include "xfs_inode.h"
27 #include "xfs_trans.h"
28 #include "xfs_inode_item.h"
29 #include "xfs_error.h"
31 #include "xfs_dir2_priv.h"
32 #include "xfs_trace.h"
35 * Prototypes for internal functions.
37 static void xfs_dir2_sf_addname_easy(xfs_da_args_t *args,
38 xfs_dir2_sf_entry_t *sfep,
39 xfs_dir2_data_aoff_t offset,
41 static void xfs_dir2_sf_addname_hard(xfs_da_args_t *args, int objchange,
43 static int xfs_dir2_sf_addname_pick(xfs_da_args_t *args, int objchange,
44 xfs_dir2_sf_entry_t **sfepp,
45 xfs_dir2_data_aoff_t *offsetp);
47 static void xfs_dir2_sf_check(xfs_da_args_t *args);
49 #define xfs_dir2_sf_check(args)
52 static void xfs_dir2_sf_toino4(xfs_da_args_t *args);
53 static void xfs_dir2_sf_toino8(xfs_da_args_t *args);
56 * Given a block directory (dp/block), calculate its size as a shortform (sf)
57 * directory and a header for the sf directory, if it will fit it the
58 * space currently present in the inode. If it won't fit, the output
59 * size is too big (but not accurate).
61 int /* size for sf form */
62 xfs_dir2_block_sfsize(
63 xfs_inode_t *dp, /* incore inode pointer */
64 xfs_dir2_data_hdr_t *hdr, /* block directory data */
65 xfs_dir2_sf_hdr_t *sfhp) /* output: header for sf form */
67 xfs_dir2_dataptr_t addr; /* data entry address */
68 xfs_dir2_leaf_entry_t *blp; /* leaf area of the block */
69 xfs_dir2_block_tail_t *btp; /* tail area of the block */
70 int count; /* shortform entry count */
71 xfs_dir2_data_entry_t *dep; /* data entry in the block */
72 int i; /* block entry index */
73 int i8count; /* count of big-inode entries */
74 int isdot; /* entry is "." */
75 int isdotdot; /* entry is ".." */
76 xfs_mount_t *mp; /* mount structure pointer */
77 int namelen; /* total name bytes */
78 xfs_ino_t parent = 0; /* parent inode number */
79 int size=0; /* total computed size */
81 struct xfs_da_geometry *geo;
87 * if there is a filetype field, add the extra byte to the namelen
88 * for each entry that we see.
90 has_ftype = xfs_sb_version_hasftype(&mp->m_sb) ? 1 : 0;
92 count = i8count = namelen = 0;
93 btp = xfs_dir2_block_tail_p(geo, hdr);
94 blp = xfs_dir2_block_leaf_p(btp);
97 * Iterate over the block's data entries by using the leaf pointers.
99 for (i = 0; i < be32_to_cpu(btp->count); i++) {
100 if ((addr = be32_to_cpu(blp[i].address)) == XFS_DIR2_NULL_DATAPTR)
103 * Calculate the pointer to the entry at hand.
105 dep = (xfs_dir2_data_entry_t *)((char *)hdr +
106 xfs_dir2_dataptr_to_off(geo, addr));
108 * Detect . and .., so we can special-case them.
109 * . is not included in sf directories.
110 * .. is included by just the parent inode number.
112 isdot = dep->namelen == 1 && dep->name[0] == '.';
115 dep->name[0] == '.' && dep->name[1] == '.';
118 i8count += be64_to_cpu(dep->inumber) > XFS_DIR2_MAX_SHORT_INUM;
120 /* take into account the file type field */
121 if (!isdot && !isdotdot) {
123 namelen += dep->namelen + has_ftype;
125 parent = be64_to_cpu(dep->inumber);
127 * Calculate the new size, see if we should give up yet.
129 size = xfs_dir2_sf_hdr_size(i8count) + /* header */
130 count + /* namelen */
131 count * (uint)sizeof(xfs_dir2_sf_off_t) + /* offset */
133 (i8count ? /* inumber */
134 (uint)sizeof(xfs_dir2_ino8_t) * count :
135 (uint)sizeof(xfs_dir2_ino4_t) * count);
136 if (size > XFS_IFORK_DSIZE(dp))
137 return size; /* size value is a failure */
140 * Create the output header, if it worked.
143 sfhp->i8count = i8count;
144 dp->d_ops->sf_put_parent_ino(sfhp, parent);
149 * Convert a block format directory to shortform.
150 * Caller has already checked that it will fit, and built us a header.
153 xfs_dir2_block_to_sf(
154 xfs_da_args_t *args, /* operation arguments */
156 int size, /* shortform directory size */
157 xfs_dir2_sf_hdr_t *sfhp) /* shortform directory hdr */
159 xfs_dir2_data_hdr_t *hdr; /* block header */
160 xfs_dir2_block_tail_t *btp; /* block tail pointer */
161 xfs_dir2_data_entry_t *dep; /* data entry pointer */
162 xfs_inode_t *dp; /* incore directory inode */
163 xfs_dir2_data_unused_t *dup; /* unused data pointer */
164 char *endptr; /* end of data entries */
165 int error; /* error return value */
166 int logflags; /* inode logging flags */
167 xfs_mount_t *mp; /* filesystem mount point */
168 char *ptr; /* current data pointer */
169 xfs_dir2_sf_entry_t *sfep; /* shortform entry */
170 xfs_dir2_sf_hdr_t *sfp; /* shortform directory header */
171 xfs_dir2_sf_hdr_t *dst; /* temporary data buffer */
173 trace_xfs_dir2_block_to_sf(args);
179 * allocate a temporary destination buffer the size of the inode
180 * to format the data into. Once we have formatted the data, we
181 * can free the block and copy the formatted data into the inode literal
184 dst = kmem_alloc(mp->m_sb.sb_inodesize, KM_SLEEP);
188 * Copy the header into the newly allocate local space.
190 sfp = (xfs_dir2_sf_hdr_t *)dst;
191 memcpy(sfp, sfhp, xfs_dir2_sf_hdr_size(sfhp->i8count));
194 * Set up to loop over the block's entries.
196 btp = xfs_dir2_block_tail_p(args->geo, hdr);
197 ptr = (char *)dp->d_ops->data_entry_p(hdr);
198 endptr = (char *)xfs_dir2_block_leaf_p(btp);
199 sfep = xfs_dir2_sf_firstentry(sfp);
201 * Loop over the active and unused entries.
202 * Stop when we reach the leaf/tail portion of the block.
204 while (ptr < endptr) {
206 * If it's unused, just skip over it.
208 dup = (xfs_dir2_data_unused_t *)ptr;
209 if (be16_to_cpu(dup->freetag) == XFS_DIR2_DATA_FREE_TAG) {
210 ptr += be16_to_cpu(dup->length);
213 dep = (xfs_dir2_data_entry_t *)ptr;
217 if (dep->namelen == 1 && dep->name[0] == '.')
218 ASSERT(be64_to_cpu(dep->inumber) == dp->i_ino);
220 * Skip .., but make sure the inode number is right.
222 else if (dep->namelen == 2 &&
223 dep->name[0] == '.' && dep->name[1] == '.')
224 ASSERT(be64_to_cpu(dep->inumber) ==
225 dp->d_ops->sf_get_parent_ino(sfp));
227 * Normal entry, copy it into shortform.
230 sfep->namelen = dep->namelen;
231 xfs_dir2_sf_put_offset(sfep,
232 (xfs_dir2_data_aoff_t)
233 ((char *)dep - (char *)hdr));
234 memcpy(sfep->name, dep->name, dep->namelen);
235 dp->d_ops->sf_put_ino(sfp, sfep,
236 be64_to_cpu(dep->inumber));
237 dp->d_ops->sf_put_ftype(sfep,
238 dp->d_ops->data_get_ftype(dep));
240 sfep = dp->d_ops->sf_nextentry(sfp, sfep);
242 ptr += dp->d_ops->data_entsize(dep->namelen);
244 ASSERT((char *)sfep - (char *)sfp == size);
246 /* now we are done with the block, we can shrink the inode */
247 logflags = XFS_ILOG_CORE;
248 error = xfs_dir2_shrink_inode(args, args->geo->datablk, bp);
250 ASSERT(error != -ENOSPC);
255 * The buffer is now unconditionally gone, whether
256 * xfs_dir2_shrink_inode worked or not.
258 * Convert the inode to local format and copy the data in.
260 ASSERT(dp->i_df.if_bytes == 0);
261 xfs_init_local_fork(dp, XFS_DATA_FORK, dst, size);
262 dp->i_d.di_format = XFS_DINODE_FMT_LOCAL;
263 dp->i_d.di_size = size;
265 logflags |= XFS_ILOG_DDATA;
266 xfs_dir2_sf_check(args);
268 xfs_trans_log_inode(args->trans, dp, logflags);
274 * Add a name to a shortform directory.
275 * There are two algorithms, "easy" and "hard" which we decide on
276 * before changing anything.
277 * Convert to block form if necessary, if the new entry won't fit.
281 xfs_da_args_t *args) /* operation arguments */
283 xfs_inode_t *dp; /* incore directory inode */
284 int error; /* error return value */
285 int incr_isize; /* total change in size */
286 int new_isize; /* di_size after adding name */
287 int objchange; /* changing to 8-byte inodes */
288 xfs_dir2_data_aoff_t offset = 0; /* offset for new entry */
289 int pick; /* which algorithm to use */
290 xfs_dir2_sf_hdr_t *sfp; /* shortform structure */
291 xfs_dir2_sf_entry_t *sfep = NULL; /* shortform entry */
293 trace_xfs_dir2_sf_addname(args);
295 ASSERT(xfs_dir2_sf_lookup(args) == -ENOENT);
297 ASSERT(dp->i_df.if_flags & XFS_IFINLINE);
299 * Make sure the shortform value has some of its header.
301 if (dp->i_d.di_size < offsetof(xfs_dir2_sf_hdr_t, parent)) {
302 ASSERT(XFS_FORCED_SHUTDOWN(dp->i_mount));
305 ASSERT(dp->i_df.if_bytes == dp->i_d.di_size);
306 ASSERT(dp->i_df.if_u1.if_data != NULL);
307 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
308 ASSERT(dp->i_d.di_size >= xfs_dir2_sf_hdr_size(sfp->i8count));
310 * Compute entry (and change in) size.
312 incr_isize = dp->d_ops->sf_entsize(sfp, args->namelen);
316 * Do we have to change to 8 byte inodes?
318 if (args->inumber > XFS_DIR2_MAX_SHORT_INUM && sfp->i8count == 0) {
320 * Yes, adjust the inode size. old count + (parent + new)
324 ((uint)sizeof(xfs_dir2_ino8_t) -
325 (uint)sizeof(xfs_dir2_ino4_t));
329 new_isize = (int)dp->i_d.di_size + incr_isize;
331 * Won't fit as shortform any more (due to size),
332 * or the pick routine says it won't (due to offset values).
334 if (new_isize > XFS_IFORK_DSIZE(dp) ||
336 xfs_dir2_sf_addname_pick(args, objchange, &sfep, &offset)) == 0) {
338 * Just checking or no space reservation, it doesn't fit.
340 if ((args->op_flags & XFS_DA_OP_JUSTCHECK) || args->total == 0)
343 * Convert to block form then add the name.
345 error = xfs_dir2_sf_to_block(args);
348 return xfs_dir2_block_addname(args);
351 * Just checking, it fits.
353 if (args->op_flags & XFS_DA_OP_JUSTCHECK)
356 * Do it the easy way - just add it at the end.
359 xfs_dir2_sf_addname_easy(args, sfep, offset, new_isize);
361 * Do it the hard way - look for a place to insert the new entry.
362 * Convert to 8 byte inode numbers first if necessary.
367 xfs_dir2_sf_toino8(args);
368 xfs_dir2_sf_addname_hard(args, objchange, new_isize);
370 xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE | XFS_ILOG_DDATA);
375 * Add the new entry the "easy" way.
376 * This is copying the old directory and adding the new entry at the end.
377 * Since it's sorted by "offset" we need room after the last offset
378 * that's already there, and then room to convert to a block directory.
379 * This is already checked by the pick routine.
382 xfs_dir2_sf_addname_easy(
383 xfs_da_args_t *args, /* operation arguments */
384 xfs_dir2_sf_entry_t *sfep, /* pointer to new entry */
385 xfs_dir2_data_aoff_t offset, /* offset to use for new ent */
386 int new_isize) /* new directory size */
388 int byteoff; /* byte offset in sf dir */
389 xfs_inode_t *dp; /* incore directory inode */
390 xfs_dir2_sf_hdr_t *sfp; /* shortform structure */
394 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
395 byteoff = (int)((char *)sfep - (char *)sfp);
397 * Grow the in-inode space.
399 xfs_idata_realloc(dp, dp->d_ops->sf_entsize(sfp, args->namelen),
402 * Need to set up again due to realloc of the inode data.
404 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
405 sfep = (xfs_dir2_sf_entry_t *)((char *)sfp + byteoff);
407 * Fill in the new entry.
409 sfep->namelen = args->namelen;
410 xfs_dir2_sf_put_offset(sfep, offset);
411 memcpy(sfep->name, args->name, sfep->namelen);
412 dp->d_ops->sf_put_ino(sfp, sfep, args->inumber);
413 dp->d_ops->sf_put_ftype(sfep, args->filetype);
416 * Update the header and inode.
419 if (args->inumber > XFS_DIR2_MAX_SHORT_INUM)
421 dp->i_d.di_size = new_isize;
422 xfs_dir2_sf_check(args);
426 * Add the new entry the "hard" way.
427 * The caller has already converted to 8 byte inode numbers if necessary,
428 * in which case we need to leave the i8count at 1.
429 * Find a hole that the new entry will fit into, and copy
430 * the first part of the entries, the new entry, and the last part of
435 xfs_dir2_sf_addname_hard(
436 xfs_da_args_t *args, /* operation arguments */
437 int objchange, /* changing inode number size */
438 int new_isize) /* new directory size */
440 int add_datasize; /* data size need for new ent */
441 char *buf; /* buffer for old */
442 xfs_inode_t *dp; /* incore directory inode */
443 int eof; /* reached end of old dir */
444 int nbytes; /* temp for byte copies */
445 xfs_dir2_data_aoff_t new_offset; /* next offset value */
446 xfs_dir2_data_aoff_t offset; /* current offset value */
447 int old_isize; /* previous di_size */
448 xfs_dir2_sf_entry_t *oldsfep; /* entry in original dir */
449 xfs_dir2_sf_hdr_t *oldsfp; /* original shortform dir */
450 xfs_dir2_sf_entry_t *sfep; /* entry in new dir */
451 xfs_dir2_sf_hdr_t *sfp; /* new shortform dir */
454 * Copy the old directory to the stack buffer.
458 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
459 old_isize = (int)dp->i_d.di_size;
460 buf = kmem_alloc(old_isize, KM_SLEEP);
461 oldsfp = (xfs_dir2_sf_hdr_t *)buf;
462 memcpy(oldsfp, sfp, old_isize);
464 * Loop over the old directory finding the place we're going
465 * to insert the new entry.
466 * If it's going to end up at the end then oldsfep will point there.
468 for (offset = dp->d_ops->data_first_offset,
469 oldsfep = xfs_dir2_sf_firstentry(oldsfp),
470 add_datasize = dp->d_ops->data_entsize(args->namelen),
471 eof = (char *)oldsfep == &buf[old_isize];
473 offset = new_offset + dp->d_ops->data_entsize(oldsfep->namelen),
474 oldsfep = dp->d_ops->sf_nextentry(oldsfp, oldsfep),
475 eof = (char *)oldsfep == &buf[old_isize]) {
476 new_offset = xfs_dir2_sf_get_offset(oldsfep);
477 if (offset + add_datasize <= new_offset)
481 * Get rid of the old directory, then allocate space for
482 * the new one. We do this so xfs_idata_realloc won't copy
485 xfs_idata_realloc(dp, -old_isize, XFS_DATA_FORK);
486 xfs_idata_realloc(dp, new_isize, XFS_DATA_FORK);
488 * Reset the pointer since the buffer was reallocated.
490 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
492 * Copy the first part of the directory, including the header.
494 nbytes = (int)((char *)oldsfep - (char *)oldsfp);
495 memcpy(sfp, oldsfp, nbytes);
496 sfep = (xfs_dir2_sf_entry_t *)((char *)sfp + nbytes);
498 * Fill in the new entry, and update the header counts.
500 sfep->namelen = args->namelen;
501 xfs_dir2_sf_put_offset(sfep, offset);
502 memcpy(sfep->name, args->name, sfep->namelen);
503 dp->d_ops->sf_put_ino(sfp, sfep, args->inumber);
504 dp->d_ops->sf_put_ftype(sfep, args->filetype);
506 if (args->inumber > XFS_DIR2_MAX_SHORT_INUM && !objchange)
509 * If there's more left to copy, do that.
512 sfep = dp->d_ops->sf_nextentry(sfp, sfep);
513 memcpy(sfep, oldsfep, old_isize - nbytes);
516 dp->i_d.di_size = new_isize;
517 xfs_dir2_sf_check(args);
521 * Decide if the new entry will fit at all.
522 * If it will fit, pick between adding the new entry to the end (easy)
523 * or somewhere else (hard).
524 * Return 0 (won't fit), 1 (easy), 2 (hard).
527 static int /* pick result */
528 xfs_dir2_sf_addname_pick(
529 xfs_da_args_t *args, /* operation arguments */
530 int objchange, /* inode # size changes */
531 xfs_dir2_sf_entry_t **sfepp, /* out(1): new entry ptr */
532 xfs_dir2_data_aoff_t *offsetp) /* out(1): new offset */
534 xfs_inode_t *dp; /* incore directory inode */
535 int holefit; /* found hole it will fit in */
536 int i; /* entry number */
537 xfs_dir2_data_aoff_t offset; /* data block offset */
538 xfs_dir2_sf_entry_t *sfep; /* shortform entry */
539 xfs_dir2_sf_hdr_t *sfp; /* shortform structure */
540 int size; /* entry's data size */
541 int used; /* data bytes used */
545 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
546 size = dp->d_ops->data_entsize(args->namelen);
547 offset = dp->d_ops->data_first_offset;
548 sfep = xfs_dir2_sf_firstentry(sfp);
551 * Loop over sf entries.
552 * Keep track of data offset and whether we've seen a place
553 * to insert the new entry.
555 for (i = 0; i < sfp->count; i++) {
557 holefit = offset + size <= xfs_dir2_sf_get_offset(sfep);
558 offset = xfs_dir2_sf_get_offset(sfep) +
559 dp->d_ops->data_entsize(sfep->namelen);
560 sfep = dp->d_ops->sf_nextentry(sfp, sfep);
563 * Calculate data bytes used excluding the new entry, if this
564 * was a data block (block form directory).
567 (sfp->count + 3) * (uint)sizeof(xfs_dir2_leaf_entry_t) +
568 (uint)sizeof(xfs_dir2_block_tail_t);
570 * If it won't fit in a block form then we can't insert it,
571 * we'll go back, convert to block, then try the insert and convert
574 if (used + (holefit ? 0 : size) > args->geo->blksize)
577 * If changing the inode number size, do it the hard way.
582 * If it won't fit at the end then do it the hard way (use the hole).
584 if (used + size > args->geo->blksize)
587 * Do it the easy way.
596 * Check consistency of shortform directory, assert if bad.
600 xfs_da_args_t *args) /* operation arguments */
602 xfs_inode_t *dp; /* incore directory inode */
603 int i; /* entry number */
604 int i8count; /* number of big inode#s */
605 xfs_ino_t ino; /* entry inode number */
606 int offset; /* data offset */
607 xfs_dir2_sf_entry_t *sfep; /* shortform dir entry */
608 xfs_dir2_sf_hdr_t *sfp; /* shortform structure */
612 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
613 offset = dp->d_ops->data_first_offset;
614 ino = dp->d_ops->sf_get_parent_ino(sfp);
615 i8count = ino > XFS_DIR2_MAX_SHORT_INUM;
617 for (i = 0, sfep = xfs_dir2_sf_firstentry(sfp);
619 i++, sfep = dp->d_ops->sf_nextentry(sfp, sfep)) {
620 ASSERT(xfs_dir2_sf_get_offset(sfep) >= offset);
621 ino = dp->d_ops->sf_get_ino(sfp, sfep);
622 i8count += ino > XFS_DIR2_MAX_SHORT_INUM;
624 xfs_dir2_sf_get_offset(sfep) +
625 dp->d_ops->data_entsize(sfep->namelen);
626 ASSERT(dp->d_ops->sf_get_ftype(sfep) < XFS_DIR3_FT_MAX);
628 ASSERT(i8count == sfp->i8count);
629 ASSERT((char *)sfep - (char *)sfp == dp->i_d.di_size);
631 (sfp->count + 2) * (uint)sizeof(xfs_dir2_leaf_entry_t) +
632 (uint)sizeof(xfs_dir2_block_tail_t) <= args->geo->blksize);
637 * Create a new (shortform) directory.
639 int /* error, always 0 */
641 xfs_da_args_t *args, /* operation arguments */
642 xfs_ino_t pino) /* parent inode number */
644 xfs_inode_t *dp; /* incore directory inode */
645 int i8count; /* parent inode is an 8-byte number */
646 xfs_dir2_sf_hdr_t *sfp; /* shortform structure */
647 int size; /* directory size */
649 trace_xfs_dir2_sf_create(args);
654 ASSERT(dp->i_d.di_size == 0);
656 * If it's currently a zero-length extent file,
657 * convert it to local format.
659 if (dp->i_d.di_format == XFS_DINODE_FMT_EXTENTS) {
660 dp->i_df.if_flags &= ~XFS_IFEXTENTS; /* just in case */
661 dp->i_d.di_format = XFS_DINODE_FMT_LOCAL;
662 xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE);
663 dp->i_df.if_flags |= XFS_IFINLINE;
665 ASSERT(dp->i_df.if_flags & XFS_IFINLINE);
666 ASSERT(dp->i_df.if_bytes == 0);
667 i8count = pino > XFS_DIR2_MAX_SHORT_INUM;
668 size = xfs_dir2_sf_hdr_size(i8count);
670 * Make a buffer for the data.
672 xfs_idata_realloc(dp, size, XFS_DATA_FORK);
674 * Fill in the header,
676 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
677 sfp->i8count = i8count;
679 * Now can put in the inode number, since i8count is set.
681 dp->d_ops->sf_put_parent_ino(sfp, pino);
683 dp->i_d.di_size = size;
684 xfs_dir2_sf_check(args);
685 xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE | XFS_ILOG_DDATA);
690 * Lookup an entry in a shortform directory.
691 * Returns EEXIST if found, ENOENT if not found.
695 xfs_da_args_t *args) /* operation arguments */
697 xfs_inode_t *dp; /* incore directory inode */
698 int i; /* entry index */
700 xfs_dir2_sf_entry_t *sfep; /* shortform directory entry */
701 xfs_dir2_sf_hdr_t *sfp; /* shortform structure */
702 enum xfs_dacmp cmp; /* comparison result */
703 xfs_dir2_sf_entry_t *ci_sfep; /* case-insens. entry */
705 trace_xfs_dir2_sf_lookup(args);
707 xfs_dir2_sf_check(args);
710 ASSERT(dp->i_df.if_flags & XFS_IFINLINE);
712 * Bail out if the directory is way too short.
714 if (dp->i_d.di_size < offsetof(xfs_dir2_sf_hdr_t, parent)) {
715 ASSERT(XFS_FORCED_SHUTDOWN(dp->i_mount));
718 ASSERT(dp->i_df.if_bytes == dp->i_d.di_size);
719 ASSERT(dp->i_df.if_u1.if_data != NULL);
720 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
721 ASSERT(dp->i_d.di_size >= xfs_dir2_sf_hdr_size(sfp->i8count));
725 if (args->namelen == 1 && args->name[0] == '.') {
726 args->inumber = dp->i_ino;
727 args->cmpresult = XFS_CMP_EXACT;
728 args->filetype = XFS_DIR3_FT_DIR;
732 * Special case for ..
734 if (args->namelen == 2 &&
735 args->name[0] == '.' && args->name[1] == '.') {
736 args->inumber = dp->d_ops->sf_get_parent_ino(sfp);
737 args->cmpresult = XFS_CMP_EXACT;
738 args->filetype = XFS_DIR3_FT_DIR;
742 * Loop over all the entries trying to match ours.
745 for (i = 0, sfep = xfs_dir2_sf_firstentry(sfp); i < sfp->count;
746 i++, sfep = dp->d_ops->sf_nextentry(sfp, sfep)) {
748 * Compare name and if it's an exact match, return the inode
749 * number. If it's the first case-insensitive match, store the
750 * inode number and continue looking for an exact match.
752 cmp = dp->i_mount->m_dirnameops->compname(args, sfep->name,
754 if (cmp != XFS_CMP_DIFFERENT && cmp != args->cmpresult) {
755 args->cmpresult = cmp;
756 args->inumber = dp->d_ops->sf_get_ino(sfp, sfep);
757 args->filetype = dp->d_ops->sf_get_ftype(sfep);
758 if (cmp == XFS_CMP_EXACT)
763 ASSERT(args->op_flags & XFS_DA_OP_OKNOENT);
765 * Here, we can only be doing a lookup (not a rename or replace).
766 * If a case-insensitive match was not found, return -ENOENT.
770 /* otherwise process the CI match as required by the caller */
771 error = xfs_dir_cilookup_result(args, ci_sfep->name, ci_sfep->namelen);
776 * Remove an entry from a shortform directory.
779 xfs_dir2_sf_removename(
782 int byteoff; /* offset of removed entry */
783 xfs_inode_t *dp; /* incore directory inode */
784 int entsize; /* this entry's size */
785 int i; /* shortform entry index */
786 int newsize; /* new inode size */
787 int oldsize; /* old inode size */
788 xfs_dir2_sf_entry_t *sfep; /* shortform directory entry */
789 xfs_dir2_sf_hdr_t *sfp; /* shortform structure */
791 trace_xfs_dir2_sf_removename(args);
795 ASSERT(dp->i_df.if_flags & XFS_IFINLINE);
796 oldsize = (int)dp->i_d.di_size;
798 * Bail out if the directory is way too short.
800 if (oldsize < offsetof(xfs_dir2_sf_hdr_t, parent)) {
801 ASSERT(XFS_FORCED_SHUTDOWN(dp->i_mount));
804 ASSERT(dp->i_df.if_bytes == oldsize);
805 ASSERT(dp->i_df.if_u1.if_data != NULL);
806 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
807 ASSERT(oldsize >= xfs_dir2_sf_hdr_size(sfp->i8count));
809 * Loop over the old directory entries.
810 * Find the one we're deleting.
812 for (i = 0, sfep = xfs_dir2_sf_firstentry(sfp); i < sfp->count;
813 i++, sfep = dp->d_ops->sf_nextentry(sfp, sfep)) {
814 if (xfs_da_compname(args, sfep->name, sfep->namelen) ==
816 ASSERT(dp->d_ops->sf_get_ino(sfp, sfep) ==
829 byteoff = (int)((char *)sfep - (char *)sfp);
830 entsize = dp->d_ops->sf_entsize(sfp, args->namelen);
831 newsize = oldsize - entsize;
833 * Copy the part if any after the removed entry, sliding it down.
835 if (byteoff + entsize < oldsize)
836 memmove((char *)sfp + byteoff, (char *)sfp + byteoff + entsize,
837 oldsize - (byteoff + entsize));
839 * Fix up the header and file size.
842 dp->i_d.di_size = newsize;
844 * Reallocate, making it smaller.
846 xfs_idata_realloc(dp, newsize - oldsize, XFS_DATA_FORK);
847 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
849 * Are we changing inode number size?
851 if (args->inumber > XFS_DIR2_MAX_SHORT_INUM) {
852 if (sfp->i8count == 1)
853 xfs_dir2_sf_toino4(args);
857 xfs_dir2_sf_check(args);
858 xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE | XFS_ILOG_DDATA);
863 * Replace the inode number of an entry in a shortform directory.
867 xfs_da_args_t *args) /* operation arguments */
869 xfs_inode_t *dp; /* incore directory inode */
870 int i; /* entry index */
871 xfs_ino_t ino=0; /* entry old inode number */
872 int i8elevated; /* sf_toino8 set i8count=1 */
873 xfs_dir2_sf_entry_t *sfep; /* shortform directory entry */
874 xfs_dir2_sf_hdr_t *sfp; /* shortform structure */
876 trace_xfs_dir2_sf_replace(args);
880 ASSERT(dp->i_df.if_flags & XFS_IFINLINE);
882 * Bail out if the shortform directory is way too small.
884 if (dp->i_d.di_size < offsetof(xfs_dir2_sf_hdr_t, parent)) {
885 ASSERT(XFS_FORCED_SHUTDOWN(dp->i_mount));
888 ASSERT(dp->i_df.if_bytes == dp->i_d.di_size);
889 ASSERT(dp->i_df.if_u1.if_data != NULL);
890 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
891 ASSERT(dp->i_d.di_size >= xfs_dir2_sf_hdr_size(sfp->i8count));
894 * New inode number is large, and need to convert to 8-byte inodes.
896 if (args->inumber > XFS_DIR2_MAX_SHORT_INUM && sfp->i8count == 0) {
897 int error; /* error return value */
898 int newsize; /* new inode size */
903 ((uint)sizeof(xfs_dir2_ino8_t) -
904 (uint)sizeof(xfs_dir2_ino4_t));
906 * Won't fit as shortform, convert to block then do replace.
908 if (newsize > XFS_IFORK_DSIZE(dp)) {
909 error = xfs_dir2_sf_to_block(args);
913 return xfs_dir2_block_replace(args);
916 * Still fits, convert to 8-byte now.
918 xfs_dir2_sf_toino8(args);
920 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
924 ASSERT(args->namelen != 1 || args->name[0] != '.');
926 * Replace ..'s entry.
928 if (args->namelen == 2 &&
929 args->name[0] == '.' && args->name[1] == '.') {
930 ino = dp->d_ops->sf_get_parent_ino(sfp);
931 ASSERT(args->inumber != ino);
932 dp->d_ops->sf_put_parent_ino(sfp, args->inumber);
935 * Normal entry, look for the name.
938 for (i = 0, sfep = xfs_dir2_sf_firstentry(sfp); i < sfp->count;
939 i++, sfep = dp->d_ops->sf_nextentry(sfp, sfep)) {
940 if (xfs_da_compname(args, sfep->name, sfep->namelen) ==
942 ino = dp->d_ops->sf_get_ino(sfp, sfep);
943 ASSERT(args->inumber != ino);
944 dp->d_ops->sf_put_ino(sfp, sfep, args->inumber);
945 dp->d_ops->sf_put_ftype(sfep, args->filetype);
952 if (i == sfp->count) {
953 ASSERT(args->op_flags & XFS_DA_OP_OKNOENT);
955 xfs_dir2_sf_toino4(args);
960 * See if the old number was large, the new number is small.
962 if (ino > XFS_DIR2_MAX_SHORT_INUM &&
963 args->inumber <= XFS_DIR2_MAX_SHORT_INUM) {
965 * And the old count was one, so need to convert to small.
967 if (sfp->i8count == 1)
968 xfs_dir2_sf_toino4(args);
973 * See if the old number was small, the new number is large.
975 if (ino <= XFS_DIR2_MAX_SHORT_INUM &&
976 args->inumber > XFS_DIR2_MAX_SHORT_INUM) {
978 * add to the i8count unless we just converted to 8-byte
979 * inodes (which does an implied i8count = 1)
981 ASSERT(sfp->i8count != 0);
985 xfs_dir2_sf_check(args);
986 xfs_trans_log_inode(args->trans, dp, XFS_ILOG_DDATA);
991 * Convert from 8-byte inode numbers to 4-byte inode numbers.
992 * The last 8-byte inode number is gone, but the count is still 1.
996 xfs_da_args_t *args) /* operation arguments */
998 char *buf; /* old dir's buffer */
999 xfs_inode_t *dp; /* incore directory inode */
1000 int i; /* entry index */
1001 int newsize; /* new inode size */
1002 xfs_dir2_sf_entry_t *oldsfep; /* old sf entry */
1003 xfs_dir2_sf_hdr_t *oldsfp; /* old sf directory */
1004 int oldsize; /* old inode size */
1005 xfs_dir2_sf_entry_t *sfep; /* new sf entry */
1006 xfs_dir2_sf_hdr_t *sfp; /* new sf directory */
1008 trace_xfs_dir2_sf_toino4(args);
1013 * Copy the old directory to the buffer.
1014 * Then nuke it from the inode, and add the new buffer to the inode.
1015 * Don't want xfs_idata_realloc copying the data here.
1017 oldsize = dp->i_df.if_bytes;
1018 buf = kmem_alloc(oldsize, KM_SLEEP);
1019 oldsfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
1020 ASSERT(oldsfp->i8count == 1);
1021 memcpy(buf, oldsfp, oldsize);
1023 * Compute the new inode size.
1027 (oldsfp->count + 1) *
1028 ((uint)sizeof(xfs_dir2_ino8_t) - (uint)sizeof(xfs_dir2_ino4_t));
1029 xfs_idata_realloc(dp, -oldsize, XFS_DATA_FORK);
1030 xfs_idata_realloc(dp, newsize, XFS_DATA_FORK);
1032 * Reset our pointers, the data has moved.
1034 oldsfp = (xfs_dir2_sf_hdr_t *)buf;
1035 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
1037 * Fill in the new header.
1039 sfp->count = oldsfp->count;
1041 dp->d_ops->sf_put_parent_ino(sfp, dp->d_ops->sf_get_parent_ino(oldsfp));
1043 * Copy the entries field by field.
1045 for (i = 0, sfep = xfs_dir2_sf_firstentry(sfp),
1046 oldsfep = xfs_dir2_sf_firstentry(oldsfp);
1048 i++, sfep = dp->d_ops->sf_nextentry(sfp, sfep),
1049 oldsfep = dp->d_ops->sf_nextentry(oldsfp, oldsfep)) {
1050 sfep->namelen = oldsfep->namelen;
1051 sfep->offset = oldsfep->offset;
1052 memcpy(sfep->name, oldsfep->name, sfep->namelen);
1053 dp->d_ops->sf_put_ino(sfp, sfep,
1054 dp->d_ops->sf_get_ino(oldsfp, oldsfep));
1055 dp->d_ops->sf_put_ftype(sfep, dp->d_ops->sf_get_ftype(oldsfep));
1058 * Clean up the inode.
1061 dp->i_d.di_size = newsize;
1062 xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE | XFS_ILOG_DDATA);
1066 * Convert existing entries from 4-byte inode numbers to 8-byte inode numbers.
1067 * The new entry w/ an 8-byte inode number is not there yet; we leave with
1068 * i8count set to 1, but no corresponding 8-byte entry.
1072 xfs_da_args_t *args) /* operation arguments */
1074 char *buf; /* old dir's buffer */
1075 xfs_inode_t *dp; /* incore directory inode */
1076 int i; /* entry index */
1077 int newsize; /* new inode size */
1078 xfs_dir2_sf_entry_t *oldsfep; /* old sf entry */
1079 xfs_dir2_sf_hdr_t *oldsfp; /* old sf directory */
1080 int oldsize; /* old inode size */
1081 xfs_dir2_sf_entry_t *sfep; /* new sf entry */
1082 xfs_dir2_sf_hdr_t *sfp; /* new sf directory */
1084 trace_xfs_dir2_sf_toino8(args);
1089 * Copy the old directory to the buffer.
1090 * Then nuke it from the inode, and add the new buffer to the inode.
1091 * Don't want xfs_idata_realloc copying the data here.
1093 oldsize = dp->i_df.if_bytes;
1094 buf = kmem_alloc(oldsize, KM_SLEEP);
1095 oldsfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
1096 ASSERT(oldsfp->i8count == 0);
1097 memcpy(buf, oldsfp, oldsize);
1099 * Compute the new inode size (nb: entry count + 1 for parent)
1103 (oldsfp->count + 1) *
1104 ((uint)sizeof(xfs_dir2_ino8_t) - (uint)sizeof(xfs_dir2_ino4_t));
1105 xfs_idata_realloc(dp, -oldsize, XFS_DATA_FORK);
1106 xfs_idata_realloc(dp, newsize, XFS_DATA_FORK);
1108 * Reset our pointers, the data has moved.
1110 oldsfp = (xfs_dir2_sf_hdr_t *)buf;
1111 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
1113 * Fill in the new header.
1115 sfp->count = oldsfp->count;
1117 dp->d_ops->sf_put_parent_ino(sfp, dp->d_ops->sf_get_parent_ino(oldsfp));
1119 * Copy the entries field by field.
1121 for (i = 0, sfep = xfs_dir2_sf_firstentry(sfp),
1122 oldsfep = xfs_dir2_sf_firstentry(oldsfp);
1124 i++, sfep = dp->d_ops->sf_nextentry(sfp, sfep),
1125 oldsfep = dp->d_ops->sf_nextentry(oldsfp, oldsfep)) {
1126 sfep->namelen = oldsfep->namelen;
1127 sfep->offset = oldsfep->offset;
1128 memcpy(sfep->name, oldsfep->name, sfep->namelen);
1129 dp->d_ops->sf_put_ino(sfp, sfep,
1130 dp->d_ops->sf_get_ino(oldsfp, oldsfep));
1131 dp->d_ops->sf_put_ftype(sfep, dp->d_ops->sf_get_ftype(oldsfep));
1134 * Clean up the inode.
1137 dp->i_d.di_size = newsize;
1138 xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE | XFS_ILOG_DDATA);