1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2000-2003,2005 Silicon Graphics, Inc.
8 #include "xfs_shared.h"
9 #include "xfs_format.h"
10 #include "xfs_log_format.h"
11 #include "xfs_trans_resv.h"
12 #include "xfs_mount.h"
13 #include "xfs_inode.h"
14 #include "xfs_trans.h"
16 #include "xfs_dir2_priv.h"
17 #include "xfs_trace.h"
20 * Prototypes for internal functions.
22 static void xfs_dir2_sf_addname_easy(xfs_da_args_t *args,
23 xfs_dir2_sf_entry_t *sfep,
24 xfs_dir2_data_aoff_t offset,
26 static void xfs_dir2_sf_addname_hard(xfs_da_args_t *args, int objchange,
28 static int xfs_dir2_sf_addname_pick(xfs_da_args_t *args, int objchange,
29 xfs_dir2_sf_entry_t **sfepp,
30 xfs_dir2_data_aoff_t *offsetp);
32 static void xfs_dir2_sf_check(xfs_da_args_t *args);
34 #define xfs_dir2_sf_check(args)
37 static void xfs_dir2_sf_toino4(xfs_da_args_t *args);
38 static void xfs_dir2_sf_toino8(xfs_da_args_t *args);
41 * Given a block directory (dp/block), calculate its size as a shortform (sf)
42 * directory and a header for the sf directory, if it will fit it the
43 * space currently present in the inode. If it won't fit, the output
44 * size is too big (but not accurate).
46 int /* size for sf form */
47 xfs_dir2_block_sfsize(
48 xfs_inode_t *dp, /* incore inode pointer */
49 xfs_dir2_data_hdr_t *hdr, /* block directory data */
50 xfs_dir2_sf_hdr_t *sfhp) /* output: header for sf form */
52 xfs_dir2_dataptr_t addr; /* data entry address */
53 xfs_dir2_leaf_entry_t *blp; /* leaf area of the block */
54 xfs_dir2_block_tail_t *btp; /* tail area of the block */
55 int count; /* shortform entry count */
56 xfs_dir2_data_entry_t *dep; /* data entry in the block */
57 int i; /* block entry index */
58 int i8count; /* count of big-inode entries */
59 int isdot; /* entry is "." */
60 int isdotdot; /* entry is ".." */
61 xfs_mount_t *mp; /* mount structure pointer */
62 int namelen; /* total name bytes */
63 xfs_ino_t parent = 0; /* parent inode number */
64 int size=0; /* total computed size */
66 struct xfs_da_geometry *geo;
72 * if there is a filetype field, add the extra byte to the namelen
73 * for each entry that we see.
75 has_ftype = xfs_sb_version_hasftype(&mp->m_sb) ? 1 : 0;
77 count = i8count = namelen = 0;
78 btp = xfs_dir2_block_tail_p(geo, hdr);
79 blp = xfs_dir2_block_leaf_p(btp);
82 * Iterate over the block's data entries by using the leaf pointers.
84 for (i = 0; i < be32_to_cpu(btp->count); i++) {
85 if ((addr = be32_to_cpu(blp[i].address)) == XFS_DIR2_NULL_DATAPTR)
88 * Calculate the pointer to the entry at hand.
90 dep = (xfs_dir2_data_entry_t *)((char *)hdr +
91 xfs_dir2_dataptr_to_off(geo, addr));
93 * Detect . and .., so we can special-case them.
94 * . is not included in sf directories.
95 * .. is included by just the parent inode number.
97 isdot = dep->namelen == 1 && dep->name[0] == '.';
100 dep->name[0] == '.' && dep->name[1] == '.';
103 i8count += be64_to_cpu(dep->inumber) > XFS_DIR2_MAX_SHORT_INUM;
105 /* take into account the file type field */
106 if (!isdot && !isdotdot) {
108 namelen += dep->namelen + has_ftype;
110 parent = be64_to_cpu(dep->inumber);
112 * Calculate the new size, see if we should give up yet.
114 size = xfs_dir2_sf_hdr_size(i8count) + /* header */
115 count * 3 * sizeof(u8) + /* namelen + offset */
117 (i8count ? /* inumber */
118 count * XFS_INO64_SIZE :
119 count * XFS_INO32_SIZE);
120 if (size > XFS_IFORK_DSIZE(dp))
121 return size; /* size value is a failure */
124 * Create the output header, if it worked.
127 sfhp->i8count = i8count;
128 dp->d_ops->sf_put_parent_ino(sfhp, parent);
133 * Convert a block format directory to shortform.
134 * Caller has already checked that it will fit, and built us a header.
137 xfs_dir2_block_to_sf(
138 xfs_da_args_t *args, /* operation arguments */
140 int size, /* shortform directory size */
141 xfs_dir2_sf_hdr_t *sfhp) /* shortform directory hdr */
143 xfs_dir2_data_hdr_t *hdr; /* block header */
144 xfs_dir2_data_entry_t *dep; /* data entry pointer */
145 xfs_inode_t *dp; /* incore directory inode */
146 xfs_dir2_data_unused_t *dup; /* unused data pointer */
147 char *endptr; /* end of data entries */
148 int error; /* error return value */
149 int logflags; /* inode logging flags */
150 xfs_mount_t *mp; /* filesystem mount point */
151 char *ptr; /* current data pointer */
152 xfs_dir2_sf_entry_t *sfep; /* shortform entry */
153 xfs_dir2_sf_hdr_t *sfp; /* shortform directory header */
154 xfs_dir2_sf_hdr_t *dst; /* temporary data buffer */
156 trace_xfs_dir2_block_to_sf(args);
162 * allocate a temporary destination buffer the size of the inode
163 * to format the data into. Once we have formatted the data, we
164 * can free the block and copy the formatted data into the inode literal
167 dst = kmem_alloc(mp->m_sb.sb_inodesize, 0);
171 * Copy the header into the newly allocate local space.
173 sfp = (xfs_dir2_sf_hdr_t *)dst;
174 memcpy(sfp, sfhp, xfs_dir2_sf_hdr_size(sfhp->i8count));
177 * Set up to loop over the block's entries.
179 ptr = (char *)dp->d_ops->data_entry_p(hdr);
180 endptr = xfs_dir3_data_endp(args->geo, hdr);
181 sfep = xfs_dir2_sf_firstentry(sfp);
183 * Loop over the active and unused entries.
184 * Stop when we reach the leaf/tail portion of the block.
186 while (ptr < endptr) {
188 * If it's unused, just skip over it.
190 dup = (xfs_dir2_data_unused_t *)ptr;
191 if (be16_to_cpu(dup->freetag) == XFS_DIR2_DATA_FREE_TAG) {
192 ptr += be16_to_cpu(dup->length);
195 dep = (xfs_dir2_data_entry_t *)ptr;
199 if (dep->namelen == 1 && dep->name[0] == '.')
200 ASSERT(be64_to_cpu(dep->inumber) == dp->i_ino);
202 * Skip .., but make sure the inode number is right.
204 else if (dep->namelen == 2 &&
205 dep->name[0] == '.' && dep->name[1] == '.')
206 ASSERT(be64_to_cpu(dep->inumber) ==
207 dp->d_ops->sf_get_parent_ino(sfp));
209 * Normal entry, copy it into shortform.
212 sfep->namelen = dep->namelen;
213 xfs_dir2_sf_put_offset(sfep,
214 (xfs_dir2_data_aoff_t)
215 ((char *)dep - (char *)hdr));
216 memcpy(sfep->name, dep->name, dep->namelen);
217 dp->d_ops->sf_put_ino(sfp, sfep,
218 be64_to_cpu(dep->inumber));
219 dp->d_ops->sf_put_ftype(sfep,
220 dp->d_ops->data_get_ftype(dep));
222 sfep = dp->d_ops->sf_nextentry(sfp, sfep);
224 ptr += dp->d_ops->data_entsize(dep->namelen);
226 ASSERT((char *)sfep - (char *)sfp == size);
228 /* now we are done with the block, we can shrink the inode */
229 logflags = XFS_ILOG_CORE;
230 error = xfs_dir2_shrink_inode(args, args->geo->datablk, bp);
232 ASSERT(error != -ENOSPC);
237 * The buffer is now unconditionally gone, whether
238 * xfs_dir2_shrink_inode worked or not.
240 * Convert the inode to local format and copy the data in.
242 ASSERT(dp->i_df.if_bytes == 0);
243 xfs_init_local_fork(dp, XFS_DATA_FORK, dst, size);
244 dp->i_d.di_format = XFS_DINODE_FMT_LOCAL;
245 dp->i_d.di_size = size;
247 logflags |= XFS_ILOG_DDATA;
248 xfs_dir2_sf_check(args);
250 xfs_trans_log_inode(args->trans, dp, logflags);
256 * Add a name to a shortform directory.
257 * There are two algorithms, "easy" and "hard" which we decide on
258 * before changing anything.
259 * Convert to block form if necessary, if the new entry won't fit.
263 xfs_da_args_t *args) /* operation arguments */
265 xfs_inode_t *dp; /* incore directory inode */
266 int error; /* error return value */
267 int incr_isize; /* total change in size */
268 int new_isize; /* di_size after adding name */
269 int objchange; /* changing to 8-byte inodes */
270 xfs_dir2_data_aoff_t offset = 0; /* offset for new entry */
271 int pick; /* which algorithm to use */
272 xfs_dir2_sf_hdr_t *sfp; /* shortform structure */
273 xfs_dir2_sf_entry_t *sfep = NULL; /* shortform entry */
275 trace_xfs_dir2_sf_addname(args);
277 ASSERT(xfs_dir2_sf_lookup(args) == -ENOENT);
279 ASSERT(dp->i_df.if_flags & XFS_IFINLINE);
281 * Make sure the shortform value has some of its header.
283 if (dp->i_d.di_size < offsetof(xfs_dir2_sf_hdr_t, parent)) {
284 ASSERT(XFS_FORCED_SHUTDOWN(dp->i_mount));
287 ASSERT(dp->i_df.if_bytes == dp->i_d.di_size);
288 ASSERT(dp->i_df.if_u1.if_data != NULL);
289 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
290 ASSERT(dp->i_d.di_size >= xfs_dir2_sf_hdr_size(sfp->i8count));
292 * Compute entry (and change in) size.
294 incr_isize = dp->d_ops->sf_entsize(sfp, args->namelen);
298 * Do we have to change to 8 byte inodes?
300 if (args->inumber > XFS_DIR2_MAX_SHORT_INUM && sfp->i8count == 0) {
302 * Yes, adjust the inode size. old count + (parent + new)
304 incr_isize += (sfp->count + 2) * XFS_INO64_DIFF;
308 new_isize = (int)dp->i_d.di_size + incr_isize;
310 * Won't fit as shortform any more (due to size),
311 * or the pick routine says it won't (due to offset values).
313 if (new_isize > XFS_IFORK_DSIZE(dp) ||
315 xfs_dir2_sf_addname_pick(args, objchange, &sfep, &offset)) == 0) {
317 * Just checking or no space reservation, it doesn't fit.
319 if ((args->op_flags & XFS_DA_OP_JUSTCHECK) || args->total == 0)
322 * Convert to block form then add the name.
324 error = xfs_dir2_sf_to_block(args);
327 return xfs_dir2_block_addname(args);
330 * Just checking, it fits.
332 if (args->op_flags & XFS_DA_OP_JUSTCHECK)
335 * Do it the easy way - just add it at the end.
338 xfs_dir2_sf_addname_easy(args, sfep, offset, new_isize);
340 * Do it the hard way - look for a place to insert the new entry.
341 * Convert to 8 byte inode numbers first if necessary.
346 xfs_dir2_sf_toino8(args);
347 xfs_dir2_sf_addname_hard(args, objchange, new_isize);
349 xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE | XFS_ILOG_DDATA);
354 * Add the new entry the "easy" way.
355 * This is copying the old directory and adding the new entry at the end.
356 * Since it's sorted by "offset" we need room after the last offset
357 * that's already there, and then room to convert to a block directory.
358 * This is already checked by the pick routine.
361 xfs_dir2_sf_addname_easy(
362 xfs_da_args_t *args, /* operation arguments */
363 xfs_dir2_sf_entry_t *sfep, /* pointer to new entry */
364 xfs_dir2_data_aoff_t offset, /* offset to use for new ent */
365 int new_isize) /* new directory size */
367 int byteoff; /* byte offset in sf dir */
368 xfs_inode_t *dp; /* incore directory inode */
369 xfs_dir2_sf_hdr_t *sfp; /* shortform structure */
373 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
374 byteoff = (int)((char *)sfep - (char *)sfp);
376 * Grow the in-inode space.
378 xfs_idata_realloc(dp, dp->d_ops->sf_entsize(sfp, args->namelen),
381 * Need to set up again due to realloc of the inode data.
383 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
384 sfep = (xfs_dir2_sf_entry_t *)((char *)sfp + byteoff);
386 * Fill in the new entry.
388 sfep->namelen = args->namelen;
389 xfs_dir2_sf_put_offset(sfep, offset);
390 memcpy(sfep->name, args->name, sfep->namelen);
391 dp->d_ops->sf_put_ino(sfp, sfep, args->inumber);
392 dp->d_ops->sf_put_ftype(sfep, args->filetype);
395 * Update the header and inode.
398 if (args->inumber > XFS_DIR2_MAX_SHORT_INUM)
400 dp->i_d.di_size = new_isize;
401 xfs_dir2_sf_check(args);
405 * Add the new entry the "hard" way.
406 * The caller has already converted to 8 byte inode numbers if necessary,
407 * in which case we need to leave the i8count at 1.
408 * Find a hole that the new entry will fit into, and copy
409 * the first part of the entries, the new entry, and the last part of
414 xfs_dir2_sf_addname_hard(
415 xfs_da_args_t *args, /* operation arguments */
416 int objchange, /* changing inode number size */
417 int new_isize) /* new directory size */
419 int add_datasize; /* data size need for new ent */
420 char *buf; /* buffer for old */
421 xfs_inode_t *dp; /* incore directory inode */
422 int eof; /* reached end of old dir */
423 int nbytes; /* temp for byte copies */
424 xfs_dir2_data_aoff_t new_offset; /* next offset value */
425 xfs_dir2_data_aoff_t offset; /* current offset value */
426 int old_isize; /* previous di_size */
427 xfs_dir2_sf_entry_t *oldsfep; /* entry in original dir */
428 xfs_dir2_sf_hdr_t *oldsfp; /* original shortform dir */
429 xfs_dir2_sf_entry_t *sfep; /* entry in new dir */
430 xfs_dir2_sf_hdr_t *sfp; /* new shortform dir */
433 * Copy the old directory to the stack buffer.
437 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
438 old_isize = (int)dp->i_d.di_size;
439 buf = kmem_alloc(old_isize, 0);
440 oldsfp = (xfs_dir2_sf_hdr_t *)buf;
441 memcpy(oldsfp, sfp, old_isize);
443 * Loop over the old directory finding the place we're going
444 * to insert the new entry.
445 * If it's going to end up at the end then oldsfep will point there.
447 for (offset = dp->d_ops->data_first_offset,
448 oldsfep = xfs_dir2_sf_firstentry(oldsfp),
449 add_datasize = dp->d_ops->data_entsize(args->namelen),
450 eof = (char *)oldsfep == &buf[old_isize];
452 offset = new_offset + dp->d_ops->data_entsize(oldsfep->namelen),
453 oldsfep = dp->d_ops->sf_nextentry(oldsfp, oldsfep),
454 eof = (char *)oldsfep == &buf[old_isize]) {
455 new_offset = xfs_dir2_sf_get_offset(oldsfep);
456 if (offset + add_datasize <= new_offset)
460 * Get rid of the old directory, then allocate space for
461 * the new one. We do this so xfs_idata_realloc won't copy
464 xfs_idata_realloc(dp, -old_isize, XFS_DATA_FORK);
465 xfs_idata_realloc(dp, new_isize, XFS_DATA_FORK);
467 * Reset the pointer since the buffer was reallocated.
469 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
471 * Copy the first part of the directory, including the header.
473 nbytes = (int)((char *)oldsfep - (char *)oldsfp);
474 memcpy(sfp, oldsfp, nbytes);
475 sfep = (xfs_dir2_sf_entry_t *)((char *)sfp + nbytes);
477 * Fill in the new entry, and update the header counts.
479 sfep->namelen = args->namelen;
480 xfs_dir2_sf_put_offset(sfep, offset);
481 memcpy(sfep->name, args->name, sfep->namelen);
482 dp->d_ops->sf_put_ino(sfp, sfep, args->inumber);
483 dp->d_ops->sf_put_ftype(sfep, args->filetype);
485 if (args->inumber > XFS_DIR2_MAX_SHORT_INUM && !objchange)
488 * If there's more left to copy, do that.
491 sfep = dp->d_ops->sf_nextentry(sfp, sfep);
492 memcpy(sfep, oldsfep, old_isize - nbytes);
495 dp->i_d.di_size = new_isize;
496 xfs_dir2_sf_check(args);
500 * Decide if the new entry will fit at all.
501 * If it will fit, pick between adding the new entry to the end (easy)
502 * or somewhere else (hard).
503 * Return 0 (won't fit), 1 (easy), 2 (hard).
506 static int /* pick result */
507 xfs_dir2_sf_addname_pick(
508 xfs_da_args_t *args, /* operation arguments */
509 int objchange, /* inode # size changes */
510 xfs_dir2_sf_entry_t **sfepp, /* out(1): new entry ptr */
511 xfs_dir2_data_aoff_t *offsetp) /* out(1): new offset */
513 xfs_inode_t *dp; /* incore directory inode */
514 int holefit; /* found hole it will fit in */
515 int i; /* entry number */
516 xfs_dir2_data_aoff_t offset; /* data block offset */
517 xfs_dir2_sf_entry_t *sfep; /* shortform entry */
518 xfs_dir2_sf_hdr_t *sfp; /* shortform structure */
519 int size; /* entry's data size */
520 int used; /* data bytes used */
524 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
525 size = dp->d_ops->data_entsize(args->namelen);
526 offset = dp->d_ops->data_first_offset;
527 sfep = xfs_dir2_sf_firstentry(sfp);
530 * Loop over sf entries.
531 * Keep track of data offset and whether we've seen a place
532 * to insert the new entry.
534 for (i = 0; i < sfp->count; i++) {
536 holefit = offset + size <= xfs_dir2_sf_get_offset(sfep);
537 offset = xfs_dir2_sf_get_offset(sfep) +
538 dp->d_ops->data_entsize(sfep->namelen);
539 sfep = dp->d_ops->sf_nextentry(sfp, sfep);
542 * Calculate data bytes used excluding the new entry, if this
543 * was a data block (block form directory).
546 (sfp->count + 3) * (uint)sizeof(xfs_dir2_leaf_entry_t) +
547 (uint)sizeof(xfs_dir2_block_tail_t);
549 * If it won't fit in a block form then we can't insert it,
550 * we'll go back, convert to block, then try the insert and convert
553 if (used + (holefit ? 0 : size) > args->geo->blksize)
556 * If changing the inode number size, do it the hard way.
561 * If it won't fit at the end then do it the hard way (use the hole).
563 if (used + size > args->geo->blksize)
566 * Do it the easy way.
575 * Check consistency of shortform directory, assert if bad.
579 xfs_da_args_t *args) /* operation arguments */
581 xfs_inode_t *dp; /* incore directory inode */
582 int i; /* entry number */
583 int i8count; /* number of big inode#s */
584 xfs_ino_t ino; /* entry inode number */
585 int offset; /* data offset */
586 xfs_dir2_sf_entry_t *sfep; /* shortform dir entry */
587 xfs_dir2_sf_hdr_t *sfp; /* shortform structure */
591 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
592 offset = dp->d_ops->data_first_offset;
593 ino = dp->d_ops->sf_get_parent_ino(sfp);
594 i8count = ino > XFS_DIR2_MAX_SHORT_INUM;
596 for (i = 0, sfep = xfs_dir2_sf_firstentry(sfp);
598 i++, sfep = dp->d_ops->sf_nextentry(sfp, sfep)) {
599 ASSERT(xfs_dir2_sf_get_offset(sfep) >= offset);
600 ino = dp->d_ops->sf_get_ino(sfp, sfep);
601 i8count += ino > XFS_DIR2_MAX_SHORT_INUM;
603 xfs_dir2_sf_get_offset(sfep) +
604 dp->d_ops->data_entsize(sfep->namelen);
605 ASSERT(dp->d_ops->sf_get_ftype(sfep) < XFS_DIR3_FT_MAX);
607 ASSERT(i8count == sfp->i8count);
608 ASSERT((char *)sfep - (char *)sfp == dp->i_d.di_size);
610 (sfp->count + 2) * (uint)sizeof(xfs_dir2_leaf_entry_t) +
611 (uint)sizeof(xfs_dir2_block_tail_t) <= args->geo->blksize);
615 /* Verify the consistency of an inline directory. */
618 struct xfs_inode *ip)
620 struct xfs_mount *mp = ip->i_mount;
621 struct xfs_dir2_sf_hdr *sfp;
622 struct xfs_dir2_sf_entry *sfep;
623 struct xfs_dir2_sf_entry *next_sfep;
625 const struct xfs_dir_ops *dops;
626 struct xfs_ifork *ifp;
635 ASSERT(ip->i_d.di_format == XFS_DINODE_FMT_LOCAL);
637 * xfs_iread calls us before xfs_setup_inode sets up ip->d_ops,
638 * so we can only trust the mountpoint to have the right pointer.
640 dops = xfs_dir_get_ops(mp, NULL);
642 ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
643 sfp = (struct xfs_dir2_sf_hdr *)ifp->if_u1.if_data;
644 size = ifp->if_bytes;
647 * Give up if the directory is way too short.
649 if (size <= offsetof(struct xfs_dir2_sf_hdr, parent) ||
650 size < xfs_dir2_sf_hdr_size(sfp->i8count))
651 return __this_address;
653 endp = (char *)sfp + size;
656 ino = dops->sf_get_parent_ino(sfp);
657 i8count = ino > XFS_DIR2_MAX_SHORT_INUM;
658 error = xfs_dir_ino_validate(mp, ino);
660 return __this_address;
661 offset = dops->data_first_offset;
663 /* Check all reported entries */
664 sfep = xfs_dir2_sf_firstentry(sfp);
665 for (i = 0; i < sfp->count; i++) {
667 * struct xfs_dir2_sf_entry has a variable length.
668 * Check the fixed-offset parts of the structure are
669 * within the data buffer.
671 if (((char *)sfep + sizeof(*sfep)) >= endp)
672 return __this_address;
674 /* Don't allow names with known bad length. */
675 if (sfep->namelen == 0)
676 return __this_address;
679 * Check that the variable-length part of the structure is
680 * within the data buffer. The next entry starts after the
681 * name component, so nextentry is an acceptable test.
683 next_sfep = dops->sf_nextentry(sfp, sfep);
684 if (endp < (char *)next_sfep)
685 return __this_address;
687 /* Check that the offsets always increase. */
688 if (xfs_dir2_sf_get_offset(sfep) < offset)
689 return __this_address;
691 /* Check the inode number. */
692 ino = dops->sf_get_ino(sfp, sfep);
693 i8count += ino > XFS_DIR2_MAX_SHORT_INUM;
694 error = xfs_dir_ino_validate(mp, ino);
696 return __this_address;
698 /* Check the file type. */
699 filetype = dops->sf_get_ftype(sfep);
700 if (filetype >= XFS_DIR3_FT_MAX)
701 return __this_address;
703 offset = xfs_dir2_sf_get_offset(sfep) +
704 dops->data_entsize(sfep->namelen);
708 if (i8count != sfp->i8count)
709 return __this_address;
710 if ((void *)sfep != (void *)endp)
711 return __this_address;
713 /* Make sure this whole thing ought to be in local format. */
714 if (offset + (sfp->count + 2) * (uint)sizeof(xfs_dir2_leaf_entry_t) +
715 (uint)sizeof(xfs_dir2_block_tail_t) > mp->m_dir_geo->blksize)
716 return __this_address;
722 * Create a new (shortform) directory.
724 int /* error, always 0 */
726 xfs_da_args_t *args, /* operation arguments */
727 xfs_ino_t pino) /* parent inode number */
729 xfs_inode_t *dp; /* incore directory inode */
730 int i8count; /* parent inode is an 8-byte number */
731 xfs_dir2_sf_hdr_t *sfp; /* shortform structure */
732 int size; /* directory size */
734 trace_xfs_dir2_sf_create(args);
739 ASSERT(dp->i_d.di_size == 0);
741 * If it's currently a zero-length extent file,
742 * convert it to local format.
744 if (dp->i_d.di_format == XFS_DINODE_FMT_EXTENTS) {
745 dp->i_df.if_flags &= ~XFS_IFEXTENTS; /* just in case */
746 dp->i_d.di_format = XFS_DINODE_FMT_LOCAL;
747 xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE);
748 dp->i_df.if_flags |= XFS_IFINLINE;
750 ASSERT(dp->i_df.if_flags & XFS_IFINLINE);
751 ASSERT(dp->i_df.if_bytes == 0);
752 i8count = pino > XFS_DIR2_MAX_SHORT_INUM;
753 size = xfs_dir2_sf_hdr_size(i8count);
755 * Make a buffer for the data.
757 xfs_idata_realloc(dp, size, XFS_DATA_FORK);
759 * Fill in the header,
761 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
762 sfp->i8count = i8count;
764 * Now can put in the inode number, since i8count is set.
766 dp->d_ops->sf_put_parent_ino(sfp, pino);
768 dp->i_d.di_size = size;
769 xfs_dir2_sf_check(args);
770 xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE | XFS_ILOG_DDATA);
775 * Lookup an entry in a shortform directory.
776 * Returns EEXIST if found, ENOENT if not found.
780 xfs_da_args_t *args) /* operation arguments */
782 xfs_inode_t *dp; /* incore directory inode */
783 int i; /* entry index */
785 xfs_dir2_sf_entry_t *sfep; /* shortform directory entry */
786 xfs_dir2_sf_hdr_t *sfp; /* shortform structure */
787 enum xfs_dacmp cmp; /* comparison result */
788 xfs_dir2_sf_entry_t *ci_sfep; /* case-insens. entry */
790 trace_xfs_dir2_sf_lookup(args);
792 xfs_dir2_sf_check(args);
795 ASSERT(dp->i_df.if_flags & XFS_IFINLINE);
797 * Bail out if the directory is way too short.
799 if (dp->i_d.di_size < offsetof(xfs_dir2_sf_hdr_t, parent)) {
800 ASSERT(XFS_FORCED_SHUTDOWN(dp->i_mount));
803 ASSERT(dp->i_df.if_bytes == dp->i_d.di_size);
804 ASSERT(dp->i_df.if_u1.if_data != NULL);
805 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
806 ASSERT(dp->i_d.di_size >= xfs_dir2_sf_hdr_size(sfp->i8count));
810 if (args->namelen == 1 && args->name[0] == '.') {
811 args->inumber = dp->i_ino;
812 args->cmpresult = XFS_CMP_EXACT;
813 args->filetype = XFS_DIR3_FT_DIR;
817 * Special case for ..
819 if (args->namelen == 2 &&
820 args->name[0] == '.' && args->name[1] == '.') {
821 args->inumber = dp->d_ops->sf_get_parent_ino(sfp);
822 args->cmpresult = XFS_CMP_EXACT;
823 args->filetype = XFS_DIR3_FT_DIR;
827 * Loop over all the entries trying to match ours.
830 for (i = 0, sfep = xfs_dir2_sf_firstentry(sfp); i < sfp->count;
831 i++, sfep = dp->d_ops->sf_nextentry(sfp, sfep)) {
833 * Compare name and if it's an exact match, return the inode
834 * number. If it's the first case-insensitive match, store the
835 * inode number and continue looking for an exact match.
837 cmp = dp->i_mount->m_dirnameops->compname(args, sfep->name,
839 if (cmp != XFS_CMP_DIFFERENT && cmp != args->cmpresult) {
840 args->cmpresult = cmp;
841 args->inumber = dp->d_ops->sf_get_ino(sfp, sfep);
842 args->filetype = dp->d_ops->sf_get_ftype(sfep);
843 if (cmp == XFS_CMP_EXACT)
848 ASSERT(args->op_flags & XFS_DA_OP_OKNOENT);
850 * Here, we can only be doing a lookup (not a rename or replace).
851 * If a case-insensitive match was not found, return -ENOENT.
855 /* otherwise process the CI match as required by the caller */
856 error = xfs_dir_cilookup_result(args, ci_sfep->name, ci_sfep->namelen);
861 * Remove an entry from a shortform directory.
864 xfs_dir2_sf_removename(
867 int byteoff; /* offset of removed entry */
868 xfs_inode_t *dp; /* incore directory inode */
869 int entsize; /* this entry's size */
870 int i; /* shortform entry index */
871 int newsize; /* new inode size */
872 int oldsize; /* old inode size */
873 xfs_dir2_sf_entry_t *sfep; /* shortform directory entry */
874 xfs_dir2_sf_hdr_t *sfp; /* shortform structure */
876 trace_xfs_dir2_sf_removename(args);
880 ASSERT(dp->i_df.if_flags & XFS_IFINLINE);
881 oldsize = (int)dp->i_d.di_size;
883 * Bail out if the directory is way too short.
885 if (oldsize < offsetof(xfs_dir2_sf_hdr_t, parent)) {
886 ASSERT(XFS_FORCED_SHUTDOWN(dp->i_mount));
889 ASSERT(dp->i_df.if_bytes == oldsize);
890 ASSERT(dp->i_df.if_u1.if_data != NULL);
891 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
892 ASSERT(oldsize >= xfs_dir2_sf_hdr_size(sfp->i8count));
894 * Loop over the old directory entries.
895 * Find the one we're deleting.
897 for (i = 0, sfep = xfs_dir2_sf_firstentry(sfp); i < sfp->count;
898 i++, sfep = dp->d_ops->sf_nextentry(sfp, sfep)) {
899 if (xfs_da_compname(args, sfep->name, sfep->namelen) ==
901 ASSERT(dp->d_ops->sf_get_ino(sfp, sfep) ==
914 byteoff = (int)((char *)sfep - (char *)sfp);
915 entsize = dp->d_ops->sf_entsize(sfp, args->namelen);
916 newsize = oldsize - entsize;
918 * Copy the part if any after the removed entry, sliding it down.
920 if (byteoff + entsize < oldsize)
921 memmove((char *)sfp + byteoff, (char *)sfp + byteoff + entsize,
922 oldsize - (byteoff + entsize));
924 * Fix up the header and file size.
927 dp->i_d.di_size = newsize;
929 * Reallocate, making it smaller.
931 xfs_idata_realloc(dp, newsize - oldsize, XFS_DATA_FORK);
932 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
934 * Are we changing inode number size?
936 if (args->inumber > XFS_DIR2_MAX_SHORT_INUM) {
937 if (sfp->i8count == 1)
938 xfs_dir2_sf_toino4(args);
942 xfs_dir2_sf_check(args);
943 xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE | XFS_ILOG_DDATA);
948 * Replace the inode number of an entry in a shortform directory.
952 xfs_da_args_t *args) /* operation arguments */
954 xfs_inode_t *dp; /* incore directory inode */
955 int i; /* entry index */
956 xfs_ino_t ino=0; /* entry old inode number */
957 int i8elevated; /* sf_toino8 set i8count=1 */
958 xfs_dir2_sf_entry_t *sfep; /* shortform directory entry */
959 xfs_dir2_sf_hdr_t *sfp; /* shortform structure */
961 trace_xfs_dir2_sf_replace(args);
965 ASSERT(dp->i_df.if_flags & XFS_IFINLINE);
967 * Bail out if the shortform directory is way too small.
969 if (dp->i_d.di_size < offsetof(xfs_dir2_sf_hdr_t, parent)) {
970 ASSERT(XFS_FORCED_SHUTDOWN(dp->i_mount));
973 ASSERT(dp->i_df.if_bytes == dp->i_d.di_size);
974 ASSERT(dp->i_df.if_u1.if_data != NULL);
975 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
976 ASSERT(dp->i_d.di_size >= xfs_dir2_sf_hdr_size(sfp->i8count));
979 * New inode number is large, and need to convert to 8-byte inodes.
981 if (args->inumber > XFS_DIR2_MAX_SHORT_INUM && sfp->i8count == 0) {
982 int error; /* error return value */
983 int newsize; /* new inode size */
985 newsize = dp->i_df.if_bytes + (sfp->count + 1) * XFS_INO64_DIFF;
987 * Won't fit as shortform, convert to block then do replace.
989 if (newsize > XFS_IFORK_DSIZE(dp)) {
990 error = xfs_dir2_sf_to_block(args);
994 return xfs_dir2_block_replace(args);
997 * Still fits, convert to 8-byte now.
999 xfs_dir2_sf_toino8(args);
1001 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
1005 ASSERT(args->namelen != 1 || args->name[0] != '.');
1007 * Replace ..'s entry.
1009 if (args->namelen == 2 &&
1010 args->name[0] == '.' && args->name[1] == '.') {
1011 ino = dp->d_ops->sf_get_parent_ino(sfp);
1012 ASSERT(args->inumber != ino);
1013 dp->d_ops->sf_put_parent_ino(sfp, args->inumber);
1016 * Normal entry, look for the name.
1019 for (i = 0, sfep = xfs_dir2_sf_firstentry(sfp); i < sfp->count;
1020 i++, sfep = dp->d_ops->sf_nextentry(sfp, sfep)) {
1021 if (xfs_da_compname(args, sfep->name, sfep->namelen) ==
1023 ino = dp->d_ops->sf_get_ino(sfp, sfep);
1024 ASSERT(args->inumber != ino);
1025 dp->d_ops->sf_put_ino(sfp, sfep, args->inumber);
1026 dp->d_ops->sf_put_ftype(sfep, args->filetype);
1033 if (i == sfp->count) {
1034 ASSERT(args->op_flags & XFS_DA_OP_OKNOENT);
1036 xfs_dir2_sf_toino4(args);
1041 * See if the old number was large, the new number is small.
1043 if (ino > XFS_DIR2_MAX_SHORT_INUM &&
1044 args->inumber <= XFS_DIR2_MAX_SHORT_INUM) {
1046 * And the old count was one, so need to convert to small.
1048 if (sfp->i8count == 1)
1049 xfs_dir2_sf_toino4(args);
1054 * See if the old number was small, the new number is large.
1056 if (ino <= XFS_DIR2_MAX_SHORT_INUM &&
1057 args->inumber > XFS_DIR2_MAX_SHORT_INUM) {
1059 * add to the i8count unless we just converted to 8-byte
1060 * inodes (which does an implied i8count = 1)
1062 ASSERT(sfp->i8count != 0);
1066 xfs_dir2_sf_check(args);
1067 xfs_trans_log_inode(args->trans, dp, XFS_ILOG_DDATA);
1072 * Convert from 8-byte inode numbers to 4-byte inode numbers.
1073 * The last 8-byte inode number is gone, but the count is still 1.
1077 xfs_da_args_t *args) /* operation arguments */
1079 char *buf; /* old dir's buffer */
1080 xfs_inode_t *dp; /* incore directory inode */
1081 int i; /* entry index */
1082 int newsize; /* new inode size */
1083 xfs_dir2_sf_entry_t *oldsfep; /* old sf entry */
1084 xfs_dir2_sf_hdr_t *oldsfp; /* old sf directory */
1085 int oldsize; /* old inode size */
1086 xfs_dir2_sf_entry_t *sfep; /* new sf entry */
1087 xfs_dir2_sf_hdr_t *sfp; /* new sf directory */
1089 trace_xfs_dir2_sf_toino4(args);
1094 * Copy the old directory to the buffer.
1095 * Then nuke it from the inode, and add the new buffer to the inode.
1096 * Don't want xfs_idata_realloc copying the data here.
1098 oldsize = dp->i_df.if_bytes;
1099 buf = kmem_alloc(oldsize, 0);
1100 oldsfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
1101 ASSERT(oldsfp->i8count == 1);
1102 memcpy(buf, oldsfp, oldsize);
1104 * Compute the new inode size.
1106 newsize = oldsize - (oldsfp->count + 1) * XFS_INO64_DIFF;
1107 xfs_idata_realloc(dp, -oldsize, XFS_DATA_FORK);
1108 xfs_idata_realloc(dp, newsize, XFS_DATA_FORK);
1110 * Reset our pointers, the data has moved.
1112 oldsfp = (xfs_dir2_sf_hdr_t *)buf;
1113 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
1115 * Fill in the new header.
1117 sfp->count = oldsfp->count;
1119 dp->d_ops->sf_put_parent_ino(sfp, dp->d_ops->sf_get_parent_ino(oldsfp));
1121 * Copy the entries field by field.
1123 for (i = 0, sfep = xfs_dir2_sf_firstentry(sfp),
1124 oldsfep = xfs_dir2_sf_firstentry(oldsfp);
1126 i++, sfep = dp->d_ops->sf_nextentry(sfp, sfep),
1127 oldsfep = dp->d_ops->sf_nextentry(oldsfp, oldsfep)) {
1128 sfep->namelen = oldsfep->namelen;
1129 memcpy(sfep->offset, oldsfep->offset, sizeof(sfep->offset));
1130 memcpy(sfep->name, oldsfep->name, sfep->namelen);
1131 dp->d_ops->sf_put_ino(sfp, sfep,
1132 dp->d_ops->sf_get_ino(oldsfp, oldsfep));
1133 dp->d_ops->sf_put_ftype(sfep, dp->d_ops->sf_get_ftype(oldsfep));
1136 * Clean up the inode.
1139 dp->i_d.di_size = newsize;
1140 xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE | XFS_ILOG_DDATA);
1144 * Convert existing entries from 4-byte inode numbers to 8-byte inode numbers.
1145 * The new entry w/ an 8-byte inode number is not there yet; we leave with
1146 * i8count set to 1, but no corresponding 8-byte entry.
1150 xfs_da_args_t *args) /* operation arguments */
1152 char *buf; /* old dir's buffer */
1153 xfs_inode_t *dp; /* incore directory inode */
1154 int i; /* entry index */
1155 int newsize; /* new inode size */
1156 xfs_dir2_sf_entry_t *oldsfep; /* old sf entry */
1157 xfs_dir2_sf_hdr_t *oldsfp; /* old sf directory */
1158 int oldsize; /* old inode size */
1159 xfs_dir2_sf_entry_t *sfep; /* new sf entry */
1160 xfs_dir2_sf_hdr_t *sfp; /* new sf directory */
1162 trace_xfs_dir2_sf_toino8(args);
1167 * Copy the old directory to the buffer.
1168 * Then nuke it from the inode, and add the new buffer to the inode.
1169 * Don't want xfs_idata_realloc copying the data here.
1171 oldsize = dp->i_df.if_bytes;
1172 buf = kmem_alloc(oldsize, 0);
1173 oldsfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
1174 ASSERT(oldsfp->i8count == 0);
1175 memcpy(buf, oldsfp, oldsize);
1177 * Compute the new inode size (nb: entry count + 1 for parent)
1179 newsize = oldsize + (oldsfp->count + 1) * XFS_INO64_DIFF;
1180 xfs_idata_realloc(dp, -oldsize, XFS_DATA_FORK);
1181 xfs_idata_realloc(dp, newsize, XFS_DATA_FORK);
1183 * Reset our pointers, the data has moved.
1185 oldsfp = (xfs_dir2_sf_hdr_t *)buf;
1186 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
1188 * Fill in the new header.
1190 sfp->count = oldsfp->count;
1192 dp->d_ops->sf_put_parent_ino(sfp, dp->d_ops->sf_get_parent_ino(oldsfp));
1194 * Copy the entries field by field.
1196 for (i = 0, sfep = xfs_dir2_sf_firstentry(sfp),
1197 oldsfep = xfs_dir2_sf_firstentry(oldsfp);
1199 i++, sfep = dp->d_ops->sf_nextentry(sfp, sfep),
1200 oldsfep = dp->d_ops->sf_nextentry(oldsfp, oldsfep)) {
1201 sfep->namelen = oldsfep->namelen;
1202 memcpy(sfep->offset, oldsfep->offset, sizeof(sfep->offset));
1203 memcpy(sfep->name, oldsfep->name, sfep->namelen);
1204 dp->d_ops->sf_put_ino(sfp, sfep,
1205 dp->d_ops->sf_get_ino(oldsfp, oldsfep));
1206 dp->d_ops->sf_put_ftype(sfep, dp->d_ops->sf_get_ftype(oldsfep));
1209 * Clean up the inode.
1212 dp->i_d.di_size = newsize;
1213 xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE | XFS_ILOG_DDATA);