blob: 573a2ee7ca1e44d183de8cc79ca2f59f5bb0dc6a [file] [log] [blame]
Miklos Szeredi334f4852005-09-09 13:10:27 -07001/*
2 FUSE: Filesystem in Userspace
Miklos Szeredi1729a162008-11-26 12:03:54 +01003 Copyright (C) 2001-2008 Miklos Szeredi <miklos@szeredi.hu>
Miklos Szeredi334f4852005-09-09 13:10:27 -07004
5 This program can be distributed under the terms of the GNU GPL.
6 See the file COPYING.
7*/
8
9#include "fuse_i.h"
10
11#include <linux/init.h>
12#include <linux/module.h>
13#include <linux/poll.h>
14#include <linux/uio.h>
15#include <linux/miscdevice.h>
Daniel Rosenbergfac99a72016-04-22 00:00:48 -070016#include <linux/namei.h>
Miklos Szeredi334f4852005-09-09 13:10:27 -070017#include <linux/pagemap.h>
18#include <linux/file.h>
19#include <linux/slab.h>
Miklos Szeredidd3bb142010-05-25 15:06:06 +020020#include <linux/pipe_fs_i.h>
Miklos Szeredice534fb2010-05-25 15:06:07 +020021#include <linux/swap.h>
22#include <linux/splice.h>
Todd Poynor1672c662011-08-24 15:01:30 -070023#include <linux/freezer.h>
Miklos Szeredi334f4852005-09-09 13:10:27 -070024
25MODULE_ALIAS_MISCDEV(FUSE_MINOR);
Kay Sievers578454f2010-05-20 18:07:20 +020026MODULE_ALIAS("devname:fuse");
Miklos Szeredi334f4852005-09-09 13:10:27 -070027
Christoph Lametere18b8902006-12-06 20:33:20 -080028static struct kmem_cache *fuse_req_cachep;
Miklos Szeredi334f4852005-09-09 13:10:27 -070029
Miklos Szeredicc080e92015-07-01 16:26:08 +020030static struct fuse_dev *fuse_get_dev(struct file *file)
Miklos Szeredi334f4852005-09-09 13:10:27 -070031{
Miklos Szeredi0720b312006-04-10 22:54:55 -070032 /*
33 * Lockless access is OK, because file->private data is set
34 * once during mount and is valid until the file is released.
35 */
Miklos Szeredicc080e92015-07-01 16:26:08 +020036 return ACCESS_ONCE(file->private_data);
Miklos Szeredi334f4852005-09-09 13:10:27 -070037}
38
Maxim Patlasov4250c062012-10-26 19:48:07 +040039static void fuse_request_init(struct fuse_req *req, struct page **pages,
Maxim Patlasovb2430d72012-10-26 19:49:24 +040040 struct fuse_page_desc *page_descs,
Maxim Patlasov4250c062012-10-26 19:48:07 +040041 unsigned npages)
Miklos Szeredi334f4852005-09-09 13:10:27 -070042{
43 memset(req, 0, sizeof(*req));
Maxim Patlasov4250c062012-10-26 19:48:07 +040044 memset(pages, 0, sizeof(*pages) * npages);
Maxim Patlasovb2430d72012-10-26 19:49:24 +040045 memset(page_descs, 0, sizeof(*page_descs) * npages);
Miklos Szeredi334f4852005-09-09 13:10:27 -070046 INIT_LIST_HEAD(&req->list);
Miklos Szeredia4d27e72006-06-25 05:48:54 -070047 INIT_LIST_HEAD(&req->intr_entry);
Miklos Szeredi334f4852005-09-09 13:10:27 -070048 init_waitqueue_head(&req->waitq);
49 atomic_set(&req->count, 1);
Maxim Patlasov4250c062012-10-26 19:48:07 +040050 req->pages = pages;
Maxim Patlasovb2430d72012-10-26 19:49:24 +040051 req->page_descs = page_descs;
Maxim Patlasov4250c062012-10-26 19:48:07 +040052 req->max_pages = npages;
Miklos Szeredi33e14b42015-07-01 16:26:01 +020053 __set_bit(FR_PENDING, &req->flags);
Miklos Szeredi334f4852005-09-09 13:10:27 -070054}
55
Maxim Patlasov4250c062012-10-26 19:48:07 +040056static struct fuse_req *__fuse_request_alloc(unsigned npages, gfp_t flags)
Miklos Szeredi334f4852005-09-09 13:10:27 -070057{
Maxim Patlasov4250c062012-10-26 19:48:07 +040058 struct fuse_req *req = kmem_cache_alloc(fuse_req_cachep, flags);
59 if (req) {
60 struct page **pages;
Maxim Patlasovb2430d72012-10-26 19:49:24 +040061 struct fuse_page_desc *page_descs;
Maxim Patlasov4250c062012-10-26 19:48:07 +040062
Maxim Patlasovb2430d72012-10-26 19:49:24 +040063 if (npages <= FUSE_REQ_INLINE_PAGES) {
Maxim Patlasov4250c062012-10-26 19:48:07 +040064 pages = req->inline_pages;
Maxim Patlasovb2430d72012-10-26 19:49:24 +040065 page_descs = req->inline_page_descs;
66 } else {
Maxim Patlasov4250c062012-10-26 19:48:07 +040067 pages = kmalloc(sizeof(struct page *) * npages, flags);
Maxim Patlasovb2430d72012-10-26 19:49:24 +040068 page_descs = kmalloc(sizeof(struct fuse_page_desc) *
69 npages, flags);
70 }
Maxim Patlasov4250c062012-10-26 19:48:07 +040071
Maxim Patlasovb2430d72012-10-26 19:49:24 +040072 if (!pages || !page_descs) {
73 kfree(pages);
74 kfree(page_descs);
Maxim Patlasov4250c062012-10-26 19:48:07 +040075 kmem_cache_free(fuse_req_cachep, req);
76 return NULL;
77 }
78
Maxim Patlasovb2430d72012-10-26 19:49:24 +040079 fuse_request_init(req, pages, page_descs, npages);
Maxim Patlasov4250c062012-10-26 19:48:07 +040080 }
Miklos Szeredi334f4852005-09-09 13:10:27 -070081 return req;
82}
Maxim Patlasov4250c062012-10-26 19:48:07 +040083
84struct fuse_req *fuse_request_alloc(unsigned npages)
85{
86 return __fuse_request_alloc(npages, GFP_KERNEL);
87}
Tejun Heo08cbf542009-04-14 10:54:53 +090088EXPORT_SYMBOL_GPL(fuse_request_alloc);
Miklos Szeredi334f4852005-09-09 13:10:27 -070089
Maxim Patlasov4250c062012-10-26 19:48:07 +040090struct fuse_req *fuse_request_alloc_nofs(unsigned npages)
Miklos Szeredi3be5a522008-04-30 00:54:41 -070091{
Maxim Patlasov4250c062012-10-26 19:48:07 +040092 return __fuse_request_alloc(npages, GFP_NOFS);
Miklos Szeredi3be5a522008-04-30 00:54:41 -070093}
94
Miklos Szeredi334f4852005-09-09 13:10:27 -070095void fuse_request_free(struct fuse_req *req)
96{
Maxim Patlasovb2430d72012-10-26 19:49:24 +040097 if (req->pages != req->inline_pages) {
Maxim Patlasov4250c062012-10-26 19:48:07 +040098 kfree(req->pages);
Maxim Patlasovb2430d72012-10-26 19:49:24 +040099 kfree(req->page_descs);
100 }
Miklos Szeredi334f4852005-09-09 13:10:27 -0700101 kmem_cache_free(fuse_req_cachep, req);
102}
103
Maxim Patlasov36cf66e2012-12-14 19:20:51 +0400104void __fuse_get_request(struct fuse_req *req)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700105{
106 atomic_inc(&req->count);
107}
108
109/* Must be called with > 1 refcount */
110static void __fuse_put_request(struct fuse_req *req)
111{
112 BUG_ON(atomic_read(&req->count) < 2);
113 atomic_dec(&req->count);
114}
115
Miklos Szeredi33649c92006-06-25 05:48:52 -0700116static void fuse_req_init_context(struct fuse_req *req)
117{
Eric W. Biederman499dcf22012-02-07 16:26:03 -0800118 req->in.h.uid = from_kuid_munged(&init_user_ns, current_fsuid());
119 req->in.h.gid = from_kgid_munged(&init_user_ns, current_fsgid());
Miklos Szeredi33649c92006-06-25 05:48:52 -0700120 req->in.h.pid = current->pid;
121}
122
Miklos Szeredi9759bd512015-01-06 10:45:35 +0100123void fuse_set_initialized(struct fuse_conn *fc)
124{
125 /* Make sure stores before this are seen on another CPU */
126 smp_wmb();
127 fc->initialized = 1;
128}
129
Maxim Patlasov0aada882013-03-21 18:02:28 +0400130static bool fuse_block_alloc(struct fuse_conn *fc, bool for_background)
131{
132 return !fc->initialized || (for_background && fc->blocked);
133}
134
Miklos Szeredi6465d762018-07-26 16:13:11 +0200135static void fuse_drop_waiting(struct fuse_conn *fc)
136{
137 if (fc->connected) {
138 atomic_dec(&fc->num_waiting);
139 } else if (atomic_dec_and_test(&fc->num_waiting)) {
140 /* wake up aborters */
141 wake_up_all(&fc->blocked_waitq);
142 }
143}
144
Maxim Patlasov8b41e672013-03-21 18:02:04 +0400145static struct fuse_req *__fuse_get_req(struct fuse_conn *fc, unsigned npages,
146 bool for_background)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700147{
Miklos Szeredi08a53cd2006-04-10 22:54:59 -0700148 struct fuse_req *req;
Miklos Szeredi08a53cd2006-04-10 22:54:59 -0700149 int err;
Miklos Szeredi9bc5ddd2006-04-11 21:16:09 +0200150 atomic_inc(&fc->num_waiting);
Maxim Patlasov0aada882013-03-21 18:02:28 +0400151
152 if (fuse_block_alloc(fc, for_background)) {
Maxim Patlasov0aada882013-03-21 18:02:28 +0400153 err = -EINTR;
Al Viro7d3a07f2016-07-19 03:08:27 -0400154 if (wait_event_killable_exclusive(fc->blocked_waitq,
155 !fuse_block_alloc(fc, for_background)))
Maxim Patlasov0aada882013-03-21 18:02:28 +0400156 goto out;
157 }
Miklos Szeredi9759bd512015-01-06 10:45:35 +0100158 /* Matches smp_wmb() in fuse_set_initialized() */
159 smp_rmb();
Miklos Szeredi08a53cd2006-04-10 22:54:59 -0700160
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700161 err = -ENOTCONN;
162 if (!fc->connected)
163 goto out;
164
Miklos Szeredide155222015-07-01 16:25:57 +0200165 err = -ECONNREFUSED;
166 if (fc->conn_error)
167 goto out;
168
Maxim Patlasovb111c8c2012-10-26 19:48:30 +0400169 req = fuse_request_alloc(npages);
Miklos Szeredi9bc5ddd2006-04-11 21:16:09 +0200170 err = -ENOMEM;
Maxim Patlasov722d2be2013-03-21 18:02:36 +0400171 if (!req) {
172 if (for_background)
173 wake_up(&fc->blocked_waitq);
Miklos Szeredi9bc5ddd2006-04-11 21:16:09 +0200174 goto out;
Maxim Patlasov722d2be2013-03-21 18:02:36 +0400175 }
Miklos Szeredi334f4852005-09-09 13:10:27 -0700176
Miklos Szeredi33649c92006-06-25 05:48:52 -0700177 fuse_req_init_context(req);
Miklos Szeredi825d6d32015-07-01 16:25:58 +0200178 __set_bit(FR_WAITING, &req->flags);
179 if (for_background)
180 __set_bit(FR_BACKGROUND, &req->flags);
181
Miklos Szeredi334f4852005-09-09 13:10:27 -0700182 return req;
Miklos Szeredi9bc5ddd2006-04-11 21:16:09 +0200183
184 out:
Miklos Szeredi6465d762018-07-26 16:13:11 +0200185 fuse_drop_waiting(fc);
Miklos Szeredi9bc5ddd2006-04-11 21:16:09 +0200186 return ERR_PTR(err);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700187}
Maxim Patlasov8b41e672013-03-21 18:02:04 +0400188
189struct fuse_req *fuse_get_req(struct fuse_conn *fc, unsigned npages)
190{
191 return __fuse_get_req(fc, npages, false);
192}
Tejun Heo08cbf542009-04-14 10:54:53 +0900193EXPORT_SYMBOL_GPL(fuse_get_req);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700194
Maxim Patlasov8b41e672013-03-21 18:02:04 +0400195struct fuse_req *fuse_get_req_for_background(struct fuse_conn *fc,
196 unsigned npages)
197{
198 return __fuse_get_req(fc, npages, true);
199}
200EXPORT_SYMBOL_GPL(fuse_get_req_for_background);
201
Miklos Szeredi33649c92006-06-25 05:48:52 -0700202/*
203 * Return request in fuse_file->reserved_req. However that may
204 * currently be in use. If that is the case, wait for it to become
205 * available.
206 */
207static struct fuse_req *get_reserved_req(struct fuse_conn *fc,
208 struct file *file)
209{
210 struct fuse_req *req = NULL;
211 struct fuse_file *ff = file->private_data;
212
213 do {
Miklos Szeredide5e3de2007-10-16 23:31:00 -0700214 wait_event(fc->reserved_req_waitq, ff->reserved_req);
Miklos Szeredi33649c92006-06-25 05:48:52 -0700215 spin_lock(&fc->lock);
216 if (ff->reserved_req) {
217 req = ff->reserved_req;
218 ff->reserved_req = NULL;
Al Virocb0942b2012-08-27 14:48:26 -0400219 req->stolen_file = get_file(file);
Miklos Szeredi33649c92006-06-25 05:48:52 -0700220 }
221 spin_unlock(&fc->lock);
222 } while (!req);
223
224 return req;
225}
226
227/*
228 * Put stolen request back into fuse_file->reserved_req
229 */
230static void put_reserved_req(struct fuse_conn *fc, struct fuse_req *req)
231{
232 struct file *file = req->stolen_file;
233 struct fuse_file *ff = file->private_data;
234
235 spin_lock(&fc->lock);
Maxim Patlasovb2430d72012-10-26 19:49:24 +0400236 fuse_request_init(req, req->pages, req->page_descs, req->max_pages);
Miklos Szeredi33649c92006-06-25 05:48:52 -0700237 BUG_ON(ff->reserved_req);
238 ff->reserved_req = req;
Miklos Szeredide5e3de2007-10-16 23:31:00 -0700239 wake_up_all(&fc->reserved_req_waitq);
Miklos Szeredi33649c92006-06-25 05:48:52 -0700240 spin_unlock(&fc->lock);
241 fput(file);
242}
243
244/*
245 * Gets a requests for a file operation, always succeeds
246 *
247 * This is used for sending the FLUSH request, which must get to
248 * userspace, due to POSIX locks which may need to be unlocked.
249 *
250 * If allocation fails due to OOM, use the reserved request in
251 * fuse_file.
252 *
253 * This is very unlikely to deadlock accidentally, since the
254 * filesystem should not have it's own file open. If deadlock is
255 * intentional, it can still be broken by "aborting" the filesystem.
256 */
Maxim Patlasovb111c8c2012-10-26 19:48:30 +0400257struct fuse_req *fuse_get_req_nofail_nopages(struct fuse_conn *fc,
258 struct file *file)
Miklos Szeredi33649c92006-06-25 05:48:52 -0700259{
260 struct fuse_req *req;
261
262 atomic_inc(&fc->num_waiting);
Maxim Patlasov0aada882013-03-21 18:02:28 +0400263 wait_event(fc->blocked_waitq, fc->initialized);
Miklos Szeredi9759bd512015-01-06 10:45:35 +0100264 /* Matches smp_wmb() in fuse_set_initialized() */
265 smp_rmb();
Maxim Patlasovb111c8c2012-10-26 19:48:30 +0400266 req = fuse_request_alloc(0);
Miklos Szeredi33649c92006-06-25 05:48:52 -0700267 if (!req)
268 req = get_reserved_req(fc, file);
269
270 fuse_req_init_context(req);
Miklos Szeredi825d6d32015-07-01 16:25:58 +0200271 __set_bit(FR_WAITING, &req->flags);
272 __clear_bit(FR_BACKGROUND, &req->flags);
Miklos Szeredi33649c92006-06-25 05:48:52 -0700273 return req;
274}
275
Miklos Szeredi334f4852005-09-09 13:10:27 -0700276void fuse_put_request(struct fuse_conn *fc, struct fuse_req *req)
277{
Miklos Szeredi7128ec22006-02-04 23:27:40 -0800278 if (atomic_dec_and_test(&req->count)) {
Miklos Szeredi825d6d32015-07-01 16:25:58 +0200279 if (test_bit(FR_BACKGROUND, &req->flags)) {
Maxim Patlasov722d2be2013-03-21 18:02:36 +0400280 /*
281 * We get here in the unlikely case that a background
282 * request was allocated but not sent
283 */
284 spin_lock(&fc->lock);
285 if (!fc->blocked)
286 wake_up(&fc->blocked_waitq);
287 spin_unlock(&fc->lock);
288 }
289
Miklos Szeredi825d6d32015-07-01 16:25:58 +0200290 if (test_bit(FR_WAITING, &req->flags)) {
291 __clear_bit(FR_WAITING, &req->flags);
Miklos Szeredi6465d762018-07-26 16:13:11 +0200292 fuse_drop_waiting(fc);
Miklos Szeredi73e0e732015-07-01 16:25:56 +0200293 }
Miklos Szeredi33649c92006-06-25 05:48:52 -0700294
295 if (req->stolen_file)
296 put_reserved_req(fc, req);
297 else
298 fuse_request_free(req);
Miklos Szeredi7128ec22006-02-04 23:27:40 -0800299 }
300}
Tejun Heo08cbf542009-04-14 10:54:53 +0900301EXPORT_SYMBOL_GPL(fuse_put_request);
Miklos Szeredi7128ec22006-02-04 23:27:40 -0800302
Miklos Szeredid12def12008-02-06 01:38:39 -0800303static unsigned len_args(unsigned numargs, struct fuse_arg *args)
304{
305 unsigned nbytes = 0;
306 unsigned i;
307
308 for (i = 0; i < numargs; i++)
309 nbytes += args[i].size;
310
311 return nbytes;
312}
313
Miklos Szeredif88996a2015-07-01 16:26:01 +0200314static u64 fuse_get_unique(struct fuse_iqueue *fiq)
Miklos Szeredid12def12008-02-06 01:38:39 -0800315{
Miklos Szeredif88996a2015-07-01 16:26:01 +0200316 return ++fiq->reqctr;
Miklos Szeredid12def12008-02-06 01:38:39 -0800317}
318
Miklos Szeredif88996a2015-07-01 16:26:01 +0200319static void queue_request(struct fuse_iqueue *fiq, struct fuse_req *req)
Miklos Szeredid12def12008-02-06 01:38:39 -0800320{
Miklos Szeredid12def12008-02-06 01:38:39 -0800321 req->in.h.len = sizeof(struct fuse_in_header) +
322 len_args(req->in.numargs, (struct fuse_arg *) req->in.args);
Miklos Szeredif88996a2015-07-01 16:26:01 +0200323 list_add_tail(&req->list, &fiq->pending);
Miklos Szeredi4ce60812015-07-01 16:26:02 +0200324 wake_up_locked(&fiq->waitq);
Miklos Szeredif88996a2015-07-01 16:26:01 +0200325 kill_fasync(&fiq->fasync, SIGIO, POLL_IN);
Miklos Szeredid12def12008-02-06 01:38:39 -0800326}
327
Miklos Szeredi07e77dc2010-12-07 20:16:56 +0100328void fuse_queue_forget(struct fuse_conn *fc, struct fuse_forget_link *forget,
329 u64 nodeid, u64 nlookup)
330{
Miklos Szeredif88996a2015-07-01 16:26:01 +0200331 struct fuse_iqueue *fiq = &fc->iq;
332
Miklos Szeredi02c048b2010-12-07 20:16:56 +0100333 forget->forget_one.nodeid = nodeid;
334 forget->forget_one.nlookup = nlookup;
Miklos Szeredi07e77dc2010-12-07 20:16:56 +0100335
Miklos Szeredi4ce60812015-07-01 16:26:02 +0200336 spin_lock(&fiq->waitq.lock);
Miklos Szeredie16714d2015-07-01 16:26:01 +0200337 if (fiq->connected) {
Miklos Szeredif88996a2015-07-01 16:26:01 +0200338 fiq->forget_list_tail->next = forget;
339 fiq->forget_list_tail = forget;
Miklos Szeredi4ce60812015-07-01 16:26:02 +0200340 wake_up_locked(&fiq->waitq);
Miklos Szeredif88996a2015-07-01 16:26:01 +0200341 kill_fasync(&fiq->fasync, SIGIO, POLL_IN);
Miklos Szeredi5dfcc872011-09-12 09:38:03 +0200342 } else {
343 kfree(forget);
344 }
Miklos Szeredi4ce60812015-07-01 16:26:02 +0200345 spin_unlock(&fiq->waitq.lock);
Miklos Szeredi07e77dc2010-12-07 20:16:56 +0100346}
347
Miklos Szeredid12def12008-02-06 01:38:39 -0800348static void flush_bg_queue(struct fuse_conn *fc)
349{
Csaba Henk7a6d3c82009-07-01 17:28:41 -0700350 while (fc->active_background < fc->max_background &&
Miklos Szeredid12def12008-02-06 01:38:39 -0800351 !list_empty(&fc->bg_queue)) {
352 struct fuse_req *req;
Miklos Szeredif88996a2015-07-01 16:26:01 +0200353 struct fuse_iqueue *fiq = &fc->iq;
Miklos Szeredid12def12008-02-06 01:38:39 -0800354
355 req = list_entry(fc->bg_queue.next, struct fuse_req, list);
356 list_del(&req->list);
357 fc->active_background++;
Miklos Szeredi4ce60812015-07-01 16:26:02 +0200358 spin_lock(&fiq->waitq.lock);
Miklos Szeredif88996a2015-07-01 16:26:01 +0200359 req->in.h.unique = fuse_get_unique(fiq);
360 queue_request(fiq, req);
Miklos Szeredi4ce60812015-07-01 16:26:02 +0200361 spin_unlock(&fiq->waitq.lock);
Miklos Szeredid12def12008-02-06 01:38:39 -0800362 }
363}
364
Miklos Szeredi6dbbcb12006-04-26 10:49:06 +0200365/*
Miklos Szeredi334f4852005-09-09 13:10:27 -0700366 * This function is called when a request is finished. Either a reply
Miklos Szeredif9a28422006-06-25 05:48:53 -0700367 * has arrived or it was aborted (and not yet sent) or some error
Miklos Szeredif43b1552006-01-16 22:14:26 -0800368 * occurred during communication with userspace, or the device file
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700369 * was closed. The requester thread is woken up (if still waiting),
370 * the 'end' callback is called if given, else the reference to the
371 * request is released
Miklos Szeredi334f4852005-09-09 13:10:27 -0700372 */
373static void request_end(struct fuse_conn *fc, struct fuse_req *req)
374{
Miklos Szeredi4ce60812015-07-01 16:26:02 +0200375 struct fuse_iqueue *fiq = &fc->iq;
Miklos Szeredi365ae712015-07-01 16:26:06 +0200376
Miklos Szerediefe28002015-07-01 16:26:07 +0200377 if (test_and_set_bit(FR_FINISHED, &req->flags))
Miklos Szeredi6465d762018-07-26 16:13:11 +0200378 goto put_request;
Miklos Szeredi365ae712015-07-01 16:26:06 +0200379
Miklos Szeredi4ce60812015-07-01 16:26:02 +0200380 spin_lock(&fiq->waitq.lock);
Miklos Szeredi0d8e84b2015-07-01 16:25:58 +0200381 list_del_init(&req->intr_entry);
Miklos Szeredi4ce60812015-07-01 16:26:02 +0200382 spin_unlock(&fiq->waitq.lock);
Miklos Szeredi33e14b42015-07-01 16:26:01 +0200383 WARN_ON(test_bit(FR_PENDING, &req->flags));
384 WARN_ON(test_bit(FR_SENT, &req->flags));
Miklos Szeredi825d6d32015-07-01 16:25:58 +0200385 if (test_bit(FR_BACKGROUND, &req->flags)) {
Miklos Szerediefe28002015-07-01 16:26:07 +0200386 spin_lock(&fc->lock);
Miklos Szeredi825d6d32015-07-01 16:25:58 +0200387 clear_bit(FR_BACKGROUND, &req->flags);
Miklos Szeredi7238fcb2018-09-28 16:43:22 +0200388 if (fc->num_background == fc->max_background) {
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700389 fc->blocked = 0;
Maxim Patlasov722d2be2013-03-21 18:02:36 +0400390 wake_up(&fc->blocked_waitq);
Miklos Szeredi7238fcb2018-09-28 16:43:22 +0200391 } else if (!fc->blocked) {
392 /*
393 * Wake up next waiter, if any. It's okay to use
394 * waitqueue_active(), as we've already synced up
395 * fc->blocked with waiters with the wake_up() call
396 * above.
397 */
398 if (waitqueue_active(&fc->blocked_waitq))
399 wake_up(&fc->blocked_waitq);
400 }
Maxim Patlasov722d2be2013-03-21 18:02:36 +0400401
Csaba Henk7a6d3c82009-07-01 17:28:41 -0700402 if (fc->num_background == fc->congestion_threshold &&
Tejun Heoa325f9b2009-04-14 10:54:52 +0900403 fc->connected && fc->bdi_initialized) {
Jens Axboe8aa7e842009-07-09 14:52:32 +0200404 clear_bdi_congested(&fc->bdi, BLK_RW_SYNC);
405 clear_bdi_congested(&fc->bdi, BLK_RW_ASYNC);
Miklos Szeredif92b99b2007-10-16 23:30:59 -0700406 }
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700407 fc->num_background--;
Miklos Szeredid12def12008-02-06 01:38:39 -0800408 fc->active_background--;
409 flush_bg_queue(fc);
Miklos Szerediefe28002015-07-01 16:26:07 +0200410 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700411 }
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700412 wake_up(&req->waitq);
Miklos Szeredi1e6881c2015-07-01 16:26:07 +0200413 if (req->end)
414 req->end(fc, req);
Miklos Szeredi6465d762018-07-26 16:13:11 +0200415put_request:
Tejun Heoe9bb09d2008-11-26 12:03:54 +0100416 fuse_put_request(fc, req);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700417}
418
Miklos Szeredif88996a2015-07-01 16:26:01 +0200419static void queue_interrupt(struct fuse_iqueue *fiq, struct fuse_req *req)
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700420{
Miklos Szeredi4ce60812015-07-01 16:26:02 +0200421 spin_lock(&fiq->waitq.lock);
Sahitya Tummala72834482017-02-08 20:30:56 +0530422 if (test_bit(FR_FINISHED, &req->flags)) {
423 spin_unlock(&fiq->waitq.lock);
424 return;
425 }
Miklos Szeredi8f7bb362015-07-01 16:26:03 +0200426 if (list_empty(&req->intr_entry)) {
427 list_add_tail(&req->intr_entry, &fiq->interrupts);
428 wake_up_locked(&fiq->waitq);
429 }
Miklos Szeredi4ce60812015-07-01 16:26:02 +0200430 spin_unlock(&fiq->waitq.lock);
Miklos Szeredif88996a2015-07-01 16:26:01 +0200431 kill_fasync(&fiq->fasync, SIGIO, POLL_IN);
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700432}
433
Miklos Szeredi7c352bd2005-09-09 13:10:39 -0700434static void request_wait_answer(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700435{
Miklos Szeredi4ce60812015-07-01 16:26:02 +0200436 struct fuse_iqueue *fiq = &fc->iq;
Miklos Szeredic4775262015-07-01 16:26:00 +0200437 int err;
438
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700439 if (!fc->no_interrupt) {
440 /* Any signal may interrupt this */
Miklos Szeredic4775262015-07-01 16:26:00 +0200441 err = wait_event_interruptible(req->waitq,
Miklos Szeredi33e14b42015-07-01 16:26:01 +0200442 test_bit(FR_FINISHED, &req->flags));
Miklos Szeredic4775262015-07-01 16:26:00 +0200443 if (!err)
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700444 return;
445
Miklos Szeredi825d6d32015-07-01 16:25:58 +0200446 set_bit(FR_INTERRUPTED, &req->flags);
Miklos Szeredi8f7bb362015-07-01 16:26:03 +0200447 /* matches barrier in fuse_dev_do_read() */
448 smp_mb__after_atomic();
Miklos Szeredi33e14b42015-07-01 16:26:01 +0200449 if (test_bit(FR_SENT, &req->flags))
Miklos Szeredi4ce60812015-07-01 16:26:02 +0200450 queue_interrupt(fiq, req);
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700451 }
452
Miklos Szeredi825d6d32015-07-01 16:25:58 +0200453 if (!test_bit(FR_FORCE, &req->flags)) {
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700454 /* Only fatal signals may interrupt this */
Al Viro7d3a07f2016-07-19 03:08:27 -0400455 err = wait_event_killable(req->waitq,
Miklos Szeredi33e14b42015-07-01 16:26:01 +0200456 test_bit(FR_FINISHED, &req->flags));
Miklos Szeredic4775262015-07-01 16:26:00 +0200457 if (!err)
Miklos Szeredia131de02007-10-16 23:31:04 -0700458 return;
459
Miklos Szeredi4ce60812015-07-01 16:26:02 +0200460 spin_lock(&fiq->waitq.lock);
Miklos Szeredia131de02007-10-16 23:31:04 -0700461 /* Request is not yet in userspace, bail out */
Miklos Szeredi33e14b42015-07-01 16:26:01 +0200462 if (test_bit(FR_PENDING, &req->flags)) {
Miklos Szeredia131de02007-10-16 23:31:04 -0700463 list_del(&req->list);
Miklos Szeredi4ce60812015-07-01 16:26:02 +0200464 spin_unlock(&fiq->waitq.lock);
Miklos Szeredia131de02007-10-16 23:31:04 -0700465 __fuse_put_request(req);
466 req->out.h.error = -EINTR;
467 return;
468 }
Miklos Szeredi4ce60812015-07-01 16:26:02 +0200469 spin_unlock(&fiq->waitq.lock);
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700470 }
Miklos Szeredi334f4852005-09-09 13:10:27 -0700471
Miklos Szeredia131de02007-10-16 23:31:04 -0700472 /*
473 * Either request is already in userspace, or it was forced.
474 * Wait it out.
475 */
Todd Poynor1672c662011-08-24 15:01:30 -0700476 while (!test_bit(FR_FINISHED, &req->flags))
477 wait_event_freezable(req->waitq,
478 test_bit(FR_FINISHED, &req->flags));
Miklos Szeredi334f4852005-09-09 13:10:27 -0700479}
480
Eric Wong6a4e9222013-02-04 13:04:44 +0000481static void __fuse_request_send(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700482{
Miklos Szeredie16714d2015-07-01 16:26:01 +0200483 struct fuse_iqueue *fiq = &fc->iq;
484
Miklos Szeredi825d6d32015-07-01 16:25:58 +0200485 BUG_ON(test_bit(FR_BACKGROUND, &req->flags));
Miklos Szeredi4ce60812015-07-01 16:26:02 +0200486 spin_lock(&fiq->waitq.lock);
Miklos Szeredie16714d2015-07-01 16:26:01 +0200487 if (!fiq->connected) {
Miklos Szeredi4ce60812015-07-01 16:26:02 +0200488 spin_unlock(&fiq->waitq.lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700489 req->out.h.error = -ENOTCONN;
Miklos Szeredic4775262015-07-01 16:26:00 +0200490 } else {
Miklos Szeredif88996a2015-07-01 16:26:01 +0200491 req->in.h.unique = fuse_get_unique(fiq);
492 queue_request(fiq, req);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700493 /* acquire extra reference, since request is still needed
494 after request_end() */
495 __fuse_get_request(req);
Miklos Szeredi4ce60812015-07-01 16:26:02 +0200496 spin_unlock(&fiq->waitq.lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700497
Miklos Szeredi7c352bd2005-09-09 13:10:39 -0700498 request_wait_answer(fc, req);
Miklos Szeredic4775262015-07-01 16:26:00 +0200499 /* Pairs with smp_wmb() in request_end() */
500 smp_rmb();
Miklos Szeredi334f4852005-09-09 13:10:27 -0700501 }
Miklos Szeredi334f4852005-09-09 13:10:27 -0700502}
Eric Wong6a4e9222013-02-04 13:04:44 +0000503
504void fuse_request_send(struct fuse_conn *fc, struct fuse_req *req)
505{
Miklos Szeredi825d6d32015-07-01 16:25:58 +0200506 __set_bit(FR_ISREPLY, &req->flags);
507 if (!test_bit(FR_WAITING, &req->flags)) {
508 __set_bit(FR_WAITING, &req->flags);
Miklos Szeredi5437f242015-07-01 16:25:56 +0200509 atomic_inc(&fc->num_waiting);
510 }
Eric Wong6a4e9222013-02-04 13:04:44 +0000511 __fuse_request_send(fc, req);
512}
Tejun Heo08cbf542009-04-14 10:54:53 +0900513EXPORT_SYMBOL_GPL(fuse_request_send);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700514
Miklos Szeredi21f62172015-01-06 10:45:35 +0100515static void fuse_adjust_compat(struct fuse_conn *fc, struct fuse_args *args)
516{
517 if (fc->minor < 4 && args->in.h.opcode == FUSE_STATFS)
518 args->out.args[0].size = FUSE_COMPAT_STATFS_SIZE;
519
520 if (fc->minor < 9) {
521 switch (args->in.h.opcode) {
522 case FUSE_LOOKUP:
523 case FUSE_CREATE:
524 case FUSE_MKNOD:
525 case FUSE_MKDIR:
526 case FUSE_SYMLINK:
527 case FUSE_LINK:
528 args->out.args[0].size = FUSE_COMPAT_ENTRY_OUT_SIZE;
529 break;
530 case FUSE_GETATTR:
531 case FUSE_SETATTR:
532 args->out.args[0].size = FUSE_COMPAT_ATTR_OUT_SIZE;
533 break;
534 }
535 }
536 if (fc->minor < 12) {
537 switch (args->in.h.opcode) {
538 case FUSE_CREATE:
539 args->in.args[0].size = sizeof(struct fuse_open_in);
540 break;
541 case FUSE_MKNOD:
542 args->in.args[0].size = FUSE_COMPAT_MKNOD_IN_SIZE;
543 break;
544 }
545 }
546}
547
Miklos Szeredi70781872014-12-12 09:49:05 +0100548ssize_t fuse_simple_request(struct fuse_conn *fc, struct fuse_args *args)
549{
550 struct fuse_req *req;
551 ssize_t ret;
552
553 req = fuse_get_req(fc, 0);
554 if (IS_ERR(req))
555 return PTR_ERR(req);
556
Miklos Szeredi21f62172015-01-06 10:45:35 +0100557 /* Needs to be done after fuse_get_req() so that fc->minor is valid */
558 fuse_adjust_compat(fc, args);
559
Miklos Szeredi70781872014-12-12 09:49:05 +0100560 req->in.h.opcode = args->in.h.opcode;
561 req->in.h.nodeid = args->in.h.nodeid;
562 req->in.numargs = args->in.numargs;
563 memcpy(req->in.args, args->in.args,
564 args->in.numargs * sizeof(struct fuse_in_arg));
565 req->out.argvar = args->out.argvar;
566 req->out.numargs = args->out.numargs;
567 memcpy(req->out.args, args->out.args,
568 args->out.numargs * sizeof(struct fuse_arg));
569 fuse_request_send(fc, req);
570 ret = req->out.h.error;
571 if (!ret && args->out.argvar) {
572 BUG_ON(args->out.numargs != 1);
573 ret = req->out.args[0].size;
574 }
575 fuse_put_request(fc, req);
576
577 return ret;
578}
579
Miklos Szeredif0139aa2015-07-01 16:25:57 +0200580/*
581 * Called under fc->lock
582 *
583 * fc->connected must have been checked previously
584 */
585void fuse_request_send_background_locked(struct fuse_conn *fc,
586 struct fuse_req *req)
Miklos Szeredid12def12008-02-06 01:38:39 -0800587{
Miklos Szeredi825d6d32015-07-01 16:25:58 +0200588 BUG_ON(!test_bit(FR_BACKGROUND, &req->flags));
589 if (!test_bit(FR_WAITING, &req->flags)) {
590 __set_bit(FR_WAITING, &req->flags);
Miklos Szeredi5437f242015-07-01 16:25:56 +0200591 atomic_inc(&fc->num_waiting);
592 }
Miklos Szeredi825d6d32015-07-01 16:25:58 +0200593 __set_bit(FR_ISREPLY, &req->flags);
Miklos Szeredid12def12008-02-06 01:38:39 -0800594 fc->num_background++;
Csaba Henk7a6d3c82009-07-01 17:28:41 -0700595 if (fc->num_background == fc->max_background)
Miklos Szeredid12def12008-02-06 01:38:39 -0800596 fc->blocked = 1;
Csaba Henk7a6d3c82009-07-01 17:28:41 -0700597 if (fc->num_background == fc->congestion_threshold &&
Tejun Heoa325f9b2009-04-14 10:54:52 +0900598 fc->bdi_initialized) {
Jens Axboe8aa7e842009-07-09 14:52:32 +0200599 set_bdi_congested(&fc->bdi, BLK_RW_SYNC);
600 set_bdi_congested(&fc->bdi, BLK_RW_ASYNC);
Miklos Szeredid12def12008-02-06 01:38:39 -0800601 }
602 list_add_tail(&req->list, &fc->bg_queue);
603 flush_bg_queue(fc);
604}
605
Miklos Szeredif0139aa2015-07-01 16:25:57 +0200606void fuse_request_send_background(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700607{
Miklos Szeredi42dc6212015-07-01 16:25:56 +0200608 BUG_ON(!req->end);
Miklos Szeredid7133112006-04-10 22:54:55 -0700609 spin_lock(&fc->lock);
Miklos Szeredi1e9a4ed2005-09-09 13:10:31 -0700610 if (fc->connected) {
Miklos Szeredif0139aa2015-07-01 16:25:57 +0200611 fuse_request_send_background_locked(fc, req);
Miklos Szeredid7133112006-04-10 22:54:55 -0700612 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700613 } else {
Miklos Szeredi42dc6212015-07-01 16:25:56 +0200614 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700615 req->out.h.error = -ENOTCONN;
Miklos Szeredi42dc6212015-07-01 16:25:56 +0200616 req->end(fc, req);
617 fuse_put_request(fc, req);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700618 }
619}
Tejun Heo08cbf542009-04-14 10:54:53 +0900620EXPORT_SYMBOL_GPL(fuse_request_send_background);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700621
Miklos Szeredi2d45ba32010-07-12 14:41:40 +0200622static int fuse_request_send_notify_reply(struct fuse_conn *fc,
623 struct fuse_req *req, u64 unique)
624{
625 int err = -ENODEV;
Miklos Szeredif88996a2015-07-01 16:26:01 +0200626 struct fuse_iqueue *fiq = &fc->iq;
Miklos Szeredi2d45ba32010-07-12 14:41:40 +0200627
Miklos Szeredi825d6d32015-07-01 16:25:58 +0200628 __clear_bit(FR_ISREPLY, &req->flags);
Miklos Szeredi2d45ba32010-07-12 14:41:40 +0200629 req->in.h.unique = unique;
Miklos Szeredi4ce60812015-07-01 16:26:02 +0200630 spin_lock(&fiq->waitq.lock);
Miklos Szeredie16714d2015-07-01 16:26:01 +0200631 if (fiq->connected) {
Miklos Szeredif88996a2015-07-01 16:26:01 +0200632 queue_request(fiq, req);
Miklos Szeredi2d45ba32010-07-12 14:41:40 +0200633 err = 0;
634 }
Miklos Szeredi4ce60812015-07-01 16:26:02 +0200635 spin_unlock(&fiq->waitq.lock);
Miklos Szeredi2d45ba32010-07-12 14:41:40 +0200636
637 return err;
638}
639
Anand V. Avati0b05b182012-08-19 08:53:23 -0400640void fuse_force_forget(struct file *file, u64 nodeid)
641{
Al Viro6131ffa2013-02-27 16:59:05 -0500642 struct inode *inode = file_inode(file);
Anand V. Avati0b05b182012-08-19 08:53:23 -0400643 struct fuse_conn *fc = get_fuse_conn(inode);
644 struct fuse_req *req;
645 struct fuse_forget_in inarg;
646
647 memset(&inarg, 0, sizeof(inarg));
648 inarg.nlookup = 1;
Maxim Patlasovb111c8c2012-10-26 19:48:30 +0400649 req = fuse_get_req_nofail_nopages(fc, file);
Anand V. Avati0b05b182012-08-19 08:53:23 -0400650 req->in.h.opcode = FUSE_FORGET;
651 req->in.h.nodeid = nodeid;
652 req->in.numargs = 1;
653 req->in.args[0].size = sizeof(inarg);
654 req->in.args[0].value = &inarg;
Miklos Szeredi825d6d32015-07-01 16:25:58 +0200655 __clear_bit(FR_ISREPLY, &req->flags);
Eric Wong6a4e9222013-02-04 13:04:44 +0000656 __fuse_request_send(fc, req);
657 /* ignore errors */
658 fuse_put_request(fc, req);
Anand V. Avati0b05b182012-08-19 08:53:23 -0400659}
660
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700661/*
Miklos Szeredi334f4852005-09-09 13:10:27 -0700662 * Lock the request. Up to the next unlock_request() there mustn't be
663 * anything that could cause a page-fault. If the request was already
Miklos Szeredif9a28422006-06-25 05:48:53 -0700664 * aborted bail out.
Miklos Szeredi334f4852005-09-09 13:10:27 -0700665 */
Miklos Szeredidc008092015-07-01 16:25:58 +0200666static int lock_request(struct fuse_req *req)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700667{
668 int err = 0;
669 if (req) {
Miklos Szeredidc008092015-07-01 16:25:58 +0200670 spin_lock(&req->waitq.lock);
Miklos Szeredi825d6d32015-07-01 16:25:58 +0200671 if (test_bit(FR_ABORTED, &req->flags))
Miklos Szeredi334f4852005-09-09 13:10:27 -0700672 err = -ENOENT;
673 else
Miklos Szeredi825d6d32015-07-01 16:25:58 +0200674 set_bit(FR_LOCKED, &req->flags);
Miklos Szeredidc008092015-07-01 16:25:58 +0200675 spin_unlock(&req->waitq.lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700676 }
677 return err;
678}
679
680/*
Miklos Szeredi0d8e84b2015-07-01 16:25:58 +0200681 * Unlock request. If it was aborted while locked, caller is responsible
682 * for unlocking and ending the request.
Miklos Szeredi334f4852005-09-09 13:10:27 -0700683 */
Miklos Szeredidc008092015-07-01 16:25:58 +0200684static int unlock_request(struct fuse_req *req)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700685{
Miklos Szeredi0d8e84b2015-07-01 16:25:58 +0200686 int err = 0;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700687 if (req) {
Miklos Szeredidc008092015-07-01 16:25:58 +0200688 spin_lock(&req->waitq.lock);
Miklos Szeredi825d6d32015-07-01 16:25:58 +0200689 if (test_bit(FR_ABORTED, &req->flags))
Miklos Szeredi0d8e84b2015-07-01 16:25:58 +0200690 err = -ENOENT;
691 else
Miklos Szeredi825d6d32015-07-01 16:25:58 +0200692 clear_bit(FR_LOCKED, &req->flags);
Miklos Szeredidc008092015-07-01 16:25:58 +0200693 spin_unlock(&req->waitq.lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700694 }
Miklos Szeredi0d8e84b2015-07-01 16:25:58 +0200695 return err;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700696}
697
698struct fuse_copy_state {
699 int write;
700 struct fuse_req *req;
Al Viro6c09e942015-04-03 22:06:08 -0400701 struct iov_iter *iter;
Miklos Szeredidd3bb142010-05-25 15:06:06 +0200702 struct pipe_buffer *pipebufs;
703 struct pipe_buffer *currbuf;
704 struct pipe_inode_info *pipe;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700705 unsigned long nr_segs;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700706 struct page *pg;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700707 unsigned len;
Miklos Szeredic55a01d2014-07-07 15:28:51 +0200708 unsigned offset;
Miklos Szeredice534fb2010-05-25 15:06:07 +0200709 unsigned move_pages:1;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700710};
711
Miklos Szeredidc008092015-07-01 16:25:58 +0200712static void fuse_copy_init(struct fuse_copy_state *cs, int write,
Al Viro6c09e942015-04-03 22:06:08 -0400713 struct iov_iter *iter)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700714{
715 memset(cs, 0, sizeof(*cs));
716 cs->write = write;
Al Viro6c09e942015-04-03 22:06:08 -0400717 cs->iter = iter;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700718}
719
720/* Unmap and put previous page of userspace buffer */
Miklos Szeredi8bfc0162006-01-16 22:14:28 -0800721static void fuse_copy_finish(struct fuse_copy_state *cs)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700722{
Miklos Szeredidd3bb142010-05-25 15:06:06 +0200723 if (cs->currbuf) {
724 struct pipe_buffer *buf = cs->currbuf;
725
Miklos Szeredic55a01d2014-07-07 15:28:51 +0200726 if (cs->write)
Miklos Szeredic3021622010-05-25 15:06:07 +0200727 buf->len = PAGE_SIZE - cs->len;
Miklos Szeredidd3bb142010-05-25 15:06:06 +0200728 cs->currbuf = NULL;
Miklos Szeredic55a01d2014-07-07 15:28:51 +0200729 } else if (cs->pg) {
Miklos Szeredi334f4852005-09-09 13:10:27 -0700730 if (cs->write) {
731 flush_dcache_page(cs->pg);
732 set_page_dirty_lock(cs->pg);
733 }
734 put_page(cs->pg);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700735 }
Miklos Szeredic55a01d2014-07-07 15:28:51 +0200736 cs->pg = NULL;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700737}
738
739/*
740 * Get another pagefull of userspace buffer, and map it to kernel
741 * address space, and lock request
742 */
743static int fuse_copy_fill(struct fuse_copy_state *cs)
744{
Miklos Szeredic55a01d2014-07-07 15:28:51 +0200745 struct page *page;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700746 int err;
747
Miklos Szeredidc008092015-07-01 16:25:58 +0200748 err = unlock_request(cs->req);
Miklos Szeredi0d8e84b2015-07-01 16:25:58 +0200749 if (err)
750 return err;
751
Miklos Szeredi334f4852005-09-09 13:10:27 -0700752 fuse_copy_finish(cs);
Miklos Szeredidd3bb142010-05-25 15:06:06 +0200753 if (cs->pipebufs) {
754 struct pipe_buffer *buf = cs->pipebufs;
755
Miklos Szeredic3021622010-05-25 15:06:07 +0200756 if (!cs->write) {
Miklos Szeredifba597d2016-09-27 10:45:12 +0200757 err = pipe_buf_confirm(cs->pipe, buf);
Miklos Szeredic3021622010-05-25 15:06:07 +0200758 if (err)
759 return err;
Miklos Szeredidd3bb142010-05-25 15:06:06 +0200760
Miklos Szeredic3021622010-05-25 15:06:07 +0200761 BUG_ON(!cs->nr_segs);
762 cs->currbuf = buf;
Miklos Szeredic55a01d2014-07-07 15:28:51 +0200763 cs->pg = buf->page;
764 cs->offset = buf->offset;
Miklos Szeredic3021622010-05-25 15:06:07 +0200765 cs->len = buf->len;
Miklos Szeredic3021622010-05-25 15:06:07 +0200766 cs->pipebufs++;
767 cs->nr_segs--;
768 } else {
Miklos Szeredic3021622010-05-25 15:06:07 +0200769 if (cs->nr_segs == cs->pipe->buffers)
770 return -EIO;
771
772 page = alloc_page(GFP_HIGHUSER);
773 if (!page)
774 return -ENOMEM;
775
776 buf->page = page;
777 buf->offset = 0;
778 buf->len = 0;
779
780 cs->currbuf = buf;
Miklos Szeredic55a01d2014-07-07 15:28:51 +0200781 cs->pg = page;
782 cs->offset = 0;
Miklos Szeredic3021622010-05-25 15:06:07 +0200783 cs->len = PAGE_SIZE;
784 cs->pipebufs++;
785 cs->nr_segs++;
786 }
Miklos Szeredidd3bb142010-05-25 15:06:06 +0200787 } else {
Al Viro6c09e942015-04-03 22:06:08 -0400788 size_t off;
789 err = iov_iter_get_pages(cs->iter, &page, PAGE_SIZE, 1, &off);
Miklos Szeredidd3bb142010-05-25 15:06:06 +0200790 if (err < 0)
791 return err;
Al Viro6c09e942015-04-03 22:06:08 -0400792 BUG_ON(!err);
793 cs->len = err;
794 cs->offset = off;
Miklos Szeredic55a01d2014-07-07 15:28:51 +0200795 cs->pg = page;
Al Viro6c09e942015-04-03 22:06:08 -0400796 iov_iter_advance(cs->iter, err);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700797 }
Miklos Szeredi334f4852005-09-09 13:10:27 -0700798
Miklos Szeredidc008092015-07-01 16:25:58 +0200799 return lock_request(cs->req);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700800}
801
802/* Do as much copy to/from userspace buffer as we can */
Miklos Szeredi8bfc0162006-01-16 22:14:28 -0800803static int fuse_copy_do(struct fuse_copy_state *cs, void **val, unsigned *size)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700804{
805 unsigned ncpy = min(*size, cs->len);
806 if (val) {
Miklos Szeredic55a01d2014-07-07 15:28:51 +0200807 void *pgaddr = kmap_atomic(cs->pg);
808 void *buf = pgaddr + cs->offset;
809
Miklos Szeredi334f4852005-09-09 13:10:27 -0700810 if (cs->write)
Miklos Szeredic55a01d2014-07-07 15:28:51 +0200811 memcpy(buf, *val, ncpy);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700812 else
Miklos Szeredic55a01d2014-07-07 15:28:51 +0200813 memcpy(*val, buf, ncpy);
814
815 kunmap_atomic(pgaddr);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700816 *val += ncpy;
817 }
818 *size -= ncpy;
819 cs->len -= ncpy;
Miklos Szeredic55a01d2014-07-07 15:28:51 +0200820 cs->offset += ncpy;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700821 return ncpy;
822}
823
Miklos Szeredice534fb2010-05-25 15:06:07 +0200824static int fuse_check_page(struct page *page)
825{
826 if (page_mapcount(page) ||
827 page->mapping != NULL ||
828 page_count(page) != 1 ||
829 (page->flags & PAGE_FLAGS_CHECK_AT_PREP &
830 ~(1 << PG_locked |
831 1 << PG_referenced |
832 1 << PG_uptodate |
833 1 << PG_lru |
834 1 << PG_active |
835 1 << PG_reclaim))) {
836 printk(KERN_WARNING "fuse: trying to steal weird page\n");
837 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);
838 return 1;
839 }
840 return 0;
841}
842
843static int fuse_try_move_page(struct fuse_copy_state *cs, struct page **pagep)
844{
845 int err;
846 struct page *oldpage = *pagep;
847 struct page *newpage;
848 struct pipe_buffer *buf = cs->pipebufs;
Miklos Szeredice534fb2010-05-25 15:06:07 +0200849
Miklos Szeredidc008092015-07-01 16:25:58 +0200850 err = unlock_request(cs->req);
Miklos Szeredi0d8e84b2015-07-01 16:25:58 +0200851 if (err)
852 return err;
853
Miklos Szeredice534fb2010-05-25 15:06:07 +0200854 fuse_copy_finish(cs);
855
Miklos Szeredifba597d2016-09-27 10:45:12 +0200856 err = pipe_buf_confirm(cs->pipe, buf);
Miklos Szeredice534fb2010-05-25 15:06:07 +0200857 if (err)
858 return err;
859
860 BUG_ON(!cs->nr_segs);
861 cs->currbuf = buf;
862 cs->len = buf->len;
863 cs->pipebufs++;
864 cs->nr_segs--;
865
866 if (cs->len != PAGE_SIZE)
867 goto out_fallback;
868
Miklos Szeredica76f5b2016-09-27 10:45:12 +0200869 if (pipe_buf_steal(cs->pipe, buf) != 0)
Miklos Szeredice534fb2010-05-25 15:06:07 +0200870 goto out_fallback;
871
872 newpage = buf->page;
873
Miklos Szerediaa991b32015-02-26 11:45:47 +0100874 if (!PageUptodate(newpage))
875 SetPageUptodate(newpage);
Miklos Szeredice534fb2010-05-25 15:06:07 +0200876
877 ClearPageMappedToDisk(newpage);
878
879 if (fuse_check_page(newpage) != 0)
880 goto out_fallback_unlock;
881
Miklos Szeredice534fb2010-05-25 15:06:07 +0200882 /*
883 * This is a new and locked page, it shouldn't be mapped or
884 * have any special flags on it
885 */
886 if (WARN_ON(page_mapped(oldpage)))
887 goto out_fallback_unlock;
888 if (WARN_ON(page_has_private(oldpage)))
889 goto out_fallback_unlock;
890 if (WARN_ON(PageDirty(oldpage) || PageWriteback(oldpage)))
891 goto out_fallback_unlock;
892 if (WARN_ON(PageMlocked(oldpage)))
893 goto out_fallback_unlock;
894
Miklos Szeredief6a3c62011-03-22 16:30:52 -0700895 err = replace_page_cache_page(oldpage, newpage, GFP_KERNEL);
Miklos Szeredice534fb2010-05-25 15:06:07 +0200896 if (err) {
Miklos Szeredief6a3c62011-03-22 16:30:52 -0700897 unlock_page(newpage);
898 return err;
Miklos Szeredice534fb2010-05-25 15:06:07 +0200899 }
Miklos Szeredief6a3c62011-03-22 16:30:52 -0700900
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300901 get_page(newpage);
Miklos Szeredice534fb2010-05-25 15:06:07 +0200902
903 if (!(buf->flags & PIPE_BUF_FLAG_LRU))
904 lru_cache_add_file(newpage);
905
906 err = 0;
Miklos Szeredidc008092015-07-01 16:25:58 +0200907 spin_lock(&cs->req->waitq.lock);
Miklos Szeredi825d6d32015-07-01 16:25:58 +0200908 if (test_bit(FR_ABORTED, &cs->req->flags))
Miklos Szeredice534fb2010-05-25 15:06:07 +0200909 err = -ENOENT;
910 else
911 *pagep = newpage;
Miklos Szeredidc008092015-07-01 16:25:58 +0200912 spin_unlock(&cs->req->waitq.lock);
Miklos Szeredice534fb2010-05-25 15:06:07 +0200913
914 if (err) {
915 unlock_page(newpage);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300916 put_page(newpage);
Miklos Szeredice534fb2010-05-25 15:06:07 +0200917 return err;
918 }
919
920 unlock_page(oldpage);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300921 put_page(oldpage);
Miklos Szeredice534fb2010-05-25 15:06:07 +0200922 cs->len = 0;
923
924 return 0;
925
926out_fallback_unlock:
927 unlock_page(newpage);
928out_fallback:
Miklos Szeredic55a01d2014-07-07 15:28:51 +0200929 cs->pg = buf->page;
930 cs->offset = buf->offset;
Miklos Szeredice534fb2010-05-25 15:06:07 +0200931
Miklos Szeredidc008092015-07-01 16:25:58 +0200932 err = lock_request(cs->req);
Miklos Szeredice534fb2010-05-25 15:06:07 +0200933 if (err)
934 return err;
935
936 return 1;
937}
938
Miklos Szeredic3021622010-05-25 15:06:07 +0200939static int fuse_ref_page(struct fuse_copy_state *cs, struct page *page,
940 unsigned offset, unsigned count)
941{
942 struct pipe_buffer *buf;
Miklos Szeredi0d8e84b2015-07-01 16:25:58 +0200943 int err;
Miklos Szeredic3021622010-05-25 15:06:07 +0200944
945 if (cs->nr_segs == cs->pipe->buffers)
946 return -EIO;
947
Miklos Szeredidc008092015-07-01 16:25:58 +0200948 err = unlock_request(cs->req);
Miklos Szeredi0d8e84b2015-07-01 16:25:58 +0200949 if (err)
950 return err;
951
Miklos Szeredic3021622010-05-25 15:06:07 +0200952 fuse_copy_finish(cs);
953
954 buf = cs->pipebufs;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300955 get_page(page);
Miklos Szeredic3021622010-05-25 15:06:07 +0200956 buf->page = page;
957 buf->offset = offset;
958 buf->len = count;
959
960 cs->pipebufs++;
961 cs->nr_segs++;
962 cs->len = 0;
963
964 return 0;
965}
966
Miklos Szeredi334f4852005-09-09 13:10:27 -0700967/*
968 * Copy a page in the request to/from the userspace buffer. Must be
969 * done atomically
970 */
Miklos Szeredice534fb2010-05-25 15:06:07 +0200971static int fuse_copy_page(struct fuse_copy_state *cs, struct page **pagep,
Miklos Szeredi8bfc0162006-01-16 22:14:28 -0800972 unsigned offset, unsigned count, int zeroing)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700973{
Miklos Szeredice534fb2010-05-25 15:06:07 +0200974 int err;
975 struct page *page = *pagep;
976
Miklos Szeredib6777c42010-10-26 14:22:27 -0700977 if (page && zeroing && count < PAGE_SIZE)
978 clear_highpage(page);
979
Miklos Szeredi334f4852005-09-09 13:10:27 -0700980 while (count) {
Miklos Szeredic3021622010-05-25 15:06:07 +0200981 if (cs->write && cs->pipebufs && page) {
982 return fuse_ref_page(cs, page, offset, count);
983 } else if (!cs->len) {
Miklos Szeredice534fb2010-05-25 15:06:07 +0200984 if (cs->move_pages && page &&
985 offset == 0 && count == PAGE_SIZE) {
986 err = fuse_try_move_page(cs, pagep);
987 if (err <= 0)
988 return err;
989 } else {
990 err = fuse_copy_fill(cs);
991 if (err)
992 return err;
993 }
Miklos Szeredi1729a162008-11-26 12:03:54 +0100994 }
Miklos Szeredi334f4852005-09-09 13:10:27 -0700995 if (page) {
Cong Wang2408f6e2011-11-25 23:14:30 +0800996 void *mapaddr = kmap_atomic(page);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700997 void *buf = mapaddr + offset;
998 offset += fuse_copy_do(cs, &buf, &count);
Cong Wang2408f6e2011-11-25 23:14:30 +0800999 kunmap_atomic(mapaddr);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001000 } else
1001 offset += fuse_copy_do(cs, NULL, &count);
1002 }
1003 if (page && !cs->write)
1004 flush_dcache_page(page);
1005 return 0;
1006}
1007
1008/* Copy pages in the request to/from userspace buffer */
1009static int fuse_copy_pages(struct fuse_copy_state *cs, unsigned nbytes,
1010 int zeroing)
1011{
1012 unsigned i;
1013 struct fuse_req *req = cs->req;
Miklos Szeredi334f4852005-09-09 13:10:27 -07001014
1015 for (i = 0; i < req->num_pages && (nbytes || zeroing); i++) {
Miklos Szeredice534fb2010-05-25 15:06:07 +02001016 int err;
Maxim Patlasov85f40ae2012-10-26 19:49:33 +04001017 unsigned offset = req->page_descs[i].offset;
1018 unsigned count = min(nbytes, req->page_descs[i].length);
Miklos Szeredice534fb2010-05-25 15:06:07 +02001019
1020 err = fuse_copy_page(cs, &req->pages[i], offset, count,
1021 zeroing);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001022 if (err)
1023 return err;
1024
1025 nbytes -= count;
Miklos Szeredi334f4852005-09-09 13:10:27 -07001026 }
1027 return 0;
1028}
1029
1030/* Copy a single argument in the request to/from userspace buffer */
1031static int fuse_copy_one(struct fuse_copy_state *cs, void *val, unsigned size)
1032{
1033 while (size) {
Miklos Szeredi1729a162008-11-26 12:03:54 +01001034 if (!cs->len) {
1035 int err = fuse_copy_fill(cs);
1036 if (err)
1037 return err;
1038 }
Miklos Szeredi334f4852005-09-09 13:10:27 -07001039 fuse_copy_do(cs, &val, &size);
1040 }
1041 return 0;
1042}
1043
1044/* Copy request arguments to/from userspace buffer */
1045static int fuse_copy_args(struct fuse_copy_state *cs, unsigned numargs,
1046 unsigned argpages, struct fuse_arg *args,
1047 int zeroing)
1048{
1049 int err = 0;
1050 unsigned i;
1051
1052 for (i = 0; !err && i < numargs; i++) {
1053 struct fuse_arg *arg = &args[i];
1054 if (i == numargs - 1 && argpages)
1055 err = fuse_copy_pages(cs, arg->size, zeroing);
1056 else
1057 err = fuse_copy_one(cs, arg->value, arg->size);
1058 }
1059 return err;
1060}
1061
Miklos Szeredif88996a2015-07-01 16:26:01 +02001062static int forget_pending(struct fuse_iqueue *fiq)
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001063{
Miklos Szeredif88996a2015-07-01 16:26:01 +02001064 return fiq->forget_list_head.next != NULL;
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001065}
1066
Miklos Szeredif88996a2015-07-01 16:26:01 +02001067static int request_pending(struct fuse_iqueue *fiq)
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001068{
Miklos Szeredif88996a2015-07-01 16:26:01 +02001069 return !list_empty(&fiq->pending) || !list_empty(&fiq->interrupts) ||
1070 forget_pending(fiq);
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001071}
1072
Miklos Szeredi334f4852005-09-09 13:10:27 -07001073/*
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001074 * Transfer an interrupt request to userspace
1075 *
1076 * Unlike other requests this is assembled on demand, without a need
1077 * to allocate a separate fuse_req structure.
1078 *
Miklos Szeredifd22d622015-07-01 16:26:03 +02001079 * Called with fiq->waitq.lock held, releases it
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001080 */
Miklos Szeredifd22d622015-07-01 16:26:03 +02001081static int fuse_read_interrupt(struct fuse_iqueue *fiq,
1082 struct fuse_copy_state *cs,
Miklos Szeredic3021622010-05-25 15:06:07 +02001083 size_t nbytes, struct fuse_req *req)
Miklos Szeredifd22d622015-07-01 16:26:03 +02001084__releases(fiq->waitq.lock)
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001085{
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001086 struct fuse_in_header ih;
1087 struct fuse_interrupt_in arg;
1088 unsigned reqsize = sizeof(ih) + sizeof(arg);
1089 int err;
1090
1091 list_del_init(&req->intr_entry);
Miklos Szeredi4ce60812015-07-01 16:26:02 +02001092 req->intr_unique = fuse_get_unique(fiq);
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001093 memset(&ih, 0, sizeof(ih));
1094 memset(&arg, 0, sizeof(arg));
1095 ih.len = reqsize;
1096 ih.opcode = FUSE_INTERRUPT;
1097 ih.unique = req->intr_unique;
1098 arg.unique = req->in.h.unique;
1099
Miklos Szeredi4ce60812015-07-01 16:26:02 +02001100 spin_unlock(&fiq->waitq.lock);
Miklos Szeredic3021622010-05-25 15:06:07 +02001101 if (nbytes < reqsize)
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001102 return -EINVAL;
1103
Miklos Szeredic3021622010-05-25 15:06:07 +02001104 err = fuse_copy_one(cs, &ih, sizeof(ih));
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001105 if (!err)
Miklos Szeredic3021622010-05-25 15:06:07 +02001106 err = fuse_copy_one(cs, &arg, sizeof(arg));
1107 fuse_copy_finish(cs);
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001108
1109 return err ? err : reqsize;
1110}
1111
Miklos Szeredif88996a2015-07-01 16:26:01 +02001112static struct fuse_forget_link *dequeue_forget(struct fuse_iqueue *fiq,
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001113 unsigned max,
1114 unsigned *countp)
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001115{
Miklos Szeredif88996a2015-07-01 16:26:01 +02001116 struct fuse_forget_link *head = fiq->forget_list_head.next;
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001117 struct fuse_forget_link **newhead = &head;
1118 unsigned count;
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001119
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001120 for (count = 0; *newhead != NULL && count < max; count++)
1121 newhead = &(*newhead)->next;
1122
Miklos Szeredif88996a2015-07-01 16:26:01 +02001123 fiq->forget_list_head.next = *newhead;
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001124 *newhead = NULL;
Miklos Szeredif88996a2015-07-01 16:26:01 +02001125 if (fiq->forget_list_head.next == NULL)
1126 fiq->forget_list_tail = &fiq->forget_list_head;
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001127
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001128 if (countp != NULL)
1129 *countp = count;
1130
1131 return head;
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001132}
1133
Miklos Szeredifd22d622015-07-01 16:26:03 +02001134static int fuse_read_single_forget(struct fuse_iqueue *fiq,
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001135 struct fuse_copy_state *cs,
1136 size_t nbytes)
Miklos Szeredifd22d622015-07-01 16:26:03 +02001137__releases(fiq->waitq.lock)
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001138{
1139 int err;
Miklos Szeredif88996a2015-07-01 16:26:01 +02001140 struct fuse_forget_link *forget = dequeue_forget(fiq, 1, NULL);
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001141 struct fuse_forget_in arg = {
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001142 .nlookup = forget->forget_one.nlookup,
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001143 };
1144 struct fuse_in_header ih = {
1145 .opcode = FUSE_FORGET,
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001146 .nodeid = forget->forget_one.nodeid,
Miklos Szeredif88996a2015-07-01 16:26:01 +02001147 .unique = fuse_get_unique(fiq),
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001148 .len = sizeof(ih) + sizeof(arg),
1149 };
1150
Miklos Szeredi4ce60812015-07-01 16:26:02 +02001151 spin_unlock(&fiq->waitq.lock);
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001152 kfree(forget);
1153 if (nbytes < ih.len)
1154 return -EINVAL;
1155
1156 err = fuse_copy_one(cs, &ih, sizeof(ih));
1157 if (!err)
1158 err = fuse_copy_one(cs, &arg, sizeof(arg));
1159 fuse_copy_finish(cs);
1160
1161 if (err)
1162 return err;
1163
1164 return ih.len;
1165}
1166
Miklos Szeredifd22d622015-07-01 16:26:03 +02001167static int fuse_read_batch_forget(struct fuse_iqueue *fiq,
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001168 struct fuse_copy_state *cs, size_t nbytes)
Miklos Szeredifd22d622015-07-01 16:26:03 +02001169__releases(fiq->waitq.lock)
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001170{
1171 int err;
1172 unsigned max_forgets;
1173 unsigned count;
1174 struct fuse_forget_link *head;
1175 struct fuse_batch_forget_in arg = { .count = 0 };
1176 struct fuse_in_header ih = {
1177 .opcode = FUSE_BATCH_FORGET,
Miklos Szeredif88996a2015-07-01 16:26:01 +02001178 .unique = fuse_get_unique(fiq),
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001179 .len = sizeof(ih) + sizeof(arg),
1180 };
1181
1182 if (nbytes < ih.len) {
Miklos Szeredi4ce60812015-07-01 16:26:02 +02001183 spin_unlock(&fiq->waitq.lock);
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001184 return -EINVAL;
1185 }
1186
1187 max_forgets = (nbytes - ih.len) / sizeof(struct fuse_forget_one);
Miklos Szeredif88996a2015-07-01 16:26:01 +02001188 head = dequeue_forget(fiq, max_forgets, &count);
Miklos Szeredi4ce60812015-07-01 16:26:02 +02001189 spin_unlock(&fiq->waitq.lock);
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001190
1191 arg.count = count;
1192 ih.len += count * sizeof(struct fuse_forget_one);
1193 err = fuse_copy_one(cs, &ih, sizeof(ih));
1194 if (!err)
1195 err = fuse_copy_one(cs, &arg, sizeof(arg));
1196
1197 while (head) {
1198 struct fuse_forget_link *forget = head;
1199
1200 if (!err) {
1201 err = fuse_copy_one(cs, &forget->forget_one,
1202 sizeof(forget->forget_one));
1203 }
1204 head = forget->next;
1205 kfree(forget);
1206 }
1207
1208 fuse_copy_finish(cs);
1209
1210 if (err)
1211 return err;
1212
1213 return ih.len;
1214}
1215
Miklos Szeredifd22d622015-07-01 16:26:03 +02001216static int fuse_read_forget(struct fuse_conn *fc, struct fuse_iqueue *fiq,
1217 struct fuse_copy_state *cs,
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001218 size_t nbytes)
Miklos Szeredifd22d622015-07-01 16:26:03 +02001219__releases(fiq->waitq.lock)
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001220{
Miklos Szeredif88996a2015-07-01 16:26:01 +02001221 if (fc->minor < 16 || fiq->forget_list_head.next->next == NULL)
Miklos Szeredifd22d622015-07-01 16:26:03 +02001222 return fuse_read_single_forget(fiq, cs, nbytes);
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001223 else
Miklos Szeredifd22d622015-07-01 16:26:03 +02001224 return fuse_read_batch_forget(fiq, cs, nbytes);
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001225}
1226
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001227/*
Miklos Szeredi334f4852005-09-09 13:10:27 -07001228 * Read a single request into the userspace filesystem's buffer. This
1229 * function waits until a request is available, then removes it from
1230 * the pending list and copies request data to userspace buffer. If
Miklos Szeredif9a28422006-06-25 05:48:53 -07001231 * no reply is needed (FORGET) or request has been aborted or there
1232 * was an error during the copying then it's finished by calling
Miklos Szeredi334f4852005-09-09 13:10:27 -07001233 * request_end(). Otherwise add it to the processing list, and set
1234 * the 'sent' flag.
1235 */
Miklos Szeredic36960462015-07-01 16:26:09 +02001236static ssize_t fuse_dev_do_read(struct fuse_dev *fud, struct file *file,
Miklos Szeredic3021622010-05-25 15:06:07 +02001237 struct fuse_copy_state *cs, size_t nbytes)
Miklos Szeredi334f4852005-09-09 13:10:27 -07001238{
Miklos Szeredi82cbdcd2015-07-01 16:26:05 +02001239 ssize_t err;
Miklos Szeredic36960462015-07-01 16:26:09 +02001240 struct fuse_conn *fc = fud->fc;
Miklos Szeredif88996a2015-07-01 16:26:01 +02001241 struct fuse_iqueue *fiq = &fc->iq;
Miklos Szeredic36960462015-07-01 16:26:09 +02001242 struct fuse_pqueue *fpq = &fud->pq;
Miklos Szeredi334f4852005-09-09 13:10:27 -07001243 struct fuse_req *req;
1244 struct fuse_in *in;
Miklos Szeredi334f4852005-09-09 13:10:27 -07001245 unsigned reqsize;
1246
Miklos Szeredi1d3d7522006-01-06 00:19:40 -08001247 restart:
Miklos Szeredi4ce60812015-07-01 16:26:02 +02001248 spin_lock(&fiq->waitq.lock);
Jeff Dikee5ac1d12006-04-10 22:54:53 -07001249 err = -EAGAIN;
Miklos Szeredie16714d2015-07-01 16:26:01 +02001250 if ((file->f_flags & O_NONBLOCK) && fiq->connected &&
Miklos Szeredif88996a2015-07-01 16:26:01 +02001251 !request_pending(fiq))
Jeff Dikee5ac1d12006-04-10 22:54:53 -07001252 goto err_unlock;
1253
Miklos Szeredi52509212015-07-01 16:26:03 +02001254 err = wait_event_interruptible_exclusive_locked(fiq->waitq,
1255 !fiq->connected || request_pending(fiq));
1256 if (err)
1257 goto err_unlock;
1258
Miklos Szeredi334f4852005-09-09 13:10:27 -07001259 err = -ENODEV;
Miklos Szeredie16714d2015-07-01 16:26:01 +02001260 if (!fiq->connected)
Miklos Szeredi334f4852005-09-09 13:10:27 -07001261 goto err_unlock;
Miklos Szeredi334f4852005-09-09 13:10:27 -07001262
Miklos Szeredif88996a2015-07-01 16:26:01 +02001263 if (!list_empty(&fiq->interrupts)) {
1264 req = list_entry(fiq->interrupts.next, struct fuse_req,
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001265 intr_entry);
Miklos Szeredifd22d622015-07-01 16:26:03 +02001266 return fuse_read_interrupt(fiq, cs, nbytes, req);
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001267 }
1268
Miklos Szeredif88996a2015-07-01 16:26:01 +02001269 if (forget_pending(fiq)) {
1270 if (list_empty(&fiq->pending) || fiq->forget_batch-- > 0)
Miklos Szeredifd22d622015-07-01 16:26:03 +02001271 return fuse_read_forget(fc, fiq, cs, nbytes);
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001272
Miklos Szeredif88996a2015-07-01 16:26:01 +02001273 if (fiq->forget_batch <= -8)
1274 fiq->forget_batch = 16;
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001275 }
1276
Miklos Szeredif88996a2015-07-01 16:26:01 +02001277 req = list_entry(fiq->pending.next, struct fuse_req, list);
Miklos Szeredi33e14b42015-07-01 16:26:01 +02001278 clear_bit(FR_PENDING, &req->flags);
Miklos Szeredief759252015-07-01 16:26:02 +02001279 list_del_init(&req->list);
Miklos Szeredi4ce60812015-07-01 16:26:02 +02001280 spin_unlock(&fiq->waitq.lock);
1281
Miklos Szeredi334f4852005-09-09 13:10:27 -07001282 in = &req->in;
Miklos Szeredi1d3d7522006-01-06 00:19:40 -08001283 reqsize = in->h.len;
1284 /* If request is too large, reply with an error and restart the read */
Miklos Szeredic3021622010-05-25 15:06:07 +02001285 if (nbytes < reqsize) {
Miklos Szeredi1d3d7522006-01-06 00:19:40 -08001286 req->out.h.error = -EIO;
1287 /* SETXATTR is special, since it may contain too large data */
1288 if (in->h.opcode == FUSE_SETXATTR)
1289 req->out.h.error = -E2BIG;
1290 request_end(fc, req);
1291 goto restart;
Miklos Szeredi334f4852005-09-09 13:10:27 -07001292 }
Miklos Szeredi45a91cb2015-07-01 16:26:06 +02001293 spin_lock(&fpq->lock);
Miklos Szeredi82cbdcd2015-07-01 16:26:05 +02001294 list_add(&req->list, &fpq->io);
Miklos Szeredi45a91cb2015-07-01 16:26:06 +02001295 spin_unlock(&fpq->lock);
Miklos Szeredic3021622010-05-25 15:06:07 +02001296 cs->req = req;
1297 err = fuse_copy_one(cs, &in->h, sizeof(in->h));
Miklos Szeredi1d3d7522006-01-06 00:19:40 -08001298 if (!err)
Miklos Szeredic3021622010-05-25 15:06:07 +02001299 err = fuse_copy_args(cs, in->numargs, in->argpages,
Miklos Szeredi1d3d7522006-01-06 00:19:40 -08001300 (struct fuse_arg *) in->args, 0);
Miklos Szeredic3021622010-05-25 15:06:07 +02001301 fuse_copy_finish(cs);
Miklos Szeredi45a91cb2015-07-01 16:26:06 +02001302 spin_lock(&fpq->lock);
Miklos Szeredi825d6d32015-07-01 16:25:58 +02001303 clear_bit(FR_LOCKED, &req->flags);
Miklos Szeredie96edd92015-07-01 16:26:04 +02001304 if (!fpq->connected) {
Miklos Szeredi82cbdcd2015-07-01 16:26:05 +02001305 err = -ENODEV;
1306 goto out_end;
Miklos Szeredic9c9d7d2007-10-16 23:31:05 -07001307 }
Miklos Szeredi334f4852005-09-09 13:10:27 -07001308 if (err) {
Miklos Szeredic9c9d7d2007-10-16 23:31:05 -07001309 req->out.h.error = -EIO;
Miklos Szeredi82cbdcd2015-07-01 16:26:05 +02001310 goto out_end;
Miklos Szeredi334f4852005-09-09 13:10:27 -07001311 }
Miklos Szeredi825d6d32015-07-01 16:25:58 +02001312 if (!test_bit(FR_ISREPLY, &req->flags)) {
Miklos Szeredi82cbdcd2015-07-01 16:26:05 +02001313 err = reqsize;
1314 goto out_end;
Miklos Szeredi334f4852005-09-09 13:10:27 -07001315 }
Miklos Szeredi82cbdcd2015-07-01 16:26:05 +02001316 list_move_tail(&req->list, &fpq->processing);
Kirill Tkhai7996b1c2018-09-25 12:28:55 +03001317 __fuse_get_request(req);
Miklos Szeredi82cbdcd2015-07-01 16:26:05 +02001318 set_bit(FR_SENT, &req->flags);
Miklos Szeredic31ccf12018-09-28 16:43:22 +02001319 spin_unlock(&fpq->lock);
Miklos Szeredi82cbdcd2015-07-01 16:26:05 +02001320 /* matches barrier in request_wait_answer() */
1321 smp_mb__after_atomic();
1322 if (test_bit(FR_INTERRUPTED, &req->flags))
1323 queue_interrupt(fiq, req);
Kirill Tkhai7996b1c2018-09-25 12:28:55 +03001324 fuse_put_request(fc, req);
Miklos Szeredi82cbdcd2015-07-01 16:26:05 +02001325
Miklos Szeredi334f4852005-09-09 13:10:27 -07001326 return reqsize;
1327
Miklos Szeredi82cbdcd2015-07-01 16:26:05 +02001328out_end:
Miklos Szeredi77cd9d42015-07-01 16:26:06 +02001329 if (!test_bit(FR_PRIVATE, &req->flags))
1330 list_del_init(&req->list);
Miklos Szeredi45a91cb2015-07-01 16:26:06 +02001331 spin_unlock(&fpq->lock);
Miklos Szeredi82cbdcd2015-07-01 16:26:05 +02001332 request_end(fc, req);
1333 return err;
1334
Miklos Szeredi334f4852005-09-09 13:10:27 -07001335 err_unlock:
Miklos Szeredi4ce60812015-07-01 16:26:02 +02001336 spin_unlock(&fiq->waitq.lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001337 return err;
1338}
1339
Tom Van Braeckel94e4fe22015-01-12 05:22:16 +01001340static int fuse_dev_open(struct inode *inode, struct file *file)
1341{
1342 /*
1343 * The fuse device's file's private_data is used to hold
1344 * the fuse_conn(ection) when it is mounted, and is used to
1345 * keep track of whether the file has been mounted already.
1346 */
1347 file->private_data = NULL;
1348 return 0;
1349}
1350
Al Virofbdbacc2015-04-03 21:53:39 -04001351static ssize_t fuse_dev_read(struct kiocb *iocb, struct iov_iter *to)
Miklos Szeredic3021622010-05-25 15:06:07 +02001352{
1353 struct fuse_copy_state cs;
1354 struct file *file = iocb->ki_filp;
Miklos Szeredicc080e92015-07-01 16:26:08 +02001355 struct fuse_dev *fud = fuse_get_dev(file);
1356
1357 if (!fud)
Miklos Szeredic3021622010-05-25 15:06:07 +02001358 return -EPERM;
1359
Al Virofbdbacc2015-04-03 21:53:39 -04001360 if (!iter_is_iovec(to))
1361 return -EINVAL;
Miklos Szeredic3021622010-05-25 15:06:07 +02001362
Miklos Szeredidc008092015-07-01 16:25:58 +02001363 fuse_copy_init(&cs, 1, to);
Al Virofbdbacc2015-04-03 21:53:39 -04001364
Miklos Szeredic36960462015-07-01 16:26:09 +02001365 return fuse_dev_do_read(fud, file, &cs, iov_iter_count(to));
Miklos Szeredic3021622010-05-25 15:06:07 +02001366}
1367
Miklos Szeredic3021622010-05-25 15:06:07 +02001368static ssize_t fuse_dev_splice_read(struct file *in, loff_t *ppos,
1369 struct pipe_inode_info *pipe,
1370 size_t len, unsigned int flags)
1371{
Al Virod82718e2016-09-17 22:56:25 -04001372 int total, ret;
Miklos Szeredic3021622010-05-25 15:06:07 +02001373 int page_nr = 0;
Miklos Szeredic3021622010-05-25 15:06:07 +02001374 struct pipe_buffer *bufs;
1375 struct fuse_copy_state cs;
Miklos Szeredicc080e92015-07-01 16:26:08 +02001376 struct fuse_dev *fud = fuse_get_dev(in);
1377
1378 if (!fud)
Miklos Szeredic3021622010-05-25 15:06:07 +02001379 return -EPERM;
1380
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001381 bufs = kmalloc(pipe->buffers * sizeof(struct pipe_buffer), GFP_KERNEL);
Miklos Szeredic3021622010-05-25 15:06:07 +02001382 if (!bufs)
1383 return -ENOMEM;
1384
Miklos Szeredidc008092015-07-01 16:25:58 +02001385 fuse_copy_init(&cs, 1, NULL);
Miklos Szeredic3021622010-05-25 15:06:07 +02001386 cs.pipebufs = bufs;
1387 cs.pipe = pipe;
Miklos Szeredic36960462015-07-01 16:26:09 +02001388 ret = fuse_dev_do_read(fud, in, &cs, len);
Miklos Szeredic3021622010-05-25 15:06:07 +02001389 if (ret < 0)
1390 goto out;
1391
Miklos Szeredic3021622010-05-25 15:06:07 +02001392 if (pipe->nrbufs + cs.nr_segs > pipe->buffers) {
1393 ret = -EIO;
Al Virod82718e2016-09-17 22:56:25 -04001394 goto out;
Miklos Szeredic3021622010-05-25 15:06:07 +02001395 }
1396
Al Virod82718e2016-09-17 22:56:25 -04001397 for (ret = total = 0; page_nr < cs.nr_segs; total += ret) {
Miklos Szeredi28a625c2014-01-22 19:36:57 +01001398 /*
1399 * Need to be careful about this. Having buf->ops in module
1400 * code can Oops if the buffer persists after module unload.
1401 */
Al Virod82718e2016-09-17 22:56:25 -04001402 bufs[page_nr].ops = &nosteal_pipe_buf_ops;
Miklos Szeredi80a04772017-02-16 15:08:20 +01001403 bufs[page_nr].flags = 0;
Al Virod82718e2016-09-17 22:56:25 -04001404 ret = add_to_pipe(pipe, &bufs[page_nr++]);
1405 if (unlikely(ret < 0))
1406 break;
Miklos Szeredic3021622010-05-25 15:06:07 +02001407 }
Al Virod82718e2016-09-17 22:56:25 -04001408 if (total)
1409 ret = total;
Miklos Szeredic3021622010-05-25 15:06:07 +02001410out:
1411 for (; page_nr < cs.nr_segs; page_nr++)
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001412 put_page(bufs[page_nr].page);
Miklos Szeredic3021622010-05-25 15:06:07 +02001413
1414 kfree(bufs);
1415 return ret;
1416}
1417
Tejun Heo95668a62008-11-26 12:03:55 +01001418static int fuse_notify_poll(struct fuse_conn *fc, unsigned int size,
1419 struct fuse_copy_state *cs)
1420{
1421 struct fuse_notify_poll_wakeup_out outarg;
Miklos Szeredif6d47a12009-01-26 15:00:59 +01001422 int err = -EINVAL;
Tejun Heo95668a62008-11-26 12:03:55 +01001423
1424 if (size != sizeof(outarg))
Miklos Szeredif6d47a12009-01-26 15:00:59 +01001425 goto err;
Tejun Heo95668a62008-11-26 12:03:55 +01001426
1427 err = fuse_copy_one(cs, &outarg, sizeof(outarg));
1428 if (err)
Miklos Szeredif6d47a12009-01-26 15:00:59 +01001429 goto err;
Tejun Heo95668a62008-11-26 12:03:55 +01001430
Miklos Szeredif6d47a12009-01-26 15:00:59 +01001431 fuse_copy_finish(cs);
Tejun Heo95668a62008-11-26 12:03:55 +01001432 return fuse_notify_poll_wakeup(fc, &outarg);
Miklos Szeredif6d47a12009-01-26 15:00:59 +01001433
1434err:
1435 fuse_copy_finish(cs);
1436 return err;
Tejun Heo95668a62008-11-26 12:03:55 +01001437}
1438
John Muir3b463ae2009-05-31 11:13:57 -04001439static int fuse_notify_inval_inode(struct fuse_conn *fc, unsigned int size,
1440 struct fuse_copy_state *cs)
1441{
1442 struct fuse_notify_inval_inode_out outarg;
1443 int err = -EINVAL;
1444
1445 if (size != sizeof(outarg))
1446 goto err;
1447
1448 err = fuse_copy_one(cs, &outarg, sizeof(outarg));
1449 if (err)
1450 goto err;
1451 fuse_copy_finish(cs);
1452
1453 down_read(&fc->killsb);
1454 err = -ENOENT;
Miklos Szeredib21dda42010-02-05 12:08:31 +01001455 if (fc->sb) {
1456 err = fuse_reverse_inval_inode(fc->sb, outarg.ino,
1457 outarg.off, outarg.len);
1458 }
John Muir3b463ae2009-05-31 11:13:57 -04001459 up_read(&fc->killsb);
1460 return err;
1461
1462err:
1463 fuse_copy_finish(cs);
1464 return err;
1465}
1466
1467static int fuse_notify_inval_entry(struct fuse_conn *fc, unsigned int size,
1468 struct fuse_copy_state *cs)
1469{
1470 struct fuse_notify_inval_entry_out outarg;
Fang Wenqib2d82ee2009-12-30 18:37:13 +08001471 int err = -ENOMEM;
1472 char *buf;
John Muir3b463ae2009-05-31 11:13:57 -04001473 struct qstr name;
1474
Fang Wenqib2d82ee2009-12-30 18:37:13 +08001475 buf = kzalloc(FUSE_NAME_MAX + 1, GFP_KERNEL);
1476 if (!buf)
1477 goto err;
1478
1479 err = -EINVAL;
John Muir3b463ae2009-05-31 11:13:57 -04001480 if (size < sizeof(outarg))
1481 goto err;
1482
1483 err = fuse_copy_one(cs, &outarg, sizeof(outarg));
1484 if (err)
1485 goto err;
1486
1487 err = -ENAMETOOLONG;
1488 if (outarg.namelen > FUSE_NAME_MAX)
1489 goto err;
1490
Miklos Szeredic2183d12011-08-24 10:20:17 +02001491 err = -EINVAL;
1492 if (size != sizeof(outarg) + outarg.namelen + 1)
1493 goto err;
1494
John Muir3b463ae2009-05-31 11:13:57 -04001495 name.name = buf;
1496 name.len = outarg.namelen;
1497 err = fuse_copy_one(cs, buf, outarg.namelen + 1);
1498 if (err)
1499 goto err;
1500 fuse_copy_finish(cs);
1501 buf[outarg.namelen] = 0;
John Muir3b463ae2009-05-31 11:13:57 -04001502
1503 down_read(&fc->killsb);
1504 err = -ENOENT;
Miklos Szeredib21dda42010-02-05 12:08:31 +01001505 if (fc->sb)
John Muir451d0f52011-12-06 21:50:06 +01001506 err = fuse_reverse_inval_entry(fc->sb, outarg.parent, 0, &name);
1507 up_read(&fc->killsb);
1508 kfree(buf);
1509 return err;
1510
1511err:
1512 kfree(buf);
1513 fuse_copy_finish(cs);
1514 return err;
1515}
1516
1517static int fuse_notify_delete(struct fuse_conn *fc, unsigned int size,
1518 struct fuse_copy_state *cs)
1519{
1520 struct fuse_notify_delete_out outarg;
1521 int err = -ENOMEM;
1522 char *buf;
1523 struct qstr name;
1524
1525 buf = kzalloc(FUSE_NAME_MAX + 1, GFP_KERNEL);
1526 if (!buf)
1527 goto err;
1528
1529 err = -EINVAL;
1530 if (size < sizeof(outarg))
1531 goto err;
1532
1533 err = fuse_copy_one(cs, &outarg, sizeof(outarg));
1534 if (err)
1535 goto err;
1536
1537 err = -ENAMETOOLONG;
1538 if (outarg.namelen > FUSE_NAME_MAX)
1539 goto err;
1540
1541 err = -EINVAL;
1542 if (size != sizeof(outarg) + outarg.namelen + 1)
1543 goto err;
1544
1545 name.name = buf;
1546 name.len = outarg.namelen;
1547 err = fuse_copy_one(cs, buf, outarg.namelen + 1);
1548 if (err)
1549 goto err;
1550 fuse_copy_finish(cs);
1551 buf[outarg.namelen] = 0;
John Muir451d0f52011-12-06 21:50:06 +01001552
1553 down_read(&fc->killsb);
1554 err = -ENOENT;
1555 if (fc->sb)
1556 err = fuse_reverse_inval_entry(fc->sb, outarg.parent,
1557 outarg.child, &name);
John Muir3b463ae2009-05-31 11:13:57 -04001558 up_read(&fc->killsb);
Fang Wenqib2d82ee2009-12-30 18:37:13 +08001559 kfree(buf);
John Muir3b463ae2009-05-31 11:13:57 -04001560 return err;
1561
1562err:
Fang Wenqib2d82ee2009-12-30 18:37:13 +08001563 kfree(buf);
John Muir3b463ae2009-05-31 11:13:57 -04001564 fuse_copy_finish(cs);
1565 return err;
1566}
1567
Miklos Szeredia1d75f22010-07-12 14:41:40 +02001568static int fuse_notify_store(struct fuse_conn *fc, unsigned int size,
1569 struct fuse_copy_state *cs)
1570{
1571 struct fuse_notify_store_out outarg;
1572 struct inode *inode;
1573 struct address_space *mapping;
1574 u64 nodeid;
1575 int err;
1576 pgoff_t index;
1577 unsigned int offset;
1578 unsigned int num;
1579 loff_t file_size;
1580 loff_t end;
1581
1582 err = -EINVAL;
1583 if (size < sizeof(outarg))
1584 goto out_finish;
1585
1586 err = fuse_copy_one(cs, &outarg, sizeof(outarg));
1587 if (err)
1588 goto out_finish;
1589
1590 err = -EINVAL;
1591 if (size - sizeof(outarg) != outarg.size)
1592 goto out_finish;
1593
1594 nodeid = outarg.nodeid;
1595
1596 down_read(&fc->killsb);
1597
1598 err = -ENOENT;
1599 if (!fc->sb)
1600 goto out_up_killsb;
1601
1602 inode = ilookup5(fc->sb, nodeid, fuse_inode_eq, &nodeid);
1603 if (!inode)
1604 goto out_up_killsb;
1605
1606 mapping = inode->i_mapping;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001607 index = outarg.offset >> PAGE_SHIFT;
1608 offset = outarg.offset & ~PAGE_MASK;
Miklos Szeredia1d75f22010-07-12 14:41:40 +02001609 file_size = i_size_read(inode);
1610 end = outarg.offset + outarg.size;
1611 if (end > file_size) {
1612 file_size = end;
1613 fuse_write_update_size(inode, file_size);
1614 }
1615
1616 num = outarg.size;
1617 while (num) {
1618 struct page *page;
1619 unsigned int this_num;
1620
1621 err = -ENOMEM;
1622 page = find_or_create_page(mapping, index,
1623 mapping_gfp_mask(mapping));
1624 if (!page)
1625 goto out_iput;
1626
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001627 this_num = min_t(unsigned, num, PAGE_SIZE - offset);
Miklos Szeredia1d75f22010-07-12 14:41:40 +02001628 err = fuse_copy_page(cs, &page, offset, this_num, 0);
Miklos Szeredi063ec1e2014-01-22 19:36:58 +01001629 if (!err && offset == 0 &&
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001630 (this_num == PAGE_SIZE || file_size == end))
Miklos Szeredia1d75f22010-07-12 14:41:40 +02001631 SetPageUptodate(page);
1632 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001633 put_page(page);
Miklos Szeredia1d75f22010-07-12 14:41:40 +02001634
1635 if (err)
1636 goto out_iput;
1637
1638 num -= this_num;
1639 offset = 0;
1640 index++;
1641 }
1642
1643 err = 0;
1644
1645out_iput:
1646 iput(inode);
1647out_up_killsb:
1648 up_read(&fc->killsb);
1649out_finish:
1650 fuse_copy_finish(cs);
1651 return err;
1652}
1653
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001654static void fuse_retrieve_end(struct fuse_conn *fc, struct fuse_req *req)
1655{
Mel Gormanb745bc82014-06-04 16:10:22 -07001656 release_pages(req->pages, req->num_pages, false);
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001657}
1658
1659static int fuse_retrieve(struct fuse_conn *fc, struct inode *inode,
1660 struct fuse_notify_retrieve_out *outarg)
1661{
1662 int err;
1663 struct address_space *mapping = inode->i_mapping;
1664 struct fuse_req *req;
1665 pgoff_t index;
1666 loff_t file_size;
1667 unsigned int num;
1668 unsigned int offset;
Geert Uytterhoeven01574432010-09-30 22:06:21 +02001669 size_t total_len = 0;
Maxim Patlasov4d53dc92012-10-26 19:48:42 +04001670 int num_pages;
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001671
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001672 offset = outarg->offset & ~PAGE_MASK;
Maxim Patlasov4d53dc92012-10-26 19:48:42 +04001673 file_size = i_size_read(inode);
1674
Kirill Smelkov4edf9072019-03-27 10:15:19 +00001675 num = min(outarg->size, fc->max_write);
Maxim Patlasov4d53dc92012-10-26 19:48:42 +04001676 if (outarg->offset > file_size)
1677 num = 0;
1678 else if (outarg->offset + num > file_size)
1679 num = file_size - outarg->offset;
1680
1681 num_pages = (num + offset + PAGE_SIZE - 1) >> PAGE_SHIFT;
1682 num_pages = min(num_pages, FUSE_MAX_PAGES_PER_REQ);
1683
1684 req = fuse_get_req(fc, num_pages);
1685 if (IS_ERR(req))
1686 return PTR_ERR(req);
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001687
1688 req->in.h.opcode = FUSE_NOTIFY_REPLY;
1689 req->in.h.nodeid = outarg->nodeid;
1690 req->in.numargs = 2;
1691 req->in.argpages = 1;
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001692 req->end = fuse_retrieve_end;
1693
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001694 index = outarg->offset >> PAGE_SHIFT;
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001695
Maxim Patlasov4d53dc92012-10-26 19:48:42 +04001696 while (num && req->num_pages < num_pages) {
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001697 struct page *page;
1698 unsigned int this_num;
1699
1700 page = find_get_page(mapping, index);
1701 if (!page)
1702 break;
1703
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001704 this_num = min_t(unsigned, num, PAGE_SIZE - offset);
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001705 req->pages[req->num_pages] = page;
Miklos Szeredi64702de2019-01-16 10:27:59 +01001706 req->page_descs[req->num_pages].offset = offset;
Maxim Patlasov85f40ae2012-10-26 19:49:33 +04001707 req->page_descs[req->num_pages].length = this_num;
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001708 req->num_pages++;
1709
Miklos Szeredic9e67d42012-09-04 18:45:54 +02001710 offset = 0;
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001711 num -= this_num;
1712 total_len += this_num;
Miklos Szeredi48706d02011-12-13 10:36:59 +01001713 index++;
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001714 }
1715 req->misc.retrieve_in.offset = outarg->offset;
1716 req->misc.retrieve_in.size = total_len;
1717 req->in.args[0].size = sizeof(req->misc.retrieve_in);
1718 req->in.args[0].value = &req->misc.retrieve_in;
1719 req->in.args[1].size = total_len;
1720
1721 err = fuse_request_send_notify_reply(fc, req, outarg->notify_unique);
Miklos Szeredid180fee2018-11-09 15:52:16 +01001722 if (err) {
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001723 fuse_retrieve_end(fc, req);
Miklos Szeredid180fee2018-11-09 15:52:16 +01001724 fuse_put_request(fc, req);
1725 }
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001726
1727 return err;
1728}
1729
1730static int fuse_notify_retrieve(struct fuse_conn *fc, unsigned int size,
1731 struct fuse_copy_state *cs)
1732{
1733 struct fuse_notify_retrieve_out outarg;
1734 struct inode *inode;
1735 int err;
1736
1737 err = -EINVAL;
1738 if (size != sizeof(outarg))
1739 goto copy_finish;
1740
1741 err = fuse_copy_one(cs, &outarg, sizeof(outarg));
1742 if (err)
1743 goto copy_finish;
1744
1745 fuse_copy_finish(cs);
1746
1747 down_read(&fc->killsb);
1748 err = -ENOENT;
1749 if (fc->sb) {
1750 u64 nodeid = outarg.nodeid;
1751
1752 inode = ilookup5(fc->sb, nodeid, fuse_inode_eq, &nodeid);
1753 if (inode) {
1754 err = fuse_retrieve(fc, inode, &outarg);
1755 iput(inode);
1756 }
1757 }
1758 up_read(&fc->killsb);
1759
1760 return err;
1761
1762copy_finish:
1763 fuse_copy_finish(cs);
1764 return err;
1765}
1766
Tejun Heo85993962008-11-26 12:03:55 +01001767static int fuse_notify(struct fuse_conn *fc, enum fuse_notify_code code,
1768 unsigned int size, struct fuse_copy_state *cs)
1769{
Miklos Szeredi0d278362015-02-26 11:45:47 +01001770 /* Don't try to move pages (yet) */
1771 cs->move_pages = 0;
1772
Tejun Heo85993962008-11-26 12:03:55 +01001773 switch (code) {
Tejun Heo95668a62008-11-26 12:03:55 +01001774 case FUSE_NOTIFY_POLL:
1775 return fuse_notify_poll(fc, size, cs);
1776
John Muir3b463ae2009-05-31 11:13:57 -04001777 case FUSE_NOTIFY_INVAL_INODE:
1778 return fuse_notify_inval_inode(fc, size, cs);
1779
1780 case FUSE_NOTIFY_INVAL_ENTRY:
1781 return fuse_notify_inval_entry(fc, size, cs);
1782
Miklos Szeredia1d75f22010-07-12 14:41:40 +02001783 case FUSE_NOTIFY_STORE:
1784 return fuse_notify_store(fc, size, cs);
1785
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001786 case FUSE_NOTIFY_RETRIEVE:
1787 return fuse_notify_retrieve(fc, size, cs);
1788
John Muir451d0f52011-12-06 21:50:06 +01001789 case FUSE_NOTIFY_DELETE:
1790 return fuse_notify_delete(fc, size, cs);
1791
Tejun Heo85993962008-11-26 12:03:55 +01001792 default:
Miklos Szeredif6d47a12009-01-26 15:00:59 +01001793 fuse_copy_finish(cs);
Tejun Heo85993962008-11-26 12:03:55 +01001794 return -EINVAL;
1795 }
1796}
1797
Miklos Szeredi334f4852005-09-09 13:10:27 -07001798/* Look up request on processing list by unique ID */
Miklos Szeredi3a2b5b92015-07-01 16:26:04 +02001799static struct fuse_req *request_find(struct fuse_pqueue *fpq, u64 unique)
Miklos Szeredi334f4852005-09-09 13:10:27 -07001800{
Dong Fang05726ac2013-07-30 22:50:01 -04001801 struct fuse_req *req;
Miklos Szeredi334f4852005-09-09 13:10:27 -07001802
Miklos Szeredi3a2b5b92015-07-01 16:26:04 +02001803 list_for_each_entry(req, &fpq->processing, list) {
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001804 if (req->in.h.unique == unique || req->intr_unique == unique)
Miklos Szeredi334f4852005-09-09 13:10:27 -07001805 return req;
1806 }
1807 return NULL;
1808}
1809
1810static int copy_out_args(struct fuse_copy_state *cs, struct fuse_out *out,
1811 unsigned nbytes)
1812{
1813 unsigned reqsize = sizeof(struct fuse_out_header);
1814
1815 if (out->h.error)
1816 return nbytes != reqsize ? -EINVAL : 0;
1817
1818 reqsize += len_args(out->numargs, out->args);
1819
1820 if (reqsize < nbytes || (reqsize > nbytes && !out->argvar))
1821 return -EINVAL;
1822 else if (reqsize > nbytes) {
1823 struct fuse_arg *lastarg = &out->args[out->numargs-1];
1824 unsigned diffsize = reqsize - nbytes;
1825 if (diffsize > lastarg->size)
1826 return -EINVAL;
1827 lastarg->size -= diffsize;
1828 }
1829 return fuse_copy_args(cs, out->numargs, out->argpages, out->args,
1830 out->page_zeroing);
1831}
1832
1833/*
1834 * Write a single reply to a request. First the header is copied from
1835 * the write buffer. The request is then searched on the processing
1836 * list by the unique ID found in the header. If found, then remove
1837 * it from the list and copy the rest of the buffer to the request.
1838 * The request is finished by calling request_end()
1839 */
Miklos Szeredic36960462015-07-01 16:26:09 +02001840static ssize_t fuse_dev_do_write(struct fuse_dev *fud,
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001841 struct fuse_copy_state *cs, size_t nbytes)
Miklos Szeredi334f4852005-09-09 13:10:27 -07001842{
1843 int err;
Miklos Szeredic36960462015-07-01 16:26:09 +02001844 struct fuse_conn *fc = fud->fc;
1845 struct fuse_pqueue *fpq = &fud->pq;
Miklos Szeredi334f4852005-09-09 13:10:27 -07001846 struct fuse_req *req;
1847 struct fuse_out_header oh;
Miklos Szeredi334f4852005-09-09 13:10:27 -07001848
Miklos Szeredi334f4852005-09-09 13:10:27 -07001849 if (nbytes < sizeof(struct fuse_out_header))
1850 return -EINVAL;
1851
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001852 err = fuse_copy_one(cs, &oh, sizeof(oh));
Miklos Szeredi334f4852005-09-09 13:10:27 -07001853 if (err)
1854 goto err_finish;
Tejun Heo85993962008-11-26 12:03:55 +01001855
Miklos Szeredi334f4852005-09-09 13:10:27 -07001856 err = -EINVAL;
Tejun Heo85993962008-11-26 12:03:55 +01001857 if (oh.len != nbytes)
1858 goto err_finish;
1859
1860 /*
1861 * Zero oh.unique indicates unsolicited notification message
1862 * and error contains notification code.
1863 */
1864 if (!oh.unique) {
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001865 err = fuse_notify(fc, oh.error, nbytes - sizeof(oh), cs);
Tejun Heo85993962008-11-26 12:03:55 +01001866 return err ? err : nbytes;
1867 }
1868
1869 err = -EINVAL;
1870 if (oh.error <= -1000 || oh.error > 0)
Miklos Szeredi334f4852005-09-09 13:10:27 -07001871 goto err_finish;
1872
Miklos Szeredi45a91cb2015-07-01 16:26:06 +02001873 spin_lock(&fpq->lock);
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08001874 err = -ENOENT;
Miklos Szeredie96edd92015-07-01 16:26:04 +02001875 if (!fpq->connected)
Miklos Szeredi45a91cb2015-07-01 16:26:06 +02001876 goto err_unlock_pq;
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08001877
Miklos Szeredi3a2b5b92015-07-01 16:26:04 +02001878 req = request_find(fpq, oh.unique);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001879 if (!req)
Miklos Szeredi45a91cb2015-07-01 16:26:06 +02001880 goto err_unlock_pq;
Miklos Szeredi334f4852005-09-09 13:10:27 -07001881
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001882 /* Is it an interrupt reply? */
1883 if (req->intr_unique == oh.unique) {
Kirill Tkhai0799a932018-09-25 12:52:42 +03001884 __fuse_get_request(req);
Miklos Szeredi45a91cb2015-07-01 16:26:06 +02001885 spin_unlock(&fpq->lock);
1886
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001887 err = -EINVAL;
Kirill Tkhai0799a932018-09-25 12:52:42 +03001888 if (nbytes != sizeof(struct fuse_out_header)) {
1889 fuse_put_request(fc, req);
Miklos Szeredi46c34a32015-07-01 16:26:07 +02001890 goto err_finish;
Kirill Tkhai0799a932018-09-25 12:52:42 +03001891 }
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001892
1893 if (oh.error == -ENOSYS)
1894 fc->no_interrupt = 1;
1895 else if (oh.error == -EAGAIN)
Miklos Szeredif88996a2015-07-01 16:26:01 +02001896 queue_interrupt(&fc->iq, req);
Kirill Tkhai0799a932018-09-25 12:52:42 +03001897 fuse_put_request(fc, req);
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001898
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001899 fuse_copy_finish(cs);
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001900 return nbytes;
1901 }
1902
Miklos Szeredi33e14b42015-07-01 16:26:01 +02001903 clear_bit(FR_SENT, &req->flags);
Miklos Szeredi3a2b5b92015-07-01 16:26:04 +02001904 list_move(&req->list, &fpq->io);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001905 req->out.h = oh;
Miklos Szeredi825d6d32015-07-01 16:25:58 +02001906 set_bit(FR_LOCKED, &req->flags);
Miklos Szeredi45a91cb2015-07-01 16:26:06 +02001907 spin_unlock(&fpq->lock);
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001908 cs->req = req;
Miklos Szeredice534fb2010-05-25 15:06:07 +02001909 if (!req->out.page_replace)
1910 cs->move_pages = 0;
Miklos Szeredi334f4852005-09-09 13:10:27 -07001911
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001912 err = copy_out_args(cs, &req->out, nbytes);
Daniel Rosenbergfac99a72016-04-22 00:00:48 -07001913 if (req->in.h.opcode == FUSE_CANONICAL_PATH) {
Ritesh Harjani4fb542f2018-03-19 16:03:09 +05301914 char *path = (char *)req->out.args[0].value;
1915
1916 path[req->out.args[0].size - 1] = 0;
1917 req->out.h.error = kern_path(path, 0, req->canonical_path);
Daniel Rosenbergfac99a72016-04-22 00:00:48 -07001918 }
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001919 fuse_copy_finish(cs);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001920
Miklos Szeredi45a91cb2015-07-01 16:26:06 +02001921 spin_lock(&fpq->lock);
Miklos Szeredi825d6d32015-07-01 16:25:58 +02001922 clear_bit(FR_LOCKED, &req->flags);
Miklos Szeredie96edd92015-07-01 16:26:04 +02001923 if (!fpq->connected)
Miklos Szeredi0d8e84b2015-07-01 16:25:58 +02001924 err = -ENOENT;
1925 else if (err)
Miklos Szeredi334f4852005-09-09 13:10:27 -07001926 req->out.h.error = -EIO;
Miklos Szeredi77cd9d42015-07-01 16:26:06 +02001927 if (!test_bit(FR_PRIVATE, &req->flags))
1928 list_del_init(&req->list);
Miklos Szeredi45a91cb2015-07-01 16:26:06 +02001929 spin_unlock(&fpq->lock);
Miklos Szeredi46c34a32015-07-01 16:26:07 +02001930
Miklos Szeredi334f4852005-09-09 13:10:27 -07001931 request_end(fc, req);
1932
1933 return err ? err : nbytes;
1934
Miklos Szeredi45a91cb2015-07-01 16:26:06 +02001935 err_unlock_pq:
1936 spin_unlock(&fpq->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001937 err_finish:
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001938 fuse_copy_finish(cs);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001939 return err;
1940}
1941
Al Virofbdbacc2015-04-03 21:53:39 -04001942static ssize_t fuse_dev_write(struct kiocb *iocb, struct iov_iter *from)
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001943{
1944 struct fuse_copy_state cs;
Miklos Szeredicc080e92015-07-01 16:26:08 +02001945 struct fuse_dev *fud = fuse_get_dev(iocb->ki_filp);
1946
1947 if (!fud)
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001948 return -EPERM;
1949
Al Virofbdbacc2015-04-03 21:53:39 -04001950 if (!iter_is_iovec(from))
1951 return -EINVAL;
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001952
Miklos Szeredidc008092015-07-01 16:25:58 +02001953 fuse_copy_init(&cs, 0, from);
Al Virofbdbacc2015-04-03 21:53:39 -04001954
Miklos Szeredic36960462015-07-01 16:26:09 +02001955 return fuse_dev_do_write(fud, &cs, iov_iter_count(from));
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001956}
1957
1958static ssize_t fuse_dev_splice_write(struct pipe_inode_info *pipe,
1959 struct file *out, loff_t *ppos,
1960 size_t len, unsigned int flags)
1961{
1962 unsigned nbuf;
1963 unsigned idx;
1964 struct pipe_buffer *bufs;
1965 struct fuse_copy_state cs;
Miklos Szeredicc080e92015-07-01 16:26:08 +02001966 struct fuse_dev *fud;
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001967 size_t rem;
1968 ssize_t ret;
1969
Miklos Szeredicc080e92015-07-01 16:26:08 +02001970 fud = fuse_get_dev(out);
1971 if (!fud)
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001972 return -EPERM;
1973
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001974 pipe_lock(pipe);
Andrey Ryabinin67a9e482018-07-17 19:00:33 +03001975
1976 bufs = kmalloc(pipe->buffers * sizeof(struct pipe_buffer), GFP_KERNEL);
1977 if (!bufs) {
1978 pipe_unlock(pipe);
1979 return -ENOMEM;
1980 }
1981
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001982 nbuf = 0;
1983 rem = 0;
1984 for (idx = 0; idx < pipe->nrbufs && rem < len; idx++)
1985 rem += pipe->bufs[(pipe->curbuf + idx) & (pipe->buffers - 1)].len;
1986
1987 ret = -EINVAL;
Matthew Wilcox95570902019-04-05 14:02:10 -07001988 if (rem < len)
1989 goto out_free;
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001990
1991 rem = len;
1992 while (rem) {
1993 struct pipe_buffer *ibuf;
1994 struct pipe_buffer *obuf;
1995
1996 BUG_ON(nbuf >= pipe->buffers);
1997 BUG_ON(!pipe->nrbufs);
1998 ibuf = &pipe->bufs[pipe->curbuf];
1999 obuf = &bufs[nbuf];
2000
2001 if (rem >= ibuf->len) {
2002 *obuf = *ibuf;
2003 ibuf->ops = NULL;
2004 pipe->curbuf = (pipe->curbuf + 1) & (pipe->buffers - 1);
2005 pipe->nrbufs--;
2006 } else {
Matthew Wilcox95570902019-04-05 14:02:10 -07002007 if (!pipe_buf_get(pipe, ibuf))
2008 goto out_free;
2009
Miklos Szeredidd3bb142010-05-25 15:06:06 +02002010 *obuf = *ibuf;
2011 obuf->flags &= ~PIPE_BUF_FLAG_GIFT;
2012 obuf->len = rem;
2013 ibuf->offset += obuf->len;
2014 ibuf->len -= obuf->len;
2015 }
2016 nbuf++;
2017 rem -= obuf->len;
2018 }
2019 pipe_unlock(pipe);
2020
Miklos Szeredidc008092015-07-01 16:25:58 +02002021 fuse_copy_init(&cs, 0, NULL);
Miklos Szeredidd3bb142010-05-25 15:06:06 +02002022 cs.pipebufs = bufs;
Al Viro6c09e942015-04-03 22:06:08 -04002023 cs.nr_segs = nbuf;
Miklos Szeredidd3bb142010-05-25 15:06:06 +02002024 cs.pipe = pipe;
2025
Miklos Szeredice534fb2010-05-25 15:06:07 +02002026 if (flags & SPLICE_F_MOVE)
2027 cs.move_pages = 1;
2028
Miklos Szeredic36960462015-07-01 16:26:09 +02002029 ret = fuse_dev_do_write(fud, &cs, len);
Miklos Szeredidd3bb142010-05-25 15:06:06 +02002030
Jann Horn50449aa2019-01-12 02:39:05 +01002031 pipe_lock(pipe);
Matthew Wilcox95570902019-04-05 14:02:10 -07002032out_free:
Miklos Szeredia7796382016-09-27 10:45:12 +02002033 for (idx = 0; idx < nbuf; idx++)
2034 pipe_buf_release(pipe, &bufs[idx]);
Jann Horn50449aa2019-01-12 02:39:05 +01002035 pipe_unlock(pipe);
Miklos Szeredia7796382016-09-27 10:45:12 +02002036
Miklos Szeredidd3bb142010-05-25 15:06:06 +02002037 kfree(bufs);
2038 return ret;
2039}
2040
Miklos Szeredi334f4852005-09-09 13:10:27 -07002041static unsigned fuse_dev_poll(struct file *file, poll_table *wait)
2042{
Miklos Szeredi334f4852005-09-09 13:10:27 -07002043 unsigned mask = POLLOUT | POLLWRNORM;
Miklos Szeredif88996a2015-07-01 16:26:01 +02002044 struct fuse_iqueue *fiq;
Miklos Szeredicc080e92015-07-01 16:26:08 +02002045 struct fuse_dev *fud = fuse_get_dev(file);
2046
2047 if (!fud)
Miklos Szeredi7025d9a2006-04-10 22:54:50 -07002048 return POLLERR;
Miklos Szeredi334f4852005-09-09 13:10:27 -07002049
Miklos Szeredicc080e92015-07-01 16:26:08 +02002050 fiq = &fud->fc->iq;
Miklos Szeredif88996a2015-07-01 16:26:01 +02002051 poll_wait(file, &fiq->waitq, wait);
Miklos Szeredi334f4852005-09-09 13:10:27 -07002052
Miklos Szeredi4ce60812015-07-01 16:26:02 +02002053 spin_lock(&fiq->waitq.lock);
Miklos Szeredie16714d2015-07-01 16:26:01 +02002054 if (!fiq->connected)
Miklos Szeredi7025d9a2006-04-10 22:54:50 -07002055 mask = POLLERR;
Miklos Szeredif88996a2015-07-01 16:26:01 +02002056 else if (request_pending(fiq))
Miklos Szeredi7025d9a2006-04-10 22:54:50 -07002057 mask |= POLLIN | POLLRDNORM;
Miklos Szeredi4ce60812015-07-01 16:26:02 +02002058 spin_unlock(&fiq->waitq.lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -07002059
2060 return mask;
2061}
2062
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002063/*
2064 * Abort all requests on the given list (pending or processing)
2065 *
Miklos Szeredid7133112006-04-10 22:54:55 -07002066 * This function releases and reacquires fc->lock
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002067 */
Miklos Szeredi334f4852005-09-09 13:10:27 -07002068static void end_requests(struct fuse_conn *fc, struct list_head *head)
2069{
2070 while (!list_empty(head)) {
2071 struct fuse_req *req;
2072 req = list_entry(head->next, struct fuse_req, list);
Miklos Szeredi334f4852005-09-09 13:10:27 -07002073 req->out.h.error = -ECONNABORTED;
Miklos Szeredi33e14b42015-07-01 16:26:01 +02002074 clear_bit(FR_SENT, &req->flags);
Miklos Szeredif377cb72015-07-01 16:26:04 +02002075 list_del_init(&req->list);
Miklos Szeredi334f4852005-09-09 13:10:27 -07002076 request_end(fc, req);
Miklos Szeredi334f4852005-09-09 13:10:27 -07002077 }
2078}
2079
Bryan Green357ccf22011-03-01 16:43:52 -08002080static void end_polls(struct fuse_conn *fc)
2081{
2082 struct rb_node *p;
2083
2084 p = rb_first(&fc->polled_files);
2085
2086 while (p) {
2087 struct fuse_file *ff;
2088 ff = rb_entry(p, struct fuse_file, polled_node);
2089 wake_up_interruptible_all(&ff->poll_wait);
2090
2091 p = rb_next(p);
2092 }
2093}
2094
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002095/*
2096 * Abort all requests.
2097 *
Miklos Szeredib716d422015-07-01 16:25:59 +02002098 * Emergency exit in case of a malicious or accidental deadlock, or just a hung
2099 * filesystem.
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002100 *
Miklos Szeredib716d422015-07-01 16:25:59 +02002101 * The same effect is usually achievable through killing the filesystem daemon
2102 * and all users of the filesystem. The exception is the combination of an
2103 * asynchronous request and the tricky deadlock (see
2104 * Documentation/filesystems/fuse.txt).
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002105 *
Miklos Szeredib716d422015-07-01 16:25:59 +02002106 * Aborting requests under I/O goes as follows: 1: Separate out unlocked
2107 * requests, they should be finished off immediately. Locked requests will be
2108 * finished after unlock; see unlock_request(). 2: Finish off the unlocked
2109 * requests. It is possible that some request will finish before we can. This
2110 * is OK, the request will in that case be removed from the list before we touch
2111 * it.
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002112 */
2113void fuse_abort_conn(struct fuse_conn *fc)
2114{
Miklos Szeredif88996a2015-07-01 16:26:01 +02002115 struct fuse_iqueue *fiq = &fc->iq;
2116
Miklos Szeredid7133112006-04-10 22:54:55 -07002117 spin_lock(&fc->lock);
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002118 if (fc->connected) {
Miklos Szeredic36960462015-07-01 16:26:09 +02002119 struct fuse_dev *fud;
Miklos Szeredib716d422015-07-01 16:25:59 +02002120 struct fuse_req *req, *next;
Miklos Szeredi41f98272015-07-01 16:25:59 +02002121 LIST_HEAD(to_end1);
2122 LIST_HEAD(to_end2);
Miklos Szeredib716d422015-07-01 16:25:59 +02002123
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002124 fc->connected = 0;
Miklos Szeredi51eb01e2006-06-25 05:48:50 -07002125 fc->blocked = 0;
Miklos Szeredi9759bd512015-01-06 10:45:35 +01002126 fuse_set_initialized(fc);
Miklos Szeredic36960462015-07-01 16:26:09 +02002127 list_for_each_entry(fud, &fc->devices, entry) {
2128 struct fuse_pqueue *fpq = &fud->pq;
2129
2130 spin_lock(&fpq->lock);
2131 fpq->connected = 0;
2132 list_for_each_entry_safe(req, next, &fpq->io, list) {
2133 req->out.h.error = -ECONNABORTED;
2134 spin_lock(&req->waitq.lock);
2135 set_bit(FR_ABORTED, &req->flags);
2136 if (!test_bit(FR_LOCKED, &req->flags)) {
2137 set_bit(FR_PRIVATE, &req->flags);
Miklos Szeredi501c4ca2018-07-26 16:13:11 +02002138 __fuse_get_request(req);
Miklos Szeredic36960462015-07-01 16:26:09 +02002139 list_move(&req->list, &to_end1);
2140 }
2141 spin_unlock(&req->waitq.lock);
Miklos Szeredi77cd9d42015-07-01 16:26:06 +02002142 }
Miklos Szeredic36960462015-07-01 16:26:09 +02002143 list_splice_init(&fpq->processing, &to_end2);
2144 spin_unlock(&fpq->lock);
Miklos Szeredib716d422015-07-01 16:25:59 +02002145 }
Miklos Szeredi41f98272015-07-01 16:25:59 +02002146 fc->max_background = UINT_MAX;
2147 flush_bg_queue(fc);
Miklos Szeredi8c911892015-07-01 16:26:02 +02002148
Miklos Szeredi4ce60812015-07-01 16:26:02 +02002149 spin_lock(&fiq->waitq.lock);
Miklos Szeredi8c911892015-07-01 16:26:02 +02002150 fiq->connected = 0;
Miklos Szeredif88996a2015-07-01 16:26:01 +02002151 list_splice_init(&fiq->pending, &to_end2);
Tahsin Erdogan0181b362017-01-12 12:04:04 -08002152 list_for_each_entry(req, &to_end2, list)
2153 clear_bit(FR_PENDING, &req->flags);
Miklos Szeredi8c911892015-07-01 16:26:02 +02002154 while (forget_pending(fiq))
2155 kfree(dequeue_forget(fiq, 1, NULL));
Miklos Szeredi4ce60812015-07-01 16:26:02 +02002156 wake_up_all_locked(&fiq->waitq);
2157 spin_unlock(&fiq->waitq.lock);
Miklos Szeredi8c911892015-07-01 16:26:02 +02002158 kill_fasync(&fiq->fasync, SIGIO, POLL_IN);
Miklos Szerediee314a82015-07-01 16:26:08 +02002159 end_polls(fc);
2160 wake_up_all(&fc->blocked_waitq);
2161 spin_unlock(&fc->lock);
Miklos Szeredi8c911892015-07-01 16:26:02 +02002162
Miklos Szeredi41f98272015-07-01 16:25:59 +02002163 while (!list_empty(&to_end1)) {
2164 req = list_first_entry(&to_end1, struct fuse_req, list);
Miklos Szeredif377cb72015-07-01 16:26:04 +02002165 list_del_init(&req->list);
Miklos Szeredib716d422015-07-01 16:25:59 +02002166 request_end(fc, req);
Miklos Szeredib716d422015-07-01 16:25:59 +02002167 }
Miklos Szeredi41f98272015-07-01 16:25:59 +02002168 end_requests(fc, &to_end2);
Miklos Szerediee314a82015-07-01 16:26:08 +02002169 } else {
2170 spin_unlock(&fc->lock);
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002171 }
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002172}
Tejun Heo08cbf542009-04-14 10:54:53 +09002173EXPORT_SYMBOL_GPL(fuse_abort_conn);
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002174
Miklos Szeredi6465d762018-07-26 16:13:11 +02002175void fuse_wait_aborted(struct fuse_conn *fc)
2176{
2177 wait_event(fc->blocked_waitq, atomic_read(&fc->num_waiting) == 0);
2178}
2179
Tejun Heo08cbf542009-04-14 10:54:53 +09002180int fuse_dev_release(struct inode *inode, struct file *file)
Miklos Szeredi334f4852005-09-09 13:10:27 -07002181{
Miklos Szeredicc080e92015-07-01 16:26:08 +02002182 struct fuse_dev *fud = fuse_get_dev(file);
2183
2184 if (fud) {
2185 struct fuse_conn *fc = fud->fc;
Miklos Szeredic36960462015-07-01 16:26:09 +02002186 struct fuse_pqueue *fpq = &fud->pq;
Miklos Szeredic66025c2018-07-26 16:13:11 +02002187 LIST_HEAD(to_end);
Miklos Szeredicc080e92015-07-01 16:26:08 +02002188
Miklos Szeredic66025c2018-07-26 16:13:11 +02002189 spin_lock(&fpq->lock);
Miklos Szeredic36960462015-07-01 16:26:09 +02002190 WARN_ON(!list_empty(&fpq->io));
Miklos Szeredic66025c2018-07-26 16:13:11 +02002191 list_splice_init(&fpq->processing, &to_end);
2192 spin_unlock(&fpq->lock);
2193
2194 end_requests(fc, &to_end);
2195
Miklos Szeredic36960462015-07-01 16:26:09 +02002196 /* Are we the last open device? */
2197 if (atomic_dec_and_test(&fc->dev_count)) {
2198 WARN_ON(fc->iq.fasync != NULL);
2199 fuse_abort_conn(fc);
2200 }
Miklos Szeredicc080e92015-07-01 16:26:08 +02002201 fuse_dev_free(fud);
Jeff Dike385a17b2006-04-10 22:54:52 -07002202 }
Miklos Szeredi334f4852005-09-09 13:10:27 -07002203 return 0;
2204}
Tejun Heo08cbf542009-04-14 10:54:53 +09002205EXPORT_SYMBOL_GPL(fuse_dev_release);
Miklos Szeredi334f4852005-09-09 13:10:27 -07002206
Jeff Dike385a17b2006-04-10 22:54:52 -07002207static int fuse_dev_fasync(int fd, struct file *file, int on)
2208{
Miklos Szeredicc080e92015-07-01 16:26:08 +02002209 struct fuse_dev *fud = fuse_get_dev(file);
2210
2211 if (!fud)
Miklos Szeredia87046d2006-04-10 22:54:56 -07002212 return -EPERM;
Jeff Dike385a17b2006-04-10 22:54:52 -07002213
2214 /* No locking - fasync_helper does its own locking */
Miklos Szeredicc080e92015-07-01 16:26:08 +02002215 return fasync_helper(fd, file, on, &fud->fc->iq.fasync);
Jeff Dike385a17b2006-04-10 22:54:52 -07002216}
2217
Miklos Szeredi00c570f2015-07-01 16:26:08 +02002218static int fuse_device_clone(struct fuse_conn *fc, struct file *new)
2219{
Miklos Szeredicc080e92015-07-01 16:26:08 +02002220 struct fuse_dev *fud;
2221
Miklos Szeredi00c570f2015-07-01 16:26:08 +02002222 if (new->private_data)
2223 return -EINVAL;
2224
Miklos Szeredicc080e92015-07-01 16:26:08 +02002225 fud = fuse_dev_alloc(fc);
2226 if (!fud)
2227 return -ENOMEM;
2228
2229 new->private_data = fud;
Miklos Szeredic36960462015-07-01 16:26:09 +02002230 atomic_inc(&fc->dev_count);
Miklos Szeredi00c570f2015-07-01 16:26:08 +02002231
2232 return 0;
2233}
2234
2235static long fuse_dev_ioctl(struct file *file, unsigned int cmd,
2236 unsigned long arg)
2237{
2238 int err = -ENOTTY;
2239
2240 if (cmd == FUSE_DEV_IOC_CLONE) {
2241 int oldfd;
2242
2243 err = -EFAULT;
2244 if (!get_user(oldfd, (__u32 __user *) arg)) {
2245 struct file *old = fget(oldfd);
2246
2247 err = -EINVAL;
2248 if (old) {
Jann Horn8ed1f0e2015-08-16 20:27:01 +02002249 struct fuse_dev *fud = NULL;
2250
2251 /*
2252 * Check against file->f_op because CUSE
2253 * uses the same ioctl handler.
2254 */
2255 if (old->f_op == file->f_op &&
2256 old->f_cred->user_ns == file->f_cred->user_ns)
2257 fud = fuse_get_dev(old);
Miklos Szeredi00c570f2015-07-01 16:26:08 +02002258
Miklos Szeredicc080e92015-07-01 16:26:08 +02002259 if (fud) {
Miklos Szeredi00c570f2015-07-01 16:26:08 +02002260 mutex_lock(&fuse_mutex);
Miklos Szeredicc080e92015-07-01 16:26:08 +02002261 err = fuse_device_clone(fud->fc, file);
Miklos Szeredi00c570f2015-07-01 16:26:08 +02002262 mutex_unlock(&fuse_mutex);
2263 }
2264 fput(old);
2265 }
2266 }
2267 }
2268 return err;
2269}
2270
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -08002271const struct file_operations fuse_dev_operations = {
Miklos Szeredi334f4852005-09-09 13:10:27 -07002272 .owner = THIS_MODULE,
Tom Van Braeckel94e4fe22015-01-12 05:22:16 +01002273 .open = fuse_dev_open,
Miklos Szeredi334f4852005-09-09 13:10:27 -07002274 .llseek = no_llseek,
Al Virofbdbacc2015-04-03 21:53:39 -04002275 .read_iter = fuse_dev_read,
Miklos Szeredic3021622010-05-25 15:06:07 +02002276 .splice_read = fuse_dev_splice_read,
Al Virofbdbacc2015-04-03 21:53:39 -04002277 .write_iter = fuse_dev_write,
Miklos Szeredidd3bb142010-05-25 15:06:06 +02002278 .splice_write = fuse_dev_splice_write,
Miklos Szeredi334f4852005-09-09 13:10:27 -07002279 .poll = fuse_dev_poll,
2280 .release = fuse_dev_release,
Jeff Dike385a17b2006-04-10 22:54:52 -07002281 .fasync = fuse_dev_fasync,
Miklos Szeredi00c570f2015-07-01 16:26:08 +02002282 .unlocked_ioctl = fuse_dev_ioctl,
2283 .compat_ioctl = fuse_dev_ioctl,
Miklos Szeredi334f4852005-09-09 13:10:27 -07002284};
Tejun Heo08cbf542009-04-14 10:54:53 +09002285EXPORT_SYMBOL_GPL(fuse_dev_operations);
Miklos Szeredi334f4852005-09-09 13:10:27 -07002286
2287static struct miscdevice fuse_miscdevice = {
2288 .minor = FUSE_MINOR,
2289 .name = "fuse",
2290 .fops = &fuse_dev_operations,
2291};
2292
2293int __init fuse_dev_init(void)
2294{
2295 int err = -ENOMEM;
2296 fuse_req_cachep = kmem_cache_create("fuse_request",
2297 sizeof(struct fuse_req),
Paul Mundt20c2df82007-07-20 10:11:58 +09002298 0, 0, NULL);
Miklos Szeredi334f4852005-09-09 13:10:27 -07002299 if (!fuse_req_cachep)
2300 goto out;
2301
2302 err = misc_register(&fuse_miscdevice);
2303 if (err)
2304 goto out_cache_clean;
2305
2306 return 0;
2307
2308 out_cache_clean:
2309 kmem_cache_destroy(fuse_req_cachep);
2310 out:
2311 return err;
2312}
2313
2314void fuse_dev_cleanup(void)
2315{
2316 misc_deregister(&fuse_miscdevice);
2317 kmem_cache_destroy(fuse_req_cachep);
2318}