blob: 9d3dff2ae4c519c9895bf9cf46214f9e6a1981e2 [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"
David Chinnerda353b02007-08-28 14:00:13 +100025#include "xfs_ag.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include "xfs_dir2.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#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"
31#include "xfs_dinode.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include "xfs_inode.h"
Nathan Scotta844f452005-11-02 14:38:42 +110033#include "xfs_inode_item.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include "xfs_error.h"
35#include "xfs_dir2_data.h"
36#include "xfs_dir2_leaf.h"
37#include "xfs_dir2_block.h"
Christoph Hellwig0b1b2132009-12-14 23:14:59 +000038#include "xfs_trace.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
40/*
41 * Prototypes for internal functions.
42 */
43static void xfs_dir2_sf_addname_easy(xfs_da_args_t *args,
44 xfs_dir2_sf_entry_t *sfep,
45 xfs_dir2_data_aoff_t offset,
46 int new_isize);
47static void xfs_dir2_sf_addname_hard(xfs_da_args_t *args, int objchange,
48 int new_isize);
49static int xfs_dir2_sf_addname_pick(xfs_da_args_t *args, int objchange,
50 xfs_dir2_sf_entry_t **sfepp,
51 xfs_dir2_data_aoff_t *offsetp);
52#ifdef DEBUG
53static void xfs_dir2_sf_check(xfs_da_args_t *args);
54#else
55#define xfs_dir2_sf_check(args)
56#endif /* DEBUG */
57#if XFS_BIG_INUMS
58static void xfs_dir2_sf_toino4(xfs_da_args_t *args);
59static void xfs_dir2_sf_toino8(xfs_da_args_t *args);
60#endif /* XFS_BIG_INUMS */
61
Christoph Hellwig8bc38782011-07-08 14:35:03 +020062/*
63 * Inode numbers in short-form directories can come in two versions,
64 * either 4 bytes or 8 bytes wide. These helpers deal with the
65 * two forms transparently by looking at the headers i8count field.
Christoph Hellwig218106a2011-07-08 14:35:58 +020066 *
67 * For 64-bit inode number the most significant byte must be zero.
Christoph Hellwig8bc38782011-07-08 14:35:03 +020068 */
69static xfs_ino_t
70xfs_dir2_sf_get_ino(
Christoph Hellwigac8ba502011-07-08 14:35:13 +020071 struct xfs_dir2_sf_hdr *hdr,
Christoph Hellwig8bc38782011-07-08 14:35:03 +020072 xfs_dir2_inou_t *from)
73{
Christoph Hellwigac8ba502011-07-08 14:35:13 +020074 if (hdr->i8count)
Christoph Hellwig218106a2011-07-08 14:35:58 +020075 return get_unaligned_be64(&from->i8.i) & 0x00ffffffffffffffULL;
Christoph Hellwig8bc38782011-07-08 14:35:03 +020076 else
Christoph Hellwig218106a2011-07-08 14:35:58 +020077 return get_unaligned_be32(&from->i4.i);
Christoph Hellwig8bc38782011-07-08 14:35:03 +020078}
79
80static void
81xfs_dir2_sf_put_ino(
Christoph Hellwigac8ba502011-07-08 14:35:13 +020082 struct xfs_dir2_sf_hdr *hdr,
Christoph Hellwig8bc38782011-07-08 14:35:03 +020083 xfs_dir2_inou_t *to,
84 xfs_ino_t ino)
85{
Christoph Hellwig218106a2011-07-08 14:35:58 +020086 ASSERT((ino & 0xff00000000000000ULL) == 0);
87
Christoph Hellwigac8ba502011-07-08 14:35:13 +020088 if (hdr->i8count)
Christoph Hellwig218106a2011-07-08 14:35:58 +020089 put_unaligned_be64(ino, &to->i8.i);
Christoph Hellwig8bc38782011-07-08 14:35:03 +020090 else
Christoph Hellwig218106a2011-07-08 14:35:58 +020091 put_unaligned_be32(ino, &to->i4.i);
Christoph Hellwig8bc38782011-07-08 14:35:03 +020092}
93
94xfs_ino_t
95xfs_dir2_sf_get_parent_ino(
Christoph Hellwigac8ba502011-07-08 14:35:13 +020096 struct xfs_dir2_sf_hdr *hdr)
Christoph Hellwig8bc38782011-07-08 14:35:03 +020097{
Christoph Hellwigac8ba502011-07-08 14:35:13 +020098 return xfs_dir2_sf_get_ino(hdr, &hdr->parent);
Christoph Hellwig8bc38782011-07-08 14:35:03 +020099}
100
101static void
102xfs_dir2_sf_put_parent_ino(
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200103 struct xfs_dir2_sf_hdr *hdr,
Christoph Hellwig8bc38782011-07-08 14:35:03 +0200104 xfs_ino_t ino)
105{
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200106 xfs_dir2_sf_put_ino(hdr, &hdr->parent, ino);
Christoph Hellwig8bc38782011-07-08 14:35:03 +0200107}
108
109/*
110 * In short-form directory entries the inode numbers are stored at variable
111 * offset behind the entry name. The inode numbers may only be accessed
112 * through the helpers below.
113 */
114static xfs_dir2_inou_t *
115xfs_dir2_sfe_inop(
116 struct xfs_dir2_sf_entry *sfep)
117{
118 return (xfs_dir2_inou_t *)&sfep->name[sfep->namelen];
119}
120
121xfs_ino_t
122xfs_dir2_sfe_get_ino(
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200123 struct xfs_dir2_sf_hdr *hdr,
Christoph Hellwig8bc38782011-07-08 14:35:03 +0200124 struct xfs_dir2_sf_entry *sfep)
125{
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200126 return xfs_dir2_sf_get_ino(hdr, xfs_dir2_sfe_inop(sfep));
Christoph Hellwig8bc38782011-07-08 14:35:03 +0200127}
128
129static void
130xfs_dir2_sfe_put_ino(
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200131 struct xfs_dir2_sf_hdr *hdr,
Christoph Hellwig8bc38782011-07-08 14:35:03 +0200132 struct xfs_dir2_sf_entry *sfep,
133 xfs_ino_t ino)
134{
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200135 xfs_dir2_sf_put_ino(hdr, xfs_dir2_sfe_inop(sfep), ino);
Christoph Hellwig8bc38782011-07-08 14:35:03 +0200136}
137
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138/*
139 * Given a block directory (dp/block), calculate its size as a shortform (sf)
140 * directory and a header for the sf directory, if it will fit it the
141 * space currently present in the inode. If it won't fit, the output
142 * size is too big (but not accurate).
143 */
144int /* size for sf form */
145xfs_dir2_block_sfsize(
146 xfs_inode_t *dp, /* incore inode pointer */
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +0200147 xfs_dir2_data_hdr_t *hdr, /* block directory data */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 xfs_dir2_sf_hdr_t *sfhp) /* output: header for sf form */
149{
150 xfs_dir2_dataptr_t addr; /* data entry address */
151 xfs_dir2_leaf_entry_t *blp; /* leaf area of the block */
152 xfs_dir2_block_tail_t *btp; /* tail area of the block */
153 int count; /* shortform entry count */
154 xfs_dir2_data_entry_t *dep; /* data entry in the block */
155 int i; /* block entry index */
156 int i8count; /* count of big-inode entries */
157 int isdot; /* entry is "." */
158 int isdotdot; /* entry is ".." */
159 xfs_mount_t *mp; /* mount structure pointer */
160 int namelen; /* total name bytes */
Christoph Hellwig5bde1ba92005-11-02 15:06:18 +1100161 xfs_ino_t parent = 0; /* parent inode number */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 int size=0; /* total computed size */
163
164 mp = dp->i_mount;
165
166 count = i8count = namelen = 0;
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +0200167 btp = xfs_dir2_block_tail_p(mp, hdr);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000168 blp = xfs_dir2_block_leaf_p(btp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169
170 /*
171 * Iterate over the block's data entries by using the leaf pointers.
172 */
Nathan Scotte922fff2006-03-17 17:27:56 +1100173 for (i = 0; i < be32_to_cpu(btp->count); i++) {
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100174 if ((addr = be32_to_cpu(blp[i].address)) == XFS_DIR2_NULL_DATAPTR)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 continue;
176 /*
177 * Calculate the pointer to the entry at hand.
178 */
179 dep = (xfs_dir2_data_entry_t *)
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +0200180 ((char *)hdr + xfs_dir2_dataptr_to_off(mp, addr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 /*
182 * Detect . and .., so we can special-case them.
183 * . is not included in sf directories.
184 * .. is included by just the parent inode number.
185 */
186 isdot = dep->namelen == 1 && dep->name[0] == '.';
187 isdotdot =
188 dep->namelen == 2 &&
189 dep->name[0] == '.' && dep->name[1] == '.';
190#if XFS_BIG_INUMS
191 if (!isdot)
Christoph Hellwigff9901c2006-06-09 14:48:37 +1000192 i8count += be64_to_cpu(dep->inumber) > XFS_DIR2_MAX_SHORT_INUM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193#endif
194 if (!isdot && !isdotdot) {
195 count++;
196 namelen += dep->namelen;
197 } else if (isdotdot)
Christoph Hellwigff9901c2006-06-09 14:48:37 +1000198 parent = be64_to_cpu(dep->inumber);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 /*
200 * Calculate the new size, see if we should give up yet.
201 */
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000202 size = xfs_dir2_sf_hdr_size(i8count) + /* header */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 count + /* namelen */
204 count * (uint)sizeof(xfs_dir2_sf_off_t) + /* offset */
205 namelen + /* name */
206 (i8count ? /* inumber */
207 (uint)sizeof(xfs_dir2_ino8_t) * count :
208 (uint)sizeof(xfs_dir2_ino4_t) * count);
209 if (size > XFS_IFORK_DSIZE(dp))
210 return size; /* size value is a failure */
211 }
212 /*
213 * Create the output header, if it worked.
214 */
215 sfhp->count = count;
216 sfhp->i8count = i8count;
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200217 xfs_dir2_sf_put_parent_ino(sfhp, parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 return size;
219}
220
221/*
222 * Convert a block format directory to shortform.
223 * Caller has already checked that it will fit, and built us a header.
224 */
225int /* error */
226xfs_dir2_block_to_sf(
227 xfs_da_args_t *args, /* operation arguments */
228 xfs_dabuf_t *bp, /* block buffer */
229 int size, /* shortform directory size */
230 xfs_dir2_sf_hdr_t *sfhp) /* shortform directory hdr */
231{
Christoph Hellwiga64b0412011-07-08 14:35:32 +0200232 xfs_dir2_data_hdr_t *hdr; /* block header */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 xfs_dir2_block_tail_t *btp; /* block tail pointer */
234 xfs_dir2_data_entry_t *dep; /* data entry pointer */
235 xfs_inode_t *dp; /* incore directory inode */
236 xfs_dir2_data_unused_t *dup; /* unused data pointer */
237 char *endptr; /* end of data entries */
238 int error; /* error return value */
239 int logflags; /* inode logging flags */
240 xfs_mount_t *mp; /* filesystem mount point */
241 char *ptr; /* current data pointer */
242 xfs_dir2_sf_entry_t *sfep; /* shortform entry */
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200243 xfs_dir2_sf_hdr_t *sfp; /* shortform directory header */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000245 trace_xfs_dir2_block_to_sf(args);
246
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 dp = args->dp;
248 mp = dp->i_mount;
249
250 /*
251 * Make a copy of the block data, so we can shrink the inode
252 * and add local data.
253 */
Christoph Hellwiga64b0412011-07-08 14:35:32 +0200254 hdr = kmem_alloc(mp->m_dirblksize, KM_SLEEP);
255 memcpy(hdr, bp->data, mp->m_dirblksize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 logflags = XFS_ILOG_CORE;
257 if ((error = xfs_dir2_shrink_inode(args, mp->m_dirdatablk, bp))) {
258 ASSERT(error != ENOSPC);
259 goto out;
260 }
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +0200261
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 /*
263 * The buffer is now unconditionally gone, whether
264 * xfs_dir2_shrink_inode worked or not.
265 *
266 * Convert the inode to local format.
267 */
268 dp->i_df.if_flags &= ~XFS_IFEXTENTS;
269 dp->i_df.if_flags |= XFS_IFINLINE;
270 dp->i_d.di_format = XFS_DINODE_FMT_LOCAL;
271 ASSERT(dp->i_df.if_bytes == 0);
272 xfs_idata_realloc(dp, size, XFS_DATA_FORK);
273 logflags |= XFS_ILOG_DDATA;
274 /*
275 * Copy the header into the newly allocate local space.
276 */
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200277 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000278 memcpy(sfp, sfhp, xfs_dir2_sf_hdr_size(sfhp->i8count));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 dp->i_d.di_size = size;
280 /*
281 * Set up to loop over the block's entries.
282 */
Christoph Hellwiga64b0412011-07-08 14:35:32 +0200283 btp = xfs_dir2_block_tail_p(mp, hdr);
284 ptr = (char *)(hdr + 1);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000285 endptr = (char *)xfs_dir2_block_leaf_p(btp);
286 sfep = xfs_dir2_sf_firstentry(sfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 /*
288 * Loop over the active and unused entries.
289 * Stop when we reach the leaf/tail portion of the block.
290 */
291 while (ptr < endptr) {
292 /*
293 * If it's unused, just skip over it.
294 */
295 dup = (xfs_dir2_data_unused_t *)ptr;
Nathan Scottad354eb2006-03-17 17:27:37 +1100296 if (be16_to_cpu(dup->freetag) == XFS_DIR2_DATA_FREE_TAG) {
297 ptr += be16_to_cpu(dup->length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 continue;
299 }
300 dep = (xfs_dir2_data_entry_t *)ptr;
301 /*
302 * Skip .
303 */
304 if (dep->namelen == 1 && dep->name[0] == '.')
Christoph Hellwigff9901c2006-06-09 14:48:37 +1000305 ASSERT(be64_to_cpu(dep->inumber) == dp->i_ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 /*
307 * Skip .., but make sure the inode number is right.
308 */
309 else if (dep->namelen == 2 &&
310 dep->name[0] == '.' && dep->name[1] == '.')
Christoph Hellwigff9901c2006-06-09 14:48:37 +1000311 ASSERT(be64_to_cpu(dep->inumber) ==
Christoph Hellwig8bc38782011-07-08 14:35:03 +0200312 xfs_dir2_sf_get_parent_ino(sfp));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 /*
314 * Normal entry, copy it into shortform.
315 */
316 else {
317 sfep->namelen = dep->namelen;
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000318 xfs_dir2_sf_put_offset(sfep,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 (xfs_dir2_data_aoff_t)
Christoph Hellwiga64b0412011-07-08 14:35:32 +0200320 ((char *)dep - (char *)hdr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 memcpy(sfep->name, dep->name, dep->namelen);
Christoph Hellwig8bc38782011-07-08 14:35:03 +0200322 xfs_dir2_sfe_put_ino(sfp, sfep,
323 be64_to_cpu(dep->inumber));
324
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000325 sfep = xfs_dir2_sf_nextentry(sfp, sfep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 }
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000327 ptr += xfs_dir2_data_entsize(dep->namelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 }
329 ASSERT((char *)sfep - (char *)sfp == size);
330 xfs_dir2_sf_check(args);
331out:
332 xfs_trans_log_inode(args->trans, dp, logflags);
Christoph Hellwiga64b0412011-07-08 14:35:32 +0200333 kmem_free(hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 return error;
335}
336
337/*
338 * Add a name to a shortform directory.
339 * There are two algorithms, "easy" and "hard" which we decide on
340 * before changing anything.
341 * Convert to block form if necessary, if the new entry won't fit.
342 */
343int /* error */
344xfs_dir2_sf_addname(
345 xfs_da_args_t *args) /* operation arguments */
346{
347 int add_entsize; /* size of the new entry */
348 xfs_inode_t *dp; /* incore directory inode */
349 int error; /* error return value */
350 int incr_isize; /* total change in size */
351 int new_isize; /* di_size after adding name */
352 int objchange; /* changing to 8-byte inodes */
Christoph Hellwig5bde1ba92005-11-02 15:06:18 +1100353 xfs_dir2_data_aoff_t offset = 0; /* offset for new entry */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 int old_isize; /* di_size before adding name */
355 int pick; /* which algorithm to use */
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200356 xfs_dir2_sf_hdr_t *sfp; /* shortform structure */
Christoph Hellwig5bde1ba92005-11-02 15:06:18 +1100357 xfs_dir2_sf_entry_t *sfep = NULL; /* shortform entry */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000359 trace_xfs_dir2_sf_addname(args);
360
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 ASSERT(xfs_dir2_sf_lookup(args) == ENOENT);
362 dp = args->dp;
363 ASSERT(dp->i_df.if_flags & XFS_IFINLINE);
364 /*
365 * Make sure the shortform value has some of its header.
366 */
367 if (dp->i_d.di_size < offsetof(xfs_dir2_sf_hdr_t, parent)) {
368 ASSERT(XFS_FORCED_SHUTDOWN(dp->i_mount));
369 return XFS_ERROR(EIO);
370 }
371 ASSERT(dp->i_df.if_bytes == dp->i_d.di_size);
372 ASSERT(dp->i_df.if_u1.if_data != NULL);
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200373 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
374 ASSERT(dp->i_d.di_size >= xfs_dir2_sf_hdr_size(sfp->i8count));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 /*
376 * Compute entry (and change in) size.
377 */
Christoph Hellwig78f70cd2011-07-08 14:35:19 +0200378 add_entsize = xfs_dir2_sf_entsize(sfp, args->namelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 incr_isize = add_entsize;
380 objchange = 0;
381#if XFS_BIG_INUMS
382 /*
383 * Do we have to change to 8 byte inodes?
384 */
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200385 if (args->inumber > XFS_DIR2_MAX_SHORT_INUM && sfp->i8count == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 /*
387 * Yes, adjust the entry size and the total size.
388 */
389 add_entsize +=
390 (uint)sizeof(xfs_dir2_ino8_t) -
391 (uint)sizeof(xfs_dir2_ino4_t);
392 incr_isize +=
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200393 (sfp->count + 2) *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 ((uint)sizeof(xfs_dir2_ino8_t) -
395 (uint)sizeof(xfs_dir2_ino4_t));
396 objchange = 1;
397 }
398#endif
399 old_isize = (int)dp->i_d.di_size;
400 new_isize = old_isize + incr_isize;
401 /*
402 * Won't fit as shortform any more (due to size),
403 * or the pick routine says it won't (due to offset values).
404 */
405 if (new_isize > XFS_IFORK_DSIZE(dp) ||
406 (pick =
407 xfs_dir2_sf_addname_pick(args, objchange, &sfep, &offset)) == 0) {
408 /*
409 * Just checking or no space reservation, it doesn't fit.
410 */
Barry Naujok6a178102008-05-21 16:42:05 +1000411 if ((args->op_flags & XFS_DA_OP_JUSTCHECK) || args->total == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 return XFS_ERROR(ENOSPC);
413 /*
414 * Convert to block form then add the name.
415 */
416 error = xfs_dir2_sf_to_block(args);
417 if (error)
418 return error;
419 return xfs_dir2_block_addname(args);
420 }
421 /*
422 * Just checking, it fits.
423 */
Barry Naujok6a178102008-05-21 16:42:05 +1000424 if (args->op_flags & XFS_DA_OP_JUSTCHECK)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 return 0;
426 /*
427 * Do it the easy way - just add it at the end.
428 */
429 if (pick == 1)
430 xfs_dir2_sf_addname_easy(args, sfep, offset, new_isize);
431 /*
432 * Do it the hard way - look for a place to insert the new entry.
433 * Convert to 8 byte inode numbers first if necessary.
434 */
435 else {
436 ASSERT(pick == 2);
437#if XFS_BIG_INUMS
438 if (objchange)
439 xfs_dir2_sf_toino8(args);
440#endif
441 xfs_dir2_sf_addname_hard(args, objchange, new_isize);
442 }
443 xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE | XFS_ILOG_DDATA);
444 return 0;
445}
446
447/*
448 * Add the new entry the "easy" way.
449 * This is copying the old directory and adding the new entry at the end.
450 * Since it's sorted by "offset" we need room after the last offset
451 * that's already there, and then room to convert to a block directory.
452 * This is already checked by the pick routine.
453 */
454static void
455xfs_dir2_sf_addname_easy(
456 xfs_da_args_t *args, /* operation arguments */
457 xfs_dir2_sf_entry_t *sfep, /* pointer to new entry */
458 xfs_dir2_data_aoff_t offset, /* offset to use for new ent */
459 int new_isize) /* new directory size */
460{
461 int byteoff; /* byte offset in sf dir */
462 xfs_inode_t *dp; /* incore directory inode */
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200463 xfs_dir2_sf_hdr_t *sfp; /* shortform structure */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464
465 dp = args->dp;
466
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200467 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 byteoff = (int)((char *)sfep - (char *)sfp);
469 /*
470 * Grow the in-inode space.
471 */
Christoph Hellwig78f70cd2011-07-08 14:35:19 +0200472 xfs_idata_realloc(dp, xfs_dir2_sf_entsize(sfp, args->namelen),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 XFS_DATA_FORK);
474 /*
475 * Need to set up again due to realloc of the inode data.
476 */
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200477 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 sfep = (xfs_dir2_sf_entry_t *)((char *)sfp + byteoff);
479 /*
480 * Fill in the new entry.
481 */
482 sfep->namelen = args->namelen;
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000483 xfs_dir2_sf_put_offset(sfep, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 memcpy(sfep->name, args->name, sfep->namelen);
Christoph Hellwig8bc38782011-07-08 14:35:03 +0200485 xfs_dir2_sfe_put_ino(sfp, sfep, args->inumber);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 /*
487 * Update the header and inode.
488 */
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200489 sfp->count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490#if XFS_BIG_INUMS
491 if (args->inumber > XFS_DIR2_MAX_SHORT_INUM)
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200492 sfp->i8count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493#endif
494 dp->i_d.di_size = new_isize;
495 xfs_dir2_sf_check(args);
496}
497
498/*
499 * Add the new entry the "hard" way.
500 * The caller has already converted to 8 byte inode numbers if necessary,
501 * in which case we need to leave the i8count at 1.
502 * Find a hole that the new entry will fit into, and copy
503 * the first part of the entries, the new entry, and the last part of
504 * the entries.
505 */
506/* ARGSUSED */
507static void
508xfs_dir2_sf_addname_hard(
509 xfs_da_args_t *args, /* operation arguments */
510 int objchange, /* changing inode number size */
511 int new_isize) /* new directory size */
512{
513 int add_datasize; /* data size need for new ent */
514 char *buf; /* buffer for old */
515 xfs_inode_t *dp; /* incore directory inode */
516 int eof; /* reached end of old dir */
517 int nbytes; /* temp for byte copies */
518 xfs_dir2_data_aoff_t new_offset; /* next offset value */
519 xfs_dir2_data_aoff_t offset; /* current offset value */
520 int old_isize; /* previous di_size */
521 xfs_dir2_sf_entry_t *oldsfep; /* entry in original dir */
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200522 xfs_dir2_sf_hdr_t *oldsfp; /* original shortform dir */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 xfs_dir2_sf_entry_t *sfep; /* entry in new dir */
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200524 xfs_dir2_sf_hdr_t *sfp; /* new shortform dir */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525
526 /*
527 * Copy the old directory to the stack buffer.
528 */
529 dp = args->dp;
530
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200531 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 old_isize = (int)dp->i_d.di_size;
533 buf = kmem_alloc(old_isize, KM_SLEEP);
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200534 oldsfp = (xfs_dir2_sf_hdr_t *)buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 memcpy(oldsfp, sfp, old_isize);
536 /*
537 * Loop over the old directory finding the place we're going
538 * to insert the new entry.
539 * If it's going to end up at the end then oldsfep will point there.
540 */
541 for (offset = XFS_DIR2_DATA_FIRST_OFFSET,
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000542 oldsfep = xfs_dir2_sf_firstentry(oldsfp),
543 add_datasize = xfs_dir2_data_entsize(args->namelen),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 eof = (char *)oldsfep == &buf[old_isize];
545 !eof;
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000546 offset = new_offset + xfs_dir2_data_entsize(oldsfep->namelen),
547 oldsfep = xfs_dir2_sf_nextentry(oldsfp, oldsfep),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 eof = (char *)oldsfep == &buf[old_isize]) {
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000549 new_offset = xfs_dir2_sf_get_offset(oldsfep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 if (offset + add_datasize <= new_offset)
551 break;
552 }
553 /*
554 * Get rid of the old directory, then allocate space for
555 * the new one. We do this so xfs_idata_realloc won't copy
556 * the data.
557 */
558 xfs_idata_realloc(dp, -old_isize, XFS_DATA_FORK);
559 xfs_idata_realloc(dp, new_isize, XFS_DATA_FORK);
560 /*
561 * Reset the pointer since the buffer was reallocated.
562 */
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200563 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 /*
565 * Copy the first part of the directory, including the header.
566 */
567 nbytes = (int)((char *)oldsfep - (char *)oldsfp);
568 memcpy(sfp, oldsfp, nbytes);
569 sfep = (xfs_dir2_sf_entry_t *)((char *)sfp + nbytes);
570 /*
571 * Fill in the new entry, and update the header counts.
572 */
573 sfep->namelen = args->namelen;
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000574 xfs_dir2_sf_put_offset(sfep, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 memcpy(sfep->name, args->name, sfep->namelen);
Christoph Hellwig8bc38782011-07-08 14:35:03 +0200576 xfs_dir2_sfe_put_ino(sfp, sfep, args->inumber);
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200577 sfp->count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578#if XFS_BIG_INUMS
579 if (args->inumber > XFS_DIR2_MAX_SHORT_INUM && !objchange)
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200580 sfp->i8count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581#endif
582 /*
583 * If there's more left to copy, do that.
584 */
585 if (!eof) {
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000586 sfep = xfs_dir2_sf_nextentry(sfp, sfep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 memcpy(sfep, oldsfep, old_isize - nbytes);
588 }
Denys Vlasenkof0e2d932008-05-19 16:31:57 +1000589 kmem_free(buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 dp->i_d.di_size = new_isize;
591 xfs_dir2_sf_check(args);
592}
593
594/*
595 * Decide if the new entry will fit at all.
596 * If it will fit, pick between adding the new entry to the end (easy)
597 * or somewhere else (hard).
598 * Return 0 (won't fit), 1 (easy), 2 (hard).
599 */
600/*ARGSUSED*/
601static int /* pick result */
602xfs_dir2_sf_addname_pick(
603 xfs_da_args_t *args, /* operation arguments */
604 int objchange, /* inode # size changes */
605 xfs_dir2_sf_entry_t **sfepp, /* out(1): new entry ptr */
606 xfs_dir2_data_aoff_t *offsetp) /* out(1): new offset */
607{
608 xfs_inode_t *dp; /* incore directory inode */
609 int holefit; /* found hole it will fit in */
610 int i; /* entry number */
611 xfs_mount_t *mp; /* filesystem mount point */
612 xfs_dir2_data_aoff_t offset; /* data block offset */
613 xfs_dir2_sf_entry_t *sfep; /* shortform entry */
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200614 xfs_dir2_sf_hdr_t *sfp; /* shortform structure */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 int size; /* entry's data size */
616 int used; /* data bytes used */
617
618 dp = args->dp;
619 mp = dp->i_mount;
620
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200621 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000622 size = xfs_dir2_data_entsize(args->namelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 offset = XFS_DIR2_DATA_FIRST_OFFSET;
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000624 sfep = xfs_dir2_sf_firstentry(sfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 holefit = 0;
626 /*
627 * Loop over sf entries.
628 * Keep track of data offset and whether we've seen a place
629 * to insert the new entry.
630 */
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200631 for (i = 0; i < sfp->count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 if (!holefit)
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000633 holefit = offset + size <= xfs_dir2_sf_get_offset(sfep);
634 offset = xfs_dir2_sf_get_offset(sfep) +
635 xfs_dir2_data_entsize(sfep->namelen);
636 sfep = xfs_dir2_sf_nextentry(sfp, sfep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 }
638 /*
639 * Calculate data bytes used excluding the new entry, if this
640 * was a data block (block form directory).
641 */
642 used = offset +
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200643 (sfp->count + 3) * (uint)sizeof(xfs_dir2_leaf_entry_t) +
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 (uint)sizeof(xfs_dir2_block_tail_t);
645 /*
646 * If it won't fit in a block form then we can't insert it,
647 * we'll go back, convert to block, then try the insert and convert
648 * to leaf.
649 */
650 if (used + (holefit ? 0 : size) > mp->m_dirblksize)
651 return 0;
652 /*
653 * If changing the inode number size, do it the hard way.
654 */
655#if XFS_BIG_INUMS
656 if (objchange) {
657 return 2;
658 }
659#else
660 ASSERT(objchange == 0);
661#endif
662 /*
663 * If it won't fit at the end then do it the hard way (use the hole).
664 */
665 if (used + size > mp->m_dirblksize)
666 return 2;
667 /*
668 * Do it the easy way.
669 */
670 *sfepp = sfep;
671 *offsetp = offset;
672 return 1;
673}
674
675#ifdef DEBUG
676/*
677 * Check consistency of shortform directory, assert if bad.
678 */
679static void
680xfs_dir2_sf_check(
681 xfs_da_args_t *args) /* operation arguments */
682{
683 xfs_inode_t *dp; /* incore directory inode */
684 int i; /* entry number */
685 int i8count; /* number of big inode#s */
686 xfs_ino_t ino; /* entry inode number */
687 int offset; /* data offset */
688 xfs_dir2_sf_entry_t *sfep; /* shortform dir entry */
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200689 xfs_dir2_sf_hdr_t *sfp; /* shortform structure */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690
691 dp = args->dp;
692
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200693 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 offset = XFS_DIR2_DATA_FIRST_OFFSET;
Christoph Hellwig8bc38782011-07-08 14:35:03 +0200695 ino = xfs_dir2_sf_get_parent_ino(sfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 i8count = ino > XFS_DIR2_MAX_SHORT_INUM;
697
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000698 for (i = 0, sfep = xfs_dir2_sf_firstentry(sfp);
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200699 i < sfp->count;
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000700 i++, sfep = xfs_dir2_sf_nextentry(sfp, sfep)) {
701 ASSERT(xfs_dir2_sf_get_offset(sfep) >= offset);
Christoph Hellwig8bc38782011-07-08 14:35:03 +0200702 ino = xfs_dir2_sfe_get_ino(sfp, sfep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 i8count += ino > XFS_DIR2_MAX_SHORT_INUM;
704 offset =
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000705 xfs_dir2_sf_get_offset(sfep) +
706 xfs_dir2_data_entsize(sfep->namelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 }
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200708 ASSERT(i8count == sfp->i8count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 ASSERT(XFS_BIG_INUMS || i8count == 0);
710 ASSERT((char *)sfep - (char *)sfp == dp->i_d.di_size);
711 ASSERT(offset +
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200712 (sfp->count + 2) * (uint)sizeof(xfs_dir2_leaf_entry_t) +
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 (uint)sizeof(xfs_dir2_block_tail_t) <=
714 dp->i_mount->m_dirblksize);
715}
716#endif /* DEBUG */
717
718/*
719 * Create a new (shortform) directory.
720 */
721int /* error, always 0 */
722xfs_dir2_sf_create(
723 xfs_da_args_t *args, /* operation arguments */
724 xfs_ino_t pino) /* parent inode number */
725{
726 xfs_inode_t *dp; /* incore directory inode */
727 int i8count; /* parent inode is an 8-byte number */
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200728 xfs_dir2_sf_hdr_t *sfp; /* shortform structure */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 int size; /* directory size */
730
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000731 trace_xfs_dir2_sf_create(args);
732
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 dp = args->dp;
734
735 ASSERT(dp != NULL);
736 ASSERT(dp->i_d.di_size == 0);
737 /*
738 * If it's currently a zero-length extent file,
739 * convert it to local format.
740 */
741 if (dp->i_d.di_format == XFS_DINODE_FMT_EXTENTS) {
742 dp->i_df.if_flags &= ~XFS_IFEXTENTS; /* just in case */
743 dp->i_d.di_format = XFS_DINODE_FMT_LOCAL;
744 xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE);
745 dp->i_df.if_flags |= XFS_IFINLINE;
746 }
747 ASSERT(dp->i_df.if_flags & XFS_IFINLINE);
748 ASSERT(dp->i_df.if_bytes == 0);
749 i8count = pino > XFS_DIR2_MAX_SHORT_INUM;
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000750 size = xfs_dir2_sf_hdr_size(i8count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 /*
752 * Make a buffer for the data.
753 */
754 xfs_idata_realloc(dp, size, XFS_DATA_FORK);
755 /*
756 * Fill in the header,
757 */
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200758 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
759 sfp->i8count = i8count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 /*
761 * Now can put in the inode number, since i8count is set.
762 */
Christoph Hellwig8bc38782011-07-08 14:35:03 +0200763 xfs_dir2_sf_put_parent_ino(sfp, pino);
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200764 sfp->count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 dp->i_d.di_size = size;
766 xfs_dir2_sf_check(args);
767 xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE | XFS_ILOG_DDATA);
768 return 0;
769}
770
771int /* error */
772xfs_dir2_sf_getdents(
773 xfs_inode_t *dp, /* incore directory inode */
Christoph Hellwig051e7cd2007-08-28 13:58:24 +1000774 void *dirent,
775 xfs_off_t *offset,
776 filldir_t filldir)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 int i; /* shortform entry number */
779 xfs_mount_t *mp; /* filesystem mount point */
780 xfs_dir2_dataptr_t off; /* current entry's offset */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 xfs_dir2_sf_entry_t *sfep; /* shortform directory entry */
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200782 xfs_dir2_sf_hdr_t *sfp; /* shortform structure */
Christoph Hellwig051e7cd2007-08-28 13:58:24 +1000783 xfs_dir2_dataptr_t dot_offset;
784 xfs_dir2_dataptr_t dotdot_offset;
785 xfs_ino_t ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786
787 mp = dp->i_mount;
788
789 ASSERT(dp->i_df.if_flags & XFS_IFINLINE);
790 /*
791 * Give up if the directory is way too short.
792 */
793 if (dp->i_d.di_size < offsetof(xfs_dir2_sf_hdr_t, parent)) {
794 ASSERT(XFS_FORCED_SHUTDOWN(mp));
795 return XFS_ERROR(EIO);
796 }
797
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 ASSERT(dp->i_df.if_bytes == dp->i_d.di_size);
799 ASSERT(dp->i_df.if_u1.if_data != NULL);
800
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200801 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200803 ASSERT(dp->i_d.di_size >= xfs_dir2_sf_hdr_size(sfp->i8count));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804
805 /*
806 * If the block number in the offset is out of range, we're done.
807 */
Christoph Hellwig051e7cd2007-08-28 13:58:24 +1000808 if (xfs_dir2_dataptr_to_db(mp, *offset) > mp->m_dirdatablk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810
811 /*
Christoph Hellwig051e7cd2007-08-28 13:58:24 +1000812 * Precalculate offsets for . and .. as we will always need them.
813 *
814 * XXX(hch): the second argument is sometimes 0 and sometimes
815 * mp->m_dirdatablk.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 */
Christoph Hellwig051e7cd2007-08-28 13:58:24 +1000817 dot_offset = xfs_dir2_db_off_to_dataptr(mp, mp->m_dirdatablk,
818 XFS_DIR2_DATA_DOT_OFFSET);
819 dotdot_offset = xfs_dir2_db_off_to_dataptr(mp, mp->m_dirdatablk,
820 XFS_DIR2_DATA_DOTDOT_OFFSET);
821
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 /*
823 * Put . entry unless we're starting past it.
824 */
Christoph Hellwig051e7cd2007-08-28 13:58:24 +1000825 if (*offset <= dot_offset) {
Christoph Hellwiga19d9f82009-03-29 09:51:08 +0200826 if (filldir(dirent, ".", 1, dot_offset & 0x7fffffff, dp->i_ino, DT_DIR)) {
Christoph Hellwig15440312009-01-08 14:00:00 -0500827 *offset = dot_offset & 0x7fffffff;
Christoph Hellwig051e7cd2007-08-28 13:58:24 +1000828 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 }
830 }
831
832 /*
833 * Put .. entry unless we're starting past it.
834 */
Christoph Hellwig051e7cd2007-08-28 13:58:24 +1000835 if (*offset <= dotdot_offset) {
Christoph Hellwig8bc38782011-07-08 14:35:03 +0200836 ino = xfs_dir2_sf_get_parent_ino(sfp);
Christoph Hellwig15440312009-01-08 14:00:00 -0500837 if (filldir(dirent, "..", 2, dotdot_offset & 0x7fffffff, ino, DT_DIR)) {
838 *offset = dotdot_offset & 0x7fffffff;
Christoph Hellwig051e7cd2007-08-28 13:58:24 +1000839 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 }
841 }
842
843 /*
844 * Loop while there are more entries and put'ing works.
845 */
Christoph Hellwig051e7cd2007-08-28 13:58:24 +1000846 sfep = xfs_dir2_sf_firstentry(sfp);
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200847 for (i = 0; i < sfp->count; i++) {
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000848 off = xfs_dir2_db_off_to_dataptr(mp, mp->m_dirdatablk,
849 xfs_dir2_sf_get_offset(sfep));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850
Christoph Hellwig051e7cd2007-08-28 13:58:24 +1000851 if (*offset > off) {
852 sfep = xfs_dir2_sf_nextentry(sfp, sfep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 }
Christoph Hellwig051e7cd2007-08-28 13:58:24 +1000855
Christoph Hellwig8bc38782011-07-08 14:35:03 +0200856 ino = xfs_dir2_sfe_get_ino(sfp, sfep);
Dave Chinner4a24cb72010-01-20 10:48:05 +1100857 if (filldir(dirent, (char *)sfep->name, sfep->namelen,
Christoph Hellwig15440312009-01-08 14:00:00 -0500858 off & 0x7fffffff, ino, DT_UNKNOWN)) {
859 *offset = off & 0x7fffffff;
Christoph Hellwig051e7cd2007-08-28 13:58:24 +1000860 return 0;
861 }
862 sfep = xfs_dir2_sf_nextentry(sfp, sfep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 }
864
Christoph Hellwig15440312009-01-08 14:00:00 -0500865 *offset = xfs_dir2_db_off_to_dataptr(mp, mp->m_dirdatablk + 1, 0) &
866 0x7fffffff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 return 0;
868}
869
870/*
871 * Lookup an entry in a shortform directory.
872 * Returns EEXIST if found, ENOENT if not found.
873 */
874int /* error */
875xfs_dir2_sf_lookup(
876 xfs_da_args_t *args) /* operation arguments */
877{
878 xfs_inode_t *dp; /* incore directory inode */
879 int i; /* entry index */
Barry Naujok384f3ce2008-05-21 16:58:22 +1000880 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 xfs_dir2_sf_entry_t *sfep; /* shortform directory entry */
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200882 xfs_dir2_sf_hdr_t *sfp; /* shortform structure */
Barry Naujok5163f952008-05-21 16:41:01 +1000883 enum xfs_dacmp cmp; /* comparison result */
Barry Naujok384f3ce2008-05-21 16:58:22 +1000884 xfs_dir2_sf_entry_t *ci_sfep; /* case-insens. entry */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000886 trace_xfs_dir2_sf_lookup(args);
887
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 xfs_dir2_sf_check(args);
889 dp = args->dp;
890
891 ASSERT(dp->i_df.if_flags & XFS_IFINLINE);
892 /*
893 * Bail out if the directory is way too short.
894 */
895 if (dp->i_d.di_size < offsetof(xfs_dir2_sf_hdr_t, parent)) {
896 ASSERT(XFS_FORCED_SHUTDOWN(dp->i_mount));
897 return XFS_ERROR(EIO);
898 }
899 ASSERT(dp->i_df.if_bytes == dp->i_d.di_size);
900 ASSERT(dp->i_df.if_u1.if_data != NULL);
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200901 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
902 ASSERT(dp->i_d.di_size >= xfs_dir2_sf_hdr_size(sfp->i8count));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 /*
904 * Special case for .
905 */
906 if (args->namelen == 1 && args->name[0] == '.') {
907 args->inumber = dp->i_ino;
Barry Naujok5163f952008-05-21 16:41:01 +1000908 args->cmpresult = XFS_CMP_EXACT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 return XFS_ERROR(EEXIST);
910 }
911 /*
912 * Special case for ..
913 */
914 if (args->namelen == 2 &&
915 args->name[0] == '.' && args->name[1] == '.') {
Christoph Hellwig8bc38782011-07-08 14:35:03 +0200916 args->inumber = xfs_dir2_sf_get_parent_ino(sfp);
Barry Naujok5163f952008-05-21 16:41:01 +1000917 args->cmpresult = XFS_CMP_EXACT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 return XFS_ERROR(EEXIST);
919 }
920 /*
921 * Loop over all the entries trying to match ours.
922 */
Barry Naujok384f3ce2008-05-21 16:58:22 +1000923 ci_sfep = NULL;
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200924 for (i = 0, sfep = xfs_dir2_sf_firstentry(sfp); i < sfp->count;
Barry Naujok5163f952008-05-21 16:41:01 +1000925 i++, sfep = xfs_dir2_sf_nextentry(sfp, sfep)) {
926 /*
927 * Compare name and if it's an exact match, return the inode
928 * number. If it's the first case-insensitive match, store the
929 * inode number and continue looking for an exact match.
930 */
931 cmp = dp->i_mount->m_dirnameops->compname(args, sfep->name,
932 sfep->namelen);
933 if (cmp != XFS_CMP_DIFFERENT && cmp != args->cmpresult) {
934 args->cmpresult = cmp;
Christoph Hellwig8bc38782011-07-08 14:35:03 +0200935 args->inumber = xfs_dir2_sfe_get_ino(sfp, sfep);
Barry Naujok5163f952008-05-21 16:41:01 +1000936 if (cmp == XFS_CMP_EXACT)
937 return XFS_ERROR(EEXIST);
Barry Naujok384f3ce2008-05-21 16:58:22 +1000938 ci_sfep = sfep;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 }
940 }
Barry Naujok6a178102008-05-21 16:42:05 +1000941 ASSERT(args->op_flags & XFS_DA_OP_OKNOENT);
Barry Naujok5163f952008-05-21 16:41:01 +1000942 /*
943 * Here, we can only be doing a lookup (not a rename or replace).
Barry Naujok384f3ce2008-05-21 16:58:22 +1000944 * If a case-insensitive match was not found, return ENOENT.
Barry Naujok5163f952008-05-21 16:41:01 +1000945 */
Barry Naujok384f3ce2008-05-21 16:58:22 +1000946 if (!ci_sfep)
947 return XFS_ERROR(ENOENT);
948 /* otherwise process the CI match as required by the caller */
949 error = xfs_dir_cilookup_result(args, ci_sfep->name, ci_sfep->namelen);
950 return XFS_ERROR(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951}
952
953/*
954 * Remove an entry from a shortform directory.
955 */
956int /* error */
957xfs_dir2_sf_removename(
958 xfs_da_args_t *args)
959{
960 int byteoff; /* offset of removed entry */
961 xfs_inode_t *dp; /* incore directory inode */
962 int entsize; /* this entry's size */
963 int i; /* shortform entry index */
964 int newsize; /* new inode size */
965 int oldsize; /* old inode size */
966 xfs_dir2_sf_entry_t *sfep; /* shortform directory entry */
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200967 xfs_dir2_sf_hdr_t *sfp; /* shortform structure */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000969 trace_xfs_dir2_sf_removename(args);
970
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 dp = args->dp;
972
973 ASSERT(dp->i_df.if_flags & XFS_IFINLINE);
974 oldsize = (int)dp->i_d.di_size;
975 /*
976 * Bail out if the directory is way too short.
977 */
978 if (oldsize < offsetof(xfs_dir2_sf_hdr_t, parent)) {
979 ASSERT(XFS_FORCED_SHUTDOWN(dp->i_mount));
980 return XFS_ERROR(EIO);
981 }
982 ASSERT(dp->i_df.if_bytes == oldsize);
983 ASSERT(dp->i_df.if_u1.if_data != NULL);
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200984 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
985 ASSERT(oldsize >= xfs_dir2_sf_hdr_size(sfp->i8count));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 /*
987 * Loop over the old directory entries.
988 * Find the one we're deleting.
989 */
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200990 for (i = 0, sfep = xfs_dir2_sf_firstentry(sfp); i < sfp->count;
Barry Naujok5163f952008-05-21 16:41:01 +1000991 i++, sfep = xfs_dir2_sf_nextentry(sfp, sfep)) {
992 if (xfs_da_compname(args, sfep->name, sfep->namelen) ==
993 XFS_CMP_EXACT) {
Christoph Hellwig8bc38782011-07-08 14:35:03 +0200994 ASSERT(xfs_dir2_sfe_get_ino(sfp, sfep) ==
995 args->inumber);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 break;
997 }
998 }
999 /*
1000 * Didn't find it.
1001 */
Christoph Hellwigac8ba502011-07-08 14:35:13 +02001002 if (i == sfp->count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 return XFS_ERROR(ENOENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 /*
1005 * Calculate sizes.
1006 */
1007 byteoff = (int)((char *)sfep - (char *)sfp);
Christoph Hellwig78f70cd2011-07-08 14:35:19 +02001008 entsize = xfs_dir2_sf_entsize(sfp, args->namelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 newsize = oldsize - entsize;
1010 /*
1011 * Copy the part if any after the removed entry, sliding it down.
1012 */
1013 if (byteoff + entsize < oldsize)
1014 memmove((char *)sfp + byteoff, (char *)sfp + byteoff + entsize,
1015 oldsize - (byteoff + entsize));
1016 /*
1017 * Fix up the header and file size.
1018 */
Christoph Hellwigac8ba502011-07-08 14:35:13 +02001019 sfp->count--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 dp->i_d.di_size = newsize;
1021 /*
1022 * Reallocate, making it smaller.
1023 */
1024 xfs_idata_realloc(dp, newsize - oldsize, XFS_DATA_FORK);
Christoph Hellwigac8ba502011-07-08 14:35:13 +02001025 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026#if XFS_BIG_INUMS
1027 /*
1028 * Are we changing inode number size?
1029 */
1030 if (args->inumber > XFS_DIR2_MAX_SHORT_INUM) {
Christoph Hellwigac8ba502011-07-08 14:35:13 +02001031 if (sfp->i8count == 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 xfs_dir2_sf_toino4(args);
1033 else
Christoph Hellwigac8ba502011-07-08 14:35:13 +02001034 sfp->i8count--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 }
1036#endif
1037 xfs_dir2_sf_check(args);
1038 xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE | XFS_ILOG_DDATA);
1039 return 0;
1040}
1041
1042/*
1043 * Replace the inode number of an entry in a shortform directory.
1044 */
1045int /* error */
1046xfs_dir2_sf_replace(
1047 xfs_da_args_t *args) /* operation arguments */
1048{
1049 xfs_inode_t *dp; /* incore directory inode */
1050 int i; /* entry index */
1051#if XFS_BIG_INUMS || defined(DEBUG)
1052 xfs_ino_t ino=0; /* entry old inode number */
1053#endif
1054#if XFS_BIG_INUMS
1055 int i8elevated; /* sf_toino8 set i8count=1 */
1056#endif
1057 xfs_dir2_sf_entry_t *sfep; /* shortform directory entry */
Christoph Hellwigac8ba502011-07-08 14:35:13 +02001058 xfs_dir2_sf_hdr_t *sfp; /* shortform structure */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001060 trace_xfs_dir2_sf_replace(args);
1061
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 dp = args->dp;
1063
1064 ASSERT(dp->i_df.if_flags & XFS_IFINLINE);
1065 /*
1066 * Bail out if the shortform directory is way too small.
1067 */
1068 if (dp->i_d.di_size < offsetof(xfs_dir2_sf_hdr_t, parent)) {
1069 ASSERT(XFS_FORCED_SHUTDOWN(dp->i_mount));
1070 return XFS_ERROR(EIO);
1071 }
1072 ASSERT(dp->i_df.if_bytes == dp->i_d.di_size);
1073 ASSERT(dp->i_df.if_u1.if_data != NULL);
Christoph Hellwigac8ba502011-07-08 14:35:13 +02001074 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
1075 ASSERT(dp->i_d.di_size >= xfs_dir2_sf_hdr_size(sfp->i8count));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076#if XFS_BIG_INUMS
1077 /*
1078 * New inode number is large, and need to convert to 8-byte inodes.
1079 */
Christoph Hellwigac8ba502011-07-08 14:35:13 +02001080 if (args->inumber > XFS_DIR2_MAX_SHORT_INUM && sfp->i8count == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 int error; /* error return value */
1082 int newsize; /* new inode size */
1083
1084 newsize =
1085 dp->i_df.if_bytes +
Christoph Hellwigac8ba502011-07-08 14:35:13 +02001086 (sfp->count + 1) *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 ((uint)sizeof(xfs_dir2_ino8_t) -
1088 (uint)sizeof(xfs_dir2_ino4_t));
1089 /*
1090 * Won't fit as shortform, convert to block then do replace.
1091 */
1092 if (newsize > XFS_IFORK_DSIZE(dp)) {
1093 error = xfs_dir2_sf_to_block(args);
1094 if (error) {
1095 return error;
1096 }
1097 return xfs_dir2_block_replace(args);
1098 }
1099 /*
1100 * Still fits, convert to 8-byte now.
1101 */
1102 xfs_dir2_sf_toino8(args);
1103 i8elevated = 1;
Christoph Hellwigac8ba502011-07-08 14:35:13 +02001104 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 } else
1106 i8elevated = 0;
1107#endif
1108 ASSERT(args->namelen != 1 || args->name[0] != '.');
1109 /*
1110 * Replace ..'s entry.
1111 */
1112 if (args->namelen == 2 &&
1113 args->name[0] == '.' && args->name[1] == '.') {
1114#if XFS_BIG_INUMS || defined(DEBUG)
Christoph Hellwig8bc38782011-07-08 14:35:03 +02001115 ino = xfs_dir2_sf_get_parent_ino(sfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 ASSERT(args->inumber != ino);
1117#endif
Christoph Hellwig8bc38782011-07-08 14:35:03 +02001118 xfs_dir2_sf_put_parent_ino(sfp, args->inumber);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 }
1120 /*
1121 * Normal entry, look for the name.
1122 */
1123 else {
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001124 for (i = 0, sfep = xfs_dir2_sf_firstentry(sfp);
Christoph Hellwigac8ba502011-07-08 14:35:13 +02001125 i < sfp->count;
Barry Naujok5163f952008-05-21 16:41:01 +10001126 i++, sfep = xfs_dir2_sf_nextentry(sfp, sfep)) {
1127 if (xfs_da_compname(args, sfep->name, sfep->namelen) ==
1128 XFS_CMP_EXACT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129#if XFS_BIG_INUMS || defined(DEBUG)
Christoph Hellwig8bc38782011-07-08 14:35:03 +02001130 ino = xfs_dir2_sfe_get_ino(sfp, sfep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 ASSERT(args->inumber != ino);
1132#endif
Christoph Hellwig8bc38782011-07-08 14:35:03 +02001133 xfs_dir2_sfe_put_ino(sfp, sfep, args->inumber);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 break;
1135 }
1136 }
1137 /*
1138 * Didn't find it.
1139 */
Christoph Hellwigac8ba502011-07-08 14:35:13 +02001140 if (i == sfp->count) {
Barry Naujok6a178102008-05-21 16:42:05 +10001141 ASSERT(args->op_flags & XFS_DA_OP_OKNOENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142#if XFS_BIG_INUMS
1143 if (i8elevated)
1144 xfs_dir2_sf_toino4(args);
1145#endif
1146 return XFS_ERROR(ENOENT);
1147 }
1148 }
1149#if XFS_BIG_INUMS
1150 /*
1151 * See if the old number was large, the new number is small.
1152 */
1153 if (ino > XFS_DIR2_MAX_SHORT_INUM &&
1154 args->inumber <= XFS_DIR2_MAX_SHORT_INUM) {
1155 /*
1156 * And the old count was one, so need to convert to small.
1157 */
Christoph Hellwigac8ba502011-07-08 14:35:13 +02001158 if (sfp->i8count == 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 xfs_dir2_sf_toino4(args);
1160 else
Christoph Hellwigac8ba502011-07-08 14:35:13 +02001161 sfp->i8count--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 }
1163 /*
1164 * See if the old number was small, the new number is large.
1165 */
1166 if (ino <= XFS_DIR2_MAX_SHORT_INUM &&
1167 args->inumber > XFS_DIR2_MAX_SHORT_INUM) {
1168 /*
1169 * add to the i8count unless we just converted to 8-byte
1170 * inodes (which does an implied i8count = 1)
1171 */
Christoph Hellwigac8ba502011-07-08 14:35:13 +02001172 ASSERT(sfp->i8count != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 if (!i8elevated)
Christoph Hellwigac8ba502011-07-08 14:35:13 +02001174 sfp->i8count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 }
1176#endif
1177 xfs_dir2_sf_check(args);
1178 xfs_trans_log_inode(args->trans, dp, XFS_ILOG_DDATA);
1179 return 0;
1180}
1181
1182#if XFS_BIG_INUMS
1183/*
1184 * Convert from 8-byte inode numbers to 4-byte inode numbers.
1185 * The last 8-byte inode number is gone, but the count is still 1.
1186 */
1187static void
1188xfs_dir2_sf_toino4(
1189 xfs_da_args_t *args) /* operation arguments */
1190{
1191 char *buf; /* old dir's buffer */
1192 xfs_inode_t *dp; /* incore directory inode */
1193 int i; /* entry index */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 int newsize; /* new inode size */
1195 xfs_dir2_sf_entry_t *oldsfep; /* old sf entry */
Christoph Hellwigac8ba502011-07-08 14:35:13 +02001196 xfs_dir2_sf_hdr_t *oldsfp; /* old sf directory */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 int oldsize; /* old inode size */
1198 xfs_dir2_sf_entry_t *sfep; /* new sf entry */
Christoph Hellwigac8ba502011-07-08 14:35:13 +02001199 xfs_dir2_sf_hdr_t *sfp; /* new sf directory */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001201 trace_xfs_dir2_sf_toino4(args);
1202
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 dp = args->dp;
1204
1205 /*
1206 * Copy the old directory to the buffer.
1207 * Then nuke it from the inode, and add the new buffer to the inode.
1208 * Don't want xfs_idata_realloc copying the data here.
1209 */
1210 oldsize = dp->i_df.if_bytes;
1211 buf = kmem_alloc(oldsize, KM_SLEEP);
Christoph Hellwigac8ba502011-07-08 14:35:13 +02001212 oldsfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
1213 ASSERT(oldsfp->i8count == 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 memcpy(buf, oldsfp, oldsize);
1215 /*
1216 * Compute the new inode size.
1217 */
1218 newsize =
1219 oldsize -
Christoph Hellwigac8ba502011-07-08 14:35:13 +02001220 (oldsfp->count + 1) *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 ((uint)sizeof(xfs_dir2_ino8_t) - (uint)sizeof(xfs_dir2_ino4_t));
1222 xfs_idata_realloc(dp, -oldsize, XFS_DATA_FORK);
1223 xfs_idata_realloc(dp, newsize, XFS_DATA_FORK);
1224 /*
1225 * Reset our pointers, the data has moved.
1226 */
Christoph Hellwigac8ba502011-07-08 14:35:13 +02001227 oldsfp = (xfs_dir2_sf_hdr_t *)buf;
1228 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 /*
1230 * Fill in the new header.
1231 */
Christoph Hellwigac8ba502011-07-08 14:35:13 +02001232 sfp->count = oldsfp->count;
1233 sfp->i8count = 0;
Christoph Hellwig8bc38782011-07-08 14:35:03 +02001234 xfs_dir2_sf_put_parent_ino(sfp, xfs_dir2_sf_get_parent_ino(oldsfp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 /*
1236 * Copy the entries field by field.
1237 */
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001238 for (i = 0, sfep = xfs_dir2_sf_firstentry(sfp),
1239 oldsfep = xfs_dir2_sf_firstentry(oldsfp);
Christoph Hellwigac8ba502011-07-08 14:35:13 +02001240 i < sfp->count;
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001241 i++, sfep = xfs_dir2_sf_nextentry(sfp, sfep),
1242 oldsfep = xfs_dir2_sf_nextentry(oldsfp, oldsfep)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 sfep->namelen = oldsfep->namelen;
1244 sfep->offset = oldsfep->offset;
1245 memcpy(sfep->name, oldsfep->name, sfep->namelen);
Christoph Hellwig8bc38782011-07-08 14:35:03 +02001246 xfs_dir2_sfe_put_ino(sfp, sfep,
1247 xfs_dir2_sfe_get_ino(oldsfp, oldsfep));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248 }
1249 /*
1250 * Clean up the inode.
1251 */
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10001252 kmem_free(buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253 dp->i_d.di_size = newsize;
1254 xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE | XFS_ILOG_DDATA);
1255}
1256
1257/*
1258 * Convert from 4-byte inode numbers to 8-byte inode numbers.
1259 * The new 8-byte inode number is not there yet, we leave with the
1260 * count 1 but no corresponding entry.
1261 */
1262static void
1263xfs_dir2_sf_toino8(
1264 xfs_da_args_t *args) /* operation arguments */
1265{
1266 char *buf; /* old dir's buffer */
1267 xfs_inode_t *dp; /* incore directory inode */
1268 int i; /* entry index */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 int newsize; /* new inode size */
1270 xfs_dir2_sf_entry_t *oldsfep; /* old sf entry */
Christoph Hellwigac8ba502011-07-08 14:35:13 +02001271 xfs_dir2_sf_hdr_t *oldsfp; /* old sf directory */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272 int oldsize; /* old inode size */
1273 xfs_dir2_sf_entry_t *sfep; /* new sf entry */
Christoph Hellwigac8ba502011-07-08 14:35:13 +02001274 xfs_dir2_sf_hdr_t *sfp; /* new sf directory */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001276 trace_xfs_dir2_sf_toino8(args);
1277
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 dp = args->dp;
1279
1280 /*
1281 * Copy the old directory to the buffer.
1282 * Then nuke it from the inode, and add the new buffer to the inode.
1283 * Don't want xfs_idata_realloc copying the data here.
1284 */
1285 oldsize = dp->i_df.if_bytes;
1286 buf = kmem_alloc(oldsize, KM_SLEEP);
Christoph Hellwigac8ba502011-07-08 14:35:13 +02001287 oldsfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
1288 ASSERT(oldsfp->i8count == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 memcpy(buf, oldsfp, oldsize);
1290 /*
1291 * Compute the new inode size.
1292 */
1293 newsize =
1294 oldsize +
Christoph Hellwigac8ba502011-07-08 14:35:13 +02001295 (oldsfp->count + 1) *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 ((uint)sizeof(xfs_dir2_ino8_t) - (uint)sizeof(xfs_dir2_ino4_t));
1297 xfs_idata_realloc(dp, -oldsize, XFS_DATA_FORK);
1298 xfs_idata_realloc(dp, newsize, XFS_DATA_FORK);
1299 /*
1300 * Reset our pointers, the data has moved.
1301 */
Christoph Hellwigac8ba502011-07-08 14:35:13 +02001302 oldsfp = (xfs_dir2_sf_hdr_t *)buf;
1303 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 /*
1305 * Fill in the new header.
1306 */
Christoph Hellwigac8ba502011-07-08 14:35:13 +02001307 sfp->count = oldsfp->count;
1308 sfp->i8count = 1;
Christoph Hellwig8bc38782011-07-08 14:35:03 +02001309 xfs_dir2_sf_put_parent_ino(sfp, xfs_dir2_sf_get_parent_ino(oldsfp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 /*
1311 * Copy the entries field by field.
1312 */
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001313 for (i = 0, sfep = xfs_dir2_sf_firstentry(sfp),
1314 oldsfep = xfs_dir2_sf_firstentry(oldsfp);
Christoph Hellwigac8ba502011-07-08 14:35:13 +02001315 i < sfp->count;
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001316 i++, sfep = xfs_dir2_sf_nextentry(sfp, sfep),
1317 oldsfep = xfs_dir2_sf_nextentry(oldsfp, oldsfep)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 sfep->namelen = oldsfep->namelen;
1319 sfep->offset = oldsfep->offset;
1320 memcpy(sfep->name, oldsfep->name, sfep->namelen);
Christoph Hellwig8bc38782011-07-08 14:35:03 +02001321 xfs_dir2_sfe_put_ino(sfp, sfep,
1322 xfs_dir2_sfe_get_ino(oldsfp, oldsfep));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 }
1324 /*
1325 * Clean up the inode.
1326 */
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10001327 kmem_free(buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328 dp->i_d.di_size = newsize;
1329 xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE | XFS_ILOG_DDATA);
1330}
1331#endif /* XFS_BIG_INUMS */