blob: affbedf7816054dc0e39c8e806867da52311fcbe [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"
Darrick J. Wong7b7820b2021-12-15 12:07:41 -080025#include "xfs_error.h"
Dave Chinner19de7352013-04-03 16:11:18 +110026
27/* ----- Kernel only functions below ----- */
Darrick J. Wong5da8f2f2017-06-16 11:00:15 -070028int
29xfs_readlink_bmap_ilocked(
Dave Chinnerf948dd72013-04-03 16:11:19 +110030 struct xfs_inode *ip,
31 char *link)
Dave Chinner19de7352013-04-03 16:11:18 +110032{
Dave Chinnerf948dd72013-04-03 16:11:19 +110033 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;
Christoph Hellwig13d2c102021-03-29 11:11:40 -070038 int pathlen = ip->i_disk_size;
Dave Chinnerf948dd72013-04-03 16:11:19 +110039 int nmaps = XFS_SYMLINK_MAPS;
40 int byte_cnt;
41 int n;
42 int error = 0;
43 int fsblocks = 0;
44 int offset;
Dave Chinner19de7352013-04-03 16:11:18 +110045
Christoph Hellwig29db2502017-07-13 12:14:34 -070046 ASSERT(xfs_isilocked(ip, XFS_ILOCK_SHARED | XFS_ILOCK_EXCL));
47
Dave Chinnerf948dd72013-04-03 16:11:19 +110048 fsblocks = xfs_symlink_blocks(mp, pathlen);
49 error = xfs_bmapi_read(ip, 0, fsblocks, mval, &nmaps, 0);
Dave Chinner19de7352013-04-03 16:11:18 +110050 if (error)
51 goto out;
52
Dave Chinnerf948dd72013-04-03 16:11:19 +110053 offset = 0;
Dave Chinner19de7352013-04-03 16:11:18 +110054 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
Darrick J. Wong0e3eccc2020-01-23 17:01:17 -080058 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;
Dave Chinnerf948dd72013-04-03 16:11:19 +110062 byte_cnt = XFS_SYMLINK_BUF_SPACE(mp, byte_cnt);
Dave Chinner19de7352013-04-03 16:11:18 +110063 if (pathlen < byte_cnt)
64 byte_cnt = pathlen;
Dave Chinner19de7352013-04-03 16:11:18 +110065
Dave Chinnerf948dd72013-04-03 16:11:19 +110066 cur_chunk = bp->b_addr;
Dave Chinner38c26bf2021-08-18 18:46:37 -070067 if (xfs_has_crc(mp)) {
Eric Sandeenbda65ef2014-04-14 19:05:43 +100068 if (!xfs_symlink_hdr_ok(ip->i_ino, offset,
Dave Chinnerf948dd72013-04-03 16:11:19 +110069 byte_cnt, bp)) {
Dave Chinner24513372014-06-25 14:58:08 +100070 error = -EFSCORRUPTED;
Dave Chinnerf948dd72013-04-03 16:11:19 +110071 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
Eric Sandeen2ac56d32015-06-22 09:42:48 +100082 memcpy(link + offset, cur_chunk, byte_cnt);
Dave Chinnerf948dd72013-04-03 16:11:19 +110083
84 pathlen -= byte_cnt;
85 offset += byte_cnt;
86
Dave Chinner19de7352013-04-03 16:11:18 +110087 xfs_buf_relse(bp);
88 }
Dave Chinnerf948dd72013-04-03 16:11:19 +110089 ASSERT(pathlen == 0);
Dave Chinner19de7352013-04-03 16:11:18 +110090
Christoph Hellwig13d2c102021-03-29 11:11:40 -070091 link[ip->i_disk_size] = '\0';
Dave Chinner19de7352013-04-03 16:11:18 +110092 error = 0;
93
94 out:
95 return error;
96}
97
98int
99xfs_readlink(
Darrick J. Wong7b7820b2021-12-15 12:07:41 -0800100 struct xfs_inode *ip,
101 char *link)
Dave Chinner19de7352013-04-03 16:11:18 +1100102{
Darrick J. Wong7b7820b2021-12-15 12:07:41 -0800103 struct xfs_mount *mp = ip->i_mount;
104 xfs_fsize_t pathlen;
105 int error = -EFSCORRUPTED;
Dave Chinner19de7352013-04-03 16:11:18 +1100106
107 trace_xfs_readlink(ip);
108
Dave Chinner75c8c50f2021-08-18 18:46:53 -0700109 if (xfs_is_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 Chinner19de7352013-04-03 16:11:18 +1100123 goto out;
124 }
125
Darrick J. Wong7b7820b2021-12-15 12:07:41 -0800126 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;
Dave Chinner19de7352013-04-03 16:11:18 +1100133
Darrick J. Wong7b7820b2021-12-15 12:07:41 -0800134 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 }
Dave Chinner19de7352013-04-03 16:11:18 +1100139
140 out:
141 xfs_iunlock(ip, XFS_ILOCK_SHARED);
142 return error;
143}
144
145int
146xfs_symlink(
Christoph Hellwigf736d932021-01-21 14:19:58 +0100147 struct user_namespace *mnt_userns,
Dave Chinnerf948dd72013-04-03 16:11:19 +1100148 struct xfs_inode *dp,
Dave Chinner19de7352013-04-03 16:11:18 +1100149 struct xfs_name *link_name,
150 const char *target_path,
151 umode_t mode,
Dave Chinnerf948dd72013-04-03 16:11:19 +1100152 struct xfs_inode **ipp)
Dave Chinner19de7352013-04-03 16:11:18 +1100153{
Dave Chinnerf948dd72013-04-03 16:11:19 +1100154 struct xfs_mount *mp = dp->i_mount;
155 struct xfs_trans *tp = NULL;
156 struct xfs_inode *ip = NULL;
157 int error = 0;
Dave Chinner19de7352013-04-03 16:11:18 +1100158 int pathlen;
Dave Chinner58c90472015-02-23 22:38:08 +1100159 bool unlock_dp_on_error = false;
Dave Chinner19de7352013-04-03 16:11:18 +1100160 xfs_fileoff_t first_fsb;
161 xfs_filblks_t fs_blocks;
162 int nmaps;
Dave Chinnerf948dd72013-04-03 16:11:19 +1100163 struct xfs_bmbt_irec mval[XFS_SYMLINK_MAPS];
Dave Chinner19de7352013-04-03 16:11:18 +1100164 xfs_daddr_t d;
165 const char *cur_chunk;
166 int byte_cnt;
167 int n;
Dave Chinnere8222612020-12-16 16:07:34 -0800168 struct xfs_buf *bp;
Dave Chinner19de7352013-04-03 16:11:18 +1100169 prid_t prid;
Chandra Seetharaman113a5682013-06-27 17:25:07 -0500170 struct xfs_dquot *udqp = NULL;
171 struct xfs_dquot *gdqp = NULL;
Chandra Seetharaman92f8ff72013-07-11 00:00:40 -0500172 struct xfs_dquot *pdqp = NULL;
Dave Chinner19de7352013-04-03 16:11:18 +1100173 uint resblks;
Dave Chinnerb652afd2021-06-02 10:48:24 +1000174 xfs_ino_t ino;
Dave Chinner19de7352013-04-03 16:11:18 +1100175
176 *ipp = NULL;
Dave Chinner19de7352013-04-03 16:11:18 +1100177
178 trace_xfs_symlink(dp, link_name);
179
Dave Chinner75c8c50f2021-08-18 18:46:53 -0700180 if (xfs_is_shutdown(mp))
Dave Chinner24513372014-06-25 14:58:08 +1000181 return -EIO;
Dave Chinner19de7352013-04-03 16:11:18 +1100182
183 /*
184 * Check component lengths of the target path name.
185 */
186 pathlen = strlen(target_path);
Darrick J. Wong6eb0b8d2017-07-07 08:37:26 -0700187 if (pathlen >= XFS_SYMLINK_MAXLEN) /* total string too long */
Dave Chinner24513372014-06-25 14:58:08 +1000188 return -ENAMETOOLONG;
Dave Chinner43feeea2018-12-12 08:46:21 -0800189 ASSERT(pathlen > 0);
Dave Chinner19de7352013-04-03 16:11:18 +1100190
Zhi Yong Wu163467d2013-12-18 08:22:39 +0800191 prid = xfs_get_initial_prid(dp);
Dave Chinner19de7352013-04-03 16:11:18 +1100192
193 /*
194 * Make sure that we have allocated dquot(s) on disk.
195 */
Christian Brauner209188c2021-12-03 12:17:05 +0100196 error = xfs_qm_vop_dqalloc(dp, mapped_fsuid(mnt_userns, &init_user_ns),
197 mapped_fsgid(mnt_userns, &init_user_ns), prid,
Dwight Engen7aab1b22013-08-15 14:08:01 -0400198 XFS_QMOPT_QUOTALL | XFS_QMOPT_INHERIT,
199 &udqp, &gdqp, &pdqp);
Dave Chinner19de7352013-04-03 16:11:18 +1100200 if (error)
Dave Chinner58c90472015-02-23 22:38:08 +1100201 return error;
Dave Chinner19de7352013-04-03 16:11:18 +1100202
Dave Chinner19de7352013-04-03 16:11:18 +1100203 /*
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 */
Christoph Hellwige9e2eae2020-03-18 08:15:10 -0700207 if (pathlen <= XFS_LITINO(mp))
Dave Chinner19de7352013-04-03 16:11:18 +1100208 fs_blocks = 0;
209 else
Dave Chinner321a9582013-05-27 16:38:20 +1000210 fs_blocks = xfs_symlink_blocks(mp, pathlen);
Dave Chinner19de7352013-04-03 16:11:18 +1100211 resblks = XFS_SYMLINK_SPACE_RES(mp, link_name->len, fs_blocks);
Christoph Hellwig253f4912016-04-06 09:19:55 +1000212
Darrick J. Wongf2f7b9f2021-01-27 12:07:57 -0800213 error = xfs_trans_alloc_icreate(mp, &M_RES(mp)->tr_symlink, udqp, gdqp,
214 pdqp, resblks, &tp);
Christoph Hellwig4906e212015-06-04 13:47:56 +1000215 if (error)
Darrick J. Wongf2f7b9f2021-01-27 12:07:57 -0800216 goto out_release_dquots;
Dave Chinner19de7352013-04-03 16:11:18 +1100217
Christoph Hellwig65523212016-11-30 14:33:25 +1100218 xfs_ilock(dp, XFS_ILOCK_EXCL | XFS_ILOCK_PARENT);
Dave Chinner19de7352013-04-03 16:11:18 +1100219 unlock_dp_on_error = true;
220
221 /*
222 * Check whether the directory allows new symlinks or not.
223 */
Christoph Hellwigdb073492021-03-29 11:11:44 -0700224 if (dp->i_diflags & XFS_DIFLAG_NOSYMLINKS) {
Dave Chinner24513372014-06-25 14:58:08 +1000225 error = -EPERM;
Dave Chinner58c90472015-02-23 22:38:08 +1100226 goto out_trans_cancel;
Dave Chinner19de7352013-04-03 16:11:18 +1100227 }
228
Chandan Babu Rf5d92742021-01-22 16:48:12 -0800229 error = xfs_iext_count_may_overflow(dp, XFS_DATA_FORK,
230 XFS_IEXT_DIR_MANIP_CNT(mp));
231 if (error)
232 goto out_trans_cancel;
233
Dave Chinner19de7352013-04-03 16:11:18 +1100234 /*
Dave Chinner19de7352013-04-03 16:11:18 +1100235 * Allocate an inode for the symlink.
236 */
Dave Chinnerb652afd2021-06-02 10:48:24 +1000237 error = xfs_dialloc(&tp, dp->i_ino, S_IFLNK, &ino);
238 if (!error)
239 error = xfs_init_new_inode(mnt_userns, tp, dp, ino,
240 S_IFLNK | (mode & ~S_IFMT), 1, 0, prid,
241 false, &ip);
Dave Chinner58c90472015-02-23 22:38:08 +1100242 if (error)
243 goto out_trans_cancel;
Dave Chinner19de7352013-04-03 16:11:18 +1100244
245 /*
Dave Chinner58c90472015-02-23 22:38:08 +1100246 * Now we join the directory inode to the transaction. We do not do it
247 * earlier because xfs_dir_ialloc might commit the previous transaction
248 * (and release all the locks). An error from here on will result in
249 * the transaction cancel unlocking dp so don't do it explicitly in the
Dave Chinner19de7352013-04-03 16:11:18 +1100250 * error path.
251 */
Christoph Hellwig65523212016-11-30 14:33:25 +1100252 xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
Dave Chinner19de7352013-04-03 16:11:18 +1100253 unlock_dp_on_error = false;
254
255 /*
256 * Also attach the dquot(s) to it, if applicable.
257 */
Chandra Seetharaman92f8ff72013-07-11 00:00:40 -0500258 xfs_qm_vop_create_dqattach(tp, ip, udqp, gdqp, pdqp);
Dave Chinner19de7352013-04-03 16:11:18 +1100259
Kaixu Xia57fd2d82020-04-22 21:54:31 -0700260 resblks -= XFS_IALLOC_SPACE_RES(mp);
Dave Chinner19de7352013-04-03 16:11:18 +1100261 /*
262 * If the symlink will fit into the inode, write it inline.
263 */
264 if (pathlen <= XFS_IFORK_DSIZE(ip)) {
Christoph Hellwig143f4ae2016-04-06 07:41:43 +1000265 xfs_init_local_fork(ip, XFS_DATA_FORK, target_path, pathlen);
266
Christoph Hellwig13d2c102021-03-29 11:11:40 -0700267 ip->i_disk_size = pathlen;
Christoph Hellwigf7e67b22020-05-18 10:28:05 -0700268 ip->i_df.if_format = XFS_DINODE_FMT_LOCAL;
Dave Chinner19de7352013-04-03 16:11:18 +1100269 xfs_trans_log_inode(tp, ip, XFS_ILOG_DDATA | XFS_ILOG_CORE);
Dave Chinner19de7352013-04-03 16:11:18 +1100270 } else {
Dave Chinnerf948dd72013-04-03 16:11:19 +1100271 int offset;
272
Dave Chinner19de7352013-04-03 16:11:18 +1100273 first_fsb = 0;
274 nmaps = XFS_SYMLINK_MAPS;
275
276 error = xfs_bmapi_write(tp, ip, first_fsb, fs_blocks,
Brian Fostera7beabe2018-07-11 22:26:25 -0700277 XFS_BMAPI_METADATA, resblks, mval, &nmaps);
Dave Chinner19de7352013-04-03 16:11:18 +1100278 if (error)
Brian Fosterc8eac492018-07-24 13:43:13 -0700279 goto out_trans_cancel;
Dave Chinner19de7352013-04-03 16:11:18 +1100280
Kaixu Xia57fd2d82020-04-22 21:54:31 -0700281 resblks -= fs_blocks;
Christoph Hellwig13d2c102021-03-29 11:11:40 -0700282 ip->i_disk_size = pathlen;
Dave Chinner19de7352013-04-03 16:11:18 +1100283 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
284
285 cur_chunk = target_path;
Dave Chinnerf948dd72013-04-03 16:11:19 +1100286 offset = 0;
Dave Chinner19de7352013-04-03 16:11:18 +1100287 for (n = 0; n < nmaps; n++) {
Dave Chinner321a9582013-05-27 16:38:20 +1000288 char *buf;
Dave Chinnerf948dd72013-04-03 16:11:19 +1100289
Dave Chinner19de7352013-04-03 16:11:18 +1100290 d = XFS_FSB_TO_DADDR(mp, mval[n].br_startblock);
291 byte_cnt = XFS_FSB_TO_B(mp, mval[n].br_blockcount);
Darrick J. Wongce924642020-01-23 17:01:18 -0800292 error = xfs_trans_get_buf(tp, mp->m_ddev_targp, d,
293 BTOBB(byte_cnt), 0, &bp);
294 if (error)
Brian Fosterc8eac492018-07-24 13:43:13 -0700295 goto out_trans_cancel;
Dave Chinnerf948dd72013-04-03 16:11:19 +1100296 bp->b_ops = &xfs_symlink_buf_ops;
297
298 byte_cnt = XFS_SYMLINK_BUF_SPACE(mp, byte_cnt);
Dave Chinner321a9582013-05-27 16:38:20 +1000299 byte_cnt = min(byte_cnt, pathlen);
Dave Chinner19de7352013-04-03 16:11:18 +1100300
Dave Chinnerf948dd72013-04-03 16:11:19 +1100301 buf = bp->b_addr;
302 buf += xfs_symlink_hdr_set(mp, ip->i_ino, offset,
303 byte_cnt, bp);
304
305 memcpy(buf, cur_chunk, byte_cnt);
306
Dave Chinner19de7352013-04-03 16:11:18 +1100307 cur_chunk += byte_cnt;
Dave Chinnerf948dd72013-04-03 16:11:19 +1100308 pathlen -= byte_cnt;
309 offset += byte_cnt;
Dave Chinner19de7352013-04-03 16:11:18 +1100310
Dave Chinnerdaf7b792013-09-02 10:32:00 +1000311 xfs_trans_buf_set_type(tp, bp, XFS_BLFT_SYMLINK_BUF);
Dave Chinnerf948dd72013-04-03 16:11:19 +1100312 xfs_trans_log_buf(tp, bp, 0, (buf + byte_cnt - 1) -
313 (char *)bp->b_addr);
Dave Chinner19de7352013-04-03 16:11:18 +1100314 }
Dave Chinner321a9582013-05-27 16:38:20 +1000315 ASSERT(pathlen == 0);
Dave Chinner19de7352013-04-03 16:11:18 +1100316 }
Christoph Hellwig13d2c102021-03-29 11:11:40 -0700317 i_size_write(VFS_I(ip), ip->i_disk_size);
Dave Chinner19de7352013-04-03 16:11:18 +1100318
319 /*
320 * Create the directory entry for the symlink.
321 */
Brian Foster381eee62018-07-11 22:26:21 -0700322 error = xfs_dir_createname(tp, dp, link_name, ip->i_ino, resblks);
Dave Chinner19de7352013-04-03 16:11:18 +1100323 if (error)
Brian Fosterc8eac492018-07-24 13:43:13 -0700324 goto out_trans_cancel;
Dave Chinner19de7352013-04-03 16:11:18 +1100325 xfs_trans_ichgtime(tp, dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
326 xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
327
328 /*
329 * If this is a synchronous mount, make sure that the
330 * symlink transaction goes to disk before returning to
331 * the user.
332 */
Dave Chinner0560f312021-08-18 18:46:52 -0700333 if (xfs_has_wsync(mp) || xfs_has_dirsync(mp))
Dave Chinner19de7352013-04-03 16:11:18 +1100334 xfs_trans_set_sync(tp);
Dave Chinner19de7352013-04-03 16:11:18 +1100335
Christoph Hellwig70393312015-06-04 13:48:08 +1000336 error = xfs_trans_commit(tp);
Dave Chinner58c90472015-02-23 22:38:08 +1100337 if (error)
338 goto out_release_inode;
339
Dave Chinner19de7352013-04-03 16:11:18 +1100340 xfs_qm_dqrele(udqp);
341 xfs_qm_dqrele(gdqp);
Chandra Seetharaman92f8ff72013-07-11 00:00:40 -0500342 xfs_qm_dqrele(pdqp);
Dave Chinner19de7352013-04-03 16:11:18 +1100343
344 *ipp = ip;
345 return 0;
346
Dave Chinner58c90472015-02-23 22:38:08 +1100347out_trans_cancel:
Christoph Hellwig4906e212015-06-04 13:47:56 +1000348 xfs_trans_cancel(tp);
Dave Chinner58c90472015-02-23 22:38:08 +1100349out_release_inode:
350 /*
351 * Wait until after the current transaction is aborted to finish the
352 * setup of the inode and release the inode. This prevents recursive
353 * transactions and deadlocks from xfs_inactive.
354 */
355 if (ip) {
356 xfs_finish_inode_setup(ip);
Darrick J. Wong44a87362018-07-25 12:52:32 -0700357 xfs_irele(ip);
Dave Chinner58c90472015-02-23 22:38:08 +1100358 }
Darrick J. Wongf2f7b9f2021-01-27 12:07:57 -0800359out_release_dquots:
Dave Chinner19de7352013-04-03 16:11:18 +1100360 xfs_qm_dqrele(udqp);
361 xfs_qm_dqrele(gdqp);
Chandra Seetharaman92f8ff72013-07-11 00:00:40 -0500362 xfs_qm_dqrele(pdqp);
Dave Chinner19de7352013-04-03 16:11:18 +1100363
364 if (unlock_dp_on_error)
Christoph Hellwig65523212016-11-30 14:33:25 +1100365 xfs_iunlock(dp, XFS_ILOCK_EXCL);
Dave Chinner19de7352013-04-03 16:11:18 +1100366 return error;
367}
368
369/*
370 * Free a symlink that has blocks associated with it.
Dave Chinner43feeea2018-12-12 08:46:21 -0800371 *
372 * Note: zero length symlinks are not allowed to exist. When we set the size to
373 * zero, also change it to a regular file so that it does not get written to
374 * disk as a zero length symlink. The inode is on the unlinked list already, so
375 * userspace cannot find this inode anymore, so this change is not user visible
376 * but allows us to catch corrupt zero-length symlinks in the verifiers.
Dave Chinner19de7352013-04-03 16:11:18 +1100377 */
Mark Tinguely725eb1e2013-06-17 15:35:57 -0500378STATIC int
Dave Chinner19de7352013-04-03 16:11:18 +1100379xfs_inactive_symlink_rmt(
Brian Foster36b21dd2013-09-20 11:06:09 -0400380 struct xfs_inode *ip)
Dave Chinner19de7352013-04-03 16:11:18 +1100381{
Dave Chinnere8222612020-12-16 16:07:34 -0800382 struct xfs_buf *bp;
Dave Chinner19de7352013-04-03 16:11:18 +1100383 int done;
384 int error;
Dave Chinner19de7352013-04-03 16:11:18 +1100385 int i;
386 xfs_mount_t *mp;
387 xfs_bmbt_irec_t mval[XFS_SYMLINK_MAPS];
388 int nmaps;
Dave Chinner19de7352013-04-03 16:11:18 +1100389 int size;
390 xfs_trans_t *tp;
391
Dave Chinner19de7352013-04-03 16:11:18 +1100392 mp = ip->i_mount;
Christoph Hellwigb2197a32021-04-13 11:15:12 -0700393 ASSERT(!xfs_need_iread_extents(&ip->i_df));
Dave Chinner19de7352013-04-03 16:11:18 +1100394 /*
395 * We're freeing a symlink that has some
396 * blocks allocated to it. Free the
397 * blocks here. We know that we've got
398 * either 1 or 2 extents and that we can
399 * free them all in one bunmapi call.
400 */
Christoph Hellwigdaf83962020-05-18 10:27:22 -0700401 ASSERT(ip->i_df.if_nextents > 0 && ip->i_df.if_nextents <= 2);
Dave Chinner19de7352013-04-03 16:11:18 +1100402
Christoph Hellwig253f4912016-04-06 09:19:55 +1000403 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_itruncate, 0, 0, 0, &tp);
404 if (error)
Brian Foster36b21dd2013-09-20 11:06:09 -0400405 return error;
Brian Foster36b21dd2013-09-20 11:06:09 -0400406
407 xfs_ilock(ip, XFS_ILOCK_EXCL);
408 xfs_trans_ijoin(tp, ip, 0);
409
Dave Chinner19de7352013-04-03 16:11:18 +1100410 /*
Dave Chinner43feeea2018-12-12 08:46:21 -0800411 * Lock the inode, fix the size, turn it into a regular file and join it
412 * to the transaction. Hold it so in the normal path, we still have it
413 * locked for the second transaction. In the error paths we need it
Dave Chinner19de7352013-04-03 16:11:18 +1100414 * held so the cancel won't rele it, see below.
415 */
Christoph Hellwig13d2c102021-03-29 11:11:40 -0700416 size = (int)ip->i_disk_size;
417 ip->i_disk_size = 0;
Dave Chinner43feeea2018-12-12 08:46:21 -0800418 VFS_I(ip)->i_mode = (VFS_I(ip)->i_mode & ~S_IFMT) | S_IFREG;
Dave Chinner19de7352013-04-03 16:11:18 +1100419 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
420 /*
421 * Find the block(s) so we can inval and unmap them.
422 */
423 done = 0;
Dave Chinner19de7352013-04-03 16:11:18 +1100424 nmaps = ARRAY_SIZE(mval);
Dave Chinnerf948dd72013-04-03 16:11:19 +1100425 error = xfs_bmapi_read(ip, 0, xfs_symlink_blocks(mp, size),
Dave Chinner19de7352013-04-03 16:11:18 +1100426 mval, &nmaps, 0);
427 if (error)
Brian Foster36b21dd2013-09-20 11:06:09 -0400428 goto error_trans_cancel;
Dave Chinner19de7352013-04-03 16:11:18 +1100429 /*
Dave Chinnerf948dd72013-04-03 16:11:19 +1100430 * Invalidate the block(s). No validation is done.
Dave Chinner19de7352013-04-03 16:11:18 +1100431 */
432 for (i = 0; i < nmaps; i++) {
Darrick J. Wongce924642020-01-23 17:01:18 -0800433 error = xfs_trans_get_buf(tp, mp->m_ddev_targp,
434 XFS_FSB_TO_DADDR(mp, mval[i].br_startblock),
435 XFS_FSB_TO_BB(mp, mval[i].br_blockcount), 0,
436 &bp);
437 if (error)
Brian Fosterc8eac492018-07-24 13:43:13 -0700438 goto error_trans_cancel;
Dave Chinner19de7352013-04-03 16:11:18 +1100439 xfs_trans_binval(tp, bp);
440 }
441 /*
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000442 * Unmap the dead block(s) to the dfops.
Dave Chinner19de7352013-04-03 16:11:18 +1100443 */
Brian Foster2af52842018-07-11 22:26:25 -0700444 error = xfs_bunmapi(tp, ip, 0, size, 0, nmaps, &done);
Brian Foster36b21dd2013-09-20 11:06:09 -0400445 if (error)
Brian Fosterc8eac492018-07-24 13:43:13 -0700446 goto error_trans_cancel;
Dave Chinner19de7352013-04-03 16:11:18 +1100447 ASSERT(done);
Dave Chinner3565b6602018-05-09 07:49:09 -0700448
Dave Chinner19de7352013-04-03 16:11:18 +1100449 /*
Brian Fosterc8eac492018-07-24 13:43:13 -0700450 * Commit the transaction. This first logs the EFI and the inode, then
451 * rolls and commits the transaction that frees the extents.
Dave Chinner19de7352013-04-03 16:11:18 +1100452 */
Dave Chinner3565b6602018-05-09 07:49:09 -0700453 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
Christoph Hellwig70393312015-06-04 13:48:08 +1000454 error = xfs_trans_commit(tp);
Dave Chinner19de7352013-04-03 16:11:18 +1100455 if (error) {
Dave Chinner75c8c50f2021-08-18 18:46:53 -0700456 ASSERT(xfs_is_shutdown(mp));
Brian Foster36b21dd2013-09-20 11:06:09 -0400457 goto error_unlock;
Dave Chinner19de7352013-04-03 16:11:18 +1100458 }
Dave Chinner19de7352013-04-03 16:11:18 +1100459
460 /*
461 * Remove the memory for extent descriptions (just bookkeeping).
462 */
463 if (ip->i_df.if_bytes)
464 xfs_idata_realloc(ip, -ip->i_df.if_bytes, XFS_DATA_FORK);
465 ASSERT(ip->i_df.if_bytes == 0);
Dave Chinner19de7352013-04-03 16:11:18 +1100466
Brian Foster36b21dd2013-09-20 11:06:09 -0400467 xfs_iunlock(ip, XFS_ILOCK_EXCL);
Dave Chinner19de7352013-04-03 16:11:18 +1100468 return 0;
469
Brian Foster36b21dd2013-09-20 11:06:09 -0400470error_trans_cancel:
Christoph Hellwig4906e212015-06-04 13:47:56 +1000471 xfs_trans_cancel(tp);
Brian Foster36b21dd2013-09-20 11:06:09 -0400472error_unlock:
473 xfs_iunlock(ip, XFS_ILOCK_EXCL);
Dave Chinner19de7352013-04-03 16:11:18 +1100474 return error;
475}
Mark Tinguely725eb1e2013-06-17 15:35:57 -0500476
477/*
478 * xfs_inactive_symlink - free a symlink
479 */
480int
481xfs_inactive_symlink(
Brian Foster36b21dd2013-09-20 11:06:09 -0400482 struct xfs_inode *ip)
Mark Tinguely725eb1e2013-06-17 15:35:57 -0500483{
484 struct xfs_mount *mp = ip->i_mount;
485 int pathlen;
486
487 trace_xfs_inactive_symlink(ip);
488
Dave Chinner75c8c50f2021-08-18 18:46:53 -0700489 if (xfs_is_shutdown(mp))
Dave Chinner24513372014-06-25 14:58:08 +1000490 return -EIO;
Mark Tinguely725eb1e2013-06-17 15:35:57 -0500491
Brian Foster36b21dd2013-09-20 11:06:09 -0400492 xfs_ilock(ip, XFS_ILOCK_EXCL);
Christoph Hellwig13d2c102021-03-29 11:11:40 -0700493 pathlen = (int)ip->i_disk_size;
Dave Chinner43feeea2018-12-12 08:46:21 -0800494 ASSERT(pathlen);
Mark Tinguely725eb1e2013-06-17 15:35:57 -0500495
Dave Chinner43feeea2018-12-12 08:46:21 -0800496 if (pathlen <= 0 || pathlen > XFS_SYMLINK_MAXLEN) {
Mark Tinguely725eb1e2013-06-17 15:35:57 -0500497 xfs_alert(mp, "%s: inode (0x%llx) bad symlink length (%d)",
498 __func__, (unsigned long long)ip->i_ino, pathlen);
Brian Foster36b21dd2013-09-20 11:06:09 -0400499 xfs_iunlock(ip, XFS_ILOCK_EXCL);
Mark Tinguely725eb1e2013-06-17 15:35:57 -0500500 ASSERT(0);
Dave Chinner24513372014-06-25 14:58:08 +1000501 return -EFSCORRUPTED;
Mark Tinguely725eb1e2013-06-17 15:35:57 -0500502 }
503
Dave Chinner43feeea2018-12-12 08:46:21 -0800504 /*
505 * Inline fork state gets removed by xfs_difree() so we have nothing to
506 * do here in that case.
507 */
Christoph Hellwig0779f4a2021-04-13 11:15:11 -0700508 if (ip->i_df.if_format == XFS_DINODE_FMT_LOCAL) {
Brian Foster36b21dd2013-09-20 11:06:09 -0400509 xfs_iunlock(ip, XFS_ILOCK_EXCL);
Mark Tinguely725eb1e2013-06-17 15:35:57 -0500510 return 0;
511 }
512
Brian Foster36b21dd2013-09-20 11:06:09 -0400513 xfs_iunlock(ip, XFS_ILOCK_EXCL);
514
Mark Tinguely725eb1e2013-06-17 15:35:57 -0500515 /* remove the remote symlink */
Brian Foster36b21dd2013-09-20 11:06:09 -0400516 return xfs_inactive_symlink_rmt(ip);
Mark Tinguely725eb1e2013-06-17 15:35:57 -0500517}