aboutsummaryrefslogtreecommitdiffstats
path: root/fs/fuse
diff options
context:
space:
mode:
authorMaxim Patlasov <MPatlasov@parallels.com>2013-08-16 07:51:41 -0400
committerMiklos Szeredi <mszeredi@suse.cz>2013-10-01 10:44:53 -0400
commit2d033eaa0073d276ee6c324dd0ade0c5074a5542 (patch)
tree1d75adee60456c49cb680210fb85ed0dbd0cd639 /fs/fuse
parent26d614df1da9d7d255686af5d6d4508f77853c01 (diff)
fuse: fix race in fuse_writepages()
The patch fixes a race between ftruncate(2), mmap-ed write and write(2): 1) An user makes a page dirty via mmap-ed write. 2) The user performs shrinking truncate(2) intended to purge the page. 3) Before fuse_do_setattr calls truncate_pagecache, the page goes to writeback. fuse_writepages_fill attaches a new page to FUSE_WRITE request, then releases the original page by end_page_writeback and unlock it. 4) fuse_do_setattr completes and successfully returns. Since now, i_mutex is free. 5) Ordinary write(2) extends i_size back to cover the page. Note that fuse_send_write_pages do wait for fuse writeback, but for another page->index. 6) fuse_writepages_fill attaches more pages to the request (if any), then fuse_writepages_send is eventually called. It is supposed to crop inarg->size of the request, but it doesn't because i_size has already been extended back. Moving end_page_writeback behind fuse_writepages_send guarantees that __fuse_release_nowrite (called from fuse_do_setattr) will crop inarg->size of the request before write(2) gets the chance to extend i_size. Signed-off-by: Maxim Patlasov <mpatlasov@parallels.com> Signed-off-by: Miklos Szeredi <mszeredi@suse.cz>
Diffstat (limited to 'fs/fuse')
-rw-r--r--fs/fuse/file.c17
1 files changed, 16 insertions, 1 deletions
diff --git a/fs/fuse/file.c b/fs/fuse/file.c
index 0bd349dd968f..cc3a6c4437e4 100644
--- a/fs/fuse/file.c
+++ b/fs/fuse/file.c
@@ -1590,6 +1590,7 @@ struct fuse_fill_wb_data {
1590 struct fuse_req *req; 1590 struct fuse_req *req;
1591 struct fuse_file *ff; 1591 struct fuse_file *ff;
1592 struct inode *inode; 1592 struct inode *inode;
1593 struct page **orig_pages;
1593}; 1594};
1594 1595
1595static void fuse_writepages_send(struct fuse_fill_wb_data *data) 1596static void fuse_writepages_send(struct fuse_fill_wb_data *data)
@@ -1598,12 +1599,17 @@ static void fuse_writepages_send(struct fuse_fill_wb_data *data)
1598 struct inode *inode = data->inode; 1599 struct inode *inode = data->inode;
1599 struct fuse_conn *fc = get_fuse_conn(inode); 1600 struct fuse_conn *fc = get_fuse_conn(inode);
1600 struct fuse_inode *fi = get_fuse_inode(inode); 1601 struct fuse_inode *fi = get_fuse_inode(inode);
1602 int num_pages = req->num_pages;
1603 int i;
1601 1604
1602 req->ff = fuse_file_get(data->ff); 1605 req->ff = fuse_file_get(data->ff);
1603 spin_lock(&fc->lock); 1606 spin_lock(&fc->lock);
1604 list_add_tail(&req->list, &fi->queued_writes); 1607 list_add_tail(&req->list, &fi->queued_writes);
1605 fuse_flush_writepages(inode); 1608 fuse_flush_writepages(inode);
1606 spin_unlock(&fc->lock); 1609 spin_unlock(&fc->lock);
1610
1611 for (i = 0; i < num_pages; i++)
1612 end_page_writeback(data->orig_pages[i]);
1607} 1613}
1608 1614
1609static int fuse_writepages_fill(struct page *page, 1615static int fuse_writepages_fill(struct page *page,
@@ -1684,7 +1690,7 @@ static int fuse_writepages_fill(struct page *page,
1684 1690
1685 inc_bdi_stat(page->mapping->backing_dev_info, BDI_WRITEBACK); 1691 inc_bdi_stat(page->mapping->backing_dev_info, BDI_WRITEBACK);
1686 inc_zone_page_state(tmp_page, NR_WRITEBACK_TEMP); 1692 inc_zone_page_state(tmp_page, NR_WRITEBACK_TEMP);
1687 end_page_writeback(page); 1693 data->orig_pages[req->num_pages] = page;
1688 1694
1689 /* 1695 /*
1690 * Protected by fc->lock against concurrent access by 1696 * Protected by fc->lock against concurrent access by
@@ -1716,6 +1722,13 @@ static int fuse_writepages(struct address_space *mapping,
1716 data.req = NULL; 1722 data.req = NULL;
1717 data.ff = NULL; 1723 data.ff = NULL;
1718 1724
1725 err = -ENOMEM;
1726 data.orig_pages = kzalloc(sizeof(struct page *) *
1727 FUSE_MAX_PAGES_PER_REQ,
1728 GFP_NOFS);
1729 if (!data.orig_pages)
1730 goto out;
1731
1719 err = write_cache_pages(mapping, wbc, fuse_writepages_fill, &data); 1732 err = write_cache_pages(mapping, wbc, fuse_writepages_fill, &data);
1720 if (data.req) { 1733 if (data.req) {
1721 /* Ignore errors if we can write at least one page */ 1734 /* Ignore errors if we can write at least one page */
@@ -1725,6 +1738,8 @@ static int fuse_writepages(struct address_space *mapping,
1725 } 1738 }
1726 if (data.ff) 1739 if (data.ff)
1727 fuse_file_put(data.ff, false); 1740 fuse_file_put(data.ff, false);
1741
1742 kfree(data.orig_pages);
1728out: 1743out:
1729 return err; 1744 return err;
1730} 1745}