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);
57 #endif /* XFS_BIG_INUMS */
60 * Given a block directory (dp/block), calculate its size as a shortform (sf)
61 * directory and a header for the sf directory, if it will fit it the
62 * space currently present in the inode. If it won't fit, the output
63 * size is too big (but not accurate).
65 int /* size for sf form */
66 xfs_dir2_block_sfsize(
67 xfs_inode_t *dp, /* incore inode pointer */
68 xfs_dir2_data_hdr_t *hdr, /* block directory data */
69 xfs_dir2_sf_hdr_t *sfhp) /* output: header for sf form */
71 xfs_dir2_dataptr_t addr; /* data entry address */
72 xfs_dir2_leaf_entry_t *blp; /* leaf area of the block */
73 xfs_dir2_block_tail_t *btp; /* tail area of the block */
74 int count; /* shortform entry count */
75 xfs_dir2_data_entry_t *dep; /* data entry in the block */
76 int i; /* block entry index */
77 int i8count; /* count of big-inode entries */
78 int isdot; /* entry is "." */
79 int isdotdot; /* entry is ".." */
80 xfs_mount_t *mp; /* mount structure pointer */
81 int namelen; /* total name bytes */
82 xfs_ino_t parent = 0; /* parent inode number */
83 int size=0; /* total computed size */
89 * if there is a filetype field, add the extra byte to the namelen
90 * for each entry that we see.
92 has_ftype = xfs_sb_version_hasftype(&mp->m_sb) ? 1 : 0;
94 count = i8count = namelen = 0;
95 btp = xfs_dir2_block_tail_p(mp, hdr);
96 blp = xfs_dir2_block_leaf_p(btp);
99 * Iterate over the block's data entries by using the leaf pointers.
101 for (i = 0; i < be32_to_cpu(btp->count); i++) {
102 if ((addr = be32_to_cpu(blp[i].address)) == XFS_DIR2_NULL_DATAPTR)
105 * Calculate the pointer to the entry at hand.
107 dep = (xfs_dir2_data_entry_t *)
108 ((char *)hdr + xfs_dir2_dataptr_to_off(mp, addr));
110 * Detect . and .., so we can special-case them.
111 * . is not included in sf directories.
112 * .. is included by just the parent inode number.
114 isdot = dep->namelen == 1 && dep->name[0] == '.';
117 dep->name[0] == '.' && dep->name[1] == '.';
120 i8count += be64_to_cpu(dep->inumber) > XFS_DIR2_MAX_SHORT_INUM;
122 /* take into account the file type field */
123 if (!isdot && !isdotdot) {
125 namelen += dep->namelen + has_ftype;
127 parent = be64_to_cpu(dep->inumber);
129 * Calculate the new size, see if we should give up yet.
131 size = xfs_dir2_sf_hdr_size(i8count) + /* header */
132 count + /* namelen */
133 count * (uint)sizeof(xfs_dir2_sf_off_t) + /* offset */
135 (i8count ? /* inumber */
136 (uint)sizeof(xfs_dir2_ino8_t) * count :
137 (uint)sizeof(xfs_dir2_ino4_t) * count);
138 if (size > XFS_IFORK_DSIZE(dp))
139 return size; /* size value is a failure */
142 * Create the output header, if it worked.
145 sfhp->i8count = i8count;
146 dp->d_ops->sf_put_parent_ino(sfhp, parent);
151 * Convert a block format directory to shortform.
152 * Caller has already checked that it will fit, and built us a header.
155 xfs_dir2_block_to_sf(
156 xfs_da_args_t *args, /* operation arguments */
158 int size, /* shortform directory size */
159 xfs_dir2_sf_hdr_t *sfhp) /* shortform directory hdr */
161 xfs_dir2_data_hdr_t *hdr; /* block header */
162 xfs_dir2_block_tail_t *btp; /* block tail pointer */
163 xfs_dir2_data_entry_t *dep; /* data entry pointer */
164 xfs_inode_t *dp; /* incore directory inode */
165 xfs_dir2_data_unused_t *dup; /* unused data pointer */
166 char *endptr; /* end of data entries */
167 int error; /* error return value */
168 int logflags; /* inode logging flags */
169 xfs_mount_t *mp; /* filesystem mount point */
170 char *ptr; /* current data pointer */
171 xfs_dir2_sf_entry_t *sfep; /* shortform entry */
172 xfs_dir2_sf_hdr_t *sfp; /* shortform directory header */
174 trace_xfs_dir2_block_to_sf(args);
180 * Make a copy of the block data, so we can shrink the inode
181 * and add local data.
183 hdr = kmem_alloc(mp->m_dirblksize, KM_SLEEP);
184 memcpy(hdr, bp->b_addr, mp->m_dirblksize);
185 logflags = XFS_ILOG_CORE;
186 if ((error = xfs_dir2_shrink_inode(args, mp->m_dirdatablk, bp))) {
187 ASSERT(error != ENOSPC);
192 * The buffer is now unconditionally gone, whether
193 * xfs_dir2_shrink_inode worked or not.
195 * Convert the inode to local format.
197 dp->i_df.if_flags &= ~XFS_IFEXTENTS;
198 dp->i_df.if_flags |= XFS_IFINLINE;
199 dp->i_d.di_format = XFS_DINODE_FMT_LOCAL;
200 ASSERT(dp->i_df.if_bytes == 0);
201 xfs_idata_realloc(dp, size, XFS_DATA_FORK);
202 logflags |= XFS_ILOG_DDATA;
204 * Copy the header into the newly allocate local space.
206 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
207 memcpy(sfp, sfhp, xfs_dir2_sf_hdr_size(sfhp->i8count));
208 dp->i_d.di_size = size;
210 * Set up to loop over the block's entries.
212 btp = xfs_dir2_block_tail_p(mp, hdr);
213 ptr = (char *)dp->d_ops->data_entry_p(hdr);
214 endptr = (char *)xfs_dir2_block_leaf_p(btp);
215 sfep = xfs_dir2_sf_firstentry(sfp);
217 * Loop over the active and unused entries.
218 * Stop when we reach the leaf/tail portion of the block.
220 while (ptr < endptr) {
222 * If it's unused, just skip over it.
224 dup = (xfs_dir2_data_unused_t *)ptr;
225 if (be16_to_cpu(dup->freetag) == XFS_DIR2_DATA_FREE_TAG) {
226 ptr += be16_to_cpu(dup->length);
229 dep = (xfs_dir2_data_entry_t *)ptr;
233 if (dep->namelen == 1 && dep->name[0] == '.')
234 ASSERT(be64_to_cpu(dep->inumber) == dp->i_ino);
236 * Skip .., but make sure the inode number is right.
238 else if (dep->namelen == 2 &&
239 dep->name[0] == '.' && dep->name[1] == '.')
240 ASSERT(be64_to_cpu(dep->inumber) ==
241 dp->d_ops->sf_get_parent_ino(sfp));
243 * Normal entry, copy it into shortform.
246 sfep->namelen = dep->namelen;
247 xfs_dir2_sf_put_offset(sfep,
248 (xfs_dir2_data_aoff_t)
249 ((char *)dep - (char *)hdr));
250 memcpy(sfep->name, dep->name, dep->namelen);
251 dp->d_ops->sf_put_ino(sfp, sfep,
252 be64_to_cpu(dep->inumber));
253 dp->d_ops->sf_put_ftype(sfep,
254 dp->d_ops->data_get_ftype(dep));
256 sfep = dp->d_ops->sf_nextentry(sfp, sfep);
258 ptr += dp->d_ops->data_entsize(dep->namelen);
260 ASSERT((char *)sfep - (char *)sfp == size);
261 xfs_dir2_sf_check(args);
263 xfs_trans_log_inode(args->trans, dp, logflags);
269 * Add a name to a shortform directory.
270 * There are two algorithms, "easy" and "hard" which we decide on
271 * before changing anything.
272 * Convert to block form if necessary, if the new entry won't fit.
276 xfs_da_args_t *args) /* operation arguments */
278 int add_entsize; /* size of the new entry */
279 xfs_inode_t *dp; /* incore directory inode */
280 int error; /* error return value */
281 int incr_isize; /* total change in size */
282 int new_isize; /* di_size after adding name */
283 int objchange; /* changing to 8-byte inodes */
284 xfs_dir2_data_aoff_t offset = 0; /* offset for new entry */
285 int old_isize; /* di_size before adding name */
286 int pick; /* which algorithm to use */
287 xfs_dir2_sf_hdr_t *sfp; /* shortform structure */
288 xfs_dir2_sf_entry_t *sfep = NULL; /* shortform entry */
290 trace_xfs_dir2_sf_addname(args);
292 ASSERT(xfs_dir2_sf_lookup(args) == ENOENT);
294 ASSERT(dp->i_df.if_flags & XFS_IFINLINE);
296 * Make sure the shortform value has some of its header.
298 if (dp->i_d.di_size < offsetof(xfs_dir2_sf_hdr_t, parent)) {
299 ASSERT(XFS_FORCED_SHUTDOWN(dp->i_mount));
300 return XFS_ERROR(EIO);
302 ASSERT(dp->i_df.if_bytes == dp->i_d.di_size);
303 ASSERT(dp->i_df.if_u1.if_data != NULL);
304 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
305 ASSERT(dp->i_d.di_size >= xfs_dir2_sf_hdr_size(sfp->i8count));
307 * Compute entry (and change in) size.
309 add_entsize = dp->d_ops->sf_entsize(sfp, args->namelen);
310 incr_isize = add_entsize;
314 * Do we have to change to 8 byte inodes?
316 if (args->inumber > XFS_DIR2_MAX_SHORT_INUM && sfp->i8count == 0) {
318 * Yes, adjust the entry size and the total size.
321 (uint)sizeof(xfs_dir2_ino8_t) -
322 (uint)sizeof(xfs_dir2_ino4_t);
325 ((uint)sizeof(xfs_dir2_ino8_t) -
326 (uint)sizeof(xfs_dir2_ino4_t));
330 old_isize = (int)dp->i_d.di_size;
331 new_isize = old_isize + incr_isize;
333 * Won't fit as shortform any more (due to size),
334 * or the pick routine says it won't (due to offset values).
336 if (new_isize > XFS_IFORK_DSIZE(dp) ||
338 xfs_dir2_sf_addname_pick(args, objchange, &sfep, &offset)) == 0) {
340 * Just checking or no space reservation, it doesn't fit.
342 if ((args->op_flags & XFS_DA_OP_JUSTCHECK) || args->total == 0)
343 return XFS_ERROR(ENOSPC);
345 * Convert to block form then add the name.
347 error = xfs_dir2_sf_to_block(args);
350 return xfs_dir2_block_addname(args);
353 * Just checking, it fits.
355 if (args->op_flags & XFS_DA_OP_JUSTCHECK)
358 * Do it the easy way - just add it at the end.
361 xfs_dir2_sf_addname_easy(args, sfep, offset, new_isize);
363 * Do it the hard way - look for a place to insert the new entry.
364 * Convert to 8 byte inode numbers first if necessary.
370 xfs_dir2_sf_toino8(args);
372 xfs_dir2_sf_addname_hard(args, objchange, new_isize);
374 xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE | XFS_ILOG_DDATA);
379 * Add the new entry the "easy" way.
380 * This is copying the old directory and adding the new entry at the end.
381 * Since it's sorted by "offset" we need room after the last offset
382 * that's already there, and then room to convert to a block directory.
383 * This is already checked by the pick routine.
386 xfs_dir2_sf_addname_easy(
387 xfs_da_args_t *args, /* operation arguments */
388 xfs_dir2_sf_entry_t *sfep, /* pointer to new entry */
389 xfs_dir2_data_aoff_t offset, /* offset to use for new ent */
390 int new_isize) /* new directory size */
392 int byteoff; /* byte offset in sf dir */
393 xfs_inode_t *dp; /* incore directory inode */
394 xfs_dir2_sf_hdr_t *sfp; /* shortform structure */
398 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
399 byteoff = (int)((char *)sfep - (char *)sfp);
401 * Grow the in-inode space.
403 xfs_idata_realloc(dp, dp->d_ops->sf_entsize(sfp, args->namelen),
406 * Need to set up again due to realloc of the inode data.
408 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
409 sfep = (xfs_dir2_sf_entry_t *)((char *)sfp + byteoff);
411 * Fill in the new entry.
413 sfep->namelen = args->namelen;
414 xfs_dir2_sf_put_offset(sfep, offset);
415 memcpy(sfep->name, args->name, sfep->namelen);
416 dp->d_ops->sf_put_ino(sfp, sfep, args->inumber);
417 dp->d_ops->sf_put_ftype(sfep, args->filetype);
420 * Update the header and inode.
424 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);
515 if (args->inumber > XFS_DIR2_MAX_SHORT_INUM && !objchange)
519 * If there's more left to copy, do that.
522 sfep = dp->d_ops->sf_nextentry(sfp, sfep);
523 memcpy(sfep, oldsfep, old_isize - nbytes);
526 dp->i_d.di_size = new_isize;
527 xfs_dir2_sf_check(args);
531 * Decide if the new entry will fit at all.
532 * If it will fit, pick between adding the new entry to the end (easy)
533 * or somewhere else (hard).
534 * Return 0 (won't fit), 1 (easy), 2 (hard).
537 static int /* pick result */
538 xfs_dir2_sf_addname_pick(
539 xfs_da_args_t *args, /* operation arguments */
540 int objchange, /* inode # size changes */
541 xfs_dir2_sf_entry_t **sfepp, /* out(1): new entry ptr */
542 xfs_dir2_data_aoff_t *offsetp) /* out(1): new offset */
544 xfs_inode_t *dp; /* incore directory inode */
545 int holefit; /* found hole it will fit in */
546 int i; /* entry number */
547 xfs_mount_t *mp; /* filesystem mount point */
548 xfs_dir2_data_aoff_t offset; /* data block offset */
549 xfs_dir2_sf_entry_t *sfep; /* shortform entry */
550 xfs_dir2_sf_hdr_t *sfp; /* shortform structure */
551 int size; /* entry's data size */
552 int used; /* data bytes used */
557 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
558 size = dp->d_ops->data_entsize(args->namelen);
559 offset = dp->d_ops->data_first_offset;
560 sfep = xfs_dir2_sf_firstentry(sfp);
563 * Loop over sf entries.
564 * Keep track of data offset and whether we've seen a place
565 * to insert the new entry.
567 for (i = 0; i < sfp->count; i++) {
569 holefit = offset + size <= xfs_dir2_sf_get_offset(sfep);
570 offset = xfs_dir2_sf_get_offset(sfep) +
571 dp->d_ops->data_entsize(sfep->namelen);
572 sfep = dp->d_ops->sf_nextentry(sfp, sfep);
575 * Calculate data bytes used excluding the new entry, if this
576 * was a data block (block form directory).
579 (sfp->count + 3) * (uint)sizeof(xfs_dir2_leaf_entry_t) +
580 (uint)sizeof(xfs_dir2_block_tail_t);
582 * If it won't fit in a block form then we can't insert it,
583 * we'll go back, convert to block, then try the insert and convert
586 if (used + (holefit ? 0 : size) > mp->m_dirblksize)
589 * If changing the inode number size, do it the hard way.
596 ASSERT(objchange == 0);
599 * If it won't fit at the end then do it the hard way (use the hole).
601 if (used + size > mp->m_dirblksize)
604 * Do it the easy way.
613 * Check consistency of shortform directory, assert if bad.
617 xfs_da_args_t *args) /* operation arguments */
619 xfs_inode_t *dp; /* incore directory inode */
620 int i; /* entry number */
621 int i8count; /* number of big inode#s */
622 xfs_ino_t ino; /* entry inode number */
623 int offset; /* data offset */
624 xfs_dir2_sf_entry_t *sfep; /* shortform dir entry */
625 xfs_dir2_sf_hdr_t *sfp; /* shortform structure */
626 struct xfs_mount *mp;
631 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
632 offset = dp->d_ops->data_first_offset;
633 ino = dp->d_ops->sf_get_parent_ino(sfp);
634 i8count = ino > XFS_DIR2_MAX_SHORT_INUM;
636 for (i = 0, sfep = xfs_dir2_sf_firstentry(sfp);
638 i++, sfep = dp->d_ops->sf_nextentry(sfp, sfep)) {
639 ASSERT(xfs_dir2_sf_get_offset(sfep) >= offset);
640 ino = dp->d_ops->sf_get_ino(sfp, sfep);
641 i8count += ino > XFS_DIR2_MAX_SHORT_INUM;
643 xfs_dir2_sf_get_offset(sfep) +
644 dp->d_ops->data_entsize(sfep->namelen);
645 ASSERT(dp->d_ops->sf_get_ftype(sfep) < XFS_DIR3_FT_MAX);
647 ASSERT(i8count == sfp->i8count);
648 ASSERT(XFS_BIG_INUMS || i8count == 0);
649 ASSERT((char *)sfep - (char *)sfp == dp->i_d.di_size);
651 (sfp->count + 2) * (uint)sizeof(xfs_dir2_leaf_entry_t) +
652 (uint)sizeof(xfs_dir2_block_tail_t) <= mp->m_dirblksize);
657 * Create a new (shortform) directory.
659 int /* error, always 0 */
661 xfs_da_args_t *args, /* operation arguments */
662 xfs_ino_t pino) /* parent inode number */
664 xfs_inode_t *dp; /* incore directory inode */
665 int i8count; /* parent inode is an 8-byte number */
666 xfs_dir2_sf_hdr_t *sfp; /* shortform structure */
667 int size; /* directory size */
669 trace_xfs_dir2_sf_create(args);
674 ASSERT(dp->i_d.di_size == 0);
676 * If it's currently a zero-length extent file,
677 * convert it to local format.
679 if (dp->i_d.di_format == XFS_DINODE_FMT_EXTENTS) {
680 dp->i_df.if_flags &= ~XFS_IFEXTENTS; /* just in case */
681 dp->i_d.di_format = XFS_DINODE_FMT_LOCAL;
682 xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE);
683 dp->i_df.if_flags |= XFS_IFINLINE;
685 ASSERT(dp->i_df.if_flags & XFS_IFINLINE);
686 ASSERT(dp->i_df.if_bytes == 0);
687 i8count = pino > XFS_DIR2_MAX_SHORT_INUM;
688 size = xfs_dir2_sf_hdr_size(i8count);
690 * Make a buffer for the data.
692 xfs_idata_realloc(dp, size, XFS_DATA_FORK);
694 * Fill in the header,
696 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
697 sfp->i8count = i8count;
699 * Now can put in the inode number, since i8count is set.
701 dp->d_ops->sf_put_parent_ino(sfp, pino);
703 dp->i_d.di_size = size;
704 xfs_dir2_sf_check(args);
705 xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE | XFS_ILOG_DDATA);
710 * Lookup an entry in a shortform directory.
711 * Returns EEXIST if found, ENOENT if not found.
715 xfs_da_args_t *args) /* operation arguments */
717 xfs_inode_t *dp; /* incore directory inode */
718 int i; /* entry index */
720 xfs_dir2_sf_entry_t *sfep; /* shortform directory entry */
721 xfs_dir2_sf_hdr_t *sfp; /* shortform structure */
722 enum xfs_dacmp cmp; /* comparison result */
723 xfs_dir2_sf_entry_t *ci_sfep; /* case-insens. entry */
725 trace_xfs_dir2_sf_lookup(args);
727 xfs_dir2_sf_check(args);
730 ASSERT(dp->i_df.if_flags & XFS_IFINLINE);
732 * Bail out if the directory is way too short.
734 if (dp->i_d.di_size < offsetof(xfs_dir2_sf_hdr_t, parent)) {
735 ASSERT(XFS_FORCED_SHUTDOWN(dp->i_mount));
736 return XFS_ERROR(EIO);
738 ASSERT(dp->i_df.if_bytes == dp->i_d.di_size);
739 ASSERT(dp->i_df.if_u1.if_data != NULL);
740 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
741 ASSERT(dp->i_d.di_size >= xfs_dir2_sf_hdr_size(sfp->i8count));
745 if (args->namelen == 1 && args->name[0] == '.') {
746 args->inumber = dp->i_ino;
747 args->cmpresult = XFS_CMP_EXACT;
748 args->filetype = XFS_DIR3_FT_DIR;
749 return XFS_ERROR(EEXIST);
752 * Special case for ..
754 if (args->namelen == 2 &&
755 args->name[0] == '.' && args->name[1] == '.') {
756 args->inumber = dp->d_ops->sf_get_parent_ino(sfp);
757 args->cmpresult = XFS_CMP_EXACT;
758 args->filetype = XFS_DIR3_FT_DIR;
759 return XFS_ERROR(EEXIST);
762 * Loop over all the entries trying to match ours.
765 for (i = 0, sfep = xfs_dir2_sf_firstentry(sfp); i < sfp->count;
766 i++, sfep = dp->d_ops->sf_nextentry(sfp, sfep)) {
768 * Compare name and if it's an exact match, return the inode
769 * number. If it's the first case-insensitive match, store the
770 * inode number and continue looking for an exact match.
772 cmp = dp->i_mount->m_dirnameops->compname(args, sfep->name,
774 if (cmp != XFS_CMP_DIFFERENT && cmp != args->cmpresult) {
775 args->cmpresult = cmp;
776 args->inumber = dp->d_ops->sf_get_ino(sfp, sfep);
777 args->filetype = dp->d_ops->sf_get_ftype(sfep);
778 if (cmp == XFS_CMP_EXACT)
779 return XFS_ERROR(EEXIST);
783 ASSERT(args->op_flags & XFS_DA_OP_OKNOENT);
785 * Here, we can only be doing a lookup (not a rename or replace).
786 * If a case-insensitive match was not found, return ENOENT.
789 return XFS_ERROR(ENOENT);
790 /* otherwise process the CI match as required by the caller */
791 error = xfs_dir_cilookup_result(args, ci_sfep->name, ci_sfep->namelen);
792 return XFS_ERROR(error);
796 * Remove an entry from a shortform directory.
799 xfs_dir2_sf_removename(
802 int byteoff; /* offset of removed entry */
803 xfs_inode_t *dp; /* incore directory inode */
804 int entsize; /* this entry's size */
805 int i; /* shortform entry index */
806 int newsize; /* new inode size */
807 int oldsize; /* old inode size */
808 xfs_dir2_sf_entry_t *sfep; /* shortform directory entry */
809 xfs_dir2_sf_hdr_t *sfp; /* shortform structure */
811 trace_xfs_dir2_sf_removename(args);
815 ASSERT(dp->i_df.if_flags & XFS_IFINLINE);
816 oldsize = (int)dp->i_d.di_size;
818 * Bail out if the directory is way too short.
820 if (oldsize < offsetof(xfs_dir2_sf_hdr_t, parent)) {
821 ASSERT(XFS_FORCED_SHUTDOWN(dp->i_mount));
822 return XFS_ERROR(EIO);
824 ASSERT(dp->i_df.if_bytes == oldsize);
825 ASSERT(dp->i_df.if_u1.if_data != NULL);
826 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
827 ASSERT(oldsize >= xfs_dir2_sf_hdr_size(sfp->i8count));
829 * Loop over the old directory entries.
830 * Find the one we're deleting.
832 for (i = 0, sfep = xfs_dir2_sf_firstentry(sfp); i < sfp->count;
833 i++, sfep = dp->d_ops->sf_nextentry(sfp, sfep)) {
834 if (xfs_da_compname(args, sfep->name, sfep->namelen) ==
836 ASSERT(dp->d_ops->sf_get_ino(sfp, sfep) ==
845 return XFS_ERROR(ENOENT);
849 byteoff = (int)((char *)sfep - (char *)sfp);
850 entsize = dp->d_ops->sf_entsize(sfp, args->namelen);
851 newsize = oldsize - entsize;
853 * Copy the part if any after the removed entry, sliding it down.
855 if (byteoff + entsize < oldsize)
856 memmove((char *)sfp + byteoff, (char *)sfp + byteoff + entsize,
857 oldsize - (byteoff + entsize));
859 * Fix up the header and file size.
862 dp->i_d.di_size = newsize;
864 * Reallocate, making it smaller.
866 xfs_idata_realloc(dp, newsize - oldsize, XFS_DATA_FORK);
867 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
870 * Are we changing inode number size?
872 if (args->inumber > XFS_DIR2_MAX_SHORT_INUM) {
873 if (sfp->i8count == 1)
874 xfs_dir2_sf_toino4(args);
879 xfs_dir2_sf_check(args);
880 xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE | XFS_ILOG_DDATA);
885 * Replace the inode number of an entry in a shortform directory.
889 xfs_da_args_t *args) /* operation arguments */
891 xfs_inode_t *dp; /* incore directory inode */
892 int i; /* entry index */
893 #if XFS_BIG_INUMS || defined(DEBUG)
894 xfs_ino_t ino=0; /* entry old inode number */
897 int i8elevated; /* sf_toino8 set i8count=1 */
899 xfs_dir2_sf_entry_t *sfep; /* shortform directory entry */
900 xfs_dir2_sf_hdr_t *sfp; /* shortform structure */
902 trace_xfs_dir2_sf_replace(args);
906 ASSERT(dp->i_df.if_flags & XFS_IFINLINE);
908 * Bail out if the shortform directory is way too small.
910 if (dp->i_d.di_size < offsetof(xfs_dir2_sf_hdr_t, parent)) {
911 ASSERT(XFS_FORCED_SHUTDOWN(dp->i_mount));
912 return XFS_ERROR(EIO);
914 ASSERT(dp->i_df.if_bytes == dp->i_d.di_size);
915 ASSERT(dp->i_df.if_u1.if_data != NULL);
916 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
917 ASSERT(dp->i_d.di_size >= xfs_dir2_sf_hdr_size(sfp->i8count));
920 * New inode number is large, and need to convert to 8-byte inodes.
922 if (args->inumber > XFS_DIR2_MAX_SHORT_INUM && sfp->i8count == 0) {
923 int error; /* error return value */
924 int newsize; /* new inode size */
929 ((uint)sizeof(xfs_dir2_ino8_t) -
930 (uint)sizeof(xfs_dir2_ino4_t));
932 * Won't fit as shortform, convert to block then do replace.
934 if (newsize > XFS_IFORK_DSIZE(dp)) {
935 error = xfs_dir2_sf_to_block(args);
939 return xfs_dir2_block_replace(args);
942 * Still fits, convert to 8-byte now.
944 xfs_dir2_sf_toino8(args);
946 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
950 ASSERT(args->namelen != 1 || args->name[0] != '.');
952 * Replace ..'s entry.
954 if (args->namelen == 2 &&
955 args->name[0] == '.' && args->name[1] == '.') {
956 #if XFS_BIG_INUMS || defined(DEBUG)
957 ino = dp->d_ops->sf_get_parent_ino(sfp);
958 ASSERT(args->inumber != ino);
960 dp->d_ops->sf_put_parent_ino(sfp, args->inumber);
963 * Normal entry, look for the name.
966 for (i = 0, sfep = xfs_dir2_sf_firstentry(sfp); i < sfp->count;
967 i++, sfep = dp->d_ops->sf_nextentry(sfp, sfep)) {
968 if (xfs_da_compname(args, sfep->name, sfep->namelen) ==
970 #if XFS_BIG_INUMS || defined(DEBUG)
971 ino = dp->d_ops->sf_get_ino(sfp, sfep);
972 ASSERT(args->inumber != ino);
974 dp->d_ops->sf_put_ino(sfp, sfep, args->inumber);
975 dp->d_ops->sf_put_ftype(sfep, args->filetype);
982 if (i == sfp->count) {
983 ASSERT(args->op_flags & XFS_DA_OP_OKNOENT);
986 xfs_dir2_sf_toino4(args);
988 return XFS_ERROR(ENOENT);
993 * See if the old number was large, the new number is small.
995 if (ino > XFS_DIR2_MAX_SHORT_INUM &&
996 args->inumber <= XFS_DIR2_MAX_SHORT_INUM) {
998 * And the old count was one, so need to convert to small.
1000 if (sfp->i8count == 1)
1001 xfs_dir2_sf_toino4(args);
1006 * See if the old number was small, the new number is large.
1008 if (ino <= XFS_DIR2_MAX_SHORT_INUM &&
1009 args->inumber > XFS_DIR2_MAX_SHORT_INUM) {
1011 * add to the i8count unless we just converted to 8-byte
1012 * inodes (which does an implied i8count = 1)
1014 ASSERT(sfp->i8count != 0);
1019 xfs_dir2_sf_check(args);
1020 xfs_trans_log_inode(args->trans, dp, XFS_ILOG_DDATA);
1026 * Convert from 8-byte inode numbers to 4-byte inode numbers.
1027 * The last 8-byte inode number is gone, but the count is still 1.
1031 xfs_da_args_t *args) /* operation arguments */
1033 char *buf; /* old dir's buffer */
1034 xfs_inode_t *dp; /* incore directory inode */
1035 int i; /* entry index */
1036 int newsize; /* new inode size */
1037 xfs_dir2_sf_entry_t *oldsfep; /* old sf entry */
1038 xfs_dir2_sf_hdr_t *oldsfp; /* old sf directory */
1039 int oldsize; /* old inode size */
1040 xfs_dir2_sf_entry_t *sfep; /* new sf entry */
1041 xfs_dir2_sf_hdr_t *sfp; /* new sf directory */
1042 struct xfs_mount *mp;
1044 trace_xfs_dir2_sf_toino4(args);
1050 * Copy the old directory to the buffer.
1051 * Then nuke it from the inode, and add the new buffer to the inode.
1052 * Don't want xfs_idata_realloc copying the data here.
1054 oldsize = dp->i_df.if_bytes;
1055 buf = kmem_alloc(oldsize, KM_SLEEP);
1056 oldsfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
1057 ASSERT(oldsfp->i8count == 1);
1058 memcpy(buf, oldsfp, oldsize);
1060 * Compute the new inode size.
1064 (oldsfp->count + 1) *
1065 ((uint)sizeof(xfs_dir2_ino8_t) - (uint)sizeof(xfs_dir2_ino4_t));
1066 xfs_idata_realloc(dp, -oldsize, XFS_DATA_FORK);
1067 xfs_idata_realloc(dp, newsize, XFS_DATA_FORK);
1069 * Reset our pointers, the data has moved.
1071 oldsfp = (xfs_dir2_sf_hdr_t *)buf;
1072 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
1074 * Fill in the new header.
1076 sfp->count = oldsfp->count;
1078 dp->d_ops->sf_put_parent_ino(sfp, dp->d_ops->sf_get_parent_ino(oldsfp));
1080 * Copy the entries field by field.
1082 for (i = 0, sfep = xfs_dir2_sf_firstentry(sfp),
1083 oldsfep = xfs_dir2_sf_firstentry(oldsfp);
1085 i++, sfep = dp->d_ops->sf_nextentry(sfp, sfep),
1086 oldsfep = dp->d_ops->sf_nextentry(oldsfp, oldsfep)) {
1087 sfep->namelen = oldsfep->namelen;
1088 sfep->offset = oldsfep->offset;
1089 memcpy(sfep->name, oldsfep->name, sfep->namelen);
1090 dp->d_ops->sf_put_ino(sfp, sfep,
1091 dp->d_ops->sf_get_ino(oldsfp, oldsfep));
1092 dp->d_ops->sf_put_ftype(sfep, dp->d_ops->sf_get_ftype(oldsfep));
1095 * Clean up the inode.
1098 dp->i_d.di_size = newsize;
1099 xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE | XFS_ILOG_DDATA);
1103 * Convert from 4-byte inode numbers to 8-byte inode numbers.
1104 * The new 8-byte inode number is not there yet, we leave with the
1105 * count 1 but no corresponding entry.
1109 xfs_da_args_t *args) /* operation arguments */
1111 char *buf; /* old dir's buffer */
1112 xfs_inode_t *dp; /* incore directory inode */
1113 int i; /* entry index */
1114 int newsize; /* new inode size */
1115 xfs_dir2_sf_entry_t *oldsfep; /* old sf entry */
1116 xfs_dir2_sf_hdr_t *oldsfp; /* old sf directory */
1117 int oldsize; /* old inode size */
1118 xfs_dir2_sf_entry_t *sfep; /* new sf entry */
1119 xfs_dir2_sf_hdr_t *sfp; /* new sf directory */
1120 struct xfs_mount *mp;
1122 trace_xfs_dir2_sf_toino8(args);
1128 * Copy the old directory to the buffer.
1129 * Then nuke it from the inode, and add the new buffer to the inode.
1130 * Don't want xfs_idata_realloc copying the data here.
1132 oldsize = dp->i_df.if_bytes;
1133 buf = kmem_alloc(oldsize, KM_SLEEP);
1134 oldsfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
1135 ASSERT(oldsfp->i8count == 0);
1136 memcpy(buf, oldsfp, oldsize);
1138 * Compute the new inode size.
1142 (oldsfp->count + 1) *
1143 ((uint)sizeof(xfs_dir2_ino8_t) - (uint)sizeof(xfs_dir2_ino4_t));
1144 xfs_idata_realloc(dp, -oldsize, XFS_DATA_FORK);
1145 xfs_idata_realloc(dp, newsize, XFS_DATA_FORK);
1147 * Reset our pointers, the data has moved.
1149 oldsfp = (xfs_dir2_sf_hdr_t *)buf;
1150 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
1152 * Fill in the new header.
1154 sfp->count = oldsfp->count;
1156 dp->d_ops->sf_put_parent_ino(sfp, dp->d_ops->sf_get_parent_ino(oldsfp));
1158 * Copy the entries field by field.
1160 for (i = 0, sfep = xfs_dir2_sf_firstentry(sfp),
1161 oldsfep = xfs_dir2_sf_firstentry(oldsfp);
1163 i++, sfep = dp->d_ops->sf_nextentry(sfp, sfep),
1164 oldsfep = dp->d_ops->sf_nextentry(oldsfp, oldsfep)) {
1165 sfep->namelen = oldsfep->namelen;
1166 sfep->offset = oldsfep->offset;
1167 memcpy(sfep->name, oldsfep->name, sfep->namelen);
1168 dp->d_ops->sf_put_ino(sfp, sfep,
1169 dp->d_ops->sf_get_ino(oldsfp, oldsfep));
1170 dp->d_ops->sf_put_ftype(sfep, dp->d_ops->sf_get_ftype(oldsfep));
1173 * Clean up the inode.
1176 dp->i_d.di_size = newsize;
1177 xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE | XFS_ILOG_DDATA);
1179 #endif /* XFS_BIG_INUMS */