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