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