bridge: fix a possible use after free
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / fs / xfs / xfs_dir2_sf.c
blob79d05e84e296a8d7f932e21e574c0f0545308ea7
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_ag.h"
26 #include "xfs_mount.h"
27 #include "xfs_da_btree.h"
28 #include "xfs_bmap_btree.h"
29 #include "xfs_dinode.h"
30 #include "xfs_inode.h"
31 #include "xfs_inode_item.h"
32 #include "xfs_error.h"
33 #include "xfs_dir2.h"
34 #include "xfs_dir2_format.h"
35 #include "xfs_dir2_priv.h"
36 #include "xfs_trace.h"
39 * Prototypes for internal functions.
41 static void xfs_dir2_sf_addname_easy(xfs_da_args_t *args,
42 xfs_dir2_sf_entry_t *sfep,
43 xfs_dir2_data_aoff_t offset,
44 int new_isize);
45 static void xfs_dir2_sf_addname_hard(xfs_da_args_t *args, int objchange,
46 int new_isize);
47 static int xfs_dir2_sf_addname_pick(xfs_da_args_t *args, int objchange,
48 xfs_dir2_sf_entry_t **sfepp,
49 xfs_dir2_data_aoff_t *offsetp);
50 #ifdef DEBUG
51 static void xfs_dir2_sf_check(xfs_da_args_t *args);
52 #else
53 #define xfs_dir2_sf_check(args)
54 #endif /* DEBUG */
55 #if XFS_BIG_INUMS
56 static void xfs_dir2_sf_toino4(xfs_da_args_t *args);
57 static void xfs_dir2_sf_toino8(xfs_da_args_t *args);
58 #endif /* XFS_BIG_INUMS */
61 * Inode numbers in short-form directories can come in two versions,
62 * either 4 bytes or 8 bytes wide. These helpers deal with the
63 * two forms transparently by looking at the headers i8count field.
65 * For 64-bit inode number the most significant byte must be zero.
67 static xfs_ino_t
68 xfs_dir2_sf_get_ino(
69 struct xfs_dir2_sf_hdr *hdr,
70 xfs_dir2_inou_t *from)
72 if (hdr->i8count)
73 return get_unaligned_be64(&from->i8.i) & 0x00ffffffffffffffULL;
74 else
75 return get_unaligned_be32(&from->i4.i);
78 static void
79 xfs_dir2_sf_put_ino(
80 struct xfs_dir2_sf_hdr *hdr,
81 xfs_dir2_inou_t *to,
82 xfs_ino_t ino)
84 ASSERT((ino & 0xff00000000000000ULL) == 0);
86 if (hdr->i8count)
87 put_unaligned_be64(ino, &to->i8.i);
88 else
89 put_unaligned_be32(ino, &to->i4.i);
92 xfs_ino_t
93 xfs_dir2_sf_get_parent_ino(
94 struct xfs_dir2_sf_hdr *hdr)
96 return xfs_dir2_sf_get_ino(hdr, &hdr->parent);
99 static void
100 xfs_dir2_sf_put_parent_ino(
101 struct xfs_dir2_sf_hdr *hdr,
102 xfs_ino_t ino)
104 xfs_dir2_sf_put_ino(hdr, &hdr->parent, ino);
108 * In short-form directory entries the inode numbers are stored at variable
109 * offset behind the entry name. The inode numbers may only be accessed
110 * through the helpers below.
112 static xfs_dir2_inou_t *
113 xfs_dir2_sfe_inop(
114 struct xfs_dir2_sf_entry *sfep)
116 return (xfs_dir2_inou_t *)&sfep->name[sfep->namelen];
119 xfs_ino_t
120 xfs_dir2_sfe_get_ino(
121 struct xfs_dir2_sf_hdr *hdr,
122 struct xfs_dir2_sf_entry *sfep)
124 return xfs_dir2_sf_get_ino(hdr, xfs_dir2_sfe_inop(sfep));
127 static void
128 xfs_dir2_sfe_put_ino(
129 struct xfs_dir2_sf_hdr *hdr,
130 struct xfs_dir2_sf_entry *sfep,
131 xfs_ino_t ino)
133 xfs_dir2_sf_put_ino(hdr, xfs_dir2_sfe_inop(sfep), ino);
137 * Given a block directory (dp/block), calculate its size as a shortform (sf)
138 * directory and a header for the sf directory, if it will fit it the
139 * space currently present in the inode. If it won't fit, the output
140 * size is too big (but not accurate).
142 int /* size for sf form */
143 xfs_dir2_block_sfsize(
144 xfs_inode_t *dp, /* incore inode pointer */
145 xfs_dir2_data_hdr_t *hdr, /* block directory data */
146 xfs_dir2_sf_hdr_t *sfhp) /* output: header for sf form */
148 xfs_dir2_dataptr_t addr; /* data entry address */
149 xfs_dir2_leaf_entry_t *blp; /* leaf area of the block */
150 xfs_dir2_block_tail_t *btp; /* tail area of the block */
151 int count; /* shortform entry count */
152 xfs_dir2_data_entry_t *dep; /* data entry in the block */
153 int i; /* block entry index */
154 int i8count; /* count of big-inode entries */
155 int isdot; /* entry is "." */
156 int isdotdot; /* entry is ".." */
157 xfs_mount_t *mp; /* mount structure pointer */
158 int namelen; /* total name bytes */
159 xfs_ino_t parent = 0; /* parent inode number */
160 int size=0; /* total computed size */
162 mp = dp->i_mount;
164 count = i8count = namelen = 0;
165 btp = xfs_dir2_block_tail_p(mp, hdr);
166 blp = xfs_dir2_block_leaf_p(btp);
169 * Iterate over the block's data entries by using the leaf pointers.
171 for (i = 0; i < be32_to_cpu(btp->count); i++) {
172 if ((addr = be32_to_cpu(blp[i].address)) == XFS_DIR2_NULL_DATAPTR)
173 continue;
175 * Calculate the pointer to the entry at hand.
177 dep = (xfs_dir2_data_entry_t *)
178 ((char *)hdr + xfs_dir2_dataptr_to_off(mp, addr));
180 * Detect . and .., so we can special-case them.
181 * . is not included in sf directories.
182 * .. is included by just the parent inode number.
184 isdot = dep->namelen == 1 && dep->name[0] == '.';
185 isdotdot =
186 dep->namelen == 2 &&
187 dep->name[0] == '.' && dep->name[1] == '.';
188 #if XFS_BIG_INUMS
189 if (!isdot)
190 i8count += be64_to_cpu(dep->inumber) > XFS_DIR2_MAX_SHORT_INUM;
191 #endif
192 if (!isdot && !isdotdot) {
193 count++;
194 namelen += dep->namelen;
195 } else if (isdotdot)
196 parent = be64_to_cpu(dep->inumber);
198 * Calculate the new size, see if we should give up yet.
200 size = xfs_dir2_sf_hdr_size(i8count) + /* header */
201 count + /* namelen */
202 count * (uint)sizeof(xfs_dir2_sf_off_t) + /* offset */
203 namelen + /* name */
204 (i8count ? /* inumber */
205 (uint)sizeof(xfs_dir2_ino8_t) * count :
206 (uint)sizeof(xfs_dir2_ino4_t) * count);
207 if (size > XFS_IFORK_DSIZE(dp))
208 return size; /* size value is a failure */
211 * Create the output header, if it worked.
213 sfhp->count = count;
214 sfhp->i8count = i8count;
215 xfs_dir2_sf_put_parent_ino(sfhp, parent);
216 return size;
220 * Convert a block format directory to shortform.
221 * Caller has already checked that it will fit, and built us a header.
223 int /* error */
224 xfs_dir2_block_to_sf(
225 xfs_da_args_t *args, /* operation arguments */
226 xfs_dabuf_t *bp, /* block buffer */
227 int size, /* shortform directory size */
228 xfs_dir2_sf_hdr_t *sfhp) /* shortform directory hdr */
230 xfs_dir2_data_hdr_t *hdr; /* block header */
231 xfs_dir2_block_tail_t *btp; /* block tail pointer */
232 xfs_dir2_data_entry_t *dep; /* data entry pointer */
233 xfs_inode_t *dp; /* incore directory inode */
234 xfs_dir2_data_unused_t *dup; /* unused data pointer */
235 char *endptr; /* end of data entries */
236 int error; /* error return value */
237 int logflags; /* inode logging flags */
238 xfs_mount_t *mp; /* filesystem mount point */
239 char *ptr; /* current data pointer */
240 xfs_dir2_sf_entry_t *sfep; /* shortform entry */
241 xfs_dir2_sf_hdr_t *sfp; /* shortform directory header */
243 trace_xfs_dir2_block_to_sf(args);
245 dp = args->dp;
246 mp = dp->i_mount;
249 * Make a copy of the block data, so we can shrink the inode
250 * and add local data.
252 hdr = kmem_alloc(mp->m_dirblksize, KM_SLEEP);
253 memcpy(hdr, bp->data, mp->m_dirblksize);
254 logflags = XFS_ILOG_CORE;
255 if ((error = xfs_dir2_shrink_inode(args, mp->m_dirdatablk, bp))) {
256 ASSERT(error != ENOSPC);
257 goto out;
261 * The buffer is now unconditionally gone, whether
262 * xfs_dir2_shrink_inode worked or not.
264 * Convert the inode to local format.
266 dp->i_df.if_flags &= ~XFS_IFEXTENTS;
267 dp->i_df.if_flags |= XFS_IFINLINE;
268 dp->i_d.di_format = XFS_DINODE_FMT_LOCAL;
269 ASSERT(dp->i_df.if_bytes == 0);
270 xfs_idata_realloc(dp, size, XFS_DATA_FORK);
271 logflags |= XFS_ILOG_DDATA;
273 * Copy the header into the newly allocate local space.
275 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
276 memcpy(sfp, sfhp, xfs_dir2_sf_hdr_size(sfhp->i8count));
277 dp->i_d.di_size = size;
279 * Set up to loop over the block's entries.
281 btp = xfs_dir2_block_tail_p(mp, hdr);
282 ptr = (char *)(hdr + 1);
283 endptr = (char *)xfs_dir2_block_leaf_p(btp);
284 sfep = xfs_dir2_sf_firstentry(sfp);
286 * Loop over the active and unused entries.
287 * Stop when we reach the leaf/tail portion of the block.
289 while (ptr < endptr) {
291 * If it's unused, just skip over it.
293 dup = (xfs_dir2_data_unused_t *)ptr;
294 if (be16_to_cpu(dup->freetag) == XFS_DIR2_DATA_FREE_TAG) {
295 ptr += be16_to_cpu(dup->length);
296 continue;
298 dep = (xfs_dir2_data_entry_t *)ptr;
300 * Skip .
302 if (dep->namelen == 1 && dep->name[0] == '.')
303 ASSERT(be64_to_cpu(dep->inumber) == dp->i_ino);
305 * Skip .., but make sure the inode number is right.
307 else if (dep->namelen == 2 &&
308 dep->name[0] == '.' && dep->name[1] == '.')
309 ASSERT(be64_to_cpu(dep->inumber) ==
310 xfs_dir2_sf_get_parent_ino(sfp));
312 * Normal entry, copy it into shortform.
314 else {
315 sfep->namelen = dep->namelen;
316 xfs_dir2_sf_put_offset(sfep,
317 (xfs_dir2_data_aoff_t)
318 ((char *)dep - (char *)hdr));
319 memcpy(sfep->name, dep->name, dep->namelen);
320 xfs_dir2_sfe_put_ino(sfp, sfep,
321 be64_to_cpu(dep->inumber));
323 sfep = xfs_dir2_sf_nextentry(sfp, sfep);
325 ptr += xfs_dir2_data_entsize(dep->namelen);
327 ASSERT((char *)sfep - (char *)sfp == size);
328 xfs_dir2_sf_check(args);
329 out:
330 xfs_trans_log_inode(args->trans, dp, logflags);
331 kmem_free(hdr);
332 return error;
336 * Add a name to a shortform directory.
337 * There are two algorithms, "easy" and "hard" which we decide on
338 * before changing anything.
339 * Convert to block form if necessary, if the new entry won't fit.
341 int /* error */
342 xfs_dir2_sf_addname(
343 xfs_da_args_t *args) /* operation arguments */
345 int add_entsize; /* size of the new entry */
346 xfs_inode_t *dp; /* incore directory inode */
347 int error; /* error return value */
348 int incr_isize; /* total change in size */
349 int new_isize; /* di_size after adding name */
350 int objchange; /* changing to 8-byte inodes */
351 xfs_dir2_data_aoff_t offset = 0; /* offset for new entry */
352 int old_isize; /* di_size before adding name */
353 int pick; /* which algorithm to use */
354 xfs_dir2_sf_hdr_t *sfp; /* shortform structure */
355 xfs_dir2_sf_entry_t *sfep = NULL; /* shortform entry */
357 trace_xfs_dir2_sf_addname(args);
359 ASSERT(xfs_dir2_sf_lookup(args) == ENOENT);
360 dp = args->dp;
361 ASSERT(dp->i_df.if_flags & XFS_IFINLINE);
363 * Make sure the shortform value has some of its header.
365 if (dp->i_d.di_size < offsetof(xfs_dir2_sf_hdr_t, parent)) {
366 ASSERT(XFS_FORCED_SHUTDOWN(dp->i_mount));
367 return XFS_ERROR(EIO);
369 ASSERT(dp->i_df.if_bytes == dp->i_d.di_size);
370 ASSERT(dp->i_df.if_u1.if_data != NULL);
371 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
372 ASSERT(dp->i_d.di_size >= xfs_dir2_sf_hdr_size(sfp->i8count));
374 * Compute entry (and change in) size.
376 add_entsize = xfs_dir2_sf_entsize(sfp, args->namelen);
377 incr_isize = add_entsize;
378 objchange = 0;
379 #if XFS_BIG_INUMS
381 * Do we have to change to 8 byte inodes?
383 if (args->inumber > XFS_DIR2_MAX_SHORT_INUM && sfp->i8count == 0) {
385 * Yes, adjust the entry size and the total size.
387 add_entsize +=
388 (uint)sizeof(xfs_dir2_ino8_t) -
389 (uint)sizeof(xfs_dir2_ino4_t);
390 incr_isize +=
391 (sfp->count + 2) *
392 ((uint)sizeof(xfs_dir2_ino8_t) -
393 (uint)sizeof(xfs_dir2_ino4_t));
394 objchange = 1;
396 #endif
397 old_isize = (int)dp->i_d.di_size;
398 new_isize = old_isize + incr_isize;
400 * Won't fit as shortform any more (due to size),
401 * or the pick routine says it won't (due to offset values).
403 if (new_isize > XFS_IFORK_DSIZE(dp) ||
404 (pick =
405 xfs_dir2_sf_addname_pick(args, objchange, &sfep, &offset)) == 0) {
407 * Just checking or no space reservation, it doesn't fit.
409 if ((args->op_flags & XFS_DA_OP_JUSTCHECK) || args->total == 0)
410 return XFS_ERROR(ENOSPC);
412 * Convert to block form then add the name.
414 error = xfs_dir2_sf_to_block(args);
415 if (error)
416 return error;
417 return xfs_dir2_block_addname(args);
420 * Just checking, it fits.
422 if (args->op_flags & XFS_DA_OP_JUSTCHECK)
423 return 0;
425 * Do it the easy way - just add it at the end.
427 if (pick == 1)
428 xfs_dir2_sf_addname_easy(args, sfep, offset, new_isize);
430 * Do it the hard way - look for a place to insert the new entry.
431 * Convert to 8 byte inode numbers first if necessary.
433 else {
434 ASSERT(pick == 2);
435 #if XFS_BIG_INUMS
436 if (objchange)
437 xfs_dir2_sf_toino8(args);
438 #endif
439 xfs_dir2_sf_addname_hard(args, objchange, new_isize);
441 xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE | XFS_ILOG_DDATA);
442 return 0;
446 * Add the new entry the "easy" way.
447 * This is copying the old directory and adding the new entry at the end.
448 * Since it's sorted by "offset" we need room after the last offset
449 * that's already there, and then room to convert to a block directory.
450 * This is already checked by the pick routine.
452 static void
453 xfs_dir2_sf_addname_easy(
454 xfs_da_args_t *args, /* operation arguments */
455 xfs_dir2_sf_entry_t *sfep, /* pointer to new entry */
456 xfs_dir2_data_aoff_t offset, /* offset to use for new ent */
457 int new_isize) /* new directory size */
459 int byteoff; /* byte offset in sf dir */
460 xfs_inode_t *dp; /* incore directory inode */
461 xfs_dir2_sf_hdr_t *sfp; /* shortform structure */
463 dp = args->dp;
465 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
466 byteoff = (int)((char *)sfep - (char *)sfp);
468 * Grow the in-inode space.
470 xfs_idata_realloc(dp, xfs_dir2_sf_entsize(sfp, args->namelen),
471 XFS_DATA_FORK);
473 * Need to set up again due to realloc of the inode data.
475 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
476 sfep = (xfs_dir2_sf_entry_t *)((char *)sfp + byteoff);
478 * Fill in the new entry.
480 sfep->namelen = args->namelen;
481 xfs_dir2_sf_put_offset(sfep, offset);
482 memcpy(sfep->name, args->name, sfep->namelen);
483 xfs_dir2_sfe_put_ino(sfp, sfep, args->inumber);
485 * Update the header and inode.
487 sfp->count++;
488 #if XFS_BIG_INUMS
489 if (args->inumber > XFS_DIR2_MAX_SHORT_INUM)
490 sfp->i8count++;
491 #endif
492 dp->i_d.di_size = new_isize;
493 xfs_dir2_sf_check(args);
497 * Add the new entry the "hard" way.
498 * The caller has already converted to 8 byte inode numbers if necessary,
499 * in which case we need to leave the i8count at 1.
500 * Find a hole that the new entry will fit into, and copy
501 * the first part of the entries, the new entry, and the last part of
502 * the entries.
504 /* ARGSUSED */
505 static void
506 xfs_dir2_sf_addname_hard(
507 xfs_da_args_t *args, /* operation arguments */
508 int objchange, /* changing inode number size */
509 int new_isize) /* new directory size */
511 int add_datasize; /* data size need for new ent */
512 char *buf; /* buffer for old */
513 xfs_inode_t *dp; /* incore directory inode */
514 int eof; /* reached end of old dir */
515 int nbytes; /* temp for byte copies */
516 xfs_dir2_data_aoff_t new_offset; /* next offset value */
517 xfs_dir2_data_aoff_t offset; /* current offset value */
518 int old_isize; /* previous di_size */
519 xfs_dir2_sf_entry_t *oldsfep; /* entry in original dir */
520 xfs_dir2_sf_hdr_t *oldsfp; /* original shortform dir */
521 xfs_dir2_sf_entry_t *sfep; /* entry in new dir */
522 xfs_dir2_sf_hdr_t *sfp; /* new shortform dir */
525 * Copy the old directory to the stack buffer.
527 dp = args->dp;
529 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
530 old_isize = (int)dp->i_d.di_size;
531 buf = kmem_alloc(old_isize, KM_SLEEP);
532 oldsfp = (xfs_dir2_sf_hdr_t *)buf;
533 memcpy(oldsfp, sfp, old_isize);
535 * Loop over the old directory finding the place we're going
536 * to insert the new entry.
537 * If it's going to end up at the end then oldsfep will point there.
539 for (offset = XFS_DIR2_DATA_FIRST_OFFSET,
540 oldsfep = xfs_dir2_sf_firstentry(oldsfp),
541 add_datasize = xfs_dir2_data_entsize(args->namelen),
542 eof = (char *)oldsfep == &buf[old_isize];
543 !eof;
544 offset = new_offset + xfs_dir2_data_entsize(oldsfep->namelen),
545 oldsfep = xfs_dir2_sf_nextentry(oldsfp, oldsfep),
546 eof = (char *)oldsfep == &buf[old_isize]) {
547 new_offset = xfs_dir2_sf_get_offset(oldsfep);
548 if (offset + add_datasize <= new_offset)
549 break;
552 * Get rid of the old directory, then allocate space for
553 * the new one. We do this so xfs_idata_realloc won't copy
554 * the data.
556 xfs_idata_realloc(dp, -old_isize, XFS_DATA_FORK);
557 xfs_idata_realloc(dp, new_isize, XFS_DATA_FORK);
559 * Reset the pointer since the buffer was reallocated.
561 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
563 * Copy the first part of the directory, including the header.
565 nbytes = (int)((char *)oldsfep - (char *)oldsfp);
566 memcpy(sfp, oldsfp, nbytes);
567 sfep = (xfs_dir2_sf_entry_t *)((char *)sfp + nbytes);
569 * Fill in the new entry, and update the header counts.
571 sfep->namelen = args->namelen;
572 xfs_dir2_sf_put_offset(sfep, offset);
573 memcpy(sfep->name, args->name, sfep->namelen);
574 xfs_dir2_sfe_put_ino(sfp, sfep, args->inumber);
575 sfp->count++;
576 #if XFS_BIG_INUMS
577 if (args->inumber > XFS_DIR2_MAX_SHORT_INUM && !objchange)
578 sfp->i8count++;
579 #endif
581 * If there's more left to copy, do that.
583 if (!eof) {
584 sfep = xfs_dir2_sf_nextentry(sfp, sfep);
585 memcpy(sfep, oldsfep, old_isize - nbytes);
587 kmem_free(buf);
588 dp->i_d.di_size = new_isize;
589 xfs_dir2_sf_check(args);
593 * Decide if the new entry will fit at all.
594 * If it will fit, pick between adding the new entry to the end (easy)
595 * or somewhere else (hard).
596 * Return 0 (won't fit), 1 (easy), 2 (hard).
598 /*ARGSUSED*/
599 static int /* pick result */
600 xfs_dir2_sf_addname_pick(
601 xfs_da_args_t *args, /* operation arguments */
602 int objchange, /* inode # size changes */
603 xfs_dir2_sf_entry_t **sfepp, /* out(1): new entry ptr */
604 xfs_dir2_data_aoff_t *offsetp) /* out(1): new offset */
606 xfs_inode_t *dp; /* incore directory inode */
607 int holefit; /* found hole it will fit in */
608 int i; /* entry number */
609 xfs_mount_t *mp; /* filesystem mount point */
610 xfs_dir2_data_aoff_t offset; /* data block offset */
611 xfs_dir2_sf_entry_t *sfep; /* shortform entry */
612 xfs_dir2_sf_hdr_t *sfp; /* shortform structure */
613 int size; /* entry's data size */
614 int used; /* data bytes used */
616 dp = args->dp;
617 mp = dp->i_mount;
619 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
620 size = xfs_dir2_data_entsize(args->namelen);
621 offset = XFS_DIR2_DATA_FIRST_OFFSET;
622 sfep = xfs_dir2_sf_firstentry(sfp);
623 holefit = 0;
625 * Loop over sf entries.
626 * Keep track of data offset and whether we've seen a place
627 * to insert the new entry.
629 for (i = 0; i < sfp->count; i++) {
630 if (!holefit)
631 holefit = offset + size <= xfs_dir2_sf_get_offset(sfep);
632 offset = xfs_dir2_sf_get_offset(sfep) +
633 xfs_dir2_data_entsize(sfep->namelen);
634 sfep = xfs_dir2_sf_nextentry(sfp, sfep);
637 * Calculate data bytes used excluding the new entry, if this
638 * was a data block (block form directory).
640 used = offset +
641 (sfp->count + 3) * (uint)sizeof(xfs_dir2_leaf_entry_t) +
642 (uint)sizeof(xfs_dir2_block_tail_t);
644 * If it won't fit in a block form then we can't insert it,
645 * we'll go back, convert to block, then try the insert and convert
646 * to leaf.
648 if (used + (holefit ? 0 : size) > mp->m_dirblksize)
649 return 0;
651 * If changing the inode number size, do it the hard way.
653 #if XFS_BIG_INUMS
654 if (objchange) {
655 return 2;
657 #else
658 ASSERT(objchange == 0);
659 #endif
661 * If it won't fit at the end then do it the hard way (use the hole).
663 if (used + size > mp->m_dirblksize)
664 return 2;
666 * Do it the easy way.
668 *sfepp = sfep;
669 *offsetp = offset;
670 return 1;
673 #ifdef DEBUG
675 * Check consistency of shortform directory, assert if bad.
677 static void
678 xfs_dir2_sf_check(
679 xfs_da_args_t *args) /* operation arguments */
681 xfs_inode_t *dp; /* incore directory inode */
682 int i; /* entry number */
683 int i8count; /* number of big inode#s */
684 xfs_ino_t ino; /* entry inode number */
685 int offset; /* data offset */
686 xfs_dir2_sf_entry_t *sfep; /* shortform dir entry */
687 xfs_dir2_sf_hdr_t *sfp; /* shortform structure */
689 dp = args->dp;
691 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
692 offset = XFS_DIR2_DATA_FIRST_OFFSET;
693 ino = xfs_dir2_sf_get_parent_ino(sfp);
694 i8count = ino > XFS_DIR2_MAX_SHORT_INUM;
696 for (i = 0, sfep = xfs_dir2_sf_firstentry(sfp);
697 i < sfp->count;
698 i++, sfep = xfs_dir2_sf_nextentry(sfp, sfep)) {
699 ASSERT(xfs_dir2_sf_get_offset(sfep) >= offset);
700 ino = xfs_dir2_sfe_get_ino(sfp, sfep);
701 i8count += ino > XFS_DIR2_MAX_SHORT_INUM;
702 offset =
703 xfs_dir2_sf_get_offset(sfep) +
704 xfs_dir2_data_entsize(sfep->namelen);
706 ASSERT(i8count == sfp->i8count);
707 ASSERT(XFS_BIG_INUMS || i8count == 0);
708 ASSERT((char *)sfep - (char *)sfp == dp->i_d.di_size);
709 ASSERT(offset +
710 (sfp->count + 2) * (uint)sizeof(xfs_dir2_leaf_entry_t) +
711 (uint)sizeof(xfs_dir2_block_tail_t) <=
712 dp->i_mount->m_dirblksize);
714 #endif /* DEBUG */
717 * Create a new (shortform) directory.
719 int /* error, always 0 */
720 xfs_dir2_sf_create(
721 xfs_da_args_t *args, /* operation arguments */
722 xfs_ino_t pino) /* parent inode number */
724 xfs_inode_t *dp; /* incore directory inode */
725 int i8count; /* parent inode is an 8-byte number */
726 xfs_dir2_sf_hdr_t *sfp; /* shortform structure */
727 int size; /* directory size */
729 trace_xfs_dir2_sf_create(args);
731 dp = args->dp;
733 ASSERT(dp != NULL);
734 ASSERT(dp->i_d.di_size == 0);
736 * If it's currently a zero-length extent file,
737 * convert it to local format.
739 if (dp->i_d.di_format == XFS_DINODE_FMT_EXTENTS) {
740 dp->i_df.if_flags &= ~XFS_IFEXTENTS; /* just in case */
741 dp->i_d.di_format = XFS_DINODE_FMT_LOCAL;
742 xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE);
743 dp->i_df.if_flags |= XFS_IFINLINE;
745 ASSERT(dp->i_df.if_flags & XFS_IFINLINE);
746 ASSERT(dp->i_df.if_bytes == 0);
747 i8count = pino > XFS_DIR2_MAX_SHORT_INUM;
748 size = xfs_dir2_sf_hdr_size(i8count);
750 * Make a buffer for the data.
752 xfs_idata_realloc(dp, size, XFS_DATA_FORK);
754 * Fill in the header,
756 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
757 sfp->i8count = i8count;
759 * Now can put in the inode number, since i8count is set.
761 xfs_dir2_sf_put_parent_ino(sfp, pino);
762 sfp->count = 0;
763 dp->i_d.di_size = size;
764 xfs_dir2_sf_check(args);
765 xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE | XFS_ILOG_DDATA);
766 return 0;
769 int /* error */
770 xfs_dir2_sf_getdents(
771 xfs_inode_t *dp, /* incore directory inode */
772 void *dirent,
773 xfs_off_t *offset,
774 filldir_t filldir)
776 int i; /* shortform entry number */
777 xfs_mount_t *mp; /* filesystem mount point */
778 xfs_dir2_dataptr_t off; /* current entry's offset */
779 xfs_dir2_sf_entry_t *sfep; /* shortform directory entry */
780 xfs_dir2_sf_hdr_t *sfp; /* shortform structure */
781 xfs_dir2_dataptr_t dot_offset;
782 xfs_dir2_dataptr_t dotdot_offset;
783 xfs_ino_t ino;
785 mp = dp->i_mount;
787 ASSERT(dp->i_df.if_flags & XFS_IFINLINE);
789 * Give up if the directory is way too short.
791 if (dp->i_d.di_size < offsetof(xfs_dir2_sf_hdr_t, parent)) {
792 ASSERT(XFS_FORCED_SHUTDOWN(mp));
793 return XFS_ERROR(EIO);
796 ASSERT(dp->i_df.if_bytes == dp->i_d.di_size);
797 ASSERT(dp->i_df.if_u1.if_data != NULL);
799 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
801 ASSERT(dp->i_d.di_size >= xfs_dir2_sf_hdr_size(sfp->i8count));
804 * If the block number in the offset is out of range, we're done.
806 if (xfs_dir2_dataptr_to_db(mp, *offset) > mp->m_dirdatablk)
807 return 0;
810 * Precalculate offsets for . and .. as we will always need them.
812 * XXX(hch): the second argument is sometimes 0 and sometimes
813 * mp->m_dirdatablk.
815 dot_offset = xfs_dir2_db_off_to_dataptr(mp, mp->m_dirdatablk,
816 XFS_DIR2_DATA_DOT_OFFSET);
817 dotdot_offset = xfs_dir2_db_off_to_dataptr(mp, mp->m_dirdatablk,
818 XFS_DIR2_DATA_DOTDOT_OFFSET);
821 * Put . entry unless we're starting past it.
823 if (*offset <= dot_offset) {
824 if (filldir(dirent, ".", 1, dot_offset & 0x7fffffff, dp->i_ino, DT_DIR)) {
825 *offset = dot_offset & 0x7fffffff;
826 return 0;
831 * Put .. entry unless we're starting past it.
833 if (*offset <= dotdot_offset) {
834 ino = xfs_dir2_sf_get_parent_ino(sfp);
835 if (filldir(dirent, "..", 2, dotdot_offset & 0x7fffffff, ino, DT_DIR)) {
836 *offset = dotdot_offset & 0x7fffffff;
837 return 0;
842 * Loop while there are more entries and put'ing works.
844 sfep = xfs_dir2_sf_firstentry(sfp);
845 for (i = 0; i < sfp->count; i++) {
846 off = xfs_dir2_db_off_to_dataptr(mp, mp->m_dirdatablk,
847 xfs_dir2_sf_get_offset(sfep));
849 if (*offset > off) {
850 sfep = xfs_dir2_sf_nextentry(sfp, sfep);
851 continue;
854 ino = xfs_dir2_sfe_get_ino(sfp, sfep);
855 if (filldir(dirent, (char *)sfep->name, sfep->namelen,
856 off & 0x7fffffff, ino, DT_UNKNOWN)) {
857 *offset = off & 0x7fffffff;
858 return 0;
860 sfep = xfs_dir2_sf_nextentry(sfp, sfep);
863 *offset = xfs_dir2_db_off_to_dataptr(mp, mp->m_dirdatablk + 1, 0) &
864 0x7fffffff;
865 return 0;
869 * Lookup an entry in a shortform directory.
870 * Returns EEXIST if found, ENOENT if not found.
872 int /* error */
873 xfs_dir2_sf_lookup(
874 xfs_da_args_t *args) /* operation arguments */
876 xfs_inode_t *dp; /* incore directory inode */
877 int i; /* entry index */
878 int error;
879 xfs_dir2_sf_entry_t *sfep; /* shortform directory entry */
880 xfs_dir2_sf_hdr_t *sfp; /* shortform structure */
881 enum xfs_dacmp cmp; /* comparison result */
882 xfs_dir2_sf_entry_t *ci_sfep; /* case-insens. entry */
884 trace_xfs_dir2_sf_lookup(args);
886 xfs_dir2_sf_check(args);
887 dp = args->dp;
889 ASSERT(dp->i_df.if_flags & XFS_IFINLINE);
891 * Bail out if the directory is way too short.
893 if (dp->i_d.di_size < offsetof(xfs_dir2_sf_hdr_t, parent)) {
894 ASSERT(XFS_FORCED_SHUTDOWN(dp->i_mount));
895 return XFS_ERROR(EIO);
897 ASSERT(dp->i_df.if_bytes == dp->i_d.di_size);
898 ASSERT(dp->i_df.if_u1.if_data != NULL);
899 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
900 ASSERT(dp->i_d.di_size >= xfs_dir2_sf_hdr_size(sfp->i8count));
902 * Special case for .
904 if (args->namelen == 1 && args->name[0] == '.') {
905 args->inumber = dp->i_ino;
906 args->cmpresult = XFS_CMP_EXACT;
907 return XFS_ERROR(EEXIST);
910 * Special case for ..
912 if (args->namelen == 2 &&
913 args->name[0] == '.' && args->name[1] == '.') {
914 args->inumber = xfs_dir2_sf_get_parent_ino(sfp);
915 args->cmpresult = XFS_CMP_EXACT;
916 return XFS_ERROR(EEXIST);
919 * Loop over all the entries trying to match ours.
921 ci_sfep = NULL;
922 for (i = 0, sfep = xfs_dir2_sf_firstentry(sfp); i < sfp->count;
923 i++, sfep = xfs_dir2_sf_nextentry(sfp, sfep)) {
925 * Compare name and if it's an exact match, return the inode
926 * number. If it's the first case-insensitive match, store the
927 * inode number and continue looking for an exact match.
929 cmp = dp->i_mount->m_dirnameops->compname(args, sfep->name,
930 sfep->namelen);
931 if (cmp != XFS_CMP_DIFFERENT && cmp != args->cmpresult) {
932 args->cmpresult = cmp;
933 args->inumber = xfs_dir2_sfe_get_ino(sfp, sfep);
934 if (cmp == XFS_CMP_EXACT)
935 return XFS_ERROR(EEXIST);
936 ci_sfep = sfep;
939 ASSERT(args->op_flags & XFS_DA_OP_OKNOENT);
941 * Here, we can only be doing a lookup (not a rename or replace).
942 * If a case-insensitive match was not found, return ENOENT.
944 if (!ci_sfep)
945 return XFS_ERROR(ENOENT);
946 /* otherwise process the CI match as required by the caller */
947 error = xfs_dir_cilookup_result(args, ci_sfep->name, ci_sfep->namelen);
948 return XFS_ERROR(error);
952 * Remove an entry from a shortform directory.
954 int /* error */
955 xfs_dir2_sf_removename(
956 xfs_da_args_t *args)
958 int byteoff; /* offset of removed entry */
959 xfs_inode_t *dp; /* incore directory inode */
960 int entsize; /* this entry's size */
961 int i; /* shortform entry index */
962 int newsize; /* new inode size */
963 int oldsize; /* old inode size */
964 xfs_dir2_sf_entry_t *sfep; /* shortform directory entry */
965 xfs_dir2_sf_hdr_t *sfp; /* shortform structure */
967 trace_xfs_dir2_sf_removename(args);
969 dp = args->dp;
971 ASSERT(dp->i_df.if_flags & XFS_IFINLINE);
972 oldsize = (int)dp->i_d.di_size;
974 * Bail out if the directory is way too short.
976 if (oldsize < offsetof(xfs_dir2_sf_hdr_t, parent)) {
977 ASSERT(XFS_FORCED_SHUTDOWN(dp->i_mount));
978 return XFS_ERROR(EIO);
980 ASSERT(dp->i_df.if_bytes == oldsize);
981 ASSERT(dp->i_df.if_u1.if_data != NULL);
982 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
983 ASSERT(oldsize >= xfs_dir2_sf_hdr_size(sfp->i8count));
985 * Loop over the old directory entries.
986 * Find the one we're deleting.
988 for (i = 0, sfep = xfs_dir2_sf_firstentry(sfp); i < sfp->count;
989 i++, sfep = xfs_dir2_sf_nextentry(sfp, sfep)) {
990 if (xfs_da_compname(args, sfep->name, sfep->namelen) ==
991 XFS_CMP_EXACT) {
992 ASSERT(xfs_dir2_sfe_get_ino(sfp, sfep) ==
993 args->inumber);
994 break;
998 * Didn't find it.
1000 if (i == sfp->count)
1001 return XFS_ERROR(ENOENT);
1003 * Calculate sizes.
1005 byteoff = (int)((char *)sfep - (char *)sfp);
1006 entsize = xfs_dir2_sf_entsize(sfp, args->namelen);
1007 newsize = oldsize - entsize;
1009 * Copy the part if any after the removed entry, sliding it down.
1011 if (byteoff + entsize < oldsize)
1012 memmove((char *)sfp + byteoff, (char *)sfp + byteoff + entsize,
1013 oldsize - (byteoff + entsize));
1015 * Fix up the header and file size.
1017 sfp->count--;
1018 dp->i_d.di_size = newsize;
1020 * Reallocate, making it smaller.
1022 xfs_idata_realloc(dp, newsize - oldsize, XFS_DATA_FORK);
1023 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
1024 #if XFS_BIG_INUMS
1026 * Are we changing inode number size?
1028 if (args->inumber > XFS_DIR2_MAX_SHORT_INUM) {
1029 if (sfp->i8count == 1)
1030 xfs_dir2_sf_toino4(args);
1031 else
1032 sfp->i8count--;
1034 #endif
1035 xfs_dir2_sf_check(args);
1036 xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE | XFS_ILOG_DDATA);
1037 return 0;
1041 * Replace the inode number of an entry in a shortform directory.
1043 int /* error */
1044 xfs_dir2_sf_replace(
1045 xfs_da_args_t *args) /* operation arguments */
1047 xfs_inode_t *dp; /* incore directory inode */
1048 int i; /* entry index */
1049 #if XFS_BIG_INUMS || defined(DEBUG)
1050 xfs_ino_t ino=0; /* entry old inode number */
1051 #endif
1052 #if XFS_BIG_INUMS
1053 int i8elevated; /* sf_toino8 set i8count=1 */
1054 #endif
1055 xfs_dir2_sf_entry_t *sfep; /* shortform directory entry */
1056 xfs_dir2_sf_hdr_t *sfp; /* shortform structure */
1058 trace_xfs_dir2_sf_replace(args);
1060 dp = args->dp;
1062 ASSERT(dp->i_df.if_flags & XFS_IFINLINE);
1064 * Bail out if the shortform directory is way too small.
1066 if (dp->i_d.di_size < offsetof(xfs_dir2_sf_hdr_t, parent)) {
1067 ASSERT(XFS_FORCED_SHUTDOWN(dp->i_mount));
1068 return XFS_ERROR(EIO);
1070 ASSERT(dp->i_df.if_bytes == dp->i_d.di_size);
1071 ASSERT(dp->i_df.if_u1.if_data != NULL);
1072 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
1073 ASSERT(dp->i_d.di_size >= xfs_dir2_sf_hdr_size(sfp->i8count));
1074 #if XFS_BIG_INUMS
1076 * New inode number is large, and need to convert to 8-byte inodes.
1078 if (args->inumber > XFS_DIR2_MAX_SHORT_INUM && sfp->i8count == 0) {
1079 int error; /* error return value */
1080 int newsize; /* new inode size */
1082 newsize =
1083 dp->i_df.if_bytes +
1084 (sfp->count + 1) *
1085 ((uint)sizeof(xfs_dir2_ino8_t) -
1086 (uint)sizeof(xfs_dir2_ino4_t));
1088 * Won't fit as shortform, convert to block then do replace.
1090 if (newsize > XFS_IFORK_DSIZE(dp)) {
1091 error = xfs_dir2_sf_to_block(args);
1092 if (error) {
1093 return error;
1095 return xfs_dir2_block_replace(args);
1098 * Still fits, convert to 8-byte now.
1100 xfs_dir2_sf_toino8(args);
1101 i8elevated = 1;
1102 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
1103 } else
1104 i8elevated = 0;
1105 #endif
1106 ASSERT(args->namelen != 1 || args->name[0] != '.');
1108 * Replace ..'s entry.
1110 if (args->namelen == 2 &&
1111 args->name[0] == '.' && args->name[1] == '.') {
1112 #if XFS_BIG_INUMS || defined(DEBUG)
1113 ino = xfs_dir2_sf_get_parent_ino(sfp);
1114 ASSERT(args->inumber != ino);
1115 #endif
1116 xfs_dir2_sf_put_parent_ino(sfp, args->inumber);
1119 * Normal entry, look for the name.
1121 else {
1122 for (i = 0, sfep = xfs_dir2_sf_firstentry(sfp);
1123 i < sfp->count;
1124 i++, sfep = xfs_dir2_sf_nextentry(sfp, sfep)) {
1125 if (xfs_da_compname(args, sfep->name, sfep->namelen) ==
1126 XFS_CMP_EXACT) {
1127 #if XFS_BIG_INUMS || defined(DEBUG)
1128 ino = xfs_dir2_sfe_get_ino(sfp, sfep);
1129 ASSERT(args->inumber != ino);
1130 #endif
1131 xfs_dir2_sfe_put_ino(sfp, sfep, args->inumber);
1132 break;
1136 * Didn't find it.
1138 if (i == sfp->count) {
1139 ASSERT(args->op_flags & XFS_DA_OP_OKNOENT);
1140 #if XFS_BIG_INUMS
1141 if (i8elevated)
1142 xfs_dir2_sf_toino4(args);
1143 #endif
1144 return XFS_ERROR(ENOENT);
1147 #if XFS_BIG_INUMS
1149 * See if the old number was large, the new number is small.
1151 if (ino > XFS_DIR2_MAX_SHORT_INUM &&
1152 args->inumber <= XFS_DIR2_MAX_SHORT_INUM) {
1154 * And the old count was one, so need to convert to small.
1156 if (sfp->i8count == 1)
1157 xfs_dir2_sf_toino4(args);
1158 else
1159 sfp->i8count--;
1162 * See if the old number was small, the new number is large.
1164 if (ino <= XFS_DIR2_MAX_SHORT_INUM &&
1165 args->inumber > XFS_DIR2_MAX_SHORT_INUM) {
1167 * add to the i8count unless we just converted to 8-byte
1168 * inodes (which does an implied i8count = 1)
1170 ASSERT(sfp->i8count != 0);
1171 if (!i8elevated)
1172 sfp->i8count++;
1174 #endif
1175 xfs_dir2_sf_check(args);
1176 xfs_trans_log_inode(args->trans, dp, XFS_ILOG_DDATA);
1177 return 0;
1180 #if XFS_BIG_INUMS
1182 * Convert from 8-byte inode numbers to 4-byte inode numbers.
1183 * The last 8-byte inode number is gone, but the count is still 1.
1185 static void
1186 xfs_dir2_sf_toino4(
1187 xfs_da_args_t *args) /* operation arguments */
1189 char *buf; /* old dir's buffer */
1190 xfs_inode_t *dp; /* incore directory inode */
1191 int i; /* entry index */
1192 int newsize; /* new inode size */
1193 xfs_dir2_sf_entry_t *oldsfep; /* old sf entry */
1194 xfs_dir2_sf_hdr_t *oldsfp; /* old sf directory */
1195 int oldsize; /* old inode size */
1196 xfs_dir2_sf_entry_t *sfep; /* new sf entry */
1197 xfs_dir2_sf_hdr_t *sfp; /* new sf directory */
1199 trace_xfs_dir2_sf_toino4(args);
1201 dp = args->dp;
1204 * Copy the old directory to the buffer.
1205 * Then nuke it from the inode, and add the new buffer to the inode.
1206 * Don't want xfs_idata_realloc copying the data here.
1208 oldsize = dp->i_df.if_bytes;
1209 buf = kmem_alloc(oldsize, KM_SLEEP);
1210 oldsfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
1211 ASSERT(oldsfp->i8count == 1);
1212 memcpy(buf, oldsfp, oldsize);
1214 * Compute the new inode size.
1216 newsize =
1217 oldsize -
1218 (oldsfp->count + 1) *
1219 ((uint)sizeof(xfs_dir2_ino8_t) - (uint)sizeof(xfs_dir2_ino4_t));
1220 xfs_idata_realloc(dp, -oldsize, XFS_DATA_FORK);
1221 xfs_idata_realloc(dp, newsize, XFS_DATA_FORK);
1223 * Reset our pointers, the data has moved.
1225 oldsfp = (xfs_dir2_sf_hdr_t *)buf;
1226 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
1228 * Fill in the new header.
1230 sfp->count = oldsfp->count;
1231 sfp->i8count = 0;
1232 xfs_dir2_sf_put_parent_ino(sfp, xfs_dir2_sf_get_parent_ino(oldsfp));
1234 * Copy the entries field by field.
1236 for (i = 0, sfep = xfs_dir2_sf_firstentry(sfp),
1237 oldsfep = xfs_dir2_sf_firstentry(oldsfp);
1238 i < sfp->count;
1239 i++, sfep = xfs_dir2_sf_nextentry(sfp, sfep),
1240 oldsfep = xfs_dir2_sf_nextentry(oldsfp, oldsfep)) {
1241 sfep->namelen = oldsfep->namelen;
1242 sfep->offset = oldsfep->offset;
1243 memcpy(sfep->name, oldsfep->name, sfep->namelen);
1244 xfs_dir2_sfe_put_ino(sfp, sfep,
1245 xfs_dir2_sfe_get_ino(oldsfp, oldsfep));
1248 * Clean up the inode.
1250 kmem_free(buf);
1251 dp->i_d.di_size = newsize;
1252 xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE | XFS_ILOG_DDATA);
1256 * Convert from 4-byte inode numbers to 8-byte inode numbers.
1257 * The new 8-byte inode number is not there yet, we leave with the
1258 * count 1 but no corresponding entry.
1260 static void
1261 xfs_dir2_sf_toino8(
1262 xfs_da_args_t *args) /* operation arguments */
1264 char *buf; /* old dir's buffer */
1265 xfs_inode_t *dp; /* incore directory inode */
1266 int i; /* entry index */
1267 int newsize; /* new inode size */
1268 xfs_dir2_sf_entry_t *oldsfep; /* old sf entry */
1269 xfs_dir2_sf_hdr_t *oldsfp; /* old sf directory */
1270 int oldsize; /* old inode size */
1271 xfs_dir2_sf_entry_t *sfep; /* new sf entry */
1272 xfs_dir2_sf_hdr_t *sfp; /* new sf directory */
1274 trace_xfs_dir2_sf_toino8(args);
1276 dp = args->dp;
1279 * Copy the old directory to the buffer.
1280 * Then nuke it from the inode, and add the new buffer to the inode.
1281 * Don't want xfs_idata_realloc copying the data here.
1283 oldsize = dp->i_df.if_bytes;
1284 buf = kmem_alloc(oldsize, KM_SLEEP);
1285 oldsfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
1286 ASSERT(oldsfp->i8count == 0);
1287 memcpy(buf, oldsfp, oldsize);
1289 * Compute the new inode size.
1291 newsize =
1292 oldsize +
1293 (oldsfp->count + 1) *
1294 ((uint)sizeof(xfs_dir2_ino8_t) - (uint)sizeof(xfs_dir2_ino4_t));
1295 xfs_idata_realloc(dp, -oldsize, XFS_DATA_FORK);
1296 xfs_idata_realloc(dp, newsize, XFS_DATA_FORK);
1298 * Reset our pointers, the data has moved.
1300 oldsfp = (xfs_dir2_sf_hdr_t *)buf;
1301 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
1303 * Fill in the new header.
1305 sfp->count = oldsfp->count;
1306 sfp->i8count = 1;
1307 xfs_dir2_sf_put_parent_ino(sfp, xfs_dir2_sf_get_parent_ino(oldsfp));
1309 * Copy the entries field by field.
1311 for (i = 0, sfep = xfs_dir2_sf_firstentry(sfp),
1312 oldsfep = xfs_dir2_sf_firstentry(oldsfp);
1313 i < sfp->count;
1314 i++, sfep = xfs_dir2_sf_nextentry(sfp, sfep),
1315 oldsfep = xfs_dir2_sf_nextentry(oldsfp, oldsfep)) {
1316 sfep->namelen = oldsfep->namelen;
1317 sfep->offset = oldsfep->offset;
1318 memcpy(sfep->name, oldsfep->name, sfep->namelen);
1319 xfs_dir2_sfe_put_ino(sfp, sfep,
1320 xfs_dir2_sfe_get_ino(oldsfp, oldsfep));
1323 * Clean up the inode.
1325 kmem_free(buf);
1326 dp->i_d.di_size = newsize;
1327 xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE | XFS_ILOG_DDATA);
1329 #endif /* XFS_BIG_INUMS */