blob: 0c75a7f00883ff4b9bb2808c8d7c9229b4616088 [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_mount.h"
Dave Chinner57062782013-10-15 09:17:51 +110024#include "xfs_da_format.h"
Nathan Scotta844f452005-11-02 14:38:42 +110025#include "xfs_da_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include "xfs_inode.h"
Dave Chinner239880e2013-10-23 10:50:10 +110027#include "xfs_trans.h"
Nathan Scotta844f452005-11-02 14:38:42 +110028#include "xfs_inode_item.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include "xfs_error.h"
Dave Chinner2b9ab5a2013-08-12 20:49:37 +100030#include "xfs_dir2.h"
Christoph Hellwig57926642011-07-13 13:43:48 +020031#include "xfs_dir2_priv.h"
Christoph Hellwig0b1b2132009-12-14 23:14:59 +000032#include "xfs_trace.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070033
34/*
35 * Prototypes for internal functions.
36 */
37static void xfs_dir2_sf_addname_easy(xfs_da_args_t *args,
38 xfs_dir2_sf_entry_t *sfep,
39 xfs_dir2_data_aoff_t offset,
40 int new_isize);
41static void xfs_dir2_sf_addname_hard(xfs_da_args_t *args, int objchange,
42 int new_isize);
43static int xfs_dir2_sf_addname_pick(xfs_da_args_t *args, int objchange,
44 xfs_dir2_sf_entry_t **sfepp,
45 xfs_dir2_data_aoff_t *offsetp);
46#ifdef DEBUG
47static void xfs_dir2_sf_check(xfs_da_args_t *args);
48#else
49#define xfs_dir2_sf_check(args)
50#endif /* DEBUG */
Christoph Hellwigd5cf09b2014-07-30 09:12:05 +100051
Linus Torvalds1da177e2005-04-16 15:20:36 -070052static void xfs_dir2_sf_toino4(xfs_da_args_t *args);
53static void xfs_dir2_sf_toino8(xfs_da_args_t *args);
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
Christoph Hellwig8bc38782011-07-08 14:35:03 +020055/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070056 * Given a block directory (dp/block), calculate its size as a shortform (sf)
57 * directory and a header for the sf directory, if it will fit it the
58 * space currently present in the inode. If it won't fit, the output
59 * size is too big (but not accurate).
60 */
61int /* size for sf form */
62xfs_dir2_block_sfsize(
63 xfs_inode_t *dp, /* incore inode pointer */
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +020064 xfs_dir2_data_hdr_t *hdr, /* block directory data */
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 xfs_dir2_sf_hdr_t *sfhp) /* output: header for sf form */
66{
67 xfs_dir2_dataptr_t addr; /* data entry address */
68 xfs_dir2_leaf_entry_t *blp; /* leaf area of the block */
69 xfs_dir2_block_tail_t *btp; /* tail area of the block */
70 int count; /* shortform entry count */
71 xfs_dir2_data_entry_t *dep; /* data entry in the block */
72 int i; /* block entry index */
73 int i8count; /* count of big-inode entries */
74 int isdot; /* entry is "." */
75 int isdotdot; /* entry is ".." */
76 xfs_mount_t *mp; /* mount structure pointer */
77 int namelen; /* total name bytes */
Christoph Hellwig5bde1ba92005-11-02 15:06:18 +110078 xfs_ino_t parent = 0; /* parent inode number */
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 int size=0; /* total computed size */
Dave Chinner0cb97762013-08-12 20:50:09 +100080 int has_ftype;
Dave Chinner8f661932014-06-06 15:15:59 +100081 struct xfs_da_geometry *geo;
Linus Torvalds1da177e2005-04-16 15:20:36 -070082
83 mp = dp->i_mount;
Dave Chinner8f661932014-06-06 15:15:59 +100084 geo = mp->m_dir_geo;
Linus Torvalds1da177e2005-04-16 15:20:36 -070085
Dave Chinner0cb97762013-08-12 20:50:09 +100086 /*
87 * if there is a filetype field, add the extra byte to the namelen
88 * for each entry that we see.
89 */
90 has_ftype = xfs_sb_version_hasftype(&mp->m_sb) ? 1 : 0;
91
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 count = i8count = namelen = 0;
Dave Chinner8f661932014-06-06 15:15:59 +100093 btp = xfs_dir2_block_tail_p(geo, hdr);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +100094 blp = xfs_dir2_block_leaf_p(btp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070095
96 /*
97 * Iterate over the block's data entries by using the leaf pointers.
98 */
Nathan Scotte922fff2006-03-17 17:27:56 +110099 for (i = 0; i < be32_to_cpu(btp->count); i++) {
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100100 if ((addr = be32_to_cpu(blp[i].address)) == XFS_DIR2_NULL_DATAPTR)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 continue;
102 /*
103 * Calculate the pointer to the entry at hand.
104 */
Dave Chinner30028032014-06-06 15:08:18 +1000105 dep = (xfs_dir2_data_entry_t *)((char *)hdr +
Dave Chinner8f661932014-06-06 15:15:59 +1000106 xfs_dir2_dataptr_to_off(geo, addr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 /*
108 * Detect . and .., so we can special-case them.
109 * . is not included in sf directories.
110 * .. is included by just the parent inode number.
111 */
112 isdot = dep->namelen == 1 && dep->name[0] == '.';
113 isdotdot =
114 dep->namelen == 2 &&
115 dep->name[0] == '.' && dep->name[1] == '.';
Christoph Hellwigd5cf09b2014-07-30 09:12:05 +1000116
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 if (!isdot)
Christoph Hellwigff9901c2006-06-09 14:48:37 +1000118 i8count += be64_to_cpu(dep->inumber) > XFS_DIR2_MAX_SHORT_INUM;
Christoph Hellwigd5cf09b2014-07-30 09:12:05 +1000119
Dave Chinner0cb97762013-08-12 20:50:09 +1000120 /* take into account the file type field */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 if (!isdot && !isdotdot) {
122 count++;
Dave Chinner0cb97762013-08-12 20:50:09 +1000123 namelen += dep->namelen + has_ftype;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 } else if (isdotdot)
Christoph Hellwigff9901c2006-06-09 14:48:37 +1000125 parent = be64_to_cpu(dep->inumber);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 /*
127 * Calculate the new size, see if we should give up yet.
128 */
Christoph Hellwig8353a642016-07-20 11:47:21 +1000129 size = xfs_dir2_sf_hdr_size(i8count) + /* header */
130 count * 3 * sizeof(u8) + /* namelen + offset */
131 namelen + /* name */
132 (i8count ? /* inumber */
Christoph Hellwig266b6962016-07-20 11:48:31 +1000133 count * XFS_INO64_SIZE :
134 count * XFS_INO32_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 if (size > XFS_IFORK_DSIZE(dp))
136 return size; /* size value is a failure */
137 }
138 /*
139 * Create the output header, if it worked.
140 */
141 sfhp->count = count;
142 sfhp->i8count = i8count;
Dave Chinner47401752013-10-29 22:11:47 +1100143 dp->d_ops->sf_put_parent_ino(sfhp, parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 return size;
145}
146
147/*
148 * Convert a block format directory to shortform.
149 * Caller has already checked that it will fit, and built us a header.
150 */
151int /* error */
152xfs_dir2_block_to_sf(
153 xfs_da_args_t *args, /* operation arguments */
Dave Chinner1d9025e2012-06-22 18:50:14 +1000154 struct xfs_buf *bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 int size, /* shortform directory size */
156 xfs_dir2_sf_hdr_t *sfhp) /* shortform directory hdr */
157{
Christoph Hellwiga64b0412011-07-08 14:35:32 +0200158 xfs_dir2_data_hdr_t *hdr; /* block header */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 xfs_dir2_data_entry_t *dep; /* data entry pointer */
160 xfs_inode_t *dp; /* incore directory inode */
161 xfs_dir2_data_unused_t *dup; /* unused data pointer */
162 char *endptr; /* end of data entries */
163 int error; /* error return value */
164 int logflags; /* inode logging flags */
165 xfs_mount_t *mp; /* filesystem mount point */
166 char *ptr; /* current data pointer */
167 xfs_dir2_sf_entry_t *sfep; /* shortform entry */
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200168 xfs_dir2_sf_hdr_t *sfp; /* shortform directory header */
Dave Chinnerb3f03ba2013-12-03 23:50:57 +1100169 xfs_dir2_sf_hdr_t *dst; /* temporary data buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000171 trace_xfs_dir2_block_to_sf(args);
172
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 dp = args->dp;
174 mp = dp->i_mount;
175
176 /*
Dave Chinnerb3f03ba2013-12-03 23:50:57 +1100177 * allocate a temporary destination buffer the size of the inode
178 * to format the data into. Once we have formatted the data, we
179 * can free the block and copy the formatted data into the inode literal
180 * area.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 */
Dave Chinnerb3f03ba2013-12-03 23:50:57 +1100182 dst = kmem_alloc(mp->m_sb.sb_inodesize, KM_SLEEP);
183 hdr = bp->b_addr;
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +0200184
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 * Copy the header into the newly allocate local space.
187 */
Dave Chinnerb3f03ba2013-12-03 23:50:57 +1100188 sfp = (xfs_dir2_sf_hdr_t *)dst;
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000189 memcpy(sfp, sfhp, xfs_dir2_sf_hdr_size(sfhp->i8count));
Dave Chinnerb3f03ba2013-12-03 23:50:57 +1100190
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 /*
192 * Set up to loop over the block's entries.
193 */
Dave Chinner2ca98772013-10-29 22:11:49 +1100194 ptr = (char *)dp->d_ops->data_entry_p(hdr);
Darrick J. Wongce92d292018-01-16 18:54:12 -0800195 endptr = xfs_dir3_data_endp(args->geo, hdr);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000196 sfep = xfs_dir2_sf_firstentry(sfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 /*
198 * Loop over the active and unused entries.
199 * Stop when we reach the leaf/tail portion of the block.
200 */
201 while (ptr < endptr) {
202 /*
203 * If it's unused, just skip over it.
204 */
205 dup = (xfs_dir2_data_unused_t *)ptr;
Nathan Scottad354eb2006-03-17 17:27:37 +1100206 if (be16_to_cpu(dup->freetag) == XFS_DIR2_DATA_FREE_TAG) {
207 ptr += be16_to_cpu(dup->length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 continue;
209 }
210 dep = (xfs_dir2_data_entry_t *)ptr;
211 /*
212 * Skip .
213 */
214 if (dep->namelen == 1 && dep->name[0] == '.')
Christoph Hellwigff9901c2006-06-09 14:48:37 +1000215 ASSERT(be64_to_cpu(dep->inumber) == dp->i_ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 /*
217 * Skip .., but make sure the inode number is right.
218 */
219 else if (dep->namelen == 2 &&
220 dep->name[0] == '.' && dep->name[1] == '.')
Christoph Hellwigff9901c2006-06-09 14:48:37 +1000221 ASSERT(be64_to_cpu(dep->inumber) ==
Dave Chinner47401752013-10-29 22:11:47 +1100222 dp->d_ops->sf_get_parent_ino(sfp));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 /*
224 * Normal entry, copy it into shortform.
225 */
226 else {
227 sfep->namelen = dep->namelen;
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000228 xfs_dir2_sf_put_offset(sfep,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 (xfs_dir2_data_aoff_t)
Christoph Hellwiga64b0412011-07-08 14:35:32 +0200230 ((char *)dep - (char *)hdr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 memcpy(sfep->name, dep->name, dep->namelen);
Dave Chinner47401752013-10-29 22:11:47 +1100232 dp->d_ops->sf_put_ino(sfp, sfep,
233 be64_to_cpu(dep->inumber));
234 dp->d_ops->sf_put_ftype(sfep,
Dave Chinner9d23fc82013-10-29 22:11:48 +1100235 dp->d_ops->data_get_ftype(dep));
Christoph Hellwig8bc38782011-07-08 14:35:03 +0200236
Dave Chinner32c54832013-10-29 22:11:46 +1100237 sfep = dp->d_ops->sf_nextentry(sfp, sfep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 }
Dave Chinner9d23fc82013-10-29 22:11:48 +1100239 ptr += dp->d_ops->data_entsize(dep->namelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 }
241 ASSERT((char *)sfep - (char *)sfp == size);
Dave Chinnerb3f03ba2013-12-03 23:50:57 +1100242
243 /* now we are done with the block, we can shrink the inode */
244 logflags = XFS_ILOG_CORE;
Dave Chinner7dda6e82014-06-06 15:11:18 +1000245 error = xfs_dir2_shrink_inode(args, args->geo->datablk, bp);
Dave Chinnerb3f03ba2013-12-03 23:50:57 +1100246 if (error) {
Dave Chinner24513372014-06-25 14:58:08 +1000247 ASSERT(error != -ENOSPC);
Dave Chinnerb3f03ba2013-12-03 23:50:57 +1100248 goto out;
249 }
250
251 /*
252 * The buffer is now unconditionally gone, whether
253 * xfs_dir2_shrink_inode worked or not.
254 *
255 * Convert the inode to local format and copy the data in.
256 */
Dave Chinnerb3f03ba2013-12-03 23:50:57 +1100257 ASSERT(dp->i_df.if_bytes == 0);
Christoph Hellwig143f4ae2016-04-06 07:41:43 +1000258 xfs_init_local_fork(dp, XFS_DATA_FORK, dst, size);
259 dp->i_d.di_format = XFS_DINODE_FMT_LOCAL;
260 dp->i_d.di_size = size;
Dave Chinnerb3f03ba2013-12-03 23:50:57 +1100261
262 logflags |= XFS_ILOG_DDATA;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 xfs_dir2_sf_check(args);
264out:
265 xfs_trans_log_inode(args->trans, dp, logflags);
Dave Chinnerb3f03ba2013-12-03 23:50:57 +1100266 kmem_free(dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 return error;
268}
269
270/*
271 * Add a name to a shortform directory.
272 * There are two algorithms, "easy" and "hard" which we decide on
273 * before changing anything.
274 * Convert to block form if necessary, if the new entry won't fit.
275 */
276int /* error */
277xfs_dir2_sf_addname(
278 xfs_da_args_t *args) /* operation arguments */
279{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 xfs_inode_t *dp; /* incore directory inode */
281 int error; /* error return value */
282 int incr_isize; /* total change in size */
283 int new_isize; /* di_size after adding name */
284 int objchange; /* changing to 8-byte inodes */
Christoph Hellwig5bde1ba92005-11-02 15:06:18 +1100285 xfs_dir2_data_aoff_t offset = 0; /* offset for new entry */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 int pick; /* which algorithm to use */
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200287 xfs_dir2_sf_hdr_t *sfp; /* shortform structure */
Christoph Hellwig5bde1ba92005-11-02 15:06:18 +1100288 xfs_dir2_sf_entry_t *sfep = NULL; /* shortform entry */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000290 trace_xfs_dir2_sf_addname(args);
291
Dave Chinner24513372014-06-25 14:58:08 +1000292 ASSERT(xfs_dir2_sf_lookup(args) == -ENOENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 dp = args->dp;
294 ASSERT(dp->i_df.if_flags & XFS_IFINLINE);
295 /*
296 * Make sure the shortform value has some of its header.
297 */
298 if (dp->i_d.di_size < offsetof(xfs_dir2_sf_hdr_t, parent)) {
299 ASSERT(XFS_FORCED_SHUTDOWN(dp->i_mount));
Dave Chinner24513372014-06-25 14:58:08 +1000300 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 }
302 ASSERT(dp->i_df.if_bytes == dp->i_d.di_size);
303 ASSERT(dp->i_df.if_u1.if_data != NULL);
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200304 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
305 ASSERT(dp->i_d.di_size >= xfs_dir2_sf_hdr_size(sfp->i8count));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 /*
307 * Compute entry (and change in) size.
308 */
Eric Sandeen5e06d142014-04-14 19:07:23 +1000309 incr_isize = dp->d_ops->sf_entsize(sfp, args->namelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 objchange = 0;
Christoph Hellwigd5cf09b2014-07-30 09:12:05 +1000311
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 /*
313 * Do we have to change to 8 byte inodes?
314 */
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200315 if (args->inumber > XFS_DIR2_MAX_SHORT_INUM && sfp->i8count == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 /*
Eric Sandeen5e06d142014-04-14 19:07:23 +1000317 * Yes, adjust the inode size. old count + (parent + new)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 */
Christoph Hellwig266b6962016-07-20 11:48:31 +1000319 incr_isize += (sfp->count + 2) * XFS_INO64_DIFF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 objchange = 1;
321 }
Christoph Hellwigd5cf09b2014-07-30 09:12:05 +1000322
Eric Sandeen5e06d142014-04-14 19:07:23 +1000323 new_isize = (int)dp->i_d.di_size + incr_isize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 /*
325 * Won't fit as shortform any more (due to size),
326 * or the pick routine says it won't (due to offset values).
327 */
328 if (new_isize > XFS_IFORK_DSIZE(dp) ||
329 (pick =
330 xfs_dir2_sf_addname_pick(args, objchange, &sfep, &offset)) == 0) {
331 /*
332 * Just checking or no space reservation, it doesn't fit.
333 */
Barry Naujok6a178102008-05-21 16:42:05 +1000334 if ((args->op_flags & XFS_DA_OP_JUSTCHECK) || args->total == 0)
Dave Chinner24513372014-06-25 14:58:08 +1000335 return -ENOSPC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 /*
337 * Convert to block form then add the name.
338 */
339 error = xfs_dir2_sf_to_block(args);
340 if (error)
341 return error;
342 return xfs_dir2_block_addname(args);
343 }
344 /*
345 * Just checking, it fits.
346 */
Barry Naujok6a178102008-05-21 16:42:05 +1000347 if (args->op_flags & XFS_DA_OP_JUSTCHECK)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 return 0;
349 /*
350 * Do it the easy way - just add it at the end.
351 */
352 if (pick == 1)
353 xfs_dir2_sf_addname_easy(args, sfep, offset, new_isize);
354 /*
355 * Do it the hard way - look for a place to insert the new entry.
356 * Convert to 8 byte inode numbers first if necessary.
357 */
358 else {
359 ASSERT(pick == 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 if (objchange)
361 xfs_dir2_sf_toino8(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 xfs_dir2_sf_addname_hard(args, objchange, new_isize);
363 }
364 xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE | XFS_ILOG_DDATA);
365 return 0;
366}
367
368/*
369 * Add the new entry the "easy" way.
370 * This is copying the old directory and adding the new entry at the end.
371 * Since it's sorted by "offset" we need room after the last offset
372 * that's already there, and then room to convert to a block directory.
373 * This is already checked by the pick routine.
374 */
375static void
376xfs_dir2_sf_addname_easy(
377 xfs_da_args_t *args, /* operation arguments */
378 xfs_dir2_sf_entry_t *sfep, /* pointer to new entry */
379 xfs_dir2_data_aoff_t offset, /* offset to use for new ent */
380 int new_isize) /* new directory size */
381{
382 int byteoff; /* byte offset in sf dir */
383 xfs_inode_t *dp; /* incore directory inode */
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200384 xfs_dir2_sf_hdr_t *sfp; /* shortform structure */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385
386 dp = args->dp;
387
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200388 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 byteoff = (int)((char *)sfep - (char *)sfp);
390 /*
391 * Grow the in-inode space.
392 */
Dave Chinner32c54832013-10-29 22:11:46 +1100393 xfs_idata_realloc(dp, dp->d_ops->sf_entsize(sfp, args->namelen),
Dave Chinner0cb97762013-08-12 20:50:09 +1000394 XFS_DATA_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 /*
396 * Need to set up again due to realloc of the inode data.
397 */
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200398 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 sfep = (xfs_dir2_sf_entry_t *)((char *)sfp + byteoff);
400 /*
401 * Fill in the new entry.
402 */
403 sfep->namelen = args->namelen;
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000404 xfs_dir2_sf_put_offset(sfep, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 memcpy(sfep->name, args->name, sfep->namelen);
Dave Chinner47401752013-10-29 22:11:47 +1100406 dp->d_ops->sf_put_ino(sfp, sfep, args->inumber);
407 dp->d_ops->sf_put_ftype(sfep, args->filetype);
Dave Chinner1c55cec2013-08-12 20:50:10 +1000408
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 /*
410 * Update the header and inode.
411 */
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200412 sfp->count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 if (args->inumber > XFS_DIR2_MAX_SHORT_INUM)
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200414 sfp->i8count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 dp->i_d.di_size = new_isize;
416 xfs_dir2_sf_check(args);
417}
418
419/*
420 * Add the new entry the "hard" way.
421 * The caller has already converted to 8 byte inode numbers if necessary,
422 * in which case we need to leave the i8count at 1.
423 * Find a hole that the new entry will fit into, and copy
424 * the first part of the entries, the new entry, and the last part of
425 * the entries.
426 */
427/* ARGSUSED */
428static void
429xfs_dir2_sf_addname_hard(
430 xfs_da_args_t *args, /* operation arguments */
431 int objchange, /* changing inode number size */
432 int new_isize) /* new directory size */
433{
434 int add_datasize; /* data size need for new ent */
435 char *buf; /* buffer for old */
436 xfs_inode_t *dp; /* incore directory inode */
437 int eof; /* reached end of old dir */
438 int nbytes; /* temp for byte copies */
439 xfs_dir2_data_aoff_t new_offset; /* next offset value */
440 xfs_dir2_data_aoff_t offset; /* current offset value */
441 int old_isize; /* previous di_size */
442 xfs_dir2_sf_entry_t *oldsfep; /* entry in original dir */
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200443 xfs_dir2_sf_hdr_t *oldsfp; /* original shortform dir */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 xfs_dir2_sf_entry_t *sfep; /* entry in new dir */
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200445 xfs_dir2_sf_hdr_t *sfp; /* new shortform dir */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446
447 /*
448 * Copy the old directory to the stack buffer.
449 */
450 dp = args->dp;
451
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200452 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 old_isize = (int)dp->i_d.di_size;
454 buf = kmem_alloc(old_isize, KM_SLEEP);
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200455 oldsfp = (xfs_dir2_sf_hdr_t *)buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 memcpy(oldsfp, sfp, old_isize);
457 /*
458 * Loop over the old directory finding the place we're going
459 * to insert the new entry.
460 * If it's going to end up at the end then oldsfep will point there.
461 */
Dave Chinner1c9a5b22013-10-30 09:15:02 +1100462 for (offset = dp->d_ops->data_first_offset,
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000463 oldsfep = xfs_dir2_sf_firstentry(oldsfp),
Dave Chinner9d23fc82013-10-29 22:11:48 +1100464 add_datasize = dp->d_ops->data_entsize(args->namelen),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 eof = (char *)oldsfep == &buf[old_isize];
466 !eof;
Dave Chinner9d23fc82013-10-29 22:11:48 +1100467 offset = new_offset + dp->d_ops->data_entsize(oldsfep->namelen),
Dave Chinner32c54832013-10-29 22:11:46 +1100468 oldsfep = dp->d_ops->sf_nextentry(oldsfp, oldsfep),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 eof = (char *)oldsfep == &buf[old_isize]) {
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000470 new_offset = xfs_dir2_sf_get_offset(oldsfep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 if (offset + add_datasize <= new_offset)
472 break;
473 }
474 /*
475 * Get rid of the old directory, then allocate space for
476 * the new one. We do this so xfs_idata_realloc won't copy
477 * the data.
478 */
479 xfs_idata_realloc(dp, -old_isize, XFS_DATA_FORK);
480 xfs_idata_realloc(dp, new_isize, XFS_DATA_FORK);
481 /*
482 * Reset the pointer since the buffer was reallocated.
483 */
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200484 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 /*
486 * Copy the first part of the directory, including the header.
487 */
488 nbytes = (int)((char *)oldsfep - (char *)oldsfp);
489 memcpy(sfp, oldsfp, nbytes);
490 sfep = (xfs_dir2_sf_entry_t *)((char *)sfp + nbytes);
491 /*
492 * Fill in the new entry, and update the header counts.
493 */
494 sfep->namelen = args->namelen;
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000495 xfs_dir2_sf_put_offset(sfep, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 memcpy(sfep->name, args->name, sfep->namelen);
Dave Chinner47401752013-10-29 22:11:47 +1100497 dp->d_ops->sf_put_ino(sfp, sfep, args->inumber);
498 dp->d_ops->sf_put_ftype(sfep, args->filetype);
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200499 sfp->count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 if (args->inumber > XFS_DIR2_MAX_SHORT_INUM && !objchange)
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200501 sfp->i8count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 /*
503 * If there's more left to copy, do that.
504 */
505 if (!eof) {
Dave Chinner32c54832013-10-29 22:11:46 +1100506 sfep = dp->d_ops->sf_nextentry(sfp, sfep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 memcpy(sfep, oldsfep, old_isize - nbytes);
508 }
Denys Vlasenkof0e2d932008-05-19 16:31:57 +1000509 kmem_free(buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 dp->i_d.di_size = new_isize;
511 xfs_dir2_sf_check(args);
512}
513
514/*
515 * Decide if the new entry will fit at all.
516 * If it will fit, pick between adding the new entry to the end (easy)
517 * or somewhere else (hard).
518 * Return 0 (won't fit), 1 (easy), 2 (hard).
519 */
520/*ARGSUSED*/
521static int /* pick result */
522xfs_dir2_sf_addname_pick(
523 xfs_da_args_t *args, /* operation arguments */
524 int objchange, /* inode # size changes */
525 xfs_dir2_sf_entry_t **sfepp, /* out(1): new entry ptr */
526 xfs_dir2_data_aoff_t *offsetp) /* out(1): new offset */
527{
528 xfs_inode_t *dp; /* incore directory inode */
529 int holefit; /* found hole it will fit in */
530 int i; /* entry number */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 xfs_dir2_data_aoff_t offset; /* data block offset */
532 xfs_dir2_sf_entry_t *sfep; /* shortform entry */
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200533 xfs_dir2_sf_hdr_t *sfp; /* shortform structure */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 int size; /* entry's data size */
535 int used; /* data bytes used */
536
537 dp = args->dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200539 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
Dave Chinner9d23fc82013-10-29 22:11:48 +1100540 size = dp->d_ops->data_entsize(args->namelen);
Dave Chinner1c9a5b22013-10-30 09:15:02 +1100541 offset = dp->d_ops->data_first_offset;
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000542 sfep = xfs_dir2_sf_firstentry(sfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 holefit = 0;
544 /*
545 * Loop over sf entries.
546 * Keep track of data offset and whether we've seen a place
547 * to insert the new entry.
548 */
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200549 for (i = 0; i < sfp->count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 if (!holefit)
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000551 holefit = offset + size <= xfs_dir2_sf_get_offset(sfep);
552 offset = xfs_dir2_sf_get_offset(sfep) +
Dave Chinner9d23fc82013-10-29 22:11:48 +1100553 dp->d_ops->data_entsize(sfep->namelen);
Dave Chinner32c54832013-10-29 22:11:46 +1100554 sfep = dp->d_ops->sf_nextentry(sfp, sfep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 }
556 /*
557 * Calculate data bytes used excluding the new entry, if this
558 * was a data block (block form directory).
559 */
560 used = offset +
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200561 (sfp->count + 3) * (uint)sizeof(xfs_dir2_leaf_entry_t) +
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 (uint)sizeof(xfs_dir2_block_tail_t);
563 /*
564 * If it won't fit in a block form then we can't insert it,
565 * we'll go back, convert to block, then try the insert and convert
566 * to leaf.
567 */
Dave Chinner8f661932014-06-06 15:15:59 +1000568 if (used + (holefit ? 0 : size) > args->geo->blksize)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 return 0;
570 /*
571 * If changing the inode number size, do it the hard way.
572 */
Christoph Hellwigd5cf09b2014-07-30 09:12:05 +1000573 if (objchange)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 return 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 /*
576 * If it won't fit at the end then do it the hard way (use the hole).
577 */
Dave Chinner8f661932014-06-06 15:15:59 +1000578 if (used + size > args->geo->blksize)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 return 2;
580 /*
581 * Do it the easy way.
582 */
583 *sfepp = sfep;
584 *offsetp = offset;
585 return 1;
586}
587
588#ifdef DEBUG
589/*
590 * Check consistency of shortform directory, assert if bad.
591 */
592static void
593xfs_dir2_sf_check(
594 xfs_da_args_t *args) /* operation arguments */
595{
596 xfs_inode_t *dp; /* incore directory inode */
597 int i; /* entry number */
598 int i8count; /* number of big inode#s */
599 xfs_ino_t ino; /* entry inode number */
600 int offset; /* data offset */
601 xfs_dir2_sf_entry_t *sfep; /* shortform dir entry */
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200602 xfs_dir2_sf_hdr_t *sfp; /* shortform structure */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603
604 dp = args->dp;
605
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200606 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
Dave Chinner1c9a5b22013-10-30 09:15:02 +1100607 offset = dp->d_ops->data_first_offset;
Dave Chinner47401752013-10-29 22:11:47 +1100608 ino = dp->d_ops->sf_get_parent_ino(sfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 i8count = ino > XFS_DIR2_MAX_SHORT_INUM;
610
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000611 for (i = 0, sfep = xfs_dir2_sf_firstentry(sfp);
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200612 i < sfp->count;
Dave Chinner32c54832013-10-29 22:11:46 +1100613 i++, sfep = dp->d_ops->sf_nextentry(sfp, sfep)) {
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000614 ASSERT(xfs_dir2_sf_get_offset(sfep) >= offset);
Dave Chinner47401752013-10-29 22:11:47 +1100615 ino = dp->d_ops->sf_get_ino(sfp, sfep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 i8count += ino > XFS_DIR2_MAX_SHORT_INUM;
617 offset =
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000618 xfs_dir2_sf_get_offset(sfep) +
Dave Chinner9d23fc82013-10-29 22:11:48 +1100619 dp->d_ops->data_entsize(sfep->namelen);
Dave Chinner47401752013-10-29 22:11:47 +1100620 ASSERT(dp->d_ops->sf_get_ftype(sfep) < XFS_DIR3_FT_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 }
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200622 ASSERT(i8count == sfp->i8count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 ASSERT((char *)sfep - (char *)sfp == dp->i_d.di_size);
624 ASSERT(offset +
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200625 (sfp->count + 2) * (uint)sizeof(xfs_dir2_leaf_entry_t) +
Dave Chinner8f661932014-06-06 15:15:59 +1000626 (uint)sizeof(xfs_dir2_block_tail_t) <= args->geo->blksize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627}
628#endif /* DEBUG */
629
Darrick J. Wong630a04e2017-03-15 00:24:25 -0700630/* Verify the consistency of an inline directory. */
Darrick J. Wongdc042c22018-01-08 10:51:06 -0800631xfs_failaddr_t
Darrick J. Wong630a04e2017-03-15 00:24:25 -0700632xfs_dir2_sf_verify(
Darrick J. Wong78420282017-04-03 12:22:20 -0700633 struct xfs_inode *ip)
Darrick J. Wong630a04e2017-03-15 00:24:25 -0700634{
Darrick J. Wong78420282017-04-03 12:22:20 -0700635 struct xfs_mount *mp = ip->i_mount;
636 struct xfs_dir2_sf_hdr *sfp;
Darrick J. Wong630a04e2017-03-15 00:24:25 -0700637 struct xfs_dir2_sf_entry *sfep;
638 struct xfs_dir2_sf_entry *next_sfep;
639 char *endp;
640 const struct xfs_dir_ops *dops;
Darrick J. Wong78420282017-04-03 12:22:20 -0700641 struct xfs_ifork *ifp;
Darrick J. Wong630a04e2017-03-15 00:24:25 -0700642 xfs_ino_t ino;
643 int i;
644 int i8count;
645 int offset;
Darrick J. Wong78420282017-04-03 12:22:20 -0700646 int size;
647 int error;
Darrick J. Wongc8ce5402017-06-16 11:00:05 -0700648 uint8_t filetype;
Darrick J. Wong630a04e2017-03-15 00:24:25 -0700649
Darrick J. Wong78420282017-04-03 12:22:20 -0700650 ASSERT(ip->i_d.di_format == XFS_DINODE_FMT_LOCAL);
651 /*
652 * xfs_iread calls us before xfs_setup_inode sets up ip->d_ops,
653 * so we can only trust the mountpoint to have the right pointer.
654 */
Darrick J. Wong630a04e2017-03-15 00:24:25 -0700655 dops = xfs_dir_get_ops(mp, NULL);
656
Darrick J. Wong78420282017-04-03 12:22:20 -0700657 ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
658 sfp = (struct xfs_dir2_sf_hdr *)ifp->if_u1.if_data;
659 size = ifp->if_bytes;
660
Darrick J. Wong630a04e2017-03-15 00:24:25 -0700661 /*
662 * Give up if the directory is way too short.
663 */
Darrick J. Wong78420282017-04-03 12:22:20 -0700664 if (size <= offsetof(struct xfs_dir2_sf_hdr, parent) ||
665 size < xfs_dir2_sf_hdr_size(sfp->i8count))
Darrick J. Wongdc042c22018-01-08 10:51:06 -0800666 return __this_address;
Darrick J. Wong630a04e2017-03-15 00:24:25 -0700667
668 endp = (char *)sfp + size;
669
670 /* Check .. entry */
671 ino = dops->sf_get_parent_ino(sfp);
672 i8count = ino > XFS_DIR2_MAX_SHORT_INUM;
Darrick J. Wong78420282017-04-03 12:22:20 -0700673 error = xfs_dir_ino_validate(mp, ino);
674 if (error)
Darrick J. Wongdc042c22018-01-08 10:51:06 -0800675 return __this_address;
Darrick J. Wong630a04e2017-03-15 00:24:25 -0700676 offset = dops->data_first_offset;
677
678 /* Check all reported entries */
679 sfep = xfs_dir2_sf_firstentry(sfp);
680 for (i = 0; i < sfp->count; i++) {
681 /*
682 * struct xfs_dir2_sf_entry has a variable length.
683 * Check the fixed-offset parts of the structure are
684 * within the data buffer.
685 */
Darrick J. Wong78420282017-04-03 12:22:20 -0700686 if (((char *)sfep + sizeof(*sfep)) >= endp)
Darrick J. Wongdc042c22018-01-08 10:51:06 -0800687 return __this_address;
Darrick J. Wong630a04e2017-03-15 00:24:25 -0700688
689 /* Don't allow names with known bad length. */
Darrick J. Wong78420282017-04-03 12:22:20 -0700690 if (sfep->namelen == 0)
Darrick J. Wongdc042c22018-01-08 10:51:06 -0800691 return __this_address;
Darrick J. Wong630a04e2017-03-15 00:24:25 -0700692
693 /*
694 * Check that the variable-length part of the structure is
695 * within the data buffer. The next entry starts after the
696 * name component, so nextentry is an acceptable test.
697 */
698 next_sfep = dops->sf_nextentry(sfp, sfep);
Darrick J. Wong78420282017-04-03 12:22:20 -0700699 if (endp < (char *)next_sfep)
Darrick J. Wongdc042c22018-01-08 10:51:06 -0800700 return __this_address;
Darrick J. Wong630a04e2017-03-15 00:24:25 -0700701
702 /* Check that the offsets always increase. */
Darrick J. Wong78420282017-04-03 12:22:20 -0700703 if (xfs_dir2_sf_get_offset(sfep) < offset)
Darrick J. Wongdc042c22018-01-08 10:51:06 -0800704 return __this_address;
Darrick J. Wong630a04e2017-03-15 00:24:25 -0700705
706 /* Check the inode number. */
707 ino = dops->sf_get_ino(sfp, sfep);
708 i8count += ino > XFS_DIR2_MAX_SHORT_INUM;
Darrick J. Wong78420282017-04-03 12:22:20 -0700709 error = xfs_dir_ino_validate(mp, ino);
710 if (error)
Darrick J. Wongdc042c22018-01-08 10:51:06 -0800711 return __this_address;
Darrick J. Wong630a04e2017-03-15 00:24:25 -0700712
713 /* Check the file type. */
714 filetype = dops->sf_get_ftype(sfep);
Darrick J. Wong78420282017-04-03 12:22:20 -0700715 if (filetype >= XFS_DIR3_FT_MAX)
Darrick J. Wongdc042c22018-01-08 10:51:06 -0800716 return __this_address;
Darrick J. Wong630a04e2017-03-15 00:24:25 -0700717
718 offset = xfs_dir2_sf_get_offset(sfep) +
719 dops->data_entsize(sfep->namelen);
720
721 sfep = next_sfep;
722 }
Darrick J. Wong78420282017-04-03 12:22:20 -0700723 if (i8count != sfp->i8count)
Darrick J. Wongdc042c22018-01-08 10:51:06 -0800724 return __this_address;
Darrick J. Wong78420282017-04-03 12:22:20 -0700725 if ((void *)sfep != (void *)endp)
Darrick J. Wongdc042c22018-01-08 10:51:06 -0800726 return __this_address;
Darrick J. Wong630a04e2017-03-15 00:24:25 -0700727
728 /* Make sure this whole thing ought to be in local format. */
Darrick J. Wong78420282017-04-03 12:22:20 -0700729 if (offset + (sfp->count + 2) * (uint)sizeof(xfs_dir2_leaf_entry_t) +
730 (uint)sizeof(xfs_dir2_block_tail_t) > mp->m_dir_geo->blksize)
Darrick J. Wongdc042c22018-01-08 10:51:06 -0800731 return __this_address;
Darrick J. Wong630a04e2017-03-15 00:24:25 -0700732
Darrick J. Wongdc042c22018-01-08 10:51:06 -0800733 return NULL;
Darrick J. Wong630a04e2017-03-15 00:24:25 -0700734}
735
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736/*
737 * Create a new (shortform) directory.
738 */
739int /* error, always 0 */
740xfs_dir2_sf_create(
741 xfs_da_args_t *args, /* operation arguments */
742 xfs_ino_t pino) /* parent inode number */
743{
744 xfs_inode_t *dp; /* incore directory inode */
745 int i8count; /* parent inode is an 8-byte number */
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200746 xfs_dir2_sf_hdr_t *sfp; /* shortform structure */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 int size; /* directory size */
748
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000749 trace_xfs_dir2_sf_create(args);
750
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 dp = args->dp;
752
753 ASSERT(dp != NULL);
754 ASSERT(dp->i_d.di_size == 0);
755 /*
756 * If it's currently a zero-length extent file,
757 * convert it to local format.
758 */
759 if (dp->i_d.di_format == XFS_DINODE_FMT_EXTENTS) {
760 dp->i_df.if_flags &= ~XFS_IFEXTENTS; /* just in case */
761 dp->i_d.di_format = XFS_DINODE_FMT_LOCAL;
762 xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE);
763 dp->i_df.if_flags |= XFS_IFINLINE;
764 }
765 ASSERT(dp->i_df.if_flags & XFS_IFINLINE);
766 ASSERT(dp->i_df.if_bytes == 0);
767 i8count = pino > XFS_DIR2_MAX_SHORT_INUM;
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000768 size = xfs_dir2_sf_hdr_size(i8count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 /*
770 * Make a buffer for the data.
771 */
772 xfs_idata_realloc(dp, size, XFS_DATA_FORK);
773 /*
774 * Fill in the header,
775 */
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200776 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
777 sfp->i8count = i8count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 /*
779 * Now can put in the inode number, since i8count is set.
780 */
Dave Chinner47401752013-10-29 22:11:47 +1100781 dp->d_ops->sf_put_parent_ino(sfp, pino);
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200782 sfp->count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 dp->i_d.di_size = size;
784 xfs_dir2_sf_check(args);
785 xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE | XFS_ILOG_DDATA);
786 return 0;
787}
788
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789/*
790 * Lookup an entry in a shortform directory.
791 * Returns EEXIST if found, ENOENT if not found.
792 */
793int /* error */
794xfs_dir2_sf_lookup(
795 xfs_da_args_t *args) /* operation arguments */
796{
797 xfs_inode_t *dp; /* incore directory inode */
798 int i; /* entry index */
Barry Naujok384f3ce2008-05-21 16:58:22 +1000799 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 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 */
Barry Naujok5163f952008-05-21 16:41:01 +1000802 enum xfs_dacmp cmp; /* comparison result */
Barry Naujok384f3ce2008-05-21 16:58:22 +1000803 xfs_dir2_sf_entry_t *ci_sfep; /* case-insens. entry */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000805 trace_xfs_dir2_sf_lookup(args);
806
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 xfs_dir2_sf_check(args);
808 dp = args->dp;
809
810 ASSERT(dp->i_df.if_flags & XFS_IFINLINE);
811 /*
812 * Bail out if the directory is way too short.
813 */
814 if (dp->i_d.di_size < offsetof(xfs_dir2_sf_hdr_t, parent)) {
815 ASSERT(XFS_FORCED_SHUTDOWN(dp->i_mount));
Dave Chinner24513372014-06-25 14:58:08 +1000816 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 }
818 ASSERT(dp->i_df.if_bytes == dp->i_d.di_size);
819 ASSERT(dp->i_df.if_u1.if_data != NULL);
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200820 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
821 ASSERT(dp->i_d.di_size >= xfs_dir2_sf_hdr_size(sfp->i8count));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 /*
823 * Special case for .
824 */
825 if (args->namelen == 1 && args->name[0] == '.') {
826 args->inumber = dp->i_ino;
Barry Naujok5163f952008-05-21 16:41:01 +1000827 args->cmpresult = XFS_CMP_EXACT;
Dave Chinner1c55cec2013-08-12 20:50:10 +1000828 args->filetype = XFS_DIR3_FT_DIR;
Dave Chinner24513372014-06-25 14:58:08 +1000829 return -EEXIST;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 }
831 /*
832 * Special case for ..
833 */
834 if (args->namelen == 2 &&
835 args->name[0] == '.' && args->name[1] == '.') {
Dave Chinner47401752013-10-29 22:11:47 +1100836 args->inumber = dp->d_ops->sf_get_parent_ino(sfp);
Barry Naujok5163f952008-05-21 16:41:01 +1000837 args->cmpresult = XFS_CMP_EXACT;
Dave Chinner1c55cec2013-08-12 20:50:10 +1000838 args->filetype = XFS_DIR3_FT_DIR;
Dave Chinner24513372014-06-25 14:58:08 +1000839 return -EEXIST;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 }
841 /*
842 * Loop over all the entries trying to match ours.
843 */
Barry Naujok384f3ce2008-05-21 16:58:22 +1000844 ci_sfep = NULL;
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200845 for (i = 0, sfep = xfs_dir2_sf_firstentry(sfp); i < sfp->count;
Dave Chinner32c54832013-10-29 22:11:46 +1100846 i++, sfep = dp->d_ops->sf_nextentry(sfp, sfep)) {
Barry Naujok5163f952008-05-21 16:41:01 +1000847 /*
848 * Compare name and if it's an exact match, return the inode
849 * number. If it's the first case-insensitive match, store the
850 * inode number and continue looking for an exact match.
851 */
852 cmp = dp->i_mount->m_dirnameops->compname(args, sfep->name,
853 sfep->namelen);
854 if (cmp != XFS_CMP_DIFFERENT && cmp != args->cmpresult) {
855 args->cmpresult = cmp;
Dave Chinner47401752013-10-29 22:11:47 +1100856 args->inumber = dp->d_ops->sf_get_ino(sfp, sfep);
857 args->filetype = dp->d_ops->sf_get_ftype(sfep);
Barry Naujok5163f952008-05-21 16:41:01 +1000858 if (cmp == XFS_CMP_EXACT)
Dave Chinner24513372014-06-25 14:58:08 +1000859 return -EEXIST;
Barry Naujok384f3ce2008-05-21 16:58:22 +1000860 ci_sfep = sfep;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 }
862 }
Barry Naujok6a178102008-05-21 16:42:05 +1000863 ASSERT(args->op_flags & XFS_DA_OP_OKNOENT);
Barry Naujok5163f952008-05-21 16:41:01 +1000864 /*
865 * Here, we can only be doing a lookup (not a rename or replace).
Dave Chinner24513372014-06-25 14:58:08 +1000866 * If a case-insensitive match was not found, return -ENOENT.
Barry Naujok5163f952008-05-21 16:41:01 +1000867 */
Barry Naujok384f3ce2008-05-21 16:58:22 +1000868 if (!ci_sfep)
Dave Chinner24513372014-06-25 14:58:08 +1000869 return -ENOENT;
Barry Naujok384f3ce2008-05-21 16:58:22 +1000870 /* otherwise process the CI match as required by the caller */
871 error = xfs_dir_cilookup_result(args, ci_sfep->name, ci_sfep->namelen);
Eric Sandeenb474c7a2014-06-22 15:04:54 +1000872 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873}
874
875/*
876 * Remove an entry from a shortform directory.
877 */
878int /* error */
879xfs_dir2_sf_removename(
880 xfs_da_args_t *args)
881{
882 int byteoff; /* offset of removed entry */
883 xfs_inode_t *dp; /* incore directory inode */
884 int entsize; /* this entry's size */
885 int i; /* shortform entry index */
886 int newsize; /* new inode size */
887 int oldsize; /* old inode size */
888 xfs_dir2_sf_entry_t *sfep; /* shortform directory entry */
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200889 xfs_dir2_sf_hdr_t *sfp; /* shortform structure */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000891 trace_xfs_dir2_sf_removename(args);
892
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 dp = args->dp;
894
895 ASSERT(dp->i_df.if_flags & XFS_IFINLINE);
896 oldsize = (int)dp->i_d.di_size;
897 /*
898 * Bail out if the directory is way too short.
899 */
900 if (oldsize < offsetof(xfs_dir2_sf_hdr_t, parent)) {
901 ASSERT(XFS_FORCED_SHUTDOWN(dp->i_mount));
Dave Chinner24513372014-06-25 14:58:08 +1000902 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 }
904 ASSERT(dp->i_df.if_bytes == oldsize);
905 ASSERT(dp->i_df.if_u1.if_data != NULL);
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200906 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
907 ASSERT(oldsize >= xfs_dir2_sf_hdr_size(sfp->i8count));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 /*
909 * Loop over the old directory entries.
910 * Find the one we're deleting.
911 */
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200912 for (i = 0, sfep = xfs_dir2_sf_firstentry(sfp); i < sfp->count;
Dave Chinner32c54832013-10-29 22:11:46 +1100913 i++, sfep = dp->d_ops->sf_nextentry(sfp, sfep)) {
Barry Naujok5163f952008-05-21 16:41:01 +1000914 if (xfs_da_compname(args, sfep->name, sfep->namelen) ==
915 XFS_CMP_EXACT) {
Dave Chinner47401752013-10-29 22:11:47 +1100916 ASSERT(dp->d_ops->sf_get_ino(sfp, sfep) ==
Christoph Hellwig8bc38782011-07-08 14:35:03 +0200917 args->inumber);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 break;
919 }
920 }
921 /*
922 * Didn't find it.
923 */
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200924 if (i == sfp->count)
Dave Chinner24513372014-06-25 14:58:08 +1000925 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 /*
927 * Calculate sizes.
928 */
929 byteoff = (int)((char *)sfep - (char *)sfp);
Dave Chinner32c54832013-10-29 22:11:46 +1100930 entsize = dp->d_ops->sf_entsize(sfp, args->namelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 newsize = oldsize - entsize;
932 /*
933 * Copy the part if any after the removed entry, sliding it down.
934 */
935 if (byteoff + entsize < oldsize)
936 memmove((char *)sfp + byteoff, (char *)sfp + byteoff + entsize,
937 oldsize - (byteoff + entsize));
938 /*
939 * Fix up the header and file size.
940 */
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200941 sfp->count--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 dp->i_d.di_size = newsize;
943 /*
944 * Reallocate, making it smaller.
945 */
946 xfs_idata_realloc(dp, newsize - oldsize, XFS_DATA_FORK);
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200947 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 /*
949 * Are we changing inode number size?
950 */
951 if (args->inumber > XFS_DIR2_MAX_SHORT_INUM) {
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200952 if (sfp->i8count == 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 xfs_dir2_sf_toino4(args);
954 else
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200955 sfp->i8count--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 xfs_dir2_sf_check(args);
958 xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE | XFS_ILOG_DDATA);
959 return 0;
960}
961
962/*
963 * Replace the inode number of an entry in a shortform directory.
964 */
965int /* error */
966xfs_dir2_sf_replace(
967 xfs_da_args_t *args) /* operation arguments */
968{
969 xfs_inode_t *dp; /* incore directory inode */
970 int i; /* entry index */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 xfs_ino_t ino=0; /* entry old inode number */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 int i8elevated; /* sf_toino8 set i8count=1 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 xfs_dir2_sf_entry_t *sfep; /* shortform directory entry */
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200974 xfs_dir2_sf_hdr_t *sfp; /* shortform structure */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000976 trace_xfs_dir2_sf_replace(args);
977
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 dp = args->dp;
979
980 ASSERT(dp->i_df.if_flags & XFS_IFINLINE);
981 /*
982 * Bail out if the shortform directory is way too small.
983 */
984 if (dp->i_d.di_size < offsetof(xfs_dir2_sf_hdr_t, parent)) {
985 ASSERT(XFS_FORCED_SHUTDOWN(dp->i_mount));
Dave Chinner24513372014-06-25 14:58:08 +1000986 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 }
988 ASSERT(dp->i_df.if_bytes == dp->i_d.di_size);
989 ASSERT(dp->i_df.if_u1.if_data != NULL);
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200990 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
991 ASSERT(dp->i_d.di_size >= xfs_dir2_sf_hdr_size(sfp->i8count));
Christoph Hellwigd5cf09b2014-07-30 09:12:05 +1000992
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 /*
994 * New inode number is large, and need to convert to 8-byte inodes.
995 */
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200996 if (args->inumber > XFS_DIR2_MAX_SHORT_INUM && sfp->i8count == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 int error; /* error return value */
998 int newsize; /* new inode size */
999
Christoph Hellwig266b6962016-07-20 11:48:31 +10001000 newsize = dp->i_df.if_bytes + (sfp->count + 1) * XFS_INO64_DIFF;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 /*
1002 * Won't fit as shortform, convert to block then do replace.
1003 */
1004 if (newsize > XFS_IFORK_DSIZE(dp)) {
1005 error = xfs_dir2_sf_to_block(args);
1006 if (error) {
1007 return error;
1008 }
1009 return xfs_dir2_block_replace(args);
1010 }
1011 /*
1012 * Still fits, convert to 8-byte now.
1013 */
1014 xfs_dir2_sf_toino8(args);
1015 i8elevated = 1;
Christoph Hellwigac8ba502011-07-08 14:35:13 +02001016 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 } else
1018 i8elevated = 0;
Christoph Hellwigd5cf09b2014-07-30 09:12:05 +10001019
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 ASSERT(args->namelen != 1 || args->name[0] != '.');
1021 /*
1022 * Replace ..'s entry.
1023 */
1024 if (args->namelen == 2 &&
1025 args->name[0] == '.' && args->name[1] == '.') {
Dave Chinner47401752013-10-29 22:11:47 +11001026 ino = dp->d_ops->sf_get_parent_ino(sfp);
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_parent_ino(sfp, args->inumber);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 }
1030 /*
1031 * Normal entry, look for the name.
1032 */
1033 else {
Dave Chinner0cb97762013-08-12 20:50:09 +10001034 for (i = 0, sfep = xfs_dir2_sf_firstentry(sfp); i < sfp->count;
Dave Chinner32c54832013-10-29 22:11:46 +11001035 i++, sfep = dp->d_ops->sf_nextentry(sfp, sfep)) {
Barry Naujok5163f952008-05-21 16:41:01 +10001036 if (xfs_da_compname(args, sfep->name, sfep->namelen) ==
1037 XFS_CMP_EXACT) {
Dave Chinner47401752013-10-29 22:11:47 +11001038 ino = dp->d_ops->sf_get_ino(sfp, sfep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 ASSERT(args->inumber != ino);
Dave Chinner47401752013-10-29 22:11:47 +11001040 dp->d_ops->sf_put_ino(sfp, sfep, args->inumber);
1041 dp->d_ops->sf_put_ftype(sfep, args->filetype);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 break;
1043 }
1044 }
1045 /*
1046 * Didn't find it.
1047 */
Christoph Hellwigac8ba502011-07-08 14:35:13 +02001048 if (i == sfp->count) {
Barry Naujok6a178102008-05-21 16:42:05 +10001049 ASSERT(args->op_flags & XFS_DA_OP_OKNOENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 if (i8elevated)
1051 xfs_dir2_sf_toino4(args);
Dave Chinner24513372014-06-25 14:58:08 +10001052 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 }
1054 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 /*
1056 * See if the old number was large, the new number is small.
1057 */
1058 if (ino > XFS_DIR2_MAX_SHORT_INUM &&
1059 args->inumber <= XFS_DIR2_MAX_SHORT_INUM) {
1060 /*
1061 * And the old count was one, so need to convert to small.
1062 */
Christoph Hellwigac8ba502011-07-08 14:35:13 +02001063 if (sfp->i8count == 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 xfs_dir2_sf_toino4(args);
1065 else
Christoph Hellwigac8ba502011-07-08 14:35:13 +02001066 sfp->i8count--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 }
1068 /*
1069 * See if the old number was small, the new number is large.
1070 */
1071 if (ino <= XFS_DIR2_MAX_SHORT_INUM &&
1072 args->inumber > XFS_DIR2_MAX_SHORT_INUM) {
1073 /*
1074 * add to the i8count unless we just converted to 8-byte
1075 * inodes (which does an implied i8count = 1)
1076 */
Christoph Hellwigac8ba502011-07-08 14:35:13 +02001077 ASSERT(sfp->i8count != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 if (!i8elevated)
Christoph Hellwigac8ba502011-07-08 14:35:13 +02001079 sfp->i8count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 xfs_dir2_sf_check(args);
1082 xfs_trans_log_inode(args->trans, dp, XFS_ILOG_DDATA);
1083 return 0;
1084}
1085
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086/*
1087 * Convert from 8-byte inode numbers to 4-byte inode numbers.
1088 * The last 8-byte inode number is gone, but the count is still 1.
1089 */
1090static void
1091xfs_dir2_sf_toino4(
1092 xfs_da_args_t *args) /* operation arguments */
1093{
1094 char *buf; /* old dir's buffer */
1095 xfs_inode_t *dp; /* incore directory inode */
1096 int i; /* entry index */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 int newsize; /* new inode size */
1098 xfs_dir2_sf_entry_t *oldsfep; /* old sf entry */
Christoph Hellwigac8ba502011-07-08 14:35:13 +02001099 xfs_dir2_sf_hdr_t *oldsfp; /* old sf directory */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 int oldsize; /* old inode size */
1101 xfs_dir2_sf_entry_t *sfep; /* new sf entry */
Christoph Hellwigac8ba502011-07-08 14:35:13 +02001102 xfs_dir2_sf_hdr_t *sfp; /* new sf directory */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001104 trace_xfs_dir2_sf_toino4(args);
1105
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 dp = args->dp;
1107
1108 /*
1109 * Copy the old directory to the buffer.
1110 * Then nuke it from the inode, and add the new buffer to the inode.
1111 * Don't want xfs_idata_realloc copying the data here.
1112 */
1113 oldsize = dp->i_df.if_bytes;
1114 buf = kmem_alloc(oldsize, KM_SLEEP);
Christoph Hellwigac8ba502011-07-08 14:35:13 +02001115 oldsfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
1116 ASSERT(oldsfp->i8count == 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 memcpy(buf, oldsfp, oldsize);
1118 /*
1119 * Compute the new inode size.
1120 */
Christoph Hellwig266b6962016-07-20 11:48:31 +10001121 newsize = oldsize - (oldsfp->count + 1) * XFS_INO64_DIFF;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 xfs_idata_realloc(dp, -oldsize, XFS_DATA_FORK);
1123 xfs_idata_realloc(dp, newsize, XFS_DATA_FORK);
1124 /*
1125 * Reset our pointers, the data has moved.
1126 */
Christoph Hellwigac8ba502011-07-08 14:35:13 +02001127 oldsfp = (xfs_dir2_sf_hdr_t *)buf;
1128 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 /*
1130 * Fill in the new header.
1131 */
Christoph Hellwigac8ba502011-07-08 14:35:13 +02001132 sfp->count = oldsfp->count;
1133 sfp->i8count = 0;
Dave Chinner47401752013-10-29 22:11:47 +11001134 dp->d_ops->sf_put_parent_ino(sfp, dp->d_ops->sf_get_parent_ino(oldsfp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 /*
1136 * Copy the entries field by field.
1137 */
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001138 for (i = 0, sfep = xfs_dir2_sf_firstentry(sfp),
1139 oldsfep = xfs_dir2_sf_firstentry(oldsfp);
Christoph Hellwigac8ba502011-07-08 14:35:13 +02001140 i < sfp->count;
Dave Chinner32c54832013-10-29 22:11:46 +11001141 i++, sfep = dp->d_ops->sf_nextentry(sfp, sfep),
1142 oldsfep = dp->d_ops->sf_nextentry(oldsfp, oldsfep)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 sfep->namelen = oldsfep->namelen;
Christoph Hellwig8353a642016-07-20 11:47:21 +10001144 memcpy(sfep->offset, oldsfep->offset, sizeof(sfep->offset));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 memcpy(sfep->name, oldsfep->name, sfep->namelen);
Dave Chinner47401752013-10-29 22:11:47 +11001146 dp->d_ops->sf_put_ino(sfp, sfep,
1147 dp->d_ops->sf_get_ino(oldsfp, oldsfep));
1148 dp->d_ops->sf_put_ftype(sfep, dp->d_ops->sf_get_ftype(oldsfep));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149 }
1150 /*
1151 * Clean up the inode.
1152 */
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10001153 kmem_free(buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 dp->i_d.di_size = newsize;
1155 xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE | XFS_ILOG_DDATA);
1156}
1157
1158/*
Eric Sandeen5e06d142014-04-14 19:07:23 +10001159 * Convert existing entries from 4-byte inode numbers to 8-byte inode numbers.
1160 * The new entry w/ an 8-byte inode number is not there yet; we leave with
1161 * i8count set to 1, but no corresponding 8-byte entry.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 */
1163static void
1164xfs_dir2_sf_toino8(
1165 xfs_da_args_t *args) /* operation arguments */
1166{
1167 char *buf; /* old dir's buffer */
1168 xfs_inode_t *dp; /* incore directory inode */
1169 int i; /* entry index */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 int newsize; /* new inode size */
1171 xfs_dir2_sf_entry_t *oldsfep; /* old sf entry */
Christoph Hellwigac8ba502011-07-08 14:35:13 +02001172 xfs_dir2_sf_hdr_t *oldsfp; /* old sf directory */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 int oldsize; /* old inode size */
1174 xfs_dir2_sf_entry_t *sfep; /* new sf entry */
Christoph Hellwigac8ba502011-07-08 14:35:13 +02001175 xfs_dir2_sf_hdr_t *sfp; /* new sf directory */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001177 trace_xfs_dir2_sf_toino8(args);
1178
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 dp = args->dp;
1180
1181 /*
1182 * Copy the old directory to the buffer.
1183 * Then nuke it from the inode, and add the new buffer to the inode.
1184 * Don't want xfs_idata_realloc copying the data here.
1185 */
1186 oldsize = dp->i_df.if_bytes;
1187 buf = kmem_alloc(oldsize, KM_SLEEP);
Christoph Hellwigac8ba502011-07-08 14:35:13 +02001188 oldsfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
1189 ASSERT(oldsfp->i8count == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 memcpy(buf, oldsfp, oldsize);
1191 /*
Eric Sandeen5e06d142014-04-14 19:07:23 +10001192 * Compute the new inode size (nb: entry count + 1 for parent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 */
Christoph Hellwig266b6962016-07-20 11:48:31 +10001194 newsize = oldsize + (oldsfp->count + 1) * XFS_INO64_DIFF;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195 xfs_idata_realloc(dp, -oldsize, XFS_DATA_FORK);
1196 xfs_idata_realloc(dp, newsize, XFS_DATA_FORK);
1197 /*
1198 * Reset our pointers, the data has moved.
1199 */
Christoph Hellwigac8ba502011-07-08 14:35:13 +02001200 oldsfp = (xfs_dir2_sf_hdr_t *)buf;
1201 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 /*
1203 * Fill in the new header.
1204 */
Christoph Hellwigac8ba502011-07-08 14:35:13 +02001205 sfp->count = oldsfp->count;
1206 sfp->i8count = 1;
Dave Chinner47401752013-10-29 22:11:47 +11001207 dp->d_ops->sf_put_parent_ino(sfp, dp->d_ops->sf_get_parent_ino(oldsfp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208 /*
1209 * Copy the entries field by field.
1210 */
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001211 for (i = 0, sfep = xfs_dir2_sf_firstentry(sfp),
1212 oldsfep = xfs_dir2_sf_firstentry(oldsfp);
Christoph Hellwigac8ba502011-07-08 14:35:13 +02001213 i < sfp->count;
Dave Chinner32c54832013-10-29 22:11:46 +11001214 i++, sfep = dp->d_ops->sf_nextentry(sfp, sfep),
1215 oldsfep = dp->d_ops->sf_nextentry(oldsfp, oldsfep)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 sfep->namelen = oldsfep->namelen;
Christoph Hellwig8353a642016-07-20 11:47:21 +10001217 memcpy(sfep->offset, oldsfep->offset, sizeof(sfep->offset));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 memcpy(sfep->name, oldsfep->name, sfep->namelen);
Dave Chinner47401752013-10-29 22:11:47 +11001219 dp->d_ops->sf_put_ino(sfp, sfep,
1220 dp->d_ops->sf_get_ino(oldsfp, oldsfep));
1221 dp->d_ops->sf_put_ftype(sfep, dp->d_ops->sf_get_ftype(oldsfep));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 }
1223 /*
1224 * Clean up the inode.
1225 */
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10001226 kmem_free(buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 dp->i_d.di_size = newsize;
1228 xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE | XFS_ILOG_DDATA);
1229}