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