2 * Copyright (c) 2000-2003,2005 Silicon Graphics, Inc.
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation.
9 * This program is distributed in the hope that it would be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write the Free Software Foundation,
16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 #include "xfs_types.h"
23 #include "xfs_trans.h"
27 #include "xfs_mount.h"
28 #include "xfs_da_btree.h"
29 #include "xfs_bmap_btree.h"
30 #include "xfs_dir2_sf.h"
31 #include "xfs_dinode.h"
32 #include "xfs_inode.h"
33 #include "xfs_inode_item.h"
34 #include "xfs_error.h"
35 #include "xfs_dir2_data.h"
36 #include "xfs_dir2_leaf.h"
37 #include "xfs_dir2_block.h"
38 #include "xfs_trace.h"
41 * Prototypes for internal functions.
43 static void xfs_dir2_sf_addname_easy(xfs_da_args_t *args,
44 xfs_dir2_sf_entry_t *sfep,
45 xfs_dir2_data_aoff_t offset,
47 static void xfs_dir2_sf_addname_hard(xfs_da_args_t *args, int objchange,
49 static int xfs_dir2_sf_addname_pick(xfs_da_args_t *args, int objchange,
50 xfs_dir2_sf_entry_t **sfepp,
51 xfs_dir2_data_aoff_t *offsetp);
53 static void xfs_dir2_sf_check(xfs_da_args_t *args);
55 #define xfs_dir2_sf_check(args)
58 static void xfs_dir2_sf_toino4(xfs_da_args_t *args);
59 static void xfs_dir2_sf_toino8(xfs_da_args_t *args);
60 #endif /* XFS_BIG_INUMS */
63 * Inode numbers in short-form directories can come in two versions,
64 * either 4 bytes or 8 bytes wide. These helpers deal with the
65 * two forms transparently by looking at the headers i8count field.
67 * For 64-bit inode number the most significant byte must be zero.
71 struct xfs_dir2_sf_hdr *hdr,
72 xfs_dir2_inou_t *from)
75 return get_unaligned_be64(&from->i8.i) & 0x00ffffffffffffffULL;
77 return get_unaligned_be32(&from->i4.i);
82 struct xfs_dir2_sf_hdr *hdr,
86 ASSERT((ino & 0xff00000000000000ULL) == 0);
89 put_unaligned_be64(ino, &to->i8.i);
91 put_unaligned_be32(ino, &to->i4.i);
95 xfs_dir2_sf_get_parent_ino(
96 struct xfs_dir2_sf_hdr *hdr)
98 return xfs_dir2_sf_get_ino(hdr, &hdr->parent);
102 xfs_dir2_sf_put_parent_ino(
103 struct xfs_dir2_sf_hdr *hdr,
106 xfs_dir2_sf_put_ino(hdr, &hdr->parent, ino);
110 * In short-form directory entries the inode numbers are stored at variable
111 * offset behind the entry name. The inode numbers may only be accessed
112 * through the helpers below.
114 static xfs_dir2_inou_t *
116 struct xfs_dir2_sf_entry *sfep)
118 return (xfs_dir2_inou_t *)&sfep->name[sfep->namelen];
122 xfs_dir2_sfe_get_ino(
123 struct xfs_dir2_sf_hdr *hdr,
124 struct xfs_dir2_sf_entry *sfep)
126 return xfs_dir2_sf_get_ino(hdr, xfs_dir2_sfe_inop(sfep));
130 xfs_dir2_sfe_put_ino(
131 struct xfs_dir2_sf_hdr *hdr,
132 struct xfs_dir2_sf_entry *sfep,
135 xfs_dir2_sf_put_ino(hdr, xfs_dir2_sfe_inop(sfep), ino);
139 * Given a block directory (dp/block), calculate its size as a shortform (sf)
140 * directory and a header for the sf directory, if it will fit it the
141 * space currently present in the inode. If it won't fit, the output
142 * size is too big (but not accurate).
144 int /* size for sf form */
145 xfs_dir2_block_sfsize(
146 xfs_inode_t *dp, /* incore inode pointer */
147 xfs_dir2_data_hdr_t *hdr, /* block directory data */
148 xfs_dir2_sf_hdr_t *sfhp) /* output: header for sf form */
150 xfs_dir2_dataptr_t addr; /* data entry address */
151 xfs_dir2_leaf_entry_t *blp; /* leaf area of the block */
152 xfs_dir2_block_tail_t *btp; /* tail area of the block */
153 int count; /* shortform entry count */
154 xfs_dir2_data_entry_t *dep; /* data entry in the block */
155 int i; /* block entry index */
156 int i8count; /* count of big-inode entries */
157 int isdot; /* entry is "." */
158 int isdotdot; /* entry is ".." */
159 xfs_mount_t *mp; /* mount structure pointer */
160 int namelen; /* total name bytes */
161 xfs_ino_t parent = 0; /* parent inode number */
162 int size=0; /* total computed size */
166 count = i8count = namelen = 0;
167 btp = xfs_dir2_block_tail_p(mp, hdr);
168 blp = xfs_dir2_block_leaf_p(btp);
171 * Iterate over the block's data entries by using the leaf pointers.
173 for (i = 0; i < be32_to_cpu(btp->count); i++) {
174 if ((addr = be32_to_cpu(blp[i].address)) == XFS_DIR2_NULL_DATAPTR)
177 * Calculate the pointer to the entry at hand.
179 dep = (xfs_dir2_data_entry_t *)
180 ((char *)hdr + xfs_dir2_dataptr_to_off(mp, addr));
182 * Detect . and .., so we can special-case them.
183 * . is not included in sf directories.
184 * .. is included by just the parent inode number.
186 isdot = dep->namelen == 1 && dep->name[0] == '.';
189 dep->name[0] == '.' && dep->name[1] == '.';
192 i8count += be64_to_cpu(dep->inumber) > XFS_DIR2_MAX_SHORT_INUM;
194 if (!isdot && !isdotdot) {
196 namelen += dep->namelen;
198 parent = be64_to_cpu(dep->inumber);
200 * Calculate the new size, see if we should give up yet.
202 size = xfs_dir2_sf_hdr_size(i8count) + /* header */
203 count + /* namelen */
204 count * (uint)sizeof(xfs_dir2_sf_off_t) + /* offset */
206 (i8count ? /* inumber */
207 (uint)sizeof(xfs_dir2_ino8_t) * count :
208 (uint)sizeof(xfs_dir2_ino4_t) * count);
209 if (size > XFS_IFORK_DSIZE(dp))
210 return size; /* size value is a failure */
213 * Create the output header, if it worked.
216 sfhp->i8count = i8count;
217 xfs_dir2_sf_put_parent_ino(sfhp, parent);
222 * Convert a block format directory to shortform.
223 * Caller has already checked that it will fit, and built us a header.
226 xfs_dir2_block_to_sf(
227 xfs_da_args_t *args, /* operation arguments */
228 xfs_dabuf_t *bp, /* block buffer */
229 int size, /* shortform directory size */
230 xfs_dir2_sf_hdr_t *sfhp) /* shortform directory hdr */
232 xfs_dir2_data_hdr_t *hdr; /* block header */
233 xfs_dir2_block_tail_t *btp; /* block tail pointer */
234 xfs_dir2_data_entry_t *dep; /* data entry pointer */
235 xfs_inode_t *dp; /* incore directory inode */
236 xfs_dir2_data_unused_t *dup; /* unused data pointer */
237 char *endptr; /* end of data entries */
238 int error; /* error return value */
239 int logflags; /* inode logging flags */
240 xfs_mount_t *mp; /* filesystem mount point */
241 char *ptr; /* current data pointer */
242 xfs_dir2_sf_entry_t *sfep; /* shortform entry */
243 xfs_dir2_sf_hdr_t *sfp; /* shortform directory header */
245 trace_xfs_dir2_block_to_sf(args);
251 * Make a copy of the block data, so we can shrink the inode
252 * and add local data.
254 hdr = kmem_alloc(mp->m_dirblksize, KM_SLEEP);
255 memcpy(hdr, bp->data, mp->m_dirblksize);
256 logflags = XFS_ILOG_CORE;
257 if ((error = xfs_dir2_shrink_inode(args, mp->m_dirdatablk, bp))) {
258 ASSERT(error != ENOSPC);
263 * The buffer is now unconditionally gone, whether
264 * xfs_dir2_shrink_inode worked or not.
266 * Convert the inode to local format.
268 dp->i_df.if_flags &= ~XFS_IFEXTENTS;
269 dp->i_df.if_flags |= XFS_IFINLINE;
270 dp->i_d.di_format = XFS_DINODE_FMT_LOCAL;
271 ASSERT(dp->i_df.if_bytes == 0);
272 xfs_idata_realloc(dp, size, XFS_DATA_FORK);
273 logflags |= XFS_ILOG_DDATA;
275 * Copy the header into the newly allocate local space.
277 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
278 memcpy(sfp, sfhp, xfs_dir2_sf_hdr_size(sfhp->i8count));
279 dp->i_d.di_size = size;
281 * Set up to loop over the block's entries.
283 btp = xfs_dir2_block_tail_p(mp, hdr);
284 ptr = (char *)(hdr + 1);
285 endptr = (char *)xfs_dir2_block_leaf_p(btp);
286 sfep = xfs_dir2_sf_firstentry(sfp);
288 * Loop over the active and unused entries.
289 * Stop when we reach the leaf/tail portion of the block.
291 while (ptr < endptr) {
293 * If it's unused, just skip over it.
295 dup = (xfs_dir2_data_unused_t *)ptr;
296 if (be16_to_cpu(dup->freetag) == XFS_DIR2_DATA_FREE_TAG) {
297 ptr += be16_to_cpu(dup->length);
300 dep = (xfs_dir2_data_entry_t *)ptr;
304 if (dep->namelen == 1 && dep->name[0] == '.')
305 ASSERT(be64_to_cpu(dep->inumber) == dp->i_ino);
307 * Skip .., but make sure the inode number is right.
309 else if (dep->namelen == 2 &&
310 dep->name[0] == '.' && dep->name[1] == '.')
311 ASSERT(be64_to_cpu(dep->inumber) ==
312 xfs_dir2_sf_get_parent_ino(sfp));
314 * Normal entry, copy it into shortform.
317 sfep->namelen = dep->namelen;
318 xfs_dir2_sf_put_offset(sfep,
319 (xfs_dir2_data_aoff_t)
320 ((char *)dep - (char *)hdr));
321 memcpy(sfep->name, dep->name, dep->namelen);
322 xfs_dir2_sfe_put_ino(sfp, sfep,
323 be64_to_cpu(dep->inumber));
325 sfep = xfs_dir2_sf_nextentry(sfp, sfep);
327 ptr += xfs_dir2_data_entsize(dep->namelen);
329 ASSERT((char *)sfep - (char *)sfp == size);
330 xfs_dir2_sf_check(args);
332 xfs_trans_log_inode(args->trans, dp, logflags);
338 * Add a name to a shortform directory.
339 * There are two algorithms, "easy" and "hard" which we decide on
340 * before changing anything.
341 * Convert to block form if necessary, if the new entry won't fit.
345 xfs_da_args_t *args) /* operation arguments */
347 int add_entsize; /* size of the new entry */
348 xfs_inode_t *dp; /* incore directory inode */
349 int error; /* error return value */
350 int incr_isize; /* total change in size */
351 int new_isize; /* di_size after adding name */
352 int objchange; /* changing to 8-byte inodes */
353 xfs_dir2_data_aoff_t offset = 0; /* offset for new entry */
354 int old_isize; /* di_size before adding name */
355 int pick; /* which algorithm to use */
356 xfs_dir2_sf_hdr_t *sfp; /* shortform structure */
357 xfs_dir2_sf_entry_t *sfep = NULL; /* shortform entry */
359 trace_xfs_dir2_sf_addname(args);
361 ASSERT(xfs_dir2_sf_lookup(args) == ENOENT);
363 ASSERT(dp->i_df.if_flags & XFS_IFINLINE);
365 * Make sure the shortform value has some of its header.
367 if (dp->i_d.di_size < offsetof(xfs_dir2_sf_hdr_t, parent)) {
368 ASSERT(XFS_FORCED_SHUTDOWN(dp->i_mount));
369 return XFS_ERROR(EIO);
371 ASSERT(dp->i_df.if_bytes == dp->i_d.di_size);
372 ASSERT(dp->i_df.if_u1.if_data != NULL);
373 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
374 ASSERT(dp->i_d.di_size >= xfs_dir2_sf_hdr_size(sfp->i8count));
376 * Compute entry (and change in) size.
378 add_entsize = xfs_dir2_sf_entsize(sfp, args->namelen);
379 incr_isize = add_entsize;
383 * Do we have to change to 8 byte inodes?
385 if (args->inumber > XFS_DIR2_MAX_SHORT_INUM && sfp->i8count == 0) {
387 * Yes, adjust the entry size and the total size.
390 (uint)sizeof(xfs_dir2_ino8_t) -
391 (uint)sizeof(xfs_dir2_ino4_t);
394 ((uint)sizeof(xfs_dir2_ino8_t) -
395 (uint)sizeof(xfs_dir2_ino4_t));
399 old_isize = (int)dp->i_d.di_size;
400 new_isize = old_isize + incr_isize;
402 * Won't fit as shortform any more (due to size),
403 * or the pick routine says it won't (due to offset values).
405 if (new_isize > XFS_IFORK_DSIZE(dp) ||
407 xfs_dir2_sf_addname_pick(args, objchange, &sfep, &offset)) == 0) {
409 * Just checking or no space reservation, it doesn't fit.
411 if ((args->op_flags & XFS_DA_OP_JUSTCHECK) || args->total == 0)
412 return XFS_ERROR(ENOSPC);
414 * Convert to block form then add the name.
416 error = xfs_dir2_sf_to_block(args);
419 return xfs_dir2_block_addname(args);
422 * Just checking, it fits.
424 if (args->op_flags & XFS_DA_OP_JUSTCHECK)
427 * Do it the easy way - just add it at the end.
430 xfs_dir2_sf_addname_easy(args, sfep, offset, new_isize);
432 * Do it the hard way - look for a place to insert the new entry.
433 * Convert to 8 byte inode numbers first if necessary.
439 xfs_dir2_sf_toino8(args);
441 xfs_dir2_sf_addname_hard(args, objchange, new_isize);
443 xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE | XFS_ILOG_DDATA);
448 * Add the new entry the "easy" way.
449 * This is copying the old directory and adding the new entry at the end.
450 * Since it's sorted by "offset" we need room after the last offset
451 * that's already there, and then room to convert to a block directory.
452 * This is already checked by the pick routine.
455 xfs_dir2_sf_addname_easy(
456 xfs_da_args_t *args, /* operation arguments */
457 xfs_dir2_sf_entry_t *sfep, /* pointer to new entry */
458 xfs_dir2_data_aoff_t offset, /* offset to use for new ent */
459 int new_isize) /* new directory size */
461 int byteoff; /* byte offset in sf dir */
462 xfs_inode_t *dp; /* incore directory inode */
463 xfs_dir2_sf_hdr_t *sfp; /* shortform structure */
467 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
468 byteoff = (int)((char *)sfep - (char *)sfp);
470 * Grow the in-inode space.
472 xfs_idata_realloc(dp, xfs_dir2_sf_entsize(sfp, args->namelen),
475 * Need to set up again due to realloc of the inode data.
477 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
478 sfep = (xfs_dir2_sf_entry_t *)((char *)sfp + byteoff);
480 * Fill in the new entry.
482 sfep->namelen = args->namelen;
483 xfs_dir2_sf_put_offset(sfep, offset);
484 memcpy(sfep->name, args->name, sfep->namelen);
485 xfs_dir2_sfe_put_ino(sfp, sfep, args->inumber);
487 * Update the header and inode.
491 if (args->inumber > XFS_DIR2_MAX_SHORT_INUM)
494 dp->i_d.di_size = new_isize;
495 xfs_dir2_sf_check(args);
499 * Add the new entry the "hard" way.
500 * The caller has already converted to 8 byte inode numbers if necessary,
501 * in which case we need to leave the i8count at 1.
502 * Find a hole that the new entry will fit into, and copy
503 * the first part of the entries, the new entry, and the last part of
508 xfs_dir2_sf_addname_hard(
509 xfs_da_args_t *args, /* operation arguments */
510 int objchange, /* changing inode number size */
511 int new_isize) /* new directory size */
513 int add_datasize; /* data size need for new ent */
514 char *buf; /* buffer for old */
515 xfs_inode_t *dp; /* incore directory inode */
516 int eof; /* reached end of old dir */
517 int nbytes; /* temp for byte copies */
518 xfs_dir2_data_aoff_t new_offset; /* next offset value */
519 xfs_dir2_data_aoff_t offset; /* current offset value */
520 int old_isize; /* previous di_size */
521 xfs_dir2_sf_entry_t *oldsfep; /* entry in original dir */
522 xfs_dir2_sf_hdr_t *oldsfp; /* original shortform dir */
523 xfs_dir2_sf_entry_t *sfep; /* entry in new dir */
524 xfs_dir2_sf_hdr_t *sfp; /* new shortform dir */
527 * Copy the old directory to the stack buffer.
531 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
532 old_isize = (int)dp->i_d.di_size;
533 buf = kmem_alloc(old_isize, KM_SLEEP);
534 oldsfp = (xfs_dir2_sf_hdr_t *)buf;
535 memcpy(oldsfp, sfp, old_isize);
537 * Loop over the old directory finding the place we're going
538 * to insert the new entry.
539 * If it's going to end up at the end then oldsfep will point there.
541 for (offset = XFS_DIR2_DATA_FIRST_OFFSET,
542 oldsfep = xfs_dir2_sf_firstentry(oldsfp),
543 add_datasize = xfs_dir2_data_entsize(args->namelen),
544 eof = (char *)oldsfep == &buf[old_isize];
546 offset = new_offset + xfs_dir2_data_entsize(oldsfep->namelen),
547 oldsfep = xfs_dir2_sf_nextentry(oldsfp, oldsfep),
548 eof = (char *)oldsfep == &buf[old_isize]) {
549 new_offset = xfs_dir2_sf_get_offset(oldsfep);
550 if (offset + add_datasize <= new_offset)
554 * Get rid of the old directory, then allocate space for
555 * the new one. We do this so xfs_idata_realloc won't copy
558 xfs_idata_realloc(dp, -old_isize, XFS_DATA_FORK);
559 xfs_idata_realloc(dp, new_isize, XFS_DATA_FORK);
561 * Reset the pointer since the buffer was reallocated.
563 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
565 * Copy the first part of the directory, including the header.
567 nbytes = (int)((char *)oldsfep - (char *)oldsfp);
568 memcpy(sfp, oldsfp, nbytes);
569 sfep = (xfs_dir2_sf_entry_t *)((char *)sfp + nbytes);
571 * Fill in the new entry, and update the header counts.
573 sfep->namelen = args->namelen;
574 xfs_dir2_sf_put_offset(sfep, offset);
575 memcpy(sfep->name, args->name, sfep->namelen);
576 xfs_dir2_sfe_put_ino(sfp, sfep, args->inumber);
579 if (args->inumber > XFS_DIR2_MAX_SHORT_INUM && !objchange)
583 * If there's more left to copy, do that.
586 sfep = xfs_dir2_sf_nextentry(sfp, sfep);
587 memcpy(sfep, oldsfep, old_isize - nbytes);
590 dp->i_d.di_size = new_isize;
591 xfs_dir2_sf_check(args);
595 * Decide if the new entry will fit at all.
596 * If it will fit, pick between adding the new entry to the end (easy)
597 * or somewhere else (hard).
598 * Return 0 (won't fit), 1 (easy), 2 (hard).
601 static int /* pick result */
602 xfs_dir2_sf_addname_pick(
603 xfs_da_args_t *args, /* operation arguments */
604 int objchange, /* inode # size changes */
605 xfs_dir2_sf_entry_t **sfepp, /* out(1): new entry ptr */
606 xfs_dir2_data_aoff_t *offsetp) /* out(1): new offset */
608 xfs_inode_t *dp; /* incore directory inode */
609 int holefit; /* found hole it will fit in */
610 int i; /* entry number */
611 xfs_mount_t *mp; /* filesystem mount point */
612 xfs_dir2_data_aoff_t offset; /* data block offset */
613 xfs_dir2_sf_entry_t *sfep; /* shortform entry */
614 xfs_dir2_sf_hdr_t *sfp; /* shortform structure */
615 int size; /* entry's data size */
616 int used; /* data bytes used */
621 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
622 size = xfs_dir2_data_entsize(args->namelen);
623 offset = XFS_DIR2_DATA_FIRST_OFFSET;
624 sfep = xfs_dir2_sf_firstentry(sfp);
627 * Loop over sf entries.
628 * Keep track of data offset and whether we've seen a place
629 * to insert the new entry.
631 for (i = 0; i < sfp->count; i++) {
633 holefit = offset + size <= xfs_dir2_sf_get_offset(sfep);
634 offset = xfs_dir2_sf_get_offset(sfep) +
635 xfs_dir2_data_entsize(sfep->namelen);
636 sfep = xfs_dir2_sf_nextentry(sfp, sfep);
639 * Calculate data bytes used excluding the new entry, if this
640 * was a data block (block form directory).
643 (sfp->count + 3) * (uint)sizeof(xfs_dir2_leaf_entry_t) +
644 (uint)sizeof(xfs_dir2_block_tail_t);
646 * If it won't fit in a block form then we can't insert it,
647 * we'll go back, convert to block, then try the insert and convert
650 if (used + (holefit ? 0 : size) > mp->m_dirblksize)
653 * If changing the inode number size, do it the hard way.
660 ASSERT(objchange == 0);
663 * If it won't fit at the end then do it the hard way (use the hole).
665 if (used + size > mp->m_dirblksize)
668 * Do it the easy way.
677 * Check consistency of shortform directory, assert if bad.
681 xfs_da_args_t *args) /* operation arguments */
683 xfs_inode_t *dp; /* incore directory inode */
684 int i; /* entry number */
685 int i8count; /* number of big inode#s */
686 xfs_ino_t ino; /* entry inode number */
687 int offset; /* data offset */
688 xfs_dir2_sf_entry_t *sfep; /* shortform dir entry */
689 xfs_dir2_sf_hdr_t *sfp; /* shortform structure */
693 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
694 offset = XFS_DIR2_DATA_FIRST_OFFSET;
695 ino = xfs_dir2_sf_get_parent_ino(sfp);
696 i8count = ino > XFS_DIR2_MAX_SHORT_INUM;
698 for (i = 0, sfep = xfs_dir2_sf_firstentry(sfp);
700 i++, sfep = xfs_dir2_sf_nextentry(sfp, sfep)) {
701 ASSERT(xfs_dir2_sf_get_offset(sfep) >= offset);
702 ino = xfs_dir2_sfe_get_ino(sfp, sfep);
703 i8count += ino > XFS_DIR2_MAX_SHORT_INUM;
705 xfs_dir2_sf_get_offset(sfep) +
706 xfs_dir2_data_entsize(sfep->namelen);
708 ASSERT(i8count == sfp->i8count);
709 ASSERT(XFS_BIG_INUMS || i8count == 0);
710 ASSERT((char *)sfep - (char *)sfp == dp->i_d.di_size);
712 (sfp->count + 2) * (uint)sizeof(xfs_dir2_leaf_entry_t) +
713 (uint)sizeof(xfs_dir2_block_tail_t) <=
714 dp->i_mount->m_dirblksize);
719 * Create a new (shortform) directory.
721 int /* error, always 0 */
723 xfs_da_args_t *args, /* operation arguments */
724 xfs_ino_t pino) /* parent inode number */
726 xfs_inode_t *dp; /* incore directory inode */
727 int i8count; /* parent inode is an 8-byte number */
728 xfs_dir2_sf_hdr_t *sfp; /* shortform structure */
729 int size; /* directory size */
731 trace_xfs_dir2_sf_create(args);
736 ASSERT(dp->i_d.di_size == 0);
738 * If it's currently a zero-length extent file,
739 * convert it to local format.
741 if (dp->i_d.di_format == XFS_DINODE_FMT_EXTENTS) {
742 dp->i_df.if_flags &= ~XFS_IFEXTENTS; /* just in case */
743 dp->i_d.di_format = XFS_DINODE_FMT_LOCAL;
744 xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE);
745 dp->i_df.if_flags |= XFS_IFINLINE;
747 ASSERT(dp->i_df.if_flags & XFS_IFINLINE);
748 ASSERT(dp->i_df.if_bytes == 0);
749 i8count = pino > XFS_DIR2_MAX_SHORT_INUM;
750 size = xfs_dir2_sf_hdr_size(i8count);
752 * Make a buffer for the data.
754 xfs_idata_realloc(dp, size, XFS_DATA_FORK);
756 * Fill in the header,
758 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
759 sfp->i8count = i8count;
761 * Now can put in the inode number, since i8count is set.
763 xfs_dir2_sf_put_parent_ino(sfp, pino);
765 dp->i_d.di_size = size;
766 xfs_dir2_sf_check(args);
767 xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE | XFS_ILOG_DDATA);
772 xfs_dir2_sf_getdents(
773 xfs_inode_t *dp, /* incore directory inode */
778 int i; /* shortform entry number */
779 xfs_mount_t *mp; /* filesystem mount point */
780 xfs_dir2_dataptr_t off; /* current entry's offset */
781 xfs_dir2_sf_entry_t *sfep; /* shortform directory entry */
782 xfs_dir2_sf_hdr_t *sfp; /* shortform structure */
783 xfs_dir2_dataptr_t dot_offset;
784 xfs_dir2_dataptr_t dotdot_offset;
789 ASSERT(dp->i_df.if_flags & XFS_IFINLINE);
791 * Give up if the directory is way too short.
793 if (dp->i_d.di_size < offsetof(xfs_dir2_sf_hdr_t, parent)) {
794 ASSERT(XFS_FORCED_SHUTDOWN(mp));
795 return XFS_ERROR(EIO);
798 ASSERT(dp->i_df.if_bytes == dp->i_d.di_size);
799 ASSERT(dp->i_df.if_u1.if_data != NULL);
801 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
803 ASSERT(dp->i_d.di_size >= xfs_dir2_sf_hdr_size(sfp->i8count));
806 * If the block number in the offset is out of range, we're done.
808 if (xfs_dir2_dataptr_to_db(mp, *offset) > mp->m_dirdatablk)
812 * Precalculate offsets for . and .. as we will always need them.
814 * XXX(hch): the second argument is sometimes 0 and sometimes
817 dot_offset = xfs_dir2_db_off_to_dataptr(mp, mp->m_dirdatablk,
818 XFS_DIR2_DATA_DOT_OFFSET);
819 dotdot_offset = xfs_dir2_db_off_to_dataptr(mp, mp->m_dirdatablk,
820 XFS_DIR2_DATA_DOTDOT_OFFSET);
823 * Put . entry unless we're starting past it.
825 if (*offset <= dot_offset) {
826 if (filldir(dirent, ".", 1, dot_offset & 0x7fffffff, dp->i_ino, DT_DIR)) {
827 *offset = dot_offset & 0x7fffffff;
833 * Put .. entry unless we're starting past it.
835 if (*offset <= dotdot_offset) {
836 ino = xfs_dir2_sf_get_parent_ino(sfp);
837 if (filldir(dirent, "..", 2, dotdot_offset & 0x7fffffff, ino, DT_DIR)) {
838 *offset = dotdot_offset & 0x7fffffff;
844 * Loop while there are more entries and put'ing works.
846 sfep = xfs_dir2_sf_firstentry(sfp);
847 for (i = 0; i < sfp->count; i++) {
848 off = xfs_dir2_db_off_to_dataptr(mp, mp->m_dirdatablk,
849 xfs_dir2_sf_get_offset(sfep));
852 sfep = xfs_dir2_sf_nextentry(sfp, sfep);
856 ino = xfs_dir2_sfe_get_ino(sfp, sfep);
857 if (filldir(dirent, (char *)sfep->name, sfep->namelen,
858 off & 0x7fffffff, ino, DT_UNKNOWN)) {
859 *offset = off & 0x7fffffff;
862 sfep = xfs_dir2_sf_nextentry(sfp, sfep);
865 *offset = xfs_dir2_db_off_to_dataptr(mp, mp->m_dirdatablk + 1, 0) &
871 * Lookup an entry in a shortform directory.
872 * Returns EEXIST if found, ENOENT if not found.
876 xfs_da_args_t *args) /* operation arguments */
878 xfs_inode_t *dp; /* incore directory inode */
879 int i; /* entry index */
881 xfs_dir2_sf_entry_t *sfep; /* shortform directory entry */
882 xfs_dir2_sf_hdr_t *sfp; /* shortform structure */
883 enum xfs_dacmp cmp; /* comparison result */
884 xfs_dir2_sf_entry_t *ci_sfep; /* case-insens. entry */
886 trace_xfs_dir2_sf_lookup(args);
888 xfs_dir2_sf_check(args);
891 ASSERT(dp->i_df.if_flags & XFS_IFINLINE);
893 * Bail out if the directory is way too short.
895 if (dp->i_d.di_size < offsetof(xfs_dir2_sf_hdr_t, parent)) {
896 ASSERT(XFS_FORCED_SHUTDOWN(dp->i_mount));
897 return XFS_ERROR(EIO);
899 ASSERT(dp->i_df.if_bytes == dp->i_d.di_size);
900 ASSERT(dp->i_df.if_u1.if_data != NULL);
901 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
902 ASSERT(dp->i_d.di_size >= xfs_dir2_sf_hdr_size(sfp->i8count));
906 if (args->namelen == 1 && args->name[0] == '.') {
907 args->inumber = dp->i_ino;
908 args->cmpresult = XFS_CMP_EXACT;
909 return XFS_ERROR(EEXIST);
912 * Special case for ..
914 if (args->namelen == 2 &&
915 args->name[0] == '.' && args->name[1] == '.') {
916 args->inumber = xfs_dir2_sf_get_parent_ino(sfp);
917 args->cmpresult = XFS_CMP_EXACT;
918 return XFS_ERROR(EEXIST);
921 * Loop over all the entries trying to match ours.
924 for (i = 0, sfep = xfs_dir2_sf_firstentry(sfp); i < sfp->count;
925 i++, sfep = xfs_dir2_sf_nextentry(sfp, sfep)) {
927 * Compare name and if it's an exact match, return the inode
928 * number. If it's the first case-insensitive match, store the
929 * inode number and continue looking for an exact match.
931 cmp = dp->i_mount->m_dirnameops->compname(args, sfep->name,
933 if (cmp != XFS_CMP_DIFFERENT && cmp != args->cmpresult) {
934 args->cmpresult = cmp;
935 args->inumber = xfs_dir2_sfe_get_ino(sfp, sfep);
936 if (cmp == XFS_CMP_EXACT)
937 return XFS_ERROR(EEXIST);
941 ASSERT(args->op_flags & XFS_DA_OP_OKNOENT);
943 * Here, we can only be doing a lookup (not a rename or replace).
944 * If a case-insensitive match was not found, return ENOENT.
947 return XFS_ERROR(ENOENT);
948 /* otherwise process the CI match as required by the caller */
949 error = xfs_dir_cilookup_result(args, ci_sfep->name, ci_sfep->namelen);
950 return XFS_ERROR(error);
954 * Remove an entry from a shortform directory.
957 xfs_dir2_sf_removename(
960 int byteoff; /* offset of removed entry */
961 xfs_inode_t *dp; /* incore directory inode */
962 int entsize; /* this entry's size */
963 int i; /* shortform entry index */
964 int newsize; /* new inode size */
965 int oldsize; /* old inode size */
966 xfs_dir2_sf_entry_t *sfep; /* shortform directory entry */
967 xfs_dir2_sf_hdr_t *sfp; /* shortform structure */
969 trace_xfs_dir2_sf_removename(args);
973 ASSERT(dp->i_df.if_flags & XFS_IFINLINE);
974 oldsize = (int)dp->i_d.di_size;
976 * Bail out if the directory is way too short.
978 if (oldsize < offsetof(xfs_dir2_sf_hdr_t, parent)) {
979 ASSERT(XFS_FORCED_SHUTDOWN(dp->i_mount));
980 return XFS_ERROR(EIO);
982 ASSERT(dp->i_df.if_bytes == oldsize);
983 ASSERT(dp->i_df.if_u1.if_data != NULL);
984 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
985 ASSERT(oldsize >= xfs_dir2_sf_hdr_size(sfp->i8count));
987 * Loop over the old directory entries.
988 * Find the one we're deleting.
990 for (i = 0, sfep = xfs_dir2_sf_firstentry(sfp); i < sfp->count;
991 i++, sfep = xfs_dir2_sf_nextentry(sfp, sfep)) {
992 if (xfs_da_compname(args, sfep->name, sfep->namelen) ==
994 ASSERT(xfs_dir2_sfe_get_ino(sfp, sfep) ==
1002 if (i == sfp->count)
1003 return XFS_ERROR(ENOENT);
1007 byteoff = (int)((char *)sfep - (char *)sfp);
1008 entsize = xfs_dir2_sf_entsize(sfp, args->namelen);
1009 newsize = oldsize - entsize;
1011 * Copy the part if any after the removed entry, sliding it down.
1013 if (byteoff + entsize < oldsize)
1014 memmove((char *)sfp + byteoff, (char *)sfp + byteoff + entsize,
1015 oldsize - (byteoff + entsize));
1017 * Fix up the header and file size.
1020 dp->i_d.di_size = newsize;
1022 * Reallocate, making it smaller.
1024 xfs_idata_realloc(dp, newsize - oldsize, XFS_DATA_FORK);
1025 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
1028 * Are we changing inode number size?
1030 if (args->inumber > XFS_DIR2_MAX_SHORT_INUM) {
1031 if (sfp->i8count == 1)
1032 xfs_dir2_sf_toino4(args);
1037 xfs_dir2_sf_check(args);
1038 xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE | XFS_ILOG_DDATA);
1043 * Replace the inode number of an entry in a shortform directory.
1046 xfs_dir2_sf_replace(
1047 xfs_da_args_t *args) /* operation arguments */
1049 xfs_inode_t *dp; /* incore directory inode */
1050 int i; /* entry index */
1051 #if XFS_BIG_INUMS || defined(DEBUG)
1052 xfs_ino_t ino=0; /* entry old inode number */
1055 int i8elevated; /* sf_toino8 set i8count=1 */
1057 xfs_dir2_sf_entry_t *sfep; /* shortform directory entry */
1058 xfs_dir2_sf_hdr_t *sfp; /* shortform structure */
1060 trace_xfs_dir2_sf_replace(args);
1064 ASSERT(dp->i_df.if_flags & XFS_IFINLINE);
1066 * Bail out if the shortform directory is way too small.
1068 if (dp->i_d.di_size < offsetof(xfs_dir2_sf_hdr_t, parent)) {
1069 ASSERT(XFS_FORCED_SHUTDOWN(dp->i_mount));
1070 return XFS_ERROR(EIO);
1072 ASSERT(dp->i_df.if_bytes == dp->i_d.di_size);
1073 ASSERT(dp->i_df.if_u1.if_data != NULL);
1074 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
1075 ASSERT(dp->i_d.di_size >= xfs_dir2_sf_hdr_size(sfp->i8count));
1078 * New inode number is large, and need to convert to 8-byte inodes.
1080 if (args->inumber > XFS_DIR2_MAX_SHORT_INUM && sfp->i8count == 0) {
1081 int error; /* error return value */
1082 int newsize; /* new inode size */
1087 ((uint)sizeof(xfs_dir2_ino8_t) -
1088 (uint)sizeof(xfs_dir2_ino4_t));
1090 * Won't fit as shortform, convert to block then do replace.
1092 if (newsize > XFS_IFORK_DSIZE(dp)) {
1093 error = xfs_dir2_sf_to_block(args);
1097 return xfs_dir2_block_replace(args);
1100 * Still fits, convert to 8-byte now.
1102 xfs_dir2_sf_toino8(args);
1104 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
1108 ASSERT(args->namelen != 1 || args->name[0] != '.');
1110 * Replace ..'s entry.
1112 if (args->namelen == 2 &&
1113 args->name[0] == '.' && args->name[1] == '.') {
1114 #if XFS_BIG_INUMS || defined(DEBUG)
1115 ino = xfs_dir2_sf_get_parent_ino(sfp);
1116 ASSERT(args->inumber != ino);
1118 xfs_dir2_sf_put_parent_ino(sfp, args->inumber);
1121 * Normal entry, look for the name.
1124 for (i = 0, sfep = xfs_dir2_sf_firstentry(sfp);
1126 i++, sfep = xfs_dir2_sf_nextentry(sfp, sfep)) {
1127 if (xfs_da_compname(args, sfep->name, sfep->namelen) ==
1129 #if XFS_BIG_INUMS || defined(DEBUG)
1130 ino = xfs_dir2_sfe_get_ino(sfp, sfep);
1131 ASSERT(args->inumber != ino);
1133 xfs_dir2_sfe_put_ino(sfp, sfep, args->inumber);
1140 if (i == sfp->count) {
1141 ASSERT(args->op_flags & XFS_DA_OP_OKNOENT);
1144 xfs_dir2_sf_toino4(args);
1146 return XFS_ERROR(ENOENT);
1151 * See if the old number was large, the new number is small.
1153 if (ino > XFS_DIR2_MAX_SHORT_INUM &&
1154 args->inumber <= XFS_DIR2_MAX_SHORT_INUM) {
1156 * And the old count was one, so need to convert to small.
1158 if (sfp->i8count == 1)
1159 xfs_dir2_sf_toino4(args);
1164 * See if the old number was small, the new number is large.
1166 if (ino <= XFS_DIR2_MAX_SHORT_INUM &&
1167 args->inumber > XFS_DIR2_MAX_SHORT_INUM) {
1169 * add to the i8count unless we just converted to 8-byte
1170 * inodes (which does an implied i8count = 1)
1172 ASSERT(sfp->i8count != 0);
1177 xfs_dir2_sf_check(args);
1178 xfs_trans_log_inode(args->trans, dp, XFS_ILOG_DDATA);
1184 * Convert from 8-byte inode numbers to 4-byte inode numbers.
1185 * The last 8-byte inode number is gone, but the count is still 1.
1189 xfs_da_args_t *args) /* operation arguments */
1191 char *buf; /* old dir's buffer */
1192 xfs_inode_t *dp; /* incore directory inode */
1193 int i; /* entry index */
1194 int newsize; /* new inode size */
1195 xfs_dir2_sf_entry_t *oldsfep; /* old sf entry */
1196 xfs_dir2_sf_hdr_t *oldsfp; /* old sf directory */
1197 int oldsize; /* old inode size */
1198 xfs_dir2_sf_entry_t *sfep; /* new sf entry */
1199 xfs_dir2_sf_hdr_t *sfp; /* new sf directory */
1201 trace_xfs_dir2_sf_toino4(args);
1206 * Copy the old directory to the buffer.
1207 * Then nuke it from the inode, and add the new buffer to the inode.
1208 * Don't want xfs_idata_realloc copying the data here.
1210 oldsize = dp->i_df.if_bytes;
1211 buf = kmem_alloc(oldsize, KM_SLEEP);
1212 oldsfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
1213 ASSERT(oldsfp->i8count == 1);
1214 memcpy(buf, oldsfp, oldsize);
1216 * Compute the new inode size.
1220 (oldsfp->count + 1) *
1221 ((uint)sizeof(xfs_dir2_ino8_t) - (uint)sizeof(xfs_dir2_ino4_t));
1222 xfs_idata_realloc(dp, -oldsize, XFS_DATA_FORK);
1223 xfs_idata_realloc(dp, newsize, XFS_DATA_FORK);
1225 * Reset our pointers, the data has moved.
1227 oldsfp = (xfs_dir2_sf_hdr_t *)buf;
1228 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
1230 * Fill in the new header.
1232 sfp->count = oldsfp->count;
1234 xfs_dir2_sf_put_parent_ino(sfp, xfs_dir2_sf_get_parent_ino(oldsfp));
1236 * Copy the entries field by field.
1238 for (i = 0, sfep = xfs_dir2_sf_firstentry(sfp),
1239 oldsfep = xfs_dir2_sf_firstentry(oldsfp);
1241 i++, sfep = xfs_dir2_sf_nextentry(sfp, sfep),
1242 oldsfep = xfs_dir2_sf_nextentry(oldsfp, oldsfep)) {
1243 sfep->namelen = oldsfep->namelen;
1244 sfep->offset = oldsfep->offset;
1245 memcpy(sfep->name, oldsfep->name, sfep->namelen);
1246 xfs_dir2_sfe_put_ino(sfp, sfep,
1247 xfs_dir2_sfe_get_ino(oldsfp, oldsfep));
1250 * Clean up the inode.
1253 dp->i_d.di_size = newsize;
1254 xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE | XFS_ILOG_DDATA);
1258 * Convert from 4-byte inode numbers to 8-byte inode numbers.
1259 * The new 8-byte inode number is not there yet, we leave with the
1260 * count 1 but no corresponding entry.
1264 xfs_da_args_t *args) /* operation arguments */
1266 char *buf; /* old dir's buffer */
1267 xfs_inode_t *dp; /* incore directory inode */
1268 int i; /* entry index */
1269 int newsize; /* new inode size */
1270 xfs_dir2_sf_entry_t *oldsfep; /* old sf entry */
1271 xfs_dir2_sf_hdr_t *oldsfp; /* old sf directory */
1272 int oldsize; /* old inode size */
1273 xfs_dir2_sf_entry_t *sfep; /* new sf entry */
1274 xfs_dir2_sf_hdr_t *sfp; /* new sf directory */
1276 trace_xfs_dir2_sf_toino8(args);
1281 * Copy the old directory to the buffer.
1282 * Then nuke it from the inode, and add the new buffer to the inode.
1283 * Don't want xfs_idata_realloc copying the data here.
1285 oldsize = dp->i_df.if_bytes;
1286 buf = kmem_alloc(oldsize, KM_SLEEP);
1287 oldsfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
1288 ASSERT(oldsfp->i8count == 0);
1289 memcpy(buf, oldsfp, oldsize);
1291 * Compute the new inode size.
1295 (oldsfp->count + 1) *
1296 ((uint)sizeof(xfs_dir2_ino8_t) - (uint)sizeof(xfs_dir2_ino4_t));
1297 xfs_idata_realloc(dp, -oldsize, XFS_DATA_FORK);
1298 xfs_idata_realloc(dp, newsize, XFS_DATA_FORK);
1300 * Reset our pointers, the data has moved.
1302 oldsfp = (xfs_dir2_sf_hdr_t *)buf;
1303 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
1305 * Fill in the new header.
1307 sfp->count = oldsfp->count;
1309 xfs_dir2_sf_put_parent_ino(sfp, xfs_dir2_sf_get_parent_ino(oldsfp));
1311 * Copy the entries field by field.
1313 for (i = 0, sfep = xfs_dir2_sf_firstentry(sfp),
1314 oldsfep = xfs_dir2_sf_firstentry(oldsfp);
1316 i++, sfep = xfs_dir2_sf_nextentry(sfp, sfep),
1317 oldsfep = xfs_dir2_sf_nextentry(oldsfp, oldsfep)) {
1318 sfep->namelen = oldsfep->namelen;
1319 sfep->offset = oldsfep->offset;
1320 memcpy(sfep->name, oldsfep->name, sfep->namelen);
1321 xfs_dir2_sfe_put_ino(sfp, sfep,
1322 xfs_dir2_sfe_get_ino(oldsfp, oldsfep));
1325 * Clean up the inode.
1328 dp->i_d.di_size = newsize;
1329 xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE | XFS_ILOG_DDATA);
1331 #endif /* XFS_BIG_INUMS */