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);
43 struct xfs_dir2_sf_hdr *hdr,
48 count += sizeof(struct xfs_dir2_sf_entry); /* namelen + offset */
49 count += hdr->i8count ? XFS_INO64_SIZE : XFS_INO32_SIZE; /* ino # */
51 if (xfs_sb_version_hasftype(&mp->m_sb))
52 count += sizeof(uint8_t);
56 struct xfs_dir2_sf_entry *
57 xfs_dir2_sf_nextentry(
59 struct xfs_dir2_sf_hdr *hdr,
60 struct xfs_dir2_sf_entry *sfep)
62 return (void *)sfep + xfs_dir2_sf_entsize(mp, hdr, sfep->namelen);
66 * In short-form directory entries the inode numbers are stored at variable
67 * offset behind the entry name. If the entry stores a filetype value, then it
68 * sits between the name and the inode number. The actual inode numbers can
69 * come in two formats as well, either 4 bytes or 8 bytes wide.
74 struct xfs_dir2_sf_hdr *hdr,
75 struct xfs_dir2_sf_entry *sfep)
77 uint8_t *from = sfep->name + sfep->namelen;
79 if (xfs_sb_version_hasftype(&mp->m_sb))
83 return get_unaligned_be32(from);
84 return get_unaligned_be64(from) & XFS_MAXINUMBER;
90 struct xfs_dir2_sf_hdr *hdr,
91 struct xfs_dir2_sf_entry *sfep,
94 uint8_t *to = sfep->name + sfep->namelen;
96 ASSERT(ino <= XFS_MAXINUMBER);
98 if (xfs_sb_version_hasftype(&mp->m_sb))
102 put_unaligned_be64(ino, to);
104 put_unaligned_be32(ino, to);
108 xfs_dir2_sf_get_parent_ino(
109 struct xfs_dir2_sf_hdr *hdr)
112 return get_unaligned_be32(hdr->parent);
113 return get_unaligned_be64(hdr->parent) & XFS_MAXINUMBER;
117 xfs_dir2_sf_put_parent_ino(
118 struct xfs_dir2_sf_hdr *hdr,
121 ASSERT(ino <= XFS_MAXINUMBER);
124 put_unaligned_be64(ino, hdr->parent);
126 put_unaligned_be32(ino, hdr->parent);
130 * The file type field is stored at the end of the name for filetype enabled
131 * shortform directories, or not at all otherwise.
134 xfs_dir2_sf_get_ftype(
135 struct xfs_mount *mp,
136 struct xfs_dir2_sf_entry *sfep)
138 if (xfs_sb_version_hasftype(&mp->m_sb)) {
139 uint8_t ftype = sfep->name[sfep->namelen];
141 if (ftype < XFS_DIR3_FT_MAX)
145 return XFS_DIR3_FT_UNKNOWN;
149 xfs_dir2_sf_put_ftype(
150 struct xfs_mount *mp,
151 struct xfs_dir2_sf_entry *sfep,
154 ASSERT(ftype < XFS_DIR3_FT_MAX);
156 if (xfs_sb_version_hasftype(&mp->m_sb))
157 sfep->name[sfep->namelen] = ftype;
161 * Given a block directory (dp/block), calculate its size as a shortform (sf)
162 * directory and a header for the sf directory, if it will fit it the
163 * space currently present in the inode. If it won't fit, the output
164 * size is too big (but not accurate).
166 int /* size for sf form */
167 xfs_dir2_block_sfsize(
168 xfs_inode_t *dp, /* incore inode pointer */
169 xfs_dir2_data_hdr_t *hdr, /* block directory data */
170 xfs_dir2_sf_hdr_t *sfhp) /* output: header for sf form */
172 xfs_dir2_dataptr_t addr; /* data entry address */
173 xfs_dir2_leaf_entry_t *blp; /* leaf area of the block */
174 xfs_dir2_block_tail_t *btp; /* tail area of the block */
175 int count; /* shortform entry count */
176 xfs_dir2_data_entry_t *dep; /* data entry in the block */
177 int i; /* block entry index */
178 int i8count; /* count of big-inode entries */
179 int isdot; /* entry is "." */
180 int isdotdot; /* entry is ".." */
181 xfs_mount_t *mp; /* mount structure pointer */
182 int namelen; /* total name bytes */
183 xfs_ino_t parent = 0; /* parent inode number */
184 int size=0; /* total computed size */
186 struct xfs_da_geometry *geo;
192 * if there is a filetype field, add the extra byte to the namelen
193 * for each entry that we see.
195 has_ftype = xfs_sb_version_hasftype(&mp->m_sb) ? 1 : 0;
197 count = i8count = namelen = 0;
198 btp = xfs_dir2_block_tail_p(geo, hdr);
199 blp = xfs_dir2_block_leaf_p(btp);
202 * Iterate over the block's data entries by using the leaf pointers.
204 for (i = 0; i < be32_to_cpu(btp->count); i++) {
205 if ((addr = be32_to_cpu(blp[i].address)) == XFS_DIR2_NULL_DATAPTR)
208 * Calculate the pointer to the entry at hand.
210 dep = (xfs_dir2_data_entry_t *)((char *)hdr +
211 xfs_dir2_dataptr_to_off(geo, addr));
213 * Detect . and .., so we can special-case them.
214 * . is not included in sf directories.
215 * .. is included by just the parent inode number.
217 isdot = dep->namelen == 1 && dep->name[0] == '.';
220 dep->name[0] == '.' && dep->name[1] == '.';
223 i8count += be64_to_cpu(dep->inumber) > XFS_DIR2_MAX_SHORT_INUM;
225 /* take into account the file type field */
226 if (!isdot && !isdotdot) {
228 namelen += dep->namelen + has_ftype;
230 parent = be64_to_cpu(dep->inumber);
232 * Calculate the new size, see if we should give up yet.
234 size = xfs_dir2_sf_hdr_size(i8count) + /* header */
235 count * 3 * sizeof(u8) + /* namelen + offset */
237 (i8count ? /* inumber */
238 count * XFS_INO64_SIZE :
239 count * XFS_INO32_SIZE);
240 if (size > XFS_IFORK_DSIZE(dp))
241 return size; /* size value is a failure */
244 * Create the output header, if it worked.
247 sfhp->i8count = i8count;
248 xfs_dir2_sf_put_parent_ino(sfhp, parent);
253 * Convert a block format directory to shortform.
254 * Caller has already checked that it will fit, and built us a header.
257 xfs_dir2_block_to_sf(
258 struct xfs_da_args *args, /* operation arguments */
260 int size, /* shortform directory size */
261 struct xfs_dir2_sf_hdr *sfhp) /* shortform directory hdr */
263 struct xfs_inode *dp = args->dp;
264 struct xfs_mount *mp = dp->i_mount;
265 int error; /* error return value */
266 int logflags; /* inode logging flags */
267 struct xfs_dir2_sf_entry *sfep; /* shortform entry */
268 struct xfs_dir2_sf_hdr *sfp; /* shortform directory header */
269 unsigned int offset = dp->d_ops->data_entry_offset;
272 trace_xfs_dir2_block_to_sf(args);
275 * Allocate a temporary destination buffer the size of the inode to
276 * format the data into. Once we have formatted the data, we can free
277 * the block and copy the formatted data into the inode literal area.
279 sfp = kmem_alloc(mp->m_sb.sb_inodesize, 0);
280 memcpy(sfp, sfhp, xfs_dir2_sf_hdr_size(sfhp->i8count));
283 * Loop over the active and unused entries. Stop when we reach the
284 * leaf/tail portion of the block.
286 end = xfs_dir3_data_end_offset(args->geo, bp->b_addr);
287 sfep = xfs_dir2_sf_firstentry(sfp);
288 while (offset < end) {
289 struct xfs_dir2_data_unused *dup = bp->b_addr + offset;
290 struct xfs_dir2_data_entry *dep = bp->b_addr + offset;
293 * If it's unused, just skip over it.
295 if (be16_to_cpu(dup->freetag) == XFS_DIR2_DATA_FREE_TAG) {
296 offset += be16_to_cpu(dup->length);
303 if (dep->namelen == 1 && dep->name[0] == '.')
304 ASSERT(be64_to_cpu(dep->inumber) == dp->i_ino);
306 * Skip .., but make sure the inode number is right.
308 else if (dep->namelen == 2 &&
309 dep->name[0] == '.' && dep->name[1] == '.')
310 ASSERT(be64_to_cpu(dep->inumber) ==
311 xfs_dir2_sf_get_parent_ino(sfp));
313 * Normal entry, copy it into shortform.
316 sfep->namelen = dep->namelen;
317 xfs_dir2_sf_put_offset(sfep, offset);
318 memcpy(sfep->name, dep->name, dep->namelen);
319 xfs_dir2_sf_put_ino(mp, sfp, sfep,
320 be64_to_cpu(dep->inumber));
321 xfs_dir2_sf_put_ftype(mp, sfep,
322 dp->d_ops->data_get_ftype(dep));
324 sfep = xfs_dir2_sf_nextentry(mp, sfp, sfep);
326 offset += dp->d_ops->data_entsize(dep->namelen);
328 ASSERT((char *)sfep - (char *)sfp == size);
330 /* now we are done with the block, we can shrink the inode */
331 logflags = XFS_ILOG_CORE;
332 error = xfs_dir2_shrink_inode(args, args->geo->datablk, bp);
334 ASSERT(error != -ENOSPC);
339 * The buffer is now unconditionally gone, whether
340 * xfs_dir2_shrink_inode worked or not.
342 * Convert the inode to local format and copy the data in.
344 ASSERT(dp->i_df.if_bytes == 0);
345 xfs_init_local_fork(dp, XFS_DATA_FORK, sfp, size);
346 dp->i_d.di_format = XFS_DINODE_FMT_LOCAL;
347 dp->i_d.di_size = size;
349 logflags |= XFS_ILOG_DDATA;
350 xfs_dir2_sf_check(args);
352 xfs_trans_log_inode(args->trans, dp, logflags);
358 * Add a name to a shortform directory.
359 * There are two algorithms, "easy" and "hard" which we decide on
360 * before changing anything.
361 * Convert to block form if necessary, if the new entry won't fit.
365 xfs_da_args_t *args) /* operation arguments */
367 xfs_inode_t *dp; /* incore directory inode */
368 int error; /* error return value */
369 int incr_isize; /* total change in size */
370 int new_isize; /* di_size after adding name */
371 int objchange; /* changing to 8-byte inodes */
372 xfs_dir2_data_aoff_t offset = 0; /* offset for new entry */
373 int pick; /* which algorithm to use */
374 xfs_dir2_sf_hdr_t *sfp; /* shortform structure */
375 xfs_dir2_sf_entry_t *sfep = NULL; /* shortform entry */
377 trace_xfs_dir2_sf_addname(args);
379 ASSERT(xfs_dir2_sf_lookup(args) == -ENOENT);
381 ASSERT(dp->i_df.if_flags & XFS_IFINLINE);
382 ASSERT(dp->i_d.di_size >= offsetof(struct xfs_dir2_sf_hdr, parent));
383 ASSERT(dp->i_df.if_bytes == dp->i_d.di_size);
384 ASSERT(dp->i_df.if_u1.if_data != NULL);
385 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
386 ASSERT(dp->i_d.di_size >= xfs_dir2_sf_hdr_size(sfp->i8count));
388 * Compute entry (and change in) size.
390 incr_isize = xfs_dir2_sf_entsize(dp->i_mount, sfp, args->namelen);
394 * Do we have to change to 8 byte inodes?
396 if (args->inumber > XFS_DIR2_MAX_SHORT_INUM && sfp->i8count == 0) {
398 * Yes, adjust the inode size. old count + (parent + new)
400 incr_isize += (sfp->count + 2) * XFS_INO64_DIFF;
404 new_isize = (int)dp->i_d.di_size + incr_isize;
406 * Won't fit as shortform any more (due to size),
407 * or the pick routine says it won't (due to offset values).
409 if (new_isize > XFS_IFORK_DSIZE(dp) ||
411 xfs_dir2_sf_addname_pick(args, objchange, &sfep, &offset)) == 0) {
413 * Just checking or no space reservation, it doesn't fit.
415 if ((args->op_flags & XFS_DA_OP_JUSTCHECK) || args->total == 0)
418 * Convert to block form then add the name.
420 error = xfs_dir2_sf_to_block(args);
423 return xfs_dir2_block_addname(args);
426 * Just checking, it fits.
428 if (args->op_flags & XFS_DA_OP_JUSTCHECK)
431 * Do it the easy way - just add it at the end.
434 xfs_dir2_sf_addname_easy(args, sfep, offset, new_isize);
436 * Do it the hard way - look for a place to insert the new entry.
437 * Convert to 8 byte inode numbers first if necessary.
442 xfs_dir2_sf_toino8(args);
443 xfs_dir2_sf_addname_hard(args, objchange, new_isize);
445 xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE | XFS_ILOG_DDATA);
450 * Add the new entry the "easy" way.
451 * This is copying the old directory and adding the new entry at the end.
452 * Since it's sorted by "offset" we need room after the last offset
453 * that's already there, and then room to convert to a block directory.
454 * This is already checked by the pick routine.
457 xfs_dir2_sf_addname_easy(
458 xfs_da_args_t *args, /* operation arguments */
459 xfs_dir2_sf_entry_t *sfep, /* pointer to new entry */
460 xfs_dir2_data_aoff_t offset, /* offset to use for new ent */
461 int new_isize) /* new directory size */
463 struct xfs_inode *dp = args->dp;
464 struct xfs_mount *mp = dp->i_mount;
465 int byteoff; /* byte offset in sf dir */
466 xfs_dir2_sf_hdr_t *sfp; /* shortform structure */
468 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
469 byteoff = (int)((char *)sfep - (char *)sfp);
471 * Grow the in-inode space.
473 xfs_idata_realloc(dp, xfs_dir2_sf_entsize(mp, sfp, args->namelen),
476 * Need to set up again due to realloc of the inode data.
478 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
479 sfep = (xfs_dir2_sf_entry_t *)((char *)sfp + byteoff);
481 * Fill in the new entry.
483 sfep->namelen = args->namelen;
484 xfs_dir2_sf_put_offset(sfep, offset);
485 memcpy(sfep->name, args->name, sfep->namelen);
486 xfs_dir2_sf_put_ino(mp, sfp, sfep, args->inumber);
487 xfs_dir2_sf_put_ftype(mp, sfep, args->filetype);
490 * Update the header and inode.
493 if (args->inumber > XFS_DIR2_MAX_SHORT_INUM)
495 dp->i_d.di_size = new_isize;
496 xfs_dir2_sf_check(args);
500 * Add the new entry the "hard" way.
501 * The caller has already converted to 8 byte inode numbers if necessary,
502 * in which case we need to leave the i8count at 1.
503 * Find a hole that the new entry will fit into, and copy
504 * the first part of the entries, the new entry, and the last part of
509 xfs_dir2_sf_addname_hard(
510 xfs_da_args_t *args, /* operation arguments */
511 int objchange, /* changing inode number size */
512 int new_isize) /* new directory size */
514 struct xfs_inode *dp = args->dp;
515 struct xfs_mount *mp = dp->i_mount;
516 int add_datasize; /* data size need for new ent */
517 char *buf; /* buffer for old */
518 int eof; /* reached end of old dir */
519 int nbytes; /* temp for byte copies */
520 xfs_dir2_data_aoff_t new_offset; /* next offset value */
521 xfs_dir2_data_aoff_t offset; /* current offset value */
522 int old_isize; /* previous di_size */
523 xfs_dir2_sf_entry_t *oldsfep; /* entry in original dir */
524 xfs_dir2_sf_hdr_t *oldsfp; /* original shortform dir */
525 xfs_dir2_sf_entry_t *sfep; /* entry in new dir */
526 xfs_dir2_sf_hdr_t *sfp; /* new shortform dir */
529 * Copy the old directory to the stack buffer.
531 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
532 old_isize = (int)dp->i_d.di_size;
533 buf = kmem_alloc(old_isize, 0);
534 oldsfp = (xfs_dir2_sf_hdr_t *)buf;
535 memcpy(oldsfp, sfp, old_isize);
537 * Loop over the old directory finding the place we're going
538 * to insert the new entry.
539 * If it's going to end up at the end then oldsfep will point there.
541 for (offset = dp->d_ops->data_first_offset,
542 oldsfep = xfs_dir2_sf_firstentry(oldsfp),
543 add_datasize = dp->d_ops->data_entsize(args->namelen),
544 eof = (char *)oldsfep == &buf[old_isize];
546 offset = new_offset + dp->d_ops->data_entsize(oldsfep->namelen),
547 oldsfep = xfs_dir2_sf_nextentry(mp, oldsfp, oldsfep),
548 eof = (char *)oldsfep == &buf[old_isize]) {
549 new_offset = xfs_dir2_sf_get_offset(oldsfep);
550 if (offset + add_datasize <= new_offset)
554 * Get rid of the old directory, then allocate space for
555 * the new one. We do this so xfs_idata_realloc won't copy
558 xfs_idata_realloc(dp, -old_isize, XFS_DATA_FORK);
559 xfs_idata_realloc(dp, new_isize, XFS_DATA_FORK);
561 * Reset the pointer since the buffer was reallocated.
563 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
565 * Copy the first part of the directory, including the header.
567 nbytes = (int)((char *)oldsfep - (char *)oldsfp);
568 memcpy(sfp, oldsfp, nbytes);
569 sfep = (xfs_dir2_sf_entry_t *)((char *)sfp + nbytes);
571 * Fill in the new entry, and update the header counts.
573 sfep->namelen = args->namelen;
574 xfs_dir2_sf_put_offset(sfep, offset);
575 memcpy(sfep->name, args->name, sfep->namelen);
576 xfs_dir2_sf_put_ino(mp, sfp, sfep, args->inumber);
577 xfs_dir2_sf_put_ftype(mp, sfep, args->filetype);
579 if (args->inumber > XFS_DIR2_MAX_SHORT_INUM && !objchange)
582 * If there's more left to copy, do that.
585 sfep = xfs_dir2_sf_nextentry(mp, sfp, sfep);
586 memcpy(sfep, oldsfep, old_isize - nbytes);
589 dp->i_d.di_size = new_isize;
590 xfs_dir2_sf_check(args);
594 * Decide if the new entry will fit at all.
595 * If it will fit, pick between adding the new entry to the end (easy)
596 * or somewhere else (hard).
597 * Return 0 (won't fit), 1 (easy), 2 (hard).
600 static int /* pick result */
601 xfs_dir2_sf_addname_pick(
602 xfs_da_args_t *args, /* operation arguments */
603 int objchange, /* inode # size changes */
604 xfs_dir2_sf_entry_t **sfepp, /* out(1): new entry ptr */
605 xfs_dir2_data_aoff_t *offsetp) /* out(1): new offset */
607 struct xfs_inode *dp = args->dp;
608 struct xfs_mount *mp = dp->i_mount;
609 int holefit; /* found hole it will fit in */
610 int i; /* entry number */
611 xfs_dir2_data_aoff_t offset; /* data block offset */
612 xfs_dir2_sf_entry_t *sfep; /* shortform entry */
613 xfs_dir2_sf_hdr_t *sfp; /* shortform structure */
614 int size; /* entry's data size */
615 int used; /* data bytes used */
617 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
618 size = dp->d_ops->data_entsize(args->namelen);
619 offset = dp->d_ops->data_first_offset;
620 sfep = xfs_dir2_sf_firstentry(sfp);
623 * Loop over sf entries.
624 * Keep track of data offset and whether we've seen a place
625 * to insert the new entry.
627 for (i = 0; i < sfp->count; i++) {
629 holefit = offset + size <= xfs_dir2_sf_get_offset(sfep);
630 offset = xfs_dir2_sf_get_offset(sfep) +
631 dp->d_ops->data_entsize(sfep->namelen);
632 sfep = xfs_dir2_sf_nextentry(mp, sfp, sfep);
635 * Calculate data bytes used excluding the new entry, if this
636 * was a data block (block form directory).
639 (sfp->count + 3) * (uint)sizeof(xfs_dir2_leaf_entry_t) +
640 (uint)sizeof(xfs_dir2_block_tail_t);
642 * If it won't fit in a block form then we can't insert it,
643 * we'll go back, convert to block, then try the insert and convert
646 if (used + (holefit ? 0 : size) > args->geo->blksize)
649 * If changing the inode number size, do it the hard way.
654 * If it won't fit at the end then do it the hard way (use the hole).
656 if (used + size > args->geo->blksize)
659 * Do it the easy way.
668 * Check consistency of shortform directory, assert if bad.
672 xfs_da_args_t *args) /* operation arguments */
674 struct xfs_inode *dp = args->dp;
675 struct xfs_mount *mp = dp->i_mount;
676 int i; /* entry number */
677 int i8count; /* number of big inode#s */
678 xfs_ino_t ino; /* entry inode number */
679 int offset; /* data offset */
680 xfs_dir2_sf_entry_t *sfep; /* shortform dir entry */
681 xfs_dir2_sf_hdr_t *sfp; /* shortform structure */
683 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
684 offset = dp->d_ops->data_first_offset;
685 ino = xfs_dir2_sf_get_parent_ino(sfp);
686 i8count = ino > XFS_DIR2_MAX_SHORT_INUM;
688 for (i = 0, sfep = xfs_dir2_sf_firstentry(sfp);
690 i++, sfep = xfs_dir2_sf_nextentry(mp, sfp, sfep)) {
691 ASSERT(xfs_dir2_sf_get_offset(sfep) >= offset);
692 ino = xfs_dir2_sf_get_ino(mp, sfp, sfep);
693 i8count += ino > XFS_DIR2_MAX_SHORT_INUM;
695 xfs_dir2_sf_get_offset(sfep) +
696 dp->d_ops->data_entsize(sfep->namelen);
697 ASSERT(xfs_dir2_sf_get_ftype(mp, sfep) < XFS_DIR3_FT_MAX);
699 ASSERT(i8count == sfp->i8count);
700 ASSERT((char *)sfep - (char *)sfp == dp->i_d.di_size);
702 (sfp->count + 2) * (uint)sizeof(xfs_dir2_leaf_entry_t) +
703 (uint)sizeof(xfs_dir2_block_tail_t) <= args->geo->blksize);
707 /* Verify the consistency of an inline directory. */
710 struct xfs_inode *ip)
712 struct xfs_mount *mp = ip->i_mount;
713 struct xfs_dir2_sf_hdr *sfp;
714 struct xfs_dir2_sf_entry *sfep;
715 struct xfs_dir2_sf_entry *next_sfep;
717 const struct xfs_dir_ops *dops;
718 struct xfs_ifork *ifp;
727 ASSERT(ip->i_d.di_format == XFS_DINODE_FMT_LOCAL);
729 * xfs_iread calls us before xfs_setup_inode sets up ip->d_ops,
730 * so we can only trust the mountpoint to have the right pointer.
732 dops = xfs_dir_get_ops(mp, NULL);
734 ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
735 sfp = (struct xfs_dir2_sf_hdr *)ifp->if_u1.if_data;
736 size = ifp->if_bytes;
739 * Give up if the directory is way too short.
741 if (size <= offsetof(struct xfs_dir2_sf_hdr, parent) ||
742 size < xfs_dir2_sf_hdr_size(sfp->i8count))
743 return __this_address;
745 endp = (char *)sfp + size;
748 ino = xfs_dir2_sf_get_parent_ino(sfp);
749 i8count = ino > XFS_DIR2_MAX_SHORT_INUM;
750 error = xfs_dir_ino_validate(mp, ino);
752 return __this_address;
753 offset = dops->data_first_offset;
755 /* Check all reported entries */
756 sfep = xfs_dir2_sf_firstentry(sfp);
757 for (i = 0; i < sfp->count; i++) {
759 * struct xfs_dir2_sf_entry has a variable length.
760 * Check the fixed-offset parts of the structure are
761 * within the data buffer.
763 if (((char *)sfep + sizeof(*sfep)) >= endp)
764 return __this_address;
766 /* Don't allow names with known bad length. */
767 if (sfep->namelen == 0)
768 return __this_address;
771 * Check that the variable-length part of the structure is
772 * within the data buffer. The next entry starts after the
773 * name component, so nextentry is an acceptable test.
775 next_sfep = xfs_dir2_sf_nextentry(mp, sfp, sfep);
776 if (endp < (char *)next_sfep)
777 return __this_address;
779 /* Check that the offsets always increase. */
780 if (xfs_dir2_sf_get_offset(sfep) < offset)
781 return __this_address;
783 /* Check the inode number. */
784 ino = xfs_dir2_sf_get_ino(mp, sfp, sfep);
785 i8count += ino > XFS_DIR2_MAX_SHORT_INUM;
786 error = xfs_dir_ino_validate(mp, ino);
788 return __this_address;
790 /* Check the file type. */
791 filetype = xfs_dir2_sf_get_ftype(mp, sfep);
792 if (filetype >= XFS_DIR3_FT_MAX)
793 return __this_address;
795 offset = xfs_dir2_sf_get_offset(sfep) +
796 dops->data_entsize(sfep->namelen);
800 if (i8count != sfp->i8count)
801 return __this_address;
802 if ((void *)sfep != (void *)endp)
803 return __this_address;
805 /* Make sure this whole thing ought to be in local format. */
806 if (offset + (sfp->count + 2) * (uint)sizeof(xfs_dir2_leaf_entry_t) +
807 (uint)sizeof(xfs_dir2_block_tail_t) > mp->m_dir_geo->blksize)
808 return __this_address;
814 * Create a new (shortform) directory.
816 int /* error, always 0 */
818 xfs_da_args_t *args, /* operation arguments */
819 xfs_ino_t pino) /* parent inode number */
821 xfs_inode_t *dp; /* incore directory inode */
822 int i8count; /* parent inode is an 8-byte number */
823 xfs_dir2_sf_hdr_t *sfp; /* shortform structure */
824 int size; /* directory size */
826 trace_xfs_dir2_sf_create(args);
831 ASSERT(dp->i_d.di_size == 0);
833 * If it's currently a zero-length extent file,
834 * convert it to local format.
836 if (dp->i_d.di_format == XFS_DINODE_FMT_EXTENTS) {
837 dp->i_df.if_flags &= ~XFS_IFEXTENTS; /* just in case */
838 dp->i_d.di_format = XFS_DINODE_FMT_LOCAL;
839 xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE);
840 dp->i_df.if_flags |= XFS_IFINLINE;
842 ASSERT(dp->i_df.if_flags & XFS_IFINLINE);
843 ASSERT(dp->i_df.if_bytes == 0);
844 i8count = pino > XFS_DIR2_MAX_SHORT_INUM;
845 size = xfs_dir2_sf_hdr_size(i8count);
847 * Make a buffer for the data.
849 xfs_idata_realloc(dp, size, XFS_DATA_FORK);
851 * Fill in the header,
853 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
854 sfp->i8count = i8count;
856 * Now can put in the inode number, since i8count is set.
858 xfs_dir2_sf_put_parent_ino(sfp, pino);
860 dp->i_d.di_size = size;
861 xfs_dir2_sf_check(args);
862 xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE | XFS_ILOG_DDATA);
867 * Lookup an entry in a shortform directory.
868 * Returns EEXIST if found, ENOENT if not found.
872 xfs_da_args_t *args) /* operation arguments */
874 struct xfs_inode *dp = args->dp;
875 struct xfs_mount *mp = dp->i_mount;
876 int i; /* entry index */
878 xfs_dir2_sf_entry_t *sfep; /* shortform directory entry */
879 xfs_dir2_sf_hdr_t *sfp; /* shortform structure */
880 enum xfs_dacmp cmp; /* comparison result */
881 xfs_dir2_sf_entry_t *ci_sfep; /* case-insens. entry */
883 trace_xfs_dir2_sf_lookup(args);
885 xfs_dir2_sf_check(args);
887 ASSERT(dp->i_df.if_flags & XFS_IFINLINE);
888 ASSERT(dp->i_d.di_size >= offsetof(struct xfs_dir2_sf_hdr, parent));
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_hdr_t *)dp->i_df.if_u1.if_data;
892 ASSERT(dp->i_d.di_size >= xfs_dir2_sf_hdr_size(sfp->i8count));
896 if (args->namelen == 1 && args->name[0] == '.') {
897 args->inumber = dp->i_ino;
898 args->cmpresult = XFS_CMP_EXACT;
899 args->filetype = XFS_DIR3_FT_DIR;
903 * Special case for ..
905 if (args->namelen == 2 &&
906 args->name[0] == '.' && args->name[1] == '.') {
907 args->inumber = xfs_dir2_sf_get_parent_ino(sfp);
908 args->cmpresult = XFS_CMP_EXACT;
909 args->filetype = XFS_DIR3_FT_DIR;
913 * Loop over all the entries trying to match ours.
916 for (i = 0, sfep = xfs_dir2_sf_firstentry(sfp); i < sfp->count;
917 i++, sfep = xfs_dir2_sf_nextentry(mp, sfp, sfep)) {
919 * Compare name and if it's an exact match, return the inode
920 * number. If it's the first case-insensitive match, store the
921 * inode number and continue looking for an exact match.
923 cmp = dp->i_mount->m_dirnameops->compname(args, sfep->name,
925 if (cmp != XFS_CMP_DIFFERENT && cmp != args->cmpresult) {
926 args->cmpresult = cmp;
927 args->inumber = xfs_dir2_sf_get_ino(mp, sfp, sfep);
928 args->filetype = xfs_dir2_sf_get_ftype(mp, sfep);
929 if (cmp == XFS_CMP_EXACT)
934 ASSERT(args->op_flags & XFS_DA_OP_OKNOENT);
936 * Here, we can only be doing a lookup (not a rename or replace).
937 * If a case-insensitive match was not found, return -ENOENT.
941 /* otherwise process the CI match as required by the caller */
942 error = xfs_dir_cilookup_result(args, ci_sfep->name, ci_sfep->namelen);
947 * Remove an entry from a shortform directory.
950 xfs_dir2_sf_removename(
953 struct xfs_inode *dp = args->dp;
954 struct xfs_mount *mp = dp->i_mount;
955 int byteoff; /* offset of removed entry */
956 int entsize; /* this entry's size */
957 int i; /* shortform entry index */
958 int newsize; /* new inode size */
959 int oldsize; /* old inode size */
960 xfs_dir2_sf_entry_t *sfep; /* shortform directory entry */
961 xfs_dir2_sf_hdr_t *sfp; /* shortform structure */
963 trace_xfs_dir2_sf_removename(args);
965 ASSERT(dp->i_df.if_flags & XFS_IFINLINE);
966 oldsize = (int)dp->i_d.di_size;
967 ASSERT(oldsize >= offsetof(struct xfs_dir2_sf_hdr, parent));
968 ASSERT(dp->i_df.if_bytes == oldsize);
969 ASSERT(dp->i_df.if_u1.if_data != NULL);
970 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
971 ASSERT(oldsize >= xfs_dir2_sf_hdr_size(sfp->i8count));
973 * Loop over the old directory entries.
974 * Find the one we're deleting.
976 for (i = 0, sfep = xfs_dir2_sf_firstentry(sfp); i < sfp->count;
977 i++, sfep = xfs_dir2_sf_nextentry(mp, sfp, sfep)) {
978 if (xfs_da_compname(args, sfep->name, sfep->namelen) ==
980 ASSERT(xfs_dir2_sf_get_ino(mp, sfp, sfep) ==
993 byteoff = (int)((char *)sfep - (char *)sfp);
994 entsize = xfs_dir2_sf_entsize(mp, sfp, args->namelen);
995 newsize = oldsize - entsize;
997 * Copy the part if any after the removed entry, sliding it down.
999 if (byteoff + entsize < oldsize)
1000 memmove((char *)sfp + byteoff, (char *)sfp + byteoff + entsize,
1001 oldsize - (byteoff + entsize));
1003 * Fix up the header and file size.
1006 dp->i_d.di_size = newsize;
1008 * Reallocate, making it smaller.
1010 xfs_idata_realloc(dp, newsize - oldsize, XFS_DATA_FORK);
1011 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
1013 * Are we changing inode number size?
1015 if (args->inumber > XFS_DIR2_MAX_SHORT_INUM) {
1016 if (sfp->i8count == 1)
1017 xfs_dir2_sf_toino4(args);
1021 xfs_dir2_sf_check(args);
1022 xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE | XFS_ILOG_DDATA);
1027 * Replace the inode number of an entry in a shortform directory.
1030 xfs_dir2_sf_replace(
1031 xfs_da_args_t *args) /* operation arguments */
1033 struct xfs_inode *dp = args->dp;
1034 struct xfs_mount *mp = dp->i_mount;
1035 int i; /* entry index */
1036 xfs_ino_t ino=0; /* entry old inode number */
1037 int i8elevated; /* sf_toino8 set i8count=1 */
1038 xfs_dir2_sf_entry_t *sfep; /* shortform directory entry */
1039 xfs_dir2_sf_hdr_t *sfp; /* shortform structure */
1041 trace_xfs_dir2_sf_replace(args);
1043 ASSERT(dp->i_df.if_flags & XFS_IFINLINE);
1044 ASSERT(dp->i_d.di_size >= offsetof(struct xfs_dir2_sf_hdr, parent));
1045 ASSERT(dp->i_df.if_bytes == dp->i_d.di_size);
1046 ASSERT(dp->i_df.if_u1.if_data != NULL);
1047 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
1048 ASSERT(dp->i_d.di_size >= xfs_dir2_sf_hdr_size(sfp->i8count));
1051 * New inode number is large, and need to convert to 8-byte inodes.
1053 if (args->inumber > XFS_DIR2_MAX_SHORT_INUM && sfp->i8count == 0) {
1054 int error; /* error return value */
1055 int newsize; /* new inode size */
1057 newsize = dp->i_df.if_bytes + (sfp->count + 1) * XFS_INO64_DIFF;
1059 * Won't fit as shortform, convert to block then do replace.
1061 if (newsize > XFS_IFORK_DSIZE(dp)) {
1062 error = xfs_dir2_sf_to_block(args);
1066 return xfs_dir2_block_replace(args);
1069 * Still fits, convert to 8-byte now.
1071 xfs_dir2_sf_toino8(args);
1073 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
1077 ASSERT(args->namelen != 1 || args->name[0] != '.');
1079 * Replace ..'s entry.
1081 if (args->namelen == 2 &&
1082 args->name[0] == '.' && args->name[1] == '.') {
1083 ino = xfs_dir2_sf_get_parent_ino(sfp);
1084 ASSERT(args->inumber != ino);
1085 xfs_dir2_sf_put_parent_ino(sfp, args->inumber);
1088 * Normal entry, look for the name.
1091 for (i = 0, sfep = xfs_dir2_sf_firstentry(sfp); i < sfp->count;
1092 i++, sfep = xfs_dir2_sf_nextentry(mp, sfp, sfep)) {
1093 if (xfs_da_compname(args, sfep->name, sfep->namelen) ==
1095 ino = xfs_dir2_sf_get_ino(mp, sfp, sfep);
1096 ASSERT(args->inumber != ino);
1097 xfs_dir2_sf_put_ino(mp, sfp, sfep,
1099 xfs_dir2_sf_put_ftype(mp, sfep, args->filetype);
1106 if (i == sfp->count) {
1107 ASSERT(args->op_flags & XFS_DA_OP_OKNOENT);
1109 xfs_dir2_sf_toino4(args);
1114 * See if the old number was large, the new number is small.
1116 if (ino > XFS_DIR2_MAX_SHORT_INUM &&
1117 args->inumber <= XFS_DIR2_MAX_SHORT_INUM) {
1119 * And the old count was one, so need to convert to small.
1121 if (sfp->i8count == 1)
1122 xfs_dir2_sf_toino4(args);
1127 * See if the old number was small, the new number is large.
1129 if (ino <= XFS_DIR2_MAX_SHORT_INUM &&
1130 args->inumber > XFS_DIR2_MAX_SHORT_INUM) {
1132 * add to the i8count unless we just converted to 8-byte
1133 * inodes (which does an implied i8count = 1)
1135 ASSERT(sfp->i8count != 0);
1139 xfs_dir2_sf_check(args);
1140 xfs_trans_log_inode(args->trans, dp, XFS_ILOG_DDATA);
1145 * Convert from 8-byte inode numbers to 4-byte inode numbers.
1146 * The last 8-byte inode number is gone, but the count is still 1.
1150 xfs_da_args_t *args) /* operation arguments */
1152 struct xfs_inode *dp = args->dp;
1153 struct xfs_mount *mp = dp->i_mount;
1154 char *buf; /* old dir's buffer */
1155 int i; /* entry index */
1156 int newsize; /* new inode size */
1157 xfs_dir2_sf_entry_t *oldsfep; /* old sf entry */
1158 xfs_dir2_sf_hdr_t *oldsfp; /* old sf directory */
1159 int oldsize; /* old inode size */
1160 xfs_dir2_sf_entry_t *sfep; /* new sf entry */
1161 xfs_dir2_sf_hdr_t *sfp; /* new sf directory */
1163 trace_xfs_dir2_sf_toino4(args);
1166 * Copy the old directory to the buffer.
1167 * Then nuke it from the inode, and add the new buffer to the inode.
1168 * Don't want xfs_idata_realloc copying the data here.
1170 oldsize = dp->i_df.if_bytes;
1171 buf = kmem_alloc(oldsize, 0);
1172 oldsfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
1173 ASSERT(oldsfp->i8count == 1);
1174 memcpy(buf, oldsfp, oldsize);
1176 * Compute the new inode size.
1178 newsize = oldsize - (oldsfp->count + 1) * XFS_INO64_DIFF;
1179 xfs_idata_realloc(dp, -oldsize, XFS_DATA_FORK);
1180 xfs_idata_realloc(dp, newsize, XFS_DATA_FORK);
1182 * Reset our pointers, the data has moved.
1184 oldsfp = (xfs_dir2_sf_hdr_t *)buf;
1185 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
1187 * Fill in the new header.
1189 sfp->count = oldsfp->count;
1191 xfs_dir2_sf_put_parent_ino(sfp, xfs_dir2_sf_get_parent_ino(oldsfp));
1193 * Copy the entries field by field.
1195 for (i = 0, sfep = xfs_dir2_sf_firstentry(sfp),
1196 oldsfep = xfs_dir2_sf_firstentry(oldsfp);
1198 i++, sfep = xfs_dir2_sf_nextentry(mp, sfp, sfep),
1199 oldsfep = xfs_dir2_sf_nextentry(mp, oldsfp, oldsfep)) {
1200 sfep->namelen = oldsfep->namelen;
1201 memcpy(sfep->offset, oldsfep->offset, sizeof(sfep->offset));
1202 memcpy(sfep->name, oldsfep->name, sfep->namelen);
1203 xfs_dir2_sf_put_ino(mp, sfp, sfep,
1204 xfs_dir2_sf_get_ino(mp, oldsfp, oldsfep));
1205 xfs_dir2_sf_put_ftype(mp, sfep,
1206 xfs_dir2_sf_get_ftype(mp, 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);
1217 * Convert existing entries from 4-byte inode numbers to 8-byte inode numbers.
1218 * The new entry w/ an 8-byte inode number is not there yet; we leave with
1219 * i8count set to 1, but no corresponding 8-byte entry.
1223 xfs_da_args_t *args) /* operation arguments */
1225 struct xfs_inode *dp = args->dp;
1226 struct xfs_mount *mp = dp->i_mount;
1227 char *buf; /* old dir's buffer */
1228 int i; /* entry index */
1229 int newsize; /* new inode size */
1230 xfs_dir2_sf_entry_t *oldsfep; /* old sf entry */
1231 xfs_dir2_sf_hdr_t *oldsfp; /* old sf directory */
1232 int oldsize; /* old inode size */
1233 xfs_dir2_sf_entry_t *sfep; /* new sf entry */
1234 xfs_dir2_sf_hdr_t *sfp; /* new sf directory */
1236 trace_xfs_dir2_sf_toino8(args);
1239 * Copy the old directory to the buffer.
1240 * Then nuke it from the inode, and add the new buffer to the inode.
1241 * Don't want xfs_idata_realloc copying the data here.
1243 oldsize = dp->i_df.if_bytes;
1244 buf = kmem_alloc(oldsize, 0);
1245 oldsfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
1246 ASSERT(oldsfp->i8count == 0);
1247 memcpy(buf, oldsfp, oldsize);
1249 * Compute the new inode size (nb: entry count + 1 for parent)
1251 newsize = oldsize + (oldsfp->count + 1) * XFS_INO64_DIFF;
1252 xfs_idata_realloc(dp, -oldsize, XFS_DATA_FORK);
1253 xfs_idata_realloc(dp, newsize, XFS_DATA_FORK);
1255 * Reset our pointers, the data has moved.
1257 oldsfp = (xfs_dir2_sf_hdr_t *)buf;
1258 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
1260 * Fill in the new header.
1262 sfp->count = oldsfp->count;
1264 xfs_dir2_sf_put_parent_ino(sfp, xfs_dir2_sf_get_parent_ino(oldsfp));
1266 * Copy the entries field by field.
1268 for (i = 0, sfep = xfs_dir2_sf_firstentry(sfp),
1269 oldsfep = xfs_dir2_sf_firstentry(oldsfp);
1271 i++, sfep = xfs_dir2_sf_nextentry(mp, sfp, sfep),
1272 oldsfep = xfs_dir2_sf_nextentry(mp, oldsfp, oldsfep)) {
1273 sfep->namelen = oldsfep->namelen;
1274 memcpy(sfep->offset, oldsfep->offset, sizeof(sfep->offset));
1275 memcpy(sfep->name, oldsfep->name, sfep->namelen);
1276 xfs_dir2_sf_put_ino(mp, sfp, sfep,
1277 xfs_dir2_sf_get_ino(mp, oldsfp, oldsfep));
1278 xfs_dir2_sf_put_ftype(mp, sfep,
1279 xfs_dir2_sf_get_ftype(mp, oldsfep));
1282 * Clean up the inode.
1285 dp->i_d.di_size = newsize;
1286 xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE | XFS_ILOG_DDATA);