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/pagemap.h> 12 #include <linux/slab.h> 13 #include <linux/kernel.h> 14 #include <linux/sched.h> 15 #include <linux/sched/signal.h> 16 #include <linux/module.h> 17 #include <linux/compat.h> 18 #include <linux/swap.h> 19 #include <linux/falloc.h> 20 #include <linux/uio.h> 21 22 static int fuse_send_open(struct fuse_conn *fc, u64 nodeid, struct file *file, 23 int opcode, struct fuse_open_out *outargp) 24 { 25 struct fuse_open_in inarg; 26 FUSE_ARGS(args); 27 28 memset(&inarg, 0, sizeof(inarg)); 29 inarg.flags = file->f_flags & ~(O_CREAT | O_EXCL | O_NOCTTY); 30 if (!fc->atomic_o_trunc) 31 inarg.flags &= ~O_TRUNC; 32 args.in.h.opcode = opcode; 33 args.in.h.nodeid = nodeid; 34 args.in.numargs = 1; 35 args.in.args[0].size = sizeof(inarg); 36 args.in.args[0].value = &inarg; 37 args.out.numargs = 1; 38 args.out.args[0].size = sizeof(*outargp); 39 args.out.args[0].value = outargp; 40 41 return fuse_simple_request(fc, &args); 42 } 43 44 struct fuse_file *fuse_file_alloc(struct fuse_conn *fc) 45 { 46 struct fuse_file *ff; 47 48 ff = kzalloc(sizeof(struct fuse_file), GFP_KERNEL); 49 if (unlikely(!ff)) 50 return NULL; 51 52 ff->fc = fc; 53 ff->reserved_req = fuse_request_alloc(0); 54 if (unlikely(!ff->reserved_req)) { 55 kfree(ff); 56 return NULL; 57 } 58 59 INIT_LIST_HEAD(&ff->write_entry); 60 mutex_init(&ff->readdir.lock); 61 refcount_set(&ff->count, 1); 62 RB_CLEAR_NODE(&ff->polled_node); 63 init_waitqueue_head(&ff->poll_wait); 64 65 ff->kh = atomic64_inc_return(&fc->khctr); 66 67 return ff; 68 } 69 70 void fuse_file_free(struct fuse_file *ff) 71 { 72 fuse_request_free(ff->reserved_req); 73 mutex_destroy(&ff->readdir.lock); 74 kfree(ff); 75 } 76 77 static struct fuse_file *fuse_file_get(struct fuse_file *ff) 78 { 79 refcount_inc(&ff->count); 80 return ff; 81 } 82 83 static void fuse_release_end(struct fuse_conn *fc, struct fuse_req *req) 84 { 85 iput(req->misc.release.inode); 86 } 87 88 static void fuse_file_put(struct fuse_file *ff, bool sync, bool isdir) 89 { 90 if (refcount_dec_and_test(&ff->count)) { 91 struct fuse_req *req = ff->reserved_req; 92 93 if (isdir ? ff->fc->no_opendir : ff->fc->no_open) { 94 /* 95 * Drop the release request when client does not 96 * implement 'open' 97 */ 98 __clear_bit(FR_BACKGROUND, &req->flags); 99 iput(req->misc.release.inode); 100 fuse_put_request(ff->fc, req); 101 } else if (sync) { 102 __set_bit(FR_FORCE, &req->flags); 103 __clear_bit(FR_BACKGROUND, &req->flags); 104 fuse_request_send(ff->fc, req); 105 iput(req->misc.release.inode); 106 fuse_put_request(ff->fc, req); 107 } else { 108 req->end = fuse_release_end; 109 __set_bit(FR_BACKGROUND, &req->flags); 110 fuse_request_send_background(ff->fc, req); 111 } 112 kfree(ff); 113 } 114 } 115 116 int fuse_do_open(struct fuse_conn *fc, u64 nodeid, struct file *file, 117 bool isdir) 118 { 119 struct fuse_file *ff; 120 int opcode = isdir ? FUSE_OPENDIR : FUSE_OPEN; 121 122 ff = fuse_file_alloc(fc); 123 if (!ff) 124 return -ENOMEM; 125 126 ff->fh = 0; 127 /* Default for no-open */ 128 ff->open_flags = FOPEN_KEEP_CACHE | (isdir ? FOPEN_CACHE_DIR : 0); 129 if (isdir ? !fc->no_opendir : !fc->no_open) { 130 struct fuse_open_out outarg; 131 int err; 132 133 err = fuse_send_open(fc, nodeid, file, opcode, &outarg); 134 if (!err) { 135 ff->fh = outarg.fh; 136 ff->open_flags = outarg.open_flags; 137 138 } else if (err != -ENOSYS) { 139 fuse_file_free(ff); 140 return err; 141 } else { 142 if (isdir) 143 fc->no_opendir = 1; 144 else 145 fc->no_open = 1; 146 } 147 } 148 149 if (isdir) 150 ff->open_flags &= ~FOPEN_DIRECT_IO; 151 152 ff->nodeid = nodeid; 153 file->private_data = ff; 154 155 return 0; 156 } 157 EXPORT_SYMBOL_GPL(fuse_do_open); 158 159 static void fuse_link_write_file(struct file *file) 160 { 161 struct inode *inode = file_inode(file); 162 struct fuse_inode *fi = get_fuse_inode(inode); 163 struct fuse_file *ff = file->private_data; 164 /* 165 * file may be written through mmap, so chain it onto the 166 * inodes's write_file list 167 */ 168 spin_lock(&fi->lock); 169 if (list_empty(&ff->write_entry)) 170 list_add(&ff->write_entry, &fi->write_files); 171 spin_unlock(&fi->lock); 172 } 173 174 void fuse_finish_open(struct inode *inode, struct file *file) 175 { 176 struct fuse_file *ff = file->private_data; 177 struct fuse_conn *fc = get_fuse_conn(inode); 178 179 if (!(ff->open_flags & FOPEN_KEEP_CACHE)) 180 invalidate_inode_pages2(inode->i_mapping); 181 if (ff->open_flags & FOPEN_STREAM) 182 stream_open(inode, file); 183 else if (ff->open_flags & FOPEN_NONSEEKABLE) 184 nonseekable_open(inode, file); 185 if (fc->atomic_o_trunc && (file->f_flags & O_TRUNC)) { 186 struct fuse_inode *fi = get_fuse_inode(inode); 187 188 spin_lock(&fi->lock); 189 fi->attr_version = atomic64_inc_return(&fc->attr_version); 190 i_size_write(inode, 0); 191 spin_unlock(&fi->lock); 192 fuse_invalidate_attr(inode); 193 if (fc->writeback_cache) 194 file_update_time(file); 195 } 196 if ((file->f_mode & FMODE_WRITE) && fc->writeback_cache) 197 fuse_link_write_file(file); 198 } 199 200 int fuse_open_common(struct inode *inode, struct file *file, bool isdir) 201 { 202 struct fuse_conn *fc = get_fuse_conn(inode); 203 int err; 204 bool lock_inode = (file->f_flags & O_TRUNC) && 205 fc->atomic_o_trunc && 206 fc->writeback_cache; 207 208 err = generic_file_open(inode, file); 209 if (err) 210 return err; 211 212 if (lock_inode) 213 inode_lock(inode); 214 215 err = fuse_do_open(fc, get_node_id(inode), file, isdir); 216 217 if (!err) 218 fuse_finish_open(inode, file); 219 220 if (lock_inode) 221 inode_unlock(inode); 222 223 return err; 224 } 225 226 static void fuse_prepare_release(struct fuse_inode *fi, struct fuse_file *ff, 227 int flags, int opcode) 228 { 229 struct fuse_conn *fc = ff->fc; 230 struct fuse_req *req = ff->reserved_req; 231 struct fuse_release_in *inarg = &req->misc.release.in; 232 233 /* Inode is NULL on error path of fuse_create_open() */ 234 if (likely(fi)) { 235 spin_lock(&fi->lock); 236 list_del(&ff->write_entry); 237 spin_unlock(&fi->lock); 238 } 239 spin_lock(&fc->lock); 240 if (!RB_EMPTY_NODE(&ff->polled_node)) 241 rb_erase(&ff->polled_node, &fc->polled_files); 242 spin_unlock(&fc->lock); 243 244 wake_up_interruptible_all(&ff->poll_wait); 245 246 inarg->fh = ff->fh; 247 inarg->flags = flags; 248 req->in.h.opcode = opcode; 249 req->in.h.nodeid = ff->nodeid; 250 req->in.numargs = 1; 251 req->in.args[0].size = sizeof(struct fuse_release_in); 252 req->in.args[0].value = inarg; 253 } 254 255 void fuse_release_common(struct file *file, bool isdir) 256 { 257 struct fuse_inode *fi = get_fuse_inode(file_inode(file)); 258 struct fuse_file *ff = file->private_data; 259 struct fuse_req *req = ff->reserved_req; 260 int opcode = isdir ? FUSE_RELEASEDIR : FUSE_RELEASE; 261 262 fuse_prepare_release(fi, ff, file->f_flags, opcode); 263 264 if (ff->flock) { 265 struct fuse_release_in *inarg = &req->misc.release.in; 266 inarg->release_flags |= FUSE_RELEASE_FLOCK_UNLOCK; 267 inarg->lock_owner = fuse_lock_owner_id(ff->fc, 268 (fl_owner_t) file); 269 } 270 /* Hold inode until release is finished */ 271 req->misc.release.inode = igrab(file_inode(file)); 272 273 /* 274 * Normally this will send the RELEASE request, however if 275 * some asynchronous READ or WRITE requests are outstanding, 276 * the sending will be delayed. 277 * 278 * Make the release synchronous if this is a fuseblk mount, 279 * synchronous RELEASE is allowed (and desirable) in this case 280 * because the server can be trusted not to screw up. 281 */ 282 fuse_file_put(ff, ff->fc->destroy_req != NULL, isdir); 283 } 284 285 static int fuse_open(struct inode *inode, struct file *file) 286 { 287 return fuse_open_common(inode, file, false); 288 } 289 290 static int fuse_release(struct inode *inode, struct file *file) 291 { 292 struct fuse_conn *fc = get_fuse_conn(inode); 293 294 /* see fuse_vma_close() for !writeback_cache case */ 295 if (fc->writeback_cache) 296 write_inode_now(inode, 1); 297 298 fuse_release_common(file, false); 299 300 /* return value is ignored by VFS */ 301 return 0; 302 } 303 304 void fuse_sync_release(struct fuse_inode *fi, struct fuse_file *ff, int flags) 305 { 306 WARN_ON(refcount_read(&ff->count) > 1); 307 fuse_prepare_release(fi, ff, flags, FUSE_RELEASE); 308 /* 309 * iput(NULL) is a no-op and since the refcount is 1 and everything's 310 * synchronous, we are fine with not doing igrab() here" 311 */ 312 fuse_file_put(ff, true, false); 313 } 314 EXPORT_SYMBOL_GPL(fuse_sync_release); 315 316 /* 317 * Scramble the ID space with XTEA, so that the value of the files_struct 318 * pointer is not exposed to userspace. 319 */ 320 u64 fuse_lock_owner_id(struct fuse_conn *fc, fl_owner_t id) 321 { 322 u32 *k = fc->scramble_key; 323 u64 v = (unsigned long) id; 324 u32 v0 = v; 325 u32 v1 = v >> 32; 326 u32 sum = 0; 327 int i; 328 329 for (i = 0; i < 32; i++) { 330 v0 += ((v1 << 4 ^ v1 >> 5) + v1) ^ (sum + k[sum & 3]); 331 sum += 0x9E3779B9; 332 v1 += ((v0 << 4 ^ v0 >> 5) + v0) ^ (sum + k[sum>>11 & 3]); 333 } 334 335 return (u64) v0 + ((u64) v1 << 32); 336 } 337 338 static struct fuse_req *fuse_find_writeback(struct fuse_inode *fi, 339 pgoff_t idx_from, pgoff_t idx_to) 340 { 341 struct fuse_req *req; 342 343 list_for_each_entry(req, &fi->writepages, writepages_entry) { 344 pgoff_t curr_index; 345 346 WARN_ON(get_fuse_inode(req->inode) != fi); 347 curr_index = req->misc.write.in.offset >> PAGE_SHIFT; 348 if (idx_from < curr_index + req->num_pages && 349 curr_index <= idx_to) { 350 return req; 351 } 352 } 353 return NULL; 354 } 355 356 /* 357 * Check if any page in a range is under writeback 358 * 359 * This is currently done by walking the list of writepage requests 360 * for the inode, which can be pretty inefficient. 361 */ 362 static bool fuse_range_is_writeback(struct inode *inode, pgoff_t idx_from, 363 pgoff_t idx_to) 364 { 365 struct fuse_inode *fi = get_fuse_inode(inode); 366 bool found; 367 368 spin_lock(&fi->lock); 369 found = fuse_find_writeback(fi, idx_from, idx_to); 370 spin_unlock(&fi->lock); 371 372 return found; 373 } 374 375 static inline bool fuse_page_is_writeback(struct inode *inode, pgoff_t index) 376 { 377 return fuse_range_is_writeback(inode, index, index); 378 } 379 380 /* 381 * Wait for page writeback to be completed. 382 * 383 * Since fuse doesn't rely on the VM writeback tracking, this has to 384 * use some other means. 385 */ 386 static int fuse_wait_on_page_writeback(struct inode *inode, pgoff_t index) 387 { 388 struct fuse_inode *fi = get_fuse_inode(inode); 389 390 wait_event(fi->page_waitq, !fuse_page_is_writeback(inode, index)); 391 return 0; 392 } 393 394 /* 395 * Wait for all pending writepages on the inode to finish. 396 * 397 * This is currently done by blocking further writes with FUSE_NOWRITE 398 * and waiting for all sent writes to complete. 399 * 400 * This must be called under i_mutex, otherwise the FUSE_NOWRITE usage 401 * could conflict with truncation. 402 */ 403 static void fuse_sync_writes(struct inode *inode) 404 { 405 fuse_set_nowrite(inode); 406 fuse_release_nowrite(inode); 407 } 408 409 static int fuse_flush(struct file *file, fl_owner_t id) 410 { 411 struct inode *inode = file_inode(file); 412 struct fuse_conn *fc = get_fuse_conn(inode); 413 struct fuse_file *ff = file->private_data; 414 struct fuse_req *req; 415 struct fuse_flush_in inarg; 416 int err; 417 418 if (is_bad_inode(inode)) 419 return -EIO; 420 421 if (fc->no_flush) 422 return 0; 423 424 err = write_inode_now(inode, 1); 425 if (err) 426 return err; 427 428 inode_lock(inode); 429 fuse_sync_writes(inode); 430 inode_unlock(inode); 431 432 err = filemap_check_errors(file->f_mapping); 433 if (err) 434 return err; 435 436 req = fuse_get_req_nofail_nopages(fc, file); 437 memset(&inarg, 0, sizeof(inarg)); 438 inarg.fh = ff->fh; 439 inarg.lock_owner = fuse_lock_owner_id(fc, id); 440 req->in.h.opcode = FUSE_FLUSH; 441 req->in.h.nodeid = get_node_id(inode); 442 req->in.numargs = 1; 443 req->in.args[0].size = sizeof(inarg); 444 req->in.args[0].value = &inarg; 445 __set_bit(FR_FORCE, &req->flags); 446 fuse_request_send(fc, req); 447 err = req->out.h.error; 448 fuse_put_request(fc, req); 449 if (err == -ENOSYS) { 450 fc->no_flush = 1; 451 err = 0; 452 } 453 return err; 454 } 455 456 int fuse_fsync_common(struct file *file, loff_t start, loff_t end, 457 int datasync, int opcode) 458 { 459 struct inode *inode = file->f_mapping->host; 460 struct fuse_conn *fc = get_fuse_conn(inode); 461 struct fuse_file *ff = file->private_data; 462 FUSE_ARGS(args); 463 struct fuse_fsync_in inarg; 464 465 memset(&inarg, 0, sizeof(inarg)); 466 inarg.fh = ff->fh; 467 inarg.fsync_flags = datasync ? FUSE_FSYNC_FDATASYNC : 0; 468 args.in.h.opcode = opcode; 469 args.in.h.nodeid = get_node_id(inode); 470 args.in.numargs = 1; 471 args.in.args[0].size = sizeof(inarg); 472 args.in.args[0].value = &inarg; 473 return fuse_simple_request(fc, &args); 474 } 475 476 static int fuse_fsync(struct file *file, loff_t start, loff_t end, 477 int datasync) 478 { 479 struct inode *inode = file->f_mapping->host; 480 struct fuse_conn *fc = get_fuse_conn(inode); 481 int err; 482 483 if (is_bad_inode(inode)) 484 return -EIO; 485 486 inode_lock(inode); 487 488 /* 489 * Start writeback against all dirty pages of the inode, then 490 * wait for all outstanding writes, before sending the FSYNC 491 * request. 492 */ 493 err = file_write_and_wait_range(file, start, end); 494 if (err) 495 goto out; 496 497 fuse_sync_writes(inode); 498 499 /* 500 * Due to implementation of fuse writeback 501 * file_write_and_wait_range() does not catch errors. 502 * We have to do this directly after fuse_sync_writes() 503 */ 504 err = file_check_and_advance_wb_err(file); 505 if (err) 506 goto out; 507 508 err = sync_inode_metadata(inode, 1); 509 if (err) 510 goto out; 511 512 if (fc->no_fsync) 513 goto out; 514 515 err = fuse_fsync_common(file, start, end, datasync, FUSE_FSYNC); 516 if (err == -ENOSYS) { 517 fc->no_fsync = 1; 518 err = 0; 519 } 520 out: 521 inode_unlock(inode); 522 523 return err; 524 } 525 526 void fuse_read_fill(struct fuse_req *req, struct file *file, loff_t pos, 527 size_t count, int opcode) 528 { 529 struct fuse_read_in *inarg = &req->misc.read.in; 530 struct fuse_file *ff = file->private_data; 531 532 inarg->fh = ff->fh; 533 inarg->offset = pos; 534 inarg->size = count; 535 inarg->flags = file->f_flags; 536 req->in.h.opcode = opcode; 537 req->in.h.nodeid = ff->nodeid; 538 req->in.numargs = 1; 539 req->in.args[0].size = sizeof(struct fuse_read_in); 540 req->in.args[0].value = inarg; 541 req->out.argvar = 1; 542 req->out.numargs = 1; 543 req->out.args[0].size = count; 544 } 545 546 static void fuse_release_user_pages(struct fuse_req *req, bool should_dirty) 547 { 548 unsigned i; 549 550 for (i = 0; i < req->num_pages; i++) { 551 struct page *page = req->pages[i]; 552 if (should_dirty) 553 set_page_dirty_lock(page); 554 put_page(page); 555 } 556 } 557 558 static void fuse_io_release(struct kref *kref) 559 { 560 kfree(container_of(kref, struct fuse_io_priv, refcnt)); 561 } 562 563 static ssize_t fuse_get_res_by_io(struct fuse_io_priv *io) 564 { 565 if (io->err) 566 return io->err; 567 568 if (io->bytes >= 0 && io->write) 569 return -EIO; 570 571 return io->bytes < 0 ? io->size : io->bytes; 572 } 573 574 /** 575 * In case of short read, the caller sets 'pos' to the position of 576 * actual end of fuse request in IO request. Otherwise, if bytes_requested 577 * == bytes_transferred or rw == WRITE, the caller sets 'pos' to -1. 578 * 579 * An example: 580 * User requested DIO read of 64K. It was splitted into two 32K fuse requests, 581 * both submitted asynchronously. The first of them was ACKed by userspace as 582 * fully completed (req->out.args[0].size == 32K) resulting in pos == -1. The 583 * second request was ACKed as short, e.g. only 1K was read, resulting in 584 * pos == 33K. 585 * 586 * Thus, when all fuse requests are completed, the minimal non-negative 'pos' 587 * will be equal to the length of the longest contiguous fragment of 588 * transferred data starting from the beginning of IO request. 589 */ 590 static void fuse_aio_complete(struct fuse_io_priv *io, int err, ssize_t pos) 591 { 592 int left; 593 594 spin_lock(&io->lock); 595 if (err) 596 io->err = io->err ? : err; 597 else if (pos >= 0 && (io->bytes < 0 || pos < io->bytes)) 598 io->bytes = pos; 599 600 left = --io->reqs; 601 if (!left && io->blocking) 602 complete(io->done); 603 spin_unlock(&io->lock); 604 605 if (!left && !io->blocking) { 606 ssize_t res = fuse_get_res_by_io(io); 607 608 if (res >= 0) { 609 struct inode *inode = file_inode(io->iocb->ki_filp); 610 struct fuse_conn *fc = get_fuse_conn(inode); 611 struct fuse_inode *fi = get_fuse_inode(inode); 612 613 spin_lock(&fi->lock); 614 fi->attr_version = atomic64_inc_return(&fc->attr_version); 615 spin_unlock(&fi->lock); 616 } 617 618 io->iocb->ki_complete(io->iocb, res, 0); 619 } 620 621 kref_put(&io->refcnt, fuse_io_release); 622 } 623 624 static void fuse_aio_complete_req(struct fuse_conn *fc, struct fuse_req *req) 625 { 626 struct fuse_io_priv *io = req->io; 627 ssize_t pos = -1; 628 629 fuse_release_user_pages(req, io->should_dirty); 630 631 if (io->write) { 632 if (req->misc.write.in.size != req->misc.write.out.size) 633 pos = req->misc.write.in.offset - io->offset + 634 req->misc.write.out.size; 635 } else { 636 if (req->misc.read.in.size != req->out.args[0].size) 637 pos = req->misc.read.in.offset - io->offset + 638 req->out.args[0].size; 639 } 640 641 fuse_aio_complete(io, req->out.h.error, pos); 642 } 643 644 static size_t fuse_async_req_send(struct fuse_conn *fc, struct fuse_req *req, 645 size_t num_bytes, struct fuse_io_priv *io) 646 { 647 spin_lock(&io->lock); 648 kref_get(&io->refcnt); 649 io->size += num_bytes; 650 io->reqs++; 651 spin_unlock(&io->lock); 652 653 req->io = io; 654 req->end = fuse_aio_complete_req; 655 656 __fuse_get_request(req); 657 fuse_request_send_background(fc, req); 658 659 return num_bytes; 660 } 661 662 static size_t fuse_send_read(struct fuse_req *req, struct fuse_io_priv *io, 663 loff_t pos, size_t count, fl_owner_t owner) 664 { 665 struct file *file = io->iocb->ki_filp; 666 struct fuse_file *ff = file->private_data; 667 struct fuse_conn *fc = ff->fc; 668 669 fuse_read_fill(req, file, pos, count, FUSE_READ); 670 if (owner != NULL) { 671 struct fuse_read_in *inarg = &req->misc.read.in; 672 673 inarg->read_flags |= FUSE_READ_LOCKOWNER; 674 inarg->lock_owner = fuse_lock_owner_id(fc, owner); 675 } 676 677 if (io->async) 678 return fuse_async_req_send(fc, req, count, io); 679 680 fuse_request_send(fc, req); 681 return req->out.args[0].size; 682 } 683 684 static void fuse_read_update_size(struct inode *inode, loff_t size, 685 u64 attr_ver) 686 { 687 struct fuse_conn *fc = get_fuse_conn(inode); 688 struct fuse_inode *fi = get_fuse_inode(inode); 689 690 spin_lock(&fi->lock); 691 if (attr_ver == fi->attr_version && size < inode->i_size && 692 !test_bit(FUSE_I_SIZE_UNSTABLE, &fi->state)) { 693 fi->attr_version = atomic64_inc_return(&fc->attr_version); 694 i_size_write(inode, size); 695 } 696 spin_unlock(&fi->lock); 697 } 698 699 static void fuse_short_read(struct fuse_req *req, struct inode *inode, 700 u64 attr_ver) 701 { 702 size_t num_read = req->out.args[0].size; 703 struct fuse_conn *fc = get_fuse_conn(inode); 704 705 if (fc->writeback_cache) { 706 /* 707 * A hole in a file. Some data after the hole are in page cache, 708 * but have not reached the client fs yet. So, the hole is not 709 * present there. 710 */ 711 int i; 712 int start_idx = num_read >> PAGE_SHIFT; 713 size_t off = num_read & (PAGE_SIZE - 1); 714 715 for (i = start_idx; i < req->num_pages; i++) { 716 zero_user_segment(req->pages[i], off, PAGE_SIZE); 717 off = 0; 718 } 719 } else { 720 loff_t pos = page_offset(req->pages[0]) + num_read; 721 fuse_read_update_size(inode, pos, attr_ver); 722 } 723 } 724 725 static int fuse_do_readpage(struct file *file, struct page *page) 726 { 727 struct kiocb iocb; 728 struct fuse_io_priv io; 729 struct inode *inode = page->mapping->host; 730 struct fuse_conn *fc = get_fuse_conn(inode); 731 struct fuse_req *req; 732 size_t num_read; 733 loff_t pos = page_offset(page); 734 size_t count = PAGE_SIZE; 735 u64 attr_ver; 736 int err; 737 738 /* 739 * Page writeback can extend beyond the lifetime of the 740 * page-cache page, so make sure we read a properly synced 741 * page. 742 */ 743 fuse_wait_on_page_writeback(inode, page->index); 744 745 req = fuse_get_req(fc, 1); 746 if (IS_ERR(req)) 747 return PTR_ERR(req); 748 749 attr_ver = fuse_get_attr_version(fc); 750 751 req->out.page_zeroing = 1; 752 req->out.argpages = 1; 753 req->num_pages = 1; 754 req->pages[0] = page; 755 req->page_descs[0].length = count; 756 init_sync_kiocb(&iocb, file); 757 io = (struct fuse_io_priv) FUSE_IO_PRIV_SYNC(&iocb); 758 num_read = fuse_send_read(req, &io, pos, count, NULL); 759 err = req->out.h.error; 760 761 if (!err) { 762 /* 763 * Short read means EOF. If file size is larger, truncate it 764 */ 765 if (num_read < count) 766 fuse_short_read(req, inode, attr_ver); 767 768 SetPageUptodate(page); 769 } 770 771 fuse_put_request(fc, req); 772 773 return err; 774 } 775 776 static int fuse_readpage(struct file *file, struct page *page) 777 { 778 struct inode *inode = page->mapping->host; 779 int err; 780 781 err = -EIO; 782 if (is_bad_inode(inode)) 783 goto out; 784 785 err = fuse_do_readpage(file, page); 786 fuse_invalidate_atime(inode); 787 out: 788 unlock_page(page); 789 return err; 790 } 791 792 static void fuse_readpages_end(struct fuse_conn *fc, struct fuse_req *req) 793 { 794 int i; 795 size_t count = req->misc.read.in.size; 796 size_t num_read = req->out.args[0].size; 797 struct address_space *mapping = NULL; 798 799 for (i = 0; mapping == NULL && i < req->num_pages; i++) 800 mapping = req->pages[i]->mapping; 801 802 if (mapping) { 803 struct inode *inode = mapping->host; 804 805 /* 806 * Short read means EOF. If file size is larger, truncate it 807 */ 808 if (!req->out.h.error && num_read < count) 809 fuse_short_read(req, inode, req->misc.read.attr_ver); 810 811 fuse_invalidate_atime(inode); 812 } 813 814 for (i = 0; i < req->num_pages; i++) { 815 struct page *page = req->pages[i]; 816 if (!req->out.h.error) 817 SetPageUptodate(page); 818 else 819 SetPageError(page); 820 unlock_page(page); 821 put_page(page); 822 } 823 if (req->ff) 824 fuse_file_put(req->ff, false, false); 825 } 826 827 static void fuse_send_readpages(struct fuse_req *req, struct file *file) 828 { 829 struct fuse_file *ff = file->private_data; 830 struct fuse_conn *fc = ff->fc; 831 loff_t pos = page_offset(req->pages[0]); 832 size_t count = req->num_pages << PAGE_SHIFT; 833 834 req->out.argpages = 1; 835 req->out.page_zeroing = 1; 836 req->out.page_replace = 1; 837 fuse_read_fill(req, file, pos, count, FUSE_READ); 838 req->misc.read.attr_ver = fuse_get_attr_version(fc); 839 if (fc->async_read) { 840 req->ff = fuse_file_get(ff); 841 req->end = fuse_readpages_end; 842 fuse_request_send_background(fc, req); 843 } else { 844 fuse_request_send(fc, req); 845 fuse_readpages_end(fc, req); 846 fuse_put_request(fc, req); 847 } 848 } 849 850 struct fuse_fill_data { 851 struct fuse_req *req; 852 struct file *file; 853 struct inode *inode; 854 unsigned nr_pages; 855 }; 856 857 static int fuse_readpages_fill(void *_data, struct page *page) 858 { 859 struct fuse_fill_data *data = _data; 860 struct fuse_req *req = data->req; 861 struct inode *inode = data->inode; 862 struct fuse_conn *fc = get_fuse_conn(inode); 863 864 fuse_wait_on_page_writeback(inode, page->index); 865 866 if (req->num_pages && 867 (req->num_pages == fc->max_pages || 868 (req->num_pages + 1) * PAGE_SIZE > fc->max_read || 869 req->pages[req->num_pages - 1]->index + 1 != page->index)) { 870 unsigned int nr_alloc = min_t(unsigned int, data->nr_pages, 871 fc->max_pages); 872 fuse_send_readpages(req, data->file); 873 if (fc->async_read) 874 req = fuse_get_req_for_background(fc, nr_alloc); 875 else 876 req = fuse_get_req(fc, nr_alloc); 877 878 data->req = req; 879 if (IS_ERR(req)) { 880 unlock_page(page); 881 return PTR_ERR(req); 882 } 883 } 884 885 if (WARN_ON(req->num_pages >= req->max_pages)) { 886 unlock_page(page); 887 fuse_put_request(fc, req); 888 return -EIO; 889 } 890 891 get_page(page); 892 req->pages[req->num_pages] = page; 893 req->page_descs[req->num_pages].length = PAGE_SIZE; 894 req->num_pages++; 895 data->nr_pages--; 896 return 0; 897 } 898 899 static int fuse_readpages(struct file *file, struct address_space *mapping, 900 struct list_head *pages, unsigned nr_pages) 901 { 902 struct inode *inode = mapping->host; 903 struct fuse_conn *fc = get_fuse_conn(inode); 904 struct fuse_fill_data data; 905 int err; 906 unsigned int nr_alloc = min_t(unsigned int, nr_pages, fc->max_pages); 907 908 err = -EIO; 909 if (is_bad_inode(inode)) 910 goto out; 911 912 data.file = file; 913 data.inode = inode; 914 if (fc->async_read) 915 data.req = fuse_get_req_for_background(fc, nr_alloc); 916 else 917 data.req = fuse_get_req(fc, nr_alloc); 918 data.nr_pages = nr_pages; 919 err = PTR_ERR(data.req); 920 if (IS_ERR(data.req)) 921 goto out; 922 923 err = read_cache_pages(mapping, pages, fuse_readpages_fill, &data); 924 if (!err) { 925 if (data.req->num_pages) 926 fuse_send_readpages(data.req, file); 927 else 928 fuse_put_request(fc, data.req); 929 } 930 out: 931 return err; 932 } 933 934 static ssize_t fuse_cache_read_iter(struct kiocb *iocb, struct iov_iter *to) 935 { 936 struct inode *inode = iocb->ki_filp->f_mapping->host; 937 struct fuse_conn *fc = get_fuse_conn(inode); 938 939 /* 940 * In auto invalidate mode, always update attributes on read. 941 * Otherwise, only update if we attempt to read past EOF (to ensure 942 * i_size is up to date). 943 */ 944 if (fc->auto_inval_data || 945 (iocb->ki_pos + iov_iter_count(to) > i_size_read(inode))) { 946 int err; 947 err = fuse_update_attributes(inode, iocb->ki_filp); 948 if (err) 949 return err; 950 } 951 952 return generic_file_read_iter(iocb, to); 953 } 954 955 static void fuse_write_fill(struct fuse_req *req, struct fuse_file *ff, 956 loff_t pos, size_t count) 957 { 958 struct fuse_write_in *inarg = &req->misc.write.in; 959 struct fuse_write_out *outarg = &req->misc.write.out; 960 961 inarg->fh = ff->fh; 962 inarg->offset = pos; 963 inarg->size = count; 964 req->in.h.opcode = FUSE_WRITE; 965 req->in.h.nodeid = ff->nodeid; 966 req->in.numargs = 2; 967 if (ff->fc->minor < 9) 968 req->in.args[0].size = FUSE_COMPAT_WRITE_IN_SIZE; 969 else 970 req->in.args[0].size = sizeof(struct fuse_write_in); 971 req->in.args[0].value = inarg; 972 req->in.args[1].size = count; 973 req->out.numargs = 1; 974 req->out.args[0].size = sizeof(struct fuse_write_out); 975 req->out.args[0].value = outarg; 976 } 977 978 static size_t fuse_send_write(struct fuse_req *req, struct fuse_io_priv *io, 979 loff_t pos, size_t count, fl_owner_t owner) 980 { 981 struct kiocb *iocb = io->iocb; 982 struct file *file = iocb->ki_filp; 983 struct fuse_file *ff = file->private_data; 984 struct fuse_conn *fc = ff->fc; 985 struct fuse_write_in *inarg = &req->misc.write.in; 986 987 fuse_write_fill(req, ff, pos, count); 988 inarg->flags = file->f_flags; 989 if (iocb->ki_flags & IOCB_DSYNC) 990 inarg->flags |= O_DSYNC; 991 if (iocb->ki_flags & IOCB_SYNC) 992 inarg->flags |= O_SYNC; 993 if (owner != NULL) { 994 inarg->write_flags |= FUSE_WRITE_LOCKOWNER; 995 inarg->lock_owner = fuse_lock_owner_id(fc, owner); 996 } 997 998 if (io->async) 999 return fuse_async_req_send(fc, req, count, io); 1000 1001 fuse_request_send(fc, req); 1002 return req->misc.write.out.size; 1003 } 1004 1005 bool fuse_write_update_size(struct inode *inode, loff_t pos) 1006 { 1007 struct fuse_conn *fc = get_fuse_conn(inode); 1008 struct fuse_inode *fi = get_fuse_inode(inode); 1009 bool ret = false; 1010 1011 spin_lock(&fi->lock); 1012 fi->attr_version = atomic64_inc_return(&fc->attr_version); 1013 if (pos > inode->i_size) { 1014 i_size_write(inode, pos); 1015 ret = true; 1016 } 1017 spin_unlock(&fi->lock); 1018 1019 return ret; 1020 } 1021 1022 static size_t fuse_send_write_pages(struct fuse_req *req, struct kiocb *iocb, 1023 struct inode *inode, loff_t pos, 1024 size_t count) 1025 { 1026 size_t res; 1027 unsigned offset; 1028 unsigned i; 1029 struct fuse_io_priv io = FUSE_IO_PRIV_SYNC(iocb); 1030 1031 for (i = 0; i < req->num_pages; i++) 1032 fuse_wait_on_page_writeback(inode, req->pages[i]->index); 1033 1034 res = fuse_send_write(req, &io, pos, count, NULL); 1035 1036 offset = req->page_descs[0].offset; 1037 count = res; 1038 for (i = 0; i < req->num_pages; i++) { 1039 struct page *page = req->pages[i]; 1040 1041 if (!req->out.h.error && !offset && count >= PAGE_SIZE) 1042 SetPageUptodate(page); 1043 1044 if (count > PAGE_SIZE - offset) 1045 count -= PAGE_SIZE - offset; 1046 else 1047 count = 0; 1048 offset = 0; 1049 1050 unlock_page(page); 1051 put_page(page); 1052 } 1053 1054 return res; 1055 } 1056 1057 static ssize_t fuse_fill_write_pages(struct fuse_req *req, 1058 struct address_space *mapping, 1059 struct iov_iter *ii, loff_t pos) 1060 { 1061 struct fuse_conn *fc = get_fuse_conn(mapping->host); 1062 unsigned offset = pos & (PAGE_SIZE - 1); 1063 size_t count = 0; 1064 int err; 1065 1066 req->in.argpages = 1; 1067 req->page_descs[0].offset = offset; 1068 1069 do { 1070 size_t tmp; 1071 struct page *page; 1072 pgoff_t index = pos >> PAGE_SHIFT; 1073 size_t bytes = min_t(size_t, PAGE_SIZE - offset, 1074 iov_iter_count(ii)); 1075 1076 bytes = min_t(size_t, bytes, fc->max_write - count); 1077 1078 again: 1079 err = -EFAULT; 1080 if (iov_iter_fault_in_readable(ii, bytes)) 1081 break; 1082 1083 err = -ENOMEM; 1084 page = grab_cache_page_write_begin(mapping, index, 0); 1085 if (!page) 1086 break; 1087 1088 if (mapping_writably_mapped(mapping)) 1089 flush_dcache_page(page); 1090 1091 tmp = iov_iter_copy_from_user_atomic(page, ii, offset, bytes); 1092 flush_dcache_page(page); 1093 1094 iov_iter_advance(ii, tmp); 1095 if (!tmp) { 1096 unlock_page(page); 1097 put_page(page); 1098 bytes = min(bytes, iov_iter_single_seg_count(ii)); 1099 goto again; 1100 } 1101 1102 err = 0; 1103 req->pages[req->num_pages] = page; 1104 req->page_descs[req->num_pages].length = tmp; 1105 req->num_pages++; 1106 1107 count += tmp; 1108 pos += tmp; 1109 offset += tmp; 1110 if (offset == PAGE_SIZE) 1111 offset = 0; 1112 1113 if (!fc->big_writes) 1114 break; 1115 } while (iov_iter_count(ii) && count < fc->max_write && 1116 req->num_pages < req->max_pages && offset == 0); 1117 1118 return count > 0 ? count : err; 1119 } 1120 1121 static inline unsigned int fuse_wr_pages(loff_t pos, size_t len, 1122 unsigned int max_pages) 1123 { 1124 return min_t(unsigned int, 1125 ((pos + len - 1) >> PAGE_SHIFT) - 1126 (pos >> PAGE_SHIFT) + 1, 1127 max_pages); 1128 } 1129 1130 static ssize_t fuse_perform_write(struct kiocb *iocb, 1131 struct address_space *mapping, 1132 struct iov_iter *ii, loff_t pos) 1133 { 1134 struct inode *inode = mapping->host; 1135 struct fuse_conn *fc = get_fuse_conn(inode); 1136 struct fuse_inode *fi = get_fuse_inode(inode); 1137 int err = 0; 1138 ssize_t res = 0; 1139 1140 if (inode->i_size < pos + iov_iter_count(ii)) 1141 set_bit(FUSE_I_SIZE_UNSTABLE, &fi->state); 1142 1143 do { 1144 struct fuse_req *req; 1145 ssize_t count; 1146 unsigned int nr_pages = fuse_wr_pages(pos, iov_iter_count(ii), 1147 fc->max_pages); 1148 1149 req = fuse_get_req(fc, nr_pages); 1150 if (IS_ERR(req)) { 1151 err = PTR_ERR(req); 1152 break; 1153 } 1154 1155 count = fuse_fill_write_pages(req, mapping, ii, pos); 1156 if (count <= 0) { 1157 err = count; 1158 } else { 1159 size_t num_written; 1160 1161 num_written = fuse_send_write_pages(req, iocb, inode, 1162 pos, count); 1163 err = req->out.h.error; 1164 if (!err) { 1165 res += num_written; 1166 pos += num_written; 1167 1168 /* break out of the loop on short write */ 1169 if (num_written != count) 1170 err = -EIO; 1171 } 1172 } 1173 fuse_put_request(fc, req); 1174 } while (!err && iov_iter_count(ii)); 1175 1176 if (res > 0) 1177 fuse_write_update_size(inode, pos); 1178 1179 clear_bit(FUSE_I_SIZE_UNSTABLE, &fi->state); 1180 fuse_invalidate_attr(inode); 1181 1182 return res > 0 ? res : err; 1183 } 1184 1185 static ssize_t fuse_cache_write_iter(struct kiocb *iocb, struct iov_iter *from) 1186 { 1187 struct file *file = iocb->ki_filp; 1188 struct address_space *mapping = file->f_mapping; 1189 ssize_t written = 0; 1190 ssize_t written_buffered = 0; 1191 struct inode *inode = mapping->host; 1192 ssize_t err; 1193 loff_t endbyte = 0; 1194 1195 if (get_fuse_conn(inode)->writeback_cache) { 1196 /* Update size (EOF optimization) and mode (SUID clearing) */ 1197 err = fuse_update_attributes(mapping->host, file); 1198 if (err) 1199 return err; 1200 1201 return generic_file_write_iter(iocb, from); 1202 } 1203 1204 inode_lock(inode); 1205 1206 /* We can write back this queue in page reclaim */ 1207 current->backing_dev_info = inode_to_bdi(inode); 1208 1209 err = generic_write_checks(iocb, from); 1210 if (err <= 0) 1211 goto out; 1212 1213 err = file_remove_privs(file); 1214 if (err) 1215 goto out; 1216 1217 err = file_update_time(file); 1218 if (err) 1219 goto out; 1220 1221 if (iocb->ki_flags & IOCB_DIRECT) { 1222 loff_t pos = iocb->ki_pos; 1223 written = generic_file_direct_write(iocb, from); 1224 if (written < 0 || !iov_iter_count(from)) 1225 goto out; 1226 1227 pos += written; 1228 1229 written_buffered = fuse_perform_write(iocb, mapping, from, pos); 1230 if (written_buffered < 0) { 1231 err = written_buffered; 1232 goto out; 1233 } 1234 endbyte = pos + written_buffered - 1; 1235 1236 err = filemap_write_and_wait_range(file->f_mapping, pos, 1237 endbyte); 1238 if (err) 1239 goto out; 1240 1241 invalidate_mapping_pages(file->f_mapping, 1242 pos >> PAGE_SHIFT, 1243 endbyte >> PAGE_SHIFT); 1244 1245 written += written_buffered; 1246 iocb->ki_pos = pos + written_buffered; 1247 } else { 1248 written = fuse_perform_write(iocb, mapping, from, iocb->ki_pos); 1249 if (written >= 0) 1250 iocb->ki_pos += written; 1251 } 1252 out: 1253 current->backing_dev_info = NULL; 1254 inode_unlock(inode); 1255 if (written > 0) 1256 written = generic_write_sync(iocb, written); 1257 1258 return written ? written : err; 1259 } 1260 1261 static inline void fuse_page_descs_length_init(struct fuse_req *req, 1262 unsigned index, unsigned nr_pages) 1263 { 1264 int i; 1265 1266 for (i = index; i < index + nr_pages; i++) 1267 req->page_descs[i].length = PAGE_SIZE - 1268 req->page_descs[i].offset; 1269 } 1270 1271 static inline unsigned long fuse_get_user_addr(const struct iov_iter *ii) 1272 { 1273 return (unsigned long)ii->iov->iov_base + ii->iov_offset; 1274 } 1275 1276 static inline size_t fuse_get_frag_size(const struct iov_iter *ii, 1277 size_t max_size) 1278 { 1279 return min(iov_iter_single_seg_count(ii), max_size); 1280 } 1281 1282 static int fuse_get_user_pages(struct fuse_req *req, struct iov_iter *ii, 1283 size_t *nbytesp, int write) 1284 { 1285 size_t nbytes = 0; /* # bytes already packed in req */ 1286 ssize_t ret = 0; 1287 1288 /* Special case for kernel I/O: can copy directly into the buffer */ 1289 if (iov_iter_is_kvec(ii)) { 1290 unsigned long user_addr = fuse_get_user_addr(ii); 1291 size_t frag_size = fuse_get_frag_size(ii, *nbytesp); 1292 1293 if (write) 1294 req->in.args[1].value = (void *) user_addr; 1295 else 1296 req->out.args[0].value = (void *) user_addr; 1297 1298 iov_iter_advance(ii, frag_size); 1299 *nbytesp = frag_size; 1300 return 0; 1301 } 1302 1303 while (nbytes < *nbytesp && req->num_pages < req->max_pages) { 1304 unsigned npages; 1305 size_t start; 1306 ret = iov_iter_get_pages(ii, &req->pages[req->num_pages], 1307 *nbytesp - nbytes, 1308 req->max_pages - req->num_pages, 1309 &start); 1310 if (ret < 0) 1311 break; 1312 1313 iov_iter_advance(ii, ret); 1314 nbytes += ret; 1315 1316 ret += start; 1317 npages = (ret + PAGE_SIZE - 1) / PAGE_SIZE; 1318 1319 req->page_descs[req->num_pages].offset = start; 1320 fuse_page_descs_length_init(req, req->num_pages, npages); 1321 1322 req->num_pages += npages; 1323 req->page_descs[req->num_pages - 1].length -= 1324 (PAGE_SIZE - ret) & (PAGE_SIZE - 1); 1325 } 1326 1327 if (write) 1328 req->in.argpages = 1; 1329 else 1330 req->out.argpages = 1; 1331 1332 *nbytesp = nbytes; 1333 1334 return ret < 0 ? ret : 0; 1335 } 1336 1337 ssize_t fuse_direct_io(struct fuse_io_priv *io, struct iov_iter *iter, 1338 loff_t *ppos, int flags) 1339 { 1340 int write = flags & FUSE_DIO_WRITE; 1341 int cuse = flags & FUSE_DIO_CUSE; 1342 struct file *file = io->iocb->ki_filp; 1343 struct inode *inode = file->f_mapping->host; 1344 struct fuse_file *ff = file->private_data; 1345 struct fuse_conn *fc = ff->fc; 1346 size_t nmax = write ? fc->max_write : fc->max_read; 1347 loff_t pos = *ppos; 1348 size_t count = iov_iter_count(iter); 1349 pgoff_t idx_from = pos >> PAGE_SHIFT; 1350 pgoff_t idx_to = (pos + count - 1) >> PAGE_SHIFT; 1351 ssize_t res = 0; 1352 struct fuse_req *req; 1353 int err = 0; 1354 1355 if (io->async) 1356 req = fuse_get_req_for_background(fc, iov_iter_npages(iter, 1357 fc->max_pages)); 1358 else 1359 req = fuse_get_req(fc, iov_iter_npages(iter, fc->max_pages)); 1360 if (IS_ERR(req)) 1361 return PTR_ERR(req); 1362 1363 if (!cuse && fuse_range_is_writeback(inode, idx_from, idx_to)) { 1364 if (!write) 1365 inode_lock(inode); 1366 fuse_sync_writes(inode); 1367 if (!write) 1368 inode_unlock(inode); 1369 } 1370 1371 io->should_dirty = !write && iter_is_iovec(iter); 1372 while (count) { 1373 size_t nres; 1374 fl_owner_t owner = current->files; 1375 size_t nbytes = min(count, nmax); 1376 err = fuse_get_user_pages(req, iter, &nbytes, write); 1377 if (err && !nbytes) 1378 break; 1379 1380 if (write) { 1381 if (!capable(CAP_FSETID)) { 1382 struct fuse_write_in *inarg; 1383 1384 inarg = &req->misc.write.in; 1385 inarg->write_flags |= FUSE_WRITE_KILL_PRIV; 1386 } 1387 nres = fuse_send_write(req, io, pos, nbytes, owner); 1388 } else { 1389 nres = fuse_send_read(req, io, pos, nbytes, owner); 1390 } 1391 1392 if (!io->async) 1393 fuse_release_user_pages(req, io->should_dirty); 1394 if (req->out.h.error) { 1395 err = req->out.h.error; 1396 break; 1397 } else if (nres > nbytes) { 1398 res = 0; 1399 err = -EIO; 1400 break; 1401 } 1402 count -= nres; 1403 res += nres; 1404 pos += nres; 1405 if (nres != nbytes) 1406 break; 1407 if (count) { 1408 fuse_put_request(fc, req); 1409 if (io->async) 1410 req = fuse_get_req_for_background(fc, 1411 iov_iter_npages(iter, fc->max_pages)); 1412 else 1413 req = fuse_get_req(fc, iov_iter_npages(iter, 1414 fc->max_pages)); 1415 if (IS_ERR(req)) 1416 break; 1417 } 1418 } 1419 if (!IS_ERR(req)) 1420 fuse_put_request(fc, req); 1421 if (res > 0) 1422 *ppos = pos; 1423 1424 return res > 0 ? res : err; 1425 } 1426 EXPORT_SYMBOL_GPL(fuse_direct_io); 1427 1428 static ssize_t __fuse_direct_read(struct fuse_io_priv *io, 1429 struct iov_iter *iter, 1430 loff_t *ppos) 1431 { 1432 ssize_t res; 1433 struct inode *inode = file_inode(io->iocb->ki_filp); 1434 1435 res = fuse_direct_io(io, iter, ppos, 0); 1436 1437 fuse_invalidate_atime(inode); 1438 1439 return res; 1440 } 1441 1442 static ssize_t fuse_direct_IO(struct kiocb *iocb, struct iov_iter *iter); 1443 1444 static ssize_t fuse_direct_read_iter(struct kiocb *iocb, struct iov_iter *to) 1445 { 1446 ssize_t res; 1447 1448 if (!is_sync_kiocb(iocb) && iocb->ki_flags & IOCB_DIRECT) { 1449 res = fuse_direct_IO(iocb, to); 1450 } else { 1451 struct fuse_io_priv io = FUSE_IO_PRIV_SYNC(iocb); 1452 1453 res = __fuse_direct_read(&io, to, &iocb->ki_pos); 1454 } 1455 1456 return res; 1457 } 1458 1459 static ssize_t fuse_direct_write_iter(struct kiocb *iocb, struct iov_iter *from) 1460 { 1461 struct inode *inode = file_inode(iocb->ki_filp); 1462 struct fuse_io_priv io = FUSE_IO_PRIV_SYNC(iocb); 1463 ssize_t res; 1464 1465 /* Don't allow parallel writes to the same file */ 1466 inode_lock(inode); 1467 res = generic_write_checks(iocb, from); 1468 if (res > 0) { 1469 if (!is_sync_kiocb(iocb) && iocb->ki_flags & IOCB_DIRECT) { 1470 res = fuse_direct_IO(iocb, from); 1471 } else { 1472 res = fuse_direct_io(&io, from, &iocb->ki_pos, 1473 FUSE_DIO_WRITE); 1474 } 1475 } 1476 fuse_invalidate_attr(inode); 1477 if (res > 0) 1478 fuse_write_update_size(inode, iocb->ki_pos); 1479 inode_unlock(inode); 1480 1481 return res; 1482 } 1483 1484 static ssize_t fuse_file_read_iter(struct kiocb *iocb, struct iov_iter *to) 1485 { 1486 struct file *file = iocb->ki_filp; 1487 struct fuse_file *ff = file->private_data; 1488 1489 if (is_bad_inode(file_inode(file))) 1490 return -EIO; 1491 1492 if (!(ff->open_flags & FOPEN_DIRECT_IO)) 1493 return fuse_cache_read_iter(iocb, to); 1494 else 1495 return fuse_direct_read_iter(iocb, to); 1496 } 1497 1498 static ssize_t fuse_file_write_iter(struct kiocb *iocb, struct iov_iter *from) 1499 { 1500 struct file *file = iocb->ki_filp; 1501 struct fuse_file *ff = file->private_data; 1502 1503 if (is_bad_inode(file_inode(file))) 1504 return -EIO; 1505 1506 if (!(ff->open_flags & FOPEN_DIRECT_IO)) 1507 return fuse_cache_write_iter(iocb, from); 1508 else 1509 return fuse_direct_write_iter(iocb, from); 1510 } 1511 1512 static void fuse_writepage_free(struct fuse_conn *fc, struct fuse_req *req) 1513 { 1514 int i; 1515 1516 for (i = 0; i < req->num_pages; i++) 1517 __free_page(req->pages[i]); 1518 1519 if (req->ff) 1520 fuse_file_put(req->ff, false, false); 1521 } 1522 1523 static void fuse_writepage_finish(struct fuse_conn *fc, struct fuse_req *req) 1524 { 1525 struct inode *inode = req->inode; 1526 struct fuse_inode *fi = get_fuse_inode(inode); 1527 struct backing_dev_info *bdi = inode_to_bdi(inode); 1528 int i; 1529 1530 list_del(&req->writepages_entry); 1531 for (i = 0; i < req->num_pages; i++) { 1532 dec_wb_stat(&bdi->wb, WB_WRITEBACK); 1533 dec_node_page_state(req->pages[i], NR_WRITEBACK_TEMP); 1534 wb_writeout_inc(&bdi->wb); 1535 } 1536 wake_up(&fi->page_waitq); 1537 } 1538 1539 /* Called under fi->lock, may release and reacquire it */ 1540 static void fuse_send_writepage(struct fuse_conn *fc, struct fuse_req *req, 1541 loff_t size) 1542 __releases(fi->lock) 1543 __acquires(fi->lock) 1544 { 1545 struct fuse_req *aux, *next; 1546 struct fuse_inode *fi = get_fuse_inode(req->inode); 1547 struct fuse_write_in *inarg = &req->misc.write.in; 1548 __u64 data_size = req->num_pages * PAGE_SIZE; 1549 bool queued; 1550 1551 if (inarg->offset + data_size <= size) { 1552 inarg->size = data_size; 1553 } else if (inarg->offset < size) { 1554 inarg->size = size - inarg->offset; 1555 } else { 1556 /* Got truncated off completely */ 1557 goto out_free; 1558 } 1559 1560 req->in.args[1].size = inarg->size; 1561 queued = fuse_request_queue_background(fc, req); 1562 /* Fails on broken connection only */ 1563 if (unlikely(!queued)) 1564 goto out_free; 1565 1566 fi->writectr++; 1567 return; 1568 1569 out_free: 1570 fuse_writepage_finish(fc, req); 1571 spin_unlock(&fi->lock); 1572 1573 /* After fuse_writepage_finish() aux request list is private */ 1574 for (aux = req->misc.write.next; aux; aux = next) { 1575 next = aux->misc.write.next; 1576 aux->misc.write.next = NULL; 1577 fuse_writepage_free(fc, aux); 1578 fuse_put_request(fc, aux); 1579 } 1580 1581 fuse_writepage_free(fc, req); 1582 fuse_put_request(fc, req); 1583 spin_lock(&fi->lock); 1584 } 1585 1586 /* 1587 * If fi->writectr is positive (no truncate or fsync going on) send 1588 * all queued writepage requests. 1589 * 1590 * Called with fi->lock 1591 */ 1592 void fuse_flush_writepages(struct inode *inode) 1593 __releases(fi->lock) 1594 __acquires(fi->lock) 1595 { 1596 struct fuse_conn *fc = get_fuse_conn(inode); 1597 struct fuse_inode *fi = get_fuse_inode(inode); 1598 loff_t crop = i_size_read(inode); 1599 struct fuse_req *req; 1600 1601 while (fi->writectr >= 0 && !list_empty(&fi->queued_writes)) { 1602 req = list_entry(fi->queued_writes.next, struct fuse_req, list); 1603 list_del_init(&req->list); 1604 fuse_send_writepage(fc, req, crop); 1605 } 1606 } 1607 1608 static void fuse_writepage_end(struct fuse_conn *fc, struct fuse_req *req) 1609 { 1610 struct inode *inode = req->inode; 1611 struct fuse_inode *fi = get_fuse_inode(inode); 1612 1613 mapping_set_error(inode->i_mapping, req->out.h.error); 1614 spin_lock(&fi->lock); 1615 while (req->misc.write.next) { 1616 struct fuse_conn *fc = get_fuse_conn(inode); 1617 struct fuse_write_in *inarg = &req->misc.write.in; 1618 struct fuse_req *next = req->misc.write.next; 1619 req->misc.write.next = next->misc.write.next; 1620 next->misc.write.next = NULL; 1621 next->ff = fuse_file_get(req->ff); 1622 list_add(&next->writepages_entry, &fi->writepages); 1623 1624 /* 1625 * Skip fuse_flush_writepages() to make it easy to crop requests 1626 * based on primary request size. 1627 * 1628 * 1st case (trivial): there are no concurrent activities using 1629 * fuse_set/release_nowrite. Then we're on safe side because 1630 * fuse_flush_writepages() would call fuse_send_writepage() 1631 * anyway. 1632 * 1633 * 2nd case: someone called fuse_set_nowrite and it is waiting 1634 * now for completion of all in-flight requests. This happens 1635 * rarely and no more than once per page, so this should be 1636 * okay. 1637 * 1638 * 3rd case: someone (e.g. fuse_do_setattr()) is in the middle 1639 * of fuse_set_nowrite..fuse_release_nowrite section. The fact 1640 * that fuse_set_nowrite returned implies that all in-flight 1641 * requests were completed along with all of their secondary 1642 * requests. Further primary requests are blocked by negative 1643 * writectr. Hence there cannot be any in-flight requests and 1644 * no invocations of fuse_writepage_end() while we're in 1645 * fuse_set_nowrite..fuse_release_nowrite section. 1646 */ 1647 fuse_send_writepage(fc, next, inarg->offset + inarg->size); 1648 } 1649 fi->writectr--; 1650 fuse_writepage_finish(fc, req); 1651 spin_unlock(&fi->lock); 1652 fuse_writepage_free(fc, req); 1653 } 1654 1655 static struct fuse_file *__fuse_write_file_get(struct fuse_conn *fc, 1656 struct fuse_inode *fi) 1657 { 1658 struct fuse_file *ff = NULL; 1659 1660 spin_lock(&fi->lock); 1661 if (!list_empty(&fi->write_files)) { 1662 ff = list_entry(fi->write_files.next, struct fuse_file, 1663 write_entry); 1664 fuse_file_get(ff); 1665 } 1666 spin_unlock(&fi->lock); 1667 1668 return ff; 1669 } 1670 1671 static struct fuse_file *fuse_write_file_get(struct fuse_conn *fc, 1672 struct fuse_inode *fi) 1673 { 1674 struct fuse_file *ff = __fuse_write_file_get(fc, fi); 1675 WARN_ON(!ff); 1676 return ff; 1677 } 1678 1679 int fuse_write_inode(struct inode *inode, struct writeback_control *wbc) 1680 { 1681 struct fuse_conn *fc = get_fuse_conn(inode); 1682 struct fuse_inode *fi = get_fuse_inode(inode); 1683 struct fuse_file *ff; 1684 int err; 1685 1686 ff = __fuse_write_file_get(fc, fi); 1687 err = fuse_flush_times(inode, ff); 1688 if (ff) 1689 fuse_file_put(ff, false, false); 1690 1691 return err; 1692 } 1693 1694 static int fuse_writepage_locked(struct page *page) 1695 { 1696 struct address_space *mapping = page->mapping; 1697 struct inode *inode = mapping->host; 1698 struct fuse_conn *fc = get_fuse_conn(inode); 1699 struct fuse_inode *fi = get_fuse_inode(inode); 1700 struct fuse_req *req; 1701 struct page *tmp_page; 1702 int error = -ENOMEM; 1703 1704 set_page_writeback(page); 1705 1706 req = fuse_request_alloc_nofs(1); 1707 if (!req) 1708 goto err; 1709 1710 /* writeback always goes to bg_queue */ 1711 __set_bit(FR_BACKGROUND, &req->flags); 1712 tmp_page = alloc_page(GFP_NOFS | __GFP_HIGHMEM); 1713 if (!tmp_page) 1714 goto err_free; 1715 1716 error = -EIO; 1717 req->ff = fuse_write_file_get(fc, fi); 1718 if (!req->ff) 1719 goto err_nofile; 1720 1721 fuse_write_fill(req, req->ff, page_offset(page), 0); 1722 1723 copy_highpage(tmp_page, page); 1724 req->misc.write.in.write_flags |= FUSE_WRITE_CACHE; 1725 req->misc.write.next = NULL; 1726 req->in.argpages = 1; 1727 req->num_pages = 1; 1728 req->pages[0] = tmp_page; 1729 req->page_descs[0].offset = 0; 1730 req->page_descs[0].length = PAGE_SIZE; 1731 req->end = fuse_writepage_end; 1732 req->inode = inode; 1733 1734 inc_wb_stat(&inode_to_bdi(inode)->wb, WB_WRITEBACK); 1735 inc_node_page_state(tmp_page, NR_WRITEBACK_TEMP); 1736 1737 spin_lock(&fi->lock); 1738 list_add(&req->writepages_entry, &fi->writepages); 1739 list_add_tail(&req->list, &fi->queued_writes); 1740 fuse_flush_writepages(inode); 1741 spin_unlock(&fi->lock); 1742 1743 end_page_writeback(page); 1744 1745 return 0; 1746 1747 err_nofile: 1748 __free_page(tmp_page); 1749 err_free: 1750 fuse_request_free(req); 1751 err: 1752 mapping_set_error(page->mapping, error); 1753 end_page_writeback(page); 1754 return error; 1755 } 1756 1757 static int fuse_writepage(struct page *page, struct writeback_control *wbc) 1758 { 1759 int err; 1760 1761 if (fuse_page_is_writeback(page->mapping->host, page->index)) { 1762 /* 1763 * ->writepages() should be called for sync() and friends. We 1764 * should only get here on direct reclaim and then we are 1765 * allowed to skip a page which is already in flight 1766 */ 1767 WARN_ON(wbc->sync_mode == WB_SYNC_ALL); 1768 1769 redirty_page_for_writepage(wbc, page); 1770 unlock_page(page); 1771 return 0; 1772 } 1773 1774 err = fuse_writepage_locked(page); 1775 unlock_page(page); 1776 1777 return err; 1778 } 1779 1780 struct fuse_fill_wb_data { 1781 struct fuse_req *req; 1782 struct fuse_file *ff; 1783 struct inode *inode; 1784 struct page **orig_pages; 1785 }; 1786 1787 static void fuse_writepages_send(struct fuse_fill_wb_data *data) 1788 { 1789 struct fuse_req *req = data->req; 1790 struct inode *inode = data->inode; 1791 struct fuse_inode *fi = get_fuse_inode(inode); 1792 int num_pages = req->num_pages; 1793 int i; 1794 1795 req->ff = fuse_file_get(data->ff); 1796 spin_lock(&fi->lock); 1797 list_add_tail(&req->list, &fi->queued_writes); 1798 fuse_flush_writepages(inode); 1799 spin_unlock(&fi->lock); 1800 1801 for (i = 0; i < num_pages; i++) 1802 end_page_writeback(data->orig_pages[i]); 1803 } 1804 1805 /* 1806 * First recheck under fi->lock if the offending offset is still under 1807 * writeback. If yes, then iterate auxiliary write requests, to see if there's 1808 * one already added for a page at this offset. If there's none, then insert 1809 * this new request onto the auxiliary list, otherwise reuse the existing one by 1810 * copying the new page contents over to the old temporary page. 1811 */ 1812 static bool fuse_writepage_in_flight(struct fuse_req *new_req, 1813 struct page *page) 1814 { 1815 struct fuse_conn *fc = get_fuse_conn(new_req->inode); 1816 struct fuse_inode *fi = get_fuse_inode(new_req->inode); 1817 struct fuse_req *tmp; 1818 struct fuse_req *old_req; 1819 1820 WARN_ON(new_req->num_pages != 0); 1821 1822 spin_lock(&fi->lock); 1823 list_del(&new_req->writepages_entry); 1824 old_req = fuse_find_writeback(fi, page->index, page->index); 1825 if (!old_req) { 1826 list_add(&new_req->writepages_entry, &fi->writepages); 1827 spin_unlock(&fi->lock); 1828 return false; 1829 } 1830 1831 new_req->num_pages = 1; 1832 for (tmp = old_req->misc.write.next; tmp; tmp = tmp->misc.write.next) { 1833 pgoff_t curr_index; 1834 1835 WARN_ON(tmp->inode != new_req->inode); 1836 curr_index = tmp->misc.write.in.offset >> PAGE_SHIFT; 1837 if (curr_index == page->index) { 1838 WARN_ON(tmp->num_pages != 1); 1839 WARN_ON(!test_bit(FR_PENDING, &tmp->flags)); 1840 swap(tmp->pages[0], new_req->pages[0]); 1841 break; 1842 } 1843 } 1844 1845 if (!tmp) { 1846 new_req->misc.write.next = old_req->misc.write.next; 1847 old_req->misc.write.next = new_req; 1848 } 1849 1850 spin_unlock(&fi->lock); 1851 1852 if (tmp) { 1853 struct backing_dev_info *bdi = inode_to_bdi(new_req->inode); 1854 1855 dec_wb_stat(&bdi->wb, WB_WRITEBACK); 1856 dec_node_page_state(new_req->pages[0], NR_WRITEBACK_TEMP); 1857 wb_writeout_inc(&bdi->wb); 1858 fuse_writepage_free(fc, new_req); 1859 fuse_request_free(new_req); 1860 } 1861 1862 return true; 1863 } 1864 1865 static int fuse_writepages_fill(struct page *page, 1866 struct writeback_control *wbc, void *_data) 1867 { 1868 struct fuse_fill_wb_data *data = _data; 1869 struct fuse_req *req = data->req; 1870 struct inode *inode = data->inode; 1871 struct fuse_inode *fi = get_fuse_inode(inode); 1872 struct fuse_conn *fc = get_fuse_conn(inode); 1873 struct page *tmp_page; 1874 bool is_writeback; 1875 int err; 1876 1877 if (!data->ff) { 1878 err = -EIO; 1879 data->ff = fuse_write_file_get(fc, get_fuse_inode(inode)); 1880 if (!data->ff) 1881 goto out_unlock; 1882 } 1883 1884 /* 1885 * Being under writeback is unlikely but possible. For example direct 1886 * read to an mmaped fuse file will set the page dirty twice; once when 1887 * the pages are faulted with get_user_pages(), and then after the read 1888 * completed. 1889 */ 1890 is_writeback = fuse_page_is_writeback(inode, page->index); 1891 1892 if (req && req->num_pages && 1893 (is_writeback || req->num_pages == fc->max_pages || 1894 (req->num_pages + 1) * PAGE_SIZE > fc->max_write || 1895 data->orig_pages[req->num_pages - 1]->index + 1 != page->index)) { 1896 fuse_writepages_send(data); 1897 data->req = NULL; 1898 } else if (req && req->num_pages == req->max_pages) { 1899 if (!fuse_req_realloc_pages(fc, req, GFP_NOFS)) { 1900 fuse_writepages_send(data); 1901 req = data->req = NULL; 1902 } 1903 } 1904 1905 err = -ENOMEM; 1906 tmp_page = alloc_page(GFP_NOFS | __GFP_HIGHMEM); 1907 if (!tmp_page) 1908 goto out_unlock; 1909 1910 /* 1911 * The page must not be redirtied until the writeout is completed 1912 * (i.e. userspace has sent a reply to the write request). Otherwise 1913 * there could be more than one temporary page instance for each real 1914 * page. 1915 * 1916 * This is ensured by holding the page lock in page_mkwrite() while 1917 * checking fuse_page_is_writeback(). We already hold the page lock 1918 * since clear_page_dirty_for_io() and keep it held until we add the 1919 * request to the fi->writepages list and increment req->num_pages. 1920 * After this fuse_page_is_writeback() will indicate that the page is 1921 * under writeback, so we can release the page lock. 1922 */ 1923 if (data->req == NULL) { 1924 struct fuse_inode *fi = get_fuse_inode(inode); 1925 1926 err = -ENOMEM; 1927 req = fuse_request_alloc_nofs(FUSE_REQ_INLINE_PAGES); 1928 if (!req) { 1929 __free_page(tmp_page); 1930 goto out_unlock; 1931 } 1932 1933 fuse_write_fill(req, data->ff, page_offset(page), 0); 1934 req->misc.write.in.write_flags |= FUSE_WRITE_CACHE; 1935 req->misc.write.next = NULL; 1936 req->in.argpages = 1; 1937 __set_bit(FR_BACKGROUND, &req->flags); 1938 req->num_pages = 0; 1939 req->end = fuse_writepage_end; 1940 req->inode = inode; 1941 1942 spin_lock(&fi->lock); 1943 list_add(&req->writepages_entry, &fi->writepages); 1944 spin_unlock(&fi->lock); 1945 1946 data->req = req; 1947 } 1948 set_page_writeback(page); 1949 1950 copy_highpage(tmp_page, page); 1951 req->pages[req->num_pages] = tmp_page; 1952 req->page_descs[req->num_pages].offset = 0; 1953 req->page_descs[req->num_pages].length = PAGE_SIZE; 1954 1955 inc_wb_stat(&inode_to_bdi(inode)->wb, WB_WRITEBACK); 1956 inc_node_page_state(tmp_page, NR_WRITEBACK_TEMP); 1957 1958 err = 0; 1959 if (is_writeback && fuse_writepage_in_flight(req, page)) { 1960 end_page_writeback(page); 1961 data->req = NULL; 1962 goto out_unlock; 1963 } 1964 data->orig_pages[req->num_pages] = page; 1965 1966 /* 1967 * Protected by fi->lock against concurrent access by 1968 * fuse_page_is_writeback(). 1969 */ 1970 spin_lock(&fi->lock); 1971 req->num_pages++; 1972 spin_unlock(&fi->lock); 1973 1974 out_unlock: 1975 unlock_page(page); 1976 1977 return err; 1978 } 1979 1980 static int fuse_writepages(struct address_space *mapping, 1981 struct writeback_control *wbc) 1982 { 1983 struct inode *inode = mapping->host; 1984 struct fuse_conn *fc = get_fuse_conn(inode); 1985 struct fuse_fill_wb_data data; 1986 int err; 1987 1988 err = -EIO; 1989 if (is_bad_inode(inode)) 1990 goto out; 1991 1992 data.inode = inode; 1993 data.req = NULL; 1994 data.ff = NULL; 1995 1996 err = -ENOMEM; 1997 data.orig_pages = kcalloc(fc->max_pages, 1998 sizeof(struct page *), 1999 GFP_NOFS); 2000 if (!data.orig_pages) 2001 goto out; 2002 2003 err = write_cache_pages(mapping, wbc, fuse_writepages_fill, &data); 2004 if (data.req) { 2005 /* Ignore errors if we can write at least one page */ 2006 BUG_ON(!data.req->num_pages); 2007 fuse_writepages_send(&data); 2008 err = 0; 2009 } 2010 if (data.ff) 2011 fuse_file_put(data.ff, false, false); 2012 2013 kfree(data.orig_pages); 2014 out: 2015 return err; 2016 } 2017 2018 /* 2019 * It's worthy to make sure that space is reserved on disk for the write, 2020 * but how to implement it without killing performance need more thinking. 2021 */ 2022 static int fuse_write_begin(struct file *file, struct address_space *mapping, 2023 loff_t pos, unsigned len, unsigned flags, 2024 struct page **pagep, void **fsdata) 2025 { 2026 pgoff_t index = pos >> PAGE_SHIFT; 2027 struct fuse_conn *fc = get_fuse_conn(file_inode(file)); 2028 struct page *page; 2029 loff_t fsize; 2030 int err = -ENOMEM; 2031 2032 WARN_ON(!fc->writeback_cache); 2033 2034 page = grab_cache_page_write_begin(mapping, index, flags); 2035 if (!page) 2036 goto error; 2037 2038 fuse_wait_on_page_writeback(mapping->host, page->index); 2039 2040 if (PageUptodate(page) || len == PAGE_SIZE) 2041 goto success; 2042 /* 2043 * Check if the start this page comes after the end of file, in which 2044 * case the readpage can be optimized away. 2045 */ 2046 fsize = i_size_read(mapping->host); 2047 if (fsize <= (pos & PAGE_MASK)) { 2048 size_t off = pos & ~PAGE_MASK; 2049 if (off) 2050 zero_user_segment(page, 0, off); 2051 goto success; 2052 } 2053 err = fuse_do_readpage(file, page); 2054 if (err) 2055 goto cleanup; 2056 success: 2057 *pagep = page; 2058 return 0; 2059 2060 cleanup: 2061 unlock_page(page); 2062 put_page(page); 2063 error: 2064 return err; 2065 } 2066 2067 static int fuse_write_end(struct file *file, struct address_space *mapping, 2068 loff_t pos, unsigned len, unsigned copied, 2069 struct page *page, void *fsdata) 2070 { 2071 struct inode *inode = page->mapping->host; 2072 2073 /* Haven't copied anything? Skip zeroing, size extending, dirtying. */ 2074 if (!copied) 2075 goto unlock; 2076 2077 if (!PageUptodate(page)) { 2078 /* Zero any unwritten bytes at the end of the page */ 2079 size_t endoff = (pos + copied) & ~PAGE_MASK; 2080 if (endoff) 2081 zero_user_segment(page, endoff, PAGE_SIZE); 2082 SetPageUptodate(page); 2083 } 2084 2085 fuse_write_update_size(inode, pos + copied); 2086 set_page_dirty(page); 2087 2088 unlock: 2089 unlock_page(page); 2090 put_page(page); 2091 2092 return copied; 2093 } 2094 2095 static int fuse_launder_page(struct page *page) 2096 { 2097 int err = 0; 2098 if (clear_page_dirty_for_io(page)) { 2099 struct inode *inode = page->mapping->host; 2100 err = fuse_writepage_locked(page); 2101 if (!err) 2102 fuse_wait_on_page_writeback(inode, page->index); 2103 } 2104 return err; 2105 } 2106 2107 /* 2108 * Write back dirty pages now, because there may not be any suitable 2109 * open files later 2110 */ 2111 static void fuse_vma_close(struct vm_area_struct *vma) 2112 { 2113 filemap_write_and_wait(vma->vm_file->f_mapping); 2114 } 2115 2116 /* 2117 * Wait for writeback against this page to complete before allowing it 2118 * to be marked dirty again, and hence written back again, possibly 2119 * before the previous writepage completed. 2120 * 2121 * Block here, instead of in ->writepage(), so that the userspace fs 2122 * can only block processes actually operating on the filesystem. 2123 * 2124 * Otherwise unprivileged userspace fs would be able to block 2125 * unrelated: 2126 * 2127 * - page migration 2128 * - sync(2) 2129 * - try_to_free_pages() with order > PAGE_ALLOC_COSTLY_ORDER 2130 */ 2131 static vm_fault_t fuse_page_mkwrite(struct vm_fault *vmf) 2132 { 2133 struct page *page = vmf->page; 2134 struct inode *inode = file_inode(vmf->vma->vm_file); 2135 2136 file_update_time(vmf->vma->vm_file); 2137 lock_page(page); 2138 if (page->mapping != inode->i_mapping) { 2139 unlock_page(page); 2140 return VM_FAULT_NOPAGE; 2141 } 2142 2143 fuse_wait_on_page_writeback(inode, page->index); 2144 return VM_FAULT_LOCKED; 2145 } 2146 2147 static const struct vm_operations_struct fuse_file_vm_ops = { 2148 .close = fuse_vma_close, 2149 .fault = filemap_fault, 2150 .map_pages = filemap_map_pages, 2151 .page_mkwrite = fuse_page_mkwrite, 2152 }; 2153 2154 static int fuse_file_mmap(struct file *file, struct vm_area_struct *vma) 2155 { 2156 struct fuse_file *ff = file->private_data; 2157 2158 if (ff->open_flags & FOPEN_DIRECT_IO) { 2159 /* Can't provide the coherency needed for MAP_SHARED */ 2160 if (vma->vm_flags & VM_MAYSHARE) 2161 return -ENODEV; 2162 2163 invalidate_inode_pages2(file->f_mapping); 2164 2165 return generic_file_mmap(file, vma); 2166 } 2167 2168 if ((vma->vm_flags & VM_SHARED) && (vma->vm_flags & VM_MAYWRITE)) 2169 fuse_link_write_file(file); 2170 2171 file_accessed(file); 2172 vma->vm_ops = &fuse_file_vm_ops; 2173 return 0; 2174 } 2175 2176 static int convert_fuse_file_lock(struct fuse_conn *fc, 2177 const struct fuse_file_lock *ffl, 2178 struct file_lock *fl) 2179 { 2180 switch (ffl->type) { 2181 case F_UNLCK: 2182 break; 2183 2184 case F_RDLCK: 2185 case F_WRLCK: 2186 if (ffl->start > OFFSET_MAX || ffl->end > OFFSET_MAX || 2187 ffl->end < ffl->start) 2188 return -EIO; 2189 2190 fl->fl_start = ffl->start; 2191 fl->fl_end = ffl->end; 2192 2193 /* 2194 * Convert pid into init's pid namespace. The locks API will 2195 * translate it into the caller's pid namespace. 2196 */ 2197 rcu_read_lock(); 2198 fl->fl_pid = pid_nr_ns(find_pid_ns(ffl->pid, fc->pid_ns), &init_pid_ns); 2199 rcu_read_unlock(); 2200 break; 2201 2202 default: 2203 return -EIO; 2204 } 2205 fl->fl_type = ffl->type; 2206 return 0; 2207 } 2208 2209 static void fuse_lk_fill(struct fuse_args *args, struct file *file, 2210 const struct file_lock *fl, int opcode, pid_t pid, 2211 int flock, struct fuse_lk_in *inarg) 2212 { 2213 struct inode *inode = file_inode(file); 2214 struct fuse_conn *fc = get_fuse_conn(inode); 2215 struct fuse_file *ff = file->private_data; 2216 2217 memset(inarg, 0, sizeof(*inarg)); 2218 inarg->fh = ff->fh; 2219 inarg->owner = fuse_lock_owner_id(fc, fl->fl_owner); 2220 inarg->lk.start = fl->fl_start; 2221 inarg->lk.end = fl->fl_end; 2222 inarg->lk.type = fl->fl_type; 2223 inarg->lk.pid = pid; 2224 if (flock) 2225 inarg->lk_flags |= FUSE_LK_FLOCK; 2226 args->in.h.opcode = opcode; 2227 args->in.h.nodeid = get_node_id(inode); 2228 args->in.numargs = 1; 2229 args->in.args[0].size = sizeof(*inarg); 2230 args->in.args[0].value = inarg; 2231 } 2232 2233 static int fuse_getlk(struct file *file, struct file_lock *fl) 2234 { 2235 struct inode *inode = file_inode(file); 2236 struct fuse_conn *fc = get_fuse_conn(inode); 2237 FUSE_ARGS(args); 2238 struct fuse_lk_in inarg; 2239 struct fuse_lk_out outarg; 2240 int err; 2241 2242 fuse_lk_fill(&args, file, fl, FUSE_GETLK, 0, 0, &inarg); 2243 args.out.numargs = 1; 2244 args.out.args[0].size = sizeof(outarg); 2245 args.out.args[0].value = &outarg; 2246 err = fuse_simple_request(fc, &args); 2247 if (!err) 2248 err = convert_fuse_file_lock(fc, &outarg.lk, fl); 2249 2250 return err; 2251 } 2252 2253 static int fuse_setlk(struct file *file, struct file_lock *fl, int flock) 2254 { 2255 struct inode *inode = file_inode(file); 2256 struct fuse_conn *fc = get_fuse_conn(inode); 2257 FUSE_ARGS(args); 2258 struct fuse_lk_in inarg; 2259 int opcode = (fl->fl_flags & FL_SLEEP) ? FUSE_SETLKW : FUSE_SETLK; 2260 struct pid *pid = fl->fl_type != F_UNLCK ? task_tgid(current) : NULL; 2261 pid_t pid_nr = pid_nr_ns(pid, fc->pid_ns); 2262 int err; 2263 2264 if (fl->fl_lmops && fl->fl_lmops->lm_grant) { 2265 /* NLM needs asynchronous locks, which we don't support yet */ 2266 return -ENOLCK; 2267 } 2268 2269 /* Unlock on close is handled by the flush method */ 2270 if ((fl->fl_flags & FL_CLOSE_POSIX) == FL_CLOSE_POSIX) 2271 return 0; 2272 2273 fuse_lk_fill(&args, file, fl, opcode, pid_nr, flock, &inarg); 2274 err = fuse_simple_request(fc, &args); 2275 2276 /* locking is restartable */ 2277 if (err == -EINTR) 2278 err = -ERESTARTSYS; 2279 2280 return err; 2281 } 2282 2283 static int fuse_file_lock(struct file *file, int cmd, struct file_lock *fl) 2284 { 2285 struct inode *inode = file_inode(file); 2286 struct fuse_conn *fc = get_fuse_conn(inode); 2287 int err; 2288 2289 if (cmd == F_CANCELLK) { 2290 err = 0; 2291 } else if (cmd == F_GETLK) { 2292 if (fc->no_lock) { 2293 posix_test_lock(file, fl); 2294 err = 0; 2295 } else 2296 err = fuse_getlk(file, fl); 2297 } else { 2298 if (fc->no_lock) 2299 err = posix_lock_file(file, fl, NULL); 2300 else 2301 err = fuse_setlk(file, fl, 0); 2302 } 2303 return err; 2304 } 2305 2306 static int fuse_file_flock(struct file *file, int cmd, struct file_lock *fl) 2307 { 2308 struct inode *inode = file_inode(file); 2309 struct fuse_conn *fc = get_fuse_conn(inode); 2310 int err; 2311 2312 if (fc->no_flock) { 2313 err = locks_lock_file_wait(file, fl); 2314 } else { 2315 struct fuse_file *ff = file->private_data; 2316 2317 /* emulate flock with POSIX locks */ 2318 ff->flock = true; 2319 err = fuse_setlk(file, fl, 1); 2320 } 2321 2322 return err; 2323 } 2324 2325 static sector_t fuse_bmap(struct address_space *mapping, sector_t block) 2326 { 2327 struct inode *inode = mapping->host; 2328 struct fuse_conn *fc = get_fuse_conn(inode); 2329 FUSE_ARGS(args); 2330 struct fuse_bmap_in inarg; 2331 struct fuse_bmap_out outarg; 2332 int err; 2333 2334 if (!inode->i_sb->s_bdev || fc->no_bmap) 2335 return 0; 2336 2337 memset(&inarg, 0, sizeof(inarg)); 2338 inarg.block = block; 2339 inarg.blocksize = inode->i_sb->s_blocksize; 2340 args.in.h.opcode = FUSE_BMAP; 2341 args.in.h.nodeid = get_node_id(inode); 2342 args.in.numargs = 1; 2343 args.in.args[0].size = sizeof(inarg); 2344 args.in.args[0].value = &inarg; 2345 args.out.numargs = 1; 2346 args.out.args[0].size = sizeof(outarg); 2347 args.out.args[0].value = &outarg; 2348 err = fuse_simple_request(fc, &args); 2349 if (err == -ENOSYS) 2350 fc->no_bmap = 1; 2351 2352 return err ? 0 : outarg.block; 2353 } 2354 2355 static loff_t fuse_lseek(struct file *file, loff_t offset, int whence) 2356 { 2357 struct inode *inode = file->f_mapping->host; 2358 struct fuse_conn *fc = get_fuse_conn(inode); 2359 struct fuse_file *ff = file->private_data; 2360 FUSE_ARGS(args); 2361 struct fuse_lseek_in inarg = { 2362 .fh = ff->fh, 2363 .offset = offset, 2364 .whence = whence 2365 }; 2366 struct fuse_lseek_out outarg; 2367 int err; 2368 2369 if (fc->no_lseek) 2370 goto fallback; 2371 2372 args.in.h.opcode = FUSE_LSEEK; 2373 args.in.h.nodeid = ff->nodeid; 2374 args.in.numargs = 1; 2375 args.in.args[0].size = sizeof(inarg); 2376 args.in.args[0].value = &inarg; 2377 args.out.numargs = 1; 2378 args.out.args[0].size = sizeof(outarg); 2379 args.out.args[0].value = &outarg; 2380 err = fuse_simple_request(fc, &args); 2381 if (err) { 2382 if (err == -ENOSYS) { 2383 fc->no_lseek = 1; 2384 goto fallback; 2385 } 2386 return err; 2387 } 2388 2389 return vfs_setpos(file, outarg.offset, inode->i_sb->s_maxbytes); 2390 2391 fallback: 2392 err = fuse_update_attributes(inode, file); 2393 if (!err) 2394 return generic_file_llseek(file, offset, whence); 2395 else 2396 return err; 2397 } 2398 2399 static loff_t fuse_file_llseek(struct file *file, loff_t offset, int whence) 2400 { 2401 loff_t retval; 2402 struct inode *inode = file_inode(file); 2403 2404 switch (whence) { 2405 case SEEK_SET: 2406 case SEEK_CUR: 2407 /* No i_mutex protection necessary for SEEK_CUR and SEEK_SET */ 2408 retval = generic_file_llseek(file, offset, whence); 2409 break; 2410 case SEEK_END: 2411 inode_lock(inode); 2412 retval = fuse_update_attributes(inode, file); 2413 if (!retval) 2414 retval = generic_file_llseek(file, offset, whence); 2415 inode_unlock(inode); 2416 break; 2417 case SEEK_HOLE: 2418 case SEEK_DATA: 2419 inode_lock(inode); 2420 retval = fuse_lseek(file, offset, whence); 2421 inode_unlock(inode); 2422 break; 2423 default: 2424 retval = -EINVAL; 2425 } 2426 2427 return retval; 2428 } 2429 2430 /* 2431 * CUSE servers compiled on 32bit broke on 64bit kernels because the 2432 * ABI was defined to be 'struct iovec' which is different on 32bit 2433 * and 64bit. Fortunately we can determine which structure the server 2434 * used from the size of the reply. 2435 */ 2436 static int fuse_copy_ioctl_iovec_old(struct iovec *dst, void *src, 2437 size_t transferred, unsigned count, 2438 bool is_compat) 2439 { 2440 #ifdef CONFIG_COMPAT 2441 if (count * sizeof(struct compat_iovec) == transferred) { 2442 struct compat_iovec *ciov = src; 2443 unsigned i; 2444 2445 /* 2446 * With this interface a 32bit server cannot support 2447 * non-compat (i.e. ones coming from 64bit apps) ioctl 2448 * requests 2449 */ 2450 if (!is_compat) 2451 return -EINVAL; 2452 2453 for (i = 0; i < count; i++) { 2454 dst[i].iov_base = compat_ptr(ciov[i].iov_base); 2455 dst[i].iov_len = ciov[i].iov_len; 2456 } 2457 return 0; 2458 } 2459 #endif 2460 2461 if (count * sizeof(struct iovec) != transferred) 2462 return -EIO; 2463 2464 memcpy(dst, src, transferred); 2465 return 0; 2466 } 2467 2468 /* Make sure iov_length() won't overflow */ 2469 static int fuse_verify_ioctl_iov(struct fuse_conn *fc, struct iovec *iov, 2470 size_t count) 2471 { 2472 size_t n; 2473 u32 max = fc->max_pages << PAGE_SHIFT; 2474 2475 for (n = 0; n < count; n++, iov++) { 2476 if (iov->iov_len > (size_t) max) 2477 return -ENOMEM; 2478 max -= iov->iov_len; 2479 } 2480 return 0; 2481 } 2482 2483 static int fuse_copy_ioctl_iovec(struct fuse_conn *fc, struct iovec *dst, 2484 void *src, size_t transferred, unsigned count, 2485 bool is_compat) 2486 { 2487 unsigned i; 2488 struct fuse_ioctl_iovec *fiov = src; 2489 2490 if (fc->minor < 16) { 2491 return fuse_copy_ioctl_iovec_old(dst, src, transferred, 2492 count, is_compat); 2493 } 2494 2495 if (count * sizeof(struct fuse_ioctl_iovec) != transferred) 2496 return -EIO; 2497 2498 for (i = 0; i < count; i++) { 2499 /* Did the server supply an inappropriate value? */ 2500 if (fiov[i].base != (unsigned long) fiov[i].base || 2501 fiov[i].len != (unsigned long) fiov[i].len) 2502 return -EIO; 2503 2504 dst[i].iov_base = (void __user *) (unsigned long) fiov[i].base; 2505 dst[i].iov_len = (size_t) fiov[i].len; 2506 2507 #ifdef CONFIG_COMPAT 2508 if (is_compat && 2509 (ptr_to_compat(dst[i].iov_base) != fiov[i].base || 2510 (compat_size_t) dst[i].iov_len != fiov[i].len)) 2511 return -EIO; 2512 #endif 2513 } 2514 2515 return 0; 2516 } 2517 2518 2519 /* 2520 * For ioctls, there is no generic way to determine how much memory 2521 * needs to be read and/or written. Furthermore, ioctls are allowed 2522 * to dereference the passed pointer, so the parameter requires deep 2523 * copying but FUSE has no idea whatsoever about what to copy in or 2524 * out. 2525 * 2526 * This is solved by allowing FUSE server to retry ioctl with 2527 * necessary in/out iovecs. Let's assume the ioctl implementation 2528 * needs to read in the following structure. 2529 * 2530 * struct a { 2531 * char *buf; 2532 * size_t buflen; 2533 * } 2534 * 2535 * On the first callout to FUSE server, inarg->in_size and 2536 * inarg->out_size will be NULL; then, the server completes the ioctl 2537 * with FUSE_IOCTL_RETRY set in out->flags, out->in_iovs set to 1 and 2538 * the actual iov array to 2539 * 2540 * { { .iov_base = inarg.arg, .iov_len = sizeof(struct a) } } 2541 * 2542 * which tells FUSE to copy in the requested area and retry the ioctl. 2543 * On the second round, the server has access to the structure and 2544 * from that it can tell what to look for next, so on the invocation, 2545 * it sets FUSE_IOCTL_RETRY, out->in_iovs to 2 and iov array to 2546 * 2547 * { { .iov_base = inarg.arg, .iov_len = sizeof(struct a) }, 2548 * { .iov_base = a.buf, .iov_len = a.buflen } } 2549 * 2550 * FUSE will copy both struct a and the pointed buffer from the 2551 * process doing the ioctl and retry ioctl with both struct a and the 2552 * buffer. 2553 * 2554 * This time, FUSE server has everything it needs and completes ioctl 2555 * without FUSE_IOCTL_RETRY which finishes the ioctl call. 2556 * 2557 * Copying data out works the same way. 2558 * 2559 * Note that if FUSE_IOCTL_UNRESTRICTED is clear, the kernel 2560 * automatically initializes in and out iovs by decoding @cmd with 2561 * _IOC_* macros and the server is not allowed to request RETRY. This 2562 * limits ioctl data transfers to well-formed ioctls and is the forced 2563 * behavior for all FUSE servers. 2564 */ 2565 long fuse_do_ioctl(struct file *file, unsigned int cmd, unsigned long arg, 2566 unsigned int flags) 2567 { 2568 struct fuse_file *ff = file->private_data; 2569 struct fuse_conn *fc = ff->fc; 2570 struct fuse_ioctl_in inarg = { 2571 .fh = ff->fh, 2572 .cmd = cmd, 2573 .arg = arg, 2574 .flags = flags 2575 }; 2576 struct fuse_ioctl_out outarg; 2577 struct fuse_req *req = NULL; 2578 struct page **pages = NULL; 2579 struct iovec *iov_page = NULL; 2580 struct iovec *in_iov = NULL, *out_iov = NULL; 2581 unsigned int in_iovs = 0, out_iovs = 0, num_pages = 0, max_pages; 2582 size_t in_size, out_size, transferred, c; 2583 int err, i; 2584 struct iov_iter ii; 2585 2586 #if BITS_PER_LONG == 32 2587 inarg.flags |= FUSE_IOCTL_32BIT; 2588 #else 2589 if (flags & FUSE_IOCTL_COMPAT) { 2590 inarg.flags |= FUSE_IOCTL_32BIT; 2591 #ifdef CONFIG_X86_X32 2592 if (in_x32_syscall()) 2593 inarg.flags |= FUSE_IOCTL_COMPAT_X32; 2594 #endif 2595 } 2596 #endif 2597 2598 /* assume all the iovs returned by client always fits in a page */ 2599 BUILD_BUG_ON(sizeof(struct fuse_ioctl_iovec) * FUSE_IOCTL_MAX_IOV > PAGE_SIZE); 2600 2601 err = -ENOMEM; 2602 pages = kcalloc(fc->max_pages, sizeof(pages[0]), GFP_KERNEL); 2603 iov_page = (struct iovec *) __get_free_page(GFP_KERNEL); 2604 if (!pages || !iov_page) 2605 goto out; 2606 2607 /* 2608 * If restricted, initialize IO parameters as encoded in @cmd. 2609 * RETRY from server is not allowed. 2610 */ 2611 if (!(flags & FUSE_IOCTL_UNRESTRICTED)) { 2612 struct iovec *iov = iov_page; 2613 2614 iov->iov_base = (void __user *)arg; 2615 iov->iov_len = _IOC_SIZE(cmd); 2616 2617 if (_IOC_DIR(cmd) & _IOC_WRITE) { 2618 in_iov = iov; 2619 in_iovs = 1; 2620 } 2621 2622 if (_IOC_DIR(cmd) & _IOC_READ) { 2623 out_iov = iov; 2624 out_iovs = 1; 2625 } 2626 } 2627 2628 retry: 2629 inarg.in_size = in_size = iov_length(in_iov, in_iovs); 2630 inarg.out_size = out_size = iov_length(out_iov, out_iovs); 2631 2632 /* 2633 * Out data can be used either for actual out data or iovs, 2634 * make sure there always is at least one page. 2635 */ 2636 out_size = max_t(size_t, out_size, PAGE_SIZE); 2637 max_pages = DIV_ROUND_UP(max(in_size, out_size), PAGE_SIZE); 2638 2639 /* make sure there are enough buffer pages and init request with them */ 2640 err = -ENOMEM; 2641 if (max_pages > fc->max_pages) 2642 goto out; 2643 while (num_pages < max_pages) { 2644 pages[num_pages] = alloc_page(GFP_KERNEL | __GFP_HIGHMEM); 2645 if (!pages[num_pages]) 2646 goto out; 2647 num_pages++; 2648 } 2649 2650 req = fuse_get_req(fc, num_pages); 2651 if (IS_ERR(req)) { 2652 err = PTR_ERR(req); 2653 req = NULL; 2654 goto out; 2655 } 2656 memcpy(req->pages, pages, sizeof(req->pages[0]) * num_pages); 2657 req->num_pages = num_pages; 2658 fuse_page_descs_length_init(req, 0, req->num_pages); 2659 2660 /* okay, let's send it to the client */ 2661 req->in.h.opcode = FUSE_IOCTL; 2662 req->in.h.nodeid = ff->nodeid; 2663 req->in.numargs = 1; 2664 req->in.args[0].size = sizeof(inarg); 2665 req->in.args[0].value = &inarg; 2666 if (in_size) { 2667 req->in.numargs++; 2668 req->in.args[1].size = in_size; 2669 req->in.argpages = 1; 2670 2671 err = -EFAULT; 2672 iov_iter_init(&ii, WRITE, in_iov, in_iovs, in_size); 2673 for (i = 0; iov_iter_count(&ii) && !WARN_ON(i >= num_pages); i++) { 2674 c = copy_page_from_iter(pages[i], 0, PAGE_SIZE, &ii); 2675 if (c != PAGE_SIZE && iov_iter_count(&ii)) 2676 goto out; 2677 } 2678 } 2679 2680 req->out.numargs = 2; 2681 req->out.args[0].size = sizeof(outarg); 2682 req->out.args[0].value = &outarg; 2683 req->out.args[1].size = out_size; 2684 req->out.argpages = 1; 2685 req->out.argvar = 1; 2686 2687 fuse_request_send(fc, req); 2688 err = req->out.h.error; 2689 transferred = req->out.args[1].size; 2690 fuse_put_request(fc, req); 2691 req = NULL; 2692 if (err) 2693 goto out; 2694 2695 /* did it ask for retry? */ 2696 if (outarg.flags & FUSE_IOCTL_RETRY) { 2697 void *vaddr; 2698 2699 /* no retry if in restricted mode */ 2700 err = -EIO; 2701 if (!(flags & FUSE_IOCTL_UNRESTRICTED)) 2702 goto out; 2703 2704 in_iovs = outarg.in_iovs; 2705 out_iovs = outarg.out_iovs; 2706 2707 /* 2708 * Make sure things are in boundary, separate checks 2709 * are to protect against overflow. 2710 */ 2711 err = -ENOMEM; 2712 if (in_iovs > FUSE_IOCTL_MAX_IOV || 2713 out_iovs > FUSE_IOCTL_MAX_IOV || 2714 in_iovs + out_iovs > FUSE_IOCTL_MAX_IOV) 2715 goto out; 2716 2717 vaddr = kmap_atomic(pages[0]); 2718 err = fuse_copy_ioctl_iovec(fc, iov_page, vaddr, 2719 transferred, in_iovs + out_iovs, 2720 (flags & FUSE_IOCTL_COMPAT) != 0); 2721 kunmap_atomic(vaddr); 2722 if (err) 2723 goto out; 2724 2725 in_iov = iov_page; 2726 out_iov = in_iov + in_iovs; 2727 2728 err = fuse_verify_ioctl_iov(fc, in_iov, in_iovs); 2729 if (err) 2730 goto out; 2731 2732 err = fuse_verify_ioctl_iov(fc, out_iov, out_iovs); 2733 if (err) 2734 goto out; 2735 2736 goto retry; 2737 } 2738 2739 err = -EIO; 2740 if (transferred > inarg.out_size) 2741 goto out; 2742 2743 err = -EFAULT; 2744 iov_iter_init(&ii, READ, out_iov, out_iovs, transferred); 2745 for (i = 0; iov_iter_count(&ii) && !WARN_ON(i >= num_pages); i++) { 2746 c = copy_page_to_iter(pages[i], 0, PAGE_SIZE, &ii); 2747 if (c != PAGE_SIZE && iov_iter_count(&ii)) 2748 goto out; 2749 } 2750 err = 0; 2751 out: 2752 if (req) 2753 fuse_put_request(fc, req); 2754 free_page((unsigned long) iov_page); 2755 while (num_pages) 2756 __free_page(pages[--num_pages]); 2757 kfree(pages); 2758 2759 return err ? err : outarg.result; 2760 } 2761 EXPORT_SYMBOL_GPL(fuse_do_ioctl); 2762 2763 long fuse_ioctl_common(struct file *file, unsigned int cmd, 2764 unsigned long arg, unsigned int flags) 2765 { 2766 struct inode *inode = file_inode(file); 2767 struct fuse_conn *fc = get_fuse_conn(inode); 2768 2769 if (!fuse_allow_current_process(fc)) 2770 return -EACCES; 2771 2772 if (is_bad_inode(inode)) 2773 return -EIO; 2774 2775 return fuse_do_ioctl(file, cmd, arg, flags); 2776 } 2777 2778 static long fuse_file_ioctl(struct file *file, unsigned int cmd, 2779 unsigned long arg) 2780 { 2781 return fuse_ioctl_common(file, cmd, arg, 0); 2782 } 2783 2784 static long fuse_file_compat_ioctl(struct file *file, unsigned int cmd, 2785 unsigned long arg) 2786 { 2787 return fuse_ioctl_common(file, cmd, arg, FUSE_IOCTL_COMPAT); 2788 } 2789 2790 /* 2791 * All files which have been polled are linked to RB tree 2792 * fuse_conn->polled_files which is indexed by kh. Walk the tree and 2793 * find the matching one. 2794 */ 2795 static struct rb_node **fuse_find_polled_node(struct fuse_conn *fc, u64 kh, 2796 struct rb_node **parent_out) 2797 { 2798 struct rb_node **link = &fc->polled_files.rb_node; 2799 struct rb_node *last = NULL; 2800 2801 while (*link) { 2802 struct fuse_file *ff; 2803 2804 last = *link; 2805 ff = rb_entry(last, struct fuse_file, polled_node); 2806 2807 if (kh < ff->kh) 2808 link = &last->rb_left; 2809 else if (kh > ff->kh) 2810 link = &last->rb_right; 2811 else 2812 return link; 2813 } 2814 2815 if (parent_out) 2816 *parent_out = last; 2817 return link; 2818 } 2819 2820 /* 2821 * The file is about to be polled. Make sure it's on the polled_files 2822 * RB tree. Note that files once added to the polled_files tree are 2823 * not removed before the file is released. This is because a file 2824 * polled once is likely to be polled again. 2825 */ 2826 static void fuse_register_polled_file(struct fuse_conn *fc, 2827 struct fuse_file *ff) 2828 { 2829 spin_lock(&fc->lock); 2830 if (RB_EMPTY_NODE(&ff->polled_node)) { 2831 struct rb_node **link, *uninitialized_var(parent); 2832 2833 link = fuse_find_polled_node(fc, ff->kh, &parent); 2834 BUG_ON(*link); 2835 rb_link_node(&ff->polled_node, parent, link); 2836 rb_insert_color(&ff->polled_node, &fc->polled_files); 2837 } 2838 spin_unlock(&fc->lock); 2839 } 2840 2841 __poll_t fuse_file_poll(struct file *file, poll_table *wait) 2842 { 2843 struct fuse_file *ff = file->private_data; 2844 struct fuse_conn *fc = ff->fc; 2845 struct fuse_poll_in inarg = { .fh = ff->fh, .kh = ff->kh }; 2846 struct fuse_poll_out outarg; 2847 FUSE_ARGS(args); 2848 int err; 2849 2850 if (fc->no_poll) 2851 return DEFAULT_POLLMASK; 2852 2853 poll_wait(file, &ff->poll_wait, wait); 2854 inarg.events = mangle_poll(poll_requested_events(wait)); 2855 2856 /* 2857 * Ask for notification iff there's someone waiting for it. 2858 * The client may ignore the flag and always notify. 2859 */ 2860 if (waitqueue_active(&ff->poll_wait)) { 2861 inarg.flags |= FUSE_POLL_SCHEDULE_NOTIFY; 2862 fuse_register_polled_file(fc, ff); 2863 } 2864 2865 args.in.h.opcode = FUSE_POLL; 2866 args.in.h.nodeid = ff->nodeid; 2867 args.in.numargs = 1; 2868 args.in.args[0].size = sizeof(inarg); 2869 args.in.args[0].value = &inarg; 2870 args.out.numargs = 1; 2871 args.out.args[0].size = sizeof(outarg); 2872 args.out.args[0].value = &outarg; 2873 err = fuse_simple_request(fc, &args); 2874 2875 if (!err) 2876 return demangle_poll(outarg.revents); 2877 if (err == -ENOSYS) { 2878 fc->no_poll = 1; 2879 return DEFAULT_POLLMASK; 2880 } 2881 return EPOLLERR; 2882 } 2883 EXPORT_SYMBOL_GPL(fuse_file_poll); 2884 2885 /* 2886 * This is called from fuse_handle_notify() on FUSE_NOTIFY_POLL and 2887 * wakes up the poll waiters. 2888 */ 2889 int fuse_notify_poll_wakeup(struct fuse_conn *fc, 2890 struct fuse_notify_poll_wakeup_out *outarg) 2891 { 2892 u64 kh = outarg->kh; 2893 struct rb_node **link; 2894 2895 spin_lock(&fc->lock); 2896 2897 link = fuse_find_polled_node(fc, kh, NULL); 2898 if (*link) { 2899 struct fuse_file *ff; 2900 2901 ff = rb_entry(*link, struct fuse_file, polled_node); 2902 wake_up_interruptible_sync(&ff->poll_wait); 2903 } 2904 2905 spin_unlock(&fc->lock); 2906 return 0; 2907 } 2908 2909 static void fuse_do_truncate(struct file *file) 2910 { 2911 struct inode *inode = file->f_mapping->host; 2912 struct iattr attr; 2913 2914 attr.ia_valid = ATTR_SIZE; 2915 attr.ia_size = i_size_read(inode); 2916 2917 attr.ia_file = file; 2918 attr.ia_valid |= ATTR_FILE; 2919 2920 fuse_do_setattr(file_dentry(file), &attr, file); 2921 } 2922 2923 static inline loff_t fuse_round_up(struct fuse_conn *fc, loff_t off) 2924 { 2925 return round_up(off, fc->max_pages << PAGE_SHIFT); 2926 } 2927 2928 static ssize_t 2929 fuse_direct_IO(struct kiocb *iocb, struct iov_iter *iter) 2930 { 2931 DECLARE_COMPLETION_ONSTACK(wait); 2932 ssize_t ret = 0; 2933 struct file *file = iocb->ki_filp; 2934 struct fuse_file *ff = file->private_data; 2935 bool async_dio = ff->fc->async_dio; 2936 loff_t pos = 0; 2937 struct inode *inode; 2938 loff_t i_size; 2939 size_t count = iov_iter_count(iter); 2940 loff_t offset = iocb->ki_pos; 2941 struct fuse_io_priv *io; 2942 2943 pos = offset; 2944 inode = file->f_mapping->host; 2945 i_size = i_size_read(inode); 2946 2947 if ((iov_iter_rw(iter) == READ) && (offset > i_size)) 2948 return 0; 2949 2950 /* optimization for short read */ 2951 if (async_dio && iov_iter_rw(iter) != WRITE && offset + count > i_size) { 2952 if (offset >= i_size) 2953 return 0; 2954 iov_iter_truncate(iter, fuse_round_up(ff->fc, i_size - offset)); 2955 count = iov_iter_count(iter); 2956 } 2957 2958 io = kmalloc(sizeof(struct fuse_io_priv), GFP_KERNEL); 2959 if (!io) 2960 return -ENOMEM; 2961 spin_lock_init(&io->lock); 2962 kref_init(&io->refcnt); 2963 io->reqs = 1; 2964 io->bytes = -1; 2965 io->size = 0; 2966 io->offset = offset; 2967 io->write = (iov_iter_rw(iter) == WRITE); 2968 io->err = 0; 2969 /* 2970 * By default, we want to optimize all I/Os with async request 2971 * submission to the client filesystem if supported. 2972 */ 2973 io->async = async_dio; 2974 io->iocb = iocb; 2975 io->blocking = is_sync_kiocb(iocb); 2976 2977 /* 2978 * We cannot asynchronously extend the size of a file. 2979 * In such case the aio will behave exactly like sync io. 2980 */ 2981 if ((offset + count > i_size) && iov_iter_rw(iter) == WRITE) 2982 io->blocking = true; 2983 2984 if (io->async && io->blocking) { 2985 /* 2986 * Additional reference to keep io around after 2987 * calling fuse_aio_complete() 2988 */ 2989 kref_get(&io->refcnt); 2990 io->done = &wait; 2991 } 2992 2993 if (iov_iter_rw(iter) == WRITE) { 2994 ret = fuse_direct_io(io, iter, &pos, FUSE_DIO_WRITE); 2995 fuse_invalidate_attr(inode); 2996 } else { 2997 ret = __fuse_direct_read(io, iter, &pos); 2998 } 2999 3000 if (io->async) { 3001 bool blocking = io->blocking; 3002 3003 fuse_aio_complete(io, ret < 0 ? ret : 0, -1); 3004 3005 /* we have a non-extending, async request, so return */ 3006 if (!blocking) 3007 return -EIOCBQUEUED; 3008 3009 wait_for_completion(&wait); 3010 ret = fuse_get_res_by_io(io); 3011 } 3012 3013 kref_put(&io->refcnt, fuse_io_release); 3014 3015 if (iov_iter_rw(iter) == WRITE) { 3016 if (ret > 0) 3017 fuse_write_update_size(inode, pos); 3018 else if (ret < 0 && offset + count > i_size) 3019 fuse_do_truncate(file); 3020 } 3021 3022 return ret; 3023 } 3024 3025 static int fuse_writeback_range(struct inode *inode, loff_t start, loff_t end) 3026 { 3027 int err = filemap_write_and_wait_range(inode->i_mapping, start, end); 3028 3029 if (!err) 3030 fuse_sync_writes(inode); 3031 3032 return err; 3033 } 3034 3035 static long fuse_file_fallocate(struct file *file, int mode, loff_t offset, 3036 loff_t length) 3037 { 3038 struct fuse_file *ff = file->private_data; 3039 struct inode *inode = file_inode(file); 3040 struct fuse_inode *fi = get_fuse_inode(inode); 3041 struct fuse_conn *fc = ff->fc; 3042 FUSE_ARGS(args); 3043 struct fuse_fallocate_in inarg = { 3044 .fh = ff->fh, 3045 .offset = offset, 3046 .length = length, 3047 .mode = mode 3048 }; 3049 int err; 3050 bool lock_inode = !(mode & FALLOC_FL_KEEP_SIZE) || 3051 (mode & FALLOC_FL_PUNCH_HOLE); 3052 3053 if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE)) 3054 return -EOPNOTSUPP; 3055 3056 if (fc->no_fallocate) 3057 return -EOPNOTSUPP; 3058 3059 if (lock_inode) { 3060 inode_lock(inode); 3061 if (mode & FALLOC_FL_PUNCH_HOLE) { 3062 loff_t endbyte = offset + length - 1; 3063 3064 err = fuse_writeback_range(inode, offset, endbyte); 3065 if (err) 3066 goto out; 3067 } 3068 } 3069 3070 if (!(mode & FALLOC_FL_KEEP_SIZE) && 3071 offset + length > i_size_read(inode)) { 3072 err = inode_newsize_ok(inode, offset + length); 3073 if (err) 3074 goto out; 3075 } 3076 3077 if (!(mode & FALLOC_FL_KEEP_SIZE)) 3078 set_bit(FUSE_I_SIZE_UNSTABLE, &fi->state); 3079 3080 args.in.h.opcode = FUSE_FALLOCATE; 3081 args.in.h.nodeid = ff->nodeid; 3082 args.in.numargs = 1; 3083 args.in.args[0].size = sizeof(inarg); 3084 args.in.args[0].value = &inarg; 3085 err = fuse_simple_request(fc, &args); 3086 if (err == -ENOSYS) { 3087 fc->no_fallocate = 1; 3088 err = -EOPNOTSUPP; 3089 } 3090 if (err) 3091 goto out; 3092 3093 /* we could have extended the file */ 3094 if (!(mode & FALLOC_FL_KEEP_SIZE)) { 3095 bool changed = fuse_write_update_size(inode, offset + length); 3096 3097 if (changed && fc->writeback_cache) 3098 file_update_time(file); 3099 } 3100 3101 if (mode & FALLOC_FL_PUNCH_HOLE) 3102 truncate_pagecache_range(inode, offset, offset + length - 1); 3103 3104 fuse_invalidate_attr(inode); 3105 3106 out: 3107 if (!(mode & FALLOC_FL_KEEP_SIZE)) 3108 clear_bit(FUSE_I_SIZE_UNSTABLE, &fi->state); 3109 3110 if (lock_inode) 3111 inode_unlock(inode); 3112 3113 return err; 3114 } 3115 3116 static ssize_t fuse_copy_file_range(struct file *file_in, loff_t pos_in, 3117 struct file *file_out, loff_t pos_out, 3118 size_t len, unsigned int flags) 3119 { 3120 struct fuse_file *ff_in = file_in->private_data; 3121 struct fuse_file *ff_out = file_out->private_data; 3122 struct inode *inode_in = file_inode(file_in); 3123 struct inode *inode_out = file_inode(file_out); 3124 struct fuse_inode *fi_out = get_fuse_inode(inode_out); 3125 struct fuse_conn *fc = ff_in->fc; 3126 FUSE_ARGS(args); 3127 struct fuse_copy_file_range_in inarg = { 3128 .fh_in = ff_in->fh, 3129 .off_in = pos_in, 3130 .nodeid_out = ff_out->nodeid, 3131 .fh_out = ff_out->fh, 3132 .off_out = pos_out, 3133 .len = len, 3134 .flags = flags 3135 }; 3136 struct fuse_write_out outarg; 3137 ssize_t err; 3138 /* mark unstable when write-back is not used, and file_out gets 3139 * extended */ 3140 bool is_unstable = (!fc->writeback_cache) && 3141 ((pos_out + len) > inode_out->i_size); 3142 3143 if (fc->no_copy_file_range) 3144 return -EOPNOTSUPP; 3145 3146 if (fc->writeback_cache) { 3147 inode_lock(inode_in); 3148 err = fuse_writeback_range(inode_in, pos_in, pos_in + len); 3149 inode_unlock(inode_in); 3150 if (err) 3151 return err; 3152 } 3153 3154 inode_lock(inode_out); 3155 3156 if (fc->writeback_cache) { 3157 err = fuse_writeback_range(inode_out, pos_out, pos_out + len); 3158 if (err) 3159 goto out; 3160 } 3161 3162 if (is_unstable) 3163 set_bit(FUSE_I_SIZE_UNSTABLE, &fi_out->state); 3164 3165 args.in.h.opcode = FUSE_COPY_FILE_RANGE; 3166 args.in.h.nodeid = ff_in->nodeid; 3167 args.in.numargs = 1; 3168 args.in.args[0].size = sizeof(inarg); 3169 args.in.args[0].value = &inarg; 3170 args.out.numargs = 1; 3171 args.out.args[0].size = sizeof(outarg); 3172 args.out.args[0].value = &outarg; 3173 err = fuse_simple_request(fc, &args); 3174 if (err == -ENOSYS) { 3175 fc->no_copy_file_range = 1; 3176 err = -EOPNOTSUPP; 3177 } 3178 if (err) 3179 goto out; 3180 3181 if (fc->writeback_cache) { 3182 fuse_write_update_size(inode_out, pos_out + outarg.size); 3183 file_update_time(file_out); 3184 } 3185 3186 fuse_invalidate_attr(inode_out); 3187 3188 err = outarg.size; 3189 out: 3190 if (is_unstable) 3191 clear_bit(FUSE_I_SIZE_UNSTABLE, &fi_out->state); 3192 3193 inode_unlock(inode_out); 3194 3195 return err; 3196 } 3197 3198 static const struct file_operations fuse_file_operations = { 3199 .llseek = fuse_file_llseek, 3200 .read_iter = fuse_file_read_iter, 3201 .write_iter = fuse_file_write_iter, 3202 .mmap = fuse_file_mmap, 3203 .open = fuse_open, 3204 .flush = fuse_flush, 3205 .release = fuse_release, 3206 .fsync = fuse_fsync, 3207 .lock = fuse_file_lock, 3208 .flock = fuse_file_flock, 3209 .splice_read = generic_file_splice_read, 3210 .splice_write = iter_file_splice_write, 3211 .unlocked_ioctl = fuse_file_ioctl, 3212 .compat_ioctl = fuse_file_compat_ioctl, 3213 .poll = fuse_file_poll, 3214 .fallocate = fuse_file_fallocate, 3215 .copy_file_range = fuse_copy_file_range, 3216 }; 3217 3218 static const struct address_space_operations fuse_file_aops = { 3219 .readpage = fuse_readpage, 3220 .writepage = fuse_writepage, 3221 .writepages = fuse_writepages, 3222 .launder_page = fuse_launder_page, 3223 .readpages = fuse_readpages, 3224 .set_page_dirty = __set_page_dirty_nobuffers, 3225 .bmap = fuse_bmap, 3226 .direct_IO = fuse_direct_IO, 3227 .write_begin = fuse_write_begin, 3228 .write_end = fuse_write_end, 3229 }; 3230 3231 void fuse_init_file_inode(struct inode *inode) 3232 { 3233 struct fuse_inode *fi = get_fuse_inode(inode); 3234 3235 inode->i_fop = &fuse_file_operations; 3236 inode->i_data.a_ops = &fuse_file_aops; 3237 3238 INIT_LIST_HEAD(&fi->write_files); 3239 INIT_LIST_HEAD(&fi->queued_writes); 3240 fi->writectr = 0; 3241 init_waitqueue_head(&fi->page_waitq); 3242 INIT_LIST_HEAD(&fi->writepages); 3243 } 3244
Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.