Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 1 | /* |
| 2 | FUSE: Filesystem in Userspace |
Miklos Szeredi | 1729a16 | 2008-11-26 12:03:54 +0100 | [diff] [blame] | 3 | Copyright (C) 2001-2008 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 | |
Christoph Lameter | e18b890 | 2006-12-06 20:33:20 -0800 | [diff] [blame] | 22 | static struct kmem_cache *fuse_req_cachep; |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 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); |
Miklos Szeredi | a4d27e7 | 2006-06-25 05:48:54 -0700 | [diff] [blame] | 37 | INIT_LIST_HEAD(&req->intr_entry); |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 38 | init_waitqueue_head(&req->waitq); |
| 39 | atomic_set(&req->count, 1); |
| 40 | } |
| 41 | |
| 42 | struct fuse_req *fuse_request_alloc(void) |
| 43 | { |
Christoph Lameter | e94b176 | 2006-12-06 20:33:17 -0800 | [diff] [blame] | 44 | struct fuse_req *req = kmem_cache_alloc(fuse_req_cachep, GFP_KERNEL); |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 45 | if (req) |
| 46 | fuse_request_init(req); |
| 47 | return req; |
| 48 | } |
| 49 | |
Miklos Szeredi | 3be5a52 | 2008-04-30 00:54:41 -0700 | [diff] [blame] | 50 | struct fuse_req *fuse_request_alloc_nofs(void) |
| 51 | { |
| 52 | struct fuse_req *req = kmem_cache_alloc(fuse_req_cachep, GFP_NOFS); |
| 53 | if (req) |
| 54 | fuse_request_init(req); |
| 55 | return req; |
| 56 | } |
| 57 | |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 58 | void fuse_request_free(struct fuse_req *req) |
| 59 | { |
| 60 | kmem_cache_free(fuse_req_cachep, req); |
| 61 | } |
| 62 | |
Miklos Szeredi | 8bfc016 | 2006-01-16 22:14:28 -0800 | [diff] [blame] | 63 | static void block_sigs(sigset_t *oldset) |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 64 | { |
| 65 | sigset_t mask; |
| 66 | |
| 67 | siginitsetinv(&mask, sigmask(SIGKILL)); |
| 68 | sigprocmask(SIG_BLOCK, &mask, oldset); |
| 69 | } |
| 70 | |
Miklos Szeredi | 8bfc016 | 2006-01-16 22:14:28 -0800 | [diff] [blame] | 71 | static void restore_sigs(sigset_t *oldset) |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 72 | { |
| 73 | sigprocmask(SIG_SETMASK, oldset, NULL); |
| 74 | } |
| 75 | |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 76 | static void __fuse_get_request(struct fuse_req *req) |
| 77 | { |
| 78 | atomic_inc(&req->count); |
| 79 | } |
| 80 | |
| 81 | /* Must be called with > 1 refcount */ |
| 82 | static void __fuse_put_request(struct fuse_req *req) |
| 83 | { |
| 84 | BUG_ON(atomic_read(&req->count) < 2); |
| 85 | atomic_dec(&req->count); |
| 86 | } |
| 87 | |
Miklos Szeredi | 33649c9 | 2006-06-25 05:48:52 -0700 | [diff] [blame] | 88 | static void fuse_req_init_context(struct fuse_req *req) |
| 89 | { |
| 90 | req->in.h.uid = current->fsuid; |
| 91 | req->in.h.gid = current->fsgid; |
| 92 | req->in.h.pid = current->pid; |
| 93 | } |
| 94 | |
Miklos Szeredi | ce1d5a4 | 2006-04-10 22:54:58 -0700 | [diff] [blame] | 95 | struct fuse_req *fuse_get_req(struct fuse_conn *fc) |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 96 | { |
Miklos Szeredi | 08a53cd | 2006-04-10 22:54:59 -0700 | [diff] [blame] | 97 | struct fuse_req *req; |
| 98 | sigset_t oldset; |
Miklos Szeredi | 9bc5ddd | 2006-04-11 21:16:09 +0200 | [diff] [blame] | 99 | int intr; |
Miklos Szeredi | 08a53cd | 2006-04-10 22:54:59 -0700 | [diff] [blame] | 100 | int err; |
| 101 | |
Miklos Szeredi | 9bc5ddd | 2006-04-11 21:16:09 +0200 | [diff] [blame] | 102 | atomic_inc(&fc->num_waiting); |
Miklos Szeredi | 08a53cd | 2006-04-10 22:54:59 -0700 | [diff] [blame] | 103 | block_sigs(&oldset); |
Miklos Szeredi | 9bc5ddd | 2006-04-11 21:16:09 +0200 | [diff] [blame] | 104 | intr = wait_event_interruptible(fc->blocked_waitq, !fc->blocked); |
Miklos Szeredi | 08a53cd | 2006-04-10 22:54:59 -0700 | [diff] [blame] | 105 | restore_sigs(&oldset); |
Miklos Szeredi | 9bc5ddd | 2006-04-11 21:16:09 +0200 | [diff] [blame] | 106 | err = -EINTR; |
| 107 | if (intr) |
| 108 | goto out; |
Miklos Szeredi | 08a53cd | 2006-04-10 22:54:59 -0700 | [diff] [blame] | 109 | |
Miklos Szeredi | 51eb01e | 2006-06-25 05:48:50 -0700 | [diff] [blame] | 110 | err = -ENOTCONN; |
| 111 | if (!fc->connected) |
| 112 | goto out; |
| 113 | |
Miklos Szeredi | 08a53cd | 2006-04-10 22:54:59 -0700 | [diff] [blame] | 114 | req = fuse_request_alloc(); |
Miklos Szeredi | 9bc5ddd | 2006-04-11 21:16:09 +0200 | [diff] [blame] | 115 | err = -ENOMEM; |
Miklos Szeredi | ce1d5a4 | 2006-04-10 22:54:58 -0700 | [diff] [blame] | 116 | if (!req) |
Miklos Szeredi | 9bc5ddd | 2006-04-11 21:16:09 +0200 | [diff] [blame] | 117 | goto out; |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 118 | |
Miklos Szeredi | 33649c9 | 2006-06-25 05:48:52 -0700 | [diff] [blame] | 119 | fuse_req_init_context(req); |
Miklos Szeredi | 9bc5ddd | 2006-04-11 21:16:09 +0200 | [diff] [blame] | 120 | req->waiting = 1; |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 121 | return req; |
Miklos Szeredi | 9bc5ddd | 2006-04-11 21:16:09 +0200 | [diff] [blame] | 122 | |
| 123 | out: |
| 124 | atomic_dec(&fc->num_waiting); |
| 125 | return ERR_PTR(err); |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 126 | } |
| 127 | |
Miklos Szeredi | 33649c9 | 2006-06-25 05:48:52 -0700 | [diff] [blame] | 128 | /* |
| 129 | * Return request in fuse_file->reserved_req. However that may |
| 130 | * currently be in use. If that is the case, wait for it to become |
| 131 | * available. |
| 132 | */ |
| 133 | static struct fuse_req *get_reserved_req(struct fuse_conn *fc, |
| 134 | struct file *file) |
| 135 | { |
| 136 | struct fuse_req *req = NULL; |
| 137 | struct fuse_file *ff = file->private_data; |
| 138 | |
| 139 | do { |
Miklos Szeredi | de5e3de | 2007-10-16 23:31:00 -0700 | [diff] [blame] | 140 | wait_event(fc->reserved_req_waitq, ff->reserved_req); |
Miklos Szeredi | 33649c9 | 2006-06-25 05:48:52 -0700 | [diff] [blame] | 141 | spin_lock(&fc->lock); |
| 142 | if (ff->reserved_req) { |
| 143 | req = ff->reserved_req; |
| 144 | ff->reserved_req = NULL; |
| 145 | get_file(file); |
| 146 | req->stolen_file = file; |
| 147 | } |
| 148 | spin_unlock(&fc->lock); |
| 149 | } while (!req); |
| 150 | |
| 151 | return req; |
| 152 | } |
| 153 | |
| 154 | /* |
| 155 | * Put stolen request back into fuse_file->reserved_req |
| 156 | */ |
| 157 | static void put_reserved_req(struct fuse_conn *fc, struct fuse_req *req) |
| 158 | { |
| 159 | struct file *file = req->stolen_file; |
| 160 | struct fuse_file *ff = file->private_data; |
| 161 | |
| 162 | spin_lock(&fc->lock); |
| 163 | fuse_request_init(req); |
| 164 | BUG_ON(ff->reserved_req); |
| 165 | ff->reserved_req = req; |
Miklos Szeredi | de5e3de | 2007-10-16 23:31:00 -0700 | [diff] [blame] | 166 | wake_up_all(&fc->reserved_req_waitq); |
Miklos Szeredi | 33649c9 | 2006-06-25 05:48:52 -0700 | [diff] [blame] | 167 | spin_unlock(&fc->lock); |
| 168 | fput(file); |
| 169 | } |
| 170 | |
| 171 | /* |
| 172 | * Gets a requests for a file operation, always succeeds |
| 173 | * |
| 174 | * This is used for sending the FLUSH request, which must get to |
| 175 | * userspace, due to POSIX locks which may need to be unlocked. |
| 176 | * |
| 177 | * If allocation fails due to OOM, use the reserved request in |
| 178 | * fuse_file. |
| 179 | * |
| 180 | * This is very unlikely to deadlock accidentally, since the |
| 181 | * filesystem should not have it's own file open. If deadlock is |
| 182 | * intentional, it can still be broken by "aborting" the filesystem. |
| 183 | */ |
| 184 | struct fuse_req *fuse_get_req_nofail(struct fuse_conn *fc, struct file *file) |
| 185 | { |
| 186 | struct fuse_req *req; |
| 187 | |
| 188 | atomic_inc(&fc->num_waiting); |
| 189 | wait_event(fc->blocked_waitq, !fc->blocked); |
| 190 | req = fuse_request_alloc(); |
| 191 | if (!req) |
| 192 | req = get_reserved_req(fc, file); |
| 193 | |
| 194 | fuse_req_init_context(req); |
| 195 | req->waiting = 1; |
| 196 | return req; |
| 197 | } |
| 198 | |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 199 | void fuse_put_request(struct fuse_conn *fc, struct fuse_req *req) |
| 200 | { |
Miklos Szeredi | 7128ec2 | 2006-02-04 23:27:40 -0800 | [diff] [blame] | 201 | if (atomic_dec_and_test(&req->count)) { |
Miklos Szeredi | 9bc5ddd | 2006-04-11 21:16:09 +0200 | [diff] [blame] | 202 | if (req->waiting) |
| 203 | atomic_dec(&fc->num_waiting); |
Miklos Szeredi | 33649c9 | 2006-06-25 05:48:52 -0700 | [diff] [blame] | 204 | |
| 205 | if (req->stolen_file) |
| 206 | put_reserved_req(fc, req); |
| 207 | else |
| 208 | fuse_request_free(req); |
Miklos Szeredi | 7128ec2 | 2006-02-04 23:27:40 -0800 | [diff] [blame] | 209 | } |
| 210 | } |
| 211 | |
Miklos Szeredi | d12def1 | 2008-02-06 01:38:39 -0800 | [diff] [blame] | 212 | static unsigned len_args(unsigned numargs, struct fuse_arg *args) |
| 213 | { |
| 214 | unsigned nbytes = 0; |
| 215 | unsigned i; |
| 216 | |
| 217 | for (i = 0; i < numargs; i++) |
| 218 | nbytes += args[i].size; |
| 219 | |
| 220 | return nbytes; |
| 221 | } |
| 222 | |
| 223 | static u64 fuse_get_unique(struct fuse_conn *fc) |
| 224 | { |
| 225 | fc->reqctr++; |
| 226 | /* zero is special */ |
| 227 | if (fc->reqctr == 0) |
| 228 | fc->reqctr = 1; |
| 229 | |
| 230 | return fc->reqctr; |
| 231 | } |
| 232 | |
| 233 | static void queue_request(struct fuse_conn *fc, struct fuse_req *req) |
| 234 | { |
| 235 | req->in.h.unique = fuse_get_unique(fc); |
| 236 | req->in.h.len = sizeof(struct fuse_in_header) + |
| 237 | len_args(req->in.numargs, (struct fuse_arg *) req->in.args); |
| 238 | list_add_tail(&req->list, &fc->pending); |
| 239 | req->state = FUSE_REQ_PENDING; |
| 240 | if (!req->waiting) { |
| 241 | req->waiting = 1; |
| 242 | atomic_inc(&fc->num_waiting); |
| 243 | } |
| 244 | wake_up(&fc->waitq); |
| 245 | kill_fasync(&fc->fasync, SIGIO, POLL_IN); |
| 246 | } |
| 247 | |
| 248 | static void flush_bg_queue(struct fuse_conn *fc) |
| 249 | { |
| 250 | while (fc->active_background < FUSE_MAX_BACKGROUND && |
| 251 | !list_empty(&fc->bg_queue)) { |
| 252 | struct fuse_req *req; |
| 253 | |
| 254 | req = list_entry(fc->bg_queue.next, struct fuse_req, list); |
| 255 | list_del(&req->list); |
| 256 | fc->active_background++; |
| 257 | queue_request(fc, req); |
| 258 | } |
| 259 | } |
| 260 | |
Miklos Szeredi | 6dbbcb1 | 2006-04-26 10:49:06 +0200 | [diff] [blame] | 261 | /* |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 262 | * This function is called when a request is finished. Either a reply |
Miklos Szeredi | f9a2842 | 2006-06-25 05:48:53 -0700 | [diff] [blame] | 263 | * has arrived or it was aborted (and not yet sent) or some error |
Miklos Szeredi | f43b155 | 2006-01-16 22:14:26 -0800 | [diff] [blame] | 264 | * occurred during communication with userspace, or the device file |
Miklos Szeredi | 51eb01e | 2006-06-25 05:48:50 -0700 | [diff] [blame] | 265 | * was closed. The requester thread is woken up (if still waiting), |
| 266 | * the 'end' callback is called if given, else the reference to the |
| 267 | * request is released |
Miklos Szeredi | 7128ec2 | 2006-02-04 23:27:40 -0800 | [diff] [blame] | 268 | * |
Miklos Szeredi | d713311 | 2006-04-10 22:54:55 -0700 | [diff] [blame] | 269 | * Called with fc->lock, unlocks it |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 270 | */ |
| 271 | static void request_end(struct fuse_conn *fc, struct fuse_req *req) |
Josh Triplett | 105f4d7 | 2006-09-29 01:59:25 -0700 | [diff] [blame] | 272 | __releases(fc->lock) |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 273 | { |
Miklos Szeredi | 51eb01e | 2006-06-25 05:48:50 -0700 | [diff] [blame] | 274 | void (*end) (struct fuse_conn *, struct fuse_req *) = req->end; |
| 275 | req->end = NULL; |
Miklos Szeredi | d77a1d5 | 2006-01-16 22:14:31 -0800 | [diff] [blame] | 276 | list_del(&req->list); |
Miklos Szeredi | a4d27e7 | 2006-06-25 05:48:54 -0700 | [diff] [blame] | 277 | list_del(&req->intr_entry); |
Miklos Szeredi | 83cfd49 | 2006-01-16 22:14:31 -0800 | [diff] [blame] | 278 | req->state = FUSE_REQ_FINISHED; |
Miklos Szeredi | 51eb01e | 2006-06-25 05:48:50 -0700 | [diff] [blame] | 279 | if (req->background) { |
| 280 | if (fc->num_background == FUSE_MAX_BACKGROUND) { |
| 281 | fc->blocked = 0; |
| 282 | wake_up_all(&fc->blocked_waitq); |
| 283 | } |
Miklos Szeredi | f92b99b | 2007-10-16 23:30:59 -0700 | [diff] [blame] | 284 | if (fc->num_background == FUSE_CONGESTION_THRESHOLD) { |
| 285 | clear_bdi_congested(&fc->bdi, READ); |
| 286 | clear_bdi_congested(&fc->bdi, WRITE); |
| 287 | } |
Miklos Szeredi | 51eb01e | 2006-06-25 05:48:50 -0700 | [diff] [blame] | 288 | fc->num_background--; |
Miklos Szeredi | d12def1 | 2008-02-06 01:38:39 -0800 | [diff] [blame] | 289 | fc->active_background--; |
| 290 | flush_bg_queue(fc); |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 291 | } |
Miklos Szeredi | 51eb01e | 2006-06-25 05:48:50 -0700 | [diff] [blame] | 292 | spin_unlock(&fc->lock); |
Miklos Szeredi | 51eb01e | 2006-06-25 05:48:50 -0700 | [diff] [blame] | 293 | wake_up(&req->waitq); |
| 294 | if (end) |
| 295 | end(fc, req); |
Tejun Heo | e9bb09d | 2008-11-26 12:03:54 +0100 | [diff] [blame^] | 296 | fuse_put_request(fc, req); |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 297 | } |
| 298 | |
Miklos Szeredi | a4d27e7 | 2006-06-25 05:48:54 -0700 | [diff] [blame] | 299 | static void wait_answer_interruptible(struct fuse_conn *fc, |
| 300 | struct fuse_req *req) |
Miklos Szeredi | 4dbf930 | 2008-04-30 00:54:45 -0700 | [diff] [blame] | 301 | __releases(fc->lock) __acquires(fc->lock) |
Miklos Szeredi | a4d27e7 | 2006-06-25 05:48:54 -0700 | [diff] [blame] | 302 | { |
| 303 | if (signal_pending(current)) |
| 304 | return; |
| 305 | |
| 306 | spin_unlock(&fc->lock); |
| 307 | wait_event_interruptible(req->waitq, req->state == FUSE_REQ_FINISHED); |
| 308 | spin_lock(&fc->lock); |
| 309 | } |
| 310 | |
| 311 | static void queue_interrupt(struct fuse_conn *fc, struct fuse_req *req) |
| 312 | { |
| 313 | list_add_tail(&req->intr_entry, &fc->interrupts); |
| 314 | wake_up(&fc->waitq); |
| 315 | kill_fasync(&fc->fasync, SIGIO, POLL_IN); |
| 316 | } |
| 317 | |
Miklos Szeredi | 7c352bd | 2005-09-09 13:10:39 -0700 | [diff] [blame] | 318 | static void request_wait_answer(struct fuse_conn *fc, struct fuse_req *req) |
Miklos Szeredi | 4dbf930 | 2008-04-30 00:54:45 -0700 | [diff] [blame] | 319 | __releases(fc->lock) __acquires(fc->lock) |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 320 | { |
Miklos Szeredi | a4d27e7 | 2006-06-25 05:48:54 -0700 | [diff] [blame] | 321 | if (!fc->no_interrupt) { |
| 322 | /* Any signal may interrupt this */ |
| 323 | wait_answer_interruptible(fc, req); |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 324 | |
Miklos Szeredi | a4d27e7 | 2006-06-25 05:48:54 -0700 | [diff] [blame] | 325 | if (req->aborted) |
| 326 | goto aborted; |
| 327 | if (req->state == FUSE_REQ_FINISHED) |
| 328 | return; |
| 329 | |
| 330 | req->interrupted = 1; |
| 331 | if (req->state == FUSE_REQ_SENT) |
| 332 | queue_interrupt(fc, req); |
| 333 | } |
| 334 | |
Miklos Szeredi | a131de0 | 2007-10-16 23:31:04 -0700 | [diff] [blame] | 335 | if (!req->force) { |
Miklos Szeredi | a4d27e7 | 2006-06-25 05:48:54 -0700 | [diff] [blame] | 336 | sigset_t oldset; |
| 337 | |
| 338 | /* Only fatal signals may interrupt this */ |
Miklos Szeredi | 51eb01e | 2006-06-25 05:48:50 -0700 | [diff] [blame] | 339 | block_sigs(&oldset); |
Miklos Szeredi | a4d27e7 | 2006-06-25 05:48:54 -0700 | [diff] [blame] | 340 | wait_answer_interruptible(fc, req); |
Miklos Szeredi | 51eb01e | 2006-06-25 05:48:50 -0700 | [diff] [blame] | 341 | restore_sigs(&oldset); |
Miklos Szeredi | a131de0 | 2007-10-16 23:31:04 -0700 | [diff] [blame] | 342 | |
| 343 | if (req->aborted) |
| 344 | goto aborted; |
| 345 | if (req->state == FUSE_REQ_FINISHED) |
| 346 | return; |
| 347 | |
| 348 | /* Request is not yet in userspace, bail out */ |
| 349 | if (req->state == FUSE_REQ_PENDING) { |
| 350 | list_del(&req->list); |
| 351 | __fuse_put_request(req); |
| 352 | req->out.h.error = -EINTR; |
| 353 | return; |
| 354 | } |
Miklos Szeredi | 51eb01e | 2006-06-25 05:48:50 -0700 | [diff] [blame] | 355 | } |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 356 | |
Miklos Szeredi | a131de0 | 2007-10-16 23:31:04 -0700 | [diff] [blame] | 357 | /* |
| 358 | * Either request is already in userspace, or it was forced. |
| 359 | * Wait it out. |
| 360 | */ |
| 361 | spin_unlock(&fc->lock); |
| 362 | wait_event(req->waitq, req->state == FUSE_REQ_FINISHED); |
| 363 | spin_lock(&fc->lock); |
Miklos Szeredi | a4d27e7 | 2006-06-25 05:48:54 -0700 | [diff] [blame] | 364 | |
Miklos Szeredi | a131de0 | 2007-10-16 23:31:04 -0700 | [diff] [blame] | 365 | if (!req->aborted) |
| 366 | return; |
Miklos Szeredi | a4d27e7 | 2006-06-25 05:48:54 -0700 | [diff] [blame] | 367 | |
| 368 | aborted: |
Miklos Szeredi | a131de0 | 2007-10-16 23:31:04 -0700 | [diff] [blame] | 369 | BUG_ON(req->state != FUSE_REQ_FINISHED); |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 370 | if (req->locked) { |
| 371 | /* This is uninterruptible sleep, because data is |
| 372 | being copied to/from the buffers of req. During |
| 373 | locked state, there mustn't be any filesystem |
| 374 | operation (e.g. page fault), since that could lead |
| 375 | to deadlock */ |
Miklos Szeredi | d713311 | 2006-04-10 22:54:55 -0700 | [diff] [blame] | 376 | spin_unlock(&fc->lock); |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 377 | wait_event(req->waitq, !req->locked); |
Miklos Szeredi | d713311 | 2006-04-10 22:54:55 -0700 | [diff] [blame] | 378 | spin_lock(&fc->lock); |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 379 | } |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 380 | } |
| 381 | |
Miklos Szeredi | 7c352bd | 2005-09-09 13:10:39 -0700 | [diff] [blame] | 382 | void request_send(struct fuse_conn *fc, struct fuse_req *req) |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 383 | { |
| 384 | req->isreply = 1; |
Miklos Szeredi | d713311 | 2006-04-10 22:54:55 -0700 | [diff] [blame] | 385 | spin_lock(&fc->lock); |
Miklos Szeredi | 1e9a4ed | 2005-09-09 13:10:31 -0700 | [diff] [blame] | 386 | if (!fc->connected) |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 387 | req->out.h.error = -ENOTCONN; |
| 388 | else if (fc->conn_error) |
| 389 | req->out.h.error = -ECONNREFUSED; |
| 390 | else { |
| 391 | queue_request(fc, req); |
| 392 | /* acquire extra reference, since request is still needed |
| 393 | after request_end() */ |
| 394 | __fuse_get_request(req); |
| 395 | |
Miklos Szeredi | 7c352bd | 2005-09-09 13:10:39 -0700 | [diff] [blame] | 396 | request_wait_answer(fc, req); |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 397 | } |
Miklos Szeredi | d713311 | 2006-04-10 22:54:55 -0700 | [diff] [blame] | 398 | spin_unlock(&fc->lock); |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 399 | } |
| 400 | |
Miklos Szeredi | d12def1 | 2008-02-06 01:38:39 -0800 | [diff] [blame] | 401 | static void request_send_nowait_locked(struct fuse_conn *fc, |
| 402 | struct fuse_req *req) |
| 403 | { |
| 404 | req->background = 1; |
| 405 | fc->num_background++; |
| 406 | if (fc->num_background == FUSE_MAX_BACKGROUND) |
| 407 | fc->blocked = 1; |
| 408 | if (fc->num_background == FUSE_CONGESTION_THRESHOLD) { |
| 409 | set_bdi_congested(&fc->bdi, READ); |
| 410 | set_bdi_congested(&fc->bdi, WRITE); |
| 411 | } |
| 412 | list_add_tail(&req->list, &fc->bg_queue); |
| 413 | flush_bg_queue(fc); |
| 414 | } |
| 415 | |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 416 | static void request_send_nowait(struct fuse_conn *fc, struct fuse_req *req) |
| 417 | { |
Miklos Szeredi | d713311 | 2006-04-10 22:54:55 -0700 | [diff] [blame] | 418 | spin_lock(&fc->lock); |
Miklos Szeredi | 1e9a4ed | 2005-09-09 13:10:31 -0700 | [diff] [blame] | 419 | if (fc->connected) { |
Miklos Szeredi | d12def1 | 2008-02-06 01:38:39 -0800 | [diff] [blame] | 420 | request_send_nowait_locked(fc, req); |
Miklos Szeredi | d713311 | 2006-04-10 22:54:55 -0700 | [diff] [blame] | 421 | spin_unlock(&fc->lock); |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 422 | } else { |
| 423 | req->out.h.error = -ENOTCONN; |
| 424 | request_end(fc, req); |
| 425 | } |
| 426 | } |
| 427 | |
| 428 | void request_send_noreply(struct fuse_conn *fc, struct fuse_req *req) |
| 429 | { |
| 430 | req->isreply = 0; |
| 431 | request_send_nowait(fc, req); |
| 432 | } |
| 433 | |
| 434 | void request_send_background(struct fuse_conn *fc, struct fuse_req *req) |
| 435 | { |
| 436 | req->isreply = 1; |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 437 | request_send_nowait(fc, req); |
| 438 | } |
| 439 | |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 440 | /* |
Miklos Szeredi | 3be5a52 | 2008-04-30 00:54:41 -0700 | [diff] [blame] | 441 | * Called under fc->lock |
| 442 | * |
| 443 | * fc->connected must have been checked previously |
| 444 | */ |
| 445 | void request_send_background_locked(struct fuse_conn *fc, struct fuse_req *req) |
| 446 | { |
| 447 | req->isreply = 1; |
| 448 | request_send_nowait_locked(fc, req); |
| 449 | } |
| 450 | |
| 451 | /* |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 452 | * Lock the request. Up to the next unlock_request() there mustn't be |
| 453 | * anything that could cause a page-fault. If the request was already |
Miklos Szeredi | f9a2842 | 2006-06-25 05:48:53 -0700 | [diff] [blame] | 454 | * aborted bail out. |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 455 | */ |
Miklos Szeredi | d713311 | 2006-04-10 22:54:55 -0700 | [diff] [blame] | 456 | static int lock_request(struct fuse_conn *fc, struct fuse_req *req) |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 457 | { |
| 458 | int err = 0; |
| 459 | if (req) { |
Miklos Szeredi | d713311 | 2006-04-10 22:54:55 -0700 | [diff] [blame] | 460 | spin_lock(&fc->lock); |
Miklos Szeredi | f9a2842 | 2006-06-25 05:48:53 -0700 | [diff] [blame] | 461 | if (req->aborted) |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 462 | err = -ENOENT; |
| 463 | else |
| 464 | req->locked = 1; |
Miklos Szeredi | d713311 | 2006-04-10 22:54:55 -0700 | [diff] [blame] | 465 | spin_unlock(&fc->lock); |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 466 | } |
| 467 | return err; |
| 468 | } |
| 469 | |
| 470 | /* |
Miklos Szeredi | f9a2842 | 2006-06-25 05:48:53 -0700 | [diff] [blame] | 471 | * Unlock request. If it was aborted during being locked, the |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 472 | * requester thread is currently waiting for it to be unlocked, so |
| 473 | * wake it up. |
| 474 | */ |
Miklos Szeredi | d713311 | 2006-04-10 22:54:55 -0700 | [diff] [blame] | 475 | static void unlock_request(struct fuse_conn *fc, struct fuse_req *req) |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 476 | { |
| 477 | if (req) { |
Miklos Szeredi | d713311 | 2006-04-10 22:54:55 -0700 | [diff] [blame] | 478 | spin_lock(&fc->lock); |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 479 | req->locked = 0; |
Miklos Szeredi | f9a2842 | 2006-06-25 05:48:53 -0700 | [diff] [blame] | 480 | if (req->aborted) |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 481 | wake_up(&req->waitq); |
Miklos Szeredi | d713311 | 2006-04-10 22:54:55 -0700 | [diff] [blame] | 482 | spin_unlock(&fc->lock); |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 483 | } |
| 484 | } |
| 485 | |
| 486 | struct fuse_copy_state { |
Miklos Szeredi | d713311 | 2006-04-10 22:54:55 -0700 | [diff] [blame] | 487 | struct fuse_conn *fc; |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 488 | int write; |
| 489 | struct fuse_req *req; |
| 490 | const struct iovec *iov; |
| 491 | unsigned long nr_segs; |
| 492 | unsigned long seglen; |
| 493 | unsigned long addr; |
| 494 | struct page *pg; |
| 495 | void *mapaddr; |
| 496 | void *buf; |
| 497 | unsigned len; |
| 498 | }; |
| 499 | |
Miklos Szeredi | d713311 | 2006-04-10 22:54:55 -0700 | [diff] [blame] | 500 | static void fuse_copy_init(struct fuse_copy_state *cs, struct fuse_conn *fc, |
| 501 | int write, struct fuse_req *req, |
| 502 | const struct iovec *iov, unsigned long nr_segs) |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 503 | { |
| 504 | memset(cs, 0, sizeof(*cs)); |
Miklos Szeredi | d713311 | 2006-04-10 22:54:55 -0700 | [diff] [blame] | 505 | cs->fc = fc; |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 506 | cs->write = write; |
| 507 | cs->req = req; |
| 508 | cs->iov = iov; |
| 509 | cs->nr_segs = nr_segs; |
| 510 | } |
| 511 | |
| 512 | /* Unmap and put previous page of userspace buffer */ |
Miklos Szeredi | 8bfc016 | 2006-01-16 22:14:28 -0800 | [diff] [blame] | 513 | static void fuse_copy_finish(struct fuse_copy_state *cs) |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 514 | { |
| 515 | if (cs->mapaddr) { |
| 516 | kunmap_atomic(cs->mapaddr, KM_USER0); |
| 517 | if (cs->write) { |
| 518 | flush_dcache_page(cs->pg); |
| 519 | set_page_dirty_lock(cs->pg); |
| 520 | } |
| 521 | put_page(cs->pg); |
| 522 | cs->mapaddr = NULL; |
| 523 | } |
| 524 | } |
| 525 | |
| 526 | /* |
| 527 | * Get another pagefull of userspace buffer, and map it to kernel |
| 528 | * address space, and lock request |
| 529 | */ |
| 530 | static int fuse_copy_fill(struct fuse_copy_state *cs) |
| 531 | { |
| 532 | unsigned long offset; |
| 533 | int err; |
| 534 | |
Miklos Szeredi | d713311 | 2006-04-10 22:54:55 -0700 | [diff] [blame] | 535 | unlock_request(cs->fc, cs->req); |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 536 | fuse_copy_finish(cs); |
| 537 | if (!cs->seglen) { |
| 538 | BUG_ON(!cs->nr_segs); |
| 539 | cs->seglen = cs->iov[0].iov_len; |
| 540 | cs->addr = (unsigned long) cs->iov[0].iov_base; |
Miklos Szeredi | 1729a16 | 2008-11-26 12:03:54 +0100 | [diff] [blame] | 541 | cs->iov++; |
| 542 | cs->nr_segs--; |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 543 | } |
| 544 | down_read(¤t->mm->mmap_sem); |
| 545 | err = get_user_pages(current, current->mm, cs->addr, 1, cs->write, 0, |
| 546 | &cs->pg, NULL); |
| 547 | up_read(¤t->mm->mmap_sem); |
| 548 | if (err < 0) |
| 549 | return err; |
| 550 | BUG_ON(err != 1); |
| 551 | offset = cs->addr % PAGE_SIZE; |
| 552 | cs->mapaddr = kmap_atomic(cs->pg, KM_USER0); |
| 553 | cs->buf = cs->mapaddr + offset; |
| 554 | cs->len = min(PAGE_SIZE - offset, cs->seglen); |
| 555 | cs->seglen -= cs->len; |
| 556 | cs->addr += cs->len; |
| 557 | |
Miklos Szeredi | d713311 | 2006-04-10 22:54:55 -0700 | [diff] [blame] | 558 | return lock_request(cs->fc, cs->req); |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 559 | } |
| 560 | |
| 561 | /* Do as much copy to/from userspace buffer as we can */ |
Miklos Szeredi | 8bfc016 | 2006-01-16 22:14:28 -0800 | [diff] [blame] | 562 | 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] | 563 | { |
| 564 | unsigned ncpy = min(*size, cs->len); |
| 565 | if (val) { |
| 566 | if (cs->write) |
| 567 | memcpy(cs->buf, *val, ncpy); |
| 568 | else |
| 569 | memcpy(*val, cs->buf, ncpy); |
| 570 | *val += ncpy; |
| 571 | } |
| 572 | *size -= ncpy; |
| 573 | cs->len -= ncpy; |
| 574 | cs->buf += ncpy; |
| 575 | return ncpy; |
| 576 | } |
| 577 | |
| 578 | /* |
| 579 | * Copy a page in the request to/from the userspace buffer. Must be |
| 580 | * done atomically |
| 581 | */ |
Miklos Szeredi | 8bfc016 | 2006-01-16 22:14:28 -0800 | [diff] [blame] | 582 | static int fuse_copy_page(struct fuse_copy_state *cs, struct page *page, |
| 583 | unsigned offset, unsigned count, int zeroing) |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 584 | { |
| 585 | if (page && zeroing && count < PAGE_SIZE) { |
| 586 | void *mapaddr = kmap_atomic(page, KM_USER1); |
| 587 | memset(mapaddr, 0, PAGE_SIZE); |
| 588 | kunmap_atomic(mapaddr, KM_USER1); |
| 589 | } |
| 590 | while (count) { |
Miklos Szeredi | 1729a16 | 2008-11-26 12:03:54 +0100 | [diff] [blame] | 591 | if (!cs->len) { |
| 592 | int err = fuse_copy_fill(cs); |
| 593 | if (err) |
| 594 | return err; |
| 595 | } |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 596 | if (page) { |
| 597 | void *mapaddr = kmap_atomic(page, KM_USER1); |
| 598 | void *buf = mapaddr + offset; |
| 599 | offset += fuse_copy_do(cs, &buf, &count); |
| 600 | kunmap_atomic(mapaddr, KM_USER1); |
| 601 | } else |
| 602 | offset += fuse_copy_do(cs, NULL, &count); |
| 603 | } |
| 604 | if (page && !cs->write) |
| 605 | flush_dcache_page(page); |
| 606 | return 0; |
| 607 | } |
| 608 | |
| 609 | /* Copy pages in the request to/from userspace buffer */ |
| 610 | static int fuse_copy_pages(struct fuse_copy_state *cs, unsigned nbytes, |
| 611 | int zeroing) |
| 612 | { |
| 613 | unsigned i; |
| 614 | struct fuse_req *req = cs->req; |
| 615 | unsigned offset = req->page_offset; |
| 616 | unsigned count = min(nbytes, (unsigned) PAGE_SIZE - offset); |
| 617 | |
| 618 | for (i = 0; i < req->num_pages && (nbytes || zeroing); i++) { |
| 619 | struct page *page = req->pages[i]; |
| 620 | int err = fuse_copy_page(cs, page, offset, count, zeroing); |
| 621 | if (err) |
| 622 | return err; |
| 623 | |
| 624 | nbytes -= count; |
| 625 | count = min(nbytes, (unsigned) PAGE_SIZE); |
| 626 | offset = 0; |
| 627 | } |
| 628 | return 0; |
| 629 | } |
| 630 | |
| 631 | /* Copy a single argument in the request to/from userspace buffer */ |
| 632 | static int fuse_copy_one(struct fuse_copy_state *cs, void *val, unsigned size) |
| 633 | { |
| 634 | while (size) { |
Miklos Szeredi | 1729a16 | 2008-11-26 12:03:54 +0100 | [diff] [blame] | 635 | if (!cs->len) { |
| 636 | int err = fuse_copy_fill(cs); |
| 637 | if (err) |
| 638 | return err; |
| 639 | } |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 640 | fuse_copy_do(cs, &val, &size); |
| 641 | } |
| 642 | return 0; |
| 643 | } |
| 644 | |
| 645 | /* Copy request arguments to/from userspace buffer */ |
| 646 | static int fuse_copy_args(struct fuse_copy_state *cs, unsigned numargs, |
| 647 | unsigned argpages, struct fuse_arg *args, |
| 648 | int zeroing) |
| 649 | { |
| 650 | int err = 0; |
| 651 | unsigned i; |
| 652 | |
| 653 | for (i = 0; !err && i < numargs; i++) { |
| 654 | struct fuse_arg *arg = &args[i]; |
| 655 | if (i == numargs - 1 && argpages) |
| 656 | err = fuse_copy_pages(cs, arg->size, zeroing); |
| 657 | else |
| 658 | err = fuse_copy_one(cs, arg->value, arg->size); |
| 659 | } |
| 660 | return err; |
| 661 | } |
| 662 | |
Miklos Szeredi | a4d27e7 | 2006-06-25 05:48:54 -0700 | [diff] [blame] | 663 | static int request_pending(struct fuse_conn *fc) |
| 664 | { |
| 665 | return !list_empty(&fc->pending) || !list_empty(&fc->interrupts); |
| 666 | } |
| 667 | |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 668 | /* Wait until a request is available on the pending list */ |
| 669 | static void request_wait(struct fuse_conn *fc) |
| 670 | { |
| 671 | DECLARE_WAITQUEUE(wait, current); |
| 672 | |
| 673 | add_wait_queue_exclusive(&fc->waitq, &wait); |
Miklos Szeredi | a4d27e7 | 2006-06-25 05:48:54 -0700 | [diff] [blame] | 674 | while (fc->connected && !request_pending(fc)) { |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 675 | set_current_state(TASK_INTERRUPTIBLE); |
| 676 | if (signal_pending(current)) |
| 677 | break; |
| 678 | |
Miklos Szeredi | d713311 | 2006-04-10 22:54:55 -0700 | [diff] [blame] | 679 | spin_unlock(&fc->lock); |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 680 | schedule(); |
Miklos Szeredi | d713311 | 2006-04-10 22:54:55 -0700 | [diff] [blame] | 681 | spin_lock(&fc->lock); |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 682 | } |
| 683 | set_current_state(TASK_RUNNING); |
| 684 | remove_wait_queue(&fc->waitq, &wait); |
| 685 | } |
| 686 | |
| 687 | /* |
Miklos Szeredi | a4d27e7 | 2006-06-25 05:48:54 -0700 | [diff] [blame] | 688 | * Transfer an interrupt request to userspace |
| 689 | * |
| 690 | * Unlike other requests this is assembled on demand, without a need |
| 691 | * to allocate a separate fuse_req structure. |
| 692 | * |
| 693 | * Called with fc->lock held, releases it |
| 694 | */ |
| 695 | static int fuse_read_interrupt(struct fuse_conn *fc, struct fuse_req *req, |
| 696 | const struct iovec *iov, unsigned long nr_segs) |
Josh Triplett | 105f4d7 | 2006-09-29 01:59:25 -0700 | [diff] [blame] | 697 | __releases(fc->lock) |
Miklos Szeredi | a4d27e7 | 2006-06-25 05:48:54 -0700 | [diff] [blame] | 698 | { |
| 699 | struct fuse_copy_state cs; |
| 700 | struct fuse_in_header ih; |
| 701 | struct fuse_interrupt_in arg; |
| 702 | unsigned reqsize = sizeof(ih) + sizeof(arg); |
| 703 | int err; |
| 704 | |
| 705 | list_del_init(&req->intr_entry); |
| 706 | req->intr_unique = fuse_get_unique(fc); |
| 707 | memset(&ih, 0, sizeof(ih)); |
| 708 | memset(&arg, 0, sizeof(arg)); |
| 709 | ih.len = reqsize; |
| 710 | ih.opcode = FUSE_INTERRUPT; |
| 711 | ih.unique = req->intr_unique; |
| 712 | arg.unique = req->in.h.unique; |
| 713 | |
| 714 | spin_unlock(&fc->lock); |
| 715 | if (iov_length(iov, nr_segs) < reqsize) |
| 716 | return -EINVAL; |
| 717 | |
| 718 | fuse_copy_init(&cs, fc, 1, NULL, iov, nr_segs); |
| 719 | err = fuse_copy_one(&cs, &ih, sizeof(ih)); |
| 720 | if (!err) |
| 721 | err = fuse_copy_one(&cs, &arg, sizeof(arg)); |
| 722 | fuse_copy_finish(&cs); |
| 723 | |
| 724 | return err ? err : reqsize; |
| 725 | } |
| 726 | |
| 727 | /* |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 728 | * Read a single request into the userspace filesystem's buffer. This |
| 729 | * function waits until a request is available, then removes it from |
| 730 | * the pending list and copies request data to userspace buffer. If |
Miklos Szeredi | f9a2842 | 2006-06-25 05:48:53 -0700 | [diff] [blame] | 731 | * no reply is needed (FORGET) or request has been aborted or there |
| 732 | * was an error during the copying then it's finished by calling |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 733 | * request_end(). Otherwise add it to the processing list, and set |
| 734 | * the 'sent' flag. |
| 735 | */ |
Badari Pulavarty | ee0b3e6 | 2006-09-30 23:28:47 -0700 | [diff] [blame] | 736 | static ssize_t fuse_dev_read(struct kiocb *iocb, const struct iovec *iov, |
| 737 | unsigned long nr_segs, loff_t pos) |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 738 | { |
| 739 | int err; |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 740 | struct fuse_req *req; |
| 741 | struct fuse_in *in; |
| 742 | struct fuse_copy_state cs; |
| 743 | unsigned reqsize; |
Badari Pulavarty | ee0b3e6 | 2006-09-30 23:28:47 -0700 | [diff] [blame] | 744 | struct file *file = iocb->ki_filp; |
Miklos Szeredi | 0720b31 | 2006-04-10 22:54:55 -0700 | [diff] [blame] | 745 | struct fuse_conn *fc = fuse_get_conn(file); |
| 746 | if (!fc) |
| 747 | return -EPERM; |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 748 | |
Miklos Szeredi | 1d3d752 | 2006-01-06 00:19:40 -0800 | [diff] [blame] | 749 | restart: |
Miklos Szeredi | d713311 | 2006-04-10 22:54:55 -0700 | [diff] [blame] | 750 | spin_lock(&fc->lock); |
Jeff Dike | e5ac1d1 | 2006-04-10 22:54:53 -0700 | [diff] [blame] | 751 | err = -EAGAIN; |
| 752 | if ((file->f_flags & O_NONBLOCK) && fc->connected && |
Miklos Szeredi | a4d27e7 | 2006-06-25 05:48:54 -0700 | [diff] [blame] | 753 | !request_pending(fc)) |
Jeff Dike | e5ac1d1 | 2006-04-10 22:54:53 -0700 | [diff] [blame] | 754 | goto err_unlock; |
| 755 | |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 756 | request_wait(fc); |
| 757 | err = -ENODEV; |
Miklos Szeredi | 9ba7cbb | 2006-01-16 22:14:34 -0800 | [diff] [blame] | 758 | if (!fc->connected) |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 759 | goto err_unlock; |
| 760 | err = -ERESTARTSYS; |
Miklos Szeredi | a4d27e7 | 2006-06-25 05:48:54 -0700 | [diff] [blame] | 761 | if (!request_pending(fc)) |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 762 | goto err_unlock; |
| 763 | |
Miklos Szeredi | a4d27e7 | 2006-06-25 05:48:54 -0700 | [diff] [blame] | 764 | if (!list_empty(&fc->interrupts)) { |
| 765 | req = list_entry(fc->interrupts.next, struct fuse_req, |
| 766 | intr_entry); |
| 767 | return fuse_read_interrupt(fc, req, iov, nr_segs); |
| 768 | } |
| 769 | |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 770 | req = list_entry(fc->pending.next, struct fuse_req, list); |
Miklos Szeredi | 83cfd49 | 2006-01-16 22:14:31 -0800 | [diff] [blame] | 771 | req->state = FUSE_REQ_READING; |
Miklos Szeredi | d77a1d5 | 2006-01-16 22:14:31 -0800 | [diff] [blame] | 772 | list_move(&req->list, &fc->io); |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 773 | |
| 774 | in = &req->in; |
Miklos Szeredi | 1d3d752 | 2006-01-06 00:19:40 -0800 | [diff] [blame] | 775 | reqsize = in->h.len; |
| 776 | /* If request is too large, reply with an error and restart the read */ |
| 777 | if (iov_length(iov, nr_segs) < reqsize) { |
| 778 | req->out.h.error = -EIO; |
| 779 | /* SETXATTR is special, since it may contain too large data */ |
| 780 | if (in->h.opcode == FUSE_SETXATTR) |
| 781 | req->out.h.error = -E2BIG; |
| 782 | request_end(fc, req); |
| 783 | goto restart; |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 784 | } |
Miklos Szeredi | d713311 | 2006-04-10 22:54:55 -0700 | [diff] [blame] | 785 | spin_unlock(&fc->lock); |
| 786 | fuse_copy_init(&cs, fc, 1, req, iov, nr_segs); |
Miklos Szeredi | 1d3d752 | 2006-01-06 00:19:40 -0800 | [diff] [blame] | 787 | err = fuse_copy_one(&cs, &in->h, sizeof(in->h)); |
| 788 | if (!err) |
| 789 | err = fuse_copy_args(&cs, in->numargs, in->argpages, |
| 790 | (struct fuse_arg *) in->args, 0); |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 791 | fuse_copy_finish(&cs); |
Miklos Szeredi | d713311 | 2006-04-10 22:54:55 -0700 | [diff] [blame] | 792 | spin_lock(&fc->lock); |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 793 | req->locked = 0; |
Miklos Szeredi | c9c9d7d | 2007-10-16 23:31:05 -0700 | [diff] [blame] | 794 | if (req->aborted) { |
| 795 | request_end(fc, req); |
| 796 | return -ENODEV; |
| 797 | } |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 798 | if (err) { |
Miklos Szeredi | c9c9d7d | 2007-10-16 23:31:05 -0700 | [diff] [blame] | 799 | req->out.h.error = -EIO; |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 800 | request_end(fc, req); |
| 801 | return err; |
| 802 | } |
| 803 | if (!req->isreply) |
| 804 | request_end(fc, req); |
| 805 | else { |
Miklos Szeredi | 83cfd49 | 2006-01-16 22:14:31 -0800 | [diff] [blame] | 806 | req->state = FUSE_REQ_SENT; |
Miklos Szeredi | d77a1d5 | 2006-01-16 22:14:31 -0800 | [diff] [blame] | 807 | list_move_tail(&req->list, &fc->processing); |
Miklos Szeredi | a4d27e7 | 2006-06-25 05:48:54 -0700 | [diff] [blame] | 808 | if (req->interrupted) |
| 809 | queue_interrupt(fc, req); |
Miklos Szeredi | d713311 | 2006-04-10 22:54:55 -0700 | [diff] [blame] | 810 | spin_unlock(&fc->lock); |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 811 | } |
| 812 | return reqsize; |
| 813 | |
| 814 | err_unlock: |
Miklos Szeredi | d713311 | 2006-04-10 22:54:55 -0700 | [diff] [blame] | 815 | spin_unlock(&fc->lock); |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 816 | return err; |
| 817 | } |
| 818 | |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 819 | /* Look up request on processing list by unique ID */ |
| 820 | static struct fuse_req *request_find(struct fuse_conn *fc, u64 unique) |
| 821 | { |
| 822 | struct list_head *entry; |
| 823 | |
| 824 | list_for_each(entry, &fc->processing) { |
| 825 | struct fuse_req *req; |
| 826 | req = list_entry(entry, struct fuse_req, list); |
Miklos Szeredi | a4d27e7 | 2006-06-25 05:48:54 -0700 | [diff] [blame] | 827 | if (req->in.h.unique == unique || req->intr_unique == unique) |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 828 | return req; |
| 829 | } |
| 830 | return NULL; |
| 831 | } |
| 832 | |
| 833 | static int copy_out_args(struct fuse_copy_state *cs, struct fuse_out *out, |
| 834 | unsigned nbytes) |
| 835 | { |
| 836 | unsigned reqsize = sizeof(struct fuse_out_header); |
| 837 | |
| 838 | if (out->h.error) |
| 839 | return nbytes != reqsize ? -EINVAL : 0; |
| 840 | |
| 841 | reqsize += len_args(out->numargs, out->args); |
| 842 | |
| 843 | if (reqsize < nbytes || (reqsize > nbytes && !out->argvar)) |
| 844 | return -EINVAL; |
| 845 | else if (reqsize > nbytes) { |
| 846 | struct fuse_arg *lastarg = &out->args[out->numargs-1]; |
| 847 | unsigned diffsize = reqsize - nbytes; |
| 848 | if (diffsize > lastarg->size) |
| 849 | return -EINVAL; |
| 850 | lastarg->size -= diffsize; |
| 851 | } |
| 852 | return fuse_copy_args(cs, out->numargs, out->argpages, out->args, |
| 853 | out->page_zeroing); |
| 854 | } |
| 855 | |
| 856 | /* |
| 857 | * Write a single reply to a request. First the header is copied from |
| 858 | * the write buffer. The request is then searched on the processing |
| 859 | * list by the unique ID found in the header. If found, then remove |
| 860 | * it from the list and copy the rest of the buffer to the request. |
| 861 | * The request is finished by calling request_end() |
| 862 | */ |
Badari Pulavarty | ee0b3e6 | 2006-09-30 23:28:47 -0700 | [diff] [blame] | 863 | static ssize_t fuse_dev_write(struct kiocb *iocb, const struct iovec *iov, |
| 864 | unsigned long nr_segs, loff_t pos) |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 865 | { |
| 866 | int err; |
| 867 | unsigned nbytes = iov_length(iov, nr_segs); |
| 868 | struct fuse_req *req; |
| 869 | struct fuse_out_header oh; |
| 870 | struct fuse_copy_state cs; |
Badari Pulavarty | ee0b3e6 | 2006-09-30 23:28:47 -0700 | [diff] [blame] | 871 | struct fuse_conn *fc = fuse_get_conn(iocb->ki_filp); |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 872 | if (!fc) |
Miklos Szeredi | a87046d | 2006-04-10 22:54:56 -0700 | [diff] [blame] | 873 | return -EPERM; |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 874 | |
Miklos Szeredi | d713311 | 2006-04-10 22:54:55 -0700 | [diff] [blame] | 875 | fuse_copy_init(&cs, fc, 0, NULL, iov, nr_segs); |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 876 | if (nbytes < sizeof(struct fuse_out_header)) |
| 877 | return -EINVAL; |
| 878 | |
| 879 | err = fuse_copy_one(&cs, &oh, sizeof(oh)); |
| 880 | if (err) |
| 881 | goto err_finish; |
| 882 | err = -EINVAL; |
| 883 | if (!oh.unique || oh.error <= -1000 || oh.error > 0 || |
| 884 | oh.len != nbytes) |
| 885 | goto err_finish; |
| 886 | |
Miklos Szeredi | d713311 | 2006-04-10 22:54:55 -0700 | [diff] [blame] | 887 | spin_lock(&fc->lock); |
Miklos Szeredi | 69a53bf | 2006-01-16 22:14:41 -0800 | [diff] [blame] | 888 | err = -ENOENT; |
| 889 | if (!fc->connected) |
| 890 | goto err_unlock; |
| 891 | |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 892 | req = request_find(fc, oh.unique); |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 893 | if (!req) |
| 894 | goto err_unlock; |
| 895 | |
Miklos Szeredi | f9a2842 | 2006-06-25 05:48:53 -0700 | [diff] [blame] | 896 | if (req->aborted) { |
Miklos Szeredi | d713311 | 2006-04-10 22:54:55 -0700 | [diff] [blame] | 897 | spin_unlock(&fc->lock); |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 898 | fuse_copy_finish(&cs); |
Miklos Szeredi | d713311 | 2006-04-10 22:54:55 -0700 | [diff] [blame] | 899 | spin_lock(&fc->lock); |
Miklos Szeredi | 222f1d6 | 2006-01-16 22:14:25 -0800 | [diff] [blame] | 900 | request_end(fc, req); |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 901 | return -ENOENT; |
| 902 | } |
Miklos Szeredi | a4d27e7 | 2006-06-25 05:48:54 -0700 | [diff] [blame] | 903 | /* Is it an interrupt reply? */ |
| 904 | if (req->intr_unique == oh.unique) { |
| 905 | err = -EINVAL; |
| 906 | if (nbytes != sizeof(struct fuse_out_header)) |
| 907 | goto err_unlock; |
| 908 | |
| 909 | if (oh.error == -ENOSYS) |
| 910 | fc->no_interrupt = 1; |
| 911 | else if (oh.error == -EAGAIN) |
| 912 | queue_interrupt(fc, req); |
| 913 | |
| 914 | spin_unlock(&fc->lock); |
| 915 | fuse_copy_finish(&cs); |
| 916 | return nbytes; |
| 917 | } |
| 918 | |
| 919 | req->state = FUSE_REQ_WRITING; |
Miklos Szeredi | d77a1d5 | 2006-01-16 22:14:31 -0800 | [diff] [blame] | 920 | list_move(&req->list, &fc->io); |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 921 | req->out.h = oh; |
| 922 | req->locked = 1; |
| 923 | cs.req = req; |
Miklos Szeredi | d713311 | 2006-04-10 22:54:55 -0700 | [diff] [blame] | 924 | spin_unlock(&fc->lock); |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 925 | |
| 926 | err = copy_out_args(&cs, &req->out, nbytes); |
| 927 | fuse_copy_finish(&cs); |
| 928 | |
Miklos Szeredi | d713311 | 2006-04-10 22:54:55 -0700 | [diff] [blame] | 929 | spin_lock(&fc->lock); |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 930 | req->locked = 0; |
| 931 | if (!err) { |
Miklos Szeredi | f9a2842 | 2006-06-25 05:48:53 -0700 | [diff] [blame] | 932 | if (req->aborted) |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 933 | err = -ENOENT; |
Miklos Szeredi | f9a2842 | 2006-06-25 05:48:53 -0700 | [diff] [blame] | 934 | } else if (!req->aborted) |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 935 | req->out.h.error = -EIO; |
| 936 | request_end(fc, req); |
| 937 | |
| 938 | return err ? err : nbytes; |
| 939 | |
| 940 | err_unlock: |
Miklos Szeredi | d713311 | 2006-04-10 22:54:55 -0700 | [diff] [blame] | 941 | spin_unlock(&fc->lock); |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 942 | err_finish: |
| 943 | fuse_copy_finish(&cs); |
| 944 | return err; |
| 945 | } |
| 946 | |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 947 | static unsigned fuse_dev_poll(struct file *file, poll_table *wait) |
| 948 | { |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 949 | unsigned mask = POLLOUT | POLLWRNORM; |
Miklos Szeredi | 7025d9a | 2006-04-10 22:54:50 -0700 | [diff] [blame] | 950 | struct fuse_conn *fc = fuse_get_conn(file); |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 951 | if (!fc) |
Miklos Szeredi | 7025d9a | 2006-04-10 22:54:50 -0700 | [diff] [blame] | 952 | return POLLERR; |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 953 | |
| 954 | poll_wait(file, &fc->waitq, wait); |
| 955 | |
Miklos Szeredi | d713311 | 2006-04-10 22:54:55 -0700 | [diff] [blame] | 956 | spin_lock(&fc->lock); |
Miklos Szeredi | 7025d9a | 2006-04-10 22:54:50 -0700 | [diff] [blame] | 957 | if (!fc->connected) |
| 958 | mask = POLLERR; |
Miklos Szeredi | a4d27e7 | 2006-06-25 05:48:54 -0700 | [diff] [blame] | 959 | else if (request_pending(fc)) |
Miklos Szeredi | 7025d9a | 2006-04-10 22:54:50 -0700 | [diff] [blame] | 960 | mask |= POLLIN | POLLRDNORM; |
Miklos Szeredi | d713311 | 2006-04-10 22:54:55 -0700 | [diff] [blame] | 961 | spin_unlock(&fc->lock); |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 962 | |
| 963 | return mask; |
| 964 | } |
| 965 | |
Miklos Szeredi | 69a53bf | 2006-01-16 22:14:41 -0800 | [diff] [blame] | 966 | /* |
| 967 | * Abort all requests on the given list (pending or processing) |
| 968 | * |
Miklos Szeredi | d713311 | 2006-04-10 22:54:55 -0700 | [diff] [blame] | 969 | * This function releases and reacquires fc->lock |
Miklos Szeredi | 69a53bf | 2006-01-16 22:14:41 -0800 | [diff] [blame] | 970 | */ |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 971 | static void end_requests(struct fuse_conn *fc, struct list_head *head) |
| 972 | { |
| 973 | while (!list_empty(head)) { |
| 974 | struct fuse_req *req; |
| 975 | req = list_entry(head->next, struct fuse_req, list); |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 976 | req->out.h.error = -ECONNABORTED; |
| 977 | request_end(fc, req); |
Miklos Szeredi | d713311 | 2006-04-10 22:54:55 -0700 | [diff] [blame] | 978 | spin_lock(&fc->lock); |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 979 | } |
| 980 | } |
| 981 | |
Miklos Szeredi | 69a53bf | 2006-01-16 22:14:41 -0800 | [diff] [blame] | 982 | /* |
| 983 | * Abort requests under I/O |
| 984 | * |
Miklos Szeredi | f9a2842 | 2006-06-25 05:48:53 -0700 | [diff] [blame] | 985 | * The requests are set to aborted and finished, and the request |
Miklos Szeredi | 69a53bf | 2006-01-16 22:14:41 -0800 | [diff] [blame] | 986 | * waiter is woken up. This will make request_wait_answer() wait |
| 987 | * until the request is unlocked and then return. |
Miklos Szeredi | 64c6d8e | 2006-01-16 22:14:42 -0800 | [diff] [blame] | 988 | * |
| 989 | * If the request is asynchronous, then the end function needs to be |
| 990 | * called after waiting for the request to be unlocked (if it was |
| 991 | * locked). |
Miklos Szeredi | 69a53bf | 2006-01-16 22:14:41 -0800 | [diff] [blame] | 992 | */ |
| 993 | static void end_io_requests(struct fuse_conn *fc) |
Miklos Szeredi | 4dbf930 | 2008-04-30 00:54:45 -0700 | [diff] [blame] | 994 | __releases(fc->lock) __acquires(fc->lock) |
Miklos Szeredi | 69a53bf | 2006-01-16 22:14:41 -0800 | [diff] [blame] | 995 | { |
| 996 | while (!list_empty(&fc->io)) { |
Miklos Szeredi | 64c6d8e | 2006-01-16 22:14:42 -0800 | [diff] [blame] | 997 | struct fuse_req *req = |
| 998 | list_entry(fc->io.next, struct fuse_req, list); |
| 999 | void (*end) (struct fuse_conn *, struct fuse_req *) = req->end; |
| 1000 | |
Miklos Szeredi | f9a2842 | 2006-06-25 05:48:53 -0700 | [diff] [blame] | 1001 | req->aborted = 1; |
Miklos Szeredi | 69a53bf | 2006-01-16 22:14:41 -0800 | [diff] [blame] | 1002 | req->out.h.error = -ECONNABORTED; |
| 1003 | req->state = FUSE_REQ_FINISHED; |
| 1004 | list_del_init(&req->list); |
| 1005 | wake_up(&req->waitq); |
Miklos Szeredi | 64c6d8e | 2006-01-16 22:14:42 -0800 | [diff] [blame] | 1006 | if (end) { |
| 1007 | req->end = NULL; |
Miklos Szeredi | 64c6d8e | 2006-01-16 22:14:42 -0800 | [diff] [blame] | 1008 | __fuse_get_request(req); |
Miklos Szeredi | d713311 | 2006-04-10 22:54:55 -0700 | [diff] [blame] | 1009 | spin_unlock(&fc->lock); |
Miklos Szeredi | 64c6d8e | 2006-01-16 22:14:42 -0800 | [diff] [blame] | 1010 | wait_event(req->waitq, !req->locked); |
| 1011 | end(fc, req); |
Tejun Heo | e9bb09d | 2008-11-26 12:03:54 +0100 | [diff] [blame^] | 1012 | fuse_put_request(fc, req); |
Miklos Szeredi | d713311 | 2006-04-10 22:54:55 -0700 | [diff] [blame] | 1013 | spin_lock(&fc->lock); |
Miklos Szeredi | 64c6d8e | 2006-01-16 22:14:42 -0800 | [diff] [blame] | 1014 | } |
Miklos Szeredi | 69a53bf | 2006-01-16 22:14:41 -0800 | [diff] [blame] | 1015 | } |
| 1016 | } |
| 1017 | |
| 1018 | /* |
| 1019 | * Abort all requests. |
| 1020 | * |
| 1021 | * Emergency exit in case of a malicious or accidental deadlock, or |
| 1022 | * just a hung filesystem. |
| 1023 | * |
| 1024 | * The same effect is usually achievable through killing the |
| 1025 | * filesystem daemon and all users of the filesystem. The exception |
| 1026 | * is the combination of an asynchronous request and the tricky |
| 1027 | * deadlock (see Documentation/filesystems/fuse.txt). |
| 1028 | * |
| 1029 | * During the aborting, progression of requests from the pending and |
| 1030 | * processing lists onto the io list, and progression of new requests |
| 1031 | * onto the pending list is prevented by req->connected being false. |
| 1032 | * |
| 1033 | * Progression of requests under I/O to the processing list is |
Miklos Szeredi | f9a2842 | 2006-06-25 05:48:53 -0700 | [diff] [blame] | 1034 | * prevented by the req->aborted flag being true for these requests. |
| 1035 | * For this reason requests on the io list must be aborted first. |
Miklos Szeredi | 69a53bf | 2006-01-16 22:14:41 -0800 | [diff] [blame] | 1036 | */ |
| 1037 | void fuse_abort_conn(struct fuse_conn *fc) |
| 1038 | { |
Miklos Szeredi | d713311 | 2006-04-10 22:54:55 -0700 | [diff] [blame] | 1039 | spin_lock(&fc->lock); |
Miklos Szeredi | 69a53bf | 2006-01-16 22:14:41 -0800 | [diff] [blame] | 1040 | if (fc->connected) { |
| 1041 | fc->connected = 0; |
Miklos Szeredi | 51eb01e | 2006-06-25 05:48:50 -0700 | [diff] [blame] | 1042 | fc->blocked = 0; |
Miklos Szeredi | 69a53bf | 2006-01-16 22:14:41 -0800 | [diff] [blame] | 1043 | end_io_requests(fc); |
| 1044 | end_requests(fc, &fc->pending); |
| 1045 | end_requests(fc, &fc->processing); |
| 1046 | wake_up_all(&fc->waitq); |
Miklos Szeredi | 51eb01e | 2006-06-25 05:48:50 -0700 | [diff] [blame] | 1047 | wake_up_all(&fc->blocked_waitq); |
Jeff Dike | 385a17b | 2006-04-10 22:54:52 -0700 | [diff] [blame] | 1048 | kill_fasync(&fc->fasync, SIGIO, POLL_IN); |
Miklos Szeredi | 69a53bf | 2006-01-16 22:14:41 -0800 | [diff] [blame] | 1049 | } |
Miklos Szeredi | d713311 | 2006-04-10 22:54:55 -0700 | [diff] [blame] | 1050 | spin_unlock(&fc->lock); |
Miklos Szeredi | 69a53bf | 2006-01-16 22:14:41 -0800 | [diff] [blame] | 1051 | } |
| 1052 | |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 1053 | static int fuse_dev_release(struct inode *inode, struct file *file) |
| 1054 | { |
Miklos Szeredi | 0720b31 | 2006-04-10 22:54:55 -0700 | [diff] [blame] | 1055 | struct fuse_conn *fc = fuse_get_conn(file); |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 1056 | if (fc) { |
Miklos Szeredi | d713311 | 2006-04-10 22:54:55 -0700 | [diff] [blame] | 1057 | spin_lock(&fc->lock); |
Miklos Szeredi | 1e9a4ed | 2005-09-09 13:10:31 -0700 | [diff] [blame] | 1058 | fc->connected = 0; |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 1059 | end_requests(fc, &fc->pending); |
| 1060 | end_requests(fc, &fc->processing); |
Miklos Szeredi | d713311 | 2006-04-10 22:54:55 -0700 | [diff] [blame] | 1061 | spin_unlock(&fc->lock); |
Miklos Szeredi | bafa965 | 2006-06-25 05:48:51 -0700 | [diff] [blame] | 1062 | fuse_conn_put(fc); |
Jeff Dike | 385a17b | 2006-04-10 22:54:52 -0700 | [diff] [blame] | 1063 | } |
Miklos Szeredi | f543f25 | 2006-01-16 22:14:35 -0800 | [diff] [blame] | 1064 | |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 1065 | return 0; |
| 1066 | } |
| 1067 | |
Jeff Dike | 385a17b | 2006-04-10 22:54:52 -0700 | [diff] [blame] | 1068 | static int fuse_dev_fasync(int fd, struct file *file, int on) |
| 1069 | { |
| 1070 | struct fuse_conn *fc = fuse_get_conn(file); |
| 1071 | if (!fc) |
Miklos Szeredi | a87046d | 2006-04-10 22:54:56 -0700 | [diff] [blame] | 1072 | return -EPERM; |
Jeff Dike | 385a17b | 2006-04-10 22:54:52 -0700 | [diff] [blame] | 1073 | |
| 1074 | /* No locking - fasync_helper does its own locking */ |
| 1075 | return fasync_helper(fd, file, on, &fc->fasync); |
| 1076 | } |
| 1077 | |
Arjan van de Ven | 4b6f5d2 | 2006-03-28 01:56:42 -0800 | [diff] [blame] | 1078 | const struct file_operations fuse_dev_operations = { |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 1079 | .owner = THIS_MODULE, |
| 1080 | .llseek = no_llseek, |
Badari Pulavarty | ee0b3e6 | 2006-09-30 23:28:47 -0700 | [diff] [blame] | 1081 | .read = do_sync_read, |
| 1082 | .aio_read = fuse_dev_read, |
| 1083 | .write = do_sync_write, |
| 1084 | .aio_write = fuse_dev_write, |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 1085 | .poll = fuse_dev_poll, |
| 1086 | .release = fuse_dev_release, |
Jeff Dike | 385a17b | 2006-04-10 22:54:52 -0700 | [diff] [blame] | 1087 | .fasync = fuse_dev_fasync, |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 1088 | }; |
| 1089 | |
| 1090 | static struct miscdevice fuse_miscdevice = { |
| 1091 | .minor = FUSE_MINOR, |
| 1092 | .name = "fuse", |
| 1093 | .fops = &fuse_dev_operations, |
| 1094 | }; |
| 1095 | |
| 1096 | int __init fuse_dev_init(void) |
| 1097 | { |
| 1098 | int err = -ENOMEM; |
| 1099 | fuse_req_cachep = kmem_cache_create("fuse_request", |
| 1100 | sizeof(struct fuse_req), |
Paul Mundt | 20c2df8 | 2007-07-20 10:11:58 +0900 | [diff] [blame] | 1101 | 0, 0, NULL); |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 1102 | if (!fuse_req_cachep) |
| 1103 | goto out; |
| 1104 | |
| 1105 | err = misc_register(&fuse_miscdevice); |
| 1106 | if (err) |
| 1107 | goto out_cache_clean; |
| 1108 | |
| 1109 | return 0; |
| 1110 | |
| 1111 | out_cache_clean: |
| 1112 | kmem_cache_destroy(fuse_req_cachep); |
| 1113 | out: |
| 1114 | return err; |
| 1115 | } |
| 1116 | |
| 1117 | void fuse_dev_cleanup(void) |
| 1118 | { |
| 1119 | misc_deregister(&fuse_miscdevice); |
| 1120 | kmem_cache_destroy(fuse_req_cachep); |
| 1121 | } |