blob: 65b65c5f8c3cdbe933135a3200763811e34976e8 [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"
22#include "xfs_trans.h"
23#include "xfs_sb.h"
David Chinnerda353b02007-08-28 14:00:13 +100024#include "xfs_ag.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include "xfs_mount.h"
Nathan Scotta844f452005-11-02 14:38:42 +110026#include "xfs_da_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include "xfs_bmap_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include "xfs_dinode.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include "xfs_inode.h"
Nathan Scotta844f452005-11-02 14:38:42 +110030#include "xfs_inode_item.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include "xfs_error.h"
Christoph Hellwig57926642011-07-13 13:43:48 +020032#include "xfs_dir2_format.h"
Dave Chinner2b9ab5a2013-08-12 20:49:37 +100033#include "xfs_dir2.h"
Christoph Hellwig57926642011-07-13 13:43:48 +020034#include "xfs_dir2_priv.h"
Christoph Hellwig0b1b2132009-12-14 23:14:59 +000035#include "xfs_trace.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
37/*
38 * Prototypes for internal functions.
39 */
40static void xfs_dir2_sf_addname_easy(xfs_da_args_t *args,
41 xfs_dir2_sf_entry_t *sfep,
42 xfs_dir2_data_aoff_t offset,
43 int new_isize);
44static void xfs_dir2_sf_addname_hard(xfs_da_args_t *args, int objchange,
45 int new_isize);
46static int xfs_dir2_sf_addname_pick(xfs_da_args_t *args, int objchange,
47 xfs_dir2_sf_entry_t **sfepp,
48 xfs_dir2_data_aoff_t *offsetp);
49#ifdef DEBUG
50static void xfs_dir2_sf_check(xfs_da_args_t *args);
51#else
52#define xfs_dir2_sf_check(args)
53#endif /* DEBUG */
54#if XFS_BIG_INUMS
55static void xfs_dir2_sf_toino4(xfs_da_args_t *args);
56static void xfs_dir2_sf_toino8(xfs_da_args_t *args);
57#endif /* XFS_BIG_INUMS */
58
Christoph Hellwig8bc38782011-07-08 14:35:03 +020059/*
60 * Inode numbers in short-form directories can come in two versions,
61 * either 4 bytes or 8 bytes wide. These helpers deal with the
62 * two forms transparently by looking at the headers i8count field.
Christoph Hellwig218106a2011-07-08 14:35:58 +020063 *
64 * For 64-bit inode number the most significant byte must be zero.
Christoph Hellwig8bc38782011-07-08 14:35:03 +020065 */
66static xfs_ino_t
67xfs_dir2_sf_get_ino(
Christoph Hellwigac8ba502011-07-08 14:35:13 +020068 struct xfs_dir2_sf_hdr *hdr,
Christoph Hellwig8bc38782011-07-08 14:35:03 +020069 xfs_dir2_inou_t *from)
70{
Christoph Hellwigac8ba502011-07-08 14:35:13 +020071 if (hdr->i8count)
Christoph Hellwig218106a2011-07-08 14:35:58 +020072 return get_unaligned_be64(&from->i8.i) & 0x00ffffffffffffffULL;
Christoph Hellwig8bc38782011-07-08 14:35:03 +020073 else
Christoph Hellwig218106a2011-07-08 14:35:58 +020074 return get_unaligned_be32(&from->i4.i);
Christoph Hellwig8bc38782011-07-08 14:35:03 +020075}
76
77static void
78xfs_dir2_sf_put_ino(
Christoph Hellwigac8ba502011-07-08 14:35:13 +020079 struct xfs_dir2_sf_hdr *hdr,
Christoph Hellwig8bc38782011-07-08 14:35:03 +020080 xfs_dir2_inou_t *to,
81 xfs_ino_t ino)
82{
Christoph Hellwig218106a2011-07-08 14:35:58 +020083 ASSERT((ino & 0xff00000000000000ULL) == 0);
84
Christoph Hellwigac8ba502011-07-08 14:35:13 +020085 if (hdr->i8count)
Christoph Hellwig218106a2011-07-08 14:35:58 +020086 put_unaligned_be64(ino, &to->i8.i);
Christoph Hellwig8bc38782011-07-08 14:35:03 +020087 else
Christoph Hellwig218106a2011-07-08 14:35:58 +020088 put_unaligned_be32(ino, &to->i4.i);
Christoph Hellwig8bc38782011-07-08 14:35:03 +020089}
90
91xfs_ino_t
92xfs_dir2_sf_get_parent_ino(
Christoph Hellwigac8ba502011-07-08 14:35:13 +020093 struct xfs_dir2_sf_hdr *hdr)
Christoph Hellwig8bc38782011-07-08 14:35:03 +020094{
Christoph Hellwigac8ba502011-07-08 14:35:13 +020095 return xfs_dir2_sf_get_ino(hdr, &hdr->parent);
Christoph Hellwig8bc38782011-07-08 14:35:03 +020096}
97
Dave Chinner2b9ab5a2013-08-12 20:49:37 +100098void
Christoph Hellwig8bc38782011-07-08 14:35:03 +020099xfs_dir2_sf_put_parent_ino(
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200100 struct xfs_dir2_sf_hdr *hdr,
Christoph Hellwig8bc38782011-07-08 14:35:03 +0200101 xfs_ino_t ino)
102{
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200103 xfs_dir2_sf_put_ino(hdr, &hdr->parent, ino);
Christoph Hellwig8bc38782011-07-08 14:35:03 +0200104}
105
106/*
107 * In short-form directory entries the inode numbers are stored at variable
108 * offset behind the entry name. The inode numbers may only be accessed
109 * through the helpers below.
110 */
111static xfs_dir2_inou_t *
112xfs_dir2_sfe_inop(
113 struct xfs_dir2_sf_entry *sfep)
114{
115 return (xfs_dir2_inou_t *)&sfep->name[sfep->namelen];
116}
117
118xfs_ino_t
119xfs_dir2_sfe_get_ino(
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200120 struct xfs_dir2_sf_hdr *hdr,
Christoph Hellwig8bc38782011-07-08 14:35:03 +0200121 struct xfs_dir2_sf_entry *sfep)
122{
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200123 return xfs_dir2_sf_get_ino(hdr, xfs_dir2_sfe_inop(sfep));
Christoph Hellwig8bc38782011-07-08 14:35:03 +0200124}
125
Dave Chinner2b9ab5a2013-08-12 20:49:37 +1000126void
Christoph Hellwig8bc38782011-07-08 14:35:03 +0200127xfs_dir2_sfe_put_ino(
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200128 struct xfs_dir2_sf_hdr *hdr,
Christoph Hellwig8bc38782011-07-08 14:35:03 +0200129 struct xfs_dir2_sf_entry *sfep,
130 xfs_ino_t ino)
131{
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200132 xfs_dir2_sf_put_ino(hdr, xfs_dir2_sfe_inop(sfep), ino);
Christoph Hellwig8bc38782011-07-08 14:35:03 +0200133}
134
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135/*
136 * Given a block directory (dp/block), calculate its size as a shortform (sf)
137 * directory and a header for the sf directory, if it will fit it the
138 * space currently present in the inode. If it won't fit, the output
139 * size is too big (but not accurate).
140 */
141int /* size for sf form */
142xfs_dir2_block_sfsize(
143 xfs_inode_t *dp, /* incore inode pointer */
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +0200144 xfs_dir2_data_hdr_t *hdr, /* block directory data */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 xfs_dir2_sf_hdr_t *sfhp) /* output: header for sf form */
146{
147 xfs_dir2_dataptr_t addr; /* data entry address */
148 xfs_dir2_leaf_entry_t *blp; /* leaf area of the block */
149 xfs_dir2_block_tail_t *btp; /* tail area of the block */
150 int count; /* shortform entry count */
151 xfs_dir2_data_entry_t *dep; /* data entry in the block */
152 int i; /* block entry index */
153 int i8count; /* count of big-inode entries */
154 int isdot; /* entry is "." */
155 int isdotdot; /* entry is ".." */
156 xfs_mount_t *mp; /* mount structure pointer */
157 int namelen; /* total name bytes */
Christoph Hellwig5bde1ba92005-11-02 15:06:18 +1100158 xfs_ino_t parent = 0; /* parent inode number */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 int size=0; /* total computed size */
160
161 mp = dp->i_mount;
162
163 count = i8count = namelen = 0;
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +0200164 btp = xfs_dir2_block_tail_p(mp, hdr);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000165 blp = xfs_dir2_block_leaf_p(btp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166
167 /*
168 * Iterate over the block's data entries by using the leaf pointers.
169 */
Nathan Scotte922fff2006-03-17 17:27:56 +1100170 for (i = 0; i < be32_to_cpu(btp->count); i++) {
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100171 if ((addr = be32_to_cpu(blp[i].address)) == XFS_DIR2_NULL_DATAPTR)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 continue;
173 /*
174 * Calculate the pointer to the entry at hand.
175 */
176 dep = (xfs_dir2_data_entry_t *)
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +0200177 ((char *)hdr + xfs_dir2_dataptr_to_off(mp, addr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 /*
179 * Detect . and .., so we can special-case them.
180 * . is not included in sf directories.
181 * .. is included by just the parent inode number.
182 */
183 isdot = dep->namelen == 1 && dep->name[0] == '.';
184 isdotdot =
185 dep->namelen == 2 &&
186 dep->name[0] == '.' && dep->name[1] == '.';
187#if XFS_BIG_INUMS
188 if (!isdot)
Christoph Hellwigff9901c2006-06-09 14:48:37 +1000189 i8count += be64_to_cpu(dep->inumber) > XFS_DIR2_MAX_SHORT_INUM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190#endif
191 if (!isdot && !isdotdot) {
192 count++;
193 namelen += dep->namelen;
194 } else if (isdotdot)
Christoph Hellwigff9901c2006-06-09 14:48:37 +1000195 parent = be64_to_cpu(dep->inumber);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 /*
197 * Calculate the new size, see if we should give up yet.
198 */
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000199 size = xfs_dir2_sf_hdr_size(i8count) + /* header */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 count + /* namelen */
201 count * (uint)sizeof(xfs_dir2_sf_off_t) + /* offset */
202 namelen + /* name */
203 (i8count ? /* inumber */
204 (uint)sizeof(xfs_dir2_ino8_t) * count :
205 (uint)sizeof(xfs_dir2_ino4_t) * count);
206 if (size > XFS_IFORK_DSIZE(dp))
207 return size; /* size value is a failure */
208 }
209 /*
210 * Create the output header, if it worked.
211 */
212 sfhp->count = count;
213 sfhp->i8count = i8count;
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200214 xfs_dir2_sf_put_parent_ino(sfhp, parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 return size;
216}
217
218/*
219 * Convert a block format directory to shortform.
220 * Caller has already checked that it will fit, and built us a header.
221 */
222int /* error */
223xfs_dir2_block_to_sf(
224 xfs_da_args_t *args, /* operation arguments */
Dave Chinner1d9025e2012-06-22 18:50:14 +1000225 struct xfs_buf *bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 int size, /* shortform directory size */
227 xfs_dir2_sf_hdr_t *sfhp) /* shortform directory hdr */
228{
Christoph Hellwiga64b0412011-07-08 14:35:32 +0200229 xfs_dir2_data_hdr_t *hdr; /* block header */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 xfs_dir2_block_tail_t *btp; /* block tail pointer */
231 xfs_dir2_data_entry_t *dep; /* data entry pointer */
232 xfs_inode_t *dp; /* incore directory inode */
233 xfs_dir2_data_unused_t *dup; /* unused data pointer */
234 char *endptr; /* end of data entries */
235 int error; /* error return value */
236 int logflags; /* inode logging flags */
237 xfs_mount_t *mp; /* filesystem mount point */
238 char *ptr; /* current data pointer */
239 xfs_dir2_sf_entry_t *sfep; /* shortform entry */
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200240 xfs_dir2_sf_hdr_t *sfp; /* shortform directory header */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000242 trace_xfs_dir2_block_to_sf(args);
243
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 dp = args->dp;
245 mp = dp->i_mount;
246
247 /*
248 * Make a copy of the block data, so we can shrink the inode
249 * and add local data.
250 */
Christoph Hellwiga64b0412011-07-08 14:35:32 +0200251 hdr = kmem_alloc(mp->m_dirblksize, KM_SLEEP);
Dave Chinner1d9025e2012-06-22 18:50:14 +1000252 memcpy(hdr, bp->b_addr, mp->m_dirblksize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 logflags = XFS_ILOG_CORE;
254 if ((error = xfs_dir2_shrink_inode(args, mp->m_dirdatablk, bp))) {
255 ASSERT(error != ENOSPC);
256 goto out;
257 }
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +0200258
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 /*
260 * The buffer is now unconditionally gone, whether
261 * xfs_dir2_shrink_inode worked or not.
262 *
263 * Convert the inode to local format.
264 */
265 dp->i_df.if_flags &= ~XFS_IFEXTENTS;
266 dp->i_df.if_flags |= XFS_IFINLINE;
267 dp->i_d.di_format = XFS_DINODE_FMT_LOCAL;
268 ASSERT(dp->i_df.if_bytes == 0);
269 xfs_idata_realloc(dp, size, XFS_DATA_FORK);
270 logflags |= XFS_ILOG_DDATA;
271 /*
272 * Copy the header into the newly allocate local space.
273 */
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200274 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000275 memcpy(sfp, sfhp, xfs_dir2_sf_hdr_size(sfhp->i8count));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 dp->i_d.di_size = size;
277 /*
278 * Set up to loop over the block's entries.
279 */
Christoph Hellwiga64b0412011-07-08 14:35:32 +0200280 btp = xfs_dir2_block_tail_p(mp, hdr);
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100281 ptr = (char *)xfs_dir3_data_entry_p(hdr);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000282 endptr = (char *)xfs_dir2_block_leaf_p(btp);
283 sfep = xfs_dir2_sf_firstentry(sfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 /*
285 * Loop over the active and unused entries.
286 * Stop when we reach the leaf/tail portion of the block.
287 */
288 while (ptr < endptr) {
289 /*
290 * If it's unused, just skip over it.
291 */
292 dup = (xfs_dir2_data_unused_t *)ptr;
Nathan Scottad354eb2006-03-17 17:27:37 +1100293 if (be16_to_cpu(dup->freetag) == XFS_DIR2_DATA_FREE_TAG) {
294 ptr += be16_to_cpu(dup->length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 continue;
296 }
297 dep = (xfs_dir2_data_entry_t *)ptr;
298 /*
299 * Skip .
300 */
301 if (dep->namelen == 1 && dep->name[0] == '.')
Christoph Hellwigff9901c2006-06-09 14:48:37 +1000302 ASSERT(be64_to_cpu(dep->inumber) == dp->i_ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 /*
304 * Skip .., but make sure the inode number is right.
305 */
306 else if (dep->namelen == 2 &&
307 dep->name[0] == '.' && dep->name[1] == '.')
Christoph Hellwigff9901c2006-06-09 14:48:37 +1000308 ASSERT(be64_to_cpu(dep->inumber) ==
Christoph Hellwig8bc38782011-07-08 14:35:03 +0200309 xfs_dir2_sf_get_parent_ino(sfp));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 /*
311 * Normal entry, copy it into shortform.
312 */
313 else {
314 sfep->namelen = dep->namelen;
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000315 xfs_dir2_sf_put_offset(sfep,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 (xfs_dir2_data_aoff_t)
Christoph Hellwiga64b0412011-07-08 14:35:32 +0200317 ((char *)dep - (char *)hdr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 memcpy(sfep->name, dep->name, dep->namelen);
Christoph Hellwig8bc38782011-07-08 14:35:03 +0200319 xfs_dir2_sfe_put_ino(sfp, sfep,
320 be64_to_cpu(dep->inumber));
321
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000322 sfep = xfs_dir2_sf_nextentry(sfp, sfep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 }
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000324 ptr += xfs_dir2_data_entsize(dep->namelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 }
326 ASSERT((char *)sfep - (char *)sfp == size);
327 xfs_dir2_sf_check(args);
328out:
329 xfs_trans_log_inode(args->trans, dp, logflags);
Christoph Hellwiga64b0412011-07-08 14:35:32 +0200330 kmem_free(hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 return error;
332}
333
334/*
335 * Add a name to a shortform directory.
336 * There are two algorithms, "easy" and "hard" which we decide on
337 * before changing anything.
338 * Convert to block form if necessary, if the new entry won't fit.
339 */
340int /* error */
341xfs_dir2_sf_addname(
342 xfs_da_args_t *args) /* operation arguments */
343{
344 int add_entsize; /* size of the new entry */
345 xfs_inode_t *dp; /* incore directory inode */
346 int error; /* error return value */
347 int incr_isize; /* total change in size */
348 int new_isize; /* di_size after adding name */
349 int objchange; /* changing to 8-byte inodes */
Christoph Hellwig5bde1ba92005-11-02 15:06:18 +1100350 xfs_dir2_data_aoff_t offset = 0; /* offset for new entry */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 int old_isize; /* di_size before adding name */
352 int pick; /* which algorithm to use */
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200353 xfs_dir2_sf_hdr_t *sfp; /* shortform structure */
Christoph Hellwig5bde1ba92005-11-02 15:06:18 +1100354 xfs_dir2_sf_entry_t *sfep = NULL; /* shortform entry */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000356 trace_xfs_dir2_sf_addname(args);
357
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 ASSERT(xfs_dir2_sf_lookup(args) == ENOENT);
359 dp = args->dp;
360 ASSERT(dp->i_df.if_flags & XFS_IFINLINE);
361 /*
362 * Make sure the shortform value has some of its header.
363 */
364 if (dp->i_d.di_size < offsetof(xfs_dir2_sf_hdr_t, parent)) {
365 ASSERT(XFS_FORCED_SHUTDOWN(dp->i_mount));
366 return XFS_ERROR(EIO);
367 }
368 ASSERT(dp->i_df.if_bytes == dp->i_d.di_size);
369 ASSERT(dp->i_df.if_u1.if_data != NULL);
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200370 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
371 ASSERT(dp->i_d.di_size >= xfs_dir2_sf_hdr_size(sfp->i8count));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 /*
373 * Compute entry (and change in) size.
374 */
Christoph Hellwig78f70cd2011-07-08 14:35:19 +0200375 add_entsize = xfs_dir2_sf_entsize(sfp, args->namelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 incr_isize = add_entsize;
377 objchange = 0;
378#if XFS_BIG_INUMS
379 /*
380 * Do we have to change to 8 byte inodes?
381 */
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200382 if (args->inumber > XFS_DIR2_MAX_SHORT_INUM && sfp->i8count == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 /*
384 * Yes, adjust the entry size and the total size.
385 */
386 add_entsize +=
387 (uint)sizeof(xfs_dir2_ino8_t) -
388 (uint)sizeof(xfs_dir2_ino4_t);
389 incr_isize +=
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200390 (sfp->count + 2) *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 ((uint)sizeof(xfs_dir2_ino8_t) -
392 (uint)sizeof(xfs_dir2_ino4_t));
393 objchange = 1;
394 }
395#endif
396 old_isize = (int)dp->i_d.di_size;
397 new_isize = old_isize + incr_isize;
398 /*
399 * Won't fit as shortform any more (due to size),
400 * or the pick routine says it won't (due to offset values).
401 */
402 if (new_isize > XFS_IFORK_DSIZE(dp) ||
403 (pick =
404 xfs_dir2_sf_addname_pick(args, objchange, &sfep, &offset)) == 0) {
405 /*
406 * Just checking or no space reservation, it doesn't fit.
407 */
Barry Naujok6a178102008-05-21 16:42:05 +1000408 if ((args->op_flags & XFS_DA_OP_JUSTCHECK) || args->total == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 return XFS_ERROR(ENOSPC);
410 /*
411 * Convert to block form then add the name.
412 */
413 error = xfs_dir2_sf_to_block(args);
414 if (error)
415 return error;
416 return xfs_dir2_block_addname(args);
417 }
418 /*
419 * Just checking, it fits.
420 */
Barry Naujok6a178102008-05-21 16:42:05 +1000421 if (args->op_flags & XFS_DA_OP_JUSTCHECK)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 return 0;
423 /*
424 * Do it the easy way - just add it at the end.
425 */
426 if (pick == 1)
427 xfs_dir2_sf_addname_easy(args, sfep, offset, new_isize);
428 /*
429 * Do it the hard way - look for a place to insert the new entry.
430 * Convert to 8 byte inode numbers first if necessary.
431 */
432 else {
433 ASSERT(pick == 2);
434#if XFS_BIG_INUMS
435 if (objchange)
436 xfs_dir2_sf_toino8(args);
437#endif
438 xfs_dir2_sf_addname_hard(args, objchange, new_isize);
439 }
440 xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE | XFS_ILOG_DDATA);
441 return 0;
442}
443
444/*
445 * Add the new entry the "easy" way.
446 * This is copying the old directory and adding the new entry at the end.
447 * Since it's sorted by "offset" we need room after the last offset
448 * that's already there, and then room to convert to a block directory.
449 * This is already checked by the pick routine.
450 */
451static void
452xfs_dir2_sf_addname_easy(
453 xfs_da_args_t *args, /* operation arguments */
454 xfs_dir2_sf_entry_t *sfep, /* pointer to new entry */
455 xfs_dir2_data_aoff_t offset, /* offset to use for new ent */
456 int new_isize) /* new directory size */
457{
458 int byteoff; /* byte offset in sf dir */
459 xfs_inode_t *dp; /* incore directory inode */
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200460 xfs_dir2_sf_hdr_t *sfp; /* shortform structure */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461
462 dp = args->dp;
463
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200464 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 byteoff = (int)((char *)sfep - (char *)sfp);
466 /*
467 * Grow the in-inode space.
468 */
Christoph Hellwig78f70cd2011-07-08 14:35:19 +0200469 xfs_idata_realloc(dp, xfs_dir2_sf_entsize(sfp, args->namelen),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 XFS_DATA_FORK);
471 /*
472 * Need to set up again due to realloc of the inode data.
473 */
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200474 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 sfep = (xfs_dir2_sf_entry_t *)((char *)sfp + byteoff);
476 /*
477 * Fill in the new entry.
478 */
479 sfep->namelen = args->namelen;
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000480 xfs_dir2_sf_put_offset(sfep, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 memcpy(sfep->name, args->name, sfep->namelen);
Christoph Hellwig8bc38782011-07-08 14:35:03 +0200482 xfs_dir2_sfe_put_ino(sfp, sfep, args->inumber);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 /*
484 * Update the header and inode.
485 */
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200486 sfp->count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487#if XFS_BIG_INUMS
488 if (args->inumber > XFS_DIR2_MAX_SHORT_INUM)
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200489 sfp->i8count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490#endif
491 dp->i_d.di_size = new_isize;
492 xfs_dir2_sf_check(args);
493}
494
495/*
496 * Add the new entry the "hard" way.
497 * The caller has already converted to 8 byte inode numbers if necessary,
498 * in which case we need to leave the i8count at 1.
499 * Find a hole that the new entry will fit into, and copy
500 * the first part of the entries, the new entry, and the last part of
501 * the entries.
502 */
503/* ARGSUSED */
504static void
505xfs_dir2_sf_addname_hard(
506 xfs_da_args_t *args, /* operation arguments */
507 int objchange, /* changing inode number size */
508 int new_isize) /* new directory size */
509{
510 int add_datasize; /* data size need for new ent */
511 char *buf; /* buffer for old */
512 xfs_inode_t *dp; /* incore directory inode */
513 int eof; /* reached end of old dir */
514 int nbytes; /* temp for byte copies */
515 xfs_dir2_data_aoff_t new_offset; /* next offset value */
516 xfs_dir2_data_aoff_t offset; /* current offset value */
517 int old_isize; /* previous di_size */
518 xfs_dir2_sf_entry_t *oldsfep; /* entry in original dir */
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200519 xfs_dir2_sf_hdr_t *oldsfp; /* original shortform dir */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 xfs_dir2_sf_entry_t *sfep; /* entry in new dir */
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200521 xfs_dir2_sf_hdr_t *sfp; /* new shortform dir */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522
523 /*
524 * Copy the old directory to the stack buffer.
525 */
526 dp = args->dp;
527
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200528 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 old_isize = (int)dp->i_d.di_size;
530 buf = kmem_alloc(old_isize, KM_SLEEP);
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200531 oldsfp = (xfs_dir2_sf_hdr_t *)buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 memcpy(oldsfp, sfp, old_isize);
533 /*
534 * Loop over the old directory finding the place we're going
535 * to insert the new entry.
536 * If it's going to end up at the end then oldsfep will point there.
537 */
Dave Chinner6b2647a2013-04-03 16:11:24 +1100538 for (offset = XFS_DIR3_DATA_FIRST_OFFSET(dp->i_mount),
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000539 oldsfep = xfs_dir2_sf_firstentry(oldsfp),
540 add_datasize = xfs_dir2_data_entsize(args->namelen),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 eof = (char *)oldsfep == &buf[old_isize];
542 !eof;
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000543 offset = new_offset + xfs_dir2_data_entsize(oldsfep->namelen),
544 oldsfep = xfs_dir2_sf_nextentry(oldsfp, oldsfep),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 eof = (char *)oldsfep == &buf[old_isize]) {
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000546 new_offset = xfs_dir2_sf_get_offset(oldsfep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 if (offset + add_datasize <= new_offset)
548 break;
549 }
550 /*
551 * Get rid of the old directory, then allocate space for
552 * the new one. We do this so xfs_idata_realloc won't copy
553 * the data.
554 */
555 xfs_idata_realloc(dp, -old_isize, XFS_DATA_FORK);
556 xfs_idata_realloc(dp, new_isize, XFS_DATA_FORK);
557 /*
558 * Reset the pointer since the buffer was reallocated.
559 */
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200560 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 /*
562 * Copy the first part of the directory, including the header.
563 */
564 nbytes = (int)((char *)oldsfep - (char *)oldsfp);
565 memcpy(sfp, oldsfp, nbytes);
566 sfep = (xfs_dir2_sf_entry_t *)((char *)sfp + nbytes);
567 /*
568 * Fill in the new entry, and update the header counts.
569 */
570 sfep->namelen = args->namelen;
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000571 xfs_dir2_sf_put_offset(sfep, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 memcpy(sfep->name, args->name, sfep->namelen);
Christoph Hellwig8bc38782011-07-08 14:35:03 +0200573 xfs_dir2_sfe_put_ino(sfp, sfep, args->inumber);
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200574 sfp->count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575#if XFS_BIG_INUMS
576 if (args->inumber > XFS_DIR2_MAX_SHORT_INUM && !objchange)
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200577 sfp->i8count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578#endif
579 /*
580 * If there's more left to copy, do that.
581 */
582 if (!eof) {
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000583 sfep = xfs_dir2_sf_nextentry(sfp, sfep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 memcpy(sfep, oldsfep, old_isize - nbytes);
585 }
Denys Vlasenkof0e2d932008-05-19 16:31:57 +1000586 kmem_free(buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 dp->i_d.di_size = new_isize;
588 xfs_dir2_sf_check(args);
589}
590
591/*
592 * Decide if the new entry will fit at all.
593 * If it will fit, pick between adding the new entry to the end (easy)
594 * or somewhere else (hard).
595 * Return 0 (won't fit), 1 (easy), 2 (hard).
596 */
597/*ARGSUSED*/
598static int /* pick result */
599xfs_dir2_sf_addname_pick(
600 xfs_da_args_t *args, /* operation arguments */
601 int objchange, /* inode # size changes */
602 xfs_dir2_sf_entry_t **sfepp, /* out(1): new entry ptr */
603 xfs_dir2_data_aoff_t *offsetp) /* out(1): new offset */
604{
605 xfs_inode_t *dp; /* incore directory inode */
606 int holefit; /* found hole it will fit in */
607 int i; /* entry number */
608 xfs_mount_t *mp; /* filesystem mount point */
609 xfs_dir2_data_aoff_t offset; /* data block offset */
610 xfs_dir2_sf_entry_t *sfep; /* shortform entry */
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200611 xfs_dir2_sf_hdr_t *sfp; /* shortform structure */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 int size; /* entry's data size */
613 int used; /* data bytes used */
614
615 dp = args->dp;
616 mp = dp->i_mount;
617
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200618 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000619 size = xfs_dir2_data_entsize(args->namelen);
Dave Chinner6b2647a2013-04-03 16:11:24 +1100620 offset = XFS_DIR3_DATA_FIRST_OFFSET(mp);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000621 sfep = xfs_dir2_sf_firstentry(sfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 holefit = 0;
623 /*
624 * Loop over sf entries.
625 * Keep track of data offset and whether we've seen a place
626 * to insert the new entry.
627 */
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200628 for (i = 0; i < sfp->count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 if (!holefit)
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000630 holefit = offset + size <= xfs_dir2_sf_get_offset(sfep);
631 offset = xfs_dir2_sf_get_offset(sfep) +
632 xfs_dir2_data_entsize(sfep->namelen);
633 sfep = xfs_dir2_sf_nextentry(sfp, sfep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 }
635 /*
636 * Calculate data bytes used excluding the new entry, if this
637 * was a data block (block form directory).
638 */
639 used = offset +
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200640 (sfp->count + 3) * (uint)sizeof(xfs_dir2_leaf_entry_t) +
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 (uint)sizeof(xfs_dir2_block_tail_t);
642 /*
643 * If it won't fit in a block form then we can't insert it,
644 * we'll go back, convert to block, then try the insert and convert
645 * to leaf.
646 */
647 if (used + (holefit ? 0 : size) > mp->m_dirblksize)
648 return 0;
649 /*
650 * If changing the inode number size, do it the hard way.
651 */
652#if XFS_BIG_INUMS
653 if (objchange) {
654 return 2;
655 }
656#else
657 ASSERT(objchange == 0);
658#endif
659 /*
660 * If it won't fit at the end then do it the hard way (use the hole).
661 */
662 if (used + size > mp->m_dirblksize)
663 return 2;
664 /*
665 * Do it the easy way.
666 */
667 *sfepp = sfep;
668 *offsetp = offset;
669 return 1;
670}
671
672#ifdef DEBUG
673/*
674 * Check consistency of shortform directory, assert if bad.
675 */
676static void
677xfs_dir2_sf_check(
678 xfs_da_args_t *args) /* operation arguments */
679{
680 xfs_inode_t *dp; /* incore directory inode */
681 int i; /* entry number */
682 int i8count; /* number of big inode#s */
683 xfs_ino_t ino; /* entry inode number */
684 int offset; /* data offset */
685 xfs_dir2_sf_entry_t *sfep; /* shortform dir entry */
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200686 xfs_dir2_sf_hdr_t *sfp; /* shortform structure */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687
688 dp = args->dp;
689
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200690 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
Dave Chinner6b2647a2013-04-03 16:11:24 +1100691 offset = XFS_DIR3_DATA_FIRST_OFFSET(dp->i_mount);
Christoph Hellwig8bc38782011-07-08 14:35:03 +0200692 ino = xfs_dir2_sf_get_parent_ino(sfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 i8count = ino > XFS_DIR2_MAX_SHORT_INUM;
694
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000695 for (i = 0, sfep = xfs_dir2_sf_firstentry(sfp);
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200696 i < sfp->count;
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000697 i++, sfep = xfs_dir2_sf_nextentry(sfp, sfep)) {
698 ASSERT(xfs_dir2_sf_get_offset(sfep) >= offset);
Christoph Hellwig8bc38782011-07-08 14:35:03 +0200699 ino = xfs_dir2_sfe_get_ino(sfp, sfep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 i8count += ino > XFS_DIR2_MAX_SHORT_INUM;
701 offset =
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000702 xfs_dir2_sf_get_offset(sfep) +
703 xfs_dir2_data_entsize(sfep->namelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 }
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200705 ASSERT(i8count == sfp->i8count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 ASSERT(XFS_BIG_INUMS || i8count == 0);
707 ASSERT((char *)sfep - (char *)sfp == dp->i_d.di_size);
708 ASSERT(offset +
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200709 (sfp->count + 2) * (uint)sizeof(xfs_dir2_leaf_entry_t) +
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 (uint)sizeof(xfs_dir2_block_tail_t) <=
711 dp->i_mount->m_dirblksize);
712}
713#endif /* DEBUG */
714
715/*
716 * Create a new (shortform) directory.
717 */
718int /* error, always 0 */
719xfs_dir2_sf_create(
720 xfs_da_args_t *args, /* operation arguments */
721 xfs_ino_t pino) /* parent inode number */
722{
723 xfs_inode_t *dp; /* incore directory inode */
724 int i8count; /* parent inode is an 8-byte number */
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200725 xfs_dir2_sf_hdr_t *sfp; /* shortform structure */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 int size; /* directory size */
727
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000728 trace_xfs_dir2_sf_create(args);
729
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 dp = args->dp;
731
732 ASSERT(dp != NULL);
733 ASSERT(dp->i_d.di_size == 0);
734 /*
735 * If it's currently a zero-length extent file,
736 * convert it to local format.
737 */
738 if (dp->i_d.di_format == XFS_DINODE_FMT_EXTENTS) {
739 dp->i_df.if_flags &= ~XFS_IFEXTENTS; /* just in case */
740 dp->i_d.di_format = XFS_DINODE_FMT_LOCAL;
741 xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE);
742 dp->i_df.if_flags |= XFS_IFINLINE;
743 }
744 ASSERT(dp->i_df.if_flags & XFS_IFINLINE);
745 ASSERT(dp->i_df.if_bytes == 0);
746 i8count = pino > XFS_DIR2_MAX_SHORT_INUM;
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000747 size = xfs_dir2_sf_hdr_size(i8count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 /*
749 * Make a buffer for the data.
750 */
751 xfs_idata_realloc(dp, size, XFS_DATA_FORK);
752 /*
753 * Fill in the header,
754 */
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200755 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
756 sfp->i8count = i8count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 /*
758 * Now can put in the inode number, since i8count is set.
759 */
Christoph Hellwig8bc38782011-07-08 14:35:03 +0200760 xfs_dir2_sf_put_parent_ino(sfp, pino);
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200761 sfp->count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 dp->i_d.di_size = size;
763 xfs_dir2_sf_check(args);
764 xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE | XFS_ILOG_DDATA);
765 return 0;
766}
767
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768/*
769 * Lookup an entry in a shortform directory.
770 * Returns EEXIST if found, ENOENT if not found.
771 */
772int /* error */
773xfs_dir2_sf_lookup(
774 xfs_da_args_t *args) /* operation arguments */
775{
776 xfs_inode_t *dp; /* incore directory inode */
777 int i; /* entry index */
Barry Naujok384f3ce2008-05-21 16:58:22 +1000778 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 xfs_dir2_sf_entry_t *sfep; /* shortform directory entry */
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200780 xfs_dir2_sf_hdr_t *sfp; /* shortform structure */
Barry Naujok5163f952008-05-21 16:41:01 +1000781 enum xfs_dacmp cmp; /* comparison result */
Barry Naujok384f3ce2008-05-21 16:58:22 +1000782 xfs_dir2_sf_entry_t *ci_sfep; /* case-insens. entry */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000784 trace_xfs_dir2_sf_lookup(args);
785
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 xfs_dir2_sf_check(args);
787 dp = args->dp;
788
789 ASSERT(dp->i_df.if_flags & XFS_IFINLINE);
790 /*
791 * Bail out 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(dp->i_mount));
795 return XFS_ERROR(EIO);
796 }
797 ASSERT(dp->i_df.if_bytes == dp->i_d.di_size);
798 ASSERT(dp->i_df.if_u1.if_data != NULL);
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200799 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
800 ASSERT(dp->i_d.di_size >= xfs_dir2_sf_hdr_size(sfp->i8count));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 /*
802 * Special case for .
803 */
804 if (args->namelen == 1 && args->name[0] == '.') {
805 args->inumber = dp->i_ino;
Barry Naujok5163f952008-05-21 16:41:01 +1000806 args->cmpresult = XFS_CMP_EXACT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 return XFS_ERROR(EEXIST);
808 }
809 /*
810 * Special case for ..
811 */
812 if (args->namelen == 2 &&
813 args->name[0] == '.' && args->name[1] == '.') {
Christoph Hellwig8bc38782011-07-08 14:35:03 +0200814 args->inumber = xfs_dir2_sf_get_parent_ino(sfp);
Barry Naujok5163f952008-05-21 16:41:01 +1000815 args->cmpresult = XFS_CMP_EXACT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 return XFS_ERROR(EEXIST);
817 }
818 /*
819 * Loop over all the entries trying to match ours.
820 */
Barry Naujok384f3ce2008-05-21 16:58:22 +1000821 ci_sfep = NULL;
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200822 for (i = 0, sfep = xfs_dir2_sf_firstentry(sfp); i < sfp->count;
Barry Naujok5163f952008-05-21 16:41:01 +1000823 i++, sfep = xfs_dir2_sf_nextentry(sfp, sfep)) {
824 /*
825 * Compare name and if it's an exact match, return the inode
826 * number. If it's the first case-insensitive match, store the
827 * inode number and continue looking for an exact match.
828 */
829 cmp = dp->i_mount->m_dirnameops->compname(args, sfep->name,
830 sfep->namelen);
831 if (cmp != XFS_CMP_DIFFERENT && cmp != args->cmpresult) {
832 args->cmpresult = cmp;
Christoph Hellwig8bc38782011-07-08 14:35:03 +0200833 args->inumber = xfs_dir2_sfe_get_ino(sfp, sfep);
Barry Naujok5163f952008-05-21 16:41:01 +1000834 if (cmp == XFS_CMP_EXACT)
835 return XFS_ERROR(EEXIST);
Barry Naujok384f3ce2008-05-21 16:58:22 +1000836 ci_sfep = sfep;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 }
838 }
Barry Naujok6a178102008-05-21 16:42:05 +1000839 ASSERT(args->op_flags & XFS_DA_OP_OKNOENT);
Barry Naujok5163f952008-05-21 16:41:01 +1000840 /*
841 * Here, we can only be doing a lookup (not a rename or replace).
Barry Naujok384f3ce2008-05-21 16:58:22 +1000842 * If a case-insensitive match was not found, return ENOENT.
Barry Naujok5163f952008-05-21 16:41:01 +1000843 */
Barry Naujok384f3ce2008-05-21 16:58:22 +1000844 if (!ci_sfep)
845 return XFS_ERROR(ENOENT);
846 /* otherwise process the CI match as required by the caller */
847 error = xfs_dir_cilookup_result(args, ci_sfep->name, ci_sfep->namelen);
848 return XFS_ERROR(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849}
850
851/*
852 * Remove an entry from a shortform directory.
853 */
854int /* error */
855xfs_dir2_sf_removename(
856 xfs_da_args_t *args)
857{
858 int byteoff; /* offset of removed entry */
859 xfs_inode_t *dp; /* incore directory inode */
860 int entsize; /* this entry's size */
861 int i; /* shortform entry index */
862 int newsize; /* new inode size */
863 int oldsize; /* old inode size */
864 xfs_dir2_sf_entry_t *sfep; /* shortform directory entry */
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200865 xfs_dir2_sf_hdr_t *sfp; /* shortform structure */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000867 trace_xfs_dir2_sf_removename(args);
868
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 dp = args->dp;
870
871 ASSERT(dp->i_df.if_flags & XFS_IFINLINE);
872 oldsize = (int)dp->i_d.di_size;
873 /*
874 * Bail out if the directory is way too short.
875 */
876 if (oldsize < offsetof(xfs_dir2_sf_hdr_t, parent)) {
877 ASSERT(XFS_FORCED_SHUTDOWN(dp->i_mount));
878 return XFS_ERROR(EIO);
879 }
880 ASSERT(dp->i_df.if_bytes == oldsize);
881 ASSERT(dp->i_df.if_u1.if_data != NULL);
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200882 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
883 ASSERT(oldsize >= xfs_dir2_sf_hdr_size(sfp->i8count));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 /*
885 * Loop over the old directory entries.
886 * Find the one we're deleting.
887 */
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200888 for (i = 0, sfep = xfs_dir2_sf_firstentry(sfp); i < sfp->count;
Barry Naujok5163f952008-05-21 16:41:01 +1000889 i++, sfep = xfs_dir2_sf_nextentry(sfp, sfep)) {
890 if (xfs_da_compname(args, sfep->name, sfep->namelen) ==
891 XFS_CMP_EXACT) {
Christoph Hellwig8bc38782011-07-08 14:35:03 +0200892 ASSERT(xfs_dir2_sfe_get_ino(sfp, sfep) ==
893 args->inumber);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 break;
895 }
896 }
897 /*
898 * Didn't find it.
899 */
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200900 if (i == sfp->count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 return XFS_ERROR(ENOENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 /*
903 * Calculate sizes.
904 */
905 byteoff = (int)((char *)sfep - (char *)sfp);
Christoph Hellwig78f70cd2011-07-08 14:35:19 +0200906 entsize = xfs_dir2_sf_entsize(sfp, args->namelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 newsize = oldsize - entsize;
908 /*
909 * Copy the part if any after the removed entry, sliding it down.
910 */
911 if (byteoff + entsize < oldsize)
912 memmove((char *)sfp + byteoff, (char *)sfp + byteoff + entsize,
913 oldsize - (byteoff + entsize));
914 /*
915 * Fix up the header and file size.
916 */
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200917 sfp->count--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 dp->i_d.di_size = newsize;
919 /*
920 * Reallocate, making it smaller.
921 */
922 xfs_idata_realloc(dp, newsize - oldsize, XFS_DATA_FORK);
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200923 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924#if XFS_BIG_INUMS
925 /*
926 * Are we changing inode number size?
927 */
928 if (args->inumber > XFS_DIR2_MAX_SHORT_INUM) {
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200929 if (sfp->i8count == 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 xfs_dir2_sf_toino4(args);
931 else
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200932 sfp->i8count--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 }
934#endif
935 xfs_dir2_sf_check(args);
936 xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE | XFS_ILOG_DDATA);
937 return 0;
938}
939
940/*
941 * Replace the inode number of an entry in a shortform directory.
942 */
943int /* error */
944xfs_dir2_sf_replace(
945 xfs_da_args_t *args) /* operation arguments */
946{
947 xfs_inode_t *dp; /* incore directory inode */
948 int i; /* entry index */
949#if XFS_BIG_INUMS || defined(DEBUG)
950 xfs_ino_t ino=0; /* entry old inode number */
951#endif
952#if XFS_BIG_INUMS
953 int i8elevated; /* sf_toino8 set i8count=1 */
954#endif
955 xfs_dir2_sf_entry_t *sfep; /* shortform directory entry */
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200956 xfs_dir2_sf_hdr_t *sfp; /* shortform structure */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000958 trace_xfs_dir2_sf_replace(args);
959
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 dp = args->dp;
961
962 ASSERT(dp->i_df.if_flags & XFS_IFINLINE);
963 /*
964 * Bail out if the shortform directory is way too small.
965 */
966 if (dp->i_d.di_size < offsetof(xfs_dir2_sf_hdr_t, parent)) {
967 ASSERT(XFS_FORCED_SHUTDOWN(dp->i_mount));
968 return XFS_ERROR(EIO);
969 }
970 ASSERT(dp->i_df.if_bytes == dp->i_d.di_size);
971 ASSERT(dp->i_df.if_u1.if_data != NULL);
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200972 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
973 ASSERT(dp->i_d.di_size >= xfs_dir2_sf_hdr_size(sfp->i8count));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974#if XFS_BIG_INUMS
975 /*
976 * New inode number is large, and need to convert to 8-byte inodes.
977 */
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200978 if (args->inumber > XFS_DIR2_MAX_SHORT_INUM && sfp->i8count == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 int error; /* error return value */
980 int newsize; /* new inode size */
981
982 newsize =
983 dp->i_df.if_bytes +
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200984 (sfp->count + 1) *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 ((uint)sizeof(xfs_dir2_ino8_t) -
986 (uint)sizeof(xfs_dir2_ino4_t));
987 /*
988 * Won't fit as shortform, convert to block then do replace.
989 */
990 if (newsize > XFS_IFORK_DSIZE(dp)) {
991 error = xfs_dir2_sf_to_block(args);
992 if (error) {
993 return error;
994 }
995 return xfs_dir2_block_replace(args);
996 }
997 /*
998 * Still fits, convert to 8-byte now.
999 */
1000 xfs_dir2_sf_toino8(args);
1001 i8elevated = 1;
Christoph Hellwigac8ba502011-07-08 14:35:13 +02001002 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 } else
1004 i8elevated = 0;
1005#endif
1006 ASSERT(args->namelen != 1 || args->name[0] != '.');
1007 /*
1008 * Replace ..'s entry.
1009 */
1010 if (args->namelen == 2 &&
1011 args->name[0] == '.' && args->name[1] == '.') {
1012#if XFS_BIG_INUMS || defined(DEBUG)
Christoph Hellwig8bc38782011-07-08 14:35:03 +02001013 ino = xfs_dir2_sf_get_parent_ino(sfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 ASSERT(args->inumber != ino);
1015#endif
Christoph Hellwig8bc38782011-07-08 14:35:03 +02001016 xfs_dir2_sf_put_parent_ino(sfp, args->inumber);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 }
1018 /*
1019 * Normal entry, look for the name.
1020 */
1021 else {
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001022 for (i = 0, sfep = xfs_dir2_sf_firstentry(sfp);
Christoph Hellwigac8ba502011-07-08 14:35:13 +02001023 i < sfp->count;
Barry Naujok5163f952008-05-21 16:41:01 +10001024 i++, sfep = xfs_dir2_sf_nextentry(sfp, sfep)) {
1025 if (xfs_da_compname(args, sfep->name, sfep->namelen) ==
1026 XFS_CMP_EXACT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027#if XFS_BIG_INUMS || defined(DEBUG)
Christoph Hellwig8bc38782011-07-08 14:35:03 +02001028 ino = xfs_dir2_sfe_get_ino(sfp, sfep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 ASSERT(args->inumber != ino);
1030#endif
Christoph Hellwig8bc38782011-07-08 14:35:03 +02001031 xfs_dir2_sfe_put_ino(sfp, sfep, args->inumber);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 break;
1033 }
1034 }
1035 /*
1036 * Didn't find it.
1037 */
Christoph Hellwigac8ba502011-07-08 14:35:13 +02001038 if (i == sfp->count) {
Barry Naujok6a178102008-05-21 16:42:05 +10001039 ASSERT(args->op_flags & XFS_DA_OP_OKNOENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040#if XFS_BIG_INUMS
1041 if (i8elevated)
1042 xfs_dir2_sf_toino4(args);
1043#endif
1044 return XFS_ERROR(ENOENT);
1045 }
1046 }
1047#if XFS_BIG_INUMS
1048 /*
1049 * See if the old number was large, the new number is small.
1050 */
1051 if (ino > XFS_DIR2_MAX_SHORT_INUM &&
1052 args->inumber <= XFS_DIR2_MAX_SHORT_INUM) {
1053 /*
1054 * And the old count was one, so need to convert to small.
1055 */
Christoph Hellwigac8ba502011-07-08 14:35:13 +02001056 if (sfp->i8count == 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 xfs_dir2_sf_toino4(args);
1058 else
Christoph Hellwigac8ba502011-07-08 14:35:13 +02001059 sfp->i8count--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 }
1061 /*
1062 * See if the old number was small, the new number is large.
1063 */
1064 if (ino <= XFS_DIR2_MAX_SHORT_INUM &&
1065 args->inumber > XFS_DIR2_MAX_SHORT_INUM) {
1066 /*
1067 * add to the i8count unless we just converted to 8-byte
1068 * inodes (which does an implied i8count = 1)
1069 */
Christoph Hellwigac8ba502011-07-08 14:35:13 +02001070 ASSERT(sfp->i8count != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 if (!i8elevated)
Christoph Hellwigac8ba502011-07-08 14:35:13 +02001072 sfp->i8count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 }
1074#endif
1075 xfs_dir2_sf_check(args);
1076 xfs_trans_log_inode(args->trans, dp, XFS_ILOG_DDATA);
1077 return 0;
1078}
1079
1080#if XFS_BIG_INUMS
1081/*
1082 * Convert from 8-byte inode numbers to 4-byte inode numbers.
1083 * The last 8-byte inode number is gone, but the count is still 1.
1084 */
1085static void
1086xfs_dir2_sf_toino4(
1087 xfs_da_args_t *args) /* operation arguments */
1088{
1089 char *buf; /* old dir's buffer */
1090 xfs_inode_t *dp; /* incore directory inode */
1091 int i; /* entry index */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 int newsize; /* new inode size */
1093 xfs_dir2_sf_entry_t *oldsfep; /* old sf entry */
Christoph Hellwigac8ba502011-07-08 14:35:13 +02001094 xfs_dir2_sf_hdr_t *oldsfp; /* old sf directory */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 int oldsize; /* old inode size */
1096 xfs_dir2_sf_entry_t *sfep; /* new sf entry */
Christoph Hellwigac8ba502011-07-08 14:35:13 +02001097 xfs_dir2_sf_hdr_t *sfp; /* new sf directory */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001099 trace_xfs_dir2_sf_toino4(args);
1100
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 dp = args->dp;
1102
1103 /*
1104 * Copy the old directory to the buffer.
1105 * Then nuke it from the inode, and add the new buffer to the inode.
1106 * Don't want xfs_idata_realloc copying the data here.
1107 */
1108 oldsize = dp->i_df.if_bytes;
1109 buf = kmem_alloc(oldsize, KM_SLEEP);
Christoph Hellwigac8ba502011-07-08 14:35:13 +02001110 oldsfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
1111 ASSERT(oldsfp->i8count == 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 memcpy(buf, oldsfp, oldsize);
1113 /*
1114 * Compute the new inode size.
1115 */
1116 newsize =
1117 oldsize -
Christoph Hellwigac8ba502011-07-08 14:35:13 +02001118 (oldsfp->count + 1) *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 ((uint)sizeof(xfs_dir2_ino8_t) - (uint)sizeof(xfs_dir2_ino4_t));
1120 xfs_idata_realloc(dp, -oldsize, XFS_DATA_FORK);
1121 xfs_idata_realloc(dp, newsize, XFS_DATA_FORK);
1122 /*
1123 * Reset our pointers, the data has moved.
1124 */
Christoph Hellwigac8ba502011-07-08 14:35:13 +02001125 oldsfp = (xfs_dir2_sf_hdr_t *)buf;
1126 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 /*
1128 * Fill in the new header.
1129 */
Christoph Hellwigac8ba502011-07-08 14:35:13 +02001130 sfp->count = oldsfp->count;
1131 sfp->i8count = 0;
Christoph Hellwig8bc38782011-07-08 14:35:03 +02001132 xfs_dir2_sf_put_parent_ino(sfp, xfs_dir2_sf_get_parent_ino(oldsfp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 /*
1134 * Copy the entries field by field.
1135 */
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001136 for (i = 0, sfep = xfs_dir2_sf_firstentry(sfp),
1137 oldsfep = xfs_dir2_sf_firstentry(oldsfp);
Christoph Hellwigac8ba502011-07-08 14:35:13 +02001138 i < sfp->count;
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001139 i++, sfep = xfs_dir2_sf_nextentry(sfp, sfep),
1140 oldsfep = xfs_dir2_sf_nextentry(oldsfp, oldsfep)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 sfep->namelen = oldsfep->namelen;
1142 sfep->offset = oldsfep->offset;
1143 memcpy(sfep->name, oldsfep->name, sfep->namelen);
Christoph Hellwig8bc38782011-07-08 14:35:03 +02001144 xfs_dir2_sfe_put_ino(sfp, sfep,
1145 xfs_dir2_sfe_get_ino(oldsfp, oldsfep));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 }
1147 /*
1148 * Clean up the inode.
1149 */
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10001150 kmem_free(buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 dp->i_d.di_size = newsize;
1152 xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE | XFS_ILOG_DDATA);
1153}
1154
1155/*
1156 * Convert from 4-byte inode numbers to 8-byte inode numbers.
1157 * The new 8-byte inode number is not there yet, we leave with the
1158 * count 1 but no corresponding entry.
1159 */
1160static void
1161xfs_dir2_sf_toino8(
1162 xfs_da_args_t *args) /* operation arguments */
1163{
1164 char *buf; /* old dir's buffer */
1165 xfs_inode_t *dp; /* incore directory inode */
1166 int i; /* entry index */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 int newsize; /* new inode size */
1168 xfs_dir2_sf_entry_t *oldsfep; /* old sf entry */
Christoph Hellwigac8ba502011-07-08 14:35:13 +02001169 xfs_dir2_sf_hdr_t *oldsfp; /* old sf directory */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 int oldsize; /* old inode size */
1171 xfs_dir2_sf_entry_t *sfep; /* new sf entry */
Christoph Hellwigac8ba502011-07-08 14:35:13 +02001172 xfs_dir2_sf_hdr_t *sfp; /* new sf directory */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001174 trace_xfs_dir2_sf_toino8(args);
1175
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 dp = args->dp;
1177
1178 /*
1179 * Copy the old directory to the buffer.
1180 * Then nuke it from the inode, and add the new buffer to the inode.
1181 * Don't want xfs_idata_realloc copying the data here.
1182 */
1183 oldsize = dp->i_df.if_bytes;
1184 buf = kmem_alloc(oldsize, KM_SLEEP);
Christoph Hellwigac8ba502011-07-08 14:35:13 +02001185 oldsfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
1186 ASSERT(oldsfp->i8count == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 memcpy(buf, oldsfp, oldsize);
1188 /*
1189 * Compute the new inode size.
1190 */
1191 newsize =
1192 oldsize +
Christoph Hellwigac8ba502011-07-08 14:35:13 +02001193 (oldsfp->count + 1) *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 ((uint)sizeof(xfs_dir2_ino8_t) - (uint)sizeof(xfs_dir2_ino4_t));
1195 xfs_idata_realloc(dp, -oldsize, XFS_DATA_FORK);
1196 xfs_idata_realloc(dp, newsize, XFS_DATA_FORK);
1197 /*
1198 * Reset our pointers, the data has moved.
1199 */
Christoph Hellwigac8ba502011-07-08 14:35:13 +02001200 oldsfp = (xfs_dir2_sf_hdr_t *)buf;
1201 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 /*
1203 * Fill in the new header.
1204 */
Christoph Hellwigac8ba502011-07-08 14:35:13 +02001205 sfp->count = oldsfp->count;
1206 sfp->i8count = 1;
Christoph Hellwig8bc38782011-07-08 14:35:03 +02001207 xfs_dir2_sf_put_parent_ino(sfp, xfs_dir2_sf_get_parent_ino(oldsfp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208 /*
1209 * Copy the entries field by field.
1210 */
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001211 for (i = 0, sfep = xfs_dir2_sf_firstentry(sfp),
1212 oldsfep = xfs_dir2_sf_firstentry(oldsfp);
Christoph Hellwigac8ba502011-07-08 14:35:13 +02001213 i < sfp->count;
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001214 i++, sfep = xfs_dir2_sf_nextentry(sfp, sfep),
1215 oldsfep = xfs_dir2_sf_nextentry(oldsfp, oldsfep)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 sfep->namelen = oldsfep->namelen;
1217 sfep->offset = oldsfep->offset;
1218 memcpy(sfep->name, oldsfep->name, sfep->namelen);
Christoph Hellwig8bc38782011-07-08 14:35:03 +02001219 xfs_dir2_sfe_put_ino(sfp, sfep,
1220 xfs_dir2_sfe_get_ino(oldsfp, oldsfep));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 }
1222 /*
1223 * Clean up the inode.
1224 */
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10001225 kmem_free(buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 dp->i_d.di_size = newsize;
1227 xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE | XFS_ILOG_DDATA);
1228}
1229#endif /* XFS_BIG_INUMS */