Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 1 | /* |
| 2 | FUSE: Filesystem in Userspace |
Miklos Szeredi | 1729a16 | 2008-11-26 12:03:54 +0100 | [diff] [blame] | 3 | Copyright (C) 2001-2008 Miklos Szeredi <miklos@szeredi.hu> |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 4 | |
| 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 Molnar | 174cd4b | 2017-02-02 19:15:33 +0100 | [diff] [blame] | 14 | #include <linux/sched/signal.h> |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 15 | #include <linux/uio.h> |
| 16 | #include <linux/miscdevice.h> |
| 17 | #include <linux/pagemap.h> |
| 18 | #include <linux/file.h> |
| 19 | #include <linux/slab.h> |
Miklos Szeredi | dd3bb14 | 2010-05-25 15:06:06 +0200 | [diff] [blame] | 20 | #include <linux/pipe_fs_i.h> |
Miklos Szeredi | ce534fb | 2010-05-25 15:06:07 +0200 | [diff] [blame] | 21 | #include <linux/swap.h> |
| 22 | #include <linux/splice.h> |
Seth Forshee | 0b6e9ea | 2014-07-02 16:29:19 -0500 | [diff] [blame] | 23 | #include <linux/sched.h> |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 24 | |
| 25 | MODULE_ALIAS_MISCDEV(FUSE_MINOR); |
Kay Sievers | 578454f | 2010-05-20 18:07:20 +0200 | [diff] [blame] | 26 | MODULE_ALIAS("devname:fuse"); |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 27 | |
Kirill Tkhai | c59fd85 | 2018-09-11 13:11:56 +0300 | [diff] [blame] | 28 | /* Ordinary requests have even IDs, while interrupts IDs are odd */ |
| 29 | #define FUSE_INT_REQ_BIT (1ULL << 0) |
| 30 | #define FUSE_REQ_ID_STEP (1ULL << 1) |
| 31 | |
Christoph Lameter | e18b890 | 2006-12-06 20:33:20 -0800 | [diff] [blame] | 32 | static struct kmem_cache *fuse_req_cachep; |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 33 | |
Miklos Szeredi | cc080e9 | 2015-07-01 16:26:08 +0200 | [diff] [blame] | 34 | static struct fuse_dev *fuse_get_dev(struct file *file) |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 35 | { |
Miklos Szeredi | 0720b31 | 2006-04-10 22:54:55 -0700 | [diff] [blame] | 36 | /* |
| 37 | * Lockless access is OK, because file->private data is set |
| 38 | * once during mount and is valid until the file is released. |
| 39 | */ |
Mark Rutland | 6aa7de0 | 2017-10-23 14:07:29 -0700 | [diff] [blame] | 40 | return READ_ONCE(file->private_data); |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 41 | } |
| 42 | |
Miklos Szeredi | 7213394 | 2019-09-10 15:04:11 +0200 | [diff] [blame] | 43 | static void fuse_request_init(struct fuse_req *req) |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 44 | { |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 45 | INIT_LIST_HEAD(&req->list); |
Miklos Szeredi | a4d27e7 | 2006-06-25 05:48:54 -0700 | [diff] [blame] | 46 | INIT_LIST_HEAD(&req->intr_entry); |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 47 | init_waitqueue_head(&req->waitq); |
Elena Reshetova | ec99f6d | 2017-03-03 11:04:04 +0200 | [diff] [blame] | 48 | refcount_set(&req->count, 1); |
Miklos Szeredi | 33e14b4 | 2015-07-01 16:26:01 +0200 | [diff] [blame] | 49 | __set_bit(FR_PENDING, &req->flags); |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 50 | } |
| 51 | |
Miklos Szeredi | 7213394 | 2019-09-10 15:04:11 +0200 | [diff] [blame] | 52 | static struct fuse_req *fuse_request_alloc(gfp_t flags) |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 53 | { |
Miklos Szeredi | 8a7aa28 | 2018-10-01 10:07:05 +0200 | [diff] [blame] | 54 | struct fuse_req *req = kmem_cache_zalloc(fuse_req_cachep, flags); |
Miklos Szeredi | 7213394 | 2019-09-10 15:04:11 +0200 | [diff] [blame] | 55 | if (req) |
| 56 | fuse_request_init(req); |
Maxim Patlasov | 4250c06 | 2012-10-26 19:48:07 +0400 | [diff] [blame] | 57 | |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 58 | return req; |
| 59 | } |
Maxim Patlasov | 4250c06 | 2012-10-26 19:48:07 +0400 | [diff] [blame] | 60 | |
Miklos Szeredi | 66abc35 | 2019-09-10 15:04:11 +0200 | [diff] [blame] | 61 | static void fuse_request_free(struct fuse_req *req) |
Miklos Szeredi | e52a8250 | 2018-10-01 10:07:06 +0200 | [diff] [blame] | 62 | { |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 63 | kmem_cache_free(fuse_req_cachep, req); |
| 64 | } |
| 65 | |
Miklos Szeredi | 66abc35 | 2019-09-10 15:04:11 +0200 | [diff] [blame] | 66 | static void __fuse_get_request(struct fuse_req *req) |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 67 | { |
Elena Reshetova | ec99f6d | 2017-03-03 11:04:04 +0200 | [diff] [blame] | 68 | refcount_inc(&req->count); |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | /* Must be called with > 1 refcount */ |
| 72 | static void __fuse_put_request(struct fuse_req *req) |
| 73 | { |
Elena Reshetova | ec99f6d | 2017-03-03 11:04:04 +0200 | [diff] [blame] | 74 | refcount_dec(&req->count); |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 75 | } |
| 76 | |
Miklos Szeredi | 9759bd51 | 2015-01-06 10:45:35 +0100 | [diff] [blame] | 77 | void fuse_set_initialized(struct fuse_conn *fc) |
| 78 | { |
| 79 | /* Make sure stores before this are seen on another CPU */ |
| 80 | smp_wmb(); |
| 81 | fc->initialized = 1; |
| 82 | } |
| 83 | |
Maxim Patlasov | 0aada88 | 2013-03-21 18:02:28 +0400 | [diff] [blame] | 84 | static bool fuse_block_alloc(struct fuse_conn *fc, bool for_background) |
| 85 | { |
| 86 | return !fc->initialized || (for_background && fc->blocked); |
| 87 | } |
| 88 | |
Miklos Szeredi | b8f95e5 | 2018-07-26 16:13:11 +0200 | [diff] [blame] | 89 | static void fuse_drop_waiting(struct fuse_conn *fc) |
| 90 | { |
Miklos Szeredi | 2d84a2d | 2018-11-09 15:52:16 +0100 | [diff] [blame] | 91 | /* |
| 92 | * lockess check of fc->connected is okay, because atomic_dec_and_test() |
| 93 | * provides a memory barrier mached with the one in fuse_wait_aborted() |
| 94 | * to ensure no wake-up is missed. |
| 95 | */ |
| 96 | if (atomic_dec_and_test(&fc->num_waiting) && |
| 97 | !READ_ONCE(fc->connected)) { |
Miklos Szeredi | b8f95e5 | 2018-07-26 16:13:11 +0200 | [diff] [blame] | 98 | /* wake up aborters */ |
| 99 | wake_up_all(&fc->blocked_waitq); |
| 100 | } |
| 101 | } |
| 102 | |
Miklos Szeredi | 66abc35 | 2019-09-10 15:04:11 +0200 | [diff] [blame] | 103 | static void fuse_put_request(struct fuse_conn *fc, struct fuse_req *req); |
| 104 | |
Miklos Szeredi | 7213394 | 2019-09-10 15:04:11 +0200 | [diff] [blame] | 105 | static struct fuse_req *fuse_get_req(struct fuse_conn *fc, bool for_background) |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 106 | { |
Miklos Szeredi | 08a53cd | 2006-04-10 22:54:59 -0700 | [diff] [blame] | 107 | struct fuse_req *req; |
Miklos Szeredi | 08a53cd | 2006-04-10 22:54:59 -0700 | [diff] [blame] | 108 | int err; |
Miklos Szeredi | 9bc5ddd | 2006-04-11 21:16:09 +0200 | [diff] [blame] | 109 | atomic_inc(&fc->num_waiting); |
Maxim Patlasov | 0aada88 | 2013-03-21 18:02:28 +0400 | [diff] [blame] | 110 | |
| 111 | if (fuse_block_alloc(fc, for_background)) { |
Maxim Patlasov | 0aada88 | 2013-03-21 18:02:28 +0400 | [diff] [blame] | 112 | err = -EINTR; |
Al Viro | 7d3a07f | 2016-07-19 03:08:27 -0400 | [diff] [blame] | 113 | if (wait_event_killable_exclusive(fc->blocked_waitq, |
| 114 | !fuse_block_alloc(fc, for_background))) |
Maxim Patlasov | 0aada88 | 2013-03-21 18:02:28 +0400 | [diff] [blame] | 115 | goto out; |
| 116 | } |
Miklos Szeredi | 9759bd51 | 2015-01-06 10:45:35 +0100 | [diff] [blame] | 117 | /* Matches smp_wmb() in fuse_set_initialized() */ |
| 118 | smp_rmb(); |
Miklos Szeredi | 08a53cd | 2006-04-10 22:54:59 -0700 | [diff] [blame] | 119 | |
Miklos Szeredi | 51eb01e | 2006-06-25 05:48:50 -0700 | [diff] [blame] | 120 | err = -ENOTCONN; |
| 121 | if (!fc->connected) |
| 122 | goto out; |
| 123 | |
Miklos Szeredi | de15522 | 2015-07-01 16:25:57 +0200 | [diff] [blame] | 124 | err = -ECONNREFUSED; |
| 125 | if (fc->conn_error) |
| 126 | goto out; |
| 127 | |
Miklos Szeredi | 7213394 | 2019-09-10 15:04:11 +0200 | [diff] [blame] | 128 | req = fuse_request_alloc(GFP_KERNEL); |
Miklos Szeredi | 9bc5ddd | 2006-04-11 21:16:09 +0200 | [diff] [blame] | 129 | err = -ENOMEM; |
Maxim Patlasov | 722d2be | 2013-03-21 18:02:36 +0400 | [diff] [blame] | 130 | if (!req) { |
| 131 | if (for_background) |
| 132 | wake_up(&fc->blocked_waitq); |
Miklos Szeredi | 9bc5ddd | 2006-04-11 21:16:09 +0200 | [diff] [blame] | 133 | goto out; |
Maxim Patlasov | 722d2be | 2013-03-21 18:02:36 +0400 | [diff] [blame] | 134 | } |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 135 | |
Eric W. Biederman | 8cb0832 | 2018-02-21 11:18:07 -0600 | [diff] [blame] | 136 | req->in.h.uid = from_kuid(fc->user_ns, current_fsuid()); |
| 137 | req->in.h.gid = from_kgid(fc->user_ns, current_fsgid()); |
Eric W. Biederman | c9582eb | 2018-02-21 10:52:06 -0600 | [diff] [blame] | 138 | req->in.h.pid = pid_nr_ns(task_pid(current), fc->pid_ns); |
| 139 | |
Miklos Szeredi | 825d6d3 | 2015-07-01 16:25:58 +0200 | [diff] [blame] | 140 | __set_bit(FR_WAITING, &req->flags); |
| 141 | if (for_background) |
| 142 | __set_bit(FR_BACKGROUND, &req->flags); |
| 143 | |
Eric W. Biederman | c9582eb | 2018-02-21 10:52:06 -0600 | [diff] [blame] | 144 | if (unlikely(req->in.h.uid == ((uid_t)-1) || |
| 145 | req->in.h.gid == ((gid_t)-1))) { |
| 146 | fuse_put_request(fc, req); |
| 147 | return ERR_PTR(-EOVERFLOW); |
| 148 | } |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 149 | return req; |
Miklos Szeredi | 9bc5ddd | 2006-04-11 21:16:09 +0200 | [diff] [blame] | 150 | |
| 151 | out: |
Miklos Szeredi | b8f95e5 | 2018-07-26 16:13:11 +0200 | [diff] [blame] | 152 | fuse_drop_waiting(fc); |
Miklos Szeredi | 9bc5ddd | 2006-04-11 21:16:09 +0200 | [diff] [blame] | 153 | return ERR_PTR(err); |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 154 | } |
Maxim Patlasov | 8b41e67 | 2013-03-21 18:02:04 +0400 | [diff] [blame] | 155 | |
Miklos Szeredi | 66abc35 | 2019-09-10 15:04:11 +0200 | [diff] [blame] | 156 | static void fuse_put_request(struct fuse_conn *fc, struct fuse_req *req) |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 157 | { |
Elena Reshetova | ec99f6d | 2017-03-03 11:04:04 +0200 | [diff] [blame] | 158 | if (refcount_dec_and_test(&req->count)) { |
Miklos Szeredi | 825d6d3 | 2015-07-01 16:25:58 +0200 | [diff] [blame] | 159 | if (test_bit(FR_BACKGROUND, &req->flags)) { |
Maxim Patlasov | 722d2be | 2013-03-21 18:02:36 +0400 | [diff] [blame] | 160 | /* |
| 161 | * We get here in the unlikely case that a background |
| 162 | * request was allocated but not sent |
| 163 | */ |
Kirill Tkhai | ae2dffa | 2018-08-27 18:29:46 +0300 | [diff] [blame] | 164 | spin_lock(&fc->bg_lock); |
Maxim Patlasov | 722d2be | 2013-03-21 18:02:36 +0400 | [diff] [blame] | 165 | if (!fc->blocked) |
| 166 | wake_up(&fc->blocked_waitq); |
Kirill Tkhai | ae2dffa | 2018-08-27 18:29:46 +0300 | [diff] [blame] | 167 | spin_unlock(&fc->bg_lock); |
Maxim Patlasov | 722d2be | 2013-03-21 18:02:36 +0400 | [diff] [blame] | 168 | } |
| 169 | |
Miklos Szeredi | 825d6d3 | 2015-07-01 16:25:58 +0200 | [diff] [blame] | 170 | if (test_bit(FR_WAITING, &req->flags)) { |
| 171 | __clear_bit(FR_WAITING, &req->flags); |
Miklos Szeredi | b8f95e5 | 2018-07-26 16:13:11 +0200 | [diff] [blame] | 172 | fuse_drop_waiting(fc); |
Miklos Szeredi | 73e0e73 | 2015-07-01 16:25:56 +0200 | [diff] [blame] | 173 | } |
Miklos Szeredi | 33649c9 | 2006-06-25 05:48:52 -0700 | [diff] [blame] | 174 | |
Miklos Szeredi | 40ac7ab | 2019-09-10 15:04:08 +0200 | [diff] [blame] | 175 | fuse_request_free(req); |
Miklos Szeredi | 7128ec2 | 2006-02-04 23:27:40 -0800 | [diff] [blame] | 176 | } |
| 177 | } |
| 178 | |
Stefan Hajnoczi | 14d46d7 | 2018-06-21 09:34:25 +0100 | [diff] [blame] | 179 | unsigned int fuse_len_args(unsigned int numargs, struct fuse_arg *args) |
Miklos Szeredi | d12def1 | 2008-02-06 01:38:39 -0800 | [diff] [blame] | 180 | { |
| 181 | unsigned nbytes = 0; |
| 182 | unsigned i; |
| 183 | |
| 184 | for (i = 0; i < numargs; i++) |
| 185 | nbytes += args[i].size; |
| 186 | |
| 187 | return nbytes; |
| 188 | } |
Stefan Hajnoczi | 14d46d7 | 2018-06-21 09:34:25 +0100 | [diff] [blame] | 189 | EXPORT_SYMBOL_GPL(fuse_len_args); |
Miklos Szeredi | d12def1 | 2008-02-06 01:38:39 -0800 | [diff] [blame] | 190 | |
Stefan Hajnoczi | 79d96ef | 2018-06-22 13:48:30 +0100 | [diff] [blame] | 191 | u64 fuse_get_unique(struct fuse_iqueue *fiq) |
Miklos Szeredi | d12def1 | 2008-02-06 01:38:39 -0800 | [diff] [blame] | 192 | { |
Kirill Tkhai | c59fd85 | 2018-09-11 13:11:56 +0300 | [diff] [blame] | 193 | fiq->reqctr += FUSE_REQ_ID_STEP; |
| 194 | return fiq->reqctr; |
Miklos Szeredi | d12def1 | 2008-02-06 01:38:39 -0800 | [diff] [blame] | 195 | } |
Stefan Hajnoczi | 79d96ef | 2018-06-22 13:48:30 +0100 | [diff] [blame] | 196 | EXPORT_SYMBOL_GPL(fuse_get_unique); |
Miklos Szeredi | d12def1 | 2008-02-06 01:38:39 -0800 | [diff] [blame] | 197 | |
Kirill Tkhai | be2ff42 | 2018-09-11 13:12:14 +0300 | [diff] [blame] | 198 | static unsigned int fuse_req_hash(u64 unique) |
| 199 | { |
| 200 | return hash_long(unique & ~FUSE_INT_REQ_BIT, FUSE_PQ_HASH_BITS); |
| 201 | } |
| 202 | |
Stefan Hajnoczi | ae3aad7 | 2018-06-18 15:53:19 +0100 | [diff] [blame] | 203 | /** |
| 204 | * A new request is available, wake fiq->waitq |
| 205 | */ |
| 206 | static void fuse_dev_wake_and_unlock(struct fuse_iqueue *fiq) |
| 207 | __releases(fiq->lock) |
| 208 | { |
| 209 | wake_up(&fiq->waitq); |
| 210 | kill_fasync(&fiq->fasync, SIGIO, POLL_IN); |
| 211 | spin_unlock(&fiq->lock); |
| 212 | } |
| 213 | |
| 214 | const struct fuse_iqueue_ops fuse_dev_fiq_ops = { |
| 215 | .wake_forget_and_unlock = fuse_dev_wake_and_unlock, |
| 216 | .wake_interrupt_and_unlock = fuse_dev_wake_and_unlock, |
| 217 | .wake_pending_and_unlock = fuse_dev_wake_and_unlock, |
| 218 | }; |
| 219 | EXPORT_SYMBOL_GPL(fuse_dev_fiq_ops); |
| 220 | |
| 221 | static void queue_request_and_unlock(struct fuse_iqueue *fiq, |
| 222 | struct fuse_req *req) |
| 223 | __releases(fiq->lock) |
Miklos Szeredi | d12def1 | 2008-02-06 01:38:39 -0800 | [diff] [blame] | 224 | { |
Miklos Szeredi | d12def1 | 2008-02-06 01:38:39 -0800 | [diff] [blame] | 225 | req->in.h.len = sizeof(struct fuse_in_header) + |
Stefan Hajnoczi | 14d46d7 | 2018-06-21 09:34:25 +0100 | [diff] [blame] | 226 | fuse_len_args(req->args->in_numargs, |
| 227 | (struct fuse_arg *) req->args->in_args); |
Miklos Szeredi | f88996a | 2015-07-01 16:26:01 +0200 | [diff] [blame] | 228 | list_add_tail(&req->list, &fiq->pending); |
Stefan Hajnoczi | ae3aad7 | 2018-06-18 15:53:19 +0100 | [diff] [blame] | 229 | fiq->ops->wake_pending_and_unlock(fiq); |
Miklos Szeredi | d12def1 | 2008-02-06 01:38:39 -0800 | [diff] [blame] | 230 | } |
| 231 | |
Miklos Szeredi | 07e77dc | 2010-12-07 20:16:56 +0100 | [diff] [blame] | 232 | void fuse_queue_forget(struct fuse_conn *fc, struct fuse_forget_link *forget, |
| 233 | u64 nodeid, u64 nlookup) |
| 234 | { |
Miklos Szeredi | f88996a | 2015-07-01 16:26:01 +0200 | [diff] [blame] | 235 | struct fuse_iqueue *fiq = &fc->iq; |
| 236 | |
Miklos Szeredi | 02c048b | 2010-12-07 20:16:56 +0100 | [diff] [blame] | 237 | forget->forget_one.nodeid = nodeid; |
| 238 | forget->forget_one.nlookup = nlookup; |
Miklos Szeredi | 07e77dc | 2010-12-07 20:16:56 +0100 | [diff] [blame] | 239 | |
Eric Biggers | 76e43c8 | 2019-09-08 20:15:18 -0700 | [diff] [blame] | 240 | spin_lock(&fiq->lock); |
Miklos Szeredi | e16714d | 2015-07-01 16:26:01 +0200 | [diff] [blame] | 241 | if (fiq->connected) { |
Miklos Szeredi | f88996a | 2015-07-01 16:26:01 +0200 | [diff] [blame] | 242 | fiq->forget_list_tail->next = forget; |
| 243 | fiq->forget_list_tail = forget; |
Stefan Hajnoczi | ae3aad7 | 2018-06-18 15:53:19 +0100 | [diff] [blame] | 244 | fiq->ops->wake_forget_and_unlock(fiq); |
Miklos Szeredi | 5dfcc87 | 2011-09-12 09:38:03 +0200 | [diff] [blame] | 245 | } else { |
| 246 | kfree(forget); |
Stefan Hajnoczi | ae3aad7 | 2018-06-18 15:53:19 +0100 | [diff] [blame] | 247 | spin_unlock(&fiq->lock); |
Miklos Szeredi | 5dfcc87 | 2011-09-12 09:38:03 +0200 | [diff] [blame] | 248 | } |
Miklos Szeredi | 07e77dc | 2010-12-07 20:16:56 +0100 | [diff] [blame] | 249 | } |
| 250 | |
Miklos Szeredi | d12def1 | 2008-02-06 01:38:39 -0800 | [diff] [blame] | 251 | static void flush_bg_queue(struct fuse_conn *fc) |
| 252 | { |
Kirill Tkhai | e287179 | 2018-07-31 13:25:25 +0300 | [diff] [blame] | 253 | struct fuse_iqueue *fiq = &fc->iq; |
| 254 | |
Csaba Henk | 7a6d3c8 | 2009-07-01 17:28:41 -0700 | [diff] [blame] | 255 | while (fc->active_background < fc->max_background && |
Miklos Szeredi | d12def1 | 2008-02-06 01:38:39 -0800 | [diff] [blame] | 256 | !list_empty(&fc->bg_queue)) { |
| 257 | struct fuse_req *req; |
| 258 | |
Kirill Tkhai | e287179 | 2018-07-31 13:25:25 +0300 | [diff] [blame] | 259 | req = list_first_entry(&fc->bg_queue, struct fuse_req, list); |
Miklos Szeredi | d12def1 | 2008-02-06 01:38:39 -0800 | [diff] [blame] | 260 | list_del(&req->list); |
| 261 | fc->active_background++; |
Eric Biggers | 76e43c8 | 2019-09-08 20:15:18 -0700 | [diff] [blame] | 262 | spin_lock(&fiq->lock); |
Miklos Szeredi | f88996a | 2015-07-01 16:26:01 +0200 | [diff] [blame] | 263 | req->in.h.unique = fuse_get_unique(fiq); |
Stefan Hajnoczi | ae3aad7 | 2018-06-18 15:53:19 +0100 | [diff] [blame] | 264 | queue_request_and_unlock(fiq, req); |
Miklos Szeredi | d12def1 | 2008-02-06 01:38:39 -0800 | [diff] [blame] | 265 | } |
| 266 | } |
| 267 | |
Miklos Szeredi | 6dbbcb1 | 2006-04-26 10:49:06 +0200 | [diff] [blame] | 268 | /* |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 269 | * This function is called when a request is finished. Either a reply |
Miklos Szeredi | f9a2842 | 2006-06-25 05:48:53 -0700 | [diff] [blame] | 270 | * has arrived or it was aborted (and not yet sent) or some error |
Miklos Szeredi | f43b155 | 2006-01-16 22:14:26 -0800 | [diff] [blame] | 271 | * occurred during communication with userspace, or the device file |
Miklos Szeredi | 51eb01e | 2006-06-25 05:48:50 -0700 | [diff] [blame] | 272 | * was closed. The requester thread is woken up (if still waiting), |
| 273 | * the 'end' callback is called if given, else the reference to the |
| 274 | * request is released |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 275 | */ |
Stefan Hajnoczi | 04ec5af | 2018-06-21 09:33:40 +0100 | [diff] [blame] | 276 | void fuse_request_end(struct fuse_conn *fc, struct fuse_req *req) |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 277 | { |
Miklos Szeredi | 4ce6081 | 2015-07-01 16:26:02 +0200 | [diff] [blame] | 278 | struct fuse_iqueue *fiq = &fc->iq; |
Miklos Szeredi | d499377 | 2019-09-10 15:04:11 +0200 | [diff] [blame] | 279 | bool async = req->args->end; |
Miklos Szeredi | 365ae71 | 2015-07-01 16:26:06 +0200 | [diff] [blame] | 280 | |
Miklos Szeredi | efe2800 | 2015-07-01 16:26:07 +0200 | [diff] [blame] | 281 | if (test_and_set_bit(FR_FINISHED, &req->flags)) |
Miklos Szeredi | b8f95e5 | 2018-07-26 16:13:11 +0200 | [diff] [blame] | 282 | goto put_request; |
Kirill Tkhai | 217316a | 2018-11-08 12:05:25 +0300 | [diff] [blame] | 283 | /* |
| 284 | * test_and_set_bit() implies smp_mb() between bit |
| 285 | * changing and below intr_entry check. Pairs with |
| 286 | * smp_mb() from queue_interrupt(). |
| 287 | */ |
| 288 | if (!list_empty(&req->intr_entry)) { |
Eric Biggers | 76e43c8 | 2019-09-08 20:15:18 -0700 | [diff] [blame] | 289 | spin_lock(&fiq->lock); |
Kirill Tkhai | 217316a | 2018-11-08 12:05:25 +0300 | [diff] [blame] | 290 | list_del_init(&req->intr_entry); |
Eric Biggers | 76e43c8 | 2019-09-08 20:15:18 -0700 | [diff] [blame] | 291 | spin_unlock(&fiq->lock); |
Kirill Tkhai | 217316a | 2018-11-08 12:05:25 +0300 | [diff] [blame] | 292 | } |
Miklos Szeredi | 33e14b4 | 2015-07-01 16:26:01 +0200 | [diff] [blame] | 293 | WARN_ON(test_bit(FR_PENDING, &req->flags)); |
| 294 | WARN_ON(test_bit(FR_SENT, &req->flags)); |
Miklos Szeredi | 825d6d3 | 2015-07-01 16:25:58 +0200 | [diff] [blame] | 295 | if (test_bit(FR_BACKGROUND, &req->flags)) { |
Kirill Tkhai | ae2dffa | 2018-08-27 18:29:46 +0300 | [diff] [blame] | 296 | spin_lock(&fc->bg_lock); |
Miklos Szeredi | 825d6d3 | 2015-07-01 16:25:58 +0200 | [diff] [blame] | 297 | clear_bit(FR_BACKGROUND, &req->flags); |
Miklos Szeredi | 908a572 | 2018-09-28 16:43:22 +0200 | [diff] [blame] | 298 | if (fc->num_background == fc->max_background) { |
Miklos Szeredi | 51eb01e | 2006-06-25 05:48:50 -0700 | [diff] [blame] | 299 | fc->blocked = 0; |
Maxim Patlasov | 722d2be | 2013-03-21 18:02:36 +0400 | [diff] [blame] | 300 | wake_up(&fc->blocked_waitq); |
Miklos Szeredi | 908a572 | 2018-09-28 16:43:22 +0200 | [diff] [blame] | 301 | } else if (!fc->blocked) { |
| 302 | /* |
| 303 | * Wake up next waiter, if any. It's okay to use |
| 304 | * waitqueue_active(), as we've already synced up |
| 305 | * fc->blocked with waiters with the wake_up() call |
| 306 | * above. |
| 307 | */ |
| 308 | if (waitqueue_active(&fc->blocked_waitq)) |
| 309 | wake_up(&fc->blocked_waitq); |
| 310 | } |
Maxim Patlasov | 722d2be | 2013-03-21 18:02:36 +0400 | [diff] [blame] | 311 | |
Tejun Heo | 8a301eb | 2018-02-02 09:54:14 -0800 | [diff] [blame] | 312 | if (fc->num_background == fc->congestion_threshold && fc->sb) { |
Jan Kara | 5f7f754 | 2017-04-12 12:24:40 +0200 | [diff] [blame] | 313 | clear_bdi_congested(fc->sb->s_bdi, BLK_RW_SYNC); |
| 314 | clear_bdi_congested(fc->sb->s_bdi, BLK_RW_ASYNC); |
Miklos Szeredi | f92b99b | 2007-10-16 23:30:59 -0700 | [diff] [blame] | 315 | } |
Miklos Szeredi | 51eb01e | 2006-06-25 05:48:50 -0700 | [diff] [blame] | 316 | fc->num_background--; |
Miklos Szeredi | d12def1 | 2008-02-06 01:38:39 -0800 | [diff] [blame] | 317 | fc->active_background--; |
| 318 | flush_bg_queue(fc); |
Kirill Tkhai | ae2dffa | 2018-08-27 18:29:46 +0300 | [diff] [blame] | 319 | spin_unlock(&fc->bg_lock); |
Kirill Tkhai | 5e0fed7 | 2018-11-08 12:05:31 +0300 | [diff] [blame] | 320 | } else { |
| 321 | /* Wake up waiter sleeping in request_wait_answer() */ |
| 322 | wake_up(&req->waitq); |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 323 | } |
Kirill Tkhai | 5e0fed7 | 2018-11-08 12:05:31 +0300 | [diff] [blame] | 324 | |
Miklos Szeredi | d499377 | 2019-09-10 15:04:11 +0200 | [diff] [blame] | 325 | if (async) |
| 326 | req->args->end(fc, req->args, req->out.h.error); |
Miklos Szeredi | b8f95e5 | 2018-07-26 16:13:11 +0200 | [diff] [blame] | 327 | put_request: |
Tejun Heo | e9bb09d | 2008-11-26 12:03:54 +0100 | [diff] [blame] | 328 | fuse_put_request(fc, req); |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 329 | } |
Stefan Hajnoczi | 04ec5af | 2018-06-21 09:33:40 +0100 | [diff] [blame] | 330 | EXPORT_SYMBOL_GPL(fuse_request_end); |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 331 | |
Kirill Tkhai | b782911 | 2018-11-08 12:05:42 +0300 | [diff] [blame] | 332 | static int queue_interrupt(struct fuse_iqueue *fiq, struct fuse_req *req) |
Miklos Szeredi | a4d27e7 | 2006-06-25 05:48:54 -0700 | [diff] [blame] | 333 | { |
Eric Biggers | 76e43c8 | 2019-09-08 20:15:18 -0700 | [diff] [blame] | 334 | spin_lock(&fiq->lock); |
Kirill Tkhai | b782911 | 2018-11-08 12:05:42 +0300 | [diff] [blame] | 335 | /* Check for we've sent request to interrupt this req */ |
| 336 | if (unlikely(!test_bit(FR_INTERRUPTED, &req->flags))) { |
Eric Biggers | 76e43c8 | 2019-09-08 20:15:18 -0700 | [diff] [blame] | 337 | spin_unlock(&fiq->lock); |
Kirill Tkhai | b782911 | 2018-11-08 12:05:42 +0300 | [diff] [blame] | 338 | return -EINVAL; |
| 339 | } |
| 340 | |
Miklos Szeredi | 8f7bb36 | 2015-07-01 16:26:03 +0200 | [diff] [blame] | 341 | if (list_empty(&req->intr_entry)) { |
| 342 | list_add_tail(&req->intr_entry, &fiq->interrupts); |
Kirill Tkhai | 217316a | 2018-11-08 12:05:25 +0300 | [diff] [blame] | 343 | /* |
| 344 | * Pairs with smp_mb() implied by test_and_set_bit() |
| 345 | * from request_end(). |
| 346 | */ |
| 347 | smp_mb(); |
| 348 | if (test_bit(FR_FINISHED, &req->flags)) { |
| 349 | list_del_init(&req->intr_entry); |
Eric Biggers | 76e43c8 | 2019-09-08 20:15:18 -0700 | [diff] [blame] | 350 | spin_unlock(&fiq->lock); |
Kirill Tkhai | b782911 | 2018-11-08 12:05:42 +0300 | [diff] [blame] | 351 | return 0; |
Kirill Tkhai | 217316a | 2018-11-08 12:05:25 +0300 | [diff] [blame] | 352 | } |
Stefan Hajnoczi | ae3aad7 | 2018-06-18 15:53:19 +0100 | [diff] [blame] | 353 | fiq->ops->wake_interrupt_and_unlock(fiq); |
| 354 | } else { |
| 355 | spin_unlock(&fiq->lock); |
Miklos Szeredi | 8f7bb36 | 2015-07-01 16:26:03 +0200 | [diff] [blame] | 356 | } |
Kirill Tkhai | b782911 | 2018-11-08 12:05:42 +0300 | [diff] [blame] | 357 | return 0; |
Miklos Szeredi | a4d27e7 | 2006-06-25 05:48:54 -0700 | [diff] [blame] | 358 | } |
| 359 | |
Miklos Szeredi | 7c352bd | 2005-09-09 13:10:39 -0700 | [diff] [blame] | 360 | static void request_wait_answer(struct fuse_conn *fc, struct fuse_req *req) |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 361 | { |
Miklos Szeredi | 4ce6081 | 2015-07-01 16:26:02 +0200 | [diff] [blame] | 362 | struct fuse_iqueue *fiq = &fc->iq; |
Miklos Szeredi | c477526 | 2015-07-01 16:26:00 +0200 | [diff] [blame] | 363 | int err; |
| 364 | |
Miklos Szeredi | a4d27e7 | 2006-06-25 05:48:54 -0700 | [diff] [blame] | 365 | if (!fc->no_interrupt) { |
| 366 | /* Any signal may interrupt this */ |
Miklos Szeredi | c477526 | 2015-07-01 16:26:00 +0200 | [diff] [blame] | 367 | err = wait_event_interruptible(req->waitq, |
Miklos Szeredi | 33e14b4 | 2015-07-01 16:26:01 +0200 | [diff] [blame] | 368 | test_bit(FR_FINISHED, &req->flags)); |
Miklos Szeredi | c477526 | 2015-07-01 16:26:00 +0200 | [diff] [blame] | 369 | if (!err) |
Miklos Szeredi | a4d27e7 | 2006-06-25 05:48:54 -0700 | [diff] [blame] | 370 | return; |
| 371 | |
Miklos Szeredi | 825d6d3 | 2015-07-01 16:25:58 +0200 | [diff] [blame] | 372 | set_bit(FR_INTERRUPTED, &req->flags); |
Miklos Szeredi | 8f7bb36 | 2015-07-01 16:26:03 +0200 | [diff] [blame] | 373 | /* matches barrier in fuse_dev_do_read() */ |
| 374 | smp_mb__after_atomic(); |
Miklos Szeredi | 33e14b4 | 2015-07-01 16:26:01 +0200 | [diff] [blame] | 375 | if (test_bit(FR_SENT, &req->flags)) |
Miklos Szeredi | 4ce6081 | 2015-07-01 16:26:02 +0200 | [diff] [blame] | 376 | queue_interrupt(fiq, req); |
Miklos Szeredi | a4d27e7 | 2006-06-25 05:48:54 -0700 | [diff] [blame] | 377 | } |
| 378 | |
Miklos Szeredi | 825d6d3 | 2015-07-01 16:25:58 +0200 | [diff] [blame] | 379 | if (!test_bit(FR_FORCE, &req->flags)) { |
Miklos Szeredi | a4d27e7 | 2006-06-25 05:48:54 -0700 | [diff] [blame] | 380 | /* Only fatal signals may interrupt this */ |
Al Viro | 7d3a07f | 2016-07-19 03:08:27 -0400 | [diff] [blame] | 381 | err = wait_event_killable(req->waitq, |
Miklos Szeredi | 33e14b4 | 2015-07-01 16:26:01 +0200 | [diff] [blame] | 382 | test_bit(FR_FINISHED, &req->flags)); |
Miklos Szeredi | c477526 | 2015-07-01 16:26:00 +0200 | [diff] [blame] | 383 | if (!err) |
Miklos Szeredi | a131de0 | 2007-10-16 23:31:04 -0700 | [diff] [blame] | 384 | return; |
| 385 | |
Eric Biggers | 76e43c8 | 2019-09-08 20:15:18 -0700 | [diff] [blame] | 386 | spin_lock(&fiq->lock); |
Miklos Szeredi | a131de0 | 2007-10-16 23:31:04 -0700 | [diff] [blame] | 387 | /* Request is not yet in userspace, bail out */ |
Miklos Szeredi | 33e14b4 | 2015-07-01 16:26:01 +0200 | [diff] [blame] | 388 | if (test_bit(FR_PENDING, &req->flags)) { |
Miklos Szeredi | a131de0 | 2007-10-16 23:31:04 -0700 | [diff] [blame] | 389 | list_del(&req->list); |
Eric Biggers | 76e43c8 | 2019-09-08 20:15:18 -0700 | [diff] [blame] | 390 | spin_unlock(&fiq->lock); |
Miklos Szeredi | a131de0 | 2007-10-16 23:31:04 -0700 | [diff] [blame] | 391 | __fuse_put_request(req); |
| 392 | req->out.h.error = -EINTR; |
| 393 | return; |
| 394 | } |
Eric Biggers | 76e43c8 | 2019-09-08 20:15:18 -0700 | [diff] [blame] | 395 | spin_unlock(&fiq->lock); |
Miklos Szeredi | 51eb01e | 2006-06-25 05:48:50 -0700 | [diff] [blame] | 396 | } |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 397 | |
Miklos Szeredi | a131de0 | 2007-10-16 23:31:04 -0700 | [diff] [blame] | 398 | /* |
| 399 | * Either request is already in userspace, or it was forced. |
| 400 | * Wait it out. |
| 401 | */ |
Miklos Szeredi | 33e14b4 | 2015-07-01 16:26:01 +0200 | [diff] [blame] | 402 | wait_event(req->waitq, test_bit(FR_FINISHED, &req->flags)); |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 403 | } |
| 404 | |
Eric Wong | 6a4e922 | 2013-02-04 13:04:44 +0000 | [diff] [blame] | 405 | static void __fuse_request_send(struct fuse_conn *fc, struct fuse_req *req) |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 406 | { |
Miklos Szeredi | e16714d | 2015-07-01 16:26:01 +0200 | [diff] [blame] | 407 | struct fuse_iqueue *fiq = &fc->iq; |
| 408 | |
Miklos Szeredi | 825d6d3 | 2015-07-01 16:25:58 +0200 | [diff] [blame] | 409 | BUG_ON(test_bit(FR_BACKGROUND, &req->flags)); |
Eric Biggers | 76e43c8 | 2019-09-08 20:15:18 -0700 | [diff] [blame] | 410 | spin_lock(&fiq->lock); |
Miklos Szeredi | e16714d | 2015-07-01 16:26:01 +0200 | [diff] [blame] | 411 | if (!fiq->connected) { |
Eric Biggers | 76e43c8 | 2019-09-08 20:15:18 -0700 | [diff] [blame] | 412 | spin_unlock(&fiq->lock); |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 413 | req->out.h.error = -ENOTCONN; |
Miklos Szeredi | c477526 | 2015-07-01 16:26:00 +0200 | [diff] [blame] | 414 | } else { |
Miklos Szeredi | f88996a | 2015-07-01 16:26:01 +0200 | [diff] [blame] | 415 | req->in.h.unique = fuse_get_unique(fiq); |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 416 | /* acquire extra reference, since request is still needed |
Stefan Hajnoczi | 04ec5af | 2018-06-21 09:33:40 +0100 | [diff] [blame] | 417 | after fuse_request_end() */ |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 418 | __fuse_get_request(req); |
Stefan Hajnoczi | ae3aad7 | 2018-06-18 15:53:19 +0100 | [diff] [blame] | 419 | queue_request_and_unlock(fiq, req); |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 420 | |
Miklos Szeredi | 7c352bd | 2005-09-09 13:10:39 -0700 | [diff] [blame] | 421 | request_wait_answer(fc, req); |
Stefan Hajnoczi | 04ec5af | 2018-06-21 09:33:40 +0100 | [diff] [blame] | 422 | /* Pairs with smp_wmb() in fuse_request_end() */ |
Miklos Szeredi | c477526 | 2015-07-01 16:26:00 +0200 | [diff] [blame] | 423 | smp_rmb(); |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 424 | } |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 425 | } |
Eric Wong | 6a4e922 | 2013-02-04 13:04:44 +0000 | [diff] [blame] | 426 | |
Miklos Szeredi | 21f6217 | 2015-01-06 10:45:35 +0100 | [diff] [blame] | 427 | static void fuse_adjust_compat(struct fuse_conn *fc, struct fuse_args *args) |
| 428 | { |
Miklos Szeredi | d5b4854 | 2019-09-10 15:04:08 +0200 | [diff] [blame] | 429 | if (fc->minor < 4 && args->opcode == FUSE_STATFS) |
| 430 | args->out_args[0].size = FUSE_COMPAT_STATFS_SIZE; |
Miklos Szeredi | 21f6217 | 2015-01-06 10:45:35 +0100 | [diff] [blame] | 431 | |
| 432 | if (fc->minor < 9) { |
Miklos Szeredi | d5b4854 | 2019-09-10 15:04:08 +0200 | [diff] [blame] | 433 | switch (args->opcode) { |
Miklos Szeredi | 21f6217 | 2015-01-06 10:45:35 +0100 | [diff] [blame] | 434 | case FUSE_LOOKUP: |
| 435 | case FUSE_CREATE: |
| 436 | case FUSE_MKNOD: |
| 437 | case FUSE_MKDIR: |
| 438 | case FUSE_SYMLINK: |
| 439 | case FUSE_LINK: |
Miklos Szeredi | d5b4854 | 2019-09-10 15:04:08 +0200 | [diff] [blame] | 440 | args->out_args[0].size = FUSE_COMPAT_ENTRY_OUT_SIZE; |
Miklos Szeredi | 21f6217 | 2015-01-06 10:45:35 +0100 | [diff] [blame] | 441 | break; |
| 442 | case FUSE_GETATTR: |
| 443 | case FUSE_SETATTR: |
Miklos Szeredi | d5b4854 | 2019-09-10 15:04:08 +0200 | [diff] [blame] | 444 | args->out_args[0].size = FUSE_COMPAT_ATTR_OUT_SIZE; |
Miklos Szeredi | 21f6217 | 2015-01-06 10:45:35 +0100 | [diff] [blame] | 445 | break; |
| 446 | } |
| 447 | } |
| 448 | if (fc->minor < 12) { |
Miklos Szeredi | d5b4854 | 2019-09-10 15:04:08 +0200 | [diff] [blame] | 449 | switch (args->opcode) { |
Miklos Szeredi | 21f6217 | 2015-01-06 10:45:35 +0100 | [diff] [blame] | 450 | case FUSE_CREATE: |
Miklos Szeredi | d5b4854 | 2019-09-10 15:04:08 +0200 | [diff] [blame] | 451 | args->in_args[0].size = sizeof(struct fuse_open_in); |
Miklos Szeredi | 21f6217 | 2015-01-06 10:45:35 +0100 | [diff] [blame] | 452 | break; |
| 453 | case FUSE_MKNOD: |
Miklos Szeredi | d5b4854 | 2019-09-10 15:04:08 +0200 | [diff] [blame] | 454 | args->in_args[0].size = FUSE_COMPAT_MKNOD_IN_SIZE; |
Miklos Szeredi | 21f6217 | 2015-01-06 10:45:35 +0100 | [diff] [blame] | 455 | break; |
| 456 | } |
| 457 | } |
| 458 | } |
| 459 | |
Miklos Szeredi | e413754 | 2019-09-10 15:04:08 +0200 | [diff] [blame] | 460 | static void fuse_force_creds(struct fuse_conn *fc, struct fuse_req *req) |
| 461 | { |
| 462 | req->in.h.uid = from_kuid_munged(fc->user_ns, current_fsuid()); |
| 463 | req->in.h.gid = from_kgid_munged(fc->user_ns, current_fsgid()); |
| 464 | req->in.h.pid = pid_nr_ns(task_pid(current), fc->pid_ns); |
| 465 | } |
| 466 | |
YueHaibing | 5addcd5 | 2019-09-23 13:52:31 +0800 | [diff] [blame] | 467 | static void fuse_args_to_req(struct fuse_req *req, struct fuse_args *args) |
Miklos Szeredi | 6858316 | 2019-09-10 15:04:09 +0200 | [diff] [blame] | 468 | { |
Miklos Szeredi | 6858316 | 2019-09-10 15:04:09 +0200 | [diff] [blame] | 469 | req->in.h.opcode = args->opcode; |
| 470 | req->in.h.nodeid = args->nodeid; |
Miklos Szeredi | d499377 | 2019-09-10 15:04:11 +0200 | [diff] [blame] | 471 | req->args = args; |
Miklos Szeredi | 6858316 | 2019-09-10 15:04:09 +0200 | [diff] [blame] | 472 | } |
| 473 | |
Miklos Szeredi | 7078187 | 2014-12-12 09:49:05 +0100 | [diff] [blame] | 474 | ssize_t fuse_simple_request(struct fuse_conn *fc, struct fuse_args *args) |
| 475 | { |
| 476 | struct fuse_req *req; |
| 477 | ssize_t ret; |
| 478 | |
Miklos Szeredi | c500eba | 2019-09-10 15:04:08 +0200 | [diff] [blame] | 479 | if (args->force) { |
Miklos Szeredi | e413754 | 2019-09-10 15:04:08 +0200 | [diff] [blame] | 480 | atomic_inc(&fc->num_waiting); |
Miklos Szeredi | 7213394 | 2019-09-10 15:04:11 +0200 | [diff] [blame] | 481 | req = fuse_request_alloc(GFP_KERNEL | __GFP_NOFAIL); |
Miklos Szeredi | e413754 | 2019-09-10 15:04:08 +0200 | [diff] [blame] | 482 | |
| 483 | if (!args->nocreds) |
| 484 | fuse_force_creds(fc, req); |
| 485 | |
| 486 | __set_bit(FR_WAITING, &req->flags); |
Miklos Szeredi | c500eba | 2019-09-10 15:04:08 +0200 | [diff] [blame] | 487 | __set_bit(FR_FORCE, &req->flags); |
| 488 | } else { |
Miklos Szeredi | e413754 | 2019-09-10 15:04:08 +0200 | [diff] [blame] | 489 | WARN_ON(args->nocreds); |
Miklos Szeredi | 7213394 | 2019-09-10 15:04:11 +0200 | [diff] [blame] | 490 | req = fuse_get_req(fc, false); |
Miklos Szeredi | c500eba | 2019-09-10 15:04:08 +0200 | [diff] [blame] | 491 | if (IS_ERR(req)) |
| 492 | return PTR_ERR(req); |
| 493 | } |
Miklos Szeredi | 7078187 | 2014-12-12 09:49:05 +0100 | [diff] [blame] | 494 | |
Miklos Szeredi | 21f6217 | 2015-01-06 10:45:35 +0100 | [diff] [blame] | 495 | /* Needs to be done after fuse_get_req() so that fc->minor is valid */ |
| 496 | fuse_adjust_compat(fc, args); |
Miklos Szeredi | 6858316 | 2019-09-10 15:04:09 +0200 | [diff] [blame] | 497 | fuse_args_to_req(req, args); |
Miklos Szeredi | 21f6217 | 2015-01-06 10:45:35 +0100 | [diff] [blame] | 498 | |
Miklos Szeredi | 454a761 | 2019-09-10 15:04:08 +0200 | [diff] [blame] | 499 | if (!args->noreply) |
| 500 | __set_bit(FR_ISREPLY, &req->flags); |
| 501 | __fuse_request_send(fc, req); |
Miklos Szeredi | 7078187 | 2014-12-12 09:49:05 +0100 | [diff] [blame] | 502 | ret = req->out.h.error; |
Miklos Szeredi | d5b4854 | 2019-09-10 15:04:08 +0200 | [diff] [blame] | 503 | if (!ret && args->out_argvar) { |
Miklos Szeredi | 093f38a | 2019-09-10 15:04:09 +0200 | [diff] [blame] | 504 | BUG_ON(args->out_numargs == 0); |
Miklos Szeredi | d499377 | 2019-09-10 15:04:11 +0200 | [diff] [blame] | 505 | ret = args->out_args[args->out_numargs - 1].size; |
Miklos Szeredi | 7078187 | 2014-12-12 09:49:05 +0100 | [diff] [blame] | 506 | } |
| 507 | fuse_put_request(fc, req); |
| 508 | |
| 509 | return ret; |
| 510 | } |
| 511 | |
Miklos Szeredi | 66abc35 | 2019-09-10 15:04:11 +0200 | [diff] [blame] | 512 | static bool fuse_request_queue_background(struct fuse_conn *fc, |
| 513 | struct fuse_req *req) |
Miklos Szeredi | d12def1 | 2008-02-06 01:38:39 -0800 | [diff] [blame] | 514 | { |
Kirill Tkhai | 63825b4 | 2018-08-27 18:29:56 +0300 | [diff] [blame] | 515 | bool queued = false; |
| 516 | |
| 517 | WARN_ON(!test_bit(FR_BACKGROUND, &req->flags)); |
Miklos Szeredi | 825d6d3 | 2015-07-01 16:25:58 +0200 | [diff] [blame] | 518 | if (!test_bit(FR_WAITING, &req->flags)) { |
| 519 | __set_bit(FR_WAITING, &req->flags); |
Miklos Szeredi | 5437f24 | 2015-07-01 16:25:56 +0200 | [diff] [blame] | 520 | atomic_inc(&fc->num_waiting); |
| 521 | } |
Miklos Szeredi | 825d6d3 | 2015-07-01 16:25:58 +0200 | [diff] [blame] | 522 | __set_bit(FR_ISREPLY, &req->flags); |
Kirill Tkhai | ae2dffa | 2018-08-27 18:29:46 +0300 | [diff] [blame] | 523 | spin_lock(&fc->bg_lock); |
Kirill Tkhai | 63825b4 | 2018-08-27 18:29:56 +0300 | [diff] [blame] | 524 | if (likely(fc->connected)) { |
| 525 | fc->num_background++; |
| 526 | if (fc->num_background == fc->max_background) |
| 527 | fc->blocked = 1; |
| 528 | if (fc->num_background == fc->congestion_threshold && fc->sb) { |
| 529 | set_bdi_congested(fc->sb->s_bdi, BLK_RW_SYNC); |
| 530 | set_bdi_congested(fc->sb->s_bdi, BLK_RW_ASYNC); |
| 531 | } |
| 532 | list_add_tail(&req->list, &fc->bg_queue); |
| 533 | flush_bg_queue(fc); |
| 534 | queued = true; |
Miklos Szeredi | d12def1 | 2008-02-06 01:38:39 -0800 | [diff] [blame] | 535 | } |
Kirill Tkhai | ae2dffa | 2018-08-27 18:29:46 +0300 | [diff] [blame] | 536 | spin_unlock(&fc->bg_lock); |
Kirill Tkhai | 63825b4 | 2018-08-27 18:29:56 +0300 | [diff] [blame] | 537 | |
| 538 | return queued; |
Miklos Szeredi | d12def1 | 2008-02-06 01:38:39 -0800 | [diff] [blame] | 539 | } |
| 540 | |
Miklos Szeredi | 1259728 | 2019-09-10 15:04:10 +0200 | [diff] [blame] | 541 | int fuse_simple_background(struct fuse_conn *fc, struct fuse_args *args, |
| 542 | gfp_t gfp_flags) |
| 543 | { |
| 544 | struct fuse_req *req; |
| 545 | |
| 546 | if (args->force) { |
| 547 | WARN_ON(!args->nocreds); |
Miklos Szeredi | 7213394 | 2019-09-10 15:04:11 +0200 | [diff] [blame] | 548 | req = fuse_request_alloc(gfp_flags); |
Miklos Szeredi | 1259728 | 2019-09-10 15:04:10 +0200 | [diff] [blame] | 549 | if (!req) |
| 550 | return -ENOMEM; |
| 551 | __set_bit(FR_BACKGROUND, &req->flags); |
| 552 | } else { |
| 553 | WARN_ON(args->nocreds); |
Miklos Szeredi | 7213394 | 2019-09-10 15:04:11 +0200 | [diff] [blame] | 554 | req = fuse_get_req(fc, true); |
Miklos Szeredi | 1259728 | 2019-09-10 15:04:10 +0200 | [diff] [blame] | 555 | if (IS_ERR(req)) |
| 556 | return PTR_ERR(req); |
| 557 | } |
| 558 | |
| 559 | fuse_args_to_req(req, args); |
| 560 | |
Miklos Szeredi | 1259728 | 2019-09-10 15:04:10 +0200 | [diff] [blame] | 561 | if (!fuse_request_queue_background(fc, req)) { |
| 562 | fuse_put_request(fc, req); |
| 563 | return -ENOTCONN; |
| 564 | } |
| 565 | |
| 566 | return 0; |
| 567 | } |
| 568 | EXPORT_SYMBOL_GPL(fuse_simple_background); |
| 569 | |
Miklos Szeredi | 75b399d | 2019-09-10 15:04:11 +0200 | [diff] [blame] | 570 | static int fuse_simple_notify_reply(struct fuse_conn *fc, |
| 571 | struct fuse_args *args, u64 unique) |
Miklos Szeredi | 2d45ba3 | 2010-07-12 14:41:40 +0200 | [diff] [blame] | 572 | { |
Miklos Szeredi | 75b399d | 2019-09-10 15:04:11 +0200 | [diff] [blame] | 573 | struct fuse_req *req; |
Miklos Szeredi | f88996a | 2015-07-01 16:26:01 +0200 | [diff] [blame] | 574 | struct fuse_iqueue *fiq = &fc->iq; |
Miklos Szeredi | 75b399d | 2019-09-10 15:04:11 +0200 | [diff] [blame] | 575 | int err = 0; |
| 576 | |
Miklos Szeredi | 7213394 | 2019-09-10 15:04:11 +0200 | [diff] [blame] | 577 | req = fuse_get_req(fc, false); |
Miklos Szeredi | 75b399d | 2019-09-10 15:04:11 +0200 | [diff] [blame] | 578 | if (IS_ERR(req)) |
| 579 | return PTR_ERR(req); |
Miklos Szeredi | 2d45ba3 | 2010-07-12 14:41:40 +0200 | [diff] [blame] | 580 | |
Miklos Szeredi | 825d6d3 | 2015-07-01 16:25:58 +0200 | [diff] [blame] | 581 | __clear_bit(FR_ISREPLY, &req->flags); |
Miklos Szeredi | 2d45ba3 | 2010-07-12 14:41:40 +0200 | [diff] [blame] | 582 | req->in.h.unique = unique; |
Miklos Szeredi | 75b399d | 2019-09-10 15:04:11 +0200 | [diff] [blame] | 583 | |
| 584 | fuse_args_to_req(req, args); |
Miklos Szeredi | 75b399d | 2019-09-10 15:04:11 +0200 | [diff] [blame] | 585 | |
Eric Biggers | 76e43c8 | 2019-09-08 20:15:18 -0700 | [diff] [blame] | 586 | spin_lock(&fiq->lock); |
Miklos Szeredi | e16714d | 2015-07-01 16:26:01 +0200 | [diff] [blame] | 587 | if (fiq->connected) { |
Stefan Hajnoczi | ae3aad7 | 2018-06-18 15:53:19 +0100 | [diff] [blame] | 588 | queue_request_and_unlock(fiq, req); |
Miklos Szeredi | 75b399d | 2019-09-10 15:04:11 +0200 | [diff] [blame] | 589 | } else { |
| 590 | err = -ENODEV; |
| 591 | spin_unlock(&fiq->lock); |
| 592 | fuse_put_request(fc, req); |
Miklos Szeredi | 2d45ba3 | 2010-07-12 14:41:40 +0200 | [diff] [blame] | 593 | } |
Miklos Szeredi | 2d45ba3 | 2010-07-12 14:41:40 +0200 | [diff] [blame] | 594 | |
| 595 | return err; |
| 596 | } |
| 597 | |
Miklos Szeredi | 3be5a52 | 2008-04-30 00:54:41 -0700 | [diff] [blame] | 598 | /* |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 599 | * Lock the request. Up to the next unlock_request() there mustn't be |
| 600 | * anything that could cause a page-fault. If the request was already |
Miklos Szeredi | f9a2842 | 2006-06-25 05:48:53 -0700 | [diff] [blame] | 601 | * aborted bail out. |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 602 | */ |
Miklos Szeredi | dc00809 | 2015-07-01 16:25:58 +0200 | [diff] [blame] | 603 | static int lock_request(struct fuse_req *req) |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 604 | { |
| 605 | int err = 0; |
| 606 | if (req) { |
Miklos Szeredi | dc00809 | 2015-07-01 16:25:58 +0200 | [diff] [blame] | 607 | spin_lock(&req->waitq.lock); |
Miklos Szeredi | 825d6d3 | 2015-07-01 16:25:58 +0200 | [diff] [blame] | 608 | if (test_bit(FR_ABORTED, &req->flags)) |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 609 | err = -ENOENT; |
| 610 | else |
Miklos Szeredi | 825d6d3 | 2015-07-01 16:25:58 +0200 | [diff] [blame] | 611 | set_bit(FR_LOCKED, &req->flags); |
Miklos Szeredi | dc00809 | 2015-07-01 16:25:58 +0200 | [diff] [blame] | 612 | spin_unlock(&req->waitq.lock); |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 613 | } |
| 614 | return err; |
| 615 | } |
| 616 | |
| 617 | /* |
Miklos Szeredi | 0d8e84b | 2015-07-01 16:25:58 +0200 | [diff] [blame] | 618 | * Unlock request. If it was aborted while locked, caller is responsible |
| 619 | * for unlocking and ending the request. |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 620 | */ |
Miklos Szeredi | dc00809 | 2015-07-01 16:25:58 +0200 | [diff] [blame] | 621 | static int unlock_request(struct fuse_req *req) |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 622 | { |
Miklos Szeredi | 0d8e84b | 2015-07-01 16:25:58 +0200 | [diff] [blame] | 623 | int err = 0; |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 624 | if (req) { |
Miklos Szeredi | dc00809 | 2015-07-01 16:25:58 +0200 | [diff] [blame] | 625 | spin_lock(&req->waitq.lock); |
Miklos Szeredi | 825d6d3 | 2015-07-01 16:25:58 +0200 | [diff] [blame] | 626 | if (test_bit(FR_ABORTED, &req->flags)) |
Miklos Szeredi | 0d8e84b | 2015-07-01 16:25:58 +0200 | [diff] [blame] | 627 | err = -ENOENT; |
| 628 | else |
Miklos Szeredi | 825d6d3 | 2015-07-01 16:25:58 +0200 | [diff] [blame] | 629 | clear_bit(FR_LOCKED, &req->flags); |
Miklos Szeredi | dc00809 | 2015-07-01 16:25:58 +0200 | [diff] [blame] | 630 | spin_unlock(&req->waitq.lock); |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 631 | } |
Miklos Szeredi | 0d8e84b | 2015-07-01 16:25:58 +0200 | [diff] [blame] | 632 | return err; |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 633 | } |
| 634 | |
| 635 | struct fuse_copy_state { |
| 636 | int write; |
| 637 | struct fuse_req *req; |
Al Viro | 6c09e94 | 2015-04-03 22:06:08 -0400 | [diff] [blame] | 638 | struct iov_iter *iter; |
Miklos Szeredi | dd3bb14 | 2010-05-25 15:06:06 +0200 | [diff] [blame] | 639 | struct pipe_buffer *pipebufs; |
| 640 | struct pipe_buffer *currbuf; |
| 641 | struct pipe_inode_info *pipe; |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 642 | unsigned long nr_segs; |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 643 | struct page *pg; |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 644 | unsigned len; |
Miklos Szeredi | c55a01d | 2014-07-07 15:28:51 +0200 | [diff] [blame] | 645 | unsigned offset; |
Miklos Szeredi | ce534fb | 2010-05-25 15:06:07 +0200 | [diff] [blame] | 646 | unsigned move_pages:1; |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 647 | }; |
| 648 | |
Miklos Szeredi | dc00809 | 2015-07-01 16:25:58 +0200 | [diff] [blame] | 649 | static void fuse_copy_init(struct fuse_copy_state *cs, int write, |
Al Viro | 6c09e94 | 2015-04-03 22:06:08 -0400 | [diff] [blame] | 650 | struct iov_iter *iter) |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 651 | { |
| 652 | memset(cs, 0, sizeof(*cs)); |
| 653 | cs->write = write; |
Al Viro | 6c09e94 | 2015-04-03 22:06:08 -0400 | [diff] [blame] | 654 | cs->iter = iter; |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 655 | } |
| 656 | |
| 657 | /* Unmap and put previous page of userspace buffer */ |
Miklos Szeredi | 8bfc016 | 2006-01-16 22:14:28 -0800 | [diff] [blame] | 658 | static void fuse_copy_finish(struct fuse_copy_state *cs) |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 659 | { |
Miklos Szeredi | dd3bb14 | 2010-05-25 15:06:06 +0200 | [diff] [blame] | 660 | if (cs->currbuf) { |
| 661 | struct pipe_buffer *buf = cs->currbuf; |
| 662 | |
Miklos Szeredi | c55a01d | 2014-07-07 15:28:51 +0200 | [diff] [blame] | 663 | if (cs->write) |
Miklos Szeredi | c302162 | 2010-05-25 15:06:07 +0200 | [diff] [blame] | 664 | buf->len = PAGE_SIZE - cs->len; |
Miklos Szeredi | dd3bb14 | 2010-05-25 15:06:06 +0200 | [diff] [blame] | 665 | cs->currbuf = NULL; |
Miklos Szeredi | c55a01d | 2014-07-07 15:28:51 +0200 | [diff] [blame] | 666 | } else if (cs->pg) { |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 667 | if (cs->write) { |
| 668 | flush_dcache_page(cs->pg); |
| 669 | set_page_dirty_lock(cs->pg); |
| 670 | } |
| 671 | put_page(cs->pg); |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 672 | } |
Miklos Szeredi | c55a01d | 2014-07-07 15:28:51 +0200 | [diff] [blame] | 673 | cs->pg = NULL; |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 674 | } |
| 675 | |
| 676 | /* |
| 677 | * Get another pagefull of userspace buffer, and map it to kernel |
| 678 | * address space, and lock request |
| 679 | */ |
| 680 | static int fuse_copy_fill(struct fuse_copy_state *cs) |
| 681 | { |
Miklos Szeredi | c55a01d | 2014-07-07 15:28:51 +0200 | [diff] [blame] | 682 | struct page *page; |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 683 | int err; |
| 684 | |
Miklos Szeredi | dc00809 | 2015-07-01 16:25:58 +0200 | [diff] [blame] | 685 | err = unlock_request(cs->req); |
Miklos Szeredi | 0d8e84b | 2015-07-01 16:25:58 +0200 | [diff] [blame] | 686 | if (err) |
| 687 | return err; |
| 688 | |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 689 | fuse_copy_finish(cs); |
Miklos Szeredi | dd3bb14 | 2010-05-25 15:06:06 +0200 | [diff] [blame] | 690 | if (cs->pipebufs) { |
| 691 | struct pipe_buffer *buf = cs->pipebufs; |
| 692 | |
Miklos Szeredi | c302162 | 2010-05-25 15:06:07 +0200 | [diff] [blame] | 693 | if (!cs->write) { |
Miklos Szeredi | fba597d | 2016-09-27 10:45:12 +0200 | [diff] [blame] | 694 | err = pipe_buf_confirm(cs->pipe, buf); |
Miklos Szeredi | c302162 | 2010-05-25 15:06:07 +0200 | [diff] [blame] | 695 | if (err) |
| 696 | return err; |
Miklos Szeredi | dd3bb14 | 2010-05-25 15:06:06 +0200 | [diff] [blame] | 697 | |
Miklos Szeredi | c302162 | 2010-05-25 15:06:07 +0200 | [diff] [blame] | 698 | BUG_ON(!cs->nr_segs); |
| 699 | cs->currbuf = buf; |
Miklos Szeredi | c55a01d | 2014-07-07 15:28:51 +0200 | [diff] [blame] | 700 | cs->pg = buf->page; |
| 701 | cs->offset = buf->offset; |
Miklos Szeredi | c302162 | 2010-05-25 15:06:07 +0200 | [diff] [blame] | 702 | cs->len = buf->len; |
Miklos Szeredi | c302162 | 2010-05-25 15:06:07 +0200 | [diff] [blame] | 703 | cs->pipebufs++; |
| 704 | cs->nr_segs--; |
| 705 | } else { |
David Howells | 6718b6f | 2019-10-16 16:47:32 +0100 | [diff] [blame^] | 706 | if (cs->nr_segs >= cs->pipe->max_usage) |
Miklos Szeredi | c302162 | 2010-05-25 15:06:07 +0200 | [diff] [blame] | 707 | return -EIO; |
| 708 | |
| 709 | page = alloc_page(GFP_HIGHUSER); |
| 710 | if (!page) |
| 711 | return -ENOMEM; |
| 712 | |
| 713 | buf->page = page; |
| 714 | buf->offset = 0; |
| 715 | buf->len = 0; |
| 716 | |
| 717 | cs->currbuf = buf; |
Miklos Szeredi | c55a01d | 2014-07-07 15:28:51 +0200 | [diff] [blame] | 718 | cs->pg = page; |
| 719 | cs->offset = 0; |
Miklos Szeredi | c302162 | 2010-05-25 15:06:07 +0200 | [diff] [blame] | 720 | cs->len = PAGE_SIZE; |
| 721 | cs->pipebufs++; |
| 722 | cs->nr_segs++; |
| 723 | } |
Miklos Szeredi | dd3bb14 | 2010-05-25 15:06:06 +0200 | [diff] [blame] | 724 | } else { |
Al Viro | 6c09e94 | 2015-04-03 22:06:08 -0400 | [diff] [blame] | 725 | size_t off; |
| 726 | err = iov_iter_get_pages(cs->iter, &page, PAGE_SIZE, 1, &off); |
Miklos Szeredi | dd3bb14 | 2010-05-25 15:06:06 +0200 | [diff] [blame] | 727 | if (err < 0) |
| 728 | return err; |
Al Viro | 6c09e94 | 2015-04-03 22:06:08 -0400 | [diff] [blame] | 729 | BUG_ON(!err); |
| 730 | cs->len = err; |
| 731 | cs->offset = off; |
Miklos Szeredi | c55a01d | 2014-07-07 15:28:51 +0200 | [diff] [blame] | 732 | cs->pg = page; |
Al Viro | 6c09e94 | 2015-04-03 22:06:08 -0400 | [diff] [blame] | 733 | iov_iter_advance(cs->iter, err); |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 734 | } |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 735 | |
Miklos Szeredi | dc00809 | 2015-07-01 16:25:58 +0200 | [diff] [blame] | 736 | return lock_request(cs->req); |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 737 | } |
| 738 | |
| 739 | /* Do as much copy to/from userspace buffer as we can */ |
Miklos Szeredi | 8bfc016 | 2006-01-16 22:14:28 -0800 | [diff] [blame] | 740 | static int fuse_copy_do(struct fuse_copy_state *cs, void **val, unsigned *size) |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 741 | { |
| 742 | unsigned ncpy = min(*size, cs->len); |
| 743 | if (val) { |
Miklos Szeredi | c55a01d | 2014-07-07 15:28:51 +0200 | [diff] [blame] | 744 | void *pgaddr = kmap_atomic(cs->pg); |
| 745 | void *buf = pgaddr + cs->offset; |
| 746 | |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 747 | if (cs->write) |
Miklos Szeredi | c55a01d | 2014-07-07 15:28:51 +0200 | [diff] [blame] | 748 | memcpy(buf, *val, ncpy); |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 749 | else |
Miklos Szeredi | c55a01d | 2014-07-07 15:28:51 +0200 | [diff] [blame] | 750 | memcpy(*val, buf, ncpy); |
| 751 | |
| 752 | kunmap_atomic(pgaddr); |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 753 | *val += ncpy; |
| 754 | } |
| 755 | *size -= ncpy; |
| 756 | cs->len -= ncpy; |
Miklos Szeredi | c55a01d | 2014-07-07 15:28:51 +0200 | [diff] [blame] | 757 | cs->offset += ncpy; |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 758 | return ncpy; |
| 759 | } |
| 760 | |
Miklos Szeredi | ce534fb | 2010-05-25 15:06:07 +0200 | [diff] [blame] | 761 | static int fuse_check_page(struct page *page) |
| 762 | { |
| 763 | if (page_mapcount(page) || |
| 764 | page->mapping != NULL || |
| 765 | page_count(page) != 1 || |
| 766 | (page->flags & PAGE_FLAGS_CHECK_AT_PREP & |
| 767 | ~(1 << PG_locked | |
| 768 | 1 << PG_referenced | |
| 769 | 1 << PG_uptodate | |
| 770 | 1 << PG_lru | |
| 771 | 1 << PG_active | |
| 772 | 1 << PG_reclaim))) { |
Kirill Smelkov | f229448 | 2019-03-27 09:15:17 +0000 | [diff] [blame] | 773 | pr_warn("trying to steal weird page\n"); |
| 774 | pr_warn(" page=%p index=%li flags=%08lx, count=%i, mapcount=%i, mapping=%p\n", page, page->index, page->flags, page_count(page), page_mapcount(page), page->mapping); |
Miklos Szeredi | ce534fb | 2010-05-25 15:06:07 +0200 | [diff] [blame] | 775 | return 1; |
| 776 | } |
| 777 | return 0; |
| 778 | } |
| 779 | |
| 780 | static int fuse_try_move_page(struct fuse_copy_state *cs, struct page **pagep) |
| 781 | { |
| 782 | int err; |
| 783 | struct page *oldpage = *pagep; |
| 784 | struct page *newpage; |
| 785 | struct pipe_buffer *buf = cs->pipebufs; |
Miklos Szeredi | ce534fb | 2010-05-25 15:06:07 +0200 | [diff] [blame] | 786 | |
Miklos Szeredi | dc00809 | 2015-07-01 16:25:58 +0200 | [diff] [blame] | 787 | err = unlock_request(cs->req); |
Miklos Szeredi | 0d8e84b | 2015-07-01 16:25:58 +0200 | [diff] [blame] | 788 | if (err) |
| 789 | return err; |
| 790 | |
Miklos Szeredi | ce534fb | 2010-05-25 15:06:07 +0200 | [diff] [blame] | 791 | fuse_copy_finish(cs); |
| 792 | |
Miklos Szeredi | fba597d | 2016-09-27 10:45:12 +0200 | [diff] [blame] | 793 | err = pipe_buf_confirm(cs->pipe, buf); |
Miklos Szeredi | ce534fb | 2010-05-25 15:06:07 +0200 | [diff] [blame] | 794 | if (err) |
| 795 | return err; |
| 796 | |
| 797 | BUG_ON(!cs->nr_segs); |
| 798 | cs->currbuf = buf; |
| 799 | cs->len = buf->len; |
| 800 | cs->pipebufs++; |
| 801 | cs->nr_segs--; |
| 802 | |
| 803 | if (cs->len != PAGE_SIZE) |
| 804 | goto out_fallback; |
| 805 | |
Miklos Szeredi | ca76f5b | 2016-09-27 10:45:12 +0200 | [diff] [blame] | 806 | if (pipe_buf_steal(cs->pipe, buf) != 0) |
Miklos Szeredi | ce534fb | 2010-05-25 15:06:07 +0200 | [diff] [blame] | 807 | goto out_fallback; |
| 808 | |
| 809 | newpage = buf->page; |
| 810 | |
Miklos Szeredi | aa991b3 | 2015-02-26 11:45:47 +0100 | [diff] [blame] | 811 | if (!PageUptodate(newpage)) |
| 812 | SetPageUptodate(newpage); |
Miklos Szeredi | ce534fb | 2010-05-25 15:06:07 +0200 | [diff] [blame] | 813 | |
| 814 | ClearPageMappedToDisk(newpage); |
| 815 | |
| 816 | if (fuse_check_page(newpage) != 0) |
| 817 | goto out_fallback_unlock; |
| 818 | |
Miklos Szeredi | ce534fb | 2010-05-25 15:06:07 +0200 | [diff] [blame] | 819 | /* |
| 820 | * This is a new and locked page, it shouldn't be mapped or |
| 821 | * have any special flags on it |
| 822 | */ |
| 823 | if (WARN_ON(page_mapped(oldpage))) |
| 824 | goto out_fallback_unlock; |
| 825 | if (WARN_ON(page_has_private(oldpage))) |
| 826 | goto out_fallback_unlock; |
| 827 | if (WARN_ON(PageDirty(oldpage) || PageWriteback(oldpage))) |
| 828 | goto out_fallback_unlock; |
| 829 | if (WARN_ON(PageMlocked(oldpage))) |
| 830 | goto out_fallback_unlock; |
| 831 | |
Miklos Szeredi | ef6a3c6 | 2011-03-22 16:30:52 -0700 | [diff] [blame] | 832 | err = replace_page_cache_page(oldpage, newpage, GFP_KERNEL); |
Miklos Szeredi | ce534fb | 2010-05-25 15:06:07 +0200 | [diff] [blame] | 833 | if (err) { |
Miklos Szeredi | ef6a3c6 | 2011-03-22 16:30:52 -0700 | [diff] [blame] | 834 | unlock_page(newpage); |
| 835 | return err; |
Miklos Szeredi | ce534fb | 2010-05-25 15:06:07 +0200 | [diff] [blame] | 836 | } |
Miklos Szeredi | ef6a3c6 | 2011-03-22 16:30:52 -0700 | [diff] [blame] | 837 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 838 | get_page(newpage); |
Miklos Szeredi | ce534fb | 2010-05-25 15:06:07 +0200 | [diff] [blame] | 839 | |
| 840 | if (!(buf->flags & PIPE_BUF_FLAG_LRU)) |
| 841 | lru_cache_add_file(newpage); |
| 842 | |
| 843 | err = 0; |
Miklos Szeredi | dc00809 | 2015-07-01 16:25:58 +0200 | [diff] [blame] | 844 | spin_lock(&cs->req->waitq.lock); |
Miklos Szeredi | 825d6d3 | 2015-07-01 16:25:58 +0200 | [diff] [blame] | 845 | if (test_bit(FR_ABORTED, &cs->req->flags)) |
Miklos Szeredi | ce534fb | 2010-05-25 15:06:07 +0200 | [diff] [blame] | 846 | err = -ENOENT; |
| 847 | else |
| 848 | *pagep = newpage; |
Miklos Szeredi | dc00809 | 2015-07-01 16:25:58 +0200 | [diff] [blame] | 849 | spin_unlock(&cs->req->waitq.lock); |
Miklos Szeredi | ce534fb | 2010-05-25 15:06:07 +0200 | [diff] [blame] | 850 | |
| 851 | if (err) { |
| 852 | unlock_page(newpage); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 853 | put_page(newpage); |
Miklos Szeredi | ce534fb | 2010-05-25 15:06:07 +0200 | [diff] [blame] | 854 | return err; |
| 855 | } |
| 856 | |
| 857 | unlock_page(oldpage); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 858 | put_page(oldpage); |
Miklos Szeredi | ce534fb | 2010-05-25 15:06:07 +0200 | [diff] [blame] | 859 | cs->len = 0; |
| 860 | |
| 861 | return 0; |
| 862 | |
| 863 | out_fallback_unlock: |
| 864 | unlock_page(newpage); |
| 865 | out_fallback: |
Miklos Szeredi | c55a01d | 2014-07-07 15:28:51 +0200 | [diff] [blame] | 866 | cs->pg = buf->page; |
| 867 | cs->offset = buf->offset; |
Miklos Szeredi | ce534fb | 2010-05-25 15:06:07 +0200 | [diff] [blame] | 868 | |
Miklos Szeredi | dc00809 | 2015-07-01 16:25:58 +0200 | [diff] [blame] | 869 | err = lock_request(cs->req); |
Miklos Szeredi | ce534fb | 2010-05-25 15:06:07 +0200 | [diff] [blame] | 870 | if (err) |
| 871 | return err; |
| 872 | |
| 873 | return 1; |
| 874 | } |
| 875 | |
Miklos Szeredi | c302162 | 2010-05-25 15:06:07 +0200 | [diff] [blame] | 876 | static int fuse_ref_page(struct fuse_copy_state *cs, struct page *page, |
| 877 | unsigned offset, unsigned count) |
| 878 | { |
| 879 | struct pipe_buffer *buf; |
Miklos Szeredi | 0d8e84b | 2015-07-01 16:25:58 +0200 | [diff] [blame] | 880 | int err; |
Miklos Szeredi | c302162 | 2010-05-25 15:06:07 +0200 | [diff] [blame] | 881 | |
David Howells | 6718b6f | 2019-10-16 16:47:32 +0100 | [diff] [blame^] | 882 | if (cs->nr_segs >= cs->pipe->max_usage) |
Miklos Szeredi | c302162 | 2010-05-25 15:06:07 +0200 | [diff] [blame] | 883 | return -EIO; |
| 884 | |
Miklos Szeredi | dc00809 | 2015-07-01 16:25:58 +0200 | [diff] [blame] | 885 | err = unlock_request(cs->req); |
Miklos Szeredi | 0d8e84b | 2015-07-01 16:25:58 +0200 | [diff] [blame] | 886 | if (err) |
| 887 | return err; |
| 888 | |
Miklos Szeredi | c302162 | 2010-05-25 15:06:07 +0200 | [diff] [blame] | 889 | fuse_copy_finish(cs); |
| 890 | |
| 891 | buf = cs->pipebufs; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 892 | get_page(page); |
Miklos Szeredi | c302162 | 2010-05-25 15:06:07 +0200 | [diff] [blame] | 893 | buf->page = page; |
| 894 | buf->offset = offset; |
| 895 | buf->len = count; |
| 896 | |
| 897 | cs->pipebufs++; |
| 898 | cs->nr_segs++; |
| 899 | cs->len = 0; |
| 900 | |
| 901 | return 0; |
| 902 | } |
| 903 | |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 904 | /* |
| 905 | * Copy a page in the request to/from the userspace buffer. Must be |
| 906 | * done atomically |
| 907 | */ |
Miklos Szeredi | ce534fb | 2010-05-25 15:06:07 +0200 | [diff] [blame] | 908 | static int fuse_copy_page(struct fuse_copy_state *cs, struct page **pagep, |
Miklos Szeredi | 8bfc016 | 2006-01-16 22:14:28 -0800 | [diff] [blame] | 909 | unsigned offset, unsigned count, int zeroing) |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 910 | { |
Miklos Szeredi | ce534fb | 2010-05-25 15:06:07 +0200 | [diff] [blame] | 911 | int err; |
| 912 | struct page *page = *pagep; |
| 913 | |
Miklos Szeredi | b6777c4 | 2010-10-26 14:22:27 -0700 | [diff] [blame] | 914 | if (page && zeroing && count < PAGE_SIZE) |
| 915 | clear_highpage(page); |
| 916 | |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 917 | while (count) { |
Miklos Szeredi | c302162 | 2010-05-25 15:06:07 +0200 | [diff] [blame] | 918 | if (cs->write && cs->pipebufs && page) { |
| 919 | return fuse_ref_page(cs, page, offset, count); |
| 920 | } else if (!cs->len) { |
Miklos Szeredi | ce534fb | 2010-05-25 15:06:07 +0200 | [diff] [blame] | 921 | if (cs->move_pages && page && |
| 922 | offset == 0 && count == PAGE_SIZE) { |
| 923 | err = fuse_try_move_page(cs, pagep); |
| 924 | if (err <= 0) |
| 925 | return err; |
| 926 | } else { |
| 927 | err = fuse_copy_fill(cs); |
| 928 | if (err) |
| 929 | return err; |
| 930 | } |
Miklos Szeredi | 1729a16 | 2008-11-26 12:03:54 +0100 | [diff] [blame] | 931 | } |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 932 | if (page) { |
Cong Wang | 2408f6e | 2011-11-25 23:14:30 +0800 | [diff] [blame] | 933 | void *mapaddr = kmap_atomic(page); |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 934 | void *buf = mapaddr + offset; |
| 935 | offset += fuse_copy_do(cs, &buf, &count); |
Cong Wang | 2408f6e | 2011-11-25 23:14:30 +0800 | [diff] [blame] | 936 | kunmap_atomic(mapaddr); |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 937 | } else |
| 938 | offset += fuse_copy_do(cs, NULL, &count); |
| 939 | } |
| 940 | if (page && !cs->write) |
| 941 | flush_dcache_page(page); |
| 942 | return 0; |
| 943 | } |
| 944 | |
| 945 | /* Copy pages in the request to/from userspace buffer */ |
| 946 | static int fuse_copy_pages(struct fuse_copy_state *cs, unsigned nbytes, |
| 947 | int zeroing) |
| 948 | { |
| 949 | unsigned i; |
| 950 | struct fuse_req *req = cs->req; |
Miklos Szeredi | 05ea48c | 2019-09-10 15:04:11 +0200 | [diff] [blame] | 951 | struct fuse_args_pages *ap = container_of(req->args, typeof(*ap), args); |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 952 | |
Miklos Szeredi | 05ea48c | 2019-09-10 15:04:11 +0200 | [diff] [blame] | 953 | |
| 954 | for (i = 0; i < ap->num_pages && (nbytes || zeroing); i++) { |
Miklos Szeredi | ce534fb | 2010-05-25 15:06:07 +0200 | [diff] [blame] | 955 | int err; |
Miklos Szeredi | 05ea48c | 2019-09-10 15:04:11 +0200 | [diff] [blame] | 956 | unsigned int offset = ap->descs[i].offset; |
| 957 | unsigned int count = min(nbytes, ap->descs[i].length); |
Miklos Szeredi | ce534fb | 2010-05-25 15:06:07 +0200 | [diff] [blame] | 958 | |
Miklos Szeredi | 05ea48c | 2019-09-10 15:04:11 +0200 | [diff] [blame] | 959 | err = fuse_copy_page(cs, &ap->pages[i], offset, count, zeroing); |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 960 | if (err) |
| 961 | return err; |
| 962 | |
| 963 | nbytes -= count; |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 964 | } |
| 965 | return 0; |
| 966 | } |
| 967 | |
| 968 | /* Copy a single argument in the request to/from userspace buffer */ |
| 969 | static int fuse_copy_one(struct fuse_copy_state *cs, void *val, unsigned size) |
| 970 | { |
| 971 | while (size) { |
Miklos Szeredi | 1729a16 | 2008-11-26 12:03:54 +0100 | [diff] [blame] | 972 | if (!cs->len) { |
| 973 | int err = fuse_copy_fill(cs); |
| 974 | if (err) |
| 975 | return err; |
| 976 | } |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 977 | fuse_copy_do(cs, &val, &size); |
| 978 | } |
| 979 | return 0; |
| 980 | } |
| 981 | |
| 982 | /* Copy request arguments to/from userspace buffer */ |
| 983 | static int fuse_copy_args(struct fuse_copy_state *cs, unsigned numargs, |
| 984 | unsigned argpages, struct fuse_arg *args, |
| 985 | int zeroing) |
| 986 | { |
| 987 | int err = 0; |
| 988 | unsigned i; |
| 989 | |
| 990 | for (i = 0; !err && i < numargs; i++) { |
| 991 | struct fuse_arg *arg = &args[i]; |
| 992 | if (i == numargs - 1 && argpages) |
| 993 | err = fuse_copy_pages(cs, arg->size, zeroing); |
| 994 | else |
| 995 | err = fuse_copy_one(cs, arg->value, arg->size); |
| 996 | } |
| 997 | return err; |
| 998 | } |
| 999 | |
Miklos Szeredi | f88996a | 2015-07-01 16:26:01 +0200 | [diff] [blame] | 1000 | static int forget_pending(struct fuse_iqueue *fiq) |
Miklos Szeredi | 07e77dc | 2010-12-07 20:16:56 +0100 | [diff] [blame] | 1001 | { |
Miklos Szeredi | f88996a | 2015-07-01 16:26:01 +0200 | [diff] [blame] | 1002 | return fiq->forget_list_head.next != NULL; |
Miklos Szeredi | 07e77dc | 2010-12-07 20:16:56 +0100 | [diff] [blame] | 1003 | } |
| 1004 | |
Miklos Szeredi | f88996a | 2015-07-01 16:26:01 +0200 | [diff] [blame] | 1005 | static int request_pending(struct fuse_iqueue *fiq) |
Miklos Szeredi | a4d27e7 | 2006-06-25 05:48:54 -0700 | [diff] [blame] | 1006 | { |
Miklos Szeredi | f88996a | 2015-07-01 16:26:01 +0200 | [diff] [blame] | 1007 | return !list_empty(&fiq->pending) || !list_empty(&fiq->interrupts) || |
| 1008 | forget_pending(fiq); |
Miklos Szeredi | a4d27e7 | 2006-06-25 05:48:54 -0700 | [diff] [blame] | 1009 | } |
| 1010 | |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 1011 | /* |
Miklos Szeredi | a4d27e7 | 2006-06-25 05:48:54 -0700 | [diff] [blame] | 1012 | * Transfer an interrupt request to userspace |
| 1013 | * |
| 1014 | * Unlike other requests this is assembled on demand, without a need |
| 1015 | * to allocate a separate fuse_req structure. |
| 1016 | * |
Eric Biggers | 76e43c8 | 2019-09-08 20:15:18 -0700 | [diff] [blame] | 1017 | * Called with fiq->lock held, releases it |
Miklos Szeredi | a4d27e7 | 2006-06-25 05:48:54 -0700 | [diff] [blame] | 1018 | */ |
Miklos Szeredi | fd22d62 | 2015-07-01 16:26:03 +0200 | [diff] [blame] | 1019 | static int fuse_read_interrupt(struct fuse_iqueue *fiq, |
| 1020 | struct fuse_copy_state *cs, |
Miklos Szeredi | c302162 | 2010-05-25 15:06:07 +0200 | [diff] [blame] | 1021 | size_t nbytes, struct fuse_req *req) |
Eric Biggers | 76e43c8 | 2019-09-08 20:15:18 -0700 | [diff] [blame] | 1022 | __releases(fiq->lock) |
Miklos Szeredi | a4d27e7 | 2006-06-25 05:48:54 -0700 | [diff] [blame] | 1023 | { |
Miklos Szeredi | a4d27e7 | 2006-06-25 05:48:54 -0700 | [diff] [blame] | 1024 | struct fuse_in_header ih; |
| 1025 | struct fuse_interrupt_in arg; |
| 1026 | unsigned reqsize = sizeof(ih) + sizeof(arg); |
| 1027 | int err; |
| 1028 | |
| 1029 | list_del_init(&req->intr_entry); |
Miklos Szeredi | a4d27e7 | 2006-06-25 05:48:54 -0700 | [diff] [blame] | 1030 | memset(&ih, 0, sizeof(ih)); |
| 1031 | memset(&arg, 0, sizeof(arg)); |
| 1032 | ih.len = reqsize; |
| 1033 | ih.opcode = FUSE_INTERRUPT; |
Kirill Tkhai | 3a5358d | 2018-09-11 13:12:05 +0300 | [diff] [blame] | 1034 | ih.unique = (req->in.h.unique | FUSE_INT_REQ_BIT); |
Miklos Szeredi | a4d27e7 | 2006-06-25 05:48:54 -0700 | [diff] [blame] | 1035 | arg.unique = req->in.h.unique; |
| 1036 | |
Eric Biggers | 76e43c8 | 2019-09-08 20:15:18 -0700 | [diff] [blame] | 1037 | spin_unlock(&fiq->lock); |
Miklos Szeredi | c302162 | 2010-05-25 15:06:07 +0200 | [diff] [blame] | 1038 | if (nbytes < reqsize) |
Miklos Szeredi | a4d27e7 | 2006-06-25 05:48:54 -0700 | [diff] [blame] | 1039 | return -EINVAL; |
| 1040 | |
Miklos Szeredi | c302162 | 2010-05-25 15:06:07 +0200 | [diff] [blame] | 1041 | err = fuse_copy_one(cs, &ih, sizeof(ih)); |
Miklos Szeredi | a4d27e7 | 2006-06-25 05:48:54 -0700 | [diff] [blame] | 1042 | if (!err) |
Miklos Szeredi | c302162 | 2010-05-25 15:06:07 +0200 | [diff] [blame] | 1043 | err = fuse_copy_one(cs, &arg, sizeof(arg)); |
| 1044 | fuse_copy_finish(cs); |
Miklos Szeredi | a4d27e7 | 2006-06-25 05:48:54 -0700 | [diff] [blame] | 1045 | |
| 1046 | return err ? err : reqsize; |
| 1047 | } |
| 1048 | |
Vivek Goyal | 4388c5a | 2019-06-05 15:50:43 -0400 | [diff] [blame] | 1049 | struct fuse_forget_link *fuse_dequeue_forget(struct fuse_iqueue *fiq, |
| 1050 | unsigned int max, |
| 1051 | unsigned int *countp) |
Miklos Szeredi | 07e77dc | 2010-12-07 20:16:56 +0100 | [diff] [blame] | 1052 | { |
Miklos Szeredi | f88996a | 2015-07-01 16:26:01 +0200 | [diff] [blame] | 1053 | struct fuse_forget_link *head = fiq->forget_list_head.next; |
Miklos Szeredi | 02c048b | 2010-12-07 20:16:56 +0100 | [diff] [blame] | 1054 | struct fuse_forget_link **newhead = &head; |
| 1055 | unsigned count; |
Miklos Szeredi | 07e77dc | 2010-12-07 20:16:56 +0100 | [diff] [blame] | 1056 | |
Miklos Szeredi | 02c048b | 2010-12-07 20:16:56 +0100 | [diff] [blame] | 1057 | for (count = 0; *newhead != NULL && count < max; count++) |
| 1058 | newhead = &(*newhead)->next; |
| 1059 | |
Miklos Szeredi | f88996a | 2015-07-01 16:26:01 +0200 | [diff] [blame] | 1060 | fiq->forget_list_head.next = *newhead; |
Miklos Szeredi | 02c048b | 2010-12-07 20:16:56 +0100 | [diff] [blame] | 1061 | *newhead = NULL; |
Miklos Szeredi | f88996a | 2015-07-01 16:26:01 +0200 | [diff] [blame] | 1062 | if (fiq->forget_list_head.next == NULL) |
| 1063 | fiq->forget_list_tail = &fiq->forget_list_head; |
Miklos Szeredi | 07e77dc | 2010-12-07 20:16:56 +0100 | [diff] [blame] | 1064 | |
Miklos Szeredi | 02c048b | 2010-12-07 20:16:56 +0100 | [diff] [blame] | 1065 | if (countp != NULL) |
| 1066 | *countp = count; |
| 1067 | |
| 1068 | return head; |
Miklos Szeredi | 07e77dc | 2010-12-07 20:16:56 +0100 | [diff] [blame] | 1069 | } |
Vivek Goyal | 4388c5a | 2019-06-05 15:50:43 -0400 | [diff] [blame] | 1070 | EXPORT_SYMBOL(fuse_dequeue_forget); |
Miklos Szeredi | 07e77dc | 2010-12-07 20:16:56 +0100 | [diff] [blame] | 1071 | |
Miklos Szeredi | fd22d62 | 2015-07-01 16:26:03 +0200 | [diff] [blame] | 1072 | static int fuse_read_single_forget(struct fuse_iqueue *fiq, |
Miklos Szeredi | 07e77dc | 2010-12-07 20:16:56 +0100 | [diff] [blame] | 1073 | struct fuse_copy_state *cs, |
| 1074 | size_t nbytes) |
Eric Biggers | 76e43c8 | 2019-09-08 20:15:18 -0700 | [diff] [blame] | 1075 | __releases(fiq->lock) |
Miklos Szeredi | 07e77dc | 2010-12-07 20:16:56 +0100 | [diff] [blame] | 1076 | { |
| 1077 | int err; |
Vivek Goyal | 4388c5a | 2019-06-05 15:50:43 -0400 | [diff] [blame] | 1078 | struct fuse_forget_link *forget = fuse_dequeue_forget(fiq, 1, NULL); |
Miklos Szeredi | 07e77dc | 2010-12-07 20:16:56 +0100 | [diff] [blame] | 1079 | struct fuse_forget_in arg = { |
Miklos Szeredi | 02c048b | 2010-12-07 20:16:56 +0100 | [diff] [blame] | 1080 | .nlookup = forget->forget_one.nlookup, |
Miklos Szeredi | 07e77dc | 2010-12-07 20:16:56 +0100 | [diff] [blame] | 1081 | }; |
| 1082 | struct fuse_in_header ih = { |
| 1083 | .opcode = FUSE_FORGET, |
Miklos Szeredi | 02c048b | 2010-12-07 20:16:56 +0100 | [diff] [blame] | 1084 | .nodeid = forget->forget_one.nodeid, |
Miklos Szeredi | f88996a | 2015-07-01 16:26:01 +0200 | [diff] [blame] | 1085 | .unique = fuse_get_unique(fiq), |
Miklos Szeredi | 07e77dc | 2010-12-07 20:16:56 +0100 | [diff] [blame] | 1086 | .len = sizeof(ih) + sizeof(arg), |
| 1087 | }; |
| 1088 | |
Eric Biggers | 76e43c8 | 2019-09-08 20:15:18 -0700 | [diff] [blame] | 1089 | spin_unlock(&fiq->lock); |
Miklos Szeredi | 07e77dc | 2010-12-07 20:16:56 +0100 | [diff] [blame] | 1090 | kfree(forget); |
| 1091 | if (nbytes < ih.len) |
| 1092 | return -EINVAL; |
| 1093 | |
| 1094 | err = fuse_copy_one(cs, &ih, sizeof(ih)); |
| 1095 | if (!err) |
| 1096 | err = fuse_copy_one(cs, &arg, sizeof(arg)); |
| 1097 | fuse_copy_finish(cs); |
| 1098 | |
| 1099 | if (err) |
| 1100 | return err; |
| 1101 | |
| 1102 | return ih.len; |
| 1103 | } |
| 1104 | |
Miklos Szeredi | fd22d62 | 2015-07-01 16:26:03 +0200 | [diff] [blame] | 1105 | static int fuse_read_batch_forget(struct fuse_iqueue *fiq, |
Miklos Szeredi | 02c048b | 2010-12-07 20:16:56 +0100 | [diff] [blame] | 1106 | struct fuse_copy_state *cs, size_t nbytes) |
Eric Biggers | 76e43c8 | 2019-09-08 20:15:18 -0700 | [diff] [blame] | 1107 | __releases(fiq->lock) |
Miklos Szeredi | 02c048b | 2010-12-07 20:16:56 +0100 | [diff] [blame] | 1108 | { |
| 1109 | int err; |
| 1110 | unsigned max_forgets; |
| 1111 | unsigned count; |
| 1112 | struct fuse_forget_link *head; |
| 1113 | struct fuse_batch_forget_in arg = { .count = 0 }; |
| 1114 | struct fuse_in_header ih = { |
| 1115 | .opcode = FUSE_BATCH_FORGET, |
Miklos Szeredi | f88996a | 2015-07-01 16:26:01 +0200 | [diff] [blame] | 1116 | .unique = fuse_get_unique(fiq), |
Miklos Szeredi | 02c048b | 2010-12-07 20:16:56 +0100 | [diff] [blame] | 1117 | .len = sizeof(ih) + sizeof(arg), |
| 1118 | }; |
| 1119 | |
| 1120 | if (nbytes < ih.len) { |
Eric Biggers | 76e43c8 | 2019-09-08 20:15:18 -0700 | [diff] [blame] | 1121 | spin_unlock(&fiq->lock); |
Miklos Szeredi | 02c048b | 2010-12-07 20:16:56 +0100 | [diff] [blame] | 1122 | return -EINVAL; |
| 1123 | } |
| 1124 | |
| 1125 | max_forgets = (nbytes - ih.len) / sizeof(struct fuse_forget_one); |
Vivek Goyal | 4388c5a | 2019-06-05 15:50:43 -0400 | [diff] [blame] | 1126 | head = fuse_dequeue_forget(fiq, max_forgets, &count); |
Eric Biggers | 76e43c8 | 2019-09-08 20:15:18 -0700 | [diff] [blame] | 1127 | spin_unlock(&fiq->lock); |
Miklos Szeredi | 02c048b | 2010-12-07 20:16:56 +0100 | [diff] [blame] | 1128 | |
| 1129 | arg.count = count; |
| 1130 | ih.len += count * sizeof(struct fuse_forget_one); |
| 1131 | err = fuse_copy_one(cs, &ih, sizeof(ih)); |
| 1132 | if (!err) |
| 1133 | err = fuse_copy_one(cs, &arg, sizeof(arg)); |
| 1134 | |
| 1135 | while (head) { |
| 1136 | struct fuse_forget_link *forget = head; |
| 1137 | |
| 1138 | if (!err) { |
| 1139 | err = fuse_copy_one(cs, &forget->forget_one, |
| 1140 | sizeof(forget->forget_one)); |
| 1141 | } |
| 1142 | head = forget->next; |
| 1143 | kfree(forget); |
| 1144 | } |
| 1145 | |
| 1146 | fuse_copy_finish(cs); |
| 1147 | |
| 1148 | if (err) |
| 1149 | return err; |
| 1150 | |
| 1151 | return ih.len; |
| 1152 | } |
| 1153 | |
Miklos Szeredi | fd22d62 | 2015-07-01 16:26:03 +0200 | [diff] [blame] | 1154 | static int fuse_read_forget(struct fuse_conn *fc, struct fuse_iqueue *fiq, |
| 1155 | struct fuse_copy_state *cs, |
Miklos Szeredi | 02c048b | 2010-12-07 20:16:56 +0100 | [diff] [blame] | 1156 | size_t nbytes) |
Eric Biggers | 76e43c8 | 2019-09-08 20:15:18 -0700 | [diff] [blame] | 1157 | __releases(fiq->lock) |
Miklos Szeredi | 02c048b | 2010-12-07 20:16:56 +0100 | [diff] [blame] | 1158 | { |
Miklos Szeredi | f88996a | 2015-07-01 16:26:01 +0200 | [diff] [blame] | 1159 | if (fc->minor < 16 || fiq->forget_list_head.next->next == NULL) |
Miklos Szeredi | fd22d62 | 2015-07-01 16:26:03 +0200 | [diff] [blame] | 1160 | return fuse_read_single_forget(fiq, cs, nbytes); |
Miklos Szeredi | 02c048b | 2010-12-07 20:16:56 +0100 | [diff] [blame] | 1161 | else |
Miklos Szeredi | fd22d62 | 2015-07-01 16:26:03 +0200 | [diff] [blame] | 1162 | return fuse_read_batch_forget(fiq, cs, nbytes); |
Miklos Szeredi | 02c048b | 2010-12-07 20:16:56 +0100 | [diff] [blame] | 1163 | } |
| 1164 | |
Miklos Szeredi | a4d27e7 | 2006-06-25 05:48:54 -0700 | [diff] [blame] | 1165 | /* |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 1166 | * Read a single request into the userspace filesystem's buffer. This |
| 1167 | * function waits until a request is available, then removes it from |
| 1168 | * the pending list and copies request data to userspace buffer. If |
Miklos Szeredi | f9a2842 | 2006-06-25 05:48:53 -0700 | [diff] [blame] | 1169 | * no reply is needed (FORGET) or request has been aborted or there |
| 1170 | * was an error during the copying then it's finished by calling |
Stefan Hajnoczi | 04ec5af | 2018-06-21 09:33:40 +0100 | [diff] [blame] | 1171 | * fuse_request_end(). Otherwise add it to the processing list, and set |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 1172 | * the 'sent' flag. |
| 1173 | */ |
Miklos Szeredi | c3696046 | 2015-07-01 16:26:09 +0200 | [diff] [blame] | 1174 | static ssize_t fuse_dev_do_read(struct fuse_dev *fud, struct file *file, |
Miklos Szeredi | c302162 | 2010-05-25 15:06:07 +0200 | [diff] [blame] | 1175 | struct fuse_copy_state *cs, size_t nbytes) |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 1176 | { |
Miklos Szeredi | 82cbdcd | 2015-07-01 16:26:05 +0200 | [diff] [blame] | 1177 | ssize_t err; |
Miklos Szeredi | c3696046 | 2015-07-01 16:26:09 +0200 | [diff] [blame] | 1178 | struct fuse_conn *fc = fud->fc; |
Miklos Szeredi | f88996a | 2015-07-01 16:26:01 +0200 | [diff] [blame] | 1179 | struct fuse_iqueue *fiq = &fc->iq; |
Miklos Szeredi | c3696046 | 2015-07-01 16:26:09 +0200 | [diff] [blame] | 1180 | struct fuse_pqueue *fpq = &fud->pq; |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 1181 | struct fuse_req *req; |
Miklos Szeredi | d499377 | 2019-09-10 15:04:11 +0200 | [diff] [blame] | 1182 | struct fuse_args *args; |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 1183 | unsigned reqsize; |
Kirill Tkhai | be2ff42 | 2018-09-11 13:12:14 +0300 | [diff] [blame] | 1184 | unsigned int hash; |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 1185 | |
Kirill Smelkov | 1fb027d | 2019-07-08 17:03:31 +0000 | [diff] [blame] | 1186 | /* |
| 1187 | * Require sane minimum read buffer - that has capacity for fixed part |
| 1188 | * of any request header + negotiated max_write room for data. |
| 1189 | * |
| 1190 | * Historically libfuse reserves 4K for fixed header room, but e.g. |
| 1191 | * GlusterFS reserves only 80 bytes |
| 1192 | * |
| 1193 | * = `sizeof(fuse_in_header) + sizeof(fuse_write_in)` |
| 1194 | * |
| 1195 | * which is the absolute minimum any sane filesystem should be using |
| 1196 | * for header room. |
| 1197 | */ |
| 1198 | if (nbytes < max_t(size_t, FUSE_MIN_READ_BUFFER, |
| 1199 | sizeof(struct fuse_in_header) + |
| 1200 | sizeof(struct fuse_write_in) + |
| 1201 | fc->max_write)) |
| 1202 | return -EINVAL; |
| 1203 | |
Miklos Szeredi | 1d3d752 | 2006-01-06 00:19:40 -0800 | [diff] [blame] | 1204 | restart: |
Eric Biggers | 76e43c8 | 2019-09-08 20:15:18 -0700 | [diff] [blame] | 1205 | for (;;) { |
| 1206 | spin_lock(&fiq->lock); |
| 1207 | if (!fiq->connected || request_pending(fiq)) |
| 1208 | break; |
| 1209 | spin_unlock(&fiq->lock); |
Jeff Dike | e5ac1d1 | 2006-04-10 22:54:53 -0700 | [diff] [blame] | 1210 | |
Eric Biggers | 76e43c8 | 2019-09-08 20:15:18 -0700 | [diff] [blame] | 1211 | if (file->f_flags & O_NONBLOCK) |
| 1212 | return -EAGAIN; |
| 1213 | err = wait_event_interruptible_exclusive(fiq->waitq, |
Miklos Szeredi | 5250921 | 2015-07-01 16:26:03 +0200 | [diff] [blame] | 1214 | !fiq->connected || request_pending(fiq)); |
Eric Biggers | 76e43c8 | 2019-09-08 20:15:18 -0700 | [diff] [blame] | 1215 | if (err) |
| 1216 | return err; |
| 1217 | } |
Miklos Szeredi | 5250921 | 2015-07-01 16:26:03 +0200 | [diff] [blame] | 1218 | |
Szymon Lukasz | 3b7008b | 2017-11-09 21:23:35 +0100 | [diff] [blame] | 1219 | if (!fiq->connected) { |
Miklos Szeredi | eb98e3bd | 2019-01-24 10:40:16 +0100 | [diff] [blame] | 1220 | err = fc->aborted ? -ECONNABORTED : -ENODEV; |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 1221 | goto err_unlock; |
Szymon Lukasz | 3b7008b | 2017-11-09 21:23:35 +0100 | [diff] [blame] | 1222 | } |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 1223 | |
Miklos Szeredi | f88996a | 2015-07-01 16:26:01 +0200 | [diff] [blame] | 1224 | if (!list_empty(&fiq->interrupts)) { |
| 1225 | req = list_entry(fiq->interrupts.next, struct fuse_req, |
Miklos Szeredi | a4d27e7 | 2006-06-25 05:48:54 -0700 | [diff] [blame] | 1226 | intr_entry); |
Miklos Szeredi | fd22d62 | 2015-07-01 16:26:03 +0200 | [diff] [blame] | 1227 | return fuse_read_interrupt(fiq, cs, nbytes, req); |
Miklos Szeredi | a4d27e7 | 2006-06-25 05:48:54 -0700 | [diff] [blame] | 1228 | } |
| 1229 | |
Miklos Szeredi | f88996a | 2015-07-01 16:26:01 +0200 | [diff] [blame] | 1230 | if (forget_pending(fiq)) { |
| 1231 | if (list_empty(&fiq->pending) || fiq->forget_batch-- > 0) |
Miklos Szeredi | fd22d62 | 2015-07-01 16:26:03 +0200 | [diff] [blame] | 1232 | return fuse_read_forget(fc, fiq, cs, nbytes); |
Miklos Szeredi | 07e77dc | 2010-12-07 20:16:56 +0100 | [diff] [blame] | 1233 | |
Miklos Szeredi | f88996a | 2015-07-01 16:26:01 +0200 | [diff] [blame] | 1234 | if (fiq->forget_batch <= -8) |
| 1235 | fiq->forget_batch = 16; |
Miklos Szeredi | 07e77dc | 2010-12-07 20:16:56 +0100 | [diff] [blame] | 1236 | } |
| 1237 | |
Miklos Szeredi | f88996a | 2015-07-01 16:26:01 +0200 | [diff] [blame] | 1238 | req = list_entry(fiq->pending.next, struct fuse_req, list); |
Miklos Szeredi | 33e14b4 | 2015-07-01 16:26:01 +0200 | [diff] [blame] | 1239 | clear_bit(FR_PENDING, &req->flags); |
Miklos Szeredi | ef75925 | 2015-07-01 16:26:02 +0200 | [diff] [blame] | 1240 | list_del_init(&req->list); |
Eric Biggers | 76e43c8 | 2019-09-08 20:15:18 -0700 | [diff] [blame] | 1241 | spin_unlock(&fiq->lock); |
Miklos Szeredi | 4ce6081 | 2015-07-01 16:26:02 +0200 | [diff] [blame] | 1242 | |
Miklos Szeredi | d499377 | 2019-09-10 15:04:11 +0200 | [diff] [blame] | 1243 | args = req->args; |
| 1244 | reqsize = req->in.h.len; |
Miklos Szeredi | 5d6d3a3 | 2017-09-12 16:57:53 +0200 | [diff] [blame] | 1245 | |
Miklos Szeredi | 1d3d752 | 2006-01-06 00:19:40 -0800 | [diff] [blame] | 1246 | /* If request is too large, reply with an error and restart the read */ |
Miklos Szeredi | c302162 | 2010-05-25 15:06:07 +0200 | [diff] [blame] | 1247 | if (nbytes < reqsize) { |
Miklos Szeredi | 1d3d752 | 2006-01-06 00:19:40 -0800 | [diff] [blame] | 1248 | req->out.h.error = -EIO; |
| 1249 | /* SETXATTR is special, since it may contain too large data */ |
Miklos Szeredi | d499377 | 2019-09-10 15:04:11 +0200 | [diff] [blame] | 1250 | if (args->opcode == FUSE_SETXATTR) |
Miklos Szeredi | 1d3d752 | 2006-01-06 00:19:40 -0800 | [diff] [blame] | 1251 | req->out.h.error = -E2BIG; |
Stefan Hajnoczi | 04ec5af | 2018-06-21 09:33:40 +0100 | [diff] [blame] | 1252 | fuse_request_end(fc, req); |
Miklos Szeredi | 1d3d752 | 2006-01-06 00:19:40 -0800 | [diff] [blame] | 1253 | goto restart; |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 1254 | } |
Miklos Szeredi | 45a91cb | 2015-07-01 16:26:06 +0200 | [diff] [blame] | 1255 | spin_lock(&fpq->lock); |
Miklos Szeredi | 82cbdcd | 2015-07-01 16:26:05 +0200 | [diff] [blame] | 1256 | list_add(&req->list, &fpq->io); |
Miklos Szeredi | 45a91cb | 2015-07-01 16:26:06 +0200 | [diff] [blame] | 1257 | spin_unlock(&fpq->lock); |
Miklos Szeredi | c302162 | 2010-05-25 15:06:07 +0200 | [diff] [blame] | 1258 | cs->req = req; |
Miklos Szeredi | d499377 | 2019-09-10 15:04:11 +0200 | [diff] [blame] | 1259 | err = fuse_copy_one(cs, &req->in.h, sizeof(req->in.h)); |
Miklos Szeredi | 1d3d752 | 2006-01-06 00:19:40 -0800 | [diff] [blame] | 1260 | if (!err) |
Miklos Szeredi | d499377 | 2019-09-10 15:04:11 +0200 | [diff] [blame] | 1261 | err = fuse_copy_args(cs, args->in_numargs, args->in_pages, |
| 1262 | (struct fuse_arg *) args->in_args, 0); |
Miklos Szeredi | c302162 | 2010-05-25 15:06:07 +0200 | [diff] [blame] | 1263 | fuse_copy_finish(cs); |
Miklos Szeredi | 45a91cb | 2015-07-01 16:26:06 +0200 | [diff] [blame] | 1264 | spin_lock(&fpq->lock); |
Miklos Szeredi | 825d6d3 | 2015-07-01 16:25:58 +0200 | [diff] [blame] | 1265 | clear_bit(FR_LOCKED, &req->flags); |
Miklos Szeredi | e96edd9 | 2015-07-01 16:26:04 +0200 | [diff] [blame] | 1266 | if (!fpq->connected) { |
Miklos Szeredi | eb98e3bd | 2019-01-24 10:40:16 +0100 | [diff] [blame] | 1267 | err = fc->aborted ? -ECONNABORTED : -ENODEV; |
Miklos Szeredi | 82cbdcd | 2015-07-01 16:26:05 +0200 | [diff] [blame] | 1268 | goto out_end; |
Miklos Szeredi | c9c9d7d | 2007-10-16 23:31:05 -0700 | [diff] [blame] | 1269 | } |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 1270 | if (err) { |
Miklos Szeredi | c9c9d7d | 2007-10-16 23:31:05 -0700 | [diff] [blame] | 1271 | req->out.h.error = -EIO; |
Miklos Szeredi | 82cbdcd | 2015-07-01 16:26:05 +0200 | [diff] [blame] | 1272 | goto out_end; |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 1273 | } |
Miklos Szeredi | 825d6d3 | 2015-07-01 16:25:58 +0200 | [diff] [blame] | 1274 | if (!test_bit(FR_ISREPLY, &req->flags)) { |
Miklos Szeredi | 82cbdcd | 2015-07-01 16:26:05 +0200 | [diff] [blame] | 1275 | err = reqsize; |
| 1276 | goto out_end; |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 1277 | } |
Kirill Tkhai | be2ff42 | 2018-09-11 13:12:14 +0300 | [diff] [blame] | 1278 | hash = fuse_req_hash(req->in.h.unique); |
| 1279 | list_move_tail(&req->list, &fpq->processing[hash]); |
Kirill Tkhai | bc78abb | 2018-09-25 12:28:55 +0300 | [diff] [blame] | 1280 | __fuse_get_request(req); |
Miklos Szeredi | 82cbdcd | 2015-07-01 16:26:05 +0200 | [diff] [blame] | 1281 | set_bit(FR_SENT, &req->flags); |
Miklos Szeredi | 4c316f2 | 2018-09-28 16:43:22 +0200 | [diff] [blame] | 1282 | spin_unlock(&fpq->lock); |
Miklos Szeredi | 82cbdcd | 2015-07-01 16:26:05 +0200 | [diff] [blame] | 1283 | /* matches barrier in request_wait_answer() */ |
| 1284 | smp_mb__after_atomic(); |
| 1285 | if (test_bit(FR_INTERRUPTED, &req->flags)) |
| 1286 | queue_interrupt(fiq, req); |
Kirill Tkhai | bc78abb | 2018-09-25 12:28:55 +0300 | [diff] [blame] | 1287 | fuse_put_request(fc, req); |
Miklos Szeredi | 82cbdcd | 2015-07-01 16:26:05 +0200 | [diff] [blame] | 1288 | |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 1289 | return reqsize; |
| 1290 | |
Miklos Szeredi | 82cbdcd | 2015-07-01 16:26:05 +0200 | [diff] [blame] | 1291 | out_end: |
Miklos Szeredi | 77cd9d4 | 2015-07-01 16:26:06 +0200 | [diff] [blame] | 1292 | if (!test_bit(FR_PRIVATE, &req->flags)) |
| 1293 | list_del_init(&req->list); |
Miklos Szeredi | 45a91cb | 2015-07-01 16:26:06 +0200 | [diff] [blame] | 1294 | spin_unlock(&fpq->lock); |
Stefan Hajnoczi | 04ec5af | 2018-06-21 09:33:40 +0100 | [diff] [blame] | 1295 | fuse_request_end(fc, req); |
Miklos Szeredi | 82cbdcd | 2015-07-01 16:26:05 +0200 | [diff] [blame] | 1296 | return err; |
| 1297 | |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 1298 | err_unlock: |
Eric Biggers | 76e43c8 | 2019-09-08 20:15:18 -0700 | [diff] [blame] | 1299 | spin_unlock(&fiq->lock); |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 1300 | return err; |
| 1301 | } |
| 1302 | |
Tom Van Braeckel | 94e4fe2 | 2015-01-12 05:22:16 +0100 | [diff] [blame] | 1303 | static int fuse_dev_open(struct inode *inode, struct file *file) |
| 1304 | { |
| 1305 | /* |
| 1306 | * The fuse device's file's private_data is used to hold |
| 1307 | * the fuse_conn(ection) when it is mounted, and is used to |
| 1308 | * keep track of whether the file has been mounted already. |
| 1309 | */ |
| 1310 | file->private_data = NULL; |
| 1311 | return 0; |
| 1312 | } |
| 1313 | |
Al Viro | fbdbacc | 2015-04-03 21:53:39 -0400 | [diff] [blame] | 1314 | static ssize_t fuse_dev_read(struct kiocb *iocb, struct iov_iter *to) |
Miklos Szeredi | c302162 | 2010-05-25 15:06:07 +0200 | [diff] [blame] | 1315 | { |
| 1316 | struct fuse_copy_state cs; |
| 1317 | struct file *file = iocb->ki_filp; |
Miklos Szeredi | cc080e9 | 2015-07-01 16:26:08 +0200 | [diff] [blame] | 1318 | struct fuse_dev *fud = fuse_get_dev(file); |
| 1319 | |
| 1320 | if (!fud) |
Miklos Szeredi | c302162 | 2010-05-25 15:06:07 +0200 | [diff] [blame] | 1321 | return -EPERM; |
| 1322 | |
Al Viro | fbdbacc | 2015-04-03 21:53:39 -0400 | [diff] [blame] | 1323 | if (!iter_is_iovec(to)) |
| 1324 | return -EINVAL; |
Miklos Szeredi | c302162 | 2010-05-25 15:06:07 +0200 | [diff] [blame] | 1325 | |
Miklos Szeredi | dc00809 | 2015-07-01 16:25:58 +0200 | [diff] [blame] | 1326 | fuse_copy_init(&cs, 1, to); |
Al Viro | fbdbacc | 2015-04-03 21:53:39 -0400 | [diff] [blame] | 1327 | |
Miklos Szeredi | c3696046 | 2015-07-01 16:26:09 +0200 | [diff] [blame] | 1328 | return fuse_dev_do_read(fud, file, &cs, iov_iter_count(to)); |
Miklos Szeredi | c302162 | 2010-05-25 15:06:07 +0200 | [diff] [blame] | 1329 | } |
| 1330 | |
Miklos Szeredi | c302162 | 2010-05-25 15:06:07 +0200 | [diff] [blame] | 1331 | static ssize_t fuse_dev_splice_read(struct file *in, loff_t *ppos, |
| 1332 | struct pipe_inode_info *pipe, |
| 1333 | size_t len, unsigned int flags) |
| 1334 | { |
Al Viro | d82718e | 2016-09-17 22:56:25 -0400 | [diff] [blame] | 1335 | int total, ret; |
Miklos Szeredi | c302162 | 2010-05-25 15:06:07 +0200 | [diff] [blame] | 1336 | int page_nr = 0; |
Miklos Szeredi | c302162 | 2010-05-25 15:06:07 +0200 | [diff] [blame] | 1337 | struct pipe_buffer *bufs; |
| 1338 | struct fuse_copy_state cs; |
Miklos Szeredi | cc080e9 | 2015-07-01 16:26:08 +0200 | [diff] [blame] | 1339 | struct fuse_dev *fud = fuse_get_dev(in); |
| 1340 | |
| 1341 | if (!fud) |
Miklos Szeredi | c302162 | 2010-05-25 15:06:07 +0200 | [diff] [blame] | 1342 | return -EPERM; |
| 1343 | |
David Howells | 6718b6f | 2019-10-16 16:47:32 +0100 | [diff] [blame^] | 1344 | bufs = kvmalloc_array(pipe->max_usage, sizeof(struct pipe_buffer), |
Andrey Ryabinin | d6d931a | 2018-07-17 19:00:34 +0300 | [diff] [blame] | 1345 | GFP_KERNEL); |
Miklos Szeredi | c302162 | 2010-05-25 15:06:07 +0200 | [diff] [blame] | 1346 | if (!bufs) |
| 1347 | return -ENOMEM; |
| 1348 | |
Miklos Szeredi | dc00809 | 2015-07-01 16:25:58 +0200 | [diff] [blame] | 1349 | fuse_copy_init(&cs, 1, NULL); |
Miklos Szeredi | c302162 | 2010-05-25 15:06:07 +0200 | [diff] [blame] | 1350 | cs.pipebufs = bufs; |
| 1351 | cs.pipe = pipe; |
Miklos Szeredi | c3696046 | 2015-07-01 16:26:09 +0200 | [diff] [blame] | 1352 | ret = fuse_dev_do_read(fud, in, &cs, len); |
Miklos Szeredi | c302162 | 2010-05-25 15:06:07 +0200 | [diff] [blame] | 1353 | if (ret < 0) |
| 1354 | goto out; |
| 1355 | |
David Howells | 6718b6f | 2019-10-16 16:47:32 +0100 | [diff] [blame^] | 1356 | if (pipe_occupancy(pipe->head, pipe->tail) + cs.nr_segs > pipe->max_usage) { |
Miklos Szeredi | c302162 | 2010-05-25 15:06:07 +0200 | [diff] [blame] | 1357 | ret = -EIO; |
Al Viro | d82718e | 2016-09-17 22:56:25 -0400 | [diff] [blame] | 1358 | goto out; |
Miklos Szeredi | c302162 | 2010-05-25 15:06:07 +0200 | [diff] [blame] | 1359 | } |
| 1360 | |
Al Viro | d82718e | 2016-09-17 22:56:25 -0400 | [diff] [blame] | 1361 | for (ret = total = 0; page_nr < cs.nr_segs; total += ret) { |
Miklos Szeredi | 28a625c | 2014-01-22 19:36:57 +0100 | [diff] [blame] | 1362 | /* |
| 1363 | * Need to be careful about this. Having buf->ops in module |
| 1364 | * code can Oops if the buffer persists after module unload. |
| 1365 | */ |
Al Viro | d82718e | 2016-09-17 22:56:25 -0400 | [diff] [blame] | 1366 | bufs[page_nr].ops = &nosteal_pipe_buf_ops; |
Miklos Szeredi | 84588a9 | 2017-02-16 15:08:20 +0100 | [diff] [blame] | 1367 | bufs[page_nr].flags = 0; |
Al Viro | d82718e | 2016-09-17 22:56:25 -0400 | [diff] [blame] | 1368 | ret = add_to_pipe(pipe, &bufs[page_nr++]); |
| 1369 | if (unlikely(ret < 0)) |
| 1370 | break; |
Miklos Szeredi | c302162 | 2010-05-25 15:06:07 +0200 | [diff] [blame] | 1371 | } |
Al Viro | d82718e | 2016-09-17 22:56:25 -0400 | [diff] [blame] | 1372 | if (total) |
| 1373 | ret = total; |
Miklos Szeredi | c302162 | 2010-05-25 15:06:07 +0200 | [diff] [blame] | 1374 | out: |
| 1375 | for (; page_nr < cs.nr_segs; page_nr++) |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1376 | put_page(bufs[page_nr].page); |
Miklos Szeredi | c302162 | 2010-05-25 15:06:07 +0200 | [diff] [blame] | 1377 | |
Andrey Ryabinin | d6d931a | 2018-07-17 19:00:34 +0300 | [diff] [blame] | 1378 | kvfree(bufs); |
Miklos Szeredi | c302162 | 2010-05-25 15:06:07 +0200 | [diff] [blame] | 1379 | return ret; |
| 1380 | } |
| 1381 | |
Tejun Heo | 95668a6 | 2008-11-26 12:03:55 +0100 | [diff] [blame] | 1382 | static int fuse_notify_poll(struct fuse_conn *fc, unsigned int size, |
| 1383 | struct fuse_copy_state *cs) |
| 1384 | { |
| 1385 | struct fuse_notify_poll_wakeup_out outarg; |
Miklos Szeredi | f6d47a1 | 2009-01-26 15:00:59 +0100 | [diff] [blame] | 1386 | int err = -EINVAL; |
Tejun Heo | 95668a6 | 2008-11-26 12:03:55 +0100 | [diff] [blame] | 1387 | |
| 1388 | if (size != sizeof(outarg)) |
Miklos Szeredi | f6d47a1 | 2009-01-26 15:00:59 +0100 | [diff] [blame] | 1389 | goto err; |
Tejun Heo | 95668a6 | 2008-11-26 12:03:55 +0100 | [diff] [blame] | 1390 | |
| 1391 | err = fuse_copy_one(cs, &outarg, sizeof(outarg)); |
| 1392 | if (err) |
Miklos Szeredi | f6d47a1 | 2009-01-26 15:00:59 +0100 | [diff] [blame] | 1393 | goto err; |
Tejun Heo | 95668a6 | 2008-11-26 12:03:55 +0100 | [diff] [blame] | 1394 | |
Miklos Szeredi | f6d47a1 | 2009-01-26 15:00:59 +0100 | [diff] [blame] | 1395 | fuse_copy_finish(cs); |
Tejun Heo | 95668a6 | 2008-11-26 12:03:55 +0100 | [diff] [blame] | 1396 | return fuse_notify_poll_wakeup(fc, &outarg); |
Miklos Szeredi | f6d47a1 | 2009-01-26 15:00:59 +0100 | [diff] [blame] | 1397 | |
| 1398 | err: |
| 1399 | fuse_copy_finish(cs); |
| 1400 | return err; |
Tejun Heo | 95668a6 | 2008-11-26 12:03:55 +0100 | [diff] [blame] | 1401 | } |
| 1402 | |
John Muir | 3b463ae | 2009-05-31 11:13:57 -0400 | [diff] [blame] | 1403 | static int fuse_notify_inval_inode(struct fuse_conn *fc, unsigned int size, |
| 1404 | struct fuse_copy_state *cs) |
| 1405 | { |
| 1406 | struct fuse_notify_inval_inode_out outarg; |
| 1407 | int err = -EINVAL; |
| 1408 | |
| 1409 | if (size != sizeof(outarg)) |
| 1410 | goto err; |
| 1411 | |
| 1412 | err = fuse_copy_one(cs, &outarg, sizeof(outarg)); |
| 1413 | if (err) |
| 1414 | goto err; |
| 1415 | fuse_copy_finish(cs); |
| 1416 | |
| 1417 | down_read(&fc->killsb); |
| 1418 | err = -ENOENT; |
Miklos Szeredi | b21dda4 | 2010-02-05 12:08:31 +0100 | [diff] [blame] | 1419 | if (fc->sb) { |
| 1420 | err = fuse_reverse_inval_inode(fc->sb, outarg.ino, |
| 1421 | outarg.off, outarg.len); |
| 1422 | } |
John Muir | 3b463ae | 2009-05-31 11:13:57 -0400 | [diff] [blame] | 1423 | up_read(&fc->killsb); |
| 1424 | return err; |
| 1425 | |
| 1426 | err: |
| 1427 | fuse_copy_finish(cs); |
| 1428 | return err; |
| 1429 | } |
| 1430 | |
| 1431 | static int fuse_notify_inval_entry(struct fuse_conn *fc, unsigned int size, |
| 1432 | struct fuse_copy_state *cs) |
| 1433 | { |
| 1434 | struct fuse_notify_inval_entry_out outarg; |
Fang Wenqi | b2d82ee | 2009-12-30 18:37:13 +0800 | [diff] [blame] | 1435 | int err = -ENOMEM; |
| 1436 | char *buf; |
John Muir | 3b463ae | 2009-05-31 11:13:57 -0400 | [diff] [blame] | 1437 | struct qstr name; |
| 1438 | |
Fang Wenqi | b2d82ee | 2009-12-30 18:37:13 +0800 | [diff] [blame] | 1439 | buf = kzalloc(FUSE_NAME_MAX + 1, GFP_KERNEL); |
| 1440 | if (!buf) |
| 1441 | goto err; |
| 1442 | |
| 1443 | err = -EINVAL; |
John Muir | 3b463ae | 2009-05-31 11:13:57 -0400 | [diff] [blame] | 1444 | if (size < sizeof(outarg)) |
| 1445 | goto err; |
| 1446 | |
| 1447 | err = fuse_copy_one(cs, &outarg, sizeof(outarg)); |
| 1448 | if (err) |
| 1449 | goto err; |
| 1450 | |
| 1451 | err = -ENAMETOOLONG; |
| 1452 | if (outarg.namelen > FUSE_NAME_MAX) |
| 1453 | goto err; |
| 1454 | |
Miklos Szeredi | c2183d1 | 2011-08-24 10:20:17 +0200 | [diff] [blame] | 1455 | err = -EINVAL; |
| 1456 | if (size != sizeof(outarg) + outarg.namelen + 1) |
| 1457 | goto err; |
| 1458 | |
John Muir | 3b463ae | 2009-05-31 11:13:57 -0400 | [diff] [blame] | 1459 | name.name = buf; |
| 1460 | name.len = outarg.namelen; |
| 1461 | err = fuse_copy_one(cs, buf, outarg.namelen + 1); |
| 1462 | if (err) |
| 1463 | goto err; |
| 1464 | fuse_copy_finish(cs); |
| 1465 | buf[outarg.namelen] = 0; |
John Muir | 3b463ae | 2009-05-31 11:13:57 -0400 | [diff] [blame] | 1466 | |
| 1467 | down_read(&fc->killsb); |
| 1468 | err = -ENOENT; |
Miklos Szeredi | b21dda4 | 2010-02-05 12:08:31 +0100 | [diff] [blame] | 1469 | if (fc->sb) |
John Muir | 451d0f5 | 2011-12-06 21:50:06 +0100 | [diff] [blame] | 1470 | err = fuse_reverse_inval_entry(fc->sb, outarg.parent, 0, &name); |
| 1471 | up_read(&fc->killsb); |
| 1472 | kfree(buf); |
| 1473 | return err; |
| 1474 | |
| 1475 | err: |
| 1476 | kfree(buf); |
| 1477 | fuse_copy_finish(cs); |
| 1478 | return err; |
| 1479 | } |
| 1480 | |
| 1481 | static int fuse_notify_delete(struct fuse_conn *fc, unsigned int size, |
| 1482 | struct fuse_copy_state *cs) |
| 1483 | { |
| 1484 | struct fuse_notify_delete_out outarg; |
| 1485 | int err = -ENOMEM; |
| 1486 | char *buf; |
| 1487 | struct qstr name; |
| 1488 | |
| 1489 | buf = kzalloc(FUSE_NAME_MAX + 1, GFP_KERNEL); |
| 1490 | if (!buf) |
| 1491 | goto err; |
| 1492 | |
| 1493 | err = -EINVAL; |
| 1494 | if (size < sizeof(outarg)) |
| 1495 | goto err; |
| 1496 | |
| 1497 | err = fuse_copy_one(cs, &outarg, sizeof(outarg)); |
| 1498 | if (err) |
| 1499 | goto err; |
| 1500 | |
| 1501 | err = -ENAMETOOLONG; |
| 1502 | if (outarg.namelen > FUSE_NAME_MAX) |
| 1503 | goto err; |
| 1504 | |
| 1505 | err = -EINVAL; |
| 1506 | if (size != sizeof(outarg) + outarg.namelen + 1) |
| 1507 | goto err; |
| 1508 | |
| 1509 | name.name = buf; |
| 1510 | name.len = outarg.namelen; |
| 1511 | err = fuse_copy_one(cs, buf, outarg.namelen + 1); |
| 1512 | if (err) |
| 1513 | goto err; |
| 1514 | fuse_copy_finish(cs); |
| 1515 | buf[outarg.namelen] = 0; |
John Muir | 451d0f5 | 2011-12-06 21:50:06 +0100 | [diff] [blame] | 1516 | |
| 1517 | down_read(&fc->killsb); |
| 1518 | err = -ENOENT; |
| 1519 | if (fc->sb) |
| 1520 | err = fuse_reverse_inval_entry(fc->sb, outarg.parent, |
| 1521 | outarg.child, &name); |
John Muir | 3b463ae | 2009-05-31 11:13:57 -0400 | [diff] [blame] | 1522 | up_read(&fc->killsb); |
Fang Wenqi | b2d82ee | 2009-12-30 18:37:13 +0800 | [diff] [blame] | 1523 | kfree(buf); |
John Muir | 3b463ae | 2009-05-31 11:13:57 -0400 | [diff] [blame] | 1524 | return err; |
| 1525 | |
| 1526 | err: |
Fang Wenqi | b2d82ee | 2009-12-30 18:37:13 +0800 | [diff] [blame] | 1527 | kfree(buf); |
John Muir | 3b463ae | 2009-05-31 11:13:57 -0400 | [diff] [blame] | 1528 | fuse_copy_finish(cs); |
| 1529 | return err; |
| 1530 | } |
| 1531 | |
Miklos Szeredi | a1d75f2 | 2010-07-12 14:41:40 +0200 | [diff] [blame] | 1532 | static int fuse_notify_store(struct fuse_conn *fc, unsigned int size, |
| 1533 | struct fuse_copy_state *cs) |
| 1534 | { |
| 1535 | struct fuse_notify_store_out outarg; |
| 1536 | struct inode *inode; |
| 1537 | struct address_space *mapping; |
| 1538 | u64 nodeid; |
| 1539 | int err; |
| 1540 | pgoff_t index; |
| 1541 | unsigned int offset; |
| 1542 | unsigned int num; |
| 1543 | loff_t file_size; |
| 1544 | loff_t end; |
| 1545 | |
| 1546 | err = -EINVAL; |
| 1547 | if (size < sizeof(outarg)) |
| 1548 | goto out_finish; |
| 1549 | |
| 1550 | err = fuse_copy_one(cs, &outarg, sizeof(outarg)); |
| 1551 | if (err) |
| 1552 | goto out_finish; |
| 1553 | |
| 1554 | err = -EINVAL; |
| 1555 | if (size - sizeof(outarg) != outarg.size) |
| 1556 | goto out_finish; |
| 1557 | |
| 1558 | nodeid = outarg.nodeid; |
| 1559 | |
| 1560 | down_read(&fc->killsb); |
| 1561 | |
| 1562 | err = -ENOENT; |
| 1563 | if (!fc->sb) |
| 1564 | goto out_up_killsb; |
| 1565 | |
| 1566 | inode = ilookup5(fc->sb, nodeid, fuse_inode_eq, &nodeid); |
| 1567 | if (!inode) |
| 1568 | goto out_up_killsb; |
| 1569 | |
| 1570 | mapping = inode->i_mapping; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1571 | index = outarg.offset >> PAGE_SHIFT; |
| 1572 | offset = outarg.offset & ~PAGE_MASK; |
Miklos Szeredi | a1d75f2 | 2010-07-12 14:41:40 +0200 | [diff] [blame] | 1573 | file_size = i_size_read(inode); |
| 1574 | end = outarg.offset + outarg.size; |
| 1575 | if (end > file_size) { |
| 1576 | file_size = end; |
| 1577 | fuse_write_update_size(inode, file_size); |
| 1578 | } |
| 1579 | |
| 1580 | num = outarg.size; |
| 1581 | while (num) { |
| 1582 | struct page *page; |
| 1583 | unsigned int this_num; |
| 1584 | |
| 1585 | err = -ENOMEM; |
| 1586 | page = find_or_create_page(mapping, index, |
| 1587 | mapping_gfp_mask(mapping)); |
| 1588 | if (!page) |
| 1589 | goto out_iput; |
| 1590 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1591 | this_num = min_t(unsigned, num, PAGE_SIZE - offset); |
Miklos Szeredi | a1d75f2 | 2010-07-12 14:41:40 +0200 | [diff] [blame] | 1592 | err = fuse_copy_page(cs, &page, offset, this_num, 0); |
Miklos Szeredi | 063ec1e | 2014-01-22 19:36:58 +0100 | [diff] [blame] | 1593 | if (!err && offset == 0 && |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1594 | (this_num == PAGE_SIZE || file_size == end)) |
Miklos Szeredi | a1d75f2 | 2010-07-12 14:41:40 +0200 | [diff] [blame] | 1595 | SetPageUptodate(page); |
| 1596 | unlock_page(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1597 | put_page(page); |
Miklos Szeredi | a1d75f2 | 2010-07-12 14:41:40 +0200 | [diff] [blame] | 1598 | |
| 1599 | if (err) |
| 1600 | goto out_iput; |
| 1601 | |
| 1602 | num -= this_num; |
| 1603 | offset = 0; |
| 1604 | index++; |
| 1605 | } |
| 1606 | |
| 1607 | err = 0; |
| 1608 | |
| 1609 | out_iput: |
| 1610 | iput(inode); |
| 1611 | out_up_killsb: |
| 1612 | up_read(&fc->killsb); |
| 1613 | out_finish: |
| 1614 | fuse_copy_finish(cs); |
| 1615 | return err; |
| 1616 | } |
| 1617 | |
Miklos Szeredi | 75b399d | 2019-09-10 15:04:11 +0200 | [diff] [blame] | 1618 | struct fuse_retrieve_args { |
| 1619 | struct fuse_args_pages ap; |
| 1620 | struct fuse_notify_retrieve_in inarg; |
| 1621 | }; |
| 1622 | |
| 1623 | static void fuse_retrieve_end(struct fuse_conn *fc, struct fuse_args *args, |
| 1624 | int error) |
Miklos Szeredi | 2d45ba3 | 2010-07-12 14:41:40 +0200 | [diff] [blame] | 1625 | { |
Miklos Szeredi | 75b399d | 2019-09-10 15:04:11 +0200 | [diff] [blame] | 1626 | struct fuse_retrieve_args *ra = |
| 1627 | container_of(args, typeof(*ra), ap.args); |
| 1628 | |
| 1629 | release_pages(ra->ap.pages, ra->ap.num_pages); |
| 1630 | kfree(ra); |
Miklos Szeredi | 2d45ba3 | 2010-07-12 14:41:40 +0200 | [diff] [blame] | 1631 | } |
| 1632 | |
| 1633 | static int fuse_retrieve(struct fuse_conn *fc, struct inode *inode, |
| 1634 | struct fuse_notify_retrieve_out *outarg) |
| 1635 | { |
| 1636 | int err; |
| 1637 | struct address_space *mapping = inode->i_mapping; |
Miklos Szeredi | 2d45ba3 | 2010-07-12 14:41:40 +0200 | [diff] [blame] | 1638 | pgoff_t index; |
| 1639 | loff_t file_size; |
| 1640 | unsigned int num; |
| 1641 | unsigned int offset; |
Geert Uytterhoeven | 0157443 | 2010-09-30 22:06:21 +0200 | [diff] [blame] | 1642 | size_t total_len = 0; |
Constantine Shulyupin | 5da784c | 2018-09-06 15:37:06 +0300 | [diff] [blame] | 1643 | unsigned int num_pages; |
Miklos Szeredi | 75b399d | 2019-09-10 15:04:11 +0200 | [diff] [blame] | 1644 | struct fuse_retrieve_args *ra; |
| 1645 | size_t args_size = sizeof(*ra); |
| 1646 | struct fuse_args_pages *ap; |
| 1647 | struct fuse_args *args; |
Miklos Szeredi | 2d45ba3 | 2010-07-12 14:41:40 +0200 | [diff] [blame] | 1648 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1649 | offset = outarg->offset & ~PAGE_MASK; |
Maxim Patlasov | 4d53dc9 | 2012-10-26 19:48:42 +0400 | [diff] [blame] | 1650 | file_size = i_size_read(inode); |
| 1651 | |
Kirill Smelkov | 7640682 | 2019-03-27 10:15:19 +0000 | [diff] [blame] | 1652 | num = min(outarg->size, fc->max_write); |
Maxim Patlasov | 4d53dc9 | 2012-10-26 19:48:42 +0400 | [diff] [blame] | 1653 | if (outarg->offset > file_size) |
| 1654 | num = 0; |
| 1655 | else if (outarg->offset + num > file_size) |
| 1656 | num = file_size - outarg->offset; |
| 1657 | |
| 1658 | num_pages = (num + offset + PAGE_SIZE - 1) >> PAGE_SHIFT; |
Constantine Shulyupin | 5da784c | 2018-09-06 15:37:06 +0300 | [diff] [blame] | 1659 | num_pages = min(num_pages, fc->max_pages); |
Maxim Patlasov | 4d53dc9 | 2012-10-26 19:48:42 +0400 | [diff] [blame] | 1660 | |
Miklos Szeredi | 75b399d | 2019-09-10 15:04:11 +0200 | [diff] [blame] | 1661 | args_size += num_pages * (sizeof(ap->pages[0]) + sizeof(ap->descs[0])); |
Miklos Szeredi | 2d45ba3 | 2010-07-12 14:41:40 +0200 | [diff] [blame] | 1662 | |
Miklos Szeredi | 75b399d | 2019-09-10 15:04:11 +0200 | [diff] [blame] | 1663 | ra = kzalloc(args_size, GFP_KERNEL); |
| 1664 | if (!ra) |
| 1665 | return -ENOMEM; |
| 1666 | |
| 1667 | ap = &ra->ap; |
| 1668 | ap->pages = (void *) (ra + 1); |
| 1669 | ap->descs = (void *) (ap->pages + num_pages); |
| 1670 | |
| 1671 | args = &ap->args; |
| 1672 | args->nodeid = outarg->nodeid; |
| 1673 | args->opcode = FUSE_NOTIFY_REPLY; |
| 1674 | args->in_numargs = 2; |
| 1675 | args->in_pages = true; |
| 1676 | args->end = fuse_retrieve_end; |
Miklos Szeredi | 2d45ba3 | 2010-07-12 14:41:40 +0200 | [diff] [blame] | 1677 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1678 | index = outarg->offset >> PAGE_SHIFT; |
Miklos Szeredi | 2d45ba3 | 2010-07-12 14:41:40 +0200 | [diff] [blame] | 1679 | |
Miklos Szeredi | 75b399d | 2019-09-10 15:04:11 +0200 | [diff] [blame] | 1680 | while (num && ap->num_pages < num_pages) { |
Miklos Szeredi | 2d45ba3 | 2010-07-12 14:41:40 +0200 | [diff] [blame] | 1681 | struct page *page; |
| 1682 | unsigned int this_num; |
| 1683 | |
| 1684 | page = find_get_page(mapping, index); |
| 1685 | if (!page) |
| 1686 | break; |
| 1687 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1688 | this_num = min_t(unsigned, num, PAGE_SIZE - offset); |
Miklos Szeredi | 75b399d | 2019-09-10 15:04:11 +0200 | [diff] [blame] | 1689 | ap->pages[ap->num_pages] = page; |
| 1690 | ap->descs[ap->num_pages].offset = offset; |
| 1691 | ap->descs[ap->num_pages].length = this_num; |
| 1692 | ap->num_pages++; |
Miklos Szeredi | 2d45ba3 | 2010-07-12 14:41:40 +0200 | [diff] [blame] | 1693 | |
Miklos Szeredi | c9e67d4 | 2012-09-04 18:45:54 +0200 | [diff] [blame] | 1694 | offset = 0; |
Miklos Szeredi | 2d45ba3 | 2010-07-12 14:41:40 +0200 | [diff] [blame] | 1695 | num -= this_num; |
| 1696 | total_len += this_num; |
Miklos Szeredi | 48706d0 | 2011-12-13 10:36:59 +0100 | [diff] [blame] | 1697 | index++; |
Miklos Szeredi | 2d45ba3 | 2010-07-12 14:41:40 +0200 | [diff] [blame] | 1698 | } |
Miklos Szeredi | 75b399d | 2019-09-10 15:04:11 +0200 | [diff] [blame] | 1699 | ra->inarg.offset = outarg->offset; |
| 1700 | ra->inarg.size = total_len; |
| 1701 | args->in_args[0].size = sizeof(ra->inarg); |
| 1702 | args->in_args[0].value = &ra->inarg; |
| 1703 | args->in_args[1].size = total_len; |
Miklos Szeredi | 2d45ba3 | 2010-07-12 14:41:40 +0200 | [diff] [blame] | 1704 | |
Miklos Szeredi | 75b399d | 2019-09-10 15:04:11 +0200 | [diff] [blame] | 1705 | err = fuse_simple_notify_reply(fc, args, outarg->notify_unique); |
| 1706 | if (err) |
| 1707 | fuse_retrieve_end(fc, args, err); |
Miklos Szeredi | 2d45ba3 | 2010-07-12 14:41:40 +0200 | [diff] [blame] | 1708 | |
| 1709 | return err; |
| 1710 | } |
| 1711 | |
| 1712 | static int fuse_notify_retrieve(struct fuse_conn *fc, unsigned int size, |
| 1713 | struct fuse_copy_state *cs) |
| 1714 | { |
| 1715 | struct fuse_notify_retrieve_out outarg; |
| 1716 | struct inode *inode; |
| 1717 | int err; |
| 1718 | |
| 1719 | err = -EINVAL; |
| 1720 | if (size != sizeof(outarg)) |
| 1721 | goto copy_finish; |
| 1722 | |
| 1723 | err = fuse_copy_one(cs, &outarg, sizeof(outarg)); |
| 1724 | if (err) |
| 1725 | goto copy_finish; |
| 1726 | |
| 1727 | fuse_copy_finish(cs); |
| 1728 | |
| 1729 | down_read(&fc->killsb); |
| 1730 | err = -ENOENT; |
| 1731 | if (fc->sb) { |
| 1732 | u64 nodeid = outarg.nodeid; |
| 1733 | |
| 1734 | inode = ilookup5(fc->sb, nodeid, fuse_inode_eq, &nodeid); |
| 1735 | if (inode) { |
| 1736 | err = fuse_retrieve(fc, inode, &outarg); |
| 1737 | iput(inode); |
| 1738 | } |
| 1739 | } |
| 1740 | up_read(&fc->killsb); |
| 1741 | |
| 1742 | return err; |
| 1743 | |
| 1744 | copy_finish: |
| 1745 | fuse_copy_finish(cs); |
| 1746 | return err; |
| 1747 | } |
| 1748 | |
Tejun Heo | 8599396 | 2008-11-26 12:03:55 +0100 | [diff] [blame] | 1749 | static int fuse_notify(struct fuse_conn *fc, enum fuse_notify_code code, |
| 1750 | unsigned int size, struct fuse_copy_state *cs) |
| 1751 | { |
Miklos Szeredi | 0d27836 | 2015-02-26 11:45:47 +0100 | [diff] [blame] | 1752 | /* Don't try to move pages (yet) */ |
| 1753 | cs->move_pages = 0; |
| 1754 | |
Tejun Heo | 8599396 | 2008-11-26 12:03:55 +0100 | [diff] [blame] | 1755 | switch (code) { |
Tejun Heo | 95668a6 | 2008-11-26 12:03:55 +0100 | [diff] [blame] | 1756 | case FUSE_NOTIFY_POLL: |
| 1757 | return fuse_notify_poll(fc, size, cs); |
| 1758 | |
John Muir | 3b463ae | 2009-05-31 11:13:57 -0400 | [diff] [blame] | 1759 | case FUSE_NOTIFY_INVAL_INODE: |
| 1760 | return fuse_notify_inval_inode(fc, size, cs); |
| 1761 | |
| 1762 | case FUSE_NOTIFY_INVAL_ENTRY: |
| 1763 | return fuse_notify_inval_entry(fc, size, cs); |
| 1764 | |
Miklos Szeredi | a1d75f2 | 2010-07-12 14:41:40 +0200 | [diff] [blame] | 1765 | case FUSE_NOTIFY_STORE: |
| 1766 | return fuse_notify_store(fc, size, cs); |
| 1767 | |
Miklos Szeredi | 2d45ba3 | 2010-07-12 14:41:40 +0200 | [diff] [blame] | 1768 | case FUSE_NOTIFY_RETRIEVE: |
| 1769 | return fuse_notify_retrieve(fc, size, cs); |
| 1770 | |
John Muir | 451d0f5 | 2011-12-06 21:50:06 +0100 | [diff] [blame] | 1771 | case FUSE_NOTIFY_DELETE: |
| 1772 | return fuse_notify_delete(fc, size, cs); |
| 1773 | |
Tejun Heo | 8599396 | 2008-11-26 12:03:55 +0100 | [diff] [blame] | 1774 | default: |
Miklos Szeredi | f6d47a1 | 2009-01-26 15:00:59 +0100 | [diff] [blame] | 1775 | fuse_copy_finish(cs); |
Tejun Heo | 8599396 | 2008-11-26 12:03:55 +0100 | [diff] [blame] | 1776 | return -EINVAL; |
| 1777 | } |
| 1778 | } |
| 1779 | |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 1780 | /* Look up request on processing list by unique ID */ |
Miklos Szeredi | 3a2b5b9 | 2015-07-01 16:26:04 +0200 | [diff] [blame] | 1781 | static struct fuse_req *request_find(struct fuse_pqueue *fpq, u64 unique) |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 1782 | { |
Kirill Tkhai | be2ff42 | 2018-09-11 13:12:14 +0300 | [diff] [blame] | 1783 | unsigned int hash = fuse_req_hash(unique); |
Dong Fang | 05726ac | 2013-07-30 22:50:01 -0400 | [diff] [blame] | 1784 | struct fuse_req *req; |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 1785 | |
Kirill Tkhai | be2ff42 | 2018-09-11 13:12:14 +0300 | [diff] [blame] | 1786 | list_for_each_entry(req, &fpq->processing[hash], list) { |
Kirill Tkhai | 3a5358d | 2018-09-11 13:12:05 +0300 | [diff] [blame] | 1787 | if (req->in.h.unique == unique) |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 1788 | return req; |
| 1789 | } |
| 1790 | return NULL; |
| 1791 | } |
| 1792 | |
Miklos Szeredi | d499377 | 2019-09-10 15:04:11 +0200 | [diff] [blame] | 1793 | static int copy_out_args(struct fuse_copy_state *cs, struct fuse_args *args, |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 1794 | unsigned nbytes) |
| 1795 | { |
| 1796 | unsigned reqsize = sizeof(struct fuse_out_header); |
| 1797 | |
Stefan Hajnoczi | 14d46d7 | 2018-06-21 09:34:25 +0100 | [diff] [blame] | 1798 | reqsize += fuse_len_args(args->out_numargs, args->out_args); |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 1799 | |
Miklos Szeredi | d499377 | 2019-09-10 15:04:11 +0200 | [diff] [blame] | 1800 | if (reqsize < nbytes || (reqsize > nbytes && !args->out_argvar)) |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 1801 | return -EINVAL; |
| 1802 | else if (reqsize > nbytes) { |
Miklos Szeredi | d499377 | 2019-09-10 15:04:11 +0200 | [diff] [blame] | 1803 | struct fuse_arg *lastarg = &args->out_args[args->out_numargs-1]; |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 1804 | unsigned diffsize = reqsize - nbytes; |
Miklos Szeredi | d499377 | 2019-09-10 15:04:11 +0200 | [diff] [blame] | 1805 | |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 1806 | if (diffsize > lastarg->size) |
| 1807 | return -EINVAL; |
| 1808 | lastarg->size -= diffsize; |
| 1809 | } |
Miklos Szeredi | d499377 | 2019-09-10 15:04:11 +0200 | [diff] [blame] | 1810 | return fuse_copy_args(cs, args->out_numargs, args->out_pages, |
| 1811 | args->out_args, args->page_zeroing); |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 1812 | } |
| 1813 | |
| 1814 | /* |
| 1815 | * Write a single reply to a request. First the header is copied from |
| 1816 | * the write buffer. The request is then searched on the processing |
| 1817 | * list by the unique ID found in the header. If found, then remove |
| 1818 | * it from the list and copy the rest of the buffer to the request. |
Stefan Hajnoczi | 04ec5af | 2018-06-21 09:33:40 +0100 | [diff] [blame] | 1819 | * The request is finished by calling fuse_request_end(). |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 1820 | */ |
Miklos Szeredi | c3696046 | 2015-07-01 16:26:09 +0200 | [diff] [blame] | 1821 | static ssize_t fuse_dev_do_write(struct fuse_dev *fud, |
Miklos Szeredi | dd3bb14 | 2010-05-25 15:06:06 +0200 | [diff] [blame] | 1822 | struct fuse_copy_state *cs, size_t nbytes) |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 1823 | { |
| 1824 | int err; |
Miklos Szeredi | c3696046 | 2015-07-01 16:26:09 +0200 | [diff] [blame] | 1825 | struct fuse_conn *fc = fud->fc; |
| 1826 | struct fuse_pqueue *fpq = &fud->pq; |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 1827 | struct fuse_req *req; |
| 1828 | struct fuse_out_header oh; |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 1829 | |
Kirill Tkhai | 7407a10 | 2018-11-08 12:05:36 +0300 | [diff] [blame] | 1830 | err = -EINVAL; |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 1831 | if (nbytes < sizeof(struct fuse_out_header)) |
Kirill Tkhai | 7407a10 | 2018-11-08 12:05:36 +0300 | [diff] [blame] | 1832 | goto out; |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 1833 | |
Miklos Szeredi | dd3bb14 | 2010-05-25 15:06:06 +0200 | [diff] [blame] | 1834 | err = fuse_copy_one(cs, &oh, sizeof(oh)); |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 1835 | if (err) |
Kirill Tkhai | 7407a10 | 2018-11-08 12:05:36 +0300 | [diff] [blame] | 1836 | goto copy_finish; |
Tejun Heo | 8599396 | 2008-11-26 12:03:55 +0100 | [diff] [blame] | 1837 | |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 1838 | err = -EINVAL; |
Tejun Heo | 8599396 | 2008-11-26 12:03:55 +0100 | [diff] [blame] | 1839 | if (oh.len != nbytes) |
Kirill Tkhai | 7407a10 | 2018-11-08 12:05:36 +0300 | [diff] [blame] | 1840 | goto copy_finish; |
Tejun Heo | 8599396 | 2008-11-26 12:03:55 +0100 | [diff] [blame] | 1841 | |
| 1842 | /* |
| 1843 | * Zero oh.unique indicates unsolicited notification message |
| 1844 | * and error contains notification code. |
| 1845 | */ |
| 1846 | if (!oh.unique) { |
Miklos Szeredi | dd3bb14 | 2010-05-25 15:06:06 +0200 | [diff] [blame] | 1847 | err = fuse_notify(fc, oh.error, nbytes - sizeof(oh), cs); |
Kirill Tkhai | 7407a10 | 2018-11-08 12:05:36 +0300 | [diff] [blame] | 1848 | goto out; |
Tejun Heo | 8599396 | 2008-11-26 12:03:55 +0100 | [diff] [blame] | 1849 | } |
| 1850 | |
| 1851 | err = -EINVAL; |
| 1852 | if (oh.error <= -1000 || oh.error > 0) |
Kirill Tkhai | 7407a10 | 2018-11-08 12:05:36 +0300 | [diff] [blame] | 1853 | goto copy_finish; |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 1854 | |
Miklos Szeredi | 45a91cb | 2015-07-01 16:26:06 +0200 | [diff] [blame] | 1855 | spin_lock(&fpq->lock); |
Kirill Tkhai | 7407a10 | 2018-11-08 12:05:36 +0300 | [diff] [blame] | 1856 | req = NULL; |
| 1857 | if (fpq->connected) |
| 1858 | req = request_find(fpq, oh.unique & ~FUSE_INT_REQ_BIT); |
Miklos Szeredi | 69a53bf | 2006-01-16 22:14:41 -0800 | [diff] [blame] | 1859 | |
Kirill Tkhai | 7407a10 | 2018-11-08 12:05:36 +0300 | [diff] [blame] | 1860 | err = -ENOENT; |
| 1861 | if (!req) { |
| 1862 | spin_unlock(&fpq->lock); |
| 1863 | goto copy_finish; |
| 1864 | } |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 1865 | |
Kirill Tkhai | 3a5358d | 2018-09-11 13:12:05 +0300 | [diff] [blame] | 1866 | /* Is it an interrupt reply ID? */ |
| 1867 | if (oh.unique & FUSE_INT_REQ_BIT) { |
Kirill Tkhai | d2d2d4f | 2018-09-25 12:52:42 +0300 | [diff] [blame] | 1868 | __fuse_get_request(req); |
Miklos Szeredi | 45a91cb | 2015-07-01 16:26:06 +0200 | [diff] [blame] | 1869 | spin_unlock(&fpq->lock); |
| 1870 | |
Kirill Tkhai | 7407a10 | 2018-11-08 12:05:36 +0300 | [diff] [blame] | 1871 | err = 0; |
| 1872 | if (nbytes != sizeof(struct fuse_out_header)) |
| 1873 | err = -EINVAL; |
| 1874 | else if (oh.error == -ENOSYS) |
Miklos Szeredi | a4d27e7 | 2006-06-25 05:48:54 -0700 | [diff] [blame] | 1875 | fc->no_interrupt = 1; |
| 1876 | else if (oh.error == -EAGAIN) |
Kirill Tkhai | b782911 | 2018-11-08 12:05:42 +0300 | [diff] [blame] | 1877 | err = queue_interrupt(&fc->iq, req); |
Kirill Tkhai | 7407a10 | 2018-11-08 12:05:36 +0300 | [diff] [blame] | 1878 | |
Kirill Tkhai | d2d2d4f | 2018-09-25 12:52:42 +0300 | [diff] [blame] | 1879 | fuse_put_request(fc, req); |
Miklos Szeredi | a4d27e7 | 2006-06-25 05:48:54 -0700 | [diff] [blame] | 1880 | |
Kirill Tkhai | 7407a10 | 2018-11-08 12:05:36 +0300 | [diff] [blame] | 1881 | goto copy_finish; |
Miklos Szeredi | a4d27e7 | 2006-06-25 05:48:54 -0700 | [diff] [blame] | 1882 | } |
| 1883 | |
Miklos Szeredi | 33e14b4 | 2015-07-01 16:26:01 +0200 | [diff] [blame] | 1884 | clear_bit(FR_SENT, &req->flags); |
Miklos Szeredi | 3a2b5b9 | 2015-07-01 16:26:04 +0200 | [diff] [blame] | 1885 | list_move(&req->list, &fpq->io); |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 1886 | req->out.h = oh; |
Miklos Szeredi | 825d6d3 | 2015-07-01 16:25:58 +0200 | [diff] [blame] | 1887 | set_bit(FR_LOCKED, &req->flags); |
Miklos Szeredi | 45a91cb | 2015-07-01 16:26:06 +0200 | [diff] [blame] | 1888 | spin_unlock(&fpq->lock); |
Miklos Szeredi | dd3bb14 | 2010-05-25 15:06:06 +0200 | [diff] [blame] | 1889 | cs->req = req; |
Miklos Szeredi | d499377 | 2019-09-10 15:04:11 +0200 | [diff] [blame] | 1890 | if (!req->args->page_replace) |
Miklos Szeredi | ce534fb | 2010-05-25 15:06:07 +0200 | [diff] [blame] | 1891 | cs->move_pages = 0; |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 1892 | |
Miklos Szeredi | d499377 | 2019-09-10 15:04:11 +0200 | [diff] [blame] | 1893 | if (oh.error) |
| 1894 | err = nbytes != sizeof(oh) ? -EINVAL : 0; |
| 1895 | else |
| 1896 | err = copy_out_args(cs, req->args, nbytes); |
Miklos Szeredi | dd3bb14 | 2010-05-25 15:06:06 +0200 | [diff] [blame] | 1897 | fuse_copy_finish(cs); |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 1898 | |
Miklos Szeredi | 45a91cb | 2015-07-01 16:26:06 +0200 | [diff] [blame] | 1899 | spin_lock(&fpq->lock); |
Miklos Szeredi | 825d6d3 | 2015-07-01 16:25:58 +0200 | [diff] [blame] | 1900 | clear_bit(FR_LOCKED, &req->flags); |
Miklos Szeredi | e96edd9 | 2015-07-01 16:26:04 +0200 | [diff] [blame] | 1901 | if (!fpq->connected) |
Miklos Szeredi | 0d8e84b | 2015-07-01 16:25:58 +0200 | [diff] [blame] | 1902 | err = -ENOENT; |
| 1903 | else if (err) |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 1904 | req->out.h.error = -EIO; |
Miklos Szeredi | 77cd9d4 | 2015-07-01 16:26:06 +0200 | [diff] [blame] | 1905 | if (!test_bit(FR_PRIVATE, &req->flags)) |
| 1906 | list_del_init(&req->list); |
Miklos Szeredi | 45a91cb | 2015-07-01 16:26:06 +0200 | [diff] [blame] | 1907 | spin_unlock(&fpq->lock); |
Miklos Szeredi | 46c34a3 | 2015-07-01 16:26:07 +0200 | [diff] [blame] | 1908 | |
Stefan Hajnoczi | 04ec5af | 2018-06-21 09:33:40 +0100 | [diff] [blame] | 1909 | fuse_request_end(fc, req); |
Kirill Tkhai | 7407a10 | 2018-11-08 12:05:36 +0300 | [diff] [blame] | 1910 | out: |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 1911 | return err ? err : nbytes; |
| 1912 | |
Kirill Tkhai | 7407a10 | 2018-11-08 12:05:36 +0300 | [diff] [blame] | 1913 | copy_finish: |
Miklos Szeredi | dd3bb14 | 2010-05-25 15:06:06 +0200 | [diff] [blame] | 1914 | fuse_copy_finish(cs); |
Kirill Tkhai | 7407a10 | 2018-11-08 12:05:36 +0300 | [diff] [blame] | 1915 | goto out; |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 1916 | } |
| 1917 | |
Al Viro | fbdbacc | 2015-04-03 21:53:39 -0400 | [diff] [blame] | 1918 | static ssize_t fuse_dev_write(struct kiocb *iocb, struct iov_iter *from) |
Miklos Szeredi | dd3bb14 | 2010-05-25 15:06:06 +0200 | [diff] [blame] | 1919 | { |
| 1920 | struct fuse_copy_state cs; |
Miklos Szeredi | cc080e9 | 2015-07-01 16:26:08 +0200 | [diff] [blame] | 1921 | struct fuse_dev *fud = fuse_get_dev(iocb->ki_filp); |
| 1922 | |
| 1923 | if (!fud) |
Miklos Szeredi | dd3bb14 | 2010-05-25 15:06:06 +0200 | [diff] [blame] | 1924 | return -EPERM; |
| 1925 | |
Al Viro | fbdbacc | 2015-04-03 21:53:39 -0400 | [diff] [blame] | 1926 | if (!iter_is_iovec(from)) |
| 1927 | return -EINVAL; |
Miklos Szeredi | dd3bb14 | 2010-05-25 15:06:06 +0200 | [diff] [blame] | 1928 | |
Miklos Szeredi | dc00809 | 2015-07-01 16:25:58 +0200 | [diff] [blame] | 1929 | fuse_copy_init(&cs, 0, from); |
Al Viro | fbdbacc | 2015-04-03 21:53:39 -0400 | [diff] [blame] | 1930 | |
Miklos Szeredi | c3696046 | 2015-07-01 16:26:09 +0200 | [diff] [blame] | 1931 | return fuse_dev_do_write(fud, &cs, iov_iter_count(from)); |
Miklos Szeredi | dd3bb14 | 2010-05-25 15:06:06 +0200 | [diff] [blame] | 1932 | } |
| 1933 | |
| 1934 | static ssize_t fuse_dev_splice_write(struct pipe_inode_info *pipe, |
| 1935 | struct file *out, loff_t *ppos, |
| 1936 | size_t len, unsigned int flags) |
| 1937 | { |
David Howells | 8cefc10 | 2019-11-15 13:30:32 +0000 | [diff] [blame] | 1938 | unsigned int head, tail, mask, count; |
Miklos Szeredi | dd3bb14 | 2010-05-25 15:06:06 +0200 | [diff] [blame] | 1939 | unsigned nbuf; |
| 1940 | unsigned idx; |
| 1941 | struct pipe_buffer *bufs; |
| 1942 | struct fuse_copy_state cs; |
Miklos Szeredi | cc080e9 | 2015-07-01 16:26:08 +0200 | [diff] [blame] | 1943 | struct fuse_dev *fud; |
Miklos Szeredi | dd3bb14 | 2010-05-25 15:06:06 +0200 | [diff] [blame] | 1944 | size_t rem; |
| 1945 | ssize_t ret; |
| 1946 | |
Miklos Szeredi | cc080e9 | 2015-07-01 16:26:08 +0200 | [diff] [blame] | 1947 | fud = fuse_get_dev(out); |
| 1948 | if (!fud) |
Miklos Szeredi | dd3bb14 | 2010-05-25 15:06:06 +0200 | [diff] [blame] | 1949 | return -EPERM; |
| 1950 | |
Andrey Ryabinin | a2477b0 | 2018-07-17 19:00:33 +0300 | [diff] [blame] | 1951 | pipe_lock(pipe); |
| 1952 | |
David Howells | 8cefc10 | 2019-11-15 13:30:32 +0000 | [diff] [blame] | 1953 | head = pipe->head; |
| 1954 | tail = pipe->tail; |
| 1955 | mask = pipe->ring_size - 1; |
| 1956 | count = head - tail; |
| 1957 | |
| 1958 | bufs = kvmalloc_array(count, sizeof(struct pipe_buffer), GFP_KERNEL); |
Andrey Ryabinin | a2477b0 | 2018-07-17 19:00:33 +0300 | [diff] [blame] | 1959 | if (!bufs) { |
| 1960 | pipe_unlock(pipe); |
Miklos Szeredi | dd3bb14 | 2010-05-25 15:06:06 +0200 | [diff] [blame] | 1961 | return -ENOMEM; |
Andrey Ryabinin | a2477b0 | 2018-07-17 19:00:33 +0300 | [diff] [blame] | 1962 | } |
Miklos Szeredi | dd3bb14 | 2010-05-25 15:06:06 +0200 | [diff] [blame] | 1963 | |
Miklos Szeredi | dd3bb14 | 2010-05-25 15:06:06 +0200 | [diff] [blame] | 1964 | nbuf = 0; |
| 1965 | rem = 0; |
David Howells | 8cefc10 | 2019-11-15 13:30:32 +0000 | [diff] [blame] | 1966 | for (idx = tail; idx < head && rem < len; idx++) |
| 1967 | rem += pipe->bufs[idx & mask].len; |
Miklos Szeredi | dd3bb14 | 2010-05-25 15:06:06 +0200 | [diff] [blame] | 1968 | |
| 1969 | ret = -EINVAL; |
Matthew Wilcox | 15fab63 | 2019-04-05 14:02:10 -0700 | [diff] [blame] | 1970 | if (rem < len) |
| 1971 | goto out_free; |
Miklos Szeredi | dd3bb14 | 2010-05-25 15:06:06 +0200 | [diff] [blame] | 1972 | |
| 1973 | rem = len; |
| 1974 | while (rem) { |
| 1975 | struct pipe_buffer *ibuf; |
| 1976 | struct pipe_buffer *obuf; |
| 1977 | |
David Howells | 8cefc10 | 2019-11-15 13:30:32 +0000 | [diff] [blame] | 1978 | BUG_ON(nbuf >= pipe->ring_size); |
| 1979 | BUG_ON(tail == head); |
| 1980 | ibuf = &pipe->bufs[tail & mask]; |
Miklos Szeredi | dd3bb14 | 2010-05-25 15:06:06 +0200 | [diff] [blame] | 1981 | obuf = &bufs[nbuf]; |
| 1982 | |
| 1983 | if (rem >= ibuf->len) { |
| 1984 | *obuf = *ibuf; |
| 1985 | ibuf->ops = NULL; |
David Howells | 8cefc10 | 2019-11-15 13:30:32 +0000 | [diff] [blame] | 1986 | tail++; |
| 1987 | pipe->tail = tail; |
Miklos Szeredi | dd3bb14 | 2010-05-25 15:06:06 +0200 | [diff] [blame] | 1988 | } else { |
Matthew Wilcox | 15fab63 | 2019-04-05 14:02:10 -0700 | [diff] [blame] | 1989 | if (!pipe_buf_get(pipe, ibuf)) |
| 1990 | goto out_free; |
| 1991 | |
Miklos Szeredi | dd3bb14 | 2010-05-25 15:06:06 +0200 | [diff] [blame] | 1992 | *obuf = *ibuf; |
| 1993 | obuf->flags &= ~PIPE_BUF_FLAG_GIFT; |
| 1994 | obuf->len = rem; |
| 1995 | ibuf->offset += obuf->len; |
| 1996 | ibuf->len -= obuf->len; |
| 1997 | } |
| 1998 | nbuf++; |
| 1999 | rem -= obuf->len; |
| 2000 | } |
| 2001 | pipe_unlock(pipe); |
| 2002 | |
Miklos Szeredi | dc00809 | 2015-07-01 16:25:58 +0200 | [diff] [blame] | 2003 | fuse_copy_init(&cs, 0, NULL); |
Miklos Szeredi | dd3bb14 | 2010-05-25 15:06:06 +0200 | [diff] [blame] | 2004 | cs.pipebufs = bufs; |
Al Viro | 6c09e94 | 2015-04-03 22:06:08 -0400 | [diff] [blame] | 2005 | cs.nr_segs = nbuf; |
Miklos Szeredi | dd3bb14 | 2010-05-25 15:06:06 +0200 | [diff] [blame] | 2006 | cs.pipe = pipe; |
| 2007 | |
Miklos Szeredi | ce534fb | 2010-05-25 15:06:07 +0200 | [diff] [blame] | 2008 | if (flags & SPLICE_F_MOVE) |
| 2009 | cs.move_pages = 1; |
| 2010 | |
Miklos Szeredi | c3696046 | 2015-07-01 16:26:09 +0200 | [diff] [blame] | 2011 | ret = fuse_dev_do_write(fud, &cs, len); |
Miklos Szeredi | dd3bb14 | 2010-05-25 15:06:06 +0200 | [diff] [blame] | 2012 | |
Jann Horn | 9509941 | 2019-01-12 02:39:05 +0100 | [diff] [blame] | 2013 | pipe_lock(pipe); |
Matthew Wilcox | 15fab63 | 2019-04-05 14:02:10 -0700 | [diff] [blame] | 2014 | out_free: |
Miklos Szeredi | a779638 | 2016-09-27 10:45:12 +0200 | [diff] [blame] | 2015 | for (idx = 0; idx < nbuf; idx++) |
| 2016 | pipe_buf_release(pipe, &bufs[idx]); |
Jann Horn | 9509941 | 2019-01-12 02:39:05 +0100 | [diff] [blame] | 2017 | pipe_unlock(pipe); |
Miklos Szeredi | a779638 | 2016-09-27 10:45:12 +0200 | [diff] [blame] | 2018 | |
Andrey Ryabinin | d6d931a | 2018-07-17 19:00:34 +0300 | [diff] [blame] | 2019 | kvfree(bufs); |
Miklos Szeredi | dd3bb14 | 2010-05-25 15:06:06 +0200 | [diff] [blame] | 2020 | return ret; |
| 2021 | } |
| 2022 | |
Al Viro | 076ccb7 | 2017-07-03 01:02:18 -0400 | [diff] [blame] | 2023 | static __poll_t fuse_dev_poll(struct file *file, poll_table *wait) |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 2024 | { |
Linus Torvalds | a9a0884 | 2018-02-11 14:34:03 -0800 | [diff] [blame] | 2025 | __poll_t mask = EPOLLOUT | EPOLLWRNORM; |
Miklos Szeredi | f88996a | 2015-07-01 16:26:01 +0200 | [diff] [blame] | 2026 | struct fuse_iqueue *fiq; |
Miklos Szeredi | cc080e9 | 2015-07-01 16:26:08 +0200 | [diff] [blame] | 2027 | struct fuse_dev *fud = fuse_get_dev(file); |
| 2028 | |
| 2029 | if (!fud) |
Linus Torvalds | a9a0884 | 2018-02-11 14:34:03 -0800 | [diff] [blame] | 2030 | return EPOLLERR; |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 2031 | |
Miklos Szeredi | cc080e9 | 2015-07-01 16:26:08 +0200 | [diff] [blame] | 2032 | fiq = &fud->fc->iq; |
Miklos Szeredi | f88996a | 2015-07-01 16:26:01 +0200 | [diff] [blame] | 2033 | poll_wait(file, &fiq->waitq, wait); |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 2034 | |
Eric Biggers | 76e43c8 | 2019-09-08 20:15:18 -0700 | [diff] [blame] | 2035 | spin_lock(&fiq->lock); |
Miklos Szeredi | e16714d | 2015-07-01 16:26:01 +0200 | [diff] [blame] | 2036 | if (!fiq->connected) |
Linus Torvalds | a9a0884 | 2018-02-11 14:34:03 -0800 | [diff] [blame] | 2037 | mask = EPOLLERR; |
Miklos Szeredi | f88996a | 2015-07-01 16:26:01 +0200 | [diff] [blame] | 2038 | else if (request_pending(fiq)) |
Linus Torvalds | a9a0884 | 2018-02-11 14:34:03 -0800 | [diff] [blame] | 2039 | mask |= EPOLLIN | EPOLLRDNORM; |
Eric Biggers | 76e43c8 | 2019-09-08 20:15:18 -0700 | [diff] [blame] | 2040 | spin_unlock(&fiq->lock); |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 2041 | |
| 2042 | return mask; |
| 2043 | } |
| 2044 | |
Kirill Tkhai | 3406175 | 2018-11-06 12:15:20 +0300 | [diff] [blame] | 2045 | /* Abort all requests on the given list (pending or processing) */ |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 2046 | static void end_requests(struct fuse_conn *fc, struct list_head *head) |
| 2047 | { |
| 2048 | while (!list_empty(head)) { |
| 2049 | struct fuse_req *req; |
| 2050 | req = list_entry(head->next, struct fuse_req, list); |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 2051 | req->out.h.error = -ECONNABORTED; |
Miklos Szeredi | 33e14b4 | 2015-07-01 16:26:01 +0200 | [diff] [blame] | 2052 | clear_bit(FR_SENT, &req->flags); |
Miklos Szeredi | f377cb7 | 2015-07-01 16:26:04 +0200 | [diff] [blame] | 2053 | list_del_init(&req->list); |
Stefan Hajnoczi | 04ec5af | 2018-06-21 09:33:40 +0100 | [diff] [blame] | 2054 | fuse_request_end(fc, req); |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 2055 | } |
| 2056 | } |
| 2057 | |
Bryan Green | 357ccf2 | 2011-03-01 16:43:52 -0800 | [diff] [blame] | 2058 | static void end_polls(struct fuse_conn *fc) |
| 2059 | { |
| 2060 | struct rb_node *p; |
| 2061 | |
| 2062 | p = rb_first(&fc->polled_files); |
| 2063 | |
| 2064 | while (p) { |
| 2065 | struct fuse_file *ff; |
| 2066 | ff = rb_entry(p, struct fuse_file, polled_node); |
| 2067 | wake_up_interruptible_all(&ff->poll_wait); |
| 2068 | |
| 2069 | p = rb_next(p); |
| 2070 | } |
| 2071 | } |
| 2072 | |
Miklos Szeredi | 69a53bf | 2006-01-16 22:14:41 -0800 | [diff] [blame] | 2073 | /* |
| 2074 | * Abort all requests. |
| 2075 | * |
Miklos Szeredi | b716d42 | 2015-07-01 16:25:59 +0200 | [diff] [blame] | 2076 | * Emergency exit in case of a malicious or accidental deadlock, or just a hung |
| 2077 | * filesystem. |
Miklos Szeredi | 69a53bf | 2006-01-16 22:14:41 -0800 | [diff] [blame] | 2078 | * |
Miklos Szeredi | b716d42 | 2015-07-01 16:25:59 +0200 | [diff] [blame] | 2079 | * The same effect is usually achievable through killing the filesystem daemon |
| 2080 | * and all users of the filesystem. The exception is the combination of an |
| 2081 | * asynchronous request and the tricky deadlock (see |
| 2082 | * Documentation/filesystems/fuse.txt). |
Miklos Szeredi | 69a53bf | 2006-01-16 22:14:41 -0800 | [diff] [blame] | 2083 | * |
Miklos Szeredi | b716d42 | 2015-07-01 16:25:59 +0200 | [diff] [blame] | 2084 | * Aborting requests under I/O goes as follows: 1: Separate out unlocked |
| 2085 | * requests, they should be finished off immediately. Locked requests will be |
| 2086 | * finished after unlock; see unlock_request(). 2: Finish off the unlocked |
| 2087 | * requests. It is possible that some request will finish before we can. This |
| 2088 | * is OK, the request will in that case be removed from the list before we touch |
| 2089 | * it. |
Miklos Szeredi | 69a53bf | 2006-01-16 22:14:41 -0800 | [diff] [blame] | 2090 | */ |
Miklos Szeredi | eb98e3bd | 2019-01-24 10:40:16 +0100 | [diff] [blame] | 2091 | void fuse_abort_conn(struct fuse_conn *fc) |
Miklos Szeredi | 69a53bf | 2006-01-16 22:14:41 -0800 | [diff] [blame] | 2092 | { |
Miklos Szeredi | f88996a | 2015-07-01 16:26:01 +0200 | [diff] [blame] | 2093 | struct fuse_iqueue *fiq = &fc->iq; |
| 2094 | |
Miklos Szeredi | d713311 | 2006-04-10 22:54:55 -0700 | [diff] [blame] | 2095 | spin_lock(&fc->lock); |
Miklos Szeredi | 69a53bf | 2006-01-16 22:14:41 -0800 | [diff] [blame] | 2096 | if (fc->connected) { |
Miklos Szeredi | c3696046 | 2015-07-01 16:26:09 +0200 | [diff] [blame] | 2097 | struct fuse_dev *fud; |
Miklos Szeredi | b716d42 | 2015-07-01 16:25:59 +0200 | [diff] [blame] | 2098 | struct fuse_req *req, *next; |
Miklos Szeredi | 75f3ee4 | 2018-07-26 16:13:12 +0200 | [diff] [blame] | 2099 | LIST_HEAD(to_end); |
Kirill Tkhai | be2ff42 | 2018-09-11 13:12:14 +0300 | [diff] [blame] | 2100 | unsigned int i; |
Miklos Szeredi | b716d42 | 2015-07-01 16:25:59 +0200 | [diff] [blame] | 2101 | |
Kirill Tkhai | 63825b4 | 2018-08-27 18:29:56 +0300 | [diff] [blame] | 2102 | /* Background queuing checks fc->connected under bg_lock */ |
| 2103 | spin_lock(&fc->bg_lock); |
Miklos Szeredi | 69a53bf | 2006-01-16 22:14:41 -0800 | [diff] [blame] | 2104 | fc->connected = 0; |
Kirill Tkhai | 63825b4 | 2018-08-27 18:29:56 +0300 | [diff] [blame] | 2105 | spin_unlock(&fc->bg_lock); |
| 2106 | |
Miklos Szeredi | 9759bd51 | 2015-01-06 10:45:35 +0100 | [diff] [blame] | 2107 | fuse_set_initialized(fc); |
Miklos Szeredi | c3696046 | 2015-07-01 16:26:09 +0200 | [diff] [blame] | 2108 | list_for_each_entry(fud, &fc->devices, entry) { |
| 2109 | struct fuse_pqueue *fpq = &fud->pq; |
| 2110 | |
| 2111 | spin_lock(&fpq->lock); |
| 2112 | fpq->connected = 0; |
| 2113 | list_for_each_entry_safe(req, next, &fpq->io, list) { |
| 2114 | req->out.h.error = -ECONNABORTED; |
| 2115 | spin_lock(&req->waitq.lock); |
| 2116 | set_bit(FR_ABORTED, &req->flags); |
| 2117 | if (!test_bit(FR_LOCKED, &req->flags)) { |
| 2118 | set_bit(FR_PRIVATE, &req->flags); |
Miklos Szeredi | 8711437 | 2018-07-26 16:13:11 +0200 | [diff] [blame] | 2119 | __fuse_get_request(req); |
Miklos Szeredi | 75f3ee4 | 2018-07-26 16:13:12 +0200 | [diff] [blame] | 2120 | list_move(&req->list, &to_end); |
Miklos Szeredi | c3696046 | 2015-07-01 16:26:09 +0200 | [diff] [blame] | 2121 | } |
| 2122 | spin_unlock(&req->waitq.lock); |
Miklos Szeredi | 77cd9d4 | 2015-07-01 16:26:06 +0200 | [diff] [blame] | 2123 | } |
Kirill Tkhai | be2ff42 | 2018-09-11 13:12:14 +0300 | [diff] [blame] | 2124 | for (i = 0; i < FUSE_PQ_HASH_SIZE; i++) |
| 2125 | list_splice_tail_init(&fpq->processing[i], |
| 2126 | &to_end); |
Miklos Szeredi | c3696046 | 2015-07-01 16:26:09 +0200 | [diff] [blame] | 2127 | spin_unlock(&fpq->lock); |
Miklos Szeredi | b716d42 | 2015-07-01 16:25:59 +0200 | [diff] [blame] | 2128 | } |
Kirill Tkhai | ae2dffa | 2018-08-27 18:29:46 +0300 | [diff] [blame] | 2129 | spin_lock(&fc->bg_lock); |
| 2130 | fc->blocked = 0; |
Miklos Szeredi | 41f9827 | 2015-07-01 16:25:59 +0200 | [diff] [blame] | 2131 | fc->max_background = UINT_MAX; |
| 2132 | flush_bg_queue(fc); |
Kirill Tkhai | ae2dffa | 2018-08-27 18:29:46 +0300 | [diff] [blame] | 2133 | spin_unlock(&fc->bg_lock); |
Miklos Szeredi | 8c91189 | 2015-07-01 16:26:02 +0200 | [diff] [blame] | 2134 | |
Eric Biggers | 76e43c8 | 2019-09-08 20:15:18 -0700 | [diff] [blame] | 2135 | spin_lock(&fiq->lock); |
Miklos Szeredi | 8c91189 | 2015-07-01 16:26:02 +0200 | [diff] [blame] | 2136 | fiq->connected = 0; |
Miklos Szeredi | 75f3ee4 | 2018-07-26 16:13:12 +0200 | [diff] [blame] | 2137 | list_for_each_entry(req, &fiq->pending, list) |
Tahsin Erdogan | a8a86d7 | 2017-01-12 12:04:04 -0800 | [diff] [blame] | 2138 | clear_bit(FR_PENDING, &req->flags); |
Miklos Szeredi | 75f3ee4 | 2018-07-26 16:13:12 +0200 | [diff] [blame] | 2139 | list_splice_tail_init(&fiq->pending, &to_end); |
Miklos Szeredi | 8c91189 | 2015-07-01 16:26:02 +0200 | [diff] [blame] | 2140 | while (forget_pending(fiq)) |
Vivek Goyal | 4388c5a | 2019-06-05 15:50:43 -0400 | [diff] [blame] | 2141 | kfree(fuse_dequeue_forget(fiq, 1, NULL)); |
Eric Biggers | 76e43c8 | 2019-09-08 20:15:18 -0700 | [diff] [blame] | 2142 | wake_up_all(&fiq->waitq); |
| 2143 | spin_unlock(&fiq->lock); |
Miklos Szeredi | 8c91189 | 2015-07-01 16:26:02 +0200 | [diff] [blame] | 2144 | kill_fasync(&fiq->fasync, SIGIO, POLL_IN); |
Miklos Szeredi | ee314a8 | 2015-07-01 16:26:08 +0200 | [diff] [blame] | 2145 | end_polls(fc); |
| 2146 | wake_up_all(&fc->blocked_waitq); |
| 2147 | spin_unlock(&fc->lock); |
Miklos Szeredi | 8c91189 | 2015-07-01 16:26:02 +0200 | [diff] [blame] | 2148 | |
Miklos Szeredi | 75f3ee4 | 2018-07-26 16:13:12 +0200 | [diff] [blame] | 2149 | end_requests(fc, &to_end); |
Miklos Szeredi | ee314a8 | 2015-07-01 16:26:08 +0200 | [diff] [blame] | 2150 | } else { |
| 2151 | spin_unlock(&fc->lock); |
Miklos Szeredi | 69a53bf | 2006-01-16 22:14:41 -0800 | [diff] [blame] | 2152 | } |
Miklos Szeredi | 69a53bf | 2006-01-16 22:14:41 -0800 | [diff] [blame] | 2153 | } |
Tejun Heo | 08cbf54 | 2009-04-14 10:54:53 +0900 | [diff] [blame] | 2154 | EXPORT_SYMBOL_GPL(fuse_abort_conn); |
Miklos Szeredi | 69a53bf | 2006-01-16 22:14:41 -0800 | [diff] [blame] | 2155 | |
Miklos Szeredi | b8f95e5 | 2018-07-26 16:13:11 +0200 | [diff] [blame] | 2156 | void fuse_wait_aborted(struct fuse_conn *fc) |
| 2157 | { |
Miklos Szeredi | 2d84a2d | 2018-11-09 15:52:16 +0100 | [diff] [blame] | 2158 | /* matches implicit memory barrier in fuse_drop_waiting() */ |
| 2159 | smp_mb(); |
Miklos Szeredi | b8f95e5 | 2018-07-26 16:13:11 +0200 | [diff] [blame] | 2160 | wait_event(fc->blocked_waitq, atomic_read(&fc->num_waiting) == 0); |
| 2161 | } |
| 2162 | |
Tejun Heo | 08cbf54 | 2009-04-14 10:54:53 +0900 | [diff] [blame] | 2163 | int fuse_dev_release(struct inode *inode, struct file *file) |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 2164 | { |
Miklos Szeredi | cc080e9 | 2015-07-01 16:26:08 +0200 | [diff] [blame] | 2165 | struct fuse_dev *fud = fuse_get_dev(file); |
| 2166 | |
| 2167 | if (fud) { |
| 2168 | struct fuse_conn *fc = fud->fc; |
Miklos Szeredi | c3696046 | 2015-07-01 16:26:09 +0200 | [diff] [blame] | 2169 | struct fuse_pqueue *fpq = &fud->pq; |
Miklos Szeredi | 45ff350 | 2018-07-26 16:13:11 +0200 | [diff] [blame] | 2170 | LIST_HEAD(to_end); |
Kirill Tkhai | be2ff42 | 2018-09-11 13:12:14 +0300 | [diff] [blame] | 2171 | unsigned int i; |
Miklos Szeredi | cc080e9 | 2015-07-01 16:26:08 +0200 | [diff] [blame] | 2172 | |
Miklos Szeredi | 45ff350 | 2018-07-26 16:13:11 +0200 | [diff] [blame] | 2173 | spin_lock(&fpq->lock); |
Miklos Szeredi | c3696046 | 2015-07-01 16:26:09 +0200 | [diff] [blame] | 2174 | WARN_ON(!list_empty(&fpq->io)); |
Kirill Tkhai | be2ff42 | 2018-09-11 13:12:14 +0300 | [diff] [blame] | 2175 | for (i = 0; i < FUSE_PQ_HASH_SIZE; i++) |
| 2176 | list_splice_init(&fpq->processing[i], &to_end); |
Miklos Szeredi | 45ff350 | 2018-07-26 16:13:11 +0200 | [diff] [blame] | 2177 | spin_unlock(&fpq->lock); |
| 2178 | |
| 2179 | end_requests(fc, &to_end); |
| 2180 | |
Miklos Szeredi | c3696046 | 2015-07-01 16:26:09 +0200 | [diff] [blame] | 2181 | /* Are we the last open device? */ |
| 2182 | if (atomic_dec_and_test(&fc->dev_count)) { |
| 2183 | WARN_ON(fc->iq.fasync != NULL); |
Miklos Szeredi | eb98e3bd | 2019-01-24 10:40:16 +0100 | [diff] [blame] | 2184 | fuse_abort_conn(fc); |
Miklos Szeredi | c3696046 | 2015-07-01 16:26:09 +0200 | [diff] [blame] | 2185 | } |
Miklos Szeredi | cc080e9 | 2015-07-01 16:26:08 +0200 | [diff] [blame] | 2186 | fuse_dev_free(fud); |
Jeff Dike | 385a17b | 2006-04-10 22:54:52 -0700 | [diff] [blame] | 2187 | } |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 2188 | return 0; |
| 2189 | } |
Tejun Heo | 08cbf54 | 2009-04-14 10:54:53 +0900 | [diff] [blame] | 2190 | EXPORT_SYMBOL_GPL(fuse_dev_release); |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 2191 | |
Jeff Dike | 385a17b | 2006-04-10 22:54:52 -0700 | [diff] [blame] | 2192 | static int fuse_dev_fasync(int fd, struct file *file, int on) |
| 2193 | { |
Miklos Szeredi | cc080e9 | 2015-07-01 16:26:08 +0200 | [diff] [blame] | 2194 | struct fuse_dev *fud = fuse_get_dev(file); |
| 2195 | |
| 2196 | if (!fud) |
Miklos Szeredi | a87046d | 2006-04-10 22:54:56 -0700 | [diff] [blame] | 2197 | return -EPERM; |
Jeff Dike | 385a17b | 2006-04-10 22:54:52 -0700 | [diff] [blame] | 2198 | |
| 2199 | /* No locking - fasync_helper does its own locking */ |
Miklos Szeredi | cc080e9 | 2015-07-01 16:26:08 +0200 | [diff] [blame] | 2200 | return fasync_helper(fd, file, on, &fud->fc->iq.fasync); |
Jeff Dike | 385a17b | 2006-04-10 22:54:52 -0700 | [diff] [blame] | 2201 | } |
| 2202 | |
Miklos Szeredi | 00c570f | 2015-07-01 16:26:08 +0200 | [diff] [blame] | 2203 | static int fuse_device_clone(struct fuse_conn *fc, struct file *new) |
| 2204 | { |
Miklos Szeredi | cc080e9 | 2015-07-01 16:26:08 +0200 | [diff] [blame] | 2205 | struct fuse_dev *fud; |
| 2206 | |
Miklos Szeredi | 00c570f | 2015-07-01 16:26:08 +0200 | [diff] [blame] | 2207 | if (new->private_data) |
| 2208 | return -EINVAL; |
| 2209 | |
Vivek Goyal | 0cd1eb9a | 2019-03-06 16:51:40 -0500 | [diff] [blame] | 2210 | fud = fuse_dev_alloc_install(fc); |
Miklos Szeredi | cc080e9 | 2015-07-01 16:26:08 +0200 | [diff] [blame] | 2211 | if (!fud) |
| 2212 | return -ENOMEM; |
| 2213 | |
| 2214 | new->private_data = fud; |
Miklos Szeredi | c3696046 | 2015-07-01 16:26:09 +0200 | [diff] [blame] | 2215 | atomic_inc(&fc->dev_count); |
Miklos Szeredi | 00c570f | 2015-07-01 16:26:08 +0200 | [diff] [blame] | 2216 | |
| 2217 | return 0; |
| 2218 | } |
| 2219 | |
| 2220 | static long fuse_dev_ioctl(struct file *file, unsigned int cmd, |
| 2221 | unsigned long arg) |
| 2222 | { |
| 2223 | int err = -ENOTTY; |
| 2224 | |
| 2225 | if (cmd == FUSE_DEV_IOC_CLONE) { |
| 2226 | int oldfd; |
| 2227 | |
| 2228 | err = -EFAULT; |
| 2229 | if (!get_user(oldfd, (__u32 __user *) arg)) { |
| 2230 | struct file *old = fget(oldfd); |
| 2231 | |
| 2232 | err = -EINVAL; |
| 2233 | if (old) { |
Jann Horn | 8ed1f0e | 2015-08-16 20:27:01 +0200 | [diff] [blame] | 2234 | struct fuse_dev *fud = NULL; |
| 2235 | |
| 2236 | /* |
| 2237 | * Check against file->f_op because CUSE |
| 2238 | * uses the same ioctl handler. |
| 2239 | */ |
| 2240 | if (old->f_op == file->f_op && |
| 2241 | old->f_cred->user_ns == file->f_cred->user_ns) |
| 2242 | fud = fuse_get_dev(old); |
Miklos Szeredi | 00c570f | 2015-07-01 16:26:08 +0200 | [diff] [blame] | 2243 | |
Miklos Szeredi | cc080e9 | 2015-07-01 16:26:08 +0200 | [diff] [blame] | 2244 | if (fud) { |
Miklos Szeredi | 00c570f | 2015-07-01 16:26:08 +0200 | [diff] [blame] | 2245 | mutex_lock(&fuse_mutex); |
Miklos Szeredi | cc080e9 | 2015-07-01 16:26:08 +0200 | [diff] [blame] | 2246 | err = fuse_device_clone(fud->fc, file); |
Miklos Szeredi | 00c570f | 2015-07-01 16:26:08 +0200 | [diff] [blame] | 2247 | mutex_unlock(&fuse_mutex); |
| 2248 | } |
| 2249 | fput(old); |
| 2250 | } |
| 2251 | } |
| 2252 | } |
| 2253 | return err; |
| 2254 | } |
| 2255 | |
Arjan van de Ven | 4b6f5d2 | 2006-03-28 01:56:42 -0800 | [diff] [blame] | 2256 | const struct file_operations fuse_dev_operations = { |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 2257 | .owner = THIS_MODULE, |
Tom Van Braeckel | 94e4fe2 | 2015-01-12 05:22:16 +0100 | [diff] [blame] | 2258 | .open = fuse_dev_open, |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 2259 | .llseek = no_llseek, |
Al Viro | fbdbacc | 2015-04-03 21:53:39 -0400 | [diff] [blame] | 2260 | .read_iter = fuse_dev_read, |
Miklos Szeredi | c302162 | 2010-05-25 15:06:07 +0200 | [diff] [blame] | 2261 | .splice_read = fuse_dev_splice_read, |
Al Viro | fbdbacc | 2015-04-03 21:53:39 -0400 | [diff] [blame] | 2262 | .write_iter = fuse_dev_write, |
Miklos Szeredi | dd3bb14 | 2010-05-25 15:06:06 +0200 | [diff] [blame] | 2263 | .splice_write = fuse_dev_splice_write, |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 2264 | .poll = fuse_dev_poll, |
| 2265 | .release = fuse_dev_release, |
Jeff Dike | 385a17b | 2006-04-10 22:54:52 -0700 | [diff] [blame] | 2266 | .fasync = fuse_dev_fasync, |
Miklos Szeredi | 00c570f | 2015-07-01 16:26:08 +0200 | [diff] [blame] | 2267 | .unlocked_ioctl = fuse_dev_ioctl, |
| 2268 | .compat_ioctl = fuse_dev_ioctl, |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 2269 | }; |
Tejun Heo | 08cbf54 | 2009-04-14 10:54:53 +0900 | [diff] [blame] | 2270 | EXPORT_SYMBOL_GPL(fuse_dev_operations); |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 2271 | |
| 2272 | static struct miscdevice fuse_miscdevice = { |
| 2273 | .minor = FUSE_MINOR, |
| 2274 | .name = "fuse", |
| 2275 | .fops = &fuse_dev_operations, |
| 2276 | }; |
| 2277 | |
| 2278 | int __init fuse_dev_init(void) |
| 2279 | { |
| 2280 | int err = -ENOMEM; |
| 2281 | fuse_req_cachep = kmem_cache_create("fuse_request", |
| 2282 | sizeof(struct fuse_req), |
Paul Mundt | 20c2df8 | 2007-07-20 10:11:58 +0900 | [diff] [blame] | 2283 | 0, 0, NULL); |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 2284 | if (!fuse_req_cachep) |
| 2285 | goto out; |
| 2286 | |
| 2287 | err = misc_register(&fuse_miscdevice); |
| 2288 | if (err) |
| 2289 | goto out_cache_clean; |
| 2290 | |
| 2291 | return 0; |
| 2292 | |
| 2293 | out_cache_clean: |
| 2294 | kmem_cache_destroy(fuse_req_cachep); |
| 2295 | out: |
| 2296 | return err; |
| 2297 | } |
| 2298 | |
| 2299 | void fuse_dev_cleanup(void) |
| 2300 | { |
| 2301 | misc_deregister(&fuse_miscdevice); |
| 2302 | kmem_cache_destroy(fuse_req_cachep); |
| 2303 | } |