blob: 6ee471b72a34da2b9057bf3a3d993bab458115f5 [file] [log] [blame]
Miklos Szeredi334f4852005-09-09 13:10:27 -07001/*
2 FUSE: Filesystem in Userspace
Miklos Szeredi1729a162008-11-26 12:03:54 +01003 Copyright (C) 2001-2008 Miklos Szeredi <miklos@szeredi.hu>
Miklos Szeredi334f4852005-09-09 13:10:27 -07004
5 This program can be distributed under the terms of the GNU GPL.
6 See the file COPYING.
7*/
8
9#include "fuse_i.h"
10
11#include <linux/init.h>
12#include <linux/module.h>
13#include <linux/poll.h>
Ingo Molnar174cd4b2017-02-02 19:15:33 +010014#include <linux/sched/signal.h>
Miklos Szeredi334f4852005-09-09 13:10:27 -070015#include <linux/uio.h>
16#include <linux/miscdevice.h>
17#include <linux/pagemap.h>
18#include <linux/file.h>
19#include <linux/slab.h>
Miklos Szeredidd3bb142010-05-25 15:06:06 +020020#include <linux/pipe_fs_i.h>
Miklos Szeredice534fb2010-05-25 15:06:07 +020021#include <linux/swap.h>
22#include <linux/splice.h>
Seth Forshee0b6e9ea2014-07-02 16:29:19 -050023#include <linux/sched.h>
Miklos Szeredi334f4852005-09-09 13:10:27 -070024
25MODULE_ALIAS_MISCDEV(FUSE_MINOR);
Kay Sievers578454f2010-05-20 18:07:20 +020026MODULE_ALIAS("devname:fuse");
Miklos Szeredi334f4852005-09-09 13:10:27 -070027
Christoph Lametere18b8902006-12-06 20:33:20 -080028static struct kmem_cache *fuse_req_cachep;
Miklos Szeredi334f4852005-09-09 13:10:27 -070029
Miklos Szeredicc080e92015-07-01 16:26:08 +020030static struct fuse_dev *fuse_get_dev(struct file *file)
Miklos Szeredi334f4852005-09-09 13:10:27 -070031{
Miklos Szeredi0720b312006-04-10 22:54:55 -070032 /*
33 * Lockless access is OK, because file->private data is set
34 * once during mount and is valid until the file is released.
35 */
Mark Rutland6aa7de02017-10-23 14:07:29 -070036 return READ_ONCE(file->private_data);
Miklos Szeredi334f4852005-09-09 13:10:27 -070037}
38
Maxim Patlasov4250c062012-10-26 19:48:07 +040039static void fuse_request_init(struct fuse_req *req, struct page **pages,
Maxim Patlasovb2430d72012-10-26 19:49:24 +040040 struct fuse_page_desc *page_descs,
Maxim Patlasov4250c062012-10-26 19:48:07 +040041 unsigned npages)
Miklos Szeredi334f4852005-09-09 13:10:27 -070042{
43 memset(req, 0, sizeof(*req));
Maxim Patlasov4250c062012-10-26 19:48:07 +040044 memset(pages, 0, sizeof(*pages) * npages);
Maxim Patlasovb2430d72012-10-26 19:49:24 +040045 memset(page_descs, 0, sizeof(*page_descs) * npages);
Miklos Szeredi334f4852005-09-09 13:10:27 -070046 INIT_LIST_HEAD(&req->list);
Miklos Szeredia4d27e72006-06-25 05:48:54 -070047 INIT_LIST_HEAD(&req->intr_entry);
Miklos Szeredi334f4852005-09-09 13:10:27 -070048 init_waitqueue_head(&req->waitq);
Elena Reshetovaec99f6d2017-03-03 11:04:04 +020049 refcount_set(&req->count, 1);
Maxim Patlasov4250c062012-10-26 19:48:07 +040050 req->pages = pages;
Maxim Patlasovb2430d72012-10-26 19:49:24 +040051 req->page_descs = page_descs;
Maxim Patlasov4250c062012-10-26 19:48:07 +040052 req->max_pages = npages;
Miklos Szeredi33e14b42015-07-01 16:26:01 +020053 __set_bit(FR_PENDING, &req->flags);
Miklos Szeredi334f4852005-09-09 13:10:27 -070054}
55
Maxim Patlasov4250c062012-10-26 19:48:07 +040056static struct fuse_req *__fuse_request_alloc(unsigned npages, gfp_t flags)
Miklos Szeredi334f4852005-09-09 13:10:27 -070057{
Maxim Patlasov4250c062012-10-26 19:48:07 +040058 struct fuse_req *req = kmem_cache_alloc(fuse_req_cachep, flags);
59 if (req) {
60 struct page **pages;
Maxim Patlasovb2430d72012-10-26 19:49:24 +040061 struct fuse_page_desc *page_descs;
Maxim Patlasov4250c062012-10-26 19:48:07 +040062
Maxim Patlasovb2430d72012-10-26 19:49:24 +040063 if (npages <= FUSE_REQ_INLINE_PAGES) {
Maxim Patlasov4250c062012-10-26 19:48:07 +040064 pages = req->inline_pages;
Maxim Patlasovb2430d72012-10-26 19:49:24 +040065 page_descs = req->inline_page_descs;
66 } else {
Kees Cook6da2ec52018-06-12 13:55:00 -070067 pages = kmalloc_array(npages, sizeof(struct page *),
68 flags);
69 page_descs =
70 kmalloc_array(npages,
71 sizeof(struct fuse_page_desc),
72 flags);
Maxim Patlasovb2430d72012-10-26 19:49:24 +040073 }
Maxim Patlasov4250c062012-10-26 19:48:07 +040074
Maxim Patlasovb2430d72012-10-26 19:49:24 +040075 if (!pages || !page_descs) {
76 kfree(pages);
77 kfree(page_descs);
Maxim Patlasov4250c062012-10-26 19:48:07 +040078 kmem_cache_free(fuse_req_cachep, req);
79 return NULL;
80 }
81
Maxim Patlasovb2430d72012-10-26 19:49:24 +040082 fuse_request_init(req, pages, page_descs, npages);
Maxim Patlasov4250c062012-10-26 19:48:07 +040083 }
Miklos Szeredi334f4852005-09-09 13:10:27 -070084 return req;
85}
Maxim Patlasov4250c062012-10-26 19:48:07 +040086
87struct fuse_req *fuse_request_alloc(unsigned npages)
88{
89 return __fuse_request_alloc(npages, GFP_KERNEL);
90}
Tejun Heo08cbf542009-04-14 10:54:53 +090091EXPORT_SYMBOL_GPL(fuse_request_alloc);
Miklos Szeredi334f4852005-09-09 13:10:27 -070092
Maxim Patlasov4250c062012-10-26 19:48:07 +040093struct fuse_req *fuse_request_alloc_nofs(unsigned npages)
Miklos Szeredi3be5a522008-04-30 00:54:41 -070094{
Maxim Patlasov4250c062012-10-26 19:48:07 +040095 return __fuse_request_alloc(npages, GFP_NOFS);
Miklos Szeredi3be5a522008-04-30 00:54:41 -070096}
97
Miklos Szeredi334f4852005-09-09 13:10:27 -070098void fuse_request_free(struct fuse_req *req)
99{
Maxim Patlasovb2430d72012-10-26 19:49:24 +0400100 if (req->pages != req->inline_pages) {
Maxim Patlasov4250c062012-10-26 19:48:07 +0400101 kfree(req->pages);
Maxim Patlasovb2430d72012-10-26 19:49:24 +0400102 kfree(req->page_descs);
103 }
Miklos Szeredi334f4852005-09-09 13:10:27 -0700104 kmem_cache_free(fuse_req_cachep, req);
105}
106
Maxim Patlasov36cf66e2012-12-14 19:20:51 +0400107void __fuse_get_request(struct fuse_req *req)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700108{
Elena Reshetovaec99f6d2017-03-03 11:04:04 +0200109 refcount_inc(&req->count);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700110}
111
112/* Must be called with > 1 refcount */
113static void __fuse_put_request(struct fuse_req *req)
114{
Elena Reshetovaec99f6d2017-03-03 11:04:04 +0200115 refcount_dec(&req->count);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700116}
117
Miklos Szeredi9759bd512015-01-06 10:45:35 +0100118void fuse_set_initialized(struct fuse_conn *fc)
119{
120 /* Make sure stores before this are seen on another CPU */
121 smp_wmb();
122 fc->initialized = 1;
123}
124
Maxim Patlasov0aada882013-03-21 18:02:28 +0400125static bool fuse_block_alloc(struct fuse_conn *fc, bool for_background)
126{
127 return !fc->initialized || (for_background && fc->blocked);
128}
129
Miklos Szeredib8f95e52018-07-26 16:13:11 +0200130static void fuse_drop_waiting(struct fuse_conn *fc)
131{
Miklos Szeredi18cd6102018-11-09 15:52:16 +0100132 /*
133 * lockess check of fc->connected is okay, because atomic_dec_and_test()
134 * provides a memory barrier mached with the one in fuse_wait_aborted()
135 * to ensure no wake-up is missed.
136 */
137 if (atomic_dec_and_test(&fc->num_waiting) &&
138 !READ_ONCE(fc->connected)) {
Miklos Szeredib8f95e52018-07-26 16:13:11 +0200139 /* wake up aborters */
140 wake_up_all(&fc->blocked_waitq);
141 }
142}
143
Maxim Patlasov8b41e672013-03-21 18:02:04 +0400144static struct fuse_req *__fuse_get_req(struct fuse_conn *fc, unsigned npages,
145 bool for_background)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700146{
Miklos Szeredi08a53cd2006-04-10 22:54:59 -0700147 struct fuse_req *req;
Miklos Szeredi08a53cd2006-04-10 22:54:59 -0700148 int err;
Miklos Szeredi9bc5ddd2006-04-11 21:16:09 +0200149 atomic_inc(&fc->num_waiting);
Maxim Patlasov0aada882013-03-21 18:02:28 +0400150
151 if (fuse_block_alloc(fc, for_background)) {
Maxim Patlasov0aada882013-03-21 18:02:28 +0400152 err = -EINTR;
Al Viro7d3a07f2016-07-19 03:08:27 -0400153 if (wait_event_killable_exclusive(fc->blocked_waitq,
154 !fuse_block_alloc(fc, for_background)))
Maxim Patlasov0aada882013-03-21 18:02:28 +0400155 goto out;
156 }
Miklos Szeredi9759bd512015-01-06 10:45:35 +0100157 /* Matches smp_wmb() in fuse_set_initialized() */
158 smp_rmb();
Miklos Szeredi08a53cd2006-04-10 22:54:59 -0700159
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700160 err = -ENOTCONN;
161 if (!fc->connected)
162 goto out;
163
Miklos Szeredide155222015-07-01 16:25:57 +0200164 err = -ECONNREFUSED;
165 if (fc->conn_error)
166 goto out;
167
Maxim Patlasovb111c8c2012-10-26 19:48:30 +0400168 req = fuse_request_alloc(npages);
Miklos Szeredi9bc5ddd2006-04-11 21:16:09 +0200169 err = -ENOMEM;
Maxim Patlasov722d2be2013-03-21 18:02:36 +0400170 if (!req) {
171 if (for_background)
172 wake_up(&fc->blocked_waitq);
Miklos Szeredi9bc5ddd2006-04-11 21:16:09 +0200173 goto out;
Maxim Patlasov722d2be2013-03-21 18:02:36 +0400174 }
Miklos Szeredi334f4852005-09-09 13:10:27 -0700175
Eric W. Biederman8cb08322018-02-21 11:18:07 -0600176 req->in.h.uid = from_kuid(fc->user_ns, current_fsuid());
177 req->in.h.gid = from_kgid(fc->user_ns, current_fsgid());
Eric W. Biedermanc9582eb2018-02-21 10:52:06 -0600178 req->in.h.pid = pid_nr_ns(task_pid(current), fc->pid_ns);
179
Miklos Szeredi825d6d32015-07-01 16:25:58 +0200180 __set_bit(FR_WAITING, &req->flags);
181 if (for_background)
182 __set_bit(FR_BACKGROUND, &req->flags);
183
Eric W. Biedermanc9582eb2018-02-21 10:52:06 -0600184 if (unlikely(req->in.h.uid == ((uid_t)-1) ||
185 req->in.h.gid == ((gid_t)-1))) {
186 fuse_put_request(fc, req);
187 return ERR_PTR(-EOVERFLOW);
188 }
Miklos Szeredi334f4852005-09-09 13:10:27 -0700189 return req;
Miklos Szeredi9bc5ddd2006-04-11 21:16:09 +0200190
191 out:
Miklos Szeredib8f95e52018-07-26 16:13:11 +0200192 fuse_drop_waiting(fc);
Miklos Szeredi9bc5ddd2006-04-11 21:16:09 +0200193 return ERR_PTR(err);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700194}
Maxim Patlasov8b41e672013-03-21 18:02:04 +0400195
196struct fuse_req *fuse_get_req(struct fuse_conn *fc, unsigned npages)
197{
198 return __fuse_get_req(fc, npages, false);
199}
Tejun Heo08cbf542009-04-14 10:54:53 +0900200EXPORT_SYMBOL_GPL(fuse_get_req);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700201
Maxim Patlasov8b41e672013-03-21 18:02:04 +0400202struct fuse_req *fuse_get_req_for_background(struct fuse_conn *fc,
203 unsigned npages)
204{
205 return __fuse_get_req(fc, npages, true);
206}
207EXPORT_SYMBOL_GPL(fuse_get_req_for_background);
208
Miklos Szeredi33649c92006-06-25 05:48:52 -0700209/*
210 * Return request in fuse_file->reserved_req. However that may
211 * currently be in use. If that is the case, wait for it to become
212 * available.
213 */
214static struct fuse_req *get_reserved_req(struct fuse_conn *fc,
215 struct file *file)
216{
217 struct fuse_req *req = NULL;
218 struct fuse_file *ff = file->private_data;
219
220 do {
Miklos Szeredide5e3de2007-10-16 23:31:00 -0700221 wait_event(fc->reserved_req_waitq, ff->reserved_req);
Miklos Szeredi33649c92006-06-25 05:48:52 -0700222 spin_lock(&fc->lock);
223 if (ff->reserved_req) {
224 req = ff->reserved_req;
225 ff->reserved_req = NULL;
Al Virocb0942b2012-08-27 14:48:26 -0400226 req->stolen_file = get_file(file);
Miklos Szeredi33649c92006-06-25 05:48:52 -0700227 }
228 spin_unlock(&fc->lock);
229 } while (!req);
230
231 return req;
232}
233
234/*
235 * Put stolen request back into fuse_file->reserved_req
236 */
237static void put_reserved_req(struct fuse_conn *fc, struct fuse_req *req)
238{
239 struct file *file = req->stolen_file;
240 struct fuse_file *ff = file->private_data;
241
242 spin_lock(&fc->lock);
Maxim Patlasovb2430d72012-10-26 19:49:24 +0400243 fuse_request_init(req, req->pages, req->page_descs, req->max_pages);
Miklos Szeredi33649c92006-06-25 05:48:52 -0700244 BUG_ON(ff->reserved_req);
245 ff->reserved_req = req;
Miklos Szeredide5e3de2007-10-16 23:31:00 -0700246 wake_up_all(&fc->reserved_req_waitq);
Miklos Szeredi33649c92006-06-25 05:48:52 -0700247 spin_unlock(&fc->lock);
248 fput(file);
249}
250
251/*
252 * Gets a requests for a file operation, always succeeds
253 *
254 * This is used for sending the FLUSH request, which must get to
255 * userspace, due to POSIX locks which may need to be unlocked.
256 *
257 * If allocation fails due to OOM, use the reserved request in
258 * fuse_file.
259 *
260 * This is very unlikely to deadlock accidentally, since the
261 * filesystem should not have it's own file open. If deadlock is
262 * intentional, it can still be broken by "aborting" the filesystem.
263 */
Maxim Patlasovb111c8c2012-10-26 19:48:30 +0400264struct fuse_req *fuse_get_req_nofail_nopages(struct fuse_conn *fc,
265 struct file *file)
Miklos Szeredi33649c92006-06-25 05:48:52 -0700266{
267 struct fuse_req *req;
268
269 atomic_inc(&fc->num_waiting);
Maxim Patlasov0aada882013-03-21 18:02:28 +0400270 wait_event(fc->blocked_waitq, fc->initialized);
Miklos Szeredi9759bd512015-01-06 10:45:35 +0100271 /* Matches smp_wmb() in fuse_set_initialized() */
272 smp_rmb();
Maxim Patlasovb111c8c2012-10-26 19:48:30 +0400273 req = fuse_request_alloc(0);
Miklos Szeredi33649c92006-06-25 05:48:52 -0700274 if (!req)
275 req = get_reserved_req(fc, file);
276
Eric W. Biederman8cb08322018-02-21 11:18:07 -0600277 req->in.h.uid = from_kuid_munged(fc->user_ns, current_fsuid());
278 req->in.h.gid = from_kgid_munged(fc->user_ns, current_fsgid());
Eric W. Biedermanc9582eb2018-02-21 10:52:06 -0600279 req->in.h.pid = pid_nr_ns(task_pid(current), fc->pid_ns);
280
Miklos Szeredi825d6d32015-07-01 16:25:58 +0200281 __set_bit(FR_WAITING, &req->flags);
282 __clear_bit(FR_BACKGROUND, &req->flags);
Miklos Szeredi33649c92006-06-25 05:48:52 -0700283 return req;
284}
285
Miklos Szeredi334f4852005-09-09 13:10:27 -0700286void fuse_put_request(struct fuse_conn *fc, struct fuse_req *req)
287{
Elena Reshetovaec99f6d2017-03-03 11:04:04 +0200288 if (refcount_dec_and_test(&req->count)) {
Miklos Szeredi825d6d32015-07-01 16:25:58 +0200289 if (test_bit(FR_BACKGROUND, &req->flags)) {
Maxim Patlasov722d2be2013-03-21 18:02:36 +0400290 /*
291 * We get here in the unlikely case that a background
292 * request was allocated but not sent
293 */
294 spin_lock(&fc->lock);
295 if (!fc->blocked)
296 wake_up(&fc->blocked_waitq);
297 spin_unlock(&fc->lock);
298 }
299
Miklos Szeredi825d6d32015-07-01 16:25:58 +0200300 if (test_bit(FR_WAITING, &req->flags)) {
301 __clear_bit(FR_WAITING, &req->flags);
Miklos Szeredib8f95e52018-07-26 16:13:11 +0200302 fuse_drop_waiting(fc);
Miklos Szeredi73e0e732015-07-01 16:25:56 +0200303 }
Miklos Szeredi33649c92006-06-25 05:48:52 -0700304
305 if (req->stolen_file)
306 put_reserved_req(fc, req);
307 else
308 fuse_request_free(req);
Miklos Szeredi7128ec22006-02-04 23:27:40 -0800309 }
310}
Tejun Heo08cbf542009-04-14 10:54:53 +0900311EXPORT_SYMBOL_GPL(fuse_put_request);
Miklos Szeredi7128ec22006-02-04 23:27:40 -0800312
Miklos Szeredid12def12008-02-06 01:38:39 -0800313static unsigned len_args(unsigned numargs, struct fuse_arg *args)
314{
315 unsigned nbytes = 0;
316 unsigned i;
317
318 for (i = 0; i < numargs; i++)
319 nbytes += args[i].size;
320
321 return nbytes;
322}
323
Miklos Szeredif88996a2015-07-01 16:26:01 +0200324static u64 fuse_get_unique(struct fuse_iqueue *fiq)
Miklos Szeredid12def12008-02-06 01:38:39 -0800325{
Miklos Szeredif88996a2015-07-01 16:26:01 +0200326 return ++fiq->reqctr;
Miklos Szeredid12def12008-02-06 01:38:39 -0800327}
328
Miklos Szeredif88996a2015-07-01 16:26:01 +0200329static void queue_request(struct fuse_iqueue *fiq, struct fuse_req *req)
Miklos Szeredid12def12008-02-06 01:38:39 -0800330{
Miklos Szeredid12def12008-02-06 01:38:39 -0800331 req->in.h.len = sizeof(struct fuse_in_header) +
332 len_args(req->in.numargs, (struct fuse_arg *) req->in.args);
Miklos Szeredif88996a2015-07-01 16:26:01 +0200333 list_add_tail(&req->list, &fiq->pending);
Miklos Szeredi4ce60812015-07-01 16:26:02 +0200334 wake_up_locked(&fiq->waitq);
Miklos Szeredif88996a2015-07-01 16:26:01 +0200335 kill_fasync(&fiq->fasync, SIGIO, POLL_IN);
Miklos Szeredid12def12008-02-06 01:38:39 -0800336}
337
Miklos Szeredi07e77dc2010-12-07 20:16:56 +0100338void fuse_queue_forget(struct fuse_conn *fc, struct fuse_forget_link *forget,
339 u64 nodeid, u64 nlookup)
340{
Miklos Szeredif88996a2015-07-01 16:26:01 +0200341 struct fuse_iqueue *fiq = &fc->iq;
342
Miklos Szeredi02c048b2010-12-07 20:16:56 +0100343 forget->forget_one.nodeid = nodeid;
344 forget->forget_one.nlookup = nlookup;
Miklos Szeredi07e77dc2010-12-07 20:16:56 +0100345
Miklos Szeredi4ce60812015-07-01 16:26:02 +0200346 spin_lock(&fiq->waitq.lock);
Miklos Szeredie16714d2015-07-01 16:26:01 +0200347 if (fiq->connected) {
Miklos Szeredif88996a2015-07-01 16:26:01 +0200348 fiq->forget_list_tail->next = forget;
349 fiq->forget_list_tail = forget;
Miklos Szeredi4ce60812015-07-01 16:26:02 +0200350 wake_up_locked(&fiq->waitq);
Miklos Szeredif88996a2015-07-01 16:26:01 +0200351 kill_fasync(&fiq->fasync, SIGIO, POLL_IN);
Miklos Szeredi5dfcc872011-09-12 09:38:03 +0200352 } else {
353 kfree(forget);
354 }
Miklos Szeredi4ce60812015-07-01 16:26:02 +0200355 spin_unlock(&fiq->waitq.lock);
Miklos Szeredi07e77dc2010-12-07 20:16:56 +0100356}
357
Miklos Szeredid12def12008-02-06 01:38:39 -0800358static void flush_bg_queue(struct fuse_conn *fc)
359{
Csaba Henk7a6d3c82009-07-01 17:28:41 -0700360 while (fc->active_background < fc->max_background &&
Miklos Szeredid12def12008-02-06 01:38:39 -0800361 !list_empty(&fc->bg_queue)) {
362 struct fuse_req *req;
Miklos Szeredif88996a2015-07-01 16:26:01 +0200363 struct fuse_iqueue *fiq = &fc->iq;
Miklos Szeredid12def12008-02-06 01:38:39 -0800364
365 req = list_entry(fc->bg_queue.next, struct fuse_req, list);
366 list_del(&req->list);
367 fc->active_background++;
Miklos Szeredi4ce60812015-07-01 16:26:02 +0200368 spin_lock(&fiq->waitq.lock);
Miklos Szeredif88996a2015-07-01 16:26:01 +0200369 req->in.h.unique = fuse_get_unique(fiq);
370 queue_request(fiq, req);
Miklos Szeredi4ce60812015-07-01 16:26:02 +0200371 spin_unlock(&fiq->waitq.lock);
Miklos Szeredid12def12008-02-06 01:38:39 -0800372 }
373}
374
Miklos Szeredi6dbbcb12006-04-26 10:49:06 +0200375/*
Miklos Szeredi334f4852005-09-09 13:10:27 -0700376 * This function is called when a request is finished. Either a reply
Miklos Szeredif9a28422006-06-25 05:48:53 -0700377 * has arrived or it was aborted (and not yet sent) or some error
Miklos Szeredif43b1552006-01-16 22:14:26 -0800378 * occurred during communication with userspace, or the device file
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700379 * was closed. The requester thread is woken up (if still waiting),
380 * the 'end' callback is called if given, else the reference to the
381 * request is released
Miklos Szeredi334f4852005-09-09 13:10:27 -0700382 */
383static void request_end(struct fuse_conn *fc, struct fuse_req *req)
384{
Miklos Szeredi4ce60812015-07-01 16:26:02 +0200385 struct fuse_iqueue *fiq = &fc->iq;
Miklos Szeredi365ae712015-07-01 16:26:06 +0200386
Miklos Szerediefe28002015-07-01 16:26:07 +0200387 if (test_and_set_bit(FR_FINISHED, &req->flags))
Miklos Szeredib8f95e52018-07-26 16:13:11 +0200388 goto put_request;
Miklos Szeredi365ae712015-07-01 16:26:06 +0200389
Miklos Szeredi4ce60812015-07-01 16:26:02 +0200390 spin_lock(&fiq->waitq.lock);
Miklos Szeredi0d8e84b2015-07-01 16:25:58 +0200391 list_del_init(&req->intr_entry);
Miklos Szeredi4ce60812015-07-01 16:26:02 +0200392 spin_unlock(&fiq->waitq.lock);
Miklos Szeredi33e14b42015-07-01 16:26:01 +0200393 WARN_ON(test_bit(FR_PENDING, &req->flags));
394 WARN_ON(test_bit(FR_SENT, &req->flags));
Miklos Szeredi825d6d32015-07-01 16:25:58 +0200395 if (test_bit(FR_BACKGROUND, &req->flags)) {
Miklos Szerediefe28002015-07-01 16:26:07 +0200396 spin_lock(&fc->lock);
Miklos Szeredi825d6d32015-07-01 16:25:58 +0200397 clear_bit(FR_BACKGROUND, &req->flags);
Miklos Szeredi1ed087a2018-09-28 16:43:22 +0200398 if (fc->num_background == fc->max_background) {
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700399 fc->blocked = 0;
Maxim Patlasov722d2be2013-03-21 18:02:36 +0400400 wake_up(&fc->blocked_waitq);
Miklos Szeredi1ed087a2018-09-28 16:43:22 +0200401 } else if (!fc->blocked) {
402 /*
403 * Wake up next waiter, if any. It's okay to use
404 * waitqueue_active(), as we've already synced up
405 * fc->blocked with waiters with the wake_up() call
406 * above.
407 */
408 if (waitqueue_active(&fc->blocked_waitq))
409 wake_up(&fc->blocked_waitq);
410 }
Maxim Patlasov722d2be2013-03-21 18:02:36 +0400411
Tejun Heo8a301eb2018-02-02 09:54:14 -0800412 if (fc->num_background == fc->congestion_threshold && fc->sb) {
Jan Kara5f7f7542017-04-12 12:24:40 +0200413 clear_bdi_congested(fc->sb->s_bdi, BLK_RW_SYNC);
414 clear_bdi_congested(fc->sb->s_bdi, BLK_RW_ASYNC);
Miklos Szeredif92b99b2007-10-16 23:30:59 -0700415 }
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700416 fc->num_background--;
Miklos Szeredid12def12008-02-06 01:38:39 -0800417 fc->active_background--;
418 flush_bg_queue(fc);
Miklos Szerediefe28002015-07-01 16:26:07 +0200419 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700420 }
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700421 wake_up(&req->waitq);
Miklos Szeredi1e6881c2015-07-01 16:26:07 +0200422 if (req->end)
423 req->end(fc, req);
Miklos Szeredib8f95e52018-07-26 16:13:11 +0200424put_request:
Tejun Heoe9bb09d2008-11-26 12:03:54 +0100425 fuse_put_request(fc, req);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700426}
427
Miklos Szeredif88996a2015-07-01 16:26:01 +0200428static void queue_interrupt(struct fuse_iqueue *fiq, struct fuse_req *req)
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700429{
Miklos Szeredi4ce60812015-07-01 16:26:02 +0200430 spin_lock(&fiq->waitq.lock);
Sahitya Tummala6ba4d272017-02-08 20:30:56 +0530431 if (test_bit(FR_FINISHED, &req->flags)) {
432 spin_unlock(&fiq->waitq.lock);
433 return;
434 }
Miklos Szeredi8f7bb362015-07-01 16:26:03 +0200435 if (list_empty(&req->intr_entry)) {
436 list_add_tail(&req->intr_entry, &fiq->interrupts);
437 wake_up_locked(&fiq->waitq);
438 }
Miklos Szeredi4ce60812015-07-01 16:26:02 +0200439 spin_unlock(&fiq->waitq.lock);
Miklos Szeredif88996a2015-07-01 16:26:01 +0200440 kill_fasync(&fiq->fasync, SIGIO, POLL_IN);
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700441}
442
Miklos Szeredi7c352bd2005-09-09 13:10:39 -0700443static void request_wait_answer(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700444{
Miklos Szeredi4ce60812015-07-01 16:26:02 +0200445 struct fuse_iqueue *fiq = &fc->iq;
Miklos Szeredic4775262015-07-01 16:26:00 +0200446 int err;
447
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700448 if (!fc->no_interrupt) {
449 /* Any signal may interrupt this */
Miklos Szeredic4775262015-07-01 16:26:00 +0200450 err = wait_event_interruptible(req->waitq,
Miklos Szeredi33e14b42015-07-01 16:26:01 +0200451 test_bit(FR_FINISHED, &req->flags));
Miklos Szeredic4775262015-07-01 16:26:00 +0200452 if (!err)
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700453 return;
454
Miklos Szeredi825d6d32015-07-01 16:25:58 +0200455 set_bit(FR_INTERRUPTED, &req->flags);
Miklos Szeredi8f7bb362015-07-01 16:26:03 +0200456 /* matches barrier in fuse_dev_do_read() */
457 smp_mb__after_atomic();
Miklos Szeredi33e14b42015-07-01 16:26:01 +0200458 if (test_bit(FR_SENT, &req->flags))
Miklos Szeredi4ce60812015-07-01 16:26:02 +0200459 queue_interrupt(fiq, req);
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700460 }
461
Miklos Szeredi825d6d32015-07-01 16:25:58 +0200462 if (!test_bit(FR_FORCE, &req->flags)) {
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700463 /* Only fatal signals may interrupt this */
Al Viro7d3a07f2016-07-19 03:08:27 -0400464 err = wait_event_killable(req->waitq,
Miklos Szeredi33e14b42015-07-01 16:26:01 +0200465 test_bit(FR_FINISHED, &req->flags));
Miklos Szeredic4775262015-07-01 16:26:00 +0200466 if (!err)
Miklos Szeredia131de02007-10-16 23:31:04 -0700467 return;
468
Miklos Szeredi4ce60812015-07-01 16:26:02 +0200469 spin_lock(&fiq->waitq.lock);
Miklos Szeredia131de02007-10-16 23:31:04 -0700470 /* Request is not yet in userspace, bail out */
Miklos Szeredi33e14b42015-07-01 16:26:01 +0200471 if (test_bit(FR_PENDING, &req->flags)) {
Miklos Szeredia131de02007-10-16 23:31:04 -0700472 list_del(&req->list);
Miklos Szeredi4ce60812015-07-01 16:26:02 +0200473 spin_unlock(&fiq->waitq.lock);
Miklos Szeredia131de02007-10-16 23:31:04 -0700474 __fuse_put_request(req);
475 req->out.h.error = -EINTR;
476 return;
477 }
Miklos Szeredi4ce60812015-07-01 16:26:02 +0200478 spin_unlock(&fiq->waitq.lock);
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700479 }
Miklos Szeredi334f4852005-09-09 13:10:27 -0700480
Miklos Szeredia131de02007-10-16 23:31:04 -0700481 /*
482 * Either request is already in userspace, or it was forced.
483 * Wait it out.
484 */
Miklos Szeredi33e14b42015-07-01 16:26:01 +0200485 wait_event(req->waitq, test_bit(FR_FINISHED, &req->flags));
Miklos Szeredi334f4852005-09-09 13:10:27 -0700486}
487
Eric Wong6a4e9222013-02-04 13:04:44 +0000488static void __fuse_request_send(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700489{
Miklos Szeredie16714d2015-07-01 16:26:01 +0200490 struct fuse_iqueue *fiq = &fc->iq;
491
Miklos Szeredi825d6d32015-07-01 16:25:58 +0200492 BUG_ON(test_bit(FR_BACKGROUND, &req->flags));
Miklos Szeredi4ce60812015-07-01 16:26:02 +0200493 spin_lock(&fiq->waitq.lock);
Miklos Szeredie16714d2015-07-01 16:26:01 +0200494 if (!fiq->connected) {
Miklos Szeredi4ce60812015-07-01 16:26:02 +0200495 spin_unlock(&fiq->waitq.lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700496 req->out.h.error = -ENOTCONN;
Miklos Szeredic4775262015-07-01 16:26:00 +0200497 } else {
Miklos Szeredif88996a2015-07-01 16:26:01 +0200498 req->in.h.unique = fuse_get_unique(fiq);
499 queue_request(fiq, req);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700500 /* acquire extra reference, since request is still needed
501 after request_end() */
502 __fuse_get_request(req);
Miklos Szeredi4ce60812015-07-01 16:26:02 +0200503 spin_unlock(&fiq->waitq.lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700504
Miklos Szeredi7c352bd2005-09-09 13:10:39 -0700505 request_wait_answer(fc, req);
Miklos Szeredic4775262015-07-01 16:26:00 +0200506 /* Pairs with smp_wmb() in request_end() */
507 smp_rmb();
Miklos Szeredi334f4852005-09-09 13:10:27 -0700508 }
Miklos Szeredi334f4852005-09-09 13:10:27 -0700509}
Eric Wong6a4e9222013-02-04 13:04:44 +0000510
511void fuse_request_send(struct fuse_conn *fc, struct fuse_req *req)
512{
Miklos Szeredi825d6d32015-07-01 16:25:58 +0200513 __set_bit(FR_ISREPLY, &req->flags);
514 if (!test_bit(FR_WAITING, &req->flags)) {
515 __set_bit(FR_WAITING, &req->flags);
Miklos Szeredi5437f242015-07-01 16:25:56 +0200516 atomic_inc(&fc->num_waiting);
517 }
Eric Wong6a4e9222013-02-04 13:04:44 +0000518 __fuse_request_send(fc, req);
519}
Tejun Heo08cbf542009-04-14 10:54:53 +0900520EXPORT_SYMBOL_GPL(fuse_request_send);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700521
Miklos Szeredi21f62172015-01-06 10:45:35 +0100522static void fuse_adjust_compat(struct fuse_conn *fc, struct fuse_args *args)
523{
524 if (fc->minor < 4 && args->in.h.opcode == FUSE_STATFS)
525 args->out.args[0].size = FUSE_COMPAT_STATFS_SIZE;
526
527 if (fc->minor < 9) {
528 switch (args->in.h.opcode) {
529 case FUSE_LOOKUP:
530 case FUSE_CREATE:
531 case FUSE_MKNOD:
532 case FUSE_MKDIR:
533 case FUSE_SYMLINK:
534 case FUSE_LINK:
535 args->out.args[0].size = FUSE_COMPAT_ENTRY_OUT_SIZE;
536 break;
537 case FUSE_GETATTR:
538 case FUSE_SETATTR:
539 args->out.args[0].size = FUSE_COMPAT_ATTR_OUT_SIZE;
540 break;
541 }
542 }
543 if (fc->minor < 12) {
544 switch (args->in.h.opcode) {
545 case FUSE_CREATE:
546 args->in.args[0].size = sizeof(struct fuse_open_in);
547 break;
548 case FUSE_MKNOD:
549 args->in.args[0].size = FUSE_COMPAT_MKNOD_IN_SIZE;
550 break;
551 }
552 }
553}
554
Miklos Szeredi70781872014-12-12 09:49:05 +0100555ssize_t fuse_simple_request(struct fuse_conn *fc, struct fuse_args *args)
556{
557 struct fuse_req *req;
558 ssize_t ret;
559
560 req = fuse_get_req(fc, 0);
561 if (IS_ERR(req))
562 return PTR_ERR(req);
563
Miklos Szeredi21f62172015-01-06 10:45:35 +0100564 /* Needs to be done after fuse_get_req() so that fc->minor is valid */
565 fuse_adjust_compat(fc, args);
566
Miklos Szeredi70781872014-12-12 09:49:05 +0100567 req->in.h.opcode = args->in.h.opcode;
568 req->in.h.nodeid = args->in.h.nodeid;
569 req->in.numargs = args->in.numargs;
570 memcpy(req->in.args, args->in.args,
571 args->in.numargs * sizeof(struct fuse_in_arg));
572 req->out.argvar = args->out.argvar;
573 req->out.numargs = args->out.numargs;
574 memcpy(req->out.args, args->out.args,
575 args->out.numargs * sizeof(struct fuse_arg));
576 fuse_request_send(fc, req);
577 ret = req->out.h.error;
578 if (!ret && args->out.argvar) {
579 BUG_ON(args->out.numargs != 1);
580 ret = req->out.args[0].size;
581 }
582 fuse_put_request(fc, req);
583
584 return ret;
585}
586
Miklos Szeredif0139aa2015-07-01 16:25:57 +0200587/*
588 * Called under fc->lock
589 *
590 * fc->connected must have been checked previously
591 */
592void fuse_request_send_background_locked(struct fuse_conn *fc,
593 struct fuse_req *req)
Miklos Szeredid12def12008-02-06 01:38:39 -0800594{
Miklos Szeredi825d6d32015-07-01 16:25:58 +0200595 BUG_ON(!test_bit(FR_BACKGROUND, &req->flags));
596 if (!test_bit(FR_WAITING, &req->flags)) {
597 __set_bit(FR_WAITING, &req->flags);
Miklos Szeredi5437f242015-07-01 16:25:56 +0200598 atomic_inc(&fc->num_waiting);
599 }
Miklos Szeredi825d6d32015-07-01 16:25:58 +0200600 __set_bit(FR_ISREPLY, &req->flags);
Miklos Szeredid12def12008-02-06 01:38:39 -0800601 fc->num_background++;
Csaba Henk7a6d3c82009-07-01 17:28:41 -0700602 if (fc->num_background == fc->max_background)
Miklos Szeredid12def12008-02-06 01:38:39 -0800603 fc->blocked = 1;
Jan Kara7fbbe972017-04-12 12:24:41 +0200604 if (fc->num_background == fc->congestion_threshold && fc->sb) {
Jan Kara5f7f7542017-04-12 12:24:40 +0200605 set_bdi_congested(fc->sb->s_bdi, BLK_RW_SYNC);
606 set_bdi_congested(fc->sb->s_bdi, BLK_RW_ASYNC);
Miklos Szeredid12def12008-02-06 01:38:39 -0800607 }
608 list_add_tail(&req->list, &fc->bg_queue);
609 flush_bg_queue(fc);
610}
611
Miklos Szeredif0139aa2015-07-01 16:25:57 +0200612void fuse_request_send_background(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700613{
Miklos Szeredi42dc6212015-07-01 16:25:56 +0200614 BUG_ON(!req->end);
Miklos Szeredid7133112006-04-10 22:54:55 -0700615 spin_lock(&fc->lock);
Miklos Szeredi1e9a4ed2005-09-09 13:10:31 -0700616 if (fc->connected) {
Miklos Szeredif0139aa2015-07-01 16:25:57 +0200617 fuse_request_send_background_locked(fc, req);
Miklos Szeredid7133112006-04-10 22:54:55 -0700618 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700619 } else {
Miklos Szeredi42dc6212015-07-01 16:25:56 +0200620 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700621 req->out.h.error = -ENOTCONN;
Miklos Szeredi42dc6212015-07-01 16:25:56 +0200622 req->end(fc, req);
623 fuse_put_request(fc, req);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700624 }
625}
Tejun Heo08cbf542009-04-14 10:54:53 +0900626EXPORT_SYMBOL_GPL(fuse_request_send_background);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700627
Miklos Szeredi2d45ba32010-07-12 14:41:40 +0200628static int fuse_request_send_notify_reply(struct fuse_conn *fc,
629 struct fuse_req *req, u64 unique)
630{
631 int err = -ENODEV;
Miklos Szeredif88996a2015-07-01 16:26:01 +0200632 struct fuse_iqueue *fiq = &fc->iq;
Miklos Szeredi2d45ba32010-07-12 14:41:40 +0200633
Miklos Szeredi825d6d32015-07-01 16:25:58 +0200634 __clear_bit(FR_ISREPLY, &req->flags);
Miklos Szeredi2d45ba32010-07-12 14:41:40 +0200635 req->in.h.unique = unique;
Miklos Szeredi4ce60812015-07-01 16:26:02 +0200636 spin_lock(&fiq->waitq.lock);
Miklos Szeredie16714d2015-07-01 16:26:01 +0200637 if (fiq->connected) {
Miklos Szeredif88996a2015-07-01 16:26:01 +0200638 queue_request(fiq, req);
Miklos Szeredi2d45ba32010-07-12 14:41:40 +0200639 err = 0;
640 }
Miklos Szeredi4ce60812015-07-01 16:26:02 +0200641 spin_unlock(&fiq->waitq.lock);
Miklos Szeredi2d45ba32010-07-12 14:41:40 +0200642
643 return err;
644}
645
Anand V. Avati0b05b182012-08-19 08:53:23 -0400646void fuse_force_forget(struct file *file, u64 nodeid)
647{
Al Viro6131ffa2013-02-27 16:59:05 -0500648 struct inode *inode = file_inode(file);
Anand V. Avati0b05b182012-08-19 08:53:23 -0400649 struct fuse_conn *fc = get_fuse_conn(inode);
650 struct fuse_req *req;
651 struct fuse_forget_in inarg;
652
653 memset(&inarg, 0, sizeof(inarg));
654 inarg.nlookup = 1;
Maxim Patlasovb111c8c2012-10-26 19:48:30 +0400655 req = fuse_get_req_nofail_nopages(fc, file);
Anand V. Avati0b05b182012-08-19 08:53:23 -0400656 req->in.h.opcode = FUSE_FORGET;
657 req->in.h.nodeid = nodeid;
658 req->in.numargs = 1;
659 req->in.args[0].size = sizeof(inarg);
660 req->in.args[0].value = &inarg;
Miklos Szeredi825d6d32015-07-01 16:25:58 +0200661 __clear_bit(FR_ISREPLY, &req->flags);
Eric Wong6a4e9222013-02-04 13:04:44 +0000662 __fuse_request_send(fc, req);
663 /* ignore errors */
664 fuse_put_request(fc, req);
Anand V. Avati0b05b182012-08-19 08:53:23 -0400665}
666
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700667/*
Miklos Szeredi334f4852005-09-09 13:10:27 -0700668 * Lock the request. Up to the next unlock_request() there mustn't be
669 * anything that could cause a page-fault. If the request was already
Miklos Szeredif9a28422006-06-25 05:48:53 -0700670 * aborted bail out.
Miklos Szeredi334f4852005-09-09 13:10:27 -0700671 */
Miklos Szeredidc008092015-07-01 16:25:58 +0200672static int lock_request(struct fuse_req *req)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700673{
674 int err = 0;
675 if (req) {
Miklos Szeredidc008092015-07-01 16:25:58 +0200676 spin_lock(&req->waitq.lock);
Miklos Szeredi825d6d32015-07-01 16:25:58 +0200677 if (test_bit(FR_ABORTED, &req->flags))
Miklos Szeredi334f4852005-09-09 13:10:27 -0700678 err = -ENOENT;
679 else
Miklos Szeredi825d6d32015-07-01 16:25:58 +0200680 set_bit(FR_LOCKED, &req->flags);
Miklos Szeredidc008092015-07-01 16:25:58 +0200681 spin_unlock(&req->waitq.lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700682 }
683 return err;
684}
685
686/*
Miklos Szeredi0d8e84b2015-07-01 16:25:58 +0200687 * Unlock request. If it was aborted while locked, caller is responsible
688 * for unlocking and ending the request.
Miklos Szeredi334f4852005-09-09 13:10:27 -0700689 */
Miklos Szeredidc008092015-07-01 16:25:58 +0200690static int unlock_request(struct fuse_req *req)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700691{
Miklos Szeredi0d8e84b2015-07-01 16:25:58 +0200692 int err = 0;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700693 if (req) {
Miklos Szeredidc008092015-07-01 16:25:58 +0200694 spin_lock(&req->waitq.lock);
Miklos Szeredi825d6d32015-07-01 16:25:58 +0200695 if (test_bit(FR_ABORTED, &req->flags))
Miklos Szeredi0d8e84b2015-07-01 16:25:58 +0200696 err = -ENOENT;
697 else
Miklos Szeredi825d6d32015-07-01 16:25:58 +0200698 clear_bit(FR_LOCKED, &req->flags);
Miklos Szeredidc008092015-07-01 16:25:58 +0200699 spin_unlock(&req->waitq.lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700700 }
Miklos Szeredi0d8e84b2015-07-01 16:25:58 +0200701 return err;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700702}
703
704struct fuse_copy_state {
705 int write;
706 struct fuse_req *req;
Al Viro6c09e942015-04-03 22:06:08 -0400707 struct iov_iter *iter;
Miklos Szeredidd3bb142010-05-25 15:06:06 +0200708 struct pipe_buffer *pipebufs;
709 struct pipe_buffer *currbuf;
710 struct pipe_inode_info *pipe;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700711 unsigned long nr_segs;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700712 struct page *pg;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700713 unsigned len;
Miklos Szeredic55a01d2014-07-07 15:28:51 +0200714 unsigned offset;
Miklos Szeredice534fb2010-05-25 15:06:07 +0200715 unsigned move_pages:1;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700716};
717
Miklos Szeredidc008092015-07-01 16:25:58 +0200718static void fuse_copy_init(struct fuse_copy_state *cs, int write,
Al Viro6c09e942015-04-03 22:06:08 -0400719 struct iov_iter *iter)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700720{
721 memset(cs, 0, sizeof(*cs));
722 cs->write = write;
Al Viro6c09e942015-04-03 22:06:08 -0400723 cs->iter = iter;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700724}
725
726/* Unmap and put previous page of userspace buffer */
Miklos Szeredi8bfc0162006-01-16 22:14:28 -0800727static void fuse_copy_finish(struct fuse_copy_state *cs)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700728{
Miklos Szeredidd3bb142010-05-25 15:06:06 +0200729 if (cs->currbuf) {
730 struct pipe_buffer *buf = cs->currbuf;
731
Miklos Szeredic55a01d2014-07-07 15:28:51 +0200732 if (cs->write)
Miklos Szeredic3021622010-05-25 15:06:07 +0200733 buf->len = PAGE_SIZE - cs->len;
Miklos Szeredidd3bb142010-05-25 15:06:06 +0200734 cs->currbuf = NULL;
Miklos Szeredic55a01d2014-07-07 15:28:51 +0200735 } else if (cs->pg) {
Miklos Szeredi334f4852005-09-09 13:10:27 -0700736 if (cs->write) {
737 flush_dcache_page(cs->pg);
738 set_page_dirty_lock(cs->pg);
739 }
740 put_page(cs->pg);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700741 }
Miklos Szeredic55a01d2014-07-07 15:28:51 +0200742 cs->pg = NULL;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700743}
744
745/*
746 * Get another pagefull of userspace buffer, and map it to kernel
747 * address space, and lock request
748 */
749static int fuse_copy_fill(struct fuse_copy_state *cs)
750{
Miklos Szeredic55a01d2014-07-07 15:28:51 +0200751 struct page *page;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700752 int err;
753
Miklos Szeredidc008092015-07-01 16:25:58 +0200754 err = unlock_request(cs->req);
Miklos Szeredi0d8e84b2015-07-01 16:25:58 +0200755 if (err)
756 return err;
757
Miklos Szeredi334f4852005-09-09 13:10:27 -0700758 fuse_copy_finish(cs);
Miklos Szeredidd3bb142010-05-25 15:06:06 +0200759 if (cs->pipebufs) {
760 struct pipe_buffer *buf = cs->pipebufs;
761
Miklos Szeredic3021622010-05-25 15:06:07 +0200762 if (!cs->write) {
Miklos Szeredifba597d2016-09-27 10:45:12 +0200763 err = pipe_buf_confirm(cs->pipe, buf);
Miklos Szeredic3021622010-05-25 15:06:07 +0200764 if (err)
765 return err;
Miklos Szeredidd3bb142010-05-25 15:06:06 +0200766
Miklos Szeredic3021622010-05-25 15:06:07 +0200767 BUG_ON(!cs->nr_segs);
768 cs->currbuf = buf;
Miklos Szeredic55a01d2014-07-07 15:28:51 +0200769 cs->pg = buf->page;
770 cs->offset = buf->offset;
Miklos Szeredic3021622010-05-25 15:06:07 +0200771 cs->len = buf->len;
Miklos Szeredic3021622010-05-25 15:06:07 +0200772 cs->pipebufs++;
773 cs->nr_segs--;
774 } else {
Miklos Szeredic3021622010-05-25 15:06:07 +0200775 if (cs->nr_segs == cs->pipe->buffers)
776 return -EIO;
777
778 page = alloc_page(GFP_HIGHUSER);
779 if (!page)
780 return -ENOMEM;
781
782 buf->page = page;
783 buf->offset = 0;
784 buf->len = 0;
785
786 cs->currbuf = buf;
Miklos Szeredic55a01d2014-07-07 15:28:51 +0200787 cs->pg = page;
788 cs->offset = 0;
Miklos Szeredic3021622010-05-25 15:06:07 +0200789 cs->len = PAGE_SIZE;
790 cs->pipebufs++;
791 cs->nr_segs++;
792 }
Miklos Szeredidd3bb142010-05-25 15:06:06 +0200793 } else {
Al Viro6c09e942015-04-03 22:06:08 -0400794 size_t off;
795 err = iov_iter_get_pages(cs->iter, &page, PAGE_SIZE, 1, &off);
Miklos Szeredidd3bb142010-05-25 15:06:06 +0200796 if (err < 0)
797 return err;
Al Viro6c09e942015-04-03 22:06:08 -0400798 BUG_ON(!err);
799 cs->len = err;
800 cs->offset = off;
Miklos Szeredic55a01d2014-07-07 15:28:51 +0200801 cs->pg = page;
Al Viro6c09e942015-04-03 22:06:08 -0400802 iov_iter_advance(cs->iter, err);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700803 }
Miklos Szeredi334f4852005-09-09 13:10:27 -0700804
Miklos Szeredidc008092015-07-01 16:25:58 +0200805 return lock_request(cs->req);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700806}
807
808/* Do as much copy to/from userspace buffer as we can */
Miklos Szeredi8bfc0162006-01-16 22:14:28 -0800809static int fuse_copy_do(struct fuse_copy_state *cs, void **val, unsigned *size)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700810{
811 unsigned ncpy = min(*size, cs->len);
812 if (val) {
Miklos Szeredic55a01d2014-07-07 15:28:51 +0200813 void *pgaddr = kmap_atomic(cs->pg);
814 void *buf = pgaddr + cs->offset;
815
Miklos Szeredi334f4852005-09-09 13:10:27 -0700816 if (cs->write)
Miklos Szeredic55a01d2014-07-07 15:28:51 +0200817 memcpy(buf, *val, ncpy);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700818 else
Miklos Szeredic55a01d2014-07-07 15:28:51 +0200819 memcpy(*val, buf, ncpy);
820
821 kunmap_atomic(pgaddr);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700822 *val += ncpy;
823 }
824 *size -= ncpy;
825 cs->len -= ncpy;
Miklos Szeredic55a01d2014-07-07 15:28:51 +0200826 cs->offset += ncpy;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700827 return ncpy;
828}
829
Miklos Szeredice534fb2010-05-25 15:06:07 +0200830static int fuse_check_page(struct page *page)
831{
832 if (page_mapcount(page) ||
833 page->mapping != NULL ||
834 page_count(page) != 1 ||
835 (page->flags & PAGE_FLAGS_CHECK_AT_PREP &
836 ~(1 << PG_locked |
837 1 << PG_referenced |
838 1 << PG_uptodate |
839 1 << PG_lru |
840 1 << PG_active |
841 1 << PG_reclaim))) {
842 printk(KERN_WARNING "fuse: trying to steal weird page\n");
843 printk(KERN_WARNING " page=%p index=%li flags=%08lx, count=%i, mapcount=%i, mapping=%p\n", page, page->index, page->flags, page_count(page), page_mapcount(page), page->mapping);
844 return 1;
845 }
846 return 0;
847}
848
849static int fuse_try_move_page(struct fuse_copy_state *cs, struct page **pagep)
850{
851 int err;
852 struct page *oldpage = *pagep;
853 struct page *newpage;
854 struct pipe_buffer *buf = cs->pipebufs;
Miklos Szeredice534fb2010-05-25 15:06:07 +0200855
Miklos Szeredidc008092015-07-01 16:25:58 +0200856 err = unlock_request(cs->req);
Miklos Szeredi0d8e84b2015-07-01 16:25:58 +0200857 if (err)
858 return err;
859
Miklos Szeredice534fb2010-05-25 15:06:07 +0200860 fuse_copy_finish(cs);
861
Miklos Szeredifba597d2016-09-27 10:45:12 +0200862 err = pipe_buf_confirm(cs->pipe, buf);
Miklos Szeredice534fb2010-05-25 15:06:07 +0200863 if (err)
864 return err;
865
866 BUG_ON(!cs->nr_segs);
867 cs->currbuf = buf;
868 cs->len = buf->len;
869 cs->pipebufs++;
870 cs->nr_segs--;
871
872 if (cs->len != PAGE_SIZE)
873 goto out_fallback;
874
Miklos Szeredica76f5b2016-09-27 10:45:12 +0200875 if (pipe_buf_steal(cs->pipe, buf) != 0)
Miklos Szeredice534fb2010-05-25 15:06:07 +0200876 goto out_fallback;
877
878 newpage = buf->page;
879
Miklos Szerediaa991b32015-02-26 11:45:47 +0100880 if (!PageUptodate(newpage))
881 SetPageUptodate(newpage);
Miklos Szeredice534fb2010-05-25 15:06:07 +0200882
883 ClearPageMappedToDisk(newpage);
884
885 if (fuse_check_page(newpage) != 0)
886 goto out_fallback_unlock;
887
Miklos Szeredice534fb2010-05-25 15:06:07 +0200888 /*
889 * This is a new and locked page, it shouldn't be mapped or
890 * have any special flags on it
891 */
892 if (WARN_ON(page_mapped(oldpage)))
893 goto out_fallback_unlock;
894 if (WARN_ON(page_has_private(oldpage)))
895 goto out_fallback_unlock;
896 if (WARN_ON(PageDirty(oldpage) || PageWriteback(oldpage)))
897 goto out_fallback_unlock;
898 if (WARN_ON(PageMlocked(oldpage)))
899 goto out_fallback_unlock;
900
Miklos Szeredief6a3c62011-03-22 16:30:52 -0700901 err = replace_page_cache_page(oldpage, newpage, GFP_KERNEL);
Miklos Szeredice534fb2010-05-25 15:06:07 +0200902 if (err) {
Miklos Szeredief6a3c62011-03-22 16:30:52 -0700903 unlock_page(newpage);
904 return err;
Miklos Szeredice534fb2010-05-25 15:06:07 +0200905 }
Miklos Szeredief6a3c62011-03-22 16:30:52 -0700906
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300907 get_page(newpage);
Miklos Szeredice534fb2010-05-25 15:06:07 +0200908
909 if (!(buf->flags & PIPE_BUF_FLAG_LRU))
910 lru_cache_add_file(newpage);
911
912 err = 0;
Miklos Szeredidc008092015-07-01 16:25:58 +0200913 spin_lock(&cs->req->waitq.lock);
Miklos Szeredi825d6d32015-07-01 16:25:58 +0200914 if (test_bit(FR_ABORTED, &cs->req->flags))
Miklos Szeredice534fb2010-05-25 15:06:07 +0200915 err = -ENOENT;
916 else
917 *pagep = newpage;
Miklos Szeredidc008092015-07-01 16:25:58 +0200918 spin_unlock(&cs->req->waitq.lock);
Miklos Szeredice534fb2010-05-25 15:06:07 +0200919
920 if (err) {
921 unlock_page(newpage);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300922 put_page(newpage);
Miklos Szeredice534fb2010-05-25 15:06:07 +0200923 return err;
924 }
925
926 unlock_page(oldpage);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300927 put_page(oldpage);
Miklos Szeredice534fb2010-05-25 15:06:07 +0200928 cs->len = 0;
929
930 return 0;
931
932out_fallback_unlock:
933 unlock_page(newpage);
934out_fallback:
Miklos Szeredic55a01d2014-07-07 15:28:51 +0200935 cs->pg = buf->page;
936 cs->offset = buf->offset;
Miklos Szeredice534fb2010-05-25 15:06:07 +0200937
Miklos Szeredidc008092015-07-01 16:25:58 +0200938 err = lock_request(cs->req);
Miklos Szeredice534fb2010-05-25 15:06:07 +0200939 if (err)
940 return err;
941
942 return 1;
943}
944
Miklos Szeredic3021622010-05-25 15:06:07 +0200945static int fuse_ref_page(struct fuse_copy_state *cs, struct page *page,
946 unsigned offset, unsigned count)
947{
948 struct pipe_buffer *buf;
Miklos Szeredi0d8e84b2015-07-01 16:25:58 +0200949 int err;
Miklos Szeredic3021622010-05-25 15:06:07 +0200950
951 if (cs->nr_segs == cs->pipe->buffers)
952 return -EIO;
953
Miklos Szeredidc008092015-07-01 16:25:58 +0200954 err = unlock_request(cs->req);
Miklos Szeredi0d8e84b2015-07-01 16:25:58 +0200955 if (err)
956 return err;
957
Miklos Szeredic3021622010-05-25 15:06:07 +0200958 fuse_copy_finish(cs);
959
960 buf = cs->pipebufs;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300961 get_page(page);
Miklos Szeredic3021622010-05-25 15:06:07 +0200962 buf->page = page;
963 buf->offset = offset;
964 buf->len = count;
965
966 cs->pipebufs++;
967 cs->nr_segs++;
968 cs->len = 0;
969
970 return 0;
971}
972
Miklos Szeredi334f4852005-09-09 13:10:27 -0700973/*
974 * Copy a page in the request to/from the userspace buffer. Must be
975 * done atomically
976 */
Miklos Szeredice534fb2010-05-25 15:06:07 +0200977static int fuse_copy_page(struct fuse_copy_state *cs, struct page **pagep,
Miklos Szeredi8bfc0162006-01-16 22:14:28 -0800978 unsigned offset, unsigned count, int zeroing)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700979{
Miklos Szeredice534fb2010-05-25 15:06:07 +0200980 int err;
981 struct page *page = *pagep;
982
Miklos Szeredib6777c42010-10-26 14:22:27 -0700983 if (page && zeroing && count < PAGE_SIZE)
984 clear_highpage(page);
985
Miklos Szeredi334f4852005-09-09 13:10:27 -0700986 while (count) {
Miklos Szeredic3021622010-05-25 15:06:07 +0200987 if (cs->write && cs->pipebufs && page) {
988 return fuse_ref_page(cs, page, offset, count);
989 } else if (!cs->len) {
Miklos Szeredice534fb2010-05-25 15:06:07 +0200990 if (cs->move_pages && page &&
991 offset == 0 && count == PAGE_SIZE) {
992 err = fuse_try_move_page(cs, pagep);
993 if (err <= 0)
994 return err;
995 } else {
996 err = fuse_copy_fill(cs);
997 if (err)
998 return err;
999 }
Miklos Szeredi1729a162008-11-26 12:03:54 +01001000 }
Miklos Szeredi334f4852005-09-09 13:10:27 -07001001 if (page) {
Cong Wang2408f6e2011-11-25 23:14:30 +08001002 void *mapaddr = kmap_atomic(page);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001003 void *buf = mapaddr + offset;
1004 offset += fuse_copy_do(cs, &buf, &count);
Cong Wang2408f6e2011-11-25 23:14:30 +08001005 kunmap_atomic(mapaddr);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001006 } else
1007 offset += fuse_copy_do(cs, NULL, &count);
1008 }
1009 if (page && !cs->write)
1010 flush_dcache_page(page);
1011 return 0;
1012}
1013
1014/* Copy pages in the request to/from userspace buffer */
1015static int fuse_copy_pages(struct fuse_copy_state *cs, unsigned nbytes,
1016 int zeroing)
1017{
1018 unsigned i;
1019 struct fuse_req *req = cs->req;
Miklos Szeredi334f4852005-09-09 13:10:27 -07001020
1021 for (i = 0; i < req->num_pages && (nbytes || zeroing); i++) {
Miklos Szeredice534fb2010-05-25 15:06:07 +02001022 int err;
Maxim Patlasov85f40ae2012-10-26 19:49:33 +04001023 unsigned offset = req->page_descs[i].offset;
1024 unsigned count = min(nbytes, req->page_descs[i].length);
Miklos Szeredice534fb2010-05-25 15:06:07 +02001025
1026 err = fuse_copy_page(cs, &req->pages[i], offset, count,
1027 zeroing);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001028 if (err)
1029 return err;
1030
1031 nbytes -= count;
Miklos Szeredi334f4852005-09-09 13:10:27 -07001032 }
1033 return 0;
1034}
1035
1036/* Copy a single argument in the request to/from userspace buffer */
1037static int fuse_copy_one(struct fuse_copy_state *cs, void *val, unsigned size)
1038{
1039 while (size) {
Miklos Szeredi1729a162008-11-26 12:03:54 +01001040 if (!cs->len) {
1041 int err = fuse_copy_fill(cs);
1042 if (err)
1043 return err;
1044 }
Miklos Szeredi334f4852005-09-09 13:10:27 -07001045 fuse_copy_do(cs, &val, &size);
1046 }
1047 return 0;
1048}
1049
1050/* Copy request arguments to/from userspace buffer */
1051static int fuse_copy_args(struct fuse_copy_state *cs, unsigned numargs,
1052 unsigned argpages, struct fuse_arg *args,
1053 int zeroing)
1054{
1055 int err = 0;
1056 unsigned i;
1057
1058 for (i = 0; !err && i < numargs; i++) {
1059 struct fuse_arg *arg = &args[i];
1060 if (i == numargs - 1 && argpages)
1061 err = fuse_copy_pages(cs, arg->size, zeroing);
1062 else
1063 err = fuse_copy_one(cs, arg->value, arg->size);
1064 }
1065 return err;
1066}
1067
Miklos Szeredif88996a2015-07-01 16:26:01 +02001068static int forget_pending(struct fuse_iqueue *fiq)
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001069{
Miklos Szeredif88996a2015-07-01 16:26:01 +02001070 return fiq->forget_list_head.next != NULL;
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001071}
1072
Miklos Szeredif88996a2015-07-01 16:26:01 +02001073static int request_pending(struct fuse_iqueue *fiq)
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001074{
Miklos Szeredif88996a2015-07-01 16:26:01 +02001075 return !list_empty(&fiq->pending) || !list_empty(&fiq->interrupts) ||
1076 forget_pending(fiq);
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001077}
1078
Miklos Szeredi334f4852005-09-09 13:10:27 -07001079/*
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001080 * Transfer an interrupt request to userspace
1081 *
1082 * Unlike other requests this is assembled on demand, without a need
1083 * to allocate a separate fuse_req structure.
1084 *
Miklos Szeredifd22d622015-07-01 16:26:03 +02001085 * Called with fiq->waitq.lock held, releases it
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001086 */
Miklos Szeredifd22d622015-07-01 16:26:03 +02001087static int fuse_read_interrupt(struct fuse_iqueue *fiq,
1088 struct fuse_copy_state *cs,
Miklos Szeredic3021622010-05-25 15:06:07 +02001089 size_t nbytes, struct fuse_req *req)
Miklos Szeredifd22d622015-07-01 16:26:03 +02001090__releases(fiq->waitq.lock)
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001091{
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001092 struct fuse_in_header ih;
1093 struct fuse_interrupt_in arg;
1094 unsigned reqsize = sizeof(ih) + sizeof(arg);
1095 int err;
1096
1097 list_del_init(&req->intr_entry);
Miklos Szeredi4ce60812015-07-01 16:26:02 +02001098 req->intr_unique = fuse_get_unique(fiq);
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001099 memset(&ih, 0, sizeof(ih));
1100 memset(&arg, 0, sizeof(arg));
1101 ih.len = reqsize;
1102 ih.opcode = FUSE_INTERRUPT;
1103 ih.unique = req->intr_unique;
1104 arg.unique = req->in.h.unique;
1105
Miklos Szeredi4ce60812015-07-01 16:26:02 +02001106 spin_unlock(&fiq->waitq.lock);
Miklos Szeredic3021622010-05-25 15:06:07 +02001107 if (nbytes < reqsize)
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001108 return -EINVAL;
1109
Miklos Szeredic3021622010-05-25 15:06:07 +02001110 err = fuse_copy_one(cs, &ih, sizeof(ih));
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001111 if (!err)
Miklos Szeredic3021622010-05-25 15:06:07 +02001112 err = fuse_copy_one(cs, &arg, sizeof(arg));
1113 fuse_copy_finish(cs);
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001114
1115 return err ? err : reqsize;
1116}
1117
Miklos Szeredif88996a2015-07-01 16:26:01 +02001118static struct fuse_forget_link *dequeue_forget(struct fuse_iqueue *fiq,
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001119 unsigned max,
1120 unsigned *countp)
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001121{
Miklos Szeredif88996a2015-07-01 16:26:01 +02001122 struct fuse_forget_link *head = fiq->forget_list_head.next;
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001123 struct fuse_forget_link **newhead = &head;
1124 unsigned count;
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001125
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001126 for (count = 0; *newhead != NULL && count < max; count++)
1127 newhead = &(*newhead)->next;
1128
Miklos Szeredif88996a2015-07-01 16:26:01 +02001129 fiq->forget_list_head.next = *newhead;
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001130 *newhead = NULL;
Miklos Szeredif88996a2015-07-01 16:26:01 +02001131 if (fiq->forget_list_head.next == NULL)
1132 fiq->forget_list_tail = &fiq->forget_list_head;
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001133
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001134 if (countp != NULL)
1135 *countp = count;
1136
1137 return head;
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001138}
1139
Miklos Szeredifd22d622015-07-01 16:26:03 +02001140static int fuse_read_single_forget(struct fuse_iqueue *fiq,
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001141 struct fuse_copy_state *cs,
1142 size_t nbytes)
Miklos Szeredifd22d622015-07-01 16:26:03 +02001143__releases(fiq->waitq.lock)
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001144{
1145 int err;
Miklos Szeredif88996a2015-07-01 16:26:01 +02001146 struct fuse_forget_link *forget = dequeue_forget(fiq, 1, NULL);
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001147 struct fuse_forget_in arg = {
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001148 .nlookup = forget->forget_one.nlookup,
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001149 };
1150 struct fuse_in_header ih = {
1151 .opcode = FUSE_FORGET,
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001152 .nodeid = forget->forget_one.nodeid,
Miklos Szeredif88996a2015-07-01 16:26:01 +02001153 .unique = fuse_get_unique(fiq),
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001154 .len = sizeof(ih) + sizeof(arg),
1155 };
1156
Miklos Szeredi4ce60812015-07-01 16:26:02 +02001157 spin_unlock(&fiq->waitq.lock);
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001158 kfree(forget);
1159 if (nbytes < ih.len)
1160 return -EINVAL;
1161
1162 err = fuse_copy_one(cs, &ih, sizeof(ih));
1163 if (!err)
1164 err = fuse_copy_one(cs, &arg, sizeof(arg));
1165 fuse_copy_finish(cs);
1166
1167 if (err)
1168 return err;
1169
1170 return ih.len;
1171}
1172
Miklos Szeredifd22d622015-07-01 16:26:03 +02001173static int fuse_read_batch_forget(struct fuse_iqueue *fiq,
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001174 struct fuse_copy_state *cs, size_t nbytes)
Miklos Szeredifd22d622015-07-01 16:26:03 +02001175__releases(fiq->waitq.lock)
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001176{
1177 int err;
1178 unsigned max_forgets;
1179 unsigned count;
1180 struct fuse_forget_link *head;
1181 struct fuse_batch_forget_in arg = { .count = 0 };
1182 struct fuse_in_header ih = {
1183 .opcode = FUSE_BATCH_FORGET,
Miklos Szeredif88996a2015-07-01 16:26:01 +02001184 .unique = fuse_get_unique(fiq),
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001185 .len = sizeof(ih) + sizeof(arg),
1186 };
1187
1188 if (nbytes < ih.len) {
Miklos Szeredi4ce60812015-07-01 16:26:02 +02001189 spin_unlock(&fiq->waitq.lock);
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001190 return -EINVAL;
1191 }
1192
1193 max_forgets = (nbytes - ih.len) / sizeof(struct fuse_forget_one);
Miklos Szeredif88996a2015-07-01 16:26:01 +02001194 head = dequeue_forget(fiq, max_forgets, &count);
Miklos Szeredi4ce60812015-07-01 16:26:02 +02001195 spin_unlock(&fiq->waitq.lock);
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001196
1197 arg.count = count;
1198 ih.len += count * sizeof(struct fuse_forget_one);
1199 err = fuse_copy_one(cs, &ih, sizeof(ih));
1200 if (!err)
1201 err = fuse_copy_one(cs, &arg, sizeof(arg));
1202
1203 while (head) {
1204 struct fuse_forget_link *forget = head;
1205
1206 if (!err) {
1207 err = fuse_copy_one(cs, &forget->forget_one,
1208 sizeof(forget->forget_one));
1209 }
1210 head = forget->next;
1211 kfree(forget);
1212 }
1213
1214 fuse_copy_finish(cs);
1215
1216 if (err)
1217 return err;
1218
1219 return ih.len;
1220}
1221
Miklos Szeredifd22d622015-07-01 16:26:03 +02001222static int fuse_read_forget(struct fuse_conn *fc, struct fuse_iqueue *fiq,
1223 struct fuse_copy_state *cs,
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001224 size_t nbytes)
Miklos Szeredifd22d622015-07-01 16:26:03 +02001225__releases(fiq->waitq.lock)
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001226{
Miklos Szeredif88996a2015-07-01 16:26:01 +02001227 if (fc->minor < 16 || fiq->forget_list_head.next->next == NULL)
Miklos Szeredifd22d622015-07-01 16:26:03 +02001228 return fuse_read_single_forget(fiq, cs, nbytes);
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001229 else
Miklos Szeredifd22d622015-07-01 16:26:03 +02001230 return fuse_read_batch_forget(fiq, cs, nbytes);
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001231}
1232
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001233/*
Miklos Szeredi334f4852005-09-09 13:10:27 -07001234 * Read a single request into the userspace filesystem's buffer. This
1235 * function waits until a request is available, then removes it from
1236 * the pending list and copies request data to userspace buffer. If
Miklos Szeredif9a28422006-06-25 05:48:53 -07001237 * no reply is needed (FORGET) or request has been aborted or there
1238 * was an error during the copying then it's finished by calling
Miklos Szeredi334f4852005-09-09 13:10:27 -07001239 * request_end(). Otherwise add it to the processing list, and set
1240 * the 'sent' flag.
1241 */
Miklos Szeredic36960462015-07-01 16:26:09 +02001242static ssize_t fuse_dev_do_read(struct fuse_dev *fud, struct file *file,
Miklos Szeredic3021622010-05-25 15:06:07 +02001243 struct fuse_copy_state *cs, size_t nbytes)
Miklos Szeredi334f4852005-09-09 13:10:27 -07001244{
Miklos Szeredi82cbdcd2015-07-01 16:26:05 +02001245 ssize_t err;
Miklos Szeredic36960462015-07-01 16:26:09 +02001246 struct fuse_conn *fc = fud->fc;
Miklos Szeredif88996a2015-07-01 16:26:01 +02001247 struct fuse_iqueue *fiq = &fc->iq;
Miklos Szeredic36960462015-07-01 16:26:09 +02001248 struct fuse_pqueue *fpq = &fud->pq;
Miklos Szeredi334f4852005-09-09 13:10:27 -07001249 struct fuse_req *req;
1250 struct fuse_in *in;
Miklos Szeredi334f4852005-09-09 13:10:27 -07001251 unsigned reqsize;
1252
Miklos Szeredi1d3d7522006-01-06 00:19:40 -08001253 restart:
Miklos Szeredi4ce60812015-07-01 16:26:02 +02001254 spin_lock(&fiq->waitq.lock);
Jeff Dikee5ac1d12006-04-10 22:54:53 -07001255 err = -EAGAIN;
Miklos Szeredie16714d2015-07-01 16:26:01 +02001256 if ((file->f_flags & O_NONBLOCK) && fiq->connected &&
Miklos Szeredif88996a2015-07-01 16:26:01 +02001257 !request_pending(fiq))
Jeff Dikee5ac1d12006-04-10 22:54:53 -07001258 goto err_unlock;
1259
Miklos Szeredi52509212015-07-01 16:26:03 +02001260 err = wait_event_interruptible_exclusive_locked(fiq->waitq,
1261 !fiq->connected || request_pending(fiq));
1262 if (err)
1263 goto err_unlock;
1264
Szymon Lukasz3b7008b2017-11-09 21:23:35 +01001265 if (!fiq->connected) {
1266 err = (fc->aborted && fc->abort_err) ? -ECONNABORTED : -ENODEV;
Miklos Szeredi334f4852005-09-09 13:10:27 -07001267 goto err_unlock;
Szymon Lukasz3b7008b2017-11-09 21:23:35 +01001268 }
Miklos Szeredi334f4852005-09-09 13:10:27 -07001269
Miklos Szeredif88996a2015-07-01 16:26:01 +02001270 if (!list_empty(&fiq->interrupts)) {
1271 req = list_entry(fiq->interrupts.next, struct fuse_req,
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001272 intr_entry);
Miklos Szeredifd22d622015-07-01 16:26:03 +02001273 return fuse_read_interrupt(fiq, cs, nbytes, req);
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001274 }
1275
Miklos Szeredif88996a2015-07-01 16:26:01 +02001276 if (forget_pending(fiq)) {
1277 if (list_empty(&fiq->pending) || fiq->forget_batch-- > 0)
Miklos Szeredifd22d622015-07-01 16:26:03 +02001278 return fuse_read_forget(fc, fiq, cs, nbytes);
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001279
Miklos Szeredif88996a2015-07-01 16:26:01 +02001280 if (fiq->forget_batch <= -8)
1281 fiq->forget_batch = 16;
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001282 }
1283
Miklos Szeredif88996a2015-07-01 16:26:01 +02001284 req = list_entry(fiq->pending.next, struct fuse_req, list);
Miklos Szeredi33e14b42015-07-01 16:26:01 +02001285 clear_bit(FR_PENDING, &req->flags);
Miklos Szeredief759252015-07-01 16:26:02 +02001286 list_del_init(&req->list);
Miklos Szeredi4ce60812015-07-01 16:26:02 +02001287 spin_unlock(&fiq->waitq.lock);
1288
Miklos Szeredi334f4852005-09-09 13:10:27 -07001289 in = &req->in;
Miklos Szeredi1d3d7522006-01-06 00:19:40 -08001290 reqsize = in->h.len;
Miklos Szeredi5d6d3a32017-09-12 16:57:53 +02001291
Miklos Szeredi1d3d7522006-01-06 00:19:40 -08001292 /* If request is too large, reply with an error and restart the read */
Miklos Szeredic3021622010-05-25 15:06:07 +02001293 if (nbytes < reqsize) {
Miklos Szeredi1d3d7522006-01-06 00:19:40 -08001294 req->out.h.error = -EIO;
1295 /* SETXATTR is special, since it may contain too large data */
1296 if (in->h.opcode == FUSE_SETXATTR)
1297 req->out.h.error = -E2BIG;
1298 request_end(fc, req);
1299 goto restart;
Miklos Szeredi334f4852005-09-09 13:10:27 -07001300 }
Miklos Szeredi45a91cb2015-07-01 16:26:06 +02001301 spin_lock(&fpq->lock);
Miklos Szeredi82cbdcd2015-07-01 16:26:05 +02001302 list_add(&req->list, &fpq->io);
Miklos Szeredi45a91cb2015-07-01 16:26:06 +02001303 spin_unlock(&fpq->lock);
Miklos Szeredic3021622010-05-25 15:06:07 +02001304 cs->req = req;
1305 err = fuse_copy_one(cs, &in->h, sizeof(in->h));
Miklos Szeredi1d3d7522006-01-06 00:19:40 -08001306 if (!err)
Miklos Szeredic3021622010-05-25 15:06:07 +02001307 err = fuse_copy_args(cs, in->numargs, in->argpages,
Miklos Szeredi1d3d7522006-01-06 00:19:40 -08001308 (struct fuse_arg *) in->args, 0);
Miklos Szeredic3021622010-05-25 15:06:07 +02001309 fuse_copy_finish(cs);
Miklos Szeredi45a91cb2015-07-01 16:26:06 +02001310 spin_lock(&fpq->lock);
Miklos Szeredi825d6d32015-07-01 16:25:58 +02001311 clear_bit(FR_LOCKED, &req->flags);
Miklos Szeredie96edd92015-07-01 16:26:04 +02001312 if (!fpq->connected) {
Szymon Lukasz3b7008b2017-11-09 21:23:35 +01001313 err = (fc->aborted && fc->abort_err) ? -ECONNABORTED : -ENODEV;
Miklos Szeredi82cbdcd2015-07-01 16:26:05 +02001314 goto out_end;
Miklos Szeredic9c9d7d2007-10-16 23:31:05 -07001315 }
Miklos Szeredi334f4852005-09-09 13:10:27 -07001316 if (err) {
Miklos Szeredic9c9d7d2007-10-16 23:31:05 -07001317 req->out.h.error = -EIO;
Miklos Szeredi82cbdcd2015-07-01 16:26:05 +02001318 goto out_end;
Miklos Szeredi334f4852005-09-09 13:10:27 -07001319 }
Miklos Szeredi825d6d32015-07-01 16:25:58 +02001320 if (!test_bit(FR_ISREPLY, &req->flags)) {
Miklos Szeredi82cbdcd2015-07-01 16:26:05 +02001321 err = reqsize;
1322 goto out_end;
Miklos Szeredi334f4852005-09-09 13:10:27 -07001323 }
Miklos Szeredi82cbdcd2015-07-01 16:26:05 +02001324 list_move_tail(&req->list, &fpq->processing);
Kirill Tkhaie8e17b12018-09-25 12:28:55 +03001325 __fuse_get_request(req);
Miklos Szeredi82cbdcd2015-07-01 16:26:05 +02001326 set_bit(FR_SENT, &req->flags);
Miklos Szeredic1ef6c92018-09-28 16:43:22 +02001327 spin_unlock(&fpq->lock);
Miklos Szeredi82cbdcd2015-07-01 16:26:05 +02001328 /* matches barrier in request_wait_answer() */
1329 smp_mb__after_atomic();
1330 if (test_bit(FR_INTERRUPTED, &req->flags))
1331 queue_interrupt(fiq, req);
Kirill Tkhaie8e17b12018-09-25 12:28:55 +03001332 fuse_put_request(fc, req);
Miklos Szeredi82cbdcd2015-07-01 16:26:05 +02001333
Miklos Szeredi334f4852005-09-09 13:10:27 -07001334 return reqsize;
1335
Miklos Szeredi82cbdcd2015-07-01 16:26:05 +02001336out_end:
Miklos Szeredi77cd9d42015-07-01 16:26:06 +02001337 if (!test_bit(FR_PRIVATE, &req->flags))
1338 list_del_init(&req->list);
Miklos Szeredi45a91cb2015-07-01 16:26:06 +02001339 spin_unlock(&fpq->lock);
Miklos Szeredi82cbdcd2015-07-01 16:26:05 +02001340 request_end(fc, req);
1341 return err;
1342
Miklos Szeredi334f4852005-09-09 13:10:27 -07001343 err_unlock:
Miklos Szeredi4ce60812015-07-01 16:26:02 +02001344 spin_unlock(&fiq->waitq.lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001345 return err;
1346}
1347
Tom Van Braeckel94e4fe22015-01-12 05:22:16 +01001348static int fuse_dev_open(struct inode *inode, struct file *file)
1349{
1350 /*
1351 * The fuse device's file's private_data is used to hold
1352 * the fuse_conn(ection) when it is mounted, and is used to
1353 * keep track of whether the file has been mounted already.
1354 */
1355 file->private_data = NULL;
1356 return 0;
1357}
1358
Al Virofbdbacc2015-04-03 21:53:39 -04001359static ssize_t fuse_dev_read(struct kiocb *iocb, struct iov_iter *to)
Miklos Szeredic3021622010-05-25 15:06:07 +02001360{
1361 struct fuse_copy_state cs;
1362 struct file *file = iocb->ki_filp;
Miklos Szeredicc080e92015-07-01 16:26:08 +02001363 struct fuse_dev *fud = fuse_get_dev(file);
1364
1365 if (!fud)
Miklos Szeredic3021622010-05-25 15:06:07 +02001366 return -EPERM;
1367
Al Virofbdbacc2015-04-03 21:53:39 -04001368 if (!iter_is_iovec(to))
1369 return -EINVAL;
Miklos Szeredic3021622010-05-25 15:06:07 +02001370
Miklos Szeredidc008092015-07-01 16:25:58 +02001371 fuse_copy_init(&cs, 1, to);
Al Virofbdbacc2015-04-03 21:53:39 -04001372
Miklos Szeredic36960462015-07-01 16:26:09 +02001373 return fuse_dev_do_read(fud, file, &cs, iov_iter_count(to));
Miklos Szeredic3021622010-05-25 15:06:07 +02001374}
1375
Miklos Szeredic3021622010-05-25 15:06:07 +02001376static ssize_t fuse_dev_splice_read(struct file *in, loff_t *ppos,
1377 struct pipe_inode_info *pipe,
1378 size_t len, unsigned int flags)
1379{
Al Virod82718e2016-09-17 22:56:25 -04001380 int total, ret;
Miklos Szeredic3021622010-05-25 15:06:07 +02001381 int page_nr = 0;
Miklos Szeredic3021622010-05-25 15:06:07 +02001382 struct pipe_buffer *bufs;
1383 struct fuse_copy_state cs;
Miklos Szeredicc080e92015-07-01 16:26:08 +02001384 struct fuse_dev *fud = fuse_get_dev(in);
1385
1386 if (!fud)
Miklos Szeredic3021622010-05-25 15:06:07 +02001387 return -EPERM;
1388
Andrey Ryabinind6d931a2018-07-17 19:00:34 +03001389 bufs = kvmalloc_array(pipe->buffers, sizeof(struct pipe_buffer),
1390 GFP_KERNEL);
Miklos Szeredic3021622010-05-25 15:06:07 +02001391 if (!bufs)
1392 return -ENOMEM;
1393
Miklos Szeredidc008092015-07-01 16:25:58 +02001394 fuse_copy_init(&cs, 1, NULL);
Miklos Szeredic3021622010-05-25 15:06:07 +02001395 cs.pipebufs = bufs;
1396 cs.pipe = pipe;
Miklos Szeredic36960462015-07-01 16:26:09 +02001397 ret = fuse_dev_do_read(fud, in, &cs, len);
Miklos Szeredic3021622010-05-25 15:06:07 +02001398 if (ret < 0)
1399 goto out;
1400
Miklos Szeredic3021622010-05-25 15:06:07 +02001401 if (pipe->nrbufs + cs.nr_segs > pipe->buffers) {
1402 ret = -EIO;
Al Virod82718e2016-09-17 22:56:25 -04001403 goto out;
Miklos Szeredic3021622010-05-25 15:06:07 +02001404 }
1405
Al Virod82718e2016-09-17 22:56:25 -04001406 for (ret = total = 0; page_nr < cs.nr_segs; total += ret) {
Miklos Szeredi28a625c2014-01-22 19:36:57 +01001407 /*
1408 * Need to be careful about this. Having buf->ops in module
1409 * code can Oops if the buffer persists after module unload.
1410 */
Al Virod82718e2016-09-17 22:56:25 -04001411 bufs[page_nr].ops = &nosteal_pipe_buf_ops;
Miklos Szeredi84588a92017-02-16 15:08:20 +01001412 bufs[page_nr].flags = 0;
Al Virod82718e2016-09-17 22:56:25 -04001413 ret = add_to_pipe(pipe, &bufs[page_nr++]);
1414 if (unlikely(ret < 0))
1415 break;
Miklos Szeredic3021622010-05-25 15:06:07 +02001416 }
Al Virod82718e2016-09-17 22:56:25 -04001417 if (total)
1418 ret = total;
Miklos Szeredic3021622010-05-25 15:06:07 +02001419out:
1420 for (; page_nr < cs.nr_segs; page_nr++)
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001421 put_page(bufs[page_nr].page);
Miklos Szeredic3021622010-05-25 15:06:07 +02001422
Andrey Ryabinind6d931a2018-07-17 19:00:34 +03001423 kvfree(bufs);
Miklos Szeredic3021622010-05-25 15:06:07 +02001424 return ret;
1425}
1426
Tejun Heo95668a62008-11-26 12:03:55 +01001427static int fuse_notify_poll(struct fuse_conn *fc, unsigned int size,
1428 struct fuse_copy_state *cs)
1429{
1430 struct fuse_notify_poll_wakeup_out outarg;
Miklos Szeredif6d47a12009-01-26 15:00:59 +01001431 int err = -EINVAL;
Tejun Heo95668a62008-11-26 12:03:55 +01001432
1433 if (size != sizeof(outarg))
Miklos Szeredif6d47a12009-01-26 15:00:59 +01001434 goto err;
Tejun Heo95668a62008-11-26 12:03:55 +01001435
1436 err = fuse_copy_one(cs, &outarg, sizeof(outarg));
1437 if (err)
Miklos Szeredif6d47a12009-01-26 15:00:59 +01001438 goto err;
Tejun Heo95668a62008-11-26 12:03:55 +01001439
Miklos Szeredif6d47a12009-01-26 15:00:59 +01001440 fuse_copy_finish(cs);
Tejun Heo95668a62008-11-26 12:03:55 +01001441 return fuse_notify_poll_wakeup(fc, &outarg);
Miklos Szeredif6d47a12009-01-26 15:00:59 +01001442
1443err:
1444 fuse_copy_finish(cs);
1445 return err;
Tejun Heo95668a62008-11-26 12:03:55 +01001446}
1447
John Muir3b463ae2009-05-31 11:13:57 -04001448static int fuse_notify_inval_inode(struct fuse_conn *fc, unsigned int size,
1449 struct fuse_copy_state *cs)
1450{
1451 struct fuse_notify_inval_inode_out outarg;
1452 int err = -EINVAL;
1453
1454 if (size != sizeof(outarg))
1455 goto err;
1456
1457 err = fuse_copy_one(cs, &outarg, sizeof(outarg));
1458 if (err)
1459 goto err;
1460 fuse_copy_finish(cs);
1461
1462 down_read(&fc->killsb);
1463 err = -ENOENT;
Miklos Szeredib21dda42010-02-05 12:08:31 +01001464 if (fc->sb) {
1465 err = fuse_reverse_inval_inode(fc->sb, outarg.ino,
1466 outarg.off, outarg.len);
1467 }
John Muir3b463ae2009-05-31 11:13:57 -04001468 up_read(&fc->killsb);
1469 return err;
1470
1471err:
1472 fuse_copy_finish(cs);
1473 return err;
1474}
1475
1476static int fuse_notify_inval_entry(struct fuse_conn *fc, unsigned int size,
1477 struct fuse_copy_state *cs)
1478{
1479 struct fuse_notify_inval_entry_out outarg;
Fang Wenqib2d82ee2009-12-30 18:37:13 +08001480 int err = -ENOMEM;
1481 char *buf;
John Muir3b463ae2009-05-31 11:13:57 -04001482 struct qstr name;
1483
Fang Wenqib2d82ee2009-12-30 18:37:13 +08001484 buf = kzalloc(FUSE_NAME_MAX + 1, GFP_KERNEL);
1485 if (!buf)
1486 goto err;
1487
1488 err = -EINVAL;
John Muir3b463ae2009-05-31 11:13:57 -04001489 if (size < sizeof(outarg))
1490 goto err;
1491
1492 err = fuse_copy_one(cs, &outarg, sizeof(outarg));
1493 if (err)
1494 goto err;
1495
1496 err = -ENAMETOOLONG;
1497 if (outarg.namelen > FUSE_NAME_MAX)
1498 goto err;
1499
Miklos Szeredic2183d12011-08-24 10:20:17 +02001500 err = -EINVAL;
1501 if (size != sizeof(outarg) + outarg.namelen + 1)
1502 goto err;
1503
John Muir3b463ae2009-05-31 11:13:57 -04001504 name.name = buf;
1505 name.len = outarg.namelen;
1506 err = fuse_copy_one(cs, buf, outarg.namelen + 1);
1507 if (err)
1508 goto err;
1509 fuse_copy_finish(cs);
1510 buf[outarg.namelen] = 0;
John Muir3b463ae2009-05-31 11:13:57 -04001511
1512 down_read(&fc->killsb);
1513 err = -ENOENT;
Miklos Szeredib21dda42010-02-05 12:08:31 +01001514 if (fc->sb)
John Muir451d0f52011-12-06 21:50:06 +01001515 err = fuse_reverse_inval_entry(fc->sb, outarg.parent, 0, &name);
1516 up_read(&fc->killsb);
1517 kfree(buf);
1518 return err;
1519
1520err:
1521 kfree(buf);
1522 fuse_copy_finish(cs);
1523 return err;
1524}
1525
1526static int fuse_notify_delete(struct fuse_conn *fc, unsigned int size,
1527 struct fuse_copy_state *cs)
1528{
1529 struct fuse_notify_delete_out outarg;
1530 int err = -ENOMEM;
1531 char *buf;
1532 struct qstr name;
1533
1534 buf = kzalloc(FUSE_NAME_MAX + 1, GFP_KERNEL);
1535 if (!buf)
1536 goto err;
1537
1538 err = -EINVAL;
1539 if (size < sizeof(outarg))
1540 goto err;
1541
1542 err = fuse_copy_one(cs, &outarg, sizeof(outarg));
1543 if (err)
1544 goto err;
1545
1546 err = -ENAMETOOLONG;
1547 if (outarg.namelen > FUSE_NAME_MAX)
1548 goto err;
1549
1550 err = -EINVAL;
1551 if (size != sizeof(outarg) + outarg.namelen + 1)
1552 goto err;
1553
1554 name.name = buf;
1555 name.len = outarg.namelen;
1556 err = fuse_copy_one(cs, buf, outarg.namelen + 1);
1557 if (err)
1558 goto err;
1559 fuse_copy_finish(cs);
1560 buf[outarg.namelen] = 0;
John Muir451d0f52011-12-06 21:50:06 +01001561
1562 down_read(&fc->killsb);
1563 err = -ENOENT;
1564 if (fc->sb)
1565 err = fuse_reverse_inval_entry(fc->sb, outarg.parent,
1566 outarg.child, &name);
John Muir3b463ae2009-05-31 11:13:57 -04001567 up_read(&fc->killsb);
Fang Wenqib2d82ee2009-12-30 18:37:13 +08001568 kfree(buf);
John Muir3b463ae2009-05-31 11:13:57 -04001569 return err;
1570
1571err:
Fang Wenqib2d82ee2009-12-30 18:37:13 +08001572 kfree(buf);
John Muir3b463ae2009-05-31 11:13:57 -04001573 fuse_copy_finish(cs);
1574 return err;
1575}
1576
Miklos Szeredia1d75f22010-07-12 14:41:40 +02001577static int fuse_notify_store(struct fuse_conn *fc, unsigned int size,
1578 struct fuse_copy_state *cs)
1579{
1580 struct fuse_notify_store_out outarg;
1581 struct inode *inode;
1582 struct address_space *mapping;
1583 u64 nodeid;
1584 int err;
1585 pgoff_t index;
1586 unsigned int offset;
1587 unsigned int num;
1588 loff_t file_size;
1589 loff_t end;
1590
1591 err = -EINVAL;
1592 if (size < sizeof(outarg))
1593 goto out_finish;
1594
1595 err = fuse_copy_one(cs, &outarg, sizeof(outarg));
1596 if (err)
1597 goto out_finish;
1598
1599 err = -EINVAL;
1600 if (size - sizeof(outarg) != outarg.size)
1601 goto out_finish;
1602
1603 nodeid = outarg.nodeid;
1604
1605 down_read(&fc->killsb);
1606
1607 err = -ENOENT;
1608 if (!fc->sb)
1609 goto out_up_killsb;
1610
1611 inode = ilookup5(fc->sb, nodeid, fuse_inode_eq, &nodeid);
1612 if (!inode)
1613 goto out_up_killsb;
1614
1615 mapping = inode->i_mapping;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001616 index = outarg.offset >> PAGE_SHIFT;
1617 offset = outarg.offset & ~PAGE_MASK;
Miklos Szeredia1d75f22010-07-12 14:41:40 +02001618 file_size = i_size_read(inode);
1619 end = outarg.offset + outarg.size;
1620 if (end > file_size) {
1621 file_size = end;
1622 fuse_write_update_size(inode, file_size);
1623 }
1624
1625 num = outarg.size;
1626 while (num) {
1627 struct page *page;
1628 unsigned int this_num;
1629
1630 err = -ENOMEM;
1631 page = find_or_create_page(mapping, index,
1632 mapping_gfp_mask(mapping));
1633 if (!page)
1634 goto out_iput;
1635
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001636 this_num = min_t(unsigned, num, PAGE_SIZE - offset);
Miklos Szeredia1d75f22010-07-12 14:41:40 +02001637 err = fuse_copy_page(cs, &page, offset, this_num, 0);
Miklos Szeredi063ec1e2014-01-22 19:36:58 +01001638 if (!err && offset == 0 &&
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001639 (this_num == PAGE_SIZE || file_size == end))
Miklos Szeredia1d75f22010-07-12 14:41:40 +02001640 SetPageUptodate(page);
1641 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001642 put_page(page);
Miklos Szeredia1d75f22010-07-12 14:41:40 +02001643
1644 if (err)
1645 goto out_iput;
1646
1647 num -= this_num;
1648 offset = 0;
1649 index++;
1650 }
1651
1652 err = 0;
1653
1654out_iput:
1655 iput(inode);
1656out_up_killsb:
1657 up_read(&fc->killsb);
1658out_finish:
1659 fuse_copy_finish(cs);
1660 return err;
1661}
1662
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001663static void fuse_retrieve_end(struct fuse_conn *fc, struct fuse_req *req)
1664{
Mel Gormanc6f92f92017-11-15 17:37:55 -08001665 release_pages(req->pages, req->num_pages);
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001666}
1667
1668static int fuse_retrieve(struct fuse_conn *fc, struct inode *inode,
1669 struct fuse_notify_retrieve_out *outarg)
1670{
1671 int err;
1672 struct address_space *mapping = inode->i_mapping;
1673 struct fuse_req *req;
1674 pgoff_t index;
1675 loff_t file_size;
1676 unsigned int num;
1677 unsigned int offset;
Geert Uytterhoeven01574432010-09-30 22:06:21 +02001678 size_t total_len = 0;
Maxim Patlasov4d53dc92012-10-26 19:48:42 +04001679 int num_pages;
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001680
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001681 offset = outarg->offset & ~PAGE_MASK;
Maxim Patlasov4d53dc92012-10-26 19:48:42 +04001682 file_size = i_size_read(inode);
1683
Kirill Smelkovae35c322019-03-27 10:15:19 +00001684 num = min(outarg->size, fc->max_write);
Maxim Patlasov4d53dc92012-10-26 19:48:42 +04001685 if (outarg->offset > file_size)
1686 num = 0;
1687 else if (outarg->offset + num > file_size)
1688 num = file_size - outarg->offset;
1689
1690 num_pages = (num + offset + PAGE_SIZE - 1) >> PAGE_SHIFT;
1691 num_pages = min(num_pages, FUSE_MAX_PAGES_PER_REQ);
1692
1693 req = fuse_get_req(fc, num_pages);
1694 if (IS_ERR(req))
1695 return PTR_ERR(req);
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001696
1697 req->in.h.opcode = FUSE_NOTIFY_REPLY;
1698 req->in.h.nodeid = outarg->nodeid;
1699 req->in.numargs = 2;
1700 req->in.argpages = 1;
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001701 req->end = fuse_retrieve_end;
1702
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001703 index = outarg->offset >> PAGE_SHIFT;
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001704
Maxim Patlasov4d53dc92012-10-26 19:48:42 +04001705 while (num && req->num_pages < num_pages) {
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001706 struct page *page;
1707 unsigned int this_num;
1708
1709 page = find_get_page(mapping, index);
1710 if (!page)
1711 break;
1712
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001713 this_num = min_t(unsigned, num, PAGE_SIZE - offset);
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001714 req->pages[req->num_pages] = page;
Miklos Szeredi6ccc9e12019-01-16 10:27:59 +01001715 req->page_descs[req->num_pages].offset = offset;
Maxim Patlasov85f40ae2012-10-26 19:49:33 +04001716 req->page_descs[req->num_pages].length = this_num;
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001717 req->num_pages++;
1718
Miklos Szeredic9e67d42012-09-04 18:45:54 +02001719 offset = 0;
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001720 num -= this_num;
1721 total_len += this_num;
Miklos Szeredi48706d02011-12-13 10:36:59 +01001722 index++;
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001723 }
1724 req->misc.retrieve_in.offset = outarg->offset;
1725 req->misc.retrieve_in.size = total_len;
1726 req->in.args[0].size = sizeof(req->misc.retrieve_in);
1727 req->in.args[0].value = &req->misc.retrieve_in;
1728 req->in.args[1].size = total_len;
1729
1730 err = fuse_request_send_notify_reply(fc, req, outarg->notify_unique);
Miklos Szeredi280da472018-11-09 15:52:16 +01001731 if (err) {
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001732 fuse_retrieve_end(fc, req);
Miklos Szeredi280da472018-11-09 15:52:16 +01001733 fuse_put_request(fc, req);
1734 }
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001735
1736 return err;
1737}
1738
1739static int fuse_notify_retrieve(struct fuse_conn *fc, unsigned int size,
1740 struct fuse_copy_state *cs)
1741{
1742 struct fuse_notify_retrieve_out outarg;
1743 struct inode *inode;
1744 int err;
1745
1746 err = -EINVAL;
1747 if (size != sizeof(outarg))
1748 goto copy_finish;
1749
1750 err = fuse_copy_one(cs, &outarg, sizeof(outarg));
1751 if (err)
1752 goto copy_finish;
1753
1754 fuse_copy_finish(cs);
1755
1756 down_read(&fc->killsb);
1757 err = -ENOENT;
1758 if (fc->sb) {
1759 u64 nodeid = outarg.nodeid;
1760
1761 inode = ilookup5(fc->sb, nodeid, fuse_inode_eq, &nodeid);
1762 if (inode) {
1763 err = fuse_retrieve(fc, inode, &outarg);
1764 iput(inode);
1765 }
1766 }
1767 up_read(&fc->killsb);
1768
1769 return err;
1770
1771copy_finish:
1772 fuse_copy_finish(cs);
1773 return err;
1774}
1775
Tejun Heo85993962008-11-26 12:03:55 +01001776static int fuse_notify(struct fuse_conn *fc, enum fuse_notify_code code,
1777 unsigned int size, struct fuse_copy_state *cs)
1778{
Miklos Szeredi0d278362015-02-26 11:45:47 +01001779 /* Don't try to move pages (yet) */
1780 cs->move_pages = 0;
1781
Tejun Heo85993962008-11-26 12:03:55 +01001782 switch (code) {
Tejun Heo95668a62008-11-26 12:03:55 +01001783 case FUSE_NOTIFY_POLL:
1784 return fuse_notify_poll(fc, size, cs);
1785
John Muir3b463ae2009-05-31 11:13:57 -04001786 case FUSE_NOTIFY_INVAL_INODE:
1787 return fuse_notify_inval_inode(fc, size, cs);
1788
1789 case FUSE_NOTIFY_INVAL_ENTRY:
1790 return fuse_notify_inval_entry(fc, size, cs);
1791
Miklos Szeredia1d75f22010-07-12 14:41:40 +02001792 case FUSE_NOTIFY_STORE:
1793 return fuse_notify_store(fc, size, cs);
1794
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001795 case FUSE_NOTIFY_RETRIEVE:
1796 return fuse_notify_retrieve(fc, size, cs);
1797
John Muir451d0f52011-12-06 21:50:06 +01001798 case FUSE_NOTIFY_DELETE:
1799 return fuse_notify_delete(fc, size, cs);
1800
Tejun Heo85993962008-11-26 12:03:55 +01001801 default:
Miklos Szeredif6d47a12009-01-26 15:00:59 +01001802 fuse_copy_finish(cs);
Tejun Heo85993962008-11-26 12:03:55 +01001803 return -EINVAL;
1804 }
1805}
1806
Miklos Szeredi334f4852005-09-09 13:10:27 -07001807/* Look up request on processing list by unique ID */
Miklos Szeredi3a2b5b92015-07-01 16:26:04 +02001808static struct fuse_req *request_find(struct fuse_pqueue *fpq, u64 unique)
Miklos Szeredi334f4852005-09-09 13:10:27 -07001809{
Dong Fang05726ac2013-07-30 22:50:01 -04001810 struct fuse_req *req;
Miklos Szeredi334f4852005-09-09 13:10:27 -07001811
Miklos Szeredi3a2b5b92015-07-01 16:26:04 +02001812 list_for_each_entry(req, &fpq->processing, list) {
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001813 if (req->in.h.unique == unique || req->intr_unique == unique)
Miklos Szeredi334f4852005-09-09 13:10:27 -07001814 return req;
1815 }
1816 return NULL;
1817}
1818
1819static int copy_out_args(struct fuse_copy_state *cs, struct fuse_out *out,
1820 unsigned nbytes)
1821{
1822 unsigned reqsize = sizeof(struct fuse_out_header);
1823
1824 if (out->h.error)
1825 return nbytes != reqsize ? -EINVAL : 0;
1826
1827 reqsize += len_args(out->numargs, out->args);
1828
1829 if (reqsize < nbytes || (reqsize > nbytes && !out->argvar))
1830 return -EINVAL;
1831 else if (reqsize > nbytes) {
1832 struct fuse_arg *lastarg = &out->args[out->numargs-1];
1833 unsigned diffsize = reqsize - nbytes;
1834 if (diffsize > lastarg->size)
1835 return -EINVAL;
1836 lastarg->size -= diffsize;
1837 }
1838 return fuse_copy_args(cs, out->numargs, out->argpages, out->args,
1839 out->page_zeroing);
1840}
1841
1842/*
1843 * Write a single reply to a request. First the header is copied from
1844 * the write buffer. The request is then searched on the processing
1845 * list by the unique ID found in the header. If found, then remove
1846 * it from the list and copy the rest of the buffer to the request.
1847 * The request is finished by calling request_end()
1848 */
Miklos Szeredic36960462015-07-01 16:26:09 +02001849static ssize_t fuse_dev_do_write(struct fuse_dev *fud,
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001850 struct fuse_copy_state *cs, size_t nbytes)
Miklos Szeredi334f4852005-09-09 13:10:27 -07001851{
1852 int err;
Miklos Szeredic36960462015-07-01 16:26:09 +02001853 struct fuse_conn *fc = fud->fc;
1854 struct fuse_pqueue *fpq = &fud->pq;
Miklos Szeredi334f4852005-09-09 13:10:27 -07001855 struct fuse_req *req;
1856 struct fuse_out_header oh;
Miklos Szeredi334f4852005-09-09 13:10:27 -07001857
Miklos Szeredi334f4852005-09-09 13:10:27 -07001858 if (nbytes < sizeof(struct fuse_out_header))
1859 return -EINVAL;
1860
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001861 err = fuse_copy_one(cs, &oh, sizeof(oh));
Miklos Szeredi334f4852005-09-09 13:10:27 -07001862 if (err)
1863 goto err_finish;
Tejun Heo85993962008-11-26 12:03:55 +01001864
Miklos Szeredi334f4852005-09-09 13:10:27 -07001865 err = -EINVAL;
Tejun Heo85993962008-11-26 12:03:55 +01001866 if (oh.len != nbytes)
1867 goto err_finish;
1868
1869 /*
1870 * Zero oh.unique indicates unsolicited notification message
1871 * and error contains notification code.
1872 */
1873 if (!oh.unique) {
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001874 err = fuse_notify(fc, oh.error, nbytes - sizeof(oh), cs);
Tejun Heo85993962008-11-26 12:03:55 +01001875 return err ? err : nbytes;
1876 }
1877
1878 err = -EINVAL;
1879 if (oh.error <= -1000 || oh.error > 0)
Miklos Szeredi334f4852005-09-09 13:10:27 -07001880 goto err_finish;
1881
Miklos Szeredi45a91cb2015-07-01 16:26:06 +02001882 spin_lock(&fpq->lock);
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08001883 err = -ENOENT;
Miklos Szeredie96edd92015-07-01 16:26:04 +02001884 if (!fpq->connected)
Miklos Szeredi45a91cb2015-07-01 16:26:06 +02001885 goto err_unlock_pq;
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08001886
Miklos Szeredi3a2b5b92015-07-01 16:26:04 +02001887 req = request_find(fpq, oh.unique);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001888 if (!req)
Miklos Szeredi45a91cb2015-07-01 16:26:06 +02001889 goto err_unlock_pq;
Miklos Szeredi334f4852005-09-09 13:10:27 -07001890
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001891 /* Is it an interrupt reply? */
1892 if (req->intr_unique == oh.unique) {
Kirill Tkhai569fda52018-09-25 12:52:42 +03001893 __fuse_get_request(req);
Miklos Szeredi45a91cb2015-07-01 16:26:06 +02001894 spin_unlock(&fpq->lock);
1895
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001896 err = -EINVAL;
Kirill Tkhai569fda52018-09-25 12:52:42 +03001897 if (nbytes != sizeof(struct fuse_out_header)) {
1898 fuse_put_request(fc, req);
Miklos Szeredi46c34a32015-07-01 16:26:07 +02001899 goto err_finish;
Kirill Tkhai569fda52018-09-25 12:52:42 +03001900 }
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001901
1902 if (oh.error == -ENOSYS)
1903 fc->no_interrupt = 1;
1904 else if (oh.error == -EAGAIN)
Miklos Szeredif88996a2015-07-01 16:26:01 +02001905 queue_interrupt(&fc->iq, req);
Kirill Tkhai569fda52018-09-25 12:52:42 +03001906 fuse_put_request(fc, req);
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001907
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001908 fuse_copy_finish(cs);
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001909 return nbytes;
1910 }
1911
Miklos Szeredi33e14b42015-07-01 16:26:01 +02001912 clear_bit(FR_SENT, &req->flags);
Miklos Szeredi3a2b5b92015-07-01 16:26:04 +02001913 list_move(&req->list, &fpq->io);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001914 req->out.h = oh;
Miklos Szeredi825d6d32015-07-01 16:25:58 +02001915 set_bit(FR_LOCKED, &req->flags);
Miklos Szeredi45a91cb2015-07-01 16:26:06 +02001916 spin_unlock(&fpq->lock);
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001917 cs->req = req;
Miklos Szeredice534fb2010-05-25 15:06:07 +02001918 if (!req->out.page_replace)
1919 cs->move_pages = 0;
Miklos Szeredi334f4852005-09-09 13:10:27 -07001920
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001921 err = copy_out_args(cs, &req->out, nbytes);
1922 fuse_copy_finish(cs);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001923
Miklos Szeredi45a91cb2015-07-01 16:26:06 +02001924 spin_lock(&fpq->lock);
Miklos Szeredi825d6d32015-07-01 16:25:58 +02001925 clear_bit(FR_LOCKED, &req->flags);
Miklos Szeredie96edd92015-07-01 16:26:04 +02001926 if (!fpq->connected)
Miklos Szeredi0d8e84b2015-07-01 16:25:58 +02001927 err = -ENOENT;
1928 else if (err)
Miklos Szeredi334f4852005-09-09 13:10:27 -07001929 req->out.h.error = -EIO;
Miklos Szeredi77cd9d42015-07-01 16:26:06 +02001930 if (!test_bit(FR_PRIVATE, &req->flags))
1931 list_del_init(&req->list);
Miklos Szeredi45a91cb2015-07-01 16:26:06 +02001932 spin_unlock(&fpq->lock);
Miklos Szeredi46c34a32015-07-01 16:26:07 +02001933
Miklos Szeredi334f4852005-09-09 13:10:27 -07001934 request_end(fc, req);
1935
1936 return err ? err : nbytes;
1937
Miklos Szeredi45a91cb2015-07-01 16:26:06 +02001938 err_unlock_pq:
1939 spin_unlock(&fpq->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001940 err_finish:
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001941 fuse_copy_finish(cs);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001942 return err;
1943}
1944
Al Virofbdbacc2015-04-03 21:53:39 -04001945static ssize_t fuse_dev_write(struct kiocb *iocb, struct iov_iter *from)
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001946{
1947 struct fuse_copy_state cs;
Miklos Szeredicc080e92015-07-01 16:26:08 +02001948 struct fuse_dev *fud = fuse_get_dev(iocb->ki_filp);
1949
1950 if (!fud)
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001951 return -EPERM;
1952
Al Virofbdbacc2015-04-03 21:53:39 -04001953 if (!iter_is_iovec(from))
1954 return -EINVAL;
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001955
Miklos Szeredidc008092015-07-01 16:25:58 +02001956 fuse_copy_init(&cs, 0, from);
Al Virofbdbacc2015-04-03 21:53:39 -04001957
Miklos Szeredic36960462015-07-01 16:26:09 +02001958 return fuse_dev_do_write(fud, &cs, iov_iter_count(from));
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001959}
1960
1961static ssize_t fuse_dev_splice_write(struct pipe_inode_info *pipe,
1962 struct file *out, loff_t *ppos,
1963 size_t len, unsigned int flags)
1964{
1965 unsigned nbuf;
1966 unsigned idx;
1967 struct pipe_buffer *bufs;
1968 struct fuse_copy_state cs;
Miklos Szeredicc080e92015-07-01 16:26:08 +02001969 struct fuse_dev *fud;
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001970 size_t rem;
1971 ssize_t ret;
1972
Miklos Szeredicc080e92015-07-01 16:26:08 +02001973 fud = fuse_get_dev(out);
1974 if (!fud)
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001975 return -EPERM;
1976
Andrey Ryabinina2477b02018-07-17 19:00:33 +03001977 pipe_lock(pipe);
1978
Andrey Ryabinin96354532018-07-17 19:00:35 +03001979 bufs = kvmalloc_array(pipe->nrbufs, sizeof(struct pipe_buffer),
Andrey Ryabinind6d931a2018-07-17 19:00:34 +03001980 GFP_KERNEL);
Andrey Ryabinina2477b02018-07-17 19:00:33 +03001981 if (!bufs) {
1982 pipe_unlock(pipe);
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001983 return -ENOMEM;
Andrey Ryabinina2477b02018-07-17 19:00:33 +03001984 }
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001985
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001986 nbuf = 0;
1987 rem = 0;
1988 for (idx = 0; idx < pipe->nrbufs && rem < len; idx++)
1989 rem += pipe->bufs[(pipe->curbuf + idx) & (pipe->buffers - 1)].len;
1990
1991 ret = -EINVAL;
Matthew Wilcox0311ff82019-04-05 14:02:10 -07001992 if (rem < len)
1993 goto out_free;
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001994
1995 rem = len;
1996 while (rem) {
1997 struct pipe_buffer *ibuf;
1998 struct pipe_buffer *obuf;
1999
2000 BUG_ON(nbuf >= pipe->buffers);
2001 BUG_ON(!pipe->nrbufs);
2002 ibuf = &pipe->bufs[pipe->curbuf];
2003 obuf = &bufs[nbuf];
2004
2005 if (rem >= ibuf->len) {
2006 *obuf = *ibuf;
2007 ibuf->ops = NULL;
2008 pipe->curbuf = (pipe->curbuf + 1) & (pipe->buffers - 1);
2009 pipe->nrbufs--;
2010 } else {
Matthew Wilcox0311ff82019-04-05 14:02:10 -07002011 if (!pipe_buf_get(pipe, ibuf))
2012 goto out_free;
2013
Miklos Szeredidd3bb142010-05-25 15:06:06 +02002014 *obuf = *ibuf;
2015 obuf->flags &= ~PIPE_BUF_FLAG_GIFT;
2016 obuf->len = rem;
2017 ibuf->offset += obuf->len;
2018 ibuf->len -= obuf->len;
2019 }
2020 nbuf++;
2021 rem -= obuf->len;
2022 }
2023 pipe_unlock(pipe);
2024
Miklos Szeredidc008092015-07-01 16:25:58 +02002025 fuse_copy_init(&cs, 0, NULL);
Miklos Szeredidd3bb142010-05-25 15:06:06 +02002026 cs.pipebufs = bufs;
Al Viro6c09e942015-04-03 22:06:08 -04002027 cs.nr_segs = nbuf;
Miklos Szeredidd3bb142010-05-25 15:06:06 +02002028 cs.pipe = pipe;
2029
Miklos Szeredice534fb2010-05-25 15:06:07 +02002030 if (flags & SPLICE_F_MOVE)
2031 cs.move_pages = 1;
2032
Miklos Szeredic36960462015-07-01 16:26:09 +02002033 ret = fuse_dev_do_write(fud, &cs, len);
Miklos Szeredidd3bb142010-05-25 15:06:06 +02002034
Jann Horn48be0eb2019-01-12 02:39:05 +01002035 pipe_lock(pipe);
Matthew Wilcox0311ff82019-04-05 14:02:10 -07002036out_free:
Miklos Szeredia7796382016-09-27 10:45:12 +02002037 for (idx = 0; idx < nbuf; idx++)
2038 pipe_buf_release(pipe, &bufs[idx]);
Jann Horn48be0eb2019-01-12 02:39:05 +01002039 pipe_unlock(pipe);
Miklos Szeredia7796382016-09-27 10:45:12 +02002040
Andrey Ryabinind6d931a2018-07-17 19:00:34 +03002041 kvfree(bufs);
Miklos Szeredidd3bb142010-05-25 15:06:06 +02002042 return ret;
2043}
2044
Al Viro076ccb72017-07-03 01:02:18 -04002045static __poll_t fuse_dev_poll(struct file *file, poll_table *wait)
Miklos Szeredi334f4852005-09-09 13:10:27 -07002046{
Linus Torvaldsa9a08842018-02-11 14:34:03 -08002047 __poll_t mask = EPOLLOUT | EPOLLWRNORM;
Miklos Szeredif88996a2015-07-01 16:26:01 +02002048 struct fuse_iqueue *fiq;
Miklos Szeredicc080e92015-07-01 16:26:08 +02002049 struct fuse_dev *fud = fuse_get_dev(file);
2050
2051 if (!fud)
Linus Torvaldsa9a08842018-02-11 14:34:03 -08002052 return EPOLLERR;
Miklos Szeredi334f4852005-09-09 13:10:27 -07002053
Miklos Szeredicc080e92015-07-01 16:26:08 +02002054 fiq = &fud->fc->iq;
Miklos Szeredif88996a2015-07-01 16:26:01 +02002055 poll_wait(file, &fiq->waitq, wait);
Miklos Szeredi334f4852005-09-09 13:10:27 -07002056
Miklos Szeredi4ce60812015-07-01 16:26:02 +02002057 spin_lock(&fiq->waitq.lock);
Miklos Szeredie16714d2015-07-01 16:26:01 +02002058 if (!fiq->connected)
Linus Torvaldsa9a08842018-02-11 14:34:03 -08002059 mask = EPOLLERR;
Miklos Szeredif88996a2015-07-01 16:26:01 +02002060 else if (request_pending(fiq))
Linus Torvaldsa9a08842018-02-11 14:34:03 -08002061 mask |= EPOLLIN | EPOLLRDNORM;
Miklos Szeredi4ce60812015-07-01 16:26:02 +02002062 spin_unlock(&fiq->waitq.lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -07002063
2064 return mask;
2065}
2066
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002067/*
2068 * Abort all requests on the given list (pending or processing)
2069 *
Miklos Szeredid7133112006-04-10 22:54:55 -07002070 * This function releases and reacquires fc->lock
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002071 */
Miklos Szeredi334f4852005-09-09 13:10:27 -07002072static void end_requests(struct fuse_conn *fc, struct list_head *head)
2073{
2074 while (!list_empty(head)) {
2075 struct fuse_req *req;
2076 req = list_entry(head->next, struct fuse_req, list);
Miklos Szeredi334f4852005-09-09 13:10:27 -07002077 req->out.h.error = -ECONNABORTED;
Miklos Szeredi33e14b42015-07-01 16:26:01 +02002078 clear_bit(FR_SENT, &req->flags);
Miklos Szeredif377cb72015-07-01 16:26:04 +02002079 list_del_init(&req->list);
Miklos Szeredi334f4852005-09-09 13:10:27 -07002080 request_end(fc, req);
Miklos Szeredi334f4852005-09-09 13:10:27 -07002081 }
2082}
2083
Bryan Green357ccf22011-03-01 16:43:52 -08002084static void end_polls(struct fuse_conn *fc)
2085{
2086 struct rb_node *p;
2087
2088 p = rb_first(&fc->polled_files);
2089
2090 while (p) {
2091 struct fuse_file *ff;
2092 ff = rb_entry(p, struct fuse_file, polled_node);
2093 wake_up_interruptible_all(&ff->poll_wait);
2094
2095 p = rb_next(p);
2096 }
2097}
2098
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002099/*
2100 * Abort all requests.
2101 *
Miklos Szeredib716d422015-07-01 16:25:59 +02002102 * Emergency exit in case of a malicious or accidental deadlock, or just a hung
2103 * filesystem.
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002104 *
Miklos Szeredib716d422015-07-01 16:25:59 +02002105 * The same effect is usually achievable through killing the filesystem daemon
2106 * and all users of the filesystem. The exception is the combination of an
2107 * asynchronous request and the tricky deadlock (see
2108 * Documentation/filesystems/fuse.txt).
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002109 *
Miklos Szeredib716d422015-07-01 16:25:59 +02002110 * Aborting requests under I/O goes as follows: 1: Separate out unlocked
2111 * requests, they should be finished off immediately. Locked requests will be
2112 * finished after unlock; see unlock_request(). 2: Finish off the unlocked
2113 * requests. It is possible that some request will finish before we can. This
2114 * is OK, the request will in that case be removed from the list before we touch
2115 * it.
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002116 */
Szymon Lukasz3b7008b2017-11-09 21:23:35 +01002117void fuse_abort_conn(struct fuse_conn *fc, bool is_abort)
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002118{
Miklos Szeredif88996a2015-07-01 16:26:01 +02002119 struct fuse_iqueue *fiq = &fc->iq;
2120
Miklos Szeredid7133112006-04-10 22:54:55 -07002121 spin_lock(&fc->lock);
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002122 if (fc->connected) {
Miklos Szeredic36960462015-07-01 16:26:09 +02002123 struct fuse_dev *fud;
Miklos Szeredib716d422015-07-01 16:25:59 +02002124 struct fuse_req *req, *next;
Miklos Szeredi75f3ee42018-07-26 16:13:12 +02002125 LIST_HEAD(to_end);
Miklos Szeredib716d422015-07-01 16:25:59 +02002126
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002127 fc->connected = 0;
Miklos Szeredi51eb01e2006-06-25 05:48:50 -07002128 fc->blocked = 0;
Szymon Lukasz3b7008b2017-11-09 21:23:35 +01002129 fc->aborted = is_abort;
Miklos Szeredi9759bd512015-01-06 10:45:35 +01002130 fuse_set_initialized(fc);
Miklos Szeredic36960462015-07-01 16:26:09 +02002131 list_for_each_entry(fud, &fc->devices, entry) {
2132 struct fuse_pqueue *fpq = &fud->pq;
2133
2134 spin_lock(&fpq->lock);
2135 fpq->connected = 0;
2136 list_for_each_entry_safe(req, next, &fpq->io, list) {
2137 req->out.h.error = -ECONNABORTED;
2138 spin_lock(&req->waitq.lock);
2139 set_bit(FR_ABORTED, &req->flags);
2140 if (!test_bit(FR_LOCKED, &req->flags)) {
2141 set_bit(FR_PRIVATE, &req->flags);
Miklos Szeredi87114372018-07-26 16:13:11 +02002142 __fuse_get_request(req);
Miklos Szeredi75f3ee42018-07-26 16:13:12 +02002143 list_move(&req->list, &to_end);
Miklos Szeredic36960462015-07-01 16:26:09 +02002144 }
2145 spin_unlock(&req->waitq.lock);
Miklos Szeredi77cd9d42015-07-01 16:26:06 +02002146 }
Miklos Szeredi75f3ee42018-07-26 16:13:12 +02002147 list_splice_tail_init(&fpq->processing, &to_end);
Miklos Szeredic36960462015-07-01 16:26:09 +02002148 spin_unlock(&fpq->lock);
Miklos Szeredib716d422015-07-01 16:25:59 +02002149 }
Miklos Szeredi41f98272015-07-01 16:25:59 +02002150 fc->max_background = UINT_MAX;
2151 flush_bg_queue(fc);
Miklos Szeredi8c911892015-07-01 16:26:02 +02002152
Miklos Szeredi4ce60812015-07-01 16:26:02 +02002153 spin_lock(&fiq->waitq.lock);
Miklos Szeredi8c911892015-07-01 16:26:02 +02002154 fiq->connected = 0;
Miklos Szeredi75f3ee42018-07-26 16:13:12 +02002155 list_for_each_entry(req, &fiq->pending, list)
Tahsin Erdogana8a86d72017-01-12 12:04:04 -08002156 clear_bit(FR_PENDING, &req->flags);
Miklos Szeredi75f3ee42018-07-26 16:13:12 +02002157 list_splice_tail_init(&fiq->pending, &to_end);
Miklos Szeredi8c911892015-07-01 16:26:02 +02002158 while (forget_pending(fiq))
2159 kfree(dequeue_forget(fiq, 1, NULL));
Miklos Szeredi4ce60812015-07-01 16:26:02 +02002160 wake_up_all_locked(&fiq->waitq);
2161 spin_unlock(&fiq->waitq.lock);
Miklos Szeredi8c911892015-07-01 16:26:02 +02002162 kill_fasync(&fiq->fasync, SIGIO, POLL_IN);
Miklos Szerediee314a82015-07-01 16:26:08 +02002163 end_polls(fc);
2164 wake_up_all(&fc->blocked_waitq);
2165 spin_unlock(&fc->lock);
Miklos Szeredi8c911892015-07-01 16:26:02 +02002166
Miklos Szeredi75f3ee42018-07-26 16:13:12 +02002167 end_requests(fc, &to_end);
Miklos Szerediee314a82015-07-01 16:26:08 +02002168 } else {
2169 spin_unlock(&fc->lock);
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002170 }
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002171}
Tejun Heo08cbf542009-04-14 10:54:53 +09002172EXPORT_SYMBOL_GPL(fuse_abort_conn);
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002173
Miklos Szeredib8f95e52018-07-26 16:13:11 +02002174void fuse_wait_aborted(struct fuse_conn *fc)
2175{
Miklos Szeredi18cd6102018-11-09 15:52:16 +01002176 /* matches implicit memory barrier in fuse_drop_waiting() */
2177 smp_mb();
Miklos Szeredib8f95e52018-07-26 16:13:11 +02002178 wait_event(fc->blocked_waitq, atomic_read(&fc->num_waiting) == 0);
2179}
2180
Tejun Heo08cbf542009-04-14 10:54:53 +09002181int fuse_dev_release(struct inode *inode, struct file *file)
Miklos Szeredi334f4852005-09-09 13:10:27 -07002182{
Miklos Szeredicc080e92015-07-01 16:26:08 +02002183 struct fuse_dev *fud = fuse_get_dev(file);
2184
2185 if (fud) {
2186 struct fuse_conn *fc = fud->fc;
Miklos Szeredic36960462015-07-01 16:26:09 +02002187 struct fuse_pqueue *fpq = &fud->pq;
Miklos Szeredi45ff3502018-07-26 16:13:11 +02002188 LIST_HEAD(to_end);
Miklos Szeredicc080e92015-07-01 16:26:08 +02002189
Miklos Szeredi45ff3502018-07-26 16:13:11 +02002190 spin_lock(&fpq->lock);
Miklos Szeredic36960462015-07-01 16:26:09 +02002191 WARN_ON(!list_empty(&fpq->io));
Miklos Szeredi45ff3502018-07-26 16:13:11 +02002192 list_splice_init(&fpq->processing, &to_end);
2193 spin_unlock(&fpq->lock);
2194
2195 end_requests(fc, &to_end);
2196
Miklos Szeredic36960462015-07-01 16:26:09 +02002197 /* Are we the last open device? */
2198 if (atomic_dec_and_test(&fc->dev_count)) {
2199 WARN_ON(fc->iq.fasync != NULL);
Szymon Lukasz3b7008b2017-11-09 21:23:35 +01002200 fuse_abort_conn(fc, false);
Miklos Szeredic36960462015-07-01 16:26:09 +02002201 }
Miklos Szeredicc080e92015-07-01 16:26:08 +02002202 fuse_dev_free(fud);
Jeff Dike385a17b2006-04-10 22:54:52 -07002203 }
Miklos Szeredi334f4852005-09-09 13:10:27 -07002204 return 0;
2205}
Tejun Heo08cbf542009-04-14 10:54:53 +09002206EXPORT_SYMBOL_GPL(fuse_dev_release);
Miklos Szeredi334f4852005-09-09 13:10:27 -07002207
Jeff Dike385a17b2006-04-10 22:54:52 -07002208static int fuse_dev_fasync(int fd, struct file *file, int on)
2209{
Miklos Szeredicc080e92015-07-01 16:26:08 +02002210 struct fuse_dev *fud = fuse_get_dev(file);
2211
2212 if (!fud)
Miklos Szeredia87046d2006-04-10 22:54:56 -07002213 return -EPERM;
Jeff Dike385a17b2006-04-10 22:54:52 -07002214
2215 /* No locking - fasync_helper does its own locking */
Miklos Szeredicc080e92015-07-01 16:26:08 +02002216 return fasync_helper(fd, file, on, &fud->fc->iq.fasync);
Jeff Dike385a17b2006-04-10 22:54:52 -07002217}
2218
Miklos Szeredi00c570f2015-07-01 16:26:08 +02002219static int fuse_device_clone(struct fuse_conn *fc, struct file *new)
2220{
Miklos Szeredicc080e92015-07-01 16:26:08 +02002221 struct fuse_dev *fud;
2222
Miklos Szeredi00c570f2015-07-01 16:26:08 +02002223 if (new->private_data)
2224 return -EINVAL;
2225
Miklos Szeredicc080e92015-07-01 16:26:08 +02002226 fud = fuse_dev_alloc(fc);
2227 if (!fud)
2228 return -ENOMEM;
2229
2230 new->private_data = fud;
Miklos Szeredic36960462015-07-01 16:26:09 +02002231 atomic_inc(&fc->dev_count);
Miklos Szeredi00c570f2015-07-01 16:26:08 +02002232
2233 return 0;
2234}
2235
2236static long fuse_dev_ioctl(struct file *file, unsigned int cmd,
2237 unsigned long arg)
2238{
2239 int err = -ENOTTY;
2240
2241 if (cmd == FUSE_DEV_IOC_CLONE) {
2242 int oldfd;
2243
2244 err = -EFAULT;
2245 if (!get_user(oldfd, (__u32 __user *) arg)) {
2246 struct file *old = fget(oldfd);
2247
2248 err = -EINVAL;
2249 if (old) {
Jann Horn8ed1f0e2015-08-16 20:27:01 +02002250 struct fuse_dev *fud = NULL;
2251
2252 /*
2253 * Check against file->f_op because CUSE
2254 * uses the same ioctl handler.
2255 */
2256 if (old->f_op == file->f_op &&
2257 old->f_cred->user_ns == file->f_cred->user_ns)
2258 fud = fuse_get_dev(old);
Miklos Szeredi00c570f2015-07-01 16:26:08 +02002259
Miklos Szeredicc080e92015-07-01 16:26:08 +02002260 if (fud) {
Miklos Szeredi00c570f2015-07-01 16:26:08 +02002261 mutex_lock(&fuse_mutex);
Miklos Szeredicc080e92015-07-01 16:26:08 +02002262 err = fuse_device_clone(fud->fc, file);
Miklos Szeredi00c570f2015-07-01 16:26:08 +02002263 mutex_unlock(&fuse_mutex);
2264 }
2265 fput(old);
2266 }
2267 }
2268 }
2269 return err;
2270}
2271
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -08002272const struct file_operations fuse_dev_operations = {
Miklos Szeredi334f4852005-09-09 13:10:27 -07002273 .owner = THIS_MODULE,
Tom Van Braeckel94e4fe22015-01-12 05:22:16 +01002274 .open = fuse_dev_open,
Miklos Szeredi334f4852005-09-09 13:10:27 -07002275 .llseek = no_llseek,
Al Virofbdbacc2015-04-03 21:53:39 -04002276 .read_iter = fuse_dev_read,
Miklos Szeredic3021622010-05-25 15:06:07 +02002277 .splice_read = fuse_dev_splice_read,
Al Virofbdbacc2015-04-03 21:53:39 -04002278 .write_iter = fuse_dev_write,
Miklos Szeredidd3bb142010-05-25 15:06:06 +02002279 .splice_write = fuse_dev_splice_write,
Miklos Szeredi334f4852005-09-09 13:10:27 -07002280 .poll = fuse_dev_poll,
2281 .release = fuse_dev_release,
Jeff Dike385a17b2006-04-10 22:54:52 -07002282 .fasync = fuse_dev_fasync,
Miklos Szeredi00c570f2015-07-01 16:26:08 +02002283 .unlocked_ioctl = fuse_dev_ioctl,
2284 .compat_ioctl = fuse_dev_ioctl,
Miklos Szeredi334f4852005-09-09 13:10:27 -07002285};
Tejun Heo08cbf542009-04-14 10:54:53 +09002286EXPORT_SYMBOL_GPL(fuse_dev_operations);
Miklos Szeredi334f4852005-09-09 13:10:27 -07002287
2288static struct miscdevice fuse_miscdevice = {
2289 .minor = FUSE_MINOR,
2290 .name = "fuse",
2291 .fops = &fuse_dev_operations,
2292};
2293
2294int __init fuse_dev_init(void)
2295{
2296 int err = -ENOMEM;
2297 fuse_req_cachep = kmem_cache_create("fuse_request",
2298 sizeof(struct fuse_req),
Paul Mundt20c2df82007-07-20 10:11:58 +09002299 0, 0, NULL);
Miklos Szeredi334f4852005-09-09 13:10:27 -07002300 if (!fuse_req_cachep)
2301 goto out;
2302
2303 err = misc_register(&fuse_miscdevice);
2304 if (err)
2305 goto out_cache_clean;
2306
2307 return 0;
2308
2309 out_cache_clean:
2310 kmem_cache_destroy(fuse_req_cachep);
2311 out:
2312 return err;
2313}
2314
2315void fuse_dev_cleanup(void)
2316{
2317 misc_deregister(&fuse_miscdevice);
2318 kmem_cache_destroy(fuse_req_cachep);
2319}