blob: 658fa9e7bc10735fbdf54eb64e9c97a0350e593d [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
Maxim Patlasov8b41e672013-03-21 18:02:04 +0400136static struct fuse_req *__fuse_get_req(struct fuse_conn *fc, unsigned npages,
137 bool for_background)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700138{
Miklos Szeredi08a53cd2006-04-10 22:54:59 -0700139 struct fuse_req *req;
Miklos Szeredi08a53cd2006-04-10 22:54:59 -0700140 int err;
Miklos Szeredi9bc5ddd2006-04-11 21:16:09 +0200141 atomic_inc(&fc->num_waiting);
Maxim Patlasov0aada882013-03-21 18:02:28 +0400142
143 if (fuse_block_alloc(fc, for_background)) {
Maxim Patlasov0aada882013-03-21 18:02:28 +0400144 err = -EINTR;
Al Viro7d3a07f2016-07-19 03:08:27 -0400145 if (wait_event_killable_exclusive(fc->blocked_waitq,
146 !fuse_block_alloc(fc, for_background)))
Maxim Patlasov0aada882013-03-21 18:02:28 +0400147 goto out;
148 }
Miklos Szeredi9759bd512015-01-06 10:45:35 +0100149 /* Matches smp_wmb() in fuse_set_initialized() */
150 smp_rmb();
Miklos Szeredi08a53cd2006-04-10 22:54:59 -0700151
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700152 err = -ENOTCONN;
153 if (!fc->connected)
154 goto out;
155
Miklos Szeredide155222015-07-01 16:25:57 +0200156 err = -ECONNREFUSED;
157 if (fc->conn_error)
158 goto out;
159
Maxim Patlasovb111c8c2012-10-26 19:48:30 +0400160 req = fuse_request_alloc(npages);
Miklos Szeredi9bc5ddd2006-04-11 21:16:09 +0200161 err = -ENOMEM;
Maxim Patlasov722d2be2013-03-21 18:02:36 +0400162 if (!req) {
163 if (for_background)
164 wake_up(&fc->blocked_waitq);
Miklos Szeredi9bc5ddd2006-04-11 21:16:09 +0200165 goto out;
Maxim Patlasov722d2be2013-03-21 18:02:36 +0400166 }
Miklos Szeredi334f4852005-09-09 13:10:27 -0700167
Miklos Szeredi33649c92006-06-25 05:48:52 -0700168 fuse_req_init_context(req);
Miklos Szeredi825d6d32015-07-01 16:25:58 +0200169 __set_bit(FR_WAITING, &req->flags);
170 if (for_background)
171 __set_bit(FR_BACKGROUND, &req->flags);
172
Miklos Szeredi334f4852005-09-09 13:10:27 -0700173 return req;
Miklos Szeredi9bc5ddd2006-04-11 21:16:09 +0200174
175 out:
176 atomic_dec(&fc->num_waiting);
177 return ERR_PTR(err);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700178}
Maxim Patlasov8b41e672013-03-21 18:02:04 +0400179
180struct fuse_req *fuse_get_req(struct fuse_conn *fc, unsigned npages)
181{
182 return __fuse_get_req(fc, npages, false);
183}
Tejun Heo08cbf542009-04-14 10:54:53 +0900184EXPORT_SYMBOL_GPL(fuse_get_req);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700185
Maxim Patlasov8b41e672013-03-21 18:02:04 +0400186struct fuse_req *fuse_get_req_for_background(struct fuse_conn *fc,
187 unsigned npages)
188{
189 return __fuse_get_req(fc, npages, true);
190}
191EXPORT_SYMBOL_GPL(fuse_get_req_for_background);
192
Miklos Szeredi33649c92006-06-25 05:48:52 -0700193/*
194 * Return request in fuse_file->reserved_req. However that may
195 * currently be in use. If that is the case, wait for it to become
196 * available.
197 */
198static struct fuse_req *get_reserved_req(struct fuse_conn *fc,
199 struct file *file)
200{
201 struct fuse_req *req = NULL;
202 struct fuse_file *ff = file->private_data;
203
204 do {
Miklos Szeredide5e3de2007-10-16 23:31:00 -0700205 wait_event(fc->reserved_req_waitq, ff->reserved_req);
Miklos Szeredi33649c92006-06-25 05:48:52 -0700206 spin_lock(&fc->lock);
207 if (ff->reserved_req) {
208 req = ff->reserved_req;
209 ff->reserved_req = NULL;
Al Virocb0942b2012-08-27 14:48:26 -0400210 req->stolen_file = get_file(file);
Miklos Szeredi33649c92006-06-25 05:48:52 -0700211 }
212 spin_unlock(&fc->lock);
213 } while (!req);
214
215 return req;
216}
217
218/*
219 * Put stolen request back into fuse_file->reserved_req
220 */
221static void put_reserved_req(struct fuse_conn *fc, struct fuse_req *req)
222{
223 struct file *file = req->stolen_file;
224 struct fuse_file *ff = file->private_data;
225
226 spin_lock(&fc->lock);
Maxim Patlasovb2430d72012-10-26 19:49:24 +0400227 fuse_request_init(req, req->pages, req->page_descs, req->max_pages);
Miklos Szeredi33649c92006-06-25 05:48:52 -0700228 BUG_ON(ff->reserved_req);
229 ff->reserved_req = req;
Miklos Szeredide5e3de2007-10-16 23:31:00 -0700230 wake_up_all(&fc->reserved_req_waitq);
Miklos Szeredi33649c92006-06-25 05:48:52 -0700231 spin_unlock(&fc->lock);
232 fput(file);
233}
234
235/*
236 * Gets a requests for a file operation, always succeeds
237 *
238 * This is used for sending the FLUSH request, which must get to
239 * userspace, due to POSIX locks which may need to be unlocked.
240 *
241 * If allocation fails due to OOM, use the reserved request in
242 * fuse_file.
243 *
244 * This is very unlikely to deadlock accidentally, since the
245 * filesystem should not have it's own file open. If deadlock is
246 * intentional, it can still be broken by "aborting" the filesystem.
247 */
Maxim Patlasovb111c8c2012-10-26 19:48:30 +0400248struct fuse_req *fuse_get_req_nofail_nopages(struct fuse_conn *fc,
249 struct file *file)
Miklos Szeredi33649c92006-06-25 05:48:52 -0700250{
251 struct fuse_req *req;
252
253 atomic_inc(&fc->num_waiting);
Maxim Patlasov0aada882013-03-21 18:02:28 +0400254 wait_event(fc->blocked_waitq, fc->initialized);
Miklos Szeredi9759bd512015-01-06 10:45:35 +0100255 /* Matches smp_wmb() in fuse_set_initialized() */
256 smp_rmb();
Maxim Patlasovb111c8c2012-10-26 19:48:30 +0400257 req = fuse_request_alloc(0);
Miklos Szeredi33649c92006-06-25 05:48:52 -0700258 if (!req)
259 req = get_reserved_req(fc, file);
260
261 fuse_req_init_context(req);
Miklos Szeredi825d6d32015-07-01 16:25:58 +0200262 __set_bit(FR_WAITING, &req->flags);
263 __clear_bit(FR_BACKGROUND, &req->flags);
Miklos Szeredi33649c92006-06-25 05:48:52 -0700264 return req;
265}
266
Miklos Szeredi334f4852005-09-09 13:10:27 -0700267void fuse_put_request(struct fuse_conn *fc, struct fuse_req *req)
268{
Miklos Szeredi7128ec22006-02-04 23:27:40 -0800269 if (atomic_dec_and_test(&req->count)) {
Miklos Szeredi825d6d32015-07-01 16:25:58 +0200270 if (test_bit(FR_BACKGROUND, &req->flags)) {
Maxim Patlasov722d2be2013-03-21 18:02:36 +0400271 /*
272 * We get here in the unlikely case that a background
273 * request was allocated but not sent
274 */
275 spin_lock(&fc->lock);
276 if (!fc->blocked)
277 wake_up(&fc->blocked_waitq);
278 spin_unlock(&fc->lock);
279 }
280
Miklos Szeredi825d6d32015-07-01 16:25:58 +0200281 if (test_bit(FR_WAITING, &req->flags)) {
282 __clear_bit(FR_WAITING, &req->flags);
Miklos Szeredi9bc5ddd2006-04-11 21:16:09 +0200283 atomic_dec(&fc->num_waiting);
Miklos Szeredi73e0e732015-07-01 16:25:56 +0200284 }
Miklos Szeredi33649c92006-06-25 05:48:52 -0700285
286 if (req->stolen_file)
287 put_reserved_req(fc, req);
288 else
289 fuse_request_free(req);
Miklos Szeredi7128ec22006-02-04 23:27:40 -0800290 }
291}
Tejun Heo08cbf542009-04-14 10:54:53 +0900292EXPORT_SYMBOL_GPL(fuse_put_request);
Miklos Szeredi7128ec22006-02-04 23:27:40 -0800293
Miklos Szeredid12def12008-02-06 01:38:39 -0800294static unsigned len_args(unsigned numargs, struct fuse_arg *args)
295{
296 unsigned nbytes = 0;
297 unsigned i;
298
299 for (i = 0; i < numargs; i++)
300 nbytes += args[i].size;
301
302 return nbytes;
303}
304
Miklos Szeredif88996a2015-07-01 16:26:01 +0200305static u64 fuse_get_unique(struct fuse_iqueue *fiq)
Miklos Szeredid12def12008-02-06 01:38:39 -0800306{
Miklos Szeredif88996a2015-07-01 16:26:01 +0200307 return ++fiq->reqctr;
Miklos Szeredid12def12008-02-06 01:38:39 -0800308}
309
Miklos Szeredif88996a2015-07-01 16:26:01 +0200310static void queue_request(struct fuse_iqueue *fiq, struct fuse_req *req)
Miklos Szeredid12def12008-02-06 01:38:39 -0800311{
Miklos Szeredid12def12008-02-06 01:38:39 -0800312 req->in.h.len = sizeof(struct fuse_in_header) +
313 len_args(req->in.numargs, (struct fuse_arg *) req->in.args);
Miklos Szeredif88996a2015-07-01 16:26:01 +0200314 list_add_tail(&req->list, &fiq->pending);
Miklos Szeredi4ce60812015-07-01 16:26:02 +0200315 wake_up_locked(&fiq->waitq);
Miklos Szeredif88996a2015-07-01 16:26:01 +0200316 kill_fasync(&fiq->fasync, SIGIO, POLL_IN);
Miklos Szeredid12def12008-02-06 01:38:39 -0800317}
318
Miklos Szeredi07e77dc2010-12-07 20:16:56 +0100319void fuse_queue_forget(struct fuse_conn *fc, struct fuse_forget_link *forget,
320 u64 nodeid, u64 nlookup)
321{
Miklos Szeredif88996a2015-07-01 16:26:01 +0200322 struct fuse_iqueue *fiq = &fc->iq;
323
Miklos Szeredi02c048b2010-12-07 20:16:56 +0100324 forget->forget_one.nodeid = nodeid;
325 forget->forget_one.nlookup = nlookup;
Miklos Szeredi07e77dc2010-12-07 20:16:56 +0100326
Miklos Szeredi4ce60812015-07-01 16:26:02 +0200327 spin_lock(&fiq->waitq.lock);
Miklos Szeredie16714d2015-07-01 16:26:01 +0200328 if (fiq->connected) {
Miklos Szeredif88996a2015-07-01 16:26:01 +0200329 fiq->forget_list_tail->next = forget;
330 fiq->forget_list_tail = forget;
Miklos Szeredi4ce60812015-07-01 16:26:02 +0200331 wake_up_locked(&fiq->waitq);
Miklos Szeredif88996a2015-07-01 16:26:01 +0200332 kill_fasync(&fiq->fasync, SIGIO, POLL_IN);
Miklos Szeredi5dfcc872011-09-12 09:38:03 +0200333 } else {
334 kfree(forget);
335 }
Miklos Szeredi4ce60812015-07-01 16:26:02 +0200336 spin_unlock(&fiq->waitq.lock);
Miklos Szeredi07e77dc2010-12-07 20:16:56 +0100337}
338
Miklos Szeredid12def12008-02-06 01:38:39 -0800339static void flush_bg_queue(struct fuse_conn *fc)
340{
Csaba Henk7a6d3c82009-07-01 17:28:41 -0700341 while (fc->active_background < fc->max_background &&
Miklos Szeredid12def12008-02-06 01:38:39 -0800342 !list_empty(&fc->bg_queue)) {
343 struct fuse_req *req;
Miklos Szeredif88996a2015-07-01 16:26:01 +0200344 struct fuse_iqueue *fiq = &fc->iq;
Miklos Szeredid12def12008-02-06 01:38:39 -0800345
346 req = list_entry(fc->bg_queue.next, struct fuse_req, list);
347 list_del(&req->list);
348 fc->active_background++;
Miklos Szeredi4ce60812015-07-01 16:26:02 +0200349 spin_lock(&fiq->waitq.lock);
Miklos Szeredif88996a2015-07-01 16:26:01 +0200350 req->in.h.unique = fuse_get_unique(fiq);
351 queue_request(fiq, req);
Miklos Szeredi4ce60812015-07-01 16:26:02 +0200352 spin_unlock(&fiq->waitq.lock);
Miklos Szeredid12def12008-02-06 01:38:39 -0800353 }
354}
355
Miklos Szeredi6dbbcb12006-04-26 10:49:06 +0200356/*
Miklos Szeredi334f4852005-09-09 13:10:27 -0700357 * This function is called when a request is finished. Either a reply
Miklos Szeredif9a28422006-06-25 05:48:53 -0700358 * has arrived or it was aborted (and not yet sent) or some error
Miklos Szeredif43b1552006-01-16 22:14:26 -0800359 * occurred during communication with userspace, or the device file
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700360 * was closed. The requester thread is woken up (if still waiting),
361 * the 'end' callback is called if given, else the reference to the
362 * request is released
Miklos Szeredi334f4852005-09-09 13:10:27 -0700363 */
364static void request_end(struct fuse_conn *fc, struct fuse_req *req)
365{
Miklos Szeredi4ce60812015-07-01 16:26:02 +0200366 struct fuse_iqueue *fiq = &fc->iq;
Miklos Szeredi365ae712015-07-01 16:26:06 +0200367
Miklos Szerediefe28002015-07-01 16:26:07 +0200368 if (test_and_set_bit(FR_FINISHED, &req->flags))
Miklos Szeredi365ae712015-07-01 16:26:06 +0200369 return;
Miklos Szeredi365ae712015-07-01 16:26:06 +0200370
Miklos Szeredi4ce60812015-07-01 16:26:02 +0200371 spin_lock(&fiq->waitq.lock);
Miklos Szeredi0d8e84b2015-07-01 16:25:58 +0200372 list_del_init(&req->intr_entry);
Miklos Szeredi4ce60812015-07-01 16:26:02 +0200373 spin_unlock(&fiq->waitq.lock);
Miklos Szeredi33e14b42015-07-01 16:26:01 +0200374 WARN_ON(test_bit(FR_PENDING, &req->flags));
375 WARN_ON(test_bit(FR_SENT, &req->flags));
Miklos Szeredi825d6d32015-07-01 16:25:58 +0200376 if (test_bit(FR_BACKGROUND, &req->flags)) {
Miklos Szerediefe28002015-07-01 16:26:07 +0200377 spin_lock(&fc->lock);
Miklos Szeredi825d6d32015-07-01 16:25:58 +0200378 clear_bit(FR_BACKGROUND, &req->flags);
Maxim Patlasov722d2be2013-03-21 18:02:36 +0400379 if (fc->num_background == fc->max_background)
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700380 fc->blocked = 0;
Maxim Patlasov722d2be2013-03-21 18:02:36 +0400381
382 /* Wake up next waiter, if any */
Miklos Szeredi3c18ef82013-04-17 21:50:58 +0200383 if (!fc->blocked && waitqueue_active(&fc->blocked_waitq))
Maxim Patlasov722d2be2013-03-21 18:02:36 +0400384 wake_up(&fc->blocked_waitq);
385
Csaba Henk7a6d3c82009-07-01 17:28:41 -0700386 if (fc->num_background == fc->congestion_threshold &&
Tejun Heoa325f9b2009-04-14 10:54:52 +0900387 fc->connected && fc->bdi_initialized) {
Jens Axboe8aa7e842009-07-09 14:52:32 +0200388 clear_bdi_congested(&fc->bdi, BLK_RW_SYNC);
389 clear_bdi_congested(&fc->bdi, BLK_RW_ASYNC);
Miklos Szeredif92b99b2007-10-16 23:30:59 -0700390 }
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700391 fc->num_background--;
Miklos Szeredid12def12008-02-06 01:38:39 -0800392 fc->active_background--;
393 flush_bg_queue(fc);
Miklos Szerediefe28002015-07-01 16:26:07 +0200394 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700395 }
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700396 wake_up(&req->waitq);
Miklos Szeredi1e6881c2015-07-01 16:26:07 +0200397 if (req->end)
398 req->end(fc, req);
Tejun Heoe9bb09d2008-11-26 12:03:54 +0100399 fuse_put_request(fc, req);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700400}
401
Miklos Szeredif88996a2015-07-01 16:26:01 +0200402static void queue_interrupt(struct fuse_iqueue *fiq, struct fuse_req *req)
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700403{
Miklos Szeredi4ce60812015-07-01 16:26:02 +0200404 spin_lock(&fiq->waitq.lock);
Sahitya Tummala72834482017-02-08 20:30:56 +0530405 if (test_bit(FR_FINISHED, &req->flags)) {
406 spin_unlock(&fiq->waitq.lock);
407 return;
408 }
Miklos Szeredi8f7bb362015-07-01 16:26:03 +0200409 if (list_empty(&req->intr_entry)) {
410 list_add_tail(&req->intr_entry, &fiq->interrupts);
411 wake_up_locked(&fiq->waitq);
412 }
Miklos Szeredi4ce60812015-07-01 16:26:02 +0200413 spin_unlock(&fiq->waitq.lock);
Miklos Szeredif88996a2015-07-01 16:26:01 +0200414 kill_fasync(&fiq->fasync, SIGIO, POLL_IN);
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700415}
416
Miklos Szeredi7c352bd2005-09-09 13:10:39 -0700417static void request_wait_answer(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700418{
Miklos Szeredi4ce60812015-07-01 16:26:02 +0200419 struct fuse_iqueue *fiq = &fc->iq;
Miklos Szeredic4775262015-07-01 16:26:00 +0200420 int err;
421
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700422 if (!fc->no_interrupt) {
423 /* Any signal may interrupt this */
Miklos Szeredic4775262015-07-01 16:26:00 +0200424 err = wait_event_interruptible(req->waitq,
Miklos Szeredi33e14b42015-07-01 16:26:01 +0200425 test_bit(FR_FINISHED, &req->flags));
Miklos Szeredic4775262015-07-01 16:26:00 +0200426 if (!err)
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700427 return;
428
Miklos Szeredi825d6d32015-07-01 16:25:58 +0200429 set_bit(FR_INTERRUPTED, &req->flags);
Miklos Szeredi8f7bb362015-07-01 16:26:03 +0200430 /* matches barrier in fuse_dev_do_read() */
431 smp_mb__after_atomic();
Miklos Szeredi33e14b42015-07-01 16:26:01 +0200432 if (test_bit(FR_SENT, &req->flags))
Miklos Szeredi4ce60812015-07-01 16:26:02 +0200433 queue_interrupt(fiq, req);
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700434 }
435
Miklos Szeredi825d6d32015-07-01 16:25:58 +0200436 if (!test_bit(FR_FORCE, &req->flags)) {
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700437 /* Only fatal signals may interrupt this */
Al Viro7d3a07f2016-07-19 03:08:27 -0400438 err = wait_event_killable(req->waitq,
Miklos Szeredi33e14b42015-07-01 16:26:01 +0200439 test_bit(FR_FINISHED, &req->flags));
Miklos Szeredic4775262015-07-01 16:26:00 +0200440 if (!err)
Miklos Szeredia131de02007-10-16 23:31:04 -0700441 return;
442
Miklos Szeredi4ce60812015-07-01 16:26:02 +0200443 spin_lock(&fiq->waitq.lock);
Miklos Szeredia131de02007-10-16 23:31:04 -0700444 /* Request is not yet in userspace, bail out */
Miklos Szeredi33e14b42015-07-01 16:26:01 +0200445 if (test_bit(FR_PENDING, &req->flags)) {
Miklos Szeredia131de02007-10-16 23:31:04 -0700446 list_del(&req->list);
Miklos Szeredi4ce60812015-07-01 16:26:02 +0200447 spin_unlock(&fiq->waitq.lock);
Miklos Szeredia131de02007-10-16 23:31:04 -0700448 __fuse_put_request(req);
449 req->out.h.error = -EINTR;
450 return;
451 }
Miklos Szeredi4ce60812015-07-01 16:26:02 +0200452 spin_unlock(&fiq->waitq.lock);
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700453 }
Miklos Szeredi334f4852005-09-09 13:10:27 -0700454
Miklos Szeredia131de02007-10-16 23:31:04 -0700455 /*
456 * Either request is already in userspace, or it was forced.
457 * Wait it out.
458 */
Todd Poynor1672c662011-08-24 15:01:30 -0700459 while (!test_bit(FR_FINISHED, &req->flags))
460 wait_event_freezable(req->waitq,
461 test_bit(FR_FINISHED, &req->flags));
Miklos Szeredi334f4852005-09-09 13:10:27 -0700462}
463
Eric Wong6a4e9222013-02-04 13:04:44 +0000464static void __fuse_request_send(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700465{
Miklos Szeredie16714d2015-07-01 16:26:01 +0200466 struct fuse_iqueue *fiq = &fc->iq;
467
Miklos Szeredi825d6d32015-07-01 16:25:58 +0200468 BUG_ON(test_bit(FR_BACKGROUND, &req->flags));
Miklos Szeredi4ce60812015-07-01 16:26:02 +0200469 spin_lock(&fiq->waitq.lock);
Miklos Szeredie16714d2015-07-01 16:26:01 +0200470 if (!fiq->connected) {
Miklos Szeredi4ce60812015-07-01 16:26:02 +0200471 spin_unlock(&fiq->waitq.lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700472 req->out.h.error = -ENOTCONN;
Miklos Szeredic4775262015-07-01 16:26:00 +0200473 } else {
Miklos Szeredif88996a2015-07-01 16:26:01 +0200474 req->in.h.unique = fuse_get_unique(fiq);
475 queue_request(fiq, req);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700476 /* acquire extra reference, since request is still needed
477 after request_end() */
478 __fuse_get_request(req);
Miklos Szeredi4ce60812015-07-01 16:26:02 +0200479 spin_unlock(&fiq->waitq.lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700480
Miklos Szeredi7c352bd2005-09-09 13:10:39 -0700481 request_wait_answer(fc, req);
Miklos Szeredic4775262015-07-01 16:26:00 +0200482 /* Pairs with smp_wmb() in request_end() */
483 smp_rmb();
Miklos Szeredi334f4852005-09-09 13:10:27 -0700484 }
Miklos Szeredi334f4852005-09-09 13:10:27 -0700485}
Eric Wong6a4e9222013-02-04 13:04:44 +0000486
487void fuse_request_send(struct fuse_conn *fc, struct fuse_req *req)
488{
Miklos Szeredi825d6d32015-07-01 16:25:58 +0200489 __set_bit(FR_ISREPLY, &req->flags);
490 if (!test_bit(FR_WAITING, &req->flags)) {
491 __set_bit(FR_WAITING, &req->flags);
Miklos Szeredi5437f242015-07-01 16:25:56 +0200492 atomic_inc(&fc->num_waiting);
493 }
Eric Wong6a4e9222013-02-04 13:04:44 +0000494 __fuse_request_send(fc, req);
495}
Tejun Heo08cbf542009-04-14 10:54:53 +0900496EXPORT_SYMBOL_GPL(fuse_request_send);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700497
Miklos Szeredi21f62172015-01-06 10:45:35 +0100498static void fuse_adjust_compat(struct fuse_conn *fc, struct fuse_args *args)
499{
500 if (fc->minor < 4 && args->in.h.opcode == FUSE_STATFS)
501 args->out.args[0].size = FUSE_COMPAT_STATFS_SIZE;
502
503 if (fc->minor < 9) {
504 switch (args->in.h.opcode) {
505 case FUSE_LOOKUP:
506 case FUSE_CREATE:
507 case FUSE_MKNOD:
508 case FUSE_MKDIR:
509 case FUSE_SYMLINK:
510 case FUSE_LINK:
511 args->out.args[0].size = FUSE_COMPAT_ENTRY_OUT_SIZE;
512 break;
513 case FUSE_GETATTR:
514 case FUSE_SETATTR:
515 args->out.args[0].size = FUSE_COMPAT_ATTR_OUT_SIZE;
516 break;
517 }
518 }
519 if (fc->minor < 12) {
520 switch (args->in.h.opcode) {
521 case FUSE_CREATE:
522 args->in.args[0].size = sizeof(struct fuse_open_in);
523 break;
524 case FUSE_MKNOD:
525 args->in.args[0].size = FUSE_COMPAT_MKNOD_IN_SIZE;
526 break;
527 }
528 }
529}
530
Miklos Szeredi70781872014-12-12 09:49:05 +0100531ssize_t fuse_simple_request(struct fuse_conn *fc, struct fuse_args *args)
532{
533 struct fuse_req *req;
534 ssize_t ret;
535
536 req = fuse_get_req(fc, 0);
537 if (IS_ERR(req))
538 return PTR_ERR(req);
539
Miklos Szeredi21f62172015-01-06 10:45:35 +0100540 /* Needs to be done after fuse_get_req() so that fc->minor is valid */
541 fuse_adjust_compat(fc, args);
542
Miklos Szeredi70781872014-12-12 09:49:05 +0100543 req->in.h.opcode = args->in.h.opcode;
544 req->in.h.nodeid = args->in.h.nodeid;
545 req->in.numargs = args->in.numargs;
546 memcpy(req->in.args, args->in.args,
547 args->in.numargs * sizeof(struct fuse_in_arg));
548 req->out.argvar = args->out.argvar;
549 req->out.numargs = args->out.numargs;
550 memcpy(req->out.args, args->out.args,
551 args->out.numargs * sizeof(struct fuse_arg));
552 fuse_request_send(fc, req);
553 ret = req->out.h.error;
Nikhilesh Reddy759832b2017-07-26 09:41:57 +0530554 if (!ret) {
555 if (args->out.argvar) {
556 WARN_ON(args->out.numargs != 1);
557 ret = req->out.args[0].size;
558 }
559
560 if (req->passthrough_filp != NULL)
561 args->out.passthrough_filp = req->passthrough_filp;
Miklos Szeredi70781872014-12-12 09:49:05 +0100562 }
563 fuse_put_request(fc, req);
564
565 return ret;
566}
567
Miklos Szeredif0139aa2015-07-01 16:25:57 +0200568/*
569 * Called under fc->lock
570 *
571 * fc->connected must have been checked previously
572 */
573void fuse_request_send_background_locked(struct fuse_conn *fc,
574 struct fuse_req *req)
Miklos Szeredid12def12008-02-06 01:38:39 -0800575{
Miklos Szeredi825d6d32015-07-01 16:25:58 +0200576 BUG_ON(!test_bit(FR_BACKGROUND, &req->flags));
577 if (!test_bit(FR_WAITING, &req->flags)) {
578 __set_bit(FR_WAITING, &req->flags);
Miklos Szeredi5437f242015-07-01 16:25:56 +0200579 atomic_inc(&fc->num_waiting);
580 }
Miklos Szeredi825d6d32015-07-01 16:25:58 +0200581 __set_bit(FR_ISREPLY, &req->flags);
Miklos Szeredid12def12008-02-06 01:38:39 -0800582 fc->num_background++;
Csaba Henk7a6d3c82009-07-01 17:28:41 -0700583 if (fc->num_background == fc->max_background)
Miklos Szeredid12def12008-02-06 01:38:39 -0800584 fc->blocked = 1;
Csaba Henk7a6d3c82009-07-01 17:28:41 -0700585 if (fc->num_background == fc->congestion_threshold &&
Tejun Heoa325f9b2009-04-14 10:54:52 +0900586 fc->bdi_initialized) {
Jens Axboe8aa7e842009-07-09 14:52:32 +0200587 set_bdi_congested(&fc->bdi, BLK_RW_SYNC);
588 set_bdi_congested(&fc->bdi, BLK_RW_ASYNC);
Miklos Szeredid12def12008-02-06 01:38:39 -0800589 }
590 list_add_tail(&req->list, &fc->bg_queue);
591 flush_bg_queue(fc);
592}
593
Miklos Szeredif0139aa2015-07-01 16:25:57 +0200594void fuse_request_send_background(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700595{
Miklos Szeredi42dc6212015-07-01 16:25:56 +0200596 BUG_ON(!req->end);
Miklos Szeredid7133112006-04-10 22:54:55 -0700597 spin_lock(&fc->lock);
Miklos Szeredi1e9a4ed2005-09-09 13:10:31 -0700598 if (fc->connected) {
Miklos Szeredif0139aa2015-07-01 16:25:57 +0200599 fuse_request_send_background_locked(fc, req);
Miklos Szeredid7133112006-04-10 22:54:55 -0700600 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700601 } else {
Miklos Szeredi42dc6212015-07-01 16:25:56 +0200602 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700603 req->out.h.error = -ENOTCONN;
Miklos Szeredi42dc6212015-07-01 16:25:56 +0200604 req->end(fc, req);
605 fuse_put_request(fc, req);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700606 }
607}
Tejun Heo08cbf542009-04-14 10:54:53 +0900608EXPORT_SYMBOL_GPL(fuse_request_send_background);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700609
Miklos Szeredi2d45ba32010-07-12 14:41:40 +0200610static int fuse_request_send_notify_reply(struct fuse_conn *fc,
611 struct fuse_req *req, u64 unique)
612{
613 int err = -ENODEV;
Miklos Szeredif88996a2015-07-01 16:26:01 +0200614 struct fuse_iqueue *fiq = &fc->iq;
Miklos Szeredi2d45ba32010-07-12 14:41:40 +0200615
Miklos Szeredi825d6d32015-07-01 16:25:58 +0200616 __clear_bit(FR_ISREPLY, &req->flags);
Miklos Szeredi2d45ba32010-07-12 14:41:40 +0200617 req->in.h.unique = unique;
Miklos Szeredi4ce60812015-07-01 16:26:02 +0200618 spin_lock(&fiq->waitq.lock);
Miklos Szeredie16714d2015-07-01 16:26:01 +0200619 if (fiq->connected) {
Miklos Szeredif88996a2015-07-01 16:26:01 +0200620 queue_request(fiq, req);
Miklos Szeredi2d45ba32010-07-12 14:41:40 +0200621 err = 0;
622 }
Miklos Szeredi4ce60812015-07-01 16:26:02 +0200623 spin_unlock(&fiq->waitq.lock);
Miklos Szeredi2d45ba32010-07-12 14:41:40 +0200624
625 return err;
626}
627
Anand V. Avati0b05b182012-08-19 08:53:23 -0400628void fuse_force_forget(struct file *file, u64 nodeid)
629{
Al Viro6131ffa2013-02-27 16:59:05 -0500630 struct inode *inode = file_inode(file);
Anand V. Avati0b05b182012-08-19 08:53:23 -0400631 struct fuse_conn *fc = get_fuse_conn(inode);
632 struct fuse_req *req;
633 struct fuse_forget_in inarg;
634
635 memset(&inarg, 0, sizeof(inarg));
636 inarg.nlookup = 1;
Maxim Patlasovb111c8c2012-10-26 19:48:30 +0400637 req = fuse_get_req_nofail_nopages(fc, file);
Anand V. Avati0b05b182012-08-19 08:53:23 -0400638 req->in.h.opcode = FUSE_FORGET;
639 req->in.h.nodeid = nodeid;
640 req->in.numargs = 1;
641 req->in.args[0].size = sizeof(inarg);
642 req->in.args[0].value = &inarg;
Miklos Szeredi825d6d32015-07-01 16:25:58 +0200643 __clear_bit(FR_ISREPLY, &req->flags);
Eric Wong6a4e9222013-02-04 13:04:44 +0000644 __fuse_request_send(fc, req);
645 /* ignore errors */
646 fuse_put_request(fc, req);
Anand V. Avati0b05b182012-08-19 08:53:23 -0400647}
648
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700649/*
Miklos Szeredi334f4852005-09-09 13:10:27 -0700650 * Lock the request. Up to the next unlock_request() there mustn't be
651 * anything that could cause a page-fault. If the request was already
Miklos Szeredif9a28422006-06-25 05:48:53 -0700652 * aborted bail out.
Miklos Szeredi334f4852005-09-09 13:10:27 -0700653 */
Miklos Szeredidc008092015-07-01 16:25:58 +0200654static int lock_request(struct fuse_req *req)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700655{
656 int err = 0;
657 if (req) {
Miklos Szeredidc008092015-07-01 16:25:58 +0200658 spin_lock(&req->waitq.lock);
Miklos Szeredi825d6d32015-07-01 16:25:58 +0200659 if (test_bit(FR_ABORTED, &req->flags))
Miklos Szeredi334f4852005-09-09 13:10:27 -0700660 err = -ENOENT;
661 else
Miklos Szeredi825d6d32015-07-01 16:25:58 +0200662 set_bit(FR_LOCKED, &req->flags);
Miklos Szeredidc008092015-07-01 16:25:58 +0200663 spin_unlock(&req->waitq.lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700664 }
665 return err;
666}
667
668/*
Miklos Szeredi0d8e84b2015-07-01 16:25:58 +0200669 * Unlock request. If it was aborted while locked, caller is responsible
670 * for unlocking and ending the request.
Miklos Szeredi334f4852005-09-09 13:10:27 -0700671 */
Miklos Szeredidc008092015-07-01 16:25:58 +0200672static int unlock_request(struct fuse_req *req)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700673{
Miklos Szeredi0d8e84b2015-07-01 16:25:58 +0200674 int err = 0;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700675 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 Szeredi0d8e84b2015-07-01 16:25:58 +0200678 err = -ENOENT;
679 else
Miklos Szeredi825d6d32015-07-01 16:25:58 +0200680 clear_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 }
Miklos Szeredi0d8e84b2015-07-01 16:25:58 +0200683 return err;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700684}
685
686struct fuse_copy_state {
687 int write;
688 struct fuse_req *req;
Al Viro6c09e942015-04-03 22:06:08 -0400689 struct iov_iter *iter;
Miklos Szeredidd3bb142010-05-25 15:06:06 +0200690 struct pipe_buffer *pipebufs;
691 struct pipe_buffer *currbuf;
692 struct pipe_inode_info *pipe;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700693 unsigned long nr_segs;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700694 struct page *pg;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700695 unsigned len;
Miklos Szeredic55a01d2014-07-07 15:28:51 +0200696 unsigned offset;
Miklos Szeredice534fb2010-05-25 15:06:07 +0200697 unsigned move_pages:1;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700698};
699
Miklos Szeredidc008092015-07-01 16:25:58 +0200700static void fuse_copy_init(struct fuse_copy_state *cs, int write,
Al Viro6c09e942015-04-03 22:06:08 -0400701 struct iov_iter *iter)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700702{
703 memset(cs, 0, sizeof(*cs));
704 cs->write = write;
Al Viro6c09e942015-04-03 22:06:08 -0400705 cs->iter = iter;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700706}
707
708/* Unmap and put previous page of userspace buffer */
Miklos Szeredi8bfc0162006-01-16 22:14:28 -0800709static void fuse_copy_finish(struct fuse_copy_state *cs)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700710{
Miklos Szeredidd3bb142010-05-25 15:06:06 +0200711 if (cs->currbuf) {
712 struct pipe_buffer *buf = cs->currbuf;
713
Miklos Szeredic55a01d2014-07-07 15:28:51 +0200714 if (cs->write)
Miklos Szeredic3021622010-05-25 15:06:07 +0200715 buf->len = PAGE_SIZE - cs->len;
Miklos Szeredidd3bb142010-05-25 15:06:06 +0200716 cs->currbuf = NULL;
Miklos Szeredic55a01d2014-07-07 15:28:51 +0200717 } else if (cs->pg) {
Miklos Szeredi334f4852005-09-09 13:10:27 -0700718 if (cs->write) {
719 flush_dcache_page(cs->pg);
720 set_page_dirty_lock(cs->pg);
721 }
722 put_page(cs->pg);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700723 }
Miklos Szeredic55a01d2014-07-07 15:28:51 +0200724 cs->pg = NULL;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700725}
726
727/*
728 * Get another pagefull of userspace buffer, and map it to kernel
729 * address space, and lock request
730 */
731static int fuse_copy_fill(struct fuse_copy_state *cs)
732{
Miklos Szeredic55a01d2014-07-07 15:28:51 +0200733 struct page *page;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700734 int err;
735
Miklos Szeredidc008092015-07-01 16:25:58 +0200736 err = unlock_request(cs->req);
Miklos Szeredi0d8e84b2015-07-01 16:25:58 +0200737 if (err)
738 return err;
739
Miklos Szeredi334f4852005-09-09 13:10:27 -0700740 fuse_copy_finish(cs);
Miklos Szeredidd3bb142010-05-25 15:06:06 +0200741 if (cs->pipebufs) {
742 struct pipe_buffer *buf = cs->pipebufs;
743
Miklos Szeredic3021622010-05-25 15:06:07 +0200744 if (!cs->write) {
Miklos Szeredifba597d2016-09-27 10:45:12 +0200745 err = pipe_buf_confirm(cs->pipe, buf);
Miklos Szeredic3021622010-05-25 15:06:07 +0200746 if (err)
747 return err;
Miklos Szeredidd3bb142010-05-25 15:06:06 +0200748
Miklos Szeredic3021622010-05-25 15:06:07 +0200749 BUG_ON(!cs->nr_segs);
750 cs->currbuf = buf;
Miklos Szeredic55a01d2014-07-07 15:28:51 +0200751 cs->pg = buf->page;
752 cs->offset = buf->offset;
Miklos Szeredic3021622010-05-25 15:06:07 +0200753 cs->len = buf->len;
Miklos Szeredic3021622010-05-25 15:06:07 +0200754 cs->pipebufs++;
755 cs->nr_segs--;
756 } else {
Miklos Szeredic3021622010-05-25 15:06:07 +0200757 if (cs->nr_segs == cs->pipe->buffers)
758 return -EIO;
759
760 page = alloc_page(GFP_HIGHUSER);
761 if (!page)
762 return -ENOMEM;
763
764 buf->page = page;
765 buf->offset = 0;
766 buf->len = 0;
767
768 cs->currbuf = buf;
Miklos Szeredic55a01d2014-07-07 15:28:51 +0200769 cs->pg = page;
770 cs->offset = 0;
Miklos Szeredic3021622010-05-25 15:06:07 +0200771 cs->len = PAGE_SIZE;
772 cs->pipebufs++;
773 cs->nr_segs++;
774 }
Miklos Szeredidd3bb142010-05-25 15:06:06 +0200775 } else {
Al Viro6c09e942015-04-03 22:06:08 -0400776 size_t off;
777 err = iov_iter_get_pages(cs->iter, &page, PAGE_SIZE, 1, &off);
Miklos Szeredidd3bb142010-05-25 15:06:06 +0200778 if (err < 0)
779 return err;
Al Viro6c09e942015-04-03 22:06:08 -0400780 BUG_ON(!err);
781 cs->len = err;
782 cs->offset = off;
Miklos Szeredic55a01d2014-07-07 15:28:51 +0200783 cs->pg = page;
Al Viro6c09e942015-04-03 22:06:08 -0400784 iov_iter_advance(cs->iter, err);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700785 }
Miklos Szeredi334f4852005-09-09 13:10:27 -0700786
Miklos Szeredidc008092015-07-01 16:25:58 +0200787 return lock_request(cs->req);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700788}
789
790/* Do as much copy to/from userspace buffer as we can */
Miklos Szeredi8bfc0162006-01-16 22:14:28 -0800791static int fuse_copy_do(struct fuse_copy_state *cs, void **val, unsigned *size)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700792{
793 unsigned ncpy = min(*size, cs->len);
794 if (val) {
Miklos Szeredic55a01d2014-07-07 15:28:51 +0200795 void *pgaddr = kmap_atomic(cs->pg);
796 void *buf = pgaddr + cs->offset;
797
Miklos Szeredi334f4852005-09-09 13:10:27 -0700798 if (cs->write)
Miklos Szeredic55a01d2014-07-07 15:28:51 +0200799 memcpy(buf, *val, ncpy);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700800 else
Miklos Szeredic55a01d2014-07-07 15:28:51 +0200801 memcpy(*val, buf, ncpy);
802
803 kunmap_atomic(pgaddr);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700804 *val += ncpy;
805 }
806 *size -= ncpy;
807 cs->len -= ncpy;
Miklos Szeredic55a01d2014-07-07 15:28:51 +0200808 cs->offset += ncpy;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700809 return ncpy;
810}
811
Miklos Szeredice534fb2010-05-25 15:06:07 +0200812static int fuse_check_page(struct page *page)
813{
814 if (page_mapcount(page) ||
815 page->mapping != NULL ||
816 page_count(page) != 1 ||
817 (page->flags & PAGE_FLAGS_CHECK_AT_PREP &
818 ~(1 << PG_locked |
819 1 << PG_referenced |
820 1 << PG_uptodate |
821 1 << PG_lru |
822 1 << PG_active |
823 1 << PG_reclaim))) {
824 printk(KERN_WARNING "fuse: trying to steal weird page\n");
825 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);
826 return 1;
827 }
828 return 0;
829}
830
831static int fuse_try_move_page(struct fuse_copy_state *cs, struct page **pagep)
832{
833 int err;
834 struct page *oldpage = *pagep;
835 struct page *newpage;
836 struct pipe_buffer *buf = cs->pipebufs;
Miklos Szeredice534fb2010-05-25 15:06:07 +0200837
Miklos Szeredidc008092015-07-01 16:25:58 +0200838 err = unlock_request(cs->req);
Miklos Szeredi0d8e84b2015-07-01 16:25:58 +0200839 if (err)
840 return err;
841
Miklos Szeredice534fb2010-05-25 15:06:07 +0200842 fuse_copy_finish(cs);
843
Miklos Szeredifba597d2016-09-27 10:45:12 +0200844 err = pipe_buf_confirm(cs->pipe, buf);
Miklos Szeredice534fb2010-05-25 15:06:07 +0200845 if (err)
846 return err;
847
848 BUG_ON(!cs->nr_segs);
849 cs->currbuf = buf;
850 cs->len = buf->len;
851 cs->pipebufs++;
852 cs->nr_segs--;
853
854 if (cs->len != PAGE_SIZE)
855 goto out_fallback;
856
Miklos Szeredica76f5b2016-09-27 10:45:12 +0200857 if (pipe_buf_steal(cs->pipe, buf) != 0)
Miklos Szeredice534fb2010-05-25 15:06:07 +0200858 goto out_fallback;
859
860 newpage = buf->page;
861
Miklos Szerediaa991b32015-02-26 11:45:47 +0100862 if (!PageUptodate(newpage))
863 SetPageUptodate(newpage);
Miklos Szeredice534fb2010-05-25 15:06:07 +0200864
865 ClearPageMappedToDisk(newpage);
866
867 if (fuse_check_page(newpage) != 0)
868 goto out_fallback_unlock;
869
Miklos Szeredice534fb2010-05-25 15:06:07 +0200870 /*
871 * This is a new and locked page, it shouldn't be mapped or
872 * have any special flags on it
873 */
874 if (WARN_ON(page_mapped(oldpage)))
875 goto out_fallback_unlock;
876 if (WARN_ON(page_has_private(oldpage)))
877 goto out_fallback_unlock;
878 if (WARN_ON(PageDirty(oldpage) || PageWriteback(oldpage)))
879 goto out_fallback_unlock;
880 if (WARN_ON(PageMlocked(oldpage)))
881 goto out_fallback_unlock;
882
Miklos Szeredief6a3c62011-03-22 16:30:52 -0700883 err = replace_page_cache_page(oldpage, newpage, GFP_KERNEL);
Miklos Szeredice534fb2010-05-25 15:06:07 +0200884 if (err) {
Miklos Szeredief6a3c62011-03-22 16:30:52 -0700885 unlock_page(newpage);
886 return err;
Miklos Szeredice534fb2010-05-25 15:06:07 +0200887 }
Miklos Szeredief6a3c62011-03-22 16:30:52 -0700888
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300889 get_page(newpage);
Miklos Szeredice534fb2010-05-25 15:06:07 +0200890
891 if (!(buf->flags & PIPE_BUF_FLAG_LRU))
892 lru_cache_add_file(newpage);
893
894 err = 0;
Miklos Szeredidc008092015-07-01 16:25:58 +0200895 spin_lock(&cs->req->waitq.lock);
Miklos Szeredi825d6d32015-07-01 16:25:58 +0200896 if (test_bit(FR_ABORTED, &cs->req->flags))
Miklos Szeredice534fb2010-05-25 15:06:07 +0200897 err = -ENOENT;
898 else
899 *pagep = newpage;
Miklos Szeredidc008092015-07-01 16:25:58 +0200900 spin_unlock(&cs->req->waitq.lock);
Miklos Szeredice534fb2010-05-25 15:06:07 +0200901
902 if (err) {
903 unlock_page(newpage);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300904 put_page(newpage);
Miklos Szeredice534fb2010-05-25 15:06:07 +0200905 return err;
906 }
907
908 unlock_page(oldpage);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300909 put_page(oldpage);
Miklos Szeredice534fb2010-05-25 15:06:07 +0200910 cs->len = 0;
911
912 return 0;
913
914out_fallback_unlock:
915 unlock_page(newpage);
916out_fallback:
Miklos Szeredic55a01d2014-07-07 15:28:51 +0200917 cs->pg = buf->page;
918 cs->offset = buf->offset;
Miklos Szeredice534fb2010-05-25 15:06:07 +0200919
Miklos Szeredidc008092015-07-01 16:25:58 +0200920 err = lock_request(cs->req);
Miklos Szeredice534fb2010-05-25 15:06:07 +0200921 if (err)
922 return err;
923
924 return 1;
925}
926
Miklos Szeredic3021622010-05-25 15:06:07 +0200927static int fuse_ref_page(struct fuse_copy_state *cs, struct page *page,
928 unsigned offset, unsigned count)
929{
930 struct pipe_buffer *buf;
Miklos Szeredi0d8e84b2015-07-01 16:25:58 +0200931 int err;
Miklos Szeredic3021622010-05-25 15:06:07 +0200932
933 if (cs->nr_segs == cs->pipe->buffers)
934 return -EIO;
935
Miklos Szeredidc008092015-07-01 16:25:58 +0200936 err = unlock_request(cs->req);
Miklos Szeredi0d8e84b2015-07-01 16:25:58 +0200937 if (err)
938 return err;
939
Miklos Szeredic3021622010-05-25 15:06:07 +0200940 fuse_copy_finish(cs);
941
942 buf = cs->pipebufs;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300943 get_page(page);
Miklos Szeredic3021622010-05-25 15:06:07 +0200944 buf->page = page;
945 buf->offset = offset;
946 buf->len = count;
947
948 cs->pipebufs++;
949 cs->nr_segs++;
950 cs->len = 0;
951
952 return 0;
953}
954
Miklos Szeredi334f4852005-09-09 13:10:27 -0700955/*
956 * Copy a page in the request to/from the userspace buffer. Must be
957 * done atomically
958 */
Miklos Szeredice534fb2010-05-25 15:06:07 +0200959static int fuse_copy_page(struct fuse_copy_state *cs, struct page **pagep,
Miklos Szeredi8bfc0162006-01-16 22:14:28 -0800960 unsigned offset, unsigned count, int zeroing)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700961{
Miklos Szeredice534fb2010-05-25 15:06:07 +0200962 int err;
963 struct page *page = *pagep;
964
Miklos Szeredib6777c42010-10-26 14:22:27 -0700965 if (page && zeroing && count < PAGE_SIZE)
966 clear_highpage(page);
967
Miklos Szeredi334f4852005-09-09 13:10:27 -0700968 while (count) {
Miklos Szeredic3021622010-05-25 15:06:07 +0200969 if (cs->write && cs->pipebufs && page) {
970 return fuse_ref_page(cs, page, offset, count);
971 } else if (!cs->len) {
Miklos Szeredice534fb2010-05-25 15:06:07 +0200972 if (cs->move_pages && page &&
973 offset == 0 && count == PAGE_SIZE) {
974 err = fuse_try_move_page(cs, pagep);
975 if (err <= 0)
976 return err;
977 } else {
978 err = fuse_copy_fill(cs);
979 if (err)
980 return err;
981 }
Miklos Szeredi1729a162008-11-26 12:03:54 +0100982 }
Miklos Szeredi334f4852005-09-09 13:10:27 -0700983 if (page) {
Cong Wang2408f6e2011-11-25 23:14:30 +0800984 void *mapaddr = kmap_atomic(page);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700985 void *buf = mapaddr + offset;
986 offset += fuse_copy_do(cs, &buf, &count);
Cong Wang2408f6e2011-11-25 23:14:30 +0800987 kunmap_atomic(mapaddr);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700988 } else
989 offset += fuse_copy_do(cs, NULL, &count);
990 }
991 if (page && !cs->write)
992 flush_dcache_page(page);
993 return 0;
994}
995
996/* Copy pages in the request to/from userspace buffer */
997static int fuse_copy_pages(struct fuse_copy_state *cs, unsigned nbytes,
998 int zeroing)
999{
1000 unsigned i;
1001 struct fuse_req *req = cs->req;
Miklos Szeredi334f4852005-09-09 13:10:27 -07001002
1003 for (i = 0; i < req->num_pages && (nbytes || zeroing); i++) {
Miklos Szeredice534fb2010-05-25 15:06:07 +02001004 int err;
Maxim Patlasov85f40ae2012-10-26 19:49:33 +04001005 unsigned offset = req->page_descs[i].offset;
1006 unsigned count = min(nbytes, req->page_descs[i].length);
Miklos Szeredice534fb2010-05-25 15:06:07 +02001007
1008 err = fuse_copy_page(cs, &req->pages[i], offset, count,
1009 zeroing);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001010 if (err)
1011 return err;
1012
1013 nbytes -= count;
Miklos Szeredi334f4852005-09-09 13:10:27 -07001014 }
1015 return 0;
1016}
1017
1018/* Copy a single argument in the request to/from userspace buffer */
1019static int fuse_copy_one(struct fuse_copy_state *cs, void *val, unsigned size)
1020{
1021 while (size) {
Miklos Szeredi1729a162008-11-26 12:03:54 +01001022 if (!cs->len) {
1023 int err = fuse_copy_fill(cs);
1024 if (err)
1025 return err;
1026 }
Miklos Szeredi334f4852005-09-09 13:10:27 -07001027 fuse_copy_do(cs, &val, &size);
1028 }
1029 return 0;
1030}
1031
1032/* Copy request arguments to/from userspace buffer */
1033static int fuse_copy_args(struct fuse_copy_state *cs, unsigned numargs,
1034 unsigned argpages, struct fuse_arg *args,
1035 int zeroing)
1036{
1037 int err = 0;
1038 unsigned i;
1039
1040 for (i = 0; !err && i < numargs; i++) {
1041 struct fuse_arg *arg = &args[i];
1042 if (i == numargs - 1 && argpages)
1043 err = fuse_copy_pages(cs, arg->size, zeroing);
1044 else
1045 err = fuse_copy_one(cs, arg->value, arg->size);
1046 }
1047 return err;
1048}
1049
Miklos Szeredif88996a2015-07-01 16:26:01 +02001050static int forget_pending(struct fuse_iqueue *fiq)
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001051{
Miklos Szeredif88996a2015-07-01 16:26:01 +02001052 return fiq->forget_list_head.next != NULL;
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001053}
1054
Miklos Szeredif88996a2015-07-01 16:26:01 +02001055static int request_pending(struct fuse_iqueue *fiq)
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001056{
Miklos Szeredif88996a2015-07-01 16:26:01 +02001057 return !list_empty(&fiq->pending) || !list_empty(&fiq->interrupts) ||
1058 forget_pending(fiq);
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001059}
1060
Miklos Szeredi334f4852005-09-09 13:10:27 -07001061/*
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001062 * Transfer an interrupt request to userspace
1063 *
1064 * Unlike other requests this is assembled on demand, without a need
1065 * to allocate a separate fuse_req structure.
1066 *
Miklos Szeredifd22d622015-07-01 16:26:03 +02001067 * Called with fiq->waitq.lock held, releases it
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001068 */
Miklos Szeredifd22d622015-07-01 16:26:03 +02001069static int fuse_read_interrupt(struct fuse_iqueue *fiq,
1070 struct fuse_copy_state *cs,
Miklos Szeredic3021622010-05-25 15:06:07 +02001071 size_t nbytes, struct fuse_req *req)
Miklos Szeredifd22d622015-07-01 16:26:03 +02001072__releases(fiq->waitq.lock)
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001073{
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001074 struct fuse_in_header ih;
1075 struct fuse_interrupt_in arg;
1076 unsigned reqsize = sizeof(ih) + sizeof(arg);
1077 int err;
1078
1079 list_del_init(&req->intr_entry);
Miklos Szeredi4ce60812015-07-01 16:26:02 +02001080 req->intr_unique = fuse_get_unique(fiq);
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001081 memset(&ih, 0, sizeof(ih));
1082 memset(&arg, 0, sizeof(arg));
1083 ih.len = reqsize;
1084 ih.opcode = FUSE_INTERRUPT;
1085 ih.unique = req->intr_unique;
1086 arg.unique = req->in.h.unique;
1087
Miklos Szeredi4ce60812015-07-01 16:26:02 +02001088 spin_unlock(&fiq->waitq.lock);
Miklos Szeredic3021622010-05-25 15:06:07 +02001089 if (nbytes < reqsize)
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001090 return -EINVAL;
1091
Miklos Szeredic3021622010-05-25 15:06:07 +02001092 err = fuse_copy_one(cs, &ih, sizeof(ih));
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001093 if (!err)
Miklos Szeredic3021622010-05-25 15:06:07 +02001094 err = fuse_copy_one(cs, &arg, sizeof(arg));
1095 fuse_copy_finish(cs);
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001096
1097 return err ? err : reqsize;
1098}
1099
Miklos Szeredif88996a2015-07-01 16:26:01 +02001100static struct fuse_forget_link *dequeue_forget(struct fuse_iqueue *fiq,
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001101 unsigned max,
1102 unsigned *countp)
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001103{
Miklos Szeredif88996a2015-07-01 16:26:01 +02001104 struct fuse_forget_link *head = fiq->forget_list_head.next;
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001105 struct fuse_forget_link **newhead = &head;
1106 unsigned count;
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001107
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001108 for (count = 0; *newhead != NULL && count < max; count++)
1109 newhead = &(*newhead)->next;
1110
Miklos Szeredif88996a2015-07-01 16:26:01 +02001111 fiq->forget_list_head.next = *newhead;
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001112 *newhead = NULL;
Miklos Szeredif88996a2015-07-01 16:26:01 +02001113 if (fiq->forget_list_head.next == NULL)
1114 fiq->forget_list_tail = &fiq->forget_list_head;
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001115
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001116 if (countp != NULL)
1117 *countp = count;
1118
1119 return head;
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001120}
1121
Miklos Szeredifd22d622015-07-01 16:26:03 +02001122static int fuse_read_single_forget(struct fuse_iqueue *fiq,
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001123 struct fuse_copy_state *cs,
1124 size_t nbytes)
Miklos Szeredifd22d622015-07-01 16:26:03 +02001125__releases(fiq->waitq.lock)
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001126{
1127 int err;
Miklos Szeredif88996a2015-07-01 16:26:01 +02001128 struct fuse_forget_link *forget = dequeue_forget(fiq, 1, NULL);
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001129 struct fuse_forget_in arg = {
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001130 .nlookup = forget->forget_one.nlookup,
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001131 };
1132 struct fuse_in_header ih = {
1133 .opcode = FUSE_FORGET,
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001134 .nodeid = forget->forget_one.nodeid,
Miklos Szeredif88996a2015-07-01 16:26:01 +02001135 .unique = fuse_get_unique(fiq),
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001136 .len = sizeof(ih) + sizeof(arg),
1137 };
1138
Miklos Szeredi4ce60812015-07-01 16:26:02 +02001139 spin_unlock(&fiq->waitq.lock);
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001140 kfree(forget);
1141 if (nbytes < ih.len)
1142 return -EINVAL;
1143
1144 err = fuse_copy_one(cs, &ih, sizeof(ih));
1145 if (!err)
1146 err = fuse_copy_one(cs, &arg, sizeof(arg));
1147 fuse_copy_finish(cs);
1148
1149 if (err)
1150 return err;
1151
1152 return ih.len;
1153}
1154
Miklos Szeredifd22d622015-07-01 16:26:03 +02001155static int fuse_read_batch_forget(struct fuse_iqueue *fiq,
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001156 struct fuse_copy_state *cs, size_t nbytes)
Miklos Szeredifd22d622015-07-01 16:26:03 +02001157__releases(fiq->waitq.lock)
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001158{
1159 int err;
1160 unsigned max_forgets;
1161 unsigned count;
1162 struct fuse_forget_link *head;
1163 struct fuse_batch_forget_in arg = { .count = 0 };
1164 struct fuse_in_header ih = {
1165 .opcode = FUSE_BATCH_FORGET,
Miklos Szeredif88996a2015-07-01 16:26:01 +02001166 .unique = fuse_get_unique(fiq),
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001167 .len = sizeof(ih) + sizeof(arg),
1168 };
1169
1170 if (nbytes < ih.len) {
Miklos Szeredi4ce60812015-07-01 16:26:02 +02001171 spin_unlock(&fiq->waitq.lock);
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001172 return -EINVAL;
1173 }
1174
1175 max_forgets = (nbytes - ih.len) / sizeof(struct fuse_forget_one);
Miklos Szeredif88996a2015-07-01 16:26:01 +02001176 head = dequeue_forget(fiq, max_forgets, &count);
Miklos Szeredi4ce60812015-07-01 16:26:02 +02001177 spin_unlock(&fiq->waitq.lock);
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001178
1179 arg.count = count;
1180 ih.len += count * sizeof(struct fuse_forget_one);
1181 err = fuse_copy_one(cs, &ih, sizeof(ih));
1182 if (!err)
1183 err = fuse_copy_one(cs, &arg, sizeof(arg));
1184
1185 while (head) {
1186 struct fuse_forget_link *forget = head;
1187
1188 if (!err) {
1189 err = fuse_copy_one(cs, &forget->forget_one,
1190 sizeof(forget->forget_one));
1191 }
1192 head = forget->next;
1193 kfree(forget);
1194 }
1195
1196 fuse_copy_finish(cs);
1197
1198 if (err)
1199 return err;
1200
1201 return ih.len;
1202}
1203
Miklos Szeredifd22d622015-07-01 16:26:03 +02001204static int fuse_read_forget(struct fuse_conn *fc, struct fuse_iqueue *fiq,
1205 struct fuse_copy_state *cs,
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001206 size_t nbytes)
Miklos Szeredifd22d622015-07-01 16:26:03 +02001207__releases(fiq->waitq.lock)
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001208{
Miklos Szeredif88996a2015-07-01 16:26:01 +02001209 if (fc->minor < 16 || fiq->forget_list_head.next->next == NULL)
Miklos Szeredifd22d622015-07-01 16:26:03 +02001210 return fuse_read_single_forget(fiq, cs, nbytes);
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001211 else
Miklos Szeredifd22d622015-07-01 16:26:03 +02001212 return fuse_read_batch_forget(fiq, cs, nbytes);
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001213}
1214
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001215/*
Miklos Szeredi334f4852005-09-09 13:10:27 -07001216 * Read a single request into the userspace filesystem's buffer. This
1217 * function waits until a request is available, then removes it from
1218 * the pending list and copies request data to userspace buffer. If
Miklos Szeredif9a28422006-06-25 05:48:53 -07001219 * no reply is needed (FORGET) or request has been aborted or there
1220 * was an error during the copying then it's finished by calling
Miklos Szeredi334f4852005-09-09 13:10:27 -07001221 * request_end(). Otherwise add it to the processing list, and set
1222 * the 'sent' flag.
1223 */
Miklos Szeredic36960462015-07-01 16:26:09 +02001224static ssize_t fuse_dev_do_read(struct fuse_dev *fud, struct file *file,
Miklos Szeredic3021622010-05-25 15:06:07 +02001225 struct fuse_copy_state *cs, size_t nbytes)
Miklos Szeredi334f4852005-09-09 13:10:27 -07001226{
Miklos Szeredi82cbdcd2015-07-01 16:26:05 +02001227 ssize_t err;
Miklos Szeredic36960462015-07-01 16:26:09 +02001228 struct fuse_conn *fc = fud->fc;
Miklos Szeredif88996a2015-07-01 16:26:01 +02001229 struct fuse_iqueue *fiq = &fc->iq;
Miklos Szeredic36960462015-07-01 16:26:09 +02001230 struct fuse_pqueue *fpq = &fud->pq;
Miklos Szeredi334f4852005-09-09 13:10:27 -07001231 struct fuse_req *req;
1232 struct fuse_in *in;
Miklos Szeredi334f4852005-09-09 13:10:27 -07001233 unsigned reqsize;
1234
Miklos Szeredi1d3d7522006-01-06 00:19:40 -08001235 restart:
Miklos Szeredi4ce60812015-07-01 16:26:02 +02001236 spin_lock(&fiq->waitq.lock);
Jeff Dikee5ac1d12006-04-10 22:54:53 -07001237 err = -EAGAIN;
Miklos Szeredie16714d2015-07-01 16:26:01 +02001238 if ((file->f_flags & O_NONBLOCK) && fiq->connected &&
Miklos Szeredif88996a2015-07-01 16:26:01 +02001239 !request_pending(fiq))
Jeff Dikee5ac1d12006-04-10 22:54:53 -07001240 goto err_unlock;
1241
Miklos Szeredi52509212015-07-01 16:26:03 +02001242 err = wait_event_interruptible_exclusive_locked(fiq->waitq,
1243 !fiq->connected || request_pending(fiq));
1244 if (err)
1245 goto err_unlock;
1246
Miklos Szeredi334f4852005-09-09 13:10:27 -07001247 err = -ENODEV;
Miklos Szeredie16714d2015-07-01 16:26:01 +02001248 if (!fiq->connected)
Miklos Szeredi334f4852005-09-09 13:10:27 -07001249 goto err_unlock;
Miklos Szeredi334f4852005-09-09 13:10:27 -07001250
Miklos Szeredif88996a2015-07-01 16:26:01 +02001251 if (!list_empty(&fiq->interrupts)) {
1252 req = list_entry(fiq->interrupts.next, struct fuse_req,
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001253 intr_entry);
Miklos Szeredifd22d622015-07-01 16:26:03 +02001254 return fuse_read_interrupt(fiq, cs, nbytes, req);
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001255 }
1256
Miklos Szeredif88996a2015-07-01 16:26:01 +02001257 if (forget_pending(fiq)) {
1258 if (list_empty(&fiq->pending) || fiq->forget_batch-- > 0)
Miklos Szeredifd22d622015-07-01 16:26:03 +02001259 return fuse_read_forget(fc, fiq, cs, nbytes);
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001260
Miklos Szeredif88996a2015-07-01 16:26:01 +02001261 if (fiq->forget_batch <= -8)
1262 fiq->forget_batch = 16;
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001263 }
1264
Miklos Szeredif88996a2015-07-01 16:26:01 +02001265 req = list_entry(fiq->pending.next, struct fuse_req, list);
Miklos Szeredi33e14b42015-07-01 16:26:01 +02001266 clear_bit(FR_PENDING, &req->flags);
Miklos Szeredief759252015-07-01 16:26:02 +02001267 list_del_init(&req->list);
Miklos Szeredi4ce60812015-07-01 16:26:02 +02001268 spin_unlock(&fiq->waitq.lock);
1269
Miklos Szeredi334f4852005-09-09 13:10:27 -07001270 in = &req->in;
Miklos Szeredi1d3d7522006-01-06 00:19:40 -08001271 reqsize = in->h.len;
1272 /* If request is too large, reply with an error and restart the read */
Miklos Szeredic3021622010-05-25 15:06:07 +02001273 if (nbytes < reqsize) {
Miklos Szeredi1d3d7522006-01-06 00:19:40 -08001274 req->out.h.error = -EIO;
1275 /* SETXATTR is special, since it may contain too large data */
1276 if (in->h.opcode == FUSE_SETXATTR)
1277 req->out.h.error = -E2BIG;
1278 request_end(fc, req);
1279 goto restart;
Miklos Szeredi334f4852005-09-09 13:10:27 -07001280 }
Miklos Szeredi45a91cb2015-07-01 16:26:06 +02001281 spin_lock(&fpq->lock);
Miklos Szeredi82cbdcd2015-07-01 16:26:05 +02001282 list_add(&req->list, &fpq->io);
Miklos Szeredi45a91cb2015-07-01 16:26:06 +02001283 spin_unlock(&fpq->lock);
Miklos Szeredic3021622010-05-25 15:06:07 +02001284 cs->req = req;
1285 err = fuse_copy_one(cs, &in->h, sizeof(in->h));
Miklos Szeredi1d3d7522006-01-06 00:19:40 -08001286 if (!err)
Miklos Szeredic3021622010-05-25 15:06:07 +02001287 err = fuse_copy_args(cs, in->numargs, in->argpages,
Miklos Szeredi1d3d7522006-01-06 00:19:40 -08001288 (struct fuse_arg *) in->args, 0);
Miklos Szeredic3021622010-05-25 15:06:07 +02001289 fuse_copy_finish(cs);
Miklos Szeredi45a91cb2015-07-01 16:26:06 +02001290 spin_lock(&fpq->lock);
Miklos Szeredi825d6d32015-07-01 16:25:58 +02001291 clear_bit(FR_LOCKED, &req->flags);
Miklos Szeredie96edd92015-07-01 16:26:04 +02001292 if (!fpq->connected) {
Miklos Szeredi82cbdcd2015-07-01 16:26:05 +02001293 err = -ENODEV;
1294 goto out_end;
Miklos Szeredic9c9d7d2007-10-16 23:31:05 -07001295 }
Miklos Szeredi334f4852005-09-09 13:10:27 -07001296 if (err) {
Miklos Szeredic9c9d7d2007-10-16 23:31:05 -07001297 req->out.h.error = -EIO;
Miklos Szeredi82cbdcd2015-07-01 16:26:05 +02001298 goto out_end;
Miklos Szeredi334f4852005-09-09 13:10:27 -07001299 }
Miklos Szeredi825d6d32015-07-01 16:25:58 +02001300 if (!test_bit(FR_ISREPLY, &req->flags)) {
Miklos Szeredi82cbdcd2015-07-01 16:26:05 +02001301 err = reqsize;
1302 goto out_end;
Miklos Szeredi334f4852005-09-09 13:10:27 -07001303 }
Miklos Szeredi82cbdcd2015-07-01 16:26:05 +02001304 list_move_tail(&req->list, &fpq->processing);
Miklos Szeredi45a91cb2015-07-01 16:26:06 +02001305 spin_unlock(&fpq->lock);
Miklos Szeredi82cbdcd2015-07-01 16:26:05 +02001306 set_bit(FR_SENT, &req->flags);
1307 /* matches barrier in request_wait_answer() */
1308 smp_mb__after_atomic();
1309 if (test_bit(FR_INTERRUPTED, &req->flags))
1310 queue_interrupt(fiq, req);
Miklos Szeredi82cbdcd2015-07-01 16:26:05 +02001311
Miklos Szeredi334f4852005-09-09 13:10:27 -07001312 return reqsize;
1313
Miklos Szeredi82cbdcd2015-07-01 16:26:05 +02001314out_end:
Miklos Szeredi77cd9d42015-07-01 16:26:06 +02001315 if (!test_bit(FR_PRIVATE, &req->flags))
1316 list_del_init(&req->list);
Miklos Szeredi45a91cb2015-07-01 16:26:06 +02001317 spin_unlock(&fpq->lock);
Miklos Szeredi82cbdcd2015-07-01 16:26:05 +02001318 request_end(fc, req);
1319 return err;
1320
Miklos Szeredi334f4852005-09-09 13:10:27 -07001321 err_unlock:
Miklos Szeredi4ce60812015-07-01 16:26:02 +02001322 spin_unlock(&fiq->waitq.lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001323 return err;
1324}
1325
Tom Van Braeckel94e4fe22015-01-12 05:22:16 +01001326static int fuse_dev_open(struct inode *inode, struct file *file)
1327{
1328 /*
1329 * The fuse device's file's private_data is used to hold
1330 * the fuse_conn(ection) when it is mounted, and is used to
1331 * keep track of whether the file has been mounted already.
1332 */
1333 file->private_data = NULL;
1334 return 0;
1335}
1336
Al Virofbdbacc2015-04-03 21:53:39 -04001337static ssize_t fuse_dev_read(struct kiocb *iocb, struct iov_iter *to)
Miklos Szeredic3021622010-05-25 15:06:07 +02001338{
1339 struct fuse_copy_state cs;
1340 struct file *file = iocb->ki_filp;
Miklos Szeredicc080e92015-07-01 16:26:08 +02001341 struct fuse_dev *fud = fuse_get_dev(file);
1342
1343 if (!fud)
Miklos Szeredic3021622010-05-25 15:06:07 +02001344 return -EPERM;
1345
Al Virofbdbacc2015-04-03 21:53:39 -04001346 if (!iter_is_iovec(to))
1347 return -EINVAL;
Miklos Szeredic3021622010-05-25 15:06:07 +02001348
Miklos Szeredidc008092015-07-01 16:25:58 +02001349 fuse_copy_init(&cs, 1, to);
Al Virofbdbacc2015-04-03 21:53:39 -04001350
Miklos Szeredic36960462015-07-01 16:26:09 +02001351 return fuse_dev_do_read(fud, file, &cs, iov_iter_count(to));
Miklos Szeredic3021622010-05-25 15:06:07 +02001352}
1353
Miklos Szeredic3021622010-05-25 15:06:07 +02001354static ssize_t fuse_dev_splice_read(struct file *in, loff_t *ppos,
1355 struct pipe_inode_info *pipe,
1356 size_t len, unsigned int flags)
1357{
Al Virod82718e2016-09-17 22:56:25 -04001358 int total, ret;
Miklos Szeredic3021622010-05-25 15:06:07 +02001359 int page_nr = 0;
Miklos Szeredic3021622010-05-25 15:06:07 +02001360 struct pipe_buffer *bufs;
1361 struct fuse_copy_state cs;
Miklos Szeredicc080e92015-07-01 16:26:08 +02001362 struct fuse_dev *fud = fuse_get_dev(in);
1363
1364 if (!fud)
Miklos Szeredic3021622010-05-25 15:06:07 +02001365 return -EPERM;
1366
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001367 bufs = kmalloc(pipe->buffers * sizeof(struct pipe_buffer), GFP_KERNEL);
Miklos Szeredic3021622010-05-25 15:06:07 +02001368 if (!bufs)
1369 return -ENOMEM;
1370
Miklos Szeredidc008092015-07-01 16:25:58 +02001371 fuse_copy_init(&cs, 1, NULL);
Miklos Szeredic3021622010-05-25 15:06:07 +02001372 cs.pipebufs = bufs;
1373 cs.pipe = pipe;
Miklos Szeredic36960462015-07-01 16:26:09 +02001374 ret = fuse_dev_do_read(fud, in, &cs, len);
Miklos Szeredic3021622010-05-25 15:06:07 +02001375 if (ret < 0)
1376 goto out;
1377
Miklos Szeredic3021622010-05-25 15:06:07 +02001378 if (pipe->nrbufs + cs.nr_segs > pipe->buffers) {
1379 ret = -EIO;
Al Virod82718e2016-09-17 22:56:25 -04001380 goto out;
Miklos Szeredic3021622010-05-25 15:06:07 +02001381 }
1382
Al Virod82718e2016-09-17 22:56:25 -04001383 for (ret = total = 0; page_nr < cs.nr_segs; total += ret) {
Miklos Szeredi28a625c2014-01-22 19:36:57 +01001384 /*
1385 * Need to be careful about this. Having buf->ops in module
1386 * code can Oops if the buffer persists after module unload.
1387 */
Al Virod82718e2016-09-17 22:56:25 -04001388 bufs[page_nr].ops = &nosteal_pipe_buf_ops;
Miklos Szeredi80a04772017-02-16 15:08:20 +01001389 bufs[page_nr].flags = 0;
Al Virod82718e2016-09-17 22:56:25 -04001390 ret = add_to_pipe(pipe, &bufs[page_nr++]);
1391 if (unlikely(ret < 0))
1392 break;
Miklos Szeredic3021622010-05-25 15:06:07 +02001393 }
Al Virod82718e2016-09-17 22:56:25 -04001394 if (total)
1395 ret = total;
Miklos Szeredic3021622010-05-25 15:06:07 +02001396out:
1397 for (; page_nr < cs.nr_segs; page_nr++)
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001398 put_page(bufs[page_nr].page);
Miklos Szeredic3021622010-05-25 15:06:07 +02001399
1400 kfree(bufs);
1401 return ret;
1402}
1403
Tejun Heo95668a62008-11-26 12:03:55 +01001404static int fuse_notify_poll(struct fuse_conn *fc, unsigned int size,
1405 struct fuse_copy_state *cs)
1406{
1407 struct fuse_notify_poll_wakeup_out outarg;
Miklos Szeredif6d47a12009-01-26 15:00:59 +01001408 int err = -EINVAL;
Tejun Heo95668a62008-11-26 12:03:55 +01001409
1410 if (size != sizeof(outarg))
Miklos Szeredif6d47a12009-01-26 15:00:59 +01001411 goto err;
Tejun Heo95668a62008-11-26 12:03:55 +01001412
1413 err = fuse_copy_one(cs, &outarg, sizeof(outarg));
1414 if (err)
Miklos Szeredif6d47a12009-01-26 15:00:59 +01001415 goto err;
Tejun Heo95668a62008-11-26 12:03:55 +01001416
Miklos Szeredif6d47a12009-01-26 15:00:59 +01001417 fuse_copy_finish(cs);
Tejun Heo95668a62008-11-26 12:03:55 +01001418 return fuse_notify_poll_wakeup(fc, &outarg);
Miklos Szeredif6d47a12009-01-26 15:00:59 +01001419
1420err:
1421 fuse_copy_finish(cs);
1422 return err;
Tejun Heo95668a62008-11-26 12:03:55 +01001423}
1424
John Muir3b463ae2009-05-31 11:13:57 -04001425static int fuse_notify_inval_inode(struct fuse_conn *fc, unsigned int size,
1426 struct fuse_copy_state *cs)
1427{
1428 struct fuse_notify_inval_inode_out outarg;
1429 int err = -EINVAL;
1430
1431 if (size != sizeof(outarg))
1432 goto err;
1433
1434 err = fuse_copy_one(cs, &outarg, sizeof(outarg));
1435 if (err)
1436 goto err;
1437 fuse_copy_finish(cs);
1438
1439 down_read(&fc->killsb);
1440 err = -ENOENT;
Miklos Szeredib21dda42010-02-05 12:08:31 +01001441 if (fc->sb) {
1442 err = fuse_reverse_inval_inode(fc->sb, outarg.ino,
1443 outarg.off, outarg.len);
1444 }
John Muir3b463ae2009-05-31 11:13:57 -04001445 up_read(&fc->killsb);
1446 return err;
1447
1448err:
1449 fuse_copy_finish(cs);
1450 return err;
1451}
1452
1453static int fuse_notify_inval_entry(struct fuse_conn *fc, unsigned int size,
1454 struct fuse_copy_state *cs)
1455{
1456 struct fuse_notify_inval_entry_out outarg;
Fang Wenqib2d82ee2009-12-30 18:37:13 +08001457 int err = -ENOMEM;
1458 char *buf;
John Muir3b463ae2009-05-31 11:13:57 -04001459 struct qstr name;
1460
Fang Wenqib2d82ee2009-12-30 18:37:13 +08001461 buf = kzalloc(FUSE_NAME_MAX + 1, GFP_KERNEL);
1462 if (!buf)
1463 goto err;
1464
1465 err = -EINVAL;
John Muir3b463ae2009-05-31 11:13:57 -04001466 if (size < sizeof(outarg))
1467 goto err;
1468
1469 err = fuse_copy_one(cs, &outarg, sizeof(outarg));
1470 if (err)
1471 goto err;
1472
1473 err = -ENAMETOOLONG;
1474 if (outarg.namelen > FUSE_NAME_MAX)
1475 goto err;
1476
Miklos Szeredic2183d12011-08-24 10:20:17 +02001477 err = -EINVAL;
1478 if (size != sizeof(outarg) + outarg.namelen + 1)
1479 goto err;
1480
John Muir3b463ae2009-05-31 11:13:57 -04001481 name.name = buf;
1482 name.len = outarg.namelen;
1483 err = fuse_copy_one(cs, buf, outarg.namelen + 1);
1484 if (err)
1485 goto err;
1486 fuse_copy_finish(cs);
1487 buf[outarg.namelen] = 0;
John Muir3b463ae2009-05-31 11:13:57 -04001488
1489 down_read(&fc->killsb);
1490 err = -ENOENT;
Miklos Szeredib21dda42010-02-05 12:08:31 +01001491 if (fc->sb)
John Muir451d0f52011-12-06 21:50:06 +01001492 err = fuse_reverse_inval_entry(fc->sb, outarg.parent, 0, &name);
1493 up_read(&fc->killsb);
1494 kfree(buf);
1495 return err;
1496
1497err:
1498 kfree(buf);
1499 fuse_copy_finish(cs);
1500 return err;
1501}
1502
1503static int fuse_notify_delete(struct fuse_conn *fc, unsigned int size,
1504 struct fuse_copy_state *cs)
1505{
1506 struct fuse_notify_delete_out outarg;
1507 int err = -ENOMEM;
1508 char *buf;
1509 struct qstr name;
1510
1511 buf = kzalloc(FUSE_NAME_MAX + 1, GFP_KERNEL);
1512 if (!buf)
1513 goto err;
1514
1515 err = -EINVAL;
1516 if (size < sizeof(outarg))
1517 goto err;
1518
1519 err = fuse_copy_one(cs, &outarg, sizeof(outarg));
1520 if (err)
1521 goto err;
1522
1523 err = -ENAMETOOLONG;
1524 if (outarg.namelen > FUSE_NAME_MAX)
1525 goto err;
1526
1527 err = -EINVAL;
1528 if (size != sizeof(outarg) + outarg.namelen + 1)
1529 goto err;
1530
1531 name.name = buf;
1532 name.len = outarg.namelen;
1533 err = fuse_copy_one(cs, buf, outarg.namelen + 1);
1534 if (err)
1535 goto err;
1536 fuse_copy_finish(cs);
1537 buf[outarg.namelen] = 0;
John Muir451d0f52011-12-06 21:50:06 +01001538
1539 down_read(&fc->killsb);
1540 err = -ENOENT;
1541 if (fc->sb)
1542 err = fuse_reverse_inval_entry(fc->sb, outarg.parent,
1543 outarg.child, &name);
John Muir3b463ae2009-05-31 11:13:57 -04001544 up_read(&fc->killsb);
Fang Wenqib2d82ee2009-12-30 18:37:13 +08001545 kfree(buf);
John Muir3b463ae2009-05-31 11:13:57 -04001546 return err;
1547
1548err:
Fang Wenqib2d82ee2009-12-30 18:37:13 +08001549 kfree(buf);
John Muir3b463ae2009-05-31 11:13:57 -04001550 fuse_copy_finish(cs);
1551 return err;
1552}
1553
Miklos Szeredia1d75f22010-07-12 14:41:40 +02001554static int fuse_notify_store(struct fuse_conn *fc, unsigned int size,
1555 struct fuse_copy_state *cs)
1556{
1557 struct fuse_notify_store_out outarg;
1558 struct inode *inode;
1559 struct address_space *mapping;
1560 u64 nodeid;
1561 int err;
1562 pgoff_t index;
1563 unsigned int offset;
1564 unsigned int num;
1565 loff_t file_size;
1566 loff_t end;
1567
1568 err = -EINVAL;
1569 if (size < sizeof(outarg))
1570 goto out_finish;
1571
1572 err = fuse_copy_one(cs, &outarg, sizeof(outarg));
1573 if (err)
1574 goto out_finish;
1575
1576 err = -EINVAL;
1577 if (size - sizeof(outarg) != outarg.size)
1578 goto out_finish;
1579
1580 nodeid = outarg.nodeid;
1581
1582 down_read(&fc->killsb);
1583
1584 err = -ENOENT;
1585 if (!fc->sb)
1586 goto out_up_killsb;
1587
1588 inode = ilookup5(fc->sb, nodeid, fuse_inode_eq, &nodeid);
1589 if (!inode)
1590 goto out_up_killsb;
1591
1592 mapping = inode->i_mapping;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001593 index = outarg.offset >> PAGE_SHIFT;
1594 offset = outarg.offset & ~PAGE_MASK;
Miklos Szeredia1d75f22010-07-12 14:41:40 +02001595 file_size = i_size_read(inode);
1596 end = outarg.offset + outarg.size;
1597 if (end > file_size) {
1598 file_size = end;
1599 fuse_write_update_size(inode, file_size);
1600 }
1601
1602 num = outarg.size;
1603 while (num) {
1604 struct page *page;
1605 unsigned int this_num;
1606
1607 err = -ENOMEM;
1608 page = find_or_create_page(mapping, index,
1609 mapping_gfp_mask(mapping));
1610 if (!page)
1611 goto out_iput;
1612
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001613 this_num = min_t(unsigned, num, PAGE_SIZE - offset);
Miklos Szeredia1d75f22010-07-12 14:41:40 +02001614 err = fuse_copy_page(cs, &page, offset, this_num, 0);
Miklos Szeredi063ec1e2014-01-22 19:36:58 +01001615 if (!err && offset == 0 &&
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001616 (this_num == PAGE_SIZE || file_size == end))
Miklos Szeredia1d75f22010-07-12 14:41:40 +02001617 SetPageUptodate(page);
1618 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001619 put_page(page);
Miklos Szeredia1d75f22010-07-12 14:41:40 +02001620
1621 if (err)
1622 goto out_iput;
1623
1624 num -= this_num;
1625 offset = 0;
1626 index++;
1627 }
1628
1629 err = 0;
1630
1631out_iput:
1632 iput(inode);
1633out_up_killsb:
1634 up_read(&fc->killsb);
1635out_finish:
1636 fuse_copy_finish(cs);
1637 return err;
1638}
1639
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001640static void fuse_retrieve_end(struct fuse_conn *fc, struct fuse_req *req)
1641{
Mel Gormanb745bc82014-06-04 16:10:22 -07001642 release_pages(req->pages, req->num_pages, false);
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001643}
1644
1645static int fuse_retrieve(struct fuse_conn *fc, struct inode *inode,
1646 struct fuse_notify_retrieve_out *outarg)
1647{
1648 int err;
1649 struct address_space *mapping = inode->i_mapping;
1650 struct fuse_req *req;
1651 pgoff_t index;
1652 loff_t file_size;
1653 unsigned int num;
1654 unsigned int offset;
Geert Uytterhoeven01574432010-09-30 22:06:21 +02001655 size_t total_len = 0;
Maxim Patlasov4d53dc92012-10-26 19:48:42 +04001656 int num_pages;
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001657
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001658 offset = outarg->offset & ~PAGE_MASK;
Maxim Patlasov4d53dc92012-10-26 19:48:42 +04001659 file_size = i_size_read(inode);
1660
1661 num = outarg->size;
1662 if (outarg->offset > file_size)
1663 num = 0;
1664 else if (outarg->offset + num > file_size)
1665 num = file_size - outarg->offset;
1666
1667 num_pages = (num + offset + PAGE_SIZE - 1) >> PAGE_SHIFT;
1668 num_pages = min(num_pages, FUSE_MAX_PAGES_PER_REQ);
1669
1670 req = fuse_get_req(fc, num_pages);
1671 if (IS_ERR(req))
1672 return PTR_ERR(req);
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001673
1674 req->in.h.opcode = FUSE_NOTIFY_REPLY;
1675 req->in.h.nodeid = outarg->nodeid;
1676 req->in.numargs = 2;
1677 req->in.argpages = 1;
Maxim Patlasovb2430d72012-10-26 19:49:24 +04001678 req->page_descs[0].offset = offset;
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001679 req->end = fuse_retrieve_end;
1680
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001681 index = outarg->offset >> PAGE_SHIFT;
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001682
Maxim Patlasov4d53dc92012-10-26 19:48:42 +04001683 while (num && req->num_pages < num_pages) {
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001684 struct page *page;
1685 unsigned int this_num;
1686
1687 page = find_get_page(mapping, index);
1688 if (!page)
1689 break;
1690
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001691 this_num = min_t(unsigned, num, PAGE_SIZE - offset);
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001692 req->pages[req->num_pages] = page;
Maxim Patlasov85f40ae2012-10-26 19:49:33 +04001693 req->page_descs[req->num_pages].length = this_num;
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001694 req->num_pages++;
1695
Miklos Szeredic9e67d42012-09-04 18:45:54 +02001696 offset = 0;
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001697 num -= this_num;
1698 total_len += this_num;
Miklos Szeredi48706d02011-12-13 10:36:59 +01001699 index++;
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001700 }
1701 req->misc.retrieve_in.offset = outarg->offset;
1702 req->misc.retrieve_in.size = total_len;
1703 req->in.args[0].size = sizeof(req->misc.retrieve_in);
1704 req->in.args[0].value = &req->misc.retrieve_in;
1705 req->in.args[1].size = total_len;
1706
1707 err = fuse_request_send_notify_reply(fc, req, outarg->notify_unique);
1708 if (err)
1709 fuse_retrieve_end(fc, req);
1710
1711 return err;
1712}
1713
1714static int fuse_notify_retrieve(struct fuse_conn *fc, unsigned int size,
1715 struct fuse_copy_state *cs)
1716{
1717 struct fuse_notify_retrieve_out outarg;
1718 struct inode *inode;
1719 int err;
1720
1721 err = -EINVAL;
1722 if (size != sizeof(outarg))
1723 goto copy_finish;
1724
1725 err = fuse_copy_one(cs, &outarg, sizeof(outarg));
1726 if (err)
1727 goto copy_finish;
1728
1729 fuse_copy_finish(cs);
1730
1731 down_read(&fc->killsb);
1732 err = -ENOENT;
1733 if (fc->sb) {
1734 u64 nodeid = outarg.nodeid;
1735
1736 inode = ilookup5(fc->sb, nodeid, fuse_inode_eq, &nodeid);
1737 if (inode) {
1738 err = fuse_retrieve(fc, inode, &outarg);
1739 iput(inode);
1740 }
1741 }
1742 up_read(&fc->killsb);
1743
1744 return err;
1745
1746copy_finish:
1747 fuse_copy_finish(cs);
1748 return err;
1749}
1750
Tejun Heo85993962008-11-26 12:03:55 +01001751static int fuse_notify(struct fuse_conn *fc, enum fuse_notify_code code,
1752 unsigned int size, struct fuse_copy_state *cs)
1753{
Miklos Szeredi0d278362015-02-26 11:45:47 +01001754 /* Don't try to move pages (yet) */
1755 cs->move_pages = 0;
1756
Tejun Heo85993962008-11-26 12:03:55 +01001757 switch (code) {
Tejun Heo95668a62008-11-26 12:03:55 +01001758 case FUSE_NOTIFY_POLL:
1759 return fuse_notify_poll(fc, size, cs);
1760
John Muir3b463ae2009-05-31 11:13:57 -04001761 case FUSE_NOTIFY_INVAL_INODE:
1762 return fuse_notify_inval_inode(fc, size, cs);
1763
1764 case FUSE_NOTIFY_INVAL_ENTRY:
1765 return fuse_notify_inval_entry(fc, size, cs);
1766
Miklos Szeredia1d75f22010-07-12 14:41:40 +02001767 case FUSE_NOTIFY_STORE:
1768 return fuse_notify_store(fc, size, cs);
1769
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001770 case FUSE_NOTIFY_RETRIEVE:
1771 return fuse_notify_retrieve(fc, size, cs);
1772
John Muir451d0f52011-12-06 21:50:06 +01001773 case FUSE_NOTIFY_DELETE:
1774 return fuse_notify_delete(fc, size, cs);
1775
Tejun Heo85993962008-11-26 12:03:55 +01001776 default:
Miklos Szeredif6d47a12009-01-26 15:00:59 +01001777 fuse_copy_finish(cs);
Tejun Heo85993962008-11-26 12:03:55 +01001778 return -EINVAL;
1779 }
1780}
1781
Miklos Szeredi334f4852005-09-09 13:10:27 -07001782/* Look up request on processing list by unique ID */
Miklos Szeredi3a2b5b92015-07-01 16:26:04 +02001783static struct fuse_req *request_find(struct fuse_pqueue *fpq, u64 unique)
Miklos Szeredi334f4852005-09-09 13:10:27 -07001784{
Dong Fang05726ac2013-07-30 22:50:01 -04001785 struct fuse_req *req;
Miklos Szeredi334f4852005-09-09 13:10:27 -07001786
Miklos Szeredi3a2b5b92015-07-01 16:26:04 +02001787 list_for_each_entry(req, &fpq->processing, list) {
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001788 if (req->in.h.unique == unique || req->intr_unique == unique)
Miklos Szeredi334f4852005-09-09 13:10:27 -07001789 return req;
1790 }
1791 return NULL;
1792}
1793
1794static int copy_out_args(struct fuse_copy_state *cs, struct fuse_out *out,
1795 unsigned nbytes)
1796{
1797 unsigned reqsize = sizeof(struct fuse_out_header);
1798
1799 if (out->h.error)
1800 return nbytes != reqsize ? -EINVAL : 0;
1801
1802 reqsize += len_args(out->numargs, out->args);
1803
1804 if (reqsize < nbytes || (reqsize > nbytes && !out->argvar))
1805 return -EINVAL;
1806 else if (reqsize > nbytes) {
1807 struct fuse_arg *lastarg = &out->args[out->numargs-1];
1808 unsigned diffsize = reqsize - nbytes;
1809 if (diffsize > lastarg->size)
1810 return -EINVAL;
1811 lastarg->size -= diffsize;
1812 }
1813 return fuse_copy_args(cs, out->numargs, out->argpages, out->args,
1814 out->page_zeroing);
1815}
1816
1817/*
1818 * Write a single reply to a request. First the header is copied from
1819 * the write buffer. The request is then searched on the processing
1820 * list by the unique ID found in the header. If found, then remove
1821 * it from the list and copy the rest of the buffer to the request.
1822 * The request is finished by calling request_end()
1823 */
Miklos Szeredic36960462015-07-01 16:26:09 +02001824static ssize_t fuse_dev_do_write(struct fuse_dev *fud,
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001825 struct fuse_copy_state *cs, size_t nbytes)
Miklos Szeredi334f4852005-09-09 13:10:27 -07001826{
1827 int err;
Miklos Szeredic36960462015-07-01 16:26:09 +02001828 struct fuse_conn *fc = fud->fc;
1829 struct fuse_pqueue *fpq = &fud->pq;
Miklos Szeredi334f4852005-09-09 13:10:27 -07001830 struct fuse_req *req;
1831 struct fuse_out_header oh;
Miklos Szeredi334f4852005-09-09 13:10:27 -07001832
Miklos Szeredi334f4852005-09-09 13:10:27 -07001833 if (nbytes < sizeof(struct fuse_out_header))
1834 return -EINVAL;
1835
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001836 err = fuse_copy_one(cs, &oh, sizeof(oh));
Miklos Szeredi334f4852005-09-09 13:10:27 -07001837 if (err)
1838 goto err_finish;
Tejun Heo85993962008-11-26 12:03:55 +01001839
Miklos Szeredi334f4852005-09-09 13:10:27 -07001840 err = -EINVAL;
Tejun Heo85993962008-11-26 12:03:55 +01001841 if (oh.len != nbytes)
1842 goto err_finish;
1843
1844 /*
1845 * Zero oh.unique indicates unsolicited notification message
1846 * and error contains notification code.
1847 */
1848 if (!oh.unique) {
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001849 err = fuse_notify(fc, oh.error, nbytes - sizeof(oh), cs);
Tejun Heo85993962008-11-26 12:03:55 +01001850 return err ? err : nbytes;
1851 }
1852
1853 err = -EINVAL;
1854 if (oh.error <= -1000 || oh.error > 0)
Miklos Szeredi334f4852005-09-09 13:10:27 -07001855 goto err_finish;
1856
Miklos Szeredi45a91cb2015-07-01 16:26:06 +02001857 spin_lock(&fpq->lock);
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08001858 err = -ENOENT;
Miklos Szeredie96edd92015-07-01 16:26:04 +02001859 if (!fpq->connected)
Miklos Szeredi45a91cb2015-07-01 16:26:06 +02001860 goto err_unlock_pq;
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08001861
Miklos Szeredi3a2b5b92015-07-01 16:26:04 +02001862 req = request_find(fpq, oh.unique);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001863 if (!req)
Miklos Szeredi45a91cb2015-07-01 16:26:06 +02001864 goto err_unlock_pq;
Miklos Szeredi334f4852005-09-09 13:10:27 -07001865
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001866 /* Is it an interrupt reply? */
1867 if (req->intr_unique == oh.unique) {
Miklos Szeredi45a91cb2015-07-01 16:26:06 +02001868 spin_unlock(&fpq->lock);
1869
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001870 err = -EINVAL;
1871 if (nbytes != sizeof(struct fuse_out_header))
Miklos Szeredi46c34a32015-07-01 16:26:07 +02001872 goto err_finish;
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001873
1874 if (oh.error == -ENOSYS)
1875 fc->no_interrupt = 1;
1876 else if (oh.error == -EAGAIN)
Miklos Szeredif88996a2015-07-01 16:26:01 +02001877 queue_interrupt(&fc->iq, req);
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001878
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001879 fuse_copy_finish(cs);
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001880 return nbytes;
1881 }
1882
Miklos Szeredi33e14b42015-07-01 16:26:01 +02001883 clear_bit(FR_SENT, &req->flags);
Miklos Szeredi3a2b5b92015-07-01 16:26:04 +02001884 list_move(&req->list, &fpq->io);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001885 req->out.h = oh;
Miklos Szeredi825d6d32015-07-01 16:25:58 +02001886 set_bit(FR_LOCKED, &req->flags);
Miklos Szeredi45a91cb2015-07-01 16:26:06 +02001887 spin_unlock(&fpq->lock);
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001888 cs->req = req;
Miklos Szeredice534fb2010-05-25 15:06:07 +02001889 if (!req->out.page_replace)
1890 cs->move_pages = 0;
Miklos Szeredi334f4852005-09-09 13:10:27 -07001891
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001892 err = copy_out_args(cs, &req->out, nbytes);
Daniel Rosenbergfac99a72016-04-22 00:00:48 -07001893 if (req->in.h.opcode == FUSE_CANONICAL_PATH) {
1894 req->out.h.error = kern_path((char *)req->out.args[0].value, 0,
1895 req->canonical_path);
1896 }
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001897 fuse_copy_finish(cs);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001898
Nikhilesh Reddy759832b2017-07-26 09:41:57 +05301899 fuse_setup_passthrough(fc, req);
1900
1901
Miklos Szeredi45a91cb2015-07-01 16:26:06 +02001902 spin_lock(&fpq->lock);
Miklos Szeredi825d6d32015-07-01 16:25:58 +02001903 clear_bit(FR_LOCKED, &req->flags);
Miklos Szeredie96edd92015-07-01 16:26:04 +02001904 if (!fpq->connected)
Miklos Szeredi0d8e84b2015-07-01 16:25:58 +02001905 err = -ENOENT;
1906 else if (err)
Miklos Szeredi334f4852005-09-09 13:10:27 -07001907 req->out.h.error = -EIO;
Miklos Szeredi77cd9d42015-07-01 16:26:06 +02001908 if (!test_bit(FR_PRIVATE, &req->flags))
1909 list_del_init(&req->list);
Miklos Szeredi45a91cb2015-07-01 16:26:06 +02001910 spin_unlock(&fpq->lock);
Miklos Szeredi46c34a32015-07-01 16:26:07 +02001911
Miklos Szeredi334f4852005-09-09 13:10:27 -07001912 request_end(fc, req);
1913
1914 return err ? err : nbytes;
1915
Miklos Szeredi45a91cb2015-07-01 16:26:06 +02001916 err_unlock_pq:
1917 spin_unlock(&fpq->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001918 err_finish:
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001919 fuse_copy_finish(cs);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001920 return err;
1921}
1922
Al Virofbdbacc2015-04-03 21:53:39 -04001923static ssize_t fuse_dev_write(struct kiocb *iocb, struct iov_iter *from)
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001924{
1925 struct fuse_copy_state cs;
Miklos Szeredicc080e92015-07-01 16:26:08 +02001926 struct fuse_dev *fud = fuse_get_dev(iocb->ki_filp);
1927
1928 if (!fud)
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001929 return -EPERM;
1930
Al Virofbdbacc2015-04-03 21:53:39 -04001931 if (!iter_is_iovec(from))
1932 return -EINVAL;
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001933
Miklos Szeredidc008092015-07-01 16:25:58 +02001934 fuse_copy_init(&cs, 0, from);
Al Virofbdbacc2015-04-03 21:53:39 -04001935
Miklos Szeredic36960462015-07-01 16:26:09 +02001936 return fuse_dev_do_write(fud, &cs, iov_iter_count(from));
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001937}
1938
1939static ssize_t fuse_dev_splice_write(struct pipe_inode_info *pipe,
1940 struct file *out, loff_t *ppos,
1941 size_t len, unsigned int flags)
1942{
1943 unsigned nbuf;
1944 unsigned idx;
1945 struct pipe_buffer *bufs;
1946 struct fuse_copy_state cs;
Miklos Szeredicc080e92015-07-01 16:26:08 +02001947 struct fuse_dev *fud;
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001948 size_t rem;
1949 ssize_t ret;
1950
Miklos Szeredicc080e92015-07-01 16:26:08 +02001951 fud = fuse_get_dev(out);
1952 if (!fud)
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001953 return -EPERM;
1954
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001955 bufs = kmalloc(pipe->buffers * sizeof(struct pipe_buffer), GFP_KERNEL);
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001956 if (!bufs)
1957 return -ENOMEM;
1958
1959 pipe_lock(pipe);
1960 nbuf = 0;
1961 rem = 0;
1962 for (idx = 0; idx < pipe->nrbufs && rem < len; idx++)
1963 rem += pipe->bufs[(pipe->curbuf + idx) & (pipe->buffers - 1)].len;
1964
1965 ret = -EINVAL;
1966 if (rem < len) {
1967 pipe_unlock(pipe);
1968 goto out;
1969 }
1970
1971 rem = len;
1972 while (rem) {
1973 struct pipe_buffer *ibuf;
1974 struct pipe_buffer *obuf;
1975
1976 BUG_ON(nbuf >= pipe->buffers);
1977 BUG_ON(!pipe->nrbufs);
1978 ibuf = &pipe->bufs[pipe->curbuf];
1979 obuf = &bufs[nbuf];
1980
1981 if (rem >= ibuf->len) {
1982 *obuf = *ibuf;
1983 ibuf->ops = NULL;
1984 pipe->curbuf = (pipe->curbuf + 1) & (pipe->buffers - 1);
1985 pipe->nrbufs--;
1986 } else {
Miklos Szeredi7bf2d1d2016-09-27 10:45:12 +02001987 pipe_buf_get(pipe, ibuf);
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001988 *obuf = *ibuf;
1989 obuf->flags &= ~PIPE_BUF_FLAG_GIFT;
1990 obuf->len = rem;
1991 ibuf->offset += obuf->len;
1992 ibuf->len -= obuf->len;
1993 }
1994 nbuf++;
1995 rem -= obuf->len;
1996 }
1997 pipe_unlock(pipe);
1998
Miklos Szeredidc008092015-07-01 16:25:58 +02001999 fuse_copy_init(&cs, 0, NULL);
Miklos Szeredidd3bb142010-05-25 15:06:06 +02002000 cs.pipebufs = bufs;
Al Viro6c09e942015-04-03 22:06:08 -04002001 cs.nr_segs = nbuf;
Miklos Szeredidd3bb142010-05-25 15:06:06 +02002002 cs.pipe = pipe;
2003
Miklos Szeredice534fb2010-05-25 15:06:07 +02002004 if (flags & SPLICE_F_MOVE)
2005 cs.move_pages = 1;
2006
Miklos Szeredic36960462015-07-01 16:26:09 +02002007 ret = fuse_dev_do_write(fud, &cs, len);
Miklos Szeredidd3bb142010-05-25 15:06:06 +02002008
Miklos Szeredia7796382016-09-27 10:45:12 +02002009 for (idx = 0; idx < nbuf; idx++)
2010 pipe_buf_release(pipe, &bufs[idx]);
2011
Miklos Szeredidd3bb142010-05-25 15:06:06 +02002012out:
2013 kfree(bufs);
2014 return ret;
2015}
2016
Miklos Szeredi334f4852005-09-09 13:10:27 -07002017static unsigned fuse_dev_poll(struct file *file, poll_table *wait)
2018{
Miklos Szeredi334f4852005-09-09 13:10:27 -07002019 unsigned mask = POLLOUT | POLLWRNORM;
Miklos Szeredif88996a2015-07-01 16:26:01 +02002020 struct fuse_iqueue *fiq;
Miklos Szeredicc080e92015-07-01 16:26:08 +02002021 struct fuse_dev *fud = fuse_get_dev(file);
2022
2023 if (!fud)
Miklos Szeredi7025d9a2006-04-10 22:54:50 -07002024 return POLLERR;
Miklos Szeredi334f4852005-09-09 13:10:27 -07002025
Miklos Szeredicc080e92015-07-01 16:26:08 +02002026 fiq = &fud->fc->iq;
Miklos Szeredif88996a2015-07-01 16:26:01 +02002027 poll_wait(file, &fiq->waitq, wait);
Miklos Szeredi334f4852005-09-09 13:10:27 -07002028
Miklos Szeredi4ce60812015-07-01 16:26:02 +02002029 spin_lock(&fiq->waitq.lock);
Miklos Szeredie16714d2015-07-01 16:26:01 +02002030 if (!fiq->connected)
Miklos Szeredi7025d9a2006-04-10 22:54:50 -07002031 mask = POLLERR;
Miklos Szeredif88996a2015-07-01 16:26:01 +02002032 else if (request_pending(fiq))
Miklos Szeredi7025d9a2006-04-10 22:54:50 -07002033 mask |= POLLIN | POLLRDNORM;
Miklos Szeredi4ce60812015-07-01 16:26:02 +02002034 spin_unlock(&fiq->waitq.lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -07002035
2036 return mask;
2037}
2038
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002039/*
2040 * Abort all requests on the given list (pending or processing)
2041 *
Miklos Szeredid7133112006-04-10 22:54:55 -07002042 * This function releases and reacquires fc->lock
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002043 */
Miklos Szeredi334f4852005-09-09 13:10:27 -07002044static void end_requests(struct fuse_conn *fc, struct list_head *head)
2045{
2046 while (!list_empty(head)) {
2047 struct fuse_req *req;
2048 req = list_entry(head->next, struct fuse_req, list);
Miklos Szeredi334f4852005-09-09 13:10:27 -07002049 req->out.h.error = -ECONNABORTED;
Miklos Szeredi33e14b42015-07-01 16:26:01 +02002050 clear_bit(FR_SENT, &req->flags);
Miklos Szeredif377cb72015-07-01 16:26:04 +02002051 list_del_init(&req->list);
Miklos Szeredi334f4852005-09-09 13:10:27 -07002052 request_end(fc, req);
Miklos Szeredi334f4852005-09-09 13:10:27 -07002053 }
2054}
2055
Bryan Green357ccf22011-03-01 16:43:52 -08002056static void end_polls(struct fuse_conn *fc)
2057{
2058 struct rb_node *p;
2059
2060 p = rb_first(&fc->polled_files);
2061
2062 while (p) {
2063 struct fuse_file *ff;
2064 ff = rb_entry(p, struct fuse_file, polled_node);
2065 wake_up_interruptible_all(&ff->poll_wait);
2066
2067 p = rb_next(p);
2068 }
2069}
2070
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002071/*
2072 * Abort all requests.
2073 *
Miklos Szeredib716d422015-07-01 16:25:59 +02002074 * Emergency exit in case of a malicious or accidental deadlock, or just a hung
2075 * filesystem.
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002076 *
Miklos Szeredib716d422015-07-01 16:25:59 +02002077 * The same effect is usually achievable through killing the filesystem daemon
2078 * and all users of the filesystem. The exception is the combination of an
2079 * asynchronous request and the tricky deadlock (see
2080 * Documentation/filesystems/fuse.txt).
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002081 *
Miklos Szeredib716d422015-07-01 16:25:59 +02002082 * Aborting requests under I/O goes as follows: 1: Separate out unlocked
2083 * requests, they should be finished off immediately. Locked requests will be
2084 * finished after unlock; see unlock_request(). 2: Finish off the unlocked
2085 * requests. It is possible that some request will finish before we can. This
2086 * is OK, the request will in that case be removed from the list before we touch
2087 * it.
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002088 */
2089void fuse_abort_conn(struct fuse_conn *fc)
2090{
Miklos Szeredif88996a2015-07-01 16:26:01 +02002091 struct fuse_iqueue *fiq = &fc->iq;
2092
Miklos Szeredid7133112006-04-10 22:54:55 -07002093 spin_lock(&fc->lock);
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002094 if (fc->connected) {
Miklos Szeredic36960462015-07-01 16:26:09 +02002095 struct fuse_dev *fud;
Miklos Szeredib716d422015-07-01 16:25:59 +02002096 struct fuse_req *req, *next;
Miklos Szeredi41f98272015-07-01 16:25:59 +02002097 LIST_HEAD(to_end1);
2098 LIST_HEAD(to_end2);
Miklos Szeredib716d422015-07-01 16:25:59 +02002099
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002100 fc->connected = 0;
Miklos Szeredi51eb01e2006-06-25 05:48:50 -07002101 fc->blocked = 0;
Miklos Szeredi9759bd512015-01-06 10:45:35 +01002102 fuse_set_initialized(fc);
Miklos Szeredic36960462015-07-01 16:26:09 +02002103 list_for_each_entry(fud, &fc->devices, entry) {
2104 struct fuse_pqueue *fpq = &fud->pq;
2105
2106 spin_lock(&fpq->lock);
2107 fpq->connected = 0;
2108 list_for_each_entry_safe(req, next, &fpq->io, list) {
2109 req->out.h.error = -ECONNABORTED;
2110 spin_lock(&req->waitq.lock);
2111 set_bit(FR_ABORTED, &req->flags);
2112 if (!test_bit(FR_LOCKED, &req->flags)) {
2113 set_bit(FR_PRIVATE, &req->flags);
2114 list_move(&req->list, &to_end1);
2115 }
2116 spin_unlock(&req->waitq.lock);
Miklos Szeredi77cd9d42015-07-01 16:26:06 +02002117 }
Miklos Szeredic36960462015-07-01 16:26:09 +02002118 list_splice_init(&fpq->processing, &to_end2);
2119 spin_unlock(&fpq->lock);
Miklos Szeredib716d422015-07-01 16:25:59 +02002120 }
Miklos Szeredi41f98272015-07-01 16:25:59 +02002121 fc->max_background = UINT_MAX;
2122 flush_bg_queue(fc);
Miklos Szeredi8c911892015-07-01 16:26:02 +02002123
Miklos Szeredi4ce60812015-07-01 16:26:02 +02002124 spin_lock(&fiq->waitq.lock);
Miklos Szeredi8c911892015-07-01 16:26:02 +02002125 fiq->connected = 0;
Miklos Szeredif88996a2015-07-01 16:26:01 +02002126 list_splice_init(&fiq->pending, &to_end2);
Tahsin Erdogan0181b362017-01-12 12:04:04 -08002127 list_for_each_entry(req, &to_end2, list)
2128 clear_bit(FR_PENDING, &req->flags);
Miklos Szeredi8c911892015-07-01 16:26:02 +02002129 while (forget_pending(fiq))
2130 kfree(dequeue_forget(fiq, 1, NULL));
Miklos Szeredi4ce60812015-07-01 16:26:02 +02002131 wake_up_all_locked(&fiq->waitq);
2132 spin_unlock(&fiq->waitq.lock);
Miklos Szeredi8c911892015-07-01 16:26:02 +02002133 kill_fasync(&fiq->fasync, SIGIO, POLL_IN);
Miklos Szerediee314a82015-07-01 16:26:08 +02002134 end_polls(fc);
2135 wake_up_all(&fc->blocked_waitq);
2136 spin_unlock(&fc->lock);
Miklos Szeredi8c911892015-07-01 16:26:02 +02002137
Miklos Szeredi41f98272015-07-01 16:25:59 +02002138 while (!list_empty(&to_end1)) {
2139 req = list_first_entry(&to_end1, struct fuse_req, list);
Miklos Szeredib716d422015-07-01 16:25:59 +02002140 __fuse_get_request(req);
Miklos Szeredif377cb72015-07-01 16:26:04 +02002141 list_del_init(&req->list);
Miklos Szeredib716d422015-07-01 16:25:59 +02002142 request_end(fc, req);
Miklos Szeredib716d422015-07-01 16:25:59 +02002143 }
Miklos Szeredi41f98272015-07-01 16:25:59 +02002144 end_requests(fc, &to_end2);
Miklos Szerediee314a82015-07-01 16:26:08 +02002145 } else {
2146 spin_unlock(&fc->lock);
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002147 }
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002148}
Tejun Heo08cbf542009-04-14 10:54:53 +09002149EXPORT_SYMBOL_GPL(fuse_abort_conn);
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002150
Tejun Heo08cbf542009-04-14 10:54:53 +09002151int fuse_dev_release(struct inode *inode, struct file *file)
Miklos Szeredi334f4852005-09-09 13:10:27 -07002152{
Miklos Szeredicc080e92015-07-01 16:26:08 +02002153 struct fuse_dev *fud = fuse_get_dev(file);
2154
2155 if (fud) {
2156 struct fuse_conn *fc = fud->fc;
Miklos Szeredic36960462015-07-01 16:26:09 +02002157 struct fuse_pqueue *fpq = &fud->pq;
Miklos Szeredicc080e92015-07-01 16:26:08 +02002158
Miklos Szeredic36960462015-07-01 16:26:09 +02002159 WARN_ON(!list_empty(&fpq->io));
2160 end_requests(fc, &fpq->processing);
2161 /* Are we the last open device? */
2162 if (atomic_dec_and_test(&fc->dev_count)) {
2163 WARN_ON(fc->iq.fasync != NULL);
2164 fuse_abort_conn(fc);
2165 }
Miklos Szeredicc080e92015-07-01 16:26:08 +02002166 fuse_dev_free(fud);
Jeff Dike385a17b2006-04-10 22:54:52 -07002167 }
Miklos Szeredi334f4852005-09-09 13:10:27 -07002168 return 0;
2169}
Tejun Heo08cbf542009-04-14 10:54:53 +09002170EXPORT_SYMBOL_GPL(fuse_dev_release);
Miklos Szeredi334f4852005-09-09 13:10:27 -07002171
Jeff Dike385a17b2006-04-10 22:54:52 -07002172static int fuse_dev_fasync(int fd, struct file *file, int on)
2173{
Miklos Szeredicc080e92015-07-01 16:26:08 +02002174 struct fuse_dev *fud = fuse_get_dev(file);
2175
2176 if (!fud)
Miklos Szeredia87046d2006-04-10 22:54:56 -07002177 return -EPERM;
Jeff Dike385a17b2006-04-10 22:54:52 -07002178
2179 /* No locking - fasync_helper does its own locking */
Miklos Szeredicc080e92015-07-01 16:26:08 +02002180 return fasync_helper(fd, file, on, &fud->fc->iq.fasync);
Jeff Dike385a17b2006-04-10 22:54:52 -07002181}
2182
Miklos Szeredi00c570f2015-07-01 16:26:08 +02002183static int fuse_device_clone(struct fuse_conn *fc, struct file *new)
2184{
Miklos Szeredicc080e92015-07-01 16:26:08 +02002185 struct fuse_dev *fud;
2186
Miklos Szeredi00c570f2015-07-01 16:26:08 +02002187 if (new->private_data)
2188 return -EINVAL;
2189
Miklos Szeredicc080e92015-07-01 16:26:08 +02002190 fud = fuse_dev_alloc(fc);
2191 if (!fud)
2192 return -ENOMEM;
2193
2194 new->private_data = fud;
Miklos Szeredic36960462015-07-01 16:26:09 +02002195 atomic_inc(&fc->dev_count);
Miklos Szeredi00c570f2015-07-01 16:26:08 +02002196
2197 return 0;
2198}
2199
2200static long fuse_dev_ioctl(struct file *file, unsigned int cmd,
2201 unsigned long arg)
2202{
2203 int err = -ENOTTY;
2204
2205 if (cmd == FUSE_DEV_IOC_CLONE) {
2206 int oldfd;
2207
2208 err = -EFAULT;
2209 if (!get_user(oldfd, (__u32 __user *) arg)) {
2210 struct file *old = fget(oldfd);
2211
2212 err = -EINVAL;
2213 if (old) {
Jann Horn8ed1f0e2015-08-16 20:27:01 +02002214 struct fuse_dev *fud = NULL;
2215
2216 /*
2217 * Check against file->f_op because CUSE
2218 * uses the same ioctl handler.
2219 */
2220 if (old->f_op == file->f_op &&
2221 old->f_cred->user_ns == file->f_cred->user_ns)
2222 fud = fuse_get_dev(old);
Miklos Szeredi00c570f2015-07-01 16:26:08 +02002223
Miklos Szeredicc080e92015-07-01 16:26:08 +02002224 if (fud) {
Miklos Szeredi00c570f2015-07-01 16:26:08 +02002225 mutex_lock(&fuse_mutex);
Miklos Szeredicc080e92015-07-01 16:26:08 +02002226 err = fuse_device_clone(fud->fc, file);
Miklos Szeredi00c570f2015-07-01 16:26:08 +02002227 mutex_unlock(&fuse_mutex);
2228 }
2229 fput(old);
2230 }
2231 }
2232 }
2233 return err;
2234}
2235
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -08002236const struct file_operations fuse_dev_operations = {
Miklos Szeredi334f4852005-09-09 13:10:27 -07002237 .owner = THIS_MODULE,
Tom Van Braeckel94e4fe22015-01-12 05:22:16 +01002238 .open = fuse_dev_open,
Miklos Szeredi334f4852005-09-09 13:10:27 -07002239 .llseek = no_llseek,
Al Virofbdbacc2015-04-03 21:53:39 -04002240 .read_iter = fuse_dev_read,
Miklos Szeredic3021622010-05-25 15:06:07 +02002241 .splice_read = fuse_dev_splice_read,
Al Virofbdbacc2015-04-03 21:53:39 -04002242 .write_iter = fuse_dev_write,
Miklos Szeredidd3bb142010-05-25 15:06:06 +02002243 .splice_write = fuse_dev_splice_write,
Miklos Szeredi334f4852005-09-09 13:10:27 -07002244 .poll = fuse_dev_poll,
2245 .release = fuse_dev_release,
Jeff Dike385a17b2006-04-10 22:54:52 -07002246 .fasync = fuse_dev_fasync,
Miklos Szeredi00c570f2015-07-01 16:26:08 +02002247 .unlocked_ioctl = fuse_dev_ioctl,
2248 .compat_ioctl = fuse_dev_ioctl,
Miklos Szeredi334f4852005-09-09 13:10:27 -07002249};
Tejun Heo08cbf542009-04-14 10:54:53 +09002250EXPORT_SYMBOL_GPL(fuse_dev_operations);
Miklos Szeredi334f4852005-09-09 13:10:27 -07002251
2252static struct miscdevice fuse_miscdevice = {
2253 .minor = FUSE_MINOR,
2254 .name = "fuse",
2255 .fops = &fuse_dev_operations,
2256};
2257
2258int __init fuse_dev_init(void)
2259{
2260 int err = -ENOMEM;
2261 fuse_req_cachep = kmem_cache_create("fuse_request",
2262 sizeof(struct fuse_req),
Paul Mundt20c2df82007-07-20 10:11:58 +09002263 0, 0, NULL);
Miklos Szeredi334f4852005-09-09 13:10:27 -07002264 if (!fuse_req_cachep)
2265 goto out;
2266
2267 err = misc_register(&fuse_miscdevice);
2268 if (err)
2269 goto out_cache_clean;
2270
2271 return 0;
2272
2273 out_cache_clean:
2274 kmem_cache_destroy(fuse_req_cachep);
2275 out:
2276 return err;
2277}
2278
2279void fuse_dev_cleanup(void)
2280{
2281 misc_deregister(&fuse_miscdevice);
2282 kmem_cache_destroy(fuse_req_cachep);
2283}