blob: c67d735729051c40e91783d6f062e99384069b0a [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Nathan Scott7b718762005-11-02 14:58:39 +11002 * Copyright (c) 2000-2003,2005 Silicon Graphics, Inc.
3 * All Rights Reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
Nathan Scott7b718762005-11-02 14:58:39 +11005 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * published by the Free Software Foundation.
8 *
Nathan Scott7b718762005-11-02 14:58:39 +11009 * 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.
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 *
Nathan Scott7b718762005-11-02 14:58:39 +110014 * 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
Linus Torvalds1da177e2005-04-16 15:20:36 -070017 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include "xfs.h"
Nathan Scotta844f452005-11-02 14:38:42 +110019#include "xfs_fs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include "xfs_types.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include "xfs_log.h"
Nathan Scotta844f452005-11-02 14:38:42 +110022#include "xfs_inum.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include "xfs_trans.h"
24#include "xfs_sb.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include "xfs_dir2.h"
26#include "xfs_dmapi.h"
27#include "xfs_mount.h"
Nathan Scotta844f452005-11-02 14:38:42 +110028#include "xfs_da_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include "xfs_bmap_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include "xfs_dir2_sf.h"
Nathan Scotta844f452005-11-02 14:38:42 +110031#include "xfs_attr_sf.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include "xfs_dinode.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include "xfs_inode.h"
Nathan Scotta844f452005-11-02 14:38:42 +110034#include "xfs_inode_item.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include "xfs_error.h"
36#include "xfs_dir2_data.h"
37#include "xfs_dir2_leaf.h"
38#include "xfs_dir2_block.h"
39#include "xfs_dir2_trace.h"
40
41/*
42 * Prototypes for internal functions.
43 */
44static void xfs_dir2_sf_addname_easy(xfs_da_args_t *args,
45 xfs_dir2_sf_entry_t *sfep,
46 xfs_dir2_data_aoff_t offset,
47 int new_isize);
48static void xfs_dir2_sf_addname_hard(xfs_da_args_t *args, int objchange,
49 int new_isize);
50static int xfs_dir2_sf_addname_pick(xfs_da_args_t *args, int objchange,
51 xfs_dir2_sf_entry_t **sfepp,
52 xfs_dir2_data_aoff_t *offsetp);
53#ifdef DEBUG
54static void xfs_dir2_sf_check(xfs_da_args_t *args);
55#else
56#define xfs_dir2_sf_check(args)
57#endif /* DEBUG */
58#if XFS_BIG_INUMS
59static void xfs_dir2_sf_toino4(xfs_da_args_t *args);
60static void xfs_dir2_sf_toino8(xfs_da_args_t *args);
61#endif /* XFS_BIG_INUMS */
62
63/*
64 * Given a block directory (dp/block), calculate its size as a shortform (sf)
65 * directory and a header for the sf directory, if it will fit it the
66 * space currently present in the inode. If it won't fit, the output
67 * size is too big (but not accurate).
68 */
69int /* size for sf form */
70xfs_dir2_block_sfsize(
71 xfs_inode_t *dp, /* incore inode pointer */
72 xfs_dir2_block_t *block, /* block directory data */
73 xfs_dir2_sf_hdr_t *sfhp) /* output: header for sf form */
74{
75 xfs_dir2_dataptr_t addr; /* data entry address */
76 xfs_dir2_leaf_entry_t *blp; /* leaf area of the block */
77 xfs_dir2_block_tail_t *btp; /* tail area of the block */
78 int count; /* shortform entry count */
79 xfs_dir2_data_entry_t *dep; /* data entry in the block */
80 int i; /* block entry index */
81 int i8count; /* count of big-inode entries */
82 int isdot; /* entry is "." */
83 int isdotdot; /* entry is ".." */
84 xfs_mount_t *mp; /* mount structure pointer */
85 int namelen; /* total name bytes */
Christoph Hellwig5bde1ba92005-11-02 15:06:18 +110086 xfs_ino_t parent = 0; /* parent inode number */
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 int size=0; /* total computed size */
88
89 mp = dp->i_mount;
90
91 count = i8count = namelen = 0;
Christoph Hellwigbbaaf532007-06-28 16:43:50 +100092 btp = xfs_dir2_block_tail_p(mp, block);
93 blp = xfs_dir2_block_leaf_p(btp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070094
95 /*
96 * Iterate over the block's data entries by using the leaf pointers.
97 */
Nathan Scotte922fff2006-03-17 17:27:56 +110098 for (i = 0; i < be32_to_cpu(btp->count); i++) {
Nathan Scott3c1f9c12006-03-17 17:28:18 +110099 if ((addr = be32_to_cpu(blp[i].address)) == XFS_DIR2_NULL_DATAPTR)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 continue;
101 /*
102 * Calculate the pointer to the entry at hand.
103 */
104 dep = (xfs_dir2_data_entry_t *)
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000105 ((char *)block + xfs_dir2_dataptr_to_off(mp, addr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 /*
107 * Detect . and .., so we can special-case them.
108 * . is not included in sf directories.
109 * .. is included by just the parent inode number.
110 */
111 isdot = dep->namelen == 1 && dep->name[0] == '.';
112 isdotdot =
113 dep->namelen == 2 &&
114 dep->name[0] == '.' && dep->name[1] == '.';
115#if XFS_BIG_INUMS
116 if (!isdot)
Christoph Hellwigff9901c2006-06-09 14:48:37 +1000117 i8count += be64_to_cpu(dep->inumber) > XFS_DIR2_MAX_SHORT_INUM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118#endif
119 if (!isdot && !isdotdot) {
120 count++;
121 namelen += dep->namelen;
122 } else if (isdotdot)
Christoph Hellwigff9901c2006-06-09 14:48:37 +1000123 parent = be64_to_cpu(dep->inumber);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 /*
125 * Calculate the new size, see if we should give up yet.
126 */
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000127 size = xfs_dir2_sf_hdr_size(i8count) + /* header */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 count + /* namelen */
129 count * (uint)sizeof(xfs_dir2_sf_off_t) + /* offset */
130 namelen + /* name */
131 (i8count ? /* inumber */
132 (uint)sizeof(xfs_dir2_ino8_t) * count :
133 (uint)sizeof(xfs_dir2_ino4_t) * count);
134 if (size > XFS_IFORK_DSIZE(dp))
135 return size; /* size value is a failure */
136 }
137 /*
138 * Create the output header, if it worked.
139 */
140 sfhp->count = count;
141 sfhp->i8count = i8count;
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000142 xfs_dir2_sf_put_inumber((xfs_dir2_sf_t *)sfhp, &parent, &sfhp->parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 return size;
144}
145
146/*
147 * Convert a block format directory to shortform.
148 * Caller has already checked that it will fit, and built us a header.
149 */
150int /* error */
151xfs_dir2_block_to_sf(
152 xfs_da_args_t *args, /* operation arguments */
153 xfs_dabuf_t *bp, /* block buffer */
154 int size, /* shortform directory size */
155 xfs_dir2_sf_hdr_t *sfhp) /* shortform directory hdr */
156{
157 xfs_dir2_block_t *block; /* block structure */
158 xfs_dir2_block_tail_t *btp; /* block tail pointer */
159 xfs_dir2_data_entry_t *dep; /* data entry pointer */
160 xfs_inode_t *dp; /* incore directory inode */
161 xfs_dir2_data_unused_t *dup; /* unused data pointer */
162 char *endptr; /* end of data entries */
163 int error; /* error return value */
164 int logflags; /* inode logging flags */
165 xfs_mount_t *mp; /* filesystem mount point */
166 char *ptr; /* current data pointer */
167 xfs_dir2_sf_entry_t *sfep; /* shortform entry */
168 xfs_dir2_sf_t *sfp; /* shortform structure */
169 xfs_ino_t temp;
170
171 xfs_dir2_trace_args_sb("block_to_sf", args, size, bp);
172 dp = args->dp;
173 mp = dp->i_mount;
174
175 /*
176 * Make a copy of the block data, so we can shrink the inode
177 * and add local data.
178 */
179 block = kmem_alloc(mp->m_dirblksize, KM_SLEEP);
180 memcpy(block, bp->data, mp->m_dirblksize);
181 logflags = XFS_ILOG_CORE;
182 if ((error = xfs_dir2_shrink_inode(args, mp->m_dirdatablk, bp))) {
183 ASSERT(error != ENOSPC);
184 goto out;
185 }
186 /*
187 * The buffer is now unconditionally gone, whether
188 * xfs_dir2_shrink_inode worked or not.
189 *
190 * Convert the inode to local format.
191 */
192 dp->i_df.if_flags &= ~XFS_IFEXTENTS;
193 dp->i_df.if_flags |= XFS_IFINLINE;
194 dp->i_d.di_format = XFS_DINODE_FMT_LOCAL;
195 ASSERT(dp->i_df.if_bytes == 0);
196 xfs_idata_realloc(dp, size, XFS_DATA_FORK);
197 logflags |= XFS_ILOG_DDATA;
198 /*
199 * Copy the header into the newly allocate local space.
200 */
201 sfp = (xfs_dir2_sf_t *)dp->i_df.if_u1.if_data;
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000202 memcpy(sfp, sfhp, xfs_dir2_sf_hdr_size(sfhp->i8count));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 dp->i_d.di_size = size;
204 /*
205 * Set up to loop over the block's entries.
206 */
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000207 btp = xfs_dir2_block_tail_p(mp, block);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 ptr = (char *)block->u;
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000209 endptr = (char *)xfs_dir2_block_leaf_p(btp);
210 sfep = xfs_dir2_sf_firstentry(sfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 /*
212 * Loop over the active and unused entries.
213 * Stop when we reach the leaf/tail portion of the block.
214 */
215 while (ptr < endptr) {
216 /*
217 * If it's unused, just skip over it.
218 */
219 dup = (xfs_dir2_data_unused_t *)ptr;
Nathan Scottad354eb2006-03-17 17:27:37 +1100220 if (be16_to_cpu(dup->freetag) == XFS_DIR2_DATA_FREE_TAG) {
221 ptr += be16_to_cpu(dup->length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 continue;
223 }
224 dep = (xfs_dir2_data_entry_t *)ptr;
225 /*
226 * Skip .
227 */
228 if (dep->namelen == 1 && dep->name[0] == '.')
Christoph Hellwigff9901c2006-06-09 14:48:37 +1000229 ASSERT(be64_to_cpu(dep->inumber) == dp->i_ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 /*
231 * Skip .., but make sure the inode number is right.
232 */
233 else if (dep->namelen == 2 &&
234 dep->name[0] == '.' && dep->name[1] == '.')
Christoph Hellwigff9901c2006-06-09 14:48:37 +1000235 ASSERT(be64_to_cpu(dep->inumber) ==
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000236 xfs_dir2_sf_get_inumber(sfp, &sfp->hdr.parent));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 /*
238 * Normal entry, copy it into shortform.
239 */
240 else {
241 sfep->namelen = dep->namelen;
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000242 xfs_dir2_sf_put_offset(sfep,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 (xfs_dir2_data_aoff_t)
244 ((char *)dep - (char *)block));
245 memcpy(sfep->name, dep->name, dep->namelen);
Christoph Hellwigff9901c2006-06-09 14:48:37 +1000246 temp = be64_to_cpu(dep->inumber);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000247 xfs_dir2_sf_put_inumber(sfp, &temp,
248 xfs_dir2_sf_inumberp(sfep));
249 sfep = xfs_dir2_sf_nextentry(sfp, sfep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 }
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000251 ptr += xfs_dir2_data_entsize(dep->namelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 }
253 ASSERT((char *)sfep - (char *)sfp == size);
254 xfs_dir2_sf_check(args);
255out:
256 xfs_trans_log_inode(args->trans, dp, logflags);
257 kmem_free(block, mp->m_dirblksize);
258 return error;
259}
260
261/*
262 * Add a name to a shortform directory.
263 * There are two algorithms, "easy" and "hard" which we decide on
264 * before changing anything.
265 * Convert to block form if necessary, if the new entry won't fit.
266 */
267int /* error */
268xfs_dir2_sf_addname(
269 xfs_da_args_t *args) /* operation arguments */
270{
271 int add_entsize; /* size of the new entry */
272 xfs_inode_t *dp; /* incore directory inode */
273 int error; /* error return value */
274 int incr_isize; /* total change in size */
275 int new_isize; /* di_size after adding name */
276 int objchange; /* changing to 8-byte inodes */
Christoph Hellwig5bde1ba92005-11-02 15:06:18 +1100277 xfs_dir2_data_aoff_t offset = 0; /* offset for new entry */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 int old_isize; /* di_size before adding name */
279 int pick; /* which algorithm to use */
280 xfs_dir2_sf_t *sfp; /* shortform structure */
Christoph Hellwig5bde1ba92005-11-02 15:06:18 +1100281 xfs_dir2_sf_entry_t *sfep = NULL; /* shortform entry */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282
283 xfs_dir2_trace_args("sf_addname", args);
284 ASSERT(xfs_dir2_sf_lookup(args) == ENOENT);
285 dp = args->dp;
286 ASSERT(dp->i_df.if_flags & XFS_IFINLINE);
287 /*
288 * Make sure the shortform value has some of its header.
289 */
290 if (dp->i_d.di_size < offsetof(xfs_dir2_sf_hdr_t, parent)) {
291 ASSERT(XFS_FORCED_SHUTDOWN(dp->i_mount));
292 return XFS_ERROR(EIO);
293 }
294 ASSERT(dp->i_df.if_bytes == dp->i_d.di_size);
295 ASSERT(dp->i_df.if_u1.if_data != NULL);
296 sfp = (xfs_dir2_sf_t *)dp->i_df.if_u1.if_data;
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000297 ASSERT(dp->i_d.di_size >= xfs_dir2_sf_hdr_size(sfp->hdr.i8count));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 /*
299 * Compute entry (and change in) size.
300 */
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000301 add_entsize = xfs_dir2_sf_entsize_byname(sfp, args->namelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 incr_isize = add_entsize;
303 objchange = 0;
304#if XFS_BIG_INUMS
305 /*
306 * Do we have to change to 8 byte inodes?
307 */
308 if (args->inumber > XFS_DIR2_MAX_SHORT_INUM && sfp->hdr.i8count == 0) {
309 /*
310 * Yes, adjust the entry size and the total size.
311 */
312 add_entsize +=
313 (uint)sizeof(xfs_dir2_ino8_t) -
314 (uint)sizeof(xfs_dir2_ino4_t);
315 incr_isize +=
316 (sfp->hdr.count + 2) *
317 ((uint)sizeof(xfs_dir2_ino8_t) -
318 (uint)sizeof(xfs_dir2_ino4_t));
319 objchange = 1;
320 }
321#endif
322 old_isize = (int)dp->i_d.di_size;
323 new_isize = old_isize + incr_isize;
324 /*
325 * Won't fit as shortform any more (due to size),
326 * or the pick routine says it won't (due to offset values).
327 */
328 if (new_isize > XFS_IFORK_DSIZE(dp) ||
329 (pick =
330 xfs_dir2_sf_addname_pick(args, objchange, &sfep, &offset)) == 0) {
331 /*
332 * Just checking or no space reservation, it doesn't fit.
333 */
334 if (args->justcheck || args->total == 0)
335 return XFS_ERROR(ENOSPC);
336 /*
337 * Convert to block form then add the name.
338 */
339 error = xfs_dir2_sf_to_block(args);
340 if (error)
341 return error;
342 return xfs_dir2_block_addname(args);
343 }
344 /*
345 * Just checking, it fits.
346 */
347 if (args->justcheck)
348 return 0;
349 /*
350 * Do it the easy way - just add it at the end.
351 */
352 if (pick == 1)
353 xfs_dir2_sf_addname_easy(args, sfep, offset, new_isize);
354 /*
355 * Do it the hard way - look for a place to insert the new entry.
356 * Convert to 8 byte inode numbers first if necessary.
357 */
358 else {
359 ASSERT(pick == 2);
360#if XFS_BIG_INUMS
361 if (objchange)
362 xfs_dir2_sf_toino8(args);
363#endif
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 */
386 xfs_dir2_sf_t *sfp; /* shortform structure */
387
388 dp = args->dp;
389
390 sfp = (xfs_dir2_sf_t *)dp->i_df.if_u1.if_data;
391 byteoff = (int)((char *)sfep - (char *)sfp);
392 /*
393 * Grow the in-inode space.
394 */
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000395 xfs_idata_realloc(dp, xfs_dir2_sf_entsize_byname(sfp, args->namelen),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 XFS_DATA_FORK);
397 /*
398 * Need to set up again due to realloc of the inode data.
399 */
400 sfp = (xfs_dir2_sf_t *)dp->i_df.if_u1.if_data;
401 sfep = (xfs_dir2_sf_entry_t *)((char *)sfp + byteoff);
402 /*
403 * Fill in the new entry.
404 */
405 sfep->namelen = args->namelen;
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000406 xfs_dir2_sf_put_offset(sfep, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 memcpy(sfep->name, args->name, sfep->namelen);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000408 xfs_dir2_sf_put_inumber(sfp, &args->inumber,
409 xfs_dir2_sf_inumberp(sfep));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 /*
411 * Update the header and inode.
412 */
413 sfp->hdr.count++;
414#if XFS_BIG_INUMS
415 if (args->inumber > XFS_DIR2_MAX_SHORT_INUM)
416 sfp->hdr.i8count++;
417#endif
418 dp->i_d.di_size = new_isize;
419 xfs_dir2_sf_check(args);
420}
421
422/*
423 * Add the new entry the "hard" way.
424 * The caller has already converted to 8 byte inode numbers if necessary,
425 * in which case we need to leave the i8count at 1.
426 * Find a hole that the new entry will fit into, and copy
427 * the first part of the entries, the new entry, and the last part of
428 * the entries.
429 */
430/* ARGSUSED */
431static void
432xfs_dir2_sf_addname_hard(
433 xfs_da_args_t *args, /* operation arguments */
434 int objchange, /* changing inode number size */
435 int new_isize) /* new directory size */
436{
437 int add_datasize; /* data size need for new ent */
438 char *buf; /* buffer for old */
439 xfs_inode_t *dp; /* incore directory inode */
440 int eof; /* reached end of old dir */
441 int nbytes; /* temp for byte copies */
442 xfs_dir2_data_aoff_t new_offset; /* next offset value */
443 xfs_dir2_data_aoff_t offset; /* current offset value */
444 int old_isize; /* previous di_size */
445 xfs_dir2_sf_entry_t *oldsfep; /* entry in original dir */
446 xfs_dir2_sf_t *oldsfp; /* original shortform dir */
447 xfs_dir2_sf_entry_t *sfep; /* entry in new dir */
448 xfs_dir2_sf_t *sfp; /* new shortform dir */
449
450 /*
451 * Copy the old directory to the stack buffer.
452 */
453 dp = args->dp;
454
455 sfp = (xfs_dir2_sf_t *)dp->i_df.if_u1.if_data;
456 old_isize = (int)dp->i_d.di_size;
457 buf = kmem_alloc(old_isize, KM_SLEEP);
458 oldsfp = (xfs_dir2_sf_t *)buf;
459 memcpy(oldsfp, sfp, old_isize);
460 /*
461 * Loop over the old directory finding the place we're going
462 * to insert the new entry.
463 * If it's going to end up at the end then oldsfep will point there.
464 */
465 for (offset = XFS_DIR2_DATA_FIRST_OFFSET,
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000466 oldsfep = xfs_dir2_sf_firstentry(oldsfp),
467 add_datasize = xfs_dir2_data_entsize(args->namelen),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 eof = (char *)oldsfep == &buf[old_isize];
469 !eof;
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000470 offset = new_offset + xfs_dir2_data_entsize(oldsfep->namelen),
471 oldsfep = xfs_dir2_sf_nextentry(oldsfp, oldsfep),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 eof = (char *)oldsfep == &buf[old_isize]) {
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000473 new_offset = xfs_dir2_sf_get_offset(oldsfep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 if (offset + add_datasize <= new_offset)
475 break;
476 }
477 /*
478 * Get rid of the old directory, then allocate space for
479 * the new one. We do this so xfs_idata_realloc won't copy
480 * the data.
481 */
482 xfs_idata_realloc(dp, -old_isize, XFS_DATA_FORK);
483 xfs_idata_realloc(dp, new_isize, XFS_DATA_FORK);
484 /*
485 * Reset the pointer since the buffer was reallocated.
486 */
487 sfp = (xfs_dir2_sf_t *)dp->i_df.if_u1.if_data;
488 /*
489 * Copy the first part of the directory, including the header.
490 */
491 nbytes = (int)((char *)oldsfep - (char *)oldsfp);
492 memcpy(sfp, oldsfp, nbytes);
493 sfep = (xfs_dir2_sf_entry_t *)((char *)sfp + nbytes);
494 /*
495 * Fill in the new entry, and update the header counts.
496 */
497 sfep->namelen = args->namelen;
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000498 xfs_dir2_sf_put_offset(sfep, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 memcpy(sfep->name, args->name, sfep->namelen);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000500 xfs_dir2_sf_put_inumber(sfp, &args->inumber,
501 xfs_dir2_sf_inumberp(sfep));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 sfp->hdr.count++;
503#if XFS_BIG_INUMS
504 if (args->inumber > XFS_DIR2_MAX_SHORT_INUM && !objchange)
505 sfp->hdr.i8count++;
506#endif
507 /*
508 * If there's more left to copy, do that.
509 */
510 if (!eof) {
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000511 sfep = xfs_dir2_sf_nextentry(sfp, sfep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 memcpy(sfep, oldsfep, old_isize - nbytes);
513 }
514 kmem_free(buf, old_isize);
515 dp->i_d.di_size = new_isize;
516 xfs_dir2_sf_check(args);
517}
518
519/*
520 * Decide if the new entry will fit at all.
521 * If it will fit, pick between adding the new entry to the end (easy)
522 * or somewhere else (hard).
523 * Return 0 (won't fit), 1 (easy), 2 (hard).
524 */
525/*ARGSUSED*/
526static int /* pick result */
527xfs_dir2_sf_addname_pick(
528 xfs_da_args_t *args, /* operation arguments */
529 int objchange, /* inode # size changes */
530 xfs_dir2_sf_entry_t **sfepp, /* out(1): new entry ptr */
531 xfs_dir2_data_aoff_t *offsetp) /* out(1): new offset */
532{
533 xfs_inode_t *dp; /* incore directory inode */
534 int holefit; /* found hole it will fit in */
535 int i; /* entry number */
536 xfs_mount_t *mp; /* filesystem mount point */
537 xfs_dir2_data_aoff_t offset; /* data block offset */
538 xfs_dir2_sf_entry_t *sfep; /* shortform entry */
539 xfs_dir2_sf_t *sfp; /* shortform structure */
540 int size; /* entry's data size */
541 int used; /* data bytes used */
542
543 dp = args->dp;
544 mp = dp->i_mount;
545
546 sfp = (xfs_dir2_sf_t *)dp->i_df.if_u1.if_data;
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000547 size = xfs_dir2_data_entsize(args->namelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 offset = XFS_DIR2_DATA_FIRST_OFFSET;
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000549 sfep = xfs_dir2_sf_firstentry(sfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 holefit = 0;
551 /*
552 * Loop over sf entries.
553 * Keep track of data offset and whether we've seen a place
554 * to insert the new entry.
555 */
556 for (i = 0; i < sfp->hdr.count; i++) {
557 if (!holefit)
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000558 holefit = offset + size <= xfs_dir2_sf_get_offset(sfep);
559 offset = xfs_dir2_sf_get_offset(sfep) +
560 xfs_dir2_data_entsize(sfep->namelen);
561 sfep = xfs_dir2_sf_nextentry(sfp, sfep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 }
563 /*
564 * Calculate data bytes used excluding the new entry, if this
565 * was a data block (block form directory).
566 */
567 used = offset +
568 (sfp->hdr.count + 3) * (uint)sizeof(xfs_dir2_leaf_entry_t) +
569 (uint)sizeof(xfs_dir2_block_tail_t);
570 /*
571 * If it won't fit in a block form then we can't insert it,
572 * we'll go back, convert to block, then try the insert and convert
573 * to leaf.
574 */
575 if (used + (holefit ? 0 : size) > mp->m_dirblksize)
576 return 0;
577 /*
578 * If changing the inode number size, do it the hard way.
579 */
580#if XFS_BIG_INUMS
581 if (objchange) {
582 return 2;
583 }
584#else
585 ASSERT(objchange == 0);
586#endif
587 /*
588 * If it won't fit at the end then do it the hard way (use the hole).
589 */
590 if (used + size > mp->m_dirblksize)
591 return 2;
592 /*
593 * Do it the easy way.
594 */
595 *sfepp = sfep;
596 *offsetp = offset;
597 return 1;
598}
599
600#ifdef DEBUG
601/*
602 * Check consistency of shortform directory, assert if bad.
603 */
604static void
605xfs_dir2_sf_check(
606 xfs_da_args_t *args) /* operation arguments */
607{
608 xfs_inode_t *dp; /* incore directory inode */
609 int i; /* entry number */
610 int i8count; /* number of big inode#s */
611 xfs_ino_t ino; /* entry inode number */
612 int offset; /* data offset */
613 xfs_dir2_sf_entry_t *sfep; /* shortform dir entry */
614 xfs_dir2_sf_t *sfp; /* shortform structure */
615
616 dp = args->dp;
617
618 sfp = (xfs_dir2_sf_t *)dp->i_df.if_u1.if_data;
619 offset = XFS_DIR2_DATA_FIRST_OFFSET;
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000620 ino = xfs_dir2_sf_get_inumber(sfp, &sfp->hdr.parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 i8count = ino > XFS_DIR2_MAX_SHORT_INUM;
622
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000623 for (i = 0, sfep = xfs_dir2_sf_firstentry(sfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 i < sfp->hdr.count;
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000625 i++, sfep = xfs_dir2_sf_nextentry(sfp, sfep)) {
626 ASSERT(xfs_dir2_sf_get_offset(sfep) >= offset);
627 ino = xfs_dir2_sf_get_inumber(sfp, xfs_dir2_sf_inumberp(sfep));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 i8count += ino > XFS_DIR2_MAX_SHORT_INUM;
629 offset =
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000630 xfs_dir2_sf_get_offset(sfep) +
631 xfs_dir2_data_entsize(sfep->namelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 }
633 ASSERT(i8count == sfp->hdr.i8count);
634 ASSERT(XFS_BIG_INUMS || i8count == 0);
635 ASSERT((char *)sfep - (char *)sfp == dp->i_d.di_size);
636 ASSERT(offset +
637 (sfp->hdr.count + 2) * (uint)sizeof(xfs_dir2_leaf_entry_t) +
638 (uint)sizeof(xfs_dir2_block_tail_t) <=
639 dp->i_mount->m_dirblksize);
640}
641#endif /* DEBUG */
642
643/*
644 * Create a new (shortform) directory.
645 */
646int /* error, always 0 */
647xfs_dir2_sf_create(
648 xfs_da_args_t *args, /* operation arguments */
649 xfs_ino_t pino) /* parent inode number */
650{
651 xfs_inode_t *dp; /* incore directory inode */
652 int i8count; /* parent inode is an 8-byte number */
653 xfs_dir2_sf_t *sfp; /* shortform structure */
654 int size; /* directory size */
655
656 xfs_dir2_trace_args_i("sf_create", args, pino);
657 dp = args->dp;
658
659 ASSERT(dp != NULL);
660 ASSERT(dp->i_d.di_size == 0);
661 /*
662 * If it's currently a zero-length extent file,
663 * convert it to local format.
664 */
665 if (dp->i_d.di_format == XFS_DINODE_FMT_EXTENTS) {
666 dp->i_df.if_flags &= ~XFS_IFEXTENTS; /* just in case */
667 dp->i_d.di_format = XFS_DINODE_FMT_LOCAL;
668 xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE);
669 dp->i_df.if_flags |= XFS_IFINLINE;
670 }
671 ASSERT(dp->i_df.if_flags & XFS_IFINLINE);
672 ASSERT(dp->i_df.if_bytes == 0);
673 i8count = pino > XFS_DIR2_MAX_SHORT_INUM;
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000674 size = xfs_dir2_sf_hdr_size(i8count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 /*
676 * Make a buffer for the data.
677 */
678 xfs_idata_realloc(dp, size, XFS_DATA_FORK);
679 /*
680 * Fill in the header,
681 */
682 sfp = (xfs_dir2_sf_t *)dp->i_df.if_u1.if_data;
683 sfp->hdr.i8count = i8count;
684 /*
685 * Now can put in the inode number, since i8count is set.
686 */
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000687 xfs_dir2_sf_put_inumber(sfp, &pino, &sfp->hdr.parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 sfp->hdr.count = 0;
689 dp->i_d.di_size = size;
690 xfs_dir2_sf_check(args);
691 xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE | XFS_ILOG_DDATA);
692 return 0;
693}
694
695int /* error */
696xfs_dir2_sf_getdents(
697 xfs_inode_t *dp, /* incore directory inode */
Christoph Hellwig051e7cd2007-08-28 13:58:24 +1000698 void *dirent,
699 xfs_off_t *offset,
700 filldir_t filldir)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 int i; /* shortform entry number */
703 xfs_mount_t *mp; /* filesystem mount point */
704 xfs_dir2_dataptr_t off; /* current entry's offset */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 xfs_dir2_sf_entry_t *sfep; /* shortform directory entry */
706 xfs_dir2_sf_t *sfp; /* shortform structure */
Christoph Hellwig051e7cd2007-08-28 13:58:24 +1000707 xfs_dir2_dataptr_t dot_offset;
708 xfs_dir2_dataptr_t dotdot_offset;
709 xfs_ino_t ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710
711 mp = dp->i_mount;
712
713 ASSERT(dp->i_df.if_flags & XFS_IFINLINE);
714 /*
715 * Give up if the directory is way too short.
716 */
717 if (dp->i_d.di_size < offsetof(xfs_dir2_sf_hdr_t, parent)) {
718 ASSERT(XFS_FORCED_SHUTDOWN(mp));
719 return XFS_ERROR(EIO);
720 }
721
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 ASSERT(dp->i_df.if_bytes == dp->i_d.di_size);
723 ASSERT(dp->i_df.if_u1.if_data != NULL);
724
725 sfp = (xfs_dir2_sf_t *)dp->i_df.if_u1.if_data;
726
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000727 ASSERT(dp->i_d.di_size >= xfs_dir2_sf_hdr_size(sfp->hdr.i8count));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728
729 /*
730 * If the block number in the offset is out of range, we're done.
731 */
Christoph Hellwig051e7cd2007-08-28 13:58:24 +1000732 if (xfs_dir2_dataptr_to_db(mp, *offset) > mp->m_dirdatablk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734
735 /*
Christoph Hellwig051e7cd2007-08-28 13:58:24 +1000736 * Precalculate offsets for . and .. as we will always need them.
737 *
738 * XXX(hch): the second argument is sometimes 0 and sometimes
739 * mp->m_dirdatablk.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 */
Christoph Hellwig051e7cd2007-08-28 13:58:24 +1000741 dot_offset = xfs_dir2_db_off_to_dataptr(mp, mp->m_dirdatablk,
742 XFS_DIR2_DATA_DOT_OFFSET);
743 dotdot_offset = xfs_dir2_db_off_to_dataptr(mp, mp->m_dirdatablk,
744 XFS_DIR2_DATA_DOTDOT_OFFSET);
745
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 /*
747 * Put . entry unless we're starting past it.
748 */
Christoph Hellwig051e7cd2007-08-28 13:58:24 +1000749 if (*offset <= dot_offset) {
750 ino = dp->i_ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751#if XFS_BIG_INUMS
Christoph Hellwig051e7cd2007-08-28 13:58:24 +1000752 ino += mp->m_inoadd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753#endif
Christoph Hellwig051e7cd2007-08-28 13:58:24 +1000754 if (filldir(dirent, ".", 1, dotdot_offset, ino, DT_DIR)) {
755 *offset = dot_offset;
756 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 }
758 }
759
760 /*
761 * Put .. entry unless we're starting past it.
762 */
Christoph Hellwig051e7cd2007-08-28 13:58:24 +1000763 if (*offset <= dotdot_offset) {
764 off = xfs_dir2_db_off_to_dataptr(mp, mp->m_dirdatablk,
765 XFS_DIR2_DATA_FIRST_OFFSET);
766 ino = xfs_dir2_sf_get_inumber(sfp, &sfp->hdr.parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767#if XFS_BIG_INUMS
Christoph Hellwig051e7cd2007-08-28 13:58:24 +1000768 ino += mp->m_inoadd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769#endif
Christoph Hellwig051e7cd2007-08-28 13:58:24 +1000770 if (filldir(dirent, "..", 2, off, ino, DT_DIR)) {
771 *offset = dotdot_offset;
772 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 }
774 }
775
776 /*
777 * Loop while there are more entries and put'ing works.
778 */
Christoph Hellwig051e7cd2007-08-28 13:58:24 +1000779 sfep = xfs_dir2_sf_firstentry(sfp);
780 for (i = 0; i < sfp->hdr.count; i++) {
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000781 off = xfs_dir2_db_off_to_dataptr(mp, mp->m_dirdatablk,
782 xfs_dir2_sf_get_offset(sfep));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783
Christoph Hellwig051e7cd2007-08-28 13:58:24 +1000784 if (*offset > off) {
785 sfep = xfs_dir2_sf_nextentry(sfp, sfep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 }
Christoph Hellwig051e7cd2007-08-28 13:58:24 +1000788
789 ino = xfs_dir2_sf_get_inumber(sfp, xfs_dir2_sf_inumberp(sfep));
790#if XFS_BIG_INUMS
791 ino += mp->m_inoadd;
792#endif
793
794 if (filldir(dirent, sfep->name, sfep->namelen,
795 off + xfs_dir2_data_entsize(sfep->namelen),
796 ino, DT_UNKNOWN)) {
797 *offset = off;
798 return 0;
799 }
800 sfep = xfs_dir2_sf_nextentry(sfp, sfep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 }
802
Christoph Hellwig051e7cd2007-08-28 13:58:24 +1000803 *offset = xfs_dir2_db_off_to_dataptr(mp, mp->m_dirdatablk + 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 return 0;
805}
806
807/*
808 * Lookup an entry in a shortform directory.
809 * Returns EEXIST if found, ENOENT if not found.
810 */
811int /* error */
812xfs_dir2_sf_lookup(
813 xfs_da_args_t *args) /* operation arguments */
814{
815 xfs_inode_t *dp; /* incore directory inode */
816 int i; /* entry index */
817 xfs_dir2_sf_entry_t *sfep; /* shortform directory entry */
818 xfs_dir2_sf_t *sfp; /* shortform structure */
819
820 xfs_dir2_trace_args("sf_lookup", args);
821 xfs_dir2_sf_check(args);
822 dp = args->dp;
823
824 ASSERT(dp->i_df.if_flags & XFS_IFINLINE);
825 /*
826 * Bail out if the directory is way too short.
827 */
828 if (dp->i_d.di_size < offsetof(xfs_dir2_sf_hdr_t, parent)) {
829 ASSERT(XFS_FORCED_SHUTDOWN(dp->i_mount));
830 return XFS_ERROR(EIO);
831 }
832 ASSERT(dp->i_df.if_bytes == dp->i_d.di_size);
833 ASSERT(dp->i_df.if_u1.if_data != NULL);
834 sfp = (xfs_dir2_sf_t *)dp->i_df.if_u1.if_data;
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000835 ASSERT(dp->i_d.di_size >= xfs_dir2_sf_hdr_size(sfp->hdr.i8count));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 /*
837 * Special case for .
838 */
839 if (args->namelen == 1 && args->name[0] == '.') {
840 args->inumber = dp->i_ino;
841 return XFS_ERROR(EEXIST);
842 }
843 /*
844 * Special case for ..
845 */
846 if (args->namelen == 2 &&
847 args->name[0] == '.' && args->name[1] == '.') {
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000848 args->inumber = xfs_dir2_sf_get_inumber(sfp, &sfp->hdr.parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 return XFS_ERROR(EEXIST);
850 }
851 /*
852 * Loop over all the entries trying to match ours.
853 */
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000854 for (i = 0, sfep = xfs_dir2_sf_firstentry(sfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 i < sfp->hdr.count;
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000856 i++, sfep = xfs_dir2_sf_nextentry(sfp, sfep)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 if (sfep->namelen == args->namelen &&
858 sfep->name[0] == args->name[0] &&
859 memcmp(args->name, sfep->name, args->namelen) == 0) {
860 args->inumber =
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000861 xfs_dir2_sf_get_inumber(sfp,
862 xfs_dir2_sf_inumberp(sfep));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 return XFS_ERROR(EEXIST);
864 }
865 }
866 /*
867 * Didn't find it.
868 */
869 ASSERT(args->oknoent);
870 return XFS_ERROR(ENOENT);
871}
872
873/*
874 * Remove an entry from a shortform directory.
875 */
876int /* error */
877xfs_dir2_sf_removename(
878 xfs_da_args_t *args)
879{
880 int byteoff; /* offset of removed entry */
881 xfs_inode_t *dp; /* incore directory inode */
882 int entsize; /* this entry's size */
883 int i; /* shortform entry index */
884 int newsize; /* new inode size */
885 int oldsize; /* old inode size */
886 xfs_dir2_sf_entry_t *sfep; /* shortform directory entry */
887 xfs_dir2_sf_t *sfp; /* shortform structure */
888
889 xfs_dir2_trace_args("sf_removename", args);
890 dp = args->dp;
891
892 ASSERT(dp->i_df.if_flags & XFS_IFINLINE);
893 oldsize = (int)dp->i_d.di_size;
894 /*
895 * Bail out if the directory is way too short.
896 */
897 if (oldsize < offsetof(xfs_dir2_sf_hdr_t, parent)) {
898 ASSERT(XFS_FORCED_SHUTDOWN(dp->i_mount));
899 return XFS_ERROR(EIO);
900 }
901 ASSERT(dp->i_df.if_bytes == oldsize);
902 ASSERT(dp->i_df.if_u1.if_data != NULL);
903 sfp = (xfs_dir2_sf_t *)dp->i_df.if_u1.if_data;
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000904 ASSERT(oldsize >= xfs_dir2_sf_hdr_size(sfp->hdr.i8count));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 /*
906 * Loop over the old directory entries.
907 * Find the one we're deleting.
908 */
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000909 for (i = 0, sfep = xfs_dir2_sf_firstentry(sfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 i < sfp->hdr.count;
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000911 i++, sfep = xfs_dir2_sf_nextentry(sfp, sfep)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 if (sfep->namelen == args->namelen &&
913 sfep->name[0] == args->name[0] &&
914 memcmp(sfep->name, args->name, args->namelen) == 0) {
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000915 ASSERT(xfs_dir2_sf_get_inumber(sfp,
916 xfs_dir2_sf_inumberp(sfep)) ==
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 args->inumber);
918 break;
919 }
920 }
921 /*
922 * Didn't find it.
923 */
924 if (i == sfp->hdr.count) {
925 return XFS_ERROR(ENOENT);
926 }
927 /*
928 * Calculate sizes.
929 */
930 byteoff = (int)((char *)sfep - (char *)sfp);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000931 entsize = xfs_dir2_sf_entsize_byname(sfp, args->namelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 newsize = oldsize - entsize;
933 /*
934 * Copy the part if any after the removed entry, sliding it down.
935 */
936 if (byteoff + entsize < oldsize)
937 memmove((char *)sfp + byteoff, (char *)sfp + byteoff + entsize,
938 oldsize - (byteoff + entsize));
939 /*
940 * Fix up the header and file size.
941 */
942 sfp->hdr.count--;
943 dp->i_d.di_size = newsize;
944 /*
945 * Reallocate, making it smaller.
946 */
947 xfs_idata_realloc(dp, newsize - oldsize, XFS_DATA_FORK);
948 sfp = (xfs_dir2_sf_t *)dp->i_df.if_u1.if_data;
949#if XFS_BIG_INUMS
950 /*
951 * Are we changing inode number size?
952 */
953 if (args->inumber > XFS_DIR2_MAX_SHORT_INUM) {
954 if (sfp->hdr.i8count == 1)
955 xfs_dir2_sf_toino4(args);
956 else
957 sfp->hdr.i8count--;
958 }
959#endif
960 xfs_dir2_sf_check(args);
961 xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE | XFS_ILOG_DDATA);
962 return 0;
963}
964
965/*
966 * Replace the inode number of an entry in a shortform directory.
967 */
968int /* error */
969xfs_dir2_sf_replace(
970 xfs_da_args_t *args) /* operation arguments */
971{
972 xfs_inode_t *dp; /* incore directory inode */
973 int i; /* entry index */
974#if XFS_BIG_INUMS || defined(DEBUG)
975 xfs_ino_t ino=0; /* entry old inode number */
976#endif
977#if XFS_BIG_INUMS
978 int i8elevated; /* sf_toino8 set i8count=1 */
979#endif
980 xfs_dir2_sf_entry_t *sfep; /* shortform directory entry */
981 xfs_dir2_sf_t *sfp; /* shortform structure */
982
983 xfs_dir2_trace_args("sf_replace", args);
984 dp = args->dp;
985
986 ASSERT(dp->i_df.if_flags & XFS_IFINLINE);
987 /*
988 * Bail out if the shortform directory is way too small.
989 */
990 if (dp->i_d.di_size < offsetof(xfs_dir2_sf_hdr_t, parent)) {
991 ASSERT(XFS_FORCED_SHUTDOWN(dp->i_mount));
992 return XFS_ERROR(EIO);
993 }
994 ASSERT(dp->i_df.if_bytes == dp->i_d.di_size);
995 ASSERT(dp->i_df.if_u1.if_data != NULL);
996 sfp = (xfs_dir2_sf_t *)dp->i_df.if_u1.if_data;
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000997 ASSERT(dp->i_d.di_size >= xfs_dir2_sf_hdr_size(sfp->hdr.i8count));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998#if XFS_BIG_INUMS
999 /*
1000 * New inode number is large, and need to convert to 8-byte inodes.
1001 */
1002 if (args->inumber > XFS_DIR2_MAX_SHORT_INUM && sfp->hdr.i8count == 0) {
1003 int error; /* error return value */
1004 int newsize; /* new inode size */
1005
1006 newsize =
1007 dp->i_df.if_bytes +
1008 (sfp->hdr.count + 1) *
1009 ((uint)sizeof(xfs_dir2_ino8_t) -
1010 (uint)sizeof(xfs_dir2_ino4_t));
1011 /*
1012 * Won't fit as shortform, convert to block then do replace.
1013 */
1014 if (newsize > XFS_IFORK_DSIZE(dp)) {
1015 error = xfs_dir2_sf_to_block(args);
1016 if (error) {
1017 return error;
1018 }
1019 return xfs_dir2_block_replace(args);
1020 }
1021 /*
1022 * Still fits, convert to 8-byte now.
1023 */
1024 xfs_dir2_sf_toino8(args);
1025 i8elevated = 1;
1026 sfp = (xfs_dir2_sf_t *)dp->i_df.if_u1.if_data;
1027 } else
1028 i8elevated = 0;
1029#endif
1030 ASSERT(args->namelen != 1 || args->name[0] != '.');
1031 /*
1032 * Replace ..'s entry.
1033 */
1034 if (args->namelen == 2 &&
1035 args->name[0] == '.' && args->name[1] == '.') {
1036#if XFS_BIG_INUMS || defined(DEBUG)
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001037 ino = xfs_dir2_sf_get_inumber(sfp, &sfp->hdr.parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 ASSERT(args->inumber != ino);
1039#endif
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001040 xfs_dir2_sf_put_inumber(sfp, &args->inumber, &sfp->hdr.parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 }
1042 /*
1043 * Normal entry, look for the name.
1044 */
1045 else {
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001046 for (i = 0, sfep = xfs_dir2_sf_firstentry(sfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 i < sfp->hdr.count;
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001048 i++, sfep = xfs_dir2_sf_nextentry(sfp, sfep)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 if (sfep->namelen == args->namelen &&
1050 sfep->name[0] == args->name[0] &&
1051 memcmp(args->name, sfep->name, args->namelen) == 0) {
1052#if XFS_BIG_INUMS || defined(DEBUG)
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001053 ino = xfs_dir2_sf_get_inumber(sfp,
1054 xfs_dir2_sf_inumberp(sfep));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 ASSERT(args->inumber != ino);
1056#endif
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001057 xfs_dir2_sf_put_inumber(sfp, &args->inumber,
1058 xfs_dir2_sf_inumberp(sfep));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 break;
1060 }
1061 }
1062 /*
1063 * Didn't find it.
1064 */
1065 if (i == sfp->hdr.count) {
1066 ASSERT(args->oknoent);
1067#if XFS_BIG_INUMS
1068 if (i8elevated)
1069 xfs_dir2_sf_toino4(args);
1070#endif
1071 return XFS_ERROR(ENOENT);
1072 }
1073 }
1074#if XFS_BIG_INUMS
1075 /*
1076 * See if the old number was large, the new number is small.
1077 */
1078 if (ino > XFS_DIR2_MAX_SHORT_INUM &&
1079 args->inumber <= XFS_DIR2_MAX_SHORT_INUM) {
1080 /*
1081 * And the old count was one, so need to convert to small.
1082 */
1083 if (sfp->hdr.i8count == 1)
1084 xfs_dir2_sf_toino4(args);
1085 else
1086 sfp->hdr.i8count--;
1087 }
1088 /*
1089 * See if the old number was small, the new number is large.
1090 */
1091 if (ino <= XFS_DIR2_MAX_SHORT_INUM &&
1092 args->inumber > XFS_DIR2_MAX_SHORT_INUM) {
1093 /*
1094 * add to the i8count unless we just converted to 8-byte
1095 * inodes (which does an implied i8count = 1)
1096 */
1097 ASSERT(sfp->hdr.i8count != 0);
1098 if (!i8elevated)
1099 sfp->hdr.i8count++;
1100 }
1101#endif
1102 xfs_dir2_sf_check(args);
1103 xfs_trans_log_inode(args->trans, dp, XFS_ILOG_DDATA);
1104 return 0;
1105}
1106
1107#if XFS_BIG_INUMS
1108/*
1109 * Convert from 8-byte inode numbers to 4-byte inode numbers.
1110 * The last 8-byte inode number is gone, but the count is still 1.
1111 */
1112static void
1113xfs_dir2_sf_toino4(
1114 xfs_da_args_t *args) /* operation arguments */
1115{
1116 char *buf; /* old dir's buffer */
1117 xfs_inode_t *dp; /* incore directory inode */
1118 int i; /* entry index */
1119 xfs_ino_t ino; /* entry inode number */
1120 int newsize; /* new inode size */
1121 xfs_dir2_sf_entry_t *oldsfep; /* old sf entry */
1122 xfs_dir2_sf_t *oldsfp; /* old sf directory */
1123 int oldsize; /* old inode size */
1124 xfs_dir2_sf_entry_t *sfep; /* new sf entry */
1125 xfs_dir2_sf_t *sfp; /* new sf directory */
1126
1127 xfs_dir2_trace_args("sf_toino4", args);
1128 dp = args->dp;
1129
1130 /*
1131 * Copy the old directory to the buffer.
1132 * Then nuke it from the inode, and add the new buffer to the inode.
1133 * Don't want xfs_idata_realloc copying the data here.
1134 */
1135 oldsize = dp->i_df.if_bytes;
1136 buf = kmem_alloc(oldsize, KM_SLEEP);
1137 oldsfp = (xfs_dir2_sf_t *)dp->i_df.if_u1.if_data;
1138 ASSERT(oldsfp->hdr.i8count == 1);
1139 memcpy(buf, oldsfp, oldsize);
1140 /*
1141 * Compute the new inode size.
1142 */
1143 newsize =
1144 oldsize -
1145 (oldsfp->hdr.count + 1) *
1146 ((uint)sizeof(xfs_dir2_ino8_t) - (uint)sizeof(xfs_dir2_ino4_t));
1147 xfs_idata_realloc(dp, -oldsize, XFS_DATA_FORK);
1148 xfs_idata_realloc(dp, newsize, XFS_DATA_FORK);
1149 /*
1150 * Reset our pointers, the data has moved.
1151 */
1152 oldsfp = (xfs_dir2_sf_t *)buf;
1153 sfp = (xfs_dir2_sf_t *)dp->i_df.if_u1.if_data;
1154 /*
1155 * Fill in the new header.
1156 */
1157 sfp->hdr.count = oldsfp->hdr.count;
1158 sfp->hdr.i8count = 0;
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001159 ino = xfs_dir2_sf_get_inumber(oldsfp, &oldsfp->hdr.parent);
1160 xfs_dir2_sf_put_inumber(sfp, &ino, &sfp->hdr.parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 /*
1162 * Copy the entries field by field.
1163 */
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001164 for (i = 0, sfep = xfs_dir2_sf_firstentry(sfp),
1165 oldsfep = xfs_dir2_sf_firstentry(oldsfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 i < sfp->hdr.count;
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001167 i++, sfep = xfs_dir2_sf_nextentry(sfp, sfep),
1168 oldsfep = xfs_dir2_sf_nextentry(oldsfp, oldsfep)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 sfep->namelen = oldsfep->namelen;
1170 sfep->offset = oldsfep->offset;
1171 memcpy(sfep->name, oldsfep->name, sfep->namelen);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001172 ino = xfs_dir2_sf_get_inumber(oldsfp,
1173 xfs_dir2_sf_inumberp(oldsfep));
1174 xfs_dir2_sf_put_inumber(sfp, &ino, xfs_dir2_sf_inumberp(sfep));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 }
1176 /*
1177 * Clean up the inode.
1178 */
1179 kmem_free(buf, oldsize);
1180 dp->i_d.di_size = newsize;
1181 xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE | XFS_ILOG_DDATA);
1182}
1183
1184/*
1185 * Convert from 4-byte inode numbers to 8-byte inode numbers.
1186 * The new 8-byte inode number is not there yet, we leave with the
1187 * count 1 but no corresponding entry.
1188 */
1189static void
1190xfs_dir2_sf_toino8(
1191 xfs_da_args_t *args) /* operation arguments */
1192{
1193 char *buf; /* old dir's buffer */
1194 xfs_inode_t *dp; /* incore directory inode */
1195 int i; /* entry index */
1196 xfs_ino_t ino; /* entry inode number */
1197 int newsize; /* new inode size */
1198 xfs_dir2_sf_entry_t *oldsfep; /* old sf entry */
1199 xfs_dir2_sf_t *oldsfp; /* old sf directory */
1200 int oldsize; /* old inode size */
1201 xfs_dir2_sf_entry_t *sfep; /* new sf entry */
1202 xfs_dir2_sf_t *sfp; /* new sf directory */
1203
1204 xfs_dir2_trace_args("sf_toino8", args);
1205 dp = args->dp;
1206
1207 /*
1208 * Copy the old directory to the buffer.
1209 * Then nuke it from the inode, and add the new buffer to the inode.
1210 * Don't want xfs_idata_realloc copying the data here.
1211 */
1212 oldsize = dp->i_df.if_bytes;
1213 buf = kmem_alloc(oldsize, KM_SLEEP);
1214 oldsfp = (xfs_dir2_sf_t *)dp->i_df.if_u1.if_data;
1215 ASSERT(oldsfp->hdr.i8count == 0);
1216 memcpy(buf, oldsfp, oldsize);
1217 /*
1218 * Compute the new inode size.
1219 */
1220 newsize =
1221 oldsize +
1222 (oldsfp->hdr.count + 1) *
1223 ((uint)sizeof(xfs_dir2_ino8_t) - (uint)sizeof(xfs_dir2_ino4_t));
1224 xfs_idata_realloc(dp, -oldsize, XFS_DATA_FORK);
1225 xfs_idata_realloc(dp, newsize, XFS_DATA_FORK);
1226 /*
1227 * Reset our pointers, the data has moved.
1228 */
1229 oldsfp = (xfs_dir2_sf_t *)buf;
1230 sfp = (xfs_dir2_sf_t *)dp->i_df.if_u1.if_data;
1231 /*
1232 * Fill in the new header.
1233 */
1234 sfp->hdr.count = oldsfp->hdr.count;
1235 sfp->hdr.i8count = 1;
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001236 ino = xfs_dir2_sf_get_inumber(oldsfp, &oldsfp->hdr.parent);
1237 xfs_dir2_sf_put_inumber(sfp, &ino, &sfp->hdr.parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238 /*
1239 * Copy the entries field by field.
1240 */
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001241 for (i = 0, sfep = xfs_dir2_sf_firstentry(sfp),
1242 oldsfep = xfs_dir2_sf_firstentry(oldsfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 i < sfp->hdr.count;
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001244 i++, sfep = xfs_dir2_sf_nextentry(sfp, sfep),
1245 oldsfep = xfs_dir2_sf_nextentry(oldsfp, oldsfep)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 sfep->namelen = oldsfep->namelen;
1247 sfep->offset = oldsfep->offset;
1248 memcpy(sfep->name, oldsfep->name, sfep->namelen);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001249 ino = xfs_dir2_sf_get_inumber(oldsfp,
1250 xfs_dir2_sf_inumberp(oldsfep));
1251 xfs_dir2_sf_put_inumber(sfp, &ino, xfs_dir2_sf_inumberp(sfep));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 }
1253 /*
1254 * Clean up the inode.
1255 */
1256 kmem_free(buf, oldsize);
1257 dp->i_d.di_size = newsize;
1258 xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE | XFS_ILOG_DDATA);
1259}
1260#endif /* XFS_BIG_INUMS */