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