blob: 79d05e84e296a8d7f932e21e574c0f0545308ea7 [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_mount.h"
Nathan Scotta844f452005-11-02 14:38:42 +110027#include "xfs_da_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include "xfs_bmap_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include "xfs_dinode.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include "xfs_inode.h"
Nathan Scotta844f452005-11-02 14:38:42 +110031#include "xfs_inode_item.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include "xfs_error.h"
Christoph Hellwig57926642011-07-13 13:43:48 +020033#include "xfs_dir2.h"
34#include "xfs_dir2_format.h"
35#include "xfs_dir2_priv.h"
Christoph Hellwig0b1b2132009-12-14 23:14:59 +000036#include "xfs_trace.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
38/*
39 * Prototypes for internal functions.
40 */
41static void xfs_dir2_sf_addname_easy(xfs_da_args_t *args,
42 xfs_dir2_sf_entry_t *sfep,
43 xfs_dir2_data_aoff_t offset,
44 int new_isize);
45static void xfs_dir2_sf_addname_hard(xfs_da_args_t *args, int objchange,
46 int new_isize);
47static int xfs_dir2_sf_addname_pick(xfs_da_args_t *args, int objchange,
48 xfs_dir2_sf_entry_t **sfepp,
49 xfs_dir2_data_aoff_t *offsetp);
50#ifdef DEBUG
51static void xfs_dir2_sf_check(xfs_da_args_t *args);
52#else
53#define xfs_dir2_sf_check(args)
54#endif /* DEBUG */
55#if XFS_BIG_INUMS
56static void xfs_dir2_sf_toino4(xfs_da_args_t *args);
57static void xfs_dir2_sf_toino8(xfs_da_args_t *args);
58#endif /* XFS_BIG_INUMS */
59
Christoph Hellwig8bc38782011-07-08 14:35:03 +020060/*
61 * Inode numbers in short-form directories can come in two versions,
62 * either 4 bytes or 8 bytes wide. These helpers deal with the
63 * two forms transparently by looking at the headers i8count field.
Christoph Hellwig218106a2011-07-08 14:35:58 +020064 *
65 * For 64-bit inode number the most significant byte must be zero.
Christoph Hellwig8bc38782011-07-08 14:35:03 +020066 */
67static xfs_ino_t
68xfs_dir2_sf_get_ino(
Christoph Hellwigac8ba502011-07-08 14:35:13 +020069 struct xfs_dir2_sf_hdr *hdr,
Christoph Hellwig8bc38782011-07-08 14:35:03 +020070 xfs_dir2_inou_t *from)
71{
Christoph Hellwigac8ba502011-07-08 14:35:13 +020072 if (hdr->i8count)
Christoph Hellwig218106a2011-07-08 14:35:58 +020073 return get_unaligned_be64(&from->i8.i) & 0x00ffffffffffffffULL;
Christoph Hellwig8bc38782011-07-08 14:35:03 +020074 else
Christoph Hellwig218106a2011-07-08 14:35:58 +020075 return get_unaligned_be32(&from->i4.i);
Christoph Hellwig8bc38782011-07-08 14:35:03 +020076}
77
78static void
79xfs_dir2_sf_put_ino(
Christoph Hellwigac8ba502011-07-08 14:35:13 +020080 struct xfs_dir2_sf_hdr *hdr,
Christoph Hellwig8bc38782011-07-08 14:35:03 +020081 xfs_dir2_inou_t *to,
82 xfs_ino_t ino)
83{
Christoph Hellwig218106a2011-07-08 14:35:58 +020084 ASSERT((ino & 0xff00000000000000ULL) == 0);
85
Christoph Hellwigac8ba502011-07-08 14:35:13 +020086 if (hdr->i8count)
Christoph Hellwig218106a2011-07-08 14:35:58 +020087 put_unaligned_be64(ino, &to->i8.i);
Christoph Hellwig8bc38782011-07-08 14:35:03 +020088 else
Christoph Hellwig218106a2011-07-08 14:35:58 +020089 put_unaligned_be32(ino, &to->i4.i);
Christoph Hellwig8bc38782011-07-08 14:35:03 +020090}
91
92xfs_ino_t
93xfs_dir2_sf_get_parent_ino(
Christoph Hellwigac8ba502011-07-08 14:35:13 +020094 struct xfs_dir2_sf_hdr *hdr)
Christoph Hellwig8bc38782011-07-08 14:35:03 +020095{
Christoph Hellwigac8ba502011-07-08 14:35:13 +020096 return xfs_dir2_sf_get_ino(hdr, &hdr->parent);
Christoph Hellwig8bc38782011-07-08 14:35:03 +020097}
98
99static void
100xfs_dir2_sf_put_parent_ino(
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200101 struct xfs_dir2_sf_hdr *hdr,
Christoph Hellwig8bc38782011-07-08 14:35:03 +0200102 xfs_ino_t ino)
103{
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200104 xfs_dir2_sf_put_ino(hdr, &hdr->parent, ino);
Christoph Hellwig8bc38782011-07-08 14:35:03 +0200105}
106
107/*
108 * In short-form directory entries the inode numbers are stored at variable
109 * offset behind the entry name. The inode numbers may only be accessed
110 * through the helpers below.
111 */
112static xfs_dir2_inou_t *
113xfs_dir2_sfe_inop(
114 struct xfs_dir2_sf_entry *sfep)
115{
116 return (xfs_dir2_inou_t *)&sfep->name[sfep->namelen];
117}
118
119xfs_ino_t
120xfs_dir2_sfe_get_ino(
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200121 struct xfs_dir2_sf_hdr *hdr,
Christoph Hellwig8bc38782011-07-08 14:35:03 +0200122 struct xfs_dir2_sf_entry *sfep)
123{
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200124 return xfs_dir2_sf_get_ino(hdr, xfs_dir2_sfe_inop(sfep));
Christoph Hellwig8bc38782011-07-08 14:35:03 +0200125}
126
127static void
128xfs_dir2_sfe_put_ino(
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200129 struct xfs_dir2_sf_hdr *hdr,
Christoph Hellwig8bc38782011-07-08 14:35:03 +0200130 struct xfs_dir2_sf_entry *sfep,
131 xfs_ino_t ino)
132{
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200133 xfs_dir2_sf_put_ino(hdr, xfs_dir2_sfe_inop(sfep), ino);
Christoph Hellwig8bc38782011-07-08 14:35:03 +0200134}
135
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136/*
137 * Given a block directory (dp/block), calculate its size as a shortform (sf)
138 * directory and a header for the sf directory, if it will fit it the
139 * space currently present in the inode. If it won't fit, the output
140 * size is too big (but not accurate).
141 */
142int /* size for sf form */
143xfs_dir2_block_sfsize(
144 xfs_inode_t *dp, /* incore inode pointer */
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +0200145 xfs_dir2_data_hdr_t *hdr, /* block directory data */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 xfs_dir2_sf_hdr_t *sfhp) /* output: header for sf form */
147{
148 xfs_dir2_dataptr_t addr; /* data entry address */
149 xfs_dir2_leaf_entry_t *blp; /* leaf area of the block */
150 xfs_dir2_block_tail_t *btp; /* tail area of the block */
151 int count; /* shortform entry count */
152 xfs_dir2_data_entry_t *dep; /* data entry in the block */
153 int i; /* block entry index */
154 int i8count; /* count of big-inode entries */
155 int isdot; /* entry is "." */
156 int isdotdot; /* entry is ".." */
157 xfs_mount_t *mp; /* mount structure pointer */
158 int namelen; /* total name bytes */
Christoph Hellwig5bde1ba2005-11-02 15:06:18 +1100159 xfs_ino_t parent = 0; /* parent inode number */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 int size=0; /* total computed size */
161
162 mp = dp->i_mount;
163
164 count = i8count = namelen = 0;
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +0200165 btp = xfs_dir2_block_tail_p(mp, hdr);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000166 blp = xfs_dir2_block_leaf_p(btp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167
168 /*
169 * Iterate over the block's data entries by using the leaf pointers.
170 */
Nathan Scotte922fff2006-03-17 17:27:56 +1100171 for (i = 0; i < be32_to_cpu(btp->count); i++) {
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100172 if ((addr = be32_to_cpu(blp[i].address)) == XFS_DIR2_NULL_DATAPTR)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 continue;
174 /*
175 * Calculate the pointer to the entry at hand.
176 */
177 dep = (xfs_dir2_data_entry_t *)
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +0200178 ((char *)hdr + xfs_dir2_dataptr_to_off(mp, addr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 /*
180 * Detect . and .., so we can special-case them.
181 * . is not included in sf directories.
182 * .. is included by just the parent inode number.
183 */
184 isdot = dep->namelen == 1 && dep->name[0] == '.';
185 isdotdot =
186 dep->namelen == 2 &&
187 dep->name[0] == '.' && dep->name[1] == '.';
188#if XFS_BIG_INUMS
189 if (!isdot)
Christoph Hellwigff9901c2006-06-09 14:48:37 +1000190 i8count += be64_to_cpu(dep->inumber) > XFS_DIR2_MAX_SHORT_INUM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191#endif
192 if (!isdot && !isdotdot) {
193 count++;
194 namelen += dep->namelen;
195 } else if (isdotdot)
Christoph Hellwigff9901c2006-06-09 14:48:37 +1000196 parent = be64_to_cpu(dep->inumber);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 /*
198 * Calculate the new size, see if we should give up yet.
199 */
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000200 size = xfs_dir2_sf_hdr_size(i8count) + /* header */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 count + /* namelen */
202 count * (uint)sizeof(xfs_dir2_sf_off_t) + /* offset */
203 namelen + /* name */
204 (i8count ? /* inumber */
205 (uint)sizeof(xfs_dir2_ino8_t) * count :
206 (uint)sizeof(xfs_dir2_ino4_t) * count);
207 if (size > XFS_IFORK_DSIZE(dp))
208 return size; /* size value is a failure */
209 }
210 /*
211 * Create the output header, if it worked.
212 */
213 sfhp->count = count;
214 sfhp->i8count = i8count;
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200215 xfs_dir2_sf_put_parent_ino(sfhp, parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 return size;
217}
218
219/*
220 * Convert a block format directory to shortform.
221 * Caller has already checked that it will fit, and built us a header.
222 */
223int /* error */
224xfs_dir2_block_to_sf(
225 xfs_da_args_t *args, /* operation arguments */
226 xfs_dabuf_t *bp, /* block buffer */
227 int size, /* shortform directory size */
228 xfs_dir2_sf_hdr_t *sfhp) /* shortform directory hdr */
229{
Christoph Hellwiga64b0412011-07-08 14:35:32 +0200230 xfs_dir2_data_hdr_t *hdr; /* block header */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 xfs_dir2_block_tail_t *btp; /* block tail pointer */
232 xfs_dir2_data_entry_t *dep; /* data entry pointer */
233 xfs_inode_t *dp; /* incore directory inode */
234 xfs_dir2_data_unused_t *dup; /* unused data pointer */
235 char *endptr; /* end of data entries */
236 int error; /* error return value */
237 int logflags; /* inode logging flags */
238 xfs_mount_t *mp; /* filesystem mount point */
239 char *ptr; /* current data pointer */
240 xfs_dir2_sf_entry_t *sfep; /* shortform entry */
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200241 xfs_dir2_sf_hdr_t *sfp; /* shortform directory header */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000243 trace_xfs_dir2_block_to_sf(args);
244
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 dp = args->dp;
246 mp = dp->i_mount;
247
248 /*
249 * Make a copy of the block data, so we can shrink the inode
250 * and add local data.
251 */
Christoph Hellwiga64b0412011-07-08 14:35:32 +0200252 hdr = kmem_alloc(mp->m_dirblksize, KM_SLEEP);
253 memcpy(hdr, bp->data, mp->m_dirblksize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 logflags = XFS_ILOG_CORE;
255 if ((error = xfs_dir2_shrink_inode(args, mp->m_dirdatablk, bp))) {
256 ASSERT(error != ENOSPC);
257 goto out;
258 }
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +0200259
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 /*
261 * The buffer is now unconditionally gone, whether
262 * xfs_dir2_shrink_inode worked or not.
263 *
264 * Convert the inode to local format.
265 */
266 dp->i_df.if_flags &= ~XFS_IFEXTENTS;
267 dp->i_df.if_flags |= XFS_IFINLINE;
268 dp->i_d.di_format = XFS_DINODE_FMT_LOCAL;
269 ASSERT(dp->i_df.if_bytes == 0);
270 xfs_idata_realloc(dp, size, XFS_DATA_FORK);
271 logflags |= XFS_ILOG_DDATA;
272 /*
273 * Copy the header into the newly allocate local space.
274 */
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200275 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000276 memcpy(sfp, sfhp, xfs_dir2_sf_hdr_size(sfhp->i8count));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 dp->i_d.di_size = size;
278 /*
279 * Set up to loop over the block's entries.
280 */
Christoph Hellwiga64b0412011-07-08 14:35:32 +0200281 btp = xfs_dir2_block_tail_p(mp, hdr);
282 ptr = (char *)(hdr + 1);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000283 endptr = (char *)xfs_dir2_block_leaf_p(btp);
284 sfep = xfs_dir2_sf_firstentry(sfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 /*
286 * Loop over the active and unused entries.
287 * Stop when we reach the leaf/tail portion of the block.
288 */
289 while (ptr < endptr) {
290 /*
291 * If it's unused, just skip over it.
292 */
293 dup = (xfs_dir2_data_unused_t *)ptr;
Nathan Scottad354eb2006-03-17 17:27:37 +1100294 if (be16_to_cpu(dup->freetag) == XFS_DIR2_DATA_FREE_TAG) {
295 ptr += be16_to_cpu(dup->length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 continue;
297 }
298 dep = (xfs_dir2_data_entry_t *)ptr;
299 /*
300 * Skip .
301 */
302 if (dep->namelen == 1 && dep->name[0] == '.')
Christoph Hellwigff9901c2006-06-09 14:48:37 +1000303 ASSERT(be64_to_cpu(dep->inumber) == dp->i_ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 /*
305 * Skip .., but make sure the inode number is right.
306 */
307 else if (dep->namelen == 2 &&
308 dep->name[0] == '.' && dep->name[1] == '.')
Christoph Hellwigff9901c2006-06-09 14:48:37 +1000309 ASSERT(be64_to_cpu(dep->inumber) ==
Christoph Hellwig8bc38782011-07-08 14:35:03 +0200310 xfs_dir2_sf_get_parent_ino(sfp));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 /*
312 * Normal entry, copy it into shortform.
313 */
314 else {
315 sfep->namelen = dep->namelen;
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000316 xfs_dir2_sf_put_offset(sfep,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 (xfs_dir2_data_aoff_t)
Christoph Hellwiga64b0412011-07-08 14:35:32 +0200318 ((char *)dep - (char *)hdr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 memcpy(sfep->name, dep->name, dep->namelen);
Christoph Hellwig8bc38782011-07-08 14:35:03 +0200320 xfs_dir2_sfe_put_ino(sfp, sfep,
321 be64_to_cpu(dep->inumber));
322
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000323 sfep = xfs_dir2_sf_nextentry(sfp, sfep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 }
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000325 ptr += xfs_dir2_data_entsize(dep->namelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 }
327 ASSERT((char *)sfep - (char *)sfp == size);
328 xfs_dir2_sf_check(args);
329out:
330 xfs_trans_log_inode(args->trans, dp, logflags);
Christoph Hellwiga64b0412011-07-08 14:35:32 +0200331 kmem_free(hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 return error;
333}
334
335/*
336 * Add a name to a shortform directory.
337 * There are two algorithms, "easy" and "hard" which we decide on
338 * before changing anything.
339 * Convert to block form if necessary, if the new entry won't fit.
340 */
341int /* error */
342xfs_dir2_sf_addname(
343 xfs_da_args_t *args) /* operation arguments */
344{
345 int add_entsize; /* size of the new entry */
346 xfs_inode_t *dp; /* incore directory inode */
347 int error; /* error return value */
348 int incr_isize; /* total change in size */
349 int new_isize; /* di_size after adding name */
350 int objchange; /* changing to 8-byte inodes */
Christoph Hellwig5bde1ba2005-11-02 15:06:18 +1100351 xfs_dir2_data_aoff_t offset = 0; /* offset for new entry */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 int old_isize; /* di_size before adding name */
353 int pick; /* which algorithm to use */
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200354 xfs_dir2_sf_hdr_t *sfp; /* shortform structure */
Christoph Hellwig5bde1ba2005-11-02 15:06:18 +1100355 xfs_dir2_sf_entry_t *sfep = NULL; /* shortform entry */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000357 trace_xfs_dir2_sf_addname(args);
358
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 ASSERT(xfs_dir2_sf_lookup(args) == ENOENT);
360 dp = args->dp;
361 ASSERT(dp->i_df.if_flags & XFS_IFINLINE);
362 /*
363 * Make sure the shortform value has some of its header.
364 */
365 if (dp->i_d.di_size < offsetof(xfs_dir2_sf_hdr_t, parent)) {
366 ASSERT(XFS_FORCED_SHUTDOWN(dp->i_mount));
367 return XFS_ERROR(EIO);
368 }
369 ASSERT(dp->i_df.if_bytes == dp->i_d.di_size);
370 ASSERT(dp->i_df.if_u1.if_data != NULL);
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200371 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
372 ASSERT(dp->i_d.di_size >= xfs_dir2_sf_hdr_size(sfp->i8count));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 /*
374 * Compute entry (and change in) size.
375 */
Christoph Hellwig78f70cd2011-07-08 14:35:19 +0200376 add_entsize = xfs_dir2_sf_entsize(sfp, args->namelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 incr_isize = add_entsize;
378 objchange = 0;
379#if XFS_BIG_INUMS
380 /*
381 * Do we have to change to 8 byte inodes?
382 */
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200383 if (args->inumber > XFS_DIR2_MAX_SHORT_INUM && sfp->i8count == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 /*
385 * Yes, adjust the entry size and the total size.
386 */
387 add_entsize +=
388 (uint)sizeof(xfs_dir2_ino8_t) -
389 (uint)sizeof(xfs_dir2_ino4_t);
390 incr_isize +=
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200391 (sfp->count + 2) *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 ((uint)sizeof(xfs_dir2_ino8_t) -
393 (uint)sizeof(xfs_dir2_ino4_t));
394 objchange = 1;
395 }
396#endif
397 old_isize = (int)dp->i_d.di_size;
398 new_isize = old_isize + incr_isize;
399 /*
400 * Won't fit as shortform any more (due to size),
401 * or the pick routine says it won't (due to offset values).
402 */
403 if (new_isize > XFS_IFORK_DSIZE(dp) ||
404 (pick =
405 xfs_dir2_sf_addname_pick(args, objchange, &sfep, &offset)) == 0) {
406 /*
407 * Just checking or no space reservation, it doesn't fit.
408 */
Barry Naujok6a178102008-05-21 16:42:05 +1000409 if ((args->op_flags & XFS_DA_OP_JUSTCHECK) || args->total == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 return XFS_ERROR(ENOSPC);
411 /*
412 * Convert to block form then add the name.
413 */
414 error = xfs_dir2_sf_to_block(args);
415 if (error)
416 return error;
417 return xfs_dir2_block_addname(args);
418 }
419 /*
420 * Just checking, it fits.
421 */
Barry Naujok6a178102008-05-21 16:42:05 +1000422 if (args->op_flags & XFS_DA_OP_JUSTCHECK)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 return 0;
424 /*
425 * Do it the easy way - just add it at the end.
426 */
427 if (pick == 1)
428 xfs_dir2_sf_addname_easy(args, sfep, offset, new_isize);
429 /*
430 * Do it the hard way - look for a place to insert the new entry.
431 * Convert to 8 byte inode numbers first if necessary.
432 */
433 else {
434 ASSERT(pick == 2);
435#if XFS_BIG_INUMS
436 if (objchange)
437 xfs_dir2_sf_toino8(args);
438#endif
439 xfs_dir2_sf_addname_hard(args, objchange, new_isize);
440 }
441 xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE | XFS_ILOG_DDATA);
442 return 0;
443}
444
445/*
446 * Add the new entry the "easy" way.
447 * This is copying the old directory and adding the new entry at the end.
448 * Since it's sorted by "offset" we need room after the last offset
449 * that's already there, and then room to convert to a block directory.
450 * This is already checked by the pick routine.
451 */
452static void
453xfs_dir2_sf_addname_easy(
454 xfs_da_args_t *args, /* operation arguments */
455 xfs_dir2_sf_entry_t *sfep, /* pointer to new entry */
456 xfs_dir2_data_aoff_t offset, /* offset to use for new ent */
457 int new_isize) /* new directory size */
458{
459 int byteoff; /* byte offset in sf dir */
460 xfs_inode_t *dp; /* incore directory inode */
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200461 xfs_dir2_sf_hdr_t *sfp; /* shortform structure */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462
463 dp = args->dp;
464
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200465 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 byteoff = (int)((char *)sfep - (char *)sfp);
467 /*
468 * Grow the in-inode space.
469 */
Christoph Hellwig78f70cd2011-07-08 14:35:19 +0200470 xfs_idata_realloc(dp, xfs_dir2_sf_entsize(sfp, args->namelen),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 XFS_DATA_FORK);
472 /*
473 * Need to set up again due to realloc of the inode data.
474 */
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200475 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 sfep = (xfs_dir2_sf_entry_t *)((char *)sfp + byteoff);
477 /*
478 * Fill in the new entry.
479 */
480 sfep->namelen = args->namelen;
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000481 xfs_dir2_sf_put_offset(sfep, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 memcpy(sfep->name, args->name, sfep->namelen);
Christoph Hellwig8bc38782011-07-08 14:35:03 +0200483 xfs_dir2_sfe_put_ino(sfp, sfep, args->inumber);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 /*
485 * Update the header and inode.
486 */
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200487 sfp->count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488#if XFS_BIG_INUMS
489 if (args->inumber > XFS_DIR2_MAX_SHORT_INUM)
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200490 sfp->i8count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491#endif
492 dp->i_d.di_size = new_isize;
493 xfs_dir2_sf_check(args);
494}
495
496/*
497 * Add the new entry the "hard" way.
498 * The caller has already converted to 8 byte inode numbers if necessary,
499 * in which case we need to leave the i8count at 1.
500 * Find a hole that the new entry will fit into, and copy
501 * the first part of the entries, the new entry, and the last part of
502 * the entries.
503 */
504/* ARGSUSED */
505static void
506xfs_dir2_sf_addname_hard(
507 xfs_da_args_t *args, /* operation arguments */
508 int objchange, /* changing inode number size */
509 int new_isize) /* new directory size */
510{
511 int add_datasize; /* data size need for new ent */
512 char *buf; /* buffer for old */
513 xfs_inode_t *dp; /* incore directory inode */
514 int eof; /* reached end of old dir */
515 int nbytes; /* temp for byte copies */
516 xfs_dir2_data_aoff_t new_offset; /* next offset value */
517 xfs_dir2_data_aoff_t offset; /* current offset value */
518 int old_isize; /* previous di_size */
519 xfs_dir2_sf_entry_t *oldsfep; /* entry in original dir */
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200520 xfs_dir2_sf_hdr_t *oldsfp; /* original shortform dir */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 xfs_dir2_sf_entry_t *sfep; /* entry in new dir */
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200522 xfs_dir2_sf_hdr_t *sfp; /* new shortform dir */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523
524 /*
525 * Copy the old directory to the stack buffer.
526 */
527 dp = args->dp;
528
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200529 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 old_isize = (int)dp->i_d.di_size;
531 buf = kmem_alloc(old_isize, KM_SLEEP);
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200532 oldsfp = (xfs_dir2_sf_hdr_t *)buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 memcpy(oldsfp, sfp, old_isize);
534 /*
535 * Loop over the old directory finding the place we're going
536 * to insert the new entry.
537 * If it's going to end up at the end then oldsfep will point there.
538 */
539 for (offset = XFS_DIR2_DATA_FIRST_OFFSET,
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000540 oldsfep = xfs_dir2_sf_firstentry(oldsfp),
541 add_datasize = xfs_dir2_data_entsize(args->namelen),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 eof = (char *)oldsfep == &buf[old_isize];
543 !eof;
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000544 offset = new_offset + xfs_dir2_data_entsize(oldsfep->namelen),
545 oldsfep = xfs_dir2_sf_nextentry(oldsfp, oldsfep),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 eof = (char *)oldsfep == &buf[old_isize]) {
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000547 new_offset = xfs_dir2_sf_get_offset(oldsfep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 if (offset + add_datasize <= new_offset)
549 break;
550 }
551 /*
552 * Get rid of the old directory, then allocate space for
553 * the new one. We do this so xfs_idata_realloc won't copy
554 * the data.
555 */
556 xfs_idata_realloc(dp, -old_isize, XFS_DATA_FORK);
557 xfs_idata_realloc(dp, new_isize, XFS_DATA_FORK);
558 /*
559 * Reset the pointer since the buffer was reallocated.
560 */
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200561 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 /*
563 * Copy the first part of the directory, including the header.
564 */
565 nbytes = (int)((char *)oldsfep - (char *)oldsfp);
566 memcpy(sfp, oldsfp, nbytes);
567 sfep = (xfs_dir2_sf_entry_t *)((char *)sfp + nbytes);
568 /*
569 * Fill in the new entry, and update the header counts.
570 */
571 sfep->namelen = args->namelen;
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000572 xfs_dir2_sf_put_offset(sfep, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 memcpy(sfep->name, args->name, sfep->namelen);
Christoph Hellwig8bc38782011-07-08 14:35:03 +0200574 xfs_dir2_sfe_put_ino(sfp, sfep, args->inumber);
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200575 sfp->count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576#if XFS_BIG_INUMS
577 if (args->inumber > XFS_DIR2_MAX_SHORT_INUM && !objchange)
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200578 sfp->i8count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579#endif
580 /*
581 * If there's more left to copy, do that.
582 */
583 if (!eof) {
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000584 sfep = xfs_dir2_sf_nextentry(sfp, sfep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 memcpy(sfep, oldsfep, old_isize - nbytes);
586 }
Denys Vlasenkof0e2d932008-05-19 16:31:57 +1000587 kmem_free(buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 dp->i_d.di_size = new_isize;
589 xfs_dir2_sf_check(args);
590}
591
592/*
593 * Decide if the new entry will fit at all.
594 * If it will fit, pick between adding the new entry to the end (easy)
595 * or somewhere else (hard).
596 * Return 0 (won't fit), 1 (easy), 2 (hard).
597 */
598/*ARGSUSED*/
599static int /* pick result */
600xfs_dir2_sf_addname_pick(
601 xfs_da_args_t *args, /* operation arguments */
602 int objchange, /* inode # size changes */
603 xfs_dir2_sf_entry_t **sfepp, /* out(1): new entry ptr */
604 xfs_dir2_data_aoff_t *offsetp) /* out(1): new offset */
605{
606 xfs_inode_t *dp; /* incore directory inode */
607 int holefit; /* found hole it will fit in */
608 int i; /* entry number */
609 xfs_mount_t *mp; /* filesystem mount point */
610 xfs_dir2_data_aoff_t offset; /* data block offset */
611 xfs_dir2_sf_entry_t *sfep; /* shortform entry */
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200612 xfs_dir2_sf_hdr_t *sfp; /* shortform structure */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 int size; /* entry's data size */
614 int used; /* data bytes used */
615
616 dp = args->dp;
617 mp = dp->i_mount;
618
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200619 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000620 size = xfs_dir2_data_entsize(args->namelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 offset = XFS_DIR2_DATA_FIRST_OFFSET;
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000622 sfep = xfs_dir2_sf_firstentry(sfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 holefit = 0;
624 /*
625 * Loop over sf entries.
626 * Keep track of data offset and whether we've seen a place
627 * to insert the new entry.
628 */
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200629 for (i = 0; i < sfp->count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 if (!holefit)
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000631 holefit = offset + size <= xfs_dir2_sf_get_offset(sfep);
632 offset = xfs_dir2_sf_get_offset(sfep) +
633 xfs_dir2_data_entsize(sfep->namelen);
634 sfep = xfs_dir2_sf_nextentry(sfp, sfep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 }
636 /*
637 * Calculate data bytes used excluding the new entry, if this
638 * was a data block (block form directory).
639 */
640 used = offset +
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200641 (sfp->count + 3) * (uint)sizeof(xfs_dir2_leaf_entry_t) +
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 (uint)sizeof(xfs_dir2_block_tail_t);
643 /*
644 * If it won't fit in a block form then we can't insert it,
645 * we'll go back, convert to block, then try the insert and convert
646 * to leaf.
647 */
648 if (used + (holefit ? 0 : size) > mp->m_dirblksize)
649 return 0;
650 /*
651 * If changing the inode number size, do it the hard way.
652 */
653#if XFS_BIG_INUMS
654 if (objchange) {
655 return 2;
656 }
657#else
658 ASSERT(objchange == 0);
659#endif
660 /*
661 * If it won't fit at the end then do it the hard way (use the hole).
662 */
663 if (used + size > mp->m_dirblksize)
664 return 2;
665 /*
666 * Do it the easy way.
667 */
668 *sfepp = sfep;
669 *offsetp = offset;
670 return 1;
671}
672
673#ifdef DEBUG
674/*
675 * Check consistency of shortform directory, assert if bad.
676 */
677static void
678xfs_dir2_sf_check(
679 xfs_da_args_t *args) /* operation arguments */
680{
681 xfs_inode_t *dp; /* incore directory inode */
682 int i; /* entry number */
683 int i8count; /* number of big inode#s */
684 xfs_ino_t ino; /* entry inode number */
685 int offset; /* data offset */
686 xfs_dir2_sf_entry_t *sfep; /* shortform dir entry */
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200687 xfs_dir2_sf_hdr_t *sfp; /* shortform structure */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688
689 dp = args->dp;
690
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200691 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 offset = XFS_DIR2_DATA_FIRST_OFFSET;
Christoph Hellwig8bc38782011-07-08 14:35:03 +0200693 ino = xfs_dir2_sf_get_parent_ino(sfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 i8count = ino > XFS_DIR2_MAX_SHORT_INUM;
695
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000696 for (i = 0, sfep = xfs_dir2_sf_firstentry(sfp);
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200697 i < sfp->count;
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000698 i++, sfep = xfs_dir2_sf_nextentry(sfp, sfep)) {
699 ASSERT(xfs_dir2_sf_get_offset(sfep) >= offset);
Christoph Hellwig8bc38782011-07-08 14:35:03 +0200700 ino = xfs_dir2_sfe_get_ino(sfp, sfep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 i8count += ino > XFS_DIR2_MAX_SHORT_INUM;
702 offset =
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000703 xfs_dir2_sf_get_offset(sfep) +
704 xfs_dir2_data_entsize(sfep->namelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 }
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200706 ASSERT(i8count == sfp->i8count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 ASSERT(XFS_BIG_INUMS || i8count == 0);
708 ASSERT((char *)sfep - (char *)sfp == dp->i_d.di_size);
709 ASSERT(offset +
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200710 (sfp->count + 2) * (uint)sizeof(xfs_dir2_leaf_entry_t) +
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 (uint)sizeof(xfs_dir2_block_tail_t) <=
712 dp->i_mount->m_dirblksize);
713}
714#endif /* DEBUG */
715
716/*
717 * Create a new (shortform) directory.
718 */
719int /* error, always 0 */
720xfs_dir2_sf_create(
721 xfs_da_args_t *args, /* operation arguments */
722 xfs_ino_t pino) /* parent inode number */
723{
724 xfs_inode_t *dp; /* incore directory inode */
725 int i8count; /* parent inode is an 8-byte number */
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200726 xfs_dir2_sf_hdr_t *sfp; /* shortform structure */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 int size; /* directory size */
728
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000729 trace_xfs_dir2_sf_create(args);
730
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 dp = args->dp;
732
733 ASSERT(dp != NULL);
734 ASSERT(dp->i_d.di_size == 0);
735 /*
736 * If it's currently a zero-length extent file,
737 * convert it to local format.
738 */
739 if (dp->i_d.di_format == XFS_DINODE_FMT_EXTENTS) {
740 dp->i_df.if_flags &= ~XFS_IFEXTENTS; /* just in case */
741 dp->i_d.di_format = XFS_DINODE_FMT_LOCAL;
742 xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE);
743 dp->i_df.if_flags |= XFS_IFINLINE;
744 }
745 ASSERT(dp->i_df.if_flags & XFS_IFINLINE);
746 ASSERT(dp->i_df.if_bytes == 0);
747 i8count = pino > XFS_DIR2_MAX_SHORT_INUM;
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000748 size = xfs_dir2_sf_hdr_size(i8count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 /*
750 * Make a buffer for the data.
751 */
752 xfs_idata_realloc(dp, size, XFS_DATA_FORK);
753 /*
754 * Fill in the header,
755 */
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200756 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
757 sfp->i8count = i8count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 /*
759 * Now can put in the inode number, since i8count is set.
760 */
Christoph Hellwig8bc38782011-07-08 14:35:03 +0200761 xfs_dir2_sf_put_parent_ino(sfp, pino);
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200762 sfp->count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 dp->i_d.di_size = size;
764 xfs_dir2_sf_check(args);
765 xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE | XFS_ILOG_DDATA);
766 return 0;
767}
768
769int /* error */
770xfs_dir2_sf_getdents(
771 xfs_inode_t *dp, /* incore directory inode */
Christoph Hellwig051e7cd2007-08-28 13:58:24 +1000772 void *dirent,
773 xfs_off_t *offset,
774 filldir_t filldir)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 int i; /* shortform entry number */
777 xfs_mount_t *mp; /* filesystem mount point */
778 xfs_dir2_dataptr_t off; /* current entry's offset */
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 */
Christoph Hellwig051e7cd2007-08-28 13:58:24 +1000781 xfs_dir2_dataptr_t dot_offset;
782 xfs_dir2_dataptr_t dotdot_offset;
783 xfs_ino_t ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784
785 mp = dp->i_mount;
786
787 ASSERT(dp->i_df.if_flags & XFS_IFINLINE);
788 /*
789 * Give up if the directory is way too short.
790 */
791 if (dp->i_d.di_size < offsetof(xfs_dir2_sf_hdr_t, parent)) {
792 ASSERT(XFS_FORCED_SHUTDOWN(mp));
793 return XFS_ERROR(EIO);
794 }
795
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 ASSERT(dp->i_df.if_bytes == dp->i_d.di_size);
797 ASSERT(dp->i_df.if_u1.if_data != NULL);
798
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200799 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200801 ASSERT(dp->i_d.di_size >= xfs_dir2_sf_hdr_size(sfp->i8count));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802
803 /*
804 * If the block number in the offset is out of range, we're done.
805 */
Christoph Hellwig051e7cd2007-08-28 13:58:24 +1000806 if (xfs_dir2_dataptr_to_db(mp, *offset) > mp->m_dirdatablk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808
809 /*
Christoph Hellwig051e7cd2007-08-28 13:58:24 +1000810 * Precalculate offsets for . and .. as we will always need them.
811 *
812 * XXX(hch): the second argument is sometimes 0 and sometimes
813 * mp->m_dirdatablk.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 */
Christoph Hellwig051e7cd2007-08-28 13:58:24 +1000815 dot_offset = xfs_dir2_db_off_to_dataptr(mp, mp->m_dirdatablk,
816 XFS_DIR2_DATA_DOT_OFFSET);
817 dotdot_offset = xfs_dir2_db_off_to_dataptr(mp, mp->m_dirdatablk,
818 XFS_DIR2_DATA_DOTDOT_OFFSET);
819
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 /*
821 * Put . entry unless we're starting past it.
822 */
Christoph Hellwig051e7cd2007-08-28 13:58:24 +1000823 if (*offset <= dot_offset) {
Christoph Hellwiga19d9f82009-03-29 09:51:08 +0200824 if (filldir(dirent, ".", 1, dot_offset & 0x7fffffff, dp->i_ino, DT_DIR)) {
Christoph Hellwig15440312009-01-08 14:00:00 -0500825 *offset = dot_offset & 0x7fffffff;
Christoph Hellwig051e7cd2007-08-28 13:58:24 +1000826 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 }
828 }
829
830 /*
831 * Put .. entry unless we're starting past it.
832 */
Christoph Hellwig051e7cd2007-08-28 13:58:24 +1000833 if (*offset <= dotdot_offset) {
Christoph Hellwig8bc38782011-07-08 14:35:03 +0200834 ino = xfs_dir2_sf_get_parent_ino(sfp);
Christoph Hellwig15440312009-01-08 14:00:00 -0500835 if (filldir(dirent, "..", 2, dotdot_offset & 0x7fffffff, ino, DT_DIR)) {
836 *offset = dotdot_offset & 0x7fffffff;
Christoph Hellwig051e7cd2007-08-28 13:58:24 +1000837 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 }
839 }
840
841 /*
842 * Loop while there are more entries and put'ing works.
843 */
Christoph Hellwig051e7cd2007-08-28 13:58:24 +1000844 sfep = xfs_dir2_sf_firstentry(sfp);
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200845 for (i = 0; i < sfp->count; i++) {
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000846 off = xfs_dir2_db_off_to_dataptr(mp, mp->m_dirdatablk,
847 xfs_dir2_sf_get_offset(sfep));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848
Christoph Hellwig051e7cd2007-08-28 13:58:24 +1000849 if (*offset > off) {
850 sfep = xfs_dir2_sf_nextentry(sfp, sfep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 }
Christoph Hellwig051e7cd2007-08-28 13:58:24 +1000853
Christoph Hellwig8bc38782011-07-08 14:35:03 +0200854 ino = xfs_dir2_sfe_get_ino(sfp, sfep);
Dave Chinner4a24cb72010-01-20 10:48:05 +1100855 if (filldir(dirent, (char *)sfep->name, sfep->namelen,
Christoph Hellwig15440312009-01-08 14:00:00 -0500856 off & 0x7fffffff, ino, DT_UNKNOWN)) {
857 *offset = off & 0x7fffffff;
Christoph Hellwig051e7cd2007-08-28 13:58:24 +1000858 return 0;
859 }
860 sfep = xfs_dir2_sf_nextentry(sfp, sfep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 }
862
Christoph Hellwig15440312009-01-08 14:00:00 -0500863 *offset = xfs_dir2_db_off_to_dataptr(mp, mp->m_dirdatablk + 1, 0) &
864 0x7fffffff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 return 0;
866}
867
868/*
869 * Lookup an entry in a shortform directory.
870 * Returns EEXIST if found, ENOENT if not found.
871 */
872int /* error */
873xfs_dir2_sf_lookup(
874 xfs_da_args_t *args) /* operation arguments */
875{
876 xfs_inode_t *dp; /* incore directory inode */
877 int i; /* entry index */
Barry Naujok384f3ce2008-05-21 16:58:22 +1000878 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 xfs_dir2_sf_entry_t *sfep; /* shortform directory entry */
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200880 xfs_dir2_sf_hdr_t *sfp; /* shortform structure */
Barry Naujok5163f952008-05-21 16:41:01 +1000881 enum xfs_dacmp cmp; /* comparison result */
Barry Naujok384f3ce2008-05-21 16:58:22 +1000882 xfs_dir2_sf_entry_t *ci_sfep; /* case-insens. entry */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000884 trace_xfs_dir2_sf_lookup(args);
885
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 xfs_dir2_sf_check(args);
887 dp = args->dp;
888
889 ASSERT(dp->i_df.if_flags & XFS_IFINLINE);
890 /*
891 * Bail out if the directory is way too short.
892 */
893 if (dp->i_d.di_size < offsetof(xfs_dir2_sf_hdr_t, parent)) {
894 ASSERT(XFS_FORCED_SHUTDOWN(dp->i_mount));
895 return XFS_ERROR(EIO);
896 }
897 ASSERT(dp->i_df.if_bytes == dp->i_d.di_size);
898 ASSERT(dp->i_df.if_u1.if_data != NULL);
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200899 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
900 ASSERT(dp->i_d.di_size >= xfs_dir2_sf_hdr_size(sfp->i8count));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 /*
902 * Special case for .
903 */
904 if (args->namelen == 1 && args->name[0] == '.') {
905 args->inumber = dp->i_ino;
Barry Naujok5163f952008-05-21 16:41:01 +1000906 args->cmpresult = XFS_CMP_EXACT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 return XFS_ERROR(EEXIST);
908 }
909 /*
910 * Special case for ..
911 */
912 if (args->namelen == 2 &&
913 args->name[0] == '.' && args->name[1] == '.') {
Christoph Hellwig8bc38782011-07-08 14:35:03 +0200914 args->inumber = xfs_dir2_sf_get_parent_ino(sfp);
Barry Naujok5163f952008-05-21 16:41:01 +1000915 args->cmpresult = XFS_CMP_EXACT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 return XFS_ERROR(EEXIST);
917 }
918 /*
919 * Loop over all the entries trying to match ours.
920 */
Barry Naujok384f3ce2008-05-21 16:58:22 +1000921 ci_sfep = NULL;
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200922 for (i = 0, sfep = xfs_dir2_sf_firstentry(sfp); i < sfp->count;
Barry Naujok5163f952008-05-21 16:41:01 +1000923 i++, sfep = xfs_dir2_sf_nextentry(sfp, sfep)) {
924 /*
925 * Compare name and if it's an exact match, return the inode
926 * number. If it's the first case-insensitive match, store the
927 * inode number and continue looking for an exact match.
928 */
929 cmp = dp->i_mount->m_dirnameops->compname(args, sfep->name,
930 sfep->namelen);
931 if (cmp != XFS_CMP_DIFFERENT && cmp != args->cmpresult) {
932 args->cmpresult = cmp;
Christoph Hellwig8bc38782011-07-08 14:35:03 +0200933 args->inumber = xfs_dir2_sfe_get_ino(sfp, sfep);
Barry Naujok5163f952008-05-21 16:41:01 +1000934 if (cmp == XFS_CMP_EXACT)
935 return XFS_ERROR(EEXIST);
Barry Naujok384f3ce2008-05-21 16:58:22 +1000936 ci_sfep = sfep;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 }
938 }
Barry Naujok6a178102008-05-21 16:42:05 +1000939 ASSERT(args->op_flags & XFS_DA_OP_OKNOENT);
Barry Naujok5163f952008-05-21 16:41:01 +1000940 /*
941 * Here, we can only be doing a lookup (not a rename or replace).
Barry Naujok384f3ce2008-05-21 16:58:22 +1000942 * If a case-insensitive match was not found, return ENOENT.
Barry Naujok5163f952008-05-21 16:41:01 +1000943 */
Barry Naujok384f3ce2008-05-21 16:58:22 +1000944 if (!ci_sfep)
945 return XFS_ERROR(ENOENT);
946 /* otherwise process the CI match as required by the caller */
947 error = xfs_dir_cilookup_result(args, ci_sfep->name, ci_sfep->namelen);
948 return XFS_ERROR(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949}
950
951/*
952 * Remove an entry from a shortform directory.
953 */
954int /* error */
955xfs_dir2_sf_removename(
956 xfs_da_args_t *args)
957{
958 int byteoff; /* offset of removed entry */
959 xfs_inode_t *dp; /* incore directory inode */
960 int entsize; /* this entry's size */
961 int i; /* shortform entry index */
962 int newsize; /* new inode size */
963 int oldsize; /* old inode size */
964 xfs_dir2_sf_entry_t *sfep; /* shortform directory entry */
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200965 xfs_dir2_sf_hdr_t *sfp; /* shortform structure */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000967 trace_xfs_dir2_sf_removename(args);
968
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 dp = args->dp;
970
971 ASSERT(dp->i_df.if_flags & XFS_IFINLINE);
972 oldsize = (int)dp->i_d.di_size;
973 /*
974 * Bail out if the directory is way too short.
975 */
976 if (oldsize < offsetof(xfs_dir2_sf_hdr_t, parent)) {
977 ASSERT(XFS_FORCED_SHUTDOWN(dp->i_mount));
978 return XFS_ERROR(EIO);
979 }
980 ASSERT(dp->i_df.if_bytes == oldsize);
981 ASSERT(dp->i_df.if_u1.if_data != NULL);
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200982 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
983 ASSERT(oldsize >= xfs_dir2_sf_hdr_size(sfp->i8count));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 /*
985 * Loop over the old directory entries.
986 * Find the one we're deleting.
987 */
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200988 for (i = 0, sfep = xfs_dir2_sf_firstentry(sfp); i < sfp->count;
Barry Naujok5163f952008-05-21 16:41:01 +1000989 i++, sfep = xfs_dir2_sf_nextentry(sfp, sfep)) {
990 if (xfs_da_compname(args, sfep->name, sfep->namelen) ==
991 XFS_CMP_EXACT) {
Christoph Hellwig8bc38782011-07-08 14:35:03 +0200992 ASSERT(xfs_dir2_sfe_get_ino(sfp, sfep) ==
993 args->inumber);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 break;
995 }
996 }
997 /*
998 * Didn't find it.
999 */
Christoph Hellwigac8ba502011-07-08 14:35:13 +02001000 if (i == sfp->count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 return XFS_ERROR(ENOENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 /*
1003 * Calculate sizes.
1004 */
1005 byteoff = (int)((char *)sfep - (char *)sfp);
Christoph Hellwig78f70cd2011-07-08 14:35:19 +02001006 entsize = xfs_dir2_sf_entsize(sfp, args->namelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 newsize = oldsize - entsize;
1008 /*
1009 * Copy the part if any after the removed entry, sliding it down.
1010 */
1011 if (byteoff + entsize < oldsize)
1012 memmove((char *)sfp + byteoff, (char *)sfp + byteoff + entsize,
1013 oldsize - (byteoff + entsize));
1014 /*
1015 * Fix up the header and file size.
1016 */
Christoph Hellwigac8ba502011-07-08 14:35:13 +02001017 sfp->count--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 dp->i_d.di_size = newsize;
1019 /*
1020 * Reallocate, making it smaller.
1021 */
1022 xfs_idata_realloc(dp, newsize - oldsize, XFS_DATA_FORK);
Christoph Hellwigac8ba502011-07-08 14:35:13 +02001023 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024#if XFS_BIG_INUMS
1025 /*
1026 * Are we changing inode number size?
1027 */
1028 if (args->inumber > XFS_DIR2_MAX_SHORT_INUM) {
Christoph Hellwigac8ba502011-07-08 14:35:13 +02001029 if (sfp->i8count == 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 xfs_dir2_sf_toino4(args);
1031 else
Christoph Hellwigac8ba502011-07-08 14:35:13 +02001032 sfp->i8count--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 }
1034#endif
1035 xfs_dir2_sf_check(args);
1036 xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE | XFS_ILOG_DDATA);
1037 return 0;
1038}
1039
1040/*
1041 * Replace the inode number of an entry in a shortform directory.
1042 */
1043int /* error */
1044xfs_dir2_sf_replace(
1045 xfs_da_args_t *args) /* operation arguments */
1046{
1047 xfs_inode_t *dp; /* incore directory inode */
1048 int i; /* entry index */
1049#if XFS_BIG_INUMS || defined(DEBUG)
1050 xfs_ino_t ino=0; /* entry old inode number */
1051#endif
1052#if XFS_BIG_INUMS
1053 int i8elevated; /* sf_toino8 set i8count=1 */
1054#endif
1055 xfs_dir2_sf_entry_t *sfep; /* shortform directory entry */
Christoph Hellwigac8ba502011-07-08 14:35:13 +02001056 xfs_dir2_sf_hdr_t *sfp; /* shortform structure */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001058 trace_xfs_dir2_sf_replace(args);
1059
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 dp = args->dp;
1061
1062 ASSERT(dp->i_df.if_flags & XFS_IFINLINE);
1063 /*
1064 * Bail out if the shortform directory is way too small.
1065 */
1066 if (dp->i_d.di_size < offsetof(xfs_dir2_sf_hdr_t, parent)) {
1067 ASSERT(XFS_FORCED_SHUTDOWN(dp->i_mount));
1068 return XFS_ERROR(EIO);
1069 }
1070 ASSERT(dp->i_df.if_bytes == dp->i_d.di_size);
1071 ASSERT(dp->i_df.if_u1.if_data != NULL);
Christoph Hellwigac8ba502011-07-08 14:35:13 +02001072 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
1073 ASSERT(dp->i_d.di_size >= xfs_dir2_sf_hdr_size(sfp->i8count));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074#if XFS_BIG_INUMS
1075 /*
1076 * New inode number is large, and need to convert to 8-byte inodes.
1077 */
Christoph Hellwigac8ba502011-07-08 14:35:13 +02001078 if (args->inumber > XFS_DIR2_MAX_SHORT_INUM && sfp->i8count == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 int error; /* error return value */
1080 int newsize; /* new inode size */
1081
1082 newsize =
1083 dp->i_df.if_bytes +
Christoph Hellwigac8ba502011-07-08 14:35:13 +02001084 (sfp->count + 1) *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 ((uint)sizeof(xfs_dir2_ino8_t) -
1086 (uint)sizeof(xfs_dir2_ino4_t));
1087 /*
1088 * Won't fit as shortform, convert to block then do replace.
1089 */
1090 if (newsize > XFS_IFORK_DSIZE(dp)) {
1091 error = xfs_dir2_sf_to_block(args);
1092 if (error) {
1093 return error;
1094 }
1095 return xfs_dir2_block_replace(args);
1096 }
1097 /*
1098 * Still fits, convert to 8-byte now.
1099 */
1100 xfs_dir2_sf_toino8(args);
1101 i8elevated = 1;
Christoph Hellwigac8ba502011-07-08 14:35:13 +02001102 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 } else
1104 i8elevated = 0;
1105#endif
1106 ASSERT(args->namelen != 1 || args->name[0] != '.');
1107 /*
1108 * Replace ..'s entry.
1109 */
1110 if (args->namelen == 2 &&
1111 args->name[0] == '.' && args->name[1] == '.') {
1112#if XFS_BIG_INUMS || defined(DEBUG)
Christoph Hellwig8bc38782011-07-08 14:35:03 +02001113 ino = xfs_dir2_sf_get_parent_ino(sfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 ASSERT(args->inumber != ino);
1115#endif
Christoph Hellwig8bc38782011-07-08 14:35:03 +02001116 xfs_dir2_sf_put_parent_ino(sfp, args->inumber);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 }
1118 /*
1119 * Normal entry, look for the name.
1120 */
1121 else {
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001122 for (i = 0, sfep = xfs_dir2_sf_firstentry(sfp);
Christoph Hellwigac8ba502011-07-08 14:35:13 +02001123 i < sfp->count;
Barry Naujok5163f952008-05-21 16:41:01 +10001124 i++, sfep = xfs_dir2_sf_nextentry(sfp, sfep)) {
1125 if (xfs_da_compname(args, sfep->name, sfep->namelen) ==
1126 XFS_CMP_EXACT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127#if XFS_BIG_INUMS || defined(DEBUG)
Christoph Hellwig8bc38782011-07-08 14:35:03 +02001128 ino = xfs_dir2_sfe_get_ino(sfp, sfep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 ASSERT(args->inumber != ino);
1130#endif
Christoph Hellwig8bc38782011-07-08 14:35:03 +02001131 xfs_dir2_sfe_put_ino(sfp, sfep, args->inumber);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 break;
1133 }
1134 }
1135 /*
1136 * Didn't find it.
1137 */
Christoph Hellwigac8ba502011-07-08 14:35:13 +02001138 if (i == sfp->count) {
Barry Naujok6a178102008-05-21 16:42:05 +10001139 ASSERT(args->op_flags & XFS_DA_OP_OKNOENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140#if XFS_BIG_INUMS
1141 if (i8elevated)
1142 xfs_dir2_sf_toino4(args);
1143#endif
1144 return XFS_ERROR(ENOENT);
1145 }
1146 }
1147#if XFS_BIG_INUMS
1148 /*
1149 * See if the old number was large, the new number is small.
1150 */
1151 if (ino > XFS_DIR2_MAX_SHORT_INUM &&
1152 args->inumber <= XFS_DIR2_MAX_SHORT_INUM) {
1153 /*
1154 * And the old count was one, so need to convert to small.
1155 */
Christoph Hellwigac8ba502011-07-08 14:35:13 +02001156 if (sfp->i8count == 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 xfs_dir2_sf_toino4(args);
1158 else
Christoph Hellwigac8ba502011-07-08 14:35:13 +02001159 sfp->i8count--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 }
1161 /*
1162 * See if the old number was small, the new number is large.
1163 */
1164 if (ino <= XFS_DIR2_MAX_SHORT_INUM &&
1165 args->inumber > XFS_DIR2_MAX_SHORT_INUM) {
1166 /*
1167 * add to the i8count unless we just converted to 8-byte
1168 * inodes (which does an implied i8count = 1)
1169 */
Christoph Hellwigac8ba502011-07-08 14:35:13 +02001170 ASSERT(sfp->i8count != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 if (!i8elevated)
Christoph Hellwigac8ba502011-07-08 14:35:13 +02001172 sfp->i8count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 }
1174#endif
1175 xfs_dir2_sf_check(args);
1176 xfs_trans_log_inode(args->trans, dp, XFS_ILOG_DDATA);
1177 return 0;
1178}
1179
1180#if XFS_BIG_INUMS
1181/*
1182 * Convert from 8-byte inode numbers to 4-byte inode numbers.
1183 * The last 8-byte inode number is gone, but the count is still 1.
1184 */
1185static void
1186xfs_dir2_sf_toino4(
1187 xfs_da_args_t *args) /* operation arguments */
1188{
1189 char *buf; /* old dir's buffer */
1190 xfs_inode_t *dp; /* incore directory inode */
1191 int i; /* entry index */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 int newsize; /* new inode size */
1193 xfs_dir2_sf_entry_t *oldsfep; /* old sf entry */
Christoph Hellwigac8ba502011-07-08 14:35:13 +02001194 xfs_dir2_sf_hdr_t *oldsfp; /* old sf directory */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195 int oldsize; /* old inode size */
1196 xfs_dir2_sf_entry_t *sfep; /* new sf entry */
Christoph Hellwigac8ba502011-07-08 14:35:13 +02001197 xfs_dir2_sf_hdr_t *sfp; /* new sf directory */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001199 trace_xfs_dir2_sf_toino4(args);
1200
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201 dp = args->dp;
1202
1203 /*
1204 * Copy the old directory to the buffer.
1205 * Then nuke it from the inode, and add the new buffer to the inode.
1206 * Don't want xfs_idata_realloc copying the data here.
1207 */
1208 oldsize = dp->i_df.if_bytes;
1209 buf = kmem_alloc(oldsize, KM_SLEEP);
Christoph Hellwigac8ba502011-07-08 14:35:13 +02001210 oldsfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
1211 ASSERT(oldsfp->i8count == 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 memcpy(buf, oldsfp, oldsize);
1213 /*
1214 * Compute the new inode size.
1215 */
1216 newsize =
1217 oldsize -
Christoph Hellwigac8ba502011-07-08 14:35:13 +02001218 (oldsfp->count + 1) *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 ((uint)sizeof(xfs_dir2_ino8_t) - (uint)sizeof(xfs_dir2_ino4_t));
1220 xfs_idata_realloc(dp, -oldsize, XFS_DATA_FORK);
1221 xfs_idata_realloc(dp, newsize, XFS_DATA_FORK);
1222 /*
1223 * Reset our pointers, the data has moved.
1224 */
Christoph Hellwigac8ba502011-07-08 14:35:13 +02001225 oldsfp = (xfs_dir2_sf_hdr_t *)buf;
1226 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 /*
1228 * Fill in the new header.
1229 */
Christoph Hellwigac8ba502011-07-08 14:35:13 +02001230 sfp->count = oldsfp->count;
1231 sfp->i8count = 0;
Christoph Hellwig8bc38782011-07-08 14:35:03 +02001232 xfs_dir2_sf_put_parent_ino(sfp, xfs_dir2_sf_get_parent_ino(oldsfp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 /*
1234 * Copy the entries field by field.
1235 */
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001236 for (i = 0, sfep = xfs_dir2_sf_firstentry(sfp),
1237 oldsfep = xfs_dir2_sf_firstentry(oldsfp);
Christoph Hellwigac8ba502011-07-08 14:35:13 +02001238 i < sfp->count;
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001239 i++, sfep = xfs_dir2_sf_nextentry(sfp, sfep),
1240 oldsfep = xfs_dir2_sf_nextentry(oldsfp, oldsfep)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 sfep->namelen = oldsfep->namelen;
1242 sfep->offset = oldsfep->offset;
1243 memcpy(sfep->name, oldsfep->name, sfep->namelen);
Christoph Hellwig8bc38782011-07-08 14:35:03 +02001244 xfs_dir2_sfe_put_ino(sfp, sfep,
1245 xfs_dir2_sfe_get_ino(oldsfp, oldsfep));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 }
1247 /*
1248 * Clean up the inode.
1249 */
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10001250 kmem_free(buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 dp->i_d.di_size = newsize;
1252 xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE | XFS_ILOG_DDATA);
1253}
1254
1255/*
1256 * Convert from 4-byte inode numbers to 8-byte inode numbers.
1257 * The new 8-byte inode number is not there yet, we leave with the
1258 * count 1 but no corresponding entry.
1259 */
1260static void
1261xfs_dir2_sf_toino8(
1262 xfs_da_args_t *args) /* operation arguments */
1263{
1264 char *buf; /* old dir's buffer */
1265 xfs_inode_t *dp; /* incore directory inode */
1266 int i; /* entry index */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 int newsize; /* new inode size */
1268 xfs_dir2_sf_entry_t *oldsfep; /* old sf entry */
Christoph Hellwigac8ba502011-07-08 14:35:13 +02001269 xfs_dir2_sf_hdr_t *oldsfp; /* old sf directory */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 int oldsize; /* old inode size */
1271 xfs_dir2_sf_entry_t *sfep; /* new sf entry */
Christoph Hellwigac8ba502011-07-08 14:35:13 +02001272 xfs_dir2_sf_hdr_t *sfp; /* new sf directory */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001274 trace_xfs_dir2_sf_toino8(args);
1275
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 dp = args->dp;
1277
1278 /*
1279 * Copy the old directory to the buffer.
1280 * Then nuke it from the inode, and add the new buffer to the inode.
1281 * Don't want xfs_idata_realloc copying the data here.
1282 */
1283 oldsize = dp->i_df.if_bytes;
1284 buf = kmem_alloc(oldsize, KM_SLEEP);
Christoph Hellwigac8ba502011-07-08 14:35:13 +02001285 oldsfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
1286 ASSERT(oldsfp->i8count == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 memcpy(buf, oldsfp, oldsize);
1288 /*
1289 * Compute the new inode size.
1290 */
1291 newsize =
1292 oldsize +
Christoph Hellwigac8ba502011-07-08 14:35:13 +02001293 (oldsfp->count + 1) *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294 ((uint)sizeof(xfs_dir2_ino8_t) - (uint)sizeof(xfs_dir2_ino4_t));
1295 xfs_idata_realloc(dp, -oldsize, XFS_DATA_FORK);
1296 xfs_idata_realloc(dp, newsize, XFS_DATA_FORK);
1297 /*
1298 * Reset our pointers, the data has moved.
1299 */
Christoph Hellwigac8ba502011-07-08 14:35:13 +02001300 oldsfp = (xfs_dir2_sf_hdr_t *)buf;
1301 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 /*
1303 * Fill in the new header.
1304 */
Christoph Hellwigac8ba502011-07-08 14:35:13 +02001305 sfp->count = oldsfp->count;
1306 sfp->i8count = 1;
Christoph Hellwig8bc38782011-07-08 14:35:03 +02001307 xfs_dir2_sf_put_parent_ino(sfp, xfs_dir2_sf_get_parent_ino(oldsfp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308 /*
1309 * Copy the entries field by field.
1310 */
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001311 for (i = 0, sfep = xfs_dir2_sf_firstentry(sfp),
1312 oldsfep = xfs_dir2_sf_firstentry(oldsfp);
Christoph Hellwigac8ba502011-07-08 14:35:13 +02001313 i < sfp->count;
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001314 i++, sfep = xfs_dir2_sf_nextentry(sfp, sfep),
1315 oldsfep = xfs_dir2_sf_nextentry(oldsfp, oldsfep)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 sfep->namelen = oldsfep->namelen;
1317 sfep->offset = oldsfep->offset;
1318 memcpy(sfep->name, oldsfep->name, sfep->namelen);
Christoph Hellwig8bc38782011-07-08 14:35:03 +02001319 xfs_dir2_sfe_put_ino(sfp, sfep,
1320 xfs_dir2_sfe_get_ino(oldsfp, oldsfep));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 }
1322 /*
1323 * Clean up the inode.
1324 */
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10001325 kmem_free(buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326 dp->i_d.di_size = newsize;
1327 xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE | XFS_ILOG_DDATA);
1328}
1329#endif /* XFS_BIG_INUMS */