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