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