11d3576fdSSage Weil #include "ceph_debug.h" 21d3576fdSSage Weil 31d3576fdSSage Weil #include <linux/backing-dev.h> 41d3576fdSSage Weil #include <linux/fs.h> 51d3576fdSSage Weil #include <linux/mm.h> 61d3576fdSSage Weil #include <linux/pagemap.h> 71d3576fdSSage Weil #include <linux/writeback.h> /* generic_writepages */ 85a0e3ad6STejun Heo #include <linux/slab.h> 91d3576fdSSage Weil #include <linux/pagevec.h> 101d3576fdSSage Weil #include <linux/task_io_accounting_ops.h> 111d3576fdSSage Weil 121d3576fdSSage Weil #include "super.h" 131d3576fdSSage Weil #include "osd_client.h" 141d3576fdSSage Weil 151d3576fdSSage Weil /* 161d3576fdSSage Weil * Ceph address space ops. 171d3576fdSSage Weil * 181d3576fdSSage Weil * There are a few funny things going on here. 191d3576fdSSage Weil * 201d3576fdSSage Weil * The page->private field is used to reference a struct 211d3576fdSSage Weil * ceph_snap_context for _every_ dirty page. This indicates which 221d3576fdSSage Weil * snapshot the page was logically dirtied in, and thus which snap 231d3576fdSSage Weil * context needs to be associated with the osd write during writeback. 241d3576fdSSage Weil * 251d3576fdSSage Weil * Similarly, struct ceph_inode_info maintains a set of counters to 261d3576fdSSage Weil * count dirty pages on the inode. In the absense of snapshots, 271d3576fdSSage Weil * i_wrbuffer_ref == i_wrbuffer_ref_head == the dirty page count. 281d3576fdSSage Weil * 291d3576fdSSage Weil * When a snapshot is taken (that is, when the client receives 301d3576fdSSage Weil * notification that a snapshot was taken), each inode with caps and 311d3576fdSSage Weil * with dirty pages (dirty pages implies there is a cap) gets a new 321d3576fdSSage Weil * ceph_cap_snap in the i_cap_snaps list (which is sorted in ascending 331d3576fdSSage Weil * order, new snaps go to the tail). The i_wrbuffer_ref_head count is 341d3576fdSSage Weil * moved to capsnap->dirty. (Unless a sync write is currently in 351d3576fdSSage Weil * progress. In that case, the capsnap is said to be "pending", new 361d3576fdSSage Weil * writes cannot start, and the capsnap isn't "finalized" until the 371d3576fdSSage Weil * write completes (or fails) and a final size/mtime for the inode for 381d3576fdSSage Weil * that snap can be settled upon.) i_wrbuffer_ref_head is reset to 0. 391d3576fdSSage Weil * 401d3576fdSSage Weil * On writeback, we must submit writes to the osd IN SNAP ORDER. So, 411d3576fdSSage Weil * we look for the first capsnap in i_cap_snaps and write out pages in 421d3576fdSSage Weil * that snap context _only_. Then we move on to the next capsnap, 431d3576fdSSage Weil * eventually reaching the "live" or "head" context (i.e., pages that 441d3576fdSSage Weil * are not yet snapped) and are writing the most recently dirtied 451d3576fdSSage Weil * pages. 461d3576fdSSage Weil * 471d3576fdSSage Weil * Invalidate and so forth must take care to ensure the dirty page 481d3576fdSSage Weil * accounting is preserved. 491d3576fdSSage Weil */ 501d3576fdSSage Weil 512baba250SYehuda Sadeh #define CONGESTION_ON_THRESH(congestion_kb) (congestion_kb >> (PAGE_SHIFT-10)) 522baba250SYehuda Sadeh #define CONGESTION_OFF_THRESH(congestion_kb) \ 532baba250SYehuda Sadeh (CONGESTION_ON_THRESH(congestion_kb) - \ 542baba250SYehuda Sadeh (CONGESTION_ON_THRESH(congestion_kb) >> 2)) 552baba250SYehuda Sadeh 562baba250SYehuda Sadeh 571d3576fdSSage Weil 581d3576fdSSage Weil /* 591d3576fdSSage Weil * Dirty a page. Optimistically adjust accounting, on the assumption 601d3576fdSSage Weil * that we won't race with invalidate. If we do, readjust. 611d3576fdSSage Weil */ 621d3576fdSSage Weil static int ceph_set_page_dirty(struct page *page) 631d3576fdSSage Weil { 641d3576fdSSage Weil struct address_space *mapping = page->mapping; 651d3576fdSSage Weil struct inode *inode; 661d3576fdSSage Weil struct ceph_inode_info *ci; 671d3576fdSSage Weil int undo = 0; 681d3576fdSSage Weil struct ceph_snap_context *snapc; 691d3576fdSSage Weil 701d3576fdSSage Weil if (unlikely(!mapping)) 711d3576fdSSage Weil return !TestSetPageDirty(page); 721d3576fdSSage Weil 731d3576fdSSage Weil if (TestSetPageDirty(page)) { 741d3576fdSSage Weil dout("%p set_page_dirty %p idx %lu -- already dirty\n", 751d3576fdSSage Weil mapping->host, page, page->index); 761d3576fdSSage Weil return 0; 771d3576fdSSage Weil } 781d3576fdSSage Weil 791d3576fdSSage Weil inode = mapping->host; 801d3576fdSSage Weil ci = ceph_inode(inode); 811d3576fdSSage Weil 821d3576fdSSage Weil /* 831d3576fdSSage Weil * Note that we're grabbing a snapc ref here without holding 841d3576fdSSage Weil * any locks! 851d3576fdSSage Weil */ 861d3576fdSSage Weil snapc = ceph_get_snap_context(ci->i_snap_realm->cached_context); 871d3576fdSSage Weil 881d3576fdSSage Weil /* dirty the head */ 891d3576fdSSage Weil spin_lock(&inode->i_lock); 901d3576fdSSage Weil if (ci->i_wrbuffer_ref_head == 0) 911d3576fdSSage Weil ci->i_head_snapc = ceph_get_snap_context(snapc); 921d3576fdSSage Weil ++ci->i_wrbuffer_ref_head; 931d3576fdSSage Weil if (ci->i_wrbuffer_ref == 0) 941d3576fdSSage Weil igrab(inode); 951d3576fdSSage Weil ++ci->i_wrbuffer_ref; 961d3576fdSSage Weil dout("%p set_page_dirty %p idx %lu head %d/%d -> %d/%d " 971d3576fdSSage Weil "snapc %p seq %lld (%d snaps)\n", 981d3576fdSSage Weil mapping->host, page, page->index, 991d3576fdSSage Weil ci->i_wrbuffer_ref-1, ci->i_wrbuffer_ref_head-1, 1001d3576fdSSage Weil ci->i_wrbuffer_ref, ci->i_wrbuffer_ref_head, 1011d3576fdSSage Weil snapc, snapc->seq, snapc->num_snaps); 1021d3576fdSSage Weil spin_unlock(&inode->i_lock); 1031d3576fdSSage Weil 1041d3576fdSSage Weil /* now adjust page */ 1051d3576fdSSage Weil spin_lock_irq(&mapping->tree_lock); 1061d3576fdSSage Weil if (page->mapping) { /* Race with truncate? */ 1071d3576fdSSage Weil WARN_ON_ONCE(!PageUptodate(page)); 1081d3576fdSSage Weil 1091d3576fdSSage Weil if (mapping_cap_account_dirty(mapping)) { 1101d3576fdSSage Weil __inc_zone_page_state(page, NR_FILE_DIRTY); 1111d3576fdSSage Weil __inc_bdi_stat(mapping->backing_dev_info, 1121d3576fdSSage Weil BDI_RECLAIMABLE); 1131d3576fdSSage Weil task_io_account_write(PAGE_CACHE_SIZE); 1141d3576fdSSage Weil } 1151d3576fdSSage Weil radix_tree_tag_set(&mapping->page_tree, 1161d3576fdSSage Weil page_index(page), PAGECACHE_TAG_DIRTY); 1171d3576fdSSage Weil 1181d3576fdSSage Weil /* 1191d3576fdSSage Weil * Reference snap context in page->private. Also set 1201d3576fdSSage Weil * PagePrivate so that we get invalidatepage callback. 1211d3576fdSSage Weil */ 1221d3576fdSSage Weil page->private = (unsigned long)snapc; 1231d3576fdSSage Weil SetPagePrivate(page); 1241d3576fdSSage Weil } else { 1251d3576fdSSage Weil dout("ANON set_page_dirty %p (raced truncate?)\n", page); 1261d3576fdSSage Weil undo = 1; 1271d3576fdSSage Weil } 1281d3576fdSSage Weil 1291d3576fdSSage Weil spin_unlock_irq(&mapping->tree_lock); 1301d3576fdSSage Weil 1311d3576fdSSage Weil if (undo) 1321d3576fdSSage Weil /* whoops, we failed to dirty the page */ 1331d3576fdSSage Weil ceph_put_wrbuffer_cap_refs(ci, 1, snapc); 1341d3576fdSSage Weil 1351d3576fdSSage Weil __mark_inode_dirty(mapping->host, I_DIRTY_PAGES); 1361d3576fdSSage Weil 1371d3576fdSSage Weil BUG_ON(!PageDirty(page)); 1381d3576fdSSage Weil return 1; 1391d3576fdSSage Weil } 1401d3576fdSSage Weil 1411d3576fdSSage Weil /* 1421d3576fdSSage Weil * If we are truncating the full page (i.e. offset == 0), adjust the 1431d3576fdSSage Weil * dirty page counters appropriately. Only called if there is private 1441d3576fdSSage Weil * data on the page. 1451d3576fdSSage Weil */ 1461d3576fdSSage Weil static void ceph_invalidatepage(struct page *page, unsigned long offset) 1471d3576fdSSage Weil { 1484ce1e9adSAlexander Beregalov struct inode *inode; 1491d3576fdSSage Weil struct ceph_inode_info *ci; 1501d3576fdSSage Weil struct ceph_snap_context *snapc = (void *)page->private; 1511d3576fdSSage Weil 1521d3576fdSSage Weil BUG_ON(!PageLocked(page)); 1531d3576fdSSage Weil BUG_ON(!page->private); 1541d3576fdSSage Weil BUG_ON(!PagePrivate(page)); 1551d3576fdSSage Weil BUG_ON(!page->mapping); 1561d3576fdSSage Weil 1574ce1e9adSAlexander Beregalov inode = page->mapping->host; 1584ce1e9adSAlexander Beregalov 1591d3576fdSSage Weil /* 1601d3576fdSSage Weil * We can get non-dirty pages here due to races between 1611d3576fdSSage Weil * set_page_dirty and truncate_complete_page; just spit out a 1621d3576fdSSage Weil * warning, in case we end up with accounting problems later. 1631d3576fdSSage Weil */ 1641d3576fdSSage Weil if (!PageDirty(page)) 1651d3576fdSSage Weil pr_err("%p invalidatepage %p page not dirty\n", inode, page); 1661d3576fdSSage Weil 1671d3576fdSSage Weil if (offset == 0) 1681d3576fdSSage Weil ClearPageChecked(page); 1691d3576fdSSage Weil 1701d3576fdSSage Weil ci = ceph_inode(inode); 1711d3576fdSSage Weil if (offset == 0) { 1721d3576fdSSage Weil dout("%p invalidatepage %p idx %lu full dirty page %lu\n", 1731d3576fdSSage Weil inode, page, page->index, offset); 1741d3576fdSSage Weil ceph_put_wrbuffer_cap_refs(ci, 1, snapc); 1751d3576fdSSage Weil ceph_put_snap_context(snapc); 1761d3576fdSSage Weil page->private = 0; 1771d3576fdSSage Weil ClearPagePrivate(page); 1781d3576fdSSage Weil } else { 1791d3576fdSSage Weil dout("%p invalidatepage %p idx %lu partial dirty page\n", 1801d3576fdSSage Weil inode, page, page->index); 1811d3576fdSSage Weil } 1821d3576fdSSage Weil } 1831d3576fdSSage Weil 1841d3576fdSSage Weil /* just a sanity check */ 1851d3576fdSSage Weil static int ceph_releasepage(struct page *page, gfp_t g) 1861d3576fdSSage Weil { 1871d3576fdSSage Weil struct inode *inode = page->mapping ? page->mapping->host : NULL; 1881d3576fdSSage Weil dout("%p releasepage %p idx %lu\n", inode, page, page->index); 1891d3576fdSSage Weil WARN_ON(PageDirty(page)); 1901d3576fdSSage Weil WARN_ON(page->private); 1911d3576fdSSage Weil WARN_ON(PagePrivate(page)); 1921d3576fdSSage Weil return 0; 1931d3576fdSSage Weil } 1941d3576fdSSage Weil 1951d3576fdSSage Weil /* 1961d3576fdSSage Weil * read a single page, without unlocking it. 1971d3576fdSSage Weil */ 1981d3576fdSSage Weil static int readpage_nounlock(struct file *filp, struct page *page) 1991d3576fdSSage Weil { 2001d3576fdSSage Weil struct inode *inode = filp->f_dentry->d_inode; 2011d3576fdSSage Weil struct ceph_inode_info *ci = ceph_inode(inode); 2021d3576fdSSage Weil struct ceph_osd_client *osdc = &ceph_inode_to_client(inode)->osdc; 2031d3576fdSSage Weil int err = 0; 2041d3576fdSSage Weil u64 len = PAGE_CACHE_SIZE; 2051d3576fdSSage Weil 2061d3576fdSSage Weil dout("readpage inode %p file %p page %p index %lu\n", 2071d3576fdSSage Weil inode, filp, page, page->index); 2081d3576fdSSage Weil err = ceph_osdc_readpages(osdc, ceph_vino(inode), &ci->i_layout, 2091d3576fdSSage Weil page->index << PAGE_CACHE_SHIFT, &len, 2101d3576fdSSage Weil ci->i_truncate_seq, ci->i_truncate_size, 2111d3576fdSSage Weil &page, 1); 2121d3576fdSSage Weil if (err == -ENOENT) 2131d3576fdSSage Weil err = 0; 2141d3576fdSSage Weil if (err < 0) { 2151d3576fdSSage Weil SetPageError(page); 2161d3576fdSSage Weil goto out; 2171d3576fdSSage Weil } else if (err < PAGE_CACHE_SIZE) { 2181d3576fdSSage Weil /* zero fill remainder of page */ 2191d3576fdSSage Weil zero_user_segment(page, err, PAGE_CACHE_SIZE); 2201d3576fdSSage Weil } 2211d3576fdSSage Weil SetPageUptodate(page); 2221d3576fdSSage Weil 2231d3576fdSSage Weil out: 2241d3576fdSSage Weil return err < 0 ? err : 0; 2251d3576fdSSage Weil } 2261d3576fdSSage Weil 2271d3576fdSSage Weil static int ceph_readpage(struct file *filp, struct page *page) 2281d3576fdSSage Weil { 2291d3576fdSSage Weil int r = readpage_nounlock(filp, page); 2301d3576fdSSage Weil unlock_page(page); 2311d3576fdSSage Weil return r; 2321d3576fdSSage Weil } 2331d3576fdSSage Weil 2341d3576fdSSage Weil /* 2351d3576fdSSage Weil * Build a vector of contiguous pages from the provided page list. 2361d3576fdSSage Weil */ 2371d3576fdSSage Weil static struct page **page_vector_from_list(struct list_head *page_list, 2381d3576fdSSage Weil unsigned *nr_pages) 2391d3576fdSSage Weil { 2401d3576fdSSage Weil struct page **pages; 2411d3576fdSSage Weil struct page *page; 2421d3576fdSSage Weil int next_index, contig_pages = 0; 2431d3576fdSSage Weil 2441d3576fdSSage Weil /* build page vector */ 2451d3576fdSSage Weil pages = kmalloc(sizeof(*pages) * *nr_pages, GFP_NOFS); 2461d3576fdSSage Weil if (!pages) 2471d3576fdSSage Weil return ERR_PTR(-ENOMEM); 2481d3576fdSSage Weil 2491d3576fdSSage Weil BUG_ON(list_empty(page_list)); 2501d3576fdSSage Weil next_index = list_entry(page_list->prev, struct page, lru)->index; 2511d3576fdSSage Weil list_for_each_entry_reverse(page, page_list, lru) { 2521d3576fdSSage Weil if (page->index == next_index) { 2531d3576fdSSage Weil dout("readpages page %d %p\n", contig_pages, page); 2541d3576fdSSage Weil pages[contig_pages] = page; 2551d3576fdSSage Weil contig_pages++; 2561d3576fdSSage Weil next_index++; 2571d3576fdSSage Weil } else { 2581d3576fdSSage Weil break; 2591d3576fdSSage Weil } 2601d3576fdSSage Weil } 2611d3576fdSSage Weil *nr_pages = contig_pages; 2621d3576fdSSage Weil return pages; 2631d3576fdSSage Weil } 2641d3576fdSSage Weil 2651d3576fdSSage Weil /* 2661d3576fdSSage Weil * Read multiple pages. Leave pages we don't read + unlock in page_list; 2671d3576fdSSage Weil * the caller (VM) cleans them up. 2681d3576fdSSage Weil */ 2691d3576fdSSage Weil static int ceph_readpages(struct file *file, struct address_space *mapping, 2701d3576fdSSage Weil struct list_head *page_list, unsigned nr_pages) 2711d3576fdSSage Weil { 2721d3576fdSSage Weil struct inode *inode = file->f_dentry->d_inode; 2731d3576fdSSage Weil struct ceph_inode_info *ci = ceph_inode(inode); 2741d3576fdSSage Weil struct ceph_osd_client *osdc = &ceph_inode_to_client(inode)->osdc; 2751d3576fdSSage Weil int rc = 0; 2761d3576fdSSage Weil struct page **pages; 2771d3576fdSSage Weil loff_t offset; 2781d3576fdSSage Weil u64 len; 2791d3576fdSSage Weil 2801d3576fdSSage Weil dout("readpages %p file %p nr_pages %d\n", 2811d3576fdSSage Weil inode, file, nr_pages); 2821d3576fdSSage Weil 2831d3576fdSSage Weil pages = page_vector_from_list(page_list, &nr_pages); 2841d3576fdSSage Weil if (IS_ERR(pages)) 2851d3576fdSSage Weil return PTR_ERR(pages); 2861d3576fdSSage Weil 2871d3576fdSSage Weil /* guess read extent */ 2881d3576fdSSage Weil offset = pages[0]->index << PAGE_CACHE_SHIFT; 2891d3576fdSSage Weil len = nr_pages << PAGE_CACHE_SHIFT; 2901d3576fdSSage Weil rc = ceph_osdc_readpages(osdc, ceph_vino(inode), &ci->i_layout, 2911d3576fdSSage Weil offset, &len, 2921d3576fdSSage Weil ci->i_truncate_seq, ci->i_truncate_size, 2931d3576fdSSage Weil pages, nr_pages); 2941d3576fdSSage Weil if (rc == -ENOENT) 2951d3576fdSSage Weil rc = 0; 2961d3576fdSSage Weil if (rc < 0) 2971d3576fdSSage Weil goto out; 2981d3576fdSSage Weil 2991d3576fdSSage Weil for (; !list_empty(page_list) && len > 0; 3001d3576fdSSage Weil rc -= PAGE_CACHE_SIZE, len -= PAGE_CACHE_SIZE) { 3011d3576fdSSage Weil struct page *page = 3021d3576fdSSage Weil list_entry(page_list->prev, struct page, lru); 3031d3576fdSSage Weil 3041d3576fdSSage Weil list_del(&page->lru); 3051d3576fdSSage Weil 3061d3576fdSSage Weil if (rc < (int)PAGE_CACHE_SIZE) { 3071d3576fdSSage Weil /* zero (remainder of) page */ 3081d3576fdSSage Weil int s = rc < 0 ? 0 : rc; 3091d3576fdSSage Weil zero_user_segment(page, s, PAGE_CACHE_SIZE); 3101d3576fdSSage Weil } 3111d3576fdSSage Weil 31231459fe4SYehuda Sadeh if (add_to_page_cache_lru(page, mapping, page->index, GFP_NOFS)) { 3131d3576fdSSage Weil page_cache_release(page); 3141d3576fdSSage Weil dout("readpages %p add_to_page_cache failed %p\n", 3151d3576fdSSage Weil inode, page); 3161d3576fdSSage Weil continue; 3171d3576fdSSage Weil } 3181d3576fdSSage Weil dout("readpages %p adding %p idx %lu\n", inode, page, 3191d3576fdSSage Weil page->index); 3201d3576fdSSage Weil flush_dcache_page(page); 3211d3576fdSSage Weil SetPageUptodate(page); 3221d3576fdSSage Weil unlock_page(page); 32331459fe4SYehuda Sadeh page_cache_release(page); 3241d3576fdSSage Weil } 3251d3576fdSSage Weil rc = 0; 3261d3576fdSSage Weil 3271d3576fdSSage Weil out: 3281d3576fdSSage Weil kfree(pages); 3291d3576fdSSage Weil return rc; 3301d3576fdSSage Weil } 3311d3576fdSSage Weil 3321d3576fdSSage Weil /* 3331d3576fdSSage Weil * Get ref for the oldest snapc for an inode with dirty data... that is, the 3341d3576fdSSage Weil * only snap context we are allowed to write back. 3351d3576fdSSage Weil */ 3366298a337SSage Weil static struct ceph_snap_context *get_oldest_context(struct inode *inode, 3371d3576fdSSage Weil u64 *snap_size) 3381d3576fdSSage Weil { 3391d3576fdSSage Weil struct ceph_inode_info *ci = ceph_inode(inode); 3401d3576fdSSage Weil struct ceph_snap_context *snapc = NULL; 3411d3576fdSSage Weil struct ceph_cap_snap *capsnap = NULL; 3421d3576fdSSage Weil 3436298a337SSage Weil spin_lock(&inode->i_lock); 3441d3576fdSSage Weil list_for_each_entry(capsnap, &ci->i_cap_snaps, ci_item) { 3451d3576fdSSage Weil dout(" cap_snap %p snapc %p has %d dirty pages\n", capsnap, 3461d3576fdSSage Weil capsnap->context, capsnap->dirty_pages); 3471d3576fdSSage Weil if (capsnap->dirty_pages) { 3481d3576fdSSage Weil snapc = ceph_get_snap_context(capsnap->context); 3491d3576fdSSage Weil if (snap_size) 3501d3576fdSSage Weil *snap_size = capsnap->size; 3511d3576fdSSage Weil break; 3521d3576fdSSage Weil } 3531d3576fdSSage Weil } 35480e755feSSage Weil if (!snapc && ci->i_head_snapc) { 35580e755feSSage Weil snapc = ceph_get_snap_context(ci->i_head_snapc); 3561d3576fdSSage Weil dout(" head snapc %p has %d dirty pages\n", 3571d3576fdSSage Weil snapc, ci->i_wrbuffer_ref_head); 3581d3576fdSSage Weil } 3591d3576fdSSage Weil spin_unlock(&inode->i_lock); 3601d3576fdSSage Weil return snapc; 3611d3576fdSSage Weil } 3621d3576fdSSage Weil 3631d3576fdSSage Weil /* 3641d3576fdSSage Weil * Write a single page, but leave the page locked. 3651d3576fdSSage Weil * 3661d3576fdSSage Weil * If we get a write error, set the page error bit, but still adjust the 3671d3576fdSSage Weil * dirty page accounting (i.e., page is no longer dirty). 3681d3576fdSSage Weil */ 3691d3576fdSSage Weil static int writepage_nounlock(struct page *page, struct writeback_control *wbc) 3701d3576fdSSage Weil { 3711d3576fdSSage Weil struct inode *inode; 3721d3576fdSSage Weil struct ceph_inode_info *ci; 3732baba250SYehuda Sadeh struct ceph_client *client; 3741d3576fdSSage Weil struct ceph_osd_client *osdc; 3751d3576fdSSage Weil loff_t page_off = page->index << PAGE_CACHE_SHIFT; 3761d3576fdSSage Weil int len = PAGE_CACHE_SIZE; 3771d3576fdSSage Weil loff_t i_size; 3781d3576fdSSage Weil int err = 0; 3796298a337SSage Weil struct ceph_snap_context *snapc, *oldest; 3801d3576fdSSage Weil u64 snap_size = 0; 3812baba250SYehuda Sadeh long writeback_stat; 3821d3576fdSSage Weil 3831d3576fdSSage Weil dout("writepage %p idx %lu\n", page, page->index); 3841d3576fdSSage Weil 3851d3576fdSSage Weil if (!page->mapping || !page->mapping->host) { 3861d3576fdSSage Weil dout("writepage %p - no mapping\n", page); 3871d3576fdSSage Weil return -EFAULT; 3881d3576fdSSage Weil } 3891d3576fdSSage Weil inode = page->mapping->host; 3901d3576fdSSage Weil ci = ceph_inode(inode); 3912baba250SYehuda Sadeh client = ceph_inode_to_client(inode); 3922baba250SYehuda Sadeh osdc = &client->osdc; 3931d3576fdSSage Weil 3941d3576fdSSage Weil /* verify this is a writeable snap context */ 3951d3576fdSSage Weil snapc = (void *)page->private; 3961d3576fdSSage Weil if (snapc == NULL) { 3971d3576fdSSage Weil dout("writepage %p page %p not dirty?\n", inode, page); 3981d3576fdSSage Weil goto out; 3991d3576fdSSage Weil } 4006298a337SSage Weil oldest = get_oldest_context(inode, &snap_size); 4016298a337SSage Weil if (snapc->seq > oldest->seq) { 4021d3576fdSSage Weil dout("writepage %p page %p snapc %p not writeable - noop\n", 4031d3576fdSSage Weil inode, page, (void *)page->private); 4041d3576fdSSage Weil /* we should only noop if called by kswapd */ 4051d3576fdSSage Weil WARN_ON((current->flags & PF_MEMALLOC) == 0); 4066298a337SSage Weil ceph_put_snap_context(oldest); 4071d3576fdSSage Weil goto out; 4081d3576fdSSage Weil } 4096298a337SSage Weil ceph_put_snap_context(oldest); 4101d3576fdSSage Weil 4111d3576fdSSage Weil /* is this a partial page at end of file? */ 4121d3576fdSSage Weil if (snap_size) 4131d3576fdSSage Weil i_size = snap_size; 4141d3576fdSSage Weil else 4151d3576fdSSage Weil i_size = i_size_read(inode); 4161d3576fdSSage Weil if (i_size < page_off + len) 4171d3576fdSSage Weil len = i_size - page_off; 4181d3576fdSSage Weil 4191d3576fdSSage Weil dout("writepage %p page %p index %lu on %llu~%u\n", 4201d3576fdSSage Weil inode, page, page->index, page_off, len); 4211d3576fdSSage Weil 4222baba250SYehuda Sadeh writeback_stat = atomic_long_inc_return(&client->writeback_count); 4232baba250SYehuda Sadeh if (writeback_stat > 4242baba250SYehuda Sadeh CONGESTION_ON_THRESH(client->mount_args->congestion_kb)) 4252baba250SYehuda Sadeh set_bdi_congested(&client->backing_dev_info, BLK_RW_ASYNC); 4262baba250SYehuda Sadeh 4271d3576fdSSage Weil set_page_writeback(page); 4281d3576fdSSage Weil err = ceph_osdc_writepages(osdc, ceph_vino(inode), 4291d3576fdSSage Weil &ci->i_layout, snapc, 4301d3576fdSSage Weil page_off, len, 4311d3576fdSSage Weil ci->i_truncate_seq, ci->i_truncate_size, 4321d3576fdSSage Weil &inode->i_mtime, 4331d3576fdSSage Weil &page, 1, 0, 0, true); 4341d3576fdSSage Weil if (err < 0) { 4351d3576fdSSage Weil dout("writepage setting page/mapping error %d %p\n", err, page); 4361d3576fdSSage Weil SetPageError(page); 4371d3576fdSSage Weil mapping_set_error(&inode->i_data, err); 4381d3576fdSSage Weil if (wbc) 4391d3576fdSSage Weil wbc->pages_skipped++; 4401d3576fdSSage Weil } else { 4411d3576fdSSage Weil dout("writepage cleaned page %p\n", page); 4421d3576fdSSage Weil err = 0; /* vfs expects us to return 0 */ 4431d3576fdSSage Weil } 4441d3576fdSSage Weil page->private = 0; 4451d3576fdSSage Weil ClearPagePrivate(page); 4461d3576fdSSage Weil end_page_writeback(page); 4471d3576fdSSage Weil ceph_put_wrbuffer_cap_refs(ci, 1, snapc); 4486298a337SSage Weil ceph_put_snap_context(snapc); /* page's reference */ 4491d3576fdSSage Weil out: 4501d3576fdSSage Weil return err; 4511d3576fdSSage Weil } 4521d3576fdSSage Weil 4531d3576fdSSage Weil static int ceph_writepage(struct page *page, struct writeback_control *wbc) 4541d3576fdSSage Weil { 455dbd646a8SYehuda Sadeh int err; 456dbd646a8SYehuda Sadeh struct inode *inode = page->mapping->host; 457dbd646a8SYehuda Sadeh BUG_ON(!inode); 458dbd646a8SYehuda Sadeh igrab(inode); 459dbd646a8SYehuda Sadeh err = writepage_nounlock(page, wbc); 4601d3576fdSSage Weil unlock_page(page); 461dbd646a8SYehuda Sadeh iput(inode); 4621d3576fdSSage Weil return err; 4631d3576fdSSage Weil } 4641d3576fdSSage Weil 4651d3576fdSSage Weil 4661d3576fdSSage Weil /* 4671d3576fdSSage Weil * lame release_pages helper. release_pages() isn't exported to 4681d3576fdSSage Weil * modules. 4691d3576fdSSage Weil */ 4701d3576fdSSage Weil static void ceph_release_pages(struct page **pages, int num) 4711d3576fdSSage Weil { 4721d3576fdSSage Weil struct pagevec pvec; 4731d3576fdSSage Weil int i; 4741d3576fdSSage Weil 4751d3576fdSSage Weil pagevec_init(&pvec, 0); 4761d3576fdSSage Weil for (i = 0; i < num; i++) { 4771d3576fdSSage Weil if (pagevec_add(&pvec, pages[i]) == 0) 4781d3576fdSSage Weil pagevec_release(&pvec); 4791d3576fdSSage Weil } 4801d3576fdSSage Weil pagevec_release(&pvec); 4811d3576fdSSage Weil } 4821d3576fdSSage Weil 4831d3576fdSSage Weil 4841d3576fdSSage Weil /* 4851d3576fdSSage Weil * async writeback completion handler. 4861d3576fdSSage Weil * 4871d3576fdSSage Weil * If we get an error, set the mapping error bit, but not the individual 4881d3576fdSSage Weil * page error bits. 4891d3576fdSSage Weil */ 4901d3576fdSSage Weil static void writepages_finish(struct ceph_osd_request *req, 4911d3576fdSSage Weil struct ceph_msg *msg) 4921d3576fdSSage Weil { 4931d3576fdSSage Weil struct inode *inode = req->r_inode; 4941d3576fdSSage Weil struct ceph_osd_reply_head *replyhead; 4951d3576fdSSage Weil struct ceph_osd_op *op; 4961d3576fdSSage Weil struct ceph_inode_info *ci = ceph_inode(inode); 4971d3576fdSSage Weil unsigned wrote; 4981d3576fdSSage Weil struct page *page; 4991d3576fdSSage Weil int i; 5001d3576fdSSage Weil struct ceph_snap_context *snapc = req->r_snapc; 5011d3576fdSSage Weil struct address_space *mapping = inode->i_mapping; 5021d3576fdSSage Weil __s32 rc = -EIO; 5031d3576fdSSage Weil u64 bytes = 0; 5042baba250SYehuda Sadeh struct ceph_client *client = ceph_inode_to_client(inode); 5052baba250SYehuda Sadeh long writeback_stat; 5067ff899daSSage Weil unsigned issued = ceph_caps_issued(ci); 5071d3576fdSSage Weil 5081d3576fdSSage Weil /* parse reply */ 5091d3576fdSSage Weil replyhead = msg->front.iov_base; 5101d3576fdSSage Weil WARN_ON(le32_to_cpu(replyhead->num_ops) == 0); 5111d3576fdSSage Weil op = (void *)(replyhead + 1); 5121d3576fdSSage Weil rc = le32_to_cpu(replyhead->result); 5131d3576fdSSage Weil bytes = le64_to_cpu(op->extent.length); 5141d3576fdSSage Weil 5151d3576fdSSage Weil if (rc >= 0) { 51679788c69SSage Weil /* 51779788c69SSage Weil * Assume we wrote the pages we originally sent. The 51879788c69SSage Weil * osd might reply with fewer pages if our writeback 51979788c69SSage Weil * raced with a truncation and was adjusted at the osd, 52079788c69SSage Weil * so don't believe the reply. 52179788c69SSage Weil */ 52279788c69SSage Weil wrote = req->r_num_pages; 5231d3576fdSSage Weil } else { 5241d3576fdSSage Weil wrote = 0; 5251d3576fdSSage Weil mapping_set_error(mapping, rc); 5261d3576fdSSage Weil } 5271d3576fdSSage Weil dout("writepages_finish %p rc %d bytes %llu wrote %d (pages)\n", 5281d3576fdSSage Weil inode, rc, bytes, wrote); 5291d3576fdSSage Weil 5301d3576fdSSage Weil /* clean all pages */ 5311d3576fdSSage Weil for (i = 0; i < req->r_num_pages; i++) { 5321d3576fdSSage Weil page = req->r_pages[i]; 5331d3576fdSSage Weil BUG_ON(!page); 5341d3576fdSSage Weil WARN_ON(!PageUptodate(page)); 5351d3576fdSSage Weil 5362baba250SYehuda Sadeh writeback_stat = 5372baba250SYehuda Sadeh atomic_long_dec_return(&client->writeback_count); 5382baba250SYehuda Sadeh if (writeback_stat < 5392baba250SYehuda Sadeh CONGESTION_OFF_THRESH(client->mount_args->congestion_kb)) 5402baba250SYehuda Sadeh clear_bdi_congested(&client->backing_dev_info, 5412baba250SYehuda Sadeh BLK_RW_ASYNC); 5422baba250SYehuda Sadeh 54380e755feSSage Weil ceph_put_snap_context((void *)page->private); 5441d3576fdSSage Weil page->private = 0; 5451d3576fdSSage Weil ClearPagePrivate(page); 5461d3576fdSSage Weil dout("unlocking %d %p\n", i, page); 5471d3576fdSSage Weil end_page_writeback(page); 548e63dc5c7SYehuda Sadeh 549e63dc5c7SYehuda Sadeh /* 550e63dc5c7SYehuda Sadeh * We lost the cache cap, need to truncate the page before 551e63dc5c7SYehuda Sadeh * it is unlocked, otherwise we'd truncate it later in the 552e63dc5c7SYehuda Sadeh * page truncation thread, possibly losing some data that 553e63dc5c7SYehuda Sadeh * raced its way in 554e63dc5c7SYehuda Sadeh */ 555e63dc5c7SYehuda Sadeh if ((issued & CEPH_CAP_FILE_CACHE) == 0) 556e63dc5c7SYehuda Sadeh generic_error_remove_page(inode->i_mapping, page); 557e63dc5c7SYehuda Sadeh 5581d3576fdSSage Weil unlock_page(page); 5591d3576fdSSage Weil } 5601d3576fdSSage Weil dout("%p wrote+cleaned %d pages\n", inode, wrote); 5611d3576fdSSage Weil ceph_put_wrbuffer_cap_refs(ci, req->r_num_pages, snapc); 5621d3576fdSSage Weil 5631d3576fdSSage Weil ceph_release_pages(req->r_pages, req->r_num_pages); 5641d3576fdSSage Weil if (req->r_pages_from_pool) 5651d3576fdSSage Weil mempool_free(req->r_pages, 5661d3576fdSSage Weil ceph_client(inode->i_sb)->wb_pagevec_pool); 5671d3576fdSSage Weil else 5681d3576fdSSage Weil kfree(req->r_pages); 5691d3576fdSSage Weil ceph_osdc_put_request(req); 5701d3576fdSSage Weil } 5711d3576fdSSage Weil 5721d3576fdSSage Weil /* 5731d3576fdSSage Weil * allocate a page vec, either directly, or if necessary, via a the 5741d3576fdSSage Weil * mempool. we avoid the mempool if we can because req->r_num_pages 5751d3576fdSSage Weil * may be less than the maximum write size. 5761d3576fdSSage Weil */ 5771d3576fdSSage Weil static void alloc_page_vec(struct ceph_client *client, 5781d3576fdSSage Weil struct ceph_osd_request *req) 5791d3576fdSSage Weil { 5801d3576fdSSage Weil req->r_pages = kmalloc(sizeof(struct page *) * req->r_num_pages, 5811d3576fdSSage Weil GFP_NOFS); 5821d3576fdSSage Weil if (!req->r_pages) { 5831d3576fdSSage Weil req->r_pages = mempool_alloc(client->wb_pagevec_pool, GFP_NOFS); 5841d3576fdSSage Weil req->r_pages_from_pool = 1; 5851d3576fdSSage Weil WARN_ON(!req->r_pages); 5861d3576fdSSage Weil } 5871d3576fdSSage Weil } 5881d3576fdSSage Weil 5891d3576fdSSage Weil /* 5901d3576fdSSage Weil * initiate async writeback 5911d3576fdSSage Weil */ 5921d3576fdSSage Weil static int ceph_writepages_start(struct address_space *mapping, 5931d3576fdSSage Weil struct writeback_control *wbc) 5941d3576fdSSage Weil { 5951d3576fdSSage Weil struct inode *inode = mapping->host; 5961d3576fdSSage Weil struct backing_dev_info *bdi = mapping->backing_dev_info; 5971d3576fdSSage Weil struct ceph_inode_info *ci = ceph_inode(inode); 598ec7384ecSJulia Lawall struct ceph_client *client; 5991d3576fdSSage Weil pgoff_t index, start, end; 6001d3576fdSSage Weil int range_whole = 0; 6011d3576fdSSage Weil int should_loop = 1; 6021d3576fdSSage Weil pgoff_t max_pages = 0, max_pages_ever = 0; 60380e755feSSage Weil struct ceph_snap_context *snapc = NULL, *last_snapc = NULL, *pgsnapc; 6041d3576fdSSage Weil struct pagevec pvec; 6051d3576fdSSage Weil int done = 0; 6061d3576fdSSage Weil int rc = 0; 6071d3576fdSSage Weil unsigned wsize = 1 << inode->i_blkbits; 6081d3576fdSSage Weil struct ceph_osd_request *req = NULL; 6091d3576fdSSage Weil int do_sync; 6101d3576fdSSage Weil u64 snap_size = 0; 6111d3576fdSSage Weil 6121d3576fdSSage Weil /* 6131d3576fdSSage Weil * Include a 'sync' in the OSD request if this is a data 6141d3576fdSSage Weil * integrity write (e.g., O_SYNC write or fsync()), or if our 6151d3576fdSSage Weil * cap is being revoked. 6161d3576fdSSage Weil */ 6171d3576fdSSage Weil do_sync = wbc->sync_mode == WB_SYNC_ALL; 6181d3576fdSSage Weil if (ceph_caps_revoking(ci, CEPH_CAP_FILE_BUFFER)) 6191d3576fdSSage Weil do_sync = 1; 6201d3576fdSSage Weil dout("writepages_start %p dosync=%d (mode=%s)\n", 6211d3576fdSSage Weil inode, do_sync, 6221d3576fdSSage Weil wbc->sync_mode == WB_SYNC_NONE ? "NONE" : 6231d3576fdSSage Weil (wbc->sync_mode == WB_SYNC_ALL ? "ALL" : "HOLD")); 6241d3576fdSSage Weil 6251d3576fdSSage Weil client = ceph_inode_to_client(inode); 6261d3576fdSSage Weil if (client->mount_state == CEPH_MOUNT_SHUTDOWN) { 6271d3576fdSSage Weil pr_warning("writepage_start %p on forced umount\n", inode); 6281d3576fdSSage Weil return -EIO; /* we're in a forced umount, don't write! */ 6291d3576fdSSage Weil } 6306b805185SSage Weil if (client->mount_args->wsize && client->mount_args->wsize < wsize) 6316b805185SSage Weil wsize = client->mount_args->wsize; 6321d3576fdSSage Weil if (wsize < PAGE_CACHE_SIZE) 6331d3576fdSSage Weil wsize = PAGE_CACHE_SIZE; 6341d3576fdSSage Weil max_pages_ever = wsize >> PAGE_CACHE_SHIFT; 6351d3576fdSSage Weil 6361d3576fdSSage Weil pagevec_init(&pvec, 0); 6371d3576fdSSage Weil 6381d3576fdSSage Weil /* ?? */ 6391d3576fdSSage Weil if (wbc->nonblocking && bdi_write_congested(bdi)) { 6401d3576fdSSage Weil dout(" writepages congested\n"); 6411d3576fdSSage Weil wbc->encountered_congestion = 1; 6421d3576fdSSage Weil goto out_final; 6431d3576fdSSage Weil } 6441d3576fdSSage Weil 6451d3576fdSSage Weil /* where to start/end? */ 6461d3576fdSSage Weil if (wbc->range_cyclic) { 6471d3576fdSSage Weil start = mapping->writeback_index; /* Start from prev offset */ 6481d3576fdSSage Weil end = -1; 6491d3576fdSSage Weil dout(" cyclic, start at %lu\n", start); 6501d3576fdSSage Weil } else { 6511d3576fdSSage Weil start = wbc->range_start >> PAGE_CACHE_SHIFT; 6521d3576fdSSage Weil end = wbc->range_end >> PAGE_CACHE_SHIFT; 6531d3576fdSSage Weil if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX) 6541d3576fdSSage Weil range_whole = 1; 6551d3576fdSSage Weil should_loop = 0; 6561d3576fdSSage Weil dout(" not cyclic, %lu to %lu\n", start, end); 6571d3576fdSSage Weil } 6581d3576fdSSage Weil index = start; 6591d3576fdSSage Weil 6601d3576fdSSage Weil retry: 6611d3576fdSSage Weil /* find oldest snap context with dirty data */ 6621d3576fdSSage Weil ceph_put_snap_context(snapc); 6631d3576fdSSage Weil snapc = get_oldest_context(inode, &snap_size); 6641d3576fdSSage Weil if (!snapc) { 6651d3576fdSSage Weil /* hmm, why does writepages get called when there 6661d3576fdSSage Weil is no dirty data? */ 6671d3576fdSSage Weil dout(" no snap context with dirty data?\n"); 6681d3576fdSSage Weil goto out; 6691d3576fdSSage Weil } 6701d3576fdSSage Weil dout(" oldest snapc is %p seq %lld (%d snaps)\n", 6711d3576fdSSage Weil snapc, snapc->seq, snapc->num_snaps); 6721d3576fdSSage Weil if (last_snapc && snapc != last_snapc) { 6731d3576fdSSage Weil /* if we switched to a newer snapc, restart our scan at the 6741d3576fdSSage Weil * start of the original file range. */ 6751d3576fdSSage Weil dout(" snapc differs from last pass, restarting at %lu\n", 6761d3576fdSSage Weil index); 6771d3576fdSSage Weil index = start; 6781d3576fdSSage Weil } 6791d3576fdSSage Weil last_snapc = snapc; 6801d3576fdSSage Weil 6811d3576fdSSage Weil while (!done && index <= end) { 6821d3576fdSSage Weil unsigned i; 6831d3576fdSSage Weil int first; 6841d3576fdSSage Weil pgoff_t next; 6851d3576fdSSage Weil int pvec_pages, locked_pages; 6861d3576fdSSage Weil struct page *page; 6871d3576fdSSage Weil int want; 6881d3576fdSSage Weil u64 offset, len; 6891d3576fdSSage Weil struct ceph_osd_request_head *reqhead; 6901d3576fdSSage Weil struct ceph_osd_op *op; 6912baba250SYehuda Sadeh long writeback_stat; 6921d3576fdSSage Weil 6931d3576fdSSage Weil next = 0; 6941d3576fdSSage Weil locked_pages = 0; 6951d3576fdSSage Weil max_pages = max_pages_ever; 6961d3576fdSSage Weil 6971d3576fdSSage Weil get_more_pages: 6981d3576fdSSage Weil first = -1; 6991d3576fdSSage Weil want = min(end - index, 7001d3576fdSSage Weil min((pgoff_t)PAGEVEC_SIZE, 7011d3576fdSSage Weil max_pages - (pgoff_t)locked_pages) - 1) 7021d3576fdSSage Weil + 1; 7031d3576fdSSage Weil pvec_pages = pagevec_lookup_tag(&pvec, mapping, &index, 7041d3576fdSSage Weil PAGECACHE_TAG_DIRTY, 7051d3576fdSSage Weil want); 7061d3576fdSSage Weil dout("pagevec_lookup_tag got %d\n", pvec_pages); 7071d3576fdSSage Weil if (!pvec_pages && !locked_pages) 7081d3576fdSSage Weil break; 7091d3576fdSSage Weil for (i = 0; i < pvec_pages && locked_pages < max_pages; i++) { 7101d3576fdSSage Weil page = pvec.pages[i]; 7111d3576fdSSage Weil dout("? %p idx %lu\n", page, page->index); 7121d3576fdSSage Weil if (locked_pages == 0) 7131d3576fdSSage Weil lock_page(page); /* first page */ 7141d3576fdSSage Weil else if (!trylock_page(page)) 7151d3576fdSSage Weil break; 7161d3576fdSSage Weil 7171d3576fdSSage Weil /* only dirty pages, or our accounting breaks */ 7181d3576fdSSage Weil if (unlikely(!PageDirty(page)) || 7191d3576fdSSage Weil unlikely(page->mapping != mapping)) { 7201d3576fdSSage Weil dout("!dirty or !mapping %p\n", page); 7211d3576fdSSage Weil unlock_page(page); 7221d3576fdSSage Weil break; 7231d3576fdSSage Weil } 7241d3576fdSSage Weil if (!wbc->range_cyclic && page->index > end) { 7251d3576fdSSage Weil dout("end of range %p\n", page); 7261d3576fdSSage Weil done = 1; 7271d3576fdSSage Weil unlock_page(page); 7281d3576fdSSage Weil break; 7291d3576fdSSage Weil } 7301d3576fdSSage Weil if (next && (page->index != next)) { 7311d3576fdSSage Weil dout("not consecutive %p\n", page); 7321d3576fdSSage Weil unlock_page(page); 7331d3576fdSSage Weil break; 7341d3576fdSSage Weil } 7351d3576fdSSage Weil if (wbc->sync_mode != WB_SYNC_NONE) { 7361d3576fdSSage Weil dout("waiting on writeback %p\n", page); 7371d3576fdSSage Weil wait_on_page_writeback(page); 7381d3576fdSSage Weil } 7391d3576fdSSage Weil if ((snap_size && page_offset(page) > snap_size) || 7401d3576fdSSage Weil (!snap_size && 7411d3576fdSSage Weil page_offset(page) > i_size_read(inode))) { 7421d3576fdSSage Weil dout("%p page eof %llu\n", page, snap_size ? 7431d3576fdSSage Weil snap_size : i_size_read(inode)); 7441d3576fdSSage Weil done = 1; 7451d3576fdSSage Weil unlock_page(page); 7461d3576fdSSage Weil break; 7471d3576fdSSage Weil } 7481d3576fdSSage Weil if (PageWriteback(page)) { 7491d3576fdSSage Weil dout("%p under writeback\n", page); 7501d3576fdSSage Weil unlock_page(page); 7511d3576fdSSage Weil break; 7521d3576fdSSage Weil } 7531d3576fdSSage Weil 7541d3576fdSSage Weil /* only if matching snap context */ 75580e755feSSage Weil pgsnapc = (void *)page->private; 75680e755feSSage Weil if (pgsnapc->seq > snapc->seq) { 75780e755feSSage Weil dout("page snapc %p %lld > oldest %p %lld\n", 75880e755feSSage Weil pgsnapc, pgsnapc->seq, snapc, snapc->seq); 7591d3576fdSSage Weil unlock_page(page); 7601d3576fdSSage Weil if (!locked_pages) 7611d3576fdSSage Weil continue; /* keep looking for snap */ 7621d3576fdSSage Weil break; 7631d3576fdSSage Weil } 7641d3576fdSSage Weil 7651d3576fdSSage Weil if (!clear_page_dirty_for_io(page)) { 7661d3576fdSSage Weil dout("%p !clear_page_dirty_for_io\n", page); 7671d3576fdSSage Weil unlock_page(page); 7681d3576fdSSage Weil break; 7691d3576fdSSage Weil } 7701d3576fdSSage Weil 7711d3576fdSSage Weil /* ok */ 7721d3576fdSSage Weil if (locked_pages == 0) { 7731d3576fdSSage Weil /* prepare async write request */ 7741d3576fdSSage Weil offset = page->index << PAGE_CACHE_SHIFT; 7751d3576fdSSage Weil len = wsize; 7761d3576fdSSage Weil req = ceph_osdc_new_request(&client->osdc, 7771d3576fdSSage Weil &ci->i_layout, 7781d3576fdSSage Weil ceph_vino(inode), 7791d3576fdSSage Weil offset, &len, 7801d3576fdSSage Weil CEPH_OSD_OP_WRITE, 7811d3576fdSSage Weil CEPH_OSD_FLAG_WRITE | 7821d3576fdSSage Weil CEPH_OSD_FLAG_ONDISK, 7831d3576fdSSage Weil snapc, do_sync, 7841d3576fdSSage Weil ci->i_truncate_seq, 7851d3576fdSSage Weil ci->i_truncate_size, 7861d3576fdSSage Weil &inode->i_mtime, true, 1); 7871d3576fdSSage Weil max_pages = req->r_num_pages; 7881d3576fdSSage Weil 7891d3576fdSSage Weil alloc_page_vec(client, req); 7901d3576fdSSage Weil req->r_callback = writepages_finish; 7911d3576fdSSage Weil req->r_inode = inode; 7921d3576fdSSage Weil } 7931d3576fdSSage Weil 7941d3576fdSSage Weil /* note position of first page in pvec */ 7951d3576fdSSage Weil if (first < 0) 7961d3576fdSSage Weil first = i; 7971d3576fdSSage Weil dout("%p will write page %p idx %lu\n", 7981d3576fdSSage Weil inode, page, page->index); 7992baba250SYehuda Sadeh 8002baba250SYehuda Sadeh writeback_stat = atomic_long_inc_return(&client->writeback_count); 8012baba250SYehuda Sadeh if (writeback_stat > CONGESTION_ON_THRESH(client->mount_args->congestion_kb)) { 8022baba250SYehuda Sadeh set_bdi_congested(&client->backing_dev_info, BLK_RW_ASYNC); 8032baba250SYehuda Sadeh } 8042baba250SYehuda Sadeh 8051d3576fdSSage Weil set_page_writeback(page); 8061d3576fdSSage Weil req->r_pages[locked_pages] = page; 8071d3576fdSSage Weil locked_pages++; 8081d3576fdSSage Weil next = page->index + 1; 8091d3576fdSSage Weil } 8101d3576fdSSage Weil 8111d3576fdSSage Weil /* did we get anything? */ 8121d3576fdSSage Weil if (!locked_pages) 8131d3576fdSSage Weil goto release_pvec_pages; 8141d3576fdSSage Weil if (i) { 8151d3576fdSSage Weil int j; 8161d3576fdSSage Weil BUG_ON(!locked_pages || first < 0); 8171d3576fdSSage Weil 8181d3576fdSSage Weil if (pvec_pages && i == pvec_pages && 8191d3576fdSSage Weil locked_pages < max_pages) { 8201d3576fdSSage Weil dout("reached end pvec, trying for more\n"); 8211d3576fdSSage Weil pagevec_reinit(&pvec); 8221d3576fdSSage Weil goto get_more_pages; 8231d3576fdSSage Weil } 8241d3576fdSSage Weil 8251d3576fdSSage Weil /* shift unused pages over in the pvec... we 8261d3576fdSSage Weil * will need to release them below. */ 8271d3576fdSSage Weil for (j = i; j < pvec_pages; j++) { 8281d3576fdSSage Weil dout(" pvec leftover page %p\n", 8291d3576fdSSage Weil pvec.pages[j]); 8301d3576fdSSage Weil pvec.pages[j-i+first] = pvec.pages[j]; 8311d3576fdSSage Weil } 8321d3576fdSSage Weil pvec.nr -= i-first; 8331d3576fdSSage Weil } 8341d3576fdSSage Weil 8351d3576fdSSage Weil /* submit the write */ 8361d3576fdSSage Weil offset = req->r_pages[0]->index << PAGE_CACHE_SHIFT; 8371d3576fdSSage Weil len = min((snap_size ? snap_size : i_size_read(inode)) - offset, 8381d3576fdSSage Weil (u64)locked_pages << PAGE_CACHE_SHIFT); 8391d3576fdSSage Weil dout("writepages got %d pages at %llu~%llu\n", 8401d3576fdSSage Weil locked_pages, offset, len); 8411d3576fdSSage Weil 8421d3576fdSSage Weil /* revise final length, page count */ 8431d3576fdSSage Weil req->r_num_pages = locked_pages; 8441d3576fdSSage Weil reqhead = req->r_request->front.iov_base; 8451d3576fdSSage Weil op = (void *)(reqhead + 1); 8461d3576fdSSage Weil op->extent.length = cpu_to_le64(len); 8471d3576fdSSage Weil op->payload_len = cpu_to_le32(len); 8481d3576fdSSage Weil req->r_request->hdr.data_len = cpu_to_le32(len); 8491d3576fdSSage Weil 8501d3576fdSSage Weil ceph_osdc_start_request(&client->osdc, req, true); 8511d3576fdSSage Weil req = NULL; 8521d3576fdSSage Weil 8531d3576fdSSage Weil /* continue? */ 8541d3576fdSSage Weil index = next; 8551d3576fdSSage Weil wbc->nr_to_write -= locked_pages; 8561d3576fdSSage Weil if (wbc->nr_to_write <= 0) 8571d3576fdSSage Weil done = 1; 8581d3576fdSSage Weil 8591d3576fdSSage Weil release_pvec_pages: 8601d3576fdSSage Weil dout("pagevec_release on %d pages (%p)\n", (int)pvec.nr, 8611d3576fdSSage Weil pvec.nr ? pvec.pages[0] : NULL); 8621d3576fdSSage Weil pagevec_release(&pvec); 8631d3576fdSSage Weil 8641d3576fdSSage Weil if (locked_pages && !done) 8651d3576fdSSage Weil goto retry; 8661d3576fdSSage Weil } 8671d3576fdSSage Weil 8681d3576fdSSage Weil if (should_loop && !done) { 8691d3576fdSSage Weil /* more to do; loop back to beginning of file */ 8701d3576fdSSage Weil dout("writepages looping back to beginning of file\n"); 8711d3576fdSSage Weil should_loop = 0; 8721d3576fdSSage Weil index = 0; 8731d3576fdSSage Weil goto retry; 8741d3576fdSSage Weil } 8751d3576fdSSage Weil 8761d3576fdSSage Weil if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0)) 8771d3576fdSSage Weil mapping->writeback_index = index; 8781d3576fdSSage Weil 8791d3576fdSSage Weil out: 8801d3576fdSSage Weil if (req) 8811d3576fdSSage Weil ceph_osdc_put_request(req); 8821d3576fdSSage Weil if (rc > 0) 8831d3576fdSSage Weil rc = 0; /* vfs expects us to return 0 */ 8841d3576fdSSage Weil ceph_put_snap_context(snapc); 8851d3576fdSSage Weil dout("writepages done, rc = %d\n", rc); 8861d3576fdSSage Weil out_final: 8871d3576fdSSage Weil return rc; 8881d3576fdSSage Weil } 8891d3576fdSSage Weil 8901d3576fdSSage Weil 8911d3576fdSSage Weil 8921d3576fdSSage Weil /* 8931d3576fdSSage Weil * See if a given @snapc is either writeable, or already written. 8941d3576fdSSage Weil */ 8951d3576fdSSage Weil static int context_is_writeable_or_written(struct inode *inode, 8961d3576fdSSage Weil struct ceph_snap_context *snapc) 8971d3576fdSSage Weil { 8981d3576fdSSage Weil struct ceph_snap_context *oldest = get_oldest_context(inode, NULL); 8996298a337SSage Weil int ret = !oldest || snapc->seq <= oldest->seq; 9006298a337SSage Weil 9016298a337SSage Weil ceph_put_snap_context(oldest); 9026298a337SSage Weil return ret; 9031d3576fdSSage Weil } 9041d3576fdSSage Weil 9051d3576fdSSage Weil /* 9061d3576fdSSage Weil * We are only allowed to write into/dirty the page if the page is 9071d3576fdSSage Weil * clean, or already dirty within the same snap context. 9088f883c24SSage Weil * 9098f883c24SSage Weil * called with page locked. 9108f883c24SSage Weil * return success with page locked, 9118f883c24SSage Weil * or any failure (incl -EAGAIN) with page unlocked. 9121d3576fdSSage Weil */ 9134af6b225SYehuda Sadeh static int ceph_update_writeable_page(struct file *file, 9144af6b225SYehuda Sadeh loff_t pos, unsigned len, 9154af6b225SYehuda Sadeh struct page *page) 9161d3576fdSSage Weil { 9171d3576fdSSage Weil struct inode *inode = file->f_dentry->d_inode; 9181d3576fdSSage Weil struct ceph_inode_info *ci = ceph_inode(inode); 9191d3576fdSSage Weil struct ceph_mds_client *mdsc = &ceph_inode_to_client(inode)->mdsc; 9201d3576fdSSage Weil loff_t page_off = pos & PAGE_CACHE_MASK; 9211d3576fdSSage Weil int pos_in_page = pos & ~PAGE_CACHE_MASK; 9221d3576fdSSage Weil int end_in_page = pos_in_page + len; 9231d3576fdSSage Weil loff_t i_size; 9241d3576fdSSage Weil int r; 92580e755feSSage Weil struct ceph_snap_context *snapc, *oldest; 9261d3576fdSSage Weil 9271d3576fdSSage Weil retry_locked: 9281d3576fdSSage Weil /* writepages currently holds page lock, but if we change that later, */ 9291d3576fdSSage Weil wait_on_page_writeback(page); 9301d3576fdSSage Weil 9311d3576fdSSage Weil /* check snap context */ 9321d3576fdSSage Weil BUG_ON(!ci->i_snap_realm); 9331d3576fdSSage Weil down_read(&mdsc->snap_rwsem); 9341d3576fdSSage Weil BUG_ON(!ci->i_snap_realm->cached_context); 93580e755feSSage Weil snapc = (void *)page->private; 93680e755feSSage Weil if (snapc && snapc != ci->i_head_snapc) { 9371d3576fdSSage Weil /* 9381d3576fdSSage Weil * this page is already dirty in another (older) snap 9391d3576fdSSage Weil * context! is it writeable now? 9401d3576fdSSage Weil */ 94180e755feSSage Weil oldest = get_oldest_context(inode, NULL); 9421d3576fdSSage Weil up_read(&mdsc->snap_rwsem); 9431d3576fdSSage Weil 94480e755feSSage Weil if (snapc->seq > oldest->seq) { 9456298a337SSage Weil ceph_put_snap_context(oldest); 9461d3576fdSSage Weil dout(" page %p snapc %p not current or oldest\n", 9476298a337SSage Weil page, snapc); 9481d3576fdSSage Weil /* 9491d3576fdSSage Weil * queue for writeback, and wait for snapc to 9501d3576fdSSage Weil * be writeable or written 9511d3576fdSSage Weil */ 9526298a337SSage Weil snapc = ceph_get_snap_context(snapc); 9531d3576fdSSage Weil unlock_page(page); 9543c6f6b79SSage Weil ceph_queue_writeback(inode); 9558f883c24SSage Weil r = wait_event_interruptible(ci->i_cap_wq, 9561d3576fdSSage Weil context_is_writeable_or_written(inode, snapc)); 9571d3576fdSSage Weil ceph_put_snap_context(snapc); 9588f883c24SSage Weil if (r == -ERESTARTSYS) 9598f883c24SSage Weil return r; 9604af6b225SYehuda Sadeh return -EAGAIN; 9611d3576fdSSage Weil } 9626298a337SSage Weil ceph_put_snap_context(oldest); 9631d3576fdSSage Weil 9641d3576fdSSage Weil /* yay, writeable, do it now (without dropping page lock) */ 9651d3576fdSSage Weil dout(" page %p snapc %p not current, but oldest\n", 9661d3576fdSSage Weil page, snapc); 9671d3576fdSSage Weil if (!clear_page_dirty_for_io(page)) 9681d3576fdSSage Weil goto retry_locked; 9691d3576fdSSage Weil r = writepage_nounlock(page, NULL); 9701d3576fdSSage Weil if (r < 0) 9711d3576fdSSage Weil goto fail_nosnap; 9721d3576fdSSage Weil goto retry_locked; 9731d3576fdSSage Weil } 9741d3576fdSSage Weil 9751d3576fdSSage Weil if (PageUptodate(page)) { 9761d3576fdSSage Weil dout(" page %p already uptodate\n", page); 9771d3576fdSSage Weil return 0; 9781d3576fdSSage Weil } 9791d3576fdSSage Weil 9801d3576fdSSage Weil /* full page? */ 9811d3576fdSSage Weil if (pos_in_page == 0 && len == PAGE_CACHE_SIZE) 9821d3576fdSSage Weil return 0; 9831d3576fdSSage Weil 9841d3576fdSSage Weil /* past end of file? */ 9851d3576fdSSage Weil i_size = inode->i_size; /* caller holds i_mutex */ 9861d3576fdSSage Weil 9871d3576fdSSage Weil if (i_size + len > inode->i_sb->s_maxbytes) { 9881d3576fdSSage Weil /* file is too big */ 9891d3576fdSSage Weil r = -EINVAL; 9901d3576fdSSage Weil goto fail; 9911d3576fdSSage Weil } 9921d3576fdSSage Weil 9931d3576fdSSage Weil if (page_off >= i_size || 9941d3576fdSSage Weil (pos_in_page == 0 && (pos+len) >= i_size && 9951d3576fdSSage Weil end_in_page - pos_in_page != PAGE_CACHE_SIZE)) { 9961d3576fdSSage Weil dout(" zeroing %p 0 - %d and %d - %d\n", 9971d3576fdSSage Weil page, pos_in_page, end_in_page, (int)PAGE_CACHE_SIZE); 9981d3576fdSSage Weil zero_user_segments(page, 9991d3576fdSSage Weil 0, pos_in_page, 10001d3576fdSSage Weil end_in_page, PAGE_CACHE_SIZE); 10011d3576fdSSage Weil return 0; 10021d3576fdSSage Weil } 10031d3576fdSSage Weil 10041d3576fdSSage Weil /* we need to read it. */ 10051d3576fdSSage Weil up_read(&mdsc->snap_rwsem); 10061d3576fdSSage Weil r = readpage_nounlock(file, page); 10071d3576fdSSage Weil if (r < 0) 10081d3576fdSSage Weil goto fail_nosnap; 10091d3576fdSSage Weil goto retry_locked; 10101d3576fdSSage Weil 10111d3576fdSSage Weil fail: 10121d3576fdSSage Weil up_read(&mdsc->snap_rwsem); 10131d3576fdSSage Weil fail_nosnap: 10141d3576fdSSage Weil unlock_page(page); 10151d3576fdSSage Weil return r; 10161d3576fdSSage Weil } 10171d3576fdSSage Weil 10181d3576fdSSage Weil /* 10194af6b225SYehuda Sadeh * We are only allowed to write into/dirty the page if the page is 10204af6b225SYehuda Sadeh * clean, or already dirty within the same snap context. 10214af6b225SYehuda Sadeh */ 10224af6b225SYehuda Sadeh static int ceph_write_begin(struct file *file, struct address_space *mapping, 10234af6b225SYehuda Sadeh loff_t pos, unsigned len, unsigned flags, 10244af6b225SYehuda Sadeh struct page **pagep, void **fsdata) 10254af6b225SYehuda Sadeh { 10264af6b225SYehuda Sadeh struct inode *inode = file->f_dentry->d_inode; 10274af6b225SYehuda Sadeh struct page *page; 10284af6b225SYehuda Sadeh pgoff_t index = pos >> PAGE_CACHE_SHIFT; 10294af6b225SYehuda Sadeh int r; 10304af6b225SYehuda Sadeh 10314af6b225SYehuda Sadeh do { 10324af6b225SYehuda Sadeh /* get a page */ 10334af6b225SYehuda Sadeh page = grab_cache_page_write_begin(mapping, index, 0); 10344af6b225SYehuda Sadeh if (!page) 10354af6b225SYehuda Sadeh return -ENOMEM; 10364af6b225SYehuda Sadeh *pagep = page; 10374af6b225SYehuda Sadeh 10384af6b225SYehuda Sadeh dout("write_begin file %p inode %p page %p %d~%d\n", file, 10394af6b225SYehuda Sadeh inode, page, (int)pos, (int)len); 10404af6b225SYehuda Sadeh 10414af6b225SYehuda Sadeh r = ceph_update_writeable_page(file, pos, len, page); 10424af6b225SYehuda Sadeh } while (r == -EAGAIN); 10434af6b225SYehuda Sadeh 10444af6b225SYehuda Sadeh return r; 10454af6b225SYehuda Sadeh } 10464af6b225SYehuda Sadeh 10474af6b225SYehuda Sadeh /* 10481d3576fdSSage Weil * we don't do anything in here that simple_write_end doesn't do 10491d3576fdSSage Weil * except adjust dirty page accounting and drop read lock on 10501d3576fdSSage Weil * mdsc->snap_rwsem. 10511d3576fdSSage Weil */ 10521d3576fdSSage Weil static int ceph_write_end(struct file *file, struct address_space *mapping, 10531d3576fdSSage Weil loff_t pos, unsigned len, unsigned copied, 10541d3576fdSSage Weil struct page *page, void *fsdata) 10551d3576fdSSage Weil { 10561d3576fdSSage Weil struct inode *inode = file->f_dentry->d_inode; 10572baba250SYehuda Sadeh struct ceph_client *client = ceph_inode_to_client(inode); 10582baba250SYehuda Sadeh struct ceph_mds_client *mdsc = &client->mdsc; 10591d3576fdSSage Weil unsigned from = pos & (PAGE_CACHE_SIZE - 1); 10601d3576fdSSage Weil int check_cap = 0; 10611d3576fdSSage Weil 10621d3576fdSSage Weil dout("write_end file %p inode %p page %p %d~%d (%d)\n", file, 10631d3576fdSSage Weil inode, page, (int)pos, (int)copied, (int)len); 10641d3576fdSSage Weil 10651d3576fdSSage Weil /* zero the stale part of the page if we did a short copy */ 10661d3576fdSSage Weil if (copied < len) 10671d3576fdSSage Weil zero_user_segment(page, from+copied, len); 10681d3576fdSSage Weil 10691d3576fdSSage Weil /* did file size increase? */ 10701d3576fdSSage Weil /* (no need for i_size_read(); we caller holds i_mutex */ 10711d3576fdSSage Weil if (pos+copied > inode->i_size) 10721d3576fdSSage Weil check_cap = ceph_inode_set_size(inode, pos+copied); 10731d3576fdSSage Weil 10741d3576fdSSage Weil if (!PageUptodate(page)) 10751d3576fdSSage Weil SetPageUptodate(page); 10761d3576fdSSage Weil 10771d3576fdSSage Weil set_page_dirty(page); 10781d3576fdSSage Weil 10791d3576fdSSage Weil unlock_page(page); 10801d3576fdSSage Weil up_read(&mdsc->snap_rwsem); 10811d3576fdSSage Weil page_cache_release(page); 10821d3576fdSSage Weil 10831d3576fdSSage Weil if (check_cap) 10841d3576fdSSage Weil ceph_check_caps(ceph_inode(inode), CHECK_CAPS_AUTHONLY, NULL); 10851d3576fdSSage Weil 10861d3576fdSSage Weil return copied; 10871d3576fdSSage Weil } 10881d3576fdSSage Weil 10891d3576fdSSage Weil /* 10901d3576fdSSage Weil * we set .direct_IO to indicate direct io is supported, but since we 10911d3576fdSSage Weil * intercept O_DIRECT reads and writes early, this function should 10921d3576fdSSage Weil * never get called. 10931d3576fdSSage Weil */ 10941d3576fdSSage Weil static ssize_t ceph_direct_io(int rw, struct kiocb *iocb, 10951d3576fdSSage Weil const struct iovec *iov, 10961d3576fdSSage Weil loff_t pos, unsigned long nr_segs) 10971d3576fdSSage Weil { 10981d3576fdSSage Weil WARN_ON(1); 10991d3576fdSSage Weil return -EINVAL; 11001d3576fdSSage Weil } 11011d3576fdSSage Weil 11021d3576fdSSage Weil const struct address_space_operations ceph_aops = { 11031d3576fdSSage Weil .readpage = ceph_readpage, 11041d3576fdSSage Weil .readpages = ceph_readpages, 11051d3576fdSSage Weil .writepage = ceph_writepage, 11061d3576fdSSage Weil .writepages = ceph_writepages_start, 11071d3576fdSSage Weil .write_begin = ceph_write_begin, 11081d3576fdSSage Weil .write_end = ceph_write_end, 11091d3576fdSSage Weil .set_page_dirty = ceph_set_page_dirty, 11101d3576fdSSage Weil .invalidatepage = ceph_invalidatepage, 11111d3576fdSSage Weil .releasepage = ceph_releasepage, 11121d3576fdSSage Weil .direct_IO = ceph_direct_io, 11131d3576fdSSage Weil }; 11141d3576fdSSage Weil 11151d3576fdSSage Weil 11161d3576fdSSage Weil /* 11171d3576fdSSage Weil * vm ops 11181d3576fdSSage Weil */ 11191d3576fdSSage Weil 11201d3576fdSSage Weil /* 11211d3576fdSSage Weil * Reuse write_begin here for simplicity. 11221d3576fdSSage Weil */ 11231d3576fdSSage Weil static int ceph_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf) 11241d3576fdSSage Weil { 11251d3576fdSSage Weil struct inode *inode = vma->vm_file->f_dentry->d_inode; 11261d3576fdSSage Weil struct page *page = vmf->page; 11271d3576fdSSage Weil struct ceph_mds_client *mdsc = &ceph_inode_to_client(inode)->mdsc; 11281d3576fdSSage Weil loff_t off = page->index << PAGE_CACHE_SHIFT; 11291d3576fdSSage Weil loff_t size, len; 11301d3576fdSSage Weil int ret; 11311d3576fdSSage Weil 11321d3576fdSSage Weil size = i_size_read(inode); 11331d3576fdSSage Weil if (off + PAGE_CACHE_SIZE <= size) 11341d3576fdSSage Weil len = PAGE_CACHE_SIZE; 11351d3576fdSSage Weil else 11361d3576fdSSage Weil len = size & ~PAGE_CACHE_MASK; 11371d3576fdSSage Weil 11381d3576fdSSage Weil dout("page_mkwrite %p %llu~%llu page %p idx %lu\n", inode, 11391d3576fdSSage Weil off, len, page, page->index); 11404af6b225SYehuda Sadeh 11414af6b225SYehuda Sadeh lock_page(page); 11424af6b225SYehuda Sadeh 11434af6b225SYehuda Sadeh ret = VM_FAULT_NOPAGE; 11444af6b225SYehuda Sadeh if ((off > size) || 11454af6b225SYehuda Sadeh (page->mapping != inode->i_mapping)) 11464af6b225SYehuda Sadeh goto out; 11474af6b225SYehuda Sadeh 11484af6b225SYehuda Sadeh ret = ceph_update_writeable_page(vma->vm_file, off, len, page); 11494af6b225SYehuda Sadeh if (ret == 0) { 11504af6b225SYehuda Sadeh /* success. we'll keep the page locked. */ 11511d3576fdSSage Weil set_page_dirty(page); 11521d3576fdSSage Weil up_read(&mdsc->snap_rwsem); 11531d3576fdSSage Weil ret = VM_FAULT_LOCKED; 11541d3576fdSSage Weil } else { 11554af6b225SYehuda Sadeh if (ret == -ENOMEM) 11564af6b225SYehuda Sadeh ret = VM_FAULT_OOM; 11574af6b225SYehuda Sadeh else 11581d3576fdSSage Weil ret = VM_FAULT_SIGBUS; 11591d3576fdSSage Weil } 11604af6b225SYehuda Sadeh out: 11611d3576fdSSage Weil dout("page_mkwrite %p %llu~%llu = %d\n", inode, off, len, ret); 11624af6b225SYehuda Sadeh if (ret != VM_FAULT_LOCKED) 11634af6b225SYehuda Sadeh unlock_page(page); 11641d3576fdSSage Weil return ret; 11651d3576fdSSage Weil } 11661d3576fdSSage Weil 11671d3576fdSSage Weil static struct vm_operations_struct ceph_vmops = { 11681d3576fdSSage Weil .fault = filemap_fault, 11691d3576fdSSage Weil .page_mkwrite = ceph_page_mkwrite, 11701d3576fdSSage Weil }; 11711d3576fdSSage Weil 11721d3576fdSSage Weil int ceph_mmap(struct file *file, struct vm_area_struct *vma) 11731d3576fdSSage Weil { 11741d3576fdSSage Weil struct address_space *mapping = file->f_mapping; 11751d3576fdSSage Weil 11761d3576fdSSage Weil if (!mapping->a_ops->readpage) 11771d3576fdSSage Weil return -ENOEXEC; 11781d3576fdSSage Weil file_accessed(file); 11791d3576fdSSage Weil vma->vm_ops = &ceph_vmops; 11801d3576fdSSage Weil vma->vm_flags |= VM_CAN_NONLINEAR; 11811d3576fdSSage Weil return 0; 11821d3576fdSSage Weil } 1183