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/
34 #include "xfs_types.h"
37 #include "xfs_trans.h"
41 #include "xfs_dmapi.h"
42 #include "xfs_mount.h"
43 #include "xfs_da_btree.h"
44 #include "xfs_bmap_btree.h"
45 #include "xfs_dir_sf.h"
46 #include "xfs_dir2_sf.h"
47 #include "xfs_attr_sf.h"
48 #include "xfs_dinode.h"
49 #include "xfs_inode.h"
50 #include "xfs_inode_item.h"
51 #include "xfs_dir_leaf.h"
52 #include "xfs_error.h"
53 #include "xfs_dir2_data.h"
54 #include "xfs_dir2_leaf.h"
55 #include "xfs_dir2_block.h"
56 #include "xfs_dir2_trace.h"
59 * Prototypes for internal functions.
61 static void xfs_dir2_sf_addname_easy(xfs_da_args_t *args,
62 xfs_dir2_sf_entry_t *sfep,
63 xfs_dir2_data_aoff_t offset,
65 static void xfs_dir2_sf_addname_hard(xfs_da_args_t *args, int objchange,
67 static int xfs_dir2_sf_addname_pick(xfs_da_args_t *args, int objchange,
68 xfs_dir2_sf_entry_t **sfepp,
69 xfs_dir2_data_aoff_t *offsetp);
71 static void xfs_dir2_sf_check(xfs_da_args_t *args);
73 #define xfs_dir2_sf_check(args)
76 static void xfs_dir2_sf_toino4(xfs_da_args_t *args);
77 static void xfs_dir2_sf_toino8(xfs_da_args_t *args);
78 #endif /* XFS_BIG_INUMS */
81 * Given a block directory (dp/block), calculate its size as a shortform (sf)
82 * directory and a header for the sf directory, if it will fit it the
83 * space currently present in the inode. If it won't fit, the output
84 * size is too big (but not accurate).
86 int /* size for sf form */
87 xfs_dir2_block_sfsize(
88 xfs_inode_t *dp, /* incore inode pointer */
89 xfs_dir2_block_t *block, /* block directory data */
90 xfs_dir2_sf_hdr_t *sfhp) /* output: header for sf form */
92 xfs_dir2_dataptr_t addr; /* data entry address */
93 xfs_dir2_leaf_entry_t *blp; /* leaf area of the block */
94 xfs_dir2_block_tail_t *btp; /* tail area of the block */
95 int count; /* shortform entry count */
96 xfs_dir2_data_entry_t *dep; /* data entry in the block */
97 int i; /* block entry index */
98 int i8count; /* count of big-inode entries */
99 int isdot; /* entry is "." */
100 int isdotdot; /* entry is ".." */
101 xfs_mount_t *mp; /* mount structure pointer */
102 int namelen; /* total name bytes */
103 xfs_ino_t parent; /* parent inode number */
104 int size=0; /* total computed size */
108 count = i8count = namelen = 0;
109 btp = XFS_DIR2_BLOCK_TAIL_P(mp, block);
110 blp = XFS_DIR2_BLOCK_LEAF_P(btp);
113 * Iterate over the block's data entries by using the leaf pointers.
115 for (i = 0; i < INT_GET(btp->count, ARCH_CONVERT); i++) {
116 if ((addr = INT_GET(blp[i].address, ARCH_CONVERT)) == XFS_DIR2_NULL_DATAPTR)
119 * Calculate the pointer to the entry at hand.
121 dep = (xfs_dir2_data_entry_t *)
122 ((char *)block + XFS_DIR2_DATAPTR_TO_OFF(mp, addr));
124 * Detect . and .., so we can special-case them.
125 * . is not included in sf directories.
126 * .. is included by just the parent inode number.
128 isdot = dep->namelen == 1 && dep->name[0] == '.';
131 dep->name[0] == '.' && dep->name[1] == '.';
134 i8count += INT_GET(dep->inumber, ARCH_CONVERT) > XFS_DIR2_MAX_SHORT_INUM;
136 if (!isdot && !isdotdot) {
138 namelen += dep->namelen;
140 parent = INT_GET(dep->inumber, ARCH_CONVERT);
142 * Calculate the new size, see if we should give up yet.
144 size = XFS_DIR2_SF_HDR_SIZE(i8count) + /* header */
145 count + /* namelen */
146 count * (uint)sizeof(xfs_dir2_sf_off_t) + /* offset */
148 (i8count ? /* inumber */
149 (uint)sizeof(xfs_dir2_ino8_t) * count :
150 (uint)sizeof(xfs_dir2_ino4_t) * count);
151 if (size > XFS_IFORK_DSIZE(dp))
152 return size; /* size value is a failure */
155 * Create the output header, if it worked.
158 sfhp->i8count = i8count;
159 XFS_DIR2_SF_PUT_INUMBER((xfs_dir2_sf_t *)sfhp, &parent, &sfhp->parent);
164 * Convert a block format directory to shortform.
165 * Caller has already checked that it will fit, and built us a header.
168 xfs_dir2_block_to_sf(
169 xfs_da_args_t *args, /* operation arguments */
170 xfs_dabuf_t *bp, /* block buffer */
171 int size, /* shortform directory size */
172 xfs_dir2_sf_hdr_t *sfhp) /* shortform directory hdr */
174 xfs_dir2_block_t *block; /* block structure */
175 xfs_dir2_block_tail_t *btp; /* block tail pointer */
176 xfs_dir2_data_entry_t *dep; /* data entry pointer */
177 xfs_inode_t *dp; /* incore directory inode */
178 xfs_dir2_data_unused_t *dup; /* unused data pointer */
179 char *endptr; /* end of data entries */
180 int error; /* error return value */
181 int logflags; /* inode logging flags */
182 xfs_mount_t *mp; /* filesystem mount point */
183 char *ptr; /* current data pointer */
184 xfs_dir2_sf_entry_t *sfep; /* shortform entry */
185 xfs_dir2_sf_t *sfp; /* shortform structure */
188 xfs_dir2_trace_args_sb("block_to_sf", args, size, bp);
193 * Make a copy of the block data, so we can shrink the inode
194 * and add local data.
196 block = kmem_alloc(mp->m_dirblksize, KM_SLEEP);
197 memcpy(block, bp->data, mp->m_dirblksize);
198 logflags = XFS_ILOG_CORE;
199 if ((error = xfs_dir2_shrink_inode(args, mp->m_dirdatablk, bp))) {
200 ASSERT(error != ENOSPC);
204 * The buffer is now unconditionally gone, whether
205 * xfs_dir2_shrink_inode worked or not.
207 * Convert the inode to local format.
209 dp->i_df.if_flags &= ~XFS_IFEXTENTS;
210 dp->i_df.if_flags |= XFS_IFINLINE;
211 dp->i_d.di_format = XFS_DINODE_FMT_LOCAL;
212 ASSERT(dp->i_df.if_bytes == 0);
213 xfs_idata_realloc(dp, size, XFS_DATA_FORK);
214 logflags |= XFS_ILOG_DDATA;
216 * Copy the header into the newly allocate local space.
218 sfp = (xfs_dir2_sf_t *)dp->i_df.if_u1.if_data;
219 memcpy(sfp, sfhp, XFS_DIR2_SF_HDR_SIZE(sfhp->i8count));
220 dp->i_d.di_size = size;
222 * Set up to loop over the block's entries.
224 btp = XFS_DIR2_BLOCK_TAIL_P(mp, block);
225 ptr = (char *)block->u;
226 endptr = (char *)XFS_DIR2_BLOCK_LEAF_P(btp);
227 sfep = XFS_DIR2_SF_FIRSTENTRY(sfp);
229 * Loop over the active and unused entries.
230 * Stop when we reach the leaf/tail portion of the block.
232 while (ptr < endptr) {
234 * If it's unused, just skip over it.
236 dup = (xfs_dir2_data_unused_t *)ptr;
237 if (INT_GET(dup->freetag, ARCH_CONVERT) == XFS_DIR2_DATA_FREE_TAG) {
238 ptr += INT_GET(dup->length, ARCH_CONVERT);
241 dep = (xfs_dir2_data_entry_t *)ptr;
245 if (dep->namelen == 1 && dep->name[0] == '.')
246 ASSERT(INT_GET(dep->inumber, ARCH_CONVERT) == dp->i_ino);
248 * Skip .., but make sure the inode number is right.
250 else if (dep->namelen == 2 &&
251 dep->name[0] == '.' && dep->name[1] == '.')
252 ASSERT(INT_GET(dep->inumber, ARCH_CONVERT) ==
253 XFS_DIR2_SF_GET_INUMBER(sfp, &sfp->hdr.parent));
255 * Normal entry, copy it into shortform.
258 sfep->namelen = dep->namelen;
259 XFS_DIR2_SF_PUT_OFFSET(sfep,
260 (xfs_dir2_data_aoff_t)
261 ((char *)dep - (char *)block));
262 memcpy(sfep->name, dep->name, dep->namelen);
263 temp=INT_GET(dep->inumber, ARCH_CONVERT);
264 XFS_DIR2_SF_PUT_INUMBER(sfp, &temp,
265 XFS_DIR2_SF_INUMBERP(sfep));
266 sfep = XFS_DIR2_SF_NEXTENTRY(sfp, sfep);
268 ptr += XFS_DIR2_DATA_ENTSIZE(dep->namelen);
270 ASSERT((char *)sfep - (char *)sfp == size);
271 xfs_dir2_sf_check(args);
273 xfs_trans_log_inode(args->trans, dp, logflags);
274 kmem_free(block, mp->m_dirblksize);
279 * Add a name to a shortform directory.
280 * There are two algorithms, "easy" and "hard" which we decide on
281 * before changing anything.
282 * Convert to block form if necessary, if the new entry won't fit.
286 xfs_da_args_t *args) /* operation arguments */
288 int add_entsize; /* size of the new entry */
289 xfs_inode_t *dp; /* incore directory inode */
290 int error; /* error return value */
291 int incr_isize; /* total change in size */
292 int new_isize; /* di_size after adding name */
293 int objchange; /* changing to 8-byte inodes */
294 xfs_dir2_data_aoff_t offset; /* offset for new entry */
295 int old_isize; /* di_size before adding name */
296 int pick; /* which algorithm to use */
297 xfs_dir2_sf_t *sfp; /* shortform structure */
298 xfs_dir2_sf_entry_t *sfep; /* shortform entry */
300 xfs_dir2_trace_args("sf_addname", args);
301 ASSERT(xfs_dir2_sf_lookup(args) == ENOENT);
303 ASSERT(dp->i_df.if_flags & XFS_IFINLINE);
305 * Make sure the shortform value has some of its header.
307 if (dp->i_d.di_size < offsetof(xfs_dir2_sf_hdr_t, parent)) {
308 ASSERT(XFS_FORCED_SHUTDOWN(dp->i_mount));
309 return XFS_ERROR(EIO);
311 ASSERT(dp->i_df.if_bytes == dp->i_d.di_size);
312 ASSERT(dp->i_df.if_u1.if_data != NULL);
313 sfp = (xfs_dir2_sf_t *)dp->i_df.if_u1.if_data;
314 ASSERT(dp->i_d.di_size >= XFS_DIR2_SF_HDR_SIZE(sfp->hdr.i8count));
316 * Compute entry (and change in) size.
318 add_entsize = XFS_DIR2_SF_ENTSIZE_BYNAME(sfp, args->namelen);
319 incr_isize = add_entsize;
323 * Do we have to change to 8 byte inodes?
325 if (args->inumber > XFS_DIR2_MAX_SHORT_INUM && sfp->hdr.i8count == 0) {
327 * Yes, adjust the entry size and the total size.
330 (uint)sizeof(xfs_dir2_ino8_t) -
331 (uint)sizeof(xfs_dir2_ino4_t);
333 (sfp->hdr.count + 2) *
334 ((uint)sizeof(xfs_dir2_ino8_t) -
335 (uint)sizeof(xfs_dir2_ino4_t));
339 old_isize = (int)dp->i_d.di_size;
340 new_isize = old_isize + incr_isize;
342 * Won't fit as shortform any more (due to size),
343 * or the pick routine says it won't (due to offset values).
345 if (new_isize > XFS_IFORK_DSIZE(dp) ||
347 xfs_dir2_sf_addname_pick(args, objchange, &sfep, &offset)) == 0) {
349 * Just checking or no space reservation, it doesn't fit.
351 if (args->justcheck || args->total == 0)
352 return XFS_ERROR(ENOSPC);
354 * Convert to block form then add the name.
356 error = xfs_dir2_sf_to_block(args);
359 return xfs_dir2_block_addname(args);
362 * Just checking, it fits.
367 * Do it the easy way - just add it at the end.
370 xfs_dir2_sf_addname_easy(args, sfep, offset, new_isize);
372 * Do it the hard way - look for a place to insert the new entry.
373 * Convert to 8 byte inode numbers first if necessary.
379 xfs_dir2_sf_toino8(args);
381 xfs_dir2_sf_addname_hard(args, objchange, new_isize);
383 xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE | XFS_ILOG_DDATA);
388 * Add the new entry the "easy" way.
389 * This is copying the old directory and adding the new entry at the end.
390 * Since it's sorted by "offset" we need room after the last offset
391 * that's already there, and then room to convert to a block directory.
392 * This is already checked by the pick routine.
395 xfs_dir2_sf_addname_easy(
396 xfs_da_args_t *args, /* operation arguments */
397 xfs_dir2_sf_entry_t *sfep, /* pointer to new entry */
398 xfs_dir2_data_aoff_t offset, /* offset to use for new ent */
399 int new_isize) /* new directory size */
401 int byteoff; /* byte offset in sf dir */
402 xfs_inode_t *dp; /* incore directory inode */
403 xfs_dir2_sf_t *sfp; /* shortform structure */
407 sfp = (xfs_dir2_sf_t *)dp->i_df.if_u1.if_data;
408 byteoff = (int)((char *)sfep - (char *)sfp);
410 * Grow the in-inode space.
412 xfs_idata_realloc(dp, XFS_DIR2_SF_ENTSIZE_BYNAME(sfp, args->namelen),
415 * Need to set up again due to realloc of the inode data.
417 sfp = (xfs_dir2_sf_t *)dp->i_df.if_u1.if_data;
418 sfep = (xfs_dir2_sf_entry_t *)((char *)sfp + byteoff);
420 * Fill in the new entry.
422 sfep->namelen = args->namelen;
423 XFS_DIR2_SF_PUT_OFFSET(sfep, offset);
424 memcpy(sfep->name, args->name, sfep->namelen);
425 XFS_DIR2_SF_PUT_INUMBER(sfp, &args->inumber,
426 XFS_DIR2_SF_INUMBERP(sfep));
428 * Update the header and inode.
432 if (args->inumber > XFS_DIR2_MAX_SHORT_INUM)
435 dp->i_d.di_size = new_isize;
436 xfs_dir2_sf_check(args);
440 * Add the new entry the "hard" way.
441 * The caller has already converted to 8 byte inode numbers if necessary,
442 * in which case we need to leave the i8count at 1.
443 * Find a hole that the new entry will fit into, and copy
444 * the first part of the entries, the new entry, and the last part of
449 xfs_dir2_sf_addname_hard(
450 xfs_da_args_t *args, /* operation arguments */
451 int objchange, /* changing inode number size */
452 int new_isize) /* new directory size */
454 int add_datasize; /* data size need for new ent */
455 char *buf; /* buffer for old */
456 xfs_inode_t *dp; /* incore directory inode */
457 int eof; /* reached end of old dir */
458 int nbytes; /* temp for byte copies */
459 xfs_dir2_data_aoff_t new_offset; /* next offset value */
460 xfs_dir2_data_aoff_t offset; /* current offset value */
461 int old_isize; /* previous di_size */
462 xfs_dir2_sf_entry_t *oldsfep; /* entry in original dir */
463 xfs_dir2_sf_t *oldsfp; /* original shortform dir */
464 xfs_dir2_sf_entry_t *sfep; /* entry in new dir */
465 xfs_dir2_sf_t *sfp; /* new shortform dir */
468 * Copy the old directory to the stack buffer.
472 sfp = (xfs_dir2_sf_t *)dp->i_df.if_u1.if_data;
473 old_isize = (int)dp->i_d.di_size;
474 buf = kmem_alloc(old_isize, KM_SLEEP);
475 oldsfp = (xfs_dir2_sf_t *)buf;
476 memcpy(oldsfp, sfp, old_isize);
478 * Loop over the old directory finding the place we're going
479 * to insert the new entry.
480 * If it's going to end up at the end then oldsfep will point there.
482 for (offset = XFS_DIR2_DATA_FIRST_OFFSET,
483 oldsfep = XFS_DIR2_SF_FIRSTENTRY(oldsfp),
484 add_datasize = XFS_DIR2_DATA_ENTSIZE(args->namelen),
485 eof = (char *)oldsfep == &buf[old_isize];
487 offset = new_offset + XFS_DIR2_DATA_ENTSIZE(oldsfep->namelen),
488 oldsfep = XFS_DIR2_SF_NEXTENTRY(oldsfp, oldsfep),
489 eof = (char *)oldsfep == &buf[old_isize]) {
490 new_offset = XFS_DIR2_SF_GET_OFFSET(oldsfep);
491 if (offset + add_datasize <= new_offset)
495 * Get rid of the old directory, then allocate space for
496 * the new one. We do this so xfs_idata_realloc won't copy
499 xfs_idata_realloc(dp, -old_isize, XFS_DATA_FORK);
500 xfs_idata_realloc(dp, new_isize, XFS_DATA_FORK);
502 * Reset the pointer since the buffer was reallocated.
504 sfp = (xfs_dir2_sf_t *)dp->i_df.if_u1.if_data;
506 * Copy the first part of the directory, including the header.
508 nbytes = (int)((char *)oldsfep - (char *)oldsfp);
509 memcpy(sfp, oldsfp, nbytes);
510 sfep = (xfs_dir2_sf_entry_t *)((char *)sfp + nbytes);
512 * Fill in the new entry, and update the header counts.
514 sfep->namelen = args->namelen;
515 XFS_DIR2_SF_PUT_OFFSET(sfep, offset);
516 memcpy(sfep->name, args->name, sfep->namelen);
517 XFS_DIR2_SF_PUT_INUMBER(sfp, &args->inumber,
518 XFS_DIR2_SF_INUMBERP(sfep));
521 if (args->inumber > XFS_DIR2_MAX_SHORT_INUM && !objchange)
525 * If there's more left to copy, do that.
528 sfep = XFS_DIR2_SF_NEXTENTRY(sfp, sfep);
529 memcpy(sfep, oldsfep, old_isize - nbytes);
531 kmem_free(buf, old_isize);
532 dp->i_d.di_size = new_isize;
533 xfs_dir2_sf_check(args);
537 * Decide if the new entry will fit at all.
538 * If it will fit, pick between adding the new entry to the end (easy)
539 * or somewhere else (hard).
540 * Return 0 (won't fit), 1 (easy), 2 (hard).
543 static int /* pick result */
544 xfs_dir2_sf_addname_pick(
545 xfs_da_args_t *args, /* operation arguments */
546 int objchange, /* inode # size changes */
547 xfs_dir2_sf_entry_t **sfepp, /* out(1): new entry ptr */
548 xfs_dir2_data_aoff_t *offsetp) /* out(1): new offset */
550 xfs_inode_t *dp; /* incore directory inode */
551 int holefit; /* found hole it will fit in */
552 int i; /* entry number */
553 xfs_mount_t *mp; /* filesystem mount point */
554 xfs_dir2_data_aoff_t offset; /* data block offset */
555 xfs_dir2_sf_entry_t *sfep; /* shortform entry */
556 xfs_dir2_sf_t *sfp; /* shortform structure */
557 int size; /* entry's data size */
558 int used; /* data bytes used */
563 sfp = (xfs_dir2_sf_t *)dp->i_df.if_u1.if_data;
564 size = XFS_DIR2_DATA_ENTSIZE(args->namelen);
565 offset = XFS_DIR2_DATA_FIRST_OFFSET;
566 sfep = XFS_DIR2_SF_FIRSTENTRY(sfp);
569 * Loop over sf entries.
570 * Keep track of data offset and whether we've seen a place
571 * to insert the new entry.
573 for (i = 0; i < sfp->hdr.count; i++) {
575 holefit = offset + size <= XFS_DIR2_SF_GET_OFFSET(sfep);
576 offset = XFS_DIR2_SF_GET_OFFSET(sfep) +
577 XFS_DIR2_DATA_ENTSIZE(sfep->namelen);
578 sfep = XFS_DIR2_SF_NEXTENTRY(sfp, sfep);
581 * Calculate data bytes used excluding the new entry, if this
582 * was a data block (block form directory).
585 (sfp->hdr.count + 3) * (uint)sizeof(xfs_dir2_leaf_entry_t) +
586 (uint)sizeof(xfs_dir2_block_tail_t);
588 * If it won't fit in a block form then we can't insert it,
589 * we'll go back, convert to block, then try the insert and convert
592 if (used + (holefit ? 0 : size) > mp->m_dirblksize)
595 * If changing the inode number size, do it the hard way.
602 ASSERT(objchange == 0);
605 * If it won't fit at the end then do it the hard way (use the hole).
607 if (used + size > mp->m_dirblksize)
610 * Do it the easy way.
619 * Check consistency of shortform directory, assert if bad.
623 xfs_da_args_t *args) /* operation arguments */
625 xfs_inode_t *dp; /* incore directory inode */
626 int i; /* entry number */
627 int i8count; /* number of big inode#s */
628 xfs_ino_t ino; /* entry inode number */
629 int offset; /* data offset */
630 xfs_dir2_sf_entry_t *sfep; /* shortform dir entry */
631 xfs_dir2_sf_t *sfp; /* shortform structure */
635 sfp = (xfs_dir2_sf_t *)dp->i_df.if_u1.if_data;
636 offset = XFS_DIR2_DATA_FIRST_OFFSET;
637 ino = XFS_DIR2_SF_GET_INUMBER(sfp, &sfp->hdr.parent);
638 i8count = ino > XFS_DIR2_MAX_SHORT_INUM;
640 for (i = 0, sfep = XFS_DIR2_SF_FIRSTENTRY(sfp);
642 i++, sfep = XFS_DIR2_SF_NEXTENTRY(sfp, sfep)) {
643 ASSERT(XFS_DIR2_SF_GET_OFFSET(sfep) >= offset);
644 ino = XFS_DIR2_SF_GET_INUMBER(sfp, XFS_DIR2_SF_INUMBERP(sfep));
645 i8count += ino > XFS_DIR2_MAX_SHORT_INUM;
647 XFS_DIR2_SF_GET_OFFSET(sfep) +
648 XFS_DIR2_DATA_ENTSIZE(sfep->namelen);
650 ASSERT(i8count == sfp->hdr.i8count);
651 ASSERT(XFS_BIG_INUMS || i8count == 0);
652 ASSERT((char *)sfep - (char *)sfp == dp->i_d.di_size);
654 (sfp->hdr.count + 2) * (uint)sizeof(xfs_dir2_leaf_entry_t) +
655 (uint)sizeof(xfs_dir2_block_tail_t) <=
656 dp->i_mount->m_dirblksize);
661 * Create a new (shortform) directory.
663 int /* error, always 0 */
665 xfs_da_args_t *args, /* operation arguments */
666 xfs_ino_t pino) /* parent inode number */
668 xfs_inode_t *dp; /* incore directory inode */
669 int i8count; /* parent inode is an 8-byte number */
670 xfs_dir2_sf_t *sfp; /* shortform structure */
671 int size; /* directory size */
673 xfs_dir2_trace_args_i("sf_create", args, pino);
677 ASSERT(dp->i_d.di_size == 0);
679 * If it's currently a zero-length extent file,
680 * convert it to local format.
682 if (dp->i_d.di_format == XFS_DINODE_FMT_EXTENTS) {
683 dp->i_df.if_flags &= ~XFS_IFEXTENTS; /* just in case */
684 dp->i_d.di_format = XFS_DINODE_FMT_LOCAL;
685 xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE);
686 dp->i_df.if_flags |= XFS_IFINLINE;
688 ASSERT(dp->i_df.if_flags & XFS_IFINLINE);
689 ASSERT(dp->i_df.if_bytes == 0);
690 i8count = pino > XFS_DIR2_MAX_SHORT_INUM;
691 size = XFS_DIR2_SF_HDR_SIZE(i8count);
693 * Make a buffer for the data.
695 xfs_idata_realloc(dp, size, XFS_DATA_FORK);
697 * Fill in the header,
699 sfp = (xfs_dir2_sf_t *)dp->i_df.if_u1.if_data;
700 sfp->hdr.i8count = i8count;
702 * Now can put in the inode number, since i8count is set.
704 XFS_DIR2_SF_PUT_INUMBER(sfp, &pino, &sfp->hdr.parent);
706 dp->i_d.di_size = size;
707 xfs_dir2_sf_check(args);
708 xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE | XFS_ILOG_DDATA);
713 xfs_dir2_sf_getdents(
714 xfs_inode_t *dp, /* incore directory inode */
715 uio_t *uio, /* caller's buffer control */
716 int *eofp, /* eof reached? (out) */
717 xfs_dirent_t *dbp, /* caller's buffer */
718 xfs_dir2_put_t put) /* abi's formatting function */
720 int error; /* error return value */
721 int i; /* shortform entry number */
722 xfs_mount_t *mp; /* filesystem mount point */
723 xfs_dir2_dataptr_t off; /* current entry's offset */
724 xfs_dir2_put_args_t p; /* arg package for put rtn */
725 xfs_dir2_sf_entry_t *sfep; /* shortform directory entry */
726 xfs_dir2_sf_t *sfp; /* shortform structure */
727 xfs_off_t dir_offset;
731 ASSERT(dp->i_df.if_flags & XFS_IFINLINE);
733 * Give up if the directory is way too short.
735 if (dp->i_d.di_size < offsetof(xfs_dir2_sf_hdr_t, parent)) {
736 ASSERT(XFS_FORCED_SHUTDOWN(mp));
737 return XFS_ERROR(EIO);
740 dir_offset = uio->uio_offset;
742 ASSERT(dp->i_df.if_bytes == dp->i_d.di_size);
743 ASSERT(dp->i_df.if_u1.if_data != NULL);
745 sfp = (xfs_dir2_sf_t *)dp->i_df.if_u1.if_data;
747 ASSERT(dp->i_d.di_size >= XFS_DIR2_SF_HDR_SIZE(sfp->hdr.i8count));
750 * If the block number in the offset is out of range, we're done.
752 if (XFS_DIR2_DATAPTR_TO_DB(mp, dir_offset) > mp->m_dirdatablk) {
758 * Set up putargs structure.
764 * Put . entry unless we're starting past it.
767 XFS_DIR2_DB_OFF_TO_DATAPTR(mp, mp->m_dirdatablk,
768 XFS_DIR2_DATA_DOT_OFFSET)) {
769 p.cook = XFS_DIR2_DB_OFF_TO_DATAPTR(mp, 0,
770 XFS_DIR2_DATA_DOTDOT_OFFSET);
773 p.ino += mp->m_inoadd;
782 XFS_DIR2_DB_OFF_TO_DATAPTR(mp, mp->m_dirdatablk,
783 XFS_DIR2_DATA_DOT_OFFSET);
789 * Put .. entry unless we're starting past it.
792 XFS_DIR2_DB_OFF_TO_DATAPTR(mp, mp->m_dirdatablk,
793 XFS_DIR2_DATA_DOTDOT_OFFSET)) {
794 p.cook = XFS_DIR2_DB_OFF_TO_DATAPTR(mp, mp->m_dirdatablk,
795 XFS_DIR2_DATA_FIRST_OFFSET);
796 p.ino = XFS_DIR2_SF_GET_INUMBER(sfp, &sfp->hdr.parent);
798 p.ino += mp->m_inoadd;
807 XFS_DIR2_DB_OFF_TO_DATAPTR(mp, mp->m_dirdatablk,
808 XFS_DIR2_DATA_DOTDOT_OFFSET);
814 * Loop while there are more entries and put'ing works.
816 for (i = 0, sfep = XFS_DIR2_SF_FIRSTENTRY(sfp);
818 i++, sfep = XFS_DIR2_SF_NEXTENTRY(sfp, sfep)) {
820 off = XFS_DIR2_DB_OFF_TO_DATAPTR(mp, mp->m_dirdatablk,
821 XFS_DIR2_SF_GET_OFFSET(sfep));
823 if (dir_offset > off)
826 p.namelen = sfep->namelen;
828 p.cook = XFS_DIR2_DB_OFF_TO_DATAPTR(mp, mp->m_dirdatablk,
829 XFS_DIR2_SF_GET_OFFSET(sfep) +
830 XFS_DIR2_DATA_ENTSIZE(p.namelen));
832 p.ino = XFS_DIR2_SF_GET_INUMBER(sfp, XFS_DIR2_SF_INUMBERP(sfep));
834 p.ino += mp->m_inoadd;
836 p.name = (char *)sfep->name;
841 uio->uio_offset = off;
852 XFS_DIR2_DB_OFF_TO_DATAPTR(mp, mp->m_dirdatablk + 1, 0);
858 * Lookup an entry in a shortform directory.
859 * Returns EEXIST if found, ENOENT if not found.
863 xfs_da_args_t *args) /* operation arguments */
865 xfs_inode_t *dp; /* incore directory inode */
866 int i; /* entry index */
867 xfs_dir2_sf_entry_t *sfep; /* shortform directory entry */
868 xfs_dir2_sf_t *sfp; /* shortform structure */
870 xfs_dir2_trace_args("sf_lookup", args);
871 xfs_dir2_sf_check(args);
874 ASSERT(dp->i_df.if_flags & XFS_IFINLINE);
876 * Bail out if the directory is way too short.
878 if (dp->i_d.di_size < offsetof(xfs_dir2_sf_hdr_t, parent)) {
879 ASSERT(XFS_FORCED_SHUTDOWN(dp->i_mount));
880 return XFS_ERROR(EIO);
882 ASSERT(dp->i_df.if_bytes == dp->i_d.di_size);
883 ASSERT(dp->i_df.if_u1.if_data != NULL);
884 sfp = (xfs_dir2_sf_t *)dp->i_df.if_u1.if_data;
885 ASSERT(dp->i_d.di_size >= XFS_DIR2_SF_HDR_SIZE(sfp->hdr.i8count));
889 if (args->namelen == 1 && args->name[0] == '.') {
890 args->inumber = dp->i_ino;
891 return XFS_ERROR(EEXIST);
894 * Special case for ..
896 if (args->namelen == 2 &&
897 args->name[0] == '.' && args->name[1] == '.') {
898 args->inumber = XFS_DIR2_SF_GET_INUMBER(sfp, &sfp->hdr.parent);
899 return XFS_ERROR(EEXIST);
902 * Loop over all the entries trying to match ours.
904 for (i = 0, sfep = XFS_DIR2_SF_FIRSTENTRY(sfp);
906 i++, sfep = XFS_DIR2_SF_NEXTENTRY(sfp, sfep)) {
907 if (sfep->namelen == args->namelen &&
908 sfep->name[0] == args->name[0] &&
909 memcmp(args->name, sfep->name, args->namelen) == 0) {
911 XFS_DIR2_SF_GET_INUMBER(sfp,
912 XFS_DIR2_SF_INUMBERP(sfep));
913 return XFS_ERROR(EEXIST);
919 ASSERT(args->oknoent);
920 return XFS_ERROR(ENOENT);
924 * Remove an entry from a shortform directory.
927 xfs_dir2_sf_removename(
930 int byteoff; /* offset of removed entry */
931 xfs_inode_t *dp; /* incore directory inode */
932 int entsize; /* this entry's size */
933 int i; /* shortform entry index */
934 int newsize; /* new inode size */
935 int oldsize; /* old inode size */
936 xfs_dir2_sf_entry_t *sfep; /* shortform directory entry */
937 xfs_dir2_sf_t *sfp; /* shortform structure */
939 xfs_dir2_trace_args("sf_removename", args);
942 ASSERT(dp->i_df.if_flags & XFS_IFINLINE);
943 oldsize = (int)dp->i_d.di_size;
945 * Bail out if the directory is way too short.
947 if (oldsize < offsetof(xfs_dir2_sf_hdr_t, parent)) {
948 ASSERT(XFS_FORCED_SHUTDOWN(dp->i_mount));
949 return XFS_ERROR(EIO);
951 ASSERT(dp->i_df.if_bytes == oldsize);
952 ASSERT(dp->i_df.if_u1.if_data != NULL);
953 sfp = (xfs_dir2_sf_t *)dp->i_df.if_u1.if_data;
954 ASSERT(oldsize >= XFS_DIR2_SF_HDR_SIZE(sfp->hdr.i8count));
956 * Loop over the old directory entries.
957 * Find the one we're deleting.
959 for (i = 0, sfep = XFS_DIR2_SF_FIRSTENTRY(sfp);
961 i++, sfep = XFS_DIR2_SF_NEXTENTRY(sfp, sfep)) {
962 if (sfep->namelen == args->namelen &&
963 sfep->name[0] == args->name[0] &&
964 memcmp(sfep->name, args->name, args->namelen) == 0) {
965 ASSERT(XFS_DIR2_SF_GET_INUMBER(sfp,
966 XFS_DIR2_SF_INUMBERP(sfep)) ==
974 if (i == sfp->hdr.count) {
975 return XFS_ERROR(ENOENT);
980 byteoff = (int)((char *)sfep - (char *)sfp);
981 entsize = XFS_DIR2_SF_ENTSIZE_BYNAME(sfp, args->namelen);
982 newsize = oldsize - entsize;
984 * Copy the part if any after the removed entry, sliding it down.
986 if (byteoff + entsize < oldsize)
987 memmove((char *)sfp + byteoff, (char *)sfp + byteoff + entsize,
988 oldsize - (byteoff + entsize));
990 * Fix up the header and file size.
993 dp->i_d.di_size = newsize;
995 * Reallocate, making it smaller.
997 xfs_idata_realloc(dp, newsize - oldsize, XFS_DATA_FORK);
998 sfp = (xfs_dir2_sf_t *)dp->i_df.if_u1.if_data;
1001 * Are we changing inode number size?
1003 if (args->inumber > XFS_DIR2_MAX_SHORT_INUM) {
1004 if (sfp->hdr.i8count == 1)
1005 xfs_dir2_sf_toino4(args);
1010 xfs_dir2_sf_check(args);
1011 xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE | XFS_ILOG_DDATA);
1016 * Replace the inode number of an entry in a shortform directory.
1019 xfs_dir2_sf_replace(
1020 xfs_da_args_t *args) /* operation arguments */
1022 xfs_inode_t *dp; /* incore directory inode */
1023 int i; /* entry index */
1024 #if XFS_BIG_INUMS || defined(DEBUG)
1025 xfs_ino_t ino=0; /* entry old inode number */
1028 int i8elevated; /* sf_toino8 set i8count=1 */
1030 xfs_dir2_sf_entry_t *sfep; /* shortform directory entry */
1031 xfs_dir2_sf_t *sfp; /* shortform structure */
1033 xfs_dir2_trace_args("sf_replace", args);
1036 ASSERT(dp->i_df.if_flags & XFS_IFINLINE);
1038 * Bail out if the shortform directory is way too small.
1040 if (dp->i_d.di_size < offsetof(xfs_dir2_sf_hdr_t, parent)) {
1041 ASSERT(XFS_FORCED_SHUTDOWN(dp->i_mount));
1042 return XFS_ERROR(EIO);
1044 ASSERT(dp->i_df.if_bytes == dp->i_d.di_size);
1045 ASSERT(dp->i_df.if_u1.if_data != NULL);
1046 sfp = (xfs_dir2_sf_t *)dp->i_df.if_u1.if_data;
1047 ASSERT(dp->i_d.di_size >= XFS_DIR2_SF_HDR_SIZE(sfp->hdr.i8count));
1050 * New inode number is large, and need to convert to 8-byte inodes.
1052 if (args->inumber > XFS_DIR2_MAX_SHORT_INUM && sfp->hdr.i8count == 0) {
1053 int error; /* error return value */
1054 int newsize; /* new inode size */
1058 (sfp->hdr.count + 1) *
1059 ((uint)sizeof(xfs_dir2_ino8_t) -
1060 (uint)sizeof(xfs_dir2_ino4_t));
1062 * Won't fit as shortform, convert to block then do replace.
1064 if (newsize > XFS_IFORK_DSIZE(dp)) {
1065 error = xfs_dir2_sf_to_block(args);
1069 return xfs_dir2_block_replace(args);
1072 * Still fits, convert to 8-byte now.
1074 xfs_dir2_sf_toino8(args);
1076 sfp = (xfs_dir2_sf_t *)dp->i_df.if_u1.if_data;
1080 ASSERT(args->namelen != 1 || args->name[0] != '.');
1082 * Replace ..'s entry.
1084 if (args->namelen == 2 &&
1085 args->name[0] == '.' && args->name[1] == '.') {
1086 #if XFS_BIG_INUMS || defined(DEBUG)
1087 ino = XFS_DIR2_SF_GET_INUMBER(sfp, &sfp->hdr.parent);
1088 ASSERT(args->inumber != ino);
1090 XFS_DIR2_SF_PUT_INUMBER(sfp, &args->inumber, &sfp->hdr.parent);
1093 * Normal entry, look for the name.
1096 for (i = 0, sfep = XFS_DIR2_SF_FIRSTENTRY(sfp);
1098 i++, sfep = XFS_DIR2_SF_NEXTENTRY(sfp, sfep)) {
1099 if (sfep->namelen == args->namelen &&
1100 sfep->name[0] == args->name[0] &&
1101 memcmp(args->name, sfep->name, args->namelen) == 0) {
1102 #if XFS_BIG_INUMS || defined(DEBUG)
1103 ino = XFS_DIR2_SF_GET_INUMBER(sfp,
1104 XFS_DIR2_SF_INUMBERP(sfep));
1105 ASSERT(args->inumber != ino);
1107 XFS_DIR2_SF_PUT_INUMBER(sfp, &args->inumber,
1108 XFS_DIR2_SF_INUMBERP(sfep));
1115 if (i == sfp->hdr.count) {
1116 ASSERT(args->oknoent);
1119 xfs_dir2_sf_toino4(args);
1121 return XFS_ERROR(ENOENT);
1126 * See if the old number was large, the new number is small.
1128 if (ino > XFS_DIR2_MAX_SHORT_INUM &&
1129 args->inumber <= XFS_DIR2_MAX_SHORT_INUM) {
1131 * And the old count was one, so need to convert to small.
1133 if (sfp->hdr.i8count == 1)
1134 xfs_dir2_sf_toino4(args);
1139 * See if the old number was small, the new number is large.
1141 if (ino <= XFS_DIR2_MAX_SHORT_INUM &&
1142 args->inumber > XFS_DIR2_MAX_SHORT_INUM) {
1144 * add to the i8count unless we just converted to 8-byte
1145 * inodes (which does an implied i8count = 1)
1147 ASSERT(sfp->hdr.i8count != 0);
1152 xfs_dir2_sf_check(args);
1153 xfs_trans_log_inode(args->trans, dp, XFS_ILOG_DDATA);
1159 * Convert from 8-byte inode numbers to 4-byte inode numbers.
1160 * The last 8-byte inode number is gone, but the count is still 1.
1164 xfs_da_args_t *args) /* operation arguments */
1166 char *buf; /* old dir's buffer */
1167 xfs_inode_t *dp; /* incore directory inode */
1168 int i; /* entry index */
1169 xfs_ino_t ino; /* entry inode number */
1170 int newsize; /* new inode size */
1171 xfs_dir2_sf_entry_t *oldsfep; /* old sf entry */
1172 xfs_dir2_sf_t *oldsfp; /* old sf directory */
1173 int oldsize; /* old inode size */
1174 xfs_dir2_sf_entry_t *sfep; /* new sf entry */
1175 xfs_dir2_sf_t *sfp; /* new sf directory */
1177 xfs_dir2_trace_args("sf_toino4", args);
1181 * Copy the old directory to the buffer.
1182 * Then nuke it from the inode, and add the new buffer to the inode.
1183 * Don't want xfs_idata_realloc copying the data here.
1185 oldsize = dp->i_df.if_bytes;
1186 buf = kmem_alloc(oldsize, KM_SLEEP);
1187 oldsfp = (xfs_dir2_sf_t *)dp->i_df.if_u1.if_data;
1188 ASSERT(oldsfp->hdr.i8count == 1);
1189 memcpy(buf, oldsfp, oldsize);
1191 * Compute the new inode size.
1195 (oldsfp->hdr.count + 1) *
1196 ((uint)sizeof(xfs_dir2_ino8_t) - (uint)sizeof(xfs_dir2_ino4_t));
1197 xfs_idata_realloc(dp, -oldsize, XFS_DATA_FORK);
1198 xfs_idata_realloc(dp, newsize, XFS_DATA_FORK);
1200 * Reset our pointers, the data has moved.
1202 oldsfp = (xfs_dir2_sf_t *)buf;
1203 sfp = (xfs_dir2_sf_t *)dp->i_df.if_u1.if_data;
1205 * Fill in the new header.
1207 sfp->hdr.count = oldsfp->hdr.count;
1208 sfp->hdr.i8count = 0;
1209 ino = XFS_DIR2_SF_GET_INUMBER(oldsfp, &oldsfp->hdr.parent);
1210 XFS_DIR2_SF_PUT_INUMBER(sfp, &ino, &sfp->hdr.parent);
1212 * Copy the entries field by field.
1214 for (i = 0, sfep = XFS_DIR2_SF_FIRSTENTRY(sfp),
1215 oldsfep = XFS_DIR2_SF_FIRSTENTRY(oldsfp);
1217 i++, sfep = XFS_DIR2_SF_NEXTENTRY(sfp, sfep),
1218 oldsfep = XFS_DIR2_SF_NEXTENTRY(oldsfp, oldsfep)) {
1219 sfep->namelen = oldsfep->namelen;
1220 sfep->offset = oldsfep->offset;
1221 memcpy(sfep->name, oldsfep->name, sfep->namelen);
1222 ino = XFS_DIR2_SF_GET_INUMBER(oldsfp,
1223 XFS_DIR2_SF_INUMBERP(oldsfep));
1224 XFS_DIR2_SF_PUT_INUMBER(sfp, &ino, XFS_DIR2_SF_INUMBERP(sfep));
1227 * Clean up the inode.
1229 kmem_free(buf, oldsize);
1230 dp->i_d.di_size = newsize;
1231 xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE | XFS_ILOG_DDATA);
1235 * Convert from 4-byte inode numbers to 8-byte inode numbers.
1236 * The new 8-byte inode number is not there yet, we leave with the
1237 * count 1 but no corresponding entry.
1241 xfs_da_args_t *args) /* operation arguments */
1243 char *buf; /* old dir's buffer */
1244 xfs_inode_t *dp; /* incore directory inode */
1245 int i; /* entry index */
1246 xfs_ino_t ino; /* entry inode number */
1247 int newsize; /* new inode size */
1248 xfs_dir2_sf_entry_t *oldsfep; /* old sf entry */
1249 xfs_dir2_sf_t *oldsfp; /* old sf directory */
1250 int oldsize; /* old inode size */
1251 xfs_dir2_sf_entry_t *sfep; /* new sf entry */
1252 xfs_dir2_sf_t *sfp; /* new sf directory */
1254 xfs_dir2_trace_args("sf_toino8", args);
1258 * Copy the old directory to the buffer.
1259 * Then nuke it from the inode, and add the new buffer to the inode.
1260 * Don't want xfs_idata_realloc copying the data here.
1262 oldsize = dp->i_df.if_bytes;
1263 buf = kmem_alloc(oldsize, KM_SLEEP);
1264 oldsfp = (xfs_dir2_sf_t *)dp->i_df.if_u1.if_data;
1265 ASSERT(oldsfp->hdr.i8count == 0);
1266 memcpy(buf, oldsfp, oldsize);
1268 * Compute the new inode size.
1272 (oldsfp->hdr.count + 1) *
1273 ((uint)sizeof(xfs_dir2_ino8_t) - (uint)sizeof(xfs_dir2_ino4_t));
1274 xfs_idata_realloc(dp, -oldsize, XFS_DATA_FORK);
1275 xfs_idata_realloc(dp, newsize, XFS_DATA_FORK);
1277 * Reset our pointers, the data has moved.
1279 oldsfp = (xfs_dir2_sf_t *)buf;
1280 sfp = (xfs_dir2_sf_t *)dp->i_df.if_u1.if_data;
1282 * Fill in the new header.
1284 sfp->hdr.count = oldsfp->hdr.count;
1285 sfp->hdr.i8count = 1;
1286 ino = XFS_DIR2_SF_GET_INUMBER(oldsfp, &oldsfp->hdr.parent);
1287 XFS_DIR2_SF_PUT_INUMBER(sfp, &ino, &sfp->hdr.parent);
1289 * Copy the entries field by field.
1291 for (i = 0, sfep = XFS_DIR2_SF_FIRSTENTRY(sfp),
1292 oldsfep = XFS_DIR2_SF_FIRSTENTRY(oldsfp);
1294 i++, sfep = XFS_DIR2_SF_NEXTENTRY(sfp, sfep),
1295 oldsfep = XFS_DIR2_SF_NEXTENTRY(oldsfp, oldsfep)) {
1296 sfep->namelen = oldsfep->namelen;
1297 sfep->offset = oldsfep->offset;
1298 memcpy(sfep->name, oldsfep->name, sfep->namelen);
1299 ino = XFS_DIR2_SF_GET_INUMBER(oldsfp,
1300 XFS_DIR2_SF_INUMBERP(oldsfep));
1301 XFS_DIR2_SF_PUT_INUMBER(sfp, &ino, XFS_DIR2_SF_INUMBERP(sfep));
1304 * Clean up the inode.
1306 kmem_free(buf, oldsize);
1307 dp->i_d.di_size = newsize;
1308 xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE | XFS_ILOG_DDATA);
1310 #endif /* XFS_BIG_INUMS */