Merge tag 'mm-hotfixes-stable-2025-07-11-16-16' of git://git.kernel.org/pub/scm/linux...
[linux-block.git] / fs / xfs / xfs_symlink.c
CommitLineData
0b61f8a4 1// SPDX-License-Identifier: GPL-2.0
19de7351
DC
2/*
3 * Copyright (c) 2000-2006 Silicon Graphics, Inc.
4 * Copyright (c) 2012-2013 Red Hat, Inc.
5 * All rights reserved.
19de7351
DC
6 */
7#include "xfs.h"
239880ef 8#include "xfs_shared.h"
19de7351 9#include "xfs_fs.h"
6ca1c906 10#include "xfs_format.h"
239880ef
DC
11#include "xfs_log_format.h"
12#include "xfs_trans_resv.h"
19de7351 13#include "xfs_bit.h"
19de7351 14#include "xfs_mount.h"
2b9ab5ab 15#include "xfs_dir2.h"
19de7351 16#include "xfs_inode.h"
19de7351 17#include "xfs_bmap.h"
a4fbe6ab 18#include "xfs_bmap_btree.h"
19de7351 19#include "xfs_quota.h"
5f213ddb 20#include "xfs_symlink.h"
19de7351 21#include "xfs_trans_space.h"
19de7351 22#include "xfs_trace.h"
239880ef 23#include "xfs_trans.h"
b652afd9 24#include "xfs_ialloc.h"
7b7820b8 25#include "xfs_error.h"
d9041681 26#include "xfs_health.h"
622d88e2 27#include "xfs_symlink_remote.h"
5d31a85d
AH
28#include "xfs_parent.h"
29#include "xfs_defer.h"
19de7351 30
19de7351
DC
31int
32xfs_readlink(
7b7820b8
DW
33 struct xfs_inode *ip,
34 char *link)
19de7351 35{
7b7820b8
DW
36 struct xfs_mount *mp = ip->i_mount;
37 xfs_fsize_t pathlen;
b280fb0c 38 int error;
19de7351
DC
39
40 trace_xfs_readlink(ip);
41
75c8c50f 42 if (xfs_is_shutdown(mp))
2451337d 43 return -EIO;
d9041681
DW
44 if (xfs_ifork_zapped(ip, XFS_DATA_FORK))
45 return -EIO;
19de7351
DC
46
47 xfs_ilock(ip, XFS_ILOCK_SHARED);
48
13d2c10b 49 pathlen = ip->i_disk_size;
19de7351 50 if (!pathlen)
b280fb0c 51 goto out_corrupt;
19de7351 52
6eb0b8df 53 if (pathlen < 0 || pathlen > XFS_SYMLINK_MAXLEN) {
19de7351
DC
54 xfs_alert(mp, "%s: inode (%llu) bad symlink length (%lld)",
55 __func__, (unsigned long long) ip->i_ino,
56 (long long) pathlen);
57 ASSERT(0);
b280fb0c 58 goto out_corrupt;
19de7351
DC
59 }
60
7b7820b8
DW
61 if (ip->i_df.if_format == XFS_DINODE_FMT_LOCAL) {
62 /*
63 * The VFS crashes on a NULL pointer, so return -EFSCORRUPTED
64 * if if_data is junk.
65 */
6e145f94 66 if (XFS_IS_CORRUPT(ip->i_mount, !ip->i_df.if_data))
b280fb0c 67 goto out_corrupt;
7b7820b8 68
6e145f94 69 memcpy(link, ip->i_df.if_data, pathlen + 1);
7b7820b8
DW
70 error = 0;
71 } else {
376b4f05 72 error = xfs_symlink_remote_read(ip, link);
7b7820b8 73 }
19de7351 74
19de7351
DC
75 xfs_iunlock(ip, XFS_ILOCK_SHARED);
76 return error;
b280fb0c
DW
77 out_corrupt:
78 xfs_iunlock(ip, XFS_ILOCK_SHARED);
79 xfs_inode_mark_sick(ip, XFS_SICK_INO_SYMLINK);
80 return -EFSCORRUPTED;
19de7351
DC
81}
82
83int
84xfs_symlink(
f2d40141 85 struct mnt_idmap *idmap,
f948dd76 86 struct xfs_inode *dp,
19de7351
DC
87 struct xfs_name *link_name,
88 const char *target_path,
89 umode_t mode,
f948dd76 90 struct xfs_inode **ipp)
19de7351 91{
f948dd76 92 struct xfs_mount *mp = dp->i_mount;
ba4b39fe
DW
93 struct xfs_icreate_args args = {
94 .idmap = idmap,
95 .pip = dp,
96 .mode = S_IFLNK | (mode & ~S_IFMT),
97 };
1fa2e819
DW
98 struct xfs_dir_update du = {
99 .dp = dp,
100 .name = link_name,
101 };
f948dd76 102 struct xfs_trans *tp = NULL;
f948dd76 103 int error = 0;
19de7351 104 int pathlen;
58c90473 105 bool unlock_dp_on_error = false;
19de7351 106 xfs_filblks_t fs_blocks;
c0223b8d
DW
107 struct xfs_dquot *udqp;
108 struct xfs_dquot *gdqp;
109 struct xfs_dquot *pdqp;
19de7351 110 uint resblks;
b652afd9 111 xfs_ino_t ino;
19de7351
DC
112
113 *ipp = NULL;
19de7351
DC
114
115 trace_xfs_symlink(dp, link_name);
116
75c8c50f 117 if (xfs_is_shutdown(mp))
2451337d 118 return -EIO;
19de7351
DC
119
120 /*
121 * Check component lengths of the target path name.
122 */
123 pathlen = strlen(target_path);
6eb0b8df 124 if (pathlen >= XFS_SYMLINK_MAXLEN) /* total string too long */
2451337d 125 return -ENAMETOOLONG;
43feeea8 126 ASSERT(pathlen > 0);
19de7351 127
c0223b8d
DW
128 /* Make sure that we have allocated dquot(s) on disk. */
129 error = xfs_icreate_dqalloc(&args, &udqp, &gdqp, &pdqp);
19de7351 130 if (error)
58c90473 131 return error;
19de7351 132
19de7351
DC
133 /*
134 * The symlink will fit into the inode data fork?
5d31a85d
AH
135 * If there are no parent pointers, then there wont't be any attributes.
136 * So we get the whole variable part, and do not need to reserve extra
137 * blocks. Otherwise, we need to reserve the blocks.
19de7351 138 */
5d31a85d 139 if (pathlen <= XFS_LITINO(mp) && !xfs_has_parent(mp))
19de7351
DC
140 fs_blocks = 0;
141 else
321a9583 142 fs_blocks = xfs_symlink_blocks(mp, pathlen);
5d31a85d
AH
143 resblks = xfs_symlink_space_res(mp, link_name->len, fs_blocks);
144
1fa2e819 145 error = xfs_parent_start(mp, &du.ppargs);
5d31a85d
AH
146 if (error)
147 goto out_release_dquots;
253f4911 148
f2f7b9ff
DW
149 error = xfs_trans_alloc_icreate(mp, &M_RES(mp)->tr_symlink, udqp, gdqp,
150 pdqp, resblks, &tp);
4906e215 151 if (error)
5d31a85d 152 goto out_parent;
19de7351 153
65523218 154 xfs_ilock(dp, XFS_ILOCK_EXCL | XFS_ILOCK_PARENT);
19de7351
DC
155 unlock_dp_on_error = true;
156
157 /*
158 * Check whether the directory allows new symlinks or not.
159 */
db07349d 160 if (dp->i_diflags & XFS_DIFLAG_NOSYMLINKS) {
2451337d 161 error = -EPERM;
58c90473 162 goto out_trans_cancel;
19de7351
DC
163 }
164
19de7351
DC
165 /*
166 * Allocate an inode for the symlink.
167 */
390b4775 168 error = xfs_dialloc(&tp, &args, &ino);
b652afd9 169 if (!error)
1fa2e819 170 error = xfs_icreate(tp, ino, &args, &du.ip);
58c90473
DC
171 if (error)
172 goto out_trans_cancel;
19de7351
DC
173
174 /*
58c90473
DC
175 * Now we join the directory inode to the transaction. We do not do it
176 * earlier because xfs_dir_ialloc might commit the previous transaction
177 * (and release all the locks). An error from here on will result in
178 * the transaction cancel unlocking dp so don't do it explicitly in the
19de7351
DC
179 * error path.
180 */
267979b4 181 xfs_trans_ijoin(tp, dp, 0);
19de7351
DC
182
183 /*
184 * Also attach the dquot(s) to it, if applicable.
185 */
1fa2e819 186 xfs_qm_vop_create_dqattach(tp, du.ip, udqp, gdqp, pdqp);
19de7351 187
57fd2d8f 188 resblks -= XFS_IALLOC_SPACE_RES(mp);
1fa2e819 189 error = xfs_symlink_write_target(tp, du.ip, du.ip->i_ino, target_path,
ea8214c3 190 pathlen, fs_blocks, resblks);
b8102b61
DW
191 if (error)
192 goto out_trans_cancel;
193 resblks -= fs_blocks;
1fa2e819 194 i_size_write(VFS_I(du.ip), du.ip->i_disk_size);
19de7351
DC
195
196 /*
197 * Create the directory entry for the symlink.
198 */
1fa2e819 199 error = xfs_dir_create_child(tp, resblks, &du);
19de7351 200 if (error)
c8eac49e 201 goto out_trans_cancel;
5d31a85d 202
19de7351
DC
203 /*
204 * If this is a synchronous mount, make sure that the
205 * symlink transaction goes to disk before returning to
206 * the user.
207 */
0560f31a 208 if (xfs_has_wsync(mp) || xfs_has_dirsync(mp))
19de7351 209 xfs_trans_set_sync(tp);
19de7351 210
70393313 211 error = xfs_trans_commit(tp);
58c90473
DC
212 if (error)
213 goto out_release_inode;
214
19de7351
DC
215 xfs_qm_dqrele(udqp);
216 xfs_qm_dqrele(gdqp);
92f8ff73 217 xfs_qm_dqrele(pdqp);
19de7351 218
1fa2e819
DW
219 *ipp = du.ip;
220 xfs_iunlock(du.ip, XFS_ILOCK_EXCL);
267979b4 221 xfs_iunlock(dp, XFS_ILOCK_EXCL);
1fa2e819 222 xfs_parent_finish(mp, du.ppargs);
19de7351
DC
223 return 0;
224
58c90473 225out_trans_cancel:
4906e215 226 xfs_trans_cancel(tp);
58c90473
DC
227out_release_inode:
228 /*
229 * Wait until after the current transaction is aborted to finish the
230 * setup of the inode and release the inode. This prevents recursive
231 * transactions and deadlocks from xfs_inactive.
232 */
1fa2e819
DW
233 if (du.ip) {
234 xfs_iunlock(du.ip, XFS_ILOCK_EXCL);
235 xfs_finish_inode_setup(du.ip);
236 xfs_irele(du.ip);
58c90473 237 }
5d31a85d 238out_parent:
1fa2e819 239 xfs_parent_finish(mp, du.ppargs);
f2f7b9ff 240out_release_dquots:
19de7351
DC
241 xfs_qm_dqrele(udqp);
242 xfs_qm_dqrele(gdqp);
92f8ff73 243 xfs_qm_dqrele(pdqp);
19de7351
DC
244
245 if (unlock_dp_on_error)
65523218 246 xfs_iunlock(dp, XFS_ILOCK_EXCL);
19de7351
DC
247 return error;
248}
249
250/*
251 * Free a symlink that has blocks associated with it.
43feeea8
DC
252 *
253 * Note: zero length symlinks are not allowed to exist. When we set the size to
254 * zero, also change it to a regular file so that it does not get written to
255 * disk as a zero length symlink. The inode is on the unlinked list already, so
256 * userspace cannot find this inode anymore, so this change is not user visible
257 * but allows us to catch corrupt zero-length symlinks in the verifiers.
19de7351 258 */
725eb1eb 259STATIC int
19de7351 260xfs_inactive_symlink_rmt(
33a9be2b 261 struct xfs_inode *ip)
19de7351 262{
33a9be2b
DW
263 struct xfs_mount *mp = ip->i_mount;
264 struct xfs_trans *tp;
265 int error;
266
b2197a36 267 ASSERT(!xfs_need_iread_extents(&ip->i_df));
19de7351
DC
268 /*
269 * We're freeing a symlink that has some
270 * blocks allocated to it. Free the
271 * blocks here. We know that we've got
272 * either 1 or 2 extents and that we can
273 * free them all in one bunmapi call.
274 */
daf83964 275 ASSERT(ip->i_df.if_nextents > 0 && ip->i_df.if_nextents <= 2);
19de7351 276
253f4911
CH
277 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_itruncate, 0, 0, 0, &tp);
278 if (error)
36b21dde 279 return error;
36b21dde
BF
280
281 xfs_ilock(ip, XFS_ILOCK_EXCL);
282 xfs_trans_ijoin(tp, ip, 0);
283
19de7351 284 /*
43feeea8
DC
285 * Lock the inode, fix the size, turn it into a regular file and join it
286 * to the transaction. Hold it so in the normal path, we still have it
287 * locked for the second transaction. In the error paths we need it
19de7351
DC
288 * held so the cancel won't rele it, see below.
289 */
13d2c10b 290 ip->i_disk_size = 0;
43feeea8 291 VFS_I(ip)->i_mode = (VFS_I(ip)->i_mode & ~S_IFMT) | S_IFREG;
19de7351 292 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
33a9be2b
DW
293
294 error = xfs_symlink_remote_truncate(tp, ip);
36b21dde 295 if (error)
c8eac49e 296 goto error_trans_cancel;
3565b660 297
70393313 298 error = xfs_trans_commit(tp);
19de7351 299 if (error) {
75c8c50f 300 ASSERT(xfs_is_shutdown(mp));
36b21dde 301 goto error_unlock;
19de7351 302 }
19de7351
DC
303
304 /*
305 * Remove the memory for extent descriptions (just bookkeeping).
306 */
307 if (ip->i_df.if_bytes)
308 xfs_idata_realloc(ip, -ip->i_df.if_bytes, XFS_DATA_FORK);
309 ASSERT(ip->i_df.if_bytes == 0);
19de7351 310
36b21dde 311 xfs_iunlock(ip, XFS_ILOCK_EXCL);
19de7351
DC
312 return 0;
313
36b21dde 314error_trans_cancel:
4906e215 315 xfs_trans_cancel(tp);
36b21dde
BF
316error_unlock:
317 xfs_iunlock(ip, XFS_ILOCK_EXCL);
19de7351
DC
318 return error;
319}
725eb1eb
MT
320
321/*
322 * xfs_inactive_symlink - free a symlink
323 */
324int
325xfs_inactive_symlink(
36b21dde 326 struct xfs_inode *ip)
725eb1eb
MT
327{
328 struct xfs_mount *mp = ip->i_mount;
329 int pathlen;
330
331 trace_xfs_inactive_symlink(ip);
332
75c8c50f 333 if (xfs_is_shutdown(mp))
2451337d 334 return -EIO;
725eb1eb 335
36b21dde 336 xfs_ilock(ip, XFS_ILOCK_EXCL);
13d2c10b 337 pathlen = (int)ip->i_disk_size;
43feeea8 338 ASSERT(pathlen);
725eb1eb 339
43feeea8 340 if (pathlen <= 0 || pathlen > XFS_SYMLINK_MAXLEN) {
725eb1eb
MT
341 xfs_alert(mp, "%s: inode (0x%llx) bad symlink length (%d)",
342 __func__, (unsigned long long)ip->i_ino, pathlen);
36b21dde 343 xfs_iunlock(ip, XFS_ILOCK_EXCL);
725eb1eb 344 ASSERT(0);
b280fb0c 345 xfs_inode_mark_sick(ip, XFS_SICK_INO_SYMLINK);
2451337d 346 return -EFSCORRUPTED;
725eb1eb
MT
347 }
348
43feeea8
DC
349 /*
350 * Inline fork state gets removed by xfs_difree() so we have nothing to
351 * do here in that case.
352 */
0779f4a6 353 if (ip->i_df.if_format == XFS_DINODE_FMT_LOCAL) {
36b21dde 354 xfs_iunlock(ip, XFS_ILOCK_EXCL);
725eb1eb
MT
355 return 0;
356 }
357
36b21dde
BF
358 xfs_iunlock(ip, XFS_ILOCK_EXCL);
359
725eb1eb 360 /* remove the remote symlink */
36b21dde 361 return xfs_inactive_symlink_rmt(ip);
725eb1eb 362}