blob: 1ff18d319949a8dbf659db09c5b5e8a4adc0b9e0 [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
Constantine Shulyupin5da784c2018-09-06 15:37:06 +030064 WARN_ON(npages > FUSE_MAX_MAX_PAGES);
Miklos Szeredi8a7aa282018-10-01 10:07:05 +020065 if (npages > FUSE_REQ_INLINE_PAGES) {
Miklos Szeredi4c4f03f2019-09-10 15:04:09 +020066 pages = fuse_pages_alloc(npages, flags, &page_descs);
Miklos Szeredi8a7aa282018-10-01 10:07:05 +020067 if (!pages) {
68 kmem_cache_free(fuse_req_cachep, req);
69 return NULL;
70 }
Miklos Szeredi68583162019-09-10 15:04:09 +020071 __set_bit(FR_ALLOC_PAGES, &req->flags);
Miklos Szeredi8a7aa282018-10-01 10:07:05 +020072 } 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
Miklos Szeredi66abc352019-09-10 15:04:11 +020082static struct fuse_req *fuse_request_alloc(unsigned int npages)
Maxim Patlasov4250c062012-10-26 19:48:07 +040083{
84 return __fuse_request_alloc(npages, GFP_KERNEL);
85}
Miklos Szeredi3be5a522008-04-30 00:54:41 -070086
Miklos Szeredie52a82502018-10-01 10:07:06 +020087static void fuse_req_pages_free(struct fuse_req *req)
Miklos Szeredi334f4852005-09-09 13:10:27 -070088{
Miklos Szeredi68583162019-09-10 15:04:09 +020089 if (test_bit(FR_ALLOC_PAGES, &req->flags))
Maxim Patlasov4250c062012-10-26 19:48:07 +040090 kfree(req->pages);
Miklos Szeredie52a82502018-10-01 10:07:06 +020091}
Miklos Szeredi8a7aa282018-10-01 10:07:05 +020092
Miklos Szeredi66abc352019-09-10 15:04:11 +020093static void fuse_request_free(struct fuse_req *req)
Miklos Szeredie52a82502018-10-01 10:07:06 +020094{
95 fuse_req_pages_free(req);
Miklos Szeredi334f4852005-09-09 13:10:27 -070096 kmem_cache_free(fuse_req_cachep, req);
97}
98
Miklos Szeredi66abc352019-09-10 15:04:11 +020099static void __fuse_get_request(struct fuse_req *req)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700100{
Elena Reshetovaec99f6d2017-03-03 11:04:04 +0200101 refcount_inc(&req->count);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700102}
103
104/* Must be called with > 1 refcount */
105static void __fuse_put_request(struct fuse_req *req)
106{
Elena Reshetovaec99f6d2017-03-03 11:04:04 +0200107 refcount_dec(&req->count);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700108}
109
Miklos Szeredi9759bd512015-01-06 10:45:35 +0100110void fuse_set_initialized(struct fuse_conn *fc)
111{
112 /* Make sure stores before this are seen on another CPU */
113 smp_wmb();
114 fc->initialized = 1;
115}
116
Maxim Patlasov0aada882013-03-21 18:02:28 +0400117static bool fuse_block_alloc(struct fuse_conn *fc, bool for_background)
118{
119 return !fc->initialized || (for_background && fc->blocked);
120}
121
Miklos Szeredib8f95e52018-07-26 16:13:11 +0200122static void fuse_drop_waiting(struct fuse_conn *fc)
123{
Miklos Szeredi2d84a2d2018-11-09 15:52:16 +0100124 /*
125 * lockess check of fc->connected is okay, because atomic_dec_and_test()
126 * provides a memory barrier mached with the one in fuse_wait_aborted()
127 * to ensure no wake-up is missed.
128 */
129 if (atomic_dec_and_test(&fc->num_waiting) &&
130 !READ_ONCE(fc->connected)) {
Miklos Szeredib8f95e52018-07-26 16:13:11 +0200131 /* wake up aborters */
132 wake_up_all(&fc->blocked_waitq);
133 }
134}
135
Miklos Szeredi66abc352019-09-10 15:04:11 +0200136static void fuse_put_request(struct fuse_conn *fc, struct fuse_req *req);
137
Maxim Patlasov8b41e672013-03-21 18:02:04 +0400138static struct fuse_req *__fuse_get_req(struct fuse_conn *fc, unsigned npages,
139 bool for_background)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700140{
Miklos Szeredi08a53cd2006-04-10 22:54:59 -0700141 struct fuse_req *req;
Miklos Szeredi08a53cd2006-04-10 22:54:59 -0700142 int err;
Miklos Szeredi9bc5ddd2006-04-11 21:16:09 +0200143 atomic_inc(&fc->num_waiting);
Maxim Patlasov0aada882013-03-21 18:02:28 +0400144
145 if (fuse_block_alloc(fc, for_background)) {
Maxim Patlasov0aada882013-03-21 18:02:28 +0400146 err = -EINTR;
Al Viro7d3a07f2016-07-19 03:08:27 -0400147 if (wait_event_killable_exclusive(fc->blocked_waitq,
148 !fuse_block_alloc(fc, for_background)))
Maxim Patlasov0aada882013-03-21 18:02:28 +0400149 goto out;
150 }
Miklos Szeredi9759bd512015-01-06 10:45:35 +0100151 /* Matches smp_wmb() in fuse_set_initialized() */
152 smp_rmb();
Miklos Szeredi08a53cd2006-04-10 22:54:59 -0700153
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700154 err = -ENOTCONN;
155 if (!fc->connected)
156 goto out;
157
Miklos Szeredide155222015-07-01 16:25:57 +0200158 err = -ECONNREFUSED;
159 if (fc->conn_error)
160 goto out;
161
Maxim Patlasovb111c8c2012-10-26 19:48:30 +0400162 req = fuse_request_alloc(npages);
Miklos Szeredi9bc5ddd2006-04-11 21:16:09 +0200163 err = -ENOMEM;
Maxim Patlasov722d2be2013-03-21 18:02:36 +0400164 if (!req) {
165 if (for_background)
166 wake_up(&fc->blocked_waitq);
Miklos Szeredi9bc5ddd2006-04-11 21:16:09 +0200167 goto out;
Maxim Patlasov722d2be2013-03-21 18:02:36 +0400168 }
Miklos Szeredi334f4852005-09-09 13:10:27 -0700169
Eric W. Biederman8cb08322018-02-21 11:18:07 -0600170 req->in.h.uid = from_kuid(fc->user_ns, current_fsuid());
171 req->in.h.gid = from_kgid(fc->user_ns, current_fsgid());
Eric W. Biedermanc9582eb2018-02-21 10:52:06 -0600172 req->in.h.pid = pid_nr_ns(task_pid(current), fc->pid_ns);
173
Miklos Szeredi825d6d32015-07-01 16:25:58 +0200174 __set_bit(FR_WAITING, &req->flags);
175 if (for_background)
176 __set_bit(FR_BACKGROUND, &req->flags);
177
Eric W. Biedermanc9582eb2018-02-21 10:52:06 -0600178 if (unlikely(req->in.h.uid == ((uid_t)-1) ||
179 req->in.h.gid == ((gid_t)-1))) {
180 fuse_put_request(fc, req);
181 return ERR_PTR(-EOVERFLOW);
182 }
Miklos Szeredi334f4852005-09-09 13:10:27 -0700183 return req;
Miklos Szeredi9bc5ddd2006-04-11 21:16:09 +0200184
185 out:
Miklos Szeredib8f95e52018-07-26 16:13:11 +0200186 fuse_drop_waiting(fc);
Miklos Szeredi9bc5ddd2006-04-11 21:16:09 +0200187 return ERR_PTR(err);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700188}
Maxim Patlasov8b41e672013-03-21 18:02:04 +0400189
Miklos Szeredi66abc352019-09-10 15:04:11 +0200190static struct fuse_req *fuse_get_req(struct fuse_conn *fc, unsigned int npages)
Maxim Patlasov8b41e672013-03-21 18:02:04 +0400191{
192 return __fuse_get_req(fc, npages, false);
193}
Miklos Szeredi334f4852005-09-09 13:10:27 -0700194
Miklos Szeredi66abc352019-09-10 15:04:11 +0200195static void fuse_put_request(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700196{
Elena Reshetovaec99f6d2017-03-03 11:04:04 +0200197 if (refcount_dec_and_test(&req->count)) {
Miklos Szeredi825d6d32015-07-01 16:25:58 +0200198 if (test_bit(FR_BACKGROUND, &req->flags)) {
Maxim Patlasov722d2be2013-03-21 18:02:36 +0400199 /*
200 * We get here in the unlikely case that a background
201 * request was allocated but not sent
202 */
Kirill Tkhaiae2dffa2018-08-27 18:29:46 +0300203 spin_lock(&fc->bg_lock);
Maxim Patlasov722d2be2013-03-21 18:02:36 +0400204 if (!fc->blocked)
205 wake_up(&fc->blocked_waitq);
Kirill Tkhaiae2dffa2018-08-27 18:29:46 +0300206 spin_unlock(&fc->bg_lock);
Maxim Patlasov722d2be2013-03-21 18:02:36 +0400207 }
208
Miklos Szeredi825d6d32015-07-01 16:25:58 +0200209 if (test_bit(FR_WAITING, &req->flags)) {
210 __clear_bit(FR_WAITING, &req->flags);
Miklos Szeredib8f95e52018-07-26 16:13:11 +0200211 fuse_drop_waiting(fc);
Miklos Szeredi73e0e732015-07-01 16:25:56 +0200212 }
Miklos Szeredi33649c92006-06-25 05:48:52 -0700213
Miklos Szeredi40ac7ab2019-09-10 15:04:08 +0200214 fuse_request_free(req);
Miklos Szeredi7128ec22006-02-04 23:27:40 -0800215 }
216}
Tejun Heo08cbf542009-04-14 10:54:53 +0900217EXPORT_SYMBOL_GPL(fuse_put_request);
Miklos Szeredi7128ec22006-02-04 23:27:40 -0800218
Miklos Szeredid12def12008-02-06 01:38:39 -0800219static unsigned len_args(unsigned numargs, struct fuse_arg *args)
220{
221 unsigned nbytes = 0;
222 unsigned i;
223
224 for (i = 0; i < numargs; i++)
225 nbytes += args[i].size;
226
227 return nbytes;
228}
229
Miklos Szeredif88996a2015-07-01 16:26:01 +0200230static u64 fuse_get_unique(struct fuse_iqueue *fiq)
Miklos Szeredid12def12008-02-06 01:38:39 -0800231{
Kirill Tkhaic59fd852018-09-11 13:11:56 +0300232 fiq->reqctr += FUSE_REQ_ID_STEP;
233 return fiq->reqctr;
Miklos Szeredid12def12008-02-06 01:38:39 -0800234}
235
Kirill Tkhaibe2ff422018-09-11 13:12:14 +0300236static unsigned int fuse_req_hash(u64 unique)
237{
238 return hash_long(unique & ~FUSE_INT_REQ_BIT, FUSE_PQ_HASH_BITS);
239}
240
Miklos Szeredif88996a2015-07-01 16:26:01 +0200241static void queue_request(struct fuse_iqueue *fiq, struct fuse_req *req)
Miklos Szeredid12def12008-02-06 01:38:39 -0800242{
Miklos Szeredid12def12008-02-06 01:38:39 -0800243 req->in.h.len = sizeof(struct fuse_in_header) +
244 len_args(req->in.numargs, (struct fuse_arg *) req->in.args);
Miklos Szeredif88996a2015-07-01 16:26:01 +0200245 list_add_tail(&req->list, &fiq->pending);
Eric Biggers76e43c82019-09-08 20:15:18 -0700246 wake_up(&fiq->waitq);
Miklos Szeredif88996a2015-07-01 16:26:01 +0200247 kill_fasync(&fiq->fasync, SIGIO, POLL_IN);
Miklos Szeredid12def12008-02-06 01:38:39 -0800248}
249
Miklos Szeredi07e77dc2010-12-07 20:16:56 +0100250void fuse_queue_forget(struct fuse_conn *fc, struct fuse_forget_link *forget,
251 u64 nodeid, u64 nlookup)
252{
Miklos Szeredif88996a2015-07-01 16:26:01 +0200253 struct fuse_iqueue *fiq = &fc->iq;
254
Miklos Szeredi02c048b2010-12-07 20:16:56 +0100255 forget->forget_one.nodeid = nodeid;
256 forget->forget_one.nlookup = nlookup;
Miklos Szeredi07e77dc2010-12-07 20:16:56 +0100257
Eric Biggers76e43c82019-09-08 20:15:18 -0700258 spin_lock(&fiq->lock);
Miklos Szeredie16714d2015-07-01 16:26:01 +0200259 if (fiq->connected) {
Miklos Szeredif88996a2015-07-01 16:26:01 +0200260 fiq->forget_list_tail->next = forget;
261 fiq->forget_list_tail = forget;
Eric Biggers76e43c82019-09-08 20:15:18 -0700262 wake_up(&fiq->waitq);
Miklos Szeredif88996a2015-07-01 16:26:01 +0200263 kill_fasync(&fiq->fasync, SIGIO, POLL_IN);
Miklos Szeredi5dfcc872011-09-12 09:38:03 +0200264 } else {
265 kfree(forget);
266 }
Eric Biggers76e43c82019-09-08 20:15:18 -0700267 spin_unlock(&fiq->lock);
Miklos Szeredi07e77dc2010-12-07 20:16:56 +0100268}
269
Miklos Szeredid12def12008-02-06 01:38:39 -0800270static void flush_bg_queue(struct fuse_conn *fc)
271{
Kirill Tkhaie2871792018-07-31 13:25:25 +0300272 struct fuse_iqueue *fiq = &fc->iq;
273
Csaba Henk7a6d3c82009-07-01 17:28:41 -0700274 while (fc->active_background < fc->max_background &&
Miklos Szeredid12def12008-02-06 01:38:39 -0800275 !list_empty(&fc->bg_queue)) {
276 struct fuse_req *req;
277
Kirill Tkhaie2871792018-07-31 13:25:25 +0300278 req = list_first_entry(&fc->bg_queue, struct fuse_req, list);
Miklos Szeredid12def12008-02-06 01:38:39 -0800279 list_del(&req->list);
280 fc->active_background++;
Eric Biggers76e43c82019-09-08 20:15:18 -0700281 spin_lock(&fiq->lock);
Miklos Szeredif88996a2015-07-01 16:26:01 +0200282 req->in.h.unique = fuse_get_unique(fiq);
283 queue_request(fiq, req);
Eric Biggers76e43c82019-09-08 20:15:18 -0700284 spin_unlock(&fiq->lock);
Miklos Szeredid12def12008-02-06 01:38:39 -0800285 }
286}
287
Miklos Szeredi6dbbcb12006-04-26 10:49:06 +0200288/*
Miklos Szeredi334f4852005-09-09 13:10:27 -0700289 * This function is called when a request is finished. Either a reply
Miklos Szeredif9a28422006-06-25 05:48:53 -0700290 * has arrived or it was aborted (and not yet sent) or some error
Miklos Szeredif43b1552006-01-16 22:14:26 -0800291 * occurred during communication with userspace, or the device file
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700292 * was closed. The requester thread is woken up (if still waiting),
293 * the 'end' callback is called if given, else the reference to the
294 * request is released
Miklos Szeredi334f4852005-09-09 13:10:27 -0700295 */
296static void request_end(struct fuse_conn *fc, struct fuse_req *req)
297{
Miklos Szeredi4ce60812015-07-01 16:26:02 +0200298 struct fuse_iqueue *fiq = &fc->iq;
Miklos Szeredi365ae712015-07-01 16:26:06 +0200299
Miklos Szerediefe28002015-07-01 16:26:07 +0200300 if (test_and_set_bit(FR_FINISHED, &req->flags))
Miklos Szeredib8f95e52018-07-26 16:13:11 +0200301 goto put_request;
Kirill Tkhai217316a2018-11-08 12:05:25 +0300302 /*
303 * test_and_set_bit() implies smp_mb() between bit
304 * changing and below intr_entry check. Pairs with
305 * smp_mb() from queue_interrupt().
306 */
307 if (!list_empty(&req->intr_entry)) {
Eric Biggers76e43c82019-09-08 20:15:18 -0700308 spin_lock(&fiq->lock);
Kirill Tkhai217316a2018-11-08 12:05:25 +0300309 list_del_init(&req->intr_entry);
Eric Biggers76e43c82019-09-08 20:15:18 -0700310 spin_unlock(&fiq->lock);
Kirill Tkhai217316a2018-11-08 12:05:25 +0300311 }
Miklos Szeredi33e14b42015-07-01 16:26:01 +0200312 WARN_ON(test_bit(FR_PENDING, &req->flags));
313 WARN_ON(test_bit(FR_SENT, &req->flags));
Miklos Szeredi825d6d32015-07-01 16:25:58 +0200314 if (test_bit(FR_BACKGROUND, &req->flags)) {
Kirill Tkhaiae2dffa2018-08-27 18:29:46 +0300315 spin_lock(&fc->bg_lock);
Miklos Szeredi825d6d32015-07-01 16:25:58 +0200316 clear_bit(FR_BACKGROUND, &req->flags);
Miklos Szeredi908a5722018-09-28 16:43:22 +0200317 if (fc->num_background == fc->max_background) {
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700318 fc->blocked = 0;
Maxim Patlasov722d2be2013-03-21 18:02:36 +0400319 wake_up(&fc->blocked_waitq);
Miklos Szeredi908a5722018-09-28 16:43:22 +0200320 } else if (!fc->blocked) {
321 /*
322 * Wake up next waiter, if any. It's okay to use
323 * waitqueue_active(), as we've already synced up
324 * fc->blocked with waiters with the wake_up() call
325 * above.
326 */
327 if (waitqueue_active(&fc->blocked_waitq))
328 wake_up(&fc->blocked_waitq);
329 }
Maxim Patlasov722d2be2013-03-21 18:02:36 +0400330
Tejun Heo8a301eb2018-02-02 09:54:14 -0800331 if (fc->num_background == fc->congestion_threshold && fc->sb) {
Jan Kara5f7f7542017-04-12 12:24:40 +0200332 clear_bdi_congested(fc->sb->s_bdi, BLK_RW_SYNC);
333 clear_bdi_congested(fc->sb->s_bdi, BLK_RW_ASYNC);
Miklos Szeredif92b99b2007-10-16 23:30:59 -0700334 }
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700335 fc->num_background--;
Miklos Szeredid12def12008-02-06 01:38:39 -0800336 fc->active_background--;
337 flush_bg_queue(fc);
Kirill Tkhaiae2dffa2018-08-27 18:29:46 +0300338 spin_unlock(&fc->bg_lock);
Kirill Tkhai5e0fed72018-11-08 12:05:31 +0300339 } else {
340 /* Wake up waiter sleeping in request_wait_answer() */
341 wake_up(&req->waitq);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700342 }
Kirill Tkhai5e0fed72018-11-08 12:05:31 +0300343
Miklos Szeredi1e6881c2015-07-01 16:26:07 +0200344 if (req->end)
345 req->end(fc, req);
Miklos Szeredib8f95e52018-07-26 16:13:11 +0200346put_request:
Tejun Heoe9bb09d2008-11-26 12:03:54 +0100347 fuse_put_request(fc, req);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700348}
349
Kirill Tkhaib7829112018-11-08 12:05:42 +0300350static int queue_interrupt(struct fuse_iqueue *fiq, struct fuse_req *req)
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700351{
Eric Biggers76e43c82019-09-08 20:15:18 -0700352 spin_lock(&fiq->lock);
Kirill Tkhaib7829112018-11-08 12:05:42 +0300353 /* Check for we've sent request to interrupt this req */
354 if (unlikely(!test_bit(FR_INTERRUPTED, &req->flags))) {
Eric Biggers76e43c82019-09-08 20:15:18 -0700355 spin_unlock(&fiq->lock);
Kirill Tkhaib7829112018-11-08 12:05:42 +0300356 return -EINVAL;
357 }
358
Miklos Szeredi8f7bb362015-07-01 16:26:03 +0200359 if (list_empty(&req->intr_entry)) {
360 list_add_tail(&req->intr_entry, &fiq->interrupts);
Kirill Tkhai217316a2018-11-08 12:05:25 +0300361 /*
362 * Pairs with smp_mb() implied by test_and_set_bit()
363 * from request_end().
364 */
365 smp_mb();
366 if (test_bit(FR_FINISHED, &req->flags)) {
367 list_del_init(&req->intr_entry);
Eric Biggers76e43c82019-09-08 20:15:18 -0700368 spin_unlock(&fiq->lock);
Kirill Tkhaib7829112018-11-08 12:05:42 +0300369 return 0;
Kirill Tkhai217316a2018-11-08 12:05:25 +0300370 }
Eric Biggers76e43c82019-09-08 20:15:18 -0700371 wake_up(&fiq->waitq);
Kirill Tkhai8da6e912018-11-08 12:05:20 +0300372 kill_fasync(&fiq->fasync, SIGIO, POLL_IN);
Miklos Szeredi8f7bb362015-07-01 16:26:03 +0200373 }
Eric Biggers76e43c82019-09-08 20:15:18 -0700374 spin_unlock(&fiq->lock);
Kirill Tkhaib7829112018-11-08 12:05:42 +0300375 return 0;
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700376}
377
Miklos Szeredi7c352bd2005-09-09 13:10:39 -0700378static void request_wait_answer(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700379{
Miklos Szeredi4ce60812015-07-01 16:26:02 +0200380 struct fuse_iqueue *fiq = &fc->iq;
Miklos Szeredic4775262015-07-01 16:26:00 +0200381 int err;
382
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700383 if (!fc->no_interrupt) {
384 /* Any signal may interrupt this */
Miklos Szeredic4775262015-07-01 16:26:00 +0200385 err = wait_event_interruptible(req->waitq,
Miklos Szeredi33e14b42015-07-01 16:26:01 +0200386 test_bit(FR_FINISHED, &req->flags));
Miklos Szeredic4775262015-07-01 16:26:00 +0200387 if (!err)
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700388 return;
389
Miklos Szeredi825d6d32015-07-01 16:25:58 +0200390 set_bit(FR_INTERRUPTED, &req->flags);
Miklos Szeredi8f7bb362015-07-01 16:26:03 +0200391 /* matches barrier in fuse_dev_do_read() */
392 smp_mb__after_atomic();
Miklos Szeredi33e14b42015-07-01 16:26:01 +0200393 if (test_bit(FR_SENT, &req->flags))
Miklos Szeredi4ce60812015-07-01 16:26:02 +0200394 queue_interrupt(fiq, req);
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700395 }
396
Miklos Szeredi825d6d32015-07-01 16:25:58 +0200397 if (!test_bit(FR_FORCE, &req->flags)) {
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700398 /* Only fatal signals may interrupt this */
Al Viro7d3a07f2016-07-19 03:08:27 -0400399 err = wait_event_killable(req->waitq,
Miklos Szeredi33e14b42015-07-01 16:26:01 +0200400 test_bit(FR_FINISHED, &req->flags));
Miklos Szeredic4775262015-07-01 16:26:00 +0200401 if (!err)
Miklos Szeredia131de02007-10-16 23:31:04 -0700402 return;
403
Eric Biggers76e43c82019-09-08 20:15:18 -0700404 spin_lock(&fiq->lock);
Miklos Szeredia131de02007-10-16 23:31:04 -0700405 /* Request is not yet in userspace, bail out */
Miklos Szeredi33e14b42015-07-01 16:26:01 +0200406 if (test_bit(FR_PENDING, &req->flags)) {
Miklos Szeredia131de02007-10-16 23:31:04 -0700407 list_del(&req->list);
Eric Biggers76e43c82019-09-08 20:15:18 -0700408 spin_unlock(&fiq->lock);
Miklos Szeredia131de02007-10-16 23:31:04 -0700409 __fuse_put_request(req);
410 req->out.h.error = -EINTR;
411 return;
412 }
Eric Biggers76e43c82019-09-08 20:15:18 -0700413 spin_unlock(&fiq->lock);
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700414 }
Miklos Szeredi334f4852005-09-09 13:10:27 -0700415
Miklos Szeredia131de02007-10-16 23:31:04 -0700416 /*
417 * Either request is already in userspace, or it was forced.
418 * Wait it out.
419 */
Miklos Szeredi33e14b42015-07-01 16:26:01 +0200420 wait_event(req->waitq, test_bit(FR_FINISHED, &req->flags));
Miklos Szeredi334f4852005-09-09 13:10:27 -0700421}
422
Eric Wong6a4e9222013-02-04 13:04:44 +0000423static void __fuse_request_send(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700424{
Miklos Szeredie16714d2015-07-01 16:26:01 +0200425 struct fuse_iqueue *fiq = &fc->iq;
426
Miklos Szeredi825d6d32015-07-01 16:25:58 +0200427 BUG_ON(test_bit(FR_BACKGROUND, &req->flags));
Eric Biggers76e43c82019-09-08 20:15:18 -0700428 spin_lock(&fiq->lock);
Miklos Szeredie16714d2015-07-01 16:26:01 +0200429 if (!fiq->connected) {
Eric Biggers76e43c82019-09-08 20:15:18 -0700430 spin_unlock(&fiq->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700431 req->out.h.error = -ENOTCONN;
Miklos Szeredic4775262015-07-01 16:26:00 +0200432 } else {
Miklos Szeredif88996a2015-07-01 16:26:01 +0200433 req->in.h.unique = fuse_get_unique(fiq);
434 queue_request(fiq, req);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700435 /* acquire extra reference, since request is still needed
436 after request_end() */
437 __fuse_get_request(req);
Eric Biggers76e43c82019-09-08 20:15:18 -0700438 spin_unlock(&fiq->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700439
Miklos Szeredi7c352bd2005-09-09 13:10:39 -0700440 request_wait_answer(fc, req);
Miklos Szeredic4775262015-07-01 16:26:00 +0200441 /* Pairs with smp_wmb() in request_end() */
442 smp_rmb();
Miklos Szeredi334f4852005-09-09 13:10:27 -0700443 }
Miklos Szeredi334f4852005-09-09 13:10:27 -0700444}
Eric Wong6a4e9222013-02-04 13:04:44 +0000445
Miklos Szeredi21f62172015-01-06 10:45:35 +0100446static void fuse_adjust_compat(struct fuse_conn *fc, struct fuse_args *args)
447{
Miklos Szeredid5b48542019-09-10 15:04:08 +0200448 if (fc->minor < 4 && args->opcode == FUSE_STATFS)
449 args->out_args[0].size = FUSE_COMPAT_STATFS_SIZE;
Miklos Szeredi21f62172015-01-06 10:45:35 +0100450
451 if (fc->minor < 9) {
Miklos Szeredid5b48542019-09-10 15:04:08 +0200452 switch (args->opcode) {
Miklos Szeredi21f62172015-01-06 10:45:35 +0100453 case FUSE_LOOKUP:
454 case FUSE_CREATE:
455 case FUSE_MKNOD:
456 case FUSE_MKDIR:
457 case FUSE_SYMLINK:
458 case FUSE_LINK:
Miklos Szeredid5b48542019-09-10 15:04:08 +0200459 args->out_args[0].size = FUSE_COMPAT_ENTRY_OUT_SIZE;
Miklos Szeredi21f62172015-01-06 10:45:35 +0100460 break;
461 case FUSE_GETATTR:
462 case FUSE_SETATTR:
Miklos Szeredid5b48542019-09-10 15:04:08 +0200463 args->out_args[0].size = FUSE_COMPAT_ATTR_OUT_SIZE;
Miklos Szeredi21f62172015-01-06 10:45:35 +0100464 break;
465 }
466 }
467 if (fc->minor < 12) {
Miklos Szeredid5b48542019-09-10 15:04:08 +0200468 switch (args->opcode) {
Miklos Szeredi21f62172015-01-06 10:45:35 +0100469 case FUSE_CREATE:
Miklos Szeredid5b48542019-09-10 15:04:08 +0200470 args->in_args[0].size = sizeof(struct fuse_open_in);
Miklos Szeredi21f62172015-01-06 10:45:35 +0100471 break;
472 case FUSE_MKNOD:
Miklos Szeredid5b48542019-09-10 15:04:08 +0200473 args->in_args[0].size = FUSE_COMPAT_MKNOD_IN_SIZE;
Miklos Szeredi21f62172015-01-06 10:45:35 +0100474 break;
475 }
476 }
477}
478
Miklos Szeredie4137542019-09-10 15:04:08 +0200479static void fuse_force_creds(struct fuse_conn *fc, struct fuse_req *req)
480{
481 req->in.h.uid = from_kuid_munged(fc->user_ns, current_fsuid());
482 req->in.h.gid = from_kgid_munged(fc->user_ns, current_fsgid());
483 req->in.h.pid = pid_nr_ns(task_pid(current), fc->pid_ns);
484}
485
Miklos Szeredi68583162019-09-10 15:04:09 +0200486void fuse_args_to_req(struct fuse_req *req, struct fuse_args *args)
487{
488 struct fuse_args_pages *ap = container_of(args, typeof(*ap), args);
489
490 req->in.h.opcode = args->opcode;
491 req->in.h.nodeid = args->nodeid;
492 req->in.numargs = args->in_numargs;
493 memcpy(req->in.args, args->in_args,
494 args->in_numargs * sizeof(struct fuse_in_arg));
495 req->out.argvar = args->out_argvar;
496 req->out.numargs = args->out_numargs;
497 memcpy(req->out.args, args->out_args,
498 args->out_numargs * sizeof(struct fuse_arg));
499
500 if (args->in_pages || args->out_pages) {
501 req->in.argpages = args->in_pages;
502 req->out.argpages = args->out_pages;
503 req->out.page_zeroing = args->page_zeroing;
504 req->out.page_replace = args->page_replace;
505
506 req->pages = ap->pages;
507 req->page_descs = ap->descs;
508 req->num_pages = ap->num_pages;
509 }
510
511}
512
Miklos Szeredi70781872014-12-12 09:49:05 +0100513ssize_t fuse_simple_request(struct fuse_conn *fc, struct fuse_args *args)
514{
515 struct fuse_req *req;
516 ssize_t ret;
517
Miklos Szeredic500eba2019-09-10 15:04:08 +0200518 if (args->force) {
Miklos Szeredie4137542019-09-10 15:04:08 +0200519 atomic_inc(&fc->num_waiting);
520 req = __fuse_request_alloc(0, GFP_KERNEL | __GFP_NOFAIL);
521
522 if (!args->nocreds)
523 fuse_force_creds(fc, req);
524
525 __set_bit(FR_WAITING, &req->flags);
Miklos Szeredic500eba2019-09-10 15:04:08 +0200526 __set_bit(FR_FORCE, &req->flags);
527 } else {
Miklos Szeredie4137542019-09-10 15:04:08 +0200528 WARN_ON(args->nocreds);
Miklos Szeredic500eba2019-09-10 15:04:08 +0200529 req = fuse_get_req(fc, 0);
530 if (IS_ERR(req))
531 return PTR_ERR(req);
532 }
Miklos Szeredi70781872014-12-12 09:49:05 +0100533
Miklos Szeredi21f62172015-01-06 10:45:35 +0100534 /* Needs to be done after fuse_get_req() so that fc->minor is valid */
535 fuse_adjust_compat(fc, args);
Miklos Szeredi68583162019-09-10 15:04:09 +0200536 fuse_args_to_req(req, args);
Miklos Szeredi21f62172015-01-06 10:45:35 +0100537
Miklos Szeredi454a7612019-09-10 15:04:08 +0200538 if (!args->noreply)
539 __set_bit(FR_ISREPLY, &req->flags);
540 __fuse_request_send(fc, req);
Miklos Szeredi70781872014-12-12 09:49:05 +0100541 ret = req->out.h.error;
Miklos Szeredid5b48542019-09-10 15:04:08 +0200542 if (!ret && args->out_argvar) {
Miklos Szeredi093f38a2019-09-10 15:04:09 +0200543 BUG_ON(args->out_numargs == 0);
544 ret = req->out.args[args->out_numargs - 1].size;
Miklos Szeredi70781872014-12-12 09:49:05 +0100545 }
546 fuse_put_request(fc, req);
547
548 return ret;
549}
550
Miklos Szeredi66abc352019-09-10 15:04:11 +0200551static bool fuse_request_queue_background(struct fuse_conn *fc,
552 struct fuse_req *req)
Miklos Szeredid12def12008-02-06 01:38:39 -0800553{
Kirill Tkhai63825b42018-08-27 18:29:56 +0300554 bool queued = false;
555
556 WARN_ON(!test_bit(FR_BACKGROUND, &req->flags));
Miklos Szeredi825d6d32015-07-01 16:25:58 +0200557 if (!test_bit(FR_WAITING, &req->flags)) {
558 __set_bit(FR_WAITING, &req->flags);
Miklos Szeredi5437f242015-07-01 16:25:56 +0200559 atomic_inc(&fc->num_waiting);
560 }
Miklos Szeredi825d6d32015-07-01 16:25:58 +0200561 __set_bit(FR_ISREPLY, &req->flags);
Kirill Tkhaiae2dffa2018-08-27 18:29:46 +0300562 spin_lock(&fc->bg_lock);
Kirill Tkhai63825b42018-08-27 18:29:56 +0300563 if (likely(fc->connected)) {
564 fc->num_background++;
565 if (fc->num_background == fc->max_background)
566 fc->blocked = 1;
567 if (fc->num_background == fc->congestion_threshold && fc->sb) {
568 set_bdi_congested(fc->sb->s_bdi, BLK_RW_SYNC);
569 set_bdi_congested(fc->sb->s_bdi, BLK_RW_ASYNC);
570 }
571 list_add_tail(&req->list, &fc->bg_queue);
572 flush_bg_queue(fc);
573 queued = true;
Miklos Szeredid12def12008-02-06 01:38:39 -0800574 }
Kirill Tkhaiae2dffa2018-08-27 18:29:46 +0300575 spin_unlock(&fc->bg_lock);
Kirill Tkhai63825b42018-08-27 18:29:56 +0300576
577 return queued;
Miklos Szeredid12def12008-02-06 01:38:39 -0800578}
579
Miklos Szeredi12597282019-09-10 15:04:10 +0200580static void fuse_simple_end(struct fuse_conn *fc, struct fuse_req *req)
581{
582 struct fuse_args *args = req->args;
583 int err = req->out.h.error;
584
585 if (!err && args->out_argvar) {
586 BUG_ON(args->out_numargs == 0);
587 args->out_args[args->out_numargs - 1].size =
588 req->out.args[args->out_numargs - 1].size;
589 }
590 req->args->end(fc, req->args, req->out.h.error);
591}
592
593int fuse_simple_background(struct fuse_conn *fc, struct fuse_args *args,
594 gfp_t gfp_flags)
595{
596 struct fuse_req *req;
597
598 if (args->force) {
599 WARN_ON(!args->nocreds);
600 req = __fuse_request_alloc(0, gfp_flags);
601 if (!req)
602 return -ENOMEM;
603 __set_bit(FR_BACKGROUND, &req->flags);
604 } else {
605 WARN_ON(args->nocreds);
606 req = __fuse_get_req(fc, 0, true);
607 if (IS_ERR(req))
608 return PTR_ERR(req);
609 }
610
611 fuse_args_to_req(req, args);
612
613 req->args = args;
614 req->end = fuse_simple_end;
615
616 if (!fuse_request_queue_background(fc, req)) {
617 fuse_put_request(fc, req);
618 return -ENOTCONN;
619 }
620
621 return 0;
622}
623EXPORT_SYMBOL_GPL(fuse_simple_background);
624
Miklos Szeredi75b399d2019-09-10 15:04:11 +0200625static int fuse_simple_notify_reply(struct fuse_conn *fc,
626 struct fuse_args *args, u64 unique)
Miklos Szeredi2d45ba32010-07-12 14:41:40 +0200627{
Miklos Szeredi75b399d2019-09-10 15:04:11 +0200628 struct fuse_req *req;
Miklos Szeredif88996a2015-07-01 16:26:01 +0200629 struct fuse_iqueue *fiq = &fc->iq;
Miklos Szeredi75b399d2019-09-10 15:04:11 +0200630 int err = 0;
631
632 req = fuse_get_req(fc, 0);
633 if (IS_ERR(req))
634 return PTR_ERR(req);
Miklos Szeredi2d45ba32010-07-12 14:41:40 +0200635
Miklos Szeredi825d6d32015-07-01 16:25:58 +0200636 __clear_bit(FR_ISREPLY, &req->flags);
Miklos Szeredi2d45ba32010-07-12 14:41:40 +0200637 req->in.h.unique = unique;
Miklos Szeredi75b399d2019-09-10 15:04:11 +0200638
639 fuse_args_to_req(req, args);
640 req->args = args;
641 req->end = fuse_simple_end;
642
Eric Biggers76e43c82019-09-08 20:15:18 -0700643 spin_lock(&fiq->lock);
Miklos Szeredie16714d2015-07-01 16:26:01 +0200644 if (fiq->connected) {
Miklos Szeredif88996a2015-07-01 16:26:01 +0200645 queue_request(fiq, req);
Miklos Szeredi75b399d2019-09-10 15:04:11 +0200646 spin_unlock(&fiq->lock);
647 } else {
648 err = -ENODEV;
649 spin_unlock(&fiq->lock);
650 fuse_put_request(fc, req);
Miklos Szeredi2d45ba32010-07-12 14:41:40 +0200651 }
Eric Biggers76e43c82019-09-08 20:15:18 -0700652 spin_unlock(&fiq->lock);
Miklos Szeredi2d45ba32010-07-12 14:41:40 +0200653
654 return err;
655}
656
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700657/*
Miklos Szeredi334f4852005-09-09 13:10:27 -0700658 * Lock the request. Up to the next unlock_request() there mustn't be
659 * anything that could cause a page-fault. If the request was already
Miklos Szeredif9a28422006-06-25 05:48:53 -0700660 * aborted bail out.
Miklos Szeredi334f4852005-09-09 13:10:27 -0700661 */
Miklos Szeredidc008092015-07-01 16:25:58 +0200662static int lock_request(struct fuse_req *req)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700663{
664 int err = 0;
665 if (req) {
Miklos Szeredidc008092015-07-01 16:25:58 +0200666 spin_lock(&req->waitq.lock);
Miklos Szeredi825d6d32015-07-01 16:25:58 +0200667 if (test_bit(FR_ABORTED, &req->flags))
Miklos Szeredi334f4852005-09-09 13:10:27 -0700668 err = -ENOENT;
669 else
Miklos Szeredi825d6d32015-07-01 16:25:58 +0200670 set_bit(FR_LOCKED, &req->flags);
Miklos Szeredidc008092015-07-01 16:25:58 +0200671 spin_unlock(&req->waitq.lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700672 }
673 return err;
674}
675
676/*
Miklos Szeredi0d8e84b2015-07-01 16:25:58 +0200677 * Unlock request. If it was aborted while locked, caller is responsible
678 * for unlocking and ending the request.
Miklos Szeredi334f4852005-09-09 13:10:27 -0700679 */
Miklos Szeredidc008092015-07-01 16:25:58 +0200680static int unlock_request(struct fuse_req *req)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700681{
Miklos Szeredi0d8e84b2015-07-01 16:25:58 +0200682 int err = 0;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700683 if (req) {
Miklos Szeredidc008092015-07-01 16:25:58 +0200684 spin_lock(&req->waitq.lock);
Miklos Szeredi825d6d32015-07-01 16:25:58 +0200685 if (test_bit(FR_ABORTED, &req->flags))
Miklos Szeredi0d8e84b2015-07-01 16:25:58 +0200686 err = -ENOENT;
687 else
Miklos Szeredi825d6d32015-07-01 16:25:58 +0200688 clear_bit(FR_LOCKED, &req->flags);
Miklos Szeredidc008092015-07-01 16:25:58 +0200689 spin_unlock(&req->waitq.lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700690 }
Miklos Szeredi0d8e84b2015-07-01 16:25:58 +0200691 return err;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700692}
693
694struct fuse_copy_state {
695 int write;
696 struct fuse_req *req;
Al Viro6c09e942015-04-03 22:06:08 -0400697 struct iov_iter *iter;
Miklos Szeredidd3bb142010-05-25 15:06:06 +0200698 struct pipe_buffer *pipebufs;
699 struct pipe_buffer *currbuf;
700 struct pipe_inode_info *pipe;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700701 unsigned long nr_segs;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700702 struct page *pg;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700703 unsigned len;
Miklos Szeredic55a01d2014-07-07 15:28:51 +0200704 unsigned offset;
Miklos Szeredice534fb2010-05-25 15:06:07 +0200705 unsigned move_pages:1;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700706};
707
Miklos Szeredidc008092015-07-01 16:25:58 +0200708static void fuse_copy_init(struct fuse_copy_state *cs, int write,
Al Viro6c09e942015-04-03 22:06:08 -0400709 struct iov_iter *iter)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700710{
711 memset(cs, 0, sizeof(*cs));
712 cs->write = write;
Al Viro6c09e942015-04-03 22:06:08 -0400713 cs->iter = iter;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700714}
715
716/* Unmap and put previous page of userspace buffer */
Miklos Szeredi8bfc0162006-01-16 22:14:28 -0800717static void fuse_copy_finish(struct fuse_copy_state *cs)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700718{
Miklos Szeredidd3bb142010-05-25 15:06:06 +0200719 if (cs->currbuf) {
720 struct pipe_buffer *buf = cs->currbuf;
721
Miklos Szeredic55a01d2014-07-07 15:28:51 +0200722 if (cs->write)
Miklos Szeredic3021622010-05-25 15:06:07 +0200723 buf->len = PAGE_SIZE - cs->len;
Miklos Szeredidd3bb142010-05-25 15:06:06 +0200724 cs->currbuf = NULL;
Miklos Szeredic55a01d2014-07-07 15:28:51 +0200725 } else if (cs->pg) {
Miklos Szeredi334f4852005-09-09 13:10:27 -0700726 if (cs->write) {
727 flush_dcache_page(cs->pg);
728 set_page_dirty_lock(cs->pg);
729 }
730 put_page(cs->pg);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700731 }
Miklos Szeredic55a01d2014-07-07 15:28:51 +0200732 cs->pg = NULL;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700733}
734
735/*
736 * Get another pagefull of userspace buffer, and map it to kernel
737 * address space, and lock request
738 */
739static int fuse_copy_fill(struct fuse_copy_state *cs)
740{
Miklos Szeredic55a01d2014-07-07 15:28:51 +0200741 struct page *page;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700742 int err;
743
Miklos Szeredidc008092015-07-01 16:25:58 +0200744 err = unlock_request(cs->req);
Miklos Szeredi0d8e84b2015-07-01 16:25:58 +0200745 if (err)
746 return err;
747
Miklos Szeredi334f4852005-09-09 13:10:27 -0700748 fuse_copy_finish(cs);
Miklos Szeredidd3bb142010-05-25 15:06:06 +0200749 if (cs->pipebufs) {
750 struct pipe_buffer *buf = cs->pipebufs;
751
Miklos Szeredic3021622010-05-25 15:06:07 +0200752 if (!cs->write) {
Miklos Szeredifba597d2016-09-27 10:45:12 +0200753 err = pipe_buf_confirm(cs->pipe, buf);
Miklos Szeredic3021622010-05-25 15:06:07 +0200754 if (err)
755 return err;
Miklos Szeredidd3bb142010-05-25 15:06:06 +0200756
Miklos Szeredic3021622010-05-25 15:06:07 +0200757 BUG_ON(!cs->nr_segs);
758 cs->currbuf = buf;
Miklos Szeredic55a01d2014-07-07 15:28:51 +0200759 cs->pg = buf->page;
760 cs->offset = buf->offset;
Miklos Szeredic3021622010-05-25 15:06:07 +0200761 cs->len = buf->len;
Miklos Szeredic3021622010-05-25 15:06:07 +0200762 cs->pipebufs++;
763 cs->nr_segs--;
764 } else {
Miklos Szeredic3021622010-05-25 15:06:07 +0200765 if (cs->nr_segs == cs->pipe->buffers)
766 return -EIO;
767
768 page = alloc_page(GFP_HIGHUSER);
769 if (!page)
770 return -ENOMEM;
771
772 buf->page = page;
773 buf->offset = 0;
774 buf->len = 0;
775
776 cs->currbuf = buf;
Miklos Szeredic55a01d2014-07-07 15:28:51 +0200777 cs->pg = page;
778 cs->offset = 0;
Miklos Szeredic3021622010-05-25 15:06:07 +0200779 cs->len = PAGE_SIZE;
780 cs->pipebufs++;
781 cs->nr_segs++;
782 }
Miklos Szeredidd3bb142010-05-25 15:06:06 +0200783 } else {
Al Viro6c09e942015-04-03 22:06:08 -0400784 size_t off;
785 err = iov_iter_get_pages(cs->iter, &page, PAGE_SIZE, 1, &off);
Miklos Szeredidd3bb142010-05-25 15:06:06 +0200786 if (err < 0)
787 return err;
Al Viro6c09e942015-04-03 22:06:08 -0400788 BUG_ON(!err);
789 cs->len = err;
790 cs->offset = off;
Miklos Szeredic55a01d2014-07-07 15:28:51 +0200791 cs->pg = page;
Al Viro6c09e942015-04-03 22:06:08 -0400792 iov_iter_advance(cs->iter, err);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700793 }
Miklos Szeredi334f4852005-09-09 13:10:27 -0700794
Miklos Szeredidc008092015-07-01 16:25:58 +0200795 return lock_request(cs->req);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700796}
797
798/* Do as much copy to/from userspace buffer as we can */
Miklos Szeredi8bfc0162006-01-16 22:14:28 -0800799static int fuse_copy_do(struct fuse_copy_state *cs, void **val, unsigned *size)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700800{
801 unsigned ncpy = min(*size, cs->len);
802 if (val) {
Miklos Szeredic55a01d2014-07-07 15:28:51 +0200803 void *pgaddr = kmap_atomic(cs->pg);
804 void *buf = pgaddr + cs->offset;
805
Miklos Szeredi334f4852005-09-09 13:10:27 -0700806 if (cs->write)
Miklos Szeredic55a01d2014-07-07 15:28:51 +0200807 memcpy(buf, *val, ncpy);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700808 else
Miklos Szeredic55a01d2014-07-07 15:28:51 +0200809 memcpy(*val, buf, ncpy);
810
811 kunmap_atomic(pgaddr);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700812 *val += ncpy;
813 }
814 *size -= ncpy;
815 cs->len -= ncpy;
Miklos Szeredic55a01d2014-07-07 15:28:51 +0200816 cs->offset += ncpy;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700817 return ncpy;
818}
819
Miklos Szeredice534fb2010-05-25 15:06:07 +0200820static int fuse_check_page(struct page *page)
821{
822 if (page_mapcount(page) ||
823 page->mapping != NULL ||
824 page_count(page) != 1 ||
825 (page->flags & PAGE_FLAGS_CHECK_AT_PREP &
826 ~(1 << PG_locked |
827 1 << PG_referenced |
828 1 << PG_uptodate |
829 1 << PG_lru |
830 1 << PG_active |
831 1 << PG_reclaim))) {
Kirill Smelkovf2294482019-03-27 09:15:17 +0000832 pr_warn("trying to steal weird page\n");
833 pr_warn(" 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);
Miklos Szeredice534fb2010-05-25 15:06:07 +0200834 return 1;
835 }
836 return 0;
837}
838
839static int fuse_try_move_page(struct fuse_copy_state *cs, struct page **pagep)
840{
841 int err;
842 struct page *oldpage = *pagep;
843 struct page *newpage;
844 struct pipe_buffer *buf = cs->pipebufs;
Miklos Szeredice534fb2010-05-25 15:06:07 +0200845
Miklos Szeredidc008092015-07-01 16:25:58 +0200846 err = unlock_request(cs->req);
Miklos Szeredi0d8e84b2015-07-01 16:25:58 +0200847 if (err)
848 return err;
849
Miklos Szeredice534fb2010-05-25 15:06:07 +0200850 fuse_copy_finish(cs);
851
Miklos Szeredifba597d2016-09-27 10:45:12 +0200852 err = pipe_buf_confirm(cs->pipe, buf);
Miklos Szeredice534fb2010-05-25 15:06:07 +0200853 if (err)
854 return err;
855
856 BUG_ON(!cs->nr_segs);
857 cs->currbuf = buf;
858 cs->len = buf->len;
859 cs->pipebufs++;
860 cs->nr_segs--;
861
862 if (cs->len != PAGE_SIZE)
863 goto out_fallback;
864
Miklos Szeredica76f5b2016-09-27 10:45:12 +0200865 if (pipe_buf_steal(cs->pipe, buf) != 0)
Miklos Szeredice534fb2010-05-25 15:06:07 +0200866 goto out_fallback;
867
868 newpage = buf->page;
869
Miklos Szerediaa991b32015-02-26 11:45:47 +0100870 if (!PageUptodate(newpage))
871 SetPageUptodate(newpage);
Miklos Szeredice534fb2010-05-25 15:06:07 +0200872
873 ClearPageMappedToDisk(newpage);
874
875 if (fuse_check_page(newpage) != 0)
876 goto out_fallback_unlock;
877
Miklos Szeredice534fb2010-05-25 15:06:07 +0200878 /*
879 * This is a new and locked page, it shouldn't be mapped or
880 * have any special flags on it
881 */
882 if (WARN_ON(page_mapped(oldpage)))
883 goto out_fallback_unlock;
884 if (WARN_ON(page_has_private(oldpage)))
885 goto out_fallback_unlock;
886 if (WARN_ON(PageDirty(oldpage) || PageWriteback(oldpage)))
887 goto out_fallback_unlock;
888 if (WARN_ON(PageMlocked(oldpage)))
889 goto out_fallback_unlock;
890
Miklos Szeredief6a3c62011-03-22 16:30:52 -0700891 err = replace_page_cache_page(oldpage, newpage, GFP_KERNEL);
Miklos Szeredice534fb2010-05-25 15:06:07 +0200892 if (err) {
Miklos Szeredief6a3c62011-03-22 16:30:52 -0700893 unlock_page(newpage);
894 return err;
Miklos Szeredice534fb2010-05-25 15:06:07 +0200895 }
Miklos Szeredief6a3c62011-03-22 16:30:52 -0700896
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300897 get_page(newpage);
Miklos Szeredice534fb2010-05-25 15:06:07 +0200898
899 if (!(buf->flags & PIPE_BUF_FLAG_LRU))
900 lru_cache_add_file(newpage);
901
902 err = 0;
Miklos Szeredidc008092015-07-01 16:25:58 +0200903 spin_lock(&cs->req->waitq.lock);
Miklos Szeredi825d6d32015-07-01 16:25:58 +0200904 if (test_bit(FR_ABORTED, &cs->req->flags))
Miklos Szeredice534fb2010-05-25 15:06:07 +0200905 err = -ENOENT;
906 else
907 *pagep = newpage;
Miklos Szeredidc008092015-07-01 16:25:58 +0200908 spin_unlock(&cs->req->waitq.lock);
Miklos Szeredice534fb2010-05-25 15:06:07 +0200909
910 if (err) {
911 unlock_page(newpage);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300912 put_page(newpage);
Miklos Szeredice534fb2010-05-25 15:06:07 +0200913 return err;
914 }
915
916 unlock_page(oldpage);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300917 put_page(oldpage);
Miklos Szeredice534fb2010-05-25 15:06:07 +0200918 cs->len = 0;
919
920 return 0;
921
922out_fallback_unlock:
923 unlock_page(newpage);
924out_fallback:
Miklos Szeredic55a01d2014-07-07 15:28:51 +0200925 cs->pg = buf->page;
926 cs->offset = buf->offset;
Miklos Szeredice534fb2010-05-25 15:06:07 +0200927
Miklos Szeredidc008092015-07-01 16:25:58 +0200928 err = lock_request(cs->req);
Miklos Szeredice534fb2010-05-25 15:06:07 +0200929 if (err)
930 return err;
931
932 return 1;
933}
934
Miklos Szeredic3021622010-05-25 15:06:07 +0200935static int fuse_ref_page(struct fuse_copy_state *cs, struct page *page,
936 unsigned offset, unsigned count)
937{
938 struct pipe_buffer *buf;
Miklos Szeredi0d8e84b2015-07-01 16:25:58 +0200939 int err;
Miklos Szeredic3021622010-05-25 15:06:07 +0200940
941 if (cs->nr_segs == cs->pipe->buffers)
942 return -EIO;
943
Miklos Szeredidc008092015-07-01 16:25:58 +0200944 err = unlock_request(cs->req);
Miklos Szeredi0d8e84b2015-07-01 16:25:58 +0200945 if (err)
946 return err;
947
Miklos Szeredic3021622010-05-25 15:06:07 +0200948 fuse_copy_finish(cs);
949
950 buf = cs->pipebufs;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300951 get_page(page);
Miklos Szeredic3021622010-05-25 15:06:07 +0200952 buf->page = page;
953 buf->offset = offset;
954 buf->len = count;
955
956 cs->pipebufs++;
957 cs->nr_segs++;
958 cs->len = 0;
959
960 return 0;
961}
962
Miklos Szeredi334f4852005-09-09 13:10:27 -0700963/*
964 * Copy a page in the request to/from the userspace buffer. Must be
965 * done atomically
966 */
Miklos Szeredice534fb2010-05-25 15:06:07 +0200967static int fuse_copy_page(struct fuse_copy_state *cs, struct page **pagep,
Miklos Szeredi8bfc0162006-01-16 22:14:28 -0800968 unsigned offset, unsigned count, int zeroing)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700969{
Miklos Szeredice534fb2010-05-25 15:06:07 +0200970 int err;
971 struct page *page = *pagep;
972
Miklos Szeredib6777c42010-10-26 14:22:27 -0700973 if (page && zeroing && count < PAGE_SIZE)
974 clear_highpage(page);
975
Miklos Szeredi334f4852005-09-09 13:10:27 -0700976 while (count) {
Miklos Szeredic3021622010-05-25 15:06:07 +0200977 if (cs->write && cs->pipebufs && page) {
978 return fuse_ref_page(cs, page, offset, count);
979 } else if (!cs->len) {
Miklos Szeredice534fb2010-05-25 15:06:07 +0200980 if (cs->move_pages && page &&
981 offset == 0 && count == PAGE_SIZE) {
982 err = fuse_try_move_page(cs, pagep);
983 if (err <= 0)
984 return err;
985 } else {
986 err = fuse_copy_fill(cs);
987 if (err)
988 return err;
989 }
Miklos Szeredi1729a162008-11-26 12:03:54 +0100990 }
Miklos Szeredi334f4852005-09-09 13:10:27 -0700991 if (page) {
Cong Wang2408f6e2011-11-25 23:14:30 +0800992 void *mapaddr = kmap_atomic(page);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700993 void *buf = mapaddr + offset;
994 offset += fuse_copy_do(cs, &buf, &count);
Cong Wang2408f6e2011-11-25 23:14:30 +0800995 kunmap_atomic(mapaddr);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700996 } else
997 offset += fuse_copy_do(cs, NULL, &count);
998 }
999 if (page && !cs->write)
1000 flush_dcache_page(page);
1001 return 0;
1002}
1003
1004/* Copy pages in the request to/from userspace buffer */
1005static int fuse_copy_pages(struct fuse_copy_state *cs, unsigned nbytes,
1006 int zeroing)
1007{
1008 unsigned i;
1009 struct fuse_req *req = cs->req;
Miklos Szeredi334f4852005-09-09 13:10:27 -07001010
1011 for (i = 0; i < req->num_pages && (nbytes || zeroing); i++) {
Miklos Szeredice534fb2010-05-25 15:06:07 +02001012 int err;
Maxim Patlasov85f40ae2012-10-26 19:49:33 +04001013 unsigned offset = req->page_descs[i].offset;
1014 unsigned count = min(nbytes, req->page_descs[i].length);
Miklos Szeredice534fb2010-05-25 15:06:07 +02001015
1016 err = fuse_copy_page(cs, &req->pages[i], offset, count,
1017 zeroing);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001018 if (err)
1019 return err;
1020
1021 nbytes -= count;
Miklos Szeredi334f4852005-09-09 13:10:27 -07001022 }
1023 return 0;
1024}
1025
1026/* Copy a single argument in the request to/from userspace buffer */
1027static int fuse_copy_one(struct fuse_copy_state *cs, void *val, unsigned size)
1028{
1029 while (size) {
Miklos Szeredi1729a162008-11-26 12:03:54 +01001030 if (!cs->len) {
1031 int err = fuse_copy_fill(cs);
1032 if (err)
1033 return err;
1034 }
Miklos Szeredi334f4852005-09-09 13:10:27 -07001035 fuse_copy_do(cs, &val, &size);
1036 }
1037 return 0;
1038}
1039
1040/* Copy request arguments to/from userspace buffer */
1041static int fuse_copy_args(struct fuse_copy_state *cs, unsigned numargs,
1042 unsigned argpages, struct fuse_arg *args,
1043 int zeroing)
1044{
1045 int err = 0;
1046 unsigned i;
1047
1048 for (i = 0; !err && i < numargs; i++) {
1049 struct fuse_arg *arg = &args[i];
1050 if (i == numargs - 1 && argpages)
1051 err = fuse_copy_pages(cs, arg->size, zeroing);
1052 else
1053 err = fuse_copy_one(cs, arg->value, arg->size);
1054 }
1055 return err;
1056}
1057
Miklos Szeredif88996a2015-07-01 16:26:01 +02001058static int forget_pending(struct fuse_iqueue *fiq)
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001059{
Miklos Szeredif88996a2015-07-01 16:26:01 +02001060 return fiq->forget_list_head.next != NULL;
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001061}
1062
Miklos Szeredif88996a2015-07-01 16:26:01 +02001063static int request_pending(struct fuse_iqueue *fiq)
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001064{
Miklos Szeredif88996a2015-07-01 16:26:01 +02001065 return !list_empty(&fiq->pending) || !list_empty(&fiq->interrupts) ||
1066 forget_pending(fiq);
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001067}
1068
Miklos Szeredi334f4852005-09-09 13:10:27 -07001069/*
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001070 * Transfer an interrupt request to userspace
1071 *
1072 * Unlike other requests this is assembled on demand, without a need
1073 * to allocate a separate fuse_req structure.
1074 *
Eric Biggers76e43c82019-09-08 20:15:18 -07001075 * Called with fiq->lock held, releases it
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001076 */
Miklos Szeredifd22d622015-07-01 16:26:03 +02001077static int fuse_read_interrupt(struct fuse_iqueue *fiq,
1078 struct fuse_copy_state *cs,
Miklos Szeredic3021622010-05-25 15:06:07 +02001079 size_t nbytes, struct fuse_req *req)
Eric Biggers76e43c82019-09-08 20:15:18 -07001080__releases(fiq->lock)
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001081{
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001082 struct fuse_in_header ih;
1083 struct fuse_interrupt_in arg;
1084 unsigned reqsize = sizeof(ih) + sizeof(arg);
1085 int err;
1086
1087 list_del_init(&req->intr_entry);
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001088 memset(&ih, 0, sizeof(ih));
1089 memset(&arg, 0, sizeof(arg));
1090 ih.len = reqsize;
1091 ih.opcode = FUSE_INTERRUPT;
Kirill Tkhai3a5358d2018-09-11 13:12:05 +03001092 ih.unique = (req->in.h.unique | FUSE_INT_REQ_BIT);
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001093 arg.unique = req->in.h.unique;
1094
Eric Biggers76e43c82019-09-08 20:15:18 -07001095 spin_unlock(&fiq->lock);
Miklos Szeredic3021622010-05-25 15:06:07 +02001096 if (nbytes < reqsize)
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001097 return -EINVAL;
1098
Miklos Szeredic3021622010-05-25 15:06:07 +02001099 err = fuse_copy_one(cs, &ih, sizeof(ih));
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001100 if (!err)
Miklos Szeredic3021622010-05-25 15:06:07 +02001101 err = fuse_copy_one(cs, &arg, sizeof(arg));
1102 fuse_copy_finish(cs);
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001103
1104 return err ? err : reqsize;
1105}
1106
Miklos Szeredif88996a2015-07-01 16:26:01 +02001107static struct fuse_forget_link *dequeue_forget(struct fuse_iqueue *fiq,
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001108 unsigned max,
1109 unsigned *countp)
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001110{
Miklos Szeredif88996a2015-07-01 16:26:01 +02001111 struct fuse_forget_link *head = fiq->forget_list_head.next;
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001112 struct fuse_forget_link **newhead = &head;
1113 unsigned count;
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001114
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001115 for (count = 0; *newhead != NULL && count < max; count++)
1116 newhead = &(*newhead)->next;
1117
Miklos Szeredif88996a2015-07-01 16:26:01 +02001118 fiq->forget_list_head.next = *newhead;
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001119 *newhead = NULL;
Miklos Szeredif88996a2015-07-01 16:26:01 +02001120 if (fiq->forget_list_head.next == NULL)
1121 fiq->forget_list_tail = &fiq->forget_list_head;
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001122
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001123 if (countp != NULL)
1124 *countp = count;
1125
1126 return head;
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001127}
1128
Miklos Szeredifd22d622015-07-01 16:26:03 +02001129static int fuse_read_single_forget(struct fuse_iqueue *fiq,
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001130 struct fuse_copy_state *cs,
1131 size_t nbytes)
Eric Biggers76e43c82019-09-08 20:15:18 -07001132__releases(fiq->lock)
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001133{
1134 int err;
Miklos Szeredif88996a2015-07-01 16:26:01 +02001135 struct fuse_forget_link *forget = dequeue_forget(fiq, 1, NULL);
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001136 struct fuse_forget_in arg = {
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001137 .nlookup = forget->forget_one.nlookup,
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001138 };
1139 struct fuse_in_header ih = {
1140 .opcode = FUSE_FORGET,
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001141 .nodeid = forget->forget_one.nodeid,
Miklos Szeredif88996a2015-07-01 16:26:01 +02001142 .unique = fuse_get_unique(fiq),
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001143 .len = sizeof(ih) + sizeof(arg),
1144 };
1145
Eric Biggers76e43c82019-09-08 20:15:18 -07001146 spin_unlock(&fiq->lock);
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001147 kfree(forget);
1148 if (nbytes < ih.len)
1149 return -EINVAL;
1150
1151 err = fuse_copy_one(cs, &ih, sizeof(ih));
1152 if (!err)
1153 err = fuse_copy_one(cs, &arg, sizeof(arg));
1154 fuse_copy_finish(cs);
1155
1156 if (err)
1157 return err;
1158
1159 return ih.len;
1160}
1161
Miklos Szeredifd22d622015-07-01 16:26:03 +02001162static int fuse_read_batch_forget(struct fuse_iqueue *fiq,
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001163 struct fuse_copy_state *cs, size_t nbytes)
Eric Biggers76e43c82019-09-08 20:15:18 -07001164__releases(fiq->lock)
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001165{
1166 int err;
1167 unsigned max_forgets;
1168 unsigned count;
1169 struct fuse_forget_link *head;
1170 struct fuse_batch_forget_in arg = { .count = 0 };
1171 struct fuse_in_header ih = {
1172 .opcode = FUSE_BATCH_FORGET,
Miklos Szeredif88996a2015-07-01 16:26:01 +02001173 .unique = fuse_get_unique(fiq),
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001174 .len = sizeof(ih) + sizeof(arg),
1175 };
1176
1177 if (nbytes < ih.len) {
Eric Biggers76e43c82019-09-08 20:15:18 -07001178 spin_unlock(&fiq->lock);
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001179 return -EINVAL;
1180 }
1181
1182 max_forgets = (nbytes - ih.len) / sizeof(struct fuse_forget_one);
Miklos Szeredif88996a2015-07-01 16:26:01 +02001183 head = dequeue_forget(fiq, max_forgets, &count);
Eric Biggers76e43c82019-09-08 20:15:18 -07001184 spin_unlock(&fiq->lock);
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001185
1186 arg.count = count;
1187 ih.len += count * sizeof(struct fuse_forget_one);
1188 err = fuse_copy_one(cs, &ih, sizeof(ih));
1189 if (!err)
1190 err = fuse_copy_one(cs, &arg, sizeof(arg));
1191
1192 while (head) {
1193 struct fuse_forget_link *forget = head;
1194
1195 if (!err) {
1196 err = fuse_copy_one(cs, &forget->forget_one,
1197 sizeof(forget->forget_one));
1198 }
1199 head = forget->next;
1200 kfree(forget);
1201 }
1202
1203 fuse_copy_finish(cs);
1204
1205 if (err)
1206 return err;
1207
1208 return ih.len;
1209}
1210
Miklos Szeredifd22d622015-07-01 16:26:03 +02001211static int fuse_read_forget(struct fuse_conn *fc, struct fuse_iqueue *fiq,
1212 struct fuse_copy_state *cs,
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001213 size_t nbytes)
Eric Biggers76e43c82019-09-08 20:15:18 -07001214__releases(fiq->lock)
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001215{
Miklos Szeredif88996a2015-07-01 16:26:01 +02001216 if (fc->minor < 16 || fiq->forget_list_head.next->next == NULL)
Miklos Szeredifd22d622015-07-01 16:26:03 +02001217 return fuse_read_single_forget(fiq, cs, nbytes);
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001218 else
Miklos Szeredifd22d622015-07-01 16:26:03 +02001219 return fuse_read_batch_forget(fiq, cs, nbytes);
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001220}
1221
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001222/*
Miklos Szeredi334f4852005-09-09 13:10:27 -07001223 * Read a single request into the userspace filesystem's buffer. This
1224 * function waits until a request is available, then removes it from
1225 * the pending list and copies request data to userspace buffer. If
Miklos Szeredif9a28422006-06-25 05:48:53 -07001226 * no reply is needed (FORGET) or request has been aborted or there
1227 * was an error during the copying then it's finished by calling
Miklos Szeredi334f4852005-09-09 13:10:27 -07001228 * request_end(). Otherwise add it to the processing list, and set
1229 * the 'sent' flag.
1230 */
Miklos Szeredic36960462015-07-01 16:26:09 +02001231static ssize_t fuse_dev_do_read(struct fuse_dev *fud, struct file *file,
Miklos Szeredic3021622010-05-25 15:06:07 +02001232 struct fuse_copy_state *cs, size_t nbytes)
Miklos Szeredi334f4852005-09-09 13:10:27 -07001233{
Miklos Szeredi82cbdcd2015-07-01 16:26:05 +02001234 ssize_t err;
Miklos Szeredic36960462015-07-01 16:26:09 +02001235 struct fuse_conn *fc = fud->fc;
Miklos Szeredif88996a2015-07-01 16:26:01 +02001236 struct fuse_iqueue *fiq = &fc->iq;
Miklos Szeredic36960462015-07-01 16:26:09 +02001237 struct fuse_pqueue *fpq = &fud->pq;
Miklos Szeredi334f4852005-09-09 13:10:27 -07001238 struct fuse_req *req;
1239 struct fuse_in *in;
Miklos Szeredi334f4852005-09-09 13:10:27 -07001240 unsigned reqsize;
Kirill Tkhaibe2ff422018-09-11 13:12:14 +03001241 unsigned int hash;
Miklos Szeredi334f4852005-09-09 13:10:27 -07001242
Kirill Smelkov1fb027d2019-07-08 17:03:31 +00001243 /*
1244 * Require sane minimum read buffer - that has capacity for fixed part
1245 * of any request header + negotiated max_write room for data.
1246 *
1247 * Historically libfuse reserves 4K for fixed header room, but e.g.
1248 * GlusterFS reserves only 80 bytes
1249 *
1250 * = `sizeof(fuse_in_header) + sizeof(fuse_write_in)`
1251 *
1252 * which is the absolute minimum any sane filesystem should be using
1253 * for header room.
1254 */
1255 if (nbytes < max_t(size_t, FUSE_MIN_READ_BUFFER,
1256 sizeof(struct fuse_in_header) +
1257 sizeof(struct fuse_write_in) +
1258 fc->max_write))
1259 return -EINVAL;
1260
Miklos Szeredi1d3d7522006-01-06 00:19:40 -08001261 restart:
Eric Biggers76e43c82019-09-08 20:15:18 -07001262 for (;;) {
1263 spin_lock(&fiq->lock);
1264 if (!fiq->connected || request_pending(fiq))
1265 break;
1266 spin_unlock(&fiq->lock);
Jeff Dikee5ac1d12006-04-10 22:54:53 -07001267
Eric Biggers76e43c82019-09-08 20:15:18 -07001268 if (file->f_flags & O_NONBLOCK)
1269 return -EAGAIN;
1270 err = wait_event_interruptible_exclusive(fiq->waitq,
Miklos Szeredi52509212015-07-01 16:26:03 +02001271 !fiq->connected || request_pending(fiq));
Eric Biggers76e43c82019-09-08 20:15:18 -07001272 if (err)
1273 return err;
1274 }
Miklos Szeredi52509212015-07-01 16:26:03 +02001275
Szymon Lukasz3b7008b2017-11-09 21:23:35 +01001276 if (!fiq->connected) {
Miklos Szeredieb98e3bd2019-01-24 10:40:16 +01001277 err = fc->aborted ? -ECONNABORTED : -ENODEV;
Miklos Szeredi334f4852005-09-09 13:10:27 -07001278 goto err_unlock;
Szymon Lukasz3b7008b2017-11-09 21:23:35 +01001279 }
Miklos Szeredi334f4852005-09-09 13:10:27 -07001280
Miklos Szeredif88996a2015-07-01 16:26:01 +02001281 if (!list_empty(&fiq->interrupts)) {
1282 req = list_entry(fiq->interrupts.next, struct fuse_req,
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001283 intr_entry);
Miklos Szeredifd22d622015-07-01 16:26:03 +02001284 return fuse_read_interrupt(fiq, cs, nbytes, req);
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001285 }
1286
Miklos Szeredif88996a2015-07-01 16:26:01 +02001287 if (forget_pending(fiq)) {
1288 if (list_empty(&fiq->pending) || fiq->forget_batch-- > 0)
Miklos Szeredifd22d622015-07-01 16:26:03 +02001289 return fuse_read_forget(fc, fiq, cs, nbytes);
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001290
Miklos Szeredif88996a2015-07-01 16:26:01 +02001291 if (fiq->forget_batch <= -8)
1292 fiq->forget_batch = 16;
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001293 }
1294
Miklos Szeredif88996a2015-07-01 16:26:01 +02001295 req = list_entry(fiq->pending.next, struct fuse_req, list);
Miklos Szeredi33e14b42015-07-01 16:26:01 +02001296 clear_bit(FR_PENDING, &req->flags);
Miklos Szeredief759252015-07-01 16:26:02 +02001297 list_del_init(&req->list);
Eric Biggers76e43c82019-09-08 20:15:18 -07001298 spin_unlock(&fiq->lock);
Miklos Szeredi4ce60812015-07-01 16:26:02 +02001299
Miklos Szeredi334f4852005-09-09 13:10:27 -07001300 in = &req->in;
Miklos Szeredi1d3d7522006-01-06 00:19:40 -08001301 reqsize = in->h.len;
Miklos Szeredi5d6d3a32017-09-12 16:57:53 +02001302
Miklos Szeredi1d3d7522006-01-06 00:19:40 -08001303 /* If request is too large, reply with an error and restart the read */
Miklos Szeredic3021622010-05-25 15:06:07 +02001304 if (nbytes < reqsize) {
Miklos Szeredi1d3d7522006-01-06 00:19:40 -08001305 req->out.h.error = -EIO;
1306 /* SETXATTR is special, since it may contain too large data */
1307 if (in->h.opcode == FUSE_SETXATTR)
1308 req->out.h.error = -E2BIG;
1309 request_end(fc, req);
1310 goto restart;
Miklos Szeredi334f4852005-09-09 13:10:27 -07001311 }
Miklos Szeredi45a91cb2015-07-01 16:26:06 +02001312 spin_lock(&fpq->lock);
Miklos Szeredi82cbdcd2015-07-01 16:26:05 +02001313 list_add(&req->list, &fpq->io);
Miklos Szeredi45a91cb2015-07-01 16:26:06 +02001314 spin_unlock(&fpq->lock);
Miklos Szeredic3021622010-05-25 15:06:07 +02001315 cs->req = req;
1316 err = fuse_copy_one(cs, &in->h, sizeof(in->h));
Miklos Szeredi1d3d7522006-01-06 00:19:40 -08001317 if (!err)
Miklos Szeredic3021622010-05-25 15:06:07 +02001318 err = fuse_copy_args(cs, in->numargs, in->argpages,
Miklos Szeredi1d3d7522006-01-06 00:19:40 -08001319 (struct fuse_arg *) in->args, 0);
Miklos Szeredic3021622010-05-25 15:06:07 +02001320 fuse_copy_finish(cs);
Miklos Szeredi45a91cb2015-07-01 16:26:06 +02001321 spin_lock(&fpq->lock);
Miklos Szeredi825d6d32015-07-01 16:25:58 +02001322 clear_bit(FR_LOCKED, &req->flags);
Miklos Szeredie96edd92015-07-01 16:26:04 +02001323 if (!fpq->connected) {
Miklos Szeredieb98e3bd2019-01-24 10:40:16 +01001324 err = fc->aborted ? -ECONNABORTED : -ENODEV;
Miklos Szeredi82cbdcd2015-07-01 16:26:05 +02001325 goto out_end;
Miklos Szeredic9c9d7d2007-10-16 23:31:05 -07001326 }
Miklos Szeredi334f4852005-09-09 13:10:27 -07001327 if (err) {
Miklos Szeredic9c9d7d2007-10-16 23:31:05 -07001328 req->out.h.error = -EIO;
Miklos Szeredi82cbdcd2015-07-01 16:26:05 +02001329 goto out_end;
Miklos Szeredi334f4852005-09-09 13:10:27 -07001330 }
Miklos Szeredi825d6d32015-07-01 16:25:58 +02001331 if (!test_bit(FR_ISREPLY, &req->flags)) {
Miklos Szeredi82cbdcd2015-07-01 16:26:05 +02001332 err = reqsize;
1333 goto out_end;
Miklos Szeredi334f4852005-09-09 13:10:27 -07001334 }
Kirill Tkhaibe2ff422018-09-11 13:12:14 +03001335 hash = fuse_req_hash(req->in.h.unique);
1336 list_move_tail(&req->list, &fpq->processing[hash]);
Kirill Tkhaibc78abb2018-09-25 12:28:55 +03001337 __fuse_get_request(req);
Miklos Szeredi82cbdcd2015-07-01 16:26:05 +02001338 set_bit(FR_SENT, &req->flags);
Miklos Szeredi4c316f22018-09-28 16:43:22 +02001339 spin_unlock(&fpq->lock);
Miklos Szeredi82cbdcd2015-07-01 16:26:05 +02001340 /* matches barrier in request_wait_answer() */
1341 smp_mb__after_atomic();
1342 if (test_bit(FR_INTERRUPTED, &req->flags))
1343 queue_interrupt(fiq, req);
Kirill Tkhaibc78abb2018-09-25 12:28:55 +03001344 fuse_put_request(fc, req);
Miklos Szeredi82cbdcd2015-07-01 16:26:05 +02001345
Miklos Szeredi334f4852005-09-09 13:10:27 -07001346 return reqsize;
1347
Miklos Szeredi82cbdcd2015-07-01 16:26:05 +02001348out_end:
Miklos Szeredi77cd9d42015-07-01 16:26:06 +02001349 if (!test_bit(FR_PRIVATE, &req->flags))
1350 list_del_init(&req->list);
Miklos Szeredi45a91cb2015-07-01 16:26:06 +02001351 spin_unlock(&fpq->lock);
Miklos Szeredi82cbdcd2015-07-01 16:26:05 +02001352 request_end(fc, req);
1353 return err;
1354
Miklos Szeredi334f4852005-09-09 13:10:27 -07001355 err_unlock:
Eric Biggers76e43c82019-09-08 20:15:18 -07001356 spin_unlock(&fiq->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001357 return err;
1358}
1359
Tom Van Braeckel94e4fe22015-01-12 05:22:16 +01001360static int fuse_dev_open(struct inode *inode, struct file *file)
1361{
1362 /*
1363 * The fuse device's file's private_data is used to hold
1364 * the fuse_conn(ection) when it is mounted, and is used to
1365 * keep track of whether the file has been mounted already.
1366 */
1367 file->private_data = NULL;
1368 return 0;
1369}
1370
Al Virofbdbacc2015-04-03 21:53:39 -04001371static ssize_t fuse_dev_read(struct kiocb *iocb, struct iov_iter *to)
Miklos Szeredic3021622010-05-25 15:06:07 +02001372{
1373 struct fuse_copy_state cs;
1374 struct file *file = iocb->ki_filp;
Miklos Szeredicc080e92015-07-01 16:26:08 +02001375 struct fuse_dev *fud = fuse_get_dev(file);
1376
1377 if (!fud)
Miklos Szeredic3021622010-05-25 15:06:07 +02001378 return -EPERM;
1379
Al Virofbdbacc2015-04-03 21:53:39 -04001380 if (!iter_is_iovec(to))
1381 return -EINVAL;
Miklos Szeredic3021622010-05-25 15:06:07 +02001382
Miklos Szeredidc008092015-07-01 16:25:58 +02001383 fuse_copy_init(&cs, 1, to);
Al Virofbdbacc2015-04-03 21:53:39 -04001384
Miklos Szeredic36960462015-07-01 16:26:09 +02001385 return fuse_dev_do_read(fud, file, &cs, iov_iter_count(to));
Miklos Szeredic3021622010-05-25 15:06:07 +02001386}
1387
Miklos Szeredic3021622010-05-25 15:06:07 +02001388static ssize_t fuse_dev_splice_read(struct file *in, loff_t *ppos,
1389 struct pipe_inode_info *pipe,
1390 size_t len, unsigned int flags)
1391{
Al Virod82718e2016-09-17 22:56:25 -04001392 int total, ret;
Miklos Szeredic3021622010-05-25 15:06:07 +02001393 int page_nr = 0;
Miklos Szeredic3021622010-05-25 15:06:07 +02001394 struct pipe_buffer *bufs;
1395 struct fuse_copy_state cs;
Miklos Szeredicc080e92015-07-01 16:26:08 +02001396 struct fuse_dev *fud = fuse_get_dev(in);
1397
1398 if (!fud)
Miklos Szeredic3021622010-05-25 15:06:07 +02001399 return -EPERM;
1400
Andrey Ryabinind6d931a2018-07-17 19:00:34 +03001401 bufs = kvmalloc_array(pipe->buffers, sizeof(struct pipe_buffer),
1402 GFP_KERNEL);
Miklos Szeredic3021622010-05-25 15:06:07 +02001403 if (!bufs)
1404 return -ENOMEM;
1405
Miklos Szeredidc008092015-07-01 16:25:58 +02001406 fuse_copy_init(&cs, 1, NULL);
Miklos Szeredic3021622010-05-25 15:06:07 +02001407 cs.pipebufs = bufs;
1408 cs.pipe = pipe;
Miklos Szeredic36960462015-07-01 16:26:09 +02001409 ret = fuse_dev_do_read(fud, in, &cs, len);
Miklos Szeredic3021622010-05-25 15:06:07 +02001410 if (ret < 0)
1411 goto out;
1412
Miklos Szeredic3021622010-05-25 15:06:07 +02001413 if (pipe->nrbufs + cs.nr_segs > pipe->buffers) {
1414 ret = -EIO;
Al Virod82718e2016-09-17 22:56:25 -04001415 goto out;
Miklos Szeredic3021622010-05-25 15:06:07 +02001416 }
1417
Al Virod82718e2016-09-17 22:56:25 -04001418 for (ret = total = 0; page_nr < cs.nr_segs; total += ret) {
Miklos Szeredi28a625c2014-01-22 19:36:57 +01001419 /*
1420 * Need to be careful about this. Having buf->ops in module
1421 * code can Oops if the buffer persists after module unload.
1422 */
Al Virod82718e2016-09-17 22:56:25 -04001423 bufs[page_nr].ops = &nosteal_pipe_buf_ops;
Miklos Szeredi84588a92017-02-16 15:08:20 +01001424 bufs[page_nr].flags = 0;
Al Virod82718e2016-09-17 22:56:25 -04001425 ret = add_to_pipe(pipe, &bufs[page_nr++]);
1426 if (unlikely(ret < 0))
1427 break;
Miklos Szeredic3021622010-05-25 15:06:07 +02001428 }
Al Virod82718e2016-09-17 22:56:25 -04001429 if (total)
1430 ret = total;
Miklos Szeredic3021622010-05-25 15:06:07 +02001431out:
1432 for (; page_nr < cs.nr_segs; page_nr++)
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001433 put_page(bufs[page_nr].page);
Miklos Szeredic3021622010-05-25 15:06:07 +02001434
Andrey Ryabinind6d931a2018-07-17 19:00:34 +03001435 kvfree(bufs);
Miklos Szeredic3021622010-05-25 15:06:07 +02001436 return ret;
1437}
1438
Tejun Heo95668a62008-11-26 12:03:55 +01001439static int fuse_notify_poll(struct fuse_conn *fc, unsigned int size,
1440 struct fuse_copy_state *cs)
1441{
1442 struct fuse_notify_poll_wakeup_out outarg;
Miklos Szeredif6d47a12009-01-26 15:00:59 +01001443 int err = -EINVAL;
Tejun Heo95668a62008-11-26 12:03:55 +01001444
1445 if (size != sizeof(outarg))
Miklos Szeredif6d47a12009-01-26 15:00:59 +01001446 goto err;
Tejun Heo95668a62008-11-26 12:03:55 +01001447
1448 err = fuse_copy_one(cs, &outarg, sizeof(outarg));
1449 if (err)
Miklos Szeredif6d47a12009-01-26 15:00:59 +01001450 goto err;
Tejun Heo95668a62008-11-26 12:03:55 +01001451
Miklos Szeredif6d47a12009-01-26 15:00:59 +01001452 fuse_copy_finish(cs);
Tejun Heo95668a62008-11-26 12:03:55 +01001453 return fuse_notify_poll_wakeup(fc, &outarg);
Miklos Szeredif6d47a12009-01-26 15:00:59 +01001454
1455err:
1456 fuse_copy_finish(cs);
1457 return err;
Tejun Heo95668a62008-11-26 12:03:55 +01001458}
1459
John Muir3b463ae2009-05-31 11:13:57 -04001460static int fuse_notify_inval_inode(struct fuse_conn *fc, unsigned int size,
1461 struct fuse_copy_state *cs)
1462{
1463 struct fuse_notify_inval_inode_out outarg;
1464 int err = -EINVAL;
1465
1466 if (size != sizeof(outarg))
1467 goto err;
1468
1469 err = fuse_copy_one(cs, &outarg, sizeof(outarg));
1470 if (err)
1471 goto err;
1472 fuse_copy_finish(cs);
1473
1474 down_read(&fc->killsb);
1475 err = -ENOENT;
Miklos Szeredib21dda42010-02-05 12:08:31 +01001476 if (fc->sb) {
1477 err = fuse_reverse_inval_inode(fc->sb, outarg.ino,
1478 outarg.off, outarg.len);
1479 }
John Muir3b463ae2009-05-31 11:13:57 -04001480 up_read(&fc->killsb);
1481 return err;
1482
1483err:
1484 fuse_copy_finish(cs);
1485 return err;
1486}
1487
1488static int fuse_notify_inval_entry(struct fuse_conn *fc, unsigned int size,
1489 struct fuse_copy_state *cs)
1490{
1491 struct fuse_notify_inval_entry_out outarg;
Fang Wenqib2d82ee2009-12-30 18:37:13 +08001492 int err = -ENOMEM;
1493 char *buf;
John Muir3b463ae2009-05-31 11:13:57 -04001494 struct qstr name;
1495
Fang Wenqib2d82ee2009-12-30 18:37:13 +08001496 buf = kzalloc(FUSE_NAME_MAX + 1, GFP_KERNEL);
1497 if (!buf)
1498 goto err;
1499
1500 err = -EINVAL;
John Muir3b463ae2009-05-31 11:13:57 -04001501 if (size < sizeof(outarg))
1502 goto err;
1503
1504 err = fuse_copy_one(cs, &outarg, sizeof(outarg));
1505 if (err)
1506 goto err;
1507
1508 err = -ENAMETOOLONG;
1509 if (outarg.namelen > FUSE_NAME_MAX)
1510 goto err;
1511
Miklos Szeredic2183d12011-08-24 10:20:17 +02001512 err = -EINVAL;
1513 if (size != sizeof(outarg) + outarg.namelen + 1)
1514 goto err;
1515
John Muir3b463ae2009-05-31 11:13:57 -04001516 name.name = buf;
1517 name.len = outarg.namelen;
1518 err = fuse_copy_one(cs, buf, outarg.namelen + 1);
1519 if (err)
1520 goto err;
1521 fuse_copy_finish(cs);
1522 buf[outarg.namelen] = 0;
John Muir3b463ae2009-05-31 11:13:57 -04001523
1524 down_read(&fc->killsb);
1525 err = -ENOENT;
Miklos Szeredib21dda42010-02-05 12:08:31 +01001526 if (fc->sb)
John Muir451d0f52011-12-06 21:50:06 +01001527 err = fuse_reverse_inval_entry(fc->sb, outarg.parent, 0, &name);
1528 up_read(&fc->killsb);
1529 kfree(buf);
1530 return err;
1531
1532err:
1533 kfree(buf);
1534 fuse_copy_finish(cs);
1535 return err;
1536}
1537
1538static int fuse_notify_delete(struct fuse_conn *fc, unsigned int size,
1539 struct fuse_copy_state *cs)
1540{
1541 struct fuse_notify_delete_out outarg;
1542 int err = -ENOMEM;
1543 char *buf;
1544 struct qstr name;
1545
1546 buf = kzalloc(FUSE_NAME_MAX + 1, GFP_KERNEL);
1547 if (!buf)
1548 goto err;
1549
1550 err = -EINVAL;
1551 if (size < sizeof(outarg))
1552 goto err;
1553
1554 err = fuse_copy_one(cs, &outarg, sizeof(outarg));
1555 if (err)
1556 goto err;
1557
1558 err = -ENAMETOOLONG;
1559 if (outarg.namelen > FUSE_NAME_MAX)
1560 goto err;
1561
1562 err = -EINVAL;
1563 if (size != sizeof(outarg) + outarg.namelen + 1)
1564 goto err;
1565
1566 name.name = buf;
1567 name.len = outarg.namelen;
1568 err = fuse_copy_one(cs, buf, outarg.namelen + 1);
1569 if (err)
1570 goto err;
1571 fuse_copy_finish(cs);
1572 buf[outarg.namelen] = 0;
John Muir451d0f52011-12-06 21:50:06 +01001573
1574 down_read(&fc->killsb);
1575 err = -ENOENT;
1576 if (fc->sb)
1577 err = fuse_reverse_inval_entry(fc->sb, outarg.parent,
1578 outarg.child, &name);
John Muir3b463ae2009-05-31 11:13:57 -04001579 up_read(&fc->killsb);
Fang Wenqib2d82ee2009-12-30 18:37:13 +08001580 kfree(buf);
John Muir3b463ae2009-05-31 11:13:57 -04001581 return err;
1582
1583err:
Fang Wenqib2d82ee2009-12-30 18:37:13 +08001584 kfree(buf);
John Muir3b463ae2009-05-31 11:13:57 -04001585 fuse_copy_finish(cs);
1586 return err;
1587}
1588
Miklos Szeredia1d75f22010-07-12 14:41:40 +02001589static int fuse_notify_store(struct fuse_conn *fc, unsigned int size,
1590 struct fuse_copy_state *cs)
1591{
1592 struct fuse_notify_store_out outarg;
1593 struct inode *inode;
1594 struct address_space *mapping;
1595 u64 nodeid;
1596 int err;
1597 pgoff_t index;
1598 unsigned int offset;
1599 unsigned int num;
1600 loff_t file_size;
1601 loff_t end;
1602
1603 err = -EINVAL;
1604 if (size < sizeof(outarg))
1605 goto out_finish;
1606
1607 err = fuse_copy_one(cs, &outarg, sizeof(outarg));
1608 if (err)
1609 goto out_finish;
1610
1611 err = -EINVAL;
1612 if (size - sizeof(outarg) != outarg.size)
1613 goto out_finish;
1614
1615 nodeid = outarg.nodeid;
1616
1617 down_read(&fc->killsb);
1618
1619 err = -ENOENT;
1620 if (!fc->sb)
1621 goto out_up_killsb;
1622
1623 inode = ilookup5(fc->sb, nodeid, fuse_inode_eq, &nodeid);
1624 if (!inode)
1625 goto out_up_killsb;
1626
1627 mapping = inode->i_mapping;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001628 index = outarg.offset >> PAGE_SHIFT;
1629 offset = outarg.offset & ~PAGE_MASK;
Miklos Szeredia1d75f22010-07-12 14:41:40 +02001630 file_size = i_size_read(inode);
1631 end = outarg.offset + outarg.size;
1632 if (end > file_size) {
1633 file_size = end;
1634 fuse_write_update_size(inode, file_size);
1635 }
1636
1637 num = outarg.size;
1638 while (num) {
1639 struct page *page;
1640 unsigned int this_num;
1641
1642 err = -ENOMEM;
1643 page = find_or_create_page(mapping, index,
1644 mapping_gfp_mask(mapping));
1645 if (!page)
1646 goto out_iput;
1647
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001648 this_num = min_t(unsigned, num, PAGE_SIZE - offset);
Miklos Szeredia1d75f22010-07-12 14:41:40 +02001649 err = fuse_copy_page(cs, &page, offset, this_num, 0);
Miklos Szeredi063ec1e2014-01-22 19:36:58 +01001650 if (!err && offset == 0 &&
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001651 (this_num == PAGE_SIZE || file_size == end))
Miklos Szeredia1d75f22010-07-12 14:41:40 +02001652 SetPageUptodate(page);
1653 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001654 put_page(page);
Miklos Szeredia1d75f22010-07-12 14:41:40 +02001655
1656 if (err)
1657 goto out_iput;
1658
1659 num -= this_num;
1660 offset = 0;
1661 index++;
1662 }
1663
1664 err = 0;
1665
1666out_iput:
1667 iput(inode);
1668out_up_killsb:
1669 up_read(&fc->killsb);
1670out_finish:
1671 fuse_copy_finish(cs);
1672 return err;
1673}
1674
Miklos Szeredi75b399d2019-09-10 15:04:11 +02001675struct fuse_retrieve_args {
1676 struct fuse_args_pages ap;
1677 struct fuse_notify_retrieve_in inarg;
1678};
1679
1680static void fuse_retrieve_end(struct fuse_conn *fc, struct fuse_args *args,
1681 int error)
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001682{
Miklos Szeredi75b399d2019-09-10 15:04:11 +02001683 struct fuse_retrieve_args *ra =
1684 container_of(args, typeof(*ra), ap.args);
1685
1686 release_pages(ra->ap.pages, ra->ap.num_pages);
1687 kfree(ra);
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001688}
1689
1690static int fuse_retrieve(struct fuse_conn *fc, struct inode *inode,
1691 struct fuse_notify_retrieve_out *outarg)
1692{
1693 int err;
1694 struct address_space *mapping = inode->i_mapping;
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001695 pgoff_t index;
1696 loff_t file_size;
1697 unsigned int num;
1698 unsigned int offset;
Geert Uytterhoeven01574432010-09-30 22:06:21 +02001699 size_t total_len = 0;
Constantine Shulyupin5da784c2018-09-06 15:37:06 +03001700 unsigned int num_pages;
Miklos Szeredi75b399d2019-09-10 15:04:11 +02001701 struct fuse_retrieve_args *ra;
1702 size_t args_size = sizeof(*ra);
1703 struct fuse_args_pages *ap;
1704 struct fuse_args *args;
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001705
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001706 offset = outarg->offset & ~PAGE_MASK;
Maxim Patlasov4d53dc92012-10-26 19:48:42 +04001707 file_size = i_size_read(inode);
1708
Kirill Smelkov76406822019-03-27 10:15:19 +00001709 num = min(outarg->size, fc->max_write);
Maxim Patlasov4d53dc92012-10-26 19:48:42 +04001710 if (outarg->offset > file_size)
1711 num = 0;
1712 else if (outarg->offset + num > file_size)
1713 num = file_size - outarg->offset;
1714
1715 num_pages = (num + offset + PAGE_SIZE - 1) >> PAGE_SHIFT;
Constantine Shulyupin5da784c2018-09-06 15:37:06 +03001716 num_pages = min(num_pages, fc->max_pages);
Maxim Patlasov4d53dc92012-10-26 19:48:42 +04001717
Miklos Szeredi75b399d2019-09-10 15:04:11 +02001718 args_size += num_pages * (sizeof(ap->pages[0]) + sizeof(ap->descs[0]));
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001719
Miklos Szeredi75b399d2019-09-10 15:04:11 +02001720 ra = kzalloc(args_size, GFP_KERNEL);
1721 if (!ra)
1722 return -ENOMEM;
1723
1724 ap = &ra->ap;
1725 ap->pages = (void *) (ra + 1);
1726 ap->descs = (void *) (ap->pages + num_pages);
1727
1728 args = &ap->args;
1729 args->nodeid = outarg->nodeid;
1730 args->opcode = FUSE_NOTIFY_REPLY;
1731 args->in_numargs = 2;
1732 args->in_pages = true;
1733 args->end = fuse_retrieve_end;
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001734
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001735 index = outarg->offset >> PAGE_SHIFT;
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001736
Miklos Szeredi75b399d2019-09-10 15:04:11 +02001737 while (num && ap->num_pages < num_pages) {
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001738 struct page *page;
1739 unsigned int this_num;
1740
1741 page = find_get_page(mapping, index);
1742 if (!page)
1743 break;
1744
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001745 this_num = min_t(unsigned, num, PAGE_SIZE - offset);
Miklos Szeredi75b399d2019-09-10 15:04:11 +02001746 ap->pages[ap->num_pages] = page;
1747 ap->descs[ap->num_pages].offset = offset;
1748 ap->descs[ap->num_pages].length = this_num;
1749 ap->num_pages++;
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001750
Miklos Szeredic9e67d42012-09-04 18:45:54 +02001751 offset = 0;
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001752 num -= this_num;
1753 total_len += this_num;
Miklos Szeredi48706d02011-12-13 10:36:59 +01001754 index++;
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001755 }
Miklos Szeredi75b399d2019-09-10 15:04:11 +02001756 ra->inarg.offset = outarg->offset;
1757 ra->inarg.size = total_len;
1758 args->in_args[0].size = sizeof(ra->inarg);
1759 args->in_args[0].value = &ra->inarg;
1760 args->in_args[1].size = total_len;
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001761
Miklos Szeredi75b399d2019-09-10 15:04:11 +02001762 err = fuse_simple_notify_reply(fc, args, outarg->notify_unique);
1763 if (err)
1764 fuse_retrieve_end(fc, args, err);
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001765
1766 return err;
1767}
1768
1769static int fuse_notify_retrieve(struct fuse_conn *fc, unsigned int size,
1770 struct fuse_copy_state *cs)
1771{
1772 struct fuse_notify_retrieve_out outarg;
1773 struct inode *inode;
1774 int err;
1775
1776 err = -EINVAL;
1777 if (size != sizeof(outarg))
1778 goto copy_finish;
1779
1780 err = fuse_copy_one(cs, &outarg, sizeof(outarg));
1781 if (err)
1782 goto copy_finish;
1783
1784 fuse_copy_finish(cs);
1785
1786 down_read(&fc->killsb);
1787 err = -ENOENT;
1788 if (fc->sb) {
1789 u64 nodeid = outarg.nodeid;
1790
1791 inode = ilookup5(fc->sb, nodeid, fuse_inode_eq, &nodeid);
1792 if (inode) {
1793 err = fuse_retrieve(fc, inode, &outarg);
1794 iput(inode);
1795 }
1796 }
1797 up_read(&fc->killsb);
1798
1799 return err;
1800
1801copy_finish:
1802 fuse_copy_finish(cs);
1803 return err;
1804}
1805
Tejun Heo85993962008-11-26 12:03:55 +01001806static int fuse_notify(struct fuse_conn *fc, enum fuse_notify_code code,
1807 unsigned int size, struct fuse_copy_state *cs)
1808{
Miklos Szeredi0d278362015-02-26 11:45:47 +01001809 /* Don't try to move pages (yet) */
1810 cs->move_pages = 0;
1811
Tejun Heo85993962008-11-26 12:03:55 +01001812 switch (code) {
Tejun Heo95668a62008-11-26 12:03:55 +01001813 case FUSE_NOTIFY_POLL:
1814 return fuse_notify_poll(fc, size, cs);
1815
John Muir3b463ae2009-05-31 11:13:57 -04001816 case FUSE_NOTIFY_INVAL_INODE:
1817 return fuse_notify_inval_inode(fc, size, cs);
1818
1819 case FUSE_NOTIFY_INVAL_ENTRY:
1820 return fuse_notify_inval_entry(fc, size, cs);
1821
Miklos Szeredia1d75f22010-07-12 14:41:40 +02001822 case FUSE_NOTIFY_STORE:
1823 return fuse_notify_store(fc, size, cs);
1824
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001825 case FUSE_NOTIFY_RETRIEVE:
1826 return fuse_notify_retrieve(fc, size, cs);
1827
John Muir451d0f52011-12-06 21:50:06 +01001828 case FUSE_NOTIFY_DELETE:
1829 return fuse_notify_delete(fc, size, cs);
1830
Tejun Heo85993962008-11-26 12:03:55 +01001831 default:
Miklos Szeredif6d47a12009-01-26 15:00:59 +01001832 fuse_copy_finish(cs);
Tejun Heo85993962008-11-26 12:03:55 +01001833 return -EINVAL;
1834 }
1835}
1836
Miklos Szeredi334f4852005-09-09 13:10:27 -07001837/* Look up request on processing list by unique ID */
Miklos Szeredi3a2b5b92015-07-01 16:26:04 +02001838static struct fuse_req *request_find(struct fuse_pqueue *fpq, u64 unique)
Miklos Szeredi334f4852005-09-09 13:10:27 -07001839{
Kirill Tkhaibe2ff422018-09-11 13:12:14 +03001840 unsigned int hash = fuse_req_hash(unique);
Dong Fang05726ac2013-07-30 22:50:01 -04001841 struct fuse_req *req;
Miklos Szeredi334f4852005-09-09 13:10:27 -07001842
Kirill Tkhaibe2ff422018-09-11 13:12:14 +03001843 list_for_each_entry(req, &fpq->processing[hash], list) {
Kirill Tkhai3a5358d2018-09-11 13:12:05 +03001844 if (req->in.h.unique == unique)
Miklos Szeredi334f4852005-09-09 13:10:27 -07001845 return req;
1846 }
1847 return NULL;
1848}
1849
1850static int copy_out_args(struct fuse_copy_state *cs, struct fuse_out *out,
1851 unsigned nbytes)
1852{
1853 unsigned reqsize = sizeof(struct fuse_out_header);
1854
1855 if (out->h.error)
1856 return nbytes != reqsize ? -EINVAL : 0;
1857
1858 reqsize += len_args(out->numargs, out->args);
1859
1860 if (reqsize < nbytes || (reqsize > nbytes && !out->argvar))
1861 return -EINVAL;
1862 else if (reqsize > nbytes) {
1863 struct fuse_arg *lastarg = &out->args[out->numargs-1];
1864 unsigned diffsize = reqsize - nbytes;
1865 if (diffsize > lastarg->size)
1866 return -EINVAL;
1867 lastarg->size -= diffsize;
1868 }
1869 return fuse_copy_args(cs, out->numargs, out->argpages, out->args,
1870 out->page_zeroing);
1871}
1872
1873/*
1874 * Write a single reply to a request. First the header is copied from
1875 * the write buffer. The request is then searched on the processing
1876 * list by the unique ID found in the header. If found, then remove
1877 * it from the list and copy the rest of the buffer to the request.
1878 * The request is finished by calling request_end()
1879 */
Miklos Szeredic36960462015-07-01 16:26:09 +02001880static ssize_t fuse_dev_do_write(struct fuse_dev *fud,
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001881 struct fuse_copy_state *cs, size_t nbytes)
Miklos Szeredi334f4852005-09-09 13:10:27 -07001882{
1883 int err;
Miklos Szeredic36960462015-07-01 16:26:09 +02001884 struct fuse_conn *fc = fud->fc;
1885 struct fuse_pqueue *fpq = &fud->pq;
Miklos Szeredi334f4852005-09-09 13:10:27 -07001886 struct fuse_req *req;
1887 struct fuse_out_header oh;
Miklos Szeredi334f4852005-09-09 13:10:27 -07001888
Kirill Tkhai7407a102018-11-08 12:05:36 +03001889 err = -EINVAL;
Miklos Szeredi334f4852005-09-09 13:10:27 -07001890 if (nbytes < sizeof(struct fuse_out_header))
Kirill Tkhai7407a102018-11-08 12:05:36 +03001891 goto out;
Miklos Szeredi334f4852005-09-09 13:10:27 -07001892
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001893 err = fuse_copy_one(cs, &oh, sizeof(oh));
Miklos Szeredi334f4852005-09-09 13:10:27 -07001894 if (err)
Kirill Tkhai7407a102018-11-08 12:05:36 +03001895 goto copy_finish;
Tejun Heo85993962008-11-26 12:03:55 +01001896
Miklos Szeredi334f4852005-09-09 13:10:27 -07001897 err = -EINVAL;
Tejun Heo85993962008-11-26 12:03:55 +01001898 if (oh.len != nbytes)
Kirill Tkhai7407a102018-11-08 12:05:36 +03001899 goto copy_finish;
Tejun Heo85993962008-11-26 12:03:55 +01001900
1901 /*
1902 * Zero oh.unique indicates unsolicited notification message
1903 * and error contains notification code.
1904 */
1905 if (!oh.unique) {
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001906 err = fuse_notify(fc, oh.error, nbytes - sizeof(oh), cs);
Kirill Tkhai7407a102018-11-08 12:05:36 +03001907 goto out;
Tejun Heo85993962008-11-26 12:03:55 +01001908 }
1909
1910 err = -EINVAL;
1911 if (oh.error <= -1000 || oh.error > 0)
Kirill Tkhai7407a102018-11-08 12:05:36 +03001912 goto copy_finish;
Miklos Szeredi334f4852005-09-09 13:10:27 -07001913
Miklos Szeredi45a91cb2015-07-01 16:26:06 +02001914 spin_lock(&fpq->lock);
Kirill Tkhai7407a102018-11-08 12:05:36 +03001915 req = NULL;
1916 if (fpq->connected)
1917 req = request_find(fpq, oh.unique & ~FUSE_INT_REQ_BIT);
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08001918
Kirill Tkhai7407a102018-11-08 12:05:36 +03001919 err = -ENOENT;
1920 if (!req) {
1921 spin_unlock(&fpq->lock);
1922 goto copy_finish;
1923 }
Miklos Szeredi334f4852005-09-09 13:10:27 -07001924
Kirill Tkhai3a5358d2018-09-11 13:12:05 +03001925 /* Is it an interrupt reply ID? */
1926 if (oh.unique & FUSE_INT_REQ_BIT) {
Kirill Tkhaid2d2d4f2018-09-25 12:52:42 +03001927 __fuse_get_request(req);
Miklos Szeredi45a91cb2015-07-01 16:26:06 +02001928 spin_unlock(&fpq->lock);
1929
Kirill Tkhai7407a102018-11-08 12:05:36 +03001930 err = 0;
1931 if (nbytes != sizeof(struct fuse_out_header))
1932 err = -EINVAL;
1933 else if (oh.error == -ENOSYS)
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001934 fc->no_interrupt = 1;
1935 else if (oh.error == -EAGAIN)
Kirill Tkhaib7829112018-11-08 12:05:42 +03001936 err = queue_interrupt(&fc->iq, req);
Kirill Tkhai7407a102018-11-08 12:05:36 +03001937
Kirill Tkhaid2d2d4f2018-09-25 12:52:42 +03001938 fuse_put_request(fc, req);
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001939
Kirill Tkhai7407a102018-11-08 12:05:36 +03001940 goto copy_finish;
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001941 }
1942
Miklos Szeredi33e14b42015-07-01 16:26:01 +02001943 clear_bit(FR_SENT, &req->flags);
Miklos Szeredi3a2b5b92015-07-01 16:26:04 +02001944 list_move(&req->list, &fpq->io);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001945 req->out.h = oh;
Miklos Szeredi825d6d32015-07-01 16:25:58 +02001946 set_bit(FR_LOCKED, &req->flags);
Miklos Szeredi45a91cb2015-07-01 16:26:06 +02001947 spin_unlock(&fpq->lock);
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001948 cs->req = req;
Miklos Szeredice534fb2010-05-25 15:06:07 +02001949 if (!req->out.page_replace)
1950 cs->move_pages = 0;
Miklos Szeredi334f4852005-09-09 13:10:27 -07001951
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001952 err = copy_out_args(cs, &req->out, nbytes);
1953 fuse_copy_finish(cs);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001954
Miklos Szeredi45a91cb2015-07-01 16:26:06 +02001955 spin_lock(&fpq->lock);
Miklos Szeredi825d6d32015-07-01 16:25:58 +02001956 clear_bit(FR_LOCKED, &req->flags);
Miklos Szeredie96edd92015-07-01 16:26:04 +02001957 if (!fpq->connected)
Miklos Szeredi0d8e84b2015-07-01 16:25:58 +02001958 err = -ENOENT;
1959 else if (err)
Miklos Szeredi334f4852005-09-09 13:10:27 -07001960 req->out.h.error = -EIO;
Miklos Szeredi77cd9d42015-07-01 16:26:06 +02001961 if (!test_bit(FR_PRIVATE, &req->flags))
1962 list_del_init(&req->list);
Miklos Szeredi45a91cb2015-07-01 16:26:06 +02001963 spin_unlock(&fpq->lock);
Miklos Szeredi46c34a32015-07-01 16:26:07 +02001964
Miklos Szeredi334f4852005-09-09 13:10:27 -07001965 request_end(fc, req);
Kirill Tkhai7407a102018-11-08 12:05:36 +03001966out:
Miklos Szeredi334f4852005-09-09 13:10:27 -07001967 return err ? err : nbytes;
1968
Kirill Tkhai7407a102018-11-08 12:05:36 +03001969copy_finish:
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001970 fuse_copy_finish(cs);
Kirill Tkhai7407a102018-11-08 12:05:36 +03001971 goto out;
Miklos Szeredi334f4852005-09-09 13:10:27 -07001972}
1973
Al Virofbdbacc2015-04-03 21:53:39 -04001974static ssize_t fuse_dev_write(struct kiocb *iocb, struct iov_iter *from)
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001975{
1976 struct fuse_copy_state cs;
Miklos Szeredicc080e92015-07-01 16:26:08 +02001977 struct fuse_dev *fud = fuse_get_dev(iocb->ki_filp);
1978
1979 if (!fud)
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001980 return -EPERM;
1981
Al Virofbdbacc2015-04-03 21:53:39 -04001982 if (!iter_is_iovec(from))
1983 return -EINVAL;
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001984
Miklos Szeredidc008092015-07-01 16:25:58 +02001985 fuse_copy_init(&cs, 0, from);
Al Virofbdbacc2015-04-03 21:53:39 -04001986
Miklos Szeredic36960462015-07-01 16:26:09 +02001987 return fuse_dev_do_write(fud, &cs, iov_iter_count(from));
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001988}
1989
1990static ssize_t fuse_dev_splice_write(struct pipe_inode_info *pipe,
1991 struct file *out, loff_t *ppos,
1992 size_t len, unsigned int flags)
1993{
1994 unsigned nbuf;
1995 unsigned idx;
1996 struct pipe_buffer *bufs;
1997 struct fuse_copy_state cs;
Miklos Szeredicc080e92015-07-01 16:26:08 +02001998 struct fuse_dev *fud;
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001999 size_t rem;
2000 ssize_t ret;
2001
Miklos Szeredicc080e92015-07-01 16:26:08 +02002002 fud = fuse_get_dev(out);
2003 if (!fud)
Miklos Szeredidd3bb142010-05-25 15:06:06 +02002004 return -EPERM;
2005
Andrey Ryabinina2477b02018-07-17 19:00:33 +03002006 pipe_lock(pipe);
2007
Andrey Ryabinin96354532018-07-17 19:00:35 +03002008 bufs = kvmalloc_array(pipe->nrbufs, sizeof(struct pipe_buffer),
Andrey Ryabinind6d931a2018-07-17 19:00:34 +03002009 GFP_KERNEL);
Andrey Ryabinina2477b02018-07-17 19:00:33 +03002010 if (!bufs) {
2011 pipe_unlock(pipe);
Miklos Szeredidd3bb142010-05-25 15:06:06 +02002012 return -ENOMEM;
Andrey Ryabinina2477b02018-07-17 19:00:33 +03002013 }
Miklos Szeredidd3bb142010-05-25 15:06:06 +02002014
Miklos Szeredidd3bb142010-05-25 15:06:06 +02002015 nbuf = 0;
2016 rem = 0;
2017 for (idx = 0; idx < pipe->nrbufs && rem < len; idx++)
2018 rem += pipe->bufs[(pipe->curbuf + idx) & (pipe->buffers - 1)].len;
2019
2020 ret = -EINVAL;
Matthew Wilcox15fab632019-04-05 14:02:10 -07002021 if (rem < len)
2022 goto out_free;
Miklos Szeredidd3bb142010-05-25 15:06:06 +02002023
2024 rem = len;
2025 while (rem) {
2026 struct pipe_buffer *ibuf;
2027 struct pipe_buffer *obuf;
2028
2029 BUG_ON(nbuf >= pipe->buffers);
2030 BUG_ON(!pipe->nrbufs);
2031 ibuf = &pipe->bufs[pipe->curbuf];
2032 obuf = &bufs[nbuf];
2033
2034 if (rem >= ibuf->len) {
2035 *obuf = *ibuf;
2036 ibuf->ops = NULL;
2037 pipe->curbuf = (pipe->curbuf + 1) & (pipe->buffers - 1);
2038 pipe->nrbufs--;
2039 } else {
Matthew Wilcox15fab632019-04-05 14:02:10 -07002040 if (!pipe_buf_get(pipe, ibuf))
2041 goto out_free;
2042
Miklos Szeredidd3bb142010-05-25 15:06:06 +02002043 *obuf = *ibuf;
2044 obuf->flags &= ~PIPE_BUF_FLAG_GIFT;
2045 obuf->len = rem;
2046 ibuf->offset += obuf->len;
2047 ibuf->len -= obuf->len;
2048 }
2049 nbuf++;
2050 rem -= obuf->len;
2051 }
2052 pipe_unlock(pipe);
2053
Miklos Szeredidc008092015-07-01 16:25:58 +02002054 fuse_copy_init(&cs, 0, NULL);
Miklos Szeredidd3bb142010-05-25 15:06:06 +02002055 cs.pipebufs = bufs;
Al Viro6c09e942015-04-03 22:06:08 -04002056 cs.nr_segs = nbuf;
Miklos Szeredidd3bb142010-05-25 15:06:06 +02002057 cs.pipe = pipe;
2058
Miklos Szeredice534fb2010-05-25 15:06:07 +02002059 if (flags & SPLICE_F_MOVE)
2060 cs.move_pages = 1;
2061
Miklos Szeredic36960462015-07-01 16:26:09 +02002062 ret = fuse_dev_do_write(fud, &cs, len);
Miklos Szeredidd3bb142010-05-25 15:06:06 +02002063
Jann Horn95099412019-01-12 02:39:05 +01002064 pipe_lock(pipe);
Matthew Wilcox15fab632019-04-05 14:02:10 -07002065out_free:
Miklos Szeredia7796382016-09-27 10:45:12 +02002066 for (idx = 0; idx < nbuf; idx++)
2067 pipe_buf_release(pipe, &bufs[idx]);
Jann Horn95099412019-01-12 02:39:05 +01002068 pipe_unlock(pipe);
Miklos Szeredia7796382016-09-27 10:45:12 +02002069
Andrey Ryabinind6d931a2018-07-17 19:00:34 +03002070 kvfree(bufs);
Miklos Szeredidd3bb142010-05-25 15:06:06 +02002071 return ret;
2072}
2073
Al Viro076ccb72017-07-03 01:02:18 -04002074static __poll_t fuse_dev_poll(struct file *file, poll_table *wait)
Miklos Szeredi334f4852005-09-09 13:10:27 -07002075{
Linus Torvaldsa9a08842018-02-11 14:34:03 -08002076 __poll_t mask = EPOLLOUT | EPOLLWRNORM;
Miklos Szeredif88996a2015-07-01 16:26:01 +02002077 struct fuse_iqueue *fiq;
Miklos Szeredicc080e92015-07-01 16:26:08 +02002078 struct fuse_dev *fud = fuse_get_dev(file);
2079
2080 if (!fud)
Linus Torvaldsa9a08842018-02-11 14:34:03 -08002081 return EPOLLERR;
Miklos Szeredi334f4852005-09-09 13:10:27 -07002082
Miklos Szeredicc080e92015-07-01 16:26:08 +02002083 fiq = &fud->fc->iq;
Miklos Szeredif88996a2015-07-01 16:26:01 +02002084 poll_wait(file, &fiq->waitq, wait);
Miklos Szeredi334f4852005-09-09 13:10:27 -07002085
Eric Biggers76e43c82019-09-08 20:15:18 -07002086 spin_lock(&fiq->lock);
Miklos Szeredie16714d2015-07-01 16:26:01 +02002087 if (!fiq->connected)
Linus Torvaldsa9a08842018-02-11 14:34:03 -08002088 mask = EPOLLERR;
Miklos Szeredif88996a2015-07-01 16:26:01 +02002089 else if (request_pending(fiq))
Linus Torvaldsa9a08842018-02-11 14:34:03 -08002090 mask |= EPOLLIN | EPOLLRDNORM;
Eric Biggers76e43c82019-09-08 20:15:18 -07002091 spin_unlock(&fiq->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -07002092
2093 return mask;
2094}
2095
Kirill Tkhai34061752018-11-06 12:15:20 +03002096/* Abort all requests on the given list (pending or processing) */
Miklos Szeredi334f4852005-09-09 13:10:27 -07002097static void end_requests(struct fuse_conn *fc, struct list_head *head)
2098{
2099 while (!list_empty(head)) {
2100 struct fuse_req *req;
2101 req = list_entry(head->next, struct fuse_req, list);
Miklos Szeredi334f4852005-09-09 13:10:27 -07002102 req->out.h.error = -ECONNABORTED;
Miklos Szeredi33e14b42015-07-01 16:26:01 +02002103 clear_bit(FR_SENT, &req->flags);
Miklos Szeredif377cb72015-07-01 16:26:04 +02002104 list_del_init(&req->list);
Miklos Szeredi334f4852005-09-09 13:10:27 -07002105 request_end(fc, req);
Miklos Szeredi334f4852005-09-09 13:10:27 -07002106 }
2107}
2108
Bryan Green357ccf22011-03-01 16:43:52 -08002109static void end_polls(struct fuse_conn *fc)
2110{
2111 struct rb_node *p;
2112
2113 p = rb_first(&fc->polled_files);
2114
2115 while (p) {
2116 struct fuse_file *ff;
2117 ff = rb_entry(p, struct fuse_file, polled_node);
2118 wake_up_interruptible_all(&ff->poll_wait);
2119
2120 p = rb_next(p);
2121 }
2122}
2123
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002124/*
2125 * Abort all requests.
2126 *
Miklos Szeredib716d422015-07-01 16:25:59 +02002127 * Emergency exit in case of a malicious or accidental deadlock, or just a hung
2128 * filesystem.
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002129 *
Miklos Szeredib716d422015-07-01 16:25:59 +02002130 * The same effect is usually achievable through killing the filesystem daemon
2131 * and all users of the filesystem. The exception is the combination of an
2132 * asynchronous request and the tricky deadlock (see
2133 * Documentation/filesystems/fuse.txt).
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002134 *
Miklos Szeredib716d422015-07-01 16:25:59 +02002135 * Aborting requests under I/O goes as follows: 1: Separate out unlocked
2136 * requests, they should be finished off immediately. Locked requests will be
2137 * finished after unlock; see unlock_request(). 2: Finish off the unlocked
2138 * requests. It is possible that some request will finish before we can. This
2139 * is OK, the request will in that case be removed from the list before we touch
2140 * it.
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002141 */
Miklos Szeredieb98e3bd2019-01-24 10:40:16 +01002142void fuse_abort_conn(struct fuse_conn *fc)
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002143{
Miklos Szeredif88996a2015-07-01 16:26:01 +02002144 struct fuse_iqueue *fiq = &fc->iq;
2145
Miklos Szeredid7133112006-04-10 22:54:55 -07002146 spin_lock(&fc->lock);
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002147 if (fc->connected) {
Miklos Szeredic36960462015-07-01 16:26:09 +02002148 struct fuse_dev *fud;
Miklos Szeredib716d422015-07-01 16:25:59 +02002149 struct fuse_req *req, *next;
Miklos Szeredi75f3ee42018-07-26 16:13:12 +02002150 LIST_HEAD(to_end);
Kirill Tkhaibe2ff422018-09-11 13:12:14 +03002151 unsigned int i;
Miklos Szeredib716d422015-07-01 16:25:59 +02002152
Kirill Tkhai63825b42018-08-27 18:29:56 +03002153 /* Background queuing checks fc->connected under bg_lock */
2154 spin_lock(&fc->bg_lock);
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002155 fc->connected = 0;
Kirill Tkhai63825b42018-08-27 18:29:56 +03002156 spin_unlock(&fc->bg_lock);
2157
Miklos Szeredi9759bd512015-01-06 10:45:35 +01002158 fuse_set_initialized(fc);
Miklos Szeredic36960462015-07-01 16:26:09 +02002159 list_for_each_entry(fud, &fc->devices, entry) {
2160 struct fuse_pqueue *fpq = &fud->pq;
2161
2162 spin_lock(&fpq->lock);
2163 fpq->connected = 0;
2164 list_for_each_entry_safe(req, next, &fpq->io, list) {
2165 req->out.h.error = -ECONNABORTED;
2166 spin_lock(&req->waitq.lock);
2167 set_bit(FR_ABORTED, &req->flags);
2168 if (!test_bit(FR_LOCKED, &req->flags)) {
2169 set_bit(FR_PRIVATE, &req->flags);
Miklos Szeredi87114372018-07-26 16:13:11 +02002170 __fuse_get_request(req);
Miklos Szeredi75f3ee42018-07-26 16:13:12 +02002171 list_move(&req->list, &to_end);
Miklos Szeredic36960462015-07-01 16:26:09 +02002172 }
2173 spin_unlock(&req->waitq.lock);
Miklos Szeredi77cd9d42015-07-01 16:26:06 +02002174 }
Kirill Tkhaibe2ff422018-09-11 13:12:14 +03002175 for (i = 0; i < FUSE_PQ_HASH_SIZE; i++)
2176 list_splice_tail_init(&fpq->processing[i],
2177 &to_end);
Miklos Szeredic36960462015-07-01 16:26:09 +02002178 spin_unlock(&fpq->lock);
Miklos Szeredib716d422015-07-01 16:25:59 +02002179 }
Kirill Tkhaiae2dffa2018-08-27 18:29:46 +03002180 spin_lock(&fc->bg_lock);
2181 fc->blocked = 0;
Miklos Szeredi41f98272015-07-01 16:25:59 +02002182 fc->max_background = UINT_MAX;
2183 flush_bg_queue(fc);
Kirill Tkhaiae2dffa2018-08-27 18:29:46 +03002184 spin_unlock(&fc->bg_lock);
Miklos Szeredi8c911892015-07-01 16:26:02 +02002185
Eric Biggers76e43c82019-09-08 20:15:18 -07002186 spin_lock(&fiq->lock);
Miklos Szeredi8c911892015-07-01 16:26:02 +02002187 fiq->connected = 0;
Miklos Szeredi75f3ee42018-07-26 16:13:12 +02002188 list_for_each_entry(req, &fiq->pending, list)
Tahsin Erdogana8a86d72017-01-12 12:04:04 -08002189 clear_bit(FR_PENDING, &req->flags);
Miklos Szeredi75f3ee42018-07-26 16:13:12 +02002190 list_splice_tail_init(&fiq->pending, &to_end);
Miklos Szeredi8c911892015-07-01 16:26:02 +02002191 while (forget_pending(fiq))
2192 kfree(dequeue_forget(fiq, 1, NULL));
Eric Biggers76e43c82019-09-08 20:15:18 -07002193 wake_up_all(&fiq->waitq);
2194 spin_unlock(&fiq->lock);
Miklos Szeredi8c911892015-07-01 16:26:02 +02002195 kill_fasync(&fiq->fasync, SIGIO, POLL_IN);
Miklos Szerediee314a82015-07-01 16:26:08 +02002196 end_polls(fc);
2197 wake_up_all(&fc->blocked_waitq);
2198 spin_unlock(&fc->lock);
Miklos Szeredi8c911892015-07-01 16:26:02 +02002199
Miklos Szeredi75f3ee42018-07-26 16:13:12 +02002200 end_requests(fc, &to_end);
Miklos Szerediee314a82015-07-01 16:26:08 +02002201 } else {
2202 spin_unlock(&fc->lock);
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002203 }
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002204}
Tejun Heo08cbf542009-04-14 10:54:53 +09002205EXPORT_SYMBOL_GPL(fuse_abort_conn);
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002206
Miklos Szeredib8f95e52018-07-26 16:13:11 +02002207void fuse_wait_aborted(struct fuse_conn *fc)
2208{
Miklos Szeredi2d84a2d2018-11-09 15:52:16 +01002209 /* matches implicit memory barrier in fuse_drop_waiting() */
2210 smp_mb();
Miklos Szeredib8f95e52018-07-26 16:13:11 +02002211 wait_event(fc->blocked_waitq, atomic_read(&fc->num_waiting) == 0);
2212}
2213
Tejun Heo08cbf542009-04-14 10:54:53 +09002214int fuse_dev_release(struct inode *inode, struct file *file)
Miklos Szeredi334f4852005-09-09 13:10:27 -07002215{
Miklos Szeredicc080e92015-07-01 16:26:08 +02002216 struct fuse_dev *fud = fuse_get_dev(file);
2217
2218 if (fud) {
2219 struct fuse_conn *fc = fud->fc;
Miklos Szeredic36960462015-07-01 16:26:09 +02002220 struct fuse_pqueue *fpq = &fud->pq;
Miklos Szeredi45ff3502018-07-26 16:13:11 +02002221 LIST_HEAD(to_end);
Kirill Tkhaibe2ff422018-09-11 13:12:14 +03002222 unsigned int i;
Miklos Szeredicc080e92015-07-01 16:26:08 +02002223
Miklos Szeredi45ff3502018-07-26 16:13:11 +02002224 spin_lock(&fpq->lock);
Miklos Szeredic36960462015-07-01 16:26:09 +02002225 WARN_ON(!list_empty(&fpq->io));
Kirill Tkhaibe2ff422018-09-11 13:12:14 +03002226 for (i = 0; i < FUSE_PQ_HASH_SIZE; i++)
2227 list_splice_init(&fpq->processing[i], &to_end);
Miklos Szeredi45ff3502018-07-26 16:13:11 +02002228 spin_unlock(&fpq->lock);
2229
2230 end_requests(fc, &to_end);
2231
Miklos Szeredic36960462015-07-01 16:26:09 +02002232 /* Are we the last open device? */
2233 if (atomic_dec_and_test(&fc->dev_count)) {
2234 WARN_ON(fc->iq.fasync != NULL);
Miklos Szeredieb98e3bd2019-01-24 10:40:16 +01002235 fuse_abort_conn(fc);
Miklos Szeredic36960462015-07-01 16:26:09 +02002236 }
Miklos Szeredicc080e92015-07-01 16:26:08 +02002237 fuse_dev_free(fud);
Jeff Dike385a17b2006-04-10 22:54:52 -07002238 }
Miklos Szeredi334f4852005-09-09 13:10:27 -07002239 return 0;
2240}
Tejun Heo08cbf542009-04-14 10:54:53 +09002241EXPORT_SYMBOL_GPL(fuse_dev_release);
Miklos Szeredi334f4852005-09-09 13:10:27 -07002242
Jeff Dike385a17b2006-04-10 22:54:52 -07002243static int fuse_dev_fasync(int fd, struct file *file, int on)
2244{
Miklos Szeredicc080e92015-07-01 16:26:08 +02002245 struct fuse_dev *fud = fuse_get_dev(file);
2246
2247 if (!fud)
Miklos Szeredia87046d2006-04-10 22:54:56 -07002248 return -EPERM;
Jeff Dike385a17b2006-04-10 22:54:52 -07002249
2250 /* No locking - fasync_helper does its own locking */
Miklos Szeredicc080e92015-07-01 16:26:08 +02002251 return fasync_helper(fd, file, on, &fud->fc->iq.fasync);
Jeff Dike385a17b2006-04-10 22:54:52 -07002252}
2253
Miklos Szeredi00c570f2015-07-01 16:26:08 +02002254static int fuse_device_clone(struct fuse_conn *fc, struct file *new)
2255{
Miklos Szeredicc080e92015-07-01 16:26:08 +02002256 struct fuse_dev *fud;
2257
Miklos Szeredi00c570f2015-07-01 16:26:08 +02002258 if (new->private_data)
2259 return -EINVAL;
2260
Miklos Szeredicc080e92015-07-01 16:26:08 +02002261 fud = fuse_dev_alloc(fc);
2262 if (!fud)
2263 return -ENOMEM;
2264
2265 new->private_data = fud;
Miklos Szeredic36960462015-07-01 16:26:09 +02002266 atomic_inc(&fc->dev_count);
Miklos Szeredi00c570f2015-07-01 16:26:08 +02002267
2268 return 0;
2269}
2270
2271static long fuse_dev_ioctl(struct file *file, unsigned int cmd,
2272 unsigned long arg)
2273{
2274 int err = -ENOTTY;
2275
2276 if (cmd == FUSE_DEV_IOC_CLONE) {
2277 int oldfd;
2278
2279 err = -EFAULT;
2280 if (!get_user(oldfd, (__u32 __user *) arg)) {
2281 struct file *old = fget(oldfd);
2282
2283 err = -EINVAL;
2284 if (old) {
Jann Horn8ed1f0e2015-08-16 20:27:01 +02002285 struct fuse_dev *fud = NULL;
2286
2287 /*
2288 * Check against file->f_op because CUSE
2289 * uses the same ioctl handler.
2290 */
2291 if (old->f_op == file->f_op &&
2292 old->f_cred->user_ns == file->f_cred->user_ns)
2293 fud = fuse_get_dev(old);
Miklos Szeredi00c570f2015-07-01 16:26:08 +02002294
Miklos Szeredicc080e92015-07-01 16:26:08 +02002295 if (fud) {
Miklos Szeredi00c570f2015-07-01 16:26:08 +02002296 mutex_lock(&fuse_mutex);
Miklos Szeredicc080e92015-07-01 16:26:08 +02002297 err = fuse_device_clone(fud->fc, file);
Miklos Szeredi00c570f2015-07-01 16:26:08 +02002298 mutex_unlock(&fuse_mutex);
2299 }
2300 fput(old);
2301 }
2302 }
2303 }
2304 return err;
2305}
2306
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -08002307const struct file_operations fuse_dev_operations = {
Miklos Szeredi334f4852005-09-09 13:10:27 -07002308 .owner = THIS_MODULE,
Tom Van Braeckel94e4fe22015-01-12 05:22:16 +01002309 .open = fuse_dev_open,
Miklos Szeredi334f4852005-09-09 13:10:27 -07002310 .llseek = no_llseek,
Al Virofbdbacc2015-04-03 21:53:39 -04002311 .read_iter = fuse_dev_read,
Miklos Szeredic3021622010-05-25 15:06:07 +02002312 .splice_read = fuse_dev_splice_read,
Al Virofbdbacc2015-04-03 21:53:39 -04002313 .write_iter = fuse_dev_write,
Miklos Szeredidd3bb142010-05-25 15:06:06 +02002314 .splice_write = fuse_dev_splice_write,
Miklos Szeredi334f4852005-09-09 13:10:27 -07002315 .poll = fuse_dev_poll,
2316 .release = fuse_dev_release,
Jeff Dike385a17b2006-04-10 22:54:52 -07002317 .fasync = fuse_dev_fasync,
Miklos Szeredi00c570f2015-07-01 16:26:08 +02002318 .unlocked_ioctl = fuse_dev_ioctl,
2319 .compat_ioctl = fuse_dev_ioctl,
Miklos Szeredi334f4852005-09-09 13:10:27 -07002320};
Tejun Heo08cbf542009-04-14 10:54:53 +09002321EXPORT_SYMBOL_GPL(fuse_dev_operations);
Miklos Szeredi334f4852005-09-09 13:10:27 -07002322
2323static struct miscdevice fuse_miscdevice = {
2324 .minor = FUSE_MINOR,
2325 .name = "fuse",
2326 .fops = &fuse_dev_operations,
2327};
2328
2329int __init fuse_dev_init(void)
2330{
2331 int err = -ENOMEM;
2332 fuse_req_cachep = kmem_cache_create("fuse_request",
2333 sizeof(struct fuse_req),
Paul Mundt20c2df82007-07-20 10:11:58 +09002334 0, 0, NULL);
Miklos Szeredi334f4852005-09-09 13:10:27 -07002335 if (!fuse_req_cachep)
2336 goto out;
2337
2338 err = misc_register(&fuse_miscdevice);
2339 if (err)
2340 goto out_cache_clean;
2341
2342 return 0;
2343
2344 out_cache_clean:
2345 kmem_cache_destroy(fuse_req_cachep);
2346 out:
2347 return err;
2348}
2349
2350void fuse_dev_cleanup(void)
2351{
2352 misc_deregister(&fuse_miscdevice);
2353 kmem_cache_destroy(fuse_req_cachep);
2354}