blob: c6b88fa85e2e5d048ea49b268c402843d53c2c0e [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 Szeredi365ae712015-07-01 16:26:06 +0200374 return;
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);
Tejun Heoe9bb09d2008-11-26 12:03:54 +0100403 fuse_put_request(fc, req);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700404}
405
Miklos Szeredif88996a2015-07-01 16:26:01 +0200406static void queue_interrupt(struct fuse_iqueue *fiq, struct fuse_req *req)
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700407{
Miklos Szeredi4ce60812015-07-01 16:26:02 +0200408 spin_lock(&fiq->waitq.lock);
Sahitya Tummala6ba4d272017-02-08 20:30:56 +0530409 if (test_bit(FR_FINISHED, &req->flags)) {
410 spin_unlock(&fiq->waitq.lock);
411 return;
412 }
Miklos Szeredi8f7bb362015-07-01 16:26:03 +0200413 if (list_empty(&req->intr_entry)) {
414 list_add_tail(&req->intr_entry, &fiq->interrupts);
415 wake_up_locked(&fiq->waitq);
416 }
Miklos Szeredi4ce60812015-07-01 16:26:02 +0200417 spin_unlock(&fiq->waitq.lock);
Miklos Szeredif88996a2015-07-01 16:26:01 +0200418 kill_fasync(&fiq->fasync, SIGIO, POLL_IN);
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700419}
420
Miklos Szeredi7c352bd2005-09-09 13:10:39 -0700421static void request_wait_answer(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700422{
Miklos Szeredi4ce60812015-07-01 16:26:02 +0200423 struct fuse_iqueue *fiq = &fc->iq;
Miklos Szeredic4775262015-07-01 16:26:00 +0200424 int err;
425
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700426 if (!fc->no_interrupt) {
427 /* Any signal may interrupt this */
Miklos Szeredic4775262015-07-01 16:26:00 +0200428 err = wait_event_interruptible(req->waitq,
Miklos Szeredi33e14b42015-07-01 16:26:01 +0200429 test_bit(FR_FINISHED, &req->flags));
Miklos Szeredic4775262015-07-01 16:26:00 +0200430 if (!err)
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700431 return;
432
Miklos Szeredi825d6d32015-07-01 16:25:58 +0200433 set_bit(FR_INTERRUPTED, &req->flags);
Miklos Szeredi8f7bb362015-07-01 16:26:03 +0200434 /* matches barrier in fuse_dev_do_read() */
435 smp_mb__after_atomic();
Miklos Szeredi33e14b42015-07-01 16:26:01 +0200436 if (test_bit(FR_SENT, &req->flags))
Miklos Szeredi4ce60812015-07-01 16:26:02 +0200437 queue_interrupt(fiq, req);
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700438 }
439
Miklos Szeredi825d6d32015-07-01 16:25:58 +0200440 if (!test_bit(FR_FORCE, &req->flags)) {
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700441 /* Only fatal signals may interrupt this */
Al Viro7d3a07f2016-07-19 03:08:27 -0400442 err = wait_event_killable(req->waitq,
Miklos Szeredi33e14b42015-07-01 16:26:01 +0200443 test_bit(FR_FINISHED, &req->flags));
Miklos Szeredic4775262015-07-01 16:26:00 +0200444 if (!err)
Miklos Szeredia131de02007-10-16 23:31:04 -0700445 return;
446
Miklos Szeredi4ce60812015-07-01 16:26:02 +0200447 spin_lock(&fiq->waitq.lock);
Miklos Szeredia131de02007-10-16 23:31:04 -0700448 /* Request is not yet in userspace, bail out */
Miklos Szeredi33e14b42015-07-01 16:26:01 +0200449 if (test_bit(FR_PENDING, &req->flags)) {
Miklos Szeredia131de02007-10-16 23:31:04 -0700450 list_del(&req->list);
Miklos Szeredi4ce60812015-07-01 16:26:02 +0200451 spin_unlock(&fiq->waitq.lock);
Miklos Szeredia131de02007-10-16 23:31:04 -0700452 __fuse_put_request(req);
453 req->out.h.error = -EINTR;
454 return;
455 }
Miklos Szeredi4ce60812015-07-01 16:26:02 +0200456 spin_unlock(&fiq->waitq.lock);
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700457 }
Miklos Szeredi334f4852005-09-09 13:10:27 -0700458
Miklos Szeredia131de02007-10-16 23:31:04 -0700459 /*
460 * Either request is already in userspace, or it was forced.
461 * Wait it out.
462 */
Miklos Szeredi33e14b42015-07-01 16:26:01 +0200463 wait_event(req->waitq, test_bit(FR_FINISHED, &req->flags));
Miklos Szeredi334f4852005-09-09 13:10:27 -0700464}
465
Eric Wong6a4e9222013-02-04 13:04:44 +0000466static void __fuse_request_send(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700467{
Miklos Szeredie16714d2015-07-01 16:26:01 +0200468 struct fuse_iqueue *fiq = &fc->iq;
469
Miklos Szeredi825d6d32015-07-01 16:25:58 +0200470 BUG_ON(test_bit(FR_BACKGROUND, &req->flags));
Miklos Szeredi4ce60812015-07-01 16:26:02 +0200471 spin_lock(&fiq->waitq.lock);
Miklos Szeredie16714d2015-07-01 16:26:01 +0200472 if (!fiq->connected) {
Miklos Szeredi4ce60812015-07-01 16:26:02 +0200473 spin_unlock(&fiq->waitq.lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700474 req->out.h.error = -ENOTCONN;
Miklos Szeredic4775262015-07-01 16:26:00 +0200475 } else {
Miklos Szeredif88996a2015-07-01 16:26:01 +0200476 req->in.h.unique = fuse_get_unique(fiq);
477 queue_request(fiq, req);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700478 /* acquire extra reference, since request is still needed
479 after request_end() */
480 __fuse_get_request(req);
Miklos Szeredi4ce60812015-07-01 16:26:02 +0200481 spin_unlock(&fiq->waitq.lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700482
Miklos Szeredi7c352bd2005-09-09 13:10:39 -0700483 request_wait_answer(fc, req);
Miklos Szeredic4775262015-07-01 16:26:00 +0200484 /* Pairs with smp_wmb() in request_end() */
485 smp_rmb();
Miklos Szeredi334f4852005-09-09 13:10:27 -0700486 }
Miklos Szeredi334f4852005-09-09 13:10:27 -0700487}
Eric Wong6a4e9222013-02-04 13:04:44 +0000488
489void fuse_request_send(struct fuse_conn *fc, struct fuse_req *req)
490{
Miklos Szeredi825d6d32015-07-01 16:25:58 +0200491 __set_bit(FR_ISREPLY, &req->flags);
492 if (!test_bit(FR_WAITING, &req->flags)) {
493 __set_bit(FR_WAITING, &req->flags);
Miklos Szeredi5437f242015-07-01 16:25:56 +0200494 atomic_inc(&fc->num_waiting);
495 }
Eric Wong6a4e9222013-02-04 13:04:44 +0000496 __fuse_request_send(fc, req);
497}
Tejun Heo08cbf542009-04-14 10:54:53 +0900498EXPORT_SYMBOL_GPL(fuse_request_send);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700499
Miklos Szeredi21f62172015-01-06 10:45:35 +0100500static void fuse_adjust_compat(struct fuse_conn *fc, struct fuse_args *args)
501{
502 if (fc->minor < 4 && args->in.h.opcode == FUSE_STATFS)
503 args->out.args[0].size = FUSE_COMPAT_STATFS_SIZE;
504
505 if (fc->minor < 9) {
506 switch (args->in.h.opcode) {
507 case FUSE_LOOKUP:
508 case FUSE_CREATE:
509 case FUSE_MKNOD:
510 case FUSE_MKDIR:
511 case FUSE_SYMLINK:
512 case FUSE_LINK:
513 args->out.args[0].size = FUSE_COMPAT_ENTRY_OUT_SIZE;
514 break;
515 case FUSE_GETATTR:
516 case FUSE_SETATTR:
517 args->out.args[0].size = FUSE_COMPAT_ATTR_OUT_SIZE;
518 break;
519 }
520 }
521 if (fc->minor < 12) {
522 switch (args->in.h.opcode) {
523 case FUSE_CREATE:
524 args->in.args[0].size = sizeof(struct fuse_open_in);
525 break;
526 case FUSE_MKNOD:
527 args->in.args[0].size = FUSE_COMPAT_MKNOD_IN_SIZE;
528 break;
529 }
530 }
531}
532
Miklos Szeredi70781872014-12-12 09:49:05 +0100533ssize_t fuse_simple_request(struct fuse_conn *fc, struct fuse_args *args)
534{
535 struct fuse_req *req;
536 ssize_t ret;
537
538 req = fuse_get_req(fc, 0);
539 if (IS_ERR(req))
540 return PTR_ERR(req);
541
Miklos Szeredi21f62172015-01-06 10:45:35 +0100542 /* Needs to be done after fuse_get_req() so that fc->minor is valid */
543 fuse_adjust_compat(fc, args);
544
Miklos Szeredi70781872014-12-12 09:49:05 +0100545 req->in.h.opcode = args->in.h.opcode;
546 req->in.h.nodeid = args->in.h.nodeid;
547 req->in.numargs = args->in.numargs;
548 memcpy(req->in.args, args->in.args,
549 args->in.numargs * sizeof(struct fuse_in_arg));
550 req->out.argvar = args->out.argvar;
551 req->out.numargs = args->out.numargs;
552 memcpy(req->out.args, args->out.args,
553 args->out.numargs * sizeof(struct fuse_arg));
554 fuse_request_send(fc, req);
555 ret = req->out.h.error;
556 if (!ret && args->out.argvar) {
557 BUG_ON(args->out.numargs != 1);
558 ret = req->out.args[0].size;
559 }
560 fuse_put_request(fc, req);
561
562 return ret;
563}
564
Miklos Szeredif0139aa2015-07-01 16:25:57 +0200565/*
566 * Called under fc->lock
567 *
568 * fc->connected must have been checked previously
569 */
570void fuse_request_send_background_locked(struct fuse_conn *fc,
571 struct fuse_req *req)
Miklos Szeredid12def12008-02-06 01:38:39 -0800572{
Miklos Szeredi825d6d32015-07-01 16:25:58 +0200573 BUG_ON(!test_bit(FR_BACKGROUND, &req->flags));
574 if (!test_bit(FR_WAITING, &req->flags)) {
575 __set_bit(FR_WAITING, &req->flags);
Miklos Szeredi5437f242015-07-01 16:25:56 +0200576 atomic_inc(&fc->num_waiting);
577 }
Miklos Szeredi825d6d32015-07-01 16:25:58 +0200578 __set_bit(FR_ISREPLY, &req->flags);
Miklos Szeredid12def12008-02-06 01:38:39 -0800579 fc->num_background++;
Csaba Henk7a6d3c82009-07-01 17:28:41 -0700580 if (fc->num_background == fc->max_background)
Miklos Szeredid12def12008-02-06 01:38:39 -0800581 fc->blocked = 1;
Jan Kara7fbbe972017-04-12 12:24:41 +0200582 if (fc->num_background == fc->congestion_threshold && fc->sb) {
Jan Kara5f7f7542017-04-12 12:24:40 +0200583 set_bdi_congested(fc->sb->s_bdi, BLK_RW_SYNC);
584 set_bdi_congested(fc->sb->s_bdi, BLK_RW_ASYNC);
Miklos Szeredid12def12008-02-06 01:38:39 -0800585 }
586 list_add_tail(&req->list, &fc->bg_queue);
587 flush_bg_queue(fc);
588}
589
Miklos Szeredif0139aa2015-07-01 16:25:57 +0200590void fuse_request_send_background(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700591{
Miklos Szeredi42dc6212015-07-01 16:25:56 +0200592 BUG_ON(!req->end);
Miklos Szeredid7133112006-04-10 22:54:55 -0700593 spin_lock(&fc->lock);
Miklos Szeredi1e9a4ed2005-09-09 13:10:31 -0700594 if (fc->connected) {
Miklos Szeredif0139aa2015-07-01 16:25:57 +0200595 fuse_request_send_background_locked(fc, req);
Miklos Szeredid7133112006-04-10 22:54:55 -0700596 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700597 } else {
Miklos Szeredi42dc6212015-07-01 16:25:56 +0200598 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700599 req->out.h.error = -ENOTCONN;
Miklos Szeredi42dc6212015-07-01 16:25:56 +0200600 req->end(fc, req);
601 fuse_put_request(fc, req);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700602 }
603}
Tejun Heo08cbf542009-04-14 10:54:53 +0900604EXPORT_SYMBOL_GPL(fuse_request_send_background);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700605
Miklos Szeredi2d45ba32010-07-12 14:41:40 +0200606static int fuse_request_send_notify_reply(struct fuse_conn *fc,
607 struct fuse_req *req, u64 unique)
608{
609 int err = -ENODEV;
Miklos Szeredif88996a2015-07-01 16:26:01 +0200610 struct fuse_iqueue *fiq = &fc->iq;
Miklos Szeredi2d45ba32010-07-12 14:41:40 +0200611
Miklos Szeredi825d6d32015-07-01 16:25:58 +0200612 __clear_bit(FR_ISREPLY, &req->flags);
Miklos Szeredi2d45ba32010-07-12 14:41:40 +0200613 req->in.h.unique = unique;
Miklos Szeredi4ce60812015-07-01 16:26:02 +0200614 spin_lock(&fiq->waitq.lock);
Miklos Szeredie16714d2015-07-01 16:26:01 +0200615 if (fiq->connected) {
Miklos Szeredif88996a2015-07-01 16:26:01 +0200616 queue_request(fiq, req);
Miklos Szeredi2d45ba32010-07-12 14:41:40 +0200617 err = 0;
618 }
Miklos Szeredi4ce60812015-07-01 16:26:02 +0200619 spin_unlock(&fiq->waitq.lock);
Miklos Szeredi2d45ba32010-07-12 14:41:40 +0200620
621 return err;
622}
623
Anand V. Avati0b05b182012-08-19 08:53:23 -0400624void fuse_force_forget(struct file *file, u64 nodeid)
625{
Al Viro6131ffa2013-02-27 16:59:05 -0500626 struct inode *inode = file_inode(file);
Anand V. Avati0b05b182012-08-19 08:53:23 -0400627 struct fuse_conn *fc = get_fuse_conn(inode);
628 struct fuse_req *req;
629 struct fuse_forget_in inarg;
630
631 memset(&inarg, 0, sizeof(inarg));
632 inarg.nlookup = 1;
Maxim Patlasovb111c8c2012-10-26 19:48:30 +0400633 req = fuse_get_req_nofail_nopages(fc, file);
Anand V. Avati0b05b182012-08-19 08:53:23 -0400634 req->in.h.opcode = FUSE_FORGET;
635 req->in.h.nodeid = nodeid;
636 req->in.numargs = 1;
637 req->in.args[0].size = sizeof(inarg);
638 req->in.args[0].value = &inarg;
Miklos Szeredi825d6d32015-07-01 16:25:58 +0200639 __clear_bit(FR_ISREPLY, &req->flags);
Eric Wong6a4e9222013-02-04 13:04:44 +0000640 __fuse_request_send(fc, req);
641 /* ignore errors */
642 fuse_put_request(fc, req);
Anand V. Avati0b05b182012-08-19 08:53:23 -0400643}
644
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700645/*
Miklos Szeredi334f4852005-09-09 13:10:27 -0700646 * Lock the request. Up to the next unlock_request() there mustn't be
647 * anything that could cause a page-fault. If the request was already
Miklos Szeredif9a28422006-06-25 05:48:53 -0700648 * aborted bail out.
Miklos Szeredi334f4852005-09-09 13:10:27 -0700649 */
Miklos Szeredidc008092015-07-01 16:25:58 +0200650static int lock_request(struct fuse_req *req)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700651{
652 int err = 0;
653 if (req) {
Miklos Szeredidc008092015-07-01 16:25:58 +0200654 spin_lock(&req->waitq.lock);
Miklos Szeredi825d6d32015-07-01 16:25:58 +0200655 if (test_bit(FR_ABORTED, &req->flags))
Miklos Szeredi334f4852005-09-09 13:10:27 -0700656 err = -ENOENT;
657 else
Miklos Szeredi825d6d32015-07-01 16:25:58 +0200658 set_bit(FR_LOCKED, &req->flags);
Miklos Szeredidc008092015-07-01 16:25:58 +0200659 spin_unlock(&req->waitq.lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700660 }
661 return err;
662}
663
664/*
Miklos Szeredi0d8e84b2015-07-01 16:25:58 +0200665 * Unlock request. If it was aborted while locked, caller is responsible
666 * for unlocking and ending the request.
Miklos Szeredi334f4852005-09-09 13:10:27 -0700667 */
Miklos Szeredidc008092015-07-01 16:25:58 +0200668static int unlock_request(struct fuse_req *req)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700669{
Miklos Szeredi0d8e84b2015-07-01 16:25:58 +0200670 int err = 0;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700671 if (req) {
Miklos Szeredidc008092015-07-01 16:25:58 +0200672 spin_lock(&req->waitq.lock);
Miklos Szeredi825d6d32015-07-01 16:25:58 +0200673 if (test_bit(FR_ABORTED, &req->flags))
Miklos Szeredi0d8e84b2015-07-01 16:25:58 +0200674 err = -ENOENT;
675 else
Miklos Szeredi825d6d32015-07-01 16:25:58 +0200676 clear_bit(FR_LOCKED, &req->flags);
Miklos Szeredidc008092015-07-01 16:25:58 +0200677 spin_unlock(&req->waitq.lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700678 }
Miklos Szeredi0d8e84b2015-07-01 16:25:58 +0200679 return err;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700680}
681
682struct fuse_copy_state {
683 int write;
684 struct fuse_req *req;
Al Viro6c09e942015-04-03 22:06:08 -0400685 struct iov_iter *iter;
Miklos Szeredidd3bb142010-05-25 15:06:06 +0200686 struct pipe_buffer *pipebufs;
687 struct pipe_buffer *currbuf;
688 struct pipe_inode_info *pipe;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700689 unsigned long nr_segs;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700690 struct page *pg;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700691 unsigned len;
Miklos Szeredic55a01d2014-07-07 15:28:51 +0200692 unsigned offset;
Miklos Szeredice534fb2010-05-25 15:06:07 +0200693 unsigned move_pages:1;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700694};
695
Miklos Szeredidc008092015-07-01 16:25:58 +0200696static void fuse_copy_init(struct fuse_copy_state *cs, int write,
Al Viro6c09e942015-04-03 22:06:08 -0400697 struct iov_iter *iter)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700698{
699 memset(cs, 0, sizeof(*cs));
700 cs->write = write;
Al Viro6c09e942015-04-03 22:06:08 -0400701 cs->iter = iter;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700702}
703
704/* Unmap and put previous page of userspace buffer */
Miklos Szeredi8bfc0162006-01-16 22:14:28 -0800705static void fuse_copy_finish(struct fuse_copy_state *cs)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700706{
Miklos Szeredidd3bb142010-05-25 15:06:06 +0200707 if (cs->currbuf) {
708 struct pipe_buffer *buf = cs->currbuf;
709
Miklos Szeredic55a01d2014-07-07 15:28:51 +0200710 if (cs->write)
Miklos Szeredic3021622010-05-25 15:06:07 +0200711 buf->len = PAGE_SIZE - cs->len;
Miklos Szeredidd3bb142010-05-25 15:06:06 +0200712 cs->currbuf = NULL;
Miklos Szeredic55a01d2014-07-07 15:28:51 +0200713 } else if (cs->pg) {
Miklos Szeredi334f4852005-09-09 13:10:27 -0700714 if (cs->write) {
715 flush_dcache_page(cs->pg);
716 set_page_dirty_lock(cs->pg);
717 }
718 put_page(cs->pg);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700719 }
Miklos Szeredic55a01d2014-07-07 15:28:51 +0200720 cs->pg = NULL;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700721}
722
723/*
724 * Get another pagefull of userspace buffer, and map it to kernel
725 * address space, and lock request
726 */
727static int fuse_copy_fill(struct fuse_copy_state *cs)
728{
Miklos Szeredic55a01d2014-07-07 15:28:51 +0200729 struct page *page;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700730 int err;
731
Miklos Szeredidc008092015-07-01 16:25:58 +0200732 err = unlock_request(cs->req);
Miklos Szeredi0d8e84b2015-07-01 16:25:58 +0200733 if (err)
734 return err;
735
Miklos Szeredi334f4852005-09-09 13:10:27 -0700736 fuse_copy_finish(cs);
Miklos Szeredidd3bb142010-05-25 15:06:06 +0200737 if (cs->pipebufs) {
738 struct pipe_buffer *buf = cs->pipebufs;
739
Miklos Szeredic3021622010-05-25 15:06:07 +0200740 if (!cs->write) {
Miklos Szeredifba597d2016-09-27 10:45:12 +0200741 err = pipe_buf_confirm(cs->pipe, buf);
Miklos Szeredic3021622010-05-25 15:06:07 +0200742 if (err)
743 return err;
Miklos Szeredidd3bb142010-05-25 15:06:06 +0200744
Miklos Szeredic3021622010-05-25 15:06:07 +0200745 BUG_ON(!cs->nr_segs);
746 cs->currbuf = buf;
Miklos Szeredic55a01d2014-07-07 15:28:51 +0200747 cs->pg = buf->page;
748 cs->offset = buf->offset;
Miklos Szeredic3021622010-05-25 15:06:07 +0200749 cs->len = buf->len;
Miklos Szeredic3021622010-05-25 15:06:07 +0200750 cs->pipebufs++;
751 cs->nr_segs--;
752 } else {
Miklos Szeredic3021622010-05-25 15:06:07 +0200753 if (cs->nr_segs == cs->pipe->buffers)
754 return -EIO;
755
756 page = alloc_page(GFP_HIGHUSER);
757 if (!page)
758 return -ENOMEM;
759
760 buf->page = page;
761 buf->offset = 0;
762 buf->len = 0;
763
764 cs->currbuf = buf;
Miklos Szeredic55a01d2014-07-07 15:28:51 +0200765 cs->pg = page;
766 cs->offset = 0;
Miklos Szeredic3021622010-05-25 15:06:07 +0200767 cs->len = PAGE_SIZE;
768 cs->pipebufs++;
769 cs->nr_segs++;
770 }
Miklos Szeredidd3bb142010-05-25 15:06:06 +0200771 } else {
Al Viro6c09e942015-04-03 22:06:08 -0400772 size_t off;
773 err = iov_iter_get_pages(cs->iter, &page, PAGE_SIZE, 1, &off);
Miklos Szeredidd3bb142010-05-25 15:06:06 +0200774 if (err < 0)
775 return err;
Al Viro6c09e942015-04-03 22:06:08 -0400776 BUG_ON(!err);
777 cs->len = err;
778 cs->offset = off;
Miklos Szeredic55a01d2014-07-07 15:28:51 +0200779 cs->pg = page;
Al Viro6c09e942015-04-03 22:06:08 -0400780 iov_iter_advance(cs->iter, err);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700781 }
Miklos Szeredi334f4852005-09-09 13:10:27 -0700782
Miklos Szeredidc008092015-07-01 16:25:58 +0200783 return lock_request(cs->req);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700784}
785
786/* Do as much copy to/from userspace buffer as we can */
Miklos Szeredi8bfc0162006-01-16 22:14:28 -0800787static int fuse_copy_do(struct fuse_copy_state *cs, void **val, unsigned *size)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700788{
789 unsigned ncpy = min(*size, cs->len);
790 if (val) {
Miklos Szeredic55a01d2014-07-07 15:28:51 +0200791 void *pgaddr = kmap_atomic(cs->pg);
792 void *buf = pgaddr + cs->offset;
793
Miklos Szeredi334f4852005-09-09 13:10:27 -0700794 if (cs->write)
Miklos Szeredic55a01d2014-07-07 15:28:51 +0200795 memcpy(buf, *val, ncpy);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700796 else
Miklos Szeredic55a01d2014-07-07 15:28:51 +0200797 memcpy(*val, buf, ncpy);
798
799 kunmap_atomic(pgaddr);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700800 *val += ncpy;
801 }
802 *size -= ncpy;
803 cs->len -= ncpy;
Miklos Szeredic55a01d2014-07-07 15:28:51 +0200804 cs->offset += ncpy;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700805 return ncpy;
806}
807
Miklos Szeredice534fb2010-05-25 15:06:07 +0200808static int fuse_check_page(struct page *page)
809{
810 if (page_mapcount(page) ||
811 page->mapping != NULL ||
812 page_count(page) != 1 ||
813 (page->flags & PAGE_FLAGS_CHECK_AT_PREP &
814 ~(1 << PG_locked |
815 1 << PG_referenced |
816 1 << PG_uptodate |
817 1 << PG_lru |
818 1 << PG_active |
819 1 << PG_reclaim))) {
820 printk(KERN_WARNING "fuse: trying to steal weird page\n");
821 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);
822 return 1;
823 }
824 return 0;
825}
826
827static int fuse_try_move_page(struct fuse_copy_state *cs, struct page **pagep)
828{
829 int err;
830 struct page *oldpage = *pagep;
831 struct page *newpage;
832 struct pipe_buffer *buf = cs->pipebufs;
Miklos Szeredice534fb2010-05-25 15:06:07 +0200833
Miklos Szeredidc008092015-07-01 16:25:58 +0200834 err = unlock_request(cs->req);
Miklos Szeredi0d8e84b2015-07-01 16:25:58 +0200835 if (err)
836 return err;
837
Miklos Szeredice534fb2010-05-25 15:06:07 +0200838 fuse_copy_finish(cs);
839
Miklos Szeredifba597d2016-09-27 10:45:12 +0200840 err = pipe_buf_confirm(cs->pipe, buf);
Miklos Szeredice534fb2010-05-25 15:06:07 +0200841 if (err)
842 return err;
843
844 BUG_ON(!cs->nr_segs);
845 cs->currbuf = buf;
846 cs->len = buf->len;
847 cs->pipebufs++;
848 cs->nr_segs--;
849
850 if (cs->len != PAGE_SIZE)
851 goto out_fallback;
852
Miklos Szeredica76f5b2016-09-27 10:45:12 +0200853 if (pipe_buf_steal(cs->pipe, buf) != 0)
Miklos Szeredice534fb2010-05-25 15:06:07 +0200854 goto out_fallback;
855
856 newpage = buf->page;
857
Miklos Szerediaa991b32015-02-26 11:45:47 +0100858 if (!PageUptodate(newpage))
859 SetPageUptodate(newpage);
Miklos Szeredice534fb2010-05-25 15:06:07 +0200860
861 ClearPageMappedToDisk(newpage);
862
863 if (fuse_check_page(newpage) != 0)
864 goto out_fallback_unlock;
865
Miklos Szeredice534fb2010-05-25 15:06:07 +0200866 /*
867 * This is a new and locked page, it shouldn't be mapped or
868 * have any special flags on it
869 */
870 if (WARN_ON(page_mapped(oldpage)))
871 goto out_fallback_unlock;
872 if (WARN_ON(page_has_private(oldpage)))
873 goto out_fallback_unlock;
874 if (WARN_ON(PageDirty(oldpage) || PageWriteback(oldpage)))
875 goto out_fallback_unlock;
876 if (WARN_ON(PageMlocked(oldpage)))
877 goto out_fallback_unlock;
878
Miklos Szeredief6a3c62011-03-22 16:30:52 -0700879 err = replace_page_cache_page(oldpage, newpage, GFP_KERNEL);
Miklos Szeredice534fb2010-05-25 15:06:07 +0200880 if (err) {
Miklos Szeredief6a3c62011-03-22 16:30:52 -0700881 unlock_page(newpage);
882 return err;
Miklos Szeredice534fb2010-05-25 15:06:07 +0200883 }
Miklos Szeredief6a3c62011-03-22 16:30:52 -0700884
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300885 get_page(newpage);
Miklos Szeredice534fb2010-05-25 15:06:07 +0200886
887 if (!(buf->flags & PIPE_BUF_FLAG_LRU))
888 lru_cache_add_file(newpage);
889
890 err = 0;
Miklos Szeredidc008092015-07-01 16:25:58 +0200891 spin_lock(&cs->req->waitq.lock);
Miklos Szeredi825d6d32015-07-01 16:25:58 +0200892 if (test_bit(FR_ABORTED, &cs->req->flags))
Miklos Szeredice534fb2010-05-25 15:06:07 +0200893 err = -ENOENT;
894 else
895 *pagep = newpage;
Miklos Szeredidc008092015-07-01 16:25:58 +0200896 spin_unlock(&cs->req->waitq.lock);
Miklos Szeredice534fb2010-05-25 15:06:07 +0200897
898 if (err) {
899 unlock_page(newpage);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300900 put_page(newpage);
Miklos Szeredice534fb2010-05-25 15:06:07 +0200901 return err;
902 }
903
904 unlock_page(oldpage);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300905 put_page(oldpage);
Miklos Szeredice534fb2010-05-25 15:06:07 +0200906 cs->len = 0;
907
908 return 0;
909
910out_fallback_unlock:
911 unlock_page(newpage);
912out_fallback:
Miklos Szeredic55a01d2014-07-07 15:28:51 +0200913 cs->pg = buf->page;
914 cs->offset = buf->offset;
Miklos Szeredice534fb2010-05-25 15:06:07 +0200915
Miklos Szeredidc008092015-07-01 16:25:58 +0200916 err = lock_request(cs->req);
Miklos Szeredice534fb2010-05-25 15:06:07 +0200917 if (err)
918 return err;
919
920 return 1;
921}
922
Miklos Szeredic3021622010-05-25 15:06:07 +0200923static int fuse_ref_page(struct fuse_copy_state *cs, struct page *page,
924 unsigned offset, unsigned count)
925{
926 struct pipe_buffer *buf;
Miklos Szeredi0d8e84b2015-07-01 16:25:58 +0200927 int err;
Miklos Szeredic3021622010-05-25 15:06:07 +0200928
929 if (cs->nr_segs == cs->pipe->buffers)
930 return -EIO;
931
Miklos Szeredidc008092015-07-01 16:25:58 +0200932 err = unlock_request(cs->req);
Miklos Szeredi0d8e84b2015-07-01 16:25:58 +0200933 if (err)
934 return err;
935
Miklos Szeredic3021622010-05-25 15:06:07 +0200936 fuse_copy_finish(cs);
937
938 buf = cs->pipebufs;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300939 get_page(page);
Miklos Szeredic3021622010-05-25 15:06:07 +0200940 buf->page = page;
941 buf->offset = offset;
942 buf->len = count;
943
944 cs->pipebufs++;
945 cs->nr_segs++;
946 cs->len = 0;
947
948 return 0;
949}
950
Miklos Szeredi334f4852005-09-09 13:10:27 -0700951/*
952 * Copy a page in the request to/from the userspace buffer. Must be
953 * done atomically
954 */
Miklos Szeredice534fb2010-05-25 15:06:07 +0200955static int fuse_copy_page(struct fuse_copy_state *cs, struct page **pagep,
Miklos Szeredi8bfc0162006-01-16 22:14:28 -0800956 unsigned offset, unsigned count, int zeroing)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700957{
Miklos Szeredice534fb2010-05-25 15:06:07 +0200958 int err;
959 struct page *page = *pagep;
960
Miklos Szeredib6777c42010-10-26 14:22:27 -0700961 if (page && zeroing && count < PAGE_SIZE)
962 clear_highpage(page);
963
Miklos Szeredi334f4852005-09-09 13:10:27 -0700964 while (count) {
Miklos Szeredic3021622010-05-25 15:06:07 +0200965 if (cs->write && cs->pipebufs && page) {
966 return fuse_ref_page(cs, page, offset, count);
967 } else if (!cs->len) {
Miklos Szeredice534fb2010-05-25 15:06:07 +0200968 if (cs->move_pages && page &&
969 offset == 0 && count == PAGE_SIZE) {
970 err = fuse_try_move_page(cs, pagep);
971 if (err <= 0)
972 return err;
973 } else {
974 err = fuse_copy_fill(cs);
975 if (err)
976 return err;
977 }
Miklos Szeredi1729a162008-11-26 12:03:54 +0100978 }
Miklos Szeredi334f4852005-09-09 13:10:27 -0700979 if (page) {
Cong Wang2408f6e2011-11-25 23:14:30 +0800980 void *mapaddr = kmap_atomic(page);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700981 void *buf = mapaddr + offset;
982 offset += fuse_copy_do(cs, &buf, &count);
Cong Wang2408f6e2011-11-25 23:14:30 +0800983 kunmap_atomic(mapaddr);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700984 } else
985 offset += fuse_copy_do(cs, NULL, &count);
986 }
987 if (page && !cs->write)
988 flush_dcache_page(page);
989 return 0;
990}
991
992/* Copy pages in the request to/from userspace buffer */
993static int fuse_copy_pages(struct fuse_copy_state *cs, unsigned nbytes,
994 int zeroing)
995{
996 unsigned i;
997 struct fuse_req *req = cs->req;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700998
999 for (i = 0; i < req->num_pages && (nbytes || zeroing); i++) {
Miklos Szeredice534fb2010-05-25 15:06:07 +02001000 int err;
Maxim Patlasov85f40ae2012-10-26 19:49:33 +04001001 unsigned offset = req->page_descs[i].offset;
1002 unsigned count = min(nbytes, req->page_descs[i].length);
Miklos Szeredice534fb2010-05-25 15:06:07 +02001003
1004 err = fuse_copy_page(cs, &req->pages[i], offset, count,
1005 zeroing);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001006 if (err)
1007 return err;
1008
1009 nbytes -= count;
Miklos Szeredi334f4852005-09-09 13:10:27 -07001010 }
1011 return 0;
1012}
1013
1014/* Copy a single argument in the request to/from userspace buffer */
1015static int fuse_copy_one(struct fuse_copy_state *cs, void *val, unsigned size)
1016{
1017 while (size) {
Miklos Szeredi1729a162008-11-26 12:03:54 +01001018 if (!cs->len) {
1019 int err = fuse_copy_fill(cs);
1020 if (err)
1021 return err;
1022 }
Miklos Szeredi334f4852005-09-09 13:10:27 -07001023 fuse_copy_do(cs, &val, &size);
1024 }
1025 return 0;
1026}
1027
1028/* Copy request arguments to/from userspace buffer */
1029static int fuse_copy_args(struct fuse_copy_state *cs, unsigned numargs,
1030 unsigned argpages, struct fuse_arg *args,
1031 int zeroing)
1032{
1033 int err = 0;
1034 unsigned i;
1035
1036 for (i = 0; !err && i < numargs; i++) {
1037 struct fuse_arg *arg = &args[i];
1038 if (i == numargs - 1 && argpages)
1039 err = fuse_copy_pages(cs, arg->size, zeroing);
1040 else
1041 err = fuse_copy_one(cs, arg->value, arg->size);
1042 }
1043 return err;
1044}
1045
Miklos Szeredif88996a2015-07-01 16:26:01 +02001046static int forget_pending(struct fuse_iqueue *fiq)
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001047{
Miklos Szeredif88996a2015-07-01 16:26:01 +02001048 return fiq->forget_list_head.next != NULL;
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001049}
1050
Miklos Szeredif88996a2015-07-01 16:26:01 +02001051static int request_pending(struct fuse_iqueue *fiq)
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001052{
Miklos Szeredif88996a2015-07-01 16:26:01 +02001053 return !list_empty(&fiq->pending) || !list_empty(&fiq->interrupts) ||
1054 forget_pending(fiq);
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001055}
1056
Miklos Szeredi334f4852005-09-09 13:10:27 -07001057/*
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001058 * Transfer an interrupt request to userspace
1059 *
1060 * Unlike other requests this is assembled on demand, without a need
1061 * to allocate a separate fuse_req structure.
1062 *
Miklos Szeredifd22d622015-07-01 16:26:03 +02001063 * Called with fiq->waitq.lock held, releases it
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001064 */
Miklos Szeredifd22d622015-07-01 16:26:03 +02001065static int fuse_read_interrupt(struct fuse_iqueue *fiq,
1066 struct fuse_copy_state *cs,
Miklos Szeredic3021622010-05-25 15:06:07 +02001067 size_t nbytes, struct fuse_req *req)
Miklos Szeredifd22d622015-07-01 16:26:03 +02001068__releases(fiq->waitq.lock)
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001069{
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001070 struct fuse_in_header ih;
1071 struct fuse_interrupt_in arg;
1072 unsigned reqsize = sizeof(ih) + sizeof(arg);
1073 int err;
1074
1075 list_del_init(&req->intr_entry);
Miklos Szeredi4ce60812015-07-01 16:26:02 +02001076 req->intr_unique = fuse_get_unique(fiq);
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001077 memset(&ih, 0, sizeof(ih));
1078 memset(&arg, 0, sizeof(arg));
1079 ih.len = reqsize;
1080 ih.opcode = FUSE_INTERRUPT;
1081 ih.unique = req->intr_unique;
1082 arg.unique = req->in.h.unique;
1083
Miklos Szeredi4ce60812015-07-01 16:26:02 +02001084 spin_unlock(&fiq->waitq.lock);
Miklos Szeredic3021622010-05-25 15:06:07 +02001085 if (nbytes < reqsize)
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001086 return -EINVAL;
1087
Miklos Szeredic3021622010-05-25 15:06:07 +02001088 err = fuse_copy_one(cs, &ih, sizeof(ih));
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001089 if (!err)
Miklos Szeredic3021622010-05-25 15:06:07 +02001090 err = fuse_copy_one(cs, &arg, sizeof(arg));
1091 fuse_copy_finish(cs);
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001092
1093 return err ? err : reqsize;
1094}
1095
Miklos Szeredif88996a2015-07-01 16:26:01 +02001096static struct fuse_forget_link *dequeue_forget(struct fuse_iqueue *fiq,
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001097 unsigned max,
1098 unsigned *countp)
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001099{
Miklos Szeredif88996a2015-07-01 16:26:01 +02001100 struct fuse_forget_link *head = fiq->forget_list_head.next;
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001101 struct fuse_forget_link **newhead = &head;
1102 unsigned count;
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001103
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001104 for (count = 0; *newhead != NULL && count < max; count++)
1105 newhead = &(*newhead)->next;
1106
Miklos Szeredif88996a2015-07-01 16:26:01 +02001107 fiq->forget_list_head.next = *newhead;
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001108 *newhead = NULL;
Miklos Szeredif88996a2015-07-01 16:26:01 +02001109 if (fiq->forget_list_head.next == NULL)
1110 fiq->forget_list_tail = &fiq->forget_list_head;
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001111
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001112 if (countp != NULL)
1113 *countp = count;
1114
1115 return head;
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001116}
1117
Miklos Szeredifd22d622015-07-01 16:26:03 +02001118static int fuse_read_single_forget(struct fuse_iqueue *fiq,
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001119 struct fuse_copy_state *cs,
1120 size_t nbytes)
Miklos Szeredifd22d622015-07-01 16:26:03 +02001121__releases(fiq->waitq.lock)
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001122{
1123 int err;
Miklos Szeredif88996a2015-07-01 16:26:01 +02001124 struct fuse_forget_link *forget = dequeue_forget(fiq, 1, NULL);
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001125 struct fuse_forget_in arg = {
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001126 .nlookup = forget->forget_one.nlookup,
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001127 };
1128 struct fuse_in_header ih = {
1129 .opcode = FUSE_FORGET,
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001130 .nodeid = forget->forget_one.nodeid,
Miklos Szeredif88996a2015-07-01 16:26:01 +02001131 .unique = fuse_get_unique(fiq),
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001132 .len = sizeof(ih) + sizeof(arg),
1133 };
1134
Miklos Szeredi4ce60812015-07-01 16:26:02 +02001135 spin_unlock(&fiq->waitq.lock);
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001136 kfree(forget);
1137 if (nbytes < ih.len)
1138 return -EINVAL;
1139
1140 err = fuse_copy_one(cs, &ih, sizeof(ih));
1141 if (!err)
1142 err = fuse_copy_one(cs, &arg, sizeof(arg));
1143 fuse_copy_finish(cs);
1144
1145 if (err)
1146 return err;
1147
1148 return ih.len;
1149}
1150
Miklos Szeredifd22d622015-07-01 16:26:03 +02001151static int fuse_read_batch_forget(struct fuse_iqueue *fiq,
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001152 struct fuse_copy_state *cs, size_t nbytes)
Miklos Szeredifd22d622015-07-01 16:26:03 +02001153__releases(fiq->waitq.lock)
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001154{
1155 int err;
1156 unsigned max_forgets;
1157 unsigned count;
1158 struct fuse_forget_link *head;
1159 struct fuse_batch_forget_in arg = { .count = 0 };
1160 struct fuse_in_header ih = {
1161 .opcode = FUSE_BATCH_FORGET,
Miklos Szeredif88996a2015-07-01 16:26:01 +02001162 .unique = fuse_get_unique(fiq),
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001163 .len = sizeof(ih) + sizeof(arg),
1164 };
1165
1166 if (nbytes < ih.len) {
Miklos Szeredi4ce60812015-07-01 16:26:02 +02001167 spin_unlock(&fiq->waitq.lock);
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001168 return -EINVAL;
1169 }
1170
1171 max_forgets = (nbytes - ih.len) / sizeof(struct fuse_forget_one);
Miklos Szeredif88996a2015-07-01 16:26:01 +02001172 head = dequeue_forget(fiq, max_forgets, &count);
Miklos Szeredi4ce60812015-07-01 16:26:02 +02001173 spin_unlock(&fiq->waitq.lock);
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001174
1175 arg.count = count;
1176 ih.len += count * sizeof(struct fuse_forget_one);
1177 err = fuse_copy_one(cs, &ih, sizeof(ih));
1178 if (!err)
1179 err = fuse_copy_one(cs, &arg, sizeof(arg));
1180
1181 while (head) {
1182 struct fuse_forget_link *forget = head;
1183
1184 if (!err) {
1185 err = fuse_copy_one(cs, &forget->forget_one,
1186 sizeof(forget->forget_one));
1187 }
1188 head = forget->next;
1189 kfree(forget);
1190 }
1191
1192 fuse_copy_finish(cs);
1193
1194 if (err)
1195 return err;
1196
1197 return ih.len;
1198}
1199
Miklos Szeredifd22d622015-07-01 16:26:03 +02001200static int fuse_read_forget(struct fuse_conn *fc, struct fuse_iqueue *fiq,
1201 struct fuse_copy_state *cs,
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001202 size_t nbytes)
Miklos Szeredifd22d622015-07-01 16:26:03 +02001203__releases(fiq->waitq.lock)
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001204{
Miklos Szeredif88996a2015-07-01 16:26:01 +02001205 if (fc->minor < 16 || fiq->forget_list_head.next->next == NULL)
Miklos Szeredifd22d622015-07-01 16:26:03 +02001206 return fuse_read_single_forget(fiq, cs, nbytes);
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001207 else
Miklos Szeredifd22d622015-07-01 16:26:03 +02001208 return fuse_read_batch_forget(fiq, cs, nbytes);
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001209}
1210
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001211/*
Miklos Szeredi334f4852005-09-09 13:10:27 -07001212 * Read a single request into the userspace filesystem's buffer. This
1213 * function waits until a request is available, then removes it from
1214 * the pending list and copies request data to userspace buffer. If
Miklos Szeredif9a28422006-06-25 05:48:53 -07001215 * no reply is needed (FORGET) or request has been aborted or there
1216 * was an error during the copying then it's finished by calling
Miklos Szeredi334f4852005-09-09 13:10:27 -07001217 * request_end(). Otherwise add it to the processing list, and set
1218 * the 'sent' flag.
1219 */
Miklos Szeredic36960462015-07-01 16:26:09 +02001220static ssize_t fuse_dev_do_read(struct fuse_dev *fud, struct file *file,
Miklos Szeredic3021622010-05-25 15:06:07 +02001221 struct fuse_copy_state *cs, size_t nbytes)
Miklos Szeredi334f4852005-09-09 13:10:27 -07001222{
Miklos Szeredi82cbdcd2015-07-01 16:26:05 +02001223 ssize_t err;
Miklos Szeredic36960462015-07-01 16:26:09 +02001224 struct fuse_conn *fc = fud->fc;
Miklos Szeredif88996a2015-07-01 16:26:01 +02001225 struct fuse_iqueue *fiq = &fc->iq;
Miklos Szeredic36960462015-07-01 16:26:09 +02001226 struct fuse_pqueue *fpq = &fud->pq;
Miklos Szeredi334f4852005-09-09 13:10:27 -07001227 struct fuse_req *req;
1228 struct fuse_in *in;
Miklos Szeredi334f4852005-09-09 13:10:27 -07001229 unsigned reqsize;
1230
Miklos Szeredi1d3d7522006-01-06 00:19:40 -08001231 restart:
Miklos Szeredi4ce60812015-07-01 16:26:02 +02001232 spin_lock(&fiq->waitq.lock);
Jeff Dikee5ac1d12006-04-10 22:54:53 -07001233 err = -EAGAIN;
Miklos Szeredie16714d2015-07-01 16:26:01 +02001234 if ((file->f_flags & O_NONBLOCK) && fiq->connected &&
Miklos Szeredif88996a2015-07-01 16:26:01 +02001235 !request_pending(fiq))
Jeff Dikee5ac1d12006-04-10 22:54:53 -07001236 goto err_unlock;
1237
Miklos Szeredi52509212015-07-01 16:26:03 +02001238 err = wait_event_interruptible_exclusive_locked(fiq->waitq,
1239 !fiq->connected || request_pending(fiq));
1240 if (err)
1241 goto err_unlock;
1242
Szymon Lukasz3b7008b2017-11-09 21:23:35 +01001243 if (!fiq->connected) {
1244 err = (fc->aborted && fc->abort_err) ? -ECONNABORTED : -ENODEV;
Miklos Szeredi334f4852005-09-09 13:10:27 -07001245 goto err_unlock;
Szymon Lukasz3b7008b2017-11-09 21:23:35 +01001246 }
Miklos Szeredi334f4852005-09-09 13:10:27 -07001247
Miklos Szeredif88996a2015-07-01 16:26:01 +02001248 if (!list_empty(&fiq->interrupts)) {
1249 req = list_entry(fiq->interrupts.next, struct fuse_req,
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001250 intr_entry);
Miklos Szeredifd22d622015-07-01 16:26:03 +02001251 return fuse_read_interrupt(fiq, cs, nbytes, req);
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001252 }
1253
Miklos Szeredif88996a2015-07-01 16:26:01 +02001254 if (forget_pending(fiq)) {
1255 if (list_empty(&fiq->pending) || fiq->forget_batch-- > 0)
Miklos Szeredifd22d622015-07-01 16:26:03 +02001256 return fuse_read_forget(fc, fiq, cs, nbytes);
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001257
Miklos Szeredif88996a2015-07-01 16:26:01 +02001258 if (fiq->forget_batch <= -8)
1259 fiq->forget_batch = 16;
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001260 }
1261
Miklos Szeredif88996a2015-07-01 16:26:01 +02001262 req = list_entry(fiq->pending.next, struct fuse_req, list);
Miklos Szeredi33e14b42015-07-01 16:26:01 +02001263 clear_bit(FR_PENDING, &req->flags);
Miklos Szeredief759252015-07-01 16:26:02 +02001264 list_del_init(&req->list);
Miklos Szeredi4ce60812015-07-01 16:26:02 +02001265 spin_unlock(&fiq->waitq.lock);
1266
Miklos Szeredi334f4852005-09-09 13:10:27 -07001267 in = &req->in;
Miklos Szeredi1d3d7522006-01-06 00:19:40 -08001268 reqsize = in->h.len;
Miklos Szeredi5d6d3a32017-09-12 16:57:53 +02001269
Miklos Szeredi1d3d7522006-01-06 00:19:40 -08001270 /* If request is too large, reply with an error and restart the read */
Miklos Szeredic3021622010-05-25 15:06:07 +02001271 if (nbytes < reqsize) {
Miklos Szeredi1d3d7522006-01-06 00:19:40 -08001272 req->out.h.error = -EIO;
1273 /* SETXATTR is special, since it may contain too large data */
1274 if (in->h.opcode == FUSE_SETXATTR)
1275 req->out.h.error = -E2BIG;
1276 request_end(fc, req);
1277 goto restart;
Miklos Szeredi334f4852005-09-09 13:10:27 -07001278 }
Miklos Szeredi45a91cb2015-07-01 16:26:06 +02001279 spin_lock(&fpq->lock);
Miklos Szeredi82cbdcd2015-07-01 16:26:05 +02001280 list_add(&req->list, &fpq->io);
Miklos Szeredi45a91cb2015-07-01 16:26:06 +02001281 spin_unlock(&fpq->lock);
Miklos Szeredic3021622010-05-25 15:06:07 +02001282 cs->req = req;
1283 err = fuse_copy_one(cs, &in->h, sizeof(in->h));
Miklos Szeredi1d3d7522006-01-06 00:19:40 -08001284 if (!err)
Miklos Szeredic3021622010-05-25 15:06:07 +02001285 err = fuse_copy_args(cs, in->numargs, in->argpages,
Miklos Szeredi1d3d7522006-01-06 00:19:40 -08001286 (struct fuse_arg *) in->args, 0);
Miklos Szeredic3021622010-05-25 15:06:07 +02001287 fuse_copy_finish(cs);
Miklos Szeredi45a91cb2015-07-01 16:26:06 +02001288 spin_lock(&fpq->lock);
Miklos Szeredi825d6d32015-07-01 16:25:58 +02001289 clear_bit(FR_LOCKED, &req->flags);
Miklos Szeredie96edd92015-07-01 16:26:04 +02001290 if (!fpq->connected) {
Szymon Lukasz3b7008b2017-11-09 21:23:35 +01001291 err = (fc->aborted && fc->abort_err) ? -ECONNABORTED : -ENODEV;
Miklos Szeredi82cbdcd2015-07-01 16:26:05 +02001292 goto out_end;
Miklos Szeredic9c9d7d2007-10-16 23:31:05 -07001293 }
Miklos Szeredi334f4852005-09-09 13:10:27 -07001294 if (err) {
Miklos Szeredic9c9d7d2007-10-16 23:31:05 -07001295 req->out.h.error = -EIO;
Miklos Szeredi82cbdcd2015-07-01 16:26:05 +02001296 goto out_end;
Miklos Szeredi334f4852005-09-09 13:10:27 -07001297 }
Miklos Szeredi825d6d32015-07-01 16:25:58 +02001298 if (!test_bit(FR_ISREPLY, &req->flags)) {
Miklos Szeredi82cbdcd2015-07-01 16:26:05 +02001299 err = reqsize;
1300 goto out_end;
Miklos Szeredi334f4852005-09-09 13:10:27 -07001301 }
Miklos Szeredi82cbdcd2015-07-01 16:26:05 +02001302 list_move_tail(&req->list, &fpq->processing);
Miklos Szeredi45a91cb2015-07-01 16:26:06 +02001303 spin_unlock(&fpq->lock);
Miklos Szeredi82cbdcd2015-07-01 16:26:05 +02001304 set_bit(FR_SENT, &req->flags);
1305 /* matches barrier in request_wait_answer() */
1306 smp_mb__after_atomic();
1307 if (test_bit(FR_INTERRUPTED, &req->flags))
1308 queue_interrupt(fiq, req);
Miklos Szeredi82cbdcd2015-07-01 16:26:05 +02001309
Miklos Szeredi334f4852005-09-09 13:10:27 -07001310 return reqsize;
1311
Miklos Szeredi82cbdcd2015-07-01 16:26:05 +02001312out_end:
Miklos Szeredi77cd9d42015-07-01 16:26:06 +02001313 if (!test_bit(FR_PRIVATE, &req->flags))
1314 list_del_init(&req->list);
Miklos Szeredi45a91cb2015-07-01 16:26:06 +02001315 spin_unlock(&fpq->lock);
Miklos Szeredi82cbdcd2015-07-01 16:26:05 +02001316 request_end(fc, req);
1317 return err;
1318
Miklos Szeredi334f4852005-09-09 13:10:27 -07001319 err_unlock:
Miklos Szeredi4ce60812015-07-01 16:26:02 +02001320 spin_unlock(&fiq->waitq.lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001321 return err;
1322}
1323
Tom Van Braeckel94e4fe22015-01-12 05:22:16 +01001324static int fuse_dev_open(struct inode *inode, struct file *file)
1325{
1326 /*
1327 * The fuse device's file's private_data is used to hold
1328 * the fuse_conn(ection) when it is mounted, and is used to
1329 * keep track of whether the file has been mounted already.
1330 */
1331 file->private_data = NULL;
1332 return 0;
1333}
1334
Al Virofbdbacc2015-04-03 21:53:39 -04001335static ssize_t fuse_dev_read(struct kiocb *iocb, struct iov_iter *to)
Miklos Szeredic3021622010-05-25 15:06:07 +02001336{
1337 struct fuse_copy_state cs;
1338 struct file *file = iocb->ki_filp;
Miklos Szeredicc080e92015-07-01 16:26:08 +02001339 struct fuse_dev *fud = fuse_get_dev(file);
1340
1341 if (!fud)
Miklos Szeredic3021622010-05-25 15:06:07 +02001342 return -EPERM;
1343
Al Virofbdbacc2015-04-03 21:53:39 -04001344 if (!iter_is_iovec(to))
1345 return -EINVAL;
Miklos Szeredic3021622010-05-25 15:06:07 +02001346
Miklos Szeredidc008092015-07-01 16:25:58 +02001347 fuse_copy_init(&cs, 1, to);
Al Virofbdbacc2015-04-03 21:53:39 -04001348
Miklos Szeredic36960462015-07-01 16:26:09 +02001349 return fuse_dev_do_read(fud, file, &cs, iov_iter_count(to));
Miklos Szeredic3021622010-05-25 15:06:07 +02001350}
1351
Miklos Szeredic3021622010-05-25 15:06:07 +02001352static ssize_t fuse_dev_splice_read(struct file *in, loff_t *ppos,
1353 struct pipe_inode_info *pipe,
1354 size_t len, unsigned int flags)
1355{
Al Virod82718e2016-09-17 22:56:25 -04001356 int total, ret;
Miklos Szeredic3021622010-05-25 15:06:07 +02001357 int page_nr = 0;
Miklos Szeredic3021622010-05-25 15:06:07 +02001358 struct pipe_buffer *bufs;
1359 struct fuse_copy_state cs;
Miklos Szeredicc080e92015-07-01 16:26:08 +02001360 struct fuse_dev *fud = fuse_get_dev(in);
1361
1362 if (!fud)
Miklos Szeredic3021622010-05-25 15:06:07 +02001363 return -EPERM;
1364
Kees Cook6da2ec52018-06-12 13:55:00 -07001365 bufs = kmalloc_array(pipe->buffers, sizeof(struct pipe_buffer),
1366 GFP_KERNEL);
Miklos Szeredic3021622010-05-25 15:06:07 +02001367 if (!bufs)
1368 return -ENOMEM;
1369
Miklos Szeredidc008092015-07-01 16:25:58 +02001370 fuse_copy_init(&cs, 1, NULL);
Miklos Szeredic3021622010-05-25 15:06:07 +02001371 cs.pipebufs = bufs;
1372 cs.pipe = pipe;
Miklos Szeredic36960462015-07-01 16:26:09 +02001373 ret = fuse_dev_do_read(fud, in, &cs, len);
Miklos Szeredic3021622010-05-25 15:06:07 +02001374 if (ret < 0)
1375 goto out;
1376
Miklos Szeredic3021622010-05-25 15:06:07 +02001377 if (pipe->nrbufs + cs.nr_segs > pipe->buffers) {
1378 ret = -EIO;
Al Virod82718e2016-09-17 22:56:25 -04001379 goto out;
Miklos Szeredic3021622010-05-25 15:06:07 +02001380 }
1381
Al Virod82718e2016-09-17 22:56:25 -04001382 for (ret = total = 0; page_nr < cs.nr_segs; total += ret) {
Miklos Szeredi28a625c2014-01-22 19:36:57 +01001383 /*
1384 * Need to be careful about this. Having buf->ops in module
1385 * code can Oops if the buffer persists after module unload.
1386 */
Al Virod82718e2016-09-17 22:56:25 -04001387 bufs[page_nr].ops = &nosteal_pipe_buf_ops;
Miklos Szeredi84588a92017-02-16 15:08:20 +01001388 bufs[page_nr].flags = 0;
Al Virod82718e2016-09-17 22:56:25 -04001389 ret = add_to_pipe(pipe, &bufs[page_nr++]);
1390 if (unlikely(ret < 0))
1391 break;
Miklos Szeredic3021622010-05-25 15:06:07 +02001392 }
Al Virod82718e2016-09-17 22:56:25 -04001393 if (total)
1394 ret = total;
Miklos Szeredic3021622010-05-25 15:06:07 +02001395out:
1396 for (; page_nr < cs.nr_segs; page_nr++)
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001397 put_page(bufs[page_nr].page);
Miklos Szeredic3021622010-05-25 15:06:07 +02001398
1399 kfree(bufs);
1400 return ret;
1401}
1402
Tejun Heo95668a62008-11-26 12:03:55 +01001403static int fuse_notify_poll(struct fuse_conn *fc, unsigned int size,
1404 struct fuse_copy_state *cs)
1405{
1406 struct fuse_notify_poll_wakeup_out outarg;
Miklos Szeredif6d47a12009-01-26 15:00:59 +01001407 int err = -EINVAL;
Tejun Heo95668a62008-11-26 12:03:55 +01001408
1409 if (size != sizeof(outarg))
Miklos Szeredif6d47a12009-01-26 15:00:59 +01001410 goto err;
Tejun Heo95668a62008-11-26 12:03:55 +01001411
1412 err = fuse_copy_one(cs, &outarg, sizeof(outarg));
1413 if (err)
Miklos Szeredif6d47a12009-01-26 15:00:59 +01001414 goto err;
Tejun Heo95668a62008-11-26 12:03:55 +01001415
Miklos Szeredif6d47a12009-01-26 15:00:59 +01001416 fuse_copy_finish(cs);
Tejun Heo95668a62008-11-26 12:03:55 +01001417 return fuse_notify_poll_wakeup(fc, &outarg);
Miklos Szeredif6d47a12009-01-26 15:00:59 +01001418
1419err:
1420 fuse_copy_finish(cs);
1421 return err;
Tejun Heo95668a62008-11-26 12:03:55 +01001422}
1423
John Muir3b463ae2009-05-31 11:13:57 -04001424static int fuse_notify_inval_inode(struct fuse_conn *fc, unsigned int size,
1425 struct fuse_copy_state *cs)
1426{
1427 struct fuse_notify_inval_inode_out outarg;
1428 int err = -EINVAL;
1429
1430 if (size != sizeof(outarg))
1431 goto err;
1432
1433 err = fuse_copy_one(cs, &outarg, sizeof(outarg));
1434 if (err)
1435 goto err;
1436 fuse_copy_finish(cs);
1437
1438 down_read(&fc->killsb);
1439 err = -ENOENT;
Miklos Szeredib21dda42010-02-05 12:08:31 +01001440 if (fc->sb) {
1441 err = fuse_reverse_inval_inode(fc->sb, outarg.ino,
1442 outarg.off, outarg.len);
1443 }
John Muir3b463ae2009-05-31 11:13:57 -04001444 up_read(&fc->killsb);
1445 return err;
1446
1447err:
1448 fuse_copy_finish(cs);
1449 return err;
1450}
1451
1452static int fuse_notify_inval_entry(struct fuse_conn *fc, unsigned int size,
1453 struct fuse_copy_state *cs)
1454{
1455 struct fuse_notify_inval_entry_out outarg;
Fang Wenqib2d82ee2009-12-30 18:37:13 +08001456 int err = -ENOMEM;
1457 char *buf;
John Muir3b463ae2009-05-31 11:13:57 -04001458 struct qstr name;
1459
Fang Wenqib2d82ee2009-12-30 18:37:13 +08001460 buf = kzalloc(FUSE_NAME_MAX + 1, GFP_KERNEL);
1461 if (!buf)
1462 goto err;
1463
1464 err = -EINVAL;
John Muir3b463ae2009-05-31 11:13:57 -04001465 if (size < sizeof(outarg))
1466 goto err;
1467
1468 err = fuse_copy_one(cs, &outarg, sizeof(outarg));
1469 if (err)
1470 goto err;
1471
1472 err = -ENAMETOOLONG;
1473 if (outarg.namelen > FUSE_NAME_MAX)
1474 goto err;
1475
Miklos Szeredic2183d12011-08-24 10:20:17 +02001476 err = -EINVAL;
1477 if (size != sizeof(outarg) + outarg.namelen + 1)
1478 goto err;
1479
John Muir3b463ae2009-05-31 11:13:57 -04001480 name.name = buf;
1481 name.len = outarg.namelen;
1482 err = fuse_copy_one(cs, buf, outarg.namelen + 1);
1483 if (err)
1484 goto err;
1485 fuse_copy_finish(cs);
1486 buf[outarg.namelen] = 0;
John Muir3b463ae2009-05-31 11:13:57 -04001487
1488 down_read(&fc->killsb);
1489 err = -ENOENT;
Miklos Szeredib21dda42010-02-05 12:08:31 +01001490 if (fc->sb)
John Muir451d0f52011-12-06 21:50:06 +01001491 err = fuse_reverse_inval_entry(fc->sb, outarg.parent, 0, &name);
1492 up_read(&fc->killsb);
1493 kfree(buf);
1494 return err;
1495
1496err:
1497 kfree(buf);
1498 fuse_copy_finish(cs);
1499 return err;
1500}
1501
1502static int fuse_notify_delete(struct fuse_conn *fc, unsigned int size,
1503 struct fuse_copy_state *cs)
1504{
1505 struct fuse_notify_delete_out outarg;
1506 int err = -ENOMEM;
1507 char *buf;
1508 struct qstr name;
1509
1510 buf = kzalloc(FUSE_NAME_MAX + 1, GFP_KERNEL);
1511 if (!buf)
1512 goto err;
1513
1514 err = -EINVAL;
1515 if (size < sizeof(outarg))
1516 goto err;
1517
1518 err = fuse_copy_one(cs, &outarg, sizeof(outarg));
1519 if (err)
1520 goto err;
1521
1522 err = -ENAMETOOLONG;
1523 if (outarg.namelen > FUSE_NAME_MAX)
1524 goto err;
1525
1526 err = -EINVAL;
1527 if (size != sizeof(outarg) + outarg.namelen + 1)
1528 goto err;
1529
1530 name.name = buf;
1531 name.len = outarg.namelen;
1532 err = fuse_copy_one(cs, buf, outarg.namelen + 1);
1533 if (err)
1534 goto err;
1535 fuse_copy_finish(cs);
1536 buf[outarg.namelen] = 0;
John Muir451d0f52011-12-06 21:50:06 +01001537
1538 down_read(&fc->killsb);
1539 err = -ENOENT;
1540 if (fc->sb)
1541 err = fuse_reverse_inval_entry(fc->sb, outarg.parent,
1542 outarg.child, &name);
John Muir3b463ae2009-05-31 11:13:57 -04001543 up_read(&fc->killsb);
Fang Wenqib2d82ee2009-12-30 18:37:13 +08001544 kfree(buf);
John Muir3b463ae2009-05-31 11:13:57 -04001545 return err;
1546
1547err:
Fang Wenqib2d82ee2009-12-30 18:37:13 +08001548 kfree(buf);
John Muir3b463ae2009-05-31 11:13:57 -04001549 fuse_copy_finish(cs);
1550 return err;
1551}
1552
Miklos Szeredia1d75f22010-07-12 14:41:40 +02001553static int fuse_notify_store(struct fuse_conn *fc, unsigned int size,
1554 struct fuse_copy_state *cs)
1555{
1556 struct fuse_notify_store_out outarg;
1557 struct inode *inode;
1558 struct address_space *mapping;
1559 u64 nodeid;
1560 int err;
1561 pgoff_t index;
1562 unsigned int offset;
1563 unsigned int num;
1564 loff_t file_size;
1565 loff_t end;
1566
1567 err = -EINVAL;
1568 if (size < sizeof(outarg))
1569 goto out_finish;
1570
1571 err = fuse_copy_one(cs, &outarg, sizeof(outarg));
1572 if (err)
1573 goto out_finish;
1574
1575 err = -EINVAL;
1576 if (size - sizeof(outarg) != outarg.size)
1577 goto out_finish;
1578
1579 nodeid = outarg.nodeid;
1580
1581 down_read(&fc->killsb);
1582
1583 err = -ENOENT;
1584 if (!fc->sb)
1585 goto out_up_killsb;
1586
1587 inode = ilookup5(fc->sb, nodeid, fuse_inode_eq, &nodeid);
1588 if (!inode)
1589 goto out_up_killsb;
1590
1591 mapping = inode->i_mapping;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001592 index = outarg.offset >> PAGE_SHIFT;
1593 offset = outarg.offset & ~PAGE_MASK;
Miklos Szeredia1d75f22010-07-12 14:41:40 +02001594 file_size = i_size_read(inode);
1595 end = outarg.offset + outarg.size;
1596 if (end > file_size) {
1597 file_size = end;
1598 fuse_write_update_size(inode, file_size);
1599 }
1600
1601 num = outarg.size;
1602 while (num) {
1603 struct page *page;
1604 unsigned int this_num;
1605
1606 err = -ENOMEM;
1607 page = find_or_create_page(mapping, index,
1608 mapping_gfp_mask(mapping));
1609 if (!page)
1610 goto out_iput;
1611
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001612 this_num = min_t(unsigned, num, PAGE_SIZE - offset);
Miklos Szeredia1d75f22010-07-12 14:41:40 +02001613 err = fuse_copy_page(cs, &page, offset, this_num, 0);
Miklos Szeredi063ec1e2014-01-22 19:36:58 +01001614 if (!err && offset == 0 &&
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001615 (this_num == PAGE_SIZE || file_size == end))
Miklos Szeredia1d75f22010-07-12 14:41:40 +02001616 SetPageUptodate(page);
1617 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001618 put_page(page);
Miklos Szeredia1d75f22010-07-12 14:41:40 +02001619
1620 if (err)
1621 goto out_iput;
1622
1623 num -= this_num;
1624 offset = 0;
1625 index++;
1626 }
1627
1628 err = 0;
1629
1630out_iput:
1631 iput(inode);
1632out_up_killsb:
1633 up_read(&fc->killsb);
1634out_finish:
1635 fuse_copy_finish(cs);
1636 return err;
1637}
1638
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001639static void fuse_retrieve_end(struct fuse_conn *fc, struct fuse_req *req)
1640{
Mel Gormanc6f92f92017-11-15 17:37:55 -08001641 release_pages(req->pages, req->num_pages);
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001642}
1643
1644static int fuse_retrieve(struct fuse_conn *fc, struct inode *inode,
1645 struct fuse_notify_retrieve_out *outarg)
1646{
1647 int err;
1648 struct address_space *mapping = inode->i_mapping;
1649 struct fuse_req *req;
1650 pgoff_t index;
1651 loff_t file_size;
1652 unsigned int num;
1653 unsigned int offset;
Geert Uytterhoeven01574432010-09-30 22:06:21 +02001654 size_t total_len = 0;
Maxim Patlasov4d53dc92012-10-26 19:48:42 +04001655 int num_pages;
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001656
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001657 offset = outarg->offset & ~PAGE_MASK;
Maxim Patlasov4d53dc92012-10-26 19:48:42 +04001658 file_size = i_size_read(inode);
1659
1660 num = outarg->size;
1661 if (outarg->offset > file_size)
1662 num = 0;
1663 else if (outarg->offset + num > file_size)
1664 num = file_size - outarg->offset;
1665
1666 num_pages = (num + offset + PAGE_SIZE - 1) >> PAGE_SHIFT;
1667 num_pages = min(num_pages, FUSE_MAX_PAGES_PER_REQ);
1668
1669 req = fuse_get_req(fc, num_pages);
1670 if (IS_ERR(req))
1671 return PTR_ERR(req);
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001672
1673 req->in.h.opcode = FUSE_NOTIFY_REPLY;
1674 req->in.h.nodeid = outarg->nodeid;
1675 req->in.numargs = 2;
1676 req->in.argpages = 1;
Maxim Patlasovb2430d72012-10-26 19:49:24 +04001677 req->page_descs[0].offset = offset;
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001678 req->end = fuse_retrieve_end;
1679
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001680 index = outarg->offset >> PAGE_SHIFT;
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001681
Maxim Patlasov4d53dc92012-10-26 19:48:42 +04001682 while (num && req->num_pages < num_pages) {
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001683 struct page *page;
1684 unsigned int this_num;
1685
1686 page = find_get_page(mapping, index);
1687 if (!page)
1688 break;
1689
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001690 this_num = min_t(unsigned, num, PAGE_SIZE - offset);
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001691 req->pages[req->num_pages] = page;
Maxim Patlasov85f40ae2012-10-26 19:49:33 +04001692 req->page_descs[req->num_pages].length = this_num;
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001693 req->num_pages++;
1694
Miklos Szeredic9e67d42012-09-04 18:45:54 +02001695 offset = 0;
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001696 num -= this_num;
1697 total_len += this_num;
Miklos Szeredi48706d02011-12-13 10:36:59 +01001698 index++;
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001699 }
1700 req->misc.retrieve_in.offset = outarg->offset;
1701 req->misc.retrieve_in.size = total_len;
1702 req->in.args[0].size = sizeof(req->misc.retrieve_in);
1703 req->in.args[0].value = &req->misc.retrieve_in;
1704 req->in.args[1].size = total_len;
1705
1706 err = fuse_request_send_notify_reply(fc, req, outarg->notify_unique);
1707 if (err)
1708 fuse_retrieve_end(fc, req);
1709
1710 return err;
1711}
1712
1713static int fuse_notify_retrieve(struct fuse_conn *fc, unsigned int size,
1714 struct fuse_copy_state *cs)
1715{
1716 struct fuse_notify_retrieve_out outarg;
1717 struct inode *inode;
1718 int err;
1719
1720 err = -EINVAL;
1721 if (size != sizeof(outarg))
1722 goto copy_finish;
1723
1724 err = fuse_copy_one(cs, &outarg, sizeof(outarg));
1725 if (err)
1726 goto copy_finish;
1727
1728 fuse_copy_finish(cs);
1729
1730 down_read(&fc->killsb);
1731 err = -ENOENT;
1732 if (fc->sb) {
1733 u64 nodeid = outarg.nodeid;
1734
1735 inode = ilookup5(fc->sb, nodeid, fuse_inode_eq, &nodeid);
1736 if (inode) {
1737 err = fuse_retrieve(fc, inode, &outarg);
1738 iput(inode);
1739 }
1740 }
1741 up_read(&fc->killsb);
1742
1743 return err;
1744
1745copy_finish:
1746 fuse_copy_finish(cs);
1747 return err;
1748}
1749
Tejun Heo85993962008-11-26 12:03:55 +01001750static int fuse_notify(struct fuse_conn *fc, enum fuse_notify_code code,
1751 unsigned int size, struct fuse_copy_state *cs)
1752{
Miklos Szeredi0d278362015-02-26 11:45:47 +01001753 /* Don't try to move pages (yet) */
1754 cs->move_pages = 0;
1755
Tejun Heo85993962008-11-26 12:03:55 +01001756 switch (code) {
Tejun Heo95668a62008-11-26 12:03:55 +01001757 case FUSE_NOTIFY_POLL:
1758 return fuse_notify_poll(fc, size, cs);
1759
John Muir3b463ae2009-05-31 11:13:57 -04001760 case FUSE_NOTIFY_INVAL_INODE:
1761 return fuse_notify_inval_inode(fc, size, cs);
1762
1763 case FUSE_NOTIFY_INVAL_ENTRY:
1764 return fuse_notify_inval_entry(fc, size, cs);
1765
Miklos Szeredia1d75f22010-07-12 14:41:40 +02001766 case FUSE_NOTIFY_STORE:
1767 return fuse_notify_store(fc, size, cs);
1768
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001769 case FUSE_NOTIFY_RETRIEVE:
1770 return fuse_notify_retrieve(fc, size, cs);
1771
John Muir451d0f52011-12-06 21:50:06 +01001772 case FUSE_NOTIFY_DELETE:
1773 return fuse_notify_delete(fc, size, cs);
1774
Tejun Heo85993962008-11-26 12:03:55 +01001775 default:
Miklos Szeredif6d47a12009-01-26 15:00:59 +01001776 fuse_copy_finish(cs);
Tejun Heo85993962008-11-26 12:03:55 +01001777 return -EINVAL;
1778 }
1779}
1780
Miklos Szeredi334f4852005-09-09 13:10:27 -07001781/* Look up request on processing list by unique ID */
Miklos Szeredi3a2b5b92015-07-01 16:26:04 +02001782static struct fuse_req *request_find(struct fuse_pqueue *fpq, u64 unique)
Miklos Szeredi334f4852005-09-09 13:10:27 -07001783{
Dong Fang05726ac2013-07-30 22:50:01 -04001784 struct fuse_req *req;
Miklos Szeredi334f4852005-09-09 13:10:27 -07001785
Miklos Szeredi3a2b5b92015-07-01 16:26:04 +02001786 list_for_each_entry(req, &fpq->processing, list) {
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001787 if (req->in.h.unique == unique || req->intr_unique == unique)
Miklos Szeredi334f4852005-09-09 13:10:27 -07001788 return req;
1789 }
1790 return NULL;
1791}
1792
1793static int copy_out_args(struct fuse_copy_state *cs, struct fuse_out *out,
1794 unsigned nbytes)
1795{
1796 unsigned reqsize = sizeof(struct fuse_out_header);
1797
1798 if (out->h.error)
1799 return nbytes != reqsize ? -EINVAL : 0;
1800
1801 reqsize += len_args(out->numargs, out->args);
1802
1803 if (reqsize < nbytes || (reqsize > nbytes && !out->argvar))
1804 return -EINVAL;
1805 else if (reqsize > nbytes) {
1806 struct fuse_arg *lastarg = &out->args[out->numargs-1];
1807 unsigned diffsize = reqsize - nbytes;
1808 if (diffsize > lastarg->size)
1809 return -EINVAL;
1810 lastarg->size -= diffsize;
1811 }
1812 return fuse_copy_args(cs, out->numargs, out->argpages, out->args,
1813 out->page_zeroing);
1814}
1815
1816/*
1817 * Write a single reply to a request. First the header is copied from
1818 * the write buffer. The request is then searched on the processing
1819 * list by the unique ID found in the header. If found, then remove
1820 * it from the list and copy the rest of the buffer to the request.
1821 * The request is finished by calling request_end()
1822 */
Miklos Szeredic36960462015-07-01 16:26:09 +02001823static ssize_t fuse_dev_do_write(struct fuse_dev *fud,
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001824 struct fuse_copy_state *cs, size_t nbytes)
Miklos Szeredi334f4852005-09-09 13:10:27 -07001825{
1826 int err;
Miklos Szeredic36960462015-07-01 16:26:09 +02001827 struct fuse_conn *fc = fud->fc;
1828 struct fuse_pqueue *fpq = &fud->pq;
Miklos Szeredi334f4852005-09-09 13:10:27 -07001829 struct fuse_req *req;
1830 struct fuse_out_header oh;
Miklos Szeredi334f4852005-09-09 13:10:27 -07001831
Miklos Szeredi334f4852005-09-09 13:10:27 -07001832 if (nbytes < sizeof(struct fuse_out_header))
1833 return -EINVAL;
1834
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001835 err = fuse_copy_one(cs, &oh, sizeof(oh));
Miklos Szeredi334f4852005-09-09 13:10:27 -07001836 if (err)
1837 goto err_finish;
Tejun Heo85993962008-11-26 12:03:55 +01001838
Miklos Szeredi334f4852005-09-09 13:10:27 -07001839 err = -EINVAL;
Tejun Heo85993962008-11-26 12:03:55 +01001840 if (oh.len != nbytes)
1841 goto err_finish;
1842
1843 /*
1844 * Zero oh.unique indicates unsolicited notification message
1845 * and error contains notification code.
1846 */
1847 if (!oh.unique) {
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001848 err = fuse_notify(fc, oh.error, nbytes - sizeof(oh), cs);
Tejun Heo85993962008-11-26 12:03:55 +01001849 return err ? err : nbytes;
1850 }
1851
1852 err = -EINVAL;
1853 if (oh.error <= -1000 || oh.error > 0)
Miklos Szeredi334f4852005-09-09 13:10:27 -07001854 goto err_finish;
1855
Miklos Szeredi45a91cb2015-07-01 16:26:06 +02001856 spin_lock(&fpq->lock);
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08001857 err = -ENOENT;
Miklos Szeredie96edd92015-07-01 16:26:04 +02001858 if (!fpq->connected)
Miklos Szeredi45a91cb2015-07-01 16:26:06 +02001859 goto err_unlock_pq;
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08001860
Miklos Szeredi3a2b5b92015-07-01 16:26:04 +02001861 req = request_find(fpq, oh.unique);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001862 if (!req)
Miklos Szeredi45a91cb2015-07-01 16:26:06 +02001863 goto err_unlock_pq;
Miklos Szeredi334f4852005-09-09 13:10:27 -07001864
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001865 /* Is it an interrupt reply? */
1866 if (req->intr_unique == oh.unique) {
Miklos Szeredi45a91cb2015-07-01 16:26:06 +02001867 spin_unlock(&fpq->lock);
1868
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001869 err = -EINVAL;
1870 if (nbytes != sizeof(struct fuse_out_header))
Miklos Szeredi46c34a32015-07-01 16:26:07 +02001871 goto err_finish;
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001872
1873 if (oh.error == -ENOSYS)
1874 fc->no_interrupt = 1;
1875 else if (oh.error == -EAGAIN)
Miklos Szeredif88996a2015-07-01 16:26:01 +02001876 queue_interrupt(&fc->iq, req);
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001877
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001878 fuse_copy_finish(cs);
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001879 return nbytes;
1880 }
1881
Miklos Szeredi33e14b42015-07-01 16:26:01 +02001882 clear_bit(FR_SENT, &req->flags);
Miklos Szeredi3a2b5b92015-07-01 16:26:04 +02001883 list_move(&req->list, &fpq->io);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001884 req->out.h = oh;
Miklos Szeredi825d6d32015-07-01 16:25:58 +02001885 set_bit(FR_LOCKED, &req->flags);
Miklos Szeredi45a91cb2015-07-01 16:26:06 +02001886 spin_unlock(&fpq->lock);
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001887 cs->req = req;
Miklos Szeredice534fb2010-05-25 15:06:07 +02001888 if (!req->out.page_replace)
1889 cs->move_pages = 0;
Miklos Szeredi334f4852005-09-09 13:10:27 -07001890
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001891 err = copy_out_args(cs, &req->out, nbytes);
1892 fuse_copy_finish(cs);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001893
Miklos Szeredi45a91cb2015-07-01 16:26:06 +02001894 spin_lock(&fpq->lock);
Miklos Szeredi825d6d32015-07-01 16:25:58 +02001895 clear_bit(FR_LOCKED, &req->flags);
Miklos Szeredie96edd92015-07-01 16:26:04 +02001896 if (!fpq->connected)
Miklos Szeredi0d8e84b2015-07-01 16:25:58 +02001897 err = -ENOENT;
1898 else if (err)
Miklos Szeredi334f4852005-09-09 13:10:27 -07001899 req->out.h.error = -EIO;
Miklos Szeredi77cd9d42015-07-01 16:26:06 +02001900 if (!test_bit(FR_PRIVATE, &req->flags))
1901 list_del_init(&req->list);
Miklos Szeredi45a91cb2015-07-01 16:26:06 +02001902 spin_unlock(&fpq->lock);
Miklos Szeredi46c34a32015-07-01 16:26:07 +02001903
Miklos Szeredi334f4852005-09-09 13:10:27 -07001904 request_end(fc, req);
1905
1906 return err ? err : nbytes;
1907
Miklos Szeredi45a91cb2015-07-01 16:26:06 +02001908 err_unlock_pq:
1909 spin_unlock(&fpq->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001910 err_finish:
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001911 fuse_copy_finish(cs);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001912 return err;
1913}
1914
Al Virofbdbacc2015-04-03 21:53:39 -04001915static ssize_t fuse_dev_write(struct kiocb *iocb, struct iov_iter *from)
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001916{
1917 struct fuse_copy_state cs;
Miklos Szeredicc080e92015-07-01 16:26:08 +02001918 struct fuse_dev *fud = fuse_get_dev(iocb->ki_filp);
1919
1920 if (!fud)
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001921 return -EPERM;
1922
Al Virofbdbacc2015-04-03 21:53:39 -04001923 if (!iter_is_iovec(from))
1924 return -EINVAL;
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001925
Miklos Szeredidc008092015-07-01 16:25:58 +02001926 fuse_copy_init(&cs, 0, from);
Al Virofbdbacc2015-04-03 21:53:39 -04001927
Miklos Szeredic36960462015-07-01 16:26:09 +02001928 return fuse_dev_do_write(fud, &cs, iov_iter_count(from));
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001929}
1930
1931static ssize_t fuse_dev_splice_write(struct pipe_inode_info *pipe,
1932 struct file *out, loff_t *ppos,
1933 size_t len, unsigned int flags)
1934{
1935 unsigned nbuf;
1936 unsigned idx;
1937 struct pipe_buffer *bufs;
1938 struct fuse_copy_state cs;
Miklos Szeredicc080e92015-07-01 16:26:08 +02001939 struct fuse_dev *fud;
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001940 size_t rem;
1941 ssize_t ret;
1942
Miklos Szeredicc080e92015-07-01 16:26:08 +02001943 fud = fuse_get_dev(out);
1944 if (!fud)
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001945 return -EPERM;
1946
Kees Cook6da2ec52018-06-12 13:55:00 -07001947 bufs = kmalloc_array(pipe->buffers, sizeof(struct pipe_buffer),
1948 GFP_KERNEL);
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001949 if (!bufs)
1950 return -ENOMEM;
1951
1952 pipe_lock(pipe);
1953 nbuf = 0;
1954 rem = 0;
1955 for (idx = 0; idx < pipe->nrbufs && rem < len; idx++)
1956 rem += pipe->bufs[(pipe->curbuf + idx) & (pipe->buffers - 1)].len;
1957
1958 ret = -EINVAL;
1959 if (rem < len) {
1960 pipe_unlock(pipe);
1961 goto out;
1962 }
1963
1964 rem = len;
1965 while (rem) {
1966 struct pipe_buffer *ibuf;
1967 struct pipe_buffer *obuf;
1968
1969 BUG_ON(nbuf >= pipe->buffers);
1970 BUG_ON(!pipe->nrbufs);
1971 ibuf = &pipe->bufs[pipe->curbuf];
1972 obuf = &bufs[nbuf];
1973
1974 if (rem >= ibuf->len) {
1975 *obuf = *ibuf;
1976 ibuf->ops = NULL;
1977 pipe->curbuf = (pipe->curbuf + 1) & (pipe->buffers - 1);
1978 pipe->nrbufs--;
1979 } else {
Miklos Szeredi7bf2d1d2016-09-27 10:45:12 +02001980 pipe_buf_get(pipe, ibuf);
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001981 *obuf = *ibuf;
1982 obuf->flags &= ~PIPE_BUF_FLAG_GIFT;
1983 obuf->len = rem;
1984 ibuf->offset += obuf->len;
1985 ibuf->len -= obuf->len;
1986 }
1987 nbuf++;
1988 rem -= obuf->len;
1989 }
1990 pipe_unlock(pipe);
1991
Miklos Szeredidc008092015-07-01 16:25:58 +02001992 fuse_copy_init(&cs, 0, NULL);
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001993 cs.pipebufs = bufs;
Al Viro6c09e942015-04-03 22:06:08 -04001994 cs.nr_segs = nbuf;
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001995 cs.pipe = pipe;
1996
Miklos Szeredice534fb2010-05-25 15:06:07 +02001997 if (flags & SPLICE_F_MOVE)
1998 cs.move_pages = 1;
1999
Miklos Szeredic36960462015-07-01 16:26:09 +02002000 ret = fuse_dev_do_write(fud, &cs, len);
Miklos Szeredidd3bb142010-05-25 15:06:06 +02002001
Miklos Szeredia7796382016-09-27 10:45:12 +02002002 for (idx = 0; idx < nbuf; idx++)
2003 pipe_buf_release(pipe, &bufs[idx]);
2004
Miklos Szeredidd3bb142010-05-25 15:06:06 +02002005out:
2006 kfree(bufs);
2007 return ret;
2008}
2009
Al Viro076ccb72017-07-03 01:02:18 -04002010static __poll_t fuse_dev_poll(struct file *file, poll_table *wait)
Miklos Szeredi334f4852005-09-09 13:10:27 -07002011{
Linus Torvaldsa9a08842018-02-11 14:34:03 -08002012 __poll_t mask = EPOLLOUT | EPOLLWRNORM;
Miklos Szeredif88996a2015-07-01 16:26:01 +02002013 struct fuse_iqueue *fiq;
Miklos Szeredicc080e92015-07-01 16:26:08 +02002014 struct fuse_dev *fud = fuse_get_dev(file);
2015
2016 if (!fud)
Linus Torvaldsa9a08842018-02-11 14:34:03 -08002017 return EPOLLERR;
Miklos Szeredi334f4852005-09-09 13:10:27 -07002018
Miklos Szeredicc080e92015-07-01 16:26:08 +02002019 fiq = &fud->fc->iq;
Miklos Szeredif88996a2015-07-01 16:26:01 +02002020 poll_wait(file, &fiq->waitq, wait);
Miklos Szeredi334f4852005-09-09 13:10:27 -07002021
Miklos Szeredi4ce60812015-07-01 16:26:02 +02002022 spin_lock(&fiq->waitq.lock);
Miklos Szeredie16714d2015-07-01 16:26:01 +02002023 if (!fiq->connected)
Linus Torvaldsa9a08842018-02-11 14:34:03 -08002024 mask = EPOLLERR;
Miklos Szeredif88996a2015-07-01 16:26:01 +02002025 else if (request_pending(fiq))
Linus Torvaldsa9a08842018-02-11 14:34:03 -08002026 mask |= EPOLLIN | EPOLLRDNORM;
Miklos Szeredi4ce60812015-07-01 16:26:02 +02002027 spin_unlock(&fiq->waitq.lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -07002028
2029 return mask;
2030}
2031
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002032/*
2033 * Abort all requests on the given list (pending or processing)
2034 *
Miklos Szeredid7133112006-04-10 22:54:55 -07002035 * This function releases and reacquires fc->lock
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002036 */
Miklos Szeredi334f4852005-09-09 13:10:27 -07002037static void end_requests(struct fuse_conn *fc, struct list_head *head)
2038{
2039 while (!list_empty(head)) {
2040 struct fuse_req *req;
2041 req = list_entry(head->next, struct fuse_req, list);
Miklos Szeredi334f4852005-09-09 13:10:27 -07002042 req->out.h.error = -ECONNABORTED;
Miklos Szeredi33e14b42015-07-01 16:26:01 +02002043 clear_bit(FR_SENT, &req->flags);
Miklos Szeredif377cb72015-07-01 16:26:04 +02002044 list_del_init(&req->list);
Miklos Szeredi334f4852005-09-09 13:10:27 -07002045 request_end(fc, req);
Miklos Szeredi334f4852005-09-09 13:10:27 -07002046 }
2047}
2048
Bryan Green357ccf22011-03-01 16:43:52 -08002049static void end_polls(struct fuse_conn *fc)
2050{
2051 struct rb_node *p;
2052
2053 p = rb_first(&fc->polled_files);
2054
2055 while (p) {
2056 struct fuse_file *ff;
2057 ff = rb_entry(p, struct fuse_file, polled_node);
2058 wake_up_interruptible_all(&ff->poll_wait);
2059
2060 p = rb_next(p);
2061 }
2062}
2063
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002064/*
2065 * Abort all requests.
2066 *
Miklos Szeredib716d422015-07-01 16:25:59 +02002067 * Emergency exit in case of a malicious or accidental deadlock, or just a hung
2068 * filesystem.
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002069 *
Miklos Szeredib716d422015-07-01 16:25:59 +02002070 * The same effect is usually achievable through killing the filesystem daemon
2071 * and all users of the filesystem. The exception is the combination of an
2072 * asynchronous request and the tricky deadlock (see
2073 * Documentation/filesystems/fuse.txt).
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002074 *
Miklos Szeredib716d422015-07-01 16:25:59 +02002075 * Aborting requests under I/O goes as follows: 1: Separate out unlocked
2076 * requests, they should be finished off immediately. Locked requests will be
2077 * finished after unlock; see unlock_request(). 2: Finish off the unlocked
2078 * requests. It is possible that some request will finish before we can. This
2079 * is OK, the request will in that case be removed from the list before we touch
2080 * it.
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002081 */
Szymon Lukasz3b7008b2017-11-09 21:23:35 +01002082void fuse_abort_conn(struct fuse_conn *fc, bool is_abort)
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002083{
Miklos Szeredif88996a2015-07-01 16:26:01 +02002084 struct fuse_iqueue *fiq = &fc->iq;
2085
Miklos Szeredid7133112006-04-10 22:54:55 -07002086 spin_lock(&fc->lock);
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002087 if (fc->connected) {
Miklos Szeredic36960462015-07-01 16:26:09 +02002088 struct fuse_dev *fud;
Miklos Szeredib716d422015-07-01 16:25:59 +02002089 struct fuse_req *req, *next;
Miklos Szeredi41f98272015-07-01 16:25:59 +02002090 LIST_HEAD(to_end1);
2091 LIST_HEAD(to_end2);
Miklos Szeredib716d422015-07-01 16:25:59 +02002092
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002093 fc->connected = 0;
Miklos Szeredi51eb01e2006-06-25 05:48:50 -07002094 fc->blocked = 0;
Szymon Lukasz3b7008b2017-11-09 21:23:35 +01002095 fc->aborted = is_abort;
Miklos Szeredi9759bd512015-01-06 10:45:35 +01002096 fuse_set_initialized(fc);
Miklos Szeredic36960462015-07-01 16:26:09 +02002097 list_for_each_entry(fud, &fc->devices, entry) {
2098 struct fuse_pqueue *fpq = &fud->pq;
2099
2100 spin_lock(&fpq->lock);
2101 fpq->connected = 0;
2102 list_for_each_entry_safe(req, next, &fpq->io, list) {
2103 req->out.h.error = -ECONNABORTED;
2104 spin_lock(&req->waitq.lock);
2105 set_bit(FR_ABORTED, &req->flags);
2106 if (!test_bit(FR_LOCKED, &req->flags)) {
2107 set_bit(FR_PRIVATE, &req->flags);
2108 list_move(&req->list, &to_end1);
2109 }
2110 spin_unlock(&req->waitq.lock);
Miklos Szeredi77cd9d42015-07-01 16:26:06 +02002111 }
Miklos Szeredic36960462015-07-01 16:26:09 +02002112 list_splice_init(&fpq->processing, &to_end2);
2113 spin_unlock(&fpq->lock);
Miklos Szeredib716d422015-07-01 16:25:59 +02002114 }
Miklos Szeredi41f98272015-07-01 16:25:59 +02002115 fc->max_background = UINT_MAX;
2116 flush_bg_queue(fc);
Miklos Szeredi8c911892015-07-01 16:26:02 +02002117
Miklos Szeredi4ce60812015-07-01 16:26:02 +02002118 spin_lock(&fiq->waitq.lock);
Miklos Szeredi8c911892015-07-01 16:26:02 +02002119 fiq->connected = 0;
Miklos Szeredif88996a2015-07-01 16:26:01 +02002120 list_splice_init(&fiq->pending, &to_end2);
Tahsin Erdogana8a86d72017-01-12 12:04:04 -08002121 list_for_each_entry(req, &to_end2, list)
2122 clear_bit(FR_PENDING, &req->flags);
Miklos Szeredi8c911892015-07-01 16:26:02 +02002123 while (forget_pending(fiq))
2124 kfree(dequeue_forget(fiq, 1, NULL));
Miklos Szeredi4ce60812015-07-01 16:26:02 +02002125 wake_up_all_locked(&fiq->waitq);
2126 spin_unlock(&fiq->waitq.lock);
Miklos Szeredi8c911892015-07-01 16:26:02 +02002127 kill_fasync(&fiq->fasync, SIGIO, POLL_IN);
Miklos Szerediee314a82015-07-01 16:26:08 +02002128 end_polls(fc);
2129 wake_up_all(&fc->blocked_waitq);
2130 spin_unlock(&fc->lock);
Miklos Szeredi8c911892015-07-01 16:26:02 +02002131
Miklos Szeredi41f98272015-07-01 16:25:59 +02002132 while (!list_empty(&to_end1)) {
2133 req = list_first_entry(&to_end1, struct fuse_req, list);
Miklos Szeredib716d422015-07-01 16:25:59 +02002134 __fuse_get_request(req);
Miklos Szeredif377cb72015-07-01 16:26:04 +02002135 list_del_init(&req->list);
Miklos Szeredib716d422015-07-01 16:25:59 +02002136 request_end(fc, req);
Miklos Szeredib716d422015-07-01 16:25:59 +02002137 }
Miklos Szeredi41f98272015-07-01 16:25:59 +02002138 end_requests(fc, &to_end2);
Miklos Szerediee314a82015-07-01 16:26:08 +02002139 } else {
2140 spin_unlock(&fc->lock);
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002141 }
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002142}
Tejun Heo08cbf542009-04-14 10:54:53 +09002143EXPORT_SYMBOL_GPL(fuse_abort_conn);
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002144
Tejun Heo08cbf542009-04-14 10:54:53 +09002145int fuse_dev_release(struct inode *inode, struct file *file)
Miklos Szeredi334f4852005-09-09 13:10:27 -07002146{
Miklos Szeredicc080e92015-07-01 16:26:08 +02002147 struct fuse_dev *fud = fuse_get_dev(file);
2148
2149 if (fud) {
2150 struct fuse_conn *fc = fud->fc;
Miklos Szeredic36960462015-07-01 16:26:09 +02002151 struct fuse_pqueue *fpq = &fud->pq;
Miklos Szeredicc080e92015-07-01 16:26:08 +02002152
Miklos Szeredic36960462015-07-01 16:26:09 +02002153 WARN_ON(!list_empty(&fpq->io));
2154 end_requests(fc, &fpq->processing);
2155 /* Are we the last open device? */
2156 if (atomic_dec_and_test(&fc->dev_count)) {
2157 WARN_ON(fc->iq.fasync != NULL);
Szymon Lukasz3b7008b2017-11-09 21:23:35 +01002158 fuse_abort_conn(fc, false);
Miklos Szeredic36960462015-07-01 16:26:09 +02002159 }
Miklos Szeredicc080e92015-07-01 16:26:08 +02002160 fuse_dev_free(fud);
Jeff Dike385a17b2006-04-10 22:54:52 -07002161 }
Miklos Szeredi334f4852005-09-09 13:10:27 -07002162 return 0;
2163}
Tejun Heo08cbf542009-04-14 10:54:53 +09002164EXPORT_SYMBOL_GPL(fuse_dev_release);
Miklos Szeredi334f4852005-09-09 13:10:27 -07002165
Jeff Dike385a17b2006-04-10 22:54:52 -07002166static int fuse_dev_fasync(int fd, struct file *file, int on)
2167{
Miklos Szeredicc080e92015-07-01 16:26:08 +02002168 struct fuse_dev *fud = fuse_get_dev(file);
2169
2170 if (!fud)
Miklos Szeredia87046d2006-04-10 22:54:56 -07002171 return -EPERM;
Jeff Dike385a17b2006-04-10 22:54:52 -07002172
2173 /* No locking - fasync_helper does its own locking */
Miklos Szeredicc080e92015-07-01 16:26:08 +02002174 return fasync_helper(fd, file, on, &fud->fc->iq.fasync);
Jeff Dike385a17b2006-04-10 22:54:52 -07002175}
2176
Miklos Szeredi00c570f2015-07-01 16:26:08 +02002177static int fuse_device_clone(struct fuse_conn *fc, struct file *new)
2178{
Miklos Szeredicc080e92015-07-01 16:26:08 +02002179 struct fuse_dev *fud;
2180
Miklos Szeredi00c570f2015-07-01 16:26:08 +02002181 if (new->private_data)
2182 return -EINVAL;
2183
Miklos Szeredicc080e92015-07-01 16:26:08 +02002184 fud = fuse_dev_alloc(fc);
2185 if (!fud)
2186 return -ENOMEM;
2187
2188 new->private_data = fud;
Miklos Szeredic36960462015-07-01 16:26:09 +02002189 atomic_inc(&fc->dev_count);
Miklos Szeredi00c570f2015-07-01 16:26:08 +02002190
2191 return 0;
2192}
2193
2194static long fuse_dev_ioctl(struct file *file, unsigned int cmd,
2195 unsigned long arg)
2196{
2197 int err = -ENOTTY;
2198
2199 if (cmd == FUSE_DEV_IOC_CLONE) {
2200 int oldfd;
2201
2202 err = -EFAULT;
2203 if (!get_user(oldfd, (__u32 __user *) arg)) {
2204 struct file *old = fget(oldfd);
2205
2206 err = -EINVAL;
2207 if (old) {
Jann Horn8ed1f0e2015-08-16 20:27:01 +02002208 struct fuse_dev *fud = NULL;
2209
2210 /*
2211 * Check against file->f_op because CUSE
2212 * uses the same ioctl handler.
2213 */
2214 if (old->f_op == file->f_op &&
2215 old->f_cred->user_ns == file->f_cred->user_ns)
2216 fud = fuse_get_dev(old);
Miklos Szeredi00c570f2015-07-01 16:26:08 +02002217
Miklos Szeredicc080e92015-07-01 16:26:08 +02002218 if (fud) {
Miklos Szeredi00c570f2015-07-01 16:26:08 +02002219 mutex_lock(&fuse_mutex);
Miklos Szeredicc080e92015-07-01 16:26:08 +02002220 err = fuse_device_clone(fud->fc, file);
Miklos Szeredi00c570f2015-07-01 16:26:08 +02002221 mutex_unlock(&fuse_mutex);
2222 }
2223 fput(old);
2224 }
2225 }
2226 }
2227 return err;
2228}
2229
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -08002230const struct file_operations fuse_dev_operations = {
Miklos Szeredi334f4852005-09-09 13:10:27 -07002231 .owner = THIS_MODULE,
Tom Van Braeckel94e4fe22015-01-12 05:22:16 +01002232 .open = fuse_dev_open,
Miklos Szeredi334f4852005-09-09 13:10:27 -07002233 .llseek = no_llseek,
Al Virofbdbacc2015-04-03 21:53:39 -04002234 .read_iter = fuse_dev_read,
Miklos Szeredic3021622010-05-25 15:06:07 +02002235 .splice_read = fuse_dev_splice_read,
Al Virofbdbacc2015-04-03 21:53:39 -04002236 .write_iter = fuse_dev_write,
Miklos Szeredidd3bb142010-05-25 15:06:06 +02002237 .splice_write = fuse_dev_splice_write,
Miklos Szeredi334f4852005-09-09 13:10:27 -07002238 .poll = fuse_dev_poll,
2239 .release = fuse_dev_release,
Jeff Dike385a17b2006-04-10 22:54:52 -07002240 .fasync = fuse_dev_fasync,
Miklos Szeredi00c570f2015-07-01 16:26:08 +02002241 .unlocked_ioctl = fuse_dev_ioctl,
2242 .compat_ioctl = fuse_dev_ioctl,
Miklos Szeredi334f4852005-09-09 13:10:27 -07002243};
Tejun Heo08cbf542009-04-14 10:54:53 +09002244EXPORT_SYMBOL_GPL(fuse_dev_operations);
Miklos Szeredi334f4852005-09-09 13:10:27 -07002245
2246static struct miscdevice fuse_miscdevice = {
2247 .minor = FUSE_MINOR,
2248 .name = "fuse",
2249 .fops = &fuse_dev_operations,
2250};
2251
2252int __init fuse_dev_init(void)
2253{
2254 int err = -ENOMEM;
2255 fuse_req_cachep = kmem_cache_create("fuse_request",
2256 sizeof(struct fuse_req),
Paul Mundt20c2df82007-07-20 10:11:58 +09002257 0, 0, NULL);
Miklos Szeredi334f4852005-09-09 13:10:27 -07002258 if (!fuse_req_cachep)
2259 goto out;
2260
2261 err = misc_register(&fuse_miscdevice);
2262 if (err)
2263 goto out_cache_clean;
2264
2265 return 0;
2266
2267 out_cache_clean:
2268 kmem_cache_destroy(fuse_req_cachep);
2269 out:
2270 return err;
2271}
2272
2273void fuse_dev_cleanup(void)
2274{
2275 misc_deregister(&fuse_miscdevice);
2276 kmem_cache_destroy(fuse_req_cachep);
2277}