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