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> 135c308356SJeff Layton #include <linux/iversion.h> 141d3576fdSSage Weil 151d3576fdSSage Weil #include "super.h" 163d14c5d2SYehuda Sadeh #include "mds_client.h" 1799ccbd22SMilosz Tanski #include "cache.h" 183d14c5d2SYehuda Sadeh #include <linux/ceph/osd_client.h> 1908c1ac50SIlya Dryomov #include <linux/ceph/striper.h> 201d3576fdSSage Weil 211d3576fdSSage Weil /* 221d3576fdSSage Weil * Ceph address space ops. 231d3576fdSSage Weil * 241d3576fdSSage Weil * There are a few funny things going on here. 251d3576fdSSage Weil * 261d3576fdSSage Weil * The page->private field is used to reference a struct 271d3576fdSSage Weil * ceph_snap_context for _every_ dirty page. This indicates which 281d3576fdSSage Weil * snapshot the page was logically dirtied in, and thus which snap 291d3576fdSSage Weil * context needs to be associated with the osd write during writeback. 301d3576fdSSage Weil * 311d3576fdSSage Weil * Similarly, struct ceph_inode_info maintains a set of counters to 3225985edcSLucas De Marchi * count dirty pages on the inode. In the absence of snapshots, 331d3576fdSSage Weil * i_wrbuffer_ref == i_wrbuffer_ref_head == the dirty page count. 341d3576fdSSage Weil * 351d3576fdSSage Weil * When a snapshot is taken (that is, when the client receives 361d3576fdSSage Weil * notification that a snapshot was taken), each inode with caps and 371d3576fdSSage Weil * with dirty pages (dirty pages implies there is a cap) gets a new 381d3576fdSSage Weil * ceph_cap_snap in the i_cap_snaps list (which is sorted in ascending 391d3576fdSSage Weil * order, new snaps go to the tail). The i_wrbuffer_ref_head count is 401d3576fdSSage Weil * moved to capsnap->dirty. (Unless a sync write is currently in 411d3576fdSSage Weil * progress. In that case, the capsnap is said to be "pending", new 421d3576fdSSage Weil * writes cannot start, and the capsnap isn't "finalized" until the 431d3576fdSSage Weil * write completes (or fails) and a final size/mtime for the inode for 441d3576fdSSage Weil * that snap can be settled upon.) i_wrbuffer_ref_head is reset to 0. 451d3576fdSSage Weil * 461d3576fdSSage Weil * On writeback, we must submit writes to the osd IN SNAP ORDER. So, 471d3576fdSSage Weil * we look for the first capsnap in i_cap_snaps and write out pages in 481d3576fdSSage Weil * that snap context _only_. Then we move on to the next capsnap, 491d3576fdSSage Weil * eventually reaching the "live" or "head" context (i.e., pages that 501d3576fdSSage Weil * are not yet snapped) and are writing the most recently dirtied 511d3576fdSSage Weil * pages. 521d3576fdSSage Weil * 531d3576fdSSage Weil * Invalidate and so forth must take care to ensure the dirty page 541d3576fdSSage Weil * accounting is preserved. 551d3576fdSSage Weil */ 561d3576fdSSage Weil 572baba250SYehuda Sadeh #define CONGESTION_ON_THRESH(congestion_kb) (congestion_kb >> (PAGE_SHIFT-10)) 582baba250SYehuda Sadeh #define CONGESTION_OFF_THRESH(congestion_kb) \ 592baba250SYehuda Sadeh (CONGESTION_ON_THRESH(congestion_kb) - \ 602baba250SYehuda Sadeh (CONGESTION_ON_THRESH(congestion_kb) >> 2)) 612baba250SYehuda Sadeh 6261600ef8SYan, Zheng static inline struct ceph_snap_context *page_snap_context(struct page *page) 6361600ef8SYan, Zheng { 6461600ef8SYan, Zheng if (PagePrivate(page)) 6561600ef8SYan, Zheng return (void *)page->private; 6661600ef8SYan, Zheng return NULL; 6761600ef8SYan, Zheng } 681d3576fdSSage Weil 691d3576fdSSage Weil /* 701d3576fdSSage Weil * Dirty a page. Optimistically adjust accounting, on the assumption 711d3576fdSSage Weil * that we won't race with invalidate. If we do, readjust. 721d3576fdSSage Weil */ 731d3576fdSSage Weil static int ceph_set_page_dirty(struct page *page) 741d3576fdSSage Weil { 751d3576fdSSage Weil struct address_space *mapping = page->mapping; 761d3576fdSSage Weil struct inode *inode; 771d3576fdSSage Weil struct ceph_inode_info *ci; 781d3576fdSSage Weil struct ceph_snap_context *snapc; 797d6e1f54SSha Zhengju int ret; 801d3576fdSSage Weil 811d3576fdSSage Weil if (unlikely(!mapping)) 821d3576fdSSage Weil return !TestSetPageDirty(page); 831d3576fdSSage Weil 847d6e1f54SSha Zhengju if (PageDirty(page)) { 851d3576fdSSage Weil dout("%p set_page_dirty %p idx %lu -- already dirty\n", 861d3576fdSSage Weil mapping->host, page, page->index); 877d6e1f54SSha Zhengju BUG_ON(!PagePrivate(page)); 881d3576fdSSage Weil return 0; 891d3576fdSSage Weil } 901d3576fdSSage Weil 911d3576fdSSage Weil inode = mapping->host; 921d3576fdSSage Weil ci = ceph_inode(inode); 931d3576fdSSage Weil 941d3576fdSSage Weil /* dirty the head */ 95be655596SSage Weil spin_lock(&ci->i_ceph_lock); 965dda377cSYan, Zheng BUG_ON(ci->i_wr_ref == 0); // caller should hold Fw reference 975dda377cSYan, Zheng if (__ceph_have_pending_cap_snap(ci)) { 985dda377cSYan, Zheng struct ceph_cap_snap *capsnap = 995dda377cSYan, Zheng list_last_entry(&ci->i_cap_snaps, 1005dda377cSYan, Zheng struct ceph_cap_snap, 1015dda377cSYan, Zheng ci_item); 1025dda377cSYan, Zheng snapc = ceph_get_snap_context(capsnap->context); 1035dda377cSYan, Zheng capsnap->dirty_pages++; 1045dda377cSYan, Zheng } else { 1055dda377cSYan, Zheng BUG_ON(!ci->i_head_snapc); 1065dda377cSYan, Zheng snapc = ceph_get_snap_context(ci->i_head_snapc); 1071d3576fdSSage Weil ++ci->i_wrbuffer_ref_head; 1085dda377cSYan, Zheng } 1091d3576fdSSage Weil if (ci->i_wrbuffer_ref == 0) 1100444d76aSDave Chinner ihold(inode); 1111d3576fdSSage Weil ++ci->i_wrbuffer_ref; 1121d3576fdSSage Weil dout("%p set_page_dirty %p idx %lu head %d/%d -> %d/%d " 1131d3576fdSSage Weil "snapc %p seq %lld (%d snaps)\n", 1141d3576fdSSage Weil mapping->host, page, page->index, 1151d3576fdSSage Weil ci->i_wrbuffer_ref-1, ci->i_wrbuffer_ref_head-1, 1161d3576fdSSage Weil ci->i_wrbuffer_ref, ci->i_wrbuffer_ref_head, 1171d3576fdSSage Weil snapc, snapc->seq, snapc->num_snaps); 118be655596SSage Weil spin_unlock(&ci->i_ceph_lock); 1191d3576fdSSage Weil 1201d3576fdSSage Weil /* 1211d3576fdSSage Weil * Reference snap context in page->private. Also set 1221d3576fdSSage Weil * PagePrivate so that we get invalidatepage callback. 1231d3576fdSSage Weil */ 1247d6e1f54SSha Zhengju BUG_ON(PagePrivate(page)); 1251d3576fdSSage Weil page->private = (unsigned long)snapc; 1261d3576fdSSage Weil SetPagePrivate(page); 1271d3576fdSSage Weil 1287d6e1f54SSha Zhengju ret = __set_page_dirty_nobuffers(page); 1297d6e1f54SSha Zhengju WARN_ON(!PageLocked(page)); 1307d6e1f54SSha Zhengju WARN_ON(!page->mapping); 1311d3576fdSSage Weil 1327d6e1f54SSha Zhengju return ret; 1331d3576fdSSage Weil } 1341d3576fdSSage Weil 1351d3576fdSSage Weil /* 1361d3576fdSSage Weil * If we are truncating the full page (i.e. offset == 0), adjust the 1371d3576fdSSage Weil * dirty page counters appropriately. Only called if there is private 1381d3576fdSSage Weil * data on the page. 1391d3576fdSSage Weil */ 140d47992f8SLukas Czerner static void ceph_invalidatepage(struct page *page, unsigned int offset, 141d47992f8SLukas Czerner unsigned int length) 1421d3576fdSSage Weil { 1434ce1e9adSAlexander Beregalov struct inode *inode; 1441d3576fdSSage Weil struct ceph_inode_info *ci; 14561600ef8SYan, Zheng struct ceph_snap_context *snapc = page_snap_context(page); 1461d3576fdSSage Weil 1474ce1e9adSAlexander Beregalov inode = page->mapping->host; 148b150f5c1SMilosz Tanski ci = ceph_inode(inode); 149b150f5c1SMilosz Tanski 15009cbfeafSKirill A. Shutemov if (offset != 0 || length != PAGE_SIZE) { 151b150f5c1SMilosz Tanski dout("%p invalidatepage %p idx %lu partial dirty page %u~%u\n", 152b150f5c1SMilosz Tanski inode, page, page->index, offset, length); 153b150f5c1SMilosz Tanski return; 154b150f5c1SMilosz Tanski } 1554ce1e9adSAlexander Beregalov 15699ccbd22SMilosz Tanski ceph_invalidate_fscache_page(inode, page); 15799ccbd22SMilosz Tanski 158b072d774SYan, Zheng WARN_ON(!PageLocked(page)); 15999ccbd22SMilosz Tanski if (!PagePrivate(page)) 16099ccbd22SMilosz Tanski return; 16199ccbd22SMilosz Tanski 1621d3576fdSSage Weil ClearPageChecked(page); 1631d3576fdSSage Weil 164569d39fcSLukas Czerner dout("%p invalidatepage %p idx %lu full dirty page\n", 165569d39fcSLukas Czerner inode, page, page->index); 166b150f5c1SMilosz Tanski 1671d3576fdSSage Weil ceph_put_wrbuffer_cap_refs(ci, 1, snapc); 1681d3576fdSSage Weil ceph_put_snap_context(snapc); 1691d3576fdSSage Weil page->private = 0; 1701d3576fdSSage Weil ClearPagePrivate(page); 1711d3576fdSSage Weil } 1721d3576fdSSage Weil 1731d3576fdSSage Weil static int ceph_releasepage(struct page *page, gfp_t g) 1741d3576fdSSage Weil { 175e55f1a18SNeilBrown dout("%p releasepage %p idx %lu (%sdirty)\n", page->mapping->host, 176e55f1a18SNeilBrown page, page->index, PageDirty(page) ? "" : "not "); 17799ccbd22SMilosz Tanski 17899ccbd22SMilosz Tanski /* Can we release the page from the cache? */ 17999ccbd22SMilosz Tanski if (!ceph_release_fscache_page(page, g)) 1801d3576fdSSage Weil return 0; 18199ccbd22SMilosz Tanski 18299ccbd22SMilosz Tanski return !PagePrivate(page); 1831d3576fdSSage Weil } 1841d3576fdSSage Weil 1851d3576fdSSage Weil /* 1861d3576fdSSage Weil * read a single page, without unlocking it. 1871d3576fdSSage Weil */ 188dd2bc473SYan, Zheng static int ceph_do_readpage(struct file *filp, struct page *page) 1891d3576fdSSage Weil { 190496ad9aaSAl Viro struct inode *inode = file_inode(filp); 1911d3576fdSSage Weil struct ceph_inode_info *ci = ceph_inode(inode); 192131d7eb4SYan, Zheng struct ceph_fs_client *fsc = ceph_inode_to_client(inode); 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); 221131d7eb4SYan, Zheng err = ceph_osdc_readpages(&fsc->client->osdc, ceph_vino(inode), 222131d7eb4SYan, Zheng &ci->i_layout, 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); 230131d7eb4SYan, Zheng if (err == -EBLACKLISTED) 231131d7eb4SYan, Zheng fsc->blacklisted = true; 2321d3576fdSSage Weil goto out; 23323cd573bSZhang Zhen } 23409cbfeafSKirill A. Shutemov if (err < PAGE_SIZE) 2351d3576fdSSage Weil /* zero fill remainder of page */ 23609cbfeafSKirill A. Shutemov zero_user_segment(page, err, PAGE_SIZE); 23723cd573bSZhang Zhen else 23856f91aadSLi Wang flush_dcache_page(page); 2391d3576fdSSage Weil 24023cd573bSZhang Zhen SetPageUptodate(page); 24199ccbd22SMilosz Tanski ceph_readpage_to_fscache(inode, page); 24299ccbd22SMilosz Tanski 2431d3576fdSSage Weil out: 2441d3576fdSSage Weil return err < 0 ? err : 0; 2451d3576fdSSage Weil } 2461d3576fdSSage Weil 2471d3576fdSSage Weil static int ceph_readpage(struct file *filp, struct page *page) 2481d3576fdSSage Weil { 249dd2bc473SYan, Zheng int r = ceph_do_readpage(filp, page); 250dd2bc473SYan, Zheng if (r != -EINPROGRESS) 2511d3576fdSSage Weil unlock_page(page); 252dd2bc473SYan, Zheng else 253dd2bc473SYan, Zheng r = 0; 2541d3576fdSSage Weil return r; 2551d3576fdSSage Weil } 2561d3576fdSSage Weil 2571d3576fdSSage Weil /* 2587c272194SSage Weil * Finish an async read(ahead) op. 2591d3576fdSSage Weil */ 26085e084feSIlya Dryomov static void finish_read(struct ceph_osd_request *req) 2611d3576fdSSage Weil { 2627c272194SSage Weil struct inode *inode = req->r_inode; 26387060c10SAlex Elder struct ceph_osd_data *osd_data; 26485e084feSIlya Dryomov int rc = req->r_result <= 0 ? req->r_result : 0; 26585e084feSIlya Dryomov int bytes = req->r_result >= 0 ? req->r_result : 0; 266e0c59487SAlex Elder int num_pages; 2677c272194SSage Weil int i; 2687c272194SSage Weil 2697c272194SSage Weil dout("finish_read %p req %p rc %d bytes %d\n", inode, req, rc, bytes); 270131d7eb4SYan, Zheng if (rc == -EBLACKLISTED) 271131d7eb4SYan, Zheng ceph_inode_to_client(inode)->blacklisted = true; 2727c272194SSage Weil 2737c272194SSage Weil /* unlock all pages, zeroing any data we didn't read */ 274406e2c9fSAlex Elder osd_data = osd_req_op_extent_osd_data(req, 0); 27587060c10SAlex Elder BUG_ON(osd_data->type != CEPH_OSD_DATA_TYPE_PAGES); 27687060c10SAlex Elder num_pages = calc_pages_for((u64)osd_data->alignment, 27787060c10SAlex Elder (u64)osd_data->length); 278e0c59487SAlex Elder for (i = 0; i < num_pages; i++) { 27987060c10SAlex Elder struct page *page = osd_data->pages[i]; 2807c272194SSage Weil 281368e3585SYan, Zheng if (rc < 0 && rc != -ENOENT) { 282368e3585SYan, Zheng ceph_fscache_readpage_cancel(inode, page); 283f36132a7SLi Wang goto unlock; 284368e3585SYan, Zheng } 28509cbfeafSKirill A. Shutemov if (bytes < (int)PAGE_SIZE) { 2867c272194SSage Weil /* zero (remainder of) page */ 2877c272194SSage Weil int s = bytes < 0 ? 0 : bytes; 28809cbfeafSKirill A. Shutemov zero_user_segment(page, s, PAGE_SIZE); 2897c272194SSage Weil } 2907c272194SSage Weil dout("finish_read %p uptodate %p idx %lu\n", inode, page, 2917c272194SSage Weil page->index); 2927c272194SSage Weil flush_dcache_page(page); 2937c272194SSage Weil SetPageUptodate(page); 29499ccbd22SMilosz Tanski ceph_readpage_to_fscache(inode, page); 295f36132a7SLi Wang unlock: 2967c272194SSage Weil unlock_page(page); 29709cbfeafSKirill A. Shutemov put_page(page); 29809cbfeafSKirill A. Shutemov bytes -= PAGE_SIZE; 2997c272194SSage Weil } 30087060c10SAlex Elder kfree(osd_data->pages); 3017c272194SSage Weil } 3027c272194SSage Weil 3037c272194SSage Weil /* 3047c272194SSage Weil * start an async read(ahead) operation. return nr_pages we submitted 3057c272194SSage Weil * a read for on success, or negative error code. 3067c272194SSage Weil */ 3075d988308SYan, Zheng static int start_read(struct inode *inode, struct ceph_rw_context *rw_ctx, 3085d988308SYan, Zheng struct list_head *page_list, int max) 3097c272194SSage Weil { 3107c272194SSage Weil struct ceph_osd_client *osdc = 3117c272194SSage Weil &ceph_inode_to_client(inode)->client->osdc; 3127c272194SSage Weil struct ceph_inode_info *ci = ceph_inode(inode); 313f86196eaSNikolay Borisov struct page *page = lru_to_page(page_list); 314acead002SAlex Elder struct ceph_vino vino; 3157c272194SSage Weil struct ceph_osd_request *req; 3167c272194SSage Weil u64 off; 3177c272194SSage Weil u64 len; 3187c272194SSage Weil int i; 3191d3576fdSSage Weil struct page **pages; 3207c272194SSage Weil pgoff_t next_index; 3217c272194SSage Weil int nr_pages = 0; 3222b1ac852SYan, Zheng int got = 0; 3232b1ac852SYan, Zheng int ret = 0; 3242b1ac852SYan, Zheng 3255d988308SYan, Zheng if (!rw_ctx) { 3262b1ac852SYan, Zheng /* caller of readpages does not hold buffer and read caps 3272b1ac852SYan, Zheng * (fadvise, madvise and readahead cases) */ 3282b1ac852SYan, Zheng int want = CEPH_CAP_FILE_CACHE; 3295e3ded1bSYan, Zheng ret = ceph_try_get_caps(inode, CEPH_CAP_FILE_RD, want, 3305e3ded1bSYan, Zheng true, &got); 3312b1ac852SYan, Zheng if (ret < 0) { 3322b1ac852SYan, Zheng dout("start_read %p, error getting cap\n", inode); 3332b1ac852SYan, Zheng } else if (!(got & want)) { 3342b1ac852SYan, Zheng dout("start_read %p, no cache cap\n", inode); 3352b1ac852SYan, Zheng ret = 0; 3362b1ac852SYan, Zheng } 3372b1ac852SYan, Zheng if (ret <= 0) { 3382b1ac852SYan, Zheng if (got) 3392b1ac852SYan, Zheng ceph_put_cap_refs(ci, got); 3402b1ac852SYan, Zheng while (!list_empty(page_list)) { 341f86196eaSNikolay Borisov page = lru_to_page(page_list); 3422b1ac852SYan, Zheng list_del(&page->lru); 3432b1ac852SYan, Zheng put_page(page); 3442b1ac852SYan, Zheng } 3452b1ac852SYan, Zheng return ret; 3462b1ac852SYan, Zheng } 3472b1ac852SYan, Zheng } 3487c272194SSage Weil 3496285bc23SAlex Elder off = (u64) page_offset(page); 3507c272194SSage Weil 3517c272194SSage Weil /* count pages */ 3527c272194SSage Weil next_index = page->index; 3537c272194SSage Weil list_for_each_entry_reverse(page, page_list, lru) { 3547c272194SSage Weil if (page->index != next_index) 3557c272194SSage Weil break; 3567c272194SSage Weil nr_pages++; 3577c272194SSage Weil next_index++; 3580d66a487SSage Weil if (max && nr_pages == max) 3590d66a487SSage Weil break; 3607c272194SSage Weil } 36109cbfeafSKirill A. Shutemov len = nr_pages << PAGE_SHIFT; 3627c272194SSage Weil dout("start_read %p nr_pages %d is %lld~%lld\n", inode, nr_pages, 3637c272194SSage Weil off, len); 364acead002SAlex Elder vino = ceph_vino(inode); 365acead002SAlex Elder req = ceph_osdc_new_request(osdc, &ci->i_layout, vino, off, &len, 366715e4cd4SYan, Zheng 0, 1, CEPH_OSD_OP_READ, 367acead002SAlex Elder CEPH_OSD_FLAG_READ, NULL, 3687c272194SSage Weil ci->i_truncate_seq, ci->i_truncate_size, 369acead002SAlex Elder false); 3702b1ac852SYan, Zheng if (IS_ERR(req)) { 3712b1ac852SYan, Zheng ret = PTR_ERR(req); 3722b1ac852SYan, Zheng goto out; 3732b1ac852SYan, Zheng } 3741d3576fdSSage Weil 3751d3576fdSSage Weil /* build page vector */ 376cf7b7e14SAlex Elder nr_pages = calc_pages_for(0, len); 3776da2ec56SKees Cook pages = kmalloc_array(nr_pages, sizeof(*pages), GFP_KERNEL); 3782b1ac852SYan, Zheng if (!pages) { 3797c272194SSage Weil ret = -ENOMEM; 3802b1ac852SYan, Zheng goto out_put; 3812b1ac852SYan, Zheng } 3827c272194SSage Weil for (i = 0; i < nr_pages; ++i) { 3837c272194SSage Weil page = list_entry(page_list->prev, struct page, lru); 3847c272194SSage Weil BUG_ON(PageLocked(page)); 3857c272194SSage Weil list_del(&page->lru); 3861d3576fdSSage Weil 3877c272194SSage Weil dout("start_read %p adding %p idx %lu\n", inode, page, 3887c272194SSage Weil page->index); 3897c272194SSage Weil if (add_to_page_cache_lru(page, &inode->i_data, page->index, 390687265e5SYan, Zheng GFP_KERNEL)) { 391d4d3aa38SMilosz Tanski ceph_fscache_uncache_page(inode, page); 39209cbfeafSKirill A. Shutemov put_page(page); 3937c272194SSage Weil dout("start_read %p add_to_page_cache failed %p\n", 3947c272194SSage Weil inode, page); 3957c272194SSage Weil nr_pages = i; 3961afe4785SYan, Zheng if (nr_pages > 0) { 3971afe4785SYan, Zheng len = nr_pages << PAGE_SHIFT; 398d641df81SYan, Zheng osd_req_op_extent_update(req, 0, len); 3991afe4785SYan, Zheng break; 4001afe4785SYan, Zheng } 4017c272194SSage Weil goto out_pages; 4021d3576fdSSage Weil } 4037c272194SSage Weil pages[i] = page; 4041d3576fdSSage Weil } 405406e2c9fSAlex Elder osd_req_op_extent_osd_data_pages(req, 0, pages, len, 0, false, false); 4067c272194SSage Weil req->r_callback = finish_read; 4077c272194SSage Weil req->r_inode = inode; 4087c272194SSage Weil 4097c272194SSage Weil dout("start_read %p starting %p %lld~%lld\n", inode, req, off, len); 4107c272194SSage Weil ret = ceph_osdc_start_request(osdc, req, false); 4117c272194SSage Weil if (ret < 0) 4127c272194SSage Weil goto out_pages; 4137c272194SSage Weil ceph_osdc_put_request(req); 4142b1ac852SYan, Zheng 4152b1ac852SYan, Zheng /* After adding locked pages to page cache, the inode holds cache cap. 4162b1ac852SYan, Zheng * So we can drop our cap refs. */ 4172b1ac852SYan, Zheng if (got) 4182b1ac852SYan, Zheng ceph_put_cap_refs(ci, got); 4192b1ac852SYan, Zheng 4207c272194SSage Weil return nr_pages; 4217c272194SSage Weil 4227c272194SSage Weil out_pages: 4231afe4785SYan, Zheng for (i = 0; i < nr_pages; ++i) { 4241afe4785SYan, Zheng ceph_fscache_readpage_cancel(inode, pages[i]); 4251afe4785SYan, Zheng unlock_page(pages[i]); 4261afe4785SYan, Zheng } 4271afe4785SYan, Zheng ceph_put_page_vector(pages, nr_pages, false); 4282b1ac852SYan, Zheng out_put: 4297c272194SSage Weil ceph_osdc_put_request(req); 4302b1ac852SYan, Zheng out: 4312b1ac852SYan, Zheng if (got) 4322b1ac852SYan, Zheng ceph_put_cap_refs(ci, got); 4337c272194SSage Weil return ret; 4341d3576fdSSage Weil } 4351d3576fdSSage Weil 4367c272194SSage Weil 4371d3576fdSSage Weil /* 4381d3576fdSSage Weil * Read multiple pages. Leave pages we don't read + unlock in page_list; 4391d3576fdSSage Weil * the caller (VM) cleans them up. 4401d3576fdSSage Weil */ 4411d3576fdSSage Weil static int ceph_readpages(struct file *file, struct address_space *mapping, 4421d3576fdSSage Weil struct list_head *page_list, unsigned nr_pages) 4431d3576fdSSage Weil { 444496ad9aaSAl Viro struct inode *inode = file_inode(file); 4450d66a487SSage Weil struct ceph_fs_client *fsc = ceph_inode_to_client(inode); 44673737682SChengguang Xu struct ceph_file_info *fi = file->private_data; 4475d988308SYan, Zheng struct ceph_rw_context *rw_ctx; 4481d3576fdSSage Weil int rc = 0; 4490d66a487SSage Weil int max = 0; 4501d3576fdSSage Weil 45183701246SYan, Zheng if (ceph_inode(inode)->i_inline_version != CEPH_INLINE_NONE) 45283701246SYan, Zheng return -EINVAL; 45383701246SYan, Zheng 45499ccbd22SMilosz Tanski rc = ceph_readpages_from_fscache(mapping->host, mapping, page_list, 45599ccbd22SMilosz Tanski &nr_pages); 45699ccbd22SMilosz Tanski 45799ccbd22SMilosz Tanski if (rc == 0) 45899ccbd22SMilosz Tanski goto out; 45999ccbd22SMilosz Tanski 46073737682SChengguang Xu rw_ctx = ceph_find_rw_context(fi); 461aa187926SYan, Zheng max = fsc->mount_options->rsize >> PAGE_SHIFT; 4625d988308SYan, Zheng dout("readpages %p file %p ctx %p nr_pages %d max %d\n", 4635d988308SYan, Zheng inode, file, rw_ctx, nr_pages, max); 4647c272194SSage Weil while (!list_empty(page_list)) { 4655d988308SYan, Zheng rc = start_read(inode, rw_ctx, page_list, max); 4661d3576fdSSage Weil if (rc < 0) 4671d3576fdSSage Weil goto out; 4681d3576fdSSage Weil } 4691d3576fdSSage Weil out: 47076be778bSMilosz Tanski ceph_fscache_readpages_cancel(inode, page_list); 47176be778bSMilosz Tanski 4727c272194SSage Weil dout("readpages %p file %p ret %d\n", inode, file, rc); 4731d3576fdSSage Weil return rc; 4741d3576fdSSage Weil } 4751d3576fdSSage Weil 4761f934b00SYan, Zheng struct ceph_writeback_ctl 4771f934b00SYan, Zheng { 4781f934b00SYan, Zheng loff_t i_size; 4791f934b00SYan, Zheng u64 truncate_size; 4801f934b00SYan, Zheng u32 truncate_seq; 4811f934b00SYan, Zheng bool size_stable; 4822a2d927eSYan, Zheng bool head_snapc; 4831f934b00SYan, Zheng }; 4841f934b00SYan, Zheng 4851d3576fdSSage Weil /* 4861d3576fdSSage Weil * Get ref for the oldest snapc for an inode with dirty data... that is, the 4871d3576fdSSage Weil * only snap context we are allowed to write back. 4881d3576fdSSage Weil */ 4891f934b00SYan, Zheng static struct ceph_snap_context * 49005455e11SYan, Zheng get_oldest_context(struct inode *inode, struct ceph_writeback_ctl *ctl, 49105455e11SYan, Zheng struct ceph_snap_context *page_snapc) 4921d3576fdSSage Weil { 4931d3576fdSSage Weil struct ceph_inode_info *ci = ceph_inode(inode); 4941d3576fdSSage Weil struct ceph_snap_context *snapc = NULL; 4951d3576fdSSage Weil struct ceph_cap_snap *capsnap = NULL; 4961d3576fdSSage Weil 497be655596SSage Weil spin_lock(&ci->i_ceph_lock); 4981d3576fdSSage Weil list_for_each_entry(capsnap, &ci->i_cap_snaps, ci_item) { 4991d3576fdSSage Weil dout(" cap_snap %p snapc %p has %d dirty pages\n", capsnap, 5001d3576fdSSage Weil capsnap->context, capsnap->dirty_pages); 50105455e11SYan, Zheng if (!capsnap->dirty_pages) 50205455e11SYan, Zheng continue; 50305455e11SYan, Zheng 50405455e11SYan, Zheng /* get i_size, truncate_{seq,size} for page_snapc? */ 50505455e11SYan, Zheng if (snapc && capsnap->context != page_snapc) 50605455e11SYan, Zheng continue; 50705455e11SYan, Zheng 5081f934b00SYan, Zheng if (ctl) { 5091f934b00SYan, Zheng if (capsnap->writing) { 5101f934b00SYan, Zheng ctl->i_size = i_size_read(inode); 5111f934b00SYan, Zheng ctl->size_stable = false; 5121f934b00SYan, Zheng } else { 5131f934b00SYan, Zheng ctl->i_size = capsnap->size; 5141f934b00SYan, Zheng ctl->size_stable = true; 5151f934b00SYan, Zheng } 5161f934b00SYan, Zheng ctl->truncate_size = capsnap->truncate_size; 5171f934b00SYan, Zheng ctl->truncate_seq = capsnap->truncate_seq; 5182a2d927eSYan, Zheng ctl->head_snapc = false; 5191f934b00SYan, Zheng } 52005455e11SYan, Zheng 52105455e11SYan, Zheng if (snapc) 5221d3576fdSSage Weil break; 52305455e11SYan, Zheng 52405455e11SYan, Zheng snapc = ceph_get_snap_context(capsnap->context); 52505455e11SYan, Zheng if (!page_snapc || 52605455e11SYan, Zheng page_snapc == snapc || 52705455e11SYan, Zheng page_snapc->seq > snapc->seq) 52805455e11SYan, Zheng break; 5291d3576fdSSage Weil } 5307d8cb26dSSage Weil if (!snapc && ci->i_wrbuffer_ref_head) { 53180e755feSSage Weil snapc = ceph_get_snap_context(ci->i_head_snapc); 5321d3576fdSSage Weil dout(" head snapc %p has %d dirty pages\n", 5331d3576fdSSage Weil snapc, ci->i_wrbuffer_ref_head); 5341f934b00SYan, Zheng if (ctl) { 5351f934b00SYan, Zheng ctl->i_size = i_size_read(inode); 5361f934b00SYan, Zheng ctl->truncate_size = ci->i_truncate_size; 5371f934b00SYan, Zheng ctl->truncate_seq = ci->i_truncate_seq; 5381f934b00SYan, Zheng ctl->size_stable = false; 5392a2d927eSYan, Zheng ctl->head_snapc = true; 5401f934b00SYan, Zheng } 5411d3576fdSSage Weil } 542be655596SSage Weil spin_unlock(&ci->i_ceph_lock); 5431d3576fdSSage Weil return snapc; 5441d3576fdSSage Weil } 5451d3576fdSSage Weil 5461f934b00SYan, Zheng static u64 get_writepages_data_length(struct inode *inode, 5471f934b00SYan, Zheng struct page *page, u64 start) 5481f934b00SYan, Zheng { 5491f934b00SYan, Zheng struct ceph_inode_info *ci = ceph_inode(inode); 5501f934b00SYan, Zheng struct ceph_snap_context *snapc = page_snap_context(page); 5511f934b00SYan, Zheng struct ceph_cap_snap *capsnap = NULL; 5521f934b00SYan, Zheng u64 end = i_size_read(inode); 5531f934b00SYan, Zheng 5541f934b00SYan, Zheng if (snapc != ci->i_head_snapc) { 5551f934b00SYan, Zheng bool found = false; 5561f934b00SYan, Zheng spin_lock(&ci->i_ceph_lock); 5571f934b00SYan, Zheng list_for_each_entry(capsnap, &ci->i_cap_snaps, ci_item) { 5581f934b00SYan, Zheng if (capsnap->context == snapc) { 5591f934b00SYan, Zheng if (!capsnap->writing) 5601f934b00SYan, Zheng end = capsnap->size; 5611f934b00SYan, Zheng found = true; 5621f934b00SYan, Zheng break; 5631f934b00SYan, Zheng } 5641f934b00SYan, Zheng } 5651f934b00SYan, Zheng spin_unlock(&ci->i_ceph_lock); 5661f934b00SYan, Zheng WARN_ON(!found); 5671f934b00SYan, Zheng } 5681f934b00SYan, Zheng if (end > page_offset(page) + PAGE_SIZE) 5691f934b00SYan, Zheng end = page_offset(page) + PAGE_SIZE; 5701f934b00SYan, Zheng return end > start ? end - start : 0; 5711f934b00SYan, Zheng } 5721f934b00SYan, Zheng 5731d3576fdSSage Weil /* 5741d3576fdSSage Weil * Write a single page, but leave the page locked. 5751d3576fdSSage Weil * 576b72b13ebSJeff Layton * If we get a write error, mark the mapping for error, but still adjust the 5771d3576fdSSage Weil * dirty page accounting (i.e., page is no longer dirty). 5781d3576fdSSage Weil */ 5791d3576fdSSage Weil static int writepage_nounlock(struct page *page, struct writeback_control *wbc) 5801d3576fdSSage Weil { 5811d3576fdSSage Weil struct inode *inode; 5821d3576fdSSage Weil struct ceph_inode_info *ci; 5833d14c5d2SYehuda Sadeh struct ceph_fs_client *fsc; 5846298a337SSage Weil struct ceph_snap_context *snapc, *oldest; 585fc2744aaSYan, Zheng loff_t page_off = page_offset(page); 58643986881SYan, Zheng int err, len = PAGE_SIZE; 5871f934b00SYan, Zheng struct ceph_writeback_ctl ceph_wbc; 5881d3576fdSSage Weil 5891d3576fdSSage Weil dout("writepage %p idx %lu\n", page, page->index); 5901d3576fdSSage Weil 5911d3576fdSSage Weil inode = page->mapping->host; 5921d3576fdSSage Weil ci = ceph_inode(inode); 5933d14c5d2SYehuda Sadeh fsc = ceph_inode_to_client(inode); 5941d3576fdSSage Weil 5951d3576fdSSage Weil /* verify this is a writeable snap context */ 59661600ef8SYan, Zheng snapc = page_snap_context(page); 597d37b1d99SMarkus Elfring if (!snapc) { 5981d3576fdSSage Weil dout("writepage %p page %p not dirty?\n", inode, page); 59943986881SYan, Zheng return 0; 6001d3576fdSSage Weil } 60105455e11SYan, Zheng oldest = get_oldest_context(inode, &ceph_wbc, snapc); 6026298a337SSage Weil if (snapc->seq > oldest->seq) { 6031d3576fdSSage Weil dout("writepage %p page %p snapc %p not writeable - noop\n", 60461600ef8SYan, Zheng inode, page, snapc); 6051d3576fdSSage Weil /* we should only noop if called by kswapd */ 606fa71fefbSYan, Zheng WARN_ON(!(current->flags & PF_MEMALLOC)); 6076298a337SSage Weil ceph_put_snap_context(oldest); 608fa71fefbSYan, Zheng redirty_page_for_writepage(wbc, page); 60943986881SYan, Zheng return 0; 6101d3576fdSSage Weil } 6116298a337SSage Weil ceph_put_snap_context(oldest); 6121d3576fdSSage Weil 6131d3576fdSSage Weil /* is this a partial page at end of file? */ 6141f934b00SYan, Zheng if (page_off >= ceph_wbc.i_size) { 6151f934b00SYan, Zheng dout("%p page eof %llu\n", page, ceph_wbc.i_size); 61605455e11SYan, Zheng page->mapping->a_ops->invalidatepage(page, 0, PAGE_SIZE); 61743986881SYan, Zheng return 0; 618fc2744aaSYan, Zheng } 61943986881SYan, Zheng 6201f934b00SYan, Zheng if (ceph_wbc.i_size < page_off + len) 6211f934b00SYan, Zheng len = ceph_wbc.i_size - page_off; 6221d3576fdSSage Weil 6231c0a9c2dSYan, Zheng dout("writepage %p page %p index %lu on %llu~%u snapc %p seq %lld\n", 6241c0a9c2dSYan, Zheng inode, page, page->index, page_off, len, snapc, snapc->seq); 6251d3576fdSSage Weil 626314c4737SYan, Zheng if (atomic_long_inc_return(&fsc->writeback_count) > 6273d14c5d2SYehuda Sadeh CONGESTION_ON_THRESH(fsc->mount_options->congestion_kb)) 62809dc9fc2SJan Kara set_bdi_congested(inode_to_bdi(inode), BLK_RW_ASYNC); 6292baba250SYehuda Sadeh 6301d3576fdSSage Weil set_page_writeback(page); 6311f934b00SYan, Zheng err = ceph_osdc_writepages(&fsc->client->osdc, ceph_vino(inode), 6321f934b00SYan, Zheng &ci->i_layout, snapc, page_off, len, 6331f934b00SYan, Zheng ceph_wbc.truncate_seq, 6341f934b00SYan, Zheng ceph_wbc.truncate_size, 635fac02ddfSArnd Bergmann &inode->i_mtime, &page, 1); 6361d3576fdSSage Weil if (err < 0) { 637ad15ec06SYan, Zheng struct writeback_control tmp_wbc; 638ad15ec06SYan, Zheng if (!wbc) 639ad15ec06SYan, Zheng wbc = &tmp_wbc; 640ad15ec06SYan, Zheng if (err == -ERESTARTSYS) { 641ad15ec06SYan, Zheng /* killed by SIGKILL */ 642ad15ec06SYan, Zheng dout("writepage interrupted page %p\n", page); 643ad15ec06SYan, Zheng redirty_page_for_writepage(wbc, page); 644ad15ec06SYan, Zheng end_page_writeback(page); 64543986881SYan, Zheng return err; 646ad15ec06SYan, Zheng } 647131d7eb4SYan, Zheng if (err == -EBLACKLISTED) 648131d7eb4SYan, Zheng fsc->blacklisted = true; 649ad15ec06SYan, Zheng dout("writepage setting page/mapping error %d %p\n", 650ad15ec06SYan, Zheng err, page); 6511d3576fdSSage Weil mapping_set_error(&inode->i_data, err); 6521d3576fdSSage Weil wbc->pages_skipped++; 6531d3576fdSSage Weil } else { 6541d3576fdSSage Weil dout("writepage cleaned page %p\n", page); 6551d3576fdSSage Weil err = 0; /* vfs expects us to return 0 */ 6561d3576fdSSage Weil } 6571d3576fdSSage Weil page->private = 0; 6581d3576fdSSage Weil ClearPagePrivate(page); 6591d3576fdSSage Weil end_page_writeback(page); 6601d3576fdSSage Weil ceph_put_wrbuffer_cap_refs(ci, 1, snapc); 6616298a337SSage Weil ceph_put_snap_context(snapc); /* page's reference */ 662314c4737SYan, Zheng 663314c4737SYan, Zheng if (atomic_long_dec_return(&fsc->writeback_count) < 664314c4737SYan, Zheng CONGESTION_OFF_THRESH(fsc->mount_options->congestion_kb)) 665314c4737SYan, Zheng clear_bdi_congested(inode_to_bdi(inode), BLK_RW_ASYNC); 666314c4737SYan, Zheng 6671d3576fdSSage Weil return err; 6681d3576fdSSage Weil } 6691d3576fdSSage Weil 6701d3576fdSSage Weil static int ceph_writepage(struct page *page, struct writeback_control *wbc) 6711d3576fdSSage Weil { 672dbd646a8SYehuda Sadeh int err; 673dbd646a8SYehuda Sadeh struct inode *inode = page->mapping->host; 674dbd646a8SYehuda Sadeh BUG_ON(!inode); 67570b666c3SSage Weil ihold(inode); 676dbd646a8SYehuda Sadeh err = writepage_nounlock(page, wbc); 677ad15ec06SYan, Zheng if (err == -ERESTARTSYS) { 678ad15ec06SYan, Zheng /* direct memory reclaimer was killed by SIGKILL. return 0 679ad15ec06SYan, Zheng * to prevent caller from setting mapping/page error */ 680ad15ec06SYan, Zheng err = 0; 681ad15ec06SYan, Zheng } 6821d3576fdSSage Weil unlock_page(page); 683dbd646a8SYehuda Sadeh iput(inode); 6841d3576fdSSage Weil return err; 6851d3576fdSSage Weil } 6861d3576fdSSage Weil 6871d3576fdSSage Weil /* 6881d3576fdSSage Weil * async writeback completion handler. 6891d3576fdSSage Weil * 6901d3576fdSSage Weil * If we get an error, set the mapping error bit, but not the individual 6911d3576fdSSage Weil * page error bits. 6921d3576fdSSage Weil */ 69385e084feSIlya Dryomov static void writepages_finish(struct ceph_osd_request *req) 6941d3576fdSSage Weil { 6951d3576fdSSage Weil struct inode *inode = req->r_inode; 6961d3576fdSSage Weil struct ceph_inode_info *ci = ceph_inode(inode); 69787060c10SAlex Elder struct ceph_osd_data *osd_data; 6981d3576fdSSage Weil struct page *page; 6995b64640cSYan, Zheng int num_pages, total_pages = 0; 7005b64640cSYan, Zheng int i, j; 7015b64640cSYan, Zheng int rc = req->r_result; 7021d3576fdSSage Weil struct ceph_snap_context *snapc = req->r_snapc; 7031d3576fdSSage Weil struct address_space *mapping = inode->i_mapping; 7043d14c5d2SYehuda Sadeh struct ceph_fs_client *fsc = ceph_inode_to_client(inode); 7055b64640cSYan, Zheng bool remove_page; 7061d3576fdSSage Weil 7075b64640cSYan, Zheng dout("writepages_finish %p rc %d\n", inode, rc); 70826544c62SJeff Layton if (rc < 0) { 7091d3576fdSSage Weil mapping_set_error(mapping, rc); 71026544c62SJeff Layton ceph_set_error_write(ci); 711131d7eb4SYan, Zheng if (rc == -EBLACKLISTED) 712131d7eb4SYan, Zheng fsc->blacklisted = true; 71326544c62SJeff Layton } else { 71426544c62SJeff Layton ceph_clear_error_write(ci); 71526544c62SJeff Layton } 716e63dc5c7SYehuda Sadeh 717e63dc5c7SYehuda Sadeh /* 718e63dc5c7SYehuda Sadeh * We lost the cache cap, need to truncate the page before 719e63dc5c7SYehuda Sadeh * it is unlocked, otherwise we'd truncate it later in the 720e63dc5c7SYehuda Sadeh * page truncation thread, possibly losing some data that 721e63dc5c7SYehuda Sadeh * raced its way in 722e63dc5c7SYehuda Sadeh */ 7235b64640cSYan, Zheng remove_page = !(ceph_caps_issued(ci) & 7245b64640cSYan, Zheng (CEPH_CAP_FILE_CACHE|CEPH_CAP_FILE_LAZYIO)); 7255b64640cSYan, Zheng 7265b64640cSYan, Zheng /* clean all pages */ 7275b64640cSYan, Zheng for (i = 0; i < req->r_num_ops; i++) { 7285b64640cSYan, Zheng if (req->r_ops[i].op != CEPH_OSD_OP_WRITE) 7295b64640cSYan, Zheng break; 7305b64640cSYan, Zheng 7315b64640cSYan, Zheng osd_data = osd_req_op_extent_osd_data(req, i); 7325b64640cSYan, Zheng BUG_ON(osd_data->type != CEPH_OSD_DATA_TYPE_PAGES); 7335b64640cSYan, Zheng num_pages = calc_pages_for((u64)osd_data->alignment, 7345b64640cSYan, Zheng (u64)osd_data->length); 7355b64640cSYan, Zheng total_pages += num_pages; 7365b64640cSYan, Zheng for (j = 0; j < num_pages; j++) { 7375b64640cSYan, Zheng page = osd_data->pages[j]; 7385b64640cSYan, Zheng BUG_ON(!page); 7395b64640cSYan, Zheng WARN_ON(!PageUptodate(page)); 7405b64640cSYan, Zheng 7415b64640cSYan, Zheng if (atomic_long_dec_return(&fsc->writeback_count) < 7425b64640cSYan, Zheng CONGESTION_OFF_THRESH( 7435b64640cSYan, Zheng fsc->mount_options->congestion_kb)) 74409dc9fc2SJan Kara clear_bdi_congested(inode_to_bdi(inode), 7455b64640cSYan, Zheng BLK_RW_ASYNC); 7465b64640cSYan, Zheng 7475b64640cSYan, Zheng ceph_put_snap_context(page_snap_context(page)); 7485b64640cSYan, Zheng page->private = 0; 7495b64640cSYan, Zheng ClearPagePrivate(page); 7505b64640cSYan, Zheng dout("unlocking %p\n", page); 7515b64640cSYan, Zheng end_page_writeback(page); 7525b64640cSYan, Zheng 7535b64640cSYan, Zheng if (remove_page) 7545b64640cSYan, Zheng generic_error_remove_page(inode->i_mapping, 7555b64640cSYan, Zheng page); 756e63dc5c7SYehuda Sadeh 7571d3576fdSSage Weil unlock_page(page); 7581d3576fdSSage Weil } 7595b64640cSYan, Zheng dout("writepages_finish %p wrote %llu bytes cleaned %d pages\n", 7605b64640cSYan, Zheng inode, osd_data->length, rc >= 0 ? num_pages : 0); 7611d3576fdSSage Weil 76296ac9158SJohn Hubbard release_pages(osd_data->pages, num_pages); 7635b64640cSYan, Zheng } 7645b64640cSYan, Zheng 7655b64640cSYan, Zheng ceph_put_wrbuffer_cap_refs(ci, total_pages, snapc); 7665b64640cSYan, Zheng 7675b64640cSYan, Zheng osd_data = osd_req_op_extent_osd_data(req, 0); 76887060c10SAlex Elder if (osd_data->pages_from_pool) 76987060c10SAlex Elder mempool_free(osd_data->pages, 770640ef79dSCheng Renquan ceph_sb_to_client(inode->i_sb)->wb_pagevec_pool); 7711d3576fdSSage Weil else 77287060c10SAlex Elder kfree(osd_data->pages); 7731d3576fdSSage Weil ceph_osdc_put_request(req); 7741d3576fdSSage Weil } 7751d3576fdSSage Weil 7761d3576fdSSage Weil /* 7771d3576fdSSage Weil * initiate async writeback 7781d3576fdSSage Weil */ 7791d3576fdSSage Weil static int ceph_writepages_start(struct address_space *mapping, 7801d3576fdSSage Weil struct writeback_control *wbc) 7811d3576fdSSage Weil { 7821d3576fdSSage Weil struct inode *inode = mapping->host; 7831d3576fdSSage Weil struct ceph_inode_info *ci = ceph_inode(inode); 784fc2744aaSYan, Zheng struct ceph_fs_client *fsc = ceph_inode_to_client(inode); 785fc2744aaSYan, Zheng struct ceph_vino vino = ceph_vino(inode); 7862a2d927eSYan, Zheng pgoff_t index, start_index, end = -1; 78780e755feSSage Weil struct ceph_snap_context *snapc = NULL, *last_snapc = NULL, *pgsnapc; 7881d3576fdSSage Weil struct pagevec pvec; 7891d3576fdSSage Weil int rc = 0; 79093407472SFabian Frederick unsigned int wsize = i_blocksize(inode); 7911d3576fdSSage Weil struct ceph_osd_request *req = NULL; 7921f934b00SYan, Zheng struct ceph_writeback_ctl ceph_wbc; 793590e9d98SYan, Zheng bool should_loop, range_whole = false; 794af9cc401SYan, Zheng bool done = false; 7951d3576fdSSage Weil 7963fb99d48SYanhu Cao dout("writepages_start %p (mode=%s)\n", inode, 7971d3576fdSSage Weil wbc->sync_mode == WB_SYNC_NONE ? "NONE" : 7981d3576fdSSage Weil (wbc->sync_mode == WB_SYNC_ALL ? "ALL" : "HOLD")); 7991d3576fdSSage Weil 80052953d55SSeraphime Kirkovski if (READ_ONCE(fsc->mount_state) == CEPH_MOUNT_SHUTDOWN) { 8016c93df5dSYan, Zheng if (ci->i_wrbuffer_ref > 0) { 8026c93df5dSYan, Zheng pr_warn_ratelimited( 8036c93df5dSYan, Zheng "writepage_start %p %lld forced umount\n", 8046c93df5dSYan, Zheng inode, ceph_ino(inode)); 8056c93df5dSYan, Zheng } 806a341d4dfSYan, Zheng mapping_set_error(mapping, -EIO); 8071d3576fdSSage Weil return -EIO; /* we're in a forced umount, don't write! */ 8081d3576fdSSage Weil } 80995cca2b4SYan, Zheng if (fsc->mount_options->wsize < wsize) 8103d14c5d2SYehuda Sadeh wsize = fsc->mount_options->wsize; 8111d3576fdSSage Weil 81286679820SMel Gorman pagevec_init(&pvec); 8131d3576fdSSage Weil 814590e9d98SYan, Zheng start_index = wbc->range_cyclic ? mapping->writeback_index : 0; 815590e9d98SYan, Zheng index = start_index; 8161d3576fdSSage Weil 8171d3576fdSSage Weil retry: 8181d3576fdSSage Weil /* find oldest snap context with dirty data */ 81905455e11SYan, Zheng snapc = get_oldest_context(inode, &ceph_wbc, NULL); 8201d3576fdSSage Weil if (!snapc) { 8211d3576fdSSage Weil /* hmm, why does writepages get called when there 8221d3576fdSSage Weil is no dirty data? */ 8231d3576fdSSage Weil dout(" no snap context with dirty data?\n"); 8241d3576fdSSage Weil goto out; 8251d3576fdSSage Weil } 8261d3576fdSSage Weil dout(" oldest snapc is %p seq %lld (%d snaps)\n", 8271d3576fdSSage Weil snapc, snapc->seq, snapc->num_snaps); 828fc2744aaSYan, Zheng 8292a2d927eSYan, Zheng should_loop = false; 8302a2d927eSYan, Zheng if (ceph_wbc.head_snapc && snapc != last_snapc) { 8312a2d927eSYan, Zheng /* where to start/end? */ 8322a2d927eSYan, Zheng if (wbc->range_cyclic) { 8332a2d927eSYan, Zheng index = start_index; 8342a2d927eSYan, Zheng end = -1; 8352a2d927eSYan, Zheng if (index > 0) 8362a2d927eSYan, Zheng should_loop = true; 8372a2d927eSYan, Zheng dout(" cyclic, start at %lu\n", index); 8382a2d927eSYan, Zheng } else { 8392a2d927eSYan, Zheng index = wbc->range_start >> PAGE_SHIFT; 8402a2d927eSYan, Zheng end = wbc->range_end >> PAGE_SHIFT; 8412a2d927eSYan, Zheng if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX) 8422a2d927eSYan, Zheng range_whole = true; 8432a2d927eSYan, Zheng dout(" not cyclic, %lu to %lu\n", index, end); 8441d3576fdSSage Weil } 8452a2d927eSYan, Zheng } else if (!ceph_wbc.head_snapc) { 8462a2d927eSYan, Zheng /* Do not respect wbc->range_{start,end}. Dirty pages 8472a2d927eSYan, Zheng * in that range can be associated with newer snapc. 8482a2d927eSYan, Zheng * They are not writeable until we write all dirty pages 8492a2d927eSYan, Zheng * associated with 'snapc' get written */ 8501582af2eSYan, Zheng if (index > 0) 8512a2d927eSYan, Zheng should_loop = true; 8522a2d927eSYan, Zheng dout(" non-head snapc, range whole\n"); 8532a2d927eSYan, Zheng } 8542a2d927eSYan, Zheng 8552a2d927eSYan, Zheng ceph_put_snap_context(last_snapc); 8561d3576fdSSage Weil last_snapc = snapc; 8571d3576fdSSage Weil 858af9cc401SYan, Zheng while (!done && index <= end) { 8595b64640cSYan, Zheng int num_ops = 0, op_idx; 8600e5ecac7SYan, Zheng unsigned i, pvec_pages, max_pages, locked_pages = 0; 8615b64640cSYan, Zheng struct page **pages = NULL, **data_pages; 862e5975c7cSAlex Elder mempool_t *pool = NULL; /* Becomes non-null if mempool used */ 8631d3576fdSSage Weil struct page *page; 8640e5ecac7SYan, Zheng pgoff_t strip_unit_end = 0; 8655b64640cSYan, Zheng u64 offset = 0, len = 0; 8661d3576fdSSage Weil 8670e5ecac7SYan, Zheng max_pages = wsize >> PAGE_SHIFT; 8681d3576fdSSage Weil 8691d3576fdSSage Weil get_more_pages: 8704be90299SJan Kara pvec_pages = pagevec_lookup_range_nr_tag(&pvec, mapping, &index, 8710ed75fc8SJan Kara end, PAGECACHE_TAG_DIRTY, 8724be90299SJan Kara max_pages - locked_pages); 8730ed75fc8SJan Kara dout("pagevec_lookup_range_tag got %d\n", pvec_pages); 8741d3576fdSSage Weil if (!pvec_pages && !locked_pages) 8751d3576fdSSage Weil break; 8761d3576fdSSage Weil for (i = 0; i < pvec_pages && locked_pages < max_pages; i++) { 8771d3576fdSSage Weil page = pvec.pages[i]; 8781d3576fdSSage Weil dout("? %p idx %lu\n", page, page->index); 8791d3576fdSSage Weil if (locked_pages == 0) 8801d3576fdSSage Weil lock_page(page); /* first page */ 8811d3576fdSSage Weil else if (!trylock_page(page)) 8821d3576fdSSage Weil break; 8831d3576fdSSage Weil 8841d3576fdSSage Weil /* only dirty pages, or our accounting breaks */ 8851d3576fdSSage Weil if (unlikely(!PageDirty(page)) || 8861d3576fdSSage Weil unlikely(page->mapping != mapping)) { 8871d3576fdSSage Weil dout("!dirty or !mapping %p\n", page); 8881d3576fdSSage Weil unlock_page(page); 8890713e5f2SYan, Zheng continue; 8901d3576fdSSage Weil } 891af9cc401SYan, Zheng /* only if matching snap context */ 892af9cc401SYan, Zheng pgsnapc = page_snap_context(page); 893af9cc401SYan, Zheng if (pgsnapc != snapc) { 894af9cc401SYan, Zheng dout("page snapc %p %lld != oldest %p %lld\n", 895af9cc401SYan, Zheng pgsnapc, pgsnapc->seq, snapc, snapc->seq); 8961582af2eSYan, Zheng if (!should_loop && 8971582af2eSYan, Zheng !ceph_wbc.head_snapc && 8981582af2eSYan, Zheng wbc->sync_mode != WB_SYNC_NONE) 8991582af2eSYan, Zheng should_loop = true; 9001d3576fdSSage Weil unlock_page(page); 901af9cc401SYan, Zheng continue; 9021d3576fdSSage Weil } 9031f934b00SYan, Zheng if (page_offset(page) >= ceph_wbc.i_size) { 9041f934b00SYan, Zheng dout("%p page eof %llu\n", 9051f934b00SYan, Zheng page, ceph_wbc.i_size); 906c95f1c5fSErqi Chen if ((ceph_wbc.size_stable || 907c95f1c5fSErqi Chen page_offset(page) >= i_size_read(inode)) && 908c95f1c5fSErqi Chen clear_page_dirty_for_io(page)) 909af9cc401SYan, Zheng mapping->a_ops->invalidatepage(page, 910af9cc401SYan, Zheng 0, PAGE_SIZE); 911af9cc401SYan, Zheng unlock_page(page); 912af9cc401SYan, Zheng continue; 913af9cc401SYan, Zheng } 914af9cc401SYan, Zheng if (strip_unit_end && (page->index > strip_unit_end)) { 915af9cc401SYan, Zheng dout("end of strip unit %p\n", page); 9161d3576fdSSage Weil unlock_page(page); 9171d3576fdSSage Weil break; 9181d3576fdSSage Weil } 9191d3576fdSSage Weil if (PageWriteback(page)) { 9200713e5f2SYan, Zheng if (wbc->sync_mode == WB_SYNC_NONE) { 9211d3576fdSSage Weil dout("%p under writeback\n", page); 9221d3576fdSSage Weil unlock_page(page); 9230713e5f2SYan, Zheng continue; 9240713e5f2SYan, Zheng } 9250713e5f2SYan, Zheng dout("waiting on writeback %p\n", page); 9260713e5f2SYan, Zheng wait_on_page_writeback(page); 9271d3576fdSSage Weil } 9281d3576fdSSage Weil 9291d3576fdSSage Weil if (!clear_page_dirty_for_io(page)) { 9301d3576fdSSage Weil dout("%p !clear_page_dirty_for_io\n", page); 9311d3576fdSSage Weil unlock_page(page); 9320713e5f2SYan, Zheng continue; 9331d3576fdSSage Weil } 9341d3576fdSSage Weil 935e5975c7cSAlex Elder /* 936e5975c7cSAlex Elder * We have something to write. If this is 937e5975c7cSAlex Elder * the first locked page this time through, 9385b64640cSYan, Zheng * calculate max possinle write size and 9395b64640cSYan, Zheng * allocate a page array 940e5975c7cSAlex Elder */ 9411d3576fdSSage Weil if (locked_pages == 0) { 9425b64640cSYan, Zheng u64 objnum; 9435b64640cSYan, Zheng u64 objoff; 944dccbf080SIlya Dryomov u32 xlen; 9455b64640cSYan, Zheng 9461d3576fdSSage Weil /* prepare async write request */ 9476285bc23SAlex Elder offset = (u64)page_offset(page); 948dccbf080SIlya Dryomov ceph_calc_file_object_mapping(&ci->i_layout, 949dccbf080SIlya Dryomov offset, wsize, 9505b64640cSYan, Zheng &objnum, &objoff, 951dccbf080SIlya Dryomov &xlen); 952dccbf080SIlya Dryomov len = xlen; 9538c71897bSHenry C Chang 9543fb99d48SYanhu Cao num_ops = 1; 9555b64640cSYan, Zheng strip_unit_end = page->index + 95609cbfeafSKirill A. Shutemov ((len - 1) >> PAGE_SHIFT); 957715e4cd4SYan, Zheng 9585b64640cSYan, Zheng BUG_ON(pages); 95988486957SAlex Elder max_pages = calc_pages_for(0, (u64)len); 9606da2ec56SKees Cook pages = kmalloc_array(max_pages, 9616da2ec56SKees Cook sizeof(*pages), 962fc2744aaSYan, Zheng GFP_NOFS); 96388486957SAlex Elder if (!pages) { 96488486957SAlex Elder pool = fsc->wb_pagevec_pool; 96588486957SAlex Elder pages = mempool_alloc(pool, GFP_NOFS); 966e5975c7cSAlex Elder BUG_ON(!pages); 96788486957SAlex Elder } 9685b64640cSYan, Zheng 9695b64640cSYan, Zheng len = 0; 9705b64640cSYan, Zheng } else if (page->index != 97109cbfeafSKirill A. Shutemov (offset + len) >> PAGE_SHIFT) { 9725b64640cSYan, Zheng if (num_ops >= (pool ? CEPH_OSD_SLAB_OPS : 9735b64640cSYan, Zheng CEPH_OSD_MAX_OPS)) { 9745b64640cSYan, Zheng redirty_page_for_writepage(wbc, page); 9755b64640cSYan, Zheng unlock_page(page); 9765b64640cSYan, Zheng break; 9775b64640cSYan, Zheng } 9785b64640cSYan, Zheng 9795b64640cSYan, Zheng num_ops++; 9805b64640cSYan, Zheng offset = (u64)page_offset(page); 9815b64640cSYan, Zheng len = 0; 9821d3576fdSSage Weil } 9831d3576fdSSage Weil 9841d3576fdSSage Weil /* note position of first page in pvec */ 9851d3576fdSSage Weil dout("%p will write page %p idx %lu\n", 9861d3576fdSSage Weil inode, page, page->index); 9872baba250SYehuda Sadeh 9885b64640cSYan, Zheng if (atomic_long_inc_return(&fsc->writeback_count) > 9895b64640cSYan, Zheng CONGESTION_ON_THRESH( 9903d14c5d2SYehuda Sadeh fsc->mount_options->congestion_kb)) { 99109dc9fc2SJan Kara set_bdi_congested(inode_to_bdi(inode), 992213c99eeSSage Weil BLK_RW_ASYNC); 9932baba250SYehuda Sadeh } 9942baba250SYehuda Sadeh 9950713e5f2SYan, Zheng 9960713e5f2SYan, Zheng pages[locked_pages++] = page; 9970713e5f2SYan, Zheng pvec.pages[i] = NULL; 9980713e5f2SYan, Zheng 99909cbfeafSKirill A. Shutemov len += PAGE_SIZE; 10001d3576fdSSage Weil } 10011d3576fdSSage Weil 10021d3576fdSSage Weil /* did we get anything? */ 10031d3576fdSSage Weil if (!locked_pages) 10041d3576fdSSage Weil goto release_pvec_pages; 10051d3576fdSSage Weil if (i) { 10060713e5f2SYan, Zheng unsigned j, n = 0; 10070713e5f2SYan, Zheng /* shift unused page to beginning of pvec */ 10080713e5f2SYan, Zheng for (j = 0; j < pvec_pages; j++) { 10090713e5f2SYan, Zheng if (!pvec.pages[j]) 10100713e5f2SYan, Zheng continue; 10110713e5f2SYan, Zheng if (n < j) 10120713e5f2SYan, Zheng pvec.pages[n] = pvec.pages[j]; 10130713e5f2SYan, Zheng n++; 10140713e5f2SYan, Zheng } 10150713e5f2SYan, Zheng pvec.nr = n; 10161d3576fdSSage Weil 10171d3576fdSSage Weil if (pvec_pages && i == pvec_pages && 10181d3576fdSSage Weil locked_pages < max_pages) { 10191d3576fdSSage Weil dout("reached end pvec, trying for more\n"); 10200713e5f2SYan, Zheng pagevec_release(&pvec); 10211d3576fdSSage Weil goto get_more_pages; 10221d3576fdSSage Weil } 10231d3576fdSSage Weil } 10241d3576fdSSage Weil 10255b64640cSYan, Zheng new_request: 1026e5975c7cSAlex Elder offset = page_offset(pages[0]); 10275b64640cSYan, Zheng len = wsize; 10285b64640cSYan, Zheng 10295b64640cSYan, Zheng req = ceph_osdc_new_request(&fsc->client->osdc, 10305b64640cSYan, Zheng &ci->i_layout, vino, 10315b64640cSYan, Zheng offset, &len, 0, num_ops, 10321f934b00SYan, Zheng CEPH_OSD_OP_WRITE, CEPH_OSD_FLAG_WRITE, 10331f934b00SYan, Zheng snapc, ceph_wbc.truncate_seq, 10341f934b00SYan, Zheng ceph_wbc.truncate_size, false); 10355b64640cSYan, Zheng if (IS_ERR(req)) { 10365b64640cSYan, Zheng req = ceph_osdc_new_request(&fsc->client->osdc, 10375b64640cSYan, Zheng &ci->i_layout, vino, 10385b64640cSYan, Zheng offset, &len, 0, 10395b64640cSYan, Zheng min(num_ops, 10405b64640cSYan, Zheng CEPH_OSD_SLAB_OPS), 10415b64640cSYan, Zheng CEPH_OSD_OP_WRITE, 104254ea0046SIlya Dryomov CEPH_OSD_FLAG_WRITE, 10431f934b00SYan, Zheng snapc, ceph_wbc.truncate_seq, 10441f934b00SYan, Zheng ceph_wbc.truncate_size, true); 10455b64640cSYan, Zheng BUG_ON(IS_ERR(req)); 10465b64640cSYan, Zheng } 10475b64640cSYan, Zheng BUG_ON(len < page_offset(pages[locked_pages - 1]) + 104809cbfeafSKirill A. Shutemov PAGE_SIZE - offset); 10495b64640cSYan, Zheng 10505b64640cSYan, Zheng req->r_callback = writepages_finish; 10515b64640cSYan, Zheng req->r_inode = inode; 10525b64640cSYan, Zheng 10535b64640cSYan, Zheng /* Format the osd request message and submit the write */ 10545b64640cSYan, Zheng len = 0; 10555b64640cSYan, Zheng data_pages = pages; 10565b64640cSYan, Zheng op_idx = 0; 10575b64640cSYan, Zheng for (i = 0; i < locked_pages; i++) { 10585b64640cSYan, Zheng u64 cur_offset = page_offset(pages[i]); 10595b64640cSYan, Zheng if (offset + len != cur_offset) { 10603fb99d48SYanhu Cao if (op_idx + 1 == req->r_num_ops) 10615b64640cSYan, Zheng break; 10625b64640cSYan, Zheng osd_req_op_extent_dup_last(req, op_idx, 10635b64640cSYan, Zheng cur_offset - offset); 10645b64640cSYan, Zheng dout("writepages got pages at %llu~%llu\n", 10655b64640cSYan, Zheng offset, len); 10665b64640cSYan, Zheng osd_req_op_extent_osd_data_pages(req, op_idx, 10675b64640cSYan, Zheng data_pages, len, 0, 10685b64640cSYan, Zheng !!pool, false); 10695b64640cSYan, Zheng osd_req_op_extent_update(req, op_idx, len); 10705b64640cSYan, Zheng 10715b64640cSYan, Zheng len = 0; 10725b64640cSYan, Zheng offset = cur_offset; 10735b64640cSYan, Zheng data_pages = pages + i; 10745b64640cSYan, Zheng op_idx++; 10755b64640cSYan, Zheng } 10765b64640cSYan, Zheng 10775b64640cSYan, Zheng set_page_writeback(pages[i]); 107809cbfeafSKirill A. Shutemov len += PAGE_SIZE; 10795b64640cSYan, Zheng } 10805b64640cSYan, Zheng 10811f934b00SYan, Zheng if (ceph_wbc.size_stable) { 10821f934b00SYan, Zheng len = min(len, ceph_wbc.i_size - offset); 10835b64640cSYan, Zheng } else if (i == locked_pages) { 1084e1966b49SYan, Zheng /* writepages_finish() clears writeback pages 1085e1966b49SYan, Zheng * according to the data length, so make sure 1086e1966b49SYan, Zheng * data length covers all locked pages */ 108709cbfeafSKirill A. Shutemov u64 min_len = len + 1 - PAGE_SIZE; 10881f934b00SYan, Zheng len = get_writepages_data_length(inode, pages[i - 1], 10891f934b00SYan, Zheng offset); 10905b64640cSYan, Zheng len = max(len, min_len); 1091e1966b49SYan, Zheng } 10925b64640cSYan, Zheng dout("writepages got pages at %llu~%llu\n", offset, len); 10931d3576fdSSage Weil 10945b64640cSYan, Zheng osd_req_op_extent_osd_data_pages(req, op_idx, data_pages, len, 10955b64640cSYan, Zheng 0, !!pool, false); 10965b64640cSYan, Zheng osd_req_op_extent_update(req, op_idx, len); 1097e5975c7cSAlex Elder 10985b64640cSYan, Zheng BUG_ON(op_idx + 1 != req->r_num_ops); 10995b64640cSYan, Zheng 1100e5975c7cSAlex Elder pool = NULL; 11015b64640cSYan, Zheng if (i < locked_pages) { 11025b64640cSYan, Zheng BUG_ON(num_ops <= req->r_num_ops); 11035b64640cSYan, Zheng num_ops -= req->r_num_ops; 11045b64640cSYan, Zheng locked_pages -= i; 1105e5975c7cSAlex Elder 11065b64640cSYan, Zheng /* allocate new pages array for next request */ 11075b64640cSYan, Zheng data_pages = pages; 11086da2ec56SKees Cook pages = kmalloc_array(locked_pages, sizeof(*pages), 11095b64640cSYan, Zheng GFP_NOFS); 11105b64640cSYan, Zheng if (!pages) { 11115b64640cSYan, Zheng pool = fsc->wb_pagevec_pool; 11125b64640cSYan, Zheng pages = mempool_alloc(pool, GFP_NOFS); 11135b64640cSYan, Zheng BUG_ON(!pages); 11145b64640cSYan, Zheng } 11155b64640cSYan, Zheng memcpy(pages, data_pages + i, 11165b64640cSYan, Zheng locked_pages * sizeof(*pages)); 11175b64640cSYan, Zheng memset(data_pages + i, 0, 11185b64640cSYan, Zheng locked_pages * sizeof(*pages)); 11195b64640cSYan, Zheng } else { 11205b64640cSYan, Zheng BUG_ON(num_ops != req->r_num_ops); 11215b64640cSYan, Zheng index = pages[i - 1]->index + 1; 11225b64640cSYan, Zheng /* request message now owns the pages array */ 11235b64640cSYan, Zheng pages = NULL; 11245b64640cSYan, Zheng } 1125e5975c7cSAlex Elder 1126fac02ddfSArnd Bergmann req->r_mtime = inode->i_mtime; 11279d6fcb08SSage Weil rc = ceph_osdc_start_request(&fsc->client->osdc, req, true); 11289d6fcb08SSage Weil BUG_ON(rc); 11291d3576fdSSage Weil req = NULL; 11301d3576fdSSage Weil 11315b64640cSYan, Zheng wbc->nr_to_write -= i; 11325b64640cSYan, Zheng if (pages) 11335b64640cSYan, Zheng goto new_request; 11345b64640cSYan, Zheng 11352a2d927eSYan, Zheng /* 11362a2d927eSYan, Zheng * We stop writing back only if we are not doing 11372a2d927eSYan, Zheng * integrity sync. In case of integrity sync we have to 11382a2d927eSYan, Zheng * keep going until we have written all the pages 11392a2d927eSYan, Zheng * we tagged for writeback prior to entering this loop. 11402a2d927eSYan, Zheng */ 11412a2d927eSYan, Zheng if (wbc->nr_to_write <= 0 && wbc->sync_mode == WB_SYNC_NONE) 1142af9cc401SYan, Zheng done = true; 11431d3576fdSSage Weil 11441d3576fdSSage Weil release_pvec_pages: 11451d3576fdSSage Weil dout("pagevec_release on %d pages (%p)\n", (int)pvec.nr, 11461d3576fdSSage Weil pvec.nr ? pvec.pages[0] : NULL); 11471d3576fdSSage Weil pagevec_release(&pvec); 11481d3576fdSSage Weil } 11491d3576fdSSage Weil 11501d3576fdSSage Weil if (should_loop && !done) { 11511d3576fdSSage Weil /* more to do; loop back to beginning of file */ 11521d3576fdSSage Weil dout("writepages looping back to beginning of file\n"); 11532a2d927eSYan, Zheng end = start_index - 1; /* OK even when start_index == 0 */ 1154f275635eSYan, Zheng 1155f275635eSYan, Zheng /* to write dirty pages associated with next snapc, 1156f275635eSYan, Zheng * we need to wait until current writes complete */ 1157f275635eSYan, Zheng if (wbc->sync_mode != WB_SYNC_NONE && 1158f275635eSYan, Zheng start_index == 0 && /* all dirty pages were checked */ 1159f275635eSYan, Zheng !ceph_wbc.head_snapc) { 1160f275635eSYan, Zheng struct page *page; 1161f275635eSYan, Zheng unsigned i, nr; 1162f275635eSYan, Zheng index = 0; 1163f275635eSYan, Zheng while ((index <= end) && 1164f275635eSYan, Zheng (nr = pagevec_lookup_tag(&pvec, mapping, &index, 116567fd707fSJan Kara PAGECACHE_TAG_WRITEBACK))) { 1166f275635eSYan, Zheng for (i = 0; i < nr; i++) { 1167f275635eSYan, Zheng page = pvec.pages[i]; 1168f275635eSYan, Zheng if (page_snap_context(page) != snapc) 1169f275635eSYan, Zheng continue; 1170f275635eSYan, Zheng wait_on_page_writeback(page); 1171f275635eSYan, Zheng } 1172f275635eSYan, Zheng pagevec_release(&pvec); 1173f275635eSYan, Zheng cond_resched(); 1174f275635eSYan, Zheng } 1175f275635eSYan, Zheng } 1176f275635eSYan, Zheng 11772a2d927eSYan, Zheng start_index = 0; 11781d3576fdSSage Weil index = 0; 11791d3576fdSSage Weil goto retry; 11801d3576fdSSage Weil } 11811d3576fdSSage Weil 11821d3576fdSSage Weil if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0)) 11831d3576fdSSage Weil mapping->writeback_index = index; 11841d3576fdSSage Weil 11851d3576fdSSage Weil out: 11861d3576fdSSage Weil ceph_osdc_put_request(req); 11872a2d927eSYan, Zheng ceph_put_snap_context(last_snapc); 11882a2d927eSYan, Zheng dout("writepages dend - startone, rc = %d\n", rc); 11891d3576fdSSage Weil return rc; 11901d3576fdSSage Weil } 11911d3576fdSSage Weil 11921d3576fdSSage Weil 11931d3576fdSSage Weil 11941d3576fdSSage Weil /* 11951d3576fdSSage Weil * See if a given @snapc is either writeable, or already written. 11961d3576fdSSage Weil */ 11971d3576fdSSage Weil static int context_is_writeable_or_written(struct inode *inode, 11981d3576fdSSage Weil struct ceph_snap_context *snapc) 11991d3576fdSSage Weil { 120005455e11SYan, Zheng struct ceph_snap_context *oldest = get_oldest_context(inode, NULL, NULL); 12016298a337SSage Weil int ret = !oldest || snapc->seq <= oldest->seq; 12026298a337SSage Weil 12036298a337SSage Weil ceph_put_snap_context(oldest); 12046298a337SSage Weil return ret; 12051d3576fdSSage Weil } 12061d3576fdSSage Weil 12071d3576fdSSage Weil /* 12081d3576fdSSage Weil * We are only allowed to write into/dirty the page if the page is 12091d3576fdSSage Weil * clean, or already dirty within the same snap context. 12108f883c24SSage Weil * 12118f883c24SSage Weil * called with page locked. 12128f883c24SSage Weil * return success with page locked, 12138f883c24SSage Weil * or any failure (incl -EAGAIN) with page unlocked. 12141d3576fdSSage Weil */ 12154af6b225SYehuda Sadeh static int ceph_update_writeable_page(struct file *file, 12164af6b225SYehuda Sadeh loff_t pos, unsigned len, 12174af6b225SYehuda Sadeh struct page *page) 12181d3576fdSSage Weil { 1219496ad9aaSAl Viro struct inode *inode = file_inode(file); 12206c93df5dSYan, Zheng struct ceph_fs_client *fsc = ceph_inode_to_client(inode); 12211d3576fdSSage Weil struct ceph_inode_info *ci = ceph_inode(inode); 122209cbfeafSKirill A. Shutemov loff_t page_off = pos & PAGE_MASK; 122309cbfeafSKirill A. Shutemov int pos_in_page = pos & ~PAGE_MASK; 12241d3576fdSSage Weil int end_in_page = pos_in_page + len; 12251d3576fdSSage Weil loff_t i_size; 12261d3576fdSSage Weil int r; 122780e755feSSage Weil struct ceph_snap_context *snapc, *oldest; 12281d3576fdSSage Weil 122952953d55SSeraphime Kirkovski if (READ_ONCE(fsc->mount_state) == CEPH_MOUNT_SHUTDOWN) { 12306c93df5dSYan, Zheng dout(" page %p forced umount\n", page); 12316c93df5dSYan, Zheng unlock_page(page); 12326c93df5dSYan, Zheng return -EIO; 12336c93df5dSYan, Zheng } 12346c93df5dSYan, Zheng 12351d3576fdSSage Weil retry_locked: 12361d3576fdSSage Weil /* writepages currently holds page lock, but if we change that later, */ 12371d3576fdSSage Weil wait_on_page_writeback(page); 12381d3576fdSSage Weil 123961600ef8SYan, Zheng snapc = page_snap_context(page); 124080e755feSSage Weil if (snapc && snapc != ci->i_head_snapc) { 12411d3576fdSSage Weil /* 12421d3576fdSSage Weil * this page is already dirty in another (older) snap 12431d3576fdSSage Weil * context! is it writeable now? 12441d3576fdSSage Weil */ 124505455e11SYan, Zheng oldest = get_oldest_context(inode, NULL, NULL); 124680e755feSSage Weil if (snapc->seq > oldest->seq) { 12476298a337SSage Weil ceph_put_snap_context(oldest); 12481d3576fdSSage Weil dout(" page %p snapc %p not current or oldest\n", 12496298a337SSage Weil page, snapc); 12501d3576fdSSage Weil /* 12511d3576fdSSage Weil * queue for writeback, and wait for snapc to 12521d3576fdSSage Weil * be writeable or written 12531d3576fdSSage Weil */ 12546298a337SSage Weil snapc = ceph_get_snap_context(snapc); 12551d3576fdSSage Weil unlock_page(page); 12563c6f6b79SSage Weil ceph_queue_writeback(inode); 1257a78bbd4bSYan, Zheng r = wait_event_killable(ci->i_cap_wq, 12581d3576fdSSage Weil context_is_writeable_or_written(inode, snapc)); 12591d3576fdSSage Weil ceph_put_snap_context(snapc); 12608f883c24SSage Weil if (r == -ERESTARTSYS) 12618f883c24SSage Weil return r; 12624af6b225SYehuda Sadeh return -EAGAIN; 12631d3576fdSSage Weil } 12646298a337SSage Weil ceph_put_snap_context(oldest); 12651d3576fdSSage Weil 12661d3576fdSSage Weil /* yay, writeable, do it now (without dropping page lock) */ 12671d3576fdSSage Weil dout(" page %p snapc %p not current, but oldest\n", 12681d3576fdSSage Weil page, snapc); 12691d3576fdSSage Weil if (!clear_page_dirty_for_io(page)) 12701d3576fdSSage Weil goto retry_locked; 12711d3576fdSSage Weil r = writepage_nounlock(page, NULL); 12721d3576fdSSage Weil if (r < 0) 1273dd2bc473SYan, Zheng goto fail_unlock; 12741d3576fdSSage Weil goto retry_locked; 12751d3576fdSSage Weil } 12761d3576fdSSage Weil 12771d3576fdSSage Weil if (PageUptodate(page)) { 12781d3576fdSSage Weil dout(" page %p already uptodate\n", page); 12791d3576fdSSage Weil return 0; 12801d3576fdSSage Weil } 12811d3576fdSSage Weil 12821d3576fdSSage Weil /* full page? */ 128309cbfeafSKirill A. Shutemov if (pos_in_page == 0 && len == PAGE_SIZE) 12841d3576fdSSage Weil return 0; 12851d3576fdSSage Weil 12861d3576fdSSage Weil /* past end of file? */ 128799c88e69SYan, Zheng i_size = i_size_read(inode); 12881d3576fdSSage Weil 12891d3576fdSSage Weil if (page_off >= i_size || 12901d3576fdSSage Weil (pos_in_page == 0 && (pos+len) >= i_size && 129109cbfeafSKirill A. Shutemov end_in_page - pos_in_page != PAGE_SIZE)) { 12921d3576fdSSage Weil dout(" zeroing %p 0 - %d and %d - %d\n", 129309cbfeafSKirill A. Shutemov page, pos_in_page, end_in_page, (int)PAGE_SIZE); 12941d3576fdSSage Weil zero_user_segments(page, 12951d3576fdSSage Weil 0, pos_in_page, 129609cbfeafSKirill A. Shutemov end_in_page, PAGE_SIZE); 12971d3576fdSSage Weil return 0; 12981d3576fdSSage Weil } 12991d3576fdSSage Weil 13001d3576fdSSage Weil /* we need to read it. */ 1301dd2bc473SYan, Zheng r = ceph_do_readpage(file, page); 1302dd2bc473SYan, Zheng if (r < 0) { 1303dd2bc473SYan, Zheng if (r == -EINPROGRESS) 1304dd2bc473SYan, Zheng return -EAGAIN; 1305dd2bc473SYan, Zheng goto fail_unlock; 1306dd2bc473SYan, Zheng } 13071d3576fdSSage Weil goto retry_locked; 1308dd2bc473SYan, Zheng fail_unlock: 13091d3576fdSSage Weil unlock_page(page); 13101d3576fdSSage Weil return r; 13111d3576fdSSage Weil } 13121d3576fdSSage Weil 13131d3576fdSSage Weil /* 13144af6b225SYehuda Sadeh * We are only allowed to write into/dirty the page if the page is 13154af6b225SYehuda Sadeh * clean, or already dirty within the same snap context. 13164af6b225SYehuda Sadeh */ 13174af6b225SYehuda Sadeh static int ceph_write_begin(struct file *file, struct address_space *mapping, 13184af6b225SYehuda Sadeh loff_t pos, unsigned len, unsigned flags, 13194af6b225SYehuda Sadeh struct page **pagep, void **fsdata) 13204af6b225SYehuda Sadeh { 1321496ad9aaSAl Viro struct inode *inode = file_inode(file); 13224af6b225SYehuda Sadeh struct page *page; 132309cbfeafSKirill A. Shutemov pgoff_t index = pos >> PAGE_SHIFT; 13247971bd92SSage Weil int r; 13254af6b225SYehuda Sadeh 13264af6b225SYehuda Sadeh do { 13274af6b225SYehuda Sadeh /* get a page */ 13284af6b225SYehuda Sadeh page = grab_cache_page_write_begin(mapping, index, 0); 13297971bd92SSage Weil if (!page) 13307971bd92SSage Weil return -ENOMEM; 13314af6b225SYehuda Sadeh 13324af6b225SYehuda Sadeh dout("write_begin file %p inode %p page %p %d~%d\n", file, 13334af6b225SYehuda Sadeh inode, page, (int)pos, (int)len); 13344af6b225SYehuda Sadeh 13354af6b225SYehuda Sadeh r = ceph_update_writeable_page(file, pos, len, page); 1336c1d00b2dSTaesoo Kim if (r < 0) 133709cbfeafSKirill A. Shutemov put_page(page); 1338c1d00b2dSTaesoo Kim else 1339c1d00b2dSTaesoo Kim *pagep = page; 13404af6b225SYehuda Sadeh } while (r == -EAGAIN); 13414af6b225SYehuda Sadeh 13424af6b225SYehuda Sadeh return r; 13434af6b225SYehuda Sadeh } 13444af6b225SYehuda Sadeh 13454af6b225SYehuda Sadeh /* 13461d3576fdSSage Weil * we don't do anything in here that simple_write_end doesn't do 13475dda377cSYan, Zheng * except adjust dirty page accounting 13481d3576fdSSage Weil */ 13491d3576fdSSage Weil static int ceph_write_end(struct file *file, struct address_space *mapping, 13501d3576fdSSage Weil loff_t pos, unsigned len, unsigned copied, 13511d3576fdSSage Weil struct page *page, void *fsdata) 13521d3576fdSSage Weil { 1353496ad9aaSAl Viro struct inode *inode = file_inode(file); 1354efb0ca76SYan, Zheng bool check_cap = false; 13551d3576fdSSage Weil 13561d3576fdSSage Weil dout("write_end file %p inode %p page %p %d~%d (%d)\n", file, 13571d3576fdSSage Weil inode, page, (int)pos, (int)copied, (int)len); 13581d3576fdSSage Weil 13591d3576fdSSage Weil /* zero the stale part of the page if we did a short copy */ 1360b9de313cSAl Viro if (!PageUptodate(page)) { 1361b9de313cSAl Viro if (copied < len) { 1362b9de313cSAl Viro copied = 0; 1363b9de313cSAl Viro goto out; 1364b9de313cSAl Viro } 1365b9de313cSAl Viro SetPageUptodate(page); 1366b9de313cSAl Viro } 13671d3576fdSSage Weil 13681d3576fdSSage Weil /* did file size increase? */ 136999c88e69SYan, Zheng if (pos+copied > i_size_read(inode)) 13701d3576fdSSage Weil check_cap = ceph_inode_set_size(inode, pos+copied); 13711d3576fdSSage Weil 13721d3576fdSSage Weil set_page_dirty(page); 13731d3576fdSSage Weil 1374b9de313cSAl Viro out: 13751d3576fdSSage Weil unlock_page(page); 137609cbfeafSKirill A. Shutemov put_page(page); 13771d3576fdSSage Weil 13781d3576fdSSage Weil if (check_cap) 13791d3576fdSSage Weil ceph_check_caps(ceph_inode(inode), CHECK_CAPS_AUTHONLY, NULL); 13801d3576fdSSage Weil 13811d3576fdSSage Weil return copied; 13821d3576fdSSage Weil } 13831d3576fdSSage Weil 13841d3576fdSSage Weil /* 13851d3576fdSSage Weil * we set .direct_IO to indicate direct io is supported, but since we 13861d3576fdSSage Weil * intercept O_DIRECT reads and writes early, this function should 13871d3576fdSSage Weil * never get called. 13881d3576fdSSage Weil */ 1389c8b8e32dSChristoph Hellwig static ssize_t ceph_direct_io(struct kiocb *iocb, struct iov_iter *iter) 13901d3576fdSSage Weil { 13911d3576fdSSage Weil WARN_ON(1); 13921d3576fdSSage Weil return -EINVAL; 13931d3576fdSSage Weil } 13941d3576fdSSage Weil 13951d3576fdSSage Weil const struct address_space_operations ceph_aops = { 13961d3576fdSSage Weil .readpage = ceph_readpage, 13971d3576fdSSage Weil .readpages = ceph_readpages, 13981d3576fdSSage Weil .writepage = ceph_writepage, 13991d3576fdSSage Weil .writepages = ceph_writepages_start, 14001d3576fdSSage Weil .write_begin = ceph_write_begin, 14011d3576fdSSage Weil .write_end = ceph_write_end, 14021d3576fdSSage Weil .set_page_dirty = ceph_set_page_dirty, 14031d3576fdSSage Weil .invalidatepage = ceph_invalidatepage, 14041d3576fdSSage Weil .releasepage = ceph_releasepage, 14051d3576fdSSage Weil .direct_IO = ceph_direct_io, 14061d3576fdSSage Weil }; 14071d3576fdSSage Weil 14084f7e89f6SYan, Zheng static void ceph_block_sigs(sigset_t *oldset) 14094f7e89f6SYan, Zheng { 14104f7e89f6SYan, Zheng sigset_t mask; 14114f7e89f6SYan, Zheng siginitsetinv(&mask, sigmask(SIGKILL)); 14124f7e89f6SYan, Zheng sigprocmask(SIG_BLOCK, &mask, oldset); 14134f7e89f6SYan, Zheng } 14144f7e89f6SYan, Zheng 14154f7e89f6SYan, Zheng static void ceph_restore_sigs(sigset_t *oldset) 14164f7e89f6SYan, Zheng { 14174f7e89f6SYan, Zheng sigprocmask(SIG_SETMASK, oldset, NULL); 14184f7e89f6SYan, Zheng } 14191d3576fdSSage Weil 14201d3576fdSSage Weil /* 14211d3576fdSSage Weil * vm ops 14221d3576fdSSage Weil */ 142324499847SSouptick Joarder static vm_fault_t ceph_filemap_fault(struct vm_fault *vmf) 142461f68816SYan, Zheng { 142511bac800SDave Jiang struct vm_area_struct *vma = vmf->vma; 142661f68816SYan, Zheng struct inode *inode = file_inode(vma->vm_file); 142761f68816SYan, Zheng struct ceph_inode_info *ci = ceph_inode(inode); 142861f68816SYan, Zheng struct ceph_file_info *fi = vma->vm_file->private_data; 14293738daa6SYan, Zheng struct page *pinned_page = NULL; 143009cbfeafSKirill A. Shutemov loff_t off = vmf->pgoff << PAGE_SHIFT; 143124499847SSouptick Joarder int want, got, err; 14324f7e89f6SYan, Zheng sigset_t oldset; 143324499847SSouptick Joarder vm_fault_t ret = VM_FAULT_SIGBUS; 14344f7e89f6SYan, Zheng 14354f7e89f6SYan, Zheng ceph_block_sigs(&oldset); 143661f68816SYan, Zheng 143761f68816SYan, Zheng dout("filemap_fault %p %llx.%llx %llu~%zd trying to get caps\n", 143809cbfeafSKirill A. Shutemov inode, ceph_vinop(inode), off, (size_t)PAGE_SIZE); 143961f68816SYan, Zheng if (fi->fmode & CEPH_FILE_MODE_LAZY) 144061f68816SYan, Zheng want = CEPH_CAP_FILE_CACHE | CEPH_CAP_FILE_LAZYIO; 144161f68816SYan, Zheng else 144261f68816SYan, Zheng want = CEPH_CAP_FILE_CACHE; 14434f7e89f6SYan, Zheng 144461f68816SYan, Zheng got = 0; 14455e3ded1bSYan, Zheng err = ceph_get_caps(vma->vm_file, CEPH_CAP_FILE_RD, want, -1, 14465e3ded1bSYan, Zheng &got, &pinned_page); 144724499847SSouptick Joarder if (err < 0) 14484f7e89f6SYan, Zheng goto out_restore; 14496ce026e4SYan, Zheng 145061f68816SYan, Zheng dout("filemap_fault %p %llu~%zd got cap refs on %s\n", 145109cbfeafSKirill A. Shutemov inode, off, (size_t)PAGE_SIZE, ceph_cap_string(got)); 145261f68816SYan, Zheng 145383701246SYan, Zheng if ((got & (CEPH_CAP_FILE_CACHE | CEPH_CAP_FILE_LAZYIO)) || 14542b1ac852SYan, Zheng ci->i_inline_version == CEPH_INLINE_NONE) { 14555d988308SYan, Zheng CEPH_DEFINE_RW_CONTEXT(rw_ctx, got); 14565d988308SYan, Zheng ceph_add_rw_context(fi, &rw_ctx); 145711bac800SDave Jiang ret = filemap_fault(vmf); 14585d988308SYan, Zheng ceph_del_rw_context(fi, &rw_ctx); 145924499847SSouptick Joarder dout("filemap_fault %p %llu~%zd drop cap refs %s ret %x\n", 146024499847SSouptick Joarder inode, off, (size_t)PAGE_SIZE, 146124499847SSouptick Joarder ceph_cap_string(got), ret); 14622b1ac852SYan, Zheng } else 146324499847SSouptick Joarder err = -EAGAIN; 146461f68816SYan, Zheng 14653738daa6SYan, Zheng if (pinned_page) 146609cbfeafSKirill A. Shutemov put_page(pinned_page); 146761f68816SYan, Zheng ceph_put_cap_refs(ci, got); 146861f68816SYan, Zheng 146924499847SSouptick Joarder if (err != -EAGAIN) 14704f7e89f6SYan, Zheng goto out_restore; 147183701246SYan, Zheng 147283701246SYan, Zheng /* read inline data */ 147309cbfeafSKirill A. Shutemov if (off >= PAGE_SIZE) { 147483701246SYan, Zheng /* does not support inline data > PAGE_SIZE */ 147583701246SYan, Zheng ret = VM_FAULT_SIGBUS; 147683701246SYan, Zheng } else { 147783701246SYan, Zheng struct address_space *mapping = inode->i_mapping; 147883701246SYan, Zheng struct page *page = find_or_create_page(mapping, 0, 1479c62d2555SMichal Hocko mapping_gfp_constraint(mapping, 1480c62d2555SMichal Hocko ~__GFP_FS)); 148183701246SYan, Zheng if (!page) { 148283701246SYan, Zheng ret = VM_FAULT_OOM; 14834f7e89f6SYan, Zheng goto out_inline; 148483701246SYan, Zheng } 148524499847SSouptick Joarder err = __ceph_do_getattr(inode, page, 148683701246SYan, Zheng CEPH_STAT_CAP_INLINE_DATA, true); 148724499847SSouptick Joarder if (err < 0 || off >= i_size_read(inode)) { 148883701246SYan, Zheng unlock_page(page); 148909cbfeafSKirill A. Shutemov put_page(page); 1490c64a2b05SSouptick Joarder ret = vmf_error(err); 14914f7e89f6SYan, Zheng goto out_inline; 149283701246SYan, Zheng } 149324499847SSouptick Joarder if (err < PAGE_SIZE) 149424499847SSouptick Joarder zero_user_segment(page, err, PAGE_SIZE); 149583701246SYan, Zheng else 149683701246SYan, Zheng flush_dcache_page(page); 149783701246SYan, Zheng SetPageUptodate(page); 149883701246SYan, Zheng vmf->page = page; 149983701246SYan, Zheng ret = VM_FAULT_MAJOR | VM_FAULT_LOCKED; 15004f7e89f6SYan, Zheng out_inline: 150124499847SSouptick Joarder dout("filemap_fault %p %llu~%zd read inline data ret %x\n", 150209cbfeafSKirill A. Shutemov inode, off, (size_t)PAGE_SIZE, ret); 15034f7e89f6SYan, Zheng } 15044f7e89f6SYan, Zheng out_restore: 15054f7e89f6SYan, Zheng ceph_restore_sigs(&oldset); 150624499847SSouptick Joarder if (err < 0) 150724499847SSouptick Joarder ret = vmf_error(err); 15086ce026e4SYan, Zheng 150961f68816SYan, Zheng return ret; 151061f68816SYan, Zheng } 15111d3576fdSSage Weil 15121d3576fdSSage Weil /* 15131d3576fdSSage Weil * Reuse write_begin here for simplicity. 15141d3576fdSSage Weil */ 151524499847SSouptick Joarder static vm_fault_t ceph_page_mkwrite(struct vm_fault *vmf) 15161d3576fdSSage Weil { 151711bac800SDave Jiang struct vm_area_struct *vma = vmf->vma; 1518496ad9aaSAl Viro struct inode *inode = file_inode(vma->vm_file); 151961f68816SYan, Zheng struct ceph_inode_info *ci = ceph_inode(inode); 152061f68816SYan, Zheng struct ceph_file_info *fi = vma->vm_file->private_data; 1521f66fd9f0SYan, Zheng struct ceph_cap_flush *prealloc_cf; 152261f68816SYan, Zheng struct page *page = vmf->page; 15236285bc23SAlex Elder loff_t off = page_offset(page); 152461f68816SYan, Zheng loff_t size = i_size_read(inode); 152561f68816SYan, Zheng size_t len; 152624499847SSouptick Joarder int want, got, err; 15274f7e89f6SYan, Zheng sigset_t oldset; 152824499847SSouptick Joarder vm_fault_t ret = VM_FAULT_SIGBUS; 15291d3576fdSSage Weil 1530f66fd9f0SYan, Zheng prealloc_cf = ceph_alloc_cap_flush(); 1531f66fd9f0SYan, Zheng if (!prealloc_cf) 15326ce026e4SYan, Zheng return VM_FAULT_OOM; 1533f66fd9f0SYan, Zheng 1534249c1df5SJeff Layton sb_start_pagefault(inode->i_sb); 15354f7e89f6SYan, Zheng ceph_block_sigs(&oldset); 15361d3576fdSSage Weil 153728127bddSYan, Zheng if (ci->i_inline_version != CEPH_INLINE_NONE) { 153828127bddSYan, Zheng struct page *locked_page = NULL; 153928127bddSYan, Zheng if (off == 0) { 154028127bddSYan, Zheng lock_page(page); 154128127bddSYan, Zheng locked_page = page; 154228127bddSYan, Zheng } 154324499847SSouptick Joarder err = ceph_uninline_data(vma->vm_file, locked_page); 154428127bddSYan, Zheng if (locked_page) 154528127bddSYan, Zheng unlock_page(locked_page); 154624499847SSouptick Joarder if (err < 0) 1547f66fd9f0SYan, Zheng goto out_free; 1548f66fd9f0SYan, Zheng } 154928127bddSYan, Zheng 155009cbfeafSKirill A. Shutemov if (off + PAGE_SIZE <= size) 155109cbfeafSKirill A. Shutemov len = PAGE_SIZE; 15521d3576fdSSage Weil else 155309cbfeafSKirill A. Shutemov len = size & ~PAGE_MASK; 15541d3576fdSSage Weil 155561f68816SYan, Zheng dout("page_mkwrite %p %llx.%llx %llu~%zd getting caps i_size %llu\n", 155661f68816SYan, Zheng inode, ceph_vinop(inode), off, len, size); 155761f68816SYan, Zheng if (fi->fmode & CEPH_FILE_MODE_LAZY) 155861f68816SYan, Zheng want = CEPH_CAP_FILE_BUFFER | CEPH_CAP_FILE_LAZYIO; 155961f68816SYan, Zheng else 156061f68816SYan, Zheng want = CEPH_CAP_FILE_BUFFER; 15614f7e89f6SYan, Zheng 156261f68816SYan, Zheng got = 0; 15635e3ded1bSYan, Zheng err = ceph_get_caps(vma->vm_file, CEPH_CAP_FILE_WR, want, off + len, 15643738daa6SYan, Zheng &got, NULL); 156524499847SSouptick Joarder if (err < 0) 1566f66fd9f0SYan, Zheng goto out_free; 15676ce026e4SYan, Zheng 156861f68816SYan, Zheng dout("page_mkwrite %p %llu~%zd got cap refs on %s\n", 156961f68816SYan, Zheng inode, off, len, ceph_cap_string(got)); 157061f68816SYan, Zheng 157161f68816SYan, Zheng /* Update time before taking page lock */ 157261f68816SYan, Zheng file_update_time(vma->vm_file); 15735c308356SJeff Layton inode_inc_iversion_raw(inode); 15744af6b225SYehuda Sadeh 1575f0b33df5SYan, Zheng do { 15764af6b225SYehuda Sadeh lock_page(page); 15774af6b225SYehuda Sadeh 15786ce026e4SYan, Zheng if ((off > size) || (page->mapping != inode->i_mapping)) { 1579f9cac5acSYan, Zheng unlock_page(page); 15806ce026e4SYan, Zheng ret = VM_FAULT_NOPAGE; 1581f0b33df5SYan, Zheng break; 1582f9cac5acSYan, Zheng } 15834af6b225SYehuda Sadeh 158424499847SSouptick Joarder err = ceph_update_writeable_page(vma->vm_file, off, len, page); 158524499847SSouptick Joarder if (err >= 0) { 15864af6b225SYehuda Sadeh /* success. we'll keep the page locked. */ 15871d3576fdSSage Weil set_page_dirty(page); 15881d3576fdSSage Weil ret = VM_FAULT_LOCKED; 15891d3576fdSSage Weil } 159024499847SSouptick Joarder } while (err == -EAGAIN); 1591f0b33df5SYan, Zheng 159228127bddSYan, Zheng if (ret == VM_FAULT_LOCKED || 159328127bddSYan, Zheng ci->i_inline_version != CEPH_INLINE_NONE) { 159461f68816SYan, Zheng int dirty; 159561f68816SYan, Zheng spin_lock(&ci->i_ceph_lock); 159628127bddSYan, Zheng ci->i_inline_version = CEPH_INLINE_NONE; 1597f66fd9f0SYan, Zheng dirty = __ceph_mark_dirty_caps(ci, CEPH_CAP_FILE_WR, 1598f66fd9f0SYan, Zheng &prealloc_cf); 159961f68816SYan, Zheng spin_unlock(&ci->i_ceph_lock); 160061f68816SYan, Zheng if (dirty) 160161f68816SYan, Zheng __mark_inode_dirty(inode, dirty); 160261f68816SYan, Zheng } 160361f68816SYan, Zheng 160424499847SSouptick Joarder dout("page_mkwrite %p %llu~%zd dropping cap refs on %s ret %x\n", 160561f68816SYan, Zheng inode, off, len, ceph_cap_string(got), ret); 160661f68816SYan, Zheng ceph_put_cap_refs(ci, got); 1607f66fd9f0SYan, Zheng out_free: 16084f7e89f6SYan, Zheng ceph_restore_sigs(&oldset); 1609249c1df5SJeff Layton sb_end_pagefault(inode->i_sb); 1610f66fd9f0SYan, Zheng ceph_free_cap_flush(prealloc_cf); 161124499847SSouptick Joarder if (err < 0) 161224499847SSouptick Joarder ret = vmf_error(err); 16131d3576fdSSage Weil return ret; 16141d3576fdSSage Weil } 16151d3576fdSSage Weil 161631c542a1SYan, Zheng void ceph_fill_inline_data(struct inode *inode, struct page *locked_page, 161731c542a1SYan, Zheng char *data, size_t len) 161831c542a1SYan, Zheng { 161931c542a1SYan, Zheng struct address_space *mapping = inode->i_mapping; 162031c542a1SYan, Zheng struct page *page; 162131c542a1SYan, Zheng 162231c542a1SYan, Zheng if (locked_page) { 162331c542a1SYan, Zheng page = locked_page; 162431c542a1SYan, Zheng } else { 162531c542a1SYan, Zheng if (i_size_read(inode) == 0) 162631c542a1SYan, Zheng return; 162731c542a1SYan, Zheng page = find_or_create_page(mapping, 0, 1628c62d2555SMichal Hocko mapping_gfp_constraint(mapping, 1629c62d2555SMichal Hocko ~__GFP_FS)); 163031c542a1SYan, Zheng if (!page) 163131c542a1SYan, Zheng return; 163231c542a1SYan, Zheng if (PageUptodate(page)) { 163331c542a1SYan, Zheng unlock_page(page); 163409cbfeafSKirill A. Shutemov put_page(page); 163531c542a1SYan, Zheng return; 163631c542a1SYan, Zheng } 163731c542a1SYan, Zheng } 163831c542a1SYan, Zheng 16390668ff52SIlya Dryomov dout("fill_inline_data %p %llx.%llx len %zu locked_page %p\n", 164031c542a1SYan, Zheng inode, ceph_vinop(inode), len, locked_page); 164131c542a1SYan, Zheng 164231c542a1SYan, Zheng if (len > 0) { 164331c542a1SYan, Zheng void *kaddr = kmap_atomic(page); 164431c542a1SYan, Zheng memcpy(kaddr, data, len); 164531c542a1SYan, Zheng kunmap_atomic(kaddr); 164631c542a1SYan, Zheng } 164731c542a1SYan, Zheng 164831c542a1SYan, Zheng if (page != locked_page) { 164909cbfeafSKirill A. Shutemov if (len < PAGE_SIZE) 165009cbfeafSKirill A. Shutemov zero_user_segment(page, len, PAGE_SIZE); 165131c542a1SYan, Zheng else 165231c542a1SYan, Zheng flush_dcache_page(page); 165331c542a1SYan, Zheng 165431c542a1SYan, Zheng SetPageUptodate(page); 165531c542a1SYan, Zheng unlock_page(page); 165609cbfeafSKirill A. Shutemov put_page(page); 165731c542a1SYan, Zheng } 165831c542a1SYan, Zheng } 165931c542a1SYan, Zheng 166028127bddSYan, Zheng int ceph_uninline_data(struct file *filp, struct page *locked_page) 166128127bddSYan, Zheng { 166228127bddSYan, Zheng struct inode *inode = file_inode(filp); 166328127bddSYan, Zheng struct ceph_inode_info *ci = ceph_inode(inode); 166428127bddSYan, Zheng struct ceph_fs_client *fsc = ceph_inode_to_client(inode); 166528127bddSYan, Zheng struct ceph_osd_request *req; 166628127bddSYan, Zheng struct page *page = NULL; 166728127bddSYan, Zheng u64 len, inline_version; 166828127bddSYan, Zheng int err = 0; 166928127bddSYan, Zheng bool from_pagecache = false; 167028127bddSYan, Zheng 167128127bddSYan, Zheng spin_lock(&ci->i_ceph_lock); 167228127bddSYan, Zheng inline_version = ci->i_inline_version; 167328127bddSYan, Zheng spin_unlock(&ci->i_ceph_lock); 167428127bddSYan, Zheng 167528127bddSYan, Zheng dout("uninline_data %p %llx.%llx inline_version %llu\n", 167628127bddSYan, Zheng inode, ceph_vinop(inode), inline_version); 167728127bddSYan, Zheng 167828127bddSYan, Zheng if (inline_version == 1 || /* initial version, no data */ 167928127bddSYan, Zheng inline_version == CEPH_INLINE_NONE) 168028127bddSYan, Zheng goto out; 168128127bddSYan, Zheng 168228127bddSYan, Zheng if (locked_page) { 168328127bddSYan, Zheng page = locked_page; 168428127bddSYan, Zheng WARN_ON(!PageUptodate(page)); 168528127bddSYan, Zheng } else if (ceph_caps_issued(ci) & 168628127bddSYan, Zheng (CEPH_CAP_FILE_CACHE|CEPH_CAP_FILE_LAZYIO)) { 168728127bddSYan, Zheng page = find_get_page(inode->i_mapping, 0); 168828127bddSYan, Zheng if (page) { 168928127bddSYan, Zheng if (PageUptodate(page)) { 169028127bddSYan, Zheng from_pagecache = true; 169128127bddSYan, Zheng lock_page(page); 169228127bddSYan, Zheng } else { 169309cbfeafSKirill A. Shutemov put_page(page); 169428127bddSYan, Zheng page = NULL; 169528127bddSYan, Zheng } 169628127bddSYan, Zheng } 169728127bddSYan, Zheng } 169828127bddSYan, Zheng 169928127bddSYan, Zheng if (page) { 170028127bddSYan, Zheng len = i_size_read(inode); 170109cbfeafSKirill A. Shutemov if (len > PAGE_SIZE) 170209cbfeafSKirill A. Shutemov len = PAGE_SIZE; 170328127bddSYan, Zheng } else { 170428127bddSYan, Zheng page = __page_cache_alloc(GFP_NOFS); 170528127bddSYan, Zheng if (!page) { 170628127bddSYan, Zheng err = -ENOMEM; 170728127bddSYan, Zheng goto out; 170828127bddSYan, Zheng } 170928127bddSYan, Zheng err = __ceph_do_getattr(inode, page, 171028127bddSYan, Zheng CEPH_STAT_CAP_INLINE_DATA, true); 171128127bddSYan, Zheng if (err < 0) { 171228127bddSYan, Zheng /* no inline data */ 171328127bddSYan, Zheng if (err == -ENODATA) 171428127bddSYan, Zheng err = 0; 171528127bddSYan, Zheng goto out; 171628127bddSYan, Zheng } 171728127bddSYan, Zheng len = err; 171828127bddSYan, Zheng } 171928127bddSYan, Zheng 172028127bddSYan, Zheng req = ceph_osdc_new_request(&fsc->client->osdc, &ci->i_layout, 172128127bddSYan, Zheng ceph_vino(inode), 0, &len, 0, 1, 172254ea0046SIlya Dryomov CEPH_OSD_OP_CREATE, CEPH_OSD_FLAG_WRITE, 172334b759b4SIlya Dryomov NULL, 0, 0, false); 172428127bddSYan, Zheng if (IS_ERR(req)) { 172528127bddSYan, Zheng err = PTR_ERR(req); 172628127bddSYan, Zheng goto out; 172728127bddSYan, Zheng } 172828127bddSYan, Zheng 1729fac02ddfSArnd Bergmann req->r_mtime = inode->i_mtime; 173028127bddSYan, Zheng err = ceph_osdc_start_request(&fsc->client->osdc, req, false); 173128127bddSYan, Zheng if (!err) 173228127bddSYan, Zheng err = ceph_osdc_wait_request(&fsc->client->osdc, req); 173328127bddSYan, Zheng ceph_osdc_put_request(req); 173428127bddSYan, Zheng if (err < 0) 173528127bddSYan, Zheng goto out; 173628127bddSYan, Zheng 173728127bddSYan, Zheng req = ceph_osdc_new_request(&fsc->client->osdc, &ci->i_layout, 173828127bddSYan, Zheng ceph_vino(inode), 0, &len, 1, 3, 173954ea0046SIlya Dryomov CEPH_OSD_OP_WRITE, CEPH_OSD_FLAG_WRITE, 174034b759b4SIlya Dryomov NULL, ci->i_truncate_seq, 174134b759b4SIlya Dryomov ci->i_truncate_size, false); 174228127bddSYan, Zheng if (IS_ERR(req)) { 174328127bddSYan, Zheng err = PTR_ERR(req); 174428127bddSYan, Zheng goto out; 174528127bddSYan, Zheng } 174628127bddSYan, Zheng 174728127bddSYan, Zheng osd_req_op_extent_osd_data_pages(req, 1, &page, len, 0, false, false); 174828127bddSYan, Zheng 1749ec137c10SYan, Zheng { 1750ec137c10SYan, Zheng __le64 xattr_buf = cpu_to_le64(inline_version); 175128127bddSYan, Zheng err = osd_req_op_xattr_init(req, 0, CEPH_OSD_OP_CMPXATTR, 1752ec137c10SYan, Zheng "inline_version", &xattr_buf, 1753ec137c10SYan, Zheng sizeof(xattr_buf), 175428127bddSYan, Zheng CEPH_OSD_CMPXATTR_OP_GT, 175528127bddSYan, Zheng CEPH_OSD_CMPXATTR_MODE_U64); 175628127bddSYan, Zheng if (err) 175728127bddSYan, Zheng goto out_put; 1758ec137c10SYan, Zheng } 175928127bddSYan, Zheng 1760ec137c10SYan, Zheng { 1761ec137c10SYan, Zheng char xattr_buf[32]; 1762ec137c10SYan, Zheng int xattr_len = snprintf(xattr_buf, sizeof(xattr_buf), 1763ec137c10SYan, Zheng "%llu", inline_version); 176428127bddSYan, Zheng err = osd_req_op_xattr_init(req, 2, CEPH_OSD_OP_SETXATTR, 1765ec137c10SYan, Zheng "inline_version", 1766ec137c10SYan, Zheng xattr_buf, xattr_len, 0, 0); 176728127bddSYan, Zheng if (err) 176828127bddSYan, Zheng goto out_put; 1769ec137c10SYan, Zheng } 177028127bddSYan, Zheng 1771fac02ddfSArnd Bergmann req->r_mtime = inode->i_mtime; 177228127bddSYan, Zheng err = ceph_osdc_start_request(&fsc->client->osdc, req, false); 177328127bddSYan, Zheng if (!err) 177428127bddSYan, Zheng err = ceph_osdc_wait_request(&fsc->client->osdc, req); 177528127bddSYan, Zheng out_put: 177628127bddSYan, Zheng ceph_osdc_put_request(req); 177728127bddSYan, Zheng if (err == -ECANCELED) 177828127bddSYan, Zheng err = 0; 177928127bddSYan, Zheng out: 178028127bddSYan, Zheng if (page && page != locked_page) { 178128127bddSYan, Zheng if (from_pagecache) { 178228127bddSYan, Zheng unlock_page(page); 178309cbfeafSKirill A. Shutemov put_page(page); 178428127bddSYan, Zheng } else 178528127bddSYan, Zheng __free_pages(page, 0); 178628127bddSYan, Zheng } 178728127bddSYan, Zheng 178828127bddSYan, Zheng dout("uninline_data %p %llx.%llx inline_version %llu = %d\n", 178928127bddSYan, Zheng inode, ceph_vinop(inode), inline_version, err); 179028127bddSYan, Zheng return err; 179128127bddSYan, Zheng } 179228127bddSYan, Zheng 17937cbea8dcSKirill A. Shutemov static const struct vm_operations_struct ceph_vmops = { 179461f68816SYan, Zheng .fault = ceph_filemap_fault, 17951d3576fdSSage Weil .page_mkwrite = ceph_page_mkwrite, 17961d3576fdSSage Weil }; 17971d3576fdSSage Weil 17981d3576fdSSage Weil int ceph_mmap(struct file *file, struct vm_area_struct *vma) 17991d3576fdSSage Weil { 18001d3576fdSSage Weil struct address_space *mapping = file->f_mapping; 18011d3576fdSSage Weil 18021d3576fdSSage Weil if (!mapping->a_ops->readpage) 18031d3576fdSSage Weil return -ENOEXEC; 18041d3576fdSSage Weil file_accessed(file); 18051d3576fdSSage Weil vma->vm_ops = &ceph_vmops; 18061d3576fdSSage Weil return 0; 18071d3576fdSSage Weil } 180810183a69SYan, Zheng 180910183a69SYan, Zheng enum { 181010183a69SYan, Zheng POOL_READ = 1, 181110183a69SYan, Zheng POOL_WRITE = 2, 181210183a69SYan, Zheng }; 181310183a69SYan, Zheng 1814779fe0fbSYan, Zheng static int __ceph_pool_perm_get(struct ceph_inode_info *ci, 1815779fe0fbSYan, Zheng s64 pool, struct ceph_string *pool_ns) 181610183a69SYan, Zheng { 181710183a69SYan, Zheng struct ceph_fs_client *fsc = ceph_inode_to_client(&ci->vfs_inode); 181810183a69SYan, Zheng struct ceph_mds_client *mdsc = fsc->mdsc; 181910183a69SYan, Zheng struct ceph_osd_request *rd_req = NULL, *wr_req = NULL; 182010183a69SYan, Zheng struct rb_node **p, *parent; 182110183a69SYan, Zheng struct ceph_pool_perm *perm; 182210183a69SYan, Zheng struct page **pages; 1823779fe0fbSYan, Zheng size_t pool_ns_len; 182410183a69SYan, Zheng int err = 0, err2 = 0, have = 0; 182510183a69SYan, Zheng 182610183a69SYan, Zheng down_read(&mdsc->pool_perm_rwsem); 182710183a69SYan, Zheng p = &mdsc->pool_perm_tree.rb_node; 182810183a69SYan, Zheng while (*p) { 182910183a69SYan, Zheng perm = rb_entry(*p, struct ceph_pool_perm, node); 183010183a69SYan, Zheng if (pool < perm->pool) 183110183a69SYan, Zheng p = &(*p)->rb_left; 183210183a69SYan, Zheng else if (pool > perm->pool) 183310183a69SYan, Zheng p = &(*p)->rb_right; 183410183a69SYan, Zheng else { 1835779fe0fbSYan, Zheng int ret = ceph_compare_string(pool_ns, 1836779fe0fbSYan, Zheng perm->pool_ns, 1837779fe0fbSYan, Zheng perm->pool_ns_len); 1838779fe0fbSYan, Zheng if (ret < 0) 1839779fe0fbSYan, Zheng p = &(*p)->rb_left; 1840779fe0fbSYan, Zheng else if (ret > 0) 1841779fe0fbSYan, Zheng p = &(*p)->rb_right; 1842779fe0fbSYan, Zheng else { 184310183a69SYan, Zheng have = perm->perm; 184410183a69SYan, Zheng break; 184510183a69SYan, Zheng } 184610183a69SYan, Zheng } 1847779fe0fbSYan, Zheng } 184810183a69SYan, Zheng up_read(&mdsc->pool_perm_rwsem); 184910183a69SYan, Zheng if (*p) 185010183a69SYan, Zheng goto out; 185110183a69SYan, Zheng 1852779fe0fbSYan, Zheng if (pool_ns) 1853779fe0fbSYan, Zheng dout("__ceph_pool_perm_get pool %lld ns %.*s no perm cached\n", 1854779fe0fbSYan, Zheng pool, (int)pool_ns->len, pool_ns->str); 1855779fe0fbSYan, Zheng else 18567627151eSYan, Zheng dout("__ceph_pool_perm_get pool %lld no perm cached\n", pool); 185710183a69SYan, Zheng 185810183a69SYan, Zheng down_write(&mdsc->pool_perm_rwsem); 1859779fe0fbSYan, Zheng p = &mdsc->pool_perm_tree.rb_node; 186010183a69SYan, Zheng parent = NULL; 186110183a69SYan, Zheng while (*p) { 186210183a69SYan, Zheng parent = *p; 186310183a69SYan, Zheng perm = rb_entry(parent, struct ceph_pool_perm, node); 186410183a69SYan, Zheng if (pool < perm->pool) 186510183a69SYan, Zheng p = &(*p)->rb_left; 186610183a69SYan, Zheng else if (pool > perm->pool) 186710183a69SYan, Zheng p = &(*p)->rb_right; 186810183a69SYan, Zheng else { 1869779fe0fbSYan, Zheng int ret = ceph_compare_string(pool_ns, 1870779fe0fbSYan, Zheng perm->pool_ns, 1871779fe0fbSYan, Zheng perm->pool_ns_len); 1872779fe0fbSYan, Zheng if (ret < 0) 1873779fe0fbSYan, Zheng p = &(*p)->rb_left; 1874779fe0fbSYan, Zheng else if (ret > 0) 1875779fe0fbSYan, Zheng p = &(*p)->rb_right; 1876779fe0fbSYan, Zheng else { 187710183a69SYan, Zheng have = perm->perm; 187810183a69SYan, Zheng break; 187910183a69SYan, Zheng } 188010183a69SYan, Zheng } 1881779fe0fbSYan, Zheng } 188210183a69SYan, Zheng if (*p) { 188310183a69SYan, Zheng up_write(&mdsc->pool_perm_rwsem); 188410183a69SYan, Zheng goto out; 188510183a69SYan, Zheng } 188610183a69SYan, Zheng 188734b759b4SIlya Dryomov rd_req = ceph_osdc_alloc_request(&fsc->client->osdc, NULL, 188810183a69SYan, Zheng 1, false, GFP_NOFS); 188910183a69SYan, Zheng if (!rd_req) { 189010183a69SYan, Zheng err = -ENOMEM; 189110183a69SYan, Zheng goto out_unlock; 189210183a69SYan, Zheng } 189310183a69SYan, Zheng 189410183a69SYan, Zheng rd_req->r_flags = CEPH_OSD_FLAG_READ; 189510183a69SYan, Zheng osd_req_op_init(rd_req, 0, CEPH_OSD_OP_STAT, 0); 189610183a69SYan, Zheng rd_req->r_base_oloc.pool = pool; 1897779fe0fbSYan, Zheng if (pool_ns) 1898779fe0fbSYan, Zheng rd_req->r_base_oloc.pool_ns = ceph_get_string(pool_ns); 1899d30291b9SIlya Dryomov ceph_oid_printf(&rd_req->r_base_oid, "%llx.00000000", ci->i_vino.ino); 190010183a69SYan, Zheng 190113d1ad16SIlya Dryomov err = ceph_osdc_alloc_messages(rd_req, GFP_NOFS); 190213d1ad16SIlya Dryomov if (err) 190313d1ad16SIlya Dryomov goto out_unlock; 190410183a69SYan, Zheng 190534b759b4SIlya Dryomov wr_req = ceph_osdc_alloc_request(&fsc->client->osdc, NULL, 190610183a69SYan, Zheng 1, false, GFP_NOFS); 190710183a69SYan, Zheng if (!wr_req) { 190810183a69SYan, Zheng err = -ENOMEM; 190910183a69SYan, Zheng goto out_unlock; 191010183a69SYan, Zheng } 191110183a69SYan, Zheng 191254ea0046SIlya Dryomov wr_req->r_flags = CEPH_OSD_FLAG_WRITE; 191310183a69SYan, Zheng osd_req_op_init(wr_req, 0, CEPH_OSD_OP_CREATE, CEPH_OSD_OP_FLAG_EXCL); 191463244fa1SIlya Dryomov ceph_oloc_copy(&wr_req->r_base_oloc, &rd_req->r_base_oloc); 1915d30291b9SIlya Dryomov ceph_oid_copy(&wr_req->r_base_oid, &rd_req->r_base_oid); 191610183a69SYan, Zheng 191713d1ad16SIlya Dryomov err = ceph_osdc_alloc_messages(wr_req, GFP_NOFS); 191813d1ad16SIlya Dryomov if (err) 191913d1ad16SIlya Dryomov goto out_unlock; 192010183a69SYan, Zheng 192110183a69SYan, Zheng /* one page should be large enough for STAT data */ 192210183a69SYan, Zheng pages = ceph_alloc_page_vector(1, GFP_KERNEL); 192310183a69SYan, Zheng if (IS_ERR(pages)) { 192410183a69SYan, Zheng err = PTR_ERR(pages); 192510183a69SYan, Zheng goto out_unlock; 192610183a69SYan, Zheng } 192710183a69SYan, Zheng 192810183a69SYan, Zheng osd_req_op_raw_data_in_pages(rd_req, 0, pages, PAGE_SIZE, 192910183a69SYan, Zheng 0, false, true); 193010183a69SYan, Zheng err = ceph_osdc_start_request(&fsc->client->osdc, rd_req, false); 193110183a69SYan, Zheng 1932fac02ddfSArnd Bergmann wr_req->r_mtime = ci->vfs_inode.i_mtime; 193310183a69SYan, Zheng err2 = ceph_osdc_start_request(&fsc->client->osdc, wr_req, false); 193410183a69SYan, Zheng 193510183a69SYan, Zheng if (!err) 193610183a69SYan, Zheng err = ceph_osdc_wait_request(&fsc->client->osdc, rd_req); 193710183a69SYan, Zheng if (!err2) 193810183a69SYan, Zheng err2 = ceph_osdc_wait_request(&fsc->client->osdc, wr_req); 193910183a69SYan, Zheng 194010183a69SYan, Zheng if (err >= 0 || err == -ENOENT) 194110183a69SYan, Zheng have |= POOL_READ; 1942131d7eb4SYan, Zheng else if (err != -EPERM) { 1943131d7eb4SYan, Zheng if (err == -EBLACKLISTED) 1944131d7eb4SYan, Zheng fsc->blacklisted = true; 194510183a69SYan, Zheng goto out_unlock; 1946131d7eb4SYan, Zheng } 194710183a69SYan, Zheng 194810183a69SYan, Zheng if (err2 == 0 || err2 == -EEXIST) 194910183a69SYan, Zheng have |= POOL_WRITE; 195010183a69SYan, Zheng else if (err2 != -EPERM) { 1951131d7eb4SYan, Zheng if (err2 == -EBLACKLISTED) 1952131d7eb4SYan, Zheng fsc->blacklisted = true; 195310183a69SYan, Zheng err = err2; 195410183a69SYan, Zheng goto out_unlock; 195510183a69SYan, Zheng } 195610183a69SYan, Zheng 1957779fe0fbSYan, Zheng pool_ns_len = pool_ns ? pool_ns->len : 0; 1958779fe0fbSYan, Zheng perm = kmalloc(sizeof(*perm) + pool_ns_len + 1, GFP_NOFS); 195910183a69SYan, Zheng if (!perm) { 196010183a69SYan, Zheng err = -ENOMEM; 196110183a69SYan, Zheng goto out_unlock; 196210183a69SYan, Zheng } 196310183a69SYan, Zheng 196410183a69SYan, Zheng perm->pool = pool; 196510183a69SYan, Zheng perm->perm = have; 1966779fe0fbSYan, Zheng perm->pool_ns_len = pool_ns_len; 1967779fe0fbSYan, Zheng if (pool_ns_len > 0) 1968779fe0fbSYan, Zheng memcpy(perm->pool_ns, pool_ns->str, pool_ns_len); 1969779fe0fbSYan, Zheng perm->pool_ns[pool_ns_len] = 0; 1970779fe0fbSYan, Zheng 197110183a69SYan, Zheng rb_link_node(&perm->node, parent, p); 197210183a69SYan, Zheng rb_insert_color(&perm->node, &mdsc->pool_perm_tree); 197310183a69SYan, Zheng err = 0; 197410183a69SYan, Zheng out_unlock: 197510183a69SYan, Zheng up_write(&mdsc->pool_perm_rwsem); 197610183a69SYan, Zheng 197710183a69SYan, Zheng ceph_osdc_put_request(rd_req); 197810183a69SYan, Zheng ceph_osdc_put_request(wr_req); 197910183a69SYan, Zheng out: 198010183a69SYan, Zheng if (!err) 198110183a69SYan, Zheng err = have; 1982779fe0fbSYan, Zheng if (pool_ns) 1983779fe0fbSYan, Zheng dout("__ceph_pool_perm_get pool %lld ns %.*s result = %d\n", 1984779fe0fbSYan, Zheng pool, (int)pool_ns->len, pool_ns->str, err); 1985779fe0fbSYan, Zheng else 19867627151eSYan, Zheng dout("__ceph_pool_perm_get pool %lld result = %d\n", pool, err); 198710183a69SYan, Zheng return err; 198810183a69SYan, Zheng } 198910183a69SYan, Zheng 19905e3ded1bSYan, Zheng int ceph_pool_perm_check(struct inode *inode, int need) 199110183a69SYan, Zheng { 19925e3ded1bSYan, Zheng struct ceph_inode_info *ci = ceph_inode(inode); 1993779fe0fbSYan, Zheng struct ceph_string *pool_ns; 19945e3ded1bSYan, Zheng s64 pool; 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 20065e3ded1bSYan, Zheng if (ceph_test_mount_opt(ceph_inode_to_client(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