blob: 8564d91c7d416ae87aea6019457f1476926d48cc [file] [log] [blame]
Miklos Szeredi334f4852005-09-09 13:10:27 -07001/*
2 FUSE: Filesystem in Userspace
Miklos Szeredi1729a162008-11-26 12:03:54 +01003 Copyright (C) 2001-2008 Miklos Szeredi <miklos@szeredi.hu>
Miklos Szeredi334f4852005-09-09 13:10:27 -07004
5 This program can be distributed under the terms of the GNU GPL.
6 See the file COPYING.
7*/
8
9#include "fuse_i.h"
10
11#include <linux/init.h>
12#include <linux/module.h>
13#include <linux/poll.h>
Ingo Molnar174cd4b2017-02-02 19:15:33 +010014#include <linux/sched/signal.h>
Miklos Szeredi334f4852005-09-09 13:10:27 -070015#include <linux/uio.h>
16#include <linux/miscdevice.h>
17#include <linux/pagemap.h>
18#include <linux/file.h>
19#include <linux/slab.h>
Miklos Szeredidd3bb142010-05-25 15:06:06 +020020#include <linux/pipe_fs_i.h>
Miklos Szeredice534fb2010-05-25 15:06:07 +020021#include <linux/swap.h>
22#include <linux/splice.h>
Seth Forshee0b6e9ea2014-07-02 16:29:19 -050023#include <linux/sched.h>
Miklos Szeredi334f4852005-09-09 13:10:27 -070024
25MODULE_ALIAS_MISCDEV(FUSE_MINOR);
Kay Sievers578454f2010-05-20 18:07:20 +020026MODULE_ALIAS("devname:fuse");
Miklos Szeredi334f4852005-09-09 13:10:27 -070027
Christoph Lametere18b8902006-12-06 20:33:20 -080028static struct kmem_cache *fuse_req_cachep;
Miklos Szeredi334f4852005-09-09 13:10:27 -070029
Miklos Szeredicc080e92015-07-01 16:26:08 +020030static struct fuse_dev *fuse_get_dev(struct file *file)
Miklos Szeredi334f4852005-09-09 13:10:27 -070031{
Miklos Szeredi0720b312006-04-10 22:54:55 -070032 /*
33 * Lockless access is OK, because file->private data is set
34 * once during mount and is valid until the file is released.
35 */
Mark Rutland6aa7de02017-10-23 14:07:29 -070036 return READ_ONCE(file->private_data);
Miklos Szeredi334f4852005-09-09 13:10:27 -070037}
38
Maxim Patlasov4250c062012-10-26 19:48:07 +040039static void fuse_request_init(struct fuse_req *req, struct page **pages,
Maxim Patlasovb2430d72012-10-26 19:49:24 +040040 struct fuse_page_desc *page_descs,
Maxim Patlasov4250c062012-10-26 19:48:07 +040041 unsigned npages)
Miklos Szeredi334f4852005-09-09 13:10:27 -070042{
43 memset(req, 0, sizeof(*req));
Maxim Patlasov4250c062012-10-26 19:48:07 +040044 memset(pages, 0, sizeof(*pages) * npages);
Maxim Patlasovb2430d72012-10-26 19:49:24 +040045 memset(page_descs, 0, sizeof(*page_descs) * npages);
Miklos Szeredi334f4852005-09-09 13:10:27 -070046 INIT_LIST_HEAD(&req->list);
Miklos Szeredia4d27e72006-06-25 05:48:54 -070047 INIT_LIST_HEAD(&req->intr_entry);
Miklos Szeredi334f4852005-09-09 13:10:27 -070048 init_waitqueue_head(&req->waitq);
Elena Reshetovaec99f6d2017-03-03 11:04:04 +020049 refcount_set(&req->count, 1);
Maxim Patlasov4250c062012-10-26 19:48:07 +040050 req->pages = pages;
Maxim Patlasovb2430d72012-10-26 19:49:24 +040051 req->page_descs = page_descs;
Maxim Patlasov4250c062012-10-26 19:48:07 +040052 req->max_pages = npages;
Miklos Szeredi33e14b42015-07-01 16:26:01 +020053 __set_bit(FR_PENDING, &req->flags);
Miklos Szeredi334f4852005-09-09 13:10:27 -070054}
55
Maxim Patlasov4250c062012-10-26 19:48:07 +040056static struct fuse_req *__fuse_request_alloc(unsigned npages, gfp_t flags)
Miklos Szeredi334f4852005-09-09 13:10:27 -070057{
Maxim Patlasov4250c062012-10-26 19:48:07 +040058 struct fuse_req *req = kmem_cache_alloc(fuse_req_cachep, flags);
59 if (req) {
60 struct page **pages;
Maxim Patlasovb2430d72012-10-26 19:49:24 +040061 struct fuse_page_desc *page_descs;
Maxim Patlasov4250c062012-10-26 19:48:07 +040062
Maxim Patlasovb2430d72012-10-26 19:49:24 +040063 if (npages <= FUSE_REQ_INLINE_PAGES) {
Maxim Patlasov4250c062012-10-26 19:48:07 +040064 pages = req->inline_pages;
Maxim Patlasovb2430d72012-10-26 19:49:24 +040065 page_descs = req->inline_page_descs;
66 } else {
Kees Cook6da2ec52018-06-12 13:55:00 -070067 pages = kmalloc_array(npages, sizeof(struct page *),
68 flags);
69 page_descs =
70 kmalloc_array(npages,
71 sizeof(struct fuse_page_desc),
72 flags);
Maxim Patlasovb2430d72012-10-26 19:49:24 +040073 }
Maxim Patlasov4250c062012-10-26 19:48:07 +040074
Maxim Patlasovb2430d72012-10-26 19:49:24 +040075 if (!pages || !page_descs) {
76 kfree(pages);
77 kfree(page_descs);
Maxim Patlasov4250c062012-10-26 19:48:07 +040078 kmem_cache_free(fuse_req_cachep, req);
79 return NULL;
80 }
81
Maxim Patlasovb2430d72012-10-26 19:49:24 +040082 fuse_request_init(req, pages, page_descs, npages);
Maxim Patlasov4250c062012-10-26 19:48:07 +040083 }
Miklos Szeredi334f4852005-09-09 13:10:27 -070084 return req;
85}
Maxim Patlasov4250c062012-10-26 19:48:07 +040086
87struct fuse_req *fuse_request_alloc(unsigned npages)
88{
89 return __fuse_request_alloc(npages, GFP_KERNEL);
90}
Tejun Heo08cbf542009-04-14 10:54:53 +090091EXPORT_SYMBOL_GPL(fuse_request_alloc);
Miklos Szeredi334f4852005-09-09 13:10:27 -070092
Maxim Patlasov4250c062012-10-26 19:48:07 +040093struct fuse_req *fuse_request_alloc_nofs(unsigned npages)
Miklos Szeredi3be5a522008-04-30 00:54:41 -070094{
Maxim Patlasov4250c062012-10-26 19:48:07 +040095 return __fuse_request_alloc(npages, GFP_NOFS);
Miklos Szeredi3be5a522008-04-30 00:54:41 -070096}
97
Miklos Szeredi334f4852005-09-09 13:10:27 -070098void fuse_request_free(struct fuse_req *req)
99{
Maxim Patlasovb2430d72012-10-26 19:49:24 +0400100 if (req->pages != req->inline_pages) {
Maxim Patlasov4250c062012-10-26 19:48:07 +0400101 kfree(req->pages);
Maxim Patlasovb2430d72012-10-26 19:49:24 +0400102 kfree(req->page_descs);
103 }
Miklos Szeredi334f4852005-09-09 13:10:27 -0700104 kmem_cache_free(fuse_req_cachep, req);
105}
106
Maxim Patlasov36cf66e2012-12-14 19:20:51 +0400107void __fuse_get_request(struct fuse_req *req)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700108{
Elena Reshetovaec99f6d2017-03-03 11:04:04 +0200109 refcount_inc(&req->count);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700110}
111
112/* Must be called with > 1 refcount */
113static void __fuse_put_request(struct fuse_req *req)
114{
Elena Reshetovaec99f6d2017-03-03 11:04:04 +0200115 refcount_dec(&req->count);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700116}
117
Miklos Szeredi9759bd512015-01-06 10:45:35 +0100118void fuse_set_initialized(struct fuse_conn *fc)
119{
120 /* Make sure stores before this are seen on another CPU */
121 smp_wmb();
122 fc->initialized = 1;
123}
124
Maxim Patlasov0aada882013-03-21 18:02:28 +0400125static bool fuse_block_alloc(struct fuse_conn *fc, bool for_background)
126{
127 return !fc->initialized || (for_background && fc->blocked);
128}
129
Maxim Patlasov8b41e672013-03-21 18:02:04 +0400130static struct fuse_req *__fuse_get_req(struct fuse_conn *fc, unsigned npages,
131 bool for_background)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700132{
Miklos Szeredi08a53cd2006-04-10 22:54:59 -0700133 struct fuse_req *req;
Miklos Szeredi08a53cd2006-04-10 22:54:59 -0700134 int err;
Miklos Szeredi9bc5ddd2006-04-11 21:16:09 +0200135 atomic_inc(&fc->num_waiting);
Maxim Patlasov0aada882013-03-21 18:02:28 +0400136
137 if (fuse_block_alloc(fc, for_background)) {
Maxim Patlasov0aada882013-03-21 18:02:28 +0400138 err = -EINTR;
Al Viro7d3a07f2016-07-19 03:08:27 -0400139 if (wait_event_killable_exclusive(fc->blocked_waitq,
140 !fuse_block_alloc(fc, for_background)))
Maxim Patlasov0aada882013-03-21 18:02:28 +0400141 goto out;
142 }
Miklos Szeredi9759bd512015-01-06 10:45:35 +0100143 /* Matches smp_wmb() in fuse_set_initialized() */
144 smp_rmb();
Miklos Szeredi08a53cd2006-04-10 22:54:59 -0700145
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700146 err = -ENOTCONN;
147 if (!fc->connected)
148 goto out;
149
Miklos Szeredide155222015-07-01 16:25:57 +0200150 err = -ECONNREFUSED;
151 if (fc->conn_error)
152 goto out;
153
Maxim Patlasovb111c8c2012-10-26 19:48:30 +0400154 req = fuse_request_alloc(npages);
Miklos Szeredi9bc5ddd2006-04-11 21:16:09 +0200155 err = -ENOMEM;
Maxim Patlasov722d2be2013-03-21 18:02:36 +0400156 if (!req) {
157 if (for_background)
158 wake_up(&fc->blocked_waitq);
Miklos Szeredi9bc5ddd2006-04-11 21:16:09 +0200159 goto out;
Maxim Patlasov722d2be2013-03-21 18:02:36 +0400160 }
Miklos Szeredi334f4852005-09-09 13:10:27 -0700161
Eric W. Biederman8cb08322018-02-21 11:18:07 -0600162 req->in.h.uid = from_kuid(fc->user_ns, current_fsuid());
163 req->in.h.gid = from_kgid(fc->user_ns, current_fsgid());
Eric W. Biedermanc9582eb2018-02-21 10:52:06 -0600164 req->in.h.pid = pid_nr_ns(task_pid(current), fc->pid_ns);
165
Miklos Szeredi825d6d32015-07-01 16:25:58 +0200166 __set_bit(FR_WAITING, &req->flags);
167 if (for_background)
168 __set_bit(FR_BACKGROUND, &req->flags);
169
Eric W. Biedermanc9582eb2018-02-21 10:52:06 -0600170 if (unlikely(req->in.h.uid == ((uid_t)-1) ||
171 req->in.h.gid == ((gid_t)-1))) {
172 fuse_put_request(fc, req);
173 return ERR_PTR(-EOVERFLOW);
174 }
Miklos Szeredi334f4852005-09-09 13:10:27 -0700175 return req;
Miklos Szeredi9bc5ddd2006-04-11 21:16:09 +0200176
177 out:
178 atomic_dec(&fc->num_waiting);
179 return ERR_PTR(err);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700180}
Maxim Patlasov8b41e672013-03-21 18:02:04 +0400181
182struct fuse_req *fuse_get_req(struct fuse_conn *fc, unsigned npages)
183{
184 return __fuse_get_req(fc, npages, false);
185}
Tejun Heo08cbf542009-04-14 10:54:53 +0900186EXPORT_SYMBOL_GPL(fuse_get_req);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700187
Maxim Patlasov8b41e672013-03-21 18:02:04 +0400188struct fuse_req *fuse_get_req_for_background(struct fuse_conn *fc,
189 unsigned npages)
190{
191 return __fuse_get_req(fc, npages, true);
192}
193EXPORT_SYMBOL_GPL(fuse_get_req_for_background);
194
Miklos Szeredi33649c92006-06-25 05:48:52 -0700195/*
196 * Return request in fuse_file->reserved_req. However that may
197 * currently be in use. If that is the case, wait for it to become
198 * available.
199 */
200static struct fuse_req *get_reserved_req(struct fuse_conn *fc,
201 struct file *file)
202{
203 struct fuse_req *req = NULL;
204 struct fuse_file *ff = file->private_data;
205
206 do {
Miklos Szeredide5e3de2007-10-16 23:31:00 -0700207 wait_event(fc->reserved_req_waitq, ff->reserved_req);
Miklos Szeredi33649c92006-06-25 05:48:52 -0700208 spin_lock(&fc->lock);
209 if (ff->reserved_req) {
210 req = ff->reserved_req;
211 ff->reserved_req = NULL;
Al Virocb0942b2012-08-27 14:48:26 -0400212 req->stolen_file = get_file(file);
Miklos Szeredi33649c92006-06-25 05:48:52 -0700213 }
214 spin_unlock(&fc->lock);
215 } while (!req);
216
217 return req;
218}
219
220/*
221 * Put stolen request back into fuse_file->reserved_req
222 */
223static void put_reserved_req(struct fuse_conn *fc, struct fuse_req *req)
224{
225 struct file *file = req->stolen_file;
226 struct fuse_file *ff = file->private_data;
227
228 spin_lock(&fc->lock);
Maxim Patlasovb2430d72012-10-26 19:49:24 +0400229 fuse_request_init(req, req->pages, req->page_descs, req->max_pages);
Miklos Szeredi33649c92006-06-25 05:48:52 -0700230 BUG_ON(ff->reserved_req);
231 ff->reserved_req = req;
Miklos Szeredide5e3de2007-10-16 23:31:00 -0700232 wake_up_all(&fc->reserved_req_waitq);
Miklos Szeredi33649c92006-06-25 05:48:52 -0700233 spin_unlock(&fc->lock);
234 fput(file);
235}
236
237/*
238 * Gets a requests for a file operation, always succeeds
239 *
240 * This is used for sending the FLUSH request, which must get to
241 * userspace, due to POSIX locks which may need to be unlocked.
242 *
243 * If allocation fails due to OOM, use the reserved request in
244 * fuse_file.
245 *
246 * This is very unlikely to deadlock accidentally, since the
247 * filesystem should not have it's own file open. If deadlock is
248 * intentional, it can still be broken by "aborting" the filesystem.
249 */
Maxim Patlasovb111c8c2012-10-26 19:48:30 +0400250struct fuse_req *fuse_get_req_nofail_nopages(struct fuse_conn *fc,
251 struct file *file)
Miklos Szeredi33649c92006-06-25 05:48:52 -0700252{
253 struct fuse_req *req;
254
255 atomic_inc(&fc->num_waiting);
Maxim Patlasov0aada882013-03-21 18:02:28 +0400256 wait_event(fc->blocked_waitq, fc->initialized);
Miklos Szeredi9759bd512015-01-06 10:45:35 +0100257 /* Matches smp_wmb() in fuse_set_initialized() */
258 smp_rmb();
Maxim Patlasovb111c8c2012-10-26 19:48:30 +0400259 req = fuse_request_alloc(0);
Miklos Szeredi33649c92006-06-25 05:48:52 -0700260 if (!req)
261 req = get_reserved_req(fc, file);
262
Eric W. Biederman8cb08322018-02-21 11:18:07 -0600263 req->in.h.uid = from_kuid_munged(fc->user_ns, current_fsuid());
264 req->in.h.gid = from_kgid_munged(fc->user_ns, current_fsgid());
Eric W. Biedermanc9582eb2018-02-21 10:52:06 -0600265 req->in.h.pid = pid_nr_ns(task_pid(current), fc->pid_ns);
266
Miklos Szeredi825d6d32015-07-01 16:25:58 +0200267 __set_bit(FR_WAITING, &req->flags);
268 __clear_bit(FR_BACKGROUND, &req->flags);
Miklos Szeredi33649c92006-06-25 05:48:52 -0700269 return req;
270}
271
Miklos Szeredi334f4852005-09-09 13:10:27 -0700272void fuse_put_request(struct fuse_conn *fc, struct fuse_req *req)
273{
Elena Reshetovaec99f6d2017-03-03 11:04:04 +0200274 if (refcount_dec_and_test(&req->count)) {
Miklos Szeredi825d6d32015-07-01 16:25:58 +0200275 if (test_bit(FR_BACKGROUND, &req->flags)) {
Maxim Patlasov722d2be2013-03-21 18:02:36 +0400276 /*
277 * We get here in the unlikely case that a background
278 * request was allocated but not sent
279 */
280 spin_lock(&fc->lock);
281 if (!fc->blocked)
282 wake_up(&fc->blocked_waitq);
283 spin_unlock(&fc->lock);
284 }
285
Miklos Szeredi825d6d32015-07-01 16:25:58 +0200286 if (test_bit(FR_WAITING, &req->flags)) {
287 __clear_bit(FR_WAITING, &req->flags);
Miklos Szeredi9bc5ddd2006-04-11 21:16:09 +0200288 atomic_dec(&fc->num_waiting);
Miklos Szeredi73e0e732015-07-01 16:25:56 +0200289 }
Miklos Szeredi33649c92006-06-25 05:48:52 -0700290
291 if (req->stolen_file)
292 put_reserved_req(fc, req);
293 else
294 fuse_request_free(req);
Miklos Szeredi7128ec22006-02-04 23:27:40 -0800295 }
296}
Tejun Heo08cbf542009-04-14 10:54:53 +0900297EXPORT_SYMBOL_GPL(fuse_put_request);
Miklos Szeredi7128ec22006-02-04 23:27:40 -0800298
Miklos Szeredid12def12008-02-06 01:38:39 -0800299static unsigned len_args(unsigned numargs, struct fuse_arg *args)
300{
301 unsigned nbytes = 0;
302 unsigned i;
303
304 for (i = 0; i < numargs; i++)
305 nbytes += args[i].size;
306
307 return nbytes;
308}
309
Miklos Szeredif88996a2015-07-01 16:26:01 +0200310static u64 fuse_get_unique(struct fuse_iqueue *fiq)
Miklos Szeredid12def12008-02-06 01:38:39 -0800311{
Miklos Szeredif88996a2015-07-01 16:26:01 +0200312 return ++fiq->reqctr;
Miklos Szeredid12def12008-02-06 01:38:39 -0800313}
314
Miklos Szeredif88996a2015-07-01 16:26:01 +0200315static void queue_request(struct fuse_iqueue *fiq, struct fuse_req *req)
Miklos Szeredid12def12008-02-06 01:38:39 -0800316{
Miklos Szeredid12def12008-02-06 01:38:39 -0800317 req->in.h.len = sizeof(struct fuse_in_header) +
318 len_args(req->in.numargs, (struct fuse_arg *) req->in.args);
Miklos Szeredif88996a2015-07-01 16:26:01 +0200319 list_add_tail(&req->list, &fiq->pending);
Miklos Szeredi4ce60812015-07-01 16:26:02 +0200320 wake_up_locked(&fiq->waitq);
Miklos Szeredif88996a2015-07-01 16:26:01 +0200321 kill_fasync(&fiq->fasync, SIGIO, POLL_IN);
Miklos Szeredid12def12008-02-06 01:38:39 -0800322}
323
Miklos Szeredi07e77dc2010-12-07 20:16:56 +0100324void fuse_queue_forget(struct fuse_conn *fc, struct fuse_forget_link *forget,
325 u64 nodeid, u64 nlookup)
326{
Miklos Szeredif88996a2015-07-01 16:26:01 +0200327 struct fuse_iqueue *fiq = &fc->iq;
328
Miklos Szeredi02c048b2010-12-07 20:16:56 +0100329 forget->forget_one.nodeid = nodeid;
330 forget->forget_one.nlookup = nlookup;
Miklos Szeredi07e77dc2010-12-07 20:16:56 +0100331
Miklos Szeredi4ce60812015-07-01 16:26:02 +0200332 spin_lock(&fiq->waitq.lock);
Miklos Szeredie16714d2015-07-01 16:26:01 +0200333 if (fiq->connected) {
Miklos Szeredif88996a2015-07-01 16:26:01 +0200334 fiq->forget_list_tail->next = forget;
335 fiq->forget_list_tail = forget;
Miklos Szeredi4ce60812015-07-01 16:26:02 +0200336 wake_up_locked(&fiq->waitq);
Miklos Szeredif88996a2015-07-01 16:26:01 +0200337 kill_fasync(&fiq->fasync, SIGIO, POLL_IN);
Miklos Szeredi5dfcc872011-09-12 09:38:03 +0200338 } else {
339 kfree(forget);
340 }
Miklos Szeredi4ce60812015-07-01 16:26:02 +0200341 spin_unlock(&fiq->waitq.lock);
Miklos Szeredi07e77dc2010-12-07 20:16:56 +0100342}
343
Miklos Szeredid12def12008-02-06 01:38:39 -0800344static void flush_bg_queue(struct fuse_conn *fc)
345{
Csaba Henk7a6d3c82009-07-01 17:28:41 -0700346 while (fc->active_background < fc->max_background &&
Miklos Szeredid12def12008-02-06 01:38:39 -0800347 !list_empty(&fc->bg_queue)) {
348 struct fuse_req *req;
Miklos Szeredif88996a2015-07-01 16:26:01 +0200349 struct fuse_iqueue *fiq = &fc->iq;
Miklos Szeredid12def12008-02-06 01:38:39 -0800350
351 req = list_entry(fc->bg_queue.next, struct fuse_req, list);
352 list_del(&req->list);
353 fc->active_background++;
Miklos Szeredi4ce60812015-07-01 16:26:02 +0200354 spin_lock(&fiq->waitq.lock);
Miklos Szeredif88996a2015-07-01 16:26:01 +0200355 req->in.h.unique = fuse_get_unique(fiq);
356 queue_request(fiq, req);
Miklos Szeredi4ce60812015-07-01 16:26:02 +0200357 spin_unlock(&fiq->waitq.lock);
Miklos Szeredid12def12008-02-06 01:38:39 -0800358 }
359}
360
Miklos Szeredi6dbbcb12006-04-26 10:49:06 +0200361/*
Miklos Szeredi334f4852005-09-09 13:10:27 -0700362 * This function is called when a request is finished. Either a reply
Miklos Szeredif9a28422006-06-25 05:48:53 -0700363 * has arrived or it was aborted (and not yet sent) or some error
Miklos Szeredif43b1552006-01-16 22:14:26 -0800364 * occurred during communication with userspace, or the device file
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700365 * was closed. The requester thread is woken up (if still waiting),
366 * the 'end' callback is called if given, else the reference to the
367 * request is released
Miklos Szeredi334f4852005-09-09 13:10:27 -0700368 */
369static void request_end(struct fuse_conn *fc, struct fuse_req *req)
370{
Miklos Szeredi4ce60812015-07-01 16:26:02 +0200371 struct fuse_iqueue *fiq = &fc->iq;
Miklos Szeredi365ae712015-07-01 16:26:06 +0200372
Miklos Szerediefe28002015-07-01 16:26:07 +0200373 if (test_and_set_bit(FR_FINISHED, &req->flags))
Miklos Szeredi87114372018-07-26 16:13:11 +0200374 goto out_put_req;
Miklos Szeredi365ae712015-07-01 16:26:06 +0200375
Miklos Szeredi4ce60812015-07-01 16:26:02 +0200376 spin_lock(&fiq->waitq.lock);
Miklos Szeredi0d8e84b2015-07-01 16:25:58 +0200377 list_del_init(&req->intr_entry);
Miklos Szeredi4ce60812015-07-01 16:26:02 +0200378 spin_unlock(&fiq->waitq.lock);
Miklos Szeredi33e14b42015-07-01 16:26:01 +0200379 WARN_ON(test_bit(FR_PENDING, &req->flags));
380 WARN_ON(test_bit(FR_SENT, &req->flags));
Miklos Szeredi825d6d32015-07-01 16:25:58 +0200381 if (test_bit(FR_BACKGROUND, &req->flags)) {
Miklos Szerediefe28002015-07-01 16:26:07 +0200382 spin_lock(&fc->lock);
Miklos Szeredi825d6d32015-07-01 16:25:58 +0200383 clear_bit(FR_BACKGROUND, &req->flags);
Maxim Patlasov722d2be2013-03-21 18:02:36 +0400384 if (fc->num_background == fc->max_background)
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700385 fc->blocked = 0;
Maxim Patlasov722d2be2013-03-21 18:02:36 +0400386
387 /* Wake up next waiter, if any */
Miklos Szeredi3c18ef82013-04-17 21:50:58 +0200388 if (!fc->blocked && waitqueue_active(&fc->blocked_waitq))
Maxim Patlasov722d2be2013-03-21 18:02:36 +0400389 wake_up(&fc->blocked_waitq);
390
Tejun Heo8a301eb2018-02-02 09:54:14 -0800391 if (fc->num_background == fc->congestion_threshold && fc->sb) {
Jan Kara5f7f7542017-04-12 12:24:40 +0200392 clear_bdi_congested(fc->sb->s_bdi, BLK_RW_SYNC);
393 clear_bdi_congested(fc->sb->s_bdi, BLK_RW_ASYNC);
Miklos Szeredif92b99b2007-10-16 23:30:59 -0700394 }
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700395 fc->num_background--;
Miklos Szeredid12def12008-02-06 01:38:39 -0800396 fc->active_background--;
397 flush_bg_queue(fc);
Miklos Szerediefe28002015-07-01 16:26:07 +0200398 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700399 }
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700400 wake_up(&req->waitq);
Miklos Szeredi1e6881c2015-07-01 16:26:07 +0200401 if (req->end)
402 req->end(fc, req);
Miklos Szeredi87114372018-07-26 16:13:11 +0200403out_put_req:
Tejun Heoe9bb09d2008-11-26 12:03:54 +0100404 fuse_put_request(fc, req);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700405}
406
Miklos Szeredif88996a2015-07-01 16:26:01 +0200407static void queue_interrupt(struct fuse_iqueue *fiq, struct fuse_req *req)
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700408{
Miklos Szeredi4ce60812015-07-01 16:26:02 +0200409 spin_lock(&fiq->waitq.lock);
Sahitya Tummala6ba4d272017-02-08 20:30:56 +0530410 if (test_bit(FR_FINISHED, &req->flags)) {
411 spin_unlock(&fiq->waitq.lock);
412 return;
413 }
Miklos Szeredi8f7bb362015-07-01 16:26:03 +0200414 if (list_empty(&req->intr_entry)) {
415 list_add_tail(&req->intr_entry, &fiq->interrupts);
416 wake_up_locked(&fiq->waitq);
417 }
Miklos Szeredi4ce60812015-07-01 16:26:02 +0200418 spin_unlock(&fiq->waitq.lock);
Miklos Szeredif88996a2015-07-01 16:26:01 +0200419 kill_fasync(&fiq->fasync, SIGIO, POLL_IN);
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700420}
421
Miklos Szeredi7c352bd2005-09-09 13:10:39 -0700422static void request_wait_answer(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700423{
Miklos Szeredi4ce60812015-07-01 16:26:02 +0200424 struct fuse_iqueue *fiq = &fc->iq;
Miklos Szeredic4775262015-07-01 16:26:00 +0200425 int err;
426
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700427 if (!fc->no_interrupt) {
428 /* Any signal may interrupt this */
Miklos Szeredic4775262015-07-01 16:26:00 +0200429 err = wait_event_interruptible(req->waitq,
Miklos Szeredi33e14b42015-07-01 16:26:01 +0200430 test_bit(FR_FINISHED, &req->flags));
Miklos Szeredic4775262015-07-01 16:26:00 +0200431 if (!err)
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700432 return;
433
Miklos Szeredi825d6d32015-07-01 16:25:58 +0200434 set_bit(FR_INTERRUPTED, &req->flags);
Miklos Szeredi8f7bb362015-07-01 16:26:03 +0200435 /* matches barrier in fuse_dev_do_read() */
436 smp_mb__after_atomic();
Miklos Szeredi33e14b42015-07-01 16:26:01 +0200437 if (test_bit(FR_SENT, &req->flags))
Miklos Szeredi4ce60812015-07-01 16:26:02 +0200438 queue_interrupt(fiq, req);
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700439 }
440
Miklos Szeredi825d6d32015-07-01 16:25:58 +0200441 if (!test_bit(FR_FORCE, &req->flags)) {
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700442 /* Only fatal signals may interrupt this */
Al Viro7d3a07f2016-07-19 03:08:27 -0400443 err = wait_event_killable(req->waitq,
Miklos Szeredi33e14b42015-07-01 16:26:01 +0200444 test_bit(FR_FINISHED, &req->flags));
Miklos Szeredic4775262015-07-01 16:26:00 +0200445 if (!err)
Miklos Szeredia131de02007-10-16 23:31:04 -0700446 return;
447
Miklos Szeredi4ce60812015-07-01 16:26:02 +0200448 spin_lock(&fiq->waitq.lock);
Miklos Szeredia131de02007-10-16 23:31:04 -0700449 /* Request is not yet in userspace, bail out */
Miklos Szeredi33e14b42015-07-01 16:26:01 +0200450 if (test_bit(FR_PENDING, &req->flags)) {
Miklos Szeredia131de02007-10-16 23:31:04 -0700451 list_del(&req->list);
Miklos Szeredi4ce60812015-07-01 16:26:02 +0200452 spin_unlock(&fiq->waitq.lock);
Miklos Szeredia131de02007-10-16 23:31:04 -0700453 __fuse_put_request(req);
454 req->out.h.error = -EINTR;
455 return;
456 }
Miklos Szeredi4ce60812015-07-01 16:26:02 +0200457 spin_unlock(&fiq->waitq.lock);
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700458 }
Miklos Szeredi334f4852005-09-09 13:10:27 -0700459
Miklos Szeredia131de02007-10-16 23:31:04 -0700460 /*
461 * Either request is already in userspace, or it was forced.
462 * Wait it out.
463 */
Miklos Szeredi33e14b42015-07-01 16:26:01 +0200464 wait_event(req->waitq, test_bit(FR_FINISHED, &req->flags));
Miklos Szeredi334f4852005-09-09 13:10:27 -0700465}
466
Eric Wong6a4e9222013-02-04 13:04:44 +0000467static void __fuse_request_send(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700468{
Miklos Szeredie16714d2015-07-01 16:26:01 +0200469 struct fuse_iqueue *fiq = &fc->iq;
470
Miklos Szeredi825d6d32015-07-01 16:25:58 +0200471 BUG_ON(test_bit(FR_BACKGROUND, &req->flags));
Miklos Szeredi4ce60812015-07-01 16:26:02 +0200472 spin_lock(&fiq->waitq.lock);
Miklos Szeredie16714d2015-07-01 16:26:01 +0200473 if (!fiq->connected) {
Miklos Szeredi4ce60812015-07-01 16:26:02 +0200474 spin_unlock(&fiq->waitq.lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700475 req->out.h.error = -ENOTCONN;
Miklos Szeredic4775262015-07-01 16:26:00 +0200476 } else {
Miklos Szeredif88996a2015-07-01 16:26:01 +0200477 req->in.h.unique = fuse_get_unique(fiq);
478 queue_request(fiq, req);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700479 /* acquire extra reference, since request is still needed
480 after request_end() */
481 __fuse_get_request(req);
Miklos Szeredi4ce60812015-07-01 16:26:02 +0200482 spin_unlock(&fiq->waitq.lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700483
Miklos Szeredi7c352bd2005-09-09 13:10:39 -0700484 request_wait_answer(fc, req);
Miklos Szeredic4775262015-07-01 16:26:00 +0200485 /* Pairs with smp_wmb() in request_end() */
486 smp_rmb();
Miklos Szeredi334f4852005-09-09 13:10:27 -0700487 }
Miklos Szeredi334f4852005-09-09 13:10:27 -0700488}
Eric Wong6a4e9222013-02-04 13:04:44 +0000489
490void fuse_request_send(struct fuse_conn *fc, struct fuse_req *req)
491{
Miklos Szeredi825d6d32015-07-01 16:25:58 +0200492 __set_bit(FR_ISREPLY, &req->flags);
493 if (!test_bit(FR_WAITING, &req->flags)) {
494 __set_bit(FR_WAITING, &req->flags);
Miklos Szeredi5437f242015-07-01 16:25:56 +0200495 atomic_inc(&fc->num_waiting);
496 }
Eric Wong6a4e9222013-02-04 13:04:44 +0000497 __fuse_request_send(fc, req);
498}
Tejun Heo08cbf542009-04-14 10:54:53 +0900499EXPORT_SYMBOL_GPL(fuse_request_send);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700500
Miklos Szeredi21f62172015-01-06 10:45:35 +0100501static void fuse_adjust_compat(struct fuse_conn *fc, struct fuse_args *args)
502{
503 if (fc->minor < 4 && args->in.h.opcode == FUSE_STATFS)
504 args->out.args[0].size = FUSE_COMPAT_STATFS_SIZE;
505
506 if (fc->minor < 9) {
507 switch (args->in.h.opcode) {
508 case FUSE_LOOKUP:
509 case FUSE_CREATE:
510 case FUSE_MKNOD:
511 case FUSE_MKDIR:
512 case FUSE_SYMLINK:
513 case FUSE_LINK:
514 args->out.args[0].size = FUSE_COMPAT_ENTRY_OUT_SIZE;
515 break;
516 case FUSE_GETATTR:
517 case FUSE_SETATTR:
518 args->out.args[0].size = FUSE_COMPAT_ATTR_OUT_SIZE;
519 break;
520 }
521 }
522 if (fc->minor < 12) {
523 switch (args->in.h.opcode) {
524 case FUSE_CREATE:
525 args->in.args[0].size = sizeof(struct fuse_open_in);
526 break;
527 case FUSE_MKNOD:
528 args->in.args[0].size = FUSE_COMPAT_MKNOD_IN_SIZE;
529 break;
530 }
531 }
532}
533
Miklos Szeredi70781872014-12-12 09:49:05 +0100534ssize_t fuse_simple_request(struct fuse_conn *fc, struct fuse_args *args)
535{
536 struct fuse_req *req;
537 ssize_t ret;
538
539 req = fuse_get_req(fc, 0);
540 if (IS_ERR(req))
541 return PTR_ERR(req);
542
Miklos Szeredi21f62172015-01-06 10:45:35 +0100543 /* Needs to be done after fuse_get_req() so that fc->minor is valid */
544 fuse_adjust_compat(fc, args);
545
Miklos Szeredi70781872014-12-12 09:49:05 +0100546 req->in.h.opcode = args->in.h.opcode;
547 req->in.h.nodeid = args->in.h.nodeid;
548 req->in.numargs = args->in.numargs;
549 memcpy(req->in.args, args->in.args,
550 args->in.numargs * sizeof(struct fuse_in_arg));
551 req->out.argvar = args->out.argvar;
552 req->out.numargs = args->out.numargs;
553 memcpy(req->out.args, args->out.args,
554 args->out.numargs * sizeof(struct fuse_arg));
555 fuse_request_send(fc, req);
556 ret = req->out.h.error;
557 if (!ret && args->out.argvar) {
558 BUG_ON(args->out.numargs != 1);
559 ret = req->out.args[0].size;
560 }
561 fuse_put_request(fc, req);
562
563 return ret;
564}
565
Miklos Szeredif0139aa2015-07-01 16:25:57 +0200566/*
567 * Called under fc->lock
568 *
569 * fc->connected must have been checked previously
570 */
571void fuse_request_send_background_locked(struct fuse_conn *fc,
572 struct fuse_req *req)
Miklos Szeredid12def12008-02-06 01:38:39 -0800573{
Miklos Szeredi825d6d32015-07-01 16:25:58 +0200574 BUG_ON(!test_bit(FR_BACKGROUND, &req->flags));
575 if (!test_bit(FR_WAITING, &req->flags)) {
576 __set_bit(FR_WAITING, &req->flags);
Miklos Szeredi5437f242015-07-01 16:25:56 +0200577 atomic_inc(&fc->num_waiting);
578 }
Miklos Szeredi825d6d32015-07-01 16:25:58 +0200579 __set_bit(FR_ISREPLY, &req->flags);
Miklos Szeredid12def12008-02-06 01:38:39 -0800580 fc->num_background++;
Csaba Henk7a6d3c82009-07-01 17:28:41 -0700581 if (fc->num_background == fc->max_background)
Miklos Szeredid12def12008-02-06 01:38:39 -0800582 fc->blocked = 1;
Jan Kara7fbbe972017-04-12 12:24:41 +0200583 if (fc->num_background == fc->congestion_threshold && fc->sb) {
Jan Kara5f7f7542017-04-12 12:24:40 +0200584 set_bdi_congested(fc->sb->s_bdi, BLK_RW_SYNC);
585 set_bdi_congested(fc->sb->s_bdi, BLK_RW_ASYNC);
Miklos Szeredid12def12008-02-06 01:38:39 -0800586 }
587 list_add_tail(&req->list, &fc->bg_queue);
588 flush_bg_queue(fc);
589}
590
Miklos Szeredif0139aa2015-07-01 16:25:57 +0200591void fuse_request_send_background(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700592{
Miklos Szeredi42dc6212015-07-01 16:25:56 +0200593 BUG_ON(!req->end);
Miklos Szeredid7133112006-04-10 22:54:55 -0700594 spin_lock(&fc->lock);
Miklos Szeredi1e9a4ed2005-09-09 13:10:31 -0700595 if (fc->connected) {
Miklos Szeredif0139aa2015-07-01 16:25:57 +0200596 fuse_request_send_background_locked(fc, req);
Miklos Szeredid7133112006-04-10 22:54:55 -0700597 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700598 } else {
Miklos Szeredi42dc6212015-07-01 16:25:56 +0200599 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700600 req->out.h.error = -ENOTCONN;
Miklos Szeredi42dc6212015-07-01 16:25:56 +0200601 req->end(fc, req);
602 fuse_put_request(fc, req);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700603 }
604}
Tejun Heo08cbf542009-04-14 10:54:53 +0900605EXPORT_SYMBOL_GPL(fuse_request_send_background);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700606
Miklos Szeredi2d45ba32010-07-12 14:41:40 +0200607static int fuse_request_send_notify_reply(struct fuse_conn *fc,
608 struct fuse_req *req, u64 unique)
609{
610 int err = -ENODEV;
Miklos Szeredif88996a2015-07-01 16:26:01 +0200611 struct fuse_iqueue *fiq = &fc->iq;
Miklos Szeredi2d45ba32010-07-12 14:41:40 +0200612
Miklos Szeredi825d6d32015-07-01 16:25:58 +0200613 __clear_bit(FR_ISREPLY, &req->flags);
Miklos Szeredi2d45ba32010-07-12 14:41:40 +0200614 req->in.h.unique = unique;
Miklos Szeredi4ce60812015-07-01 16:26:02 +0200615 spin_lock(&fiq->waitq.lock);
Miklos Szeredie16714d2015-07-01 16:26:01 +0200616 if (fiq->connected) {
Miklos Szeredif88996a2015-07-01 16:26:01 +0200617 queue_request(fiq, req);
Miklos Szeredi2d45ba32010-07-12 14:41:40 +0200618 err = 0;
619 }
Miklos Szeredi4ce60812015-07-01 16:26:02 +0200620 spin_unlock(&fiq->waitq.lock);
Miklos Szeredi2d45ba32010-07-12 14:41:40 +0200621
622 return err;
623}
624
Anand V. Avati0b05b182012-08-19 08:53:23 -0400625void fuse_force_forget(struct file *file, u64 nodeid)
626{
Al Viro6131ffa2013-02-27 16:59:05 -0500627 struct inode *inode = file_inode(file);
Anand V. Avati0b05b182012-08-19 08:53:23 -0400628 struct fuse_conn *fc = get_fuse_conn(inode);
629 struct fuse_req *req;
630 struct fuse_forget_in inarg;
631
632 memset(&inarg, 0, sizeof(inarg));
633 inarg.nlookup = 1;
Maxim Patlasovb111c8c2012-10-26 19:48:30 +0400634 req = fuse_get_req_nofail_nopages(fc, file);
Anand V. Avati0b05b182012-08-19 08:53:23 -0400635 req->in.h.opcode = FUSE_FORGET;
636 req->in.h.nodeid = nodeid;
637 req->in.numargs = 1;
638 req->in.args[0].size = sizeof(inarg);
639 req->in.args[0].value = &inarg;
Miklos Szeredi825d6d32015-07-01 16:25:58 +0200640 __clear_bit(FR_ISREPLY, &req->flags);
Eric Wong6a4e9222013-02-04 13:04:44 +0000641 __fuse_request_send(fc, req);
642 /* ignore errors */
643 fuse_put_request(fc, req);
Anand V. Avati0b05b182012-08-19 08:53:23 -0400644}
645
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700646/*
Miklos Szeredi334f4852005-09-09 13:10:27 -0700647 * Lock the request. Up to the next unlock_request() there mustn't be
648 * anything that could cause a page-fault. If the request was already
Miklos Szeredif9a28422006-06-25 05:48:53 -0700649 * aborted bail out.
Miklos Szeredi334f4852005-09-09 13:10:27 -0700650 */
Miklos Szeredidc008092015-07-01 16:25:58 +0200651static int lock_request(struct fuse_req *req)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700652{
653 int err = 0;
654 if (req) {
Miklos Szeredidc008092015-07-01 16:25:58 +0200655 spin_lock(&req->waitq.lock);
Miklos Szeredi825d6d32015-07-01 16:25:58 +0200656 if (test_bit(FR_ABORTED, &req->flags))
Miklos Szeredi334f4852005-09-09 13:10:27 -0700657 err = -ENOENT;
658 else
Miklos Szeredi825d6d32015-07-01 16:25:58 +0200659 set_bit(FR_LOCKED, &req->flags);
Miklos Szeredidc008092015-07-01 16:25:58 +0200660 spin_unlock(&req->waitq.lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700661 }
662 return err;
663}
664
665/*
Miklos Szeredi0d8e84b2015-07-01 16:25:58 +0200666 * Unlock request. If it was aborted while locked, caller is responsible
667 * for unlocking and ending the request.
Miklos Szeredi334f4852005-09-09 13:10:27 -0700668 */
Miklos Szeredidc008092015-07-01 16:25:58 +0200669static int unlock_request(struct fuse_req *req)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700670{
Miklos Szeredi0d8e84b2015-07-01 16:25:58 +0200671 int err = 0;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700672 if (req) {
Miklos Szeredidc008092015-07-01 16:25:58 +0200673 spin_lock(&req->waitq.lock);
Miklos Szeredi825d6d32015-07-01 16:25:58 +0200674 if (test_bit(FR_ABORTED, &req->flags))
Miklos Szeredi0d8e84b2015-07-01 16:25:58 +0200675 err = -ENOENT;
676 else
Miklos Szeredi825d6d32015-07-01 16:25:58 +0200677 clear_bit(FR_LOCKED, &req->flags);
Miklos Szeredidc008092015-07-01 16:25:58 +0200678 spin_unlock(&req->waitq.lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700679 }
Miklos Szeredi0d8e84b2015-07-01 16:25:58 +0200680 return err;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700681}
682
683struct fuse_copy_state {
684 int write;
685 struct fuse_req *req;
Al Viro6c09e942015-04-03 22:06:08 -0400686 struct iov_iter *iter;
Miklos Szeredidd3bb142010-05-25 15:06:06 +0200687 struct pipe_buffer *pipebufs;
688 struct pipe_buffer *currbuf;
689 struct pipe_inode_info *pipe;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700690 unsigned long nr_segs;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700691 struct page *pg;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700692 unsigned len;
Miklos Szeredic55a01d2014-07-07 15:28:51 +0200693 unsigned offset;
Miklos Szeredice534fb2010-05-25 15:06:07 +0200694 unsigned move_pages:1;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700695};
696
Miklos Szeredidc008092015-07-01 16:25:58 +0200697static void fuse_copy_init(struct fuse_copy_state *cs, int write,
Al Viro6c09e942015-04-03 22:06:08 -0400698 struct iov_iter *iter)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700699{
700 memset(cs, 0, sizeof(*cs));
701 cs->write = write;
Al Viro6c09e942015-04-03 22:06:08 -0400702 cs->iter = iter;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700703}
704
705/* Unmap and put previous page of userspace buffer */
Miklos Szeredi8bfc0162006-01-16 22:14:28 -0800706static void fuse_copy_finish(struct fuse_copy_state *cs)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700707{
Miklos Szeredidd3bb142010-05-25 15:06:06 +0200708 if (cs->currbuf) {
709 struct pipe_buffer *buf = cs->currbuf;
710
Miklos Szeredic55a01d2014-07-07 15:28:51 +0200711 if (cs->write)
Miklos Szeredic3021622010-05-25 15:06:07 +0200712 buf->len = PAGE_SIZE - cs->len;
Miklos Szeredidd3bb142010-05-25 15:06:06 +0200713 cs->currbuf = NULL;
Miklos Szeredic55a01d2014-07-07 15:28:51 +0200714 } else if (cs->pg) {
Miklos Szeredi334f4852005-09-09 13:10:27 -0700715 if (cs->write) {
716 flush_dcache_page(cs->pg);
717 set_page_dirty_lock(cs->pg);
718 }
719 put_page(cs->pg);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700720 }
Miklos Szeredic55a01d2014-07-07 15:28:51 +0200721 cs->pg = NULL;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700722}
723
724/*
725 * Get another pagefull of userspace buffer, and map it to kernel
726 * address space, and lock request
727 */
728static int fuse_copy_fill(struct fuse_copy_state *cs)
729{
Miklos Szeredic55a01d2014-07-07 15:28:51 +0200730 struct page *page;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700731 int err;
732
Miklos Szeredidc008092015-07-01 16:25:58 +0200733 err = unlock_request(cs->req);
Miklos Szeredi0d8e84b2015-07-01 16:25:58 +0200734 if (err)
735 return err;
736
Miklos Szeredi334f4852005-09-09 13:10:27 -0700737 fuse_copy_finish(cs);
Miklos Szeredidd3bb142010-05-25 15:06:06 +0200738 if (cs->pipebufs) {
739 struct pipe_buffer *buf = cs->pipebufs;
740
Miklos Szeredic3021622010-05-25 15:06:07 +0200741 if (!cs->write) {
Miklos Szeredifba597d2016-09-27 10:45:12 +0200742 err = pipe_buf_confirm(cs->pipe, buf);
Miklos Szeredic3021622010-05-25 15:06:07 +0200743 if (err)
744 return err;
Miklos Szeredidd3bb142010-05-25 15:06:06 +0200745
Miklos Szeredic3021622010-05-25 15:06:07 +0200746 BUG_ON(!cs->nr_segs);
747 cs->currbuf = buf;
Miklos Szeredic55a01d2014-07-07 15:28:51 +0200748 cs->pg = buf->page;
749 cs->offset = buf->offset;
Miklos Szeredic3021622010-05-25 15:06:07 +0200750 cs->len = buf->len;
Miklos Szeredic3021622010-05-25 15:06:07 +0200751 cs->pipebufs++;
752 cs->nr_segs--;
753 } else {
Miklos Szeredic3021622010-05-25 15:06:07 +0200754 if (cs->nr_segs == cs->pipe->buffers)
755 return -EIO;
756
757 page = alloc_page(GFP_HIGHUSER);
758 if (!page)
759 return -ENOMEM;
760
761 buf->page = page;
762 buf->offset = 0;
763 buf->len = 0;
764
765 cs->currbuf = buf;
Miklos Szeredic55a01d2014-07-07 15:28:51 +0200766 cs->pg = page;
767 cs->offset = 0;
Miklos Szeredic3021622010-05-25 15:06:07 +0200768 cs->len = PAGE_SIZE;
769 cs->pipebufs++;
770 cs->nr_segs++;
771 }
Miklos Szeredidd3bb142010-05-25 15:06:06 +0200772 } else {
Al Viro6c09e942015-04-03 22:06:08 -0400773 size_t off;
774 err = iov_iter_get_pages(cs->iter, &page, PAGE_SIZE, 1, &off);
Miklos Szeredidd3bb142010-05-25 15:06:06 +0200775 if (err < 0)
776 return err;
Al Viro6c09e942015-04-03 22:06:08 -0400777 BUG_ON(!err);
778 cs->len = err;
779 cs->offset = off;
Miklos Szeredic55a01d2014-07-07 15:28:51 +0200780 cs->pg = page;
Al Viro6c09e942015-04-03 22:06:08 -0400781 iov_iter_advance(cs->iter, err);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700782 }
Miklos Szeredi334f4852005-09-09 13:10:27 -0700783
Miklos Szeredidc008092015-07-01 16:25:58 +0200784 return lock_request(cs->req);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700785}
786
787/* Do as much copy to/from userspace buffer as we can */
Miklos Szeredi8bfc0162006-01-16 22:14:28 -0800788static int fuse_copy_do(struct fuse_copy_state *cs, void **val, unsigned *size)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700789{
790 unsigned ncpy = min(*size, cs->len);
791 if (val) {
Miklos Szeredic55a01d2014-07-07 15:28:51 +0200792 void *pgaddr = kmap_atomic(cs->pg);
793 void *buf = pgaddr + cs->offset;
794
Miklos Szeredi334f4852005-09-09 13:10:27 -0700795 if (cs->write)
Miklos Szeredic55a01d2014-07-07 15:28:51 +0200796 memcpy(buf, *val, ncpy);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700797 else
Miklos Szeredic55a01d2014-07-07 15:28:51 +0200798 memcpy(*val, buf, ncpy);
799
800 kunmap_atomic(pgaddr);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700801 *val += ncpy;
802 }
803 *size -= ncpy;
804 cs->len -= ncpy;
Miklos Szeredic55a01d2014-07-07 15:28:51 +0200805 cs->offset += ncpy;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700806 return ncpy;
807}
808
Miklos Szeredice534fb2010-05-25 15:06:07 +0200809static int fuse_check_page(struct page *page)
810{
811 if (page_mapcount(page) ||
812 page->mapping != NULL ||
813 page_count(page) != 1 ||
814 (page->flags & PAGE_FLAGS_CHECK_AT_PREP &
815 ~(1 << PG_locked |
816 1 << PG_referenced |
817 1 << PG_uptodate |
818 1 << PG_lru |
819 1 << PG_active |
820 1 << PG_reclaim))) {
821 printk(KERN_WARNING "fuse: trying to steal weird page\n");
822 printk(KERN_WARNING " page=%p index=%li flags=%08lx, count=%i, mapcount=%i, mapping=%p\n", page, page->index, page->flags, page_count(page), page_mapcount(page), page->mapping);
823 return 1;
824 }
825 return 0;
826}
827
828static int fuse_try_move_page(struct fuse_copy_state *cs, struct page **pagep)
829{
830 int err;
831 struct page *oldpage = *pagep;
832 struct page *newpage;
833 struct pipe_buffer *buf = cs->pipebufs;
Miklos Szeredice534fb2010-05-25 15:06:07 +0200834
Miklos Szeredidc008092015-07-01 16:25:58 +0200835 err = unlock_request(cs->req);
Miklos Szeredi0d8e84b2015-07-01 16:25:58 +0200836 if (err)
837 return err;
838
Miklos Szeredice534fb2010-05-25 15:06:07 +0200839 fuse_copy_finish(cs);
840
Miklos Szeredifba597d2016-09-27 10:45:12 +0200841 err = pipe_buf_confirm(cs->pipe, buf);
Miklos Szeredice534fb2010-05-25 15:06:07 +0200842 if (err)
843 return err;
844
845 BUG_ON(!cs->nr_segs);
846 cs->currbuf = buf;
847 cs->len = buf->len;
848 cs->pipebufs++;
849 cs->nr_segs--;
850
851 if (cs->len != PAGE_SIZE)
852 goto out_fallback;
853
Miklos Szeredica76f5b2016-09-27 10:45:12 +0200854 if (pipe_buf_steal(cs->pipe, buf) != 0)
Miklos Szeredice534fb2010-05-25 15:06:07 +0200855 goto out_fallback;
856
857 newpage = buf->page;
858
Miklos Szerediaa991b32015-02-26 11:45:47 +0100859 if (!PageUptodate(newpage))
860 SetPageUptodate(newpage);
Miklos Szeredice534fb2010-05-25 15:06:07 +0200861
862 ClearPageMappedToDisk(newpage);
863
864 if (fuse_check_page(newpage) != 0)
865 goto out_fallback_unlock;
866
Miklos Szeredice534fb2010-05-25 15:06:07 +0200867 /*
868 * This is a new and locked page, it shouldn't be mapped or
869 * have any special flags on it
870 */
871 if (WARN_ON(page_mapped(oldpage)))
872 goto out_fallback_unlock;
873 if (WARN_ON(page_has_private(oldpage)))
874 goto out_fallback_unlock;
875 if (WARN_ON(PageDirty(oldpage) || PageWriteback(oldpage)))
876 goto out_fallback_unlock;
877 if (WARN_ON(PageMlocked(oldpage)))
878 goto out_fallback_unlock;
879
Miklos Szeredief6a3c62011-03-22 16:30:52 -0700880 err = replace_page_cache_page(oldpage, newpage, GFP_KERNEL);
Miklos Szeredice534fb2010-05-25 15:06:07 +0200881 if (err) {
Miklos Szeredief6a3c62011-03-22 16:30:52 -0700882 unlock_page(newpage);
883 return err;
Miklos Szeredice534fb2010-05-25 15:06:07 +0200884 }
Miklos Szeredief6a3c62011-03-22 16:30:52 -0700885
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300886 get_page(newpage);
Miklos Szeredice534fb2010-05-25 15:06:07 +0200887
888 if (!(buf->flags & PIPE_BUF_FLAG_LRU))
889 lru_cache_add_file(newpage);
890
891 err = 0;
Miklos Szeredidc008092015-07-01 16:25:58 +0200892 spin_lock(&cs->req->waitq.lock);
Miklos Szeredi825d6d32015-07-01 16:25:58 +0200893 if (test_bit(FR_ABORTED, &cs->req->flags))
Miklos Szeredice534fb2010-05-25 15:06:07 +0200894 err = -ENOENT;
895 else
896 *pagep = newpage;
Miklos Szeredidc008092015-07-01 16:25:58 +0200897 spin_unlock(&cs->req->waitq.lock);
Miklos Szeredice534fb2010-05-25 15:06:07 +0200898
899 if (err) {
900 unlock_page(newpage);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300901 put_page(newpage);
Miklos Szeredice534fb2010-05-25 15:06:07 +0200902 return err;
903 }
904
905 unlock_page(oldpage);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300906 put_page(oldpage);
Miklos Szeredice534fb2010-05-25 15:06:07 +0200907 cs->len = 0;
908
909 return 0;
910
911out_fallback_unlock:
912 unlock_page(newpage);
913out_fallback:
Miklos Szeredic55a01d2014-07-07 15:28:51 +0200914 cs->pg = buf->page;
915 cs->offset = buf->offset;
Miklos Szeredice534fb2010-05-25 15:06:07 +0200916
Miklos Szeredidc008092015-07-01 16:25:58 +0200917 err = lock_request(cs->req);
Miklos Szeredice534fb2010-05-25 15:06:07 +0200918 if (err)
919 return err;
920
921 return 1;
922}
923
Miklos Szeredic3021622010-05-25 15:06:07 +0200924static int fuse_ref_page(struct fuse_copy_state *cs, struct page *page,
925 unsigned offset, unsigned count)
926{
927 struct pipe_buffer *buf;
Miklos Szeredi0d8e84b2015-07-01 16:25:58 +0200928 int err;
Miklos Szeredic3021622010-05-25 15:06:07 +0200929
930 if (cs->nr_segs == cs->pipe->buffers)
931 return -EIO;
932
Miklos Szeredidc008092015-07-01 16:25:58 +0200933 err = unlock_request(cs->req);
Miklos Szeredi0d8e84b2015-07-01 16:25:58 +0200934 if (err)
935 return err;
936
Miklos Szeredic3021622010-05-25 15:06:07 +0200937 fuse_copy_finish(cs);
938
939 buf = cs->pipebufs;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300940 get_page(page);
Miklos Szeredic3021622010-05-25 15:06:07 +0200941 buf->page = page;
942 buf->offset = offset;
943 buf->len = count;
944
945 cs->pipebufs++;
946 cs->nr_segs++;
947 cs->len = 0;
948
949 return 0;
950}
951
Miklos Szeredi334f4852005-09-09 13:10:27 -0700952/*
953 * Copy a page in the request to/from the userspace buffer. Must be
954 * done atomically
955 */
Miklos Szeredice534fb2010-05-25 15:06:07 +0200956static int fuse_copy_page(struct fuse_copy_state *cs, struct page **pagep,
Miklos Szeredi8bfc0162006-01-16 22:14:28 -0800957 unsigned offset, unsigned count, int zeroing)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700958{
Miklos Szeredice534fb2010-05-25 15:06:07 +0200959 int err;
960 struct page *page = *pagep;
961
Miklos Szeredib6777c42010-10-26 14:22:27 -0700962 if (page && zeroing && count < PAGE_SIZE)
963 clear_highpage(page);
964
Miklos Szeredi334f4852005-09-09 13:10:27 -0700965 while (count) {
Miklos Szeredic3021622010-05-25 15:06:07 +0200966 if (cs->write && cs->pipebufs && page) {
967 return fuse_ref_page(cs, page, offset, count);
968 } else if (!cs->len) {
Miklos Szeredice534fb2010-05-25 15:06:07 +0200969 if (cs->move_pages && page &&
970 offset == 0 && count == PAGE_SIZE) {
971 err = fuse_try_move_page(cs, pagep);
972 if (err <= 0)
973 return err;
974 } else {
975 err = fuse_copy_fill(cs);
976 if (err)
977 return err;
978 }
Miklos Szeredi1729a162008-11-26 12:03:54 +0100979 }
Miklos Szeredi334f4852005-09-09 13:10:27 -0700980 if (page) {
Cong Wang2408f6e2011-11-25 23:14:30 +0800981 void *mapaddr = kmap_atomic(page);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700982 void *buf = mapaddr + offset;
983 offset += fuse_copy_do(cs, &buf, &count);
Cong Wang2408f6e2011-11-25 23:14:30 +0800984 kunmap_atomic(mapaddr);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700985 } else
986 offset += fuse_copy_do(cs, NULL, &count);
987 }
988 if (page && !cs->write)
989 flush_dcache_page(page);
990 return 0;
991}
992
993/* Copy pages in the request to/from userspace buffer */
994static int fuse_copy_pages(struct fuse_copy_state *cs, unsigned nbytes,
995 int zeroing)
996{
997 unsigned i;
998 struct fuse_req *req = cs->req;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700999
1000 for (i = 0; i < req->num_pages && (nbytes || zeroing); i++) {
Miklos Szeredice534fb2010-05-25 15:06:07 +02001001 int err;
Maxim Patlasov85f40ae2012-10-26 19:49:33 +04001002 unsigned offset = req->page_descs[i].offset;
1003 unsigned count = min(nbytes, req->page_descs[i].length);
Miklos Szeredice534fb2010-05-25 15:06:07 +02001004
1005 err = fuse_copy_page(cs, &req->pages[i], offset, count,
1006 zeroing);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001007 if (err)
1008 return err;
1009
1010 nbytes -= count;
Miklos Szeredi334f4852005-09-09 13:10:27 -07001011 }
1012 return 0;
1013}
1014
1015/* Copy a single argument in the request to/from userspace buffer */
1016static int fuse_copy_one(struct fuse_copy_state *cs, void *val, unsigned size)
1017{
1018 while (size) {
Miklos Szeredi1729a162008-11-26 12:03:54 +01001019 if (!cs->len) {
1020 int err = fuse_copy_fill(cs);
1021 if (err)
1022 return err;
1023 }
Miklos Szeredi334f4852005-09-09 13:10:27 -07001024 fuse_copy_do(cs, &val, &size);
1025 }
1026 return 0;
1027}
1028
1029/* Copy request arguments to/from userspace buffer */
1030static int fuse_copy_args(struct fuse_copy_state *cs, unsigned numargs,
1031 unsigned argpages, struct fuse_arg *args,
1032 int zeroing)
1033{
1034 int err = 0;
1035 unsigned i;
1036
1037 for (i = 0; !err && i < numargs; i++) {
1038 struct fuse_arg *arg = &args[i];
1039 if (i == numargs - 1 && argpages)
1040 err = fuse_copy_pages(cs, arg->size, zeroing);
1041 else
1042 err = fuse_copy_one(cs, arg->value, arg->size);
1043 }
1044 return err;
1045}
1046
Miklos Szeredif88996a2015-07-01 16:26:01 +02001047static int forget_pending(struct fuse_iqueue *fiq)
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001048{
Miklos Szeredif88996a2015-07-01 16:26:01 +02001049 return fiq->forget_list_head.next != NULL;
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001050}
1051
Miklos Szeredif88996a2015-07-01 16:26:01 +02001052static int request_pending(struct fuse_iqueue *fiq)
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001053{
Miklos Szeredif88996a2015-07-01 16:26:01 +02001054 return !list_empty(&fiq->pending) || !list_empty(&fiq->interrupts) ||
1055 forget_pending(fiq);
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001056}
1057
Miklos Szeredi334f4852005-09-09 13:10:27 -07001058/*
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001059 * Transfer an interrupt request to userspace
1060 *
1061 * Unlike other requests this is assembled on demand, without a need
1062 * to allocate a separate fuse_req structure.
1063 *
Miklos Szeredifd22d622015-07-01 16:26:03 +02001064 * Called with fiq->waitq.lock held, releases it
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001065 */
Miklos Szeredifd22d622015-07-01 16:26:03 +02001066static int fuse_read_interrupt(struct fuse_iqueue *fiq,
1067 struct fuse_copy_state *cs,
Miklos Szeredic3021622010-05-25 15:06:07 +02001068 size_t nbytes, struct fuse_req *req)
Miklos Szeredifd22d622015-07-01 16:26:03 +02001069__releases(fiq->waitq.lock)
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001070{
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001071 struct fuse_in_header ih;
1072 struct fuse_interrupt_in arg;
1073 unsigned reqsize = sizeof(ih) + sizeof(arg);
1074 int err;
1075
1076 list_del_init(&req->intr_entry);
Miklos Szeredi4ce60812015-07-01 16:26:02 +02001077 req->intr_unique = fuse_get_unique(fiq);
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001078 memset(&ih, 0, sizeof(ih));
1079 memset(&arg, 0, sizeof(arg));
1080 ih.len = reqsize;
1081 ih.opcode = FUSE_INTERRUPT;
1082 ih.unique = req->intr_unique;
1083 arg.unique = req->in.h.unique;
1084
Miklos Szeredi4ce60812015-07-01 16:26:02 +02001085 spin_unlock(&fiq->waitq.lock);
Miklos Szeredic3021622010-05-25 15:06:07 +02001086 if (nbytes < reqsize)
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001087 return -EINVAL;
1088
Miklos Szeredic3021622010-05-25 15:06:07 +02001089 err = fuse_copy_one(cs, &ih, sizeof(ih));
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001090 if (!err)
Miklos Szeredic3021622010-05-25 15:06:07 +02001091 err = fuse_copy_one(cs, &arg, sizeof(arg));
1092 fuse_copy_finish(cs);
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001093
1094 return err ? err : reqsize;
1095}
1096
Miklos Szeredif88996a2015-07-01 16:26:01 +02001097static struct fuse_forget_link *dequeue_forget(struct fuse_iqueue *fiq,
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001098 unsigned max,
1099 unsigned *countp)
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001100{
Miklos Szeredif88996a2015-07-01 16:26:01 +02001101 struct fuse_forget_link *head = fiq->forget_list_head.next;
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001102 struct fuse_forget_link **newhead = &head;
1103 unsigned count;
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001104
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001105 for (count = 0; *newhead != NULL && count < max; count++)
1106 newhead = &(*newhead)->next;
1107
Miklos Szeredif88996a2015-07-01 16:26:01 +02001108 fiq->forget_list_head.next = *newhead;
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001109 *newhead = NULL;
Miklos Szeredif88996a2015-07-01 16:26:01 +02001110 if (fiq->forget_list_head.next == NULL)
1111 fiq->forget_list_tail = &fiq->forget_list_head;
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001112
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001113 if (countp != NULL)
1114 *countp = count;
1115
1116 return head;
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001117}
1118
Miklos Szeredifd22d622015-07-01 16:26:03 +02001119static int fuse_read_single_forget(struct fuse_iqueue *fiq,
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001120 struct fuse_copy_state *cs,
1121 size_t nbytes)
Miklos Szeredifd22d622015-07-01 16:26:03 +02001122__releases(fiq->waitq.lock)
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001123{
1124 int err;
Miklos Szeredif88996a2015-07-01 16:26:01 +02001125 struct fuse_forget_link *forget = dequeue_forget(fiq, 1, NULL);
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001126 struct fuse_forget_in arg = {
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001127 .nlookup = forget->forget_one.nlookup,
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001128 };
1129 struct fuse_in_header ih = {
1130 .opcode = FUSE_FORGET,
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001131 .nodeid = forget->forget_one.nodeid,
Miklos Szeredif88996a2015-07-01 16:26:01 +02001132 .unique = fuse_get_unique(fiq),
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001133 .len = sizeof(ih) + sizeof(arg),
1134 };
1135
Miklos Szeredi4ce60812015-07-01 16:26:02 +02001136 spin_unlock(&fiq->waitq.lock);
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001137 kfree(forget);
1138 if (nbytes < ih.len)
1139 return -EINVAL;
1140
1141 err = fuse_copy_one(cs, &ih, sizeof(ih));
1142 if (!err)
1143 err = fuse_copy_one(cs, &arg, sizeof(arg));
1144 fuse_copy_finish(cs);
1145
1146 if (err)
1147 return err;
1148
1149 return ih.len;
1150}
1151
Miklos Szeredifd22d622015-07-01 16:26:03 +02001152static int fuse_read_batch_forget(struct fuse_iqueue *fiq,
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001153 struct fuse_copy_state *cs, size_t nbytes)
Miklos Szeredifd22d622015-07-01 16:26:03 +02001154__releases(fiq->waitq.lock)
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001155{
1156 int err;
1157 unsigned max_forgets;
1158 unsigned count;
1159 struct fuse_forget_link *head;
1160 struct fuse_batch_forget_in arg = { .count = 0 };
1161 struct fuse_in_header ih = {
1162 .opcode = FUSE_BATCH_FORGET,
Miklos Szeredif88996a2015-07-01 16:26:01 +02001163 .unique = fuse_get_unique(fiq),
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001164 .len = sizeof(ih) + sizeof(arg),
1165 };
1166
1167 if (nbytes < ih.len) {
Miklos Szeredi4ce60812015-07-01 16:26:02 +02001168 spin_unlock(&fiq->waitq.lock);
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001169 return -EINVAL;
1170 }
1171
1172 max_forgets = (nbytes - ih.len) / sizeof(struct fuse_forget_one);
Miklos Szeredif88996a2015-07-01 16:26:01 +02001173 head = dequeue_forget(fiq, max_forgets, &count);
Miklos Szeredi4ce60812015-07-01 16:26:02 +02001174 spin_unlock(&fiq->waitq.lock);
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001175
1176 arg.count = count;
1177 ih.len += count * sizeof(struct fuse_forget_one);
1178 err = fuse_copy_one(cs, &ih, sizeof(ih));
1179 if (!err)
1180 err = fuse_copy_one(cs, &arg, sizeof(arg));
1181
1182 while (head) {
1183 struct fuse_forget_link *forget = head;
1184
1185 if (!err) {
1186 err = fuse_copy_one(cs, &forget->forget_one,
1187 sizeof(forget->forget_one));
1188 }
1189 head = forget->next;
1190 kfree(forget);
1191 }
1192
1193 fuse_copy_finish(cs);
1194
1195 if (err)
1196 return err;
1197
1198 return ih.len;
1199}
1200
Miklos Szeredifd22d622015-07-01 16:26:03 +02001201static int fuse_read_forget(struct fuse_conn *fc, struct fuse_iqueue *fiq,
1202 struct fuse_copy_state *cs,
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001203 size_t nbytes)
Miklos Szeredifd22d622015-07-01 16:26:03 +02001204__releases(fiq->waitq.lock)
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001205{
Miklos Szeredif88996a2015-07-01 16:26:01 +02001206 if (fc->minor < 16 || fiq->forget_list_head.next->next == NULL)
Miklos Szeredifd22d622015-07-01 16:26:03 +02001207 return fuse_read_single_forget(fiq, cs, nbytes);
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001208 else
Miklos Szeredifd22d622015-07-01 16:26:03 +02001209 return fuse_read_batch_forget(fiq, cs, nbytes);
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001210}
1211
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001212/*
Miklos Szeredi334f4852005-09-09 13:10:27 -07001213 * Read a single request into the userspace filesystem's buffer. This
1214 * function waits until a request is available, then removes it from
1215 * the pending list and copies request data to userspace buffer. If
Miklos Szeredif9a28422006-06-25 05:48:53 -07001216 * no reply is needed (FORGET) or request has been aborted or there
1217 * was an error during the copying then it's finished by calling
Miklos Szeredi334f4852005-09-09 13:10:27 -07001218 * request_end(). Otherwise add it to the processing list, and set
1219 * the 'sent' flag.
1220 */
Miklos Szeredic36960462015-07-01 16:26:09 +02001221static ssize_t fuse_dev_do_read(struct fuse_dev *fud, struct file *file,
Miklos Szeredic3021622010-05-25 15:06:07 +02001222 struct fuse_copy_state *cs, size_t nbytes)
Miklos Szeredi334f4852005-09-09 13:10:27 -07001223{
Miklos Szeredi82cbdcd2015-07-01 16:26:05 +02001224 ssize_t err;
Miklos Szeredic36960462015-07-01 16:26:09 +02001225 struct fuse_conn *fc = fud->fc;
Miklos Szeredif88996a2015-07-01 16:26:01 +02001226 struct fuse_iqueue *fiq = &fc->iq;
Miklos Szeredic36960462015-07-01 16:26:09 +02001227 struct fuse_pqueue *fpq = &fud->pq;
Miklos Szeredi334f4852005-09-09 13:10:27 -07001228 struct fuse_req *req;
1229 struct fuse_in *in;
Miklos Szeredi334f4852005-09-09 13:10:27 -07001230 unsigned reqsize;
1231
Miklos Szeredi1d3d7522006-01-06 00:19:40 -08001232 restart:
Miklos Szeredi4ce60812015-07-01 16:26:02 +02001233 spin_lock(&fiq->waitq.lock);
Jeff Dikee5ac1d12006-04-10 22:54:53 -07001234 err = -EAGAIN;
Miklos Szeredie16714d2015-07-01 16:26:01 +02001235 if ((file->f_flags & O_NONBLOCK) && fiq->connected &&
Miklos Szeredif88996a2015-07-01 16:26:01 +02001236 !request_pending(fiq))
Jeff Dikee5ac1d12006-04-10 22:54:53 -07001237 goto err_unlock;
1238
Miklos Szeredi52509212015-07-01 16:26:03 +02001239 err = wait_event_interruptible_exclusive_locked(fiq->waitq,
1240 !fiq->connected || request_pending(fiq));
1241 if (err)
1242 goto err_unlock;
1243
Szymon Lukasz3b7008b2017-11-09 21:23:35 +01001244 if (!fiq->connected) {
1245 err = (fc->aborted && fc->abort_err) ? -ECONNABORTED : -ENODEV;
Miklos Szeredi334f4852005-09-09 13:10:27 -07001246 goto err_unlock;
Szymon Lukasz3b7008b2017-11-09 21:23:35 +01001247 }
Miklos Szeredi334f4852005-09-09 13:10:27 -07001248
Miklos Szeredif88996a2015-07-01 16:26:01 +02001249 if (!list_empty(&fiq->interrupts)) {
1250 req = list_entry(fiq->interrupts.next, struct fuse_req,
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001251 intr_entry);
Miklos Szeredifd22d622015-07-01 16:26:03 +02001252 return fuse_read_interrupt(fiq, cs, nbytes, req);
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001253 }
1254
Miklos Szeredif88996a2015-07-01 16:26:01 +02001255 if (forget_pending(fiq)) {
1256 if (list_empty(&fiq->pending) || fiq->forget_batch-- > 0)
Miklos Szeredifd22d622015-07-01 16:26:03 +02001257 return fuse_read_forget(fc, fiq, cs, nbytes);
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001258
Miklos Szeredif88996a2015-07-01 16:26:01 +02001259 if (fiq->forget_batch <= -8)
1260 fiq->forget_batch = 16;
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001261 }
1262
Miklos Szeredif88996a2015-07-01 16:26:01 +02001263 req = list_entry(fiq->pending.next, struct fuse_req, list);
Miklos Szeredi33e14b42015-07-01 16:26:01 +02001264 clear_bit(FR_PENDING, &req->flags);
Miklos Szeredief759252015-07-01 16:26:02 +02001265 list_del_init(&req->list);
Miklos Szeredi4ce60812015-07-01 16:26:02 +02001266 spin_unlock(&fiq->waitq.lock);
1267
Miklos Szeredi334f4852005-09-09 13:10:27 -07001268 in = &req->in;
Miklos Szeredi1d3d7522006-01-06 00:19:40 -08001269 reqsize = in->h.len;
Miklos Szeredi5d6d3a32017-09-12 16:57:53 +02001270
Miklos Szeredi1d3d7522006-01-06 00:19:40 -08001271 /* If request is too large, reply with an error and restart the read */
Miklos Szeredic3021622010-05-25 15:06:07 +02001272 if (nbytes < reqsize) {
Miklos Szeredi1d3d7522006-01-06 00:19:40 -08001273 req->out.h.error = -EIO;
1274 /* SETXATTR is special, since it may contain too large data */
1275 if (in->h.opcode == FUSE_SETXATTR)
1276 req->out.h.error = -E2BIG;
1277 request_end(fc, req);
1278 goto restart;
Miklos Szeredi334f4852005-09-09 13:10:27 -07001279 }
Miklos Szeredi45a91cb2015-07-01 16:26:06 +02001280 spin_lock(&fpq->lock);
Miklos Szeredi82cbdcd2015-07-01 16:26:05 +02001281 list_add(&req->list, &fpq->io);
Miklos Szeredi45a91cb2015-07-01 16:26:06 +02001282 spin_unlock(&fpq->lock);
Miklos Szeredic3021622010-05-25 15:06:07 +02001283 cs->req = req;
1284 err = fuse_copy_one(cs, &in->h, sizeof(in->h));
Miklos Szeredi1d3d7522006-01-06 00:19:40 -08001285 if (!err)
Miklos Szeredic3021622010-05-25 15:06:07 +02001286 err = fuse_copy_args(cs, in->numargs, in->argpages,
Miklos Szeredi1d3d7522006-01-06 00:19:40 -08001287 (struct fuse_arg *) in->args, 0);
Miklos Szeredic3021622010-05-25 15:06:07 +02001288 fuse_copy_finish(cs);
Miklos Szeredi45a91cb2015-07-01 16:26:06 +02001289 spin_lock(&fpq->lock);
Miklos Szeredi825d6d32015-07-01 16:25:58 +02001290 clear_bit(FR_LOCKED, &req->flags);
Miklos Szeredie96edd92015-07-01 16:26:04 +02001291 if (!fpq->connected) {
Szymon Lukasz3b7008b2017-11-09 21:23:35 +01001292 err = (fc->aborted && fc->abort_err) ? -ECONNABORTED : -ENODEV;
Miklos Szeredi82cbdcd2015-07-01 16:26:05 +02001293 goto out_end;
Miklos Szeredic9c9d7d2007-10-16 23:31:05 -07001294 }
Miklos Szeredi334f4852005-09-09 13:10:27 -07001295 if (err) {
Miklos Szeredic9c9d7d2007-10-16 23:31:05 -07001296 req->out.h.error = -EIO;
Miklos Szeredi82cbdcd2015-07-01 16:26:05 +02001297 goto out_end;
Miklos Szeredi334f4852005-09-09 13:10:27 -07001298 }
Miklos Szeredi825d6d32015-07-01 16:25:58 +02001299 if (!test_bit(FR_ISREPLY, &req->flags)) {
Miklos Szeredi82cbdcd2015-07-01 16:26:05 +02001300 err = reqsize;
1301 goto out_end;
Miklos Szeredi334f4852005-09-09 13:10:27 -07001302 }
Miklos Szeredi82cbdcd2015-07-01 16:26:05 +02001303 list_move_tail(&req->list, &fpq->processing);
Miklos Szeredi45a91cb2015-07-01 16:26:06 +02001304 spin_unlock(&fpq->lock);
Miklos Szeredi82cbdcd2015-07-01 16:26:05 +02001305 set_bit(FR_SENT, &req->flags);
1306 /* matches barrier in request_wait_answer() */
1307 smp_mb__after_atomic();
1308 if (test_bit(FR_INTERRUPTED, &req->flags))
1309 queue_interrupt(fiq, req);
Miklos Szeredi82cbdcd2015-07-01 16:26:05 +02001310
Miklos Szeredi334f4852005-09-09 13:10:27 -07001311 return reqsize;
1312
Miklos Szeredi82cbdcd2015-07-01 16:26:05 +02001313out_end:
Miklos Szeredi77cd9d42015-07-01 16:26:06 +02001314 if (!test_bit(FR_PRIVATE, &req->flags))
1315 list_del_init(&req->list);
Miklos Szeredi45a91cb2015-07-01 16:26:06 +02001316 spin_unlock(&fpq->lock);
Miklos Szeredi82cbdcd2015-07-01 16:26:05 +02001317 request_end(fc, req);
1318 return err;
1319
Miklos Szeredi334f4852005-09-09 13:10:27 -07001320 err_unlock:
Miklos Szeredi4ce60812015-07-01 16:26:02 +02001321 spin_unlock(&fiq->waitq.lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001322 return err;
1323}
1324
Tom Van Braeckel94e4fe22015-01-12 05:22:16 +01001325static int fuse_dev_open(struct inode *inode, struct file *file)
1326{
1327 /*
1328 * The fuse device's file's private_data is used to hold
1329 * the fuse_conn(ection) when it is mounted, and is used to
1330 * keep track of whether the file has been mounted already.
1331 */
1332 file->private_data = NULL;
1333 return 0;
1334}
1335
Al Virofbdbacc2015-04-03 21:53:39 -04001336static ssize_t fuse_dev_read(struct kiocb *iocb, struct iov_iter *to)
Miklos Szeredic3021622010-05-25 15:06:07 +02001337{
1338 struct fuse_copy_state cs;
1339 struct file *file = iocb->ki_filp;
Miklos Szeredicc080e92015-07-01 16:26:08 +02001340 struct fuse_dev *fud = fuse_get_dev(file);
1341
1342 if (!fud)
Miklos Szeredic3021622010-05-25 15:06:07 +02001343 return -EPERM;
1344
Al Virofbdbacc2015-04-03 21:53:39 -04001345 if (!iter_is_iovec(to))
1346 return -EINVAL;
Miklos Szeredic3021622010-05-25 15:06:07 +02001347
Miklos Szeredidc008092015-07-01 16:25:58 +02001348 fuse_copy_init(&cs, 1, to);
Al Virofbdbacc2015-04-03 21:53:39 -04001349
Miklos Szeredic36960462015-07-01 16:26:09 +02001350 return fuse_dev_do_read(fud, file, &cs, iov_iter_count(to));
Miklos Szeredic3021622010-05-25 15:06:07 +02001351}
1352
Miklos Szeredic3021622010-05-25 15:06:07 +02001353static ssize_t fuse_dev_splice_read(struct file *in, loff_t *ppos,
1354 struct pipe_inode_info *pipe,
1355 size_t len, unsigned int flags)
1356{
Al Virod82718e2016-09-17 22:56:25 -04001357 int total, ret;
Miklos Szeredic3021622010-05-25 15:06:07 +02001358 int page_nr = 0;
Miklos Szeredic3021622010-05-25 15:06:07 +02001359 struct pipe_buffer *bufs;
1360 struct fuse_copy_state cs;
Miklos Szeredicc080e92015-07-01 16:26:08 +02001361 struct fuse_dev *fud = fuse_get_dev(in);
1362
1363 if (!fud)
Miklos Szeredic3021622010-05-25 15:06:07 +02001364 return -EPERM;
1365
Kees Cook6da2ec52018-06-12 13:55:00 -07001366 bufs = kmalloc_array(pipe->buffers, sizeof(struct pipe_buffer),
1367 GFP_KERNEL);
Miklos Szeredic3021622010-05-25 15:06:07 +02001368 if (!bufs)
1369 return -ENOMEM;
1370
Miklos Szeredidc008092015-07-01 16:25:58 +02001371 fuse_copy_init(&cs, 1, NULL);
Miklos Szeredic3021622010-05-25 15:06:07 +02001372 cs.pipebufs = bufs;
1373 cs.pipe = pipe;
Miklos Szeredic36960462015-07-01 16:26:09 +02001374 ret = fuse_dev_do_read(fud, in, &cs, len);
Miklos Szeredic3021622010-05-25 15:06:07 +02001375 if (ret < 0)
1376 goto out;
1377
Miklos Szeredic3021622010-05-25 15:06:07 +02001378 if (pipe->nrbufs + cs.nr_segs > pipe->buffers) {
1379 ret = -EIO;
Al Virod82718e2016-09-17 22:56:25 -04001380 goto out;
Miklos Szeredic3021622010-05-25 15:06:07 +02001381 }
1382
Al Virod82718e2016-09-17 22:56:25 -04001383 for (ret = total = 0; page_nr < cs.nr_segs; total += ret) {
Miklos Szeredi28a625c2014-01-22 19:36:57 +01001384 /*
1385 * Need to be careful about this. Having buf->ops in module
1386 * code can Oops if the buffer persists after module unload.
1387 */
Al Virod82718e2016-09-17 22:56:25 -04001388 bufs[page_nr].ops = &nosteal_pipe_buf_ops;
Miklos Szeredi84588a92017-02-16 15:08:20 +01001389 bufs[page_nr].flags = 0;
Al Virod82718e2016-09-17 22:56:25 -04001390 ret = add_to_pipe(pipe, &bufs[page_nr++]);
1391 if (unlikely(ret < 0))
1392 break;
Miklos Szeredic3021622010-05-25 15:06:07 +02001393 }
Al Virod82718e2016-09-17 22:56:25 -04001394 if (total)
1395 ret = total;
Miklos Szeredic3021622010-05-25 15:06:07 +02001396out:
1397 for (; page_nr < cs.nr_segs; page_nr++)
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001398 put_page(bufs[page_nr].page);
Miklos Szeredic3021622010-05-25 15:06:07 +02001399
1400 kfree(bufs);
1401 return ret;
1402}
1403
Tejun Heo95668a62008-11-26 12:03:55 +01001404static int fuse_notify_poll(struct fuse_conn *fc, unsigned int size,
1405 struct fuse_copy_state *cs)
1406{
1407 struct fuse_notify_poll_wakeup_out outarg;
Miklos Szeredif6d47a12009-01-26 15:00:59 +01001408 int err = -EINVAL;
Tejun Heo95668a62008-11-26 12:03:55 +01001409
1410 if (size != sizeof(outarg))
Miklos Szeredif6d47a12009-01-26 15:00:59 +01001411 goto err;
Tejun Heo95668a62008-11-26 12:03:55 +01001412
1413 err = fuse_copy_one(cs, &outarg, sizeof(outarg));
1414 if (err)
Miklos Szeredif6d47a12009-01-26 15:00:59 +01001415 goto err;
Tejun Heo95668a62008-11-26 12:03:55 +01001416
Miklos Szeredif6d47a12009-01-26 15:00:59 +01001417 fuse_copy_finish(cs);
Tejun Heo95668a62008-11-26 12:03:55 +01001418 return fuse_notify_poll_wakeup(fc, &outarg);
Miklos Szeredif6d47a12009-01-26 15:00:59 +01001419
1420err:
1421 fuse_copy_finish(cs);
1422 return err;
Tejun Heo95668a62008-11-26 12:03:55 +01001423}
1424
John Muir3b463ae2009-05-31 11:13:57 -04001425static int fuse_notify_inval_inode(struct fuse_conn *fc, unsigned int size,
1426 struct fuse_copy_state *cs)
1427{
1428 struct fuse_notify_inval_inode_out outarg;
1429 int err = -EINVAL;
1430
1431 if (size != sizeof(outarg))
1432 goto err;
1433
1434 err = fuse_copy_one(cs, &outarg, sizeof(outarg));
1435 if (err)
1436 goto err;
1437 fuse_copy_finish(cs);
1438
1439 down_read(&fc->killsb);
1440 err = -ENOENT;
Miklos Szeredib21dda42010-02-05 12:08:31 +01001441 if (fc->sb) {
1442 err = fuse_reverse_inval_inode(fc->sb, outarg.ino,
1443 outarg.off, outarg.len);
1444 }
John Muir3b463ae2009-05-31 11:13:57 -04001445 up_read(&fc->killsb);
1446 return err;
1447
1448err:
1449 fuse_copy_finish(cs);
1450 return err;
1451}
1452
1453static int fuse_notify_inval_entry(struct fuse_conn *fc, unsigned int size,
1454 struct fuse_copy_state *cs)
1455{
1456 struct fuse_notify_inval_entry_out outarg;
Fang Wenqib2d82ee2009-12-30 18:37:13 +08001457 int err = -ENOMEM;
1458 char *buf;
John Muir3b463ae2009-05-31 11:13:57 -04001459 struct qstr name;
1460
Fang Wenqib2d82ee2009-12-30 18:37:13 +08001461 buf = kzalloc(FUSE_NAME_MAX + 1, GFP_KERNEL);
1462 if (!buf)
1463 goto err;
1464
1465 err = -EINVAL;
John Muir3b463ae2009-05-31 11:13:57 -04001466 if (size < sizeof(outarg))
1467 goto err;
1468
1469 err = fuse_copy_one(cs, &outarg, sizeof(outarg));
1470 if (err)
1471 goto err;
1472
1473 err = -ENAMETOOLONG;
1474 if (outarg.namelen > FUSE_NAME_MAX)
1475 goto err;
1476
Miklos Szeredic2183d12011-08-24 10:20:17 +02001477 err = -EINVAL;
1478 if (size != sizeof(outarg) + outarg.namelen + 1)
1479 goto err;
1480
John Muir3b463ae2009-05-31 11:13:57 -04001481 name.name = buf;
1482 name.len = outarg.namelen;
1483 err = fuse_copy_one(cs, buf, outarg.namelen + 1);
1484 if (err)
1485 goto err;
1486 fuse_copy_finish(cs);
1487 buf[outarg.namelen] = 0;
John Muir3b463ae2009-05-31 11:13:57 -04001488
1489 down_read(&fc->killsb);
1490 err = -ENOENT;
Miklos Szeredib21dda42010-02-05 12:08:31 +01001491 if (fc->sb)
John Muir451d0f52011-12-06 21:50:06 +01001492 err = fuse_reverse_inval_entry(fc->sb, outarg.parent, 0, &name);
1493 up_read(&fc->killsb);
1494 kfree(buf);
1495 return err;
1496
1497err:
1498 kfree(buf);
1499 fuse_copy_finish(cs);
1500 return err;
1501}
1502
1503static int fuse_notify_delete(struct fuse_conn *fc, unsigned int size,
1504 struct fuse_copy_state *cs)
1505{
1506 struct fuse_notify_delete_out outarg;
1507 int err = -ENOMEM;
1508 char *buf;
1509 struct qstr name;
1510
1511 buf = kzalloc(FUSE_NAME_MAX + 1, GFP_KERNEL);
1512 if (!buf)
1513 goto err;
1514
1515 err = -EINVAL;
1516 if (size < sizeof(outarg))
1517 goto err;
1518
1519 err = fuse_copy_one(cs, &outarg, sizeof(outarg));
1520 if (err)
1521 goto err;
1522
1523 err = -ENAMETOOLONG;
1524 if (outarg.namelen > FUSE_NAME_MAX)
1525 goto err;
1526
1527 err = -EINVAL;
1528 if (size != sizeof(outarg) + outarg.namelen + 1)
1529 goto err;
1530
1531 name.name = buf;
1532 name.len = outarg.namelen;
1533 err = fuse_copy_one(cs, buf, outarg.namelen + 1);
1534 if (err)
1535 goto err;
1536 fuse_copy_finish(cs);
1537 buf[outarg.namelen] = 0;
John Muir451d0f52011-12-06 21:50:06 +01001538
1539 down_read(&fc->killsb);
1540 err = -ENOENT;
1541 if (fc->sb)
1542 err = fuse_reverse_inval_entry(fc->sb, outarg.parent,
1543 outarg.child, &name);
John Muir3b463ae2009-05-31 11:13:57 -04001544 up_read(&fc->killsb);
Fang Wenqib2d82ee2009-12-30 18:37:13 +08001545 kfree(buf);
John Muir3b463ae2009-05-31 11:13:57 -04001546 return err;
1547
1548err:
Fang Wenqib2d82ee2009-12-30 18:37:13 +08001549 kfree(buf);
John Muir3b463ae2009-05-31 11:13:57 -04001550 fuse_copy_finish(cs);
1551 return err;
1552}
1553
Miklos Szeredia1d75f22010-07-12 14:41:40 +02001554static int fuse_notify_store(struct fuse_conn *fc, unsigned int size,
1555 struct fuse_copy_state *cs)
1556{
1557 struct fuse_notify_store_out outarg;
1558 struct inode *inode;
1559 struct address_space *mapping;
1560 u64 nodeid;
1561 int err;
1562 pgoff_t index;
1563 unsigned int offset;
1564 unsigned int num;
1565 loff_t file_size;
1566 loff_t end;
1567
1568 err = -EINVAL;
1569 if (size < sizeof(outarg))
1570 goto out_finish;
1571
1572 err = fuse_copy_one(cs, &outarg, sizeof(outarg));
1573 if (err)
1574 goto out_finish;
1575
1576 err = -EINVAL;
1577 if (size - sizeof(outarg) != outarg.size)
1578 goto out_finish;
1579
1580 nodeid = outarg.nodeid;
1581
1582 down_read(&fc->killsb);
1583
1584 err = -ENOENT;
1585 if (!fc->sb)
1586 goto out_up_killsb;
1587
1588 inode = ilookup5(fc->sb, nodeid, fuse_inode_eq, &nodeid);
1589 if (!inode)
1590 goto out_up_killsb;
1591
1592 mapping = inode->i_mapping;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001593 index = outarg.offset >> PAGE_SHIFT;
1594 offset = outarg.offset & ~PAGE_MASK;
Miklos Szeredia1d75f22010-07-12 14:41:40 +02001595 file_size = i_size_read(inode);
1596 end = outarg.offset + outarg.size;
1597 if (end > file_size) {
1598 file_size = end;
1599 fuse_write_update_size(inode, file_size);
1600 }
1601
1602 num = outarg.size;
1603 while (num) {
1604 struct page *page;
1605 unsigned int this_num;
1606
1607 err = -ENOMEM;
1608 page = find_or_create_page(mapping, index,
1609 mapping_gfp_mask(mapping));
1610 if (!page)
1611 goto out_iput;
1612
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001613 this_num = min_t(unsigned, num, PAGE_SIZE - offset);
Miklos Szeredia1d75f22010-07-12 14:41:40 +02001614 err = fuse_copy_page(cs, &page, offset, this_num, 0);
Miklos Szeredi063ec1e2014-01-22 19:36:58 +01001615 if (!err && offset == 0 &&
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001616 (this_num == PAGE_SIZE || file_size == end))
Miklos Szeredia1d75f22010-07-12 14:41:40 +02001617 SetPageUptodate(page);
1618 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001619 put_page(page);
Miklos Szeredia1d75f22010-07-12 14:41:40 +02001620
1621 if (err)
1622 goto out_iput;
1623
1624 num -= this_num;
1625 offset = 0;
1626 index++;
1627 }
1628
1629 err = 0;
1630
1631out_iput:
1632 iput(inode);
1633out_up_killsb:
1634 up_read(&fc->killsb);
1635out_finish:
1636 fuse_copy_finish(cs);
1637 return err;
1638}
1639
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001640static void fuse_retrieve_end(struct fuse_conn *fc, struct fuse_req *req)
1641{
Mel Gormanc6f92f92017-11-15 17:37:55 -08001642 release_pages(req->pages, req->num_pages);
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001643}
1644
1645static int fuse_retrieve(struct fuse_conn *fc, struct inode *inode,
1646 struct fuse_notify_retrieve_out *outarg)
1647{
1648 int err;
1649 struct address_space *mapping = inode->i_mapping;
1650 struct fuse_req *req;
1651 pgoff_t index;
1652 loff_t file_size;
1653 unsigned int num;
1654 unsigned int offset;
Geert Uytterhoeven01574432010-09-30 22:06:21 +02001655 size_t total_len = 0;
Maxim Patlasov4d53dc92012-10-26 19:48:42 +04001656 int num_pages;
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001657
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001658 offset = outarg->offset & ~PAGE_MASK;
Maxim Patlasov4d53dc92012-10-26 19:48:42 +04001659 file_size = i_size_read(inode);
1660
1661 num = outarg->size;
1662 if (outarg->offset > file_size)
1663 num = 0;
1664 else if (outarg->offset + num > file_size)
1665 num = file_size - outarg->offset;
1666
1667 num_pages = (num + offset + PAGE_SIZE - 1) >> PAGE_SHIFT;
1668 num_pages = min(num_pages, FUSE_MAX_PAGES_PER_REQ);
1669
1670 req = fuse_get_req(fc, num_pages);
1671 if (IS_ERR(req))
1672 return PTR_ERR(req);
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001673
1674 req->in.h.opcode = FUSE_NOTIFY_REPLY;
1675 req->in.h.nodeid = outarg->nodeid;
1676 req->in.numargs = 2;
1677 req->in.argpages = 1;
Maxim Patlasovb2430d72012-10-26 19:49:24 +04001678 req->page_descs[0].offset = offset;
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001679 req->end = fuse_retrieve_end;
1680
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001681 index = outarg->offset >> PAGE_SHIFT;
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001682
Maxim Patlasov4d53dc92012-10-26 19:48:42 +04001683 while (num && req->num_pages < num_pages) {
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001684 struct page *page;
1685 unsigned int this_num;
1686
1687 page = find_get_page(mapping, index);
1688 if (!page)
1689 break;
1690
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001691 this_num = min_t(unsigned, num, PAGE_SIZE - offset);
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001692 req->pages[req->num_pages] = page;
Maxim Patlasov85f40ae2012-10-26 19:49:33 +04001693 req->page_descs[req->num_pages].length = this_num;
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001694 req->num_pages++;
1695
Miklos Szeredic9e67d42012-09-04 18:45:54 +02001696 offset = 0;
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001697 num -= this_num;
1698 total_len += this_num;
Miklos Szeredi48706d02011-12-13 10:36:59 +01001699 index++;
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001700 }
1701 req->misc.retrieve_in.offset = outarg->offset;
1702 req->misc.retrieve_in.size = total_len;
1703 req->in.args[0].size = sizeof(req->misc.retrieve_in);
1704 req->in.args[0].value = &req->misc.retrieve_in;
1705 req->in.args[1].size = total_len;
1706
1707 err = fuse_request_send_notify_reply(fc, req, outarg->notify_unique);
1708 if (err)
1709 fuse_retrieve_end(fc, req);
1710
1711 return err;
1712}
1713
1714static int fuse_notify_retrieve(struct fuse_conn *fc, unsigned int size,
1715 struct fuse_copy_state *cs)
1716{
1717 struct fuse_notify_retrieve_out outarg;
1718 struct inode *inode;
1719 int err;
1720
1721 err = -EINVAL;
1722 if (size != sizeof(outarg))
1723 goto copy_finish;
1724
1725 err = fuse_copy_one(cs, &outarg, sizeof(outarg));
1726 if (err)
1727 goto copy_finish;
1728
1729 fuse_copy_finish(cs);
1730
1731 down_read(&fc->killsb);
1732 err = -ENOENT;
1733 if (fc->sb) {
1734 u64 nodeid = outarg.nodeid;
1735
1736 inode = ilookup5(fc->sb, nodeid, fuse_inode_eq, &nodeid);
1737 if (inode) {
1738 err = fuse_retrieve(fc, inode, &outarg);
1739 iput(inode);
1740 }
1741 }
1742 up_read(&fc->killsb);
1743
1744 return err;
1745
1746copy_finish:
1747 fuse_copy_finish(cs);
1748 return err;
1749}
1750
Tejun Heo85993962008-11-26 12:03:55 +01001751static int fuse_notify(struct fuse_conn *fc, enum fuse_notify_code code,
1752 unsigned int size, struct fuse_copy_state *cs)
1753{
Miklos Szeredi0d278362015-02-26 11:45:47 +01001754 /* Don't try to move pages (yet) */
1755 cs->move_pages = 0;
1756
Tejun Heo85993962008-11-26 12:03:55 +01001757 switch (code) {
Tejun Heo95668a62008-11-26 12:03:55 +01001758 case FUSE_NOTIFY_POLL:
1759 return fuse_notify_poll(fc, size, cs);
1760
John Muir3b463ae2009-05-31 11:13:57 -04001761 case FUSE_NOTIFY_INVAL_INODE:
1762 return fuse_notify_inval_inode(fc, size, cs);
1763
1764 case FUSE_NOTIFY_INVAL_ENTRY:
1765 return fuse_notify_inval_entry(fc, size, cs);
1766
Miklos Szeredia1d75f22010-07-12 14:41:40 +02001767 case FUSE_NOTIFY_STORE:
1768 return fuse_notify_store(fc, size, cs);
1769
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001770 case FUSE_NOTIFY_RETRIEVE:
1771 return fuse_notify_retrieve(fc, size, cs);
1772
John Muir451d0f52011-12-06 21:50:06 +01001773 case FUSE_NOTIFY_DELETE:
1774 return fuse_notify_delete(fc, size, cs);
1775
Tejun Heo85993962008-11-26 12:03:55 +01001776 default:
Miklos Szeredif6d47a12009-01-26 15:00:59 +01001777 fuse_copy_finish(cs);
Tejun Heo85993962008-11-26 12:03:55 +01001778 return -EINVAL;
1779 }
1780}
1781
Miklos Szeredi334f4852005-09-09 13:10:27 -07001782/* Look up request on processing list by unique ID */
Miklos Szeredi3a2b5b92015-07-01 16:26:04 +02001783static struct fuse_req *request_find(struct fuse_pqueue *fpq, u64 unique)
Miklos Szeredi334f4852005-09-09 13:10:27 -07001784{
Dong Fang05726ac2013-07-30 22:50:01 -04001785 struct fuse_req *req;
Miklos Szeredi334f4852005-09-09 13:10:27 -07001786
Miklos Szeredi3a2b5b92015-07-01 16:26:04 +02001787 list_for_each_entry(req, &fpq->processing, list) {
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001788 if (req->in.h.unique == unique || req->intr_unique == unique)
Miklos Szeredi334f4852005-09-09 13:10:27 -07001789 return req;
1790 }
1791 return NULL;
1792}
1793
1794static int copy_out_args(struct fuse_copy_state *cs, struct fuse_out *out,
1795 unsigned nbytes)
1796{
1797 unsigned reqsize = sizeof(struct fuse_out_header);
1798
1799 if (out->h.error)
1800 return nbytes != reqsize ? -EINVAL : 0;
1801
1802 reqsize += len_args(out->numargs, out->args);
1803
1804 if (reqsize < nbytes || (reqsize > nbytes && !out->argvar))
1805 return -EINVAL;
1806 else if (reqsize > nbytes) {
1807 struct fuse_arg *lastarg = &out->args[out->numargs-1];
1808 unsigned diffsize = reqsize - nbytes;
1809 if (diffsize > lastarg->size)
1810 return -EINVAL;
1811 lastarg->size -= diffsize;
1812 }
1813 return fuse_copy_args(cs, out->numargs, out->argpages, out->args,
1814 out->page_zeroing);
1815}
1816
1817/*
1818 * Write a single reply to a request. First the header is copied from
1819 * the write buffer. The request is then searched on the processing
1820 * list by the unique ID found in the header. If found, then remove
1821 * it from the list and copy the rest of the buffer to the request.
1822 * The request is finished by calling request_end()
1823 */
Miklos Szeredic36960462015-07-01 16:26:09 +02001824static ssize_t fuse_dev_do_write(struct fuse_dev *fud,
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001825 struct fuse_copy_state *cs, size_t nbytes)
Miklos Szeredi334f4852005-09-09 13:10:27 -07001826{
1827 int err;
Miklos Szeredic36960462015-07-01 16:26:09 +02001828 struct fuse_conn *fc = fud->fc;
1829 struct fuse_pqueue *fpq = &fud->pq;
Miklos Szeredi334f4852005-09-09 13:10:27 -07001830 struct fuse_req *req;
1831 struct fuse_out_header oh;
Miklos Szeredi334f4852005-09-09 13:10:27 -07001832
Miklos Szeredi334f4852005-09-09 13:10:27 -07001833 if (nbytes < sizeof(struct fuse_out_header))
1834 return -EINVAL;
1835
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001836 err = fuse_copy_one(cs, &oh, sizeof(oh));
Miklos Szeredi334f4852005-09-09 13:10:27 -07001837 if (err)
1838 goto err_finish;
Tejun Heo85993962008-11-26 12:03:55 +01001839
Miklos Szeredi334f4852005-09-09 13:10:27 -07001840 err = -EINVAL;
Tejun Heo85993962008-11-26 12:03:55 +01001841 if (oh.len != nbytes)
1842 goto err_finish;
1843
1844 /*
1845 * Zero oh.unique indicates unsolicited notification message
1846 * and error contains notification code.
1847 */
1848 if (!oh.unique) {
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001849 err = fuse_notify(fc, oh.error, nbytes - sizeof(oh), cs);
Tejun Heo85993962008-11-26 12:03:55 +01001850 return err ? err : nbytes;
1851 }
1852
1853 err = -EINVAL;
1854 if (oh.error <= -1000 || oh.error > 0)
Miklos Szeredi334f4852005-09-09 13:10:27 -07001855 goto err_finish;
1856
Miklos Szeredi45a91cb2015-07-01 16:26:06 +02001857 spin_lock(&fpq->lock);
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08001858 err = -ENOENT;
Miklos Szeredie96edd92015-07-01 16:26:04 +02001859 if (!fpq->connected)
Miklos Szeredi45a91cb2015-07-01 16:26:06 +02001860 goto err_unlock_pq;
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08001861
Miklos Szeredi3a2b5b92015-07-01 16:26:04 +02001862 req = request_find(fpq, oh.unique);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001863 if (!req)
Miklos Szeredi45a91cb2015-07-01 16:26:06 +02001864 goto err_unlock_pq;
Miklos Szeredi334f4852005-09-09 13:10:27 -07001865
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001866 /* Is it an interrupt reply? */
1867 if (req->intr_unique == oh.unique) {
Miklos Szeredi45a91cb2015-07-01 16:26:06 +02001868 spin_unlock(&fpq->lock);
1869
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001870 err = -EINVAL;
1871 if (nbytes != sizeof(struct fuse_out_header))
Miklos Szeredi46c34a32015-07-01 16:26:07 +02001872 goto err_finish;
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001873
1874 if (oh.error == -ENOSYS)
1875 fc->no_interrupt = 1;
1876 else if (oh.error == -EAGAIN)
Miklos Szeredif88996a2015-07-01 16:26:01 +02001877 queue_interrupt(&fc->iq, req);
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001878
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001879 fuse_copy_finish(cs);
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001880 return nbytes;
1881 }
1882
Miklos Szeredi33e14b42015-07-01 16:26:01 +02001883 clear_bit(FR_SENT, &req->flags);
Miklos Szeredi3a2b5b92015-07-01 16:26:04 +02001884 list_move(&req->list, &fpq->io);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001885 req->out.h = oh;
Miklos Szeredi825d6d32015-07-01 16:25:58 +02001886 set_bit(FR_LOCKED, &req->flags);
Miklos Szeredi45a91cb2015-07-01 16:26:06 +02001887 spin_unlock(&fpq->lock);
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001888 cs->req = req;
Miklos Szeredice534fb2010-05-25 15:06:07 +02001889 if (!req->out.page_replace)
1890 cs->move_pages = 0;
Miklos Szeredi334f4852005-09-09 13:10:27 -07001891
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001892 err = copy_out_args(cs, &req->out, nbytes);
1893 fuse_copy_finish(cs);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001894
Miklos Szeredi45a91cb2015-07-01 16:26:06 +02001895 spin_lock(&fpq->lock);
Miklos Szeredi825d6d32015-07-01 16:25:58 +02001896 clear_bit(FR_LOCKED, &req->flags);
Miklos Szeredie96edd92015-07-01 16:26:04 +02001897 if (!fpq->connected)
Miklos Szeredi0d8e84b2015-07-01 16:25:58 +02001898 err = -ENOENT;
1899 else if (err)
Miklos Szeredi334f4852005-09-09 13:10:27 -07001900 req->out.h.error = -EIO;
Miklos Szeredi77cd9d42015-07-01 16:26:06 +02001901 if (!test_bit(FR_PRIVATE, &req->flags))
1902 list_del_init(&req->list);
Miklos Szeredi45a91cb2015-07-01 16:26:06 +02001903 spin_unlock(&fpq->lock);
Miklos Szeredi46c34a32015-07-01 16:26:07 +02001904
Miklos Szeredi334f4852005-09-09 13:10:27 -07001905 request_end(fc, req);
1906
1907 return err ? err : nbytes;
1908
Miklos Szeredi45a91cb2015-07-01 16:26:06 +02001909 err_unlock_pq:
1910 spin_unlock(&fpq->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001911 err_finish:
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001912 fuse_copy_finish(cs);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001913 return err;
1914}
1915
Al Virofbdbacc2015-04-03 21:53:39 -04001916static ssize_t fuse_dev_write(struct kiocb *iocb, struct iov_iter *from)
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001917{
1918 struct fuse_copy_state cs;
Miklos Szeredicc080e92015-07-01 16:26:08 +02001919 struct fuse_dev *fud = fuse_get_dev(iocb->ki_filp);
1920
1921 if (!fud)
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001922 return -EPERM;
1923
Al Virofbdbacc2015-04-03 21:53:39 -04001924 if (!iter_is_iovec(from))
1925 return -EINVAL;
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001926
Miklos Szeredidc008092015-07-01 16:25:58 +02001927 fuse_copy_init(&cs, 0, from);
Al Virofbdbacc2015-04-03 21:53:39 -04001928
Miklos Szeredic36960462015-07-01 16:26:09 +02001929 return fuse_dev_do_write(fud, &cs, iov_iter_count(from));
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001930}
1931
1932static ssize_t fuse_dev_splice_write(struct pipe_inode_info *pipe,
1933 struct file *out, loff_t *ppos,
1934 size_t len, unsigned int flags)
1935{
1936 unsigned nbuf;
1937 unsigned idx;
1938 struct pipe_buffer *bufs;
1939 struct fuse_copy_state cs;
Miklos Szeredicc080e92015-07-01 16:26:08 +02001940 struct fuse_dev *fud;
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001941 size_t rem;
1942 ssize_t ret;
1943
Miklos Szeredicc080e92015-07-01 16:26:08 +02001944 fud = fuse_get_dev(out);
1945 if (!fud)
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001946 return -EPERM;
1947
Kees Cook6da2ec52018-06-12 13:55:00 -07001948 bufs = kmalloc_array(pipe->buffers, sizeof(struct pipe_buffer),
1949 GFP_KERNEL);
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001950 if (!bufs)
1951 return -ENOMEM;
1952
1953 pipe_lock(pipe);
1954 nbuf = 0;
1955 rem = 0;
1956 for (idx = 0; idx < pipe->nrbufs && rem < len; idx++)
1957 rem += pipe->bufs[(pipe->curbuf + idx) & (pipe->buffers - 1)].len;
1958
1959 ret = -EINVAL;
1960 if (rem < len) {
1961 pipe_unlock(pipe);
1962 goto out;
1963 }
1964
1965 rem = len;
1966 while (rem) {
1967 struct pipe_buffer *ibuf;
1968 struct pipe_buffer *obuf;
1969
1970 BUG_ON(nbuf >= pipe->buffers);
1971 BUG_ON(!pipe->nrbufs);
1972 ibuf = &pipe->bufs[pipe->curbuf];
1973 obuf = &bufs[nbuf];
1974
1975 if (rem >= ibuf->len) {
1976 *obuf = *ibuf;
1977 ibuf->ops = NULL;
1978 pipe->curbuf = (pipe->curbuf + 1) & (pipe->buffers - 1);
1979 pipe->nrbufs--;
1980 } else {
Miklos Szeredi7bf2d1d2016-09-27 10:45:12 +02001981 pipe_buf_get(pipe, ibuf);
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001982 *obuf = *ibuf;
1983 obuf->flags &= ~PIPE_BUF_FLAG_GIFT;
1984 obuf->len = rem;
1985 ibuf->offset += obuf->len;
1986 ibuf->len -= obuf->len;
1987 }
1988 nbuf++;
1989 rem -= obuf->len;
1990 }
1991 pipe_unlock(pipe);
1992
Miklos Szeredidc008092015-07-01 16:25:58 +02001993 fuse_copy_init(&cs, 0, NULL);
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001994 cs.pipebufs = bufs;
Al Viro6c09e942015-04-03 22:06:08 -04001995 cs.nr_segs = nbuf;
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001996 cs.pipe = pipe;
1997
Miklos Szeredice534fb2010-05-25 15:06:07 +02001998 if (flags & SPLICE_F_MOVE)
1999 cs.move_pages = 1;
2000
Miklos Szeredic36960462015-07-01 16:26:09 +02002001 ret = fuse_dev_do_write(fud, &cs, len);
Miklos Szeredidd3bb142010-05-25 15:06:06 +02002002
Miklos Szeredia7796382016-09-27 10:45:12 +02002003 for (idx = 0; idx < nbuf; idx++)
2004 pipe_buf_release(pipe, &bufs[idx]);
2005
Miklos Szeredidd3bb142010-05-25 15:06:06 +02002006out:
2007 kfree(bufs);
2008 return ret;
2009}
2010
Al Viro076ccb72017-07-03 01:02:18 -04002011static __poll_t fuse_dev_poll(struct file *file, poll_table *wait)
Miklos Szeredi334f4852005-09-09 13:10:27 -07002012{
Linus Torvaldsa9a08842018-02-11 14:34:03 -08002013 __poll_t mask = EPOLLOUT | EPOLLWRNORM;
Miklos Szeredif88996a2015-07-01 16:26:01 +02002014 struct fuse_iqueue *fiq;
Miklos Szeredicc080e92015-07-01 16:26:08 +02002015 struct fuse_dev *fud = fuse_get_dev(file);
2016
2017 if (!fud)
Linus Torvaldsa9a08842018-02-11 14:34:03 -08002018 return EPOLLERR;
Miklos Szeredi334f4852005-09-09 13:10:27 -07002019
Miklos Szeredicc080e92015-07-01 16:26:08 +02002020 fiq = &fud->fc->iq;
Miklos Szeredif88996a2015-07-01 16:26:01 +02002021 poll_wait(file, &fiq->waitq, wait);
Miklos Szeredi334f4852005-09-09 13:10:27 -07002022
Miklos Szeredi4ce60812015-07-01 16:26:02 +02002023 spin_lock(&fiq->waitq.lock);
Miklos Szeredie16714d2015-07-01 16:26:01 +02002024 if (!fiq->connected)
Linus Torvaldsa9a08842018-02-11 14:34:03 -08002025 mask = EPOLLERR;
Miklos Szeredif88996a2015-07-01 16:26:01 +02002026 else if (request_pending(fiq))
Linus Torvaldsa9a08842018-02-11 14:34:03 -08002027 mask |= EPOLLIN | EPOLLRDNORM;
Miklos Szeredi4ce60812015-07-01 16:26:02 +02002028 spin_unlock(&fiq->waitq.lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -07002029
2030 return mask;
2031}
2032
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002033/*
2034 * Abort all requests on the given list (pending or processing)
2035 *
Miklos Szeredid7133112006-04-10 22:54:55 -07002036 * This function releases and reacquires fc->lock
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002037 */
Miklos Szeredi334f4852005-09-09 13:10:27 -07002038static void end_requests(struct fuse_conn *fc, struct list_head *head)
2039{
2040 while (!list_empty(head)) {
2041 struct fuse_req *req;
2042 req = list_entry(head->next, struct fuse_req, list);
Miklos Szeredi334f4852005-09-09 13:10:27 -07002043 req->out.h.error = -ECONNABORTED;
Miklos Szeredi33e14b42015-07-01 16:26:01 +02002044 clear_bit(FR_SENT, &req->flags);
Miklos Szeredif377cb72015-07-01 16:26:04 +02002045 list_del_init(&req->list);
Miklos Szeredi334f4852005-09-09 13:10:27 -07002046 request_end(fc, req);
Miklos Szeredi334f4852005-09-09 13:10:27 -07002047 }
2048}
2049
Bryan Green357ccf22011-03-01 16:43:52 -08002050static void end_polls(struct fuse_conn *fc)
2051{
2052 struct rb_node *p;
2053
2054 p = rb_first(&fc->polled_files);
2055
2056 while (p) {
2057 struct fuse_file *ff;
2058 ff = rb_entry(p, struct fuse_file, polled_node);
2059 wake_up_interruptible_all(&ff->poll_wait);
2060
2061 p = rb_next(p);
2062 }
2063}
2064
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002065/*
2066 * Abort all requests.
2067 *
Miklos Szeredib716d422015-07-01 16:25:59 +02002068 * Emergency exit in case of a malicious or accidental deadlock, or just a hung
2069 * filesystem.
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002070 *
Miklos Szeredib716d422015-07-01 16:25:59 +02002071 * The same effect is usually achievable through killing the filesystem daemon
2072 * and all users of the filesystem. The exception is the combination of an
2073 * asynchronous request and the tricky deadlock (see
2074 * Documentation/filesystems/fuse.txt).
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002075 *
Miklos Szeredib716d422015-07-01 16:25:59 +02002076 * Aborting requests under I/O goes as follows: 1: Separate out unlocked
2077 * requests, they should be finished off immediately. Locked requests will be
2078 * finished after unlock; see unlock_request(). 2: Finish off the unlocked
2079 * requests. It is possible that some request will finish before we can. This
2080 * is OK, the request will in that case be removed from the list before we touch
2081 * it.
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002082 */
Szymon Lukasz3b7008b2017-11-09 21:23:35 +01002083void fuse_abort_conn(struct fuse_conn *fc, bool is_abort)
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002084{
Miklos Szeredif88996a2015-07-01 16:26:01 +02002085 struct fuse_iqueue *fiq = &fc->iq;
2086
Miklos Szeredid7133112006-04-10 22:54:55 -07002087 spin_lock(&fc->lock);
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002088 if (fc->connected) {
Miklos Szeredic36960462015-07-01 16:26:09 +02002089 struct fuse_dev *fud;
Miklos Szeredib716d422015-07-01 16:25:59 +02002090 struct fuse_req *req, *next;
Miklos Szeredi41f98272015-07-01 16:25:59 +02002091 LIST_HEAD(to_end1);
2092 LIST_HEAD(to_end2);
Miklos Szeredib716d422015-07-01 16:25:59 +02002093
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002094 fc->connected = 0;
Miklos Szeredi51eb01e2006-06-25 05:48:50 -07002095 fc->blocked = 0;
Szymon Lukasz3b7008b2017-11-09 21:23:35 +01002096 fc->aborted = is_abort;
Miklos Szeredi9759bd512015-01-06 10:45:35 +01002097 fuse_set_initialized(fc);
Miklos Szeredic36960462015-07-01 16:26:09 +02002098 list_for_each_entry(fud, &fc->devices, entry) {
2099 struct fuse_pqueue *fpq = &fud->pq;
2100
2101 spin_lock(&fpq->lock);
2102 fpq->connected = 0;
2103 list_for_each_entry_safe(req, next, &fpq->io, list) {
2104 req->out.h.error = -ECONNABORTED;
2105 spin_lock(&req->waitq.lock);
2106 set_bit(FR_ABORTED, &req->flags);
2107 if (!test_bit(FR_LOCKED, &req->flags)) {
2108 set_bit(FR_PRIVATE, &req->flags);
Miklos Szeredi87114372018-07-26 16:13:11 +02002109 __fuse_get_request(req);
Miklos Szeredic36960462015-07-01 16:26:09 +02002110 list_move(&req->list, &to_end1);
2111 }
2112 spin_unlock(&req->waitq.lock);
Miklos Szeredi77cd9d42015-07-01 16:26:06 +02002113 }
Miklos Szeredic36960462015-07-01 16:26:09 +02002114 list_splice_init(&fpq->processing, &to_end2);
2115 spin_unlock(&fpq->lock);
Miklos Szeredib716d422015-07-01 16:25:59 +02002116 }
Miklos Szeredi41f98272015-07-01 16:25:59 +02002117 fc->max_background = UINT_MAX;
2118 flush_bg_queue(fc);
Miklos Szeredi8c911892015-07-01 16:26:02 +02002119
Miklos Szeredi4ce60812015-07-01 16:26:02 +02002120 spin_lock(&fiq->waitq.lock);
Miklos Szeredi8c911892015-07-01 16:26:02 +02002121 fiq->connected = 0;
Miklos Szeredif88996a2015-07-01 16:26:01 +02002122 list_splice_init(&fiq->pending, &to_end2);
Tahsin Erdogana8a86d72017-01-12 12:04:04 -08002123 list_for_each_entry(req, &to_end2, list)
2124 clear_bit(FR_PENDING, &req->flags);
Miklos Szeredi8c911892015-07-01 16:26:02 +02002125 while (forget_pending(fiq))
2126 kfree(dequeue_forget(fiq, 1, NULL));
Miklos Szeredi4ce60812015-07-01 16:26:02 +02002127 wake_up_all_locked(&fiq->waitq);
2128 spin_unlock(&fiq->waitq.lock);
Miklos Szeredi8c911892015-07-01 16:26:02 +02002129 kill_fasync(&fiq->fasync, SIGIO, POLL_IN);
Miklos Szerediee314a82015-07-01 16:26:08 +02002130 end_polls(fc);
2131 wake_up_all(&fc->blocked_waitq);
2132 spin_unlock(&fc->lock);
Miklos Szeredi8c911892015-07-01 16:26:02 +02002133
Miklos Szeredi41f98272015-07-01 16:25:59 +02002134 while (!list_empty(&to_end1)) {
2135 req = list_first_entry(&to_end1, struct fuse_req, list);
Miklos Szeredif377cb72015-07-01 16:26:04 +02002136 list_del_init(&req->list);
Miklos Szeredib716d422015-07-01 16:25:59 +02002137 request_end(fc, req);
Miklos Szeredib716d422015-07-01 16:25:59 +02002138 }
Miklos Szeredi41f98272015-07-01 16:25:59 +02002139 end_requests(fc, &to_end2);
Miklos Szerediee314a82015-07-01 16:26:08 +02002140 } else {
2141 spin_unlock(&fc->lock);
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002142 }
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002143}
Tejun Heo08cbf542009-04-14 10:54:53 +09002144EXPORT_SYMBOL_GPL(fuse_abort_conn);
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002145
Tejun Heo08cbf542009-04-14 10:54:53 +09002146int fuse_dev_release(struct inode *inode, struct file *file)
Miklos Szeredi334f4852005-09-09 13:10:27 -07002147{
Miklos Szeredicc080e92015-07-01 16:26:08 +02002148 struct fuse_dev *fud = fuse_get_dev(file);
2149
2150 if (fud) {
2151 struct fuse_conn *fc = fud->fc;
Miklos Szeredic36960462015-07-01 16:26:09 +02002152 struct fuse_pqueue *fpq = &fud->pq;
Miklos Szeredicc080e92015-07-01 16:26:08 +02002153
Miklos Szeredic36960462015-07-01 16:26:09 +02002154 WARN_ON(!list_empty(&fpq->io));
2155 end_requests(fc, &fpq->processing);
2156 /* Are we the last open device? */
2157 if (atomic_dec_and_test(&fc->dev_count)) {
2158 WARN_ON(fc->iq.fasync != NULL);
Szymon Lukasz3b7008b2017-11-09 21:23:35 +01002159 fuse_abort_conn(fc, false);
Miklos Szeredic36960462015-07-01 16:26:09 +02002160 }
Miklos Szeredicc080e92015-07-01 16:26:08 +02002161 fuse_dev_free(fud);
Jeff Dike385a17b2006-04-10 22:54:52 -07002162 }
Miklos Szeredi334f4852005-09-09 13:10:27 -07002163 return 0;
2164}
Tejun Heo08cbf542009-04-14 10:54:53 +09002165EXPORT_SYMBOL_GPL(fuse_dev_release);
Miklos Szeredi334f4852005-09-09 13:10:27 -07002166
Jeff Dike385a17b2006-04-10 22:54:52 -07002167static int fuse_dev_fasync(int fd, struct file *file, int on)
2168{
Miklos Szeredicc080e92015-07-01 16:26:08 +02002169 struct fuse_dev *fud = fuse_get_dev(file);
2170
2171 if (!fud)
Miklos Szeredia87046d2006-04-10 22:54:56 -07002172 return -EPERM;
Jeff Dike385a17b2006-04-10 22:54:52 -07002173
2174 /* No locking - fasync_helper does its own locking */
Miklos Szeredicc080e92015-07-01 16:26:08 +02002175 return fasync_helper(fd, file, on, &fud->fc->iq.fasync);
Jeff Dike385a17b2006-04-10 22:54:52 -07002176}
2177
Miklos Szeredi00c570f2015-07-01 16:26:08 +02002178static int fuse_device_clone(struct fuse_conn *fc, struct file *new)
2179{
Miklos Szeredicc080e92015-07-01 16:26:08 +02002180 struct fuse_dev *fud;
2181
Miklos Szeredi00c570f2015-07-01 16:26:08 +02002182 if (new->private_data)
2183 return -EINVAL;
2184
Miklos Szeredicc080e92015-07-01 16:26:08 +02002185 fud = fuse_dev_alloc(fc);
2186 if (!fud)
2187 return -ENOMEM;
2188
2189 new->private_data = fud;
Miklos Szeredic36960462015-07-01 16:26:09 +02002190 atomic_inc(&fc->dev_count);
Miklos Szeredi00c570f2015-07-01 16:26:08 +02002191
2192 return 0;
2193}
2194
2195static long fuse_dev_ioctl(struct file *file, unsigned int cmd,
2196 unsigned long arg)
2197{
2198 int err = -ENOTTY;
2199
2200 if (cmd == FUSE_DEV_IOC_CLONE) {
2201 int oldfd;
2202
2203 err = -EFAULT;
2204 if (!get_user(oldfd, (__u32 __user *) arg)) {
2205 struct file *old = fget(oldfd);
2206
2207 err = -EINVAL;
2208 if (old) {
Jann Horn8ed1f0e2015-08-16 20:27:01 +02002209 struct fuse_dev *fud = NULL;
2210
2211 /*
2212 * Check against file->f_op because CUSE
2213 * uses the same ioctl handler.
2214 */
2215 if (old->f_op == file->f_op &&
2216 old->f_cred->user_ns == file->f_cred->user_ns)
2217 fud = fuse_get_dev(old);
Miklos Szeredi00c570f2015-07-01 16:26:08 +02002218
Miklos Szeredicc080e92015-07-01 16:26:08 +02002219 if (fud) {
Miklos Szeredi00c570f2015-07-01 16:26:08 +02002220 mutex_lock(&fuse_mutex);
Miklos Szeredicc080e92015-07-01 16:26:08 +02002221 err = fuse_device_clone(fud->fc, file);
Miklos Szeredi00c570f2015-07-01 16:26:08 +02002222 mutex_unlock(&fuse_mutex);
2223 }
2224 fput(old);
2225 }
2226 }
2227 }
2228 return err;
2229}
2230
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -08002231const struct file_operations fuse_dev_operations = {
Miklos Szeredi334f4852005-09-09 13:10:27 -07002232 .owner = THIS_MODULE,
Tom Van Braeckel94e4fe22015-01-12 05:22:16 +01002233 .open = fuse_dev_open,
Miklos Szeredi334f4852005-09-09 13:10:27 -07002234 .llseek = no_llseek,
Al Virofbdbacc2015-04-03 21:53:39 -04002235 .read_iter = fuse_dev_read,
Miklos Szeredic3021622010-05-25 15:06:07 +02002236 .splice_read = fuse_dev_splice_read,
Al Virofbdbacc2015-04-03 21:53:39 -04002237 .write_iter = fuse_dev_write,
Miklos Szeredidd3bb142010-05-25 15:06:06 +02002238 .splice_write = fuse_dev_splice_write,
Miklos Szeredi334f4852005-09-09 13:10:27 -07002239 .poll = fuse_dev_poll,
2240 .release = fuse_dev_release,
Jeff Dike385a17b2006-04-10 22:54:52 -07002241 .fasync = fuse_dev_fasync,
Miklos Szeredi00c570f2015-07-01 16:26:08 +02002242 .unlocked_ioctl = fuse_dev_ioctl,
2243 .compat_ioctl = fuse_dev_ioctl,
Miklos Szeredi334f4852005-09-09 13:10:27 -07002244};
Tejun Heo08cbf542009-04-14 10:54:53 +09002245EXPORT_SYMBOL_GPL(fuse_dev_operations);
Miklos Szeredi334f4852005-09-09 13:10:27 -07002246
2247static struct miscdevice fuse_miscdevice = {
2248 .minor = FUSE_MINOR,
2249 .name = "fuse",
2250 .fops = &fuse_dev_operations,
2251};
2252
2253int __init fuse_dev_init(void)
2254{
2255 int err = -ENOMEM;
2256 fuse_req_cachep = kmem_cache_create("fuse_request",
2257 sizeof(struct fuse_req),
Paul Mundt20c2df82007-07-20 10:11:58 +09002258 0, 0, NULL);
Miklos Szeredi334f4852005-09-09 13:10:27 -07002259 if (!fuse_req_cachep)
2260 goto out;
2261
2262 err = misc_register(&fuse_miscdevice);
2263 if (err)
2264 goto out_cache_clean;
2265
2266 return 0;
2267
2268 out_cache_clean:
2269 kmem_cache_destroy(fuse_req_cachep);
2270 out:
2271 return err;
2272}
2273
2274void fuse_dev_cleanup(void)
2275{
2276 misc_deregister(&fuse_miscdevice);
2277 kmem_cache_destroy(fuse_req_cachep);
2278}