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"
25 #include "xfs_mount.h"
26 #include "xfs_da_format.h"
27 #include "xfs_da_btree.h"
28 #include "xfs_inode.h"
29 #include "xfs_trans.h"
30 #include "xfs_inode_item.h"
31 #include "xfs_error.h"
33 #include "xfs_dir2_priv.h"
34 #include "xfs_trace.h"
35 #include "xfs_dinode.h"
38 * Prototypes for internal functions.
40 static void xfs_dir2_sf_addname_easy(xfs_da_args_t *args,
41 xfs_dir2_sf_entry_t *sfep,
42 xfs_dir2_data_aoff_t offset,
44 static void xfs_dir2_sf_addname_hard(xfs_da_args_t *args, int objchange,
46 static int xfs_dir2_sf_addname_pick(xfs_da_args_t *args, int objchange,
47 xfs_dir2_sf_entry_t **sfepp,
48 xfs_dir2_data_aoff_t *offsetp);
50 static void xfs_dir2_sf_check(xfs_da_args_t *args);
52 #define xfs_dir2_sf_check(args)
55 static void xfs_dir2_sf_toino4(xfs_da_args_t *args);
56 static void xfs_dir2_sf_toino8(xfs_da_args_t *args);
59 * Given a block directory (dp/block), calculate its size as a shortform (sf)
60 * directory and a header for the sf directory, if it will fit it the
61 * space currently present in the inode. If it won't fit, the output
62 * size is too big (but not accurate).
64 int /* size for sf form */
65 xfs_dir2_block_sfsize(
66 xfs_inode_t *dp, /* incore inode pointer */
67 xfs_dir2_data_hdr_t *hdr, /* block directory data */
68 xfs_dir2_sf_hdr_t *sfhp) /* output: header for sf form */
70 xfs_dir2_dataptr_t addr; /* data entry address */
71 xfs_dir2_leaf_entry_t *blp; /* leaf area of the block */
72 xfs_dir2_block_tail_t *btp; /* tail area of the block */
73 int count; /* shortform entry count */
74 xfs_dir2_data_entry_t *dep; /* data entry in the block */
75 int i; /* block entry index */
76 int i8count; /* count of big-inode entries */
77 int isdot; /* entry is "." */
78 int isdotdot; /* entry is ".." */
79 xfs_mount_t *mp; /* mount structure pointer */
80 int namelen; /* total name bytes */
81 xfs_ino_t parent = 0; /* parent inode number */
82 int size=0; /* total computed size */
84 struct xfs_da_geometry *geo;
90 * if there is a filetype field, add the extra byte to the namelen
91 * for each entry that we see.
93 has_ftype = xfs_sb_version_hasftype(&mp->m_sb) ? 1 : 0;
95 count = i8count = namelen = 0;
96 btp = xfs_dir2_block_tail_p(geo, hdr);
97 blp = xfs_dir2_block_leaf_p(btp);
100 * Iterate over the block's data entries by using the leaf pointers.
102 for (i = 0; i < be32_to_cpu(btp->count); i++) {
103 if ((addr = be32_to_cpu(blp[i].address)) == XFS_DIR2_NULL_DATAPTR)
106 * Calculate the pointer to the entry at hand.
108 dep = (xfs_dir2_data_entry_t *)((char *)hdr +
109 xfs_dir2_dataptr_to_off(geo, addr));
111 * Detect . and .., so we can special-case them.
112 * . is not included in sf directories.
113 * .. is included by just the parent inode number.
115 isdot = dep->namelen == 1 && dep->name[0] == '.';
118 dep->name[0] == '.' && dep->name[1] == '.';
121 i8count += be64_to_cpu(dep->inumber) > XFS_DIR2_MAX_SHORT_INUM;
123 /* take into account the file type field */
124 if (!isdot && !isdotdot) {
126 namelen += dep->namelen + has_ftype;
128 parent = be64_to_cpu(dep->inumber);
130 * Calculate the new size, see if we should give up yet.
132 size = xfs_dir2_sf_hdr_size(i8count) + /* header */
133 count + /* namelen */
134 count * (uint)sizeof(xfs_dir2_sf_off_t) + /* offset */
136 (i8count ? /* inumber */
137 (uint)sizeof(xfs_dir2_ino8_t) * count :
138 (uint)sizeof(xfs_dir2_ino4_t) * count);
139 if (size > XFS_IFORK_DSIZE(dp))
140 return size; /* size value is a failure */
143 * Create the output header, if it worked.
146 sfhp->i8count = i8count;
147 dp->d_ops->sf_put_parent_ino(sfhp, parent);
152 * Convert a block format directory to shortform.
153 * Caller has already checked that it will fit, and built us a header.
156 xfs_dir2_block_to_sf(
157 xfs_da_args_t *args, /* operation arguments */
159 int size, /* shortform directory size */
160 xfs_dir2_sf_hdr_t *sfhp) /* shortform directory hdr */
162 xfs_dir2_data_hdr_t *hdr; /* block header */
163 xfs_dir2_block_tail_t *btp; /* block tail pointer */
164 xfs_dir2_data_entry_t *dep; /* data entry pointer */
165 xfs_inode_t *dp; /* incore directory inode */
166 xfs_dir2_data_unused_t *dup; /* unused data pointer */
167 char *endptr; /* end of data entries */
168 int error; /* error return value */
169 int logflags; /* inode logging flags */
170 xfs_mount_t *mp; /* filesystem mount point */
171 char *ptr; /* current data pointer */
172 xfs_dir2_sf_entry_t *sfep; /* shortform entry */
173 xfs_dir2_sf_hdr_t *sfp; /* shortform directory header */
174 xfs_dir2_sf_hdr_t *dst; /* temporary data buffer */
176 trace_xfs_dir2_block_to_sf(args);
182 * allocate a temporary destination buffer the size of the inode
183 * to format the data into. Once we have formatted the data, we
184 * can free the block and copy the formatted data into the inode literal
187 dst = kmem_alloc(mp->m_sb.sb_inodesize, KM_SLEEP);
191 * Copy the header into the newly allocate local space.
193 sfp = (xfs_dir2_sf_hdr_t *)dst;
194 memcpy(sfp, sfhp, xfs_dir2_sf_hdr_size(sfhp->i8count));
197 * Set up to loop over the block's entries.
199 btp = xfs_dir2_block_tail_p(args->geo, hdr);
200 ptr = (char *)dp->d_ops->data_entry_p(hdr);
201 endptr = (char *)xfs_dir2_block_leaf_p(btp);
202 sfep = xfs_dir2_sf_firstentry(sfp);
204 * Loop over the active and unused entries.
205 * Stop when we reach the leaf/tail portion of the block.
207 while (ptr < endptr) {
209 * If it's unused, just skip over it.
211 dup = (xfs_dir2_data_unused_t *)ptr;
212 if (be16_to_cpu(dup->freetag) == XFS_DIR2_DATA_FREE_TAG) {
213 ptr += be16_to_cpu(dup->length);
216 dep = (xfs_dir2_data_entry_t *)ptr;
220 if (dep->namelen == 1 && dep->name[0] == '.')
221 ASSERT(be64_to_cpu(dep->inumber) == dp->i_ino);
223 * Skip .., but make sure the inode number is right.
225 else if (dep->namelen == 2 &&
226 dep->name[0] == '.' && dep->name[1] == '.')
227 ASSERT(be64_to_cpu(dep->inumber) ==
228 dp->d_ops->sf_get_parent_ino(sfp));
230 * Normal entry, copy it into shortform.
233 sfep->namelen = dep->namelen;
234 xfs_dir2_sf_put_offset(sfep,
235 (xfs_dir2_data_aoff_t)
236 ((char *)dep - (char *)hdr));
237 memcpy(sfep->name, dep->name, dep->namelen);
238 dp->d_ops->sf_put_ino(sfp, sfep,
239 be64_to_cpu(dep->inumber));
240 dp->d_ops->sf_put_ftype(sfep,
241 dp->d_ops->data_get_ftype(dep));
243 sfep = dp->d_ops->sf_nextentry(sfp, sfep);
245 ptr += dp->d_ops->data_entsize(dep->namelen);
247 ASSERT((char *)sfep - (char *)sfp == size);
249 /* now we are done with the block, we can shrink the inode */
250 logflags = XFS_ILOG_CORE;
251 error = xfs_dir2_shrink_inode(args, args->geo->datablk, bp);
253 ASSERT(error != -ENOSPC);
258 * The buffer is now unconditionally gone, whether
259 * xfs_dir2_shrink_inode worked or not.
261 * Convert the inode to local format and copy the data in.
263 dp->i_df.if_flags &= ~XFS_IFEXTENTS;
264 dp->i_df.if_flags |= XFS_IFINLINE;
265 dp->i_d.di_format = XFS_DINODE_FMT_LOCAL;
266 ASSERT(dp->i_df.if_bytes == 0);
267 xfs_idata_realloc(dp, size, XFS_DATA_FORK);
269 logflags |= XFS_ILOG_DDATA;
270 memcpy(dp->i_df.if_u1.if_data, dst, size);
271 dp->i_d.di_size = size;
272 xfs_dir2_sf_check(args);
274 xfs_trans_log_inode(args->trans, dp, logflags);
280 * Add a name to a shortform directory.
281 * There are two algorithms, "easy" and "hard" which we decide on
282 * before changing anything.
283 * Convert to block form if necessary, if the new entry won't fit.
287 xfs_da_args_t *args) /* operation arguments */
289 xfs_inode_t *dp; /* incore directory inode */
290 int error; /* error return value */
291 int incr_isize; /* total change in size */
292 int new_isize; /* di_size after adding name */
293 int objchange; /* changing to 8-byte inodes */
294 xfs_dir2_data_aoff_t offset = 0; /* offset for new entry */
295 int pick; /* which algorithm to use */
296 xfs_dir2_sf_hdr_t *sfp; /* shortform structure */
297 xfs_dir2_sf_entry_t *sfep = NULL; /* shortform entry */
299 trace_xfs_dir2_sf_addname(args);
301 ASSERT(xfs_dir2_sf_lookup(args) == -ENOENT);
303 ASSERT(dp->i_df.if_flags & XFS_IFINLINE);
305 * Make sure the shortform value has some of its header.
307 if (dp->i_d.di_size < offsetof(xfs_dir2_sf_hdr_t, parent)) {
308 ASSERT(XFS_FORCED_SHUTDOWN(dp->i_mount));
311 ASSERT(dp->i_df.if_bytes == dp->i_d.di_size);
312 ASSERT(dp->i_df.if_u1.if_data != NULL);
313 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
314 ASSERT(dp->i_d.di_size >= xfs_dir2_sf_hdr_size(sfp->i8count));
316 * Compute entry (and change in) size.
318 incr_isize = dp->d_ops->sf_entsize(sfp, args->namelen);
322 * Do we have to change to 8 byte inodes?
324 if (args->inumber > XFS_DIR2_MAX_SHORT_INUM && sfp->i8count == 0) {
326 * Yes, adjust the inode size. old count + (parent + new)
330 ((uint)sizeof(xfs_dir2_ino8_t) -
331 (uint)sizeof(xfs_dir2_ino4_t));
335 new_isize = (int)dp->i_d.di_size + incr_isize;
337 * Won't fit as shortform any more (due to size),
338 * or the pick routine says it won't (due to offset values).
340 if (new_isize > XFS_IFORK_DSIZE(dp) ||
342 xfs_dir2_sf_addname_pick(args, objchange, &sfep, &offset)) == 0) {
344 * Just checking or no space reservation, it doesn't fit.
346 if ((args->op_flags & XFS_DA_OP_JUSTCHECK) || args->total == 0)
349 * Convert to block form then add the name.
351 error = xfs_dir2_sf_to_block(args);
354 return xfs_dir2_block_addname(args);
357 * Just checking, it fits.
359 if (args->op_flags & XFS_DA_OP_JUSTCHECK)
362 * Do it the easy way - just add it at the end.
365 xfs_dir2_sf_addname_easy(args, sfep, offset, new_isize);
367 * Do it the hard way - look for a place to insert the new entry.
368 * Convert to 8 byte inode numbers first if necessary.
373 xfs_dir2_sf_toino8(args);
374 xfs_dir2_sf_addname_hard(args, objchange, new_isize);
376 xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE | XFS_ILOG_DDATA);
381 * Add the new entry the "easy" way.
382 * This is copying the old directory and adding the new entry at the end.
383 * Since it's sorted by "offset" we need room after the last offset
384 * that's already there, and then room to convert to a block directory.
385 * This is already checked by the pick routine.
388 xfs_dir2_sf_addname_easy(
389 xfs_da_args_t *args, /* operation arguments */
390 xfs_dir2_sf_entry_t *sfep, /* pointer to new entry */
391 xfs_dir2_data_aoff_t offset, /* offset to use for new ent */
392 int new_isize) /* new directory size */
394 int byteoff; /* byte offset in sf dir */
395 xfs_inode_t *dp; /* incore directory inode */
396 xfs_dir2_sf_hdr_t *sfp; /* shortform structure */
400 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
401 byteoff = (int)((char *)sfep - (char *)sfp);
403 * Grow the in-inode space.
405 xfs_idata_realloc(dp, dp->d_ops->sf_entsize(sfp, args->namelen),
408 * Need to set up again due to realloc of the inode data.
410 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
411 sfep = (xfs_dir2_sf_entry_t *)((char *)sfp + byteoff);
413 * Fill in the new entry.
415 sfep->namelen = args->namelen;
416 xfs_dir2_sf_put_offset(sfep, offset);
417 memcpy(sfep->name, args->name, sfep->namelen);
418 dp->d_ops->sf_put_ino(sfp, sfep, args->inumber);
419 dp->d_ops->sf_put_ftype(sfep, args->filetype);
422 * Update the header and inode.
425 if (args->inumber > XFS_DIR2_MAX_SHORT_INUM)
427 dp->i_d.di_size = new_isize;
428 xfs_dir2_sf_check(args);
432 * Add the new entry the "hard" way.
433 * The caller has already converted to 8 byte inode numbers if necessary,
434 * in which case we need to leave the i8count at 1.
435 * Find a hole that the new entry will fit into, and copy
436 * the first part of the entries, the new entry, and the last part of
441 xfs_dir2_sf_addname_hard(
442 xfs_da_args_t *args, /* operation arguments */
443 int objchange, /* changing inode number size */
444 int new_isize) /* new directory size */
446 int add_datasize; /* data size need for new ent */
447 char *buf; /* buffer for old */
448 xfs_inode_t *dp; /* incore directory inode */
449 int eof; /* reached end of old dir */
450 int nbytes; /* temp for byte copies */
451 xfs_dir2_data_aoff_t new_offset; /* next offset value */
452 xfs_dir2_data_aoff_t offset; /* current offset value */
453 int old_isize; /* previous di_size */
454 xfs_dir2_sf_entry_t *oldsfep; /* entry in original dir */
455 xfs_dir2_sf_hdr_t *oldsfp; /* original shortform dir */
456 xfs_dir2_sf_entry_t *sfep; /* entry in new dir */
457 xfs_dir2_sf_hdr_t *sfp; /* new shortform dir */
458 struct xfs_mount *mp;
461 * Copy the old directory to the stack buffer.
466 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
467 old_isize = (int)dp->i_d.di_size;
468 buf = kmem_alloc(old_isize, KM_SLEEP);
469 oldsfp = (xfs_dir2_sf_hdr_t *)buf;
470 memcpy(oldsfp, sfp, old_isize);
472 * Loop over the old directory finding the place we're going
473 * to insert the new entry.
474 * If it's going to end up at the end then oldsfep will point there.
476 for (offset = dp->d_ops->data_first_offset,
477 oldsfep = xfs_dir2_sf_firstentry(oldsfp),
478 add_datasize = dp->d_ops->data_entsize(args->namelen),
479 eof = (char *)oldsfep == &buf[old_isize];
481 offset = new_offset + dp->d_ops->data_entsize(oldsfep->namelen),
482 oldsfep = dp->d_ops->sf_nextentry(oldsfp, oldsfep),
483 eof = (char *)oldsfep == &buf[old_isize]) {
484 new_offset = xfs_dir2_sf_get_offset(oldsfep);
485 if (offset + add_datasize <= new_offset)
489 * Get rid of the old directory, then allocate space for
490 * the new one. We do this so xfs_idata_realloc won't copy
493 xfs_idata_realloc(dp, -old_isize, XFS_DATA_FORK);
494 xfs_idata_realloc(dp, new_isize, XFS_DATA_FORK);
496 * Reset the pointer since the buffer was reallocated.
498 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
500 * Copy the first part of the directory, including the header.
502 nbytes = (int)((char *)oldsfep - (char *)oldsfp);
503 memcpy(sfp, oldsfp, nbytes);
504 sfep = (xfs_dir2_sf_entry_t *)((char *)sfp + nbytes);
506 * Fill in the new entry, and update the header counts.
508 sfep->namelen = args->namelen;
509 xfs_dir2_sf_put_offset(sfep, offset);
510 memcpy(sfep->name, args->name, sfep->namelen);
511 dp->d_ops->sf_put_ino(sfp, sfep, args->inumber);
512 dp->d_ops->sf_put_ftype(sfep, args->filetype);
514 if (args->inumber > XFS_DIR2_MAX_SHORT_INUM && !objchange)
517 * If there's more left to copy, do that.
520 sfep = dp->d_ops->sf_nextentry(sfp, sfep);
521 memcpy(sfep, oldsfep, old_isize - nbytes);
524 dp->i_d.di_size = new_isize;
525 xfs_dir2_sf_check(args);
529 * Decide if the new entry will fit at all.
530 * If it will fit, pick between adding the new entry to the end (easy)
531 * or somewhere else (hard).
532 * Return 0 (won't fit), 1 (easy), 2 (hard).
535 static int /* pick result */
536 xfs_dir2_sf_addname_pick(
537 xfs_da_args_t *args, /* operation arguments */
538 int objchange, /* inode # size changes */
539 xfs_dir2_sf_entry_t **sfepp, /* out(1): new entry ptr */
540 xfs_dir2_data_aoff_t *offsetp) /* out(1): new offset */
542 xfs_inode_t *dp; /* incore directory inode */
543 int holefit; /* found hole it will fit in */
544 int i; /* entry number */
545 xfs_mount_t *mp; /* filesystem mount point */
546 xfs_dir2_data_aoff_t offset; /* data block offset */
547 xfs_dir2_sf_entry_t *sfep; /* shortform entry */
548 xfs_dir2_sf_hdr_t *sfp; /* shortform structure */
549 int size; /* entry's data size */
550 int used; /* data bytes used */
555 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
556 size = dp->d_ops->data_entsize(args->namelen);
557 offset = dp->d_ops->data_first_offset;
558 sfep = xfs_dir2_sf_firstentry(sfp);
561 * Loop over sf entries.
562 * Keep track of data offset and whether we've seen a place
563 * to insert the new entry.
565 for (i = 0; i < sfp->count; i++) {
567 holefit = offset + size <= xfs_dir2_sf_get_offset(sfep);
568 offset = xfs_dir2_sf_get_offset(sfep) +
569 dp->d_ops->data_entsize(sfep->namelen);
570 sfep = dp->d_ops->sf_nextentry(sfp, sfep);
573 * Calculate data bytes used excluding the new entry, if this
574 * was a data block (block form directory).
577 (sfp->count + 3) * (uint)sizeof(xfs_dir2_leaf_entry_t) +
578 (uint)sizeof(xfs_dir2_block_tail_t);
580 * If it won't fit in a block form then we can't insert it,
581 * we'll go back, convert to block, then try the insert and convert
584 if (used + (holefit ? 0 : size) > args->geo->blksize)
587 * If changing the inode number size, do it the hard way.
592 * If it won't fit at the end then do it the hard way (use the hole).
594 if (used + size > args->geo->blksize)
597 * Do it the easy way.
606 * Check consistency of shortform directory, assert if bad.
610 xfs_da_args_t *args) /* operation arguments */
612 xfs_inode_t *dp; /* incore directory inode */
613 int i; /* entry number */
614 int i8count; /* number of big inode#s */
615 xfs_ino_t ino; /* entry inode number */
616 int offset; /* data offset */
617 xfs_dir2_sf_entry_t *sfep; /* shortform dir entry */
618 xfs_dir2_sf_hdr_t *sfp; /* shortform structure */
619 struct xfs_mount *mp;
624 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
625 offset = dp->d_ops->data_first_offset;
626 ino = dp->d_ops->sf_get_parent_ino(sfp);
627 i8count = ino > XFS_DIR2_MAX_SHORT_INUM;
629 for (i = 0, sfep = xfs_dir2_sf_firstentry(sfp);
631 i++, sfep = dp->d_ops->sf_nextentry(sfp, sfep)) {
632 ASSERT(xfs_dir2_sf_get_offset(sfep) >= offset);
633 ino = dp->d_ops->sf_get_ino(sfp, sfep);
634 i8count += ino > XFS_DIR2_MAX_SHORT_INUM;
636 xfs_dir2_sf_get_offset(sfep) +
637 dp->d_ops->data_entsize(sfep->namelen);
638 ASSERT(dp->d_ops->sf_get_ftype(sfep) < XFS_DIR3_FT_MAX);
640 ASSERT(i8count == sfp->i8count);
641 ASSERT((char *)sfep - (char *)sfp == dp->i_d.di_size);
643 (sfp->count + 2) * (uint)sizeof(xfs_dir2_leaf_entry_t) +
644 (uint)sizeof(xfs_dir2_block_tail_t) <= args->geo->blksize);
649 * Create a new (shortform) directory.
651 int /* error, always 0 */
653 xfs_da_args_t *args, /* operation arguments */
654 xfs_ino_t pino) /* parent inode number */
656 xfs_inode_t *dp; /* incore directory inode */
657 int i8count; /* parent inode is an 8-byte number */
658 xfs_dir2_sf_hdr_t *sfp; /* shortform structure */
659 int size; /* directory size */
661 trace_xfs_dir2_sf_create(args);
666 ASSERT(dp->i_d.di_size == 0);
668 * If it's currently a zero-length extent file,
669 * convert it to local format.
671 if (dp->i_d.di_format == XFS_DINODE_FMT_EXTENTS) {
672 dp->i_df.if_flags &= ~XFS_IFEXTENTS; /* just in case */
673 dp->i_d.di_format = XFS_DINODE_FMT_LOCAL;
674 xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE);
675 dp->i_df.if_flags |= XFS_IFINLINE;
677 ASSERT(dp->i_df.if_flags & XFS_IFINLINE);
678 ASSERT(dp->i_df.if_bytes == 0);
679 i8count = pino > XFS_DIR2_MAX_SHORT_INUM;
680 size = xfs_dir2_sf_hdr_size(i8count);
682 * Make a buffer for the data.
684 xfs_idata_realloc(dp, size, XFS_DATA_FORK);
686 * Fill in the header,
688 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
689 sfp->i8count = i8count;
691 * Now can put in the inode number, since i8count is set.
693 dp->d_ops->sf_put_parent_ino(sfp, pino);
695 dp->i_d.di_size = size;
696 xfs_dir2_sf_check(args);
697 xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE | XFS_ILOG_DDATA);
702 * Lookup an entry in a shortform directory.
703 * Returns EEXIST if found, ENOENT if not found.
707 xfs_da_args_t *args) /* operation arguments */
709 xfs_inode_t *dp; /* incore directory inode */
710 int i; /* entry index */
712 xfs_dir2_sf_entry_t *sfep; /* shortform directory entry */
713 xfs_dir2_sf_hdr_t *sfp; /* shortform structure */
714 enum xfs_dacmp cmp; /* comparison result */
715 xfs_dir2_sf_entry_t *ci_sfep; /* case-insens. entry */
717 trace_xfs_dir2_sf_lookup(args);
719 xfs_dir2_sf_check(args);
722 ASSERT(dp->i_df.if_flags & XFS_IFINLINE);
724 * Bail out if the directory is way too short.
726 if (dp->i_d.di_size < offsetof(xfs_dir2_sf_hdr_t, parent)) {
727 ASSERT(XFS_FORCED_SHUTDOWN(dp->i_mount));
730 ASSERT(dp->i_df.if_bytes == dp->i_d.di_size);
731 ASSERT(dp->i_df.if_u1.if_data != NULL);
732 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
733 ASSERT(dp->i_d.di_size >= xfs_dir2_sf_hdr_size(sfp->i8count));
737 if (args->namelen == 1 && args->name[0] == '.') {
738 args->inumber = dp->i_ino;
739 args->cmpresult = XFS_CMP_EXACT;
740 args->filetype = XFS_DIR3_FT_DIR;
744 * Special case for ..
746 if (args->namelen == 2 &&
747 args->name[0] == '.' && args->name[1] == '.') {
748 args->inumber = dp->d_ops->sf_get_parent_ino(sfp);
749 args->cmpresult = XFS_CMP_EXACT;
750 args->filetype = XFS_DIR3_FT_DIR;
754 * Loop over all the entries trying to match ours.
757 for (i = 0, sfep = xfs_dir2_sf_firstentry(sfp); i < sfp->count;
758 i++, sfep = dp->d_ops->sf_nextentry(sfp, sfep)) {
760 * Compare name and if it's an exact match, return the inode
761 * number. If it's the first case-insensitive match, store the
762 * inode number and continue looking for an exact match.
764 cmp = dp->i_mount->m_dirnameops->compname(args, sfep->name,
766 if (cmp != XFS_CMP_DIFFERENT && cmp != args->cmpresult) {
767 args->cmpresult = cmp;
768 args->inumber = dp->d_ops->sf_get_ino(sfp, sfep);
769 args->filetype = dp->d_ops->sf_get_ftype(sfep);
770 if (cmp == XFS_CMP_EXACT)
775 ASSERT(args->op_flags & XFS_DA_OP_OKNOENT);
777 * Here, we can only be doing a lookup (not a rename or replace).
778 * If a case-insensitive match was not found, return -ENOENT.
782 /* otherwise process the CI match as required by the caller */
783 error = xfs_dir_cilookup_result(args, ci_sfep->name, ci_sfep->namelen);
788 * Remove an entry from a shortform directory.
791 xfs_dir2_sf_removename(
794 int byteoff; /* offset of removed entry */
795 xfs_inode_t *dp; /* incore directory inode */
796 int entsize; /* this entry's size */
797 int i; /* shortform entry index */
798 int newsize; /* new inode size */
799 int oldsize; /* old inode size */
800 xfs_dir2_sf_entry_t *sfep; /* shortform directory entry */
801 xfs_dir2_sf_hdr_t *sfp; /* shortform structure */
803 trace_xfs_dir2_sf_removename(args);
807 ASSERT(dp->i_df.if_flags & XFS_IFINLINE);
808 oldsize = (int)dp->i_d.di_size;
810 * Bail out if the directory is way too short.
812 if (oldsize < offsetof(xfs_dir2_sf_hdr_t, parent)) {
813 ASSERT(XFS_FORCED_SHUTDOWN(dp->i_mount));
816 ASSERT(dp->i_df.if_bytes == oldsize);
817 ASSERT(dp->i_df.if_u1.if_data != NULL);
818 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
819 ASSERT(oldsize >= xfs_dir2_sf_hdr_size(sfp->i8count));
821 * Loop over the old directory entries.
822 * Find the one we're deleting.
824 for (i = 0, sfep = xfs_dir2_sf_firstentry(sfp); i < sfp->count;
825 i++, sfep = dp->d_ops->sf_nextentry(sfp, sfep)) {
826 if (xfs_da_compname(args, sfep->name, sfep->namelen) ==
828 ASSERT(dp->d_ops->sf_get_ino(sfp, sfep) ==
841 byteoff = (int)((char *)sfep - (char *)sfp);
842 entsize = dp->d_ops->sf_entsize(sfp, args->namelen);
843 newsize = oldsize - entsize;
845 * Copy the part if any after the removed entry, sliding it down.
847 if (byteoff + entsize < oldsize)
848 memmove((char *)sfp + byteoff, (char *)sfp + byteoff + entsize,
849 oldsize - (byteoff + entsize));
851 * Fix up the header and file size.
854 dp->i_d.di_size = newsize;
856 * Reallocate, making it smaller.
858 xfs_idata_realloc(dp, newsize - oldsize, XFS_DATA_FORK);
859 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
861 * Are we changing inode number size?
863 if (args->inumber > XFS_DIR2_MAX_SHORT_INUM) {
864 if (sfp->i8count == 1)
865 xfs_dir2_sf_toino4(args);
869 xfs_dir2_sf_check(args);
870 xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE | XFS_ILOG_DDATA);
875 * Replace the inode number of an entry in a shortform directory.
879 xfs_da_args_t *args) /* operation arguments */
881 xfs_inode_t *dp; /* incore directory inode */
882 int i; /* entry index */
883 xfs_ino_t ino=0; /* entry old inode number */
884 int i8elevated; /* sf_toino8 set i8count=1 */
885 xfs_dir2_sf_entry_t *sfep; /* shortform directory entry */
886 xfs_dir2_sf_hdr_t *sfp; /* shortform structure */
888 trace_xfs_dir2_sf_replace(args);
892 ASSERT(dp->i_df.if_flags & XFS_IFINLINE);
894 * Bail out if the shortform directory is way too small.
896 if (dp->i_d.di_size < offsetof(xfs_dir2_sf_hdr_t, parent)) {
897 ASSERT(XFS_FORCED_SHUTDOWN(dp->i_mount));
900 ASSERT(dp->i_df.if_bytes == dp->i_d.di_size);
901 ASSERT(dp->i_df.if_u1.if_data != NULL);
902 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
903 ASSERT(dp->i_d.di_size >= xfs_dir2_sf_hdr_size(sfp->i8count));
906 * New inode number is large, and need to convert to 8-byte inodes.
908 if (args->inumber > XFS_DIR2_MAX_SHORT_INUM && sfp->i8count == 0) {
909 int error; /* error return value */
910 int newsize; /* new inode size */
915 ((uint)sizeof(xfs_dir2_ino8_t) -
916 (uint)sizeof(xfs_dir2_ino4_t));
918 * Won't fit as shortform, convert to block then do replace.
920 if (newsize > XFS_IFORK_DSIZE(dp)) {
921 error = xfs_dir2_sf_to_block(args);
925 return xfs_dir2_block_replace(args);
928 * Still fits, convert to 8-byte now.
930 xfs_dir2_sf_toino8(args);
932 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
936 ASSERT(args->namelen != 1 || args->name[0] != '.');
938 * Replace ..'s entry.
940 if (args->namelen == 2 &&
941 args->name[0] == '.' && args->name[1] == '.') {
942 ino = dp->d_ops->sf_get_parent_ino(sfp);
943 ASSERT(args->inumber != ino);
944 dp->d_ops->sf_put_parent_ino(sfp, args->inumber);
947 * Normal entry, look for the name.
950 for (i = 0, sfep = xfs_dir2_sf_firstentry(sfp); i < sfp->count;
951 i++, sfep = dp->d_ops->sf_nextentry(sfp, sfep)) {
952 if (xfs_da_compname(args, sfep->name, sfep->namelen) ==
954 ino = dp->d_ops->sf_get_ino(sfp, sfep);
955 ASSERT(args->inumber != ino);
956 dp->d_ops->sf_put_ino(sfp, sfep, args->inumber);
957 dp->d_ops->sf_put_ftype(sfep, args->filetype);
964 if (i == sfp->count) {
965 ASSERT(args->op_flags & XFS_DA_OP_OKNOENT);
967 xfs_dir2_sf_toino4(args);
972 * See if the old number was large, the new number is small.
974 if (ino > XFS_DIR2_MAX_SHORT_INUM &&
975 args->inumber <= XFS_DIR2_MAX_SHORT_INUM) {
977 * And the old count was one, so need to convert to small.
979 if (sfp->i8count == 1)
980 xfs_dir2_sf_toino4(args);
985 * See if the old number was small, the new number is large.
987 if (ino <= XFS_DIR2_MAX_SHORT_INUM &&
988 args->inumber > XFS_DIR2_MAX_SHORT_INUM) {
990 * add to the i8count unless we just converted to 8-byte
991 * inodes (which does an implied i8count = 1)
993 ASSERT(sfp->i8count != 0);
997 xfs_dir2_sf_check(args);
998 xfs_trans_log_inode(args->trans, dp, XFS_ILOG_DDATA);
1003 * Convert from 8-byte inode numbers to 4-byte inode numbers.
1004 * The last 8-byte inode number is gone, but the count is still 1.
1008 xfs_da_args_t *args) /* operation arguments */
1010 char *buf; /* old dir's buffer */
1011 xfs_inode_t *dp; /* incore directory inode */
1012 int i; /* entry index */
1013 int newsize; /* new inode size */
1014 xfs_dir2_sf_entry_t *oldsfep; /* old sf entry */
1015 xfs_dir2_sf_hdr_t *oldsfp; /* old sf directory */
1016 int oldsize; /* old inode size */
1017 xfs_dir2_sf_entry_t *sfep; /* new sf entry */
1018 xfs_dir2_sf_hdr_t *sfp; /* new sf directory */
1019 struct xfs_mount *mp;
1021 trace_xfs_dir2_sf_toino4(args);
1027 * Copy the old directory to the buffer.
1028 * Then nuke it from the inode, and add the new buffer to the inode.
1029 * Don't want xfs_idata_realloc copying the data here.
1031 oldsize = dp->i_df.if_bytes;
1032 buf = kmem_alloc(oldsize, KM_SLEEP);
1033 oldsfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
1034 ASSERT(oldsfp->i8count == 1);
1035 memcpy(buf, oldsfp, oldsize);
1037 * Compute the new inode size.
1041 (oldsfp->count + 1) *
1042 ((uint)sizeof(xfs_dir2_ino8_t) - (uint)sizeof(xfs_dir2_ino4_t));
1043 xfs_idata_realloc(dp, -oldsize, XFS_DATA_FORK);
1044 xfs_idata_realloc(dp, newsize, XFS_DATA_FORK);
1046 * Reset our pointers, the data has moved.
1048 oldsfp = (xfs_dir2_sf_hdr_t *)buf;
1049 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
1051 * Fill in the new header.
1053 sfp->count = oldsfp->count;
1055 dp->d_ops->sf_put_parent_ino(sfp, dp->d_ops->sf_get_parent_ino(oldsfp));
1057 * Copy the entries field by field.
1059 for (i = 0, sfep = xfs_dir2_sf_firstentry(sfp),
1060 oldsfep = xfs_dir2_sf_firstentry(oldsfp);
1062 i++, sfep = dp->d_ops->sf_nextentry(sfp, sfep),
1063 oldsfep = dp->d_ops->sf_nextentry(oldsfp, oldsfep)) {
1064 sfep->namelen = oldsfep->namelen;
1065 sfep->offset = oldsfep->offset;
1066 memcpy(sfep->name, oldsfep->name, sfep->namelen);
1067 dp->d_ops->sf_put_ino(sfp, sfep,
1068 dp->d_ops->sf_get_ino(oldsfp, oldsfep));
1069 dp->d_ops->sf_put_ftype(sfep, dp->d_ops->sf_get_ftype(oldsfep));
1072 * Clean up the inode.
1075 dp->i_d.di_size = newsize;
1076 xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE | XFS_ILOG_DDATA);
1080 * Convert existing entries from 4-byte inode numbers to 8-byte inode numbers.
1081 * The new entry w/ an 8-byte inode number is not there yet; we leave with
1082 * i8count set to 1, but no corresponding 8-byte entry.
1086 xfs_da_args_t *args) /* operation arguments */
1088 char *buf; /* old dir's buffer */
1089 xfs_inode_t *dp; /* incore directory inode */
1090 int i; /* entry index */
1091 int newsize; /* new inode size */
1092 xfs_dir2_sf_entry_t *oldsfep; /* old sf entry */
1093 xfs_dir2_sf_hdr_t *oldsfp; /* old sf directory */
1094 int oldsize; /* old inode size */
1095 xfs_dir2_sf_entry_t *sfep; /* new sf entry */
1096 xfs_dir2_sf_hdr_t *sfp; /* new sf directory */
1097 struct xfs_mount *mp;
1099 trace_xfs_dir2_sf_toino8(args);
1105 * Copy the old directory to the buffer.
1106 * Then nuke it from the inode, and add the new buffer to the inode.
1107 * Don't want xfs_idata_realloc copying the data here.
1109 oldsize = dp->i_df.if_bytes;
1110 buf = kmem_alloc(oldsize, KM_SLEEP);
1111 oldsfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
1112 ASSERT(oldsfp->i8count == 0);
1113 memcpy(buf, oldsfp, oldsize);
1115 * Compute the new inode size (nb: entry count + 1 for parent)
1119 (oldsfp->count + 1) *
1120 ((uint)sizeof(xfs_dir2_ino8_t) - (uint)sizeof(xfs_dir2_ino4_t));
1121 xfs_idata_realloc(dp, -oldsize, XFS_DATA_FORK);
1122 xfs_idata_realloc(dp, newsize, XFS_DATA_FORK);
1124 * Reset our pointers, the data has moved.
1126 oldsfp = (xfs_dir2_sf_hdr_t *)buf;
1127 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
1129 * Fill in the new header.
1131 sfp->count = oldsfp->count;
1133 dp->d_ops->sf_put_parent_ino(sfp, dp->d_ops->sf_get_parent_ino(oldsfp));
1135 * Copy the entries field by field.
1137 for (i = 0, sfep = xfs_dir2_sf_firstentry(sfp),
1138 oldsfep = xfs_dir2_sf_firstentry(oldsfp);
1140 i++, sfep = dp->d_ops->sf_nextentry(sfp, sfep),
1141 oldsfep = dp->d_ops->sf_nextentry(oldsfp, oldsfep)) {
1142 sfep->namelen = oldsfep->namelen;
1143 sfep->offset = oldsfep->offset;
1144 memcpy(sfep->name, oldsfep->name, sfep->namelen);
1145 dp->d_ops->sf_put_ino(sfp, sfep,
1146 dp->d_ops->sf_get_ino(oldsfp, oldsfep));
1147 dp->d_ops->sf_put_ftype(sfep, dp->d_ops->sf_get_ftype(oldsfep));
1150 * Clean up the inode.
1153 dp->i_d.di_size = newsize;
1154 xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE | XFS_ILOG_DDATA);