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); 3097c272194SSage Weil struct page *page = list_entry(page_list->prev, struct page, lru); 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; 3252b1ac852SYan, Zheng ret = ceph_try_get_caps(ci, CEPH_CAP_FILE_RD, want, &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)) { 3362b1ac852SYan, Zheng page = list_entry(page_list->prev, 3372b1ac852SYan, Zheng struct page, lru); 3382b1ac852SYan, Zheng list_del(&page->lru); 3392b1ac852SYan, Zheng put_page(page); 3402b1ac852SYan, Zheng } 3412b1ac852SYan, Zheng return ret; 3422b1ac852SYan, Zheng } 3432b1ac852SYan, Zheng } 3447c272194SSage Weil 3456285bc23SAlex Elder off = (u64) page_offset(page); 3467c272194SSage Weil 3477c272194SSage Weil /* count pages */ 3487c272194SSage Weil next_index = page->index; 3497c272194SSage Weil list_for_each_entry_reverse(page, page_list, lru) { 3507c272194SSage Weil if (page->index != next_index) 3517c272194SSage Weil break; 3527c272194SSage Weil nr_pages++; 3537c272194SSage Weil next_index++; 3540d66a487SSage Weil if (max && nr_pages == max) 3550d66a487SSage Weil break; 3567c272194SSage Weil } 35709cbfeafSKirill A. Shutemov len = nr_pages << PAGE_SHIFT; 3587c272194SSage Weil dout("start_read %p nr_pages %d is %lld~%lld\n", inode, nr_pages, 3597c272194SSage Weil off, len); 360acead002SAlex Elder vino = ceph_vino(inode); 361acead002SAlex Elder req = ceph_osdc_new_request(osdc, &ci->i_layout, vino, off, &len, 362715e4cd4SYan, Zheng 0, 1, CEPH_OSD_OP_READ, 363acead002SAlex Elder CEPH_OSD_FLAG_READ, NULL, 3647c272194SSage Weil ci->i_truncate_seq, ci->i_truncate_size, 365acead002SAlex Elder false); 3662b1ac852SYan, Zheng if (IS_ERR(req)) { 3672b1ac852SYan, Zheng ret = PTR_ERR(req); 3682b1ac852SYan, Zheng goto out; 3692b1ac852SYan, Zheng } 3701d3576fdSSage Weil 3711d3576fdSSage Weil /* build page vector */ 372cf7b7e14SAlex Elder nr_pages = calc_pages_for(0, len); 373687265e5SYan, Zheng pages = kmalloc(sizeof(*pages) * nr_pages, GFP_KERNEL); 3742b1ac852SYan, Zheng if (!pages) { 3757c272194SSage Weil ret = -ENOMEM; 3762b1ac852SYan, Zheng goto out_put; 3772b1ac852SYan, Zheng } 3787c272194SSage Weil for (i = 0; i < nr_pages; ++i) { 3797c272194SSage Weil page = list_entry(page_list->prev, struct page, lru); 3807c272194SSage Weil BUG_ON(PageLocked(page)); 3817c272194SSage Weil list_del(&page->lru); 3821d3576fdSSage Weil 3837c272194SSage Weil dout("start_read %p adding %p idx %lu\n", inode, page, 3847c272194SSage Weil page->index); 3857c272194SSage Weil if (add_to_page_cache_lru(page, &inode->i_data, page->index, 386687265e5SYan, Zheng GFP_KERNEL)) { 387d4d3aa38SMilosz Tanski ceph_fscache_uncache_page(inode, page); 38809cbfeafSKirill A. Shutemov put_page(page); 3897c272194SSage Weil dout("start_read %p add_to_page_cache failed %p\n", 3907c272194SSage Weil inode, page); 3917c272194SSage Weil nr_pages = i; 3921afe4785SYan, Zheng if (nr_pages > 0) { 3931afe4785SYan, Zheng len = nr_pages << PAGE_SHIFT; 394d641df81SYan, Zheng osd_req_op_extent_update(req, 0, len); 3951afe4785SYan, Zheng break; 3961afe4785SYan, Zheng } 3977c272194SSage Weil goto out_pages; 3981d3576fdSSage Weil } 3997c272194SSage Weil pages[i] = page; 4001d3576fdSSage Weil } 401406e2c9fSAlex Elder osd_req_op_extent_osd_data_pages(req, 0, pages, len, 0, false, false); 4027c272194SSage Weil req->r_callback = finish_read; 4037c272194SSage Weil req->r_inode = inode; 4047c272194SSage Weil 4057c272194SSage Weil dout("start_read %p starting %p %lld~%lld\n", inode, req, off, len); 4067c272194SSage Weil ret = ceph_osdc_start_request(osdc, req, false); 4077c272194SSage Weil if (ret < 0) 4087c272194SSage Weil goto out_pages; 4097c272194SSage Weil ceph_osdc_put_request(req); 4102b1ac852SYan, Zheng 4112b1ac852SYan, Zheng /* After adding locked pages to page cache, the inode holds cache cap. 4122b1ac852SYan, Zheng * So we can drop our cap refs. */ 4132b1ac852SYan, Zheng if (got) 4142b1ac852SYan, Zheng ceph_put_cap_refs(ci, got); 4152b1ac852SYan, Zheng 4167c272194SSage Weil return nr_pages; 4177c272194SSage Weil 4187c272194SSage Weil out_pages: 4191afe4785SYan, Zheng for (i = 0; i < nr_pages; ++i) { 4201afe4785SYan, Zheng ceph_fscache_readpage_cancel(inode, pages[i]); 4211afe4785SYan, Zheng unlock_page(pages[i]); 4221afe4785SYan, Zheng } 4231afe4785SYan, Zheng ceph_put_page_vector(pages, nr_pages, false); 4242b1ac852SYan, Zheng out_put: 4257c272194SSage Weil ceph_osdc_put_request(req); 4262b1ac852SYan, Zheng out: 4272b1ac852SYan, Zheng if (got) 4282b1ac852SYan, Zheng ceph_put_cap_refs(ci, got); 4297c272194SSage Weil return ret; 4301d3576fdSSage Weil } 4311d3576fdSSage Weil 4327c272194SSage Weil 4331d3576fdSSage Weil /* 4341d3576fdSSage Weil * Read multiple pages. Leave pages we don't read + unlock in page_list; 4351d3576fdSSage Weil * the caller (VM) cleans them up. 4361d3576fdSSage Weil */ 4371d3576fdSSage Weil static int ceph_readpages(struct file *file, struct address_space *mapping, 4381d3576fdSSage Weil struct list_head *page_list, unsigned nr_pages) 4391d3576fdSSage Weil { 440496ad9aaSAl Viro struct inode *inode = file_inode(file); 4410d66a487SSage Weil struct ceph_fs_client *fsc = ceph_inode_to_client(inode); 44273737682SChengguang Xu struct ceph_file_info *fi = file->private_data; 4435d988308SYan, Zheng struct ceph_rw_context *rw_ctx; 4441d3576fdSSage Weil int rc = 0; 4450d66a487SSage Weil int max = 0; 4461d3576fdSSage Weil 44783701246SYan, Zheng if (ceph_inode(inode)->i_inline_version != CEPH_INLINE_NONE) 44883701246SYan, Zheng return -EINVAL; 44983701246SYan, Zheng 45099ccbd22SMilosz Tanski rc = ceph_readpages_from_fscache(mapping->host, mapping, page_list, 45199ccbd22SMilosz Tanski &nr_pages); 45299ccbd22SMilosz Tanski 45399ccbd22SMilosz Tanski if (rc == 0) 45499ccbd22SMilosz Tanski goto out; 45599ccbd22SMilosz Tanski 45673737682SChengguang Xu rw_ctx = ceph_find_rw_context(fi); 457aa187926SYan, Zheng max = fsc->mount_options->rsize >> PAGE_SHIFT; 4585d988308SYan, Zheng dout("readpages %p file %p ctx %p nr_pages %d max %d\n", 4595d988308SYan, Zheng inode, file, rw_ctx, nr_pages, max); 4607c272194SSage Weil while (!list_empty(page_list)) { 4615d988308SYan, Zheng rc = start_read(inode, rw_ctx, page_list, max); 4621d3576fdSSage Weil if (rc < 0) 4631d3576fdSSage Weil goto out; 4641d3576fdSSage Weil } 4651d3576fdSSage Weil out: 46676be778bSMilosz Tanski ceph_fscache_readpages_cancel(inode, page_list); 46776be778bSMilosz Tanski 4687c272194SSage Weil dout("readpages %p file %p ret %d\n", inode, file, rc); 4691d3576fdSSage Weil return rc; 4701d3576fdSSage Weil } 4711d3576fdSSage Weil 4721f934b00SYan, Zheng struct ceph_writeback_ctl 4731f934b00SYan, Zheng { 4741f934b00SYan, Zheng loff_t i_size; 4751f934b00SYan, Zheng u64 truncate_size; 4761f934b00SYan, Zheng u32 truncate_seq; 4771f934b00SYan, Zheng bool size_stable; 4782a2d927eSYan, Zheng bool head_snapc; 4791f934b00SYan, Zheng }; 4801f934b00SYan, Zheng 4811d3576fdSSage Weil /* 4821d3576fdSSage Weil * Get ref for the oldest snapc for an inode with dirty data... that is, the 4831d3576fdSSage Weil * only snap context we are allowed to write back. 4841d3576fdSSage Weil */ 4851f934b00SYan, Zheng static struct ceph_snap_context * 48605455e11SYan, Zheng get_oldest_context(struct inode *inode, struct ceph_writeback_ctl *ctl, 48705455e11SYan, Zheng struct ceph_snap_context *page_snapc) 4881d3576fdSSage Weil { 4891d3576fdSSage Weil struct ceph_inode_info *ci = ceph_inode(inode); 4901d3576fdSSage Weil struct ceph_snap_context *snapc = NULL; 4911d3576fdSSage Weil struct ceph_cap_snap *capsnap = NULL; 4921d3576fdSSage Weil 493be655596SSage Weil spin_lock(&ci->i_ceph_lock); 4941d3576fdSSage Weil list_for_each_entry(capsnap, &ci->i_cap_snaps, ci_item) { 4951d3576fdSSage Weil dout(" cap_snap %p snapc %p has %d dirty pages\n", capsnap, 4961d3576fdSSage Weil capsnap->context, capsnap->dirty_pages); 49705455e11SYan, Zheng if (!capsnap->dirty_pages) 49805455e11SYan, Zheng continue; 49905455e11SYan, Zheng 50005455e11SYan, Zheng /* get i_size, truncate_{seq,size} for page_snapc? */ 50105455e11SYan, Zheng if (snapc && capsnap->context != page_snapc) 50205455e11SYan, Zheng continue; 50305455e11SYan, Zheng 5041f934b00SYan, Zheng if (ctl) { 5051f934b00SYan, Zheng if (capsnap->writing) { 5061f934b00SYan, Zheng ctl->i_size = i_size_read(inode); 5071f934b00SYan, Zheng ctl->size_stable = false; 5081f934b00SYan, Zheng } else { 5091f934b00SYan, Zheng ctl->i_size = capsnap->size; 5101f934b00SYan, Zheng ctl->size_stable = true; 5111f934b00SYan, Zheng } 5121f934b00SYan, Zheng ctl->truncate_size = capsnap->truncate_size; 5131f934b00SYan, Zheng ctl->truncate_seq = capsnap->truncate_seq; 5142a2d927eSYan, Zheng ctl->head_snapc = false; 5151f934b00SYan, Zheng } 51605455e11SYan, Zheng 51705455e11SYan, Zheng if (snapc) 5181d3576fdSSage Weil break; 51905455e11SYan, Zheng 52005455e11SYan, Zheng snapc = ceph_get_snap_context(capsnap->context); 52105455e11SYan, Zheng if (!page_snapc || 52205455e11SYan, Zheng page_snapc == snapc || 52305455e11SYan, Zheng page_snapc->seq > snapc->seq) 52405455e11SYan, Zheng break; 5251d3576fdSSage Weil } 5267d8cb26dSSage Weil if (!snapc && ci->i_wrbuffer_ref_head) { 52780e755feSSage Weil snapc = ceph_get_snap_context(ci->i_head_snapc); 5281d3576fdSSage Weil dout(" head snapc %p has %d dirty pages\n", 5291d3576fdSSage Weil snapc, ci->i_wrbuffer_ref_head); 5301f934b00SYan, Zheng if (ctl) { 5311f934b00SYan, Zheng ctl->i_size = i_size_read(inode); 5321f934b00SYan, Zheng ctl->truncate_size = ci->i_truncate_size; 5331f934b00SYan, Zheng ctl->truncate_seq = ci->i_truncate_seq; 5341f934b00SYan, Zheng ctl->size_stable = false; 5352a2d927eSYan, Zheng ctl->head_snapc = true; 5361f934b00SYan, Zheng } 5371d3576fdSSage Weil } 538be655596SSage Weil spin_unlock(&ci->i_ceph_lock); 5391d3576fdSSage Weil return snapc; 5401d3576fdSSage Weil } 5411d3576fdSSage Weil 5421f934b00SYan, Zheng static u64 get_writepages_data_length(struct inode *inode, 5431f934b00SYan, Zheng struct page *page, u64 start) 5441f934b00SYan, Zheng { 5451f934b00SYan, Zheng struct ceph_inode_info *ci = ceph_inode(inode); 5461f934b00SYan, Zheng struct ceph_snap_context *snapc = page_snap_context(page); 5471f934b00SYan, Zheng struct ceph_cap_snap *capsnap = NULL; 5481f934b00SYan, Zheng u64 end = i_size_read(inode); 5491f934b00SYan, Zheng 5501f934b00SYan, Zheng if (snapc != ci->i_head_snapc) { 5511f934b00SYan, Zheng bool found = false; 5521f934b00SYan, Zheng spin_lock(&ci->i_ceph_lock); 5531f934b00SYan, Zheng list_for_each_entry(capsnap, &ci->i_cap_snaps, ci_item) { 5541f934b00SYan, Zheng if (capsnap->context == snapc) { 5551f934b00SYan, Zheng if (!capsnap->writing) 5561f934b00SYan, Zheng end = capsnap->size; 5571f934b00SYan, Zheng found = true; 5581f934b00SYan, Zheng break; 5591f934b00SYan, Zheng } 5601f934b00SYan, Zheng } 5611f934b00SYan, Zheng spin_unlock(&ci->i_ceph_lock); 5621f934b00SYan, Zheng WARN_ON(!found); 5631f934b00SYan, Zheng } 5641f934b00SYan, Zheng if (end > page_offset(page) + PAGE_SIZE) 5651f934b00SYan, Zheng end = page_offset(page) + PAGE_SIZE; 5661f934b00SYan, Zheng return end > start ? end - start : 0; 5671f934b00SYan, Zheng } 5681f934b00SYan, Zheng 5691d3576fdSSage Weil /* 5701d3576fdSSage Weil * Write a single page, but leave the page locked. 5711d3576fdSSage Weil * 5721d3576fdSSage Weil * If we get a write error, set the page error bit, but still adjust the 5731d3576fdSSage Weil * dirty page accounting (i.e., page is no longer dirty). 5741d3576fdSSage Weil */ 5751d3576fdSSage Weil static int writepage_nounlock(struct page *page, struct writeback_control *wbc) 5761d3576fdSSage Weil { 5771d3576fdSSage Weil struct inode *inode; 5781d3576fdSSage Weil struct ceph_inode_info *ci; 5793d14c5d2SYehuda Sadeh struct ceph_fs_client *fsc; 5806298a337SSage Weil struct ceph_snap_context *snapc, *oldest; 581fc2744aaSYan, Zheng loff_t page_off = page_offset(page); 58243986881SYan, Zheng int err, len = PAGE_SIZE; 5831f934b00SYan, Zheng struct ceph_writeback_ctl ceph_wbc; 5841d3576fdSSage Weil 5851d3576fdSSage Weil dout("writepage %p idx %lu\n", page, page->index); 5861d3576fdSSage Weil 5871d3576fdSSage Weil inode = page->mapping->host; 5881d3576fdSSage Weil ci = ceph_inode(inode); 5893d14c5d2SYehuda Sadeh fsc = ceph_inode_to_client(inode); 5901d3576fdSSage Weil 5911d3576fdSSage Weil /* verify this is a writeable snap context */ 59261600ef8SYan, Zheng snapc = page_snap_context(page); 593d37b1d99SMarkus Elfring if (!snapc) { 5941d3576fdSSage Weil dout("writepage %p page %p not dirty?\n", inode, page); 59543986881SYan, Zheng return 0; 5961d3576fdSSage Weil } 59705455e11SYan, Zheng oldest = get_oldest_context(inode, &ceph_wbc, snapc); 5986298a337SSage Weil if (snapc->seq > oldest->seq) { 5991d3576fdSSage Weil dout("writepage %p page %p snapc %p not writeable - noop\n", 60061600ef8SYan, Zheng inode, page, snapc); 6011d3576fdSSage Weil /* we should only noop if called by kswapd */ 602fa71fefbSYan, Zheng WARN_ON(!(current->flags & PF_MEMALLOC)); 6036298a337SSage Weil ceph_put_snap_context(oldest); 604fa71fefbSYan, Zheng redirty_page_for_writepage(wbc, page); 60543986881SYan, Zheng return 0; 6061d3576fdSSage Weil } 6076298a337SSage Weil ceph_put_snap_context(oldest); 6081d3576fdSSage Weil 6091d3576fdSSage Weil /* is this a partial page at end of file? */ 6101f934b00SYan, Zheng if (page_off >= ceph_wbc.i_size) { 6111f934b00SYan, Zheng dout("%p page eof %llu\n", page, ceph_wbc.i_size); 61205455e11SYan, Zheng page->mapping->a_ops->invalidatepage(page, 0, PAGE_SIZE); 61343986881SYan, Zheng return 0; 614fc2744aaSYan, Zheng } 61543986881SYan, Zheng 6161f934b00SYan, Zheng if (ceph_wbc.i_size < page_off + len) 6171f934b00SYan, Zheng len = ceph_wbc.i_size - page_off; 6181d3576fdSSage Weil 6191c0a9c2dSYan, Zheng dout("writepage %p page %p index %lu on %llu~%u snapc %p seq %lld\n", 6201c0a9c2dSYan, Zheng inode, page, page->index, page_off, len, snapc, snapc->seq); 6211d3576fdSSage Weil 622314c4737SYan, Zheng if (atomic_long_inc_return(&fsc->writeback_count) > 6233d14c5d2SYehuda Sadeh CONGESTION_ON_THRESH(fsc->mount_options->congestion_kb)) 62409dc9fc2SJan Kara set_bdi_congested(inode_to_bdi(inode), BLK_RW_ASYNC); 6252baba250SYehuda Sadeh 6261d3576fdSSage Weil set_page_writeback(page); 6271f934b00SYan, Zheng err = ceph_osdc_writepages(&fsc->client->osdc, ceph_vino(inode), 6281f934b00SYan, Zheng &ci->i_layout, snapc, page_off, len, 6291f934b00SYan, Zheng ceph_wbc.truncate_seq, 6301f934b00SYan, Zheng ceph_wbc.truncate_size, 63124808826SAlex Elder &inode->i_mtime, &page, 1); 6321d3576fdSSage Weil if (err < 0) { 633ad15ec06SYan, Zheng struct writeback_control tmp_wbc; 634ad15ec06SYan, Zheng if (!wbc) 635ad15ec06SYan, Zheng wbc = &tmp_wbc; 636ad15ec06SYan, Zheng if (err == -ERESTARTSYS) { 637ad15ec06SYan, Zheng /* killed by SIGKILL */ 638ad15ec06SYan, Zheng dout("writepage interrupted page %p\n", page); 639ad15ec06SYan, Zheng redirty_page_for_writepage(wbc, page); 640ad15ec06SYan, Zheng end_page_writeback(page); 64143986881SYan, Zheng return err; 642ad15ec06SYan, Zheng } 643ad15ec06SYan, Zheng dout("writepage setting page/mapping error %d %p\n", 644ad15ec06SYan, Zheng err, page); 6451d3576fdSSage Weil SetPageError(page); 6461d3576fdSSage Weil mapping_set_error(&inode->i_data, err); 6471d3576fdSSage Weil wbc->pages_skipped++; 6481d3576fdSSage Weil } else { 6491d3576fdSSage Weil dout("writepage cleaned page %p\n", page); 6501d3576fdSSage Weil err = 0; /* vfs expects us to return 0 */ 6511d3576fdSSage Weil } 6521d3576fdSSage Weil page->private = 0; 6531d3576fdSSage Weil ClearPagePrivate(page); 6541d3576fdSSage Weil end_page_writeback(page); 6551d3576fdSSage Weil ceph_put_wrbuffer_cap_refs(ci, 1, snapc); 6566298a337SSage Weil ceph_put_snap_context(snapc); /* page's reference */ 657314c4737SYan, Zheng 658314c4737SYan, Zheng if (atomic_long_dec_return(&fsc->writeback_count) < 659314c4737SYan, Zheng CONGESTION_OFF_THRESH(fsc->mount_options->congestion_kb)) 660314c4737SYan, Zheng clear_bdi_congested(inode_to_bdi(inode), BLK_RW_ASYNC); 661314c4737SYan, Zheng 6621d3576fdSSage Weil return err; 6631d3576fdSSage Weil } 6641d3576fdSSage Weil 6651d3576fdSSage Weil static int ceph_writepage(struct page *page, struct writeback_control *wbc) 6661d3576fdSSage Weil { 667dbd646a8SYehuda Sadeh int err; 668dbd646a8SYehuda Sadeh struct inode *inode = page->mapping->host; 669dbd646a8SYehuda Sadeh BUG_ON(!inode); 67070b666c3SSage Weil ihold(inode); 671dbd646a8SYehuda Sadeh err = writepage_nounlock(page, wbc); 672ad15ec06SYan, Zheng if (err == -ERESTARTSYS) { 673ad15ec06SYan, Zheng /* direct memory reclaimer was killed by SIGKILL. return 0 674ad15ec06SYan, Zheng * to prevent caller from setting mapping/page error */ 675ad15ec06SYan, Zheng err = 0; 676ad15ec06SYan, Zheng } 6771d3576fdSSage Weil unlock_page(page); 678dbd646a8SYehuda Sadeh iput(inode); 6791d3576fdSSage Weil return err; 6801d3576fdSSage Weil } 6811d3576fdSSage Weil 6821d3576fdSSage Weil /* 6831d3576fdSSage Weil * lame release_pages helper. release_pages() isn't exported to 6841d3576fdSSage Weil * modules. 6851d3576fdSSage Weil */ 6861d3576fdSSage Weil static void ceph_release_pages(struct page **pages, int num) 6871d3576fdSSage Weil { 6881d3576fdSSage Weil struct pagevec pvec; 6891d3576fdSSage Weil int i; 6901d3576fdSSage Weil 69186679820SMel Gorman pagevec_init(&pvec); 6921d3576fdSSage Weil for (i = 0; i < num; i++) { 6931d3576fdSSage Weil if (pagevec_add(&pvec, pages[i]) == 0) 6941d3576fdSSage Weil pagevec_release(&pvec); 6951d3576fdSSage Weil } 6961d3576fdSSage Weil pagevec_release(&pvec); 6971d3576fdSSage Weil } 6981d3576fdSSage Weil 6991d3576fdSSage Weil /* 7001d3576fdSSage Weil * async writeback completion handler. 7011d3576fdSSage Weil * 7021d3576fdSSage Weil * If we get an error, set the mapping error bit, but not the individual 7031d3576fdSSage Weil * page error bits. 7041d3576fdSSage Weil */ 70585e084feSIlya Dryomov static void writepages_finish(struct ceph_osd_request *req) 7061d3576fdSSage Weil { 7071d3576fdSSage Weil struct inode *inode = req->r_inode; 7081d3576fdSSage Weil struct ceph_inode_info *ci = ceph_inode(inode); 70987060c10SAlex Elder struct ceph_osd_data *osd_data; 7101d3576fdSSage Weil struct page *page; 7115b64640cSYan, Zheng int num_pages, total_pages = 0; 7125b64640cSYan, Zheng int i, j; 7135b64640cSYan, Zheng int rc = req->r_result; 7141d3576fdSSage Weil struct ceph_snap_context *snapc = req->r_snapc; 7151d3576fdSSage Weil struct address_space *mapping = inode->i_mapping; 7163d14c5d2SYehuda Sadeh struct ceph_fs_client *fsc = ceph_inode_to_client(inode); 7175b64640cSYan, Zheng bool remove_page; 7181d3576fdSSage Weil 7195b64640cSYan, Zheng dout("writepages_finish %p rc %d\n", inode, rc); 72026544c62SJeff Layton if (rc < 0) { 7211d3576fdSSage Weil mapping_set_error(mapping, rc); 72226544c62SJeff Layton ceph_set_error_write(ci); 72326544c62SJeff Layton } else { 72426544c62SJeff Layton ceph_clear_error_write(ci); 72526544c62SJeff Layton } 726e63dc5c7SYehuda Sadeh 727e63dc5c7SYehuda Sadeh /* 728e63dc5c7SYehuda Sadeh * We lost the cache cap, need to truncate the page before 729e63dc5c7SYehuda Sadeh * it is unlocked, otherwise we'd truncate it later in the 730e63dc5c7SYehuda Sadeh * page truncation thread, possibly losing some data that 731e63dc5c7SYehuda Sadeh * raced its way in 732e63dc5c7SYehuda Sadeh */ 7335b64640cSYan, Zheng remove_page = !(ceph_caps_issued(ci) & 7345b64640cSYan, Zheng (CEPH_CAP_FILE_CACHE|CEPH_CAP_FILE_LAZYIO)); 7355b64640cSYan, Zheng 7365b64640cSYan, Zheng /* clean all pages */ 7375b64640cSYan, Zheng for (i = 0; i < req->r_num_ops; i++) { 7385b64640cSYan, Zheng if (req->r_ops[i].op != CEPH_OSD_OP_WRITE) 7395b64640cSYan, Zheng break; 7405b64640cSYan, Zheng 7415b64640cSYan, Zheng osd_data = osd_req_op_extent_osd_data(req, i); 7425b64640cSYan, Zheng BUG_ON(osd_data->type != CEPH_OSD_DATA_TYPE_PAGES); 7435b64640cSYan, Zheng num_pages = calc_pages_for((u64)osd_data->alignment, 7445b64640cSYan, Zheng (u64)osd_data->length); 7455b64640cSYan, Zheng total_pages += num_pages; 7465b64640cSYan, Zheng for (j = 0; j < num_pages; j++) { 7475b64640cSYan, Zheng page = osd_data->pages[j]; 7485b64640cSYan, Zheng BUG_ON(!page); 7495b64640cSYan, Zheng WARN_ON(!PageUptodate(page)); 7505b64640cSYan, Zheng 7515b64640cSYan, Zheng if (atomic_long_dec_return(&fsc->writeback_count) < 7525b64640cSYan, Zheng CONGESTION_OFF_THRESH( 7535b64640cSYan, Zheng fsc->mount_options->congestion_kb)) 75409dc9fc2SJan Kara clear_bdi_congested(inode_to_bdi(inode), 7555b64640cSYan, Zheng BLK_RW_ASYNC); 7565b64640cSYan, Zheng 7575b64640cSYan, Zheng ceph_put_snap_context(page_snap_context(page)); 7585b64640cSYan, Zheng page->private = 0; 7595b64640cSYan, Zheng ClearPagePrivate(page); 7605b64640cSYan, Zheng dout("unlocking %p\n", page); 7615b64640cSYan, Zheng end_page_writeback(page); 7625b64640cSYan, Zheng 7635b64640cSYan, Zheng if (remove_page) 7645b64640cSYan, Zheng generic_error_remove_page(inode->i_mapping, 7655b64640cSYan, Zheng page); 766e63dc5c7SYehuda Sadeh 7671d3576fdSSage Weil unlock_page(page); 7681d3576fdSSage Weil } 7695b64640cSYan, Zheng dout("writepages_finish %p wrote %llu bytes cleaned %d pages\n", 7705b64640cSYan, Zheng inode, osd_data->length, rc >= 0 ? num_pages : 0); 7711d3576fdSSage Weil 77287060c10SAlex Elder ceph_release_pages(osd_data->pages, num_pages); 7735b64640cSYan, Zheng } 7745b64640cSYan, Zheng 7755b64640cSYan, Zheng ceph_put_wrbuffer_cap_refs(ci, total_pages, snapc); 7765b64640cSYan, Zheng 7775b64640cSYan, Zheng osd_data = osd_req_op_extent_osd_data(req, 0); 77887060c10SAlex Elder if (osd_data->pages_from_pool) 77987060c10SAlex Elder mempool_free(osd_data->pages, 780640ef79dSCheng Renquan ceph_sb_to_client(inode->i_sb)->wb_pagevec_pool); 7811d3576fdSSage Weil else 78287060c10SAlex Elder kfree(osd_data->pages); 7831d3576fdSSage Weil ceph_osdc_put_request(req); 7841d3576fdSSage Weil } 7851d3576fdSSage Weil 7861d3576fdSSage Weil /* 7871d3576fdSSage Weil * initiate async writeback 7881d3576fdSSage Weil */ 7891d3576fdSSage Weil static int ceph_writepages_start(struct address_space *mapping, 7901d3576fdSSage Weil struct writeback_control *wbc) 7911d3576fdSSage Weil { 7921d3576fdSSage Weil struct inode *inode = mapping->host; 7931d3576fdSSage Weil struct ceph_inode_info *ci = ceph_inode(inode); 794fc2744aaSYan, Zheng struct ceph_fs_client *fsc = ceph_inode_to_client(inode); 795fc2744aaSYan, Zheng struct ceph_vino vino = ceph_vino(inode); 7962a2d927eSYan, Zheng pgoff_t index, start_index, end = -1; 79780e755feSSage Weil struct ceph_snap_context *snapc = NULL, *last_snapc = NULL, *pgsnapc; 7981d3576fdSSage Weil struct pagevec pvec; 7991d3576fdSSage Weil int rc = 0; 80093407472SFabian Frederick unsigned int wsize = i_blocksize(inode); 8011d3576fdSSage Weil struct ceph_osd_request *req = NULL; 8021f934b00SYan, Zheng struct ceph_writeback_ctl ceph_wbc; 803590e9d98SYan, Zheng bool should_loop, range_whole = false; 804af9cc401SYan, Zheng bool done = false; 8051d3576fdSSage Weil 8063fb99d48SYanhu Cao dout("writepages_start %p (mode=%s)\n", inode, 8071d3576fdSSage Weil wbc->sync_mode == WB_SYNC_NONE ? "NONE" : 8081d3576fdSSage Weil (wbc->sync_mode == WB_SYNC_ALL ? "ALL" : "HOLD")); 8091d3576fdSSage Weil 81052953d55SSeraphime Kirkovski if (READ_ONCE(fsc->mount_state) == CEPH_MOUNT_SHUTDOWN) { 8116c93df5dSYan, Zheng if (ci->i_wrbuffer_ref > 0) { 8126c93df5dSYan, Zheng pr_warn_ratelimited( 8136c93df5dSYan, Zheng "writepage_start %p %lld forced umount\n", 8146c93df5dSYan, Zheng inode, ceph_ino(inode)); 8156c93df5dSYan, Zheng } 816a341d4dfSYan, Zheng mapping_set_error(mapping, -EIO); 8171d3576fdSSage Weil return -EIO; /* we're in a forced umount, don't write! */ 8181d3576fdSSage Weil } 81995cca2b4SYan, Zheng if (fsc->mount_options->wsize < wsize) 8203d14c5d2SYehuda Sadeh wsize = fsc->mount_options->wsize; 8211d3576fdSSage Weil 82286679820SMel Gorman pagevec_init(&pvec); 8231d3576fdSSage Weil 824590e9d98SYan, Zheng start_index = wbc->range_cyclic ? mapping->writeback_index : 0; 825590e9d98SYan, Zheng index = start_index; 8261d3576fdSSage Weil 8271d3576fdSSage Weil retry: 8281d3576fdSSage Weil /* find oldest snap context with dirty data */ 82905455e11SYan, Zheng snapc = get_oldest_context(inode, &ceph_wbc, NULL); 8301d3576fdSSage Weil if (!snapc) { 8311d3576fdSSage Weil /* hmm, why does writepages get called when there 8321d3576fdSSage Weil is no dirty data? */ 8331d3576fdSSage Weil dout(" no snap context with dirty data?\n"); 8341d3576fdSSage Weil goto out; 8351d3576fdSSage Weil } 8361d3576fdSSage Weil dout(" oldest snapc is %p seq %lld (%d snaps)\n", 8371d3576fdSSage Weil snapc, snapc->seq, snapc->num_snaps); 838fc2744aaSYan, Zheng 8392a2d927eSYan, Zheng should_loop = false; 8402a2d927eSYan, Zheng if (ceph_wbc.head_snapc && snapc != last_snapc) { 8412a2d927eSYan, Zheng /* where to start/end? */ 8422a2d927eSYan, Zheng if (wbc->range_cyclic) { 8432a2d927eSYan, Zheng index = start_index; 8442a2d927eSYan, Zheng end = -1; 8452a2d927eSYan, Zheng if (index > 0) 8462a2d927eSYan, Zheng should_loop = true; 8472a2d927eSYan, Zheng dout(" cyclic, start at %lu\n", index); 8482a2d927eSYan, Zheng } else { 8492a2d927eSYan, Zheng index = wbc->range_start >> PAGE_SHIFT; 8502a2d927eSYan, Zheng end = wbc->range_end >> PAGE_SHIFT; 8512a2d927eSYan, Zheng if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX) 8522a2d927eSYan, Zheng range_whole = true; 8532a2d927eSYan, Zheng dout(" not cyclic, %lu to %lu\n", index, end); 8541d3576fdSSage Weil } 8552a2d927eSYan, Zheng } else if (!ceph_wbc.head_snapc) { 8562a2d927eSYan, Zheng /* Do not respect wbc->range_{start,end}. Dirty pages 8572a2d927eSYan, Zheng * in that range can be associated with newer snapc. 8582a2d927eSYan, Zheng * They are not writeable until we write all dirty pages 8592a2d927eSYan, Zheng * associated with 'snapc' get written */ 8601582af2eSYan, Zheng if (index > 0) 8612a2d927eSYan, Zheng should_loop = true; 8622a2d927eSYan, Zheng dout(" non-head snapc, range whole\n"); 8632a2d927eSYan, Zheng } 8642a2d927eSYan, Zheng 8652a2d927eSYan, Zheng ceph_put_snap_context(last_snapc); 8661d3576fdSSage Weil last_snapc = snapc; 8671d3576fdSSage Weil 868af9cc401SYan, Zheng while (!done && index <= end) { 8695b64640cSYan, Zheng int num_ops = 0, op_idx; 8700e5ecac7SYan, Zheng unsigned i, pvec_pages, max_pages, locked_pages = 0; 8715b64640cSYan, Zheng struct page **pages = NULL, **data_pages; 872e5975c7cSAlex Elder mempool_t *pool = NULL; /* Becomes non-null if mempool used */ 8731d3576fdSSage Weil struct page *page; 8740e5ecac7SYan, Zheng pgoff_t strip_unit_end = 0; 8755b64640cSYan, Zheng u64 offset = 0, len = 0; 8761d3576fdSSage Weil 8770e5ecac7SYan, Zheng max_pages = wsize >> PAGE_SHIFT; 8781d3576fdSSage Weil 8791d3576fdSSage Weil get_more_pages: 8804be90299SJan Kara pvec_pages = pagevec_lookup_range_nr_tag(&pvec, mapping, &index, 8810ed75fc8SJan Kara end, PAGECACHE_TAG_DIRTY, 8824be90299SJan Kara max_pages - locked_pages); 8830ed75fc8SJan Kara dout("pagevec_lookup_range_tag got %d\n", pvec_pages); 8841d3576fdSSage Weil if (!pvec_pages && !locked_pages) 8851d3576fdSSage Weil break; 8861d3576fdSSage Weil for (i = 0; i < pvec_pages && locked_pages < max_pages; i++) { 8871d3576fdSSage Weil page = pvec.pages[i]; 8881d3576fdSSage Weil dout("? %p idx %lu\n", page, page->index); 8891d3576fdSSage Weil if (locked_pages == 0) 8901d3576fdSSage Weil lock_page(page); /* first page */ 8911d3576fdSSage Weil else if (!trylock_page(page)) 8921d3576fdSSage Weil break; 8931d3576fdSSage Weil 8941d3576fdSSage Weil /* only dirty pages, or our accounting breaks */ 8951d3576fdSSage Weil if (unlikely(!PageDirty(page)) || 8961d3576fdSSage Weil unlikely(page->mapping != mapping)) { 8971d3576fdSSage Weil dout("!dirty or !mapping %p\n", page); 8981d3576fdSSage Weil unlock_page(page); 8990713e5f2SYan, Zheng continue; 9001d3576fdSSage Weil } 901af9cc401SYan, Zheng /* only if matching snap context */ 902af9cc401SYan, Zheng pgsnapc = page_snap_context(page); 903af9cc401SYan, Zheng if (pgsnapc != snapc) { 904af9cc401SYan, Zheng dout("page snapc %p %lld != oldest %p %lld\n", 905af9cc401SYan, Zheng pgsnapc, pgsnapc->seq, snapc, snapc->seq); 9061582af2eSYan, Zheng if (!should_loop && 9071582af2eSYan, Zheng !ceph_wbc.head_snapc && 9081582af2eSYan, Zheng wbc->sync_mode != WB_SYNC_NONE) 9091582af2eSYan, Zheng should_loop = true; 9101d3576fdSSage Weil unlock_page(page); 911af9cc401SYan, Zheng continue; 9121d3576fdSSage Weil } 9131f934b00SYan, Zheng if (page_offset(page) >= ceph_wbc.i_size) { 9141f934b00SYan, Zheng dout("%p page eof %llu\n", 9151f934b00SYan, Zheng page, ceph_wbc.i_size); 916af9cc401SYan, Zheng if (ceph_wbc.size_stable || 917af9cc401SYan, Zheng page_offset(page) >= i_size_read(inode)) 918af9cc401SYan, Zheng mapping->a_ops->invalidatepage(page, 919af9cc401SYan, Zheng 0, PAGE_SIZE); 920af9cc401SYan, Zheng unlock_page(page); 921af9cc401SYan, Zheng continue; 922af9cc401SYan, Zheng } 923af9cc401SYan, Zheng if (strip_unit_end && (page->index > strip_unit_end)) { 924af9cc401SYan, Zheng dout("end of strip unit %p\n", page); 9251d3576fdSSage Weil unlock_page(page); 9261d3576fdSSage Weil break; 9271d3576fdSSage Weil } 9281d3576fdSSage Weil if (PageWriteback(page)) { 9290713e5f2SYan, Zheng if (wbc->sync_mode == WB_SYNC_NONE) { 9301d3576fdSSage Weil dout("%p under writeback\n", page); 9311d3576fdSSage Weil unlock_page(page); 9320713e5f2SYan, Zheng continue; 9330713e5f2SYan, Zheng } 9340713e5f2SYan, Zheng dout("waiting on writeback %p\n", page); 9350713e5f2SYan, Zheng wait_on_page_writeback(page); 9361d3576fdSSage Weil } 9371d3576fdSSage Weil 9381d3576fdSSage Weil if (!clear_page_dirty_for_io(page)) { 9391d3576fdSSage Weil dout("%p !clear_page_dirty_for_io\n", page); 9401d3576fdSSage Weil unlock_page(page); 9410713e5f2SYan, Zheng continue; 9421d3576fdSSage Weil } 9431d3576fdSSage Weil 944e5975c7cSAlex Elder /* 945e5975c7cSAlex Elder * We have something to write. If this is 946e5975c7cSAlex Elder * the first locked page this time through, 9475b64640cSYan, Zheng * calculate max possinle write size and 9485b64640cSYan, Zheng * allocate a page array 949e5975c7cSAlex Elder */ 9501d3576fdSSage Weil if (locked_pages == 0) { 9515b64640cSYan, Zheng u64 objnum; 9525b64640cSYan, Zheng u64 objoff; 953dccbf080SIlya Dryomov u32 xlen; 9545b64640cSYan, Zheng 9551d3576fdSSage Weil /* prepare async write request */ 9566285bc23SAlex Elder offset = (u64)page_offset(page); 957dccbf080SIlya Dryomov ceph_calc_file_object_mapping(&ci->i_layout, 958dccbf080SIlya Dryomov offset, wsize, 9595b64640cSYan, Zheng &objnum, &objoff, 960dccbf080SIlya Dryomov &xlen); 961dccbf080SIlya Dryomov len = xlen; 9628c71897bSHenry C Chang 9633fb99d48SYanhu Cao num_ops = 1; 9645b64640cSYan, Zheng strip_unit_end = page->index + 96509cbfeafSKirill A. Shutemov ((len - 1) >> PAGE_SHIFT); 966715e4cd4SYan, Zheng 9675b64640cSYan, Zheng BUG_ON(pages); 96888486957SAlex Elder max_pages = calc_pages_for(0, (u64)len); 969fc2744aaSYan, Zheng pages = kmalloc(max_pages * 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; 11165b64640cSYan, Zheng pages = kmalloc(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 1134bb873b53SIlya Dryomov 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 */ 143111bac800SDave Jiang static int 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; 143961f68816SYan, Zheng int want, got, ret; 14404f7e89f6SYan, Zheng sigset_t oldset; 14414f7e89f6SYan, Zheng 14424f7e89f6SYan, Zheng ceph_block_sigs(&oldset); 144361f68816SYan, Zheng 144461f68816SYan, Zheng dout("filemap_fault %p %llx.%llx %llu~%zd trying to get caps\n", 144509cbfeafSKirill A. Shutemov inode, ceph_vinop(inode), off, (size_t)PAGE_SIZE); 144661f68816SYan, Zheng if (fi->fmode & CEPH_FILE_MODE_LAZY) 144761f68816SYan, Zheng want = CEPH_CAP_FILE_CACHE | CEPH_CAP_FILE_LAZYIO; 144861f68816SYan, Zheng else 144961f68816SYan, Zheng want = CEPH_CAP_FILE_CACHE; 14504f7e89f6SYan, Zheng 145161f68816SYan, Zheng got = 0; 14524f7e89f6SYan, Zheng ret = ceph_get_caps(ci, CEPH_CAP_FILE_RD, want, -1, &got, &pinned_page); 14536ce026e4SYan, Zheng if (ret < 0) 14544f7e89f6SYan, Zheng goto out_restore; 14556ce026e4SYan, Zheng 145661f68816SYan, Zheng dout("filemap_fault %p %llu~%zd got cap refs on %s\n", 145709cbfeafSKirill A. Shutemov inode, off, (size_t)PAGE_SIZE, ceph_cap_string(got)); 145861f68816SYan, Zheng 145983701246SYan, Zheng if ((got & (CEPH_CAP_FILE_CACHE | CEPH_CAP_FILE_LAZYIO)) || 14602b1ac852SYan, Zheng ci->i_inline_version == CEPH_INLINE_NONE) { 14615d988308SYan, Zheng CEPH_DEFINE_RW_CONTEXT(rw_ctx, got); 14625d988308SYan, Zheng ceph_add_rw_context(fi, &rw_ctx); 146311bac800SDave Jiang ret = filemap_fault(vmf); 14645d988308SYan, Zheng ceph_del_rw_context(fi, &rw_ctx); 14652b1ac852SYan, Zheng } else 146683701246SYan, Zheng ret = -EAGAIN; 146761f68816SYan, Zheng 146861f68816SYan, Zheng dout("filemap_fault %p %llu~%zd dropping cap refs on %s ret %d\n", 146909cbfeafSKirill A. Shutemov inode, off, (size_t)PAGE_SIZE, ceph_cap_string(got), ret); 14703738daa6SYan, Zheng if (pinned_page) 147109cbfeafSKirill A. Shutemov put_page(pinned_page); 147261f68816SYan, Zheng ceph_put_cap_refs(ci, got); 147361f68816SYan, Zheng 147483701246SYan, Zheng if (ret != -EAGAIN) 14754f7e89f6SYan, Zheng goto out_restore; 147683701246SYan, Zheng 147783701246SYan, Zheng /* read inline data */ 147809cbfeafSKirill A. Shutemov if (off >= PAGE_SIZE) { 147983701246SYan, Zheng /* does not support inline data > PAGE_SIZE */ 148083701246SYan, Zheng ret = VM_FAULT_SIGBUS; 148183701246SYan, Zheng } else { 148283701246SYan, Zheng int ret1; 148383701246SYan, Zheng struct address_space *mapping = inode->i_mapping; 148483701246SYan, Zheng struct page *page = find_or_create_page(mapping, 0, 1485c62d2555SMichal Hocko mapping_gfp_constraint(mapping, 1486c62d2555SMichal Hocko ~__GFP_FS)); 148783701246SYan, Zheng if (!page) { 148883701246SYan, Zheng ret = VM_FAULT_OOM; 14894f7e89f6SYan, Zheng goto out_inline; 149083701246SYan, Zheng } 149183701246SYan, Zheng ret1 = __ceph_do_getattr(inode, page, 149283701246SYan, Zheng CEPH_STAT_CAP_INLINE_DATA, true); 149383701246SYan, Zheng if (ret1 < 0 || off >= i_size_read(inode)) { 149483701246SYan, Zheng unlock_page(page); 149509cbfeafSKirill A. Shutemov put_page(page); 14966ce026e4SYan, Zheng if (ret1 < 0) 14976ce026e4SYan, Zheng ret = ret1; 14986ce026e4SYan, Zheng else 149983701246SYan, Zheng ret = VM_FAULT_SIGBUS; 15004f7e89f6SYan, Zheng goto out_inline; 150183701246SYan, Zheng } 150209cbfeafSKirill A. Shutemov if (ret1 < PAGE_SIZE) 150309cbfeafSKirill A. Shutemov zero_user_segment(page, ret1, PAGE_SIZE); 150483701246SYan, Zheng else 150583701246SYan, Zheng flush_dcache_page(page); 150683701246SYan, Zheng SetPageUptodate(page); 150783701246SYan, Zheng vmf->page = page; 150883701246SYan, Zheng ret = VM_FAULT_MAJOR | VM_FAULT_LOCKED; 15094f7e89f6SYan, Zheng out_inline: 151083701246SYan, Zheng dout("filemap_fault %p %llu~%zd read inline data ret %d\n", 151109cbfeafSKirill A. Shutemov inode, off, (size_t)PAGE_SIZE, ret); 15124f7e89f6SYan, Zheng } 15134f7e89f6SYan, Zheng out_restore: 15144f7e89f6SYan, Zheng ceph_restore_sigs(&oldset); 15156ce026e4SYan, Zheng if (ret < 0) 15166ce026e4SYan, Zheng ret = (ret == -ENOMEM) ? VM_FAULT_OOM : VM_FAULT_SIGBUS; 15176ce026e4SYan, Zheng 151861f68816SYan, Zheng return ret; 151961f68816SYan, Zheng } 15201d3576fdSSage Weil 15211d3576fdSSage Weil /* 15221d3576fdSSage Weil * Reuse write_begin here for simplicity. 15231d3576fdSSage Weil */ 152411bac800SDave Jiang static int ceph_page_mkwrite(struct vm_fault *vmf) 15251d3576fdSSage Weil { 152611bac800SDave Jiang struct vm_area_struct *vma = vmf->vma; 1527496ad9aaSAl Viro struct inode *inode = file_inode(vma->vm_file); 152861f68816SYan, Zheng struct ceph_inode_info *ci = ceph_inode(inode); 152961f68816SYan, Zheng struct ceph_file_info *fi = vma->vm_file->private_data; 1530f66fd9f0SYan, Zheng struct ceph_cap_flush *prealloc_cf; 153161f68816SYan, Zheng struct page *page = vmf->page; 15326285bc23SAlex Elder loff_t off = page_offset(page); 153361f68816SYan, Zheng loff_t size = i_size_read(inode); 153461f68816SYan, Zheng size_t len; 153561f68816SYan, Zheng int want, got, ret; 15364f7e89f6SYan, Zheng sigset_t oldset; 15371d3576fdSSage Weil 1538f66fd9f0SYan, Zheng prealloc_cf = ceph_alloc_cap_flush(); 1539f66fd9f0SYan, Zheng if (!prealloc_cf) 15406ce026e4SYan, Zheng return VM_FAULT_OOM; 1541f66fd9f0SYan, Zheng 15424f7e89f6SYan, Zheng ceph_block_sigs(&oldset); 15431d3576fdSSage Weil 154428127bddSYan, Zheng if (ci->i_inline_version != CEPH_INLINE_NONE) { 154528127bddSYan, Zheng struct page *locked_page = NULL; 154628127bddSYan, Zheng if (off == 0) { 154728127bddSYan, Zheng lock_page(page); 154828127bddSYan, Zheng locked_page = page; 154928127bddSYan, Zheng } 155028127bddSYan, Zheng ret = ceph_uninline_data(vma->vm_file, locked_page); 155128127bddSYan, Zheng if (locked_page) 155228127bddSYan, Zheng unlock_page(locked_page); 15536ce026e4SYan, Zheng if (ret < 0) 1554f66fd9f0SYan, Zheng goto out_free; 1555f66fd9f0SYan, Zheng } 155628127bddSYan, Zheng 155709cbfeafSKirill A. Shutemov if (off + PAGE_SIZE <= size) 155809cbfeafSKirill A. Shutemov len = PAGE_SIZE; 15591d3576fdSSage Weil else 156009cbfeafSKirill A. Shutemov len = size & ~PAGE_MASK; 15611d3576fdSSage Weil 156261f68816SYan, Zheng dout("page_mkwrite %p %llx.%llx %llu~%zd getting caps i_size %llu\n", 156361f68816SYan, Zheng inode, ceph_vinop(inode), off, len, size); 156461f68816SYan, Zheng if (fi->fmode & CEPH_FILE_MODE_LAZY) 156561f68816SYan, Zheng want = CEPH_CAP_FILE_BUFFER | CEPH_CAP_FILE_LAZYIO; 156661f68816SYan, Zheng else 156761f68816SYan, Zheng want = CEPH_CAP_FILE_BUFFER; 15684f7e89f6SYan, Zheng 156961f68816SYan, Zheng got = 0; 15703738daa6SYan, Zheng ret = ceph_get_caps(ci, CEPH_CAP_FILE_WR, want, off + len, 15713738daa6SYan, Zheng &got, NULL); 15726ce026e4SYan, Zheng if (ret < 0) 1573f66fd9f0SYan, Zheng goto out_free; 15746ce026e4SYan, Zheng 157561f68816SYan, Zheng dout("page_mkwrite %p %llu~%zd got cap refs on %s\n", 157661f68816SYan, Zheng inode, off, len, ceph_cap_string(got)); 157761f68816SYan, Zheng 157861f68816SYan, Zheng /* Update time before taking page lock */ 157961f68816SYan, Zheng file_update_time(vma->vm_file); 15804af6b225SYehuda Sadeh 1581f0b33df5SYan, Zheng do { 15824af6b225SYehuda Sadeh lock_page(page); 15834af6b225SYehuda Sadeh 15846ce026e4SYan, Zheng if ((off > size) || (page->mapping != inode->i_mapping)) { 1585f9cac5acSYan, Zheng unlock_page(page); 15866ce026e4SYan, Zheng ret = VM_FAULT_NOPAGE; 1587f0b33df5SYan, Zheng break; 1588f9cac5acSYan, Zheng } 15894af6b225SYehuda Sadeh 15904af6b225SYehuda Sadeh ret = ceph_update_writeable_page(vma->vm_file, off, len, page); 1591f9cac5acSYan, Zheng if (ret >= 0) { 15924af6b225SYehuda Sadeh /* success. we'll keep the page locked. */ 15931d3576fdSSage Weil set_page_dirty(page); 15941d3576fdSSage Weil ret = VM_FAULT_LOCKED; 15951d3576fdSSage Weil } 1596f0b33df5SYan, Zheng } while (ret == -EAGAIN); 1597f0b33df5SYan, Zheng 159828127bddSYan, Zheng if (ret == VM_FAULT_LOCKED || 159928127bddSYan, Zheng ci->i_inline_version != CEPH_INLINE_NONE) { 160061f68816SYan, Zheng int dirty; 160161f68816SYan, Zheng spin_lock(&ci->i_ceph_lock); 160228127bddSYan, Zheng ci->i_inline_version = CEPH_INLINE_NONE; 1603f66fd9f0SYan, Zheng dirty = __ceph_mark_dirty_caps(ci, CEPH_CAP_FILE_WR, 1604f66fd9f0SYan, Zheng &prealloc_cf); 160561f68816SYan, Zheng spin_unlock(&ci->i_ceph_lock); 160661f68816SYan, Zheng if (dirty) 160761f68816SYan, Zheng __mark_inode_dirty(inode, dirty); 160861f68816SYan, Zheng } 160961f68816SYan, Zheng 161061f68816SYan, Zheng dout("page_mkwrite %p %llu~%zd dropping cap refs on %s ret %d\n", 161161f68816SYan, Zheng inode, off, len, ceph_cap_string(got), ret); 161261f68816SYan, Zheng ceph_put_cap_refs(ci, got); 1613f66fd9f0SYan, Zheng out_free: 16144f7e89f6SYan, Zheng ceph_restore_sigs(&oldset); 1615f66fd9f0SYan, Zheng ceph_free_cap_flush(prealloc_cf); 16166ce026e4SYan, Zheng if (ret < 0) 16176ce026e4SYan, Zheng ret = (ret == -ENOMEM) ? VM_FAULT_OOM : VM_FAULT_SIGBUS; 16181d3576fdSSage Weil return ret; 16191d3576fdSSage Weil } 16201d3576fdSSage Weil 162131c542a1SYan, Zheng void ceph_fill_inline_data(struct inode *inode, struct page *locked_page, 162231c542a1SYan, Zheng char *data, size_t len) 162331c542a1SYan, Zheng { 162431c542a1SYan, Zheng struct address_space *mapping = inode->i_mapping; 162531c542a1SYan, Zheng struct page *page; 162631c542a1SYan, Zheng 162731c542a1SYan, Zheng if (locked_page) { 162831c542a1SYan, Zheng page = locked_page; 162931c542a1SYan, Zheng } else { 163031c542a1SYan, Zheng if (i_size_read(inode) == 0) 163131c542a1SYan, Zheng return; 163231c542a1SYan, Zheng page = find_or_create_page(mapping, 0, 1633c62d2555SMichal Hocko mapping_gfp_constraint(mapping, 1634c62d2555SMichal Hocko ~__GFP_FS)); 163531c542a1SYan, Zheng if (!page) 163631c542a1SYan, Zheng return; 163731c542a1SYan, Zheng if (PageUptodate(page)) { 163831c542a1SYan, Zheng unlock_page(page); 163909cbfeafSKirill A. Shutemov put_page(page); 164031c542a1SYan, Zheng return; 164131c542a1SYan, Zheng } 164231c542a1SYan, Zheng } 164331c542a1SYan, Zheng 16440668ff52SIlya Dryomov dout("fill_inline_data %p %llx.%llx len %zu locked_page %p\n", 164531c542a1SYan, Zheng inode, ceph_vinop(inode), len, locked_page); 164631c542a1SYan, Zheng 164731c542a1SYan, Zheng if (len > 0) { 164831c542a1SYan, Zheng void *kaddr = kmap_atomic(page); 164931c542a1SYan, Zheng memcpy(kaddr, data, len); 165031c542a1SYan, Zheng kunmap_atomic(kaddr); 165131c542a1SYan, Zheng } 165231c542a1SYan, Zheng 165331c542a1SYan, Zheng if (page != locked_page) { 165409cbfeafSKirill A. Shutemov if (len < PAGE_SIZE) 165509cbfeafSKirill A. Shutemov zero_user_segment(page, len, PAGE_SIZE); 165631c542a1SYan, Zheng else 165731c542a1SYan, Zheng flush_dcache_page(page); 165831c542a1SYan, Zheng 165931c542a1SYan, Zheng SetPageUptodate(page); 166031c542a1SYan, Zheng unlock_page(page); 166109cbfeafSKirill A. Shutemov put_page(page); 166231c542a1SYan, Zheng } 166331c542a1SYan, Zheng } 166431c542a1SYan, Zheng 166528127bddSYan, Zheng int ceph_uninline_data(struct file *filp, struct page *locked_page) 166628127bddSYan, Zheng { 166728127bddSYan, Zheng struct inode *inode = file_inode(filp); 166828127bddSYan, Zheng struct ceph_inode_info *ci = ceph_inode(inode); 166928127bddSYan, Zheng struct ceph_fs_client *fsc = ceph_inode_to_client(inode); 167028127bddSYan, Zheng struct ceph_osd_request *req; 167128127bddSYan, Zheng struct page *page = NULL; 167228127bddSYan, Zheng u64 len, inline_version; 167328127bddSYan, Zheng int err = 0; 167428127bddSYan, Zheng bool from_pagecache = false; 167528127bddSYan, Zheng 167628127bddSYan, Zheng spin_lock(&ci->i_ceph_lock); 167728127bddSYan, Zheng inline_version = ci->i_inline_version; 167828127bddSYan, Zheng spin_unlock(&ci->i_ceph_lock); 167928127bddSYan, Zheng 168028127bddSYan, Zheng dout("uninline_data %p %llx.%llx inline_version %llu\n", 168128127bddSYan, Zheng inode, ceph_vinop(inode), inline_version); 168228127bddSYan, Zheng 168328127bddSYan, Zheng if (inline_version == 1 || /* initial version, no data */ 168428127bddSYan, Zheng inline_version == CEPH_INLINE_NONE) 168528127bddSYan, Zheng goto out; 168628127bddSYan, Zheng 168728127bddSYan, Zheng if (locked_page) { 168828127bddSYan, Zheng page = locked_page; 168928127bddSYan, Zheng WARN_ON(!PageUptodate(page)); 169028127bddSYan, Zheng } else if (ceph_caps_issued(ci) & 169128127bddSYan, Zheng (CEPH_CAP_FILE_CACHE|CEPH_CAP_FILE_LAZYIO)) { 169228127bddSYan, Zheng page = find_get_page(inode->i_mapping, 0); 169328127bddSYan, Zheng if (page) { 169428127bddSYan, Zheng if (PageUptodate(page)) { 169528127bddSYan, Zheng from_pagecache = true; 169628127bddSYan, Zheng lock_page(page); 169728127bddSYan, Zheng } else { 169809cbfeafSKirill A. Shutemov put_page(page); 169928127bddSYan, Zheng page = NULL; 170028127bddSYan, Zheng } 170128127bddSYan, Zheng } 170228127bddSYan, Zheng } 170328127bddSYan, Zheng 170428127bddSYan, Zheng if (page) { 170528127bddSYan, Zheng len = i_size_read(inode); 170609cbfeafSKirill A. Shutemov if (len > PAGE_SIZE) 170709cbfeafSKirill A. Shutemov len = PAGE_SIZE; 170828127bddSYan, Zheng } else { 170928127bddSYan, Zheng page = __page_cache_alloc(GFP_NOFS); 171028127bddSYan, Zheng if (!page) { 171128127bddSYan, Zheng err = -ENOMEM; 171228127bddSYan, Zheng goto out; 171328127bddSYan, Zheng } 171428127bddSYan, Zheng err = __ceph_do_getattr(inode, page, 171528127bddSYan, Zheng CEPH_STAT_CAP_INLINE_DATA, true); 171628127bddSYan, Zheng if (err < 0) { 171728127bddSYan, Zheng /* no inline data */ 171828127bddSYan, Zheng if (err == -ENODATA) 171928127bddSYan, Zheng err = 0; 172028127bddSYan, Zheng goto out; 172128127bddSYan, Zheng } 172228127bddSYan, Zheng len = err; 172328127bddSYan, Zheng } 172428127bddSYan, Zheng 172528127bddSYan, Zheng req = ceph_osdc_new_request(&fsc->client->osdc, &ci->i_layout, 172628127bddSYan, Zheng ceph_vino(inode), 0, &len, 0, 1, 172754ea0046SIlya Dryomov CEPH_OSD_OP_CREATE, CEPH_OSD_FLAG_WRITE, 172834b759b4SIlya Dryomov NULL, 0, 0, false); 172928127bddSYan, Zheng if (IS_ERR(req)) { 173028127bddSYan, Zheng err = PTR_ERR(req); 173128127bddSYan, Zheng goto out; 173228127bddSYan, Zheng } 173328127bddSYan, Zheng 1734bb873b53SIlya Dryomov req->r_mtime = inode->i_mtime; 173528127bddSYan, Zheng err = ceph_osdc_start_request(&fsc->client->osdc, req, false); 173628127bddSYan, Zheng if (!err) 173728127bddSYan, Zheng err = ceph_osdc_wait_request(&fsc->client->osdc, req); 173828127bddSYan, Zheng ceph_osdc_put_request(req); 173928127bddSYan, Zheng if (err < 0) 174028127bddSYan, Zheng goto out; 174128127bddSYan, Zheng 174228127bddSYan, Zheng req = ceph_osdc_new_request(&fsc->client->osdc, &ci->i_layout, 174328127bddSYan, Zheng ceph_vino(inode), 0, &len, 1, 3, 174454ea0046SIlya Dryomov CEPH_OSD_OP_WRITE, CEPH_OSD_FLAG_WRITE, 174534b759b4SIlya Dryomov NULL, ci->i_truncate_seq, 174634b759b4SIlya Dryomov ci->i_truncate_size, false); 174728127bddSYan, Zheng if (IS_ERR(req)) { 174828127bddSYan, Zheng err = PTR_ERR(req); 174928127bddSYan, Zheng goto out; 175028127bddSYan, Zheng } 175128127bddSYan, Zheng 175228127bddSYan, Zheng osd_req_op_extent_osd_data_pages(req, 1, &page, len, 0, false, false); 175328127bddSYan, Zheng 1754ec137c10SYan, Zheng { 1755ec137c10SYan, Zheng __le64 xattr_buf = cpu_to_le64(inline_version); 175628127bddSYan, Zheng err = osd_req_op_xattr_init(req, 0, CEPH_OSD_OP_CMPXATTR, 1757ec137c10SYan, Zheng "inline_version", &xattr_buf, 1758ec137c10SYan, Zheng sizeof(xattr_buf), 175928127bddSYan, Zheng CEPH_OSD_CMPXATTR_OP_GT, 176028127bddSYan, Zheng CEPH_OSD_CMPXATTR_MODE_U64); 176128127bddSYan, Zheng if (err) 176228127bddSYan, Zheng goto out_put; 1763ec137c10SYan, Zheng } 176428127bddSYan, Zheng 1765ec137c10SYan, Zheng { 1766ec137c10SYan, Zheng char xattr_buf[32]; 1767ec137c10SYan, Zheng int xattr_len = snprintf(xattr_buf, sizeof(xattr_buf), 1768ec137c10SYan, Zheng "%llu", inline_version); 176928127bddSYan, Zheng err = osd_req_op_xattr_init(req, 2, CEPH_OSD_OP_SETXATTR, 1770ec137c10SYan, Zheng "inline_version", 1771ec137c10SYan, Zheng xattr_buf, xattr_len, 0, 0); 177228127bddSYan, Zheng if (err) 177328127bddSYan, Zheng goto out_put; 1774ec137c10SYan, Zheng } 177528127bddSYan, Zheng 1776bb873b53SIlya Dryomov req->r_mtime = inode->i_mtime; 177728127bddSYan, Zheng err = ceph_osdc_start_request(&fsc->client->osdc, req, false); 177828127bddSYan, Zheng if (!err) 177928127bddSYan, Zheng err = ceph_osdc_wait_request(&fsc->client->osdc, req); 178028127bddSYan, Zheng out_put: 178128127bddSYan, Zheng ceph_osdc_put_request(req); 178228127bddSYan, Zheng if (err == -ECANCELED) 178328127bddSYan, Zheng err = 0; 178428127bddSYan, Zheng out: 178528127bddSYan, Zheng if (page && page != locked_page) { 178628127bddSYan, Zheng if (from_pagecache) { 178728127bddSYan, Zheng unlock_page(page); 178809cbfeafSKirill A. Shutemov put_page(page); 178928127bddSYan, Zheng } else 179028127bddSYan, Zheng __free_pages(page, 0); 179128127bddSYan, Zheng } 179228127bddSYan, Zheng 179328127bddSYan, Zheng dout("uninline_data %p %llx.%llx inline_version %llu = %d\n", 179428127bddSYan, Zheng inode, ceph_vinop(inode), inline_version, err); 179528127bddSYan, Zheng return err; 179628127bddSYan, Zheng } 179728127bddSYan, Zheng 17987cbea8dcSKirill A. Shutemov static const struct vm_operations_struct ceph_vmops = { 179961f68816SYan, Zheng .fault = ceph_filemap_fault, 18001d3576fdSSage Weil .page_mkwrite = ceph_page_mkwrite, 18011d3576fdSSage Weil }; 18021d3576fdSSage Weil 18031d3576fdSSage Weil int ceph_mmap(struct file *file, struct vm_area_struct *vma) 18041d3576fdSSage Weil { 18051d3576fdSSage Weil struct address_space *mapping = file->f_mapping; 18061d3576fdSSage Weil 18071d3576fdSSage Weil if (!mapping->a_ops->readpage) 18081d3576fdSSage Weil return -ENOEXEC; 18091d3576fdSSage Weil file_accessed(file); 18101d3576fdSSage Weil vma->vm_ops = &ceph_vmops; 18111d3576fdSSage Weil return 0; 18121d3576fdSSage Weil } 181310183a69SYan, Zheng 181410183a69SYan, Zheng enum { 181510183a69SYan, Zheng POOL_READ = 1, 181610183a69SYan, Zheng POOL_WRITE = 2, 181710183a69SYan, Zheng }; 181810183a69SYan, Zheng 1819779fe0fbSYan, Zheng static int __ceph_pool_perm_get(struct ceph_inode_info *ci, 1820779fe0fbSYan, Zheng s64 pool, struct ceph_string *pool_ns) 182110183a69SYan, Zheng { 182210183a69SYan, Zheng struct ceph_fs_client *fsc = ceph_inode_to_client(&ci->vfs_inode); 182310183a69SYan, Zheng struct ceph_mds_client *mdsc = fsc->mdsc; 182410183a69SYan, Zheng struct ceph_osd_request *rd_req = NULL, *wr_req = NULL; 182510183a69SYan, Zheng struct rb_node **p, *parent; 182610183a69SYan, Zheng struct ceph_pool_perm *perm; 182710183a69SYan, Zheng struct page **pages; 1828779fe0fbSYan, Zheng size_t pool_ns_len; 182910183a69SYan, Zheng int err = 0, err2 = 0, have = 0; 183010183a69SYan, Zheng 183110183a69SYan, Zheng down_read(&mdsc->pool_perm_rwsem); 183210183a69SYan, Zheng p = &mdsc->pool_perm_tree.rb_node; 183310183a69SYan, Zheng while (*p) { 183410183a69SYan, Zheng perm = rb_entry(*p, struct ceph_pool_perm, node); 183510183a69SYan, Zheng if (pool < perm->pool) 183610183a69SYan, Zheng p = &(*p)->rb_left; 183710183a69SYan, Zheng else if (pool > perm->pool) 183810183a69SYan, Zheng p = &(*p)->rb_right; 183910183a69SYan, Zheng else { 1840779fe0fbSYan, Zheng int ret = ceph_compare_string(pool_ns, 1841779fe0fbSYan, Zheng perm->pool_ns, 1842779fe0fbSYan, Zheng perm->pool_ns_len); 1843779fe0fbSYan, Zheng if (ret < 0) 1844779fe0fbSYan, Zheng p = &(*p)->rb_left; 1845779fe0fbSYan, Zheng else if (ret > 0) 1846779fe0fbSYan, Zheng p = &(*p)->rb_right; 1847779fe0fbSYan, Zheng else { 184810183a69SYan, Zheng have = perm->perm; 184910183a69SYan, Zheng break; 185010183a69SYan, Zheng } 185110183a69SYan, Zheng } 1852779fe0fbSYan, Zheng } 185310183a69SYan, Zheng up_read(&mdsc->pool_perm_rwsem); 185410183a69SYan, Zheng if (*p) 185510183a69SYan, Zheng goto out; 185610183a69SYan, Zheng 1857779fe0fbSYan, Zheng if (pool_ns) 1858779fe0fbSYan, Zheng dout("__ceph_pool_perm_get pool %lld ns %.*s no perm cached\n", 1859779fe0fbSYan, Zheng pool, (int)pool_ns->len, pool_ns->str); 1860779fe0fbSYan, Zheng else 18617627151eSYan, Zheng dout("__ceph_pool_perm_get pool %lld no perm cached\n", pool); 186210183a69SYan, Zheng 186310183a69SYan, Zheng down_write(&mdsc->pool_perm_rwsem); 1864779fe0fbSYan, Zheng p = &mdsc->pool_perm_tree.rb_node; 186510183a69SYan, Zheng parent = NULL; 186610183a69SYan, Zheng while (*p) { 186710183a69SYan, Zheng parent = *p; 186810183a69SYan, Zheng perm = rb_entry(parent, struct ceph_pool_perm, node); 186910183a69SYan, Zheng if (pool < perm->pool) 187010183a69SYan, Zheng p = &(*p)->rb_left; 187110183a69SYan, Zheng else if (pool > perm->pool) 187210183a69SYan, Zheng p = &(*p)->rb_right; 187310183a69SYan, Zheng else { 1874779fe0fbSYan, Zheng int ret = ceph_compare_string(pool_ns, 1875779fe0fbSYan, Zheng perm->pool_ns, 1876779fe0fbSYan, Zheng perm->pool_ns_len); 1877779fe0fbSYan, Zheng if (ret < 0) 1878779fe0fbSYan, Zheng p = &(*p)->rb_left; 1879779fe0fbSYan, Zheng else if (ret > 0) 1880779fe0fbSYan, Zheng p = &(*p)->rb_right; 1881779fe0fbSYan, Zheng else { 188210183a69SYan, Zheng have = perm->perm; 188310183a69SYan, Zheng break; 188410183a69SYan, Zheng } 188510183a69SYan, Zheng } 1886779fe0fbSYan, Zheng } 188710183a69SYan, Zheng if (*p) { 188810183a69SYan, Zheng up_write(&mdsc->pool_perm_rwsem); 188910183a69SYan, Zheng goto out; 189010183a69SYan, Zheng } 189110183a69SYan, Zheng 189234b759b4SIlya Dryomov rd_req = ceph_osdc_alloc_request(&fsc->client->osdc, NULL, 189310183a69SYan, Zheng 1, false, GFP_NOFS); 189410183a69SYan, Zheng if (!rd_req) { 189510183a69SYan, Zheng err = -ENOMEM; 189610183a69SYan, Zheng goto out_unlock; 189710183a69SYan, Zheng } 189810183a69SYan, Zheng 189910183a69SYan, Zheng rd_req->r_flags = CEPH_OSD_FLAG_READ; 190010183a69SYan, Zheng osd_req_op_init(rd_req, 0, CEPH_OSD_OP_STAT, 0); 190110183a69SYan, Zheng rd_req->r_base_oloc.pool = pool; 1902779fe0fbSYan, Zheng if (pool_ns) 1903779fe0fbSYan, Zheng rd_req->r_base_oloc.pool_ns = ceph_get_string(pool_ns); 1904d30291b9SIlya Dryomov ceph_oid_printf(&rd_req->r_base_oid, "%llx.00000000", ci->i_vino.ino); 190510183a69SYan, Zheng 190613d1ad16SIlya Dryomov err = ceph_osdc_alloc_messages(rd_req, GFP_NOFS); 190713d1ad16SIlya Dryomov if (err) 190813d1ad16SIlya Dryomov goto out_unlock; 190910183a69SYan, Zheng 191034b759b4SIlya Dryomov wr_req = ceph_osdc_alloc_request(&fsc->client->osdc, NULL, 191110183a69SYan, Zheng 1, false, GFP_NOFS); 191210183a69SYan, Zheng if (!wr_req) { 191310183a69SYan, Zheng err = -ENOMEM; 191410183a69SYan, Zheng goto out_unlock; 191510183a69SYan, Zheng } 191610183a69SYan, Zheng 191754ea0046SIlya Dryomov wr_req->r_flags = CEPH_OSD_FLAG_WRITE; 191810183a69SYan, Zheng osd_req_op_init(wr_req, 0, CEPH_OSD_OP_CREATE, CEPH_OSD_OP_FLAG_EXCL); 191963244fa1SIlya Dryomov ceph_oloc_copy(&wr_req->r_base_oloc, &rd_req->r_base_oloc); 1920d30291b9SIlya Dryomov ceph_oid_copy(&wr_req->r_base_oid, &rd_req->r_base_oid); 192110183a69SYan, Zheng 192213d1ad16SIlya Dryomov err = ceph_osdc_alloc_messages(wr_req, GFP_NOFS); 192313d1ad16SIlya Dryomov if (err) 192413d1ad16SIlya Dryomov goto out_unlock; 192510183a69SYan, Zheng 192610183a69SYan, Zheng /* one page should be large enough for STAT data */ 192710183a69SYan, Zheng pages = ceph_alloc_page_vector(1, GFP_KERNEL); 192810183a69SYan, Zheng if (IS_ERR(pages)) { 192910183a69SYan, Zheng err = PTR_ERR(pages); 193010183a69SYan, Zheng goto out_unlock; 193110183a69SYan, Zheng } 193210183a69SYan, Zheng 193310183a69SYan, Zheng osd_req_op_raw_data_in_pages(rd_req, 0, pages, PAGE_SIZE, 193410183a69SYan, Zheng 0, false, true); 193510183a69SYan, Zheng err = ceph_osdc_start_request(&fsc->client->osdc, rd_req, false); 193610183a69SYan, Zheng 1937bb873b53SIlya Dryomov wr_req->r_mtime = ci->vfs_inode.i_mtime; 1938a1f4020aSJeff Layton wr_req->r_abort_on_full = true; 193910183a69SYan, Zheng err2 = ceph_osdc_start_request(&fsc->client->osdc, wr_req, false); 194010183a69SYan, Zheng 194110183a69SYan, Zheng if (!err) 194210183a69SYan, Zheng err = ceph_osdc_wait_request(&fsc->client->osdc, rd_req); 194310183a69SYan, Zheng if (!err2) 194410183a69SYan, Zheng err2 = ceph_osdc_wait_request(&fsc->client->osdc, wr_req); 194510183a69SYan, Zheng 194610183a69SYan, Zheng if (err >= 0 || err == -ENOENT) 194710183a69SYan, Zheng have |= POOL_READ; 194810183a69SYan, Zheng else if (err != -EPERM) 194910183a69SYan, Zheng goto out_unlock; 195010183a69SYan, Zheng 195110183a69SYan, Zheng if (err2 == 0 || err2 == -EEXIST) 195210183a69SYan, Zheng have |= POOL_WRITE; 195310183a69SYan, Zheng else if (err2 != -EPERM) { 195410183a69SYan, Zheng err = err2; 195510183a69SYan, Zheng goto out_unlock; 195610183a69SYan, Zheng } 195710183a69SYan, Zheng 1958779fe0fbSYan, Zheng pool_ns_len = pool_ns ? pool_ns->len : 0; 1959779fe0fbSYan, Zheng perm = kmalloc(sizeof(*perm) + pool_ns_len + 1, GFP_NOFS); 196010183a69SYan, Zheng if (!perm) { 196110183a69SYan, Zheng err = -ENOMEM; 196210183a69SYan, Zheng goto out_unlock; 196310183a69SYan, Zheng } 196410183a69SYan, Zheng 196510183a69SYan, Zheng perm->pool = pool; 196610183a69SYan, Zheng perm->perm = have; 1967779fe0fbSYan, Zheng perm->pool_ns_len = pool_ns_len; 1968779fe0fbSYan, Zheng if (pool_ns_len > 0) 1969779fe0fbSYan, Zheng memcpy(perm->pool_ns, pool_ns->str, pool_ns_len); 1970779fe0fbSYan, Zheng perm->pool_ns[pool_ns_len] = 0; 1971779fe0fbSYan, Zheng 197210183a69SYan, Zheng rb_link_node(&perm->node, parent, p); 197310183a69SYan, Zheng rb_insert_color(&perm->node, &mdsc->pool_perm_tree); 197410183a69SYan, Zheng err = 0; 197510183a69SYan, Zheng out_unlock: 197610183a69SYan, Zheng up_write(&mdsc->pool_perm_rwsem); 197710183a69SYan, Zheng 197810183a69SYan, Zheng ceph_osdc_put_request(rd_req); 197910183a69SYan, Zheng ceph_osdc_put_request(wr_req); 198010183a69SYan, Zheng out: 198110183a69SYan, Zheng if (!err) 198210183a69SYan, Zheng err = have; 1983779fe0fbSYan, Zheng if (pool_ns) 1984779fe0fbSYan, Zheng dout("__ceph_pool_perm_get pool %lld ns %.*s result = %d\n", 1985779fe0fbSYan, Zheng pool, (int)pool_ns->len, pool_ns->str, err); 1986779fe0fbSYan, Zheng else 19877627151eSYan, Zheng dout("__ceph_pool_perm_get pool %lld result = %d\n", pool, err); 198810183a69SYan, Zheng return err; 198910183a69SYan, Zheng } 199010183a69SYan, Zheng 199110183a69SYan, Zheng int ceph_pool_perm_check(struct ceph_inode_info *ci, int need) 199210183a69SYan, Zheng { 19937627151eSYan, Zheng s64 pool; 1994779fe0fbSYan, Zheng struct ceph_string *pool_ns; 199510183a69SYan, Zheng int ret, flags; 199610183a69SYan, Zheng 199780e80fbbSYan, Zheng if (ci->i_vino.snap != CEPH_NOSNAP) { 199880e80fbbSYan, Zheng /* 199980e80fbbSYan, Zheng * Pool permission check needs to write to the first object. 200080e80fbbSYan, Zheng * But for snapshot, head of the first object may have alread 200180e80fbbSYan, Zheng * been deleted. Skip check to avoid creating orphan object. 200280e80fbbSYan, Zheng */ 200380e80fbbSYan, Zheng return 0; 200480e80fbbSYan, Zheng } 200580e80fbbSYan, Zheng 200610183a69SYan, Zheng if (ceph_test_mount_opt(ceph_inode_to_client(&ci->vfs_inode), 200710183a69SYan, Zheng NOPOOLPERM)) 200810183a69SYan, Zheng return 0; 200910183a69SYan, Zheng 201010183a69SYan, Zheng spin_lock(&ci->i_ceph_lock); 201110183a69SYan, Zheng flags = ci->i_ceph_flags; 20127627151eSYan, Zheng pool = ci->i_layout.pool_id; 201310183a69SYan, Zheng spin_unlock(&ci->i_ceph_lock); 201410183a69SYan, Zheng check: 201510183a69SYan, Zheng if (flags & CEPH_I_POOL_PERM) { 201610183a69SYan, Zheng if ((need & CEPH_CAP_FILE_RD) && !(flags & CEPH_I_POOL_RD)) { 20177627151eSYan, Zheng dout("ceph_pool_perm_check pool %lld no read perm\n", 201810183a69SYan, Zheng pool); 201910183a69SYan, Zheng return -EPERM; 202010183a69SYan, Zheng } 202110183a69SYan, Zheng if ((need & CEPH_CAP_FILE_WR) && !(flags & CEPH_I_POOL_WR)) { 20227627151eSYan, Zheng dout("ceph_pool_perm_check pool %lld no write perm\n", 202310183a69SYan, Zheng pool); 202410183a69SYan, Zheng return -EPERM; 202510183a69SYan, Zheng } 202610183a69SYan, Zheng return 0; 202710183a69SYan, Zheng } 202810183a69SYan, Zheng 2029779fe0fbSYan, Zheng pool_ns = ceph_try_get_string(ci->i_layout.pool_ns); 2030779fe0fbSYan, Zheng ret = __ceph_pool_perm_get(ci, pool, pool_ns); 2031779fe0fbSYan, Zheng ceph_put_string(pool_ns); 203210183a69SYan, Zheng if (ret < 0) 203310183a69SYan, Zheng return ret; 203410183a69SYan, Zheng 203510183a69SYan, Zheng flags = CEPH_I_POOL_PERM; 203610183a69SYan, Zheng if (ret & POOL_READ) 203710183a69SYan, Zheng flags |= CEPH_I_POOL_RD; 203810183a69SYan, Zheng if (ret & POOL_WRITE) 203910183a69SYan, Zheng flags |= CEPH_I_POOL_WR; 204010183a69SYan, Zheng 204110183a69SYan, Zheng spin_lock(&ci->i_ceph_lock); 2042779fe0fbSYan, Zheng if (pool == ci->i_layout.pool_id && 2043779fe0fbSYan, Zheng pool_ns == rcu_dereference_raw(ci->i_layout.pool_ns)) { 2044779fe0fbSYan, Zheng ci->i_ceph_flags |= flags; 204510183a69SYan, Zheng } else { 20467627151eSYan, Zheng pool = ci->i_layout.pool_id; 204710183a69SYan, Zheng flags = ci->i_ceph_flags; 204810183a69SYan, Zheng } 204910183a69SYan, Zheng spin_unlock(&ci->i_ceph_lock); 205010183a69SYan, Zheng goto check; 205110183a69SYan, Zheng } 205210183a69SYan, Zheng 205310183a69SYan, Zheng void ceph_pool_perm_destroy(struct ceph_mds_client *mdsc) 205410183a69SYan, Zheng { 205510183a69SYan, Zheng struct ceph_pool_perm *perm; 205610183a69SYan, Zheng struct rb_node *n; 205710183a69SYan, Zheng 205810183a69SYan, Zheng while (!RB_EMPTY_ROOT(&mdsc->pool_perm_tree)) { 205910183a69SYan, Zheng n = rb_first(&mdsc->pool_perm_tree); 206010183a69SYan, Zheng perm = rb_entry(n, struct ceph_pool_perm, node); 206110183a69SYan, Zheng rb_erase(n, &mdsc->pool_perm_tree); 206210183a69SYan, Zheng kfree(perm); 206310183a69SYan, Zheng } 206410183a69SYan, Zheng } 2065