1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0 23d14c5d2SYehuda Sadeh #include <linux/ceph/ceph_debug.h> 31d3576fdSSage Weil 41d3576fdSSage Weil #include <linux/backing-dev.h> 51d3576fdSSage Weil #include <linux/fs.h> 61d3576fdSSage Weil #include <linux/mm.h> 71d3576fdSSage Weil #include <linux/pagemap.h> 81d3576fdSSage Weil #include <linux/writeback.h> /* generic_writepages */ 95a0e3ad6STejun Heo #include <linux/slab.h> 101d3576fdSSage Weil #include <linux/pagevec.h> 111d3576fdSSage Weil #include <linux/task_io_accounting_ops.h> 12f361bf4aSIngo Molnar #include <linux/signal.h> 131d3576fdSSage Weil 141d3576fdSSage Weil #include "super.h" 153d14c5d2SYehuda Sadeh #include "mds_client.h" 1699ccbd22SMilosz Tanski #include "cache.h" 173d14c5d2SYehuda Sadeh #include <linux/ceph/osd_client.h> 1808c1ac50SIlya Dryomov #include <linux/ceph/striper.h> 191d3576fdSSage Weil 201d3576fdSSage Weil /* 211d3576fdSSage Weil * Ceph address space ops. 221d3576fdSSage Weil * 231d3576fdSSage Weil * There are a few funny things going on here. 241d3576fdSSage Weil * 251d3576fdSSage Weil * The page->private field is used to reference a struct 261d3576fdSSage Weil * ceph_snap_context for _every_ dirty page. This indicates which 271d3576fdSSage Weil * snapshot the page was logically dirtied in, and thus which snap 281d3576fdSSage Weil * context needs to be associated with the osd write during writeback. 291d3576fdSSage Weil * 301d3576fdSSage Weil * Similarly, struct ceph_inode_info maintains a set of counters to 3125985edcSLucas De Marchi * count dirty pages on the inode. In the absence of snapshots, 321d3576fdSSage Weil * i_wrbuffer_ref == i_wrbuffer_ref_head == the dirty page count. 331d3576fdSSage Weil * 341d3576fdSSage Weil * When a snapshot is taken (that is, when the client receives 351d3576fdSSage Weil * notification that a snapshot was taken), each inode with caps and 361d3576fdSSage Weil * with dirty pages (dirty pages implies there is a cap) gets a new 371d3576fdSSage Weil * ceph_cap_snap in the i_cap_snaps list (which is sorted in ascending 381d3576fdSSage Weil * order, new snaps go to the tail). The i_wrbuffer_ref_head count is 391d3576fdSSage Weil * moved to capsnap->dirty. (Unless a sync write is currently in 401d3576fdSSage Weil * progress. In that case, the capsnap is said to be "pending", new 411d3576fdSSage Weil * writes cannot start, and the capsnap isn't "finalized" until the 421d3576fdSSage Weil * write completes (or fails) and a final size/mtime for the inode for 431d3576fdSSage Weil * that snap can be settled upon.) i_wrbuffer_ref_head is reset to 0. 441d3576fdSSage Weil * 451d3576fdSSage Weil * On writeback, we must submit writes to the osd IN SNAP ORDER. So, 461d3576fdSSage Weil * we look for the first capsnap in i_cap_snaps and write out pages in 471d3576fdSSage Weil * that snap context _only_. Then we move on to the next capsnap, 481d3576fdSSage Weil * eventually reaching the "live" or "head" context (i.e., pages that 491d3576fdSSage Weil * are not yet snapped) and are writing the most recently dirtied 501d3576fdSSage Weil * pages. 511d3576fdSSage Weil * 521d3576fdSSage Weil * Invalidate and so forth must take care to ensure the dirty page 531d3576fdSSage Weil * accounting is preserved. 541d3576fdSSage Weil */ 551d3576fdSSage Weil 562baba250SYehuda Sadeh #define CONGESTION_ON_THRESH(congestion_kb) (congestion_kb >> (PAGE_SHIFT-10)) 572baba250SYehuda Sadeh #define CONGESTION_OFF_THRESH(congestion_kb) \ 582baba250SYehuda Sadeh (CONGESTION_ON_THRESH(congestion_kb) - \ 592baba250SYehuda Sadeh (CONGESTION_ON_THRESH(congestion_kb) >> 2)) 602baba250SYehuda Sadeh 6161600ef8SYan, Zheng static inline struct ceph_snap_context *page_snap_context(struct page *page) 6261600ef8SYan, Zheng { 6361600ef8SYan, Zheng if (PagePrivate(page)) 6461600ef8SYan, Zheng return (void *)page->private; 6561600ef8SYan, Zheng return NULL; 6661600ef8SYan, Zheng } 671d3576fdSSage Weil 681d3576fdSSage Weil /* 691d3576fdSSage Weil * Dirty a page. Optimistically adjust accounting, on the assumption 701d3576fdSSage Weil * that we won't race with invalidate. If we do, readjust. 711d3576fdSSage Weil */ 721d3576fdSSage Weil static int ceph_set_page_dirty(struct page *page) 731d3576fdSSage Weil { 741d3576fdSSage Weil struct address_space *mapping = page->mapping; 751d3576fdSSage Weil struct inode *inode; 761d3576fdSSage Weil struct ceph_inode_info *ci; 771d3576fdSSage Weil struct ceph_snap_context *snapc; 787d6e1f54SSha Zhengju int ret; 791d3576fdSSage Weil 801d3576fdSSage Weil if (unlikely(!mapping)) 811d3576fdSSage Weil return !TestSetPageDirty(page); 821d3576fdSSage Weil 837d6e1f54SSha Zhengju if (PageDirty(page)) { 841d3576fdSSage Weil dout("%p set_page_dirty %p idx %lu -- already dirty\n", 851d3576fdSSage Weil mapping->host, page, page->index); 867d6e1f54SSha Zhengju BUG_ON(!PagePrivate(page)); 871d3576fdSSage Weil return 0; 881d3576fdSSage Weil } 891d3576fdSSage Weil 901d3576fdSSage Weil inode = mapping->host; 911d3576fdSSage Weil ci = ceph_inode(inode); 921d3576fdSSage Weil 931d3576fdSSage Weil /* dirty the head */ 94be655596SSage Weil spin_lock(&ci->i_ceph_lock); 955dda377cSYan, Zheng BUG_ON(ci->i_wr_ref == 0); // caller should hold Fw reference 965dda377cSYan, Zheng if (__ceph_have_pending_cap_snap(ci)) { 975dda377cSYan, Zheng struct ceph_cap_snap *capsnap = 985dda377cSYan, Zheng list_last_entry(&ci->i_cap_snaps, 995dda377cSYan, Zheng struct ceph_cap_snap, 1005dda377cSYan, Zheng ci_item); 1015dda377cSYan, Zheng snapc = ceph_get_snap_context(capsnap->context); 1025dda377cSYan, Zheng capsnap->dirty_pages++; 1035dda377cSYan, Zheng } else { 1045dda377cSYan, Zheng BUG_ON(!ci->i_head_snapc); 1055dda377cSYan, Zheng snapc = ceph_get_snap_context(ci->i_head_snapc); 1061d3576fdSSage Weil ++ci->i_wrbuffer_ref_head; 1075dda377cSYan, Zheng } 1081d3576fdSSage Weil if (ci->i_wrbuffer_ref == 0) 1090444d76aSDave Chinner ihold(inode); 1101d3576fdSSage Weil ++ci->i_wrbuffer_ref; 1111d3576fdSSage Weil dout("%p set_page_dirty %p idx %lu head %d/%d -> %d/%d " 1121d3576fdSSage Weil "snapc %p seq %lld (%d snaps)\n", 1131d3576fdSSage Weil mapping->host, page, page->index, 1141d3576fdSSage Weil ci->i_wrbuffer_ref-1, ci->i_wrbuffer_ref_head-1, 1151d3576fdSSage Weil ci->i_wrbuffer_ref, ci->i_wrbuffer_ref_head, 1161d3576fdSSage Weil snapc, snapc->seq, snapc->num_snaps); 117be655596SSage Weil spin_unlock(&ci->i_ceph_lock); 1181d3576fdSSage Weil 1191d3576fdSSage Weil /* 1201d3576fdSSage Weil * Reference snap context in page->private. Also set 1211d3576fdSSage Weil * PagePrivate so that we get invalidatepage callback. 1221d3576fdSSage Weil */ 1237d6e1f54SSha Zhengju BUG_ON(PagePrivate(page)); 1241d3576fdSSage Weil page->private = (unsigned long)snapc; 1251d3576fdSSage Weil SetPagePrivate(page); 1261d3576fdSSage Weil 1277d6e1f54SSha Zhengju ret = __set_page_dirty_nobuffers(page); 1287d6e1f54SSha Zhengju WARN_ON(!PageLocked(page)); 1297d6e1f54SSha Zhengju WARN_ON(!page->mapping); 1301d3576fdSSage Weil 1317d6e1f54SSha Zhengju return ret; 1321d3576fdSSage Weil } 1331d3576fdSSage Weil 1341d3576fdSSage Weil /* 1351d3576fdSSage Weil * If we are truncating the full page (i.e. offset == 0), adjust the 1361d3576fdSSage Weil * dirty page counters appropriately. Only called if there is private 1371d3576fdSSage Weil * data on the page. 1381d3576fdSSage Weil */ 139d47992f8SLukas Czerner static void ceph_invalidatepage(struct page *page, unsigned int offset, 140d47992f8SLukas Czerner unsigned int length) 1411d3576fdSSage Weil { 1424ce1e9adSAlexander Beregalov struct inode *inode; 1431d3576fdSSage Weil struct ceph_inode_info *ci; 14461600ef8SYan, Zheng struct ceph_snap_context *snapc = page_snap_context(page); 1451d3576fdSSage Weil 1464ce1e9adSAlexander Beregalov inode = page->mapping->host; 147b150f5c1SMilosz Tanski ci = ceph_inode(inode); 148b150f5c1SMilosz Tanski 14909cbfeafSKirill A. Shutemov if (offset != 0 || length != PAGE_SIZE) { 150b150f5c1SMilosz Tanski dout("%p invalidatepage %p idx %lu partial dirty page %u~%u\n", 151b150f5c1SMilosz Tanski inode, page, page->index, offset, length); 152b150f5c1SMilosz Tanski return; 153b150f5c1SMilosz Tanski } 1544ce1e9adSAlexander Beregalov 15599ccbd22SMilosz Tanski ceph_invalidate_fscache_page(inode, page); 15699ccbd22SMilosz Tanski 157b072d774SYan, Zheng WARN_ON(!PageLocked(page)); 15899ccbd22SMilosz Tanski if (!PagePrivate(page)) 15999ccbd22SMilosz Tanski return; 16099ccbd22SMilosz Tanski 1611d3576fdSSage Weil ClearPageChecked(page); 1621d3576fdSSage Weil 163569d39fcSLukas Czerner dout("%p invalidatepage %p idx %lu full dirty page\n", 164569d39fcSLukas Czerner inode, page, page->index); 165b150f5c1SMilosz Tanski 1661d3576fdSSage Weil ceph_put_wrbuffer_cap_refs(ci, 1, snapc); 1671d3576fdSSage Weil ceph_put_snap_context(snapc); 1681d3576fdSSage Weil page->private = 0; 1691d3576fdSSage Weil ClearPagePrivate(page); 1701d3576fdSSage Weil } 1711d3576fdSSage Weil 1721d3576fdSSage Weil static int ceph_releasepage(struct page *page, gfp_t g) 1731d3576fdSSage Weil { 174e55f1a18SNeilBrown dout("%p releasepage %p idx %lu (%sdirty)\n", page->mapping->host, 175e55f1a18SNeilBrown page, page->index, PageDirty(page) ? "" : "not "); 17699ccbd22SMilosz Tanski 17799ccbd22SMilosz Tanski /* Can we release the page from the cache? */ 17899ccbd22SMilosz Tanski if (!ceph_release_fscache_page(page, g)) 1791d3576fdSSage Weil return 0; 18099ccbd22SMilosz Tanski 18199ccbd22SMilosz Tanski return !PagePrivate(page); 1821d3576fdSSage Weil } 1831d3576fdSSage Weil 1841d3576fdSSage Weil /* 1851d3576fdSSage Weil * read a single page, without unlocking it. 1861d3576fdSSage Weil */ 187dd2bc473SYan, Zheng static int ceph_do_readpage(struct file *filp, struct page *page) 1881d3576fdSSage Weil { 189496ad9aaSAl Viro struct inode *inode = file_inode(filp); 1901d3576fdSSage Weil struct ceph_inode_info *ci = ceph_inode(inode); 1913d14c5d2SYehuda Sadeh struct ceph_osd_client *osdc = 1923d14c5d2SYehuda Sadeh &ceph_inode_to_client(inode)->client->osdc; 1931d3576fdSSage Weil int err = 0; 19483701246SYan, Zheng u64 off = page_offset(page); 19509cbfeafSKirill A. Shutemov u64 len = PAGE_SIZE; 1961d3576fdSSage Weil 19783701246SYan, Zheng if (off >= i_size_read(inode)) { 19809cbfeafSKirill A. Shutemov zero_user_segment(page, 0, PAGE_SIZE); 19983701246SYan, Zheng SetPageUptodate(page); 20083701246SYan, Zheng return 0; 20183701246SYan, Zheng } 20299ccbd22SMilosz Tanski 203fcc02d2aSYan, Zheng if (ci->i_inline_version != CEPH_INLINE_NONE) { 20483701246SYan, Zheng /* 205fcc02d2aSYan, Zheng * Uptodate inline data should have been added 206fcc02d2aSYan, Zheng * into page cache while getting Fcr caps. 20783701246SYan, Zheng */ 208fcc02d2aSYan, Zheng if (off == 0) 20983701246SYan, Zheng return -EINVAL; 21009cbfeafSKirill A. Shutemov zero_user_segment(page, 0, PAGE_SIZE); 211fcc02d2aSYan, Zheng SetPageUptodate(page); 212fcc02d2aSYan, Zheng return 0; 213fcc02d2aSYan, Zheng } 21483701246SYan, Zheng 21583701246SYan, Zheng err = ceph_readpage_from_fscache(inode, page); 21699ccbd22SMilosz Tanski if (err == 0) 217dd2bc473SYan, Zheng return -EINPROGRESS; 21899ccbd22SMilosz Tanski 2191d3576fdSSage Weil dout("readpage inode %p file %p page %p index %lu\n", 2201d3576fdSSage Weil inode, filp, page, page->index); 2211d3576fdSSage Weil err = ceph_osdc_readpages(osdc, ceph_vino(inode), &ci->i_layout, 22283701246SYan, Zheng off, &len, 2231d3576fdSSage Weil ci->i_truncate_seq, ci->i_truncate_size, 224b7495fc2SSage Weil &page, 1, 0); 2251d3576fdSSage Weil if (err == -ENOENT) 2261d3576fdSSage Weil err = 0; 2271d3576fdSSage Weil if (err < 0) { 2281d3576fdSSage Weil SetPageError(page); 22918302805SLi Wang ceph_fscache_readpage_cancel(inode, page); 2301d3576fdSSage Weil goto out; 23123cd573bSZhang Zhen } 23209cbfeafSKirill A. Shutemov if (err < PAGE_SIZE) 2331d3576fdSSage Weil /* zero fill remainder of page */ 23409cbfeafSKirill A. Shutemov zero_user_segment(page, err, PAGE_SIZE); 23523cd573bSZhang Zhen else 23656f91aadSLi Wang flush_dcache_page(page); 2371d3576fdSSage Weil 23823cd573bSZhang Zhen SetPageUptodate(page); 23999ccbd22SMilosz Tanski ceph_readpage_to_fscache(inode, page); 24099ccbd22SMilosz Tanski 2411d3576fdSSage Weil out: 2421d3576fdSSage Weil return err < 0 ? err : 0; 2431d3576fdSSage Weil } 2441d3576fdSSage Weil 2451d3576fdSSage Weil static int ceph_readpage(struct file *filp, struct page *page) 2461d3576fdSSage Weil { 247dd2bc473SYan, Zheng int r = ceph_do_readpage(filp, page); 248dd2bc473SYan, Zheng if (r != -EINPROGRESS) 2491d3576fdSSage Weil unlock_page(page); 250dd2bc473SYan, Zheng else 251dd2bc473SYan, Zheng r = 0; 2521d3576fdSSage Weil return r; 2531d3576fdSSage Weil } 2541d3576fdSSage Weil 2551d3576fdSSage Weil /* 2567c272194SSage Weil * Finish an async read(ahead) op. 2571d3576fdSSage Weil */ 25885e084feSIlya Dryomov static void finish_read(struct ceph_osd_request *req) 2591d3576fdSSage Weil { 2607c272194SSage Weil struct inode *inode = req->r_inode; 26187060c10SAlex Elder struct ceph_osd_data *osd_data; 26285e084feSIlya Dryomov int rc = req->r_result <= 0 ? req->r_result : 0; 26385e084feSIlya Dryomov int bytes = req->r_result >= 0 ? req->r_result : 0; 264e0c59487SAlex Elder int num_pages; 2657c272194SSage Weil int i; 2667c272194SSage Weil 2677c272194SSage Weil dout("finish_read %p req %p rc %d bytes %d\n", inode, req, rc, bytes); 2687c272194SSage Weil 2697c272194SSage Weil /* unlock all pages, zeroing any data we didn't read */ 270406e2c9fSAlex Elder osd_data = osd_req_op_extent_osd_data(req, 0); 27187060c10SAlex Elder BUG_ON(osd_data->type != CEPH_OSD_DATA_TYPE_PAGES); 27287060c10SAlex Elder num_pages = calc_pages_for((u64)osd_data->alignment, 27387060c10SAlex Elder (u64)osd_data->length); 274e0c59487SAlex Elder for (i = 0; i < num_pages; i++) { 27587060c10SAlex Elder struct page *page = osd_data->pages[i]; 2767c272194SSage Weil 277368e3585SYan, Zheng if (rc < 0 && rc != -ENOENT) { 278368e3585SYan, Zheng ceph_fscache_readpage_cancel(inode, page); 279f36132a7SLi Wang goto unlock; 280368e3585SYan, Zheng } 28109cbfeafSKirill A. Shutemov if (bytes < (int)PAGE_SIZE) { 2827c272194SSage Weil /* zero (remainder of) page */ 2837c272194SSage Weil int s = bytes < 0 ? 0 : bytes; 28409cbfeafSKirill A. Shutemov zero_user_segment(page, s, PAGE_SIZE); 2857c272194SSage Weil } 2867c272194SSage Weil dout("finish_read %p uptodate %p idx %lu\n", inode, page, 2877c272194SSage Weil page->index); 2887c272194SSage Weil flush_dcache_page(page); 2897c272194SSage Weil SetPageUptodate(page); 29099ccbd22SMilosz Tanski ceph_readpage_to_fscache(inode, page); 291f36132a7SLi Wang unlock: 2927c272194SSage Weil unlock_page(page); 29309cbfeafSKirill A. Shutemov put_page(page); 29409cbfeafSKirill A. Shutemov bytes -= PAGE_SIZE; 2957c272194SSage Weil } 29687060c10SAlex Elder kfree(osd_data->pages); 2977c272194SSage Weil } 2987c272194SSage Weil 2997c272194SSage Weil /* 3007c272194SSage Weil * start an async read(ahead) operation. return nr_pages we submitted 3017c272194SSage Weil * a read for on success, or negative error code. 3027c272194SSage Weil */ 3035d988308SYan, Zheng static int start_read(struct inode *inode, struct ceph_rw_context *rw_ctx, 3045d988308SYan, Zheng struct list_head *page_list, int max) 3057c272194SSage Weil { 3067c272194SSage Weil struct ceph_osd_client *osdc = 3077c272194SSage Weil &ceph_inode_to_client(inode)->client->osdc; 3087c272194SSage Weil struct ceph_inode_info *ci = ceph_inode(inode); 309f86196eaSNikolay Borisov struct page *page = lru_to_page(page_list); 310acead002SAlex Elder struct ceph_vino vino; 3117c272194SSage Weil struct ceph_osd_request *req; 3127c272194SSage Weil u64 off; 3137c272194SSage Weil u64 len; 3147c272194SSage Weil int i; 3151d3576fdSSage Weil struct page **pages; 3167c272194SSage Weil pgoff_t next_index; 3177c272194SSage Weil int nr_pages = 0; 3182b1ac852SYan, Zheng int got = 0; 3192b1ac852SYan, Zheng int ret = 0; 3202b1ac852SYan, Zheng 3215d988308SYan, Zheng if (!rw_ctx) { 3222b1ac852SYan, Zheng /* caller of readpages does not hold buffer and read caps 3232b1ac852SYan, Zheng * (fadvise, madvise and readahead cases) */ 3242b1ac852SYan, Zheng int want = CEPH_CAP_FILE_CACHE; 3252ee9dd95SLuis Henriques ret = ceph_try_get_caps(ci, CEPH_CAP_FILE_RD, want, true, &got); 3262b1ac852SYan, Zheng if (ret < 0) { 3272b1ac852SYan, Zheng dout("start_read %p, error getting cap\n", inode); 3282b1ac852SYan, Zheng } else if (!(got & want)) { 3292b1ac852SYan, Zheng dout("start_read %p, no cache cap\n", inode); 3302b1ac852SYan, Zheng ret = 0; 3312b1ac852SYan, Zheng } 3322b1ac852SYan, Zheng if (ret <= 0) { 3332b1ac852SYan, Zheng if (got) 3342b1ac852SYan, Zheng ceph_put_cap_refs(ci, got); 3352b1ac852SYan, Zheng while (!list_empty(page_list)) { 336f86196eaSNikolay Borisov page = lru_to_page(page_list); 3372b1ac852SYan, Zheng list_del(&page->lru); 3382b1ac852SYan, Zheng put_page(page); 3392b1ac852SYan, Zheng } 3402b1ac852SYan, Zheng return ret; 3412b1ac852SYan, Zheng } 3422b1ac852SYan, Zheng } 3437c272194SSage Weil 3446285bc23SAlex Elder off = (u64) page_offset(page); 3457c272194SSage Weil 3467c272194SSage Weil /* count pages */ 3477c272194SSage Weil next_index = page->index; 3487c272194SSage Weil list_for_each_entry_reverse(page, page_list, lru) { 3497c272194SSage Weil if (page->index != next_index) 3507c272194SSage Weil break; 3517c272194SSage Weil nr_pages++; 3527c272194SSage Weil next_index++; 3530d66a487SSage Weil if (max && nr_pages == max) 3540d66a487SSage Weil break; 3557c272194SSage Weil } 35609cbfeafSKirill A. Shutemov len = nr_pages << PAGE_SHIFT; 3577c272194SSage Weil dout("start_read %p nr_pages %d is %lld~%lld\n", inode, nr_pages, 3587c272194SSage Weil off, len); 359acead002SAlex Elder vino = ceph_vino(inode); 360acead002SAlex Elder req = ceph_osdc_new_request(osdc, &ci->i_layout, vino, off, &len, 361715e4cd4SYan, Zheng 0, 1, CEPH_OSD_OP_READ, 362acead002SAlex Elder CEPH_OSD_FLAG_READ, NULL, 3637c272194SSage Weil ci->i_truncate_seq, ci->i_truncate_size, 364acead002SAlex Elder false); 3652b1ac852SYan, Zheng if (IS_ERR(req)) { 3662b1ac852SYan, Zheng ret = PTR_ERR(req); 3672b1ac852SYan, Zheng goto out; 3682b1ac852SYan, Zheng } 3691d3576fdSSage Weil 3701d3576fdSSage Weil /* build page vector */ 371cf7b7e14SAlex Elder nr_pages = calc_pages_for(0, len); 3726da2ec56SKees Cook pages = kmalloc_array(nr_pages, sizeof(*pages), GFP_KERNEL); 3732b1ac852SYan, Zheng if (!pages) { 3747c272194SSage Weil ret = -ENOMEM; 3752b1ac852SYan, Zheng goto out_put; 3762b1ac852SYan, Zheng } 3777c272194SSage Weil for (i = 0; i < nr_pages; ++i) { 3787c272194SSage Weil page = list_entry(page_list->prev, struct page, lru); 3797c272194SSage Weil BUG_ON(PageLocked(page)); 3807c272194SSage Weil list_del(&page->lru); 3811d3576fdSSage Weil 3827c272194SSage Weil dout("start_read %p adding %p idx %lu\n", inode, page, 3837c272194SSage Weil page->index); 3847c272194SSage Weil if (add_to_page_cache_lru(page, &inode->i_data, page->index, 385687265e5SYan, Zheng GFP_KERNEL)) { 386d4d3aa38SMilosz Tanski ceph_fscache_uncache_page(inode, page); 38709cbfeafSKirill A. Shutemov put_page(page); 3887c272194SSage Weil dout("start_read %p add_to_page_cache failed %p\n", 3897c272194SSage Weil inode, page); 3907c272194SSage Weil nr_pages = i; 3911afe4785SYan, Zheng if (nr_pages > 0) { 3921afe4785SYan, Zheng len = nr_pages << PAGE_SHIFT; 393d641df81SYan, Zheng osd_req_op_extent_update(req, 0, len); 3941afe4785SYan, Zheng break; 3951afe4785SYan, Zheng } 3967c272194SSage Weil goto out_pages; 3971d3576fdSSage Weil } 3987c272194SSage Weil pages[i] = page; 3991d3576fdSSage Weil } 400406e2c9fSAlex Elder osd_req_op_extent_osd_data_pages(req, 0, pages, len, 0, false, false); 4017c272194SSage Weil req->r_callback = finish_read; 4027c272194SSage Weil req->r_inode = inode; 4037c272194SSage Weil 4047c272194SSage Weil dout("start_read %p starting %p %lld~%lld\n", inode, req, off, len); 4057c272194SSage Weil ret = ceph_osdc_start_request(osdc, req, false); 4067c272194SSage Weil if (ret < 0) 4077c272194SSage Weil goto out_pages; 4087c272194SSage Weil ceph_osdc_put_request(req); 4092b1ac852SYan, Zheng 4102b1ac852SYan, Zheng /* After adding locked pages to page cache, the inode holds cache cap. 4112b1ac852SYan, Zheng * So we can drop our cap refs. */ 4122b1ac852SYan, Zheng if (got) 4132b1ac852SYan, Zheng ceph_put_cap_refs(ci, got); 4142b1ac852SYan, Zheng 4157c272194SSage Weil return nr_pages; 4167c272194SSage Weil 4177c272194SSage Weil out_pages: 4181afe4785SYan, Zheng for (i = 0; i < nr_pages; ++i) { 4191afe4785SYan, Zheng ceph_fscache_readpage_cancel(inode, pages[i]); 4201afe4785SYan, Zheng unlock_page(pages[i]); 4211afe4785SYan, Zheng } 4221afe4785SYan, Zheng ceph_put_page_vector(pages, nr_pages, false); 4232b1ac852SYan, Zheng out_put: 4247c272194SSage Weil ceph_osdc_put_request(req); 4252b1ac852SYan, Zheng out: 4262b1ac852SYan, Zheng if (got) 4272b1ac852SYan, Zheng ceph_put_cap_refs(ci, got); 4287c272194SSage Weil return ret; 4291d3576fdSSage Weil } 4301d3576fdSSage Weil 4317c272194SSage Weil 4321d3576fdSSage Weil /* 4331d3576fdSSage Weil * Read multiple pages. Leave pages we don't read + unlock in page_list; 4341d3576fdSSage Weil * the caller (VM) cleans them up. 4351d3576fdSSage Weil */ 4361d3576fdSSage Weil static int ceph_readpages(struct file *file, struct address_space *mapping, 4371d3576fdSSage Weil struct list_head *page_list, unsigned nr_pages) 4381d3576fdSSage Weil { 439496ad9aaSAl Viro struct inode *inode = file_inode(file); 4400d66a487SSage Weil struct ceph_fs_client *fsc = ceph_inode_to_client(inode); 44173737682SChengguang Xu struct ceph_file_info *fi = file->private_data; 4425d988308SYan, Zheng struct ceph_rw_context *rw_ctx; 4431d3576fdSSage Weil int rc = 0; 4440d66a487SSage Weil int max = 0; 4451d3576fdSSage Weil 44683701246SYan, Zheng if (ceph_inode(inode)->i_inline_version != CEPH_INLINE_NONE) 44783701246SYan, Zheng return -EINVAL; 44883701246SYan, Zheng 44999ccbd22SMilosz Tanski rc = ceph_readpages_from_fscache(mapping->host, mapping, page_list, 45099ccbd22SMilosz Tanski &nr_pages); 45199ccbd22SMilosz Tanski 45299ccbd22SMilosz Tanski if (rc == 0) 45399ccbd22SMilosz Tanski goto out; 45499ccbd22SMilosz Tanski 45573737682SChengguang Xu rw_ctx = ceph_find_rw_context(fi); 456aa187926SYan, Zheng max = fsc->mount_options->rsize >> PAGE_SHIFT; 4575d988308SYan, Zheng dout("readpages %p file %p ctx %p nr_pages %d max %d\n", 4585d988308SYan, Zheng inode, file, rw_ctx, nr_pages, max); 4597c272194SSage Weil while (!list_empty(page_list)) { 4605d988308SYan, Zheng rc = start_read(inode, rw_ctx, page_list, max); 4611d3576fdSSage Weil if (rc < 0) 4621d3576fdSSage Weil goto out; 4631d3576fdSSage Weil } 4641d3576fdSSage Weil out: 46576be778bSMilosz Tanski ceph_fscache_readpages_cancel(inode, page_list); 46676be778bSMilosz Tanski 4677c272194SSage Weil dout("readpages %p file %p ret %d\n", inode, file, rc); 4681d3576fdSSage Weil return rc; 4691d3576fdSSage Weil } 4701d3576fdSSage Weil 4711f934b00SYan, Zheng struct ceph_writeback_ctl 4721f934b00SYan, Zheng { 4731f934b00SYan, Zheng loff_t i_size; 4741f934b00SYan, Zheng u64 truncate_size; 4751f934b00SYan, Zheng u32 truncate_seq; 4761f934b00SYan, Zheng bool size_stable; 4772a2d927eSYan, Zheng bool head_snapc; 4781f934b00SYan, Zheng }; 4791f934b00SYan, Zheng 4801d3576fdSSage Weil /* 4811d3576fdSSage Weil * Get ref for the oldest snapc for an inode with dirty data... that is, the 4821d3576fdSSage Weil * only snap context we are allowed to write back. 4831d3576fdSSage Weil */ 4841f934b00SYan, Zheng static struct ceph_snap_context * 48505455e11SYan, Zheng get_oldest_context(struct inode *inode, struct ceph_writeback_ctl *ctl, 48605455e11SYan, Zheng struct ceph_snap_context *page_snapc) 4871d3576fdSSage Weil { 4881d3576fdSSage Weil struct ceph_inode_info *ci = ceph_inode(inode); 4891d3576fdSSage Weil struct ceph_snap_context *snapc = NULL; 4901d3576fdSSage Weil struct ceph_cap_snap *capsnap = NULL; 4911d3576fdSSage Weil 492be655596SSage Weil spin_lock(&ci->i_ceph_lock); 4931d3576fdSSage Weil list_for_each_entry(capsnap, &ci->i_cap_snaps, ci_item) { 4941d3576fdSSage Weil dout(" cap_snap %p snapc %p has %d dirty pages\n", capsnap, 4951d3576fdSSage Weil capsnap->context, capsnap->dirty_pages); 49605455e11SYan, Zheng if (!capsnap->dirty_pages) 49705455e11SYan, Zheng continue; 49805455e11SYan, Zheng 49905455e11SYan, Zheng /* get i_size, truncate_{seq,size} for page_snapc? */ 50005455e11SYan, Zheng if (snapc && capsnap->context != page_snapc) 50105455e11SYan, Zheng continue; 50205455e11SYan, Zheng 5031f934b00SYan, Zheng if (ctl) { 5041f934b00SYan, Zheng if (capsnap->writing) { 5051f934b00SYan, Zheng ctl->i_size = i_size_read(inode); 5061f934b00SYan, Zheng ctl->size_stable = false; 5071f934b00SYan, Zheng } else { 5081f934b00SYan, Zheng ctl->i_size = capsnap->size; 5091f934b00SYan, Zheng ctl->size_stable = true; 5101f934b00SYan, Zheng } 5111f934b00SYan, Zheng ctl->truncate_size = capsnap->truncate_size; 5121f934b00SYan, Zheng ctl->truncate_seq = capsnap->truncate_seq; 5132a2d927eSYan, Zheng ctl->head_snapc = false; 5141f934b00SYan, Zheng } 51505455e11SYan, Zheng 51605455e11SYan, Zheng if (snapc) 5171d3576fdSSage Weil break; 51805455e11SYan, Zheng 51905455e11SYan, Zheng snapc = ceph_get_snap_context(capsnap->context); 52005455e11SYan, Zheng if (!page_snapc || 52105455e11SYan, Zheng page_snapc == snapc || 52205455e11SYan, Zheng page_snapc->seq > snapc->seq) 52305455e11SYan, Zheng break; 5241d3576fdSSage Weil } 5257d8cb26dSSage Weil if (!snapc && ci->i_wrbuffer_ref_head) { 52680e755feSSage Weil snapc = ceph_get_snap_context(ci->i_head_snapc); 5271d3576fdSSage Weil dout(" head snapc %p has %d dirty pages\n", 5281d3576fdSSage Weil snapc, ci->i_wrbuffer_ref_head); 5291f934b00SYan, Zheng if (ctl) { 5301f934b00SYan, Zheng ctl->i_size = i_size_read(inode); 5311f934b00SYan, Zheng ctl->truncate_size = ci->i_truncate_size; 5321f934b00SYan, Zheng ctl->truncate_seq = ci->i_truncate_seq; 5331f934b00SYan, Zheng ctl->size_stable = false; 5342a2d927eSYan, Zheng ctl->head_snapc = true; 5351f934b00SYan, Zheng } 5361d3576fdSSage Weil } 537be655596SSage Weil spin_unlock(&ci->i_ceph_lock); 5381d3576fdSSage Weil return snapc; 5391d3576fdSSage Weil } 5401d3576fdSSage Weil 5411f934b00SYan, Zheng static u64 get_writepages_data_length(struct inode *inode, 5421f934b00SYan, Zheng struct page *page, u64 start) 5431f934b00SYan, Zheng { 5441f934b00SYan, Zheng struct ceph_inode_info *ci = ceph_inode(inode); 5451f934b00SYan, Zheng struct ceph_snap_context *snapc = page_snap_context(page); 5461f934b00SYan, Zheng struct ceph_cap_snap *capsnap = NULL; 5471f934b00SYan, Zheng u64 end = i_size_read(inode); 5481f934b00SYan, Zheng 5491f934b00SYan, Zheng if (snapc != ci->i_head_snapc) { 5501f934b00SYan, Zheng bool found = false; 5511f934b00SYan, Zheng spin_lock(&ci->i_ceph_lock); 5521f934b00SYan, Zheng list_for_each_entry(capsnap, &ci->i_cap_snaps, ci_item) { 5531f934b00SYan, Zheng if (capsnap->context == snapc) { 5541f934b00SYan, Zheng if (!capsnap->writing) 5551f934b00SYan, Zheng end = capsnap->size; 5561f934b00SYan, Zheng found = true; 5571f934b00SYan, Zheng break; 5581f934b00SYan, Zheng } 5591f934b00SYan, Zheng } 5601f934b00SYan, Zheng spin_unlock(&ci->i_ceph_lock); 5611f934b00SYan, Zheng WARN_ON(!found); 5621f934b00SYan, Zheng } 5631f934b00SYan, Zheng if (end > page_offset(page) + PAGE_SIZE) 5641f934b00SYan, Zheng end = page_offset(page) + PAGE_SIZE; 5651f934b00SYan, Zheng return end > start ? end - start : 0; 5661f934b00SYan, Zheng } 5671f934b00SYan, Zheng 5681d3576fdSSage Weil /* 5691d3576fdSSage Weil * Write a single page, but leave the page locked. 5701d3576fdSSage Weil * 5711d3576fdSSage Weil * If we get a write error, set the page error bit, but still adjust the 5721d3576fdSSage Weil * dirty page accounting (i.e., page is no longer dirty). 5731d3576fdSSage Weil */ 5741d3576fdSSage Weil static int writepage_nounlock(struct page *page, struct writeback_control *wbc) 5751d3576fdSSage Weil { 5761d3576fdSSage Weil struct inode *inode; 5771d3576fdSSage Weil struct ceph_inode_info *ci; 5783d14c5d2SYehuda Sadeh struct ceph_fs_client *fsc; 5796298a337SSage Weil struct ceph_snap_context *snapc, *oldest; 580fc2744aaSYan, Zheng loff_t page_off = page_offset(page); 58143986881SYan, Zheng int err, len = PAGE_SIZE; 5821f934b00SYan, Zheng struct ceph_writeback_ctl ceph_wbc; 5831d3576fdSSage Weil 5841d3576fdSSage Weil dout("writepage %p idx %lu\n", page, page->index); 5851d3576fdSSage Weil 5861d3576fdSSage Weil inode = page->mapping->host; 5871d3576fdSSage Weil ci = ceph_inode(inode); 5883d14c5d2SYehuda Sadeh fsc = ceph_inode_to_client(inode); 5891d3576fdSSage Weil 5901d3576fdSSage Weil /* verify this is a writeable snap context */ 59161600ef8SYan, Zheng snapc = page_snap_context(page); 592d37b1d99SMarkus Elfring if (!snapc) { 5931d3576fdSSage Weil dout("writepage %p page %p not dirty?\n", inode, page); 59443986881SYan, Zheng return 0; 5951d3576fdSSage Weil } 59605455e11SYan, Zheng oldest = get_oldest_context(inode, &ceph_wbc, snapc); 5976298a337SSage Weil if (snapc->seq > oldest->seq) { 5981d3576fdSSage Weil dout("writepage %p page %p snapc %p not writeable - noop\n", 59961600ef8SYan, Zheng inode, page, snapc); 6001d3576fdSSage Weil /* we should only noop if called by kswapd */ 601fa71fefbSYan, Zheng WARN_ON(!(current->flags & PF_MEMALLOC)); 6026298a337SSage Weil ceph_put_snap_context(oldest); 603fa71fefbSYan, Zheng redirty_page_for_writepage(wbc, page); 60443986881SYan, Zheng return 0; 6051d3576fdSSage Weil } 6066298a337SSage Weil ceph_put_snap_context(oldest); 6071d3576fdSSage Weil 6081d3576fdSSage Weil /* is this a partial page at end of file? */ 6091f934b00SYan, Zheng if (page_off >= ceph_wbc.i_size) { 6101f934b00SYan, Zheng dout("%p page eof %llu\n", page, ceph_wbc.i_size); 61105455e11SYan, Zheng page->mapping->a_ops->invalidatepage(page, 0, PAGE_SIZE); 61243986881SYan, Zheng return 0; 613fc2744aaSYan, Zheng } 61443986881SYan, Zheng 6151f934b00SYan, Zheng if (ceph_wbc.i_size < page_off + len) 6161f934b00SYan, Zheng len = ceph_wbc.i_size - page_off; 6171d3576fdSSage Weil 6181c0a9c2dSYan, Zheng dout("writepage %p page %p index %lu on %llu~%u snapc %p seq %lld\n", 6191c0a9c2dSYan, Zheng inode, page, page->index, page_off, len, snapc, snapc->seq); 6201d3576fdSSage Weil 621314c4737SYan, Zheng if (atomic_long_inc_return(&fsc->writeback_count) > 6223d14c5d2SYehuda Sadeh CONGESTION_ON_THRESH(fsc->mount_options->congestion_kb)) 62309dc9fc2SJan Kara set_bdi_congested(inode_to_bdi(inode), BLK_RW_ASYNC); 6242baba250SYehuda Sadeh 6251d3576fdSSage Weil set_page_writeback(page); 6261f934b00SYan, Zheng err = ceph_osdc_writepages(&fsc->client->osdc, ceph_vino(inode), 6271f934b00SYan, Zheng &ci->i_layout, snapc, page_off, len, 6281f934b00SYan, Zheng ceph_wbc.truncate_seq, 6291f934b00SYan, Zheng ceph_wbc.truncate_size, 630fac02ddfSArnd Bergmann &inode->i_mtime, &page, 1); 6311d3576fdSSage Weil if (err < 0) { 632ad15ec06SYan, Zheng struct writeback_control tmp_wbc; 633ad15ec06SYan, Zheng if (!wbc) 634ad15ec06SYan, Zheng wbc = &tmp_wbc; 635ad15ec06SYan, Zheng if (err == -ERESTARTSYS) { 636ad15ec06SYan, Zheng /* killed by SIGKILL */ 637ad15ec06SYan, Zheng dout("writepage interrupted page %p\n", page); 638ad15ec06SYan, Zheng redirty_page_for_writepage(wbc, page); 639ad15ec06SYan, Zheng end_page_writeback(page); 64043986881SYan, Zheng return err; 641ad15ec06SYan, Zheng } 642ad15ec06SYan, Zheng dout("writepage setting page/mapping error %d %p\n", 643ad15ec06SYan, Zheng err, page); 6441d3576fdSSage Weil SetPageError(page); 6451d3576fdSSage Weil mapping_set_error(&inode->i_data, err); 6461d3576fdSSage Weil wbc->pages_skipped++; 6471d3576fdSSage Weil } else { 6481d3576fdSSage Weil dout("writepage cleaned page %p\n", page); 6491d3576fdSSage Weil err = 0; /* vfs expects us to return 0 */ 6501d3576fdSSage Weil } 6511d3576fdSSage Weil page->private = 0; 6521d3576fdSSage Weil ClearPagePrivate(page); 6531d3576fdSSage Weil end_page_writeback(page); 6541d3576fdSSage Weil ceph_put_wrbuffer_cap_refs(ci, 1, snapc); 6556298a337SSage Weil ceph_put_snap_context(snapc); /* page's reference */ 656314c4737SYan, Zheng 657314c4737SYan, Zheng if (atomic_long_dec_return(&fsc->writeback_count) < 658314c4737SYan, Zheng CONGESTION_OFF_THRESH(fsc->mount_options->congestion_kb)) 659314c4737SYan, Zheng clear_bdi_congested(inode_to_bdi(inode), BLK_RW_ASYNC); 660314c4737SYan, Zheng 6611d3576fdSSage Weil return err; 6621d3576fdSSage Weil } 6631d3576fdSSage Weil 6641d3576fdSSage Weil static int ceph_writepage(struct page *page, struct writeback_control *wbc) 6651d3576fdSSage Weil { 666dbd646a8SYehuda Sadeh int err; 667dbd646a8SYehuda Sadeh struct inode *inode = page->mapping->host; 668dbd646a8SYehuda Sadeh BUG_ON(!inode); 66970b666c3SSage Weil ihold(inode); 670dbd646a8SYehuda Sadeh err = writepage_nounlock(page, wbc); 671ad15ec06SYan, Zheng if (err == -ERESTARTSYS) { 672ad15ec06SYan, Zheng /* direct memory reclaimer was killed by SIGKILL. return 0 673ad15ec06SYan, Zheng * to prevent caller from setting mapping/page error */ 674ad15ec06SYan, Zheng err = 0; 675ad15ec06SYan, Zheng } 6761d3576fdSSage Weil unlock_page(page); 677dbd646a8SYehuda Sadeh iput(inode); 6781d3576fdSSage Weil return err; 6791d3576fdSSage Weil } 6801d3576fdSSage Weil 6811d3576fdSSage Weil /* 6821d3576fdSSage Weil * lame release_pages helper. release_pages() isn't exported to 6831d3576fdSSage Weil * modules. 6841d3576fdSSage Weil */ 6851d3576fdSSage Weil static void ceph_release_pages(struct page **pages, int num) 6861d3576fdSSage Weil { 6871d3576fdSSage Weil struct pagevec pvec; 6881d3576fdSSage Weil int i; 6891d3576fdSSage Weil 69086679820SMel Gorman pagevec_init(&pvec); 6911d3576fdSSage Weil for (i = 0; i < num; i++) { 6921d3576fdSSage Weil if (pagevec_add(&pvec, pages[i]) == 0) 6931d3576fdSSage Weil pagevec_release(&pvec); 6941d3576fdSSage Weil } 6951d3576fdSSage Weil pagevec_release(&pvec); 6961d3576fdSSage Weil } 6971d3576fdSSage Weil 6981d3576fdSSage Weil /* 6991d3576fdSSage Weil * async writeback completion handler. 7001d3576fdSSage Weil * 7011d3576fdSSage Weil * If we get an error, set the mapping error bit, but not the individual 7021d3576fdSSage Weil * page error bits. 7031d3576fdSSage Weil */ 70485e084feSIlya Dryomov static void writepages_finish(struct ceph_osd_request *req) 7051d3576fdSSage Weil { 7061d3576fdSSage Weil struct inode *inode = req->r_inode; 7071d3576fdSSage Weil struct ceph_inode_info *ci = ceph_inode(inode); 70887060c10SAlex Elder struct ceph_osd_data *osd_data; 7091d3576fdSSage Weil struct page *page; 7105b64640cSYan, Zheng int num_pages, total_pages = 0; 7115b64640cSYan, Zheng int i, j; 7125b64640cSYan, Zheng int rc = req->r_result; 7131d3576fdSSage Weil struct ceph_snap_context *snapc = req->r_snapc; 7141d3576fdSSage Weil struct address_space *mapping = inode->i_mapping; 7153d14c5d2SYehuda Sadeh struct ceph_fs_client *fsc = ceph_inode_to_client(inode); 7165b64640cSYan, Zheng bool remove_page; 7171d3576fdSSage Weil 7185b64640cSYan, Zheng dout("writepages_finish %p rc %d\n", inode, rc); 71926544c62SJeff Layton if (rc < 0) { 7201d3576fdSSage Weil mapping_set_error(mapping, rc); 72126544c62SJeff Layton ceph_set_error_write(ci); 72226544c62SJeff Layton } else { 72326544c62SJeff Layton ceph_clear_error_write(ci); 72426544c62SJeff Layton } 725e63dc5c7SYehuda Sadeh 726e63dc5c7SYehuda Sadeh /* 727e63dc5c7SYehuda Sadeh * We lost the cache cap, need to truncate the page before 728e63dc5c7SYehuda Sadeh * it is unlocked, otherwise we'd truncate it later in the 729e63dc5c7SYehuda Sadeh * page truncation thread, possibly losing some data that 730e63dc5c7SYehuda Sadeh * raced its way in 731e63dc5c7SYehuda Sadeh */ 7325b64640cSYan, Zheng remove_page = !(ceph_caps_issued(ci) & 7335b64640cSYan, Zheng (CEPH_CAP_FILE_CACHE|CEPH_CAP_FILE_LAZYIO)); 7345b64640cSYan, Zheng 7355b64640cSYan, Zheng /* clean all pages */ 7365b64640cSYan, Zheng for (i = 0; i < req->r_num_ops; i++) { 7375b64640cSYan, Zheng if (req->r_ops[i].op != CEPH_OSD_OP_WRITE) 7385b64640cSYan, Zheng break; 7395b64640cSYan, Zheng 7405b64640cSYan, Zheng osd_data = osd_req_op_extent_osd_data(req, i); 7415b64640cSYan, Zheng BUG_ON(osd_data->type != CEPH_OSD_DATA_TYPE_PAGES); 7425b64640cSYan, Zheng num_pages = calc_pages_for((u64)osd_data->alignment, 7435b64640cSYan, Zheng (u64)osd_data->length); 7445b64640cSYan, Zheng total_pages += num_pages; 7455b64640cSYan, Zheng for (j = 0; j < num_pages; j++) { 7465b64640cSYan, Zheng page = osd_data->pages[j]; 7475b64640cSYan, Zheng BUG_ON(!page); 7485b64640cSYan, Zheng WARN_ON(!PageUptodate(page)); 7495b64640cSYan, Zheng 7505b64640cSYan, Zheng if (atomic_long_dec_return(&fsc->writeback_count) < 7515b64640cSYan, Zheng CONGESTION_OFF_THRESH( 7525b64640cSYan, Zheng fsc->mount_options->congestion_kb)) 75309dc9fc2SJan Kara clear_bdi_congested(inode_to_bdi(inode), 7545b64640cSYan, Zheng BLK_RW_ASYNC); 7555b64640cSYan, Zheng 7565b64640cSYan, Zheng ceph_put_snap_context(page_snap_context(page)); 7575b64640cSYan, Zheng page->private = 0; 7585b64640cSYan, Zheng ClearPagePrivate(page); 7595b64640cSYan, Zheng dout("unlocking %p\n", page); 7605b64640cSYan, Zheng end_page_writeback(page); 7615b64640cSYan, Zheng 7625b64640cSYan, Zheng if (remove_page) 7635b64640cSYan, Zheng generic_error_remove_page(inode->i_mapping, 7645b64640cSYan, Zheng page); 765e63dc5c7SYehuda Sadeh 7661d3576fdSSage Weil unlock_page(page); 7671d3576fdSSage Weil } 7685b64640cSYan, Zheng dout("writepages_finish %p wrote %llu bytes cleaned %d pages\n", 7695b64640cSYan, Zheng inode, osd_data->length, rc >= 0 ? num_pages : 0); 7701d3576fdSSage Weil 77187060c10SAlex Elder ceph_release_pages(osd_data->pages, num_pages); 7725b64640cSYan, Zheng } 7735b64640cSYan, Zheng 7745b64640cSYan, Zheng ceph_put_wrbuffer_cap_refs(ci, total_pages, snapc); 7755b64640cSYan, Zheng 7765b64640cSYan, Zheng osd_data = osd_req_op_extent_osd_data(req, 0); 77787060c10SAlex Elder if (osd_data->pages_from_pool) 77887060c10SAlex Elder mempool_free(osd_data->pages, 779640ef79dSCheng Renquan ceph_sb_to_client(inode->i_sb)->wb_pagevec_pool); 7801d3576fdSSage Weil else 78187060c10SAlex Elder kfree(osd_data->pages); 7821d3576fdSSage Weil ceph_osdc_put_request(req); 7831d3576fdSSage Weil } 7841d3576fdSSage Weil 7851d3576fdSSage Weil /* 7861d3576fdSSage Weil * initiate async writeback 7871d3576fdSSage Weil */ 7881d3576fdSSage Weil static int ceph_writepages_start(struct address_space *mapping, 7891d3576fdSSage Weil struct writeback_control *wbc) 7901d3576fdSSage Weil { 7911d3576fdSSage Weil struct inode *inode = mapping->host; 7921d3576fdSSage Weil struct ceph_inode_info *ci = ceph_inode(inode); 793fc2744aaSYan, Zheng struct ceph_fs_client *fsc = ceph_inode_to_client(inode); 794fc2744aaSYan, Zheng struct ceph_vino vino = ceph_vino(inode); 7952a2d927eSYan, Zheng pgoff_t index, start_index, end = -1; 79680e755feSSage Weil struct ceph_snap_context *snapc = NULL, *last_snapc = NULL, *pgsnapc; 7971d3576fdSSage Weil struct pagevec pvec; 7981d3576fdSSage Weil int rc = 0; 79993407472SFabian Frederick unsigned int wsize = i_blocksize(inode); 8001d3576fdSSage Weil struct ceph_osd_request *req = NULL; 8011f934b00SYan, Zheng struct ceph_writeback_ctl ceph_wbc; 802590e9d98SYan, Zheng bool should_loop, range_whole = false; 803af9cc401SYan, Zheng bool done = false; 8041d3576fdSSage Weil 8053fb99d48SYanhu Cao dout("writepages_start %p (mode=%s)\n", inode, 8061d3576fdSSage Weil wbc->sync_mode == WB_SYNC_NONE ? "NONE" : 8071d3576fdSSage Weil (wbc->sync_mode == WB_SYNC_ALL ? "ALL" : "HOLD")); 8081d3576fdSSage Weil 80952953d55SSeraphime Kirkovski if (READ_ONCE(fsc->mount_state) == CEPH_MOUNT_SHUTDOWN) { 8106c93df5dSYan, Zheng if (ci->i_wrbuffer_ref > 0) { 8116c93df5dSYan, Zheng pr_warn_ratelimited( 8126c93df5dSYan, Zheng "writepage_start %p %lld forced umount\n", 8136c93df5dSYan, Zheng inode, ceph_ino(inode)); 8146c93df5dSYan, Zheng } 815a341d4dfSYan, Zheng mapping_set_error(mapping, -EIO); 8161d3576fdSSage Weil return -EIO; /* we're in a forced umount, don't write! */ 8171d3576fdSSage Weil } 81895cca2b4SYan, Zheng if (fsc->mount_options->wsize < wsize) 8193d14c5d2SYehuda Sadeh wsize = fsc->mount_options->wsize; 8201d3576fdSSage Weil 82186679820SMel Gorman pagevec_init(&pvec); 8221d3576fdSSage Weil 823590e9d98SYan, Zheng start_index = wbc->range_cyclic ? mapping->writeback_index : 0; 824590e9d98SYan, Zheng index = start_index; 8251d3576fdSSage Weil 8261d3576fdSSage Weil retry: 8271d3576fdSSage Weil /* find oldest snap context with dirty data */ 82805455e11SYan, Zheng snapc = get_oldest_context(inode, &ceph_wbc, NULL); 8291d3576fdSSage Weil if (!snapc) { 8301d3576fdSSage Weil /* hmm, why does writepages get called when there 8311d3576fdSSage Weil is no dirty data? */ 8321d3576fdSSage Weil dout(" no snap context with dirty data?\n"); 8331d3576fdSSage Weil goto out; 8341d3576fdSSage Weil } 8351d3576fdSSage Weil dout(" oldest snapc is %p seq %lld (%d snaps)\n", 8361d3576fdSSage Weil snapc, snapc->seq, snapc->num_snaps); 837fc2744aaSYan, Zheng 8382a2d927eSYan, Zheng should_loop = false; 8392a2d927eSYan, Zheng if (ceph_wbc.head_snapc && snapc != last_snapc) { 8402a2d927eSYan, Zheng /* where to start/end? */ 8412a2d927eSYan, Zheng if (wbc->range_cyclic) { 8422a2d927eSYan, Zheng index = start_index; 8432a2d927eSYan, Zheng end = -1; 8442a2d927eSYan, Zheng if (index > 0) 8452a2d927eSYan, Zheng should_loop = true; 8462a2d927eSYan, Zheng dout(" cyclic, start at %lu\n", index); 8472a2d927eSYan, Zheng } else { 8482a2d927eSYan, Zheng index = wbc->range_start >> PAGE_SHIFT; 8492a2d927eSYan, Zheng end = wbc->range_end >> PAGE_SHIFT; 8502a2d927eSYan, Zheng if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX) 8512a2d927eSYan, Zheng range_whole = true; 8522a2d927eSYan, Zheng dout(" not cyclic, %lu to %lu\n", index, end); 8531d3576fdSSage Weil } 8542a2d927eSYan, Zheng } else if (!ceph_wbc.head_snapc) { 8552a2d927eSYan, Zheng /* Do not respect wbc->range_{start,end}. Dirty pages 8562a2d927eSYan, Zheng * in that range can be associated with newer snapc. 8572a2d927eSYan, Zheng * They are not writeable until we write all dirty pages 8582a2d927eSYan, Zheng * associated with 'snapc' get written */ 8591582af2eSYan, Zheng if (index > 0) 8602a2d927eSYan, Zheng should_loop = true; 8612a2d927eSYan, Zheng dout(" non-head snapc, range whole\n"); 8622a2d927eSYan, Zheng } 8632a2d927eSYan, Zheng 8642a2d927eSYan, Zheng ceph_put_snap_context(last_snapc); 8651d3576fdSSage Weil last_snapc = snapc; 8661d3576fdSSage Weil 867af9cc401SYan, Zheng while (!done && index <= end) { 8685b64640cSYan, Zheng int num_ops = 0, op_idx; 8690e5ecac7SYan, Zheng unsigned i, pvec_pages, max_pages, locked_pages = 0; 8705b64640cSYan, Zheng struct page **pages = NULL, **data_pages; 871e5975c7cSAlex Elder mempool_t *pool = NULL; /* Becomes non-null if mempool used */ 8721d3576fdSSage Weil struct page *page; 8730e5ecac7SYan, Zheng pgoff_t strip_unit_end = 0; 8745b64640cSYan, Zheng u64 offset = 0, len = 0; 8751d3576fdSSage Weil 8760e5ecac7SYan, Zheng max_pages = wsize >> PAGE_SHIFT; 8771d3576fdSSage Weil 8781d3576fdSSage Weil get_more_pages: 8794be90299SJan Kara pvec_pages = pagevec_lookup_range_nr_tag(&pvec, mapping, &index, 8800ed75fc8SJan Kara end, PAGECACHE_TAG_DIRTY, 8814be90299SJan Kara max_pages - locked_pages); 8820ed75fc8SJan Kara dout("pagevec_lookup_range_tag got %d\n", pvec_pages); 8831d3576fdSSage Weil if (!pvec_pages && !locked_pages) 8841d3576fdSSage Weil break; 8851d3576fdSSage Weil for (i = 0; i < pvec_pages && locked_pages < max_pages; i++) { 8861d3576fdSSage Weil page = pvec.pages[i]; 8871d3576fdSSage Weil dout("? %p idx %lu\n", page, page->index); 8881d3576fdSSage Weil if (locked_pages == 0) 8891d3576fdSSage Weil lock_page(page); /* first page */ 8901d3576fdSSage Weil else if (!trylock_page(page)) 8911d3576fdSSage Weil break; 8921d3576fdSSage Weil 8931d3576fdSSage Weil /* only dirty pages, or our accounting breaks */ 8941d3576fdSSage Weil if (unlikely(!PageDirty(page)) || 8951d3576fdSSage Weil unlikely(page->mapping != mapping)) { 8961d3576fdSSage Weil dout("!dirty or !mapping %p\n", page); 8971d3576fdSSage Weil unlock_page(page); 8980713e5f2SYan, Zheng continue; 8991d3576fdSSage Weil } 900af9cc401SYan, Zheng /* only if matching snap context */ 901af9cc401SYan, Zheng pgsnapc = page_snap_context(page); 902af9cc401SYan, Zheng if (pgsnapc != snapc) { 903af9cc401SYan, Zheng dout("page snapc %p %lld != oldest %p %lld\n", 904af9cc401SYan, Zheng pgsnapc, pgsnapc->seq, snapc, snapc->seq); 9051582af2eSYan, Zheng if (!should_loop && 9061582af2eSYan, Zheng !ceph_wbc.head_snapc && 9071582af2eSYan, Zheng wbc->sync_mode != WB_SYNC_NONE) 9081582af2eSYan, Zheng should_loop = true; 9091d3576fdSSage Weil unlock_page(page); 910af9cc401SYan, Zheng continue; 9111d3576fdSSage Weil } 9121f934b00SYan, Zheng if (page_offset(page) >= ceph_wbc.i_size) { 9131f934b00SYan, Zheng dout("%p page eof %llu\n", 9141f934b00SYan, Zheng page, ceph_wbc.i_size); 915af9cc401SYan, Zheng if (ceph_wbc.size_stable || 916af9cc401SYan, Zheng page_offset(page) >= i_size_read(inode)) 917af9cc401SYan, Zheng mapping->a_ops->invalidatepage(page, 918af9cc401SYan, Zheng 0, PAGE_SIZE); 919af9cc401SYan, Zheng unlock_page(page); 920af9cc401SYan, Zheng continue; 921af9cc401SYan, Zheng } 922af9cc401SYan, Zheng if (strip_unit_end && (page->index > strip_unit_end)) { 923af9cc401SYan, Zheng dout("end of strip unit %p\n", page); 9241d3576fdSSage Weil unlock_page(page); 9251d3576fdSSage Weil break; 9261d3576fdSSage Weil } 9271d3576fdSSage Weil if (PageWriteback(page)) { 9280713e5f2SYan, Zheng if (wbc->sync_mode == WB_SYNC_NONE) { 9291d3576fdSSage Weil dout("%p under writeback\n", page); 9301d3576fdSSage Weil unlock_page(page); 9310713e5f2SYan, Zheng continue; 9320713e5f2SYan, Zheng } 9330713e5f2SYan, Zheng dout("waiting on writeback %p\n", page); 9340713e5f2SYan, Zheng wait_on_page_writeback(page); 9351d3576fdSSage Weil } 9361d3576fdSSage Weil 9371d3576fdSSage Weil if (!clear_page_dirty_for_io(page)) { 9381d3576fdSSage Weil dout("%p !clear_page_dirty_for_io\n", page); 9391d3576fdSSage Weil unlock_page(page); 9400713e5f2SYan, Zheng continue; 9411d3576fdSSage Weil } 9421d3576fdSSage Weil 943e5975c7cSAlex Elder /* 944e5975c7cSAlex Elder * We have something to write. If this is 945e5975c7cSAlex Elder * the first locked page this time through, 9465b64640cSYan, Zheng * calculate max possinle write size and 9475b64640cSYan, Zheng * allocate a page array 948e5975c7cSAlex Elder */ 9491d3576fdSSage Weil if (locked_pages == 0) { 9505b64640cSYan, Zheng u64 objnum; 9515b64640cSYan, Zheng u64 objoff; 952dccbf080SIlya Dryomov u32 xlen; 9535b64640cSYan, Zheng 9541d3576fdSSage Weil /* prepare async write request */ 9556285bc23SAlex Elder offset = (u64)page_offset(page); 956dccbf080SIlya Dryomov ceph_calc_file_object_mapping(&ci->i_layout, 957dccbf080SIlya Dryomov offset, wsize, 9585b64640cSYan, Zheng &objnum, &objoff, 959dccbf080SIlya Dryomov &xlen); 960dccbf080SIlya Dryomov len = xlen; 9618c71897bSHenry C Chang 9623fb99d48SYanhu Cao num_ops = 1; 9635b64640cSYan, Zheng strip_unit_end = page->index + 96409cbfeafSKirill A. Shutemov ((len - 1) >> PAGE_SHIFT); 965715e4cd4SYan, Zheng 9665b64640cSYan, Zheng BUG_ON(pages); 96788486957SAlex Elder max_pages = calc_pages_for(0, (u64)len); 9686da2ec56SKees Cook pages = kmalloc_array(max_pages, 9696da2ec56SKees Cook sizeof(*pages), 970fc2744aaSYan, Zheng GFP_NOFS); 97188486957SAlex Elder if (!pages) { 97288486957SAlex Elder pool = fsc->wb_pagevec_pool; 97388486957SAlex Elder pages = mempool_alloc(pool, GFP_NOFS); 974e5975c7cSAlex Elder BUG_ON(!pages); 97588486957SAlex Elder } 9765b64640cSYan, Zheng 9775b64640cSYan, Zheng len = 0; 9785b64640cSYan, Zheng } else if (page->index != 97909cbfeafSKirill A. Shutemov (offset + len) >> PAGE_SHIFT) { 9805b64640cSYan, Zheng if (num_ops >= (pool ? CEPH_OSD_SLAB_OPS : 9815b64640cSYan, Zheng CEPH_OSD_MAX_OPS)) { 9825b64640cSYan, Zheng redirty_page_for_writepage(wbc, page); 9835b64640cSYan, Zheng unlock_page(page); 9845b64640cSYan, Zheng break; 9855b64640cSYan, Zheng } 9865b64640cSYan, Zheng 9875b64640cSYan, Zheng num_ops++; 9885b64640cSYan, Zheng offset = (u64)page_offset(page); 9895b64640cSYan, Zheng len = 0; 9901d3576fdSSage Weil } 9911d3576fdSSage Weil 9921d3576fdSSage Weil /* note position of first page in pvec */ 9931d3576fdSSage Weil dout("%p will write page %p idx %lu\n", 9941d3576fdSSage Weil inode, page, page->index); 9952baba250SYehuda Sadeh 9965b64640cSYan, Zheng if (atomic_long_inc_return(&fsc->writeback_count) > 9975b64640cSYan, Zheng CONGESTION_ON_THRESH( 9983d14c5d2SYehuda Sadeh fsc->mount_options->congestion_kb)) { 99909dc9fc2SJan Kara set_bdi_congested(inode_to_bdi(inode), 1000213c99eeSSage Weil BLK_RW_ASYNC); 10012baba250SYehuda Sadeh } 10022baba250SYehuda Sadeh 10030713e5f2SYan, Zheng 10040713e5f2SYan, Zheng pages[locked_pages++] = page; 10050713e5f2SYan, Zheng pvec.pages[i] = NULL; 10060713e5f2SYan, Zheng 100709cbfeafSKirill A. Shutemov len += PAGE_SIZE; 10081d3576fdSSage Weil } 10091d3576fdSSage Weil 10101d3576fdSSage Weil /* did we get anything? */ 10111d3576fdSSage Weil if (!locked_pages) 10121d3576fdSSage Weil goto release_pvec_pages; 10131d3576fdSSage Weil if (i) { 10140713e5f2SYan, Zheng unsigned j, n = 0; 10150713e5f2SYan, Zheng /* shift unused page to beginning of pvec */ 10160713e5f2SYan, Zheng for (j = 0; j < pvec_pages; j++) { 10170713e5f2SYan, Zheng if (!pvec.pages[j]) 10180713e5f2SYan, Zheng continue; 10190713e5f2SYan, Zheng if (n < j) 10200713e5f2SYan, Zheng pvec.pages[n] = pvec.pages[j]; 10210713e5f2SYan, Zheng n++; 10220713e5f2SYan, Zheng } 10230713e5f2SYan, Zheng pvec.nr = n; 10241d3576fdSSage Weil 10251d3576fdSSage Weil if (pvec_pages && i == pvec_pages && 10261d3576fdSSage Weil locked_pages < max_pages) { 10271d3576fdSSage Weil dout("reached end pvec, trying for more\n"); 10280713e5f2SYan, Zheng pagevec_release(&pvec); 10291d3576fdSSage Weil goto get_more_pages; 10301d3576fdSSage Weil } 10311d3576fdSSage Weil } 10321d3576fdSSage Weil 10335b64640cSYan, Zheng new_request: 1034e5975c7cSAlex Elder offset = page_offset(pages[0]); 10355b64640cSYan, Zheng len = wsize; 10365b64640cSYan, Zheng 10375b64640cSYan, Zheng req = ceph_osdc_new_request(&fsc->client->osdc, 10385b64640cSYan, Zheng &ci->i_layout, vino, 10395b64640cSYan, Zheng offset, &len, 0, num_ops, 10401f934b00SYan, Zheng CEPH_OSD_OP_WRITE, CEPH_OSD_FLAG_WRITE, 10411f934b00SYan, Zheng snapc, ceph_wbc.truncate_seq, 10421f934b00SYan, Zheng ceph_wbc.truncate_size, false); 10435b64640cSYan, Zheng if (IS_ERR(req)) { 10445b64640cSYan, Zheng req = ceph_osdc_new_request(&fsc->client->osdc, 10455b64640cSYan, Zheng &ci->i_layout, vino, 10465b64640cSYan, Zheng offset, &len, 0, 10475b64640cSYan, Zheng min(num_ops, 10485b64640cSYan, Zheng CEPH_OSD_SLAB_OPS), 10495b64640cSYan, Zheng CEPH_OSD_OP_WRITE, 105054ea0046SIlya Dryomov CEPH_OSD_FLAG_WRITE, 10511f934b00SYan, Zheng snapc, ceph_wbc.truncate_seq, 10521f934b00SYan, Zheng ceph_wbc.truncate_size, true); 10535b64640cSYan, Zheng BUG_ON(IS_ERR(req)); 10545b64640cSYan, Zheng } 10555b64640cSYan, Zheng BUG_ON(len < page_offset(pages[locked_pages - 1]) + 105609cbfeafSKirill A. Shutemov PAGE_SIZE - offset); 10575b64640cSYan, Zheng 10585b64640cSYan, Zheng req->r_callback = writepages_finish; 10595b64640cSYan, Zheng req->r_inode = inode; 10605b64640cSYan, Zheng 10615b64640cSYan, Zheng /* Format the osd request message and submit the write */ 10625b64640cSYan, Zheng len = 0; 10635b64640cSYan, Zheng data_pages = pages; 10645b64640cSYan, Zheng op_idx = 0; 10655b64640cSYan, Zheng for (i = 0; i < locked_pages; i++) { 10665b64640cSYan, Zheng u64 cur_offset = page_offset(pages[i]); 10675b64640cSYan, Zheng if (offset + len != cur_offset) { 10683fb99d48SYanhu Cao if (op_idx + 1 == req->r_num_ops) 10695b64640cSYan, Zheng break; 10705b64640cSYan, Zheng osd_req_op_extent_dup_last(req, op_idx, 10715b64640cSYan, Zheng cur_offset - offset); 10725b64640cSYan, Zheng dout("writepages got pages at %llu~%llu\n", 10735b64640cSYan, Zheng offset, len); 10745b64640cSYan, Zheng osd_req_op_extent_osd_data_pages(req, op_idx, 10755b64640cSYan, Zheng data_pages, len, 0, 10765b64640cSYan, Zheng !!pool, false); 10775b64640cSYan, Zheng osd_req_op_extent_update(req, op_idx, len); 10785b64640cSYan, Zheng 10795b64640cSYan, Zheng len = 0; 10805b64640cSYan, Zheng offset = cur_offset; 10815b64640cSYan, Zheng data_pages = pages + i; 10825b64640cSYan, Zheng op_idx++; 10835b64640cSYan, Zheng } 10845b64640cSYan, Zheng 10855b64640cSYan, Zheng set_page_writeback(pages[i]); 108609cbfeafSKirill A. Shutemov len += PAGE_SIZE; 10875b64640cSYan, Zheng } 10885b64640cSYan, Zheng 10891f934b00SYan, Zheng if (ceph_wbc.size_stable) { 10901f934b00SYan, Zheng len = min(len, ceph_wbc.i_size - offset); 10915b64640cSYan, Zheng } else if (i == locked_pages) { 1092e1966b49SYan, Zheng /* writepages_finish() clears writeback pages 1093e1966b49SYan, Zheng * according to the data length, so make sure 1094e1966b49SYan, Zheng * data length covers all locked pages */ 109509cbfeafSKirill A. Shutemov u64 min_len = len + 1 - PAGE_SIZE; 10961f934b00SYan, Zheng len = get_writepages_data_length(inode, pages[i - 1], 10971f934b00SYan, Zheng offset); 10985b64640cSYan, Zheng len = max(len, min_len); 1099e1966b49SYan, Zheng } 11005b64640cSYan, Zheng dout("writepages got pages at %llu~%llu\n", offset, len); 11011d3576fdSSage Weil 11025b64640cSYan, Zheng osd_req_op_extent_osd_data_pages(req, op_idx, data_pages, len, 11035b64640cSYan, Zheng 0, !!pool, false); 11045b64640cSYan, Zheng osd_req_op_extent_update(req, op_idx, len); 1105e5975c7cSAlex Elder 11065b64640cSYan, Zheng BUG_ON(op_idx + 1 != req->r_num_ops); 11075b64640cSYan, Zheng 1108e5975c7cSAlex Elder pool = NULL; 11095b64640cSYan, Zheng if (i < locked_pages) { 11105b64640cSYan, Zheng BUG_ON(num_ops <= req->r_num_ops); 11115b64640cSYan, Zheng num_ops -= req->r_num_ops; 11125b64640cSYan, Zheng locked_pages -= i; 1113e5975c7cSAlex Elder 11145b64640cSYan, Zheng /* allocate new pages array for next request */ 11155b64640cSYan, Zheng data_pages = pages; 11166da2ec56SKees Cook pages = kmalloc_array(locked_pages, sizeof(*pages), 11175b64640cSYan, Zheng GFP_NOFS); 11185b64640cSYan, Zheng if (!pages) { 11195b64640cSYan, Zheng pool = fsc->wb_pagevec_pool; 11205b64640cSYan, Zheng pages = mempool_alloc(pool, GFP_NOFS); 11215b64640cSYan, Zheng BUG_ON(!pages); 11225b64640cSYan, Zheng } 11235b64640cSYan, Zheng memcpy(pages, data_pages + i, 11245b64640cSYan, Zheng locked_pages * sizeof(*pages)); 11255b64640cSYan, Zheng memset(data_pages + i, 0, 11265b64640cSYan, Zheng locked_pages * sizeof(*pages)); 11275b64640cSYan, Zheng } else { 11285b64640cSYan, Zheng BUG_ON(num_ops != req->r_num_ops); 11295b64640cSYan, Zheng index = pages[i - 1]->index + 1; 11305b64640cSYan, Zheng /* request message now owns the pages array */ 11315b64640cSYan, Zheng pages = NULL; 11325b64640cSYan, Zheng } 1133e5975c7cSAlex Elder 1134fac02ddfSArnd Bergmann req->r_mtime = inode->i_mtime; 11359d6fcb08SSage Weil rc = ceph_osdc_start_request(&fsc->client->osdc, req, true); 11369d6fcb08SSage Weil BUG_ON(rc); 11371d3576fdSSage Weil req = NULL; 11381d3576fdSSage Weil 11395b64640cSYan, Zheng wbc->nr_to_write -= i; 11405b64640cSYan, Zheng if (pages) 11415b64640cSYan, Zheng goto new_request; 11425b64640cSYan, Zheng 11432a2d927eSYan, Zheng /* 11442a2d927eSYan, Zheng * We stop writing back only if we are not doing 11452a2d927eSYan, Zheng * integrity sync. In case of integrity sync we have to 11462a2d927eSYan, Zheng * keep going until we have written all the pages 11472a2d927eSYan, Zheng * we tagged for writeback prior to entering this loop. 11482a2d927eSYan, Zheng */ 11492a2d927eSYan, Zheng if (wbc->nr_to_write <= 0 && wbc->sync_mode == WB_SYNC_NONE) 1150af9cc401SYan, Zheng done = true; 11511d3576fdSSage Weil 11521d3576fdSSage Weil release_pvec_pages: 11531d3576fdSSage Weil dout("pagevec_release on %d pages (%p)\n", (int)pvec.nr, 11541d3576fdSSage Weil pvec.nr ? pvec.pages[0] : NULL); 11551d3576fdSSage Weil pagevec_release(&pvec); 11561d3576fdSSage Weil } 11571d3576fdSSage Weil 11581d3576fdSSage Weil if (should_loop && !done) { 11591d3576fdSSage Weil /* more to do; loop back to beginning of file */ 11601d3576fdSSage Weil dout("writepages looping back to beginning of file\n"); 11612a2d927eSYan, Zheng end = start_index - 1; /* OK even when start_index == 0 */ 1162f275635eSYan, Zheng 1163f275635eSYan, Zheng /* to write dirty pages associated with next snapc, 1164f275635eSYan, Zheng * we need to wait until current writes complete */ 1165f275635eSYan, Zheng if (wbc->sync_mode != WB_SYNC_NONE && 1166f275635eSYan, Zheng start_index == 0 && /* all dirty pages were checked */ 1167f275635eSYan, Zheng !ceph_wbc.head_snapc) { 1168f275635eSYan, Zheng struct page *page; 1169f275635eSYan, Zheng unsigned i, nr; 1170f275635eSYan, Zheng index = 0; 1171f275635eSYan, Zheng while ((index <= end) && 1172f275635eSYan, Zheng (nr = pagevec_lookup_tag(&pvec, mapping, &index, 117367fd707fSJan Kara PAGECACHE_TAG_WRITEBACK))) { 1174f275635eSYan, Zheng for (i = 0; i < nr; i++) { 1175f275635eSYan, Zheng page = pvec.pages[i]; 1176f275635eSYan, Zheng if (page_snap_context(page) != snapc) 1177f275635eSYan, Zheng continue; 1178f275635eSYan, Zheng wait_on_page_writeback(page); 1179f275635eSYan, Zheng } 1180f275635eSYan, Zheng pagevec_release(&pvec); 1181f275635eSYan, Zheng cond_resched(); 1182f275635eSYan, Zheng } 1183f275635eSYan, Zheng } 1184f275635eSYan, Zheng 11852a2d927eSYan, Zheng start_index = 0; 11861d3576fdSSage Weil index = 0; 11871d3576fdSSage Weil goto retry; 11881d3576fdSSage Weil } 11891d3576fdSSage Weil 11901d3576fdSSage Weil if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0)) 11911d3576fdSSage Weil mapping->writeback_index = index; 11921d3576fdSSage Weil 11931d3576fdSSage Weil out: 11941d3576fdSSage Weil ceph_osdc_put_request(req); 11952a2d927eSYan, Zheng ceph_put_snap_context(last_snapc); 11962a2d927eSYan, Zheng dout("writepages dend - startone, rc = %d\n", rc); 11971d3576fdSSage Weil return rc; 11981d3576fdSSage Weil } 11991d3576fdSSage Weil 12001d3576fdSSage Weil 12011d3576fdSSage Weil 12021d3576fdSSage Weil /* 12031d3576fdSSage Weil * See if a given @snapc is either writeable, or already written. 12041d3576fdSSage Weil */ 12051d3576fdSSage Weil static int context_is_writeable_or_written(struct inode *inode, 12061d3576fdSSage Weil struct ceph_snap_context *snapc) 12071d3576fdSSage Weil { 120805455e11SYan, Zheng struct ceph_snap_context *oldest = get_oldest_context(inode, NULL, NULL); 12096298a337SSage Weil int ret = !oldest || snapc->seq <= oldest->seq; 12106298a337SSage Weil 12116298a337SSage Weil ceph_put_snap_context(oldest); 12126298a337SSage Weil return ret; 12131d3576fdSSage Weil } 12141d3576fdSSage Weil 12151d3576fdSSage Weil /* 12161d3576fdSSage Weil * We are only allowed to write into/dirty the page if the page is 12171d3576fdSSage Weil * clean, or already dirty within the same snap context. 12188f883c24SSage Weil * 12198f883c24SSage Weil * called with page locked. 12208f883c24SSage Weil * return success with page locked, 12218f883c24SSage Weil * or any failure (incl -EAGAIN) with page unlocked. 12221d3576fdSSage Weil */ 12234af6b225SYehuda Sadeh static int ceph_update_writeable_page(struct file *file, 12244af6b225SYehuda Sadeh loff_t pos, unsigned len, 12254af6b225SYehuda Sadeh struct page *page) 12261d3576fdSSage Weil { 1227496ad9aaSAl Viro struct inode *inode = file_inode(file); 12286c93df5dSYan, Zheng struct ceph_fs_client *fsc = ceph_inode_to_client(inode); 12291d3576fdSSage Weil struct ceph_inode_info *ci = ceph_inode(inode); 123009cbfeafSKirill A. Shutemov loff_t page_off = pos & PAGE_MASK; 123109cbfeafSKirill A. Shutemov int pos_in_page = pos & ~PAGE_MASK; 12321d3576fdSSage Weil int end_in_page = pos_in_page + len; 12331d3576fdSSage Weil loff_t i_size; 12341d3576fdSSage Weil int r; 123580e755feSSage Weil struct ceph_snap_context *snapc, *oldest; 12361d3576fdSSage Weil 123752953d55SSeraphime Kirkovski if (READ_ONCE(fsc->mount_state) == CEPH_MOUNT_SHUTDOWN) { 12386c93df5dSYan, Zheng dout(" page %p forced umount\n", page); 12396c93df5dSYan, Zheng unlock_page(page); 12406c93df5dSYan, Zheng return -EIO; 12416c93df5dSYan, Zheng } 12426c93df5dSYan, Zheng 12431d3576fdSSage Weil retry_locked: 12441d3576fdSSage Weil /* writepages currently holds page lock, but if we change that later, */ 12451d3576fdSSage Weil wait_on_page_writeback(page); 12461d3576fdSSage Weil 124761600ef8SYan, Zheng snapc = page_snap_context(page); 124880e755feSSage Weil if (snapc && snapc != ci->i_head_snapc) { 12491d3576fdSSage Weil /* 12501d3576fdSSage Weil * this page is already dirty in another (older) snap 12511d3576fdSSage Weil * context! is it writeable now? 12521d3576fdSSage Weil */ 125305455e11SYan, Zheng oldest = get_oldest_context(inode, NULL, NULL); 125480e755feSSage Weil if (snapc->seq > oldest->seq) { 12556298a337SSage Weil ceph_put_snap_context(oldest); 12561d3576fdSSage Weil dout(" page %p snapc %p not current or oldest\n", 12576298a337SSage Weil page, snapc); 12581d3576fdSSage Weil /* 12591d3576fdSSage Weil * queue for writeback, and wait for snapc to 12601d3576fdSSage Weil * be writeable or written 12611d3576fdSSage Weil */ 12626298a337SSage Weil snapc = ceph_get_snap_context(snapc); 12631d3576fdSSage Weil unlock_page(page); 12643c6f6b79SSage Weil ceph_queue_writeback(inode); 1265a78bbd4bSYan, Zheng r = wait_event_killable(ci->i_cap_wq, 12661d3576fdSSage Weil context_is_writeable_or_written(inode, snapc)); 12671d3576fdSSage Weil ceph_put_snap_context(snapc); 12688f883c24SSage Weil if (r == -ERESTARTSYS) 12698f883c24SSage Weil return r; 12704af6b225SYehuda Sadeh return -EAGAIN; 12711d3576fdSSage Weil } 12726298a337SSage Weil ceph_put_snap_context(oldest); 12731d3576fdSSage Weil 12741d3576fdSSage Weil /* yay, writeable, do it now (without dropping page lock) */ 12751d3576fdSSage Weil dout(" page %p snapc %p not current, but oldest\n", 12761d3576fdSSage Weil page, snapc); 12771d3576fdSSage Weil if (!clear_page_dirty_for_io(page)) 12781d3576fdSSage Weil goto retry_locked; 12791d3576fdSSage Weil r = writepage_nounlock(page, NULL); 12801d3576fdSSage Weil if (r < 0) 1281dd2bc473SYan, Zheng goto fail_unlock; 12821d3576fdSSage Weil goto retry_locked; 12831d3576fdSSage Weil } 12841d3576fdSSage Weil 12851d3576fdSSage Weil if (PageUptodate(page)) { 12861d3576fdSSage Weil dout(" page %p already uptodate\n", page); 12871d3576fdSSage Weil return 0; 12881d3576fdSSage Weil } 12891d3576fdSSage Weil 12901d3576fdSSage Weil /* full page? */ 129109cbfeafSKirill A. Shutemov if (pos_in_page == 0 && len == PAGE_SIZE) 12921d3576fdSSage Weil return 0; 12931d3576fdSSage Weil 12941d3576fdSSage Weil /* past end of file? */ 129599c88e69SYan, Zheng i_size = i_size_read(inode); 12961d3576fdSSage Weil 12971d3576fdSSage Weil if (page_off >= i_size || 12981d3576fdSSage Weil (pos_in_page == 0 && (pos+len) >= i_size && 129909cbfeafSKirill A. Shutemov end_in_page - pos_in_page != PAGE_SIZE)) { 13001d3576fdSSage Weil dout(" zeroing %p 0 - %d and %d - %d\n", 130109cbfeafSKirill A. Shutemov page, pos_in_page, end_in_page, (int)PAGE_SIZE); 13021d3576fdSSage Weil zero_user_segments(page, 13031d3576fdSSage Weil 0, pos_in_page, 130409cbfeafSKirill A. Shutemov end_in_page, PAGE_SIZE); 13051d3576fdSSage Weil return 0; 13061d3576fdSSage Weil } 13071d3576fdSSage Weil 13081d3576fdSSage Weil /* we need to read it. */ 1309dd2bc473SYan, Zheng r = ceph_do_readpage(file, page); 1310dd2bc473SYan, Zheng if (r < 0) { 1311dd2bc473SYan, Zheng if (r == -EINPROGRESS) 1312dd2bc473SYan, Zheng return -EAGAIN; 1313dd2bc473SYan, Zheng goto fail_unlock; 1314dd2bc473SYan, Zheng } 13151d3576fdSSage Weil goto retry_locked; 1316dd2bc473SYan, Zheng fail_unlock: 13171d3576fdSSage Weil unlock_page(page); 13181d3576fdSSage Weil return r; 13191d3576fdSSage Weil } 13201d3576fdSSage Weil 13211d3576fdSSage Weil /* 13224af6b225SYehuda Sadeh * We are only allowed to write into/dirty the page if the page is 13234af6b225SYehuda Sadeh * clean, or already dirty within the same snap context. 13244af6b225SYehuda Sadeh */ 13254af6b225SYehuda Sadeh static int ceph_write_begin(struct file *file, struct address_space *mapping, 13264af6b225SYehuda Sadeh loff_t pos, unsigned len, unsigned flags, 13274af6b225SYehuda Sadeh struct page **pagep, void **fsdata) 13284af6b225SYehuda Sadeh { 1329496ad9aaSAl Viro struct inode *inode = file_inode(file); 13304af6b225SYehuda Sadeh struct page *page; 133109cbfeafSKirill A. Shutemov pgoff_t index = pos >> PAGE_SHIFT; 13327971bd92SSage Weil int r; 13334af6b225SYehuda Sadeh 13344af6b225SYehuda Sadeh do { 13354af6b225SYehuda Sadeh /* get a page */ 13364af6b225SYehuda Sadeh page = grab_cache_page_write_begin(mapping, index, 0); 13377971bd92SSage Weil if (!page) 13387971bd92SSage Weil return -ENOMEM; 13394af6b225SYehuda Sadeh 13404af6b225SYehuda Sadeh dout("write_begin file %p inode %p page %p %d~%d\n", file, 13414af6b225SYehuda Sadeh inode, page, (int)pos, (int)len); 13424af6b225SYehuda Sadeh 13434af6b225SYehuda Sadeh r = ceph_update_writeable_page(file, pos, len, page); 1344c1d00b2dSTaesoo Kim if (r < 0) 134509cbfeafSKirill A. Shutemov put_page(page); 1346c1d00b2dSTaesoo Kim else 1347c1d00b2dSTaesoo Kim *pagep = page; 13484af6b225SYehuda Sadeh } while (r == -EAGAIN); 13494af6b225SYehuda Sadeh 13504af6b225SYehuda Sadeh return r; 13514af6b225SYehuda Sadeh } 13524af6b225SYehuda Sadeh 13534af6b225SYehuda Sadeh /* 13541d3576fdSSage Weil * we don't do anything in here that simple_write_end doesn't do 13555dda377cSYan, Zheng * except adjust dirty page accounting 13561d3576fdSSage Weil */ 13571d3576fdSSage Weil static int ceph_write_end(struct file *file, struct address_space *mapping, 13581d3576fdSSage Weil loff_t pos, unsigned len, unsigned copied, 13591d3576fdSSage Weil struct page *page, void *fsdata) 13601d3576fdSSage Weil { 1361496ad9aaSAl Viro struct inode *inode = file_inode(file); 1362efb0ca76SYan, Zheng bool check_cap = false; 13631d3576fdSSage Weil 13641d3576fdSSage Weil dout("write_end file %p inode %p page %p %d~%d (%d)\n", file, 13651d3576fdSSage Weil inode, page, (int)pos, (int)copied, (int)len); 13661d3576fdSSage Weil 13671d3576fdSSage Weil /* zero the stale part of the page if we did a short copy */ 1368b9de313cSAl Viro if (!PageUptodate(page)) { 1369b9de313cSAl Viro if (copied < len) { 1370b9de313cSAl Viro copied = 0; 1371b9de313cSAl Viro goto out; 1372b9de313cSAl Viro } 1373b9de313cSAl Viro SetPageUptodate(page); 1374b9de313cSAl Viro } 13751d3576fdSSage Weil 13761d3576fdSSage Weil /* did file size increase? */ 137799c88e69SYan, Zheng if (pos+copied > i_size_read(inode)) 13781d3576fdSSage Weil check_cap = ceph_inode_set_size(inode, pos+copied); 13791d3576fdSSage Weil 13801d3576fdSSage Weil set_page_dirty(page); 13811d3576fdSSage Weil 1382b9de313cSAl Viro out: 13831d3576fdSSage Weil unlock_page(page); 138409cbfeafSKirill A. Shutemov put_page(page); 13851d3576fdSSage Weil 13861d3576fdSSage Weil if (check_cap) 13871d3576fdSSage Weil ceph_check_caps(ceph_inode(inode), CHECK_CAPS_AUTHONLY, NULL); 13881d3576fdSSage Weil 13891d3576fdSSage Weil return copied; 13901d3576fdSSage Weil } 13911d3576fdSSage Weil 13921d3576fdSSage Weil /* 13931d3576fdSSage Weil * we set .direct_IO to indicate direct io is supported, but since we 13941d3576fdSSage Weil * intercept O_DIRECT reads and writes early, this function should 13951d3576fdSSage Weil * never get called. 13961d3576fdSSage Weil */ 1397c8b8e32dSChristoph Hellwig static ssize_t ceph_direct_io(struct kiocb *iocb, struct iov_iter *iter) 13981d3576fdSSage Weil { 13991d3576fdSSage Weil WARN_ON(1); 14001d3576fdSSage Weil return -EINVAL; 14011d3576fdSSage Weil } 14021d3576fdSSage Weil 14031d3576fdSSage Weil const struct address_space_operations ceph_aops = { 14041d3576fdSSage Weil .readpage = ceph_readpage, 14051d3576fdSSage Weil .readpages = ceph_readpages, 14061d3576fdSSage Weil .writepage = ceph_writepage, 14071d3576fdSSage Weil .writepages = ceph_writepages_start, 14081d3576fdSSage Weil .write_begin = ceph_write_begin, 14091d3576fdSSage Weil .write_end = ceph_write_end, 14101d3576fdSSage Weil .set_page_dirty = ceph_set_page_dirty, 14111d3576fdSSage Weil .invalidatepage = ceph_invalidatepage, 14121d3576fdSSage Weil .releasepage = ceph_releasepage, 14131d3576fdSSage Weil .direct_IO = ceph_direct_io, 14141d3576fdSSage Weil }; 14151d3576fdSSage Weil 14164f7e89f6SYan, Zheng static void ceph_block_sigs(sigset_t *oldset) 14174f7e89f6SYan, Zheng { 14184f7e89f6SYan, Zheng sigset_t mask; 14194f7e89f6SYan, Zheng siginitsetinv(&mask, sigmask(SIGKILL)); 14204f7e89f6SYan, Zheng sigprocmask(SIG_BLOCK, &mask, oldset); 14214f7e89f6SYan, Zheng } 14224f7e89f6SYan, Zheng 14234f7e89f6SYan, Zheng static void ceph_restore_sigs(sigset_t *oldset) 14244f7e89f6SYan, Zheng { 14254f7e89f6SYan, Zheng sigprocmask(SIG_SETMASK, oldset, NULL); 14264f7e89f6SYan, Zheng } 14271d3576fdSSage Weil 14281d3576fdSSage Weil /* 14291d3576fdSSage Weil * vm ops 14301d3576fdSSage Weil */ 143124499847SSouptick Joarder static vm_fault_t ceph_filemap_fault(struct vm_fault *vmf) 143261f68816SYan, Zheng { 143311bac800SDave Jiang struct vm_area_struct *vma = vmf->vma; 143461f68816SYan, Zheng struct inode *inode = file_inode(vma->vm_file); 143561f68816SYan, Zheng struct ceph_inode_info *ci = ceph_inode(inode); 143661f68816SYan, Zheng struct ceph_file_info *fi = vma->vm_file->private_data; 14373738daa6SYan, Zheng struct page *pinned_page = NULL; 143809cbfeafSKirill A. Shutemov loff_t off = vmf->pgoff << PAGE_SHIFT; 143924499847SSouptick Joarder int want, got, err; 14404f7e89f6SYan, Zheng sigset_t oldset; 144124499847SSouptick Joarder vm_fault_t ret = VM_FAULT_SIGBUS; 14424f7e89f6SYan, Zheng 14434f7e89f6SYan, Zheng ceph_block_sigs(&oldset); 144461f68816SYan, Zheng 144561f68816SYan, Zheng dout("filemap_fault %p %llx.%llx %llu~%zd trying to get caps\n", 144609cbfeafSKirill A. Shutemov inode, ceph_vinop(inode), off, (size_t)PAGE_SIZE); 144761f68816SYan, Zheng if (fi->fmode & CEPH_FILE_MODE_LAZY) 144861f68816SYan, Zheng want = CEPH_CAP_FILE_CACHE | CEPH_CAP_FILE_LAZYIO; 144961f68816SYan, Zheng else 145061f68816SYan, Zheng want = CEPH_CAP_FILE_CACHE; 14514f7e89f6SYan, Zheng 145261f68816SYan, Zheng got = 0; 145324499847SSouptick Joarder err = ceph_get_caps(ci, CEPH_CAP_FILE_RD, want, -1, &got, &pinned_page); 145424499847SSouptick Joarder if (err < 0) 14554f7e89f6SYan, Zheng goto out_restore; 14566ce026e4SYan, Zheng 145761f68816SYan, Zheng dout("filemap_fault %p %llu~%zd got cap refs on %s\n", 145809cbfeafSKirill A. Shutemov inode, off, (size_t)PAGE_SIZE, ceph_cap_string(got)); 145961f68816SYan, Zheng 146083701246SYan, Zheng if ((got & (CEPH_CAP_FILE_CACHE | CEPH_CAP_FILE_LAZYIO)) || 14612b1ac852SYan, Zheng ci->i_inline_version == CEPH_INLINE_NONE) { 14625d988308SYan, Zheng CEPH_DEFINE_RW_CONTEXT(rw_ctx, got); 14635d988308SYan, Zheng ceph_add_rw_context(fi, &rw_ctx); 146411bac800SDave Jiang ret = filemap_fault(vmf); 14655d988308SYan, Zheng ceph_del_rw_context(fi, &rw_ctx); 146624499847SSouptick Joarder dout("filemap_fault %p %llu~%zd drop cap refs %s ret %x\n", 146724499847SSouptick Joarder inode, off, (size_t)PAGE_SIZE, 146824499847SSouptick Joarder ceph_cap_string(got), ret); 14692b1ac852SYan, Zheng } else 147024499847SSouptick Joarder err = -EAGAIN; 147161f68816SYan, Zheng 14723738daa6SYan, Zheng if (pinned_page) 147309cbfeafSKirill A. Shutemov put_page(pinned_page); 147461f68816SYan, Zheng ceph_put_cap_refs(ci, got); 147561f68816SYan, Zheng 147624499847SSouptick Joarder if (err != -EAGAIN) 14774f7e89f6SYan, Zheng goto out_restore; 147883701246SYan, Zheng 147983701246SYan, Zheng /* read inline data */ 148009cbfeafSKirill A. Shutemov if (off >= PAGE_SIZE) { 148183701246SYan, Zheng /* does not support inline data > PAGE_SIZE */ 148283701246SYan, Zheng ret = VM_FAULT_SIGBUS; 148383701246SYan, Zheng } else { 148483701246SYan, Zheng struct address_space *mapping = inode->i_mapping; 148583701246SYan, Zheng struct page *page = find_or_create_page(mapping, 0, 1486c62d2555SMichal Hocko mapping_gfp_constraint(mapping, 1487c62d2555SMichal Hocko ~__GFP_FS)); 148883701246SYan, Zheng if (!page) { 148983701246SYan, Zheng ret = VM_FAULT_OOM; 14904f7e89f6SYan, Zheng goto out_inline; 149183701246SYan, Zheng } 149224499847SSouptick Joarder err = __ceph_do_getattr(inode, page, 149383701246SYan, Zheng CEPH_STAT_CAP_INLINE_DATA, true); 149424499847SSouptick Joarder if (err < 0 || off >= i_size_read(inode)) { 149583701246SYan, Zheng unlock_page(page); 149609cbfeafSKirill A. Shutemov put_page(page); 1497c64a2b05SSouptick Joarder ret = vmf_error(err); 14984f7e89f6SYan, Zheng goto out_inline; 149983701246SYan, Zheng } 150024499847SSouptick Joarder if (err < PAGE_SIZE) 150124499847SSouptick Joarder zero_user_segment(page, err, PAGE_SIZE); 150283701246SYan, Zheng else 150383701246SYan, Zheng flush_dcache_page(page); 150483701246SYan, Zheng SetPageUptodate(page); 150583701246SYan, Zheng vmf->page = page; 150683701246SYan, Zheng ret = VM_FAULT_MAJOR | VM_FAULT_LOCKED; 15074f7e89f6SYan, Zheng out_inline: 150824499847SSouptick Joarder dout("filemap_fault %p %llu~%zd read inline data ret %x\n", 150909cbfeafSKirill A. Shutemov inode, off, (size_t)PAGE_SIZE, ret); 15104f7e89f6SYan, Zheng } 15114f7e89f6SYan, Zheng out_restore: 15124f7e89f6SYan, Zheng ceph_restore_sigs(&oldset); 151324499847SSouptick Joarder if (err < 0) 151424499847SSouptick Joarder ret = vmf_error(err); 15156ce026e4SYan, Zheng 151661f68816SYan, Zheng return ret; 151761f68816SYan, Zheng } 15181d3576fdSSage Weil 15191d3576fdSSage Weil /* 15201d3576fdSSage Weil * Reuse write_begin here for simplicity. 15211d3576fdSSage Weil */ 152224499847SSouptick Joarder static vm_fault_t ceph_page_mkwrite(struct vm_fault *vmf) 15231d3576fdSSage Weil { 152411bac800SDave Jiang struct vm_area_struct *vma = vmf->vma; 1525496ad9aaSAl Viro struct inode *inode = file_inode(vma->vm_file); 152661f68816SYan, Zheng struct ceph_inode_info *ci = ceph_inode(inode); 152761f68816SYan, Zheng struct ceph_file_info *fi = vma->vm_file->private_data; 1528f66fd9f0SYan, Zheng struct ceph_cap_flush *prealloc_cf; 152961f68816SYan, Zheng struct page *page = vmf->page; 15306285bc23SAlex Elder loff_t off = page_offset(page); 153161f68816SYan, Zheng loff_t size = i_size_read(inode); 153261f68816SYan, Zheng size_t len; 153324499847SSouptick Joarder int want, got, err; 15344f7e89f6SYan, Zheng sigset_t oldset; 153524499847SSouptick Joarder vm_fault_t ret = VM_FAULT_SIGBUS; 15361d3576fdSSage Weil 1537f66fd9f0SYan, Zheng prealloc_cf = ceph_alloc_cap_flush(); 1538f66fd9f0SYan, Zheng if (!prealloc_cf) 15396ce026e4SYan, Zheng return VM_FAULT_OOM; 1540f66fd9f0SYan, Zheng 15414f7e89f6SYan, Zheng ceph_block_sigs(&oldset); 15421d3576fdSSage Weil 154328127bddSYan, Zheng if (ci->i_inline_version != CEPH_INLINE_NONE) { 154428127bddSYan, Zheng struct page *locked_page = NULL; 154528127bddSYan, Zheng if (off == 0) { 154628127bddSYan, Zheng lock_page(page); 154728127bddSYan, Zheng locked_page = page; 154828127bddSYan, Zheng } 154924499847SSouptick Joarder err = ceph_uninline_data(vma->vm_file, locked_page); 155028127bddSYan, Zheng if (locked_page) 155128127bddSYan, Zheng unlock_page(locked_page); 155224499847SSouptick Joarder if (err < 0) 1553f66fd9f0SYan, Zheng goto out_free; 1554f66fd9f0SYan, Zheng } 155528127bddSYan, Zheng 155609cbfeafSKirill A. Shutemov if (off + PAGE_SIZE <= size) 155709cbfeafSKirill A. Shutemov len = PAGE_SIZE; 15581d3576fdSSage Weil else 155909cbfeafSKirill A. Shutemov len = size & ~PAGE_MASK; 15601d3576fdSSage Weil 156161f68816SYan, Zheng dout("page_mkwrite %p %llx.%llx %llu~%zd getting caps i_size %llu\n", 156261f68816SYan, Zheng inode, ceph_vinop(inode), off, len, size); 156361f68816SYan, Zheng if (fi->fmode & CEPH_FILE_MODE_LAZY) 156461f68816SYan, Zheng want = CEPH_CAP_FILE_BUFFER | CEPH_CAP_FILE_LAZYIO; 156561f68816SYan, Zheng else 156661f68816SYan, Zheng want = CEPH_CAP_FILE_BUFFER; 15674f7e89f6SYan, Zheng 156861f68816SYan, Zheng got = 0; 156924499847SSouptick Joarder err = ceph_get_caps(ci, CEPH_CAP_FILE_WR, want, off + len, 15703738daa6SYan, Zheng &got, NULL); 157124499847SSouptick Joarder if (err < 0) 1572f66fd9f0SYan, Zheng goto out_free; 15736ce026e4SYan, Zheng 157461f68816SYan, Zheng dout("page_mkwrite %p %llu~%zd got cap refs on %s\n", 157561f68816SYan, Zheng inode, off, len, ceph_cap_string(got)); 157661f68816SYan, Zheng 157761f68816SYan, Zheng /* Update time before taking page lock */ 157861f68816SYan, Zheng file_update_time(vma->vm_file); 15794af6b225SYehuda Sadeh 1580f0b33df5SYan, Zheng do { 15814af6b225SYehuda Sadeh lock_page(page); 15824af6b225SYehuda Sadeh 15836ce026e4SYan, Zheng if ((off > size) || (page->mapping != inode->i_mapping)) { 1584f9cac5acSYan, Zheng unlock_page(page); 15856ce026e4SYan, Zheng ret = VM_FAULT_NOPAGE; 1586f0b33df5SYan, Zheng break; 1587f9cac5acSYan, Zheng } 15884af6b225SYehuda Sadeh 158924499847SSouptick Joarder err = ceph_update_writeable_page(vma->vm_file, off, len, page); 159024499847SSouptick Joarder if (err >= 0) { 15914af6b225SYehuda Sadeh /* success. we'll keep the page locked. */ 15921d3576fdSSage Weil set_page_dirty(page); 15931d3576fdSSage Weil ret = VM_FAULT_LOCKED; 15941d3576fdSSage Weil } 159524499847SSouptick Joarder } while (err == -EAGAIN); 1596f0b33df5SYan, Zheng 159728127bddSYan, Zheng if (ret == VM_FAULT_LOCKED || 159828127bddSYan, Zheng ci->i_inline_version != CEPH_INLINE_NONE) { 159961f68816SYan, Zheng int dirty; 160061f68816SYan, Zheng spin_lock(&ci->i_ceph_lock); 160128127bddSYan, Zheng ci->i_inline_version = CEPH_INLINE_NONE; 1602f66fd9f0SYan, Zheng dirty = __ceph_mark_dirty_caps(ci, CEPH_CAP_FILE_WR, 1603f66fd9f0SYan, Zheng &prealloc_cf); 160461f68816SYan, Zheng spin_unlock(&ci->i_ceph_lock); 160561f68816SYan, Zheng if (dirty) 160661f68816SYan, Zheng __mark_inode_dirty(inode, dirty); 160761f68816SYan, Zheng } 160861f68816SYan, Zheng 160924499847SSouptick Joarder dout("page_mkwrite %p %llu~%zd dropping cap refs on %s ret %x\n", 161061f68816SYan, Zheng inode, off, len, ceph_cap_string(got), ret); 161161f68816SYan, Zheng ceph_put_cap_refs(ci, got); 1612f66fd9f0SYan, Zheng out_free: 16134f7e89f6SYan, Zheng ceph_restore_sigs(&oldset); 1614f66fd9f0SYan, Zheng ceph_free_cap_flush(prealloc_cf); 161524499847SSouptick Joarder if (err < 0) 161624499847SSouptick Joarder ret = vmf_error(err); 16171d3576fdSSage Weil return ret; 16181d3576fdSSage Weil } 16191d3576fdSSage Weil 162031c542a1SYan, Zheng void ceph_fill_inline_data(struct inode *inode, struct page *locked_page, 162131c542a1SYan, Zheng char *data, size_t len) 162231c542a1SYan, Zheng { 162331c542a1SYan, Zheng struct address_space *mapping = inode->i_mapping; 162431c542a1SYan, Zheng struct page *page; 162531c542a1SYan, Zheng 162631c542a1SYan, Zheng if (locked_page) { 162731c542a1SYan, Zheng page = locked_page; 162831c542a1SYan, Zheng } else { 162931c542a1SYan, Zheng if (i_size_read(inode) == 0) 163031c542a1SYan, Zheng return; 163131c542a1SYan, Zheng page = find_or_create_page(mapping, 0, 1632c62d2555SMichal Hocko mapping_gfp_constraint(mapping, 1633c62d2555SMichal Hocko ~__GFP_FS)); 163431c542a1SYan, Zheng if (!page) 163531c542a1SYan, Zheng return; 163631c542a1SYan, Zheng if (PageUptodate(page)) { 163731c542a1SYan, Zheng unlock_page(page); 163809cbfeafSKirill A. Shutemov put_page(page); 163931c542a1SYan, Zheng return; 164031c542a1SYan, Zheng } 164131c542a1SYan, Zheng } 164231c542a1SYan, Zheng 16430668ff52SIlya Dryomov dout("fill_inline_data %p %llx.%llx len %zu locked_page %p\n", 164431c542a1SYan, Zheng inode, ceph_vinop(inode), len, locked_page); 164531c542a1SYan, Zheng 164631c542a1SYan, Zheng if (len > 0) { 164731c542a1SYan, Zheng void *kaddr = kmap_atomic(page); 164831c542a1SYan, Zheng memcpy(kaddr, data, len); 164931c542a1SYan, Zheng kunmap_atomic(kaddr); 165031c542a1SYan, Zheng } 165131c542a1SYan, Zheng 165231c542a1SYan, Zheng if (page != locked_page) { 165309cbfeafSKirill A. Shutemov if (len < PAGE_SIZE) 165409cbfeafSKirill A. Shutemov zero_user_segment(page, len, PAGE_SIZE); 165531c542a1SYan, Zheng else 165631c542a1SYan, Zheng flush_dcache_page(page); 165731c542a1SYan, Zheng 165831c542a1SYan, Zheng SetPageUptodate(page); 165931c542a1SYan, Zheng unlock_page(page); 166009cbfeafSKirill A. Shutemov put_page(page); 166131c542a1SYan, Zheng } 166231c542a1SYan, Zheng } 166331c542a1SYan, Zheng 166428127bddSYan, Zheng int ceph_uninline_data(struct file *filp, struct page *locked_page) 166528127bddSYan, Zheng { 166628127bddSYan, Zheng struct inode *inode = file_inode(filp); 166728127bddSYan, Zheng struct ceph_inode_info *ci = ceph_inode(inode); 166828127bddSYan, Zheng struct ceph_fs_client *fsc = ceph_inode_to_client(inode); 166928127bddSYan, Zheng struct ceph_osd_request *req; 167028127bddSYan, Zheng struct page *page = NULL; 167128127bddSYan, Zheng u64 len, inline_version; 167228127bddSYan, Zheng int err = 0; 167328127bddSYan, Zheng bool from_pagecache = false; 167428127bddSYan, Zheng 167528127bddSYan, Zheng spin_lock(&ci->i_ceph_lock); 167628127bddSYan, Zheng inline_version = ci->i_inline_version; 167728127bddSYan, Zheng spin_unlock(&ci->i_ceph_lock); 167828127bddSYan, Zheng 167928127bddSYan, Zheng dout("uninline_data %p %llx.%llx inline_version %llu\n", 168028127bddSYan, Zheng inode, ceph_vinop(inode), inline_version); 168128127bddSYan, Zheng 168228127bddSYan, Zheng if (inline_version == 1 || /* initial version, no data */ 168328127bddSYan, Zheng inline_version == CEPH_INLINE_NONE) 168428127bddSYan, Zheng goto out; 168528127bddSYan, Zheng 168628127bddSYan, Zheng if (locked_page) { 168728127bddSYan, Zheng page = locked_page; 168828127bddSYan, Zheng WARN_ON(!PageUptodate(page)); 168928127bddSYan, Zheng } else if (ceph_caps_issued(ci) & 169028127bddSYan, Zheng (CEPH_CAP_FILE_CACHE|CEPH_CAP_FILE_LAZYIO)) { 169128127bddSYan, Zheng page = find_get_page(inode->i_mapping, 0); 169228127bddSYan, Zheng if (page) { 169328127bddSYan, Zheng if (PageUptodate(page)) { 169428127bddSYan, Zheng from_pagecache = true; 169528127bddSYan, Zheng lock_page(page); 169628127bddSYan, Zheng } else { 169709cbfeafSKirill A. Shutemov put_page(page); 169828127bddSYan, Zheng page = NULL; 169928127bddSYan, Zheng } 170028127bddSYan, Zheng } 170128127bddSYan, Zheng } 170228127bddSYan, Zheng 170328127bddSYan, Zheng if (page) { 170428127bddSYan, Zheng len = i_size_read(inode); 170509cbfeafSKirill A. Shutemov if (len > PAGE_SIZE) 170609cbfeafSKirill A. Shutemov len = PAGE_SIZE; 170728127bddSYan, Zheng } else { 170828127bddSYan, Zheng page = __page_cache_alloc(GFP_NOFS); 170928127bddSYan, Zheng if (!page) { 171028127bddSYan, Zheng err = -ENOMEM; 171128127bddSYan, Zheng goto out; 171228127bddSYan, Zheng } 171328127bddSYan, Zheng err = __ceph_do_getattr(inode, page, 171428127bddSYan, Zheng CEPH_STAT_CAP_INLINE_DATA, true); 171528127bddSYan, Zheng if (err < 0) { 171628127bddSYan, Zheng /* no inline data */ 171728127bddSYan, Zheng if (err == -ENODATA) 171828127bddSYan, Zheng err = 0; 171928127bddSYan, Zheng goto out; 172028127bddSYan, Zheng } 172128127bddSYan, Zheng len = err; 172228127bddSYan, Zheng } 172328127bddSYan, Zheng 172428127bddSYan, Zheng req = ceph_osdc_new_request(&fsc->client->osdc, &ci->i_layout, 172528127bddSYan, Zheng ceph_vino(inode), 0, &len, 0, 1, 172654ea0046SIlya Dryomov CEPH_OSD_OP_CREATE, CEPH_OSD_FLAG_WRITE, 172734b759b4SIlya Dryomov NULL, 0, 0, false); 172828127bddSYan, Zheng if (IS_ERR(req)) { 172928127bddSYan, Zheng err = PTR_ERR(req); 173028127bddSYan, Zheng goto out; 173128127bddSYan, Zheng } 173228127bddSYan, Zheng 1733fac02ddfSArnd Bergmann req->r_mtime = inode->i_mtime; 173428127bddSYan, Zheng err = ceph_osdc_start_request(&fsc->client->osdc, req, false); 173528127bddSYan, Zheng if (!err) 173628127bddSYan, Zheng err = ceph_osdc_wait_request(&fsc->client->osdc, req); 173728127bddSYan, Zheng ceph_osdc_put_request(req); 173828127bddSYan, Zheng if (err < 0) 173928127bddSYan, Zheng goto out; 174028127bddSYan, Zheng 174128127bddSYan, Zheng req = ceph_osdc_new_request(&fsc->client->osdc, &ci->i_layout, 174228127bddSYan, Zheng ceph_vino(inode), 0, &len, 1, 3, 174354ea0046SIlya Dryomov CEPH_OSD_OP_WRITE, CEPH_OSD_FLAG_WRITE, 174434b759b4SIlya Dryomov NULL, ci->i_truncate_seq, 174534b759b4SIlya Dryomov ci->i_truncate_size, false); 174628127bddSYan, Zheng if (IS_ERR(req)) { 174728127bddSYan, Zheng err = PTR_ERR(req); 174828127bddSYan, Zheng goto out; 174928127bddSYan, Zheng } 175028127bddSYan, Zheng 175128127bddSYan, Zheng osd_req_op_extent_osd_data_pages(req, 1, &page, len, 0, false, false); 175228127bddSYan, Zheng 1753ec137c10SYan, Zheng { 1754ec137c10SYan, Zheng __le64 xattr_buf = cpu_to_le64(inline_version); 175528127bddSYan, Zheng err = osd_req_op_xattr_init(req, 0, CEPH_OSD_OP_CMPXATTR, 1756ec137c10SYan, Zheng "inline_version", &xattr_buf, 1757ec137c10SYan, Zheng sizeof(xattr_buf), 175828127bddSYan, Zheng CEPH_OSD_CMPXATTR_OP_GT, 175928127bddSYan, Zheng CEPH_OSD_CMPXATTR_MODE_U64); 176028127bddSYan, Zheng if (err) 176128127bddSYan, Zheng goto out_put; 1762ec137c10SYan, Zheng } 176328127bddSYan, Zheng 1764ec137c10SYan, Zheng { 1765ec137c10SYan, Zheng char xattr_buf[32]; 1766ec137c10SYan, Zheng int xattr_len = snprintf(xattr_buf, sizeof(xattr_buf), 1767ec137c10SYan, Zheng "%llu", inline_version); 176828127bddSYan, Zheng err = osd_req_op_xattr_init(req, 2, CEPH_OSD_OP_SETXATTR, 1769ec137c10SYan, Zheng "inline_version", 1770ec137c10SYan, Zheng xattr_buf, xattr_len, 0, 0); 177128127bddSYan, Zheng if (err) 177228127bddSYan, Zheng goto out_put; 1773ec137c10SYan, Zheng } 177428127bddSYan, Zheng 1775fac02ddfSArnd Bergmann req->r_mtime = inode->i_mtime; 177628127bddSYan, Zheng err = ceph_osdc_start_request(&fsc->client->osdc, req, false); 177728127bddSYan, Zheng if (!err) 177828127bddSYan, Zheng err = ceph_osdc_wait_request(&fsc->client->osdc, req); 177928127bddSYan, Zheng out_put: 178028127bddSYan, Zheng ceph_osdc_put_request(req); 178128127bddSYan, Zheng if (err == -ECANCELED) 178228127bddSYan, Zheng err = 0; 178328127bddSYan, Zheng out: 178428127bddSYan, Zheng if (page && page != locked_page) { 178528127bddSYan, Zheng if (from_pagecache) { 178628127bddSYan, Zheng unlock_page(page); 178709cbfeafSKirill A. Shutemov put_page(page); 178828127bddSYan, Zheng } else 178928127bddSYan, Zheng __free_pages(page, 0); 179028127bddSYan, Zheng } 179128127bddSYan, Zheng 179228127bddSYan, Zheng dout("uninline_data %p %llx.%llx inline_version %llu = %d\n", 179328127bddSYan, Zheng inode, ceph_vinop(inode), inline_version, err); 179428127bddSYan, Zheng return err; 179528127bddSYan, Zheng } 179628127bddSYan, Zheng 17977cbea8dcSKirill A. Shutemov static const struct vm_operations_struct ceph_vmops = { 179861f68816SYan, Zheng .fault = ceph_filemap_fault, 17991d3576fdSSage Weil .page_mkwrite = ceph_page_mkwrite, 18001d3576fdSSage Weil }; 18011d3576fdSSage Weil 18021d3576fdSSage Weil int ceph_mmap(struct file *file, struct vm_area_struct *vma) 18031d3576fdSSage Weil { 18041d3576fdSSage Weil struct address_space *mapping = file->f_mapping; 18051d3576fdSSage Weil 18061d3576fdSSage Weil if (!mapping->a_ops->readpage) 18071d3576fdSSage Weil return -ENOEXEC; 18081d3576fdSSage Weil file_accessed(file); 18091d3576fdSSage Weil vma->vm_ops = &ceph_vmops; 18101d3576fdSSage Weil return 0; 18111d3576fdSSage Weil } 181210183a69SYan, Zheng 181310183a69SYan, Zheng enum { 181410183a69SYan, Zheng POOL_READ = 1, 181510183a69SYan, Zheng POOL_WRITE = 2, 181610183a69SYan, Zheng }; 181710183a69SYan, Zheng 1818779fe0fbSYan, Zheng static int __ceph_pool_perm_get(struct ceph_inode_info *ci, 1819779fe0fbSYan, Zheng s64 pool, struct ceph_string *pool_ns) 182010183a69SYan, Zheng { 182110183a69SYan, Zheng struct ceph_fs_client *fsc = ceph_inode_to_client(&ci->vfs_inode); 182210183a69SYan, Zheng struct ceph_mds_client *mdsc = fsc->mdsc; 182310183a69SYan, Zheng struct ceph_osd_request *rd_req = NULL, *wr_req = NULL; 182410183a69SYan, Zheng struct rb_node **p, *parent; 182510183a69SYan, Zheng struct ceph_pool_perm *perm; 182610183a69SYan, Zheng struct page **pages; 1827779fe0fbSYan, Zheng size_t pool_ns_len; 182810183a69SYan, Zheng int err = 0, err2 = 0, have = 0; 182910183a69SYan, Zheng 183010183a69SYan, Zheng down_read(&mdsc->pool_perm_rwsem); 183110183a69SYan, Zheng p = &mdsc->pool_perm_tree.rb_node; 183210183a69SYan, Zheng while (*p) { 183310183a69SYan, Zheng perm = rb_entry(*p, struct ceph_pool_perm, node); 183410183a69SYan, Zheng if (pool < perm->pool) 183510183a69SYan, Zheng p = &(*p)->rb_left; 183610183a69SYan, Zheng else if (pool > perm->pool) 183710183a69SYan, Zheng p = &(*p)->rb_right; 183810183a69SYan, Zheng else { 1839779fe0fbSYan, Zheng int ret = ceph_compare_string(pool_ns, 1840779fe0fbSYan, Zheng perm->pool_ns, 1841779fe0fbSYan, Zheng perm->pool_ns_len); 1842779fe0fbSYan, Zheng if (ret < 0) 1843779fe0fbSYan, Zheng p = &(*p)->rb_left; 1844779fe0fbSYan, Zheng else if (ret > 0) 1845779fe0fbSYan, Zheng p = &(*p)->rb_right; 1846779fe0fbSYan, Zheng else { 184710183a69SYan, Zheng have = perm->perm; 184810183a69SYan, Zheng break; 184910183a69SYan, Zheng } 185010183a69SYan, Zheng } 1851779fe0fbSYan, Zheng } 185210183a69SYan, Zheng up_read(&mdsc->pool_perm_rwsem); 185310183a69SYan, Zheng if (*p) 185410183a69SYan, Zheng goto out; 185510183a69SYan, Zheng 1856779fe0fbSYan, Zheng if (pool_ns) 1857779fe0fbSYan, Zheng dout("__ceph_pool_perm_get pool %lld ns %.*s no perm cached\n", 1858779fe0fbSYan, Zheng pool, (int)pool_ns->len, pool_ns->str); 1859779fe0fbSYan, Zheng else 18607627151eSYan, Zheng dout("__ceph_pool_perm_get pool %lld no perm cached\n", pool); 186110183a69SYan, Zheng 186210183a69SYan, Zheng down_write(&mdsc->pool_perm_rwsem); 1863779fe0fbSYan, Zheng p = &mdsc->pool_perm_tree.rb_node; 186410183a69SYan, Zheng parent = NULL; 186510183a69SYan, Zheng while (*p) { 186610183a69SYan, Zheng parent = *p; 186710183a69SYan, Zheng perm = rb_entry(parent, struct ceph_pool_perm, node); 186810183a69SYan, Zheng if (pool < perm->pool) 186910183a69SYan, Zheng p = &(*p)->rb_left; 187010183a69SYan, Zheng else if (pool > perm->pool) 187110183a69SYan, Zheng p = &(*p)->rb_right; 187210183a69SYan, Zheng else { 1873779fe0fbSYan, Zheng int ret = ceph_compare_string(pool_ns, 1874779fe0fbSYan, Zheng perm->pool_ns, 1875779fe0fbSYan, Zheng perm->pool_ns_len); 1876779fe0fbSYan, Zheng if (ret < 0) 1877779fe0fbSYan, Zheng p = &(*p)->rb_left; 1878779fe0fbSYan, Zheng else if (ret > 0) 1879779fe0fbSYan, Zheng p = &(*p)->rb_right; 1880779fe0fbSYan, Zheng else { 188110183a69SYan, Zheng have = perm->perm; 188210183a69SYan, Zheng break; 188310183a69SYan, Zheng } 188410183a69SYan, Zheng } 1885779fe0fbSYan, Zheng } 188610183a69SYan, Zheng if (*p) { 188710183a69SYan, Zheng up_write(&mdsc->pool_perm_rwsem); 188810183a69SYan, Zheng goto out; 188910183a69SYan, Zheng } 189010183a69SYan, Zheng 189134b759b4SIlya Dryomov rd_req = ceph_osdc_alloc_request(&fsc->client->osdc, NULL, 189210183a69SYan, Zheng 1, false, GFP_NOFS); 189310183a69SYan, Zheng if (!rd_req) { 189410183a69SYan, Zheng err = -ENOMEM; 189510183a69SYan, Zheng goto out_unlock; 189610183a69SYan, Zheng } 189710183a69SYan, Zheng 189810183a69SYan, Zheng rd_req->r_flags = CEPH_OSD_FLAG_READ; 189910183a69SYan, Zheng osd_req_op_init(rd_req, 0, CEPH_OSD_OP_STAT, 0); 190010183a69SYan, Zheng rd_req->r_base_oloc.pool = pool; 1901779fe0fbSYan, Zheng if (pool_ns) 1902779fe0fbSYan, Zheng rd_req->r_base_oloc.pool_ns = ceph_get_string(pool_ns); 1903d30291b9SIlya Dryomov ceph_oid_printf(&rd_req->r_base_oid, "%llx.00000000", ci->i_vino.ino); 190410183a69SYan, Zheng 190513d1ad16SIlya Dryomov err = ceph_osdc_alloc_messages(rd_req, GFP_NOFS); 190613d1ad16SIlya Dryomov if (err) 190713d1ad16SIlya Dryomov goto out_unlock; 190810183a69SYan, Zheng 190934b759b4SIlya Dryomov wr_req = ceph_osdc_alloc_request(&fsc->client->osdc, NULL, 191010183a69SYan, Zheng 1, false, GFP_NOFS); 191110183a69SYan, Zheng if (!wr_req) { 191210183a69SYan, Zheng err = -ENOMEM; 191310183a69SYan, Zheng goto out_unlock; 191410183a69SYan, Zheng } 191510183a69SYan, Zheng 191654ea0046SIlya Dryomov wr_req->r_flags = CEPH_OSD_FLAG_WRITE; 191710183a69SYan, Zheng osd_req_op_init(wr_req, 0, CEPH_OSD_OP_CREATE, CEPH_OSD_OP_FLAG_EXCL); 191863244fa1SIlya Dryomov ceph_oloc_copy(&wr_req->r_base_oloc, &rd_req->r_base_oloc); 1919d30291b9SIlya Dryomov ceph_oid_copy(&wr_req->r_base_oid, &rd_req->r_base_oid); 192010183a69SYan, Zheng 192113d1ad16SIlya Dryomov err = ceph_osdc_alloc_messages(wr_req, GFP_NOFS); 192213d1ad16SIlya Dryomov if (err) 192313d1ad16SIlya Dryomov goto out_unlock; 192410183a69SYan, Zheng 192510183a69SYan, Zheng /* one page should be large enough for STAT data */ 192610183a69SYan, Zheng pages = ceph_alloc_page_vector(1, GFP_KERNEL); 192710183a69SYan, Zheng if (IS_ERR(pages)) { 192810183a69SYan, Zheng err = PTR_ERR(pages); 192910183a69SYan, Zheng goto out_unlock; 193010183a69SYan, Zheng } 193110183a69SYan, Zheng 193210183a69SYan, Zheng osd_req_op_raw_data_in_pages(rd_req, 0, pages, PAGE_SIZE, 193310183a69SYan, Zheng 0, false, true); 193410183a69SYan, Zheng err = ceph_osdc_start_request(&fsc->client->osdc, rd_req, false); 193510183a69SYan, Zheng 1936fac02ddfSArnd Bergmann wr_req->r_mtime = ci->vfs_inode.i_mtime; 193710183a69SYan, Zheng err2 = ceph_osdc_start_request(&fsc->client->osdc, wr_req, false); 193810183a69SYan, Zheng 193910183a69SYan, Zheng if (!err) 194010183a69SYan, Zheng err = ceph_osdc_wait_request(&fsc->client->osdc, rd_req); 194110183a69SYan, Zheng if (!err2) 194210183a69SYan, Zheng err2 = ceph_osdc_wait_request(&fsc->client->osdc, wr_req); 194310183a69SYan, Zheng 194410183a69SYan, Zheng if (err >= 0 || err == -ENOENT) 194510183a69SYan, Zheng have |= POOL_READ; 194610183a69SYan, Zheng else if (err != -EPERM) 194710183a69SYan, Zheng goto out_unlock; 194810183a69SYan, Zheng 194910183a69SYan, Zheng if (err2 == 0 || err2 == -EEXIST) 195010183a69SYan, Zheng have |= POOL_WRITE; 195110183a69SYan, Zheng else if (err2 != -EPERM) { 195210183a69SYan, Zheng err = err2; 195310183a69SYan, Zheng goto out_unlock; 195410183a69SYan, Zheng } 195510183a69SYan, Zheng 1956779fe0fbSYan, Zheng pool_ns_len = pool_ns ? pool_ns->len : 0; 1957779fe0fbSYan, Zheng perm = kmalloc(sizeof(*perm) + pool_ns_len + 1, GFP_NOFS); 195810183a69SYan, Zheng if (!perm) { 195910183a69SYan, Zheng err = -ENOMEM; 196010183a69SYan, Zheng goto out_unlock; 196110183a69SYan, Zheng } 196210183a69SYan, Zheng 196310183a69SYan, Zheng perm->pool = pool; 196410183a69SYan, Zheng perm->perm = have; 1965779fe0fbSYan, Zheng perm->pool_ns_len = pool_ns_len; 1966779fe0fbSYan, Zheng if (pool_ns_len > 0) 1967779fe0fbSYan, Zheng memcpy(perm->pool_ns, pool_ns->str, pool_ns_len); 1968779fe0fbSYan, Zheng perm->pool_ns[pool_ns_len] = 0; 1969779fe0fbSYan, Zheng 197010183a69SYan, Zheng rb_link_node(&perm->node, parent, p); 197110183a69SYan, Zheng rb_insert_color(&perm->node, &mdsc->pool_perm_tree); 197210183a69SYan, Zheng err = 0; 197310183a69SYan, Zheng out_unlock: 197410183a69SYan, Zheng up_write(&mdsc->pool_perm_rwsem); 197510183a69SYan, Zheng 197610183a69SYan, Zheng ceph_osdc_put_request(rd_req); 197710183a69SYan, Zheng ceph_osdc_put_request(wr_req); 197810183a69SYan, Zheng out: 197910183a69SYan, Zheng if (!err) 198010183a69SYan, Zheng err = have; 1981779fe0fbSYan, Zheng if (pool_ns) 1982779fe0fbSYan, Zheng dout("__ceph_pool_perm_get pool %lld ns %.*s result = %d\n", 1983779fe0fbSYan, Zheng pool, (int)pool_ns->len, pool_ns->str, err); 1984779fe0fbSYan, Zheng else 19857627151eSYan, Zheng dout("__ceph_pool_perm_get pool %lld result = %d\n", pool, err); 198610183a69SYan, Zheng return err; 198710183a69SYan, Zheng } 198810183a69SYan, Zheng 198910183a69SYan, Zheng int ceph_pool_perm_check(struct ceph_inode_info *ci, int need) 199010183a69SYan, Zheng { 19917627151eSYan, Zheng s64 pool; 1992779fe0fbSYan, Zheng struct ceph_string *pool_ns; 199310183a69SYan, Zheng int ret, flags; 199410183a69SYan, Zheng 199580e80fbbSYan, Zheng if (ci->i_vino.snap != CEPH_NOSNAP) { 199680e80fbbSYan, Zheng /* 199780e80fbbSYan, Zheng * Pool permission check needs to write to the first object. 199880e80fbbSYan, Zheng * But for snapshot, head of the first object may have alread 199980e80fbbSYan, Zheng * been deleted. Skip check to avoid creating orphan object. 200080e80fbbSYan, Zheng */ 200180e80fbbSYan, Zheng return 0; 200280e80fbbSYan, Zheng } 200380e80fbbSYan, Zheng 200410183a69SYan, Zheng if (ceph_test_mount_opt(ceph_inode_to_client(&ci->vfs_inode), 200510183a69SYan, Zheng NOPOOLPERM)) 200610183a69SYan, Zheng return 0; 200710183a69SYan, Zheng 200810183a69SYan, Zheng spin_lock(&ci->i_ceph_lock); 200910183a69SYan, Zheng flags = ci->i_ceph_flags; 20107627151eSYan, Zheng pool = ci->i_layout.pool_id; 201110183a69SYan, Zheng spin_unlock(&ci->i_ceph_lock); 201210183a69SYan, Zheng check: 201310183a69SYan, Zheng if (flags & CEPH_I_POOL_PERM) { 201410183a69SYan, Zheng if ((need & CEPH_CAP_FILE_RD) && !(flags & CEPH_I_POOL_RD)) { 20157627151eSYan, Zheng dout("ceph_pool_perm_check pool %lld no read perm\n", 201610183a69SYan, Zheng pool); 201710183a69SYan, Zheng return -EPERM; 201810183a69SYan, Zheng } 201910183a69SYan, Zheng if ((need & CEPH_CAP_FILE_WR) && !(flags & CEPH_I_POOL_WR)) { 20207627151eSYan, Zheng dout("ceph_pool_perm_check pool %lld no write perm\n", 202110183a69SYan, Zheng pool); 202210183a69SYan, Zheng return -EPERM; 202310183a69SYan, Zheng } 202410183a69SYan, Zheng return 0; 202510183a69SYan, Zheng } 202610183a69SYan, Zheng 2027779fe0fbSYan, Zheng pool_ns = ceph_try_get_string(ci->i_layout.pool_ns); 2028779fe0fbSYan, Zheng ret = __ceph_pool_perm_get(ci, pool, pool_ns); 2029779fe0fbSYan, Zheng ceph_put_string(pool_ns); 203010183a69SYan, Zheng if (ret < 0) 203110183a69SYan, Zheng return ret; 203210183a69SYan, Zheng 203310183a69SYan, Zheng flags = CEPH_I_POOL_PERM; 203410183a69SYan, Zheng if (ret & POOL_READ) 203510183a69SYan, Zheng flags |= CEPH_I_POOL_RD; 203610183a69SYan, Zheng if (ret & POOL_WRITE) 203710183a69SYan, Zheng flags |= CEPH_I_POOL_WR; 203810183a69SYan, Zheng 203910183a69SYan, Zheng spin_lock(&ci->i_ceph_lock); 2040779fe0fbSYan, Zheng if (pool == ci->i_layout.pool_id && 2041779fe0fbSYan, Zheng pool_ns == rcu_dereference_raw(ci->i_layout.pool_ns)) { 2042779fe0fbSYan, Zheng ci->i_ceph_flags |= flags; 204310183a69SYan, Zheng } else { 20447627151eSYan, Zheng pool = ci->i_layout.pool_id; 204510183a69SYan, Zheng flags = ci->i_ceph_flags; 204610183a69SYan, Zheng } 204710183a69SYan, Zheng spin_unlock(&ci->i_ceph_lock); 204810183a69SYan, Zheng goto check; 204910183a69SYan, Zheng } 205010183a69SYan, Zheng 205110183a69SYan, Zheng void ceph_pool_perm_destroy(struct ceph_mds_client *mdsc) 205210183a69SYan, Zheng { 205310183a69SYan, Zheng struct ceph_pool_perm *perm; 205410183a69SYan, Zheng struct rb_node *n; 205510183a69SYan, Zheng 205610183a69SYan, Zheng while (!RB_EMPTY_ROOT(&mdsc->pool_perm_tree)) { 205710183a69SYan, Zheng n = rb_first(&mdsc->pool_perm_tree); 205810183a69SYan, Zheng perm = rb_entry(n, struct ceph_pool_perm, node); 205910183a69SYan, Zheng rb_erase(n, &mdsc->pool_perm_tree); 206010183a69SYan, Zheng kfree(perm); 206110183a69SYan, Zheng } 206210183a69SYan, Zheng } 2063