blob: b2c1177c717ff4f0083a3788f02c3e6d3c745374 [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 Chinner57062782013-10-15 09:17:51 +110015#include "xfs_da_format.h"
Dave Chinnerd6cf1302014-06-06 15:14:11 +100016#include "xfs_da_btree.h"
Darrick J. Wong3ab78df2016-08-03 11:15:38 +100017#include "xfs_defer.h"
Dave Chinner2b9ab5a2013-08-12 20:49:37 +100018#include "xfs_dir2.h"
Dave Chinner19de7352013-04-03 16:11:18 +110019#include "xfs_inode.h"
Dave Chinner19de7352013-04-03 16:11:18 +110020#include "xfs_ialloc.h"
21#include "xfs_alloc.h"
22#include "xfs_bmap.h"
Dave Chinnera4fbe6a2013-10-23 10:51:50 +110023#include "xfs_bmap_btree.h"
Dave Chinner68988112013-08-12 20:49:42 +100024#include "xfs_bmap_util.h"
Dave Chinner19de7352013-04-03 16:11:18 +110025#include "xfs_error.h"
26#include "xfs_quota.h"
Dave Chinner19de7352013-04-03 16:11:18 +110027#include "xfs_trans_space.h"
Dave Chinner19de7352013-04-03 16:11:18 +110028#include "xfs_trace.h"
29#include "xfs_symlink.h"
Dave Chinner239880e2013-10-23 10:50:10 +110030#include "xfs_trans.h"
Dave Chinner239880e2013-10-23 10:50:10 +110031#include "xfs_log.h"
Dave Chinner19de7352013-04-03 16:11:18 +110032
33/* ----- Kernel only functions below ----- */
Darrick J. Wong5da8f2f2017-06-16 11:00:15 -070034int
35xfs_readlink_bmap_ilocked(
Dave Chinnerf948dd72013-04-03 16:11:19 +110036 struct xfs_inode *ip,
37 char *link)
Dave Chinner19de7352013-04-03 16:11:18 +110038{
Dave Chinnerf948dd72013-04-03 16:11:19 +110039 struct xfs_mount *mp = ip->i_mount;
40 struct xfs_bmbt_irec mval[XFS_SYMLINK_MAPS];
41 struct xfs_buf *bp;
42 xfs_daddr_t d;
43 char *cur_chunk;
44 int pathlen = ip->i_d.di_size;
45 int nmaps = XFS_SYMLINK_MAPS;
46 int byte_cnt;
47 int n;
48 int error = 0;
49 int fsblocks = 0;
50 int offset;
Dave Chinner19de7352013-04-03 16:11:18 +110051
Christoph Hellwig29db2502017-07-13 12:14:34 -070052 ASSERT(xfs_isilocked(ip, XFS_ILOCK_SHARED | XFS_ILOCK_EXCL));
53
Dave Chinnerf948dd72013-04-03 16:11:19 +110054 fsblocks = xfs_symlink_blocks(mp, pathlen);
55 error = xfs_bmapi_read(ip, 0, fsblocks, mval, &nmaps, 0);
Dave Chinner19de7352013-04-03 16:11:18 +110056 if (error)
57 goto out;
58
Dave Chinnerf948dd72013-04-03 16:11:19 +110059 offset = 0;
Dave Chinner19de7352013-04-03 16:11:18 +110060 for (n = 0; n < nmaps; n++) {
61 d = XFS_FSB_TO_DADDR(mp, mval[n].br_startblock);
62 byte_cnt = XFS_FSB_TO_B(mp, mval[n].br_blockcount);
63
Dave Chinnerf948dd72013-04-03 16:11:19 +110064 bp = xfs_buf_read(mp->m_ddev_targp, d, BTOBB(byte_cnt), 0,
65 &xfs_symlink_buf_ops);
Dave Chinner19de7352013-04-03 16:11:18 +110066 if (!bp)
Dave Chinner24513372014-06-25 14:58:08 +100067 return -ENOMEM;
Dave Chinner19de7352013-04-03 16:11:18 +110068 error = bp->b_error;
69 if (error) {
70 xfs_buf_ioerror_alert(bp, __func__);
71 xfs_buf_relse(bp);
Dave Chinnerac75a1f2014-03-07 16:19:14 +110072
73 /* bad CRC means corrupted metadata */
Dave Chinner24513372014-06-25 14:58:08 +100074 if (error == -EFSBADCRC)
75 error = -EFSCORRUPTED;
Dave Chinner19de7352013-04-03 16:11:18 +110076 goto out;
77 }
Dave Chinnerf948dd72013-04-03 16:11:19 +110078 byte_cnt = XFS_SYMLINK_BUF_SPACE(mp, byte_cnt);
Dave Chinner19de7352013-04-03 16:11:18 +110079 if (pathlen < byte_cnt)
80 byte_cnt = pathlen;
Dave Chinner19de7352013-04-03 16:11:18 +110081
Dave Chinnerf948dd72013-04-03 16:11:19 +110082 cur_chunk = bp->b_addr;
83 if (xfs_sb_version_hascrc(&mp->m_sb)) {
Eric Sandeenbda65ef2014-04-14 19:05:43 +100084 if (!xfs_symlink_hdr_ok(ip->i_ino, offset,
Dave Chinnerf948dd72013-04-03 16:11:19 +110085 byte_cnt, bp)) {
Dave Chinner24513372014-06-25 14:58:08 +100086 error = -EFSCORRUPTED;
Dave Chinnerf948dd72013-04-03 16:11:19 +110087 xfs_alert(mp,
88"symlink header does not match required off/len/owner (0x%x/Ox%x,0x%llx)",
89 offset, byte_cnt, ip->i_ino);
90 xfs_buf_relse(bp);
91 goto out;
92
93 }
94
95 cur_chunk += sizeof(struct xfs_dsymlink_hdr);
96 }
97
Eric Sandeen2ac56d32015-06-22 09:42:48 +100098 memcpy(link + offset, cur_chunk, byte_cnt);
Dave Chinnerf948dd72013-04-03 16:11:19 +110099
100 pathlen -= byte_cnt;
101 offset += byte_cnt;
102
Dave Chinner19de7352013-04-03 16:11:18 +1100103 xfs_buf_relse(bp);
104 }
Dave Chinnerf948dd72013-04-03 16:11:19 +1100105 ASSERT(pathlen == 0);
Dave Chinner19de7352013-04-03 16:11:18 +1100106
107 link[ip->i_d.di_size] = '\0';
108 error = 0;
109
110 out:
111 return error;
112}
113
114int
115xfs_readlink(
Dave Chinnerf948dd72013-04-03 16:11:19 +1100116 struct xfs_inode *ip,
Dave Chinner19de7352013-04-03 16:11:18 +1100117 char *link)
118{
Dave Chinnerf948dd72013-04-03 16:11:19 +1100119 struct xfs_mount *mp = ip->i_mount;
Dave Chinner19de7352013-04-03 16:11:18 +1100120 xfs_fsize_t pathlen;
121 int error = 0;
122
123 trace_xfs_readlink(ip);
124
Christoph Hellwig30ee0522016-04-06 07:53:29 +1000125 ASSERT(!(ip->i_df.if_flags & XFS_IFINLINE));
126
Dave Chinner19de7352013-04-03 16:11:18 +1100127 if (XFS_FORCED_SHUTDOWN(mp))
Dave Chinner24513372014-06-25 14:58:08 +1000128 return -EIO;
Dave Chinner19de7352013-04-03 16:11:18 +1100129
130 xfs_ilock(ip, XFS_ILOCK_SHARED);
131
132 pathlen = ip->i_d.di_size;
133 if (!pathlen)
134 goto out;
135
Darrick J. Wong6eb0b8d2017-07-07 08:37:26 -0700136 if (pathlen < 0 || pathlen > XFS_SYMLINK_MAXLEN) {
Dave Chinner19de7352013-04-03 16:11:18 +1100137 xfs_alert(mp, "%s: inode (%llu) bad symlink length (%lld)",
138 __func__, (unsigned long long) ip->i_ino,
139 (long long) pathlen);
140 ASSERT(0);
Dave Chinner24513372014-06-25 14:58:08 +1000141 error = -EFSCORRUPTED;
Dave Chinner19de7352013-04-03 16:11:18 +1100142 goto out;
143 }
144
145
Darrick J. Wong5da8f2f2017-06-16 11:00:15 -0700146 error = xfs_readlink_bmap_ilocked(ip, link);
Dave Chinner19de7352013-04-03 16:11:18 +1100147
148 out:
149 xfs_iunlock(ip, XFS_ILOCK_SHARED);
150 return error;
151}
152
153int
154xfs_symlink(
Dave Chinnerf948dd72013-04-03 16:11:19 +1100155 struct xfs_inode *dp,
Dave Chinner19de7352013-04-03 16:11:18 +1100156 struct xfs_name *link_name,
157 const char *target_path,
158 umode_t mode,
Dave Chinnerf948dd72013-04-03 16:11:19 +1100159 struct xfs_inode **ipp)
Dave Chinner19de7352013-04-03 16:11:18 +1100160{
Dave Chinnerf948dd72013-04-03 16:11:19 +1100161 struct xfs_mount *mp = dp->i_mount;
162 struct xfs_trans *tp = NULL;
163 struct xfs_inode *ip = NULL;
164 int error = 0;
Dave Chinner19de7352013-04-03 16:11:18 +1100165 int pathlen;
Dave Chinner58c90472015-02-23 22:38:08 +1100166 bool unlock_dp_on_error = false;
Dave Chinner19de7352013-04-03 16:11:18 +1100167 xfs_fileoff_t first_fsb;
168 xfs_filblks_t fs_blocks;
169 int nmaps;
Dave Chinnerf948dd72013-04-03 16:11:19 +1100170 struct xfs_bmbt_irec mval[XFS_SYMLINK_MAPS];
Dave Chinner19de7352013-04-03 16:11:18 +1100171 xfs_daddr_t d;
172 const char *cur_chunk;
173 int byte_cnt;
174 int n;
175 xfs_buf_t *bp;
176 prid_t prid;
Chandra Seetharaman113a5682013-06-27 17:25:07 -0500177 struct xfs_dquot *udqp = NULL;
178 struct xfs_dquot *gdqp = NULL;
Chandra Seetharaman92f8ff72013-07-11 00:00:40 -0500179 struct xfs_dquot *pdqp = NULL;
Dave Chinner19de7352013-04-03 16:11:18 +1100180 uint resblks;
181
182 *ipp = NULL;
Dave Chinner19de7352013-04-03 16:11:18 +1100183
184 trace_xfs_symlink(dp, link_name);
185
186 if (XFS_FORCED_SHUTDOWN(mp))
Dave Chinner24513372014-06-25 14:58:08 +1000187 return -EIO;
Dave Chinner19de7352013-04-03 16:11:18 +1100188
189 /*
190 * Check component lengths of the target path name.
191 */
192 pathlen = strlen(target_path);
Darrick J. Wong6eb0b8d2017-07-07 08:37:26 -0700193 if (pathlen >= XFS_SYMLINK_MAXLEN) /* total string too long */
Dave Chinner24513372014-06-25 14:58:08 +1000194 return -ENAMETOOLONG;
Dave Chinner43feeea2018-12-12 08:46:21 -0800195 ASSERT(pathlen > 0);
Dave Chinner19de7352013-04-03 16:11:18 +1100196
197 udqp = gdqp = NULL;
Zhi Yong Wu163467d2013-12-18 08:22:39 +0800198 prid = xfs_get_initial_prid(dp);
Dave Chinner19de7352013-04-03 16:11:18 +1100199
200 /*
201 * Make sure that we have allocated dquot(s) on disk.
202 */
Dwight Engen7aab1b22013-08-15 14:08:01 -0400203 error = xfs_qm_vop_dqalloc(dp,
204 xfs_kuid_to_uid(current_fsuid()),
205 xfs_kgid_to_gid(current_fsgid()), prid,
206 XFS_QMOPT_QUOTALL | XFS_QMOPT_INHERIT,
207 &udqp, &gdqp, &pdqp);
Dave Chinner19de7352013-04-03 16:11:18 +1100208 if (error)
Dave Chinner58c90472015-02-23 22:38:08 +1100209 return error;
Dave Chinner19de7352013-04-03 16:11:18 +1100210
Dave Chinner19de7352013-04-03 16:11:18 +1100211 /*
212 * The symlink will fit into the inode data fork?
213 * There can't be any attributes so we get the whole variable part.
214 */
215 if (pathlen <= XFS_LITINO(mp, dp->i_d.di_version))
216 fs_blocks = 0;
217 else
Dave Chinner321a9582013-05-27 16:38:20 +1000218 fs_blocks = xfs_symlink_blocks(mp, pathlen);
Dave Chinner19de7352013-04-03 16:11:18 +1100219 resblks = XFS_SYMLINK_SPACE_RES(mp, link_name->len, fs_blocks);
Christoph Hellwig253f4912016-04-06 09:19:55 +1000220
221 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_symlink, resblks, 0, 0, &tp);
Christoph Hellwig4906e212015-06-04 13:47:56 +1000222 if (error)
Christoph Hellwig253f4912016-04-06 09:19:55 +1000223 goto out_release_inode;
Dave Chinner19de7352013-04-03 16:11:18 +1100224
Christoph Hellwig65523212016-11-30 14:33:25 +1100225 xfs_ilock(dp, XFS_ILOCK_EXCL | XFS_ILOCK_PARENT);
Dave Chinner19de7352013-04-03 16:11:18 +1100226 unlock_dp_on_error = true;
227
228 /*
229 * Check whether the directory allows new symlinks or not.
230 */
231 if (dp->i_d.di_flags & XFS_DIFLAG_NOSYMLINKS) {
Dave Chinner24513372014-06-25 14:58:08 +1000232 error = -EPERM;
Dave Chinner58c90472015-02-23 22:38:08 +1100233 goto out_trans_cancel;
Dave Chinner19de7352013-04-03 16:11:18 +1100234 }
235
236 /*
237 * Reserve disk quota : blocks and inode.
238 */
Chandra Seetharaman92f8ff72013-07-11 00:00:40 -0500239 error = xfs_trans_reserve_quota(tp, mp, udqp, gdqp,
240 pdqp, resblks, 1, 0);
Dave Chinner19de7352013-04-03 16:11:18 +1100241 if (error)
Dave Chinner58c90472015-02-23 22:38:08 +1100242 goto out_trans_cancel;
Dave Chinner19de7352013-04-03 16:11:18 +1100243
244 /*
Dave Chinner19de7352013-04-03 16:11:18 +1100245 * Allocate an inode for the symlink.
246 */
247 error = xfs_dir_ialloc(&tp, dp, S_IFLNK | (mode & ~S_IFMT), 1, 0,
Chandan Rajendrac9590252018-04-02 15:47:43 -0700248 prid, &ip);
Dave Chinner58c90472015-02-23 22:38:08 +1100249 if (error)
250 goto out_trans_cancel;
Dave Chinner19de7352013-04-03 16:11:18 +1100251
252 /*
Dave Chinner58c90472015-02-23 22:38:08 +1100253 * Now we join the directory inode to the transaction. We do not do it
254 * earlier because xfs_dir_ialloc might commit the previous transaction
255 * (and release all the locks). An error from here on will result in
256 * the transaction cancel unlocking dp so don't do it explicitly in the
Dave Chinner19de7352013-04-03 16:11:18 +1100257 * error path.
258 */
Christoph Hellwig65523212016-11-30 14:33:25 +1100259 xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
Dave Chinner19de7352013-04-03 16:11:18 +1100260 unlock_dp_on_error = false;
261
262 /*
263 * Also attach the dquot(s) to it, if applicable.
264 */
Chandra Seetharaman92f8ff72013-07-11 00:00:40 -0500265 xfs_qm_vop_create_dqattach(tp, ip, udqp, gdqp, pdqp);
Dave Chinner19de7352013-04-03 16:11:18 +1100266
267 if (resblks)
268 resblks -= XFS_IALLOC_SPACE_RES(mp);
269 /*
270 * If the symlink will fit into the inode, write it inline.
271 */
272 if (pathlen <= XFS_IFORK_DSIZE(ip)) {
Christoph Hellwig143f4ae2016-04-06 07:41:43 +1000273 xfs_init_local_fork(ip, XFS_DATA_FORK, target_path, pathlen);
274
Dave Chinner19de7352013-04-03 16:11:18 +1100275 ip->i_d.di_size = pathlen;
Dave Chinner19de7352013-04-03 16:11:18 +1100276 ip->i_d.di_format = XFS_DINODE_FMT_LOCAL;
277 xfs_trans_log_inode(tp, ip, XFS_ILOG_DDATA | XFS_ILOG_CORE);
Dave Chinner19de7352013-04-03 16:11:18 +1100278 } else {
Dave Chinnerf948dd72013-04-03 16:11:19 +1100279 int offset;
280
Dave Chinner19de7352013-04-03 16:11:18 +1100281 first_fsb = 0;
282 nmaps = XFS_SYMLINK_MAPS;
283
284 error = xfs_bmapi_write(tp, ip, first_fsb, fs_blocks,
Brian Fostera7beabe2018-07-11 22:26:25 -0700285 XFS_BMAPI_METADATA, resblks, mval, &nmaps);
Dave Chinner19de7352013-04-03 16:11:18 +1100286 if (error)
Brian Fosterc8eac492018-07-24 13:43:13 -0700287 goto out_trans_cancel;
Dave Chinner19de7352013-04-03 16:11:18 +1100288
289 if (resblks)
290 resblks -= fs_blocks;
291 ip->i_d.di_size = pathlen;
292 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
293
294 cur_chunk = target_path;
Dave Chinnerf948dd72013-04-03 16:11:19 +1100295 offset = 0;
Dave Chinner19de7352013-04-03 16:11:18 +1100296 for (n = 0; n < nmaps; n++) {
Dave Chinner321a9582013-05-27 16:38:20 +1000297 char *buf;
Dave Chinnerf948dd72013-04-03 16:11:19 +1100298
Dave Chinner19de7352013-04-03 16:11:18 +1100299 d = XFS_FSB_TO_DADDR(mp, mval[n].br_startblock);
300 byte_cnt = XFS_FSB_TO_B(mp, mval[n].br_blockcount);
301 bp = xfs_trans_get_buf(tp, mp->m_ddev_targp, d,
302 BTOBB(byte_cnt), 0);
303 if (!bp) {
Dave Chinner24513372014-06-25 14:58:08 +1000304 error = -ENOMEM;
Brian Fosterc8eac492018-07-24 13:43:13 -0700305 goto out_trans_cancel;
Dave Chinner19de7352013-04-03 16:11:18 +1100306 }
Dave Chinnerf948dd72013-04-03 16:11:19 +1100307 bp->b_ops = &xfs_symlink_buf_ops;
308
309 byte_cnt = XFS_SYMLINK_BUF_SPACE(mp, byte_cnt);
Dave Chinner321a9582013-05-27 16:38:20 +1000310 byte_cnt = min(byte_cnt, pathlen);
Dave Chinner19de7352013-04-03 16:11:18 +1100311
Dave Chinnerf948dd72013-04-03 16:11:19 +1100312 buf = bp->b_addr;
313 buf += xfs_symlink_hdr_set(mp, ip->i_ino, offset,
314 byte_cnt, bp);
315
316 memcpy(buf, cur_chunk, byte_cnt);
317
Dave Chinner19de7352013-04-03 16:11:18 +1100318 cur_chunk += byte_cnt;
Dave Chinnerf948dd72013-04-03 16:11:19 +1100319 pathlen -= byte_cnt;
320 offset += byte_cnt;
Dave Chinner19de7352013-04-03 16:11:18 +1100321
Dave Chinnerdaf7b792013-09-02 10:32:00 +1000322 xfs_trans_buf_set_type(tp, bp, XFS_BLFT_SYMLINK_BUF);
Dave Chinnerf948dd72013-04-03 16:11:19 +1100323 xfs_trans_log_buf(tp, bp, 0, (buf + byte_cnt - 1) -
324 (char *)bp->b_addr);
Dave Chinner19de7352013-04-03 16:11:18 +1100325 }
Dave Chinner321a9582013-05-27 16:38:20 +1000326 ASSERT(pathlen == 0);
Dave Chinner19de7352013-04-03 16:11:18 +1100327 }
328
329 /*
330 * Create the directory entry for the symlink.
331 */
Brian Foster381eee62018-07-11 22:26:21 -0700332 error = xfs_dir_createname(tp, dp, link_name, ip->i_ino, resblks);
Dave Chinner19de7352013-04-03 16:11:18 +1100333 if (error)
Brian Fosterc8eac492018-07-24 13:43:13 -0700334 goto out_trans_cancel;
Dave Chinner19de7352013-04-03 16:11:18 +1100335 xfs_trans_ichgtime(tp, dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
336 xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
337
338 /*
339 * If this is a synchronous mount, make sure that the
340 * symlink transaction goes to disk before returning to
341 * the user.
342 */
343 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
344 xfs_trans_set_sync(tp);
345 }
346
Christoph Hellwig70393312015-06-04 13:48:08 +1000347 error = xfs_trans_commit(tp);
Dave Chinner58c90472015-02-23 22:38:08 +1100348 if (error)
349 goto out_release_inode;
350
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 *ipp = ip;
356 return 0;
357
Dave Chinner58c90472015-02-23 22:38:08 +1100358out_trans_cancel:
Christoph Hellwig4906e212015-06-04 13:47:56 +1000359 xfs_trans_cancel(tp);
Dave Chinner58c90472015-02-23 22:38:08 +1100360out_release_inode:
361 /*
362 * Wait until after the current transaction is aborted to finish the
363 * setup of the inode and release the inode. This prevents recursive
364 * transactions and deadlocks from xfs_inactive.
365 */
366 if (ip) {
367 xfs_finish_inode_setup(ip);
Darrick J. Wong44a87362018-07-25 12:52:32 -0700368 xfs_irele(ip);
Dave Chinner58c90472015-02-23 22:38:08 +1100369 }
370
Dave Chinner19de7352013-04-03 16:11:18 +1100371 xfs_qm_dqrele(udqp);
372 xfs_qm_dqrele(gdqp);
Chandra Seetharaman92f8ff72013-07-11 00:00:40 -0500373 xfs_qm_dqrele(pdqp);
Dave Chinner19de7352013-04-03 16:11:18 +1100374
375 if (unlock_dp_on_error)
Christoph Hellwig65523212016-11-30 14:33:25 +1100376 xfs_iunlock(dp, XFS_ILOCK_EXCL);
Dave Chinner19de7352013-04-03 16:11:18 +1100377 return error;
378}
379
380/*
381 * Free a symlink that has blocks associated with it.
Dave Chinner43feeea2018-12-12 08:46:21 -0800382 *
383 * Note: zero length symlinks are not allowed to exist. When we set the size to
384 * zero, also change it to a regular file so that it does not get written to
385 * disk as a zero length symlink. The inode is on the unlinked list already, so
386 * userspace cannot find this inode anymore, so this change is not user visible
387 * but allows us to catch corrupt zero-length symlinks in the verifiers.
Dave Chinner19de7352013-04-03 16:11:18 +1100388 */
Mark Tinguely725eb1e2013-06-17 15:35:57 -0500389STATIC int
Dave Chinner19de7352013-04-03 16:11:18 +1100390xfs_inactive_symlink_rmt(
Brian Foster36b21dd2013-09-20 11:06:09 -0400391 struct xfs_inode *ip)
Dave Chinner19de7352013-04-03 16:11:18 +1100392{
393 xfs_buf_t *bp;
Dave Chinner19de7352013-04-03 16:11:18 +1100394 int done;
395 int error;
Dave Chinner19de7352013-04-03 16:11:18 +1100396 int i;
397 xfs_mount_t *mp;
398 xfs_bmbt_irec_t mval[XFS_SYMLINK_MAPS];
399 int nmaps;
Dave Chinner19de7352013-04-03 16:11:18 +1100400 int size;
401 xfs_trans_t *tp;
402
Dave Chinner19de7352013-04-03 16:11:18 +1100403 mp = ip->i_mount;
Mark Tinguely725eb1e2013-06-17 15:35:57 -0500404 ASSERT(ip->i_df.if_flags & XFS_IFEXTENTS);
Dave Chinner19de7352013-04-03 16:11:18 +1100405 /*
406 * We're freeing a symlink that has some
407 * blocks allocated to it. Free the
408 * blocks here. We know that we've got
409 * either 1 or 2 extents and that we can
410 * free them all in one bunmapi call.
411 */
412 ASSERT(ip->i_d.di_nextents > 0 && ip->i_d.di_nextents <= 2);
413
Christoph Hellwig253f4912016-04-06 09:19:55 +1000414 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_itruncate, 0, 0, 0, &tp);
415 if (error)
Brian Foster36b21dd2013-09-20 11:06:09 -0400416 return error;
Brian Foster36b21dd2013-09-20 11:06:09 -0400417
418 xfs_ilock(ip, XFS_ILOCK_EXCL);
419 xfs_trans_ijoin(tp, ip, 0);
420
Dave Chinner19de7352013-04-03 16:11:18 +1100421 /*
Dave Chinner43feeea2018-12-12 08:46:21 -0800422 * Lock the inode, fix the size, turn it into a regular file and join it
423 * to the transaction. Hold it so in the normal path, we still have it
424 * locked for the second transaction. In the error paths we need it
Dave Chinner19de7352013-04-03 16:11:18 +1100425 * held so the cancel won't rele it, see below.
426 */
427 size = (int)ip->i_d.di_size;
428 ip->i_d.di_size = 0;
Dave Chinner43feeea2018-12-12 08:46:21 -0800429 VFS_I(ip)->i_mode = (VFS_I(ip)->i_mode & ~S_IFMT) | S_IFREG;
Dave Chinner19de7352013-04-03 16:11:18 +1100430 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
431 /*
432 * Find the block(s) so we can inval and unmap them.
433 */
434 done = 0;
Dave Chinner19de7352013-04-03 16:11:18 +1100435 nmaps = ARRAY_SIZE(mval);
Dave Chinnerf948dd72013-04-03 16:11:19 +1100436 error = xfs_bmapi_read(ip, 0, xfs_symlink_blocks(mp, size),
Dave Chinner19de7352013-04-03 16:11:18 +1100437 mval, &nmaps, 0);
438 if (error)
Brian Foster36b21dd2013-09-20 11:06:09 -0400439 goto error_trans_cancel;
Dave Chinner19de7352013-04-03 16:11:18 +1100440 /*
Dave Chinnerf948dd72013-04-03 16:11:19 +1100441 * Invalidate the block(s). No validation is done.
Dave Chinner19de7352013-04-03 16:11:18 +1100442 */
443 for (i = 0; i < nmaps; i++) {
444 bp = xfs_trans_get_buf(tp, mp->m_ddev_targp,
445 XFS_FSB_TO_DADDR(mp, mval[i].br_startblock),
446 XFS_FSB_TO_BB(mp, mval[i].br_blockcount), 0);
447 if (!bp) {
Dave Chinner24513372014-06-25 14:58:08 +1000448 error = -ENOMEM;
Brian Fosterc8eac492018-07-24 13:43:13 -0700449 goto error_trans_cancel;
Dave Chinner19de7352013-04-03 16:11:18 +1100450 }
451 xfs_trans_binval(tp, bp);
452 }
453 /*
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000454 * Unmap the dead block(s) to the dfops.
Dave Chinner19de7352013-04-03 16:11:18 +1100455 */
Brian Foster2af52842018-07-11 22:26:25 -0700456 error = xfs_bunmapi(tp, ip, 0, size, 0, nmaps, &done);
Brian Foster36b21dd2013-09-20 11:06:09 -0400457 if (error)
Brian Fosterc8eac492018-07-24 13:43:13 -0700458 goto error_trans_cancel;
Dave Chinner19de7352013-04-03 16:11:18 +1100459 ASSERT(done);
Dave Chinner3565b6602018-05-09 07:49:09 -0700460
Dave Chinner19de7352013-04-03 16:11:18 +1100461 /*
Brian Fosterc8eac492018-07-24 13:43:13 -0700462 * Commit the transaction. This first logs the EFI and the inode, then
463 * rolls and commits the transaction that frees the extents.
Dave Chinner19de7352013-04-03 16:11:18 +1100464 */
Dave Chinner3565b6602018-05-09 07:49:09 -0700465 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
Christoph Hellwig70393312015-06-04 13:48:08 +1000466 error = xfs_trans_commit(tp);
Dave Chinner19de7352013-04-03 16:11:18 +1100467 if (error) {
468 ASSERT(XFS_FORCED_SHUTDOWN(mp));
Brian Foster36b21dd2013-09-20 11:06:09 -0400469 goto error_unlock;
Dave Chinner19de7352013-04-03 16:11:18 +1100470 }
Dave Chinner19de7352013-04-03 16:11:18 +1100471
472 /*
473 * Remove the memory for extent descriptions (just bookkeeping).
474 */
475 if (ip->i_df.if_bytes)
476 xfs_idata_realloc(ip, -ip->i_df.if_bytes, XFS_DATA_FORK);
477 ASSERT(ip->i_df.if_bytes == 0);
Dave Chinner19de7352013-04-03 16:11:18 +1100478
Brian Foster36b21dd2013-09-20 11:06:09 -0400479 xfs_iunlock(ip, XFS_ILOCK_EXCL);
Dave Chinner19de7352013-04-03 16:11:18 +1100480 return 0;
481
Brian Foster36b21dd2013-09-20 11:06:09 -0400482error_trans_cancel:
Christoph Hellwig4906e212015-06-04 13:47:56 +1000483 xfs_trans_cancel(tp);
Brian Foster36b21dd2013-09-20 11:06:09 -0400484error_unlock:
485 xfs_iunlock(ip, XFS_ILOCK_EXCL);
Dave Chinner19de7352013-04-03 16:11:18 +1100486 return error;
487}
Mark Tinguely725eb1e2013-06-17 15:35:57 -0500488
489/*
490 * xfs_inactive_symlink - free a symlink
491 */
492int
493xfs_inactive_symlink(
Brian Foster36b21dd2013-09-20 11:06:09 -0400494 struct xfs_inode *ip)
Mark Tinguely725eb1e2013-06-17 15:35:57 -0500495{
496 struct xfs_mount *mp = ip->i_mount;
497 int pathlen;
498
499 trace_xfs_inactive_symlink(ip);
500
Mark Tinguely725eb1e2013-06-17 15:35:57 -0500501 if (XFS_FORCED_SHUTDOWN(mp))
Dave Chinner24513372014-06-25 14:58:08 +1000502 return -EIO;
Mark Tinguely725eb1e2013-06-17 15:35:57 -0500503
Brian Foster36b21dd2013-09-20 11:06:09 -0400504 xfs_ilock(ip, XFS_ILOCK_EXCL);
Mark Tinguely725eb1e2013-06-17 15:35:57 -0500505 pathlen = (int)ip->i_d.di_size;
Dave Chinner43feeea2018-12-12 08:46:21 -0800506 ASSERT(pathlen);
Mark Tinguely725eb1e2013-06-17 15:35:57 -0500507
Dave Chinner43feeea2018-12-12 08:46:21 -0800508 if (pathlen <= 0 || pathlen > XFS_SYMLINK_MAXLEN) {
Mark Tinguely725eb1e2013-06-17 15:35:57 -0500509 xfs_alert(mp, "%s: inode (0x%llx) bad symlink length (%d)",
510 __func__, (unsigned long long)ip->i_ino, pathlen);
Brian Foster36b21dd2013-09-20 11:06:09 -0400511 xfs_iunlock(ip, XFS_ILOCK_EXCL);
Mark Tinguely725eb1e2013-06-17 15:35:57 -0500512 ASSERT(0);
Dave Chinner24513372014-06-25 14:58:08 +1000513 return -EFSCORRUPTED;
Mark Tinguely725eb1e2013-06-17 15:35:57 -0500514 }
515
Dave Chinner43feeea2018-12-12 08:46:21 -0800516 /*
517 * Inline fork state gets removed by xfs_difree() so we have nothing to
518 * do here in that case.
519 */
Mark Tinguely725eb1e2013-06-17 15:35:57 -0500520 if (ip->i_df.if_flags & XFS_IFINLINE) {
Brian Foster36b21dd2013-09-20 11:06:09 -0400521 xfs_iunlock(ip, XFS_ILOCK_EXCL);
Mark Tinguely725eb1e2013-06-17 15:35:57 -0500522 return 0;
523 }
524
Brian Foster36b21dd2013-09-20 11:06:09 -0400525 xfs_iunlock(ip, XFS_ILOCK_EXCL);
526
Mark Tinguely725eb1e2013-06-17 15:35:57 -0500527 /* remove the remote symlink */
Brian Foster36b21dd2013-09-20 11:06:09 -0400528 return xfs_inactive_symlink_rmt(ip);
Mark Tinguely725eb1e2013-06-17 15:35:57 -0500529}