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