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