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