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