blob: 96b45cd6c63f0686d3c1cce5c41b232f0ab82080 [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_block_tail_t *btp; /* block tail pointer */
160 xfs_dir2_data_entry_t *dep; /* data entry pointer */
161 xfs_inode_t *dp; /* incore directory inode */
162 xfs_dir2_data_unused_t *dup; /* unused data pointer */
163 char *endptr; /* end of data entries */
164 int error; /* error return value */
165 int logflags; /* inode logging flags */
166 xfs_mount_t *mp; /* filesystem mount point */
167 char *ptr; /* current data pointer */
168 xfs_dir2_sf_entry_t *sfep; /* shortform entry */
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200169 xfs_dir2_sf_hdr_t *sfp; /* shortform directory header */
Dave Chinnerb3f03ba2013-12-03 23:50:57 +1100170 xfs_dir2_sf_hdr_t *dst; /* temporary data buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000172 trace_xfs_dir2_block_to_sf(args);
173
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 dp = args->dp;
175 mp = dp->i_mount;
176
177 /*
Dave Chinnerb3f03ba2013-12-03 23:50:57 +1100178 * allocate a temporary destination buffer the size of the inode
179 * to format the data into. Once we have formatted the data, we
180 * can free the block and copy the formatted data into the inode literal
181 * area.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 */
Dave Chinnerb3f03ba2013-12-03 23:50:57 +1100183 dst = kmem_alloc(mp->m_sb.sb_inodesize, KM_SLEEP);
184 hdr = bp->b_addr;
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +0200185
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 * Copy the header into the newly allocate local space.
188 */
Dave Chinnerb3f03ba2013-12-03 23:50:57 +1100189 sfp = (xfs_dir2_sf_hdr_t *)dst;
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000190 memcpy(sfp, sfhp, xfs_dir2_sf_hdr_size(sfhp->i8count));
Dave Chinnerb3f03ba2013-12-03 23:50:57 +1100191
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 /*
193 * Set up to loop over the block's entries.
194 */
Dave Chinner8f661932014-06-06 15:15:59 +1000195 btp = xfs_dir2_block_tail_p(args->geo, hdr);
Dave Chinner2ca98772013-10-29 22:11:49 +1100196 ptr = (char *)dp->d_ops->data_entry_p(hdr);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000197 endptr = (char *)xfs_dir2_block_leaf_p(btp);
198 sfep = xfs_dir2_sf_firstentry(sfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 /*
200 * Loop over the active and unused entries.
201 * Stop when we reach the leaf/tail portion of the block.
202 */
203 while (ptr < endptr) {
204 /*
205 * If it's unused, just skip over it.
206 */
207 dup = (xfs_dir2_data_unused_t *)ptr;
Nathan Scottad354eb2006-03-17 17:27:37 +1100208 if (be16_to_cpu(dup->freetag) == XFS_DIR2_DATA_FREE_TAG) {
209 ptr += be16_to_cpu(dup->length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 continue;
211 }
212 dep = (xfs_dir2_data_entry_t *)ptr;
213 /*
214 * Skip .
215 */
216 if (dep->namelen == 1 && dep->name[0] == '.')
Christoph Hellwigff9901c2006-06-09 14:48:37 +1000217 ASSERT(be64_to_cpu(dep->inumber) == dp->i_ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 /*
219 * Skip .., but make sure the inode number is right.
220 */
221 else if (dep->namelen == 2 &&
222 dep->name[0] == '.' && dep->name[1] == '.')
Christoph Hellwigff9901c2006-06-09 14:48:37 +1000223 ASSERT(be64_to_cpu(dep->inumber) ==
Dave Chinner47401752013-10-29 22:11:47 +1100224 dp->d_ops->sf_get_parent_ino(sfp));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 /*
226 * Normal entry, copy it into shortform.
227 */
228 else {
229 sfep->namelen = dep->namelen;
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000230 xfs_dir2_sf_put_offset(sfep,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 (xfs_dir2_data_aoff_t)
Christoph Hellwiga64b0412011-07-08 14:35:32 +0200232 ((char *)dep - (char *)hdr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 memcpy(sfep->name, dep->name, dep->namelen);
Dave Chinner47401752013-10-29 22:11:47 +1100234 dp->d_ops->sf_put_ino(sfp, sfep,
235 be64_to_cpu(dep->inumber));
236 dp->d_ops->sf_put_ftype(sfep,
Dave Chinner9d23fc82013-10-29 22:11:48 +1100237 dp->d_ops->data_get_ftype(dep));
Christoph Hellwig8bc38782011-07-08 14:35:03 +0200238
Dave Chinner32c54832013-10-29 22:11:46 +1100239 sfep = dp->d_ops->sf_nextentry(sfp, sfep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 }
Dave Chinner9d23fc82013-10-29 22:11:48 +1100241 ptr += dp->d_ops->data_entsize(dep->namelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 }
243 ASSERT((char *)sfep - (char *)sfp == size);
Dave Chinnerb3f03ba2013-12-03 23:50:57 +1100244
245 /* now we are done with the block, we can shrink the inode */
246 logflags = XFS_ILOG_CORE;
Dave Chinner7dda6e82014-06-06 15:11:18 +1000247 error = xfs_dir2_shrink_inode(args, args->geo->datablk, bp);
Dave Chinnerb3f03ba2013-12-03 23:50:57 +1100248 if (error) {
Dave Chinner24513372014-06-25 14:58:08 +1000249 ASSERT(error != -ENOSPC);
Dave Chinnerb3f03ba2013-12-03 23:50:57 +1100250 goto out;
251 }
252
253 /*
254 * The buffer is now unconditionally gone, whether
255 * xfs_dir2_shrink_inode worked or not.
256 *
257 * Convert the inode to local format and copy the data in.
258 */
Dave Chinnerb3f03ba2013-12-03 23:50:57 +1100259 ASSERT(dp->i_df.if_bytes == 0);
Christoph Hellwig143f4ae2016-04-06 07:41:43 +1000260 xfs_init_local_fork(dp, XFS_DATA_FORK, dst, size);
261 dp->i_d.di_format = XFS_DINODE_FMT_LOCAL;
262 dp->i_d.di_size = size;
Dave Chinnerb3f03ba2013-12-03 23:50:57 +1100263
264 logflags |= XFS_ILOG_DDATA;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 xfs_dir2_sf_check(args);
266out:
267 xfs_trans_log_inode(args->trans, dp, logflags);
Dave Chinnerb3f03ba2013-12-03 23:50:57 +1100268 kmem_free(dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 return error;
270}
271
272/*
273 * Add a name to a shortform directory.
274 * There are two algorithms, "easy" and "hard" which we decide on
275 * before changing anything.
276 * Convert to block form if necessary, if the new entry won't fit.
277 */
278int /* error */
279xfs_dir2_sf_addname(
280 xfs_da_args_t *args) /* operation arguments */
281{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 xfs_inode_t *dp; /* incore directory inode */
283 int error; /* error return value */
284 int incr_isize; /* total change in size */
285 int new_isize; /* di_size after adding name */
286 int objchange; /* changing to 8-byte inodes */
Christoph Hellwig5bde1ba92005-11-02 15:06:18 +1100287 xfs_dir2_data_aoff_t offset = 0; /* offset for new entry */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 int pick; /* which algorithm to use */
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200289 xfs_dir2_sf_hdr_t *sfp; /* shortform structure */
Christoph Hellwig5bde1ba92005-11-02 15:06:18 +1100290 xfs_dir2_sf_entry_t *sfep = NULL; /* shortform entry */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000292 trace_xfs_dir2_sf_addname(args);
293
Dave Chinner24513372014-06-25 14:58:08 +1000294 ASSERT(xfs_dir2_sf_lookup(args) == -ENOENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 dp = args->dp;
296 ASSERT(dp->i_df.if_flags & XFS_IFINLINE);
297 /*
298 * Make sure the shortform value has some of its header.
299 */
300 if (dp->i_d.di_size < offsetof(xfs_dir2_sf_hdr_t, parent)) {
301 ASSERT(XFS_FORCED_SHUTDOWN(dp->i_mount));
Dave Chinner24513372014-06-25 14:58:08 +1000302 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 }
304 ASSERT(dp->i_df.if_bytes == dp->i_d.di_size);
305 ASSERT(dp->i_df.if_u1.if_data != NULL);
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200306 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
307 ASSERT(dp->i_d.di_size >= xfs_dir2_sf_hdr_size(sfp->i8count));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 /*
309 * Compute entry (and change in) size.
310 */
Eric Sandeen5e06d142014-04-14 19:07:23 +1000311 incr_isize = dp->d_ops->sf_entsize(sfp, args->namelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 objchange = 0;
Christoph Hellwigd5cf09b2014-07-30 09:12:05 +1000313
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 /*
315 * Do we have to change to 8 byte inodes?
316 */
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200317 if (args->inumber > XFS_DIR2_MAX_SHORT_INUM && sfp->i8count == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 /*
Eric Sandeen5e06d142014-04-14 19:07:23 +1000319 * Yes, adjust the inode size. old count + (parent + new)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 */
Christoph Hellwig266b6962016-07-20 11:48:31 +1000321 incr_isize += (sfp->count + 2) * XFS_INO64_DIFF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 objchange = 1;
323 }
Christoph Hellwigd5cf09b2014-07-30 09:12:05 +1000324
Eric Sandeen5e06d142014-04-14 19:07:23 +1000325 new_isize = (int)dp->i_d.di_size + incr_isize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 /*
327 * Won't fit as shortform any more (due to size),
328 * or the pick routine says it won't (due to offset values).
329 */
330 if (new_isize > XFS_IFORK_DSIZE(dp) ||
331 (pick =
332 xfs_dir2_sf_addname_pick(args, objchange, &sfep, &offset)) == 0) {
333 /*
334 * Just checking or no space reservation, it doesn't fit.
335 */
Barry Naujok6a178102008-05-21 16:42:05 +1000336 if ((args->op_flags & XFS_DA_OP_JUSTCHECK) || args->total == 0)
Dave Chinner24513372014-06-25 14:58:08 +1000337 return -ENOSPC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 /*
339 * Convert to block form then add the name.
340 */
341 error = xfs_dir2_sf_to_block(args);
342 if (error)
343 return error;
344 return xfs_dir2_block_addname(args);
345 }
346 /*
347 * Just checking, it fits.
348 */
Barry Naujok6a178102008-05-21 16:42:05 +1000349 if (args->op_flags & XFS_DA_OP_JUSTCHECK)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 return 0;
351 /*
352 * Do it the easy way - just add it at the end.
353 */
354 if (pick == 1)
355 xfs_dir2_sf_addname_easy(args, sfep, offset, new_isize);
356 /*
357 * Do it the hard way - look for a place to insert the new entry.
358 * Convert to 8 byte inode numbers first if necessary.
359 */
360 else {
361 ASSERT(pick == 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 if (objchange)
363 xfs_dir2_sf_toino8(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 xfs_dir2_sf_addname_hard(args, objchange, new_isize);
365 }
366 xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE | XFS_ILOG_DDATA);
367 return 0;
368}
369
370/*
371 * Add the new entry the "easy" way.
372 * This is copying the old directory and adding the new entry at the end.
373 * Since it's sorted by "offset" we need room after the last offset
374 * that's already there, and then room to convert to a block directory.
375 * This is already checked by the pick routine.
376 */
377static void
378xfs_dir2_sf_addname_easy(
379 xfs_da_args_t *args, /* operation arguments */
380 xfs_dir2_sf_entry_t *sfep, /* pointer to new entry */
381 xfs_dir2_data_aoff_t offset, /* offset to use for new ent */
382 int new_isize) /* new directory size */
383{
384 int byteoff; /* byte offset in sf dir */
385 xfs_inode_t *dp; /* incore directory inode */
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200386 xfs_dir2_sf_hdr_t *sfp; /* shortform structure */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387
388 dp = args->dp;
389
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200390 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 byteoff = (int)((char *)sfep - (char *)sfp);
392 /*
393 * Grow the in-inode space.
394 */
Dave Chinner32c54832013-10-29 22:11:46 +1100395 xfs_idata_realloc(dp, dp->d_ops->sf_entsize(sfp, args->namelen),
Dave Chinner0cb97762013-08-12 20:50:09 +1000396 XFS_DATA_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 /*
398 * Need to set up again due to realloc of the inode data.
399 */
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200400 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 sfep = (xfs_dir2_sf_entry_t *)((char *)sfp + byteoff);
402 /*
403 * Fill in the new entry.
404 */
405 sfep->namelen = args->namelen;
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000406 xfs_dir2_sf_put_offset(sfep, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 memcpy(sfep->name, args->name, sfep->namelen);
Dave Chinner47401752013-10-29 22:11:47 +1100408 dp->d_ops->sf_put_ino(sfp, sfep, args->inumber);
409 dp->d_ops->sf_put_ftype(sfep, args->filetype);
Dave Chinner1c55cec2013-08-12 20:50:10 +1000410
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 /*
412 * Update the header and inode.
413 */
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200414 sfp->count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 if (args->inumber > XFS_DIR2_MAX_SHORT_INUM)
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200416 sfp->i8count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 dp->i_d.di_size = new_isize;
418 xfs_dir2_sf_check(args);
419}
420
421/*
422 * Add the new entry the "hard" way.
423 * The caller has already converted to 8 byte inode numbers if necessary,
424 * in which case we need to leave the i8count at 1.
425 * Find a hole that the new entry will fit into, and copy
426 * the first part of the entries, the new entry, and the last part of
427 * the entries.
428 */
429/* ARGSUSED */
430static void
431xfs_dir2_sf_addname_hard(
432 xfs_da_args_t *args, /* operation arguments */
433 int objchange, /* changing inode number size */
434 int new_isize) /* new directory size */
435{
436 int add_datasize; /* data size need for new ent */
437 char *buf; /* buffer for old */
438 xfs_inode_t *dp; /* incore directory inode */
439 int eof; /* reached end of old dir */
440 int nbytes; /* temp for byte copies */
441 xfs_dir2_data_aoff_t new_offset; /* next offset value */
442 xfs_dir2_data_aoff_t offset; /* current offset value */
443 int old_isize; /* previous di_size */
444 xfs_dir2_sf_entry_t *oldsfep; /* entry in original dir */
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200445 xfs_dir2_sf_hdr_t *oldsfp; /* original shortform dir */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 xfs_dir2_sf_entry_t *sfep; /* entry in new dir */
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200447 xfs_dir2_sf_hdr_t *sfp; /* new shortform dir */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448
449 /*
450 * Copy the old directory to the stack buffer.
451 */
452 dp = args->dp;
453
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200454 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 old_isize = (int)dp->i_d.di_size;
456 buf = kmem_alloc(old_isize, KM_SLEEP);
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200457 oldsfp = (xfs_dir2_sf_hdr_t *)buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 memcpy(oldsfp, sfp, old_isize);
459 /*
460 * Loop over the old directory finding the place we're going
461 * to insert the new entry.
462 * If it's going to end up at the end then oldsfep will point there.
463 */
Dave Chinner1c9a5b22013-10-30 09:15:02 +1100464 for (offset = dp->d_ops->data_first_offset,
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000465 oldsfep = xfs_dir2_sf_firstentry(oldsfp),
Dave Chinner9d23fc82013-10-29 22:11:48 +1100466 add_datasize = dp->d_ops->data_entsize(args->namelen),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 eof = (char *)oldsfep == &buf[old_isize];
468 !eof;
Dave Chinner9d23fc82013-10-29 22:11:48 +1100469 offset = new_offset + dp->d_ops->data_entsize(oldsfep->namelen),
Dave Chinner32c54832013-10-29 22:11:46 +1100470 oldsfep = dp->d_ops->sf_nextentry(oldsfp, oldsfep),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 eof = (char *)oldsfep == &buf[old_isize]) {
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000472 new_offset = xfs_dir2_sf_get_offset(oldsfep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 if (offset + add_datasize <= new_offset)
474 break;
475 }
476 /*
477 * Get rid of the old directory, then allocate space for
478 * the new one. We do this so xfs_idata_realloc won't copy
479 * the data.
480 */
481 xfs_idata_realloc(dp, -old_isize, XFS_DATA_FORK);
482 xfs_idata_realloc(dp, new_isize, XFS_DATA_FORK);
483 /*
484 * Reset the pointer since the buffer was reallocated.
485 */
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200486 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 /*
488 * Copy the first part of the directory, including the header.
489 */
490 nbytes = (int)((char *)oldsfep - (char *)oldsfp);
491 memcpy(sfp, oldsfp, nbytes);
492 sfep = (xfs_dir2_sf_entry_t *)((char *)sfp + nbytes);
493 /*
494 * Fill in the new entry, and update the header counts.
495 */
496 sfep->namelen = args->namelen;
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000497 xfs_dir2_sf_put_offset(sfep, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 memcpy(sfep->name, args->name, sfep->namelen);
Dave Chinner47401752013-10-29 22:11:47 +1100499 dp->d_ops->sf_put_ino(sfp, sfep, args->inumber);
500 dp->d_ops->sf_put_ftype(sfep, args->filetype);
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200501 sfp->count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 if (args->inumber > XFS_DIR2_MAX_SHORT_INUM && !objchange)
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200503 sfp->i8count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 /*
505 * If there's more left to copy, do that.
506 */
507 if (!eof) {
Dave Chinner32c54832013-10-29 22:11:46 +1100508 sfep = dp->d_ops->sf_nextentry(sfp, sfep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 memcpy(sfep, oldsfep, old_isize - nbytes);
510 }
Denys Vlasenkof0e2d932008-05-19 16:31:57 +1000511 kmem_free(buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 dp->i_d.di_size = new_isize;
513 xfs_dir2_sf_check(args);
514}
515
516/*
517 * Decide if the new entry will fit at all.
518 * If it will fit, pick between adding the new entry to the end (easy)
519 * or somewhere else (hard).
520 * Return 0 (won't fit), 1 (easy), 2 (hard).
521 */
522/*ARGSUSED*/
523static int /* pick result */
524xfs_dir2_sf_addname_pick(
525 xfs_da_args_t *args, /* operation arguments */
526 int objchange, /* inode # size changes */
527 xfs_dir2_sf_entry_t **sfepp, /* out(1): new entry ptr */
528 xfs_dir2_data_aoff_t *offsetp) /* out(1): new offset */
529{
530 xfs_inode_t *dp; /* incore directory inode */
531 int holefit; /* found hole it will fit in */
532 int i; /* entry number */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 xfs_dir2_data_aoff_t offset; /* data block offset */
534 xfs_dir2_sf_entry_t *sfep; /* shortform entry */
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200535 xfs_dir2_sf_hdr_t *sfp; /* shortform structure */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 int size; /* entry's data size */
537 int used; /* data bytes used */
538
539 dp = args->dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200541 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
Dave Chinner9d23fc82013-10-29 22:11:48 +1100542 size = dp->d_ops->data_entsize(args->namelen);
Dave Chinner1c9a5b22013-10-30 09:15:02 +1100543 offset = dp->d_ops->data_first_offset;
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000544 sfep = xfs_dir2_sf_firstentry(sfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 holefit = 0;
546 /*
547 * Loop over sf entries.
548 * Keep track of data offset and whether we've seen a place
549 * to insert the new entry.
550 */
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200551 for (i = 0; i < sfp->count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 if (!holefit)
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000553 holefit = offset + size <= xfs_dir2_sf_get_offset(sfep);
554 offset = xfs_dir2_sf_get_offset(sfep) +
Dave Chinner9d23fc82013-10-29 22:11:48 +1100555 dp->d_ops->data_entsize(sfep->namelen);
Dave Chinner32c54832013-10-29 22:11:46 +1100556 sfep = dp->d_ops->sf_nextentry(sfp, sfep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 }
558 /*
559 * Calculate data bytes used excluding the new entry, if this
560 * was a data block (block form directory).
561 */
562 used = offset +
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200563 (sfp->count + 3) * (uint)sizeof(xfs_dir2_leaf_entry_t) +
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 (uint)sizeof(xfs_dir2_block_tail_t);
565 /*
566 * If it won't fit in a block form then we can't insert it,
567 * we'll go back, convert to block, then try the insert and convert
568 * to leaf.
569 */
Dave Chinner8f661932014-06-06 15:15:59 +1000570 if (used + (holefit ? 0 : size) > args->geo->blksize)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 return 0;
572 /*
573 * If changing the inode number size, do it the hard way.
574 */
Christoph Hellwigd5cf09b2014-07-30 09:12:05 +1000575 if (objchange)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 return 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 /*
578 * If it won't fit at the end then do it the hard way (use the hole).
579 */
Dave Chinner8f661932014-06-06 15:15:59 +1000580 if (used + size > args->geo->blksize)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 return 2;
582 /*
583 * Do it the easy way.
584 */
585 *sfepp = sfep;
586 *offsetp = offset;
587 return 1;
588}
589
590#ifdef DEBUG
591/*
592 * Check consistency of shortform directory, assert if bad.
593 */
594static void
595xfs_dir2_sf_check(
596 xfs_da_args_t *args) /* operation arguments */
597{
598 xfs_inode_t *dp; /* incore directory inode */
599 int i; /* entry number */
600 int i8count; /* number of big inode#s */
601 xfs_ino_t ino; /* entry inode number */
602 int offset; /* data offset */
603 xfs_dir2_sf_entry_t *sfep; /* shortform dir entry */
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200604 xfs_dir2_sf_hdr_t *sfp; /* shortform structure */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605
606 dp = args->dp;
607
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200608 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
Dave Chinner1c9a5b22013-10-30 09:15:02 +1100609 offset = dp->d_ops->data_first_offset;
Dave Chinner47401752013-10-29 22:11:47 +1100610 ino = dp->d_ops->sf_get_parent_ino(sfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 i8count = ino > XFS_DIR2_MAX_SHORT_INUM;
612
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000613 for (i = 0, sfep = xfs_dir2_sf_firstentry(sfp);
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200614 i < sfp->count;
Dave Chinner32c54832013-10-29 22:11:46 +1100615 i++, sfep = dp->d_ops->sf_nextentry(sfp, sfep)) {
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000616 ASSERT(xfs_dir2_sf_get_offset(sfep) >= offset);
Dave Chinner47401752013-10-29 22:11:47 +1100617 ino = dp->d_ops->sf_get_ino(sfp, sfep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 i8count += ino > XFS_DIR2_MAX_SHORT_INUM;
619 offset =
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000620 xfs_dir2_sf_get_offset(sfep) +
Dave Chinner9d23fc82013-10-29 22:11:48 +1100621 dp->d_ops->data_entsize(sfep->namelen);
Dave Chinner47401752013-10-29 22:11:47 +1100622 ASSERT(dp->d_ops->sf_get_ftype(sfep) < XFS_DIR3_FT_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 }
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200624 ASSERT(i8count == sfp->i8count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 ASSERT((char *)sfep - (char *)sfp == dp->i_d.di_size);
626 ASSERT(offset +
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200627 (sfp->count + 2) * (uint)sizeof(xfs_dir2_leaf_entry_t) +
Dave Chinner8f661932014-06-06 15:15:59 +1000628 (uint)sizeof(xfs_dir2_block_tail_t) <= args->geo->blksize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629}
630#endif /* DEBUG */
631
Darrick J. Wong630a04e2017-03-15 00:24:25 -0700632/* Verify the consistency of an inline directory. */
633int
634xfs_dir2_sf_verify(
635 struct xfs_mount *mp,
636 struct xfs_dir2_sf_hdr *sfp,
637 int size)
638{
639 struct xfs_dir2_sf_entry *sfep;
640 struct xfs_dir2_sf_entry *next_sfep;
641 char *endp;
642 const struct xfs_dir_ops *dops;
643 xfs_ino_t ino;
644 int i;
645 int i8count;
646 int offset;
647 __uint8_t filetype;
648
649 dops = xfs_dir_get_ops(mp, NULL);
650
651 /*
652 * Give up if the directory is way too short.
653 */
654 XFS_WANT_CORRUPTED_RETURN(mp, size >
655 offsetof(struct xfs_dir2_sf_hdr, parent));
656 XFS_WANT_CORRUPTED_RETURN(mp, size >=
657 xfs_dir2_sf_hdr_size(sfp->i8count));
658
659 endp = (char *)sfp + size;
660
661 /* Check .. entry */
662 ino = dops->sf_get_parent_ino(sfp);
663 i8count = ino > XFS_DIR2_MAX_SHORT_INUM;
664 XFS_WANT_CORRUPTED_RETURN(mp, !xfs_dir_ino_validate(mp, ino));
665 offset = dops->data_first_offset;
666
667 /* Check all reported entries */
668 sfep = xfs_dir2_sf_firstentry(sfp);
669 for (i = 0; i < sfp->count; i++) {
670 /*
671 * struct xfs_dir2_sf_entry has a variable length.
672 * Check the fixed-offset parts of the structure are
673 * within the data buffer.
674 */
675 XFS_WANT_CORRUPTED_RETURN(mp,
676 ((char *)sfep + sizeof(*sfep)) < endp);
677
678 /* Don't allow names with known bad length. */
679 XFS_WANT_CORRUPTED_RETURN(mp, sfep->namelen > 0);
680 XFS_WANT_CORRUPTED_RETURN(mp, sfep->namelen < MAXNAMELEN);
681
682 /*
683 * Check that the variable-length part of the structure is
684 * within the data buffer. The next entry starts after the
685 * name component, so nextentry is an acceptable test.
686 */
687 next_sfep = dops->sf_nextentry(sfp, sfep);
688 XFS_WANT_CORRUPTED_RETURN(mp, endp >= (char *)next_sfep);
689
690 /* Check that the offsets always increase. */
691 XFS_WANT_CORRUPTED_RETURN(mp,
692 xfs_dir2_sf_get_offset(sfep) >= offset);
693
694 /* Check the inode number. */
695 ino = dops->sf_get_ino(sfp, sfep);
696 i8count += ino > XFS_DIR2_MAX_SHORT_INUM;
697 XFS_WANT_CORRUPTED_RETURN(mp, !xfs_dir_ino_validate(mp, ino));
698
699 /* Check the file type. */
700 filetype = dops->sf_get_ftype(sfep);
701 XFS_WANT_CORRUPTED_RETURN(mp, filetype < XFS_DIR3_FT_MAX);
702
703 offset = xfs_dir2_sf_get_offset(sfep) +
704 dops->data_entsize(sfep->namelen);
705
706 sfep = next_sfep;
707 }
708 XFS_WANT_CORRUPTED_RETURN(mp, i8count == sfp->i8count);
709 XFS_WANT_CORRUPTED_RETURN(mp, (void *)sfep == (void *)endp);
710
711 /* Make sure this whole thing ought to be in local format. */
712 XFS_WANT_CORRUPTED_RETURN(mp, offset +
713 (sfp->count + 2) * (uint)sizeof(xfs_dir2_leaf_entry_t) +
714 (uint)sizeof(xfs_dir2_block_tail_t) <= mp->m_dir_geo->blksize);
715
716 return 0;
717}
718
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719/*
720 * Create a new (shortform) directory.
721 */
722int /* error, always 0 */
723xfs_dir2_sf_create(
724 xfs_da_args_t *args, /* operation arguments */
725 xfs_ino_t pino) /* parent inode number */
726{
727 xfs_inode_t *dp; /* incore directory inode */
728 int i8count; /* parent inode is an 8-byte number */
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200729 xfs_dir2_sf_hdr_t *sfp; /* shortform structure */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 int size; /* directory size */
731
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000732 trace_xfs_dir2_sf_create(args);
733
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 dp = args->dp;
735
736 ASSERT(dp != NULL);
737 ASSERT(dp->i_d.di_size == 0);
738 /*
739 * If it's currently a zero-length extent file,
740 * convert it to local format.
741 */
742 if (dp->i_d.di_format == XFS_DINODE_FMT_EXTENTS) {
743 dp->i_df.if_flags &= ~XFS_IFEXTENTS; /* just in case */
744 dp->i_d.di_format = XFS_DINODE_FMT_LOCAL;
745 xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE);
746 dp->i_df.if_flags |= XFS_IFINLINE;
747 }
748 ASSERT(dp->i_df.if_flags & XFS_IFINLINE);
749 ASSERT(dp->i_df.if_bytes == 0);
750 i8count = pino > XFS_DIR2_MAX_SHORT_INUM;
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000751 size = xfs_dir2_sf_hdr_size(i8count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 /*
753 * Make a buffer for the data.
754 */
755 xfs_idata_realloc(dp, size, XFS_DATA_FORK);
756 /*
757 * Fill in the header,
758 */
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200759 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
760 sfp->i8count = i8count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 /*
762 * Now can put in the inode number, since i8count is set.
763 */
Dave Chinner47401752013-10-29 22:11:47 +1100764 dp->d_ops->sf_put_parent_ino(sfp, pino);
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200765 sfp->count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 dp->i_d.di_size = size;
767 xfs_dir2_sf_check(args);
768 xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE | XFS_ILOG_DDATA);
769 return 0;
770}
771
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772/*
773 * Lookup an entry in a shortform directory.
774 * Returns EEXIST if found, ENOENT if not found.
775 */
776int /* error */
777xfs_dir2_sf_lookup(
778 xfs_da_args_t *args) /* operation arguments */
779{
780 xfs_inode_t *dp; /* incore directory inode */
781 int i; /* entry index */
Barry Naujok384f3ce2008-05-21 16:58:22 +1000782 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 xfs_dir2_sf_entry_t *sfep; /* shortform directory entry */
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200784 xfs_dir2_sf_hdr_t *sfp; /* shortform structure */
Barry Naujok5163f952008-05-21 16:41:01 +1000785 enum xfs_dacmp cmp; /* comparison result */
Barry Naujok384f3ce2008-05-21 16:58:22 +1000786 xfs_dir2_sf_entry_t *ci_sfep; /* case-insens. entry */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000788 trace_xfs_dir2_sf_lookup(args);
789
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 xfs_dir2_sf_check(args);
791 dp = args->dp;
792
793 ASSERT(dp->i_df.if_flags & XFS_IFINLINE);
794 /*
795 * Bail out if the directory is way too short.
796 */
797 if (dp->i_d.di_size < offsetof(xfs_dir2_sf_hdr_t, parent)) {
798 ASSERT(XFS_FORCED_SHUTDOWN(dp->i_mount));
Dave Chinner24513372014-06-25 14:58:08 +1000799 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 }
801 ASSERT(dp->i_df.if_bytes == dp->i_d.di_size);
802 ASSERT(dp->i_df.if_u1.if_data != NULL);
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200803 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
804 ASSERT(dp->i_d.di_size >= xfs_dir2_sf_hdr_size(sfp->i8count));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 /*
806 * Special case for .
807 */
808 if (args->namelen == 1 && args->name[0] == '.') {
809 args->inumber = dp->i_ino;
Barry Naujok5163f952008-05-21 16:41:01 +1000810 args->cmpresult = XFS_CMP_EXACT;
Dave Chinner1c55cec2013-08-12 20:50:10 +1000811 args->filetype = XFS_DIR3_FT_DIR;
Dave Chinner24513372014-06-25 14:58:08 +1000812 return -EEXIST;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 }
814 /*
815 * Special case for ..
816 */
817 if (args->namelen == 2 &&
818 args->name[0] == '.' && args->name[1] == '.') {
Dave Chinner47401752013-10-29 22:11:47 +1100819 args->inumber = dp->d_ops->sf_get_parent_ino(sfp);
Barry Naujok5163f952008-05-21 16:41:01 +1000820 args->cmpresult = XFS_CMP_EXACT;
Dave Chinner1c55cec2013-08-12 20:50:10 +1000821 args->filetype = XFS_DIR3_FT_DIR;
Dave Chinner24513372014-06-25 14:58:08 +1000822 return -EEXIST;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 }
824 /*
825 * Loop over all the entries trying to match ours.
826 */
Barry Naujok384f3ce2008-05-21 16:58:22 +1000827 ci_sfep = NULL;
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200828 for (i = 0, sfep = xfs_dir2_sf_firstentry(sfp); i < sfp->count;
Dave Chinner32c54832013-10-29 22:11:46 +1100829 i++, sfep = dp->d_ops->sf_nextentry(sfp, sfep)) {
Barry Naujok5163f952008-05-21 16:41:01 +1000830 /*
831 * Compare name and if it's an exact match, return the inode
832 * number. If it's the first case-insensitive match, store the
833 * inode number and continue looking for an exact match.
834 */
835 cmp = dp->i_mount->m_dirnameops->compname(args, sfep->name,
836 sfep->namelen);
837 if (cmp != XFS_CMP_DIFFERENT && cmp != args->cmpresult) {
838 args->cmpresult = cmp;
Dave Chinner47401752013-10-29 22:11:47 +1100839 args->inumber = dp->d_ops->sf_get_ino(sfp, sfep);
840 args->filetype = dp->d_ops->sf_get_ftype(sfep);
Barry Naujok5163f952008-05-21 16:41:01 +1000841 if (cmp == XFS_CMP_EXACT)
Dave Chinner24513372014-06-25 14:58:08 +1000842 return -EEXIST;
Barry Naujok384f3ce2008-05-21 16:58:22 +1000843 ci_sfep = sfep;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 }
845 }
Barry Naujok6a178102008-05-21 16:42:05 +1000846 ASSERT(args->op_flags & XFS_DA_OP_OKNOENT);
Barry Naujok5163f952008-05-21 16:41:01 +1000847 /*
848 * Here, we can only be doing a lookup (not a rename or replace).
Dave Chinner24513372014-06-25 14:58:08 +1000849 * If a case-insensitive match was not found, return -ENOENT.
Barry Naujok5163f952008-05-21 16:41:01 +1000850 */
Barry Naujok384f3ce2008-05-21 16:58:22 +1000851 if (!ci_sfep)
Dave Chinner24513372014-06-25 14:58:08 +1000852 return -ENOENT;
Barry Naujok384f3ce2008-05-21 16:58:22 +1000853 /* otherwise process the CI match as required by the caller */
854 error = xfs_dir_cilookup_result(args, ci_sfep->name, ci_sfep->namelen);
Eric Sandeenb474c7a2014-06-22 15:04:54 +1000855 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856}
857
858/*
859 * Remove an entry from a shortform directory.
860 */
861int /* error */
862xfs_dir2_sf_removename(
863 xfs_da_args_t *args)
864{
865 int byteoff; /* offset of removed entry */
866 xfs_inode_t *dp; /* incore directory inode */
867 int entsize; /* this entry's size */
868 int i; /* shortform entry index */
869 int newsize; /* new inode size */
870 int oldsize; /* old inode size */
871 xfs_dir2_sf_entry_t *sfep; /* shortform directory entry */
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200872 xfs_dir2_sf_hdr_t *sfp; /* shortform structure */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000874 trace_xfs_dir2_sf_removename(args);
875
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 dp = args->dp;
877
878 ASSERT(dp->i_df.if_flags & XFS_IFINLINE);
879 oldsize = (int)dp->i_d.di_size;
880 /*
881 * Bail out if the directory is way too short.
882 */
883 if (oldsize < offsetof(xfs_dir2_sf_hdr_t, parent)) {
884 ASSERT(XFS_FORCED_SHUTDOWN(dp->i_mount));
Dave Chinner24513372014-06-25 14:58:08 +1000885 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 }
887 ASSERT(dp->i_df.if_bytes == oldsize);
888 ASSERT(dp->i_df.if_u1.if_data != NULL);
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200889 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
890 ASSERT(oldsize >= xfs_dir2_sf_hdr_size(sfp->i8count));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 /*
892 * Loop over the old directory entries.
893 * Find the one we're deleting.
894 */
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200895 for (i = 0, sfep = xfs_dir2_sf_firstentry(sfp); i < sfp->count;
Dave Chinner32c54832013-10-29 22:11:46 +1100896 i++, sfep = dp->d_ops->sf_nextentry(sfp, sfep)) {
Barry Naujok5163f952008-05-21 16:41:01 +1000897 if (xfs_da_compname(args, sfep->name, sfep->namelen) ==
898 XFS_CMP_EXACT) {
Dave Chinner47401752013-10-29 22:11:47 +1100899 ASSERT(dp->d_ops->sf_get_ino(sfp, sfep) ==
Christoph Hellwig8bc38782011-07-08 14:35:03 +0200900 args->inumber);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 break;
902 }
903 }
904 /*
905 * Didn't find it.
906 */
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200907 if (i == sfp->count)
Dave Chinner24513372014-06-25 14:58:08 +1000908 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 /*
910 * Calculate sizes.
911 */
912 byteoff = (int)((char *)sfep - (char *)sfp);
Dave Chinner32c54832013-10-29 22:11:46 +1100913 entsize = dp->d_ops->sf_entsize(sfp, args->namelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 newsize = oldsize - entsize;
915 /*
916 * Copy the part if any after the removed entry, sliding it down.
917 */
918 if (byteoff + entsize < oldsize)
919 memmove((char *)sfp + byteoff, (char *)sfp + byteoff + entsize,
920 oldsize - (byteoff + entsize));
921 /*
922 * Fix up the header and file size.
923 */
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200924 sfp->count--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 dp->i_d.di_size = newsize;
926 /*
927 * Reallocate, making it smaller.
928 */
929 xfs_idata_realloc(dp, newsize - oldsize, XFS_DATA_FORK);
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200930 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 /*
932 * Are we changing inode number size?
933 */
934 if (args->inumber > XFS_DIR2_MAX_SHORT_INUM) {
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200935 if (sfp->i8count == 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 xfs_dir2_sf_toino4(args);
937 else
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200938 sfp->i8count--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 xfs_dir2_sf_check(args);
941 xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE | XFS_ILOG_DDATA);
942 return 0;
943}
944
945/*
946 * Replace the inode number of an entry in a shortform directory.
947 */
948int /* error */
949xfs_dir2_sf_replace(
950 xfs_da_args_t *args) /* operation arguments */
951{
952 xfs_inode_t *dp; /* incore directory inode */
953 int i; /* entry index */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 xfs_ino_t ino=0; /* entry old inode number */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 int i8elevated; /* sf_toino8 set i8count=1 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 xfs_dir2_sf_entry_t *sfep; /* shortform directory entry */
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200957 xfs_dir2_sf_hdr_t *sfp; /* shortform structure */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000959 trace_xfs_dir2_sf_replace(args);
960
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 dp = args->dp;
962
963 ASSERT(dp->i_df.if_flags & XFS_IFINLINE);
964 /*
965 * Bail out if the shortform directory is way too small.
966 */
967 if (dp->i_d.di_size < offsetof(xfs_dir2_sf_hdr_t, parent)) {
968 ASSERT(XFS_FORCED_SHUTDOWN(dp->i_mount));
Dave Chinner24513372014-06-25 14:58:08 +1000969 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 }
971 ASSERT(dp->i_df.if_bytes == dp->i_d.di_size);
972 ASSERT(dp->i_df.if_u1.if_data != NULL);
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200973 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
974 ASSERT(dp->i_d.di_size >= xfs_dir2_sf_hdr_size(sfp->i8count));
Christoph Hellwigd5cf09b2014-07-30 09:12:05 +1000975
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 /*
977 * New inode number is large, and need to convert to 8-byte inodes.
978 */
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200979 if (args->inumber > XFS_DIR2_MAX_SHORT_INUM && sfp->i8count == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 int error; /* error return value */
981 int newsize; /* new inode size */
982
Christoph Hellwig266b6962016-07-20 11:48:31 +1000983 newsize = dp->i_df.if_bytes + (sfp->count + 1) * XFS_INO64_DIFF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 /*
985 * Won't fit as shortform, convert to block then do replace.
986 */
987 if (newsize > XFS_IFORK_DSIZE(dp)) {
988 error = xfs_dir2_sf_to_block(args);
989 if (error) {
990 return error;
991 }
992 return xfs_dir2_block_replace(args);
993 }
994 /*
995 * Still fits, convert to 8-byte now.
996 */
997 xfs_dir2_sf_toino8(args);
998 i8elevated = 1;
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200999 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 } else
1001 i8elevated = 0;
Christoph Hellwigd5cf09b2014-07-30 09:12:05 +10001002
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 ASSERT(args->namelen != 1 || args->name[0] != '.');
1004 /*
1005 * Replace ..'s entry.
1006 */
1007 if (args->namelen == 2 &&
1008 args->name[0] == '.' && args->name[1] == '.') {
Dave Chinner47401752013-10-29 22:11:47 +11001009 ino = dp->d_ops->sf_get_parent_ino(sfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 ASSERT(args->inumber != ino);
Dave Chinner47401752013-10-29 22:11:47 +11001011 dp->d_ops->sf_put_parent_ino(sfp, args->inumber);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 }
1013 /*
1014 * Normal entry, look for the name.
1015 */
1016 else {
Dave Chinner0cb97762013-08-12 20:50:09 +10001017 for (i = 0, sfep = xfs_dir2_sf_firstentry(sfp); i < sfp->count;
Dave Chinner32c54832013-10-29 22:11:46 +11001018 i++, sfep = dp->d_ops->sf_nextentry(sfp, sfep)) {
Barry Naujok5163f952008-05-21 16:41:01 +10001019 if (xfs_da_compname(args, sfep->name, sfep->namelen) ==
1020 XFS_CMP_EXACT) {
Dave Chinner47401752013-10-29 22:11:47 +11001021 ino = dp->d_ops->sf_get_ino(sfp, sfep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 ASSERT(args->inumber != ino);
Dave Chinner47401752013-10-29 22:11:47 +11001023 dp->d_ops->sf_put_ino(sfp, sfep, args->inumber);
1024 dp->d_ops->sf_put_ftype(sfep, args->filetype);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 break;
1026 }
1027 }
1028 /*
1029 * Didn't find it.
1030 */
Christoph Hellwigac8ba502011-07-08 14:35:13 +02001031 if (i == sfp->count) {
Barry Naujok6a178102008-05-21 16:42:05 +10001032 ASSERT(args->op_flags & XFS_DA_OP_OKNOENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 if (i8elevated)
1034 xfs_dir2_sf_toino4(args);
Dave Chinner24513372014-06-25 14:58:08 +10001035 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 }
1037 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 /*
1039 * See if the old number was large, the new number is small.
1040 */
1041 if (ino > XFS_DIR2_MAX_SHORT_INUM &&
1042 args->inumber <= XFS_DIR2_MAX_SHORT_INUM) {
1043 /*
1044 * And the old count was one, so need to convert to small.
1045 */
Christoph Hellwigac8ba502011-07-08 14:35:13 +02001046 if (sfp->i8count == 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 xfs_dir2_sf_toino4(args);
1048 else
Christoph Hellwigac8ba502011-07-08 14:35:13 +02001049 sfp->i8count--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 }
1051 /*
1052 * See if the old number was small, the new number is large.
1053 */
1054 if (ino <= XFS_DIR2_MAX_SHORT_INUM &&
1055 args->inumber > XFS_DIR2_MAX_SHORT_INUM) {
1056 /*
1057 * add to the i8count unless we just converted to 8-byte
1058 * inodes (which does an implied i8count = 1)
1059 */
Christoph Hellwigac8ba502011-07-08 14:35:13 +02001060 ASSERT(sfp->i8count != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 if (!i8elevated)
Christoph Hellwigac8ba502011-07-08 14:35:13 +02001062 sfp->i8count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 xfs_dir2_sf_check(args);
1065 xfs_trans_log_inode(args->trans, dp, XFS_ILOG_DDATA);
1066 return 0;
1067}
1068
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069/*
1070 * Convert from 8-byte inode numbers to 4-byte inode numbers.
1071 * The last 8-byte inode number is gone, but the count is still 1.
1072 */
1073static void
1074xfs_dir2_sf_toino4(
1075 xfs_da_args_t *args) /* operation arguments */
1076{
1077 char *buf; /* old dir's buffer */
1078 xfs_inode_t *dp; /* incore directory inode */
1079 int i; /* entry index */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 int newsize; /* new inode size */
1081 xfs_dir2_sf_entry_t *oldsfep; /* old sf entry */
Christoph Hellwigac8ba502011-07-08 14:35:13 +02001082 xfs_dir2_sf_hdr_t *oldsfp; /* old sf directory */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 int oldsize; /* old inode size */
1084 xfs_dir2_sf_entry_t *sfep; /* new sf entry */
Christoph Hellwigac8ba502011-07-08 14:35:13 +02001085 xfs_dir2_sf_hdr_t *sfp; /* new sf directory */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001087 trace_xfs_dir2_sf_toino4(args);
1088
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 dp = args->dp;
1090
1091 /*
1092 * Copy the old directory to the buffer.
1093 * Then nuke it from the inode, and add the new buffer to the inode.
1094 * Don't want xfs_idata_realloc copying the data here.
1095 */
1096 oldsize = dp->i_df.if_bytes;
1097 buf = kmem_alloc(oldsize, KM_SLEEP);
Christoph Hellwigac8ba502011-07-08 14:35:13 +02001098 oldsfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
1099 ASSERT(oldsfp->i8count == 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 memcpy(buf, oldsfp, oldsize);
1101 /*
1102 * Compute the new inode size.
1103 */
Christoph Hellwig266b6962016-07-20 11:48:31 +10001104 newsize = oldsize - (oldsfp->count + 1) * XFS_INO64_DIFF;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 xfs_idata_realloc(dp, -oldsize, XFS_DATA_FORK);
1106 xfs_idata_realloc(dp, newsize, XFS_DATA_FORK);
1107 /*
1108 * Reset our pointers, the data has moved.
1109 */
Christoph Hellwigac8ba502011-07-08 14:35:13 +02001110 oldsfp = (xfs_dir2_sf_hdr_t *)buf;
1111 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 /*
1113 * Fill in the new header.
1114 */
Christoph Hellwigac8ba502011-07-08 14:35:13 +02001115 sfp->count = oldsfp->count;
1116 sfp->i8count = 0;
Dave Chinner47401752013-10-29 22:11:47 +11001117 dp->d_ops->sf_put_parent_ino(sfp, dp->d_ops->sf_get_parent_ino(oldsfp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 /*
1119 * Copy the entries field by field.
1120 */
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001121 for (i = 0, sfep = xfs_dir2_sf_firstentry(sfp),
1122 oldsfep = xfs_dir2_sf_firstentry(oldsfp);
Christoph Hellwigac8ba502011-07-08 14:35:13 +02001123 i < sfp->count;
Dave Chinner32c54832013-10-29 22:11:46 +11001124 i++, sfep = dp->d_ops->sf_nextentry(sfp, sfep),
1125 oldsfep = dp->d_ops->sf_nextentry(oldsfp, oldsfep)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 sfep->namelen = oldsfep->namelen;
Christoph Hellwig8353a642016-07-20 11:47:21 +10001127 memcpy(sfep->offset, oldsfep->offset, sizeof(sfep->offset));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 memcpy(sfep->name, oldsfep->name, sfep->namelen);
Dave Chinner47401752013-10-29 22:11:47 +11001129 dp->d_ops->sf_put_ino(sfp, sfep,
1130 dp->d_ops->sf_get_ino(oldsfp, oldsfep));
1131 dp->d_ops->sf_put_ftype(sfep, dp->d_ops->sf_get_ftype(oldsfep));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 }
1133 /*
1134 * Clean up the inode.
1135 */
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10001136 kmem_free(buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137 dp->i_d.di_size = newsize;
1138 xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE | XFS_ILOG_DDATA);
1139}
1140
1141/*
Eric Sandeen5e06d142014-04-14 19:07:23 +10001142 * Convert existing entries from 4-byte inode numbers to 8-byte inode numbers.
1143 * The new entry w/ an 8-byte inode number is not there yet; we leave with
1144 * i8count set to 1, but no corresponding 8-byte entry.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 */
1146static void
1147xfs_dir2_sf_toino8(
1148 xfs_da_args_t *args) /* operation arguments */
1149{
1150 char *buf; /* old dir's buffer */
1151 xfs_inode_t *dp; /* incore directory inode */
1152 int i; /* entry index */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 int newsize; /* new inode size */
1154 xfs_dir2_sf_entry_t *oldsfep; /* old sf entry */
Christoph Hellwigac8ba502011-07-08 14:35:13 +02001155 xfs_dir2_sf_hdr_t *oldsfp; /* old sf directory */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 int oldsize; /* old inode size */
1157 xfs_dir2_sf_entry_t *sfep; /* new sf entry */
Christoph Hellwigac8ba502011-07-08 14:35:13 +02001158 xfs_dir2_sf_hdr_t *sfp; /* new sf directory */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001160 trace_xfs_dir2_sf_toino8(args);
1161
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 dp = args->dp;
1163
1164 /*
1165 * Copy the old directory to the buffer.
1166 * Then nuke it from the inode, and add the new buffer to the inode.
1167 * Don't want xfs_idata_realloc copying the data here.
1168 */
1169 oldsize = dp->i_df.if_bytes;
1170 buf = kmem_alloc(oldsize, KM_SLEEP);
Christoph Hellwigac8ba502011-07-08 14:35:13 +02001171 oldsfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
1172 ASSERT(oldsfp->i8count == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 memcpy(buf, oldsfp, oldsize);
1174 /*
Eric Sandeen5e06d142014-04-14 19:07:23 +10001175 * Compute the new inode size (nb: entry count + 1 for parent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 */
Christoph Hellwig266b6962016-07-20 11:48:31 +10001177 newsize = oldsize + (oldsfp->count + 1) * XFS_INO64_DIFF;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 xfs_idata_realloc(dp, -oldsize, XFS_DATA_FORK);
1179 xfs_idata_realloc(dp, newsize, XFS_DATA_FORK);
1180 /*
1181 * Reset our pointers, the data has moved.
1182 */
Christoph Hellwigac8ba502011-07-08 14:35:13 +02001183 oldsfp = (xfs_dir2_sf_hdr_t *)buf;
1184 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 /*
1186 * Fill in the new header.
1187 */
Christoph Hellwigac8ba502011-07-08 14:35:13 +02001188 sfp->count = oldsfp->count;
1189 sfp->i8count = 1;
Dave Chinner47401752013-10-29 22:11:47 +11001190 dp->d_ops->sf_put_parent_ino(sfp, dp->d_ops->sf_get_parent_ino(oldsfp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 /*
1192 * Copy the entries field by field.
1193 */
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001194 for (i = 0, sfep = xfs_dir2_sf_firstentry(sfp),
1195 oldsfep = xfs_dir2_sf_firstentry(oldsfp);
Christoph Hellwigac8ba502011-07-08 14:35:13 +02001196 i < sfp->count;
Dave Chinner32c54832013-10-29 22:11:46 +11001197 i++, sfep = dp->d_ops->sf_nextentry(sfp, sfep),
1198 oldsfep = dp->d_ops->sf_nextentry(oldsfp, oldsfep)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199 sfep->namelen = oldsfep->namelen;
Christoph Hellwig8353a642016-07-20 11:47:21 +10001200 memcpy(sfep->offset, oldsfep->offset, sizeof(sfep->offset));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201 memcpy(sfep->name, oldsfep->name, sfep->namelen);
Dave Chinner47401752013-10-29 22:11:47 +11001202 dp->d_ops->sf_put_ino(sfp, sfep,
1203 dp->d_ops->sf_get_ino(oldsfp, oldsfep));
1204 dp->d_ops->sf_put_ftype(sfep, dp->d_ops->sf_get_ftype(oldsfep));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 }
1206 /*
1207 * Clean up the inode.
1208 */
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10001209 kmem_free(buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210 dp->i_d.di_size = newsize;
1211 xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE | XFS_ILOG_DDATA);
1212}