blob: fc2c6a404647122c755361e65a41cf738e585d5a [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 Chinnerb652afd2021-06-02 10:48:24 +100024#include "xfs_ialloc.h"
Dave Chinner19de7352013-04-03 16:11:18 +110025
26/* ----- Kernel only functions below ----- */
Darrick J. Wong5da8f2f2017-06-16 11:00:15 -070027int
28xfs_readlink_bmap_ilocked(
Dave Chinnerf948dd72013-04-03 16:11:19 +110029 struct xfs_inode *ip,
30 char *link)
Dave Chinner19de7352013-04-03 16:11:18 +110031{
Dave Chinnerf948dd72013-04-03 16:11:19 +110032 struct xfs_mount *mp = ip->i_mount;
33 struct xfs_bmbt_irec mval[XFS_SYMLINK_MAPS];
34 struct xfs_buf *bp;
35 xfs_daddr_t d;
36 char *cur_chunk;
Christoph Hellwig13d2c102021-03-29 11:11:40 -070037 int pathlen = ip->i_disk_size;
Dave Chinnerf948dd72013-04-03 16:11:19 +110038 int nmaps = XFS_SYMLINK_MAPS;
39 int byte_cnt;
40 int n;
41 int error = 0;
42 int fsblocks = 0;
43 int offset;
Dave Chinner19de7352013-04-03 16:11:18 +110044
Christoph Hellwig29db2502017-07-13 12:14:34 -070045 ASSERT(xfs_isilocked(ip, XFS_ILOCK_SHARED | XFS_ILOCK_EXCL));
46
Dave Chinnerf948dd72013-04-03 16:11:19 +110047 fsblocks = xfs_symlink_blocks(mp, pathlen);
48 error = xfs_bmapi_read(ip, 0, fsblocks, mval, &nmaps, 0);
Dave Chinner19de7352013-04-03 16:11:18 +110049 if (error)
50 goto out;
51
Dave Chinnerf948dd72013-04-03 16:11:19 +110052 offset = 0;
Dave Chinner19de7352013-04-03 16:11:18 +110053 for (n = 0; n < nmaps; n++) {
54 d = XFS_FSB_TO_DADDR(mp, mval[n].br_startblock);
55 byte_cnt = XFS_FSB_TO_B(mp, mval[n].br_blockcount);
56
Darrick J. Wong0e3eccc2020-01-23 17:01:17 -080057 error = xfs_buf_read(mp->m_ddev_targp, d, BTOBB(byte_cnt), 0,
58 &bp, &xfs_symlink_buf_ops);
59 if (error)
60 return error;
Dave Chinnerf948dd72013-04-03 16:11:19 +110061 byte_cnt = XFS_SYMLINK_BUF_SPACE(mp, byte_cnt);
Dave Chinner19de7352013-04-03 16:11:18 +110062 if (pathlen < byte_cnt)
63 byte_cnt = pathlen;
Dave Chinner19de7352013-04-03 16:11:18 +110064
Dave Chinnerf948dd72013-04-03 16:11:19 +110065 cur_chunk = bp->b_addr;
Dave Chinner38c26bf2021-08-18 18:46:37 -070066 if (xfs_has_crc(mp)) {
Eric Sandeenbda65ef2014-04-14 19:05:43 +100067 if (!xfs_symlink_hdr_ok(ip->i_ino, offset,
Dave Chinnerf948dd72013-04-03 16:11:19 +110068 byte_cnt, bp)) {
Dave Chinner24513372014-06-25 14:58:08 +100069 error = -EFSCORRUPTED;
Dave Chinnerf948dd72013-04-03 16:11:19 +110070 xfs_alert(mp,
71"symlink header does not match required off/len/owner (0x%x/Ox%x,0x%llx)",
72 offset, byte_cnt, ip->i_ino);
73 xfs_buf_relse(bp);
74 goto out;
75
76 }
77
78 cur_chunk += sizeof(struct xfs_dsymlink_hdr);
79 }
80
Eric Sandeen2ac56d32015-06-22 09:42:48 +100081 memcpy(link + offset, cur_chunk, byte_cnt);
Dave Chinnerf948dd72013-04-03 16:11:19 +110082
83 pathlen -= byte_cnt;
84 offset += byte_cnt;
85
Dave Chinner19de7352013-04-03 16:11:18 +110086 xfs_buf_relse(bp);
87 }
Dave Chinnerf948dd72013-04-03 16:11:19 +110088 ASSERT(pathlen == 0);
Dave Chinner19de7352013-04-03 16:11:18 +110089
Christoph Hellwig13d2c102021-03-29 11:11:40 -070090 link[ip->i_disk_size] = '\0';
Dave Chinner19de7352013-04-03 16:11:18 +110091 error = 0;
92
93 out:
94 return error;
95}
96
97int
98xfs_readlink(
Dave Chinnerf948dd72013-04-03 16:11:19 +110099 struct xfs_inode *ip,
Dave Chinner19de7352013-04-03 16:11:18 +1100100 char *link)
101{
Dave Chinnerf948dd72013-04-03 16:11:19 +1100102 struct xfs_mount *mp = ip->i_mount;
Dave Chinner19de7352013-04-03 16:11:18 +1100103 xfs_fsize_t pathlen;
104 int error = 0;
105
106 trace_xfs_readlink(ip);
107
Christoph Hellwig0779f4a2021-04-13 11:15:11 -0700108 ASSERT(ip->i_df.if_format != XFS_DINODE_FMT_LOCAL);
Christoph Hellwig30ee0522016-04-06 07:53:29 +1000109
Dave Chinner75c8c50f2021-08-18 18:46:53 -0700110 if (xfs_is_shutdown(mp))
Dave Chinner24513372014-06-25 14:58:08 +1000111 return -EIO;
Dave Chinner19de7352013-04-03 16:11:18 +1100112
113 xfs_ilock(ip, XFS_ILOCK_SHARED);
114
Christoph Hellwig13d2c102021-03-29 11:11:40 -0700115 pathlen = ip->i_disk_size;
Dave Chinner19de7352013-04-03 16:11:18 +1100116 if (!pathlen)
117 goto out;
118
Darrick J. Wong6eb0b8d2017-07-07 08:37:26 -0700119 if (pathlen < 0 || pathlen > XFS_SYMLINK_MAXLEN) {
Dave Chinner19de7352013-04-03 16:11:18 +1100120 xfs_alert(mp, "%s: inode (%llu) bad symlink length (%lld)",
121 __func__, (unsigned long long) ip->i_ino,
122 (long long) pathlen);
123 ASSERT(0);
Dave Chinner24513372014-06-25 14:58:08 +1000124 error = -EFSCORRUPTED;
Dave Chinner19de7352013-04-03 16:11:18 +1100125 goto out;
126 }
127
128
Darrick J. Wong5da8f2f2017-06-16 11:00:15 -0700129 error = xfs_readlink_bmap_ilocked(ip, link);
Dave Chinner19de7352013-04-03 16:11:18 +1100130
131 out:
132 xfs_iunlock(ip, XFS_ILOCK_SHARED);
133 return error;
134}
135
136int
137xfs_symlink(
Christoph Hellwigf736d932021-01-21 14:19:58 +0100138 struct user_namespace *mnt_userns,
Dave Chinnerf948dd72013-04-03 16:11:19 +1100139 struct xfs_inode *dp,
Dave Chinner19de7352013-04-03 16:11:18 +1100140 struct xfs_name *link_name,
141 const char *target_path,
142 umode_t mode,
Dave Chinnerf948dd72013-04-03 16:11:19 +1100143 struct xfs_inode **ipp)
Dave Chinner19de7352013-04-03 16:11:18 +1100144{
Dave Chinnerf948dd72013-04-03 16:11:19 +1100145 struct xfs_mount *mp = dp->i_mount;
146 struct xfs_trans *tp = NULL;
147 struct xfs_inode *ip = NULL;
148 int error = 0;
Dave Chinner19de7352013-04-03 16:11:18 +1100149 int pathlen;
Dave Chinner58c90472015-02-23 22:38:08 +1100150 bool unlock_dp_on_error = false;
Dave Chinner19de7352013-04-03 16:11:18 +1100151 xfs_fileoff_t first_fsb;
152 xfs_filblks_t fs_blocks;
153 int nmaps;
Dave Chinnerf948dd72013-04-03 16:11:19 +1100154 struct xfs_bmbt_irec mval[XFS_SYMLINK_MAPS];
Dave Chinner19de7352013-04-03 16:11:18 +1100155 xfs_daddr_t d;
156 const char *cur_chunk;
157 int byte_cnt;
158 int n;
Dave Chinnere8222612020-12-16 16:07:34 -0800159 struct xfs_buf *bp;
Dave Chinner19de7352013-04-03 16:11:18 +1100160 prid_t prid;
Chandra Seetharaman113a5682013-06-27 17:25:07 -0500161 struct xfs_dquot *udqp = NULL;
162 struct xfs_dquot *gdqp = NULL;
Chandra Seetharaman92f8ff72013-07-11 00:00:40 -0500163 struct xfs_dquot *pdqp = NULL;
Dave Chinner19de7352013-04-03 16:11:18 +1100164 uint resblks;
Dave Chinnerb652afd2021-06-02 10:48:24 +1000165 xfs_ino_t ino;
Dave Chinner19de7352013-04-03 16:11:18 +1100166
167 *ipp = NULL;
Dave Chinner19de7352013-04-03 16:11:18 +1100168
169 trace_xfs_symlink(dp, link_name);
170
Dave Chinner75c8c50f2021-08-18 18:46:53 -0700171 if (xfs_is_shutdown(mp))
Dave Chinner24513372014-06-25 14:58:08 +1000172 return -EIO;
Dave Chinner19de7352013-04-03 16:11:18 +1100173
174 /*
175 * Check component lengths of the target path name.
176 */
177 pathlen = strlen(target_path);
Darrick J. Wong6eb0b8d2017-07-07 08:37:26 -0700178 if (pathlen >= XFS_SYMLINK_MAXLEN) /* total string too long */
Dave Chinner24513372014-06-25 14:58:08 +1000179 return -ENAMETOOLONG;
Dave Chinner43feeea2018-12-12 08:46:21 -0800180 ASSERT(pathlen > 0);
Dave Chinner19de7352013-04-03 16:11:18 +1100181
Zhi Yong Wu163467d2013-12-18 08:22:39 +0800182 prid = xfs_get_initial_prid(dp);
Dave Chinner19de7352013-04-03 16:11:18 +1100183
184 /*
185 * Make sure that we have allocated dquot(s) on disk.
186 */
Christian Braunera65e58e2021-03-20 13:26:22 +0100187 error = xfs_qm_vop_dqalloc(dp, mapped_fsuid(mnt_userns),
188 mapped_fsgid(mnt_userns), prid,
Dwight Engen7aab1b22013-08-15 14:08:01 -0400189 XFS_QMOPT_QUOTALL | XFS_QMOPT_INHERIT,
190 &udqp, &gdqp, &pdqp);
Dave Chinner19de7352013-04-03 16:11:18 +1100191 if (error)
Dave Chinner58c90472015-02-23 22:38:08 +1100192 return error;
Dave Chinner19de7352013-04-03 16:11:18 +1100193
Dave Chinner19de7352013-04-03 16:11:18 +1100194 /*
195 * The symlink will fit into the inode data fork?
196 * There can't be any attributes so we get the whole variable part.
197 */
Christoph Hellwige9e2eae2020-03-18 08:15:10 -0700198 if (pathlen <= XFS_LITINO(mp))
Dave Chinner19de7352013-04-03 16:11:18 +1100199 fs_blocks = 0;
200 else
Dave Chinner321a9582013-05-27 16:38:20 +1000201 fs_blocks = xfs_symlink_blocks(mp, pathlen);
Dave Chinner19de7352013-04-03 16:11:18 +1100202 resblks = XFS_SYMLINK_SPACE_RES(mp, link_name->len, fs_blocks);
Christoph Hellwig253f4912016-04-06 09:19:55 +1000203
Darrick J. Wongf2f7b9f2021-01-27 12:07:57 -0800204 error = xfs_trans_alloc_icreate(mp, &M_RES(mp)->tr_symlink, udqp, gdqp,
205 pdqp, resblks, &tp);
Christoph Hellwig4906e212015-06-04 13:47:56 +1000206 if (error)
Darrick J. Wongf2f7b9f2021-01-27 12:07:57 -0800207 goto out_release_dquots;
Dave Chinner19de7352013-04-03 16:11:18 +1100208
Christoph Hellwig65523212016-11-30 14:33:25 +1100209 xfs_ilock(dp, XFS_ILOCK_EXCL | XFS_ILOCK_PARENT);
Dave Chinner19de7352013-04-03 16:11:18 +1100210 unlock_dp_on_error = true;
211
212 /*
213 * Check whether the directory allows new symlinks or not.
214 */
Christoph Hellwigdb073492021-03-29 11:11:44 -0700215 if (dp->i_diflags & XFS_DIFLAG_NOSYMLINKS) {
Dave Chinner24513372014-06-25 14:58:08 +1000216 error = -EPERM;
Dave Chinner58c90472015-02-23 22:38:08 +1100217 goto out_trans_cancel;
Dave Chinner19de7352013-04-03 16:11:18 +1100218 }
219
Chandan Babu Rf5d92742021-01-22 16:48:12 -0800220 error = xfs_iext_count_may_overflow(dp, XFS_DATA_FORK,
221 XFS_IEXT_DIR_MANIP_CNT(mp));
222 if (error)
223 goto out_trans_cancel;
224
Dave Chinner19de7352013-04-03 16:11:18 +1100225 /*
Dave Chinner19de7352013-04-03 16:11:18 +1100226 * Allocate an inode for the symlink.
227 */
Dave Chinnerb652afd2021-06-02 10:48:24 +1000228 error = xfs_dialloc(&tp, dp->i_ino, S_IFLNK, &ino);
229 if (!error)
230 error = xfs_init_new_inode(mnt_userns, tp, dp, ino,
231 S_IFLNK | (mode & ~S_IFMT), 1, 0, prid,
232 false, &ip);
Dave Chinner58c90472015-02-23 22:38:08 +1100233 if (error)
234 goto out_trans_cancel;
Dave Chinner19de7352013-04-03 16:11:18 +1100235
236 /*
Dave Chinner58c90472015-02-23 22:38:08 +1100237 * Now we join the directory inode to the transaction. We do not do it
238 * earlier because xfs_dir_ialloc might commit the previous transaction
239 * (and release all the locks). An error from here on will result in
240 * the transaction cancel unlocking dp so don't do it explicitly in the
Dave Chinner19de7352013-04-03 16:11:18 +1100241 * error path.
242 */
Christoph Hellwig65523212016-11-30 14:33:25 +1100243 xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
Dave Chinner19de7352013-04-03 16:11:18 +1100244 unlock_dp_on_error = false;
245
246 /*
247 * Also attach the dquot(s) to it, if applicable.
248 */
Chandra Seetharaman92f8ff72013-07-11 00:00:40 -0500249 xfs_qm_vop_create_dqattach(tp, ip, udqp, gdqp, pdqp);
Dave Chinner19de7352013-04-03 16:11:18 +1100250
Kaixu Xia57fd2d82020-04-22 21:54:31 -0700251 resblks -= XFS_IALLOC_SPACE_RES(mp);
Dave Chinner19de7352013-04-03 16:11:18 +1100252 /*
253 * If the symlink will fit into the inode, write it inline.
254 */
255 if (pathlen <= XFS_IFORK_DSIZE(ip)) {
Christoph Hellwig143f4ae2016-04-06 07:41:43 +1000256 xfs_init_local_fork(ip, XFS_DATA_FORK, target_path, pathlen);
257
Christoph Hellwig13d2c102021-03-29 11:11:40 -0700258 ip->i_disk_size = pathlen;
Christoph Hellwigf7e67b22020-05-18 10:28:05 -0700259 ip->i_df.if_format = XFS_DINODE_FMT_LOCAL;
Dave Chinner19de7352013-04-03 16:11:18 +1100260 xfs_trans_log_inode(tp, ip, XFS_ILOG_DDATA | XFS_ILOG_CORE);
Dave Chinner19de7352013-04-03 16:11:18 +1100261 } else {
Dave Chinnerf948dd72013-04-03 16:11:19 +1100262 int offset;
263
Dave Chinner19de7352013-04-03 16:11:18 +1100264 first_fsb = 0;
265 nmaps = XFS_SYMLINK_MAPS;
266
267 error = xfs_bmapi_write(tp, ip, first_fsb, fs_blocks,
Brian Fostera7beabe2018-07-11 22:26:25 -0700268 XFS_BMAPI_METADATA, resblks, mval, &nmaps);
Dave Chinner19de7352013-04-03 16:11:18 +1100269 if (error)
Brian Fosterc8eac492018-07-24 13:43:13 -0700270 goto out_trans_cancel;
Dave Chinner19de7352013-04-03 16:11:18 +1100271
Kaixu Xia57fd2d82020-04-22 21:54:31 -0700272 resblks -= fs_blocks;
Christoph Hellwig13d2c102021-03-29 11:11:40 -0700273 ip->i_disk_size = pathlen;
Dave Chinner19de7352013-04-03 16:11:18 +1100274 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
275
276 cur_chunk = target_path;
Dave Chinnerf948dd72013-04-03 16:11:19 +1100277 offset = 0;
Dave Chinner19de7352013-04-03 16:11:18 +1100278 for (n = 0; n < nmaps; n++) {
Dave Chinner321a9582013-05-27 16:38:20 +1000279 char *buf;
Dave Chinnerf948dd72013-04-03 16:11:19 +1100280
Dave Chinner19de7352013-04-03 16:11:18 +1100281 d = XFS_FSB_TO_DADDR(mp, mval[n].br_startblock);
282 byte_cnt = XFS_FSB_TO_B(mp, mval[n].br_blockcount);
Darrick J. Wongce924642020-01-23 17:01:18 -0800283 error = xfs_trans_get_buf(tp, mp->m_ddev_targp, d,
284 BTOBB(byte_cnt), 0, &bp);
285 if (error)
Brian Fosterc8eac492018-07-24 13:43:13 -0700286 goto out_trans_cancel;
Dave Chinnerf948dd72013-04-03 16:11:19 +1100287 bp->b_ops = &xfs_symlink_buf_ops;
288
289 byte_cnt = XFS_SYMLINK_BUF_SPACE(mp, byte_cnt);
Dave Chinner321a9582013-05-27 16:38:20 +1000290 byte_cnt = min(byte_cnt, pathlen);
Dave Chinner19de7352013-04-03 16:11:18 +1100291
Dave Chinnerf948dd72013-04-03 16:11:19 +1100292 buf = bp->b_addr;
293 buf += xfs_symlink_hdr_set(mp, ip->i_ino, offset,
294 byte_cnt, bp);
295
296 memcpy(buf, cur_chunk, byte_cnt);
297
Dave Chinner19de7352013-04-03 16:11:18 +1100298 cur_chunk += byte_cnt;
Dave Chinnerf948dd72013-04-03 16:11:19 +1100299 pathlen -= byte_cnt;
300 offset += byte_cnt;
Dave Chinner19de7352013-04-03 16:11:18 +1100301
Dave Chinnerdaf7b792013-09-02 10:32:00 +1000302 xfs_trans_buf_set_type(tp, bp, XFS_BLFT_SYMLINK_BUF);
Dave Chinnerf948dd72013-04-03 16:11:19 +1100303 xfs_trans_log_buf(tp, bp, 0, (buf + byte_cnt - 1) -
304 (char *)bp->b_addr);
Dave Chinner19de7352013-04-03 16:11:18 +1100305 }
Dave Chinner321a9582013-05-27 16:38:20 +1000306 ASSERT(pathlen == 0);
Dave Chinner19de7352013-04-03 16:11:18 +1100307 }
Christoph Hellwig13d2c102021-03-29 11:11:40 -0700308 i_size_write(VFS_I(ip), ip->i_disk_size);
Dave Chinner19de7352013-04-03 16:11:18 +1100309
310 /*
311 * Create the directory entry for the symlink.
312 */
Brian Foster381eee62018-07-11 22:26:21 -0700313 error = xfs_dir_createname(tp, dp, link_name, ip->i_ino, resblks);
Dave Chinner19de7352013-04-03 16:11:18 +1100314 if (error)
Brian Fosterc8eac492018-07-24 13:43:13 -0700315 goto out_trans_cancel;
Dave Chinner19de7352013-04-03 16:11:18 +1100316 xfs_trans_ichgtime(tp, dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
317 xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
318
319 /*
320 * If this is a synchronous mount, make sure that the
321 * symlink transaction goes to disk before returning to
322 * the user.
323 */
Dave Chinner0560f312021-08-18 18:46:52 -0700324 if (xfs_has_wsync(mp) || xfs_has_dirsync(mp))
Dave Chinner19de7352013-04-03 16:11:18 +1100325 xfs_trans_set_sync(tp);
Dave Chinner19de7352013-04-03 16:11:18 +1100326
Christoph Hellwig70393312015-06-04 13:48:08 +1000327 error = xfs_trans_commit(tp);
Dave Chinner58c90472015-02-23 22:38:08 +1100328 if (error)
329 goto out_release_inode;
330
Dave Chinner19de7352013-04-03 16:11:18 +1100331 xfs_qm_dqrele(udqp);
332 xfs_qm_dqrele(gdqp);
Chandra Seetharaman92f8ff72013-07-11 00:00:40 -0500333 xfs_qm_dqrele(pdqp);
Dave Chinner19de7352013-04-03 16:11:18 +1100334
335 *ipp = ip;
336 return 0;
337
Dave Chinner58c90472015-02-23 22:38:08 +1100338out_trans_cancel:
Christoph Hellwig4906e212015-06-04 13:47:56 +1000339 xfs_trans_cancel(tp);
Dave Chinner58c90472015-02-23 22:38:08 +1100340out_release_inode:
341 /*
342 * Wait until after the current transaction is aborted to finish the
343 * setup of the inode and release the inode. This prevents recursive
344 * transactions and deadlocks from xfs_inactive.
345 */
346 if (ip) {
347 xfs_finish_inode_setup(ip);
Darrick J. Wong44a87362018-07-25 12:52:32 -0700348 xfs_irele(ip);
Dave Chinner58c90472015-02-23 22:38:08 +1100349 }
Darrick J. Wongf2f7b9f2021-01-27 12:07:57 -0800350out_release_dquots:
Dave Chinner19de7352013-04-03 16:11:18 +1100351 xfs_qm_dqrele(udqp);
352 xfs_qm_dqrele(gdqp);
Chandra Seetharaman92f8ff72013-07-11 00:00:40 -0500353 xfs_qm_dqrele(pdqp);
Dave Chinner19de7352013-04-03 16:11:18 +1100354
355 if (unlock_dp_on_error)
Christoph Hellwig65523212016-11-30 14:33:25 +1100356 xfs_iunlock(dp, XFS_ILOCK_EXCL);
Dave Chinner19de7352013-04-03 16:11:18 +1100357 return error;
358}
359
360/*
361 * Free a symlink that has blocks associated with it.
Dave Chinner43feeea2018-12-12 08:46:21 -0800362 *
363 * Note: zero length symlinks are not allowed to exist. When we set the size to
364 * zero, also change it to a regular file so that it does not get written to
365 * disk as a zero length symlink. The inode is on the unlinked list already, so
366 * userspace cannot find this inode anymore, so this change is not user visible
367 * but allows us to catch corrupt zero-length symlinks in the verifiers.
Dave Chinner19de7352013-04-03 16:11:18 +1100368 */
Mark Tinguely725eb1e2013-06-17 15:35:57 -0500369STATIC int
Dave Chinner19de7352013-04-03 16:11:18 +1100370xfs_inactive_symlink_rmt(
Brian Foster36b21dd2013-09-20 11:06:09 -0400371 struct xfs_inode *ip)
Dave Chinner19de7352013-04-03 16:11:18 +1100372{
Dave Chinnere8222612020-12-16 16:07:34 -0800373 struct xfs_buf *bp;
Dave Chinner19de7352013-04-03 16:11:18 +1100374 int done;
375 int error;
Dave Chinner19de7352013-04-03 16:11:18 +1100376 int i;
377 xfs_mount_t *mp;
378 xfs_bmbt_irec_t mval[XFS_SYMLINK_MAPS];
379 int nmaps;
Dave Chinner19de7352013-04-03 16:11:18 +1100380 int size;
381 xfs_trans_t *tp;
382
Dave Chinner19de7352013-04-03 16:11:18 +1100383 mp = ip->i_mount;
Christoph Hellwigb2197a32021-04-13 11:15:12 -0700384 ASSERT(!xfs_need_iread_extents(&ip->i_df));
Dave Chinner19de7352013-04-03 16:11:18 +1100385 /*
386 * We're freeing a symlink that has some
387 * blocks allocated to it. Free the
388 * blocks here. We know that we've got
389 * either 1 or 2 extents and that we can
390 * free them all in one bunmapi call.
391 */
Christoph Hellwigdaf83962020-05-18 10:27:22 -0700392 ASSERT(ip->i_df.if_nextents > 0 && ip->i_df.if_nextents <= 2);
Dave Chinner19de7352013-04-03 16:11:18 +1100393
Christoph Hellwig253f4912016-04-06 09:19:55 +1000394 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_itruncate, 0, 0, 0, &tp);
395 if (error)
Brian Foster36b21dd2013-09-20 11:06:09 -0400396 return error;
Brian Foster36b21dd2013-09-20 11:06:09 -0400397
398 xfs_ilock(ip, XFS_ILOCK_EXCL);
399 xfs_trans_ijoin(tp, ip, 0);
400
Dave Chinner19de7352013-04-03 16:11:18 +1100401 /*
Dave Chinner43feeea2018-12-12 08:46:21 -0800402 * Lock the inode, fix the size, turn it into a regular file and join it
403 * to the transaction. Hold it so in the normal path, we still have it
404 * locked for the second transaction. In the error paths we need it
Dave Chinner19de7352013-04-03 16:11:18 +1100405 * held so the cancel won't rele it, see below.
406 */
Christoph Hellwig13d2c102021-03-29 11:11:40 -0700407 size = (int)ip->i_disk_size;
408 ip->i_disk_size = 0;
Dave Chinner43feeea2018-12-12 08:46:21 -0800409 VFS_I(ip)->i_mode = (VFS_I(ip)->i_mode & ~S_IFMT) | S_IFREG;
Dave Chinner19de7352013-04-03 16:11:18 +1100410 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
411 /*
412 * Find the block(s) so we can inval and unmap them.
413 */
414 done = 0;
Dave Chinner19de7352013-04-03 16:11:18 +1100415 nmaps = ARRAY_SIZE(mval);
Dave Chinnerf948dd72013-04-03 16:11:19 +1100416 error = xfs_bmapi_read(ip, 0, xfs_symlink_blocks(mp, size),
Dave Chinner19de7352013-04-03 16:11:18 +1100417 mval, &nmaps, 0);
418 if (error)
Brian Foster36b21dd2013-09-20 11:06:09 -0400419 goto error_trans_cancel;
Dave Chinner19de7352013-04-03 16:11:18 +1100420 /*
Dave Chinnerf948dd72013-04-03 16:11:19 +1100421 * Invalidate the block(s). No validation is done.
Dave Chinner19de7352013-04-03 16:11:18 +1100422 */
423 for (i = 0; i < nmaps; i++) {
Darrick J. Wongce924642020-01-23 17:01:18 -0800424 error = xfs_trans_get_buf(tp, mp->m_ddev_targp,
425 XFS_FSB_TO_DADDR(mp, mval[i].br_startblock),
426 XFS_FSB_TO_BB(mp, mval[i].br_blockcount), 0,
427 &bp);
428 if (error)
Brian Fosterc8eac492018-07-24 13:43:13 -0700429 goto error_trans_cancel;
Dave Chinner19de7352013-04-03 16:11:18 +1100430 xfs_trans_binval(tp, bp);
431 }
432 /*
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000433 * Unmap the dead block(s) to the dfops.
Dave Chinner19de7352013-04-03 16:11:18 +1100434 */
Brian Foster2af52842018-07-11 22:26:25 -0700435 error = xfs_bunmapi(tp, ip, 0, size, 0, nmaps, &done);
Brian Foster36b21dd2013-09-20 11:06:09 -0400436 if (error)
Brian Fosterc8eac492018-07-24 13:43:13 -0700437 goto error_trans_cancel;
Dave Chinner19de7352013-04-03 16:11:18 +1100438 ASSERT(done);
Dave Chinner3565b6602018-05-09 07:49:09 -0700439
Dave Chinner19de7352013-04-03 16:11:18 +1100440 /*
Brian Fosterc8eac492018-07-24 13:43:13 -0700441 * Commit the transaction. This first logs the EFI and the inode, then
442 * rolls and commits the transaction that frees the extents.
Dave Chinner19de7352013-04-03 16:11:18 +1100443 */
Dave Chinner3565b6602018-05-09 07:49:09 -0700444 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
Christoph Hellwig70393312015-06-04 13:48:08 +1000445 error = xfs_trans_commit(tp);
Dave Chinner19de7352013-04-03 16:11:18 +1100446 if (error) {
Dave Chinner75c8c50f2021-08-18 18:46:53 -0700447 ASSERT(xfs_is_shutdown(mp));
Brian Foster36b21dd2013-09-20 11:06:09 -0400448 goto error_unlock;
Dave Chinner19de7352013-04-03 16:11:18 +1100449 }
Dave Chinner19de7352013-04-03 16:11:18 +1100450
451 /*
452 * Remove the memory for extent descriptions (just bookkeeping).
453 */
454 if (ip->i_df.if_bytes)
455 xfs_idata_realloc(ip, -ip->i_df.if_bytes, XFS_DATA_FORK);
456 ASSERT(ip->i_df.if_bytes == 0);
Dave Chinner19de7352013-04-03 16:11:18 +1100457
Brian Foster36b21dd2013-09-20 11:06:09 -0400458 xfs_iunlock(ip, XFS_ILOCK_EXCL);
Dave Chinner19de7352013-04-03 16:11:18 +1100459 return 0;
460
Brian Foster36b21dd2013-09-20 11:06:09 -0400461error_trans_cancel:
Christoph Hellwig4906e212015-06-04 13:47:56 +1000462 xfs_trans_cancel(tp);
Brian Foster36b21dd2013-09-20 11:06:09 -0400463error_unlock:
464 xfs_iunlock(ip, XFS_ILOCK_EXCL);
Dave Chinner19de7352013-04-03 16:11:18 +1100465 return error;
466}
Mark Tinguely725eb1e2013-06-17 15:35:57 -0500467
468/*
469 * xfs_inactive_symlink - free a symlink
470 */
471int
472xfs_inactive_symlink(
Brian Foster36b21dd2013-09-20 11:06:09 -0400473 struct xfs_inode *ip)
Mark Tinguely725eb1e2013-06-17 15:35:57 -0500474{
475 struct xfs_mount *mp = ip->i_mount;
476 int pathlen;
477
478 trace_xfs_inactive_symlink(ip);
479
Dave Chinner75c8c50f2021-08-18 18:46:53 -0700480 if (xfs_is_shutdown(mp))
Dave Chinner24513372014-06-25 14:58:08 +1000481 return -EIO;
Mark Tinguely725eb1e2013-06-17 15:35:57 -0500482
Brian Foster36b21dd2013-09-20 11:06:09 -0400483 xfs_ilock(ip, XFS_ILOCK_EXCL);
Christoph Hellwig13d2c102021-03-29 11:11:40 -0700484 pathlen = (int)ip->i_disk_size;
Dave Chinner43feeea2018-12-12 08:46:21 -0800485 ASSERT(pathlen);
Mark Tinguely725eb1e2013-06-17 15:35:57 -0500486
Dave Chinner43feeea2018-12-12 08:46:21 -0800487 if (pathlen <= 0 || pathlen > XFS_SYMLINK_MAXLEN) {
Mark Tinguely725eb1e2013-06-17 15:35:57 -0500488 xfs_alert(mp, "%s: inode (0x%llx) bad symlink length (%d)",
489 __func__, (unsigned long long)ip->i_ino, pathlen);
Brian Foster36b21dd2013-09-20 11:06:09 -0400490 xfs_iunlock(ip, XFS_ILOCK_EXCL);
Mark Tinguely725eb1e2013-06-17 15:35:57 -0500491 ASSERT(0);
Dave Chinner24513372014-06-25 14:58:08 +1000492 return -EFSCORRUPTED;
Mark Tinguely725eb1e2013-06-17 15:35:57 -0500493 }
494
Dave Chinner43feeea2018-12-12 08:46:21 -0800495 /*
496 * Inline fork state gets removed by xfs_difree() so we have nothing to
497 * do here in that case.
498 */
Christoph Hellwig0779f4a2021-04-13 11:15:11 -0700499 if (ip->i_df.if_format == XFS_DINODE_FMT_LOCAL) {
Brian Foster36b21dd2013-09-20 11:06:09 -0400500 xfs_iunlock(ip, XFS_ILOCK_EXCL);
Mark Tinguely725eb1e2013-06-17 15:35:57 -0500501 return 0;
502 }
503
Brian Foster36b21dd2013-09-20 11:06:09 -0400504 xfs_iunlock(ip, XFS_ILOCK_EXCL);
505
Mark Tinguely725eb1e2013-06-17 15:35:57 -0500506 /* remove the remote symlink */
Brian Foster36b21dd2013-09-20 11:06:09 -0400507 return xfs_inactive_symlink_rmt(ip);
Mark Tinguely725eb1e2013-06-17 15:35:57 -0500508}