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