blob: a3e98c64b6e38b6f09685224fc0c8357b8394bc5 [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 Chinner19de7352013-04-03 16:11:18 +1100195
196 udqp = gdqp = NULL;
Zhi Yong Wu163467d2013-12-18 08:22:39 +0800197 prid = xfs_get_initial_prid(dp);
Dave Chinner19de7352013-04-03 16:11:18 +1100198
199 /*
200 * Make sure that we have allocated dquot(s) on disk.
201 */
Dwight Engen7aab1b22013-08-15 14:08:01 -0400202 error = xfs_qm_vop_dqalloc(dp,
203 xfs_kuid_to_uid(current_fsuid()),
204 xfs_kgid_to_gid(current_fsgid()), prid,
205 XFS_QMOPT_QUOTALL | XFS_QMOPT_INHERIT,
206 &udqp, &gdqp, &pdqp);
Dave Chinner19de7352013-04-03 16:11:18 +1100207 if (error)
Dave Chinner58c90472015-02-23 22:38:08 +1100208 return error;
Dave Chinner19de7352013-04-03 16:11:18 +1100209
Dave Chinner19de7352013-04-03 16:11:18 +1100210 /*
211 * The symlink will fit into the inode data fork?
212 * There can't be any attributes so we get the whole variable part.
213 */
214 if (pathlen <= XFS_LITINO(mp, dp->i_d.di_version))
215 fs_blocks = 0;
216 else
Dave Chinner321a9582013-05-27 16:38:20 +1000217 fs_blocks = xfs_symlink_blocks(mp, pathlen);
Dave Chinner19de7352013-04-03 16:11:18 +1100218 resblks = XFS_SYMLINK_SPACE_RES(mp, link_name->len, fs_blocks);
Christoph Hellwig253f4912016-04-06 09:19:55 +1000219
220 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_symlink, resblks, 0, 0, &tp);
Christoph Hellwig4906e212015-06-04 13:47:56 +1000221 if (error)
Christoph Hellwig253f4912016-04-06 09:19:55 +1000222 goto out_release_inode;
Dave Chinner19de7352013-04-03 16:11:18 +1100223
Christoph Hellwig65523212016-11-30 14:33:25 +1100224 xfs_ilock(dp, XFS_ILOCK_EXCL | XFS_ILOCK_PARENT);
Dave Chinner19de7352013-04-03 16:11:18 +1100225 unlock_dp_on_error = true;
226
227 /*
228 * Check whether the directory allows new symlinks or not.
229 */
230 if (dp->i_d.di_flags & XFS_DIFLAG_NOSYMLINKS) {
Dave Chinner24513372014-06-25 14:58:08 +1000231 error = -EPERM;
Dave Chinner58c90472015-02-23 22:38:08 +1100232 goto out_trans_cancel;
Dave Chinner19de7352013-04-03 16:11:18 +1100233 }
234
235 /*
236 * Reserve disk quota : blocks and inode.
237 */
Chandra Seetharaman92f8ff72013-07-11 00:00:40 -0500238 error = xfs_trans_reserve_quota(tp, mp, udqp, gdqp,
239 pdqp, resblks, 1, 0);
Dave Chinner19de7352013-04-03 16:11:18 +1100240 if (error)
Dave Chinner58c90472015-02-23 22:38:08 +1100241 goto out_trans_cancel;
Dave Chinner19de7352013-04-03 16:11:18 +1100242
243 /*
Dave Chinner19de7352013-04-03 16:11:18 +1100244 * Allocate an inode for the symlink.
245 */
246 error = xfs_dir_ialloc(&tp, dp, S_IFLNK | (mode & ~S_IFMT), 1, 0,
Chandan Rajendrac9590252018-04-02 15:47:43 -0700247 prid, &ip);
Dave Chinner58c90472015-02-23 22:38:08 +1100248 if (error)
249 goto out_trans_cancel;
Dave Chinner19de7352013-04-03 16:11:18 +1100250
251 /*
Dave Chinner58c90472015-02-23 22:38:08 +1100252 * Now we join the directory inode to the transaction. We do not do it
253 * earlier because xfs_dir_ialloc might commit the previous transaction
254 * (and release all the locks). An error from here on will result in
255 * the transaction cancel unlocking dp so don't do it explicitly in the
Dave Chinner19de7352013-04-03 16:11:18 +1100256 * error path.
257 */
Christoph Hellwig65523212016-11-30 14:33:25 +1100258 xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
Dave Chinner19de7352013-04-03 16:11:18 +1100259 unlock_dp_on_error = false;
260
261 /*
262 * Also attach the dquot(s) to it, if applicable.
263 */
Chandra Seetharaman92f8ff72013-07-11 00:00:40 -0500264 xfs_qm_vop_create_dqattach(tp, ip, udqp, gdqp, pdqp);
Dave Chinner19de7352013-04-03 16:11:18 +1100265
266 if (resblks)
267 resblks -= XFS_IALLOC_SPACE_RES(mp);
268 /*
269 * If the symlink will fit into the inode, write it inline.
270 */
271 if (pathlen <= XFS_IFORK_DSIZE(ip)) {
Christoph Hellwig143f4ae2016-04-06 07:41:43 +1000272 xfs_init_local_fork(ip, XFS_DATA_FORK, target_path, pathlen);
273
Dave Chinner19de7352013-04-03 16:11:18 +1100274 ip->i_d.di_size = pathlen;
Dave Chinner19de7352013-04-03 16:11:18 +1100275 ip->i_d.di_format = XFS_DINODE_FMT_LOCAL;
276 xfs_trans_log_inode(tp, ip, XFS_ILOG_DDATA | XFS_ILOG_CORE);
Dave Chinner19de7352013-04-03 16:11:18 +1100277 } else {
Dave Chinnerf948dd72013-04-03 16:11:19 +1100278 int offset;
279
Dave Chinner19de7352013-04-03 16:11:18 +1100280 first_fsb = 0;
281 nmaps = XFS_SYMLINK_MAPS;
282
283 error = xfs_bmapi_write(tp, ip, first_fsb, fs_blocks,
Brian Fostera7beabe2018-07-11 22:26:25 -0700284 XFS_BMAPI_METADATA, resblks, mval, &nmaps);
Dave Chinner19de7352013-04-03 16:11:18 +1100285 if (error)
Brian Fosterc8eac492018-07-24 13:43:13 -0700286 goto out_trans_cancel;
Dave Chinner19de7352013-04-03 16:11:18 +1100287
288 if (resblks)
289 resblks -= fs_blocks;
290 ip->i_d.di_size = pathlen;
291 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
292
293 cur_chunk = target_path;
Dave Chinnerf948dd72013-04-03 16:11:19 +1100294 offset = 0;
Dave Chinner19de7352013-04-03 16:11:18 +1100295 for (n = 0; n < nmaps; n++) {
Dave Chinner321a9582013-05-27 16:38:20 +1000296 char *buf;
Dave Chinnerf948dd72013-04-03 16:11:19 +1100297
Dave Chinner19de7352013-04-03 16:11:18 +1100298 d = XFS_FSB_TO_DADDR(mp, mval[n].br_startblock);
299 byte_cnt = XFS_FSB_TO_B(mp, mval[n].br_blockcount);
300 bp = xfs_trans_get_buf(tp, mp->m_ddev_targp, d,
301 BTOBB(byte_cnt), 0);
302 if (!bp) {
Dave Chinner24513372014-06-25 14:58:08 +1000303 error = -ENOMEM;
Brian Fosterc8eac492018-07-24 13:43:13 -0700304 goto out_trans_cancel;
Dave Chinner19de7352013-04-03 16:11:18 +1100305 }
Dave Chinnerf948dd72013-04-03 16:11:19 +1100306 bp->b_ops = &xfs_symlink_buf_ops;
307
308 byte_cnt = XFS_SYMLINK_BUF_SPACE(mp, byte_cnt);
Dave Chinner321a9582013-05-27 16:38:20 +1000309 byte_cnt = min(byte_cnt, pathlen);
Dave Chinner19de7352013-04-03 16:11:18 +1100310
Dave Chinnerf948dd72013-04-03 16:11:19 +1100311 buf = bp->b_addr;
312 buf += xfs_symlink_hdr_set(mp, ip->i_ino, offset,
313 byte_cnt, bp);
314
315 memcpy(buf, cur_chunk, byte_cnt);
316
Dave Chinner19de7352013-04-03 16:11:18 +1100317 cur_chunk += byte_cnt;
Dave Chinnerf948dd72013-04-03 16:11:19 +1100318 pathlen -= byte_cnt;
319 offset += byte_cnt;
Dave Chinner19de7352013-04-03 16:11:18 +1100320
Dave Chinnerdaf7b792013-09-02 10:32:00 +1000321 xfs_trans_buf_set_type(tp, bp, XFS_BLFT_SYMLINK_BUF);
Dave Chinnerf948dd72013-04-03 16:11:19 +1100322 xfs_trans_log_buf(tp, bp, 0, (buf + byte_cnt - 1) -
323 (char *)bp->b_addr);
Dave Chinner19de7352013-04-03 16:11:18 +1100324 }
Dave Chinner321a9582013-05-27 16:38:20 +1000325 ASSERT(pathlen == 0);
Dave Chinner19de7352013-04-03 16:11:18 +1100326 }
327
328 /*
329 * Create the directory entry for the symlink.
330 */
Brian Foster381eee62018-07-11 22:26:21 -0700331 error = xfs_dir_createname(tp, dp, link_name, ip->i_ino, resblks);
Dave Chinner19de7352013-04-03 16:11:18 +1100332 if (error)
Brian Fosterc8eac492018-07-24 13:43:13 -0700333 goto out_trans_cancel;
Dave Chinner19de7352013-04-03 16:11:18 +1100334 xfs_trans_ichgtime(tp, dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
335 xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
336
337 /*
338 * If this is a synchronous mount, make sure that the
339 * symlink transaction goes to disk before returning to
340 * the user.
341 */
342 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
343 xfs_trans_set_sync(tp);
344 }
345
Christoph Hellwig70393312015-06-04 13:48:08 +1000346 error = xfs_trans_commit(tp);
Dave Chinner58c90472015-02-23 22:38:08 +1100347 if (error)
348 goto out_release_inode;
349
Dave Chinner19de7352013-04-03 16:11:18 +1100350 xfs_qm_dqrele(udqp);
351 xfs_qm_dqrele(gdqp);
Chandra Seetharaman92f8ff72013-07-11 00:00:40 -0500352 xfs_qm_dqrele(pdqp);
Dave Chinner19de7352013-04-03 16:11:18 +1100353
354 *ipp = ip;
355 return 0;
356
Dave Chinner58c90472015-02-23 22:38:08 +1100357out_trans_cancel:
Christoph Hellwig4906e212015-06-04 13:47:56 +1000358 xfs_trans_cancel(tp);
Dave Chinner58c90472015-02-23 22:38:08 +1100359out_release_inode:
360 /*
361 * Wait until after the current transaction is aborted to finish the
362 * setup of the inode and release the inode. This prevents recursive
363 * transactions and deadlocks from xfs_inactive.
364 */
365 if (ip) {
366 xfs_finish_inode_setup(ip);
Darrick J. Wong44a87362018-07-25 12:52:32 -0700367 xfs_irele(ip);
Dave Chinner58c90472015-02-23 22:38:08 +1100368 }
369
Dave Chinner19de7352013-04-03 16:11:18 +1100370 xfs_qm_dqrele(udqp);
371 xfs_qm_dqrele(gdqp);
Chandra Seetharaman92f8ff72013-07-11 00:00:40 -0500372 xfs_qm_dqrele(pdqp);
Dave Chinner19de7352013-04-03 16:11:18 +1100373
374 if (unlock_dp_on_error)
Christoph Hellwig65523212016-11-30 14:33:25 +1100375 xfs_iunlock(dp, XFS_ILOCK_EXCL);
Dave Chinner19de7352013-04-03 16:11:18 +1100376 return error;
377}
378
379/*
380 * Free a symlink that has blocks associated with it.
381 */
Mark Tinguely725eb1e2013-06-17 15:35:57 -0500382STATIC int
Dave Chinner19de7352013-04-03 16:11:18 +1100383xfs_inactive_symlink_rmt(
Brian Foster36b21dd2013-09-20 11:06:09 -0400384 struct xfs_inode *ip)
Dave Chinner19de7352013-04-03 16:11:18 +1100385{
386 xfs_buf_t *bp;
Dave Chinner19de7352013-04-03 16:11:18 +1100387 int done;
388 int error;
Dave Chinner19de7352013-04-03 16:11:18 +1100389 int i;
390 xfs_mount_t *mp;
391 xfs_bmbt_irec_t mval[XFS_SYMLINK_MAPS];
392 int nmaps;
Dave Chinner19de7352013-04-03 16:11:18 +1100393 int size;
394 xfs_trans_t *tp;
395
Dave Chinner19de7352013-04-03 16:11:18 +1100396 mp = ip->i_mount;
Mark Tinguely725eb1e2013-06-17 15:35:57 -0500397 ASSERT(ip->i_df.if_flags & XFS_IFEXTENTS);
Dave Chinner19de7352013-04-03 16:11:18 +1100398 /*
399 * We're freeing a symlink that has some
400 * blocks allocated to it. Free the
401 * blocks here. We know that we've got
402 * either 1 or 2 extents and that we can
403 * free them all in one bunmapi call.
404 */
405 ASSERT(ip->i_d.di_nextents > 0 && ip->i_d.di_nextents <= 2);
406
Christoph Hellwig253f4912016-04-06 09:19:55 +1000407 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_itruncate, 0, 0, 0, &tp);
408 if (error)
Brian Foster36b21dd2013-09-20 11:06:09 -0400409 return error;
Brian Foster36b21dd2013-09-20 11:06:09 -0400410
411 xfs_ilock(ip, XFS_ILOCK_EXCL);
412 xfs_trans_ijoin(tp, ip, 0);
413
Dave Chinner19de7352013-04-03 16:11:18 +1100414 /*
415 * Lock the inode, fix the size, and join it to the transaction.
416 * Hold it so in the normal path, we still have it locked for
417 * the second transaction. In the error paths we need it
418 * held so the cancel won't rele it, see below.
419 */
420 size = (int)ip->i_d.di_size;
421 ip->i_d.di_size = 0;
422 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
423 /*
424 * Find the block(s) so we can inval and unmap them.
425 */
426 done = 0;
Dave Chinner19de7352013-04-03 16:11:18 +1100427 nmaps = ARRAY_SIZE(mval);
Dave Chinnerf948dd72013-04-03 16:11:19 +1100428 error = xfs_bmapi_read(ip, 0, xfs_symlink_blocks(mp, size),
Dave Chinner19de7352013-04-03 16:11:18 +1100429 mval, &nmaps, 0);
430 if (error)
Brian Foster36b21dd2013-09-20 11:06:09 -0400431 goto error_trans_cancel;
Dave Chinner19de7352013-04-03 16:11:18 +1100432 /*
Dave Chinnerf948dd72013-04-03 16:11:19 +1100433 * Invalidate the block(s). No validation is done.
Dave Chinner19de7352013-04-03 16:11:18 +1100434 */
435 for (i = 0; i < nmaps; i++) {
436 bp = xfs_trans_get_buf(tp, mp->m_ddev_targp,
437 XFS_FSB_TO_DADDR(mp, mval[i].br_startblock),
438 XFS_FSB_TO_BB(mp, mval[i].br_blockcount), 0);
439 if (!bp) {
Dave Chinner24513372014-06-25 14:58:08 +1000440 error = -ENOMEM;
Brian Fosterc8eac492018-07-24 13:43:13 -0700441 goto error_trans_cancel;
Dave Chinner19de7352013-04-03 16:11:18 +1100442 }
443 xfs_trans_binval(tp, bp);
444 }
445 /*
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000446 * Unmap the dead block(s) to the dfops.
Dave Chinner19de7352013-04-03 16:11:18 +1100447 */
Brian Foster2af52842018-07-11 22:26:25 -0700448 error = xfs_bunmapi(tp, ip, 0, size, 0, nmaps, &done);
Brian Foster36b21dd2013-09-20 11:06:09 -0400449 if (error)
Brian Fosterc8eac492018-07-24 13:43:13 -0700450 goto error_trans_cancel;
Dave Chinner19de7352013-04-03 16:11:18 +1100451 ASSERT(done);
Dave Chinner3565b6602018-05-09 07:49:09 -0700452
Dave Chinner19de7352013-04-03 16:11:18 +1100453 /*
Brian Fosterc8eac492018-07-24 13:43:13 -0700454 * Commit the transaction. This first logs the EFI and the inode, then
455 * rolls and commits the transaction that frees the extents.
Dave Chinner19de7352013-04-03 16:11:18 +1100456 */
Dave Chinner3565b6602018-05-09 07:49:09 -0700457 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
Christoph Hellwig70393312015-06-04 13:48:08 +1000458 error = xfs_trans_commit(tp);
Dave Chinner19de7352013-04-03 16:11:18 +1100459 if (error) {
460 ASSERT(XFS_FORCED_SHUTDOWN(mp));
Brian Foster36b21dd2013-09-20 11:06:09 -0400461 goto error_unlock;
Dave Chinner19de7352013-04-03 16:11:18 +1100462 }
Dave Chinner19de7352013-04-03 16:11:18 +1100463
464 /*
465 * Remove the memory for extent descriptions (just bookkeeping).
466 */
467 if (ip->i_df.if_bytes)
468 xfs_idata_realloc(ip, -ip->i_df.if_bytes, XFS_DATA_FORK);
469 ASSERT(ip->i_df.if_bytes == 0);
Dave Chinner19de7352013-04-03 16:11:18 +1100470
Brian Foster36b21dd2013-09-20 11:06:09 -0400471 xfs_iunlock(ip, XFS_ILOCK_EXCL);
Dave Chinner19de7352013-04-03 16:11:18 +1100472 return 0;
473
Brian Foster36b21dd2013-09-20 11:06:09 -0400474error_trans_cancel:
Christoph Hellwig4906e212015-06-04 13:47:56 +1000475 xfs_trans_cancel(tp);
Brian Foster36b21dd2013-09-20 11:06:09 -0400476error_unlock:
477 xfs_iunlock(ip, XFS_ILOCK_EXCL);
Dave Chinner19de7352013-04-03 16:11:18 +1100478 return error;
479}
Mark Tinguely725eb1e2013-06-17 15:35:57 -0500480
481/*
482 * xfs_inactive_symlink - free a symlink
483 */
484int
485xfs_inactive_symlink(
Brian Foster36b21dd2013-09-20 11:06:09 -0400486 struct xfs_inode *ip)
Mark Tinguely725eb1e2013-06-17 15:35:57 -0500487{
488 struct xfs_mount *mp = ip->i_mount;
489 int pathlen;
490
491 trace_xfs_inactive_symlink(ip);
492
Mark Tinguely725eb1e2013-06-17 15:35:57 -0500493 if (XFS_FORCED_SHUTDOWN(mp))
Dave Chinner24513372014-06-25 14:58:08 +1000494 return -EIO;
Mark Tinguely725eb1e2013-06-17 15:35:57 -0500495
Brian Foster36b21dd2013-09-20 11:06:09 -0400496 xfs_ilock(ip, XFS_ILOCK_EXCL);
497
Mark Tinguely725eb1e2013-06-17 15:35:57 -0500498 /*
499 * Zero length symlinks _can_ exist.
500 */
501 pathlen = (int)ip->i_d.di_size;
Brian Foster36b21dd2013-09-20 11:06:09 -0400502 if (!pathlen) {
503 xfs_iunlock(ip, XFS_ILOCK_EXCL);
Mark Tinguely725eb1e2013-06-17 15:35:57 -0500504 return 0;
Brian Foster36b21dd2013-09-20 11:06:09 -0400505 }
Mark Tinguely725eb1e2013-06-17 15:35:57 -0500506
Darrick J. Wong6eb0b8d2017-07-07 08:37:26 -0700507 if (pathlen < 0 || pathlen > XFS_SYMLINK_MAXLEN) {
Mark Tinguely725eb1e2013-06-17 15:35:57 -0500508 xfs_alert(mp, "%s: inode (0x%llx) bad symlink length (%d)",
509 __func__, (unsigned long long)ip->i_ino, pathlen);
Brian Foster36b21dd2013-09-20 11:06:09 -0400510 xfs_iunlock(ip, XFS_ILOCK_EXCL);
Mark Tinguely725eb1e2013-06-17 15:35:57 -0500511 ASSERT(0);
Dave Chinner24513372014-06-25 14:58:08 +1000512 return -EFSCORRUPTED;
Mark Tinguely725eb1e2013-06-17 15:35:57 -0500513 }
514
515 if (ip->i_df.if_flags & XFS_IFINLINE) {
Brian Foster36b21dd2013-09-20 11:06:09 -0400516 if (ip->i_df.if_bytes > 0)
Mark Tinguely725eb1e2013-06-17 15:35:57 -0500517 xfs_idata_realloc(ip, -(ip->i_df.if_bytes),
518 XFS_DATA_FORK);
Brian Foster36b21dd2013-09-20 11:06:09 -0400519 xfs_iunlock(ip, XFS_ILOCK_EXCL);
Mark Tinguely725eb1e2013-06-17 15:35:57 -0500520 ASSERT(ip->i_df.if_bytes == 0);
521 return 0;
522 }
523
Brian Foster36b21dd2013-09-20 11:06:09 -0400524 xfs_iunlock(ip, XFS_ILOCK_EXCL);
525
Mark Tinguely725eb1e2013-06-17 15:35:57 -0500526 /* remove the remote symlink */
Brian Foster36b21dd2013-09-20 11:06:09 -0400527 return xfs_inactive_symlink_rmt(ip);
Mark Tinguely725eb1e2013-06-17 15:35:57 -0500528}