arm64: kgdb: Fix single-step exception handling oops
[linux/fpc-iii.git] / fs / xfs / libxfs / xfs_dir2_sf.c
blobe84af093b2ab99e5d7a75467bc6d7a490682611a
1 /*
2 * Copyright (c) 2000-2003,2005 Silicon Graphics, Inc.
3 * All Rights Reserved.
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation.
9 * This program is distributed in the hope that it would be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write the Free Software Foundation,
16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 #include "xfs.h"
19 #include "xfs_fs.h"
20 #include "xfs_format.h"
21 #include "xfs_log_format.h"
22 #include "xfs_trans_resv.h"
23 #include "xfs_mount.h"
24 #include "xfs_da_format.h"
25 #include "xfs_da_btree.h"
26 #include "xfs_inode.h"
27 #include "xfs_trans.h"
28 #include "xfs_inode_item.h"
29 #include "xfs_error.h"
30 #include "xfs_dir2.h"
31 #include "xfs_dir2_priv.h"
32 #include "xfs_trace.h"
35 * Prototypes for internal functions.
37 static void xfs_dir2_sf_addname_easy(xfs_da_args_t *args,
38 xfs_dir2_sf_entry_t *sfep,
39 xfs_dir2_data_aoff_t offset,
40 int new_isize);
41 static void xfs_dir2_sf_addname_hard(xfs_da_args_t *args, int objchange,
42 int new_isize);
43 static int xfs_dir2_sf_addname_pick(xfs_da_args_t *args, int objchange,
44 xfs_dir2_sf_entry_t **sfepp,
45 xfs_dir2_data_aoff_t *offsetp);
46 #ifdef DEBUG
47 static void xfs_dir2_sf_check(xfs_da_args_t *args);
48 #else
49 #define xfs_dir2_sf_check(args)
50 #endif /* DEBUG */
52 static void xfs_dir2_sf_toino4(xfs_da_args_t *args);
53 static void xfs_dir2_sf_toino8(xfs_da_args_t *args);
56 * Given a block directory (dp/block), calculate its size as a shortform (sf)
57 * directory and a header for the sf directory, if it will fit it the
58 * space currently present in the inode. If it won't fit, the output
59 * size is too big (but not accurate).
61 int /* size for sf form */
62 xfs_dir2_block_sfsize(
63 xfs_inode_t *dp, /* incore inode pointer */
64 xfs_dir2_data_hdr_t *hdr, /* block directory data */
65 xfs_dir2_sf_hdr_t *sfhp) /* output: header for sf form */
67 xfs_dir2_dataptr_t addr; /* data entry address */
68 xfs_dir2_leaf_entry_t *blp; /* leaf area of the block */
69 xfs_dir2_block_tail_t *btp; /* tail area of the block */
70 int count; /* shortform entry count */
71 xfs_dir2_data_entry_t *dep; /* data entry in the block */
72 int i; /* block entry index */
73 int i8count; /* count of big-inode entries */
74 int isdot; /* entry is "." */
75 int isdotdot; /* entry is ".." */
76 xfs_mount_t *mp; /* mount structure pointer */
77 int namelen; /* total name bytes */
78 xfs_ino_t parent = 0; /* parent inode number */
79 int size=0; /* total computed size */
80 int has_ftype;
81 struct xfs_da_geometry *geo;
83 mp = dp->i_mount;
84 geo = mp->m_dir_geo;
87 * if there is a filetype field, add the extra byte to the namelen
88 * for each entry that we see.
90 has_ftype = xfs_sb_version_hasftype(&mp->m_sb) ? 1 : 0;
92 count = i8count = namelen = 0;
93 btp = xfs_dir2_block_tail_p(geo, hdr);
94 blp = xfs_dir2_block_leaf_p(btp);
97 * Iterate over the block's data entries by using the leaf pointers.
99 for (i = 0; i < be32_to_cpu(btp->count); i++) {
100 if ((addr = be32_to_cpu(blp[i].address)) == XFS_DIR2_NULL_DATAPTR)
101 continue;
103 * Calculate the pointer to the entry at hand.
105 dep = (xfs_dir2_data_entry_t *)((char *)hdr +
106 xfs_dir2_dataptr_to_off(geo, addr));
108 * Detect . and .., so we can special-case them.
109 * . is not included in sf directories.
110 * .. is included by just the parent inode number.
112 isdot = dep->namelen == 1 && dep->name[0] == '.';
113 isdotdot =
114 dep->namelen == 2 &&
115 dep->name[0] == '.' && dep->name[1] == '.';
117 if (!isdot)
118 i8count += be64_to_cpu(dep->inumber) > XFS_DIR2_MAX_SHORT_INUM;
120 /* take into account the file type field */
121 if (!isdot && !isdotdot) {
122 count++;
123 namelen += dep->namelen + has_ftype;
124 } else if (isdotdot)
125 parent = be64_to_cpu(dep->inumber);
127 * Calculate the new size, see if we should give up yet.
129 size = xfs_dir2_sf_hdr_size(i8count) + /* header */
130 count * 3 * sizeof(u8) + /* namelen + offset */
131 namelen + /* name */
132 (i8count ? /* inumber */
133 count * XFS_INO64_SIZE :
134 count * XFS_INO32_SIZE);
135 if (size > XFS_IFORK_DSIZE(dp))
136 return size; /* size value is a failure */
139 * Create the output header, if it worked.
141 sfhp->count = count;
142 sfhp->i8count = i8count;
143 dp->d_ops->sf_put_parent_ino(sfhp, parent);
144 return size;
148 * Convert a block format directory to shortform.
149 * Caller has already checked that it will fit, and built us a header.
151 int /* error */
152 xfs_dir2_block_to_sf(
153 xfs_da_args_t *args, /* operation arguments */
154 struct xfs_buf *bp,
155 int size, /* shortform directory size */
156 xfs_dir2_sf_hdr_t *sfhp) /* shortform directory hdr */
158 xfs_dir2_data_hdr_t *hdr; /* block header */
159 xfs_dir2_block_tail_t *btp; /* block tail pointer */
160 xfs_dir2_data_entry_t *dep; /* data entry pointer */
161 xfs_inode_t *dp; /* incore directory inode */
162 xfs_dir2_data_unused_t *dup; /* unused data pointer */
163 char *endptr; /* end of data entries */
164 int error; /* error return value */
165 int logflags; /* inode logging flags */
166 xfs_mount_t *mp; /* filesystem mount point */
167 char *ptr; /* current data pointer */
168 xfs_dir2_sf_entry_t *sfep; /* shortform entry */
169 xfs_dir2_sf_hdr_t *sfp; /* shortform directory header */
170 xfs_dir2_sf_hdr_t *dst; /* temporary data buffer */
172 trace_xfs_dir2_block_to_sf(args);
174 dp = args->dp;
175 mp = dp->i_mount;
178 * allocate a temporary destination buffer the size of the inode
179 * to format the data into. Once we have formatted the data, we
180 * can free the block and copy the formatted data into the inode literal
181 * area.
183 dst = kmem_alloc(mp->m_sb.sb_inodesize, KM_SLEEP);
184 hdr = bp->b_addr;
187 * Copy the header into the newly allocate local space.
189 sfp = (xfs_dir2_sf_hdr_t *)dst;
190 memcpy(sfp, sfhp, xfs_dir2_sf_hdr_size(sfhp->i8count));
193 * Set up to loop over the block's entries.
195 btp = xfs_dir2_block_tail_p(args->geo, hdr);
196 ptr = (char *)dp->d_ops->data_entry_p(hdr);
197 endptr = (char *)xfs_dir2_block_leaf_p(btp);
198 sfep = xfs_dir2_sf_firstentry(sfp);
200 * Loop over the active and unused entries.
201 * Stop when we reach the leaf/tail portion of the block.
203 while (ptr < endptr) {
205 * If it's unused, just skip over it.
207 dup = (xfs_dir2_data_unused_t *)ptr;
208 if (be16_to_cpu(dup->freetag) == XFS_DIR2_DATA_FREE_TAG) {
209 ptr += be16_to_cpu(dup->length);
210 continue;
212 dep = (xfs_dir2_data_entry_t *)ptr;
214 * Skip .
216 if (dep->namelen == 1 && dep->name[0] == '.')
217 ASSERT(be64_to_cpu(dep->inumber) == dp->i_ino);
219 * Skip .., but make sure the inode number is right.
221 else if (dep->namelen == 2 &&
222 dep->name[0] == '.' && dep->name[1] == '.')
223 ASSERT(be64_to_cpu(dep->inumber) ==
224 dp->d_ops->sf_get_parent_ino(sfp));
226 * Normal entry, copy it into shortform.
228 else {
229 sfep->namelen = dep->namelen;
230 xfs_dir2_sf_put_offset(sfep,
231 (xfs_dir2_data_aoff_t)
232 ((char *)dep - (char *)hdr));
233 memcpy(sfep->name, dep->name, dep->namelen);
234 dp->d_ops->sf_put_ino(sfp, sfep,
235 be64_to_cpu(dep->inumber));
236 dp->d_ops->sf_put_ftype(sfep,
237 dp->d_ops->data_get_ftype(dep));
239 sfep = dp->d_ops->sf_nextentry(sfp, sfep);
241 ptr += dp->d_ops->data_entsize(dep->namelen);
243 ASSERT((char *)sfep - (char *)sfp == size);
245 /* now we are done with the block, we can shrink the inode */
246 logflags = XFS_ILOG_CORE;
247 error = xfs_dir2_shrink_inode(args, args->geo->datablk, bp);
248 if (error) {
249 ASSERT(error != -ENOSPC);
250 goto out;
254 * The buffer is now unconditionally gone, whether
255 * xfs_dir2_shrink_inode worked or not.
257 * Convert the inode to local format and copy the data in.
259 ASSERT(dp->i_df.if_bytes == 0);
260 xfs_init_local_fork(dp, XFS_DATA_FORK, dst, size);
261 dp->i_d.di_format = XFS_DINODE_FMT_LOCAL;
262 dp->i_d.di_size = size;
264 logflags |= XFS_ILOG_DDATA;
265 xfs_dir2_sf_check(args);
266 out:
267 xfs_trans_log_inode(args->trans, dp, logflags);
268 kmem_free(dst);
269 return error;
273 * Add a name to a shortform directory.
274 * There are two algorithms, "easy" and "hard" which we decide on
275 * before changing anything.
276 * Convert to block form if necessary, if the new entry won't fit.
278 int /* error */
279 xfs_dir2_sf_addname(
280 xfs_da_args_t *args) /* operation arguments */
282 xfs_inode_t *dp; /* incore directory inode */
283 int error; /* error return value */
284 int incr_isize; /* total change in size */
285 int new_isize; /* di_size after adding name */
286 int objchange; /* changing to 8-byte inodes */
287 xfs_dir2_data_aoff_t offset = 0; /* offset for new entry */
288 int pick; /* which algorithm to use */
289 xfs_dir2_sf_hdr_t *sfp; /* shortform structure */
290 xfs_dir2_sf_entry_t *sfep = NULL; /* shortform entry */
292 trace_xfs_dir2_sf_addname(args);
294 ASSERT(xfs_dir2_sf_lookup(args) == -ENOENT);
295 dp = args->dp;
296 ASSERT(dp->i_df.if_flags & XFS_IFINLINE);
298 * Make sure the shortform value has some of its header.
300 if (dp->i_d.di_size < offsetof(xfs_dir2_sf_hdr_t, parent)) {
301 ASSERT(XFS_FORCED_SHUTDOWN(dp->i_mount));
302 return -EIO;
304 ASSERT(dp->i_df.if_bytes == dp->i_d.di_size);
305 ASSERT(dp->i_df.if_u1.if_data != NULL);
306 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
307 ASSERT(dp->i_d.di_size >= xfs_dir2_sf_hdr_size(sfp->i8count));
309 * Compute entry (and change in) size.
311 incr_isize = dp->d_ops->sf_entsize(sfp, args->namelen);
312 objchange = 0;
315 * Do we have to change to 8 byte inodes?
317 if (args->inumber > XFS_DIR2_MAX_SHORT_INUM && sfp->i8count == 0) {
319 * Yes, adjust the inode size. old count + (parent + new)
321 incr_isize += (sfp->count + 2) * XFS_INO64_DIFF;
322 objchange = 1;
325 new_isize = (int)dp->i_d.di_size + incr_isize;
327 * Won't fit as shortform any more (due to size),
328 * or the pick routine says it won't (due to offset values).
330 if (new_isize > XFS_IFORK_DSIZE(dp) ||
331 (pick =
332 xfs_dir2_sf_addname_pick(args, objchange, &sfep, &offset)) == 0) {
334 * Just checking or no space reservation, it doesn't fit.
336 if ((args->op_flags & XFS_DA_OP_JUSTCHECK) || args->total == 0)
337 return -ENOSPC;
339 * Convert to block form then add the name.
341 error = xfs_dir2_sf_to_block(args);
342 if (error)
343 return error;
344 return xfs_dir2_block_addname(args);
347 * Just checking, it fits.
349 if (args->op_flags & XFS_DA_OP_JUSTCHECK)
350 return 0;
352 * Do it the easy way - just add it at the end.
354 if (pick == 1)
355 xfs_dir2_sf_addname_easy(args, sfep, offset, new_isize);
357 * Do it the hard way - look for a place to insert the new entry.
358 * Convert to 8 byte inode numbers first if necessary.
360 else {
361 ASSERT(pick == 2);
362 if (objchange)
363 xfs_dir2_sf_toino8(args);
364 xfs_dir2_sf_addname_hard(args, objchange, new_isize);
366 xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE | XFS_ILOG_DDATA);
367 return 0;
371 * Add the new entry the "easy" way.
372 * This is copying the old directory and adding the new entry at the end.
373 * Since it's sorted by "offset" we need room after the last offset
374 * that's already there, and then room to convert to a block directory.
375 * This is already checked by the pick routine.
377 static void
378 xfs_dir2_sf_addname_easy(
379 xfs_da_args_t *args, /* operation arguments */
380 xfs_dir2_sf_entry_t *sfep, /* pointer to new entry */
381 xfs_dir2_data_aoff_t offset, /* offset to use for new ent */
382 int new_isize) /* new directory size */
384 int byteoff; /* byte offset in sf dir */
385 xfs_inode_t *dp; /* incore directory inode */
386 xfs_dir2_sf_hdr_t *sfp; /* shortform structure */
388 dp = args->dp;
390 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
391 byteoff = (int)((char *)sfep - (char *)sfp);
393 * Grow the in-inode space.
395 xfs_idata_realloc(dp, dp->d_ops->sf_entsize(sfp, args->namelen),
396 XFS_DATA_FORK);
398 * Need to set up again due to realloc of the inode data.
400 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
401 sfep = (xfs_dir2_sf_entry_t *)((char *)sfp + byteoff);
403 * Fill in the new entry.
405 sfep->namelen = args->namelen;
406 xfs_dir2_sf_put_offset(sfep, offset);
407 memcpy(sfep->name, args->name, sfep->namelen);
408 dp->d_ops->sf_put_ino(sfp, sfep, args->inumber);
409 dp->d_ops->sf_put_ftype(sfep, args->filetype);
412 * Update the header and inode.
414 sfp->count++;
415 if (args->inumber > XFS_DIR2_MAX_SHORT_INUM)
416 sfp->i8count++;
417 dp->i_d.di_size = new_isize;
418 xfs_dir2_sf_check(args);
422 * Add the new entry the "hard" way.
423 * The caller has already converted to 8 byte inode numbers if necessary,
424 * in which case we need to leave the i8count at 1.
425 * Find a hole that the new entry will fit into, and copy
426 * the first part of the entries, the new entry, and the last part of
427 * the entries.
429 /* ARGSUSED */
430 static void
431 xfs_dir2_sf_addname_hard(
432 xfs_da_args_t *args, /* operation arguments */
433 int objchange, /* changing inode number size */
434 int new_isize) /* new directory size */
436 int add_datasize; /* data size need for new ent */
437 char *buf; /* buffer for old */
438 xfs_inode_t *dp; /* incore directory inode */
439 int eof; /* reached end of old dir */
440 int nbytes; /* temp for byte copies */
441 xfs_dir2_data_aoff_t new_offset; /* next offset value */
442 xfs_dir2_data_aoff_t offset; /* current offset value */
443 int old_isize; /* previous di_size */
444 xfs_dir2_sf_entry_t *oldsfep; /* entry in original dir */
445 xfs_dir2_sf_hdr_t *oldsfp; /* original shortform dir */
446 xfs_dir2_sf_entry_t *sfep; /* entry in new dir */
447 xfs_dir2_sf_hdr_t *sfp; /* new shortform dir */
450 * Copy the old directory to the stack buffer.
452 dp = args->dp;
454 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
455 old_isize = (int)dp->i_d.di_size;
456 buf = kmem_alloc(old_isize, KM_SLEEP);
457 oldsfp = (xfs_dir2_sf_hdr_t *)buf;
458 memcpy(oldsfp, sfp, old_isize);
460 * Loop over the old directory finding the place we're going
461 * to insert the new entry.
462 * If it's going to end up at the end then oldsfep will point there.
464 for (offset = dp->d_ops->data_first_offset,
465 oldsfep = xfs_dir2_sf_firstentry(oldsfp),
466 add_datasize = dp->d_ops->data_entsize(args->namelen),
467 eof = (char *)oldsfep == &buf[old_isize];
468 !eof;
469 offset = new_offset + dp->d_ops->data_entsize(oldsfep->namelen),
470 oldsfep = dp->d_ops->sf_nextentry(oldsfp, oldsfep),
471 eof = (char *)oldsfep == &buf[old_isize]) {
472 new_offset = xfs_dir2_sf_get_offset(oldsfep);
473 if (offset + add_datasize <= new_offset)
474 break;
477 * Get rid of the old directory, then allocate space for
478 * the new one. We do this so xfs_idata_realloc won't copy
479 * the data.
481 xfs_idata_realloc(dp, -old_isize, XFS_DATA_FORK);
482 xfs_idata_realloc(dp, new_isize, XFS_DATA_FORK);
484 * Reset the pointer since the buffer was reallocated.
486 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
488 * Copy the first part of the directory, including the header.
490 nbytes = (int)((char *)oldsfep - (char *)oldsfp);
491 memcpy(sfp, oldsfp, nbytes);
492 sfep = (xfs_dir2_sf_entry_t *)((char *)sfp + nbytes);
494 * Fill in the new entry, and update the header counts.
496 sfep->namelen = args->namelen;
497 xfs_dir2_sf_put_offset(sfep, offset);
498 memcpy(sfep->name, args->name, sfep->namelen);
499 dp->d_ops->sf_put_ino(sfp, sfep, args->inumber);
500 dp->d_ops->sf_put_ftype(sfep, args->filetype);
501 sfp->count++;
502 if (args->inumber > XFS_DIR2_MAX_SHORT_INUM && !objchange)
503 sfp->i8count++;
505 * If there's more left to copy, do that.
507 if (!eof) {
508 sfep = dp->d_ops->sf_nextentry(sfp, sfep);
509 memcpy(sfep, oldsfep, old_isize - nbytes);
511 kmem_free(buf);
512 dp->i_d.di_size = new_isize;
513 xfs_dir2_sf_check(args);
517 * Decide if the new entry will fit at all.
518 * If it will fit, pick between adding the new entry to the end (easy)
519 * or somewhere else (hard).
520 * Return 0 (won't fit), 1 (easy), 2 (hard).
522 /*ARGSUSED*/
523 static int /* pick result */
524 xfs_dir2_sf_addname_pick(
525 xfs_da_args_t *args, /* operation arguments */
526 int objchange, /* inode # size changes */
527 xfs_dir2_sf_entry_t **sfepp, /* out(1): new entry ptr */
528 xfs_dir2_data_aoff_t *offsetp) /* out(1): new offset */
530 xfs_inode_t *dp; /* incore directory inode */
531 int holefit; /* found hole it will fit in */
532 int i; /* entry number */
533 xfs_dir2_data_aoff_t offset; /* data block offset */
534 xfs_dir2_sf_entry_t *sfep; /* shortform entry */
535 xfs_dir2_sf_hdr_t *sfp; /* shortform structure */
536 int size; /* entry's data size */
537 int used; /* data bytes used */
539 dp = args->dp;
541 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
542 size = dp->d_ops->data_entsize(args->namelen);
543 offset = dp->d_ops->data_first_offset;
544 sfep = xfs_dir2_sf_firstentry(sfp);
545 holefit = 0;
547 * Loop over sf entries.
548 * Keep track of data offset and whether we've seen a place
549 * to insert the new entry.
551 for (i = 0; i < sfp->count; i++) {
552 if (!holefit)
553 holefit = offset + size <= xfs_dir2_sf_get_offset(sfep);
554 offset = xfs_dir2_sf_get_offset(sfep) +
555 dp->d_ops->data_entsize(sfep->namelen);
556 sfep = dp->d_ops->sf_nextentry(sfp, sfep);
559 * Calculate data bytes used excluding the new entry, if this
560 * was a data block (block form directory).
562 used = offset +
563 (sfp->count + 3) * (uint)sizeof(xfs_dir2_leaf_entry_t) +
564 (uint)sizeof(xfs_dir2_block_tail_t);
566 * If it won't fit in a block form then we can't insert it,
567 * we'll go back, convert to block, then try the insert and convert
568 * to leaf.
570 if (used + (holefit ? 0 : size) > args->geo->blksize)
571 return 0;
573 * If changing the inode number size, do it the hard way.
575 if (objchange)
576 return 2;
578 * If it won't fit at the end then do it the hard way (use the hole).
580 if (used + size > args->geo->blksize)
581 return 2;
583 * Do it the easy way.
585 *sfepp = sfep;
586 *offsetp = offset;
587 return 1;
590 #ifdef DEBUG
592 * Check consistency of shortform directory, assert if bad.
594 static void
595 xfs_dir2_sf_check(
596 xfs_da_args_t *args) /* operation arguments */
598 xfs_inode_t *dp; /* incore directory inode */
599 int i; /* entry number */
600 int i8count; /* number of big inode#s */
601 xfs_ino_t ino; /* entry inode number */
602 int offset; /* data offset */
603 xfs_dir2_sf_entry_t *sfep; /* shortform dir entry */
604 xfs_dir2_sf_hdr_t *sfp; /* shortform structure */
606 dp = args->dp;
608 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
609 offset = dp->d_ops->data_first_offset;
610 ino = dp->d_ops->sf_get_parent_ino(sfp);
611 i8count = ino > XFS_DIR2_MAX_SHORT_INUM;
613 for (i = 0, sfep = xfs_dir2_sf_firstentry(sfp);
614 i < sfp->count;
615 i++, sfep = dp->d_ops->sf_nextentry(sfp, sfep)) {
616 ASSERT(xfs_dir2_sf_get_offset(sfep) >= offset);
617 ino = dp->d_ops->sf_get_ino(sfp, sfep);
618 i8count += ino > XFS_DIR2_MAX_SHORT_INUM;
619 offset =
620 xfs_dir2_sf_get_offset(sfep) +
621 dp->d_ops->data_entsize(sfep->namelen);
622 ASSERT(dp->d_ops->sf_get_ftype(sfep) < XFS_DIR3_FT_MAX);
624 ASSERT(i8count == sfp->i8count);
625 ASSERT((char *)sfep - (char *)sfp == dp->i_d.di_size);
626 ASSERT(offset +
627 (sfp->count + 2) * (uint)sizeof(xfs_dir2_leaf_entry_t) +
628 (uint)sizeof(xfs_dir2_block_tail_t) <= args->geo->blksize);
630 #endif /* DEBUG */
632 /* Verify the consistency of an inline directory. */
634 xfs_dir2_sf_verify(
635 struct xfs_inode *ip)
637 struct xfs_mount *mp = ip->i_mount;
638 struct xfs_dir2_sf_hdr *sfp;
639 struct xfs_dir2_sf_entry *sfep;
640 struct xfs_dir2_sf_entry *next_sfep;
641 char *endp;
642 const struct xfs_dir_ops *dops;
643 struct xfs_ifork *ifp;
644 xfs_ino_t ino;
645 int i;
646 int i8count;
647 int offset;
648 int size;
649 int error;
650 __uint8_t filetype;
652 ASSERT(ip->i_d.di_format == XFS_DINODE_FMT_LOCAL);
654 * xfs_iread calls us before xfs_setup_inode sets up ip->d_ops,
655 * so we can only trust the mountpoint to have the right pointer.
657 dops = xfs_dir_get_ops(mp, NULL);
659 ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
660 sfp = (struct xfs_dir2_sf_hdr *)ifp->if_u1.if_data;
661 size = ifp->if_bytes;
664 * Give up if the directory is way too short.
666 if (size <= offsetof(struct xfs_dir2_sf_hdr, parent) ||
667 size < xfs_dir2_sf_hdr_size(sfp->i8count))
668 return -EFSCORRUPTED;
670 endp = (char *)sfp + size;
672 /* Check .. entry */
673 ino = dops->sf_get_parent_ino(sfp);
674 i8count = ino > XFS_DIR2_MAX_SHORT_INUM;
675 error = xfs_dir_ino_validate(mp, ino);
676 if (error)
677 return error;
678 offset = dops->data_first_offset;
680 /* Check all reported entries */
681 sfep = xfs_dir2_sf_firstentry(sfp);
682 for (i = 0; i < sfp->count; i++) {
684 * struct xfs_dir2_sf_entry has a variable length.
685 * Check the fixed-offset parts of the structure are
686 * within the data buffer.
688 if (((char *)sfep + sizeof(*sfep)) >= endp)
689 return -EFSCORRUPTED;
691 /* Don't allow names with known bad length. */
692 if (sfep->namelen == 0)
693 return -EFSCORRUPTED;
696 * Check that the variable-length part of the structure is
697 * within the data buffer. The next entry starts after the
698 * name component, so nextentry is an acceptable test.
700 next_sfep = dops->sf_nextentry(sfp, sfep);
701 if (endp < (char *)next_sfep)
702 return -EFSCORRUPTED;
704 /* Check that the offsets always increase. */
705 if (xfs_dir2_sf_get_offset(sfep) < offset)
706 return -EFSCORRUPTED;
708 /* Check the inode number. */
709 ino = dops->sf_get_ino(sfp, sfep);
710 i8count += ino > XFS_DIR2_MAX_SHORT_INUM;
711 error = xfs_dir_ino_validate(mp, ino);
712 if (error)
713 return error;
715 /* Check the file type. */
716 filetype = dops->sf_get_ftype(sfep);
717 if (filetype >= XFS_DIR3_FT_MAX)
718 return -EFSCORRUPTED;
720 offset = xfs_dir2_sf_get_offset(sfep) +
721 dops->data_entsize(sfep->namelen);
723 sfep = next_sfep;
725 if (i8count != sfp->i8count)
726 return -EFSCORRUPTED;
727 if ((void *)sfep != (void *)endp)
728 return -EFSCORRUPTED;
730 /* Make sure this whole thing ought to be in local format. */
731 if (offset + (sfp->count + 2) * (uint)sizeof(xfs_dir2_leaf_entry_t) +
732 (uint)sizeof(xfs_dir2_block_tail_t) > mp->m_dir_geo->blksize)
733 return -EFSCORRUPTED;
735 return 0;
739 * Create a new (shortform) directory.
741 int /* error, always 0 */
742 xfs_dir2_sf_create(
743 xfs_da_args_t *args, /* operation arguments */
744 xfs_ino_t pino) /* parent inode number */
746 xfs_inode_t *dp; /* incore directory inode */
747 int i8count; /* parent inode is an 8-byte number */
748 xfs_dir2_sf_hdr_t *sfp; /* shortform structure */
749 int size; /* directory size */
751 trace_xfs_dir2_sf_create(args);
753 dp = args->dp;
755 ASSERT(dp != NULL);
756 ASSERT(dp->i_d.di_size == 0);
758 * If it's currently a zero-length extent file,
759 * convert it to local format.
761 if (dp->i_d.di_format == XFS_DINODE_FMT_EXTENTS) {
762 dp->i_df.if_flags &= ~XFS_IFEXTENTS; /* just in case */
763 dp->i_d.di_format = XFS_DINODE_FMT_LOCAL;
764 xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE);
765 dp->i_df.if_flags |= XFS_IFINLINE;
767 ASSERT(dp->i_df.if_flags & XFS_IFINLINE);
768 ASSERT(dp->i_df.if_bytes == 0);
769 i8count = pino > XFS_DIR2_MAX_SHORT_INUM;
770 size = xfs_dir2_sf_hdr_size(i8count);
772 * Make a buffer for the data.
774 xfs_idata_realloc(dp, size, XFS_DATA_FORK);
776 * Fill in the header,
778 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
779 sfp->i8count = i8count;
781 * Now can put in the inode number, since i8count is set.
783 dp->d_ops->sf_put_parent_ino(sfp, pino);
784 sfp->count = 0;
785 dp->i_d.di_size = size;
786 xfs_dir2_sf_check(args);
787 xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE | XFS_ILOG_DDATA);
788 return 0;
792 * Lookup an entry in a shortform directory.
793 * Returns EEXIST if found, ENOENT if not found.
795 int /* error */
796 xfs_dir2_sf_lookup(
797 xfs_da_args_t *args) /* operation arguments */
799 xfs_inode_t *dp; /* incore directory inode */
800 int i; /* entry index */
801 int error;
802 xfs_dir2_sf_entry_t *sfep; /* shortform directory entry */
803 xfs_dir2_sf_hdr_t *sfp; /* shortform structure */
804 enum xfs_dacmp cmp; /* comparison result */
805 xfs_dir2_sf_entry_t *ci_sfep; /* case-insens. entry */
807 trace_xfs_dir2_sf_lookup(args);
809 xfs_dir2_sf_check(args);
810 dp = args->dp;
812 ASSERT(dp->i_df.if_flags & XFS_IFINLINE);
814 * Bail out if the directory is way too short.
816 if (dp->i_d.di_size < offsetof(xfs_dir2_sf_hdr_t, parent)) {
817 ASSERT(XFS_FORCED_SHUTDOWN(dp->i_mount));
818 return -EIO;
820 ASSERT(dp->i_df.if_bytes == dp->i_d.di_size);
821 ASSERT(dp->i_df.if_u1.if_data != NULL);
822 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
823 ASSERT(dp->i_d.di_size >= xfs_dir2_sf_hdr_size(sfp->i8count));
825 * Special case for .
827 if (args->namelen == 1 && args->name[0] == '.') {
828 args->inumber = dp->i_ino;
829 args->cmpresult = XFS_CMP_EXACT;
830 args->filetype = XFS_DIR3_FT_DIR;
831 return -EEXIST;
834 * Special case for ..
836 if (args->namelen == 2 &&
837 args->name[0] == '.' && args->name[1] == '.') {
838 args->inumber = dp->d_ops->sf_get_parent_ino(sfp);
839 args->cmpresult = XFS_CMP_EXACT;
840 args->filetype = XFS_DIR3_FT_DIR;
841 return -EEXIST;
844 * Loop over all the entries trying to match ours.
846 ci_sfep = NULL;
847 for (i = 0, sfep = xfs_dir2_sf_firstentry(sfp); i < sfp->count;
848 i++, sfep = dp->d_ops->sf_nextentry(sfp, sfep)) {
850 * Compare name and if it's an exact match, return the inode
851 * number. If it's the first case-insensitive match, store the
852 * inode number and continue looking for an exact match.
854 cmp = dp->i_mount->m_dirnameops->compname(args, sfep->name,
855 sfep->namelen);
856 if (cmp != XFS_CMP_DIFFERENT && cmp != args->cmpresult) {
857 args->cmpresult = cmp;
858 args->inumber = dp->d_ops->sf_get_ino(sfp, sfep);
859 args->filetype = dp->d_ops->sf_get_ftype(sfep);
860 if (cmp == XFS_CMP_EXACT)
861 return -EEXIST;
862 ci_sfep = sfep;
865 ASSERT(args->op_flags & XFS_DA_OP_OKNOENT);
867 * Here, we can only be doing a lookup (not a rename or replace).
868 * If a case-insensitive match was not found, return -ENOENT.
870 if (!ci_sfep)
871 return -ENOENT;
872 /* otherwise process the CI match as required by the caller */
873 error = xfs_dir_cilookup_result(args, ci_sfep->name, ci_sfep->namelen);
874 return error;
878 * Remove an entry from a shortform directory.
880 int /* error */
881 xfs_dir2_sf_removename(
882 xfs_da_args_t *args)
884 int byteoff; /* offset of removed entry */
885 xfs_inode_t *dp; /* incore directory inode */
886 int entsize; /* this entry's size */
887 int i; /* shortform entry index */
888 int newsize; /* new inode size */
889 int oldsize; /* old inode size */
890 xfs_dir2_sf_entry_t *sfep; /* shortform directory entry */
891 xfs_dir2_sf_hdr_t *sfp; /* shortform structure */
893 trace_xfs_dir2_sf_removename(args);
895 dp = args->dp;
897 ASSERT(dp->i_df.if_flags & XFS_IFINLINE);
898 oldsize = (int)dp->i_d.di_size;
900 * Bail out if the directory is way too short.
902 if (oldsize < offsetof(xfs_dir2_sf_hdr_t, parent)) {
903 ASSERT(XFS_FORCED_SHUTDOWN(dp->i_mount));
904 return -EIO;
906 ASSERT(dp->i_df.if_bytes == oldsize);
907 ASSERT(dp->i_df.if_u1.if_data != NULL);
908 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
909 ASSERT(oldsize >= xfs_dir2_sf_hdr_size(sfp->i8count));
911 * Loop over the old directory entries.
912 * Find the one we're deleting.
914 for (i = 0, sfep = xfs_dir2_sf_firstentry(sfp); i < sfp->count;
915 i++, sfep = dp->d_ops->sf_nextentry(sfp, sfep)) {
916 if (xfs_da_compname(args, sfep->name, sfep->namelen) ==
917 XFS_CMP_EXACT) {
918 ASSERT(dp->d_ops->sf_get_ino(sfp, sfep) ==
919 args->inumber);
920 break;
924 * Didn't find it.
926 if (i == sfp->count)
927 return -ENOENT;
929 * Calculate sizes.
931 byteoff = (int)((char *)sfep - (char *)sfp);
932 entsize = dp->d_ops->sf_entsize(sfp, args->namelen);
933 newsize = oldsize - entsize;
935 * Copy the part if any after the removed entry, sliding it down.
937 if (byteoff + entsize < oldsize)
938 memmove((char *)sfp + byteoff, (char *)sfp + byteoff + entsize,
939 oldsize - (byteoff + entsize));
941 * Fix up the header and file size.
943 sfp->count--;
944 dp->i_d.di_size = newsize;
946 * Reallocate, making it smaller.
948 xfs_idata_realloc(dp, newsize - oldsize, XFS_DATA_FORK);
949 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
951 * Are we changing inode number size?
953 if (args->inumber > XFS_DIR2_MAX_SHORT_INUM) {
954 if (sfp->i8count == 1)
955 xfs_dir2_sf_toino4(args);
956 else
957 sfp->i8count--;
959 xfs_dir2_sf_check(args);
960 xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE | XFS_ILOG_DDATA);
961 return 0;
965 * Replace the inode number of an entry in a shortform directory.
967 int /* error */
968 xfs_dir2_sf_replace(
969 xfs_da_args_t *args) /* operation arguments */
971 xfs_inode_t *dp; /* incore directory inode */
972 int i; /* entry index */
973 xfs_ino_t ino=0; /* entry old inode number */
974 int i8elevated; /* sf_toino8 set i8count=1 */
975 xfs_dir2_sf_entry_t *sfep; /* shortform directory entry */
976 xfs_dir2_sf_hdr_t *sfp; /* shortform structure */
978 trace_xfs_dir2_sf_replace(args);
980 dp = args->dp;
982 ASSERT(dp->i_df.if_flags & XFS_IFINLINE);
984 * Bail out if the shortform directory is way too small.
986 if (dp->i_d.di_size < offsetof(xfs_dir2_sf_hdr_t, parent)) {
987 ASSERT(XFS_FORCED_SHUTDOWN(dp->i_mount));
988 return -EIO;
990 ASSERT(dp->i_df.if_bytes == dp->i_d.di_size);
991 ASSERT(dp->i_df.if_u1.if_data != NULL);
992 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
993 ASSERT(dp->i_d.di_size >= xfs_dir2_sf_hdr_size(sfp->i8count));
996 * New inode number is large, and need to convert to 8-byte inodes.
998 if (args->inumber > XFS_DIR2_MAX_SHORT_INUM && sfp->i8count == 0) {
999 int error; /* error return value */
1000 int newsize; /* new inode size */
1002 newsize = dp->i_df.if_bytes + (sfp->count + 1) * XFS_INO64_DIFF;
1004 * Won't fit as shortform, convert to block then do replace.
1006 if (newsize > XFS_IFORK_DSIZE(dp)) {
1007 error = xfs_dir2_sf_to_block(args);
1008 if (error) {
1009 return error;
1011 return xfs_dir2_block_replace(args);
1014 * Still fits, convert to 8-byte now.
1016 xfs_dir2_sf_toino8(args);
1017 i8elevated = 1;
1018 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
1019 } else
1020 i8elevated = 0;
1022 ASSERT(args->namelen != 1 || args->name[0] != '.');
1024 * Replace ..'s entry.
1026 if (args->namelen == 2 &&
1027 args->name[0] == '.' && args->name[1] == '.') {
1028 ino = dp->d_ops->sf_get_parent_ino(sfp);
1029 ASSERT(args->inumber != ino);
1030 dp->d_ops->sf_put_parent_ino(sfp, args->inumber);
1033 * Normal entry, look for the name.
1035 else {
1036 for (i = 0, sfep = xfs_dir2_sf_firstentry(sfp); i < sfp->count;
1037 i++, sfep = dp->d_ops->sf_nextentry(sfp, sfep)) {
1038 if (xfs_da_compname(args, sfep->name, sfep->namelen) ==
1039 XFS_CMP_EXACT) {
1040 ino = dp->d_ops->sf_get_ino(sfp, sfep);
1041 ASSERT(args->inumber != ino);
1042 dp->d_ops->sf_put_ino(sfp, sfep, args->inumber);
1043 dp->d_ops->sf_put_ftype(sfep, args->filetype);
1044 break;
1048 * Didn't find it.
1050 if (i == sfp->count) {
1051 ASSERT(args->op_flags & XFS_DA_OP_OKNOENT);
1052 if (i8elevated)
1053 xfs_dir2_sf_toino4(args);
1054 return -ENOENT;
1058 * See if the old number was large, the new number is small.
1060 if (ino > XFS_DIR2_MAX_SHORT_INUM &&
1061 args->inumber <= XFS_DIR2_MAX_SHORT_INUM) {
1063 * And the old count was one, so need to convert to small.
1065 if (sfp->i8count == 1)
1066 xfs_dir2_sf_toino4(args);
1067 else
1068 sfp->i8count--;
1071 * See if the old number was small, the new number is large.
1073 if (ino <= XFS_DIR2_MAX_SHORT_INUM &&
1074 args->inumber > XFS_DIR2_MAX_SHORT_INUM) {
1076 * add to the i8count unless we just converted to 8-byte
1077 * inodes (which does an implied i8count = 1)
1079 ASSERT(sfp->i8count != 0);
1080 if (!i8elevated)
1081 sfp->i8count++;
1083 xfs_dir2_sf_check(args);
1084 xfs_trans_log_inode(args->trans, dp, XFS_ILOG_DDATA);
1085 return 0;
1089 * Convert from 8-byte inode numbers to 4-byte inode numbers.
1090 * The last 8-byte inode number is gone, but the count is still 1.
1092 static void
1093 xfs_dir2_sf_toino4(
1094 xfs_da_args_t *args) /* operation arguments */
1096 char *buf; /* old dir's buffer */
1097 xfs_inode_t *dp; /* incore directory inode */
1098 int i; /* entry index */
1099 int newsize; /* new inode size */
1100 xfs_dir2_sf_entry_t *oldsfep; /* old sf entry */
1101 xfs_dir2_sf_hdr_t *oldsfp; /* old sf directory */
1102 int oldsize; /* old inode size */
1103 xfs_dir2_sf_entry_t *sfep; /* new sf entry */
1104 xfs_dir2_sf_hdr_t *sfp; /* new sf directory */
1106 trace_xfs_dir2_sf_toino4(args);
1108 dp = args->dp;
1111 * Copy the old directory to the buffer.
1112 * Then nuke it from the inode, and add the new buffer to the inode.
1113 * Don't want xfs_idata_realloc copying the data here.
1115 oldsize = dp->i_df.if_bytes;
1116 buf = kmem_alloc(oldsize, KM_SLEEP);
1117 oldsfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
1118 ASSERT(oldsfp->i8count == 1);
1119 memcpy(buf, oldsfp, oldsize);
1121 * Compute the new inode size.
1123 newsize = oldsize - (oldsfp->count + 1) * XFS_INO64_DIFF;
1124 xfs_idata_realloc(dp, -oldsize, XFS_DATA_FORK);
1125 xfs_idata_realloc(dp, newsize, XFS_DATA_FORK);
1127 * Reset our pointers, the data has moved.
1129 oldsfp = (xfs_dir2_sf_hdr_t *)buf;
1130 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
1132 * Fill in the new header.
1134 sfp->count = oldsfp->count;
1135 sfp->i8count = 0;
1136 dp->d_ops->sf_put_parent_ino(sfp, dp->d_ops->sf_get_parent_ino(oldsfp));
1138 * Copy the entries field by field.
1140 for (i = 0, sfep = xfs_dir2_sf_firstentry(sfp),
1141 oldsfep = xfs_dir2_sf_firstentry(oldsfp);
1142 i < sfp->count;
1143 i++, sfep = dp->d_ops->sf_nextentry(sfp, sfep),
1144 oldsfep = dp->d_ops->sf_nextentry(oldsfp, oldsfep)) {
1145 sfep->namelen = oldsfep->namelen;
1146 memcpy(sfep->offset, oldsfep->offset, sizeof(sfep->offset));
1147 memcpy(sfep->name, oldsfep->name, sfep->namelen);
1148 dp->d_ops->sf_put_ino(sfp, sfep,
1149 dp->d_ops->sf_get_ino(oldsfp, oldsfep));
1150 dp->d_ops->sf_put_ftype(sfep, dp->d_ops->sf_get_ftype(oldsfep));
1153 * Clean up the inode.
1155 kmem_free(buf);
1156 dp->i_d.di_size = newsize;
1157 xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE | XFS_ILOG_DDATA);
1161 * Convert existing entries from 4-byte inode numbers to 8-byte inode numbers.
1162 * The new entry w/ an 8-byte inode number is not there yet; we leave with
1163 * i8count set to 1, but no corresponding 8-byte entry.
1165 static void
1166 xfs_dir2_sf_toino8(
1167 xfs_da_args_t *args) /* operation arguments */
1169 char *buf; /* old dir's buffer */
1170 xfs_inode_t *dp; /* incore directory inode */
1171 int i; /* entry index */
1172 int newsize; /* new inode size */
1173 xfs_dir2_sf_entry_t *oldsfep; /* old sf entry */
1174 xfs_dir2_sf_hdr_t *oldsfp; /* old sf directory */
1175 int oldsize; /* old inode size */
1176 xfs_dir2_sf_entry_t *sfep; /* new sf entry */
1177 xfs_dir2_sf_hdr_t *sfp; /* new sf directory */
1179 trace_xfs_dir2_sf_toino8(args);
1181 dp = args->dp;
1184 * Copy the old directory to the buffer.
1185 * Then nuke it from the inode, and add the new buffer to the inode.
1186 * Don't want xfs_idata_realloc copying the data here.
1188 oldsize = dp->i_df.if_bytes;
1189 buf = kmem_alloc(oldsize, KM_SLEEP);
1190 oldsfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
1191 ASSERT(oldsfp->i8count == 0);
1192 memcpy(buf, oldsfp, oldsize);
1194 * Compute the new inode size (nb: entry count + 1 for parent)
1196 newsize = oldsize + (oldsfp->count + 1) * XFS_INO64_DIFF;
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_hdr_t *)buf;
1203 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
1205 * Fill in the new header.
1207 sfp->count = oldsfp->count;
1208 sfp->i8count = 1;
1209 dp->d_ops->sf_put_parent_ino(sfp, dp->d_ops->sf_get_parent_ino(oldsfp));
1211 * Copy the entries field by field.
1213 for (i = 0, sfep = xfs_dir2_sf_firstentry(sfp),
1214 oldsfep = xfs_dir2_sf_firstentry(oldsfp);
1215 i < sfp->count;
1216 i++, sfep = dp->d_ops->sf_nextentry(sfp, sfep),
1217 oldsfep = dp->d_ops->sf_nextentry(oldsfp, oldsfep)) {
1218 sfep->namelen = oldsfep->namelen;
1219 memcpy(sfep->offset, oldsfep->offset, sizeof(sfep->offset));
1220 memcpy(sfep->name, oldsfep->name, sfep->namelen);
1221 dp->d_ops->sf_put_ino(sfp, sfep,
1222 dp->d_ops->sf_get_ino(oldsfp, oldsfep));
1223 dp->d_ops->sf_put_ftype(sfep, dp->d_ops->sf_get_ftype(oldsfep));
1226 * Clean up the inode.
1228 kmem_free(buf);
1229 dp->i_d.di_size = newsize;
1230 xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE | XFS_ILOG_DDATA);