Line data Source code
1 : // SPDX-License-Identifier: GPL-2.0
2 : /*
3 : * Copyright (c) 2000-2006 Silicon Graphics, Inc.
4 : * Copyright (c) 2012-2013 Red Hat, Inc.
5 : * All rights reserved.
6 : */
7 : #include "xfs.h"
8 : #include "xfs_shared.h"
9 : #include "xfs_fs.h"
10 : #include "xfs_format.h"
11 : #include "xfs_log_format.h"
12 : #include "xfs_trans_resv.h"
13 : #include "xfs_bit.h"
14 : #include "xfs_mount.h"
15 : #include "xfs_dir2.h"
16 : #include "xfs_inode.h"
17 : #include "xfs_bmap.h"
18 : #include "xfs_bmap_btree.h"
19 : #include "xfs_quota.h"
20 : #include "xfs_symlink.h"
21 : #include "xfs_trans_space.h"
22 : #include "xfs_trace.h"
23 : #include "xfs_trans.h"
24 : #include "xfs_ialloc.h"
25 : #include "xfs_error.h"
26 : #include "xfs_health.h"
27 : #include "xfs_symlink_remote.h"
28 : #include "xfs_parent.h"
29 : #include "xfs_defer.h"
30 :
31 : /* ----- Kernel only functions below ----- */
32 : int
33 256208179 : xfs_readlink(
34 : struct xfs_inode *ip,
35 : char *link)
36 : {
37 256208179 : struct xfs_mount *mp = ip->i_mount;
38 256208179 : xfs_fsize_t pathlen;
39 256208179 : int error;
40 :
41 256208179 : trace_xfs_readlink(ip);
42 :
43 512486720 : if (xfs_is_shutdown(mp))
44 : return -EIO;
45 :
46 256243325 : xfs_ilock(ip, XFS_ILOCK_SHARED);
47 :
48 256301086 : pathlen = ip->i_disk_size;
49 256301086 : if (!pathlen)
50 0 : goto out_corrupt;
51 :
52 256301086 : if (pathlen < 0 || pathlen > XFS_SYMLINK_MAXLEN) {
53 0 : xfs_alert(mp, "%s: inode (%llu) bad symlink length (%lld)",
54 : __func__, (unsigned long long) ip->i_ino,
55 : (long long) pathlen);
56 0 : ASSERT(0);
57 0 : goto out_corrupt;
58 : }
59 :
60 256301086 : if (ip->i_df.if_format == XFS_DINODE_FMT_LOCAL) {
61 : /*
62 : * The VFS crashes on a NULL pointer, so return -EFSCORRUPTED
63 : * if if_data is junk.
64 : */
65 48121224 : if (XFS_IS_CORRUPT(ip->i_mount, !ip->i_df.if_u1.if_data))
66 0 : goto out_corrupt;
67 :
68 96242448 : memcpy(link, ip->i_df.if_u1.if_data, pathlen + 1);
69 48121224 : error = 0;
70 : } else {
71 208179862 : error = xfs_symlink_remote_read(ip, link);
72 : }
73 :
74 256326357 : xfs_iunlock(ip, XFS_ILOCK_SHARED);
75 256326357 : return error;
76 0 : out_corrupt:
77 0 : xfs_iunlock(ip, XFS_ILOCK_SHARED);
78 0 : xfs_inode_mark_sick(ip, XFS_SICK_INO_SYMLINK);
79 0 : return -EFSCORRUPTED;
80 : }
81 :
82 : int
83 432321466 : xfs_symlink(
84 : struct mnt_idmap *idmap,
85 : struct xfs_inode *dp,
86 : struct xfs_name *link_name,
87 : const char *target_path,
88 : umode_t mode,
89 : struct xfs_inode **ipp)
90 : {
91 432321466 : struct xfs_icreate_args args = {
92 : .nlink = 1,
93 : };
94 432321466 : struct xfs_dir_update du = {
95 : .dp = dp,
96 : .name = link_name,
97 : };
98 432321466 : struct xfs_mount *mp = dp->i_mount;
99 432321466 : struct xfs_trans *tp = NULL;
100 432321466 : int error = 0;
101 432321466 : int pathlen;
102 432321466 : bool unlock_dp_on_error = false;
103 432321466 : xfs_filblks_t fs_blocks;
104 432321466 : struct xfs_dquot *udqp;
105 432321466 : struct xfs_dquot *gdqp;
106 432321466 : struct xfs_dquot *pdqp;
107 432321466 : uint resblks;
108 432321466 : xfs_ino_t ino;
109 :
110 432321466 : *ipp = NULL;
111 :
112 432321466 : trace_xfs_symlink(dp, link_name);
113 :
114 864723268 : if (xfs_is_shutdown(mp))
115 : return -EIO;
116 :
117 : /*
118 : * Check component lengths of the target path name.
119 : */
120 432361634 : pathlen = strlen(target_path);
121 432361634 : if (pathlen >= XFS_SYMLINK_MAXLEN) /* total string too long */
122 : return -ENAMETOOLONG;
123 27175855 : ASSERT(pathlen > 0);
124 :
125 27175855 : xfs_icreate_args_inherit(&args, dp, idmap, S_IFLNK | (mode & ~S_IFMT),
126 : xfs_has_parent(mp));
127 :
128 : /*
129 : * Make sure that we have allocated dquot(s) on disk.
130 : */
131 27153859 : error = xfs_icreate_dqalloc(&args, &udqp, &gdqp, &pdqp);
132 27154906 : if (error)
133 : return error;
134 :
135 : /*
136 : * The symlink will fit into the inode data fork?
137 : * If there are no parent pointers, then there wont't be any attributes.
138 : * So we get the whole variable part, and do not need to reserve extra
139 : * blocks. Otherwise, we need to reserve the blocks.
140 : */
141 27154897 : if (pathlen <= XFS_LITINO(mp) && !xfs_has_parent(mp))
142 : fs_blocks = 0;
143 : else
144 27154681 : fs_blocks = xfs_symlink_blocks(mp, pathlen);
145 27154573 : resblks = xfs_symlink_space_res(mp, link_name->len, fs_blocks);
146 :
147 27154385 : error = xfs_parent_start(mp, &du.parent);
148 27154727 : if (error)
149 0 : goto out_release_dquots;
150 :
151 27154727 : error = xfs_trans_alloc_icreate(mp, &M_RES(mp)->tr_symlink, udqp, gdqp,
152 : pdqp, resblks, &tp);
153 27154818 : if (error)
154 19453 : goto out_parent;
155 :
156 27135365 : xfs_ilock(dp, XFS_ILOCK_EXCL | XFS_ILOCK_PARENT);
157 27135438 : unlock_dp_on_error = true;
158 :
159 : /*
160 : * Check whether the directory allows new symlinks or not.
161 : */
162 27135438 : if (dp->i_diflags & XFS_DIFLAG_NOSYMLINKS) {
163 2 : error = -EPERM;
164 2 : goto out_trans_cancel;
165 : }
166 :
167 : /*
168 : * Allocate an inode for the symlink.
169 : */
170 27135436 : error = xfs_dialloc(&tp, dp, S_IFLNK, &ino);
171 27133794 : if (!error)
172 27134022 : error = xfs_icreate(tp, ino, &args, &du.ip);
173 27134862 : if (error)
174 2 : goto out_trans_cancel;
175 :
176 : /*
177 : * Now we join the directory inode to the transaction. We do not do it
178 : * earlier because xfs_dir_ialloc might commit the previous transaction
179 : * (and release all the locks). An error from here on will result in
180 : * the transaction cancel unlocking dp so don't do it explicitly in the
181 : * error path.
182 : */
183 27134860 : xfs_trans_ijoin(tp, dp, 0);
184 :
185 : /*
186 : * Also attach the dquot(s) to it, if applicable.
187 : */
188 27134626 : xfs_qm_vop_create_dqattach(tp, du.ip, udqp, gdqp, pdqp);
189 :
190 27135332 : resblks -= XFS_IALLOC_SPACE_RES(mp);
191 27135332 : error = xfs_symlink_write_target(tp, du.ip, target_path, pathlen,
192 : fs_blocks, resblks);
193 27132758 : if (error)
194 19 : goto out_trans_cancel;
195 27132739 : resblks -= fs_blocks;
196 27132739 : i_size_write(VFS_I(du.ip), du.ip->i_disk_size);
197 :
198 : /*
199 : * Create the directory entry for the symlink.
200 : */
201 27132739 : error = xfs_dir_create_child(tp, resblks, &du);
202 27133344 : if (error)
203 2 : goto out_trans_cancel;
204 :
205 : /*
206 : * If this is a synchronous mount, make sure that the
207 : * symlink transaction goes to disk before returning to
208 : * the user.
209 : */
210 27133342 : if (xfs_has_wsync(mp) || xfs_has_dirsync(mp))
211 0 : xfs_trans_set_sync(tp);
212 :
213 27133342 : error = xfs_trans_commit(tp);
214 27134905 : if (error)
215 23 : goto out_release_inode;
216 :
217 27134882 : xfs_qm_dqrele(udqp);
218 27135158 : xfs_qm_dqrele(gdqp);
219 27134672 : xfs_qm_dqrele(pdqp);
220 :
221 27135402 : *ipp = du.ip;
222 27135402 : xfs_iunlock(du.ip, XFS_ILOCK_EXCL);
223 27135325 : xfs_iunlock(dp, XFS_ILOCK_EXCL);
224 27135341 : xfs_parent_finish(mp, du.parent);
225 : return 0;
226 :
227 25 : out_trans_cancel:
228 25 : xfs_trans_cancel(tp);
229 48 : out_release_inode:
230 : /*
231 : * Wait until after the current transaction is aborted to finish the
232 : * setup of the inode and release the inode. This prevents recursive
233 : * transactions and deadlocks from xfs_inactive.
234 : */
235 48 : if (du.ip) {
236 44 : xfs_iunlock(du.ip, XFS_ILOCK_EXCL);
237 44 : xfs_finish_inode_setup(du.ip);
238 44 : xfs_irele(du.ip);
239 : }
240 4 : out_parent:
241 19501 : xfs_parent_finish(mp, du.parent);
242 19501 : out_release_dquots:
243 19501 : xfs_qm_dqrele(udqp);
244 19501 : xfs_qm_dqrele(gdqp);
245 19501 : xfs_qm_dqrele(pdqp);
246 :
247 19501 : if (unlock_dp_on_error)
248 48 : xfs_iunlock(dp, XFS_ILOCK_EXCL);
249 : return error;
250 : }
251 :
252 : /*
253 : * Free a symlink that has blocks associated with it.
254 : *
255 : * Note: zero length symlinks are not allowed to exist. When we set the size to
256 : * zero, also change it to a regular file so that it does not get written to
257 : * disk as a zero length symlink. The inode is on the unlinked list already, so
258 : * userspace cannot find this inode anymore, so this change is not user visible
259 : * but allows us to catch corrupt zero-length symlinks in the verifiers.
260 : */
261 : STATIC int
262 20975525 : xfs_inactive_symlink_rmt(
263 : struct xfs_inode *ip)
264 : {
265 20975525 : struct xfs_mount *mp = ip->i_mount;
266 20975525 : struct xfs_trans *tp;
267 20975525 : int error;
268 :
269 20975525 : ASSERT(!xfs_need_iread_extents(&ip->i_df));
270 : /*
271 : * We're freeing a symlink that has some
272 : * blocks allocated to it. Free the
273 : * blocks here. We know that we've got
274 : * either 1 or 2 extents and that we can
275 : * free them all in one bunmapi call.
276 : */
277 20976037 : ASSERT(ip->i_df.if_nextents > 0 && ip->i_df.if_nextents <= 2);
278 :
279 20976037 : error = xfs_trans_alloc(mp, &M_RES(mp)->tr_itruncate, 0, 0, 0, &tp);
280 20976154 : if (error)
281 : return error;
282 :
283 20976300 : xfs_ilock(ip, XFS_ILOCK_EXCL);
284 20976339 : xfs_trans_ijoin(tp, ip, 0);
285 :
286 : /*
287 : * Lock the inode, fix the size, turn it into a regular file and join it
288 : * to the transaction. Hold it so in the normal path, we still have it
289 : * locked for the second transaction. In the error paths we need it
290 : * held so the cancel won't rele it, see below.
291 : */
292 20974403 : ip->i_disk_size = 0;
293 20974403 : VFS_I(ip)->i_mode = (VFS_I(ip)->i_mode & ~S_IFMT) | S_IFREG;
294 20974403 : xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
295 :
296 20976317 : error = xfs_symlink_remote_truncate(tp, ip);
297 20975541 : if (error)
298 0 : goto error_trans_cancel;
299 :
300 20975541 : error = xfs_trans_commit(tp);
301 20976381 : if (error) {
302 16 : ASSERT(xfs_is_shutdown(mp));
303 8 : goto error_unlock;
304 : }
305 :
306 : /*
307 : * Remove the memory for extent descriptions (just bookkeeping).
308 : */
309 20976373 : if (ip->i_df.if_bytes)
310 0 : xfs_idata_realloc(ip, -ip->i_df.if_bytes, XFS_DATA_FORK);
311 20976373 : ASSERT(ip->i_df.if_bytes == 0);
312 :
313 20976373 : xfs_iunlock(ip, XFS_ILOCK_EXCL);
314 20976373 : return 0;
315 :
316 : error_trans_cancel:
317 0 : xfs_trans_cancel(tp);
318 8 : error_unlock:
319 8 : xfs_iunlock(ip, XFS_ILOCK_EXCL);
320 8 : return error;
321 : }
322 :
323 : /*
324 : * xfs_inactive_symlink - free a symlink
325 : */
326 : int
327 25829985 : xfs_inactive_symlink(
328 : struct xfs_inode *ip)
329 : {
330 25829985 : struct xfs_mount *mp = ip->i_mount;
331 25829985 : int pathlen;
332 :
333 25829985 : trace_xfs_inactive_symlink(ip);
334 :
335 51659378 : if (xfs_is_shutdown(mp))
336 : return -EIO;
337 :
338 25829683 : xfs_ilock(ip, XFS_ILOCK_EXCL);
339 25831399 : pathlen = (int)ip->i_disk_size;
340 25831399 : ASSERT(pathlen);
341 :
342 25831399 : if (pathlen <= 0 || pathlen > XFS_SYMLINK_MAXLEN) {
343 0 : xfs_alert(mp, "%s: inode (0x%llx) bad symlink length (%d)",
344 : __func__, (unsigned long long)ip->i_ino, pathlen);
345 0 : xfs_iunlock(ip, XFS_ILOCK_EXCL);
346 0 : ASSERT(0);
347 0 : xfs_inode_mark_sick(ip, XFS_SICK_INO_SYMLINK);
348 0 : return -EFSCORRUPTED;
349 : }
350 :
351 : /*
352 : * Inline fork state gets removed by xfs_difree() so we have nothing to
353 : * do here in that case.
354 : */
355 25831399 : if (ip->i_df.if_format == XFS_DINODE_FMT_LOCAL) {
356 4855944 : xfs_iunlock(ip, XFS_ILOCK_EXCL);
357 4855944 : return 0;
358 : }
359 :
360 20975455 : xfs_iunlock(ip, XFS_ILOCK_EXCL);
361 :
362 : /* remove the remote symlink */
363 20974831 : return xfs_inactive_symlink_rmt(ip);
364 : }
|