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