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