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