blob: 585dfdb7b6b688f13d43fed7dd5878afedfbc8a9 [file] [log] [blame]
Dave Chinner0b61f8a2018-06-05 19:42:14 -07001// SPDX-License-Identifier: GPL-2.0
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
Nathan Scott7b718762005-11-02 14:58:39 +11003 * Copyright (c) 2000-2003,2005 Silicon Graphics, Inc.
4 * All Rights Reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006#include "xfs.h"
Nathan Scotta844f452005-11-02 14:38:42 +11007#include "xfs_fs.h"
Dave Chinnera4fbe6a2013-10-23 10:51:50 +11008#include "xfs_format.h"
Dave Chinner239880e2013-10-23 10:50:10 +11009#include "xfs_log_format.h"
10#include "xfs_trans_resv.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include "xfs_mount.h"
Dave Chinner57062782013-10-15 09:17:51 +110012#include "xfs_da_format.h"
Nathan Scotta844f452005-11-02 14:38:42 +110013#include "xfs_da_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include "xfs_inode.h"
Dave Chinner239880e2013-10-23 10:50:10 +110015#include "xfs_trans.h"
Nathan Scotta844f452005-11-02 14:38:42 +110016#include "xfs_inode_item.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include "xfs_error.h"
Dave Chinner2b9ab5a2013-08-12 20:49:37 +100018#include "xfs_dir2.h"
Christoph Hellwig57926642011-07-13 13:43:48 +020019#include "xfs_dir2_priv.h"
Christoph Hellwig0b1b2132009-12-14 23:14:59 +000020#include "xfs_trace.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070021
22/*
23 * Prototypes for internal functions.
24 */
25static void xfs_dir2_sf_addname_easy(xfs_da_args_t *args,
26 xfs_dir2_sf_entry_t *sfep,
27 xfs_dir2_data_aoff_t offset,
28 int new_isize);
29static void xfs_dir2_sf_addname_hard(xfs_da_args_t *args, int objchange,
30 int new_isize);
31static int xfs_dir2_sf_addname_pick(xfs_da_args_t *args, int objchange,
32 xfs_dir2_sf_entry_t **sfepp,
33 xfs_dir2_data_aoff_t *offsetp);
34#ifdef DEBUG
35static void xfs_dir2_sf_check(xfs_da_args_t *args);
36#else
37#define xfs_dir2_sf_check(args)
38#endif /* DEBUG */
Christoph Hellwigd5cf09b2014-07-30 09:12:05 +100039
Linus Torvalds1da177e2005-04-16 15:20:36 -070040static void xfs_dir2_sf_toino4(xfs_da_args_t *args);
41static void xfs_dir2_sf_toino8(xfs_da_args_t *args);
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
Christoph Hellwig8bc38782011-07-08 14:35:03 +020043/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070044 * Given a block directory (dp/block), calculate its size as a shortform (sf)
45 * directory and a header for the sf directory, if it will fit it the
46 * space currently present in the inode. If it won't fit, the output
47 * size is too big (but not accurate).
48 */
49int /* size for sf form */
50xfs_dir2_block_sfsize(
51 xfs_inode_t *dp, /* incore inode pointer */
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +020052 xfs_dir2_data_hdr_t *hdr, /* block directory data */
Linus Torvalds1da177e2005-04-16 15:20:36 -070053 xfs_dir2_sf_hdr_t *sfhp) /* output: header for sf form */
54{
55 xfs_dir2_dataptr_t addr; /* data entry address */
56 xfs_dir2_leaf_entry_t *blp; /* leaf area of the block */
57 xfs_dir2_block_tail_t *btp; /* tail area of the block */
58 int count; /* shortform entry count */
59 xfs_dir2_data_entry_t *dep; /* data entry in the block */
60 int i; /* block entry index */
61 int i8count; /* count of big-inode entries */
62 int isdot; /* entry is "." */
63 int isdotdot; /* entry is ".." */
64 xfs_mount_t *mp; /* mount structure pointer */
65 int namelen; /* total name bytes */
Christoph Hellwig5bde1ba92005-11-02 15:06:18 +110066 xfs_ino_t parent = 0; /* parent inode number */
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 int size=0; /* total computed size */
Dave Chinner0cb97762013-08-12 20:50:09 +100068 int has_ftype;
Dave Chinner8f661932014-06-06 15:15:59 +100069 struct xfs_da_geometry *geo;
Linus Torvalds1da177e2005-04-16 15:20:36 -070070
71 mp = dp->i_mount;
Dave Chinner8f661932014-06-06 15:15:59 +100072 geo = mp->m_dir_geo;
Linus Torvalds1da177e2005-04-16 15:20:36 -070073
Dave Chinner0cb97762013-08-12 20:50:09 +100074 /*
75 * if there is a filetype field, add the extra byte to the namelen
76 * for each entry that we see.
77 */
78 has_ftype = xfs_sb_version_hasftype(&mp->m_sb) ? 1 : 0;
79
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 count = i8count = namelen = 0;
Dave Chinner8f661932014-06-06 15:15:59 +100081 btp = xfs_dir2_block_tail_p(geo, hdr);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +100082 blp = xfs_dir2_block_leaf_p(btp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070083
84 /*
85 * Iterate over the block's data entries by using the leaf pointers.
86 */
Nathan Scotte922fff2006-03-17 17:27:56 +110087 for (i = 0; i < be32_to_cpu(btp->count); i++) {
Nathan Scott3c1f9c12006-03-17 17:28:18 +110088 if ((addr = be32_to_cpu(blp[i].address)) == XFS_DIR2_NULL_DATAPTR)
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 continue;
90 /*
91 * Calculate the pointer to the entry at hand.
92 */
Dave Chinner30028032014-06-06 15:08:18 +100093 dep = (xfs_dir2_data_entry_t *)((char *)hdr +
Dave Chinner8f661932014-06-06 15:15:59 +100094 xfs_dir2_dataptr_to_off(geo, addr));
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 /*
96 * Detect . and .., so we can special-case them.
97 * . is not included in sf directories.
98 * .. is included by just the parent inode number.
99 */
100 isdot = dep->namelen == 1 && dep->name[0] == '.';
101 isdotdot =
102 dep->namelen == 2 &&
103 dep->name[0] == '.' && dep->name[1] == '.';
Christoph Hellwigd5cf09b2014-07-30 09:12:05 +1000104
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 if (!isdot)
Christoph Hellwigff9901c2006-06-09 14:48:37 +1000106 i8count += be64_to_cpu(dep->inumber) > XFS_DIR2_MAX_SHORT_INUM;
Christoph Hellwigd5cf09b2014-07-30 09:12:05 +1000107
Dave Chinner0cb97762013-08-12 20:50:09 +1000108 /* take into account the file type field */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 if (!isdot && !isdotdot) {
110 count++;
Dave Chinner0cb97762013-08-12 20:50:09 +1000111 namelen += dep->namelen + has_ftype;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 } else if (isdotdot)
Christoph Hellwigff9901c2006-06-09 14:48:37 +1000113 parent = be64_to_cpu(dep->inumber);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 /*
115 * Calculate the new size, see if we should give up yet.
116 */
Christoph Hellwig8353a642016-07-20 11:47:21 +1000117 size = xfs_dir2_sf_hdr_size(i8count) + /* header */
118 count * 3 * sizeof(u8) + /* namelen + offset */
119 namelen + /* name */
120 (i8count ? /* inumber */
Christoph Hellwig266b6962016-07-20 11:48:31 +1000121 count * XFS_INO64_SIZE :
122 count * XFS_INO32_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 if (size > XFS_IFORK_DSIZE(dp))
124 return size; /* size value is a failure */
125 }
126 /*
127 * Create the output header, if it worked.
128 */
129 sfhp->count = count;
130 sfhp->i8count = i8count;
Dave Chinner47401752013-10-29 22:11:47 +1100131 dp->d_ops->sf_put_parent_ino(sfhp, parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 return size;
133}
134
135/*
136 * Convert a block format directory to shortform.
137 * Caller has already checked that it will fit, and built us a header.
138 */
139int /* error */
140xfs_dir2_block_to_sf(
141 xfs_da_args_t *args, /* operation arguments */
Dave Chinner1d9025e2012-06-22 18:50:14 +1000142 struct xfs_buf *bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 int size, /* shortform directory size */
144 xfs_dir2_sf_hdr_t *sfhp) /* shortform directory hdr */
145{
Christoph Hellwiga64b0412011-07-08 14:35:32 +0200146 xfs_dir2_data_hdr_t *hdr; /* block header */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 xfs_dir2_data_entry_t *dep; /* data entry pointer */
148 xfs_inode_t *dp; /* incore directory inode */
149 xfs_dir2_data_unused_t *dup; /* unused data pointer */
150 char *endptr; /* end of data entries */
151 int error; /* error return value */
152 int logflags; /* inode logging flags */
153 xfs_mount_t *mp; /* filesystem mount point */
154 char *ptr; /* current data pointer */
155 xfs_dir2_sf_entry_t *sfep; /* shortform entry */
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200156 xfs_dir2_sf_hdr_t *sfp; /* shortform directory header */
Dave Chinnerb3f03ba2013-12-03 23:50:57 +1100157 xfs_dir2_sf_hdr_t *dst; /* temporary data buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000159 trace_xfs_dir2_block_to_sf(args);
160
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 dp = args->dp;
162 mp = dp->i_mount;
163
164 /*
Dave Chinnerb3f03ba2013-12-03 23:50:57 +1100165 * allocate a temporary destination buffer the size of the inode
166 * to format the data into. Once we have formatted the data, we
167 * can free the block and copy the formatted data into the inode literal
168 * area.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 */
Dave Chinnerb3f03ba2013-12-03 23:50:57 +1100170 dst = kmem_alloc(mp->m_sb.sb_inodesize, KM_SLEEP);
171 hdr = bp->b_addr;
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +0200172
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 * Copy the header into the newly allocate local space.
175 */
Dave Chinnerb3f03ba2013-12-03 23:50:57 +1100176 sfp = (xfs_dir2_sf_hdr_t *)dst;
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000177 memcpy(sfp, sfhp, xfs_dir2_sf_hdr_size(sfhp->i8count));
Dave Chinnerb3f03ba2013-12-03 23:50:57 +1100178
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 /*
180 * Set up to loop over the block's entries.
181 */
Dave Chinner2ca98772013-10-29 22:11:49 +1100182 ptr = (char *)dp->d_ops->data_entry_p(hdr);
Darrick J. Wongce92d292018-01-16 18:54:12 -0800183 endptr = xfs_dir3_data_endp(args->geo, hdr);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000184 sfep = xfs_dir2_sf_firstentry(sfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 /*
186 * Loop over the active and unused entries.
187 * Stop when we reach the leaf/tail portion of the block.
188 */
189 while (ptr < endptr) {
190 /*
191 * If it's unused, just skip over it.
192 */
193 dup = (xfs_dir2_data_unused_t *)ptr;
Nathan Scottad354eb2006-03-17 17:27:37 +1100194 if (be16_to_cpu(dup->freetag) == XFS_DIR2_DATA_FREE_TAG) {
195 ptr += be16_to_cpu(dup->length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 continue;
197 }
198 dep = (xfs_dir2_data_entry_t *)ptr;
199 /*
200 * Skip .
201 */
202 if (dep->namelen == 1 && dep->name[0] == '.')
Christoph Hellwigff9901c2006-06-09 14:48:37 +1000203 ASSERT(be64_to_cpu(dep->inumber) == dp->i_ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 /*
205 * Skip .., but make sure the inode number is right.
206 */
207 else if (dep->namelen == 2 &&
208 dep->name[0] == '.' && dep->name[1] == '.')
Christoph Hellwigff9901c2006-06-09 14:48:37 +1000209 ASSERT(be64_to_cpu(dep->inumber) ==
Dave Chinner47401752013-10-29 22:11:47 +1100210 dp->d_ops->sf_get_parent_ino(sfp));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 /*
212 * Normal entry, copy it into shortform.
213 */
214 else {
215 sfep->namelen = dep->namelen;
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000216 xfs_dir2_sf_put_offset(sfep,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 (xfs_dir2_data_aoff_t)
Christoph Hellwiga64b0412011-07-08 14:35:32 +0200218 ((char *)dep - (char *)hdr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 memcpy(sfep->name, dep->name, dep->namelen);
Dave Chinner47401752013-10-29 22:11:47 +1100220 dp->d_ops->sf_put_ino(sfp, sfep,
221 be64_to_cpu(dep->inumber));
222 dp->d_ops->sf_put_ftype(sfep,
Dave Chinner9d23fc82013-10-29 22:11:48 +1100223 dp->d_ops->data_get_ftype(dep));
Christoph Hellwig8bc38782011-07-08 14:35:03 +0200224
Dave Chinner32c54832013-10-29 22:11:46 +1100225 sfep = dp->d_ops->sf_nextentry(sfp, sfep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 }
Dave Chinner9d23fc82013-10-29 22:11:48 +1100227 ptr += dp->d_ops->data_entsize(dep->namelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 }
229 ASSERT((char *)sfep - (char *)sfp == size);
Dave Chinnerb3f03ba2013-12-03 23:50:57 +1100230
231 /* now we are done with the block, we can shrink the inode */
232 logflags = XFS_ILOG_CORE;
Dave Chinner7dda6e82014-06-06 15:11:18 +1000233 error = xfs_dir2_shrink_inode(args, args->geo->datablk, bp);
Dave Chinnerb3f03ba2013-12-03 23:50:57 +1100234 if (error) {
Dave Chinner24513372014-06-25 14:58:08 +1000235 ASSERT(error != -ENOSPC);
Dave Chinnerb3f03ba2013-12-03 23:50:57 +1100236 goto out;
237 }
238
239 /*
240 * The buffer is now unconditionally gone, whether
241 * xfs_dir2_shrink_inode worked or not.
242 *
243 * Convert the inode to local format and copy the data in.
244 */
Dave Chinnerb3f03ba2013-12-03 23:50:57 +1100245 ASSERT(dp->i_df.if_bytes == 0);
Christoph Hellwig143f4ae2016-04-06 07:41:43 +1000246 xfs_init_local_fork(dp, XFS_DATA_FORK, dst, size);
247 dp->i_d.di_format = XFS_DINODE_FMT_LOCAL;
248 dp->i_d.di_size = size;
Dave Chinnerb3f03ba2013-12-03 23:50:57 +1100249
250 logflags |= XFS_ILOG_DDATA;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 xfs_dir2_sf_check(args);
252out:
253 xfs_trans_log_inode(args->trans, dp, logflags);
Dave Chinnerb3f03ba2013-12-03 23:50:57 +1100254 kmem_free(dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 return error;
256}
257
258/*
259 * Add a name to a shortform directory.
260 * There are two algorithms, "easy" and "hard" which we decide on
261 * before changing anything.
262 * Convert to block form if necessary, if the new entry won't fit.
263 */
264int /* error */
265xfs_dir2_sf_addname(
266 xfs_da_args_t *args) /* operation arguments */
267{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 xfs_inode_t *dp; /* incore directory inode */
269 int error; /* error return value */
270 int incr_isize; /* total change in size */
271 int new_isize; /* di_size after adding name */
272 int objchange; /* changing to 8-byte inodes */
Christoph Hellwig5bde1ba92005-11-02 15:06:18 +1100273 xfs_dir2_data_aoff_t offset = 0; /* offset for new entry */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 int pick; /* which algorithm to use */
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200275 xfs_dir2_sf_hdr_t *sfp; /* shortform structure */
Christoph Hellwig5bde1ba92005-11-02 15:06:18 +1100276 xfs_dir2_sf_entry_t *sfep = NULL; /* shortform entry */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000278 trace_xfs_dir2_sf_addname(args);
279
Dave Chinner24513372014-06-25 14:58:08 +1000280 ASSERT(xfs_dir2_sf_lookup(args) == -ENOENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 dp = args->dp;
282 ASSERT(dp->i_df.if_flags & XFS_IFINLINE);
283 /*
284 * Make sure the shortform value has some of its header.
285 */
286 if (dp->i_d.di_size < offsetof(xfs_dir2_sf_hdr_t, parent)) {
287 ASSERT(XFS_FORCED_SHUTDOWN(dp->i_mount));
Dave Chinner24513372014-06-25 14:58:08 +1000288 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 }
290 ASSERT(dp->i_df.if_bytes == dp->i_d.di_size);
291 ASSERT(dp->i_df.if_u1.if_data != NULL);
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200292 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
293 ASSERT(dp->i_d.di_size >= xfs_dir2_sf_hdr_size(sfp->i8count));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 /*
295 * Compute entry (and change in) size.
296 */
Eric Sandeen5e06d142014-04-14 19:07:23 +1000297 incr_isize = dp->d_ops->sf_entsize(sfp, args->namelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 objchange = 0;
Christoph Hellwigd5cf09b2014-07-30 09:12:05 +1000299
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 /*
301 * Do we have to change to 8 byte inodes?
302 */
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200303 if (args->inumber > XFS_DIR2_MAX_SHORT_INUM && sfp->i8count == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 /*
Eric Sandeen5e06d142014-04-14 19:07:23 +1000305 * Yes, adjust the inode size. old count + (parent + new)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 */
Christoph Hellwig266b6962016-07-20 11:48:31 +1000307 incr_isize += (sfp->count + 2) * XFS_INO64_DIFF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 objchange = 1;
309 }
Christoph Hellwigd5cf09b2014-07-30 09:12:05 +1000310
Eric Sandeen5e06d142014-04-14 19:07:23 +1000311 new_isize = (int)dp->i_d.di_size + incr_isize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 /*
313 * Won't fit as shortform any more (due to size),
314 * or the pick routine says it won't (due to offset values).
315 */
316 if (new_isize > XFS_IFORK_DSIZE(dp) ||
317 (pick =
318 xfs_dir2_sf_addname_pick(args, objchange, &sfep, &offset)) == 0) {
319 /*
320 * Just checking or no space reservation, it doesn't fit.
321 */
Barry Naujok6a178102008-05-21 16:42:05 +1000322 if ((args->op_flags & XFS_DA_OP_JUSTCHECK) || args->total == 0)
Dave Chinner24513372014-06-25 14:58:08 +1000323 return -ENOSPC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 /*
325 * Convert to block form then add the name.
326 */
327 error = xfs_dir2_sf_to_block(args);
328 if (error)
329 return error;
330 return xfs_dir2_block_addname(args);
331 }
332 /*
333 * Just checking, it fits.
334 */
Barry Naujok6a178102008-05-21 16:42:05 +1000335 if (args->op_flags & XFS_DA_OP_JUSTCHECK)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 return 0;
337 /*
338 * Do it the easy way - just add it at the end.
339 */
340 if (pick == 1)
341 xfs_dir2_sf_addname_easy(args, sfep, offset, new_isize);
342 /*
343 * Do it the hard way - look for a place to insert the new entry.
344 * Convert to 8 byte inode numbers first if necessary.
345 */
346 else {
347 ASSERT(pick == 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 if (objchange)
349 xfs_dir2_sf_toino8(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 xfs_dir2_sf_addname_hard(args, objchange, new_isize);
351 }
352 xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE | XFS_ILOG_DDATA);
353 return 0;
354}
355
356/*
357 * Add the new entry the "easy" way.
358 * This is copying the old directory and adding the new entry at the end.
359 * Since it's sorted by "offset" we need room after the last offset
360 * that's already there, and then room to convert to a block directory.
361 * This is already checked by the pick routine.
362 */
363static void
364xfs_dir2_sf_addname_easy(
365 xfs_da_args_t *args, /* operation arguments */
366 xfs_dir2_sf_entry_t *sfep, /* pointer to new entry */
367 xfs_dir2_data_aoff_t offset, /* offset to use for new ent */
368 int new_isize) /* new directory size */
369{
370 int byteoff; /* byte offset in sf dir */
371 xfs_inode_t *dp; /* incore directory inode */
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200372 xfs_dir2_sf_hdr_t *sfp; /* shortform structure */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373
374 dp = args->dp;
375
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200376 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 byteoff = (int)((char *)sfep - (char *)sfp);
378 /*
379 * Grow the in-inode space.
380 */
Dave Chinner32c54832013-10-29 22:11:46 +1100381 xfs_idata_realloc(dp, dp->d_ops->sf_entsize(sfp, args->namelen),
Dave Chinner0cb97762013-08-12 20:50:09 +1000382 XFS_DATA_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 /*
384 * Need to set up again due to realloc of the inode data.
385 */
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200386 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 sfep = (xfs_dir2_sf_entry_t *)((char *)sfp + byteoff);
388 /*
389 * Fill in the new entry.
390 */
391 sfep->namelen = args->namelen;
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000392 xfs_dir2_sf_put_offset(sfep, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 memcpy(sfep->name, args->name, sfep->namelen);
Dave Chinner47401752013-10-29 22:11:47 +1100394 dp->d_ops->sf_put_ino(sfp, sfep, args->inumber);
395 dp->d_ops->sf_put_ftype(sfep, args->filetype);
Dave Chinner1c55cec2013-08-12 20:50:10 +1000396
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 /*
398 * Update the header and inode.
399 */
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200400 sfp->count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 if (args->inumber > XFS_DIR2_MAX_SHORT_INUM)
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200402 sfp->i8count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 dp->i_d.di_size = new_isize;
404 xfs_dir2_sf_check(args);
405}
406
407/*
408 * Add the new entry the "hard" way.
409 * The caller has already converted to 8 byte inode numbers if necessary,
410 * in which case we need to leave the i8count at 1.
411 * Find a hole that the new entry will fit into, and copy
412 * the first part of the entries, the new entry, and the last part of
413 * the entries.
414 */
415/* ARGSUSED */
416static void
417xfs_dir2_sf_addname_hard(
418 xfs_da_args_t *args, /* operation arguments */
419 int objchange, /* changing inode number size */
420 int new_isize) /* new directory size */
421{
422 int add_datasize; /* data size need for new ent */
423 char *buf; /* buffer for old */
424 xfs_inode_t *dp; /* incore directory inode */
425 int eof; /* reached end of old dir */
426 int nbytes; /* temp for byte copies */
427 xfs_dir2_data_aoff_t new_offset; /* next offset value */
428 xfs_dir2_data_aoff_t offset; /* current offset value */
429 int old_isize; /* previous di_size */
430 xfs_dir2_sf_entry_t *oldsfep; /* entry in original dir */
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200431 xfs_dir2_sf_hdr_t *oldsfp; /* original shortform dir */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 xfs_dir2_sf_entry_t *sfep; /* entry in new dir */
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200433 xfs_dir2_sf_hdr_t *sfp; /* new shortform dir */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434
435 /*
436 * Copy the old directory to the stack buffer.
437 */
438 dp = args->dp;
439
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200440 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 old_isize = (int)dp->i_d.di_size;
442 buf = kmem_alloc(old_isize, KM_SLEEP);
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200443 oldsfp = (xfs_dir2_sf_hdr_t *)buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 memcpy(oldsfp, sfp, old_isize);
445 /*
446 * Loop over the old directory finding the place we're going
447 * to insert the new entry.
448 * If it's going to end up at the end then oldsfep will point there.
449 */
Dave Chinner1c9a5b22013-10-30 09:15:02 +1100450 for (offset = dp->d_ops->data_first_offset,
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000451 oldsfep = xfs_dir2_sf_firstentry(oldsfp),
Dave Chinner9d23fc82013-10-29 22:11:48 +1100452 add_datasize = dp->d_ops->data_entsize(args->namelen),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 eof = (char *)oldsfep == &buf[old_isize];
454 !eof;
Dave Chinner9d23fc82013-10-29 22:11:48 +1100455 offset = new_offset + dp->d_ops->data_entsize(oldsfep->namelen),
Dave Chinner32c54832013-10-29 22:11:46 +1100456 oldsfep = dp->d_ops->sf_nextentry(oldsfp, oldsfep),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 eof = (char *)oldsfep == &buf[old_isize]) {
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000458 new_offset = xfs_dir2_sf_get_offset(oldsfep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 if (offset + add_datasize <= new_offset)
460 break;
461 }
462 /*
463 * Get rid of the old directory, then allocate space for
464 * the new one. We do this so xfs_idata_realloc won't copy
465 * the data.
466 */
467 xfs_idata_realloc(dp, -old_isize, XFS_DATA_FORK);
468 xfs_idata_realloc(dp, new_isize, XFS_DATA_FORK);
469 /*
470 * Reset the pointer since the buffer was reallocated.
471 */
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200472 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 /*
474 * Copy the first part of the directory, including the header.
475 */
476 nbytes = (int)((char *)oldsfep - (char *)oldsfp);
477 memcpy(sfp, oldsfp, nbytes);
478 sfep = (xfs_dir2_sf_entry_t *)((char *)sfp + nbytes);
479 /*
480 * Fill in the new entry, and update the header counts.
481 */
482 sfep->namelen = args->namelen;
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000483 xfs_dir2_sf_put_offset(sfep, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 memcpy(sfep->name, args->name, sfep->namelen);
Dave Chinner47401752013-10-29 22:11:47 +1100485 dp->d_ops->sf_put_ino(sfp, sfep, args->inumber);
486 dp->d_ops->sf_put_ftype(sfep, args->filetype);
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200487 sfp->count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 if (args->inumber > XFS_DIR2_MAX_SHORT_INUM && !objchange)
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200489 sfp->i8count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 /*
491 * If there's more left to copy, do that.
492 */
493 if (!eof) {
Dave Chinner32c54832013-10-29 22:11:46 +1100494 sfep = dp->d_ops->sf_nextentry(sfp, sfep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 memcpy(sfep, oldsfep, old_isize - nbytes);
496 }
Denys Vlasenkof0e2d932008-05-19 16:31:57 +1000497 kmem_free(buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 dp->i_d.di_size = new_isize;
499 xfs_dir2_sf_check(args);
500}
501
502/*
503 * Decide if the new entry will fit at all.
504 * If it will fit, pick between adding the new entry to the end (easy)
505 * or somewhere else (hard).
506 * Return 0 (won't fit), 1 (easy), 2 (hard).
507 */
508/*ARGSUSED*/
509static int /* pick result */
510xfs_dir2_sf_addname_pick(
511 xfs_da_args_t *args, /* operation arguments */
512 int objchange, /* inode # size changes */
513 xfs_dir2_sf_entry_t **sfepp, /* out(1): new entry ptr */
514 xfs_dir2_data_aoff_t *offsetp) /* out(1): new offset */
515{
516 xfs_inode_t *dp; /* incore directory inode */
517 int holefit; /* found hole it will fit in */
518 int i; /* entry number */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 xfs_dir2_data_aoff_t offset; /* data block offset */
520 xfs_dir2_sf_entry_t *sfep; /* shortform entry */
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200521 xfs_dir2_sf_hdr_t *sfp; /* shortform structure */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 int size; /* entry's data size */
523 int used; /* data bytes used */
524
525 dp = args->dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200527 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
Dave Chinner9d23fc82013-10-29 22:11:48 +1100528 size = dp->d_ops->data_entsize(args->namelen);
Dave Chinner1c9a5b22013-10-30 09:15:02 +1100529 offset = dp->d_ops->data_first_offset;
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000530 sfep = xfs_dir2_sf_firstentry(sfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 holefit = 0;
532 /*
533 * Loop over sf entries.
534 * Keep track of data offset and whether we've seen a place
535 * to insert the new entry.
536 */
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200537 for (i = 0; i < sfp->count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 if (!holefit)
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000539 holefit = offset + size <= xfs_dir2_sf_get_offset(sfep);
540 offset = xfs_dir2_sf_get_offset(sfep) +
Dave Chinner9d23fc82013-10-29 22:11:48 +1100541 dp->d_ops->data_entsize(sfep->namelen);
Dave Chinner32c54832013-10-29 22:11:46 +1100542 sfep = dp->d_ops->sf_nextentry(sfp, sfep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 }
544 /*
545 * Calculate data bytes used excluding the new entry, if this
546 * was a data block (block form directory).
547 */
548 used = offset +
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200549 (sfp->count + 3) * (uint)sizeof(xfs_dir2_leaf_entry_t) +
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 (uint)sizeof(xfs_dir2_block_tail_t);
551 /*
552 * If it won't fit in a block form then we can't insert it,
553 * we'll go back, convert to block, then try the insert and convert
554 * to leaf.
555 */
Dave Chinner8f661932014-06-06 15:15:59 +1000556 if (used + (holefit ? 0 : size) > args->geo->blksize)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 return 0;
558 /*
559 * If changing the inode number size, do it the hard way.
560 */
Christoph Hellwigd5cf09b2014-07-30 09:12:05 +1000561 if (objchange)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 return 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 /*
564 * If it won't fit at the end then do it the hard way (use the hole).
565 */
Dave Chinner8f661932014-06-06 15:15:59 +1000566 if (used + size > args->geo->blksize)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 return 2;
568 /*
569 * Do it the easy way.
570 */
571 *sfepp = sfep;
572 *offsetp = offset;
573 return 1;
574}
575
576#ifdef DEBUG
577/*
578 * Check consistency of shortform directory, assert if bad.
579 */
580static void
581xfs_dir2_sf_check(
582 xfs_da_args_t *args) /* operation arguments */
583{
584 xfs_inode_t *dp; /* incore directory inode */
585 int i; /* entry number */
586 int i8count; /* number of big inode#s */
587 xfs_ino_t ino; /* entry inode number */
588 int offset; /* data offset */
589 xfs_dir2_sf_entry_t *sfep; /* shortform dir entry */
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200590 xfs_dir2_sf_hdr_t *sfp; /* shortform structure */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591
592 dp = args->dp;
593
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200594 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
Dave Chinner1c9a5b22013-10-30 09:15:02 +1100595 offset = dp->d_ops->data_first_offset;
Dave Chinner47401752013-10-29 22:11:47 +1100596 ino = dp->d_ops->sf_get_parent_ino(sfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 i8count = ino > XFS_DIR2_MAX_SHORT_INUM;
598
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000599 for (i = 0, sfep = xfs_dir2_sf_firstentry(sfp);
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200600 i < sfp->count;
Dave Chinner32c54832013-10-29 22:11:46 +1100601 i++, sfep = dp->d_ops->sf_nextentry(sfp, sfep)) {
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000602 ASSERT(xfs_dir2_sf_get_offset(sfep) >= offset);
Dave Chinner47401752013-10-29 22:11:47 +1100603 ino = dp->d_ops->sf_get_ino(sfp, sfep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 i8count += ino > XFS_DIR2_MAX_SHORT_INUM;
605 offset =
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000606 xfs_dir2_sf_get_offset(sfep) +
Dave Chinner9d23fc82013-10-29 22:11:48 +1100607 dp->d_ops->data_entsize(sfep->namelen);
Dave Chinner47401752013-10-29 22:11:47 +1100608 ASSERT(dp->d_ops->sf_get_ftype(sfep) < XFS_DIR3_FT_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 }
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200610 ASSERT(i8count == sfp->i8count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 ASSERT((char *)sfep - (char *)sfp == dp->i_d.di_size);
612 ASSERT(offset +
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200613 (sfp->count + 2) * (uint)sizeof(xfs_dir2_leaf_entry_t) +
Dave Chinner8f661932014-06-06 15:15:59 +1000614 (uint)sizeof(xfs_dir2_block_tail_t) <= args->geo->blksize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615}
616#endif /* DEBUG */
617
Darrick J. Wong630a04e2017-03-15 00:24:25 -0700618/* Verify the consistency of an inline directory. */
Darrick J. Wongdc042c22018-01-08 10:51:06 -0800619xfs_failaddr_t
Darrick J. Wong630a04e2017-03-15 00:24:25 -0700620xfs_dir2_sf_verify(
Darrick J. Wong78420282017-04-03 12:22:20 -0700621 struct xfs_inode *ip)
Darrick J. Wong630a04e2017-03-15 00:24:25 -0700622{
Darrick J. Wong78420282017-04-03 12:22:20 -0700623 struct xfs_mount *mp = ip->i_mount;
624 struct xfs_dir2_sf_hdr *sfp;
Darrick J. Wong630a04e2017-03-15 00:24:25 -0700625 struct xfs_dir2_sf_entry *sfep;
626 struct xfs_dir2_sf_entry *next_sfep;
627 char *endp;
628 const struct xfs_dir_ops *dops;
Darrick J. Wong78420282017-04-03 12:22:20 -0700629 struct xfs_ifork *ifp;
Darrick J. Wong630a04e2017-03-15 00:24:25 -0700630 xfs_ino_t ino;
631 int i;
632 int i8count;
633 int offset;
Darrick J. Wong78420282017-04-03 12:22:20 -0700634 int size;
635 int error;
Darrick J. Wongc8ce5402017-06-16 11:00:05 -0700636 uint8_t filetype;
Darrick J. Wong630a04e2017-03-15 00:24:25 -0700637
Darrick J. Wong78420282017-04-03 12:22:20 -0700638 ASSERT(ip->i_d.di_format == XFS_DINODE_FMT_LOCAL);
639 /*
640 * xfs_iread calls us before xfs_setup_inode sets up ip->d_ops,
641 * so we can only trust the mountpoint to have the right pointer.
642 */
Darrick J. Wong630a04e2017-03-15 00:24:25 -0700643 dops = xfs_dir_get_ops(mp, NULL);
644
Darrick J. Wong78420282017-04-03 12:22:20 -0700645 ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
646 sfp = (struct xfs_dir2_sf_hdr *)ifp->if_u1.if_data;
647 size = ifp->if_bytes;
648
Darrick J. Wong630a04e2017-03-15 00:24:25 -0700649 /*
650 * Give up if the directory is way too short.
651 */
Darrick J. Wong78420282017-04-03 12:22:20 -0700652 if (size <= offsetof(struct xfs_dir2_sf_hdr, parent) ||
653 size < xfs_dir2_sf_hdr_size(sfp->i8count))
Darrick J. Wongdc042c22018-01-08 10:51:06 -0800654 return __this_address;
Darrick J. Wong630a04e2017-03-15 00:24:25 -0700655
656 endp = (char *)sfp + size;
657
658 /* Check .. entry */
659 ino = dops->sf_get_parent_ino(sfp);
660 i8count = ino > XFS_DIR2_MAX_SHORT_INUM;
Darrick J. Wong78420282017-04-03 12:22:20 -0700661 error = xfs_dir_ino_validate(mp, ino);
662 if (error)
Darrick J. Wongdc042c22018-01-08 10:51:06 -0800663 return __this_address;
Darrick J. Wong630a04e2017-03-15 00:24:25 -0700664 offset = dops->data_first_offset;
665
666 /* Check all reported entries */
667 sfep = xfs_dir2_sf_firstentry(sfp);
668 for (i = 0; i < sfp->count; i++) {
669 /*
670 * struct xfs_dir2_sf_entry has a variable length.
671 * Check the fixed-offset parts of the structure are
672 * within the data buffer.
673 */
Darrick J. Wong78420282017-04-03 12:22:20 -0700674 if (((char *)sfep + sizeof(*sfep)) >= endp)
Darrick J. Wongdc042c22018-01-08 10:51:06 -0800675 return __this_address;
Darrick J. Wong630a04e2017-03-15 00:24:25 -0700676
677 /* Don't allow names with known bad length. */
Darrick J. Wong78420282017-04-03 12:22:20 -0700678 if (sfep->namelen == 0)
Darrick J. Wongdc042c22018-01-08 10:51:06 -0800679 return __this_address;
Darrick J. Wong630a04e2017-03-15 00:24:25 -0700680
681 /*
682 * Check that the variable-length part of the structure is
683 * within the data buffer. The next entry starts after the
684 * name component, so nextentry is an acceptable test.
685 */
686 next_sfep = dops->sf_nextentry(sfp, sfep);
Darrick J. Wong78420282017-04-03 12:22:20 -0700687 if (endp < (char *)next_sfep)
Darrick J. Wongdc042c22018-01-08 10:51:06 -0800688 return __this_address;
Darrick J. Wong630a04e2017-03-15 00:24:25 -0700689
690 /* Check that the offsets always increase. */
Darrick J. Wong78420282017-04-03 12:22:20 -0700691 if (xfs_dir2_sf_get_offset(sfep) < offset)
Darrick J. Wongdc042c22018-01-08 10:51:06 -0800692 return __this_address;
Darrick J. Wong630a04e2017-03-15 00:24:25 -0700693
694 /* Check the inode number. */
695 ino = dops->sf_get_ino(sfp, sfep);
696 i8count += ino > XFS_DIR2_MAX_SHORT_INUM;
Darrick J. Wong78420282017-04-03 12:22:20 -0700697 error = xfs_dir_ino_validate(mp, ino);
698 if (error)
Darrick J. Wongdc042c22018-01-08 10:51:06 -0800699 return __this_address;
Darrick J. Wong630a04e2017-03-15 00:24:25 -0700700
701 /* Check the file type. */
702 filetype = dops->sf_get_ftype(sfep);
Darrick J. Wong78420282017-04-03 12:22:20 -0700703 if (filetype >= XFS_DIR3_FT_MAX)
Darrick J. Wongdc042c22018-01-08 10:51:06 -0800704 return __this_address;
Darrick J. Wong630a04e2017-03-15 00:24:25 -0700705
706 offset = xfs_dir2_sf_get_offset(sfep) +
707 dops->data_entsize(sfep->namelen);
708
709 sfep = next_sfep;
710 }
Darrick J. Wong78420282017-04-03 12:22:20 -0700711 if (i8count != sfp->i8count)
Darrick J. Wongdc042c22018-01-08 10:51:06 -0800712 return __this_address;
Darrick J. Wong78420282017-04-03 12:22:20 -0700713 if ((void *)sfep != (void *)endp)
Darrick J. Wongdc042c22018-01-08 10:51:06 -0800714 return __this_address;
Darrick J. Wong630a04e2017-03-15 00:24:25 -0700715
716 /* Make sure this whole thing ought to be in local format. */
Darrick J. Wong78420282017-04-03 12:22:20 -0700717 if (offset + (sfp->count + 2) * (uint)sizeof(xfs_dir2_leaf_entry_t) +
718 (uint)sizeof(xfs_dir2_block_tail_t) > mp->m_dir_geo->blksize)
Darrick J. Wongdc042c22018-01-08 10:51:06 -0800719 return __this_address;
Darrick J. Wong630a04e2017-03-15 00:24:25 -0700720
Darrick J. Wongdc042c22018-01-08 10:51:06 -0800721 return NULL;
Darrick J. Wong630a04e2017-03-15 00:24:25 -0700722}
723
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724/*
725 * Create a new (shortform) directory.
726 */
727int /* error, always 0 */
728xfs_dir2_sf_create(
729 xfs_da_args_t *args, /* operation arguments */
730 xfs_ino_t pino) /* parent inode number */
731{
732 xfs_inode_t *dp; /* incore directory inode */
733 int i8count; /* parent inode is an 8-byte number */
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200734 xfs_dir2_sf_hdr_t *sfp; /* shortform structure */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 int size; /* directory size */
736
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000737 trace_xfs_dir2_sf_create(args);
738
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 dp = args->dp;
740
741 ASSERT(dp != NULL);
742 ASSERT(dp->i_d.di_size == 0);
743 /*
744 * If it's currently a zero-length extent file,
745 * convert it to local format.
746 */
747 if (dp->i_d.di_format == XFS_DINODE_FMT_EXTENTS) {
748 dp->i_df.if_flags &= ~XFS_IFEXTENTS; /* just in case */
749 dp->i_d.di_format = XFS_DINODE_FMT_LOCAL;
750 xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE);
751 dp->i_df.if_flags |= XFS_IFINLINE;
752 }
753 ASSERT(dp->i_df.if_flags & XFS_IFINLINE);
754 ASSERT(dp->i_df.if_bytes == 0);
755 i8count = pino > XFS_DIR2_MAX_SHORT_INUM;
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000756 size = xfs_dir2_sf_hdr_size(i8count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 /*
758 * Make a buffer for the data.
759 */
760 xfs_idata_realloc(dp, size, XFS_DATA_FORK);
761 /*
762 * Fill in the header,
763 */
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200764 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
765 sfp->i8count = i8count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 /*
767 * Now can put in the inode number, since i8count is set.
768 */
Dave Chinner47401752013-10-29 22:11:47 +1100769 dp->d_ops->sf_put_parent_ino(sfp, pino);
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200770 sfp->count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 dp->i_d.di_size = size;
772 xfs_dir2_sf_check(args);
773 xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE | XFS_ILOG_DDATA);
774 return 0;
775}
776
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777/*
778 * Lookup an entry in a shortform directory.
779 * Returns EEXIST if found, ENOENT if not found.
780 */
781int /* error */
782xfs_dir2_sf_lookup(
783 xfs_da_args_t *args) /* operation arguments */
784{
785 xfs_inode_t *dp; /* incore directory inode */
786 int i; /* entry index */
Barry Naujok384f3ce2008-05-21 16:58:22 +1000787 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 xfs_dir2_sf_entry_t *sfep; /* shortform directory entry */
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200789 xfs_dir2_sf_hdr_t *sfp; /* shortform structure */
Barry Naujok5163f952008-05-21 16:41:01 +1000790 enum xfs_dacmp cmp; /* comparison result */
Barry Naujok384f3ce2008-05-21 16:58:22 +1000791 xfs_dir2_sf_entry_t *ci_sfep; /* case-insens. entry */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000793 trace_xfs_dir2_sf_lookup(args);
794
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 xfs_dir2_sf_check(args);
796 dp = args->dp;
797
798 ASSERT(dp->i_df.if_flags & XFS_IFINLINE);
799 /*
800 * Bail out if the directory is way too short.
801 */
802 if (dp->i_d.di_size < offsetof(xfs_dir2_sf_hdr_t, parent)) {
803 ASSERT(XFS_FORCED_SHUTDOWN(dp->i_mount));
Dave Chinner24513372014-06-25 14:58:08 +1000804 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 }
806 ASSERT(dp->i_df.if_bytes == dp->i_d.di_size);
807 ASSERT(dp->i_df.if_u1.if_data != NULL);
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200808 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
809 ASSERT(dp->i_d.di_size >= xfs_dir2_sf_hdr_size(sfp->i8count));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 /*
811 * Special case for .
812 */
813 if (args->namelen == 1 && args->name[0] == '.') {
814 args->inumber = dp->i_ino;
Barry Naujok5163f952008-05-21 16:41:01 +1000815 args->cmpresult = XFS_CMP_EXACT;
Dave Chinner1c55cec2013-08-12 20:50:10 +1000816 args->filetype = XFS_DIR3_FT_DIR;
Dave Chinner24513372014-06-25 14:58:08 +1000817 return -EEXIST;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 }
819 /*
820 * Special case for ..
821 */
822 if (args->namelen == 2 &&
823 args->name[0] == '.' && args->name[1] == '.') {
Dave Chinner47401752013-10-29 22:11:47 +1100824 args->inumber = dp->d_ops->sf_get_parent_ino(sfp);
Barry Naujok5163f952008-05-21 16:41:01 +1000825 args->cmpresult = XFS_CMP_EXACT;
Dave Chinner1c55cec2013-08-12 20:50:10 +1000826 args->filetype = XFS_DIR3_FT_DIR;
Dave Chinner24513372014-06-25 14:58:08 +1000827 return -EEXIST;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 }
829 /*
830 * Loop over all the entries trying to match ours.
831 */
Barry Naujok384f3ce2008-05-21 16:58:22 +1000832 ci_sfep = NULL;
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200833 for (i = 0, sfep = xfs_dir2_sf_firstentry(sfp); i < sfp->count;
Dave Chinner32c54832013-10-29 22:11:46 +1100834 i++, sfep = dp->d_ops->sf_nextentry(sfp, sfep)) {
Barry Naujok5163f952008-05-21 16:41:01 +1000835 /*
836 * Compare name and if it's an exact match, return the inode
837 * number. If it's the first case-insensitive match, store the
838 * inode number and continue looking for an exact match.
839 */
840 cmp = dp->i_mount->m_dirnameops->compname(args, sfep->name,
841 sfep->namelen);
842 if (cmp != XFS_CMP_DIFFERENT && cmp != args->cmpresult) {
843 args->cmpresult = cmp;
Dave Chinner47401752013-10-29 22:11:47 +1100844 args->inumber = dp->d_ops->sf_get_ino(sfp, sfep);
845 args->filetype = dp->d_ops->sf_get_ftype(sfep);
Barry Naujok5163f952008-05-21 16:41:01 +1000846 if (cmp == XFS_CMP_EXACT)
Dave Chinner24513372014-06-25 14:58:08 +1000847 return -EEXIST;
Barry Naujok384f3ce2008-05-21 16:58:22 +1000848 ci_sfep = sfep;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 }
850 }
Barry Naujok6a178102008-05-21 16:42:05 +1000851 ASSERT(args->op_flags & XFS_DA_OP_OKNOENT);
Barry Naujok5163f952008-05-21 16:41:01 +1000852 /*
853 * Here, we can only be doing a lookup (not a rename or replace).
Dave Chinner24513372014-06-25 14:58:08 +1000854 * If a case-insensitive match was not found, return -ENOENT.
Barry Naujok5163f952008-05-21 16:41:01 +1000855 */
Barry Naujok384f3ce2008-05-21 16:58:22 +1000856 if (!ci_sfep)
Dave Chinner24513372014-06-25 14:58:08 +1000857 return -ENOENT;
Barry Naujok384f3ce2008-05-21 16:58:22 +1000858 /* otherwise process the CI match as required by the caller */
859 error = xfs_dir_cilookup_result(args, ci_sfep->name, ci_sfep->namelen);
Eric Sandeenb474c7a2014-06-22 15:04:54 +1000860 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861}
862
863/*
864 * Remove an entry from a shortform directory.
865 */
866int /* error */
867xfs_dir2_sf_removename(
868 xfs_da_args_t *args)
869{
870 int byteoff; /* offset of removed entry */
871 xfs_inode_t *dp; /* incore directory inode */
872 int entsize; /* this entry's size */
873 int i; /* shortform entry index */
874 int newsize; /* new inode size */
875 int oldsize; /* old inode size */
876 xfs_dir2_sf_entry_t *sfep; /* shortform directory entry */
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200877 xfs_dir2_sf_hdr_t *sfp; /* shortform structure */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000879 trace_xfs_dir2_sf_removename(args);
880
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 dp = args->dp;
882
883 ASSERT(dp->i_df.if_flags & XFS_IFINLINE);
884 oldsize = (int)dp->i_d.di_size;
885 /*
886 * Bail out if the directory is way too short.
887 */
888 if (oldsize < offsetof(xfs_dir2_sf_hdr_t, parent)) {
889 ASSERT(XFS_FORCED_SHUTDOWN(dp->i_mount));
Dave Chinner24513372014-06-25 14:58:08 +1000890 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 }
892 ASSERT(dp->i_df.if_bytes == oldsize);
893 ASSERT(dp->i_df.if_u1.if_data != NULL);
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200894 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
895 ASSERT(oldsize >= xfs_dir2_sf_hdr_size(sfp->i8count));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 /*
897 * Loop over the old directory entries.
898 * Find the one we're deleting.
899 */
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200900 for (i = 0, sfep = xfs_dir2_sf_firstentry(sfp); i < sfp->count;
Dave Chinner32c54832013-10-29 22:11:46 +1100901 i++, sfep = dp->d_ops->sf_nextentry(sfp, sfep)) {
Barry Naujok5163f952008-05-21 16:41:01 +1000902 if (xfs_da_compname(args, sfep->name, sfep->namelen) ==
903 XFS_CMP_EXACT) {
Dave Chinner47401752013-10-29 22:11:47 +1100904 ASSERT(dp->d_ops->sf_get_ino(sfp, sfep) ==
Christoph Hellwig8bc38782011-07-08 14:35:03 +0200905 args->inumber);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 break;
907 }
908 }
909 /*
910 * Didn't find it.
911 */
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200912 if (i == sfp->count)
Dave Chinner24513372014-06-25 14:58:08 +1000913 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 /*
915 * Calculate sizes.
916 */
917 byteoff = (int)((char *)sfep - (char *)sfp);
Dave Chinner32c54832013-10-29 22:11:46 +1100918 entsize = dp->d_ops->sf_entsize(sfp, args->namelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 newsize = oldsize - entsize;
920 /*
921 * Copy the part if any after the removed entry, sliding it down.
922 */
923 if (byteoff + entsize < oldsize)
924 memmove((char *)sfp + byteoff, (char *)sfp + byteoff + entsize,
925 oldsize - (byteoff + entsize));
926 /*
927 * Fix up the header and file size.
928 */
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200929 sfp->count--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 dp->i_d.di_size = newsize;
931 /*
932 * Reallocate, making it smaller.
933 */
934 xfs_idata_realloc(dp, newsize - oldsize, XFS_DATA_FORK);
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200935 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 /*
937 * Are we changing inode number size?
938 */
939 if (args->inumber > XFS_DIR2_MAX_SHORT_INUM) {
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200940 if (sfp->i8count == 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 xfs_dir2_sf_toino4(args);
942 else
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200943 sfp->i8count--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 xfs_dir2_sf_check(args);
946 xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE | XFS_ILOG_DDATA);
947 return 0;
948}
949
950/*
951 * Replace the inode number of an entry in a shortform directory.
952 */
953int /* error */
954xfs_dir2_sf_replace(
955 xfs_da_args_t *args) /* operation arguments */
956{
957 xfs_inode_t *dp; /* incore directory inode */
958 int i; /* entry index */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 xfs_ino_t ino=0; /* entry old inode number */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 int i8elevated; /* sf_toino8 set i8count=1 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 xfs_dir2_sf_entry_t *sfep; /* shortform directory entry */
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200962 xfs_dir2_sf_hdr_t *sfp; /* shortform structure */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000964 trace_xfs_dir2_sf_replace(args);
965
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 dp = args->dp;
967
968 ASSERT(dp->i_df.if_flags & XFS_IFINLINE);
969 /*
970 * Bail out if the shortform directory is way too small.
971 */
972 if (dp->i_d.di_size < offsetof(xfs_dir2_sf_hdr_t, parent)) {
973 ASSERT(XFS_FORCED_SHUTDOWN(dp->i_mount));
Dave Chinner24513372014-06-25 14:58:08 +1000974 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 }
976 ASSERT(dp->i_df.if_bytes == dp->i_d.di_size);
977 ASSERT(dp->i_df.if_u1.if_data != NULL);
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200978 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
979 ASSERT(dp->i_d.di_size >= xfs_dir2_sf_hdr_size(sfp->i8count));
Christoph Hellwigd5cf09b2014-07-30 09:12:05 +1000980
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 /*
982 * New inode number is large, and need to convert to 8-byte inodes.
983 */
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200984 if (args->inumber > XFS_DIR2_MAX_SHORT_INUM && sfp->i8count == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 int error; /* error return value */
986 int newsize; /* new inode size */
987
Christoph Hellwig266b6962016-07-20 11:48:31 +1000988 newsize = dp->i_df.if_bytes + (sfp->count + 1) * XFS_INO64_DIFF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 /*
990 * Won't fit as shortform, convert to block then do replace.
991 */
992 if (newsize > XFS_IFORK_DSIZE(dp)) {
993 error = xfs_dir2_sf_to_block(args);
994 if (error) {
995 return error;
996 }
997 return xfs_dir2_block_replace(args);
998 }
999 /*
1000 * Still fits, convert to 8-byte now.
1001 */
1002 xfs_dir2_sf_toino8(args);
1003 i8elevated = 1;
Christoph Hellwigac8ba502011-07-08 14:35:13 +02001004 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 } else
1006 i8elevated = 0;
Christoph Hellwigd5cf09b2014-07-30 09:12:05 +10001007
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 ASSERT(args->namelen != 1 || args->name[0] != '.');
1009 /*
1010 * Replace ..'s entry.
1011 */
1012 if (args->namelen == 2 &&
1013 args->name[0] == '.' && args->name[1] == '.') {
Dave Chinner47401752013-10-29 22:11:47 +11001014 ino = dp->d_ops->sf_get_parent_ino(sfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 ASSERT(args->inumber != ino);
Dave Chinner47401752013-10-29 22:11:47 +11001016 dp->d_ops->sf_put_parent_ino(sfp, args->inumber);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 }
1018 /*
1019 * Normal entry, look for the name.
1020 */
1021 else {
Dave Chinner0cb97762013-08-12 20:50:09 +10001022 for (i = 0, sfep = xfs_dir2_sf_firstentry(sfp); i < sfp->count;
Dave Chinner32c54832013-10-29 22:11:46 +11001023 i++, sfep = dp->d_ops->sf_nextentry(sfp, sfep)) {
Barry Naujok5163f952008-05-21 16:41:01 +10001024 if (xfs_da_compname(args, sfep->name, sfep->namelen) ==
1025 XFS_CMP_EXACT) {
Dave Chinner47401752013-10-29 22:11:47 +11001026 ino = dp->d_ops->sf_get_ino(sfp, sfep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 ASSERT(args->inumber != ino);
Dave Chinner47401752013-10-29 22:11:47 +11001028 dp->d_ops->sf_put_ino(sfp, sfep, args->inumber);
1029 dp->d_ops->sf_put_ftype(sfep, args->filetype);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 break;
1031 }
1032 }
1033 /*
1034 * Didn't find it.
1035 */
Christoph Hellwigac8ba502011-07-08 14:35:13 +02001036 if (i == sfp->count) {
Barry Naujok6a178102008-05-21 16:42:05 +10001037 ASSERT(args->op_flags & XFS_DA_OP_OKNOENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 if (i8elevated)
1039 xfs_dir2_sf_toino4(args);
Dave Chinner24513372014-06-25 14:58:08 +10001040 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 }
1042 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 /*
1044 * See if the old number was large, the new number is small.
1045 */
1046 if (ino > XFS_DIR2_MAX_SHORT_INUM &&
1047 args->inumber <= XFS_DIR2_MAX_SHORT_INUM) {
1048 /*
1049 * And the old count was one, so need to convert to small.
1050 */
Christoph Hellwigac8ba502011-07-08 14:35:13 +02001051 if (sfp->i8count == 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 xfs_dir2_sf_toino4(args);
1053 else
Christoph Hellwigac8ba502011-07-08 14:35:13 +02001054 sfp->i8count--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 }
1056 /*
1057 * See if the old number was small, the new number is large.
1058 */
1059 if (ino <= XFS_DIR2_MAX_SHORT_INUM &&
1060 args->inumber > XFS_DIR2_MAX_SHORT_INUM) {
1061 /*
1062 * add to the i8count unless we just converted to 8-byte
1063 * inodes (which does an implied i8count = 1)
1064 */
Christoph Hellwigac8ba502011-07-08 14:35:13 +02001065 ASSERT(sfp->i8count != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 if (!i8elevated)
Christoph Hellwigac8ba502011-07-08 14:35:13 +02001067 sfp->i8count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 xfs_dir2_sf_check(args);
1070 xfs_trans_log_inode(args->trans, dp, XFS_ILOG_DDATA);
1071 return 0;
1072}
1073
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074/*
1075 * Convert from 8-byte inode numbers to 4-byte inode numbers.
1076 * The last 8-byte inode number is gone, but the count is still 1.
1077 */
1078static void
1079xfs_dir2_sf_toino4(
1080 xfs_da_args_t *args) /* operation arguments */
1081{
1082 char *buf; /* old dir's buffer */
1083 xfs_inode_t *dp; /* incore directory inode */
1084 int i; /* entry index */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 int newsize; /* new inode size */
1086 xfs_dir2_sf_entry_t *oldsfep; /* old sf entry */
Christoph Hellwigac8ba502011-07-08 14:35:13 +02001087 xfs_dir2_sf_hdr_t *oldsfp; /* old sf directory */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 int oldsize; /* old inode size */
1089 xfs_dir2_sf_entry_t *sfep; /* new sf entry */
Christoph Hellwigac8ba502011-07-08 14:35:13 +02001090 xfs_dir2_sf_hdr_t *sfp; /* new sf directory */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001092 trace_xfs_dir2_sf_toino4(args);
1093
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 dp = args->dp;
1095
1096 /*
1097 * Copy the old directory to the buffer.
1098 * Then nuke it from the inode, and add the new buffer to the inode.
1099 * Don't want xfs_idata_realloc copying the data here.
1100 */
1101 oldsize = dp->i_df.if_bytes;
1102 buf = kmem_alloc(oldsize, KM_SLEEP);
Christoph Hellwigac8ba502011-07-08 14:35:13 +02001103 oldsfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
1104 ASSERT(oldsfp->i8count == 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 memcpy(buf, oldsfp, oldsize);
1106 /*
1107 * Compute the new inode size.
1108 */
Christoph Hellwig266b6962016-07-20 11:48:31 +10001109 newsize = oldsize - (oldsfp->count + 1) * XFS_INO64_DIFF;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 xfs_idata_realloc(dp, -oldsize, XFS_DATA_FORK);
1111 xfs_idata_realloc(dp, newsize, XFS_DATA_FORK);
1112 /*
1113 * Reset our pointers, the data has moved.
1114 */
Christoph Hellwigac8ba502011-07-08 14:35:13 +02001115 oldsfp = (xfs_dir2_sf_hdr_t *)buf;
1116 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 /*
1118 * Fill in the new header.
1119 */
Christoph Hellwigac8ba502011-07-08 14:35:13 +02001120 sfp->count = oldsfp->count;
1121 sfp->i8count = 0;
Dave Chinner47401752013-10-29 22:11:47 +11001122 dp->d_ops->sf_put_parent_ino(sfp, dp->d_ops->sf_get_parent_ino(oldsfp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 /*
1124 * Copy the entries field by field.
1125 */
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001126 for (i = 0, sfep = xfs_dir2_sf_firstentry(sfp),
1127 oldsfep = xfs_dir2_sf_firstentry(oldsfp);
Christoph Hellwigac8ba502011-07-08 14:35:13 +02001128 i < sfp->count;
Dave Chinner32c54832013-10-29 22:11:46 +11001129 i++, sfep = dp->d_ops->sf_nextentry(sfp, sfep),
1130 oldsfep = dp->d_ops->sf_nextentry(oldsfp, oldsfep)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 sfep->namelen = oldsfep->namelen;
Christoph Hellwig8353a642016-07-20 11:47:21 +10001132 memcpy(sfep->offset, oldsfep->offset, sizeof(sfep->offset));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 memcpy(sfep->name, oldsfep->name, sfep->namelen);
Dave Chinner47401752013-10-29 22:11:47 +11001134 dp->d_ops->sf_put_ino(sfp, sfep,
1135 dp->d_ops->sf_get_ino(oldsfp, oldsfep));
1136 dp->d_ops->sf_put_ftype(sfep, dp->d_ops->sf_get_ftype(oldsfep));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137 }
1138 /*
1139 * Clean up the inode.
1140 */
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10001141 kmem_free(buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 dp->i_d.di_size = newsize;
1143 xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE | XFS_ILOG_DDATA);
1144}
1145
1146/*
Eric Sandeen5e06d142014-04-14 19:07:23 +10001147 * Convert existing entries from 4-byte inode numbers to 8-byte inode numbers.
1148 * The new entry w/ an 8-byte inode number is not there yet; we leave with
1149 * i8count set to 1, but no corresponding 8-byte entry.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 */
1151static void
1152xfs_dir2_sf_toino8(
1153 xfs_da_args_t *args) /* operation arguments */
1154{
1155 char *buf; /* old dir's buffer */
1156 xfs_inode_t *dp; /* incore directory inode */
1157 int i; /* entry index */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 int newsize; /* new inode size */
1159 xfs_dir2_sf_entry_t *oldsfep; /* old sf entry */
Christoph Hellwigac8ba502011-07-08 14:35:13 +02001160 xfs_dir2_sf_hdr_t *oldsfp; /* old sf directory */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 int oldsize; /* old inode size */
1162 xfs_dir2_sf_entry_t *sfep; /* new sf entry */
Christoph Hellwigac8ba502011-07-08 14:35:13 +02001163 xfs_dir2_sf_hdr_t *sfp; /* new sf directory */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001165 trace_xfs_dir2_sf_toino8(args);
1166
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 dp = args->dp;
1168
1169 /*
1170 * Copy the old directory to the buffer.
1171 * Then nuke it from the inode, and add the new buffer to the inode.
1172 * Don't want xfs_idata_realloc copying the data here.
1173 */
1174 oldsize = dp->i_df.if_bytes;
1175 buf = kmem_alloc(oldsize, KM_SLEEP);
Christoph Hellwigac8ba502011-07-08 14:35:13 +02001176 oldsfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
1177 ASSERT(oldsfp->i8count == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 memcpy(buf, oldsfp, oldsize);
1179 /*
Eric Sandeen5e06d142014-04-14 19:07:23 +10001180 * Compute the new inode size (nb: entry count + 1 for parent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 */
Christoph Hellwig266b6962016-07-20 11:48:31 +10001182 newsize = oldsize + (oldsfp->count + 1) * XFS_INO64_DIFF;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 xfs_idata_realloc(dp, -oldsize, XFS_DATA_FORK);
1184 xfs_idata_realloc(dp, newsize, XFS_DATA_FORK);
1185 /*
1186 * Reset our pointers, the data has moved.
1187 */
Christoph Hellwigac8ba502011-07-08 14:35:13 +02001188 oldsfp = (xfs_dir2_sf_hdr_t *)buf;
1189 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 /*
1191 * Fill in the new header.
1192 */
Christoph Hellwigac8ba502011-07-08 14:35:13 +02001193 sfp->count = oldsfp->count;
1194 sfp->i8count = 1;
Dave Chinner47401752013-10-29 22:11:47 +11001195 dp->d_ops->sf_put_parent_ino(sfp, dp->d_ops->sf_get_parent_ino(oldsfp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 /*
1197 * Copy the entries field by field.
1198 */
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001199 for (i = 0, sfep = xfs_dir2_sf_firstentry(sfp),
1200 oldsfep = xfs_dir2_sf_firstentry(oldsfp);
Christoph Hellwigac8ba502011-07-08 14:35:13 +02001201 i < sfp->count;
Dave Chinner32c54832013-10-29 22:11:46 +11001202 i++, sfep = dp->d_ops->sf_nextentry(sfp, sfep),
1203 oldsfep = dp->d_ops->sf_nextentry(oldsfp, oldsfep)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 sfep->namelen = oldsfep->namelen;
Christoph Hellwig8353a642016-07-20 11:47:21 +10001205 memcpy(sfep->offset, oldsfep->offset, sizeof(sfep->offset));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 memcpy(sfep->name, oldsfep->name, sfep->namelen);
Dave Chinner47401752013-10-29 22:11:47 +11001207 dp->d_ops->sf_put_ino(sfp, sfep,
1208 dp->d_ops->sf_get_ino(oldsfp, oldsfep));
1209 dp->d_ops->sf_put_ftype(sfep, dp->d_ops->sf_get_ftype(oldsfep));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210 }
1211 /*
1212 * Clean up the inode.
1213 */
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10001214 kmem_free(buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 dp->i_d.di_size = newsize;
1216 xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE | XFS_ILOG_DDATA);
1217}