blob: 2463b5d73447244fa559b393ab96017610135c89 [file] [log] [blame]
Dave Chinner0b61f8a2018-06-05 19:42:14 -07001// SPDX-License-Identifier: GPL-2.0
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
Nathan Scott7b718762005-11-02 14:58:39 +11003 * Copyright (c) 2000-2003,2005 Silicon Graphics, Inc.
4 * All Rights Reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006#include "xfs.h"
Nathan Scotta844f452005-11-02 14:38:42 +11007#include "xfs_fs.h"
Darrick J. Wong5467b342019-06-28 19:25:35 -07008#include "xfs_shared.h"
Dave Chinnera4fbe6a2013-10-23 10:51:50 +11009#include "xfs_format.h"
Dave Chinner239880e2013-10-23 10:50:10 +110010#include "xfs_log_format.h"
11#include "xfs_trans_resv.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include "xfs_mount.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include "xfs_inode.h"
Dave Chinner239880e2013-10-23 10:50:10 +110014#include "xfs_trans.h"
Dave Chinner2b9ab5a2013-08-12 20:49:37 +100015#include "xfs_dir2.h"
Christoph Hellwig57926642011-07-13 13:43:48 +020016#include "xfs_dir2_priv.h"
Christoph Hellwig0b1b2132009-12-14 23:14:59 +000017#include "xfs_trace.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070018
19/*
20 * Prototypes for internal functions.
21 */
22static void xfs_dir2_sf_addname_easy(xfs_da_args_t *args,
23 xfs_dir2_sf_entry_t *sfep,
24 xfs_dir2_data_aoff_t offset,
25 int new_isize);
26static void xfs_dir2_sf_addname_hard(xfs_da_args_t *args, int objchange,
27 int new_isize);
28static int xfs_dir2_sf_addname_pick(xfs_da_args_t *args, int objchange,
29 xfs_dir2_sf_entry_t **sfepp,
30 xfs_dir2_data_aoff_t *offsetp);
31#ifdef DEBUG
32static void xfs_dir2_sf_check(xfs_da_args_t *args);
33#else
34#define xfs_dir2_sf_check(args)
35#endif /* DEBUG */
Christoph Hellwigd5cf09b2014-07-30 09:12:05 +100036
Linus Torvalds1da177e2005-04-16 15:20:36 -070037static void xfs_dir2_sf_toino4(xfs_da_args_t *args);
38static void xfs_dir2_sf_toino8(xfs_da_args_t *args);
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
Darrick J. Wongaf952ae2019-12-16 11:14:09 -080040int
Christoph Hellwig50f6bb62019-11-08 15:02:38 -080041xfs_dir2_sf_entsize(
42 struct xfs_mount *mp,
43 struct xfs_dir2_sf_hdr *hdr,
44 int len)
45{
46 int count = len;
47
48 count += sizeof(struct xfs_dir2_sf_entry); /* namelen + offset */
49 count += hdr->i8count ? XFS_INO64_SIZE : XFS_INO32_SIZE; /* ino # */
50
51 if (xfs_sb_version_hasftype(&mp->m_sb))
52 count += sizeof(uint8_t);
53 return count;
54}
55
56struct xfs_dir2_sf_entry *
57xfs_dir2_sf_nextentry(
58 struct xfs_mount *mp,
59 struct xfs_dir2_sf_hdr *hdr,
60 struct xfs_dir2_sf_entry *sfep)
61{
62 return (void *)sfep + xfs_dir2_sf_entsize(mp, hdr, sfep->namelen);
63}
64
Christoph Hellwig8bc38782011-07-08 14:35:03 +020065/*
Christoph Hellwig93b1e962019-11-08 15:02:59 -080066 * In short-form directory entries the inode numbers are stored at variable
67 * offset behind the entry name. If the entry stores a filetype value, then it
68 * sits between the name and the inode number. The actual inode numbers can
69 * come in two formats as well, either 4 bytes or 8 bytes wide.
70 */
71xfs_ino_t
72xfs_dir2_sf_get_ino(
73 struct xfs_mount *mp,
74 struct xfs_dir2_sf_hdr *hdr,
75 struct xfs_dir2_sf_entry *sfep)
76{
77 uint8_t *from = sfep->name + sfep->namelen;
78
79 if (xfs_sb_version_hasftype(&mp->m_sb))
80 from++;
81
82 if (!hdr->i8count)
83 return get_unaligned_be32(from);
84 return get_unaligned_be64(from) & XFS_MAXINUMBER;
85}
86
Darrick J. Wongaf952ae2019-12-16 11:14:09 -080087void
Christoph Hellwig93b1e962019-11-08 15:02:59 -080088xfs_dir2_sf_put_ino(
89 struct xfs_mount *mp,
90 struct xfs_dir2_sf_hdr *hdr,
91 struct xfs_dir2_sf_entry *sfep,
92 xfs_ino_t ino)
93{
94 uint8_t *to = sfep->name + sfep->namelen;
95
96 ASSERT(ino <= XFS_MAXINUMBER);
97
98 if (xfs_sb_version_hasftype(&mp->m_sb))
99 to++;
100
101 if (hdr->i8count)
102 put_unaligned_be64(ino, to);
103 else
104 put_unaligned_be32(ino, to);
105}
106
107xfs_ino_t
108xfs_dir2_sf_get_parent_ino(
109 struct xfs_dir2_sf_hdr *hdr)
110{
111 if (!hdr->i8count)
112 return get_unaligned_be32(hdr->parent);
113 return get_unaligned_be64(hdr->parent) & XFS_MAXINUMBER;
114}
115
116void
117xfs_dir2_sf_put_parent_ino(
118 struct xfs_dir2_sf_hdr *hdr,
119 xfs_ino_t ino)
120{
121 ASSERT(ino <= XFS_MAXINUMBER);
122
123 if (hdr->i8count)
124 put_unaligned_be64(ino, hdr->parent);
125 else
126 put_unaligned_be32(ino, hdr->parent);
127}
128
129/*
Christoph Hellwig4501ed22019-11-08 15:03:30 -0800130 * The file type field is stored at the end of the name for filetype enabled
131 * shortform directories, or not at all otherwise.
132 */
133uint8_t
134xfs_dir2_sf_get_ftype(
135 struct xfs_mount *mp,
136 struct xfs_dir2_sf_entry *sfep)
137{
138 if (xfs_sb_version_hasftype(&mp->m_sb)) {
139 uint8_t ftype = sfep->name[sfep->namelen];
140
141 if (ftype < XFS_DIR3_FT_MAX)
142 return ftype;
143 }
144
145 return XFS_DIR3_FT_UNKNOWN;
146}
147
Darrick J. Wongaf952ae2019-12-16 11:14:09 -0800148void
Christoph Hellwig4501ed22019-11-08 15:03:30 -0800149xfs_dir2_sf_put_ftype(
150 struct xfs_mount *mp,
151 struct xfs_dir2_sf_entry *sfep,
152 uint8_t ftype)
153{
154 ASSERT(ftype < XFS_DIR3_FT_MAX);
155
156 if (xfs_sb_version_hasftype(&mp->m_sb))
157 sfep->name[sfep->namelen] = ftype;
158}
159
160/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 * Given a block directory (dp/block), calculate its size as a shortform (sf)
162 * directory and a header for the sf directory, if it will fit it the
163 * space currently present in the inode. If it won't fit, the output
164 * size is too big (but not accurate).
165 */
166int /* size for sf form */
167xfs_dir2_block_sfsize(
168 xfs_inode_t *dp, /* incore inode pointer */
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +0200169 xfs_dir2_data_hdr_t *hdr, /* block directory data */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 xfs_dir2_sf_hdr_t *sfhp) /* output: header for sf form */
171{
172 xfs_dir2_dataptr_t addr; /* data entry address */
173 xfs_dir2_leaf_entry_t *blp; /* leaf area of the block */
174 xfs_dir2_block_tail_t *btp; /* tail area of the block */
175 int count; /* shortform entry count */
176 xfs_dir2_data_entry_t *dep; /* data entry in the block */
177 int i; /* block entry index */
178 int i8count; /* count of big-inode entries */
179 int isdot; /* entry is "." */
180 int isdotdot; /* entry is ".." */
181 xfs_mount_t *mp; /* mount structure pointer */
182 int namelen; /* total name bytes */
Christoph Hellwig5bde1ba92005-11-02 15:06:18 +1100183 xfs_ino_t parent = 0; /* parent inode number */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 int size=0; /* total computed size */
Dave Chinner0cb97762013-08-12 20:50:09 +1000185 int has_ftype;
Dave Chinner8f661932014-06-06 15:15:59 +1000186 struct xfs_da_geometry *geo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187
188 mp = dp->i_mount;
Dave Chinner8f661932014-06-06 15:15:59 +1000189 geo = mp->m_dir_geo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190
Dave Chinner0cb97762013-08-12 20:50:09 +1000191 /*
192 * if there is a filetype field, add the extra byte to the namelen
193 * for each entry that we see.
194 */
195 has_ftype = xfs_sb_version_hasftype(&mp->m_sb) ? 1 : 0;
196
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 count = i8count = namelen = 0;
Dave Chinner8f661932014-06-06 15:15:59 +1000198 btp = xfs_dir2_block_tail_p(geo, hdr);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000199 blp = xfs_dir2_block_leaf_p(btp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200
201 /*
202 * Iterate over the block's data entries by using the leaf pointers.
203 */
Nathan Scotte922fff2006-03-17 17:27:56 +1100204 for (i = 0; i < be32_to_cpu(btp->count); i++) {
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100205 if ((addr = be32_to_cpu(blp[i].address)) == XFS_DIR2_NULL_DATAPTR)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 continue;
207 /*
208 * Calculate the pointer to the entry at hand.
209 */
Dave Chinner30028032014-06-06 15:08:18 +1000210 dep = (xfs_dir2_data_entry_t *)((char *)hdr +
Dave Chinner8f661932014-06-06 15:15:59 +1000211 xfs_dir2_dataptr_to_off(geo, addr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 /*
213 * Detect . and .., so we can special-case them.
214 * . is not included in sf directories.
215 * .. is included by just the parent inode number.
216 */
217 isdot = dep->namelen == 1 && dep->name[0] == '.';
218 isdotdot =
219 dep->namelen == 2 &&
220 dep->name[0] == '.' && dep->name[1] == '.';
Christoph Hellwigd5cf09b2014-07-30 09:12:05 +1000221
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 if (!isdot)
Christoph Hellwigff9901c2006-06-09 14:48:37 +1000223 i8count += be64_to_cpu(dep->inumber) > XFS_DIR2_MAX_SHORT_INUM;
Christoph Hellwigd5cf09b2014-07-30 09:12:05 +1000224
Dave Chinner0cb97762013-08-12 20:50:09 +1000225 /* take into account the file type field */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 if (!isdot && !isdotdot) {
227 count++;
Dave Chinner0cb97762013-08-12 20:50:09 +1000228 namelen += dep->namelen + has_ftype;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 } else if (isdotdot)
Christoph Hellwigff9901c2006-06-09 14:48:37 +1000230 parent = be64_to_cpu(dep->inumber);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 /*
232 * Calculate the new size, see if we should give up yet.
233 */
Christoph Hellwig8353a642016-07-20 11:47:21 +1000234 size = xfs_dir2_sf_hdr_size(i8count) + /* header */
235 count * 3 * sizeof(u8) + /* namelen + offset */
236 namelen + /* name */
237 (i8count ? /* inumber */
Christoph Hellwig266b6962016-07-20 11:48:31 +1000238 count * XFS_INO64_SIZE :
239 count * XFS_INO32_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 if (size > XFS_IFORK_DSIZE(dp))
241 return size; /* size value is a failure */
242 }
243 /*
244 * Create the output header, if it worked.
245 */
246 sfhp->count = count;
247 sfhp->i8count = i8count;
Christoph Hellwig84915e12019-11-08 15:02:31 -0800248 xfs_dir2_sf_put_parent_ino(sfhp, parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 return size;
250}
251
252/*
253 * Convert a block format directory to shortform.
254 * Caller has already checked that it will fit, and built us a header.
255 */
256int /* error */
257xfs_dir2_block_to_sf(
Christoph Hellwig8073af52019-11-08 15:05:34 -0800258 struct xfs_da_args *args, /* operation arguments */
Dave Chinner1d9025e2012-06-22 18:50:14 +1000259 struct xfs_buf *bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 int size, /* shortform directory size */
Christoph Hellwig8073af52019-11-08 15:05:34 -0800261 struct xfs_dir2_sf_hdr *sfhp) /* shortform directory hdr */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262{
Christoph Hellwig8073af52019-11-08 15:05:34 -0800263 struct xfs_inode *dp = args->dp;
264 struct xfs_mount *mp = dp->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 int error; /* error return value */
266 int logflags; /* inode logging flags */
Christoph Hellwig8073af52019-11-08 15:05:34 -0800267 struct xfs_dir2_sf_entry *sfep; /* shortform entry */
268 struct xfs_dir2_sf_hdr *sfp; /* shortform directory header */
Christoph Hellwigd73e1ce2019-11-08 15:05:38 -0800269 unsigned int offset = args->geo->data_entry_offset;
Christoph Hellwig8073af52019-11-08 15:05:34 -0800270 unsigned int end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000272 trace_xfs_dir2_block_to_sf(args);
273
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 /*
Christoph Hellwig8073af52019-11-08 15:05:34 -0800275 * Allocate a temporary destination buffer the size of the inode to
276 * format the data into. Once we have formatted the data, we can free
277 * the block and copy the formatted data into the inode literal area.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 */
Christoph Hellwig8073af52019-11-08 15:05:34 -0800279 sfp = kmem_alloc(mp->m_sb.sb_inodesize, 0);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000280 memcpy(sfp, sfhp, xfs_dir2_sf_hdr_size(sfhp->i8count));
Dave Chinnerb3f03ba2013-12-03 23:50:57 +1100281
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 /*
Christoph Hellwig8073af52019-11-08 15:05:34 -0800283 * Loop over the active and unused entries. Stop when we reach the
284 * leaf/tail portion of the block.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 */
Christoph Hellwig5c072122019-11-08 15:05:36 -0800286 end = xfs_dir3_data_end_offset(args->geo, bp->b_addr);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000287 sfep = xfs_dir2_sf_firstentry(sfp);
Christoph Hellwig8073af52019-11-08 15:05:34 -0800288 while (offset < end) {
289 struct xfs_dir2_data_unused *dup = bp->b_addr + offset;
290 struct xfs_dir2_data_entry *dep = bp->b_addr + offset;
291
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 /*
293 * If it's unused, just skip over it.
294 */
Nathan Scottad354eb2006-03-17 17:27:37 +1100295 if (be16_to_cpu(dup->freetag) == XFS_DIR2_DATA_FREE_TAG) {
Christoph Hellwig8073af52019-11-08 15:05:34 -0800296 offset += be16_to_cpu(dup->length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 continue;
298 }
Christoph Hellwig8073af52019-11-08 15:05:34 -0800299
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 /*
301 * Skip .
302 */
303 if (dep->namelen == 1 && dep->name[0] == '.')
Christoph Hellwigff9901c2006-06-09 14:48:37 +1000304 ASSERT(be64_to_cpu(dep->inumber) == dp->i_ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 /*
306 * Skip .., but make sure the inode number is right.
307 */
308 else if (dep->namelen == 2 &&
309 dep->name[0] == '.' && dep->name[1] == '.')
Christoph Hellwigff9901c2006-06-09 14:48:37 +1000310 ASSERT(be64_to_cpu(dep->inumber) ==
Christoph Hellwig84915e12019-11-08 15:02:31 -0800311 xfs_dir2_sf_get_parent_ino(sfp));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 /*
313 * Normal entry, copy it into shortform.
314 */
315 else {
316 sfep->namelen = dep->namelen;
Christoph Hellwig8073af52019-11-08 15:05:34 -0800317 xfs_dir2_sf_put_offset(sfep, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 memcpy(sfep->name, dep->name, dep->namelen);
Christoph Hellwig93b1e962019-11-08 15:02:59 -0800319 xfs_dir2_sf_put_ino(mp, sfp, sfep,
Dave Chinner47401752013-10-29 22:11:47 +1100320 be64_to_cpu(dep->inumber));
Christoph Hellwig4501ed22019-11-08 15:03:30 -0800321 xfs_dir2_sf_put_ftype(mp, sfep,
Christoph Hellwig59b8b462019-11-08 15:05:48 -0800322 xfs_dir2_data_get_ftype(mp, dep));
Christoph Hellwig8bc38782011-07-08 14:35:03 +0200323
Christoph Hellwig50f6bb62019-11-08 15:02:38 -0800324 sfep = xfs_dir2_sf_nextentry(mp, sfp, sfep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 }
Christoph Hellwigfdbb8c52019-11-08 15:05:37 -0800326 offset += xfs_dir2_data_entsize(mp, dep->namelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 }
328 ASSERT((char *)sfep - (char *)sfp == size);
Dave Chinnerb3f03ba2013-12-03 23:50:57 +1100329
330 /* now we are done with the block, we can shrink the inode */
331 logflags = XFS_ILOG_CORE;
Dave Chinner7dda6e82014-06-06 15:11:18 +1000332 error = xfs_dir2_shrink_inode(args, args->geo->datablk, bp);
Dave Chinnerb3f03ba2013-12-03 23:50:57 +1100333 if (error) {
Dave Chinner24513372014-06-25 14:58:08 +1000334 ASSERT(error != -ENOSPC);
Dave Chinnerb3f03ba2013-12-03 23:50:57 +1100335 goto out;
336 }
337
338 /*
339 * The buffer is now unconditionally gone, whether
340 * xfs_dir2_shrink_inode worked or not.
341 *
342 * Convert the inode to local format and copy the data in.
343 */
Dave Chinnerb3f03ba2013-12-03 23:50:57 +1100344 ASSERT(dp->i_df.if_bytes == 0);
Christoph Hellwig8073af52019-11-08 15:05:34 -0800345 xfs_init_local_fork(dp, XFS_DATA_FORK, sfp, size);
Christoph Hellwigf7e67b22020-05-18 10:28:05 -0700346 dp->i_df.if_format = XFS_DINODE_FMT_LOCAL;
Christoph Hellwig143f4ae2016-04-06 07:41:43 +1000347 dp->i_d.di_size = size;
Dave Chinnerb3f03ba2013-12-03 23:50:57 +1100348
349 logflags |= XFS_ILOG_DDATA;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 xfs_dir2_sf_check(args);
351out:
352 xfs_trans_log_inode(args->trans, dp, logflags);
Christoph Hellwig8073af52019-11-08 15:05:34 -0800353 kmem_free(sfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 return error;
355}
356
357/*
358 * Add a name to a shortform directory.
359 * There are two algorithms, "easy" and "hard" which we decide on
360 * before changing anything.
361 * Convert to block form if necessary, if the new entry won't fit.
362 */
363int /* error */
364xfs_dir2_sf_addname(
365 xfs_da_args_t *args) /* operation arguments */
366{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 xfs_inode_t *dp; /* incore directory inode */
368 int error; /* error return value */
369 int incr_isize; /* total change in size */
370 int new_isize; /* di_size after adding name */
371 int objchange; /* changing to 8-byte inodes */
Christoph Hellwig5bde1ba92005-11-02 15:06:18 +1100372 xfs_dir2_data_aoff_t offset = 0; /* offset for new entry */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 int pick; /* which algorithm to use */
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200374 xfs_dir2_sf_hdr_t *sfp; /* shortform structure */
Christoph Hellwig5bde1ba92005-11-02 15:06:18 +1100375 xfs_dir2_sf_entry_t *sfep = NULL; /* shortform entry */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000377 trace_xfs_dir2_sf_addname(args);
378
Dave Chinner24513372014-06-25 14:58:08 +1000379 ASSERT(xfs_dir2_sf_lookup(args) == -ENOENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 dp = args->dp;
381 ASSERT(dp->i_df.if_flags & XFS_IFINLINE);
Darrick J. Wonge91ec882019-11-02 09:38:08 -0700382 ASSERT(dp->i_d.di_size >= offsetof(struct xfs_dir2_sf_hdr, parent));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 ASSERT(dp->i_df.if_bytes == dp->i_d.di_size);
384 ASSERT(dp->i_df.if_u1.if_data != NULL);
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200385 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
386 ASSERT(dp->i_d.di_size >= xfs_dir2_sf_hdr_size(sfp->i8count));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 /*
388 * Compute entry (and change in) size.
389 */
Christoph Hellwig50f6bb62019-11-08 15:02:38 -0800390 incr_isize = xfs_dir2_sf_entsize(dp->i_mount, sfp, args->namelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 objchange = 0;
Christoph Hellwigd5cf09b2014-07-30 09:12:05 +1000392
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 /*
394 * Do we have to change to 8 byte inodes?
395 */
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200396 if (args->inumber > XFS_DIR2_MAX_SHORT_INUM && sfp->i8count == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 /*
Eric Sandeen5e06d142014-04-14 19:07:23 +1000398 * Yes, adjust the inode size. old count + (parent + new)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 */
Christoph Hellwig266b6962016-07-20 11:48:31 +1000400 incr_isize += (sfp->count + 2) * XFS_INO64_DIFF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 objchange = 1;
402 }
Christoph Hellwigd5cf09b2014-07-30 09:12:05 +1000403
Eric Sandeen5e06d142014-04-14 19:07:23 +1000404 new_isize = (int)dp->i_d.di_size + incr_isize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 /*
406 * Won't fit as shortform any more (due to size),
407 * or the pick routine says it won't (due to offset values).
408 */
409 if (new_isize > XFS_IFORK_DSIZE(dp) ||
410 (pick =
411 xfs_dir2_sf_addname_pick(args, objchange, &sfep, &offset)) == 0) {
412 /*
413 * Just checking or no space reservation, it doesn't fit.
414 */
Barry Naujok6a178102008-05-21 16:42:05 +1000415 if ((args->op_flags & XFS_DA_OP_JUSTCHECK) || args->total == 0)
Dave Chinner24513372014-06-25 14:58:08 +1000416 return -ENOSPC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 /*
418 * Convert to block form then add the name.
419 */
420 error = xfs_dir2_sf_to_block(args);
421 if (error)
422 return error;
423 return xfs_dir2_block_addname(args);
424 }
425 /*
426 * Just checking, it fits.
427 */
Barry Naujok6a178102008-05-21 16:42:05 +1000428 if (args->op_flags & XFS_DA_OP_JUSTCHECK)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 return 0;
430 /*
431 * Do it the easy way - just add it at the end.
432 */
433 if (pick == 1)
434 xfs_dir2_sf_addname_easy(args, sfep, offset, new_isize);
435 /*
436 * Do it the hard way - look for a place to insert the new entry.
437 * Convert to 8 byte inode numbers first if necessary.
438 */
439 else {
440 ASSERT(pick == 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 if (objchange)
442 xfs_dir2_sf_toino8(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 xfs_dir2_sf_addname_hard(args, objchange, new_isize);
444 }
445 xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE | XFS_ILOG_DDATA);
446 return 0;
447}
448
449/*
450 * Add the new entry the "easy" way.
451 * This is copying the old directory and adding the new entry at the end.
452 * Since it's sorted by "offset" we need room after the last offset
453 * that's already there, and then room to convert to a block directory.
454 * This is already checked by the pick routine.
455 */
456static void
457xfs_dir2_sf_addname_easy(
458 xfs_da_args_t *args, /* operation arguments */
459 xfs_dir2_sf_entry_t *sfep, /* pointer to new entry */
460 xfs_dir2_data_aoff_t offset, /* offset to use for new ent */
461 int new_isize) /* new directory size */
462{
Christoph Hellwig50f6bb62019-11-08 15:02:38 -0800463 struct xfs_inode *dp = args->dp;
464 struct xfs_mount *mp = dp->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 int byteoff; /* byte offset in sf dir */
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200466 xfs_dir2_sf_hdr_t *sfp; /* shortform structure */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200468 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 byteoff = (int)((char *)sfep - (char *)sfp);
470 /*
471 * Grow the in-inode space.
472 */
Christoph Hellwig50f6bb62019-11-08 15:02:38 -0800473 xfs_idata_realloc(dp, xfs_dir2_sf_entsize(mp, sfp, args->namelen),
Dave Chinner0cb97762013-08-12 20:50:09 +1000474 XFS_DATA_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 /*
476 * Need to set up again due to realloc of the inode data.
477 */
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200478 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 sfep = (xfs_dir2_sf_entry_t *)((char *)sfp + byteoff);
480 /*
481 * Fill in the new entry.
482 */
483 sfep->namelen = args->namelen;
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000484 xfs_dir2_sf_put_offset(sfep, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 memcpy(sfep->name, args->name, sfep->namelen);
Christoph Hellwig93b1e962019-11-08 15:02:59 -0800486 xfs_dir2_sf_put_ino(mp, sfp, sfep, args->inumber);
Christoph Hellwig4501ed22019-11-08 15:03:30 -0800487 xfs_dir2_sf_put_ftype(mp, sfep, args->filetype);
Dave Chinner1c55cec2013-08-12 20:50:10 +1000488
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 /*
490 * Update the header and inode.
491 */
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200492 sfp->count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 if (args->inumber > XFS_DIR2_MAX_SHORT_INUM)
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200494 sfp->i8count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 dp->i_d.di_size = new_isize;
496 xfs_dir2_sf_check(args);
497}
498
499/*
500 * Add the new entry the "hard" way.
501 * The caller has already converted to 8 byte inode numbers if necessary,
502 * in which case we need to leave the i8count at 1.
503 * Find a hole that the new entry will fit into, and copy
504 * the first part of the entries, the new entry, and the last part of
505 * the entries.
506 */
507/* ARGSUSED */
508static void
509xfs_dir2_sf_addname_hard(
510 xfs_da_args_t *args, /* operation arguments */
511 int objchange, /* changing inode number size */
512 int new_isize) /* new directory size */
513{
Christoph Hellwig50f6bb62019-11-08 15:02:38 -0800514 struct xfs_inode *dp = args->dp;
515 struct xfs_mount *mp = dp->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 int add_datasize; /* data size need for new ent */
517 char *buf; /* buffer for old */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 int eof; /* reached end of old dir */
519 int nbytes; /* temp for byte copies */
520 xfs_dir2_data_aoff_t new_offset; /* next offset value */
521 xfs_dir2_data_aoff_t offset; /* current offset value */
522 int old_isize; /* previous di_size */
523 xfs_dir2_sf_entry_t *oldsfep; /* entry in original dir */
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200524 xfs_dir2_sf_hdr_t *oldsfp; /* original shortform dir */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 xfs_dir2_sf_entry_t *sfep; /* entry in new dir */
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200526 xfs_dir2_sf_hdr_t *sfp; /* new shortform dir */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527
528 /*
529 * Copy the old directory to the stack buffer.
530 */
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200531 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 old_isize = (int)dp->i_d.di_size;
Tetsuo Handa707e0dd2019-08-26 12:06:22 -0700533 buf = kmem_alloc(old_isize, 0);
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200534 oldsfp = (xfs_dir2_sf_hdr_t *)buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 memcpy(oldsfp, sfp, old_isize);
536 /*
537 * Loop over the old directory finding the place we're going
538 * to insert the new entry.
539 * If it's going to end up at the end then oldsfep will point there.
540 */
Christoph Hellwigd73e1ce2019-11-08 15:05:38 -0800541 for (offset = args->geo->data_first_offset,
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000542 oldsfep = xfs_dir2_sf_firstentry(oldsfp),
Christoph Hellwigfdbb8c52019-11-08 15:05:37 -0800543 add_datasize = xfs_dir2_data_entsize(mp, args->namelen),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 eof = (char *)oldsfep == &buf[old_isize];
545 !eof;
Christoph Hellwigfdbb8c52019-11-08 15:05:37 -0800546 offset = new_offset + xfs_dir2_data_entsize(mp, oldsfep->namelen),
Christoph Hellwig50f6bb62019-11-08 15:02:38 -0800547 oldsfep = xfs_dir2_sf_nextentry(mp, oldsfp, oldsfep),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 eof = (char *)oldsfep == &buf[old_isize]) {
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000549 new_offset = xfs_dir2_sf_get_offset(oldsfep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 if (offset + add_datasize <= new_offset)
551 break;
552 }
553 /*
554 * Get rid of the old directory, then allocate space for
555 * the new one. We do this so xfs_idata_realloc won't copy
556 * the data.
557 */
558 xfs_idata_realloc(dp, -old_isize, XFS_DATA_FORK);
559 xfs_idata_realloc(dp, new_isize, XFS_DATA_FORK);
560 /*
561 * Reset the pointer since the buffer was reallocated.
562 */
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200563 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 /*
565 * Copy the first part of the directory, including the header.
566 */
567 nbytes = (int)((char *)oldsfep - (char *)oldsfp);
568 memcpy(sfp, oldsfp, nbytes);
569 sfep = (xfs_dir2_sf_entry_t *)((char *)sfp + nbytes);
570 /*
571 * Fill in the new entry, and update the header counts.
572 */
573 sfep->namelen = args->namelen;
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000574 xfs_dir2_sf_put_offset(sfep, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 memcpy(sfep->name, args->name, sfep->namelen);
Christoph Hellwig93b1e962019-11-08 15:02:59 -0800576 xfs_dir2_sf_put_ino(mp, sfp, sfep, args->inumber);
Christoph Hellwig4501ed22019-11-08 15:03:30 -0800577 xfs_dir2_sf_put_ftype(mp, sfep, args->filetype);
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200578 sfp->count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 if (args->inumber > XFS_DIR2_MAX_SHORT_INUM && !objchange)
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200580 sfp->i8count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 /*
582 * If there's more left to copy, do that.
583 */
584 if (!eof) {
Christoph Hellwig50f6bb62019-11-08 15:02:38 -0800585 sfep = xfs_dir2_sf_nextentry(mp, sfp, sfep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 memcpy(sfep, oldsfep, old_isize - nbytes);
587 }
Denys Vlasenkof0e2d932008-05-19 16:31:57 +1000588 kmem_free(buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 dp->i_d.di_size = new_isize;
590 xfs_dir2_sf_check(args);
591}
592
593/*
594 * Decide if the new entry will fit at all.
595 * If it will fit, pick between adding the new entry to the end (easy)
596 * or somewhere else (hard).
597 * Return 0 (won't fit), 1 (easy), 2 (hard).
598 */
599/*ARGSUSED*/
600static int /* pick result */
601xfs_dir2_sf_addname_pick(
602 xfs_da_args_t *args, /* operation arguments */
603 int objchange, /* inode # size changes */
604 xfs_dir2_sf_entry_t **sfepp, /* out(1): new entry ptr */
605 xfs_dir2_data_aoff_t *offsetp) /* out(1): new offset */
606{
Christoph Hellwig50f6bb62019-11-08 15:02:38 -0800607 struct xfs_inode *dp = args->dp;
608 struct xfs_mount *mp = dp->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 int holefit; /* found hole it will fit in */
610 int i; /* entry number */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 xfs_dir2_data_aoff_t offset; /* data block offset */
612 xfs_dir2_sf_entry_t *sfep; /* shortform entry */
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200613 xfs_dir2_sf_hdr_t *sfp; /* shortform structure */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 int size; /* entry's data size */
615 int used; /* data bytes used */
616
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200617 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
Christoph Hellwigfdbb8c52019-11-08 15:05:37 -0800618 size = xfs_dir2_data_entsize(mp, args->namelen);
Christoph Hellwigd73e1ce2019-11-08 15:05:38 -0800619 offset = args->geo->data_first_offset;
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000620 sfep = xfs_dir2_sf_firstentry(sfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 holefit = 0;
622 /*
623 * Loop over sf entries.
624 * Keep track of data offset and whether we've seen a place
625 * to insert the new entry.
626 */
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200627 for (i = 0; i < sfp->count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 if (!holefit)
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000629 holefit = offset + size <= xfs_dir2_sf_get_offset(sfep);
630 offset = xfs_dir2_sf_get_offset(sfep) +
Christoph Hellwigfdbb8c52019-11-08 15:05:37 -0800631 xfs_dir2_data_entsize(mp, sfep->namelen);
Christoph Hellwig50f6bb62019-11-08 15:02:38 -0800632 sfep = xfs_dir2_sf_nextentry(mp, sfp, sfep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 }
634 /*
635 * Calculate data bytes used excluding the new entry, if this
636 * was a data block (block form directory).
637 */
638 used = offset +
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200639 (sfp->count + 3) * (uint)sizeof(xfs_dir2_leaf_entry_t) +
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 (uint)sizeof(xfs_dir2_block_tail_t);
641 /*
642 * If it won't fit in a block form then we can't insert it,
643 * we'll go back, convert to block, then try the insert and convert
644 * to leaf.
645 */
Dave Chinner8f661932014-06-06 15:15:59 +1000646 if (used + (holefit ? 0 : size) > args->geo->blksize)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 return 0;
648 /*
649 * If changing the inode number size, do it the hard way.
650 */
Christoph Hellwigd5cf09b2014-07-30 09:12:05 +1000651 if (objchange)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 return 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 /*
654 * If it won't fit at the end then do it the hard way (use the hole).
655 */
Dave Chinner8f661932014-06-06 15:15:59 +1000656 if (used + size > args->geo->blksize)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 return 2;
658 /*
659 * Do it the easy way.
660 */
661 *sfepp = sfep;
662 *offsetp = offset;
663 return 1;
664}
665
666#ifdef DEBUG
667/*
668 * Check consistency of shortform directory, assert if bad.
669 */
670static void
671xfs_dir2_sf_check(
672 xfs_da_args_t *args) /* operation arguments */
673{
Christoph Hellwig4501ed22019-11-08 15:03:30 -0800674 struct xfs_inode *dp = args->dp;
675 struct xfs_mount *mp = dp->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 int i; /* entry number */
677 int i8count; /* number of big inode#s */
678 xfs_ino_t ino; /* entry inode number */
679 int offset; /* data offset */
680 xfs_dir2_sf_entry_t *sfep; /* shortform dir entry */
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200681 xfs_dir2_sf_hdr_t *sfp; /* shortform structure */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200683 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
Christoph Hellwigd73e1ce2019-11-08 15:05:38 -0800684 offset = args->geo->data_first_offset;
Christoph Hellwig84915e12019-11-08 15:02:31 -0800685 ino = xfs_dir2_sf_get_parent_ino(sfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 i8count = ino > XFS_DIR2_MAX_SHORT_INUM;
687
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000688 for (i = 0, sfep = xfs_dir2_sf_firstentry(sfp);
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200689 i < sfp->count;
Christoph Hellwig4501ed22019-11-08 15:03:30 -0800690 i++, sfep = xfs_dir2_sf_nextentry(mp, sfp, sfep)) {
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000691 ASSERT(xfs_dir2_sf_get_offset(sfep) >= offset);
Christoph Hellwig4501ed22019-11-08 15:03:30 -0800692 ino = xfs_dir2_sf_get_ino(mp, sfp, sfep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 i8count += ino > XFS_DIR2_MAX_SHORT_INUM;
694 offset =
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000695 xfs_dir2_sf_get_offset(sfep) +
Christoph Hellwigfdbb8c52019-11-08 15:05:37 -0800696 xfs_dir2_data_entsize(mp, sfep->namelen);
Christoph Hellwig4501ed22019-11-08 15:03:30 -0800697 ASSERT(xfs_dir2_sf_get_ftype(mp, sfep) < XFS_DIR3_FT_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 }
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200699 ASSERT(i8count == sfp->i8count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 ASSERT((char *)sfep - (char *)sfp == dp->i_d.di_size);
701 ASSERT(offset +
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200702 (sfp->count + 2) * (uint)sizeof(xfs_dir2_leaf_entry_t) +
Dave Chinner8f661932014-06-06 15:15:59 +1000703 (uint)sizeof(xfs_dir2_block_tail_t) <= args->geo->blksize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704}
705#endif /* DEBUG */
706
Darrick J. Wong630a04e2017-03-15 00:24:25 -0700707/* Verify the consistency of an inline directory. */
Darrick J. Wongdc042c22018-01-08 10:51:06 -0800708xfs_failaddr_t
Darrick J. Wong630a04e2017-03-15 00:24:25 -0700709xfs_dir2_sf_verify(
Darrick J. Wong78420282017-04-03 12:22:20 -0700710 struct xfs_inode *ip)
Darrick J. Wong630a04e2017-03-15 00:24:25 -0700711{
Darrick J. Wong78420282017-04-03 12:22:20 -0700712 struct xfs_mount *mp = ip->i_mount;
Christoph Hellwigf7e67b22020-05-18 10:28:05 -0700713 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
Darrick J. Wong78420282017-04-03 12:22:20 -0700714 struct xfs_dir2_sf_hdr *sfp;
Darrick J. Wong630a04e2017-03-15 00:24:25 -0700715 struct xfs_dir2_sf_entry *sfep;
716 struct xfs_dir2_sf_entry *next_sfep;
717 char *endp;
Darrick J. Wong630a04e2017-03-15 00:24:25 -0700718 xfs_ino_t ino;
719 int i;
720 int i8count;
721 int offset;
Dave Chinner3f8a4f12019-10-17 13:40:33 -0700722 int64_t size;
Darrick J. Wong78420282017-04-03 12:22:20 -0700723 int error;
Darrick J. Wongc8ce5402017-06-16 11:00:05 -0700724 uint8_t filetype;
Darrick J. Wong630a04e2017-03-15 00:24:25 -0700725
Christoph Hellwigf7e67b22020-05-18 10:28:05 -0700726 ASSERT(ifp->if_format == XFS_DINODE_FMT_LOCAL);
Darrick J. Wong630a04e2017-03-15 00:24:25 -0700727
Darrick J. Wong78420282017-04-03 12:22:20 -0700728 sfp = (struct xfs_dir2_sf_hdr *)ifp->if_u1.if_data;
729 size = ifp->if_bytes;
730
Darrick J. Wong630a04e2017-03-15 00:24:25 -0700731 /*
732 * Give up if the directory is way too short.
733 */
Darrick J. Wong78420282017-04-03 12:22:20 -0700734 if (size <= offsetof(struct xfs_dir2_sf_hdr, parent) ||
735 size < xfs_dir2_sf_hdr_size(sfp->i8count))
Darrick J. Wongdc042c22018-01-08 10:51:06 -0800736 return __this_address;
Darrick J. Wong630a04e2017-03-15 00:24:25 -0700737
738 endp = (char *)sfp + size;
739
740 /* Check .. entry */
Christoph Hellwig84915e12019-11-08 15:02:31 -0800741 ino = xfs_dir2_sf_get_parent_ino(sfp);
Darrick J. Wong630a04e2017-03-15 00:24:25 -0700742 i8count = ino > XFS_DIR2_MAX_SHORT_INUM;
Darrick J. Wong78420282017-04-03 12:22:20 -0700743 error = xfs_dir_ino_validate(mp, ino);
744 if (error)
Darrick J. Wongdc042c22018-01-08 10:51:06 -0800745 return __this_address;
Christoph Hellwigd73e1ce2019-11-08 15:05:38 -0800746 offset = mp->m_dir_geo->data_first_offset;
Darrick J. Wong630a04e2017-03-15 00:24:25 -0700747
748 /* Check all reported entries */
749 sfep = xfs_dir2_sf_firstentry(sfp);
750 for (i = 0; i < sfp->count; i++) {
751 /*
752 * struct xfs_dir2_sf_entry has a variable length.
753 * Check the fixed-offset parts of the structure are
754 * within the data buffer.
755 */
Darrick J. Wong78420282017-04-03 12:22:20 -0700756 if (((char *)sfep + sizeof(*sfep)) >= endp)
Darrick J. Wongdc042c22018-01-08 10:51:06 -0800757 return __this_address;
Darrick J. Wong630a04e2017-03-15 00:24:25 -0700758
759 /* Don't allow names with known bad length. */
Darrick J. Wong78420282017-04-03 12:22:20 -0700760 if (sfep->namelen == 0)
Darrick J. Wongdc042c22018-01-08 10:51:06 -0800761 return __this_address;
Darrick J. Wong630a04e2017-03-15 00:24:25 -0700762
763 /*
764 * Check that the variable-length part of the structure is
765 * within the data buffer. The next entry starts after the
766 * name component, so nextentry is an acceptable test.
767 */
Christoph Hellwig50f6bb62019-11-08 15:02:38 -0800768 next_sfep = xfs_dir2_sf_nextentry(mp, sfp, sfep);
Darrick J. Wong78420282017-04-03 12:22:20 -0700769 if (endp < (char *)next_sfep)
Darrick J. Wongdc042c22018-01-08 10:51:06 -0800770 return __this_address;
Darrick J. Wong630a04e2017-03-15 00:24:25 -0700771
772 /* Check that the offsets always increase. */
Darrick J. Wong78420282017-04-03 12:22:20 -0700773 if (xfs_dir2_sf_get_offset(sfep) < offset)
Darrick J. Wongdc042c22018-01-08 10:51:06 -0800774 return __this_address;
Darrick J. Wong630a04e2017-03-15 00:24:25 -0700775
776 /* Check the inode number. */
Christoph Hellwig93b1e962019-11-08 15:02:59 -0800777 ino = xfs_dir2_sf_get_ino(mp, sfp, sfep);
Darrick J. Wong630a04e2017-03-15 00:24:25 -0700778 i8count += ino > XFS_DIR2_MAX_SHORT_INUM;
Darrick J. Wong78420282017-04-03 12:22:20 -0700779 error = xfs_dir_ino_validate(mp, ino);
780 if (error)
Darrick J. Wongdc042c22018-01-08 10:51:06 -0800781 return __this_address;
Darrick J. Wong630a04e2017-03-15 00:24:25 -0700782
783 /* Check the file type. */
Christoph Hellwig4501ed22019-11-08 15:03:30 -0800784 filetype = xfs_dir2_sf_get_ftype(mp, sfep);
Darrick J. Wong78420282017-04-03 12:22:20 -0700785 if (filetype >= XFS_DIR3_FT_MAX)
Darrick J. Wongdc042c22018-01-08 10:51:06 -0800786 return __this_address;
Darrick J. Wong630a04e2017-03-15 00:24:25 -0700787
788 offset = xfs_dir2_sf_get_offset(sfep) +
Christoph Hellwigfdbb8c52019-11-08 15:05:37 -0800789 xfs_dir2_data_entsize(mp, sfep->namelen);
Darrick J. Wong630a04e2017-03-15 00:24:25 -0700790
791 sfep = next_sfep;
792 }
Darrick J. Wong78420282017-04-03 12:22:20 -0700793 if (i8count != sfp->i8count)
Darrick J. Wongdc042c22018-01-08 10:51:06 -0800794 return __this_address;
Darrick J. Wong78420282017-04-03 12:22:20 -0700795 if ((void *)sfep != (void *)endp)
Darrick J. Wongdc042c22018-01-08 10:51:06 -0800796 return __this_address;
Darrick J. Wong630a04e2017-03-15 00:24:25 -0700797
798 /* Make sure this whole thing ought to be in local format. */
Darrick J. Wong78420282017-04-03 12:22:20 -0700799 if (offset + (sfp->count + 2) * (uint)sizeof(xfs_dir2_leaf_entry_t) +
800 (uint)sizeof(xfs_dir2_block_tail_t) > mp->m_dir_geo->blksize)
Darrick J. Wongdc042c22018-01-08 10:51:06 -0800801 return __this_address;
Darrick J. Wong630a04e2017-03-15 00:24:25 -0700802
Darrick J. Wongdc042c22018-01-08 10:51:06 -0800803 return NULL;
Darrick J. Wong630a04e2017-03-15 00:24:25 -0700804}
805
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806/*
807 * Create a new (shortform) directory.
808 */
809int /* error, always 0 */
810xfs_dir2_sf_create(
811 xfs_da_args_t *args, /* operation arguments */
812 xfs_ino_t pino) /* parent inode number */
813{
814 xfs_inode_t *dp; /* incore directory inode */
815 int i8count; /* parent inode is an 8-byte number */
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200816 xfs_dir2_sf_hdr_t *sfp; /* shortform structure */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 int size; /* directory size */
818
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000819 trace_xfs_dir2_sf_create(args);
820
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 dp = args->dp;
822
823 ASSERT(dp != NULL);
824 ASSERT(dp->i_d.di_size == 0);
825 /*
826 * If it's currently a zero-length extent file,
827 * convert it to local format.
828 */
Christoph Hellwigf7e67b22020-05-18 10:28:05 -0700829 if (dp->i_df.if_format == XFS_DINODE_FMT_EXTENTS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 dp->i_df.if_flags &= ~XFS_IFEXTENTS; /* just in case */
Christoph Hellwigf7e67b22020-05-18 10:28:05 -0700831 dp->i_df.if_format = XFS_DINODE_FMT_LOCAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE);
833 dp->i_df.if_flags |= XFS_IFINLINE;
834 }
835 ASSERT(dp->i_df.if_flags & XFS_IFINLINE);
836 ASSERT(dp->i_df.if_bytes == 0);
837 i8count = pino > XFS_DIR2_MAX_SHORT_INUM;
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000838 size = xfs_dir2_sf_hdr_size(i8count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 /*
840 * Make a buffer for the data.
841 */
842 xfs_idata_realloc(dp, size, XFS_DATA_FORK);
843 /*
844 * Fill in the header,
845 */
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200846 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
847 sfp->i8count = i8count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 /*
849 * Now can put in the inode number, since i8count is set.
850 */
Christoph Hellwig84915e12019-11-08 15:02:31 -0800851 xfs_dir2_sf_put_parent_ino(sfp, pino);
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200852 sfp->count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 dp->i_d.di_size = size;
854 xfs_dir2_sf_check(args);
855 xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE | XFS_ILOG_DDATA);
856 return 0;
857}
858
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859/*
860 * Lookup an entry in a shortform directory.
861 * Returns EEXIST if found, ENOENT if not found.
862 */
863int /* error */
864xfs_dir2_sf_lookup(
865 xfs_da_args_t *args) /* operation arguments */
866{
Christoph Hellwig50f6bb62019-11-08 15:02:38 -0800867 struct xfs_inode *dp = args->dp;
868 struct xfs_mount *mp = dp->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 int i; /* entry index */
Barry Naujok384f3ce2008-05-21 16:58:22 +1000870 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 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 */
Barry Naujok5163f952008-05-21 16:41:01 +1000873 enum xfs_dacmp cmp; /* comparison result */
Barry Naujok384f3ce2008-05-21 16:58:22 +1000874 xfs_dir2_sf_entry_t *ci_sfep; /* case-insens. entry */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000876 trace_xfs_dir2_sf_lookup(args);
877
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 xfs_dir2_sf_check(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879
880 ASSERT(dp->i_df.if_flags & XFS_IFINLINE);
Darrick J. Wonge91ec882019-11-02 09:38:08 -0700881 ASSERT(dp->i_d.di_size >= offsetof(struct xfs_dir2_sf_hdr, parent));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 ASSERT(dp->i_df.if_bytes == dp->i_d.di_size);
883 ASSERT(dp->i_df.if_u1.if_data != NULL);
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200884 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
885 ASSERT(dp->i_d.di_size >= xfs_dir2_sf_hdr_size(sfp->i8count));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 /*
887 * Special case for .
888 */
889 if (args->namelen == 1 && args->name[0] == '.') {
890 args->inumber = dp->i_ino;
Barry Naujok5163f952008-05-21 16:41:01 +1000891 args->cmpresult = XFS_CMP_EXACT;
Dave Chinner1c55cec2013-08-12 20:50:10 +1000892 args->filetype = XFS_DIR3_FT_DIR;
Dave Chinner24513372014-06-25 14:58:08 +1000893 return -EEXIST;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 }
895 /*
896 * Special case for ..
897 */
898 if (args->namelen == 2 &&
899 args->name[0] == '.' && args->name[1] == '.') {
Christoph Hellwig84915e12019-11-08 15:02:31 -0800900 args->inumber = xfs_dir2_sf_get_parent_ino(sfp);
Barry Naujok5163f952008-05-21 16:41:01 +1000901 args->cmpresult = XFS_CMP_EXACT;
Dave Chinner1c55cec2013-08-12 20:50:10 +1000902 args->filetype = XFS_DIR3_FT_DIR;
Dave Chinner24513372014-06-25 14:58:08 +1000903 return -EEXIST;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 }
905 /*
906 * Loop over all the entries trying to match ours.
907 */
Barry Naujok384f3ce2008-05-21 16:58:22 +1000908 ci_sfep = NULL;
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200909 for (i = 0, sfep = xfs_dir2_sf_firstentry(sfp); i < sfp->count;
Christoph Hellwig50f6bb62019-11-08 15:02:38 -0800910 i++, sfep = xfs_dir2_sf_nextentry(mp, sfp, sfep)) {
Barry Naujok5163f952008-05-21 16:41:01 +1000911 /*
912 * Compare name and if it's an exact match, return the inode
913 * number. If it's the first case-insensitive match, store the
914 * inode number and continue looking for an exact match.
915 */
Christoph Hellwigd8d11fc2019-11-11 12:59:26 -0800916 cmp = xfs_dir2_compname(args, sfep->name, sfep->namelen);
Barry Naujok5163f952008-05-21 16:41:01 +1000917 if (cmp != XFS_CMP_DIFFERENT && cmp != args->cmpresult) {
918 args->cmpresult = cmp;
Christoph Hellwig93b1e962019-11-08 15:02:59 -0800919 args->inumber = xfs_dir2_sf_get_ino(mp, sfp, sfep);
Christoph Hellwig4501ed22019-11-08 15:03:30 -0800920 args->filetype = xfs_dir2_sf_get_ftype(mp, sfep);
Barry Naujok5163f952008-05-21 16:41:01 +1000921 if (cmp == XFS_CMP_EXACT)
Dave Chinner24513372014-06-25 14:58:08 +1000922 return -EEXIST;
Barry Naujok384f3ce2008-05-21 16:58:22 +1000923 ci_sfep = sfep;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 }
925 }
Barry Naujok6a178102008-05-21 16:42:05 +1000926 ASSERT(args->op_flags & XFS_DA_OP_OKNOENT);
Barry Naujok5163f952008-05-21 16:41:01 +1000927 /*
928 * Here, we can only be doing a lookup (not a rename or replace).
Dave Chinner24513372014-06-25 14:58:08 +1000929 * If a case-insensitive match was not found, return -ENOENT.
Barry Naujok5163f952008-05-21 16:41:01 +1000930 */
Barry Naujok384f3ce2008-05-21 16:58:22 +1000931 if (!ci_sfep)
Dave Chinner24513372014-06-25 14:58:08 +1000932 return -ENOENT;
Barry Naujok384f3ce2008-05-21 16:58:22 +1000933 /* otherwise process the CI match as required by the caller */
934 error = xfs_dir_cilookup_result(args, ci_sfep->name, ci_sfep->namelen);
Eric Sandeenb474c7a2014-06-22 15:04:54 +1000935 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936}
937
938/*
939 * Remove an entry from a shortform directory.
940 */
941int /* error */
942xfs_dir2_sf_removename(
943 xfs_da_args_t *args)
944{
Christoph Hellwig50f6bb62019-11-08 15:02:38 -0800945 struct xfs_inode *dp = args->dp;
946 struct xfs_mount *mp = dp->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 int byteoff; /* offset of removed entry */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 int entsize; /* this entry's size */
949 int i; /* shortform entry index */
950 int newsize; /* new inode size */
951 int oldsize; /* old inode size */
952 xfs_dir2_sf_entry_t *sfep; /* shortform directory entry */
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200953 xfs_dir2_sf_hdr_t *sfp; /* shortform structure */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000955 trace_xfs_dir2_sf_removename(args);
956
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 ASSERT(dp->i_df.if_flags & XFS_IFINLINE);
958 oldsize = (int)dp->i_d.di_size;
Darrick J. Wonge91ec882019-11-02 09:38:08 -0700959 ASSERT(oldsize >= offsetof(struct xfs_dir2_sf_hdr, parent));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 ASSERT(dp->i_df.if_bytes == oldsize);
961 ASSERT(dp->i_df.if_u1.if_data != NULL);
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200962 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
963 ASSERT(oldsize >= xfs_dir2_sf_hdr_size(sfp->i8count));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 /*
965 * Loop over the old directory entries.
966 * Find the one we're deleting.
967 */
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200968 for (i = 0, sfep = xfs_dir2_sf_firstentry(sfp); i < sfp->count;
Christoph Hellwig50f6bb62019-11-08 15:02:38 -0800969 i++, sfep = xfs_dir2_sf_nextentry(mp, sfp, sfep)) {
Barry Naujok5163f952008-05-21 16:41:01 +1000970 if (xfs_da_compname(args, sfep->name, sfep->namelen) ==
971 XFS_CMP_EXACT) {
Christoph Hellwig93b1e962019-11-08 15:02:59 -0800972 ASSERT(xfs_dir2_sf_get_ino(mp, sfp, sfep) ==
Christoph Hellwig8bc38782011-07-08 14:35:03 +0200973 args->inumber);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 break;
975 }
976 }
977 /*
978 * Didn't find it.
979 */
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200980 if (i == sfp->count)
Dave Chinner24513372014-06-25 14:58:08 +1000981 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 /*
983 * Calculate sizes.
984 */
985 byteoff = (int)((char *)sfep - (char *)sfp);
Christoph Hellwig50f6bb62019-11-08 15:02:38 -0800986 entsize = xfs_dir2_sf_entsize(mp, sfp, args->namelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 newsize = oldsize - entsize;
988 /*
989 * Copy the part if any after the removed entry, sliding it down.
990 */
991 if (byteoff + entsize < oldsize)
992 memmove((char *)sfp + byteoff, (char *)sfp + byteoff + entsize,
993 oldsize - (byteoff + entsize));
994 /*
995 * Fix up the header and file size.
996 */
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200997 sfp->count--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 dp->i_d.di_size = newsize;
999 /*
1000 * Reallocate, making it smaller.
1001 */
1002 xfs_idata_realloc(dp, newsize - oldsize, XFS_DATA_FORK);
Christoph Hellwigac8ba502011-07-08 14:35:13 +02001003 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 /*
1005 * Are we changing inode number size?
1006 */
1007 if (args->inumber > XFS_DIR2_MAX_SHORT_INUM) {
Christoph Hellwigac8ba502011-07-08 14:35:13 +02001008 if (sfp->i8count == 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 xfs_dir2_sf_toino4(args);
1010 else
Christoph Hellwigac8ba502011-07-08 14:35:13 +02001011 sfp->i8count--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 xfs_dir2_sf_check(args);
1014 xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE | XFS_ILOG_DDATA);
1015 return 0;
1016}
1017
1018/*
kaixuxia93597ae2019-11-12 08:34:23 -08001019 * Check whether the sf dir replace operation need more blocks.
1020 */
1021bool
1022xfs_dir2_sf_replace_needblock(
1023 struct xfs_inode *dp,
1024 xfs_ino_t inum)
1025{
1026 int newsize;
1027 struct xfs_dir2_sf_hdr *sfp;
1028
Christoph Hellwigf7e67b22020-05-18 10:28:05 -07001029 if (dp->i_df.if_format != XFS_DINODE_FMT_LOCAL)
kaixuxia93597ae2019-11-12 08:34:23 -08001030 return false;
1031
1032 sfp = (struct xfs_dir2_sf_hdr *)dp->i_df.if_u1.if_data;
1033 newsize = dp->i_df.if_bytes + (sfp->count + 1) * XFS_INO64_DIFF;
1034
1035 return inum > XFS_DIR2_MAX_SHORT_INUM &&
1036 sfp->i8count == 0 && newsize > XFS_IFORK_DSIZE(dp);
1037}
1038
1039/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 * Replace the inode number of an entry in a shortform directory.
1041 */
1042int /* error */
1043xfs_dir2_sf_replace(
1044 xfs_da_args_t *args) /* operation arguments */
1045{
Christoph Hellwig50f6bb62019-11-08 15:02:38 -08001046 struct xfs_inode *dp = args->dp;
1047 struct xfs_mount *mp = dp->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 int i; /* entry index */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 xfs_ino_t ino=0; /* entry old inode number */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 int i8elevated; /* sf_toino8 set i8count=1 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 xfs_dir2_sf_entry_t *sfep; /* shortform directory entry */
Christoph Hellwigac8ba502011-07-08 14:35:13 +02001052 xfs_dir2_sf_hdr_t *sfp; /* shortform structure */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001054 trace_xfs_dir2_sf_replace(args);
1055
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 ASSERT(dp->i_df.if_flags & XFS_IFINLINE);
Darrick J. Wonge91ec882019-11-02 09:38:08 -07001057 ASSERT(dp->i_d.di_size >= offsetof(struct xfs_dir2_sf_hdr, parent));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 ASSERT(dp->i_df.if_bytes == dp->i_d.di_size);
1059 ASSERT(dp->i_df.if_u1.if_data != NULL);
Christoph Hellwigac8ba502011-07-08 14:35:13 +02001060 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
1061 ASSERT(dp->i_d.di_size >= xfs_dir2_sf_hdr_size(sfp->i8count));
Christoph Hellwigd5cf09b2014-07-30 09:12:05 +10001062
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 /*
1064 * New inode number is large, and need to convert to 8-byte inodes.
1065 */
Christoph Hellwigac8ba502011-07-08 14:35:13 +02001066 if (args->inumber > XFS_DIR2_MAX_SHORT_INUM && sfp->i8count == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 int error; /* error return value */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 /*
1070 * Won't fit as shortform, convert to block then do replace.
1071 */
kaixuxia93597ae2019-11-12 08:34:23 -08001072 if (xfs_dir2_sf_replace_needblock(dp, args->inumber)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 error = xfs_dir2_sf_to_block(args);
kaixuxia93597ae2019-11-12 08:34:23 -08001074 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 return xfs_dir2_block_replace(args);
1077 }
1078 /*
1079 * Still fits, convert to 8-byte now.
1080 */
1081 xfs_dir2_sf_toino8(args);
1082 i8elevated = 1;
Christoph Hellwigac8ba502011-07-08 14:35:13 +02001083 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 } else
1085 i8elevated = 0;
Christoph Hellwigd5cf09b2014-07-30 09:12:05 +10001086
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 ASSERT(args->namelen != 1 || args->name[0] != '.');
1088 /*
1089 * Replace ..'s entry.
1090 */
1091 if (args->namelen == 2 &&
1092 args->name[0] == '.' && args->name[1] == '.') {
Christoph Hellwig84915e12019-11-08 15:02:31 -08001093 ino = xfs_dir2_sf_get_parent_ino(sfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 ASSERT(args->inumber != ino);
Christoph Hellwig84915e12019-11-08 15:02:31 -08001095 xfs_dir2_sf_put_parent_ino(sfp, args->inumber);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 }
1097 /*
1098 * Normal entry, look for the name.
1099 */
1100 else {
Dave Chinner0cb97762013-08-12 20:50:09 +10001101 for (i = 0, sfep = xfs_dir2_sf_firstentry(sfp); i < sfp->count;
Christoph Hellwig50f6bb62019-11-08 15:02:38 -08001102 i++, sfep = xfs_dir2_sf_nextentry(mp, sfp, sfep)) {
Barry Naujok5163f952008-05-21 16:41:01 +10001103 if (xfs_da_compname(args, sfep->name, sfep->namelen) ==
1104 XFS_CMP_EXACT) {
Christoph Hellwig93b1e962019-11-08 15:02:59 -08001105 ino = xfs_dir2_sf_get_ino(mp, sfp, sfep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 ASSERT(args->inumber != ino);
Christoph Hellwig93b1e962019-11-08 15:02:59 -08001107 xfs_dir2_sf_put_ino(mp, sfp, sfep,
1108 args->inumber);
Christoph Hellwig4501ed22019-11-08 15:03:30 -08001109 xfs_dir2_sf_put_ftype(mp, sfep, args->filetype);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 break;
1111 }
1112 }
1113 /*
1114 * Didn't find it.
1115 */
Christoph Hellwigac8ba502011-07-08 14:35:13 +02001116 if (i == sfp->count) {
Barry Naujok6a178102008-05-21 16:42:05 +10001117 ASSERT(args->op_flags & XFS_DA_OP_OKNOENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 if (i8elevated)
1119 xfs_dir2_sf_toino4(args);
Dave Chinner24513372014-06-25 14:58:08 +10001120 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121 }
1122 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 /*
1124 * See if the old number was large, the new number is small.
1125 */
1126 if (ino > XFS_DIR2_MAX_SHORT_INUM &&
1127 args->inumber <= XFS_DIR2_MAX_SHORT_INUM) {
1128 /*
1129 * And the old count was one, so need to convert to small.
1130 */
Christoph Hellwigac8ba502011-07-08 14:35:13 +02001131 if (sfp->i8count == 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 xfs_dir2_sf_toino4(args);
1133 else
Christoph Hellwigac8ba502011-07-08 14:35:13 +02001134 sfp->i8count--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 }
1136 /*
1137 * See if the old number was small, the new number is large.
1138 */
1139 if (ino <= XFS_DIR2_MAX_SHORT_INUM &&
1140 args->inumber > XFS_DIR2_MAX_SHORT_INUM) {
1141 /*
1142 * add to the i8count unless we just converted to 8-byte
1143 * inodes (which does an implied i8count = 1)
1144 */
Christoph Hellwigac8ba502011-07-08 14:35:13 +02001145 ASSERT(sfp->i8count != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 if (!i8elevated)
Christoph Hellwigac8ba502011-07-08 14:35:13 +02001147 sfp->i8count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149 xfs_dir2_sf_check(args);
1150 xfs_trans_log_inode(args->trans, dp, XFS_ILOG_DDATA);
1151 return 0;
1152}
1153
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154/*
1155 * Convert from 8-byte inode numbers to 4-byte inode numbers.
1156 * The last 8-byte inode number is gone, but the count is still 1.
1157 */
1158static void
1159xfs_dir2_sf_toino4(
1160 xfs_da_args_t *args) /* operation arguments */
1161{
Christoph Hellwig50f6bb62019-11-08 15:02:38 -08001162 struct xfs_inode *dp = args->dp;
1163 struct xfs_mount *mp = dp->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 char *buf; /* old dir's buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 int i; /* entry index */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 int newsize; /* new inode size */
1167 xfs_dir2_sf_entry_t *oldsfep; /* old sf entry */
Christoph Hellwigac8ba502011-07-08 14:35:13 +02001168 xfs_dir2_sf_hdr_t *oldsfp; /* old sf directory */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 int oldsize; /* old inode size */
1170 xfs_dir2_sf_entry_t *sfep; /* new sf entry */
Christoph Hellwigac8ba502011-07-08 14:35:13 +02001171 xfs_dir2_sf_hdr_t *sfp; /* new sf directory */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001173 trace_xfs_dir2_sf_toino4(args);
1174
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 /*
1176 * Copy the old directory to the buffer.
1177 * Then nuke it from the inode, and add the new buffer to the inode.
1178 * Don't want xfs_idata_realloc copying the data here.
1179 */
1180 oldsize = dp->i_df.if_bytes;
Tetsuo Handa707e0dd2019-08-26 12:06:22 -07001181 buf = kmem_alloc(oldsize, 0);
Christoph Hellwigac8ba502011-07-08 14:35:13 +02001182 oldsfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
1183 ASSERT(oldsfp->i8count == 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 memcpy(buf, oldsfp, oldsize);
1185 /*
1186 * Compute the new inode size.
1187 */
Christoph Hellwig266b6962016-07-20 11:48:31 +10001188 newsize = oldsize - (oldsfp->count + 1) * XFS_INO64_DIFF;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 xfs_idata_realloc(dp, -oldsize, XFS_DATA_FORK);
1190 xfs_idata_realloc(dp, newsize, XFS_DATA_FORK);
1191 /*
1192 * Reset our pointers, the data has moved.
1193 */
Christoph Hellwigac8ba502011-07-08 14:35:13 +02001194 oldsfp = (xfs_dir2_sf_hdr_t *)buf;
1195 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 /*
1197 * Fill in the new header.
1198 */
Christoph Hellwigac8ba502011-07-08 14:35:13 +02001199 sfp->count = oldsfp->count;
1200 sfp->i8count = 0;
Christoph Hellwig84915e12019-11-08 15:02:31 -08001201 xfs_dir2_sf_put_parent_ino(sfp, xfs_dir2_sf_get_parent_ino(oldsfp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 /*
1203 * Copy the entries field by field.
1204 */
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001205 for (i = 0, sfep = xfs_dir2_sf_firstentry(sfp),
1206 oldsfep = xfs_dir2_sf_firstentry(oldsfp);
Christoph Hellwigac8ba502011-07-08 14:35:13 +02001207 i < sfp->count;
Christoph Hellwig50f6bb62019-11-08 15:02:38 -08001208 i++, sfep = xfs_dir2_sf_nextentry(mp, sfp, sfep),
1209 oldsfep = xfs_dir2_sf_nextentry(mp, oldsfp, oldsfep)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210 sfep->namelen = oldsfep->namelen;
Christoph Hellwig8353a642016-07-20 11:47:21 +10001211 memcpy(sfep->offset, oldsfep->offset, sizeof(sfep->offset));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 memcpy(sfep->name, oldsfep->name, sfep->namelen);
Christoph Hellwig93b1e962019-11-08 15:02:59 -08001213 xfs_dir2_sf_put_ino(mp, sfp, sfep,
1214 xfs_dir2_sf_get_ino(mp, oldsfp, oldsfep));
Christoph Hellwig4501ed22019-11-08 15:03:30 -08001215 xfs_dir2_sf_put_ftype(mp, sfep,
1216 xfs_dir2_sf_get_ftype(mp, oldsfep));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217 }
1218 /*
1219 * Clean up the inode.
1220 */
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10001221 kmem_free(buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 dp->i_d.di_size = newsize;
1223 xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE | XFS_ILOG_DDATA);
1224}
1225
1226/*
Eric Sandeen5e06d142014-04-14 19:07:23 +10001227 * Convert existing entries from 4-byte inode numbers to 8-byte inode numbers.
1228 * The new entry w/ an 8-byte inode number is not there yet; we leave with
1229 * i8count set to 1, but no corresponding 8-byte entry.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 */
1231static void
1232xfs_dir2_sf_toino8(
1233 xfs_da_args_t *args) /* operation arguments */
1234{
Christoph Hellwig50f6bb62019-11-08 15:02:38 -08001235 struct xfs_inode *dp = args->dp;
1236 struct xfs_mount *mp = dp->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237 char *buf; /* old dir's buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238 int i; /* entry index */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 int newsize; /* new inode size */
1240 xfs_dir2_sf_entry_t *oldsfep; /* old sf entry */
Christoph Hellwigac8ba502011-07-08 14:35:13 +02001241 xfs_dir2_sf_hdr_t *oldsfp; /* old sf directory */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 int oldsize; /* old inode size */
1243 xfs_dir2_sf_entry_t *sfep; /* new sf entry */
Christoph Hellwigac8ba502011-07-08 14:35:13 +02001244 xfs_dir2_sf_hdr_t *sfp; /* new sf directory */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001246 trace_xfs_dir2_sf_toino8(args);
1247
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248 /*
1249 * Copy the old directory to the buffer.
1250 * Then nuke it from the inode, and add the new buffer to the inode.
1251 * Don't want xfs_idata_realloc copying the data here.
1252 */
1253 oldsize = dp->i_df.if_bytes;
Tetsuo Handa707e0dd2019-08-26 12:06:22 -07001254 buf = kmem_alloc(oldsize, 0);
Christoph Hellwigac8ba502011-07-08 14:35:13 +02001255 oldsfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
1256 ASSERT(oldsfp->i8count == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257 memcpy(buf, oldsfp, oldsize);
1258 /*
Eric Sandeen5e06d142014-04-14 19:07:23 +10001259 * Compute the new inode size (nb: entry count + 1 for parent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 */
Christoph Hellwig266b6962016-07-20 11:48:31 +10001261 newsize = oldsize + (oldsfp->count + 1) * XFS_INO64_DIFF;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 xfs_idata_realloc(dp, -oldsize, XFS_DATA_FORK);
1263 xfs_idata_realloc(dp, newsize, XFS_DATA_FORK);
1264 /*
1265 * Reset our pointers, the data has moved.
1266 */
Christoph Hellwigac8ba502011-07-08 14:35:13 +02001267 oldsfp = (xfs_dir2_sf_hdr_t *)buf;
1268 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 /*
1270 * Fill in the new header.
1271 */
Christoph Hellwigac8ba502011-07-08 14:35:13 +02001272 sfp->count = oldsfp->count;
1273 sfp->i8count = 1;
Christoph Hellwig84915e12019-11-08 15:02:31 -08001274 xfs_dir2_sf_put_parent_ino(sfp, xfs_dir2_sf_get_parent_ino(oldsfp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 /*
1276 * Copy the entries field by field.
1277 */
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001278 for (i = 0, sfep = xfs_dir2_sf_firstentry(sfp),
1279 oldsfep = xfs_dir2_sf_firstentry(oldsfp);
Christoph Hellwigac8ba502011-07-08 14:35:13 +02001280 i < sfp->count;
Christoph Hellwig50f6bb62019-11-08 15:02:38 -08001281 i++, sfep = xfs_dir2_sf_nextentry(mp, sfp, sfep),
1282 oldsfep = xfs_dir2_sf_nextentry(mp, oldsfp, oldsfep)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 sfep->namelen = oldsfep->namelen;
Christoph Hellwig8353a642016-07-20 11:47:21 +10001284 memcpy(sfep->offset, oldsfep->offset, sizeof(sfep->offset));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 memcpy(sfep->name, oldsfep->name, sfep->namelen);
Christoph Hellwig93b1e962019-11-08 15:02:59 -08001286 xfs_dir2_sf_put_ino(mp, sfp, sfep,
1287 xfs_dir2_sf_get_ino(mp, oldsfp, oldsfep));
Christoph Hellwig4501ed22019-11-08 15:03:30 -08001288 xfs_dir2_sf_put_ftype(mp, sfep,
1289 xfs_dir2_sf_get_ftype(mp, oldsfep));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290 }
1291 /*
1292 * Clean up the inode.
1293 */
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10001294 kmem_free(buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 dp->i_d.di_size = newsize;
1296 xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE | XFS_ILOG_DDATA);
1297}