blob: 6a7d3b4424e1fb2ae3faff413978d9a66b128a79 [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
Miklos Szeredib8f95e52018-07-26 16:13:11 +0200130static void fuse_drop_waiting(struct fuse_conn *fc)
131{
132 if (fc->connected) {
133 atomic_dec(&fc->num_waiting);
134 } else if (atomic_dec_and_test(&fc->num_waiting)) {
135 /* wake up aborters */
136 wake_up_all(&fc->blocked_waitq);
137 }
138}
139
Maxim Patlasov8b41e672013-03-21 18:02:04 +0400140static struct fuse_req *__fuse_get_req(struct fuse_conn *fc, unsigned npages,
141 bool for_background)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700142{
Miklos Szeredi08a53cd2006-04-10 22:54:59 -0700143 struct fuse_req *req;
Miklos Szeredi08a53cd2006-04-10 22:54:59 -0700144 int err;
Miklos Szeredi9bc5ddd2006-04-11 21:16:09 +0200145 atomic_inc(&fc->num_waiting);
Maxim Patlasov0aada882013-03-21 18:02:28 +0400146
147 if (fuse_block_alloc(fc, for_background)) {
Maxim Patlasov0aada882013-03-21 18:02:28 +0400148 err = -EINTR;
Al Viro7d3a07f2016-07-19 03:08:27 -0400149 if (wait_event_killable_exclusive(fc->blocked_waitq,
150 !fuse_block_alloc(fc, for_background)))
Maxim Patlasov0aada882013-03-21 18:02:28 +0400151 goto out;
152 }
Miklos Szeredi9759bd512015-01-06 10:45:35 +0100153 /* Matches smp_wmb() in fuse_set_initialized() */
154 smp_rmb();
Miklos Szeredi08a53cd2006-04-10 22:54:59 -0700155
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700156 err = -ENOTCONN;
157 if (!fc->connected)
158 goto out;
159
Miklos Szeredide155222015-07-01 16:25:57 +0200160 err = -ECONNREFUSED;
161 if (fc->conn_error)
162 goto out;
163
Maxim Patlasovb111c8c2012-10-26 19:48:30 +0400164 req = fuse_request_alloc(npages);
Miklos Szeredi9bc5ddd2006-04-11 21:16:09 +0200165 err = -ENOMEM;
Maxim Patlasov722d2be2013-03-21 18:02:36 +0400166 if (!req) {
167 if (for_background)
168 wake_up(&fc->blocked_waitq);
Miklos Szeredi9bc5ddd2006-04-11 21:16:09 +0200169 goto out;
Maxim Patlasov722d2be2013-03-21 18:02:36 +0400170 }
Miklos Szeredi334f4852005-09-09 13:10:27 -0700171
Eric W. Biederman8cb08322018-02-21 11:18:07 -0600172 req->in.h.uid = from_kuid(fc->user_ns, current_fsuid());
173 req->in.h.gid = from_kgid(fc->user_ns, current_fsgid());
Eric W. Biedermanc9582eb2018-02-21 10:52:06 -0600174 req->in.h.pid = pid_nr_ns(task_pid(current), fc->pid_ns);
175
Miklos Szeredi825d6d32015-07-01 16:25:58 +0200176 __set_bit(FR_WAITING, &req->flags);
177 if (for_background)
178 __set_bit(FR_BACKGROUND, &req->flags);
179
Eric W. Biedermanc9582eb2018-02-21 10:52:06 -0600180 if (unlikely(req->in.h.uid == ((uid_t)-1) ||
181 req->in.h.gid == ((gid_t)-1))) {
182 fuse_put_request(fc, req);
183 return ERR_PTR(-EOVERFLOW);
184 }
Miklos Szeredi334f4852005-09-09 13:10:27 -0700185 return req;
Miklos Szeredi9bc5ddd2006-04-11 21:16:09 +0200186
187 out:
Miklos Szeredib8f95e52018-07-26 16:13:11 +0200188 fuse_drop_waiting(fc);
Miklos Szeredi9bc5ddd2006-04-11 21:16:09 +0200189 return ERR_PTR(err);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700190}
Maxim Patlasov8b41e672013-03-21 18:02:04 +0400191
192struct fuse_req *fuse_get_req(struct fuse_conn *fc, unsigned npages)
193{
194 return __fuse_get_req(fc, npages, false);
195}
Tejun Heo08cbf542009-04-14 10:54:53 +0900196EXPORT_SYMBOL_GPL(fuse_get_req);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700197
Maxim Patlasov8b41e672013-03-21 18:02:04 +0400198struct fuse_req *fuse_get_req_for_background(struct fuse_conn *fc,
199 unsigned npages)
200{
201 return __fuse_get_req(fc, npages, true);
202}
203EXPORT_SYMBOL_GPL(fuse_get_req_for_background);
204
Miklos Szeredi33649c92006-06-25 05:48:52 -0700205/*
206 * Return request in fuse_file->reserved_req. However that may
207 * currently be in use. If that is the case, wait for it to become
208 * available.
209 */
210static struct fuse_req *get_reserved_req(struct fuse_conn *fc,
211 struct file *file)
212{
213 struct fuse_req *req = NULL;
214 struct fuse_file *ff = file->private_data;
215
216 do {
Miklos Szeredide5e3de2007-10-16 23:31:00 -0700217 wait_event(fc->reserved_req_waitq, ff->reserved_req);
Miklos Szeredi33649c92006-06-25 05:48:52 -0700218 spin_lock(&fc->lock);
219 if (ff->reserved_req) {
220 req = ff->reserved_req;
221 ff->reserved_req = NULL;
Al Virocb0942b2012-08-27 14:48:26 -0400222 req->stolen_file = get_file(file);
Miklos Szeredi33649c92006-06-25 05:48:52 -0700223 }
224 spin_unlock(&fc->lock);
225 } while (!req);
226
227 return req;
228}
229
230/*
231 * Put stolen request back into fuse_file->reserved_req
232 */
233static void put_reserved_req(struct fuse_conn *fc, struct fuse_req *req)
234{
235 struct file *file = req->stolen_file;
236 struct fuse_file *ff = file->private_data;
237
238 spin_lock(&fc->lock);
Maxim Patlasovb2430d72012-10-26 19:49:24 +0400239 fuse_request_init(req, req->pages, req->page_descs, req->max_pages);
Miklos Szeredi33649c92006-06-25 05:48:52 -0700240 BUG_ON(ff->reserved_req);
241 ff->reserved_req = req;
Miklos Szeredide5e3de2007-10-16 23:31:00 -0700242 wake_up_all(&fc->reserved_req_waitq);
Miklos Szeredi33649c92006-06-25 05:48:52 -0700243 spin_unlock(&fc->lock);
244 fput(file);
245}
246
247/*
248 * Gets a requests for a file operation, always succeeds
249 *
250 * This is used for sending the FLUSH request, which must get to
251 * userspace, due to POSIX locks which may need to be unlocked.
252 *
253 * If allocation fails due to OOM, use the reserved request in
254 * fuse_file.
255 *
256 * This is very unlikely to deadlock accidentally, since the
257 * filesystem should not have it's own file open. If deadlock is
258 * intentional, it can still be broken by "aborting" the filesystem.
259 */
Maxim Patlasovb111c8c2012-10-26 19:48:30 +0400260struct fuse_req *fuse_get_req_nofail_nopages(struct fuse_conn *fc,
261 struct file *file)
Miklos Szeredi33649c92006-06-25 05:48:52 -0700262{
263 struct fuse_req *req;
264
265 atomic_inc(&fc->num_waiting);
Maxim Patlasov0aada882013-03-21 18:02:28 +0400266 wait_event(fc->blocked_waitq, fc->initialized);
Miklos Szeredi9759bd512015-01-06 10:45:35 +0100267 /* Matches smp_wmb() in fuse_set_initialized() */
268 smp_rmb();
Maxim Patlasovb111c8c2012-10-26 19:48:30 +0400269 req = fuse_request_alloc(0);
Miklos Szeredi33649c92006-06-25 05:48:52 -0700270 if (!req)
271 req = get_reserved_req(fc, file);
272
Eric W. Biederman8cb08322018-02-21 11:18:07 -0600273 req->in.h.uid = from_kuid_munged(fc->user_ns, current_fsuid());
274 req->in.h.gid = from_kgid_munged(fc->user_ns, current_fsgid());
Eric W. Biedermanc9582eb2018-02-21 10:52:06 -0600275 req->in.h.pid = pid_nr_ns(task_pid(current), fc->pid_ns);
276
Miklos Szeredi825d6d32015-07-01 16:25:58 +0200277 __set_bit(FR_WAITING, &req->flags);
278 __clear_bit(FR_BACKGROUND, &req->flags);
Miklos Szeredi33649c92006-06-25 05:48:52 -0700279 return req;
280}
281
Miklos Szeredi334f4852005-09-09 13:10:27 -0700282void fuse_put_request(struct fuse_conn *fc, struct fuse_req *req)
283{
Elena Reshetovaec99f6d2017-03-03 11:04:04 +0200284 if (refcount_dec_and_test(&req->count)) {
Miklos Szeredi825d6d32015-07-01 16:25:58 +0200285 if (test_bit(FR_BACKGROUND, &req->flags)) {
Maxim Patlasov722d2be2013-03-21 18:02:36 +0400286 /*
287 * We get here in the unlikely case that a background
288 * request was allocated but not sent
289 */
290 spin_lock(&fc->lock);
291 if (!fc->blocked)
292 wake_up(&fc->blocked_waitq);
293 spin_unlock(&fc->lock);
294 }
295
Miklos Szeredi825d6d32015-07-01 16:25:58 +0200296 if (test_bit(FR_WAITING, &req->flags)) {
297 __clear_bit(FR_WAITING, &req->flags);
Miklos Szeredib8f95e52018-07-26 16:13:11 +0200298 fuse_drop_waiting(fc);
Miklos Szeredi73e0e732015-07-01 16:25:56 +0200299 }
Miklos Szeredi33649c92006-06-25 05:48:52 -0700300
301 if (req->stolen_file)
302 put_reserved_req(fc, req);
303 else
304 fuse_request_free(req);
Miklos Szeredi7128ec22006-02-04 23:27:40 -0800305 }
306}
Tejun Heo08cbf542009-04-14 10:54:53 +0900307EXPORT_SYMBOL_GPL(fuse_put_request);
Miklos Szeredi7128ec22006-02-04 23:27:40 -0800308
Miklos Szeredid12def12008-02-06 01:38:39 -0800309static unsigned len_args(unsigned numargs, struct fuse_arg *args)
310{
311 unsigned nbytes = 0;
312 unsigned i;
313
314 for (i = 0; i < numargs; i++)
315 nbytes += args[i].size;
316
317 return nbytes;
318}
319
Miklos Szeredif88996a2015-07-01 16:26:01 +0200320static u64 fuse_get_unique(struct fuse_iqueue *fiq)
Miklos Szeredid12def12008-02-06 01:38:39 -0800321{
Miklos Szeredif88996a2015-07-01 16:26:01 +0200322 return ++fiq->reqctr;
Miklos Szeredid12def12008-02-06 01:38:39 -0800323}
324
Miklos Szeredif88996a2015-07-01 16:26:01 +0200325static void queue_request(struct fuse_iqueue *fiq, struct fuse_req *req)
Miklos Szeredid12def12008-02-06 01:38:39 -0800326{
Miklos Szeredid12def12008-02-06 01:38:39 -0800327 req->in.h.len = sizeof(struct fuse_in_header) +
328 len_args(req->in.numargs, (struct fuse_arg *) req->in.args);
Miklos Szeredif88996a2015-07-01 16:26:01 +0200329 list_add_tail(&req->list, &fiq->pending);
Miklos Szeredi4ce60812015-07-01 16:26:02 +0200330 wake_up_locked(&fiq->waitq);
Miklos Szeredif88996a2015-07-01 16:26:01 +0200331 kill_fasync(&fiq->fasync, SIGIO, POLL_IN);
Miklos Szeredid12def12008-02-06 01:38:39 -0800332}
333
Miklos Szeredi07e77dc2010-12-07 20:16:56 +0100334void fuse_queue_forget(struct fuse_conn *fc, struct fuse_forget_link *forget,
335 u64 nodeid, u64 nlookup)
336{
Miklos Szeredif88996a2015-07-01 16:26:01 +0200337 struct fuse_iqueue *fiq = &fc->iq;
338
Miklos Szeredi02c048b2010-12-07 20:16:56 +0100339 forget->forget_one.nodeid = nodeid;
340 forget->forget_one.nlookup = nlookup;
Miklos Szeredi07e77dc2010-12-07 20:16:56 +0100341
Miklos Szeredi4ce60812015-07-01 16:26:02 +0200342 spin_lock(&fiq->waitq.lock);
Miklos Szeredie16714d2015-07-01 16:26:01 +0200343 if (fiq->connected) {
Miklos Szeredif88996a2015-07-01 16:26:01 +0200344 fiq->forget_list_tail->next = forget;
345 fiq->forget_list_tail = forget;
Miklos Szeredi4ce60812015-07-01 16:26:02 +0200346 wake_up_locked(&fiq->waitq);
Miklos Szeredif88996a2015-07-01 16:26:01 +0200347 kill_fasync(&fiq->fasync, SIGIO, POLL_IN);
Miklos Szeredi5dfcc872011-09-12 09:38:03 +0200348 } else {
349 kfree(forget);
350 }
Miklos Szeredi4ce60812015-07-01 16:26:02 +0200351 spin_unlock(&fiq->waitq.lock);
Miklos Szeredi07e77dc2010-12-07 20:16:56 +0100352}
353
Miklos Szeredid12def12008-02-06 01:38:39 -0800354static void flush_bg_queue(struct fuse_conn *fc)
355{
Kirill Tkhaie2871792018-07-31 13:25:25 +0300356 struct fuse_iqueue *fiq = &fc->iq;
357
Csaba Henk7a6d3c82009-07-01 17:28:41 -0700358 while (fc->active_background < fc->max_background &&
Miklos Szeredid12def12008-02-06 01:38:39 -0800359 !list_empty(&fc->bg_queue)) {
360 struct fuse_req *req;
361
Kirill Tkhaie2871792018-07-31 13:25:25 +0300362 req = list_first_entry(&fc->bg_queue, struct fuse_req, list);
Miklos Szeredid12def12008-02-06 01:38:39 -0800363 list_del(&req->list);
364 fc->active_background++;
Miklos Szeredi4ce60812015-07-01 16:26:02 +0200365 spin_lock(&fiq->waitq.lock);
Miklos Szeredif88996a2015-07-01 16:26:01 +0200366 req->in.h.unique = fuse_get_unique(fiq);
367 queue_request(fiq, req);
Miklos Szeredi4ce60812015-07-01 16:26:02 +0200368 spin_unlock(&fiq->waitq.lock);
Miklos Szeredid12def12008-02-06 01:38:39 -0800369 }
370}
371
Miklos Szeredi6dbbcb12006-04-26 10:49:06 +0200372/*
Miklos Szeredi334f4852005-09-09 13:10:27 -0700373 * This function is called when a request is finished. Either a reply
Miklos Szeredif9a28422006-06-25 05:48:53 -0700374 * has arrived or it was aborted (and not yet sent) or some error
Miklos Szeredif43b1552006-01-16 22:14:26 -0800375 * occurred during communication with userspace, or the device file
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700376 * was closed. The requester thread is woken up (if still waiting),
377 * the 'end' callback is called if given, else the reference to the
378 * request is released
Miklos Szeredi334f4852005-09-09 13:10:27 -0700379 */
380static void request_end(struct fuse_conn *fc, struct fuse_req *req)
381{
Miklos Szeredi4ce60812015-07-01 16:26:02 +0200382 struct fuse_iqueue *fiq = &fc->iq;
Miklos Szeredi365ae712015-07-01 16:26:06 +0200383
Miklos Szerediefe28002015-07-01 16:26:07 +0200384 if (test_and_set_bit(FR_FINISHED, &req->flags))
Miklos Szeredib8f95e52018-07-26 16:13:11 +0200385 goto put_request;
Miklos Szeredi365ae712015-07-01 16:26:06 +0200386
Miklos Szeredi4ce60812015-07-01 16:26:02 +0200387 spin_lock(&fiq->waitq.lock);
Miklos Szeredi0d8e84b2015-07-01 16:25:58 +0200388 list_del_init(&req->intr_entry);
Miklos Szeredi4ce60812015-07-01 16:26:02 +0200389 spin_unlock(&fiq->waitq.lock);
Miklos Szeredi33e14b42015-07-01 16:26:01 +0200390 WARN_ON(test_bit(FR_PENDING, &req->flags));
391 WARN_ON(test_bit(FR_SENT, &req->flags));
Miklos Szeredi825d6d32015-07-01 16:25:58 +0200392 if (test_bit(FR_BACKGROUND, &req->flags)) {
Miklos Szerediefe28002015-07-01 16:26:07 +0200393 spin_lock(&fc->lock);
Miklos Szeredi825d6d32015-07-01 16:25:58 +0200394 clear_bit(FR_BACKGROUND, &req->flags);
Miklos Szeredi908a5722018-09-28 16:43:22 +0200395 if (fc->num_background == fc->max_background) {
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700396 fc->blocked = 0;
Maxim Patlasov722d2be2013-03-21 18:02:36 +0400397 wake_up(&fc->blocked_waitq);
Miklos Szeredi908a5722018-09-28 16:43:22 +0200398 } else if (!fc->blocked) {
399 /*
400 * Wake up next waiter, if any. It's okay to use
401 * waitqueue_active(), as we've already synced up
402 * fc->blocked with waiters with the wake_up() call
403 * above.
404 */
405 if (waitqueue_active(&fc->blocked_waitq))
406 wake_up(&fc->blocked_waitq);
407 }
Maxim Patlasov722d2be2013-03-21 18:02:36 +0400408
Tejun Heo8a301eb2018-02-02 09:54:14 -0800409 if (fc->num_background == fc->congestion_threshold && fc->sb) {
Jan Kara5f7f7542017-04-12 12:24:40 +0200410 clear_bdi_congested(fc->sb->s_bdi, BLK_RW_SYNC);
411 clear_bdi_congested(fc->sb->s_bdi, BLK_RW_ASYNC);
Miklos Szeredif92b99b2007-10-16 23:30:59 -0700412 }
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700413 fc->num_background--;
Miklos Szeredid12def12008-02-06 01:38:39 -0800414 fc->active_background--;
415 flush_bg_queue(fc);
Miklos Szerediefe28002015-07-01 16:26:07 +0200416 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700417 }
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700418 wake_up(&req->waitq);
Miklos Szeredi1e6881c2015-07-01 16:26:07 +0200419 if (req->end)
420 req->end(fc, req);
Miklos Szeredib8f95e52018-07-26 16:13:11 +0200421put_request:
Tejun Heoe9bb09d2008-11-26 12:03:54 +0100422 fuse_put_request(fc, req);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700423}
424
Miklos Szeredif88996a2015-07-01 16:26:01 +0200425static void queue_interrupt(struct fuse_iqueue *fiq, struct fuse_req *req)
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700426{
Miklos Szeredi4ce60812015-07-01 16:26:02 +0200427 spin_lock(&fiq->waitq.lock);
Sahitya Tummala6ba4d272017-02-08 20:30:56 +0530428 if (test_bit(FR_FINISHED, &req->flags)) {
429 spin_unlock(&fiq->waitq.lock);
430 return;
431 }
Miklos Szeredi8f7bb362015-07-01 16:26:03 +0200432 if (list_empty(&req->intr_entry)) {
433 list_add_tail(&req->intr_entry, &fiq->interrupts);
434 wake_up_locked(&fiq->waitq);
435 }
Miklos Szeredi4ce60812015-07-01 16:26:02 +0200436 spin_unlock(&fiq->waitq.lock);
Miklos Szeredif88996a2015-07-01 16:26:01 +0200437 kill_fasync(&fiq->fasync, SIGIO, POLL_IN);
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700438}
439
Miklos Szeredi7c352bd2005-09-09 13:10:39 -0700440static void request_wait_answer(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700441{
Miklos Szeredi4ce60812015-07-01 16:26:02 +0200442 struct fuse_iqueue *fiq = &fc->iq;
Miklos Szeredic4775262015-07-01 16:26:00 +0200443 int err;
444
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700445 if (!fc->no_interrupt) {
446 /* Any signal may interrupt this */
Miklos Szeredic4775262015-07-01 16:26:00 +0200447 err = wait_event_interruptible(req->waitq,
Miklos Szeredi33e14b42015-07-01 16:26:01 +0200448 test_bit(FR_FINISHED, &req->flags));
Miklos Szeredic4775262015-07-01 16:26:00 +0200449 if (!err)
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700450 return;
451
Miklos Szeredi825d6d32015-07-01 16:25:58 +0200452 set_bit(FR_INTERRUPTED, &req->flags);
Miklos Szeredi8f7bb362015-07-01 16:26:03 +0200453 /* matches barrier in fuse_dev_do_read() */
454 smp_mb__after_atomic();
Miklos Szeredi33e14b42015-07-01 16:26:01 +0200455 if (test_bit(FR_SENT, &req->flags))
Miklos Szeredi4ce60812015-07-01 16:26:02 +0200456 queue_interrupt(fiq, req);
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700457 }
458
Miklos Szeredi825d6d32015-07-01 16:25:58 +0200459 if (!test_bit(FR_FORCE, &req->flags)) {
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700460 /* Only fatal signals may interrupt this */
Al Viro7d3a07f2016-07-19 03:08:27 -0400461 err = wait_event_killable(req->waitq,
Miklos Szeredi33e14b42015-07-01 16:26:01 +0200462 test_bit(FR_FINISHED, &req->flags));
Miklos Szeredic4775262015-07-01 16:26:00 +0200463 if (!err)
Miklos Szeredia131de02007-10-16 23:31:04 -0700464 return;
465
Miklos Szeredi4ce60812015-07-01 16:26:02 +0200466 spin_lock(&fiq->waitq.lock);
Miklos Szeredia131de02007-10-16 23:31:04 -0700467 /* Request is not yet in userspace, bail out */
Miklos Szeredi33e14b42015-07-01 16:26:01 +0200468 if (test_bit(FR_PENDING, &req->flags)) {
Miklos Szeredia131de02007-10-16 23:31:04 -0700469 list_del(&req->list);
Miklos Szeredi4ce60812015-07-01 16:26:02 +0200470 spin_unlock(&fiq->waitq.lock);
Miklos Szeredia131de02007-10-16 23:31:04 -0700471 __fuse_put_request(req);
472 req->out.h.error = -EINTR;
473 return;
474 }
Miklos Szeredi4ce60812015-07-01 16:26:02 +0200475 spin_unlock(&fiq->waitq.lock);
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700476 }
Miklos Szeredi334f4852005-09-09 13:10:27 -0700477
Miklos Szeredia131de02007-10-16 23:31:04 -0700478 /*
479 * Either request is already in userspace, or it was forced.
480 * Wait it out.
481 */
Miklos Szeredi33e14b42015-07-01 16:26:01 +0200482 wait_event(req->waitq, test_bit(FR_FINISHED, &req->flags));
Miklos Szeredi334f4852005-09-09 13:10:27 -0700483}
484
Eric Wong6a4e9222013-02-04 13:04:44 +0000485static void __fuse_request_send(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700486{
Miklos Szeredie16714d2015-07-01 16:26:01 +0200487 struct fuse_iqueue *fiq = &fc->iq;
488
Miklos Szeredi825d6d32015-07-01 16:25:58 +0200489 BUG_ON(test_bit(FR_BACKGROUND, &req->flags));
Miklos Szeredi4ce60812015-07-01 16:26:02 +0200490 spin_lock(&fiq->waitq.lock);
Miklos Szeredie16714d2015-07-01 16:26:01 +0200491 if (!fiq->connected) {
Miklos Szeredi4ce60812015-07-01 16:26:02 +0200492 spin_unlock(&fiq->waitq.lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700493 req->out.h.error = -ENOTCONN;
Miklos Szeredic4775262015-07-01 16:26:00 +0200494 } else {
Miklos Szeredif88996a2015-07-01 16:26:01 +0200495 req->in.h.unique = fuse_get_unique(fiq);
496 queue_request(fiq, req);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700497 /* acquire extra reference, since request is still needed
498 after request_end() */
499 __fuse_get_request(req);
Miklos Szeredi4ce60812015-07-01 16:26:02 +0200500 spin_unlock(&fiq->waitq.lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700501
Miklos Szeredi7c352bd2005-09-09 13:10:39 -0700502 request_wait_answer(fc, req);
Miklos Szeredic4775262015-07-01 16:26:00 +0200503 /* Pairs with smp_wmb() in request_end() */
504 smp_rmb();
Miklos Szeredi334f4852005-09-09 13:10:27 -0700505 }
Miklos Szeredi334f4852005-09-09 13:10:27 -0700506}
Eric Wong6a4e9222013-02-04 13:04:44 +0000507
508void fuse_request_send(struct fuse_conn *fc, struct fuse_req *req)
509{
Miklos Szeredi825d6d32015-07-01 16:25:58 +0200510 __set_bit(FR_ISREPLY, &req->flags);
511 if (!test_bit(FR_WAITING, &req->flags)) {
512 __set_bit(FR_WAITING, &req->flags);
Miklos Szeredi5437f242015-07-01 16:25:56 +0200513 atomic_inc(&fc->num_waiting);
514 }
Eric Wong6a4e9222013-02-04 13:04:44 +0000515 __fuse_request_send(fc, req);
516}
Tejun Heo08cbf542009-04-14 10:54:53 +0900517EXPORT_SYMBOL_GPL(fuse_request_send);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700518
Miklos Szeredi21f62172015-01-06 10:45:35 +0100519static void fuse_adjust_compat(struct fuse_conn *fc, struct fuse_args *args)
520{
521 if (fc->minor < 4 && args->in.h.opcode == FUSE_STATFS)
522 args->out.args[0].size = FUSE_COMPAT_STATFS_SIZE;
523
524 if (fc->minor < 9) {
525 switch (args->in.h.opcode) {
526 case FUSE_LOOKUP:
527 case FUSE_CREATE:
528 case FUSE_MKNOD:
529 case FUSE_MKDIR:
530 case FUSE_SYMLINK:
531 case FUSE_LINK:
532 args->out.args[0].size = FUSE_COMPAT_ENTRY_OUT_SIZE;
533 break;
534 case FUSE_GETATTR:
535 case FUSE_SETATTR:
536 args->out.args[0].size = FUSE_COMPAT_ATTR_OUT_SIZE;
537 break;
538 }
539 }
540 if (fc->minor < 12) {
541 switch (args->in.h.opcode) {
542 case FUSE_CREATE:
543 args->in.args[0].size = sizeof(struct fuse_open_in);
544 break;
545 case FUSE_MKNOD:
546 args->in.args[0].size = FUSE_COMPAT_MKNOD_IN_SIZE;
547 break;
548 }
549 }
550}
551
Miklos Szeredi70781872014-12-12 09:49:05 +0100552ssize_t fuse_simple_request(struct fuse_conn *fc, struct fuse_args *args)
553{
554 struct fuse_req *req;
555 ssize_t ret;
556
557 req = fuse_get_req(fc, 0);
558 if (IS_ERR(req))
559 return PTR_ERR(req);
560
Miklos Szeredi21f62172015-01-06 10:45:35 +0100561 /* Needs to be done after fuse_get_req() so that fc->minor is valid */
562 fuse_adjust_compat(fc, args);
563
Miklos Szeredi70781872014-12-12 09:49:05 +0100564 req->in.h.opcode = args->in.h.opcode;
565 req->in.h.nodeid = args->in.h.nodeid;
566 req->in.numargs = args->in.numargs;
567 memcpy(req->in.args, args->in.args,
568 args->in.numargs * sizeof(struct fuse_in_arg));
569 req->out.argvar = args->out.argvar;
570 req->out.numargs = args->out.numargs;
571 memcpy(req->out.args, args->out.args,
572 args->out.numargs * sizeof(struct fuse_arg));
573 fuse_request_send(fc, req);
574 ret = req->out.h.error;
575 if (!ret && args->out.argvar) {
576 BUG_ON(args->out.numargs != 1);
577 ret = req->out.args[0].size;
578 }
579 fuse_put_request(fc, req);
580
581 return ret;
582}
583
Miklos Szeredif0139aa2015-07-01 16:25:57 +0200584/*
585 * Called under fc->lock
586 *
587 * fc->connected must have been checked previously
588 */
589void fuse_request_send_background_locked(struct fuse_conn *fc,
590 struct fuse_req *req)
Miklos Szeredid12def12008-02-06 01:38:39 -0800591{
Miklos Szeredi825d6d32015-07-01 16:25:58 +0200592 BUG_ON(!test_bit(FR_BACKGROUND, &req->flags));
593 if (!test_bit(FR_WAITING, &req->flags)) {
594 __set_bit(FR_WAITING, &req->flags);
Miklos Szeredi5437f242015-07-01 16:25:56 +0200595 atomic_inc(&fc->num_waiting);
596 }
Miklos Szeredi825d6d32015-07-01 16:25:58 +0200597 __set_bit(FR_ISREPLY, &req->flags);
Miklos Szeredid12def12008-02-06 01:38:39 -0800598 fc->num_background++;
Csaba Henk7a6d3c82009-07-01 17:28:41 -0700599 if (fc->num_background == fc->max_background)
Miklos Szeredid12def12008-02-06 01:38:39 -0800600 fc->blocked = 1;
Jan Kara7fbbe972017-04-12 12:24:41 +0200601 if (fc->num_background == fc->congestion_threshold && fc->sb) {
Jan Kara5f7f7542017-04-12 12:24:40 +0200602 set_bdi_congested(fc->sb->s_bdi, BLK_RW_SYNC);
603 set_bdi_congested(fc->sb->s_bdi, BLK_RW_ASYNC);
Miklos Szeredid12def12008-02-06 01:38:39 -0800604 }
605 list_add_tail(&req->list, &fc->bg_queue);
606 flush_bg_queue(fc);
607}
608
Miklos Szeredif0139aa2015-07-01 16:25:57 +0200609void fuse_request_send_background(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700610{
Miklos Szeredi42dc6212015-07-01 16:25:56 +0200611 BUG_ON(!req->end);
Miklos Szeredid7133112006-04-10 22:54:55 -0700612 spin_lock(&fc->lock);
Miklos Szeredi1e9a4ed2005-09-09 13:10:31 -0700613 if (fc->connected) {
Miklos Szeredif0139aa2015-07-01 16:25:57 +0200614 fuse_request_send_background_locked(fc, req);
Miklos Szeredid7133112006-04-10 22:54:55 -0700615 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700616 } else {
Miklos Szeredi42dc6212015-07-01 16:25:56 +0200617 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700618 req->out.h.error = -ENOTCONN;
Miklos Szeredi42dc6212015-07-01 16:25:56 +0200619 req->end(fc, req);
620 fuse_put_request(fc, req);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700621 }
622}
Tejun Heo08cbf542009-04-14 10:54:53 +0900623EXPORT_SYMBOL_GPL(fuse_request_send_background);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700624
Miklos Szeredi2d45ba32010-07-12 14:41:40 +0200625static int fuse_request_send_notify_reply(struct fuse_conn *fc,
626 struct fuse_req *req, u64 unique)
627{
628 int err = -ENODEV;
Miklos Szeredif88996a2015-07-01 16:26:01 +0200629 struct fuse_iqueue *fiq = &fc->iq;
Miklos Szeredi2d45ba32010-07-12 14:41:40 +0200630
Miklos Szeredi825d6d32015-07-01 16:25:58 +0200631 __clear_bit(FR_ISREPLY, &req->flags);
Miklos Szeredi2d45ba32010-07-12 14:41:40 +0200632 req->in.h.unique = unique;
Miklos Szeredi4ce60812015-07-01 16:26:02 +0200633 spin_lock(&fiq->waitq.lock);
Miklos Szeredie16714d2015-07-01 16:26:01 +0200634 if (fiq->connected) {
Miklos Szeredif88996a2015-07-01 16:26:01 +0200635 queue_request(fiq, req);
Miklos Szeredi2d45ba32010-07-12 14:41:40 +0200636 err = 0;
637 }
Miklos Szeredi4ce60812015-07-01 16:26:02 +0200638 spin_unlock(&fiq->waitq.lock);
Miklos Szeredi2d45ba32010-07-12 14:41:40 +0200639
640 return err;
641}
642
Anand V. Avati0b05b182012-08-19 08:53:23 -0400643void fuse_force_forget(struct file *file, u64 nodeid)
644{
Al Viro6131ffa2013-02-27 16:59:05 -0500645 struct inode *inode = file_inode(file);
Anand V. Avati0b05b182012-08-19 08:53:23 -0400646 struct fuse_conn *fc = get_fuse_conn(inode);
647 struct fuse_req *req;
648 struct fuse_forget_in inarg;
649
650 memset(&inarg, 0, sizeof(inarg));
651 inarg.nlookup = 1;
Maxim Patlasovb111c8c2012-10-26 19:48:30 +0400652 req = fuse_get_req_nofail_nopages(fc, file);
Anand V. Avati0b05b182012-08-19 08:53:23 -0400653 req->in.h.opcode = FUSE_FORGET;
654 req->in.h.nodeid = nodeid;
655 req->in.numargs = 1;
656 req->in.args[0].size = sizeof(inarg);
657 req->in.args[0].value = &inarg;
Miklos Szeredi825d6d32015-07-01 16:25:58 +0200658 __clear_bit(FR_ISREPLY, &req->flags);
Eric Wong6a4e9222013-02-04 13:04:44 +0000659 __fuse_request_send(fc, req);
660 /* ignore errors */
661 fuse_put_request(fc, req);
Anand V. Avati0b05b182012-08-19 08:53:23 -0400662}
663
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700664/*
Miklos Szeredi334f4852005-09-09 13:10:27 -0700665 * Lock the request. Up to the next unlock_request() there mustn't be
666 * anything that could cause a page-fault. If the request was already
Miklos Szeredif9a28422006-06-25 05:48:53 -0700667 * aborted bail out.
Miklos Szeredi334f4852005-09-09 13:10:27 -0700668 */
Miklos Szeredidc008092015-07-01 16:25:58 +0200669static int lock_request(struct fuse_req *req)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700670{
671 int err = 0;
672 if (req) {
Miklos Szeredidc008092015-07-01 16:25:58 +0200673 spin_lock(&req->waitq.lock);
Miklos Szeredi825d6d32015-07-01 16:25:58 +0200674 if (test_bit(FR_ABORTED, &req->flags))
Miklos Szeredi334f4852005-09-09 13:10:27 -0700675 err = -ENOENT;
676 else
Miklos Szeredi825d6d32015-07-01 16:25:58 +0200677 set_bit(FR_LOCKED, &req->flags);
Miklos Szeredidc008092015-07-01 16:25:58 +0200678 spin_unlock(&req->waitq.lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700679 }
680 return err;
681}
682
683/*
Miklos Szeredi0d8e84b2015-07-01 16:25:58 +0200684 * Unlock request. If it was aborted while locked, caller is responsible
685 * for unlocking and ending the request.
Miklos Szeredi334f4852005-09-09 13:10:27 -0700686 */
Miklos Szeredidc008092015-07-01 16:25:58 +0200687static int unlock_request(struct fuse_req *req)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700688{
Miklos Szeredi0d8e84b2015-07-01 16:25:58 +0200689 int err = 0;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700690 if (req) {
Miklos Szeredidc008092015-07-01 16:25:58 +0200691 spin_lock(&req->waitq.lock);
Miklos Szeredi825d6d32015-07-01 16:25:58 +0200692 if (test_bit(FR_ABORTED, &req->flags))
Miklos Szeredi0d8e84b2015-07-01 16:25:58 +0200693 err = -ENOENT;
694 else
Miklos Szeredi825d6d32015-07-01 16:25:58 +0200695 clear_bit(FR_LOCKED, &req->flags);
Miklos Szeredidc008092015-07-01 16:25:58 +0200696 spin_unlock(&req->waitq.lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700697 }
Miklos Szeredi0d8e84b2015-07-01 16:25:58 +0200698 return err;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700699}
700
701struct fuse_copy_state {
702 int write;
703 struct fuse_req *req;
Al Viro6c09e942015-04-03 22:06:08 -0400704 struct iov_iter *iter;
Miklos Szeredidd3bb142010-05-25 15:06:06 +0200705 struct pipe_buffer *pipebufs;
706 struct pipe_buffer *currbuf;
707 struct pipe_inode_info *pipe;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700708 unsigned long nr_segs;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700709 struct page *pg;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700710 unsigned len;
Miklos Szeredic55a01d2014-07-07 15:28:51 +0200711 unsigned offset;
Miklos Szeredice534fb2010-05-25 15:06:07 +0200712 unsigned move_pages:1;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700713};
714
Miklos Szeredidc008092015-07-01 16:25:58 +0200715static void fuse_copy_init(struct fuse_copy_state *cs, int write,
Al Viro6c09e942015-04-03 22:06:08 -0400716 struct iov_iter *iter)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700717{
718 memset(cs, 0, sizeof(*cs));
719 cs->write = write;
Al Viro6c09e942015-04-03 22:06:08 -0400720 cs->iter = iter;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700721}
722
723/* Unmap and put previous page of userspace buffer */
Miklos Szeredi8bfc0162006-01-16 22:14:28 -0800724static void fuse_copy_finish(struct fuse_copy_state *cs)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700725{
Miklos Szeredidd3bb142010-05-25 15:06:06 +0200726 if (cs->currbuf) {
727 struct pipe_buffer *buf = cs->currbuf;
728
Miklos Szeredic55a01d2014-07-07 15:28:51 +0200729 if (cs->write)
Miklos Szeredic3021622010-05-25 15:06:07 +0200730 buf->len = PAGE_SIZE - cs->len;
Miklos Szeredidd3bb142010-05-25 15:06:06 +0200731 cs->currbuf = NULL;
Miklos Szeredic55a01d2014-07-07 15:28:51 +0200732 } else if (cs->pg) {
Miklos Szeredi334f4852005-09-09 13:10:27 -0700733 if (cs->write) {
734 flush_dcache_page(cs->pg);
735 set_page_dirty_lock(cs->pg);
736 }
737 put_page(cs->pg);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700738 }
Miklos Szeredic55a01d2014-07-07 15:28:51 +0200739 cs->pg = NULL;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700740}
741
742/*
743 * Get another pagefull of userspace buffer, and map it to kernel
744 * address space, and lock request
745 */
746static int fuse_copy_fill(struct fuse_copy_state *cs)
747{
Miklos Szeredic55a01d2014-07-07 15:28:51 +0200748 struct page *page;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700749 int err;
750
Miklos Szeredidc008092015-07-01 16:25:58 +0200751 err = unlock_request(cs->req);
Miklos Szeredi0d8e84b2015-07-01 16:25:58 +0200752 if (err)
753 return err;
754
Miklos Szeredi334f4852005-09-09 13:10:27 -0700755 fuse_copy_finish(cs);
Miklos Szeredidd3bb142010-05-25 15:06:06 +0200756 if (cs->pipebufs) {
757 struct pipe_buffer *buf = cs->pipebufs;
758
Miklos Szeredic3021622010-05-25 15:06:07 +0200759 if (!cs->write) {
Miklos Szeredifba597d2016-09-27 10:45:12 +0200760 err = pipe_buf_confirm(cs->pipe, buf);
Miklos Szeredic3021622010-05-25 15:06:07 +0200761 if (err)
762 return err;
Miklos Szeredidd3bb142010-05-25 15:06:06 +0200763
Miklos Szeredic3021622010-05-25 15:06:07 +0200764 BUG_ON(!cs->nr_segs);
765 cs->currbuf = buf;
Miklos Szeredic55a01d2014-07-07 15:28:51 +0200766 cs->pg = buf->page;
767 cs->offset = buf->offset;
Miklos Szeredic3021622010-05-25 15:06:07 +0200768 cs->len = buf->len;
Miklos Szeredic3021622010-05-25 15:06:07 +0200769 cs->pipebufs++;
770 cs->nr_segs--;
771 } else {
Miklos Szeredic3021622010-05-25 15:06:07 +0200772 if (cs->nr_segs == cs->pipe->buffers)
773 return -EIO;
774
775 page = alloc_page(GFP_HIGHUSER);
776 if (!page)
777 return -ENOMEM;
778
779 buf->page = page;
780 buf->offset = 0;
781 buf->len = 0;
782
783 cs->currbuf = buf;
Miklos Szeredic55a01d2014-07-07 15:28:51 +0200784 cs->pg = page;
785 cs->offset = 0;
Miklos Szeredic3021622010-05-25 15:06:07 +0200786 cs->len = PAGE_SIZE;
787 cs->pipebufs++;
788 cs->nr_segs++;
789 }
Miklos Szeredidd3bb142010-05-25 15:06:06 +0200790 } else {
Al Viro6c09e942015-04-03 22:06:08 -0400791 size_t off;
792 err = iov_iter_get_pages(cs->iter, &page, PAGE_SIZE, 1, &off);
Miklos Szeredidd3bb142010-05-25 15:06:06 +0200793 if (err < 0)
794 return err;
Al Viro6c09e942015-04-03 22:06:08 -0400795 BUG_ON(!err);
796 cs->len = err;
797 cs->offset = off;
Miklos Szeredic55a01d2014-07-07 15:28:51 +0200798 cs->pg = page;
Al Viro6c09e942015-04-03 22:06:08 -0400799 iov_iter_advance(cs->iter, err);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700800 }
Miklos Szeredi334f4852005-09-09 13:10:27 -0700801
Miklos Szeredidc008092015-07-01 16:25:58 +0200802 return lock_request(cs->req);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700803}
804
805/* Do as much copy to/from userspace buffer as we can */
Miklos Szeredi8bfc0162006-01-16 22:14:28 -0800806static int fuse_copy_do(struct fuse_copy_state *cs, void **val, unsigned *size)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700807{
808 unsigned ncpy = min(*size, cs->len);
809 if (val) {
Miklos Szeredic55a01d2014-07-07 15:28:51 +0200810 void *pgaddr = kmap_atomic(cs->pg);
811 void *buf = pgaddr + cs->offset;
812
Miklos Szeredi334f4852005-09-09 13:10:27 -0700813 if (cs->write)
Miklos Szeredic55a01d2014-07-07 15:28:51 +0200814 memcpy(buf, *val, ncpy);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700815 else
Miklos Szeredic55a01d2014-07-07 15:28:51 +0200816 memcpy(*val, buf, ncpy);
817
818 kunmap_atomic(pgaddr);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700819 *val += ncpy;
820 }
821 *size -= ncpy;
822 cs->len -= ncpy;
Miklos Szeredic55a01d2014-07-07 15:28:51 +0200823 cs->offset += ncpy;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700824 return ncpy;
825}
826
Miklos Szeredice534fb2010-05-25 15:06:07 +0200827static int fuse_check_page(struct page *page)
828{
829 if (page_mapcount(page) ||
830 page->mapping != NULL ||
831 page_count(page) != 1 ||
832 (page->flags & PAGE_FLAGS_CHECK_AT_PREP &
833 ~(1 << PG_locked |
834 1 << PG_referenced |
835 1 << PG_uptodate |
836 1 << PG_lru |
837 1 << PG_active |
838 1 << PG_reclaim))) {
839 printk(KERN_WARNING "fuse: trying to steal weird page\n");
840 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);
841 return 1;
842 }
843 return 0;
844}
845
846static int fuse_try_move_page(struct fuse_copy_state *cs, struct page **pagep)
847{
848 int err;
849 struct page *oldpage = *pagep;
850 struct page *newpage;
851 struct pipe_buffer *buf = cs->pipebufs;
Miklos Szeredice534fb2010-05-25 15:06:07 +0200852
Miklos Szeredidc008092015-07-01 16:25:58 +0200853 err = unlock_request(cs->req);
Miklos Szeredi0d8e84b2015-07-01 16:25:58 +0200854 if (err)
855 return err;
856
Miklos Szeredice534fb2010-05-25 15:06:07 +0200857 fuse_copy_finish(cs);
858
Miklos Szeredifba597d2016-09-27 10:45:12 +0200859 err = pipe_buf_confirm(cs->pipe, buf);
Miklos Szeredice534fb2010-05-25 15:06:07 +0200860 if (err)
861 return err;
862
863 BUG_ON(!cs->nr_segs);
864 cs->currbuf = buf;
865 cs->len = buf->len;
866 cs->pipebufs++;
867 cs->nr_segs--;
868
869 if (cs->len != PAGE_SIZE)
870 goto out_fallback;
871
Miklos Szeredica76f5b2016-09-27 10:45:12 +0200872 if (pipe_buf_steal(cs->pipe, buf) != 0)
Miklos Szeredice534fb2010-05-25 15:06:07 +0200873 goto out_fallback;
874
875 newpage = buf->page;
876
Miklos Szerediaa991b32015-02-26 11:45:47 +0100877 if (!PageUptodate(newpage))
878 SetPageUptodate(newpage);
Miklos Szeredice534fb2010-05-25 15:06:07 +0200879
880 ClearPageMappedToDisk(newpage);
881
882 if (fuse_check_page(newpage) != 0)
883 goto out_fallback_unlock;
884
Miklos Szeredice534fb2010-05-25 15:06:07 +0200885 /*
886 * This is a new and locked page, it shouldn't be mapped or
887 * have any special flags on it
888 */
889 if (WARN_ON(page_mapped(oldpage)))
890 goto out_fallback_unlock;
891 if (WARN_ON(page_has_private(oldpage)))
892 goto out_fallback_unlock;
893 if (WARN_ON(PageDirty(oldpage) || PageWriteback(oldpage)))
894 goto out_fallback_unlock;
895 if (WARN_ON(PageMlocked(oldpage)))
896 goto out_fallback_unlock;
897
Miklos Szeredief6a3c62011-03-22 16:30:52 -0700898 err = replace_page_cache_page(oldpage, newpage, GFP_KERNEL);
Miklos Szeredice534fb2010-05-25 15:06:07 +0200899 if (err) {
Miklos Szeredief6a3c62011-03-22 16:30:52 -0700900 unlock_page(newpage);
901 return err;
Miklos Szeredice534fb2010-05-25 15:06:07 +0200902 }
Miklos Szeredief6a3c62011-03-22 16:30:52 -0700903
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300904 get_page(newpage);
Miklos Szeredice534fb2010-05-25 15:06:07 +0200905
906 if (!(buf->flags & PIPE_BUF_FLAG_LRU))
907 lru_cache_add_file(newpage);
908
909 err = 0;
Miklos Szeredidc008092015-07-01 16:25:58 +0200910 spin_lock(&cs->req->waitq.lock);
Miklos Szeredi825d6d32015-07-01 16:25:58 +0200911 if (test_bit(FR_ABORTED, &cs->req->flags))
Miklos Szeredice534fb2010-05-25 15:06:07 +0200912 err = -ENOENT;
913 else
914 *pagep = newpage;
Miklos Szeredidc008092015-07-01 16:25:58 +0200915 spin_unlock(&cs->req->waitq.lock);
Miklos Szeredice534fb2010-05-25 15:06:07 +0200916
917 if (err) {
918 unlock_page(newpage);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300919 put_page(newpage);
Miklos Szeredice534fb2010-05-25 15:06:07 +0200920 return err;
921 }
922
923 unlock_page(oldpage);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300924 put_page(oldpage);
Miklos Szeredice534fb2010-05-25 15:06:07 +0200925 cs->len = 0;
926
927 return 0;
928
929out_fallback_unlock:
930 unlock_page(newpage);
931out_fallback:
Miklos Szeredic55a01d2014-07-07 15:28:51 +0200932 cs->pg = buf->page;
933 cs->offset = buf->offset;
Miklos Szeredice534fb2010-05-25 15:06:07 +0200934
Miklos Szeredidc008092015-07-01 16:25:58 +0200935 err = lock_request(cs->req);
Miklos Szeredice534fb2010-05-25 15:06:07 +0200936 if (err)
937 return err;
938
939 return 1;
940}
941
Miklos Szeredic3021622010-05-25 15:06:07 +0200942static int fuse_ref_page(struct fuse_copy_state *cs, struct page *page,
943 unsigned offset, unsigned count)
944{
945 struct pipe_buffer *buf;
Miklos Szeredi0d8e84b2015-07-01 16:25:58 +0200946 int err;
Miklos Szeredic3021622010-05-25 15:06:07 +0200947
948 if (cs->nr_segs == cs->pipe->buffers)
949 return -EIO;
950
Miklos Szeredidc008092015-07-01 16:25:58 +0200951 err = unlock_request(cs->req);
Miklos Szeredi0d8e84b2015-07-01 16:25:58 +0200952 if (err)
953 return err;
954
Miklos Szeredic3021622010-05-25 15:06:07 +0200955 fuse_copy_finish(cs);
956
957 buf = cs->pipebufs;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300958 get_page(page);
Miklos Szeredic3021622010-05-25 15:06:07 +0200959 buf->page = page;
960 buf->offset = offset;
961 buf->len = count;
962
963 cs->pipebufs++;
964 cs->nr_segs++;
965 cs->len = 0;
966
967 return 0;
968}
969
Miklos Szeredi334f4852005-09-09 13:10:27 -0700970/*
971 * Copy a page in the request to/from the userspace buffer. Must be
972 * done atomically
973 */
Miklos Szeredice534fb2010-05-25 15:06:07 +0200974static int fuse_copy_page(struct fuse_copy_state *cs, struct page **pagep,
Miklos Szeredi8bfc0162006-01-16 22:14:28 -0800975 unsigned offset, unsigned count, int zeroing)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700976{
Miklos Szeredice534fb2010-05-25 15:06:07 +0200977 int err;
978 struct page *page = *pagep;
979
Miklos Szeredib6777c42010-10-26 14:22:27 -0700980 if (page && zeroing && count < PAGE_SIZE)
981 clear_highpage(page);
982
Miklos Szeredi334f4852005-09-09 13:10:27 -0700983 while (count) {
Miklos Szeredic3021622010-05-25 15:06:07 +0200984 if (cs->write && cs->pipebufs && page) {
985 return fuse_ref_page(cs, page, offset, count);
986 } else if (!cs->len) {
Miklos Szeredice534fb2010-05-25 15:06:07 +0200987 if (cs->move_pages && page &&
988 offset == 0 && count == PAGE_SIZE) {
989 err = fuse_try_move_page(cs, pagep);
990 if (err <= 0)
991 return err;
992 } else {
993 err = fuse_copy_fill(cs);
994 if (err)
995 return err;
996 }
Miklos Szeredi1729a162008-11-26 12:03:54 +0100997 }
Miklos Szeredi334f4852005-09-09 13:10:27 -0700998 if (page) {
Cong Wang2408f6e2011-11-25 23:14:30 +0800999 void *mapaddr = kmap_atomic(page);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001000 void *buf = mapaddr + offset;
1001 offset += fuse_copy_do(cs, &buf, &count);
Cong Wang2408f6e2011-11-25 23:14:30 +08001002 kunmap_atomic(mapaddr);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001003 } else
1004 offset += fuse_copy_do(cs, NULL, &count);
1005 }
1006 if (page && !cs->write)
1007 flush_dcache_page(page);
1008 return 0;
1009}
1010
1011/* Copy pages in the request to/from userspace buffer */
1012static int fuse_copy_pages(struct fuse_copy_state *cs, unsigned nbytes,
1013 int zeroing)
1014{
1015 unsigned i;
1016 struct fuse_req *req = cs->req;
Miklos Szeredi334f4852005-09-09 13:10:27 -07001017
1018 for (i = 0; i < req->num_pages && (nbytes || zeroing); i++) {
Miklos Szeredice534fb2010-05-25 15:06:07 +02001019 int err;
Maxim Patlasov85f40ae2012-10-26 19:49:33 +04001020 unsigned offset = req->page_descs[i].offset;
1021 unsigned count = min(nbytes, req->page_descs[i].length);
Miklos Szeredice534fb2010-05-25 15:06:07 +02001022
1023 err = fuse_copy_page(cs, &req->pages[i], offset, count,
1024 zeroing);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001025 if (err)
1026 return err;
1027
1028 nbytes -= count;
Miklos Szeredi334f4852005-09-09 13:10:27 -07001029 }
1030 return 0;
1031}
1032
1033/* Copy a single argument in the request to/from userspace buffer */
1034static int fuse_copy_one(struct fuse_copy_state *cs, void *val, unsigned size)
1035{
1036 while (size) {
Miklos Szeredi1729a162008-11-26 12:03:54 +01001037 if (!cs->len) {
1038 int err = fuse_copy_fill(cs);
1039 if (err)
1040 return err;
1041 }
Miklos Szeredi334f4852005-09-09 13:10:27 -07001042 fuse_copy_do(cs, &val, &size);
1043 }
1044 return 0;
1045}
1046
1047/* Copy request arguments to/from userspace buffer */
1048static int fuse_copy_args(struct fuse_copy_state *cs, unsigned numargs,
1049 unsigned argpages, struct fuse_arg *args,
1050 int zeroing)
1051{
1052 int err = 0;
1053 unsigned i;
1054
1055 for (i = 0; !err && i < numargs; i++) {
1056 struct fuse_arg *arg = &args[i];
1057 if (i == numargs - 1 && argpages)
1058 err = fuse_copy_pages(cs, arg->size, zeroing);
1059 else
1060 err = fuse_copy_one(cs, arg->value, arg->size);
1061 }
1062 return err;
1063}
1064
Miklos Szeredif88996a2015-07-01 16:26:01 +02001065static int forget_pending(struct fuse_iqueue *fiq)
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001066{
Miklos Szeredif88996a2015-07-01 16:26:01 +02001067 return fiq->forget_list_head.next != NULL;
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001068}
1069
Miklos Szeredif88996a2015-07-01 16:26:01 +02001070static int request_pending(struct fuse_iqueue *fiq)
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001071{
Miklos Szeredif88996a2015-07-01 16:26:01 +02001072 return !list_empty(&fiq->pending) || !list_empty(&fiq->interrupts) ||
1073 forget_pending(fiq);
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001074}
1075
Miklos Szeredi334f4852005-09-09 13:10:27 -07001076/*
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001077 * Transfer an interrupt request to userspace
1078 *
1079 * Unlike other requests this is assembled on demand, without a need
1080 * to allocate a separate fuse_req structure.
1081 *
Miklos Szeredifd22d622015-07-01 16:26:03 +02001082 * Called with fiq->waitq.lock held, releases it
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001083 */
Miklos Szeredifd22d622015-07-01 16:26:03 +02001084static int fuse_read_interrupt(struct fuse_iqueue *fiq,
1085 struct fuse_copy_state *cs,
Miklos Szeredic3021622010-05-25 15:06:07 +02001086 size_t nbytes, struct fuse_req *req)
Miklos Szeredifd22d622015-07-01 16:26:03 +02001087__releases(fiq->waitq.lock)
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001088{
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001089 struct fuse_in_header ih;
1090 struct fuse_interrupt_in arg;
1091 unsigned reqsize = sizeof(ih) + sizeof(arg);
1092 int err;
1093
1094 list_del_init(&req->intr_entry);
Miklos Szeredi4ce60812015-07-01 16:26:02 +02001095 req->intr_unique = fuse_get_unique(fiq);
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001096 memset(&ih, 0, sizeof(ih));
1097 memset(&arg, 0, sizeof(arg));
1098 ih.len = reqsize;
1099 ih.opcode = FUSE_INTERRUPT;
1100 ih.unique = req->intr_unique;
1101 arg.unique = req->in.h.unique;
1102
Miklos Szeredi4ce60812015-07-01 16:26:02 +02001103 spin_unlock(&fiq->waitq.lock);
Miklos Szeredic3021622010-05-25 15:06:07 +02001104 if (nbytes < reqsize)
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001105 return -EINVAL;
1106
Miklos Szeredic3021622010-05-25 15:06:07 +02001107 err = fuse_copy_one(cs, &ih, sizeof(ih));
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001108 if (!err)
Miklos Szeredic3021622010-05-25 15:06:07 +02001109 err = fuse_copy_one(cs, &arg, sizeof(arg));
1110 fuse_copy_finish(cs);
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001111
1112 return err ? err : reqsize;
1113}
1114
Miklos Szeredif88996a2015-07-01 16:26:01 +02001115static struct fuse_forget_link *dequeue_forget(struct fuse_iqueue *fiq,
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001116 unsigned max,
1117 unsigned *countp)
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001118{
Miklos Szeredif88996a2015-07-01 16:26:01 +02001119 struct fuse_forget_link *head = fiq->forget_list_head.next;
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001120 struct fuse_forget_link **newhead = &head;
1121 unsigned count;
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001122
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001123 for (count = 0; *newhead != NULL && count < max; count++)
1124 newhead = &(*newhead)->next;
1125
Miklos Szeredif88996a2015-07-01 16:26:01 +02001126 fiq->forget_list_head.next = *newhead;
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001127 *newhead = NULL;
Miklos Szeredif88996a2015-07-01 16:26:01 +02001128 if (fiq->forget_list_head.next == NULL)
1129 fiq->forget_list_tail = &fiq->forget_list_head;
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001130
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001131 if (countp != NULL)
1132 *countp = count;
1133
1134 return head;
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001135}
1136
Miklos Szeredifd22d622015-07-01 16:26:03 +02001137static int fuse_read_single_forget(struct fuse_iqueue *fiq,
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001138 struct fuse_copy_state *cs,
1139 size_t nbytes)
Miklos Szeredifd22d622015-07-01 16:26:03 +02001140__releases(fiq->waitq.lock)
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001141{
1142 int err;
Miklos Szeredif88996a2015-07-01 16:26:01 +02001143 struct fuse_forget_link *forget = dequeue_forget(fiq, 1, NULL);
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001144 struct fuse_forget_in arg = {
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001145 .nlookup = forget->forget_one.nlookup,
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001146 };
1147 struct fuse_in_header ih = {
1148 .opcode = FUSE_FORGET,
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001149 .nodeid = forget->forget_one.nodeid,
Miklos Szeredif88996a2015-07-01 16:26:01 +02001150 .unique = fuse_get_unique(fiq),
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001151 .len = sizeof(ih) + sizeof(arg),
1152 };
1153
Miklos Szeredi4ce60812015-07-01 16:26:02 +02001154 spin_unlock(&fiq->waitq.lock);
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001155 kfree(forget);
1156 if (nbytes < ih.len)
1157 return -EINVAL;
1158
1159 err = fuse_copy_one(cs, &ih, sizeof(ih));
1160 if (!err)
1161 err = fuse_copy_one(cs, &arg, sizeof(arg));
1162 fuse_copy_finish(cs);
1163
1164 if (err)
1165 return err;
1166
1167 return ih.len;
1168}
1169
Miklos Szeredifd22d622015-07-01 16:26:03 +02001170static int fuse_read_batch_forget(struct fuse_iqueue *fiq,
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001171 struct fuse_copy_state *cs, size_t nbytes)
Miklos Szeredifd22d622015-07-01 16:26:03 +02001172__releases(fiq->waitq.lock)
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001173{
1174 int err;
1175 unsigned max_forgets;
1176 unsigned count;
1177 struct fuse_forget_link *head;
1178 struct fuse_batch_forget_in arg = { .count = 0 };
1179 struct fuse_in_header ih = {
1180 .opcode = FUSE_BATCH_FORGET,
Miklos Szeredif88996a2015-07-01 16:26:01 +02001181 .unique = fuse_get_unique(fiq),
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001182 .len = sizeof(ih) + sizeof(arg),
1183 };
1184
1185 if (nbytes < ih.len) {
Miklos Szeredi4ce60812015-07-01 16:26:02 +02001186 spin_unlock(&fiq->waitq.lock);
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001187 return -EINVAL;
1188 }
1189
1190 max_forgets = (nbytes - ih.len) / sizeof(struct fuse_forget_one);
Miklos Szeredif88996a2015-07-01 16:26:01 +02001191 head = dequeue_forget(fiq, max_forgets, &count);
Miklos Szeredi4ce60812015-07-01 16:26:02 +02001192 spin_unlock(&fiq->waitq.lock);
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001193
1194 arg.count = count;
1195 ih.len += count * sizeof(struct fuse_forget_one);
1196 err = fuse_copy_one(cs, &ih, sizeof(ih));
1197 if (!err)
1198 err = fuse_copy_one(cs, &arg, sizeof(arg));
1199
1200 while (head) {
1201 struct fuse_forget_link *forget = head;
1202
1203 if (!err) {
1204 err = fuse_copy_one(cs, &forget->forget_one,
1205 sizeof(forget->forget_one));
1206 }
1207 head = forget->next;
1208 kfree(forget);
1209 }
1210
1211 fuse_copy_finish(cs);
1212
1213 if (err)
1214 return err;
1215
1216 return ih.len;
1217}
1218
Miklos Szeredifd22d622015-07-01 16:26:03 +02001219static int fuse_read_forget(struct fuse_conn *fc, struct fuse_iqueue *fiq,
1220 struct fuse_copy_state *cs,
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001221 size_t nbytes)
Miklos Szeredifd22d622015-07-01 16:26:03 +02001222__releases(fiq->waitq.lock)
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001223{
Miklos Szeredif88996a2015-07-01 16:26:01 +02001224 if (fc->minor < 16 || fiq->forget_list_head.next->next == NULL)
Miklos Szeredifd22d622015-07-01 16:26:03 +02001225 return fuse_read_single_forget(fiq, cs, nbytes);
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001226 else
Miklos Szeredifd22d622015-07-01 16:26:03 +02001227 return fuse_read_batch_forget(fiq, cs, nbytes);
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001228}
1229
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001230/*
Miklos Szeredi334f4852005-09-09 13:10:27 -07001231 * Read a single request into the userspace filesystem's buffer. This
1232 * function waits until a request is available, then removes it from
1233 * the pending list and copies request data to userspace buffer. If
Miklos Szeredif9a28422006-06-25 05:48:53 -07001234 * no reply is needed (FORGET) or request has been aborted or there
1235 * was an error during the copying then it's finished by calling
Miklos Szeredi334f4852005-09-09 13:10:27 -07001236 * request_end(). Otherwise add it to the processing list, and set
1237 * the 'sent' flag.
1238 */
Miklos Szeredic36960462015-07-01 16:26:09 +02001239static ssize_t fuse_dev_do_read(struct fuse_dev *fud, struct file *file,
Miklos Szeredic3021622010-05-25 15:06:07 +02001240 struct fuse_copy_state *cs, size_t nbytes)
Miklos Szeredi334f4852005-09-09 13:10:27 -07001241{
Miklos Szeredi82cbdcd2015-07-01 16:26:05 +02001242 ssize_t err;
Miklos Szeredic36960462015-07-01 16:26:09 +02001243 struct fuse_conn *fc = fud->fc;
Miklos Szeredif88996a2015-07-01 16:26:01 +02001244 struct fuse_iqueue *fiq = &fc->iq;
Miklos Szeredic36960462015-07-01 16:26:09 +02001245 struct fuse_pqueue *fpq = &fud->pq;
Miklos Szeredi334f4852005-09-09 13:10:27 -07001246 struct fuse_req *req;
1247 struct fuse_in *in;
Miklos Szeredi334f4852005-09-09 13:10:27 -07001248 unsigned reqsize;
1249
Miklos Szeredi1d3d7522006-01-06 00:19:40 -08001250 restart:
Miklos Szeredi4ce60812015-07-01 16:26:02 +02001251 spin_lock(&fiq->waitq.lock);
Jeff Dikee5ac1d12006-04-10 22:54:53 -07001252 err = -EAGAIN;
Miklos Szeredie16714d2015-07-01 16:26:01 +02001253 if ((file->f_flags & O_NONBLOCK) && fiq->connected &&
Miklos Szeredif88996a2015-07-01 16:26:01 +02001254 !request_pending(fiq))
Jeff Dikee5ac1d12006-04-10 22:54:53 -07001255 goto err_unlock;
1256
Miklos Szeredi52509212015-07-01 16:26:03 +02001257 err = wait_event_interruptible_exclusive_locked(fiq->waitq,
1258 !fiq->connected || request_pending(fiq));
1259 if (err)
1260 goto err_unlock;
1261
Szymon Lukasz3b7008b2017-11-09 21:23:35 +01001262 if (!fiq->connected) {
1263 err = (fc->aborted && fc->abort_err) ? -ECONNABORTED : -ENODEV;
Miklos Szeredi334f4852005-09-09 13:10:27 -07001264 goto err_unlock;
Szymon Lukasz3b7008b2017-11-09 21:23:35 +01001265 }
Miklos Szeredi334f4852005-09-09 13:10:27 -07001266
Miklos Szeredif88996a2015-07-01 16:26:01 +02001267 if (!list_empty(&fiq->interrupts)) {
1268 req = list_entry(fiq->interrupts.next, struct fuse_req,
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001269 intr_entry);
Miklos Szeredifd22d622015-07-01 16:26:03 +02001270 return fuse_read_interrupt(fiq, cs, nbytes, req);
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001271 }
1272
Miklos Szeredif88996a2015-07-01 16:26:01 +02001273 if (forget_pending(fiq)) {
1274 if (list_empty(&fiq->pending) || fiq->forget_batch-- > 0)
Miklos Szeredifd22d622015-07-01 16:26:03 +02001275 return fuse_read_forget(fc, fiq, cs, nbytes);
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001276
Miklos Szeredif88996a2015-07-01 16:26:01 +02001277 if (fiq->forget_batch <= -8)
1278 fiq->forget_batch = 16;
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001279 }
1280
Miklos Szeredif88996a2015-07-01 16:26:01 +02001281 req = list_entry(fiq->pending.next, struct fuse_req, list);
Miklos Szeredi33e14b42015-07-01 16:26:01 +02001282 clear_bit(FR_PENDING, &req->flags);
Miklos Szeredief759252015-07-01 16:26:02 +02001283 list_del_init(&req->list);
Miklos Szeredi4ce60812015-07-01 16:26:02 +02001284 spin_unlock(&fiq->waitq.lock);
1285
Miklos Szeredi334f4852005-09-09 13:10:27 -07001286 in = &req->in;
Miklos Szeredi1d3d7522006-01-06 00:19:40 -08001287 reqsize = in->h.len;
Miklos Szeredi5d6d3a32017-09-12 16:57:53 +02001288
Miklos Szeredi1d3d7522006-01-06 00:19:40 -08001289 /* If request is too large, reply with an error and restart the read */
Miklos Szeredic3021622010-05-25 15:06:07 +02001290 if (nbytes < reqsize) {
Miklos Szeredi1d3d7522006-01-06 00:19:40 -08001291 req->out.h.error = -EIO;
1292 /* SETXATTR is special, since it may contain too large data */
1293 if (in->h.opcode == FUSE_SETXATTR)
1294 req->out.h.error = -E2BIG;
1295 request_end(fc, req);
1296 goto restart;
Miklos Szeredi334f4852005-09-09 13:10:27 -07001297 }
Miklos Szeredi45a91cb2015-07-01 16:26:06 +02001298 spin_lock(&fpq->lock);
Miklos Szeredi82cbdcd2015-07-01 16:26:05 +02001299 list_add(&req->list, &fpq->io);
Miklos Szeredi45a91cb2015-07-01 16:26:06 +02001300 spin_unlock(&fpq->lock);
Miklos Szeredic3021622010-05-25 15:06:07 +02001301 cs->req = req;
1302 err = fuse_copy_one(cs, &in->h, sizeof(in->h));
Miklos Szeredi1d3d7522006-01-06 00:19:40 -08001303 if (!err)
Miklos Szeredic3021622010-05-25 15:06:07 +02001304 err = fuse_copy_args(cs, in->numargs, in->argpages,
Miklos Szeredi1d3d7522006-01-06 00:19:40 -08001305 (struct fuse_arg *) in->args, 0);
Miklos Szeredic3021622010-05-25 15:06:07 +02001306 fuse_copy_finish(cs);
Miklos Szeredi45a91cb2015-07-01 16:26:06 +02001307 spin_lock(&fpq->lock);
Miklos Szeredi825d6d32015-07-01 16:25:58 +02001308 clear_bit(FR_LOCKED, &req->flags);
Miklos Szeredie96edd92015-07-01 16:26:04 +02001309 if (!fpq->connected) {
Szymon Lukasz3b7008b2017-11-09 21:23:35 +01001310 err = (fc->aborted && fc->abort_err) ? -ECONNABORTED : -ENODEV;
Miklos Szeredi82cbdcd2015-07-01 16:26:05 +02001311 goto out_end;
Miklos Szeredic9c9d7d2007-10-16 23:31:05 -07001312 }
Miklos Szeredi334f4852005-09-09 13:10:27 -07001313 if (err) {
Miklos Szeredic9c9d7d2007-10-16 23:31:05 -07001314 req->out.h.error = -EIO;
Miklos Szeredi82cbdcd2015-07-01 16:26:05 +02001315 goto out_end;
Miklos Szeredi334f4852005-09-09 13:10:27 -07001316 }
Miklos Szeredi825d6d32015-07-01 16:25:58 +02001317 if (!test_bit(FR_ISREPLY, &req->flags)) {
Miklos Szeredi82cbdcd2015-07-01 16:26:05 +02001318 err = reqsize;
1319 goto out_end;
Miklos Szeredi334f4852005-09-09 13:10:27 -07001320 }
Miklos Szeredi82cbdcd2015-07-01 16:26:05 +02001321 list_move_tail(&req->list, &fpq->processing);
Kirill Tkhaibc78abb2018-09-25 12:28:55 +03001322 __fuse_get_request(req);
Miklos Szeredi82cbdcd2015-07-01 16:26:05 +02001323 set_bit(FR_SENT, &req->flags);
Miklos Szeredi4c316f22018-09-28 16:43:22 +02001324 spin_unlock(&fpq->lock);
Miklos Szeredi82cbdcd2015-07-01 16:26:05 +02001325 /* matches barrier in request_wait_answer() */
1326 smp_mb__after_atomic();
1327 if (test_bit(FR_INTERRUPTED, &req->flags))
1328 queue_interrupt(fiq, req);
Kirill Tkhaibc78abb2018-09-25 12:28:55 +03001329 fuse_put_request(fc, req);
Miklos Szeredi82cbdcd2015-07-01 16:26:05 +02001330
Miklos Szeredi334f4852005-09-09 13:10:27 -07001331 return reqsize;
1332
Miklos Szeredi82cbdcd2015-07-01 16:26:05 +02001333out_end:
Miklos Szeredi77cd9d42015-07-01 16:26:06 +02001334 if (!test_bit(FR_PRIVATE, &req->flags))
1335 list_del_init(&req->list);
Miklos Szeredi45a91cb2015-07-01 16:26:06 +02001336 spin_unlock(&fpq->lock);
Miklos Szeredi82cbdcd2015-07-01 16:26:05 +02001337 request_end(fc, req);
1338 return err;
1339
Miklos Szeredi334f4852005-09-09 13:10:27 -07001340 err_unlock:
Miklos Szeredi4ce60812015-07-01 16:26:02 +02001341 spin_unlock(&fiq->waitq.lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001342 return err;
1343}
1344
Tom Van Braeckel94e4fe22015-01-12 05:22:16 +01001345static int fuse_dev_open(struct inode *inode, struct file *file)
1346{
1347 /*
1348 * The fuse device's file's private_data is used to hold
1349 * the fuse_conn(ection) when it is mounted, and is used to
1350 * keep track of whether the file has been mounted already.
1351 */
1352 file->private_data = NULL;
1353 return 0;
1354}
1355
Al Virofbdbacc2015-04-03 21:53:39 -04001356static ssize_t fuse_dev_read(struct kiocb *iocb, struct iov_iter *to)
Miklos Szeredic3021622010-05-25 15:06:07 +02001357{
1358 struct fuse_copy_state cs;
1359 struct file *file = iocb->ki_filp;
Miklos Szeredicc080e92015-07-01 16:26:08 +02001360 struct fuse_dev *fud = fuse_get_dev(file);
1361
1362 if (!fud)
Miklos Szeredic3021622010-05-25 15:06:07 +02001363 return -EPERM;
1364
Al Virofbdbacc2015-04-03 21:53:39 -04001365 if (!iter_is_iovec(to))
1366 return -EINVAL;
Miklos Szeredic3021622010-05-25 15:06:07 +02001367
Miklos Szeredidc008092015-07-01 16:25:58 +02001368 fuse_copy_init(&cs, 1, to);
Al Virofbdbacc2015-04-03 21:53:39 -04001369
Miklos Szeredic36960462015-07-01 16:26:09 +02001370 return fuse_dev_do_read(fud, file, &cs, iov_iter_count(to));
Miklos Szeredic3021622010-05-25 15:06:07 +02001371}
1372
Miklos Szeredic3021622010-05-25 15:06:07 +02001373static ssize_t fuse_dev_splice_read(struct file *in, loff_t *ppos,
1374 struct pipe_inode_info *pipe,
1375 size_t len, unsigned int flags)
1376{
Al Virod82718e2016-09-17 22:56:25 -04001377 int total, ret;
Miklos Szeredic3021622010-05-25 15:06:07 +02001378 int page_nr = 0;
Miklos Szeredic3021622010-05-25 15:06:07 +02001379 struct pipe_buffer *bufs;
1380 struct fuse_copy_state cs;
Miklos Szeredicc080e92015-07-01 16:26:08 +02001381 struct fuse_dev *fud = fuse_get_dev(in);
1382
1383 if (!fud)
Miklos Szeredic3021622010-05-25 15:06:07 +02001384 return -EPERM;
1385
Andrey Ryabinind6d931a2018-07-17 19:00:34 +03001386 bufs = kvmalloc_array(pipe->buffers, sizeof(struct pipe_buffer),
1387 GFP_KERNEL);
Miklos Szeredic3021622010-05-25 15:06:07 +02001388 if (!bufs)
1389 return -ENOMEM;
1390
Miklos Szeredidc008092015-07-01 16:25:58 +02001391 fuse_copy_init(&cs, 1, NULL);
Miklos Szeredic3021622010-05-25 15:06:07 +02001392 cs.pipebufs = bufs;
1393 cs.pipe = pipe;
Miklos Szeredic36960462015-07-01 16:26:09 +02001394 ret = fuse_dev_do_read(fud, in, &cs, len);
Miklos Szeredic3021622010-05-25 15:06:07 +02001395 if (ret < 0)
1396 goto out;
1397
Miklos Szeredic3021622010-05-25 15:06:07 +02001398 if (pipe->nrbufs + cs.nr_segs > pipe->buffers) {
1399 ret = -EIO;
Al Virod82718e2016-09-17 22:56:25 -04001400 goto out;
Miklos Szeredic3021622010-05-25 15:06:07 +02001401 }
1402
Al Virod82718e2016-09-17 22:56:25 -04001403 for (ret = total = 0; page_nr < cs.nr_segs; total += ret) {
Miklos Szeredi28a625c2014-01-22 19:36:57 +01001404 /*
1405 * Need to be careful about this. Having buf->ops in module
1406 * code can Oops if the buffer persists after module unload.
1407 */
Al Virod82718e2016-09-17 22:56:25 -04001408 bufs[page_nr].ops = &nosteal_pipe_buf_ops;
Miklos Szeredi84588a92017-02-16 15:08:20 +01001409 bufs[page_nr].flags = 0;
Al Virod82718e2016-09-17 22:56:25 -04001410 ret = add_to_pipe(pipe, &bufs[page_nr++]);
1411 if (unlikely(ret < 0))
1412 break;
Miklos Szeredic3021622010-05-25 15:06:07 +02001413 }
Al Virod82718e2016-09-17 22:56:25 -04001414 if (total)
1415 ret = total;
Miklos Szeredic3021622010-05-25 15:06:07 +02001416out:
1417 for (; page_nr < cs.nr_segs; page_nr++)
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001418 put_page(bufs[page_nr].page);
Miklos Szeredic3021622010-05-25 15:06:07 +02001419
Andrey Ryabinind6d931a2018-07-17 19:00:34 +03001420 kvfree(bufs);
Miklos Szeredic3021622010-05-25 15:06:07 +02001421 return ret;
1422}
1423
Tejun Heo95668a62008-11-26 12:03:55 +01001424static int fuse_notify_poll(struct fuse_conn *fc, unsigned int size,
1425 struct fuse_copy_state *cs)
1426{
1427 struct fuse_notify_poll_wakeup_out outarg;
Miklos Szeredif6d47a12009-01-26 15:00:59 +01001428 int err = -EINVAL;
Tejun Heo95668a62008-11-26 12:03:55 +01001429
1430 if (size != sizeof(outarg))
Miklos Szeredif6d47a12009-01-26 15:00:59 +01001431 goto err;
Tejun Heo95668a62008-11-26 12:03:55 +01001432
1433 err = fuse_copy_one(cs, &outarg, sizeof(outarg));
1434 if (err)
Miklos Szeredif6d47a12009-01-26 15:00:59 +01001435 goto err;
Tejun Heo95668a62008-11-26 12:03:55 +01001436
Miklos Szeredif6d47a12009-01-26 15:00:59 +01001437 fuse_copy_finish(cs);
Tejun Heo95668a62008-11-26 12:03:55 +01001438 return fuse_notify_poll_wakeup(fc, &outarg);
Miklos Szeredif6d47a12009-01-26 15:00:59 +01001439
1440err:
1441 fuse_copy_finish(cs);
1442 return err;
Tejun Heo95668a62008-11-26 12:03:55 +01001443}
1444
John Muir3b463ae2009-05-31 11:13:57 -04001445static int fuse_notify_inval_inode(struct fuse_conn *fc, unsigned int size,
1446 struct fuse_copy_state *cs)
1447{
1448 struct fuse_notify_inval_inode_out outarg;
1449 int err = -EINVAL;
1450
1451 if (size != sizeof(outarg))
1452 goto err;
1453
1454 err = fuse_copy_one(cs, &outarg, sizeof(outarg));
1455 if (err)
1456 goto err;
1457 fuse_copy_finish(cs);
1458
1459 down_read(&fc->killsb);
1460 err = -ENOENT;
Miklos Szeredib21dda42010-02-05 12:08:31 +01001461 if (fc->sb) {
1462 err = fuse_reverse_inval_inode(fc->sb, outarg.ino,
1463 outarg.off, outarg.len);
1464 }
John Muir3b463ae2009-05-31 11:13:57 -04001465 up_read(&fc->killsb);
1466 return err;
1467
1468err:
1469 fuse_copy_finish(cs);
1470 return err;
1471}
1472
1473static int fuse_notify_inval_entry(struct fuse_conn *fc, unsigned int size,
1474 struct fuse_copy_state *cs)
1475{
1476 struct fuse_notify_inval_entry_out outarg;
Fang Wenqib2d82ee2009-12-30 18:37:13 +08001477 int err = -ENOMEM;
1478 char *buf;
John Muir3b463ae2009-05-31 11:13:57 -04001479 struct qstr name;
1480
Fang Wenqib2d82ee2009-12-30 18:37:13 +08001481 buf = kzalloc(FUSE_NAME_MAX + 1, GFP_KERNEL);
1482 if (!buf)
1483 goto err;
1484
1485 err = -EINVAL;
John Muir3b463ae2009-05-31 11:13:57 -04001486 if (size < sizeof(outarg))
1487 goto err;
1488
1489 err = fuse_copy_one(cs, &outarg, sizeof(outarg));
1490 if (err)
1491 goto err;
1492
1493 err = -ENAMETOOLONG;
1494 if (outarg.namelen > FUSE_NAME_MAX)
1495 goto err;
1496
Miklos Szeredic2183d12011-08-24 10:20:17 +02001497 err = -EINVAL;
1498 if (size != sizeof(outarg) + outarg.namelen + 1)
1499 goto err;
1500
John Muir3b463ae2009-05-31 11:13:57 -04001501 name.name = buf;
1502 name.len = outarg.namelen;
1503 err = fuse_copy_one(cs, buf, outarg.namelen + 1);
1504 if (err)
1505 goto err;
1506 fuse_copy_finish(cs);
1507 buf[outarg.namelen] = 0;
John Muir3b463ae2009-05-31 11:13:57 -04001508
1509 down_read(&fc->killsb);
1510 err = -ENOENT;
Miklos Szeredib21dda42010-02-05 12:08:31 +01001511 if (fc->sb)
John Muir451d0f52011-12-06 21:50:06 +01001512 err = fuse_reverse_inval_entry(fc->sb, outarg.parent, 0, &name);
1513 up_read(&fc->killsb);
1514 kfree(buf);
1515 return err;
1516
1517err:
1518 kfree(buf);
1519 fuse_copy_finish(cs);
1520 return err;
1521}
1522
1523static int fuse_notify_delete(struct fuse_conn *fc, unsigned int size,
1524 struct fuse_copy_state *cs)
1525{
1526 struct fuse_notify_delete_out outarg;
1527 int err = -ENOMEM;
1528 char *buf;
1529 struct qstr name;
1530
1531 buf = kzalloc(FUSE_NAME_MAX + 1, GFP_KERNEL);
1532 if (!buf)
1533 goto err;
1534
1535 err = -EINVAL;
1536 if (size < sizeof(outarg))
1537 goto err;
1538
1539 err = fuse_copy_one(cs, &outarg, sizeof(outarg));
1540 if (err)
1541 goto err;
1542
1543 err = -ENAMETOOLONG;
1544 if (outarg.namelen > FUSE_NAME_MAX)
1545 goto err;
1546
1547 err = -EINVAL;
1548 if (size != sizeof(outarg) + outarg.namelen + 1)
1549 goto err;
1550
1551 name.name = buf;
1552 name.len = outarg.namelen;
1553 err = fuse_copy_one(cs, buf, outarg.namelen + 1);
1554 if (err)
1555 goto err;
1556 fuse_copy_finish(cs);
1557 buf[outarg.namelen] = 0;
John Muir451d0f52011-12-06 21:50:06 +01001558
1559 down_read(&fc->killsb);
1560 err = -ENOENT;
1561 if (fc->sb)
1562 err = fuse_reverse_inval_entry(fc->sb, outarg.parent,
1563 outarg.child, &name);
John Muir3b463ae2009-05-31 11:13:57 -04001564 up_read(&fc->killsb);
Fang Wenqib2d82ee2009-12-30 18:37:13 +08001565 kfree(buf);
John Muir3b463ae2009-05-31 11:13:57 -04001566 return err;
1567
1568err:
Fang Wenqib2d82ee2009-12-30 18:37:13 +08001569 kfree(buf);
John Muir3b463ae2009-05-31 11:13:57 -04001570 fuse_copy_finish(cs);
1571 return err;
1572}
1573
Miklos Szeredia1d75f22010-07-12 14:41:40 +02001574static int fuse_notify_store(struct fuse_conn *fc, unsigned int size,
1575 struct fuse_copy_state *cs)
1576{
1577 struct fuse_notify_store_out outarg;
1578 struct inode *inode;
1579 struct address_space *mapping;
1580 u64 nodeid;
1581 int err;
1582 pgoff_t index;
1583 unsigned int offset;
1584 unsigned int num;
1585 loff_t file_size;
1586 loff_t end;
1587
1588 err = -EINVAL;
1589 if (size < sizeof(outarg))
1590 goto out_finish;
1591
1592 err = fuse_copy_one(cs, &outarg, sizeof(outarg));
1593 if (err)
1594 goto out_finish;
1595
1596 err = -EINVAL;
1597 if (size - sizeof(outarg) != outarg.size)
1598 goto out_finish;
1599
1600 nodeid = outarg.nodeid;
1601
1602 down_read(&fc->killsb);
1603
1604 err = -ENOENT;
1605 if (!fc->sb)
1606 goto out_up_killsb;
1607
1608 inode = ilookup5(fc->sb, nodeid, fuse_inode_eq, &nodeid);
1609 if (!inode)
1610 goto out_up_killsb;
1611
1612 mapping = inode->i_mapping;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001613 index = outarg.offset >> PAGE_SHIFT;
1614 offset = outarg.offset & ~PAGE_MASK;
Miklos Szeredia1d75f22010-07-12 14:41:40 +02001615 file_size = i_size_read(inode);
1616 end = outarg.offset + outarg.size;
1617 if (end > file_size) {
1618 file_size = end;
1619 fuse_write_update_size(inode, file_size);
1620 }
1621
1622 num = outarg.size;
1623 while (num) {
1624 struct page *page;
1625 unsigned int this_num;
1626
1627 err = -ENOMEM;
1628 page = find_or_create_page(mapping, index,
1629 mapping_gfp_mask(mapping));
1630 if (!page)
1631 goto out_iput;
1632
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001633 this_num = min_t(unsigned, num, PAGE_SIZE - offset);
Miklos Szeredia1d75f22010-07-12 14:41:40 +02001634 err = fuse_copy_page(cs, &page, offset, this_num, 0);
Miklos Szeredi063ec1e2014-01-22 19:36:58 +01001635 if (!err && offset == 0 &&
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001636 (this_num == PAGE_SIZE || file_size == end))
Miklos Szeredia1d75f22010-07-12 14:41:40 +02001637 SetPageUptodate(page);
1638 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001639 put_page(page);
Miklos Szeredia1d75f22010-07-12 14:41:40 +02001640
1641 if (err)
1642 goto out_iput;
1643
1644 num -= this_num;
1645 offset = 0;
1646 index++;
1647 }
1648
1649 err = 0;
1650
1651out_iput:
1652 iput(inode);
1653out_up_killsb:
1654 up_read(&fc->killsb);
1655out_finish:
1656 fuse_copy_finish(cs);
1657 return err;
1658}
1659
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001660static void fuse_retrieve_end(struct fuse_conn *fc, struct fuse_req *req)
1661{
Mel Gormanc6f92f92017-11-15 17:37:55 -08001662 release_pages(req->pages, req->num_pages);
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001663}
1664
1665static int fuse_retrieve(struct fuse_conn *fc, struct inode *inode,
1666 struct fuse_notify_retrieve_out *outarg)
1667{
1668 int err;
1669 struct address_space *mapping = inode->i_mapping;
1670 struct fuse_req *req;
1671 pgoff_t index;
1672 loff_t file_size;
1673 unsigned int num;
1674 unsigned int offset;
Geert Uytterhoeven01574432010-09-30 22:06:21 +02001675 size_t total_len = 0;
Maxim Patlasov4d53dc92012-10-26 19:48:42 +04001676 int num_pages;
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001677
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001678 offset = outarg->offset & ~PAGE_MASK;
Maxim Patlasov4d53dc92012-10-26 19:48:42 +04001679 file_size = i_size_read(inode);
1680
1681 num = outarg->size;
1682 if (outarg->offset > file_size)
1683 num = 0;
1684 else if (outarg->offset + num > file_size)
1685 num = file_size - outarg->offset;
1686
1687 num_pages = (num + offset + PAGE_SIZE - 1) >> PAGE_SHIFT;
1688 num_pages = min(num_pages, FUSE_MAX_PAGES_PER_REQ);
1689
1690 req = fuse_get_req(fc, num_pages);
1691 if (IS_ERR(req))
1692 return PTR_ERR(req);
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001693
1694 req->in.h.opcode = FUSE_NOTIFY_REPLY;
1695 req->in.h.nodeid = outarg->nodeid;
1696 req->in.numargs = 2;
1697 req->in.argpages = 1;
Maxim Patlasovb2430d72012-10-26 19:49:24 +04001698 req->page_descs[0].offset = offset;
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001699 req->end = fuse_retrieve_end;
1700
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001701 index = outarg->offset >> PAGE_SHIFT;
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001702
Maxim Patlasov4d53dc92012-10-26 19:48:42 +04001703 while (num && req->num_pages < num_pages) {
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001704 struct page *page;
1705 unsigned int this_num;
1706
1707 page = find_get_page(mapping, index);
1708 if (!page)
1709 break;
1710
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001711 this_num = min_t(unsigned, num, PAGE_SIZE - offset);
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001712 req->pages[req->num_pages] = page;
Maxim Patlasov85f40ae2012-10-26 19:49:33 +04001713 req->page_descs[req->num_pages].length = this_num;
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001714 req->num_pages++;
1715
Miklos Szeredic9e67d42012-09-04 18:45:54 +02001716 offset = 0;
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001717 num -= this_num;
1718 total_len += this_num;
Miklos Szeredi48706d02011-12-13 10:36:59 +01001719 index++;
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001720 }
1721 req->misc.retrieve_in.offset = outarg->offset;
1722 req->misc.retrieve_in.size = total_len;
1723 req->in.args[0].size = sizeof(req->misc.retrieve_in);
1724 req->in.args[0].value = &req->misc.retrieve_in;
1725 req->in.args[1].size = total_len;
1726
1727 err = fuse_request_send_notify_reply(fc, req, outarg->notify_unique);
1728 if (err)
1729 fuse_retrieve_end(fc, req);
1730
1731 return err;
1732}
1733
1734static int fuse_notify_retrieve(struct fuse_conn *fc, unsigned int size,
1735 struct fuse_copy_state *cs)
1736{
1737 struct fuse_notify_retrieve_out outarg;
1738 struct inode *inode;
1739 int err;
1740
1741 err = -EINVAL;
1742 if (size != sizeof(outarg))
1743 goto copy_finish;
1744
1745 err = fuse_copy_one(cs, &outarg, sizeof(outarg));
1746 if (err)
1747 goto copy_finish;
1748
1749 fuse_copy_finish(cs);
1750
1751 down_read(&fc->killsb);
1752 err = -ENOENT;
1753 if (fc->sb) {
1754 u64 nodeid = outarg.nodeid;
1755
1756 inode = ilookup5(fc->sb, nodeid, fuse_inode_eq, &nodeid);
1757 if (inode) {
1758 err = fuse_retrieve(fc, inode, &outarg);
1759 iput(inode);
1760 }
1761 }
1762 up_read(&fc->killsb);
1763
1764 return err;
1765
1766copy_finish:
1767 fuse_copy_finish(cs);
1768 return err;
1769}
1770
Tejun Heo85993962008-11-26 12:03:55 +01001771static int fuse_notify(struct fuse_conn *fc, enum fuse_notify_code code,
1772 unsigned int size, struct fuse_copy_state *cs)
1773{
Miklos Szeredi0d278362015-02-26 11:45:47 +01001774 /* Don't try to move pages (yet) */
1775 cs->move_pages = 0;
1776
Tejun Heo85993962008-11-26 12:03:55 +01001777 switch (code) {
Tejun Heo95668a62008-11-26 12:03:55 +01001778 case FUSE_NOTIFY_POLL:
1779 return fuse_notify_poll(fc, size, cs);
1780
John Muir3b463ae2009-05-31 11:13:57 -04001781 case FUSE_NOTIFY_INVAL_INODE:
1782 return fuse_notify_inval_inode(fc, size, cs);
1783
1784 case FUSE_NOTIFY_INVAL_ENTRY:
1785 return fuse_notify_inval_entry(fc, size, cs);
1786
Miklos Szeredia1d75f22010-07-12 14:41:40 +02001787 case FUSE_NOTIFY_STORE:
1788 return fuse_notify_store(fc, size, cs);
1789
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001790 case FUSE_NOTIFY_RETRIEVE:
1791 return fuse_notify_retrieve(fc, size, cs);
1792
John Muir451d0f52011-12-06 21:50:06 +01001793 case FUSE_NOTIFY_DELETE:
1794 return fuse_notify_delete(fc, size, cs);
1795
Tejun Heo85993962008-11-26 12:03:55 +01001796 default:
Miklos Szeredif6d47a12009-01-26 15:00:59 +01001797 fuse_copy_finish(cs);
Tejun Heo85993962008-11-26 12:03:55 +01001798 return -EINVAL;
1799 }
1800}
1801
Miklos Szeredi334f4852005-09-09 13:10:27 -07001802/* Look up request on processing list by unique ID */
Miklos Szeredi3a2b5b92015-07-01 16:26:04 +02001803static struct fuse_req *request_find(struct fuse_pqueue *fpq, u64 unique)
Miklos Szeredi334f4852005-09-09 13:10:27 -07001804{
Dong Fang05726ac2013-07-30 22:50:01 -04001805 struct fuse_req *req;
Miklos Szeredi334f4852005-09-09 13:10:27 -07001806
Miklos Szeredi3a2b5b92015-07-01 16:26:04 +02001807 list_for_each_entry(req, &fpq->processing, list) {
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001808 if (req->in.h.unique == unique || req->intr_unique == unique)
Miklos Szeredi334f4852005-09-09 13:10:27 -07001809 return req;
1810 }
1811 return NULL;
1812}
1813
1814static int copy_out_args(struct fuse_copy_state *cs, struct fuse_out *out,
1815 unsigned nbytes)
1816{
1817 unsigned reqsize = sizeof(struct fuse_out_header);
1818
1819 if (out->h.error)
1820 return nbytes != reqsize ? -EINVAL : 0;
1821
1822 reqsize += len_args(out->numargs, out->args);
1823
1824 if (reqsize < nbytes || (reqsize > nbytes && !out->argvar))
1825 return -EINVAL;
1826 else if (reqsize > nbytes) {
1827 struct fuse_arg *lastarg = &out->args[out->numargs-1];
1828 unsigned diffsize = reqsize - nbytes;
1829 if (diffsize > lastarg->size)
1830 return -EINVAL;
1831 lastarg->size -= diffsize;
1832 }
1833 return fuse_copy_args(cs, out->numargs, out->argpages, out->args,
1834 out->page_zeroing);
1835}
1836
1837/*
1838 * Write a single reply to a request. First the header is copied from
1839 * the write buffer. The request is then searched on the processing
1840 * list by the unique ID found in the header. If found, then remove
1841 * it from the list and copy the rest of the buffer to the request.
1842 * The request is finished by calling request_end()
1843 */
Miklos Szeredic36960462015-07-01 16:26:09 +02001844static ssize_t fuse_dev_do_write(struct fuse_dev *fud,
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001845 struct fuse_copy_state *cs, size_t nbytes)
Miklos Szeredi334f4852005-09-09 13:10:27 -07001846{
1847 int err;
Miklos Szeredic36960462015-07-01 16:26:09 +02001848 struct fuse_conn *fc = fud->fc;
1849 struct fuse_pqueue *fpq = &fud->pq;
Miklos Szeredi334f4852005-09-09 13:10:27 -07001850 struct fuse_req *req;
1851 struct fuse_out_header oh;
Miklos Szeredi334f4852005-09-09 13:10:27 -07001852
Miklos Szeredi334f4852005-09-09 13:10:27 -07001853 if (nbytes < sizeof(struct fuse_out_header))
1854 return -EINVAL;
1855
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001856 err = fuse_copy_one(cs, &oh, sizeof(oh));
Miklos Szeredi334f4852005-09-09 13:10:27 -07001857 if (err)
1858 goto err_finish;
Tejun Heo85993962008-11-26 12:03:55 +01001859
Miklos Szeredi334f4852005-09-09 13:10:27 -07001860 err = -EINVAL;
Tejun Heo85993962008-11-26 12:03:55 +01001861 if (oh.len != nbytes)
1862 goto err_finish;
1863
1864 /*
1865 * Zero oh.unique indicates unsolicited notification message
1866 * and error contains notification code.
1867 */
1868 if (!oh.unique) {
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001869 err = fuse_notify(fc, oh.error, nbytes - sizeof(oh), cs);
Tejun Heo85993962008-11-26 12:03:55 +01001870 return err ? err : nbytes;
1871 }
1872
1873 err = -EINVAL;
1874 if (oh.error <= -1000 || oh.error > 0)
Miklos Szeredi334f4852005-09-09 13:10:27 -07001875 goto err_finish;
1876
Miklos Szeredi45a91cb2015-07-01 16:26:06 +02001877 spin_lock(&fpq->lock);
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08001878 err = -ENOENT;
Miklos Szeredie96edd92015-07-01 16:26:04 +02001879 if (!fpq->connected)
Miklos Szeredi45a91cb2015-07-01 16:26:06 +02001880 goto err_unlock_pq;
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08001881
Miklos Szeredi3a2b5b92015-07-01 16:26:04 +02001882 req = request_find(fpq, oh.unique);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001883 if (!req)
Miklos Szeredi45a91cb2015-07-01 16:26:06 +02001884 goto err_unlock_pq;
Miklos Szeredi334f4852005-09-09 13:10:27 -07001885
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001886 /* Is it an interrupt reply? */
1887 if (req->intr_unique == oh.unique) {
Kirill Tkhaid2d2d4f2018-09-25 12:52:42 +03001888 __fuse_get_request(req);
Miklos Szeredi45a91cb2015-07-01 16:26:06 +02001889 spin_unlock(&fpq->lock);
1890
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001891 err = -EINVAL;
Kirill Tkhaid2d2d4f2018-09-25 12:52:42 +03001892 if (nbytes != sizeof(struct fuse_out_header)) {
1893 fuse_put_request(fc, req);
Miklos Szeredi46c34a32015-07-01 16:26:07 +02001894 goto err_finish;
Kirill Tkhaid2d2d4f2018-09-25 12:52:42 +03001895 }
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001896
1897 if (oh.error == -ENOSYS)
1898 fc->no_interrupt = 1;
1899 else if (oh.error == -EAGAIN)
Miklos Szeredif88996a2015-07-01 16:26:01 +02001900 queue_interrupt(&fc->iq, req);
Kirill Tkhaid2d2d4f2018-09-25 12:52:42 +03001901 fuse_put_request(fc, req);
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001902
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001903 fuse_copy_finish(cs);
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001904 return nbytes;
1905 }
1906
Miklos Szeredi33e14b42015-07-01 16:26:01 +02001907 clear_bit(FR_SENT, &req->flags);
Miklos Szeredi3a2b5b92015-07-01 16:26:04 +02001908 list_move(&req->list, &fpq->io);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001909 req->out.h = oh;
Miklos Szeredi825d6d32015-07-01 16:25:58 +02001910 set_bit(FR_LOCKED, &req->flags);
Miklos Szeredi45a91cb2015-07-01 16:26:06 +02001911 spin_unlock(&fpq->lock);
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001912 cs->req = req;
Miklos Szeredice534fb2010-05-25 15:06:07 +02001913 if (!req->out.page_replace)
1914 cs->move_pages = 0;
Miklos Szeredi334f4852005-09-09 13:10:27 -07001915
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001916 err = copy_out_args(cs, &req->out, nbytes);
1917 fuse_copy_finish(cs);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001918
Miklos Szeredi45a91cb2015-07-01 16:26:06 +02001919 spin_lock(&fpq->lock);
Miklos Szeredi825d6d32015-07-01 16:25:58 +02001920 clear_bit(FR_LOCKED, &req->flags);
Miklos Szeredie96edd92015-07-01 16:26:04 +02001921 if (!fpq->connected)
Miklos Szeredi0d8e84b2015-07-01 16:25:58 +02001922 err = -ENOENT;
1923 else if (err)
Miklos Szeredi334f4852005-09-09 13:10:27 -07001924 req->out.h.error = -EIO;
Miklos Szeredi77cd9d42015-07-01 16:26:06 +02001925 if (!test_bit(FR_PRIVATE, &req->flags))
1926 list_del_init(&req->list);
Miklos Szeredi45a91cb2015-07-01 16:26:06 +02001927 spin_unlock(&fpq->lock);
Miklos Szeredi46c34a32015-07-01 16:26:07 +02001928
Miklos Szeredi334f4852005-09-09 13:10:27 -07001929 request_end(fc, req);
1930
1931 return err ? err : nbytes;
1932
Miklos Szeredi45a91cb2015-07-01 16:26:06 +02001933 err_unlock_pq:
1934 spin_unlock(&fpq->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001935 err_finish:
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001936 fuse_copy_finish(cs);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001937 return err;
1938}
1939
Al Virofbdbacc2015-04-03 21:53:39 -04001940static ssize_t fuse_dev_write(struct kiocb *iocb, struct iov_iter *from)
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001941{
1942 struct fuse_copy_state cs;
Miklos Szeredicc080e92015-07-01 16:26:08 +02001943 struct fuse_dev *fud = fuse_get_dev(iocb->ki_filp);
1944
1945 if (!fud)
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001946 return -EPERM;
1947
Al Virofbdbacc2015-04-03 21:53:39 -04001948 if (!iter_is_iovec(from))
1949 return -EINVAL;
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001950
Miklos Szeredidc008092015-07-01 16:25:58 +02001951 fuse_copy_init(&cs, 0, from);
Al Virofbdbacc2015-04-03 21:53:39 -04001952
Miklos Szeredic36960462015-07-01 16:26:09 +02001953 return fuse_dev_do_write(fud, &cs, iov_iter_count(from));
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001954}
1955
1956static ssize_t fuse_dev_splice_write(struct pipe_inode_info *pipe,
1957 struct file *out, loff_t *ppos,
1958 size_t len, unsigned int flags)
1959{
1960 unsigned nbuf;
1961 unsigned idx;
1962 struct pipe_buffer *bufs;
1963 struct fuse_copy_state cs;
Miklos Szeredicc080e92015-07-01 16:26:08 +02001964 struct fuse_dev *fud;
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001965 size_t rem;
1966 ssize_t ret;
1967
Miklos Szeredicc080e92015-07-01 16:26:08 +02001968 fud = fuse_get_dev(out);
1969 if (!fud)
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001970 return -EPERM;
1971
Andrey Ryabinina2477b02018-07-17 19:00:33 +03001972 pipe_lock(pipe);
1973
Andrey Ryabinin96354532018-07-17 19:00:35 +03001974 bufs = kvmalloc_array(pipe->nrbufs, sizeof(struct pipe_buffer),
Andrey Ryabinind6d931a2018-07-17 19:00:34 +03001975 GFP_KERNEL);
Andrey Ryabinina2477b02018-07-17 19:00:33 +03001976 if (!bufs) {
1977 pipe_unlock(pipe);
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001978 return -ENOMEM;
Andrey Ryabinina2477b02018-07-17 19:00:33 +03001979 }
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001980
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001981 nbuf = 0;
1982 rem = 0;
1983 for (idx = 0; idx < pipe->nrbufs && rem < len; idx++)
1984 rem += pipe->bufs[(pipe->curbuf + idx) & (pipe->buffers - 1)].len;
1985
1986 ret = -EINVAL;
1987 if (rem < len) {
1988 pipe_unlock(pipe);
1989 goto out;
1990 }
1991
1992 rem = len;
1993 while (rem) {
1994 struct pipe_buffer *ibuf;
1995 struct pipe_buffer *obuf;
1996
1997 BUG_ON(nbuf >= pipe->buffers);
1998 BUG_ON(!pipe->nrbufs);
1999 ibuf = &pipe->bufs[pipe->curbuf];
2000 obuf = &bufs[nbuf];
2001
2002 if (rem >= ibuf->len) {
2003 *obuf = *ibuf;
2004 ibuf->ops = NULL;
2005 pipe->curbuf = (pipe->curbuf + 1) & (pipe->buffers - 1);
2006 pipe->nrbufs--;
2007 } else {
Miklos Szeredi7bf2d1d2016-09-27 10:45:12 +02002008 pipe_buf_get(pipe, ibuf);
Miklos Szeredidd3bb142010-05-25 15:06:06 +02002009 *obuf = *ibuf;
2010 obuf->flags &= ~PIPE_BUF_FLAG_GIFT;
2011 obuf->len = rem;
2012 ibuf->offset += obuf->len;
2013 ibuf->len -= obuf->len;
2014 }
2015 nbuf++;
2016 rem -= obuf->len;
2017 }
2018 pipe_unlock(pipe);
2019
Miklos Szeredidc008092015-07-01 16:25:58 +02002020 fuse_copy_init(&cs, 0, NULL);
Miklos Szeredidd3bb142010-05-25 15:06:06 +02002021 cs.pipebufs = bufs;
Al Viro6c09e942015-04-03 22:06:08 -04002022 cs.nr_segs = nbuf;
Miklos Szeredidd3bb142010-05-25 15:06:06 +02002023 cs.pipe = pipe;
2024
Miklos Szeredice534fb2010-05-25 15:06:07 +02002025 if (flags & SPLICE_F_MOVE)
2026 cs.move_pages = 1;
2027
Miklos Szeredic36960462015-07-01 16:26:09 +02002028 ret = fuse_dev_do_write(fud, &cs, len);
Miklos Szeredidd3bb142010-05-25 15:06:06 +02002029
Miklos Szeredia7796382016-09-27 10:45:12 +02002030 for (idx = 0; idx < nbuf; idx++)
2031 pipe_buf_release(pipe, &bufs[idx]);
2032
Miklos Szeredidd3bb142010-05-25 15:06:06 +02002033out:
Andrey Ryabinind6d931a2018-07-17 19:00:34 +03002034 kvfree(bufs);
Miklos Szeredidd3bb142010-05-25 15:06:06 +02002035 return ret;
2036}
2037
Al Viro076ccb72017-07-03 01:02:18 -04002038static __poll_t fuse_dev_poll(struct file *file, poll_table *wait)
Miklos Szeredi334f4852005-09-09 13:10:27 -07002039{
Linus Torvaldsa9a08842018-02-11 14:34:03 -08002040 __poll_t mask = EPOLLOUT | EPOLLWRNORM;
Miklos Szeredif88996a2015-07-01 16:26:01 +02002041 struct fuse_iqueue *fiq;
Miklos Szeredicc080e92015-07-01 16:26:08 +02002042 struct fuse_dev *fud = fuse_get_dev(file);
2043
2044 if (!fud)
Linus Torvaldsa9a08842018-02-11 14:34:03 -08002045 return EPOLLERR;
Miklos Szeredi334f4852005-09-09 13:10:27 -07002046
Miklos Szeredicc080e92015-07-01 16:26:08 +02002047 fiq = &fud->fc->iq;
Miklos Szeredif88996a2015-07-01 16:26:01 +02002048 poll_wait(file, &fiq->waitq, wait);
Miklos Szeredi334f4852005-09-09 13:10:27 -07002049
Miklos Szeredi4ce60812015-07-01 16:26:02 +02002050 spin_lock(&fiq->waitq.lock);
Miklos Szeredie16714d2015-07-01 16:26:01 +02002051 if (!fiq->connected)
Linus Torvaldsa9a08842018-02-11 14:34:03 -08002052 mask = EPOLLERR;
Miklos Szeredif88996a2015-07-01 16:26:01 +02002053 else if (request_pending(fiq))
Linus Torvaldsa9a08842018-02-11 14:34:03 -08002054 mask |= EPOLLIN | EPOLLRDNORM;
Miklos Szeredi4ce60812015-07-01 16:26:02 +02002055 spin_unlock(&fiq->waitq.lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -07002056
2057 return mask;
2058}
2059
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002060/*
2061 * Abort all requests on the given list (pending or processing)
2062 *
Miklos Szeredid7133112006-04-10 22:54:55 -07002063 * This function releases and reacquires fc->lock
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002064 */
Miklos Szeredi334f4852005-09-09 13:10:27 -07002065static void end_requests(struct fuse_conn *fc, struct list_head *head)
2066{
2067 while (!list_empty(head)) {
2068 struct fuse_req *req;
2069 req = list_entry(head->next, struct fuse_req, list);
Miklos Szeredi334f4852005-09-09 13:10:27 -07002070 req->out.h.error = -ECONNABORTED;
Miklos Szeredi33e14b42015-07-01 16:26:01 +02002071 clear_bit(FR_SENT, &req->flags);
Miklos Szeredif377cb72015-07-01 16:26:04 +02002072 list_del_init(&req->list);
Miklos Szeredi334f4852005-09-09 13:10:27 -07002073 request_end(fc, req);
Miklos Szeredi334f4852005-09-09 13:10:27 -07002074 }
2075}
2076
Bryan Green357ccf22011-03-01 16:43:52 -08002077static void end_polls(struct fuse_conn *fc)
2078{
2079 struct rb_node *p;
2080
2081 p = rb_first(&fc->polled_files);
2082
2083 while (p) {
2084 struct fuse_file *ff;
2085 ff = rb_entry(p, struct fuse_file, polled_node);
2086 wake_up_interruptible_all(&ff->poll_wait);
2087
2088 p = rb_next(p);
2089 }
2090}
2091
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002092/*
2093 * Abort all requests.
2094 *
Miklos Szeredib716d422015-07-01 16:25:59 +02002095 * Emergency exit in case of a malicious or accidental deadlock, or just a hung
2096 * filesystem.
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002097 *
Miklos Szeredib716d422015-07-01 16:25:59 +02002098 * The same effect is usually achievable through killing the filesystem daemon
2099 * and all users of the filesystem. The exception is the combination of an
2100 * asynchronous request and the tricky deadlock (see
2101 * Documentation/filesystems/fuse.txt).
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002102 *
Miklos Szeredib716d422015-07-01 16:25:59 +02002103 * Aborting requests under I/O goes as follows: 1: Separate out unlocked
2104 * requests, they should be finished off immediately. Locked requests will be
2105 * finished after unlock; see unlock_request(). 2: Finish off the unlocked
2106 * requests. It is possible that some request will finish before we can. This
2107 * is OK, the request will in that case be removed from the list before we touch
2108 * it.
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002109 */
Szymon Lukasz3b7008b2017-11-09 21:23:35 +01002110void fuse_abort_conn(struct fuse_conn *fc, bool is_abort)
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002111{
Miklos Szeredif88996a2015-07-01 16:26:01 +02002112 struct fuse_iqueue *fiq = &fc->iq;
2113
Miklos Szeredid7133112006-04-10 22:54:55 -07002114 spin_lock(&fc->lock);
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002115 if (fc->connected) {
Miklos Szeredic36960462015-07-01 16:26:09 +02002116 struct fuse_dev *fud;
Miklos Szeredib716d422015-07-01 16:25:59 +02002117 struct fuse_req *req, *next;
Miklos Szeredi75f3ee42018-07-26 16:13:12 +02002118 LIST_HEAD(to_end);
Miklos Szeredib716d422015-07-01 16:25:59 +02002119
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002120 fc->connected = 0;
Miklos Szeredi51eb01e2006-06-25 05:48:50 -07002121 fc->blocked = 0;
Szymon Lukasz3b7008b2017-11-09 21:23:35 +01002122 fc->aborted = is_abort;
Miklos Szeredi9759bd512015-01-06 10:45:35 +01002123 fuse_set_initialized(fc);
Miklos Szeredic36960462015-07-01 16:26:09 +02002124 list_for_each_entry(fud, &fc->devices, entry) {
2125 struct fuse_pqueue *fpq = &fud->pq;
2126
2127 spin_lock(&fpq->lock);
2128 fpq->connected = 0;
2129 list_for_each_entry_safe(req, next, &fpq->io, list) {
2130 req->out.h.error = -ECONNABORTED;
2131 spin_lock(&req->waitq.lock);
2132 set_bit(FR_ABORTED, &req->flags);
2133 if (!test_bit(FR_LOCKED, &req->flags)) {
2134 set_bit(FR_PRIVATE, &req->flags);
Miklos Szeredi87114372018-07-26 16:13:11 +02002135 __fuse_get_request(req);
Miklos Szeredi75f3ee42018-07-26 16:13:12 +02002136 list_move(&req->list, &to_end);
Miklos Szeredic36960462015-07-01 16:26:09 +02002137 }
2138 spin_unlock(&req->waitq.lock);
Miklos Szeredi77cd9d42015-07-01 16:26:06 +02002139 }
Miklos Szeredi75f3ee42018-07-26 16:13:12 +02002140 list_splice_tail_init(&fpq->processing, &to_end);
Miklos Szeredic36960462015-07-01 16:26:09 +02002141 spin_unlock(&fpq->lock);
Miklos Szeredib716d422015-07-01 16:25:59 +02002142 }
Miklos Szeredi41f98272015-07-01 16:25:59 +02002143 fc->max_background = UINT_MAX;
2144 flush_bg_queue(fc);
Miklos Szeredi8c911892015-07-01 16:26:02 +02002145
Miklos Szeredi4ce60812015-07-01 16:26:02 +02002146 spin_lock(&fiq->waitq.lock);
Miklos Szeredi8c911892015-07-01 16:26:02 +02002147 fiq->connected = 0;
Miklos Szeredi75f3ee42018-07-26 16:13:12 +02002148 list_for_each_entry(req, &fiq->pending, list)
Tahsin Erdogana8a86d72017-01-12 12:04:04 -08002149 clear_bit(FR_PENDING, &req->flags);
Miklos Szeredi75f3ee42018-07-26 16:13:12 +02002150 list_splice_tail_init(&fiq->pending, &to_end);
Miklos Szeredi8c911892015-07-01 16:26:02 +02002151 while (forget_pending(fiq))
2152 kfree(dequeue_forget(fiq, 1, NULL));
Miklos Szeredi4ce60812015-07-01 16:26:02 +02002153 wake_up_all_locked(&fiq->waitq);
2154 spin_unlock(&fiq->waitq.lock);
Miklos Szeredi8c911892015-07-01 16:26:02 +02002155 kill_fasync(&fiq->fasync, SIGIO, POLL_IN);
Miklos Szerediee314a82015-07-01 16:26:08 +02002156 end_polls(fc);
2157 wake_up_all(&fc->blocked_waitq);
2158 spin_unlock(&fc->lock);
Miklos Szeredi8c911892015-07-01 16:26:02 +02002159
Miklos Szeredi75f3ee42018-07-26 16:13:12 +02002160 end_requests(fc, &to_end);
Miklos Szerediee314a82015-07-01 16:26:08 +02002161 } else {
2162 spin_unlock(&fc->lock);
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002163 }
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002164}
Tejun Heo08cbf542009-04-14 10:54:53 +09002165EXPORT_SYMBOL_GPL(fuse_abort_conn);
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002166
Miklos Szeredib8f95e52018-07-26 16:13:11 +02002167void fuse_wait_aborted(struct fuse_conn *fc)
2168{
2169 wait_event(fc->blocked_waitq, atomic_read(&fc->num_waiting) == 0);
2170}
2171
Tejun Heo08cbf542009-04-14 10:54:53 +09002172int fuse_dev_release(struct inode *inode, struct file *file)
Miklos Szeredi334f4852005-09-09 13:10:27 -07002173{
Miklos Szeredicc080e92015-07-01 16:26:08 +02002174 struct fuse_dev *fud = fuse_get_dev(file);
2175
2176 if (fud) {
2177 struct fuse_conn *fc = fud->fc;
Miklos Szeredic36960462015-07-01 16:26:09 +02002178 struct fuse_pqueue *fpq = &fud->pq;
Miklos Szeredi45ff3502018-07-26 16:13:11 +02002179 LIST_HEAD(to_end);
Miklos Szeredicc080e92015-07-01 16:26:08 +02002180
Miklos Szeredi45ff3502018-07-26 16:13:11 +02002181 spin_lock(&fpq->lock);
Miklos Szeredic36960462015-07-01 16:26:09 +02002182 WARN_ON(!list_empty(&fpq->io));
Miklos Szeredi45ff3502018-07-26 16:13:11 +02002183 list_splice_init(&fpq->processing, &to_end);
2184 spin_unlock(&fpq->lock);
2185
2186 end_requests(fc, &to_end);
2187
Miklos Szeredic36960462015-07-01 16:26:09 +02002188 /* Are we the last open device? */
2189 if (atomic_dec_and_test(&fc->dev_count)) {
2190 WARN_ON(fc->iq.fasync != NULL);
Szymon Lukasz3b7008b2017-11-09 21:23:35 +01002191 fuse_abort_conn(fc, false);
Miklos Szeredic36960462015-07-01 16:26:09 +02002192 }
Miklos Szeredicc080e92015-07-01 16:26:08 +02002193 fuse_dev_free(fud);
Jeff Dike385a17b2006-04-10 22:54:52 -07002194 }
Miklos Szeredi334f4852005-09-09 13:10:27 -07002195 return 0;
2196}
Tejun Heo08cbf542009-04-14 10:54:53 +09002197EXPORT_SYMBOL_GPL(fuse_dev_release);
Miklos Szeredi334f4852005-09-09 13:10:27 -07002198
Jeff Dike385a17b2006-04-10 22:54:52 -07002199static int fuse_dev_fasync(int fd, struct file *file, int on)
2200{
Miklos Szeredicc080e92015-07-01 16:26:08 +02002201 struct fuse_dev *fud = fuse_get_dev(file);
2202
2203 if (!fud)
Miklos Szeredia87046d2006-04-10 22:54:56 -07002204 return -EPERM;
Jeff Dike385a17b2006-04-10 22:54:52 -07002205
2206 /* No locking - fasync_helper does its own locking */
Miklos Szeredicc080e92015-07-01 16:26:08 +02002207 return fasync_helper(fd, file, on, &fud->fc->iq.fasync);
Jeff Dike385a17b2006-04-10 22:54:52 -07002208}
2209
Miklos Szeredi00c570f2015-07-01 16:26:08 +02002210static int fuse_device_clone(struct fuse_conn *fc, struct file *new)
2211{
Miklos Szeredicc080e92015-07-01 16:26:08 +02002212 struct fuse_dev *fud;
2213
Miklos Szeredi00c570f2015-07-01 16:26:08 +02002214 if (new->private_data)
2215 return -EINVAL;
2216
Miklos Szeredicc080e92015-07-01 16:26:08 +02002217 fud = fuse_dev_alloc(fc);
2218 if (!fud)
2219 return -ENOMEM;
2220
2221 new->private_data = fud;
Miklos Szeredic36960462015-07-01 16:26:09 +02002222 atomic_inc(&fc->dev_count);
Miklos Szeredi00c570f2015-07-01 16:26:08 +02002223
2224 return 0;
2225}
2226
2227static long fuse_dev_ioctl(struct file *file, unsigned int cmd,
2228 unsigned long arg)
2229{
2230 int err = -ENOTTY;
2231
2232 if (cmd == FUSE_DEV_IOC_CLONE) {
2233 int oldfd;
2234
2235 err = -EFAULT;
2236 if (!get_user(oldfd, (__u32 __user *) arg)) {
2237 struct file *old = fget(oldfd);
2238
2239 err = -EINVAL;
2240 if (old) {
Jann Horn8ed1f0e2015-08-16 20:27:01 +02002241 struct fuse_dev *fud = NULL;
2242
2243 /*
2244 * Check against file->f_op because CUSE
2245 * uses the same ioctl handler.
2246 */
2247 if (old->f_op == file->f_op &&
2248 old->f_cred->user_ns == file->f_cred->user_ns)
2249 fud = fuse_get_dev(old);
Miklos Szeredi00c570f2015-07-01 16:26:08 +02002250
Miklos Szeredicc080e92015-07-01 16:26:08 +02002251 if (fud) {
Miklos Szeredi00c570f2015-07-01 16:26:08 +02002252 mutex_lock(&fuse_mutex);
Miklos Szeredicc080e92015-07-01 16:26:08 +02002253 err = fuse_device_clone(fud->fc, file);
Miklos Szeredi00c570f2015-07-01 16:26:08 +02002254 mutex_unlock(&fuse_mutex);
2255 }
2256 fput(old);
2257 }
2258 }
2259 }
2260 return err;
2261}
2262
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -08002263const struct file_operations fuse_dev_operations = {
Miklos Szeredi334f4852005-09-09 13:10:27 -07002264 .owner = THIS_MODULE,
Tom Van Braeckel94e4fe22015-01-12 05:22:16 +01002265 .open = fuse_dev_open,
Miklos Szeredi334f4852005-09-09 13:10:27 -07002266 .llseek = no_llseek,
Al Virofbdbacc2015-04-03 21:53:39 -04002267 .read_iter = fuse_dev_read,
Miklos Szeredic3021622010-05-25 15:06:07 +02002268 .splice_read = fuse_dev_splice_read,
Al Virofbdbacc2015-04-03 21:53:39 -04002269 .write_iter = fuse_dev_write,
Miklos Szeredidd3bb142010-05-25 15:06:06 +02002270 .splice_write = fuse_dev_splice_write,
Miklos Szeredi334f4852005-09-09 13:10:27 -07002271 .poll = fuse_dev_poll,
2272 .release = fuse_dev_release,
Jeff Dike385a17b2006-04-10 22:54:52 -07002273 .fasync = fuse_dev_fasync,
Miklos Szeredi00c570f2015-07-01 16:26:08 +02002274 .unlocked_ioctl = fuse_dev_ioctl,
2275 .compat_ioctl = fuse_dev_ioctl,
Miklos Szeredi334f4852005-09-09 13:10:27 -07002276};
Tejun Heo08cbf542009-04-14 10:54:53 +09002277EXPORT_SYMBOL_GPL(fuse_dev_operations);
Miklos Szeredi334f4852005-09-09 13:10:27 -07002278
2279static struct miscdevice fuse_miscdevice = {
2280 .minor = FUSE_MINOR,
2281 .name = "fuse",
2282 .fops = &fuse_dev_operations,
2283};
2284
2285int __init fuse_dev_init(void)
2286{
2287 int err = -ENOMEM;
2288 fuse_req_cachep = kmem_cache_create("fuse_request",
2289 sizeof(struct fuse_req),
Paul Mundt20c2df82007-07-20 10:11:58 +09002290 0, 0, NULL);
Miklos Szeredi334f4852005-09-09 13:10:27 -07002291 if (!fuse_req_cachep)
2292 goto out;
2293
2294 err = misc_register(&fuse_miscdevice);
2295 if (err)
2296 goto out_cache_clean;
2297
2298 return 0;
2299
2300 out_cache_clean:
2301 kmem_cache_destroy(fuse_req_cachep);
2302 out:
2303 return err;
2304}
2305
2306void fuse_dev_cleanup(void)
2307{
2308 misc_deregister(&fuse_miscdevice);
2309 kmem_cache_destroy(fuse_req_cachep);
2310}