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