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> |
| 19 | |
| 20 | MODULE_ALIAS_MISCDEV(FUSE_MINOR); |
| 21 | |
Christoph Lameter | e18b890 | 2006-12-06 20:33:20 -0800 | [diff] [blame] | 22 | static struct kmem_cache *fuse_req_cachep; |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 23 | |
Miklos Szeredi | 8bfc016 | 2006-01-16 22:14:28 -0800 | [diff] [blame] | 24 | static struct fuse_conn *fuse_get_conn(struct file *file) |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 25 | { |
Miklos Szeredi | 0720b31 | 2006-04-10 22:54:55 -0700 | [diff] [blame] | 26 | /* |
| 27 | * Lockless access is OK, because file->private data is set |
| 28 | * once during mount and is valid until the file is released. |
| 29 | */ |
| 30 | return file->private_data; |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 31 | } |
| 32 | |
Miklos Szeredi | 8bfc016 | 2006-01-16 22:14:28 -0800 | [diff] [blame] | 33 | static void fuse_request_init(struct fuse_req *req) |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 34 | { |
| 35 | memset(req, 0, sizeof(*req)); |
| 36 | INIT_LIST_HEAD(&req->list); |
Miklos Szeredi | a4d27e7 | 2006-06-25 05:48:54 -0700 | [diff] [blame] | 37 | INIT_LIST_HEAD(&req->intr_entry); |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 38 | init_waitqueue_head(&req->waitq); |
| 39 | atomic_set(&req->count, 1); |
| 40 | } |
| 41 | |
| 42 | struct fuse_req *fuse_request_alloc(void) |
| 43 | { |
Christoph Lameter | e94b176 | 2006-12-06 20:33:17 -0800 | [diff] [blame] | 44 | struct fuse_req *req = kmem_cache_alloc(fuse_req_cachep, GFP_KERNEL); |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 45 | if (req) |
| 46 | fuse_request_init(req); |
| 47 | return req; |
| 48 | } |
| 49 | |
Miklos Szeredi | 3be5a52 | 2008-04-30 00:54:41 -0700 | [diff] [blame] | 50 | struct fuse_req *fuse_request_alloc_nofs(void) |
| 51 | { |
| 52 | struct fuse_req *req = kmem_cache_alloc(fuse_req_cachep, GFP_NOFS); |
| 53 | if (req) |
| 54 | fuse_request_init(req); |
| 55 | return req; |
| 56 | } |
| 57 | |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 58 | void fuse_request_free(struct fuse_req *req) |
| 59 | { |
| 60 | kmem_cache_free(fuse_req_cachep, req); |
| 61 | } |
| 62 | |
Miklos Szeredi | 8bfc016 | 2006-01-16 22:14:28 -0800 | [diff] [blame] | 63 | static void block_sigs(sigset_t *oldset) |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 64 | { |
| 65 | sigset_t mask; |
| 66 | |
| 67 | siginitsetinv(&mask, sigmask(SIGKILL)); |
| 68 | sigprocmask(SIG_BLOCK, &mask, oldset); |
| 69 | } |
| 70 | |
Miklos Szeredi | 8bfc016 | 2006-01-16 22:14:28 -0800 | [diff] [blame] | 71 | static void restore_sigs(sigset_t *oldset) |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 72 | { |
| 73 | sigprocmask(SIG_SETMASK, oldset, NULL); |
| 74 | } |
| 75 | |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 76 | static void __fuse_get_request(struct fuse_req *req) |
| 77 | { |
| 78 | atomic_inc(&req->count); |
| 79 | } |
| 80 | |
| 81 | /* Must be called with > 1 refcount */ |
| 82 | static void __fuse_put_request(struct fuse_req *req) |
| 83 | { |
| 84 | BUG_ON(atomic_read(&req->count) < 2); |
| 85 | atomic_dec(&req->count); |
| 86 | } |
| 87 | |
Miklos Szeredi | 33649c9 | 2006-06-25 05:48:52 -0700 | [diff] [blame] | 88 | static void fuse_req_init_context(struct fuse_req *req) |
| 89 | { |
David Howells | 2186a71 | 2008-11-14 10:38:53 +1100 | [diff] [blame] | 90 | req->in.h.uid = current_fsuid(); |
| 91 | req->in.h.gid = current_fsgid(); |
Miklos Szeredi | 33649c9 | 2006-06-25 05:48:52 -0700 | [diff] [blame] | 92 | req->in.h.pid = current->pid; |
| 93 | } |
| 94 | |
Miklos Szeredi | ce1d5a4 | 2006-04-10 22:54:58 -0700 | [diff] [blame] | 95 | struct fuse_req *fuse_get_req(struct fuse_conn *fc) |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 96 | { |
Miklos Szeredi | 08a53cd | 2006-04-10 22:54:59 -0700 | [diff] [blame] | 97 | struct fuse_req *req; |
| 98 | sigset_t oldset; |
Miklos Szeredi | 9bc5ddd | 2006-04-11 21:16:09 +0200 | [diff] [blame] | 99 | int intr; |
Miklos Szeredi | 08a53cd | 2006-04-10 22:54:59 -0700 | [diff] [blame] | 100 | int err; |
| 101 | |
Miklos Szeredi | 9bc5ddd | 2006-04-11 21:16:09 +0200 | [diff] [blame] | 102 | atomic_inc(&fc->num_waiting); |
Miklos Szeredi | 08a53cd | 2006-04-10 22:54:59 -0700 | [diff] [blame] | 103 | block_sigs(&oldset); |
Miklos Szeredi | 9bc5ddd | 2006-04-11 21:16:09 +0200 | [diff] [blame] | 104 | intr = wait_event_interruptible(fc->blocked_waitq, !fc->blocked); |
Miklos Szeredi | 08a53cd | 2006-04-10 22:54:59 -0700 | [diff] [blame] | 105 | restore_sigs(&oldset); |
Miklos Szeredi | 9bc5ddd | 2006-04-11 21:16:09 +0200 | [diff] [blame] | 106 | err = -EINTR; |
| 107 | if (intr) |
| 108 | goto out; |
Miklos Szeredi | 08a53cd | 2006-04-10 22:54:59 -0700 | [diff] [blame] | 109 | |
Miklos Szeredi | 51eb01e | 2006-06-25 05:48:50 -0700 | [diff] [blame] | 110 | err = -ENOTCONN; |
| 111 | if (!fc->connected) |
| 112 | goto out; |
| 113 | |
Miklos Szeredi | 08a53cd | 2006-04-10 22:54:59 -0700 | [diff] [blame] | 114 | req = fuse_request_alloc(); |
Miklos Szeredi | 9bc5ddd | 2006-04-11 21:16:09 +0200 | [diff] [blame] | 115 | err = -ENOMEM; |
Miklos Szeredi | ce1d5a4 | 2006-04-10 22:54:58 -0700 | [diff] [blame] | 116 | if (!req) |
Miklos Szeredi | 9bc5ddd | 2006-04-11 21:16:09 +0200 | [diff] [blame] | 117 | goto out; |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 118 | |
Miklos Szeredi | 33649c9 | 2006-06-25 05:48:52 -0700 | [diff] [blame] | 119 | fuse_req_init_context(req); |
Miklos Szeredi | 9bc5ddd | 2006-04-11 21:16:09 +0200 | [diff] [blame] | 120 | req->waiting = 1; |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 121 | return req; |
Miklos Szeredi | 9bc5ddd | 2006-04-11 21:16:09 +0200 | [diff] [blame] | 122 | |
| 123 | out: |
| 124 | atomic_dec(&fc->num_waiting); |
| 125 | return ERR_PTR(err); |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 126 | } |
| 127 | |
Miklos Szeredi | 33649c9 | 2006-06-25 05:48:52 -0700 | [diff] [blame] | 128 | /* |
| 129 | * Return request in fuse_file->reserved_req. However that may |
| 130 | * currently be in use. If that is the case, wait for it to become |
| 131 | * available. |
| 132 | */ |
| 133 | static struct fuse_req *get_reserved_req(struct fuse_conn *fc, |
| 134 | struct file *file) |
| 135 | { |
| 136 | struct fuse_req *req = NULL; |
| 137 | struct fuse_file *ff = file->private_data; |
| 138 | |
| 139 | do { |
Miklos Szeredi | de5e3de | 2007-10-16 23:31:00 -0700 | [diff] [blame] | 140 | wait_event(fc->reserved_req_waitq, ff->reserved_req); |
Miklos Szeredi | 33649c9 | 2006-06-25 05:48:52 -0700 | [diff] [blame] | 141 | spin_lock(&fc->lock); |
| 142 | if (ff->reserved_req) { |
| 143 | req = ff->reserved_req; |
| 144 | ff->reserved_req = NULL; |
| 145 | get_file(file); |
| 146 | req->stolen_file = file; |
| 147 | } |
| 148 | spin_unlock(&fc->lock); |
| 149 | } while (!req); |
| 150 | |
| 151 | return req; |
| 152 | } |
| 153 | |
| 154 | /* |
| 155 | * Put stolen request back into fuse_file->reserved_req |
| 156 | */ |
| 157 | static void put_reserved_req(struct fuse_conn *fc, struct fuse_req *req) |
| 158 | { |
| 159 | struct file *file = req->stolen_file; |
| 160 | struct fuse_file *ff = file->private_data; |
| 161 | |
| 162 | spin_lock(&fc->lock); |
| 163 | fuse_request_init(req); |
| 164 | BUG_ON(ff->reserved_req); |
| 165 | ff->reserved_req = req; |
Miklos Szeredi | de5e3de | 2007-10-16 23:31:00 -0700 | [diff] [blame] | 166 | wake_up_all(&fc->reserved_req_waitq); |
Miklos Szeredi | 33649c9 | 2006-06-25 05:48:52 -0700 | [diff] [blame] | 167 | spin_unlock(&fc->lock); |
| 168 | fput(file); |
| 169 | } |
| 170 | |
| 171 | /* |
| 172 | * Gets a requests for a file operation, always succeeds |
| 173 | * |
| 174 | * This is used for sending the FLUSH request, which must get to |
| 175 | * userspace, due to POSIX locks which may need to be unlocked. |
| 176 | * |
| 177 | * If allocation fails due to OOM, use the reserved request in |
| 178 | * fuse_file. |
| 179 | * |
| 180 | * This is very unlikely to deadlock accidentally, since the |
| 181 | * filesystem should not have it's own file open. If deadlock is |
| 182 | * intentional, it can still be broken by "aborting" the filesystem. |
| 183 | */ |
| 184 | struct fuse_req *fuse_get_req_nofail(struct fuse_conn *fc, struct file *file) |
| 185 | { |
| 186 | struct fuse_req *req; |
| 187 | |
| 188 | atomic_inc(&fc->num_waiting); |
| 189 | wait_event(fc->blocked_waitq, !fc->blocked); |
| 190 | req = fuse_request_alloc(); |
| 191 | if (!req) |
| 192 | req = get_reserved_req(fc, file); |
| 193 | |
| 194 | fuse_req_init_context(req); |
| 195 | req->waiting = 1; |
| 196 | return req; |
| 197 | } |
| 198 | |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 199 | void fuse_put_request(struct fuse_conn *fc, struct fuse_req *req) |
| 200 | { |
Miklos Szeredi | 7128ec2 | 2006-02-04 23:27:40 -0800 | [diff] [blame] | 201 | if (atomic_dec_and_test(&req->count)) { |
Miklos Szeredi | 9bc5ddd | 2006-04-11 21:16:09 +0200 | [diff] [blame] | 202 | if (req->waiting) |
| 203 | atomic_dec(&fc->num_waiting); |
Miklos Szeredi | 33649c9 | 2006-06-25 05:48:52 -0700 | [diff] [blame] | 204 | |
| 205 | if (req->stolen_file) |
| 206 | put_reserved_req(fc, req); |
| 207 | else |
| 208 | fuse_request_free(req); |
Miklos Szeredi | 7128ec2 | 2006-02-04 23:27:40 -0800 | [diff] [blame] | 209 | } |
| 210 | } |
| 211 | |
Miklos Szeredi | d12def1 | 2008-02-06 01:38:39 -0800 | [diff] [blame] | 212 | static unsigned len_args(unsigned numargs, struct fuse_arg *args) |
| 213 | { |
| 214 | unsigned nbytes = 0; |
| 215 | unsigned i; |
| 216 | |
| 217 | for (i = 0; i < numargs; i++) |
| 218 | nbytes += args[i].size; |
| 219 | |
| 220 | return nbytes; |
| 221 | } |
| 222 | |
| 223 | static u64 fuse_get_unique(struct fuse_conn *fc) |
| 224 | { |
| 225 | fc->reqctr++; |
| 226 | /* zero is special */ |
| 227 | if (fc->reqctr == 0) |
| 228 | fc->reqctr = 1; |
| 229 | |
| 230 | return fc->reqctr; |
| 231 | } |
| 232 | |
| 233 | static void queue_request(struct fuse_conn *fc, struct fuse_req *req) |
| 234 | { |
| 235 | req->in.h.unique = fuse_get_unique(fc); |
| 236 | req->in.h.len = sizeof(struct fuse_in_header) + |
| 237 | len_args(req->in.numargs, (struct fuse_arg *) req->in.args); |
| 238 | list_add_tail(&req->list, &fc->pending); |
| 239 | req->state = FUSE_REQ_PENDING; |
| 240 | if (!req->waiting) { |
| 241 | req->waiting = 1; |
| 242 | atomic_inc(&fc->num_waiting); |
| 243 | } |
| 244 | wake_up(&fc->waitq); |
| 245 | kill_fasync(&fc->fasync, SIGIO, POLL_IN); |
| 246 | } |
| 247 | |
| 248 | static void flush_bg_queue(struct fuse_conn *fc) |
| 249 | { |
| 250 | while (fc->active_background < FUSE_MAX_BACKGROUND && |
| 251 | !list_empty(&fc->bg_queue)) { |
| 252 | struct fuse_req *req; |
| 253 | |
| 254 | req = list_entry(fc->bg_queue.next, struct fuse_req, list); |
| 255 | list_del(&req->list); |
| 256 | fc->active_background++; |
| 257 | queue_request(fc, req); |
| 258 | } |
| 259 | } |
| 260 | |
Miklos Szeredi | 6dbbcb1 | 2006-04-26 10:49:06 +0200 | [diff] [blame] | 261 | /* |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 262 | * This function is called when a request is finished. Either a reply |
Miklos Szeredi | f9a2842 | 2006-06-25 05:48:53 -0700 | [diff] [blame] | 263 | * 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] | 264 | * occurred during communication with userspace, or the device file |
Miklos Szeredi | 51eb01e | 2006-06-25 05:48:50 -0700 | [diff] [blame] | 265 | * was closed. The requester thread is woken up (if still waiting), |
| 266 | * the 'end' callback is called if given, else the reference to the |
| 267 | * request is released |
Miklos Szeredi | 7128ec2 | 2006-02-04 23:27:40 -0800 | [diff] [blame] | 268 | * |
Miklos Szeredi | d713311 | 2006-04-10 22:54:55 -0700 | [diff] [blame] | 269 | * Called with fc->lock, unlocks it |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 270 | */ |
| 271 | static void request_end(struct fuse_conn *fc, struct fuse_req *req) |
Harvey Harrison | 5d9ec85 | 2008-12-02 14:49:42 +0100 | [diff] [blame] | 272 | __releases(&fc->lock) |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 273 | { |
Miklos Szeredi | 51eb01e | 2006-06-25 05:48:50 -0700 | [diff] [blame] | 274 | void (*end) (struct fuse_conn *, struct fuse_req *) = req->end; |
| 275 | req->end = NULL; |
Miklos Szeredi | d77a1d5 | 2006-01-16 22:14:31 -0800 | [diff] [blame] | 276 | list_del(&req->list); |
Miklos Szeredi | a4d27e7 | 2006-06-25 05:48:54 -0700 | [diff] [blame] | 277 | list_del(&req->intr_entry); |
Miklos Szeredi | 83cfd49 | 2006-01-16 22:14:31 -0800 | [diff] [blame] | 278 | req->state = FUSE_REQ_FINISHED; |
Miklos Szeredi | 51eb01e | 2006-06-25 05:48:50 -0700 | [diff] [blame] | 279 | if (req->background) { |
| 280 | if (fc->num_background == FUSE_MAX_BACKGROUND) { |
| 281 | fc->blocked = 0; |
| 282 | wake_up_all(&fc->blocked_waitq); |
| 283 | } |
Miklos Szeredi | 26c3679 | 2009-01-26 15:00:59 +0100 | [diff] [blame] | 284 | if (fc->num_background == FUSE_CONGESTION_THRESHOLD && |
Tejun Heo | a325f9b | 2009-04-14 10:54:52 +0900 | [diff] [blame^] | 285 | fc->connected && fc->bdi_initialized) { |
Miklos Szeredi | f92b99b | 2007-10-16 23:30:59 -0700 | [diff] [blame] | 286 | clear_bdi_congested(&fc->bdi, READ); |
| 287 | clear_bdi_congested(&fc->bdi, WRITE); |
| 288 | } |
Miklos Szeredi | 51eb01e | 2006-06-25 05:48:50 -0700 | [diff] [blame] | 289 | fc->num_background--; |
Miklos Szeredi | d12def1 | 2008-02-06 01:38:39 -0800 | [diff] [blame] | 290 | fc->active_background--; |
| 291 | flush_bg_queue(fc); |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 292 | } |
Miklos Szeredi | 51eb01e | 2006-06-25 05:48:50 -0700 | [diff] [blame] | 293 | spin_unlock(&fc->lock); |
Miklos Szeredi | 51eb01e | 2006-06-25 05:48:50 -0700 | [diff] [blame] | 294 | wake_up(&req->waitq); |
| 295 | if (end) |
| 296 | end(fc, req); |
Tejun Heo | e9bb09d | 2008-11-26 12:03:54 +0100 | [diff] [blame] | 297 | fuse_put_request(fc, req); |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 298 | } |
| 299 | |
Miklos Szeredi | a4d27e7 | 2006-06-25 05:48:54 -0700 | [diff] [blame] | 300 | static void wait_answer_interruptible(struct fuse_conn *fc, |
| 301 | struct fuse_req *req) |
Harvey Harrison | 5d9ec85 | 2008-12-02 14:49:42 +0100 | [diff] [blame] | 302 | __releases(&fc->lock) |
| 303 | __acquires(&fc->lock) |
Miklos Szeredi | a4d27e7 | 2006-06-25 05:48:54 -0700 | [diff] [blame] | 304 | { |
| 305 | if (signal_pending(current)) |
| 306 | return; |
| 307 | |
| 308 | spin_unlock(&fc->lock); |
| 309 | wait_event_interruptible(req->waitq, req->state == FUSE_REQ_FINISHED); |
| 310 | spin_lock(&fc->lock); |
| 311 | } |
| 312 | |
| 313 | static void queue_interrupt(struct fuse_conn *fc, struct fuse_req *req) |
| 314 | { |
| 315 | list_add_tail(&req->intr_entry, &fc->interrupts); |
| 316 | wake_up(&fc->waitq); |
| 317 | kill_fasync(&fc->fasync, SIGIO, POLL_IN); |
| 318 | } |
| 319 | |
Miklos Szeredi | 7c352bd | 2005-09-09 13:10:39 -0700 | [diff] [blame] | 320 | static void request_wait_answer(struct fuse_conn *fc, struct fuse_req *req) |
Harvey Harrison | 5d9ec85 | 2008-12-02 14:49:42 +0100 | [diff] [blame] | 321 | __releases(&fc->lock) |
| 322 | __acquires(&fc->lock) |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 323 | { |
Miklos Szeredi | a4d27e7 | 2006-06-25 05:48:54 -0700 | [diff] [blame] | 324 | if (!fc->no_interrupt) { |
| 325 | /* Any signal may interrupt this */ |
| 326 | wait_answer_interruptible(fc, req); |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 327 | |
Miklos Szeredi | a4d27e7 | 2006-06-25 05:48:54 -0700 | [diff] [blame] | 328 | if (req->aborted) |
| 329 | goto aborted; |
| 330 | if (req->state == FUSE_REQ_FINISHED) |
| 331 | return; |
| 332 | |
| 333 | req->interrupted = 1; |
| 334 | if (req->state == FUSE_REQ_SENT) |
| 335 | queue_interrupt(fc, req); |
| 336 | } |
| 337 | |
Miklos Szeredi | a131de0 | 2007-10-16 23:31:04 -0700 | [diff] [blame] | 338 | if (!req->force) { |
Miklos Szeredi | a4d27e7 | 2006-06-25 05:48:54 -0700 | [diff] [blame] | 339 | sigset_t oldset; |
| 340 | |
| 341 | /* Only fatal signals may interrupt this */ |
Miklos Szeredi | 51eb01e | 2006-06-25 05:48:50 -0700 | [diff] [blame] | 342 | block_sigs(&oldset); |
Miklos Szeredi | a4d27e7 | 2006-06-25 05:48:54 -0700 | [diff] [blame] | 343 | wait_answer_interruptible(fc, req); |
Miklos Szeredi | 51eb01e | 2006-06-25 05:48:50 -0700 | [diff] [blame] | 344 | restore_sigs(&oldset); |
Miklos Szeredi | a131de0 | 2007-10-16 23:31:04 -0700 | [diff] [blame] | 345 | |
| 346 | if (req->aborted) |
| 347 | goto aborted; |
| 348 | if (req->state == FUSE_REQ_FINISHED) |
| 349 | return; |
| 350 | |
| 351 | /* Request is not yet in userspace, bail out */ |
| 352 | if (req->state == FUSE_REQ_PENDING) { |
| 353 | list_del(&req->list); |
| 354 | __fuse_put_request(req); |
| 355 | req->out.h.error = -EINTR; |
| 356 | return; |
| 357 | } |
Miklos Szeredi | 51eb01e | 2006-06-25 05:48:50 -0700 | [diff] [blame] | 358 | } |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 359 | |
Miklos Szeredi | a131de0 | 2007-10-16 23:31:04 -0700 | [diff] [blame] | 360 | /* |
| 361 | * Either request is already in userspace, or it was forced. |
| 362 | * Wait it out. |
| 363 | */ |
| 364 | spin_unlock(&fc->lock); |
| 365 | wait_event(req->waitq, req->state == FUSE_REQ_FINISHED); |
| 366 | spin_lock(&fc->lock); |
Miklos Szeredi | a4d27e7 | 2006-06-25 05:48:54 -0700 | [diff] [blame] | 367 | |
Miklos Szeredi | a131de0 | 2007-10-16 23:31:04 -0700 | [diff] [blame] | 368 | if (!req->aborted) |
| 369 | return; |
Miklos Szeredi | a4d27e7 | 2006-06-25 05:48:54 -0700 | [diff] [blame] | 370 | |
| 371 | aborted: |
Miklos Szeredi | a131de0 | 2007-10-16 23:31:04 -0700 | [diff] [blame] | 372 | BUG_ON(req->state != FUSE_REQ_FINISHED); |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 373 | if (req->locked) { |
| 374 | /* This is uninterruptible sleep, because data is |
| 375 | being copied to/from the buffers of req. During |
| 376 | locked state, there mustn't be any filesystem |
| 377 | operation (e.g. page fault), since that could lead |
| 378 | to deadlock */ |
Miklos Szeredi | d713311 | 2006-04-10 22:54:55 -0700 | [diff] [blame] | 379 | spin_unlock(&fc->lock); |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 380 | wait_event(req->waitq, !req->locked); |
Miklos Szeredi | d713311 | 2006-04-10 22:54:55 -0700 | [diff] [blame] | 381 | spin_lock(&fc->lock); |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 382 | } |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 383 | } |
| 384 | |
Tejun Heo | b93f858 | 2008-11-26 12:03:55 +0100 | [diff] [blame] | 385 | void fuse_request_send(struct fuse_conn *fc, struct fuse_req *req) |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 386 | { |
| 387 | req->isreply = 1; |
Miklos Szeredi | d713311 | 2006-04-10 22:54:55 -0700 | [diff] [blame] | 388 | spin_lock(&fc->lock); |
Miklos Szeredi | 1e9a4ed | 2005-09-09 13:10:31 -0700 | [diff] [blame] | 389 | if (!fc->connected) |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 390 | req->out.h.error = -ENOTCONN; |
| 391 | else if (fc->conn_error) |
| 392 | req->out.h.error = -ECONNREFUSED; |
| 393 | else { |
| 394 | queue_request(fc, req); |
| 395 | /* acquire extra reference, since request is still needed |
| 396 | after request_end() */ |
| 397 | __fuse_get_request(req); |
| 398 | |
Miklos Szeredi | 7c352bd | 2005-09-09 13:10:39 -0700 | [diff] [blame] | 399 | request_wait_answer(fc, req); |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 400 | } |
Miklos Szeredi | d713311 | 2006-04-10 22:54:55 -0700 | [diff] [blame] | 401 | spin_unlock(&fc->lock); |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 402 | } |
| 403 | |
Tejun Heo | b93f858 | 2008-11-26 12:03:55 +0100 | [diff] [blame] | 404 | static void fuse_request_send_nowait_locked(struct fuse_conn *fc, |
| 405 | struct fuse_req *req) |
Miklos Szeredi | d12def1 | 2008-02-06 01:38:39 -0800 | [diff] [blame] | 406 | { |
| 407 | req->background = 1; |
| 408 | fc->num_background++; |
| 409 | if (fc->num_background == FUSE_MAX_BACKGROUND) |
| 410 | fc->blocked = 1; |
Tejun Heo | a325f9b | 2009-04-14 10:54:52 +0900 | [diff] [blame^] | 411 | if (fc->num_background == FUSE_CONGESTION_THRESHOLD && |
| 412 | fc->bdi_initialized) { |
Miklos Szeredi | d12def1 | 2008-02-06 01:38:39 -0800 | [diff] [blame] | 413 | set_bdi_congested(&fc->bdi, READ); |
| 414 | set_bdi_congested(&fc->bdi, WRITE); |
| 415 | } |
| 416 | list_add_tail(&req->list, &fc->bg_queue); |
| 417 | flush_bg_queue(fc); |
| 418 | } |
| 419 | |
Tejun Heo | b93f858 | 2008-11-26 12:03:55 +0100 | [diff] [blame] | 420 | 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] | 421 | { |
Miklos Szeredi | d713311 | 2006-04-10 22:54:55 -0700 | [diff] [blame] | 422 | spin_lock(&fc->lock); |
Miklos Szeredi | 1e9a4ed | 2005-09-09 13:10:31 -0700 | [diff] [blame] | 423 | if (fc->connected) { |
Tejun Heo | b93f858 | 2008-11-26 12:03:55 +0100 | [diff] [blame] | 424 | fuse_request_send_nowait_locked(fc, req); |
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 | } else { |
| 427 | req->out.h.error = -ENOTCONN; |
| 428 | request_end(fc, req); |
| 429 | } |
| 430 | } |
| 431 | |
Tejun Heo | b93f858 | 2008-11-26 12:03:55 +0100 | [diff] [blame] | 432 | void fuse_request_send_noreply(struct fuse_conn *fc, struct fuse_req *req) |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 433 | { |
| 434 | req->isreply = 0; |
Tejun Heo | b93f858 | 2008-11-26 12:03:55 +0100 | [diff] [blame] | 435 | fuse_request_send_nowait(fc, req); |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 436 | } |
| 437 | |
Tejun Heo | b93f858 | 2008-11-26 12:03:55 +0100 | [diff] [blame] | 438 | 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] | 439 | { |
| 440 | req->isreply = 1; |
Tejun Heo | b93f858 | 2008-11-26 12:03:55 +0100 | [diff] [blame] | 441 | fuse_request_send_nowait(fc, req); |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 442 | } |
| 443 | |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 444 | /* |
Miklos Szeredi | 3be5a52 | 2008-04-30 00:54:41 -0700 | [diff] [blame] | 445 | * Called under fc->lock |
| 446 | * |
| 447 | * fc->connected must have been checked previously |
| 448 | */ |
Tejun Heo | b93f858 | 2008-11-26 12:03:55 +0100 | [diff] [blame] | 449 | void fuse_request_send_background_locked(struct fuse_conn *fc, |
| 450 | struct fuse_req *req) |
Miklos Szeredi | 3be5a52 | 2008-04-30 00:54:41 -0700 | [diff] [blame] | 451 | { |
| 452 | req->isreply = 1; |
Tejun Heo | b93f858 | 2008-11-26 12:03:55 +0100 | [diff] [blame] | 453 | fuse_request_send_nowait_locked(fc, req); |
Miklos Szeredi | 3be5a52 | 2008-04-30 00:54:41 -0700 | [diff] [blame] | 454 | } |
| 455 | |
| 456 | /* |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 457 | * Lock the request. Up to the next unlock_request() there mustn't be |
| 458 | * anything that could cause a page-fault. If the request was already |
Miklos Szeredi | f9a2842 | 2006-06-25 05:48:53 -0700 | [diff] [blame] | 459 | * aborted bail out. |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 460 | */ |
Miklos Szeredi | d713311 | 2006-04-10 22:54:55 -0700 | [diff] [blame] | 461 | static int lock_request(struct fuse_conn *fc, struct fuse_req *req) |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 462 | { |
| 463 | int err = 0; |
| 464 | if (req) { |
Miklos Szeredi | d713311 | 2006-04-10 22:54:55 -0700 | [diff] [blame] | 465 | spin_lock(&fc->lock); |
Miklos Szeredi | f9a2842 | 2006-06-25 05:48:53 -0700 | [diff] [blame] | 466 | if (req->aborted) |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 467 | err = -ENOENT; |
| 468 | else |
| 469 | req->locked = 1; |
Miklos Szeredi | d713311 | 2006-04-10 22:54:55 -0700 | [diff] [blame] | 470 | spin_unlock(&fc->lock); |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 471 | } |
| 472 | return err; |
| 473 | } |
| 474 | |
| 475 | /* |
Miklos Szeredi | f9a2842 | 2006-06-25 05:48:53 -0700 | [diff] [blame] | 476 | * Unlock request. If it was aborted during being locked, the |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 477 | * requester thread is currently waiting for it to be unlocked, so |
| 478 | * wake it up. |
| 479 | */ |
Miklos Szeredi | d713311 | 2006-04-10 22:54:55 -0700 | [diff] [blame] | 480 | static void unlock_request(struct fuse_conn *fc, struct fuse_req *req) |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 481 | { |
| 482 | if (req) { |
Miklos Szeredi | d713311 | 2006-04-10 22:54:55 -0700 | [diff] [blame] | 483 | spin_lock(&fc->lock); |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 484 | req->locked = 0; |
Miklos Szeredi | f9a2842 | 2006-06-25 05:48:53 -0700 | [diff] [blame] | 485 | if (req->aborted) |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 486 | wake_up(&req->waitq); |
Miklos Szeredi | d713311 | 2006-04-10 22:54:55 -0700 | [diff] [blame] | 487 | spin_unlock(&fc->lock); |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 488 | } |
| 489 | } |
| 490 | |
| 491 | struct fuse_copy_state { |
Miklos Szeredi | d713311 | 2006-04-10 22:54:55 -0700 | [diff] [blame] | 492 | struct fuse_conn *fc; |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 493 | int write; |
| 494 | struct fuse_req *req; |
| 495 | const struct iovec *iov; |
| 496 | unsigned long nr_segs; |
| 497 | unsigned long seglen; |
| 498 | unsigned long addr; |
| 499 | struct page *pg; |
| 500 | void *mapaddr; |
| 501 | void *buf; |
| 502 | unsigned len; |
| 503 | }; |
| 504 | |
Miklos Szeredi | d713311 | 2006-04-10 22:54:55 -0700 | [diff] [blame] | 505 | static void fuse_copy_init(struct fuse_copy_state *cs, struct fuse_conn *fc, |
| 506 | int write, struct fuse_req *req, |
| 507 | const struct iovec *iov, unsigned long nr_segs) |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 508 | { |
| 509 | memset(cs, 0, sizeof(*cs)); |
Miklos Szeredi | d713311 | 2006-04-10 22:54:55 -0700 | [diff] [blame] | 510 | cs->fc = fc; |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 511 | cs->write = write; |
| 512 | cs->req = req; |
| 513 | cs->iov = iov; |
| 514 | cs->nr_segs = nr_segs; |
| 515 | } |
| 516 | |
| 517 | /* Unmap and put previous page of userspace buffer */ |
Miklos Szeredi | 8bfc016 | 2006-01-16 22:14:28 -0800 | [diff] [blame] | 518 | static void fuse_copy_finish(struct fuse_copy_state *cs) |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 519 | { |
| 520 | if (cs->mapaddr) { |
| 521 | kunmap_atomic(cs->mapaddr, KM_USER0); |
| 522 | if (cs->write) { |
| 523 | flush_dcache_page(cs->pg); |
| 524 | set_page_dirty_lock(cs->pg); |
| 525 | } |
| 526 | put_page(cs->pg); |
| 527 | cs->mapaddr = NULL; |
| 528 | } |
| 529 | } |
| 530 | |
| 531 | /* |
| 532 | * Get another pagefull of userspace buffer, and map it to kernel |
| 533 | * address space, and lock request |
| 534 | */ |
| 535 | static int fuse_copy_fill(struct fuse_copy_state *cs) |
| 536 | { |
| 537 | unsigned long offset; |
| 538 | int err; |
| 539 | |
Miklos Szeredi | d713311 | 2006-04-10 22:54:55 -0700 | [diff] [blame] | 540 | unlock_request(cs->fc, cs->req); |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 541 | fuse_copy_finish(cs); |
| 542 | if (!cs->seglen) { |
| 543 | BUG_ON(!cs->nr_segs); |
| 544 | cs->seglen = cs->iov[0].iov_len; |
| 545 | cs->addr = (unsigned long) cs->iov[0].iov_base; |
Miklos Szeredi | 1729a16 | 2008-11-26 12:03:54 +0100 | [diff] [blame] | 546 | cs->iov++; |
| 547 | cs->nr_segs--; |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 548 | } |
| 549 | down_read(¤t->mm->mmap_sem); |
| 550 | err = get_user_pages(current, current->mm, cs->addr, 1, cs->write, 0, |
| 551 | &cs->pg, NULL); |
| 552 | up_read(¤t->mm->mmap_sem); |
| 553 | if (err < 0) |
| 554 | return err; |
| 555 | BUG_ON(err != 1); |
| 556 | offset = cs->addr % PAGE_SIZE; |
| 557 | cs->mapaddr = kmap_atomic(cs->pg, KM_USER0); |
| 558 | cs->buf = cs->mapaddr + offset; |
| 559 | cs->len = min(PAGE_SIZE - offset, cs->seglen); |
| 560 | cs->seglen -= cs->len; |
| 561 | cs->addr += cs->len; |
| 562 | |
Miklos Szeredi | d713311 | 2006-04-10 22:54:55 -0700 | [diff] [blame] | 563 | return lock_request(cs->fc, cs->req); |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 564 | } |
| 565 | |
| 566 | /* Do as much copy to/from userspace buffer as we can */ |
Miklos Szeredi | 8bfc016 | 2006-01-16 22:14:28 -0800 | [diff] [blame] | 567 | 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] | 568 | { |
| 569 | unsigned ncpy = min(*size, cs->len); |
| 570 | if (val) { |
| 571 | if (cs->write) |
| 572 | memcpy(cs->buf, *val, ncpy); |
| 573 | else |
| 574 | memcpy(*val, cs->buf, ncpy); |
| 575 | *val += ncpy; |
| 576 | } |
| 577 | *size -= ncpy; |
| 578 | cs->len -= ncpy; |
| 579 | cs->buf += ncpy; |
| 580 | return ncpy; |
| 581 | } |
| 582 | |
| 583 | /* |
| 584 | * Copy a page in the request to/from the userspace buffer. Must be |
| 585 | * done atomically |
| 586 | */ |
Miklos Szeredi | 8bfc016 | 2006-01-16 22:14:28 -0800 | [diff] [blame] | 587 | static int fuse_copy_page(struct fuse_copy_state *cs, struct page *page, |
| 588 | unsigned offset, unsigned count, int zeroing) |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 589 | { |
| 590 | if (page && zeroing && count < PAGE_SIZE) { |
| 591 | void *mapaddr = kmap_atomic(page, KM_USER1); |
| 592 | memset(mapaddr, 0, PAGE_SIZE); |
| 593 | kunmap_atomic(mapaddr, KM_USER1); |
| 594 | } |
| 595 | while (count) { |
Miklos Szeredi | 1729a16 | 2008-11-26 12:03:54 +0100 | [diff] [blame] | 596 | if (!cs->len) { |
| 597 | int err = fuse_copy_fill(cs); |
| 598 | if (err) |
| 599 | return err; |
| 600 | } |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 601 | if (page) { |
| 602 | void *mapaddr = kmap_atomic(page, KM_USER1); |
| 603 | void *buf = mapaddr + offset; |
| 604 | offset += fuse_copy_do(cs, &buf, &count); |
| 605 | kunmap_atomic(mapaddr, KM_USER1); |
| 606 | } else |
| 607 | offset += fuse_copy_do(cs, NULL, &count); |
| 608 | } |
| 609 | if (page && !cs->write) |
| 610 | flush_dcache_page(page); |
| 611 | return 0; |
| 612 | } |
| 613 | |
| 614 | /* Copy pages in the request to/from userspace buffer */ |
| 615 | static int fuse_copy_pages(struct fuse_copy_state *cs, unsigned nbytes, |
| 616 | int zeroing) |
| 617 | { |
| 618 | unsigned i; |
| 619 | struct fuse_req *req = cs->req; |
| 620 | unsigned offset = req->page_offset; |
| 621 | unsigned count = min(nbytes, (unsigned) PAGE_SIZE - offset); |
| 622 | |
| 623 | for (i = 0; i < req->num_pages && (nbytes || zeroing); i++) { |
| 624 | struct page *page = req->pages[i]; |
| 625 | int err = fuse_copy_page(cs, page, offset, count, zeroing); |
| 626 | if (err) |
| 627 | return err; |
| 628 | |
| 629 | nbytes -= count; |
| 630 | count = min(nbytes, (unsigned) PAGE_SIZE); |
| 631 | offset = 0; |
| 632 | } |
| 633 | return 0; |
| 634 | } |
| 635 | |
| 636 | /* Copy a single argument in the request to/from userspace buffer */ |
| 637 | static int fuse_copy_one(struct fuse_copy_state *cs, void *val, unsigned size) |
| 638 | { |
| 639 | while (size) { |
Miklos Szeredi | 1729a16 | 2008-11-26 12:03:54 +0100 | [diff] [blame] | 640 | if (!cs->len) { |
| 641 | int err = fuse_copy_fill(cs); |
| 642 | if (err) |
| 643 | return err; |
| 644 | } |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 645 | fuse_copy_do(cs, &val, &size); |
| 646 | } |
| 647 | return 0; |
| 648 | } |
| 649 | |
| 650 | /* Copy request arguments to/from userspace buffer */ |
| 651 | static int fuse_copy_args(struct fuse_copy_state *cs, unsigned numargs, |
| 652 | unsigned argpages, struct fuse_arg *args, |
| 653 | int zeroing) |
| 654 | { |
| 655 | int err = 0; |
| 656 | unsigned i; |
| 657 | |
| 658 | for (i = 0; !err && i < numargs; i++) { |
| 659 | struct fuse_arg *arg = &args[i]; |
| 660 | if (i == numargs - 1 && argpages) |
| 661 | err = fuse_copy_pages(cs, arg->size, zeroing); |
| 662 | else |
| 663 | err = fuse_copy_one(cs, arg->value, arg->size); |
| 664 | } |
| 665 | return err; |
| 666 | } |
| 667 | |
Miklos Szeredi | a4d27e7 | 2006-06-25 05:48:54 -0700 | [diff] [blame] | 668 | static int request_pending(struct fuse_conn *fc) |
| 669 | { |
| 670 | return !list_empty(&fc->pending) || !list_empty(&fc->interrupts); |
| 671 | } |
| 672 | |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 673 | /* Wait until a request is available on the pending list */ |
| 674 | static void request_wait(struct fuse_conn *fc) |
Harvey Harrison | 5d9ec85 | 2008-12-02 14:49:42 +0100 | [diff] [blame] | 675 | __releases(&fc->lock) |
| 676 | __acquires(&fc->lock) |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 677 | { |
| 678 | DECLARE_WAITQUEUE(wait, current); |
| 679 | |
| 680 | add_wait_queue_exclusive(&fc->waitq, &wait); |
Miklos Szeredi | a4d27e7 | 2006-06-25 05:48:54 -0700 | [diff] [blame] | 681 | while (fc->connected && !request_pending(fc)) { |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 682 | set_current_state(TASK_INTERRUPTIBLE); |
| 683 | if (signal_pending(current)) |
| 684 | break; |
| 685 | |
Miklos Szeredi | d713311 | 2006-04-10 22:54:55 -0700 | [diff] [blame] | 686 | spin_unlock(&fc->lock); |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 687 | schedule(); |
Miklos Szeredi | d713311 | 2006-04-10 22:54:55 -0700 | [diff] [blame] | 688 | spin_lock(&fc->lock); |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 689 | } |
| 690 | set_current_state(TASK_RUNNING); |
| 691 | remove_wait_queue(&fc->waitq, &wait); |
| 692 | } |
| 693 | |
| 694 | /* |
Miklos Szeredi | a4d27e7 | 2006-06-25 05:48:54 -0700 | [diff] [blame] | 695 | * Transfer an interrupt request to userspace |
| 696 | * |
| 697 | * Unlike other requests this is assembled on demand, without a need |
| 698 | * to allocate a separate fuse_req structure. |
| 699 | * |
| 700 | * Called with fc->lock held, releases it |
| 701 | */ |
| 702 | static int fuse_read_interrupt(struct fuse_conn *fc, struct fuse_req *req, |
| 703 | const struct iovec *iov, unsigned long nr_segs) |
Harvey Harrison | 5d9ec85 | 2008-12-02 14:49:42 +0100 | [diff] [blame] | 704 | __releases(&fc->lock) |
Miklos Szeredi | a4d27e7 | 2006-06-25 05:48:54 -0700 | [diff] [blame] | 705 | { |
| 706 | struct fuse_copy_state cs; |
| 707 | struct fuse_in_header ih; |
| 708 | struct fuse_interrupt_in arg; |
| 709 | unsigned reqsize = sizeof(ih) + sizeof(arg); |
| 710 | int err; |
| 711 | |
| 712 | list_del_init(&req->intr_entry); |
| 713 | req->intr_unique = fuse_get_unique(fc); |
| 714 | memset(&ih, 0, sizeof(ih)); |
| 715 | memset(&arg, 0, sizeof(arg)); |
| 716 | ih.len = reqsize; |
| 717 | ih.opcode = FUSE_INTERRUPT; |
| 718 | ih.unique = req->intr_unique; |
| 719 | arg.unique = req->in.h.unique; |
| 720 | |
| 721 | spin_unlock(&fc->lock); |
| 722 | if (iov_length(iov, nr_segs) < reqsize) |
| 723 | return -EINVAL; |
| 724 | |
| 725 | fuse_copy_init(&cs, fc, 1, NULL, iov, nr_segs); |
| 726 | err = fuse_copy_one(&cs, &ih, sizeof(ih)); |
| 727 | if (!err) |
| 728 | err = fuse_copy_one(&cs, &arg, sizeof(arg)); |
| 729 | fuse_copy_finish(&cs); |
| 730 | |
| 731 | return err ? err : reqsize; |
| 732 | } |
| 733 | |
| 734 | /* |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 735 | * Read a single request into the userspace filesystem's buffer. This |
| 736 | * function waits until a request is available, then removes it from |
| 737 | * the pending list and copies request data to userspace buffer. If |
Miklos Szeredi | f9a2842 | 2006-06-25 05:48:53 -0700 | [diff] [blame] | 738 | * no reply is needed (FORGET) or request has been aborted or there |
| 739 | * was an error during the copying then it's finished by calling |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 740 | * request_end(). Otherwise add it to the processing list, and set |
| 741 | * the 'sent' flag. |
| 742 | */ |
Badari Pulavarty | ee0b3e6 | 2006-09-30 23:28:47 -0700 | [diff] [blame] | 743 | static ssize_t fuse_dev_read(struct kiocb *iocb, const struct iovec *iov, |
| 744 | unsigned long nr_segs, loff_t pos) |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 745 | { |
| 746 | int err; |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 747 | struct fuse_req *req; |
| 748 | struct fuse_in *in; |
| 749 | struct fuse_copy_state cs; |
| 750 | unsigned reqsize; |
Badari Pulavarty | ee0b3e6 | 2006-09-30 23:28:47 -0700 | [diff] [blame] | 751 | struct file *file = iocb->ki_filp; |
Miklos Szeredi | 0720b31 | 2006-04-10 22:54:55 -0700 | [diff] [blame] | 752 | struct fuse_conn *fc = fuse_get_conn(file); |
| 753 | if (!fc) |
| 754 | return -EPERM; |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 755 | |
Miklos Szeredi | 1d3d752 | 2006-01-06 00:19:40 -0800 | [diff] [blame] | 756 | restart: |
Miklos Szeredi | d713311 | 2006-04-10 22:54:55 -0700 | [diff] [blame] | 757 | spin_lock(&fc->lock); |
Jeff Dike | e5ac1d1 | 2006-04-10 22:54:53 -0700 | [diff] [blame] | 758 | err = -EAGAIN; |
| 759 | if ((file->f_flags & O_NONBLOCK) && fc->connected && |
Miklos Szeredi | a4d27e7 | 2006-06-25 05:48:54 -0700 | [diff] [blame] | 760 | !request_pending(fc)) |
Jeff Dike | e5ac1d1 | 2006-04-10 22:54:53 -0700 | [diff] [blame] | 761 | goto err_unlock; |
| 762 | |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 763 | request_wait(fc); |
| 764 | err = -ENODEV; |
Miklos Szeredi | 9ba7cbb | 2006-01-16 22:14:34 -0800 | [diff] [blame] | 765 | if (!fc->connected) |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 766 | goto err_unlock; |
| 767 | err = -ERESTARTSYS; |
Miklos Szeredi | a4d27e7 | 2006-06-25 05:48:54 -0700 | [diff] [blame] | 768 | if (!request_pending(fc)) |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 769 | goto err_unlock; |
| 770 | |
Miklos Szeredi | a4d27e7 | 2006-06-25 05:48:54 -0700 | [diff] [blame] | 771 | if (!list_empty(&fc->interrupts)) { |
| 772 | req = list_entry(fc->interrupts.next, struct fuse_req, |
| 773 | intr_entry); |
| 774 | return fuse_read_interrupt(fc, req, iov, nr_segs); |
| 775 | } |
| 776 | |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 777 | req = list_entry(fc->pending.next, struct fuse_req, list); |
Miklos Szeredi | 83cfd49 | 2006-01-16 22:14:31 -0800 | [diff] [blame] | 778 | req->state = FUSE_REQ_READING; |
Miklos Szeredi | d77a1d5 | 2006-01-16 22:14:31 -0800 | [diff] [blame] | 779 | list_move(&req->list, &fc->io); |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 780 | |
| 781 | in = &req->in; |
Miklos Szeredi | 1d3d752 | 2006-01-06 00:19:40 -0800 | [diff] [blame] | 782 | reqsize = in->h.len; |
| 783 | /* If request is too large, reply with an error and restart the read */ |
| 784 | if (iov_length(iov, nr_segs) < reqsize) { |
| 785 | req->out.h.error = -EIO; |
| 786 | /* SETXATTR is special, since it may contain too large data */ |
| 787 | if (in->h.opcode == FUSE_SETXATTR) |
| 788 | req->out.h.error = -E2BIG; |
| 789 | request_end(fc, req); |
| 790 | goto restart; |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 791 | } |
Miklos Szeredi | d713311 | 2006-04-10 22:54:55 -0700 | [diff] [blame] | 792 | spin_unlock(&fc->lock); |
| 793 | fuse_copy_init(&cs, fc, 1, req, iov, nr_segs); |
Miklos Szeredi | 1d3d752 | 2006-01-06 00:19:40 -0800 | [diff] [blame] | 794 | err = fuse_copy_one(&cs, &in->h, sizeof(in->h)); |
| 795 | if (!err) |
| 796 | err = fuse_copy_args(&cs, in->numargs, in->argpages, |
| 797 | (struct fuse_arg *) in->args, 0); |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 798 | fuse_copy_finish(&cs); |
Miklos Szeredi | d713311 | 2006-04-10 22:54:55 -0700 | [diff] [blame] | 799 | spin_lock(&fc->lock); |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 800 | req->locked = 0; |
Miklos Szeredi | c9c9d7d | 2007-10-16 23:31:05 -0700 | [diff] [blame] | 801 | if (req->aborted) { |
| 802 | request_end(fc, req); |
| 803 | return -ENODEV; |
| 804 | } |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 805 | if (err) { |
Miklos Szeredi | c9c9d7d | 2007-10-16 23:31:05 -0700 | [diff] [blame] | 806 | req->out.h.error = -EIO; |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 807 | request_end(fc, req); |
| 808 | return err; |
| 809 | } |
| 810 | if (!req->isreply) |
| 811 | request_end(fc, req); |
| 812 | else { |
Miklos Szeredi | 83cfd49 | 2006-01-16 22:14:31 -0800 | [diff] [blame] | 813 | req->state = FUSE_REQ_SENT; |
Miklos Szeredi | d77a1d5 | 2006-01-16 22:14:31 -0800 | [diff] [blame] | 814 | list_move_tail(&req->list, &fc->processing); |
Miklos Szeredi | a4d27e7 | 2006-06-25 05:48:54 -0700 | [diff] [blame] | 815 | if (req->interrupted) |
| 816 | queue_interrupt(fc, req); |
Miklos Szeredi | d713311 | 2006-04-10 22:54:55 -0700 | [diff] [blame] | 817 | spin_unlock(&fc->lock); |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 818 | } |
| 819 | return reqsize; |
| 820 | |
| 821 | err_unlock: |
Miklos Szeredi | d713311 | 2006-04-10 22:54:55 -0700 | [diff] [blame] | 822 | spin_unlock(&fc->lock); |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 823 | return err; |
| 824 | } |
| 825 | |
Tejun Heo | 95668a6 | 2008-11-26 12:03:55 +0100 | [diff] [blame] | 826 | static int fuse_notify_poll(struct fuse_conn *fc, unsigned int size, |
| 827 | struct fuse_copy_state *cs) |
| 828 | { |
| 829 | struct fuse_notify_poll_wakeup_out outarg; |
Miklos Szeredi | f6d47a1 | 2009-01-26 15:00:59 +0100 | [diff] [blame] | 830 | int err = -EINVAL; |
Tejun Heo | 95668a6 | 2008-11-26 12:03:55 +0100 | [diff] [blame] | 831 | |
| 832 | if (size != sizeof(outarg)) |
Miklos Szeredi | f6d47a1 | 2009-01-26 15:00:59 +0100 | [diff] [blame] | 833 | goto err; |
Tejun Heo | 95668a6 | 2008-11-26 12:03:55 +0100 | [diff] [blame] | 834 | |
| 835 | err = fuse_copy_one(cs, &outarg, sizeof(outarg)); |
| 836 | if (err) |
Miklos Szeredi | f6d47a1 | 2009-01-26 15:00:59 +0100 | [diff] [blame] | 837 | goto err; |
Tejun Heo | 95668a6 | 2008-11-26 12:03:55 +0100 | [diff] [blame] | 838 | |
Miklos Szeredi | f6d47a1 | 2009-01-26 15:00:59 +0100 | [diff] [blame] | 839 | fuse_copy_finish(cs); |
Tejun Heo | 95668a6 | 2008-11-26 12:03:55 +0100 | [diff] [blame] | 840 | return fuse_notify_poll_wakeup(fc, &outarg); |
Miklos Szeredi | f6d47a1 | 2009-01-26 15:00:59 +0100 | [diff] [blame] | 841 | |
| 842 | err: |
| 843 | fuse_copy_finish(cs); |
| 844 | return err; |
Tejun Heo | 95668a6 | 2008-11-26 12:03:55 +0100 | [diff] [blame] | 845 | } |
| 846 | |
Tejun Heo | 8599396 | 2008-11-26 12:03:55 +0100 | [diff] [blame] | 847 | static int fuse_notify(struct fuse_conn *fc, enum fuse_notify_code code, |
| 848 | unsigned int size, struct fuse_copy_state *cs) |
| 849 | { |
| 850 | switch (code) { |
Tejun Heo | 95668a6 | 2008-11-26 12:03:55 +0100 | [diff] [blame] | 851 | case FUSE_NOTIFY_POLL: |
| 852 | return fuse_notify_poll(fc, size, cs); |
| 853 | |
Tejun Heo | 8599396 | 2008-11-26 12:03:55 +0100 | [diff] [blame] | 854 | default: |
Miklos Szeredi | f6d47a1 | 2009-01-26 15:00:59 +0100 | [diff] [blame] | 855 | fuse_copy_finish(cs); |
Tejun Heo | 8599396 | 2008-11-26 12:03:55 +0100 | [diff] [blame] | 856 | return -EINVAL; |
| 857 | } |
| 858 | } |
| 859 | |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 860 | /* Look up request on processing list by unique ID */ |
| 861 | static struct fuse_req *request_find(struct fuse_conn *fc, u64 unique) |
| 862 | { |
| 863 | struct list_head *entry; |
| 864 | |
| 865 | list_for_each(entry, &fc->processing) { |
| 866 | struct fuse_req *req; |
| 867 | req = list_entry(entry, struct fuse_req, list); |
Miklos Szeredi | a4d27e7 | 2006-06-25 05:48:54 -0700 | [diff] [blame] | 868 | if (req->in.h.unique == unique || req->intr_unique == unique) |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 869 | return req; |
| 870 | } |
| 871 | return NULL; |
| 872 | } |
| 873 | |
| 874 | static int copy_out_args(struct fuse_copy_state *cs, struct fuse_out *out, |
| 875 | unsigned nbytes) |
| 876 | { |
| 877 | unsigned reqsize = sizeof(struct fuse_out_header); |
| 878 | |
| 879 | if (out->h.error) |
| 880 | return nbytes != reqsize ? -EINVAL : 0; |
| 881 | |
| 882 | reqsize += len_args(out->numargs, out->args); |
| 883 | |
| 884 | if (reqsize < nbytes || (reqsize > nbytes && !out->argvar)) |
| 885 | return -EINVAL; |
| 886 | else if (reqsize > nbytes) { |
| 887 | struct fuse_arg *lastarg = &out->args[out->numargs-1]; |
| 888 | unsigned diffsize = reqsize - nbytes; |
| 889 | if (diffsize > lastarg->size) |
| 890 | return -EINVAL; |
| 891 | lastarg->size -= diffsize; |
| 892 | } |
| 893 | return fuse_copy_args(cs, out->numargs, out->argpages, out->args, |
| 894 | out->page_zeroing); |
| 895 | } |
| 896 | |
| 897 | /* |
| 898 | * Write a single reply to a request. First the header is copied from |
| 899 | * the write buffer. The request is then searched on the processing |
| 900 | * list by the unique ID found in the header. If found, then remove |
| 901 | * it from the list and copy the rest of the buffer to the request. |
| 902 | * The request is finished by calling request_end() |
| 903 | */ |
Badari Pulavarty | ee0b3e6 | 2006-09-30 23:28:47 -0700 | [diff] [blame] | 904 | static ssize_t fuse_dev_write(struct kiocb *iocb, const struct iovec *iov, |
| 905 | unsigned long nr_segs, loff_t pos) |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 906 | { |
| 907 | int err; |
| 908 | unsigned nbytes = iov_length(iov, nr_segs); |
| 909 | struct fuse_req *req; |
| 910 | struct fuse_out_header oh; |
| 911 | struct fuse_copy_state cs; |
Badari Pulavarty | ee0b3e6 | 2006-09-30 23:28:47 -0700 | [diff] [blame] | 912 | struct fuse_conn *fc = fuse_get_conn(iocb->ki_filp); |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 913 | if (!fc) |
Miklos Szeredi | a87046d | 2006-04-10 22:54:56 -0700 | [diff] [blame] | 914 | return -EPERM; |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 915 | |
Miklos Szeredi | d713311 | 2006-04-10 22:54:55 -0700 | [diff] [blame] | 916 | fuse_copy_init(&cs, fc, 0, NULL, iov, nr_segs); |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 917 | if (nbytes < sizeof(struct fuse_out_header)) |
| 918 | return -EINVAL; |
| 919 | |
| 920 | err = fuse_copy_one(&cs, &oh, sizeof(oh)); |
| 921 | if (err) |
| 922 | goto err_finish; |
Tejun Heo | 8599396 | 2008-11-26 12:03:55 +0100 | [diff] [blame] | 923 | |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 924 | err = -EINVAL; |
Tejun Heo | 8599396 | 2008-11-26 12:03:55 +0100 | [diff] [blame] | 925 | if (oh.len != nbytes) |
| 926 | goto err_finish; |
| 927 | |
| 928 | /* |
| 929 | * Zero oh.unique indicates unsolicited notification message |
| 930 | * and error contains notification code. |
| 931 | */ |
| 932 | if (!oh.unique) { |
| 933 | err = fuse_notify(fc, oh.error, nbytes - sizeof(oh), &cs); |
Tejun Heo | 8599396 | 2008-11-26 12:03:55 +0100 | [diff] [blame] | 934 | return err ? err : nbytes; |
| 935 | } |
| 936 | |
| 937 | err = -EINVAL; |
| 938 | if (oh.error <= -1000 || oh.error > 0) |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 939 | goto err_finish; |
| 940 | |
Miklos Szeredi | d713311 | 2006-04-10 22:54:55 -0700 | [diff] [blame] | 941 | spin_lock(&fc->lock); |
Miklos Szeredi | 69a53bf | 2006-01-16 22:14:41 -0800 | [diff] [blame] | 942 | err = -ENOENT; |
| 943 | if (!fc->connected) |
| 944 | goto err_unlock; |
| 945 | |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 946 | req = request_find(fc, oh.unique); |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 947 | if (!req) |
| 948 | goto err_unlock; |
| 949 | |
Miklos Szeredi | f9a2842 | 2006-06-25 05:48:53 -0700 | [diff] [blame] | 950 | if (req->aborted) { |
Miklos Szeredi | d713311 | 2006-04-10 22:54:55 -0700 | [diff] [blame] | 951 | spin_unlock(&fc->lock); |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 952 | fuse_copy_finish(&cs); |
Miklos Szeredi | d713311 | 2006-04-10 22:54:55 -0700 | [diff] [blame] | 953 | spin_lock(&fc->lock); |
Miklos Szeredi | 222f1d6 | 2006-01-16 22:14:25 -0800 | [diff] [blame] | 954 | request_end(fc, req); |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 955 | return -ENOENT; |
| 956 | } |
Miklos Szeredi | a4d27e7 | 2006-06-25 05:48:54 -0700 | [diff] [blame] | 957 | /* Is it an interrupt reply? */ |
| 958 | if (req->intr_unique == oh.unique) { |
| 959 | err = -EINVAL; |
| 960 | if (nbytes != sizeof(struct fuse_out_header)) |
| 961 | goto err_unlock; |
| 962 | |
| 963 | if (oh.error == -ENOSYS) |
| 964 | fc->no_interrupt = 1; |
| 965 | else if (oh.error == -EAGAIN) |
| 966 | queue_interrupt(fc, req); |
| 967 | |
| 968 | spin_unlock(&fc->lock); |
| 969 | fuse_copy_finish(&cs); |
| 970 | return nbytes; |
| 971 | } |
| 972 | |
| 973 | req->state = FUSE_REQ_WRITING; |
Miklos Szeredi | d77a1d5 | 2006-01-16 22:14:31 -0800 | [diff] [blame] | 974 | list_move(&req->list, &fc->io); |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 975 | req->out.h = oh; |
| 976 | req->locked = 1; |
| 977 | cs.req = req; |
Miklos Szeredi | d713311 | 2006-04-10 22:54:55 -0700 | [diff] [blame] | 978 | spin_unlock(&fc->lock); |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 979 | |
| 980 | err = copy_out_args(&cs, &req->out, nbytes); |
| 981 | fuse_copy_finish(&cs); |
| 982 | |
Miklos Szeredi | d713311 | 2006-04-10 22:54:55 -0700 | [diff] [blame] | 983 | spin_lock(&fc->lock); |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 984 | req->locked = 0; |
| 985 | if (!err) { |
Miklos Szeredi | f9a2842 | 2006-06-25 05:48:53 -0700 | [diff] [blame] | 986 | if (req->aborted) |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 987 | err = -ENOENT; |
Miklos Szeredi | f9a2842 | 2006-06-25 05:48:53 -0700 | [diff] [blame] | 988 | } else if (!req->aborted) |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 989 | req->out.h.error = -EIO; |
| 990 | request_end(fc, req); |
| 991 | |
| 992 | return err ? err : nbytes; |
| 993 | |
| 994 | err_unlock: |
Miklos Szeredi | d713311 | 2006-04-10 22:54:55 -0700 | [diff] [blame] | 995 | spin_unlock(&fc->lock); |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 996 | err_finish: |
| 997 | fuse_copy_finish(&cs); |
| 998 | return err; |
| 999 | } |
| 1000 | |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 1001 | static unsigned fuse_dev_poll(struct file *file, poll_table *wait) |
| 1002 | { |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 1003 | unsigned mask = POLLOUT | POLLWRNORM; |
Miklos Szeredi | 7025d9a | 2006-04-10 22:54:50 -0700 | [diff] [blame] | 1004 | struct fuse_conn *fc = fuse_get_conn(file); |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 1005 | if (!fc) |
Miklos Szeredi | 7025d9a | 2006-04-10 22:54:50 -0700 | [diff] [blame] | 1006 | return POLLERR; |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 1007 | |
| 1008 | poll_wait(file, &fc->waitq, wait); |
| 1009 | |
Miklos Szeredi | d713311 | 2006-04-10 22:54:55 -0700 | [diff] [blame] | 1010 | spin_lock(&fc->lock); |
Miklos Szeredi | 7025d9a | 2006-04-10 22:54:50 -0700 | [diff] [blame] | 1011 | if (!fc->connected) |
| 1012 | mask = POLLERR; |
Miklos Szeredi | a4d27e7 | 2006-06-25 05:48:54 -0700 | [diff] [blame] | 1013 | else if (request_pending(fc)) |
Miklos Szeredi | 7025d9a | 2006-04-10 22:54:50 -0700 | [diff] [blame] | 1014 | mask |= POLLIN | POLLRDNORM; |
Miklos Szeredi | d713311 | 2006-04-10 22:54:55 -0700 | [diff] [blame] | 1015 | spin_unlock(&fc->lock); |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 1016 | |
| 1017 | return mask; |
| 1018 | } |
| 1019 | |
Miklos Szeredi | 69a53bf | 2006-01-16 22:14:41 -0800 | [diff] [blame] | 1020 | /* |
| 1021 | * Abort all requests on the given list (pending or processing) |
| 1022 | * |
Miklos Szeredi | d713311 | 2006-04-10 22:54:55 -0700 | [diff] [blame] | 1023 | * This function releases and reacquires fc->lock |
Miklos Szeredi | 69a53bf | 2006-01-16 22:14:41 -0800 | [diff] [blame] | 1024 | */ |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 1025 | static void end_requests(struct fuse_conn *fc, struct list_head *head) |
Harvey Harrison | 5d9ec85 | 2008-12-02 14:49:42 +0100 | [diff] [blame] | 1026 | __releases(&fc->lock) |
| 1027 | __acquires(&fc->lock) |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 1028 | { |
| 1029 | while (!list_empty(head)) { |
| 1030 | struct fuse_req *req; |
| 1031 | req = list_entry(head->next, struct fuse_req, list); |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 1032 | req->out.h.error = -ECONNABORTED; |
| 1033 | request_end(fc, req); |
Miklos Szeredi | d713311 | 2006-04-10 22:54:55 -0700 | [diff] [blame] | 1034 | spin_lock(&fc->lock); |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 1035 | } |
| 1036 | } |
| 1037 | |
Miklos Szeredi | 69a53bf | 2006-01-16 22:14:41 -0800 | [diff] [blame] | 1038 | /* |
| 1039 | * Abort requests under I/O |
| 1040 | * |
Miklos Szeredi | f9a2842 | 2006-06-25 05:48:53 -0700 | [diff] [blame] | 1041 | * The requests are set to aborted and finished, and the request |
Miklos Szeredi | 69a53bf | 2006-01-16 22:14:41 -0800 | [diff] [blame] | 1042 | * waiter is woken up. This will make request_wait_answer() wait |
| 1043 | * until the request is unlocked and then return. |
Miklos Szeredi | 64c6d8e | 2006-01-16 22:14:42 -0800 | [diff] [blame] | 1044 | * |
| 1045 | * If the request is asynchronous, then the end function needs to be |
| 1046 | * called after waiting for the request to be unlocked (if it was |
| 1047 | * locked). |
Miklos Szeredi | 69a53bf | 2006-01-16 22:14:41 -0800 | [diff] [blame] | 1048 | */ |
| 1049 | static void end_io_requests(struct fuse_conn *fc) |
Harvey Harrison | 5d9ec85 | 2008-12-02 14:49:42 +0100 | [diff] [blame] | 1050 | __releases(&fc->lock) |
| 1051 | __acquires(&fc->lock) |
Miklos Szeredi | 69a53bf | 2006-01-16 22:14:41 -0800 | [diff] [blame] | 1052 | { |
| 1053 | while (!list_empty(&fc->io)) { |
Miklos Szeredi | 64c6d8e | 2006-01-16 22:14:42 -0800 | [diff] [blame] | 1054 | struct fuse_req *req = |
| 1055 | list_entry(fc->io.next, struct fuse_req, list); |
| 1056 | void (*end) (struct fuse_conn *, struct fuse_req *) = req->end; |
| 1057 | |
Miklos Szeredi | f9a2842 | 2006-06-25 05:48:53 -0700 | [diff] [blame] | 1058 | req->aborted = 1; |
Miklos Szeredi | 69a53bf | 2006-01-16 22:14:41 -0800 | [diff] [blame] | 1059 | req->out.h.error = -ECONNABORTED; |
| 1060 | req->state = FUSE_REQ_FINISHED; |
| 1061 | list_del_init(&req->list); |
| 1062 | wake_up(&req->waitq); |
Miklos Szeredi | 64c6d8e | 2006-01-16 22:14:42 -0800 | [diff] [blame] | 1063 | if (end) { |
| 1064 | req->end = NULL; |
Miklos Szeredi | 64c6d8e | 2006-01-16 22:14:42 -0800 | [diff] [blame] | 1065 | __fuse_get_request(req); |
Miklos Szeredi | d713311 | 2006-04-10 22:54:55 -0700 | [diff] [blame] | 1066 | spin_unlock(&fc->lock); |
Miklos Szeredi | 64c6d8e | 2006-01-16 22:14:42 -0800 | [diff] [blame] | 1067 | wait_event(req->waitq, !req->locked); |
| 1068 | end(fc, req); |
Tejun Heo | e9bb09d | 2008-11-26 12:03:54 +0100 | [diff] [blame] | 1069 | fuse_put_request(fc, req); |
Miklos Szeredi | d713311 | 2006-04-10 22:54:55 -0700 | [diff] [blame] | 1070 | spin_lock(&fc->lock); |
Miklos Szeredi | 64c6d8e | 2006-01-16 22:14:42 -0800 | [diff] [blame] | 1071 | } |
Miklos Szeredi | 69a53bf | 2006-01-16 22:14:41 -0800 | [diff] [blame] | 1072 | } |
| 1073 | } |
| 1074 | |
| 1075 | /* |
| 1076 | * Abort all requests. |
| 1077 | * |
| 1078 | * Emergency exit in case of a malicious or accidental deadlock, or |
| 1079 | * just a hung filesystem. |
| 1080 | * |
| 1081 | * The same effect is usually achievable through killing the |
| 1082 | * filesystem daemon and all users of the filesystem. The exception |
| 1083 | * is the combination of an asynchronous request and the tricky |
| 1084 | * deadlock (see Documentation/filesystems/fuse.txt). |
| 1085 | * |
| 1086 | * During the aborting, progression of requests from the pending and |
| 1087 | * processing lists onto the io list, and progression of new requests |
| 1088 | * onto the pending list is prevented by req->connected being false. |
| 1089 | * |
| 1090 | * Progression of requests under I/O to the processing list is |
Miklos Szeredi | f9a2842 | 2006-06-25 05:48:53 -0700 | [diff] [blame] | 1091 | * prevented by the req->aborted flag being true for these requests. |
| 1092 | * For this reason requests on the io list must be aborted first. |
Miklos Szeredi | 69a53bf | 2006-01-16 22:14:41 -0800 | [diff] [blame] | 1093 | */ |
| 1094 | void fuse_abort_conn(struct fuse_conn *fc) |
| 1095 | { |
Miklos Szeredi | d713311 | 2006-04-10 22:54:55 -0700 | [diff] [blame] | 1096 | spin_lock(&fc->lock); |
Miklos Szeredi | 69a53bf | 2006-01-16 22:14:41 -0800 | [diff] [blame] | 1097 | if (fc->connected) { |
| 1098 | fc->connected = 0; |
Miklos Szeredi | 51eb01e | 2006-06-25 05:48:50 -0700 | [diff] [blame] | 1099 | fc->blocked = 0; |
Miklos Szeredi | 69a53bf | 2006-01-16 22:14:41 -0800 | [diff] [blame] | 1100 | end_io_requests(fc); |
| 1101 | end_requests(fc, &fc->pending); |
| 1102 | end_requests(fc, &fc->processing); |
| 1103 | wake_up_all(&fc->waitq); |
Miklos Szeredi | 51eb01e | 2006-06-25 05:48:50 -0700 | [diff] [blame] | 1104 | wake_up_all(&fc->blocked_waitq); |
Jeff Dike | 385a17b | 2006-04-10 22:54:52 -0700 | [diff] [blame] | 1105 | kill_fasync(&fc->fasync, SIGIO, POLL_IN); |
Miklos Szeredi | 69a53bf | 2006-01-16 22:14:41 -0800 | [diff] [blame] | 1106 | } |
Miklos Szeredi | d713311 | 2006-04-10 22:54:55 -0700 | [diff] [blame] | 1107 | spin_unlock(&fc->lock); |
Miklos Szeredi | 69a53bf | 2006-01-16 22:14:41 -0800 | [diff] [blame] | 1108 | } |
| 1109 | |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 1110 | static int fuse_dev_release(struct inode *inode, struct file *file) |
| 1111 | { |
Miklos Szeredi | 0720b31 | 2006-04-10 22:54:55 -0700 | [diff] [blame] | 1112 | struct fuse_conn *fc = fuse_get_conn(file); |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 1113 | if (fc) { |
Miklos Szeredi | d713311 | 2006-04-10 22:54:55 -0700 | [diff] [blame] | 1114 | spin_lock(&fc->lock); |
Miklos Szeredi | 1e9a4ed | 2005-09-09 13:10:31 -0700 | [diff] [blame] | 1115 | fc->connected = 0; |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 1116 | end_requests(fc, &fc->pending); |
| 1117 | end_requests(fc, &fc->processing); |
Miklos Szeredi | d713311 | 2006-04-10 22:54:55 -0700 | [diff] [blame] | 1118 | spin_unlock(&fc->lock); |
Miklos Szeredi | bafa965 | 2006-06-25 05:48:51 -0700 | [diff] [blame] | 1119 | fuse_conn_put(fc); |
Jeff Dike | 385a17b | 2006-04-10 22:54:52 -0700 | [diff] [blame] | 1120 | } |
Miklos Szeredi | f543f25 | 2006-01-16 22:14:35 -0800 | [diff] [blame] | 1121 | |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 1122 | return 0; |
| 1123 | } |
| 1124 | |
Jeff Dike | 385a17b | 2006-04-10 22:54:52 -0700 | [diff] [blame] | 1125 | static int fuse_dev_fasync(int fd, struct file *file, int on) |
| 1126 | { |
| 1127 | struct fuse_conn *fc = fuse_get_conn(file); |
| 1128 | if (!fc) |
Miklos Szeredi | a87046d | 2006-04-10 22:54:56 -0700 | [diff] [blame] | 1129 | return -EPERM; |
Jeff Dike | 385a17b | 2006-04-10 22:54:52 -0700 | [diff] [blame] | 1130 | |
| 1131 | /* No locking - fasync_helper does its own locking */ |
| 1132 | return fasync_helper(fd, file, on, &fc->fasync); |
| 1133 | } |
| 1134 | |
Arjan van de Ven | 4b6f5d2 | 2006-03-28 01:56:42 -0800 | [diff] [blame] | 1135 | const struct file_operations fuse_dev_operations = { |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 1136 | .owner = THIS_MODULE, |
| 1137 | .llseek = no_llseek, |
Badari Pulavarty | ee0b3e6 | 2006-09-30 23:28:47 -0700 | [diff] [blame] | 1138 | .read = do_sync_read, |
| 1139 | .aio_read = fuse_dev_read, |
| 1140 | .write = do_sync_write, |
| 1141 | .aio_write = fuse_dev_write, |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 1142 | .poll = fuse_dev_poll, |
| 1143 | .release = fuse_dev_release, |
Jeff Dike | 385a17b | 2006-04-10 22:54:52 -0700 | [diff] [blame] | 1144 | .fasync = fuse_dev_fasync, |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 1145 | }; |
| 1146 | |
| 1147 | static struct miscdevice fuse_miscdevice = { |
| 1148 | .minor = FUSE_MINOR, |
| 1149 | .name = "fuse", |
| 1150 | .fops = &fuse_dev_operations, |
| 1151 | }; |
| 1152 | |
| 1153 | int __init fuse_dev_init(void) |
| 1154 | { |
| 1155 | int err = -ENOMEM; |
| 1156 | fuse_req_cachep = kmem_cache_create("fuse_request", |
| 1157 | sizeof(struct fuse_req), |
Paul Mundt | 20c2df8 | 2007-07-20 10:11:58 +0900 | [diff] [blame] | 1158 | 0, 0, NULL); |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 1159 | if (!fuse_req_cachep) |
| 1160 | goto out; |
| 1161 | |
| 1162 | err = misc_register(&fuse_miscdevice); |
| 1163 | if (err) |
| 1164 | goto out_cache_clean; |
| 1165 | |
| 1166 | return 0; |
| 1167 | |
| 1168 | out_cache_clean: |
| 1169 | kmem_cache_destroy(fuse_req_cachep); |
| 1170 | out: |
| 1171 | return err; |
| 1172 | } |
| 1173 | |
| 1174 | void fuse_dev_cleanup(void) |
| 1175 | { |
| 1176 | misc_deregister(&fuse_miscdevice); |
| 1177 | kmem_cache_destroy(fuse_req_cachep); |
| 1178 | } |