blob: c93b0154dd514997c269b47e23b2bc6d4749d4de [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>
Daniel Rosenbergaca26512016-04-22 00:00:48 -070017#include <linux/namei.h>
Miklos Szeredi334f4852005-09-09 13:10:27 -070018#include <linux/pagemap.h>
19#include <linux/file.h>
20#include <linux/slab.h>
Miklos Szeredidd3bb142010-05-25 15:06:06 +020021#include <linux/pipe_fs_i.h>
Miklos Szeredice534fb2010-05-25 15:06:07 +020022#include <linux/swap.h>
23#include <linux/splice.h>
Seth Forshee0b6e9ea2014-07-02 16:29:19 -050024#include <linux/sched.h>
Miklos Szeredi334f4852005-09-09 13:10:27 -070025
26MODULE_ALIAS_MISCDEV(FUSE_MINOR);
Kay Sievers578454f2010-05-20 18:07:20 +020027MODULE_ALIAS("devname:fuse");
Miklos Szeredi334f4852005-09-09 13:10:27 -070028
Kirill Tkhaic59fd852018-09-11 13:11:56 +030029/* Ordinary requests have even IDs, while interrupts IDs are odd */
30#define FUSE_INT_REQ_BIT (1ULL << 0)
31#define FUSE_REQ_ID_STEP (1ULL << 1)
32
Christoph Lametere18b8902006-12-06 20:33:20 -080033static struct kmem_cache *fuse_req_cachep;
Miklos Szeredi334f4852005-09-09 13:10:27 -070034
Miklos Szeredicc080e92015-07-01 16:26:08 +020035static struct fuse_dev *fuse_get_dev(struct file *file)
Miklos Szeredi334f4852005-09-09 13:10:27 -070036{
Miklos Szeredi0720b312006-04-10 22:54:55 -070037 /*
38 * Lockless access is OK, because file->private data is set
39 * once during mount and is valid until the file is released.
40 */
Mark Rutland6aa7de02017-10-23 14:07:29 -070041 return READ_ONCE(file->private_data);
Miklos Szeredi334f4852005-09-09 13:10:27 -070042}
43
Max Reitzfcee2162020-05-06 17:44:12 +020044static void fuse_request_init(struct fuse_mount *fm, struct fuse_req *req)
Miklos Szeredi334f4852005-09-09 13:10:27 -070045{
Miklos Szeredi334f4852005-09-09 13:10:27 -070046 INIT_LIST_HEAD(&req->list);
Miklos Szeredia4d27e72006-06-25 05:48:54 -070047 INIT_LIST_HEAD(&req->intr_entry);
Miklos Szeredi334f4852005-09-09 13:10:27 -070048 init_waitqueue_head(&req->waitq);
Elena Reshetovaec99f6d2017-03-03 11:04:04 +020049 refcount_set(&req->count, 1);
Miklos Szeredi33e14b42015-07-01 16:26:01 +020050 __set_bit(FR_PENDING, &req->flags);
Max Reitzfcee2162020-05-06 17:44:12 +020051 req->fm = fm;
Miklos Szeredi334f4852005-09-09 13:10:27 -070052}
53
Max Reitzfcee2162020-05-06 17:44:12 +020054static struct fuse_req *fuse_request_alloc(struct fuse_mount *fm, gfp_t flags)
Miklos Szeredi334f4852005-09-09 13:10:27 -070055{
Miklos Szeredi8a7aa282018-10-01 10:07:05 +020056 struct fuse_req *req = kmem_cache_zalloc(fuse_req_cachep, flags);
Miklos Szeredi72133942019-09-10 15:04:11 +020057 if (req)
Max Reitzfcee2162020-05-06 17:44:12 +020058 fuse_request_init(fm, req);
Maxim Patlasov4250c062012-10-26 19:48:07 +040059
Miklos Szeredi334f4852005-09-09 13:10:27 -070060 return req;
61}
Maxim Patlasov4250c062012-10-26 19:48:07 +040062
Miklos Szeredi66abc352019-09-10 15:04:11 +020063static void fuse_request_free(struct fuse_req *req)
Miklos Szeredie52a8252018-10-01 10:07:06 +020064{
Miklos Szeredi334f4852005-09-09 13:10:27 -070065 kmem_cache_free(fuse_req_cachep, req);
66}
67
Miklos Szeredi66abc352019-09-10 15:04:11 +020068static void __fuse_get_request(struct fuse_req *req)
Miklos Szeredi334f4852005-09-09 13:10:27 -070069{
Elena Reshetovaec99f6d2017-03-03 11:04:04 +020070 refcount_inc(&req->count);
Miklos Szeredi334f4852005-09-09 13:10:27 -070071}
72
73/* Must be called with > 1 refcount */
74static void __fuse_put_request(struct fuse_req *req)
75{
Elena Reshetovaec99f6d2017-03-03 11:04:04 +020076 refcount_dec(&req->count);
Miklos Szeredi334f4852005-09-09 13:10:27 -070077}
78
Miklos Szeredi9759bd512015-01-06 10:45:35 +010079void fuse_set_initialized(struct fuse_conn *fc)
80{
81 /* Make sure stores before this are seen on another CPU */
82 smp_wmb();
83 fc->initialized = 1;
84}
85
Maxim Patlasov0aada882013-03-21 18:02:28 +040086static bool fuse_block_alloc(struct fuse_conn *fc, bool for_background)
87{
88 return !fc->initialized || (for_background && fc->blocked);
89}
90
Miklos Szeredib8f95e52018-07-26 16:13:11 +020091static void fuse_drop_waiting(struct fuse_conn *fc)
92{
Miklos Szeredi2d84a2d2018-11-09 15:52:16 +010093 /*
94 * lockess check of fc->connected is okay, because atomic_dec_and_test()
95 * provides a memory barrier mached with the one in fuse_wait_aborted()
96 * to ensure no wake-up is missed.
97 */
98 if (atomic_dec_and_test(&fc->num_waiting) &&
99 !READ_ONCE(fc->connected)) {
Miklos Szeredib8f95e52018-07-26 16:13:11 +0200100 /* wake up aborters */
101 wake_up_all(&fc->blocked_waitq);
102 }
103}
104
Max Reitz8f622e92020-04-20 17:59:34 +0200105static void fuse_put_request(struct fuse_req *req);
Miklos Szeredi66abc352019-09-10 15:04:11 +0200106
Max Reitzfcee2162020-05-06 17:44:12 +0200107static struct fuse_req *fuse_get_req(struct fuse_mount *fm, bool for_background)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700108{
Max Reitzfcee2162020-05-06 17:44:12 +0200109 struct fuse_conn *fc = fm->fc;
Miklos Szeredi08a53cd2006-04-10 22:54:59 -0700110 struct fuse_req *req;
Miklos Szeredi08a53cd2006-04-10 22:54:59 -0700111 int err;
Miklos Szeredi9bc5ddd2006-04-11 21:16:09 +0200112 atomic_inc(&fc->num_waiting);
Maxim Patlasov0aada882013-03-21 18:02:28 +0400113
114 if (fuse_block_alloc(fc, for_background)) {
Maxim Patlasov0aada882013-03-21 18:02:28 +0400115 err = -EINTR;
Al Viro7d3a07f2016-07-19 03:08:27 -0400116 if (wait_event_killable_exclusive(fc->blocked_waitq,
117 !fuse_block_alloc(fc, for_background)))
Maxim Patlasov0aada882013-03-21 18:02:28 +0400118 goto out;
119 }
Miklos Szeredi9759bd512015-01-06 10:45:35 +0100120 /* Matches smp_wmb() in fuse_set_initialized() */
121 smp_rmb();
Miklos Szeredi08a53cd2006-04-10 22:54:59 -0700122
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700123 err = -ENOTCONN;
124 if (!fc->connected)
125 goto out;
126
Miklos Szeredide155222015-07-01 16:25:57 +0200127 err = -ECONNREFUSED;
128 if (fc->conn_error)
129 goto out;
130
Max Reitzfcee2162020-05-06 17:44:12 +0200131 req = fuse_request_alloc(fm, GFP_KERNEL);
Miklos Szeredi9bc5ddd2006-04-11 21:16:09 +0200132 err = -ENOMEM;
Maxim Patlasov722d2be2013-03-21 18:02:36 +0400133 if (!req) {
134 if (for_background)
135 wake_up(&fc->blocked_waitq);
Miklos Szeredi9bc5ddd2006-04-11 21:16:09 +0200136 goto out;
Maxim Patlasov722d2be2013-03-21 18:02:36 +0400137 }
Miklos Szeredi334f4852005-09-09 13:10:27 -0700138
Eric W. Biederman8cb08322018-02-21 11:18:07 -0600139 req->in.h.uid = from_kuid(fc->user_ns, current_fsuid());
140 req->in.h.gid = from_kgid(fc->user_ns, current_fsgid());
Eric W. Biedermanc9582eb2018-02-21 10:52:06 -0600141 req->in.h.pid = pid_nr_ns(task_pid(current), fc->pid_ns);
142
Miklos Szeredi825d6d32015-07-01 16:25:58 +0200143 __set_bit(FR_WAITING, &req->flags);
144 if (for_background)
145 __set_bit(FR_BACKGROUND, &req->flags);
146
Eric W. Biedermanc9582eb2018-02-21 10:52:06 -0600147 if (unlikely(req->in.h.uid == ((uid_t)-1) ||
148 req->in.h.gid == ((gid_t)-1))) {
Max Reitz8f622e92020-04-20 17:59:34 +0200149 fuse_put_request(req);
Eric W. Biedermanc9582eb2018-02-21 10:52:06 -0600150 return ERR_PTR(-EOVERFLOW);
151 }
Miklos Szeredi334f4852005-09-09 13:10:27 -0700152 return req;
Miklos Szeredi9bc5ddd2006-04-11 21:16:09 +0200153
154 out:
Miklos Szeredib8f95e52018-07-26 16:13:11 +0200155 fuse_drop_waiting(fc);
Miklos Szeredi9bc5ddd2006-04-11 21:16:09 +0200156 return ERR_PTR(err);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700157}
Maxim Patlasov8b41e672013-03-21 18:02:04 +0400158
Max Reitz8f622e92020-04-20 17:59:34 +0200159static void fuse_put_request(struct fuse_req *req)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700160{
Max Reitzfcee2162020-05-06 17:44:12 +0200161 struct fuse_conn *fc = req->fm->fc;
Max Reitz8f622e92020-04-20 17:59:34 +0200162
Elena Reshetovaec99f6d2017-03-03 11:04:04 +0200163 if (refcount_dec_and_test(&req->count)) {
Miklos Szeredi825d6d32015-07-01 16:25:58 +0200164 if (test_bit(FR_BACKGROUND, &req->flags)) {
Maxim Patlasov722d2be2013-03-21 18:02:36 +0400165 /*
166 * We get here in the unlikely case that a background
167 * request was allocated but not sent
168 */
Kirill Tkhaiae2dffa2018-08-27 18:29:46 +0300169 spin_lock(&fc->bg_lock);
Maxim Patlasov722d2be2013-03-21 18:02:36 +0400170 if (!fc->blocked)
171 wake_up(&fc->blocked_waitq);
Kirill Tkhaiae2dffa2018-08-27 18:29:46 +0300172 spin_unlock(&fc->bg_lock);
Maxim Patlasov722d2be2013-03-21 18:02:36 +0400173 }
174
Miklos Szeredi825d6d32015-07-01 16:25:58 +0200175 if (test_bit(FR_WAITING, &req->flags)) {
176 __clear_bit(FR_WAITING, &req->flags);
Miklos Szeredib8f95e52018-07-26 16:13:11 +0200177 fuse_drop_waiting(fc);
Miklos Szeredi73e0e732015-07-01 16:25:56 +0200178 }
Miklos Szeredi33649c92006-06-25 05:48:52 -0700179
Miklos Szeredi40ac7ab2019-09-10 15:04:08 +0200180 fuse_request_free(req);
Miklos Szeredi7128ec22006-02-04 23:27:40 -0800181 }
182}
183
Stefan Hajnoczi14d46d72018-06-21 09:34:25 +0100184unsigned int fuse_len_args(unsigned int numargs, struct fuse_arg *args)
Miklos Szeredid12def12008-02-06 01:38:39 -0800185{
186 unsigned nbytes = 0;
187 unsigned i;
188
189 for (i = 0; i < numargs; i++)
190 nbytes += args[i].size;
191
192 return nbytes;
193}
Stefan Hajnoczi14d46d72018-06-21 09:34:25 +0100194EXPORT_SYMBOL_GPL(fuse_len_args);
Miklos Szeredid12def12008-02-06 01:38:39 -0800195
Stefan Hajnoczi79d96ef2018-06-22 13:48:30 +0100196u64 fuse_get_unique(struct fuse_iqueue *fiq)
Miklos Szeredid12def12008-02-06 01:38:39 -0800197{
Kirill Tkhaic59fd852018-09-11 13:11:56 +0300198 fiq->reqctr += FUSE_REQ_ID_STEP;
199 return fiq->reqctr;
Miklos Szeredid12def12008-02-06 01:38:39 -0800200}
Stefan Hajnoczi79d96ef2018-06-22 13:48:30 +0100201EXPORT_SYMBOL_GPL(fuse_get_unique);
Miklos Szeredid12def12008-02-06 01:38:39 -0800202
Kirill Tkhaibe2ff422018-09-11 13:12:14 +0300203static unsigned int fuse_req_hash(u64 unique)
204{
205 return hash_long(unique & ~FUSE_INT_REQ_BIT, FUSE_PQ_HASH_BITS);
206}
207
Stefan Hajnocziae3aad72018-06-18 15:53:19 +0100208/**
209 * A new request is available, wake fiq->waitq
210 */
211static void fuse_dev_wake_and_unlock(struct fuse_iqueue *fiq)
212__releases(fiq->lock)
213{
214 wake_up(&fiq->waitq);
215 kill_fasync(&fiq->fasync, SIGIO, POLL_IN);
216 spin_unlock(&fiq->lock);
217}
218
219const struct fuse_iqueue_ops fuse_dev_fiq_ops = {
220 .wake_forget_and_unlock = fuse_dev_wake_and_unlock,
221 .wake_interrupt_and_unlock = fuse_dev_wake_and_unlock,
222 .wake_pending_and_unlock = fuse_dev_wake_and_unlock,
223};
224EXPORT_SYMBOL_GPL(fuse_dev_fiq_ops);
225
226static void queue_request_and_unlock(struct fuse_iqueue *fiq,
227 struct fuse_req *req)
228__releases(fiq->lock)
Miklos Szeredid12def12008-02-06 01:38:39 -0800229{
Miklos Szeredid12def12008-02-06 01:38:39 -0800230 req->in.h.len = sizeof(struct fuse_in_header) +
Stefan Hajnoczi14d46d72018-06-21 09:34:25 +0100231 fuse_len_args(req->args->in_numargs,
232 (struct fuse_arg *) req->args->in_args);
Miklos Szeredif88996a2015-07-01 16:26:01 +0200233 list_add_tail(&req->list, &fiq->pending);
Stefan Hajnocziae3aad72018-06-18 15:53:19 +0100234 fiq->ops->wake_pending_and_unlock(fiq);
Miklos Szeredid12def12008-02-06 01:38:39 -0800235}
236
Miklos Szeredi07e77dc2010-12-07 20:16:56 +0100237void fuse_queue_forget(struct fuse_conn *fc, struct fuse_forget_link *forget,
238 u64 nodeid, u64 nlookup)
239{
Miklos Szeredif88996a2015-07-01 16:26:01 +0200240 struct fuse_iqueue *fiq = &fc->iq;
241
Miklos Szeredi02c048b2010-12-07 20:16:56 +0100242 forget->forget_one.nodeid = nodeid;
243 forget->forget_one.nlookup = nlookup;
Miklos Szeredi07e77dc2010-12-07 20:16:56 +0100244
Eric Biggers76e43c82019-09-08 20:15:18 -0700245 spin_lock(&fiq->lock);
Miklos Szeredie16714d2015-07-01 16:26:01 +0200246 if (fiq->connected) {
Miklos Szeredif88996a2015-07-01 16:26:01 +0200247 fiq->forget_list_tail->next = forget;
248 fiq->forget_list_tail = forget;
Stefan Hajnocziae3aad72018-06-18 15:53:19 +0100249 fiq->ops->wake_forget_and_unlock(fiq);
Miklos Szeredi5dfcc872011-09-12 09:38:03 +0200250 } else {
251 kfree(forget);
Stefan Hajnocziae3aad72018-06-18 15:53:19 +0100252 spin_unlock(&fiq->lock);
Miklos Szeredi5dfcc872011-09-12 09:38:03 +0200253 }
Miklos Szeredi07e77dc2010-12-07 20:16:56 +0100254}
255
Miklos Szeredid12def12008-02-06 01:38:39 -0800256static void flush_bg_queue(struct fuse_conn *fc)
257{
Kirill Tkhaie2871792018-07-31 13:25:25 +0300258 struct fuse_iqueue *fiq = &fc->iq;
259
Csaba Henk7a6d3c82009-07-01 17:28:41 -0700260 while (fc->active_background < fc->max_background &&
Miklos Szeredid12def12008-02-06 01:38:39 -0800261 !list_empty(&fc->bg_queue)) {
262 struct fuse_req *req;
263
Kirill Tkhaie2871792018-07-31 13:25:25 +0300264 req = list_first_entry(&fc->bg_queue, struct fuse_req, list);
Miklos Szeredid12def12008-02-06 01:38:39 -0800265 list_del(&req->list);
266 fc->active_background++;
Eric Biggers76e43c82019-09-08 20:15:18 -0700267 spin_lock(&fiq->lock);
Miklos Szeredif88996a2015-07-01 16:26:01 +0200268 req->in.h.unique = fuse_get_unique(fiq);
Stefan Hajnocziae3aad72018-06-18 15:53:19 +0100269 queue_request_and_unlock(fiq, req);
Miklos Szeredid12def12008-02-06 01:38:39 -0800270 }
271}
272
Miklos Szeredi6dbbcb12006-04-26 10:49:06 +0200273/*
Miklos Szeredi334f4852005-09-09 13:10:27 -0700274 * This function is called when a request is finished. Either a reply
Miklos Szeredif9a28422006-06-25 05:48:53 -0700275 * has arrived or it was aborted (and not yet sent) or some error
Miklos Szeredif43b1552006-01-16 22:14:26 -0800276 * occurred during communication with userspace, or the device file
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700277 * was closed. The requester thread is woken up (if still waiting),
278 * the 'end' callback is called if given, else the reference to the
279 * request is released
Miklos Szeredi334f4852005-09-09 13:10:27 -0700280 */
Max Reitz8f622e92020-04-20 17:59:34 +0200281void fuse_request_end(struct fuse_req *req)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700282{
Max Reitzfcee2162020-05-06 17:44:12 +0200283 struct fuse_mount *fm = req->fm;
284 struct fuse_conn *fc = fm->fc;
Miklos Szeredi4ce60812015-07-01 16:26:02 +0200285 struct fuse_iqueue *fiq = &fc->iq;
Miklos Szeredi365ae712015-07-01 16:26:06 +0200286
Miklos Szerediefe28002015-07-01 16:26:07 +0200287 if (test_and_set_bit(FR_FINISHED, &req->flags))
Miklos Szeredib8f95e52018-07-26 16:13:11 +0200288 goto put_request;
Miklos Szeredi2b319d12019-10-21 09:11:40 +0200289
Kirill Tkhai217316a2018-11-08 12:05:25 +0300290 /*
291 * test_and_set_bit() implies smp_mb() between bit
Miklos Szeredib7d4f312021-08-04 13:22:58 +0200292 * changing and below FR_INTERRUPTED check. Pairs with
Kirill Tkhai217316a2018-11-08 12:05:25 +0300293 * smp_mb() from queue_interrupt().
294 */
Miklos Szeredib7d4f312021-08-04 13:22:58 +0200295 if (test_bit(FR_INTERRUPTED, &req->flags)) {
Eric Biggers76e43c82019-09-08 20:15:18 -0700296 spin_lock(&fiq->lock);
Kirill Tkhai217316a2018-11-08 12:05:25 +0300297 list_del_init(&req->intr_entry);
Eric Biggers76e43c82019-09-08 20:15:18 -0700298 spin_unlock(&fiq->lock);
Kirill Tkhai217316a2018-11-08 12:05:25 +0300299 }
Miklos Szeredi33e14b42015-07-01 16:26:01 +0200300 WARN_ON(test_bit(FR_PENDING, &req->flags));
301 WARN_ON(test_bit(FR_SENT, &req->flags));
Miklos Szeredi825d6d32015-07-01 16:25:58 +0200302 if (test_bit(FR_BACKGROUND, &req->flags)) {
Kirill Tkhaiae2dffa2018-08-27 18:29:46 +0300303 spin_lock(&fc->bg_lock);
Miklos Szeredi825d6d32015-07-01 16:25:58 +0200304 clear_bit(FR_BACKGROUND, &req->flags);
Miklos Szeredi908a5722018-09-28 16:43:22 +0200305 if (fc->num_background == fc->max_background) {
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700306 fc->blocked = 0;
Maxim Patlasov722d2be2013-03-21 18:02:36 +0400307 wake_up(&fc->blocked_waitq);
Miklos Szeredi908a5722018-09-28 16:43:22 +0200308 } else if (!fc->blocked) {
309 /*
310 * Wake up next waiter, if any. It's okay to use
311 * waitqueue_active(), as we've already synced up
312 * fc->blocked with waiters with the wake_up() call
313 * above.
314 */
315 if (waitqueue_active(&fc->blocked_waitq))
316 wake_up(&fc->blocked_waitq);
317 }
Maxim Patlasov722d2be2013-03-21 18:02:36 +0400318
Max Reitzfcee2162020-05-06 17:44:12 +0200319 if (fc->num_background == fc->congestion_threshold && fm->sb) {
320 clear_bdi_congested(fm->sb->s_bdi, BLK_RW_SYNC);
321 clear_bdi_congested(fm->sb->s_bdi, BLK_RW_ASYNC);
Miklos Szeredif92b99b2007-10-16 23:30:59 -0700322 }
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700323 fc->num_background--;
Miklos Szeredid12def12008-02-06 01:38:39 -0800324 fc->active_background--;
325 flush_bg_queue(fc);
Kirill Tkhaiae2dffa2018-08-27 18:29:46 +0300326 spin_unlock(&fc->bg_lock);
Kirill Tkhai5e0fed72018-11-08 12:05:31 +0300327 } else {
328 /* Wake up waiter sleeping in request_wait_answer() */
329 wake_up(&req->waitq);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700330 }
Kirill Tkhai5e0fed72018-11-08 12:05:31 +0300331
Miklos Szeredi3e8cb8b2020-02-13 09:16:07 +0100332 if (test_bit(FR_ASYNC, &req->flags))
Max Reitzfcee2162020-05-06 17:44:12 +0200333 req->args->end(fm, req->args, req->out.h.error);
Miklos Szeredib8f95e52018-07-26 16:13:11 +0200334put_request:
Max Reitz8f622e92020-04-20 17:59:34 +0200335 fuse_put_request(req);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700336}
Stefan Hajnoczi04ec5af2018-06-21 09:33:40 +0100337EXPORT_SYMBOL_GPL(fuse_request_end);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700338
Max Reitz8f622e92020-04-20 17:59:34 +0200339static int queue_interrupt(struct fuse_req *req)
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700340{
Max Reitzfcee2162020-05-06 17:44:12 +0200341 struct fuse_iqueue *fiq = &req->fm->fc->iq;
Max Reitz8f622e92020-04-20 17:59:34 +0200342
Eric Biggers76e43c82019-09-08 20:15:18 -0700343 spin_lock(&fiq->lock);
Kirill Tkhaib7829112018-11-08 12:05:42 +0300344 /* Check for we've sent request to interrupt this req */
345 if (unlikely(!test_bit(FR_INTERRUPTED, &req->flags))) {
Eric Biggers76e43c82019-09-08 20:15:18 -0700346 spin_unlock(&fiq->lock);
Kirill Tkhaib7829112018-11-08 12:05:42 +0300347 return -EINVAL;
348 }
349
Miklos Szeredi8f7bb362015-07-01 16:26:03 +0200350 if (list_empty(&req->intr_entry)) {
351 list_add_tail(&req->intr_entry, &fiq->interrupts);
Kirill Tkhai217316a2018-11-08 12:05:25 +0300352 /*
353 * Pairs with smp_mb() implied by test_and_set_bit()
Kirill Tkhai75d89252020-02-28 15:15:24 +0300354 * from fuse_request_end().
Kirill Tkhai217316a2018-11-08 12:05:25 +0300355 */
356 smp_mb();
357 if (test_bit(FR_FINISHED, &req->flags)) {
358 list_del_init(&req->intr_entry);
Eric Biggers76e43c82019-09-08 20:15:18 -0700359 spin_unlock(&fiq->lock);
Kirill Tkhaib7829112018-11-08 12:05:42 +0300360 return 0;
Kirill Tkhai217316a2018-11-08 12:05:25 +0300361 }
Stefan Hajnocziae3aad72018-06-18 15:53:19 +0100362 fiq->ops->wake_interrupt_and_unlock(fiq);
363 } else {
364 spin_unlock(&fiq->lock);
Miklos Szeredi8f7bb362015-07-01 16:26:03 +0200365 }
Kirill Tkhaib7829112018-11-08 12:05:42 +0300366 return 0;
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700367}
368
Max Reitz8f622e92020-04-20 17:59:34 +0200369static void request_wait_answer(struct fuse_req *req)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700370{
Max Reitzfcee2162020-05-06 17:44:12 +0200371 struct fuse_conn *fc = req->fm->fc;
Miklos Szeredi4ce60812015-07-01 16:26:02 +0200372 struct fuse_iqueue *fiq = &fc->iq;
Miklos Szeredic4775262015-07-01 16:26:00 +0200373 int err;
374
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700375 if (!fc->no_interrupt) {
376 /* Any signal may interrupt this */
Miklos Szeredic4775262015-07-01 16:26:00 +0200377 err = wait_event_interruptible(req->waitq,
Miklos Szeredi33e14b42015-07-01 16:26:01 +0200378 test_bit(FR_FINISHED, &req->flags));
Miklos Szeredic4775262015-07-01 16:26:00 +0200379 if (!err)
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700380 return;
381
Miklos Szeredi825d6d32015-07-01 16:25:58 +0200382 set_bit(FR_INTERRUPTED, &req->flags);
Miklos Szeredi8f7bb362015-07-01 16:26:03 +0200383 /* matches barrier in fuse_dev_do_read() */
384 smp_mb__after_atomic();
Miklos Szeredi33e14b42015-07-01 16:26:01 +0200385 if (test_bit(FR_SENT, &req->flags))
Max Reitz8f622e92020-04-20 17:59:34 +0200386 queue_interrupt(req);
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700387 }
388
Miklos Szeredi825d6d32015-07-01 16:25:58 +0200389 if (!test_bit(FR_FORCE, &req->flags)) {
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700390 /* Only fatal signals may interrupt this */
Al Viro7d3a07f2016-07-19 03:08:27 -0400391 err = wait_event_killable(req->waitq,
Miklos Szeredi33e14b42015-07-01 16:26:01 +0200392 test_bit(FR_FINISHED, &req->flags));
Miklos Szeredic4775262015-07-01 16:26:00 +0200393 if (!err)
Miklos Szeredia131de02007-10-16 23:31:04 -0700394 return;
395
Eric Biggers76e43c82019-09-08 20:15:18 -0700396 spin_lock(&fiq->lock);
Miklos Szeredia131de02007-10-16 23:31:04 -0700397 /* Request is not yet in userspace, bail out */
Miklos Szeredi33e14b42015-07-01 16:26:01 +0200398 if (test_bit(FR_PENDING, &req->flags)) {
Miklos Szeredia131de02007-10-16 23:31:04 -0700399 list_del(&req->list);
Eric Biggers76e43c82019-09-08 20:15:18 -0700400 spin_unlock(&fiq->lock);
Miklos Szeredia131de02007-10-16 23:31:04 -0700401 __fuse_put_request(req);
402 req->out.h.error = -EINTR;
403 return;
404 }
Eric Biggers76e43c82019-09-08 20:15:18 -0700405 spin_unlock(&fiq->lock);
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700406 }
Miklos Szeredi334f4852005-09-09 13:10:27 -0700407
Miklos Szeredia131de02007-10-16 23:31:04 -0700408 /*
409 * Either request is already in userspace, or it was forced.
410 * Wait it out.
411 */
Miklos Szeredi33e14b42015-07-01 16:26:01 +0200412 wait_event(req->waitq, test_bit(FR_FINISHED, &req->flags));
Miklos Szeredi334f4852005-09-09 13:10:27 -0700413}
414
Max Reitz8f622e92020-04-20 17:59:34 +0200415static void __fuse_request_send(struct fuse_req *req)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700416{
Max Reitzfcee2162020-05-06 17:44:12 +0200417 struct fuse_iqueue *fiq = &req->fm->fc->iq;
Miklos Szeredie16714d2015-07-01 16:26:01 +0200418
Miklos Szeredi825d6d32015-07-01 16:25:58 +0200419 BUG_ON(test_bit(FR_BACKGROUND, &req->flags));
Eric Biggers76e43c82019-09-08 20:15:18 -0700420 spin_lock(&fiq->lock);
Miklos Szeredie16714d2015-07-01 16:26:01 +0200421 if (!fiq->connected) {
Eric Biggers76e43c82019-09-08 20:15:18 -0700422 spin_unlock(&fiq->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700423 req->out.h.error = -ENOTCONN;
Miklos Szeredic4775262015-07-01 16:26:00 +0200424 } else {
Miklos Szeredif88996a2015-07-01 16:26:01 +0200425 req->in.h.unique = fuse_get_unique(fiq);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700426 /* acquire extra reference, since request is still needed
Stefan Hajnoczi04ec5af2018-06-21 09:33:40 +0100427 after fuse_request_end() */
Miklos Szeredi334f4852005-09-09 13:10:27 -0700428 __fuse_get_request(req);
Stefan Hajnocziae3aad72018-06-18 15:53:19 +0100429 queue_request_and_unlock(fiq, req);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700430
Max Reitz8f622e92020-04-20 17:59:34 +0200431 request_wait_answer(req);
Stefan Hajnoczi04ec5af2018-06-21 09:33:40 +0100432 /* Pairs with smp_wmb() in fuse_request_end() */
Miklos Szeredic4775262015-07-01 16:26:00 +0200433 smp_rmb();
Miklos Szeredi334f4852005-09-09 13:10:27 -0700434 }
Miklos Szeredi334f4852005-09-09 13:10:27 -0700435}
Eric Wong6a4e9222013-02-04 13:04:44 +0000436
Miklos Szeredi21f62172015-01-06 10:45:35 +0100437static void fuse_adjust_compat(struct fuse_conn *fc, struct fuse_args *args)
438{
Miklos Szeredid5b48542019-09-10 15:04:08 +0200439 if (fc->minor < 4 && args->opcode == FUSE_STATFS)
440 args->out_args[0].size = FUSE_COMPAT_STATFS_SIZE;
Miklos Szeredi21f62172015-01-06 10:45:35 +0100441
442 if (fc->minor < 9) {
Miklos Szeredid5b48542019-09-10 15:04:08 +0200443 switch (args->opcode) {
Miklos Szeredi21f62172015-01-06 10:45:35 +0100444 case FUSE_LOOKUP:
445 case FUSE_CREATE:
446 case FUSE_MKNOD:
447 case FUSE_MKDIR:
448 case FUSE_SYMLINK:
449 case FUSE_LINK:
Miklos Szeredid5b48542019-09-10 15:04:08 +0200450 args->out_args[0].size = FUSE_COMPAT_ENTRY_OUT_SIZE;
Miklos Szeredi21f62172015-01-06 10:45:35 +0100451 break;
452 case FUSE_GETATTR:
453 case FUSE_SETATTR:
Miklos Szeredid5b48542019-09-10 15:04:08 +0200454 args->out_args[0].size = FUSE_COMPAT_ATTR_OUT_SIZE;
Miklos Szeredi21f62172015-01-06 10:45:35 +0100455 break;
456 }
457 }
458 if (fc->minor < 12) {
Miklos Szeredid5b48542019-09-10 15:04:08 +0200459 switch (args->opcode) {
Miklos Szeredi21f62172015-01-06 10:45:35 +0100460 case FUSE_CREATE:
Miklos Szeredid5b48542019-09-10 15:04:08 +0200461 args->in_args[0].size = sizeof(struct fuse_open_in);
Miklos Szeredi21f62172015-01-06 10:45:35 +0100462 break;
463 case FUSE_MKNOD:
Miklos Szeredid5b48542019-09-10 15:04:08 +0200464 args->in_args[0].size = FUSE_COMPAT_MKNOD_IN_SIZE;
Miklos Szeredi21f62172015-01-06 10:45:35 +0100465 break;
466 }
467 }
468}
469
Max Reitz8f622e92020-04-20 17:59:34 +0200470static void fuse_force_creds(struct fuse_req *req)
Miklos Szeredie4137542019-09-10 15:04:08 +0200471{
Max Reitzfcee2162020-05-06 17:44:12 +0200472 struct fuse_conn *fc = req->fm->fc;
Max Reitz8f622e92020-04-20 17:59:34 +0200473
Miklos Szeredie4137542019-09-10 15:04:08 +0200474 req->in.h.uid = from_kuid_munged(fc->user_ns, current_fsuid());
475 req->in.h.gid = from_kgid_munged(fc->user_ns, current_fsgid());
476 req->in.h.pid = pid_nr_ns(task_pid(current), fc->pid_ns);
477}
478
YueHaibing5addcd52019-09-23 13:52:31 +0800479static void fuse_args_to_req(struct fuse_req *req, struct fuse_args *args)
Miklos Szeredi68583162019-09-10 15:04:09 +0200480{
Miklos Szeredi68583162019-09-10 15:04:09 +0200481 req->in.h.opcode = args->opcode;
482 req->in.h.nodeid = args->nodeid;
Miklos Szeredid4993772019-09-10 15:04:11 +0200483 req->args = args;
Miklos Szeredi3e8cb8b2020-02-13 09:16:07 +0100484 if (args->end)
485 __set_bit(FR_ASYNC, &req->flags);
Miklos Szeredi68583162019-09-10 15:04:09 +0200486}
487
Max Reitzfcee2162020-05-06 17:44:12 +0200488ssize_t fuse_simple_request(struct fuse_mount *fm, struct fuse_args *args)
Miklos Szeredi70781872014-12-12 09:49:05 +0100489{
Max Reitzfcee2162020-05-06 17:44:12 +0200490 struct fuse_conn *fc = fm->fc;
Miklos Szeredi70781872014-12-12 09:49:05 +0100491 struct fuse_req *req;
492 ssize_t ret;
493
Miklos Szeredic500eba2019-09-10 15:04:08 +0200494 if (args->force) {
Miklos Szeredie4137542019-09-10 15:04:08 +0200495 atomic_inc(&fc->num_waiting);
Max Reitzfcee2162020-05-06 17:44:12 +0200496 req = fuse_request_alloc(fm, GFP_KERNEL | __GFP_NOFAIL);
Miklos Szeredie4137542019-09-10 15:04:08 +0200497
498 if (!args->nocreds)
Max Reitz8f622e92020-04-20 17:59:34 +0200499 fuse_force_creds(req);
Miklos Szeredie4137542019-09-10 15:04:08 +0200500
501 __set_bit(FR_WAITING, &req->flags);
Miklos Szeredic500eba2019-09-10 15:04:08 +0200502 __set_bit(FR_FORCE, &req->flags);
503 } else {
Miklos Szeredie4137542019-09-10 15:04:08 +0200504 WARN_ON(args->nocreds);
Max Reitzfcee2162020-05-06 17:44:12 +0200505 req = fuse_get_req(fm, false);
Miklos Szeredic500eba2019-09-10 15:04:08 +0200506 if (IS_ERR(req))
507 return PTR_ERR(req);
508 }
Miklos Szeredi70781872014-12-12 09:49:05 +0100509
Miklos Szeredi21f62172015-01-06 10:45:35 +0100510 /* Needs to be done after fuse_get_req() so that fc->minor is valid */
511 fuse_adjust_compat(fc, args);
Miklos Szeredi68583162019-09-10 15:04:09 +0200512 fuse_args_to_req(req, args);
Miklos Szeredi21f62172015-01-06 10:45:35 +0100513
Miklos Szeredi454a7612019-09-10 15:04:08 +0200514 if (!args->noreply)
515 __set_bit(FR_ISREPLY, &req->flags);
Max Reitz8f622e92020-04-20 17:59:34 +0200516 __fuse_request_send(req);
Miklos Szeredi70781872014-12-12 09:49:05 +0100517 ret = req->out.h.error;
Miklos Szeredid5b48542019-09-10 15:04:08 +0200518 if (!ret && args->out_argvar) {
Miklos Szeredi093f38a2019-09-10 15:04:09 +0200519 BUG_ON(args->out_numargs == 0);
Miklos Szeredid4993772019-09-10 15:04:11 +0200520 ret = args->out_args[args->out_numargs - 1].size;
Miklos Szeredi70781872014-12-12 09:49:05 +0100521 }
Max Reitz8f622e92020-04-20 17:59:34 +0200522 fuse_put_request(req);
Miklos Szeredi70781872014-12-12 09:49:05 +0100523
524 return ret;
525}
526
Max Reitz8f622e92020-04-20 17:59:34 +0200527static bool fuse_request_queue_background(struct fuse_req *req)
Miklos Szeredid12def12008-02-06 01:38:39 -0800528{
Max Reitzfcee2162020-05-06 17:44:12 +0200529 struct fuse_mount *fm = req->fm;
530 struct fuse_conn *fc = fm->fc;
Kirill Tkhai63825b42018-08-27 18:29:56 +0300531 bool queued = false;
532
533 WARN_ON(!test_bit(FR_BACKGROUND, &req->flags));
Miklos Szeredi825d6d32015-07-01 16:25:58 +0200534 if (!test_bit(FR_WAITING, &req->flags)) {
535 __set_bit(FR_WAITING, &req->flags);
Miklos Szeredi5437f242015-07-01 16:25:56 +0200536 atomic_inc(&fc->num_waiting);
537 }
Miklos Szeredi825d6d32015-07-01 16:25:58 +0200538 __set_bit(FR_ISREPLY, &req->flags);
Kirill Tkhaiae2dffa2018-08-27 18:29:46 +0300539 spin_lock(&fc->bg_lock);
Kirill Tkhai63825b42018-08-27 18:29:56 +0300540 if (likely(fc->connected)) {
541 fc->num_background++;
542 if (fc->num_background == fc->max_background)
543 fc->blocked = 1;
Max Reitzfcee2162020-05-06 17:44:12 +0200544 if (fc->num_background == fc->congestion_threshold && fm->sb) {
545 set_bdi_congested(fm->sb->s_bdi, BLK_RW_SYNC);
546 set_bdi_congested(fm->sb->s_bdi, BLK_RW_ASYNC);
Kirill Tkhai63825b42018-08-27 18:29:56 +0300547 }
548 list_add_tail(&req->list, &fc->bg_queue);
549 flush_bg_queue(fc);
550 queued = true;
Miklos Szeredid12def12008-02-06 01:38:39 -0800551 }
Kirill Tkhaiae2dffa2018-08-27 18:29:46 +0300552 spin_unlock(&fc->bg_lock);
Kirill Tkhai63825b42018-08-27 18:29:56 +0300553
554 return queued;
Miklos Szeredid12def12008-02-06 01:38:39 -0800555}
556
Max Reitzfcee2162020-05-06 17:44:12 +0200557int fuse_simple_background(struct fuse_mount *fm, struct fuse_args *args,
Miklos Szeredi12597282019-09-10 15:04:10 +0200558 gfp_t gfp_flags)
559{
560 struct fuse_req *req;
561
562 if (args->force) {
563 WARN_ON(!args->nocreds);
Max Reitzfcee2162020-05-06 17:44:12 +0200564 req = fuse_request_alloc(fm, gfp_flags);
Miklos Szeredi12597282019-09-10 15:04:10 +0200565 if (!req)
566 return -ENOMEM;
567 __set_bit(FR_BACKGROUND, &req->flags);
568 } else {
569 WARN_ON(args->nocreds);
Max Reitzfcee2162020-05-06 17:44:12 +0200570 req = fuse_get_req(fm, true);
Miklos Szeredi12597282019-09-10 15:04:10 +0200571 if (IS_ERR(req))
572 return PTR_ERR(req);
573 }
574
575 fuse_args_to_req(req, args);
576
Max Reitz8f622e92020-04-20 17:59:34 +0200577 if (!fuse_request_queue_background(req)) {
578 fuse_put_request(req);
Miklos Szeredi12597282019-09-10 15:04:10 +0200579 return -ENOTCONN;
580 }
581
582 return 0;
583}
584EXPORT_SYMBOL_GPL(fuse_simple_background);
585
Max Reitzfcee2162020-05-06 17:44:12 +0200586static int fuse_simple_notify_reply(struct fuse_mount *fm,
Miklos Szeredi75b399d2019-09-10 15:04:11 +0200587 struct fuse_args *args, u64 unique)
Miklos Szeredi2d45ba32010-07-12 14:41:40 +0200588{
Miklos Szeredi75b399d2019-09-10 15:04:11 +0200589 struct fuse_req *req;
Max Reitzfcee2162020-05-06 17:44:12 +0200590 struct fuse_iqueue *fiq = &fm->fc->iq;
Miklos Szeredi75b399d2019-09-10 15:04:11 +0200591 int err = 0;
592
Max Reitzfcee2162020-05-06 17:44:12 +0200593 req = fuse_get_req(fm, false);
Miklos Szeredi75b399d2019-09-10 15:04:11 +0200594 if (IS_ERR(req))
595 return PTR_ERR(req);
Miklos Szeredi2d45ba32010-07-12 14:41:40 +0200596
Miklos Szeredi825d6d32015-07-01 16:25:58 +0200597 __clear_bit(FR_ISREPLY, &req->flags);
Miklos Szeredi2d45ba32010-07-12 14:41:40 +0200598 req->in.h.unique = unique;
Miklos Szeredi75b399d2019-09-10 15:04:11 +0200599
600 fuse_args_to_req(req, args);
Miklos Szeredi75b399d2019-09-10 15:04:11 +0200601
Eric Biggers76e43c82019-09-08 20:15:18 -0700602 spin_lock(&fiq->lock);
Miklos Szeredie16714d2015-07-01 16:26:01 +0200603 if (fiq->connected) {
Stefan Hajnocziae3aad72018-06-18 15:53:19 +0100604 queue_request_and_unlock(fiq, req);
Miklos Szeredi75b399d2019-09-10 15:04:11 +0200605 } else {
606 err = -ENODEV;
607 spin_unlock(&fiq->lock);
Max Reitz8f622e92020-04-20 17:59:34 +0200608 fuse_put_request(req);
Miklos Szeredi2d45ba32010-07-12 14:41:40 +0200609 }
Miklos Szeredi2d45ba32010-07-12 14:41:40 +0200610
611 return err;
612}
613
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700614/*
Miklos Szeredi334f4852005-09-09 13:10:27 -0700615 * Lock the request. Up to the next unlock_request() there mustn't be
616 * anything that could cause a page-fault. If the request was already
Miklos Szeredif9a28422006-06-25 05:48:53 -0700617 * aborted bail out.
Miklos Szeredi334f4852005-09-09 13:10:27 -0700618 */
Miklos Szeredidc008092015-07-01 16:25:58 +0200619static int lock_request(struct fuse_req *req)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700620{
621 int err = 0;
622 if (req) {
Miklos Szeredidc008092015-07-01 16:25:58 +0200623 spin_lock(&req->waitq.lock);
Miklos Szeredi825d6d32015-07-01 16:25:58 +0200624 if (test_bit(FR_ABORTED, &req->flags))
Miklos Szeredi334f4852005-09-09 13:10:27 -0700625 err = -ENOENT;
626 else
Miklos Szeredi825d6d32015-07-01 16:25:58 +0200627 set_bit(FR_LOCKED, &req->flags);
Miklos Szeredidc008092015-07-01 16:25:58 +0200628 spin_unlock(&req->waitq.lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700629 }
630 return err;
631}
632
633/*
Miklos Szeredi0d8e84b2015-07-01 16:25:58 +0200634 * Unlock request. If it was aborted while locked, caller is responsible
635 * for unlocking and ending the request.
Miklos Szeredi334f4852005-09-09 13:10:27 -0700636 */
Miklos Szeredidc008092015-07-01 16:25:58 +0200637static int unlock_request(struct fuse_req *req)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700638{
Miklos Szeredi0d8e84b2015-07-01 16:25:58 +0200639 int err = 0;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700640 if (req) {
Miklos Szeredidc008092015-07-01 16:25:58 +0200641 spin_lock(&req->waitq.lock);
Miklos Szeredi825d6d32015-07-01 16:25:58 +0200642 if (test_bit(FR_ABORTED, &req->flags))
Miklos Szeredi0d8e84b2015-07-01 16:25:58 +0200643 err = -ENOENT;
644 else
Miklos Szeredi825d6d32015-07-01 16:25:58 +0200645 clear_bit(FR_LOCKED, &req->flags);
Miklos Szeredidc008092015-07-01 16:25:58 +0200646 spin_unlock(&req->waitq.lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700647 }
Miklos Szeredi0d8e84b2015-07-01 16:25:58 +0200648 return err;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700649}
650
651struct fuse_copy_state {
652 int write;
653 struct fuse_req *req;
Al Viro6c09e942015-04-03 22:06:08 -0400654 struct iov_iter *iter;
Miklos Szeredidd3bb142010-05-25 15:06:06 +0200655 struct pipe_buffer *pipebufs;
656 struct pipe_buffer *currbuf;
657 struct pipe_inode_info *pipe;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700658 unsigned long nr_segs;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700659 struct page *pg;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700660 unsigned len;
Miklos Szeredic55a01d2014-07-07 15:28:51 +0200661 unsigned offset;
Miklos Szeredice534fb2010-05-25 15:06:07 +0200662 unsigned move_pages:1;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700663};
664
Miklos Szeredidc008092015-07-01 16:25:58 +0200665static void fuse_copy_init(struct fuse_copy_state *cs, int write,
Al Viro6c09e942015-04-03 22:06:08 -0400666 struct iov_iter *iter)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700667{
668 memset(cs, 0, sizeof(*cs));
669 cs->write = write;
Al Viro6c09e942015-04-03 22:06:08 -0400670 cs->iter = iter;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700671}
672
673/* Unmap and put previous page of userspace buffer */
Miklos Szeredi8bfc0162006-01-16 22:14:28 -0800674static void fuse_copy_finish(struct fuse_copy_state *cs)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700675{
Miklos Szeredidd3bb142010-05-25 15:06:06 +0200676 if (cs->currbuf) {
677 struct pipe_buffer *buf = cs->currbuf;
678
Miklos Szeredic55a01d2014-07-07 15:28:51 +0200679 if (cs->write)
Miklos Szeredic3021622010-05-25 15:06:07 +0200680 buf->len = PAGE_SIZE - cs->len;
Miklos Szeredidd3bb142010-05-25 15:06:06 +0200681 cs->currbuf = NULL;
Miklos Szeredic55a01d2014-07-07 15:28:51 +0200682 } else if (cs->pg) {
Miklos Szeredi334f4852005-09-09 13:10:27 -0700683 if (cs->write) {
684 flush_dcache_page(cs->pg);
685 set_page_dirty_lock(cs->pg);
686 }
Minchan Kim2de28a02021-03-18 09:32:48 -0700687 /*
688 * The page could be GUP page(see iov_iter_get_pages in
689 * fuse_copy_fill) so use put_user_page to release it.
690 */
691 put_user_page(cs->pg);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700692 }
Miklos Szeredic55a01d2014-07-07 15:28:51 +0200693 cs->pg = NULL;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700694}
695
696/*
697 * Get another pagefull of userspace buffer, and map it to kernel
698 * address space, and lock request
699 */
700static int fuse_copy_fill(struct fuse_copy_state *cs)
701{
Miklos Szeredic55a01d2014-07-07 15:28:51 +0200702 struct page *page;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700703 int err;
704
Miklos Szeredidc008092015-07-01 16:25:58 +0200705 err = unlock_request(cs->req);
Miklos Szeredi0d8e84b2015-07-01 16:25:58 +0200706 if (err)
707 return err;
708
Miklos Szeredi334f4852005-09-09 13:10:27 -0700709 fuse_copy_finish(cs);
Miklos Szeredidd3bb142010-05-25 15:06:06 +0200710 if (cs->pipebufs) {
711 struct pipe_buffer *buf = cs->pipebufs;
712
Miklos Szeredic3021622010-05-25 15:06:07 +0200713 if (!cs->write) {
Miklos Szeredifba597d2016-09-27 10:45:12 +0200714 err = pipe_buf_confirm(cs->pipe, buf);
Miklos Szeredic3021622010-05-25 15:06:07 +0200715 if (err)
716 return err;
Miklos Szeredidd3bb142010-05-25 15:06:06 +0200717
Miklos Szeredic3021622010-05-25 15:06:07 +0200718 BUG_ON(!cs->nr_segs);
719 cs->currbuf = buf;
Miklos Szeredic55a01d2014-07-07 15:28:51 +0200720 cs->pg = buf->page;
721 cs->offset = buf->offset;
Miklos Szeredic3021622010-05-25 15:06:07 +0200722 cs->len = buf->len;
Miklos Szeredic3021622010-05-25 15:06:07 +0200723 cs->pipebufs++;
724 cs->nr_segs--;
725 } else {
David Howells6718b6f2019-10-16 16:47:32 +0100726 if (cs->nr_segs >= cs->pipe->max_usage)
Miklos Szeredic3021622010-05-25 15:06:07 +0200727 return -EIO;
728
729 page = alloc_page(GFP_HIGHUSER);
730 if (!page)
731 return -ENOMEM;
732
733 buf->page = page;
734 buf->offset = 0;
735 buf->len = 0;
736
737 cs->currbuf = buf;
Miklos Szeredic55a01d2014-07-07 15:28:51 +0200738 cs->pg = page;
739 cs->offset = 0;
Miklos Szeredic3021622010-05-25 15:06:07 +0200740 cs->len = PAGE_SIZE;
741 cs->pipebufs++;
742 cs->nr_segs++;
743 }
Miklos Szeredidd3bb142010-05-25 15:06:06 +0200744 } else {
Al Viro6c09e942015-04-03 22:06:08 -0400745 size_t off;
746 err = iov_iter_get_pages(cs->iter, &page, PAGE_SIZE, 1, &off);
Miklos Szeredidd3bb142010-05-25 15:06:06 +0200747 if (err < 0)
748 return err;
Al Viro6c09e942015-04-03 22:06:08 -0400749 BUG_ON(!err);
750 cs->len = err;
751 cs->offset = off;
Miklos Szeredic55a01d2014-07-07 15:28:51 +0200752 cs->pg = page;
Al Viro6c09e942015-04-03 22:06:08 -0400753 iov_iter_advance(cs->iter, err);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700754 }
Miklos Szeredi334f4852005-09-09 13:10:27 -0700755
Miklos Szeredidc008092015-07-01 16:25:58 +0200756 return lock_request(cs->req);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700757}
758
759/* Do as much copy to/from userspace buffer as we can */
Miklos Szeredi8bfc0162006-01-16 22:14:28 -0800760static int fuse_copy_do(struct fuse_copy_state *cs, void **val, unsigned *size)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700761{
762 unsigned ncpy = min(*size, cs->len);
763 if (val) {
Miklos Szeredic55a01d2014-07-07 15:28:51 +0200764 void *pgaddr = kmap_atomic(cs->pg);
765 void *buf = pgaddr + cs->offset;
766
Miklos Szeredi334f4852005-09-09 13:10:27 -0700767 if (cs->write)
Miklos Szeredic55a01d2014-07-07 15:28:51 +0200768 memcpy(buf, *val, ncpy);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700769 else
Miklos Szeredic55a01d2014-07-07 15:28:51 +0200770 memcpy(*val, buf, ncpy);
771
772 kunmap_atomic(pgaddr);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700773 *val += ncpy;
774 }
775 *size -= ncpy;
776 cs->len -= ncpy;
Miklos Szeredic55a01d2014-07-07 15:28:51 +0200777 cs->offset += ncpy;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700778 return ncpy;
779}
780
Miklos Szeredice534fb2010-05-25 15:06:07 +0200781static int fuse_check_page(struct page *page)
782{
783 if (page_mapcount(page) ||
784 page->mapping != NULL ||
Miklos Szeredice534fb2010-05-25 15:06:07 +0200785 (page->flags & PAGE_FLAGS_CHECK_AT_PREP &
786 ~(1 << PG_locked |
787 1 << PG_referenced |
788 1 << PG_uptodate |
789 1 << PG_lru |
790 1 << PG_active |
Miklos Szeredi912e9852021-06-18 21:16:42 +0200791 1 << PG_workingset |
Miklos Szeredia5005c32020-05-19 14:50:37 +0200792 1 << PG_reclaim |
793 1 << PG_waiters))) {
Miklos Szeredi00589382020-05-19 14:50:37 +0200794 dump_page(page, "fuse: trying to steal weird page");
Miklos Szeredice534fb2010-05-25 15:06:07 +0200795 return 1;
796 }
797 return 0;
798}
799
800static int fuse_try_move_page(struct fuse_copy_state *cs, struct page **pagep)
801{
802 int err;
803 struct page *oldpage = *pagep;
804 struct page *newpage;
805 struct pipe_buffer *buf = cs->pipebufs;
Miklos Szeredice534fb2010-05-25 15:06:07 +0200806
Miklos Szeredid78092e2020-09-18 10:36:50 +0200807 get_page(oldpage);
Miklos Szeredidc008092015-07-01 16:25:58 +0200808 err = unlock_request(cs->req);
Miklos Szeredi0d8e84b2015-07-01 16:25:58 +0200809 if (err)
Miklos Szeredid78092e2020-09-18 10:36:50 +0200810 goto out_put_old;
Miklos Szeredi0d8e84b2015-07-01 16:25:58 +0200811
Miklos Szeredice534fb2010-05-25 15:06:07 +0200812 fuse_copy_finish(cs);
813
Miklos Szeredifba597d2016-09-27 10:45:12 +0200814 err = pipe_buf_confirm(cs->pipe, buf);
Miklos Szeredice534fb2010-05-25 15:06:07 +0200815 if (err)
Miklos Szeredid78092e2020-09-18 10:36:50 +0200816 goto out_put_old;
Miklos Szeredice534fb2010-05-25 15:06:07 +0200817
818 BUG_ON(!cs->nr_segs);
819 cs->currbuf = buf;
820 cs->len = buf->len;
821 cs->pipebufs++;
822 cs->nr_segs--;
823
824 if (cs->len != PAGE_SIZE)
825 goto out_fallback;
826
Christoph Hellwigc928f642020-05-20 17:58:16 +0200827 if (!pipe_buf_try_steal(cs->pipe, buf))
Miklos Szeredice534fb2010-05-25 15:06:07 +0200828 goto out_fallback;
829
830 newpage = buf->page;
831
Miklos Szerediaa991b32015-02-26 11:45:47 +0100832 if (!PageUptodate(newpage))
833 SetPageUptodate(newpage);
Miklos Szeredice534fb2010-05-25 15:06:07 +0200834
835 ClearPageMappedToDisk(newpage);
836
837 if (fuse_check_page(newpage) != 0)
838 goto out_fallback_unlock;
839
Miklos Szeredice534fb2010-05-25 15:06:07 +0200840 /*
841 * This is a new and locked page, it shouldn't be mapped or
842 * have any special flags on it
843 */
844 if (WARN_ON(page_mapped(oldpage)))
845 goto out_fallback_unlock;
846 if (WARN_ON(page_has_private(oldpage)))
847 goto out_fallback_unlock;
848 if (WARN_ON(PageDirty(oldpage) || PageWriteback(oldpage)))
849 goto out_fallback_unlock;
850 if (WARN_ON(PageMlocked(oldpage)))
851 goto out_fallback_unlock;
852
Miklos Szeredief6a3c62011-03-22 16:30:52 -0700853 err = replace_page_cache_page(oldpage, newpage, GFP_KERNEL);
Miklos Szeredice534fb2010-05-25 15:06:07 +0200854 if (err) {
Miklos Szeredief6a3c62011-03-22 16:30:52 -0700855 unlock_page(newpage);
Miklos Szeredid78092e2020-09-18 10:36:50 +0200856 goto out_put_old;
Miklos Szeredice534fb2010-05-25 15:06:07 +0200857 }
Miklos Szeredief6a3c62011-03-22 16:30:52 -0700858
Miklos Szeredi8f4d0712021-11-25 14:05:18 +0100859 get_page(newpage);
860
861 if (!(buf->flags & PIPE_BUF_FLAG_LRU))
862 lru_cache_add(newpage);
863
Miklos Szeredib05eea12021-11-02 11:10:37 +0100864 /*
865 * Release while we have extra ref on stolen page. Otherwise
866 * anon_pipe_buf_release() might think the page can be reused.
867 */
868 pipe_buf_release(cs->pipe, buf);
869
Miklos Szeredice534fb2010-05-25 15:06:07 +0200870 err = 0;
Miklos Szeredidc008092015-07-01 16:25:58 +0200871 spin_lock(&cs->req->waitq.lock);
Miklos Szeredi825d6d32015-07-01 16:25:58 +0200872 if (test_bit(FR_ABORTED, &cs->req->flags))
Miklos Szeredice534fb2010-05-25 15:06:07 +0200873 err = -ENOENT;
874 else
875 *pagep = newpage;
Miklos Szeredidc008092015-07-01 16:25:58 +0200876 spin_unlock(&cs->req->waitq.lock);
Miklos Szeredice534fb2010-05-25 15:06:07 +0200877
878 if (err) {
879 unlock_page(newpage);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300880 put_page(newpage);
Miklos Szeredid78092e2020-09-18 10:36:50 +0200881 goto out_put_old;
Miklos Szeredice534fb2010-05-25 15:06:07 +0200882 }
883
884 unlock_page(oldpage);
Miklos Szeredid78092e2020-09-18 10:36:50 +0200885 /* Drop ref for ap->pages[] array */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300886 put_page(oldpage);
Miklos Szeredice534fb2010-05-25 15:06:07 +0200887 cs->len = 0;
888
Miklos Szeredid78092e2020-09-18 10:36:50 +0200889 err = 0;
890out_put_old:
891 /* Drop ref obtained in this function */
892 put_page(oldpage);
893 return err;
Miklos Szeredice534fb2010-05-25 15:06:07 +0200894
895out_fallback_unlock:
896 unlock_page(newpage);
897out_fallback:
Miklos Szeredic55a01d2014-07-07 15:28:51 +0200898 cs->pg = buf->page;
899 cs->offset = buf->offset;
Miklos Szeredice534fb2010-05-25 15:06:07 +0200900
Miklos Szeredidc008092015-07-01 16:25:58 +0200901 err = lock_request(cs->req);
Miklos Szeredid78092e2020-09-18 10:36:50 +0200902 if (!err)
903 err = 1;
Miklos Szeredice534fb2010-05-25 15:06:07 +0200904
Miklos Szeredid78092e2020-09-18 10:36:50 +0200905 goto out_put_old;
Miklos Szeredice534fb2010-05-25 15:06:07 +0200906}
907
Miklos Szeredic3021622010-05-25 15:06:07 +0200908static int fuse_ref_page(struct fuse_copy_state *cs, struct page *page,
909 unsigned offset, unsigned count)
910{
911 struct pipe_buffer *buf;
Miklos Szeredi0d8e84b2015-07-01 16:25:58 +0200912 int err;
Miklos Szeredic3021622010-05-25 15:06:07 +0200913
David Howells6718b6f2019-10-16 16:47:32 +0100914 if (cs->nr_segs >= cs->pipe->max_usage)
Miklos Szeredic3021622010-05-25 15:06:07 +0200915 return -EIO;
916
Miklos Szeredid78092e2020-09-18 10:36:50 +0200917 get_page(page);
Miklos Szeredidc008092015-07-01 16:25:58 +0200918 err = unlock_request(cs->req);
Miklos Szeredid78092e2020-09-18 10:36:50 +0200919 if (err) {
920 put_page(page);
Miklos Szeredi0d8e84b2015-07-01 16:25:58 +0200921 return err;
Miklos Szeredid78092e2020-09-18 10:36:50 +0200922 }
Miklos Szeredi0d8e84b2015-07-01 16:25:58 +0200923
Miklos Szeredic3021622010-05-25 15:06:07 +0200924 fuse_copy_finish(cs);
925
926 buf = cs->pipebufs;
Miklos Szeredic3021622010-05-25 15:06:07 +0200927 buf->page = page;
928 buf->offset = offset;
929 buf->len = count;
930
931 cs->pipebufs++;
932 cs->nr_segs++;
933 cs->len = 0;
934
935 return 0;
936}
937
Miklos Szeredi334f4852005-09-09 13:10:27 -0700938/*
939 * Copy a page in the request to/from the userspace buffer. Must be
940 * done atomically
941 */
Miklos Szeredice534fb2010-05-25 15:06:07 +0200942static int fuse_copy_page(struct fuse_copy_state *cs, struct page **pagep,
Miklos Szeredi8bfc0162006-01-16 22:14:28 -0800943 unsigned offset, unsigned count, int zeroing)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700944{
Miklos Szeredice534fb2010-05-25 15:06:07 +0200945 int err;
946 struct page *page = *pagep;
947
Miklos Szeredib6777c42010-10-26 14:22:27 -0700948 if (page && zeroing && count < PAGE_SIZE)
949 clear_highpage(page);
950
Miklos Szeredi334f4852005-09-09 13:10:27 -0700951 while (count) {
Miklos Szeredic3021622010-05-25 15:06:07 +0200952 if (cs->write && cs->pipebufs && page) {
953 return fuse_ref_page(cs, page, offset, count);
954 } else if (!cs->len) {
Miklos Szeredice534fb2010-05-25 15:06:07 +0200955 if (cs->move_pages && page &&
956 offset == 0 && count == PAGE_SIZE) {
957 err = fuse_try_move_page(cs, pagep);
958 if (err <= 0)
959 return err;
960 } else {
961 err = fuse_copy_fill(cs);
962 if (err)
963 return err;
964 }
Miklos Szeredi1729a162008-11-26 12:03:54 +0100965 }
Miklos Szeredi334f4852005-09-09 13:10:27 -0700966 if (page) {
Cong Wang2408f6e2011-11-25 23:14:30 +0800967 void *mapaddr = kmap_atomic(page);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700968 void *buf = mapaddr + offset;
969 offset += fuse_copy_do(cs, &buf, &count);
Cong Wang2408f6e2011-11-25 23:14:30 +0800970 kunmap_atomic(mapaddr);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700971 } else
972 offset += fuse_copy_do(cs, NULL, &count);
973 }
974 if (page && !cs->write)
975 flush_dcache_page(page);
976 return 0;
977}
978
979/* Copy pages in the request to/from userspace buffer */
980static int fuse_copy_pages(struct fuse_copy_state *cs, unsigned nbytes,
981 int zeroing)
982{
983 unsigned i;
984 struct fuse_req *req = cs->req;
Miklos Szeredi05ea48c2019-09-10 15:04:11 +0200985 struct fuse_args_pages *ap = container_of(req->args, typeof(*ap), args);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700986
Miklos Szeredi05ea48c2019-09-10 15:04:11 +0200987
988 for (i = 0; i < ap->num_pages && (nbytes || zeroing); i++) {
Miklos Szeredice534fb2010-05-25 15:06:07 +0200989 int err;
Miklos Szeredi05ea48c2019-09-10 15:04:11 +0200990 unsigned int offset = ap->descs[i].offset;
991 unsigned int count = min(nbytes, ap->descs[i].length);
Miklos Szeredice534fb2010-05-25 15:06:07 +0200992
Miklos Szeredi05ea48c2019-09-10 15:04:11 +0200993 err = fuse_copy_page(cs, &ap->pages[i], offset, count, zeroing);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700994 if (err)
995 return err;
996
997 nbytes -= count;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700998 }
999 return 0;
1000}
1001
1002/* Copy a single argument in the request to/from userspace buffer */
1003static int fuse_copy_one(struct fuse_copy_state *cs, void *val, unsigned size)
1004{
1005 while (size) {
Miklos Szeredi1729a162008-11-26 12:03:54 +01001006 if (!cs->len) {
1007 int err = fuse_copy_fill(cs);
1008 if (err)
1009 return err;
1010 }
Miklos Szeredi334f4852005-09-09 13:10:27 -07001011 fuse_copy_do(cs, &val, &size);
1012 }
1013 return 0;
1014}
1015
1016/* Copy request arguments to/from userspace buffer */
1017static int fuse_copy_args(struct fuse_copy_state *cs, unsigned numargs,
1018 unsigned argpages, struct fuse_arg *args,
1019 int zeroing)
1020{
1021 int err = 0;
1022 unsigned i;
1023
1024 for (i = 0; !err && i < numargs; i++) {
1025 struct fuse_arg *arg = &args[i];
1026 if (i == numargs - 1 && argpages)
1027 err = fuse_copy_pages(cs, arg->size, zeroing);
1028 else
1029 err = fuse_copy_one(cs, arg->value, arg->size);
1030 }
1031 return err;
1032}
1033
Miklos Szeredif88996a2015-07-01 16:26:01 +02001034static int forget_pending(struct fuse_iqueue *fiq)
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001035{
Miklos Szeredif88996a2015-07-01 16:26:01 +02001036 return fiq->forget_list_head.next != NULL;
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001037}
1038
Miklos Szeredif88996a2015-07-01 16:26:01 +02001039static int request_pending(struct fuse_iqueue *fiq)
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001040{
Miklos Szeredif88996a2015-07-01 16:26:01 +02001041 return !list_empty(&fiq->pending) || !list_empty(&fiq->interrupts) ||
1042 forget_pending(fiq);
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001043}
1044
Miklos Szeredi334f4852005-09-09 13:10:27 -07001045/*
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001046 * Transfer an interrupt request to userspace
1047 *
1048 * Unlike other requests this is assembled on demand, without a need
1049 * to allocate a separate fuse_req structure.
1050 *
Eric Biggers76e43c82019-09-08 20:15:18 -07001051 * Called with fiq->lock held, releases it
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001052 */
Miklos Szeredifd22d622015-07-01 16:26:03 +02001053static int fuse_read_interrupt(struct fuse_iqueue *fiq,
1054 struct fuse_copy_state *cs,
Miklos Szeredic3021622010-05-25 15:06:07 +02001055 size_t nbytes, struct fuse_req *req)
Eric Biggers76e43c82019-09-08 20:15:18 -07001056__releases(fiq->lock)
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001057{
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001058 struct fuse_in_header ih;
1059 struct fuse_interrupt_in arg;
1060 unsigned reqsize = sizeof(ih) + sizeof(arg);
1061 int err;
1062
1063 list_del_init(&req->intr_entry);
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001064 memset(&ih, 0, sizeof(ih));
1065 memset(&arg, 0, sizeof(arg));
1066 ih.len = reqsize;
1067 ih.opcode = FUSE_INTERRUPT;
Kirill Tkhai3a5358d2018-09-11 13:12:05 +03001068 ih.unique = (req->in.h.unique | FUSE_INT_REQ_BIT);
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001069 arg.unique = req->in.h.unique;
1070
Eric Biggers76e43c82019-09-08 20:15:18 -07001071 spin_unlock(&fiq->lock);
Miklos Szeredic3021622010-05-25 15:06:07 +02001072 if (nbytes < reqsize)
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001073 return -EINVAL;
1074
Miklos Szeredic3021622010-05-25 15:06:07 +02001075 err = fuse_copy_one(cs, &ih, sizeof(ih));
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001076 if (!err)
Miklos Szeredic3021622010-05-25 15:06:07 +02001077 err = fuse_copy_one(cs, &arg, sizeof(arg));
1078 fuse_copy_finish(cs);
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001079
1080 return err ? err : reqsize;
1081}
1082
Vivek Goyal4388c5a2019-06-05 15:50:43 -04001083struct fuse_forget_link *fuse_dequeue_forget(struct fuse_iqueue *fiq,
1084 unsigned int max,
1085 unsigned int *countp)
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001086{
Miklos Szeredif88996a2015-07-01 16:26:01 +02001087 struct fuse_forget_link *head = fiq->forget_list_head.next;
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001088 struct fuse_forget_link **newhead = &head;
1089 unsigned count;
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001090
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001091 for (count = 0; *newhead != NULL && count < max; count++)
1092 newhead = &(*newhead)->next;
1093
Miklos Szeredif88996a2015-07-01 16:26:01 +02001094 fiq->forget_list_head.next = *newhead;
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001095 *newhead = NULL;
Miklos Szeredif88996a2015-07-01 16:26:01 +02001096 if (fiq->forget_list_head.next == NULL)
1097 fiq->forget_list_tail = &fiq->forget_list_head;
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001098
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001099 if (countp != NULL)
1100 *countp = count;
1101
1102 return head;
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001103}
Vivek Goyal4388c5a2019-06-05 15:50:43 -04001104EXPORT_SYMBOL(fuse_dequeue_forget);
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001105
Miklos Szeredifd22d622015-07-01 16:26:03 +02001106static int fuse_read_single_forget(struct fuse_iqueue *fiq,
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001107 struct fuse_copy_state *cs,
1108 size_t nbytes)
Eric Biggers76e43c82019-09-08 20:15:18 -07001109__releases(fiq->lock)
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001110{
1111 int err;
Vivek Goyal4388c5a2019-06-05 15:50:43 -04001112 struct fuse_forget_link *forget = fuse_dequeue_forget(fiq, 1, NULL);
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001113 struct fuse_forget_in arg = {
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001114 .nlookup = forget->forget_one.nlookup,
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001115 };
1116 struct fuse_in_header ih = {
1117 .opcode = FUSE_FORGET,
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001118 .nodeid = forget->forget_one.nodeid,
Miklos Szeredif88996a2015-07-01 16:26:01 +02001119 .unique = fuse_get_unique(fiq),
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001120 .len = sizeof(ih) + sizeof(arg),
1121 };
1122
Eric Biggers76e43c82019-09-08 20:15:18 -07001123 spin_unlock(&fiq->lock);
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001124 kfree(forget);
1125 if (nbytes < ih.len)
1126 return -EINVAL;
1127
1128 err = fuse_copy_one(cs, &ih, sizeof(ih));
1129 if (!err)
1130 err = fuse_copy_one(cs, &arg, sizeof(arg));
1131 fuse_copy_finish(cs);
1132
1133 if (err)
1134 return err;
1135
1136 return ih.len;
1137}
1138
Miklos Szeredifd22d622015-07-01 16:26:03 +02001139static int fuse_read_batch_forget(struct fuse_iqueue *fiq,
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001140 struct fuse_copy_state *cs, size_t nbytes)
Eric Biggers76e43c82019-09-08 20:15:18 -07001141__releases(fiq->lock)
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001142{
1143 int err;
1144 unsigned max_forgets;
1145 unsigned count;
1146 struct fuse_forget_link *head;
1147 struct fuse_batch_forget_in arg = { .count = 0 };
1148 struct fuse_in_header ih = {
1149 .opcode = FUSE_BATCH_FORGET,
Miklos Szeredif88996a2015-07-01 16:26:01 +02001150 .unique = fuse_get_unique(fiq),
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001151 .len = sizeof(ih) + sizeof(arg),
1152 };
1153
1154 if (nbytes < ih.len) {
Eric Biggers76e43c82019-09-08 20:15:18 -07001155 spin_unlock(&fiq->lock);
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001156 return -EINVAL;
1157 }
1158
1159 max_forgets = (nbytes - ih.len) / sizeof(struct fuse_forget_one);
Vivek Goyal4388c5a2019-06-05 15:50:43 -04001160 head = fuse_dequeue_forget(fiq, max_forgets, &count);
Eric Biggers76e43c82019-09-08 20:15:18 -07001161 spin_unlock(&fiq->lock);
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001162
1163 arg.count = count;
1164 ih.len += count * sizeof(struct fuse_forget_one);
1165 err = fuse_copy_one(cs, &ih, sizeof(ih));
1166 if (!err)
1167 err = fuse_copy_one(cs, &arg, sizeof(arg));
1168
1169 while (head) {
1170 struct fuse_forget_link *forget = head;
1171
1172 if (!err) {
1173 err = fuse_copy_one(cs, &forget->forget_one,
1174 sizeof(forget->forget_one));
1175 }
1176 head = forget->next;
1177 kfree(forget);
1178 }
1179
1180 fuse_copy_finish(cs);
1181
1182 if (err)
1183 return err;
1184
1185 return ih.len;
1186}
1187
Miklos Szeredifd22d622015-07-01 16:26:03 +02001188static int fuse_read_forget(struct fuse_conn *fc, struct fuse_iqueue *fiq,
1189 struct fuse_copy_state *cs,
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001190 size_t nbytes)
Eric Biggers76e43c82019-09-08 20:15:18 -07001191__releases(fiq->lock)
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001192{
Miklos Szeredif88996a2015-07-01 16:26:01 +02001193 if (fc->minor < 16 || fiq->forget_list_head.next->next == NULL)
Miklos Szeredifd22d622015-07-01 16:26:03 +02001194 return fuse_read_single_forget(fiq, cs, nbytes);
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001195 else
Miklos Szeredifd22d622015-07-01 16:26:03 +02001196 return fuse_read_batch_forget(fiq, cs, nbytes);
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001197}
1198
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001199/*
Miklos Szeredi334f4852005-09-09 13:10:27 -07001200 * Read a single request into the userspace filesystem's buffer. This
1201 * function waits until a request is available, then removes it from
1202 * the pending list and copies request data to userspace buffer. If
Miklos Szeredif9a28422006-06-25 05:48:53 -07001203 * no reply is needed (FORGET) or request has been aborted or there
1204 * was an error during the copying then it's finished by calling
Stefan Hajnoczi04ec5af2018-06-21 09:33:40 +01001205 * fuse_request_end(). Otherwise add it to the processing list, and set
Miklos Szeredi334f4852005-09-09 13:10:27 -07001206 * the 'sent' flag.
1207 */
Miklos Szeredic36960462015-07-01 16:26:09 +02001208static ssize_t fuse_dev_do_read(struct fuse_dev *fud, struct file *file,
Miklos Szeredic3021622010-05-25 15:06:07 +02001209 struct fuse_copy_state *cs, size_t nbytes)
Miklos Szeredi334f4852005-09-09 13:10:27 -07001210{
Miklos Szeredi82cbdcd2015-07-01 16:26:05 +02001211 ssize_t err;
Miklos Szeredic36960462015-07-01 16:26:09 +02001212 struct fuse_conn *fc = fud->fc;
Miklos Szeredif88996a2015-07-01 16:26:01 +02001213 struct fuse_iqueue *fiq = &fc->iq;
Miklos Szeredic36960462015-07-01 16:26:09 +02001214 struct fuse_pqueue *fpq = &fud->pq;
Miklos Szeredi334f4852005-09-09 13:10:27 -07001215 struct fuse_req *req;
Miklos Szeredid4993772019-09-10 15:04:11 +02001216 struct fuse_args *args;
Miklos Szeredi334f4852005-09-09 13:10:27 -07001217 unsigned reqsize;
Kirill Tkhaibe2ff422018-09-11 13:12:14 +03001218 unsigned int hash;
Miklos Szeredi334f4852005-09-09 13:10:27 -07001219
Kirill Smelkov1fb027d2019-07-08 17:03:31 +00001220 /*
1221 * Require sane minimum read buffer - that has capacity for fixed part
1222 * of any request header + negotiated max_write room for data.
1223 *
1224 * Historically libfuse reserves 4K for fixed header room, but e.g.
1225 * GlusterFS reserves only 80 bytes
1226 *
1227 * = `sizeof(fuse_in_header) + sizeof(fuse_write_in)`
1228 *
1229 * which is the absolute minimum any sane filesystem should be using
1230 * for header room.
1231 */
1232 if (nbytes < max_t(size_t, FUSE_MIN_READ_BUFFER,
1233 sizeof(struct fuse_in_header) +
1234 sizeof(struct fuse_write_in) +
1235 fc->max_write))
1236 return -EINVAL;
1237
Miklos Szeredi1d3d7522006-01-06 00:19:40 -08001238 restart:
Eric Biggers76e43c82019-09-08 20:15:18 -07001239 for (;;) {
1240 spin_lock(&fiq->lock);
1241 if (!fiq->connected || request_pending(fiq))
1242 break;
1243 spin_unlock(&fiq->lock);
Jeff Dikee5ac1d12006-04-10 22:54:53 -07001244
Eric Biggers76e43c82019-09-08 20:15:18 -07001245 if (file->f_flags & O_NONBLOCK)
1246 return -EAGAIN;
1247 err = wait_event_interruptible_exclusive(fiq->waitq,
Miklos Szeredi52509212015-07-01 16:26:03 +02001248 !fiq->connected || request_pending(fiq));
Eric Biggers76e43c82019-09-08 20:15:18 -07001249 if (err)
1250 return err;
1251 }
Miklos Szeredi52509212015-07-01 16:26:03 +02001252
Szymon Lukasz3b7008b2017-11-09 21:23:35 +01001253 if (!fiq->connected) {
Miklos Szeredieb98e3bd2019-01-24 10:40:16 +01001254 err = fc->aborted ? -ECONNABORTED : -ENODEV;
Miklos Szeredi334f4852005-09-09 13:10:27 -07001255 goto err_unlock;
Szymon Lukasz3b7008b2017-11-09 21:23:35 +01001256 }
Miklos Szeredi334f4852005-09-09 13:10:27 -07001257
Miklos Szeredif88996a2015-07-01 16:26:01 +02001258 if (!list_empty(&fiq->interrupts)) {
1259 req = list_entry(fiq->interrupts.next, struct fuse_req,
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001260 intr_entry);
Miklos Szeredifd22d622015-07-01 16:26:03 +02001261 return fuse_read_interrupt(fiq, cs, nbytes, req);
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001262 }
1263
Miklos Szeredif88996a2015-07-01 16:26:01 +02001264 if (forget_pending(fiq)) {
1265 if (list_empty(&fiq->pending) || fiq->forget_batch-- > 0)
Miklos Szeredifd22d622015-07-01 16:26:03 +02001266 return fuse_read_forget(fc, fiq, cs, nbytes);
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001267
Miklos Szeredif88996a2015-07-01 16:26:01 +02001268 if (fiq->forget_batch <= -8)
1269 fiq->forget_batch = 16;
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001270 }
1271
Miklos Szeredif88996a2015-07-01 16:26:01 +02001272 req = list_entry(fiq->pending.next, struct fuse_req, list);
Miklos Szeredi33e14b42015-07-01 16:26:01 +02001273 clear_bit(FR_PENDING, &req->flags);
Miklos Szeredief759252015-07-01 16:26:02 +02001274 list_del_init(&req->list);
Eric Biggers76e43c82019-09-08 20:15:18 -07001275 spin_unlock(&fiq->lock);
Miklos Szeredi4ce60812015-07-01 16:26:02 +02001276
Miklos Szeredid4993772019-09-10 15:04:11 +02001277 args = req->args;
1278 reqsize = req->in.h.len;
Miklos Szeredi5d6d3a32017-09-12 16:57:53 +02001279
Miklos Szeredi1d3d7522006-01-06 00:19:40 -08001280 /* If request is too large, reply with an error and restart the read */
Miklos Szeredic3021622010-05-25 15:06:07 +02001281 if (nbytes < reqsize) {
Miklos Szeredi1d3d7522006-01-06 00:19:40 -08001282 req->out.h.error = -EIO;
1283 /* SETXATTR is special, since it may contain too large data */
Miklos Szeredid4993772019-09-10 15:04:11 +02001284 if (args->opcode == FUSE_SETXATTR)
Miklos Szeredi1d3d7522006-01-06 00:19:40 -08001285 req->out.h.error = -E2BIG;
Max Reitz8f622e92020-04-20 17:59:34 +02001286 fuse_request_end(req);
Miklos Szeredi1d3d7522006-01-06 00:19:40 -08001287 goto restart;
Miklos Szeredi334f4852005-09-09 13:10:27 -07001288 }
Miklos Szeredi45a91cb2015-07-01 16:26:06 +02001289 spin_lock(&fpq->lock);
Miklos Szeredibb7ee902021-06-22 09:15:35 +02001290 /*
1291 * Must not put request on fpq->io queue after having been shut down by
1292 * fuse_abort_conn()
1293 */
1294 if (!fpq->connected) {
1295 req->out.h.error = err = -ECONNABORTED;
1296 goto out_end;
1297
1298 }
Miklos Szeredi82cbdcd2015-07-01 16:26:05 +02001299 list_add(&req->list, &fpq->io);
Miklos Szeredi45a91cb2015-07-01 16:26:06 +02001300 spin_unlock(&fpq->lock);
Miklos Szeredic3021622010-05-25 15:06:07 +02001301 cs->req = req;
Miklos Szeredid4993772019-09-10 15:04:11 +02001302 err = fuse_copy_one(cs, &req->in.h, sizeof(req->in.h));
Miklos Szeredi1d3d7522006-01-06 00:19:40 -08001303 if (!err)
Miklos Szeredid4993772019-09-10 15:04:11 +02001304 err = fuse_copy_args(cs, args->in_numargs, args->in_pages,
1305 (struct fuse_arg *) args->in_args, 0);
Miklos Szeredic3021622010-05-25 15:06:07 +02001306 fuse_copy_finish(cs);
Miklos Szeredi45a91cb2015-07-01 16:26:06 +02001307 spin_lock(&fpq->lock);
Miklos Szeredi825d6d32015-07-01 16:25:58 +02001308 clear_bit(FR_LOCKED, &req->flags);
Miklos Szeredie96edd92015-07-01 16:26:04 +02001309 if (!fpq->connected) {
Miklos Szeredieb98e3bd2019-01-24 10:40:16 +01001310 err = fc->aborted ? -ECONNABORTED : -ENODEV;
Miklos Szeredi82cbdcd2015-07-01 16:26:05 +02001311 goto out_end;
Miklos Szeredic9c9d7d2007-10-16 23:31:05 -07001312 }
Miklos Szeredi334f4852005-09-09 13:10:27 -07001313 if (err) {
Miklos Szeredic9c9d7d2007-10-16 23:31:05 -07001314 req->out.h.error = -EIO;
Miklos Szeredi82cbdcd2015-07-01 16:26:05 +02001315 goto out_end;
Miklos Szeredi334f4852005-09-09 13:10:27 -07001316 }
Miklos Szeredi825d6d32015-07-01 16:25:58 +02001317 if (!test_bit(FR_ISREPLY, &req->flags)) {
Miklos Szeredi82cbdcd2015-07-01 16:26:05 +02001318 err = reqsize;
1319 goto out_end;
Miklos Szeredi334f4852005-09-09 13:10:27 -07001320 }
Kirill Tkhaibe2ff422018-09-11 13:12:14 +03001321 hash = fuse_req_hash(req->in.h.unique);
1322 list_move_tail(&req->list, &fpq->processing[hash]);
Kirill Tkhaibc78abb2018-09-25 12:28:55 +03001323 __fuse_get_request(req);
Miklos Szeredi82cbdcd2015-07-01 16:26:05 +02001324 set_bit(FR_SENT, &req->flags);
Miklos Szeredi4c316f22018-09-28 16:43:22 +02001325 spin_unlock(&fpq->lock);
Miklos Szeredi82cbdcd2015-07-01 16:26:05 +02001326 /* matches barrier in request_wait_answer() */
1327 smp_mb__after_atomic();
1328 if (test_bit(FR_INTERRUPTED, &req->flags))
Max Reitz8f622e92020-04-20 17:59:34 +02001329 queue_interrupt(req);
1330 fuse_put_request(req);
Miklos Szeredi82cbdcd2015-07-01 16:26:05 +02001331
Miklos Szeredi334f4852005-09-09 13:10:27 -07001332 return reqsize;
1333
Miklos Szeredi82cbdcd2015-07-01 16:26:05 +02001334out_end:
Miklos Szeredi77cd9d42015-07-01 16:26:06 +02001335 if (!test_bit(FR_PRIVATE, &req->flags))
1336 list_del_init(&req->list);
Miklos Szeredi45a91cb2015-07-01 16:26:06 +02001337 spin_unlock(&fpq->lock);
Max Reitz8f622e92020-04-20 17:59:34 +02001338 fuse_request_end(req);
Miklos Szeredi82cbdcd2015-07-01 16:26:05 +02001339 return err;
1340
Miklos Szeredi334f4852005-09-09 13:10:27 -07001341 err_unlock:
Eric Biggers76e43c82019-09-08 20:15:18 -07001342 spin_unlock(&fiq->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001343 return err;
1344}
1345
Tom Van Braeckel94e4fe22015-01-12 05:22:16 +01001346static int fuse_dev_open(struct inode *inode, struct file *file)
1347{
1348 /*
1349 * The fuse device's file's private_data is used to hold
1350 * the fuse_conn(ection) when it is mounted, and is used to
1351 * keep track of whether the file has been mounted already.
1352 */
1353 file->private_data = NULL;
1354 return 0;
1355}
1356
Al Virofbdbacc2015-04-03 21:53:39 -04001357static ssize_t fuse_dev_read(struct kiocb *iocb, struct iov_iter *to)
Miklos Szeredic3021622010-05-25 15:06:07 +02001358{
1359 struct fuse_copy_state cs;
1360 struct file *file = iocb->ki_filp;
Miklos Szeredicc080e92015-07-01 16:26:08 +02001361 struct fuse_dev *fud = fuse_get_dev(file);
1362
1363 if (!fud)
Miklos Szeredic3021622010-05-25 15:06:07 +02001364 return -EPERM;
1365
Al Virofbdbacc2015-04-03 21:53:39 -04001366 if (!iter_is_iovec(to))
1367 return -EINVAL;
Miklos Szeredic3021622010-05-25 15:06:07 +02001368
Miklos Szeredidc008092015-07-01 16:25:58 +02001369 fuse_copy_init(&cs, 1, to);
Al Virofbdbacc2015-04-03 21:53:39 -04001370
Miklos Szeredic36960462015-07-01 16:26:09 +02001371 return fuse_dev_do_read(fud, file, &cs, iov_iter_count(to));
Miklos Szeredic3021622010-05-25 15:06:07 +02001372}
1373
Miklos Szeredic3021622010-05-25 15:06:07 +02001374static ssize_t fuse_dev_splice_read(struct file *in, loff_t *ppos,
1375 struct pipe_inode_info *pipe,
1376 size_t len, unsigned int flags)
1377{
Al Virod82718e2016-09-17 22:56:25 -04001378 int total, ret;
Miklos Szeredic3021622010-05-25 15:06:07 +02001379 int page_nr = 0;
Miklos Szeredic3021622010-05-25 15:06:07 +02001380 struct pipe_buffer *bufs;
1381 struct fuse_copy_state cs;
Miklos Szeredicc080e92015-07-01 16:26:08 +02001382 struct fuse_dev *fud = fuse_get_dev(in);
1383
1384 if (!fud)
Miklos Szeredic3021622010-05-25 15:06:07 +02001385 return -EPERM;
1386
David Howells6718b6f2019-10-16 16:47:32 +01001387 bufs = kvmalloc_array(pipe->max_usage, sizeof(struct pipe_buffer),
Andrey Ryabinind6d931a2018-07-17 19:00:34 +03001388 GFP_KERNEL);
Miklos Szeredic3021622010-05-25 15:06:07 +02001389 if (!bufs)
1390 return -ENOMEM;
1391
Miklos Szeredidc008092015-07-01 16:25:58 +02001392 fuse_copy_init(&cs, 1, NULL);
Miklos Szeredic3021622010-05-25 15:06:07 +02001393 cs.pipebufs = bufs;
1394 cs.pipe = pipe;
Miklos Szeredic36960462015-07-01 16:26:09 +02001395 ret = fuse_dev_do_read(fud, in, &cs, len);
Miklos Szeredic3021622010-05-25 15:06:07 +02001396 if (ret < 0)
1397 goto out;
1398
David Howells6718b6f2019-10-16 16:47:32 +01001399 if (pipe_occupancy(pipe->head, pipe->tail) + cs.nr_segs > pipe->max_usage) {
Miklos Szeredic3021622010-05-25 15:06:07 +02001400 ret = -EIO;
Al Virod82718e2016-09-17 22:56:25 -04001401 goto out;
Miklos Szeredic3021622010-05-25 15:06:07 +02001402 }
1403
Al Virod82718e2016-09-17 22:56:25 -04001404 for (ret = total = 0; page_nr < cs.nr_segs; total += ret) {
Miklos Szeredi28a625c2014-01-22 19:36:57 +01001405 /*
1406 * Need to be careful about this. Having buf->ops in module
1407 * code can Oops if the buffer persists after module unload.
1408 */
Al Virod82718e2016-09-17 22:56:25 -04001409 bufs[page_nr].ops = &nosteal_pipe_buf_ops;
Miklos Szeredi84588a92017-02-16 15:08:20 +01001410 bufs[page_nr].flags = 0;
Al Virod82718e2016-09-17 22:56:25 -04001411 ret = add_to_pipe(pipe, &bufs[page_nr++]);
1412 if (unlikely(ret < 0))
1413 break;
Miklos Szeredic3021622010-05-25 15:06:07 +02001414 }
Al Virod82718e2016-09-17 22:56:25 -04001415 if (total)
1416 ret = total;
Miklos Szeredic3021622010-05-25 15:06:07 +02001417out:
1418 for (; page_nr < cs.nr_segs; page_nr++)
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001419 put_page(bufs[page_nr].page);
Miklos Szeredic3021622010-05-25 15:06:07 +02001420
Andrey Ryabinind6d931a2018-07-17 19:00:34 +03001421 kvfree(bufs);
Miklos Szeredic3021622010-05-25 15:06:07 +02001422 return ret;
1423}
1424
Tejun Heo95668a62008-11-26 12:03:55 +01001425static int fuse_notify_poll(struct fuse_conn *fc, unsigned int size,
1426 struct fuse_copy_state *cs)
1427{
1428 struct fuse_notify_poll_wakeup_out outarg;
Miklos Szeredif6d47a12009-01-26 15:00:59 +01001429 int err = -EINVAL;
Tejun Heo95668a62008-11-26 12:03:55 +01001430
1431 if (size != sizeof(outarg))
Miklos Szeredif6d47a12009-01-26 15:00:59 +01001432 goto err;
Tejun Heo95668a62008-11-26 12:03:55 +01001433
1434 err = fuse_copy_one(cs, &outarg, sizeof(outarg));
1435 if (err)
Miklos Szeredif6d47a12009-01-26 15:00:59 +01001436 goto err;
Tejun Heo95668a62008-11-26 12:03:55 +01001437
Miklos Szeredif6d47a12009-01-26 15:00:59 +01001438 fuse_copy_finish(cs);
Tejun Heo95668a62008-11-26 12:03:55 +01001439 return fuse_notify_poll_wakeup(fc, &outarg);
Miklos Szeredif6d47a12009-01-26 15:00:59 +01001440
1441err:
1442 fuse_copy_finish(cs);
1443 return err;
Tejun Heo95668a62008-11-26 12:03:55 +01001444}
1445
John Muir3b463ae2009-05-31 11:13:57 -04001446static int fuse_notify_inval_inode(struct fuse_conn *fc, unsigned int size,
1447 struct fuse_copy_state *cs)
1448{
1449 struct fuse_notify_inval_inode_out outarg;
1450 int err = -EINVAL;
1451
1452 if (size != sizeof(outarg))
1453 goto err;
1454
1455 err = fuse_copy_one(cs, &outarg, sizeof(outarg));
1456 if (err)
1457 goto err;
1458 fuse_copy_finish(cs);
1459
1460 down_read(&fc->killsb);
Max Reitzfcee2162020-05-06 17:44:12 +02001461 err = fuse_reverse_inval_inode(fc, outarg.ino,
1462 outarg.off, outarg.len);
John Muir3b463ae2009-05-31 11:13:57 -04001463 up_read(&fc->killsb);
1464 return err;
1465
1466err:
1467 fuse_copy_finish(cs);
1468 return err;
1469}
1470
1471static int fuse_notify_inval_entry(struct fuse_conn *fc, unsigned int size,
1472 struct fuse_copy_state *cs)
1473{
1474 struct fuse_notify_inval_entry_out outarg;
Fang Wenqib2d82ee2009-12-30 18:37:13 +08001475 int err = -ENOMEM;
1476 char *buf;
John Muir3b463ae2009-05-31 11:13:57 -04001477 struct qstr name;
1478
Fang Wenqib2d82ee2009-12-30 18:37:13 +08001479 buf = kzalloc(FUSE_NAME_MAX + 1, GFP_KERNEL);
1480 if (!buf)
1481 goto err;
1482
1483 err = -EINVAL;
John Muir3b463ae2009-05-31 11:13:57 -04001484 if (size < sizeof(outarg))
1485 goto err;
1486
1487 err = fuse_copy_one(cs, &outarg, sizeof(outarg));
1488 if (err)
1489 goto err;
1490
1491 err = -ENAMETOOLONG;
1492 if (outarg.namelen > FUSE_NAME_MAX)
1493 goto err;
1494
Miklos Szeredic2183d12011-08-24 10:20:17 +02001495 err = -EINVAL;
1496 if (size != sizeof(outarg) + outarg.namelen + 1)
1497 goto err;
1498
John Muir3b463ae2009-05-31 11:13:57 -04001499 name.name = buf;
1500 name.len = outarg.namelen;
1501 err = fuse_copy_one(cs, buf, outarg.namelen + 1);
1502 if (err)
1503 goto err;
1504 fuse_copy_finish(cs);
1505 buf[outarg.namelen] = 0;
John Muir3b463ae2009-05-31 11:13:57 -04001506
1507 down_read(&fc->killsb);
Max Reitzfcee2162020-05-06 17:44:12 +02001508 err = fuse_reverse_inval_entry(fc, outarg.parent, 0, &name);
John Muir451d0f52011-12-06 21:50:06 +01001509 up_read(&fc->killsb);
1510 kfree(buf);
1511 return err;
1512
1513err:
1514 kfree(buf);
1515 fuse_copy_finish(cs);
1516 return err;
1517}
1518
1519static int fuse_notify_delete(struct fuse_conn *fc, unsigned int size,
1520 struct fuse_copy_state *cs)
1521{
1522 struct fuse_notify_delete_out outarg;
1523 int err = -ENOMEM;
1524 char *buf;
1525 struct qstr name;
1526
1527 buf = kzalloc(FUSE_NAME_MAX + 1, GFP_KERNEL);
1528 if (!buf)
1529 goto err;
1530
1531 err = -EINVAL;
1532 if (size < sizeof(outarg))
1533 goto err;
1534
1535 err = fuse_copy_one(cs, &outarg, sizeof(outarg));
1536 if (err)
1537 goto err;
1538
1539 err = -ENAMETOOLONG;
1540 if (outarg.namelen > FUSE_NAME_MAX)
1541 goto err;
1542
1543 err = -EINVAL;
1544 if (size != sizeof(outarg) + outarg.namelen + 1)
1545 goto err;
1546
1547 name.name = buf;
1548 name.len = outarg.namelen;
1549 err = fuse_copy_one(cs, buf, outarg.namelen + 1);
1550 if (err)
1551 goto err;
1552 fuse_copy_finish(cs);
1553 buf[outarg.namelen] = 0;
John Muir451d0f52011-12-06 21:50:06 +01001554
1555 down_read(&fc->killsb);
Max Reitzfcee2162020-05-06 17:44:12 +02001556 err = fuse_reverse_inval_entry(fc, outarg.parent, outarg.child, &name);
John Muir3b463ae2009-05-31 11:13:57 -04001557 up_read(&fc->killsb);
Fang Wenqib2d82ee2009-12-30 18:37:13 +08001558 kfree(buf);
John Muir3b463ae2009-05-31 11:13:57 -04001559 return err;
1560
1561err:
Fang Wenqib2d82ee2009-12-30 18:37:13 +08001562 kfree(buf);
John Muir3b463ae2009-05-31 11:13:57 -04001563 fuse_copy_finish(cs);
1564 return err;
1565}
1566
Miklos Szeredia1d75f22010-07-12 14:41:40 +02001567static int fuse_notify_store(struct fuse_conn *fc, unsigned int size,
1568 struct fuse_copy_state *cs)
1569{
1570 struct fuse_notify_store_out outarg;
1571 struct inode *inode;
1572 struct address_space *mapping;
1573 u64 nodeid;
1574 int err;
1575 pgoff_t index;
1576 unsigned int offset;
1577 unsigned int num;
1578 loff_t file_size;
1579 loff_t end;
1580
1581 err = -EINVAL;
1582 if (size < sizeof(outarg))
1583 goto out_finish;
1584
1585 err = fuse_copy_one(cs, &outarg, sizeof(outarg));
1586 if (err)
1587 goto out_finish;
1588
1589 err = -EINVAL;
1590 if (size - sizeof(outarg) != outarg.size)
1591 goto out_finish;
1592
1593 nodeid = outarg.nodeid;
1594
1595 down_read(&fc->killsb);
1596
1597 err = -ENOENT;
Max Reitzfcee2162020-05-06 17:44:12 +02001598 inode = fuse_ilookup(fc, nodeid, NULL);
Miklos Szeredia1d75f22010-07-12 14:41:40 +02001599 if (!inode)
1600 goto out_up_killsb;
1601
1602 mapping = inode->i_mapping;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001603 index = outarg.offset >> PAGE_SHIFT;
1604 offset = outarg.offset & ~PAGE_MASK;
Miklos Szeredia1d75f22010-07-12 14:41:40 +02001605 file_size = i_size_read(inode);
1606 end = outarg.offset + outarg.size;
1607 if (end > file_size) {
1608 file_size = end;
1609 fuse_write_update_size(inode, file_size);
1610 }
1611
1612 num = outarg.size;
1613 while (num) {
1614 struct page *page;
1615 unsigned int this_num;
1616
1617 err = -ENOMEM;
1618 page = find_or_create_page(mapping, index,
1619 mapping_gfp_mask(mapping));
1620 if (!page)
1621 goto out_iput;
1622
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001623 this_num = min_t(unsigned, num, PAGE_SIZE - offset);
Miklos Szeredia1d75f22010-07-12 14:41:40 +02001624 err = fuse_copy_page(cs, &page, offset, this_num, 0);
Miklos Szeredi063ec1e2014-01-22 19:36:58 +01001625 if (!err && offset == 0 &&
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001626 (this_num == PAGE_SIZE || file_size == end))
Miklos Szeredia1d75f22010-07-12 14:41:40 +02001627 SetPageUptodate(page);
1628 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001629 put_page(page);
Miklos Szeredia1d75f22010-07-12 14:41:40 +02001630
1631 if (err)
1632 goto out_iput;
1633
1634 num -= this_num;
1635 offset = 0;
1636 index++;
1637 }
1638
1639 err = 0;
1640
1641out_iput:
1642 iput(inode);
1643out_up_killsb:
1644 up_read(&fc->killsb);
1645out_finish:
1646 fuse_copy_finish(cs);
1647 return err;
1648}
1649
Miklos Szeredi75b399d2019-09-10 15:04:11 +02001650struct fuse_retrieve_args {
1651 struct fuse_args_pages ap;
1652 struct fuse_notify_retrieve_in inarg;
1653};
1654
Max Reitzfcee2162020-05-06 17:44:12 +02001655static void fuse_retrieve_end(struct fuse_mount *fm, struct fuse_args *args,
Miklos Szeredi75b399d2019-09-10 15:04:11 +02001656 int error)
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001657{
Miklos Szeredi75b399d2019-09-10 15:04:11 +02001658 struct fuse_retrieve_args *ra =
1659 container_of(args, typeof(*ra), ap.args);
1660
1661 release_pages(ra->ap.pages, ra->ap.num_pages);
1662 kfree(ra);
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001663}
1664
Max Reitzfcee2162020-05-06 17:44:12 +02001665static int fuse_retrieve(struct fuse_mount *fm, struct inode *inode,
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001666 struct fuse_notify_retrieve_out *outarg)
1667{
1668 int err;
1669 struct address_space *mapping = inode->i_mapping;
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001670 pgoff_t index;
1671 loff_t file_size;
1672 unsigned int num;
1673 unsigned int offset;
Geert Uytterhoeven01574432010-09-30 22:06:21 +02001674 size_t total_len = 0;
Constantine Shulyupin5da784c2018-09-06 15:37:06 +03001675 unsigned int num_pages;
Max Reitzfcee2162020-05-06 17:44:12 +02001676 struct fuse_conn *fc = fm->fc;
Miklos Szeredi75b399d2019-09-10 15:04:11 +02001677 struct fuse_retrieve_args *ra;
1678 size_t args_size = sizeof(*ra);
1679 struct fuse_args_pages *ap;
1680 struct fuse_args *args;
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001681
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001682 offset = outarg->offset & ~PAGE_MASK;
Maxim Patlasov4d53dc92012-10-26 19:48:42 +04001683 file_size = i_size_read(inode);
1684
Kirill Smelkov76406822019-03-27 10:15:19 +00001685 num = min(outarg->size, fc->max_write);
Maxim Patlasov4d53dc92012-10-26 19:48:42 +04001686 if (outarg->offset > file_size)
1687 num = 0;
1688 else if (outarg->offset + num > file_size)
1689 num = file_size - outarg->offset;
1690
1691 num_pages = (num + offset + PAGE_SIZE - 1) >> PAGE_SHIFT;
Constantine Shulyupin5da784c2018-09-06 15:37:06 +03001692 num_pages = min(num_pages, fc->max_pages);
Maxim Patlasov4d53dc92012-10-26 19:48:42 +04001693
Miklos Szeredi75b399d2019-09-10 15:04:11 +02001694 args_size += num_pages * (sizeof(ap->pages[0]) + sizeof(ap->descs[0]));
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001695
Miklos Szeredi75b399d2019-09-10 15:04:11 +02001696 ra = kzalloc(args_size, GFP_KERNEL);
1697 if (!ra)
1698 return -ENOMEM;
1699
1700 ap = &ra->ap;
1701 ap->pages = (void *) (ra + 1);
1702 ap->descs = (void *) (ap->pages + num_pages);
1703
1704 args = &ap->args;
1705 args->nodeid = outarg->nodeid;
1706 args->opcode = FUSE_NOTIFY_REPLY;
1707 args->in_numargs = 2;
1708 args->in_pages = true;
1709 args->end = fuse_retrieve_end;
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001710
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001711 index = outarg->offset >> PAGE_SHIFT;
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001712
Miklos Szeredi75b399d2019-09-10 15:04:11 +02001713 while (num && ap->num_pages < num_pages) {
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001714 struct page *page;
1715 unsigned int this_num;
1716
1717 page = find_get_page(mapping, index);
1718 if (!page)
1719 break;
1720
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001721 this_num = min_t(unsigned, num, PAGE_SIZE - offset);
Miklos Szeredi75b399d2019-09-10 15:04:11 +02001722 ap->pages[ap->num_pages] = page;
1723 ap->descs[ap->num_pages].offset = offset;
1724 ap->descs[ap->num_pages].length = this_num;
1725 ap->num_pages++;
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001726
Miklos Szeredic9e67d42012-09-04 18:45:54 +02001727 offset = 0;
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001728 num -= this_num;
1729 total_len += this_num;
Miklos Szeredi48706d02011-12-13 10:36:59 +01001730 index++;
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001731 }
Miklos Szeredi75b399d2019-09-10 15:04:11 +02001732 ra->inarg.offset = outarg->offset;
1733 ra->inarg.size = total_len;
1734 args->in_args[0].size = sizeof(ra->inarg);
1735 args->in_args[0].value = &ra->inarg;
1736 args->in_args[1].size = total_len;
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001737
Max Reitzfcee2162020-05-06 17:44:12 +02001738 err = fuse_simple_notify_reply(fm, args, outarg->notify_unique);
Miklos Szeredi75b399d2019-09-10 15:04:11 +02001739 if (err)
Max Reitzfcee2162020-05-06 17:44:12 +02001740 fuse_retrieve_end(fm, args, err);
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001741
1742 return err;
1743}
1744
1745static int fuse_notify_retrieve(struct fuse_conn *fc, unsigned int size,
1746 struct fuse_copy_state *cs)
1747{
1748 struct fuse_notify_retrieve_out outarg;
Max Reitzfcee2162020-05-06 17:44:12 +02001749 struct fuse_mount *fm;
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001750 struct inode *inode;
Max Reitzfcee2162020-05-06 17:44:12 +02001751 u64 nodeid;
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001752 int err;
1753
1754 err = -EINVAL;
1755 if (size != sizeof(outarg))
1756 goto copy_finish;
1757
1758 err = fuse_copy_one(cs, &outarg, sizeof(outarg));
1759 if (err)
1760 goto copy_finish;
1761
1762 fuse_copy_finish(cs);
1763
1764 down_read(&fc->killsb);
1765 err = -ENOENT;
Max Reitzfcee2162020-05-06 17:44:12 +02001766 nodeid = outarg.nodeid;
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001767
Max Reitzfcee2162020-05-06 17:44:12 +02001768 inode = fuse_ilookup(fc, nodeid, &fm);
1769 if (inode) {
1770 err = fuse_retrieve(fm, inode, &outarg);
1771 iput(inode);
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001772 }
1773 up_read(&fc->killsb);
1774
1775 return err;
1776
1777copy_finish:
1778 fuse_copy_finish(cs);
1779 return err;
1780}
1781
Tejun Heo85993962008-11-26 12:03:55 +01001782static int fuse_notify(struct fuse_conn *fc, enum fuse_notify_code code,
1783 unsigned int size, struct fuse_copy_state *cs)
1784{
Miklos Szeredi0d278362015-02-26 11:45:47 +01001785 /* Don't try to move pages (yet) */
1786 cs->move_pages = 0;
1787
Tejun Heo85993962008-11-26 12:03:55 +01001788 switch (code) {
Tejun Heo95668a62008-11-26 12:03:55 +01001789 case FUSE_NOTIFY_POLL:
1790 return fuse_notify_poll(fc, size, cs);
1791
John Muir3b463ae2009-05-31 11:13:57 -04001792 case FUSE_NOTIFY_INVAL_INODE:
1793 return fuse_notify_inval_inode(fc, size, cs);
1794
1795 case FUSE_NOTIFY_INVAL_ENTRY:
1796 return fuse_notify_inval_entry(fc, size, cs);
1797
Miklos Szeredia1d75f22010-07-12 14:41:40 +02001798 case FUSE_NOTIFY_STORE:
1799 return fuse_notify_store(fc, size, cs);
1800
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001801 case FUSE_NOTIFY_RETRIEVE:
1802 return fuse_notify_retrieve(fc, size, cs);
1803
John Muir451d0f52011-12-06 21:50:06 +01001804 case FUSE_NOTIFY_DELETE:
1805 return fuse_notify_delete(fc, size, cs);
1806
Tejun Heo85993962008-11-26 12:03:55 +01001807 default:
Miklos Szeredif6d47a12009-01-26 15:00:59 +01001808 fuse_copy_finish(cs);
Tejun Heo85993962008-11-26 12:03:55 +01001809 return -EINVAL;
1810 }
1811}
1812
Miklos Szeredi334f4852005-09-09 13:10:27 -07001813/* Look up request on processing list by unique ID */
Miklos Szeredi3a2b5b92015-07-01 16:26:04 +02001814static struct fuse_req *request_find(struct fuse_pqueue *fpq, u64 unique)
Miklos Szeredi334f4852005-09-09 13:10:27 -07001815{
Kirill Tkhaibe2ff422018-09-11 13:12:14 +03001816 unsigned int hash = fuse_req_hash(unique);
Dong Fang05726ac2013-07-30 22:50:01 -04001817 struct fuse_req *req;
Miklos Szeredi334f4852005-09-09 13:10:27 -07001818
Kirill Tkhaibe2ff422018-09-11 13:12:14 +03001819 list_for_each_entry(req, &fpq->processing[hash], list) {
Kirill Tkhai3a5358d2018-09-11 13:12:05 +03001820 if (req->in.h.unique == unique)
Miklos Szeredi334f4852005-09-09 13:10:27 -07001821 return req;
1822 }
1823 return NULL;
1824}
1825
Miklos Szeredid4993772019-09-10 15:04:11 +02001826static int copy_out_args(struct fuse_copy_state *cs, struct fuse_args *args,
Miklos Szeredi334f4852005-09-09 13:10:27 -07001827 unsigned nbytes)
1828{
1829 unsigned reqsize = sizeof(struct fuse_out_header);
1830
Stefan Hajnoczi14d46d72018-06-21 09:34:25 +01001831 reqsize += fuse_len_args(args->out_numargs, args->out_args);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001832
Miklos Szeredid4993772019-09-10 15:04:11 +02001833 if (reqsize < nbytes || (reqsize > nbytes && !args->out_argvar))
Miklos Szeredi334f4852005-09-09 13:10:27 -07001834 return -EINVAL;
1835 else if (reqsize > nbytes) {
Miklos Szeredid4993772019-09-10 15:04:11 +02001836 struct fuse_arg *lastarg = &args->out_args[args->out_numargs-1];
Miklos Szeredi334f4852005-09-09 13:10:27 -07001837 unsigned diffsize = reqsize - nbytes;
Miklos Szeredid4993772019-09-10 15:04:11 +02001838
Miklos Szeredi334f4852005-09-09 13:10:27 -07001839 if (diffsize > lastarg->size)
1840 return -EINVAL;
1841 lastarg->size -= diffsize;
1842 }
Miklos Szeredid4993772019-09-10 15:04:11 +02001843 return fuse_copy_args(cs, args->out_numargs, args->out_pages,
1844 args->out_args, args->page_zeroing);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001845}
1846
1847/*
1848 * Write a single reply to a request. First the header is copied from
1849 * the write buffer. The request is then searched on the processing
1850 * list by the unique ID found in the header. If found, then remove
1851 * it from the list and copy the rest of the buffer to the request.
Stefan Hajnoczi04ec5af2018-06-21 09:33:40 +01001852 * The request is finished by calling fuse_request_end().
Miklos Szeredi334f4852005-09-09 13:10:27 -07001853 */
Miklos Szeredic36960462015-07-01 16:26:09 +02001854static ssize_t fuse_dev_do_write(struct fuse_dev *fud,
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001855 struct fuse_copy_state *cs, size_t nbytes)
Miklos Szeredi334f4852005-09-09 13:10:27 -07001856{
1857 int err;
Miklos Szeredic36960462015-07-01 16:26:09 +02001858 struct fuse_conn *fc = fud->fc;
1859 struct fuse_pqueue *fpq = &fud->pq;
Miklos Szeredi334f4852005-09-09 13:10:27 -07001860 struct fuse_req *req;
1861 struct fuse_out_header oh;
Miklos Szeredi334f4852005-09-09 13:10:27 -07001862
Kirill Tkhai7407a102018-11-08 12:05:36 +03001863 err = -EINVAL;
Miklos Szeredi334f4852005-09-09 13:10:27 -07001864 if (nbytes < sizeof(struct fuse_out_header))
Kirill Tkhai7407a102018-11-08 12:05:36 +03001865 goto out;
Miklos Szeredi334f4852005-09-09 13:10:27 -07001866
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001867 err = fuse_copy_one(cs, &oh, sizeof(oh));
Miklos Szeredi334f4852005-09-09 13:10:27 -07001868 if (err)
Kirill Tkhai7407a102018-11-08 12:05:36 +03001869 goto copy_finish;
Tejun Heo85993962008-11-26 12:03:55 +01001870
Miklos Szeredi334f4852005-09-09 13:10:27 -07001871 err = -EINVAL;
Tejun Heo85993962008-11-26 12:03:55 +01001872 if (oh.len != nbytes)
Kirill Tkhai7407a102018-11-08 12:05:36 +03001873 goto copy_finish;
Tejun Heo85993962008-11-26 12:03:55 +01001874
1875 /*
1876 * Zero oh.unique indicates unsolicited notification message
1877 * and error contains notification code.
1878 */
1879 if (!oh.unique) {
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001880 err = fuse_notify(fc, oh.error, nbytes - sizeof(oh), cs);
Kirill Tkhai7407a102018-11-08 12:05:36 +03001881 goto out;
Tejun Heo85993962008-11-26 12:03:55 +01001882 }
1883
1884 err = -EINVAL;
Miklos Szeredi4eab2e22021-06-22 09:15:35 +02001885 if (oh.error <= -512 || oh.error > 0)
Kirill Tkhai7407a102018-11-08 12:05:36 +03001886 goto copy_finish;
Miklos Szeredi334f4852005-09-09 13:10:27 -07001887
Miklos Szeredi45a91cb2015-07-01 16:26:06 +02001888 spin_lock(&fpq->lock);
Kirill Tkhai7407a102018-11-08 12:05:36 +03001889 req = NULL;
1890 if (fpq->connected)
1891 req = request_find(fpq, oh.unique & ~FUSE_INT_REQ_BIT);
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08001892
Kirill Tkhai7407a102018-11-08 12:05:36 +03001893 err = -ENOENT;
1894 if (!req) {
1895 spin_unlock(&fpq->lock);
1896 goto copy_finish;
1897 }
Miklos Szeredi334f4852005-09-09 13:10:27 -07001898
Kirill Tkhai3a5358d2018-09-11 13:12:05 +03001899 /* Is it an interrupt reply ID? */
1900 if (oh.unique & FUSE_INT_REQ_BIT) {
Kirill Tkhaid2d2d4f2018-09-25 12:52:42 +03001901 __fuse_get_request(req);
Miklos Szeredi45a91cb2015-07-01 16:26:06 +02001902 spin_unlock(&fpq->lock);
1903
Kirill Tkhai7407a102018-11-08 12:05:36 +03001904 err = 0;
1905 if (nbytes != sizeof(struct fuse_out_header))
1906 err = -EINVAL;
1907 else if (oh.error == -ENOSYS)
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001908 fc->no_interrupt = 1;
1909 else if (oh.error == -EAGAIN)
Max Reitz8f622e92020-04-20 17:59:34 +02001910 err = queue_interrupt(req);
Kirill Tkhai7407a102018-11-08 12:05:36 +03001911
Max Reitz8f622e92020-04-20 17:59:34 +02001912 fuse_put_request(req);
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001913
Kirill Tkhai7407a102018-11-08 12:05:36 +03001914 goto copy_finish;
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001915 }
1916
Miklos Szeredi33e14b42015-07-01 16:26:01 +02001917 clear_bit(FR_SENT, &req->flags);
Miklos Szeredi3a2b5b92015-07-01 16:26:04 +02001918 list_move(&req->list, &fpq->io);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001919 req->out.h = oh;
Miklos Szeredi825d6d32015-07-01 16:25:58 +02001920 set_bit(FR_LOCKED, &req->flags);
Miklos Szeredi45a91cb2015-07-01 16:26:06 +02001921 spin_unlock(&fpq->lock);
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001922 cs->req = req;
Miklos Szeredid4993772019-09-10 15:04:11 +02001923 if (!req->args->page_replace)
Miklos Szeredice534fb2010-05-25 15:06:07 +02001924 cs->move_pages = 0;
Miklos Szeredi334f4852005-09-09 13:10:27 -07001925
Miklos Szeredid4993772019-09-10 15:04:11 +02001926 if (oh.error)
1927 err = nbytes != sizeof(oh) ? -EINVAL : 0;
1928 else
1929 err = copy_out_args(cs, req->args, nbytes);
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001930 fuse_copy_finish(cs);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001931
Daniel Rosenbergaca26512016-04-22 00:00:48 -07001932 if (!err && req->in.h.opcode == FUSE_CANONICAL_PATH) {
1933 char *path = (char *)req->args->out_args[0].value;
1934
1935 path[req->args->out_args[0].size - 1] = 0;
1936 req->out.h.error =
1937 kern_path(path, 0, req->args->canonical_path);
1938 }
1939
Miklos Szeredi45a91cb2015-07-01 16:26:06 +02001940 spin_lock(&fpq->lock);
Miklos Szeredi825d6d32015-07-01 16:25:58 +02001941 clear_bit(FR_LOCKED, &req->flags);
Miklos Szeredie96edd92015-07-01 16:26:04 +02001942 if (!fpq->connected)
Miklos Szeredi0d8e84b2015-07-01 16:25:58 +02001943 err = -ENOENT;
1944 else if (err)
Miklos Szeredi334f4852005-09-09 13:10:27 -07001945 req->out.h.error = -EIO;
Miklos Szeredi77cd9d42015-07-01 16:26:06 +02001946 if (!test_bit(FR_PRIVATE, &req->flags))
1947 list_del_init(&req->list);
Miklos Szeredi45a91cb2015-07-01 16:26:06 +02001948 spin_unlock(&fpq->lock);
Miklos Szeredi46c34a32015-07-01 16:26:07 +02001949
Max Reitz8f622e92020-04-20 17:59:34 +02001950 fuse_request_end(req);
Kirill Tkhai7407a102018-11-08 12:05:36 +03001951out:
Miklos Szeredi334f4852005-09-09 13:10:27 -07001952 return err ? err : nbytes;
1953
Kirill Tkhai7407a102018-11-08 12:05:36 +03001954copy_finish:
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001955 fuse_copy_finish(cs);
Kirill Tkhai7407a102018-11-08 12:05:36 +03001956 goto out;
Miklos Szeredi334f4852005-09-09 13:10:27 -07001957}
1958
Al Virofbdbacc2015-04-03 21:53:39 -04001959static ssize_t fuse_dev_write(struct kiocb *iocb, struct iov_iter *from)
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001960{
1961 struct fuse_copy_state cs;
Miklos Szeredicc080e92015-07-01 16:26:08 +02001962 struct fuse_dev *fud = fuse_get_dev(iocb->ki_filp);
1963
1964 if (!fud)
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001965 return -EPERM;
1966
Al Virofbdbacc2015-04-03 21:53:39 -04001967 if (!iter_is_iovec(from))
1968 return -EINVAL;
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001969
Miklos Szeredidc008092015-07-01 16:25:58 +02001970 fuse_copy_init(&cs, 0, from);
Al Virofbdbacc2015-04-03 21:53:39 -04001971
Miklos Szeredic36960462015-07-01 16:26:09 +02001972 return fuse_dev_do_write(fud, &cs, iov_iter_count(from));
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001973}
1974
1975static ssize_t fuse_dev_splice_write(struct pipe_inode_info *pipe,
1976 struct file *out, loff_t *ppos,
1977 size_t len, unsigned int flags)
1978{
David Howells8cefc102019-11-15 13:30:32 +00001979 unsigned int head, tail, mask, count;
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001980 unsigned nbuf;
1981 unsigned idx;
1982 struct pipe_buffer *bufs;
1983 struct fuse_copy_state cs;
Miklos Szeredicc080e92015-07-01 16:26:08 +02001984 struct fuse_dev *fud;
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001985 size_t rem;
1986 ssize_t ret;
1987
Miklos Szeredicc080e92015-07-01 16:26:08 +02001988 fud = fuse_get_dev(out);
1989 if (!fud)
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001990 return -EPERM;
1991
Andrey Ryabinina2477b02018-07-17 19:00:33 +03001992 pipe_lock(pipe);
1993
David Howells8cefc102019-11-15 13:30:32 +00001994 head = pipe->head;
1995 tail = pipe->tail;
1996 mask = pipe->ring_size - 1;
1997 count = head - tail;
1998
1999 bufs = kvmalloc_array(count, sizeof(struct pipe_buffer), GFP_KERNEL);
Andrey Ryabinina2477b02018-07-17 19:00:33 +03002000 if (!bufs) {
2001 pipe_unlock(pipe);
Miklos Szeredidd3bb142010-05-25 15:06:06 +02002002 return -ENOMEM;
Andrey Ryabinina2477b02018-07-17 19:00:33 +03002003 }
Miklos Szeredidd3bb142010-05-25 15:06:06 +02002004
Miklos Szeredidd3bb142010-05-25 15:06:06 +02002005 nbuf = 0;
2006 rem = 0;
David Howells76f67772019-12-06 21:34:51 +00002007 for (idx = tail; idx != head && rem < len; idx++)
David Howells8cefc102019-11-15 13:30:32 +00002008 rem += pipe->bufs[idx & mask].len;
Miklos Szeredidd3bb142010-05-25 15:06:06 +02002009
2010 ret = -EINVAL;
Matthew Wilcox15fab632019-04-05 14:02:10 -07002011 if (rem < len)
2012 goto out_free;
Miklos Szeredidd3bb142010-05-25 15:06:06 +02002013
2014 rem = len;
2015 while (rem) {
2016 struct pipe_buffer *ibuf;
2017 struct pipe_buffer *obuf;
2018
Vasily Averin0e9fb6f2019-08-19 09:53:50 +03002019 if (WARN_ON(nbuf >= count || tail == head))
2020 goto out_free;
2021
David Howells8cefc102019-11-15 13:30:32 +00002022 ibuf = &pipe->bufs[tail & mask];
Miklos Szeredidd3bb142010-05-25 15:06:06 +02002023 obuf = &bufs[nbuf];
2024
2025 if (rem >= ibuf->len) {
2026 *obuf = *ibuf;
2027 ibuf->ops = NULL;
David Howells8cefc102019-11-15 13:30:32 +00002028 tail++;
2029 pipe->tail = tail;
Miklos Szeredidd3bb142010-05-25 15:06:06 +02002030 } else {
Matthew Wilcox15fab632019-04-05 14:02:10 -07002031 if (!pipe_buf_get(pipe, ibuf))
2032 goto out_free;
2033
Miklos Szeredidd3bb142010-05-25 15:06:06 +02002034 *obuf = *ibuf;
2035 obuf->flags &= ~PIPE_BUF_FLAG_GIFT;
2036 obuf->len = rem;
2037 ibuf->offset += obuf->len;
2038 ibuf->len -= obuf->len;
2039 }
2040 nbuf++;
2041 rem -= obuf->len;
2042 }
2043 pipe_unlock(pipe);
2044
Miklos Szeredidc008092015-07-01 16:25:58 +02002045 fuse_copy_init(&cs, 0, NULL);
Miklos Szeredidd3bb142010-05-25 15:06:06 +02002046 cs.pipebufs = bufs;
Al Viro6c09e942015-04-03 22:06:08 -04002047 cs.nr_segs = nbuf;
Miklos Szeredidd3bb142010-05-25 15:06:06 +02002048 cs.pipe = pipe;
2049
Miklos Szeredice534fb2010-05-25 15:06:07 +02002050 if (flags & SPLICE_F_MOVE)
2051 cs.move_pages = 1;
2052
Miklos Szeredic36960462015-07-01 16:26:09 +02002053 ret = fuse_dev_do_write(fud, &cs, len);
Miklos Szeredidd3bb142010-05-25 15:06:06 +02002054
Jann Horn95099412019-01-12 02:39:05 +01002055 pipe_lock(pipe);
Matthew Wilcox15fab632019-04-05 14:02:10 -07002056out_free:
Miklos Szeredib05eea12021-11-02 11:10:37 +01002057 for (idx = 0; idx < nbuf; idx++) {
2058 struct pipe_buffer *buf = &bufs[idx];
2059
2060 if (buf->ops)
2061 pipe_buf_release(pipe, buf);
2062 }
Jann Horn95099412019-01-12 02:39:05 +01002063 pipe_unlock(pipe);
Miklos Szeredia7796382016-09-27 10:45:12 +02002064
Andrey Ryabinind6d931a2018-07-17 19:00:34 +03002065 kvfree(bufs);
Miklos Szeredidd3bb142010-05-25 15:06:06 +02002066 return ret;
2067}
2068
Al Viro076ccb72017-07-03 01:02:18 -04002069static __poll_t fuse_dev_poll(struct file *file, poll_table *wait)
Miklos Szeredi334f4852005-09-09 13:10:27 -07002070{
Linus Torvaldsa9a08842018-02-11 14:34:03 -08002071 __poll_t mask = EPOLLOUT | EPOLLWRNORM;
Miklos Szeredif88996a2015-07-01 16:26:01 +02002072 struct fuse_iqueue *fiq;
Miklos Szeredicc080e92015-07-01 16:26:08 +02002073 struct fuse_dev *fud = fuse_get_dev(file);
2074
2075 if (!fud)
Linus Torvaldsa9a08842018-02-11 14:34:03 -08002076 return EPOLLERR;
Miklos Szeredi334f4852005-09-09 13:10:27 -07002077
Miklos Szeredicc080e92015-07-01 16:26:08 +02002078 fiq = &fud->fc->iq;
Miklos Szeredif88996a2015-07-01 16:26:01 +02002079 poll_wait(file, &fiq->waitq, wait);
Miklos Szeredi334f4852005-09-09 13:10:27 -07002080
Eric Biggers76e43c82019-09-08 20:15:18 -07002081 spin_lock(&fiq->lock);
Miklos Szeredie16714d2015-07-01 16:26:01 +02002082 if (!fiq->connected)
Linus Torvaldsa9a08842018-02-11 14:34:03 -08002083 mask = EPOLLERR;
Miklos Szeredif88996a2015-07-01 16:26:01 +02002084 else if (request_pending(fiq))
Linus Torvaldsa9a08842018-02-11 14:34:03 -08002085 mask |= EPOLLIN | EPOLLRDNORM;
Eric Biggers76e43c82019-09-08 20:15:18 -07002086 spin_unlock(&fiq->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -07002087
2088 return mask;
2089}
2090
Kirill Tkhai34061752018-11-06 12:15:20 +03002091/* Abort all requests on the given list (pending or processing) */
Max Reitz8f622e92020-04-20 17:59:34 +02002092static void end_requests(struct list_head *head)
Miklos Szeredi334f4852005-09-09 13:10:27 -07002093{
2094 while (!list_empty(head)) {
2095 struct fuse_req *req;
2096 req = list_entry(head->next, struct fuse_req, list);
Miklos Szeredi334f4852005-09-09 13:10:27 -07002097 req->out.h.error = -ECONNABORTED;
Miklos Szeredi33e14b42015-07-01 16:26:01 +02002098 clear_bit(FR_SENT, &req->flags);
Miklos Szeredif377cb72015-07-01 16:26:04 +02002099 list_del_init(&req->list);
Max Reitz8f622e92020-04-20 17:59:34 +02002100 fuse_request_end(req);
Miklos Szeredi334f4852005-09-09 13:10:27 -07002101 }
2102}
2103
Bryan Green357ccf22011-03-01 16:43:52 -08002104static void end_polls(struct fuse_conn *fc)
2105{
2106 struct rb_node *p;
2107
2108 p = rb_first(&fc->polled_files);
2109
2110 while (p) {
2111 struct fuse_file *ff;
2112 ff = rb_entry(p, struct fuse_file, polled_node);
2113 wake_up_interruptible_all(&ff->poll_wait);
2114
2115 p = rb_next(p);
2116 }
2117}
2118
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002119/*
2120 * Abort all requests.
2121 *
Miklos Szeredib716d422015-07-01 16:25:59 +02002122 * Emergency exit in case of a malicious or accidental deadlock, or just a hung
2123 * filesystem.
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002124 *
Miklos Szeredib716d422015-07-01 16:25:59 +02002125 * The same effect is usually achievable through killing the filesystem daemon
2126 * and all users of the filesystem. The exception is the combination of an
2127 * asynchronous request and the tricky deadlock (see
Mauro Carvalho Chehab72ef5e52020-04-14 18:48:35 +02002128 * Documentation/filesystems/fuse.rst).
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002129 *
Miklos Szeredib716d422015-07-01 16:25:59 +02002130 * Aborting requests under I/O goes as follows: 1: Separate out unlocked
2131 * requests, they should be finished off immediately. Locked requests will be
2132 * finished after unlock; see unlock_request(). 2: Finish off the unlocked
2133 * requests. It is possible that some request will finish before we can. This
2134 * is OK, the request will in that case be removed from the list before we touch
2135 * it.
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002136 */
Miklos Szeredieb98e3bd2019-01-24 10:40:16 +01002137void fuse_abort_conn(struct fuse_conn *fc)
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002138{
Miklos Szeredif88996a2015-07-01 16:26:01 +02002139 struct fuse_iqueue *fiq = &fc->iq;
2140
Miklos Szeredid7133112006-04-10 22:54:55 -07002141 spin_lock(&fc->lock);
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002142 if (fc->connected) {
Miklos Szeredic36960462015-07-01 16:26:09 +02002143 struct fuse_dev *fud;
Miklos Szeredib716d422015-07-01 16:25:59 +02002144 struct fuse_req *req, *next;
Miklos Szeredi75f3ee42018-07-26 16:13:12 +02002145 LIST_HEAD(to_end);
Kirill Tkhaibe2ff422018-09-11 13:12:14 +03002146 unsigned int i;
Miklos Szeredib716d422015-07-01 16:25:59 +02002147
Kirill Tkhai63825b42018-08-27 18:29:56 +03002148 /* Background queuing checks fc->connected under bg_lock */
2149 spin_lock(&fc->bg_lock);
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002150 fc->connected = 0;
Kirill Tkhai63825b42018-08-27 18:29:56 +03002151 spin_unlock(&fc->bg_lock);
2152
Miklos Szeredi9759bd512015-01-06 10:45:35 +01002153 fuse_set_initialized(fc);
Miklos Szeredic36960462015-07-01 16:26:09 +02002154 list_for_each_entry(fud, &fc->devices, entry) {
2155 struct fuse_pqueue *fpq = &fud->pq;
2156
2157 spin_lock(&fpq->lock);
2158 fpq->connected = 0;
2159 list_for_each_entry_safe(req, next, &fpq->io, list) {
2160 req->out.h.error = -ECONNABORTED;
2161 spin_lock(&req->waitq.lock);
2162 set_bit(FR_ABORTED, &req->flags);
2163 if (!test_bit(FR_LOCKED, &req->flags)) {
2164 set_bit(FR_PRIVATE, &req->flags);
Miklos Szeredi87114372018-07-26 16:13:11 +02002165 __fuse_get_request(req);
Miklos Szeredi75f3ee42018-07-26 16:13:12 +02002166 list_move(&req->list, &to_end);
Miklos Szeredic36960462015-07-01 16:26:09 +02002167 }
2168 spin_unlock(&req->waitq.lock);
Miklos Szeredi77cd9d42015-07-01 16:26:06 +02002169 }
Kirill Tkhaibe2ff422018-09-11 13:12:14 +03002170 for (i = 0; i < FUSE_PQ_HASH_SIZE; i++)
2171 list_splice_tail_init(&fpq->processing[i],
2172 &to_end);
Miklos Szeredic36960462015-07-01 16:26:09 +02002173 spin_unlock(&fpq->lock);
Miklos Szeredib716d422015-07-01 16:25:59 +02002174 }
Kirill Tkhaiae2dffa2018-08-27 18:29:46 +03002175 spin_lock(&fc->bg_lock);
2176 fc->blocked = 0;
Miklos Szeredi41f98272015-07-01 16:25:59 +02002177 fc->max_background = UINT_MAX;
2178 flush_bg_queue(fc);
Kirill Tkhaiae2dffa2018-08-27 18:29:46 +03002179 spin_unlock(&fc->bg_lock);
Miklos Szeredi8c911892015-07-01 16:26:02 +02002180
Eric Biggers76e43c82019-09-08 20:15:18 -07002181 spin_lock(&fiq->lock);
Miklos Szeredi8c911892015-07-01 16:26:02 +02002182 fiq->connected = 0;
Miklos Szeredi75f3ee42018-07-26 16:13:12 +02002183 list_for_each_entry(req, &fiq->pending, list)
Tahsin Erdogana8a86d72017-01-12 12:04:04 -08002184 clear_bit(FR_PENDING, &req->flags);
Miklos Szeredi75f3ee42018-07-26 16:13:12 +02002185 list_splice_tail_init(&fiq->pending, &to_end);
Miklos Szeredi8c911892015-07-01 16:26:02 +02002186 while (forget_pending(fiq))
Vivek Goyal4388c5a2019-06-05 15:50:43 -04002187 kfree(fuse_dequeue_forget(fiq, 1, NULL));
Eric Biggers76e43c82019-09-08 20:15:18 -07002188 wake_up_all(&fiq->waitq);
2189 spin_unlock(&fiq->lock);
Miklos Szeredi8c911892015-07-01 16:26:02 +02002190 kill_fasync(&fiq->fasync, SIGIO, POLL_IN);
Miklos Szerediee314a82015-07-01 16:26:08 +02002191 end_polls(fc);
2192 wake_up_all(&fc->blocked_waitq);
2193 spin_unlock(&fc->lock);
Miklos Szeredi8c911892015-07-01 16:26:02 +02002194
Max Reitz8f622e92020-04-20 17:59:34 +02002195 end_requests(&to_end);
Miklos Szerediee314a82015-07-01 16:26:08 +02002196 } else {
2197 spin_unlock(&fc->lock);
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002198 }
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002199}
Tejun Heo08cbf542009-04-14 10:54:53 +09002200EXPORT_SYMBOL_GPL(fuse_abort_conn);
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002201
Miklos Szeredib8f95e52018-07-26 16:13:11 +02002202void fuse_wait_aborted(struct fuse_conn *fc)
2203{
Miklos Szeredi2d84a2d2018-11-09 15:52:16 +01002204 /* matches implicit memory barrier in fuse_drop_waiting() */
2205 smp_mb();
Miklos Szeredib8f95e52018-07-26 16:13:11 +02002206 wait_event(fc->blocked_waitq, atomic_read(&fc->num_waiting) == 0);
2207}
2208
Tejun Heo08cbf542009-04-14 10:54:53 +09002209int fuse_dev_release(struct inode *inode, struct file *file)
Miklos Szeredi334f4852005-09-09 13:10:27 -07002210{
Miklos Szeredicc080e92015-07-01 16:26:08 +02002211 struct fuse_dev *fud = fuse_get_dev(file);
2212
2213 if (fud) {
2214 struct fuse_conn *fc = fud->fc;
Miklos Szeredic36960462015-07-01 16:26:09 +02002215 struct fuse_pqueue *fpq = &fud->pq;
Miklos Szeredi45ff3502018-07-26 16:13:11 +02002216 LIST_HEAD(to_end);
Kirill Tkhaibe2ff422018-09-11 13:12:14 +03002217 unsigned int i;
Miklos Szeredicc080e92015-07-01 16:26:08 +02002218
Miklos Szeredi45ff3502018-07-26 16:13:11 +02002219 spin_lock(&fpq->lock);
Miklos Szeredic36960462015-07-01 16:26:09 +02002220 WARN_ON(!list_empty(&fpq->io));
Kirill Tkhaibe2ff422018-09-11 13:12:14 +03002221 for (i = 0; i < FUSE_PQ_HASH_SIZE; i++)
2222 list_splice_init(&fpq->processing[i], &to_end);
Miklos Szeredi45ff3502018-07-26 16:13:11 +02002223 spin_unlock(&fpq->lock);
2224
Max Reitz8f622e92020-04-20 17:59:34 +02002225 end_requests(&to_end);
Miklos Szeredi45ff3502018-07-26 16:13:11 +02002226
Miklos Szeredic36960462015-07-01 16:26:09 +02002227 /* Are we the last open device? */
2228 if (atomic_dec_and_test(&fc->dev_count)) {
2229 WARN_ON(fc->iq.fasync != NULL);
Miklos Szeredieb98e3bd2019-01-24 10:40:16 +01002230 fuse_abort_conn(fc);
Miklos Szeredic36960462015-07-01 16:26:09 +02002231 }
Miklos Szeredicc080e92015-07-01 16:26:08 +02002232 fuse_dev_free(fud);
Jeff Dike385a17b2006-04-10 22:54:52 -07002233 }
Miklos Szeredi334f4852005-09-09 13:10:27 -07002234 return 0;
2235}
Tejun Heo08cbf542009-04-14 10:54:53 +09002236EXPORT_SYMBOL_GPL(fuse_dev_release);
Miklos Szeredi334f4852005-09-09 13:10:27 -07002237
Jeff Dike385a17b2006-04-10 22:54:52 -07002238static int fuse_dev_fasync(int fd, struct file *file, int on)
2239{
Miklos Szeredicc080e92015-07-01 16:26:08 +02002240 struct fuse_dev *fud = fuse_get_dev(file);
2241
2242 if (!fud)
Miklos Szeredia87046d2006-04-10 22:54:56 -07002243 return -EPERM;
Jeff Dike385a17b2006-04-10 22:54:52 -07002244
2245 /* No locking - fasync_helper does its own locking */
Miklos Szeredicc080e92015-07-01 16:26:08 +02002246 return fasync_helper(fd, file, on, &fud->fc->iq.fasync);
Jeff Dike385a17b2006-04-10 22:54:52 -07002247}
2248
Miklos Szeredi00c570f2015-07-01 16:26:08 +02002249static int fuse_device_clone(struct fuse_conn *fc, struct file *new)
2250{
Miklos Szeredicc080e92015-07-01 16:26:08 +02002251 struct fuse_dev *fud;
2252
Miklos Szeredi00c570f2015-07-01 16:26:08 +02002253 if (new->private_data)
2254 return -EINVAL;
2255
Vivek Goyal0cd1eb9a2019-03-06 16:51:40 -05002256 fud = fuse_dev_alloc_install(fc);
Miklos Szeredicc080e92015-07-01 16:26:08 +02002257 if (!fud)
2258 return -ENOMEM;
2259
2260 new->private_data = fud;
Miklos Szeredic36960462015-07-01 16:26:09 +02002261 atomic_inc(&fc->dev_count);
Miklos Szeredi00c570f2015-07-01 16:26:08 +02002262
2263 return 0;
2264}
2265
2266static long fuse_dev_ioctl(struct file *file, unsigned int cmd,
2267 unsigned long arg)
2268{
Alessio Balsini7fe32352021-01-25 17:01:30 +00002269 int res;
2270 int oldfd;
2271 struct fuse_dev *fud = NULL;
Miklos Szeredi00c570f2015-07-01 16:26:08 +02002272
Alessio Balsini2c6791b2021-03-19 15:05:14 +00002273 switch (cmd) {
2274 case FUSE_DEV_IOC_CLONE:
Alessio Balsini7fe32352021-01-25 17:01:30 +00002275 res = -EFAULT;
2276 if (!get_user(oldfd, (__u32 __user *)arg)) {
Miklos Szeredi00c570f2015-07-01 16:26:08 +02002277 struct file *old = fget(oldfd);
2278
Alessio Balsini7fe32352021-01-25 17:01:30 +00002279 res = -EINVAL;
Miklos Szeredi00c570f2015-07-01 16:26:08 +02002280 if (old) {
Jann Horn8ed1f0e2015-08-16 20:27:01 +02002281 /*
2282 * Check against file->f_op because CUSE
2283 * uses the same ioctl handler.
2284 */
2285 if (old->f_op == file->f_op &&
Alessio Balsini7fe32352021-01-25 17:01:30 +00002286 old->f_cred->user_ns ==
2287 file->f_cred->user_ns)
Jann Horn8ed1f0e2015-08-16 20:27:01 +02002288 fud = fuse_get_dev(old);
Miklos Szeredi00c570f2015-07-01 16:26:08 +02002289
Miklos Szeredicc080e92015-07-01 16:26:08 +02002290 if (fud) {
Miklos Szeredi00c570f2015-07-01 16:26:08 +02002291 mutex_lock(&fuse_mutex);
Alessio Balsini7fe32352021-01-25 17:01:30 +00002292 res = fuse_device_clone(fud->fc, file);
Miklos Szeredi00c570f2015-07-01 16:26:08 +02002293 mutex_unlock(&fuse_mutex);
2294 }
2295 fput(old);
2296 }
2297 }
Alessio Balsini7fe32352021-01-25 17:01:30 +00002298 break;
Alessio Balsini2c6791b2021-03-19 15:05:14 +00002299 case FUSE_DEV_IOC_PASSTHROUGH_OPEN:
Alessio Balsini9634f0e2021-01-25 17:02:28 +00002300 res = -EFAULT;
Alessio Balsini5e424f82021-05-13 11:58:16 +01002301 if (!get_user(oldfd, (__u32 __user *)arg)) {
Alessio Balsini9634f0e2021-01-25 17:02:28 +00002302 res = -EINVAL;
2303 fud = fuse_get_dev(file);
2304 if (fud)
Alessio Balsini5e424f82021-05-13 11:58:16 +01002305 res = fuse_passthrough_open(fud, oldfd);
Alessio Balsini9634f0e2021-01-25 17:02:28 +00002306 }
2307 break;
Alessio Balsini7fe32352021-01-25 17:01:30 +00002308 default:
2309 res = -ENOTTY;
2310 break;
Miklos Szeredi00c570f2015-07-01 16:26:08 +02002311 }
Alessio Balsini7fe32352021-01-25 17:01:30 +00002312 return res;
Miklos Szeredi00c570f2015-07-01 16:26:08 +02002313}
2314
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -08002315const struct file_operations fuse_dev_operations = {
Miklos Szeredi334f4852005-09-09 13:10:27 -07002316 .owner = THIS_MODULE,
Tom Van Braeckel94e4fe22015-01-12 05:22:16 +01002317 .open = fuse_dev_open,
Miklos Szeredi334f4852005-09-09 13:10:27 -07002318 .llseek = no_llseek,
Al Virofbdbacc2015-04-03 21:53:39 -04002319 .read_iter = fuse_dev_read,
Miklos Szeredic3021622010-05-25 15:06:07 +02002320 .splice_read = fuse_dev_splice_read,
Al Virofbdbacc2015-04-03 21:53:39 -04002321 .write_iter = fuse_dev_write,
Miklos Szeredidd3bb142010-05-25 15:06:06 +02002322 .splice_write = fuse_dev_splice_write,
Miklos Szeredi334f4852005-09-09 13:10:27 -07002323 .poll = fuse_dev_poll,
2324 .release = fuse_dev_release,
Jeff Dike385a17b2006-04-10 22:54:52 -07002325 .fasync = fuse_dev_fasync,
Miklos Szeredi00c570f2015-07-01 16:26:08 +02002326 .unlocked_ioctl = fuse_dev_ioctl,
Arnd Bergmann1832f2d2018-09-11 21:59:08 +02002327 .compat_ioctl = compat_ptr_ioctl,
Miklos Szeredi334f4852005-09-09 13:10:27 -07002328};
Tejun Heo08cbf542009-04-14 10:54:53 +09002329EXPORT_SYMBOL_GPL(fuse_dev_operations);
Miklos Szeredi334f4852005-09-09 13:10:27 -07002330
2331static struct miscdevice fuse_miscdevice = {
2332 .minor = FUSE_MINOR,
2333 .name = "fuse",
2334 .fops = &fuse_dev_operations,
2335};
2336
2337int __init fuse_dev_init(void)
2338{
2339 int err = -ENOMEM;
2340 fuse_req_cachep = kmem_cache_create("fuse_request",
2341 sizeof(struct fuse_req),
Paul Mundt20c2df82007-07-20 10:11:58 +09002342 0, 0, NULL);
Miklos Szeredi334f4852005-09-09 13:10:27 -07002343 if (!fuse_req_cachep)
2344 goto out;
2345
2346 err = misc_register(&fuse_miscdevice);
2347 if (err)
2348 goto out_cache_clean;
2349
2350 return 0;
2351
2352 out_cache_clean:
2353 kmem_cache_destroy(fuse_req_cachep);
2354 out:
2355 return err;
2356}
2357
2358void fuse_dev_cleanup(void)
2359{
2360 misc_deregister(&fuse_miscdevice);
2361 kmem_cache_destroy(fuse_req_cachep);
2362}