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