blob: 3783afcb68d24a2ece0279a8ee11e0bd6dec5404 [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;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000166 struct xfs_defer_ops dfops;
Dave Chinner19de7352013-04-03 16:11:18 +1100167 xfs_fsblock_t first_block;
Dave Chinner58c90472015-02-23 22:38:08 +1100168 bool unlock_dp_on_error = false;
Dave Chinner19de7352013-04-03 16:11:18 +1100169 xfs_fileoff_t first_fsb;
170 xfs_filblks_t fs_blocks;
171 int nmaps;
Dave Chinnerf948dd72013-04-03 16:11:19 +1100172 struct xfs_bmbt_irec mval[XFS_SYMLINK_MAPS];
Dave Chinner19de7352013-04-03 16:11:18 +1100173 xfs_daddr_t d;
174 const char *cur_chunk;
175 int byte_cnt;
176 int n;
177 xfs_buf_t *bp;
178 prid_t prid;
Chandra Seetharaman113a5682013-06-27 17:25:07 -0500179 struct xfs_dquot *udqp = NULL;
180 struct xfs_dquot *gdqp = NULL;
Chandra Seetharaman92f8ff72013-07-11 00:00:40 -0500181 struct xfs_dquot *pdqp = NULL;
Dave Chinner19de7352013-04-03 16:11:18 +1100182 uint resblks;
183
184 *ipp = NULL;
Dave Chinner19de7352013-04-03 16:11:18 +1100185
186 trace_xfs_symlink(dp, link_name);
187
188 if (XFS_FORCED_SHUTDOWN(mp))
Dave Chinner24513372014-06-25 14:58:08 +1000189 return -EIO;
Dave Chinner19de7352013-04-03 16:11:18 +1100190
191 /*
192 * Check component lengths of the target path name.
193 */
194 pathlen = strlen(target_path);
Darrick J. Wong6eb0b8d2017-07-07 08:37:26 -0700195 if (pathlen >= XFS_SYMLINK_MAXLEN) /* total string too long */
Dave Chinner24513372014-06-25 14:58:08 +1000196 return -ENAMETOOLONG;
Dave Chinner19de7352013-04-03 16:11:18 +1100197
198 udqp = gdqp = NULL;
Zhi Yong Wu163467d2013-12-18 08:22:39 +0800199 prid = xfs_get_initial_prid(dp);
Dave Chinner19de7352013-04-03 16:11:18 +1100200
201 /*
202 * Make sure that we have allocated dquot(s) on disk.
203 */
Dwight Engen7aab1b22013-08-15 14:08:01 -0400204 error = xfs_qm_vop_dqalloc(dp,
205 xfs_kuid_to_uid(current_fsuid()),
206 xfs_kgid_to_gid(current_fsgid()), prid,
207 XFS_QMOPT_QUOTALL | XFS_QMOPT_INHERIT,
208 &udqp, &gdqp, &pdqp);
Dave Chinner19de7352013-04-03 16:11:18 +1100209 if (error)
Dave Chinner58c90472015-02-23 22:38:08 +1100210 return error;
Dave Chinner19de7352013-04-03 16:11:18 +1100211
Dave Chinner19de7352013-04-03 16:11:18 +1100212 /*
213 * The symlink will fit into the inode data fork?
214 * There can't be any attributes so we get the whole variable part.
215 */
216 if (pathlen <= XFS_LITINO(mp, dp->i_d.di_version))
217 fs_blocks = 0;
218 else
Dave Chinner321a9582013-05-27 16:38:20 +1000219 fs_blocks = xfs_symlink_blocks(mp, pathlen);
Dave Chinner19de7352013-04-03 16:11:18 +1100220 resblks = XFS_SYMLINK_SPACE_RES(mp, link_name->len, fs_blocks);
Christoph Hellwig253f4912016-04-06 09:19:55 +1000221
222 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_symlink, resblks, 0, 0, &tp);
Christoph Hellwig4906e212015-06-04 13:47:56 +1000223 if (error)
Christoph Hellwig253f4912016-04-06 09:19:55 +1000224 goto out_release_inode;
Dave Chinner19de7352013-04-03 16:11:18 +1100225
Christoph Hellwig65523212016-11-30 14:33:25 +1100226 xfs_ilock(dp, XFS_ILOCK_EXCL | XFS_ILOCK_PARENT);
Dave Chinner19de7352013-04-03 16:11:18 +1100227 unlock_dp_on_error = true;
228
229 /*
230 * Check whether the directory allows new symlinks or not.
231 */
232 if (dp->i_d.di_flags & XFS_DIFLAG_NOSYMLINKS) {
Dave Chinner24513372014-06-25 14:58:08 +1000233 error = -EPERM;
Dave Chinner58c90472015-02-23 22:38:08 +1100234 goto out_trans_cancel;
Dave Chinner19de7352013-04-03 16:11:18 +1100235 }
236
237 /*
238 * Reserve disk quota : blocks and inode.
239 */
Chandra Seetharaman92f8ff72013-07-11 00:00:40 -0500240 error = xfs_trans_reserve_quota(tp, mp, udqp, gdqp,
241 pdqp, resblks, 1, 0);
Dave Chinner19de7352013-04-03 16:11:18 +1100242 if (error)
Dave Chinner58c90472015-02-23 22:38:08 +1100243 goto out_trans_cancel;
Dave Chinner19de7352013-04-03 16:11:18 +1100244
245 /*
Dave Chinner19de7352013-04-03 16:11:18 +1100246 * Initialize the bmap freelist prior to calling either
247 * bmapi or the directory create code.
248 */
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000249 xfs_defer_init(&dfops, &first_block);
Brian Foster8b922f02018-05-07 17:38:48 -0700250 tp->t_agfl_dfops = &dfops;
Dave Chinner19de7352013-04-03 16:11:18 +1100251
252 /*
253 * Allocate an inode for the symlink.
254 */
255 error = xfs_dir_ialloc(&tp, dp, S_IFLNK | (mode & ~S_IFMT), 1, 0,
Chandan Rajendrac9590252018-04-02 15:47:43 -0700256 prid, &ip);
Dave Chinner58c90472015-02-23 22:38:08 +1100257 if (error)
258 goto out_trans_cancel;
Dave Chinner19de7352013-04-03 16:11:18 +1100259
260 /*
Dave Chinner58c90472015-02-23 22:38:08 +1100261 * Now we join the directory inode to the transaction. We do not do it
262 * earlier because xfs_dir_ialloc might commit the previous transaction
263 * (and release all the locks). An error from here on will result in
264 * the transaction cancel unlocking dp so don't do it explicitly in the
Dave Chinner19de7352013-04-03 16:11:18 +1100265 * error path.
266 */
Christoph Hellwig65523212016-11-30 14:33:25 +1100267 xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
Dave Chinner19de7352013-04-03 16:11:18 +1100268 unlock_dp_on_error = false;
269
270 /*
271 * Also attach the dquot(s) to it, if applicable.
272 */
Chandra Seetharaman92f8ff72013-07-11 00:00:40 -0500273 xfs_qm_vop_create_dqattach(tp, ip, udqp, gdqp, pdqp);
Dave Chinner19de7352013-04-03 16:11:18 +1100274
275 if (resblks)
276 resblks -= XFS_IALLOC_SPACE_RES(mp);
277 /*
278 * If the symlink will fit into the inode, write it inline.
279 */
280 if (pathlen <= XFS_IFORK_DSIZE(ip)) {
Christoph Hellwig143f4ae2016-04-06 07:41:43 +1000281 xfs_init_local_fork(ip, XFS_DATA_FORK, target_path, pathlen);
282
Dave Chinner19de7352013-04-03 16:11:18 +1100283 ip->i_d.di_size = pathlen;
Dave Chinner19de7352013-04-03 16:11:18 +1100284 ip->i_d.di_format = XFS_DINODE_FMT_LOCAL;
285 xfs_trans_log_inode(tp, ip, XFS_ILOG_DDATA | XFS_ILOG_CORE);
Dave Chinner19de7352013-04-03 16:11:18 +1100286 } else {
Dave Chinnerf948dd72013-04-03 16:11:19 +1100287 int offset;
288
Dave Chinner19de7352013-04-03 16:11:18 +1100289 first_fsb = 0;
290 nmaps = XFS_SYMLINK_MAPS;
291
292 error = xfs_bmapi_write(tp, ip, first_fsb, fs_blocks,
293 XFS_BMAPI_METADATA, &first_block, resblks,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000294 mval, &nmaps, &dfops);
Dave Chinner19de7352013-04-03 16:11:18 +1100295 if (error)
Dave Chinner58c90472015-02-23 22:38:08 +1100296 goto out_bmap_cancel;
Dave Chinner19de7352013-04-03 16:11:18 +1100297
298 if (resblks)
299 resblks -= fs_blocks;
300 ip->i_d.di_size = pathlen;
301 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
302
303 cur_chunk = target_path;
Dave Chinnerf948dd72013-04-03 16:11:19 +1100304 offset = 0;
Dave Chinner19de7352013-04-03 16:11:18 +1100305 for (n = 0; n < nmaps; n++) {
Dave Chinner321a9582013-05-27 16:38:20 +1000306 char *buf;
Dave Chinnerf948dd72013-04-03 16:11:19 +1100307
Dave Chinner19de7352013-04-03 16:11:18 +1100308 d = XFS_FSB_TO_DADDR(mp, mval[n].br_startblock);
309 byte_cnt = XFS_FSB_TO_B(mp, mval[n].br_blockcount);
310 bp = xfs_trans_get_buf(tp, mp->m_ddev_targp, d,
311 BTOBB(byte_cnt), 0);
312 if (!bp) {
Dave Chinner24513372014-06-25 14:58:08 +1000313 error = -ENOMEM;
Dave Chinner58c90472015-02-23 22:38:08 +1100314 goto out_bmap_cancel;
Dave Chinner19de7352013-04-03 16:11:18 +1100315 }
Dave Chinnerf948dd72013-04-03 16:11:19 +1100316 bp->b_ops = &xfs_symlink_buf_ops;
317
318 byte_cnt = XFS_SYMLINK_BUF_SPACE(mp, byte_cnt);
Dave Chinner321a9582013-05-27 16:38:20 +1000319 byte_cnt = min(byte_cnt, pathlen);
Dave Chinner19de7352013-04-03 16:11:18 +1100320
Dave Chinnerf948dd72013-04-03 16:11:19 +1100321 buf = bp->b_addr;
322 buf += xfs_symlink_hdr_set(mp, ip->i_ino, offset,
323 byte_cnt, bp);
324
325 memcpy(buf, cur_chunk, byte_cnt);
326
Dave Chinner19de7352013-04-03 16:11:18 +1100327 cur_chunk += byte_cnt;
Dave Chinnerf948dd72013-04-03 16:11:19 +1100328 pathlen -= byte_cnt;
329 offset += byte_cnt;
Dave Chinner19de7352013-04-03 16:11:18 +1100330
Dave Chinnerdaf7b792013-09-02 10:32:00 +1000331 xfs_trans_buf_set_type(tp, bp, XFS_BLFT_SYMLINK_BUF);
Dave Chinnerf948dd72013-04-03 16:11:19 +1100332 xfs_trans_log_buf(tp, bp, 0, (buf + byte_cnt - 1) -
333 (char *)bp->b_addr);
Dave Chinner19de7352013-04-03 16:11:18 +1100334 }
Dave Chinner321a9582013-05-27 16:38:20 +1000335 ASSERT(pathlen == 0);
Dave Chinner19de7352013-04-03 16:11:18 +1100336 }
337
338 /*
339 * Create the directory entry for the symlink.
340 */
341 error = xfs_dir_createname(tp, dp, link_name, ip->i_ino,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000342 &first_block, &dfops, resblks);
Dave Chinner19de7352013-04-03 16:11:18 +1100343 if (error)
Dave Chinner58c90472015-02-23 22:38:08 +1100344 goto out_bmap_cancel;
Dave Chinner19de7352013-04-03 16:11:18 +1100345 xfs_trans_ichgtime(tp, dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
346 xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
347
348 /*
349 * If this is a synchronous mount, make sure that the
350 * symlink transaction goes to disk before returning to
351 * the user.
352 */
353 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
354 xfs_trans_set_sync(tp);
355 }
356
Christoph Hellwig8ad7c6292017-08-28 10:21:04 -0700357 error = xfs_defer_finish(&tp, &dfops);
Dave Chinner58c90472015-02-23 22:38:08 +1100358 if (error)
359 goto out_bmap_cancel;
360
Christoph Hellwig70393312015-06-04 13:48:08 +1000361 error = xfs_trans_commit(tp);
Dave Chinner58c90472015-02-23 22:38:08 +1100362 if (error)
363 goto out_release_inode;
364
Dave Chinner19de7352013-04-03 16:11:18 +1100365 xfs_qm_dqrele(udqp);
366 xfs_qm_dqrele(gdqp);
Chandra Seetharaman92f8ff72013-07-11 00:00:40 -0500367 xfs_qm_dqrele(pdqp);
Dave Chinner19de7352013-04-03 16:11:18 +1100368
369 *ipp = ip;
370 return 0;
371
Dave Chinner58c90472015-02-23 22:38:08 +1100372out_bmap_cancel:
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000373 xfs_defer_cancel(&dfops);
Dave Chinner58c90472015-02-23 22:38:08 +1100374out_trans_cancel:
Christoph Hellwig4906e212015-06-04 13:47:56 +1000375 xfs_trans_cancel(tp);
Dave Chinner58c90472015-02-23 22:38:08 +1100376out_release_inode:
377 /*
378 * Wait until after the current transaction is aborted to finish the
379 * setup of the inode and release the inode. This prevents recursive
380 * transactions and deadlocks from xfs_inactive.
381 */
382 if (ip) {
383 xfs_finish_inode_setup(ip);
384 IRELE(ip);
385 }
386
Dave Chinner19de7352013-04-03 16:11:18 +1100387 xfs_qm_dqrele(udqp);
388 xfs_qm_dqrele(gdqp);
Chandra Seetharaman92f8ff72013-07-11 00:00:40 -0500389 xfs_qm_dqrele(pdqp);
Dave Chinner19de7352013-04-03 16:11:18 +1100390
391 if (unlock_dp_on_error)
Christoph Hellwig65523212016-11-30 14:33:25 +1100392 xfs_iunlock(dp, XFS_ILOCK_EXCL);
Dave Chinner19de7352013-04-03 16:11:18 +1100393 return error;
394}
395
396/*
397 * Free a symlink that has blocks associated with it.
398 */
Mark Tinguely725eb1e2013-06-17 15:35:57 -0500399STATIC int
Dave Chinner19de7352013-04-03 16:11:18 +1100400xfs_inactive_symlink_rmt(
Brian Foster36b21dd2013-09-20 11:06:09 -0400401 struct xfs_inode *ip)
Dave Chinner19de7352013-04-03 16:11:18 +1100402{
403 xfs_buf_t *bp;
Dave Chinner19de7352013-04-03 16:11:18 +1100404 int done;
405 int error;
406 xfs_fsblock_t first_block;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000407 struct xfs_defer_ops dfops;
Dave Chinner19de7352013-04-03 16:11:18 +1100408 int i;
409 xfs_mount_t *mp;
410 xfs_bmbt_irec_t mval[XFS_SYMLINK_MAPS];
411 int nmaps;
Dave Chinner19de7352013-04-03 16:11:18 +1100412 int size;
413 xfs_trans_t *tp;
414
Dave Chinner19de7352013-04-03 16:11:18 +1100415 mp = ip->i_mount;
Mark Tinguely725eb1e2013-06-17 15:35:57 -0500416 ASSERT(ip->i_df.if_flags & XFS_IFEXTENTS);
Dave Chinner19de7352013-04-03 16:11:18 +1100417 /*
418 * We're freeing a symlink that has some
419 * blocks allocated to it. Free the
420 * blocks here. We know that we've got
421 * either 1 or 2 extents and that we can
422 * free them all in one bunmapi call.
423 */
424 ASSERT(ip->i_d.di_nextents > 0 && ip->i_d.di_nextents <= 2);
425
Christoph Hellwig253f4912016-04-06 09:19:55 +1000426 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_itruncate, 0, 0, 0, &tp);
427 if (error)
Brian Foster36b21dd2013-09-20 11:06:09 -0400428 return error;
Brian Foster36b21dd2013-09-20 11:06:09 -0400429
430 xfs_ilock(ip, XFS_ILOCK_EXCL);
431 xfs_trans_ijoin(tp, ip, 0);
432
Dave Chinner19de7352013-04-03 16:11:18 +1100433 /*
434 * Lock the inode, fix the size, and join it to the transaction.
435 * Hold it so in the normal path, we still have it locked for
436 * the second transaction. In the error paths we need it
437 * held so the cancel won't rele it, see below.
438 */
439 size = (int)ip->i_d.di_size;
440 ip->i_d.di_size = 0;
441 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
442 /*
443 * Find the block(s) so we can inval and unmap them.
444 */
445 done = 0;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000446 xfs_defer_init(&dfops, &first_block);
Dave Chinner19de7352013-04-03 16:11:18 +1100447 nmaps = ARRAY_SIZE(mval);
Dave Chinnerf948dd72013-04-03 16:11:19 +1100448 error = xfs_bmapi_read(ip, 0, xfs_symlink_blocks(mp, size),
Dave Chinner19de7352013-04-03 16:11:18 +1100449 mval, &nmaps, 0);
450 if (error)
Brian Foster36b21dd2013-09-20 11:06:09 -0400451 goto error_trans_cancel;
Dave Chinner19de7352013-04-03 16:11:18 +1100452 /*
Dave Chinnerf948dd72013-04-03 16:11:19 +1100453 * Invalidate the block(s). No validation is done.
Dave Chinner19de7352013-04-03 16:11:18 +1100454 */
455 for (i = 0; i < nmaps; i++) {
456 bp = xfs_trans_get_buf(tp, mp->m_ddev_targp,
457 XFS_FSB_TO_DADDR(mp, mval[i].br_startblock),
458 XFS_FSB_TO_BB(mp, mval[i].br_blockcount), 0);
459 if (!bp) {
Dave Chinner24513372014-06-25 14:58:08 +1000460 error = -ENOMEM;
Brian Foster36b21dd2013-09-20 11:06:09 -0400461 goto error_bmap_cancel;
Dave Chinner19de7352013-04-03 16:11:18 +1100462 }
463 xfs_trans_binval(tp, bp);
464 }
465 /*
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000466 * Unmap the dead block(s) to the dfops.
Dave Chinner19de7352013-04-03 16:11:18 +1100467 */
Dave Chinnerab7bb612015-07-29 11:51:01 +1000468 error = xfs_bunmapi(tp, ip, 0, size, 0, nmaps,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000469 &first_block, &dfops, &done);
Brian Foster36b21dd2013-09-20 11:06:09 -0400470 if (error)
471 goto error_bmap_cancel;
Dave Chinner19de7352013-04-03 16:11:18 +1100472 ASSERT(done);
473 /*
474 * Commit the first transaction. This logs the EFI and the inode.
475 */
Christoph Hellwig8ad7c6292017-08-28 10:21:04 -0700476 xfs_defer_ijoin(&dfops, ip);
477 error = xfs_defer_finish(&tp, &dfops);
Brian Foster36b21dd2013-09-20 11:06:09 -0400478 if (error)
479 goto error_bmap_cancel;
Dave Chinner3565b6602018-05-09 07:49:09 -0700480
Dave Chinner19de7352013-04-03 16:11:18 +1100481 /*
Dave Chinner19de7352013-04-03 16:11:18 +1100482 * Commit the transaction containing extent freeing and EFDs.
Dave Chinner19de7352013-04-03 16:11:18 +1100483 */
Dave Chinner3565b6602018-05-09 07:49:09 -0700484 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
Christoph Hellwig70393312015-06-04 13:48:08 +1000485 error = xfs_trans_commit(tp);
Dave Chinner19de7352013-04-03 16:11:18 +1100486 if (error) {
487 ASSERT(XFS_FORCED_SHUTDOWN(mp));
Brian Foster36b21dd2013-09-20 11:06:09 -0400488 goto error_unlock;
Dave Chinner19de7352013-04-03 16:11:18 +1100489 }
Dave Chinner19de7352013-04-03 16:11:18 +1100490
491 /*
492 * Remove the memory for extent descriptions (just bookkeeping).
493 */
494 if (ip->i_df.if_bytes)
495 xfs_idata_realloc(ip, -ip->i_df.if_bytes, XFS_DATA_FORK);
496 ASSERT(ip->i_df.if_bytes == 0);
Dave Chinner19de7352013-04-03 16:11:18 +1100497
Brian Foster36b21dd2013-09-20 11:06:09 -0400498 xfs_iunlock(ip, XFS_ILOCK_EXCL);
Dave Chinner19de7352013-04-03 16:11:18 +1100499 return 0;
500
Brian Foster36b21dd2013-09-20 11:06:09 -0400501error_bmap_cancel:
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000502 xfs_defer_cancel(&dfops);
Brian Foster36b21dd2013-09-20 11:06:09 -0400503error_trans_cancel:
Christoph Hellwig4906e212015-06-04 13:47:56 +1000504 xfs_trans_cancel(tp);
Brian Foster36b21dd2013-09-20 11:06:09 -0400505error_unlock:
506 xfs_iunlock(ip, XFS_ILOCK_EXCL);
Dave Chinner19de7352013-04-03 16:11:18 +1100507 return error;
508}
Mark Tinguely725eb1e2013-06-17 15:35:57 -0500509
510/*
511 * xfs_inactive_symlink - free a symlink
512 */
513int
514xfs_inactive_symlink(
Brian Foster36b21dd2013-09-20 11:06:09 -0400515 struct xfs_inode *ip)
Mark Tinguely725eb1e2013-06-17 15:35:57 -0500516{
517 struct xfs_mount *mp = ip->i_mount;
518 int pathlen;
519
520 trace_xfs_inactive_symlink(ip);
521
Mark Tinguely725eb1e2013-06-17 15:35:57 -0500522 if (XFS_FORCED_SHUTDOWN(mp))
Dave Chinner24513372014-06-25 14:58:08 +1000523 return -EIO;
Mark Tinguely725eb1e2013-06-17 15:35:57 -0500524
Brian Foster36b21dd2013-09-20 11:06:09 -0400525 xfs_ilock(ip, XFS_ILOCK_EXCL);
526
Mark Tinguely725eb1e2013-06-17 15:35:57 -0500527 /*
528 * Zero length symlinks _can_ exist.
529 */
530 pathlen = (int)ip->i_d.di_size;
Brian Foster36b21dd2013-09-20 11:06:09 -0400531 if (!pathlen) {
532 xfs_iunlock(ip, XFS_ILOCK_EXCL);
Mark Tinguely725eb1e2013-06-17 15:35:57 -0500533 return 0;
Brian Foster36b21dd2013-09-20 11:06:09 -0400534 }
Mark Tinguely725eb1e2013-06-17 15:35:57 -0500535
Darrick J. Wong6eb0b8d2017-07-07 08:37:26 -0700536 if (pathlen < 0 || pathlen > XFS_SYMLINK_MAXLEN) {
Mark Tinguely725eb1e2013-06-17 15:35:57 -0500537 xfs_alert(mp, "%s: inode (0x%llx) bad symlink length (%d)",
538 __func__, (unsigned long long)ip->i_ino, pathlen);
Brian Foster36b21dd2013-09-20 11:06:09 -0400539 xfs_iunlock(ip, XFS_ILOCK_EXCL);
Mark Tinguely725eb1e2013-06-17 15:35:57 -0500540 ASSERT(0);
Dave Chinner24513372014-06-25 14:58:08 +1000541 return -EFSCORRUPTED;
Mark Tinguely725eb1e2013-06-17 15:35:57 -0500542 }
543
544 if (ip->i_df.if_flags & XFS_IFINLINE) {
Brian Foster36b21dd2013-09-20 11:06:09 -0400545 if (ip->i_df.if_bytes > 0)
Mark Tinguely725eb1e2013-06-17 15:35:57 -0500546 xfs_idata_realloc(ip, -(ip->i_df.if_bytes),
547 XFS_DATA_FORK);
Brian Foster36b21dd2013-09-20 11:06:09 -0400548 xfs_iunlock(ip, XFS_ILOCK_EXCL);
Mark Tinguely725eb1e2013-06-17 15:35:57 -0500549 ASSERT(ip->i_df.if_bytes == 0);
550 return 0;
551 }
552
Brian Foster36b21dd2013-09-20 11:06:09 -0400553 xfs_iunlock(ip, XFS_ILOCK_EXCL);
554
Mark Tinguely725eb1e2013-06-17 15:35:57 -0500555 /* remove the remote symlink */
Brian Foster36b21dd2013-09-20 11:06:09 -0400556 return xfs_inactive_symlink_rmt(ip);
Mark Tinguely725eb1e2013-06-17 15:35:57 -0500557}