blob: 7dcad4c72efec924e34e5f33bbbc4247498bb4c4 [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"
Nikhilesh Reddy759832b2017-07-26 09:41:57 +053010#include "fuse_passthrough.h"
Miklos Szeredi334f4852005-09-09 13:10:27 -070011
12#include <linux/init.h>
13#include <linux/module.h>
14#include <linux/poll.h>
15#include <linux/uio.h>
16#include <linux/miscdevice.h>
Daniel Rosenbergfac99a72016-04-22 00:00:48 -070017#include <linux/namei.h>
Miklos Szeredi334f4852005-09-09 13:10:27 -070018#include <linux/pagemap.h>
19#include <linux/file.h>
20#include <linux/slab.h>
Miklos Szeredidd3bb142010-05-25 15:06:06 +020021#include <linux/pipe_fs_i.h>
Miklos Szeredice534fb2010-05-25 15:06:07 +020022#include <linux/swap.h>
23#include <linux/splice.h>
Todd Poynor1672c662011-08-24 15:01:30 -070024#include <linux/freezer.h>
Miklos Szeredi334f4852005-09-09 13:10:27 -070025
26MODULE_ALIAS_MISCDEV(FUSE_MINOR);
Kay Sievers578454f2010-05-20 18:07:20 +020027MODULE_ALIAS("devname:fuse");
Miklos Szeredi334f4852005-09-09 13:10:27 -070028
Christoph Lametere18b8902006-12-06 20:33:20 -080029static struct kmem_cache *fuse_req_cachep;
Miklos Szeredi334f4852005-09-09 13:10:27 -070030
Miklos Szeredicc080e92015-07-01 16:26:08 +020031static struct fuse_dev *fuse_get_dev(struct file *file)
Miklos Szeredi334f4852005-09-09 13:10:27 -070032{
Miklos Szeredi0720b312006-04-10 22:54:55 -070033 /*
34 * Lockless access is OK, because file->private data is set
35 * once during mount and is valid until the file is released.
36 */
Miklos Szeredicc080e92015-07-01 16:26:08 +020037 return ACCESS_ONCE(file->private_data);
Miklos Szeredi334f4852005-09-09 13:10:27 -070038}
39
Maxim Patlasov4250c062012-10-26 19:48:07 +040040static void fuse_request_init(struct fuse_req *req, struct page **pages,
Maxim Patlasovb2430d72012-10-26 19:49:24 +040041 struct fuse_page_desc *page_descs,
Maxim Patlasov4250c062012-10-26 19:48:07 +040042 unsigned npages)
Miklos Szeredi334f4852005-09-09 13:10:27 -070043{
44 memset(req, 0, sizeof(*req));
Maxim Patlasov4250c062012-10-26 19:48:07 +040045 memset(pages, 0, sizeof(*pages) * npages);
Maxim Patlasovb2430d72012-10-26 19:49:24 +040046 memset(page_descs, 0, sizeof(*page_descs) * npages);
Miklos Szeredi334f4852005-09-09 13:10:27 -070047 INIT_LIST_HEAD(&req->list);
Miklos Szeredia4d27e72006-06-25 05:48:54 -070048 INIT_LIST_HEAD(&req->intr_entry);
Miklos Szeredi334f4852005-09-09 13:10:27 -070049 init_waitqueue_head(&req->waitq);
50 atomic_set(&req->count, 1);
Maxim Patlasov4250c062012-10-26 19:48:07 +040051 req->pages = pages;
Maxim Patlasovb2430d72012-10-26 19:49:24 +040052 req->page_descs = page_descs;
Maxim Patlasov4250c062012-10-26 19:48:07 +040053 req->max_pages = npages;
Miklos Szeredi33e14b42015-07-01 16:26:01 +020054 __set_bit(FR_PENDING, &req->flags);
Miklos Szeredi334f4852005-09-09 13:10:27 -070055}
56
Maxim Patlasov4250c062012-10-26 19:48:07 +040057static struct fuse_req *__fuse_request_alloc(unsigned npages, gfp_t flags)
Miklos Szeredi334f4852005-09-09 13:10:27 -070058{
Maxim Patlasov4250c062012-10-26 19:48:07 +040059 struct fuse_req *req = kmem_cache_alloc(fuse_req_cachep, flags);
60 if (req) {
61 struct page **pages;
Maxim Patlasovb2430d72012-10-26 19:49:24 +040062 struct fuse_page_desc *page_descs;
Maxim Patlasov4250c062012-10-26 19:48:07 +040063
Maxim Patlasovb2430d72012-10-26 19:49:24 +040064 if (npages <= FUSE_REQ_INLINE_PAGES) {
Maxim Patlasov4250c062012-10-26 19:48:07 +040065 pages = req->inline_pages;
Maxim Patlasovb2430d72012-10-26 19:49:24 +040066 page_descs = req->inline_page_descs;
67 } else {
Maxim Patlasov4250c062012-10-26 19:48:07 +040068 pages = kmalloc(sizeof(struct page *) * npages, flags);
Maxim Patlasovb2430d72012-10-26 19:49:24 +040069 page_descs = kmalloc(sizeof(struct fuse_page_desc) *
70 npages, flags);
71 }
Maxim Patlasov4250c062012-10-26 19:48:07 +040072
Maxim Patlasovb2430d72012-10-26 19:49:24 +040073 if (!pages || !page_descs) {
74 kfree(pages);
75 kfree(page_descs);
Maxim Patlasov4250c062012-10-26 19:48:07 +040076 kmem_cache_free(fuse_req_cachep, req);
77 return NULL;
78 }
79
Maxim Patlasovb2430d72012-10-26 19:49:24 +040080 fuse_request_init(req, pages, page_descs, npages);
Maxim Patlasov4250c062012-10-26 19:48:07 +040081 }
Miklos Szeredi334f4852005-09-09 13:10:27 -070082 return req;
83}
Maxim Patlasov4250c062012-10-26 19:48:07 +040084
85struct fuse_req *fuse_request_alloc(unsigned npages)
86{
87 return __fuse_request_alloc(npages, GFP_KERNEL);
88}
Tejun Heo08cbf542009-04-14 10:54:53 +090089EXPORT_SYMBOL_GPL(fuse_request_alloc);
Miklos Szeredi334f4852005-09-09 13:10:27 -070090
Maxim Patlasov4250c062012-10-26 19:48:07 +040091struct fuse_req *fuse_request_alloc_nofs(unsigned npages)
Miklos Szeredi3be5a522008-04-30 00:54:41 -070092{
Maxim Patlasov4250c062012-10-26 19:48:07 +040093 return __fuse_request_alloc(npages, GFP_NOFS);
Miklos Szeredi3be5a522008-04-30 00:54:41 -070094}
95
Miklos Szeredi334f4852005-09-09 13:10:27 -070096void fuse_request_free(struct fuse_req *req)
97{
Maxim Patlasovb2430d72012-10-26 19:49:24 +040098 if (req->pages != req->inline_pages) {
Maxim Patlasov4250c062012-10-26 19:48:07 +040099 kfree(req->pages);
Maxim Patlasovb2430d72012-10-26 19:49:24 +0400100 kfree(req->page_descs);
101 }
Miklos Szeredi334f4852005-09-09 13:10:27 -0700102 kmem_cache_free(fuse_req_cachep, req);
103}
104
Maxim Patlasov36cf66e2012-12-14 19:20:51 +0400105void __fuse_get_request(struct fuse_req *req)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700106{
107 atomic_inc(&req->count);
108}
109
110/* Must be called with > 1 refcount */
111static void __fuse_put_request(struct fuse_req *req)
112{
113 BUG_ON(atomic_read(&req->count) < 2);
114 atomic_dec(&req->count);
115}
116
Miklos Szeredi33649c92006-06-25 05:48:52 -0700117static void fuse_req_init_context(struct fuse_req *req)
118{
Eric W. Biederman499dcf22012-02-07 16:26:03 -0800119 req->in.h.uid = from_kuid_munged(&init_user_ns, current_fsuid());
120 req->in.h.gid = from_kgid_munged(&init_user_ns, current_fsgid());
Miklos Szeredi33649c92006-06-25 05:48:52 -0700121 req->in.h.pid = current->pid;
122}
123
Miklos Szeredi9759bd512015-01-06 10:45:35 +0100124void fuse_set_initialized(struct fuse_conn *fc)
125{
126 /* Make sure stores before this are seen on another CPU */
127 smp_wmb();
128 fc->initialized = 1;
129}
130
Maxim Patlasov0aada882013-03-21 18:02:28 +0400131static bool fuse_block_alloc(struct fuse_conn *fc, bool for_background)
132{
133 return !fc->initialized || (for_background && fc->blocked);
134}
135
Miklos Szeredi6465d762018-07-26 16:13:11 +0200136static void fuse_drop_waiting(struct fuse_conn *fc)
137{
138 if (fc->connected) {
139 atomic_dec(&fc->num_waiting);
140 } else if (atomic_dec_and_test(&fc->num_waiting)) {
141 /* wake up aborters */
142 wake_up_all(&fc->blocked_waitq);
143 }
144}
145
Maxim Patlasov8b41e672013-03-21 18:02:04 +0400146static struct fuse_req *__fuse_get_req(struct fuse_conn *fc, unsigned npages,
147 bool for_background)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700148{
Miklos Szeredi08a53cd2006-04-10 22:54:59 -0700149 struct fuse_req *req;
Miklos Szeredi08a53cd2006-04-10 22:54:59 -0700150 int err;
Miklos Szeredi9bc5ddd2006-04-11 21:16:09 +0200151 atomic_inc(&fc->num_waiting);
Maxim Patlasov0aada882013-03-21 18:02:28 +0400152
153 if (fuse_block_alloc(fc, for_background)) {
Maxim Patlasov0aada882013-03-21 18:02:28 +0400154 err = -EINTR;
Al Viro7d3a07f2016-07-19 03:08:27 -0400155 if (wait_event_killable_exclusive(fc->blocked_waitq,
156 !fuse_block_alloc(fc, for_background)))
Maxim Patlasov0aada882013-03-21 18:02:28 +0400157 goto out;
158 }
Miklos Szeredi9759bd512015-01-06 10:45:35 +0100159 /* Matches smp_wmb() in fuse_set_initialized() */
160 smp_rmb();
Miklos Szeredi08a53cd2006-04-10 22:54:59 -0700161
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700162 err = -ENOTCONN;
163 if (!fc->connected)
164 goto out;
165
Miklos Szeredide155222015-07-01 16:25:57 +0200166 err = -ECONNREFUSED;
167 if (fc->conn_error)
168 goto out;
169
Maxim Patlasovb111c8c2012-10-26 19:48:30 +0400170 req = fuse_request_alloc(npages);
Miklos Szeredi9bc5ddd2006-04-11 21:16:09 +0200171 err = -ENOMEM;
Maxim Patlasov722d2be2013-03-21 18:02:36 +0400172 if (!req) {
173 if (for_background)
174 wake_up(&fc->blocked_waitq);
Miklos Szeredi9bc5ddd2006-04-11 21:16:09 +0200175 goto out;
Maxim Patlasov722d2be2013-03-21 18:02:36 +0400176 }
Miklos Szeredi334f4852005-09-09 13:10:27 -0700177
Miklos Szeredi33649c92006-06-25 05:48:52 -0700178 fuse_req_init_context(req);
Miklos Szeredi825d6d32015-07-01 16:25:58 +0200179 __set_bit(FR_WAITING, &req->flags);
180 if (for_background)
181 __set_bit(FR_BACKGROUND, &req->flags);
182
Miklos Szeredi334f4852005-09-09 13:10:27 -0700183 return req;
Miklos Szeredi9bc5ddd2006-04-11 21:16:09 +0200184
185 out:
Miklos Szeredi6465d762018-07-26 16:13:11 +0200186 fuse_drop_waiting(fc);
Miklos Szeredi9bc5ddd2006-04-11 21:16:09 +0200187 return ERR_PTR(err);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700188}
Maxim Patlasov8b41e672013-03-21 18:02:04 +0400189
190struct fuse_req *fuse_get_req(struct fuse_conn *fc, unsigned npages)
191{
192 return __fuse_get_req(fc, npages, false);
193}
Tejun Heo08cbf542009-04-14 10:54:53 +0900194EXPORT_SYMBOL_GPL(fuse_get_req);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700195
Maxim Patlasov8b41e672013-03-21 18:02:04 +0400196struct fuse_req *fuse_get_req_for_background(struct fuse_conn *fc,
197 unsigned npages)
198{
199 return __fuse_get_req(fc, npages, true);
200}
201EXPORT_SYMBOL_GPL(fuse_get_req_for_background);
202
Miklos Szeredi33649c92006-06-25 05:48:52 -0700203/*
204 * Return request in fuse_file->reserved_req. However that may
205 * currently be in use. If that is the case, wait for it to become
206 * available.
207 */
208static struct fuse_req *get_reserved_req(struct fuse_conn *fc,
209 struct file *file)
210{
211 struct fuse_req *req = NULL;
212 struct fuse_file *ff = file->private_data;
213
214 do {
Miklos Szeredide5e3de2007-10-16 23:31:00 -0700215 wait_event(fc->reserved_req_waitq, ff->reserved_req);
Miklos Szeredi33649c92006-06-25 05:48:52 -0700216 spin_lock(&fc->lock);
217 if (ff->reserved_req) {
218 req = ff->reserved_req;
219 ff->reserved_req = NULL;
Al Virocb0942b2012-08-27 14:48:26 -0400220 req->stolen_file = get_file(file);
Miklos Szeredi33649c92006-06-25 05:48:52 -0700221 }
222 spin_unlock(&fc->lock);
223 } while (!req);
224
225 return req;
226}
227
228/*
229 * Put stolen request back into fuse_file->reserved_req
230 */
231static void put_reserved_req(struct fuse_conn *fc, struct fuse_req *req)
232{
233 struct file *file = req->stolen_file;
234 struct fuse_file *ff = file->private_data;
235
236 spin_lock(&fc->lock);
Maxim Patlasovb2430d72012-10-26 19:49:24 +0400237 fuse_request_init(req, req->pages, req->page_descs, req->max_pages);
Miklos Szeredi33649c92006-06-25 05:48:52 -0700238 BUG_ON(ff->reserved_req);
239 ff->reserved_req = req;
Miklos Szeredide5e3de2007-10-16 23:31:00 -0700240 wake_up_all(&fc->reserved_req_waitq);
Miklos Szeredi33649c92006-06-25 05:48:52 -0700241 spin_unlock(&fc->lock);
242 fput(file);
243}
244
245/*
246 * Gets a requests for a file operation, always succeeds
247 *
248 * This is used for sending the FLUSH request, which must get to
249 * userspace, due to POSIX locks which may need to be unlocked.
250 *
251 * If allocation fails due to OOM, use the reserved request in
252 * fuse_file.
253 *
254 * This is very unlikely to deadlock accidentally, since the
255 * filesystem should not have it's own file open. If deadlock is
256 * intentional, it can still be broken by "aborting" the filesystem.
257 */
Maxim Patlasovb111c8c2012-10-26 19:48:30 +0400258struct fuse_req *fuse_get_req_nofail_nopages(struct fuse_conn *fc,
259 struct file *file)
Miklos Szeredi33649c92006-06-25 05:48:52 -0700260{
261 struct fuse_req *req;
262
263 atomic_inc(&fc->num_waiting);
Maxim Patlasov0aada882013-03-21 18:02:28 +0400264 wait_event(fc->blocked_waitq, fc->initialized);
Miklos Szeredi9759bd512015-01-06 10:45:35 +0100265 /* Matches smp_wmb() in fuse_set_initialized() */
266 smp_rmb();
Maxim Patlasovb111c8c2012-10-26 19:48:30 +0400267 req = fuse_request_alloc(0);
Miklos Szeredi33649c92006-06-25 05:48:52 -0700268 if (!req)
269 req = get_reserved_req(fc, file);
270
271 fuse_req_init_context(req);
Miklos Szeredi825d6d32015-07-01 16:25:58 +0200272 __set_bit(FR_WAITING, &req->flags);
273 __clear_bit(FR_BACKGROUND, &req->flags);
Miklos Szeredi33649c92006-06-25 05:48:52 -0700274 return req;
275}
276
Miklos Szeredi334f4852005-09-09 13:10:27 -0700277void fuse_put_request(struct fuse_conn *fc, struct fuse_req *req)
278{
Miklos Szeredi7128ec22006-02-04 23:27:40 -0800279 if (atomic_dec_and_test(&req->count)) {
Miklos Szeredi825d6d32015-07-01 16:25:58 +0200280 if (test_bit(FR_BACKGROUND, &req->flags)) {
Maxim Patlasov722d2be2013-03-21 18:02:36 +0400281 /*
282 * We get here in the unlikely case that a background
283 * request was allocated but not sent
284 */
285 spin_lock(&fc->lock);
286 if (!fc->blocked)
287 wake_up(&fc->blocked_waitq);
288 spin_unlock(&fc->lock);
289 }
290
Miklos Szeredi825d6d32015-07-01 16:25:58 +0200291 if (test_bit(FR_WAITING, &req->flags)) {
292 __clear_bit(FR_WAITING, &req->flags);
Miklos Szeredi6465d762018-07-26 16:13:11 +0200293 fuse_drop_waiting(fc);
Miklos Szeredi73e0e732015-07-01 16:25:56 +0200294 }
Miklos Szeredi33649c92006-06-25 05:48:52 -0700295
296 if (req->stolen_file)
297 put_reserved_req(fc, req);
298 else
299 fuse_request_free(req);
Miklos Szeredi7128ec22006-02-04 23:27:40 -0800300 }
301}
Tejun Heo08cbf542009-04-14 10:54:53 +0900302EXPORT_SYMBOL_GPL(fuse_put_request);
Miklos Szeredi7128ec22006-02-04 23:27:40 -0800303
Miklos Szeredid12def12008-02-06 01:38:39 -0800304static unsigned len_args(unsigned numargs, struct fuse_arg *args)
305{
306 unsigned nbytes = 0;
307 unsigned i;
308
309 for (i = 0; i < numargs; i++)
310 nbytes += args[i].size;
311
312 return nbytes;
313}
314
Miklos Szeredif88996a2015-07-01 16:26:01 +0200315static u64 fuse_get_unique(struct fuse_iqueue *fiq)
Miklos Szeredid12def12008-02-06 01:38:39 -0800316{
Miklos Szeredif88996a2015-07-01 16:26:01 +0200317 return ++fiq->reqctr;
Miklos Szeredid12def12008-02-06 01:38:39 -0800318}
319
Miklos Szeredif88996a2015-07-01 16:26:01 +0200320static void queue_request(struct fuse_iqueue *fiq, struct fuse_req *req)
Miklos Szeredid12def12008-02-06 01:38:39 -0800321{
Miklos Szeredid12def12008-02-06 01:38:39 -0800322 req->in.h.len = sizeof(struct fuse_in_header) +
323 len_args(req->in.numargs, (struct fuse_arg *) req->in.args);
Miklos Szeredif88996a2015-07-01 16:26:01 +0200324 list_add_tail(&req->list, &fiq->pending);
Miklos Szeredi4ce60812015-07-01 16:26:02 +0200325 wake_up_locked(&fiq->waitq);
Miklos Szeredif88996a2015-07-01 16:26:01 +0200326 kill_fasync(&fiq->fasync, SIGIO, POLL_IN);
Miklos Szeredid12def12008-02-06 01:38:39 -0800327}
328
Miklos Szeredi07e77dc2010-12-07 20:16:56 +0100329void fuse_queue_forget(struct fuse_conn *fc, struct fuse_forget_link *forget,
330 u64 nodeid, u64 nlookup)
331{
Miklos Szeredif88996a2015-07-01 16:26:01 +0200332 struct fuse_iqueue *fiq = &fc->iq;
333
Miklos Szeredi02c048b2010-12-07 20:16:56 +0100334 forget->forget_one.nodeid = nodeid;
335 forget->forget_one.nlookup = nlookup;
Miklos Szeredi07e77dc2010-12-07 20:16:56 +0100336
Miklos Szeredi4ce60812015-07-01 16:26:02 +0200337 spin_lock(&fiq->waitq.lock);
Miklos Szeredie16714d2015-07-01 16:26:01 +0200338 if (fiq->connected) {
Miklos Szeredif88996a2015-07-01 16:26:01 +0200339 fiq->forget_list_tail->next = forget;
340 fiq->forget_list_tail = forget;
Miklos Szeredi4ce60812015-07-01 16:26:02 +0200341 wake_up_locked(&fiq->waitq);
Miklos Szeredif88996a2015-07-01 16:26:01 +0200342 kill_fasync(&fiq->fasync, SIGIO, POLL_IN);
Miklos Szeredi5dfcc872011-09-12 09:38:03 +0200343 } else {
344 kfree(forget);
345 }
Miklos Szeredi4ce60812015-07-01 16:26:02 +0200346 spin_unlock(&fiq->waitq.lock);
Miklos Szeredi07e77dc2010-12-07 20:16:56 +0100347}
348
Miklos Szeredid12def12008-02-06 01:38:39 -0800349static void flush_bg_queue(struct fuse_conn *fc)
350{
Csaba Henk7a6d3c82009-07-01 17:28:41 -0700351 while (fc->active_background < fc->max_background &&
Miklos Szeredid12def12008-02-06 01:38:39 -0800352 !list_empty(&fc->bg_queue)) {
353 struct fuse_req *req;
Miklos Szeredif88996a2015-07-01 16:26:01 +0200354 struct fuse_iqueue *fiq = &fc->iq;
Miklos Szeredid12def12008-02-06 01:38:39 -0800355
356 req = list_entry(fc->bg_queue.next, struct fuse_req, list);
357 list_del(&req->list);
358 fc->active_background++;
Miklos Szeredi4ce60812015-07-01 16:26:02 +0200359 spin_lock(&fiq->waitq.lock);
Miklos Szeredif88996a2015-07-01 16:26:01 +0200360 req->in.h.unique = fuse_get_unique(fiq);
361 queue_request(fiq, req);
Miklos Szeredi4ce60812015-07-01 16:26:02 +0200362 spin_unlock(&fiq->waitq.lock);
Miklos Szeredid12def12008-02-06 01:38:39 -0800363 }
364}
365
Miklos Szeredi6dbbcb12006-04-26 10:49:06 +0200366/*
Miklos Szeredi334f4852005-09-09 13:10:27 -0700367 * This function is called when a request is finished. Either a reply
Miklos Szeredif9a28422006-06-25 05:48:53 -0700368 * has arrived or it was aborted (and not yet sent) or some error
Miklos Szeredif43b1552006-01-16 22:14:26 -0800369 * occurred during communication with userspace, or the device file
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700370 * was closed. The requester thread is woken up (if still waiting),
371 * the 'end' callback is called if given, else the reference to the
372 * request is released
Miklos Szeredi334f4852005-09-09 13:10:27 -0700373 */
374static void request_end(struct fuse_conn *fc, struct fuse_req *req)
375{
Miklos Szeredi4ce60812015-07-01 16:26:02 +0200376 struct fuse_iqueue *fiq = &fc->iq;
Miklos Szeredi365ae712015-07-01 16:26:06 +0200377
Miklos Szerediefe28002015-07-01 16:26:07 +0200378 if (test_and_set_bit(FR_FINISHED, &req->flags))
Miklos Szeredi6465d762018-07-26 16:13:11 +0200379 goto put_request;
Miklos Szeredi365ae712015-07-01 16:26:06 +0200380
Miklos Szeredi4ce60812015-07-01 16:26:02 +0200381 spin_lock(&fiq->waitq.lock);
Miklos Szeredi0d8e84b2015-07-01 16:25:58 +0200382 list_del_init(&req->intr_entry);
Miklos Szeredi4ce60812015-07-01 16:26:02 +0200383 spin_unlock(&fiq->waitq.lock);
Miklos Szeredi33e14b42015-07-01 16:26:01 +0200384 WARN_ON(test_bit(FR_PENDING, &req->flags));
385 WARN_ON(test_bit(FR_SENT, &req->flags));
Miklos Szeredi825d6d32015-07-01 16:25:58 +0200386 if (test_bit(FR_BACKGROUND, &req->flags)) {
Miklos Szerediefe28002015-07-01 16:26:07 +0200387 spin_lock(&fc->lock);
Miklos Szeredi825d6d32015-07-01 16:25:58 +0200388 clear_bit(FR_BACKGROUND, &req->flags);
Miklos Szeredi7238fcb2018-09-28 16:43:22 +0200389 if (fc->num_background == fc->max_background) {
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700390 fc->blocked = 0;
Maxim Patlasov722d2be2013-03-21 18:02:36 +0400391 wake_up(&fc->blocked_waitq);
Miklos Szeredi7238fcb2018-09-28 16:43:22 +0200392 } else if (!fc->blocked) {
393 /*
394 * Wake up next waiter, if any. It's okay to use
395 * waitqueue_active(), as we've already synced up
396 * fc->blocked with waiters with the wake_up() call
397 * above.
398 */
399 if (waitqueue_active(&fc->blocked_waitq))
400 wake_up(&fc->blocked_waitq);
401 }
Maxim Patlasov722d2be2013-03-21 18:02:36 +0400402
Csaba Henk7a6d3c82009-07-01 17:28:41 -0700403 if (fc->num_background == fc->congestion_threshold &&
Tejun Heoa325f9b2009-04-14 10:54:52 +0900404 fc->connected && fc->bdi_initialized) {
Jens Axboe8aa7e842009-07-09 14:52:32 +0200405 clear_bdi_congested(&fc->bdi, BLK_RW_SYNC);
406 clear_bdi_congested(&fc->bdi, BLK_RW_ASYNC);
Miklos Szeredif92b99b2007-10-16 23:30:59 -0700407 }
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700408 fc->num_background--;
Miklos Szeredid12def12008-02-06 01:38:39 -0800409 fc->active_background--;
410 flush_bg_queue(fc);
Miklos Szerediefe28002015-07-01 16:26:07 +0200411 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700412 }
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700413 wake_up(&req->waitq);
Miklos Szeredi1e6881c2015-07-01 16:26:07 +0200414 if (req->end)
415 req->end(fc, req);
Miklos Szeredi6465d762018-07-26 16:13:11 +0200416put_request:
Tejun Heoe9bb09d2008-11-26 12:03:54 +0100417 fuse_put_request(fc, req);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700418}
419
Miklos Szeredif88996a2015-07-01 16:26:01 +0200420static void queue_interrupt(struct fuse_iqueue *fiq, struct fuse_req *req)
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700421{
Miklos Szeredi4ce60812015-07-01 16:26:02 +0200422 spin_lock(&fiq->waitq.lock);
Sahitya Tummala72834482017-02-08 20:30:56 +0530423 if (test_bit(FR_FINISHED, &req->flags)) {
424 spin_unlock(&fiq->waitq.lock);
425 return;
426 }
Miklos Szeredi8f7bb362015-07-01 16:26:03 +0200427 if (list_empty(&req->intr_entry)) {
428 list_add_tail(&req->intr_entry, &fiq->interrupts);
429 wake_up_locked(&fiq->waitq);
430 }
Miklos Szeredi4ce60812015-07-01 16:26:02 +0200431 spin_unlock(&fiq->waitq.lock);
Miklos Szeredif88996a2015-07-01 16:26:01 +0200432 kill_fasync(&fiq->fasync, SIGIO, POLL_IN);
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700433}
434
Miklos Szeredi7c352bd2005-09-09 13:10:39 -0700435static void request_wait_answer(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700436{
Miklos Szeredi4ce60812015-07-01 16:26:02 +0200437 struct fuse_iqueue *fiq = &fc->iq;
Miklos Szeredic4775262015-07-01 16:26:00 +0200438 int err;
439
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700440 if (!fc->no_interrupt) {
441 /* Any signal may interrupt this */
Miklos Szeredic4775262015-07-01 16:26:00 +0200442 err = wait_event_interruptible(req->waitq,
Miklos Szeredi33e14b42015-07-01 16:26:01 +0200443 test_bit(FR_FINISHED, &req->flags));
Miklos Szeredic4775262015-07-01 16:26:00 +0200444 if (!err)
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700445 return;
446
Miklos Szeredi825d6d32015-07-01 16:25:58 +0200447 set_bit(FR_INTERRUPTED, &req->flags);
Miklos Szeredi8f7bb362015-07-01 16:26:03 +0200448 /* matches barrier in fuse_dev_do_read() */
449 smp_mb__after_atomic();
Miklos Szeredi33e14b42015-07-01 16:26:01 +0200450 if (test_bit(FR_SENT, &req->flags))
Miklos Szeredi4ce60812015-07-01 16:26:02 +0200451 queue_interrupt(fiq, req);
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700452 }
453
Miklos Szeredi825d6d32015-07-01 16:25:58 +0200454 if (!test_bit(FR_FORCE, &req->flags)) {
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700455 /* Only fatal signals may interrupt this */
Al Viro7d3a07f2016-07-19 03:08:27 -0400456 err = wait_event_killable(req->waitq,
Miklos Szeredi33e14b42015-07-01 16:26:01 +0200457 test_bit(FR_FINISHED, &req->flags));
Miklos Szeredic4775262015-07-01 16:26:00 +0200458 if (!err)
Miklos Szeredia131de02007-10-16 23:31:04 -0700459 return;
460
Miklos Szeredi4ce60812015-07-01 16:26:02 +0200461 spin_lock(&fiq->waitq.lock);
Miklos Szeredia131de02007-10-16 23:31:04 -0700462 /* Request is not yet in userspace, bail out */
Miklos Szeredi33e14b42015-07-01 16:26:01 +0200463 if (test_bit(FR_PENDING, &req->flags)) {
Miklos Szeredia131de02007-10-16 23:31:04 -0700464 list_del(&req->list);
Miklos Szeredi4ce60812015-07-01 16:26:02 +0200465 spin_unlock(&fiq->waitq.lock);
Miklos Szeredia131de02007-10-16 23:31:04 -0700466 __fuse_put_request(req);
467 req->out.h.error = -EINTR;
468 return;
469 }
Miklos Szeredi4ce60812015-07-01 16:26:02 +0200470 spin_unlock(&fiq->waitq.lock);
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700471 }
Miklos Szeredi334f4852005-09-09 13:10:27 -0700472
Miklos Szeredia131de02007-10-16 23:31:04 -0700473 /*
474 * Either request is already in userspace, or it was forced.
475 * Wait it out.
476 */
Todd Poynor1672c662011-08-24 15:01:30 -0700477 while (!test_bit(FR_FINISHED, &req->flags))
478 wait_event_freezable(req->waitq,
479 test_bit(FR_FINISHED, &req->flags));
Miklos Szeredi334f4852005-09-09 13:10:27 -0700480}
481
Eric Wong6a4e9222013-02-04 13:04:44 +0000482static void __fuse_request_send(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700483{
Miklos Szeredie16714d2015-07-01 16:26:01 +0200484 struct fuse_iqueue *fiq = &fc->iq;
485
Miklos Szeredi825d6d32015-07-01 16:25:58 +0200486 BUG_ON(test_bit(FR_BACKGROUND, &req->flags));
Miklos Szeredi4ce60812015-07-01 16:26:02 +0200487 spin_lock(&fiq->waitq.lock);
Miklos Szeredie16714d2015-07-01 16:26:01 +0200488 if (!fiq->connected) {
Miklos Szeredi4ce60812015-07-01 16:26:02 +0200489 spin_unlock(&fiq->waitq.lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700490 req->out.h.error = -ENOTCONN;
Miklos Szeredic4775262015-07-01 16:26:00 +0200491 } else {
Miklos Szeredif88996a2015-07-01 16:26:01 +0200492 req->in.h.unique = fuse_get_unique(fiq);
493 queue_request(fiq, req);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700494 /* acquire extra reference, since request is still needed
495 after request_end() */
496 __fuse_get_request(req);
Miklos Szeredi4ce60812015-07-01 16:26:02 +0200497 spin_unlock(&fiq->waitq.lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700498
Miklos Szeredi7c352bd2005-09-09 13:10:39 -0700499 request_wait_answer(fc, req);
Miklos Szeredic4775262015-07-01 16:26:00 +0200500 /* Pairs with smp_wmb() in request_end() */
501 smp_rmb();
Miklos Szeredi334f4852005-09-09 13:10:27 -0700502 }
Miklos Szeredi334f4852005-09-09 13:10:27 -0700503}
Eric Wong6a4e9222013-02-04 13:04:44 +0000504
505void fuse_request_send(struct fuse_conn *fc, struct fuse_req *req)
506{
Miklos Szeredi825d6d32015-07-01 16:25:58 +0200507 __set_bit(FR_ISREPLY, &req->flags);
508 if (!test_bit(FR_WAITING, &req->flags)) {
509 __set_bit(FR_WAITING, &req->flags);
Miklos Szeredi5437f242015-07-01 16:25:56 +0200510 atomic_inc(&fc->num_waiting);
511 }
Eric Wong6a4e9222013-02-04 13:04:44 +0000512 __fuse_request_send(fc, req);
513}
Tejun Heo08cbf542009-04-14 10:54:53 +0900514EXPORT_SYMBOL_GPL(fuse_request_send);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700515
Miklos Szeredi21f62172015-01-06 10:45:35 +0100516static void fuse_adjust_compat(struct fuse_conn *fc, struct fuse_args *args)
517{
518 if (fc->minor < 4 && args->in.h.opcode == FUSE_STATFS)
519 args->out.args[0].size = FUSE_COMPAT_STATFS_SIZE;
520
521 if (fc->minor < 9) {
522 switch (args->in.h.opcode) {
523 case FUSE_LOOKUP:
524 case FUSE_CREATE:
525 case FUSE_MKNOD:
526 case FUSE_MKDIR:
527 case FUSE_SYMLINK:
528 case FUSE_LINK:
529 args->out.args[0].size = FUSE_COMPAT_ENTRY_OUT_SIZE;
530 break;
531 case FUSE_GETATTR:
532 case FUSE_SETATTR:
533 args->out.args[0].size = FUSE_COMPAT_ATTR_OUT_SIZE;
534 break;
535 }
536 }
537 if (fc->minor < 12) {
538 switch (args->in.h.opcode) {
539 case FUSE_CREATE:
540 args->in.args[0].size = sizeof(struct fuse_open_in);
541 break;
542 case FUSE_MKNOD:
543 args->in.args[0].size = FUSE_COMPAT_MKNOD_IN_SIZE;
544 break;
545 }
546 }
547}
548
Miklos Szeredi70781872014-12-12 09:49:05 +0100549ssize_t fuse_simple_request(struct fuse_conn *fc, struct fuse_args *args)
550{
551 struct fuse_req *req;
552 ssize_t ret;
553
554 req = fuse_get_req(fc, 0);
555 if (IS_ERR(req))
556 return PTR_ERR(req);
557
Miklos Szeredi21f62172015-01-06 10:45:35 +0100558 /* Needs to be done after fuse_get_req() so that fc->minor is valid */
559 fuse_adjust_compat(fc, args);
560
Miklos Szeredi70781872014-12-12 09:49:05 +0100561 req->in.h.opcode = args->in.h.opcode;
562 req->in.h.nodeid = args->in.h.nodeid;
563 req->in.numargs = args->in.numargs;
564 memcpy(req->in.args, args->in.args,
565 args->in.numargs * sizeof(struct fuse_in_arg));
566 req->out.argvar = args->out.argvar;
567 req->out.numargs = args->out.numargs;
568 memcpy(req->out.args, args->out.args,
569 args->out.numargs * sizeof(struct fuse_arg));
570 fuse_request_send(fc, req);
571 ret = req->out.h.error;
Nikhilesh Reddy759832b2017-07-26 09:41:57 +0530572 if (!ret) {
573 if (args->out.argvar) {
574 WARN_ON(args->out.numargs != 1);
575 ret = req->out.args[0].size;
576 }
577
578 if (req->passthrough_filp != NULL)
579 args->out.passthrough_filp = req->passthrough_filp;
Miklos Szeredi70781872014-12-12 09:49:05 +0100580 }
581 fuse_put_request(fc, req);
582
583 return ret;
584}
585
Miklos Szeredif0139aa2015-07-01 16:25:57 +0200586/*
587 * Called under fc->lock
588 *
589 * fc->connected must have been checked previously
590 */
591void fuse_request_send_background_locked(struct fuse_conn *fc,
592 struct fuse_req *req)
Miklos Szeredid12def12008-02-06 01:38:39 -0800593{
Miklos Szeredi825d6d32015-07-01 16:25:58 +0200594 BUG_ON(!test_bit(FR_BACKGROUND, &req->flags));
595 if (!test_bit(FR_WAITING, &req->flags)) {
596 __set_bit(FR_WAITING, &req->flags);
Miklos Szeredi5437f242015-07-01 16:25:56 +0200597 atomic_inc(&fc->num_waiting);
598 }
Miklos Szeredi825d6d32015-07-01 16:25:58 +0200599 __set_bit(FR_ISREPLY, &req->flags);
Miklos Szeredid12def12008-02-06 01:38:39 -0800600 fc->num_background++;
Csaba Henk7a6d3c82009-07-01 17:28:41 -0700601 if (fc->num_background == fc->max_background)
Miklos Szeredid12def12008-02-06 01:38:39 -0800602 fc->blocked = 1;
Csaba Henk7a6d3c82009-07-01 17:28:41 -0700603 if (fc->num_background == fc->congestion_threshold &&
Tejun Heoa325f9b2009-04-14 10:54:52 +0900604 fc->bdi_initialized) {
Jens Axboe8aa7e842009-07-09 14:52:32 +0200605 set_bdi_congested(&fc->bdi, BLK_RW_SYNC);
606 set_bdi_congested(&fc->bdi, BLK_RW_ASYNC);
Miklos Szeredid12def12008-02-06 01:38:39 -0800607 }
608 list_add_tail(&req->list, &fc->bg_queue);
609 flush_bg_queue(fc);
610}
611
Miklos Szeredif0139aa2015-07-01 16:25:57 +0200612void fuse_request_send_background(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700613{
Miklos Szeredi42dc6212015-07-01 16:25:56 +0200614 BUG_ON(!req->end);
Miklos Szeredid7133112006-04-10 22:54:55 -0700615 spin_lock(&fc->lock);
Miklos Szeredi1e9a4ed2005-09-09 13:10:31 -0700616 if (fc->connected) {
Miklos Szeredif0139aa2015-07-01 16:25:57 +0200617 fuse_request_send_background_locked(fc, req);
Miklos Szeredid7133112006-04-10 22:54:55 -0700618 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700619 } else {
Miklos Szeredi42dc6212015-07-01 16:25:56 +0200620 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700621 req->out.h.error = -ENOTCONN;
Miklos Szeredi42dc6212015-07-01 16:25:56 +0200622 req->end(fc, req);
623 fuse_put_request(fc, req);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700624 }
625}
Tejun Heo08cbf542009-04-14 10:54:53 +0900626EXPORT_SYMBOL_GPL(fuse_request_send_background);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700627
Miklos Szeredi2d45ba32010-07-12 14:41:40 +0200628static int fuse_request_send_notify_reply(struct fuse_conn *fc,
629 struct fuse_req *req, u64 unique)
630{
631 int err = -ENODEV;
Miklos Szeredif88996a2015-07-01 16:26:01 +0200632 struct fuse_iqueue *fiq = &fc->iq;
Miklos Szeredi2d45ba32010-07-12 14:41:40 +0200633
Miklos Szeredi825d6d32015-07-01 16:25:58 +0200634 __clear_bit(FR_ISREPLY, &req->flags);
Miklos Szeredi2d45ba32010-07-12 14:41:40 +0200635 req->in.h.unique = unique;
Miklos Szeredi4ce60812015-07-01 16:26:02 +0200636 spin_lock(&fiq->waitq.lock);
Miklos Szeredie16714d2015-07-01 16:26:01 +0200637 if (fiq->connected) {
Miklos Szeredif88996a2015-07-01 16:26:01 +0200638 queue_request(fiq, req);
Miklos Szeredi2d45ba32010-07-12 14:41:40 +0200639 err = 0;
640 }
Miklos Szeredi4ce60812015-07-01 16:26:02 +0200641 spin_unlock(&fiq->waitq.lock);
Miklos Szeredi2d45ba32010-07-12 14:41:40 +0200642
643 return err;
644}
645
Anand V. Avati0b05b182012-08-19 08:53:23 -0400646void fuse_force_forget(struct file *file, u64 nodeid)
647{
Al Viro6131ffa2013-02-27 16:59:05 -0500648 struct inode *inode = file_inode(file);
Anand V. Avati0b05b182012-08-19 08:53:23 -0400649 struct fuse_conn *fc = get_fuse_conn(inode);
650 struct fuse_req *req;
651 struct fuse_forget_in inarg;
652
653 memset(&inarg, 0, sizeof(inarg));
654 inarg.nlookup = 1;
Maxim Patlasovb111c8c2012-10-26 19:48:30 +0400655 req = fuse_get_req_nofail_nopages(fc, file);
Anand V. Avati0b05b182012-08-19 08:53:23 -0400656 req->in.h.opcode = FUSE_FORGET;
657 req->in.h.nodeid = nodeid;
658 req->in.numargs = 1;
659 req->in.args[0].size = sizeof(inarg);
660 req->in.args[0].value = &inarg;
Miklos Szeredi825d6d32015-07-01 16:25:58 +0200661 __clear_bit(FR_ISREPLY, &req->flags);
Eric Wong6a4e9222013-02-04 13:04:44 +0000662 __fuse_request_send(fc, req);
663 /* ignore errors */
664 fuse_put_request(fc, req);
Anand V. Avati0b05b182012-08-19 08:53:23 -0400665}
666
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700667/*
Miklos Szeredi334f4852005-09-09 13:10:27 -0700668 * Lock the request. Up to the next unlock_request() there mustn't be
669 * anything that could cause a page-fault. If the request was already
Miklos Szeredif9a28422006-06-25 05:48:53 -0700670 * aborted bail out.
Miklos Szeredi334f4852005-09-09 13:10:27 -0700671 */
Miklos Szeredidc008092015-07-01 16:25:58 +0200672static int lock_request(struct fuse_req *req)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700673{
674 int err = 0;
675 if (req) {
Miklos Szeredidc008092015-07-01 16:25:58 +0200676 spin_lock(&req->waitq.lock);
Miklos Szeredi825d6d32015-07-01 16:25:58 +0200677 if (test_bit(FR_ABORTED, &req->flags))
Miklos Szeredi334f4852005-09-09 13:10:27 -0700678 err = -ENOENT;
679 else
Miklos Szeredi825d6d32015-07-01 16:25:58 +0200680 set_bit(FR_LOCKED, &req->flags);
Miklos Szeredidc008092015-07-01 16:25:58 +0200681 spin_unlock(&req->waitq.lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700682 }
683 return err;
684}
685
686/*
Miklos Szeredi0d8e84b2015-07-01 16:25:58 +0200687 * Unlock request. If it was aborted while locked, caller is responsible
688 * for unlocking and ending the request.
Miklos Szeredi334f4852005-09-09 13:10:27 -0700689 */
Miklos Szeredidc008092015-07-01 16:25:58 +0200690static int unlock_request(struct fuse_req *req)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700691{
Miklos Szeredi0d8e84b2015-07-01 16:25:58 +0200692 int err = 0;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700693 if (req) {
Miklos Szeredidc008092015-07-01 16:25:58 +0200694 spin_lock(&req->waitq.lock);
Miklos Szeredi825d6d32015-07-01 16:25:58 +0200695 if (test_bit(FR_ABORTED, &req->flags))
Miklos Szeredi0d8e84b2015-07-01 16:25:58 +0200696 err = -ENOENT;
697 else
Miklos Szeredi825d6d32015-07-01 16:25:58 +0200698 clear_bit(FR_LOCKED, &req->flags);
Miklos Szeredidc008092015-07-01 16:25:58 +0200699 spin_unlock(&req->waitq.lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700700 }
Miklos Szeredi0d8e84b2015-07-01 16:25:58 +0200701 return err;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700702}
703
704struct fuse_copy_state {
705 int write;
706 struct fuse_req *req;
Al Viro6c09e942015-04-03 22:06:08 -0400707 struct iov_iter *iter;
Miklos Szeredidd3bb142010-05-25 15:06:06 +0200708 struct pipe_buffer *pipebufs;
709 struct pipe_buffer *currbuf;
710 struct pipe_inode_info *pipe;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700711 unsigned long nr_segs;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700712 struct page *pg;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700713 unsigned len;
Miklos Szeredic55a01d2014-07-07 15:28:51 +0200714 unsigned offset;
Miklos Szeredice534fb2010-05-25 15:06:07 +0200715 unsigned move_pages:1;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700716};
717
Miklos Szeredidc008092015-07-01 16:25:58 +0200718static void fuse_copy_init(struct fuse_copy_state *cs, int write,
Al Viro6c09e942015-04-03 22:06:08 -0400719 struct iov_iter *iter)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700720{
721 memset(cs, 0, sizeof(*cs));
722 cs->write = write;
Al Viro6c09e942015-04-03 22:06:08 -0400723 cs->iter = iter;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700724}
725
726/* Unmap and put previous page of userspace buffer */
Miklos Szeredi8bfc0162006-01-16 22:14:28 -0800727static void fuse_copy_finish(struct fuse_copy_state *cs)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700728{
Miklos Szeredidd3bb142010-05-25 15:06:06 +0200729 if (cs->currbuf) {
730 struct pipe_buffer *buf = cs->currbuf;
731
Miklos Szeredic55a01d2014-07-07 15:28:51 +0200732 if (cs->write)
Miklos Szeredic3021622010-05-25 15:06:07 +0200733 buf->len = PAGE_SIZE - cs->len;
Miklos Szeredidd3bb142010-05-25 15:06:06 +0200734 cs->currbuf = NULL;
Miklos Szeredic55a01d2014-07-07 15:28:51 +0200735 } else if (cs->pg) {
Miklos Szeredi334f4852005-09-09 13:10:27 -0700736 if (cs->write) {
737 flush_dcache_page(cs->pg);
738 set_page_dirty_lock(cs->pg);
739 }
740 put_page(cs->pg);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700741 }
Miklos Szeredic55a01d2014-07-07 15:28:51 +0200742 cs->pg = NULL;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700743}
744
745/*
746 * Get another pagefull of userspace buffer, and map it to kernel
747 * address space, and lock request
748 */
749static int fuse_copy_fill(struct fuse_copy_state *cs)
750{
Miklos Szeredic55a01d2014-07-07 15:28:51 +0200751 struct page *page;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700752 int err;
753
Miklos Szeredidc008092015-07-01 16:25:58 +0200754 err = unlock_request(cs->req);
Miklos Szeredi0d8e84b2015-07-01 16:25:58 +0200755 if (err)
756 return err;
757
Miklos Szeredi334f4852005-09-09 13:10:27 -0700758 fuse_copy_finish(cs);
Miklos Szeredidd3bb142010-05-25 15:06:06 +0200759 if (cs->pipebufs) {
760 struct pipe_buffer *buf = cs->pipebufs;
761
Miklos Szeredic3021622010-05-25 15:06:07 +0200762 if (!cs->write) {
Miklos Szeredifba597d2016-09-27 10:45:12 +0200763 err = pipe_buf_confirm(cs->pipe, buf);
Miklos Szeredic3021622010-05-25 15:06:07 +0200764 if (err)
765 return err;
Miklos Szeredidd3bb142010-05-25 15:06:06 +0200766
Miklos Szeredic3021622010-05-25 15:06:07 +0200767 BUG_ON(!cs->nr_segs);
768 cs->currbuf = buf;
Miklos Szeredic55a01d2014-07-07 15:28:51 +0200769 cs->pg = buf->page;
770 cs->offset = buf->offset;
Miklos Szeredic3021622010-05-25 15:06:07 +0200771 cs->len = buf->len;
Miklos Szeredic3021622010-05-25 15:06:07 +0200772 cs->pipebufs++;
773 cs->nr_segs--;
774 } else {
Miklos Szeredic3021622010-05-25 15:06:07 +0200775 if (cs->nr_segs == cs->pipe->buffers)
776 return -EIO;
777
778 page = alloc_page(GFP_HIGHUSER);
779 if (!page)
780 return -ENOMEM;
781
782 buf->page = page;
783 buf->offset = 0;
784 buf->len = 0;
785
786 cs->currbuf = buf;
Miklos Szeredic55a01d2014-07-07 15:28:51 +0200787 cs->pg = page;
788 cs->offset = 0;
Miklos Szeredic3021622010-05-25 15:06:07 +0200789 cs->len = PAGE_SIZE;
790 cs->pipebufs++;
791 cs->nr_segs++;
792 }
Miklos Szeredidd3bb142010-05-25 15:06:06 +0200793 } else {
Al Viro6c09e942015-04-03 22:06:08 -0400794 size_t off;
795 err = iov_iter_get_pages(cs->iter, &page, PAGE_SIZE, 1, &off);
Miklos Szeredidd3bb142010-05-25 15:06:06 +0200796 if (err < 0)
797 return err;
Al Viro6c09e942015-04-03 22:06:08 -0400798 BUG_ON(!err);
799 cs->len = err;
800 cs->offset = off;
Miklos Szeredic55a01d2014-07-07 15:28:51 +0200801 cs->pg = page;
Al Viro6c09e942015-04-03 22:06:08 -0400802 iov_iter_advance(cs->iter, err);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700803 }
Miklos Szeredi334f4852005-09-09 13:10:27 -0700804
Miklos Szeredidc008092015-07-01 16:25:58 +0200805 return lock_request(cs->req);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700806}
807
808/* Do as much copy to/from userspace buffer as we can */
Miklos Szeredi8bfc0162006-01-16 22:14:28 -0800809static int fuse_copy_do(struct fuse_copy_state *cs, void **val, unsigned *size)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700810{
811 unsigned ncpy = min(*size, cs->len);
812 if (val) {
Miklos Szeredic55a01d2014-07-07 15:28:51 +0200813 void *pgaddr = kmap_atomic(cs->pg);
814 void *buf = pgaddr + cs->offset;
815
Miklos Szeredi334f4852005-09-09 13:10:27 -0700816 if (cs->write)
Miklos Szeredic55a01d2014-07-07 15:28:51 +0200817 memcpy(buf, *val, ncpy);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700818 else
Miklos Szeredic55a01d2014-07-07 15:28:51 +0200819 memcpy(*val, buf, ncpy);
820
821 kunmap_atomic(pgaddr);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700822 *val += ncpy;
823 }
824 *size -= ncpy;
825 cs->len -= ncpy;
Miklos Szeredic55a01d2014-07-07 15:28:51 +0200826 cs->offset += ncpy;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700827 return ncpy;
828}
829
Miklos Szeredice534fb2010-05-25 15:06:07 +0200830static int fuse_check_page(struct page *page)
831{
832 if (page_mapcount(page) ||
833 page->mapping != NULL ||
834 page_count(page) != 1 ||
835 (page->flags & PAGE_FLAGS_CHECK_AT_PREP &
836 ~(1 << PG_locked |
837 1 << PG_referenced |
838 1 << PG_uptodate |
839 1 << PG_lru |
840 1 << PG_active |
841 1 << PG_reclaim))) {
842 printk(KERN_WARNING "fuse: trying to steal weird page\n");
843 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);
844 return 1;
845 }
846 return 0;
847}
848
849static int fuse_try_move_page(struct fuse_copy_state *cs, struct page **pagep)
850{
851 int err;
852 struct page *oldpage = *pagep;
853 struct page *newpage;
854 struct pipe_buffer *buf = cs->pipebufs;
Miklos Szeredice534fb2010-05-25 15:06:07 +0200855
Miklos Szeredidc008092015-07-01 16:25:58 +0200856 err = unlock_request(cs->req);
Miklos Szeredi0d8e84b2015-07-01 16:25:58 +0200857 if (err)
858 return err;
859
Miklos Szeredice534fb2010-05-25 15:06:07 +0200860 fuse_copy_finish(cs);
861
Miklos Szeredifba597d2016-09-27 10:45:12 +0200862 err = pipe_buf_confirm(cs->pipe, buf);
Miklos Szeredice534fb2010-05-25 15:06:07 +0200863 if (err)
864 return err;
865
866 BUG_ON(!cs->nr_segs);
867 cs->currbuf = buf;
868 cs->len = buf->len;
869 cs->pipebufs++;
870 cs->nr_segs--;
871
872 if (cs->len != PAGE_SIZE)
873 goto out_fallback;
874
Miklos Szeredica76f5b2016-09-27 10:45:12 +0200875 if (pipe_buf_steal(cs->pipe, buf) != 0)
Miklos Szeredice534fb2010-05-25 15:06:07 +0200876 goto out_fallback;
877
878 newpage = buf->page;
879
Miklos Szerediaa991b32015-02-26 11:45:47 +0100880 if (!PageUptodate(newpage))
881 SetPageUptodate(newpage);
Miklos Szeredice534fb2010-05-25 15:06:07 +0200882
883 ClearPageMappedToDisk(newpage);
884
885 if (fuse_check_page(newpage) != 0)
886 goto out_fallback_unlock;
887
Miklos Szeredice534fb2010-05-25 15:06:07 +0200888 /*
889 * This is a new and locked page, it shouldn't be mapped or
890 * have any special flags on it
891 */
892 if (WARN_ON(page_mapped(oldpage)))
893 goto out_fallback_unlock;
894 if (WARN_ON(page_has_private(oldpage)))
895 goto out_fallback_unlock;
896 if (WARN_ON(PageDirty(oldpage) || PageWriteback(oldpage)))
897 goto out_fallback_unlock;
898 if (WARN_ON(PageMlocked(oldpage)))
899 goto out_fallback_unlock;
900
Miklos Szeredief6a3c62011-03-22 16:30:52 -0700901 err = replace_page_cache_page(oldpage, newpage, GFP_KERNEL);
Miklos Szeredice534fb2010-05-25 15:06:07 +0200902 if (err) {
Miklos Szeredief6a3c62011-03-22 16:30:52 -0700903 unlock_page(newpage);
904 return err;
Miklos Szeredice534fb2010-05-25 15:06:07 +0200905 }
Miklos Szeredief6a3c62011-03-22 16:30:52 -0700906
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300907 get_page(newpage);
Miklos Szeredice534fb2010-05-25 15:06:07 +0200908
909 if (!(buf->flags & PIPE_BUF_FLAG_LRU))
910 lru_cache_add_file(newpage);
911
912 err = 0;
Miklos Szeredidc008092015-07-01 16:25:58 +0200913 spin_lock(&cs->req->waitq.lock);
Miklos Szeredi825d6d32015-07-01 16:25:58 +0200914 if (test_bit(FR_ABORTED, &cs->req->flags))
Miklos Szeredice534fb2010-05-25 15:06:07 +0200915 err = -ENOENT;
916 else
917 *pagep = newpage;
Miklos Szeredidc008092015-07-01 16:25:58 +0200918 spin_unlock(&cs->req->waitq.lock);
Miklos Szeredice534fb2010-05-25 15:06:07 +0200919
920 if (err) {
921 unlock_page(newpage);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300922 put_page(newpage);
Miklos Szeredice534fb2010-05-25 15:06:07 +0200923 return err;
924 }
925
926 unlock_page(oldpage);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300927 put_page(oldpage);
Miklos Szeredice534fb2010-05-25 15:06:07 +0200928 cs->len = 0;
929
930 return 0;
931
932out_fallback_unlock:
933 unlock_page(newpage);
934out_fallback:
Miklos Szeredic55a01d2014-07-07 15:28:51 +0200935 cs->pg = buf->page;
936 cs->offset = buf->offset;
Miklos Szeredice534fb2010-05-25 15:06:07 +0200937
Miklos Szeredidc008092015-07-01 16:25:58 +0200938 err = lock_request(cs->req);
Miklos Szeredice534fb2010-05-25 15:06:07 +0200939 if (err)
940 return err;
941
942 return 1;
943}
944
Miklos Szeredic3021622010-05-25 15:06:07 +0200945static int fuse_ref_page(struct fuse_copy_state *cs, struct page *page,
946 unsigned offset, unsigned count)
947{
948 struct pipe_buffer *buf;
Miklos Szeredi0d8e84b2015-07-01 16:25:58 +0200949 int err;
Miklos Szeredic3021622010-05-25 15:06:07 +0200950
951 if (cs->nr_segs == cs->pipe->buffers)
952 return -EIO;
953
Miklos Szeredidc008092015-07-01 16:25:58 +0200954 err = unlock_request(cs->req);
Miklos Szeredi0d8e84b2015-07-01 16:25:58 +0200955 if (err)
956 return err;
957
Miklos Szeredic3021622010-05-25 15:06:07 +0200958 fuse_copy_finish(cs);
959
960 buf = cs->pipebufs;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300961 get_page(page);
Miklos Szeredic3021622010-05-25 15:06:07 +0200962 buf->page = page;
963 buf->offset = offset;
964 buf->len = count;
965
966 cs->pipebufs++;
967 cs->nr_segs++;
968 cs->len = 0;
969
970 return 0;
971}
972
Miklos Szeredi334f4852005-09-09 13:10:27 -0700973/*
974 * Copy a page in the request to/from the userspace buffer. Must be
975 * done atomically
976 */
Miklos Szeredice534fb2010-05-25 15:06:07 +0200977static int fuse_copy_page(struct fuse_copy_state *cs, struct page **pagep,
Miklos Szeredi8bfc0162006-01-16 22:14:28 -0800978 unsigned offset, unsigned count, int zeroing)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700979{
Miklos Szeredice534fb2010-05-25 15:06:07 +0200980 int err;
981 struct page *page = *pagep;
982
Miklos Szeredib6777c42010-10-26 14:22:27 -0700983 if (page && zeroing && count < PAGE_SIZE)
984 clear_highpage(page);
985
Miklos Szeredi334f4852005-09-09 13:10:27 -0700986 while (count) {
Miklos Szeredic3021622010-05-25 15:06:07 +0200987 if (cs->write && cs->pipebufs && page) {
988 return fuse_ref_page(cs, page, offset, count);
989 } else if (!cs->len) {
Miklos Szeredice534fb2010-05-25 15:06:07 +0200990 if (cs->move_pages && page &&
991 offset == 0 && count == PAGE_SIZE) {
992 err = fuse_try_move_page(cs, pagep);
993 if (err <= 0)
994 return err;
995 } else {
996 err = fuse_copy_fill(cs);
997 if (err)
998 return err;
999 }
Miklos Szeredi1729a162008-11-26 12:03:54 +01001000 }
Miklos Szeredi334f4852005-09-09 13:10:27 -07001001 if (page) {
Cong Wang2408f6e2011-11-25 23:14:30 +08001002 void *mapaddr = kmap_atomic(page);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001003 void *buf = mapaddr + offset;
1004 offset += fuse_copy_do(cs, &buf, &count);
Cong Wang2408f6e2011-11-25 23:14:30 +08001005 kunmap_atomic(mapaddr);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001006 } else
1007 offset += fuse_copy_do(cs, NULL, &count);
1008 }
1009 if (page && !cs->write)
1010 flush_dcache_page(page);
1011 return 0;
1012}
1013
1014/* Copy pages in the request to/from userspace buffer */
1015static int fuse_copy_pages(struct fuse_copy_state *cs, unsigned nbytes,
1016 int zeroing)
1017{
1018 unsigned i;
1019 struct fuse_req *req = cs->req;
Miklos Szeredi334f4852005-09-09 13:10:27 -07001020
1021 for (i = 0; i < req->num_pages && (nbytes || zeroing); i++) {
Miklos Szeredice534fb2010-05-25 15:06:07 +02001022 int err;
Maxim Patlasov85f40ae2012-10-26 19:49:33 +04001023 unsigned offset = req->page_descs[i].offset;
1024 unsigned count = min(nbytes, req->page_descs[i].length);
Miklos Szeredice534fb2010-05-25 15:06:07 +02001025
1026 err = fuse_copy_page(cs, &req->pages[i], offset, count,
1027 zeroing);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001028 if (err)
1029 return err;
1030
1031 nbytes -= count;
Miklos Szeredi334f4852005-09-09 13:10:27 -07001032 }
1033 return 0;
1034}
1035
1036/* Copy a single argument in the request to/from userspace buffer */
1037static int fuse_copy_one(struct fuse_copy_state *cs, void *val, unsigned size)
1038{
1039 while (size) {
Miklos Szeredi1729a162008-11-26 12:03:54 +01001040 if (!cs->len) {
1041 int err = fuse_copy_fill(cs);
1042 if (err)
1043 return err;
1044 }
Miklos Szeredi334f4852005-09-09 13:10:27 -07001045 fuse_copy_do(cs, &val, &size);
1046 }
1047 return 0;
1048}
1049
1050/* Copy request arguments to/from userspace buffer */
1051static int fuse_copy_args(struct fuse_copy_state *cs, unsigned numargs,
1052 unsigned argpages, struct fuse_arg *args,
1053 int zeroing)
1054{
1055 int err = 0;
1056 unsigned i;
1057
1058 for (i = 0; !err && i < numargs; i++) {
1059 struct fuse_arg *arg = &args[i];
1060 if (i == numargs - 1 && argpages)
1061 err = fuse_copy_pages(cs, arg->size, zeroing);
1062 else
1063 err = fuse_copy_one(cs, arg->value, arg->size);
1064 }
1065 return err;
1066}
1067
Miklos Szeredif88996a2015-07-01 16:26:01 +02001068static int forget_pending(struct fuse_iqueue *fiq)
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001069{
Miklos Szeredif88996a2015-07-01 16:26:01 +02001070 return fiq->forget_list_head.next != NULL;
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001071}
1072
Miklos Szeredif88996a2015-07-01 16:26:01 +02001073static int request_pending(struct fuse_iqueue *fiq)
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001074{
Miklos Szeredif88996a2015-07-01 16:26:01 +02001075 return !list_empty(&fiq->pending) || !list_empty(&fiq->interrupts) ||
1076 forget_pending(fiq);
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001077}
1078
Miklos Szeredi334f4852005-09-09 13:10:27 -07001079/*
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001080 * Transfer an interrupt request to userspace
1081 *
1082 * Unlike other requests this is assembled on demand, without a need
1083 * to allocate a separate fuse_req structure.
1084 *
Miklos Szeredifd22d622015-07-01 16:26:03 +02001085 * Called with fiq->waitq.lock held, releases it
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001086 */
Miklos Szeredifd22d622015-07-01 16:26:03 +02001087static int fuse_read_interrupt(struct fuse_iqueue *fiq,
1088 struct fuse_copy_state *cs,
Miklos Szeredic3021622010-05-25 15:06:07 +02001089 size_t nbytes, struct fuse_req *req)
Miklos Szeredifd22d622015-07-01 16:26:03 +02001090__releases(fiq->waitq.lock)
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001091{
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001092 struct fuse_in_header ih;
1093 struct fuse_interrupt_in arg;
1094 unsigned reqsize = sizeof(ih) + sizeof(arg);
1095 int err;
1096
1097 list_del_init(&req->intr_entry);
Miklos Szeredi4ce60812015-07-01 16:26:02 +02001098 req->intr_unique = fuse_get_unique(fiq);
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001099 memset(&ih, 0, sizeof(ih));
1100 memset(&arg, 0, sizeof(arg));
1101 ih.len = reqsize;
1102 ih.opcode = FUSE_INTERRUPT;
1103 ih.unique = req->intr_unique;
1104 arg.unique = req->in.h.unique;
1105
Miklos Szeredi4ce60812015-07-01 16:26:02 +02001106 spin_unlock(&fiq->waitq.lock);
Miklos Szeredic3021622010-05-25 15:06:07 +02001107 if (nbytes < reqsize)
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001108 return -EINVAL;
1109
Miklos Szeredic3021622010-05-25 15:06:07 +02001110 err = fuse_copy_one(cs, &ih, sizeof(ih));
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001111 if (!err)
Miklos Szeredic3021622010-05-25 15:06:07 +02001112 err = fuse_copy_one(cs, &arg, sizeof(arg));
1113 fuse_copy_finish(cs);
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001114
1115 return err ? err : reqsize;
1116}
1117
Miklos Szeredif88996a2015-07-01 16:26:01 +02001118static struct fuse_forget_link *dequeue_forget(struct fuse_iqueue *fiq,
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001119 unsigned max,
1120 unsigned *countp)
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001121{
Miklos Szeredif88996a2015-07-01 16:26:01 +02001122 struct fuse_forget_link *head = fiq->forget_list_head.next;
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001123 struct fuse_forget_link **newhead = &head;
1124 unsigned count;
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001125
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001126 for (count = 0; *newhead != NULL && count < max; count++)
1127 newhead = &(*newhead)->next;
1128
Miklos Szeredif88996a2015-07-01 16:26:01 +02001129 fiq->forget_list_head.next = *newhead;
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001130 *newhead = NULL;
Miklos Szeredif88996a2015-07-01 16:26:01 +02001131 if (fiq->forget_list_head.next == NULL)
1132 fiq->forget_list_tail = &fiq->forget_list_head;
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001133
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001134 if (countp != NULL)
1135 *countp = count;
1136
1137 return head;
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001138}
1139
Miklos Szeredifd22d622015-07-01 16:26:03 +02001140static int fuse_read_single_forget(struct fuse_iqueue *fiq,
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001141 struct fuse_copy_state *cs,
1142 size_t nbytes)
Miklos Szeredifd22d622015-07-01 16:26:03 +02001143__releases(fiq->waitq.lock)
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001144{
1145 int err;
Miklos Szeredif88996a2015-07-01 16:26:01 +02001146 struct fuse_forget_link *forget = dequeue_forget(fiq, 1, NULL);
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001147 struct fuse_forget_in arg = {
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001148 .nlookup = forget->forget_one.nlookup,
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001149 };
1150 struct fuse_in_header ih = {
1151 .opcode = FUSE_FORGET,
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001152 .nodeid = forget->forget_one.nodeid,
Miklos Szeredif88996a2015-07-01 16:26:01 +02001153 .unique = fuse_get_unique(fiq),
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001154 .len = sizeof(ih) + sizeof(arg),
1155 };
1156
Miklos Szeredi4ce60812015-07-01 16:26:02 +02001157 spin_unlock(&fiq->waitq.lock);
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001158 kfree(forget);
1159 if (nbytes < ih.len)
1160 return -EINVAL;
1161
1162 err = fuse_copy_one(cs, &ih, sizeof(ih));
1163 if (!err)
1164 err = fuse_copy_one(cs, &arg, sizeof(arg));
1165 fuse_copy_finish(cs);
1166
1167 if (err)
1168 return err;
1169
1170 return ih.len;
1171}
1172
Miklos Szeredifd22d622015-07-01 16:26:03 +02001173static int fuse_read_batch_forget(struct fuse_iqueue *fiq,
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001174 struct fuse_copy_state *cs, size_t nbytes)
Miklos Szeredifd22d622015-07-01 16:26:03 +02001175__releases(fiq->waitq.lock)
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001176{
1177 int err;
1178 unsigned max_forgets;
1179 unsigned count;
1180 struct fuse_forget_link *head;
1181 struct fuse_batch_forget_in arg = { .count = 0 };
1182 struct fuse_in_header ih = {
1183 .opcode = FUSE_BATCH_FORGET,
Miklos Szeredif88996a2015-07-01 16:26:01 +02001184 .unique = fuse_get_unique(fiq),
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001185 .len = sizeof(ih) + sizeof(arg),
1186 };
1187
1188 if (nbytes < ih.len) {
Miklos Szeredi4ce60812015-07-01 16:26:02 +02001189 spin_unlock(&fiq->waitq.lock);
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001190 return -EINVAL;
1191 }
1192
1193 max_forgets = (nbytes - ih.len) / sizeof(struct fuse_forget_one);
Miklos Szeredif88996a2015-07-01 16:26:01 +02001194 head = dequeue_forget(fiq, max_forgets, &count);
Miklos Szeredi4ce60812015-07-01 16:26:02 +02001195 spin_unlock(&fiq->waitq.lock);
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001196
1197 arg.count = count;
1198 ih.len += count * sizeof(struct fuse_forget_one);
1199 err = fuse_copy_one(cs, &ih, sizeof(ih));
1200 if (!err)
1201 err = fuse_copy_one(cs, &arg, sizeof(arg));
1202
1203 while (head) {
1204 struct fuse_forget_link *forget = head;
1205
1206 if (!err) {
1207 err = fuse_copy_one(cs, &forget->forget_one,
1208 sizeof(forget->forget_one));
1209 }
1210 head = forget->next;
1211 kfree(forget);
1212 }
1213
1214 fuse_copy_finish(cs);
1215
1216 if (err)
1217 return err;
1218
1219 return ih.len;
1220}
1221
Miklos Szeredifd22d622015-07-01 16:26:03 +02001222static int fuse_read_forget(struct fuse_conn *fc, struct fuse_iqueue *fiq,
1223 struct fuse_copy_state *cs,
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001224 size_t nbytes)
Miklos Szeredifd22d622015-07-01 16:26:03 +02001225__releases(fiq->waitq.lock)
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001226{
Miklos Szeredif88996a2015-07-01 16:26:01 +02001227 if (fc->minor < 16 || fiq->forget_list_head.next->next == NULL)
Miklos Szeredifd22d622015-07-01 16:26:03 +02001228 return fuse_read_single_forget(fiq, cs, nbytes);
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001229 else
Miklos Szeredifd22d622015-07-01 16:26:03 +02001230 return fuse_read_batch_forget(fiq, cs, nbytes);
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001231}
1232
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001233/*
Miklos Szeredi334f4852005-09-09 13:10:27 -07001234 * Read a single request into the userspace filesystem's buffer. This
1235 * function waits until a request is available, then removes it from
1236 * the pending list and copies request data to userspace buffer. If
Miklos Szeredif9a28422006-06-25 05:48:53 -07001237 * no reply is needed (FORGET) or request has been aborted or there
1238 * was an error during the copying then it's finished by calling
Miklos Szeredi334f4852005-09-09 13:10:27 -07001239 * request_end(). Otherwise add it to the processing list, and set
1240 * the 'sent' flag.
1241 */
Miklos Szeredic36960462015-07-01 16:26:09 +02001242static ssize_t fuse_dev_do_read(struct fuse_dev *fud, struct file *file,
Miklos Szeredic3021622010-05-25 15:06:07 +02001243 struct fuse_copy_state *cs, size_t nbytes)
Miklos Szeredi334f4852005-09-09 13:10:27 -07001244{
Miklos Szeredi82cbdcd2015-07-01 16:26:05 +02001245 ssize_t err;
Miklos Szeredic36960462015-07-01 16:26:09 +02001246 struct fuse_conn *fc = fud->fc;
Miklos Szeredif88996a2015-07-01 16:26:01 +02001247 struct fuse_iqueue *fiq = &fc->iq;
Miklos Szeredic36960462015-07-01 16:26:09 +02001248 struct fuse_pqueue *fpq = &fud->pq;
Miklos Szeredi334f4852005-09-09 13:10:27 -07001249 struct fuse_req *req;
1250 struct fuse_in *in;
Miklos Szeredi334f4852005-09-09 13:10:27 -07001251 unsigned reqsize;
1252
Miklos Szeredi1d3d7522006-01-06 00:19:40 -08001253 restart:
Miklos Szeredi4ce60812015-07-01 16:26:02 +02001254 spin_lock(&fiq->waitq.lock);
Jeff Dikee5ac1d12006-04-10 22:54:53 -07001255 err = -EAGAIN;
Miklos Szeredie16714d2015-07-01 16:26:01 +02001256 if ((file->f_flags & O_NONBLOCK) && fiq->connected &&
Miklos Szeredif88996a2015-07-01 16:26:01 +02001257 !request_pending(fiq))
Jeff Dikee5ac1d12006-04-10 22:54:53 -07001258 goto err_unlock;
1259
Miklos Szeredi52509212015-07-01 16:26:03 +02001260 err = wait_event_interruptible_exclusive_locked(fiq->waitq,
1261 !fiq->connected || request_pending(fiq));
1262 if (err)
1263 goto err_unlock;
1264
Miklos Szeredi334f4852005-09-09 13:10:27 -07001265 err = -ENODEV;
Miklos Szeredie16714d2015-07-01 16:26:01 +02001266 if (!fiq->connected)
Miklos Szeredi334f4852005-09-09 13:10:27 -07001267 goto err_unlock;
Miklos Szeredi334f4852005-09-09 13:10:27 -07001268
Miklos Szeredif88996a2015-07-01 16:26:01 +02001269 if (!list_empty(&fiq->interrupts)) {
1270 req = list_entry(fiq->interrupts.next, struct fuse_req,
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001271 intr_entry);
Miklos Szeredifd22d622015-07-01 16:26:03 +02001272 return fuse_read_interrupt(fiq, cs, nbytes, req);
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001273 }
1274
Miklos Szeredif88996a2015-07-01 16:26:01 +02001275 if (forget_pending(fiq)) {
1276 if (list_empty(&fiq->pending) || fiq->forget_batch-- > 0)
Miklos Szeredifd22d622015-07-01 16:26:03 +02001277 return fuse_read_forget(fc, fiq, cs, nbytes);
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001278
Miklos Szeredif88996a2015-07-01 16:26:01 +02001279 if (fiq->forget_batch <= -8)
1280 fiq->forget_batch = 16;
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001281 }
1282
Miklos Szeredif88996a2015-07-01 16:26:01 +02001283 req = list_entry(fiq->pending.next, struct fuse_req, list);
Miklos Szeredi33e14b42015-07-01 16:26:01 +02001284 clear_bit(FR_PENDING, &req->flags);
Miklos Szeredief759252015-07-01 16:26:02 +02001285 list_del_init(&req->list);
Miklos Szeredi4ce60812015-07-01 16:26:02 +02001286 spin_unlock(&fiq->waitq.lock);
1287
Miklos Szeredi334f4852005-09-09 13:10:27 -07001288 in = &req->in;
Miklos Szeredi1d3d7522006-01-06 00:19:40 -08001289 reqsize = in->h.len;
1290 /* If request is too large, reply with an error and restart the read */
Miklos Szeredic3021622010-05-25 15:06:07 +02001291 if (nbytes < reqsize) {
Miklos Szeredi1d3d7522006-01-06 00:19:40 -08001292 req->out.h.error = -EIO;
1293 /* SETXATTR is special, since it may contain too large data */
1294 if (in->h.opcode == FUSE_SETXATTR)
1295 req->out.h.error = -E2BIG;
1296 request_end(fc, req);
1297 goto restart;
Miklos Szeredi334f4852005-09-09 13:10:27 -07001298 }
Miklos Szeredi45a91cb2015-07-01 16:26:06 +02001299 spin_lock(&fpq->lock);
Miklos Szeredi82cbdcd2015-07-01 16:26:05 +02001300 list_add(&req->list, &fpq->io);
Miklos Szeredi45a91cb2015-07-01 16:26:06 +02001301 spin_unlock(&fpq->lock);
Miklos Szeredic3021622010-05-25 15:06:07 +02001302 cs->req = req;
1303 err = fuse_copy_one(cs, &in->h, sizeof(in->h));
Miklos Szeredi1d3d7522006-01-06 00:19:40 -08001304 if (!err)
Miklos Szeredic3021622010-05-25 15:06:07 +02001305 err = fuse_copy_args(cs, in->numargs, in->argpages,
Miklos Szeredi1d3d7522006-01-06 00:19:40 -08001306 (struct fuse_arg *) in->args, 0);
Miklos Szeredic3021622010-05-25 15:06:07 +02001307 fuse_copy_finish(cs);
Miklos Szeredi45a91cb2015-07-01 16:26:06 +02001308 spin_lock(&fpq->lock);
Miklos Szeredi825d6d32015-07-01 16:25:58 +02001309 clear_bit(FR_LOCKED, &req->flags);
Miklos Szeredie96edd92015-07-01 16:26:04 +02001310 if (!fpq->connected) {
Miklos Szeredi82cbdcd2015-07-01 16:26:05 +02001311 err = -ENODEV;
1312 goto out_end;
Miklos Szeredic9c9d7d2007-10-16 23:31:05 -07001313 }
Miklos Szeredi334f4852005-09-09 13:10:27 -07001314 if (err) {
Miklos Szeredic9c9d7d2007-10-16 23:31:05 -07001315 req->out.h.error = -EIO;
Miklos Szeredi82cbdcd2015-07-01 16:26:05 +02001316 goto out_end;
Miklos Szeredi334f4852005-09-09 13:10:27 -07001317 }
Miklos Szeredi825d6d32015-07-01 16:25:58 +02001318 if (!test_bit(FR_ISREPLY, &req->flags)) {
Miklos Szeredi82cbdcd2015-07-01 16:26:05 +02001319 err = reqsize;
1320 goto out_end;
Miklos Szeredi334f4852005-09-09 13:10:27 -07001321 }
Miklos Szeredi82cbdcd2015-07-01 16:26:05 +02001322 list_move_tail(&req->list, &fpq->processing);
Kirill Tkhai7996b1c2018-09-25 12:28:55 +03001323 __fuse_get_request(req);
Miklos Szeredi82cbdcd2015-07-01 16:26:05 +02001324 set_bit(FR_SENT, &req->flags);
Miklos Szeredic31ccf12018-09-28 16:43:22 +02001325 spin_unlock(&fpq->lock);
Miklos Szeredi82cbdcd2015-07-01 16:26:05 +02001326 /* matches barrier in request_wait_answer() */
1327 smp_mb__after_atomic();
1328 if (test_bit(FR_INTERRUPTED, &req->flags))
1329 queue_interrupt(fiq, req);
Kirill Tkhai7996b1c2018-09-25 12:28:55 +03001330 fuse_put_request(fc, req);
Miklos Szeredi82cbdcd2015-07-01 16:26:05 +02001331
Miklos Szeredi334f4852005-09-09 13:10:27 -07001332 return reqsize;
1333
Miklos Szeredi82cbdcd2015-07-01 16:26:05 +02001334out_end:
Miklos Szeredi77cd9d42015-07-01 16:26:06 +02001335 if (!test_bit(FR_PRIVATE, &req->flags))
1336 list_del_init(&req->list);
Miklos Szeredi45a91cb2015-07-01 16:26:06 +02001337 spin_unlock(&fpq->lock);
Miklos Szeredi82cbdcd2015-07-01 16:26:05 +02001338 request_end(fc, req);
1339 return err;
1340
Miklos Szeredi334f4852005-09-09 13:10:27 -07001341 err_unlock:
Miklos Szeredi4ce60812015-07-01 16:26:02 +02001342 spin_unlock(&fiq->waitq.lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001343 return err;
1344}
1345
Tom Van Braeckel94e4fe22015-01-12 05:22:16 +01001346static int fuse_dev_open(struct inode *inode, struct file *file)
1347{
1348 /*
1349 * The fuse device's file's private_data is used to hold
1350 * the fuse_conn(ection) when it is mounted, and is used to
1351 * keep track of whether the file has been mounted already.
1352 */
1353 file->private_data = NULL;
1354 return 0;
1355}
1356
Al Virofbdbacc2015-04-03 21:53:39 -04001357static ssize_t fuse_dev_read(struct kiocb *iocb, struct iov_iter *to)
Miklos Szeredic3021622010-05-25 15:06:07 +02001358{
1359 struct fuse_copy_state cs;
1360 struct file *file = iocb->ki_filp;
Miklos Szeredicc080e92015-07-01 16:26:08 +02001361 struct fuse_dev *fud = fuse_get_dev(file);
1362
1363 if (!fud)
Miklos Szeredic3021622010-05-25 15:06:07 +02001364 return -EPERM;
1365
Al Virofbdbacc2015-04-03 21:53:39 -04001366 if (!iter_is_iovec(to))
1367 return -EINVAL;
Miklos Szeredic3021622010-05-25 15:06:07 +02001368
Miklos Szeredidc008092015-07-01 16:25:58 +02001369 fuse_copy_init(&cs, 1, to);
Al Virofbdbacc2015-04-03 21:53:39 -04001370
Miklos Szeredic36960462015-07-01 16:26:09 +02001371 return fuse_dev_do_read(fud, file, &cs, iov_iter_count(to));
Miklos Szeredic3021622010-05-25 15:06:07 +02001372}
1373
Miklos Szeredic3021622010-05-25 15:06:07 +02001374static ssize_t fuse_dev_splice_read(struct file *in, loff_t *ppos,
1375 struct pipe_inode_info *pipe,
1376 size_t len, unsigned int flags)
1377{
Al Virod82718e2016-09-17 22:56:25 -04001378 int total, ret;
Miklos Szeredic3021622010-05-25 15:06:07 +02001379 int page_nr = 0;
Miklos Szeredic3021622010-05-25 15:06:07 +02001380 struct pipe_buffer *bufs;
1381 struct fuse_copy_state cs;
Miklos Szeredicc080e92015-07-01 16:26:08 +02001382 struct fuse_dev *fud = fuse_get_dev(in);
1383
1384 if (!fud)
Miklos Szeredic3021622010-05-25 15:06:07 +02001385 return -EPERM;
1386
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001387 bufs = kmalloc(pipe->buffers * sizeof(struct pipe_buffer), 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 Szeredi80a04772017-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
1420 kfree(bufs);
1421 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 Gormanb745bc82014-06-04 16:10:22 -07001662 release_pages(req->pages, req->num_pages, false);
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
Kirill Smelkov4edf9072019-03-27 10:15:19 +00001681 num = min(outarg->size, fc->max_write);
Maxim Patlasov4d53dc92012-10-26 19:48:42 +04001682 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;
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001698 req->end = fuse_retrieve_end;
1699
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001700 index = outarg->offset >> PAGE_SHIFT;
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001701
Maxim Patlasov4d53dc92012-10-26 19:48:42 +04001702 while (num && req->num_pages < num_pages) {
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001703 struct page *page;
1704 unsigned int this_num;
1705
1706 page = find_get_page(mapping, index);
1707 if (!page)
1708 break;
1709
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001710 this_num = min_t(unsigned, num, PAGE_SIZE - offset);
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001711 req->pages[req->num_pages] = page;
Miklos Szeredi64702de2019-01-16 10:27:59 +01001712 req->page_descs[req->num_pages].offset = offset;
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);
Miklos Szeredid180fee2018-11-09 15:52:16 +01001728 if (err) {
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001729 fuse_retrieve_end(fc, req);
Miklos Szeredid180fee2018-11-09 15:52:16 +01001730 fuse_put_request(fc, req);
1731 }
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001732
1733 return err;
1734}
1735
1736static int fuse_notify_retrieve(struct fuse_conn *fc, unsigned int size,
1737 struct fuse_copy_state *cs)
1738{
1739 struct fuse_notify_retrieve_out outarg;
1740 struct inode *inode;
1741 int err;
1742
1743 err = -EINVAL;
1744 if (size != sizeof(outarg))
1745 goto copy_finish;
1746
1747 err = fuse_copy_one(cs, &outarg, sizeof(outarg));
1748 if (err)
1749 goto copy_finish;
1750
1751 fuse_copy_finish(cs);
1752
1753 down_read(&fc->killsb);
1754 err = -ENOENT;
1755 if (fc->sb) {
1756 u64 nodeid = outarg.nodeid;
1757
1758 inode = ilookup5(fc->sb, nodeid, fuse_inode_eq, &nodeid);
1759 if (inode) {
1760 err = fuse_retrieve(fc, inode, &outarg);
1761 iput(inode);
1762 }
1763 }
1764 up_read(&fc->killsb);
1765
1766 return err;
1767
1768copy_finish:
1769 fuse_copy_finish(cs);
1770 return err;
1771}
1772
Tejun Heo85993962008-11-26 12:03:55 +01001773static int fuse_notify(struct fuse_conn *fc, enum fuse_notify_code code,
1774 unsigned int size, struct fuse_copy_state *cs)
1775{
Miklos Szeredi0d278362015-02-26 11:45:47 +01001776 /* Don't try to move pages (yet) */
1777 cs->move_pages = 0;
1778
Tejun Heo85993962008-11-26 12:03:55 +01001779 switch (code) {
Tejun Heo95668a62008-11-26 12:03:55 +01001780 case FUSE_NOTIFY_POLL:
1781 return fuse_notify_poll(fc, size, cs);
1782
John Muir3b463ae2009-05-31 11:13:57 -04001783 case FUSE_NOTIFY_INVAL_INODE:
1784 return fuse_notify_inval_inode(fc, size, cs);
1785
1786 case FUSE_NOTIFY_INVAL_ENTRY:
1787 return fuse_notify_inval_entry(fc, size, cs);
1788
Miklos Szeredia1d75f22010-07-12 14:41:40 +02001789 case FUSE_NOTIFY_STORE:
1790 return fuse_notify_store(fc, size, cs);
1791
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001792 case FUSE_NOTIFY_RETRIEVE:
1793 return fuse_notify_retrieve(fc, size, cs);
1794
John Muir451d0f52011-12-06 21:50:06 +01001795 case FUSE_NOTIFY_DELETE:
1796 return fuse_notify_delete(fc, size, cs);
1797
Tejun Heo85993962008-11-26 12:03:55 +01001798 default:
Miklos Szeredif6d47a12009-01-26 15:00:59 +01001799 fuse_copy_finish(cs);
Tejun Heo85993962008-11-26 12:03:55 +01001800 return -EINVAL;
1801 }
1802}
1803
Miklos Szeredi334f4852005-09-09 13:10:27 -07001804/* Look up request on processing list by unique ID */
Miklos Szeredi3a2b5b92015-07-01 16:26:04 +02001805static struct fuse_req *request_find(struct fuse_pqueue *fpq, u64 unique)
Miklos Szeredi334f4852005-09-09 13:10:27 -07001806{
Dong Fang05726ac2013-07-30 22:50:01 -04001807 struct fuse_req *req;
Miklos Szeredi334f4852005-09-09 13:10:27 -07001808
Miklos Szeredi3a2b5b92015-07-01 16:26:04 +02001809 list_for_each_entry(req, &fpq->processing, list) {
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001810 if (req->in.h.unique == unique || req->intr_unique == unique)
Miklos Szeredi334f4852005-09-09 13:10:27 -07001811 return req;
1812 }
1813 return NULL;
1814}
1815
1816static int copy_out_args(struct fuse_copy_state *cs, struct fuse_out *out,
1817 unsigned nbytes)
1818{
1819 unsigned reqsize = sizeof(struct fuse_out_header);
1820
1821 if (out->h.error)
1822 return nbytes != reqsize ? -EINVAL : 0;
1823
1824 reqsize += len_args(out->numargs, out->args);
1825
1826 if (reqsize < nbytes || (reqsize > nbytes && !out->argvar))
1827 return -EINVAL;
1828 else if (reqsize > nbytes) {
1829 struct fuse_arg *lastarg = &out->args[out->numargs-1];
1830 unsigned diffsize = reqsize - nbytes;
1831 if (diffsize > lastarg->size)
1832 return -EINVAL;
1833 lastarg->size -= diffsize;
1834 }
1835 return fuse_copy_args(cs, out->numargs, out->argpages, out->args,
1836 out->page_zeroing);
1837}
1838
1839/*
1840 * Write a single reply to a request. First the header is copied from
1841 * the write buffer. The request is then searched on the processing
1842 * list by the unique ID found in the header. If found, then remove
1843 * it from the list and copy the rest of the buffer to the request.
1844 * The request is finished by calling request_end()
1845 */
Miklos Szeredic36960462015-07-01 16:26:09 +02001846static ssize_t fuse_dev_do_write(struct fuse_dev *fud,
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001847 struct fuse_copy_state *cs, size_t nbytes)
Miklos Szeredi334f4852005-09-09 13:10:27 -07001848{
1849 int err;
Miklos Szeredic36960462015-07-01 16:26:09 +02001850 struct fuse_conn *fc = fud->fc;
1851 struct fuse_pqueue *fpq = &fud->pq;
Miklos Szeredi334f4852005-09-09 13:10:27 -07001852 struct fuse_req *req;
1853 struct fuse_out_header oh;
Miklos Szeredi334f4852005-09-09 13:10:27 -07001854
Miklos Szeredi334f4852005-09-09 13:10:27 -07001855 if (nbytes < sizeof(struct fuse_out_header))
1856 return -EINVAL;
1857
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001858 err = fuse_copy_one(cs, &oh, sizeof(oh));
Miklos Szeredi334f4852005-09-09 13:10:27 -07001859 if (err)
1860 goto err_finish;
Tejun Heo85993962008-11-26 12:03:55 +01001861
Miklos Szeredi334f4852005-09-09 13:10:27 -07001862 err = -EINVAL;
Tejun Heo85993962008-11-26 12:03:55 +01001863 if (oh.len != nbytes)
1864 goto err_finish;
1865
1866 /*
1867 * Zero oh.unique indicates unsolicited notification message
1868 * and error contains notification code.
1869 */
1870 if (!oh.unique) {
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001871 err = fuse_notify(fc, oh.error, nbytes - sizeof(oh), cs);
Tejun Heo85993962008-11-26 12:03:55 +01001872 return err ? err : nbytes;
1873 }
1874
1875 err = -EINVAL;
1876 if (oh.error <= -1000 || oh.error > 0)
Miklos Szeredi334f4852005-09-09 13:10:27 -07001877 goto err_finish;
1878
Miklos Szeredi45a91cb2015-07-01 16:26:06 +02001879 spin_lock(&fpq->lock);
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08001880 err = -ENOENT;
Miklos Szeredie96edd92015-07-01 16:26:04 +02001881 if (!fpq->connected)
Miklos Szeredi45a91cb2015-07-01 16:26:06 +02001882 goto err_unlock_pq;
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08001883
Miklos Szeredi3a2b5b92015-07-01 16:26:04 +02001884 req = request_find(fpq, oh.unique);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001885 if (!req)
Miklos Szeredi45a91cb2015-07-01 16:26:06 +02001886 goto err_unlock_pq;
Miklos Szeredi334f4852005-09-09 13:10:27 -07001887
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001888 /* Is it an interrupt reply? */
1889 if (req->intr_unique == oh.unique) {
Kirill Tkhai0799a932018-09-25 12:52:42 +03001890 __fuse_get_request(req);
Miklos Szeredi45a91cb2015-07-01 16:26:06 +02001891 spin_unlock(&fpq->lock);
1892
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001893 err = -EINVAL;
Kirill Tkhai0799a932018-09-25 12:52:42 +03001894 if (nbytes != sizeof(struct fuse_out_header)) {
1895 fuse_put_request(fc, req);
Miklos Szeredi46c34a32015-07-01 16:26:07 +02001896 goto err_finish;
Kirill Tkhai0799a932018-09-25 12:52:42 +03001897 }
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001898
1899 if (oh.error == -ENOSYS)
1900 fc->no_interrupt = 1;
1901 else if (oh.error == -EAGAIN)
Miklos Szeredif88996a2015-07-01 16:26:01 +02001902 queue_interrupt(&fc->iq, req);
Kirill Tkhai0799a932018-09-25 12:52:42 +03001903 fuse_put_request(fc, req);
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001904
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001905 fuse_copy_finish(cs);
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001906 return nbytes;
1907 }
1908
Miklos Szeredi33e14b42015-07-01 16:26:01 +02001909 clear_bit(FR_SENT, &req->flags);
Miklos Szeredi3a2b5b92015-07-01 16:26:04 +02001910 list_move(&req->list, &fpq->io);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001911 req->out.h = oh;
Miklos Szeredi825d6d32015-07-01 16:25:58 +02001912 set_bit(FR_LOCKED, &req->flags);
Miklos Szeredi45a91cb2015-07-01 16:26:06 +02001913 spin_unlock(&fpq->lock);
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001914 cs->req = req;
Miklos Szeredice534fb2010-05-25 15:06:07 +02001915 if (!req->out.page_replace)
1916 cs->move_pages = 0;
Miklos Szeredi334f4852005-09-09 13:10:27 -07001917
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001918 err = copy_out_args(cs, &req->out, nbytes);
Daniel Rosenbergfac99a72016-04-22 00:00:48 -07001919 if (req->in.h.opcode == FUSE_CANONICAL_PATH) {
Ritesh Harjani4fb542f2018-03-19 16:03:09 +05301920 char *path = (char *)req->out.args[0].value;
1921
1922 path[req->out.args[0].size - 1] = 0;
1923 req->out.h.error = kern_path(path, 0, req->canonical_path);
Daniel Rosenbergfac99a72016-04-22 00:00:48 -07001924 }
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001925 fuse_copy_finish(cs);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001926
Nikhilesh Reddy759832b2017-07-26 09:41:57 +05301927 fuse_setup_passthrough(fc, req);
1928
1929
Miklos Szeredi45a91cb2015-07-01 16:26:06 +02001930 spin_lock(&fpq->lock);
Miklos Szeredi825d6d32015-07-01 16:25:58 +02001931 clear_bit(FR_LOCKED, &req->flags);
Miklos Szeredie96edd92015-07-01 16:26:04 +02001932 if (!fpq->connected)
Miklos Szeredi0d8e84b2015-07-01 16:25:58 +02001933 err = -ENOENT;
1934 else if (err)
Miklos Szeredi334f4852005-09-09 13:10:27 -07001935 req->out.h.error = -EIO;
Miklos Szeredi77cd9d42015-07-01 16:26:06 +02001936 if (!test_bit(FR_PRIVATE, &req->flags))
1937 list_del_init(&req->list);
Miklos Szeredi45a91cb2015-07-01 16:26:06 +02001938 spin_unlock(&fpq->lock);
Miklos Szeredi46c34a32015-07-01 16:26:07 +02001939
Miklos Szeredi334f4852005-09-09 13:10:27 -07001940 request_end(fc, req);
1941
1942 return err ? err : nbytes;
1943
Miklos Szeredi45a91cb2015-07-01 16:26:06 +02001944 err_unlock_pq:
1945 spin_unlock(&fpq->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001946 err_finish:
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001947 fuse_copy_finish(cs);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001948 return err;
1949}
1950
Al Virofbdbacc2015-04-03 21:53:39 -04001951static ssize_t fuse_dev_write(struct kiocb *iocb, struct iov_iter *from)
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001952{
1953 struct fuse_copy_state cs;
Miklos Szeredicc080e92015-07-01 16:26:08 +02001954 struct fuse_dev *fud = fuse_get_dev(iocb->ki_filp);
1955
1956 if (!fud)
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001957 return -EPERM;
1958
Al Virofbdbacc2015-04-03 21:53:39 -04001959 if (!iter_is_iovec(from))
1960 return -EINVAL;
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001961
Miklos Szeredidc008092015-07-01 16:25:58 +02001962 fuse_copy_init(&cs, 0, from);
Al Virofbdbacc2015-04-03 21:53:39 -04001963
Miklos Szeredic36960462015-07-01 16:26:09 +02001964 return fuse_dev_do_write(fud, &cs, iov_iter_count(from));
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001965}
1966
1967static ssize_t fuse_dev_splice_write(struct pipe_inode_info *pipe,
1968 struct file *out, loff_t *ppos,
1969 size_t len, unsigned int flags)
1970{
1971 unsigned nbuf;
1972 unsigned idx;
1973 struct pipe_buffer *bufs;
1974 struct fuse_copy_state cs;
Miklos Szeredicc080e92015-07-01 16:26:08 +02001975 struct fuse_dev *fud;
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001976 size_t rem;
1977 ssize_t ret;
1978
Miklos Szeredicc080e92015-07-01 16:26:08 +02001979 fud = fuse_get_dev(out);
1980 if (!fud)
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001981 return -EPERM;
1982
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001983 pipe_lock(pipe);
Andrey Ryabinin67a9e482018-07-17 19:00:33 +03001984
1985 bufs = kmalloc(pipe->buffers * sizeof(struct pipe_buffer), GFP_KERNEL);
1986 if (!bufs) {
1987 pipe_unlock(pipe);
1988 return -ENOMEM;
1989 }
1990
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001991 nbuf = 0;
1992 rem = 0;
1993 for (idx = 0; idx < pipe->nrbufs && rem < len; idx++)
1994 rem += pipe->bufs[(pipe->curbuf + idx) & (pipe->buffers - 1)].len;
1995
1996 ret = -EINVAL;
Matthew Wilcox95570902019-04-05 14:02:10 -07001997 if (rem < len)
1998 goto out_free;
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001999
2000 rem = len;
2001 while (rem) {
2002 struct pipe_buffer *ibuf;
2003 struct pipe_buffer *obuf;
2004
2005 BUG_ON(nbuf >= pipe->buffers);
2006 BUG_ON(!pipe->nrbufs);
2007 ibuf = &pipe->bufs[pipe->curbuf];
2008 obuf = &bufs[nbuf];
2009
2010 if (rem >= ibuf->len) {
2011 *obuf = *ibuf;
2012 ibuf->ops = NULL;
2013 pipe->curbuf = (pipe->curbuf + 1) & (pipe->buffers - 1);
2014 pipe->nrbufs--;
2015 } else {
Matthew Wilcox95570902019-04-05 14:02:10 -07002016 if (!pipe_buf_get(pipe, ibuf))
2017 goto out_free;
2018
Miklos Szeredidd3bb142010-05-25 15:06:06 +02002019 *obuf = *ibuf;
2020 obuf->flags &= ~PIPE_BUF_FLAG_GIFT;
2021 obuf->len = rem;
2022 ibuf->offset += obuf->len;
2023 ibuf->len -= obuf->len;
2024 }
2025 nbuf++;
2026 rem -= obuf->len;
2027 }
2028 pipe_unlock(pipe);
2029
Miklos Szeredidc008092015-07-01 16:25:58 +02002030 fuse_copy_init(&cs, 0, NULL);
Miklos Szeredidd3bb142010-05-25 15:06:06 +02002031 cs.pipebufs = bufs;
Al Viro6c09e942015-04-03 22:06:08 -04002032 cs.nr_segs = nbuf;
Miklos Szeredidd3bb142010-05-25 15:06:06 +02002033 cs.pipe = pipe;
2034
Miklos Szeredice534fb2010-05-25 15:06:07 +02002035 if (flags & SPLICE_F_MOVE)
2036 cs.move_pages = 1;
2037
Miklos Szeredic36960462015-07-01 16:26:09 +02002038 ret = fuse_dev_do_write(fud, &cs, len);
Miklos Szeredidd3bb142010-05-25 15:06:06 +02002039
Jann Horn50449aa2019-01-12 02:39:05 +01002040 pipe_lock(pipe);
Matthew Wilcox95570902019-04-05 14:02:10 -07002041out_free:
Miklos Szeredia7796382016-09-27 10:45:12 +02002042 for (idx = 0; idx < nbuf; idx++)
2043 pipe_buf_release(pipe, &bufs[idx]);
Jann Horn50449aa2019-01-12 02:39:05 +01002044 pipe_unlock(pipe);
Miklos Szeredia7796382016-09-27 10:45:12 +02002045
Miklos Szeredidd3bb142010-05-25 15:06:06 +02002046 kfree(bufs);
2047 return ret;
2048}
2049
Miklos Szeredi334f4852005-09-09 13:10:27 -07002050static unsigned fuse_dev_poll(struct file *file, poll_table *wait)
2051{
Miklos Szeredi334f4852005-09-09 13:10:27 -07002052 unsigned mask = POLLOUT | POLLWRNORM;
Miklos Szeredif88996a2015-07-01 16:26:01 +02002053 struct fuse_iqueue *fiq;
Miklos Szeredicc080e92015-07-01 16:26:08 +02002054 struct fuse_dev *fud = fuse_get_dev(file);
2055
2056 if (!fud)
Miklos Szeredi7025d9a2006-04-10 22:54:50 -07002057 return POLLERR;
Miklos Szeredi334f4852005-09-09 13:10:27 -07002058
Miklos Szeredicc080e92015-07-01 16:26:08 +02002059 fiq = &fud->fc->iq;
Miklos Szeredif88996a2015-07-01 16:26:01 +02002060 poll_wait(file, &fiq->waitq, wait);
Miklos Szeredi334f4852005-09-09 13:10:27 -07002061
Miklos Szeredi4ce60812015-07-01 16:26:02 +02002062 spin_lock(&fiq->waitq.lock);
Miklos Szeredie16714d2015-07-01 16:26:01 +02002063 if (!fiq->connected)
Miklos Szeredi7025d9a2006-04-10 22:54:50 -07002064 mask = POLLERR;
Miklos Szeredif88996a2015-07-01 16:26:01 +02002065 else if (request_pending(fiq))
Miklos Szeredi7025d9a2006-04-10 22:54:50 -07002066 mask |= POLLIN | POLLRDNORM;
Miklos Szeredi4ce60812015-07-01 16:26:02 +02002067 spin_unlock(&fiq->waitq.lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -07002068
2069 return mask;
2070}
2071
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002072/*
2073 * Abort all requests on the given list (pending or processing)
2074 *
Miklos Szeredid7133112006-04-10 22:54:55 -07002075 * This function releases and reacquires fc->lock
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002076 */
Miklos Szeredi334f4852005-09-09 13:10:27 -07002077static void end_requests(struct fuse_conn *fc, struct list_head *head)
2078{
2079 while (!list_empty(head)) {
2080 struct fuse_req *req;
2081 req = list_entry(head->next, struct fuse_req, list);
Miklos Szeredi334f4852005-09-09 13:10:27 -07002082 req->out.h.error = -ECONNABORTED;
Miklos Szeredi33e14b42015-07-01 16:26:01 +02002083 clear_bit(FR_SENT, &req->flags);
Miklos Szeredif377cb72015-07-01 16:26:04 +02002084 list_del_init(&req->list);
Miklos Szeredi334f4852005-09-09 13:10:27 -07002085 request_end(fc, req);
Miklos Szeredi334f4852005-09-09 13:10:27 -07002086 }
2087}
2088
Bryan Green357ccf22011-03-01 16:43:52 -08002089static void end_polls(struct fuse_conn *fc)
2090{
2091 struct rb_node *p;
2092
2093 p = rb_first(&fc->polled_files);
2094
2095 while (p) {
2096 struct fuse_file *ff;
2097 ff = rb_entry(p, struct fuse_file, polled_node);
2098 wake_up_interruptible_all(&ff->poll_wait);
2099
2100 p = rb_next(p);
2101 }
2102}
2103
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002104/*
2105 * Abort all requests.
2106 *
Miklos Szeredib716d422015-07-01 16:25:59 +02002107 * Emergency exit in case of a malicious or accidental deadlock, or just a hung
2108 * filesystem.
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002109 *
Miklos Szeredib716d422015-07-01 16:25:59 +02002110 * The same effect is usually achievable through killing the filesystem daemon
2111 * and all users of the filesystem. The exception is the combination of an
2112 * asynchronous request and the tricky deadlock (see
2113 * Documentation/filesystems/fuse.txt).
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002114 *
Miklos Szeredib716d422015-07-01 16:25:59 +02002115 * Aborting requests under I/O goes as follows: 1: Separate out unlocked
2116 * requests, they should be finished off immediately. Locked requests will be
2117 * finished after unlock; see unlock_request(). 2: Finish off the unlocked
2118 * requests. It is possible that some request will finish before we can. This
2119 * is OK, the request will in that case be removed from the list before we touch
2120 * it.
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002121 */
2122void fuse_abort_conn(struct fuse_conn *fc)
2123{
Miklos Szeredif88996a2015-07-01 16:26:01 +02002124 struct fuse_iqueue *fiq = &fc->iq;
2125
Miklos Szeredid7133112006-04-10 22:54:55 -07002126 spin_lock(&fc->lock);
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002127 if (fc->connected) {
Miklos Szeredic36960462015-07-01 16:26:09 +02002128 struct fuse_dev *fud;
Miklos Szeredib716d422015-07-01 16:25:59 +02002129 struct fuse_req *req, *next;
Miklos Szeredi41f98272015-07-01 16:25:59 +02002130 LIST_HEAD(to_end1);
2131 LIST_HEAD(to_end2);
Miklos Szeredib716d422015-07-01 16:25:59 +02002132
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002133 fc->connected = 0;
Miklos Szeredi51eb01e2006-06-25 05:48:50 -07002134 fc->blocked = 0;
Miklos Szeredi9759bd512015-01-06 10:45:35 +01002135 fuse_set_initialized(fc);
Miklos Szeredic36960462015-07-01 16:26:09 +02002136 list_for_each_entry(fud, &fc->devices, entry) {
2137 struct fuse_pqueue *fpq = &fud->pq;
2138
2139 spin_lock(&fpq->lock);
2140 fpq->connected = 0;
2141 list_for_each_entry_safe(req, next, &fpq->io, list) {
2142 req->out.h.error = -ECONNABORTED;
2143 spin_lock(&req->waitq.lock);
2144 set_bit(FR_ABORTED, &req->flags);
2145 if (!test_bit(FR_LOCKED, &req->flags)) {
2146 set_bit(FR_PRIVATE, &req->flags);
Miklos Szeredi501c4ca2018-07-26 16:13:11 +02002147 __fuse_get_request(req);
Miklos Szeredic36960462015-07-01 16:26:09 +02002148 list_move(&req->list, &to_end1);
2149 }
2150 spin_unlock(&req->waitq.lock);
Miklos Szeredi77cd9d42015-07-01 16:26:06 +02002151 }
Miklos Szeredic36960462015-07-01 16:26:09 +02002152 list_splice_init(&fpq->processing, &to_end2);
2153 spin_unlock(&fpq->lock);
Miklos Szeredib716d422015-07-01 16:25:59 +02002154 }
Miklos Szeredi41f98272015-07-01 16:25:59 +02002155 fc->max_background = UINT_MAX;
2156 flush_bg_queue(fc);
Miklos Szeredi8c911892015-07-01 16:26:02 +02002157
Miklos Szeredi4ce60812015-07-01 16:26:02 +02002158 spin_lock(&fiq->waitq.lock);
Miklos Szeredi8c911892015-07-01 16:26:02 +02002159 fiq->connected = 0;
Miklos Szeredif88996a2015-07-01 16:26:01 +02002160 list_splice_init(&fiq->pending, &to_end2);
Tahsin Erdogan0181b362017-01-12 12:04:04 -08002161 list_for_each_entry(req, &to_end2, list)
2162 clear_bit(FR_PENDING, &req->flags);
Miklos Szeredi8c911892015-07-01 16:26:02 +02002163 while (forget_pending(fiq))
2164 kfree(dequeue_forget(fiq, 1, NULL));
Miklos Szeredi4ce60812015-07-01 16:26:02 +02002165 wake_up_all_locked(&fiq->waitq);
2166 spin_unlock(&fiq->waitq.lock);
Miklos Szeredi8c911892015-07-01 16:26:02 +02002167 kill_fasync(&fiq->fasync, SIGIO, POLL_IN);
Miklos Szerediee314a82015-07-01 16:26:08 +02002168 end_polls(fc);
2169 wake_up_all(&fc->blocked_waitq);
2170 spin_unlock(&fc->lock);
Miklos Szeredi8c911892015-07-01 16:26:02 +02002171
Miklos Szeredi41f98272015-07-01 16:25:59 +02002172 while (!list_empty(&to_end1)) {
2173 req = list_first_entry(&to_end1, struct fuse_req, list);
Miklos Szeredif377cb72015-07-01 16:26:04 +02002174 list_del_init(&req->list);
Miklos Szeredib716d422015-07-01 16:25:59 +02002175 request_end(fc, req);
Miklos Szeredib716d422015-07-01 16:25:59 +02002176 }
Miklos Szeredi41f98272015-07-01 16:25:59 +02002177 end_requests(fc, &to_end2);
Miklos Szerediee314a82015-07-01 16:26:08 +02002178 } else {
2179 spin_unlock(&fc->lock);
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002180 }
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002181}
Tejun Heo08cbf542009-04-14 10:54:53 +09002182EXPORT_SYMBOL_GPL(fuse_abort_conn);
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002183
Miklos Szeredi6465d762018-07-26 16:13:11 +02002184void fuse_wait_aborted(struct fuse_conn *fc)
2185{
2186 wait_event(fc->blocked_waitq, atomic_read(&fc->num_waiting) == 0);
2187}
2188
Tejun Heo08cbf542009-04-14 10:54:53 +09002189int fuse_dev_release(struct inode *inode, struct file *file)
Miklos Szeredi334f4852005-09-09 13:10:27 -07002190{
Miklos Szeredicc080e92015-07-01 16:26:08 +02002191 struct fuse_dev *fud = fuse_get_dev(file);
2192
2193 if (fud) {
2194 struct fuse_conn *fc = fud->fc;
Miklos Szeredic36960462015-07-01 16:26:09 +02002195 struct fuse_pqueue *fpq = &fud->pq;
Miklos Szeredic66025c2018-07-26 16:13:11 +02002196 LIST_HEAD(to_end);
Miklos Szeredicc080e92015-07-01 16:26:08 +02002197
Miklos Szeredic66025c2018-07-26 16:13:11 +02002198 spin_lock(&fpq->lock);
Miklos Szeredic36960462015-07-01 16:26:09 +02002199 WARN_ON(!list_empty(&fpq->io));
Miklos Szeredic66025c2018-07-26 16:13:11 +02002200 list_splice_init(&fpq->processing, &to_end);
2201 spin_unlock(&fpq->lock);
2202
2203 end_requests(fc, &to_end);
2204
Miklos Szeredic36960462015-07-01 16:26:09 +02002205 /* Are we the last open device? */
2206 if (atomic_dec_and_test(&fc->dev_count)) {
2207 WARN_ON(fc->iq.fasync != NULL);
2208 fuse_abort_conn(fc);
2209 }
Miklos Szeredicc080e92015-07-01 16:26:08 +02002210 fuse_dev_free(fud);
Jeff Dike385a17b2006-04-10 22:54:52 -07002211 }
Miklos Szeredi334f4852005-09-09 13:10:27 -07002212 return 0;
2213}
Tejun Heo08cbf542009-04-14 10:54:53 +09002214EXPORT_SYMBOL_GPL(fuse_dev_release);
Miklos Szeredi334f4852005-09-09 13:10:27 -07002215
Jeff Dike385a17b2006-04-10 22:54:52 -07002216static int fuse_dev_fasync(int fd, struct file *file, int on)
2217{
Miklos Szeredicc080e92015-07-01 16:26:08 +02002218 struct fuse_dev *fud = fuse_get_dev(file);
2219
2220 if (!fud)
Miklos Szeredia87046d2006-04-10 22:54:56 -07002221 return -EPERM;
Jeff Dike385a17b2006-04-10 22:54:52 -07002222
2223 /* No locking - fasync_helper does its own locking */
Miklos Szeredicc080e92015-07-01 16:26:08 +02002224 return fasync_helper(fd, file, on, &fud->fc->iq.fasync);
Jeff Dike385a17b2006-04-10 22:54:52 -07002225}
2226
Miklos Szeredi00c570f2015-07-01 16:26:08 +02002227static int fuse_device_clone(struct fuse_conn *fc, struct file *new)
2228{
Miklos Szeredicc080e92015-07-01 16:26:08 +02002229 struct fuse_dev *fud;
2230
Miklos Szeredi00c570f2015-07-01 16:26:08 +02002231 if (new->private_data)
2232 return -EINVAL;
2233
Miklos Szeredicc080e92015-07-01 16:26:08 +02002234 fud = fuse_dev_alloc(fc);
2235 if (!fud)
2236 return -ENOMEM;
2237
2238 new->private_data = fud;
Miklos Szeredic36960462015-07-01 16:26:09 +02002239 atomic_inc(&fc->dev_count);
Miklos Szeredi00c570f2015-07-01 16:26:08 +02002240
2241 return 0;
2242}
2243
2244static long fuse_dev_ioctl(struct file *file, unsigned int cmd,
2245 unsigned long arg)
2246{
2247 int err = -ENOTTY;
2248
2249 if (cmd == FUSE_DEV_IOC_CLONE) {
2250 int oldfd;
2251
2252 err = -EFAULT;
2253 if (!get_user(oldfd, (__u32 __user *) arg)) {
2254 struct file *old = fget(oldfd);
2255
2256 err = -EINVAL;
2257 if (old) {
Jann Horn8ed1f0e2015-08-16 20:27:01 +02002258 struct fuse_dev *fud = NULL;
2259
2260 /*
2261 * Check against file->f_op because CUSE
2262 * uses the same ioctl handler.
2263 */
2264 if (old->f_op == file->f_op &&
2265 old->f_cred->user_ns == file->f_cred->user_ns)
2266 fud = fuse_get_dev(old);
Miklos Szeredi00c570f2015-07-01 16:26:08 +02002267
Miklos Szeredicc080e92015-07-01 16:26:08 +02002268 if (fud) {
Miklos Szeredi00c570f2015-07-01 16:26:08 +02002269 mutex_lock(&fuse_mutex);
Miklos Szeredicc080e92015-07-01 16:26:08 +02002270 err = fuse_device_clone(fud->fc, file);
Miklos Szeredi00c570f2015-07-01 16:26:08 +02002271 mutex_unlock(&fuse_mutex);
2272 }
2273 fput(old);
2274 }
2275 }
2276 }
2277 return err;
2278}
2279
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -08002280const struct file_operations fuse_dev_operations = {
Miklos Szeredi334f4852005-09-09 13:10:27 -07002281 .owner = THIS_MODULE,
Tom Van Braeckel94e4fe22015-01-12 05:22:16 +01002282 .open = fuse_dev_open,
Miklos Szeredi334f4852005-09-09 13:10:27 -07002283 .llseek = no_llseek,
Al Virofbdbacc2015-04-03 21:53:39 -04002284 .read_iter = fuse_dev_read,
Miklos Szeredic3021622010-05-25 15:06:07 +02002285 .splice_read = fuse_dev_splice_read,
Al Virofbdbacc2015-04-03 21:53:39 -04002286 .write_iter = fuse_dev_write,
Miklos Szeredidd3bb142010-05-25 15:06:06 +02002287 .splice_write = fuse_dev_splice_write,
Miklos Szeredi334f4852005-09-09 13:10:27 -07002288 .poll = fuse_dev_poll,
2289 .release = fuse_dev_release,
Jeff Dike385a17b2006-04-10 22:54:52 -07002290 .fasync = fuse_dev_fasync,
Miklos Szeredi00c570f2015-07-01 16:26:08 +02002291 .unlocked_ioctl = fuse_dev_ioctl,
2292 .compat_ioctl = fuse_dev_ioctl,
Miklos Szeredi334f4852005-09-09 13:10:27 -07002293};
Tejun Heo08cbf542009-04-14 10:54:53 +09002294EXPORT_SYMBOL_GPL(fuse_dev_operations);
Miklos Szeredi334f4852005-09-09 13:10:27 -07002295
2296static struct miscdevice fuse_miscdevice = {
2297 .minor = FUSE_MINOR,
2298 .name = "fuse",
2299 .fops = &fuse_dev_operations,
2300};
2301
2302int __init fuse_dev_init(void)
2303{
2304 int err = -ENOMEM;
2305 fuse_req_cachep = kmem_cache_create("fuse_request",
2306 sizeof(struct fuse_req),
Paul Mundt20c2df82007-07-20 10:11:58 +09002307 0, 0, NULL);
Miklos Szeredi334f4852005-09-09 13:10:27 -07002308 if (!fuse_req_cachep)
2309 goto out;
2310
2311 err = misc_register(&fuse_miscdevice);
2312 if (err)
2313 goto out_cache_clean;
2314
2315 return 0;
2316
2317 out_cache_clean:
2318 kmem_cache_destroy(fuse_req_cachep);
2319 out:
2320 return err;
2321}
2322
2323void fuse_dev_cleanup(void)
2324{
2325 misc_deregister(&fuse_miscdevice);
2326 kmem_cache_destroy(fuse_req_cachep);
2327}