blob: d4b3567d87943f1757568d8763c789be816760c8 [file] [log] [blame]
Dave Chinner0b61f8a2018-06-05 19:42:14 -07001// SPDX-License-Identifier: GPL-2.0
Dave Chinner19de7352013-04-03 16:11:18 +11002/*
3 * Copyright (c) 2000-2006 Silicon Graphics, Inc.
4 * Copyright (c) 2012-2013 Red Hat, Inc.
5 * All rights reserved.
Dave Chinner19de7352013-04-03 16:11:18 +11006 */
7#include "xfs.h"
Dave Chinner239880e2013-10-23 10:50:10 +11008#include "xfs_shared.h"
Dave Chinner19de7352013-04-03 16:11:18 +11009#include "xfs_fs.h"
Dave Chinner6ca1c902013-08-12 20:49:26 +100010#include "xfs_format.h"
Dave Chinner239880e2013-10-23 10:50:10 +110011#include "xfs_log_format.h"
12#include "xfs_trans_resv.h"
Dave Chinner19de7352013-04-03 16:11:18 +110013#include "xfs_bit.h"
Dave Chinner19de7352013-04-03 16:11:18 +110014#include "xfs_mount.h"
Dave Chinner2b9ab5a2013-08-12 20:49:37 +100015#include "xfs_dir2.h"
Dave Chinner19de7352013-04-03 16:11:18 +110016#include "xfs_inode.h"
Dave Chinner19de7352013-04-03 16:11:18 +110017#include "xfs_bmap.h"
Dave Chinnera4fbe6a2013-10-23 10:51:50 +110018#include "xfs_bmap_btree.h"
Dave Chinner19de7352013-04-03 16:11:18 +110019#include "xfs_quota.h"
Darrick J. Wong5f213dd2019-11-06 17:19:33 -080020#include "xfs_symlink.h"
Dave Chinner19de7352013-04-03 16:11:18 +110021#include "xfs_trans_space.h"
Dave Chinner19de7352013-04-03 16:11:18 +110022#include "xfs_trace.h"
Dave Chinner239880e2013-10-23 10:50:10 +110023#include "xfs_trans.h"
Dave Chinner19de7352013-04-03 16:11:18 +110024
25/* ----- Kernel only functions below ----- */
Darrick J. Wong5da8f2f2017-06-16 11:00:15 -070026int
27xfs_readlink_bmap_ilocked(
Dave Chinnerf948dd72013-04-03 16:11:19 +110028 struct xfs_inode *ip,
29 char *link)
Dave Chinner19de7352013-04-03 16:11:18 +110030{
Dave Chinnerf948dd72013-04-03 16:11:19 +110031 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;
Christoph Hellwig13d2c102021-03-29 11:11:40 -070036 int pathlen = ip->i_disk_size;
Dave Chinnerf948dd72013-04-03 16:11:19 +110037 int nmaps = XFS_SYMLINK_MAPS;
38 int byte_cnt;
39 int n;
40 int error = 0;
41 int fsblocks = 0;
42 int offset;
Dave Chinner19de7352013-04-03 16:11:18 +110043
Christoph Hellwig29db2502017-07-13 12:14:34 -070044 ASSERT(xfs_isilocked(ip, XFS_ILOCK_SHARED | XFS_ILOCK_EXCL));
45
Dave Chinnerf948dd72013-04-03 16:11:19 +110046 fsblocks = xfs_symlink_blocks(mp, pathlen);
47 error = xfs_bmapi_read(ip, 0, fsblocks, mval, &nmaps, 0);
Dave Chinner19de7352013-04-03 16:11:18 +110048 if (error)
49 goto out;
50
Dave Chinnerf948dd72013-04-03 16:11:19 +110051 offset = 0;
Dave Chinner19de7352013-04-03 16:11:18 +110052 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
Darrick J. Wong0e3eccc2020-01-23 17:01:17 -080056 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;
Dave Chinnerf948dd72013-04-03 16:11:19 +110060 byte_cnt = XFS_SYMLINK_BUF_SPACE(mp, byte_cnt);
Dave Chinner19de7352013-04-03 16:11:18 +110061 if (pathlen < byte_cnt)
62 byte_cnt = pathlen;
Dave Chinner19de7352013-04-03 16:11:18 +110063
Dave Chinnerf948dd72013-04-03 16:11:19 +110064 cur_chunk = bp->b_addr;
65 if (xfs_sb_version_hascrc(&mp->m_sb)) {
Eric Sandeenbda65ef2014-04-14 19:05:43 +100066 if (!xfs_symlink_hdr_ok(ip->i_ino, offset,
Dave Chinnerf948dd72013-04-03 16:11:19 +110067 byte_cnt, bp)) {
Dave Chinner24513372014-06-25 14:58:08 +100068 error = -EFSCORRUPTED;
Dave Chinnerf948dd72013-04-03 16:11:19 +110069 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
Eric Sandeen2ac56d32015-06-22 09:42:48 +100080 memcpy(link + offset, cur_chunk, byte_cnt);
Dave Chinnerf948dd72013-04-03 16:11:19 +110081
82 pathlen -= byte_cnt;
83 offset += byte_cnt;
84
Dave Chinner19de7352013-04-03 16:11:18 +110085 xfs_buf_relse(bp);
86 }
Dave Chinnerf948dd72013-04-03 16:11:19 +110087 ASSERT(pathlen == 0);
Dave Chinner19de7352013-04-03 16:11:18 +110088
Christoph Hellwig13d2c102021-03-29 11:11:40 -070089 link[ip->i_disk_size] = '\0';
Dave Chinner19de7352013-04-03 16:11:18 +110090 error = 0;
91
92 out:
93 return error;
94}
95
96int
97xfs_readlink(
Dave Chinnerf948dd72013-04-03 16:11:19 +110098 struct xfs_inode *ip,
Dave Chinner19de7352013-04-03 16:11:18 +110099 char *link)
100{
Dave Chinnerf948dd72013-04-03 16:11:19 +1100101 struct xfs_mount *mp = ip->i_mount;
Dave Chinner19de7352013-04-03 16:11:18 +1100102 xfs_fsize_t pathlen;
103 int error = 0;
104
105 trace_xfs_readlink(ip);
106
Christoph Hellwig0779f4a2021-04-13 11:15:11 -0700107 ASSERT(ip->i_df.if_format != XFS_DINODE_FMT_LOCAL);
Christoph Hellwig30ee0522016-04-06 07:53:29 +1000108
Dave Chinner19de7352013-04-03 16:11:18 +1100109 if (XFS_FORCED_SHUTDOWN(mp))
Dave Chinner24513372014-06-25 14:58:08 +1000110 return -EIO;
Dave Chinner19de7352013-04-03 16:11:18 +1100111
112 xfs_ilock(ip, XFS_ILOCK_SHARED);
113
Christoph Hellwig13d2c102021-03-29 11:11:40 -0700114 pathlen = ip->i_disk_size;
Dave Chinner19de7352013-04-03 16:11:18 +1100115 if (!pathlen)
116 goto out;
117
Darrick J. Wong6eb0b8d2017-07-07 08:37:26 -0700118 if (pathlen < 0 || pathlen > XFS_SYMLINK_MAXLEN) {
Dave Chinner19de7352013-04-03 16:11:18 +1100119 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);
Dave Chinner24513372014-06-25 14:58:08 +1000123 error = -EFSCORRUPTED;
Dave Chinner19de7352013-04-03 16:11:18 +1100124 goto out;
125 }
126
127
Darrick J. Wong5da8f2f2017-06-16 11:00:15 -0700128 error = xfs_readlink_bmap_ilocked(ip, link);
Dave Chinner19de7352013-04-03 16:11:18 +1100129
130 out:
131 xfs_iunlock(ip, XFS_ILOCK_SHARED);
132 return error;
133}
134
135int
136xfs_symlink(
Christoph Hellwigf736d932021-01-21 14:19:58 +0100137 struct user_namespace *mnt_userns,
Dave Chinnerf948dd72013-04-03 16:11:19 +1100138 struct xfs_inode *dp,
Dave Chinner19de7352013-04-03 16:11:18 +1100139 struct xfs_name *link_name,
140 const char *target_path,
141 umode_t mode,
Dave Chinnerf948dd72013-04-03 16:11:19 +1100142 struct xfs_inode **ipp)
Dave Chinner19de7352013-04-03 16:11:18 +1100143{
Dave Chinnerf948dd72013-04-03 16:11:19 +1100144 struct xfs_mount *mp = dp->i_mount;
145 struct xfs_trans *tp = NULL;
146 struct xfs_inode *ip = NULL;
147 int error = 0;
Dave Chinner19de7352013-04-03 16:11:18 +1100148 int pathlen;
Dave Chinner58c90472015-02-23 22:38:08 +1100149 bool unlock_dp_on_error = false;
Dave Chinner19de7352013-04-03 16:11:18 +1100150 xfs_fileoff_t first_fsb;
151 xfs_filblks_t fs_blocks;
152 int nmaps;
Dave Chinnerf948dd72013-04-03 16:11:19 +1100153 struct xfs_bmbt_irec mval[XFS_SYMLINK_MAPS];
Dave Chinner19de7352013-04-03 16:11:18 +1100154 xfs_daddr_t d;
155 const char *cur_chunk;
156 int byte_cnt;
157 int n;
Dave Chinnere8222612020-12-16 16:07:34 -0800158 struct xfs_buf *bp;
Dave Chinner19de7352013-04-03 16:11:18 +1100159 prid_t prid;
Chandra Seetharaman113a5682013-06-27 17:25:07 -0500160 struct xfs_dquot *udqp = NULL;
161 struct xfs_dquot *gdqp = NULL;
Chandra Seetharaman92f8ff72013-07-11 00:00:40 -0500162 struct xfs_dquot *pdqp = NULL;
Dave Chinner19de7352013-04-03 16:11:18 +1100163 uint resblks;
164
165 *ipp = NULL;
Dave Chinner19de7352013-04-03 16:11:18 +1100166
167 trace_xfs_symlink(dp, link_name);
168
169 if (XFS_FORCED_SHUTDOWN(mp))
Dave Chinner24513372014-06-25 14:58:08 +1000170 return -EIO;
Dave Chinner19de7352013-04-03 16:11:18 +1100171
172 /*
173 * Check component lengths of the target path name.
174 */
175 pathlen = strlen(target_path);
Darrick J. Wong6eb0b8d2017-07-07 08:37:26 -0700176 if (pathlen >= XFS_SYMLINK_MAXLEN) /* total string too long */
Dave Chinner24513372014-06-25 14:58:08 +1000177 return -ENAMETOOLONG;
Dave Chinner43feeea2018-12-12 08:46:21 -0800178 ASSERT(pathlen > 0);
Dave Chinner19de7352013-04-03 16:11:18 +1100179
Zhi Yong Wu163467d2013-12-18 08:22:39 +0800180 prid = xfs_get_initial_prid(dp);
Dave Chinner19de7352013-04-03 16:11:18 +1100181
182 /*
183 * Make sure that we have allocated dquot(s) on disk.
184 */
Darrick J. Wongb5a08422021-03-02 09:32:52 -0800185 error = xfs_qm_vop_dqalloc(dp, fsuid_into_mnt(mnt_userns),
186 fsgid_into_mnt(mnt_userns), prid,
Dwight Engen7aab1b22013-08-15 14:08:01 -0400187 XFS_QMOPT_QUOTALL | XFS_QMOPT_INHERIT,
188 &udqp, &gdqp, &pdqp);
Dave Chinner19de7352013-04-03 16:11:18 +1100189 if (error)
Dave Chinner58c90472015-02-23 22:38:08 +1100190 return error;
Dave Chinner19de7352013-04-03 16:11:18 +1100191
Dave Chinner19de7352013-04-03 16:11:18 +1100192 /*
193 * The symlink will fit into the inode data fork?
194 * There can't be any attributes so we get the whole variable part.
195 */
Christoph Hellwige9e2eae2020-03-18 08:15:10 -0700196 if (pathlen <= XFS_LITINO(mp))
Dave Chinner19de7352013-04-03 16:11:18 +1100197 fs_blocks = 0;
198 else
Dave Chinner321a9582013-05-27 16:38:20 +1000199 fs_blocks = xfs_symlink_blocks(mp, pathlen);
Dave Chinner19de7352013-04-03 16:11:18 +1100200 resblks = XFS_SYMLINK_SPACE_RES(mp, link_name->len, fs_blocks);
Christoph Hellwig253f4912016-04-06 09:19:55 +1000201
Darrick J. Wongf2f7b9f2021-01-27 12:07:57 -0800202 error = xfs_trans_alloc_icreate(mp, &M_RES(mp)->tr_symlink, udqp, gdqp,
203 pdqp, resblks, &tp);
Christoph Hellwig4906e212015-06-04 13:47:56 +1000204 if (error)
Darrick J. Wongf2f7b9f2021-01-27 12:07:57 -0800205 goto out_release_dquots;
Dave Chinner19de7352013-04-03 16:11:18 +1100206
Christoph Hellwig65523212016-11-30 14:33:25 +1100207 xfs_ilock(dp, XFS_ILOCK_EXCL | XFS_ILOCK_PARENT);
Dave Chinner19de7352013-04-03 16:11:18 +1100208 unlock_dp_on_error = true;
209
210 /*
211 * Check whether the directory allows new symlinks or not.
212 */
Christoph Hellwigdb073492021-03-29 11:11:44 -0700213 if (dp->i_diflags & XFS_DIFLAG_NOSYMLINKS) {
Dave Chinner24513372014-06-25 14:58:08 +1000214 error = -EPERM;
Dave Chinner58c90472015-02-23 22:38:08 +1100215 goto out_trans_cancel;
Dave Chinner19de7352013-04-03 16:11:18 +1100216 }
217
Chandan Babu Rf5d92742021-01-22 16:48:12 -0800218 error = xfs_iext_count_may_overflow(dp, XFS_DATA_FORK,
219 XFS_IEXT_DIR_MANIP_CNT(mp));
220 if (error)
221 goto out_trans_cancel;
222
Dave Chinner19de7352013-04-03 16:11:18 +1100223 /*
Dave Chinner19de7352013-04-03 16:11:18 +1100224 * Allocate an inode for the symlink.
225 */
Christoph Hellwigf736d932021-01-21 14:19:58 +0100226 error = xfs_dir_ialloc(mnt_userns, &tp, dp, S_IFLNK | (mode & ~S_IFMT),
Dave Chinnere6a688c2021-03-22 09:52:03 -0700227 1, 0, prid, false, &ip);
Dave Chinner58c90472015-02-23 22:38:08 +1100228 if (error)
229 goto out_trans_cancel;
Dave Chinner19de7352013-04-03 16:11:18 +1100230
231 /*
Dave Chinner58c90472015-02-23 22:38:08 +1100232 * Now we join the directory inode to the transaction. We do not do it
233 * earlier because xfs_dir_ialloc might commit the previous transaction
234 * (and release all the locks). An error from here on will result in
235 * the transaction cancel unlocking dp so don't do it explicitly in the
Dave Chinner19de7352013-04-03 16:11:18 +1100236 * error path.
237 */
Christoph Hellwig65523212016-11-30 14:33:25 +1100238 xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
Dave Chinner19de7352013-04-03 16:11:18 +1100239 unlock_dp_on_error = false;
240
241 /*
242 * Also attach the dquot(s) to it, if applicable.
243 */
Chandra Seetharaman92f8ff72013-07-11 00:00:40 -0500244 xfs_qm_vop_create_dqattach(tp, ip, udqp, gdqp, pdqp);
Dave Chinner19de7352013-04-03 16:11:18 +1100245
Kaixu Xia57fd2d82020-04-22 21:54:31 -0700246 resblks -= XFS_IALLOC_SPACE_RES(mp);
Dave Chinner19de7352013-04-03 16:11:18 +1100247 /*
248 * If the symlink will fit into the inode, write it inline.
249 */
250 if (pathlen <= XFS_IFORK_DSIZE(ip)) {
Christoph Hellwig143f4ae2016-04-06 07:41:43 +1000251 xfs_init_local_fork(ip, XFS_DATA_FORK, target_path, pathlen);
252
Christoph Hellwig13d2c102021-03-29 11:11:40 -0700253 ip->i_disk_size = pathlen;
Christoph Hellwigf7e67b22020-05-18 10:28:05 -0700254 ip->i_df.if_format = XFS_DINODE_FMT_LOCAL;
Dave Chinner19de7352013-04-03 16:11:18 +1100255 xfs_trans_log_inode(tp, ip, XFS_ILOG_DDATA | XFS_ILOG_CORE);
Dave Chinner19de7352013-04-03 16:11:18 +1100256 } else {
Dave Chinnerf948dd72013-04-03 16:11:19 +1100257 int offset;
258
Dave Chinner19de7352013-04-03 16:11:18 +1100259 first_fsb = 0;
260 nmaps = XFS_SYMLINK_MAPS;
261
262 error = xfs_bmapi_write(tp, ip, first_fsb, fs_blocks,
Brian Fostera7beabe2018-07-11 22:26:25 -0700263 XFS_BMAPI_METADATA, resblks, mval, &nmaps);
Dave Chinner19de7352013-04-03 16:11:18 +1100264 if (error)
Brian Fosterc8eac492018-07-24 13:43:13 -0700265 goto out_trans_cancel;
Dave Chinner19de7352013-04-03 16:11:18 +1100266
Kaixu Xia57fd2d82020-04-22 21:54:31 -0700267 resblks -= fs_blocks;
Christoph Hellwig13d2c102021-03-29 11:11:40 -0700268 ip->i_disk_size = pathlen;
Dave Chinner19de7352013-04-03 16:11:18 +1100269 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
270
271 cur_chunk = target_path;
Dave Chinnerf948dd72013-04-03 16:11:19 +1100272 offset = 0;
Dave Chinner19de7352013-04-03 16:11:18 +1100273 for (n = 0; n < nmaps; n++) {
Dave Chinner321a9582013-05-27 16:38:20 +1000274 char *buf;
Dave Chinnerf948dd72013-04-03 16:11:19 +1100275
Dave Chinner19de7352013-04-03 16:11:18 +1100276 d = XFS_FSB_TO_DADDR(mp, mval[n].br_startblock);
277 byte_cnt = XFS_FSB_TO_B(mp, mval[n].br_blockcount);
Darrick J. Wongce924642020-01-23 17:01:18 -0800278 error = xfs_trans_get_buf(tp, mp->m_ddev_targp, d,
279 BTOBB(byte_cnt), 0, &bp);
280 if (error)
Brian Fosterc8eac492018-07-24 13:43:13 -0700281 goto out_trans_cancel;
Dave Chinnerf948dd72013-04-03 16:11:19 +1100282 bp->b_ops = &xfs_symlink_buf_ops;
283
284 byte_cnt = XFS_SYMLINK_BUF_SPACE(mp, byte_cnt);
Dave Chinner321a9582013-05-27 16:38:20 +1000285 byte_cnt = min(byte_cnt, pathlen);
Dave Chinner19de7352013-04-03 16:11:18 +1100286
Dave Chinnerf948dd72013-04-03 16:11:19 +1100287 buf = bp->b_addr;
288 buf += xfs_symlink_hdr_set(mp, ip->i_ino, offset,
289 byte_cnt, bp);
290
291 memcpy(buf, cur_chunk, byte_cnt);
292
Dave Chinner19de7352013-04-03 16:11:18 +1100293 cur_chunk += byte_cnt;
Dave Chinnerf948dd72013-04-03 16:11:19 +1100294 pathlen -= byte_cnt;
295 offset += byte_cnt;
Dave Chinner19de7352013-04-03 16:11:18 +1100296
Dave Chinnerdaf7b792013-09-02 10:32:00 +1000297 xfs_trans_buf_set_type(tp, bp, XFS_BLFT_SYMLINK_BUF);
Dave Chinnerf948dd72013-04-03 16:11:19 +1100298 xfs_trans_log_buf(tp, bp, 0, (buf + byte_cnt - 1) -
299 (char *)bp->b_addr);
Dave Chinner19de7352013-04-03 16:11:18 +1100300 }
Dave Chinner321a9582013-05-27 16:38:20 +1000301 ASSERT(pathlen == 0);
Dave Chinner19de7352013-04-03 16:11:18 +1100302 }
Christoph Hellwig13d2c102021-03-29 11:11:40 -0700303 i_size_write(VFS_I(ip), ip->i_disk_size);
Dave Chinner19de7352013-04-03 16:11:18 +1100304
305 /*
306 * Create the directory entry for the symlink.
307 */
Brian Foster381eee62018-07-11 22:26:21 -0700308 error = xfs_dir_createname(tp, dp, link_name, ip->i_ino, resblks);
Dave Chinner19de7352013-04-03 16:11:18 +1100309 if (error)
Brian Fosterc8eac492018-07-24 13:43:13 -0700310 goto out_trans_cancel;
Dave Chinner19de7352013-04-03 16:11:18 +1100311 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
Christoph Hellwig70393312015-06-04 13:48:08 +1000323 error = xfs_trans_commit(tp);
Dave Chinner58c90472015-02-23 22:38:08 +1100324 if (error)
325 goto out_release_inode;
326
Dave Chinner19de7352013-04-03 16:11:18 +1100327 xfs_qm_dqrele(udqp);
328 xfs_qm_dqrele(gdqp);
Chandra Seetharaman92f8ff72013-07-11 00:00:40 -0500329 xfs_qm_dqrele(pdqp);
Dave Chinner19de7352013-04-03 16:11:18 +1100330
331 *ipp = ip;
332 return 0;
333
Dave Chinner58c90472015-02-23 22:38:08 +1100334out_trans_cancel:
Christoph Hellwig4906e212015-06-04 13:47:56 +1000335 xfs_trans_cancel(tp);
Dave Chinner58c90472015-02-23 22:38:08 +1100336out_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);
Darrick J. Wong44a87362018-07-25 12:52:32 -0700344 xfs_irele(ip);
Dave Chinner58c90472015-02-23 22:38:08 +1100345 }
Darrick J. Wongf2f7b9f2021-01-27 12:07:57 -0800346out_release_dquots:
Dave Chinner19de7352013-04-03 16:11:18 +1100347 xfs_qm_dqrele(udqp);
348 xfs_qm_dqrele(gdqp);
Chandra Seetharaman92f8ff72013-07-11 00:00:40 -0500349 xfs_qm_dqrele(pdqp);
Dave Chinner19de7352013-04-03 16:11:18 +1100350
351 if (unlock_dp_on_error)
Christoph Hellwig65523212016-11-30 14:33:25 +1100352 xfs_iunlock(dp, XFS_ILOCK_EXCL);
Dave Chinner19de7352013-04-03 16:11:18 +1100353 return error;
354}
355
356/*
357 * Free a symlink that has blocks associated with it.
Dave Chinner43feeea2018-12-12 08:46:21 -0800358 *
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.
Dave Chinner19de7352013-04-03 16:11:18 +1100364 */
Mark Tinguely725eb1e2013-06-17 15:35:57 -0500365STATIC int
Dave Chinner19de7352013-04-03 16:11:18 +1100366xfs_inactive_symlink_rmt(
Brian Foster36b21dd2013-09-20 11:06:09 -0400367 struct xfs_inode *ip)
Dave Chinner19de7352013-04-03 16:11:18 +1100368{
Dave Chinnere8222612020-12-16 16:07:34 -0800369 struct xfs_buf *bp;
Dave Chinner19de7352013-04-03 16:11:18 +1100370 int done;
371 int error;
Dave Chinner19de7352013-04-03 16:11:18 +1100372 int i;
373 xfs_mount_t *mp;
374 xfs_bmbt_irec_t mval[XFS_SYMLINK_MAPS];
375 int nmaps;
Dave Chinner19de7352013-04-03 16:11:18 +1100376 int size;
377 xfs_trans_t *tp;
378
Dave Chinner19de7352013-04-03 16:11:18 +1100379 mp = ip->i_mount;
Mark Tinguely725eb1e2013-06-17 15:35:57 -0500380 ASSERT(ip->i_df.if_flags & XFS_IFEXTENTS);
Dave Chinner19de7352013-04-03 16:11:18 +1100381 /*
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 */
Christoph Hellwigdaf83962020-05-18 10:27:22 -0700388 ASSERT(ip->i_df.if_nextents > 0 && ip->i_df.if_nextents <= 2);
Dave Chinner19de7352013-04-03 16:11:18 +1100389
Christoph Hellwig253f4912016-04-06 09:19:55 +1000390 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_itruncate, 0, 0, 0, &tp);
391 if (error)
Brian Foster36b21dd2013-09-20 11:06:09 -0400392 return error;
Brian Foster36b21dd2013-09-20 11:06:09 -0400393
394 xfs_ilock(ip, XFS_ILOCK_EXCL);
395 xfs_trans_ijoin(tp, ip, 0);
396
Dave Chinner19de7352013-04-03 16:11:18 +1100397 /*
Dave Chinner43feeea2018-12-12 08:46:21 -0800398 * 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
Dave Chinner19de7352013-04-03 16:11:18 +1100401 * held so the cancel won't rele it, see below.
402 */
Christoph Hellwig13d2c102021-03-29 11:11:40 -0700403 size = (int)ip->i_disk_size;
404 ip->i_disk_size = 0;
Dave Chinner43feeea2018-12-12 08:46:21 -0800405 VFS_I(ip)->i_mode = (VFS_I(ip)->i_mode & ~S_IFMT) | S_IFREG;
Dave Chinner19de7352013-04-03 16:11:18 +1100406 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;
Dave Chinner19de7352013-04-03 16:11:18 +1100411 nmaps = ARRAY_SIZE(mval);
Dave Chinnerf948dd72013-04-03 16:11:19 +1100412 error = xfs_bmapi_read(ip, 0, xfs_symlink_blocks(mp, size),
Dave Chinner19de7352013-04-03 16:11:18 +1100413 mval, &nmaps, 0);
414 if (error)
Brian Foster36b21dd2013-09-20 11:06:09 -0400415 goto error_trans_cancel;
Dave Chinner19de7352013-04-03 16:11:18 +1100416 /*
Dave Chinnerf948dd72013-04-03 16:11:19 +1100417 * Invalidate the block(s). No validation is done.
Dave Chinner19de7352013-04-03 16:11:18 +1100418 */
419 for (i = 0; i < nmaps; i++) {
Darrick J. Wongce924642020-01-23 17:01:18 -0800420 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)
Brian Fosterc8eac492018-07-24 13:43:13 -0700425 goto error_trans_cancel;
Dave Chinner19de7352013-04-03 16:11:18 +1100426 xfs_trans_binval(tp, bp);
427 }
428 /*
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000429 * Unmap the dead block(s) to the dfops.
Dave Chinner19de7352013-04-03 16:11:18 +1100430 */
Brian Foster2af52842018-07-11 22:26:25 -0700431 error = xfs_bunmapi(tp, ip, 0, size, 0, nmaps, &done);
Brian Foster36b21dd2013-09-20 11:06:09 -0400432 if (error)
Brian Fosterc8eac492018-07-24 13:43:13 -0700433 goto error_trans_cancel;
Dave Chinner19de7352013-04-03 16:11:18 +1100434 ASSERT(done);
Dave Chinner3565b6602018-05-09 07:49:09 -0700435
Dave Chinner19de7352013-04-03 16:11:18 +1100436 /*
Brian Fosterc8eac492018-07-24 13:43:13 -0700437 * Commit the transaction. This first logs the EFI and the inode, then
438 * rolls and commits the transaction that frees the extents.
Dave Chinner19de7352013-04-03 16:11:18 +1100439 */
Dave Chinner3565b6602018-05-09 07:49:09 -0700440 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
Christoph Hellwig70393312015-06-04 13:48:08 +1000441 error = xfs_trans_commit(tp);
Dave Chinner19de7352013-04-03 16:11:18 +1100442 if (error) {
443 ASSERT(XFS_FORCED_SHUTDOWN(mp));
Brian Foster36b21dd2013-09-20 11:06:09 -0400444 goto error_unlock;
Dave Chinner19de7352013-04-03 16:11:18 +1100445 }
Dave Chinner19de7352013-04-03 16:11:18 +1100446
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);
Dave Chinner19de7352013-04-03 16:11:18 +1100453
Brian Foster36b21dd2013-09-20 11:06:09 -0400454 xfs_iunlock(ip, XFS_ILOCK_EXCL);
Dave Chinner19de7352013-04-03 16:11:18 +1100455 return 0;
456
Brian Foster36b21dd2013-09-20 11:06:09 -0400457error_trans_cancel:
Christoph Hellwig4906e212015-06-04 13:47:56 +1000458 xfs_trans_cancel(tp);
Brian Foster36b21dd2013-09-20 11:06:09 -0400459error_unlock:
460 xfs_iunlock(ip, XFS_ILOCK_EXCL);
Dave Chinner19de7352013-04-03 16:11:18 +1100461 return error;
462}
Mark Tinguely725eb1e2013-06-17 15:35:57 -0500463
464/*
465 * xfs_inactive_symlink - free a symlink
466 */
467int
468xfs_inactive_symlink(
Brian Foster36b21dd2013-09-20 11:06:09 -0400469 struct xfs_inode *ip)
Mark Tinguely725eb1e2013-06-17 15:35:57 -0500470{
471 struct xfs_mount *mp = ip->i_mount;
472 int pathlen;
473
474 trace_xfs_inactive_symlink(ip);
475
Mark Tinguely725eb1e2013-06-17 15:35:57 -0500476 if (XFS_FORCED_SHUTDOWN(mp))
Dave Chinner24513372014-06-25 14:58:08 +1000477 return -EIO;
Mark Tinguely725eb1e2013-06-17 15:35:57 -0500478
Brian Foster36b21dd2013-09-20 11:06:09 -0400479 xfs_ilock(ip, XFS_ILOCK_EXCL);
Christoph Hellwig13d2c102021-03-29 11:11:40 -0700480 pathlen = (int)ip->i_disk_size;
Dave Chinner43feeea2018-12-12 08:46:21 -0800481 ASSERT(pathlen);
Mark Tinguely725eb1e2013-06-17 15:35:57 -0500482
Dave Chinner43feeea2018-12-12 08:46:21 -0800483 if (pathlen <= 0 || pathlen > XFS_SYMLINK_MAXLEN) {
Mark Tinguely725eb1e2013-06-17 15:35:57 -0500484 xfs_alert(mp, "%s: inode (0x%llx) bad symlink length (%d)",
485 __func__, (unsigned long long)ip->i_ino, pathlen);
Brian Foster36b21dd2013-09-20 11:06:09 -0400486 xfs_iunlock(ip, XFS_ILOCK_EXCL);
Mark Tinguely725eb1e2013-06-17 15:35:57 -0500487 ASSERT(0);
Dave Chinner24513372014-06-25 14:58:08 +1000488 return -EFSCORRUPTED;
Mark Tinguely725eb1e2013-06-17 15:35:57 -0500489 }
490
Dave Chinner43feeea2018-12-12 08:46:21 -0800491 /*
492 * Inline fork state gets removed by xfs_difree() so we have nothing to
493 * do here in that case.
494 */
Christoph Hellwig0779f4a2021-04-13 11:15:11 -0700495 if (ip->i_df.if_format == XFS_DINODE_FMT_LOCAL) {
Brian Foster36b21dd2013-09-20 11:06:09 -0400496 xfs_iunlock(ip, XFS_ILOCK_EXCL);
Mark Tinguely725eb1e2013-06-17 15:35:57 -0500497 return 0;
498 }
499
Brian Foster36b21dd2013-09-20 11:06:09 -0400500 xfs_iunlock(ip, XFS_ILOCK_EXCL);
501
Mark Tinguely725eb1e2013-06-17 15:35:57 -0500502 /* remove the remote symlink */
Brian Foster36b21dd2013-09-20 11:06:09 -0400503 return xfs_inactive_symlink_rmt(ip);
Mark Tinguely725eb1e2013-06-17 15:35:57 -0500504}