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