[PATCH] powerpc: Fix sparsemem with memory holes [was Re: ppc64 oops..]
[linux-2.6.22.y-op.git] / fs / xfs / xfs_dir2_sf.c
blobec8e7476c8b75198a1b301a6eba182a9644f54f0
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_types.h"
21 #include "xfs_log.h"
22 #include "xfs_inum.h"
23 #include "xfs_trans.h"
24 #include "xfs_sb.h"
25 #include "xfs_dir.h"
26 #include "xfs_dir2.h"
27 #include "xfs_dmapi.h"
28 #include "xfs_mount.h"
29 #include "xfs_da_btree.h"
30 #include "xfs_bmap_btree.h"
31 #include "xfs_dir_sf.h"
32 #include "xfs_dir2_sf.h"
33 #include "xfs_attr_sf.h"
34 #include "xfs_dinode.h"
35 #include "xfs_inode.h"
36 #include "xfs_inode_item.h"
37 #include "xfs_dir_leaf.h"
38 #include "xfs_error.h"
39 #include "xfs_dir2_data.h"
40 #include "xfs_dir2_leaf.h"
41 #include "xfs_dir2_block.h"
42 #include "xfs_dir2_trace.h"
45 * Prototypes for internal functions.
47 static void xfs_dir2_sf_addname_easy(xfs_da_args_t *args,
48 xfs_dir2_sf_entry_t *sfep,
49 xfs_dir2_data_aoff_t offset,
50 int new_isize);
51 static void xfs_dir2_sf_addname_hard(xfs_da_args_t *args, int objchange,
52 int new_isize);
53 static int xfs_dir2_sf_addname_pick(xfs_da_args_t *args, int objchange,
54 xfs_dir2_sf_entry_t **sfepp,
55 xfs_dir2_data_aoff_t *offsetp);
56 #ifdef DEBUG
57 static void xfs_dir2_sf_check(xfs_da_args_t *args);
58 #else
59 #define xfs_dir2_sf_check(args)
60 #endif /* DEBUG */
61 #if XFS_BIG_INUMS
62 static void xfs_dir2_sf_toino4(xfs_da_args_t *args);
63 static void xfs_dir2_sf_toino8(xfs_da_args_t *args);
64 #endif /* XFS_BIG_INUMS */
67 * Given a block directory (dp/block), calculate its size as a shortform (sf)
68 * directory and a header for the sf directory, if it will fit it the
69 * space currently present in the inode. If it won't fit, the output
70 * size is too big (but not accurate).
72 int /* size for sf form */
73 xfs_dir2_block_sfsize(
74 xfs_inode_t *dp, /* incore inode pointer */
75 xfs_dir2_block_t *block, /* block directory data */
76 xfs_dir2_sf_hdr_t *sfhp) /* output: header for sf form */
78 xfs_dir2_dataptr_t addr; /* data entry address */
79 xfs_dir2_leaf_entry_t *blp; /* leaf area of the block */
80 xfs_dir2_block_tail_t *btp; /* tail area of the block */
81 int count; /* shortform entry count */
82 xfs_dir2_data_entry_t *dep; /* data entry in the block */
83 int i; /* block entry index */
84 int i8count; /* count of big-inode entries */
85 int isdot; /* entry is "." */
86 int isdotdot; /* entry is ".." */
87 xfs_mount_t *mp; /* mount structure pointer */
88 int namelen; /* total name bytes */
89 xfs_ino_t parent = 0; /* parent inode number */
90 int size=0; /* total computed size */
92 mp = dp->i_mount;
94 count = i8count = namelen = 0;
95 btp = XFS_DIR2_BLOCK_TAIL_P(mp, block);
96 blp = XFS_DIR2_BLOCK_LEAF_P(btp);
99 * Iterate over the block's data entries by using the leaf pointers.
101 for (i = 0; i < INT_GET(btp->count, ARCH_CONVERT); i++) {
102 if ((addr = INT_GET(blp[i].address, ARCH_CONVERT)) == XFS_DIR2_NULL_DATAPTR)
103 continue;
105 * Calculate the pointer to the entry at hand.
107 dep = (xfs_dir2_data_entry_t *)
108 ((char *)block + XFS_DIR2_DATAPTR_TO_OFF(mp, addr));
110 * Detect . and .., so we can special-case them.
111 * . is not included in sf directories.
112 * .. is included by just the parent inode number.
114 isdot = dep->namelen == 1 && dep->name[0] == '.';
115 isdotdot =
116 dep->namelen == 2 &&
117 dep->name[0] == '.' && dep->name[1] == '.';
118 #if XFS_BIG_INUMS
119 if (!isdot)
120 i8count += INT_GET(dep->inumber, ARCH_CONVERT) > XFS_DIR2_MAX_SHORT_INUM;
121 #endif
122 if (!isdot && !isdotdot) {
123 count++;
124 namelen += dep->namelen;
125 } else if (isdotdot)
126 parent = INT_GET(dep->inumber, ARCH_CONVERT);
128 * Calculate the new size, see if we should give up yet.
130 size = XFS_DIR2_SF_HDR_SIZE(i8count) + /* header */
131 count + /* namelen */
132 count * (uint)sizeof(xfs_dir2_sf_off_t) + /* offset */
133 namelen + /* name */
134 (i8count ? /* inumber */
135 (uint)sizeof(xfs_dir2_ino8_t) * count :
136 (uint)sizeof(xfs_dir2_ino4_t) * count);
137 if (size > XFS_IFORK_DSIZE(dp))
138 return size; /* size value is a failure */
141 * Create the output header, if it worked.
143 sfhp->count = count;
144 sfhp->i8count = i8count;
145 XFS_DIR2_SF_PUT_INUMBER((xfs_dir2_sf_t *)sfhp, &parent, &sfhp->parent);
146 return size;
150 * Convert a block format directory to shortform.
151 * Caller has already checked that it will fit, and built us a header.
153 int /* error */
154 xfs_dir2_block_to_sf(
155 xfs_da_args_t *args, /* operation arguments */
156 xfs_dabuf_t *bp, /* block buffer */
157 int size, /* shortform directory size */
158 xfs_dir2_sf_hdr_t *sfhp) /* shortform directory hdr */
160 xfs_dir2_block_t *block; /* block structure */
161 xfs_dir2_block_tail_t *btp; /* block tail pointer */
162 xfs_dir2_data_entry_t *dep; /* data entry pointer */
163 xfs_inode_t *dp; /* incore directory inode */
164 xfs_dir2_data_unused_t *dup; /* unused data pointer */
165 char *endptr; /* end of data entries */
166 int error; /* error return value */
167 int logflags; /* inode logging flags */
168 xfs_mount_t *mp; /* filesystem mount point */
169 char *ptr; /* current data pointer */
170 xfs_dir2_sf_entry_t *sfep; /* shortform entry */
171 xfs_dir2_sf_t *sfp; /* shortform structure */
172 xfs_ino_t temp;
174 xfs_dir2_trace_args_sb("block_to_sf", args, size, bp);
175 dp = args->dp;
176 mp = dp->i_mount;
179 * Make a copy of the block data, so we can shrink the inode
180 * and add local data.
182 block = kmem_alloc(mp->m_dirblksize, KM_SLEEP);
183 memcpy(block, bp->data, mp->m_dirblksize);
184 logflags = XFS_ILOG_CORE;
185 if ((error = xfs_dir2_shrink_inode(args, mp->m_dirdatablk, bp))) {
186 ASSERT(error != ENOSPC);
187 goto out;
190 * The buffer is now unconditionally gone, whether
191 * xfs_dir2_shrink_inode worked or not.
193 * Convert the inode to local format.
195 dp->i_df.if_flags &= ~XFS_IFEXTENTS;
196 dp->i_df.if_flags |= XFS_IFINLINE;
197 dp->i_d.di_format = XFS_DINODE_FMT_LOCAL;
198 ASSERT(dp->i_df.if_bytes == 0);
199 xfs_idata_realloc(dp, size, XFS_DATA_FORK);
200 logflags |= XFS_ILOG_DDATA;
202 * Copy the header into the newly allocate local space.
204 sfp = (xfs_dir2_sf_t *)dp->i_df.if_u1.if_data;
205 memcpy(sfp, sfhp, XFS_DIR2_SF_HDR_SIZE(sfhp->i8count));
206 dp->i_d.di_size = size;
208 * Set up to loop over the block's entries.
210 btp = XFS_DIR2_BLOCK_TAIL_P(mp, block);
211 ptr = (char *)block->u;
212 endptr = (char *)XFS_DIR2_BLOCK_LEAF_P(btp);
213 sfep = XFS_DIR2_SF_FIRSTENTRY(sfp);
215 * Loop over the active and unused entries.
216 * Stop when we reach the leaf/tail portion of the block.
218 while (ptr < endptr) {
220 * If it's unused, just skip over it.
222 dup = (xfs_dir2_data_unused_t *)ptr;
223 if (INT_GET(dup->freetag, ARCH_CONVERT) == XFS_DIR2_DATA_FREE_TAG) {
224 ptr += INT_GET(dup->length, ARCH_CONVERT);
225 continue;
227 dep = (xfs_dir2_data_entry_t *)ptr;
229 * Skip .
231 if (dep->namelen == 1 && dep->name[0] == '.')
232 ASSERT(INT_GET(dep->inumber, ARCH_CONVERT) == dp->i_ino);
234 * Skip .., but make sure the inode number is right.
236 else if (dep->namelen == 2 &&
237 dep->name[0] == '.' && dep->name[1] == '.')
238 ASSERT(INT_GET(dep->inumber, ARCH_CONVERT) ==
239 XFS_DIR2_SF_GET_INUMBER(sfp, &sfp->hdr.parent));
241 * Normal entry, copy it into shortform.
243 else {
244 sfep->namelen = dep->namelen;
245 XFS_DIR2_SF_PUT_OFFSET(sfep,
246 (xfs_dir2_data_aoff_t)
247 ((char *)dep - (char *)block));
248 memcpy(sfep->name, dep->name, dep->namelen);
249 temp=INT_GET(dep->inumber, ARCH_CONVERT);
250 XFS_DIR2_SF_PUT_INUMBER(sfp, &temp,
251 XFS_DIR2_SF_INUMBERP(sfep));
252 sfep = XFS_DIR2_SF_NEXTENTRY(sfp, sfep);
254 ptr += XFS_DIR2_DATA_ENTSIZE(dep->namelen);
256 ASSERT((char *)sfep - (char *)sfp == size);
257 xfs_dir2_sf_check(args);
258 out:
259 xfs_trans_log_inode(args->trans, dp, logflags);
260 kmem_free(block, mp->m_dirblksize);
261 return error;
265 * Add a name to a shortform directory.
266 * There are two algorithms, "easy" and "hard" which we decide on
267 * before changing anything.
268 * Convert to block form if necessary, if the new entry won't fit.
270 int /* error */
271 xfs_dir2_sf_addname(
272 xfs_da_args_t *args) /* operation arguments */
274 int add_entsize; /* size of the new entry */
275 xfs_inode_t *dp; /* incore directory inode */
276 int error; /* error return value */
277 int incr_isize; /* total change in size */
278 int new_isize; /* di_size after adding name */
279 int objchange; /* changing to 8-byte inodes */
280 xfs_dir2_data_aoff_t offset = 0; /* offset for new entry */
281 int old_isize; /* di_size before adding name */
282 int pick; /* which algorithm to use */
283 xfs_dir2_sf_t *sfp; /* shortform structure */
284 xfs_dir2_sf_entry_t *sfep = NULL; /* shortform entry */
286 xfs_dir2_trace_args("sf_addname", args);
287 ASSERT(xfs_dir2_sf_lookup(args) == ENOENT);
288 dp = args->dp;
289 ASSERT(dp->i_df.if_flags & XFS_IFINLINE);
291 * Make sure the shortform value has some of its header.
293 if (dp->i_d.di_size < offsetof(xfs_dir2_sf_hdr_t, parent)) {
294 ASSERT(XFS_FORCED_SHUTDOWN(dp->i_mount));
295 return XFS_ERROR(EIO);
297 ASSERT(dp->i_df.if_bytes == dp->i_d.di_size);
298 ASSERT(dp->i_df.if_u1.if_data != NULL);
299 sfp = (xfs_dir2_sf_t *)dp->i_df.if_u1.if_data;
300 ASSERT(dp->i_d.di_size >= XFS_DIR2_SF_HDR_SIZE(sfp->hdr.i8count));
302 * Compute entry (and change in) size.
304 add_entsize = XFS_DIR2_SF_ENTSIZE_BYNAME(sfp, args->namelen);
305 incr_isize = add_entsize;
306 objchange = 0;
307 #if XFS_BIG_INUMS
309 * Do we have to change to 8 byte inodes?
311 if (args->inumber > XFS_DIR2_MAX_SHORT_INUM && sfp->hdr.i8count == 0) {
313 * Yes, adjust the entry size and the total size.
315 add_entsize +=
316 (uint)sizeof(xfs_dir2_ino8_t) -
317 (uint)sizeof(xfs_dir2_ino4_t);
318 incr_isize +=
319 (sfp->hdr.count + 2) *
320 ((uint)sizeof(xfs_dir2_ino8_t) -
321 (uint)sizeof(xfs_dir2_ino4_t));
322 objchange = 1;
324 #endif
325 old_isize = (int)dp->i_d.di_size;
326 new_isize = old_isize + incr_isize;
328 * Won't fit as shortform any more (due to size),
329 * or the pick routine says it won't (due to offset values).
331 if (new_isize > XFS_IFORK_DSIZE(dp) ||
332 (pick =
333 xfs_dir2_sf_addname_pick(args, objchange, &sfep, &offset)) == 0) {
335 * Just checking or no space reservation, it doesn't fit.
337 if (args->justcheck || args->total == 0)
338 return XFS_ERROR(ENOSPC);
340 * Convert to block form then add the name.
342 error = xfs_dir2_sf_to_block(args);
343 if (error)
344 return error;
345 return xfs_dir2_block_addname(args);
348 * Just checking, it fits.
350 if (args->justcheck)
351 return 0;
353 * Do it the easy way - just add it at the end.
355 if (pick == 1)
356 xfs_dir2_sf_addname_easy(args, sfep, offset, new_isize);
358 * Do it the hard way - look for a place to insert the new entry.
359 * Convert to 8 byte inode numbers first if necessary.
361 else {
362 ASSERT(pick == 2);
363 #if XFS_BIG_INUMS
364 if (objchange)
365 xfs_dir2_sf_toino8(args);
366 #endif
367 xfs_dir2_sf_addname_hard(args, objchange, new_isize);
369 xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE | XFS_ILOG_DDATA);
370 return 0;
374 * Add the new entry the "easy" way.
375 * This is copying the old directory and adding the new entry at the end.
376 * Since it's sorted by "offset" we need room after the last offset
377 * that's already there, and then room to convert to a block directory.
378 * This is already checked by the pick routine.
380 static void
381 xfs_dir2_sf_addname_easy(
382 xfs_da_args_t *args, /* operation arguments */
383 xfs_dir2_sf_entry_t *sfep, /* pointer to new entry */
384 xfs_dir2_data_aoff_t offset, /* offset to use for new ent */
385 int new_isize) /* new directory size */
387 int byteoff; /* byte offset in sf dir */
388 xfs_inode_t *dp; /* incore directory inode */
389 xfs_dir2_sf_t *sfp; /* shortform structure */
391 dp = args->dp;
393 sfp = (xfs_dir2_sf_t *)dp->i_df.if_u1.if_data;
394 byteoff = (int)((char *)sfep - (char *)sfp);
396 * Grow the in-inode space.
398 xfs_idata_realloc(dp, XFS_DIR2_SF_ENTSIZE_BYNAME(sfp, args->namelen),
399 XFS_DATA_FORK);
401 * Need to set up again due to realloc of the inode data.
403 sfp = (xfs_dir2_sf_t *)dp->i_df.if_u1.if_data;
404 sfep = (xfs_dir2_sf_entry_t *)((char *)sfp + byteoff);
406 * Fill in the new entry.
408 sfep->namelen = args->namelen;
409 XFS_DIR2_SF_PUT_OFFSET(sfep, offset);
410 memcpy(sfep->name, args->name, sfep->namelen);
411 XFS_DIR2_SF_PUT_INUMBER(sfp, &args->inumber,
412 XFS_DIR2_SF_INUMBERP(sfep));
414 * Update the header and inode.
416 sfp->hdr.count++;
417 #if XFS_BIG_INUMS
418 if (args->inumber > XFS_DIR2_MAX_SHORT_INUM)
419 sfp->hdr.i8count++;
420 #endif
421 dp->i_d.di_size = new_isize;
422 xfs_dir2_sf_check(args);
426 * Add the new entry the "hard" way.
427 * The caller has already converted to 8 byte inode numbers if necessary,
428 * in which case we need to leave the i8count at 1.
429 * Find a hole that the new entry will fit into, and copy
430 * the first part of the entries, the new entry, and the last part of
431 * the entries.
433 /* ARGSUSED */
434 static void
435 xfs_dir2_sf_addname_hard(
436 xfs_da_args_t *args, /* operation arguments */
437 int objchange, /* changing inode number size */
438 int new_isize) /* new directory size */
440 int add_datasize; /* data size need for new ent */
441 char *buf; /* buffer for old */
442 xfs_inode_t *dp; /* incore directory inode */
443 int eof; /* reached end of old dir */
444 int nbytes; /* temp for byte copies */
445 xfs_dir2_data_aoff_t new_offset; /* next offset value */
446 xfs_dir2_data_aoff_t offset; /* current offset value */
447 int old_isize; /* previous di_size */
448 xfs_dir2_sf_entry_t *oldsfep; /* entry in original dir */
449 xfs_dir2_sf_t *oldsfp; /* original shortform dir */
450 xfs_dir2_sf_entry_t *sfep; /* entry in new dir */
451 xfs_dir2_sf_t *sfp; /* new shortform dir */
454 * Copy the old directory to the stack buffer.
456 dp = args->dp;
458 sfp = (xfs_dir2_sf_t *)dp->i_df.if_u1.if_data;
459 old_isize = (int)dp->i_d.di_size;
460 buf = kmem_alloc(old_isize, KM_SLEEP);
461 oldsfp = (xfs_dir2_sf_t *)buf;
462 memcpy(oldsfp, sfp, old_isize);
464 * Loop over the old directory finding the place we're going
465 * to insert the new entry.
466 * If it's going to end up at the end then oldsfep will point there.
468 for (offset = XFS_DIR2_DATA_FIRST_OFFSET,
469 oldsfep = XFS_DIR2_SF_FIRSTENTRY(oldsfp),
470 add_datasize = XFS_DIR2_DATA_ENTSIZE(args->namelen),
471 eof = (char *)oldsfep == &buf[old_isize];
472 !eof;
473 offset = new_offset + XFS_DIR2_DATA_ENTSIZE(oldsfep->namelen),
474 oldsfep = XFS_DIR2_SF_NEXTENTRY(oldsfp, oldsfep),
475 eof = (char *)oldsfep == &buf[old_isize]) {
476 new_offset = XFS_DIR2_SF_GET_OFFSET(oldsfep);
477 if (offset + add_datasize <= new_offset)
478 break;
481 * Get rid of the old directory, then allocate space for
482 * the new one. We do this so xfs_idata_realloc won't copy
483 * the data.
485 xfs_idata_realloc(dp, -old_isize, XFS_DATA_FORK);
486 xfs_idata_realloc(dp, new_isize, XFS_DATA_FORK);
488 * Reset the pointer since the buffer was reallocated.
490 sfp = (xfs_dir2_sf_t *)dp->i_df.if_u1.if_data;
492 * Copy the first part of the directory, including the header.
494 nbytes = (int)((char *)oldsfep - (char *)oldsfp);
495 memcpy(sfp, oldsfp, nbytes);
496 sfep = (xfs_dir2_sf_entry_t *)((char *)sfp + nbytes);
498 * Fill in the new entry, and update the header counts.
500 sfep->namelen = args->namelen;
501 XFS_DIR2_SF_PUT_OFFSET(sfep, offset);
502 memcpy(sfep->name, args->name, sfep->namelen);
503 XFS_DIR2_SF_PUT_INUMBER(sfp, &args->inumber,
504 XFS_DIR2_SF_INUMBERP(sfep));
505 sfp->hdr.count++;
506 #if XFS_BIG_INUMS
507 if (args->inumber > XFS_DIR2_MAX_SHORT_INUM && !objchange)
508 sfp->hdr.i8count++;
509 #endif
511 * If there's more left to copy, do that.
513 if (!eof) {
514 sfep = XFS_DIR2_SF_NEXTENTRY(sfp, sfep);
515 memcpy(sfep, oldsfep, old_isize - nbytes);
517 kmem_free(buf, old_isize);
518 dp->i_d.di_size = new_isize;
519 xfs_dir2_sf_check(args);
523 * Decide if the new entry will fit at all.
524 * If it will fit, pick between adding the new entry to the end (easy)
525 * or somewhere else (hard).
526 * Return 0 (won't fit), 1 (easy), 2 (hard).
528 /*ARGSUSED*/
529 static int /* pick result */
530 xfs_dir2_sf_addname_pick(
531 xfs_da_args_t *args, /* operation arguments */
532 int objchange, /* inode # size changes */
533 xfs_dir2_sf_entry_t **sfepp, /* out(1): new entry ptr */
534 xfs_dir2_data_aoff_t *offsetp) /* out(1): new offset */
536 xfs_inode_t *dp; /* incore directory inode */
537 int holefit; /* found hole it will fit in */
538 int i; /* entry number */
539 xfs_mount_t *mp; /* filesystem mount point */
540 xfs_dir2_data_aoff_t offset; /* data block offset */
541 xfs_dir2_sf_entry_t *sfep; /* shortform entry */
542 xfs_dir2_sf_t *sfp; /* shortform structure */
543 int size; /* entry's data size */
544 int used; /* data bytes used */
546 dp = args->dp;
547 mp = dp->i_mount;
549 sfp = (xfs_dir2_sf_t *)dp->i_df.if_u1.if_data;
550 size = XFS_DIR2_DATA_ENTSIZE(args->namelen);
551 offset = XFS_DIR2_DATA_FIRST_OFFSET;
552 sfep = XFS_DIR2_SF_FIRSTENTRY(sfp);
553 holefit = 0;
555 * Loop over sf entries.
556 * Keep track of data offset and whether we've seen a place
557 * to insert the new entry.
559 for (i = 0; i < sfp->hdr.count; i++) {
560 if (!holefit)
561 holefit = offset + size <= XFS_DIR2_SF_GET_OFFSET(sfep);
562 offset = XFS_DIR2_SF_GET_OFFSET(sfep) +
563 XFS_DIR2_DATA_ENTSIZE(sfep->namelen);
564 sfep = XFS_DIR2_SF_NEXTENTRY(sfp, sfep);
567 * Calculate data bytes used excluding the new entry, if this
568 * was a data block (block form directory).
570 used = offset +
571 (sfp->hdr.count + 3) * (uint)sizeof(xfs_dir2_leaf_entry_t) +
572 (uint)sizeof(xfs_dir2_block_tail_t);
574 * If it won't fit in a block form then we can't insert it,
575 * we'll go back, convert to block, then try the insert and convert
576 * to leaf.
578 if (used + (holefit ? 0 : size) > mp->m_dirblksize)
579 return 0;
581 * If changing the inode number size, do it the hard way.
583 #if XFS_BIG_INUMS
584 if (objchange) {
585 return 2;
587 #else
588 ASSERT(objchange == 0);
589 #endif
591 * If it won't fit at the end then do it the hard way (use the hole).
593 if (used + size > mp->m_dirblksize)
594 return 2;
596 * Do it the easy way.
598 *sfepp = sfep;
599 *offsetp = offset;
600 return 1;
603 #ifdef DEBUG
605 * Check consistency of shortform directory, assert if bad.
607 static void
608 xfs_dir2_sf_check(
609 xfs_da_args_t *args) /* operation arguments */
611 xfs_inode_t *dp; /* incore directory inode */
612 int i; /* entry number */
613 int i8count; /* number of big inode#s */
614 xfs_ino_t ino; /* entry inode number */
615 int offset; /* data offset */
616 xfs_dir2_sf_entry_t *sfep; /* shortform dir entry */
617 xfs_dir2_sf_t *sfp; /* shortform structure */
619 dp = args->dp;
621 sfp = (xfs_dir2_sf_t *)dp->i_df.if_u1.if_data;
622 offset = XFS_DIR2_DATA_FIRST_OFFSET;
623 ino = XFS_DIR2_SF_GET_INUMBER(sfp, &sfp->hdr.parent);
624 i8count = ino > XFS_DIR2_MAX_SHORT_INUM;
626 for (i = 0, sfep = XFS_DIR2_SF_FIRSTENTRY(sfp);
627 i < sfp->hdr.count;
628 i++, sfep = XFS_DIR2_SF_NEXTENTRY(sfp, sfep)) {
629 ASSERT(XFS_DIR2_SF_GET_OFFSET(sfep) >= offset);
630 ino = XFS_DIR2_SF_GET_INUMBER(sfp, XFS_DIR2_SF_INUMBERP(sfep));
631 i8count += ino > XFS_DIR2_MAX_SHORT_INUM;
632 offset =
633 XFS_DIR2_SF_GET_OFFSET(sfep) +
634 XFS_DIR2_DATA_ENTSIZE(sfep->namelen);
636 ASSERT(i8count == sfp->hdr.i8count);
637 ASSERT(XFS_BIG_INUMS || i8count == 0);
638 ASSERT((char *)sfep - (char *)sfp == dp->i_d.di_size);
639 ASSERT(offset +
640 (sfp->hdr.count + 2) * (uint)sizeof(xfs_dir2_leaf_entry_t) +
641 (uint)sizeof(xfs_dir2_block_tail_t) <=
642 dp->i_mount->m_dirblksize);
644 #endif /* DEBUG */
647 * Create a new (shortform) directory.
649 int /* error, always 0 */
650 xfs_dir2_sf_create(
651 xfs_da_args_t *args, /* operation arguments */
652 xfs_ino_t pino) /* parent inode number */
654 xfs_inode_t *dp; /* incore directory inode */
655 int i8count; /* parent inode is an 8-byte number */
656 xfs_dir2_sf_t *sfp; /* shortform structure */
657 int size; /* directory size */
659 xfs_dir2_trace_args_i("sf_create", args, pino);
660 dp = args->dp;
662 ASSERT(dp != NULL);
663 ASSERT(dp->i_d.di_size == 0);
665 * If it's currently a zero-length extent file,
666 * convert it to local format.
668 if (dp->i_d.di_format == XFS_DINODE_FMT_EXTENTS) {
669 dp->i_df.if_flags &= ~XFS_IFEXTENTS; /* just in case */
670 dp->i_d.di_format = XFS_DINODE_FMT_LOCAL;
671 xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE);
672 dp->i_df.if_flags |= XFS_IFINLINE;
674 ASSERT(dp->i_df.if_flags & XFS_IFINLINE);
675 ASSERT(dp->i_df.if_bytes == 0);
676 i8count = pino > XFS_DIR2_MAX_SHORT_INUM;
677 size = XFS_DIR2_SF_HDR_SIZE(i8count);
679 * Make a buffer for the data.
681 xfs_idata_realloc(dp, size, XFS_DATA_FORK);
683 * Fill in the header,
685 sfp = (xfs_dir2_sf_t *)dp->i_df.if_u1.if_data;
686 sfp->hdr.i8count = i8count;
688 * Now can put in the inode number, since i8count is set.
690 XFS_DIR2_SF_PUT_INUMBER(sfp, &pino, &sfp->hdr.parent);
691 sfp->hdr.count = 0;
692 dp->i_d.di_size = size;
693 xfs_dir2_sf_check(args);
694 xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE | XFS_ILOG_DDATA);
695 return 0;
698 int /* error */
699 xfs_dir2_sf_getdents(
700 xfs_inode_t *dp, /* incore directory inode */
701 uio_t *uio, /* caller's buffer control */
702 int *eofp, /* eof reached? (out) */
703 xfs_dirent_t *dbp, /* caller's buffer */
704 xfs_dir2_put_t put) /* abi's formatting function */
706 int error; /* error return value */
707 int i; /* shortform entry number */
708 xfs_mount_t *mp; /* filesystem mount point */
709 xfs_dir2_dataptr_t off; /* current entry's offset */
710 xfs_dir2_put_args_t p; /* arg package for put rtn */
711 xfs_dir2_sf_entry_t *sfep; /* shortform directory entry */
712 xfs_dir2_sf_t *sfp; /* shortform structure */
713 xfs_off_t dir_offset;
715 mp = dp->i_mount;
717 ASSERT(dp->i_df.if_flags & XFS_IFINLINE);
719 * Give up if the directory is way too short.
721 if (dp->i_d.di_size < offsetof(xfs_dir2_sf_hdr_t, parent)) {
722 ASSERT(XFS_FORCED_SHUTDOWN(mp));
723 return XFS_ERROR(EIO);
726 dir_offset = uio->uio_offset;
728 ASSERT(dp->i_df.if_bytes == dp->i_d.di_size);
729 ASSERT(dp->i_df.if_u1.if_data != NULL);
731 sfp = (xfs_dir2_sf_t *)dp->i_df.if_u1.if_data;
733 ASSERT(dp->i_d.di_size >= XFS_DIR2_SF_HDR_SIZE(sfp->hdr.i8count));
736 * If the block number in the offset is out of range, we're done.
738 if (XFS_DIR2_DATAPTR_TO_DB(mp, dir_offset) > mp->m_dirdatablk) {
739 *eofp = 1;
740 return 0;
744 * Set up putargs structure.
746 p.dbp = dbp;
747 p.put = put;
748 p.uio = uio;
750 * Put . entry unless we're starting past it.
752 if (dir_offset <=
753 XFS_DIR2_DB_OFF_TO_DATAPTR(mp, mp->m_dirdatablk,
754 XFS_DIR2_DATA_DOT_OFFSET)) {
755 p.cook = XFS_DIR2_DB_OFF_TO_DATAPTR(mp, 0,
756 XFS_DIR2_DATA_DOTDOT_OFFSET);
757 p.ino = dp->i_ino;
758 #if XFS_BIG_INUMS
759 p.ino += mp->m_inoadd;
760 #endif
761 p.name = ".";
762 p.namelen = 1;
764 error = p.put(&p);
766 if (!p.done) {
767 uio->uio_offset =
768 XFS_DIR2_DB_OFF_TO_DATAPTR(mp, mp->m_dirdatablk,
769 XFS_DIR2_DATA_DOT_OFFSET);
770 return error;
775 * Put .. entry unless we're starting past it.
777 if (dir_offset <=
778 XFS_DIR2_DB_OFF_TO_DATAPTR(mp, mp->m_dirdatablk,
779 XFS_DIR2_DATA_DOTDOT_OFFSET)) {
780 p.cook = XFS_DIR2_DB_OFF_TO_DATAPTR(mp, mp->m_dirdatablk,
781 XFS_DIR2_DATA_FIRST_OFFSET);
782 p.ino = XFS_DIR2_SF_GET_INUMBER(sfp, &sfp->hdr.parent);
783 #if XFS_BIG_INUMS
784 p.ino += mp->m_inoadd;
785 #endif
786 p.name = "..";
787 p.namelen = 2;
789 error = p.put(&p);
791 if (!p.done) {
792 uio->uio_offset =
793 XFS_DIR2_DB_OFF_TO_DATAPTR(mp, mp->m_dirdatablk,
794 XFS_DIR2_DATA_DOTDOT_OFFSET);
795 return error;
800 * Loop while there are more entries and put'ing works.
802 for (i = 0, sfep = XFS_DIR2_SF_FIRSTENTRY(sfp);
803 i < sfp->hdr.count;
804 i++, sfep = XFS_DIR2_SF_NEXTENTRY(sfp, sfep)) {
806 off = XFS_DIR2_DB_OFF_TO_DATAPTR(mp, mp->m_dirdatablk,
807 XFS_DIR2_SF_GET_OFFSET(sfep));
809 if (dir_offset > off)
810 continue;
812 p.namelen = sfep->namelen;
814 p.cook = XFS_DIR2_DB_OFF_TO_DATAPTR(mp, mp->m_dirdatablk,
815 XFS_DIR2_SF_GET_OFFSET(sfep) +
816 XFS_DIR2_DATA_ENTSIZE(p.namelen));
818 p.ino = XFS_DIR2_SF_GET_INUMBER(sfp, XFS_DIR2_SF_INUMBERP(sfep));
819 #if XFS_BIG_INUMS
820 p.ino += mp->m_inoadd;
821 #endif
822 p.name = (char *)sfep->name;
824 error = p.put(&p);
826 if (!p.done) {
827 uio->uio_offset = off;
828 return error;
833 * They all fit.
835 *eofp = 1;
837 uio->uio_offset =
838 XFS_DIR2_DB_OFF_TO_DATAPTR(mp, mp->m_dirdatablk + 1, 0);
840 return 0;
844 * Lookup an entry in a shortform directory.
845 * Returns EEXIST if found, ENOENT if not found.
847 int /* error */
848 xfs_dir2_sf_lookup(
849 xfs_da_args_t *args) /* operation arguments */
851 xfs_inode_t *dp; /* incore directory inode */
852 int i; /* entry index */
853 xfs_dir2_sf_entry_t *sfep; /* shortform directory entry */
854 xfs_dir2_sf_t *sfp; /* shortform structure */
856 xfs_dir2_trace_args("sf_lookup", args);
857 xfs_dir2_sf_check(args);
858 dp = args->dp;
860 ASSERT(dp->i_df.if_flags & XFS_IFINLINE);
862 * Bail out if the directory is way too short.
864 if (dp->i_d.di_size < offsetof(xfs_dir2_sf_hdr_t, parent)) {
865 ASSERT(XFS_FORCED_SHUTDOWN(dp->i_mount));
866 return XFS_ERROR(EIO);
868 ASSERT(dp->i_df.if_bytes == dp->i_d.di_size);
869 ASSERT(dp->i_df.if_u1.if_data != NULL);
870 sfp = (xfs_dir2_sf_t *)dp->i_df.if_u1.if_data;
871 ASSERT(dp->i_d.di_size >= XFS_DIR2_SF_HDR_SIZE(sfp->hdr.i8count));
873 * Special case for .
875 if (args->namelen == 1 && args->name[0] == '.') {
876 args->inumber = dp->i_ino;
877 return XFS_ERROR(EEXIST);
880 * Special case for ..
882 if (args->namelen == 2 &&
883 args->name[0] == '.' && args->name[1] == '.') {
884 args->inumber = XFS_DIR2_SF_GET_INUMBER(sfp, &sfp->hdr.parent);
885 return XFS_ERROR(EEXIST);
888 * Loop over all the entries trying to match ours.
890 for (i = 0, sfep = XFS_DIR2_SF_FIRSTENTRY(sfp);
891 i < sfp->hdr.count;
892 i++, sfep = XFS_DIR2_SF_NEXTENTRY(sfp, sfep)) {
893 if (sfep->namelen == args->namelen &&
894 sfep->name[0] == args->name[0] &&
895 memcmp(args->name, sfep->name, args->namelen) == 0) {
896 args->inumber =
897 XFS_DIR2_SF_GET_INUMBER(sfp,
898 XFS_DIR2_SF_INUMBERP(sfep));
899 return XFS_ERROR(EEXIST);
903 * Didn't find it.
905 ASSERT(args->oknoent);
906 return XFS_ERROR(ENOENT);
910 * Remove an entry from a shortform directory.
912 int /* error */
913 xfs_dir2_sf_removename(
914 xfs_da_args_t *args)
916 int byteoff; /* offset of removed entry */
917 xfs_inode_t *dp; /* incore directory inode */
918 int entsize; /* this entry's size */
919 int i; /* shortform entry index */
920 int newsize; /* new inode size */
921 int oldsize; /* old inode size */
922 xfs_dir2_sf_entry_t *sfep; /* shortform directory entry */
923 xfs_dir2_sf_t *sfp; /* shortform structure */
925 xfs_dir2_trace_args("sf_removename", args);
926 dp = args->dp;
928 ASSERT(dp->i_df.if_flags & XFS_IFINLINE);
929 oldsize = (int)dp->i_d.di_size;
931 * Bail out if the directory is way too short.
933 if (oldsize < offsetof(xfs_dir2_sf_hdr_t, parent)) {
934 ASSERT(XFS_FORCED_SHUTDOWN(dp->i_mount));
935 return XFS_ERROR(EIO);
937 ASSERT(dp->i_df.if_bytes == oldsize);
938 ASSERT(dp->i_df.if_u1.if_data != NULL);
939 sfp = (xfs_dir2_sf_t *)dp->i_df.if_u1.if_data;
940 ASSERT(oldsize >= XFS_DIR2_SF_HDR_SIZE(sfp->hdr.i8count));
942 * Loop over the old directory entries.
943 * Find the one we're deleting.
945 for (i = 0, sfep = XFS_DIR2_SF_FIRSTENTRY(sfp);
946 i < sfp->hdr.count;
947 i++, sfep = XFS_DIR2_SF_NEXTENTRY(sfp, sfep)) {
948 if (sfep->namelen == args->namelen &&
949 sfep->name[0] == args->name[0] &&
950 memcmp(sfep->name, args->name, args->namelen) == 0) {
951 ASSERT(XFS_DIR2_SF_GET_INUMBER(sfp,
952 XFS_DIR2_SF_INUMBERP(sfep)) ==
953 args->inumber);
954 break;
958 * Didn't find it.
960 if (i == sfp->hdr.count) {
961 return XFS_ERROR(ENOENT);
964 * Calculate sizes.
966 byteoff = (int)((char *)sfep - (char *)sfp);
967 entsize = XFS_DIR2_SF_ENTSIZE_BYNAME(sfp, args->namelen);
968 newsize = oldsize - entsize;
970 * Copy the part if any after the removed entry, sliding it down.
972 if (byteoff + entsize < oldsize)
973 memmove((char *)sfp + byteoff, (char *)sfp + byteoff + entsize,
974 oldsize - (byteoff + entsize));
976 * Fix up the header and file size.
978 sfp->hdr.count--;
979 dp->i_d.di_size = newsize;
981 * Reallocate, making it smaller.
983 xfs_idata_realloc(dp, newsize - oldsize, XFS_DATA_FORK);
984 sfp = (xfs_dir2_sf_t *)dp->i_df.if_u1.if_data;
985 #if XFS_BIG_INUMS
987 * Are we changing inode number size?
989 if (args->inumber > XFS_DIR2_MAX_SHORT_INUM) {
990 if (sfp->hdr.i8count == 1)
991 xfs_dir2_sf_toino4(args);
992 else
993 sfp->hdr.i8count--;
995 #endif
996 xfs_dir2_sf_check(args);
997 xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE | XFS_ILOG_DDATA);
998 return 0;
1002 * Replace the inode number of an entry in a shortform directory.
1004 int /* error */
1005 xfs_dir2_sf_replace(
1006 xfs_da_args_t *args) /* operation arguments */
1008 xfs_inode_t *dp; /* incore directory inode */
1009 int i; /* entry index */
1010 #if XFS_BIG_INUMS || defined(DEBUG)
1011 xfs_ino_t ino=0; /* entry old inode number */
1012 #endif
1013 #if XFS_BIG_INUMS
1014 int i8elevated; /* sf_toino8 set i8count=1 */
1015 #endif
1016 xfs_dir2_sf_entry_t *sfep; /* shortform directory entry */
1017 xfs_dir2_sf_t *sfp; /* shortform structure */
1019 xfs_dir2_trace_args("sf_replace", args);
1020 dp = args->dp;
1022 ASSERT(dp->i_df.if_flags & XFS_IFINLINE);
1024 * Bail out if the shortform directory is way too small.
1026 if (dp->i_d.di_size < offsetof(xfs_dir2_sf_hdr_t, parent)) {
1027 ASSERT(XFS_FORCED_SHUTDOWN(dp->i_mount));
1028 return XFS_ERROR(EIO);
1030 ASSERT(dp->i_df.if_bytes == dp->i_d.di_size);
1031 ASSERT(dp->i_df.if_u1.if_data != NULL);
1032 sfp = (xfs_dir2_sf_t *)dp->i_df.if_u1.if_data;
1033 ASSERT(dp->i_d.di_size >= XFS_DIR2_SF_HDR_SIZE(sfp->hdr.i8count));
1034 #if XFS_BIG_INUMS
1036 * New inode number is large, and need to convert to 8-byte inodes.
1038 if (args->inumber > XFS_DIR2_MAX_SHORT_INUM && sfp->hdr.i8count == 0) {
1039 int error; /* error return value */
1040 int newsize; /* new inode size */
1042 newsize =
1043 dp->i_df.if_bytes +
1044 (sfp->hdr.count + 1) *
1045 ((uint)sizeof(xfs_dir2_ino8_t) -
1046 (uint)sizeof(xfs_dir2_ino4_t));
1048 * Won't fit as shortform, convert to block then do replace.
1050 if (newsize > XFS_IFORK_DSIZE(dp)) {
1051 error = xfs_dir2_sf_to_block(args);
1052 if (error) {
1053 return error;
1055 return xfs_dir2_block_replace(args);
1058 * Still fits, convert to 8-byte now.
1060 xfs_dir2_sf_toino8(args);
1061 i8elevated = 1;
1062 sfp = (xfs_dir2_sf_t *)dp->i_df.if_u1.if_data;
1063 } else
1064 i8elevated = 0;
1065 #endif
1066 ASSERT(args->namelen != 1 || args->name[0] != '.');
1068 * Replace ..'s entry.
1070 if (args->namelen == 2 &&
1071 args->name[0] == '.' && args->name[1] == '.') {
1072 #if XFS_BIG_INUMS || defined(DEBUG)
1073 ino = XFS_DIR2_SF_GET_INUMBER(sfp, &sfp->hdr.parent);
1074 ASSERT(args->inumber != ino);
1075 #endif
1076 XFS_DIR2_SF_PUT_INUMBER(sfp, &args->inumber, &sfp->hdr.parent);
1079 * Normal entry, look for the name.
1081 else {
1082 for (i = 0, sfep = XFS_DIR2_SF_FIRSTENTRY(sfp);
1083 i < sfp->hdr.count;
1084 i++, sfep = XFS_DIR2_SF_NEXTENTRY(sfp, sfep)) {
1085 if (sfep->namelen == args->namelen &&
1086 sfep->name[0] == args->name[0] &&
1087 memcmp(args->name, sfep->name, args->namelen) == 0) {
1088 #if XFS_BIG_INUMS || defined(DEBUG)
1089 ino = XFS_DIR2_SF_GET_INUMBER(sfp,
1090 XFS_DIR2_SF_INUMBERP(sfep));
1091 ASSERT(args->inumber != ino);
1092 #endif
1093 XFS_DIR2_SF_PUT_INUMBER(sfp, &args->inumber,
1094 XFS_DIR2_SF_INUMBERP(sfep));
1095 break;
1099 * Didn't find it.
1101 if (i == sfp->hdr.count) {
1102 ASSERT(args->oknoent);
1103 #if XFS_BIG_INUMS
1104 if (i8elevated)
1105 xfs_dir2_sf_toino4(args);
1106 #endif
1107 return XFS_ERROR(ENOENT);
1110 #if XFS_BIG_INUMS
1112 * See if the old number was large, the new number is small.
1114 if (ino > XFS_DIR2_MAX_SHORT_INUM &&
1115 args->inumber <= XFS_DIR2_MAX_SHORT_INUM) {
1117 * And the old count was one, so need to convert to small.
1119 if (sfp->hdr.i8count == 1)
1120 xfs_dir2_sf_toino4(args);
1121 else
1122 sfp->hdr.i8count--;
1125 * See if the old number was small, the new number is large.
1127 if (ino <= XFS_DIR2_MAX_SHORT_INUM &&
1128 args->inumber > XFS_DIR2_MAX_SHORT_INUM) {
1130 * add to the i8count unless we just converted to 8-byte
1131 * inodes (which does an implied i8count = 1)
1133 ASSERT(sfp->hdr.i8count != 0);
1134 if (!i8elevated)
1135 sfp->hdr.i8count++;
1137 #endif
1138 xfs_dir2_sf_check(args);
1139 xfs_trans_log_inode(args->trans, dp, XFS_ILOG_DDATA);
1140 return 0;
1143 #if XFS_BIG_INUMS
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.
1148 static void
1149 xfs_dir2_sf_toino4(
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 xfs_ino_t ino; /* entry inode number */
1156 int newsize; /* new inode size */
1157 xfs_dir2_sf_entry_t *oldsfep; /* old sf entry */
1158 xfs_dir2_sf_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_t *sfp; /* new sf directory */
1163 xfs_dir2_trace_args("sf_toino4", args);
1164 dp = args->dp;
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, KM_SLEEP);
1173 oldsfp = (xfs_dir2_sf_t *)dp->i_df.if_u1.if_data;
1174 ASSERT(oldsfp->hdr.i8count == 1);
1175 memcpy(buf, oldsfp, oldsize);
1177 * Compute the new inode size.
1179 newsize =
1180 oldsize -
1181 (oldsfp->hdr.count + 1) *
1182 ((uint)sizeof(xfs_dir2_ino8_t) - (uint)sizeof(xfs_dir2_ino4_t));
1183 xfs_idata_realloc(dp, -oldsize, XFS_DATA_FORK);
1184 xfs_idata_realloc(dp, newsize, XFS_DATA_FORK);
1186 * Reset our pointers, the data has moved.
1188 oldsfp = (xfs_dir2_sf_t *)buf;
1189 sfp = (xfs_dir2_sf_t *)dp->i_df.if_u1.if_data;
1191 * Fill in the new header.
1193 sfp->hdr.count = oldsfp->hdr.count;
1194 sfp->hdr.i8count = 0;
1195 ino = XFS_DIR2_SF_GET_INUMBER(oldsfp, &oldsfp->hdr.parent);
1196 XFS_DIR2_SF_PUT_INUMBER(sfp, &ino, &sfp->hdr.parent);
1198 * Copy the entries field by field.
1200 for (i = 0, sfep = XFS_DIR2_SF_FIRSTENTRY(sfp),
1201 oldsfep = XFS_DIR2_SF_FIRSTENTRY(oldsfp);
1202 i < sfp->hdr.count;
1203 i++, sfep = XFS_DIR2_SF_NEXTENTRY(sfp, sfep),
1204 oldsfep = XFS_DIR2_SF_NEXTENTRY(oldsfp, oldsfep)) {
1205 sfep->namelen = oldsfep->namelen;
1206 sfep->offset = oldsfep->offset;
1207 memcpy(sfep->name, oldsfep->name, sfep->namelen);
1208 ino = XFS_DIR2_SF_GET_INUMBER(oldsfp,
1209 XFS_DIR2_SF_INUMBERP(oldsfep));
1210 XFS_DIR2_SF_PUT_INUMBER(sfp, &ino, XFS_DIR2_SF_INUMBERP(sfep));
1213 * Clean up the inode.
1215 kmem_free(buf, oldsize);
1216 dp->i_d.di_size = newsize;
1217 xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE | XFS_ILOG_DDATA);
1221 * Convert from 4-byte inode numbers to 8-byte inode numbers.
1222 * The new 8-byte inode number is not there yet, we leave with the
1223 * count 1 but no corresponding entry.
1225 static void
1226 xfs_dir2_sf_toino8(
1227 xfs_da_args_t *args) /* operation arguments */
1229 char *buf; /* old dir's buffer */
1230 xfs_inode_t *dp; /* incore directory inode */
1231 int i; /* entry index */
1232 xfs_ino_t ino; /* entry inode number */
1233 int newsize; /* new inode size */
1234 xfs_dir2_sf_entry_t *oldsfep; /* old sf entry */
1235 xfs_dir2_sf_t *oldsfp; /* old sf directory */
1236 int oldsize; /* old inode size */
1237 xfs_dir2_sf_entry_t *sfep; /* new sf entry */
1238 xfs_dir2_sf_t *sfp; /* new sf directory */
1240 xfs_dir2_trace_args("sf_toino8", args);
1241 dp = args->dp;
1244 * Copy the old directory to the buffer.
1245 * Then nuke it from the inode, and add the new buffer to the inode.
1246 * Don't want xfs_idata_realloc copying the data here.
1248 oldsize = dp->i_df.if_bytes;
1249 buf = kmem_alloc(oldsize, KM_SLEEP);
1250 oldsfp = (xfs_dir2_sf_t *)dp->i_df.if_u1.if_data;
1251 ASSERT(oldsfp->hdr.i8count == 0);
1252 memcpy(buf, oldsfp, oldsize);
1254 * Compute the new inode size.
1256 newsize =
1257 oldsize +
1258 (oldsfp->hdr.count + 1) *
1259 ((uint)sizeof(xfs_dir2_ino8_t) - (uint)sizeof(xfs_dir2_ino4_t));
1260 xfs_idata_realloc(dp, -oldsize, XFS_DATA_FORK);
1261 xfs_idata_realloc(dp, newsize, XFS_DATA_FORK);
1263 * Reset our pointers, the data has moved.
1265 oldsfp = (xfs_dir2_sf_t *)buf;
1266 sfp = (xfs_dir2_sf_t *)dp->i_df.if_u1.if_data;
1268 * Fill in the new header.
1270 sfp->hdr.count = oldsfp->hdr.count;
1271 sfp->hdr.i8count = 1;
1272 ino = XFS_DIR2_SF_GET_INUMBER(oldsfp, &oldsfp->hdr.parent);
1273 XFS_DIR2_SF_PUT_INUMBER(sfp, &ino, &sfp->hdr.parent);
1275 * Copy the entries field by field.
1277 for (i = 0, sfep = XFS_DIR2_SF_FIRSTENTRY(sfp),
1278 oldsfep = XFS_DIR2_SF_FIRSTENTRY(oldsfp);
1279 i < sfp->hdr.count;
1280 i++, sfep = XFS_DIR2_SF_NEXTENTRY(sfp, sfep),
1281 oldsfep = XFS_DIR2_SF_NEXTENTRY(oldsfp, oldsfep)) {
1282 sfep->namelen = oldsfep->namelen;
1283 sfep->offset = oldsfep->offset;
1284 memcpy(sfep->name, oldsfep->name, sfep->namelen);
1285 ino = XFS_DIR2_SF_GET_INUMBER(oldsfp,
1286 XFS_DIR2_SF_INUMBERP(oldsfep));
1287 XFS_DIR2_SF_PUT_INUMBER(sfp, &ino, XFS_DIR2_SF_INUMBERP(sfep));
1290 * Clean up the inode.
1292 kmem_free(buf, oldsize);
1293 dp->i_d.di_size = newsize;
1294 xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE | XFS_ILOG_DDATA);
1296 #endif /* XFS_BIG_INUMS */