]> git.ipfire.org Git - thirdparty/xfsprogs-dev.git/blob - libxfs/xfs_dir2_sf.c
xfs: verify inline directory data forks
[thirdparty/xfsprogs-dev.git] / libxfs / xfs_dir2_sf.c
1 /*
2 * Copyright (c) 2000-2003,2005 Silicon Graphics, Inc.
3 * All Rights Reserved.
4 *
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.
8 *
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.
13 *
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
17 */
18 #include "libxfs_priv.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_dir2.h"
29 #include "xfs_dir2_priv.h"
30 #include "xfs_trace.h"
31
32 /*
33 * Prototypes for internal functions.
34 */
35 static void xfs_dir2_sf_addname_easy(xfs_da_args_t *args,
36 xfs_dir2_sf_entry_t *sfep,
37 xfs_dir2_data_aoff_t offset,
38 int new_isize);
39 static void xfs_dir2_sf_addname_hard(xfs_da_args_t *args, int objchange,
40 int new_isize);
41 static int xfs_dir2_sf_addname_pick(xfs_da_args_t *args, int objchange,
42 xfs_dir2_sf_entry_t **sfepp,
43 xfs_dir2_data_aoff_t *offsetp);
44 #ifdef DEBUG
45 static void xfs_dir2_sf_check(xfs_da_args_t *args);
46 #else
47 #define xfs_dir2_sf_check(args)
48 #endif /* DEBUG */
49
50 static void xfs_dir2_sf_toino4(xfs_da_args_t *args);
51 static void xfs_dir2_sf_toino8(xfs_da_args_t *args);
52
53 /*
54 * Given a block directory (dp/block), calculate its size as a shortform (sf)
55 * directory and a header for the sf directory, if it will fit it the
56 * space currently present in the inode. If it won't fit, the output
57 * size is too big (but not accurate).
58 */
59 int /* size for sf form */
60 xfs_dir2_block_sfsize(
61 xfs_inode_t *dp, /* incore inode pointer */
62 xfs_dir2_data_hdr_t *hdr, /* block directory data */
63 xfs_dir2_sf_hdr_t *sfhp) /* output: header for sf form */
64 {
65 xfs_dir2_dataptr_t addr; /* data entry address */
66 xfs_dir2_leaf_entry_t *blp; /* leaf area of the block */
67 xfs_dir2_block_tail_t *btp; /* tail area of the block */
68 int count; /* shortform entry count */
69 xfs_dir2_data_entry_t *dep; /* data entry in the block */
70 int i; /* block entry index */
71 int i8count; /* count of big-inode entries */
72 int isdot; /* entry is "." */
73 int isdotdot; /* entry is ".." */
74 xfs_mount_t *mp; /* mount structure pointer */
75 int namelen; /* total name bytes */
76 xfs_ino_t parent = 0; /* parent inode number */
77 int size=0; /* total computed size */
78 int has_ftype;
79 struct xfs_da_geometry *geo;
80
81 mp = dp->i_mount;
82 geo = mp->m_dir_geo;
83
84 /*
85 * if there is a filetype field, add the extra byte to the namelen
86 * for each entry that we see.
87 */
88 has_ftype = xfs_sb_version_hasftype(&mp->m_sb) ? 1 : 0;
89
90 count = i8count = namelen = 0;
91 btp = xfs_dir2_block_tail_p(geo, hdr);
92 blp = xfs_dir2_block_leaf_p(btp);
93
94 /*
95 * Iterate over the block's data entries by using the leaf pointers.
96 */
97 for (i = 0; i < be32_to_cpu(btp->count); i++) {
98 if ((addr = be32_to_cpu(blp[i].address)) == XFS_DIR2_NULL_DATAPTR)
99 continue;
100 /*
101 * Calculate the pointer to the entry at hand.
102 */
103 dep = (xfs_dir2_data_entry_t *)((char *)hdr +
104 xfs_dir2_dataptr_to_off(geo, addr));
105 /*
106 * Detect . and .., so we can special-case them.
107 * . is not included in sf directories.
108 * .. is included by just the parent inode number.
109 */
110 isdot = dep->namelen == 1 && dep->name[0] == '.';
111 isdotdot =
112 dep->namelen == 2 &&
113 dep->name[0] == '.' && dep->name[1] == '.';
114
115 if (!isdot)
116 i8count += be64_to_cpu(dep->inumber) > XFS_DIR2_MAX_SHORT_INUM;
117
118 /* take into account the file type field */
119 if (!isdot && !isdotdot) {
120 count++;
121 namelen += dep->namelen + has_ftype;
122 } else if (isdotdot)
123 parent = be64_to_cpu(dep->inumber);
124 /*
125 * Calculate the new size, see if we should give up yet.
126 */
127 size = xfs_dir2_sf_hdr_size(i8count) + /* header */
128 count * 3 * sizeof(__u8) + /* namelen + offset */
129 namelen + /* name */
130 (i8count ? /* inumber */
131 count * XFS_INO64_SIZE :
132 count * XFS_INO32_SIZE);
133 if (size > XFS_IFORK_DSIZE(dp))
134 return size; /* size value is a failure */
135 }
136 /*
137 * Create the output header, if it worked.
138 */
139 sfhp->count = count;
140 sfhp->i8count = i8count;
141 dp->d_ops->sf_put_parent_ino(sfhp, parent);
142 return size;
143 }
144
145 /*
146 * Convert a block format directory to shortform.
147 * Caller has already checked that it will fit, and built us a header.
148 */
149 int /* error */
150 xfs_dir2_block_to_sf(
151 xfs_da_args_t *args, /* operation arguments */
152 struct xfs_buf *bp,
153 int size, /* shortform directory size */
154 xfs_dir2_sf_hdr_t *sfhp) /* shortform directory hdr */
155 {
156 xfs_dir2_data_hdr_t *hdr; /* block header */
157 xfs_dir2_block_tail_t *btp; /* block tail pointer */
158 xfs_dir2_data_entry_t *dep; /* data entry pointer */
159 xfs_inode_t *dp; /* incore directory inode */
160 xfs_dir2_data_unused_t *dup; /* unused data pointer */
161 char *endptr; /* end of data entries */
162 int error; /* error return value */
163 int logflags; /* inode logging flags */
164 xfs_mount_t *mp; /* filesystem mount point */
165 char *ptr; /* current data pointer */
166 xfs_dir2_sf_entry_t *sfep; /* shortform entry */
167 xfs_dir2_sf_hdr_t *sfp; /* shortform directory header */
168 xfs_dir2_sf_hdr_t *dst; /* temporary data buffer */
169
170 trace_xfs_dir2_block_to_sf(args);
171
172 dp = args->dp;
173 mp = dp->i_mount;
174
175 /*
176 * allocate a temporary destination buffer the size of the inode
177 * to format the data into. Once we have formatted the data, we
178 * can free the block and copy the formatted data into the inode literal
179 * area.
180 */
181 dst = kmem_alloc(mp->m_sb.sb_inodesize, KM_SLEEP);
182 hdr = bp->b_addr;
183
184 /*
185 * Copy the header into the newly allocate local space.
186 */
187 sfp = (xfs_dir2_sf_hdr_t *)dst;
188 memcpy(sfp, sfhp, xfs_dir2_sf_hdr_size(sfhp->i8count));
189
190 /*
191 * Set up to loop over the block's entries.
192 */
193 btp = xfs_dir2_block_tail_p(args->geo, hdr);
194 ptr = (char *)dp->d_ops->data_entry_p(hdr);
195 endptr = (char *)xfs_dir2_block_leaf_p(btp);
196 sfep = xfs_dir2_sf_firstentry(sfp);
197 /*
198 * Loop over the active and unused entries.
199 * Stop when we reach the leaf/tail portion of the block.
200 */
201 while (ptr < endptr) {
202 /*
203 * If it's unused, just skip over it.
204 */
205 dup = (xfs_dir2_data_unused_t *)ptr;
206 if (be16_to_cpu(dup->freetag) == XFS_DIR2_DATA_FREE_TAG) {
207 ptr += be16_to_cpu(dup->length);
208 continue;
209 }
210 dep = (xfs_dir2_data_entry_t *)ptr;
211 /*
212 * Skip .
213 */
214 if (dep->namelen == 1 && dep->name[0] == '.')
215 ASSERT(be64_to_cpu(dep->inumber) == dp->i_ino);
216 /*
217 * Skip .., but make sure the inode number is right.
218 */
219 else if (dep->namelen == 2 &&
220 dep->name[0] == '.' && dep->name[1] == '.')
221 ASSERT(be64_to_cpu(dep->inumber) ==
222 dp->d_ops->sf_get_parent_ino(sfp));
223 /*
224 * Normal entry, copy it into shortform.
225 */
226 else {
227 sfep->namelen = dep->namelen;
228 xfs_dir2_sf_put_offset(sfep,
229 (xfs_dir2_data_aoff_t)
230 ((char *)dep - (char *)hdr));
231 memcpy(sfep->name, dep->name, dep->namelen);
232 dp->d_ops->sf_put_ino(sfp, sfep,
233 be64_to_cpu(dep->inumber));
234 dp->d_ops->sf_put_ftype(sfep,
235 dp->d_ops->data_get_ftype(dep));
236
237 sfep = dp->d_ops->sf_nextentry(sfp, sfep);
238 }
239 ptr += dp->d_ops->data_entsize(dep->namelen);
240 }
241 ASSERT((char *)sfep - (char *)sfp == size);
242
243 /* now we are done with the block, we can shrink the inode */
244 logflags = XFS_ILOG_CORE;
245 error = xfs_dir2_shrink_inode(args, args->geo->datablk, bp);
246 if (error) {
247 ASSERT(error != -ENOSPC);
248 goto out;
249 }
250
251 /*
252 * The buffer is now unconditionally gone, whether
253 * xfs_dir2_shrink_inode worked or not.
254 *
255 * Convert the inode to local format and copy the data in.
256 */
257 ASSERT(dp->i_df.if_bytes == 0);
258 xfs_init_local_fork(dp, XFS_DATA_FORK, dst, size);
259 dp->i_d.di_format = XFS_DINODE_FMT_LOCAL;
260 dp->i_d.di_size = size;
261
262 logflags |= XFS_ILOG_DDATA;
263 xfs_dir2_sf_check(args);
264 out:
265 xfs_trans_log_inode(args->trans, dp, logflags);
266 kmem_free(dst);
267 return error;
268 }
269
270 /*
271 * Add a name to a shortform directory.
272 * There are two algorithms, "easy" and "hard" which we decide on
273 * before changing anything.
274 * Convert to block form if necessary, if the new entry won't fit.
275 */
276 int /* error */
277 xfs_dir2_sf_addname(
278 xfs_da_args_t *args) /* operation arguments */
279 {
280 xfs_inode_t *dp; /* incore directory inode */
281 int error; /* error return value */
282 int incr_isize; /* total change in size */
283 int new_isize; /* di_size after adding name */
284 int objchange; /* changing to 8-byte inodes */
285 xfs_dir2_data_aoff_t offset = 0; /* offset for new entry */
286 int pick; /* which algorithm to use */
287 xfs_dir2_sf_hdr_t *sfp; /* shortform structure */
288 xfs_dir2_sf_entry_t *sfep = NULL; /* shortform entry */
289
290 trace_xfs_dir2_sf_addname(args);
291
292 ASSERT(xfs_dir2_sf_lookup(args) == -ENOENT);
293 dp = args->dp;
294 ASSERT(dp->i_df.if_flags & XFS_IFINLINE);
295 /*
296 * Make sure the shortform value has some of its header.
297 */
298 if (dp->i_d.di_size < offsetof(xfs_dir2_sf_hdr_t, parent)) {
299 ASSERT(XFS_FORCED_SHUTDOWN(dp->i_mount));
300 return -EIO;
301 }
302 ASSERT(dp->i_df.if_bytes == dp->i_d.di_size);
303 ASSERT(dp->i_df.if_u1.if_data != NULL);
304 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
305 ASSERT(dp->i_d.di_size >= xfs_dir2_sf_hdr_size(sfp->i8count));
306 /*
307 * Compute entry (and change in) size.
308 */
309 incr_isize = dp->d_ops->sf_entsize(sfp, args->namelen);
310 objchange = 0;
311
312 /*
313 * Do we have to change to 8 byte inodes?
314 */
315 if (args->inumber > XFS_DIR2_MAX_SHORT_INUM && sfp->i8count == 0) {
316 /*
317 * Yes, adjust the inode size. old count + (parent + new)
318 */
319 incr_isize += (sfp->count + 2) * XFS_INO64_DIFF;
320 objchange = 1;
321 }
322
323 new_isize = (int)dp->i_d.di_size + incr_isize;
324 /*
325 * Won't fit as shortform any more (due to size),
326 * or the pick routine says it won't (due to offset values).
327 */
328 if (new_isize > XFS_IFORK_DSIZE(dp) ||
329 (pick =
330 xfs_dir2_sf_addname_pick(args, objchange, &sfep, &offset)) == 0) {
331 /*
332 * Just checking or no space reservation, it doesn't fit.
333 */
334 if ((args->op_flags & XFS_DA_OP_JUSTCHECK) || args->total == 0)
335 return -ENOSPC;
336 /*
337 * Convert to block form then add the name.
338 */
339 error = xfs_dir2_sf_to_block(args);
340 if (error)
341 return error;
342 return xfs_dir2_block_addname(args);
343 }
344 /*
345 * Just checking, it fits.
346 */
347 if (args->op_flags & XFS_DA_OP_JUSTCHECK)
348 return 0;
349 /*
350 * Do it the easy way - just add it at the end.
351 */
352 if (pick == 1)
353 xfs_dir2_sf_addname_easy(args, sfep, offset, new_isize);
354 /*
355 * Do it the hard way - look for a place to insert the new entry.
356 * Convert to 8 byte inode numbers first if necessary.
357 */
358 else {
359 ASSERT(pick == 2);
360 if (objchange)
361 xfs_dir2_sf_toino8(args);
362 xfs_dir2_sf_addname_hard(args, objchange, new_isize);
363 }
364 xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE | XFS_ILOG_DDATA);
365 return 0;
366 }
367
368 /*
369 * Add the new entry the "easy" way.
370 * This is copying the old directory and adding the new entry at the end.
371 * Since it's sorted by "offset" we need room after the last offset
372 * that's already there, and then room to convert to a block directory.
373 * This is already checked by the pick routine.
374 */
375 static void
376 xfs_dir2_sf_addname_easy(
377 xfs_da_args_t *args, /* operation arguments */
378 xfs_dir2_sf_entry_t *sfep, /* pointer to new entry */
379 xfs_dir2_data_aoff_t offset, /* offset to use for new ent */
380 int new_isize) /* new directory size */
381 {
382 int byteoff; /* byte offset in sf dir */
383 xfs_inode_t *dp; /* incore directory inode */
384 xfs_dir2_sf_hdr_t *sfp; /* shortform structure */
385
386 dp = args->dp;
387
388 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
389 byteoff = (int)((char *)sfep - (char *)sfp);
390 /*
391 * Grow the in-inode space.
392 */
393 xfs_idata_realloc(dp, dp->d_ops->sf_entsize(sfp, args->namelen),
394 XFS_DATA_FORK);
395 /*
396 * Need to set up again due to realloc of the inode data.
397 */
398 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
399 sfep = (xfs_dir2_sf_entry_t *)((char *)sfp + byteoff);
400 /*
401 * Fill in the new entry.
402 */
403 sfep->namelen = args->namelen;
404 xfs_dir2_sf_put_offset(sfep, offset);
405 memcpy(sfep->name, args->name, sfep->namelen);
406 dp->d_ops->sf_put_ino(sfp, sfep, args->inumber);
407 dp->d_ops->sf_put_ftype(sfep, args->filetype);
408
409 /*
410 * Update the header and inode.
411 */
412 sfp->count++;
413 if (args->inumber > XFS_DIR2_MAX_SHORT_INUM)
414 sfp->i8count++;
415 dp->i_d.di_size = new_isize;
416 xfs_dir2_sf_check(args);
417 }
418
419 /*
420 * Add the new entry the "hard" way.
421 * The caller has already converted to 8 byte inode numbers if necessary,
422 * in which case we need to leave the i8count at 1.
423 * Find a hole that the new entry will fit into, and copy
424 * the first part of the entries, the new entry, and the last part of
425 * the entries.
426 */
427 /* ARGSUSED */
428 static void
429 xfs_dir2_sf_addname_hard(
430 xfs_da_args_t *args, /* operation arguments */
431 int objchange, /* changing inode number size */
432 int new_isize) /* new directory size */
433 {
434 int add_datasize; /* data size need for new ent */
435 char *buf; /* buffer for old */
436 xfs_inode_t *dp; /* incore directory inode */
437 int eof; /* reached end of old dir */
438 int nbytes; /* temp for byte copies */
439 xfs_dir2_data_aoff_t new_offset; /* next offset value */
440 xfs_dir2_data_aoff_t offset; /* current offset value */
441 int old_isize; /* previous di_size */
442 xfs_dir2_sf_entry_t *oldsfep; /* entry in original dir */
443 xfs_dir2_sf_hdr_t *oldsfp; /* original shortform dir */
444 xfs_dir2_sf_entry_t *sfep; /* entry in new dir */
445 xfs_dir2_sf_hdr_t *sfp; /* new shortform dir */
446
447 /*
448 * Copy the old directory to the stack buffer.
449 */
450 dp = args->dp;
451
452 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
453 old_isize = (int)dp->i_d.di_size;
454 buf = kmem_alloc(old_isize, KM_SLEEP);
455 oldsfp = (xfs_dir2_sf_hdr_t *)buf;
456 memcpy(oldsfp, sfp, old_isize);
457 /*
458 * Loop over the old directory finding the place we're going
459 * to insert the new entry.
460 * If it's going to end up at the end then oldsfep will point there.
461 */
462 for (offset = dp->d_ops->data_first_offset,
463 oldsfep = xfs_dir2_sf_firstentry(oldsfp),
464 add_datasize = dp->d_ops->data_entsize(args->namelen),
465 eof = (char *)oldsfep == &buf[old_isize];
466 !eof;
467 offset = new_offset + dp->d_ops->data_entsize(oldsfep->namelen),
468 oldsfep = dp->d_ops->sf_nextentry(oldsfp, oldsfep),
469 eof = (char *)oldsfep == &buf[old_isize]) {
470 new_offset = xfs_dir2_sf_get_offset(oldsfep);
471 if (offset + add_datasize <= new_offset)
472 break;
473 }
474 /*
475 * Get rid of the old directory, then allocate space for
476 * the new one. We do this so xfs_idata_realloc won't copy
477 * the data.
478 */
479 xfs_idata_realloc(dp, -old_isize, XFS_DATA_FORK);
480 xfs_idata_realloc(dp, new_isize, XFS_DATA_FORK);
481 /*
482 * Reset the pointer since the buffer was reallocated.
483 */
484 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
485 /*
486 * Copy the first part of the directory, including the header.
487 */
488 nbytes = (int)((char *)oldsfep - (char *)oldsfp);
489 memcpy(sfp, oldsfp, nbytes);
490 sfep = (xfs_dir2_sf_entry_t *)((char *)sfp + nbytes);
491 /*
492 * Fill in the new entry, and update the header counts.
493 */
494 sfep->namelen = args->namelen;
495 xfs_dir2_sf_put_offset(sfep, offset);
496 memcpy(sfep->name, args->name, sfep->namelen);
497 dp->d_ops->sf_put_ino(sfp, sfep, args->inumber);
498 dp->d_ops->sf_put_ftype(sfep, args->filetype);
499 sfp->count++;
500 if (args->inumber > XFS_DIR2_MAX_SHORT_INUM && !objchange)
501 sfp->i8count++;
502 /*
503 * If there's more left to copy, do that.
504 */
505 if (!eof) {
506 sfep = dp->d_ops->sf_nextentry(sfp, sfep);
507 memcpy(sfep, oldsfep, old_isize - nbytes);
508 }
509 kmem_free(buf);
510 dp->i_d.di_size = new_isize;
511 xfs_dir2_sf_check(args);
512 }
513
514 /*
515 * Decide if the new entry will fit at all.
516 * If it will fit, pick between adding the new entry to the end (easy)
517 * or somewhere else (hard).
518 * Return 0 (won't fit), 1 (easy), 2 (hard).
519 */
520 /*ARGSUSED*/
521 static int /* pick result */
522 xfs_dir2_sf_addname_pick(
523 xfs_da_args_t *args, /* operation arguments */
524 int objchange, /* inode # size changes */
525 xfs_dir2_sf_entry_t **sfepp, /* out(1): new entry ptr */
526 xfs_dir2_data_aoff_t *offsetp) /* out(1): new offset */
527 {
528 xfs_inode_t *dp; /* incore directory inode */
529 int holefit; /* found hole it will fit in */
530 int i; /* entry number */
531 xfs_dir2_data_aoff_t offset; /* data block offset */
532 xfs_dir2_sf_entry_t *sfep; /* shortform entry */
533 xfs_dir2_sf_hdr_t *sfp; /* shortform structure */
534 int size; /* entry's data size */
535 int used; /* data bytes used */
536
537 dp = args->dp;
538
539 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
540 size = dp->d_ops->data_entsize(args->namelen);
541 offset = dp->d_ops->data_first_offset;
542 sfep = xfs_dir2_sf_firstentry(sfp);
543 holefit = 0;
544 /*
545 * Loop over sf entries.
546 * Keep track of data offset and whether we've seen a place
547 * to insert the new entry.
548 */
549 for (i = 0; i < sfp->count; i++) {
550 if (!holefit)
551 holefit = offset + size <= xfs_dir2_sf_get_offset(sfep);
552 offset = xfs_dir2_sf_get_offset(sfep) +
553 dp->d_ops->data_entsize(sfep->namelen);
554 sfep = dp->d_ops->sf_nextentry(sfp, sfep);
555 }
556 /*
557 * Calculate data bytes used excluding the new entry, if this
558 * was a data block (block form directory).
559 */
560 used = offset +
561 (sfp->count + 3) * (uint)sizeof(xfs_dir2_leaf_entry_t) +
562 (uint)sizeof(xfs_dir2_block_tail_t);
563 /*
564 * If it won't fit in a block form then we can't insert it,
565 * we'll go back, convert to block, then try the insert and convert
566 * to leaf.
567 */
568 if (used + (holefit ? 0 : size) > args->geo->blksize)
569 return 0;
570 /*
571 * If changing the inode number size, do it the hard way.
572 */
573 if (objchange)
574 return 2;
575 /*
576 * If it won't fit at the end then do it the hard way (use the hole).
577 */
578 if (used + size > args->geo->blksize)
579 return 2;
580 /*
581 * Do it the easy way.
582 */
583 *sfepp = sfep;
584 *offsetp = offset;
585 return 1;
586 }
587
588 #ifdef DEBUG
589 /*
590 * Check consistency of shortform directory, assert if bad.
591 */
592 static void
593 xfs_dir2_sf_check(
594 xfs_da_args_t *args) /* operation arguments */
595 {
596 xfs_inode_t *dp; /* incore directory inode */
597 int i; /* entry number */
598 int i8count; /* number of big inode#s */
599 xfs_ino_t ino; /* entry inode number */
600 int offset; /* data offset */
601 xfs_dir2_sf_entry_t *sfep; /* shortform dir entry */
602 xfs_dir2_sf_hdr_t *sfp; /* shortform structure */
603
604 dp = args->dp;
605
606 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
607 offset = dp->d_ops->data_first_offset;
608 ino = dp->d_ops->sf_get_parent_ino(sfp);
609 i8count = ino > XFS_DIR2_MAX_SHORT_INUM;
610
611 for (i = 0, sfep = xfs_dir2_sf_firstentry(sfp);
612 i < sfp->count;
613 i++, sfep = dp->d_ops->sf_nextentry(sfp, sfep)) {
614 ASSERT(xfs_dir2_sf_get_offset(sfep) >= offset);
615 ino = dp->d_ops->sf_get_ino(sfp, sfep);
616 i8count += ino > XFS_DIR2_MAX_SHORT_INUM;
617 offset =
618 xfs_dir2_sf_get_offset(sfep) +
619 dp->d_ops->data_entsize(sfep->namelen);
620 ASSERT(dp->d_ops->sf_get_ftype(sfep) < XFS_DIR3_FT_MAX);
621 }
622 ASSERT(i8count == sfp->i8count);
623 ASSERT((char *)sfep - (char *)sfp == dp->i_d.di_size);
624 ASSERT(offset +
625 (sfp->count + 2) * (uint)sizeof(xfs_dir2_leaf_entry_t) +
626 (uint)sizeof(xfs_dir2_block_tail_t) <= args->geo->blksize);
627 }
628 #endif /* DEBUG */
629
630 /* Verify the consistency of an inline directory. */
631 int
632 xfs_dir2_sf_verify(
633 struct xfs_mount *mp,
634 struct xfs_dir2_sf_hdr *sfp,
635 int size)
636 {
637 struct xfs_dir2_sf_entry *sfep;
638 struct xfs_dir2_sf_entry *next_sfep;
639 char *endp;
640 const struct xfs_dir_ops *dops;
641 xfs_ino_t ino;
642 int i;
643 int i8count;
644 int offset;
645 __uint8_t filetype;
646
647 dops = xfs_dir_get_ops(mp, NULL);
648
649 /*
650 * Give up if the directory is way too short.
651 */
652 XFS_WANT_CORRUPTED_RETURN(mp, size >
653 offsetof(struct xfs_dir2_sf_hdr, parent));
654 XFS_WANT_CORRUPTED_RETURN(mp, size >=
655 xfs_dir2_sf_hdr_size(sfp->i8count));
656
657 endp = (char *)sfp + size;
658
659 /* Check .. entry */
660 ino = dops->sf_get_parent_ino(sfp);
661 i8count = ino > XFS_DIR2_MAX_SHORT_INUM;
662 XFS_WANT_CORRUPTED_RETURN(mp, !xfs_dir_ino_validate(mp, ino));
663 offset = dops->data_first_offset;
664
665 /* Check all reported entries */
666 sfep = xfs_dir2_sf_firstentry(sfp);
667 for (i = 0; i < sfp->count; i++) {
668 /*
669 * struct xfs_dir2_sf_entry has a variable length.
670 * Check the fixed-offset parts of the structure are
671 * within the data buffer.
672 */
673 XFS_WANT_CORRUPTED_RETURN(mp,
674 ((char *)sfep + sizeof(*sfep)) < endp);
675
676 /* Don't allow names with known bad length. */
677 XFS_WANT_CORRUPTED_RETURN(mp, sfep->namelen > 0);
678 XFS_WANT_CORRUPTED_RETURN(mp, sfep->namelen < MAXNAMELEN);
679
680 /*
681 * Check that the variable-length part of the structure is
682 * within the data buffer. The next entry starts after the
683 * name component, so nextentry is an acceptable test.
684 */
685 next_sfep = dops->sf_nextentry(sfp, sfep);
686 XFS_WANT_CORRUPTED_RETURN(mp, endp >= (char *)next_sfep);
687
688 /* Check that the offsets always increase. */
689 XFS_WANT_CORRUPTED_RETURN(mp,
690 xfs_dir2_sf_get_offset(sfep) >= offset);
691
692 /* Check the inode number. */
693 ino = dops->sf_get_ino(sfp, sfep);
694 i8count += ino > XFS_DIR2_MAX_SHORT_INUM;
695 XFS_WANT_CORRUPTED_RETURN(mp, !xfs_dir_ino_validate(mp, ino));
696
697 /* Check the file type. */
698 filetype = dops->sf_get_ftype(sfep);
699 XFS_WANT_CORRUPTED_RETURN(mp, filetype < XFS_DIR3_FT_MAX);
700
701 offset = xfs_dir2_sf_get_offset(sfep) +
702 dops->data_entsize(sfep->namelen);
703
704 sfep = next_sfep;
705 }
706 XFS_WANT_CORRUPTED_RETURN(mp, i8count == sfp->i8count);
707 XFS_WANT_CORRUPTED_RETURN(mp, (void *)sfep == (void *)endp);
708
709 /* Make sure this whole thing ought to be in local format. */
710 XFS_WANT_CORRUPTED_RETURN(mp, offset +
711 (sfp->count + 2) * (uint)sizeof(xfs_dir2_leaf_entry_t) +
712 (uint)sizeof(xfs_dir2_block_tail_t) <= mp->m_dir_geo->blksize);
713
714 return 0;
715 }
716
717 /*
718 * Create a new (shortform) directory.
719 */
720 int /* error, always 0 */
721 xfs_dir2_sf_create(
722 xfs_da_args_t *args, /* operation arguments */
723 xfs_ino_t pino) /* parent inode number */
724 {
725 xfs_inode_t *dp; /* incore directory inode */
726 int i8count; /* parent inode is an 8-byte number */
727 xfs_dir2_sf_hdr_t *sfp; /* shortform structure */
728 int size; /* directory size */
729
730 trace_xfs_dir2_sf_create(args);
731
732 dp = args->dp;
733
734 ASSERT(dp != NULL);
735 ASSERT(dp->i_d.di_size == 0);
736 /*
737 * If it's currently a zero-length extent file,
738 * convert it to local format.
739 */
740 if (dp->i_d.di_format == XFS_DINODE_FMT_EXTENTS) {
741 dp->i_df.if_flags &= ~XFS_IFEXTENTS; /* just in case */
742 dp->i_d.di_format = XFS_DINODE_FMT_LOCAL;
743 xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE);
744 dp->i_df.if_flags |= XFS_IFINLINE;
745 }
746 ASSERT(dp->i_df.if_flags & XFS_IFINLINE);
747 ASSERT(dp->i_df.if_bytes == 0);
748 i8count = pino > XFS_DIR2_MAX_SHORT_INUM;
749 size = xfs_dir2_sf_hdr_size(i8count);
750 /*
751 * Make a buffer for the data.
752 */
753 xfs_idata_realloc(dp, size, XFS_DATA_FORK);
754 /*
755 * Fill in the header,
756 */
757 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
758 sfp->i8count = i8count;
759 /*
760 * Now can put in the inode number, since i8count is set.
761 */
762 dp->d_ops->sf_put_parent_ino(sfp, pino);
763 sfp->count = 0;
764 dp->i_d.di_size = size;
765 xfs_dir2_sf_check(args);
766 xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE | XFS_ILOG_DDATA);
767 return 0;
768 }
769
770 /*
771 * Lookup an entry in a shortform directory.
772 * Returns EEXIST if found, ENOENT if not found.
773 */
774 int /* error */
775 xfs_dir2_sf_lookup(
776 xfs_da_args_t *args) /* operation arguments */
777 {
778 xfs_inode_t *dp; /* incore directory inode */
779 int i; /* entry index */
780 int error;
781 xfs_dir2_sf_entry_t *sfep; /* shortform directory entry */
782 xfs_dir2_sf_hdr_t *sfp; /* shortform structure */
783 enum xfs_dacmp cmp; /* comparison result */
784 xfs_dir2_sf_entry_t *ci_sfep; /* case-insens. entry */
785
786 trace_xfs_dir2_sf_lookup(args);
787
788 xfs_dir2_sf_check(args);
789 dp = args->dp;
790
791 ASSERT(dp->i_df.if_flags & XFS_IFINLINE);
792 /*
793 * Bail out if the directory is way too short.
794 */
795 if (dp->i_d.di_size < offsetof(xfs_dir2_sf_hdr_t, parent)) {
796 ASSERT(XFS_FORCED_SHUTDOWN(dp->i_mount));
797 return -EIO;
798 }
799 ASSERT(dp->i_df.if_bytes == dp->i_d.di_size);
800 ASSERT(dp->i_df.if_u1.if_data != NULL);
801 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
802 ASSERT(dp->i_d.di_size >= xfs_dir2_sf_hdr_size(sfp->i8count));
803 /*
804 * Special case for .
805 */
806 if (args->namelen == 1 && args->name[0] == '.') {
807 args->inumber = dp->i_ino;
808 args->cmpresult = XFS_CMP_EXACT;
809 args->filetype = XFS_DIR3_FT_DIR;
810 return -EEXIST;
811 }
812 /*
813 * Special case for ..
814 */
815 if (args->namelen == 2 &&
816 args->name[0] == '.' && args->name[1] == '.') {
817 args->inumber = dp->d_ops->sf_get_parent_ino(sfp);
818 args->cmpresult = XFS_CMP_EXACT;
819 args->filetype = XFS_DIR3_FT_DIR;
820 return -EEXIST;
821 }
822 /*
823 * Loop over all the entries trying to match ours.
824 */
825 ci_sfep = NULL;
826 for (i = 0, sfep = xfs_dir2_sf_firstentry(sfp); i < sfp->count;
827 i++, sfep = dp->d_ops->sf_nextentry(sfp, sfep)) {
828 /*
829 * Compare name and if it's an exact match, return the inode
830 * number. If it's the first case-insensitive match, store the
831 * inode number and continue looking for an exact match.
832 */
833 cmp = dp->i_mount->m_dirnameops->compname(args, sfep->name,
834 sfep->namelen);
835 if (cmp != XFS_CMP_DIFFERENT && cmp != args->cmpresult) {
836 args->cmpresult = cmp;
837 args->inumber = dp->d_ops->sf_get_ino(sfp, sfep);
838 args->filetype = dp->d_ops->sf_get_ftype(sfep);
839 if (cmp == XFS_CMP_EXACT)
840 return -EEXIST;
841 ci_sfep = sfep;
842 }
843 }
844 ASSERT(args->op_flags & XFS_DA_OP_OKNOENT);
845 /*
846 * Here, we can only be doing a lookup (not a rename or replace).
847 * If a case-insensitive match was not found, return -ENOENT.
848 */
849 if (!ci_sfep)
850 return -ENOENT;
851 /* otherwise process the CI match as required by the caller */
852 error = xfs_dir_cilookup_result(args, ci_sfep->name, ci_sfep->namelen);
853 return error;
854 }
855
856 /*
857 * Remove an entry from a shortform directory.
858 */
859 int /* error */
860 xfs_dir2_sf_removename(
861 xfs_da_args_t *args)
862 {
863 int byteoff; /* offset of removed entry */
864 xfs_inode_t *dp; /* incore directory inode */
865 int entsize; /* this entry's size */
866 int i; /* shortform entry index */
867 int newsize; /* new inode size */
868 int oldsize; /* old inode size */
869 xfs_dir2_sf_entry_t *sfep; /* shortform directory entry */
870 xfs_dir2_sf_hdr_t *sfp; /* shortform structure */
871
872 trace_xfs_dir2_sf_removename(args);
873
874 dp = args->dp;
875
876 ASSERT(dp->i_df.if_flags & XFS_IFINLINE);
877 oldsize = (int)dp->i_d.di_size;
878 /*
879 * Bail out if the directory is way too short.
880 */
881 if (oldsize < offsetof(xfs_dir2_sf_hdr_t, parent)) {
882 ASSERT(XFS_FORCED_SHUTDOWN(dp->i_mount));
883 return -EIO;
884 }
885 ASSERT(dp->i_df.if_bytes == oldsize);
886 ASSERT(dp->i_df.if_u1.if_data != NULL);
887 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
888 ASSERT(oldsize >= xfs_dir2_sf_hdr_size(sfp->i8count));
889 /*
890 * Loop over the old directory entries.
891 * Find the one we're deleting.
892 */
893 for (i = 0, sfep = xfs_dir2_sf_firstentry(sfp); i < sfp->count;
894 i++, sfep = dp->d_ops->sf_nextentry(sfp, sfep)) {
895 if (xfs_da_compname(args, sfep->name, sfep->namelen) ==
896 XFS_CMP_EXACT) {
897 ASSERT(dp->d_ops->sf_get_ino(sfp, sfep) ==
898 args->inumber);
899 break;
900 }
901 }
902 /*
903 * Didn't find it.
904 */
905 if (i == sfp->count)
906 return -ENOENT;
907 /*
908 * Calculate sizes.
909 */
910 byteoff = (int)((char *)sfep - (char *)sfp);
911 entsize = dp->d_ops->sf_entsize(sfp, args->namelen);
912 newsize = oldsize - entsize;
913 /*
914 * Copy the part if any after the removed entry, sliding it down.
915 */
916 if (byteoff + entsize < oldsize)
917 memmove((char *)sfp + byteoff, (char *)sfp + byteoff + entsize,
918 oldsize - (byteoff + entsize));
919 /*
920 * Fix up the header and file size.
921 */
922 sfp->count--;
923 dp->i_d.di_size = newsize;
924 /*
925 * Reallocate, making it smaller.
926 */
927 xfs_idata_realloc(dp, newsize - oldsize, XFS_DATA_FORK);
928 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
929 /*
930 * Are we changing inode number size?
931 */
932 if (args->inumber > XFS_DIR2_MAX_SHORT_INUM) {
933 if (sfp->i8count == 1)
934 xfs_dir2_sf_toino4(args);
935 else
936 sfp->i8count--;
937 }
938 xfs_dir2_sf_check(args);
939 xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE | XFS_ILOG_DDATA);
940 return 0;
941 }
942
943 /*
944 * Replace the inode number of an entry in a shortform directory.
945 */
946 int /* error */
947 xfs_dir2_sf_replace(
948 xfs_da_args_t *args) /* operation arguments */
949 {
950 xfs_inode_t *dp; /* incore directory inode */
951 int i; /* entry index */
952 xfs_ino_t ino=0; /* entry old inode number */
953 int i8elevated; /* sf_toino8 set i8count=1 */
954 xfs_dir2_sf_entry_t *sfep; /* shortform directory entry */
955 xfs_dir2_sf_hdr_t *sfp; /* shortform structure */
956
957 trace_xfs_dir2_sf_replace(args);
958
959 dp = args->dp;
960
961 ASSERT(dp->i_df.if_flags & XFS_IFINLINE);
962 /*
963 * Bail out if the shortform directory is way too small.
964 */
965 if (dp->i_d.di_size < offsetof(xfs_dir2_sf_hdr_t, parent)) {
966 ASSERT(XFS_FORCED_SHUTDOWN(dp->i_mount));
967 return -EIO;
968 }
969 ASSERT(dp->i_df.if_bytes == dp->i_d.di_size);
970 ASSERT(dp->i_df.if_u1.if_data != NULL);
971 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
972 ASSERT(dp->i_d.di_size >= xfs_dir2_sf_hdr_size(sfp->i8count));
973
974 /*
975 * New inode number is large, and need to convert to 8-byte inodes.
976 */
977 if (args->inumber > XFS_DIR2_MAX_SHORT_INUM && sfp->i8count == 0) {
978 int error; /* error return value */
979 int newsize; /* new inode size */
980
981 newsize = dp->i_df.if_bytes + (sfp->count + 1) * XFS_INO64_DIFF;
982 /*
983 * Won't fit as shortform, convert to block then do replace.
984 */
985 if (newsize > XFS_IFORK_DSIZE(dp)) {
986 error = xfs_dir2_sf_to_block(args);
987 if (error) {
988 return error;
989 }
990 return xfs_dir2_block_replace(args);
991 }
992 /*
993 * Still fits, convert to 8-byte now.
994 */
995 xfs_dir2_sf_toino8(args);
996 i8elevated = 1;
997 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
998 } else
999 i8elevated = 0;
1000
1001 ASSERT(args->namelen != 1 || args->name[0] != '.');
1002 /*
1003 * Replace ..'s entry.
1004 */
1005 if (args->namelen == 2 &&
1006 args->name[0] == '.' && args->name[1] == '.') {
1007 ino = dp->d_ops->sf_get_parent_ino(sfp);
1008 ASSERT(args->inumber != ino);
1009 dp->d_ops->sf_put_parent_ino(sfp, args->inumber);
1010 }
1011 /*
1012 * Normal entry, look for the name.
1013 */
1014 else {
1015 for (i = 0, sfep = xfs_dir2_sf_firstentry(sfp); i < sfp->count;
1016 i++, sfep = dp->d_ops->sf_nextentry(sfp, sfep)) {
1017 if (xfs_da_compname(args, sfep->name, sfep->namelen) ==
1018 XFS_CMP_EXACT) {
1019 ino = dp->d_ops->sf_get_ino(sfp, sfep);
1020 ASSERT(args->inumber != ino);
1021 dp->d_ops->sf_put_ino(sfp, sfep, args->inumber);
1022 dp->d_ops->sf_put_ftype(sfep, args->filetype);
1023 break;
1024 }
1025 }
1026 /*
1027 * Didn't find it.
1028 */
1029 if (i == sfp->count) {
1030 ASSERT(args->op_flags & XFS_DA_OP_OKNOENT);
1031 if (i8elevated)
1032 xfs_dir2_sf_toino4(args);
1033 return -ENOENT;
1034 }
1035 }
1036 /*
1037 * See if the old number was large, the new number is small.
1038 */
1039 if (ino > XFS_DIR2_MAX_SHORT_INUM &&
1040 args->inumber <= XFS_DIR2_MAX_SHORT_INUM) {
1041 /*
1042 * And the old count was one, so need to convert to small.
1043 */
1044 if (sfp->i8count == 1)
1045 xfs_dir2_sf_toino4(args);
1046 else
1047 sfp->i8count--;
1048 }
1049 /*
1050 * See if the old number was small, the new number is large.
1051 */
1052 if (ino <= XFS_DIR2_MAX_SHORT_INUM &&
1053 args->inumber > XFS_DIR2_MAX_SHORT_INUM) {
1054 /*
1055 * add to the i8count unless we just converted to 8-byte
1056 * inodes (which does an implied i8count = 1)
1057 */
1058 ASSERT(sfp->i8count != 0);
1059 if (!i8elevated)
1060 sfp->i8count++;
1061 }
1062 xfs_dir2_sf_check(args);
1063 xfs_trans_log_inode(args->trans, dp, XFS_ILOG_DDATA);
1064 return 0;
1065 }
1066
1067 /*
1068 * Convert from 8-byte inode numbers to 4-byte inode numbers.
1069 * The last 8-byte inode number is gone, but the count is still 1.
1070 */
1071 static void
1072 xfs_dir2_sf_toino4(
1073 xfs_da_args_t *args) /* operation arguments */
1074 {
1075 char *buf; /* old dir's buffer */
1076 xfs_inode_t *dp; /* incore directory inode */
1077 int i; /* entry index */
1078 int newsize; /* new inode size */
1079 xfs_dir2_sf_entry_t *oldsfep; /* old sf entry */
1080 xfs_dir2_sf_hdr_t *oldsfp; /* old sf directory */
1081 int oldsize; /* old inode size */
1082 xfs_dir2_sf_entry_t *sfep; /* new sf entry */
1083 xfs_dir2_sf_hdr_t *sfp; /* new sf directory */
1084
1085 trace_xfs_dir2_sf_toino4(args);
1086
1087 dp = args->dp;
1088
1089 /*
1090 * Copy the old directory to the buffer.
1091 * Then nuke it from the inode, and add the new buffer to the inode.
1092 * Don't want xfs_idata_realloc copying the data here.
1093 */
1094 oldsize = dp->i_df.if_bytes;
1095 buf = kmem_alloc(oldsize, KM_SLEEP);
1096 oldsfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
1097 ASSERT(oldsfp->i8count == 1);
1098 memcpy(buf, oldsfp, oldsize);
1099 /*
1100 * Compute the new inode size.
1101 */
1102 newsize = oldsize - (oldsfp->count + 1) * XFS_INO64_DIFF;
1103 xfs_idata_realloc(dp, -oldsize, XFS_DATA_FORK);
1104 xfs_idata_realloc(dp, newsize, XFS_DATA_FORK);
1105 /*
1106 * Reset our pointers, the data has moved.
1107 */
1108 oldsfp = (xfs_dir2_sf_hdr_t *)buf;
1109 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
1110 /*
1111 * Fill in the new header.
1112 */
1113 sfp->count = oldsfp->count;
1114 sfp->i8count = 0;
1115 dp->d_ops->sf_put_parent_ino(sfp, dp->d_ops->sf_get_parent_ino(oldsfp));
1116 /*
1117 * Copy the entries field by field.
1118 */
1119 for (i = 0, sfep = xfs_dir2_sf_firstentry(sfp),
1120 oldsfep = xfs_dir2_sf_firstentry(oldsfp);
1121 i < sfp->count;
1122 i++, sfep = dp->d_ops->sf_nextentry(sfp, sfep),
1123 oldsfep = dp->d_ops->sf_nextentry(oldsfp, oldsfep)) {
1124 sfep->namelen = oldsfep->namelen;
1125 memcpy(sfep->offset, oldsfep->offset, sizeof(sfep->offset));
1126 memcpy(sfep->name, oldsfep->name, sfep->namelen);
1127 dp->d_ops->sf_put_ino(sfp, sfep,
1128 dp->d_ops->sf_get_ino(oldsfp, oldsfep));
1129 dp->d_ops->sf_put_ftype(sfep, dp->d_ops->sf_get_ftype(oldsfep));
1130 }
1131 /*
1132 * Clean up the inode.
1133 */
1134 kmem_free(buf);
1135 dp->i_d.di_size = newsize;
1136 xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE | XFS_ILOG_DDATA);
1137 }
1138
1139 /*
1140 * Convert existing entries from 4-byte inode numbers to 8-byte inode numbers.
1141 * The new entry w/ an 8-byte inode number is not there yet; we leave with
1142 * i8count set to 1, but no corresponding 8-byte entry.
1143 */
1144 static void
1145 xfs_dir2_sf_toino8(
1146 xfs_da_args_t *args) /* operation arguments */
1147 {
1148 char *buf; /* old dir's buffer */
1149 xfs_inode_t *dp; /* incore directory inode */
1150 int i; /* entry index */
1151 int newsize; /* new inode size */
1152 xfs_dir2_sf_entry_t *oldsfep; /* old sf entry */
1153 xfs_dir2_sf_hdr_t *oldsfp; /* old sf directory */
1154 int oldsize; /* old inode size */
1155 xfs_dir2_sf_entry_t *sfep; /* new sf entry */
1156 xfs_dir2_sf_hdr_t *sfp; /* new sf directory */
1157
1158 trace_xfs_dir2_sf_toino8(args);
1159
1160 dp = args->dp;
1161
1162 /*
1163 * Copy the old directory to the buffer.
1164 * Then nuke it from the inode, and add the new buffer to the inode.
1165 * Don't want xfs_idata_realloc copying the data here.
1166 */
1167 oldsize = dp->i_df.if_bytes;
1168 buf = kmem_alloc(oldsize, KM_SLEEP);
1169 oldsfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
1170 ASSERT(oldsfp->i8count == 0);
1171 memcpy(buf, oldsfp, oldsize);
1172 /*
1173 * Compute the new inode size (nb: entry count + 1 for parent)
1174 */
1175 newsize = oldsize + (oldsfp->count + 1) * XFS_INO64_DIFF;
1176 xfs_idata_realloc(dp, -oldsize, XFS_DATA_FORK);
1177 xfs_idata_realloc(dp, newsize, XFS_DATA_FORK);
1178 /*
1179 * Reset our pointers, the data has moved.
1180 */
1181 oldsfp = (xfs_dir2_sf_hdr_t *)buf;
1182 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
1183 /*
1184 * Fill in the new header.
1185 */
1186 sfp->count = oldsfp->count;
1187 sfp->i8count = 1;
1188 dp->d_ops->sf_put_parent_ino(sfp, dp->d_ops->sf_get_parent_ino(oldsfp));
1189 /*
1190 * Copy the entries field by field.
1191 */
1192 for (i = 0, sfep = xfs_dir2_sf_firstentry(sfp),
1193 oldsfep = xfs_dir2_sf_firstentry(oldsfp);
1194 i < sfp->count;
1195 i++, sfep = dp->d_ops->sf_nextentry(sfp, sfep),
1196 oldsfep = dp->d_ops->sf_nextentry(oldsfp, oldsfep)) {
1197 sfep->namelen = oldsfep->namelen;
1198 memcpy(sfep->offset, oldsfep->offset, sizeof(sfep->offset));
1199 memcpy(sfep->name, oldsfep->name, sfep->namelen);
1200 dp->d_ops->sf_put_ino(sfp, sfep,
1201 dp->d_ops->sf_get_ino(oldsfp, oldsfep));
1202 dp->d_ops->sf_put_ftype(sfep, dp->d_ops->sf_get_ftype(oldsfep));
1203 }
1204 /*
1205 * Clean up the inode.
1206 */
1207 kmem_free(buf);
1208 dp->i_d.di_size = newsize;
1209 xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE | XFS_ILOG_DDATA);
1210 }