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