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