]> git.ipfire.org Git - people/ms/linux.git/blame - fs/xfs/xfs_symlink.c
Merge tag 'soc-fixes-6.0-rc7' of git://git.kernel.org/pub/scm/linux/kernel/git/soc/soc
[people/ms/linux.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"
19de7351
DC
26
27/* ----- Kernel only functions below ----- */
5da8f2f8
DW
28int
29xfs_readlink_bmap_ilocked(
f948dd76
DC
30 struct xfs_inode *ip,
31 char *link)
19de7351 32{
f948dd76
DC
33 struct xfs_mount *mp = ip->i_mount;
34 struct xfs_bmbt_irec mval[XFS_SYMLINK_MAPS];
35 struct xfs_buf *bp;
36 xfs_daddr_t d;
37 char *cur_chunk;
13d2c10b 38 int pathlen = ip->i_disk_size;
f948dd76
DC
39 int nmaps = XFS_SYMLINK_MAPS;
40 int byte_cnt;
41 int n;
42 int error = 0;
43 int fsblocks = 0;
44 int offset;
19de7351 45
29db2500
CH
46 ASSERT(xfs_isilocked(ip, XFS_ILOCK_SHARED | XFS_ILOCK_EXCL));
47
f948dd76
DC
48 fsblocks = xfs_symlink_blocks(mp, pathlen);
49 error = xfs_bmapi_read(ip, 0, fsblocks, mval, &nmaps, 0);
19de7351
DC
50 if (error)
51 goto out;
52
f948dd76 53 offset = 0;
19de7351
DC
54 for (n = 0; n < nmaps; n++) {
55 d = XFS_FSB_TO_DADDR(mp, mval[n].br_startblock);
56 byte_cnt = XFS_FSB_TO_B(mp, mval[n].br_blockcount);
57
0e3eccce
DW
58 error = xfs_buf_read(mp->m_ddev_targp, d, BTOBB(byte_cnt), 0,
59 &bp, &xfs_symlink_buf_ops);
60 if (error)
61 return error;
f948dd76 62 byte_cnt = XFS_SYMLINK_BUF_SPACE(mp, byte_cnt);
19de7351
DC
63 if (pathlen < byte_cnt)
64 byte_cnt = pathlen;
f948dd76
DC
65
66 cur_chunk = bp->b_addr;
38c26bfd 67 if (xfs_has_crc(mp)) {
bda65ef8 68 if (!xfs_symlink_hdr_ok(ip->i_ino, offset,
f948dd76 69 byte_cnt, bp)) {
2451337d 70 error = -EFSCORRUPTED;
f948dd76
DC
71 xfs_alert(mp,
72"symlink header does not match required off/len/owner (0x%x/Ox%x,0x%llx)",
73 offset, byte_cnt, ip->i_ino);
74 xfs_buf_relse(bp);
75 goto out;
76
77 }
78
79 cur_chunk += sizeof(struct xfs_dsymlink_hdr);
80 }
81
2ac56d3d 82 memcpy(link + offset, cur_chunk, byte_cnt);
f948dd76 83
19de7351 84 pathlen -= byte_cnt;
f948dd76 85 offset += byte_cnt;
19de7351 86
19de7351
DC
87 xfs_buf_relse(bp);
88 }
f948dd76 89 ASSERT(pathlen == 0);
19de7351 90
13d2c10b 91 link[ip->i_disk_size] = '\0';
19de7351
DC
92 error = 0;
93
94 out:
95 return error;
96}
97
98int
99xfs_readlink(
7b7820b8
DW
100 struct xfs_inode *ip,
101 char *link)
19de7351 102{
7b7820b8
DW
103 struct xfs_mount *mp = ip->i_mount;
104 xfs_fsize_t pathlen;
105 int error = -EFSCORRUPTED;
19de7351
DC
106
107 trace_xfs_readlink(ip);
108
75c8c50f 109 if (xfs_is_shutdown(mp))
2451337d 110 return -EIO;
19de7351
DC
111
112 xfs_ilock(ip, XFS_ILOCK_SHARED);
113
13d2c10b 114 pathlen = ip->i_disk_size;
19de7351
DC
115 if (!pathlen)
116 goto out;
117
6eb0b8df 118 if (pathlen < 0 || pathlen > XFS_SYMLINK_MAXLEN) {
19de7351
DC
119 xfs_alert(mp, "%s: inode (%llu) bad symlink length (%lld)",
120 __func__, (unsigned long long) ip->i_ino,
121 (long long) pathlen);
122 ASSERT(0);
19de7351
DC
123 goto out;
124 }
125
7b7820b8
DW
126 if (ip->i_df.if_format == XFS_DINODE_FMT_LOCAL) {
127 /*
128 * The VFS crashes on a NULL pointer, so return -EFSCORRUPTED
129 * if if_data is junk.
130 */
131 if (XFS_IS_CORRUPT(ip->i_mount, !ip->i_df.if_u1.if_data))
132 goto out;
133
134 memcpy(link, ip->i_df.if_u1.if_data, pathlen + 1);
135 error = 0;
136 } else {
137 error = xfs_readlink_bmap_ilocked(ip, link);
138 }
19de7351
DC
139
140 out:
141 xfs_iunlock(ip, XFS_ILOCK_SHARED);
142 return error;
143}
144
145int
146xfs_symlink(
f736d93d 147 struct user_namespace *mnt_userns,
f948dd76 148 struct xfs_inode *dp,
19de7351
DC
149 struct xfs_name *link_name,
150 const char *target_path,
151 umode_t mode,
f948dd76 152 struct xfs_inode **ipp)
19de7351 153{
f948dd76
DC
154 struct xfs_mount *mp = dp->i_mount;
155 struct xfs_trans *tp = NULL;
156 struct xfs_inode *ip = NULL;
157 int error = 0;
19de7351 158 int pathlen;
58c90473 159 bool unlock_dp_on_error = false;
19de7351
DC
160 xfs_fileoff_t first_fsb;
161 xfs_filblks_t fs_blocks;
162 int nmaps;
f948dd76 163 struct xfs_bmbt_irec mval[XFS_SYMLINK_MAPS];
19de7351
DC
164 xfs_daddr_t d;
165 const char *cur_chunk;
166 int byte_cnt;
167 int n;
e8222613 168 struct xfs_buf *bp;
19de7351 169 prid_t prid;
113a5683
CS
170 struct xfs_dquot *udqp = NULL;
171 struct xfs_dquot *gdqp = NULL;
92f8ff73 172 struct xfs_dquot *pdqp = NULL;
19de7351 173 uint resblks;
b652afd9 174 xfs_ino_t ino;
19de7351
DC
175
176 *ipp = NULL;
19de7351
DC
177
178 trace_xfs_symlink(dp, link_name);
179
75c8c50f 180 if (xfs_is_shutdown(mp))
2451337d 181 return -EIO;
19de7351
DC
182
183 /*
184 * Check component lengths of the target path name.
185 */
186 pathlen = strlen(target_path);
6eb0b8df 187 if (pathlen >= XFS_SYMLINK_MAXLEN) /* total string too long */
2451337d 188 return -ENAMETOOLONG;
43feeea8 189 ASSERT(pathlen > 0);
19de7351 190
163467d3 191 prid = xfs_get_initial_prid(dp);
19de7351
DC
192
193 /*
194 * Make sure that we have allocated dquot(s) on disk.
195 */
209188ce
CB
196 error = xfs_qm_vop_dqalloc(dp, mapped_fsuid(mnt_userns, &init_user_ns),
197 mapped_fsgid(mnt_userns, &init_user_ns), prid,
7aab1b28
DE
198 XFS_QMOPT_QUOTALL | XFS_QMOPT_INHERIT,
199 &udqp, &gdqp, &pdqp);
19de7351 200 if (error)
58c90473 201 return error;
19de7351 202
19de7351
DC
203 /*
204 * The symlink will fit into the inode data fork?
205 * There can't be any attributes so we get the whole variable part.
206 */
e9e2eae8 207 if (pathlen <= XFS_LITINO(mp))
19de7351
DC
208 fs_blocks = 0;
209 else
321a9583 210 fs_blocks = xfs_symlink_blocks(mp, pathlen);
19de7351 211 resblks = XFS_SYMLINK_SPACE_RES(mp, link_name->len, fs_blocks);
253f4911 212
f2f7b9ff
DW
213 error = xfs_trans_alloc_icreate(mp, &M_RES(mp)->tr_symlink, udqp, gdqp,
214 pdqp, resblks, &tp);
4906e215 215 if (error)
f2f7b9ff 216 goto out_release_dquots;
19de7351 217
65523218 218 xfs_ilock(dp, XFS_ILOCK_EXCL | XFS_ILOCK_PARENT);
19de7351
DC
219 unlock_dp_on_error = true;
220
221 /*
222 * Check whether the directory allows new symlinks or not.
223 */
db07349d 224 if (dp->i_diflags & XFS_DIFLAG_NOSYMLINKS) {
2451337d 225 error = -EPERM;
58c90473 226 goto out_trans_cancel;
19de7351
DC
227 }
228
19de7351
DC
229 /*
230 * Allocate an inode for the symlink.
231 */
b652afd9
DC
232 error = xfs_dialloc(&tp, dp->i_ino, S_IFLNK, &ino);
233 if (!error)
234 error = xfs_init_new_inode(mnt_userns, tp, dp, ino,
235 S_IFLNK | (mode & ~S_IFMT), 1, 0, prid,
236 false, &ip);
58c90473
DC
237 if (error)
238 goto out_trans_cancel;
19de7351
DC
239
240 /*
58c90473
DC
241 * Now we join the directory inode to the transaction. We do not do it
242 * earlier because xfs_dir_ialloc might commit the previous transaction
243 * (and release all the locks). An error from here on will result in
244 * the transaction cancel unlocking dp so don't do it explicitly in the
19de7351
DC
245 * error path.
246 */
65523218 247 xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
19de7351
DC
248 unlock_dp_on_error = false;
249
250 /*
251 * Also attach the dquot(s) to it, if applicable.
252 */
92f8ff73 253 xfs_qm_vop_create_dqattach(tp, ip, udqp, gdqp, pdqp);
19de7351 254
57fd2d8f 255 resblks -= XFS_IALLOC_SPACE_RES(mp);
19de7351
DC
256 /*
257 * If the symlink will fit into the inode, write it inline.
258 */
c01147d9 259 if (pathlen <= xfs_inode_data_fork_size(ip)) {
143f4aed 260 xfs_init_local_fork(ip, XFS_DATA_FORK, target_path, pathlen);
19de7351 261
13d2c10b 262 ip->i_disk_size = pathlen;
f7e67b20 263 ip->i_df.if_format = XFS_DINODE_FMT_LOCAL;
19de7351 264 xfs_trans_log_inode(tp, ip, XFS_ILOG_DDATA | XFS_ILOG_CORE);
19de7351 265 } else {
f948dd76
DC
266 int offset;
267
19de7351
DC
268 first_fsb = 0;
269 nmaps = XFS_SYMLINK_MAPS;
270
271 error = xfs_bmapi_write(tp, ip, first_fsb, fs_blocks,
a7beabea 272 XFS_BMAPI_METADATA, resblks, mval, &nmaps);
19de7351 273 if (error)
c8eac49e 274 goto out_trans_cancel;
19de7351 275
57fd2d8f 276 resblks -= fs_blocks;
13d2c10b 277 ip->i_disk_size = pathlen;
19de7351
DC
278 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
279
280 cur_chunk = target_path;
f948dd76 281 offset = 0;
19de7351 282 for (n = 0; n < nmaps; n++) {
321a9583 283 char *buf;
f948dd76 284
19de7351
DC
285 d = XFS_FSB_TO_DADDR(mp, mval[n].br_startblock);
286 byte_cnt = XFS_FSB_TO_B(mp, mval[n].br_blockcount);
ce92464c
DW
287 error = xfs_trans_get_buf(tp, mp->m_ddev_targp, d,
288 BTOBB(byte_cnt), 0, &bp);
289 if (error)
c8eac49e 290 goto out_trans_cancel;
f948dd76
DC
291 bp->b_ops = &xfs_symlink_buf_ops;
292
293 byte_cnt = XFS_SYMLINK_BUF_SPACE(mp, byte_cnt);
321a9583 294 byte_cnt = min(byte_cnt, pathlen);
19de7351 295
f948dd76
DC
296 buf = bp->b_addr;
297 buf += xfs_symlink_hdr_set(mp, ip->i_ino, offset,
298 byte_cnt, bp);
299
300 memcpy(buf, cur_chunk, byte_cnt);
301
19de7351 302 cur_chunk += byte_cnt;
f948dd76
DC
303 pathlen -= byte_cnt;
304 offset += byte_cnt;
19de7351 305
daf7b799 306 xfs_trans_buf_set_type(tp, bp, XFS_BLFT_SYMLINK_BUF);
f948dd76
DC
307 xfs_trans_log_buf(tp, bp, 0, (buf + byte_cnt - 1) -
308 (char *)bp->b_addr);
19de7351 309 }
321a9583 310 ASSERT(pathlen == 0);
19de7351 311 }
13d2c10b 312 i_size_write(VFS_I(ip), ip->i_disk_size);
19de7351
DC
313
314 /*
315 * Create the directory entry for the symlink.
316 */
381eee69 317 error = xfs_dir_createname(tp, dp, link_name, ip->i_ino, resblks);
19de7351 318 if (error)
c8eac49e 319 goto out_trans_cancel;
19de7351
DC
320 xfs_trans_ichgtime(tp, dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
321 xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
322
323 /*
324 * If this is a synchronous mount, make sure that the
325 * symlink transaction goes to disk before returning to
326 * the user.
327 */
0560f31a 328 if (xfs_has_wsync(mp) || xfs_has_dirsync(mp))
19de7351 329 xfs_trans_set_sync(tp);
19de7351 330
70393313 331 error = xfs_trans_commit(tp);
58c90473
DC
332 if (error)
333 goto out_release_inode;
334
19de7351
DC
335 xfs_qm_dqrele(udqp);
336 xfs_qm_dqrele(gdqp);
92f8ff73 337 xfs_qm_dqrele(pdqp);
19de7351
DC
338
339 *ipp = ip;
340 return 0;
341
58c90473 342out_trans_cancel:
4906e215 343 xfs_trans_cancel(tp);
58c90473
DC
344out_release_inode:
345 /*
346 * Wait until after the current transaction is aborted to finish the
347 * setup of the inode and release the inode. This prevents recursive
348 * transactions and deadlocks from xfs_inactive.
349 */
350 if (ip) {
351 xfs_finish_inode_setup(ip);
44a8736b 352 xfs_irele(ip);
58c90473 353 }
f2f7b9ff 354out_release_dquots:
19de7351
DC
355 xfs_qm_dqrele(udqp);
356 xfs_qm_dqrele(gdqp);
92f8ff73 357 xfs_qm_dqrele(pdqp);
19de7351
DC
358
359 if (unlock_dp_on_error)
65523218 360 xfs_iunlock(dp, XFS_ILOCK_EXCL);
19de7351
DC
361 return error;
362}
363
364/*
365 * Free a symlink that has blocks associated with it.
43feeea8
DC
366 *
367 * Note: zero length symlinks are not allowed to exist. When we set the size to
368 * zero, also change it to a regular file so that it does not get written to
369 * disk as a zero length symlink. The inode is on the unlinked list already, so
370 * userspace cannot find this inode anymore, so this change is not user visible
371 * but allows us to catch corrupt zero-length symlinks in the verifiers.
19de7351 372 */
725eb1eb 373STATIC int
19de7351 374xfs_inactive_symlink_rmt(
36b21dde 375 struct xfs_inode *ip)
19de7351 376{
e8222613 377 struct xfs_buf *bp;
19de7351
DC
378 int done;
379 int error;
19de7351
DC
380 int i;
381 xfs_mount_t *mp;
382 xfs_bmbt_irec_t mval[XFS_SYMLINK_MAPS];
383 int nmaps;
19de7351
DC
384 int size;
385 xfs_trans_t *tp;
386
19de7351 387 mp = ip->i_mount;
b2197a36 388 ASSERT(!xfs_need_iread_extents(&ip->i_df));
19de7351
DC
389 /*
390 * We're freeing a symlink that has some
391 * blocks allocated to it. Free the
392 * blocks here. We know that we've got
393 * either 1 or 2 extents and that we can
394 * free them all in one bunmapi call.
395 */
daf83964 396 ASSERT(ip->i_df.if_nextents > 0 && ip->i_df.if_nextents <= 2);
19de7351 397
253f4911
CH
398 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_itruncate, 0, 0, 0, &tp);
399 if (error)
36b21dde 400 return error;
36b21dde
BF
401
402 xfs_ilock(ip, XFS_ILOCK_EXCL);
403 xfs_trans_ijoin(tp, ip, 0);
404
19de7351 405 /*
43feeea8
DC
406 * Lock the inode, fix the size, turn it into a regular file and join it
407 * to the transaction. Hold it so in the normal path, we still have it
408 * locked for the second transaction. In the error paths we need it
19de7351
DC
409 * held so the cancel won't rele it, see below.
410 */
13d2c10b
CH
411 size = (int)ip->i_disk_size;
412 ip->i_disk_size = 0;
43feeea8 413 VFS_I(ip)->i_mode = (VFS_I(ip)->i_mode & ~S_IFMT) | S_IFREG;
19de7351
DC
414 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
415 /*
416 * Find the block(s) so we can inval and unmap them.
417 */
418 done = 0;
19de7351 419 nmaps = ARRAY_SIZE(mval);
f948dd76 420 error = xfs_bmapi_read(ip, 0, xfs_symlink_blocks(mp, size),
19de7351
DC
421 mval, &nmaps, 0);
422 if (error)
36b21dde 423 goto error_trans_cancel;
19de7351 424 /*
f948dd76 425 * Invalidate the block(s). No validation is done.
19de7351
DC
426 */
427 for (i = 0; i < nmaps; i++) {
ce92464c
DW
428 error = xfs_trans_get_buf(tp, mp->m_ddev_targp,
429 XFS_FSB_TO_DADDR(mp, mval[i].br_startblock),
430 XFS_FSB_TO_BB(mp, mval[i].br_blockcount), 0,
431 &bp);
432 if (error)
c8eac49e 433 goto error_trans_cancel;
19de7351
DC
434 xfs_trans_binval(tp, bp);
435 }
436 /*
2c3234d1 437 * Unmap the dead block(s) to the dfops.
19de7351 438 */
2af52842 439 error = xfs_bunmapi(tp, ip, 0, size, 0, nmaps, &done);
36b21dde 440 if (error)
c8eac49e 441 goto error_trans_cancel;
19de7351 442 ASSERT(done);
3565b660 443
19de7351 444 /*
c8eac49e
BF
445 * Commit the transaction. This first logs the EFI and the inode, then
446 * rolls and commits the transaction that frees the extents.
19de7351 447 */
3565b660 448 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
70393313 449 error = xfs_trans_commit(tp);
19de7351 450 if (error) {
75c8c50f 451 ASSERT(xfs_is_shutdown(mp));
36b21dde 452 goto error_unlock;
19de7351 453 }
19de7351
DC
454
455 /*
456 * Remove the memory for extent descriptions (just bookkeeping).
457 */
458 if (ip->i_df.if_bytes)
459 xfs_idata_realloc(ip, -ip->i_df.if_bytes, XFS_DATA_FORK);
460 ASSERT(ip->i_df.if_bytes == 0);
19de7351 461
36b21dde 462 xfs_iunlock(ip, XFS_ILOCK_EXCL);
19de7351
DC
463 return 0;
464
36b21dde 465error_trans_cancel:
4906e215 466 xfs_trans_cancel(tp);
36b21dde
BF
467error_unlock:
468 xfs_iunlock(ip, XFS_ILOCK_EXCL);
19de7351
DC
469 return error;
470}
725eb1eb
MT
471
472/*
473 * xfs_inactive_symlink - free a symlink
474 */
475int
476xfs_inactive_symlink(
36b21dde 477 struct xfs_inode *ip)
725eb1eb
MT
478{
479 struct xfs_mount *mp = ip->i_mount;
480 int pathlen;
481
482 trace_xfs_inactive_symlink(ip);
483
75c8c50f 484 if (xfs_is_shutdown(mp))
2451337d 485 return -EIO;
725eb1eb 486
36b21dde 487 xfs_ilock(ip, XFS_ILOCK_EXCL);
13d2c10b 488 pathlen = (int)ip->i_disk_size;
43feeea8 489 ASSERT(pathlen);
725eb1eb 490
43feeea8 491 if (pathlen <= 0 || pathlen > XFS_SYMLINK_MAXLEN) {
725eb1eb
MT
492 xfs_alert(mp, "%s: inode (0x%llx) bad symlink length (%d)",
493 __func__, (unsigned long long)ip->i_ino, pathlen);
36b21dde 494 xfs_iunlock(ip, XFS_ILOCK_EXCL);
725eb1eb 495 ASSERT(0);
2451337d 496 return -EFSCORRUPTED;
725eb1eb
MT
497 }
498
43feeea8
DC
499 /*
500 * Inline fork state gets removed by xfs_difree() so we have nothing to
501 * do here in that case.
502 */
0779f4a6 503 if (ip->i_df.if_format == XFS_DINODE_FMT_LOCAL) {
36b21dde 504 xfs_iunlock(ip, XFS_ILOCK_EXCL);
725eb1eb
MT
505 return 0;
506 }
507
36b21dde
BF
508 xfs_iunlock(ip, XFS_ILOCK_EXCL);
509
725eb1eb 510 /* remove the remote symlink */
36b21dde 511 return xfs_inactive_symlink_rmt(ip);
725eb1eb 512}