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