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