blob: 73881c9f40d67d21c7d5063394d64ca64ed38f81 [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"
Dave Chinnera4fbe6a2013-10-23 10:51:50 +110020#include "xfs_format.h"
Dave Chinner239880e2013-10-23 10:50:10 +110021#include "xfs_log_format.h"
22#include "xfs_trans_resv.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include "xfs_sb.h"
David Chinnerda353b02007-08-28 14:00:13 +100024#include "xfs_ag.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include "xfs_mount.h"
Dave Chinner57062782013-10-15 09:17:51 +110026#include "xfs_da_format.h"
Nathan Scotta844f452005-11-02 14:38:42 +110027#include "xfs_da_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include "xfs_inode.h"
Dave Chinner239880e2013-10-23 10:50:10 +110029#include "xfs_trans.h"
Nathan Scotta844f452005-11-02 14:38:42 +110030#include "xfs_inode_item.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include "xfs_error.h"
Dave Chinner2b9ab5a2013-08-12 20:49:37 +100032#include "xfs_dir2.h"
Christoph Hellwig57926642011-07-13 13:43:48 +020033#include "xfs_dir2_priv.h"
Christoph Hellwig0b1b2132009-12-14 23:14:59 +000034#include "xfs_trace.h"
Dave Chinnera4fbe6a2013-10-23 10:51:50 +110035#include "xfs_dinode.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
37/*
38 * Prototypes for internal functions.
39 */
40static void xfs_dir2_sf_addname_easy(xfs_da_args_t *args,
41 xfs_dir2_sf_entry_t *sfep,
42 xfs_dir2_data_aoff_t offset,
43 int new_isize);
44static void xfs_dir2_sf_addname_hard(xfs_da_args_t *args, int objchange,
45 int new_isize);
46static int xfs_dir2_sf_addname_pick(xfs_da_args_t *args, int objchange,
47 xfs_dir2_sf_entry_t **sfepp,
48 xfs_dir2_data_aoff_t *offsetp);
49#ifdef DEBUG
50static void xfs_dir2_sf_check(xfs_da_args_t *args);
51#else
52#define xfs_dir2_sf_check(args)
53#endif /* DEBUG */
54#if XFS_BIG_INUMS
55static void xfs_dir2_sf_toino4(xfs_da_args_t *args);
56static void xfs_dir2_sf_toino8(xfs_da_args_t *args);
57#endif /* XFS_BIG_INUMS */
58
Christoph Hellwig8bc38782011-07-08 14:35:03 +020059/*
60 * Inode numbers in short-form directories can come in two versions,
61 * either 4 bytes or 8 bytes wide. These helpers deal with the
62 * two forms transparently by looking at the headers i8count field.
Christoph Hellwig218106a2011-07-08 14:35:58 +020063 *
64 * For 64-bit inode number the most significant byte must be zero.
Christoph Hellwig8bc38782011-07-08 14:35:03 +020065 */
66static xfs_ino_t
67xfs_dir2_sf_get_ino(
Christoph Hellwigac8ba502011-07-08 14:35:13 +020068 struct xfs_dir2_sf_hdr *hdr,
Christoph Hellwig8bc38782011-07-08 14:35:03 +020069 xfs_dir2_inou_t *from)
70{
Christoph Hellwigac8ba502011-07-08 14:35:13 +020071 if (hdr->i8count)
Christoph Hellwig218106a2011-07-08 14:35:58 +020072 return get_unaligned_be64(&from->i8.i) & 0x00ffffffffffffffULL;
Christoph Hellwig8bc38782011-07-08 14:35:03 +020073 else
Christoph Hellwig218106a2011-07-08 14:35:58 +020074 return get_unaligned_be32(&from->i4.i);
Christoph Hellwig8bc38782011-07-08 14:35:03 +020075}
76
77static void
78xfs_dir2_sf_put_ino(
Christoph Hellwigac8ba502011-07-08 14:35:13 +020079 struct xfs_dir2_sf_hdr *hdr,
Christoph Hellwig8bc38782011-07-08 14:35:03 +020080 xfs_dir2_inou_t *to,
81 xfs_ino_t ino)
82{
Christoph Hellwig218106a2011-07-08 14:35:58 +020083 ASSERT((ino & 0xff00000000000000ULL) == 0);
84
Christoph Hellwigac8ba502011-07-08 14:35:13 +020085 if (hdr->i8count)
Christoph Hellwig218106a2011-07-08 14:35:58 +020086 put_unaligned_be64(ino, &to->i8.i);
Christoph Hellwig8bc38782011-07-08 14:35:03 +020087 else
Christoph Hellwig218106a2011-07-08 14:35:58 +020088 put_unaligned_be32(ino, &to->i4.i);
Christoph Hellwig8bc38782011-07-08 14:35:03 +020089}
90
91xfs_ino_t
92xfs_dir2_sf_get_parent_ino(
Christoph Hellwigac8ba502011-07-08 14:35:13 +020093 struct xfs_dir2_sf_hdr *hdr)
Christoph Hellwig8bc38782011-07-08 14:35:03 +020094{
Christoph Hellwigac8ba502011-07-08 14:35:13 +020095 return xfs_dir2_sf_get_ino(hdr, &hdr->parent);
Christoph Hellwig8bc38782011-07-08 14:35:03 +020096}
97
Dave Chinner2b9ab5a2013-08-12 20:49:37 +100098void
Christoph Hellwig8bc38782011-07-08 14:35:03 +020099xfs_dir2_sf_put_parent_ino(
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200100 struct xfs_dir2_sf_hdr *hdr,
Christoph Hellwig8bc38782011-07-08 14:35:03 +0200101 xfs_ino_t ino)
102{
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200103 xfs_dir2_sf_put_ino(hdr, &hdr->parent, ino);
Christoph Hellwig8bc38782011-07-08 14:35:03 +0200104}
105
106/*
107 * In short-form directory entries the inode numbers are stored at variable
Dave Chinner0cb97762013-08-12 20:50:09 +1000108 * offset behind the entry name. If the entry stores a filetype value, then it
109 * sits between the name and the inode number. Hence the inode numbers may only
110 * be accessed through the helpers below.
Christoph Hellwig8bc38782011-07-08 14:35:03 +0200111 */
112static xfs_dir2_inou_t *
Dave Chinner0cb97762013-08-12 20:50:09 +1000113xfs_dir3_sfe_inop(
114 struct xfs_mount *mp,
Christoph Hellwig8bc38782011-07-08 14:35:03 +0200115 struct xfs_dir2_sf_entry *sfep)
116{
Dave Chinner0cb97762013-08-12 20:50:09 +1000117 __uint8_t *ptr = &sfep->name[sfep->namelen];
118 if (xfs_sb_version_hasftype(&mp->m_sb))
119 ptr++;
120 return (xfs_dir2_inou_t *)ptr;
Christoph Hellwig8bc38782011-07-08 14:35:03 +0200121}
122
123xfs_ino_t
Dave Chinner0cb97762013-08-12 20:50:09 +1000124xfs_dir3_sfe_get_ino(
125 struct xfs_mount *mp,
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200126 struct xfs_dir2_sf_hdr *hdr,
Christoph Hellwig8bc38782011-07-08 14:35:03 +0200127 struct xfs_dir2_sf_entry *sfep)
128{
Dave Chinner0cb97762013-08-12 20:50:09 +1000129 return xfs_dir2_sf_get_ino(hdr, xfs_dir3_sfe_inop(mp, sfep));
Christoph Hellwig8bc38782011-07-08 14:35:03 +0200130}
131
Dave Chinner50fc5f72013-09-02 12:15:59 +1000132void
Dave Chinner0cb97762013-08-12 20:50:09 +1000133xfs_dir3_sfe_put_ino(
134 struct xfs_mount *mp,
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200135 struct xfs_dir2_sf_hdr *hdr,
Christoph Hellwig8bc38782011-07-08 14:35:03 +0200136 struct xfs_dir2_sf_entry *sfep,
137 xfs_ino_t ino)
138{
Dave Chinner0cb97762013-08-12 20:50:09 +1000139 xfs_dir2_sf_put_ino(hdr, xfs_dir3_sfe_inop(mp, sfep), ino);
Christoph Hellwig8bc38782011-07-08 14:35:03 +0200140}
141
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142/*
143 * Given a block directory (dp/block), calculate its size as a shortform (sf)
144 * directory and a header for the sf directory, if it will fit it the
145 * space currently present in the inode. If it won't fit, the output
146 * size is too big (but not accurate).
147 */
148int /* size for sf form */
149xfs_dir2_block_sfsize(
150 xfs_inode_t *dp, /* incore inode pointer */
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +0200151 xfs_dir2_data_hdr_t *hdr, /* block directory data */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 xfs_dir2_sf_hdr_t *sfhp) /* output: header for sf form */
153{
154 xfs_dir2_dataptr_t addr; /* data entry address */
155 xfs_dir2_leaf_entry_t *blp; /* leaf area of the block */
156 xfs_dir2_block_tail_t *btp; /* tail area of the block */
157 int count; /* shortform entry count */
158 xfs_dir2_data_entry_t *dep; /* data entry in the block */
159 int i; /* block entry index */
160 int i8count; /* count of big-inode entries */
161 int isdot; /* entry is "." */
162 int isdotdot; /* entry is ".." */
163 xfs_mount_t *mp; /* mount structure pointer */
164 int namelen; /* total name bytes */
Christoph Hellwig5bde1ba92005-11-02 15:06:18 +1100165 xfs_ino_t parent = 0; /* parent inode number */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 int size=0; /* total computed size */
Dave Chinner0cb97762013-08-12 20:50:09 +1000167 int has_ftype;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168
169 mp = dp->i_mount;
170
Dave Chinner0cb97762013-08-12 20:50:09 +1000171 /*
172 * if there is a filetype field, add the extra byte to the namelen
173 * for each entry that we see.
174 */
175 has_ftype = xfs_sb_version_hasftype(&mp->m_sb) ? 1 : 0;
176
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 count = i8count = namelen = 0;
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +0200178 btp = xfs_dir2_block_tail_p(mp, hdr);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000179 blp = xfs_dir2_block_leaf_p(btp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180
181 /*
182 * Iterate over the block's data entries by using the leaf pointers.
183 */
Nathan Scotte922fff2006-03-17 17:27:56 +1100184 for (i = 0; i < be32_to_cpu(btp->count); i++) {
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100185 if ((addr = be32_to_cpu(blp[i].address)) == XFS_DIR2_NULL_DATAPTR)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 continue;
187 /*
188 * Calculate the pointer to the entry at hand.
189 */
190 dep = (xfs_dir2_data_entry_t *)
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +0200191 ((char *)hdr + xfs_dir2_dataptr_to_off(mp, addr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 /*
193 * Detect . and .., so we can special-case them.
194 * . is not included in sf directories.
195 * .. is included by just the parent inode number.
196 */
197 isdot = dep->namelen == 1 && dep->name[0] == '.';
198 isdotdot =
199 dep->namelen == 2 &&
200 dep->name[0] == '.' && dep->name[1] == '.';
201#if XFS_BIG_INUMS
202 if (!isdot)
Christoph Hellwigff9901c2006-06-09 14:48:37 +1000203 i8count += be64_to_cpu(dep->inumber) > XFS_DIR2_MAX_SHORT_INUM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204#endif
Dave Chinner0cb97762013-08-12 20:50:09 +1000205 /* take into account the file type field */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 if (!isdot && !isdotdot) {
207 count++;
Dave Chinner0cb97762013-08-12 20:50:09 +1000208 namelen += dep->namelen + has_ftype;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 } else if (isdotdot)
Christoph Hellwigff9901c2006-06-09 14:48:37 +1000210 parent = be64_to_cpu(dep->inumber);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 /*
212 * Calculate the new size, see if we should give up yet.
213 */
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000214 size = xfs_dir2_sf_hdr_size(i8count) + /* header */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 count + /* namelen */
216 count * (uint)sizeof(xfs_dir2_sf_off_t) + /* offset */
217 namelen + /* name */
218 (i8count ? /* inumber */
219 (uint)sizeof(xfs_dir2_ino8_t) * count :
220 (uint)sizeof(xfs_dir2_ino4_t) * count);
221 if (size > XFS_IFORK_DSIZE(dp))
222 return size; /* size value is a failure */
223 }
224 /*
225 * Create the output header, if it worked.
226 */
227 sfhp->count = count;
228 sfhp->i8count = i8count;
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200229 xfs_dir2_sf_put_parent_ino(sfhp, parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 return size;
231}
232
233/*
234 * Convert a block format directory to shortform.
235 * Caller has already checked that it will fit, and built us a header.
236 */
237int /* error */
238xfs_dir2_block_to_sf(
239 xfs_da_args_t *args, /* operation arguments */
Dave Chinner1d9025e2012-06-22 18:50:14 +1000240 struct xfs_buf *bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 int size, /* shortform directory size */
242 xfs_dir2_sf_hdr_t *sfhp) /* shortform directory hdr */
243{
Christoph Hellwiga64b0412011-07-08 14:35:32 +0200244 xfs_dir2_data_hdr_t *hdr; /* block header */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 xfs_dir2_block_tail_t *btp; /* block tail pointer */
246 xfs_dir2_data_entry_t *dep; /* data entry pointer */
247 xfs_inode_t *dp; /* incore directory inode */
248 xfs_dir2_data_unused_t *dup; /* unused data pointer */
249 char *endptr; /* end of data entries */
250 int error; /* error return value */
251 int logflags; /* inode logging flags */
252 xfs_mount_t *mp; /* filesystem mount point */
253 char *ptr; /* current data pointer */
254 xfs_dir2_sf_entry_t *sfep; /* shortform entry */
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200255 xfs_dir2_sf_hdr_t *sfp; /* shortform directory header */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000257 trace_xfs_dir2_block_to_sf(args);
258
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 dp = args->dp;
260 mp = dp->i_mount;
261
262 /*
263 * Make a copy of the block data, so we can shrink the inode
264 * and add local data.
265 */
Christoph Hellwiga64b0412011-07-08 14:35:32 +0200266 hdr = kmem_alloc(mp->m_dirblksize, KM_SLEEP);
Dave Chinner1d9025e2012-06-22 18:50:14 +1000267 memcpy(hdr, bp->b_addr, mp->m_dirblksize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 logflags = XFS_ILOG_CORE;
269 if ((error = xfs_dir2_shrink_inode(args, mp->m_dirdatablk, bp))) {
270 ASSERT(error != ENOSPC);
271 goto out;
272 }
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +0200273
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 /*
275 * The buffer is now unconditionally gone, whether
276 * xfs_dir2_shrink_inode worked or not.
277 *
278 * Convert the inode to local format.
279 */
280 dp->i_df.if_flags &= ~XFS_IFEXTENTS;
281 dp->i_df.if_flags |= XFS_IFINLINE;
282 dp->i_d.di_format = XFS_DINODE_FMT_LOCAL;
283 ASSERT(dp->i_df.if_bytes == 0);
284 xfs_idata_realloc(dp, size, XFS_DATA_FORK);
285 logflags |= XFS_ILOG_DDATA;
286 /*
287 * Copy the header into the newly allocate local space.
288 */
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200289 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000290 memcpy(sfp, sfhp, xfs_dir2_sf_hdr_size(sfhp->i8count));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 dp->i_d.di_size = size;
292 /*
293 * Set up to loop over the block's entries.
294 */
Christoph Hellwiga64b0412011-07-08 14:35:32 +0200295 btp = xfs_dir2_block_tail_p(mp, hdr);
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100296 ptr = (char *)xfs_dir3_data_entry_p(hdr);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000297 endptr = (char *)xfs_dir2_block_leaf_p(btp);
298 sfep = xfs_dir2_sf_firstentry(sfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 /*
300 * Loop over the active and unused entries.
301 * Stop when we reach the leaf/tail portion of the block.
302 */
303 while (ptr < endptr) {
304 /*
305 * If it's unused, just skip over it.
306 */
307 dup = (xfs_dir2_data_unused_t *)ptr;
Nathan Scottad354eb2006-03-17 17:27:37 +1100308 if (be16_to_cpu(dup->freetag) == XFS_DIR2_DATA_FREE_TAG) {
309 ptr += be16_to_cpu(dup->length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 continue;
311 }
312 dep = (xfs_dir2_data_entry_t *)ptr;
313 /*
314 * Skip .
315 */
316 if (dep->namelen == 1 && dep->name[0] == '.')
Christoph Hellwigff9901c2006-06-09 14:48:37 +1000317 ASSERT(be64_to_cpu(dep->inumber) == dp->i_ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 /*
319 * Skip .., but make sure the inode number is right.
320 */
321 else if (dep->namelen == 2 &&
322 dep->name[0] == '.' && dep->name[1] == '.')
Christoph Hellwigff9901c2006-06-09 14:48:37 +1000323 ASSERT(be64_to_cpu(dep->inumber) ==
Christoph Hellwig8bc38782011-07-08 14:35:03 +0200324 xfs_dir2_sf_get_parent_ino(sfp));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 /*
326 * Normal entry, copy it into shortform.
327 */
328 else {
329 sfep->namelen = dep->namelen;
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000330 xfs_dir2_sf_put_offset(sfep,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 (xfs_dir2_data_aoff_t)
Christoph Hellwiga64b0412011-07-08 14:35:32 +0200332 ((char *)dep - (char *)hdr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 memcpy(sfep->name, dep->name, dep->namelen);
Dave Chinner0cb97762013-08-12 20:50:09 +1000334 xfs_dir3_sfe_put_ino(mp, sfp, sfep,
Christoph Hellwig8bc38782011-07-08 14:35:03 +0200335 be64_to_cpu(dep->inumber));
Dave Chinner1c55cec2013-08-12 20:50:10 +1000336 xfs_dir3_sfe_put_ftype(mp, sfp, sfep,
337 xfs_dir3_dirent_get_ftype(mp, dep));
Christoph Hellwig8bc38782011-07-08 14:35:03 +0200338
Dave Chinner32c54832013-10-29 22:11:46 +1100339 sfep = dp->d_ops->sf_nextentry(sfp, sfep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 }
Dave Chinner0cb97762013-08-12 20:50:09 +1000341 ptr += xfs_dir3_data_entsize(mp, dep->namelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 }
343 ASSERT((char *)sfep - (char *)sfp == size);
344 xfs_dir2_sf_check(args);
345out:
346 xfs_trans_log_inode(args->trans, dp, logflags);
Christoph Hellwiga64b0412011-07-08 14:35:32 +0200347 kmem_free(hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 return error;
349}
350
351/*
352 * Add a name to a shortform directory.
353 * There are two algorithms, "easy" and "hard" which we decide on
354 * before changing anything.
355 * Convert to block form if necessary, if the new entry won't fit.
356 */
357int /* error */
358xfs_dir2_sf_addname(
359 xfs_da_args_t *args) /* operation arguments */
360{
361 int add_entsize; /* size of the new entry */
362 xfs_inode_t *dp; /* incore directory inode */
363 int error; /* error return value */
364 int incr_isize; /* total change in size */
365 int new_isize; /* di_size after adding name */
366 int objchange; /* changing to 8-byte inodes */
Christoph Hellwig5bde1ba92005-11-02 15:06:18 +1100367 xfs_dir2_data_aoff_t offset = 0; /* offset for new entry */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 int old_isize; /* di_size before adding name */
369 int pick; /* which algorithm to use */
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200370 xfs_dir2_sf_hdr_t *sfp; /* shortform structure */
Christoph Hellwig5bde1ba92005-11-02 15:06:18 +1100371 xfs_dir2_sf_entry_t *sfep = NULL; /* shortform entry */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000373 trace_xfs_dir2_sf_addname(args);
374
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 ASSERT(xfs_dir2_sf_lookup(args) == ENOENT);
376 dp = args->dp;
377 ASSERT(dp->i_df.if_flags & XFS_IFINLINE);
378 /*
379 * Make sure the shortform value has some of its header.
380 */
381 if (dp->i_d.di_size < offsetof(xfs_dir2_sf_hdr_t, parent)) {
382 ASSERT(XFS_FORCED_SHUTDOWN(dp->i_mount));
383 return XFS_ERROR(EIO);
384 }
385 ASSERT(dp->i_df.if_bytes == dp->i_d.di_size);
386 ASSERT(dp->i_df.if_u1.if_data != NULL);
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200387 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
388 ASSERT(dp->i_d.di_size >= xfs_dir2_sf_hdr_size(sfp->i8count));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 /*
390 * Compute entry (and change in) size.
391 */
Dave Chinner32c54832013-10-29 22:11:46 +1100392 add_entsize = dp->d_ops->sf_entsize(sfp, args->namelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 incr_isize = add_entsize;
394 objchange = 0;
395#if XFS_BIG_INUMS
396 /*
397 * Do we have to change to 8 byte inodes?
398 */
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200399 if (args->inumber > XFS_DIR2_MAX_SHORT_INUM && sfp->i8count == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 /*
401 * Yes, adjust the entry size and the total size.
402 */
403 add_entsize +=
404 (uint)sizeof(xfs_dir2_ino8_t) -
405 (uint)sizeof(xfs_dir2_ino4_t);
406 incr_isize +=
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200407 (sfp->count + 2) *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 ((uint)sizeof(xfs_dir2_ino8_t) -
409 (uint)sizeof(xfs_dir2_ino4_t));
410 objchange = 1;
411 }
412#endif
413 old_isize = (int)dp->i_d.di_size;
414 new_isize = old_isize + incr_isize;
415 /*
416 * Won't fit as shortform any more (due to size),
417 * or the pick routine says it won't (due to offset values).
418 */
419 if (new_isize > XFS_IFORK_DSIZE(dp) ||
420 (pick =
421 xfs_dir2_sf_addname_pick(args, objchange, &sfep, &offset)) == 0) {
422 /*
423 * Just checking or no space reservation, it doesn't fit.
424 */
Barry Naujok6a178102008-05-21 16:42:05 +1000425 if ((args->op_flags & XFS_DA_OP_JUSTCHECK) || args->total == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 return XFS_ERROR(ENOSPC);
427 /*
428 * Convert to block form then add the name.
429 */
430 error = xfs_dir2_sf_to_block(args);
431 if (error)
432 return error;
433 return xfs_dir2_block_addname(args);
434 }
435 /*
436 * Just checking, it fits.
437 */
Barry Naujok6a178102008-05-21 16:42:05 +1000438 if (args->op_flags & XFS_DA_OP_JUSTCHECK)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 return 0;
440 /*
441 * Do it the easy way - just add it at the end.
442 */
443 if (pick == 1)
444 xfs_dir2_sf_addname_easy(args, sfep, offset, new_isize);
445 /*
446 * Do it the hard way - look for a place to insert the new entry.
447 * Convert to 8 byte inode numbers first if necessary.
448 */
449 else {
450 ASSERT(pick == 2);
451#if XFS_BIG_INUMS
452 if (objchange)
453 xfs_dir2_sf_toino8(args);
454#endif
455 xfs_dir2_sf_addname_hard(args, objchange, new_isize);
456 }
457 xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE | XFS_ILOG_DDATA);
458 return 0;
459}
460
461/*
462 * Add the new entry the "easy" way.
463 * This is copying the old directory and adding the new entry at the end.
464 * Since it's sorted by "offset" we need room after the last offset
465 * that's already there, and then room to convert to a block directory.
466 * This is already checked by the pick routine.
467 */
468static void
469xfs_dir2_sf_addname_easy(
470 xfs_da_args_t *args, /* operation arguments */
471 xfs_dir2_sf_entry_t *sfep, /* pointer to new entry */
472 xfs_dir2_data_aoff_t offset, /* offset to use for new ent */
473 int new_isize) /* new directory size */
474{
475 int byteoff; /* byte offset in sf dir */
476 xfs_inode_t *dp; /* incore directory inode */
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200477 xfs_dir2_sf_hdr_t *sfp; /* shortform structure */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478
479 dp = args->dp;
480
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200481 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 byteoff = (int)((char *)sfep - (char *)sfp);
483 /*
484 * Grow the in-inode space.
485 */
Dave Chinner32c54832013-10-29 22:11:46 +1100486 xfs_idata_realloc(dp, dp->d_ops->sf_entsize(sfp, args->namelen),
Dave Chinner0cb97762013-08-12 20:50:09 +1000487 XFS_DATA_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 /*
489 * Need to set up again due to realloc of the inode data.
490 */
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200491 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 sfep = (xfs_dir2_sf_entry_t *)((char *)sfp + byteoff);
493 /*
494 * Fill in the new entry.
495 */
496 sfep->namelen = args->namelen;
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000497 xfs_dir2_sf_put_offset(sfep, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 memcpy(sfep->name, args->name, sfep->namelen);
Dave Chinner0cb97762013-08-12 20:50:09 +1000499 xfs_dir3_sfe_put_ino(dp->i_mount, sfp, sfep, args->inumber);
Dave Chinner1c55cec2013-08-12 20:50:10 +1000500 xfs_dir3_sfe_put_ftype(dp->i_mount, sfp, sfep, args->filetype);
501
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 /*
503 * Update the header and inode.
504 */
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200505 sfp->count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506#if XFS_BIG_INUMS
507 if (args->inumber > XFS_DIR2_MAX_SHORT_INUM)
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200508 sfp->i8count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509#endif
510 dp->i_d.di_size = new_isize;
511 xfs_dir2_sf_check(args);
512}
513
514/*
515 * Add the new entry the "hard" way.
516 * The caller has already converted to 8 byte inode numbers if necessary,
517 * in which case we need to leave the i8count at 1.
518 * Find a hole that the new entry will fit into, and copy
519 * the first part of the entries, the new entry, and the last part of
520 * the entries.
521 */
522/* ARGSUSED */
523static void
524xfs_dir2_sf_addname_hard(
525 xfs_da_args_t *args, /* operation arguments */
526 int objchange, /* changing inode number size */
527 int new_isize) /* new directory size */
528{
529 int add_datasize; /* data size need for new ent */
530 char *buf; /* buffer for old */
531 xfs_inode_t *dp; /* incore directory inode */
532 int eof; /* reached end of old dir */
533 int nbytes; /* temp for byte copies */
534 xfs_dir2_data_aoff_t new_offset; /* next offset value */
535 xfs_dir2_data_aoff_t offset; /* current offset value */
536 int old_isize; /* previous di_size */
537 xfs_dir2_sf_entry_t *oldsfep; /* entry in original dir */
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200538 xfs_dir2_sf_hdr_t *oldsfp; /* original shortform dir */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 xfs_dir2_sf_entry_t *sfep; /* entry in new dir */
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200540 xfs_dir2_sf_hdr_t *sfp; /* new shortform dir */
Dave Chinner0cb97762013-08-12 20:50:09 +1000541 struct xfs_mount *mp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542
543 /*
544 * Copy the old directory to the stack buffer.
545 */
546 dp = args->dp;
Dave Chinner0cb97762013-08-12 20:50:09 +1000547 mp = dp->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200549 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 old_isize = (int)dp->i_d.di_size;
551 buf = kmem_alloc(old_isize, KM_SLEEP);
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200552 oldsfp = (xfs_dir2_sf_hdr_t *)buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 memcpy(oldsfp, sfp, old_isize);
554 /*
555 * Loop over the old directory finding the place we're going
556 * to insert the new entry.
557 * If it's going to end up at the end then oldsfep will point there.
558 */
Dave Chinner367993e2013-09-30 09:37:04 +1000559 for (offset = xfs_dir3_data_first_offset(mp),
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000560 oldsfep = xfs_dir2_sf_firstentry(oldsfp),
Dave Chinner0cb97762013-08-12 20:50:09 +1000561 add_datasize = xfs_dir3_data_entsize(mp, args->namelen),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 eof = (char *)oldsfep == &buf[old_isize];
563 !eof;
Dave Chinner0cb97762013-08-12 20:50:09 +1000564 offset = new_offset + xfs_dir3_data_entsize(mp, oldsfep->namelen),
Dave Chinner32c54832013-10-29 22:11:46 +1100565 oldsfep = dp->d_ops->sf_nextentry(oldsfp, oldsfep),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 eof = (char *)oldsfep == &buf[old_isize]) {
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000567 new_offset = xfs_dir2_sf_get_offset(oldsfep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 if (offset + add_datasize <= new_offset)
569 break;
570 }
571 /*
572 * Get rid of the old directory, then allocate space for
573 * the new one. We do this so xfs_idata_realloc won't copy
574 * the data.
575 */
576 xfs_idata_realloc(dp, -old_isize, XFS_DATA_FORK);
577 xfs_idata_realloc(dp, new_isize, XFS_DATA_FORK);
578 /*
579 * Reset the pointer since the buffer was reallocated.
580 */
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200581 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 /*
583 * Copy the first part of the directory, including the header.
584 */
585 nbytes = (int)((char *)oldsfep - (char *)oldsfp);
586 memcpy(sfp, oldsfp, nbytes);
587 sfep = (xfs_dir2_sf_entry_t *)((char *)sfp + nbytes);
588 /*
589 * Fill in the new entry, and update the header counts.
590 */
591 sfep->namelen = args->namelen;
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000592 xfs_dir2_sf_put_offset(sfep, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 memcpy(sfep->name, args->name, sfep->namelen);
Dave Chinner0cb97762013-08-12 20:50:09 +1000594 xfs_dir3_sfe_put_ino(mp, sfp, sfep, args->inumber);
Dave Chinner1c55cec2013-08-12 20:50:10 +1000595 xfs_dir3_sfe_put_ftype(mp, sfp, sfep, args->filetype);
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200596 sfp->count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597#if XFS_BIG_INUMS
598 if (args->inumber > XFS_DIR2_MAX_SHORT_INUM && !objchange)
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200599 sfp->i8count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600#endif
601 /*
602 * If there's more left to copy, do that.
603 */
604 if (!eof) {
Dave Chinner32c54832013-10-29 22:11:46 +1100605 sfep = dp->d_ops->sf_nextentry(sfp, sfep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 memcpy(sfep, oldsfep, old_isize - nbytes);
607 }
Denys Vlasenkof0e2d932008-05-19 16:31:57 +1000608 kmem_free(buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 dp->i_d.di_size = new_isize;
610 xfs_dir2_sf_check(args);
611}
612
613/*
614 * Decide if the new entry will fit at all.
615 * If it will fit, pick between adding the new entry to the end (easy)
616 * or somewhere else (hard).
617 * Return 0 (won't fit), 1 (easy), 2 (hard).
618 */
619/*ARGSUSED*/
620static int /* pick result */
621xfs_dir2_sf_addname_pick(
622 xfs_da_args_t *args, /* operation arguments */
623 int objchange, /* inode # size changes */
624 xfs_dir2_sf_entry_t **sfepp, /* out(1): new entry ptr */
625 xfs_dir2_data_aoff_t *offsetp) /* out(1): new offset */
626{
627 xfs_inode_t *dp; /* incore directory inode */
628 int holefit; /* found hole it will fit in */
629 int i; /* entry number */
630 xfs_mount_t *mp; /* filesystem mount point */
631 xfs_dir2_data_aoff_t offset; /* data block offset */
632 xfs_dir2_sf_entry_t *sfep; /* shortform entry */
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200633 xfs_dir2_sf_hdr_t *sfp; /* shortform structure */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 int size; /* entry's data size */
635 int used; /* data bytes used */
636
637 dp = args->dp;
638 mp = dp->i_mount;
639
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200640 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
Dave Chinner0cb97762013-08-12 20:50:09 +1000641 size = xfs_dir3_data_entsize(mp, args->namelen);
Dave Chinner367993e2013-09-30 09:37:04 +1000642 offset = xfs_dir3_data_first_offset(mp);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000643 sfep = xfs_dir2_sf_firstentry(sfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 holefit = 0;
645 /*
646 * Loop over sf entries.
647 * Keep track of data offset and whether we've seen a place
648 * to insert the new entry.
649 */
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200650 for (i = 0; i < sfp->count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 if (!holefit)
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000652 holefit = offset + size <= xfs_dir2_sf_get_offset(sfep);
653 offset = xfs_dir2_sf_get_offset(sfep) +
Dave Chinner0cb97762013-08-12 20:50:09 +1000654 xfs_dir3_data_entsize(mp, sfep->namelen);
Dave Chinner32c54832013-10-29 22:11:46 +1100655 sfep = dp->d_ops->sf_nextentry(sfp, sfep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 }
657 /*
658 * Calculate data bytes used excluding the new entry, if this
659 * was a data block (block form directory).
660 */
661 used = offset +
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200662 (sfp->count + 3) * (uint)sizeof(xfs_dir2_leaf_entry_t) +
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 (uint)sizeof(xfs_dir2_block_tail_t);
664 /*
665 * If it won't fit in a block form then we can't insert it,
666 * we'll go back, convert to block, then try the insert and convert
667 * to leaf.
668 */
669 if (used + (holefit ? 0 : size) > mp->m_dirblksize)
670 return 0;
671 /*
672 * If changing the inode number size, do it the hard way.
673 */
674#if XFS_BIG_INUMS
675 if (objchange) {
676 return 2;
677 }
678#else
679 ASSERT(objchange == 0);
680#endif
681 /*
682 * If it won't fit at the end then do it the hard way (use the hole).
683 */
684 if (used + size > mp->m_dirblksize)
685 return 2;
686 /*
687 * Do it the easy way.
688 */
689 *sfepp = sfep;
690 *offsetp = offset;
691 return 1;
692}
693
694#ifdef DEBUG
695/*
696 * Check consistency of shortform directory, assert if bad.
697 */
698static void
699xfs_dir2_sf_check(
700 xfs_da_args_t *args) /* operation arguments */
701{
702 xfs_inode_t *dp; /* incore directory inode */
703 int i; /* entry number */
704 int i8count; /* number of big inode#s */
705 xfs_ino_t ino; /* entry inode number */
706 int offset; /* data offset */
707 xfs_dir2_sf_entry_t *sfep; /* shortform dir entry */
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200708 xfs_dir2_sf_hdr_t *sfp; /* shortform structure */
Dave Chinner0cb97762013-08-12 20:50:09 +1000709 struct xfs_mount *mp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710
711 dp = args->dp;
Dave Chinner0cb97762013-08-12 20:50:09 +1000712 mp = dp->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200714 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
Dave Chinner367993e2013-09-30 09:37:04 +1000715 offset = xfs_dir3_data_first_offset(mp);
Christoph Hellwig8bc38782011-07-08 14:35:03 +0200716 ino = xfs_dir2_sf_get_parent_ino(sfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 i8count = ino > XFS_DIR2_MAX_SHORT_INUM;
718
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000719 for (i = 0, sfep = xfs_dir2_sf_firstentry(sfp);
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200720 i < sfp->count;
Dave Chinner32c54832013-10-29 22:11:46 +1100721 i++, sfep = dp->d_ops->sf_nextentry(sfp, sfep)) {
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000722 ASSERT(xfs_dir2_sf_get_offset(sfep) >= offset);
Dave Chinner0cb97762013-08-12 20:50:09 +1000723 ino = xfs_dir3_sfe_get_ino(mp, sfp, sfep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 i8count += ino > XFS_DIR2_MAX_SHORT_INUM;
725 offset =
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000726 xfs_dir2_sf_get_offset(sfep) +
Dave Chinner0cb97762013-08-12 20:50:09 +1000727 xfs_dir3_data_entsize(mp, sfep->namelen);
728 ASSERT(xfs_dir3_sfe_get_ftype(mp, sfp, sfep) <
729 XFS_DIR3_FT_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 }
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200731 ASSERT(i8count == sfp->i8count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 ASSERT(XFS_BIG_INUMS || i8count == 0);
733 ASSERT((char *)sfep - (char *)sfp == dp->i_d.di_size);
734 ASSERT(offset +
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200735 (sfp->count + 2) * (uint)sizeof(xfs_dir2_leaf_entry_t) +
Dave Chinner0cb97762013-08-12 20:50:09 +1000736 (uint)sizeof(xfs_dir2_block_tail_t) <= mp->m_dirblksize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737}
738#endif /* DEBUG */
739
740/*
741 * Create a new (shortform) directory.
742 */
743int /* error, always 0 */
744xfs_dir2_sf_create(
745 xfs_da_args_t *args, /* operation arguments */
746 xfs_ino_t pino) /* parent inode number */
747{
748 xfs_inode_t *dp; /* incore directory inode */
749 int i8count; /* parent inode is an 8-byte number */
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200750 xfs_dir2_sf_hdr_t *sfp; /* shortform structure */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 int size; /* directory size */
752
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000753 trace_xfs_dir2_sf_create(args);
754
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 dp = args->dp;
756
757 ASSERT(dp != NULL);
758 ASSERT(dp->i_d.di_size == 0);
759 /*
760 * If it's currently a zero-length extent file,
761 * convert it to local format.
762 */
763 if (dp->i_d.di_format == XFS_DINODE_FMT_EXTENTS) {
764 dp->i_df.if_flags &= ~XFS_IFEXTENTS; /* just in case */
765 dp->i_d.di_format = XFS_DINODE_FMT_LOCAL;
766 xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE);
767 dp->i_df.if_flags |= XFS_IFINLINE;
768 }
769 ASSERT(dp->i_df.if_flags & XFS_IFINLINE);
770 ASSERT(dp->i_df.if_bytes == 0);
771 i8count = pino > XFS_DIR2_MAX_SHORT_INUM;
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000772 size = xfs_dir2_sf_hdr_size(i8count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 /*
774 * Make a buffer for the data.
775 */
776 xfs_idata_realloc(dp, size, XFS_DATA_FORK);
777 /*
778 * Fill in the header,
779 */
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200780 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
781 sfp->i8count = i8count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 /*
783 * Now can put in the inode number, since i8count is set.
784 */
Christoph Hellwig8bc38782011-07-08 14:35:03 +0200785 xfs_dir2_sf_put_parent_ino(sfp, pino);
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200786 sfp->count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 dp->i_d.di_size = size;
788 xfs_dir2_sf_check(args);
789 xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE | XFS_ILOG_DDATA);
790 return 0;
791}
792
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793/*
794 * Lookup an entry in a shortform directory.
795 * Returns EEXIST if found, ENOENT if not found.
796 */
797int /* error */
798xfs_dir2_sf_lookup(
799 xfs_da_args_t *args) /* operation arguments */
800{
801 xfs_inode_t *dp; /* incore directory inode */
802 int i; /* entry index */
Barry Naujok384f3ce2008-05-21 16:58:22 +1000803 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 xfs_dir2_sf_entry_t *sfep; /* shortform directory entry */
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200805 xfs_dir2_sf_hdr_t *sfp; /* shortform structure */
Barry Naujok5163f952008-05-21 16:41:01 +1000806 enum xfs_dacmp cmp; /* comparison result */
Barry Naujok384f3ce2008-05-21 16:58:22 +1000807 xfs_dir2_sf_entry_t *ci_sfep; /* case-insens. entry */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000809 trace_xfs_dir2_sf_lookup(args);
810
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 xfs_dir2_sf_check(args);
812 dp = args->dp;
813
814 ASSERT(dp->i_df.if_flags & XFS_IFINLINE);
815 /*
816 * Bail out if the directory is way too short.
817 */
818 if (dp->i_d.di_size < offsetof(xfs_dir2_sf_hdr_t, parent)) {
819 ASSERT(XFS_FORCED_SHUTDOWN(dp->i_mount));
820 return XFS_ERROR(EIO);
821 }
822 ASSERT(dp->i_df.if_bytes == dp->i_d.di_size);
823 ASSERT(dp->i_df.if_u1.if_data != NULL);
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200824 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
825 ASSERT(dp->i_d.di_size >= xfs_dir2_sf_hdr_size(sfp->i8count));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 /*
827 * Special case for .
828 */
829 if (args->namelen == 1 && args->name[0] == '.') {
830 args->inumber = dp->i_ino;
Barry Naujok5163f952008-05-21 16:41:01 +1000831 args->cmpresult = XFS_CMP_EXACT;
Dave Chinner1c55cec2013-08-12 20:50:10 +1000832 args->filetype = XFS_DIR3_FT_DIR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 return XFS_ERROR(EEXIST);
834 }
835 /*
836 * Special case for ..
837 */
838 if (args->namelen == 2 &&
839 args->name[0] == '.' && args->name[1] == '.') {
Christoph Hellwig8bc38782011-07-08 14:35:03 +0200840 args->inumber = xfs_dir2_sf_get_parent_ino(sfp);
Barry Naujok5163f952008-05-21 16:41:01 +1000841 args->cmpresult = XFS_CMP_EXACT;
Dave Chinner1c55cec2013-08-12 20:50:10 +1000842 args->filetype = XFS_DIR3_FT_DIR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 return XFS_ERROR(EEXIST);
844 }
845 /*
846 * Loop over all the entries trying to match ours.
847 */
Barry Naujok384f3ce2008-05-21 16:58:22 +1000848 ci_sfep = NULL;
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200849 for (i = 0, sfep = xfs_dir2_sf_firstentry(sfp); i < sfp->count;
Dave Chinner32c54832013-10-29 22:11:46 +1100850 i++, sfep = dp->d_ops->sf_nextentry(sfp, sfep)) {
Barry Naujok5163f952008-05-21 16:41:01 +1000851 /*
852 * Compare name and if it's an exact match, return the inode
853 * number. If it's the first case-insensitive match, store the
854 * inode number and continue looking for an exact match.
855 */
856 cmp = dp->i_mount->m_dirnameops->compname(args, sfep->name,
857 sfep->namelen);
858 if (cmp != XFS_CMP_DIFFERENT && cmp != args->cmpresult) {
859 args->cmpresult = cmp;
Dave Chinner0cb97762013-08-12 20:50:09 +1000860 args->inumber = xfs_dir3_sfe_get_ino(dp->i_mount,
861 sfp, sfep);
Dave Chinner1c55cec2013-08-12 20:50:10 +1000862 args->filetype = xfs_dir3_sfe_get_ftype(dp->i_mount,
863 sfp, sfep);
Barry Naujok5163f952008-05-21 16:41:01 +1000864 if (cmp == XFS_CMP_EXACT)
865 return XFS_ERROR(EEXIST);
Barry Naujok384f3ce2008-05-21 16:58:22 +1000866 ci_sfep = sfep;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 }
868 }
Barry Naujok6a178102008-05-21 16:42:05 +1000869 ASSERT(args->op_flags & XFS_DA_OP_OKNOENT);
Barry Naujok5163f952008-05-21 16:41:01 +1000870 /*
871 * Here, we can only be doing a lookup (not a rename or replace).
Barry Naujok384f3ce2008-05-21 16:58:22 +1000872 * If a case-insensitive match was not found, return ENOENT.
Barry Naujok5163f952008-05-21 16:41:01 +1000873 */
Barry Naujok384f3ce2008-05-21 16:58:22 +1000874 if (!ci_sfep)
875 return XFS_ERROR(ENOENT);
876 /* otherwise process the CI match as required by the caller */
877 error = xfs_dir_cilookup_result(args, ci_sfep->name, ci_sfep->namelen);
878 return XFS_ERROR(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879}
880
881/*
882 * Remove an entry from a shortform directory.
883 */
884int /* error */
885xfs_dir2_sf_removename(
886 xfs_da_args_t *args)
887{
888 int byteoff; /* offset of removed entry */
889 xfs_inode_t *dp; /* incore directory inode */
890 int entsize; /* this entry's size */
891 int i; /* shortform entry index */
892 int newsize; /* new inode size */
893 int oldsize; /* old inode size */
894 xfs_dir2_sf_entry_t *sfep; /* shortform directory entry */
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200895 xfs_dir2_sf_hdr_t *sfp; /* shortform structure */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000897 trace_xfs_dir2_sf_removename(args);
898
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 dp = args->dp;
900
901 ASSERT(dp->i_df.if_flags & XFS_IFINLINE);
902 oldsize = (int)dp->i_d.di_size;
903 /*
904 * Bail out if the directory is way too short.
905 */
906 if (oldsize < offsetof(xfs_dir2_sf_hdr_t, parent)) {
907 ASSERT(XFS_FORCED_SHUTDOWN(dp->i_mount));
908 return XFS_ERROR(EIO);
909 }
910 ASSERT(dp->i_df.if_bytes == oldsize);
911 ASSERT(dp->i_df.if_u1.if_data != NULL);
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200912 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
913 ASSERT(oldsize >= xfs_dir2_sf_hdr_size(sfp->i8count));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 /*
915 * Loop over the old directory entries.
916 * Find the one we're deleting.
917 */
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200918 for (i = 0, sfep = xfs_dir2_sf_firstentry(sfp); i < sfp->count;
Dave Chinner32c54832013-10-29 22:11:46 +1100919 i++, sfep = dp->d_ops->sf_nextentry(sfp, sfep)) {
Barry Naujok5163f952008-05-21 16:41:01 +1000920 if (xfs_da_compname(args, sfep->name, sfep->namelen) ==
921 XFS_CMP_EXACT) {
Dave Chinner0cb97762013-08-12 20:50:09 +1000922 ASSERT(xfs_dir3_sfe_get_ino(dp->i_mount, sfp, sfep) ==
Christoph Hellwig8bc38782011-07-08 14:35:03 +0200923 args->inumber);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 break;
925 }
926 }
927 /*
928 * Didn't find it.
929 */
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200930 if (i == sfp->count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 return XFS_ERROR(ENOENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 /*
933 * Calculate sizes.
934 */
935 byteoff = (int)((char *)sfep - (char *)sfp);
Dave Chinner32c54832013-10-29 22:11:46 +1100936 entsize = dp->d_ops->sf_entsize(sfp, args->namelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 newsize = oldsize - entsize;
938 /*
939 * Copy the part if any after the removed entry, sliding it down.
940 */
941 if (byteoff + entsize < oldsize)
942 memmove((char *)sfp + byteoff, (char *)sfp + byteoff + entsize,
943 oldsize - (byteoff + entsize));
944 /*
945 * Fix up the header and file size.
946 */
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200947 sfp->count--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 dp->i_d.di_size = newsize;
949 /*
950 * Reallocate, making it smaller.
951 */
952 xfs_idata_realloc(dp, newsize - oldsize, XFS_DATA_FORK);
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200953 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954#if XFS_BIG_INUMS
955 /*
956 * Are we changing inode number size?
957 */
958 if (args->inumber > XFS_DIR2_MAX_SHORT_INUM) {
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200959 if (sfp->i8count == 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 xfs_dir2_sf_toino4(args);
961 else
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200962 sfp->i8count--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 }
964#endif
965 xfs_dir2_sf_check(args);
966 xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE | XFS_ILOG_DDATA);
967 return 0;
968}
969
970/*
971 * Replace the inode number of an entry in a shortform directory.
972 */
973int /* error */
974xfs_dir2_sf_replace(
975 xfs_da_args_t *args) /* operation arguments */
976{
977 xfs_inode_t *dp; /* incore directory inode */
978 int i; /* entry index */
979#if XFS_BIG_INUMS || defined(DEBUG)
980 xfs_ino_t ino=0; /* entry old inode number */
981#endif
982#if XFS_BIG_INUMS
983 int i8elevated; /* sf_toino8 set i8count=1 */
984#endif
985 xfs_dir2_sf_entry_t *sfep; /* shortform directory entry */
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200986 xfs_dir2_sf_hdr_t *sfp; /* shortform structure */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000988 trace_xfs_dir2_sf_replace(args);
989
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 dp = args->dp;
991
992 ASSERT(dp->i_df.if_flags & XFS_IFINLINE);
993 /*
994 * Bail out if the shortform directory is way too small.
995 */
996 if (dp->i_d.di_size < offsetof(xfs_dir2_sf_hdr_t, parent)) {
997 ASSERT(XFS_FORCED_SHUTDOWN(dp->i_mount));
998 return XFS_ERROR(EIO);
999 }
1000 ASSERT(dp->i_df.if_bytes == dp->i_d.di_size);
1001 ASSERT(dp->i_df.if_u1.if_data != NULL);
Christoph Hellwigac8ba502011-07-08 14:35:13 +02001002 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
1003 ASSERT(dp->i_d.di_size >= xfs_dir2_sf_hdr_size(sfp->i8count));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004#if XFS_BIG_INUMS
1005 /*
1006 * New inode number is large, and need to convert to 8-byte inodes.
1007 */
Christoph Hellwigac8ba502011-07-08 14:35:13 +02001008 if (args->inumber > XFS_DIR2_MAX_SHORT_INUM && sfp->i8count == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 int error; /* error return value */
1010 int newsize; /* new inode size */
1011
1012 newsize =
1013 dp->i_df.if_bytes +
Christoph Hellwigac8ba502011-07-08 14:35:13 +02001014 (sfp->count + 1) *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 ((uint)sizeof(xfs_dir2_ino8_t) -
1016 (uint)sizeof(xfs_dir2_ino4_t));
1017 /*
1018 * Won't fit as shortform, convert to block then do replace.
1019 */
1020 if (newsize > XFS_IFORK_DSIZE(dp)) {
1021 error = xfs_dir2_sf_to_block(args);
1022 if (error) {
1023 return error;
1024 }
1025 return xfs_dir2_block_replace(args);
1026 }
1027 /*
1028 * Still fits, convert to 8-byte now.
1029 */
1030 xfs_dir2_sf_toino8(args);
1031 i8elevated = 1;
Christoph Hellwigac8ba502011-07-08 14:35:13 +02001032 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 } else
1034 i8elevated = 0;
1035#endif
1036 ASSERT(args->namelen != 1 || args->name[0] != '.');
1037 /*
1038 * Replace ..'s entry.
1039 */
1040 if (args->namelen == 2 &&
1041 args->name[0] == '.' && args->name[1] == '.') {
1042#if XFS_BIG_INUMS || defined(DEBUG)
Christoph Hellwig8bc38782011-07-08 14:35:03 +02001043 ino = xfs_dir2_sf_get_parent_ino(sfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 ASSERT(args->inumber != ino);
1045#endif
Christoph Hellwig8bc38782011-07-08 14:35:03 +02001046 xfs_dir2_sf_put_parent_ino(sfp, args->inumber);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 }
1048 /*
1049 * Normal entry, look for the name.
1050 */
1051 else {
Dave Chinner0cb97762013-08-12 20:50:09 +10001052 for (i = 0, sfep = xfs_dir2_sf_firstentry(sfp); i < sfp->count;
Dave Chinner32c54832013-10-29 22:11:46 +11001053 i++, sfep = dp->d_ops->sf_nextentry(sfp, sfep)) {
Barry Naujok5163f952008-05-21 16:41:01 +10001054 if (xfs_da_compname(args, sfep->name, sfep->namelen) ==
1055 XFS_CMP_EXACT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056#if XFS_BIG_INUMS || defined(DEBUG)
Dave Chinner0cb97762013-08-12 20:50:09 +10001057 ino = xfs_dir3_sfe_get_ino(dp->i_mount,
1058 sfp, sfep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 ASSERT(args->inumber != ino);
1060#endif
Dave Chinner0cb97762013-08-12 20:50:09 +10001061 xfs_dir3_sfe_put_ino(dp->i_mount, sfp, sfep,
1062 args->inumber);
Dave Chinner1c55cec2013-08-12 20:50:10 +10001063 xfs_dir3_sfe_put_ftype(dp->i_mount, sfp, sfep,
1064 args->filetype);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 break;
1066 }
1067 }
1068 /*
1069 * Didn't find it.
1070 */
Christoph Hellwigac8ba502011-07-08 14:35:13 +02001071 if (i == sfp->count) {
Barry Naujok6a178102008-05-21 16:42:05 +10001072 ASSERT(args->op_flags & XFS_DA_OP_OKNOENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073#if XFS_BIG_INUMS
1074 if (i8elevated)
1075 xfs_dir2_sf_toino4(args);
1076#endif
1077 return XFS_ERROR(ENOENT);
1078 }
1079 }
1080#if XFS_BIG_INUMS
1081 /*
1082 * See if the old number was large, the new number is small.
1083 */
1084 if (ino > XFS_DIR2_MAX_SHORT_INUM &&
1085 args->inumber <= XFS_DIR2_MAX_SHORT_INUM) {
1086 /*
1087 * And the old count was one, so need to convert to small.
1088 */
Christoph Hellwigac8ba502011-07-08 14:35:13 +02001089 if (sfp->i8count == 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 xfs_dir2_sf_toino4(args);
1091 else
Christoph Hellwigac8ba502011-07-08 14:35:13 +02001092 sfp->i8count--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 }
1094 /*
1095 * See if the old number was small, the new number is large.
1096 */
1097 if (ino <= XFS_DIR2_MAX_SHORT_INUM &&
1098 args->inumber > XFS_DIR2_MAX_SHORT_INUM) {
1099 /*
1100 * add to the i8count unless we just converted to 8-byte
1101 * inodes (which does an implied i8count = 1)
1102 */
Christoph Hellwigac8ba502011-07-08 14:35:13 +02001103 ASSERT(sfp->i8count != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 if (!i8elevated)
Christoph Hellwigac8ba502011-07-08 14:35:13 +02001105 sfp->i8count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 }
1107#endif
1108 xfs_dir2_sf_check(args);
1109 xfs_trans_log_inode(args->trans, dp, XFS_ILOG_DDATA);
1110 return 0;
1111}
1112
1113#if XFS_BIG_INUMS
1114/*
1115 * Convert from 8-byte inode numbers to 4-byte inode numbers.
1116 * The last 8-byte inode number is gone, but the count is still 1.
1117 */
1118static void
1119xfs_dir2_sf_toino4(
1120 xfs_da_args_t *args) /* operation arguments */
1121{
1122 char *buf; /* old dir's buffer */
1123 xfs_inode_t *dp; /* incore directory inode */
1124 int i; /* entry index */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 int newsize; /* new inode size */
1126 xfs_dir2_sf_entry_t *oldsfep; /* old sf entry */
Christoph Hellwigac8ba502011-07-08 14:35:13 +02001127 xfs_dir2_sf_hdr_t *oldsfp; /* old sf directory */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 int oldsize; /* old inode size */
1129 xfs_dir2_sf_entry_t *sfep; /* new sf entry */
Christoph Hellwigac8ba502011-07-08 14:35:13 +02001130 xfs_dir2_sf_hdr_t *sfp; /* new sf directory */
Dave Chinner1c55cec2013-08-12 20:50:10 +10001131 struct xfs_mount *mp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001133 trace_xfs_dir2_sf_toino4(args);
1134
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 dp = args->dp;
Dave Chinner1c55cec2013-08-12 20:50:10 +10001136 mp = dp->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137
1138 /*
1139 * Copy the old directory to the buffer.
1140 * Then nuke it from the inode, and add the new buffer to the inode.
1141 * Don't want xfs_idata_realloc copying the data here.
1142 */
1143 oldsize = dp->i_df.if_bytes;
1144 buf = kmem_alloc(oldsize, KM_SLEEP);
Christoph Hellwigac8ba502011-07-08 14:35:13 +02001145 oldsfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
1146 ASSERT(oldsfp->i8count == 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 memcpy(buf, oldsfp, oldsize);
1148 /*
1149 * Compute the new inode size.
1150 */
1151 newsize =
1152 oldsize -
Christoph Hellwigac8ba502011-07-08 14:35:13 +02001153 (oldsfp->count + 1) *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 ((uint)sizeof(xfs_dir2_ino8_t) - (uint)sizeof(xfs_dir2_ino4_t));
1155 xfs_idata_realloc(dp, -oldsize, XFS_DATA_FORK);
1156 xfs_idata_realloc(dp, newsize, XFS_DATA_FORK);
1157 /*
1158 * Reset our pointers, the data has moved.
1159 */
Christoph Hellwigac8ba502011-07-08 14:35:13 +02001160 oldsfp = (xfs_dir2_sf_hdr_t *)buf;
1161 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 /*
1163 * Fill in the new header.
1164 */
Christoph Hellwigac8ba502011-07-08 14:35:13 +02001165 sfp->count = oldsfp->count;
1166 sfp->i8count = 0;
Christoph Hellwig8bc38782011-07-08 14:35:03 +02001167 xfs_dir2_sf_put_parent_ino(sfp, xfs_dir2_sf_get_parent_ino(oldsfp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168 /*
1169 * Copy the entries field by field.
1170 */
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001171 for (i = 0, sfep = xfs_dir2_sf_firstentry(sfp),
1172 oldsfep = xfs_dir2_sf_firstentry(oldsfp);
Christoph Hellwigac8ba502011-07-08 14:35:13 +02001173 i < sfp->count;
Dave Chinner32c54832013-10-29 22:11:46 +11001174 i++, sfep = dp->d_ops->sf_nextentry(sfp, sfep),
1175 oldsfep = dp->d_ops->sf_nextentry(oldsfp, oldsfep)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 sfep->namelen = oldsfep->namelen;
1177 sfep->offset = oldsfep->offset;
1178 memcpy(sfep->name, oldsfep->name, sfep->namelen);
Dave Chinner1c55cec2013-08-12 20:50:10 +10001179 xfs_dir3_sfe_put_ino(mp, sfp, sfep,
1180 xfs_dir3_sfe_get_ino(mp, oldsfp, oldsfep));
1181 xfs_dir3_sfe_put_ftype(mp, sfp, sfep,
1182 xfs_dir3_sfe_get_ftype(mp, oldsfp, oldsfep));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 }
1184 /*
1185 * Clean up the inode.
1186 */
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10001187 kmem_free(buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 dp->i_d.di_size = newsize;
1189 xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE | XFS_ILOG_DDATA);
1190}
1191
1192/*
1193 * Convert from 4-byte inode numbers to 8-byte inode numbers.
1194 * The new 8-byte inode number is not there yet, we leave with the
1195 * count 1 but no corresponding entry.
1196 */
1197static void
1198xfs_dir2_sf_toino8(
1199 xfs_da_args_t *args) /* operation arguments */
1200{
1201 char *buf; /* old dir's buffer */
1202 xfs_inode_t *dp; /* incore directory inode */
1203 int i; /* entry index */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 int newsize; /* new inode size */
1205 xfs_dir2_sf_entry_t *oldsfep; /* old sf entry */
Christoph Hellwigac8ba502011-07-08 14:35:13 +02001206 xfs_dir2_sf_hdr_t *oldsfp; /* old sf directory */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207 int oldsize; /* old inode size */
1208 xfs_dir2_sf_entry_t *sfep; /* new sf entry */
Christoph Hellwigac8ba502011-07-08 14:35:13 +02001209 xfs_dir2_sf_hdr_t *sfp; /* new sf directory */
Dave Chinner1c55cec2013-08-12 20:50:10 +10001210 struct xfs_mount *mp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001212 trace_xfs_dir2_sf_toino8(args);
1213
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 dp = args->dp;
Dave Chinner1c55cec2013-08-12 20:50:10 +10001215 mp = dp->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216
1217 /*
1218 * Copy the old directory to the buffer.
1219 * Then nuke it from the inode, and add the new buffer to the inode.
1220 * Don't want xfs_idata_realloc copying the data here.
1221 */
1222 oldsize = dp->i_df.if_bytes;
1223 buf = kmem_alloc(oldsize, KM_SLEEP);
Christoph Hellwigac8ba502011-07-08 14:35:13 +02001224 oldsfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
1225 ASSERT(oldsfp->i8count == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 memcpy(buf, oldsfp, oldsize);
1227 /*
1228 * Compute the new inode size.
1229 */
1230 newsize =
1231 oldsize +
Christoph Hellwigac8ba502011-07-08 14:35:13 +02001232 (oldsfp->count + 1) *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 ((uint)sizeof(xfs_dir2_ino8_t) - (uint)sizeof(xfs_dir2_ino4_t));
1234 xfs_idata_realloc(dp, -oldsize, XFS_DATA_FORK);
1235 xfs_idata_realloc(dp, newsize, XFS_DATA_FORK);
1236 /*
1237 * Reset our pointers, the data has moved.
1238 */
Christoph Hellwigac8ba502011-07-08 14:35:13 +02001239 oldsfp = (xfs_dir2_sf_hdr_t *)buf;
1240 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 /*
1242 * Fill in the new header.
1243 */
Christoph Hellwigac8ba502011-07-08 14:35:13 +02001244 sfp->count = oldsfp->count;
1245 sfp->i8count = 1;
Christoph Hellwig8bc38782011-07-08 14:35:03 +02001246 xfs_dir2_sf_put_parent_ino(sfp, xfs_dir2_sf_get_parent_ino(oldsfp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 /*
1248 * Copy the entries field by field.
1249 */
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001250 for (i = 0, sfep = xfs_dir2_sf_firstentry(sfp),
1251 oldsfep = xfs_dir2_sf_firstentry(oldsfp);
Christoph Hellwigac8ba502011-07-08 14:35:13 +02001252 i < sfp->count;
Dave Chinner32c54832013-10-29 22:11:46 +11001253 i++, sfep = dp->d_ops->sf_nextentry(sfp, sfep),
1254 oldsfep = dp->d_ops->sf_nextentry(oldsfp, oldsfep)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255 sfep->namelen = oldsfep->namelen;
1256 sfep->offset = oldsfep->offset;
1257 memcpy(sfep->name, oldsfep->name, sfep->namelen);
Dave Chinner1c55cec2013-08-12 20:50:10 +10001258 xfs_dir3_sfe_put_ino(mp, sfp, sfep,
1259 xfs_dir3_sfe_get_ino(mp, oldsfp, oldsfep));
1260 xfs_dir3_sfe_put_ftype(mp, sfp, sfep,
1261 xfs_dir3_sfe_get_ftype(mp, oldsfp, oldsfep));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 }
1263 /*
1264 * Clean up the inode.
1265 */
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10001266 kmem_free(buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 dp->i_d.di_size = newsize;
1268 xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE | XFS_ILOG_DDATA);
1269}
1270#endif /* XFS_BIG_INUMS */