• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0
2 
3 #include "fuse_i.h"
4 
5 #include <linux/fuse.h>
6 #include <linux/idr.h>
7 #include <linux/uio.h>
8 
9 #define PASSTHROUGH_IOCB_MASK                                                  \
10 	(IOCB_APPEND | IOCB_DSYNC | IOCB_HIPRI | IOCB_NOWAIT | IOCB_SYNC)
11 
12 struct fuse_aio_req {
13 	struct kiocb iocb;
14 	struct kiocb *iocb_fuse;
15 };
16 
fuse_file_accessed(struct file * dst_file,struct file * src_file)17 static void fuse_file_accessed(struct file *dst_file, struct file *src_file)
18 {
19 	struct inode *dst_inode;
20 	struct inode *src_inode;
21 
22 	if (dst_file->f_flags & O_NOATIME)
23 		return;
24 
25 	dst_inode = file_inode(dst_file);
26 	src_inode = file_inode(src_file);
27 
28 	if ((!timespec64_equal(&dst_inode->i_mtime, &src_inode->i_mtime) ||
29 	     !timespec64_equal(&dst_inode->i_ctime, &src_inode->i_ctime))) {
30 		dst_inode->i_mtime = src_inode->i_mtime;
31 		dst_inode->i_ctime = src_inode->i_ctime;
32 	}
33 
34 	touch_atime(&dst_file->f_path);
35 }
36 
fuse_copyattr(struct file * dst_file,struct file * src_file)37 static void fuse_copyattr(struct file *dst_file, struct file *src_file)
38 {
39 	struct inode *dst = file_inode(dst_file);
40 	struct inode *src = file_inode(src_file);
41 
42 	dst->i_atime = src->i_atime;
43 	dst->i_mtime = src->i_mtime;
44 	dst->i_ctime = src->i_ctime;
45 	i_size_write(dst, i_size_read(src));
46 }
47 
fuse_aio_cleanup_handler(struct fuse_aio_req * aio_req)48 static void fuse_aio_cleanup_handler(struct fuse_aio_req *aio_req)
49 {
50 	struct kiocb *iocb = &aio_req->iocb;
51 	struct kiocb *iocb_fuse = aio_req->iocb_fuse;
52 
53 	if (iocb->ki_flags & IOCB_WRITE) {
54 		__sb_writers_acquired(file_inode(iocb->ki_filp)->i_sb,
55 				      SB_FREEZE_WRITE);
56 		file_end_write(iocb->ki_filp);
57 		fuse_copyattr(iocb_fuse->ki_filp, iocb->ki_filp);
58 	}
59 
60 	iocb_fuse->ki_pos = iocb->ki_pos;
61 	kfree(aio_req);
62 }
63 
fuse_aio_rw_complete(struct kiocb * iocb,long res,long res2)64 static void fuse_aio_rw_complete(struct kiocb *iocb, long res, long res2)
65 {
66 	struct fuse_aio_req *aio_req =
67 		container_of(iocb, struct fuse_aio_req, iocb);
68 	struct kiocb *iocb_fuse = aio_req->iocb_fuse;
69 
70 	fuse_aio_cleanup_handler(aio_req);
71 	iocb_fuse->ki_complete(iocb_fuse, res, res2);
72 }
73 
fuse_passthrough_read_iter(struct kiocb * iocb_fuse,struct iov_iter * iter)74 ssize_t fuse_passthrough_read_iter(struct kiocb *iocb_fuse,
75 				   struct iov_iter *iter)
76 {
77 	ssize_t ret;
78 	const struct cred *old_cred;
79 	struct file *fuse_filp = iocb_fuse->ki_filp;
80 	struct fuse_file *ff = fuse_filp->private_data;
81 	struct file *passthrough_filp = ff->passthrough.filp;
82 
83 	if (!iov_iter_count(iter))
84 		return 0;
85 
86 	old_cred = override_creds(ff->passthrough.cred);
87 	if (is_sync_kiocb(iocb_fuse)) {
88 		ret = vfs_iter_read(passthrough_filp, iter, &iocb_fuse->ki_pos,
89 				    iocb_to_rw_flags(iocb_fuse->ki_flags,
90 						     PASSTHROUGH_IOCB_MASK));
91 	} else {
92 		struct fuse_aio_req *aio_req;
93 
94 		aio_req = kmalloc(sizeof(struct fuse_aio_req), GFP_KERNEL);
95 		if (!aio_req) {
96 			ret = -ENOMEM;
97 			goto out;
98 		}
99 
100 		aio_req->iocb_fuse = iocb_fuse;
101 		kiocb_clone(&aio_req->iocb, iocb_fuse, passthrough_filp);
102 		aio_req->iocb.ki_complete = fuse_aio_rw_complete;
103 		ret = call_read_iter(passthrough_filp, &aio_req->iocb, iter);
104 		if (ret != -EIOCBQUEUED)
105 			fuse_aio_cleanup_handler(aio_req);
106 	}
107 out:
108 	revert_creds(old_cred);
109 
110 	fuse_file_accessed(fuse_filp, passthrough_filp);
111 
112 	return ret;
113 }
114 
fuse_passthrough_write_iter(struct kiocb * iocb_fuse,struct iov_iter * iter)115 ssize_t fuse_passthrough_write_iter(struct kiocb *iocb_fuse,
116 				    struct iov_iter *iter)
117 {
118 	ssize_t ret;
119 	const struct cred *old_cred;
120 	struct file *fuse_filp = iocb_fuse->ki_filp;
121 	struct fuse_file *ff = fuse_filp->private_data;
122 	struct inode *fuse_inode = file_inode(fuse_filp);
123 	struct file *passthrough_filp = ff->passthrough.filp;
124 	struct inode *passthrough_inode = file_inode(passthrough_filp);
125 
126 	if (!iov_iter_count(iter))
127 		return 0;
128 
129 	inode_lock(fuse_inode);
130 
131 	fuse_copyattr(fuse_filp, passthrough_filp);
132 
133 	old_cred = override_creds(ff->passthrough.cred);
134 	if (is_sync_kiocb(iocb_fuse)) {
135 		file_start_write(passthrough_filp);
136 		ret = vfs_iter_write(passthrough_filp, iter, &iocb_fuse->ki_pos,
137 				     iocb_to_rw_flags(iocb_fuse->ki_flags,
138 						      PASSTHROUGH_IOCB_MASK));
139 		file_end_write(passthrough_filp);
140 		if (ret > 0)
141 			fuse_copyattr(fuse_filp, passthrough_filp);
142 	} else {
143 		struct fuse_aio_req *aio_req;
144 
145 		aio_req = kmalloc(sizeof(struct fuse_aio_req), GFP_KERNEL);
146 		if (!aio_req) {
147 			ret = -ENOMEM;
148 			goto out;
149 		}
150 
151 		file_start_write(passthrough_filp);
152 		__sb_writers_release(passthrough_inode->i_sb, SB_FREEZE_WRITE);
153 
154 		aio_req->iocb_fuse = iocb_fuse;
155 		kiocb_clone(&aio_req->iocb, iocb_fuse, passthrough_filp);
156 		aio_req->iocb.ki_complete = fuse_aio_rw_complete;
157 		ret = call_write_iter(passthrough_filp, &aio_req->iocb, iter);
158 		if (ret != -EIOCBQUEUED)
159 			fuse_aio_cleanup_handler(aio_req);
160 	}
161 out:
162 	revert_creds(old_cred);
163 	inode_unlock(fuse_inode);
164 
165 	return ret;
166 }
167 
fuse_passthrough_mmap(struct file * file,struct vm_area_struct * vma)168 ssize_t fuse_passthrough_mmap(struct file *file, struct vm_area_struct *vma)
169 {
170 	int ret;
171 	const struct cred *old_cred;
172 	struct fuse_file *ff = file->private_data;
173 	struct file *passthrough_filp = ff->passthrough.filp;
174 
175 	if (!passthrough_filp->f_op->mmap)
176 		return -ENODEV;
177 
178 	if (WARN_ON(file != vma->vm_file))
179 		return -EIO;
180 
181 	vma->vm_file = get_file(passthrough_filp);
182 
183 	old_cred = override_creds(ff->passthrough.cred);
184 	ret = call_mmap(vma->vm_file, vma);
185 	revert_creds(old_cred);
186 
187 	if (ret)
188 		fput(passthrough_filp);
189 	else
190 		fput(file);
191 
192 	fuse_file_accessed(file, passthrough_filp);
193 
194 	return ret;
195 }
196 
fuse_passthrough_open(struct fuse_dev * fud,u32 lower_fd)197 int fuse_passthrough_open(struct fuse_dev *fud, u32 lower_fd)
198 {
199 	int res;
200 	struct file *passthrough_filp;
201 	struct fuse_conn *fc = fud->fc;
202 	struct inode *passthrough_inode;
203 	struct super_block *passthrough_sb;
204 	struct fuse_passthrough *passthrough;
205 
206 	if (!fc->passthrough)
207 		return -EPERM;
208 
209 	passthrough_filp = fget(lower_fd);
210 	if (!passthrough_filp) {
211 		pr_err("FUSE: invalid file descriptor for passthrough.\n");
212 		return -EBADF;
213 	}
214 
215 	if (!passthrough_filp->f_op->read_iter ||
216 	    !passthrough_filp->f_op->write_iter) {
217 		pr_err("FUSE: passthrough file misses file operations.\n");
218 		res = -EBADF;
219 		goto err_free_file;
220 	}
221 
222 	passthrough_inode = file_inode(passthrough_filp);
223 	passthrough_sb = passthrough_inode->i_sb;
224 	if (passthrough_sb->s_stack_depth >= FILESYSTEM_MAX_STACK_DEPTH) {
225 		pr_err("FUSE: fs stacking depth exceeded for passthrough\n");
226 		res = -EINVAL;
227 		goto err_free_file;
228 	}
229 
230 	passthrough = kmalloc(sizeof(struct fuse_passthrough), GFP_KERNEL);
231 	if (!passthrough) {
232 		res = -ENOMEM;
233 		goto err_free_file;
234 	}
235 
236 	passthrough->filp = passthrough_filp;
237 	passthrough->cred = prepare_creds();
238 
239 	idr_preload(GFP_KERNEL);
240 	spin_lock(&fc->passthrough_req_lock);
241 	res = idr_alloc(&fc->passthrough_req, passthrough, 1, 0, GFP_ATOMIC);
242 	spin_unlock(&fc->passthrough_req_lock);
243 	idr_preload_end();
244 
245 	if (res > 0)
246 		return res;
247 
248 	fuse_passthrough_release(passthrough);
249 	kfree(passthrough);
250 
251 err_free_file:
252 	fput(passthrough_filp);
253 
254 	return res;
255 }
256 
fuse_passthrough_setup(struct fuse_conn * fc,struct fuse_file * ff,struct fuse_open_out * openarg)257 int fuse_passthrough_setup(struct fuse_conn *fc, struct fuse_file *ff,
258 			   struct fuse_open_out *openarg)
259 {
260 	struct fuse_passthrough *passthrough;
261 	int passthrough_fh = openarg->passthrough_fh;
262 
263 	if (!fc->passthrough)
264 		return -EPERM;
265 
266 	/* Default case, passthrough is not requested */
267 	if (passthrough_fh <= 0)
268 		return -EINVAL;
269 
270 	spin_lock(&fc->passthrough_req_lock);
271 	passthrough = idr_remove(&fc->passthrough_req, passthrough_fh);
272 	spin_unlock(&fc->passthrough_req_lock);
273 
274 	if (!passthrough)
275 		return -EINVAL;
276 
277 	ff->passthrough = *passthrough;
278 	kfree(passthrough);
279 
280 	return 0;
281 }
282 
fuse_passthrough_release(struct fuse_passthrough * passthrough)283 void fuse_passthrough_release(struct fuse_passthrough *passthrough)
284 {
285 	if (passthrough->filp) {
286 		fput(passthrough->filp);
287 		passthrough->filp = NULL;
288 	}
289 	if (passthrough->cred) {
290 		put_cred(passthrough->cred);
291 		passthrough->cred = NULL;
292 	}
293 }
294