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