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