blob: fefb9dd8a2f479b8f43b7a146e3ebb8f1cb07cb8 [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
Kirill Tkhaic59fd852018-09-11 13:11:56 +030028/* Ordinary requests have even IDs, while interrupts IDs are odd */
29#define FUSE_INT_REQ_BIT (1ULL << 0)
30#define FUSE_REQ_ID_STEP (1ULL << 1)
31
Christoph Lametere18b8902006-12-06 20:33:20 -080032static struct kmem_cache *fuse_req_cachep;
Miklos Szeredi334f4852005-09-09 13:10:27 -070033
Miklos Szeredicc080e92015-07-01 16:26:08 +020034static struct fuse_dev *fuse_get_dev(struct file *file)
Miklos Szeredi334f4852005-09-09 13:10:27 -070035{
Miklos Szeredi0720b312006-04-10 22:54:55 -070036 /*
37 * Lockless access is OK, because file->private data is set
38 * once during mount and is valid until the file is released.
39 */
Mark Rutland6aa7de02017-10-23 14:07:29 -070040 return READ_ONCE(file->private_data);
Miklos Szeredi334f4852005-09-09 13:10:27 -070041}
42
Maxim Patlasov4250c062012-10-26 19:48:07 +040043static void fuse_request_init(struct fuse_req *req, struct page **pages,
Maxim Patlasovb2430d72012-10-26 19:49:24 +040044 struct fuse_page_desc *page_descs,
Maxim Patlasov4250c062012-10-26 19:48:07 +040045 unsigned npages)
Miklos Szeredi334f4852005-09-09 13:10:27 -070046{
Miklos Szeredi334f4852005-09-09 13:10:27 -070047 INIT_LIST_HEAD(&req->list);
Miklos Szeredia4d27e72006-06-25 05:48:54 -070048 INIT_LIST_HEAD(&req->intr_entry);
Miklos Szeredi334f4852005-09-09 13:10:27 -070049 init_waitqueue_head(&req->waitq);
Elena Reshetovaec99f6d2017-03-03 11:04:04 +020050 refcount_set(&req->count, 1);
Maxim Patlasov4250c062012-10-26 19:48:07 +040051 req->pages = pages;
Maxim Patlasovb2430d72012-10-26 19:49:24 +040052 req->page_descs = page_descs;
Maxim Patlasov4250c062012-10-26 19:48:07 +040053 req->max_pages = npages;
Miklos Szeredi33e14b42015-07-01 16:26:01 +020054 __set_bit(FR_PENDING, &req->flags);
Miklos Szeredi334f4852005-09-09 13:10:27 -070055}
56
Maxim Patlasov4250c062012-10-26 19:48:07 +040057static struct fuse_req *__fuse_request_alloc(unsigned npages, gfp_t flags)
Miklos Szeredi334f4852005-09-09 13:10:27 -070058{
Miklos Szeredi8a7aa282018-10-01 10:07:05 +020059 struct fuse_req *req = kmem_cache_zalloc(fuse_req_cachep, flags);
Maxim Patlasov4250c062012-10-26 19:48:07 +040060 if (req) {
Miklos Szeredi8a7aa282018-10-01 10:07:05 +020061 struct page **pages = NULL;
62 struct fuse_page_desc *page_descs = NULL;
Maxim Patlasov4250c062012-10-26 19:48:07 +040063
Miklos Szeredi8a7aa282018-10-01 10:07:05 +020064 if (npages > FUSE_REQ_INLINE_PAGES) {
65 pages = kzalloc(npages * (sizeof(*pages) +
66 sizeof(*page_descs)), flags);
67 if (!pages) {
68 kmem_cache_free(fuse_req_cachep, req);
69 return NULL;
70 }
71 page_descs = (void *) pages + npages * sizeof(*pages);
72 } else if (npages) {
Maxim Patlasov4250c062012-10-26 19:48:07 +040073 pages = req->inline_pages;
Maxim Patlasovb2430d72012-10-26 19:49:24 +040074 page_descs = req->inline_page_descs;
Maxim Patlasov4250c062012-10-26 19:48:07 +040075 }
76
Maxim Patlasovb2430d72012-10-26 19:49:24 +040077 fuse_request_init(req, pages, page_descs, npages);
Maxim Patlasov4250c062012-10-26 19:48:07 +040078 }
Miklos Szeredi334f4852005-09-09 13:10:27 -070079 return req;
80}
Maxim Patlasov4250c062012-10-26 19:48:07 +040081
82struct fuse_req *fuse_request_alloc(unsigned npages)
83{
84 return __fuse_request_alloc(npages, GFP_KERNEL);
85}
Tejun Heo08cbf542009-04-14 10:54:53 +090086EXPORT_SYMBOL_GPL(fuse_request_alloc);
Miklos Szeredi334f4852005-09-09 13:10:27 -070087
Maxim Patlasov4250c062012-10-26 19:48:07 +040088struct fuse_req *fuse_request_alloc_nofs(unsigned npages)
Miklos Szeredi3be5a522008-04-30 00:54:41 -070089{
Maxim Patlasov4250c062012-10-26 19:48:07 +040090 return __fuse_request_alloc(npages, GFP_NOFS);
Miklos Szeredi3be5a522008-04-30 00:54:41 -070091}
92
Miklos Szeredi334f4852005-09-09 13:10:27 -070093void fuse_request_free(struct fuse_req *req)
94{
Miklos Szeredi8a7aa282018-10-01 10:07:05 +020095 if (req->pages != req->inline_pages)
Maxim Patlasov4250c062012-10-26 19:48:07 +040096 kfree(req->pages);
Miklos Szeredi8a7aa282018-10-01 10:07:05 +020097
Miklos Szeredi334f4852005-09-09 13:10:27 -070098 kmem_cache_free(fuse_req_cachep, req);
99}
100
Maxim Patlasov36cf66e2012-12-14 19:20:51 +0400101void __fuse_get_request(struct fuse_req *req)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700102{
Elena Reshetovaec99f6d2017-03-03 11:04:04 +0200103 refcount_inc(&req->count);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700104}
105
106/* Must be called with > 1 refcount */
107static void __fuse_put_request(struct fuse_req *req)
108{
Elena Reshetovaec99f6d2017-03-03 11:04:04 +0200109 refcount_dec(&req->count);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700110}
111
Miklos Szeredi9759bd512015-01-06 10:45:35 +0100112void fuse_set_initialized(struct fuse_conn *fc)
113{
114 /* Make sure stores before this are seen on another CPU */
115 smp_wmb();
116 fc->initialized = 1;
117}
118
Maxim Patlasov0aada882013-03-21 18:02:28 +0400119static bool fuse_block_alloc(struct fuse_conn *fc, bool for_background)
120{
121 return !fc->initialized || (for_background && fc->blocked);
122}
123
Miklos Szeredib8f95e52018-07-26 16:13:11 +0200124static void fuse_drop_waiting(struct fuse_conn *fc)
125{
126 if (fc->connected) {
127 atomic_dec(&fc->num_waiting);
128 } else if (atomic_dec_and_test(&fc->num_waiting)) {
129 /* wake up aborters */
130 wake_up_all(&fc->blocked_waitq);
131 }
132}
133
Maxim Patlasov8b41e672013-03-21 18:02:04 +0400134static struct fuse_req *__fuse_get_req(struct fuse_conn *fc, unsigned npages,
135 bool for_background)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700136{
Miklos Szeredi08a53cd2006-04-10 22:54:59 -0700137 struct fuse_req *req;
Miklos Szeredi08a53cd2006-04-10 22:54:59 -0700138 int err;
Miklos Szeredi9bc5ddd2006-04-11 21:16:09 +0200139 atomic_inc(&fc->num_waiting);
Maxim Patlasov0aada882013-03-21 18:02:28 +0400140
141 if (fuse_block_alloc(fc, for_background)) {
Maxim Patlasov0aada882013-03-21 18:02:28 +0400142 err = -EINTR;
Al Viro7d3a07f2016-07-19 03:08:27 -0400143 if (wait_event_killable_exclusive(fc->blocked_waitq,
144 !fuse_block_alloc(fc, for_background)))
Maxim Patlasov0aada882013-03-21 18:02:28 +0400145 goto out;
146 }
Miklos Szeredi9759bd512015-01-06 10:45:35 +0100147 /* Matches smp_wmb() in fuse_set_initialized() */
148 smp_rmb();
Miklos Szeredi08a53cd2006-04-10 22:54:59 -0700149
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700150 err = -ENOTCONN;
151 if (!fc->connected)
152 goto out;
153
Miklos Szeredide155222015-07-01 16:25:57 +0200154 err = -ECONNREFUSED;
155 if (fc->conn_error)
156 goto out;
157
Maxim Patlasovb111c8c2012-10-26 19:48:30 +0400158 req = fuse_request_alloc(npages);
Miklos Szeredi9bc5ddd2006-04-11 21:16:09 +0200159 err = -ENOMEM;
Maxim Patlasov722d2be2013-03-21 18:02:36 +0400160 if (!req) {
161 if (for_background)
162 wake_up(&fc->blocked_waitq);
Miklos Szeredi9bc5ddd2006-04-11 21:16:09 +0200163 goto out;
Maxim Patlasov722d2be2013-03-21 18:02:36 +0400164 }
Miklos Szeredi334f4852005-09-09 13:10:27 -0700165
Eric W. Biederman8cb08322018-02-21 11:18:07 -0600166 req->in.h.uid = from_kuid(fc->user_ns, current_fsuid());
167 req->in.h.gid = from_kgid(fc->user_ns, current_fsgid());
Eric W. Biedermanc9582eb2018-02-21 10:52:06 -0600168 req->in.h.pid = pid_nr_ns(task_pid(current), fc->pid_ns);
169
Miklos Szeredi825d6d32015-07-01 16:25:58 +0200170 __set_bit(FR_WAITING, &req->flags);
171 if (for_background)
172 __set_bit(FR_BACKGROUND, &req->flags);
173
Eric W. Biedermanc9582eb2018-02-21 10:52:06 -0600174 if (unlikely(req->in.h.uid == ((uid_t)-1) ||
175 req->in.h.gid == ((gid_t)-1))) {
176 fuse_put_request(fc, req);
177 return ERR_PTR(-EOVERFLOW);
178 }
Miklos Szeredi334f4852005-09-09 13:10:27 -0700179 return req;
Miklos Szeredi9bc5ddd2006-04-11 21:16:09 +0200180
181 out:
Miklos Szeredib8f95e52018-07-26 16:13:11 +0200182 fuse_drop_waiting(fc);
Miklos Szeredi9bc5ddd2006-04-11 21:16:09 +0200183 return ERR_PTR(err);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700184}
Maxim Patlasov8b41e672013-03-21 18:02:04 +0400185
186struct fuse_req *fuse_get_req(struct fuse_conn *fc, unsigned npages)
187{
188 return __fuse_get_req(fc, npages, false);
189}
Tejun Heo08cbf542009-04-14 10:54:53 +0900190EXPORT_SYMBOL_GPL(fuse_get_req);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700191
Maxim Patlasov8b41e672013-03-21 18:02:04 +0400192struct fuse_req *fuse_get_req_for_background(struct fuse_conn *fc,
193 unsigned npages)
194{
195 return __fuse_get_req(fc, npages, true);
196}
197EXPORT_SYMBOL_GPL(fuse_get_req_for_background);
198
Miklos Szeredi33649c92006-06-25 05:48:52 -0700199/*
200 * Return request in fuse_file->reserved_req. However that may
201 * currently be in use. If that is the case, wait for it to become
202 * available.
203 */
204static struct fuse_req *get_reserved_req(struct fuse_conn *fc,
205 struct file *file)
206{
207 struct fuse_req *req = NULL;
208 struct fuse_file *ff = file->private_data;
209
210 do {
Miklos Szeredide5e3de2007-10-16 23:31:00 -0700211 wait_event(fc->reserved_req_waitq, ff->reserved_req);
Miklos Szeredi33649c92006-06-25 05:48:52 -0700212 spin_lock(&fc->lock);
213 if (ff->reserved_req) {
214 req = ff->reserved_req;
215 ff->reserved_req = NULL;
Al Virocb0942b2012-08-27 14:48:26 -0400216 req->stolen_file = get_file(file);
Miklos Szeredi33649c92006-06-25 05:48:52 -0700217 }
218 spin_unlock(&fc->lock);
219 } while (!req);
220
221 return req;
222}
223
224/*
225 * Put stolen request back into fuse_file->reserved_req
226 */
227static void put_reserved_req(struct fuse_conn *fc, struct fuse_req *req)
228{
229 struct file *file = req->stolen_file;
230 struct fuse_file *ff = file->private_data;
231
Miklos Szeredi8a7aa282018-10-01 10:07:05 +0200232 WARN_ON(req->max_pages);
Miklos Szeredi33649c92006-06-25 05:48:52 -0700233 spin_lock(&fc->lock);
Miklos Szeredi8a7aa282018-10-01 10:07:05 +0200234 memset(req, 0, sizeof(*req));
235 fuse_request_init(req, NULL, NULL, 0);
Miklos Szeredi33649c92006-06-25 05:48:52 -0700236 BUG_ON(ff->reserved_req);
237 ff->reserved_req = req;
Miklos Szeredide5e3de2007-10-16 23:31:00 -0700238 wake_up_all(&fc->reserved_req_waitq);
Miklos Szeredi33649c92006-06-25 05:48:52 -0700239 spin_unlock(&fc->lock);
240 fput(file);
241}
242
243/*
244 * Gets a requests for a file operation, always succeeds
245 *
246 * This is used for sending the FLUSH request, which must get to
247 * userspace, due to POSIX locks which may need to be unlocked.
248 *
249 * If allocation fails due to OOM, use the reserved request in
250 * fuse_file.
251 *
252 * This is very unlikely to deadlock accidentally, since the
253 * filesystem should not have it's own file open. If deadlock is
254 * intentional, it can still be broken by "aborting" the filesystem.
255 */
Maxim Patlasovb111c8c2012-10-26 19:48:30 +0400256struct fuse_req *fuse_get_req_nofail_nopages(struct fuse_conn *fc,
257 struct file *file)
Miklos Szeredi33649c92006-06-25 05:48:52 -0700258{
259 struct fuse_req *req;
260
261 atomic_inc(&fc->num_waiting);
Maxim Patlasov0aada882013-03-21 18:02:28 +0400262 wait_event(fc->blocked_waitq, fc->initialized);
Miklos Szeredi9759bd512015-01-06 10:45:35 +0100263 /* Matches smp_wmb() in fuse_set_initialized() */
264 smp_rmb();
Maxim Patlasovb111c8c2012-10-26 19:48:30 +0400265 req = fuse_request_alloc(0);
Miklos Szeredi33649c92006-06-25 05:48:52 -0700266 if (!req)
267 req = get_reserved_req(fc, file);
268
Eric W. Biederman8cb08322018-02-21 11:18:07 -0600269 req->in.h.uid = from_kuid_munged(fc->user_ns, current_fsuid());
270 req->in.h.gid = from_kgid_munged(fc->user_ns, current_fsgid());
Eric W. Biedermanc9582eb2018-02-21 10:52:06 -0600271 req->in.h.pid = pid_nr_ns(task_pid(current), fc->pid_ns);
272
Miklos Szeredi825d6d32015-07-01 16:25:58 +0200273 __set_bit(FR_WAITING, &req->flags);
274 __clear_bit(FR_BACKGROUND, &req->flags);
Miklos Szeredi33649c92006-06-25 05:48:52 -0700275 return req;
276}
277
Miklos Szeredi334f4852005-09-09 13:10:27 -0700278void fuse_put_request(struct fuse_conn *fc, struct fuse_req *req)
279{
Elena Reshetovaec99f6d2017-03-03 11:04:04 +0200280 if (refcount_dec_and_test(&req->count)) {
Miklos Szeredi825d6d32015-07-01 16:25:58 +0200281 if (test_bit(FR_BACKGROUND, &req->flags)) {
Maxim Patlasov722d2be2013-03-21 18:02:36 +0400282 /*
283 * We get here in the unlikely case that a background
284 * request was allocated but not sent
285 */
Kirill Tkhaiae2dffa2018-08-27 18:29:46 +0300286 spin_lock(&fc->bg_lock);
Maxim Patlasov722d2be2013-03-21 18:02:36 +0400287 if (!fc->blocked)
288 wake_up(&fc->blocked_waitq);
Kirill Tkhaiae2dffa2018-08-27 18:29:46 +0300289 spin_unlock(&fc->bg_lock);
Maxim Patlasov722d2be2013-03-21 18:02:36 +0400290 }
291
Miklos Szeredi825d6d32015-07-01 16:25:58 +0200292 if (test_bit(FR_WAITING, &req->flags)) {
293 __clear_bit(FR_WAITING, &req->flags);
Miklos Szeredib8f95e52018-07-26 16:13:11 +0200294 fuse_drop_waiting(fc);
Miklos Szeredi73e0e732015-07-01 16:25:56 +0200295 }
Miklos Szeredi33649c92006-06-25 05:48:52 -0700296
297 if (req->stolen_file)
298 put_reserved_req(fc, req);
299 else
300 fuse_request_free(req);
Miklos Szeredi7128ec22006-02-04 23:27:40 -0800301 }
302}
Tejun Heo08cbf542009-04-14 10:54:53 +0900303EXPORT_SYMBOL_GPL(fuse_put_request);
Miklos Szeredi7128ec22006-02-04 23:27:40 -0800304
Miklos Szeredid12def12008-02-06 01:38:39 -0800305static unsigned len_args(unsigned numargs, struct fuse_arg *args)
306{
307 unsigned nbytes = 0;
308 unsigned i;
309
310 for (i = 0; i < numargs; i++)
311 nbytes += args[i].size;
312
313 return nbytes;
314}
315
Miklos Szeredif88996a2015-07-01 16:26:01 +0200316static u64 fuse_get_unique(struct fuse_iqueue *fiq)
Miklos Szeredid12def12008-02-06 01:38:39 -0800317{
Kirill Tkhaic59fd852018-09-11 13:11:56 +0300318 fiq->reqctr += FUSE_REQ_ID_STEP;
319 return fiq->reqctr;
Miklos Szeredid12def12008-02-06 01:38:39 -0800320}
321
Kirill Tkhaibe2ff422018-09-11 13:12:14 +0300322static unsigned int fuse_req_hash(u64 unique)
323{
324 return hash_long(unique & ~FUSE_INT_REQ_BIT, FUSE_PQ_HASH_BITS);
325}
326
Miklos Szeredif88996a2015-07-01 16:26:01 +0200327static void queue_request(struct fuse_iqueue *fiq, struct fuse_req *req)
Miklos Szeredid12def12008-02-06 01:38:39 -0800328{
Miklos Szeredid12def12008-02-06 01:38:39 -0800329 req->in.h.len = sizeof(struct fuse_in_header) +
330 len_args(req->in.numargs, (struct fuse_arg *) req->in.args);
Miklos Szeredif88996a2015-07-01 16:26:01 +0200331 list_add_tail(&req->list, &fiq->pending);
Miklos Szeredi4ce60812015-07-01 16:26:02 +0200332 wake_up_locked(&fiq->waitq);
Miklos Szeredif88996a2015-07-01 16:26:01 +0200333 kill_fasync(&fiq->fasync, SIGIO, POLL_IN);
Miklos Szeredid12def12008-02-06 01:38:39 -0800334}
335
Miklos Szeredi07e77dc2010-12-07 20:16:56 +0100336void fuse_queue_forget(struct fuse_conn *fc, struct fuse_forget_link *forget,
337 u64 nodeid, u64 nlookup)
338{
Miklos Szeredif88996a2015-07-01 16:26:01 +0200339 struct fuse_iqueue *fiq = &fc->iq;
340
Miklos Szeredi02c048b2010-12-07 20:16:56 +0100341 forget->forget_one.nodeid = nodeid;
342 forget->forget_one.nlookup = nlookup;
Miklos Szeredi07e77dc2010-12-07 20:16:56 +0100343
Miklos Szeredi4ce60812015-07-01 16:26:02 +0200344 spin_lock(&fiq->waitq.lock);
Miklos Szeredie16714d2015-07-01 16:26:01 +0200345 if (fiq->connected) {
Miklos Szeredif88996a2015-07-01 16:26:01 +0200346 fiq->forget_list_tail->next = forget;
347 fiq->forget_list_tail = forget;
Miklos Szeredi4ce60812015-07-01 16:26:02 +0200348 wake_up_locked(&fiq->waitq);
Miklos Szeredif88996a2015-07-01 16:26:01 +0200349 kill_fasync(&fiq->fasync, SIGIO, POLL_IN);
Miklos Szeredi5dfcc872011-09-12 09:38:03 +0200350 } else {
351 kfree(forget);
352 }
Miklos Szeredi4ce60812015-07-01 16:26:02 +0200353 spin_unlock(&fiq->waitq.lock);
Miklos Szeredi07e77dc2010-12-07 20:16:56 +0100354}
355
Miklos Szeredid12def12008-02-06 01:38:39 -0800356static void flush_bg_queue(struct fuse_conn *fc)
357{
Kirill Tkhaie2871792018-07-31 13:25:25 +0300358 struct fuse_iqueue *fiq = &fc->iq;
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;
363
Kirill Tkhaie2871792018-07-31 13:25:25 +0300364 req = list_first_entry(&fc->bg_queue, struct fuse_req, list);
Miklos Szeredid12def12008-02-06 01:38:39 -0800365 list_del(&req->list);
366 fc->active_background++;
Miklos Szeredi4ce60812015-07-01 16:26:02 +0200367 spin_lock(&fiq->waitq.lock);
Miklos Szeredif88996a2015-07-01 16:26:01 +0200368 req->in.h.unique = fuse_get_unique(fiq);
369 queue_request(fiq, req);
Miklos Szeredi4ce60812015-07-01 16:26:02 +0200370 spin_unlock(&fiq->waitq.lock);
Miklos Szeredid12def12008-02-06 01:38:39 -0800371 }
372}
373
Miklos Szeredi6dbbcb12006-04-26 10:49:06 +0200374/*
Miklos Szeredi334f4852005-09-09 13:10:27 -0700375 * This function is called when a request is finished. Either a reply
Miklos Szeredif9a28422006-06-25 05:48:53 -0700376 * has arrived or it was aborted (and not yet sent) or some error
Miklos Szeredif43b1552006-01-16 22:14:26 -0800377 * occurred during communication with userspace, or the device file
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700378 * was closed. The requester thread is woken up (if still waiting),
379 * the 'end' callback is called if given, else the reference to the
380 * request is released
Miklos Szeredi334f4852005-09-09 13:10:27 -0700381 */
382static void request_end(struct fuse_conn *fc, struct fuse_req *req)
383{
Miklos Szeredi4ce60812015-07-01 16:26:02 +0200384 struct fuse_iqueue *fiq = &fc->iq;
Miklos Szeredi365ae712015-07-01 16:26:06 +0200385
Miklos Szerediefe28002015-07-01 16:26:07 +0200386 if (test_and_set_bit(FR_FINISHED, &req->flags))
Miklos Szeredib8f95e52018-07-26 16:13:11 +0200387 goto put_request;
Miklos Szeredi365ae712015-07-01 16:26:06 +0200388
Miklos Szeredi4ce60812015-07-01 16:26:02 +0200389 spin_lock(&fiq->waitq.lock);
Miklos Szeredi0d8e84b2015-07-01 16:25:58 +0200390 list_del_init(&req->intr_entry);
Miklos Szeredi4ce60812015-07-01 16:26:02 +0200391 spin_unlock(&fiq->waitq.lock);
Miklos Szeredi33e14b42015-07-01 16:26:01 +0200392 WARN_ON(test_bit(FR_PENDING, &req->flags));
393 WARN_ON(test_bit(FR_SENT, &req->flags));
Miklos Szeredi825d6d32015-07-01 16:25:58 +0200394 if (test_bit(FR_BACKGROUND, &req->flags)) {
Kirill Tkhaiae2dffa2018-08-27 18:29:46 +0300395 spin_lock(&fc->bg_lock);
Miklos Szeredi825d6d32015-07-01 16:25:58 +0200396 clear_bit(FR_BACKGROUND, &req->flags);
Miklos Szeredi908a5722018-09-28 16:43:22 +0200397 if (fc->num_background == fc->max_background) {
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700398 fc->blocked = 0;
Maxim Patlasov722d2be2013-03-21 18:02:36 +0400399 wake_up(&fc->blocked_waitq);
Miklos Szeredi908a5722018-09-28 16:43:22 +0200400 } else if (!fc->blocked) {
401 /*
402 * Wake up next waiter, if any. It's okay to use
403 * waitqueue_active(), as we've already synced up
404 * fc->blocked with waiters with the wake_up() call
405 * above.
406 */
407 if (waitqueue_active(&fc->blocked_waitq))
408 wake_up(&fc->blocked_waitq);
409 }
Maxim Patlasov722d2be2013-03-21 18:02:36 +0400410
Tejun Heo8a301eb2018-02-02 09:54:14 -0800411 if (fc->num_background == fc->congestion_threshold && fc->sb) {
Jan Kara5f7f7542017-04-12 12:24:40 +0200412 clear_bdi_congested(fc->sb->s_bdi, BLK_RW_SYNC);
413 clear_bdi_congested(fc->sb->s_bdi, BLK_RW_ASYNC);
Miklos Szeredif92b99b2007-10-16 23:30:59 -0700414 }
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700415 fc->num_background--;
Miklos Szeredid12def12008-02-06 01:38:39 -0800416 fc->active_background--;
417 flush_bg_queue(fc);
Kirill Tkhaiae2dffa2018-08-27 18:29:46 +0300418 spin_unlock(&fc->bg_lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700419 }
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700420 wake_up(&req->waitq);
Miklos Szeredi1e6881c2015-07-01 16:26:07 +0200421 if (req->end)
422 req->end(fc, req);
Miklos Szeredib8f95e52018-07-26 16:13:11 +0200423put_request:
Tejun Heoe9bb09d2008-11-26 12:03:54 +0100424 fuse_put_request(fc, req);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700425}
426
Miklos Szeredif88996a2015-07-01 16:26:01 +0200427static void queue_interrupt(struct fuse_iqueue *fiq, struct fuse_req *req)
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700428{
Miklos Szeredi4ce60812015-07-01 16:26:02 +0200429 spin_lock(&fiq->waitq.lock);
Sahitya Tummala6ba4d272017-02-08 20:30:56 +0530430 if (test_bit(FR_FINISHED, &req->flags)) {
431 spin_unlock(&fiq->waitq.lock);
432 return;
433 }
Miklos Szeredi8f7bb362015-07-01 16:26:03 +0200434 if (list_empty(&req->intr_entry)) {
435 list_add_tail(&req->intr_entry, &fiq->interrupts);
436 wake_up_locked(&fiq->waitq);
437 }
Miklos Szeredi4ce60812015-07-01 16:26:02 +0200438 spin_unlock(&fiq->waitq.lock);
Miklos Szeredif88996a2015-07-01 16:26:01 +0200439 kill_fasync(&fiq->fasync, SIGIO, POLL_IN);
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700440}
441
Miklos Szeredi7c352bd2005-09-09 13:10:39 -0700442static void request_wait_answer(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700443{
Miklos Szeredi4ce60812015-07-01 16:26:02 +0200444 struct fuse_iqueue *fiq = &fc->iq;
Miklos Szeredic4775262015-07-01 16:26:00 +0200445 int err;
446
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700447 if (!fc->no_interrupt) {
448 /* Any signal may interrupt this */
Miklos Szeredic4775262015-07-01 16:26:00 +0200449 err = wait_event_interruptible(req->waitq,
Miklos Szeredi33e14b42015-07-01 16:26:01 +0200450 test_bit(FR_FINISHED, &req->flags));
Miklos Szeredic4775262015-07-01 16:26:00 +0200451 if (!err)
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700452 return;
453
Miklos Szeredi825d6d32015-07-01 16:25:58 +0200454 set_bit(FR_INTERRUPTED, &req->flags);
Miklos Szeredi8f7bb362015-07-01 16:26:03 +0200455 /* matches barrier in fuse_dev_do_read() */
456 smp_mb__after_atomic();
Miklos Szeredi33e14b42015-07-01 16:26:01 +0200457 if (test_bit(FR_SENT, &req->flags))
Miklos Szeredi4ce60812015-07-01 16:26:02 +0200458 queue_interrupt(fiq, req);
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700459 }
460
Miklos Szeredi825d6d32015-07-01 16:25:58 +0200461 if (!test_bit(FR_FORCE, &req->flags)) {
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700462 /* Only fatal signals may interrupt this */
Al Viro7d3a07f2016-07-19 03:08:27 -0400463 err = wait_event_killable(req->waitq,
Miklos Szeredi33e14b42015-07-01 16:26:01 +0200464 test_bit(FR_FINISHED, &req->flags));
Miklos Szeredic4775262015-07-01 16:26:00 +0200465 if (!err)
Miklos Szeredia131de02007-10-16 23:31:04 -0700466 return;
467
Miklos Szeredi4ce60812015-07-01 16:26:02 +0200468 spin_lock(&fiq->waitq.lock);
Miklos Szeredia131de02007-10-16 23:31:04 -0700469 /* Request is not yet in userspace, bail out */
Miklos Szeredi33e14b42015-07-01 16:26:01 +0200470 if (test_bit(FR_PENDING, &req->flags)) {
Miklos Szeredia131de02007-10-16 23:31:04 -0700471 list_del(&req->list);
Miklos Szeredi4ce60812015-07-01 16:26:02 +0200472 spin_unlock(&fiq->waitq.lock);
Miklos Szeredia131de02007-10-16 23:31:04 -0700473 __fuse_put_request(req);
474 req->out.h.error = -EINTR;
475 return;
476 }
Miklos Szeredi4ce60812015-07-01 16:26:02 +0200477 spin_unlock(&fiq->waitq.lock);
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700478 }
Miklos Szeredi334f4852005-09-09 13:10:27 -0700479
Miklos Szeredia131de02007-10-16 23:31:04 -0700480 /*
481 * Either request is already in userspace, or it was forced.
482 * Wait it out.
483 */
Miklos Szeredi33e14b42015-07-01 16:26:01 +0200484 wait_event(req->waitq, test_bit(FR_FINISHED, &req->flags));
Miklos Szeredi334f4852005-09-09 13:10:27 -0700485}
486
Eric Wong6a4e9222013-02-04 13:04:44 +0000487static void __fuse_request_send(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700488{
Miklos Szeredie16714d2015-07-01 16:26:01 +0200489 struct fuse_iqueue *fiq = &fc->iq;
490
Miklos Szeredi825d6d32015-07-01 16:25:58 +0200491 BUG_ON(test_bit(FR_BACKGROUND, &req->flags));
Miklos Szeredi4ce60812015-07-01 16:26:02 +0200492 spin_lock(&fiq->waitq.lock);
Miklos Szeredie16714d2015-07-01 16:26:01 +0200493 if (!fiq->connected) {
Miklos Szeredi4ce60812015-07-01 16:26:02 +0200494 spin_unlock(&fiq->waitq.lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700495 req->out.h.error = -ENOTCONN;
Miklos Szeredic4775262015-07-01 16:26:00 +0200496 } else {
Miklos Szeredif88996a2015-07-01 16:26:01 +0200497 req->in.h.unique = fuse_get_unique(fiq);
498 queue_request(fiq, req);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700499 /* acquire extra reference, since request is still needed
500 after request_end() */
501 __fuse_get_request(req);
Miklos Szeredi4ce60812015-07-01 16:26:02 +0200502 spin_unlock(&fiq->waitq.lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700503
Miklos Szeredi7c352bd2005-09-09 13:10:39 -0700504 request_wait_answer(fc, req);
Miklos Szeredic4775262015-07-01 16:26:00 +0200505 /* Pairs with smp_wmb() in request_end() */
506 smp_rmb();
Miklos Szeredi334f4852005-09-09 13:10:27 -0700507 }
Miklos Szeredi334f4852005-09-09 13:10:27 -0700508}
Eric Wong6a4e9222013-02-04 13:04:44 +0000509
510void fuse_request_send(struct fuse_conn *fc, struct fuse_req *req)
511{
Miklos Szeredi825d6d32015-07-01 16:25:58 +0200512 __set_bit(FR_ISREPLY, &req->flags);
513 if (!test_bit(FR_WAITING, &req->flags)) {
514 __set_bit(FR_WAITING, &req->flags);
Miklos Szeredi5437f242015-07-01 16:25:56 +0200515 atomic_inc(&fc->num_waiting);
516 }
Eric Wong6a4e9222013-02-04 13:04:44 +0000517 __fuse_request_send(fc, req);
518}
Tejun Heo08cbf542009-04-14 10:54:53 +0900519EXPORT_SYMBOL_GPL(fuse_request_send);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700520
Miklos Szeredi21f62172015-01-06 10:45:35 +0100521static void fuse_adjust_compat(struct fuse_conn *fc, struct fuse_args *args)
522{
523 if (fc->minor < 4 && args->in.h.opcode == FUSE_STATFS)
524 args->out.args[0].size = FUSE_COMPAT_STATFS_SIZE;
525
526 if (fc->minor < 9) {
527 switch (args->in.h.opcode) {
528 case FUSE_LOOKUP:
529 case FUSE_CREATE:
530 case FUSE_MKNOD:
531 case FUSE_MKDIR:
532 case FUSE_SYMLINK:
533 case FUSE_LINK:
534 args->out.args[0].size = FUSE_COMPAT_ENTRY_OUT_SIZE;
535 break;
536 case FUSE_GETATTR:
537 case FUSE_SETATTR:
538 args->out.args[0].size = FUSE_COMPAT_ATTR_OUT_SIZE;
539 break;
540 }
541 }
542 if (fc->minor < 12) {
543 switch (args->in.h.opcode) {
544 case FUSE_CREATE:
545 args->in.args[0].size = sizeof(struct fuse_open_in);
546 break;
547 case FUSE_MKNOD:
548 args->in.args[0].size = FUSE_COMPAT_MKNOD_IN_SIZE;
549 break;
550 }
551 }
552}
553
Miklos Szeredi70781872014-12-12 09:49:05 +0100554ssize_t fuse_simple_request(struct fuse_conn *fc, struct fuse_args *args)
555{
556 struct fuse_req *req;
557 ssize_t ret;
558
559 req = fuse_get_req(fc, 0);
560 if (IS_ERR(req))
561 return PTR_ERR(req);
562
Miklos Szeredi21f62172015-01-06 10:45:35 +0100563 /* Needs to be done after fuse_get_req() so that fc->minor is valid */
564 fuse_adjust_compat(fc, args);
565
Miklos Szeredi70781872014-12-12 09:49:05 +0100566 req->in.h.opcode = args->in.h.opcode;
567 req->in.h.nodeid = args->in.h.nodeid;
568 req->in.numargs = args->in.numargs;
569 memcpy(req->in.args, args->in.args,
570 args->in.numargs * sizeof(struct fuse_in_arg));
571 req->out.argvar = args->out.argvar;
572 req->out.numargs = args->out.numargs;
573 memcpy(req->out.args, args->out.args,
574 args->out.numargs * sizeof(struct fuse_arg));
575 fuse_request_send(fc, req);
576 ret = req->out.h.error;
577 if (!ret && args->out.argvar) {
578 BUG_ON(args->out.numargs != 1);
579 ret = req->out.args[0].size;
580 }
581 fuse_put_request(fc, req);
582
583 return ret;
584}
585
Kirill Tkhai63825b42018-08-27 18:29:56 +0300586bool fuse_request_queue_background(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredid12def12008-02-06 01:38:39 -0800587{
Kirill Tkhai63825b42018-08-27 18:29:56 +0300588 bool queued = false;
589
590 WARN_ON(!test_bit(FR_BACKGROUND, &req->flags));
Miklos Szeredi825d6d32015-07-01 16:25:58 +0200591 if (!test_bit(FR_WAITING, &req->flags)) {
592 __set_bit(FR_WAITING, &req->flags);
Miklos Szeredi5437f242015-07-01 16:25:56 +0200593 atomic_inc(&fc->num_waiting);
594 }
Miklos Szeredi825d6d32015-07-01 16:25:58 +0200595 __set_bit(FR_ISREPLY, &req->flags);
Kirill Tkhaiae2dffa2018-08-27 18:29:46 +0300596 spin_lock(&fc->bg_lock);
Kirill Tkhai63825b42018-08-27 18:29:56 +0300597 if (likely(fc->connected)) {
598 fc->num_background++;
599 if (fc->num_background == fc->max_background)
600 fc->blocked = 1;
601 if (fc->num_background == fc->congestion_threshold && fc->sb) {
602 set_bdi_congested(fc->sb->s_bdi, BLK_RW_SYNC);
603 set_bdi_congested(fc->sb->s_bdi, BLK_RW_ASYNC);
604 }
605 list_add_tail(&req->list, &fc->bg_queue);
606 flush_bg_queue(fc);
607 queued = true;
Miklos Szeredid12def12008-02-06 01:38:39 -0800608 }
Kirill Tkhaiae2dffa2018-08-27 18:29:46 +0300609 spin_unlock(&fc->bg_lock);
Kirill Tkhai63825b42018-08-27 18:29:56 +0300610
611 return queued;
Miklos Szeredid12def12008-02-06 01:38:39 -0800612}
613
Miklos Szeredif0139aa2015-07-01 16:25:57 +0200614void fuse_request_send_background(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700615{
Kirill Tkhai63825b42018-08-27 18:29:56 +0300616 WARN_ON(!req->end);
617 if (!fuse_request_queue_background(fc, req)) {
Miklos Szeredi334f4852005-09-09 13:10:27 -0700618 req->out.h.error = -ENOTCONN;
Miklos Szeredi42dc6212015-07-01 16:25:56 +0200619 req->end(fc, req);
620 fuse_put_request(fc, req);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700621 }
622}
Tejun Heo08cbf542009-04-14 10:54:53 +0900623EXPORT_SYMBOL_GPL(fuse_request_send_background);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700624
Miklos Szeredi2d45ba32010-07-12 14:41:40 +0200625static int fuse_request_send_notify_reply(struct fuse_conn *fc,
626 struct fuse_req *req, u64 unique)
627{
628 int err = -ENODEV;
Miklos Szeredif88996a2015-07-01 16:26:01 +0200629 struct fuse_iqueue *fiq = &fc->iq;
Miklos Szeredi2d45ba32010-07-12 14:41:40 +0200630
Miklos Szeredi825d6d32015-07-01 16:25:58 +0200631 __clear_bit(FR_ISREPLY, &req->flags);
Miklos Szeredi2d45ba32010-07-12 14:41:40 +0200632 req->in.h.unique = unique;
Miklos Szeredi4ce60812015-07-01 16:26:02 +0200633 spin_lock(&fiq->waitq.lock);
Miklos Szeredie16714d2015-07-01 16:26:01 +0200634 if (fiq->connected) {
Miklos Szeredif88996a2015-07-01 16:26:01 +0200635 queue_request(fiq, req);
Miklos Szeredi2d45ba32010-07-12 14:41:40 +0200636 err = 0;
637 }
Miklos Szeredi4ce60812015-07-01 16:26:02 +0200638 spin_unlock(&fiq->waitq.lock);
Miklos Szeredi2d45ba32010-07-12 14:41:40 +0200639
640 return err;
641}
642
Anand V. Avati0b05b182012-08-19 08:53:23 -0400643void fuse_force_forget(struct file *file, u64 nodeid)
644{
Al Viro6131ffa2013-02-27 16:59:05 -0500645 struct inode *inode = file_inode(file);
Anand V. Avati0b05b182012-08-19 08:53:23 -0400646 struct fuse_conn *fc = get_fuse_conn(inode);
647 struct fuse_req *req;
648 struct fuse_forget_in inarg;
649
650 memset(&inarg, 0, sizeof(inarg));
651 inarg.nlookup = 1;
Maxim Patlasovb111c8c2012-10-26 19:48:30 +0400652 req = fuse_get_req_nofail_nopages(fc, file);
Anand V. Avati0b05b182012-08-19 08:53:23 -0400653 req->in.h.opcode = FUSE_FORGET;
654 req->in.h.nodeid = nodeid;
655 req->in.numargs = 1;
656 req->in.args[0].size = sizeof(inarg);
657 req->in.args[0].value = &inarg;
Miklos Szeredi825d6d32015-07-01 16:25:58 +0200658 __clear_bit(FR_ISREPLY, &req->flags);
Eric Wong6a4e9222013-02-04 13:04:44 +0000659 __fuse_request_send(fc, req);
660 /* ignore errors */
661 fuse_put_request(fc, req);
Anand V. Avati0b05b182012-08-19 08:53:23 -0400662}
663
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700664/*
Miklos Szeredi334f4852005-09-09 13:10:27 -0700665 * Lock the request. Up to the next unlock_request() there mustn't be
666 * anything that could cause a page-fault. If the request was already
Miklos Szeredif9a28422006-06-25 05:48:53 -0700667 * aborted bail out.
Miklos Szeredi334f4852005-09-09 13:10:27 -0700668 */
Miklos Szeredidc008092015-07-01 16:25:58 +0200669static int lock_request(struct fuse_req *req)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700670{
671 int err = 0;
672 if (req) {
Miklos Szeredidc008092015-07-01 16:25:58 +0200673 spin_lock(&req->waitq.lock);
Miklos Szeredi825d6d32015-07-01 16:25:58 +0200674 if (test_bit(FR_ABORTED, &req->flags))
Miklos Szeredi334f4852005-09-09 13:10:27 -0700675 err = -ENOENT;
676 else
Miklos Szeredi825d6d32015-07-01 16:25:58 +0200677 set_bit(FR_LOCKED, &req->flags);
Miklos Szeredidc008092015-07-01 16:25:58 +0200678 spin_unlock(&req->waitq.lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700679 }
680 return err;
681}
682
683/*
Miklos Szeredi0d8e84b2015-07-01 16:25:58 +0200684 * Unlock request. If it was aborted while locked, caller is responsible
685 * for unlocking and ending the request.
Miklos Szeredi334f4852005-09-09 13:10:27 -0700686 */
Miklos Szeredidc008092015-07-01 16:25:58 +0200687static int unlock_request(struct fuse_req *req)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700688{
Miklos Szeredi0d8e84b2015-07-01 16:25:58 +0200689 int err = 0;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700690 if (req) {
Miklos Szeredidc008092015-07-01 16:25:58 +0200691 spin_lock(&req->waitq.lock);
Miklos Szeredi825d6d32015-07-01 16:25:58 +0200692 if (test_bit(FR_ABORTED, &req->flags))
Miklos Szeredi0d8e84b2015-07-01 16:25:58 +0200693 err = -ENOENT;
694 else
Miklos Szeredi825d6d32015-07-01 16:25:58 +0200695 clear_bit(FR_LOCKED, &req->flags);
Miklos Szeredidc008092015-07-01 16:25:58 +0200696 spin_unlock(&req->waitq.lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700697 }
Miklos Szeredi0d8e84b2015-07-01 16:25:58 +0200698 return err;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700699}
700
701struct fuse_copy_state {
702 int write;
703 struct fuse_req *req;
Al Viro6c09e942015-04-03 22:06:08 -0400704 struct iov_iter *iter;
Miklos Szeredidd3bb142010-05-25 15:06:06 +0200705 struct pipe_buffer *pipebufs;
706 struct pipe_buffer *currbuf;
707 struct pipe_inode_info *pipe;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700708 unsigned long nr_segs;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700709 struct page *pg;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700710 unsigned len;
Miklos Szeredic55a01d2014-07-07 15:28:51 +0200711 unsigned offset;
Miklos Szeredice534fb2010-05-25 15:06:07 +0200712 unsigned move_pages:1;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700713};
714
Miklos Szeredidc008092015-07-01 16:25:58 +0200715static void fuse_copy_init(struct fuse_copy_state *cs, int write,
Al Viro6c09e942015-04-03 22:06:08 -0400716 struct iov_iter *iter)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700717{
718 memset(cs, 0, sizeof(*cs));
719 cs->write = write;
Al Viro6c09e942015-04-03 22:06:08 -0400720 cs->iter = iter;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700721}
722
723/* Unmap and put previous page of userspace buffer */
Miklos Szeredi8bfc0162006-01-16 22:14:28 -0800724static void fuse_copy_finish(struct fuse_copy_state *cs)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700725{
Miklos Szeredidd3bb142010-05-25 15:06:06 +0200726 if (cs->currbuf) {
727 struct pipe_buffer *buf = cs->currbuf;
728
Miklos Szeredic55a01d2014-07-07 15:28:51 +0200729 if (cs->write)
Miklos Szeredic3021622010-05-25 15:06:07 +0200730 buf->len = PAGE_SIZE - cs->len;
Miklos Szeredidd3bb142010-05-25 15:06:06 +0200731 cs->currbuf = NULL;
Miklos Szeredic55a01d2014-07-07 15:28:51 +0200732 } else if (cs->pg) {
Miklos Szeredi334f4852005-09-09 13:10:27 -0700733 if (cs->write) {
734 flush_dcache_page(cs->pg);
735 set_page_dirty_lock(cs->pg);
736 }
737 put_page(cs->pg);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700738 }
Miklos Szeredic55a01d2014-07-07 15:28:51 +0200739 cs->pg = NULL;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700740}
741
742/*
743 * Get another pagefull of userspace buffer, and map it to kernel
744 * address space, and lock request
745 */
746static int fuse_copy_fill(struct fuse_copy_state *cs)
747{
Miklos Szeredic55a01d2014-07-07 15:28:51 +0200748 struct page *page;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700749 int err;
750
Miklos Szeredidc008092015-07-01 16:25:58 +0200751 err = unlock_request(cs->req);
Miklos Szeredi0d8e84b2015-07-01 16:25:58 +0200752 if (err)
753 return err;
754
Miklos Szeredi334f4852005-09-09 13:10:27 -0700755 fuse_copy_finish(cs);
Miklos Szeredidd3bb142010-05-25 15:06:06 +0200756 if (cs->pipebufs) {
757 struct pipe_buffer *buf = cs->pipebufs;
758
Miklos Szeredic3021622010-05-25 15:06:07 +0200759 if (!cs->write) {
Miklos Szeredifba597d2016-09-27 10:45:12 +0200760 err = pipe_buf_confirm(cs->pipe, buf);
Miklos Szeredic3021622010-05-25 15:06:07 +0200761 if (err)
762 return err;
Miklos Szeredidd3bb142010-05-25 15:06:06 +0200763
Miklos Szeredic3021622010-05-25 15:06:07 +0200764 BUG_ON(!cs->nr_segs);
765 cs->currbuf = buf;
Miklos Szeredic55a01d2014-07-07 15:28:51 +0200766 cs->pg = buf->page;
767 cs->offset = buf->offset;
Miklos Szeredic3021622010-05-25 15:06:07 +0200768 cs->len = buf->len;
Miklos Szeredic3021622010-05-25 15:06:07 +0200769 cs->pipebufs++;
770 cs->nr_segs--;
771 } else {
Miklos Szeredic3021622010-05-25 15:06:07 +0200772 if (cs->nr_segs == cs->pipe->buffers)
773 return -EIO;
774
775 page = alloc_page(GFP_HIGHUSER);
776 if (!page)
777 return -ENOMEM;
778
779 buf->page = page;
780 buf->offset = 0;
781 buf->len = 0;
782
783 cs->currbuf = buf;
Miklos Szeredic55a01d2014-07-07 15:28:51 +0200784 cs->pg = page;
785 cs->offset = 0;
Miklos Szeredic3021622010-05-25 15:06:07 +0200786 cs->len = PAGE_SIZE;
787 cs->pipebufs++;
788 cs->nr_segs++;
789 }
Miklos Szeredidd3bb142010-05-25 15:06:06 +0200790 } else {
Al Viro6c09e942015-04-03 22:06:08 -0400791 size_t off;
792 err = iov_iter_get_pages(cs->iter, &page, PAGE_SIZE, 1, &off);
Miklos Szeredidd3bb142010-05-25 15:06:06 +0200793 if (err < 0)
794 return err;
Al Viro6c09e942015-04-03 22:06:08 -0400795 BUG_ON(!err);
796 cs->len = err;
797 cs->offset = off;
Miklos Szeredic55a01d2014-07-07 15:28:51 +0200798 cs->pg = page;
Al Viro6c09e942015-04-03 22:06:08 -0400799 iov_iter_advance(cs->iter, err);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700800 }
Miklos Szeredi334f4852005-09-09 13:10:27 -0700801
Miklos Szeredidc008092015-07-01 16:25:58 +0200802 return lock_request(cs->req);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700803}
804
805/* Do as much copy to/from userspace buffer as we can */
Miklos Szeredi8bfc0162006-01-16 22:14:28 -0800806static int fuse_copy_do(struct fuse_copy_state *cs, void **val, unsigned *size)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700807{
808 unsigned ncpy = min(*size, cs->len);
809 if (val) {
Miklos Szeredic55a01d2014-07-07 15:28:51 +0200810 void *pgaddr = kmap_atomic(cs->pg);
811 void *buf = pgaddr + cs->offset;
812
Miklos Szeredi334f4852005-09-09 13:10:27 -0700813 if (cs->write)
Miklos Szeredic55a01d2014-07-07 15:28:51 +0200814 memcpy(buf, *val, ncpy);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700815 else
Miklos Szeredic55a01d2014-07-07 15:28:51 +0200816 memcpy(*val, buf, ncpy);
817
818 kunmap_atomic(pgaddr);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700819 *val += ncpy;
820 }
821 *size -= ncpy;
822 cs->len -= ncpy;
Miklos Szeredic55a01d2014-07-07 15:28:51 +0200823 cs->offset += ncpy;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700824 return ncpy;
825}
826
Miklos Szeredice534fb2010-05-25 15:06:07 +0200827static int fuse_check_page(struct page *page)
828{
829 if (page_mapcount(page) ||
830 page->mapping != NULL ||
831 page_count(page) != 1 ||
832 (page->flags & PAGE_FLAGS_CHECK_AT_PREP &
833 ~(1 << PG_locked |
834 1 << PG_referenced |
835 1 << PG_uptodate |
836 1 << PG_lru |
837 1 << PG_active |
838 1 << PG_reclaim))) {
839 printk(KERN_WARNING "fuse: trying to steal weird page\n");
840 printk(KERN_WARNING " page=%p index=%li flags=%08lx, count=%i, mapcount=%i, mapping=%p\n", page, page->index, page->flags, page_count(page), page_mapcount(page), page->mapping);
841 return 1;
842 }
843 return 0;
844}
845
846static int fuse_try_move_page(struct fuse_copy_state *cs, struct page **pagep)
847{
848 int err;
849 struct page *oldpage = *pagep;
850 struct page *newpage;
851 struct pipe_buffer *buf = cs->pipebufs;
Miklos Szeredice534fb2010-05-25 15:06:07 +0200852
Miklos Szeredidc008092015-07-01 16:25:58 +0200853 err = unlock_request(cs->req);
Miklos Szeredi0d8e84b2015-07-01 16:25:58 +0200854 if (err)
855 return err;
856
Miklos Szeredice534fb2010-05-25 15:06:07 +0200857 fuse_copy_finish(cs);
858
Miklos Szeredifba597d2016-09-27 10:45:12 +0200859 err = pipe_buf_confirm(cs->pipe, buf);
Miklos Szeredice534fb2010-05-25 15:06:07 +0200860 if (err)
861 return err;
862
863 BUG_ON(!cs->nr_segs);
864 cs->currbuf = buf;
865 cs->len = buf->len;
866 cs->pipebufs++;
867 cs->nr_segs--;
868
869 if (cs->len != PAGE_SIZE)
870 goto out_fallback;
871
Miklos Szeredica76f5b2016-09-27 10:45:12 +0200872 if (pipe_buf_steal(cs->pipe, buf) != 0)
Miklos Szeredice534fb2010-05-25 15:06:07 +0200873 goto out_fallback;
874
875 newpage = buf->page;
876
Miklos Szerediaa991b32015-02-26 11:45:47 +0100877 if (!PageUptodate(newpage))
878 SetPageUptodate(newpage);
Miklos Szeredice534fb2010-05-25 15:06:07 +0200879
880 ClearPageMappedToDisk(newpage);
881
882 if (fuse_check_page(newpage) != 0)
883 goto out_fallback_unlock;
884
Miklos Szeredice534fb2010-05-25 15:06:07 +0200885 /*
886 * This is a new and locked page, it shouldn't be mapped or
887 * have any special flags on it
888 */
889 if (WARN_ON(page_mapped(oldpage)))
890 goto out_fallback_unlock;
891 if (WARN_ON(page_has_private(oldpage)))
892 goto out_fallback_unlock;
893 if (WARN_ON(PageDirty(oldpage) || PageWriteback(oldpage)))
894 goto out_fallback_unlock;
895 if (WARN_ON(PageMlocked(oldpage)))
896 goto out_fallback_unlock;
897
Miklos Szeredief6a3c62011-03-22 16:30:52 -0700898 err = replace_page_cache_page(oldpage, newpage, GFP_KERNEL);
Miklos Szeredice534fb2010-05-25 15:06:07 +0200899 if (err) {
Miklos Szeredief6a3c62011-03-22 16:30:52 -0700900 unlock_page(newpage);
901 return err;
Miklos Szeredice534fb2010-05-25 15:06:07 +0200902 }
Miklos Szeredief6a3c62011-03-22 16:30:52 -0700903
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300904 get_page(newpage);
Miklos Szeredice534fb2010-05-25 15:06:07 +0200905
906 if (!(buf->flags & PIPE_BUF_FLAG_LRU))
907 lru_cache_add_file(newpage);
908
909 err = 0;
Miklos Szeredidc008092015-07-01 16:25:58 +0200910 spin_lock(&cs->req->waitq.lock);
Miklos Szeredi825d6d32015-07-01 16:25:58 +0200911 if (test_bit(FR_ABORTED, &cs->req->flags))
Miklos Szeredice534fb2010-05-25 15:06:07 +0200912 err = -ENOENT;
913 else
914 *pagep = newpage;
Miklos Szeredidc008092015-07-01 16:25:58 +0200915 spin_unlock(&cs->req->waitq.lock);
Miklos Szeredice534fb2010-05-25 15:06:07 +0200916
917 if (err) {
918 unlock_page(newpage);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300919 put_page(newpage);
Miklos Szeredice534fb2010-05-25 15:06:07 +0200920 return err;
921 }
922
923 unlock_page(oldpage);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300924 put_page(oldpage);
Miklos Szeredice534fb2010-05-25 15:06:07 +0200925 cs->len = 0;
926
927 return 0;
928
929out_fallback_unlock:
930 unlock_page(newpage);
931out_fallback:
Miklos Szeredic55a01d2014-07-07 15:28:51 +0200932 cs->pg = buf->page;
933 cs->offset = buf->offset;
Miklos Szeredice534fb2010-05-25 15:06:07 +0200934
Miklos Szeredidc008092015-07-01 16:25:58 +0200935 err = lock_request(cs->req);
Miklos Szeredice534fb2010-05-25 15:06:07 +0200936 if (err)
937 return err;
938
939 return 1;
940}
941
Miklos Szeredic3021622010-05-25 15:06:07 +0200942static int fuse_ref_page(struct fuse_copy_state *cs, struct page *page,
943 unsigned offset, unsigned count)
944{
945 struct pipe_buffer *buf;
Miklos Szeredi0d8e84b2015-07-01 16:25:58 +0200946 int err;
Miklos Szeredic3021622010-05-25 15:06:07 +0200947
948 if (cs->nr_segs == cs->pipe->buffers)
949 return -EIO;
950
Miklos Szeredidc008092015-07-01 16:25:58 +0200951 err = unlock_request(cs->req);
Miklos Szeredi0d8e84b2015-07-01 16:25:58 +0200952 if (err)
953 return err;
954
Miklos Szeredic3021622010-05-25 15:06:07 +0200955 fuse_copy_finish(cs);
956
957 buf = cs->pipebufs;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300958 get_page(page);
Miklos Szeredic3021622010-05-25 15:06:07 +0200959 buf->page = page;
960 buf->offset = offset;
961 buf->len = count;
962
963 cs->pipebufs++;
964 cs->nr_segs++;
965 cs->len = 0;
966
967 return 0;
968}
969
Miklos Szeredi334f4852005-09-09 13:10:27 -0700970/*
971 * Copy a page in the request to/from the userspace buffer. Must be
972 * done atomically
973 */
Miklos Szeredice534fb2010-05-25 15:06:07 +0200974static int fuse_copy_page(struct fuse_copy_state *cs, struct page **pagep,
Miklos Szeredi8bfc0162006-01-16 22:14:28 -0800975 unsigned offset, unsigned count, int zeroing)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700976{
Miklos Szeredice534fb2010-05-25 15:06:07 +0200977 int err;
978 struct page *page = *pagep;
979
Miklos Szeredib6777c42010-10-26 14:22:27 -0700980 if (page && zeroing && count < PAGE_SIZE)
981 clear_highpage(page);
982
Miklos Szeredi334f4852005-09-09 13:10:27 -0700983 while (count) {
Miklos Szeredic3021622010-05-25 15:06:07 +0200984 if (cs->write && cs->pipebufs && page) {
985 return fuse_ref_page(cs, page, offset, count);
986 } else if (!cs->len) {
Miklos Szeredice534fb2010-05-25 15:06:07 +0200987 if (cs->move_pages && page &&
988 offset == 0 && count == PAGE_SIZE) {
989 err = fuse_try_move_page(cs, pagep);
990 if (err <= 0)
991 return err;
992 } else {
993 err = fuse_copy_fill(cs);
994 if (err)
995 return err;
996 }
Miklos Szeredi1729a162008-11-26 12:03:54 +0100997 }
Miklos Szeredi334f4852005-09-09 13:10:27 -0700998 if (page) {
Cong Wang2408f6e2011-11-25 23:14:30 +0800999 void *mapaddr = kmap_atomic(page);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001000 void *buf = mapaddr + offset;
1001 offset += fuse_copy_do(cs, &buf, &count);
Cong Wang2408f6e2011-11-25 23:14:30 +08001002 kunmap_atomic(mapaddr);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001003 } else
1004 offset += fuse_copy_do(cs, NULL, &count);
1005 }
1006 if (page && !cs->write)
1007 flush_dcache_page(page);
1008 return 0;
1009}
1010
1011/* Copy pages in the request to/from userspace buffer */
1012static int fuse_copy_pages(struct fuse_copy_state *cs, unsigned nbytes,
1013 int zeroing)
1014{
1015 unsigned i;
1016 struct fuse_req *req = cs->req;
Miklos Szeredi334f4852005-09-09 13:10:27 -07001017
1018 for (i = 0; i < req->num_pages && (nbytes || zeroing); i++) {
Miklos Szeredice534fb2010-05-25 15:06:07 +02001019 int err;
Maxim Patlasov85f40ae2012-10-26 19:49:33 +04001020 unsigned offset = req->page_descs[i].offset;
1021 unsigned count = min(nbytes, req->page_descs[i].length);
Miklos Szeredice534fb2010-05-25 15:06:07 +02001022
1023 err = fuse_copy_page(cs, &req->pages[i], offset, count,
1024 zeroing);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001025 if (err)
1026 return err;
1027
1028 nbytes -= count;
Miklos Szeredi334f4852005-09-09 13:10:27 -07001029 }
1030 return 0;
1031}
1032
1033/* Copy a single argument in the request to/from userspace buffer */
1034static int fuse_copy_one(struct fuse_copy_state *cs, void *val, unsigned size)
1035{
1036 while (size) {
Miklos Szeredi1729a162008-11-26 12:03:54 +01001037 if (!cs->len) {
1038 int err = fuse_copy_fill(cs);
1039 if (err)
1040 return err;
1041 }
Miklos Szeredi334f4852005-09-09 13:10:27 -07001042 fuse_copy_do(cs, &val, &size);
1043 }
1044 return 0;
1045}
1046
1047/* Copy request arguments to/from userspace buffer */
1048static int fuse_copy_args(struct fuse_copy_state *cs, unsigned numargs,
1049 unsigned argpages, struct fuse_arg *args,
1050 int zeroing)
1051{
1052 int err = 0;
1053 unsigned i;
1054
1055 for (i = 0; !err && i < numargs; i++) {
1056 struct fuse_arg *arg = &args[i];
1057 if (i == numargs - 1 && argpages)
1058 err = fuse_copy_pages(cs, arg->size, zeroing);
1059 else
1060 err = fuse_copy_one(cs, arg->value, arg->size);
1061 }
1062 return err;
1063}
1064
Miklos Szeredif88996a2015-07-01 16:26:01 +02001065static int forget_pending(struct fuse_iqueue *fiq)
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001066{
Miklos Szeredif88996a2015-07-01 16:26:01 +02001067 return fiq->forget_list_head.next != NULL;
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001068}
1069
Miklos Szeredif88996a2015-07-01 16:26:01 +02001070static int request_pending(struct fuse_iqueue *fiq)
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001071{
Miklos Szeredif88996a2015-07-01 16:26:01 +02001072 return !list_empty(&fiq->pending) || !list_empty(&fiq->interrupts) ||
1073 forget_pending(fiq);
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001074}
1075
Miklos Szeredi334f4852005-09-09 13:10:27 -07001076/*
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001077 * Transfer an interrupt request to userspace
1078 *
1079 * Unlike other requests this is assembled on demand, without a need
1080 * to allocate a separate fuse_req structure.
1081 *
Miklos Szeredifd22d622015-07-01 16:26:03 +02001082 * Called with fiq->waitq.lock held, releases it
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001083 */
Miklos Szeredifd22d622015-07-01 16:26:03 +02001084static int fuse_read_interrupt(struct fuse_iqueue *fiq,
1085 struct fuse_copy_state *cs,
Miklos Szeredic3021622010-05-25 15:06:07 +02001086 size_t nbytes, struct fuse_req *req)
Miklos Szeredifd22d622015-07-01 16:26:03 +02001087__releases(fiq->waitq.lock)
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001088{
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001089 struct fuse_in_header ih;
1090 struct fuse_interrupt_in arg;
1091 unsigned reqsize = sizeof(ih) + sizeof(arg);
1092 int err;
1093
1094 list_del_init(&req->intr_entry);
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001095 memset(&ih, 0, sizeof(ih));
1096 memset(&arg, 0, sizeof(arg));
1097 ih.len = reqsize;
1098 ih.opcode = FUSE_INTERRUPT;
Kirill Tkhai3a5358d2018-09-11 13:12:05 +03001099 ih.unique = (req->in.h.unique | FUSE_INT_REQ_BIT);
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001100 arg.unique = req->in.h.unique;
1101
Miklos Szeredi4ce60812015-07-01 16:26:02 +02001102 spin_unlock(&fiq->waitq.lock);
Miklos Szeredic3021622010-05-25 15:06:07 +02001103 if (nbytes < reqsize)
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001104 return -EINVAL;
1105
Miklos Szeredic3021622010-05-25 15:06:07 +02001106 err = fuse_copy_one(cs, &ih, sizeof(ih));
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001107 if (!err)
Miklos Szeredic3021622010-05-25 15:06:07 +02001108 err = fuse_copy_one(cs, &arg, sizeof(arg));
1109 fuse_copy_finish(cs);
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001110
1111 return err ? err : reqsize;
1112}
1113
Miklos Szeredif88996a2015-07-01 16:26:01 +02001114static struct fuse_forget_link *dequeue_forget(struct fuse_iqueue *fiq,
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001115 unsigned max,
1116 unsigned *countp)
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001117{
Miklos Szeredif88996a2015-07-01 16:26:01 +02001118 struct fuse_forget_link *head = fiq->forget_list_head.next;
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001119 struct fuse_forget_link **newhead = &head;
1120 unsigned count;
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001121
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001122 for (count = 0; *newhead != NULL && count < max; count++)
1123 newhead = &(*newhead)->next;
1124
Miklos Szeredif88996a2015-07-01 16:26:01 +02001125 fiq->forget_list_head.next = *newhead;
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001126 *newhead = NULL;
Miklos Szeredif88996a2015-07-01 16:26:01 +02001127 if (fiq->forget_list_head.next == NULL)
1128 fiq->forget_list_tail = &fiq->forget_list_head;
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001129
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001130 if (countp != NULL)
1131 *countp = count;
1132
1133 return head;
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001134}
1135
Miklos Szeredifd22d622015-07-01 16:26:03 +02001136static int fuse_read_single_forget(struct fuse_iqueue *fiq,
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001137 struct fuse_copy_state *cs,
1138 size_t nbytes)
Miklos Szeredifd22d622015-07-01 16:26:03 +02001139__releases(fiq->waitq.lock)
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001140{
1141 int err;
Miklos Szeredif88996a2015-07-01 16:26:01 +02001142 struct fuse_forget_link *forget = dequeue_forget(fiq, 1, NULL);
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001143 struct fuse_forget_in arg = {
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001144 .nlookup = forget->forget_one.nlookup,
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001145 };
1146 struct fuse_in_header ih = {
1147 .opcode = FUSE_FORGET,
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001148 .nodeid = forget->forget_one.nodeid,
Miklos Szeredif88996a2015-07-01 16:26:01 +02001149 .unique = fuse_get_unique(fiq),
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001150 .len = sizeof(ih) + sizeof(arg),
1151 };
1152
Miklos Szeredi4ce60812015-07-01 16:26:02 +02001153 spin_unlock(&fiq->waitq.lock);
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001154 kfree(forget);
1155 if (nbytes < ih.len)
1156 return -EINVAL;
1157
1158 err = fuse_copy_one(cs, &ih, sizeof(ih));
1159 if (!err)
1160 err = fuse_copy_one(cs, &arg, sizeof(arg));
1161 fuse_copy_finish(cs);
1162
1163 if (err)
1164 return err;
1165
1166 return ih.len;
1167}
1168
Miklos Szeredifd22d622015-07-01 16:26:03 +02001169static int fuse_read_batch_forget(struct fuse_iqueue *fiq,
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001170 struct fuse_copy_state *cs, size_t nbytes)
Miklos Szeredifd22d622015-07-01 16:26:03 +02001171__releases(fiq->waitq.lock)
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001172{
1173 int err;
1174 unsigned max_forgets;
1175 unsigned count;
1176 struct fuse_forget_link *head;
1177 struct fuse_batch_forget_in arg = { .count = 0 };
1178 struct fuse_in_header ih = {
1179 .opcode = FUSE_BATCH_FORGET,
Miklos Szeredif88996a2015-07-01 16:26:01 +02001180 .unique = fuse_get_unique(fiq),
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001181 .len = sizeof(ih) + sizeof(arg),
1182 };
1183
1184 if (nbytes < ih.len) {
Miklos Szeredi4ce60812015-07-01 16:26:02 +02001185 spin_unlock(&fiq->waitq.lock);
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001186 return -EINVAL;
1187 }
1188
1189 max_forgets = (nbytes - ih.len) / sizeof(struct fuse_forget_one);
Miklos Szeredif88996a2015-07-01 16:26:01 +02001190 head = dequeue_forget(fiq, max_forgets, &count);
Miklos Szeredi4ce60812015-07-01 16:26:02 +02001191 spin_unlock(&fiq->waitq.lock);
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001192
1193 arg.count = count;
1194 ih.len += count * sizeof(struct fuse_forget_one);
1195 err = fuse_copy_one(cs, &ih, sizeof(ih));
1196 if (!err)
1197 err = fuse_copy_one(cs, &arg, sizeof(arg));
1198
1199 while (head) {
1200 struct fuse_forget_link *forget = head;
1201
1202 if (!err) {
1203 err = fuse_copy_one(cs, &forget->forget_one,
1204 sizeof(forget->forget_one));
1205 }
1206 head = forget->next;
1207 kfree(forget);
1208 }
1209
1210 fuse_copy_finish(cs);
1211
1212 if (err)
1213 return err;
1214
1215 return ih.len;
1216}
1217
Miklos Szeredifd22d622015-07-01 16:26:03 +02001218static int fuse_read_forget(struct fuse_conn *fc, struct fuse_iqueue *fiq,
1219 struct fuse_copy_state *cs,
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001220 size_t nbytes)
Miklos Szeredifd22d622015-07-01 16:26:03 +02001221__releases(fiq->waitq.lock)
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001222{
Miklos Szeredif88996a2015-07-01 16:26:01 +02001223 if (fc->minor < 16 || fiq->forget_list_head.next->next == NULL)
Miklos Szeredifd22d622015-07-01 16:26:03 +02001224 return fuse_read_single_forget(fiq, cs, nbytes);
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001225 else
Miklos Szeredifd22d622015-07-01 16:26:03 +02001226 return fuse_read_batch_forget(fiq, cs, nbytes);
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001227}
1228
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001229/*
Miklos Szeredi334f4852005-09-09 13:10:27 -07001230 * Read a single request into the userspace filesystem's buffer. This
1231 * function waits until a request is available, then removes it from
1232 * the pending list and copies request data to userspace buffer. If
Miklos Szeredif9a28422006-06-25 05:48:53 -07001233 * no reply is needed (FORGET) or request has been aborted or there
1234 * was an error during the copying then it's finished by calling
Miklos Szeredi334f4852005-09-09 13:10:27 -07001235 * request_end(). Otherwise add it to the processing list, and set
1236 * the 'sent' flag.
1237 */
Miklos Szeredic36960462015-07-01 16:26:09 +02001238static ssize_t fuse_dev_do_read(struct fuse_dev *fud, struct file *file,
Miklos Szeredic3021622010-05-25 15:06:07 +02001239 struct fuse_copy_state *cs, size_t nbytes)
Miklos Szeredi334f4852005-09-09 13:10:27 -07001240{
Miklos Szeredi82cbdcd2015-07-01 16:26:05 +02001241 ssize_t err;
Miklos Szeredic36960462015-07-01 16:26:09 +02001242 struct fuse_conn *fc = fud->fc;
Miklos Szeredif88996a2015-07-01 16:26:01 +02001243 struct fuse_iqueue *fiq = &fc->iq;
Miklos Szeredic36960462015-07-01 16:26:09 +02001244 struct fuse_pqueue *fpq = &fud->pq;
Miklos Szeredi334f4852005-09-09 13:10:27 -07001245 struct fuse_req *req;
1246 struct fuse_in *in;
Miklos Szeredi334f4852005-09-09 13:10:27 -07001247 unsigned reqsize;
Kirill Tkhaibe2ff422018-09-11 13:12:14 +03001248 unsigned int hash;
Miklos Szeredi334f4852005-09-09 13:10:27 -07001249
Miklos Szeredi1d3d7522006-01-06 00:19:40 -08001250 restart:
Miklos Szeredi4ce60812015-07-01 16:26:02 +02001251 spin_lock(&fiq->waitq.lock);
Jeff Dikee5ac1d12006-04-10 22:54:53 -07001252 err = -EAGAIN;
Miklos Szeredie16714d2015-07-01 16:26:01 +02001253 if ((file->f_flags & O_NONBLOCK) && fiq->connected &&
Miklos Szeredif88996a2015-07-01 16:26:01 +02001254 !request_pending(fiq))
Jeff Dikee5ac1d12006-04-10 22:54:53 -07001255 goto err_unlock;
1256
Miklos Szeredi52509212015-07-01 16:26:03 +02001257 err = wait_event_interruptible_exclusive_locked(fiq->waitq,
1258 !fiq->connected || request_pending(fiq));
1259 if (err)
1260 goto err_unlock;
1261
Szymon Lukasz3b7008b2017-11-09 21:23:35 +01001262 if (!fiq->connected) {
1263 err = (fc->aborted && fc->abort_err) ? -ECONNABORTED : -ENODEV;
Miklos Szeredi334f4852005-09-09 13:10:27 -07001264 goto err_unlock;
Szymon Lukasz3b7008b2017-11-09 21:23:35 +01001265 }
Miklos Szeredi334f4852005-09-09 13:10:27 -07001266
Miklos Szeredif88996a2015-07-01 16:26:01 +02001267 if (!list_empty(&fiq->interrupts)) {
1268 req = list_entry(fiq->interrupts.next, struct fuse_req,
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001269 intr_entry);
Miklos Szeredifd22d622015-07-01 16:26:03 +02001270 return fuse_read_interrupt(fiq, cs, nbytes, req);
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001271 }
1272
Miklos Szeredif88996a2015-07-01 16:26:01 +02001273 if (forget_pending(fiq)) {
1274 if (list_empty(&fiq->pending) || fiq->forget_batch-- > 0)
Miklos Szeredifd22d622015-07-01 16:26:03 +02001275 return fuse_read_forget(fc, fiq, cs, nbytes);
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001276
Miklos Szeredif88996a2015-07-01 16:26:01 +02001277 if (fiq->forget_batch <= -8)
1278 fiq->forget_batch = 16;
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001279 }
1280
Miklos Szeredif88996a2015-07-01 16:26:01 +02001281 req = list_entry(fiq->pending.next, struct fuse_req, list);
Miklos Szeredi33e14b42015-07-01 16:26:01 +02001282 clear_bit(FR_PENDING, &req->flags);
Miklos Szeredief759252015-07-01 16:26:02 +02001283 list_del_init(&req->list);
Miklos Szeredi4ce60812015-07-01 16:26:02 +02001284 spin_unlock(&fiq->waitq.lock);
1285
Miklos Szeredi334f4852005-09-09 13:10:27 -07001286 in = &req->in;
Miklos Szeredi1d3d7522006-01-06 00:19:40 -08001287 reqsize = in->h.len;
Miklos Szeredi5d6d3a32017-09-12 16:57:53 +02001288
Miklos Szeredi1d3d7522006-01-06 00:19:40 -08001289 /* If request is too large, reply with an error and restart the read */
Miklos Szeredic3021622010-05-25 15:06:07 +02001290 if (nbytes < reqsize) {
Miklos Szeredi1d3d7522006-01-06 00:19:40 -08001291 req->out.h.error = -EIO;
1292 /* SETXATTR is special, since it may contain too large data */
1293 if (in->h.opcode == FUSE_SETXATTR)
1294 req->out.h.error = -E2BIG;
1295 request_end(fc, req);
1296 goto restart;
Miklos Szeredi334f4852005-09-09 13:10:27 -07001297 }
Miklos Szeredi45a91cb2015-07-01 16:26:06 +02001298 spin_lock(&fpq->lock);
Miklos Szeredi82cbdcd2015-07-01 16:26:05 +02001299 list_add(&req->list, &fpq->io);
Miklos Szeredi45a91cb2015-07-01 16:26:06 +02001300 spin_unlock(&fpq->lock);
Miklos Szeredic3021622010-05-25 15:06:07 +02001301 cs->req = req;
1302 err = fuse_copy_one(cs, &in->h, sizeof(in->h));
Miklos Szeredi1d3d7522006-01-06 00:19:40 -08001303 if (!err)
Miklos Szeredic3021622010-05-25 15:06:07 +02001304 err = fuse_copy_args(cs, in->numargs, in->argpages,
Miklos Szeredi1d3d7522006-01-06 00:19:40 -08001305 (struct fuse_arg *) in->args, 0);
Miklos Szeredic3021622010-05-25 15:06:07 +02001306 fuse_copy_finish(cs);
Miklos Szeredi45a91cb2015-07-01 16:26:06 +02001307 spin_lock(&fpq->lock);
Miklos Szeredi825d6d32015-07-01 16:25:58 +02001308 clear_bit(FR_LOCKED, &req->flags);
Miklos Szeredie96edd92015-07-01 16:26:04 +02001309 if (!fpq->connected) {
Szymon Lukasz3b7008b2017-11-09 21:23:35 +01001310 err = (fc->aborted && fc->abort_err) ? -ECONNABORTED : -ENODEV;
Miklos Szeredi82cbdcd2015-07-01 16:26:05 +02001311 goto out_end;
Miklos Szeredic9c9d7d2007-10-16 23:31:05 -07001312 }
Miklos Szeredi334f4852005-09-09 13:10:27 -07001313 if (err) {
Miklos Szeredic9c9d7d2007-10-16 23:31:05 -07001314 req->out.h.error = -EIO;
Miklos Szeredi82cbdcd2015-07-01 16:26:05 +02001315 goto out_end;
Miklos Szeredi334f4852005-09-09 13:10:27 -07001316 }
Miklos Szeredi825d6d32015-07-01 16:25:58 +02001317 if (!test_bit(FR_ISREPLY, &req->flags)) {
Miklos Szeredi82cbdcd2015-07-01 16:26:05 +02001318 err = reqsize;
1319 goto out_end;
Miklos Szeredi334f4852005-09-09 13:10:27 -07001320 }
Kirill Tkhaibe2ff422018-09-11 13:12:14 +03001321 hash = fuse_req_hash(req->in.h.unique);
1322 list_move_tail(&req->list, &fpq->processing[hash]);
Kirill Tkhaibc78abb2018-09-25 12:28:55 +03001323 __fuse_get_request(req);
Miklos Szeredi82cbdcd2015-07-01 16:26:05 +02001324 set_bit(FR_SENT, &req->flags);
Miklos Szeredi4c316f22018-09-28 16:43:22 +02001325 spin_unlock(&fpq->lock);
Miklos Szeredi82cbdcd2015-07-01 16:26:05 +02001326 /* matches barrier in request_wait_answer() */
1327 smp_mb__after_atomic();
1328 if (test_bit(FR_INTERRUPTED, &req->flags))
1329 queue_interrupt(fiq, req);
Kirill Tkhaibc78abb2018-09-25 12:28:55 +03001330 fuse_put_request(fc, req);
Miklos Szeredi82cbdcd2015-07-01 16:26:05 +02001331
Miklos Szeredi334f4852005-09-09 13:10:27 -07001332 return reqsize;
1333
Miklos Szeredi82cbdcd2015-07-01 16:26:05 +02001334out_end:
Miklos Szeredi77cd9d42015-07-01 16:26:06 +02001335 if (!test_bit(FR_PRIVATE, &req->flags))
1336 list_del_init(&req->list);
Miklos Szeredi45a91cb2015-07-01 16:26:06 +02001337 spin_unlock(&fpq->lock);
Miklos Szeredi82cbdcd2015-07-01 16:26:05 +02001338 request_end(fc, req);
1339 return err;
1340
Miklos Szeredi334f4852005-09-09 13:10:27 -07001341 err_unlock:
Miklos Szeredi4ce60812015-07-01 16:26:02 +02001342 spin_unlock(&fiq->waitq.lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001343 return err;
1344}
1345
Tom Van Braeckel94e4fe22015-01-12 05:22:16 +01001346static int fuse_dev_open(struct inode *inode, struct file *file)
1347{
1348 /*
1349 * The fuse device's file's private_data is used to hold
1350 * the fuse_conn(ection) when it is mounted, and is used to
1351 * keep track of whether the file has been mounted already.
1352 */
1353 file->private_data = NULL;
1354 return 0;
1355}
1356
Al Virofbdbacc2015-04-03 21:53:39 -04001357static ssize_t fuse_dev_read(struct kiocb *iocb, struct iov_iter *to)
Miklos Szeredic3021622010-05-25 15:06:07 +02001358{
1359 struct fuse_copy_state cs;
1360 struct file *file = iocb->ki_filp;
Miklos Szeredicc080e92015-07-01 16:26:08 +02001361 struct fuse_dev *fud = fuse_get_dev(file);
1362
1363 if (!fud)
Miklos Szeredic3021622010-05-25 15:06:07 +02001364 return -EPERM;
1365
Al Virofbdbacc2015-04-03 21:53:39 -04001366 if (!iter_is_iovec(to))
1367 return -EINVAL;
Miklos Szeredic3021622010-05-25 15:06:07 +02001368
Miklos Szeredidc008092015-07-01 16:25:58 +02001369 fuse_copy_init(&cs, 1, to);
Al Virofbdbacc2015-04-03 21:53:39 -04001370
Miklos Szeredic36960462015-07-01 16:26:09 +02001371 return fuse_dev_do_read(fud, file, &cs, iov_iter_count(to));
Miklos Szeredic3021622010-05-25 15:06:07 +02001372}
1373
Miklos Szeredic3021622010-05-25 15:06:07 +02001374static ssize_t fuse_dev_splice_read(struct file *in, loff_t *ppos,
1375 struct pipe_inode_info *pipe,
1376 size_t len, unsigned int flags)
1377{
Al Virod82718e2016-09-17 22:56:25 -04001378 int total, ret;
Miklos Szeredic3021622010-05-25 15:06:07 +02001379 int page_nr = 0;
Miklos Szeredic3021622010-05-25 15:06:07 +02001380 struct pipe_buffer *bufs;
1381 struct fuse_copy_state cs;
Miklos Szeredicc080e92015-07-01 16:26:08 +02001382 struct fuse_dev *fud = fuse_get_dev(in);
1383
1384 if (!fud)
Miklos Szeredic3021622010-05-25 15:06:07 +02001385 return -EPERM;
1386
Andrey Ryabinind6d931a2018-07-17 19:00:34 +03001387 bufs = kvmalloc_array(pipe->buffers, sizeof(struct pipe_buffer),
1388 GFP_KERNEL);
Miklos Szeredic3021622010-05-25 15:06:07 +02001389 if (!bufs)
1390 return -ENOMEM;
1391
Miklos Szeredidc008092015-07-01 16:25:58 +02001392 fuse_copy_init(&cs, 1, NULL);
Miklos Szeredic3021622010-05-25 15:06:07 +02001393 cs.pipebufs = bufs;
1394 cs.pipe = pipe;
Miklos Szeredic36960462015-07-01 16:26:09 +02001395 ret = fuse_dev_do_read(fud, in, &cs, len);
Miklos Szeredic3021622010-05-25 15:06:07 +02001396 if (ret < 0)
1397 goto out;
1398
Miklos Szeredic3021622010-05-25 15:06:07 +02001399 if (pipe->nrbufs + cs.nr_segs > pipe->buffers) {
1400 ret = -EIO;
Al Virod82718e2016-09-17 22:56:25 -04001401 goto out;
Miklos Szeredic3021622010-05-25 15:06:07 +02001402 }
1403
Al Virod82718e2016-09-17 22:56:25 -04001404 for (ret = total = 0; page_nr < cs.nr_segs; total += ret) {
Miklos Szeredi28a625c2014-01-22 19:36:57 +01001405 /*
1406 * Need to be careful about this. Having buf->ops in module
1407 * code can Oops if the buffer persists after module unload.
1408 */
Al Virod82718e2016-09-17 22:56:25 -04001409 bufs[page_nr].ops = &nosteal_pipe_buf_ops;
Miklos Szeredi84588a92017-02-16 15:08:20 +01001410 bufs[page_nr].flags = 0;
Al Virod82718e2016-09-17 22:56:25 -04001411 ret = add_to_pipe(pipe, &bufs[page_nr++]);
1412 if (unlikely(ret < 0))
1413 break;
Miklos Szeredic3021622010-05-25 15:06:07 +02001414 }
Al Virod82718e2016-09-17 22:56:25 -04001415 if (total)
1416 ret = total;
Miklos Szeredic3021622010-05-25 15:06:07 +02001417out:
1418 for (; page_nr < cs.nr_segs; page_nr++)
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001419 put_page(bufs[page_nr].page);
Miklos Szeredic3021622010-05-25 15:06:07 +02001420
Andrey Ryabinind6d931a2018-07-17 19:00:34 +03001421 kvfree(bufs);
Miklos Szeredic3021622010-05-25 15:06:07 +02001422 return ret;
1423}
1424
Tejun Heo95668a62008-11-26 12:03:55 +01001425static int fuse_notify_poll(struct fuse_conn *fc, unsigned int size,
1426 struct fuse_copy_state *cs)
1427{
1428 struct fuse_notify_poll_wakeup_out outarg;
Miklos Szeredif6d47a12009-01-26 15:00:59 +01001429 int err = -EINVAL;
Tejun Heo95668a62008-11-26 12:03:55 +01001430
1431 if (size != sizeof(outarg))
Miklos Szeredif6d47a12009-01-26 15:00:59 +01001432 goto err;
Tejun Heo95668a62008-11-26 12:03:55 +01001433
1434 err = fuse_copy_one(cs, &outarg, sizeof(outarg));
1435 if (err)
Miklos Szeredif6d47a12009-01-26 15:00:59 +01001436 goto err;
Tejun Heo95668a62008-11-26 12:03:55 +01001437
Miklos Szeredif6d47a12009-01-26 15:00:59 +01001438 fuse_copy_finish(cs);
Tejun Heo95668a62008-11-26 12:03:55 +01001439 return fuse_notify_poll_wakeup(fc, &outarg);
Miklos Szeredif6d47a12009-01-26 15:00:59 +01001440
1441err:
1442 fuse_copy_finish(cs);
1443 return err;
Tejun Heo95668a62008-11-26 12:03:55 +01001444}
1445
John Muir3b463ae2009-05-31 11:13:57 -04001446static int fuse_notify_inval_inode(struct fuse_conn *fc, unsigned int size,
1447 struct fuse_copy_state *cs)
1448{
1449 struct fuse_notify_inval_inode_out outarg;
1450 int err = -EINVAL;
1451
1452 if (size != sizeof(outarg))
1453 goto err;
1454
1455 err = fuse_copy_one(cs, &outarg, sizeof(outarg));
1456 if (err)
1457 goto err;
1458 fuse_copy_finish(cs);
1459
1460 down_read(&fc->killsb);
1461 err = -ENOENT;
Miklos Szeredib21dda42010-02-05 12:08:31 +01001462 if (fc->sb) {
1463 err = fuse_reverse_inval_inode(fc->sb, outarg.ino,
1464 outarg.off, outarg.len);
1465 }
John Muir3b463ae2009-05-31 11:13:57 -04001466 up_read(&fc->killsb);
1467 return err;
1468
1469err:
1470 fuse_copy_finish(cs);
1471 return err;
1472}
1473
1474static int fuse_notify_inval_entry(struct fuse_conn *fc, unsigned int size,
1475 struct fuse_copy_state *cs)
1476{
1477 struct fuse_notify_inval_entry_out outarg;
Fang Wenqib2d82ee2009-12-30 18:37:13 +08001478 int err = -ENOMEM;
1479 char *buf;
John Muir3b463ae2009-05-31 11:13:57 -04001480 struct qstr name;
1481
Fang Wenqib2d82ee2009-12-30 18:37:13 +08001482 buf = kzalloc(FUSE_NAME_MAX + 1, GFP_KERNEL);
1483 if (!buf)
1484 goto err;
1485
1486 err = -EINVAL;
John Muir3b463ae2009-05-31 11:13:57 -04001487 if (size < sizeof(outarg))
1488 goto err;
1489
1490 err = fuse_copy_one(cs, &outarg, sizeof(outarg));
1491 if (err)
1492 goto err;
1493
1494 err = -ENAMETOOLONG;
1495 if (outarg.namelen > FUSE_NAME_MAX)
1496 goto err;
1497
Miklos Szeredic2183d12011-08-24 10:20:17 +02001498 err = -EINVAL;
1499 if (size != sizeof(outarg) + outarg.namelen + 1)
1500 goto err;
1501
John Muir3b463ae2009-05-31 11:13:57 -04001502 name.name = buf;
1503 name.len = outarg.namelen;
1504 err = fuse_copy_one(cs, buf, outarg.namelen + 1);
1505 if (err)
1506 goto err;
1507 fuse_copy_finish(cs);
1508 buf[outarg.namelen] = 0;
John Muir3b463ae2009-05-31 11:13:57 -04001509
1510 down_read(&fc->killsb);
1511 err = -ENOENT;
Miklos Szeredib21dda42010-02-05 12:08:31 +01001512 if (fc->sb)
John Muir451d0f52011-12-06 21:50:06 +01001513 err = fuse_reverse_inval_entry(fc->sb, outarg.parent, 0, &name);
1514 up_read(&fc->killsb);
1515 kfree(buf);
1516 return err;
1517
1518err:
1519 kfree(buf);
1520 fuse_copy_finish(cs);
1521 return err;
1522}
1523
1524static int fuse_notify_delete(struct fuse_conn *fc, unsigned int size,
1525 struct fuse_copy_state *cs)
1526{
1527 struct fuse_notify_delete_out outarg;
1528 int err = -ENOMEM;
1529 char *buf;
1530 struct qstr name;
1531
1532 buf = kzalloc(FUSE_NAME_MAX + 1, GFP_KERNEL);
1533 if (!buf)
1534 goto err;
1535
1536 err = -EINVAL;
1537 if (size < sizeof(outarg))
1538 goto err;
1539
1540 err = fuse_copy_one(cs, &outarg, sizeof(outarg));
1541 if (err)
1542 goto err;
1543
1544 err = -ENAMETOOLONG;
1545 if (outarg.namelen > FUSE_NAME_MAX)
1546 goto err;
1547
1548 err = -EINVAL;
1549 if (size != sizeof(outarg) + outarg.namelen + 1)
1550 goto err;
1551
1552 name.name = buf;
1553 name.len = outarg.namelen;
1554 err = fuse_copy_one(cs, buf, outarg.namelen + 1);
1555 if (err)
1556 goto err;
1557 fuse_copy_finish(cs);
1558 buf[outarg.namelen] = 0;
John Muir451d0f52011-12-06 21:50:06 +01001559
1560 down_read(&fc->killsb);
1561 err = -ENOENT;
1562 if (fc->sb)
1563 err = fuse_reverse_inval_entry(fc->sb, outarg.parent,
1564 outarg.child, &name);
John Muir3b463ae2009-05-31 11:13:57 -04001565 up_read(&fc->killsb);
Fang Wenqib2d82ee2009-12-30 18:37:13 +08001566 kfree(buf);
John Muir3b463ae2009-05-31 11:13:57 -04001567 return err;
1568
1569err:
Fang Wenqib2d82ee2009-12-30 18:37:13 +08001570 kfree(buf);
John Muir3b463ae2009-05-31 11:13:57 -04001571 fuse_copy_finish(cs);
1572 return err;
1573}
1574
Miklos Szeredia1d75f22010-07-12 14:41:40 +02001575static int fuse_notify_store(struct fuse_conn *fc, unsigned int size,
1576 struct fuse_copy_state *cs)
1577{
1578 struct fuse_notify_store_out outarg;
1579 struct inode *inode;
1580 struct address_space *mapping;
1581 u64 nodeid;
1582 int err;
1583 pgoff_t index;
1584 unsigned int offset;
1585 unsigned int num;
1586 loff_t file_size;
1587 loff_t end;
1588
1589 err = -EINVAL;
1590 if (size < sizeof(outarg))
1591 goto out_finish;
1592
1593 err = fuse_copy_one(cs, &outarg, sizeof(outarg));
1594 if (err)
1595 goto out_finish;
1596
1597 err = -EINVAL;
1598 if (size - sizeof(outarg) != outarg.size)
1599 goto out_finish;
1600
1601 nodeid = outarg.nodeid;
1602
1603 down_read(&fc->killsb);
1604
1605 err = -ENOENT;
1606 if (!fc->sb)
1607 goto out_up_killsb;
1608
1609 inode = ilookup5(fc->sb, nodeid, fuse_inode_eq, &nodeid);
1610 if (!inode)
1611 goto out_up_killsb;
1612
1613 mapping = inode->i_mapping;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001614 index = outarg.offset >> PAGE_SHIFT;
1615 offset = outarg.offset & ~PAGE_MASK;
Miklos Szeredia1d75f22010-07-12 14:41:40 +02001616 file_size = i_size_read(inode);
1617 end = outarg.offset + outarg.size;
1618 if (end > file_size) {
1619 file_size = end;
1620 fuse_write_update_size(inode, file_size);
1621 }
1622
1623 num = outarg.size;
1624 while (num) {
1625 struct page *page;
1626 unsigned int this_num;
1627
1628 err = -ENOMEM;
1629 page = find_or_create_page(mapping, index,
1630 mapping_gfp_mask(mapping));
1631 if (!page)
1632 goto out_iput;
1633
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001634 this_num = min_t(unsigned, num, PAGE_SIZE - offset);
Miklos Szeredia1d75f22010-07-12 14:41:40 +02001635 err = fuse_copy_page(cs, &page, offset, this_num, 0);
Miklos Szeredi063ec1e2014-01-22 19:36:58 +01001636 if (!err && offset == 0 &&
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001637 (this_num == PAGE_SIZE || file_size == end))
Miklos Szeredia1d75f22010-07-12 14:41:40 +02001638 SetPageUptodate(page);
1639 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001640 put_page(page);
Miklos Szeredia1d75f22010-07-12 14:41:40 +02001641
1642 if (err)
1643 goto out_iput;
1644
1645 num -= this_num;
1646 offset = 0;
1647 index++;
1648 }
1649
1650 err = 0;
1651
1652out_iput:
1653 iput(inode);
1654out_up_killsb:
1655 up_read(&fc->killsb);
1656out_finish:
1657 fuse_copy_finish(cs);
1658 return err;
1659}
1660
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001661static void fuse_retrieve_end(struct fuse_conn *fc, struct fuse_req *req)
1662{
Mel Gormanc6f92f92017-11-15 17:37:55 -08001663 release_pages(req->pages, req->num_pages);
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001664}
1665
1666static int fuse_retrieve(struct fuse_conn *fc, struct inode *inode,
1667 struct fuse_notify_retrieve_out *outarg)
1668{
1669 int err;
1670 struct address_space *mapping = inode->i_mapping;
1671 struct fuse_req *req;
1672 pgoff_t index;
1673 loff_t file_size;
1674 unsigned int num;
1675 unsigned int offset;
Geert Uytterhoeven01574432010-09-30 22:06:21 +02001676 size_t total_len = 0;
Maxim Patlasov4d53dc92012-10-26 19:48:42 +04001677 int num_pages;
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001678
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001679 offset = outarg->offset & ~PAGE_MASK;
Maxim Patlasov4d53dc92012-10-26 19:48:42 +04001680 file_size = i_size_read(inode);
1681
1682 num = outarg->size;
1683 if (outarg->offset > file_size)
1684 num = 0;
1685 else if (outarg->offset + num > file_size)
1686 num = file_size - outarg->offset;
1687
1688 num_pages = (num + offset + PAGE_SIZE - 1) >> PAGE_SHIFT;
1689 num_pages = min(num_pages, FUSE_MAX_PAGES_PER_REQ);
1690
1691 req = fuse_get_req(fc, num_pages);
1692 if (IS_ERR(req))
1693 return PTR_ERR(req);
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001694
1695 req->in.h.opcode = FUSE_NOTIFY_REPLY;
1696 req->in.h.nodeid = outarg->nodeid;
1697 req->in.numargs = 2;
1698 req->in.argpages = 1;
Maxim Patlasovb2430d72012-10-26 19:49:24 +04001699 req->page_descs[0].offset = offset;
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001700 req->end = fuse_retrieve_end;
1701
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001702 index = outarg->offset >> PAGE_SHIFT;
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001703
Maxim Patlasov4d53dc92012-10-26 19:48:42 +04001704 while (num && req->num_pages < num_pages) {
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001705 struct page *page;
1706 unsigned int this_num;
1707
1708 page = find_get_page(mapping, index);
1709 if (!page)
1710 break;
1711
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001712 this_num = min_t(unsigned, num, PAGE_SIZE - offset);
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001713 req->pages[req->num_pages] = page;
Maxim Patlasov85f40ae2012-10-26 19:49:33 +04001714 req->page_descs[req->num_pages].length = this_num;
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001715 req->num_pages++;
1716
Miklos Szeredic9e67d42012-09-04 18:45:54 +02001717 offset = 0;
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001718 num -= this_num;
1719 total_len += this_num;
Miklos Szeredi48706d02011-12-13 10:36:59 +01001720 index++;
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001721 }
1722 req->misc.retrieve_in.offset = outarg->offset;
1723 req->misc.retrieve_in.size = total_len;
1724 req->in.args[0].size = sizeof(req->misc.retrieve_in);
1725 req->in.args[0].value = &req->misc.retrieve_in;
1726 req->in.args[1].size = total_len;
1727
1728 err = fuse_request_send_notify_reply(fc, req, outarg->notify_unique);
1729 if (err)
1730 fuse_retrieve_end(fc, req);
1731
1732 return err;
1733}
1734
1735static int fuse_notify_retrieve(struct fuse_conn *fc, unsigned int size,
1736 struct fuse_copy_state *cs)
1737{
1738 struct fuse_notify_retrieve_out outarg;
1739 struct inode *inode;
1740 int err;
1741
1742 err = -EINVAL;
1743 if (size != sizeof(outarg))
1744 goto copy_finish;
1745
1746 err = fuse_copy_one(cs, &outarg, sizeof(outarg));
1747 if (err)
1748 goto copy_finish;
1749
1750 fuse_copy_finish(cs);
1751
1752 down_read(&fc->killsb);
1753 err = -ENOENT;
1754 if (fc->sb) {
1755 u64 nodeid = outarg.nodeid;
1756
1757 inode = ilookup5(fc->sb, nodeid, fuse_inode_eq, &nodeid);
1758 if (inode) {
1759 err = fuse_retrieve(fc, inode, &outarg);
1760 iput(inode);
1761 }
1762 }
1763 up_read(&fc->killsb);
1764
1765 return err;
1766
1767copy_finish:
1768 fuse_copy_finish(cs);
1769 return err;
1770}
1771
Tejun Heo85993962008-11-26 12:03:55 +01001772static int fuse_notify(struct fuse_conn *fc, enum fuse_notify_code code,
1773 unsigned int size, struct fuse_copy_state *cs)
1774{
Miklos Szeredi0d278362015-02-26 11:45:47 +01001775 /* Don't try to move pages (yet) */
1776 cs->move_pages = 0;
1777
Tejun Heo85993962008-11-26 12:03:55 +01001778 switch (code) {
Tejun Heo95668a62008-11-26 12:03:55 +01001779 case FUSE_NOTIFY_POLL:
1780 return fuse_notify_poll(fc, size, cs);
1781
John Muir3b463ae2009-05-31 11:13:57 -04001782 case FUSE_NOTIFY_INVAL_INODE:
1783 return fuse_notify_inval_inode(fc, size, cs);
1784
1785 case FUSE_NOTIFY_INVAL_ENTRY:
1786 return fuse_notify_inval_entry(fc, size, cs);
1787
Miklos Szeredia1d75f22010-07-12 14:41:40 +02001788 case FUSE_NOTIFY_STORE:
1789 return fuse_notify_store(fc, size, cs);
1790
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001791 case FUSE_NOTIFY_RETRIEVE:
1792 return fuse_notify_retrieve(fc, size, cs);
1793
John Muir451d0f52011-12-06 21:50:06 +01001794 case FUSE_NOTIFY_DELETE:
1795 return fuse_notify_delete(fc, size, cs);
1796
Tejun Heo85993962008-11-26 12:03:55 +01001797 default:
Miklos Szeredif6d47a12009-01-26 15:00:59 +01001798 fuse_copy_finish(cs);
Tejun Heo85993962008-11-26 12:03:55 +01001799 return -EINVAL;
1800 }
1801}
1802
Miklos Szeredi334f4852005-09-09 13:10:27 -07001803/* Look up request on processing list by unique ID */
Miklos Szeredi3a2b5b92015-07-01 16:26:04 +02001804static struct fuse_req *request_find(struct fuse_pqueue *fpq, u64 unique)
Miklos Szeredi334f4852005-09-09 13:10:27 -07001805{
Kirill Tkhaibe2ff422018-09-11 13:12:14 +03001806 unsigned int hash = fuse_req_hash(unique);
Dong Fang05726ac2013-07-30 22:50:01 -04001807 struct fuse_req *req;
Miklos Szeredi334f4852005-09-09 13:10:27 -07001808
Kirill Tkhaibe2ff422018-09-11 13:12:14 +03001809 list_for_each_entry(req, &fpq->processing[hash], list) {
Kirill Tkhai3a5358d2018-09-11 13:12:05 +03001810 if (req->in.h.unique == unique)
Miklos Szeredi334f4852005-09-09 13:10:27 -07001811 return req;
1812 }
1813 return NULL;
1814}
1815
1816static int copy_out_args(struct fuse_copy_state *cs, struct fuse_out *out,
1817 unsigned nbytes)
1818{
1819 unsigned reqsize = sizeof(struct fuse_out_header);
1820
1821 if (out->h.error)
1822 return nbytes != reqsize ? -EINVAL : 0;
1823
1824 reqsize += len_args(out->numargs, out->args);
1825
1826 if (reqsize < nbytes || (reqsize > nbytes && !out->argvar))
1827 return -EINVAL;
1828 else if (reqsize > nbytes) {
1829 struct fuse_arg *lastarg = &out->args[out->numargs-1];
1830 unsigned diffsize = reqsize - nbytes;
1831 if (diffsize > lastarg->size)
1832 return -EINVAL;
1833 lastarg->size -= diffsize;
1834 }
1835 return fuse_copy_args(cs, out->numargs, out->argpages, out->args,
1836 out->page_zeroing);
1837}
1838
1839/*
1840 * Write a single reply to a request. First the header is copied from
1841 * the write buffer. The request is then searched on the processing
1842 * list by the unique ID found in the header. If found, then remove
1843 * it from the list and copy the rest of the buffer to the request.
1844 * The request is finished by calling request_end()
1845 */
Miklos Szeredic36960462015-07-01 16:26:09 +02001846static ssize_t fuse_dev_do_write(struct fuse_dev *fud,
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001847 struct fuse_copy_state *cs, size_t nbytes)
Miklos Szeredi334f4852005-09-09 13:10:27 -07001848{
1849 int err;
Miklos Szeredic36960462015-07-01 16:26:09 +02001850 struct fuse_conn *fc = fud->fc;
1851 struct fuse_pqueue *fpq = &fud->pq;
Miklos Szeredi334f4852005-09-09 13:10:27 -07001852 struct fuse_req *req;
1853 struct fuse_out_header oh;
Miklos Szeredi334f4852005-09-09 13:10:27 -07001854
Miklos Szeredi334f4852005-09-09 13:10:27 -07001855 if (nbytes < sizeof(struct fuse_out_header))
1856 return -EINVAL;
1857
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001858 err = fuse_copy_one(cs, &oh, sizeof(oh));
Miklos Szeredi334f4852005-09-09 13:10:27 -07001859 if (err)
1860 goto err_finish;
Tejun Heo85993962008-11-26 12:03:55 +01001861
Miklos Szeredi334f4852005-09-09 13:10:27 -07001862 err = -EINVAL;
Tejun Heo85993962008-11-26 12:03:55 +01001863 if (oh.len != nbytes)
1864 goto err_finish;
1865
1866 /*
1867 * Zero oh.unique indicates unsolicited notification message
1868 * and error contains notification code.
1869 */
1870 if (!oh.unique) {
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001871 err = fuse_notify(fc, oh.error, nbytes - sizeof(oh), cs);
Tejun Heo85993962008-11-26 12:03:55 +01001872 return err ? err : nbytes;
1873 }
1874
1875 err = -EINVAL;
1876 if (oh.error <= -1000 || oh.error > 0)
Miklos Szeredi334f4852005-09-09 13:10:27 -07001877 goto err_finish;
1878
Miklos Szeredi45a91cb2015-07-01 16:26:06 +02001879 spin_lock(&fpq->lock);
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08001880 err = -ENOENT;
Miklos Szeredie96edd92015-07-01 16:26:04 +02001881 if (!fpq->connected)
Miklos Szeredi45a91cb2015-07-01 16:26:06 +02001882 goto err_unlock_pq;
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08001883
Kirill Tkhai3a5358d2018-09-11 13:12:05 +03001884 req = request_find(fpq, oh.unique & ~FUSE_INT_REQ_BIT);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001885 if (!req)
Miklos Szeredi45a91cb2015-07-01 16:26:06 +02001886 goto err_unlock_pq;
Miklos Szeredi334f4852005-09-09 13:10:27 -07001887
Kirill Tkhai3a5358d2018-09-11 13:12:05 +03001888 /* Is it an interrupt reply ID? */
1889 if (oh.unique & FUSE_INT_REQ_BIT) {
Kirill Tkhaid2d2d4f2018-09-25 12:52:42 +03001890 __fuse_get_request(req);
Miklos Szeredi45a91cb2015-07-01 16:26:06 +02001891 spin_unlock(&fpq->lock);
1892
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001893 err = -EINVAL;
Kirill Tkhaid2d2d4f2018-09-25 12:52:42 +03001894 if (nbytes != sizeof(struct fuse_out_header)) {
1895 fuse_put_request(fc, req);
Miklos Szeredi46c34a32015-07-01 16:26:07 +02001896 goto err_finish;
Kirill Tkhaid2d2d4f2018-09-25 12:52:42 +03001897 }
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001898
1899 if (oh.error == -ENOSYS)
1900 fc->no_interrupt = 1;
1901 else if (oh.error == -EAGAIN)
Miklos Szeredif88996a2015-07-01 16:26:01 +02001902 queue_interrupt(&fc->iq, req);
Kirill Tkhaid2d2d4f2018-09-25 12:52:42 +03001903 fuse_put_request(fc, req);
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001904
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001905 fuse_copy_finish(cs);
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001906 return nbytes;
1907 }
1908
Miklos Szeredi33e14b42015-07-01 16:26:01 +02001909 clear_bit(FR_SENT, &req->flags);
Miklos Szeredi3a2b5b92015-07-01 16:26:04 +02001910 list_move(&req->list, &fpq->io);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001911 req->out.h = oh;
Miklos Szeredi825d6d32015-07-01 16:25:58 +02001912 set_bit(FR_LOCKED, &req->flags);
Miklos Szeredi45a91cb2015-07-01 16:26:06 +02001913 spin_unlock(&fpq->lock);
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001914 cs->req = req;
Miklos Szeredice534fb2010-05-25 15:06:07 +02001915 if (!req->out.page_replace)
1916 cs->move_pages = 0;
Miklos Szeredi334f4852005-09-09 13:10:27 -07001917
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001918 err = copy_out_args(cs, &req->out, nbytes);
1919 fuse_copy_finish(cs);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001920
Miklos Szeredi45a91cb2015-07-01 16:26:06 +02001921 spin_lock(&fpq->lock);
Miklos Szeredi825d6d32015-07-01 16:25:58 +02001922 clear_bit(FR_LOCKED, &req->flags);
Miklos Szeredie96edd92015-07-01 16:26:04 +02001923 if (!fpq->connected)
Miklos Szeredi0d8e84b2015-07-01 16:25:58 +02001924 err = -ENOENT;
1925 else if (err)
Miklos Szeredi334f4852005-09-09 13:10:27 -07001926 req->out.h.error = -EIO;
Miklos Szeredi77cd9d42015-07-01 16:26:06 +02001927 if (!test_bit(FR_PRIVATE, &req->flags))
1928 list_del_init(&req->list);
Miklos Szeredi45a91cb2015-07-01 16:26:06 +02001929 spin_unlock(&fpq->lock);
Miklos Szeredi46c34a32015-07-01 16:26:07 +02001930
Miklos Szeredi334f4852005-09-09 13:10:27 -07001931 request_end(fc, req);
1932
1933 return err ? err : nbytes;
1934
Miklos Szeredi45a91cb2015-07-01 16:26:06 +02001935 err_unlock_pq:
1936 spin_unlock(&fpq->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001937 err_finish:
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001938 fuse_copy_finish(cs);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001939 return err;
1940}
1941
Al Virofbdbacc2015-04-03 21:53:39 -04001942static ssize_t fuse_dev_write(struct kiocb *iocb, struct iov_iter *from)
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001943{
1944 struct fuse_copy_state cs;
Miklos Szeredicc080e92015-07-01 16:26:08 +02001945 struct fuse_dev *fud = fuse_get_dev(iocb->ki_filp);
1946
1947 if (!fud)
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001948 return -EPERM;
1949
Al Virofbdbacc2015-04-03 21:53:39 -04001950 if (!iter_is_iovec(from))
1951 return -EINVAL;
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001952
Miklos Szeredidc008092015-07-01 16:25:58 +02001953 fuse_copy_init(&cs, 0, from);
Al Virofbdbacc2015-04-03 21:53:39 -04001954
Miklos Szeredic36960462015-07-01 16:26:09 +02001955 return fuse_dev_do_write(fud, &cs, iov_iter_count(from));
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001956}
1957
1958static ssize_t fuse_dev_splice_write(struct pipe_inode_info *pipe,
1959 struct file *out, loff_t *ppos,
1960 size_t len, unsigned int flags)
1961{
1962 unsigned nbuf;
1963 unsigned idx;
1964 struct pipe_buffer *bufs;
1965 struct fuse_copy_state cs;
Miklos Szeredicc080e92015-07-01 16:26:08 +02001966 struct fuse_dev *fud;
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001967 size_t rem;
1968 ssize_t ret;
1969
Miklos Szeredicc080e92015-07-01 16:26:08 +02001970 fud = fuse_get_dev(out);
1971 if (!fud)
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001972 return -EPERM;
1973
Andrey Ryabinina2477b02018-07-17 19:00:33 +03001974 pipe_lock(pipe);
1975
Andrey Ryabinin96354532018-07-17 19:00:35 +03001976 bufs = kvmalloc_array(pipe->nrbufs, sizeof(struct pipe_buffer),
Andrey Ryabinind6d931a2018-07-17 19:00:34 +03001977 GFP_KERNEL);
Andrey Ryabinina2477b02018-07-17 19:00:33 +03001978 if (!bufs) {
1979 pipe_unlock(pipe);
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001980 return -ENOMEM;
Andrey Ryabinina2477b02018-07-17 19:00:33 +03001981 }
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001982
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001983 nbuf = 0;
1984 rem = 0;
1985 for (idx = 0; idx < pipe->nrbufs && rem < len; idx++)
1986 rem += pipe->bufs[(pipe->curbuf + idx) & (pipe->buffers - 1)].len;
1987
1988 ret = -EINVAL;
1989 if (rem < len) {
1990 pipe_unlock(pipe);
1991 goto out;
1992 }
1993
1994 rem = len;
1995 while (rem) {
1996 struct pipe_buffer *ibuf;
1997 struct pipe_buffer *obuf;
1998
1999 BUG_ON(nbuf >= pipe->buffers);
2000 BUG_ON(!pipe->nrbufs);
2001 ibuf = &pipe->bufs[pipe->curbuf];
2002 obuf = &bufs[nbuf];
2003
2004 if (rem >= ibuf->len) {
2005 *obuf = *ibuf;
2006 ibuf->ops = NULL;
2007 pipe->curbuf = (pipe->curbuf + 1) & (pipe->buffers - 1);
2008 pipe->nrbufs--;
2009 } else {
Miklos Szeredi7bf2d1d2016-09-27 10:45:12 +02002010 pipe_buf_get(pipe, ibuf);
Miklos Szeredidd3bb142010-05-25 15:06:06 +02002011 *obuf = *ibuf;
2012 obuf->flags &= ~PIPE_BUF_FLAG_GIFT;
2013 obuf->len = rem;
2014 ibuf->offset += obuf->len;
2015 ibuf->len -= obuf->len;
2016 }
2017 nbuf++;
2018 rem -= obuf->len;
2019 }
2020 pipe_unlock(pipe);
2021
Miklos Szeredidc008092015-07-01 16:25:58 +02002022 fuse_copy_init(&cs, 0, NULL);
Miklos Szeredidd3bb142010-05-25 15:06:06 +02002023 cs.pipebufs = bufs;
Al Viro6c09e942015-04-03 22:06:08 -04002024 cs.nr_segs = nbuf;
Miklos Szeredidd3bb142010-05-25 15:06:06 +02002025 cs.pipe = pipe;
2026
Miklos Szeredice534fb2010-05-25 15:06:07 +02002027 if (flags & SPLICE_F_MOVE)
2028 cs.move_pages = 1;
2029
Miklos Szeredic36960462015-07-01 16:26:09 +02002030 ret = fuse_dev_do_write(fud, &cs, len);
Miklos Szeredidd3bb142010-05-25 15:06:06 +02002031
Miklos Szeredia7796382016-09-27 10:45:12 +02002032 for (idx = 0; idx < nbuf; idx++)
2033 pipe_buf_release(pipe, &bufs[idx]);
2034
Miklos Szeredidd3bb142010-05-25 15:06:06 +02002035out:
Andrey Ryabinind6d931a2018-07-17 19:00:34 +03002036 kvfree(bufs);
Miklos Szeredidd3bb142010-05-25 15:06:06 +02002037 return ret;
2038}
2039
Al Viro076ccb72017-07-03 01:02:18 -04002040static __poll_t fuse_dev_poll(struct file *file, poll_table *wait)
Miklos Szeredi334f4852005-09-09 13:10:27 -07002041{
Linus Torvaldsa9a08842018-02-11 14:34:03 -08002042 __poll_t mask = EPOLLOUT | EPOLLWRNORM;
Miklos Szeredif88996a2015-07-01 16:26:01 +02002043 struct fuse_iqueue *fiq;
Miklos Szeredicc080e92015-07-01 16:26:08 +02002044 struct fuse_dev *fud = fuse_get_dev(file);
2045
2046 if (!fud)
Linus Torvaldsa9a08842018-02-11 14:34:03 -08002047 return EPOLLERR;
Miklos Szeredi334f4852005-09-09 13:10:27 -07002048
Miklos Szeredicc080e92015-07-01 16:26:08 +02002049 fiq = &fud->fc->iq;
Miklos Szeredif88996a2015-07-01 16:26:01 +02002050 poll_wait(file, &fiq->waitq, wait);
Miklos Szeredi334f4852005-09-09 13:10:27 -07002051
Miklos Szeredi4ce60812015-07-01 16:26:02 +02002052 spin_lock(&fiq->waitq.lock);
Miklos Szeredie16714d2015-07-01 16:26:01 +02002053 if (!fiq->connected)
Linus Torvaldsa9a08842018-02-11 14:34:03 -08002054 mask = EPOLLERR;
Miklos Szeredif88996a2015-07-01 16:26:01 +02002055 else if (request_pending(fiq))
Linus Torvaldsa9a08842018-02-11 14:34:03 -08002056 mask |= EPOLLIN | EPOLLRDNORM;
Miklos Szeredi4ce60812015-07-01 16:26:02 +02002057 spin_unlock(&fiq->waitq.lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -07002058
2059 return mask;
2060}
2061
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002062/*
2063 * Abort all requests on the given list (pending or processing)
2064 *
Miklos Szeredid7133112006-04-10 22:54:55 -07002065 * This function releases and reacquires fc->lock
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002066 */
Miklos Szeredi334f4852005-09-09 13:10:27 -07002067static void end_requests(struct fuse_conn *fc, struct list_head *head)
2068{
2069 while (!list_empty(head)) {
2070 struct fuse_req *req;
2071 req = list_entry(head->next, struct fuse_req, list);
Miklos Szeredi334f4852005-09-09 13:10:27 -07002072 req->out.h.error = -ECONNABORTED;
Miklos Szeredi33e14b42015-07-01 16:26:01 +02002073 clear_bit(FR_SENT, &req->flags);
Miklos Szeredif377cb72015-07-01 16:26:04 +02002074 list_del_init(&req->list);
Miklos Szeredi334f4852005-09-09 13:10:27 -07002075 request_end(fc, req);
Miklos Szeredi334f4852005-09-09 13:10:27 -07002076 }
2077}
2078
Bryan Green357ccf22011-03-01 16:43:52 -08002079static void end_polls(struct fuse_conn *fc)
2080{
2081 struct rb_node *p;
2082
2083 p = rb_first(&fc->polled_files);
2084
2085 while (p) {
2086 struct fuse_file *ff;
2087 ff = rb_entry(p, struct fuse_file, polled_node);
2088 wake_up_interruptible_all(&ff->poll_wait);
2089
2090 p = rb_next(p);
2091 }
2092}
2093
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002094/*
2095 * Abort all requests.
2096 *
Miklos Szeredib716d422015-07-01 16:25:59 +02002097 * Emergency exit in case of a malicious or accidental deadlock, or just a hung
2098 * filesystem.
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002099 *
Miklos Szeredib716d422015-07-01 16:25:59 +02002100 * The same effect is usually achievable through killing the filesystem daemon
2101 * and all users of the filesystem. The exception is the combination of an
2102 * asynchronous request and the tricky deadlock (see
2103 * Documentation/filesystems/fuse.txt).
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002104 *
Miklos Szeredib716d422015-07-01 16:25:59 +02002105 * Aborting requests under I/O goes as follows: 1: Separate out unlocked
2106 * requests, they should be finished off immediately. Locked requests will be
2107 * finished after unlock; see unlock_request(). 2: Finish off the unlocked
2108 * requests. It is possible that some request will finish before we can. This
2109 * is OK, the request will in that case be removed from the list before we touch
2110 * it.
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002111 */
Szymon Lukasz3b7008b2017-11-09 21:23:35 +01002112void fuse_abort_conn(struct fuse_conn *fc, bool is_abort)
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002113{
Miklos Szeredif88996a2015-07-01 16:26:01 +02002114 struct fuse_iqueue *fiq = &fc->iq;
2115
Miklos Szeredid7133112006-04-10 22:54:55 -07002116 spin_lock(&fc->lock);
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002117 if (fc->connected) {
Miklos Szeredic36960462015-07-01 16:26:09 +02002118 struct fuse_dev *fud;
Miklos Szeredib716d422015-07-01 16:25:59 +02002119 struct fuse_req *req, *next;
Miklos Szeredi75f3ee42018-07-26 16:13:12 +02002120 LIST_HEAD(to_end);
Kirill Tkhaibe2ff422018-09-11 13:12:14 +03002121 unsigned int i;
Miklos Szeredib716d422015-07-01 16:25:59 +02002122
Kirill Tkhai63825b42018-08-27 18:29:56 +03002123 /* Background queuing checks fc->connected under bg_lock */
2124 spin_lock(&fc->bg_lock);
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002125 fc->connected = 0;
Kirill Tkhai63825b42018-08-27 18:29:56 +03002126 spin_unlock(&fc->bg_lock);
2127
Szymon Lukasz3b7008b2017-11-09 21:23:35 +01002128 fc->aborted = is_abort;
Miklos Szeredi9759bd512015-01-06 10:45:35 +01002129 fuse_set_initialized(fc);
Miklos Szeredic36960462015-07-01 16:26:09 +02002130 list_for_each_entry(fud, &fc->devices, entry) {
2131 struct fuse_pqueue *fpq = &fud->pq;
2132
2133 spin_lock(&fpq->lock);
2134 fpq->connected = 0;
2135 list_for_each_entry_safe(req, next, &fpq->io, list) {
2136 req->out.h.error = -ECONNABORTED;
2137 spin_lock(&req->waitq.lock);
2138 set_bit(FR_ABORTED, &req->flags);
2139 if (!test_bit(FR_LOCKED, &req->flags)) {
2140 set_bit(FR_PRIVATE, &req->flags);
Miklos Szeredi87114372018-07-26 16:13:11 +02002141 __fuse_get_request(req);
Miklos Szeredi75f3ee42018-07-26 16:13:12 +02002142 list_move(&req->list, &to_end);
Miklos Szeredic36960462015-07-01 16:26:09 +02002143 }
2144 spin_unlock(&req->waitq.lock);
Miklos Szeredi77cd9d42015-07-01 16:26:06 +02002145 }
Kirill Tkhaibe2ff422018-09-11 13:12:14 +03002146 for (i = 0; i < FUSE_PQ_HASH_SIZE; i++)
2147 list_splice_tail_init(&fpq->processing[i],
2148 &to_end);
Miklos Szeredic36960462015-07-01 16:26:09 +02002149 spin_unlock(&fpq->lock);
Miklos Szeredib716d422015-07-01 16:25:59 +02002150 }
Kirill Tkhaiae2dffa2018-08-27 18:29:46 +03002151 spin_lock(&fc->bg_lock);
2152 fc->blocked = 0;
Miklos Szeredi41f98272015-07-01 16:25:59 +02002153 fc->max_background = UINT_MAX;
2154 flush_bg_queue(fc);
Kirill Tkhaiae2dffa2018-08-27 18:29:46 +03002155 spin_unlock(&fc->bg_lock);
Miklos Szeredi8c911892015-07-01 16:26:02 +02002156
Miklos Szeredi4ce60812015-07-01 16:26:02 +02002157 spin_lock(&fiq->waitq.lock);
Miklos Szeredi8c911892015-07-01 16:26:02 +02002158 fiq->connected = 0;
Miklos Szeredi75f3ee42018-07-26 16:13:12 +02002159 list_for_each_entry(req, &fiq->pending, list)
Tahsin Erdogana8a86d72017-01-12 12:04:04 -08002160 clear_bit(FR_PENDING, &req->flags);
Miklos Szeredi75f3ee42018-07-26 16:13:12 +02002161 list_splice_tail_init(&fiq->pending, &to_end);
Miklos Szeredi8c911892015-07-01 16:26:02 +02002162 while (forget_pending(fiq))
2163 kfree(dequeue_forget(fiq, 1, NULL));
Miklos Szeredi4ce60812015-07-01 16:26:02 +02002164 wake_up_all_locked(&fiq->waitq);
2165 spin_unlock(&fiq->waitq.lock);
Miklos Szeredi8c911892015-07-01 16:26:02 +02002166 kill_fasync(&fiq->fasync, SIGIO, POLL_IN);
Miklos Szerediee314a82015-07-01 16:26:08 +02002167 end_polls(fc);
2168 wake_up_all(&fc->blocked_waitq);
2169 spin_unlock(&fc->lock);
Miklos Szeredi8c911892015-07-01 16:26:02 +02002170
Miklos Szeredi75f3ee42018-07-26 16:13:12 +02002171 end_requests(fc, &to_end);
Miklos Szerediee314a82015-07-01 16:26:08 +02002172 } else {
2173 spin_unlock(&fc->lock);
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002174 }
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002175}
Tejun Heo08cbf542009-04-14 10:54:53 +09002176EXPORT_SYMBOL_GPL(fuse_abort_conn);
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002177
Miklos Szeredib8f95e52018-07-26 16:13:11 +02002178void fuse_wait_aborted(struct fuse_conn *fc)
2179{
2180 wait_event(fc->blocked_waitq, atomic_read(&fc->num_waiting) == 0);
2181}
2182
Tejun Heo08cbf542009-04-14 10:54:53 +09002183int fuse_dev_release(struct inode *inode, struct file *file)
Miklos Szeredi334f4852005-09-09 13:10:27 -07002184{
Miklos Szeredicc080e92015-07-01 16:26:08 +02002185 struct fuse_dev *fud = fuse_get_dev(file);
2186
2187 if (fud) {
2188 struct fuse_conn *fc = fud->fc;
Miklos Szeredic36960462015-07-01 16:26:09 +02002189 struct fuse_pqueue *fpq = &fud->pq;
Miklos Szeredi45ff3502018-07-26 16:13:11 +02002190 LIST_HEAD(to_end);
Kirill Tkhaibe2ff422018-09-11 13:12:14 +03002191 unsigned int i;
Miklos Szeredicc080e92015-07-01 16:26:08 +02002192
Miklos Szeredi45ff3502018-07-26 16:13:11 +02002193 spin_lock(&fpq->lock);
Miklos Szeredic36960462015-07-01 16:26:09 +02002194 WARN_ON(!list_empty(&fpq->io));
Kirill Tkhaibe2ff422018-09-11 13:12:14 +03002195 for (i = 0; i < FUSE_PQ_HASH_SIZE; i++)
2196 list_splice_init(&fpq->processing[i], &to_end);
Miklos Szeredi45ff3502018-07-26 16:13:11 +02002197 spin_unlock(&fpq->lock);
2198
2199 end_requests(fc, &to_end);
2200
Miklos Szeredic36960462015-07-01 16:26:09 +02002201 /* Are we the last open device? */
2202 if (atomic_dec_and_test(&fc->dev_count)) {
2203 WARN_ON(fc->iq.fasync != NULL);
Szymon Lukasz3b7008b2017-11-09 21:23:35 +01002204 fuse_abort_conn(fc, false);
Miklos Szeredic36960462015-07-01 16:26:09 +02002205 }
Miklos Szeredicc080e92015-07-01 16:26:08 +02002206 fuse_dev_free(fud);
Jeff Dike385a17b2006-04-10 22:54:52 -07002207 }
Miklos Szeredi334f4852005-09-09 13:10:27 -07002208 return 0;
2209}
Tejun Heo08cbf542009-04-14 10:54:53 +09002210EXPORT_SYMBOL_GPL(fuse_dev_release);
Miklos Szeredi334f4852005-09-09 13:10:27 -07002211
Jeff Dike385a17b2006-04-10 22:54:52 -07002212static int fuse_dev_fasync(int fd, struct file *file, int on)
2213{
Miklos Szeredicc080e92015-07-01 16:26:08 +02002214 struct fuse_dev *fud = fuse_get_dev(file);
2215
2216 if (!fud)
Miklos Szeredia87046d2006-04-10 22:54:56 -07002217 return -EPERM;
Jeff Dike385a17b2006-04-10 22:54:52 -07002218
2219 /* No locking - fasync_helper does its own locking */
Miklos Szeredicc080e92015-07-01 16:26:08 +02002220 return fasync_helper(fd, file, on, &fud->fc->iq.fasync);
Jeff Dike385a17b2006-04-10 22:54:52 -07002221}
2222
Miklos Szeredi00c570f2015-07-01 16:26:08 +02002223static int fuse_device_clone(struct fuse_conn *fc, struct file *new)
2224{
Miklos Szeredicc080e92015-07-01 16:26:08 +02002225 struct fuse_dev *fud;
2226
Miklos Szeredi00c570f2015-07-01 16:26:08 +02002227 if (new->private_data)
2228 return -EINVAL;
2229
Miklos Szeredicc080e92015-07-01 16:26:08 +02002230 fud = fuse_dev_alloc(fc);
2231 if (!fud)
2232 return -ENOMEM;
2233
2234 new->private_data = fud;
Miklos Szeredic36960462015-07-01 16:26:09 +02002235 atomic_inc(&fc->dev_count);
Miklos Szeredi00c570f2015-07-01 16:26:08 +02002236
2237 return 0;
2238}
2239
2240static long fuse_dev_ioctl(struct file *file, unsigned int cmd,
2241 unsigned long arg)
2242{
2243 int err = -ENOTTY;
2244
2245 if (cmd == FUSE_DEV_IOC_CLONE) {
2246 int oldfd;
2247
2248 err = -EFAULT;
2249 if (!get_user(oldfd, (__u32 __user *) arg)) {
2250 struct file *old = fget(oldfd);
2251
2252 err = -EINVAL;
2253 if (old) {
Jann Horn8ed1f0e2015-08-16 20:27:01 +02002254 struct fuse_dev *fud = NULL;
2255
2256 /*
2257 * Check against file->f_op because CUSE
2258 * uses the same ioctl handler.
2259 */
2260 if (old->f_op == file->f_op &&
2261 old->f_cred->user_ns == file->f_cred->user_ns)
2262 fud = fuse_get_dev(old);
Miklos Szeredi00c570f2015-07-01 16:26:08 +02002263
Miklos Szeredicc080e92015-07-01 16:26:08 +02002264 if (fud) {
Miklos Szeredi00c570f2015-07-01 16:26:08 +02002265 mutex_lock(&fuse_mutex);
Miklos Szeredicc080e92015-07-01 16:26:08 +02002266 err = fuse_device_clone(fud->fc, file);
Miklos Szeredi00c570f2015-07-01 16:26:08 +02002267 mutex_unlock(&fuse_mutex);
2268 }
2269 fput(old);
2270 }
2271 }
2272 }
2273 return err;
2274}
2275
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -08002276const struct file_operations fuse_dev_operations = {
Miklos Szeredi334f4852005-09-09 13:10:27 -07002277 .owner = THIS_MODULE,
Tom Van Braeckel94e4fe22015-01-12 05:22:16 +01002278 .open = fuse_dev_open,
Miklos Szeredi334f4852005-09-09 13:10:27 -07002279 .llseek = no_llseek,
Al Virofbdbacc2015-04-03 21:53:39 -04002280 .read_iter = fuse_dev_read,
Miklos Szeredic3021622010-05-25 15:06:07 +02002281 .splice_read = fuse_dev_splice_read,
Al Virofbdbacc2015-04-03 21:53:39 -04002282 .write_iter = fuse_dev_write,
Miklos Szeredidd3bb142010-05-25 15:06:06 +02002283 .splice_write = fuse_dev_splice_write,
Miklos Szeredi334f4852005-09-09 13:10:27 -07002284 .poll = fuse_dev_poll,
2285 .release = fuse_dev_release,
Jeff Dike385a17b2006-04-10 22:54:52 -07002286 .fasync = fuse_dev_fasync,
Miklos Szeredi00c570f2015-07-01 16:26:08 +02002287 .unlocked_ioctl = fuse_dev_ioctl,
2288 .compat_ioctl = fuse_dev_ioctl,
Miklos Szeredi334f4852005-09-09 13:10:27 -07002289};
Tejun Heo08cbf542009-04-14 10:54:53 +09002290EXPORT_SYMBOL_GPL(fuse_dev_operations);
Miklos Szeredi334f4852005-09-09 13:10:27 -07002291
2292static struct miscdevice fuse_miscdevice = {
2293 .minor = FUSE_MINOR,
2294 .name = "fuse",
2295 .fops = &fuse_dev_operations,
2296};
2297
2298int __init fuse_dev_init(void)
2299{
2300 int err = -ENOMEM;
2301 fuse_req_cachep = kmem_cache_create("fuse_request",
2302 sizeof(struct fuse_req),
Paul Mundt20c2df82007-07-20 10:11:58 +09002303 0, 0, NULL);
Miklos Szeredi334f4852005-09-09 13:10:27 -07002304 if (!fuse_req_cachep)
2305 goto out;
2306
2307 err = misc_register(&fuse_miscdevice);
2308 if (err)
2309 goto out_cache_clean;
2310
2311 return 0;
2312
2313 out_cache_clean:
2314 kmem_cache_destroy(fuse_req_cachep);
2315 out:
2316 return err;
2317}
2318
2319void fuse_dev_cleanup(void)
2320{
2321 misc_deregister(&fuse_miscdevice);
2322 kmem_cache_destroy(fuse_req_cachep);
2323}