2 * Copyright (c) 2000-2003 Silicon Graphics, Inc. All Rights Reserved.
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of version 2 of the GNU General Public License as
6 * published by the Free Software Foundation.
8 * This program is distributed in the hope that it would be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12 * Further, this software is distributed without any warranty that it is
13 * free of the rightful claim of any third person regarding infringement
14 * or the like. Any license provided herein, whether implied or
15 * otherwise, applies only to this software file. Patent licenses, if
16 * any, provided herein do not apply to combinations of this program with
17 * other software, or any other product whatsoever.
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write the Free Software Foundation, Inc., 59
21 * Temple Place - Suite 330, Boston MA 02111-1307, USA.
23 * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
24 * Mountain View, CA 94043, or:
28 * For further information regarding this notice, see:
30 * http://oss.sgi.com/projects/GenInfo/SGIGPLNoticeExplan/
35 * Shortform directory implementation for v2 directories.
40 #include "xfs_macros.h"
41 #include "xfs_types.h"
44 #include "xfs_trans.h"
48 #include "xfs_dmapi.h"
49 #include "xfs_mount.h"
50 #include "xfs_bmap_btree.h"
51 #include "xfs_attr_sf.h"
52 #include "xfs_dir_sf.h"
53 #include "xfs_dir2_sf.h"
54 #include "xfs_dinode.h"
55 #include "xfs_inode_item.h"
56 #include "xfs_inode.h"
57 #include "xfs_da_btree.h"
58 #include "xfs_dir_leaf.h"
59 #include "xfs_error.h"
60 #include "xfs_dir2_data.h"
61 #include "xfs_dir2_leaf.h"
62 #include "xfs_dir2_block.h"
63 #include "xfs_dir2_trace.h"
66 * Prototypes for internal functions.
68 static void xfs_dir2_sf_addname_easy(xfs_da_args_t *args,
69 xfs_dir2_sf_entry_t *sfep,
70 xfs_dir2_data_aoff_t offset,
72 static void xfs_dir2_sf_addname_hard(xfs_da_args_t *args, int objchange,
74 static int xfs_dir2_sf_addname_pick(xfs_da_args_t *args, int objchange,
75 xfs_dir2_sf_entry_t **sfepp,
76 xfs_dir2_data_aoff_t *offsetp);
78 static void xfs_dir2_sf_check(xfs_da_args_t *args);
80 #define xfs_dir2_sf_check(args)
83 static void xfs_dir2_sf_toino4(xfs_da_args_t *args);
84 static void xfs_dir2_sf_toino8(xfs_da_args_t *args);
85 #endif /* XFS_BIG_INUMS */
88 * Given a block directory (dp/block), calculate its size as a shortform (sf)
89 * directory and a header for the sf directory, if it will fit it the
90 * space currently present in the inode. If it won't fit, the output
91 * size is too big (but not accurate).
93 int /* size for sf form */
94 xfs_dir2_block_sfsize(
95 xfs_inode_t *dp, /* incore inode pointer */
96 xfs_dir2_block_t *block, /* block directory data */
97 xfs_dir2_sf_hdr_t *sfhp) /* output: header for sf form */
99 xfs_dir2_dataptr_t addr; /* data entry address */
100 xfs_dir2_leaf_entry_t *blp; /* leaf area of the block */
101 xfs_dir2_block_tail_t *btp; /* tail area of the block */
102 int count; /* shortform entry count */
103 xfs_dir2_data_entry_t *dep; /* data entry in the block */
104 int i; /* block entry index */
105 int i8count; /* count of big-inode entries */
106 int isdot; /* entry is "." */
107 int isdotdot; /* entry is ".." */
108 xfs_mount_t *mp; /* mount structure pointer */
109 int namelen; /* total name bytes */
110 xfs_ino_t parent; /* parent inode number */
111 int size=0; /* total computed size */
115 count = i8count = namelen = 0;
116 btp = XFS_DIR2_BLOCK_TAIL_P(mp, block);
117 blp = XFS_DIR2_BLOCK_LEAF_P(btp);
120 * Iterate over the block's data entries by using the leaf pointers.
122 for (i = 0; i < INT_GET(btp->count, ARCH_CONVERT); i++) {
123 if ((addr = INT_GET(blp[i].address, ARCH_CONVERT)) == XFS_DIR2_NULL_DATAPTR)
126 * Calculate the pointer to the entry at hand.
128 dep = (xfs_dir2_data_entry_t *)
129 ((char *)block + XFS_DIR2_DATAPTR_TO_OFF(mp, addr));
131 * Detect . and .., so we can special-case them.
132 * . is not included in sf directories.
133 * .. is included by just the parent inode number.
135 isdot = dep->namelen == 1 && dep->name[0] == '.';
138 dep->name[0] == '.' && dep->name[1] == '.';
141 i8count += INT_GET(dep->inumber, ARCH_CONVERT) > XFS_DIR2_MAX_SHORT_INUM;
143 if (!isdot && !isdotdot) {
145 namelen += dep->namelen;
147 parent = INT_GET(dep->inumber, ARCH_CONVERT);
149 * Calculate the new size, see if we should give up yet.
151 size = XFS_DIR2_SF_HDR_SIZE(i8count) + /* header */
152 count + /* namelen */
153 count * (uint)sizeof(xfs_dir2_sf_off_t) + /* offset */
155 (i8count ? /* inumber */
156 (uint)sizeof(xfs_dir2_ino8_t) * count :
157 (uint)sizeof(xfs_dir2_ino4_t) * count);
158 if (size > XFS_IFORK_DSIZE(dp))
159 return size; /* size value is a failure */
162 * Create the output header, if it worked.
165 sfhp->i8count = i8count;
166 XFS_DIR2_SF_PUT_INUMBER((xfs_dir2_sf_t *)sfhp, &parent, &sfhp->parent);
171 * Convert a block format directory to shortform.
172 * Caller has already checked that it will fit, and built us a header.
175 xfs_dir2_block_to_sf(
176 xfs_da_args_t *args, /* operation arguments */
177 xfs_dabuf_t *bp, /* block buffer */
178 int size, /* shortform directory size */
179 xfs_dir2_sf_hdr_t *sfhp) /* shortform directory hdr */
181 xfs_dir2_block_t *block; /* block structure */
182 xfs_dir2_block_tail_t *btp; /* block tail pointer */
183 xfs_dir2_data_entry_t *dep; /* data entry pointer */
184 xfs_inode_t *dp; /* incore directory inode */
185 xfs_dir2_data_unused_t *dup; /* unused data pointer */
186 char *endptr; /* end of data entries */
187 int error; /* error return value */
188 int logflags; /* inode logging flags */
189 xfs_mount_t *mp; /* filesystem mount point */
190 char *ptr; /* current data pointer */
191 xfs_dir2_sf_entry_t *sfep; /* shortform entry */
192 xfs_dir2_sf_t *sfp; /* shortform structure */
195 xfs_dir2_trace_args_sb("block_to_sf", args, size, bp);
200 * Make a copy of the block data, so we can shrink the inode
201 * and add local data.
203 block = kmem_alloc(mp->m_dirblksize, KM_SLEEP);
204 memcpy(block, bp->data, mp->m_dirblksize);
205 logflags = XFS_ILOG_CORE;
206 if ((error = xfs_dir2_shrink_inode(args, mp->m_dirdatablk, bp))) {
207 ASSERT(error != ENOSPC);
211 * The buffer is now unconditionally gone, whether
212 * xfs_dir2_shrink_inode worked or not.
214 * Convert the inode to local format.
216 dp->i_df.if_flags &= ~XFS_IFEXTENTS;
217 dp->i_df.if_flags |= XFS_IFINLINE;
218 dp->i_d.di_format = XFS_DINODE_FMT_LOCAL;
219 ASSERT(dp->i_df.if_bytes == 0);
220 xfs_idata_realloc(dp, size, XFS_DATA_FORK);
221 logflags |= XFS_ILOG_DDATA;
223 * Copy the header into the newly allocate local space.
225 sfp = (xfs_dir2_sf_t *)dp->i_df.if_u1.if_data;
226 memcpy(sfp, sfhp, XFS_DIR2_SF_HDR_SIZE(sfhp->i8count));
227 dp->i_d.di_size = size;
229 * Set up to loop over the block's entries.
231 btp = XFS_DIR2_BLOCK_TAIL_P(mp, block);
232 ptr = (char *)block->u;
233 endptr = (char *)XFS_DIR2_BLOCK_LEAF_P(btp);
234 sfep = XFS_DIR2_SF_FIRSTENTRY(sfp);
236 * Loop over the active and unused entries.
237 * Stop when we reach the leaf/tail portion of the block.
239 while (ptr < endptr) {
241 * If it's unused, just skip over it.
243 dup = (xfs_dir2_data_unused_t *)ptr;
244 if (INT_GET(dup->freetag, ARCH_CONVERT) == XFS_DIR2_DATA_FREE_TAG) {
245 ptr += INT_GET(dup->length, ARCH_CONVERT);
248 dep = (xfs_dir2_data_entry_t *)ptr;
252 if (dep->namelen == 1 && dep->name[0] == '.')
253 ASSERT(INT_GET(dep->inumber, ARCH_CONVERT) == dp->i_ino);
255 * Skip .., but make sure the inode number is right.
257 else if (dep->namelen == 2 &&
258 dep->name[0] == '.' && dep->name[1] == '.')
259 ASSERT(INT_GET(dep->inumber, ARCH_CONVERT) ==
260 XFS_DIR2_SF_GET_INUMBER(sfp, &sfp->hdr.parent));
262 * Normal entry, copy it into shortform.
265 sfep->namelen = dep->namelen;
266 XFS_DIR2_SF_PUT_OFFSET(sfep,
267 (xfs_dir2_data_aoff_t)
268 ((char *)dep - (char *)block));
269 memcpy(sfep->name, dep->name, dep->namelen);
270 temp=INT_GET(dep->inumber, ARCH_CONVERT);
271 XFS_DIR2_SF_PUT_INUMBER(sfp, &temp,
272 XFS_DIR2_SF_INUMBERP(sfep));
273 sfep = XFS_DIR2_SF_NEXTENTRY(sfp, sfep);
275 ptr += XFS_DIR2_DATA_ENTSIZE(dep->namelen);
277 ASSERT((char *)sfep - (char *)sfp == size);
278 xfs_dir2_sf_check(args);
280 xfs_trans_log_inode(args->trans, dp, logflags);
281 kmem_free(block, mp->m_dirblksize);
286 * Add a name to a shortform directory.
287 * There are two algorithms, "easy" and "hard" which we decide on
288 * before changing anything.
289 * Convert to block form if necessary, if the new entry won't fit.
293 xfs_da_args_t *args) /* operation arguments */
295 int add_entsize; /* size of the new entry */
296 xfs_inode_t *dp; /* incore directory inode */
297 int error; /* error return value */
298 int incr_isize; /* total change in size */
299 int new_isize; /* di_size after adding name */
300 int objchange; /* changing to 8-byte inodes */
301 xfs_dir2_data_aoff_t offset; /* offset for new entry */
302 int old_isize; /* di_size before adding name */
303 int pick; /* which algorithm to use */
304 xfs_dir2_sf_t *sfp; /* shortform structure */
305 xfs_dir2_sf_entry_t *sfep; /* shortform entry */
307 xfs_dir2_trace_args("sf_addname", args);
308 ASSERT(xfs_dir2_sf_lookup(args) == ENOENT);
310 ASSERT(dp->i_df.if_flags & XFS_IFINLINE);
312 * Make sure the shortform value has some of its header.
314 if (dp->i_d.di_size < offsetof(xfs_dir2_sf_hdr_t, parent)) {
315 ASSERT(XFS_FORCED_SHUTDOWN(dp->i_mount));
316 return XFS_ERROR(EIO);
318 ASSERT(dp->i_df.if_bytes == dp->i_d.di_size);
319 ASSERT(dp->i_df.if_u1.if_data != NULL);
320 sfp = (xfs_dir2_sf_t *)dp->i_df.if_u1.if_data;
321 ASSERT(dp->i_d.di_size >= XFS_DIR2_SF_HDR_SIZE(sfp->hdr.i8count));
323 * Compute entry (and change in) size.
325 add_entsize = XFS_DIR2_SF_ENTSIZE_BYNAME(sfp, args->namelen);
326 incr_isize = add_entsize;
330 * Do we have to change to 8 byte inodes?
332 if (args->inumber > XFS_DIR2_MAX_SHORT_INUM && sfp->hdr.i8count == 0) {
334 * Yes, adjust the entry size and the total size.
337 (uint)sizeof(xfs_dir2_ino8_t) -
338 (uint)sizeof(xfs_dir2_ino4_t);
340 (sfp->hdr.count + 2) *
341 ((uint)sizeof(xfs_dir2_ino8_t) -
342 (uint)sizeof(xfs_dir2_ino4_t));
346 old_isize = (int)dp->i_d.di_size;
347 new_isize = old_isize + incr_isize;
349 * Won't fit as shortform any more (due to size),
350 * or the pick routine says it won't (due to offset values).
352 if (new_isize > XFS_IFORK_DSIZE(dp) ||
354 xfs_dir2_sf_addname_pick(args, objchange, &sfep, &offset)) == 0) {
356 * Just checking or no space reservation, it doesn't fit.
358 if (args->justcheck || args->total == 0)
359 return XFS_ERROR(ENOSPC);
361 * Convert to block form then add the name.
363 error = xfs_dir2_sf_to_block(args);
366 return xfs_dir2_block_addname(args);
369 * Just checking, it fits.
374 * Do it the easy way - just add it at the end.
377 xfs_dir2_sf_addname_easy(args, sfep, offset, new_isize);
379 * Do it the hard way - look for a place to insert the new entry.
380 * Convert to 8 byte inode numbers first if necessary.
386 xfs_dir2_sf_toino8(args);
388 xfs_dir2_sf_addname_hard(args, objchange, new_isize);
390 xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE | XFS_ILOG_DDATA);
395 * Add the new entry the "easy" way.
396 * This is copying the old directory and adding the new entry at the end.
397 * Since it's sorted by "offset" we need room after the last offset
398 * that's already there, and then room to convert to a block directory.
399 * This is already checked by the pick routine.
402 xfs_dir2_sf_addname_easy(
403 xfs_da_args_t *args, /* operation arguments */
404 xfs_dir2_sf_entry_t *sfep, /* pointer to new entry */
405 xfs_dir2_data_aoff_t offset, /* offset to use for new ent */
406 int new_isize) /* new directory size */
408 int byteoff; /* byte offset in sf dir */
409 xfs_inode_t *dp; /* incore directory inode */
410 xfs_dir2_sf_t *sfp; /* shortform structure */
414 sfp = (xfs_dir2_sf_t *)dp->i_df.if_u1.if_data;
415 byteoff = (int)((char *)sfep - (char *)sfp);
417 * Grow the in-inode space.
419 xfs_idata_realloc(dp, XFS_DIR2_SF_ENTSIZE_BYNAME(sfp, args->namelen),
422 * Need to set up again due to realloc of the inode data.
424 sfp = (xfs_dir2_sf_t *)dp->i_df.if_u1.if_data;
425 sfep = (xfs_dir2_sf_entry_t *)((char *)sfp + byteoff);
427 * Fill in the new entry.
429 sfep->namelen = args->namelen;
430 XFS_DIR2_SF_PUT_OFFSET(sfep, offset);
431 memcpy(sfep->name, args->name, sfep->namelen);
432 XFS_DIR2_SF_PUT_INUMBER(sfp, &args->inumber,
433 XFS_DIR2_SF_INUMBERP(sfep));
435 * Update the header and inode.
439 if (args->inumber > XFS_DIR2_MAX_SHORT_INUM)
442 dp->i_d.di_size = new_isize;
443 xfs_dir2_sf_check(args);
447 * Add the new entry the "hard" way.
448 * The caller has already converted to 8 byte inode numbers if necessary,
449 * in which case we need to leave the i8count at 1.
450 * Find a hole that the new entry will fit into, and copy
451 * the first part of the entries, the new entry, and the last part of
456 xfs_dir2_sf_addname_hard(
457 xfs_da_args_t *args, /* operation arguments */
458 int objchange, /* changing inode number size */
459 int new_isize) /* new directory size */
461 int add_datasize; /* data size need for new ent */
462 char *buf; /* buffer for old */
463 xfs_inode_t *dp; /* incore directory inode */
464 int eof; /* reached end of old dir */
465 int nbytes; /* temp for byte copies */
466 xfs_dir2_data_aoff_t new_offset; /* next offset value */
467 xfs_dir2_data_aoff_t offset; /* current offset value */
468 int old_isize; /* previous di_size */
469 xfs_dir2_sf_entry_t *oldsfep; /* entry in original dir */
470 xfs_dir2_sf_t *oldsfp; /* original shortform dir */
471 xfs_dir2_sf_entry_t *sfep; /* entry in new dir */
472 xfs_dir2_sf_t *sfp; /* new shortform dir */
475 * Copy the old directory to the stack buffer.
479 sfp = (xfs_dir2_sf_t *)dp->i_df.if_u1.if_data;
480 old_isize = (int)dp->i_d.di_size;
481 buf = kmem_alloc(old_isize, KM_SLEEP);
482 oldsfp = (xfs_dir2_sf_t *)buf;
483 memcpy(oldsfp, sfp, old_isize);
485 * Loop over the old directory finding the place we're going
486 * to insert the new entry.
487 * If it's going to end up at the end then oldsfep will point there.
489 for (offset = XFS_DIR2_DATA_FIRST_OFFSET,
490 oldsfep = XFS_DIR2_SF_FIRSTENTRY(oldsfp),
491 add_datasize = XFS_DIR2_DATA_ENTSIZE(args->namelen),
492 eof = (char *)oldsfep == &buf[old_isize];
494 offset = new_offset + XFS_DIR2_DATA_ENTSIZE(oldsfep->namelen),
495 oldsfep = XFS_DIR2_SF_NEXTENTRY(oldsfp, oldsfep),
496 eof = (char *)oldsfep == &buf[old_isize]) {
497 new_offset = XFS_DIR2_SF_GET_OFFSET(oldsfep);
498 if (offset + add_datasize <= new_offset)
502 * Get rid of the old directory, then allocate space for
503 * the new one. We do this so xfs_idata_realloc won't copy
506 xfs_idata_realloc(dp, -old_isize, XFS_DATA_FORK);
507 xfs_idata_realloc(dp, new_isize, XFS_DATA_FORK);
509 * Reset the pointer since the buffer was reallocated.
511 sfp = (xfs_dir2_sf_t *)dp->i_df.if_u1.if_data;
513 * Copy the first part of the directory, including the header.
515 nbytes = (int)((char *)oldsfep - (char *)oldsfp);
516 memcpy(sfp, oldsfp, nbytes);
517 sfep = (xfs_dir2_sf_entry_t *)((char *)sfp + nbytes);
519 * Fill in the new entry, and update the header counts.
521 sfep->namelen = args->namelen;
522 XFS_DIR2_SF_PUT_OFFSET(sfep, offset);
523 memcpy(sfep->name, args->name, sfep->namelen);
524 XFS_DIR2_SF_PUT_INUMBER(sfp, &args->inumber,
525 XFS_DIR2_SF_INUMBERP(sfep));
528 if (args->inumber > XFS_DIR2_MAX_SHORT_INUM && !objchange)
532 * If there's more left to copy, do that.
535 sfep = XFS_DIR2_SF_NEXTENTRY(sfp, sfep);
536 memcpy(sfep, oldsfep, old_isize - nbytes);
538 kmem_free(buf, old_isize);
539 dp->i_d.di_size = new_isize;
540 xfs_dir2_sf_check(args);
544 * Decide if the new entry will fit at all.
545 * If it will fit, pick between adding the new entry to the end (easy)
546 * or somewhere else (hard).
547 * Return 0 (won't fit), 1 (easy), 2 (hard).
550 static int /* pick result */
551 xfs_dir2_sf_addname_pick(
552 xfs_da_args_t *args, /* operation arguments */
553 int objchange, /* inode # size changes */
554 xfs_dir2_sf_entry_t **sfepp, /* out(1): new entry ptr */
555 xfs_dir2_data_aoff_t *offsetp) /* out(1): new offset */
557 xfs_inode_t *dp; /* incore directory inode */
558 int holefit; /* found hole it will fit in */
559 int i; /* entry number */
560 xfs_mount_t *mp; /* filesystem mount point */
561 xfs_dir2_data_aoff_t offset; /* data block offset */
562 xfs_dir2_sf_entry_t *sfep; /* shortform entry */
563 xfs_dir2_sf_t *sfp; /* shortform structure */
564 int size; /* entry's data size */
565 int used; /* data bytes used */
570 sfp = (xfs_dir2_sf_t *)dp->i_df.if_u1.if_data;
571 size = XFS_DIR2_DATA_ENTSIZE(args->namelen);
572 offset = XFS_DIR2_DATA_FIRST_OFFSET;
573 sfep = XFS_DIR2_SF_FIRSTENTRY(sfp);
576 * Loop over sf entries.
577 * Keep track of data offset and whether we've seen a place
578 * to insert the new entry.
580 for (i = 0; i < sfp->hdr.count; i++) {
582 holefit = offset + size <= XFS_DIR2_SF_GET_OFFSET(sfep);
583 offset = XFS_DIR2_SF_GET_OFFSET(sfep) +
584 XFS_DIR2_DATA_ENTSIZE(sfep->namelen);
585 sfep = XFS_DIR2_SF_NEXTENTRY(sfp, sfep);
588 * Calculate data bytes used excluding the new entry, if this
589 * was a data block (block form directory).
592 (sfp->hdr.count + 3) * (uint)sizeof(xfs_dir2_leaf_entry_t) +
593 (uint)sizeof(xfs_dir2_block_tail_t);
595 * If it won't fit in a block form then we can't insert it,
596 * we'll go back, convert to block, then try the insert and convert
599 if (used + (holefit ? 0 : size) > mp->m_dirblksize)
602 * If changing the inode number size, do it the hard way.
609 ASSERT(objchange == 0);
612 * If it won't fit at the end then do it the hard way (use the hole).
614 if (used + size > mp->m_dirblksize)
617 * Do it the easy way.
626 * Check consistency of shortform directory, assert if bad.
630 xfs_da_args_t *args) /* operation arguments */
632 xfs_inode_t *dp; /* incore directory inode */
633 int i; /* entry number */
634 int i8count; /* number of big inode#s */
635 xfs_ino_t ino; /* entry inode number */
636 int offset; /* data offset */
637 xfs_dir2_sf_entry_t *sfep; /* shortform dir entry */
638 xfs_dir2_sf_t *sfp; /* shortform structure */
642 sfp = (xfs_dir2_sf_t *)dp->i_df.if_u1.if_data;
643 offset = XFS_DIR2_DATA_FIRST_OFFSET;
644 ino = XFS_DIR2_SF_GET_INUMBER(sfp, &sfp->hdr.parent);
645 i8count = ino > XFS_DIR2_MAX_SHORT_INUM;
647 for (i = 0, sfep = XFS_DIR2_SF_FIRSTENTRY(sfp);
649 i++, sfep = XFS_DIR2_SF_NEXTENTRY(sfp, sfep)) {
650 ASSERT(XFS_DIR2_SF_GET_OFFSET(sfep) >= offset);
651 ino = XFS_DIR2_SF_GET_INUMBER(sfp, XFS_DIR2_SF_INUMBERP(sfep));
652 i8count += ino > XFS_DIR2_MAX_SHORT_INUM;
654 XFS_DIR2_SF_GET_OFFSET(sfep) +
655 XFS_DIR2_DATA_ENTSIZE(sfep->namelen);
657 ASSERT(i8count == sfp->hdr.i8count);
658 ASSERT(XFS_BIG_INUMS || i8count == 0);
659 ASSERT((char *)sfep - (char *)sfp == dp->i_d.di_size);
661 (sfp->hdr.count + 2) * (uint)sizeof(xfs_dir2_leaf_entry_t) +
662 (uint)sizeof(xfs_dir2_block_tail_t) <=
663 dp->i_mount->m_dirblksize);
668 * Create a new (shortform) directory.
670 int /* error, always 0 */
672 xfs_da_args_t *args, /* operation arguments */
673 xfs_ino_t pino) /* parent inode number */
675 xfs_inode_t *dp; /* incore directory inode */
676 int i8count; /* parent inode is an 8-byte number */
677 xfs_dir2_sf_t *sfp; /* shortform structure */
678 int size; /* directory size */
680 xfs_dir2_trace_args_i("sf_create", args, pino);
684 ASSERT(dp->i_d.di_size == 0);
686 * If it's currently a zero-length extent file,
687 * convert it to local format.
689 if (dp->i_d.di_format == XFS_DINODE_FMT_EXTENTS) {
690 dp->i_df.if_flags &= ~XFS_IFEXTENTS; /* just in case */
691 dp->i_d.di_format = XFS_DINODE_FMT_LOCAL;
692 xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE);
693 dp->i_df.if_flags |= XFS_IFINLINE;
695 ASSERT(dp->i_df.if_flags & XFS_IFINLINE);
696 ASSERT(dp->i_df.if_bytes == 0);
697 i8count = pino > XFS_DIR2_MAX_SHORT_INUM;
698 size = XFS_DIR2_SF_HDR_SIZE(i8count);
700 * Make a buffer for the data.
702 xfs_idata_realloc(dp, size, XFS_DATA_FORK);
704 * Fill in the header,
706 sfp = (xfs_dir2_sf_t *)dp->i_df.if_u1.if_data;
707 sfp->hdr.i8count = i8count;
709 * Now can put in the inode number, since i8count is set.
711 XFS_DIR2_SF_PUT_INUMBER(sfp, &pino, &sfp->hdr.parent);
713 dp->i_d.di_size = size;
714 xfs_dir2_sf_check(args);
715 xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE | XFS_ILOG_DDATA);
720 xfs_dir2_sf_getdents(
721 xfs_inode_t *dp, /* incore directory inode */
722 uio_t *uio, /* caller's buffer control */
723 int *eofp, /* eof reached? (out) */
724 xfs_dirent_t *dbp, /* caller's buffer */
725 xfs_dir2_put_t put) /* abi's formatting function */
727 int error; /* error return value */
728 int i; /* shortform entry number */
729 xfs_mount_t *mp; /* filesystem mount point */
730 xfs_dir2_dataptr_t off; /* current entry's offset */
731 xfs_dir2_put_args_t p; /* arg package for put rtn */
732 xfs_dir2_sf_entry_t *sfep; /* shortform directory entry */
733 xfs_dir2_sf_t *sfp; /* shortform structure */
734 xfs_off_t dir_offset;
738 ASSERT(dp->i_df.if_flags & XFS_IFINLINE);
740 * Give up if the directory is way too short.
742 if (dp->i_d.di_size < offsetof(xfs_dir2_sf_hdr_t, parent)) {
743 ASSERT(XFS_FORCED_SHUTDOWN(mp));
744 return XFS_ERROR(EIO);
747 dir_offset = uio->uio_offset;
749 ASSERT(dp->i_df.if_bytes == dp->i_d.di_size);
750 ASSERT(dp->i_df.if_u1.if_data != NULL);
752 sfp = (xfs_dir2_sf_t *)dp->i_df.if_u1.if_data;
754 ASSERT(dp->i_d.di_size >= XFS_DIR2_SF_HDR_SIZE(sfp->hdr.i8count));
757 * If the block number in the offset is out of range, we're done.
759 if (XFS_DIR2_DATAPTR_TO_DB(mp, dir_offset) > mp->m_dirdatablk) {
765 * Set up putargs structure.
771 * Put . entry unless we're starting past it.
774 XFS_DIR2_DB_OFF_TO_DATAPTR(mp, mp->m_dirdatablk,
775 XFS_DIR2_DATA_DOT_OFFSET)) {
776 p.cook = XFS_DIR2_DB_OFF_TO_DATAPTR(mp, 0,
777 XFS_DIR2_DATA_DOTDOT_OFFSET);
780 p.ino += mp->m_inoadd;
789 XFS_DIR2_DB_OFF_TO_DATAPTR(mp, mp->m_dirdatablk,
790 XFS_DIR2_DATA_DOT_OFFSET);
796 * Put .. entry unless we're starting past it.
799 XFS_DIR2_DB_OFF_TO_DATAPTR(mp, mp->m_dirdatablk,
800 XFS_DIR2_DATA_DOTDOT_OFFSET)) {
801 p.cook = XFS_DIR2_DB_OFF_TO_DATAPTR(mp, mp->m_dirdatablk,
802 XFS_DIR2_DATA_FIRST_OFFSET);
803 p.ino = XFS_DIR2_SF_GET_INUMBER(sfp, &sfp->hdr.parent);
805 p.ino += mp->m_inoadd;
814 XFS_DIR2_DB_OFF_TO_DATAPTR(mp, mp->m_dirdatablk,
815 XFS_DIR2_DATA_DOTDOT_OFFSET);
821 * Loop while there are more entries and put'ing works.
823 for (i = 0, sfep = XFS_DIR2_SF_FIRSTENTRY(sfp);
825 i++, sfep = XFS_DIR2_SF_NEXTENTRY(sfp, sfep)) {
827 off = XFS_DIR2_DB_OFF_TO_DATAPTR(mp, mp->m_dirdatablk,
828 XFS_DIR2_SF_GET_OFFSET(sfep));
830 if (dir_offset > off)
833 p.namelen = sfep->namelen;
835 p.cook = XFS_DIR2_DB_OFF_TO_DATAPTR(mp, mp->m_dirdatablk,
836 XFS_DIR2_SF_GET_OFFSET(sfep) +
837 XFS_DIR2_DATA_ENTSIZE(p.namelen));
839 p.ino = XFS_DIR2_SF_GET_INUMBER(sfp, XFS_DIR2_SF_INUMBERP(sfep));
841 p.ino += mp->m_inoadd;
843 p.name = (char *)sfep->name;
848 uio->uio_offset = off;
859 XFS_DIR2_DB_OFF_TO_DATAPTR(mp, mp->m_dirdatablk + 1, 0);
865 * Lookup an entry in a shortform directory.
866 * Returns EEXIST if found, ENOENT if not found.
870 xfs_da_args_t *args) /* operation arguments */
872 xfs_inode_t *dp; /* incore directory inode */
873 int i; /* entry index */
874 xfs_dir2_sf_entry_t *sfep; /* shortform directory entry */
875 xfs_dir2_sf_t *sfp; /* shortform structure */
877 xfs_dir2_trace_args("sf_lookup", args);
878 xfs_dir2_sf_check(args);
881 ASSERT(dp->i_df.if_flags & XFS_IFINLINE);
883 * Bail out if the directory is way too short.
885 if (dp->i_d.di_size < offsetof(xfs_dir2_sf_hdr_t, parent)) {
886 ASSERT(XFS_FORCED_SHUTDOWN(dp->i_mount));
887 return XFS_ERROR(EIO);
889 ASSERT(dp->i_df.if_bytes == dp->i_d.di_size);
890 ASSERT(dp->i_df.if_u1.if_data != NULL);
891 sfp = (xfs_dir2_sf_t *)dp->i_df.if_u1.if_data;
892 ASSERT(dp->i_d.di_size >= XFS_DIR2_SF_HDR_SIZE(sfp->hdr.i8count));
896 if (args->namelen == 1 && args->name[0] == '.') {
897 args->inumber = dp->i_ino;
898 return XFS_ERROR(EEXIST);
901 * Special case for ..
903 if (args->namelen == 2 &&
904 args->name[0] == '.' && args->name[1] == '.') {
905 args->inumber = XFS_DIR2_SF_GET_INUMBER(sfp, &sfp->hdr.parent);
906 return XFS_ERROR(EEXIST);
909 * Loop over all the entries trying to match ours.
911 for (i = 0, sfep = XFS_DIR2_SF_FIRSTENTRY(sfp);
913 i++, sfep = XFS_DIR2_SF_NEXTENTRY(sfp, sfep)) {
914 if (sfep->namelen == args->namelen &&
915 sfep->name[0] == args->name[0] &&
916 memcmp(args->name, sfep->name, args->namelen) == 0) {
918 XFS_DIR2_SF_GET_INUMBER(sfp,
919 XFS_DIR2_SF_INUMBERP(sfep));
920 return XFS_ERROR(EEXIST);
926 ASSERT(args->oknoent);
927 return XFS_ERROR(ENOENT);
931 * Remove an entry from a shortform directory.
934 xfs_dir2_sf_removename(
937 int byteoff; /* offset of removed entry */
938 xfs_inode_t *dp; /* incore directory inode */
939 int entsize; /* this entry's size */
940 int i; /* shortform entry index */
941 int newsize; /* new inode size */
942 int oldsize; /* old inode size */
943 xfs_dir2_sf_entry_t *sfep; /* shortform directory entry */
944 xfs_dir2_sf_t *sfp; /* shortform structure */
946 xfs_dir2_trace_args("sf_removename", args);
949 ASSERT(dp->i_df.if_flags & XFS_IFINLINE);
950 oldsize = (int)dp->i_d.di_size;
952 * Bail out if the directory is way too short.
954 if (oldsize < offsetof(xfs_dir2_sf_hdr_t, parent)) {
955 ASSERT(XFS_FORCED_SHUTDOWN(dp->i_mount));
956 return XFS_ERROR(EIO);
958 ASSERT(dp->i_df.if_bytes == oldsize);
959 ASSERT(dp->i_df.if_u1.if_data != NULL);
960 sfp = (xfs_dir2_sf_t *)dp->i_df.if_u1.if_data;
961 ASSERT(oldsize >= XFS_DIR2_SF_HDR_SIZE(sfp->hdr.i8count));
963 * Loop over the old directory entries.
964 * Find the one we're deleting.
966 for (i = 0, sfep = XFS_DIR2_SF_FIRSTENTRY(sfp);
968 i++, sfep = XFS_DIR2_SF_NEXTENTRY(sfp, sfep)) {
969 if (sfep->namelen == args->namelen &&
970 sfep->name[0] == args->name[0] &&
971 memcmp(sfep->name, args->name, args->namelen) == 0) {
972 ASSERT(XFS_DIR2_SF_GET_INUMBER(sfp,
973 XFS_DIR2_SF_INUMBERP(sfep)) ==
981 if (i == sfp->hdr.count) {
982 return XFS_ERROR(ENOENT);
987 byteoff = (int)((char *)sfep - (char *)sfp);
988 entsize = XFS_DIR2_SF_ENTSIZE_BYNAME(sfp, args->namelen);
989 newsize = oldsize - entsize;
991 * Copy the part if any after the removed entry, sliding it down.
993 if (byteoff + entsize < oldsize)
994 memmove((char *)sfp + byteoff, (char *)sfp + byteoff + entsize,
995 oldsize - (byteoff + entsize));
997 * Fix up the header and file size.
1000 dp->i_d.di_size = newsize;
1002 * Reallocate, making it smaller.
1004 xfs_idata_realloc(dp, newsize - oldsize, XFS_DATA_FORK);
1005 sfp = (xfs_dir2_sf_t *)dp->i_df.if_u1.if_data;
1008 * Are we changing inode number size?
1010 if (args->inumber > XFS_DIR2_MAX_SHORT_INUM) {
1011 if (sfp->hdr.i8count == 1)
1012 xfs_dir2_sf_toino4(args);
1017 xfs_dir2_sf_check(args);
1018 xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE | XFS_ILOG_DDATA);
1023 * Replace the inode number of an entry in a shortform directory.
1026 xfs_dir2_sf_replace(
1027 xfs_da_args_t *args) /* operation arguments */
1029 xfs_inode_t *dp; /* incore directory inode */
1030 int i; /* entry index */
1031 #if XFS_BIG_INUMS || defined(DEBUG)
1032 xfs_ino_t ino=0; /* entry old inode number */
1035 int i8elevated; /* sf_toino8 set i8count=1 */
1037 xfs_dir2_sf_entry_t *sfep; /* shortform directory entry */
1038 xfs_dir2_sf_t *sfp; /* shortform structure */
1040 xfs_dir2_trace_args("sf_replace", args);
1043 ASSERT(dp->i_df.if_flags & XFS_IFINLINE);
1045 * Bail out if the shortform directory is way too small.
1047 if (dp->i_d.di_size < offsetof(xfs_dir2_sf_hdr_t, parent)) {
1048 ASSERT(XFS_FORCED_SHUTDOWN(dp->i_mount));
1049 return XFS_ERROR(EIO);
1051 ASSERT(dp->i_df.if_bytes == dp->i_d.di_size);
1052 ASSERT(dp->i_df.if_u1.if_data != NULL);
1053 sfp = (xfs_dir2_sf_t *)dp->i_df.if_u1.if_data;
1054 ASSERT(dp->i_d.di_size >= XFS_DIR2_SF_HDR_SIZE(sfp->hdr.i8count));
1057 * New inode number is large, and need to convert to 8-byte inodes.
1059 if (args->inumber > XFS_DIR2_MAX_SHORT_INUM && sfp->hdr.i8count == 0) {
1060 int error; /* error return value */
1061 int newsize; /* new inode size */
1065 (sfp->hdr.count + 1) *
1066 ((uint)sizeof(xfs_dir2_ino8_t) -
1067 (uint)sizeof(xfs_dir2_ino4_t));
1069 * Won't fit as shortform, convert to block then do replace.
1071 if (newsize > XFS_IFORK_DSIZE(dp)) {
1072 error = xfs_dir2_sf_to_block(args);
1076 return xfs_dir2_block_replace(args);
1079 * Still fits, convert to 8-byte now.
1081 xfs_dir2_sf_toino8(args);
1083 sfp = (xfs_dir2_sf_t *)dp->i_df.if_u1.if_data;
1087 ASSERT(args->namelen != 1 || args->name[0] != '.');
1089 * Replace ..'s entry.
1091 if (args->namelen == 2 &&
1092 args->name[0] == '.' && args->name[1] == '.') {
1093 #if XFS_BIG_INUMS || defined(DEBUG)
1094 ino = XFS_DIR2_SF_GET_INUMBER(sfp, &sfp->hdr.parent);
1095 ASSERT(args->inumber != ino);
1097 XFS_DIR2_SF_PUT_INUMBER(sfp, &args->inumber, &sfp->hdr.parent);
1100 * Normal entry, look for the name.
1103 for (i = 0, sfep = XFS_DIR2_SF_FIRSTENTRY(sfp);
1105 i++, sfep = XFS_DIR2_SF_NEXTENTRY(sfp, sfep)) {
1106 if (sfep->namelen == args->namelen &&
1107 sfep->name[0] == args->name[0] &&
1108 memcmp(args->name, sfep->name, args->namelen) == 0) {
1109 #if XFS_BIG_INUMS || defined(DEBUG)
1110 ino = XFS_DIR2_SF_GET_INUMBER(sfp,
1111 XFS_DIR2_SF_INUMBERP(sfep));
1112 ASSERT(args->inumber != ino);
1114 XFS_DIR2_SF_PUT_INUMBER(sfp, &args->inumber,
1115 XFS_DIR2_SF_INUMBERP(sfep));
1122 if (i == sfp->hdr.count) {
1123 ASSERT(args->oknoent);
1126 xfs_dir2_sf_toino4(args);
1128 return XFS_ERROR(ENOENT);
1133 * See if the old number was large, the new number is small.
1135 if (ino > XFS_DIR2_MAX_SHORT_INUM &&
1136 args->inumber <= XFS_DIR2_MAX_SHORT_INUM) {
1138 * And the old count was one, so need to convert to small.
1140 if (sfp->hdr.i8count == 1)
1141 xfs_dir2_sf_toino4(args);
1146 * See if the old number was small, the new number is large.
1148 if (ino <= XFS_DIR2_MAX_SHORT_INUM &&
1149 args->inumber > XFS_DIR2_MAX_SHORT_INUM) {
1151 * add to the i8count unless we just converted to 8-byte
1152 * inodes (which does an implied i8count = 1)
1154 ASSERT(sfp->hdr.i8count != 0);
1159 xfs_dir2_sf_check(args);
1160 xfs_trans_log_inode(args->trans, dp, XFS_ILOG_DDATA);
1166 * Convert from 8-byte inode numbers to 4-byte inode numbers.
1167 * The last 8-byte inode number is gone, but the count is still 1.
1171 xfs_da_args_t *args) /* operation arguments */
1173 char *buf; /* old dir's buffer */
1174 xfs_inode_t *dp; /* incore directory inode */
1175 int i; /* entry index */
1176 xfs_ino_t ino; /* entry inode number */
1177 int newsize; /* new inode size */
1178 xfs_dir2_sf_entry_t *oldsfep; /* old sf entry */
1179 xfs_dir2_sf_t *oldsfp; /* old sf directory */
1180 int oldsize; /* old inode size */
1181 xfs_dir2_sf_entry_t *sfep; /* new sf entry */
1182 xfs_dir2_sf_t *sfp; /* new sf directory */
1184 xfs_dir2_trace_args("sf_toino4", args);
1188 * Copy the old directory to the buffer.
1189 * Then nuke it from the inode, and add the new buffer to the inode.
1190 * Don't want xfs_idata_realloc copying the data here.
1192 oldsize = dp->i_df.if_bytes;
1193 buf = kmem_alloc(oldsize, KM_SLEEP);
1194 oldsfp = (xfs_dir2_sf_t *)dp->i_df.if_u1.if_data;
1195 ASSERT(oldsfp->hdr.i8count == 1);
1196 memcpy(buf, oldsfp, oldsize);
1198 * Compute the new inode size.
1202 (oldsfp->hdr.count + 1) *
1203 ((uint)sizeof(xfs_dir2_ino8_t) - (uint)sizeof(xfs_dir2_ino4_t));
1204 xfs_idata_realloc(dp, -oldsize, XFS_DATA_FORK);
1205 xfs_idata_realloc(dp, newsize, XFS_DATA_FORK);
1207 * Reset our pointers, the data has moved.
1209 oldsfp = (xfs_dir2_sf_t *)buf;
1210 sfp = (xfs_dir2_sf_t *)dp->i_df.if_u1.if_data;
1212 * Fill in the new header.
1214 sfp->hdr.count = oldsfp->hdr.count;
1215 sfp->hdr.i8count = 0;
1216 ino = XFS_DIR2_SF_GET_INUMBER(oldsfp, &oldsfp->hdr.parent);
1217 XFS_DIR2_SF_PUT_INUMBER(sfp, &ino, &sfp->hdr.parent);
1219 * Copy the entries field by field.
1221 for (i = 0, sfep = XFS_DIR2_SF_FIRSTENTRY(sfp),
1222 oldsfep = XFS_DIR2_SF_FIRSTENTRY(oldsfp);
1224 i++, sfep = XFS_DIR2_SF_NEXTENTRY(sfp, sfep),
1225 oldsfep = XFS_DIR2_SF_NEXTENTRY(oldsfp, oldsfep)) {
1226 sfep->namelen = oldsfep->namelen;
1227 sfep->offset = oldsfep->offset;
1228 memcpy(sfep->name, oldsfep->name, sfep->namelen);
1229 ino = XFS_DIR2_SF_GET_INUMBER(oldsfp,
1230 XFS_DIR2_SF_INUMBERP(oldsfep));
1231 XFS_DIR2_SF_PUT_INUMBER(sfp, &ino, XFS_DIR2_SF_INUMBERP(sfep));
1234 * Clean up the inode.
1236 kmem_free(buf, oldsize);
1237 dp->i_d.di_size = newsize;
1238 xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE | XFS_ILOG_DDATA);
1242 * Convert from 4-byte inode numbers to 8-byte inode numbers.
1243 * The new 8-byte inode number is not there yet, we leave with the
1244 * count 1 but no corresponding entry.
1248 xfs_da_args_t *args) /* operation arguments */
1250 char *buf; /* old dir's buffer */
1251 xfs_inode_t *dp; /* incore directory inode */
1252 int i; /* entry index */
1253 xfs_ino_t ino; /* entry inode number */
1254 int newsize; /* new inode size */
1255 xfs_dir2_sf_entry_t *oldsfep; /* old sf entry */
1256 xfs_dir2_sf_t *oldsfp; /* old sf directory */
1257 int oldsize; /* old inode size */
1258 xfs_dir2_sf_entry_t *sfep; /* new sf entry */
1259 xfs_dir2_sf_t *sfp; /* new sf directory */
1261 xfs_dir2_trace_args("sf_toino8", args);
1265 * Copy the old directory to the buffer.
1266 * Then nuke it from the inode, and add the new buffer to the inode.
1267 * Don't want xfs_idata_realloc copying the data here.
1269 oldsize = dp->i_df.if_bytes;
1270 buf = kmem_alloc(oldsize, KM_SLEEP);
1271 oldsfp = (xfs_dir2_sf_t *)dp->i_df.if_u1.if_data;
1272 ASSERT(oldsfp->hdr.i8count == 0);
1273 memcpy(buf, oldsfp, oldsize);
1275 * Compute the new inode size.
1279 (oldsfp->hdr.count + 1) *
1280 ((uint)sizeof(xfs_dir2_ino8_t) - (uint)sizeof(xfs_dir2_ino4_t));
1281 xfs_idata_realloc(dp, -oldsize, XFS_DATA_FORK);
1282 xfs_idata_realloc(dp, newsize, XFS_DATA_FORK);
1284 * Reset our pointers, the data has moved.
1286 oldsfp = (xfs_dir2_sf_t *)buf;
1287 sfp = (xfs_dir2_sf_t *)dp->i_df.if_u1.if_data;
1289 * Fill in the new header.
1291 sfp->hdr.count = oldsfp->hdr.count;
1292 sfp->hdr.i8count = 1;
1293 ino = XFS_DIR2_SF_GET_INUMBER(oldsfp, &oldsfp->hdr.parent);
1294 XFS_DIR2_SF_PUT_INUMBER(sfp, &ino, &sfp->hdr.parent);
1296 * Copy the entries field by field.
1298 for (i = 0, sfep = XFS_DIR2_SF_FIRSTENTRY(sfp),
1299 oldsfep = XFS_DIR2_SF_FIRSTENTRY(oldsfp);
1301 i++, sfep = XFS_DIR2_SF_NEXTENTRY(sfp, sfep),
1302 oldsfep = XFS_DIR2_SF_NEXTENTRY(oldsfp, oldsfep)) {
1303 sfep->namelen = oldsfep->namelen;
1304 sfep->offset = oldsfep->offset;
1305 memcpy(sfep->name, oldsfep->name, sfep->namelen);
1306 ino = XFS_DIR2_SF_GET_INUMBER(oldsfp,
1307 XFS_DIR2_SF_INUMBERP(oldsfep));
1308 XFS_DIR2_SF_PUT_INUMBER(sfp, &ino, XFS_DIR2_SF_INUMBERP(sfep));
1311 * Clean up the inode.
1313 kmem_free(buf, oldsize);
1314 dp->i_d.di_size = newsize;
1315 xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE | XFS_ILOG_DDATA);
1317 #endif /* XFS_BIG_INUMS */