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 * 3 * sizeof(u8) + /* namelen + offset */
132 (i8count ? /* inumber */
133 (uint)sizeof(xfs_dir2_ino8_t) * count :
134 (uint)sizeof(xfs_dir2_ino4_t) * count);
135 if (size > XFS_IFORK_DSIZE(dp))
136 return size; /* size value is a failure */
139 * Create the output header, if it worked.
142 sfhp->i8count = i8count;
143 dp->d_ops->sf_put_parent_ino(sfhp, parent);
148 * Convert a block format directory to shortform.
149 * Caller has already checked that it will fit, and built us a header.
152 xfs_dir2_block_to_sf(
153 xfs_da_args_t *args, /* operation arguments */
155 int size, /* shortform directory size */
156 xfs_dir2_sf_hdr_t *sfhp) /* shortform directory hdr */
158 xfs_dir2_data_hdr_t *hdr; /* block header */
159 xfs_dir2_block_tail_t *btp; /* block tail pointer */
160 xfs_dir2_data_entry_t *dep; /* data entry pointer */
161 xfs_inode_t *dp; /* incore directory inode */
162 xfs_dir2_data_unused_t *dup; /* unused data pointer */
163 char *endptr; /* end of data entries */
164 int error; /* error return value */
165 int logflags; /* inode logging flags */
166 xfs_mount_t *mp; /* filesystem mount point */
167 char *ptr; /* current data pointer */
168 xfs_dir2_sf_entry_t *sfep; /* shortform entry */
169 xfs_dir2_sf_hdr_t *sfp; /* shortform directory header */
170 xfs_dir2_sf_hdr_t *dst; /* temporary data buffer */
172 trace_xfs_dir2_block_to_sf(args);
178 * allocate a temporary destination buffer the size of the inode
179 * to format the data into. Once we have formatted the data, we
180 * can free the block and copy the formatted data into the inode literal
183 dst = kmem_alloc(mp->m_sb.sb_inodesize, KM_SLEEP);
187 * Copy the header into the newly allocate local space.
189 sfp = (xfs_dir2_sf_hdr_t *)dst;
190 memcpy(sfp, sfhp, xfs_dir2_sf_hdr_size(sfhp->i8count));
193 * Set up to loop over the block's entries.
195 btp = xfs_dir2_block_tail_p(args->geo, hdr);
196 ptr = (char *)dp->d_ops->data_entry_p(hdr);
197 endptr = (char *)xfs_dir2_block_leaf_p(btp);
198 sfep = xfs_dir2_sf_firstentry(sfp);
200 * Loop over the active and unused entries.
201 * Stop when we reach the leaf/tail portion of the block.
203 while (ptr < endptr) {
205 * If it's unused, just skip over it.
207 dup = (xfs_dir2_data_unused_t *)ptr;
208 if (be16_to_cpu(dup->freetag) == XFS_DIR2_DATA_FREE_TAG) {
209 ptr += be16_to_cpu(dup->length);
212 dep = (xfs_dir2_data_entry_t *)ptr;
216 if (dep->namelen == 1 && dep->name[0] == '.')
217 ASSERT(be64_to_cpu(dep->inumber) == dp->i_ino);
219 * Skip .., but make sure the inode number is right.
221 else if (dep->namelen == 2 &&
222 dep->name[0] == '.' && dep->name[1] == '.')
223 ASSERT(be64_to_cpu(dep->inumber) ==
224 dp->d_ops->sf_get_parent_ino(sfp));
226 * Normal entry, copy it into shortform.
229 sfep->namelen = dep->namelen;
230 xfs_dir2_sf_put_offset(sfep,
231 (xfs_dir2_data_aoff_t)
232 ((char *)dep - (char *)hdr));
233 memcpy(sfep->name, dep->name, dep->namelen);
234 dp->d_ops->sf_put_ino(sfp, sfep,
235 be64_to_cpu(dep->inumber));
236 dp->d_ops->sf_put_ftype(sfep,
237 dp->d_ops->data_get_ftype(dep));
239 sfep = dp->d_ops->sf_nextentry(sfp, sfep);
241 ptr += dp->d_ops->data_entsize(dep->namelen);
243 ASSERT((char *)sfep - (char *)sfp == size);
245 /* now we are done with the block, we can shrink the inode */
246 logflags = XFS_ILOG_CORE;
247 error = xfs_dir2_shrink_inode(args, args->geo->datablk, bp);
249 ASSERT(error != -ENOSPC);
254 * The buffer is now unconditionally gone, whether
255 * xfs_dir2_shrink_inode worked or not.
257 * Convert the inode to local format and copy the data in.
259 ASSERT(dp->i_df.if_bytes == 0);
260 xfs_init_local_fork(dp, XFS_DATA_FORK, dst, size);
261 dp->i_d.di_format = XFS_DINODE_FMT_LOCAL;
262 dp->i_d.di_size = size;
264 logflags |= XFS_ILOG_DDATA;
265 xfs_dir2_sf_check(args);
267 xfs_trans_log_inode(args->trans, dp, logflags);
273 * Add a name to a shortform directory.
274 * There are two algorithms, "easy" and "hard" which we decide on
275 * before changing anything.
276 * Convert to block form if necessary, if the new entry won't fit.
280 xfs_da_args_t *args) /* operation arguments */
282 xfs_inode_t *dp; /* incore directory inode */
283 int error; /* error return value */
284 int incr_isize; /* total change in size */
285 int new_isize; /* di_size after adding name */
286 int objchange; /* changing to 8-byte inodes */
287 xfs_dir2_data_aoff_t offset = 0; /* offset for new entry */
288 int pick; /* which algorithm to use */
289 xfs_dir2_sf_hdr_t *sfp; /* shortform structure */
290 xfs_dir2_sf_entry_t *sfep = NULL; /* shortform entry */
292 trace_xfs_dir2_sf_addname(args);
294 ASSERT(xfs_dir2_sf_lookup(args) == -ENOENT);
296 ASSERT(dp->i_df.if_flags & XFS_IFINLINE);
298 * Make sure the shortform value has some of its header.
300 if (dp->i_d.di_size < offsetof(xfs_dir2_sf_hdr_t, parent)) {
301 ASSERT(XFS_FORCED_SHUTDOWN(dp->i_mount));
304 ASSERT(dp->i_df.if_bytes == dp->i_d.di_size);
305 ASSERT(dp->i_df.if_u1.if_data != NULL);
306 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
307 ASSERT(dp->i_d.di_size >= xfs_dir2_sf_hdr_size(sfp->i8count));
309 * Compute entry (and change in) size.
311 incr_isize = dp->d_ops->sf_entsize(sfp, args->namelen);
315 * Do we have to change to 8 byte inodes?
317 if (args->inumber > XFS_DIR2_MAX_SHORT_INUM && sfp->i8count == 0) {
319 * Yes, adjust the inode size. old count + (parent + new)
323 ((uint)sizeof(xfs_dir2_ino8_t) -
324 (uint)sizeof(xfs_dir2_ino4_t));
328 new_isize = (int)dp->i_d.di_size + incr_isize;
330 * Won't fit as shortform any more (due to size),
331 * or the pick routine says it won't (due to offset values).
333 if (new_isize > XFS_IFORK_DSIZE(dp) ||
335 xfs_dir2_sf_addname_pick(args, objchange, &sfep, &offset)) == 0) {
337 * Just checking or no space reservation, it doesn't fit.
339 if ((args->op_flags & XFS_DA_OP_JUSTCHECK) || args->total == 0)
342 * Convert to block form then add the name.
344 error = xfs_dir2_sf_to_block(args);
347 return xfs_dir2_block_addname(args);
350 * Just checking, it fits.
352 if (args->op_flags & XFS_DA_OP_JUSTCHECK)
355 * Do it the easy way - just add it at the end.
358 xfs_dir2_sf_addname_easy(args, sfep, offset, new_isize);
360 * Do it the hard way - look for a place to insert the new entry.
361 * Convert to 8 byte inode numbers first if necessary.
366 xfs_dir2_sf_toino8(args);
367 xfs_dir2_sf_addname_hard(args, objchange, new_isize);
369 xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE | XFS_ILOG_DDATA);
374 * Add the new entry the "easy" way.
375 * This is copying the old directory and adding the new entry at the end.
376 * Since it's sorted by "offset" we need room after the last offset
377 * that's already there, and then room to convert to a block directory.
378 * This is already checked by the pick routine.
381 xfs_dir2_sf_addname_easy(
382 xfs_da_args_t *args, /* operation arguments */
383 xfs_dir2_sf_entry_t *sfep, /* pointer to new entry */
384 xfs_dir2_data_aoff_t offset, /* offset to use for new ent */
385 int new_isize) /* new directory size */
387 int byteoff; /* byte offset in sf dir */
388 xfs_inode_t *dp; /* incore directory inode */
389 xfs_dir2_sf_hdr_t *sfp; /* shortform structure */
393 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
394 byteoff = (int)((char *)sfep - (char *)sfp);
396 * Grow the in-inode space.
398 xfs_idata_realloc(dp, dp->d_ops->sf_entsize(sfp, args->namelen),
401 * Need to set up again due to realloc of the inode data.
403 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
404 sfep = (xfs_dir2_sf_entry_t *)((char *)sfp + byteoff);
406 * Fill in the new entry.
408 sfep->namelen = args->namelen;
409 xfs_dir2_sf_put_offset(sfep, offset);
410 memcpy(sfep->name, args->name, sfep->namelen);
411 dp->d_ops->sf_put_ino(sfp, sfep, args->inumber);
412 dp->d_ops->sf_put_ftype(sfep, args->filetype);
415 * Update the header and inode.
418 if (args->inumber > XFS_DIR2_MAX_SHORT_INUM)
420 dp->i_d.di_size = new_isize;
421 xfs_dir2_sf_check(args);
425 * Add the new entry the "hard" way.
426 * The caller has already converted to 8 byte inode numbers if necessary,
427 * in which case we need to leave the i8count at 1.
428 * Find a hole that the new entry will fit into, and copy
429 * the first part of the entries, the new entry, and the last part of
434 xfs_dir2_sf_addname_hard(
435 xfs_da_args_t *args, /* operation arguments */
436 int objchange, /* changing inode number size */
437 int new_isize) /* new directory size */
439 int add_datasize; /* data size need for new ent */
440 char *buf; /* buffer for old */
441 xfs_inode_t *dp; /* incore directory inode */
442 int eof; /* reached end of old dir */
443 int nbytes; /* temp for byte copies */
444 xfs_dir2_data_aoff_t new_offset; /* next offset value */
445 xfs_dir2_data_aoff_t offset; /* current offset value */
446 int old_isize; /* previous di_size */
447 xfs_dir2_sf_entry_t *oldsfep; /* entry in original dir */
448 xfs_dir2_sf_hdr_t *oldsfp; /* original shortform dir */
449 xfs_dir2_sf_entry_t *sfep; /* entry in new dir */
450 xfs_dir2_sf_hdr_t *sfp; /* new shortform dir */
453 * Copy the old directory to the stack buffer.
457 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
458 old_isize = (int)dp->i_d.di_size;
459 buf = kmem_alloc(old_isize, KM_SLEEP);
460 oldsfp = (xfs_dir2_sf_hdr_t *)buf;
461 memcpy(oldsfp, sfp, old_isize);
463 * Loop over the old directory finding the place we're going
464 * to insert the new entry.
465 * If it's going to end up at the end then oldsfep will point there.
467 for (offset = dp->d_ops->data_first_offset,
468 oldsfep = xfs_dir2_sf_firstentry(oldsfp),
469 add_datasize = dp->d_ops->data_entsize(args->namelen),
470 eof = (char *)oldsfep == &buf[old_isize];
472 offset = new_offset + dp->d_ops->data_entsize(oldsfep->namelen),
473 oldsfep = dp->d_ops->sf_nextentry(oldsfp, oldsfep),
474 eof = (char *)oldsfep == &buf[old_isize]) {
475 new_offset = xfs_dir2_sf_get_offset(oldsfep);
476 if (offset + add_datasize <= new_offset)
480 * Get rid of the old directory, then allocate space for
481 * the new one. We do this so xfs_idata_realloc won't copy
484 xfs_idata_realloc(dp, -old_isize, XFS_DATA_FORK);
485 xfs_idata_realloc(dp, new_isize, XFS_DATA_FORK);
487 * Reset the pointer since the buffer was reallocated.
489 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
491 * Copy the first part of the directory, including the header.
493 nbytes = (int)((char *)oldsfep - (char *)oldsfp);
494 memcpy(sfp, oldsfp, nbytes);
495 sfep = (xfs_dir2_sf_entry_t *)((char *)sfp + nbytes);
497 * Fill in the new entry, and update the header counts.
499 sfep->namelen = args->namelen;
500 xfs_dir2_sf_put_offset(sfep, offset);
501 memcpy(sfep->name, args->name, sfep->namelen);
502 dp->d_ops->sf_put_ino(sfp, sfep, args->inumber);
503 dp->d_ops->sf_put_ftype(sfep, args->filetype);
505 if (args->inumber > XFS_DIR2_MAX_SHORT_INUM && !objchange)
508 * If there's more left to copy, do that.
511 sfep = dp->d_ops->sf_nextentry(sfp, sfep);
512 memcpy(sfep, oldsfep, old_isize - nbytes);
515 dp->i_d.di_size = new_isize;
516 xfs_dir2_sf_check(args);
520 * Decide if the new entry will fit at all.
521 * If it will fit, pick between adding the new entry to the end (easy)
522 * or somewhere else (hard).
523 * Return 0 (won't fit), 1 (easy), 2 (hard).
526 static int /* pick result */
527 xfs_dir2_sf_addname_pick(
528 xfs_da_args_t *args, /* operation arguments */
529 int objchange, /* inode # size changes */
530 xfs_dir2_sf_entry_t **sfepp, /* out(1): new entry ptr */
531 xfs_dir2_data_aoff_t *offsetp) /* out(1): new offset */
533 xfs_inode_t *dp; /* incore directory inode */
534 int holefit; /* found hole it will fit in */
535 int i; /* entry number */
536 xfs_dir2_data_aoff_t offset; /* data block offset */
537 xfs_dir2_sf_entry_t *sfep; /* shortform entry */
538 xfs_dir2_sf_hdr_t *sfp; /* shortform structure */
539 int size; /* entry's data size */
540 int used; /* data bytes used */
544 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
545 size = dp->d_ops->data_entsize(args->namelen);
546 offset = dp->d_ops->data_first_offset;
547 sfep = xfs_dir2_sf_firstentry(sfp);
550 * Loop over sf entries.
551 * Keep track of data offset and whether we've seen a place
552 * to insert the new entry.
554 for (i = 0; i < sfp->count; i++) {
556 holefit = offset + size <= xfs_dir2_sf_get_offset(sfep);
557 offset = xfs_dir2_sf_get_offset(sfep) +
558 dp->d_ops->data_entsize(sfep->namelen);
559 sfep = dp->d_ops->sf_nextentry(sfp, sfep);
562 * Calculate data bytes used excluding the new entry, if this
563 * was a data block (block form directory).
566 (sfp->count + 3) * (uint)sizeof(xfs_dir2_leaf_entry_t) +
567 (uint)sizeof(xfs_dir2_block_tail_t);
569 * If it won't fit in a block form then we can't insert it,
570 * we'll go back, convert to block, then try the insert and convert
573 if (used + (holefit ? 0 : size) > args->geo->blksize)
576 * If changing the inode number size, do it the hard way.
581 * If it won't fit at the end then do it the hard way (use the hole).
583 if (used + size > args->geo->blksize)
586 * Do it the easy way.
595 * Check consistency of shortform directory, assert if bad.
599 xfs_da_args_t *args) /* operation arguments */
601 xfs_inode_t *dp; /* incore directory inode */
602 int i; /* entry number */
603 int i8count; /* number of big inode#s */
604 xfs_ino_t ino; /* entry inode number */
605 int offset; /* data offset */
606 xfs_dir2_sf_entry_t *sfep; /* shortform dir entry */
607 xfs_dir2_sf_hdr_t *sfp; /* shortform structure */
611 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
612 offset = dp->d_ops->data_first_offset;
613 ino = dp->d_ops->sf_get_parent_ino(sfp);
614 i8count = ino > XFS_DIR2_MAX_SHORT_INUM;
616 for (i = 0, sfep = xfs_dir2_sf_firstentry(sfp);
618 i++, sfep = dp->d_ops->sf_nextentry(sfp, sfep)) {
619 ASSERT(xfs_dir2_sf_get_offset(sfep) >= offset);
620 ino = dp->d_ops->sf_get_ino(sfp, sfep);
621 i8count += ino > XFS_DIR2_MAX_SHORT_INUM;
623 xfs_dir2_sf_get_offset(sfep) +
624 dp->d_ops->data_entsize(sfep->namelen);
625 ASSERT(dp->d_ops->sf_get_ftype(sfep) < XFS_DIR3_FT_MAX);
627 ASSERT(i8count == sfp->i8count);
628 ASSERT((char *)sfep - (char *)sfp == dp->i_d.di_size);
630 (sfp->count + 2) * (uint)sizeof(xfs_dir2_leaf_entry_t) +
631 (uint)sizeof(xfs_dir2_block_tail_t) <= args->geo->blksize);
636 * Create a new (shortform) directory.
638 int /* error, always 0 */
640 xfs_da_args_t *args, /* operation arguments */
641 xfs_ino_t pino) /* parent inode number */
643 xfs_inode_t *dp; /* incore directory inode */
644 int i8count; /* parent inode is an 8-byte number */
645 xfs_dir2_sf_hdr_t *sfp; /* shortform structure */
646 int size; /* directory size */
648 trace_xfs_dir2_sf_create(args);
653 ASSERT(dp->i_d.di_size == 0);
655 * If it's currently a zero-length extent file,
656 * convert it to local format.
658 if (dp->i_d.di_format == XFS_DINODE_FMT_EXTENTS) {
659 dp->i_df.if_flags &= ~XFS_IFEXTENTS; /* just in case */
660 dp->i_d.di_format = XFS_DINODE_FMT_LOCAL;
661 xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE);
662 dp->i_df.if_flags |= XFS_IFINLINE;
664 ASSERT(dp->i_df.if_flags & XFS_IFINLINE);
665 ASSERT(dp->i_df.if_bytes == 0);
666 i8count = pino > XFS_DIR2_MAX_SHORT_INUM;
667 size = xfs_dir2_sf_hdr_size(i8count);
669 * Make a buffer for the data.
671 xfs_idata_realloc(dp, size, XFS_DATA_FORK);
673 * Fill in the header,
675 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
676 sfp->i8count = i8count;
678 * Now can put in the inode number, since i8count is set.
680 dp->d_ops->sf_put_parent_ino(sfp, pino);
682 dp->i_d.di_size = size;
683 xfs_dir2_sf_check(args);
684 xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE | XFS_ILOG_DDATA);
689 * Lookup an entry in a shortform directory.
690 * Returns EEXIST if found, ENOENT if not found.
694 xfs_da_args_t *args) /* operation arguments */
696 xfs_inode_t *dp; /* incore directory inode */
697 int i; /* entry index */
699 xfs_dir2_sf_entry_t *sfep; /* shortform directory entry */
700 xfs_dir2_sf_hdr_t *sfp; /* shortform structure */
701 enum xfs_dacmp cmp; /* comparison result */
702 xfs_dir2_sf_entry_t *ci_sfep; /* case-insens. entry */
704 trace_xfs_dir2_sf_lookup(args);
706 xfs_dir2_sf_check(args);
709 ASSERT(dp->i_df.if_flags & XFS_IFINLINE);
711 * Bail out if the directory is way too short.
713 if (dp->i_d.di_size < offsetof(xfs_dir2_sf_hdr_t, parent)) {
714 ASSERT(XFS_FORCED_SHUTDOWN(dp->i_mount));
717 ASSERT(dp->i_df.if_bytes == dp->i_d.di_size);
718 ASSERT(dp->i_df.if_u1.if_data != NULL);
719 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
720 ASSERT(dp->i_d.di_size >= xfs_dir2_sf_hdr_size(sfp->i8count));
724 if (args->namelen == 1 && args->name[0] == '.') {
725 args->inumber = dp->i_ino;
726 args->cmpresult = XFS_CMP_EXACT;
727 args->filetype = XFS_DIR3_FT_DIR;
731 * Special case for ..
733 if (args->namelen == 2 &&
734 args->name[0] == '.' && args->name[1] == '.') {
735 args->inumber = dp->d_ops->sf_get_parent_ino(sfp);
736 args->cmpresult = XFS_CMP_EXACT;
737 args->filetype = XFS_DIR3_FT_DIR;
741 * Loop over all the entries trying to match ours.
744 for (i = 0, sfep = xfs_dir2_sf_firstentry(sfp); i < sfp->count;
745 i++, sfep = dp->d_ops->sf_nextentry(sfp, sfep)) {
747 * Compare name and if it's an exact match, return the inode
748 * number. If it's the first case-insensitive match, store the
749 * inode number and continue looking for an exact match.
751 cmp = dp->i_mount->m_dirnameops->compname(args, sfep->name,
753 if (cmp != XFS_CMP_DIFFERENT && cmp != args->cmpresult) {
754 args->cmpresult = cmp;
755 args->inumber = dp->d_ops->sf_get_ino(sfp, sfep);
756 args->filetype = dp->d_ops->sf_get_ftype(sfep);
757 if (cmp == XFS_CMP_EXACT)
762 ASSERT(args->op_flags & XFS_DA_OP_OKNOENT);
764 * Here, we can only be doing a lookup (not a rename or replace).
765 * If a case-insensitive match was not found, return -ENOENT.
769 /* otherwise process the CI match as required by the caller */
770 error = xfs_dir_cilookup_result(args, ci_sfep->name, ci_sfep->namelen);
775 * Remove an entry from a shortform directory.
778 xfs_dir2_sf_removename(
781 int byteoff; /* offset of removed entry */
782 xfs_inode_t *dp; /* incore directory inode */
783 int entsize; /* this entry's size */
784 int i; /* shortform entry index */
785 int newsize; /* new inode size */
786 int oldsize; /* old inode size */
787 xfs_dir2_sf_entry_t *sfep; /* shortform directory entry */
788 xfs_dir2_sf_hdr_t *sfp; /* shortform structure */
790 trace_xfs_dir2_sf_removename(args);
794 ASSERT(dp->i_df.if_flags & XFS_IFINLINE);
795 oldsize = (int)dp->i_d.di_size;
797 * Bail out if the directory is way too short.
799 if (oldsize < offsetof(xfs_dir2_sf_hdr_t, parent)) {
800 ASSERT(XFS_FORCED_SHUTDOWN(dp->i_mount));
803 ASSERT(dp->i_df.if_bytes == oldsize);
804 ASSERT(dp->i_df.if_u1.if_data != NULL);
805 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
806 ASSERT(oldsize >= xfs_dir2_sf_hdr_size(sfp->i8count));
808 * Loop over the old directory entries.
809 * Find the one we're deleting.
811 for (i = 0, sfep = xfs_dir2_sf_firstentry(sfp); i < sfp->count;
812 i++, sfep = dp->d_ops->sf_nextentry(sfp, sfep)) {
813 if (xfs_da_compname(args, sfep->name, sfep->namelen) ==
815 ASSERT(dp->d_ops->sf_get_ino(sfp, sfep) ==
828 byteoff = (int)((char *)sfep - (char *)sfp);
829 entsize = dp->d_ops->sf_entsize(sfp, args->namelen);
830 newsize = oldsize - entsize;
832 * Copy the part if any after the removed entry, sliding it down.
834 if (byteoff + entsize < oldsize)
835 memmove((char *)sfp + byteoff, (char *)sfp + byteoff + entsize,
836 oldsize - (byteoff + entsize));
838 * Fix up the header and file size.
841 dp->i_d.di_size = newsize;
843 * Reallocate, making it smaller.
845 xfs_idata_realloc(dp, newsize - oldsize, XFS_DATA_FORK);
846 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
848 * Are we changing inode number size?
850 if (args->inumber > XFS_DIR2_MAX_SHORT_INUM) {
851 if (sfp->i8count == 1)
852 xfs_dir2_sf_toino4(args);
856 xfs_dir2_sf_check(args);
857 xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE | XFS_ILOG_DDATA);
862 * Replace the inode number of an entry in a shortform directory.
866 xfs_da_args_t *args) /* operation arguments */
868 xfs_inode_t *dp; /* incore directory inode */
869 int i; /* entry index */
870 xfs_ino_t ino=0; /* entry old inode number */
871 int i8elevated; /* sf_toino8 set i8count=1 */
872 xfs_dir2_sf_entry_t *sfep; /* shortform directory entry */
873 xfs_dir2_sf_hdr_t *sfp; /* shortform structure */
875 trace_xfs_dir2_sf_replace(args);
879 ASSERT(dp->i_df.if_flags & XFS_IFINLINE);
881 * Bail out if the shortform directory is way too small.
883 if (dp->i_d.di_size < offsetof(xfs_dir2_sf_hdr_t, parent)) {
884 ASSERT(XFS_FORCED_SHUTDOWN(dp->i_mount));
887 ASSERT(dp->i_df.if_bytes == dp->i_d.di_size);
888 ASSERT(dp->i_df.if_u1.if_data != NULL);
889 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
890 ASSERT(dp->i_d.di_size >= xfs_dir2_sf_hdr_size(sfp->i8count));
893 * New inode number is large, and need to convert to 8-byte inodes.
895 if (args->inumber > XFS_DIR2_MAX_SHORT_INUM && sfp->i8count == 0) {
896 int error; /* error return value */
897 int newsize; /* new inode size */
902 ((uint)sizeof(xfs_dir2_ino8_t) -
903 (uint)sizeof(xfs_dir2_ino4_t));
905 * Won't fit as shortform, convert to block then do replace.
907 if (newsize > XFS_IFORK_DSIZE(dp)) {
908 error = xfs_dir2_sf_to_block(args);
912 return xfs_dir2_block_replace(args);
915 * Still fits, convert to 8-byte now.
917 xfs_dir2_sf_toino8(args);
919 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
923 ASSERT(args->namelen != 1 || args->name[0] != '.');
925 * Replace ..'s entry.
927 if (args->namelen == 2 &&
928 args->name[0] == '.' && args->name[1] == '.') {
929 ino = dp->d_ops->sf_get_parent_ino(sfp);
930 ASSERT(args->inumber != ino);
931 dp->d_ops->sf_put_parent_ino(sfp, args->inumber);
934 * Normal entry, look for the name.
937 for (i = 0, sfep = xfs_dir2_sf_firstentry(sfp); i < sfp->count;
938 i++, sfep = dp->d_ops->sf_nextentry(sfp, sfep)) {
939 if (xfs_da_compname(args, sfep->name, sfep->namelen) ==
941 ino = dp->d_ops->sf_get_ino(sfp, sfep);
942 ASSERT(args->inumber != ino);
943 dp->d_ops->sf_put_ino(sfp, sfep, args->inumber);
944 dp->d_ops->sf_put_ftype(sfep, args->filetype);
951 if (i == sfp->count) {
952 ASSERT(args->op_flags & XFS_DA_OP_OKNOENT);
954 xfs_dir2_sf_toino4(args);
959 * See if the old number was large, the new number is small.
961 if (ino > XFS_DIR2_MAX_SHORT_INUM &&
962 args->inumber <= XFS_DIR2_MAX_SHORT_INUM) {
964 * And the old count was one, so need to convert to small.
966 if (sfp->i8count == 1)
967 xfs_dir2_sf_toino4(args);
972 * See if the old number was small, the new number is large.
974 if (ino <= XFS_DIR2_MAX_SHORT_INUM &&
975 args->inumber > XFS_DIR2_MAX_SHORT_INUM) {
977 * add to the i8count unless we just converted to 8-byte
978 * inodes (which does an implied i8count = 1)
980 ASSERT(sfp->i8count != 0);
984 xfs_dir2_sf_check(args);
985 xfs_trans_log_inode(args->trans, dp, XFS_ILOG_DDATA);
990 * Convert from 8-byte inode numbers to 4-byte inode numbers.
991 * The last 8-byte inode number is gone, but the count is still 1.
995 xfs_da_args_t *args) /* operation arguments */
997 char *buf; /* old dir's buffer */
998 xfs_inode_t *dp; /* incore directory inode */
999 int i; /* entry index */
1000 int newsize; /* new inode size */
1001 xfs_dir2_sf_entry_t *oldsfep; /* old sf entry */
1002 xfs_dir2_sf_hdr_t *oldsfp; /* old sf directory */
1003 int oldsize; /* old inode size */
1004 xfs_dir2_sf_entry_t *sfep; /* new sf entry */
1005 xfs_dir2_sf_hdr_t *sfp; /* new sf directory */
1007 trace_xfs_dir2_sf_toino4(args);
1012 * Copy the old directory to the buffer.
1013 * Then nuke it from the inode, and add the new buffer to the inode.
1014 * Don't want xfs_idata_realloc copying the data here.
1016 oldsize = dp->i_df.if_bytes;
1017 buf = kmem_alloc(oldsize, KM_SLEEP);
1018 oldsfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
1019 ASSERT(oldsfp->i8count == 1);
1020 memcpy(buf, oldsfp, oldsize);
1022 * Compute the new inode size.
1026 (oldsfp->count + 1) *
1027 ((uint)sizeof(xfs_dir2_ino8_t) - (uint)sizeof(xfs_dir2_ino4_t));
1028 xfs_idata_realloc(dp, -oldsize, XFS_DATA_FORK);
1029 xfs_idata_realloc(dp, newsize, XFS_DATA_FORK);
1031 * Reset our pointers, the data has moved.
1033 oldsfp = (xfs_dir2_sf_hdr_t *)buf;
1034 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
1036 * Fill in the new header.
1038 sfp->count = oldsfp->count;
1040 dp->d_ops->sf_put_parent_ino(sfp, dp->d_ops->sf_get_parent_ino(oldsfp));
1042 * Copy the entries field by field.
1044 for (i = 0, sfep = xfs_dir2_sf_firstentry(sfp),
1045 oldsfep = xfs_dir2_sf_firstentry(oldsfp);
1047 i++, sfep = dp->d_ops->sf_nextentry(sfp, sfep),
1048 oldsfep = dp->d_ops->sf_nextentry(oldsfp, oldsfep)) {
1049 sfep->namelen = oldsfep->namelen;
1050 memcpy(sfep->offset, oldsfep->offset, sizeof(sfep->offset));
1051 memcpy(sfep->name, oldsfep->name, sfep->namelen);
1052 dp->d_ops->sf_put_ino(sfp, sfep,
1053 dp->d_ops->sf_get_ino(oldsfp, oldsfep));
1054 dp->d_ops->sf_put_ftype(sfep, dp->d_ops->sf_get_ftype(oldsfep));
1057 * Clean up the inode.
1060 dp->i_d.di_size = newsize;
1061 xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE | XFS_ILOG_DDATA);
1065 * Convert existing entries from 4-byte inode numbers to 8-byte inode numbers.
1066 * The new entry w/ an 8-byte inode number is not there yet; we leave with
1067 * i8count set to 1, but no corresponding 8-byte entry.
1071 xfs_da_args_t *args) /* operation arguments */
1073 char *buf; /* old dir's buffer */
1074 xfs_inode_t *dp; /* incore directory inode */
1075 int i; /* entry index */
1076 int newsize; /* new inode size */
1077 xfs_dir2_sf_entry_t *oldsfep; /* old sf entry */
1078 xfs_dir2_sf_hdr_t *oldsfp; /* old sf directory */
1079 int oldsize; /* old inode size */
1080 xfs_dir2_sf_entry_t *sfep; /* new sf entry */
1081 xfs_dir2_sf_hdr_t *sfp; /* new sf directory */
1083 trace_xfs_dir2_sf_toino8(args);
1088 * Copy the old directory to the buffer.
1089 * Then nuke it from the inode, and add the new buffer to the inode.
1090 * Don't want xfs_idata_realloc copying the data here.
1092 oldsize = dp->i_df.if_bytes;
1093 buf = kmem_alloc(oldsize, KM_SLEEP);
1094 oldsfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
1095 ASSERT(oldsfp->i8count == 0);
1096 memcpy(buf, oldsfp, oldsize);
1098 * Compute the new inode size (nb: entry count + 1 for parent)
1102 (oldsfp->count + 1) *
1103 ((uint)sizeof(xfs_dir2_ino8_t) - (uint)sizeof(xfs_dir2_ino4_t));
1104 xfs_idata_realloc(dp, -oldsize, XFS_DATA_FORK);
1105 xfs_idata_realloc(dp, newsize, XFS_DATA_FORK);
1107 * Reset our pointers, the data has moved.
1109 oldsfp = (xfs_dir2_sf_hdr_t *)buf;
1110 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
1112 * Fill in the new header.
1114 sfp->count = oldsfp->count;
1116 dp->d_ops->sf_put_parent_ino(sfp, dp->d_ops->sf_get_parent_ino(oldsfp));
1118 * Copy the entries field by field.
1120 for (i = 0, sfep = xfs_dir2_sf_firstentry(sfp),
1121 oldsfep = xfs_dir2_sf_firstentry(oldsfp);
1123 i++, sfep = dp->d_ops->sf_nextentry(sfp, sfep),
1124 oldsfep = dp->d_ops->sf_nextentry(oldsfp, oldsfep)) {
1125 sfep->namelen = oldsfep->namelen;
1126 memcpy(sfep->offset, oldsfep->offset, sizeof(sfep->offset));
1127 memcpy(sfep->name, oldsfep->name, sfep->namelen);
1128 dp->d_ops->sf_put_ino(sfp, sfep,
1129 dp->d_ops->sf_get_ino(oldsfp, oldsfep));
1130 dp->d_ops->sf_put_ftype(sfep, dp->d_ops->sf_get_ftype(oldsfep));
1133 * Clean up the inode.
1136 dp->i_d.di_size = newsize;
1137 xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE | XFS_ILOG_DDATA);