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