blob: 423b6c657bf07a82d11aa92122e863d974936a51 [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
Miklos Szeredi72133942019-09-10 15:04:11 +020043static void fuse_request_init(struct fuse_req *req)
Miklos Szeredi334f4852005-09-09 13:10:27 -070044{
Miklos Szeredi334f4852005-09-09 13:10:27 -070045 INIT_LIST_HEAD(&req->list);
Miklos Szeredia4d27e72006-06-25 05:48:54 -070046 INIT_LIST_HEAD(&req->intr_entry);
Miklos Szeredi334f4852005-09-09 13:10:27 -070047 init_waitqueue_head(&req->waitq);
Elena Reshetovaec99f6d2017-03-03 11:04:04 +020048 refcount_set(&req->count, 1);
Miklos Szeredi33e14b42015-07-01 16:26:01 +020049 __set_bit(FR_PENDING, &req->flags);
Miklos Szeredi334f4852005-09-09 13:10:27 -070050}
51
Miklos Szeredi72133942019-09-10 15:04:11 +020052static struct fuse_req *fuse_request_alloc(gfp_t flags)
Miklos Szeredi334f4852005-09-09 13:10:27 -070053{
Miklos Szeredi8a7aa282018-10-01 10:07:05 +020054 struct fuse_req *req = kmem_cache_zalloc(fuse_req_cachep, flags);
Miklos Szeredi72133942019-09-10 15:04:11 +020055 if (req)
56 fuse_request_init(req);
Maxim Patlasov4250c062012-10-26 19:48:07 +040057
Miklos Szeredi334f4852005-09-09 13:10:27 -070058 return req;
59}
Maxim Patlasov4250c062012-10-26 19:48:07 +040060
Miklos Szeredi66abc352019-09-10 15:04:11 +020061static void fuse_request_free(struct fuse_req *req)
Miklos Szeredie52a82502018-10-01 10:07:06 +020062{
Miklos Szeredi334f4852005-09-09 13:10:27 -070063 kmem_cache_free(fuse_req_cachep, req);
64}
65
Miklos Szeredi66abc352019-09-10 15:04:11 +020066static void __fuse_get_request(struct fuse_req *req)
Miklos Szeredi334f4852005-09-09 13:10:27 -070067{
Elena Reshetovaec99f6d2017-03-03 11:04:04 +020068 refcount_inc(&req->count);
Miklos Szeredi334f4852005-09-09 13:10:27 -070069}
70
71/* Must be called with > 1 refcount */
72static void __fuse_put_request(struct fuse_req *req)
73{
Elena Reshetovaec99f6d2017-03-03 11:04:04 +020074 refcount_dec(&req->count);
Miklos Szeredi334f4852005-09-09 13:10:27 -070075}
76
Miklos Szeredi9759bd512015-01-06 10:45:35 +010077void fuse_set_initialized(struct fuse_conn *fc)
78{
79 /* Make sure stores before this are seen on another CPU */
80 smp_wmb();
81 fc->initialized = 1;
82}
83
Maxim Patlasov0aada882013-03-21 18:02:28 +040084static bool fuse_block_alloc(struct fuse_conn *fc, bool for_background)
85{
86 return !fc->initialized || (for_background && fc->blocked);
87}
88
Miklos Szeredib8f95e52018-07-26 16:13:11 +020089static void fuse_drop_waiting(struct fuse_conn *fc)
90{
Miklos Szeredi2d84a2d2018-11-09 15:52:16 +010091 /*
92 * lockess check of fc->connected is okay, because atomic_dec_and_test()
93 * provides a memory barrier mached with the one in fuse_wait_aborted()
94 * to ensure no wake-up is missed.
95 */
96 if (atomic_dec_and_test(&fc->num_waiting) &&
97 !READ_ONCE(fc->connected)) {
Miklos Szeredib8f95e52018-07-26 16:13:11 +020098 /* wake up aborters */
99 wake_up_all(&fc->blocked_waitq);
100 }
101}
102
Miklos Szeredi66abc352019-09-10 15:04:11 +0200103static void fuse_put_request(struct fuse_conn *fc, struct fuse_req *req);
104
Miklos Szeredi72133942019-09-10 15:04:11 +0200105static struct fuse_req *fuse_get_req(struct fuse_conn *fc, bool for_background)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700106{
Miklos Szeredi08a53cd2006-04-10 22:54:59 -0700107 struct fuse_req *req;
Miklos Szeredi08a53cd2006-04-10 22:54:59 -0700108 int err;
Miklos Szeredi9bc5ddd2006-04-11 21:16:09 +0200109 atomic_inc(&fc->num_waiting);
Maxim Patlasov0aada882013-03-21 18:02:28 +0400110
111 if (fuse_block_alloc(fc, for_background)) {
Maxim Patlasov0aada882013-03-21 18:02:28 +0400112 err = -EINTR;
Al Viro7d3a07f2016-07-19 03:08:27 -0400113 if (wait_event_killable_exclusive(fc->blocked_waitq,
114 !fuse_block_alloc(fc, for_background)))
Maxim Patlasov0aada882013-03-21 18:02:28 +0400115 goto out;
116 }
Miklos Szeredi9759bd512015-01-06 10:45:35 +0100117 /* Matches smp_wmb() in fuse_set_initialized() */
118 smp_rmb();
Miklos Szeredi08a53cd2006-04-10 22:54:59 -0700119
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700120 err = -ENOTCONN;
121 if (!fc->connected)
122 goto out;
123
Miklos Szeredide155222015-07-01 16:25:57 +0200124 err = -ECONNREFUSED;
125 if (fc->conn_error)
126 goto out;
127
Miklos Szeredi72133942019-09-10 15:04:11 +0200128 req = fuse_request_alloc(GFP_KERNEL);
Miklos Szeredi9bc5ddd2006-04-11 21:16:09 +0200129 err = -ENOMEM;
Maxim Patlasov722d2be2013-03-21 18:02:36 +0400130 if (!req) {
131 if (for_background)
132 wake_up(&fc->blocked_waitq);
Miklos Szeredi9bc5ddd2006-04-11 21:16:09 +0200133 goto out;
Maxim Patlasov722d2be2013-03-21 18:02:36 +0400134 }
Miklos Szeredi334f4852005-09-09 13:10:27 -0700135
Eric W. Biederman8cb08322018-02-21 11:18:07 -0600136 req->in.h.uid = from_kuid(fc->user_ns, current_fsuid());
137 req->in.h.gid = from_kgid(fc->user_ns, current_fsgid());
Eric W. Biedermanc9582eb2018-02-21 10:52:06 -0600138 req->in.h.pid = pid_nr_ns(task_pid(current), fc->pid_ns);
139
Miklos Szeredi825d6d32015-07-01 16:25:58 +0200140 __set_bit(FR_WAITING, &req->flags);
141 if (for_background)
142 __set_bit(FR_BACKGROUND, &req->flags);
143
Eric W. Biedermanc9582eb2018-02-21 10:52:06 -0600144 if (unlikely(req->in.h.uid == ((uid_t)-1) ||
145 req->in.h.gid == ((gid_t)-1))) {
146 fuse_put_request(fc, req);
147 return ERR_PTR(-EOVERFLOW);
148 }
Miklos Szeredi334f4852005-09-09 13:10:27 -0700149 return req;
Miklos Szeredi9bc5ddd2006-04-11 21:16:09 +0200150
151 out:
Miklos Szeredib8f95e52018-07-26 16:13:11 +0200152 fuse_drop_waiting(fc);
Miklos Szeredi9bc5ddd2006-04-11 21:16:09 +0200153 return ERR_PTR(err);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700154}
Maxim Patlasov8b41e672013-03-21 18:02:04 +0400155
Miklos Szeredi66abc352019-09-10 15:04:11 +0200156static void fuse_put_request(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700157{
Elena Reshetovaec99f6d2017-03-03 11:04:04 +0200158 if (refcount_dec_and_test(&req->count)) {
Miklos Szeredi825d6d32015-07-01 16:25:58 +0200159 if (test_bit(FR_BACKGROUND, &req->flags)) {
Maxim Patlasov722d2be2013-03-21 18:02:36 +0400160 /*
161 * We get here in the unlikely case that a background
162 * request was allocated but not sent
163 */
Kirill Tkhaiae2dffa2018-08-27 18:29:46 +0300164 spin_lock(&fc->bg_lock);
Maxim Patlasov722d2be2013-03-21 18:02:36 +0400165 if (!fc->blocked)
166 wake_up(&fc->blocked_waitq);
Kirill Tkhaiae2dffa2018-08-27 18:29:46 +0300167 spin_unlock(&fc->bg_lock);
Maxim Patlasov722d2be2013-03-21 18:02:36 +0400168 }
169
Miklos Szeredi825d6d32015-07-01 16:25:58 +0200170 if (test_bit(FR_WAITING, &req->flags)) {
171 __clear_bit(FR_WAITING, &req->flags);
Miklos Szeredib8f95e52018-07-26 16:13:11 +0200172 fuse_drop_waiting(fc);
Miklos Szeredi73e0e732015-07-01 16:25:56 +0200173 }
Miklos Szeredi33649c92006-06-25 05:48:52 -0700174
Miklos Szeredi40ac7ab2019-09-10 15:04:08 +0200175 fuse_request_free(req);
Miklos Szeredi7128ec22006-02-04 23:27:40 -0800176 }
177}
178
Stefan Hajnoczi14d46d72018-06-21 09:34:25 +0100179unsigned int fuse_len_args(unsigned int numargs, struct fuse_arg *args)
Miklos Szeredid12def12008-02-06 01:38:39 -0800180{
181 unsigned nbytes = 0;
182 unsigned i;
183
184 for (i = 0; i < numargs; i++)
185 nbytes += args[i].size;
186
187 return nbytes;
188}
Stefan Hajnoczi14d46d72018-06-21 09:34:25 +0100189EXPORT_SYMBOL_GPL(fuse_len_args);
Miklos Szeredid12def12008-02-06 01:38:39 -0800190
Stefan Hajnoczi79d96ef2018-06-22 13:48:30 +0100191u64 fuse_get_unique(struct fuse_iqueue *fiq)
Miklos Szeredid12def12008-02-06 01:38:39 -0800192{
Kirill Tkhaic59fd852018-09-11 13:11:56 +0300193 fiq->reqctr += FUSE_REQ_ID_STEP;
194 return fiq->reqctr;
Miklos Szeredid12def12008-02-06 01:38:39 -0800195}
Stefan Hajnoczi79d96ef2018-06-22 13:48:30 +0100196EXPORT_SYMBOL_GPL(fuse_get_unique);
Miklos Szeredid12def12008-02-06 01:38:39 -0800197
Kirill Tkhaibe2ff422018-09-11 13:12:14 +0300198static unsigned int fuse_req_hash(u64 unique)
199{
200 return hash_long(unique & ~FUSE_INT_REQ_BIT, FUSE_PQ_HASH_BITS);
201}
202
Stefan Hajnocziae3aad72018-06-18 15:53:19 +0100203/**
204 * A new request is available, wake fiq->waitq
205 */
206static void fuse_dev_wake_and_unlock(struct fuse_iqueue *fiq)
207__releases(fiq->lock)
208{
209 wake_up(&fiq->waitq);
210 kill_fasync(&fiq->fasync, SIGIO, POLL_IN);
211 spin_unlock(&fiq->lock);
212}
213
214const struct fuse_iqueue_ops fuse_dev_fiq_ops = {
215 .wake_forget_and_unlock = fuse_dev_wake_and_unlock,
216 .wake_interrupt_and_unlock = fuse_dev_wake_and_unlock,
217 .wake_pending_and_unlock = fuse_dev_wake_and_unlock,
218};
219EXPORT_SYMBOL_GPL(fuse_dev_fiq_ops);
220
221static void queue_request_and_unlock(struct fuse_iqueue *fiq,
222 struct fuse_req *req)
223__releases(fiq->lock)
Miklos Szeredid12def12008-02-06 01:38:39 -0800224{
Miklos Szeredid12def12008-02-06 01:38:39 -0800225 req->in.h.len = sizeof(struct fuse_in_header) +
Stefan Hajnoczi14d46d72018-06-21 09:34:25 +0100226 fuse_len_args(req->args->in_numargs,
227 (struct fuse_arg *) req->args->in_args);
Miklos Szeredif88996a2015-07-01 16:26:01 +0200228 list_add_tail(&req->list, &fiq->pending);
Stefan Hajnocziae3aad72018-06-18 15:53:19 +0100229 fiq->ops->wake_pending_and_unlock(fiq);
Miklos Szeredid12def12008-02-06 01:38:39 -0800230}
231
Miklos Szeredi07e77dc2010-12-07 20:16:56 +0100232void fuse_queue_forget(struct fuse_conn *fc, struct fuse_forget_link *forget,
233 u64 nodeid, u64 nlookup)
234{
Miklos Szeredif88996a2015-07-01 16:26:01 +0200235 struct fuse_iqueue *fiq = &fc->iq;
236
Miklos Szeredi02c048b2010-12-07 20:16:56 +0100237 forget->forget_one.nodeid = nodeid;
238 forget->forget_one.nlookup = nlookup;
Miklos Szeredi07e77dc2010-12-07 20:16:56 +0100239
Eric Biggers76e43c82019-09-08 20:15:18 -0700240 spin_lock(&fiq->lock);
Miklos Szeredie16714d2015-07-01 16:26:01 +0200241 if (fiq->connected) {
Miklos Szeredif88996a2015-07-01 16:26:01 +0200242 fiq->forget_list_tail->next = forget;
243 fiq->forget_list_tail = forget;
Stefan Hajnocziae3aad72018-06-18 15:53:19 +0100244 fiq->ops->wake_forget_and_unlock(fiq);
Miklos Szeredi5dfcc872011-09-12 09:38:03 +0200245 } else {
246 kfree(forget);
Stefan Hajnocziae3aad72018-06-18 15:53:19 +0100247 spin_unlock(&fiq->lock);
Miklos Szeredi5dfcc872011-09-12 09:38:03 +0200248 }
Miklos Szeredi07e77dc2010-12-07 20:16:56 +0100249}
250
Miklos Szeredid12def12008-02-06 01:38:39 -0800251static void flush_bg_queue(struct fuse_conn *fc)
252{
Kirill Tkhaie2871792018-07-31 13:25:25 +0300253 struct fuse_iqueue *fiq = &fc->iq;
254
Csaba Henk7a6d3c82009-07-01 17:28:41 -0700255 while (fc->active_background < fc->max_background &&
Miklos Szeredid12def12008-02-06 01:38:39 -0800256 !list_empty(&fc->bg_queue)) {
257 struct fuse_req *req;
258
Kirill Tkhaie2871792018-07-31 13:25:25 +0300259 req = list_first_entry(&fc->bg_queue, struct fuse_req, list);
Miklos Szeredid12def12008-02-06 01:38:39 -0800260 list_del(&req->list);
261 fc->active_background++;
Eric Biggers76e43c82019-09-08 20:15:18 -0700262 spin_lock(&fiq->lock);
Miklos Szeredif88996a2015-07-01 16:26:01 +0200263 req->in.h.unique = fuse_get_unique(fiq);
Stefan Hajnocziae3aad72018-06-18 15:53:19 +0100264 queue_request_and_unlock(fiq, req);
Miklos Szeredid12def12008-02-06 01:38:39 -0800265 }
266}
267
Miklos Szeredi6dbbcb12006-04-26 10:49:06 +0200268/*
Miklos Szeredi334f4852005-09-09 13:10:27 -0700269 * This function is called when a request is finished. Either a reply
Miklos Szeredif9a28422006-06-25 05:48:53 -0700270 * has arrived or it was aborted (and not yet sent) or some error
Miklos Szeredif43b1552006-01-16 22:14:26 -0800271 * occurred during communication with userspace, or the device file
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700272 * was closed. The requester thread is woken up (if still waiting),
273 * the 'end' callback is called if given, else the reference to the
274 * request is released
Miklos Szeredi334f4852005-09-09 13:10:27 -0700275 */
Stefan Hajnoczi04ec5af2018-06-21 09:33:40 +0100276void fuse_request_end(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700277{
Miklos Szeredi4ce60812015-07-01 16:26:02 +0200278 struct fuse_iqueue *fiq = &fc->iq;
Miklos Szeredid4993772019-09-10 15:04:11 +0200279 bool async = req->args->end;
Miklos Szeredi365ae712015-07-01 16:26:06 +0200280
Miklos Szerediefe28002015-07-01 16:26:07 +0200281 if (test_and_set_bit(FR_FINISHED, &req->flags))
Miklos Szeredib8f95e52018-07-26 16:13:11 +0200282 goto put_request;
Kirill Tkhai217316a2018-11-08 12:05:25 +0300283 /*
284 * test_and_set_bit() implies smp_mb() between bit
285 * changing and below intr_entry check. Pairs with
286 * smp_mb() from queue_interrupt().
287 */
288 if (!list_empty(&req->intr_entry)) {
Eric Biggers76e43c82019-09-08 20:15:18 -0700289 spin_lock(&fiq->lock);
Kirill Tkhai217316a2018-11-08 12:05:25 +0300290 list_del_init(&req->intr_entry);
Eric Biggers76e43c82019-09-08 20:15:18 -0700291 spin_unlock(&fiq->lock);
Kirill Tkhai217316a2018-11-08 12:05:25 +0300292 }
Miklos Szeredi33e14b42015-07-01 16:26:01 +0200293 WARN_ON(test_bit(FR_PENDING, &req->flags));
294 WARN_ON(test_bit(FR_SENT, &req->flags));
Miklos Szeredi825d6d32015-07-01 16:25:58 +0200295 if (test_bit(FR_BACKGROUND, &req->flags)) {
Kirill Tkhaiae2dffa2018-08-27 18:29:46 +0300296 spin_lock(&fc->bg_lock);
Miklos Szeredi825d6d32015-07-01 16:25:58 +0200297 clear_bit(FR_BACKGROUND, &req->flags);
Miklos Szeredi908a5722018-09-28 16:43:22 +0200298 if (fc->num_background == fc->max_background) {
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700299 fc->blocked = 0;
Maxim Patlasov722d2be2013-03-21 18:02:36 +0400300 wake_up(&fc->blocked_waitq);
Miklos Szeredi908a5722018-09-28 16:43:22 +0200301 } else if (!fc->blocked) {
302 /*
303 * Wake up next waiter, if any. It's okay to use
304 * waitqueue_active(), as we've already synced up
305 * fc->blocked with waiters with the wake_up() call
306 * above.
307 */
308 if (waitqueue_active(&fc->blocked_waitq))
309 wake_up(&fc->blocked_waitq);
310 }
Maxim Patlasov722d2be2013-03-21 18:02:36 +0400311
Tejun Heo8a301eb2018-02-02 09:54:14 -0800312 if (fc->num_background == fc->congestion_threshold && fc->sb) {
Jan Kara5f7f7542017-04-12 12:24:40 +0200313 clear_bdi_congested(fc->sb->s_bdi, BLK_RW_SYNC);
314 clear_bdi_congested(fc->sb->s_bdi, BLK_RW_ASYNC);
Miklos Szeredif92b99b2007-10-16 23:30:59 -0700315 }
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700316 fc->num_background--;
Miklos Szeredid12def12008-02-06 01:38:39 -0800317 fc->active_background--;
318 flush_bg_queue(fc);
Kirill Tkhaiae2dffa2018-08-27 18:29:46 +0300319 spin_unlock(&fc->bg_lock);
Kirill Tkhai5e0fed72018-11-08 12:05:31 +0300320 } else {
321 /* Wake up waiter sleeping in request_wait_answer() */
322 wake_up(&req->waitq);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700323 }
Kirill Tkhai5e0fed72018-11-08 12:05:31 +0300324
Miklos Szeredid4993772019-09-10 15:04:11 +0200325 if (async)
326 req->args->end(fc, req->args, req->out.h.error);
Miklos Szeredib8f95e52018-07-26 16:13:11 +0200327put_request:
Tejun Heoe9bb09d2008-11-26 12:03:54 +0100328 fuse_put_request(fc, req);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700329}
Stefan Hajnoczi04ec5af2018-06-21 09:33:40 +0100330EXPORT_SYMBOL_GPL(fuse_request_end);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700331
Kirill Tkhaib7829112018-11-08 12:05:42 +0300332static int queue_interrupt(struct fuse_iqueue *fiq, struct fuse_req *req)
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700333{
Eric Biggers76e43c82019-09-08 20:15:18 -0700334 spin_lock(&fiq->lock);
Kirill Tkhaib7829112018-11-08 12:05:42 +0300335 /* Check for we've sent request to interrupt this req */
336 if (unlikely(!test_bit(FR_INTERRUPTED, &req->flags))) {
Eric Biggers76e43c82019-09-08 20:15:18 -0700337 spin_unlock(&fiq->lock);
Kirill Tkhaib7829112018-11-08 12:05:42 +0300338 return -EINVAL;
339 }
340
Miklos Szeredi8f7bb362015-07-01 16:26:03 +0200341 if (list_empty(&req->intr_entry)) {
342 list_add_tail(&req->intr_entry, &fiq->interrupts);
Kirill Tkhai217316a2018-11-08 12:05:25 +0300343 /*
344 * Pairs with smp_mb() implied by test_and_set_bit()
345 * from request_end().
346 */
347 smp_mb();
348 if (test_bit(FR_FINISHED, &req->flags)) {
349 list_del_init(&req->intr_entry);
Eric Biggers76e43c82019-09-08 20:15:18 -0700350 spin_unlock(&fiq->lock);
Kirill Tkhaib7829112018-11-08 12:05:42 +0300351 return 0;
Kirill Tkhai217316a2018-11-08 12:05:25 +0300352 }
Stefan Hajnocziae3aad72018-06-18 15:53:19 +0100353 fiq->ops->wake_interrupt_and_unlock(fiq);
354 } else {
355 spin_unlock(&fiq->lock);
Miklos Szeredi8f7bb362015-07-01 16:26:03 +0200356 }
Kirill Tkhaib7829112018-11-08 12:05:42 +0300357 return 0;
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700358}
359
Miklos Szeredi7c352bd2005-09-09 13:10:39 -0700360static void request_wait_answer(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700361{
Miklos Szeredi4ce60812015-07-01 16:26:02 +0200362 struct fuse_iqueue *fiq = &fc->iq;
Miklos Szeredic4775262015-07-01 16:26:00 +0200363 int err;
364
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700365 if (!fc->no_interrupt) {
366 /* Any signal may interrupt this */
Miklos Szeredic4775262015-07-01 16:26:00 +0200367 err = wait_event_interruptible(req->waitq,
Miklos Szeredi33e14b42015-07-01 16:26:01 +0200368 test_bit(FR_FINISHED, &req->flags));
Miklos Szeredic4775262015-07-01 16:26:00 +0200369 if (!err)
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700370 return;
371
Miklos Szeredi825d6d32015-07-01 16:25:58 +0200372 set_bit(FR_INTERRUPTED, &req->flags);
Miklos Szeredi8f7bb362015-07-01 16:26:03 +0200373 /* matches barrier in fuse_dev_do_read() */
374 smp_mb__after_atomic();
Miklos Szeredi33e14b42015-07-01 16:26:01 +0200375 if (test_bit(FR_SENT, &req->flags))
Miklos Szeredi4ce60812015-07-01 16:26:02 +0200376 queue_interrupt(fiq, req);
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700377 }
378
Miklos Szeredi825d6d32015-07-01 16:25:58 +0200379 if (!test_bit(FR_FORCE, &req->flags)) {
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700380 /* Only fatal signals may interrupt this */
Al Viro7d3a07f2016-07-19 03:08:27 -0400381 err = wait_event_killable(req->waitq,
Miklos Szeredi33e14b42015-07-01 16:26:01 +0200382 test_bit(FR_FINISHED, &req->flags));
Miklos Szeredic4775262015-07-01 16:26:00 +0200383 if (!err)
Miklos Szeredia131de02007-10-16 23:31:04 -0700384 return;
385
Eric Biggers76e43c82019-09-08 20:15:18 -0700386 spin_lock(&fiq->lock);
Miklos Szeredia131de02007-10-16 23:31:04 -0700387 /* Request is not yet in userspace, bail out */
Miklos Szeredi33e14b42015-07-01 16:26:01 +0200388 if (test_bit(FR_PENDING, &req->flags)) {
Miklos Szeredia131de02007-10-16 23:31:04 -0700389 list_del(&req->list);
Eric Biggers76e43c82019-09-08 20:15:18 -0700390 spin_unlock(&fiq->lock);
Miklos Szeredia131de02007-10-16 23:31:04 -0700391 __fuse_put_request(req);
392 req->out.h.error = -EINTR;
393 return;
394 }
Eric Biggers76e43c82019-09-08 20:15:18 -0700395 spin_unlock(&fiq->lock);
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700396 }
Miklos Szeredi334f4852005-09-09 13:10:27 -0700397
Miklos Szeredia131de02007-10-16 23:31:04 -0700398 /*
399 * Either request is already in userspace, or it was forced.
400 * Wait it out.
401 */
Miklos Szeredi33e14b42015-07-01 16:26:01 +0200402 wait_event(req->waitq, test_bit(FR_FINISHED, &req->flags));
Miklos Szeredi334f4852005-09-09 13:10:27 -0700403}
404
Eric Wong6a4e9222013-02-04 13:04:44 +0000405static void __fuse_request_send(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700406{
Miklos Szeredie16714d2015-07-01 16:26:01 +0200407 struct fuse_iqueue *fiq = &fc->iq;
408
Miklos Szeredi825d6d32015-07-01 16:25:58 +0200409 BUG_ON(test_bit(FR_BACKGROUND, &req->flags));
Eric Biggers76e43c82019-09-08 20:15:18 -0700410 spin_lock(&fiq->lock);
Miklos Szeredie16714d2015-07-01 16:26:01 +0200411 if (!fiq->connected) {
Eric Biggers76e43c82019-09-08 20:15:18 -0700412 spin_unlock(&fiq->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700413 req->out.h.error = -ENOTCONN;
Miklos Szeredic4775262015-07-01 16:26:00 +0200414 } else {
Miklos Szeredif88996a2015-07-01 16:26:01 +0200415 req->in.h.unique = fuse_get_unique(fiq);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700416 /* acquire extra reference, since request is still needed
Stefan Hajnoczi04ec5af2018-06-21 09:33:40 +0100417 after fuse_request_end() */
Miklos Szeredi334f4852005-09-09 13:10:27 -0700418 __fuse_get_request(req);
Stefan Hajnocziae3aad72018-06-18 15:53:19 +0100419 queue_request_and_unlock(fiq, req);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700420
Miklos Szeredi7c352bd2005-09-09 13:10:39 -0700421 request_wait_answer(fc, req);
Stefan Hajnoczi04ec5af2018-06-21 09:33:40 +0100422 /* Pairs with smp_wmb() in fuse_request_end() */
Miklos Szeredic4775262015-07-01 16:26:00 +0200423 smp_rmb();
Miklos Szeredi334f4852005-09-09 13:10:27 -0700424 }
Miklos Szeredi334f4852005-09-09 13:10:27 -0700425}
Eric Wong6a4e9222013-02-04 13:04:44 +0000426
Miklos Szeredi21f62172015-01-06 10:45:35 +0100427static void fuse_adjust_compat(struct fuse_conn *fc, struct fuse_args *args)
428{
Miklos Szeredid5b48542019-09-10 15:04:08 +0200429 if (fc->minor < 4 && args->opcode == FUSE_STATFS)
430 args->out_args[0].size = FUSE_COMPAT_STATFS_SIZE;
Miklos Szeredi21f62172015-01-06 10:45:35 +0100431
432 if (fc->minor < 9) {
Miklos Szeredid5b48542019-09-10 15:04:08 +0200433 switch (args->opcode) {
Miklos Szeredi21f62172015-01-06 10:45:35 +0100434 case FUSE_LOOKUP:
435 case FUSE_CREATE:
436 case FUSE_MKNOD:
437 case FUSE_MKDIR:
438 case FUSE_SYMLINK:
439 case FUSE_LINK:
Miklos Szeredid5b48542019-09-10 15:04:08 +0200440 args->out_args[0].size = FUSE_COMPAT_ENTRY_OUT_SIZE;
Miklos Szeredi21f62172015-01-06 10:45:35 +0100441 break;
442 case FUSE_GETATTR:
443 case FUSE_SETATTR:
Miklos Szeredid5b48542019-09-10 15:04:08 +0200444 args->out_args[0].size = FUSE_COMPAT_ATTR_OUT_SIZE;
Miklos Szeredi21f62172015-01-06 10:45:35 +0100445 break;
446 }
447 }
448 if (fc->minor < 12) {
Miklos Szeredid5b48542019-09-10 15:04:08 +0200449 switch (args->opcode) {
Miklos Szeredi21f62172015-01-06 10:45:35 +0100450 case FUSE_CREATE:
Miklos Szeredid5b48542019-09-10 15:04:08 +0200451 args->in_args[0].size = sizeof(struct fuse_open_in);
Miklos Szeredi21f62172015-01-06 10:45:35 +0100452 break;
453 case FUSE_MKNOD:
Miklos Szeredid5b48542019-09-10 15:04:08 +0200454 args->in_args[0].size = FUSE_COMPAT_MKNOD_IN_SIZE;
Miklos Szeredi21f62172015-01-06 10:45:35 +0100455 break;
456 }
457 }
458}
459
Miklos Szeredie4137542019-09-10 15:04:08 +0200460static void fuse_force_creds(struct fuse_conn *fc, struct fuse_req *req)
461{
462 req->in.h.uid = from_kuid_munged(fc->user_ns, current_fsuid());
463 req->in.h.gid = from_kgid_munged(fc->user_ns, current_fsgid());
464 req->in.h.pid = pid_nr_ns(task_pid(current), fc->pid_ns);
465}
466
YueHaibing5addcd52019-09-23 13:52:31 +0800467static void fuse_args_to_req(struct fuse_req *req, struct fuse_args *args)
Miklos Szeredi68583162019-09-10 15:04:09 +0200468{
Miklos Szeredi68583162019-09-10 15:04:09 +0200469 req->in.h.opcode = args->opcode;
470 req->in.h.nodeid = args->nodeid;
Miklos Szeredid4993772019-09-10 15:04:11 +0200471 req->args = args;
Miklos Szeredi68583162019-09-10 15:04:09 +0200472}
473
Miklos Szeredi70781872014-12-12 09:49:05 +0100474ssize_t fuse_simple_request(struct fuse_conn *fc, struct fuse_args *args)
475{
476 struct fuse_req *req;
477 ssize_t ret;
478
Miklos Szeredic500eba2019-09-10 15:04:08 +0200479 if (args->force) {
Miklos Szeredie4137542019-09-10 15:04:08 +0200480 atomic_inc(&fc->num_waiting);
Miklos Szeredi72133942019-09-10 15:04:11 +0200481 req = fuse_request_alloc(GFP_KERNEL | __GFP_NOFAIL);
Miklos Szeredie4137542019-09-10 15:04:08 +0200482
483 if (!args->nocreds)
484 fuse_force_creds(fc, req);
485
486 __set_bit(FR_WAITING, &req->flags);
Miklos Szeredic500eba2019-09-10 15:04:08 +0200487 __set_bit(FR_FORCE, &req->flags);
488 } else {
Miklos Szeredie4137542019-09-10 15:04:08 +0200489 WARN_ON(args->nocreds);
Miklos Szeredi72133942019-09-10 15:04:11 +0200490 req = fuse_get_req(fc, false);
Miklos Szeredic500eba2019-09-10 15:04:08 +0200491 if (IS_ERR(req))
492 return PTR_ERR(req);
493 }
Miklos Szeredi70781872014-12-12 09:49:05 +0100494
Miklos Szeredi21f62172015-01-06 10:45:35 +0100495 /* Needs to be done after fuse_get_req() so that fc->minor is valid */
496 fuse_adjust_compat(fc, args);
Miklos Szeredi68583162019-09-10 15:04:09 +0200497 fuse_args_to_req(req, args);
Miklos Szeredi21f62172015-01-06 10:45:35 +0100498
Miklos Szeredi454a7612019-09-10 15:04:08 +0200499 if (!args->noreply)
500 __set_bit(FR_ISREPLY, &req->flags);
501 __fuse_request_send(fc, req);
Miklos Szeredi70781872014-12-12 09:49:05 +0100502 ret = req->out.h.error;
Miklos Szeredid5b48542019-09-10 15:04:08 +0200503 if (!ret && args->out_argvar) {
Miklos Szeredi093f38a2019-09-10 15:04:09 +0200504 BUG_ON(args->out_numargs == 0);
Miklos Szeredid4993772019-09-10 15:04:11 +0200505 ret = args->out_args[args->out_numargs - 1].size;
Miklos Szeredi70781872014-12-12 09:49:05 +0100506 }
507 fuse_put_request(fc, req);
508
509 return ret;
510}
511
Miklos Szeredi66abc352019-09-10 15:04:11 +0200512static bool fuse_request_queue_background(struct fuse_conn *fc,
513 struct fuse_req *req)
Miklos Szeredid12def12008-02-06 01:38:39 -0800514{
Kirill Tkhai63825b42018-08-27 18:29:56 +0300515 bool queued = false;
516
517 WARN_ON(!test_bit(FR_BACKGROUND, &req->flags));
Miklos Szeredi825d6d32015-07-01 16:25:58 +0200518 if (!test_bit(FR_WAITING, &req->flags)) {
519 __set_bit(FR_WAITING, &req->flags);
Miklos Szeredi5437f242015-07-01 16:25:56 +0200520 atomic_inc(&fc->num_waiting);
521 }
Miklos Szeredi825d6d32015-07-01 16:25:58 +0200522 __set_bit(FR_ISREPLY, &req->flags);
Kirill Tkhaiae2dffa2018-08-27 18:29:46 +0300523 spin_lock(&fc->bg_lock);
Kirill Tkhai63825b42018-08-27 18:29:56 +0300524 if (likely(fc->connected)) {
525 fc->num_background++;
526 if (fc->num_background == fc->max_background)
527 fc->blocked = 1;
528 if (fc->num_background == fc->congestion_threshold && fc->sb) {
529 set_bdi_congested(fc->sb->s_bdi, BLK_RW_SYNC);
530 set_bdi_congested(fc->sb->s_bdi, BLK_RW_ASYNC);
531 }
532 list_add_tail(&req->list, &fc->bg_queue);
533 flush_bg_queue(fc);
534 queued = true;
Miklos Szeredid12def12008-02-06 01:38:39 -0800535 }
Kirill Tkhaiae2dffa2018-08-27 18:29:46 +0300536 spin_unlock(&fc->bg_lock);
Kirill Tkhai63825b42018-08-27 18:29:56 +0300537
538 return queued;
Miklos Szeredid12def12008-02-06 01:38:39 -0800539}
540
Miklos Szeredi12597282019-09-10 15:04:10 +0200541int fuse_simple_background(struct fuse_conn *fc, struct fuse_args *args,
542 gfp_t gfp_flags)
543{
544 struct fuse_req *req;
545
546 if (args->force) {
547 WARN_ON(!args->nocreds);
Miklos Szeredi72133942019-09-10 15:04:11 +0200548 req = fuse_request_alloc(gfp_flags);
Miklos Szeredi12597282019-09-10 15:04:10 +0200549 if (!req)
550 return -ENOMEM;
551 __set_bit(FR_BACKGROUND, &req->flags);
552 } else {
553 WARN_ON(args->nocreds);
Miklos Szeredi72133942019-09-10 15:04:11 +0200554 req = fuse_get_req(fc, true);
Miklos Szeredi12597282019-09-10 15:04:10 +0200555 if (IS_ERR(req))
556 return PTR_ERR(req);
557 }
558
559 fuse_args_to_req(req, args);
560
Miklos Szeredi12597282019-09-10 15:04:10 +0200561 if (!fuse_request_queue_background(fc, req)) {
562 fuse_put_request(fc, req);
563 return -ENOTCONN;
564 }
565
566 return 0;
567}
568EXPORT_SYMBOL_GPL(fuse_simple_background);
569
Miklos Szeredi75b399d2019-09-10 15:04:11 +0200570static int fuse_simple_notify_reply(struct fuse_conn *fc,
571 struct fuse_args *args, u64 unique)
Miklos Szeredi2d45ba32010-07-12 14:41:40 +0200572{
Miklos Szeredi75b399d2019-09-10 15:04:11 +0200573 struct fuse_req *req;
Miklos Szeredif88996a2015-07-01 16:26:01 +0200574 struct fuse_iqueue *fiq = &fc->iq;
Miklos Szeredi75b399d2019-09-10 15:04:11 +0200575 int err = 0;
576
Miklos Szeredi72133942019-09-10 15:04:11 +0200577 req = fuse_get_req(fc, false);
Miklos Szeredi75b399d2019-09-10 15:04:11 +0200578 if (IS_ERR(req))
579 return PTR_ERR(req);
Miklos Szeredi2d45ba32010-07-12 14:41:40 +0200580
Miklos Szeredi825d6d32015-07-01 16:25:58 +0200581 __clear_bit(FR_ISREPLY, &req->flags);
Miklos Szeredi2d45ba32010-07-12 14:41:40 +0200582 req->in.h.unique = unique;
Miklos Szeredi75b399d2019-09-10 15:04:11 +0200583
584 fuse_args_to_req(req, args);
Miklos Szeredi75b399d2019-09-10 15:04:11 +0200585
Eric Biggers76e43c82019-09-08 20:15:18 -0700586 spin_lock(&fiq->lock);
Miklos Szeredie16714d2015-07-01 16:26:01 +0200587 if (fiq->connected) {
Stefan Hajnocziae3aad72018-06-18 15:53:19 +0100588 queue_request_and_unlock(fiq, req);
Miklos Szeredi75b399d2019-09-10 15:04:11 +0200589 } else {
590 err = -ENODEV;
591 spin_unlock(&fiq->lock);
592 fuse_put_request(fc, req);
Miklos Szeredi2d45ba32010-07-12 14:41:40 +0200593 }
Miklos Szeredi2d45ba32010-07-12 14:41:40 +0200594
595 return err;
596}
597
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700598/*
Miklos Szeredi334f4852005-09-09 13:10:27 -0700599 * Lock the request. Up to the next unlock_request() there mustn't be
600 * anything that could cause a page-fault. If the request was already
Miklos Szeredif9a28422006-06-25 05:48:53 -0700601 * aborted bail out.
Miklos Szeredi334f4852005-09-09 13:10:27 -0700602 */
Miklos Szeredidc008092015-07-01 16:25:58 +0200603static int lock_request(struct fuse_req *req)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700604{
605 int err = 0;
606 if (req) {
Miklos Szeredidc008092015-07-01 16:25:58 +0200607 spin_lock(&req->waitq.lock);
Miklos Szeredi825d6d32015-07-01 16:25:58 +0200608 if (test_bit(FR_ABORTED, &req->flags))
Miklos Szeredi334f4852005-09-09 13:10:27 -0700609 err = -ENOENT;
610 else
Miklos Szeredi825d6d32015-07-01 16:25:58 +0200611 set_bit(FR_LOCKED, &req->flags);
Miklos Szeredidc008092015-07-01 16:25:58 +0200612 spin_unlock(&req->waitq.lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700613 }
614 return err;
615}
616
617/*
Miklos Szeredi0d8e84b2015-07-01 16:25:58 +0200618 * Unlock request. If it was aborted while locked, caller is responsible
619 * for unlocking and ending the request.
Miklos Szeredi334f4852005-09-09 13:10:27 -0700620 */
Miklos Szeredidc008092015-07-01 16:25:58 +0200621static int unlock_request(struct fuse_req *req)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700622{
Miklos Szeredi0d8e84b2015-07-01 16:25:58 +0200623 int err = 0;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700624 if (req) {
Miklos Szeredidc008092015-07-01 16:25:58 +0200625 spin_lock(&req->waitq.lock);
Miklos Szeredi825d6d32015-07-01 16:25:58 +0200626 if (test_bit(FR_ABORTED, &req->flags))
Miklos Szeredi0d8e84b2015-07-01 16:25:58 +0200627 err = -ENOENT;
628 else
Miklos Szeredi825d6d32015-07-01 16:25:58 +0200629 clear_bit(FR_LOCKED, &req->flags);
Miklos Szeredidc008092015-07-01 16:25:58 +0200630 spin_unlock(&req->waitq.lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700631 }
Miklos Szeredi0d8e84b2015-07-01 16:25:58 +0200632 return err;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700633}
634
635struct fuse_copy_state {
636 int write;
637 struct fuse_req *req;
Al Viro6c09e942015-04-03 22:06:08 -0400638 struct iov_iter *iter;
Miklos Szeredidd3bb142010-05-25 15:06:06 +0200639 struct pipe_buffer *pipebufs;
640 struct pipe_buffer *currbuf;
641 struct pipe_inode_info *pipe;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700642 unsigned long nr_segs;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700643 struct page *pg;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700644 unsigned len;
Miklos Szeredic55a01d2014-07-07 15:28:51 +0200645 unsigned offset;
Miklos Szeredice534fb2010-05-25 15:06:07 +0200646 unsigned move_pages:1;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700647};
648
Miklos Szeredidc008092015-07-01 16:25:58 +0200649static void fuse_copy_init(struct fuse_copy_state *cs, int write,
Al Viro6c09e942015-04-03 22:06:08 -0400650 struct iov_iter *iter)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700651{
652 memset(cs, 0, sizeof(*cs));
653 cs->write = write;
Al Viro6c09e942015-04-03 22:06:08 -0400654 cs->iter = iter;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700655}
656
657/* Unmap and put previous page of userspace buffer */
Miklos Szeredi8bfc0162006-01-16 22:14:28 -0800658static void fuse_copy_finish(struct fuse_copy_state *cs)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700659{
Miklos Szeredidd3bb142010-05-25 15:06:06 +0200660 if (cs->currbuf) {
661 struct pipe_buffer *buf = cs->currbuf;
662
Miklos Szeredic55a01d2014-07-07 15:28:51 +0200663 if (cs->write)
Miklos Szeredic3021622010-05-25 15:06:07 +0200664 buf->len = PAGE_SIZE - cs->len;
Miklos Szeredidd3bb142010-05-25 15:06:06 +0200665 cs->currbuf = NULL;
Miklos Szeredic55a01d2014-07-07 15:28:51 +0200666 } else if (cs->pg) {
Miklos Szeredi334f4852005-09-09 13:10:27 -0700667 if (cs->write) {
668 flush_dcache_page(cs->pg);
669 set_page_dirty_lock(cs->pg);
670 }
671 put_page(cs->pg);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700672 }
Miklos Szeredic55a01d2014-07-07 15:28:51 +0200673 cs->pg = NULL;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700674}
675
676/*
677 * Get another pagefull of userspace buffer, and map it to kernel
678 * address space, and lock request
679 */
680static int fuse_copy_fill(struct fuse_copy_state *cs)
681{
Miklos Szeredic55a01d2014-07-07 15:28:51 +0200682 struct page *page;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700683 int err;
684
Miklos Szeredidc008092015-07-01 16:25:58 +0200685 err = unlock_request(cs->req);
Miklos Szeredi0d8e84b2015-07-01 16:25:58 +0200686 if (err)
687 return err;
688
Miklos Szeredi334f4852005-09-09 13:10:27 -0700689 fuse_copy_finish(cs);
Miklos Szeredidd3bb142010-05-25 15:06:06 +0200690 if (cs->pipebufs) {
691 struct pipe_buffer *buf = cs->pipebufs;
692
Miklos Szeredic3021622010-05-25 15:06:07 +0200693 if (!cs->write) {
Miklos Szeredifba597d2016-09-27 10:45:12 +0200694 err = pipe_buf_confirm(cs->pipe, buf);
Miklos Szeredic3021622010-05-25 15:06:07 +0200695 if (err)
696 return err;
Miklos Szeredidd3bb142010-05-25 15:06:06 +0200697
Miklos Szeredic3021622010-05-25 15:06:07 +0200698 BUG_ON(!cs->nr_segs);
699 cs->currbuf = buf;
Miklos Szeredic55a01d2014-07-07 15:28:51 +0200700 cs->pg = buf->page;
701 cs->offset = buf->offset;
Miklos Szeredic3021622010-05-25 15:06:07 +0200702 cs->len = buf->len;
Miklos Szeredic3021622010-05-25 15:06:07 +0200703 cs->pipebufs++;
704 cs->nr_segs--;
705 } else {
David Howells6718b6f2019-10-16 16:47:32 +0100706 if (cs->nr_segs >= cs->pipe->max_usage)
Miklos Szeredic3021622010-05-25 15:06:07 +0200707 return -EIO;
708
709 page = alloc_page(GFP_HIGHUSER);
710 if (!page)
711 return -ENOMEM;
712
713 buf->page = page;
714 buf->offset = 0;
715 buf->len = 0;
716
717 cs->currbuf = buf;
Miklos Szeredic55a01d2014-07-07 15:28:51 +0200718 cs->pg = page;
719 cs->offset = 0;
Miklos Szeredic3021622010-05-25 15:06:07 +0200720 cs->len = PAGE_SIZE;
721 cs->pipebufs++;
722 cs->nr_segs++;
723 }
Miklos Szeredidd3bb142010-05-25 15:06:06 +0200724 } else {
Al Viro6c09e942015-04-03 22:06:08 -0400725 size_t off;
726 err = iov_iter_get_pages(cs->iter, &page, PAGE_SIZE, 1, &off);
Miklos Szeredidd3bb142010-05-25 15:06:06 +0200727 if (err < 0)
728 return err;
Al Viro6c09e942015-04-03 22:06:08 -0400729 BUG_ON(!err);
730 cs->len = err;
731 cs->offset = off;
Miklos Szeredic55a01d2014-07-07 15:28:51 +0200732 cs->pg = page;
Al Viro6c09e942015-04-03 22:06:08 -0400733 iov_iter_advance(cs->iter, err);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700734 }
Miklos Szeredi334f4852005-09-09 13:10:27 -0700735
Miklos Szeredidc008092015-07-01 16:25:58 +0200736 return lock_request(cs->req);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700737}
738
739/* Do as much copy to/from userspace buffer as we can */
Miklos Szeredi8bfc0162006-01-16 22:14:28 -0800740static int fuse_copy_do(struct fuse_copy_state *cs, void **val, unsigned *size)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700741{
742 unsigned ncpy = min(*size, cs->len);
743 if (val) {
Miklos Szeredic55a01d2014-07-07 15:28:51 +0200744 void *pgaddr = kmap_atomic(cs->pg);
745 void *buf = pgaddr + cs->offset;
746
Miklos Szeredi334f4852005-09-09 13:10:27 -0700747 if (cs->write)
Miklos Szeredic55a01d2014-07-07 15:28:51 +0200748 memcpy(buf, *val, ncpy);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700749 else
Miklos Szeredic55a01d2014-07-07 15:28:51 +0200750 memcpy(*val, buf, ncpy);
751
752 kunmap_atomic(pgaddr);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700753 *val += ncpy;
754 }
755 *size -= ncpy;
756 cs->len -= ncpy;
Miklos Szeredic55a01d2014-07-07 15:28:51 +0200757 cs->offset += ncpy;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700758 return ncpy;
759}
760
Miklos Szeredice534fb2010-05-25 15:06:07 +0200761static int fuse_check_page(struct page *page)
762{
763 if (page_mapcount(page) ||
764 page->mapping != NULL ||
765 page_count(page) != 1 ||
766 (page->flags & PAGE_FLAGS_CHECK_AT_PREP &
767 ~(1 << PG_locked |
768 1 << PG_referenced |
769 1 << PG_uptodate |
770 1 << PG_lru |
771 1 << PG_active |
772 1 << PG_reclaim))) {
Kirill Smelkovf2294482019-03-27 09:15:17 +0000773 pr_warn("trying to steal weird page\n");
774 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 +0200775 return 1;
776 }
777 return 0;
778}
779
780static int fuse_try_move_page(struct fuse_copy_state *cs, struct page **pagep)
781{
782 int err;
783 struct page *oldpage = *pagep;
784 struct page *newpage;
785 struct pipe_buffer *buf = cs->pipebufs;
Miklos Szeredice534fb2010-05-25 15:06:07 +0200786
Miklos Szeredidc008092015-07-01 16:25:58 +0200787 err = unlock_request(cs->req);
Miklos Szeredi0d8e84b2015-07-01 16:25:58 +0200788 if (err)
789 return err;
790
Miklos Szeredice534fb2010-05-25 15:06:07 +0200791 fuse_copy_finish(cs);
792
Miklos Szeredifba597d2016-09-27 10:45:12 +0200793 err = pipe_buf_confirm(cs->pipe, buf);
Miklos Szeredice534fb2010-05-25 15:06:07 +0200794 if (err)
795 return err;
796
797 BUG_ON(!cs->nr_segs);
798 cs->currbuf = buf;
799 cs->len = buf->len;
800 cs->pipebufs++;
801 cs->nr_segs--;
802
803 if (cs->len != PAGE_SIZE)
804 goto out_fallback;
805
Miklos Szeredica76f5b2016-09-27 10:45:12 +0200806 if (pipe_buf_steal(cs->pipe, buf) != 0)
Miklos Szeredice534fb2010-05-25 15:06:07 +0200807 goto out_fallback;
808
809 newpage = buf->page;
810
Miklos Szerediaa991b32015-02-26 11:45:47 +0100811 if (!PageUptodate(newpage))
812 SetPageUptodate(newpage);
Miklos Szeredice534fb2010-05-25 15:06:07 +0200813
814 ClearPageMappedToDisk(newpage);
815
816 if (fuse_check_page(newpage) != 0)
817 goto out_fallback_unlock;
818
Miklos Szeredice534fb2010-05-25 15:06:07 +0200819 /*
820 * This is a new and locked page, it shouldn't be mapped or
821 * have any special flags on it
822 */
823 if (WARN_ON(page_mapped(oldpage)))
824 goto out_fallback_unlock;
825 if (WARN_ON(page_has_private(oldpage)))
826 goto out_fallback_unlock;
827 if (WARN_ON(PageDirty(oldpage) || PageWriteback(oldpage)))
828 goto out_fallback_unlock;
829 if (WARN_ON(PageMlocked(oldpage)))
830 goto out_fallback_unlock;
831
Miklos Szeredief6a3c62011-03-22 16:30:52 -0700832 err = replace_page_cache_page(oldpage, newpage, GFP_KERNEL);
Miklos Szeredice534fb2010-05-25 15:06:07 +0200833 if (err) {
Miklos Szeredief6a3c62011-03-22 16:30:52 -0700834 unlock_page(newpage);
835 return err;
Miklos Szeredice534fb2010-05-25 15:06:07 +0200836 }
Miklos Szeredief6a3c62011-03-22 16:30:52 -0700837
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300838 get_page(newpage);
Miklos Szeredice534fb2010-05-25 15:06:07 +0200839
840 if (!(buf->flags & PIPE_BUF_FLAG_LRU))
841 lru_cache_add_file(newpage);
842
843 err = 0;
Miklos Szeredidc008092015-07-01 16:25:58 +0200844 spin_lock(&cs->req->waitq.lock);
Miklos Szeredi825d6d32015-07-01 16:25:58 +0200845 if (test_bit(FR_ABORTED, &cs->req->flags))
Miklos Szeredice534fb2010-05-25 15:06:07 +0200846 err = -ENOENT;
847 else
848 *pagep = newpage;
Miklos Szeredidc008092015-07-01 16:25:58 +0200849 spin_unlock(&cs->req->waitq.lock);
Miklos Szeredice534fb2010-05-25 15:06:07 +0200850
851 if (err) {
852 unlock_page(newpage);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300853 put_page(newpage);
Miklos Szeredice534fb2010-05-25 15:06:07 +0200854 return err;
855 }
856
857 unlock_page(oldpage);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300858 put_page(oldpage);
Miklos Szeredice534fb2010-05-25 15:06:07 +0200859 cs->len = 0;
860
861 return 0;
862
863out_fallback_unlock:
864 unlock_page(newpage);
865out_fallback:
Miklos Szeredic55a01d2014-07-07 15:28:51 +0200866 cs->pg = buf->page;
867 cs->offset = buf->offset;
Miklos Szeredice534fb2010-05-25 15:06:07 +0200868
Miklos Szeredidc008092015-07-01 16:25:58 +0200869 err = lock_request(cs->req);
Miklos Szeredice534fb2010-05-25 15:06:07 +0200870 if (err)
871 return err;
872
873 return 1;
874}
875
Miklos Szeredic3021622010-05-25 15:06:07 +0200876static int fuse_ref_page(struct fuse_copy_state *cs, struct page *page,
877 unsigned offset, unsigned count)
878{
879 struct pipe_buffer *buf;
Miklos Szeredi0d8e84b2015-07-01 16:25:58 +0200880 int err;
Miklos Szeredic3021622010-05-25 15:06:07 +0200881
David Howells6718b6f2019-10-16 16:47:32 +0100882 if (cs->nr_segs >= cs->pipe->max_usage)
Miklos Szeredic3021622010-05-25 15:06:07 +0200883 return -EIO;
884
Miklos Szeredidc008092015-07-01 16:25:58 +0200885 err = unlock_request(cs->req);
Miklos Szeredi0d8e84b2015-07-01 16:25:58 +0200886 if (err)
887 return err;
888
Miklos Szeredic3021622010-05-25 15:06:07 +0200889 fuse_copy_finish(cs);
890
891 buf = cs->pipebufs;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300892 get_page(page);
Miklos Szeredic3021622010-05-25 15:06:07 +0200893 buf->page = page;
894 buf->offset = offset;
895 buf->len = count;
896
897 cs->pipebufs++;
898 cs->nr_segs++;
899 cs->len = 0;
900
901 return 0;
902}
903
Miklos Szeredi334f4852005-09-09 13:10:27 -0700904/*
905 * Copy a page in the request to/from the userspace buffer. Must be
906 * done atomically
907 */
Miklos Szeredice534fb2010-05-25 15:06:07 +0200908static int fuse_copy_page(struct fuse_copy_state *cs, struct page **pagep,
Miklos Szeredi8bfc0162006-01-16 22:14:28 -0800909 unsigned offset, unsigned count, int zeroing)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700910{
Miklos Szeredice534fb2010-05-25 15:06:07 +0200911 int err;
912 struct page *page = *pagep;
913
Miklos Szeredib6777c42010-10-26 14:22:27 -0700914 if (page && zeroing && count < PAGE_SIZE)
915 clear_highpage(page);
916
Miklos Szeredi334f4852005-09-09 13:10:27 -0700917 while (count) {
Miklos Szeredic3021622010-05-25 15:06:07 +0200918 if (cs->write && cs->pipebufs && page) {
919 return fuse_ref_page(cs, page, offset, count);
920 } else if (!cs->len) {
Miklos Szeredice534fb2010-05-25 15:06:07 +0200921 if (cs->move_pages && page &&
922 offset == 0 && count == PAGE_SIZE) {
923 err = fuse_try_move_page(cs, pagep);
924 if (err <= 0)
925 return err;
926 } else {
927 err = fuse_copy_fill(cs);
928 if (err)
929 return err;
930 }
Miklos Szeredi1729a162008-11-26 12:03:54 +0100931 }
Miklos Szeredi334f4852005-09-09 13:10:27 -0700932 if (page) {
Cong Wang2408f6e2011-11-25 23:14:30 +0800933 void *mapaddr = kmap_atomic(page);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700934 void *buf = mapaddr + offset;
935 offset += fuse_copy_do(cs, &buf, &count);
Cong Wang2408f6e2011-11-25 23:14:30 +0800936 kunmap_atomic(mapaddr);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700937 } else
938 offset += fuse_copy_do(cs, NULL, &count);
939 }
940 if (page && !cs->write)
941 flush_dcache_page(page);
942 return 0;
943}
944
945/* Copy pages in the request to/from userspace buffer */
946static int fuse_copy_pages(struct fuse_copy_state *cs, unsigned nbytes,
947 int zeroing)
948{
949 unsigned i;
950 struct fuse_req *req = cs->req;
Miklos Szeredi05ea48c2019-09-10 15:04:11 +0200951 struct fuse_args_pages *ap = container_of(req->args, typeof(*ap), args);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700952
Miklos Szeredi05ea48c2019-09-10 15:04:11 +0200953
954 for (i = 0; i < ap->num_pages && (nbytes || zeroing); i++) {
Miklos Szeredice534fb2010-05-25 15:06:07 +0200955 int err;
Miklos Szeredi05ea48c2019-09-10 15:04:11 +0200956 unsigned int offset = ap->descs[i].offset;
957 unsigned int count = min(nbytes, ap->descs[i].length);
Miklos Szeredice534fb2010-05-25 15:06:07 +0200958
Miklos Szeredi05ea48c2019-09-10 15:04:11 +0200959 err = fuse_copy_page(cs, &ap->pages[i], offset, count, zeroing);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700960 if (err)
961 return err;
962
963 nbytes -= count;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700964 }
965 return 0;
966}
967
968/* Copy a single argument in the request to/from userspace buffer */
969static int fuse_copy_one(struct fuse_copy_state *cs, void *val, unsigned size)
970{
971 while (size) {
Miklos Szeredi1729a162008-11-26 12:03:54 +0100972 if (!cs->len) {
973 int err = fuse_copy_fill(cs);
974 if (err)
975 return err;
976 }
Miklos Szeredi334f4852005-09-09 13:10:27 -0700977 fuse_copy_do(cs, &val, &size);
978 }
979 return 0;
980}
981
982/* Copy request arguments to/from userspace buffer */
983static int fuse_copy_args(struct fuse_copy_state *cs, unsigned numargs,
984 unsigned argpages, struct fuse_arg *args,
985 int zeroing)
986{
987 int err = 0;
988 unsigned i;
989
990 for (i = 0; !err && i < numargs; i++) {
991 struct fuse_arg *arg = &args[i];
992 if (i == numargs - 1 && argpages)
993 err = fuse_copy_pages(cs, arg->size, zeroing);
994 else
995 err = fuse_copy_one(cs, arg->value, arg->size);
996 }
997 return err;
998}
999
Miklos Szeredif88996a2015-07-01 16:26:01 +02001000static int forget_pending(struct fuse_iqueue *fiq)
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001001{
Miklos Szeredif88996a2015-07-01 16:26:01 +02001002 return fiq->forget_list_head.next != NULL;
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001003}
1004
Miklos Szeredif88996a2015-07-01 16:26:01 +02001005static int request_pending(struct fuse_iqueue *fiq)
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001006{
Miklos Szeredif88996a2015-07-01 16:26:01 +02001007 return !list_empty(&fiq->pending) || !list_empty(&fiq->interrupts) ||
1008 forget_pending(fiq);
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001009}
1010
Miklos Szeredi334f4852005-09-09 13:10:27 -07001011/*
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001012 * Transfer an interrupt request to userspace
1013 *
1014 * Unlike other requests this is assembled on demand, without a need
1015 * to allocate a separate fuse_req structure.
1016 *
Eric Biggers76e43c82019-09-08 20:15:18 -07001017 * Called with fiq->lock held, releases it
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001018 */
Miklos Szeredifd22d622015-07-01 16:26:03 +02001019static int fuse_read_interrupt(struct fuse_iqueue *fiq,
1020 struct fuse_copy_state *cs,
Miklos Szeredic3021622010-05-25 15:06:07 +02001021 size_t nbytes, struct fuse_req *req)
Eric Biggers76e43c82019-09-08 20:15:18 -07001022__releases(fiq->lock)
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001023{
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001024 struct fuse_in_header ih;
1025 struct fuse_interrupt_in arg;
1026 unsigned reqsize = sizeof(ih) + sizeof(arg);
1027 int err;
1028
1029 list_del_init(&req->intr_entry);
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001030 memset(&ih, 0, sizeof(ih));
1031 memset(&arg, 0, sizeof(arg));
1032 ih.len = reqsize;
1033 ih.opcode = FUSE_INTERRUPT;
Kirill Tkhai3a5358d2018-09-11 13:12:05 +03001034 ih.unique = (req->in.h.unique | FUSE_INT_REQ_BIT);
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001035 arg.unique = req->in.h.unique;
1036
Eric Biggers76e43c82019-09-08 20:15:18 -07001037 spin_unlock(&fiq->lock);
Miklos Szeredic3021622010-05-25 15:06:07 +02001038 if (nbytes < reqsize)
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001039 return -EINVAL;
1040
Miklos Szeredic3021622010-05-25 15:06:07 +02001041 err = fuse_copy_one(cs, &ih, sizeof(ih));
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001042 if (!err)
Miklos Szeredic3021622010-05-25 15:06:07 +02001043 err = fuse_copy_one(cs, &arg, sizeof(arg));
1044 fuse_copy_finish(cs);
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001045
1046 return err ? err : reqsize;
1047}
1048
Vivek Goyal4388c5a2019-06-05 15:50:43 -04001049struct fuse_forget_link *fuse_dequeue_forget(struct fuse_iqueue *fiq,
1050 unsigned int max,
1051 unsigned int *countp)
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001052{
Miklos Szeredif88996a2015-07-01 16:26:01 +02001053 struct fuse_forget_link *head = fiq->forget_list_head.next;
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001054 struct fuse_forget_link **newhead = &head;
1055 unsigned count;
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001056
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001057 for (count = 0; *newhead != NULL && count < max; count++)
1058 newhead = &(*newhead)->next;
1059
Miklos Szeredif88996a2015-07-01 16:26:01 +02001060 fiq->forget_list_head.next = *newhead;
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001061 *newhead = NULL;
Miklos Szeredif88996a2015-07-01 16:26:01 +02001062 if (fiq->forget_list_head.next == NULL)
1063 fiq->forget_list_tail = &fiq->forget_list_head;
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001064
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001065 if (countp != NULL)
1066 *countp = count;
1067
1068 return head;
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001069}
Vivek Goyal4388c5a2019-06-05 15:50:43 -04001070EXPORT_SYMBOL(fuse_dequeue_forget);
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001071
Miklos Szeredifd22d622015-07-01 16:26:03 +02001072static int fuse_read_single_forget(struct fuse_iqueue *fiq,
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001073 struct fuse_copy_state *cs,
1074 size_t nbytes)
Eric Biggers76e43c82019-09-08 20:15:18 -07001075__releases(fiq->lock)
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001076{
1077 int err;
Vivek Goyal4388c5a2019-06-05 15:50:43 -04001078 struct fuse_forget_link *forget = fuse_dequeue_forget(fiq, 1, NULL);
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001079 struct fuse_forget_in arg = {
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001080 .nlookup = forget->forget_one.nlookup,
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001081 };
1082 struct fuse_in_header ih = {
1083 .opcode = FUSE_FORGET,
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001084 .nodeid = forget->forget_one.nodeid,
Miklos Szeredif88996a2015-07-01 16:26:01 +02001085 .unique = fuse_get_unique(fiq),
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001086 .len = sizeof(ih) + sizeof(arg),
1087 };
1088
Eric Biggers76e43c82019-09-08 20:15:18 -07001089 spin_unlock(&fiq->lock);
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001090 kfree(forget);
1091 if (nbytes < ih.len)
1092 return -EINVAL;
1093
1094 err = fuse_copy_one(cs, &ih, sizeof(ih));
1095 if (!err)
1096 err = fuse_copy_one(cs, &arg, sizeof(arg));
1097 fuse_copy_finish(cs);
1098
1099 if (err)
1100 return err;
1101
1102 return ih.len;
1103}
1104
Miklos Szeredifd22d622015-07-01 16:26:03 +02001105static int fuse_read_batch_forget(struct fuse_iqueue *fiq,
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001106 struct fuse_copy_state *cs, size_t nbytes)
Eric Biggers76e43c82019-09-08 20:15:18 -07001107__releases(fiq->lock)
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001108{
1109 int err;
1110 unsigned max_forgets;
1111 unsigned count;
1112 struct fuse_forget_link *head;
1113 struct fuse_batch_forget_in arg = { .count = 0 };
1114 struct fuse_in_header ih = {
1115 .opcode = FUSE_BATCH_FORGET,
Miklos Szeredif88996a2015-07-01 16:26:01 +02001116 .unique = fuse_get_unique(fiq),
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001117 .len = sizeof(ih) + sizeof(arg),
1118 };
1119
1120 if (nbytes < ih.len) {
Eric Biggers76e43c82019-09-08 20:15:18 -07001121 spin_unlock(&fiq->lock);
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001122 return -EINVAL;
1123 }
1124
1125 max_forgets = (nbytes - ih.len) / sizeof(struct fuse_forget_one);
Vivek Goyal4388c5a2019-06-05 15:50:43 -04001126 head = fuse_dequeue_forget(fiq, max_forgets, &count);
Eric Biggers76e43c82019-09-08 20:15:18 -07001127 spin_unlock(&fiq->lock);
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001128
1129 arg.count = count;
1130 ih.len += count * sizeof(struct fuse_forget_one);
1131 err = fuse_copy_one(cs, &ih, sizeof(ih));
1132 if (!err)
1133 err = fuse_copy_one(cs, &arg, sizeof(arg));
1134
1135 while (head) {
1136 struct fuse_forget_link *forget = head;
1137
1138 if (!err) {
1139 err = fuse_copy_one(cs, &forget->forget_one,
1140 sizeof(forget->forget_one));
1141 }
1142 head = forget->next;
1143 kfree(forget);
1144 }
1145
1146 fuse_copy_finish(cs);
1147
1148 if (err)
1149 return err;
1150
1151 return ih.len;
1152}
1153
Miklos Szeredifd22d622015-07-01 16:26:03 +02001154static int fuse_read_forget(struct fuse_conn *fc, struct fuse_iqueue *fiq,
1155 struct fuse_copy_state *cs,
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001156 size_t nbytes)
Eric Biggers76e43c82019-09-08 20:15:18 -07001157__releases(fiq->lock)
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001158{
Miklos Szeredif88996a2015-07-01 16:26:01 +02001159 if (fc->minor < 16 || fiq->forget_list_head.next->next == NULL)
Miklos Szeredifd22d622015-07-01 16:26:03 +02001160 return fuse_read_single_forget(fiq, cs, nbytes);
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001161 else
Miklos Szeredifd22d622015-07-01 16:26:03 +02001162 return fuse_read_batch_forget(fiq, cs, nbytes);
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001163}
1164
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001165/*
Miklos Szeredi334f4852005-09-09 13:10:27 -07001166 * Read a single request into the userspace filesystem's buffer. This
1167 * function waits until a request is available, then removes it from
1168 * the pending list and copies request data to userspace buffer. If
Miklos Szeredif9a28422006-06-25 05:48:53 -07001169 * no reply is needed (FORGET) or request has been aborted or there
1170 * was an error during the copying then it's finished by calling
Stefan Hajnoczi04ec5af2018-06-21 09:33:40 +01001171 * fuse_request_end(). Otherwise add it to the processing list, and set
Miklos Szeredi334f4852005-09-09 13:10:27 -07001172 * the 'sent' flag.
1173 */
Miklos Szeredic36960462015-07-01 16:26:09 +02001174static ssize_t fuse_dev_do_read(struct fuse_dev *fud, struct file *file,
Miklos Szeredic3021622010-05-25 15:06:07 +02001175 struct fuse_copy_state *cs, size_t nbytes)
Miklos Szeredi334f4852005-09-09 13:10:27 -07001176{
Miklos Szeredi82cbdcd2015-07-01 16:26:05 +02001177 ssize_t err;
Miklos Szeredic36960462015-07-01 16:26:09 +02001178 struct fuse_conn *fc = fud->fc;
Miklos Szeredif88996a2015-07-01 16:26:01 +02001179 struct fuse_iqueue *fiq = &fc->iq;
Miklos Szeredic36960462015-07-01 16:26:09 +02001180 struct fuse_pqueue *fpq = &fud->pq;
Miklos Szeredi334f4852005-09-09 13:10:27 -07001181 struct fuse_req *req;
Miklos Szeredid4993772019-09-10 15:04:11 +02001182 struct fuse_args *args;
Miklos Szeredi334f4852005-09-09 13:10:27 -07001183 unsigned reqsize;
Kirill Tkhaibe2ff422018-09-11 13:12:14 +03001184 unsigned int hash;
Miklos Szeredi334f4852005-09-09 13:10:27 -07001185
Kirill Smelkov1fb027d2019-07-08 17:03:31 +00001186 /*
1187 * Require sane minimum read buffer - that has capacity for fixed part
1188 * of any request header + negotiated max_write room for data.
1189 *
1190 * Historically libfuse reserves 4K for fixed header room, but e.g.
1191 * GlusterFS reserves only 80 bytes
1192 *
1193 * = `sizeof(fuse_in_header) + sizeof(fuse_write_in)`
1194 *
1195 * which is the absolute minimum any sane filesystem should be using
1196 * for header room.
1197 */
1198 if (nbytes < max_t(size_t, FUSE_MIN_READ_BUFFER,
1199 sizeof(struct fuse_in_header) +
1200 sizeof(struct fuse_write_in) +
1201 fc->max_write))
1202 return -EINVAL;
1203
Miklos Szeredi1d3d7522006-01-06 00:19:40 -08001204 restart:
Eric Biggers76e43c82019-09-08 20:15:18 -07001205 for (;;) {
1206 spin_lock(&fiq->lock);
1207 if (!fiq->connected || request_pending(fiq))
1208 break;
1209 spin_unlock(&fiq->lock);
Jeff Dikee5ac1d12006-04-10 22:54:53 -07001210
Eric Biggers76e43c82019-09-08 20:15:18 -07001211 if (file->f_flags & O_NONBLOCK)
1212 return -EAGAIN;
1213 err = wait_event_interruptible_exclusive(fiq->waitq,
Miklos Szeredi52509212015-07-01 16:26:03 +02001214 !fiq->connected || request_pending(fiq));
Eric Biggers76e43c82019-09-08 20:15:18 -07001215 if (err)
1216 return err;
1217 }
Miklos Szeredi52509212015-07-01 16:26:03 +02001218
Szymon Lukasz3b7008b2017-11-09 21:23:35 +01001219 if (!fiq->connected) {
Miklos Szeredieb98e3bd2019-01-24 10:40:16 +01001220 err = fc->aborted ? -ECONNABORTED : -ENODEV;
Miklos Szeredi334f4852005-09-09 13:10:27 -07001221 goto err_unlock;
Szymon Lukasz3b7008b2017-11-09 21:23:35 +01001222 }
Miklos Szeredi334f4852005-09-09 13:10:27 -07001223
Miklos Szeredif88996a2015-07-01 16:26:01 +02001224 if (!list_empty(&fiq->interrupts)) {
1225 req = list_entry(fiq->interrupts.next, struct fuse_req,
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001226 intr_entry);
Miklos Szeredifd22d622015-07-01 16:26:03 +02001227 return fuse_read_interrupt(fiq, cs, nbytes, req);
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001228 }
1229
Miklos Szeredif88996a2015-07-01 16:26:01 +02001230 if (forget_pending(fiq)) {
1231 if (list_empty(&fiq->pending) || fiq->forget_batch-- > 0)
Miklos Szeredifd22d622015-07-01 16:26:03 +02001232 return fuse_read_forget(fc, fiq, cs, nbytes);
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001233
Miklos Szeredif88996a2015-07-01 16:26:01 +02001234 if (fiq->forget_batch <= -8)
1235 fiq->forget_batch = 16;
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001236 }
1237
Miklos Szeredif88996a2015-07-01 16:26:01 +02001238 req = list_entry(fiq->pending.next, struct fuse_req, list);
Miklos Szeredi33e14b42015-07-01 16:26:01 +02001239 clear_bit(FR_PENDING, &req->flags);
Miklos Szeredief759252015-07-01 16:26:02 +02001240 list_del_init(&req->list);
Eric Biggers76e43c82019-09-08 20:15:18 -07001241 spin_unlock(&fiq->lock);
Miklos Szeredi4ce60812015-07-01 16:26:02 +02001242
Miklos Szeredid4993772019-09-10 15:04:11 +02001243 args = req->args;
1244 reqsize = req->in.h.len;
Miklos Szeredi5d6d3a32017-09-12 16:57:53 +02001245
Miklos Szeredi1d3d7522006-01-06 00:19:40 -08001246 /* If request is too large, reply with an error and restart the read */
Miklos Szeredic3021622010-05-25 15:06:07 +02001247 if (nbytes < reqsize) {
Miklos Szeredi1d3d7522006-01-06 00:19:40 -08001248 req->out.h.error = -EIO;
1249 /* SETXATTR is special, since it may contain too large data */
Miklos Szeredid4993772019-09-10 15:04:11 +02001250 if (args->opcode == FUSE_SETXATTR)
Miklos Szeredi1d3d7522006-01-06 00:19:40 -08001251 req->out.h.error = -E2BIG;
Stefan Hajnoczi04ec5af2018-06-21 09:33:40 +01001252 fuse_request_end(fc, req);
Miklos Szeredi1d3d7522006-01-06 00:19:40 -08001253 goto restart;
Miklos Szeredi334f4852005-09-09 13:10:27 -07001254 }
Miklos Szeredi45a91cb2015-07-01 16:26:06 +02001255 spin_lock(&fpq->lock);
Miklos Szeredi82cbdcd2015-07-01 16:26:05 +02001256 list_add(&req->list, &fpq->io);
Miklos Szeredi45a91cb2015-07-01 16:26:06 +02001257 spin_unlock(&fpq->lock);
Miklos Szeredic3021622010-05-25 15:06:07 +02001258 cs->req = req;
Miklos Szeredid4993772019-09-10 15:04:11 +02001259 err = fuse_copy_one(cs, &req->in.h, sizeof(req->in.h));
Miklos Szeredi1d3d7522006-01-06 00:19:40 -08001260 if (!err)
Miklos Szeredid4993772019-09-10 15:04:11 +02001261 err = fuse_copy_args(cs, args->in_numargs, args->in_pages,
1262 (struct fuse_arg *) args->in_args, 0);
Miklos Szeredic3021622010-05-25 15:06:07 +02001263 fuse_copy_finish(cs);
Miklos Szeredi45a91cb2015-07-01 16:26:06 +02001264 spin_lock(&fpq->lock);
Miklos Szeredi825d6d32015-07-01 16:25:58 +02001265 clear_bit(FR_LOCKED, &req->flags);
Miklos Szeredie96edd92015-07-01 16:26:04 +02001266 if (!fpq->connected) {
Miklos Szeredieb98e3bd2019-01-24 10:40:16 +01001267 err = fc->aborted ? -ECONNABORTED : -ENODEV;
Miklos Szeredi82cbdcd2015-07-01 16:26:05 +02001268 goto out_end;
Miklos Szeredic9c9d7d2007-10-16 23:31:05 -07001269 }
Miklos Szeredi334f4852005-09-09 13:10:27 -07001270 if (err) {
Miklos Szeredic9c9d7d2007-10-16 23:31:05 -07001271 req->out.h.error = -EIO;
Miklos Szeredi82cbdcd2015-07-01 16:26:05 +02001272 goto out_end;
Miklos Szeredi334f4852005-09-09 13:10:27 -07001273 }
Miklos Szeredi825d6d32015-07-01 16:25:58 +02001274 if (!test_bit(FR_ISREPLY, &req->flags)) {
Miklos Szeredi82cbdcd2015-07-01 16:26:05 +02001275 err = reqsize;
1276 goto out_end;
Miklos Szeredi334f4852005-09-09 13:10:27 -07001277 }
Kirill Tkhaibe2ff422018-09-11 13:12:14 +03001278 hash = fuse_req_hash(req->in.h.unique);
1279 list_move_tail(&req->list, &fpq->processing[hash]);
Kirill Tkhaibc78abb2018-09-25 12:28:55 +03001280 __fuse_get_request(req);
Miklos Szeredi82cbdcd2015-07-01 16:26:05 +02001281 set_bit(FR_SENT, &req->flags);
Miklos Szeredi4c316f22018-09-28 16:43:22 +02001282 spin_unlock(&fpq->lock);
Miklos Szeredi82cbdcd2015-07-01 16:26:05 +02001283 /* matches barrier in request_wait_answer() */
1284 smp_mb__after_atomic();
1285 if (test_bit(FR_INTERRUPTED, &req->flags))
1286 queue_interrupt(fiq, req);
Kirill Tkhaibc78abb2018-09-25 12:28:55 +03001287 fuse_put_request(fc, req);
Miklos Szeredi82cbdcd2015-07-01 16:26:05 +02001288
Miklos Szeredi334f4852005-09-09 13:10:27 -07001289 return reqsize;
1290
Miklos Szeredi82cbdcd2015-07-01 16:26:05 +02001291out_end:
Miklos Szeredi77cd9d42015-07-01 16:26:06 +02001292 if (!test_bit(FR_PRIVATE, &req->flags))
1293 list_del_init(&req->list);
Miklos Szeredi45a91cb2015-07-01 16:26:06 +02001294 spin_unlock(&fpq->lock);
Stefan Hajnoczi04ec5af2018-06-21 09:33:40 +01001295 fuse_request_end(fc, req);
Miklos Szeredi82cbdcd2015-07-01 16:26:05 +02001296 return err;
1297
Miklos Szeredi334f4852005-09-09 13:10:27 -07001298 err_unlock:
Eric Biggers76e43c82019-09-08 20:15:18 -07001299 spin_unlock(&fiq->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001300 return err;
1301}
1302
Tom Van Braeckel94e4fe22015-01-12 05:22:16 +01001303static int fuse_dev_open(struct inode *inode, struct file *file)
1304{
1305 /*
1306 * The fuse device's file's private_data is used to hold
1307 * the fuse_conn(ection) when it is mounted, and is used to
1308 * keep track of whether the file has been mounted already.
1309 */
1310 file->private_data = NULL;
1311 return 0;
1312}
1313
Al Virofbdbacc2015-04-03 21:53:39 -04001314static ssize_t fuse_dev_read(struct kiocb *iocb, struct iov_iter *to)
Miklos Szeredic3021622010-05-25 15:06:07 +02001315{
1316 struct fuse_copy_state cs;
1317 struct file *file = iocb->ki_filp;
Miklos Szeredicc080e92015-07-01 16:26:08 +02001318 struct fuse_dev *fud = fuse_get_dev(file);
1319
1320 if (!fud)
Miklos Szeredic3021622010-05-25 15:06:07 +02001321 return -EPERM;
1322
Al Virofbdbacc2015-04-03 21:53:39 -04001323 if (!iter_is_iovec(to))
1324 return -EINVAL;
Miklos Szeredic3021622010-05-25 15:06:07 +02001325
Miklos Szeredidc008092015-07-01 16:25:58 +02001326 fuse_copy_init(&cs, 1, to);
Al Virofbdbacc2015-04-03 21:53:39 -04001327
Miklos Szeredic36960462015-07-01 16:26:09 +02001328 return fuse_dev_do_read(fud, file, &cs, iov_iter_count(to));
Miklos Szeredic3021622010-05-25 15:06:07 +02001329}
1330
Miklos Szeredic3021622010-05-25 15:06:07 +02001331static ssize_t fuse_dev_splice_read(struct file *in, loff_t *ppos,
1332 struct pipe_inode_info *pipe,
1333 size_t len, unsigned int flags)
1334{
Al Virod82718e2016-09-17 22:56:25 -04001335 int total, ret;
Miklos Szeredic3021622010-05-25 15:06:07 +02001336 int page_nr = 0;
Miklos Szeredic3021622010-05-25 15:06:07 +02001337 struct pipe_buffer *bufs;
1338 struct fuse_copy_state cs;
Miklos Szeredicc080e92015-07-01 16:26:08 +02001339 struct fuse_dev *fud = fuse_get_dev(in);
1340
1341 if (!fud)
Miklos Szeredic3021622010-05-25 15:06:07 +02001342 return -EPERM;
1343
David Howells6718b6f2019-10-16 16:47:32 +01001344 bufs = kvmalloc_array(pipe->max_usage, sizeof(struct pipe_buffer),
Andrey Ryabinind6d931a2018-07-17 19:00:34 +03001345 GFP_KERNEL);
Miklos Szeredic3021622010-05-25 15:06:07 +02001346 if (!bufs)
1347 return -ENOMEM;
1348
Miklos Szeredidc008092015-07-01 16:25:58 +02001349 fuse_copy_init(&cs, 1, NULL);
Miklos Szeredic3021622010-05-25 15:06:07 +02001350 cs.pipebufs = bufs;
1351 cs.pipe = pipe;
Miklos Szeredic36960462015-07-01 16:26:09 +02001352 ret = fuse_dev_do_read(fud, in, &cs, len);
Miklos Szeredic3021622010-05-25 15:06:07 +02001353 if (ret < 0)
1354 goto out;
1355
David Howells6718b6f2019-10-16 16:47:32 +01001356 if (pipe_occupancy(pipe->head, pipe->tail) + cs.nr_segs > pipe->max_usage) {
Miklos Szeredic3021622010-05-25 15:06:07 +02001357 ret = -EIO;
Al Virod82718e2016-09-17 22:56:25 -04001358 goto out;
Miklos Szeredic3021622010-05-25 15:06:07 +02001359 }
1360
Al Virod82718e2016-09-17 22:56:25 -04001361 for (ret = total = 0; page_nr < cs.nr_segs; total += ret) {
Miklos Szeredi28a625c2014-01-22 19:36:57 +01001362 /*
1363 * Need to be careful about this. Having buf->ops in module
1364 * code can Oops if the buffer persists after module unload.
1365 */
Al Virod82718e2016-09-17 22:56:25 -04001366 bufs[page_nr].ops = &nosteal_pipe_buf_ops;
Miklos Szeredi84588a92017-02-16 15:08:20 +01001367 bufs[page_nr].flags = 0;
Al Virod82718e2016-09-17 22:56:25 -04001368 ret = add_to_pipe(pipe, &bufs[page_nr++]);
1369 if (unlikely(ret < 0))
1370 break;
Miklos Szeredic3021622010-05-25 15:06:07 +02001371 }
Al Virod82718e2016-09-17 22:56:25 -04001372 if (total)
1373 ret = total;
Miklos Szeredic3021622010-05-25 15:06:07 +02001374out:
1375 for (; page_nr < cs.nr_segs; page_nr++)
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001376 put_page(bufs[page_nr].page);
Miklos Szeredic3021622010-05-25 15:06:07 +02001377
Andrey Ryabinind6d931a2018-07-17 19:00:34 +03001378 kvfree(bufs);
Miklos Szeredic3021622010-05-25 15:06:07 +02001379 return ret;
1380}
1381
Tejun Heo95668a62008-11-26 12:03:55 +01001382static int fuse_notify_poll(struct fuse_conn *fc, unsigned int size,
1383 struct fuse_copy_state *cs)
1384{
1385 struct fuse_notify_poll_wakeup_out outarg;
Miklos Szeredif6d47a12009-01-26 15:00:59 +01001386 int err = -EINVAL;
Tejun Heo95668a62008-11-26 12:03:55 +01001387
1388 if (size != sizeof(outarg))
Miklos Szeredif6d47a12009-01-26 15:00:59 +01001389 goto err;
Tejun Heo95668a62008-11-26 12:03:55 +01001390
1391 err = fuse_copy_one(cs, &outarg, sizeof(outarg));
1392 if (err)
Miklos Szeredif6d47a12009-01-26 15:00:59 +01001393 goto err;
Tejun Heo95668a62008-11-26 12:03:55 +01001394
Miklos Szeredif6d47a12009-01-26 15:00:59 +01001395 fuse_copy_finish(cs);
Tejun Heo95668a62008-11-26 12:03:55 +01001396 return fuse_notify_poll_wakeup(fc, &outarg);
Miklos Szeredif6d47a12009-01-26 15:00:59 +01001397
1398err:
1399 fuse_copy_finish(cs);
1400 return err;
Tejun Heo95668a62008-11-26 12:03:55 +01001401}
1402
John Muir3b463ae2009-05-31 11:13:57 -04001403static int fuse_notify_inval_inode(struct fuse_conn *fc, unsigned int size,
1404 struct fuse_copy_state *cs)
1405{
1406 struct fuse_notify_inval_inode_out outarg;
1407 int err = -EINVAL;
1408
1409 if (size != sizeof(outarg))
1410 goto err;
1411
1412 err = fuse_copy_one(cs, &outarg, sizeof(outarg));
1413 if (err)
1414 goto err;
1415 fuse_copy_finish(cs);
1416
1417 down_read(&fc->killsb);
1418 err = -ENOENT;
Miklos Szeredib21dda42010-02-05 12:08:31 +01001419 if (fc->sb) {
1420 err = fuse_reverse_inval_inode(fc->sb, outarg.ino,
1421 outarg.off, outarg.len);
1422 }
John Muir3b463ae2009-05-31 11:13:57 -04001423 up_read(&fc->killsb);
1424 return err;
1425
1426err:
1427 fuse_copy_finish(cs);
1428 return err;
1429}
1430
1431static int fuse_notify_inval_entry(struct fuse_conn *fc, unsigned int size,
1432 struct fuse_copy_state *cs)
1433{
1434 struct fuse_notify_inval_entry_out outarg;
Fang Wenqib2d82ee2009-12-30 18:37:13 +08001435 int err = -ENOMEM;
1436 char *buf;
John Muir3b463ae2009-05-31 11:13:57 -04001437 struct qstr name;
1438
Fang Wenqib2d82ee2009-12-30 18:37:13 +08001439 buf = kzalloc(FUSE_NAME_MAX + 1, GFP_KERNEL);
1440 if (!buf)
1441 goto err;
1442
1443 err = -EINVAL;
John Muir3b463ae2009-05-31 11:13:57 -04001444 if (size < sizeof(outarg))
1445 goto err;
1446
1447 err = fuse_copy_one(cs, &outarg, sizeof(outarg));
1448 if (err)
1449 goto err;
1450
1451 err = -ENAMETOOLONG;
1452 if (outarg.namelen > FUSE_NAME_MAX)
1453 goto err;
1454
Miklos Szeredic2183d12011-08-24 10:20:17 +02001455 err = -EINVAL;
1456 if (size != sizeof(outarg) + outarg.namelen + 1)
1457 goto err;
1458
John Muir3b463ae2009-05-31 11:13:57 -04001459 name.name = buf;
1460 name.len = outarg.namelen;
1461 err = fuse_copy_one(cs, buf, outarg.namelen + 1);
1462 if (err)
1463 goto err;
1464 fuse_copy_finish(cs);
1465 buf[outarg.namelen] = 0;
John Muir3b463ae2009-05-31 11:13:57 -04001466
1467 down_read(&fc->killsb);
1468 err = -ENOENT;
Miklos Szeredib21dda42010-02-05 12:08:31 +01001469 if (fc->sb)
John Muir451d0f52011-12-06 21:50:06 +01001470 err = fuse_reverse_inval_entry(fc->sb, outarg.parent, 0, &name);
1471 up_read(&fc->killsb);
1472 kfree(buf);
1473 return err;
1474
1475err:
1476 kfree(buf);
1477 fuse_copy_finish(cs);
1478 return err;
1479}
1480
1481static int fuse_notify_delete(struct fuse_conn *fc, unsigned int size,
1482 struct fuse_copy_state *cs)
1483{
1484 struct fuse_notify_delete_out outarg;
1485 int err = -ENOMEM;
1486 char *buf;
1487 struct qstr name;
1488
1489 buf = kzalloc(FUSE_NAME_MAX + 1, GFP_KERNEL);
1490 if (!buf)
1491 goto err;
1492
1493 err = -EINVAL;
1494 if (size < sizeof(outarg))
1495 goto err;
1496
1497 err = fuse_copy_one(cs, &outarg, sizeof(outarg));
1498 if (err)
1499 goto err;
1500
1501 err = -ENAMETOOLONG;
1502 if (outarg.namelen > FUSE_NAME_MAX)
1503 goto err;
1504
1505 err = -EINVAL;
1506 if (size != sizeof(outarg) + outarg.namelen + 1)
1507 goto err;
1508
1509 name.name = buf;
1510 name.len = outarg.namelen;
1511 err = fuse_copy_one(cs, buf, outarg.namelen + 1);
1512 if (err)
1513 goto err;
1514 fuse_copy_finish(cs);
1515 buf[outarg.namelen] = 0;
John Muir451d0f52011-12-06 21:50:06 +01001516
1517 down_read(&fc->killsb);
1518 err = -ENOENT;
1519 if (fc->sb)
1520 err = fuse_reverse_inval_entry(fc->sb, outarg.parent,
1521 outarg.child, &name);
John Muir3b463ae2009-05-31 11:13:57 -04001522 up_read(&fc->killsb);
Fang Wenqib2d82ee2009-12-30 18:37:13 +08001523 kfree(buf);
John Muir3b463ae2009-05-31 11:13:57 -04001524 return err;
1525
1526err:
Fang Wenqib2d82ee2009-12-30 18:37:13 +08001527 kfree(buf);
John Muir3b463ae2009-05-31 11:13:57 -04001528 fuse_copy_finish(cs);
1529 return err;
1530}
1531
Miklos Szeredia1d75f22010-07-12 14:41:40 +02001532static int fuse_notify_store(struct fuse_conn *fc, unsigned int size,
1533 struct fuse_copy_state *cs)
1534{
1535 struct fuse_notify_store_out outarg;
1536 struct inode *inode;
1537 struct address_space *mapping;
1538 u64 nodeid;
1539 int err;
1540 pgoff_t index;
1541 unsigned int offset;
1542 unsigned int num;
1543 loff_t file_size;
1544 loff_t end;
1545
1546 err = -EINVAL;
1547 if (size < sizeof(outarg))
1548 goto out_finish;
1549
1550 err = fuse_copy_one(cs, &outarg, sizeof(outarg));
1551 if (err)
1552 goto out_finish;
1553
1554 err = -EINVAL;
1555 if (size - sizeof(outarg) != outarg.size)
1556 goto out_finish;
1557
1558 nodeid = outarg.nodeid;
1559
1560 down_read(&fc->killsb);
1561
1562 err = -ENOENT;
1563 if (!fc->sb)
1564 goto out_up_killsb;
1565
1566 inode = ilookup5(fc->sb, nodeid, fuse_inode_eq, &nodeid);
1567 if (!inode)
1568 goto out_up_killsb;
1569
1570 mapping = inode->i_mapping;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001571 index = outarg.offset >> PAGE_SHIFT;
1572 offset = outarg.offset & ~PAGE_MASK;
Miklos Szeredia1d75f22010-07-12 14:41:40 +02001573 file_size = i_size_read(inode);
1574 end = outarg.offset + outarg.size;
1575 if (end > file_size) {
1576 file_size = end;
1577 fuse_write_update_size(inode, file_size);
1578 }
1579
1580 num = outarg.size;
1581 while (num) {
1582 struct page *page;
1583 unsigned int this_num;
1584
1585 err = -ENOMEM;
1586 page = find_or_create_page(mapping, index,
1587 mapping_gfp_mask(mapping));
1588 if (!page)
1589 goto out_iput;
1590
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001591 this_num = min_t(unsigned, num, PAGE_SIZE - offset);
Miklos Szeredia1d75f22010-07-12 14:41:40 +02001592 err = fuse_copy_page(cs, &page, offset, this_num, 0);
Miklos Szeredi063ec1e2014-01-22 19:36:58 +01001593 if (!err && offset == 0 &&
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001594 (this_num == PAGE_SIZE || file_size == end))
Miklos Szeredia1d75f22010-07-12 14:41:40 +02001595 SetPageUptodate(page);
1596 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001597 put_page(page);
Miklos Szeredia1d75f22010-07-12 14:41:40 +02001598
1599 if (err)
1600 goto out_iput;
1601
1602 num -= this_num;
1603 offset = 0;
1604 index++;
1605 }
1606
1607 err = 0;
1608
1609out_iput:
1610 iput(inode);
1611out_up_killsb:
1612 up_read(&fc->killsb);
1613out_finish:
1614 fuse_copy_finish(cs);
1615 return err;
1616}
1617
Miklos Szeredi75b399d2019-09-10 15:04:11 +02001618struct fuse_retrieve_args {
1619 struct fuse_args_pages ap;
1620 struct fuse_notify_retrieve_in inarg;
1621};
1622
1623static void fuse_retrieve_end(struct fuse_conn *fc, struct fuse_args *args,
1624 int error)
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001625{
Miklos Szeredi75b399d2019-09-10 15:04:11 +02001626 struct fuse_retrieve_args *ra =
1627 container_of(args, typeof(*ra), ap.args);
1628
1629 release_pages(ra->ap.pages, ra->ap.num_pages);
1630 kfree(ra);
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001631}
1632
1633static int fuse_retrieve(struct fuse_conn *fc, struct inode *inode,
1634 struct fuse_notify_retrieve_out *outarg)
1635{
1636 int err;
1637 struct address_space *mapping = inode->i_mapping;
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001638 pgoff_t index;
1639 loff_t file_size;
1640 unsigned int num;
1641 unsigned int offset;
Geert Uytterhoeven01574432010-09-30 22:06:21 +02001642 size_t total_len = 0;
Constantine Shulyupin5da784c2018-09-06 15:37:06 +03001643 unsigned int num_pages;
Miklos Szeredi75b399d2019-09-10 15:04:11 +02001644 struct fuse_retrieve_args *ra;
1645 size_t args_size = sizeof(*ra);
1646 struct fuse_args_pages *ap;
1647 struct fuse_args *args;
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001648
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001649 offset = outarg->offset & ~PAGE_MASK;
Maxim Patlasov4d53dc92012-10-26 19:48:42 +04001650 file_size = i_size_read(inode);
1651
Kirill Smelkov76406822019-03-27 10:15:19 +00001652 num = min(outarg->size, fc->max_write);
Maxim Patlasov4d53dc92012-10-26 19:48:42 +04001653 if (outarg->offset > file_size)
1654 num = 0;
1655 else if (outarg->offset + num > file_size)
1656 num = file_size - outarg->offset;
1657
1658 num_pages = (num + offset + PAGE_SIZE - 1) >> PAGE_SHIFT;
Constantine Shulyupin5da784c2018-09-06 15:37:06 +03001659 num_pages = min(num_pages, fc->max_pages);
Maxim Patlasov4d53dc92012-10-26 19:48:42 +04001660
Miklos Szeredi75b399d2019-09-10 15:04:11 +02001661 args_size += num_pages * (sizeof(ap->pages[0]) + sizeof(ap->descs[0]));
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001662
Miklos Szeredi75b399d2019-09-10 15:04:11 +02001663 ra = kzalloc(args_size, GFP_KERNEL);
1664 if (!ra)
1665 return -ENOMEM;
1666
1667 ap = &ra->ap;
1668 ap->pages = (void *) (ra + 1);
1669 ap->descs = (void *) (ap->pages + num_pages);
1670
1671 args = &ap->args;
1672 args->nodeid = outarg->nodeid;
1673 args->opcode = FUSE_NOTIFY_REPLY;
1674 args->in_numargs = 2;
1675 args->in_pages = true;
1676 args->end = fuse_retrieve_end;
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001677
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001678 index = outarg->offset >> PAGE_SHIFT;
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001679
Miklos Szeredi75b399d2019-09-10 15:04:11 +02001680 while (num && ap->num_pages < num_pages) {
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001681 struct page *page;
1682 unsigned int this_num;
1683
1684 page = find_get_page(mapping, index);
1685 if (!page)
1686 break;
1687
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001688 this_num = min_t(unsigned, num, PAGE_SIZE - offset);
Miklos Szeredi75b399d2019-09-10 15:04:11 +02001689 ap->pages[ap->num_pages] = page;
1690 ap->descs[ap->num_pages].offset = offset;
1691 ap->descs[ap->num_pages].length = this_num;
1692 ap->num_pages++;
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001693
Miklos Szeredic9e67d42012-09-04 18:45:54 +02001694 offset = 0;
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001695 num -= this_num;
1696 total_len += this_num;
Miklos Szeredi48706d02011-12-13 10:36:59 +01001697 index++;
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001698 }
Miklos Szeredi75b399d2019-09-10 15:04:11 +02001699 ra->inarg.offset = outarg->offset;
1700 ra->inarg.size = total_len;
1701 args->in_args[0].size = sizeof(ra->inarg);
1702 args->in_args[0].value = &ra->inarg;
1703 args->in_args[1].size = total_len;
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001704
Miklos Szeredi75b399d2019-09-10 15:04:11 +02001705 err = fuse_simple_notify_reply(fc, args, outarg->notify_unique);
1706 if (err)
1707 fuse_retrieve_end(fc, args, err);
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001708
1709 return err;
1710}
1711
1712static int fuse_notify_retrieve(struct fuse_conn *fc, unsigned int size,
1713 struct fuse_copy_state *cs)
1714{
1715 struct fuse_notify_retrieve_out outarg;
1716 struct inode *inode;
1717 int err;
1718
1719 err = -EINVAL;
1720 if (size != sizeof(outarg))
1721 goto copy_finish;
1722
1723 err = fuse_copy_one(cs, &outarg, sizeof(outarg));
1724 if (err)
1725 goto copy_finish;
1726
1727 fuse_copy_finish(cs);
1728
1729 down_read(&fc->killsb);
1730 err = -ENOENT;
1731 if (fc->sb) {
1732 u64 nodeid = outarg.nodeid;
1733
1734 inode = ilookup5(fc->sb, nodeid, fuse_inode_eq, &nodeid);
1735 if (inode) {
1736 err = fuse_retrieve(fc, inode, &outarg);
1737 iput(inode);
1738 }
1739 }
1740 up_read(&fc->killsb);
1741
1742 return err;
1743
1744copy_finish:
1745 fuse_copy_finish(cs);
1746 return err;
1747}
1748
Tejun Heo85993962008-11-26 12:03:55 +01001749static int fuse_notify(struct fuse_conn *fc, enum fuse_notify_code code,
1750 unsigned int size, struct fuse_copy_state *cs)
1751{
Miklos Szeredi0d278362015-02-26 11:45:47 +01001752 /* Don't try to move pages (yet) */
1753 cs->move_pages = 0;
1754
Tejun Heo85993962008-11-26 12:03:55 +01001755 switch (code) {
Tejun Heo95668a62008-11-26 12:03:55 +01001756 case FUSE_NOTIFY_POLL:
1757 return fuse_notify_poll(fc, size, cs);
1758
John Muir3b463ae2009-05-31 11:13:57 -04001759 case FUSE_NOTIFY_INVAL_INODE:
1760 return fuse_notify_inval_inode(fc, size, cs);
1761
1762 case FUSE_NOTIFY_INVAL_ENTRY:
1763 return fuse_notify_inval_entry(fc, size, cs);
1764
Miklos Szeredia1d75f22010-07-12 14:41:40 +02001765 case FUSE_NOTIFY_STORE:
1766 return fuse_notify_store(fc, size, cs);
1767
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001768 case FUSE_NOTIFY_RETRIEVE:
1769 return fuse_notify_retrieve(fc, size, cs);
1770
John Muir451d0f52011-12-06 21:50:06 +01001771 case FUSE_NOTIFY_DELETE:
1772 return fuse_notify_delete(fc, size, cs);
1773
Tejun Heo85993962008-11-26 12:03:55 +01001774 default:
Miklos Szeredif6d47a12009-01-26 15:00:59 +01001775 fuse_copy_finish(cs);
Tejun Heo85993962008-11-26 12:03:55 +01001776 return -EINVAL;
1777 }
1778}
1779
Miklos Szeredi334f4852005-09-09 13:10:27 -07001780/* Look up request on processing list by unique ID */
Miklos Szeredi3a2b5b92015-07-01 16:26:04 +02001781static struct fuse_req *request_find(struct fuse_pqueue *fpq, u64 unique)
Miklos Szeredi334f4852005-09-09 13:10:27 -07001782{
Kirill Tkhaibe2ff422018-09-11 13:12:14 +03001783 unsigned int hash = fuse_req_hash(unique);
Dong Fang05726ac2013-07-30 22:50:01 -04001784 struct fuse_req *req;
Miklos Szeredi334f4852005-09-09 13:10:27 -07001785
Kirill Tkhaibe2ff422018-09-11 13:12:14 +03001786 list_for_each_entry(req, &fpq->processing[hash], list) {
Kirill Tkhai3a5358d2018-09-11 13:12:05 +03001787 if (req->in.h.unique == unique)
Miklos Szeredi334f4852005-09-09 13:10:27 -07001788 return req;
1789 }
1790 return NULL;
1791}
1792
Miklos Szeredid4993772019-09-10 15:04:11 +02001793static int copy_out_args(struct fuse_copy_state *cs, struct fuse_args *args,
Miklos Szeredi334f4852005-09-09 13:10:27 -07001794 unsigned nbytes)
1795{
1796 unsigned reqsize = sizeof(struct fuse_out_header);
1797
Stefan Hajnoczi14d46d72018-06-21 09:34:25 +01001798 reqsize += fuse_len_args(args->out_numargs, args->out_args);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001799
Miklos Szeredid4993772019-09-10 15:04:11 +02001800 if (reqsize < nbytes || (reqsize > nbytes && !args->out_argvar))
Miklos Szeredi334f4852005-09-09 13:10:27 -07001801 return -EINVAL;
1802 else if (reqsize > nbytes) {
Miklos Szeredid4993772019-09-10 15:04:11 +02001803 struct fuse_arg *lastarg = &args->out_args[args->out_numargs-1];
Miklos Szeredi334f4852005-09-09 13:10:27 -07001804 unsigned diffsize = reqsize - nbytes;
Miklos Szeredid4993772019-09-10 15:04:11 +02001805
Miklos Szeredi334f4852005-09-09 13:10:27 -07001806 if (diffsize > lastarg->size)
1807 return -EINVAL;
1808 lastarg->size -= diffsize;
1809 }
Miklos Szeredid4993772019-09-10 15:04:11 +02001810 return fuse_copy_args(cs, args->out_numargs, args->out_pages,
1811 args->out_args, args->page_zeroing);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001812}
1813
1814/*
1815 * Write a single reply to a request. First the header is copied from
1816 * the write buffer. The request is then searched on the processing
1817 * list by the unique ID found in the header. If found, then remove
1818 * it from the list and copy the rest of the buffer to the request.
Stefan Hajnoczi04ec5af2018-06-21 09:33:40 +01001819 * The request is finished by calling fuse_request_end().
Miklos Szeredi334f4852005-09-09 13:10:27 -07001820 */
Miklos Szeredic36960462015-07-01 16:26:09 +02001821static ssize_t fuse_dev_do_write(struct fuse_dev *fud,
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001822 struct fuse_copy_state *cs, size_t nbytes)
Miklos Szeredi334f4852005-09-09 13:10:27 -07001823{
1824 int err;
Miklos Szeredic36960462015-07-01 16:26:09 +02001825 struct fuse_conn *fc = fud->fc;
1826 struct fuse_pqueue *fpq = &fud->pq;
Miklos Szeredi334f4852005-09-09 13:10:27 -07001827 struct fuse_req *req;
1828 struct fuse_out_header oh;
Miklos Szeredi334f4852005-09-09 13:10:27 -07001829
Kirill Tkhai7407a102018-11-08 12:05:36 +03001830 err = -EINVAL;
Miklos Szeredi334f4852005-09-09 13:10:27 -07001831 if (nbytes < sizeof(struct fuse_out_header))
Kirill Tkhai7407a102018-11-08 12:05:36 +03001832 goto out;
Miklos Szeredi334f4852005-09-09 13:10:27 -07001833
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001834 err = fuse_copy_one(cs, &oh, sizeof(oh));
Miklos Szeredi334f4852005-09-09 13:10:27 -07001835 if (err)
Kirill Tkhai7407a102018-11-08 12:05:36 +03001836 goto copy_finish;
Tejun Heo85993962008-11-26 12:03:55 +01001837
Miklos Szeredi334f4852005-09-09 13:10:27 -07001838 err = -EINVAL;
Tejun Heo85993962008-11-26 12:03:55 +01001839 if (oh.len != nbytes)
Kirill Tkhai7407a102018-11-08 12:05:36 +03001840 goto copy_finish;
Tejun Heo85993962008-11-26 12:03:55 +01001841
1842 /*
1843 * Zero oh.unique indicates unsolicited notification message
1844 * and error contains notification code.
1845 */
1846 if (!oh.unique) {
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001847 err = fuse_notify(fc, oh.error, nbytes - sizeof(oh), cs);
Kirill Tkhai7407a102018-11-08 12:05:36 +03001848 goto out;
Tejun Heo85993962008-11-26 12:03:55 +01001849 }
1850
1851 err = -EINVAL;
1852 if (oh.error <= -1000 || oh.error > 0)
Kirill Tkhai7407a102018-11-08 12:05:36 +03001853 goto copy_finish;
Miklos Szeredi334f4852005-09-09 13:10:27 -07001854
Miklos Szeredi45a91cb2015-07-01 16:26:06 +02001855 spin_lock(&fpq->lock);
Kirill Tkhai7407a102018-11-08 12:05:36 +03001856 req = NULL;
1857 if (fpq->connected)
1858 req = request_find(fpq, oh.unique & ~FUSE_INT_REQ_BIT);
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08001859
Kirill Tkhai7407a102018-11-08 12:05:36 +03001860 err = -ENOENT;
1861 if (!req) {
1862 spin_unlock(&fpq->lock);
1863 goto copy_finish;
1864 }
Miklos Szeredi334f4852005-09-09 13:10:27 -07001865
Kirill Tkhai3a5358d2018-09-11 13:12:05 +03001866 /* Is it an interrupt reply ID? */
1867 if (oh.unique & FUSE_INT_REQ_BIT) {
Kirill Tkhaid2d2d4f2018-09-25 12:52:42 +03001868 __fuse_get_request(req);
Miklos Szeredi45a91cb2015-07-01 16:26:06 +02001869 spin_unlock(&fpq->lock);
1870
Kirill Tkhai7407a102018-11-08 12:05:36 +03001871 err = 0;
1872 if (nbytes != sizeof(struct fuse_out_header))
1873 err = -EINVAL;
1874 else if (oh.error == -ENOSYS)
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001875 fc->no_interrupt = 1;
1876 else if (oh.error == -EAGAIN)
Kirill Tkhaib7829112018-11-08 12:05:42 +03001877 err = queue_interrupt(&fc->iq, req);
Kirill Tkhai7407a102018-11-08 12:05:36 +03001878
Kirill Tkhaid2d2d4f2018-09-25 12:52:42 +03001879 fuse_put_request(fc, req);
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001880
Kirill Tkhai7407a102018-11-08 12:05:36 +03001881 goto copy_finish;
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001882 }
1883
Miklos Szeredi33e14b42015-07-01 16:26:01 +02001884 clear_bit(FR_SENT, &req->flags);
Miklos Szeredi3a2b5b92015-07-01 16:26:04 +02001885 list_move(&req->list, &fpq->io);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001886 req->out.h = oh;
Miklos Szeredi825d6d32015-07-01 16:25:58 +02001887 set_bit(FR_LOCKED, &req->flags);
Miklos Szeredi45a91cb2015-07-01 16:26:06 +02001888 spin_unlock(&fpq->lock);
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001889 cs->req = req;
Miklos Szeredid4993772019-09-10 15:04:11 +02001890 if (!req->args->page_replace)
Miklos Szeredice534fb2010-05-25 15:06:07 +02001891 cs->move_pages = 0;
Miklos Szeredi334f4852005-09-09 13:10:27 -07001892
Miklos Szeredid4993772019-09-10 15:04:11 +02001893 if (oh.error)
1894 err = nbytes != sizeof(oh) ? -EINVAL : 0;
1895 else
1896 err = copy_out_args(cs, req->args, nbytes);
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001897 fuse_copy_finish(cs);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001898
Miklos Szeredi45a91cb2015-07-01 16:26:06 +02001899 spin_lock(&fpq->lock);
Miklos Szeredi825d6d32015-07-01 16:25:58 +02001900 clear_bit(FR_LOCKED, &req->flags);
Miklos Szeredie96edd92015-07-01 16:26:04 +02001901 if (!fpq->connected)
Miklos Szeredi0d8e84b2015-07-01 16:25:58 +02001902 err = -ENOENT;
1903 else if (err)
Miklos Szeredi334f4852005-09-09 13:10:27 -07001904 req->out.h.error = -EIO;
Miklos Szeredi77cd9d42015-07-01 16:26:06 +02001905 if (!test_bit(FR_PRIVATE, &req->flags))
1906 list_del_init(&req->list);
Miklos Szeredi45a91cb2015-07-01 16:26:06 +02001907 spin_unlock(&fpq->lock);
Miklos Szeredi46c34a32015-07-01 16:26:07 +02001908
Stefan Hajnoczi04ec5af2018-06-21 09:33:40 +01001909 fuse_request_end(fc, req);
Kirill Tkhai7407a102018-11-08 12:05:36 +03001910out:
Miklos Szeredi334f4852005-09-09 13:10:27 -07001911 return err ? err : nbytes;
1912
Kirill Tkhai7407a102018-11-08 12:05:36 +03001913copy_finish:
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001914 fuse_copy_finish(cs);
Kirill Tkhai7407a102018-11-08 12:05:36 +03001915 goto out;
Miklos Szeredi334f4852005-09-09 13:10:27 -07001916}
1917
Al Virofbdbacc2015-04-03 21:53:39 -04001918static ssize_t fuse_dev_write(struct kiocb *iocb, struct iov_iter *from)
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001919{
1920 struct fuse_copy_state cs;
Miklos Szeredicc080e92015-07-01 16:26:08 +02001921 struct fuse_dev *fud = fuse_get_dev(iocb->ki_filp);
1922
1923 if (!fud)
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001924 return -EPERM;
1925
Al Virofbdbacc2015-04-03 21:53:39 -04001926 if (!iter_is_iovec(from))
1927 return -EINVAL;
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001928
Miklos Szeredidc008092015-07-01 16:25:58 +02001929 fuse_copy_init(&cs, 0, from);
Al Virofbdbacc2015-04-03 21:53:39 -04001930
Miklos Szeredic36960462015-07-01 16:26:09 +02001931 return fuse_dev_do_write(fud, &cs, iov_iter_count(from));
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001932}
1933
1934static ssize_t fuse_dev_splice_write(struct pipe_inode_info *pipe,
1935 struct file *out, loff_t *ppos,
1936 size_t len, unsigned int flags)
1937{
David Howells8cefc102019-11-15 13:30:32 +00001938 unsigned int head, tail, mask, count;
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001939 unsigned nbuf;
1940 unsigned idx;
1941 struct pipe_buffer *bufs;
1942 struct fuse_copy_state cs;
Miklos Szeredicc080e92015-07-01 16:26:08 +02001943 struct fuse_dev *fud;
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001944 size_t rem;
1945 ssize_t ret;
1946
Miklos Szeredicc080e92015-07-01 16:26:08 +02001947 fud = fuse_get_dev(out);
1948 if (!fud)
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001949 return -EPERM;
1950
Andrey Ryabinina2477b02018-07-17 19:00:33 +03001951 pipe_lock(pipe);
1952
David Howells8cefc102019-11-15 13:30:32 +00001953 head = pipe->head;
1954 tail = pipe->tail;
1955 mask = pipe->ring_size - 1;
1956 count = head - tail;
1957
1958 bufs = kvmalloc_array(count, sizeof(struct pipe_buffer), GFP_KERNEL);
Andrey Ryabinina2477b02018-07-17 19:00:33 +03001959 if (!bufs) {
1960 pipe_unlock(pipe);
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001961 return -ENOMEM;
Andrey Ryabinina2477b02018-07-17 19:00:33 +03001962 }
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001963
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001964 nbuf = 0;
1965 rem = 0;
David Howells8cefc102019-11-15 13:30:32 +00001966 for (idx = tail; idx < head && rem < len; idx++)
1967 rem += pipe->bufs[idx & mask].len;
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001968
1969 ret = -EINVAL;
Matthew Wilcox15fab632019-04-05 14:02:10 -07001970 if (rem < len)
1971 goto out_free;
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001972
1973 rem = len;
1974 while (rem) {
1975 struct pipe_buffer *ibuf;
1976 struct pipe_buffer *obuf;
1977
David Howells8cefc102019-11-15 13:30:32 +00001978 BUG_ON(nbuf >= pipe->ring_size);
1979 BUG_ON(tail == head);
1980 ibuf = &pipe->bufs[tail & mask];
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001981 obuf = &bufs[nbuf];
1982
1983 if (rem >= ibuf->len) {
1984 *obuf = *ibuf;
1985 ibuf->ops = NULL;
David Howells8cefc102019-11-15 13:30:32 +00001986 tail++;
1987 pipe->tail = tail;
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001988 } else {
Matthew Wilcox15fab632019-04-05 14:02:10 -07001989 if (!pipe_buf_get(pipe, ibuf))
1990 goto out_free;
1991
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001992 *obuf = *ibuf;
1993 obuf->flags &= ~PIPE_BUF_FLAG_GIFT;
1994 obuf->len = rem;
1995 ibuf->offset += obuf->len;
1996 ibuf->len -= obuf->len;
1997 }
1998 nbuf++;
1999 rem -= obuf->len;
2000 }
2001 pipe_unlock(pipe);
2002
Miklos Szeredidc008092015-07-01 16:25:58 +02002003 fuse_copy_init(&cs, 0, NULL);
Miklos Szeredidd3bb142010-05-25 15:06:06 +02002004 cs.pipebufs = bufs;
Al Viro6c09e942015-04-03 22:06:08 -04002005 cs.nr_segs = nbuf;
Miklos Szeredidd3bb142010-05-25 15:06:06 +02002006 cs.pipe = pipe;
2007
Miklos Szeredice534fb2010-05-25 15:06:07 +02002008 if (flags & SPLICE_F_MOVE)
2009 cs.move_pages = 1;
2010
Miklos Szeredic36960462015-07-01 16:26:09 +02002011 ret = fuse_dev_do_write(fud, &cs, len);
Miklos Szeredidd3bb142010-05-25 15:06:06 +02002012
Jann Horn95099412019-01-12 02:39:05 +01002013 pipe_lock(pipe);
Matthew Wilcox15fab632019-04-05 14:02:10 -07002014out_free:
Miklos Szeredia7796382016-09-27 10:45:12 +02002015 for (idx = 0; idx < nbuf; idx++)
2016 pipe_buf_release(pipe, &bufs[idx]);
Jann Horn95099412019-01-12 02:39:05 +01002017 pipe_unlock(pipe);
Miklos Szeredia7796382016-09-27 10:45:12 +02002018
Andrey Ryabinind6d931a2018-07-17 19:00:34 +03002019 kvfree(bufs);
Miklos Szeredidd3bb142010-05-25 15:06:06 +02002020 return ret;
2021}
2022
Al Viro076ccb72017-07-03 01:02:18 -04002023static __poll_t fuse_dev_poll(struct file *file, poll_table *wait)
Miklos Szeredi334f4852005-09-09 13:10:27 -07002024{
Linus Torvaldsa9a08842018-02-11 14:34:03 -08002025 __poll_t mask = EPOLLOUT | EPOLLWRNORM;
Miklos Szeredif88996a2015-07-01 16:26:01 +02002026 struct fuse_iqueue *fiq;
Miklos Szeredicc080e92015-07-01 16:26:08 +02002027 struct fuse_dev *fud = fuse_get_dev(file);
2028
2029 if (!fud)
Linus Torvaldsa9a08842018-02-11 14:34:03 -08002030 return EPOLLERR;
Miklos Szeredi334f4852005-09-09 13:10:27 -07002031
Miklos Szeredicc080e92015-07-01 16:26:08 +02002032 fiq = &fud->fc->iq;
Miklos Szeredif88996a2015-07-01 16:26:01 +02002033 poll_wait(file, &fiq->waitq, wait);
Miklos Szeredi334f4852005-09-09 13:10:27 -07002034
Eric Biggers76e43c82019-09-08 20:15:18 -07002035 spin_lock(&fiq->lock);
Miklos Szeredie16714d2015-07-01 16:26:01 +02002036 if (!fiq->connected)
Linus Torvaldsa9a08842018-02-11 14:34:03 -08002037 mask = EPOLLERR;
Miklos Szeredif88996a2015-07-01 16:26:01 +02002038 else if (request_pending(fiq))
Linus Torvaldsa9a08842018-02-11 14:34:03 -08002039 mask |= EPOLLIN | EPOLLRDNORM;
Eric Biggers76e43c82019-09-08 20:15:18 -07002040 spin_unlock(&fiq->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -07002041
2042 return mask;
2043}
2044
Kirill Tkhai34061752018-11-06 12:15:20 +03002045/* Abort all requests on the given list (pending or processing) */
Miklos Szeredi334f4852005-09-09 13:10:27 -07002046static void end_requests(struct fuse_conn *fc, struct list_head *head)
2047{
2048 while (!list_empty(head)) {
2049 struct fuse_req *req;
2050 req = list_entry(head->next, struct fuse_req, list);
Miklos Szeredi334f4852005-09-09 13:10:27 -07002051 req->out.h.error = -ECONNABORTED;
Miklos Szeredi33e14b42015-07-01 16:26:01 +02002052 clear_bit(FR_SENT, &req->flags);
Miklos Szeredif377cb72015-07-01 16:26:04 +02002053 list_del_init(&req->list);
Stefan Hajnoczi04ec5af2018-06-21 09:33:40 +01002054 fuse_request_end(fc, req);
Miklos Szeredi334f4852005-09-09 13:10:27 -07002055 }
2056}
2057
Bryan Green357ccf22011-03-01 16:43:52 -08002058static void end_polls(struct fuse_conn *fc)
2059{
2060 struct rb_node *p;
2061
2062 p = rb_first(&fc->polled_files);
2063
2064 while (p) {
2065 struct fuse_file *ff;
2066 ff = rb_entry(p, struct fuse_file, polled_node);
2067 wake_up_interruptible_all(&ff->poll_wait);
2068
2069 p = rb_next(p);
2070 }
2071}
2072
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002073/*
2074 * Abort all requests.
2075 *
Miklos Szeredib716d422015-07-01 16:25:59 +02002076 * Emergency exit in case of a malicious or accidental deadlock, or just a hung
2077 * filesystem.
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002078 *
Miklos Szeredib716d422015-07-01 16:25:59 +02002079 * The same effect is usually achievable through killing the filesystem daemon
2080 * and all users of the filesystem. The exception is the combination of an
2081 * asynchronous request and the tricky deadlock (see
2082 * Documentation/filesystems/fuse.txt).
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002083 *
Miklos Szeredib716d422015-07-01 16:25:59 +02002084 * Aborting requests under I/O goes as follows: 1: Separate out unlocked
2085 * requests, they should be finished off immediately. Locked requests will be
2086 * finished after unlock; see unlock_request(). 2: Finish off the unlocked
2087 * requests. It is possible that some request will finish before we can. This
2088 * is OK, the request will in that case be removed from the list before we touch
2089 * it.
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002090 */
Miklos Szeredieb98e3bd2019-01-24 10:40:16 +01002091void fuse_abort_conn(struct fuse_conn *fc)
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002092{
Miklos Szeredif88996a2015-07-01 16:26:01 +02002093 struct fuse_iqueue *fiq = &fc->iq;
2094
Miklos Szeredid7133112006-04-10 22:54:55 -07002095 spin_lock(&fc->lock);
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002096 if (fc->connected) {
Miklos Szeredic36960462015-07-01 16:26:09 +02002097 struct fuse_dev *fud;
Miklos Szeredib716d422015-07-01 16:25:59 +02002098 struct fuse_req *req, *next;
Miklos Szeredi75f3ee42018-07-26 16:13:12 +02002099 LIST_HEAD(to_end);
Kirill Tkhaibe2ff422018-09-11 13:12:14 +03002100 unsigned int i;
Miklos Szeredib716d422015-07-01 16:25:59 +02002101
Kirill Tkhai63825b42018-08-27 18:29:56 +03002102 /* Background queuing checks fc->connected under bg_lock */
2103 spin_lock(&fc->bg_lock);
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002104 fc->connected = 0;
Kirill Tkhai63825b42018-08-27 18:29:56 +03002105 spin_unlock(&fc->bg_lock);
2106
Miklos Szeredi9759bd512015-01-06 10:45:35 +01002107 fuse_set_initialized(fc);
Miklos Szeredic36960462015-07-01 16:26:09 +02002108 list_for_each_entry(fud, &fc->devices, entry) {
2109 struct fuse_pqueue *fpq = &fud->pq;
2110
2111 spin_lock(&fpq->lock);
2112 fpq->connected = 0;
2113 list_for_each_entry_safe(req, next, &fpq->io, list) {
2114 req->out.h.error = -ECONNABORTED;
2115 spin_lock(&req->waitq.lock);
2116 set_bit(FR_ABORTED, &req->flags);
2117 if (!test_bit(FR_LOCKED, &req->flags)) {
2118 set_bit(FR_PRIVATE, &req->flags);
Miklos Szeredi87114372018-07-26 16:13:11 +02002119 __fuse_get_request(req);
Miklos Szeredi75f3ee42018-07-26 16:13:12 +02002120 list_move(&req->list, &to_end);
Miklos Szeredic36960462015-07-01 16:26:09 +02002121 }
2122 spin_unlock(&req->waitq.lock);
Miklos Szeredi77cd9d42015-07-01 16:26:06 +02002123 }
Kirill Tkhaibe2ff422018-09-11 13:12:14 +03002124 for (i = 0; i < FUSE_PQ_HASH_SIZE; i++)
2125 list_splice_tail_init(&fpq->processing[i],
2126 &to_end);
Miklos Szeredic36960462015-07-01 16:26:09 +02002127 spin_unlock(&fpq->lock);
Miklos Szeredib716d422015-07-01 16:25:59 +02002128 }
Kirill Tkhaiae2dffa2018-08-27 18:29:46 +03002129 spin_lock(&fc->bg_lock);
2130 fc->blocked = 0;
Miklos Szeredi41f98272015-07-01 16:25:59 +02002131 fc->max_background = UINT_MAX;
2132 flush_bg_queue(fc);
Kirill Tkhaiae2dffa2018-08-27 18:29:46 +03002133 spin_unlock(&fc->bg_lock);
Miklos Szeredi8c911892015-07-01 16:26:02 +02002134
Eric Biggers76e43c82019-09-08 20:15:18 -07002135 spin_lock(&fiq->lock);
Miklos Szeredi8c911892015-07-01 16:26:02 +02002136 fiq->connected = 0;
Miklos Szeredi75f3ee42018-07-26 16:13:12 +02002137 list_for_each_entry(req, &fiq->pending, list)
Tahsin Erdogana8a86d72017-01-12 12:04:04 -08002138 clear_bit(FR_PENDING, &req->flags);
Miklos Szeredi75f3ee42018-07-26 16:13:12 +02002139 list_splice_tail_init(&fiq->pending, &to_end);
Miklos Szeredi8c911892015-07-01 16:26:02 +02002140 while (forget_pending(fiq))
Vivek Goyal4388c5a2019-06-05 15:50:43 -04002141 kfree(fuse_dequeue_forget(fiq, 1, NULL));
Eric Biggers76e43c82019-09-08 20:15:18 -07002142 wake_up_all(&fiq->waitq);
2143 spin_unlock(&fiq->lock);
Miklos Szeredi8c911892015-07-01 16:26:02 +02002144 kill_fasync(&fiq->fasync, SIGIO, POLL_IN);
Miklos Szerediee314a82015-07-01 16:26:08 +02002145 end_polls(fc);
2146 wake_up_all(&fc->blocked_waitq);
2147 spin_unlock(&fc->lock);
Miklos Szeredi8c911892015-07-01 16:26:02 +02002148
Miklos Szeredi75f3ee42018-07-26 16:13:12 +02002149 end_requests(fc, &to_end);
Miklos Szerediee314a82015-07-01 16:26:08 +02002150 } else {
2151 spin_unlock(&fc->lock);
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002152 }
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002153}
Tejun Heo08cbf542009-04-14 10:54:53 +09002154EXPORT_SYMBOL_GPL(fuse_abort_conn);
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002155
Miklos Szeredib8f95e52018-07-26 16:13:11 +02002156void fuse_wait_aborted(struct fuse_conn *fc)
2157{
Miklos Szeredi2d84a2d2018-11-09 15:52:16 +01002158 /* matches implicit memory barrier in fuse_drop_waiting() */
2159 smp_mb();
Miklos Szeredib8f95e52018-07-26 16:13:11 +02002160 wait_event(fc->blocked_waitq, atomic_read(&fc->num_waiting) == 0);
2161}
2162
Tejun Heo08cbf542009-04-14 10:54:53 +09002163int fuse_dev_release(struct inode *inode, struct file *file)
Miklos Szeredi334f4852005-09-09 13:10:27 -07002164{
Miklos Szeredicc080e92015-07-01 16:26:08 +02002165 struct fuse_dev *fud = fuse_get_dev(file);
2166
2167 if (fud) {
2168 struct fuse_conn *fc = fud->fc;
Miklos Szeredic36960462015-07-01 16:26:09 +02002169 struct fuse_pqueue *fpq = &fud->pq;
Miklos Szeredi45ff3502018-07-26 16:13:11 +02002170 LIST_HEAD(to_end);
Kirill Tkhaibe2ff422018-09-11 13:12:14 +03002171 unsigned int i;
Miklos Szeredicc080e92015-07-01 16:26:08 +02002172
Miklos Szeredi45ff3502018-07-26 16:13:11 +02002173 spin_lock(&fpq->lock);
Miklos Szeredic36960462015-07-01 16:26:09 +02002174 WARN_ON(!list_empty(&fpq->io));
Kirill Tkhaibe2ff422018-09-11 13:12:14 +03002175 for (i = 0; i < FUSE_PQ_HASH_SIZE; i++)
2176 list_splice_init(&fpq->processing[i], &to_end);
Miklos Szeredi45ff3502018-07-26 16:13:11 +02002177 spin_unlock(&fpq->lock);
2178
2179 end_requests(fc, &to_end);
2180
Miklos Szeredic36960462015-07-01 16:26:09 +02002181 /* Are we the last open device? */
2182 if (atomic_dec_and_test(&fc->dev_count)) {
2183 WARN_ON(fc->iq.fasync != NULL);
Miklos Szeredieb98e3bd2019-01-24 10:40:16 +01002184 fuse_abort_conn(fc);
Miklos Szeredic36960462015-07-01 16:26:09 +02002185 }
Miklos Szeredicc080e92015-07-01 16:26:08 +02002186 fuse_dev_free(fud);
Jeff Dike385a17b2006-04-10 22:54:52 -07002187 }
Miklos Szeredi334f4852005-09-09 13:10:27 -07002188 return 0;
2189}
Tejun Heo08cbf542009-04-14 10:54:53 +09002190EXPORT_SYMBOL_GPL(fuse_dev_release);
Miklos Szeredi334f4852005-09-09 13:10:27 -07002191
Jeff Dike385a17b2006-04-10 22:54:52 -07002192static int fuse_dev_fasync(int fd, struct file *file, int on)
2193{
Miklos Szeredicc080e92015-07-01 16:26:08 +02002194 struct fuse_dev *fud = fuse_get_dev(file);
2195
2196 if (!fud)
Miklos Szeredia87046d2006-04-10 22:54:56 -07002197 return -EPERM;
Jeff Dike385a17b2006-04-10 22:54:52 -07002198
2199 /* No locking - fasync_helper does its own locking */
Miklos Szeredicc080e92015-07-01 16:26:08 +02002200 return fasync_helper(fd, file, on, &fud->fc->iq.fasync);
Jeff Dike385a17b2006-04-10 22:54:52 -07002201}
2202
Miklos Szeredi00c570f2015-07-01 16:26:08 +02002203static int fuse_device_clone(struct fuse_conn *fc, struct file *new)
2204{
Miklos Szeredicc080e92015-07-01 16:26:08 +02002205 struct fuse_dev *fud;
2206
Miklos Szeredi00c570f2015-07-01 16:26:08 +02002207 if (new->private_data)
2208 return -EINVAL;
2209
Vivek Goyal0cd1eb9a2019-03-06 16:51:40 -05002210 fud = fuse_dev_alloc_install(fc);
Miklos Szeredicc080e92015-07-01 16:26:08 +02002211 if (!fud)
2212 return -ENOMEM;
2213
2214 new->private_data = fud;
Miklos Szeredic36960462015-07-01 16:26:09 +02002215 atomic_inc(&fc->dev_count);
Miklos Szeredi00c570f2015-07-01 16:26:08 +02002216
2217 return 0;
2218}
2219
2220static long fuse_dev_ioctl(struct file *file, unsigned int cmd,
2221 unsigned long arg)
2222{
2223 int err = -ENOTTY;
2224
2225 if (cmd == FUSE_DEV_IOC_CLONE) {
2226 int oldfd;
2227
2228 err = -EFAULT;
2229 if (!get_user(oldfd, (__u32 __user *) arg)) {
2230 struct file *old = fget(oldfd);
2231
2232 err = -EINVAL;
2233 if (old) {
Jann Horn8ed1f0e2015-08-16 20:27:01 +02002234 struct fuse_dev *fud = NULL;
2235
2236 /*
2237 * Check against file->f_op because CUSE
2238 * uses the same ioctl handler.
2239 */
2240 if (old->f_op == file->f_op &&
2241 old->f_cred->user_ns == file->f_cred->user_ns)
2242 fud = fuse_get_dev(old);
Miklos Szeredi00c570f2015-07-01 16:26:08 +02002243
Miklos Szeredicc080e92015-07-01 16:26:08 +02002244 if (fud) {
Miklos Szeredi00c570f2015-07-01 16:26:08 +02002245 mutex_lock(&fuse_mutex);
Miklos Szeredicc080e92015-07-01 16:26:08 +02002246 err = fuse_device_clone(fud->fc, file);
Miklos Szeredi00c570f2015-07-01 16:26:08 +02002247 mutex_unlock(&fuse_mutex);
2248 }
2249 fput(old);
2250 }
2251 }
2252 }
2253 return err;
2254}
2255
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -08002256const struct file_operations fuse_dev_operations = {
Miklos Szeredi334f4852005-09-09 13:10:27 -07002257 .owner = THIS_MODULE,
Tom Van Braeckel94e4fe22015-01-12 05:22:16 +01002258 .open = fuse_dev_open,
Miklos Szeredi334f4852005-09-09 13:10:27 -07002259 .llseek = no_llseek,
Al Virofbdbacc2015-04-03 21:53:39 -04002260 .read_iter = fuse_dev_read,
Miklos Szeredic3021622010-05-25 15:06:07 +02002261 .splice_read = fuse_dev_splice_read,
Al Virofbdbacc2015-04-03 21:53:39 -04002262 .write_iter = fuse_dev_write,
Miklos Szeredidd3bb142010-05-25 15:06:06 +02002263 .splice_write = fuse_dev_splice_write,
Miklos Szeredi334f4852005-09-09 13:10:27 -07002264 .poll = fuse_dev_poll,
2265 .release = fuse_dev_release,
Jeff Dike385a17b2006-04-10 22:54:52 -07002266 .fasync = fuse_dev_fasync,
Miklos Szeredi00c570f2015-07-01 16:26:08 +02002267 .unlocked_ioctl = fuse_dev_ioctl,
2268 .compat_ioctl = fuse_dev_ioctl,
Miklos Szeredi334f4852005-09-09 13:10:27 -07002269};
Tejun Heo08cbf542009-04-14 10:54:53 +09002270EXPORT_SYMBOL_GPL(fuse_dev_operations);
Miklos Szeredi334f4852005-09-09 13:10:27 -07002271
2272static struct miscdevice fuse_miscdevice = {
2273 .minor = FUSE_MINOR,
2274 .name = "fuse",
2275 .fops = &fuse_dev_operations,
2276};
2277
2278int __init fuse_dev_init(void)
2279{
2280 int err = -ENOMEM;
2281 fuse_req_cachep = kmem_cache_create("fuse_request",
2282 sizeof(struct fuse_req),
Paul Mundt20c2df82007-07-20 10:11:58 +09002283 0, 0, NULL);
Miklos Szeredi334f4852005-09-09 13:10:27 -07002284 if (!fuse_req_cachep)
2285 goto out;
2286
2287 err = misc_register(&fuse_miscdevice);
2288 if (err)
2289 goto out_cache_clean;
2290
2291 return 0;
2292
2293 out_cache_clean:
2294 kmem_cache_destroy(fuse_req_cachep);
2295 out:
2296 return err;
2297}
2298
2299void fuse_dev_cleanup(void)
2300{
2301 misc_deregister(&fuse_miscdevice);
2302 kmem_cache_destroy(fuse_req_cachep);
2303}