blob: 5079e051ef0871817fbaa7a1014261337df9a44b [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 */
Christoph Hellwigd5cf09b2014-07-30 09:12:05 +100054
Linus Torvalds1da177e2005-04-16 15:20:36 -070055static void xfs_dir2_sf_toino4(xfs_da_args_t *args);
56static void xfs_dir2_sf_toino8(xfs_da_args_t *args);
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
Christoph Hellwig8bc38782011-07-08 14:35:03 +020058/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 * Given a block directory (dp/block), calculate its size as a shortform (sf)
60 * directory and a header for the sf directory, if it will fit it the
61 * space currently present in the inode. If it won't fit, the output
62 * size is too big (but not accurate).
63 */
64int /* size for sf form */
65xfs_dir2_block_sfsize(
66 xfs_inode_t *dp, /* incore inode pointer */
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +020067 xfs_dir2_data_hdr_t *hdr, /* block directory data */
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 xfs_dir2_sf_hdr_t *sfhp) /* output: header for sf form */
69{
70 xfs_dir2_dataptr_t addr; /* data entry address */
71 xfs_dir2_leaf_entry_t *blp; /* leaf area of the block */
72 xfs_dir2_block_tail_t *btp; /* tail area of the block */
73 int count; /* shortform entry count */
74 xfs_dir2_data_entry_t *dep; /* data entry in the block */
75 int i; /* block entry index */
76 int i8count; /* count of big-inode entries */
77 int isdot; /* entry is "." */
78 int isdotdot; /* entry is ".." */
79 xfs_mount_t *mp; /* mount structure pointer */
80 int namelen; /* total name bytes */
Christoph Hellwig5bde1ba92005-11-02 15:06:18 +110081 xfs_ino_t parent = 0; /* parent inode number */
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 int size=0; /* total computed size */
Dave Chinner0cb97762013-08-12 20:50:09 +100083 int has_ftype;
Dave Chinner8f661932014-06-06 15:15:59 +100084 struct xfs_da_geometry *geo;
Linus Torvalds1da177e2005-04-16 15:20:36 -070085
86 mp = dp->i_mount;
Dave Chinner8f661932014-06-06 15:15:59 +100087 geo = mp->m_dir_geo;
Linus Torvalds1da177e2005-04-16 15:20:36 -070088
Dave Chinner0cb97762013-08-12 20:50:09 +100089 /*
90 * if there is a filetype field, add the extra byte to the namelen
91 * for each entry that we see.
92 */
93 has_ftype = xfs_sb_version_hasftype(&mp->m_sb) ? 1 : 0;
94
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 count = i8count = namelen = 0;
Dave Chinner8f661932014-06-06 15:15:59 +100096 btp = xfs_dir2_block_tail_p(geo, hdr);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +100097 blp = xfs_dir2_block_leaf_p(btp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070098
99 /*
100 * Iterate over the block's data entries by using the leaf pointers.
101 */
Nathan Scotte922fff2006-03-17 17:27:56 +1100102 for (i = 0; i < be32_to_cpu(btp->count); i++) {
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100103 if ((addr = be32_to_cpu(blp[i].address)) == XFS_DIR2_NULL_DATAPTR)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 continue;
105 /*
106 * Calculate the pointer to the entry at hand.
107 */
Dave Chinner30028032014-06-06 15:08:18 +1000108 dep = (xfs_dir2_data_entry_t *)((char *)hdr +
Dave Chinner8f661932014-06-06 15:15:59 +1000109 xfs_dir2_dataptr_to_off(geo, addr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 /*
111 * Detect . and .., so we can special-case them.
112 * . is not included in sf directories.
113 * .. is included by just the parent inode number.
114 */
115 isdot = dep->namelen == 1 && dep->name[0] == '.';
116 isdotdot =
117 dep->namelen == 2 &&
118 dep->name[0] == '.' && dep->name[1] == '.';
Christoph Hellwigd5cf09b2014-07-30 09:12:05 +1000119
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 if (!isdot)
Christoph Hellwigff9901c2006-06-09 14:48:37 +1000121 i8count += be64_to_cpu(dep->inumber) > XFS_DIR2_MAX_SHORT_INUM;
Christoph Hellwigd5cf09b2014-07-30 09:12:05 +1000122
Dave Chinner0cb97762013-08-12 20:50:09 +1000123 /* take into account the file type field */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 if (!isdot && !isdotdot) {
125 count++;
Dave Chinner0cb97762013-08-12 20:50:09 +1000126 namelen += dep->namelen + has_ftype;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 } else if (isdotdot)
Christoph Hellwigff9901c2006-06-09 14:48:37 +1000128 parent = be64_to_cpu(dep->inumber);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 /*
130 * Calculate the new size, see if we should give up yet.
131 */
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000132 size = xfs_dir2_sf_hdr_size(i8count) + /* header */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 count + /* namelen */
134 count * (uint)sizeof(xfs_dir2_sf_off_t) + /* offset */
135 namelen + /* name */
136 (i8count ? /* inumber */
137 (uint)sizeof(xfs_dir2_ino8_t) * count :
138 (uint)sizeof(xfs_dir2_ino4_t) * count);
139 if (size > XFS_IFORK_DSIZE(dp))
140 return size; /* size value is a failure */
141 }
142 /*
143 * Create the output header, if it worked.
144 */
145 sfhp->count = count;
146 sfhp->i8count = i8count;
Dave Chinner47401752013-10-29 22:11:47 +1100147 dp->d_ops->sf_put_parent_ino(sfhp, parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 return size;
149}
150
151/*
152 * Convert a block format directory to shortform.
153 * Caller has already checked that it will fit, and built us a header.
154 */
155int /* error */
156xfs_dir2_block_to_sf(
157 xfs_da_args_t *args, /* operation arguments */
Dave Chinner1d9025e2012-06-22 18:50:14 +1000158 struct xfs_buf *bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 int size, /* shortform directory size */
160 xfs_dir2_sf_hdr_t *sfhp) /* shortform directory hdr */
161{
Christoph Hellwiga64b0412011-07-08 14:35:32 +0200162 xfs_dir2_data_hdr_t *hdr; /* block header */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 xfs_dir2_block_tail_t *btp; /* block tail pointer */
164 xfs_dir2_data_entry_t *dep; /* data entry pointer */
165 xfs_inode_t *dp; /* incore directory inode */
166 xfs_dir2_data_unused_t *dup; /* unused data pointer */
167 char *endptr; /* end of data entries */
168 int error; /* error return value */
169 int logflags; /* inode logging flags */
170 xfs_mount_t *mp; /* filesystem mount point */
171 char *ptr; /* current data pointer */
172 xfs_dir2_sf_entry_t *sfep; /* shortform entry */
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200173 xfs_dir2_sf_hdr_t *sfp; /* shortform directory header */
Dave Chinnerb3f03ba2013-12-03 23:50:57 +1100174 xfs_dir2_sf_hdr_t *dst; /* temporary data buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000176 trace_xfs_dir2_block_to_sf(args);
177
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 dp = args->dp;
179 mp = dp->i_mount;
180
181 /*
Dave Chinnerb3f03ba2013-12-03 23:50:57 +1100182 * allocate a temporary destination buffer the size of the inode
183 * to format the data into. Once we have formatted the data, we
184 * can free the block and copy the formatted data into the inode literal
185 * area.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 */
Dave Chinnerb3f03ba2013-12-03 23:50:57 +1100187 dst = kmem_alloc(mp->m_sb.sb_inodesize, KM_SLEEP);
188 hdr = bp->b_addr;
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +0200189
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 * Copy the header into the newly allocate local space.
192 */
Dave Chinnerb3f03ba2013-12-03 23:50:57 +1100193 sfp = (xfs_dir2_sf_hdr_t *)dst;
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000194 memcpy(sfp, sfhp, xfs_dir2_sf_hdr_size(sfhp->i8count));
Dave Chinnerb3f03ba2013-12-03 23:50:57 +1100195
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 /*
197 * Set up to loop over the block's entries.
198 */
Dave Chinner8f661932014-06-06 15:15:59 +1000199 btp = xfs_dir2_block_tail_p(args->geo, hdr);
Dave Chinner2ca98772013-10-29 22:11:49 +1100200 ptr = (char *)dp->d_ops->data_entry_p(hdr);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000201 endptr = (char *)xfs_dir2_block_leaf_p(btp);
202 sfep = xfs_dir2_sf_firstentry(sfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 /*
204 * Loop over the active and unused entries.
205 * Stop when we reach the leaf/tail portion of the block.
206 */
207 while (ptr < endptr) {
208 /*
209 * If it's unused, just skip over it.
210 */
211 dup = (xfs_dir2_data_unused_t *)ptr;
Nathan Scottad354eb2006-03-17 17:27:37 +1100212 if (be16_to_cpu(dup->freetag) == XFS_DIR2_DATA_FREE_TAG) {
213 ptr += be16_to_cpu(dup->length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 continue;
215 }
216 dep = (xfs_dir2_data_entry_t *)ptr;
217 /*
218 * Skip .
219 */
220 if (dep->namelen == 1 && dep->name[0] == '.')
Christoph Hellwigff9901c2006-06-09 14:48:37 +1000221 ASSERT(be64_to_cpu(dep->inumber) == dp->i_ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 /*
223 * Skip .., but make sure the inode number is right.
224 */
225 else if (dep->namelen == 2 &&
226 dep->name[0] == '.' && dep->name[1] == '.')
Christoph Hellwigff9901c2006-06-09 14:48:37 +1000227 ASSERT(be64_to_cpu(dep->inumber) ==
Dave Chinner47401752013-10-29 22:11:47 +1100228 dp->d_ops->sf_get_parent_ino(sfp));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 /*
230 * Normal entry, copy it into shortform.
231 */
232 else {
233 sfep->namelen = dep->namelen;
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000234 xfs_dir2_sf_put_offset(sfep,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 (xfs_dir2_data_aoff_t)
Christoph Hellwiga64b0412011-07-08 14:35:32 +0200236 ((char *)dep - (char *)hdr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 memcpy(sfep->name, dep->name, dep->namelen);
Dave Chinner47401752013-10-29 22:11:47 +1100238 dp->d_ops->sf_put_ino(sfp, sfep,
239 be64_to_cpu(dep->inumber));
240 dp->d_ops->sf_put_ftype(sfep,
Dave Chinner9d23fc82013-10-29 22:11:48 +1100241 dp->d_ops->data_get_ftype(dep));
Christoph Hellwig8bc38782011-07-08 14:35:03 +0200242
Dave Chinner32c54832013-10-29 22:11:46 +1100243 sfep = dp->d_ops->sf_nextentry(sfp, sfep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 }
Dave Chinner9d23fc82013-10-29 22:11:48 +1100245 ptr += dp->d_ops->data_entsize(dep->namelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 }
247 ASSERT((char *)sfep - (char *)sfp == size);
Dave Chinnerb3f03ba2013-12-03 23:50:57 +1100248
249 /* now we are done with the block, we can shrink the inode */
250 logflags = XFS_ILOG_CORE;
Dave Chinner7dda6e82014-06-06 15:11:18 +1000251 error = xfs_dir2_shrink_inode(args, args->geo->datablk, bp);
Dave Chinnerb3f03ba2013-12-03 23:50:57 +1100252 if (error) {
Dave Chinner24513372014-06-25 14:58:08 +1000253 ASSERT(error != -ENOSPC);
Dave Chinnerb3f03ba2013-12-03 23:50:57 +1100254 goto out;
255 }
256
257 /*
258 * The buffer is now unconditionally gone, whether
259 * xfs_dir2_shrink_inode worked or not.
260 *
261 * Convert the inode to local format and copy the data in.
262 */
263 dp->i_df.if_flags &= ~XFS_IFEXTENTS;
264 dp->i_df.if_flags |= XFS_IFINLINE;
265 dp->i_d.di_format = XFS_DINODE_FMT_LOCAL;
266 ASSERT(dp->i_df.if_bytes == 0);
267 xfs_idata_realloc(dp, size, XFS_DATA_FORK);
268
269 logflags |= XFS_ILOG_DDATA;
270 memcpy(dp->i_df.if_u1.if_data, dst, size);
271 dp->i_d.di_size = size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 xfs_dir2_sf_check(args);
273out:
274 xfs_trans_log_inode(args->trans, dp, logflags);
Dave Chinnerb3f03ba2013-12-03 23:50:57 +1100275 kmem_free(dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 return error;
277}
278
279/*
280 * Add a name to a shortform directory.
281 * There are two algorithms, "easy" and "hard" which we decide on
282 * before changing anything.
283 * Convert to block form if necessary, if the new entry won't fit.
284 */
285int /* error */
286xfs_dir2_sf_addname(
287 xfs_da_args_t *args) /* operation arguments */
288{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 xfs_inode_t *dp; /* incore directory inode */
290 int error; /* error return value */
291 int incr_isize; /* total change in size */
292 int new_isize; /* di_size after adding name */
293 int objchange; /* changing to 8-byte inodes */
Christoph Hellwig5bde1ba92005-11-02 15:06:18 +1100294 xfs_dir2_data_aoff_t offset = 0; /* offset for new entry */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 int pick; /* which algorithm to use */
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200296 xfs_dir2_sf_hdr_t *sfp; /* shortform structure */
Christoph Hellwig5bde1ba92005-11-02 15:06:18 +1100297 xfs_dir2_sf_entry_t *sfep = NULL; /* shortform entry */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000299 trace_xfs_dir2_sf_addname(args);
300
Dave Chinner24513372014-06-25 14:58:08 +1000301 ASSERT(xfs_dir2_sf_lookup(args) == -ENOENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 dp = args->dp;
303 ASSERT(dp->i_df.if_flags & XFS_IFINLINE);
304 /*
305 * Make sure the shortform value has some of its header.
306 */
307 if (dp->i_d.di_size < offsetof(xfs_dir2_sf_hdr_t, parent)) {
308 ASSERT(XFS_FORCED_SHUTDOWN(dp->i_mount));
Dave Chinner24513372014-06-25 14:58:08 +1000309 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 }
311 ASSERT(dp->i_df.if_bytes == dp->i_d.di_size);
312 ASSERT(dp->i_df.if_u1.if_data != NULL);
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200313 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
314 ASSERT(dp->i_d.di_size >= xfs_dir2_sf_hdr_size(sfp->i8count));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 /*
316 * Compute entry (and change in) size.
317 */
Eric Sandeen5e06d142014-04-14 19:07:23 +1000318 incr_isize = dp->d_ops->sf_entsize(sfp, args->namelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 objchange = 0;
Christoph Hellwigd5cf09b2014-07-30 09:12:05 +1000320
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 /*
322 * Do we have to change to 8 byte inodes?
323 */
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200324 if (args->inumber > XFS_DIR2_MAX_SHORT_INUM && sfp->i8count == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 /*
Eric Sandeen5e06d142014-04-14 19:07:23 +1000326 * Yes, adjust the inode size. old count + (parent + new)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 incr_isize +=
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200329 (sfp->count + 2) *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 ((uint)sizeof(xfs_dir2_ino8_t) -
331 (uint)sizeof(xfs_dir2_ino4_t));
332 objchange = 1;
333 }
Christoph Hellwigd5cf09b2014-07-30 09:12:05 +1000334
Eric Sandeen5e06d142014-04-14 19:07:23 +1000335 new_isize = (int)dp->i_d.di_size + incr_isize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 /*
337 * Won't fit as shortform any more (due to size),
338 * or the pick routine says it won't (due to offset values).
339 */
340 if (new_isize > XFS_IFORK_DSIZE(dp) ||
341 (pick =
342 xfs_dir2_sf_addname_pick(args, objchange, &sfep, &offset)) == 0) {
343 /*
344 * Just checking or no space reservation, it doesn't fit.
345 */
Barry Naujok6a178102008-05-21 16:42:05 +1000346 if ((args->op_flags & XFS_DA_OP_JUSTCHECK) || args->total == 0)
Dave Chinner24513372014-06-25 14:58:08 +1000347 return -ENOSPC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 /*
349 * Convert to block form then add the name.
350 */
351 error = xfs_dir2_sf_to_block(args);
352 if (error)
353 return error;
354 return xfs_dir2_block_addname(args);
355 }
356 /*
357 * Just checking, it fits.
358 */
Barry Naujok6a178102008-05-21 16:42:05 +1000359 if (args->op_flags & XFS_DA_OP_JUSTCHECK)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 return 0;
361 /*
362 * Do it the easy way - just add it at the end.
363 */
364 if (pick == 1)
365 xfs_dir2_sf_addname_easy(args, sfep, offset, new_isize);
366 /*
367 * Do it the hard way - look for a place to insert the new entry.
368 * Convert to 8 byte inode numbers first if necessary.
369 */
370 else {
371 ASSERT(pick == 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 if (objchange)
373 xfs_dir2_sf_toino8(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 xfs_dir2_sf_addname_hard(args, objchange, new_isize);
375 }
376 xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE | XFS_ILOG_DDATA);
377 return 0;
378}
379
380/*
381 * Add the new entry the "easy" way.
382 * This is copying the old directory and adding the new entry at the end.
383 * Since it's sorted by "offset" we need room after the last offset
384 * that's already there, and then room to convert to a block directory.
385 * This is already checked by the pick routine.
386 */
387static void
388xfs_dir2_sf_addname_easy(
389 xfs_da_args_t *args, /* operation arguments */
390 xfs_dir2_sf_entry_t *sfep, /* pointer to new entry */
391 xfs_dir2_data_aoff_t offset, /* offset to use for new ent */
392 int new_isize) /* new directory size */
393{
394 int byteoff; /* byte offset in sf dir */
395 xfs_inode_t *dp; /* incore directory inode */
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200396 xfs_dir2_sf_hdr_t *sfp; /* shortform structure */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397
398 dp = args->dp;
399
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200400 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 byteoff = (int)((char *)sfep - (char *)sfp);
402 /*
403 * Grow the in-inode space.
404 */
Dave Chinner32c54832013-10-29 22:11:46 +1100405 xfs_idata_realloc(dp, dp->d_ops->sf_entsize(sfp, args->namelen),
Dave Chinner0cb97762013-08-12 20:50:09 +1000406 XFS_DATA_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 /*
408 * Need to set up again due to realloc of the inode data.
409 */
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200410 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 sfep = (xfs_dir2_sf_entry_t *)((char *)sfp + byteoff);
412 /*
413 * Fill in the new entry.
414 */
415 sfep->namelen = args->namelen;
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000416 xfs_dir2_sf_put_offset(sfep, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 memcpy(sfep->name, args->name, sfep->namelen);
Dave Chinner47401752013-10-29 22:11:47 +1100418 dp->d_ops->sf_put_ino(sfp, sfep, args->inumber);
419 dp->d_ops->sf_put_ftype(sfep, args->filetype);
Dave Chinner1c55cec2013-08-12 20:50:10 +1000420
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 /*
422 * Update the header and inode.
423 */
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200424 sfp->count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 if (args->inumber > XFS_DIR2_MAX_SHORT_INUM)
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200426 sfp->i8count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 dp->i_d.di_size = new_isize;
428 xfs_dir2_sf_check(args);
429}
430
431/*
432 * Add the new entry the "hard" way.
433 * The caller has already converted to 8 byte inode numbers if necessary,
434 * in which case we need to leave the i8count at 1.
435 * Find a hole that the new entry will fit into, and copy
436 * the first part of the entries, the new entry, and the last part of
437 * the entries.
438 */
439/* ARGSUSED */
440static void
441xfs_dir2_sf_addname_hard(
442 xfs_da_args_t *args, /* operation arguments */
443 int objchange, /* changing inode number size */
444 int new_isize) /* new directory size */
445{
446 int add_datasize; /* data size need for new ent */
447 char *buf; /* buffer for old */
448 xfs_inode_t *dp; /* incore directory inode */
449 int eof; /* reached end of old dir */
450 int nbytes; /* temp for byte copies */
451 xfs_dir2_data_aoff_t new_offset; /* next offset value */
452 xfs_dir2_data_aoff_t offset; /* current offset value */
453 int old_isize; /* previous di_size */
454 xfs_dir2_sf_entry_t *oldsfep; /* entry in original dir */
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200455 xfs_dir2_sf_hdr_t *oldsfp; /* original shortform dir */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 xfs_dir2_sf_entry_t *sfep; /* entry in new dir */
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200457 xfs_dir2_sf_hdr_t *sfp; /* new shortform dir */
Dave Chinner0cb97762013-08-12 20:50:09 +1000458 struct xfs_mount *mp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459
460 /*
461 * Copy the old directory to the stack buffer.
462 */
463 dp = args->dp;
Dave Chinner0cb97762013-08-12 20:50:09 +1000464 mp = dp->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200466 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 old_isize = (int)dp->i_d.di_size;
468 buf = kmem_alloc(old_isize, KM_SLEEP);
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200469 oldsfp = (xfs_dir2_sf_hdr_t *)buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 memcpy(oldsfp, sfp, old_isize);
471 /*
472 * Loop over the old directory finding the place we're going
473 * to insert the new entry.
474 * If it's going to end up at the end then oldsfep will point there.
475 */
Dave Chinner1c9a5b22013-10-30 09:15:02 +1100476 for (offset = dp->d_ops->data_first_offset,
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000477 oldsfep = xfs_dir2_sf_firstentry(oldsfp),
Dave Chinner9d23fc82013-10-29 22:11:48 +1100478 add_datasize = dp->d_ops->data_entsize(args->namelen),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 eof = (char *)oldsfep == &buf[old_isize];
480 !eof;
Dave Chinner9d23fc82013-10-29 22:11:48 +1100481 offset = new_offset + dp->d_ops->data_entsize(oldsfep->namelen),
Dave Chinner32c54832013-10-29 22:11:46 +1100482 oldsfep = dp->d_ops->sf_nextentry(oldsfp, oldsfep),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 eof = (char *)oldsfep == &buf[old_isize]) {
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000484 new_offset = xfs_dir2_sf_get_offset(oldsfep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 if (offset + add_datasize <= new_offset)
486 break;
487 }
488 /*
489 * Get rid of the old directory, then allocate space for
490 * the new one. We do this so xfs_idata_realloc won't copy
491 * the data.
492 */
493 xfs_idata_realloc(dp, -old_isize, XFS_DATA_FORK);
494 xfs_idata_realloc(dp, new_isize, XFS_DATA_FORK);
495 /*
496 * Reset the pointer since the buffer was reallocated.
497 */
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200498 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 /*
500 * Copy the first part of the directory, including the header.
501 */
502 nbytes = (int)((char *)oldsfep - (char *)oldsfp);
503 memcpy(sfp, oldsfp, nbytes);
504 sfep = (xfs_dir2_sf_entry_t *)((char *)sfp + nbytes);
505 /*
506 * Fill in the new entry, and update the header counts.
507 */
508 sfep->namelen = args->namelen;
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000509 xfs_dir2_sf_put_offset(sfep, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 memcpy(sfep->name, args->name, sfep->namelen);
Dave Chinner47401752013-10-29 22:11:47 +1100511 dp->d_ops->sf_put_ino(sfp, sfep, args->inumber);
512 dp->d_ops->sf_put_ftype(sfep, args->filetype);
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200513 sfp->count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 if (args->inumber > XFS_DIR2_MAX_SHORT_INUM && !objchange)
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200515 sfp->i8count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 /*
517 * If there's more left to copy, do that.
518 */
519 if (!eof) {
Dave Chinner32c54832013-10-29 22:11:46 +1100520 sfep = dp->d_ops->sf_nextentry(sfp, sfep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 memcpy(sfep, oldsfep, old_isize - nbytes);
522 }
Denys Vlasenkof0e2d932008-05-19 16:31:57 +1000523 kmem_free(buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 dp->i_d.di_size = new_isize;
525 xfs_dir2_sf_check(args);
526}
527
528/*
529 * Decide if the new entry will fit at all.
530 * If it will fit, pick between adding the new entry to the end (easy)
531 * or somewhere else (hard).
532 * Return 0 (won't fit), 1 (easy), 2 (hard).
533 */
534/*ARGSUSED*/
535static int /* pick result */
536xfs_dir2_sf_addname_pick(
537 xfs_da_args_t *args, /* operation arguments */
538 int objchange, /* inode # size changes */
539 xfs_dir2_sf_entry_t **sfepp, /* out(1): new entry ptr */
540 xfs_dir2_data_aoff_t *offsetp) /* out(1): new offset */
541{
542 xfs_inode_t *dp; /* incore directory inode */
543 int holefit; /* found hole it will fit in */
544 int i; /* entry number */
545 xfs_mount_t *mp; /* filesystem mount point */
546 xfs_dir2_data_aoff_t offset; /* data block offset */
547 xfs_dir2_sf_entry_t *sfep; /* shortform entry */
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200548 xfs_dir2_sf_hdr_t *sfp; /* shortform structure */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 int size; /* entry's data size */
550 int used; /* data bytes used */
551
552 dp = args->dp;
553 mp = dp->i_mount;
554
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200555 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
Dave Chinner9d23fc82013-10-29 22:11:48 +1100556 size = dp->d_ops->data_entsize(args->namelen);
Dave Chinner1c9a5b22013-10-30 09:15:02 +1100557 offset = dp->d_ops->data_first_offset;
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000558 sfep = xfs_dir2_sf_firstentry(sfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 holefit = 0;
560 /*
561 * Loop over sf entries.
562 * Keep track of data offset and whether we've seen a place
563 * to insert the new entry.
564 */
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200565 for (i = 0; i < sfp->count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 if (!holefit)
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000567 holefit = offset + size <= xfs_dir2_sf_get_offset(sfep);
568 offset = xfs_dir2_sf_get_offset(sfep) +
Dave Chinner9d23fc82013-10-29 22:11:48 +1100569 dp->d_ops->data_entsize(sfep->namelen);
Dave Chinner32c54832013-10-29 22:11:46 +1100570 sfep = dp->d_ops->sf_nextentry(sfp, sfep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 }
572 /*
573 * Calculate data bytes used excluding the new entry, if this
574 * was a data block (block form directory).
575 */
576 used = offset +
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200577 (sfp->count + 3) * (uint)sizeof(xfs_dir2_leaf_entry_t) +
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 (uint)sizeof(xfs_dir2_block_tail_t);
579 /*
580 * If it won't fit in a block form then we can't insert it,
581 * we'll go back, convert to block, then try the insert and convert
582 * to leaf.
583 */
Dave Chinner8f661932014-06-06 15:15:59 +1000584 if (used + (holefit ? 0 : size) > args->geo->blksize)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 return 0;
586 /*
587 * If changing the inode number size, do it the hard way.
588 */
Christoph Hellwigd5cf09b2014-07-30 09:12:05 +1000589 if (objchange)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 return 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 /*
592 * If it won't fit at the end then do it the hard way (use the hole).
593 */
Dave Chinner8f661932014-06-06 15:15:59 +1000594 if (used + size > args->geo->blksize)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 return 2;
596 /*
597 * Do it the easy way.
598 */
599 *sfepp = sfep;
600 *offsetp = offset;
601 return 1;
602}
603
604#ifdef DEBUG
605/*
606 * Check consistency of shortform directory, assert if bad.
607 */
608static void
609xfs_dir2_sf_check(
610 xfs_da_args_t *args) /* operation arguments */
611{
612 xfs_inode_t *dp; /* incore directory inode */
613 int i; /* entry number */
614 int i8count; /* number of big inode#s */
615 xfs_ino_t ino; /* entry inode number */
616 int offset; /* data offset */
617 xfs_dir2_sf_entry_t *sfep; /* shortform dir entry */
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200618 xfs_dir2_sf_hdr_t *sfp; /* shortform structure */
Dave Chinner0cb97762013-08-12 20:50:09 +1000619 struct xfs_mount *mp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620
621 dp = args->dp;
Dave Chinner0cb97762013-08-12 20:50:09 +1000622 mp = dp->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200624 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
Dave Chinner1c9a5b22013-10-30 09:15:02 +1100625 offset = dp->d_ops->data_first_offset;
Dave Chinner47401752013-10-29 22:11:47 +1100626 ino = dp->d_ops->sf_get_parent_ino(sfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 i8count = ino > XFS_DIR2_MAX_SHORT_INUM;
628
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000629 for (i = 0, sfep = xfs_dir2_sf_firstentry(sfp);
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200630 i < sfp->count;
Dave Chinner32c54832013-10-29 22:11:46 +1100631 i++, sfep = dp->d_ops->sf_nextentry(sfp, sfep)) {
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000632 ASSERT(xfs_dir2_sf_get_offset(sfep) >= offset);
Dave Chinner47401752013-10-29 22:11:47 +1100633 ino = dp->d_ops->sf_get_ino(sfp, sfep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 i8count += ino > XFS_DIR2_MAX_SHORT_INUM;
635 offset =
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000636 xfs_dir2_sf_get_offset(sfep) +
Dave Chinner9d23fc82013-10-29 22:11:48 +1100637 dp->d_ops->data_entsize(sfep->namelen);
Dave Chinner47401752013-10-29 22:11:47 +1100638 ASSERT(dp->d_ops->sf_get_ftype(sfep) < XFS_DIR3_FT_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 }
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200640 ASSERT(i8count == sfp->i8count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 ASSERT((char *)sfep - (char *)sfp == dp->i_d.di_size);
642 ASSERT(offset +
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200643 (sfp->count + 2) * (uint)sizeof(xfs_dir2_leaf_entry_t) +
Dave Chinner8f661932014-06-06 15:15:59 +1000644 (uint)sizeof(xfs_dir2_block_tail_t) <= args->geo->blksize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645}
646#endif /* DEBUG */
647
648/*
649 * Create a new (shortform) directory.
650 */
651int /* error, always 0 */
652xfs_dir2_sf_create(
653 xfs_da_args_t *args, /* operation arguments */
654 xfs_ino_t pino) /* parent inode number */
655{
656 xfs_inode_t *dp; /* incore directory inode */
657 int i8count; /* parent inode is an 8-byte number */
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200658 xfs_dir2_sf_hdr_t *sfp; /* shortform structure */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 int size; /* directory size */
660
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000661 trace_xfs_dir2_sf_create(args);
662
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 dp = args->dp;
664
665 ASSERT(dp != NULL);
666 ASSERT(dp->i_d.di_size == 0);
667 /*
668 * If it's currently a zero-length extent file,
669 * convert it to local format.
670 */
671 if (dp->i_d.di_format == XFS_DINODE_FMT_EXTENTS) {
672 dp->i_df.if_flags &= ~XFS_IFEXTENTS; /* just in case */
673 dp->i_d.di_format = XFS_DINODE_FMT_LOCAL;
674 xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE);
675 dp->i_df.if_flags |= XFS_IFINLINE;
676 }
677 ASSERT(dp->i_df.if_flags & XFS_IFINLINE);
678 ASSERT(dp->i_df.if_bytes == 0);
679 i8count = pino > XFS_DIR2_MAX_SHORT_INUM;
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000680 size = xfs_dir2_sf_hdr_size(i8count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 /*
682 * Make a buffer for the data.
683 */
684 xfs_idata_realloc(dp, size, XFS_DATA_FORK);
685 /*
686 * Fill in the header,
687 */
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200688 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
689 sfp->i8count = i8count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 /*
691 * Now can put in the inode number, since i8count is set.
692 */
Dave Chinner47401752013-10-29 22:11:47 +1100693 dp->d_ops->sf_put_parent_ino(sfp, pino);
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200694 sfp->count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 dp->i_d.di_size = size;
696 xfs_dir2_sf_check(args);
697 xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE | XFS_ILOG_DDATA);
698 return 0;
699}
700
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701/*
702 * Lookup an entry in a shortform directory.
703 * Returns EEXIST if found, ENOENT if not found.
704 */
705int /* error */
706xfs_dir2_sf_lookup(
707 xfs_da_args_t *args) /* operation arguments */
708{
709 xfs_inode_t *dp; /* incore directory inode */
710 int i; /* entry index */
Barry Naujok384f3ce2008-05-21 16:58:22 +1000711 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 xfs_dir2_sf_entry_t *sfep; /* shortform directory entry */
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200713 xfs_dir2_sf_hdr_t *sfp; /* shortform structure */
Barry Naujok5163f952008-05-21 16:41:01 +1000714 enum xfs_dacmp cmp; /* comparison result */
Barry Naujok384f3ce2008-05-21 16:58:22 +1000715 xfs_dir2_sf_entry_t *ci_sfep; /* case-insens. entry */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000717 trace_xfs_dir2_sf_lookup(args);
718
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 xfs_dir2_sf_check(args);
720 dp = args->dp;
721
722 ASSERT(dp->i_df.if_flags & XFS_IFINLINE);
723 /*
724 * Bail out if the directory is way too short.
725 */
726 if (dp->i_d.di_size < offsetof(xfs_dir2_sf_hdr_t, parent)) {
727 ASSERT(XFS_FORCED_SHUTDOWN(dp->i_mount));
Dave Chinner24513372014-06-25 14:58:08 +1000728 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 }
730 ASSERT(dp->i_df.if_bytes == dp->i_d.di_size);
731 ASSERT(dp->i_df.if_u1.if_data != NULL);
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200732 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
733 ASSERT(dp->i_d.di_size >= xfs_dir2_sf_hdr_size(sfp->i8count));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 /*
735 * Special case for .
736 */
737 if (args->namelen == 1 && args->name[0] == '.') {
738 args->inumber = dp->i_ino;
Barry Naujok5163f952008-05-21 16:41:01 +1000739 args->cmpresult = XFS_CMP_EXACT;
Dave Chinner1c55cec2013-08-12 20:50:10 +1000740 args->filetype = XFS_DIR3_FT_DIR;
Dave Chinner24513372014-06-25 14:58:08 +1000741 return -EEXIST;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 }
743 /*
744 * Special case for ..
745 */
746 if (args->namelen == 2 &&
747 args->name[0] == '.' && args->name[1] == '.') {
Dave Chinner47401752013-10-29 22:11:47 +1100748 args->inumber = dp->d_ops->sf_get_parent_ino(sfp);
Barry Naujok5163f952008-05-21 16:41:01 +1000749 args->cmpresult = XFS_CMP_EXACT;
Dave Chinner1c55cec2013-08-12 20:50:10 +1000750 args->filetype = XFS_DIR3_FT_DIR;
Dave Chinner24513372014-06-25 14:58:08 +1000751 return -EEXIST;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 }
753 /*
754 * Loop over all the entries trying to match ours.
755 */
Barry Naujok384f3ce2008-05-21 16:58:22 +1000756 ci_sfep = NULL;
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200757 for (i = 0, sfep = xfs_dir2_sf_firstentry(sfp); i < sfp->count;
Dave Chinner32c54832013-10-29 22:11:46 +1100758 i++, sfep = dp->d_ops->sf_nextentry(sfp, sfep)) {
Barry Naujok5163f952008-05-21 16:41:01 +1000759 /*
760 * Compare name and if it's an exact match, return the inode
761 * number. If it's the first case-insensitive match, store the
762 * inode number and continue looking for an exact match.
763 */
764 cmp = dp->i_mount->m_dirnameops->compname(args, sfep->name,
765 sfep->namelen);
766 if (cmp != XFS_CMP_DIFFERENT && cmp != args->cmpresult) {
767 args->cmpresult = cmp;
Dave Chinner47401752013-10-29 22:11:47 +1100768 args->inumber = dp->d_ops->sf_get_ino(sfp, sfep);
769 args->filetype = dp->d_ops->sf_get_ftype(sfep);
Barry Naujok5163f952008-05-21 16:41:01 +1000770 if (cmp == XFS_CMP_EXACT)
Dave Chinner24513372014-06-25 14:58:08 +1000771 return -EEXIST;
Barry Naujok384f3ce2008-05-21 16:58:22 +1000772 ci_sfep = sfep;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 }
774 }
Barry Naujok6a178102008-05-21 16:42:05 +1000775 ASSERT(args->op_flags & XFS_DA_OP_OKNOENT);
Barry Naujok5163f952008-05-21 16:41:01 +1000776 /*
777 * Here, we can only be doing a lookup (not a rename or replace).
Dave Chinner24513372014-06-25 14:58:08 +1000778 * If a case-insensitive match was not found, return -ENOENT.
Barry Naujok5163f952008-05-21 16:41:01 +1000779 */
Barry Naujok384f3ce2008-05-21 16:58:22 +1000780 if (!ci_sfep)
Dave Chinner24513372014-06-25 14:58:08 +1000781 return -ENOENT;
Barry Naujok384f3ce2008-05-21 16:58:22 +1000782 /* otherwise process the CI match as required by the caller */
783 error = xfs_dir_cilookup_result(args, ci_sfep->name, ci_sfep->namelen);
Eric Sandeenb474c7a2014-06-22 15:04:54 +1000784 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785}
786
787/*
788 * Remove an entry from a shortform directory.
789 */
790int /* error */
791xfs_dir2_sf_removename(
792 xfs_da_args_t *args)
793{
794 int byteoff; /* offset of removed entry */
795 xfs_inode_t *dp; /* incore directory inode */
796 int entsize; /* this entry's size */
797 int i; /* shortform entry index */
798 int newsize; /* new inode size */
799 int oldsize; /* old inode size */
800 xfs_dir2_sf_entry_t *sfep; /* shortform directory entry */
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200801 xfs_dir2_sf_hdr_t *sfp; /* shortform structure */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000803 trace_xfs_dir2_sf_removename(args);
804
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 dp = args->dp;
806
807 ASSERT(dp->i_df.if_flags & XFS_IFINLINE);
808 oldsize = (int)dp->i_d.di_size;
809 /*
810 * Bail out if the directory is way too short.
811 */
812 if (oldsize < offsetof(xfs_dir2_sf_hdr_t, parent)) {
813 ASSERT(XFS_FORCED_SHUTDOWN(dp->i_mount));
Dave Chinner24513372014-06-25 14:58:08 +1000814 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 }
816 ASSERT(dp->i_df.if_bytes == oldsize);
817 ASSERT(dp->i_df.if_u1.if_data != NULL);
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200818 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
819 ASSERT(oldsize >= xfs_dir2_sf_hdr_size(sfp->i8count));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 /*
821 * Loop over the old directory entries.
822 * Find the one we're deleting.
823 */
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200824 for (i = 0, sfep = xfs_dir2_sf_firstentry(sfp); i < sfp->count;
Dave Chinner32c54832013-10-29 22:11:46 +1100825 i++, sfep = dp->d_ops->sf_nextentry(sfp, sfep)) {
Barry Naujok5163f952008-05-21 16:41:01 +1000826 if (xfs_da_compname(args, sfep->name, sfep->namelen) ==
827 XFS_CMP_EXACT) {
Dave Chinner47401752013-10-29 22:11:47 +1100828 ASSERT(dp->d_ops->sf_get_ino(sfp, sfep) ==
Christoph Hellwig8bc38782011-07-08 14:35:03 +0200829 args->inumber);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 break;
831 }
832 }
833 /*
834 * Didn't find it.
835 */
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200836 if (i == sfp->count)
Dave Chinner24513372014-06-25 14:58:08 +1000837 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 /*
839 * Calculate sizes.
840 */
841 byteoff = (int)((char *)sfep - (char *)sfp);
Dave Chinner32c54832013-10-29 22:11:46 +1100842 entsize = dp->d_ops->sf_entsize(sfp, args->namelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 newsize = oldsize - entsize;
844 /*
845 * Copy the part if any after the removed entry, sliding it down.
846 */
847 if (byteoff + entsize < oldsize)
848 memmove((char *)sfp + byteoff, (char *)sfp + byteoff + entsize,
849 oldsize - (byteoff + entsize));
850 /*
851 * Fix up the header and file size.
852 */
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200853 sfp->count--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 dp->i_d.di_size = newsize;
855 /*
856 * Reallocate, making it smaller.
857 */
858 xfs_idata_realloc(dp, newsize - oldsize, XFS_DATA_FORK);
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200859 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 /*
861 * Are we changing inode number size?
862 */
863 if (args->inumber > XFS_DIR2_MAX_SHORT_INUM) {
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200864 if (sfp->i8count == 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 xfs_dir2_sf_toino4(args);
866 else
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200867 sfp->i8count--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 xfs_dir2_sf_check(args);
870 xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE | XFS_ILOG_DDATA);
871 return 0;
872}
873
874/*
875 * Replace the inode number of an entry in a shortform directory.
876 */
877int /* error */
878xfs_dir2_sf_replace(
879 xfs_da_args_t *args) /* operation arguments */
880{
881 xfs_inode_t *dp; /* incore directory inode */
882 int i; /* entry index */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 xfs_ino_t ino=0; /* entry old inode number */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 int i8elevated; /* sf_toino8 set i8count=1 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 xfs_dir2_sf_entry_t *sfep; /* shortform directory entry */
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200886 xfs_dir2_sf_hdr_t *sfp; /* shortform structure */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000888 trace_xfs_dir2_sf_replace(args);
889
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 dp = args->dp;
891
892 ASSERT(dp->i_df.if_flags & XFS_IFINLINE);
893 /*
894 * Bail out if the shortform directory is way too small.
895 */
896 if (dp->i_d.di_size < offsetof(xfs_dir2_sf_hdr_t, parent)) {
897 ASSERT(XFS_FORCED_SHUTDOWN(dp->i_mount));
Dave Chinner24513372014-06-25 14:58:08 +1000898 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 }
900 ASSERT(dp->i_df.if_bytes == dp->i_d.di_size);
901 ASSERT(dp->i_df.if_u1.if_data != NULL);
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200902 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
903 ASSERT(dp->i_d.di_size >= xfs_dir2_sf_hdr_size(sfp->i8count));
Christoph Hellwigd5cf09b2014-07-30 09:12:05 +1000904
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 /*
906 * New inode number is large, and need to convert to 8-byte inodes.
907 */
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200908 if (args->inumber > XFS_DIR2_MAX_SHORT_INUM && sfp->i8count == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 int error; /* error return value */
910 int newsize; /* new inode size */
911
912 newsize =
913 dp->i_df.if_bytes +
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200914 (sfp->count + 1) *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 ((uint)sizeof(xfs_dir2_ino8_t) -
916 (uint)sizeof(xfs_dir2_ino4_t));
917 /*
918 * Won't fit as shortform, convert to block then do replace.
919 */
920 if (newsize > XFS_IFORK_DSIZE(dp)) {
921 error = xfs_dir2_sf_to_block(args);
922 if (error) {
923 return error;
924 }
925 return xfs_dir2_block_replace(args);
926 }
927 /*
928 * Still fits, convert to 8-byte now.
929 */
930 xfs_dir2_sf_toino8(args);
931 i8elevated = 1;
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200932 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 } else
934 i8elevated = 0;
Christoph Hellwigd5cf09b2014-07-30 09:12:05 +1000935
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 ASSERT(args->namelen != 1 || args->name[0] != '.');
937 /*
938 * Replace ..'s entry.
939 */
940 if (args->namelen == 2 &&
941 args->name[0] == '.' && args->name[1] == '.') {
Dave Chinner47401752013-10-29 22:11:47 +1100942 ino = dp->d_ops->sf_get_parent_ino(sfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 ASSERT(args->inumber != ino);
Dave Chinner47401752013-10-29 22:11:47 +1100944 dp->d_ops->sf_put_parent_ino(sfp, args->inumber);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 }
946 /*
947 * Normal entry, look for the name.
948 */
949 else {
Dave Chinner0cb97762013-08-12 20:50:09 +1000950 for (i = 0, sfep = xfs_dir2_sf_firstentry(sfp); i < sfp->count;
Dave Chinner32c54832013-10-29 22:11:46 +1100951 i++, sfep = dp->d_ops->sf_nextentry(sfp, sfep)) {
Barry Naujok5163f952008-05-21 16:41:01 +1000952 if (xfs_da_compname(args, sfep->name, sfep->namelen) ==
953 XFS_CMP_EXACT) {
Dave Chinner47401752013-10-29 22:11:47 +1100954 ino = dp->d_ops->sf_get_ino(sfp, sfep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 ASSERT(args->inumber != ino);
Dave Chinner47401752013-10-29 22:11:47 +1100956 dp->d_ops->sf_put_ino(sfp, sfep, args->inumber);
957 dp->d_ops->sf_put_ftype(sfep, args->filetype);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 break;
959 }
960 }
961 /*
962 * Didn't find it.
963 */
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200964 if (i == sfp->count) {
Barry Naujok6a178102008-05-21 16:42:05 +1000965 ASSERT(args->op_flags & XFS_DA_OP_OKNOENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 if (i8elevated)
967 xfs_dir2_sf_toino4(args);
Dave Chinner24513372014-06-25 14:58:08 +1000968 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 }
970 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 /*
972 * See if the old number was large, the new number is small.
973 */
974 if (ino > XFS_DIR2_MAX_SHORT_INUM &&
975 args->inumber <= XFS_DIR2_MAX_SHORT_INUM) {
976 /*
977 * And the old count was one, so need to convert to small.
978 */
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200979 if (sfp->i8count == 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 xfs_dir2_sf_toino4(args);
981 else
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200982 sfp->i8count--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 }
984 /*
985 * See if the old number was small, the new number is large.
986 */
987 if (ino <= XFS_DIR2_MAX_SHORT_INUM &&
988 args->inumber > XFS_DIR2_MAX_SHORT_INUM) {
989 /*
990 * add to the i8count unless we just converted to 8-byte
991 * inodes (which does an implied i8count = 1)
992 */
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200993 ASSERT(sfp->i8count != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 if (!i8elevated)
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200995 sfp->i8count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 xfs_dir2_sf_check(args);
998 xfs_trans_log_inode(args->trans, dp, XFS_ILOG_DDATA);
999 return 0;
1000}
1001
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002/*
1003 * Convert from 8-byte inode numbers to 4-byte inode numbers.
1004 * The last 8-byte inode number is gone, but the count is still 1.
1005 */
1006static void
1007xfs_dir2_sf_toino4(
1008 xfs_da_args_t *args) /* operation arguments */
1009{
1010 char *buf; /* old dir's buffer */
1011 xfs_inode_t *dp; /* incore directory inode */
1012 int i; /* entry index */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 int newsize; /* new inode size */
1014 xfs_dir2_sf_entry_t *oldsfep; /* old sf entry */
Christoph Hellwigac8ba502011-07-08 14:35:13 +02001015 xfs_dir2_sf_hdr_t *oldsfp; /* old sf directory */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 int oldsize; /* old inode size */
1017 xfs_dir2_sf_entry_t *sfep; /* new sf entry */
Christoph Hellwigac8ba502011-07-08 14:35:13 +02001018 xfs_dir2_sf_hdr_t *sfp; /* new sf directory */
Dave Chinner1c55cec2013-08-12 20:50:10 +10001019 struct xfs_mount *mp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001021 trace_xfs_dir2_sf_toino4(args);
1022
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 dp = args->dp;
Dave Chinner1c55cec2013-08-12 20:50:10 +10001024 mp = dp->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025
1026 /*
1027 * Copy the old directory to the buffer.
1028 * Then nuke it from the inode, and add the new buffer to the inode.
1029 * Don't want xfs_idata_realloc copying the data here.
1030 */
1031 oldsize = dp->i_df.if_bytes;
1032 buf = kmem_alloc(oldsize, KM_SLEEP);
Christoph Hellwigac8ba502011-07-08 14:35:13 +02001033 oldsfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
1034 ASSERT(oldsfp->i8count == 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 memcpy(buf, oldsfp, oldsize);
1036 /*
1037 * Compute the new inode size.
1038 */
1039 newsize =
1040 oldsize -
Christoph Hellwigac8ba502011-07-08 14:35:13 +02001041 (oldsfp->count + 1) *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 ((uint)sizeof(xfs_dir2_ino8_t) - (uint)sizeof(xfs_dir2_ino4_t));
1043 xfs_idata_realloc(dp, -oldsize, XFS_DATA_FORK);
1044 xfs_idata_realloc(dp, newsize, XFS_DATA_FORK);
1045 /*
1046 * Reset our pointers, the data has moved.
1047 */
Christoph Hellwigac8ba502011-07-08 14:35:13 +02001048 oldsfp = (xfs_dir2_sf_hdr_t *)buf;
1049 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 /*
1051 * Fill in the new header.
1052 */
Christoph Hellwigac8ba502011-07-08 14:35:13 +02001053 sfp->count = oldsfp->count;
1054 sfp->i8count = 0;
Dave Chinner47401752013-10-29 22:11:47 +11001055 dp->d_ops->sf_put_parent_ino(sfp, dp->d_ops->sf_get_parent_ino(oldsfp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 /*
1057 * Copy the entries field by field.
1058 */
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001059 for (i = 0, sfep = xfs_dir2_sf_firstentry(sfp),
1060 oldsfep = xfs_dir2_sf_firstentry(oldsfp);
Christoph Hellwigac8ba502011-07-08 14:35:13 +02001061 i < sfp->count;
Dave Chinner32c54832013-10-29 22:11:46 +11001062 i++, sfep = dp->d_ops->sf_nextentry(sfp, sfep),
1063 oldsfep = dp->d_ops->sf_nextentry(oldsfp, oldsfep)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 sfep->namelen = oldsfep->namelen;
1065 sfep->offset = oldsfep->offset;
1066 memcpy(sfep->name, oldsfep->name, sfep->namelen);
Dave Chinner47401752013-10-29 22:11:47 +11001067 dp->d_ops->sf_put_ino(sfp, sfep,
1068 dp->d_ops->sf_get_ino(oldsfp, oldsfep));
1069 dp->d_ops->sf_put_ftype(sfep, dp->d_ops->sf_get_ftype(oldsfep));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 }
1071 /*
1072 * Clean up the inode.
1073 */
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10001074 kmem_free(buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 dp->i_d.di_size = newsize;
1076 xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE | XFS_ILOG_DDATA);
1077}
1078
1079/*
Eric Sandeen5e06d142014-04-14 19:07:23 +10001080 * Convert existing entries from 4-byte inode numbers to 8-byte inode numbers.
1081 * The new entry w/ an 8-byte inode number is not there yet; we leave with
1082 * i8count set to 1, but no corresponding 8-byte entry.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 */
1084static void
1085xfs_dir2_sf_toino8(
1086 xfs_da_args_t *args) /* operation arguments */
1087{
1088 char *buf; /* old dir's buffer */
1089 xfs_inode_t *dp; /* incore directory inode */
1090 int i; /* entry index */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 int newsize; /* new inode size */
1092 xfs_dir2_sf_entry_t *oldsfep; /* old sf entry */
Christoph Hellwigac8ba502011-07-08 14:35:13 +02001093 xfs_dir2_sf_hdr_t *oldsfp; /* old sf directory */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 int oldsize; /* old inode size */
1095 xfs_dir2_sf_entry_t *sfep; /* new sf entry */
Christoph Hellwigac8ba502011-07-08 14:35:13 +02001096 xfs_dir2_sf_hdr_t *sfp; /* new sf directory */
Dave Chinner1c55cec2013-08-12 20:50:10 +10001097 struct xfs_mount *mp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001099 trace_xfs_dir2_sf_toino8(args);
1100
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 dp = args->dp;
Dave Chinner1c55cec2013-08-12 20:50:10 +10001102 mp = dp->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103
1104 /*
1105 * Copy the old directory to the buffer.
1106 * Then nuke it from the inode, and add the new buffer to the inode.
1107 * Don't want xfs_idata_realloc copying the data here.
1108 */
1109 oldsize = dp->i_df.if_bytes;
1110 buf = kmem_alloc(oldsize, KM_SLEEP);
Christoph Hellwigac8ba502011-07-08 14:35:13 +02001111 oldsfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
1112 ASSERT(oldsfp->i8count == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 memcpy(buf, oldsfp, oldsize);
1114 /*
Eric Sandeen5e06d142014-04-14 19:07:23 +10001115 * Compute the new inode size (nb: entry count + 1 for parent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 */
1117 newsize =
1118 oldsize +
Christoph Hellwigac8ba502011-07-08 14:35:13 +02001119 (oldsfp->count + 1) *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 ((uint)sizeof(xfs_dir2_ino8_t) - (uint)sizeof(xfs_dir2_ino4_t));
1121 xfs_idata_realloc(dp, -oldsize, XFS_DATA_FORK);
1122 xfs_idata_realloc(dp, newsize, XFS_DATA_FORK);
1123 /*
1124 * Reset our pointers, the data has moved.
1125 */
Christoph Hellwigac8ba502011-07-08 14:35:13 +02001126 oldsfp = (xfs_dir2_sf_hdr_t *)buf;
1127 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 /*
1129 * Fill in the new header.
1130 */
Christoph Hellwigac8ba502011-07-08 14:35:13 +02001131 sfp->count = oldsfp->count;
1132 sfp->i8count = 1;
Dave Chinner47401752013-10-29 22:11:47 +11001133 dp->d_ops->sf_put_parent_ino(sfp, dp->d_ops->sf_get_parent_ino(oldsfp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 /*
1135 * Copy the entries field by field.
1136 */
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001137 for (i = 0, sfep = xfs_dir2_sf_firstentry(sfp),
1138 oldsfep = xfs_dir2_sf_firstentry(oldsfp);
Christoph Hellwigac8ba502011-07-08 14:35:13 +02001139 i < sfp->count;
Dave Chinner32c54832013-10-29 22:11:46 +11001140 i++, sfep = dp->d_ops->sf_nextentry(sfp, sfep),
1141 oldsfep = dp->d_ops->sf_nextentry(oldsfp, oldsfep)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 sfep->namelen = oldsfep->namelen;
1143 sfep->offset = oldsfep->offset;
1144 memcpy(sfep->name, oldsfep->name, sfep->namelen);
Dave Chinner47401752013-10-29 22:11:47 +11001145 dp->d_ops->sf_put_ino(sfp, sfep,
1146 dp->d_ops->sf_get_ino(oldsfp, oldsfep));
1147 dp->d_ops->sf_put_ftype(sfep, dp->d_ops->sf_get_ftype(oldsfep));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 }
1149 /*
1150 * Clean up the inode.
1151 */
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10001152 kmem_free(buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 dp->i_d.di_size = newsize;
1154 xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE | XFS_ILOG_DDATA);
1155}