diff options
Diffstat (limited to 'fs/fuse/file.c')
-rw-r--r-- | fs/fuse/file.c | 633 |
1 files changed, 591 insertions, 42 deletions
diff --git a/fs/fuse/file.c b/fs/fuse/file.c index 676b0bc8a86d..f28cf8b46f80 100644 --- a/fs/fuse/file.c +++ b/fs/fuse/file.c | |||
@@ -210,6 +210,49 @@ u64 fuse_lock_owner_id(struct fuse_conn *fc, fl_owner_t id) | |||
210 | return (u64) v0 + ((u64) v1 << 32); | 210 | return (u64) v0 + ((u64) v1 << 32); |
211 | } | 211 | } |
212 | 212 | ||
213 | /* | ||
214 | * Check if page is under writeback | ||
215 | * | ||
216 | * This is currently done by walking the list of writepage requests | ||
217 | * for the inode, which can be pretty inefficient. | ||
218 | */ | ||
219 | static bool fuse_page_is_writeback(struct inode *inode, pgoff_t index) | ||
220 | { | ||
221 | struct fuse_conn *fc = get_fuse_conn(inode); | ||
222 | struct fuse_inode *fi = get_fuse_inode(inode); | ||
223 | struct fuse_req *req; | ||
224 | bool found = false; | ||
225 | |||
226 | spin_lock(&fc->lock); | ||
227 | list_for_each_entry(req, &fi->writepages, writepages_entry) { | ||
228 | pgoff_t curr_index; | ||
229 | |||
230 | BUG_ON(req->inode != inode); | ||
231 | curr_index = req->misc.write.in.offset >> PAGE_CACHE_SHIFT; | ||
232 | if (curr_index == index) { | ||
233 | found = true; | ||
234 | break; | ||
235 | } | ||
236 | } | ||
237 | spin_unlock(&fc->lock); | ||
238 | |||
239 | return found; | ||
240 | } | ||
241 | |||
242 | /* | ||
243 | * Wait for page writeback to be completed. | ||
244 | * | ||
245 | * Since fuse doesn't rely on the VM writeback tracking, this has to | ||
246 | * use some other means. | ||
247 | */ | ||
248 | static int fuse_wait_on_page_writeback(struct inode *inode, pgoff_t index) | ||
249 | { | ||
250 | struct fuse_inode *fi = get_fuse_inode(inode); | ||
251 | |||
252 | wait_event(fi->page_waitq, !fuse_page_is_writeback(inode, index)); | ||
253 | return 0; | ||
254 | } | ||
255 | |||
213 | static int fuse_flush(struct file *file, fl_owner_t id) | 256 | static int fuse_flush(struct file *file, fl_owner_t id) |
214 | { | 257 | { |
215 | struct inode *inode = file->f_path.dentry->d_inode; | 258 | struct inode *inode = file->f_path.dentry->d_inode; |
@@ -245,6 +288,21 @@ static int fuse_flush(struct file *file, fl_owner_t id) | |||
245 | return err; | 288 | return err; |
246 | } | 289 | } |
247 | 290 | ||
291 | /* | ||
292 | * Wait for all pending writepages on the inode to finish. | ||
293 | * | ||
294 | * This is currently done by blocking further writes with FUSE_NOWRITE | ||
295 | * and waiting for all sent writes to complete. | ||
296 | * | ||
297 | * This must be called under i_mutex, otherwise the FUSE_NOWRITE usage | ||
298 | * could conflict with truncation. | ||
299 | */ | ||
300 | static void fuse_sync_writes(struct inode *inode) | ||
301 | { | ||
302 | fuse_set_nowrite(inode); | ||
303 | fuse_release_nowrite(inode); | ||
304 | } | ||
305 | |||
248 | int fuse_fsync_common(struct file *file, struct dentry *de, int datasync, | 306 | int fuse_fsync_common(struct file *file, struct dentry *de, int datasync, |
249 | int isdir) | 307 | int isdir) |
250 | { | 308 | { |
@@ -261,6 +319,17 @@ int fuse_fsync_common(struct file *file, struct dentry *de, int datasync, | |||
261 | if ((!isdir && fc->no_fsync) || (isdir && fc->no_fsyncdir)) | 319 | if ((!isdir && fc->no_fsync) || (isdir && fc->no_fsyncdir)) |
262 | return 0; | 320 | return 0; |
263 | 321 | ||
322 | /* | ||
323 | * Start writeback against all dirty pages of the inode, then | ||
324 | * wait for all outstanding writes, before sending the FSYNC | ||
325 | * request. | ||
326 | */ | ||
327 | err = write_inode_now(inode, 0); | ||
328 | if (err) | ||
329 | return err; | ||
330 | |||
331 | fuse_sync_writes(inode); | ||
332 | |||
264 | req = fuse_get_req(fc); | 333 | req = fuse_get_req(fc); |
265 | if (IS_ERR(req)) | 334 | if (IS_ERR(req)) |
266 | return PTR_ERR(req); | 335 | return PTR_ERR(req); |
@@ -294,7 +363,7 @@ static int fuse_fsync(struct file *file, struct dentry *de, int datasync) | |||
294 | void fuse_read_fill(struct fuse_req *req, struct file *file, | 363 | void fuse_read_fill(struct fuse_req *req, struct file *file, |
295 | struct inode *inode, loff_t pos, size_t count, int opcode) | 364 | struct inode *inode, loff_t pos, size_t count, int opcode) |
296 | { | 365 | { |
297 | struct fuse_read_in *inarg = &req->misc.read_in; | 366 | struct fuse_read_in *inarg = &req->misc.read.in; |
298 | struct fuse_file *ff = file->private_data; | 367 | struct fuse_file *ff = file->private_data; |
299 | 368 | ||
300 | inarg->fh = ff->fh; | 369 | inarg->fh = ff->fh; |
@@ -320,7 +389,7 @@ static size_t fuse_send_read(struct fuse_req *req, struct file *file, | |||
320 | 389 | ||
321 | fuse_read_fill(req, file, inode, pos, count, FUSE_READ); | 390 | fuse_read_fill(req, file, inode, pos, count, FUSE_READ); |
322 | if (owner != NULL) { | 391 | if (owner != NULL) { |
323 | struct fuse_read_in *inarg = &req->misc.read_in; | 392 | struct fuse_read_in *inarg = &req->misc.read.in; |
324 | 393 | ||
325 | inarg->read_flags |= FUSE_READ_LOCKOWNER; | 394 | inarg->read_flags |= FUSE_READ_LOCKOWNER; |
326 | inarg->lock_owner = fuse_lock_owner_id(fc, owner); | 395 | inarg->lock_owner = fuse_lock_owner_id(fc, owner); |
@@ -329,31 +398,66 @@ static size_t fuse_send_read(struct fuse_req *req, struct file *file, | |||
329 | return req->out.args[0].size; | 398 | return req->out.args[0].size; |
330 | } | 399 | } |
331 | 400 | ||
401 | static void fuse_read_update_size(struct inode *inode, loff_t size, | ||
402 | u64 attr_ver) | ||
403 | { | ||
404 | struct fuse_conn *fc = get_fuse_conn(inode); | ||
405 | struct fuse_inode *fi = get_fuse_inode(inode); | ||
406 | |||
407 | spin_lock(&fc->lock); | ||
408 | if (attr_ver == fi->attr_version && size < inode->i_size) { | ||
409 | fi->attr_version = ++fc->attr_version; | ||
410 | i_size_write(inode, size); | ||
411 | } | ||
412 | spin_unlock(&fc->lock); | ||
413 | } | ||
414 | |||
332 | static int fuse_readpage(struct file *file, struct page *page) | 415 | static int fuse_readpage(struct file *file, struct page *page) |
333 | { | 416 | { |
334 | struct inode *inode = page->mapping->host; | 417 | struct inode *inode = page->mapping->host; |
335 | struct fuse_conn *fc = get_fuse_conn(inode); | 418 | struct fuse_conn *fc = get_fuse_conn(inode); |
336 | struct fuse_req *req; | 419 | struct fuse_req *req; |
420 | size_t num_read; | ||
421 | loff_t pos = page_offset(page); | ||
422 | size_t count = PAGE_CACHE_SIZE; | ||
423 | u64 attr_ver; | ||
337 | int err; | 424 | int err; |
338 | 425 | ||
339 | err = -EIO; | 426 | err = -EIO; |
340 | if (is_bad_inode(inode)) | 427 | if (is_bad_inode(inode)) |
341 | goto out; | 428 | goto out; |
342 | 429 | ||
430 | /* | ||
431 | * Page writeback can extend beyond the liftime of the | ||
432 | * page-cache page, so make sure we read a properly synced | ||
433 | * page. | ||
434 | */ | ||
435 | fuse_wait_on_page_writeback(inode, page->index); | ||
436 | |||
343 | req = fuse_get_req(fc); | 437 | req = fuse_get_req(fc); |
344 | err = PTR_ERR(req); | 438 | err = PTR_ERR(req); |
345 | if (IS_ERR(req)) | 439 | if (IS_ERR(req)) |
346 | goto out; | 440 | goto out; |
347 | 441 | ||
442 | attr_ver = fuse_get_attr_version(fc); | ||
443 | |||
348 | req->out.page_zeroing = 1; | 444 | req->out.page_zeroing = 1; |
349 | req->num_pages = 1; | 445 | req->num_pages = 1; |
350 | req->pages[0] = page; | 446 | req->pages[0] = page; |
351 | fuse_send_read(req, file, inode, page_offset(page), PAGE_CACHE_SIZE, | 447 | num_read = fuse_send_read(req, file, inode, pos, count, NULL); |
352 | NULL); | ||
353 | err = req->out.h.error; | 448 | err = req->out.h.error; |
354 | fuse_put_request(fc, req); | 449 | fuse_put_request(fc, req); |
355 | if (!err) | 450 | |
451 | if (!err) { | ||
452 | /* | ||
453 | * Short read means EOF. If file size is larger, truncate it | ||
454 | */ | ||
455 | if (num_read < count) | ||
456 | fuse_read_update_size(inode, pos + num_read, attr_ver); | ||
457 | |||
356 | SetPageUptodate(page); | 458 | SetPageUptodate(page); |
459 | } | ||
460 | |||
357 | fuse_invalidate_attr(inode); /* atime changed */ | 461 | fuse_invalidate_attr(inode); /* atime changed */ |
358 | out: | 462 | out: |
359 | unlock_page(page); | 463 | unlock_page(page); |
@@ -363,8 +467,19 @@ static int fuse_readpage(struct file *file, struct page *page) | |||
363 | static void fuse_readpages_end(struct fuse_conn *fc, struct fuse_req *req) | 467 | static void fuse_readpages_end(struct fuse_conn *fc, struct fuse_req *req) |
364 | { | 468 | { |
365 | int i; | 469 | int i; |
470 | size_t count = req->misc.read.in.size; | ||
471 | size_t num_read = req->out.args[0].size; | ||
472 | struct inode *inode = req->pages[0]->mapping->host; | ||
473 | |||
474 | /* | ||
475 | * Short read means EOF. If file size is larger, truncate it | ||
476 | */ | ||
477 | if (!req->out.h.error && num_read < count) { | ||
478 | loff_t pos = page_offset(req->pages[0]) + num_read; | ||
479 | fuse_read_update_size(inode, pos, req->misc.read.attr_ver); | ||
480 | } | ||
366 | 481 | ||
367 | fuse_invalidate_attr(req->pages[0]->mapping->host); /* atime changed */ | 482 | fuse_invalidate_attr(inode); /* atime changed */ |
368 | 483 | ||
369 | for (i = 0; i < req->num_pages; i++) { | 484 | for (i = 0; i < req->num_pages; i++) { |
370 | struct page *page = req->pages[i]; | 485 | struct page *page = req->pages[i]; |
@@ -387,6 +502,7 @@ static void fuse_send_readpages(struct fuse_req *req, struct file *file, | |||
387 | size_t count = req->num_pages << PAGE_CACHE_SHIFT; | 502 | size_t count = req->num_pages << PAGE_CACHE_SHIFT; |
388 | req->out.page_zeroing = 1; | 503 | req->out.page_zeroing = 1; |
389 | fuse_read_fill(req, file, inode, pos, count, FUSE_READ); | 504 | fuse_read_fill(req, file, inode, pos, count, FUSE_READ); |
505 | req->misc.read.attr_ver = fuse_get_attr_version(fc); | ||
390 | if (fc->async_read) { | 506 | if (fc->async_read) { |
391 | struct fuse_file *ff = file->private_data; | 507 | struct fuse_file *ff = file->private_data; |
392 | req->ff = fuse_file_get(ff); | 508 | req->ff = fuse_file_get(ff); |
@@ -411,6 +527,8 @@ static int fuse_readpages_fill(void *_data, struct page *page) | |||
411 | struct inode *inode = data->inode; | 527 | struct inode *inode = data->inode; |
412 | struct fuse_conn *fc = get_fuse_conn(inode); | 528 | struct fuse_conn *fc = get_fuse_conn(inode); |
413 | 529 | ||
530 | fuse_wait_on_page_writeback(inode, page->index); | ||
531 | |||
414 | if (req->num_pages && | 532 | if (req->num_pages && |
415 | (req->num_pages == FUSE_MAX_PAGES_PER_REQ || | 533 | (req->num_pages == FUSE_MAX_PAGES_PER_REQ || |
416 | (req->num_pages + 1) * PAGE_CACHE_SIZE > fc->max_read || | 534 | (req->num_pages + 1) * PAGE_CACHE_SIZE > fc->max_read || |
@@ -477,11 +595,10 @@ static ssize_t fuse_file_aio_read(struct kiocb *iocb, const struct iovec *iov, | |||
477 | } | 595 | } |
478 | 596 | ||
479 | static void fuse_write_fill(struct fuse_req *req, struct file *file, | 597 | static void fuse_write_fill(struct fuse_req *req, struct file *file, |
480 | struct inode *inode, loff_t pos, size_t count, | 598 | struct fuse_file *ff, struct inode *inode, |
481 | int writepage) | 599 | loff_t pos, size_t count, int writepage) |
482 | { | 600 | { |
483 | struct fuse_conn *fc = get_fuse_conn(inode); | 601 | struct fuse_conn *fc = get_fuse_conn(inode); |
484 | struct fuse_file *ff = file->private_data; | ||
485 | struct fuse_write_in *inarg = &req->misc.write.in; | 602 | struct fuse_write_in *inarg = &req->misc.write.in; |
486 | struct fuse_write_out *outarg = &req->misc.write.out; | 603 | struct fuse_write_out *outarg = &req->misc.write.out; |
487 | 604 | ||
@@ -490,7 +607,7 @@ static void fuse_write_fill(struct fuse_req *req, struct file *file, | |||
490 | inarg->offset = pos; | 607 | inarg->offset = pos; |
491 | inarg->size = count; | 608 | inarg->size = count; |
492 | inarg->write_flags = writepage ? FUSE_WRITE_CACHE : 0; | 609 | inarg->write_flags = writepage ? FUSE_WRITE_CACHE : 0; |
493 | inarg->flags = file->f_flags; | 610 | inarg->flags = file ? file->f_flags : 0; |
494 | req->in.h.opcode = FUSE_WRITE; | 611 | req->in.h.opcode = FUSE_WRITE; |
495 | req->in.h.nodeid = get_node_id(inode); | 612 | req->in.h.nodeid = get_node_id(inode); |
496 | req->in.argpages = 1; | 613 | req->in.argpages = 1; |
@@ -511,7 +628,7 @@ static size_t fuse_send_write(struct fuse_req *req, struct file *file, | |||
511 | fl_owner_t owner) | 628 | fl_owner_t owner) |
512 | { | 629 | { |
513 | struct fuse_conn *fc = get_fuse_conn(inode); | 630 | struct fuse_conn *fc = get_fuse_conn(inode); |
514 | fuse_write_fill(req, file, inode, pos, count, 0); | 631 | fuse_write_fill(req, file, file->private_data, inode, pos, count, 0); |
515 | if (owner != NULL) { | 632 | if (owner != NULL) { |
516 | struct fuse_write_in *inarg = &req->misc.write.in; | 633 | struct fuse_write_in *inarg = &req->misc.write.in; |
517 | inarg->write_flags |= FUSE_WRITE_LOCKOWNER; | 634 | inarg->write_flags |= FUSE_WRITE_LOCKOWNER; |
@@ -533,19 +650,36 @@ static int fuse_write_begin(struct file *file, struct address_space *mapping, | |||
533 | return 0; | 650 | return 0; |
534 | } | 651 | } |
535 | 652 | ||
653 | static void fuse_write_update_size(struct inode *inode, loff_t pos) | ||
654 | { | ||
655 | struct fuse_conn *fc = get_fuse_conn(inode); | ||
656 | struct fuse_inode *fi = get_fuse_inode(inode); | ||
657 | |||
658 | spin_lock(&fc->lock); | ||
659 | fi->attr_version = ++fc->attr_version; | ||
660 | if (pos > inode->i_size) | ||
661 | i_size_write(inode, pos); | ||
662 | spin_unlock(&fc->lock); | ||
663 | } | ||
664 | |||
536 | static int fuse_buffered_write(struct file *file, struct inode *inode, | 665 | static int fuse_buffered_write(struct file *file, struct inode *inode, |
537 | loff_t pos, unsigned count, struct page *page) | 666 | loff_t pos, unsigned count, struct page *page) |
538 | { | 667 | { |
539 | int err; | 668 | int err; |
540 | size_t nres; | 669 | size_t nres; |
541 | struct fuse_conn *fc = get_fuse_conn(inode); | 670 | struct fuse_conn *fc = get_fuse_conn(inode); |
542 | struct fuse_inode *fi = get_fuse_inode(inode); | ||
543 | unsigned offset = pos & (PAGE_CACHE_SIZE - 1); | 671 | unsigned offset = pos & (PAGE_CACHE_SIZE - 1); |
544 | struct fuse_req *req; | 672 | struct fuse_req *req; |
545 | 673 | ||
546 | if (is_bad_inode(inode)) | 674 | if (is_bad_inode(inode)) |
547 | return -EIO; | 675 | return -EIO; |
548 | 676 | ||
677 | /* | ||
678 | * Make sure writepages on the same page are not mixed up with | ||
679 | * plain writes. | ||
680 | */ | ||
681 | fuse_wait_on_page_writeback(inode, page->index); | ||
682 | |||
549 | req = fuse_get_req(fc); | 683 | req = fuse_get_req(fc); |
550 | if (IS_ERR(req)) | 684 | if (IS_ERR(req)) |
551 | return PTR_ERR(req); | 685 | return PTR_ERR(req); |
@@ -560,12 +694,7 @@ static int fuse_buffered_write(struct file *file, struct inode *inode, | |||
560 | err = -EIO; | 694 | err = -EIO; |
561 | if (!err) { | 695 | if (!err) { |
562 | pos += nres; | 696 | pos += nres; |
563 | spin_lock(&fc->lock); | 697 | fuse_write_update_size(inode, pos); |
564 | fi->attr_version = ++fc->attr_version; | ||
565 | if (pos > inode->i_size) | ||
566 | i_size_write(inode, pos); | ||
567 | spin_unlock(&fc->lock); | ||
568 | |||
569 | if (count == PAGE_CACHE_SIZE) | 698 | if (count == PAGE_CACHE_SIZE) |
570 | SetPageUptodate(page); | 699 | SetPageUptodate(page); |
571 | } | 700 | } |
@@ -588,6 +717,198 @@ static int fuse_write_end(struct file *file, struct address_space *mapping, | |||
588 | return res; | 717 | return res; |
589 | } | 718 | } |
590 | 719 | ||
720 | static size_t fuse_send_write_pages(struct fuse_req *req, struct file *file, | ||
721 | struct inode *inode, loff_t pos, | ||
722 | size_t count) | ||
723 | { | ||
724 | size_t res; | ||
725 | unsigned offset; | ||
726 | unsigned i; | ||
727 | |||
728 | for (i = 0; i < req->num_pages; i++) | ||
729 | fuse_wait_on_page_writeback(inode, req->pages[i]->index); | ||
730 | |||
731 | res = fuse_send_write(req, file, inode, pos, count, NULL); | ||
732 | |||
733 | offset = req->page_offset; | ||
734 | count = res; | ||
735 | for (i = 0; i < req->num_pages; i++) { | ||
736 | struct page *page = req->pages[i]; | ||
737 | |||
738 | if (!req->out.h.error && !offset && count >= PAGE_CACHE_SIZE) | ||
739 | SetPageUptodate(page); | ||
740 | |||
741 | if (count > PAGE_CACHE_SIZE - offset) | ||
742 | count -= PAGE_CACHE_SIZE - offset; | ||
743 | else | ||
744 | count = 0; | ||
745 | offset = 0; | ||
746 | |||
747 | unlock_page(page); | ||
748 | page_cache_release(page); | ||
749 | } | ||
750 | |||
751 | return res; | ||
752 | } | ||
753 | |||
754 | static ssize_t fuse_fill_write_pages(struct fuse_req *req, | ||
755 | struct address_space *mapping, | ||
756 | struct iov_iter *ii, loff_t pos) | ||
757 | { | ||
758 | struct fuse_conn *fc = get_fuse_conn(mapping->host); | ||
759 | unsigned offset = pos & (PAGE_CACHE_SIZE - 1); | ||
760 | size_t count = 0; | ||
761 | int err; | ||
762 | |||
763 | req->page_offset = offset; | ||
764 | |||
765 | do { | ||
766 | size_t tmp; | ||
767 | struct page *page; | ||
768 | pgoff_t index = pos >> PAGE_CACHE_SHIFT; | ||
769 | size_t bytes = min_t(size_t, PAGE_CACHE_SIZE - offset, | ||
770 | iov_iter_count(ii)); | ||
771 | |||
772 | bytes = min_t(size_t, bytes, fc->max_write - count); | ||
773 | |||
774 | again: | ||
775 | err = -EFAULT; | ||
776 | if (iov_iter_fault_in_readable(ii, bytes)) | ||
777 | break; | ||
778 | |||
779 | err = -ENOMEM; | ||
780 | page = __grab_cache_page(mapping, index); | ||
781 | if (!page) | ||
782 | break; | ||
783 | |||
784 | pagefault_disable(); | ||
785 | tmp = iov_iter_copy_from_user_atomic(page, ii, offset, bytes); | ||
786 | pagefault_enable(); | ||
787 | flush_dcache_page(page); | ||
788 | |||
789 | if (!tmp) { | ||
790 | unlock_page(page); | ||
791 | page_cache_release(page); | ||
792 | bytes = min(bytes, iov_iter_single_seg_count(ii)); | ||
793 | goto again; | ||
794 | } | ||
795 | |||
796 | err = 0; | ||
797 | req->pages[req->num_pages] = page; | ||
798 | req->num_pages++; | ||
799 | |||
800 | iov_iter_advance(ii, tmp); | ||
801 | count += tmp; | ||
802 | pos += tmp; | ||
803 | offset += tmp; | ||
804 | if (offset == PAGE_CACHE_SIZE) | ||
805 | offset = 0; | ||
806 | |||
807 | } while (iov_iter_count(ii) && count < fc->max_write && | ||
808 | req->num_pages < FUSE_MAX_PAGES_PER_REQ && offset == 0); | ||
809 | |||
810 | return count > 0 ? count : err; | ||
811 | } | ||
812 | |||
813 | static ssize_t fuse_perform_write(struct file *file, | ||
814 | struct address_space *mapping, | ||
815 | struct iov_iter *ii, loff_t pos) | ||
816 | { | ||
817 | struct inode *inode = mapping->host; | ||
818 | struct fuse_conn *fc = get_fuse_conn(inode); | ||
819 | int err = 0; | ||
820 | ssize_t res = 0; | ||
821 | |||
822 | if (is_bad_inode(inode)) | ||
823 | return -EIO; | ||
824 | |||
825 | do { | ||
826 | struct fuse_req *req; | ||
827 | ssize_t count; | ||
828 | |||
829 | req = fuse_get_req(fc); | ||
830 | if (IS_ERR(req)) { | ||
831 | err = PTR_ERR(req); | ||
832 | break; | ||
833 | } | ||
834 | |||
835 | count = fuse_fill_write_pages(req, mapping, ii, pos); | ||
836 | if (count <= 0) { | ||
837 | err = count; | ||
838 | } else { | ||
839 | size_t num_written; | ||
840 | |||
841 | num_written = fuse_send_write_pages(req, file, inode, | ||
842 | pos, count); | ||
843 | err = req->out.h.error; | ||
844 | if (!err) { | ||
845 | res += num_written; | ||
846 | pos += num_written; | ||
847 | |||
848 | /* break out of the loop on short write */ | ||
849 | if (num_written != count) | ||
850 | err = -EIO; | ||
851 | } | ||
852 | } | ||
853 | fuse_put_request(fc, req); | ||
854 | } while (!err && iov_iter_count(ii)); | ||
855 | |||
856 | if (res > 0) | ||
857 | fuse_write_update_size(inode, pos); | ||
858 | |||
859 | fuse_invalidate_attr(inode); | ||
860 | |||
861 | return res > 0 ? res : err; | ||
862 | } | ||
863 | |||
864 | static ssize_t fuse_file_aio_write(struct kiocb *iocb, const struct iovec *iov, | ||
865 | unsigned long nr_segs, loff_t pos) | ||
866 | { | ||
867 | struct file *file = iocb->ki_filp; | ||
868 | struct address_space *mapping = file->f_mapping; | ||
869 | size_t count = 0; | ||
870 | ssize_t written = 0; | ||
871 | struct inode *inode = mapping->host; | ||
872 | ssize_t err; | ||
873 | struct iov_iter i; | ||
874 | |||
875 | WARN_ON(iocb->ki_pos != pos); | ||
876 | |||
877 | err = generic_segment_checks(iov, &nr_segs, &count, VERIFY_READ); | ||
878 | if (err) | ||
879 | return err; | ||
880 | |||
881 | mutex_lock(&inode->i_mutex); | ||
882 | vfs_check_frozen(inode->i_sb, SB_FREEZE_WRITE); | ||
883 | |||
884 | /* We can write back this queue in page reclaim */ | ||
885 | current->backing_dev_info = mapping->backing_dev_info; | ||
886 | |||
887 | err = generic_write_checks(file, &pos, &count, S_ISBLK(inode->i_mode)); | ||
888 | if (err) | ||
889 | goto out; | ||
890 | |||
891 | if (count == 0) | ||
892 | goto out; | ||
893 | |||
894 | err = remove_suid(file->f_path.dentry); | ||
895 | if (err) | ||
896 | goto out; | ||
897 | |||
898 | file_update_time(file); | ||
899 | |||
900 | iov_iter_init(&i, iov, nr_segs, count, 0); | ||
901 | written = fuse_perform_write(file, mapping, &i, pos); | ||
902 | if (written >= 0) | ||
903 | iocb->ki_pos = pos + written; | ||
904 | |||
905 | out: | ||
906 | current->backing_dev_info = NULL; | ||
907 | mutex_unlock(&inode->i_mutex); | ||
908 | |||
909 | return written ? written : err; | ||
910 | } | ||
911 | |||
591 | static void fuse_release_user_pages(struct fuse_req *req, int write) | 912 | static void fuse_release_user_pages(struct fuse_req *req, int write) |
592 | { | 913 | { |
593 | unsigned i; | 914 | unsigned i; |
@@ -613,7 +934,7 @@ static int fuse_get_user_pages(struct fuse_req *req, const char __user *buf, | |||
613 | 934 | ||
614 | nbytes = min(nbytes, (unsigned) FUSE_MAX_PAGES_PER_REQ << PAGE_SHIFT); | 935 | nbytes = min(nbytes, (unsigned) FUSE_MAX_PAGES_PER_REQ << PAGE_SHIFT); |
615 | npages = (nbytes + offset + PAGE_SIZE - 1) >> PAGE_SHIFT; | 936 | npages = (nbytes + offset + PAGE_SIZE - 1) >> PAGE_SHIFT; |
616 | npages = min(max(npages, 1), FUSE_MAX_PAGES_PER_REQ); | 937 | npages = clamp(npages, 1, FUSE_MAX_PAGES_PER_REQ); |
617 | down_read(¤t->mm->mmap_sem); | 938 | down_read(¤t->mm->mmap_sem); |
618 | npages = get_user_pages(current, current->mm, user_addr, npages, write, | 939 | npages = get_user_pages(current, current->mm, user_addr, npages, write, |
619 | 0, req->pages, NULL); | 940 | 0, req->pages, NULL); |
@@ -645,14 +966,15 @@ static ssize_t fuse_direct_io(struct file *file, const char __user *buf, | |||
645 | 966 | ||
646 | while (count) { | 967 | while (count) { |
647 | size_t nres; | 968 | size_t nres; |
648 | size_t nbytes = min(count, nmax); | 969 | size_t nbytes_limit = min(count, nmax); |
649 | int err = fuse_get_user_pages(req, buf, nbytes, !write); | 970 | size_t nbytes; |
971 | int err = fuse_get_user_pages(req, buf, nbytes_limit, !write); | ||
650 | if (err) { | 972 | if (err) { |
651 | res = err; | 973 | res = err; |
652 | break; | 974 | break; |
653 | } | 975 | } |
654 | nbytes = (req->num_pages << PAGE_SHIFT) - req->page_offset; | 976 | nbytes = (req->num_pages << PAGE_SHIFT) - req->page_offset; |
655 | nbytes = min(count, nbytes); | 977 | nbytes = min(nbytes_limit, nbytes); |
656 | if (write) | 978 | if (write) |
657 | nres = fuse_send_write(req, file, inode, pos, nbytes, | 979 | nres = fuse_send_write(req, file, inode, pos, nbytes, |
658 | current->files); | 980 | current->files); |
@@ -683,12 +1005,8 @@ static ssize_t fuse_direct_io(struct file *file, const char __user *buf, | |||
683 | } | 1005 | } |
684 | fuse_put_request(fc, req); | 1006 | fuse_put_request(fc, req); |
685 | if (res > 0) { | 1007 | if (res > 0) { |
686 | if (write) { | 1008 | if (write) |
687 | spin_lock(&fc->lock); | 1009 | fuse_write_update_size(inode, pos); |
688 | if (pos > inode->i_size) | ||
689 | i_size_write(inode, pos); | ||
690 | spin_unlock(&fc->lock); | ||
691 | } | ||
692 | *ppos = pos; | 1010 | *ppos = pos; |
693 | } | 1011 | } |
694 | fuse_invalidate_attr(inode); | 1012 | fuse_invalidate_attr(inode); |
@@ -716,21 +1034,225 @@ static ssize_t fuse_direct_write(struct file *file, const char __user *buf, | |||
716 | return res; | 1034 | return res; |
717 | } | 1035 | } |
718 | 1036 | ||
719 | static int fuse_file_mmap(struct file *file, struct vm_area_struct *vma) | 1037 | static void fuse_writepage_free(struct fuse_conn *fc, struct fuse_req *req) |
720 | { | 1038 | { |
721 | if ((vma->vm_flags & VM_SHARED)) { | 1039 | __free_page(req->pages[0]); |
722 | if ((vma->vm_flags & VM_WRITE)) | 1040 | fuse_file_put(req->ff); |
723 | return -ENODEV; | 1041 | fuse_put_request(fc, req); |
724 | else | 1042 | } |
725 | vma->vm_flags &= ~VM_MAYWRITE; | 1043 | |
1044 | static void fuse_writepage_finish(struct fuse_conn *fc, struct fuse_req *req) | ||
1045 | { | ||
1046 | struct inode *inode = req->inode; | ||
1047 | struct fuse_inode *fi = get_fuse_inode(inode); | ||
1048 | struct backing_dev_info *bdi = inode->i_mapping->backing_dev_info; | ||
1049 | |||
1050 | list_del(&req->writepages_entry); | ||
1051 | dec_bdi_stat(bdi, BDI_WRITEBACK); | ||
1052 | dec_zone_page_state(req->pages[0], NR_WRITEBACK_TEMP); | ||
1053 | bdi_writeout_inc(bdi); | ||
1054 | wake_up(&fi->page_waitq); | ||
1055 | } | ||
1056 | |||
1057 | /* Called under fc->lock, may release and reacquire it */ | ||
1058 | static void fuse_send_writepage(struct fuse_conn *fc, struct fuse_req *req) | ||
1059 | { | ||
1060 | struct fuse_inode *fi = get_fuse_inode(req->inode); | ||
1061 | loff_t size = i_size_read(req->inode); | ||
1062 | struct fuse_write_in *inarg = &req->misc.write.in; | ||
1063 | |||
1064 | if (!fc->connected) | ||
1065 | goto out_free; | ||
1066 | |||
1067 | if (inarg->offset + PAGE_CACHE_SIZE <= size) { | ||
1068 | inarg->size = PAGE_CACHE_SIZE; | ||
1069 | } else if (inarg->offset < size) { | ||
1070 | inarg->size = size & (PAGE_CACHE_SIZE - 1); | ||
1071 | } else { | ||
1072 | /* Got truncated off completely */ | ||
1073 | goto out_free; | ||
1074 | } | ||
1075 | |||
1076 | req->in.args[1].size = inarg->size; | ||
1077 | fi->writectr++; | ||
1078 | request_send_background_locked(fc, req); | ||
1079 | return; | ||
1080 | |||
1081 | out_free: | ||
1082 | fuse_writepage_finish(fc, req); | ||
1083 | spin_unlock(&fc->lock); | ||
1084 | fuse_writepage_free(fc, req); | ||
1085 | spin_lock(&fc->lock); | ||
1086 | } | ||
1087 | |||
1088 | /* | ||
1089 | * If fi->writectr is positive (no truncate or fsync going on) send | ||
1090 | * all queued writepage requests. | ||
1091 | * | ||
1092 | * Called with fc->lock | ||
1093 | */ | ||
1094 | void fuse_flush_writepages(struct inode *inode) | ||
1095 | { | ||
1096 | struct fuse_conn *fc = get_fuse_conn(inode); | ||
1097 | struct fuse_inode *fi = get_fuse_inode(inode); | ||
1098 | struct fuse_req *req; | ||
1099 | |||
1100 | while (fi->writectr >= 0 && !list_empty(&fi->queued_writes)) { | ||
1101 | req = list_entry(fi->queued_writes.next, struct fuse_req, list); | ||
1102 | list_del_init(&req->list); | ||
1103 | fuse_send_writepage(fc, req); | ||
1104 | } | ||
1105 | } | ||
1106 | |||
1107 | static void fuse_writepage_end(struct fuse_conn *fc, struct fuse_req *req) | ||
1108 | { | ||
1109 | struct inode *inode = req->inode; | ||
1110 | struct fuse_inode *fi = get_fuse_inode(inode); | ||
1111 | |||
1112 | mapping_set_error(inode->i_mapping, req->out.h.error); | ||
1113 | spin_lock(&fc->lock); | ||
1114 | fi->writectr--; | ||
1115 | fuse_writepage_finish(fc, req); | ||
1116 | spin_unlock(&fc->lock); | ||
1117 | fuse_writepage_free(fc, req); | ||
1118 | } | ||
1119 | |||
1120 | static int fuse_writepage_locked(struct page *page) | ||
1121 | { | ||
1122 | struct address_space *mapping = page->mapping; | ||
1123 | struct inode *inode = mapping->host; | ||
1124 | struct fuse_conn *fc = get_fuse_conn(inode); | ||
1125 | struct fuse_inode *fi = get_fuse_inode(inode); | ||
1126 | struct fuse_req *req; | ||
1127 | struct fuse_file *ff; | ||
1128 | struct page *tmp_page; | ||
1129 | |||
1130 | set_page_writeback(page); | ||
1131 | |||
1132 | req = fuse_request_alloc_nofs(); | ||
1133 | if (!req) | ||
1134 | goto err; | ||
1135 | |||
1136 | tmp_page = alloc_page(GFP_NOFS | __GFP_HIGHMEM); | ||
1137 | if (!tmp_page) | ||
1138 | goto err_free; | ||
1139 | |||
1140 | spin_lock(&fc->lock); | ||
1141 | BUG_ON(list_empty(&fi->write_files)); | ||
1142 | ff = list_entry(fi->write_files.next, struct fuse_file, write_entry); | ||
1143 | req->ff = fuse_file_get(ff); | ||
1144 | spin_unlock(&fc->lock); | ||
1145 | |||
1146 | fuse_write_fill(req, NULL, ff, inode, page_offset(page), 0, 1); | ||
1147 | |||
1148 | copy_highpage(tmp_page, page); | ||
1149 | req->num_pages = 1; | ||
1150 | req->pages[0] = tmp_page; | ||
1151 | req->page_offset = 0; | ||
1152 | req->end = fuse_writepage_end; | ||
1153 | req->inode = inode; | ||
1154 | |||
1155 | inc_bdi_stat(mapping->backing_dev_info, BDI_WRITEBACK); | ||
1156 | inc_zone_page_state(tmp_page, NR_WRITEBACK_TEMP); | ||
1157 | end_page_writeback(page); | ||
1158 | |||
1159 | spin_lock(&fc->lock); | ||
1160 | list_add(&req->writepages_entry, &fi->writepages); | ||
1161 | list_add_tail(&req->list, &fi->queued_writes); | ||
1162 | fuse_flush_writepages(inode); | ||
1163 | spin_unlock(&fc->lock); | ||
1164 | |||
1165 | return 0; | ||
1166 | |||
1167 | err_free: | ||
1168 | fuse_request_free(req); | ||
1169 | err: | ||
1170 | end_page_writeback(page); | ||
1171 | return -ENOMEM; | ||
1172 | } | ||
1173 | |||
1174 | static int fuse_writepage(struct page *page, struct writeback_control *wbc) | ||
1175 | { | ||
1176 | int err; | ||
1177 | |||
1178 | err = fuse_writepage_locked(page); | ||
1179 | unlock_page(page); | ||
1180 | |||
1181 | return err; | ||
1182 | } | ||
1183 | |||
1184 | static int fuse_launder_page(struct page *page) | ||
1185 | { | ||
1186 | int err = 0; | ||
1187 | if (clear_page_dirty_for_io(page)) { | ||
1188 | struct inode *inode = page->mapping->host; | ||
1189 | err = fuse_writepage_locked(page); | ||
1190 | if (!err) | ||
1191 | fuse_wait_on_page_writeback(inode, page->index); | ||
726 | } | 1192 | } |
727 | return generic_file_mmap(file, vma); | 1193 | return err; |
728 | } | 1194 | } |
729 | 1195 | ||
730 | static int fuse_set_page_dirty(struct page *page) | 1196 | /* |
1197 | * Write back dirty pages now, because there may not be any suitable | ||
1198 | * open files later | ||
1199 | */ | ||
1200 | static void fuse_vma_close(struct vm_area_struct *vma) | ||
731 | { | 1201 | { |
732 | printk("fuse_set_page_dirty: should not happen\n"); | 1202 | filemap_write_and_wait(vma->vm_file->f_mapping); |
733 | dump_stack(); | 1203 | } |
1204 | |||
1205 | /* | ||
1206 | * Wait for writeback against this page to complete before allowing it | ||
1207 | * to be marked dirty again, and hence written back again, possibly | ||
1208 | * before the previous writepage completed. | ||
1209 | * | ||
1210 | * Block here, instead of in ->writepage(), so that the userspace fs | ||
1211 | * can only block processes actually operating on the filesystem. | ||
1212 | * | ||
1213 | * Otherwise unprivileged userspace fs would be able to block | ||
1214 | * unrelated: | ||
1215 | * | ||
1216 | * - page migration | ||
1217 | * - sync(2) | ||
1218 | * - try_to_free_pages() with order > PAGE_ALLOC_COSTLY_ORDER | ||
1219 | */ | ||
1220 | static int fuse_page_mkwrite(struct vm_area_struct *vma, struct page *page) | ||
1221 | { | ||
1222 | /* | ||
1223 | * Don't use page->mapping as it may become NULL from a | ||
1224 | * concurrent truncate. | ||
1225 | */ | ||
1226 | struct inode *inode = vma->vm_file->f_mapping->host; | ||
1227 | |||
1228 | fuse_wait_on_page_writeback(inode, page->index); | ||
1229 | return 0; | ||
1230 | } | ||
1231 | |||
1232 | static struct vm_operations_struct fuse_file_vm_ops = { | ||
1233 | .close = fuse_vma_close, | ||
1234 | .fault = filemap_fault, | ||
1235 | .page_mkwrite = fuse_page_mkwrite, | ||
1236 | }; | ||
1237 | |||
1238 | static int fuse_file_mmap(struct file *file, struct vm_area_struct *vma) | ||
1239 | { | ||
1240 | if ((vma->vm_flags & VM_SHARED) && (vma->vm_flags & VM_MAYWRITE)) { | ||
1241 | struct inode *inode = file->f_dentry->d_inode; | ||
1242 | struct fuse_conn *fc = get_fuse_conn(inode); | ||
1243 | struct fuse_inode *fi = get_fuse_inode(inode); | ||
1244 | struct fuse_file *ff = file->private_data; | ||
1245 | /* | ||
1246 | * file may be written through mmap, so chain it onto the | ||
1247 | * inodes's write_file list | ||
1248 | */ | ||
1249 | spin_lock(&fc->lock); | ||
1250 | if (list_empty(&ff->write_entry)) | ||
1251 | list_add(&ff->write_entry, &fi->write_files); | ||
1252 | spin_unlock(&fc->lock); | ||
1253 | } | ||
1254 | file_accessed(file); | ||
1255 | vma->vm_ops = &fuse_file_vm_ops; | ||
734 | return 0; | 1256 | return 0; |
735 | } | 1257 | } |
736 | 1258 | ||
@@ -909,12 +1431,37 @@ static sector_t fuse_bmap(struct address_space *mapping, sector_t block) | |||
909 | return err ? 0 : outarg.block; | 1431 | return err ? 0 : outarg.block; |
910 | } | 1432 | } |
911 | 1433 | ||
1434 | static loff_t fuse_file_llseek(struct file *file, loff_t offset, int origin) | ||
1435 | { | ||
1436 | loff_t retval; | ||
1437 | struct inode *inode = file->f_path.dentry->d_inode; | ||
1438 | |||
1439 | mutex_lock(&inode->i_mutex); | ||
1440 | switch (origin) { | ||
1441 | case SEEK_END: | ||
1442 | offset += i_size_read(inode); | ||
1443 | break; | ||
1444 | case SEEK_CUR: | ||
1445 | offset += file->f_pos; | ||
1446 | } | ||
1447 | retval = -EINVAL; | ||
1448 | if (offset >= 0 && offset <= inode->i_sb->s_maxbytes) { | ||
1449 | if (offset != file->f_pos) { | ||
1450 | file->f_pos = offset; | ||
1451 | file->f_version = 0; | ||
1452 | } | ||
1453 | retval = offset; | ||
1454 | } | ||
1455 | mutex_unlock(&inode->i_mutex); | ||
1456 | return retval; | ||
1457 | } | ||
1458 | |||
912 | static const struct file_operations fuse_file_operations = { | 1459 | static const struct file_operations fuse_file_operations = { |
913 | .llseek = generic_file_llseek, | 1460 | .llseek = fuse_file_llseek, |
914 | .read = do_sync_read, | 1461 | .read = do_sync_read, |
915 | .aio_read = fuse_file_aio_read, | 1462 | .aio_read = fuse_file_aio_read, |
916 | .write = do_sync_write, | 1463 | .write = do_sync_write, |
917 | .aio_write = generic_file_aio_write, | 1464 | .aio_write = fuse_file_aio_write, |
918 | .mmap = fuse_file_mmap, | 1465 | .mmap = fuse_file_mmap, |
919 | .open = fuse_open, | 1466 | .open = fuse_open, |
920 | .flush = fuse_flush, | 1467 | .flush = fuse_flush, |
@@ -926,7 +1473,7 @@ static const struct file_operations fuse_file_operations = { | |||
926 | }; | 1473 | }; |
927 | 1474 | ||
928 | static const struct file_operations fuse_direct_io_file_operations = { | 1475 | static const struct file_operations fuse_direct_io_file_operations = { |
929 | .llseek = generic_file_llseek, | 1476 | .llseek = fuse_file_llseek, |
930 | .read = fuse_direct_read, | 1477 | .read = fuse_direct_read, |
931 | .write = fuse_direct_write, | 1478 | .write = fuse_direct_write, |
932 | .open = fuse_open, | 1479 | .open = fuse_open, |
@@ -940,10 +1487,12 @@ static const struct file_operations fuse_direct_io_file_operations = { | |||
940 | 1487 | ||
941 | static const struct address_space_operations fuse_file_aops = { | 1488 | static const struct address_space_operations fuse_file_aops = { |
942 | .readpage = fuse_readpage, | 1489 | .readpage = fuse_readpage, |
1490 | .writepage = fuse_writepage, | ||
1491 | .launder_page = fuse_launder_page, | ||
943 | .write_begin = fuse_write_begin, | 1492 | .write_begin = fuse_write_begin, |
944 | .write_end = fuse_write_end, | 1493 | .write_end = fuse_write_end, |
945 | .readpages = fuse_readpages, | 1494 | .readpages = fuse_readpages, |
946 | .set_page_dirty = fuse_set_page_dirty, | 1495 | .set_page_dirty = __set_page_dirty_nobuffers, |
947 | .bmap = fuse_bmap, | 1496 | .bmap = fuse_bmap, |
948 | }; | 1497 | }; |
949 | 1498 | ||