b.liu | e958203 | 2025-04-17 19:18:16 +0800 | [diff] [blame^] | 1 | /* |
| 2 | FUSE: Filesystem in Userspace |
| 3 | Copyright (C) 2001-2008 Miklos Szeredi <miklos@szeredi.hu> |
| 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/sched/signal.h> |
| 15 | #include <linux/uio.h> |
| 16 | #include <linux/miscdevice.h> |
| 17 | #include <linux/namei.h> |
| 18 | #include <linux/pagemap.h> |
| 19 | #include <linux/file.h> |
| 20 | #include <linux/slab.h> |
| 21 | #include <linux/pipe_fs_i.h> |
| 22 | #include <linux/swap.h> |
| 23 | #include <linux/splice.h> |
| 24 | #include <linux/sched.h> |
| 25 | |
| 26 | MODULE_ALIAS_MISCDEV(FUSE_MINOR); |
| 27 | MODULE_ALIAS("devname:fuse"); |
| 28 | |
| 29 | /* Ordinary requests have even IDs, while interrupts IDs are odd */ |
| 30 | #define FUSE_INT_REQ_BIT (1ULL << 0) |
| 31 | #define FUSE_REQ_ID_STEP (1ULL << 1) |
| 32 | |
| 33 | static struct kmem_cache *fuse_req_cachep; |
| 34 | |
| 35 | static struct fuse_dev *fuse_get_dev(struct file *file) |
| 36 | { |
| 37 | /* |
| 38 | * Lockless access is OK, because file->private data is set |
| 39 | * once during mount and is valid until the file is released. |
| 40 | */ |
| 41 | return READ_ONCE(file->private_data); |
| 42 | } |
| 43 | |
| 44 | static void fuse_request_init(struct fuse_req *req) |
| 45 | { |
| 46 | INIT_LIST_HEAD(&req->list); |
| 47 | INIT_LIST_HEAD(&req->intr_entry); |
| 48 | init_waitqueue_head(&req->waitq); |
| 49 | refcount_set(&req->count, 1); |
| 50 | __set_bit(FR_PENDING, &req->flags); |
| 51 | } |
| 52 | |
| 53 | static struct fuse_req *fuse_request_alloc(gfp_t flags) |
| 54 | { |
| 55 | struct fuse_req *req = kmem_cache_zalloc(fuse_req_cachep, flags); |
| 56 | if (req) |
| 57 | fuse_request_init(req); |
| 58 | |
| 59 | return req; |
| 60 | } |
| 61 | |
| 62 | static void fuse_request_free(struct fuse_req *req) |
| 63 | { |
| 64 | kmem_cache_free(fuse_req_cachep, req); |
| 65 | } |
| 66 | |
| 67 | static void __fuse_get_request(struct fuse_req *req) |
| 68 | { |
| 69 | refcount_inc(&req->count); |
| 70 | } |
| 71 | |
| 72 | /* Must be called with > 1 refcount */ |
| 73 | static void __fuse_put_request(struct fuse_req *req) |
| 74 | { |
| 75 | refcount_dec(&req->count); |
| 76 | } |
| 77 | |
| 78 | void fuse_set_initialized(struct fuse_conn *fc) |
| 79 | { |
| 80 | /* Make sure stores before this are seen on another CPU */ |
| 81 | smp_wmb(); |
| 82 | fc->initialized = 1; |
| 83 | } |
| 84 | |
| 85 | static bool fuse_block_alloc(struct fuse_conn *fc, bool for_background) |
| 86 | { |
| 87 | return !fc->initialized || (for_background && fc->blocked); |
| 88 | } |
| 89 | |
| 90 | static void fuse_drop_waiting(struct fuse_conn *fc) |
| 91 | { |
| 92 | /* |
| 93 | * lockess check of fc->connected is okay, because atomic_dec_and_test() |
| 94 | * provides a memory barrier mached with the one in fuse_wait_aborted() |
| 95 | * to ensure no wake-up is missed. |
| 96 | */ |
| 97 | if (atomic_dec_and_test(&fc->num_waiting) && |
| 98 | !READ_ONCE(fc->connected)) { |
| 99 | /* wake up aborters */ |
| 100 | wake_up_all(&fc->blocked_waitq); |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | static void fuse_put_request(struct fuse_conn *fc, struct fuse_req *req); |
| 105 | |
| 106 | static struct fuse_req *fuse_get_req(struct fuse_conn *fc, bool for_background) |
| 107 | { |
| 108 | struct fuse_req *req; |
| 109 | int err; |
| 110 | atomic_inc(&fc->num_waiting); |
| 111 | |
| 112 | if (fuse_block_alloc(fc, for_background)) { |
| 113 | err = -EINTR; |
| 114 | if (wait_event_killable_exclusive(fc->blocked_waitq, |
| 115 | !fuse_block_alloc(fc, for_background))) |
| 116 | goto out; |
| 117 | } |
| 118 | /* Matches smp_wmb() in fuse_set_initialized() */ |
| 119 | smp_rmb(); |
| 120 | |
| 121 | err = -ENOTCONN; |
| 122 | if (!fc->connected) |
| 123 | goto out; |
| 124 | |
| 125 | err = -ECONNREFUSED; |
| 126 | if (fc->conn_error) |
| 127 | goto out; |
| 128 | |
| 129 | req = fuse_request_alloc(GFP_KERNEL); |
| 130 | err = -ENOMEM; |
| 131 | if (!req) { |
| 132 | if (for_background) |
| 133 | wake_up(&fc->blocked_waitq); |
| 134 | goto out; |
| 135 | } |
| 136 | |
| 137 | req->in.h.uid = from_kuid(fc->user_ns, current_fsuid()); |
| 138 | req->in.h.gid = from_kgid(fc->user_ns, current_fsgid()); |
| 139 | req->in.h.pid = pid_nr_ns(task_pid(current), fc->pid_ns); |
| 140 | |
| 141 | __set_bit(FR_WAITING, &req->flags); |
| 142 | if (for_background) |
| 143 | __set_bit(FR_BACKGROUND, &req->flags); |
| 144 | |
| 145 | if (unlikely(req->in.h.uid == ((uid_t)-1) || |
| 146 | req->in.h.gid == ((gid_t)-1))) { |
| 147 | fuse_put_request(fc, req); |
| 148 | return ERR_PTR(-EOVERFLOW); |
| 149 | } |
| 150 | return req; |
| 151 | |
| 152 | out: |
| 153 | fuse_drop_waiting(fc); |
| 154 | return ERR_PTR(err); |
| 155 | } |
| 156 | |
| 157 | static void fuse_put_request(struct fuse_conn *fc, struct fuse_req *req) |
| 158 | { |
| 159 | if (refcount_dec_and_test(&req->count)) { |
| 160 | if (test_bit(FR_BACKGROUND, &req->flags)) { |
| 161 | /* |
| 162 | * We get here in the unlikely case that a background |
| 163 | * request was allocated but not sent |
| 164 | */ |
| 165 | spin_lock(&fc->bg_lock); |
| 166 | if (!fc->blocked) |
| 167 | wake_up(&fc->blocked_waitq); |
| 168 | spin_unlock(&fc->bg_lock); |
| 169 | } |
| 170 | |
| 171 | if (test_bit(FR_WAITING, &req->flags)) { |
| 172 | __clear_bit(FR_WAITING, &req->flags); |
| 173 | fuse_drop_waiting(fc); |
| 174 | } |
| 175 | |
| 176 | fuse_request_free(req); |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | unsigned int fuse_len_args(unsigned int numargs, struct fuse_arg *args) |
| 181 | { |
| 182 | unsigned nbytes = 0; |
| 183 | unsigned i; |
| 184 | |
| 185 | for (i = 0; i < numargs; i++) |
| 186 | nbytes += args[i].size; |
| 187 | |
| 188 | return nbytes; |
| 189 | } |
| 190 | EXPORT_SYMBOL_GPL(fuse_len_args); |
| 191 | |
| 192 | u64 fuse_get_unique(struct fuse_iqueue *fiq) |
| 193 | { |
| 194 | fiq->reqctr += FUSE_REQ_ID_STEP; |
| 195 | return fiq->reqctr; |
| 196 | } |
| 197 | EXPORT_SYMBOL_GPL(fuse_get_unique); |
| 198 | |
| 199 | static unsigned int fuse_req_hash(u64 unique) |
| 200 | { |
| 201 | return hash_long(unique & ~FUSE_INT_REQ_BIT, FUSE_PQ_HASH_BITS); |
| 202 | } |
| 203 | |
| 204 | /** |
| 205 | * A new request is available, wake fiq->waitq |
| 206 | */ |
| 207 | static void fuse_dev_wake_and_unlock(struct fuse_iqueue *fiq) |
| 208 | __releases(fiq->lock) |
| 209 | { |
| 210 | wake_up(&fiq->waitq); |
| 211 | kill_fasync(&fiq->fasync, SIGIO, POLL_IN); |
| 212 | spin_unlock(&fiq->lock); |
| 213 | } |
| 214 | |
| 215 | const struct fuse_iqueue_ops fuse_dev_fiq_ops = { |
| 216 | .wake_forget_and_unlock = fuse_dev_wake_and_unlock, |
| 217 | .wake_interrupt_and_unlock = fuse_dev_wake_and_unlock, |
| 218 | .wake_pending_and_unlock = fuse_dev_wake_and_unlock, |
| 219 | }; |
| 220 | EXPORT_SYMBOL_GPL(fuse_dev_fiq_ops); |
| 221 | |
| 222 | static void queue_request_and_unlock(struct fuse_iqueue *fiq, |
| 223 | struct fuse_req *req) |
| 224 | __releases(fiq->lock) |
| 225 | { |
| 226 | req->in.h.len = sizeof(struct fuse_in_header) + |
| 227 | fuse_len_args(req->args->in_numargs, |
| 228 | (struct fuse_arg *) req->args->in_args); |
| 229 | list_add_tail(&req->list, &fiq->pending); |
| 230 | fiq->ops->wake_pending_and_unlock(fiq); |
| 231 | } |
| 232 | |
| 233 | void fuse_queue_forget(struct fuse_conn *fc, struct fuse_forget_link *forget, |
| 234 | u64 nodeid, u64 nlookup) |
| 235 | { |
| 236 | struct fuse_iqueue *fiq = &fc->iq; |
| 237 | |
| 238 | forget->forget_one.nodeid = nodeid; |
| 239 | forget->forget_one.nlookup = nlookup; |
| 240 | |
| 241 | spin_lock(&fiq->lock); |
| 242 | if (fiq->connected) { |
| 243 | fiq->forget_list_tail->next = forget; |
| 244 | fiq->forget_list_tail = forget; |
| 245 | fiq->ops->wake_forget_and_unlock(fiq); |
| 246 | } else { |
| 247 | kfree(forget); |
| 248 | spin_unlock(&fiq->lock); |
| 249 | } |
| 250 | } |
| 251 | |
| 252 | static void flush_bg_queue(struct fuse_conn *fc) |
| 253 | { |
| 254 | struct fuse_iqueue *fiq = &fc->iq; |
| 255 | |
| 256 | while (fc->active_background < fc->max_background && |
| 257 | !list_empty(&fc->bg_queue)) { |
| 258 | struct fuse_req *req; |
| 259 | |
| 260 | req = list_first_entry(&fc->bg_queue, struct fuse_req, list); |
| 261 | list_del(&req->list); |
| 262 | fc->active_background++; |
| 263 | spin_lock(&fiq->lock); |
| 264 | req->in.h.unique = fuse_get_unique(fiq); |
| 265 | queue_request_and_unlock(fiq, req); |
| 266 | } |
| 267 | } |
| 268 | |
| 269 | /* |
| 270 | * This function is called when a request is finished. Either a reply |
| 271 | * has arrived or it was aborted (and not yet sent) or some error |
| 272 | * occurred during communication with userspace, or the device file |
| 273 | * was closed. The requester thread is woken up (if still waiting), |
| 274 | * the 'end' callback is called if given, else the reference to the |
| 275 | * request is released |
| 276 | */ |
| 277 | void fuse_request_end(struct fuse_conn *fc, struct fuse_req *req) |
| 278 | { |
| 279 | struct fuse_iqueue *fiq = &fc->iq; |
| 280 | |
| 281 | if (test_and_set_bit(FR_FINISHED, &req->flags)) |
| 282 | goto put_request; |
| 283 | |
| 284 | /* |
| 285 | * test_and_set_bit() implies smp_mb() between bit |
| 286 | * changing and below FR_INTERRUPTED check. Pairs with |
| 287 | * smp_mb() from queue_interrupt(). |
| 288 | */ |
| 289 | if (test_bit(FR_INTERRUPTED, &req->flags)) { |
| 290 | spin_lock(&fiq->lock); |
| 291 | list_del_init(&req->intr_entry); |
| 292 | spin_unlock(&fiq->lock); |
| 293 | } |
| 294 | WARN_ON(test_bit(FR_PENDING, &req->flags)); |
| 295 | WARN_ON(test_bit(FR_SENT, &req->flags)); |
| 296 | if (test_bit(FR_BACKGROUND, &req->flags)) { |
| 297 | spin_lock(&fc->bg_lock); |
| 298 | clear_bit(FR_BACKGROUND, &req->flags); |
| 299 | if (fc->num_background == fc->max_background) { |
| 300 | fc->blocked = 0; |
| 301 | wake_up(&fc->blocked_waitq); |
| 302 | } else if (!fc->blocked) { |
| 303 | /* |
| 304 | * Wake up next waiter, if any. It's okay to use |
| 305 | * waitqueue_active(), as we've already synced up |
| 306 | * fc->blocked with waiters with the wake_up() call |
| 307 | * above. |
| 308 | */ |
| 309 | if (waitqueue_active(&fc->blocked_waitq)) |
| 310 | wake_up(&fc->blocked_waitq); |
| 311 | } |
| 312 | |
| 313 | if (fc->num_background == fc->congestion_threshold && fc->sb) { |
| 314 | clear_bdi_congested(fc->sb->s_bdi, BLK_RW_SYNC); |
| 315 | clear_bdi_congested(fc->sb->s_bdi, BLK_RW_ASYNC); |
| 316 | } |
| 317 | fc->num_background--; |
| 318 | fc->active_background--; |
| 319 | flush_bg_queue(fc); |
| 320 | spin_unlock(&fc->bg_lock); |
| 321 | } else { |
| 322 | /* Wake up waiter sleeping in request_wait_answer() */ |
| 323 | wake_up(&req->waitq); |
| 324 | } |
| 325 | |
| 326 | if (test_bit(FR_ASYNC, &req->flags)) |
| 327 | req->args->end(fc, req->args, req->out.h.error); |
| 328 | put_request: |
| 329 | fuse_put_request(fc, req); |
| 330 | } |
| 331 | EXPORT_SYMBOL_GPL(fuse_request_end); |
| 332 | |
| 333 | static int queue_interrupt(struct fuse_iqueue *fiq, struct fuse_req *req) |
| 334 | { |
| 335 | spin_lock(&fiq->lock); |
| 336 | /* Check for we've sent request to interrupt this req */ |
| 337 | if (unlikely(!test_bit(FR_INTERRUPTED, &req->flags))) { |
| 338 | spin_unlock(&fiq->lock); |
| 339 | return -EINVAL; |
| 340 | } |
| 341 | |
| 342 | if (list_empty(&req->intr_entry)) { |
| 343 | list_add_tail(&req->intr_entry, &fiq->interrupts); |
| 344 | /* |
| 345 | * Pairs with smp_mb() implied by test_and_set_bit() |
| 346 | * from request_end(). |
| 347 | */ |
| 348 | smp_mb(); |
| 349 | if (test_bit(FR_FINISHED, &req->flags)) { |
| 350 | list_del_init(&req->intr_entry); |
| 351 | spin_unlock(&fiq->lock); |
| 352 | return 0; |
| 353 | } |
| 354 | fiq->ops->wake_interrupt_and_unlock(fiq); |
| 355 | } else { |
| 356 | spin_unlock(&fiq->lock); |
| 357 | } |
| 358 | return 0; |
| 359 | } |
| 360 | |
| 361 | static void request_wait_answer(struct fuse_conn *fc, struct fuse_req *req) |
| 362 | { |
| 363 | struct fuse_iqueue *fiq = &fc->iq; |
| 364 | int err; |
| 365 | |
| 366 | if (!fc->no_interrupt) { |
| 367 | /* Any signal may interrupt this */ |
| 368 | err = wait_event_interruptible(req->waitq, |
| 369 | test_bit(FR_FINISHED, &req->flags)); |
| 370 | if (!err) |
| 371 | return; |
| 372 | |
| 373 | set_bit(FR_INTERRUPTED, &req->flags); |
| 374 | /* matches barrier in fuse_dev_do_read() */ |
| 375 | smp_mb__after_atomic(); |
| 376 | if (test_bit(FR_SENT, &req->flags)) |
| 377 | queue_interrupt(fiq, req); |
| 378 | } |
| 379 | |
| 380 | if (!test_bit(FR_FORCE, &req->flags)) { |
| 381 | /* Only fatal signals may interrupt this */ |
| 382 | err = wait_event_killable(req->waitq, |
| 383 | test_bit(FR_FINISHED, &req->flags)); |
| 384 | if (!err) |
| 385 | return; |
| 386 | |
| 387 | spin_lock(&fiq->lock); |
| 388 | /* Request is not yet in userspace, bail out */ |
| 389 | if (test_bit(FR_PENDING, &req->flags)) { |
| 390 | list_del(&req->list); |
| 391 | spin_unlock(&fiq->lock); |
| 392 | __fuse_put_request(req); |
| 393 | req->out.h.error = -EINTR; |
| 394 | return; |
| 395 | } |
| 396 | spin_unlock(&fiq->lock); |
| 397 | } |
| 398 | |
| 399 | /* |
| 400 | * Either request is already in userspace, or it was forced. |
| 401 | * Wait it out. |
| 402 | */ |
| 403 | wait_event(req->waitq, test_bit(FR_FINISHED, &req->flags)); |
| 404 | } |
| 405 | |
| 406 | static void __fuse_request_send(struct fuse_conn *fc, struct fuse_req *req) |
| 407 | { |
| 408 | struct fuse_iqueue *fiq = &fc->iq; |
| 409 | |
| 410 | BUG_ON(test_bit(FR_BACKGROUND, &req->flags)); |
| 411 | spin_lock(&fiq->lock); |
| 412 | if (!fiq->connected) { |
| 413 | spin_unlock(&fiq->lock); |
| 414 | req->out.h.error = -ENOTCONN; |
| 415 | } else { |
| 416 | req->in.h.unique = fuse_get_unique(fiq); |
| 417 | /* acquire extra reference, since request is still needed |
| 418 | after fuse_request_end() */ |
| 419 | __fuse_get_request(req); |
| 420 | queue_request_and_unlock(fiq, req); |
| 421 | |
| 422 | request_wait_answer(fc, req); |
| 423 | /* Pairs with smp_wmb() in fuse_request_end() */ |
| 424 | smp_rmb(); |
| 425 | } |
| 426 | } |
| 427 | |
| 428 | static void fuse_adjust_compat(struct fuse_conn *fc, struct fuse_args *args) |
| 429 | { |
| 430 | if (fc->minor < 4 && args->opcode == FUSE_STATFS) |
| 431 | args->out_args[0].size = FUSE_COMPAT_STATFS_SIZE; |
| 432 | |
| 433 | if (fc->minor < 9) { |
| 434 | switch (args->opcode) { |
| 435 | case FUSE_LOOKUP: |
| 436 | case FUSE_CREATE: |
| 437 | case FUSE_MKNOD: |
| 438 | case FUSE_MKDIR: |
| 439 | case FUSE_SYMLINK: |
| 440 | case FUSE_LINK: |
| 441 | args->out_args[0].size = FUSE_COMPAT_ENTRY_OUT_SIZE; |
| 442 | break; |
| 443 | case FUSE_GETATTR: |
| 444 | case FUSE_SETATTR: |
| 445 | args->out_args[0].size = FUSE_COMPAT_ATTR_OUT_SIZE; |
| 446 | break; |
| 447 | } |
| 448 | } |
| 449 | if (fc->minor < 12) { |
| 450 | switch (args->opcode) { |
| 451 | case FUSE_CREATE: |
| 452 | args->in_args[0].size = sizeof(struct fuse_open_in); |
| 453 | break; |
| 454 | case FUSE_MKNOD: |
| 455 | args->in_args[0].size = FUSE_COMPAT_MKNOD_IN_SIZE; |
| 456 | break; |
| 457 | } |
| 458 | } |
| 459 | } |
| 460 | |
| 461 | static void fuse_force_creds(struct fuse_conn *fc, struct fuse_req *req) |
| 462 | { |
| 463 | req->in.h.uid = from_kuid_munged(fc->user_ns, current_fsuid()); |
| 464 | req->in.h.gid = from_kgid_munged(fc->user_ns, current_fsgid()); |
| 465 | req->in.h.pid = pid_nr_ns(task_pid(current), fc->pid_ns); |
| 466 | } |
| 467 | |
| 468 | static void fuse_args_to_req(struct fuse_req *req, struct fuse_args *args) |
| 469 | { |
| 470 | req->in.h.opcode = args->opcode; |
| 471 | req->in.h.nodeid = args->nodeid; |
| 472 | req->args = args; |
| 473 | if (args->end) |
| 474 | __set_bit(FR_ASYNC, &req->flags); |
| 475 | } |
| 476 | |
| 477 | ssize_t fuse_simple_request(struct fuse_conn *fc, struct fuse_args *args) |
| 478 | { |
| 479 | struct fuse_req *req; |
| 480 | ssize_t ret; |
| 481 | |
| 482 | if (args->force) { |
| 483 | atomic_inc(&fc->num_waiting); |
| 484 | req = fuse_request_alloc(GFP_KERNEL | __GFP_NOFAIL); |
| 485 | |
| 486 | if (!args->nocreds) |
| 487 | fuse_force_creds(fc, req); |
| 488 | |
| 489 | __set_bit(FR_WAITING, &req->flags); |
| 490 | __set_bit(FR_FORCE, &req->flags); |
| 491 | } else { |
| 492 | WARN_ON(args->nocreds); |
| 493 | req = fuse_get_req(fc, false); |
| 494 | if (IS_ERR(req)) |
| 495 | return PTR_ERR(req); |
| 496 | } |
| 497 | |
| 498 | /* Needs to be done after fuse_get_req() so that fc->minor is valid */ |
| 499 | fuse_adjust_compat(fc, args); |
| 500 | fuse_args_to_req(req, args); |
| 501 | |
| 502 | if (!args->noreply) |
| 503 | __set_bit(FR_ISREPLY, &req->flags); |
| 504 | __fuse_request_send(fc, req); |
| 505 | ret = req->out.h.error; |
| 506 | if (!ret && args->out_argvar) { |
| 507 | BUG_ON(args->out_numargs == 0); |
| 508 | ret = args->out_args[args->out_numargs - 1].size; |
| 509 | } |
| 510 | fuse_put_request(fc, req); |
| 511 | |
| 512 | return ret; |
| 513 | } |
| 514 | |
| 515 | static bool fuse_request_queue_background(struct fuse_conn *fc, |
| 516 | struct fuse_req *req) |
| 517 | { |
| 518 | bool queued = false; |
| 519 | |
| 520 | WARN_ON(!test_bit(FR_BACKGROUND, &req->flags)); |
| 521 | if (!test_bit(FR_WAITING, &req->flags)) { |
| 522 | __set_bit(FR_WAITING, &req->flags); |
| 523 | atomic_inc(&fc->num_waiting); |
| 524 | } |
| 525 | __set_bit(FR_ISREPLY, &req->flags); |
| 526 | spin_lock(&fc->bg_lock); |
| 527 | if (likely(fc->connected)) { |
| 528 | fc->num_background++; |
| 529 | if (fc->num_background == fc->max_background) |
| 530 | fc->blocked = 1; |
| 531 | if (fc->num_background == fc->congestion_threshold && fc->sb) { |
| 532 | set_bdi_congested(fc->sb->s_bdi, BLK_RW_SYNC); |
| 533 | set_bdi_congested(fc->sb->s_bdi, BLK_RW_ASYNC); |
| 534 | } |
| 535 | list_add_tail(&req->list, &fc->bg_queue); |
| 536 | flush_bg_queue(fc); |
| 537 | queued = true; |
| 538 | } |
| 539 | spin_unlock(&fc->bg_lock); |
| 540 | |
| 541 | return queued; |
| 542 | } |
| 543 | |
| 544 | int fuse_simple_background(struct fuse_conn *fc, struct fuse_args *args, |
| 545 | gfp_t gfp_flags) |
| 546 | { |
| 547 | struct fuse_req *req; |
| 548 | |
| 549 | if (args->force) { |
| 550 | WARN_ON(!args->nocreds); |
| 551 | req = fuse_request_alloc(gfp_flags); |
| 552 | if (!req) |
| 553 | return -ENOMEM; |
| 554 | __set_bit(FR_BACKGROUND, &req->flags); |
| 555 | } else { |
| 556 | WARN_ON(args->nocreds); |
| 557 | req = fuse_get_req(fc, true); |
| 558 | if (IS_ERR(req)) |
| 559 | return PTR_ERR(req); |
| 560 | } |
| 561 | |
| 562 | fuse_args_to_req(req, args); |
| 563 | |
| 564 | if (!fuse_request_queue_background(fc, req)) { |
| 565 | fuse_put_request(fc, req); |
| 566 | return -ENOTCONN; |
| 567 | } |
| 568 | |
| 569 | return 0; |
| 570 | } |
| 571 | EXPORT_SYMBOL_GPL(fuse_simple_background); |
| 572 | |
| 573 | static int fuse_simple_notify_reply(struct fuse_conn *fc, |
| 574 | struct fuse_args *args, u64 unique) |
| 575 | { |
| 576 | struct fuse_req *req; |
| 577 | struct fuse_iqueue *fiq = &fc->iq; |
| 578 | int err = 0; |
| 579 | |
| 580 | req = fuse_get_req(fc, false); |
| 581 | if (IS_ERR(req)) |
| 582 | return PTR_ERR(req); |
| 583 | |
| 584 | __clear_bit(FR_ISREPLY, &req->flags); |
| 585 | req->in.h.unique = unique; |
| 586 | |
| 587 | fuse_args_to_req(req, args); |
| 588 | |
| 589 | spin_lock(&fiq->lock); |
| 590 | if (fiq->connected) { |
| 591 | queue_request_and_unlock(fiq, req); |
| 592 | } else { |
| 593 | err = -ENODEV; |
| 594 | spin_unlock(&fiq->lock); |
| 595 | fuse_put_request(fc, req); |
| 596 | } |
| 597 | |
| 598 | return err; |
| 599 | } |
| 600 | |
| 601 | /* |
| 602 | * Lock the request. Up to the next unlock_request() there mustn't be |
| 603 | * anything that could cause a page-fault. If the request was already |
| 604 | * aborted bail out. |
| 605 | */ |
| 606 | static int lock_request(struct fuse_req *req) |
| 607 | { |
| 608 | int err = 0; |
| 609 | if (req) { |
| 610 | spin_lock(&req->waitq.lock); |
| 611 | if (test_bit(FR_ABORTED, &req->flags)) |
| 612 | err = -ENOENT; |
| 613 | else |
| 614 | set_bit(FR_LOCKED, &req->flags); |
| 615 | spin_unlock(&req->waitq.lock); |
| 616 | } |
| 617 | return err; |
| 618 | } |
| 619 | |
| 620 | /* |
| 621 | * Unlock request. If it was aborted while locked, caller is responsible |
| 622 | * for unlocking and ending the request. |
| 623 | */ |
| 624 | static int unlock_request(struct fuse_req *req) |
| 625 | { |
| 626 | int err = 0; |
| 627 | if (req) { |
| 628 | spin_lock(&req->waitq.lock); |
| 629 | if (test_bit(FR_ABORTED, &req->flags)) |
| 630 | err = -ENOENT; |
| 631 | else |
| 632 | clear_bit(FR_LOCKED, &req->flags); |
| 633 | spin_unlock(&req->waitq.lock); |
| 634 | } |
| 635 | return err; |
| 636 | } |
| 637 | |
| 638 | struct fuse_copy_state { |
| 639 | int write; |
| 640 | struct fuse_req *req; |
| 641 | struct iov_iter *iter; |
| 642 | struct pipe_buffer *pipebufs; |
| 643 | struct pipe_buffer *currbuf; |
| 644 | struct pipe_inode_info *pipe; |
| 645 | unsigned long nr_segs; |
| 646 | struct page *pg; |
| 647 | unsigned len; |
| 648 | unsigned offset; |
| 649 | unsigned move_pages:1; |
| 650 | }; |
| 651 | |
| 652 | static void fuse_copy_init(struct fuse_copy_state *cs, int write, |
| 653 | struct iov_iter *iter) |
| 654 | { |
| 655 | memset(cs, 0, sizeof(*cs)); |
| 656 | cs->write = write; |
| 657 | cs->iter = iter; |
| 658 | } |
| 659 | |
| 660 | /* Unmap and put previous page of userspace buffer */ |
| 661 | static void fuse_copy_finish(struct fuse_copy_state *cs) |
| 662 | { |
| 663 | if (cs->currbuf) { |
| 664 | struct pipe_buffer *buf = cs->currbuf; |
| 665 | |
| 666 | if (cs->write) |
| 667 | buf->len = PAGE_SIZE - cs->len; |
| 668 | cs->currbuf = NULL; |
| 669 | } else if (cs->pg) { |
| 670 | if (cs->write) { |
| 671 | flush_dcache_page(cs->pg); |
| 672 | set_page_dirty_lock(cs->pg); |
| 673 | } |
| 674 | put_page(cs->pg); |
| 675 | } |
| 676 | cs->pg = NULL; |
| 677 | } |
| 678 | |
| 679 | /* |
| 680 | * Get another pagefull of userspace buffer, and map it to kernel |
| 681 | * address space, and lock request |
| 682 | */ |
| 683 | static int fuse_copy_fill(struct fuse_copy_state *cs) |
| 684 | { |
| 685 | struct page *page; |
| 686 | int err; |
| 687 | |
| 688 | err = unlock_request(cs->req); |
| 689 | if (err) |
| 690 | return err; |
| 691 | |
| 692 | fuse_copy_finish(cs); |
| 693 | if (cs->pipebufs) { |
| 694 | struct pipe_buffer *buf = cs->pipebufs; |
| 695 | |
| 696 | if (!cs->write) { |
| 697 | err = pipe_buf_confirm(cs->pipe, buf); |
| 698 | if (err) |
| 699 | return err; |
| 700 | |
| 701 | BUG_ON(!cs->nr_segs); |
| 702 | cs->currbuf = buf; |
| 703 | cs->pg = buf->page; |
| 704 | cs->offset = buf->offset; |
| 705 | cs->len = buf->len; |
| 706 | cs->pipebufs++; |
| 707 | cs->nr_segs--; |
| 708 | } else { |
| 709 | if (cs->nr_segs == cs->pipe->buffers) |
| 710 | return -EIO; |
| 711 | |
| 712 | page = alloc_page(GFP_HIGHUSER); |
| 713 | if (!page) |
| 714 | return -ENOMEM; |
| 715 | |
| 716 | buf->page = page; |
| 717 | buf->offset = 0; |
| 718 | buf->len = 0; |
| 719 | |
| 720 | cs->currbuf = buf; |
| 721 | cs->pg = page; |
| 722 | cs->offset = 0; |
| 723 | cs->len = PAGE_SIZE; |
| 724 | cs->pipebufs++; |
| 725 | cs->nr_segs++; |
| 726 | } |
| 727 | } else { |
| 728 | size_t off; |
| 729 | err = iov_iter_get_pages(cs->iter, &page, PAGE_SIZE, 1, &off); |
| 730 | if (err < 0) |
| 731 | return err; |
| 732 | BUG_ON(!err); |
| 733 | cs->len = err; |
| 734 | cs->offset = off; |
| 735 | cs->pg = page; |
| 736 | iov_iter_advance(cs->iter, err); |
| 737 | } |
| 738 | |
| 739 | return lock_request(cs->req); |
| 740 | } |
| 741 | |
| 742 | /* Do as much copy to/from userspace buffer as we can */ |
| 743 | static int fuse_copy_do(struct fuse_copy_state *cs, void **val, unsigned *size) |
| 744 | { |
| 745 | unsigned ncpy = min(*size, cs->len); |
| 746 | if (val) { |
| 747 | void *pgaddr = kmap_atomic(cs->pg); |
| 748 | void *buf = pgaddr + cs->offset; |
| 749 | |
| 750 | if (cs->write) |
| 751 | memcpy(buf, *val, ncpy); |
| 752 | else |
| 753 | memcpy(*val, buf, ncpy); |
| 754 | |
| 755 | kunmap_atomic(pgaddr); |
| 756 | *val += ncpy; |
| 757 | } |
| 758 | *size -= ncpy; |
| 759 | cs->len -= ncpy; |
| 760 | cs->offset += ncpy; |
| 761 | return ncpy; |
| 762 | } |
| 763 | |
| 764 | static int fuse_check_page(struct page *page) |
| 765 | { |
| 766 | if (page_mapcount(page) || |
| 767 | page->mapping != NULL || |
| 768 | (page->flags & PAGE_FLAGS_CHECK_AT_PREP & |
| 769 | ~(1 << PG_locked | |
| 770 | 1 << PG_referenced | |
| 771 | 1 << PG_uptodate | |
| 772 | 1 << PG_lru | |
| 773 | 1 << PG_active | |
| 774 | 1 << PG_workingset | |
| 775 | 1 << PG_reclaim | |
| 776 | 1 << PG_waiters))) { |
| 777 | pr_warn("trying to steal weird page\n"); |
| 778 | pr_warn(" page=%p index=%li flags=%08lx, count=%i, mapcount=%i, mapping=%p\n", page, page->index, page->flags, page_count(page), page_mapcount(page), page->mapping); |
| 779 | return 1; |
| 780 | } |
| 781 | return 0; |
| 782 | } |
| 783 | |
| 784 | static int fuse_try_move_page(struct fuse_copy_state *cs, struct page **pagep) |
| 785 | { |
| 786 | int err; |
| 787 | struct page *oldpage = *pagep; |
| 788 | struct page *newpage; |
| 789 | struct pipe_buffer *buf = cs->pipebufs; |
| 790 | |
| 791 | get_page(oldpage); |
| 792 | err = unlock_request(cs->req); |
| 793 | if (err) |
| 794 | goto out_put_old; |
| 795 | |
| 796 | fuse_copy_finish(cs); |
| 797 | |
| 798 | err = pipe_buf_confirm(cs->pipe, buf); |
| 799 | if (err) |
| 800 | goto out_put_old; |
| 801 | |
| 802 | BUG_ON(!cs->nr_segs); |
| 803 | cs->currbuf = buf; |
| 804 | cs->len = buf->len; |
| 805 | cs->pipebufs++; |
| 806 | cs->nr_segs--; |
| 807 | |
| 808 | if (cs->len != PAGE_SIZE) |
| 809 | goto out_fallback; |
| 810 | |
| 811 | if (pipe_buf_steal(cs->pipe, buf) != 0) |
| 812 | goto out_fallback; |
| 813 | |
| 814 | newpage = buf->page; |
| 815 | |
| 816 | if (!PageUptodate(newpage)) |
| 817 | SetPageUptodate(newpage); |
| 818 | |
| 819 | ClearPageMappedToDisk(newpage); |
| 820 | |
| 821 | if (fuse_check_page(newpage) != 0) |
| 822 | goto out_fallback_unlock; |
| 823 | |
| 824 | /* |
| 825 | * This is a new and locked page, it shouldn't be mapped or |
| 826 | * have any special flags on it |
| 827 | */ |
| 828 | if (WARN_ON(page_mapped(oldpage))) |
| 829 | goto out_fallback_unlock; |
| 830 | if (WARN_ON(page_has_private(oldpage))) |
| 831 | goto out_fallback_unlock; |
| 832 | if (WARN_ON(PageDirty(oldpage) || PageWriteback(oldpage))) |
| 833 | goto out_fallback_unlock; |
| 834 | if (WARN_ON(PageMlocked(oldpage))) |
| 835 | goto out_fallback_unlock; |
| 836 | |
| 837 | err = replace_page_cache_page(oldpage, newpage, GFP_KERNEL); |
| 838 | if (err) { |
| 839 | unlock_page(newpage); |
| 840 | goto out_put_old; |
| 841 | } |
| 842 | |
| 843 | get_page(newpage); |
| 844 | |
| 845 | if (!(buf->flags & PIPE_BUF_FLAG_LRU)) |
| 846 | lru_cache_add_file(newpage); |
| 847 | |
| 848 | /* |
| 849 | * Release while we have extra ref on stolen page. Otherwise |
| 850 | * anon_pipe_buf_release() might think the page can be reused. |
| 851 | */ |
| 852 | pipe_buf_release(cs->pipe, buf); |
| 853 | |
| 854 | err = 0; |
| 855 | spin_lock(&cs->req->waitq.lock); |
| 856 | if (test_bit(FR_ABORTED, &cs->req->flags)) |
| 857 | err = -ENOENT; |
| 858 | else |
| 859 | *pagep = newpage; |
| 860 | spin_unlock(&cs->req->waitq.lock); |
| 861 | |
| 862 | if (err) { |
| 863 | unlock_page(newpage); |
| 864 | put_page(newpage); |
| 865 | goto out_put_old; |
| 866 | } |
| 867 | |
| 868 | unlock_page(oldpage); |
| 869 | /* Drop ref for ap->pages[] array */ |
| 870 | put_page(oldpage); |
| 871 | cs->len = 0; |
| 872 | |
| 873 | err = 0; |
| 874 | out_put_old: |
| 875 | /* Drop ref obtained in this function */ |
| 876 | put_page(oldpage); |
| 877 | return err; |
| 878 | |
| 879 | out_fallback_unlock: |
| 880 | unlock_page(newpage); |
| 881 | out_fallback: |
| 882 | cs->pg = buf->page; |
| 883 | cs->offset = buf->offset; |
| 884 | |
| 885 | err = lock_request(cs->req); |
| 886 | if (!err) |
| 887 | err = 1; |
| 888 | |
| 889 | goto out_put_old; |
| 890 | } |
| 891 | |
| 892 | static int fuse_ref_page(struct fuse_copy_state *cs, struct page *page, |
| 893 | unsigned offset, unsigned count) |
| 894 | { |
| 895 | struct pipe_buffer *buf; |
| 896 | int err; |
| 897 | |
| 898 | if (cs->nr_segs == cs->pipe->buffers) |
| 899 | return -EIO; |
| 900 | |
| 901 | get_page(page); |
| 902 | err = unlock_request(cs->req); |
| 903 | if (err) { |
| 904 | put_page(page); |
| 905 | return err; |
| 906 | } |
| 907 | |
| 908 | fuse_copy_finish(cs); |
| 909 | |
| 910 | buf = cs->pipebufs; |
| 911 | buf->page = page; |
| 912 | buf->offset = offset; |
| 913 | buf->len = count; |
| 914 | |
| 915 | cs->pipebufs++; |
| 916 | cs->nr_segs++; |
| 917 | cs->len = 0; |
| 918 | |
| 919 | return 0; |
| 920 | } |
| 921 | |
| 922 | /* |
| 923 | * Copy a page in the request to/from the userspace buffer. Must be |
| 924 | * done atomically |
| 925 | */ |
| 926 | static int fuse_copy_page(struct fuse_copy_state *cs, struct page **pagep, |
| 927 | unsigned offset, unsigned count, int zeroing) |
| 928 | { |
| 929 | int err; |
| 930 | struct page *page = *pagep; |
| 931 | |
| 932 | if (page && zeroing && count < PAGE_SIZE) |
| 933 | clear_highpage(page); |
| 934 | |
| 935 | while (count) { |
| 936 | if (cs->write && cs->pipebufs && page) { |
| 937 | /* |
| 938 | * Can't control lifetime of pipe buffers, so always |
| 939 | * copy user pages. |
| 940 | */ |
| 941 | if (cs->req->args->user_pages) { |
| 942 | err = fuse_copy_fill(cs); |
| 943 | if (err) |
| 944 | return err; |
| 945 | } else { |
| 946 | return fuse_ref_page(cs, page, offset, count); |
| 947 | } |
| 948 | } else if (!cs->len) { |
| 949 | if (cs->move_pages && page && |
| 950 | offset == 0 && count == PAGE_SIZE) { |
| 951 | err = fuse_try_move_page(cs, pagep); |
| 952 | if (err <= 0) |
| 953 | return err; |
| 954 | } else { |
| 955 | err = fuse_copy_fill(cs); |
| 956 | if (err) |
| 957 | return err; |
| 958 | } |
| 959 | } |
| 960 | if (page) { |
| 961 | void *mapaddr = kmap_atomic(page); |
| 962 | void *buf = mapaddr + offset; |
| 963 | offset += fuse_copy_do(cs, &buf, &count); |
| 964 | kunmap_atomic(mapaddr); |
| 965 | } else |
| 966 | offset += fuse_copy_do(cs, NULL, &count); |
| 967 | } |
| 968 | if (page && !cs->write) |
| 969 | flush_dcache_page(page); |
| 970 | return 0; |
| 971 | } |
| 972 | |
| 973 | /* Copy pages in the request to/from userspace buffer */ |
| 974 | static int fuse_copy_pages(struct fuse_copy_state *cs, unsigned nbytes, |
| 975 | int zeroing) |
| 976 | { |
| 977 | unsigned i; |
| 978 | struct fuse_req *req = cs->req; |
| 979 | struct fuse_args_pages *ap = container_of(req->args, typeof(*ap), args); |
| 980 | |
| 981 | |
| 982 | for (i = 0; i < ap->num_pages && (nbytes || zeroing); i++) { |
| 983 | int err; |
| 984 | unsigned int offset = ap->descs[i].offset; |
| 985 | unsigned int count = min(nbytes, ap->descs[i].length); |
| 986 | |
| 987 | err = fuse_copy_page(cs, &ap->pages[i], offset, count, zeroing); |
| 988 | if (err) |
| 989 | return err; |
| 990 | |
| 991 | nbytes -= count; |
| 992 | } |
| 993 | return 0; |
| 994 | } |
| 995 | |
| 996 | /* Copy a single argument in the request to/from userspace buffer */ |
| 997 | static int fuse_copy_one(struct fuse_copy_state *cs, void *val, unsigned size) |
| 998 | { |
| 999 | while (size) { |
| 1000 | if (!cs->len) { |
| 1001 | int err = fuse_copy_fill(cs); |
| 1002 | if (err) |
| 1003 | return err; |
| 1004 | } |
| 1005 | fuse_copy_do(cs, &val, &size); |
| 1006 | } |
| 1007 | return 0; |
| 1008 | } |
| 1009 | |
| 1010 | /* Copy request arguments to/from userspace buffer */ |
| 1011 | static int fuse_copy_args(struct fuse_copy_state *cs, unsigned numargs, |
| 1012 | unsigned argpages, struct fuse_arg *args, |
| 1013 | int zeroing) |
| 1014 | { |
| 1015 | int err = 0; |
| 1016 | unsigned i; |
| 1017 | |
| 1018 | for (i = 0; !err && i < numargs; i++) { |
| 1019 | struct fuse_arg *arg = &args[i]; |
| 1020 | if (i == numargs - 1 && argpages) |
| 1021 | err = fuse_copy_pages(cs, arg->size, zeroing); |
| 1022 | else |
| 1023 | err = fuse_copy_one(cs, arg->value, arg->size); |
| 1024 | } |
| 1025 | return err; |
| 1026 | } |
| 1027 | |
| 1028 | static int forget_pending(struct fuse_iqueue *fiq) |
| 1029 | { |
| 1030 | return fiq->forget_list_head.next != NULL; |
| 1031 | } |
| 1032 | |
| 1033 | static int request_pending(struct fuse_iqueue *fiq) |
| 1034 | { |
| 1035 | return !list_empty(&fiq->pending) || !list_empty(&fiq->interrupts) || |
| 1036 | forget_pending(fiq); |
| 1037 | } |
| 1038 | |
| 1039 | /* |
| 1040 | * Transfer an interrupt request to userspace |
| 1041 | * |
| 1042 | * Unlike other requests this is assembled on demand, without a need |
| 1043 | * to allocate a separate fuse_req structure. |
| 1044 | * |
| 1045 | * Called with fiq->lock held, releases it |
| 1046 | */ |
| 1047 | static int fuse_read_interrupt(struct fuse_iqueue *fiq, |
| 1048 | struct fuse_copy_state *cs, |
| 1049 | size_t nbytes, struct fuse_req *req) |
| 1050 | __releases(fiq->lock) |
| 1051 | { |
| 1052 | struct fuse_in_header ih; |
| 1053 | struct fuse_interrupt_in arg; |
| 1054 | unsigned reqsize = sizeof(ih) + sizeof(arg); |
| 1055 | int err; |
| 1056 | |
| 1057 | list_del_init(&req->intr_entry); |
| 1058 | memset(&ih, 0, sizeof(ih)); |
| 1059 | memset(&arg, 0, sizeof(arg)); |
| 1060 | ih.len = reqsize; |
| 1061 | ih.opcode = FUSE_INTERRUPT; |
| 1062 | ih.unique = (req->in.h.unique | FUSE_INT_REQ_BIT); |
| 1063 | arg.unique = req->in.h.unique; |
| 1064 | |
| 1065 | spin_unlock(&fiq->lock); |
| 1066 | if (nbytes < reqsize) |
| 1067 | return -EINVAL; |
| 1068 | |
| 1069 | err = fuse_copy_one(cs, &ih, sizeof(ih)); |
| 1070 | if (!err) |
| 1071 | err = fuse_copy_one(cs, &arg, sizeof(arg)); |
| 1072 | fuse_copy_finish(cs); |
| 1073 | |
| 1074 | return err ? err : reqsize; |
| 1075 | } |
| 1076 | |
| 1077 | struct fuse_forget_link *fuse_dequeue_forget(struct fuse_iqueue *fiq, |
| 1078 | unsigned int max, |
| 1079 | unsigned int *countp) |
| 1080 | { |
| 1081 | struct fuse_forget_link *head = fiq->forget_list_head.next; |
| 1082 | struct fuse_forget_link **newhead = &head; |
| 1083 | unsigned count; |
| 1084 | |
| 1085 | for (count = 0; *newhead != NULL && count < max; count++) |
| 1086 | newhead = &(*newhead)->next; |
| 1087 | |
| 1088 | fiq->forget_list_head.next = *newhead; |
| 1089 | *newhead = NULL; |
| 1090 | if (fiq->forget_list_head.next == NULL) |
| 1091 | fiq->forget_list_tail = &fiq->forget_list_head; |
| 1092 | |
| 1093 | if (countp != NULL) |
| 1094 | *countp = count; |
| 1095 | |
| 1096 | return head; |
| 1097 | } |
| 1098 | EXPORT_SYMBOL(fuse_dequeue_forget); |
| 1099 | |
| 1100 | static int fuse_read_single_forget(struct fuse_iqueue *fiq, |
| 1101 | struct fuse_copy_state *cs, |
| 1102 | size_t nbytes) |
| 1103 | __releases(fiq->lock) |
| 1104 | { |
| 1105 | int err; |
| 1106 | struct fuse_forget_link *forget = fuse_dequeue_forget(fiq, 1, NULL); |
| 1107 | struct fuse_forget_in arg = { |
| 1108 | .nlookup = forget->forget_one.nlookup, |
| 1109 | }; |
| 1110 | struct fuse_in_header ih = { |
| 1111 | .opcode = FUSE_FORGET, |
| 1112 | .nodeid = forget->forget_one.nodeid, |
| 1113 | .unique = fuse_get_unique(fiq), |
| 1114 | .len = sizeof(ih) + sizeof(arg), |
| 1115 | }; |
| 1116 | |
| 1117 | spin_unlock(&fiq->lock); |
| 1118 | kfree(forget); |
| 1119 | if (nbytes < ih.len) |
| 1120 | return -EINVAL; |
| 1121 | |
| 1122 | err = fuse_copy_one(cs, &ih, sizeof(ih)); |
| 1123 | if (!err) |
| 1124 | err = fuse_copy_one(cs, &arg, sizeof(arg)); |
| 1125 | fuse_copy_finish(cs); |
| 1126 | |
| 1127 | if (err) |
| 1128 | return err; |
| 1129 | |
| 1130 | return ih.len; |
| 1131 | } |
| 1132 | |
| 1133 | static int fuse_read_batch_forget(struct fuse_iqueue *fiq, |
| 1134 | struct fuse_copy_state *cs, size_t nbytes) |
| 1135 | __releases(fiq->lock) |
| 1136 | { |
| 1137 | int err; |
| 1138 | unsigned max_forgets; |
| 1139 | unsigned count; |
| 1140 | struct fuse_forget_link *head; |
| 1141 | struct fuse_batch_forget_in arg = { .count = 0 }; |
| 1142 | struct fuse_in_header ih = { |
| 1143 | .opcode = FUSE_BATCH_FORGET, |
| 1144 | .unique = fuse_get_unique(fiq), |
| 1145 | .len = sizeof(ih) + sizeof(arg), |
| 1146 | }; |
| 1147 | |
| 1148 | if (nbytes < ih.len) { |
| 1149 | spin_unlock(&fiq->lock); |
| 1150 | return -EINVAL; |
| 1151 | } |
| 1152 | |
| 1153 | max_forgets = (nbytes - ih.len) / sizeof(struct fuse_forget_one); |
| 1154 | head = fuse_dequeue_forget(fiq, max_forgets, &count); |
| 1155 | spin_unlock(&fiq->lock); |
| 1156 | |
| 1157 | arg.count = count; |
| 1158 | ih.len += count * sizeof(struct fuse_forget_one); |
| 1159 | err = fuse_copy_one(cs, &ih, sizeof(ih)); |
| 1160 | if (!err) |
| 1161 | err = fuse_copy_one(cs, &arg, sizeof(arg)); |
| 1162 | |
| 1163 | while (head) { |
| 1164 | struct fuse_forget_link *forget = head; |
| 1165 | |
| 1166 | if (!err) { |
| 1167 | err = fuse_copy_one(cs, &forget->forget_one, |
| 1168 | sizeof(forget->forget_one)); |
| 1169 | } |
| 1170 | head = forget->next; |
| 1171 | kfree(forget); |
| 1172 | } |
| 1173 | |
| 1174 | fuse_copy_finish(cs); |
| 1175 | |
| 1176 | if (err) |
| 1177 | return err; |
| 1178 | |
| 1179 | return ih.len; |
| 1180 | } |
| 1181 | |
| 1182 | static int fuse_read_forget(struct fuse_conn *fc, struct fuse_iqueue *fiq, |
| 1183 | struct fuse_copy_state *cs, |
| 1184 | size_t nbytes) |
| 1185 | __releases(fiq->lock) |
| 1186 | { |
| 1187 | if (fc->minor < 16 || fiq->forget_list_head.next->next == NULL) |
| 1188 | return fuse_read_single_forget(fiq, cs, nbytes); |
| 1189 | else |
| 1190 | return fuse_read_batch_forget(fiq, cs, nbytes); |
| 1191 | } |
| 1192 | |
| 1193 | /* |
| 1194 | * Read a single request into the userspace filesystem's buffer. This |
| 1195 | * function waits until a request is available, then removes it from |
| 1196 | * the pending list and copies request data to userspace buffer. If |
| 1197 | * no reply is needed (FORGET) or request has been aborted or there |
| 1198 | * was an error during the copying then it's finished by calling |
| 1199 | * fuse_request_end(). Otherwise add it to the processing list, and set |
| 1200 | * the 'sent' flag. |
| 1201 | */ |
| 1202 | static ssize_t fuse_dev_do_read(struct fuse_dev *fud, struct file *file, |
| 1203 | struct fuse_copy_state *cs, size_t nbytes) |
| 1204 | { |
| 1205 | ssize_t err; |
| 1206 | struct fuse_conn *fc = fud->fc; |
| 1207 | struct fuse_iqueue *fiq = &fc->iq; |
| 1208 | struct fuse_pqueue *fpq = &fud->pq; |
| 1209 | struct fuse_req *req; |
| 1210 | struct fuse_args *args; |
| 1211 | unsigned reqsize; |
| 1212 | unsigned int hash; |
| 1213 | |
| 1214 | /* |
| 1215 | * Require sane minimum read buffer - that has capacity for fixed part |
| 1216 | * of any request header + negotiated max_write room for data. |
| 1217 | * |
| 1218 | * Historically libfuse reserves 4K for fixed header room, but e.g. |
| 1219 | * GlusterFS reserves only 80 bytes |
| 1220 | * |
| 1221 | * = `sizeof(fuse_in_header) + sizeof(fuse_write_in)` |
| 1222 | * |
| 1223 | * which is the absolute minimum any sane filesystem should be using |
| 1224 | * for header room. |
| 1225 | */ |
| 1226 | if (nbytes < max_t(size_t, FUSE_MIN_READ_BUFFER, |
| 1227 | sizeof(struct fuse_in_header) + |
| 1228 | sizeof(struct fuse_write_in) + |
| 1229 | fc->max_write)) |
| 1230 | return -EINVAL; |
| 1231 | |
| 1232 | restart: |
| 1233 | for (;;) { |
| 1234 | spin_lock(&fiq->lock); |
| 1235 | if (!fiq->connected || request_pending(fiq)) |
| 1236 | break; |
| 1237 | spin_unlock(&fiq->lock); |
| 1238 | |
| 1239 | if (file->f_flags & O_NONBLOCK) |
| 1240 | return -EAGAIN; |
| 1241 | err = wait_event_interruptible_exclusive(fiq->waitq, |
| 1242 | !fiq->connected || request_pending(fiq)); |
| 1243 | if (err) |
| 1244 | return err; |
| 1245 | } |
| 1246 | |
| 1247 | if (!fiq->connected) { |
| 1248 | err = fc->aborted ? -ECONNABORTED : -ENODEV; |
| 1249 | goto err_unlock; |
| 1250 | } |
| 1251 | |
| 1252 | if (!list_empty(&fiq->interrupts)) { |
| 1253 | req = list_entry(fiq->interrupts.next, struct fuse_req, |
| 1254 | intr_entry); |
| 1255 | return fuse_read_interrupt(fiq, cs, nbytes, req); |
| 1256 | } |
| 1257 | |
| 1258 | if (forget_pending(fiq)) { |
| 1259 | if (list_empty(&fiq->pending) || fiq->forget_batch-- > 0) |
| 1260 | return fuse_read_forget(fc, fiq, cs, nbytes); |
| 1261 | |
| 1262 | if (fiq->forget_batch <= -8) |
| 1263 | fiq->forget_batch = 16; |
| 1264 | } |
| 1265 | |
| 1266 | req = list_entry(fiq->pending.next, struct fuse_req, list); |
| 1267 | clear_bit(FR_PENDING, &req->flags); |
| 1268 | list_del_init(&req->list); |
| 1269 | spin_unlock(&fiq->lock); |
| 1270 | |
| 1271 | args = req->args; |
| 1272 | reqsize = req->in.h.len; |
| 1273 | |
| 1274 | /* If request is too large, reply with an error and restart the read */ |
| 1275 | if (nbytes < reqsize) { |
| 1276 | req->out.h.error = -EIO; |
| 1277 | /* SETXATTR is special, since it may contain too large data */ |
| 1278 | if (args->opcode == FUSE_SETXATTR) |
| 1279 | req->out.h.error = -E2BIG; |
| 1280 | fuse_request_end(fc, req); |
| 1281 | goto restart; |
| 1282 | } |
| 1283 | spin_lock(&fpq->lock); |
| 1284 | /* |
| 1285 | * Must not put request on fpq->io queue after having been shut down by |
| 1286 | * fuse_abort_conn() |
| 1287 | */ |
| 1288 | if (!fpq->connected) { |
| 1289 | req->out.h.error = err = -ECONNABORTED; |
| 1290 | goto out_end; |
| 1291 | |
| 1292 | } |
| 1293 | list_add(&req->list, &fpq->io); |
| 1294 | spin_unlock(&fpq->lock); |
| 1295 | cs->req = req; |
| 1296 | err = fuse_copy_one(cs, &req->in.h, sizeof(req->in.h)); |
| 1297 | if (!err) |
| 1298 | err = fuse_copy_args(cs, args->in_numargs, args->in_pages, |
| 1299 | (struct fuse_arg *) args->in_args, 0); |
| 1300 | fuse_copy_finish(cs); |
| 1301 | spin_lock(&fpq->lock); |
| 1302 | clear_bit(FR_LOCKED, &req->flags); |
| 1303 | if (!fpq->connected) { |
| 1304 | err = fc->aborted ? -ECONNABORTED : -ENODEV; |
| 1305 | goto out_end; |
| 1306 | } |
| 1307 | if (err) { |
| 1308 | req->out.h.error = -EIO; |
| 1309 | goto out_end; |
| 1310 | } |
| 1311 | if (!test_bit(FR_ISREPLY, &req->flags)) { |
| 1312 | err = reqsize; |
| 1313 | goto out_end; |
| 1314 | } |
| 1315 | hash = fuse_req_hash(req->in.h.unique); |
| 1316 | list_move_tail(&req->list, &fpq->processing[hash]); |
| 1317 | __fuse_get_request(req); |
| 1318 | set_bit(FR_SENT, &req->flags); |
| 1319 | spin_unlock(&fpq->lock); |
| 1320 | /* matches barrier in request_wait_answer() */ |
| 1321 | smp_mb__after_atomic(); |
| 1322 | if (test_bit(FR_INTERRUPTED, &req->flags)) |
| 1323 | queue_interrupt(fiq, req); |
| 1324 | fuse_put_request(fc, req); |
| 1325 | |
| 1326 | return reqsize; |
| 1327 | |
| 1328 | out_end: |
| 1329 | if (!test_bit(FR_PRIVATE, &req->flags)) |
| 1330 | list_del_init(&req->list); |
| 1331 | spin_unlock(&fpq->lock); |
| 1332 | fuse_request_end(fc, req); |
| 1333 | return err; |
| 1334 | |
| 1335 | err_unlock: |
| 1336 | spin_unlock(&fiq->lock); |
| 1337 | return err; |
| 1338 | } |
| 1339 | |
| 1340 | static int fuse_dev_open(struct inode *inode, struct file *file) |
| 1341 | { |
| 1342 | /* |
| 1343 | * The fuse device's file's private_data is used to hold |
| 1344 | * the fuse_conn(ection) when it is mounted, and is used to |
| 1345 | * keep track of whether the file has been mounted already. |
| 1346 | */ |
| 1347 | file->private_data = NULL; |
| 1348 | return 0; |
| 1349 | } |
| 1350 | |
| 1351 | static ssize_t fuse_dev_read(struct kiocb *iocb, struct iov_iter *to) |
| 1352 | { |
| 1353 | struct fuse_copy_state cs; |
| 1354 | struct file *file = iocb->ki_filp; |
| 1355 | struct fuse_dev *fud = fuse_get_dev(file); |
| 1356 | |
| 1357 | if (!fud) |
| 1358 | return -EPERM; |
| 1359 | |
| 1360 | if (!iter_is_iovec(to)) |
| 1361 | return -EINVAL; |
| 1362 | |
| 1363 | fuse_copy_init(&cs, 1, to); |
| 1364 | |
| 1365 | return fuse_dev_do_read(fud, file, &cs, iov_iter_count(to)); |
| 1366 | } |
| 1367 | |
| 1368 | static ssize_t fuse_dev_splice_read(struct file *in, loff_t *ppos, |
| 1369 | struct pipe_inode_info *pipe, |
| 1370 | size_t len, unsigned int flags) |
| 1371 | { |
| 1372 | int total, ret; |
| 1373 | int page_nr = 0; |
| 1374 | struct pipe_buffer *bufs; |
| 1375 | struct fuse_copy_state cs; |
| 1376 | struct fuse_dev *fud = fuse_get_dev(in); |
| 1377 | |
| 1378 | if (!fud) |
| 1379 | return -EPERM; |
| 1380 | |
| 1381 | bufs = kvmalloc_array(pipe->buffers, sizeof(struct pipe_buffer), |
| 1382 | GFP_KERNEL); |
| 1383 | if (!bufs) |
| 1384 | return -ENOMEM; |
| 1385 | |
| 1386 | fuse_copy_init(&cs, 1, NULL); |
| 1387 | cs.pipebufs = bufs; |
| 1388 | cs.pipe = pipe; |
| 1389 | ret = fuse_dev_do_read(fud, in, &cs, len); |
| 1390 | if (ret < 0) |
| 1391 | goto out; |
| 1392 | |
| 1393 | if (pipe->nrbufs + cs.nr_segs > pipe->buffers) { |
| 1394 | ret = -EIO; |
| 1395 | goto out; |
| 1396 | } |
| 1397 | |
| 1398 | for (ret = total = 0; page_nr < cs.nr_segs; total += ret) { |
| 1399 | /* |
| 1400 | * Need to be careful about this. Having buf->ops in module |
| 1401 | * code can Oops if the buffer persists after module unload. |
| 1402 | */ |
| 1403 | bufs[page_nr].ops = &nosteal_pipe_buf_ops; |
| 1404 | bufs[page_nr].flags = 0; |
| 1405 | ret = add_to_pipe(pipe, &bufs[page_nr++]); |
| 1406 | if (unlikely(ret < 0)) |
| 1407 | break; |
| 1408 | } |
| 1409 | if (total) |
| 1410 | ret = total; |
| 1411 | out: |
| 1412 | for (; page_nr < cs.nr_segs; page_nr++) |
| 1413 | put_page(bufs[page_nr].page); |
| 1414 | |
| 1415 | kvfree(bufs); |
| 1416 | return ret; |
| 1417 | } |
| 1418 | |
| 1419 | static int fuse_notify_poll(struct fuse_conn *fc, unsigned int size, |
| 1420 | struct fuse_copy_state *cs) |
| 1421 | { |
| 1422 | struct fuse_notify_poll_wakeup_out outarg; |
| 1423 | int err = -EINVAL; |
| 1424 | |
| 1425 | if (size != sizeof(outarg)) |
| 1426 | goto err; |
| 1427 | |
| 1428 | err = fuse_copy_one(cs, &outarg, sizeof(outarg)); |
| 1429 | if (err) |
| 1430 | goto err; |
| 1431 | |
| 1432 | fuse_copy_finish(cs); |
| 1433 | return fuse_notify_poll_wakeup(fc, &outarg); |
| 1434 | |
| 1435 | err: |
| 1436 | fuse_copy_finish(cs); |
| 1437 | return err; |
| 1438 | } |
| 1439 | |
| 1440 | static int fuse_notify_inval_inode(struct fuse_conn *fc, unsigned int size, |
| 1441 | struct fuse_copy_state *cs) |
| 1442 | { |
| 1443 | struct fuse_notify_inval_inode_out outarg; |
| 1444 | int err = -EINVAL; |
| 1445 | |
| 1446 | if (size != sizeof(outarg)) |
| 1447 | goto err; |
| 1448 | |
| 1449 | err = fuse_copy_one(cs, &outarg, sizeof(outarg)); |
| 1450 | if (err) |
| 1451 | goto err; |
| 1452 | fuse_copy_finish(cs); |
| 1453 | |
| 1454 | down_read(&fc->killsb); |
| 1455 | err = -ENOENT; |
| 1456 | if (fc->sb) { |
| 1457 | err = fuse_reverse_inval_inode(fc->sb, outarg.ino, |
| 1458 | outarg.off, outarg.len); |
| 1459 | } |
| 1460 | up_read(&fc->killsb); |
| 1461 | return err; |
| 1462 | |
| 1463 | err: |
| 1464 | fuse_copy_finish(cs); |
| 1465 | return err; |
| 1466 | } |
| 1467 | |
| 1468 | static int fuse_notify_inval_entry(struct fuse_conn *fc, unsigned int size, |
| 1469 | struct fuse_copy_state *cs) |
| 1470 | { |
| 1471 | struct fuse_notify_inval_entry_out outarg; |
| 1472 | int err = -ENOMEM; |
| 1473 | char *buf; |
| 1474 | struct qstr name; |
| 1475 | |
| 1476 | buf = kzalloc(FUSE_NAME_MAX + 1, GFP_KERNEL); |
| 1477 | if (!buf) |
| 1478 | goto err; |
| 1479 | |
| 1480 | err = -EINVAL; |
| 1481 | if (size < sizeof(outarg)) |
| 1482 | goto err; |
| 1483 | |
| 1484 | err = fuse_copy_one(cs, &outarg, sizeof(outarg)); |
| 1485 | if (err) |
| 1486 | goto err; |
| 1487 | |
| 1488 | err = -ENAMETOOLONG; |
| 1489 | if (outarg.namelen > FUSE_NAME_MAX) |
| 1490 | goto err; |
| 1491 | |
| 1492 | err = -EINVAL; |
| 1493 | if (size != sizeof(outarg) + outarg.namelen + 1) |
| 1494 | goto err; |
| 1495 | |
| 1496 | name.name = buf; |
| 1497 | name.len = outarg.namelen; |
| 1498 | err = fuse_copy_one(cs, buf, outarg.namelen + 1); |
| 1499 | if (err) |
| 1500 | goto err; |
| 1501 | fuse_copy_finish(cs); |
| 1502 | buf[outarg.namelen] = 0; |
| 1503 | |
| 1504 | down_read(&fc->killsb); |
| 1505 | err = -ENOENT; |
| 1506 | if (fc->sb) |
| 1507 | err = fuse_reverse_inval_entry(fc->sb, outarg.parent, 0, &name); |
| 1508 | up_read(&fc->killsb); |
| 1509 | kfree(buf); |
| 1510 | return err; |
| 1511 | |
| 1512 | err: |
| 1513 | kfree(buf); |
| 1514 | fuse_copy_finish(cs); |
| 1515 | return err; |
| 1516 | } |
| 1517 | |
| 1518 | static int fuse_notify_delete(struct fuse_conn *fc, unsigned int size, |
| 1519 | struct fuse_copy_state *cs) |
| 1520 | { |
| 1521 | struct fuse_notify_delete_out outarg; |
| 1522 | int err = -ENOMEM; |
| 1523 | char *buf; |
| 1524 | struct qstr name; |
| 1525 | |
| 1526 | buf = kzalloc(FUSE_NAME_MAX + 1, GFP_KERNEL); |
| 1527 | if (!buf) |
| 1528 | goto err; |
| 1529 | |
| 1530 | err = -EINVAL; |
| 1531 | if (size < sizeof(outarg)) |
| 1532 | goto err; |
| 1533 | |
| 1534 | err = fuse_copy_one(cs, &outarg, sizeof(outarg)); |
| 1535 | if (err) |
| 1536 | goto err; |
| 1537 | |
| 1538 | err = -ENAMETOOLONG; |
| 1539 | if (outarg.namelen > FUSE_NAME_MAX) |
| 1540 | goto err; |
| 1541 | |
| 1542 | err = -EINVAL; |
| 1543 | if (size != sizeof(outarg) + outarg.namelen + 1) |
| 1544 | goto err; |
| 1545 | |
| 1546 | name.name = buf; |
| 1547 | name.len = outarg.namelen; |
| 1548 | err = fuse_copy_one(cs, buf, outarg.namelen + 1); |
| 1549 | if (err) |
| 1550 | goto err; |
| 1551 | fuse_copy_finish(cs); |
| 1552 | buf[outarg.namelen] = 0; |
| 1553 | |
| 1554 | down_read(&fc->killsb); |
| 1555 | err = -ENOENT; |
| 1556 | if (fc->sb) |
| 1557 | err = fuse_reverse_inval_entry(fc->sb, outarg.parent, |
| 1558 | outarg.child, &name); |
| 1559 | up_read(&fc->killsb); |
| 1560 | kfree(buf); |
| 1561 | return err; |
| 1562 | |
| 1563 | err: |
| 1564 | kfree(buf); |
| 1565 | fuse_copy_finish(cs); |
| 1566 | return err; |
| 1567 | } |
| 1568 | |
| 1569 | static int fuse_notify_store(struct fuse_conn *fc, unsigned int size, |
| 1570 | struct fuse_copy_state *cs) |
| 1571 | { |
| 1572 | struct fuse_notify_store_out outarg; |
| 1573 | struct inode *inode; |
| 1574 | struct address_space *mapping; |
| 1575 | u64 nodeid; |
| 1576 | int err; |
| 1577 | pgoff_t index; |
| 1578 | unsigned int offset; |
| 1579 | unsigned int num; |
| 1580 | loff_t file_size; |
| 1581 | loff_t end; |
| 1582 | |
| 1583 | err = -EINVAL; |
| 1584 | if (size < sizeof(outarg)) |
| 1585 | goto out_finish; |
| 1586 | |
| 1587 | err = fuse_copy_one(cs, &outarg, sizeof(outarg)); |
| 1588 | if (err) |
| 1589 | goto out_finish; |
| 1590 | |
| 1591 | err = -EINVAL; |
| 1592 | if (size - sizeof(outarg) != outarg.size) |
| 1593 | goto out_finish; |
| 1594 | |
| 1595 | nodeid = outarg.nodeid; |
| 1596 | |
| 1597 | down_read(&fc->killsb); |
| 1598 | |
| 1599 | err = -ENOENT; |
| 1600 | if (!fc->sb) |
| 1601 | goto out_up_killsb; |
| 1602 | |
| 1603 | inode = ilookup5(fc->sb, nodeid, fuse_inode_eq, &nodeid); |
| 1604 | if (!inode) |
| 1605 | goto out_up_killsb; |
| 1606 | |
| 1607 | mapping = inode->i_mapping; |
| 1608 | index = outarg.offset >> PAGE_SHIFT; |
| 1609 | offset = outarg.offset & ~PAGE_MASK; |
| 1610 | file_size = i_size_read(inode); |
| 1611 | end = outarg.offset + outarg.size; |
| 1612 | if (end > file_size) { |
| 1613 | file_size = end; |
| 1614 | fuse_write_update_size(inode, file_size); |
| 1615 | } |
| 1616 | |
| 1617 | num = outarg.size; |
| 1618 | while (num) { |
| 1619 | struct page *page; |
| 1620 | unsigned int this_num; |
| 1621 | |
| 1622 | err = -ENOMEM; |
| 1623 | page = find_or_create_page(mapping, index, |
| 1624 | mapping_gfp_mask(mapping)); |
| 1625 | if (!page) |
| 1626 | goto out_iput; |
| 1627 | |
| 1628 | this_num = min_t(unsigned, num, PAGE_SIZE - offset); |
| 1629 | err = fuse_copy_page(cs, &page, offset, this_num, 0); |
| 1630 | if (!PageUptodate(page) && !err && offset == 0 && |
| 1631 | (this_num == PAGE_SIZE || file_size == end)) { |
| 1632 | zero_user_segment(page, this_num, PAGE_SIZE); |
| 1633 | SetPageUptodate(page); |
| 1634 | } |
| 1635 | unlock_page(page); |
| 1636 | put_page(page); |
| 1637 | |
| 1638 | if (err) |
| 1639 | goto out_iput; |
| 1640 | |
| 1641 | num -= this_num; |
| 1642 | offset = 0; |
| 1643 | index++; |
| 1644 | } |
| 1645 | |
| 1646 | err = 0; |
| 1647 | |
| 1648 | out_iput: |
| 1649 | iput(inode); |
| 1650 | out_up_killsb: |
| 1651 | up_read(&fc->killsb); |
| 1652 | out_finish: |
| 1653 | fuse_copy_finish(cs); |
| 1654 | return err; |
| 1655 | } |
| 1656 | |
| 1657 | struct fuse_retrieve_args { |
| 1658 | struct fuse_args_pages ap; |
| 1659 | struct fuse_notify_retrieve_in inarg; |
| 1660 | }; |
| 1661 | |
| 1662 | static void fuse_retrieve_end(struct fuse_conn *fc, struct fuse_args *args, |
| 1663 | int error) |
| 1664 | { |
| 1665 | struct fuse_retrieve_args *ra = |
| 1666 | container_of(args, typeof(*ra), ap.args); |
| 1667 | |
| 1668 | release_pages(ra->ap.pages, ra->ap.num_pages); |
| 1669 | kfree(ra); |
| 1670 | } |
| 1671 | |
| 1672 | static int fuse_retrieve(struct fuse_conn *fc, struct inode *inode, |
| 1673 | struct fuse_notify_retrieve_out *outarg) |
| 1674 | { |
| 1675 | int err; |
| 1676 | struct address_space *mapping = inode->i_mapping; |
| 1677 | pgoff_t index; |
| 1678 | loff_t file_size; |
| 1679 | unsigned int num; |
| 1680 | unsigned int offset; |
| 1681 | size_t total_len = 0; |
| 1682 | unsigned int num_pages; |
| 1683 | struct fuse_retrieve_args *ra; |
| 1684 | size_t args_size = sizeof(*ra); |
| 1685 | struct fuse_args_pages *ap; |
| 1686 | struct fuse_args *args; |
| 1687 | |
| 1688 | offset = outarg->offset & ~PAGE_MASK; |
| 1689 | file_size = i_size_read(inode); |
| 1690 | |
| 1691 | num = min(outarg->size, fc->max_write); |
| 1692 | if (outarg->offset > file_size) |
| 1693 | num = 0; |
| 1694 | else if (outarg->offset + num > file_size) |
| 1695 | num = file_size - outarg->offset; |
| 1696 | |
| 1697 | num_pages = (num + offset + PAGE_SIZE - 1) >> PAGE_SHIFT; |
| 1698 | num_pages = min(num_pages, fc->max_pages); |
| 1699 | |
| 1700 | args_size += num_pages * (sizeof(ap->pages[0]) + sizeof(ap->descs[0])); |
| 1701 | |
| 1702 | ra = kzalloc(args_size, GFP_KERNEL); |
| 1703 | if (!ra) |
| 1704 | return -ENOMEM; |
| 1705 | |
| 1706 | ap = &ra->ap; |
| 1707 | ap->pages = (void *) (ra + 1); |
| 1708 | ap->descs = (void *) (ap->pages + num_pages); |
| 1709 | |
| 1710 | args = &ap->args; |
| 1711 | args->nodeid = outarg->nodeid; |
| 1712 | args->opcode = FUSE_NOTIFY_REPLY; |
| 1713 | args->in_numargs = 2; |
| 1714 | args->in_pages = true; |
| 1715 | args->end = fuse_retrieve_end; |
| 1716 | |
| 1717 | index = outarg->offset >> PAGE_SHIFT; |
| 1718 | |
| 1719 | while (num && ap->num_pages < num_pages) { |
| 1720 | struct page *page; |
| 1721 | unsigned int this_num; |
| 1722 | |
| 1723 | page = find_get_page(mapping, index); |
| 1724 | if (!page) |
| 1725 | break; |
| 1726 | |
| 1727 | this_num = min_t(unsigned, num, PAGE_SIZE - offset); |
| 1728 | ap->pages[ap->num_pages] = page; |
| 1729 | ap->descs[ap->num_pages].offset = offset; |
| 1730 | ap->descs[ap->num_pages].length = this_num; |
| 1731 | ap->num_pages++; |
| 1732 | |
| 1733 | offset = 0; |
| 1734 | num -= this_num; |
| 1735 | total_len += this_num; |
| 1736 | index++; |
| 1737 | } |
| 1738 | ra->inarg.offset = outarg->offset; |
| 1739 | ra->inarg.size = total_len; |
| 1740 | args->in_args[0].size = sizeof(ra->inarg); |
| 1741 | args->in_args[0].value = &ra->inarg; |
| 1742 | args->in_args[1].size = total_len; |
| 1743 | |
| 1744 | err = fuse_simple_notify_reply(fc, args, outarg->notify_unique); |
| 1745 | if (err) |
| 1746 | fuse_retrieve_end(fc, args, err); |
| 1747 | |
| 1748 | return err; |
| 1749 | } |
| 1750 | |
| 1751 | static int fuse_notify_retrieve(struct fuse_conn *fc, unsigned int size, |
| 1752 | struct fuse_copy_state *cs) |
| 1753 | { |
| 1754 | struct fuse_notify_retrieve_out outarg; |
| 1755 | struct inode *inode; |
| 1756 | int err; |
| 1757 | |
| 1758 | err = -EINVAL; |
| 1759 | if (size != sizeof(outarg)) |
| 1760 | goto copy_finish; |
| 1761 | |
| 1762 | err = fuse_copy_one(cs, &outarg, sizeof(outarg)); |
| 1763 | if (err) |
| 1764 | goto copy_finish; |
| 1765 | |
| 1766 | fuse_copy_finish(cs); |
| 1767 | |
| 1768 | down_read(&fc->killsb); |
| 1769 | err = -ENOENT; |
| 1770 | if (fc->sb) { |
| 1771 | u64 nodeid = outarg.nodeid; |
| 1772 | |
| 1773 | inode = ilookup5(fc->sb, nodeid, fuse_inode_eq, &nodeid); |
| 1774 | if (inode) { |
| 1775 | err = fuse_retrieve(fc, inode, &outarg); |
| 1776 | iput(inode); |
| 1777 | } |
| 1778 | } |
| 1779 | up_read(&fc->killsb); |
| 1780 | |
| 1781 | return err; |
| 1782 | |
| 1783 | copy_finish: |
| 1784 | fuse_copy_finish(cs); |
| 1785 | return err; |
| 1786 | } |
| 1787 | |
| 1788 | static int fuse_notify(struct fuse_conn *fc, enum fuse_notify_code code, |
| 1789 | unsigned int size, struct fuse_copy_state *cs) |
| 1790 | { |
| 1791 | /* Don't try to move pages (yet) */ |
| 1792 | cs->move_pages = 0; |
| 1793 | |
| 1794 | switch (code) { |
| 1795 | case FUSE_NOTIFY_POLL: |
| 1796 | return fuse_notify_poll(fc, size, cs); |
| 1797 | |
| 1798 | case FUSE_NOTIFY_INVAL_INODE: |
| 1799 | return fuse_notify_inval_inode(fc, size, cs); |
| 1800 | |
| 1801 | case FUSE_NOTIFY_INVAL_ENTRY: |
| 1802 | return fuse_notify_inval_entry(fc, size, cs); |
| 1803 | |
| 1804 | case FUSE_NOTIFY_STORE: |
| 1805 | return fuse_notify_store(fc, size, cs); |
| 1806 | |
| 1807 | case FUSE_NOTIFY_RETRIEVE: |
| 1808 | return fuse_notify_retrieve(fc, size, cs); |
| 1809 | |
| 1810 | case FUSE_NOTIFY_DELETE: |
| 1811 | return fuse_notify_delete(fc, size, cs); |
| 1812 | |
| 1813 | default: |
| 1814 | fuse_copy_finish(cs); |
| 1815 | return -EINVAL; |
| 1816 | } |
| 1817 | } |
| 1818 | |
| 1819 | /* Look up request on processing list by unique ID */ |
| 1820 | static struct fuse_req *request_find(struct fuse_pqueue *fpq, u64 unique) |
| 1821 | { |
| 1822 | unsigned int hash = fuse_req_hash(unique); |
| 1823 | struct fuse_req *req; |
| 1824 | |
| 1825 | list_for_each_entry(req, &fpq->processing[hash], list) { |
| 1826 | if (req->in.h.unique == unique) |
| 1827 | return req; |
| 1828 | } |
| 1829 | return NULL; |
| 1830 | } |
| 1831 | |
| 1832 | static int copy_out_args(struct fuse_copy_state *cs, struct fuse_args *args, |
| 1833 | unsigned nbytes) |
| 1834 | { |
| 1835 | unsigned reqsize = sizeof(struct fuse_out_header); |
| 1836 | |
| 1837 | reqsize += fuse_len_args(args->out_numargs, args->out_args); |
| 1838 | |
| 1839 | if (reqsize < nbytes || (reqsize > nbytes && !args->out_argvar)) |
| 1840 | return -EINVAL; |
| 1841 | else if (reqsize > nbytes) { |
| 1842 | struct fuse_arg *lastarg = &args->out_args[args->out_numargs-1]; |
| 1843 | unsigned diffsize = reqsize - nbytes; |
| 1844 | |
| 1845 | if (diffsize > lastarg->size) |
| 1846 | return -EINVAL; |
| 1847 | lastarg->size -= diffsize; |
| 1848 | } |
| 1849 | return fuse_copy_args(cs, args->out_numargs, args->out_pages, |
| 1850 | args->out_args, args->page_zeroing); |
| 1851 | } |
| 1852 | |
| 1853 | /* |
| 1854 | * Write a single reply to a request. First the header is copied from |
| 1855 | * the write buffer. The request is then searched on the processing |
| 1856 | * list by the unique ID found in the header. If found, then remove |
| 1857 | * it from the list and copy the rest of the buffer to the request. |
| 1858 | * The request is finished by calling fuse_request_end(). |
| 1859 | */ |
| 1860 | static ssize_t fuse_dev_do_write(struct fuse_dev *fud, |
| 1861 | struct fuse_copy_state *cs, size_t nbytes) |
| 1862 | { |
| 1863 | int err; |
| 1864 | struct fuse_conn *fc = fud->fc; |
| 1865 | struct fuse_pqueue *fpq = &fud->pq; |
| 1866 | struct fuse_req *req; |
| 1867 | struct fuse_out_header oh; |
| 1868 | |
| 1869 | err = -EINVAL; |
| 1870 | if (nbytes < sizeof(struct fuse_out_header)) |
| 1871 | goto out; |
| 1872 | |
| 1873 | err = fuse_copy_one(cs, &oh, sizeof(oh)); |
| 1874 | if (err) |
| 1875 | goto copy_finish; |
| 1876 | |
| 1877 | err = -EINVAL; |
| 1878 | if (oh.len != nbytes) |
| 1879 | goto copy_finish; |
| 1880 | |
| 1881 | /* |
| 1882 | * Zero oh.unique indicates unsolicited notification message |
| 1883 | * and error contains notification code. |
| 1884 | */ |
| 1885 | if (!oh.unique) { |
| 1886 | err = fuse_notify(fc, oh.error, nbytes - sizeof(oh), cs); |
| 1887 | goto out; |
| 1888 | } |
| 1889 | |
| 1890 | err = -EINVAL; |
| 1891 | if (oh.error <= -512 || oh.error > 0) |
| 1892 | goto copy_finish; |
| 1893 | |
| 1894 | spin_lock(&fpq->lock); |
| 1895 | req = NULL; |
| 1896 | if (fpq->connected) |
| 1897 | req = request_find(fpq, oh.unique & ~FUSE_INT_REQ_BIT); |
| 1898 | |
| 1899 | err = -ENOENT; |
| 1900 | if (!req) { |
| 1901 | spin_unlock(&fpq->lock); |
| 1902 | goto copy_finish; |
| 1903 | } |
| 1904 | |
| 1905 | /* Is it an interrupt reply ID? */ |
| 1906 | if (oh.unique & FUSE_INT_REQ_BIT) { |
| 1907 | __fuse_get_request(req); |
| 1908 | spin_unlock(&fpq->lock); |
| 1909 | |
| 1910 | err = 0; |
| 1911 | if (nbytes != sizeof(struct fuse_out_header)) |
| 1912 | err = -EINVAL; |
| 1913 | else if (oh.error == -ENOSYS) |
| 1914 | fc->no_interrupt = 1; |
| 1915 | else if (oh.error == -EAGAIN) |
| 1916 | err = queue_interrupt(&fc->iq, req); |
| 1917 | |
| 1918 | fuse_put_request(fc, req); |
| 1919 | |
| 1920 | goto copy_finish; |
| 1921 | } |
| 1922 | |
| 1923 | clear_bit(FR_SENT, &req->flags); |
| 1924 | list_move(&req->list, &fpq->io); |
| 1925 | req->out.h = oh; |
| 1926 | set_bit(FR_LOCKED, &req->flags); |
| 1927 | spin_unlock(&fpq->lock); |
| 1928 | cs->req = req; |
| 1929 | if (!req->args->page_replace) |
| 1930 | cs->move_pages = 0; |
| 1931 | |
| 1932 | if (oh.error) |
| 1933 | err = nbytes != sizeof(oh) ? -EINVAL : 0; |
| 1934 | else |
| 1935 | err = copy_out_args(cs, req->args, nbytes); |
| 1936 | fuse_copy_finish(cs); |
| 1937 | |
| 1938 | if (!err && req->in.h.opcode == FUSE_CANONICAL_PATH) { |
| 1939 | char *path = (char *)req->args->out_args[0].value; |
| 1940 | |
| 1941 | path[req->args->out_args[0].size - 1] = 0; |
| 1942 | req->out.h.error = |
| 1943 | kern_path(path, 0, req->args->canonical_path); |
| 1944 | } |
| 1945 | |
| 1946 | spin_lock(&fpq->lock); |
| 1947 | clear_bit(FR_LOCKED, &req->flags); |
| 1948 | if (!fpq->connected) |
| 1949 | err = -ENOENT; |
| 1950 | else if (err) |
| 1951 | req->out.h.error = -EIO; |
| 1952 | if (!test_bit(FR_PRIVATE, &req->flags)) |
| 1953 | list_del_init(&req->list); |
| 1954 | spin_unlock(&fpq->lock); |
| 1955 | |
| 1956 | fuse_request_end(fc, req); |
| 1957 | out: |
| 1958 | return err ? err : nbytes; |
| 1959 | |
| 1960 | copy_finish: |
| 1961 | fuse_copy_finish(cs); |
| 1962 | goto out; |
| 1963 | } |
| 1964 | |
| 1965 | static ssize_t fuse_dev_write(struct kiocb *iocb, struct iov_iter *from) |
| 1966 | { |
| 1967 | struct fuse_copy_state cs; |
| 1968 | struct fuse_dev *fud = fuse_get_dev(iocb->ki_filp); |
| 1969 | |
| 1970 | if (!fud) |
| 1971 | return -EPERM; |
| 1972 | |
| 1973 | if (!iter_is_iovec(from)) |
| 1974 | return -EINVAL; |
| 1975 | |
| 1976 | fuse_copy_init(&cs, 0, from); |
| 1977 | |
| 1978 | return fuse_dev_do_write(fud, &cs, iov_iter_count(from)); |
| 1979 | } |
| 1980 | |
| 1981 | static ssize_t fuse_dev_splice_write(struct pipe_inode_info *pipe, |
| 1982 | struct file *out, loff_t *ppos, |
| 1983 | size_t len, unsigned int flags) |
| 1984 | { |
| 1985 | unsigned nbuf; |
| 1986 | unsigned idx; |
| 1987 | struct pipe_buffer *bufs; |
| 1988 | struct fuse_copy_state cs; |
| 1989 | struct fuse_dev *fud; |
| 1990 | size_t rem; |
| 1991 | ssize_t ret; |
| 1992 | |
| 1993 | fud = fuse_get_dev(out); |
| 1994 | if (!fud) |
| 1995 | return -EPERM; |
| 1996 | |
| 1997 | pipe_lock(pipe); |
| 1998 | |
| 1999 | bufs = kvmalloc_array(pipe->nrbufs, sizeof(struct pipe_buffer), |
| 2000 | GFP_KERNEL); |
| 2001 | if (!bufs) { |
| 2002 | pipe_unlock(pipe); |
| 2003 | return -ENOMEM; |
| 2004 | } |
| 2005 | |
| 2006 | nbuf = 0; |
| 2007 | rem = 0; |
| 2008 | for (idx = 0; idx < pipe->nrbufs && rem < len; idx++) |
| 2009 | rem += pipe->bufs[(pipe->curbuf + idx) & (pipe->buffers - 1)].len; |
| 2010 | |
| 2011 | ret = -EINVAL; |
| 2012 | if (rem < len) |
| 2013 | goto out_free; |
| 2014 | |
| 2015 | rem = len; |
| 2016 | while (rem) { |
| 2017 | struct pipe_buffer *ibuf; |
| 2018 | struct pipe_buffer *obuf; |
| 2019 | |
| 2020 | BUG_ON(nbuf >= pipe->buffers); |
| 2021 | BUG_ON(!pipe->nrbufs); |
| 2022 | ibuf = &pipe->bufs[pipe->curbuf]; |
| 2023 | obuf = &bufs[nbuf]; |
| 2024 | |
| 2025 | if (rem >= ibuf->len) { |
| 2026 | *obuf = *ibuf; |
| 2027 | ibuf->ops = NULL; |
| 2028 | pipe->curbuf = (pipe->curbuf + 1) & (pipe->buffers - 1); |
| 2029 | pipe->nrbufs--; |
| 2030 | } else { |
| 2031 | if (!pipe_buf_get(pipe, ibuf)) |
| 2032 | goto out_free; |
| 2033 | |
| 2034 | *obuf = *ibuf; |
| 2035 | obuf->flags &= ~PIPE_BUF_FLAG_GIFT; |
| 2036 | obuf->len = rem; |
| 2037 | ibuf->offset += obuf->len; |
| 2038 | ibuf->len -= obuf->len; |
| 2039 | } |
| 2040 | nbuf++; |
| 2041 | rem -= obuf->len; |
| 2042 | } |
| 2043 | pipe_unlock(pipe); |
| 2044 | |
| 2045 | fuse_copy_init(&cs, 0, NULL); |
| 2046 | cs.pipebufs = bufs; |
| 2047 | cs.nr_segs = nbuf; |
| 2048 | cs.pipe = pipe; |
| 2049 | |
| 2050 | if (flags & SPLICE_F_MOVE) |
| 2051 | cs.move_pages = 1; |
| 2052 | |
| 2053 | ret = fuse_dev_do_write(fud, &cs, len); |
| 2054 | |
| 2055 | pipe_lock(pipe); |
| 2056 | out_free: |
| 2057 | for (idx = 0; idx < nbuf; idx++) { |
| 2058 | struct pipe_buffer *buf = &bufs[idx]; |
| 2059 | |
| 2060 | if (buf->ops) |
| 2061 | pipe_buf_release(pipe, buf); |
| 2062 | } |
| 2063 | pipe_unlock(pipe); |
| 2064 | |
| 2065 | kvfree(bufs); |
| 2066 | return ret; |
| 2067 | } |
| 2068 | |
| 2069 | static __poll_t fuse_dev_poll(struct file *file, poll_table *wait) |
| 2070 | { |
| 2071 | __poll_t mask = EPOLLOUT | EPOLLWRNORM; |
| 2072 | struct fuse_iqueue *fiq; |
| 2073 | struct fuse_dev *fud = fuse_get_dev(file); |
| 2074 | |
| 2075 | if (!fud) |
| 2076 | return EPOLLERR; |
| 2077 | |
| 2078 | fiq = &fud->fc->iq; |
| 2079 | poll_wait(file, &fiq->waitq, wait); |
| 2080 | |
| 2081 | spin_lock(&fiq->lock); |
| 2082 | if (!fiq->connected) |
| 2083 | mask = EPOLLERR; |
| 2084 | else if (request_pending(fiq)) |
| 2085 | mask |= EPOLLIN | EPOLLRDNORM; |
| 2086 | spin_unlock(&fiq->lock); |
| 2087 | |
| 2088 | return mask; |
| 2089 | } |
| 2090 | |
| 2091 | /* Abort all requests on the given list (pending or processing) */ |
| 2092 | static void end_requests(struct fuse_conn *fc, struct list_head *head) |
| 2093 | { |
| 2094 | while (!list_empty(head)) { |
| 2095 | struct fuse_req *req; |
| 2096 | req = list_entry(head->next, struct fuse_req, list); |
| 2097 | req->out.h.error = -ECONNABORTED; |
| 2098 | clear_bit(FR_SENT, &req->flags); |
| 2099 | list_del_init(&req->list); |
| 2100 | fuse_request_end(fc, req); |
| 2101 | } |
| 2102 | } |
| 2103 | |
| 2104 | static void end_polls(struct fuse_conn *fc) |
| 2105 | { |
| 2106 | struct rb_node *p; |
| 2107 | |
| 2108 | p = rb_first(&fc->polled_files); |
| 2109 | |
| 2110 | while (p) { |
| 2111 | struct fuse_file *ff; |
| 2112 | ff = rb_entry(p, struct fuse_file, polled_node); |
| 2113 | wake_up_interruptible_all(&ff->poll_wait); |
| 2114 | |
| 2115 | p = rb_next(p); |
| 2116 | } |
| 2117 | } |
| 2118 | |
| 2119 | /* |
| 2120 | * Abort all requests. |
| 2121 | * |
| 2122 | * Emergency exit in case of a malicious or accidental deadlock, or just a hung |
| 2123 | * filesystem. |
| 2124 | * |
| 2125 | * The same effect is usually achievable through killing the filesystem daemon |
| 2126 | * and all users of the filesystem. The exception is the combination of an |
| 2127 | * asynchronous request and the tricky deadlock (see |
| 2128 | * Documentation/filesystems/fuse.txt). |
| 2129 | * |
| 2130 | * Aborting requests under I/O goes as follows: 1: Separate out unlocked |
| 2131 | * requests, they should be finished off immediately. Locked requests will be |
| 2132 | * finished after unlock; see unlock_request(). 2: Finish off the unlocked |
| 2133 | * requests. It is possible that some request will finish before we can. This |
| 2134 | * is OK, the request will in that case be removed from the list before we touch |
| 2135 | * it. |
| 2136 | */ |
| 2137 | void fuse_abort_conn(struct fuse_conn *fc) |
| 2138 | { |
| 2139 | struct fuse_iqueue *fiq = &fc->iq; |
| 2140 | |
| 2141 | spin_lock(&fc->lock); |
| 2142 | if (fc->connected) { |
| 2143 | struct fuse_dev *fud; |
| 2144 | struct fuse_req *req, *next; |
| 2145 | LIST_HEAD(to_end); |
| 2146 | unsigned int i; |
| 2147 | |
| 2148 | /* Background queuing checks fc->connected under bg_lock */ |
| 2149 | spin_lock(&fc->bg_lock); |
| 2150 | fc->connected = 0; |
| 2151 | spin_unlock(&fc->bg_lock); |
| 2152 | |
| 2153 | fuse_set_initialized(fc); |
| 2154 | list_for_each_entry(fud, &fc->devices, entry) { |
| 2155 | struct fuse_pqueue *fpq = &fud->pq; |
| 2156 | |
| 2157 | spin_lock(&fpq->lock); |
| 2158 | fpq->connected = 0; |
| 2159 | list_for_each_entry_safe(req, next, &fpq->io, list) { |
| 2160 | req->out.h.error = -ECONNABORTED; |
| 2161 | spin_lock(&req->waitq.lock); |
| 2162 | set_bit(FR_ABORTED, &req->flags); |
| 2163 | if (!test_bit(FR_LOCKED, &req->flags)) { |
| 2164 | set_bit(FR_PRIVATE, &req->flags); |
| 2165 | __fuse_get_request(req); |
| 2166 | list_move(&req->list, &to_end); |
| 2167 | } |
| 2168 | spin_unlock(&req->waitq.lock); |
| 2169 | } |
| 2170 | for (i = 0; i < FUSE_PQ_HASH_SIZE; i++) |
| 2171 | list_splice_tail_init(&fpq->processing[i], |
| 2172 | &to_end); |
| 2173 | spin_unlock(&fpq->lock); |
| 2174 | } |
| 2175 | spin_lock(&fc->bg_lock); |
| 2176 | fc->blocked = 0; |
| 2177 | fc->max_background = UINT_MAX; |
| 2178 | flush_bg_queue(fc); |
| 2179 | spin_unlock(&fc->bg_lock); |
| 2180 | |
| 2181 | spin_lock(&fiq->lock); |
| 2182 | fiq->connected = 0; |
| 2183 | list_for_each_entry(req, &fiq->pending, list) |
| 2184 | clear_bit(FR_PENDING, &req->flags); |
| 2185 | list_splice_tail_init(&fiq->pending, &to_end); |
| 2186 | while (forget_pending(fiq)) |
| 2187 | kfree(fuse_dequeue_forget(fiq, 1, NULL)); |
| 2188 | wake_up_all(&fiq->waitq); |
| 2189 | spin_unlock(&fiq->lock); |
| 2190 | kill_fasync(&fiq->fasync, SIGIO, POLL_IN); |
| 2191 | end_polls(fc); |
| 2192 | wake_up_all(&fc->blocked_waitq); |
| 2193 | spin_unlock(&fc->lock); |
| 2194 | |
| 2195 | end_requests(fc, &to_end); |
| 2196 | } else { |
| 2197 | spin_unlock(&fc->lock); |
| 2198 | } |
| 2199 | } |
| 2200 | EXPORT_SYMBOL_GPL(fuse_abort_conn); |
| 2201 | |
| 2202 | void fuse_wait_aborted(struct fuse_conn *fc) |
| 2203 | { |
| 2204 | /* matches implicit memory barrier in fuse_drop_waiting() */ |
| 2205 | smp_mb(); |
| 2206 | wait_event(fc->blocked_waitq, atomic_read(&fc->num_waiting) == 0); |
| 2207 | } |
| 2208 | |
| 2209 | int fuse_dev_release(struct inode *inode, struct file *file) |
| 2210 | { |
| 2211 | struct fuse_dev *fud = fuse_get_dev(file); |
| 2212 | |
| 2213 | if (fud) { |
| 2214 | struct fuse_conn *fc = fud->fc; |
| 2215 | struct fuse_pqueue *fpq = &fud->pq; |
| 2216 | LIST_HEAD(to_end); |
| 2217 | unsigned int i; |
| 2218 | |
| 2219 | spin_lock(&fpq->lock); |
| 2220 | WARN_ON(!list_empty(&fpq->io)); |
| 2221 | for (i = 0; i < FUSE_PQ_HASH_SIZE; i++) |
| 2222 | list_splice_init(&fpq->processing[i], &to_end); |
| 2223 | spin_unlock(&fpq->lock); |
| 2224 | |
| 2225 | end_requests(fc, &to_end); |
| 2226 | |
| 2227 | /* Are we the last open device? */ |
| 2228 | if (atomic_dec_and_test(&fc->dev_count)) { |
| 2229 | WARN_ON(fc->iq.fasync != NULL); |
| 2230 | fuse_abort_conn(fc); |
| 2231 | } |
| 2232 | fuse_dev_free(fud); |
| 2233 | } |
| 2234 | return 0; |
| 2235 | } |
| 2236 | EXPORT_SYMBOL_GPL(fuse_dev_release); |
| 2237 | |
| 2238 | static int fuse_dev_fasync(int fd, struct file *file, int on) |
| 2239 | { |
| 2240 | struct fuse_dev *fud = fuse_get_dev(file); |
| 2241 | |
| 2242 | if (!fud) |
| 2243 | return -EPERM; |
| 2244 | |
| 2245 | /* No locking - fasync_helper does its own locking */ |
| 2246 | return fasync_helper(fd, file, on, &fud->fc->iq.fasync); |
| 2247 | } |
| 2248 | |
| 2249 | static int fuse_device_clone(struct fuse_conn *fc, struct file *new) |
| 2250 | { |
| 2251 | struct fuse_dev *fud; |
| 2252 | |
| 2253 | if (new->private_data) |
| 2254 | return -EINVAL; |
| 2255 | |
| 2256 | fud = fuse_dev_alloc_install(fc); |
| 2257 | if (!fud) |
| 2258 | return -ENOMEM; |
| 2259 | |
| 2260 | new->private_data = fud; |
| 2261 | atomic_inc(&fc->dev_count); |
| 2262 | |
| 2263 | return 0; |
| 2264 | } |
| 2265 | |
| 2266 | static long fuse_dev_ioctl(struct file *file, unsigned int cmd, |
| 2267 | unsigned long arg) |
| 2268 | { |
| 2269 | int res; |
| 2270 | int oldfd; |
| 2271 | struct fuse_dev *fud = NULL; |
| 2272 | |
| 2273 | switch (cmd) { |
| 2274 | case FUSE_DEV_IOC_CLONE: |
| 2275 | res = -EFAULT; |
| 2276 | if (!get_user(oldfd, (__u32 __user *)arg)) { |
| 2277 | struct file *old = fget(oldfd); |
| 2278 | |
| 2279 | res = -EINVAL; |
| 2280 | if (old) { |
| 2281 | /* |
| 2282 | * Check against file->f_op because CUSE |
| 2283 | * uses the same ioctl handler. |
| 2284 | */ |
| 2285 | if (old->f_op == file->f_op && |
| 2286 | old->f_cred->user_ns == |
| 2287 | file->f_cred->user_ns) |
| 2288 | fud = fuse_get_dev(old); |
| 2289 | |
| 2290 | if (fud) { |
| 2291 | mutex_lock(&fuse_mutex); |
| 2292 | res = fuse_device_clone(fud->fc, file); |
| 2293 | mutex_unlock(&fuse_mutex); |
| 2294 | } |
| 2295 | fput(old); |
| 2296 | } |
| 2297 | } |
| 2298 | break; |
| 2299 | case FUSE_DEV_IOC_PASSTHROUGH_OPEN: |
| 2300 | res = -EFAULT; |
| 2301 | if (!get_user(oldfd, (__u32 __user *)arg)) { |
| 2302 | res = -EINVAL; |
| 2303 | fud = fuse_get_dev(file); |
| 2304 | if (fud) |
| 2305 | res = fuse_passthrough_open(fud, oldfd); |
| 2306 | } |
| 2307 | break; |
| 2308 | default: |
| 2309 | res = -ENOTTY; |
| 2310 | break; |
| 2311 | } |
| 2312 | return res; |
| 2313 | } |
| 2314 | |
| 2315 | const struct file_operations fuse_dev_operations = { |
| 2316 | .owner = THIS_MODULE, |
| 2317 | .open = fuse_dev_open, |
| 2318 | .llseek = no_llseek, |
| 2319 | .read_iter = fuse_dev_read, |
| 2320 | .splice_read = fuse_dev_splice_read, |
| 2321 | .write_iter = fuse_dev_write, |
| 2322 | .splice_write = fuse_dev_splice_write, |
| 2323 | .poll = fuse_dev_poll, |
| 2324 | .release = fuse_dev_release, |
| 2325 | .fasync = fuse_dev_fasync, |
| 2326 | .unlocked_ioctl = fuse_dev_ioctl, |
| 2327 | .compat_ioctl = fuse_dev_ioctl, |
| 2328 | }; |
| 2329 | EXPORT_SYMBOL_GPL(fuse_dev_operations); |
| 2330 | |
| 2331 | static struct miscdevice fuse_miscdevice = { |
| 2332 | .minor = FUSE_MINOR, |
| 2333 | .name = "fuse", |
| 2334 | .fops = &fuse_dev_operations, |
| 2335 | }; |
| 2336 | |
| 2337 | int __init fuse_dev_init(void) |
| 2338 | { |
| 2339 | int err = -ENOMEM; |
| 2340 | fuse_req_cachep = kmem_cache_create("fuse_request", |
| 2341 | sizeof(struct fuse_req), |
| 2342 | 0, 0, NULL); |
| 2343 | if (!fuse_req_cachep) |
| 2344 | goto out; |
| 2345 | |
| 2346 | err = misc_register(&fuse_miscdevice); |
| 2347 | if (err) |
| 2348 | goto out_cache_clean; |
| 2349 | |
| 2350 | return 0; |
| 2351 | |
| 2352 | out_cache_clean: |
| 2353 | kmem_cache_destroy(fuse_req_cachep); |
| 2354 | out: |
| 2355 | return err; |
| 2356 | } |
| 2357 | |
| 2358 | void fuse_dev_cleanup(void) |
| 2359 | { |
| 2360 | misc_deregister(&fuse_miscdevice); |
| 2361 | kmem_cache_destroy(fuse_req_cachep); |
| 2362 | } |