blob: aafc6e46cb5803620998399bea5da520aa720f83 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Nathan Scott7b718762005-11-02 14:58:39 +11002 * Copyright (c) 2000-2003,2005 Silicon Graphics, Inc.
3 * All Rights Reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
Nathan Scott7b718762005-11-02 14:58:39 +11005 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * published by the Free Software Foundation.
8 *
Nathan Scott7b718762005-11-02 14:58:39 +11009 * This program is distributed in the hope that it would be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 *
Nathan Scott7b718762005-11-02 14:58:39 +110014 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write the Free Software Foundation,
16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
Linus Torvalds1da177e2005-04-16 15:20:36 -070017 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include "xfs.h"
Nathan Scotta844f452005-11-02 14:38:42 +110019#include "xfs_fs.h"
Dave Chinnera4fbe6a2013-10-23 10:51:50 +110020#include "xfs_format.h"
Dave Chinner239880e2013-10-23 10:50:10 +110021#include "xfs_log_format.h"
22#include "xfs_trans_resv.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include "xfs_sb.h"
David Chinnerda353b02007-08-28 14:00:13 +100024#include "xfs_ag.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include "xfs_mount.h"
Dave Chinner57062782013-10-15 09:17:51 +110026#include "xfs_da_format.h"
Nathan Scotta844f452005-11-02 14:38:42 +110027#include "xfs_da_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include "xfs_inode.h"
Dave Chinner239880e2013-10-23 10:50:10 +110029#include "xfs_trans.h"
Nathan Scotta844f452005-11-02 14:38:42 +110030#include "xfs_inode_item.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include "xfs_error.h"
Dave Chinner2b9ab5a2013-08-12 20:49:37 +100032#include "xfs_dir2.h"
Christoph Hellwig57926642011-07-13 13:43:48 +020033#include "xfs_dir2_priv.h"
Christoph Hellwig0b1b2132009-12-14 23:14:59 +000034#include "xfs_trace.h"
Dave Chinnera4fbe6a2013-10-23 10:51:50 +110035#include "xfs_dinode.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
37/*
38 * Prototypes for internal functions.
39 */
40static void xfs_dir2_sf_addname_easy(xfs_da_args_t *args,
41 xfs_dir2_sf_entry_t *sfep,
42 xfs_dir2_data_aoff_t offset,
43 int new_isize);
44static void xfs_dir2_sf_addname_hard(xfs_da_args_t *args, int objchange,
45 int new_isize);
46static int xfs_dir2_sf_addname_pick(xfs_da_args_t *args, int objchange,
47 xfs_dir2_sf_entry_t **sfepp,
48 xfs_dir2_data_aoff_t *offsetp);
49#ifdef DEBUG
50static void xfs_dir2_sf_check(xfs_da_args_t *args);
51#else
52#define xfs_dir2_sf_check(args)
53#endif /* DEBUG */
54#if XFS_BIG_INUMS
55static void xfs_dir2_sf_toino4(xfs_da_args_t *args);
56static void xfs_dir2_sf_toino8(xfs_da_args_t *args);
57#endif /* XFS_BIG_INUMS */
58
Christoph Hellwig8bc38782011-07-08 14:35:03 +020059/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070060 * Given a block directory (dp/block), calculate its size as a shortform (sf)
61 * directory and a header for the sf directory, if it will fit it the
62 * space currently present in the inode. If it won't fit, the output
63 * size is too big (but not accurate).
64 */
65int /* size for sf form */
66xfs_dir2_block_sfsize(
67 xfs_inode_t *dp, /* incore inode pointer */
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +020068 xfs_dir2_data_hdr_t *hdr, /* block directory data */
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 xfs_dir2_sf_hdr_t *sfhp) /* output: header for sf form */
70{
71 xfs_dir2_dataptr_t addr; /* data entry address */
72 xfs_dir2_leaf_entry_t *blp; /* leaf area of the block */
73 xfs_dir2_block_tail_t *btp; /* tail area of the block */
74 int count; /* shortform entry count */
75 xfs_dir2_data_entry_t *dep; /* data entry in the block */
76 int i; /* block entry index */
77 int i8count; /* count of big-inode entries */
78 int isdot; /* entry is "." */
79 int isdotdot; /* entry is ".." */
80 xfs_mount_t *mp; /* mount structure pointer */
81 int namelen; /* total name bytes */
Christoph Hellwig5bde1ba92005-11-02 15:06:18 +110082 xfs_ino_t parent = 0; /* parent inode number */
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 int size=0; /* total computed size */
Dave Chinner0cb97762013-08-12 20:50:09 +100084 int has_ftype;
Linus Torvalds1da177e2005-04-16 15:20:36 -070085
86 mp = dp->i_mount;
87
Dave Chinner0cb97762013-08-12 20:50:09 +100088 /*
89 * if there is a filetype field, add the extra byte to the namelen
90 * for each entry that we see.
91 */
92 has_ftype = xfs_sb_version_hasftype(&mp->m_sb) ? 1 : 0;
93
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 count = i8count = namelen = 0;
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +020095 btp = xfs_dir2_block_tail_p(mp, hdr);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +100096 blp = xfs_dir2_block_leaf_p(btp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070097
98 /*
99 * Iterate over the block's data entries by using the leaf pointers.
100 */
Nathan Scotte922fff2006-03-17 17:27:56 +1100101 for (i = 0; i < be32_to_cpu(btp->count); i++) {
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100102 if ((addr = be32_to_cpu(blp[i].address)) == XFS_DIR2_NULL_DATAPTR)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 continue;
104 /*
105 * Calculate the pointer to the entry at hand.
106 */
107 dep = (xfs_dir2_data_entry_t *)
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +0200108 ((char *)hdr + xfs_dir2_dataptr_to_off(mp, addr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 /*
110 * Detect . and .., so we can special-case them.
111 * . is not included in sf directories.
112 * .. is included by just the parent inode number.
113 */
114 isdot = dep->namelen == 1 && dep->name[0] == '.';
115 isdotdot =
116 dep->namelen == 2 &&
117 dep->name[0] == '.' && dep->name[1] == '.';
118#if XFS_BIG_INUMS
119 if (!isdot)
Christoph Hellwigff9901c2006-06-09 14:48:37 +1000120 i8count += be64_to_cpu(dep->inumber) > XFS_DIR2_MAX_SHORT_INUM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121#endif
Dave Chinner0cb97762013-08-12 20:50:09 +1000122 /* take into account the file type field */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 if (!isdot && !isdotdot) {
124 count++;
Dave Chinner0cb97762013-08-12 20:50:09 +1000125 namelen += dep->namelen + has_ftype;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 } else if (isdotdot)
Christoph Hellwigff9901c2006-06-09 14:48:37 +1000127 parent = be64_to_cpu(dep->inumber);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 /*
129 * Calculate the new size, see if we should give up yet.
130 */
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000131 size = xfs_dir2_sf_hdr_size(i8count) + /* header */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 count + /* namelen */
133 count * (uint)sizeof(xfs_dir2_sf_off_t) + /* offset */
134 namelen + /* name */
135 (i8count ? /* inumber */
136 (uint)sizeof(xfs_dir2_ino8_t) * count :
137 (uint)sizeof(xfs_dir2_ino4_t) * count);
138 if (size > XFS_IFORK_DSIZE(dp))
139 return size; /* size value is a failure */
140 }
141 /*
142 * Create the output header, if it worked.
143 */
144 sfhp->count = count;
145 sfhp->i8count = i8count;
Dave Chinner47401752013-10-29 22:11:47 +1100146 dp->d_ops->sf_put_parent_ino(sfhp, parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 return size;
148}
149
150/*
151 * Convert a block format directory to shortform.
152 * Caller has already checked that it will fit, and built us a header.
153 */
154int /* error */
155xfs_dir2_block_to_sf(
156 xfs_da_args_t *args, /* operation arguments */
Dave Chinner1d9025e2012-06-22 18:50:14 +1000157 struct xfs_buf *bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 int size, /* shortform directory size */
159 xfs_dir2_sf_hdr_t *sfhp) /* shortform directory hdr */
160{
Christoph Hellwiga64b0412011-07-08 14:35:32 +0200161 xfs_dir2_data_hdr_t *hdr; /* block header */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 xfs_dir2_block_tail_t *btp; /* block tail pointer */
163 xfs_dir2_data_entry_t *dep; /* data entry pointer */
164 xfs_inode_t *dp; /* incore directory inode */
165 xfs_dir2_data_unused_t *dup; /* unused data pointer */
166 char *endptr; /* end of data entries */
167 int error; /* error return value */
168 int logflags; /* inode logging flags */
169 xfs_mount_t *mp; /* filesystem mount point */
170 char *ptr; /* current data pointer */
171 xfs_dir2_sf_entry_t *sfep; /* shortform entry */
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200172 xfs_dir2_sf_hdr_t *sfp; /* shortform directory header */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000174 trace_xfs_dir2_block_to_sf(args);
175
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 dp = args->dp;
177 mp = dp->i_mount;
178
179 /*
180 * Make a copy of the block data, so we can shrink the inode
181 * and add local data.
182 */
Christoph Hellwiga64b0412011-07-08 14:35:32 +0200183 hdr = kmem_alloc(mp->m_dirblksize, KM_SLEEP);
Dave Chinner1d9025e2012-06-22 18:50:14 +1000184 memcpy(hdr, bp->b_addr, mp->m_dirblksize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 logflags = XFS_ILOG_CORE;
186 if ((error = xfs_dir2_shrink_inode(args, mp->m_dirdatablk, bp))) {
187 ASSERT(error != ENOSPC);
188 goto out;
189 }
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +0200190
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 /*
192 * The buffer is now unconditionally gone, whether
193 * xfs_dir2_shrink_inode worked or not.
194 *
195 * Convert the inode to local format.
196 */
197 dp->i_df.if_flags &= ~XFS_IFEXTENTS;
198 dp->i_df.if_flags |= XFS_IFINLINE;
199 dp->i_d.di_format = XFS_DINODE_FMT_LOCAL;
200 ASSERT(dp->i_df.if_bytes == 0);
201 xfs_idata_realloc(dp, size, XFS_DATA_FORK);
202 logflags |= XFS_ILOG_DDATA;
203 /*
204 * Copy the header into the newly allocate local space.
205 */
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200206 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000207 memcpy(sfp, sfhp, xfs_dir2_sf_hdr_size(sfhp->i8count));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 dp->i_d.di_size = size;
209 /*
210 * Set up to loop over the block's entries.
211 */
Christoph Hellwiga64b0412011-07-08 14:35:32 +0200212 btp = xfs_dir2_block_tail_p(mp, hdr);
Dave Chinner2ca98772013-10-29 22:11:49 +1100213 ptr = (char *)dp->d_ops->data_entry_p(hdr);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000214 endptr = (char *)xfs_dir2_block_leaf_p(btp);
215 sfep = xfs_dir2_sf_firstentry(sfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 /*
217 * Loop over the active and unused entries.
218 * Stop when we reach the leaf/tail portion of the block.
219 */
220 while (ptr < endptr) {
221 /*
222 * If it's unused, just skip over it.
223 */
224 dup = (xfs_dir2_data_unused_t *)ptr;
Nathan Scottad354eb2006-03-17 17:27:37 +1100225 if (be16_to_cpu(dup->freetag) == XFS_DIR2_DATA_FREE_TAG) {
226 ptr += be16_to_cpu(dup->length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 continue;
228 }
229 dep = (xfs_dir2_data_entry_t *)ptr;
230 /*
231 * Skip .
232 */
233 if (dep->namelen == 1 && dep->name[0] == '.')
Christoph Hellwigff9901c2006-06-09 14:48:37 +1000234 ASSERT(be64_to_cpu(dep->inumber) == dp->i_ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 /*
236 * Skip .., but make sure the inode number is right.
237 */
238 else if (dep->namelen == 2 &&
239 dep->name[0] == '.' && dep->name[1] == '.')
Christoph Hellwigff9901c2006-06-09 14:48:37 +1000240 ASSERT(be64_to_cpu(dep->inumber) ==
Dave Chinner47401752013-10-29 22:11:47 +1100241 dp->d_ops->sf_get_parent_ino(sfp));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 /*
243 * Normal entry, copy it into shortform.
244 */
245 else {
246 sfep->namelen = dep->namelen;
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000247 xfs_dir2_sf_put_offset(sfep,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 (xfs_dir2_data_aoff_t)
Christoph Hellwiga64b0412011-07-08 14:35:32 +0200249 ((char *)dep - (char *)hdr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 memcpy(sfep->name, dep->name, dep->namelen);
Dave Chinner47401752013-10-29 22:11:47 +1100251 dp->d_ops->sf_put_ino(sfp, sfep,
252 be64_to_cpu(dep->inumber));
253 dp->d_ops->sf_put_ftype(sfep,
Dave Chinner9d23fc82013-10-29 22:11:48 +1100254 dp->d_ops->data_get_ftype(dep));
Christoph Hellwig8bc38782011-07-08 14:35:03 +0200255
Dave Chinner32c54832013-10-29 22:11:46 +1100256 sfep = dp->d_ops->sf_nextentry(sfp, sfep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 }
Dave Chinner9d23fc82013-10-29 22:11:48 +1100258 ptr += dp->d_ops->data_entsize(dep->namelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 }
260 ASSERT((char *)sfep - (char *)sfp == size);
261 xfs_dir2_sf_check(args);
262out:
263 xfs_trans_log_inode(args->trans, dp, logflags);
Christoph Hellwiga64b0412011-07-08 14:35:32 +0200264 kmem_free(hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 return error;
266}
267
268/*
269 * Add a name to a shortform directory.
270 * There are two algorithms, "easy" and "hard" which we decide on
271 * before changing anything.
272 * Convert to block form if necessary, if the new entry won't fit.
273 */
274int /* error */
275xfs_dir2_sf_addname(
276 xfs_da_args_t *args) /* operation arguments */
277{
278 int add_entsize; /* size of the new entry */
279 xfs_inode_t *dp; /* incore directory inode */
280 int error; /* error return value */
281 int incr_isize; /* total change in size */
282 int new_isize; /* di_size after adding name */
283 int objchange; /* changing to 8-byte inodes */
Christoph Hellwig5bde1ba92005-11-02 15:06:18 +1100284 xfs_dir2_data_aoff_t offset = 0; /* offset for new entry */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 int old_isize; /* di_size before adding name */
286 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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 ASSERT(xfs_dir2_sf_lookup(args) == ENOENT);
293 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));
300 return XFS_ERROR(EIO);
301 }
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 */
Dave Chinner32c54832013-10-29 22:11:46 +1100309 add_entsize = dp->d_ops->sf_entsize(sfp, args->namelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 incr_isize = add_entsize;
311 objchange = 0;
312#if XFS_BIG_INUMS
313 /*
314 * Do we have to change to 8 byte inodes?
315 */
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200316 if (args->inumber > XFS_DIR2_MAX_SHORT_INUM && sfp->i8count == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 /*
318 * Yes, adjust the entry size and the total size.
319 */
320 add_entsize +=
321 (uint)sizeof(xfs_dir2_ino8_t) -
322 (uint)sizeof(xfs_dir2_ino4_t);
323 incr_isize +=
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200324 (sfp->count + 2) *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 ((uint)sizeof(xfs_dir2_ino8_t) -
326 (uint)sizeof(xfs_dir2_ino4_t));
327 objchange = 1;
328 }
329#endif
330 old_isize = (int)dp->i_d.di_size;
331 new_isize = old_isize + incr_isize;
332 /*
333 * Won't fit as shortform any more (due to size),
334 * or the pick routine says it won't (due to offset values).
335 */
336 if (new_isize > XFS_IFORK_DSIZE(dp) ||
337 (pick =
338 xfs_dir2_sf_addname_pick(args, objchange, &sfep, &offset)) == 0) {
339 /*
340 * Just checking or no space reservation, it doesn't fit.
341 */
Barry Naujok6a178102008-05-21 16:42:05 +1000342 if ((args->op_flags & XFS_DA_OP_JUSTCHECK) || args->total == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 return XFS_ERROR(ENOSPC);
344 /*
345 * Convert to block form then add the name.
346 */
347 error = xfs_dir2_sf_to_block(args);
348 if (error)
349 return error;
350 return xfs_dir2_block_addname(args);
351 }
352 /*
353 * Just checking, it fits.
354 */
Barry Naujok6a178102008-05-21 16:42:05 +1000355 if (args->op_flags & XFS_DA_OP_JUSTCHECK)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 return 0;
357 /*
358 * Do it the easy way - just add it at the end.
359 */
360 if (pick == 1)
361 xfs_dir2_sf_addname_easy(args, sfep, offset, new_isize);
362 /*
363 * Do it the hard way - look for a place to insert the new entry.
364 * Convert to 8 byte inode numbers first if necessary.
365 */
366 else {
367 ASSERT(pick == 2);
368#if XFS_BIG_INUMS
369 if (objchange)
370 xfs_dir2_sf_toino8(args);
371#endif
372 xfs_dir2_sf_addname_hard(args, objchange, new_isize);
373 }
374 xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE | XFS_ILOG_DDATA);
375 return 0;
376}
377
378/*
379 * Add the new entry the "easy" way.
380 * This is copying the old directory and adding the new entry at the end.
381 * Since it's sorted by "offset" we need room after the last offset
382 * that's already there, and then room to convert to a block directory.
383 * This is already checked by the pick routine.
384 */
385static void
386xfs_dir2_sf_addname_easy(
387 xfs_da_args_t *args, /* operation arguments */
388 xfs_dir2_sf_entry_t *sfep, /* pointer to new entry */
389 xfs_dir2_data_aoff_t offset, /* offset to use for new ent */
390 int new_isize) /* new directory size */
391{
392 int byteoff; /* byte offset in sf dir */
393 xfs_inode_t *dp; /* incore directory inode */
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200394 xfs_dir2_sf_hdr_t *sfp; /* shortform structure */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395
396 dp = args->dp;
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 byteoff = (int)((char *)sfep - (char *)sfp);
400 /*
401 * Grow the in-inode space.
402 */
Dave Chinner32c54832013-10-29 22:11:46 +1100403 xfs_idata_realloc(dp, dp->d_ops->sf_entsize(sfp, args->namelen),
Dave Chinner0cb97762013-08-12 20:50:09 +1000404 XFS_DATA_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 /*
406 * Need to set up again due to realloc of the inode data.
407 */
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200408 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 sfep = (xfs_dir2_sf_entry_t *)((char *)sfp + byteoff);
410 /*
411 * Fill in the new entry.
412 */
413 sfep->namelen = args->namelen;
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000414 xfs_dir2_sf_put_offset(sfep, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 memcpy(sfep->name, args->name, sfep->namelen);
Dave Chinner47401752013-10-29 22:11:47 +1100416 dp->d_ops->sf_put_ino(sfp, sfep, args->inumber);
417 dp->d_ops->sf_put_ftype(sfep, args->filetype);
Dave Chinner1c55cec2013-08-12 20:50:10 +1000418
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 /*
420 * Update the header and inode.
421 */
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200422 sfp->count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423#if XFS_BIG_INUMS
424 if (args->inumber > XFS_DIR2_MAX_SHORT_INUM)
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200425 sfp->i8count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426#endif
427 dp->i_d.di_size = new_isize;
428 xfs_dir2_sf_check(args);
429}
430
431/*
432 * Add the new entry the "hard" way.
433 * The caller has already converted to 8 byte inode numbers if necessary,
434 * in which case we need to leave the i8count at 1.
435 * Find a hole that the new entry will fit into, and copy
436 * the first part of the entries, the new entry, and the last part of
437 * the entries.
438 */
439/* ARGSUSED */
440static void
441xfs_dir2_sf_addname_hard(
442 xfs_da_args_t *args, /* operation arguments */
443 int objchange, /* changing inode number size */
444 int new_isize) /* new directory size */
445{
446 int add_datasize; /* data size need for new ent */
447 char *buf; /* buffer for old */
448 xfs_inode_t *dp; /* incore directory inode */
449 int eof; /* reached end of old dir */
450 int nbytes; /* temp for byte copies */
451 xfs_dir2_data_aoff_t new_offset; /* next offset value */
452 xfs_dir2_data_aoff_t offset; /* current offset value */
453 int old_isize; /* previous di_size */
454 xfs_dir2_sf_entry_t *oldsfep; /* entry in original dir */
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200455 xfs_dir2_sf_hdr_t *oldsfp; /* original shortform dir */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 xfs_dir2_sf_entry_t *sfep; /* entry in new dir */
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200457 xfs_dir2_sf_hdr_t *sfp; /* new shortform dir */
Dave Chinner0cb97762013-08-12 20:50:09 +1000458 struct xfs_mount *mp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459
460 /*
461 * Copy the old directory to the stack buffer.
462 */
463 dp = args->dp;
Dave Chinner0cb97762013-08-12 20:50:09 +1000464 mp = dp->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200466 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 old_isize = (int)dp->i_d.di_size;
468 buf = kmem_alloc(old_isize, KM_SLEEP);
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200469 oldsfp = (xfs_dir2_sf_hdr_t *)buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 memcpy(oldsfp, sfp, old_isize);
471 /*
472 * Loop over the old directory finding the place we're going
473 * to insert the new entry.
474 * If it's going to end up at the end then oldsfep will point there.
475 */
Dave Chinner1c9a5b22013-10-30 09:15:02 +1100476 for (offset = dp->d_ops->data_first_offset,
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000477 oldsfep = xfs_dir2_sf_firstentry(oldsfp),
Dave Chinner9d23fc82013-10-29 22:11:48 +1100478 add_datasize = dp->d_ops->data_entsize(args->namelen),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 eof = (char *)oldsfep == &buf[old_isize];
480 !eof;
Dave Chinner9d23fc82013-10-29 22:11:48 +1100481 offset = new_offset + dp->d_ops->data_entsize(oldsfep->namelen),
Dave Chinner32c54832013-10-29 22:11:46 +1100482 oldsfep = dp->d_ops->sf_nextentry(oldsfp, oldsfep),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 eof = (char *)oldsfep == &buf[old_isize]) {
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000484 new_offset = xfs_dir2_sf_get_offset(oldsfep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 if (offset + add_datasize <= new_offset)
486 break;
487 }
488 /*
489 * Get rid of the old directory, then allocate space for
490 * the new one. We do this so xfs_idata_realloc won't copy
491 * the data.
492 */
493 xfs_idata_realloc(dp, -old_isize, XFS_DATA_FORK);
494 xfs_idata_realloc(dp, new_isize, XFS_DATA_FORK);
495 /*
496 * Reset the pointer since the buffer was reallocated.
497 */
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200498 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 /*
500 * Copy the first part of the directory, including the header.
501 */
502 nbytes = (int)((char *)oldsfep - (char *)oldsfp);
503 memcpy(sfp, oldsfp, nbytes);
504 sfep = (xfs_dir2_sf_entry_t *)((char *)sfp + nbytes);
505 /*
506 * Fill in the new entry, and update the header counts.
507 */
508 sfep->namelen = args->namelen;
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000509 xfs_dir2_sf_put_offset(sfep, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 memcpy(sfep->name, args->name, sfep->namelen);
Dave Chinner47401752013-10-29 22:11:47 +1100511 dp->d_ops->sf_put_ino(sfp, sfep, args->inumber);
512 dp->d_ops->sf_put_ftype(sfep, args->filetype);
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200513 sfp->count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514#if XFS_BIG_INUMS
515 if (args->inumber > XFS_DIR2_MAX_SHORT_INUM && !objchange)
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200516 sfp->i8count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517#endif
518 /*
519 * If there's more left to copy, do that.
520 */
521 if (!eof) {
Dave Chinner32c54832013-10-29 22:11:46 +1100522 sfep = dp->d_ops->sf_nextentry(sfp, sfep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 memcpy(sfep, oldsfep, old_isize - nbytes);
524 }
Denys Vlasenkof0e2d932008-05-19 16:31:57 +1000525 kmem_free(buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 dp->i_d.di_size = new_isize;
527 xfs_dir2_sf_check(args);
528}
529
530/*
531 * Decide if the new entry will fit at all.
532 * If it will fit, pick between adding the new entry to the end (easy)
533 * or somewhere else (hard).
534 * Return 0 (won't fit), 1 (easy), 2 (hard).
535 */
536/*ARGSUSED*/
537static int /* pick result */
538xfs_dir2_sf_addname_pick(
539 xfs_da_args_t *args, /* operation arguments */
540 int objchange, /* inode # size changes */
541 xfs_dir2_sf_entry_t **sfepp, /* out(1): new entry ptr */
542 xfs_dir2_data_aoff_t *offsetp) /* out(1): new offset */
543{
544 xfs_inode_t *dp; /* incore directory inode */
545 int holefit; /* found hole it will fit in */
546 int i; /* entry number */
547 xfs_mount_t *mp; /* filesystem mount point */
548 xfs_dir2_data_aoff_t offset; /* data block offset */
549 xfs_dir2_sf_entry_t *sfep; /* shortform entry */
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200550 xfs_dir2_sf_hdr_t *sfp; /* shortform structure */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 int size; /* entry's data size */
552 int used; /* data bytes used */
553
554 dp = args->dp;
555 mp = dp->i_mount;
556
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200557 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
Dave Chinner9d23fc82013-10-29 22:11:48 +1100558 size = dp->d_ops->data_entsize(args->namelen);
Dave Chinner1c9a5b22013-10-30 09:15:02 +1100559 offset = dp->d_ops->data_first_offset;
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000560 sfep = xfs_dir2_sf_firstentry(sfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 holefit = 0;
562 /*
563 * Loop over sf entries.
564 * Keep track of data offset and whether we've seen a place
565 * to insert the new entry.
566 */
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200567 for (i = 0; i < sfp->count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 if (!holefit)
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000569 holefit = offset + size <= xfs_dir2_sf_get_offset(sfep);
570 offset = xfs_dir2_sf_get_offset(sfep) +
Dave Chinner9d23fc82013-10-29 22:11:48 +1100571 dp->d_ops->data_entsize(sfep->namelen);
Dave Chinner32c54832013-10-29 22:11:46 +1100572 sfep = dp->d_ops->sf_nextentry(sfp, sfep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 }
574 /*
575 * Calculate data bytes used excluding the new entry, if this
576 * was a data block (block form directory).
577 */
578 used = offset +
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200579 (sfp->count + 3) * (uint)sizeof(xfs_dir2_leaf_entry_t) +
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 (uint)sizeof(xfs_dir2_block_tail_t);
581 /*
582 * If it won't fit in a block form then we can't insert it,
583 * we'll go back, convert to block, then try the insert and convert
584 * to leaf.
585 */
586 if (used + (holefit ? 0 : size) > mp->m_dirblksize)
587 return 0;
588 /*
589 * If changing the inode number size, do it the hard way.
590 */
591#if XFS_BIG_INUMS
592 if (objchange) {
593 return 2;
594 }
595#else
596 ASSERT(objchange == 0);
597#endif
598 /*
599 * If it won't fit at the end then do it the hard way (use the hole).
600 */
601 if (used + size > mp->m_dirblksize)
602 return 2;
603 /*
604 * Do it the easy way.
605 */
606 *sfepp = sfep;
607 *offsetp = offset;
608 return 1;
609}
610
611#ifdef DEBUG
612/*
613 * Check consistency of shortform directory, assert if bad.
614 */
615static void
616xfs_dir2_sf_check(
617 xfs_da_args_t *args) /* operation arguments */
618{
619 xfs_inode_t *dp; /* incore directory inode */
620 int i; /* entry number */
621 int i8count; /* number of big inode#s */
622 xfs_ino_t ino; /* entry inode number */
623 int offset; /* data offset */
624 xfs_dir2_sf_entry_t *sfep; /* shortform dir entry */
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200625 xfs_dir2_sf_hdr_t *sfp; /* shortform structure */
Dave Chinner0cb97762013-08-12 20:50:09 +1000626 struct xfs_mount *mp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627
628 dp = args->dp;
Dave Chinner0cb97762013-08-12 20:50:09 +1000629 mp = dp->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200631 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
Dave Chinner1c9a5b22013-10-30 09:15:02 +1100632 offset = dp->d_ops->data_first_offset;
Dave Chinner47401752013-10-29 22:11:47 +1100633 ino = dp->d_ops->sf_get_parent_ino(sfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 i8count = ino > XFS_DIR2_MAX_SHORT_INUM;
635
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000636 for (i = 0, sfep = xfs_dir2_sf_firstentry(sfp);
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200637 i < sfp->count;
Dave Chinner32c54832013-10-29 22:11:46 +1100638 i++, sfep = dp->d_ops->sf_nextentry(sfp, sfep)) {
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000639 ASSERT(xfs_dir2_sf_get_offset(sfep) >= offset);
Dave Chinner47401752013-10-29 22:11:47 +1100640 ino = dp->d_ops->sf_get_ino(sfp, sfep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 i8count += ino > XFS_DIR2_MAX_SHORT_INUM;
642 offset =
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000643 xfs_dir2_sf_get_offset(sfep) +
Dave Chinner9d23fc82013-10-29 22:11:48 +1100644 dp->d_ops->data_entsize(sfep->namelen);
Dave Chinner47401752013-10-29 22:11:47 +1100645 ASSERT(dp->d_ops->sf_get_ftype(sfep) < XFS_DIR3_FT_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 }
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200647 ASSERT(i8count == sfp->i8count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 ASSERT(XFS_BIG_INUMS || i8count == 0);
649 ASSERT((char *)sfep - (char *)sfp == dp->i_d.di_size);
650 ASSERT(offset +
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200651 (sfp->count + 2) * (uint)sizeof(xfs_dir2_leaf_entry_t) +
Dave Chinner0cb97762013-08-12 20:50:09 +1000652 (uint)sizeof(xfs_dir2_block_tail_t) <= mp->m_dirblksize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653}
654#endif /* DEBUG */
655
656/*
657 * Create a new (shortform) directory.
658 */
659int /* error, always 0 */
660xfs_dir2_sf_create(
661 xfs_da_args_t *args, /* operation arguments */
662 xfs_ino_t pino) /* parent inode number */
663{
664 xfs_inode_t *dp; /* incore directory inode */
665 int i8count; /* parent inode is an 8-byte number */
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200666 xfs_dir2_sf_hdr_t *sfp; /* shortform structure */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 int size; /* directory size */
668
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000669 trace_xfs_dir2_sf_create(args);
670
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 dp = args->dp;
672
673 ASSERT(dp != NULL);
674 ASSERT(dp->i_d.di_size == 0);
675 /*
676 * If it's currently a zero-length extent file,
677 * convert it to local format.
678 */
679 if (dp->i_d.di_format == XFS_DINODE_FMT_EXTENTS) {
680 dp->i_df.if_flags &= ~XFS_IFEXTENTS; /* just in case */
681 dp->i_d.di_format = XFS_DINODE_FMT_LOCAL;
682 xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE);
683 dp->i_df.if_flags |= XFS_IFINLINE;
684 }
685 ASSERT(dp->i_df.if_flags & XFS_IFINLINE);
686 ASSERT(dp->i_df.if_bytes == 0);
687 i8count = pino > XFS_DIR2_MAX_SHORT_INUM;
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000688 size = xfs_dir2_sf_hdr_size(i8count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 /*
690 * Make a buffer for the data.
691 */
692 xfs_idata_realloc(dp, size, XFS_DATA_FORK);
693 /*
694 * Fill in the header,
695 */
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200696 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
697 sfp->i8count = i8count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 /*
699 * Now can put in the inode number, since i8count is set.
700 */
Dave Chinner47401752013-10-29 22:11:47 +1100701 dp->d_ops->sf_put_parent_ino(sfp, pino);
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200702 sfp->count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 dp->i_d.di_size = size;
704 xfs_dir2_sf_check(args);
705 xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE | XFS_ILOG_DDATA);
706 return 0;
707}
708
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709/*
710 * Lookup an entry in a shortform directory.
711 * Returns EEXIST if found, ENOENT if not found.
712 */
713int /* error */
714xfs_dir2_sf_lookup(
715 xfs_da_args_t *args) /* operation arguments */
716{
717 xfs_inode_t *dp; /* incore directory inode */
718 int i; /* entry index */
Barry Naujok384f3ce2008-05-21 16:58:22 +1000719 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 xfs_dir2_sf_entry_t *sfep; /* shortform directory entry */
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200721 xfs_dir2_sf_hdr_t *sfp; /* shortform structure */
Barry Naujok5163f952008-05-21 16:41:01 +1000722 enum xfs_dacmp cmp; /* comparison result */
Barry Naujok384f3ce2008-05-21 16:58:22 +1000723 xfs_dir2_sf_entry_t *ci_sfep; /* case-insens. entry */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000725 trace_xfs_dir2_sf_lookup(args);
726
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 xfs_dir2_sf_check(args);
728 dp = args->dp;
729
730 ASSERT(dp->i_df.if_flags & XFS_IFINLINE);
731 /*
732 * Bail out if the directory is way too short.
733 */
734 if (dp->i_d.di_size < offsetof(xfs_dir2_sf_hdr_t, parent)) {
735 ASSERT(XFS_FORCED_SHUTDOWN(dp->i_mount));
736 return XFS_ERROR(EIO);
737 }
738 ASSERT(dp->i_df.if_bytes == dp->i_d.di_size);
739 ASSERT(dp->i_df.if_u1.if_data != NULL);
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200740 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
741 ASSERT(dp->i_d.di_size >= xfs_dir2_sf_hdr_size(sfp->i8count));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 /*
743 * Special case for .
744 */
745 if (args->namelen == 1 && args->name[0] == '.') {
746 args->inumber = dp->i_ino;
Barry Naujok5163f952008-05-21 16:41:01 +1000747 args->cmpresult = XFS_CMP_EXACT;
Dave Chinner1c55cec2013-08-12 20:50:10 +1000748 args->filetype = XFS_DIR3_FT_DIR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 return XFS_ERROR(EEXIST);
750 }
751 /*
752 * Special case for ..
753 */
754 if (args->namelen == 2 &&
755 args->name[0] == '.' && args->name[1] == '.') {
Dave Chinner47401752013-10-29 22:11:47 +1100756 args->inumber = dp->d_ops->sf_get_parent_ino(sfp);
Barry Naujok5163f952008-05-21 16:41:01 +1000757 args->cmpresult = XFS_CMP_EXACT;
Dave Chinner1c55cec2013-08-12 20:50:10 +1000758 args->filetype = XFS_DIR3_FT_DIR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 return XFS_ERROR(EEXIST);
760 }
761 /*
762 * Loop over all the entries trying to match ours.
763 */
Barry Naujok384f3ce2008-05-21 16:58:22 +1000764 ci_sfep = NULL;
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200765 for (i = 0, sfep = xfs_dir2_sf_firstentry(sfp); i < sfp->count;
Dave Chinner32c54832013-10-29 22:11:46 +1100766 i++, sfep = dp->d_ops->sf_nextentry(sfp, sfep)) {
Barry Naujok5163f952008-05-21 16:41:01 +1000767 /*
768 * Compare name and if it's an exact match, return the inode
769 * number. If it's the first case-insensitive match, store the
770 * inode number and continue looking for an exact match.
771 */
772 cmp = dp->i_mount->m_dirnameops->compname(args, sfep->name,
773 sfep->namelen);
774 if (cmp != XFS_CMP_DIFFERENT && cmp != args->cmpresult) {
775 args->cmpresult = cmp;
Dave Chinner47401752013-10-29 22:11:47 +1100776 args->inumber = dp->d_ops->sf_get_ino(sfp, sfep);
777 args->filetype = dp->d_ops->sf_get_ftype(sfep);
Barry Naujok5163f952008-05-21 16:41:01 +1000778 if (cmp == XFS_CMP_EXACT)
779 return XFS_ERROR(EEXIST);
Barry Naujok384f3ce2008-05-21 16:58:22 +1000780 ci_sfep = sfep;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 }
782 }
Barry Naujok6a178102008-05-21 16:42:05 +1000783 ASSERT(args->op_flags & XFS_DA_OP_OKNOENT);
Barry Naujok5163f952008-05-21 16:41:01 +1000784 /*
785 * Here, we can only be doing a lookup (not a rename or replace).
Barry Naujok384f3ce2008-05-21 16:58:22 +1000786 * If a case-insensitive match was not found, return ENOENT.
Barry Naujok5163f952008-05-21 16:41:01 +1000787 */
Barry Naujok384f3ce2008-05-21 16:58:22 +1000788 if (!ci_sfep)
789 return XFS_ERROR(ENOENT);
790 /* otherwise process the CI match as required by the caller */
791 error = xfs_dir_cilookup_result(args, ci_sfep->name, ci_sfep->namelen);
792 return XFS_ERROR(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793}
794
795/*
796 * Remove an entry from a shortform directory.
797 */
798int /* error */
799xfs_dir2_sf_removename(
800 xfs_da_args_t *args)
801{
802 int byteoff; /* offset of removed entry */
803 xfs_inode_t *dp; /* incore directory inode */
804 int entsize; /* this entry's size */
805 int i; /* shortform entry index */
806 int newsize; /* new inode size */
807 int oldsize; /* old inode size */
808 xfs_dir2_sf_entry_t *sfep; /* shortform directory entry */
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200809 xfs_dir2_sf_hdr_t *sfp; /* shortform structure */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000811 trace_xfs_dir2_sf_removename(args);
812
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 dp = args->dp;
814
815 ASSERT(dp->i_df.if_flags & XFS_IFINLINE);
816 oldsize = (int)dp->i_d.di_size;
817 /*
818 * Bail out if the directory is way too short.
819 */
820 if (oldsize < offsetof(xfs_dir2_sf_hdr_t, parent)) {
821 ASSERT(XFS_FORCED_SHUTDOWN(dp->i_mount));
822 return XFS_ERROR(EIO);
823 }
824 ASSERT(dp->i_df.if_bytes == oldsize);
825 ASSERT(dp->i_df.if_u1.if_data != NULL);
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200826 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
827 ASSERT(oldsize >= xfs_dir2_sf_hdr_size(sfp->i8count));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 /*
829 * Loop over the old directory entries.
830 * Find the one we're deleting.
831 */
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200832 for (i = 0, sfep = xfs_dir2_sf_firstentry(sfp); i < sfp->count;
Dave Chinner32c54832013-10-29 22:11:46 +1100833 i++, sfep = dp->d_ops->sf_nextentry(sfp, sfep)) {
Barry Naujok5163f952008-05-21 16:41:01 +1000834 if (xfs_da_compname(args, sfep->name, sfep->namelen) ==
835 XFS_CMP_EXACT) {
Dave Chinner47401752013-10-29 22:11:47 +1100836 ASSERT(dp->d_ops->sf_get_ino(sfp, sfep) ==
Christoph Hellwig8bc38782011-07-08 14:35:03 +0200837 args->inumber);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 break;
839 }
840 }
841 /*
842 * Didn't find it.
843 */
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200844 if (i == sfp->count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 return XFS_ERROR(ENOENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 /*
847 * Calculate sizes.
848 */
849 byteoff = (int)((char *)sfep - (char *)sfp);
Dave Chinner32c54832013-10-29 22:11:46 +1100850 entsize = dp->d_ops->sf_entsize(sfp, args->namelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 newsize = oldsize - entsize;
852 /*
853 * Copy the part if any after the removed entry, sliding it down.
854 */
855 if (byteoff + entsize < oldsize)
856 memmove((char *)sfp + byteoff, (char *)sfp + byteoff + entsize,
857 oldsize - (byteoff + entsize));
858 /*
859 * Fix up the header and file size.
860 */
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200861 sfp->count--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 dp->i_d.di_size = newsize;
863 /*
864 * Reallocate, making it smaller.
865 */
866 xfs_idata_realloc(dp, newsize - oldsize, XFS_DATA_FORK);
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200867 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868#if XFS_BIG_INUMS
869 /*
870 * Are we changing inode number size?
871 */
872 if (args->inumber > XFS_DIR2_MAX_SHORT_INUM) {
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200873 if (sfp->i8count == 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 xfs_dir2_sf_toino4(args);
875 else
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200876 sfp->i8count--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 }
878#endif
879 xfs_dir2_sf_check(args);
880 xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE | XFS_ILOG_DDATA);
881 return 0;
882}
883
884/*
885 * Replace the inode number of an entry in a shortform directory.
886 */
887int /* error */
888xfs_dir2_sf_replace(
889 xfs_da_args_t *args) /* operation arguments */
890{
891 xfs_inode_t *dp; /* incore directory inode */
892 int i; /* entry index */
893#if XFS_BIG_INUMS || defined(DEBUG)
894 xfs_ino_t ino=0; /* entry old inode number */
895#endif
896#if XFS_BIG_INUMS
897 int i8elevated; /* sf_toino8 set i8count=1 */
898#endif
899 xfs_dir2_sf_entry_t *sfep; /* shortform directory entry */
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200900 xfs_dir2_sf_hdr_t *sfp; /* shortform structure */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000902 trace_xfs_dir2_sf_replace(args);
903
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 dp = args->dp;
905
906 ASSERT(dp->i_df.if_flags & XFS_IFINLINE);
907 /*
908 * Bail out if the shortform directory is way too small.
909 */
910 if (dp->i_d.di_size < offsetof(xfs_dir2_sf_hdr_t, parent)) {
911 ASSERT(XFS_FORCED_SHUTDOWN(dp->i_mount));
912 return XFS_ERROR(EIO);
913 }
914 ASSERT(dp->i_df.if_bytes == dp->i_d.di_size);
915 ASSERT(dp->i_df.if_u1.if_data != NULL);
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200916 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
917 ASSERT(dp->i_d.di_size >= xfs_dir2_sf_hdr_size(sfp->i8count));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918#if XFS_BIG_INUMS
919 /*
920 * New inode number is large, and need to convert to 8-byte inodes.
921 */
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200922 if (args->inumber > XFS_DIR2_MAX_SHORT_INUM && sfp->i8count == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 int error; /* error return value */
924 int newsize; /* new inode size */
925
926 newsize =
927 dp->i_df.if_bytes +
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200928 (sfp->count + 1) *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 ((uint)sizeof(xfs_dir2_ino8_t) -
930 (uint)sizeof(xfs_dir2_ino4_t));
931 /*
932 * Won't fit as shortform, convert to block then do replace.
933 */
934 if (newsize > XFS_IFORK_DSIZE(dp)) {
935 error = xfs_dir2_sf_to_block(args);
936 if (error) {
937 return error;
938 }
939 return xfs_dir2_block_replace(args);
940 }
941 /*
942 * Still fits, convert to 8-byte now.
943 */
944 xfs_dir2_sf_toino8(args);
945 i8elevated = 1;
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200946 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 } else
948 i8elevated = 0;
949#endif
950 ASSERT(args->namelen != 1 || args->name[0] != '.');
951 /*
952 * Replace ..'s entry.
953 */
954 if (args->namelen == 2 &&
955 args->name[0] == '.' && args->name[1] == '.') {
956#if XFS_BIG_INUMS || defined(DEBUG)
Dave Chinner47401752013-10-29 22:11:47 +1100957 ino = dp->d_ops->sf_get_parent_ino(sfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 ASSERT(args->inumber != ino);
959#endif
Dave Chinner47401752013-10-29 22:11:47 +1100960 dp->d_ops->sf_put_parent_ino(sfp, args->inumber);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 }
962 /*
963 * Normal entry, look for the name.
964 */
965 else {
Dave Chinner0cb97762013-08-12 20:50:09 +1000966 for (i = 0, sfep = xfs_dir2_sf_firstentry(sfp); i < sfp->count;
Dave Chinner32c54832013-10-29 22:11:46 +1100967 i++, sfep = dp->d_ops->sf_nextentry(sfp, sfep)) {
Barry Naujok5163f952008-05-21 16:41:01 +1000968 if (xfs_da_compname(args, sfep->name, sfep->namelen) ==
969 XFS_CMP_EXACT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970#if XFS_BIG_INUMS || defined(DEBUG)
Dave Chinner47401752013-10-29 22:11:47 +1100971 ino = dp->d_ops->sf_get_ino(sfp, sfep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 ASSERT(args->inumber != ino);
973#endif
Dave Chinner47401752013-10-29 22:11:47 +1100974 dp->d_ops->sf_put_ino(sfp, sfep, args->inumber);
975 dp->d_ops->sf_put_ftype(sfep, args->filetype);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 break;
977 }
978 }
979 /*
980 * Didn't find it.
981 */
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200982 if (i == sfp->count) {
Barry Naujok6a178102008-05-21 16:42:05 +1000983 ASSERT(args->op_flags & XFS_DA_OP_OKNOENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984#if XFS_BIG_INUMS
985 if (i8elevated)
986 xfs_dir2_sf_toino4(args);
987#endif
988 return XFS_ERROR(ENOENT);
989 }
990 }
991#if XFS_BIG_INUMS
992 /*
993 * See if the old number was large, the new number is small.
994 */
995 if (ino > XFS_DIR2_MAX_SHORT_INUM &&
996 args->inumber <= XFS_DIR2_MAX_SHORT_INUM) {
997 /*
998 * And the old count was one, so need to convert to small.
999 */
Christoph Hellwigac8ba502011-07-08 14:35:13 +02001000 if (sfp->i8count == 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 xfs_dir2_sf_toino4(args);
1002 else
Christoph Hellwigac8ba502011-07-08 14:35:13 +02001003 sfp->i8count--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 }
1005 /*
1006 * See if the old number was small, the new number is large.
1007 */
1008 if (ino <= XFS_DIR2_MAX_SHORT_INUM &&
1009 args->inumber > XFS_DIR2_MAX_SHORT_INUM) {
1010 /*
1011 * add to the i8count unless we just converted to 8-byte
1012 * inodes (which does an implied i8count = 1)
1013 */
Christoph Hellwigac8ba502011-07-08 14:35:13 +02001014 ASSERT(sfp->i8count != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 if (!i8elevated)
Christoph Hellwigac8ba502011-07-08 14:35:13 +02001016 sfp->i8count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 }
1018#endif
1019 xfs_dir2_sf_check(args);
1020 xfs_trans_log_inode(args->trans, dp, XFS_ILOG_DDATA);
1021 return 0;
1022}
1023
1024#if XFS_BIG_INUMS
1025/*
1026 * Convert from 8-byte inode numbers to 4-byte inode numbers.
1027 * The last 8-byte inode number is gone, but the count is still 1.
1028 */
1029static void
1030xfs_dir2_sf_toino4(
1031 xfs_da_args_t *args) /* operation arguments */
1032{
1033 char *buf; /* old dir's buffer */
1034 xfs_inode_t *dp; /* incore directory inode */
1035 int i; /* entry index */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 int newsize; /* new inode size */
1037 xfs_dir2_sf_entry_t *oldsfep; /* old sf entry */
Christoph Hellwigac8ba502011-07-08 14:35:13 +02001038 xfs_dir2_sf_hdr_t *oldsfp; /* old sf directory */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 int oldsize; /* old inode size */
1040 xfs_dir2_sf_entry_t *sfep; /* new sf entry */
Christoph Hellwigac8ba502011-07-08 14:35:13 +02001041 xfs_dir2_sf_hdr_t *sfp; /* new sf directory */
Dave Chinner1c55cec2013-08-12 20:50:10 +10001042 struct xfs_mount *mp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001044 trace_xfs_dir2_sf_toino4(args);
1045
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 dp = args->dp;
Dave Chinner1c55cec2013-08-12 20:50:10 +10001047 mp = dp->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048
1049 /*
1050 * Copy the old directory to the buffer.
1051 * Then nuke it from the inode, and add the new buffer to the inode.
1052 * Don't want xfs_idata_realloc copying the data here.
1053 */
1054 oldsize = dp->i_df.if_bytes;
1055 buf = kmem_alloc(oldsize, KM_SLEEP);
Christoph Hellwigac8ba502011-07-08 14:35:13 +02001056 oldsfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
1057 ASSERT(oldsfp->i8count == 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 memcpy(buf, oldsfp, oldsize);
1059 /*
1060 * Compute the new inode size.
1061 */
1062 newsize =
1063 oldsize -
Christoph Hellwigac8ba502011-07-08 14:35:13 +02001064 (oldsfp->count + 1) *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 ((uint)sizeof(xfs_dir2_ino8_t) - (uint)sizeof(xfs_dir2_ino4_t));
1066 xfs_idata_realloc(dp, -oldsize, XFS_DATA_FORK);
1067 xfs_idata_realloc(dp, newsize, XFS_DATA_FORK);
1068 /*
1069 * Reset our pointers, the data has moved.
1070 */
Christoph Hellwigac8ba502011-07-08 14:35:13 +02001071 oldsfp = (xfs_dir2_sf_hdr_t *)buf;
1072 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 /*
1074 * Fill in the new header.
1075 */
Christoph Hellwigac8ba502011-07-08 14:35:13 +02001076 sfp->count = oldsfp->count;
1077 sfp->i8count = 0;
Dave Chinner47401752013-10-29 22:11:47 +11001078 dp->d_ops->sf_put_parent_ino(sfp, dp->d_ops->sf_get_parent_ino(oldsfp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 /*
1080 * Copy the entries field by field.
1081 */
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001082 for (i = 0, sfep = xfs_dir2_sf_firstentry(sfp),
1083 oldsfep = xfs_dir2_sf_firstentry(oldsfp);
Christoph Hellwigac8ba502011-07-08 14:35:13 +02001084 i < sfp->count;
Dave Chinner32c54832013-10-29 22:11:46 +11001085 i++, sfep = dp->d_ops->sf_nextentry(sfp, sfep),
1086 oldsfep = dp->d_ops->sf_nextentry(oldsfp, oldsfep)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 sfep->namelen = oldsfep->namelen;
1088 sfep->offset = oldsfep->offset;
1089 memcpy(sfep->name, oldsfep->name, sfep->namelen);
Dave Chinner47401752013-10-29 22:11:47 +11001090 dp->d_ops->sf_put_ino(sfp, sfep,
1091 dp->d_ops->sf_get_ino(oldsfp, oldsfep));
1092 dp->d_ops->sf_put_ftype(sfep, dp->d_ops->sf_get_ftype(oldsfep));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 }
1094 /*
1095 * Clean up the inode.
1096 */
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10001097 kmem_free(buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 dp->i_d.di_size = newsize;
1099 xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE | XFS_ILOG_DDATA);
1100}
1101
1102/*
1103 * Convert from 4-byte inode numbers to 8-byte inode numbers.
1104 * The new 8-byte inode number is not there yet, we leave with the
1105 * count 1 but no corresponding entry.
1106 */
1107static void
1108xfs_dir2_sf_toino8(
1109 xfs_da_args_t *args) /* operation arguments */
1110{
1111 char *buf; /* old dir's buffer */
1112 xfs_inode_t *dp; /* incore directory inode */
1113 int i; /* entry index */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 int newsize; /* new inode size */
1115 xfs_dir2_sf_entry_t *oldsfep; /* old sf entry */
Christoph Hellwigac8ba502011-07-08 14:35:13 +02001116 xfs_dir2_sf_hdr_t *oldsfp; /* old sf directory */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 int oldsize; /* old inode size */
1118 xfs_dir2_sf_entry_t *sfep; /* new sf entry */
Christoph Hellwigac8ba502011-07-08 14:35:13 +02001119 xfs_dir2_sf_hdr_t *sfp; /* new sf directory */
Dave Chinner1c55cec2013-08-12 20:50:10 +10001120 struct xfs_mount *mp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001122 trace_xfs_dir2_sf_toino8(args);
1123
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 dp = args->dp;
Dave Chinner1c55cec2013-08-12 20:50:10 +10001125 mp = dp->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126
1127 /*
1128 * Copy the old directory to the buffer.
1129 * Then nuke it from the inode, and add the new buffer to the inode.
1130 * Don't want xfs_idata_realloc copying the data here.
1131 */
1132 oldsize = dp->i_df.if_bytes;
1133 buf = kmem_alloc(oldsize, KM_SLEEP);
Christoph Hellwigac8ba502011-07-08 14:35:13 +02001134 oldsfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
1135 ASSERT(oldsfp->i8count == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 memcpy(buf, oldsfp, oldsize);
1137 /*
1138 * Compute the new inode size.
1139 */
1140 newsize =
1141 oldsize +
Christoph Hellwigac8ba502011-07-08 14:35:13 +02001142 (oldsfp->count + 1) *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 ((uint)sizeof(xfs_dir2_ino8_t) - (uint)sizeof(xfs_dir2_ino4_t));
1144 xfs_idata_realloc(dp, -oldsize, XFS_DATA_FORK);
1145 xfs_idata_realloc(dp, newsize, XFS_DATA_FORK);
1146 /*
1147 * Reset our pointers, the data has moved.
1148 */
Christoph Hellwigac8ba502011-07-08 14:35:13 +02001149 oldsfp = (xfs_dir2_sf_hdr_t *)buf;
1150 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 /*
1152 * Fill in the new header.
1153 */
Christoph Hellwigac8ba502011-07-08 14:35:13 +02001154 sfp->count = oldsfp->count;
1155 sfp->i8count = 1;
Dave Chinner47401752013-10-29 22:11:47 +11001156 dp->d_ops->sf_put_parent_ino(sfp, dp->d_ops->sf_get_parent_ino(oldsfp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 /*
1158 * Copy the entries field by field.
1159 */
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001160 for (i = 0, sfep = xfs_dir2_sf_firstentry(sfp),
1161 oldsfep = xfs_dir2_sf_firstentry(oldsfp);
Christoph Hellwigac8ba502011-07-08 14:35:13 +02001162 i < sfp->count;
Dave Chinner32c54832013-10-29 22:11:46 +11001163 i++, sfep = dp->d_ops->sf_nextentry(sfp, sfep),
1164 oldsfep = dp->d_ops->sf_nextentry(oldsfp, oldsfep)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 sfep->namelen = oldsfep->namelen;
1166 sfep->offset = oldsfep->offset;
1167 memcpy(sfep->name, oldsfep->name, sfep->namelen);
Dave Chinner47401752013-10-29 22:11:47 +11001168 dp->d_ops->sf_put_ino(sfp, sfep,
1169 dp->d_ops->sf_get_ino(oldsfp, oldsfep));
1170 dp->d_ops->sf_put_ftype(sfep, dp->d_ops->sf_get_ftype(oldsfep));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 }
1172 /*
1173 * Clean up the inode.
1174 */
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10001175 kmem_free(buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 dp->i_d.di_size = newsize;
1177 xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE | XFS_ILOG_DDATA);
1178}
1179#endif /* XFS_BIG_INUMS */