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