13d14c5d2SYehuda Sadeh #include <linux/ceph/ceph_debug.h> 21d3576fdSSage Weil 31d3576fdSSage Weil #include <linux/backing-dev.h> 41d3576fdSSage Weil #include <linux/fs.h> 51d3576fdSSage Weil #include <linux/mm.h> 61d3576fdSSage Weil #include <linux/pagemap.h> 71d3576fdSSage Weil #include <linux/writeback.h> /* generic_writepages */ 85a0e3ad6STejun Heo #include <linux/slab.h> 91d3576fdSSage Weil #include <linux/pagevec.h> 101d3576fdSSage Weil #include <linux/task_io_accounting_ops.h> 11f361bf4aSIngo Molnar #include <linux/signal.h> 121d3576fdSSage Weil 131d3576fdSSage Weil #include "super.h" 143d14c5d2SYehuda Sadeh #include "mds_client.h" 1599ccbd22SMilosz Tanski #include "cache.h" 163d14c5d2SYehuda Sadeh #include <linux/ceph/osd_client.h> 171d3576fdSSage Weil 181d3576fdSSage Weil /* 191d3576fdSSage Weil * Ceph address space ops. 201d3576fdSSage Weil * 211d3576fdSSage Weil * There are a few funny things going on here. 221d3576fdSSage Weil * 231d3576fdSSage Weil * The page->private field is used to reference a struct 241d3576fdSSage Weil * ceph_snap_context for _every_ dirty page. This indicates which 251d3576fdSSage Weil * snapshot the page was logically dirtied in, and thus which snap 261d3576fdSSage Weil * context needs to be associated with the osd write during writeback. 271d3576fdSSage Weil * 281d3576fdSSage Weil * Similarly, struct ceph_inode_info maintains a set of counters to 2925985edcSLucas De Marchi * count dirty pages on the inode. In the absence of snapshots, 301d3576fdSSage Weil * i_wrbuffer_ref == i_wrbuffer_ref_head == the dirty page count. 311d3576fdSSage Weil * 321d3576fdSSage Weil * When a snapshot is taken (that is, when the client receives 331d3576fdSSage Weil * notification that a snapshot was taken), each inode with caps and 341d3576fdSSage Weil * with dirty pages (dirty pages implies there is a cap) gets a new 351d3576fdSSage Weil * ceph_cap_snap in the i_cap_snaps list (which is sorted in ascending 361d3576fdSSage Weil * order, new snaps go to the tail). The i_wrbuffer_ref_head count is 371d3576fdSSage Weil * moved to capsnap->dirty. (Unless a sync write is currently in 381d3576fdSSage Weil * progress. In that case, the capsnap is said to be "pending", new 391d3576fdSSage Weil * writes cannot start, and the capsnap isn't "finalized" until the 401d3576fdSSage Weil * write completes (or fails) and a final size/mtime for the inode for 411d3576fdSSage Weil * that snap can be settled upon.) i_wrbuffer_ref_head is reset to 0. 421d3576fdSSage Weil * 431d3576fdSSage Weil * On writeback, we must submit writes to the osd IN SNAP ORDER. So, 441d3576fdSSage Weil * we look for the first capsnap in i_cap_snaps and write out pages in 451d3576fdSSage Weil * that snap context _only_. Then we move on to the next capsnap, 461d3576fdSSage Weil * eventually reaching the "live" or "head" context (i.e., pages that 471d3576fdSSage Weil * are not yet snapped) and are writing the most recently dirtied 481d3576fdSSage Weil * pages. 491d3576fdSSage Weil * 501d3576fdSSage Weil * Invalidate and so forth must take care to ensure the dirty page 511d3576fdSSage Weil * accounting is preserved. 521d3576fdSSage Weil */ 531d3576fdSSage Weil 542baba250SYehuda Sadeh #define CONGESTION_ON_THRESH(congestion_kb) (congestion_kb >> (PAGE_SHIFT-10)) 552baba250SYehuda Sadeh #define CONGESTION_OFF_THRESH(congestion_kb) \ 562baba250SYehuda Sadeh (CONGESTION_ON_THRESH(congestion_kb) - \ 572baba250SYehuda Sadeh (CONGESTION_ON_THRESH(congestion_kb) >> 2)) 582baba250SYehuda Sadeh 5961600ef8SYan, Zheng static inline struct ceph_snap_context *page_snap_context(struct page *page) 6061600ef8SYan, Zheng { 6161600ef8SYan, Zheng if (PagePrivate(page)) 6261600ef8SYan, Zheng return (void *)page->private; 6361600ef8SYan, Zheng return NULL; 6461600ef8SYan, Zheng } 651d3576fdSSage Weil 661d3576fdSSage Weil /* 671d3576fdSSage Weil * Dirty a page. Optimistically adjust accounting, on the assumption 681d3576fdSSage Weil * that we won't race with invalidate. If we do, readjust. 691d3576fdSSage Weil */ 701d3576fdSSage Weil static int ceph_set_page_dirty(struct page *page) 711d3576fdSSage Weil { 721d3576fdSSage Weil struct address_space *mapping = page->mapping; 731d3576fdSSage Weil struct inode *inode; 741d3576fdSSage Weil struct ceph_inode_info *ci; 751d3576fdSSage Weil struct ceph_snap_context *snapc; 767d6e1f54SSha Zhengju int ret; 771d3576fdSSage Weil 781d3576fdSSage Weil if (unlikely(!mapping)) 791d3576fdSSage Weil return !TestSetPageDirty(page); 801d3576fdSSage Weil 817d6e1f54SSha Zhengju if (PageDirty(page)) { 821d3576fdSSage Weil dout("%p set_page_dirty %p idx %lu -- already dirty\n", 831d3576fdSSage Weil mapping->host, page, page->index); 847d6e1f54SSha Zhengju BUG_ON(!PagePrivate(page)); 851d3576fdSSage Weil return 0; 861d3576fdSSage Weil } 871d3576fdSSage Weil 881d3576fdSSage Weil inode = mapping->host; 891d3576fdSSage Weil ci = ceph_inode(inode); 901d3576fdSSage Weil 911d3576fdSSage Weil /* dirty the head */ 92be655596SSage Weil spin_lock(&ci->i_ceph_lock); 935dda377cSYan, Zheng BUG_ON(ci->i_wr_ref == 0); // caller should hold Fw reference 945dda377cSYan, Zheng if (__ceph_have_pending_cap_snap(ci)) { 955dda377cSYan, Zheng struct ceph_cap_snap *capsnap = 965dda377cSYan, Zheng list_last_entry(&ci->i_cap_snaps, 975dda377cSYan, Zheng struct ceph_cap_snap, 985dda377cSYan, Zheng ci_item); 995dda377cSYan, Zheng snapc = ceph_get_snap_context(capsnap->context); 1005dda377cSYan, Zheng capsnap->dirty_pages++; 1015dda377cSYan, Zheng } else { 1025dda377cSYan, Zheng BUG_ON(!ci->i_head_snapc); 1035dda377cSYan, Zheng snapc = ceph_get_snap_context(ci->i_head_snapc); 1041d3576fdSSage Weil ++ci->i_wrbuffer_ref_head; 1055dda377cSYan, Zheng } 1061d3576fdSSage Weil if (ci->i_wrbuffer_ref == 0) 1070444d76aSDave Chinner ihold(inode); 1081d3576fdSSage Weil ++ci->i_wrbuffer_ref; 1091d3576fdSSage Weil dout("%p set_page_dirty %p idx %lu head %d/%d -> %d/%d " 1101d3576fdSSage Weil "snapc %p seq %lld (%d snaps)\n", 1111d3576fdSSage Weil mapping->host, page, page->index, 1121d3576fdSSage Weil ci->i_wrbuffer_ref-1, ci->i_wrbuffer_ref_head-1, 1131d3576fdSSage Weil ci->i_wrbuffer_ref, ci->i_wrbuffer_ref_head, 1141d3576fdSSage Weil snapc, snapc->seq, snapc->num_snaps); 115be655596SSage Weil spin_unlock(&ci->i_ceph_lock); 1161d3576fdSSage Weil 1171d3576fdSSage Weil /* 1181d3576fdSSage Weil * Reference snap context in page->private. Also set 1191d3576fdSSage Weil * PagePrivate so that we get invalidatepage callback. 1201d3576fdSSage Weil */ 1217d6e1f54SSha Zhengju BUG_ON(PagePrivate(page)); 1221d3576fdSSage Weil page->private = (unsigned long)snapc; 1231d3576fdSSage Weil SetPagePrivate(page); 1241d3576fdSSage Weil 1257d6e1f54SSha Zhengju ret = __set_page_dirty_nobuffers(page); 1267d6e1f54SSha Zhengju WARN_ON(!PageLocked(page)); 1277d6e1f54SSha Zhengju WARN_ON(!page->mapping); 1281d3576fdSSage Weil 1297d6e1f54SSha Zhengju return ret; 1301d3576fdSSage Weil } 1311d3576fdSSage Weil 1321d3576fdSSage Weil /* 1331d3576fdSSage Weil * If we are truncating the full page (i.e. offset == 0), adjust the 1341d3576fdSSage Weil * dirty page counters appropriately. Only called if there is private 1351d3576fdSSage Weil * data on the page. 1361d3576fdSSage Weil */ 137d47992f8SLukas Czerner static void ceph_invalidatepage(struct page *page, unsigned int offset, 138d47992f8SLukas Czerner unsigned int length) 1391d3576fdSSage Weil { 1404ce1e9adSAlexander Beregalov struct inode *inode; 1411d3576fdSSage Weil struct ceph_inode_info *ci; 14261600ef8SYan, Zheng struct ceph_snap_context *snapc = page_snap_context(page); 1431d3576fdSSage Weil 1444ce1e9adSAlexander Beregalov inode = page->mapping->host; 145b150f5c1SMilosz Tanski ci = ceph_inode(inode); 146b150f5c1SMilosz Tanski 14709cbfeafSKirill A. Shutemov if (offset != 0 || length != PAGE_SIZE) { 148b150f5c1SMilosz Tanski dout("%p invalidatepage %p idx %lu partial dirty page %u~%u\n", 149b150f5c1SMilosz Tanski inode, page, page->index, offset, length); 150b150f5c1SMilosz Tanski return; 151b150f5c1SMilosz Tanski } 1524ce1e9adSAlexander Beregalov 15399ccbd22SMilosz Tanski ceph_invalidate_fscache_page(inode, page); 15499ccbd22SMilosz Tanski 155b072d774SYan, Zheng WARN_ON(!PageLocked(page)); 15699ccbd22SMilosz Tanski if (!PagePrivate(page)) 15799ccbd22SMilosz Tanski return; 15899ccbd22SMilosz Tanski 1591d3576fdSSage Weil ClearPageChecked(page); 1601d3576fdSSage Weil 161569d39fcSLukas Czerner dout("%p invalidatepage %p idx %lu full dirty page\n", 162569d39fcSLukas Czerner inode, page, page->index); 163b150f5c1SMilosz Tanski 1641d3576fdSSage Weil ceph_put_wrbuffer_cap_refs(ci, 1, snapc); 1651d3576fdSSage Weil ceph_put_snap_context(snapc); 1661d3576fdSSage Weil page->private = 0; 1671d3576fdSSage Weil ClearPagePrivate(page); 1681d3576fdSSage Weil } 1691d3576fdSSage Weil 1701d3576fdSSage Weil static int ceph_releasepage(struct page *page, gfp_t g) 1711d3576fdSSage Weil { 172e55f1a18SNeilBrown dout("%p releasepage %p idx %lu (%sdirty)\n", page->mapping->host, 173e55f1a18SNeilBrown page, page->index, PageDirty(page) ? "" : "not "); 17499ccbd22SMilosz Tanski 17599ccbd22SMilosz Tanski /* Can we release the page from the cache? */ 17699ccbd22SMilosz Tanski if (!ceph_release_fscache_page(page, g)) 1771d3576fdSSage Weil return 0; 17899ccbd22SMilosz Tanski 17999ccbd22SMilosz Tanski return !PagePrivate(page); 1801d3576fdSSage Weil } 1811d3576fdSSage Weil 1821d3576fdSSage Weil /* 1831d3576fdSSage Weil * read a single page, without unlocking it. 1841d3576fdSSage Weil */ 185dd2bc473SYan, Zheng static int ceph_do_readpage(struct file *filp, struct page *page) 1861d3576fdSSage Weil { 187496ad9aaSAl Viro struct inode *inode = file_inode(filp); 1881d3576fdSSage Weil struct ceph_inode_info *ci = ceph_inode(inode); 1893d14c5d2SYehuda Sadeh struct ceph_osd_client *osdc = 1903d14c5d2SYehuda Sadeh &ceph_inode_to_client(inode)->client->osdc; 1911d3576fdSSage Weil int err = 0; 19283701246SYan, Zheng u64 off = page_offset(page); 19309cbfeafSKirill A. Shutemov u64 len = PAGE_SIZE; 1941d3576fdSSage Weil 19583701246SYan, Zheng if (off >= i_size_read(inode)) { 19609cbfeafSKirill A. Shutemov zero_user_segment(page, 0, PAGE_SIZE); 19783701246SYan, Zheng SetPageUptodate(page); 19883701246SYan, Zheng return 0; 19983701246SYan, Zheng } 20099ccbd22SMilosz Tanski 201fcc02d2aSYan, Zheng if (ci->i_inline_version != CEPH_INLINE_NONE) { 20283701246SYan, Zheng /* 203fcc02d2aSYan, Zheng * Uptodate inline data should have been added 204fcc02d2aSYan, Zheng * into page cache while getting Fcr caps. 20583701246SYan, Zheng */ 206fcc02d2aSYan, Zheng if (off == 0) 20783701246SYan, Zheng return -EINVAL; 20809cbfeafSKirill A. Shutemov zero_user_segment(page, 0, PAGE_SIZE); 209fcc02d2aSYan, Zheng SetPageUptodate(page); 210fcc02d2aSYan, Zheng return 0; 211fcc02d2aSYan, Zheng } 21283701246SYan, Zheng 21383701246SYan, Zheng err = ceph_readpage_from_fscache(inode, page); 21499ccbd22SMilosz Tanski if (err == 0) 215dd2bc473SYan, Zheng return -EINPROGRESS; 21699ccbd22SMilosz Tanski 2171d3576fdSSage Weil dout("readpage inode %p file %p page %p index %lu\n", 2181d3576fdSSage Weil inode, filp, page, page->index); 2191d3576fdSSage Weil err = ceph_osdc_readpages(osdc, ceph_vino(inode), &ci->i_layout, 22083701246SYan, Zheng off, &len, 2211d3576fdSSage Weil ci->i_truncate_seq, ci->i_truncate_size, 222b7495fc2SSage Weil &page, 1, 0); 2231d3576fdSSage Weil if (err == -ENOENT) 2241d3576fdSSage Weil err = 0; 2251d3576fdSSage Weil if (err < 0) { 2261d3576fdSSage Weil SetPageError(page); 22718302805SLi Wang ceph_fscache_readpage_cancel(inode, page); 2281d3576fdSSage Weil goto out; 22923cd573bSZhang Zhen } 23009cbfeafSKirill A. Shutemov if (err < PAGE_SIZE) 2311d3576fdSSage Weil /* zero fill remainder of page */ 23209cbfeafSKirill A. Shutemov zero_user_segment(page, err, PAGE_SIZE); 23323cd573bSZhang Zhen else 23456f91aadSLi Wang flush_dcache_page(page); 2351d3576fdSSage Weil 23623cd573bSZhang Zhen SetPageUptodate(page); 23799ccbd22SMilosz Tanski ceph_readpage_to_fscache(inode, page); 23899ccbd22SMilosz Tanski 2391d3576fdSSage Weil out: 2401d3576fdSSage Weil return err < 0 ? err : 0; 2411d3576fdSSage Weil } 2421d3576fdSSage Weil 2431d3576fdSSage Weil static int ceph_readpage(struct file *filp, struct page *page) 2441d3576fdSSage Weil { 245dd2bc473SYan, Zheng int r = ceph_do_readpage(filp, page); 246dd2bc473SYan, Zheng if (r != -EINPROGRESS) 2471d3576fdSSage Weil unlock_page(page); 248dd2bc473SYan, Zheng else 249dd2bc473SYan, Zheng r = 0; 2501d3576fdSSage Weil return r; 2511d3576fdSSage Weil } 2521d3576fdSSage Weil 2531d3576fdSSage Weil /* 2547c272194SSage Weil * Finish an async read(ahead) op. 2551d3576fdSSage Weil */ 25685e084feSIlya Dryomov static void finish_read(struct ceph_osd_request *req) 2571d3576fdSSage Weil { 2587c272194SSage Weil struct inode *inode = req->r_inode; 25987060c10SAlex Elder struct ceph_osd_data *osd_data; 26085e084feSIlya Dryomov int rc = req->r_result <= 0 ? req->r_result : 0; 26185e084feSIlya Dryomov int bytes = req->r_result >= 0 ? req->r_result : 0; 262e0c59487SAlex Elder int num_pages; 2637c272194SSage Weil int i; 2647c272194SSage Weil 2657c272194SSage Weil dout("finish_read %p req %p rc %d bytes %d\n", inode, req, rc, bytes); 2667c272194SSage Weil 2677c272194SSage Weil /* unlock all pages, zeroing any data we didn't read */ 268406e2c9fSAlex Elder osd_data = osd_req_op_extent_osd_data(req, 0); 26987060c10SAlex Elder BUG_ON(osd_data->type != CEPH_OSD_DATA_TYPE_PAGES); 27087060c10SAlex Elder num_pages = calc_pages_for((u64)osd_data->alignment, 27187060c10SAlex Elder (u64)osd_data->length); 272e0c59487SAlex Elder for (i = 0; i < num_pages; i++) { 27387060c10SAlex Elder struct page *page = osd_data->pages[i]; 2747c272194SSage Weil 275368e3585SYan, Zheng if (rc < 0 && rc != -ENOENT) { 276368e3585SYan, Zheng ceph_fscache_readpage_cancel(inode, page); 277f36132a7SLi Wang goto unlock; 278368e3585SYan, Zheng } 27909cbfeafSKirill A. Shutemov if (bytes < (int)PAGE_SIZE) { 2807c272194SSage Weil /* zero (remainder of) page */ 2817c272194SSage Weil int s = bytes < 0 ? 0 : bytes; 28209cbfeafSKirill A. Shutemov zero_user_segment(page, s, PAGE_SIZE); 2837c272194SSage Weil } 2847c272194SSage Weil dout("finish_read %p uptodate %p idx %lu\n", inode, page, 2857c272194SSage Weil page->index); 2867c272194SSage Weil flush_dcache_page(page); 2877c272194SSage Weil SetPageUptodate(page); 28899ccbd22SMilosz Tanski ceph_readpage_to_fscache(inode, page); 289f36132a7SLi Wang unlock: 2907c272194SSage Weil unlock_page(page); 29109cbfeafSKirill A. Shutemov put_page(page); 29209cbfeafSKirill A. Shutemov bytes -= PAGE_SIZE; 2937c272194SSage Weil } 29487060c10SAlex Elder kfree(osd_data->pages); 2957c272194SSage Weil } 2967c272194SSage Weil 2977c272194SSage Weil /* 2987c272194SSage Weil * start an async read(ahead) operation. return nr_pages we submitted 2997c272194SSage Weil * a read for on success, or negative error code. 3007c272194SSage Weil */ 3010d66a487SSage Weil static int start_read(struct inode *inode, struct list_head *page_list, int max) 3027c272194SSage Weil { 3037c272194SSage Weil struct ceph_osd_client *osdc = 3047c272194SSage Weil &ceph_inode_to_client(inode)->client->osdc; 3057c272194SSage Weil struct ceph_inode_info *ci = ceph_inode(inode); 3067c272194SSage Weil struct page *page = list_entry(page_list->prev, struct page, lru); 307acead002SAlex Elder struct ceph_vino vino; 3087c272194SSage Weil struct ceph_osd_request *req; 3097c272194SSage Weil u64 off; 3107c272194SSage Weil u64 len; 3117c272194SSage Weil int i; 3121d3576fdSSage Weil struct page **pages; 3137c272194SSage Weil pgoff_t next_index; 3147c272194SSage Weil int nr_pages = 0; 3152b1ac852SYan, Zheng int got = 0; 3162b1ac852SYan, Zheng int ret = 0; 3172b1ac852SYan, Zheng 3182b1ac852SYan, Zheng if (!current->journal_info) { 3192b1ac852SYan, Zheng /* caller of readpages does not hold buffer and read caps 3202b1ac852SYan, Zheng * (fadvise, madvise and readahead cases) */ 3212b1ac852SYan, Zheng int want = CEPH_CAP_FILE_CACHE; 3222b1ac852SYan, Zheng ret = ceph_try_get_caps(ci, CEPH_CAP_FILE_RD, want, &got); 3232b1ac852SYan, Zheng if (ret < 0) { 3242b1ac852SYan, Zheng dout("start_read %p, error getting cap\n", inode); 3252b1ac852SYan, Zheng } else if (!(got & want)) { 3262b1ac852SYan, Zheng dout("start_read %p, no cache cap\n", inode); 3272b1ac852SYan, Zheng ret = 0; 3282b1ac852SYan, Zheng } 3292b1ac852SYan, Zheng if (ret <= 0) { 3302b1ac852SYan, Zheng if (got) 3312b1ac852SYan, Zheng ceph_put_cap_refs(ci, got); 3322b1ac852SYan, Zheng while (!list_empty(page_list)) { 3332b1ac852SYan, Zheng page = list_entry(page_list->prev, 3342b1ac852SYan, Zheng struct page, lru); 3352b1ac852SYan, Zheng list_del(&page->lru); 3362b1ac852SYan, Zheng put_page(page); 3372b1ac852SYan, Zheng } 3382b1ac852SYan, Zheng return ret; 3392b1ac852SYan, Zheng } 3402b1ac852SYan, Zheng } 3417c272194SSage Weil 3426285bc23SAlex Elder off = (u64) page_offset(page); 3437c272194SSage Weil 3447c272194SSage Weil /* count pages */ 3457c272194SSage Weil next_index = page->index; 3467c272194SSage Weil list_for_each_entry_reverse(page, page_list, lru) { 3477c272194SSage Weil if (page->index != next_index) 3487c272194SSage Weil break; 3497c272194SSage Weil nr_pages++; 3507c272194SSage Weil next_index++; 3510d66a487SSage Weil if (max && nr_pages == max) 3520d66a487SSage Weil break; 3537c272194SSage Weil } 35409cbfeafSKirill A. Shutemov len = nr_pages << PAGE_SHIFT; 3557c272194SSage Weil dout("start_read %p nr_pages %d is %lld~%lld\n", inode, nr_pages, 3567c272194SSage Weil off, len); 357acead002SAlex Elder vino = ceph_vino(inode); 358acead002SAlex Elder req = ceph_osdc_new_request(osdc, &ci->i_layout, vino, off, &len, 359715e4cd4SYan, Zheng 0, 1, CEPH_OSD_OP_READ, 360acead002SAlex Elder CEPH_OSD_FLAG_READ, NULL, 3617c272194SSage Weil ci->i_truncate_seq, ci->i_truncate_size, 362acead002SAlex Elder false); 3632b1ac852SYan, Zheng if (IS_ERR(req)) { 3642b1ac852SYan, Zheng ret = PTR_ERR(req); 3652b1ac852SYan, Zheng goto out; 3662b1ac852SYan, Zheng } 3671d3576fdSSage Weil 3681d3576fdSSage Weil /* build page vector */ 369cf7b7e14SAlex Elder nr_pages = calc_pages_for(0, len); 370687265e5SYan, Zheng pages = kmalloc(sizeof(*pages) * nr_pages, GFP_KERNEL); 3712b1ac852SYan, Zheng if (!pages) { 3727c272194SSage Weil ret = -ENOMEM; 3732b1ac852SYan, Zheng goto out_put; 3742b1ac852SYan, Zheng } 3757c272194SSage Weil for (i = 0; i < nr_pages; ++i) { 3767c272194SSage Weil page = list_entry(page_list->prev, struct page, lru); 3777c272194SSage Weil BUG_ON(PageLocked(page)); 3787c272194SSage Weil list_del(&page->lru); 3791d3576fdSSage Weil 3807c272194SSage Weil dout("start_read %p adding %p idx %lu\n", inode, page, 3817c272194SSage Weil page->index); 3827c272194SSage Weil if (add_to_page_cache_lru(page, &inode->i_data, page->index, 383687265e5SYan, Zheng GFP_KERNEL)) { 384d4d3aa38SMilosz Tanski ceph_fscache_uncache_page(inode, page); 38509cbfeafSKirill A. Shutemov put_page(page); 3867c272194SSage Weil dout("start_read %p add_to_page_cache failed %p\n", 3877c272194SSage Weil inode, page); 3887c272194SSage Weil nr_pages = i; 3891afe4785SYan, Zheng if (nr_pages > 0) { 3901afe4785SYan, Zheng len = nr_pages << PAGE_SHIFT; 391d641df81SYan, Zheng osd_req_op_extent_update(req, 0, len); 3921afe4785SYan, Zheng break; 3931afe4785SYan, Zheng } 3947c272194SSage Weil goto out_pages; 3951d3576fdSSage Weil } 3967c272194SSage Weil pages[i] = page; 3971d3576fdSSage Weil } 398406e2c9fSAlex Elder osd_req_op_extent_osd_data_pages(req, 0, pages, len, 0, false, false); 3997c272194SSage Weil req->r_callback = finish_read; 4007c272194SSage Weil req->r_inode = inode; 4017c272194SSage Weil 4027c272194SSage Weil dout("start_read %p starting %p %lld~%lld\n", inode, req, off, len); 4037c272194SSage Weil ret = ceph_osdc_start_request(osdc, req, false); 4047c272194SSage Weil if (ret < 0) 4057c272194SSage Weil goto out_pages; 4067c272194SSage Weil ceph_osdc_put_request(req); 4072b1ac852SYan, Zheng 4082b1ac852SYan, Zheng /* After adding locked pages to page cache, the inode holds cache cap. 4092b1ac852SYan, Zheng * So we can drop our cap refs. */ 4102b1ac852SYan, Zheng if (got) 4112b1ac852SYan, Zheng ceph_put_cap_refs(ci, got); 4122b1ac852SYan, Zheng 4137c272194SSage Weil return nr_pages; 4147c272194SSage Weil 4157c272194SSage Weil out_pages: 4161afe4785SYan, Zheng for (i = 0; i < nr_pages; ++i) { 4171afe4785SYan, Zheng ceph_fscache_readpage_cancel(inode, pages[i]); 4181afe4785SYan, Zheng unlock_page(pages[i]); 4191afe4785SYan, Zheng } 4201afe4785SYan, Zheng ceph_put_page_vector(pages, nr_pages, false); 4212b1ac852SYan, Zheng out_put: 4227c272194SSage Weil ceph_osdc_put_request(req); 4232b1ac852SYan, Zheng out: 4242b1ac852SYan, Zheng if (got) 4252b1ac852SYan, Zheng ceph_put_cap_refs(ci, got); 4267c272194SSage Weil return ret; 4271d3576fdSSage Weil } 4281d3576fdSSage Weil 4297c272194SSage Weil 4301d3576fdSSage Weil /* 4311d3576fdSSage Weil * Read multiple pages. Leave pages we don't read + unlock in page_list; 4321d3576fdSSage Weil * the caller (VM) cleans them up. 4331d3576fdSSage Weil */ 4341d3576fdSSage Weil static int ceph_readpages(struct file *file, struct address_space *mapping, 4351d3576fdSSage Weil struct list_head *page_list, unsigned nr_pages) 4361d3576fdSSage Weil { 437496ad9aaSAl Viro struct inode *inode = file_inode(file); 4380d66a487SSage Weil struct ceph_fs_client *fsc = ceph_inode_to_client(inode); 4391d3576fdSSage Weil int rc = 0; 4400d66a487SSage Weil int max = 0; 4411d3576fdSSage Weil 44283701246SYan, Zheng if (ceph_inode(inode)->i_inline_version != CEPH_INLINE_NONE) 44383701246SYan, Zheng return -EINVAL; 44483701246SYan, Zheng 44599ccbd22SMilosz Tanski rc = ceph_readpages_from_fscache(mapping->host, mapping, page_list, 44699ccbd22SMilosz Tanski &nr_pages); 44799ccbd22SMilosz Tanski 44899ccbd22SMilosz Tanski if (rc == 0) 44999ccbd22SMilosz Tanski goto out; 45099ccbd22SMilosz Tanski 451aa187926SYan, Zheng max = fsc->mount_options->rsize >> PAGE_SHIFT; 452aa187926SYan, Zheng dout("readpages %p file %p nr_pages %d max %d\n", 453aa187926SYan, Zheng inode, file, nr_pages, max); 4547c272194SSage Weil while (!list_empty(page_list)) { 4550d66a487SSage Weil rc = start_read(inode, page_list, max); 4561d3576fdSSage Weil if (rc < 0) 4571d3576fdSSage Weil goto out; 4581d3576fdSSage Weil } 4591d3576fdSSage Weil out: 46076be778bSMilosz Tanski ceph_fscache_readpages_cancel(inode, page_list); 46176be778bSMilosz Tanski 4627c272194SSage Weil dout("readpages %p file %p ret %d\n", inode, file, rc); 4631d3576fdSSage Weil return rc; 4641d3576fdSSage Weil } 4651d3576fdSSage Weil 4661f934b00SYan, Zheng struct ceph_writeback_ctl 4671f934b00SYan, Zheng { 4681f934b00SYan, Zheng loff_t i_size; 4691f934b00SYan, Zheng u64 truncate_size; 4701f934b00SYan, Zheng u32 truncate_seq; 4711f934b00SYan, Zheng bool size_stable; 4721f934b00SYan, Zheng }; 4731f934b00SYan, Zheng 4741d3576fdSSage Weil /* 4751d3576fdSSage Weil * Get ref for the oldest snapc for an inode with dirty data... that is, the 4761d3576fdSSage Weil * only snap context we are allowed to write back. 4771d3576fdSSage Weil */ 4781f934b00SYan, Zheng static struct ceph_snap_context * 47905455e11SYan, Zheng get_oldest_context(struct inode *inode, struct ceph_writeback_ctl *ctl, 48005455e11SYan, Zheng struct ceph_snap_context *page_snapc) 4811d3576fdSSage Weil { 4821d3576fdSSage Weil struct ceph_inode_info *ci = ceph_inode(inode); 4831d3576fdSSage Weil struct ceph_snap_context *snapc = NULL; 4841d3576fdSSage Weil struct ceph_cap_snap *capsnap = NULL; 4851d3576fdSSage Weil 486be655596SSage Weil spin_lock(&ci->i_ceph_lock); 4871d3576fdSSage Weil list_for_each_entry(capsnap, &ci->i_cap_snaps, ci_item) { 4881d3576fdSSage Weil dout(" cap_snap %p snapc %p has %d dirty pages\n", capsnap, 4891d3576fdSSage Weil capsnap->context, capsnap->dirty_pages); 49005455e11SYan, Zheng if (!capsnap->dirty_pages) 49105455e11SYan, Zheng continue; 49205455e11SYan, Zheng 49305455e11SYan, Zheng /* get i_size, truncate_{seq,size} for page_snapc? */ 49405455e11SYan, Zheng if (snapc && capsnap->context != page_snapc) 49505455e11SYan, Zheng continue; 49605455e11SYan, Zheng 4971f934b00SYan, Zheng if (ctl) { 4981f934b00SYan, Zheng if (capsnap->writing) { 4991f934b00SYan, Zheng ctl->i_size = i_size_read(inode); 5001f934b00SYan, Zheng ctl->size_stable = false; 5011f934b00SYan, Zheng } else { 5021f934b00SYan, Zheng ctl->i_size = capsnap->size; 5031f934b00SYan, Zheng ctl->size_stable = true; 5041f934b00SYan, Zheng } 5051f934b00SYan, Zheng ctl->truncate_size = capsnap->truncate_size; 5061f934b00SYan, Zheng ctl->truncate_seq = capsnap->truncate_seq; 5071f934b00SYan, Zheng } 50805455e11SYan, Zheng 50905455e11SYan, Zheng if (snapc) 5101d3576fdSSage Weil break; 51105455e11SYan, Zheng 51205455e11SYan, Zheng snapc = ceph_get_snap_context(capsnap->context); 51305455e11SYan, Zheng if (!page_snapc || 51405455e11SYan, Zheng page_snapc == snapc || 51505455e11SYan, Zheng page_snapc->seq > snapc->seq) 51605455e11SYan, Zheng break; 5171d3576fdSSage Weil } 5187d8cb26dSSage Weil if (!snapc && ci->i_wrbuffer_ref_head) { 51980e755feSSage Weil snapc = ceph_get_snap_context(ci->i_head_snapc); 5201d3576fdSSage Weil dout(" head snapc %p has %d dirty pages\n", 5211d3576fdSSage Weil snapc, ci->i_wrbuffer_ref_head); 5221f934b00SYan, Zheng if (ctl) { 5231f934b00SYan, Zheng ctl->i_size = i_size_read(inode); 5241f934b00SYan, Zheng ctl->truncate_size = ci->i_truncate_size; 5251f934b00SYan, Zheng ctl->truncate_seq = ci->i_truncate_seq; 5261f934b00SYan, Zheng ctl->size_stable = false; 5271f934b00SYan, Zheng } 5281d3576fdSSage Weil } 529be655596SSage Weil spin_unlock(&ci->i_ceph_lock); 5301d3576fdSSage Weil return snapc; 5311d3576fdSSage Weil } 5321d3576fdSSage Weil 5331f934b00SYan, Zheng static u64 get_writepages_data_length(struct inode *inode, 5341f934b00SYan, Zheng struct page *page, u64 start) 5351f934b00SYan, Zheng { 5361f934b00SYan, Zheng struct ceph_inode_info *ci = ceph_inode(inode); 5371f934b00SYan, Zheng struct ceph_snap_context *snapc = page_snap_context(page); 5381f934b00SYan, Zheng struct ceph_cap_snap *capsnap = NULL; 5391f934b00SYan, Zheng u64 end = i_size_read(inode); 5401f934b00SYan, Zheng 5411f934b00SYan, Zheng if (snapc != ci->i_head_snapc) { 5421f934b00SYan, Zheng bool found = false; 5431f934b00SYan, Zheng spin_lock(&ci->i_ceph_lock); 5441f934b00SYan, Zheng list_for_each_entry(capsnap, &ci->i_cap_snaps, ci_item) { 5451f934b00SYan, Zheng if (capsnap->context == snapc) { 5461f934b00SYan, Zheng if (!capsnap->writing) 5471f934b00SYan, Zheng end = capsnap->size; 5481f934b00SYan, Zheng found = true; 5491f934b00SYan, Zheng break; 5501f934b00SYan, Zheng } 5511f934b00SYan, Zheng } 5521f934b00SYan, Zheng spin_unlock(&ci->i_ceph_lock); 5531f934b00SYan, Zheng WARN_ON(!found); 5541f934b00SYan, Zheng } 5551f934b00SYan, Zheng if (end > page_offset(page) + PAGE_SIZE) 5561f934b00SYan, Zheng end = page_offset(page) + PAGE_SIZE; 5571f934b00SYan, Zheng return end > start ? end - start : 0; 5581f934b00SYan, Zheng } 5591f934b00SYan, Zheng 5601d3576fdSSage Weil /* 5611d3576fdSSage Weil * Write a single page, but leave the page locked. 5621d3576fdSSage Weil * 5631d3576fdSSage Weil * If we get a write error, set the page error bit, but still adjust the 5641d3576fdSSage Weil * dirty page accounting (i.e., page is no longer dirty). 5651d3576fdSSage Weil */ 5661d3576fdSSage Weil static int writepage_nounlock(struct page *page, struct writeback_control *wbc) 5671d3576fdSSage Weil { 5681d3576fdSSage Weil struct inode *inode; 5691d3576fdSSage Weil struct ceph_inode_info *ci; 5703d14c5d2SYehuda Sadeh struct ceph_fs_client *fsc; 5716298a337SSage Weil struct ceph_snap_context *snapc, *oldest; 572fc2744aaSYan, Zheng loff_t page_off = page_offset(page); 5732baba250SYehuda Sadeh long writeback_stat; 57443986881SYan, Zheng int err, len = PAGE_SIZE; 5751f934b00SYan, Zheng struct ceph_writeback_ctl ceph_wbc; 5761d3576fdSSage Weil 5771d3576fdSSage Weil dout("writepage %p idx %lu\n", page, page->index); 5781d3576fdSSage Weil 5791d3576fdSSage Weil inode = page->mapping->host; 5801d3576fdSSage Weil ci = ceph_inode(inode); 5813d14c5d2SYehuda Sadeh fsc = ceph_inode_to_client(inode); 5821d3576fdSSage Weil 5831d3576fdSSage Weil /* verify this is a writeable snap context */ 58461600ef8SYan, Zheng snapc = page_snap_context(page); 585d37b1d99SMarkus Elfring if (!snapc) { 5861d3576fdSSage Weil dout("writepage %p page %p not dirty?\n", inode, page); 58743986881SYan, Zheng return 0; 5881d3576fdSSage Weil } 58905455e11SYan, Zheng oldest = get_oldest_context(inode, &ceph_wbc, snapc); 5906298a337SSage Weil if (snapc->seq > oldest->seq) { 5911d3576fdSSage Weil dout("writepage %p page %p snapc %p not writeable - noop\n", 59261600ef8SYan, Zheng inode, page, snapc); 5931d3576fdSSage Weil /* we should only noop if called by kswapd */ 594fa71fefbSYan, Zheng WARN_ON(!(current->flags & PF_MEMALLOC)); 5956298a337SSage Weil ceph_put_snap_context(oldest); 596fa71fefbSYan, Zheng redirty_page_for_writepage(wbc, page); 59743986881SYan, Zheng return 0; 5981d3576fdSSage Weil } 5996298a337SSage Weil ceph_put_snap_context(oldest); 6001d3576fdSSage Weil 6011d3576fdSSage Weil /* is this a partial page at end of file? */ 6021f934b00SYan, Zheng if (page_off >= ceph_wbc.i_size) { 6031f934b00SYan, Zheng dout("%p page eof %llu\n", page, ceph_wbc.i_size); 60405455e11SYan, Zheng page->mapping->a_ops->invalidatepage(page, 0, PAGE_SIZE); 60543986881SYan, Zheng return 0; 606fc2744aaSYan, Zheng } 60743986881SYan, Zheng 6081f934b00SYan, Zheng if (ceph_wbc.i_size < page_off + len) 6091f934b00SYan, Zheng len = ceph_wbc.i_size - page_off; 6101d3576fdSSage Weil 6111c0a9c2dSYan, Zheng dout("writepage %p page %p index %lu on %llu~%u snapc %p seq %lld\n", 6121c0a9c2dSYan, Zheng inode, page, page->index, page_off, len, snapc, snapc->seq); 6131d3576fdSSage Weil 6143d14c5d2SYehuda Sadeh writeback_stat = atomic_long_inc_return(&fsc->writeback_count); 6152baba250SYehuda Sadeh if (writeback_stat > 6163d14c5d2SYehuda Sadeh CONGESTION_ON_THRESH(fsc->mount_options->congestion_kb)) 61709dc9fc2SJan Kara set_bdi_congested(inode_to_bdi(inode), BLK_RW_ASYNC); 6182baba250SYehuda Sadeh 6191d3576fdSSage Weil set_page_writeback(page); 6201f934b00SYan, Zheng err = ceph_osdc_writepages(&fsc->client->osdc, ceph_vino(inode), 6211f934b00SYan, Zheng &ci->i_layout, snapc, page_off, len, 6221f934b00SYan, Zheng ceph_wbc.truncate_seq, 6231f934b00SYan, Zheng ceph_wbc.truncate_size, 62424808826SAlex Elder &inode->i_mtime, &page, 1); 6251d3576fdSSage Weil if (err < 0) { 626ad15ec06SYan, Zheng struct writeback_control tmp_wbc; 627ad15ec06SYan, Zheng if (!wbc) 628ad15ec06SYan, Zheng wbc = &tmp_wbc; 629ad15ec06SYan, Zheng if (err == -ERESTARTSYS) { 630ad15ec06SYan, Zheng /* killed by SIGKILL */ 631ad15ec06SYan, Zheng dout("writepage interrupted page %p\n", page); 632ad15ec06SYan, Zheng redirty_page_for_writepage(wbc, page); 633ad15ec06SYan, Zheng end_page_writeback(page); 63443986881SYan, Zheng return err; 635ad15ec06SYan, Zheng } 636ad15ec06SYan, Zheng dout("writepage setting page/mapping error %d %p\n", 637ad15ec06SYan, Zheng err, page); 6381d3576fdSSage Weil SetPageError(page); 6391d3576fdSSage Weil mapping_set_error(&inode->i_data, err); 6401d3576fdSSage Weil wbc->pages_skipped++; 6411d3576fdSSage Weil } else { 6421d3576fdSSage Weil dout("writepage cleaned page %p\n", page); 6431d3576fdSSage Weil err = 0; /* vfs expects us to return 0 */ 6441d3576fdSSage Weil } 6451d3576fdSSage Weil page->private = 0; 6461d3576fdSSage Weil ClearPagePrivate(page); 6471d3576fdSSage Weil end_page_writeback(page); 6481d3576fdSSage Weil ceph_put_wrbuffer_cap_refs(ci, 1, snapc); 6496298a337SSage Weil ceph_put_snap_context(snapc); /* page's reference */ 6501d3576fdSSage Weil return err; 6511d3576fdSSage Weil } 6521d3576fdSSage Weil 6531d3576fdSSage Weil static int ceph_writepage(struct page *page, struct writeback_control *wbc) 6541d3576fdSSage Weil { 655dbd646a8SYehuda Sadeh int err; 656dbd646a8SYehuda Sadeh struct inode *inode = page->mapping->host; 657dbd646a8SYehuda Sadeh BUG_ON(!inode); 65870b666c3SSage Weil ihold(inode); 659dbd646a8SYehuda Sadeh err = writepage_nounlock(page, wbc); 660ad15ec06SYan, Zheng if (err == -ERESTARTSYS) { 661ad15ec06SYan, Zheng /* direct memory reclaimer was killed by SIGKILL. return 0 662ad15ec06SYan, Zheng * to prevent caller from setting mapping/page error */ 663ad15ec06SYan, Zheng err = 0; 664ad15ec06SYan, Zheng } 6651d3576fdSSage Weil unlock_page(page); 666dbd646a8SYehuda Sadeh iput(inode); 6671d3576fdSSage Weil return err; 6681d3576fdSSage Weil } 6691d3576fdSSage Weil 6701d3576fdSSage Weil /* 6711d3576fdSSage Weil * lame release_pages helper. release_pages() isn't exported to 6721d3576fdSSage Weil * modules. 6731d3576fdSSage Weil */ 6741d3576fdSSage Weil static void ceph_release_pages(struct page **pages, int num) 6751d3576fdSSage Weil { 6761d3576fdSSage Weil struct pagevec pvec; 6771d3576fdSSage Weil int i; 6781d3576fdSSage Weil 6791d3576fdSSage Weil pagevec_init(&pvec, 0); 6801d3576fdSSage Weil for (i = 0; i < num; i++) { 6811d3576fdSSage Weil if (pagevec_add(&pvec, pages[i]) == 0) 6821d3576fdSSage Weil pagevec_release(&pvec); 6831d3576fdSSage Weil } 6841d3576fdSSage Weil pagevec_release(&pvec); 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); 71126544c62SJeff Layton } else { 71226544c62SJeff Layton ceph_clear_error_write(ci); 71326544c62SJeff Layton } 714e63dc5c7SYehuda Sadeh 715e63dc5c7SYehuda Sadeh /* 716e63dc5c7SYehuda Sadeh * We lost the cache cap, need to truncate the page before 717e63dc5c7SYehuda Sadeh * it is unlocked, otherwise we'd truncate it later in the 718e63dc5c7SYehuda Sadeh * page truncation thread, possibly losing some data that 719e63dc5c7SYehuda Sadeh * raced its way in 720e63dc5c7SYehuda Sadeh */ 7215b64640cSYan, Zheng remove_page = !(ceph_caps_issued(ci) & 7225b64640cSYan, Zheng (CEPH_CAP_FILE_CACHE|CEPH_CAP_FILE_LAZYIO)); 7235b64640cSYan, Zheng 7245b64640cSYan, Zheng /* clean all pages */ 7255b64640cSYan, Zheng for (i = 0; i < req->r_num_ops; i++) { 7265b64640cSYan, Zheng if (req->r_ops[i].op != CEPH_OSD_OP_WRITE) 7275b64640cSYan, Zheng break; 7285b64640cSYan, Zheng 7295b64640cSYan, Zheng osd_data = osd_req_op_extent_osd_data(req, i); 7305b64640cSYan, Zheng BUG_ON(osd_data->type != CEPH_OSD_DATA_TYPE_PAGES); 7315b64640cSYan, Zheng num_pages = calc_pages_for((u64)osd_data->alignment, 7325b64640cSYan, Zheng (u64)osd_data->length); 7335b64640cSYan, Zheng total_pages += num_pages; 7345b64640cSYan, Zheng for (j = 0; j < num_pages; j++) { 7355b64640cSYan, Zheng page = osd_data->pages[j]; 7365b64640cSYan, Zheng BUG_ON(!page); 7375b64640cSYan, Zheng WARN_ON(!PageUptodate(page)); 7385b64640cSYan, Zheng 7395b64640cSYan, Zheng if (atomic_long_dec_return(&fsc->writeback_count) < 7405b64640cSYan, Zheng CONGESTION_OFF_THRESH( 7415b64640cSYan, Zheng fsc->mount_options->congestion_kb)) 74209dc9fc2SJan Kara clear_bdi_congested(inode_to_bdi(inode), 7435b64640cSYan, Zheng BLK_RW_ASYNC); 7445b64640cSYan, Zheng 7455b64640cSYan, Zheng ceph_put_snap_context(page_snap_context(page)); 7465b64640cSYan, Zheng page->private = 0; 7475b64640cSYan, Zheng ClearPagePrivate(page); 7485b64640cSYan, Zheng dout("unlocking %p\n", page); 7495b64640cSYan, Zheng end_page_writeback(page); 7505b64640cSYan, Zheng 7515b64640cSYan, Zheng if (remove_page) 7525b64640cSYan, Zheng generic_error_remove_page(inode->i_mapping, 7535b64640cSYan, Zheng page); 754e63dc5c7SYehuda Sadeh 7551d3576fdSSage Weil unlock_page(page); 7561d3576fdSSage Weil } 7575b64640cSYan, Zheng dout("writepages_finish %p wrote %llu bytes cleaned %d pages\n", 7585b64640cSYan, Zheng inode, osd_data->length, rc >= 0 ? num_pages : 0); 7591d3576fdSSage Weil 76087060c10SAlex Elder ceph_release_pages(osd_data->pages, num_pages); 7615b64640cSYan, Zheng } 7625b64640cSYan, Zheng 7635b64640cSYan, Zheng ceph_put_wrbuffer_cap_refs(ci, total_pages, snapc); 7645b64640cSYan, Zheng 7655b64640cSYan, Zheng osd_data = osd_req_op_extent_osd_data(req, 0); 76687060c10SAlex Elder if (osd_data->pages_from_pool) 76787060c10SAlex Elder mempool_free(osd_data->pages, 768640ef79dSCheng Renquan ceph_sb_to_client(inode->i_sb)->wb_pagevec_pool); 7691d3576fdSSage Weil else 77087060c10SAlex Elder kfree(osd_data->pages); 7711d3576fdSSage Weil ceph_osdc_put_request(req); 7721d3576fdSSage Weil } 7731d3576fdSSage Weil 7741d3576fdSSage Weil /* 7751d3576fdSSage Weil * initiate async writeback 7761d3576fdSSage Weil */ 7771d3576fdSSage Weil static int ceph_writepages_start(struct address_space *mapping, 7781d3576fdSSage Weil struct writeback_control *wbc) 7791d3576fdSSage Weil { 7801d3576fdSSage Weil struct inode *inode = mapping->host; 7811d3576fdSSage Weil struct ceph_inode_info *ci = ceph_inode(inode); 782fc2744aaSYan, Zheng struct ceph_fs_client *fsc = ceph_inode_to_client(inode); 783fc2744aaSYan, Zheng struct ceph_vino vino = ceph_vino(inode); 7841d3576fdSSage Weil pgoff_t index, start, end; 7851d3576fdSSage Weil int range_whole = 0; 7861d3576fdSSage Weil int should_loop = 1; 7871d3576fdSSage Weil pgoff_t max_pages = 0, max_pages_ever = 0; 78880e755feSSage Weil struct ceph_snap_context *snapc = NULL, *last_snapc = NULL, *pgsnapc; 7891d3576fdSSage Weil struct pagevec pvec; 7901d3576fdSSage Weil int done = 0; 7911d3576fdSSage Weil int rc = 0; 79293407472SFabian Frederick unsigned int wsize = i_blocksize(inode); 7931d3576fdSSage Weil struct ceph_osd_request *req = NULL; 7941f934b00SYan, Zheng struct ceph_writeback_ctl ceph_wbc; 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; 81109cbfeafSKirill A. Shutemov max_pages_ever = wsize >> PAGE_SHIFT; 8121d3576fdSSage Weil 8131d3576fdSSage Weil pagevec_init(&pvec, 0); 8141d3576fdSSage Weil 8151d3576fdSSage Weil /* where to start/end? */ 8161d3576fdSSage Weil if (wbc->range_cyclic) { 8171d3576fdSSage Weil start = mapping->writeback_index; /* Start from prev offset */ 8181d3576fdSSage Weil end = -1; 8191d3576fdSSage Weil dout(" cyclic, start at %lu\n", start); 8201d3576fdSSage Weil } else { 82109cbfeafSKirill A. Shutemov start = wbc->range_start >> PAGE_SHIFT; 82209cbfeafSKirill A. Shutemov end = wbc->range_end >> PAGE_SHIFT; 8231d3576fdSSage Weil if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX) 8241d3576fdSSage Weil range_whole = 1; 8251d3576fdSSage Weil should_loop = 0; 8261d3576fdSSage Weil dout(" not cyclic, %lu to %lu\n", start, end); 8271d3576fdSSage Weil } 8281d3576fdSSage Weil index = start; 8291d3576fdSSage Weil 8301d3576fdSSage Weil retry: 8311d3576fdSSage Weil /* find oldest snap context with dirty data */ 8321d3576fdSSage Weil ceph_put_snap_context(snapc); 83305455e11SYan, Zheng snapc = get_oldest_context(inode, &ceph_wbc, NULL); 8341d3576fdSSage Weil if (!snapc) { 8351d3576fdSSage Weil /* hmm, why does writepages get called when there 8361d3576fdSSage Weil is no dirty data? */ 8371d3576fdSSage Weil dout(" no snap context with dirty data?\n"); 8381d3576fdSSage Weil goto out; 8391d3576fdSSage Weil } 8401d3576fdSSage Weil dout(" oldest snapc is %p seq %lld (%d snaps)\n", 8411d3576fdSSage Weil snapc, snapc->seq, snapc->num_snaps); 842fc2744aaSYan, Zheng 8431d3576fdSSage Weil if (last_snapc && snapc != last_snapc) { 8441d3576fdSSage Weil /* if we switched to a newer snapc, restart our scan at the 8451d3576fdSSage Weil * start of the original file range. */ 8461d3576fdSSage Weil dout(" snapc differs from last pass, restarting at %lu\n", 8471d3576fdSSage Weil index); 8481d3576fdSSage Weil index = start; 8491d3576fdSSage Weil } 8501d3576fdSSage Weil last_snapc = snapc; 8511d3576fdSSage Weil 8521d3576fdSSage Weil while (!done && index <= end) { 8531d3576fdSSage Weil unsigned i; 8545b64640cSYan, Zheng pgoff_t strip_unit_end = 0; 8555b64640cSYan, Zheng int num_ops = 0, op_idx; 8565b64640cSYan, Zheng int pvec_pages, locked_pages = 0; 8575b64640cSYan, Zheng struct page **pages = NULL, **data_pages; 858e5975c7cSAlex Elder mempool_t *pool = NULL; /* Becomes non-null if mempool used */ 8591d3576fdSSage Weil struct page *page; 8601d3576fdSSage Weil int want; 8615b64640cSYan, Zheng u64 offset = 0, len = 0; 8621d3576fdSSage Weil 8631d3576fdSSage Weil max_pages = max_pages_ever; 8641d3576fdSSage Weil 8651d3576fdSSage Weil get_more_pages: 8661d3576fdSSage Weil want = min(end - index, 8671d3576fdSSage Weil min((pgoff_t)PAGEVEC_SIZE, 8681d3576fdSSage Weil max_pages - (pgoff_t)locked_pages) - 1) 8691d3576fdSSage Weil + 1; 8701d3576fdSSage Weil pvec_pages = pagevec_lookup_tag(&pvec, mapping, &index, 8711d3576fdSSage Weil PAGECACHE_TAG_DIRTY, 8721d3576fdSSage Weil want); 8731d3576fdSSage Weil dout("pagevec_lookup_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 } 8911d3576fdSSage Weil if (!wbc->range_cyclic && page->index > end) { 8921d3576fdSSage Weil dout("end of range %p\n", page); 8931d3576fdSSage Weil done = 1; 8941d3576fdSSage Weil unlock_page(page); 8951d3576fdSSage Weil break; 8961d3576fdSSage Weil } 8975b64640cSYan, Zheng if (strip_unit_end && (page->index > strip_unit_end)) { 8985b64640cSYan, Zheng dout("end of strip unit %p\n", page); 8991d3576fdSSage Weil unlock_page(page); 9001d3576fdSSage Weil break; 9011d3576fdSSage Weil } 9021f934b00SYan, Zheng if (page_offset(page) >= ceph_wbc.i_size) { 9031f934b00SYan, Zheng dout("%p page eof %llu\n", 9041f934b00SYan, Zheng page, ceph_wbc.i_size); 9051d3576fdSSage Weil done = 1; 9061d3576fdSSage Weil unlock_page(page); 9071d3576fdSSage Weil break; 9081d3576fdSSage Weil } 9091d3576fdSSage Weil if (PageWriteback(page)) { 9100713e5f2SYan, Zheng if (wbc->sync_mode == WB_SYNC_NONE) { 9111d3576fdSSage Weil dout("%p under writeback\n", page); 9121d3576fdSSage Weil unlock_page(page); 9130713e5f2SYan, Zheng continue; 9140713e5f2SYan, Zheng } 9150713e5f2SYan, Zheng dout("waiting on writeback %p\n", page); 9160713e5f2SYan, Zheng wait_on_page_writeback(page); 9171d3576fdSSage Weil } 9181d3576fdSSage Weil 9191d3576fdSSage Weil /* only if matching snap context */ 92061600ef8SYan, Zheng pgsnapc = page_snap_context(page); 92180e755feSSage Weil if (pgsnapc->seq > snapc->seq) { 92280e755feSSage Weil dout("page snapc %p %lld > oldest %p %lld\n", 92380e755feSSage Weil pgsnapc, pgsnapc->seq, snapc, snapc->seq); 9241d3576fdSSage Weil unlock_page(page); 9250713e5f2SYan, Zheng continue; 9261d3576fdSSage Weil } 9271d3576fdSSage Weil 9281d3576fdSSage Weil if (!clear_page_dirty_for_io(page)) { 9291d3576fdSSage Weil dout("%p !clear_page_dirty_for_io\n", page); 9301d3576fdSSage Weil unlock_page(page); 9310713e5f2SYan, Zheng continue; 9321d3576fdSSage Weil } 9331d3576fdSSage Weil 934e5975c7cSAlex Elder /* 935e5975c7cSAlex Elder * We have something to write. If this is 936e5975c7cSAlex Elder * the first locked page this time through, 9375b64640cSYan, Zheng * calculate max possinle write size and 9385b64640cSYan, Zheng * allocate a page array 939e5975c7cSAlex Elder */ 9401d3576fdSSage Weil if (locked_pages == 0) { 9415b64640cSYan, Zheng u64 objnum; 9425b64640cSYan, Zheng u64 objoff; 9435b64640cSYan, Zheng 9441d3576fdSSage Weil /* prepare async write request */ 9456285bc23SAlex Elder offset = (u64)page_offset(page); 9461d3576fdSSage Weil len = wsize; 9475b64640cSYan, Zheng 9485b64640cSYan, Zheng rc = ceph_calc_file_object_mapping(&ci->i_layout, 9495b64640cSYan, Zheng offset, len, 9505b64640cSYan, Zheng &objnum, &objoff, 9515b64640cSYan, Zheng &len); 9525b64640cSYan, Zheng if (rc < 0) { 9538c71897bSHenry C Chang unlock_page(page); 9548c71897bSHenry C Chang break; 9558c71897bSHenry C Chang } 9568c71897bSHenry C Chang 9573fb99d48SYanhu Cao num_ops = 1; 9585b64640cSYan, Zheng strip_unit_end = page->index + 95909cbfeafSKirill A. Shutemov ((len - 1) >> PAGE_SHIFT); 960715e4cd4SYan, Zheng 9615b64640cSYan, Zheng BUG_ON(pages); 96288486957SAlex Elder max_pages = calc_pages_for(0, (u64)len); 963fc2744aaSYan, Zheng pages = kmalloc(max_pages * sizeof (*pages), 964fc2744aaSYan, Zheng GFP_NOFS); 96588486957SAlex Elder if (!pages) { 96688486957SAlex Elder pool = fsc->wb_pagevec_pool; 96788486957SAlex Elder pages = mempool_alloc(pool, GFP_NOFS); 968e5975c7cSAlex Elder BUG_ON(!pages); 96988486957SAlex Elder } 9705b64640cSYan, Zheng 9715b64640cSYan, Zheng len = 0; 9725b64640cSYan, Zheng } else if (page->index != 97309cbfeafSKirill A. Shutemov (offset + len) >> PAGE_SHIFT) { 9745b64640cSYan, Zheng if (num_ops >= (pool ? CEPH_OSD_SLAB_OPS : 9755b64640cSYan, Zheng CEPH_OSD_MAX_OPS)) { 9765b64640cSYan, Zheng redirty_page_for_writepage(wbc, page); 9775b64640cSYan, Zheng unlock_page(page); 9785b64640cSYan, Zheng break; 9795b64640cSYan, Zheng } 9805b64640cSYan, Zheng 9815b64640cSYan, Zheng num_ops++; 9825b64640cSYan, Zheng offset = (u64)page_offset(page); 9835b64640cSYan, Zheng len = 0; 9841d3576fdSSage Weil } 9851d3576fdSSage Weil 9861d3576fdSSage Weil /* note position of first page in pvec */ 9871d3576fdSSage Weil dout("%p will write page %p idx %lu\n", 9881d3576fdSSage Weil inode, page, page->index); 9892baba250SYehuda Sadeh 9905b64640cSYan, Zheng if (atomic_long_inc_return(&fsc->writeback_count) > 9915b64640cSYan, Zheng CONGESTION_ON_THRESH( 9923d14c5d2SYehuda Sadeh fsc->mount_options->congestion_kb)) { 99309dc9fc2SJan Kara set_bdi_congested(inode_to_bdi(inode), 994213c99eeSSage Weil BLK_RW_ASYNC); 9952baba250SYehuda Sadeh } 9962baba250SYehuda Sadeh 9970713e5f2SYan, Zheng 9980713e5f2SYan, Zheng pages[locked_pages++] = page; 9990713e5f2SYan, Zheng pvec.pages[i] = NULL; 10000713e5f2SYan, Zheng 100109cbfeafSKirill A. Shutemov len += PAGE_SIZE; 10021d3576fdSSage Weil } 10031d3576fdSSage Weil 10041d3576fdSSage Weil /* did we get anything? */ 10051d3576fdSSage Weil if (!locked_pages) 10061d3576fdSSage Weil goto release_pvec_pages; 10071d3576fdSSage Weil if (i) { 10080713e5f2SYan, Zheng unsigned j, n = 0; 10090713e5f2SYan, Zheng /* shift unused page to beginning of pvec */ 10100713e5f2SYan, Zheng for (j = 0; j < pvec_pages; j++) { 10110713e5f2SYan, Zheng if (!pvec.pages[j]) 10120713e5f2SYan, Zheng continue; 10130713e5f2SYan, Zheng if (n < j) 10140713e5f2SYan, Zheng pvec.pages[n] = pvec.pages[j]; 10150713e5f2SYan, Zheng n++; 10160713e5f2SYan, Zheng } 10170713e5f2SYan, Zheng pvec.nr = n; 10181d3576fdSSage Weil 10191d3576fdSSage Weil if (pvec_pages && i == pvec_pages && 10201d3576fdSSage Weil locked_pages < max_pages) { 10211d3576fdSSage Weil dout("reached end pvec, trying for more\n"); 10220713e5f2SYan, Zheng pagevec_release(&pvec); 10231d3576fdSSage Weil goto get_more_pages; 10241d3576fdSSage Weil } 10251d3576fdSSage Weil } 10261d3576fdSSage Weil 10275b64640cSYan, Zheng new_request: 1028e5975c7cSAlex Elder offset = page_offset(pages[0]); 10295b64640cSYan, Zheng len = wsize; 10305b64640cSYan, Zheng 10315b64640cSYan, Zheng req = ceph_osdc_new_request(&fsc->client->osdc, 10325b64640cSYan, Zheng &ci->i_layout, vino, 10335b64640cSYan, Zheng offset, &len, 0, num_ops, 10341f934b00SYan, Zheng CEPH_OSD_OP_WRITE, CEPH_OSD_FLAG_WRITE, 10351f934b00SYan, Zheng snapc, ceph_wbc.truncate_seq, 10361f934b00SYan, Zheng ceph_wbc.truncate_size, false); 10375b64640cSYan, Zheng if (IS_ERR(req)) { 10385b64640cSYan, Zheng req = ceph_osdc_new_request(&fsc->client->osdc, 10395b64640cSYan, Zheng &ci->i_layout, vino, 10405b64640cSYan, Zheng offset, &len, 0, 10415b64640cSYan, Zheng min(num_ops, 10425b64640cSYan, Zheng CEPH_OSD_SLAB_OPS), 10435b64640cSYan, Zheng CEPH_OSD_OP_WRITE, 104454ea0046SIlya Dryomov CEPH_OSD_FLAG_WRITE, 10451f934b00SYan, Zheng snapc, ceph_wbc.truncate_seq, 10461f934b00SYan, Zheng ceph_wbc.truncate_size, true); 10475b64640cSYan, Zheng BUG_ON(IS_ERR(req)); 10485b64640cSYan, Zheng } 10495b64640cSYan, Zheng BUG_ON(len < page_offset(pages[locked_pages - 1]) + 105009cbfeafSKirill A. Shutemov PAGE_SIZE - offset); 10515b64640cSYan, Zheng 10525b64640cSYan, Zheng req->r_callback = writepages_finish; 10535b64640cSYan, Zheng req->r_inode = inode; 10545b64640cSYan, Zheng 10555b64640cSYan, Zheng /* Format the osd request message and submit the write */ 10565b64640cSYan, Zheng len = 0; 10575b64640cSYan, Zheng data_pages = pages; 10585b64640cSYan, Zheng op_idx = 0; 10595b64640cSYan, Zheng for (i = 0; i < locked_pages; i++) { 10605b64640cSYan, Zheng u64 cur_offset = page_offset(pages[i]); 10615b64640cSYan, Zheng if (offset + len != cur_offset) { 10623fb99d48SYanhu Cao if (op_idx + 1 == req->r_num_ops) 10635b64640cSYan, Zheng break; 10645b64640cSYan, Zheng osd_req_op_extent_dup_last(req, op_idx, 10655b64640cSYan, Zheng cur_offset - offset); 10665b64640cSYan, Zheng dout("writepages got pages at %llu~%llu\n", 10675b64640cSYan, Zheng offset, len); 10685b64640cSYan, Zheng osd_req_op_extent_osd_data_pages(req, op_idx, 10695b64640cSYan, Zheng data_pages, len, 0, 10705b64640cSYan, Zheng !!pool, false); 10715b64640cSYan, Zheng osd_req_op_extent_update(req, op_idx, len); 10725b64640cSYan, Zheng 10735b64640cSYan, Zheng len = 0; 10745b64640cSYan, Zheng offset = cur_offset; 10755b64640cSYan, Zheng data_pages = pages + i; 10765b64640cSYan, Zheng op_idx++; 10775b64640cSYan, Zheng } 10785b64640cSYan, Zheng 10795b64640cSYan, Zheng set_page_writeback(pages[i]); 108009cbfeafSKirill A. Shutemov len += PAGE_SIZE; 10815b64640cSYan, Zheng } 10825b64640cSYan, Zheng 10831f934b00SYan, Zheng if (ceph_wbc.size_stable) { 10841f934b00SYan, Zheng len = min(len, ceph_wbc.i_size - offset); 10855b64640cSYan, Zheng } else if (i == locked_pages) { 1086e1966b49SYan, Zheng /* writepages_finish() clears writeback pages 1087e1966b49SYan, Zheng * according to the data length, so make sure 1088e1966b49SYan, Zheng * data length covers all locked pages */ 108909cbfeafSKirill A. Shutemov u64 min_len = len + 1 - PAGE_SIZE; 10901f934b00SYan, Zheng len = get_writepages_data_length(inode, pages[i - 1], 10911f934b00SYan, Zheng offset); 10925b64640cSYan, Zheng len = max(len, min_len); 1093e1966b49SYan, Zheng } 10945b64640cSYan, Zheng dout("writepages got pages at %llu~%llu\n", offset, len); 10951d3576fdSSage Weil 10965b64640cSYan, Zheng osd_req_op_extent_osd_data_pages(req, op_idx, data_pages, len, 10975b64640cSYan, Zheng 0, !!pool, false); 10985b64640cSYan, Zheng osd_req_op_extent_update(req, op_idx, len); 1099e5975c7cSAlex Elder 11005b64640cSYan, Zheng BUG_ON(op_idx + 1 != req->r_num_ops); 11015b64640cSYan, Zheng 1102e5975c7cSAlex Elder pool = NULL; 11035b64640cSYan, Zheng if (i < locked_pages) { 11045b64640cSYan, Zheng BUG_ON(num_ops <= req->r_num_ops); 11055b64640cSYan, Zheng num_ops -= req->r_num_ops; 11065b64640cSYan, Zheng locked_pages -= i; 1107e5975c7cSAlex Elder 11085b64640cSYan, Zheng /* allocate new pages array for next request */ 11095b64640cSYan, Zheng data_pages = pages; 11105b64640cSYan, Zheng pages = kmalloc(locked_pages * sizeof (*pages), 11115b64640cSYan, Zheng GFP_NOFS); 11125b64640cSYan, Zheng if (!pages) { 11135b64640cSYan, Zheng pool = fsc->wb_pagevec_pool; 11145b64640cSYan, Zheng pages = mempool_alloc(pool, GFP_NOFS); 11155b64640cSYan, Zheng BUG_ON(!pages); 11165b64640cSYan, Zheng } 11175b64640cSYan, Zheng memcpy(pages, data_pages + i, 11185b64640cSYan, Zheng locked_pages * sizeof(*pages)); 11195b64640cSYan, Zheng memset(data_pages + i, 0, 11205b64640cSYan, Zheng locked_pages * sizeof(*pages)); 11215b64640cSYan, Zheng } else { 11225b64640cSYan, Zheng BUG_ON(num_ops != req->r_num_ops); 11235b64640cSYan, Zheng index = pages[i - 1]->index + 1; 11245b64640cSYan, Zheng /* request message now owns the pages array */ 11255b64640cSYan, Zheng pages = NULL; 11265b64640cSYan, Zheng } 1127e5975c7cSAlex Elder 1128bb873b53SIlya Dryomov req->r_mtime = inode->i_mtime; 11299d6fcb08SSage Weil rc = ceph_osdc_start_request(&fsc->client->osdc, req, true); 11309d6fcb08SSage Weil BUG_ON(rc); 11311d3576fdSSage Weil req = NULL; 11321d3576fdSSage Weil 11335b64640cSYan, Zheng wbc->nr_to_write -= i; 11345b64640cSYan, Zheng if (pages) 11355b64640cSYan, Zheng goto new_request; 11365b64640cSYan, Zheng 11371d3576fdSSage Weil if (wbc->nr_to_write <= 0) 11381d3576fdSSage Weil done = 1; 11391d3576fdSSage Weil 11401d3576fdSSage Weil release_pvec_pages: 11411d3576fdSSage Weil dout("pagevec_release on %d pages (%p)\n", (int)pvec.nr, 11421d3576fdSSage Weil pvec.nr ? pvec.pages[0] : NULL); 11431d3576fdSSage Weil pagevec_release(&pvec); 11441d3576fdSSage Weil 11451d3576fdSSage Weil if (locked_pages && !done) 11461d3576fdSSage Weil goto retry; 11471d3576fdSSage Weil } 11481d3576fdSSage Weil 11491d3576fdSSage Weil if (should_loop && !done) { 11501d3576fdSSage Weil /* more to do; loop back to beginning of file */ 11511d3576fdSSage Weil dout("writepages looping back to beginning of file\n"); 11521d3576fdSSage Weil should_loop = 0; 11531d3576fdSSage Weil index = 0; 11541d3576fdSSage Weil goto retry; 11551d3576fdSSage Weil } 11561d3576fdSSage Weil 11571d3576fdSSage Weil if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0)) 11581d3576fdSSage Weil mapping->writeback_index = index; 11591d3576fdSSage Weil 11601d3576fdSSage Weil out: 11611d3576fdSSage Weil ceph_osdc_put_request(req); 11621d3576fdSSage Weil ceph_put_snap_context(snapc); 11631d3576fdSSage Weil dout("writepages done, rc = %d\n", rc); 11641d3576fdSSage Weil return rc; 11651d3576fdSSage Weil } 11661d3576fdSSage Weil 11671d3576fdSSage Weil 11681d3576fdSSage Weil 11691d3576fdSSage Weil /* 11701d3576fdSSage Weil * See if a given @snapc is either writeable, or already written. 11711d3576fdSSage Weil */ 11721d3576fdSSage Weil static int context_is_writeable_or_written(struct inode *inode, 11731d3576fdSSage Weil struct ceph_snap_context *snapc) 11741d3576fdSSage Weil { 117505455e11SYan, Zheng struct ceph_snap_context *oldest = get_oldest_context(inode, NULL, NULL); 11766298a337SSage Weil int ret = !oldest || snapc->seq <= oldest->seq; 11776298a337SSage Weil 11786298a337SSage Weil ceph_put_snap_context(oldest); 11796298a337SSage Weil return ret; 11801d3576fdSSage Weil } 11811d3576fdSSage Weil 11821d3576fdSSage Weil /* 11831d3576fdSSage Weil * We are only allowed to write into/dirty the page if the page is 11841d3576fdSSage Weil * clean, or already dirty within the same snap context. 11858f883c24SSage Weil * 11868f883c24SSage Weil * called with page locked. 11878f883c24SSage Weil * return success with page locked, 11888f883c24SSage Weil * or any failure (incl -EAGAIN) with page unlocked. 11891d3576fdSSage Weil */ 11904af6b225SYehuda Sadeh static int ceph_update_writeable_page(struct file *file, 11914af6b225SYehuda Sadeh loff_t pos, unsigned len, 11924af6b225SYehuda Sadeh struct page *page) 11931d3576fdSSage Weil { 1194496ad9aaSAl Viro struct inode *inode = file_inode(file); 11956c93df5dSYan, Zheng struct ceph_fs_client *fsc = ceph_inode_to_client(inode); 11961d3576fdSSage Weil struct ceph_inode_info *ci = ceph_inode(inode); 119709cbfeafSKirill A. Shutemov loff_t page_off = pos & PAGE_MASK; 119809cbfeafSKirill A. Shutemov int pos_in_page = pos & ~PAGE_MASK; 11991d3576fdSSage Weil int end_in_page = pos_in_page + len; 12001d3576fdSSage Weil loff_t i_size; 12011d3576fdSSage Weil int r; 120280e755feSSage Weil struct ceph_snap_context *snapc, *oldest; 12031d3576fdSSage Weil 120452953d55SSeraphime Kirkovski if (READ_ONCE(fsc->mount_state) == CEPH_MOUNT_SHUTDOWN) { 12056c93df5dSYan, Zheng dout(" page %p forced umount\n", page); 12066c93df5dSYan, Zheng unlock_page(page); 12076c93df5dSYan, Zheng return -EIO; 12086c93df5dSYan, Zheng } 12096c93df5dSYan, Zheng 12101d3576fdSSage Weil retry_locked: 12111d3576fdSSage Weil /* writepages currently holds page lock, but if we change that later, */ 12121d3576fdSSage Weil wait_on_page_writeback(page); 12131d3576fdSSage Weil 121461600ef8SYan, Zheng snapc = page_snap_context(page); 121580e755feSSage Weil if (snapc && snapc != ci->i_head_snapc) { 12161d3576fdSSage Weil /* 12171d3576fdSSage Weil * this page is already dirty in another (older) snap 12181d3576fdSSage Weil * context! is it writeable now? 12191d3576fdSSage Weil */ 122005455e11SYan, Zheng oldest = get_oldest_context(inode, NULL, NULL); 122180e755feSSage Weil if (snapc->seq > oldest->seq) { 12226298a337SSage Weil ceph_put_snap_context(oldest); 12231d3576fdSSage Weil dout(" page %p snapc %p not current or oldest\n", 12246298a337SSage Weil page, snapc); 12251d3576fdSSage Weil /* 12261d3576fdSSage Weil * queue for writeback, and wait for snapc to 12271d3576fdSSage Weil * be writeable or written 12281d3576fdSSage Weil */ 12296298a337SSage Weil snapc = ceph_get_snap_context(snapc); 12301d3576fdSSage Weil unlock_page(page); 12313c6f6b79SSage Weil ceph_queue_writeback(inode); 1232a78bbd4bSYan, Zheng r = wait_event_killable(ci->i_cap_wq, 12331d3576fdSSage Weil context_is_writeable_or_written(inode, snapc)); 12341d3576fdSSage Weil ceph_put_snap_context(snapc); 12358f883c24SSage Weil if (r == -ERESTARTSYS) 12368f883c24SSage Weil return r; 12374af6b225SYehuda Sadeh return -EAGAIN; 12381d3576fdSSage Weil } 12396298a337SSage Weil ceph_put_snap_context(oldest); 12401d3576fdSSage Weil 12411d3576fdSSage Weil /* yay, writeable, do it now (without dropping page lock) */ 12421d3576fdSSage Weil dout(" page %p snapc %p not current, but oldest\n", 12431d3576fdSSage Weil page, snapc); 12441d3576fdSSage Weil if (!clear_page_dirty_for_io(page)) 12451d3576fdSSage Weil goto retry_locked; 12461d3576fdSSage Weil r = writepage_nounlock(page, NULL); 12471d3576fdSSage Weil if (r < 0) 1248dd2bc473SYan, Zheng goto fail_unlock; 12491d3576fdSSage Weil goto retry_locked; 12501d3576fdSSage Weil } 12511d3576fdSSage Weil 12521d3576fdSSage Weil if (PageUptodate(page)) { 12531d3576fdSSage Weil dout(" page %p already uptodate\n", page); 12541d3576fdSSage Weil return 0; 12551d3576fdSSage Weil } 12561d3576fdSSage Weil 12571d3576fdSSage Weil /* full page? */ 125809cbfeafSKirill A. Shutemov if (pos_in_page == 0 && len == PAGE_SIZE) 12591d3576fdSSage Weil return 0; 12601d3576fdSSage Weil 12611d3576fdSSage Weil /* past end of file? */ 126299c88e69SYan, Zheng i_size = i_size_read(inode); 12631d3576fdSSage Weil 12641d3576fdSSage Weil if (page_off >= i_size || 12651d3576fdSSage Weil (pos_in_page == 0 && (pos+len) >= i_size && 126609cbfeafSKirill A. Shutemov end_in_page - pos_in_page != PAGE_SIZE)) { 12671d3576fdSSage Weil dout(" zeroing %p 0 - %d and %d - %d\n", 126809cbfeafSKirill A. Shutemov page, pos_in_page, end_in_page, (int)PAGE_SIZE); 12691d3576fdSSage Weil zero_user_segments(page, 12701d3576fdSSage Weil 0, pos_in_page, 127109cbfeafSKirill A. Shutemov end_in_page, PAGE_SIZE); 12721d3576fdSSage Weil return 0; 12731d3576fdSSage Weil } 12741d3576fdSSage Weil 12751d3576fdSSage Weil /* we need to read it. */ 1276dd2bc473SYan, Zheng r = ceph_do_readpage(file, page); 1277dd2bc473SYan, Zheng if (r < 0) { 1278dd2bc473SYan, Zheng if (r == -EINPROGRESS) 1279dd2bc473SYan, Zheng return -EAGAIN; 1280dd2bc473SYan, Zheng goto fail_unlock; 1281dd2bc473SYan, Zheng } 12821d3576fdSSage Weil goto retry_locked; 1283dd2bc473SYan, Zheng fail_unlock: 12841d3576fdSSage Weil unlock_page(page); 12851d3576fdSSage Weil return r; 12861d3576fdSSage Weil } 12871d3576fdSSage Weil 12881d3576fdSSage Weil /* 12894af6b225SYehuda Sadeh * We are only allowed to write into/dirty the page if the page is 12904af6b225SYehuda Sadeh * clean, or already dirty within the same snap context. 12914af6b225SYehuda Sadeh */ 12924af6b225SYehuda Sadeh static int ceph_write_begin(struct file *file, struct address_space *mapping, 12934af6b225SYehuda Sadeh loff_t pos, unsigned len, unsigned flags, 12944af6b225SYehuda Sadeh struct page **pagep, void **fsdata) 12954af6b225SYehuda Sadeh { 1296496ad9aaSAl Viro struct inode *inode = file_inode(file); 12974af6b225SYehuda Sadeh struct page *page; 129809cbfeafSKirill A. Shutemov pgoff_t index = pos >> PAGE_SHIFT; 12997971bd92SSage Weil int r; 13004af6b225SYehuda Sadeh 13014af6b225SYehuda Sadeh do { 13024af6b225SYehuda Sadeh /* get a page */ 13034af6b225SYehuda Sadeh page = grab_cache_page_write_begin(mapping, index, 0); 13047971bd92SSage Weil if (!page) 13057971bd92SSage Weil return -ENOMEM; 13064af6b225SYehuda Sadeh 13074af6b225SYehuda Sadeh dout("write_begin file %p inode %p page %p %d~%d\n", file, 13084af6b225SYehuda Sadeh inode, page, (int)pos, (int)len); 13094af6b225SYehuda Sadeh 13104af6b225SYehuda Sadeh r = ceph_update_writeable_page(file, pos, len, page); 1311c1d00b2dSTaesoo Kim if (r < 0) 131209cbfeafSKirill A. Shutemov put_page(page); 1313c1d00b2dSTaesoo Kim else 1314c1d00b2dSTaesoo Kim *pagep = page; 13154af6b225SYehuda Sadeh } while (r == -EAGAIN); 13164af6b225SYehuda Sadeh 13174af6b225SYehuda Sadeh return r; 13184af6b225SYehuda Sadeh } 13194af6b225SYehuda Sadeh 13204af6b225SYehuda Sadeh /* 13211d3576fdSSage Weil * we don't do anything in here that simple_write_end doesn't do 13225dda377cSYan, Zheng * except adjust dirty page accounting 13231d3576fdSSage Weil */ 13241d3576fdSSage Weil static int ceph_write_end(struct file *file, struct address_space *mapping, 13251d3576fdSSage Weil loff_t pos, unsigned len, unsigned copied, 13261d3576fdSSage Weil struct page *page, void *fsdata) 13271d3576fdSSage Weil { 1328496ad9aaSAl Viro struct inode *inode = file_inode(file); 1329efb0ca76SYan, Zheng bool check_cap = false; 13301d3576fdSSage Weil 13311d3576fdSSage Weil dout("write_end file %p inode %p page %p %d~%d (%d)\n", file, 13321d3576fdSSage Weil inode, page, (int)pos, (int)copied, (int)len); 13331d3576fdSSage Weil 13341d3576fdSSage Weil /* zero the stale part of the page if we did a short copy */ 1335b9de313cSAl Viro if (!PageUptodate(page)) { 1336b9de313cSAl Viro if (copied < len) { 1337b9de313cSAl Viro copied = 0; 1338b9de313cSAl Viro goto out; 1339b9de313cSAl Viro } 1340b9de313cSAl Viro SetPageUptodate(page); 1341b9de313cSAl Viro } 13421d3576fdSSage Weil 13431d3576fdSSage Weil /* did file size increase? */ 134499c88e69SYan, Zheng if (pos+copied > i_size_read(inode)) 13451d3576fdSSage Weil check_cap = ceph_inode_set_size(inode, pos+copied); 13461d3576fdSSage Weil 13471d3576fdSSage Weil set_page_dirty(page); 13481d3576fdSSage Weil 1349b9de313cSAl Viro out: 13501d3576fdSSage Weil unlock_page(page); 135109cbfeafSKirill A. Shutemov put_page(page); 13521d3576fdSSage Weil 13531d3576fdSSage Weil if (check_cap) 13541d3576fdSSage Weil ceph_check_caps(ceph_inode(inode), CHECK_CAPS_AUTHONLY, NULL); 13551d3576fdSSage Weil 13561d3576fdSSage Weil return copied; 13571d3576fdSSage Weil } 13581d3576fdSSage Weil 13591d3576fdSSage Weil /* 13601d3576fdSSage Weil * we set .direct_IO to indicate direct io is supported, but since we 13611d3576fdSSage Weil * intercept O_DIRECT reads and writes early, this function should 13621d3576fdSSage Weil * never get called. 13631d3576fdSSage Weil */ 1364c8b8e32dSChristoph Hellwig static ssize_t ceph_direct_io(struct kiocb *iocb, struct iov_iter *iter) 13651d3576fdSSage Weil { 13661d3576fdSSage Weil WARN_ON(1); 13671d3576fdSSage Weil return -EINVAL; 13681d3576fdSSage Weil } 13691d3576fdSSage Weil 13701d3576fdSSage Weil const struct address_space_operations ceph_aops = { 13711d3576fdSSage Weil .readpage = ceph_readpage, 13721d3576fdSSage Weil .readpages = ceph_readpages, 13731d3576fdSSage Weil .writepage = ceph_writepage, 13741d3576fdSSage Weil .writepages = ceph_writepages_start, 13751d3576fdSSage Weil .write_begin = ceph_write_begin, 13761d3576fdSSage Weil .write_end = ceph_write_end, 13771d3576fdSSage Weil .set_page_dirty = ceph_set_page_dirty, 13781d3576fdSSage Weil .invalidatepage = ceph_invalidatepage, 13791d3576fdSSage Weil .releasepage = ceph_releasepage, 13801d3576fdSSage Weil .direct_IO = ceph_direct_io, 13811d3576fdSSage Weil }; 13821d3576fdSSage Weil 13834f7e89f6SYan, Zheng static void ceph_block_sigs(sigset_t *oldset) 13844f7e89f6SYan, Zheng { 13854f7e89f6SYan, Zheng sigset_t mask; 13864f7e89f6SYan, Zheng siginitsetinv(&mask, sigmask(SIGKILL)); 13874f7e89f6SYan, Zheng sigprocmask(SIG_BLOCK, &mask, oldset); 13884f7e89f6SYan, Zheng } 13894f7e89f6SYan, Zheng 13904f7e89f6SYan, Zheng static void ceph_restore_sigs(sigset_t *oldset) 13914f7e89f6SYan, Zheng { 13924f7e89f6SYan, Zheng sigprocmask(SIG_SETMASK, oldset, NULL); 13934f7e89f6SYan, Zheng } 13941d3576fdSSage Weil 13951d3576fdSSage Weil /* 13961d3576fdSSage Weil * vm ops 13971d3576fdSSage Weil */ 139811bac800SDave Jiang static int ceph_filemap_fault(struct vm_fault *vmf) 139961f68816SYan, Zheng { 140011bac800SDave Jiang struct vm_area_struct *vma = vmf->vma; 140161f68816SYan, Zheng struct inode *inode = file_inode(vma->vm_file); 140261f68816SYan, Zheng struct ceph_inode_info *ci = ceph_inode(inode); 140361f68816SYan, Zheng struct ceph_file_info *fi = vma->vm_file->private_data; 14043738daa6SYan, Zheng struct page *pinned_page = NULL; 140509cbfeafSKirill A. Shutemov loff_t off = vmf->pgoff << PAGE_SHIFT; 140661f68816SYan, Zheng int want, got, ret; 14074f7e89f6SYan, Zheng sigset_t oldset; 14084f7e89f6SYan, Zheng 14094f7e89f6SYan, Zheng ceph_block_sigs(&oldset); 141061f68816SYan, Zheng 141161f68816SYan, Zheng dout("filemap_fault %p %llx.%llx %llu~%zd trying to get caps\n", 141209cbfeafSKirill A. Shutemov inode, ceph_vinop(inode), off, (size_t)PAGE_SIZE); 141361f68816SYan, Zheng if (fi->fmode & CEPH_FILE_MODE_LAZY) 141461f68816SYan, Zheng want = CEPH_CAP_FILE_CACHE | CEPH_CAP_FILE_LAZYIO; 141561f68816SYan, Zheng else 141661f68816SYan, Zheng want = CEPH_CAP_FILE_CACHE; 14174f7e89f6SYan, Zheng 141861f68816SYan, Zheng got = 0; 14194f7e89f6SYan, Zheng ret = ceph_get_caps(ci, CEPH_CAP_FILE_RD, want, -1, &got, &pinned_page); 14206ce026e4SYan, Zheng if (ret < 0) 14214f7e89f6SYan, Zheng goto out_restore; 14226ce026e4SYan, Zheng 142361f68816SYan, Zheng dout("filemap_fault %p %llu~%zd got cap refs on %s\n", 142409cbfeafSKirill A. Shutemov inode, off, (size_t)PAGE_SIZE, ceph_cap_string(got)); 142561f68816SYan, Zheng 142683701246SYan, Zheng if ((got & (CEPH_CAP_FILE_CACHE | CEPH_CAP_FILE_LAZYIO)) || 14272b1ac852SYan, Zheng ci->i_inline_version == CEPH_INLINE_NONE) { 14282b1ac852SYan, Zheng current->journal_info = vma->vm_file; 142911bac800SDave Jiang ret = filemap_fault(vmf); 14302b1ac852SYan, Zheng current->journal_info = NULL; 14312b1ac852SYan, Zheng } else 143283701246SYan, Zheng ret = -EAGAIN; 143361f68816SYan, Zheng 143461f68816SYan, Zheng dout("filemap_fault %p %llu~%zd dropping cap refs on %s ret %d\n", 143509cbfeafSKirill A. Shutemov inode, off, (size_t)PAGE_SIZE, ceph_cap_string(got), ret); 14363738daa6SYan, Zheng if (pinned_page) 143709cbfeafSKirill A. Shutemov put_page(pinned_page); 143861f68816SYan, Zheng ceph_put_cap_refs(ci, got); 143961f68816SYan, Zheng 144083701246SYan, Zheng if (ret != -EAGAIN) 14414f7e89f6SYan, Zheng goto out_restore; 144283701246SYan, Zheng 144383701246SYan, Zheng /* read inline data */ 144409cbfeafSKirill A. Shutemov if (off >= PAGE_SIZE) { 144583701246SYan, Zheng /* does not support inline data > PAGE_SIZE */ 144683701246SYan, Zheng ret = VM_FAULT_SIGBUS; 144783701246SYan, Zheng } else { 144883701246SYan, Zheng int ret1; 144983701246SYan, Zheng struct address_space *mapping = inode->i_mapping; 145083701246SYan, Zheng struct page *page = find_or_create_page(mapping, 0, 1451c62d2555SMichal Hocko mapping_gfp_constraint(mapping, 1452c62d2555SMichal Hocko ~__GFP_FS)); 145383701246SYan, Zheng if (!page) { 145483701246SYan, Zheng ret = VM_FAULT_OOM; 14554f7e89f6SYan, Zheng goto out_inline; 145683701246SYan, Zheng } 145783701246SYan, Zheng ret1 = __ceph_do_getattr(inode, page, 145883701246SYan, Zheng CEPH_STAT_CAP_INLINE_DATA, true); 145983701246SYan, Zheng if (ret1 < 0 || off >= i_size_read(inode)) { 146083701246SYan, Zheng unlock_page(page); 146109cbfeafSKirill A. Shutemov put_page(page); 14626ce026e4SYan, Zheng if (ret1 < 0) 14636ce026e4SYan, Zheng ret = ret1; 14646ce026e4SYan, Zheng else 146583701246SYan, Zheng ret = VM_FAULT_SIGBUS; 14664f7e89f6SYan, Zheng goto out_inline; 146783701246SYan, Zheng } 146809cbfeafSKirill A. Shutemov if (ret1 < PAGE_SIZE) 146909cbfeafSKirill A. Shutemov zero_user_segment(page, ret1, PAGE_SIZE); 147083701246SYan, Zheng else 147183701246SYan, Zheng flush_dcache_page(page); 147283701246SYan, Zheng SetPageUptodate(page); 147383701246SYan, Zheng vmf->page = page; 147483701246SYan, Zheng ret = VM_FAULT_MAJOR | VM_FAULT_LOCKED; 14754f7e89f6SYan, Zheng out_inline: 147683701246SYan, Zheng dout("filemap_fault %p %llu~%zd read inline data ret %d\n", 147709cbfeafSKirill A. Shutemov inode, off, (size_t)PAGE_SIZE, ret); 14784f7e89f6SYan, Zheng } 14794f7e89f6SYan, Zheng out_restore: 14804f7e89f6SYan, Zheng ceph_restore_sigs(&oldset); 14816ce026e4SYan, Zheng if (ret < 0) 14826ce026e4SYan, Zheng ret = (ret == -ENOMEM) ? VM_FAULT_OOM : VM_FAULT_SIGBUS; 14836ce026e4SYan, Zheng 148461f68816SYan, Zheng return ret; 148561f68816SYan, Zheng } 14861d3576fdSSage Weil 14871d3576fdSSage Weil /* 14881d3576fdSSage Weil * Reuse write_begin here for simplicity. 14891d3576fdSSage Weil */ 149011bac800SDave Jiang static int ceph_page_mkwrite(struct vm_fault *vmf) 14911d3576fdSSage Weil { 149211bac800SDave Jiang struct vm_area_struct *vma = vmf->vma; 1493496ad9aaSAl Viro struct inode *inode = file_inode(vma->vm_file); 149461f68816SYan, Zheng struct ceph_inode_info *ci = ceph_inode(inode); 149561f68816SYan, Zheng struct ceph_file_info *fi = vma->vm_file->private_data; 1496f66fd9f0SYan, Zheng struct ceph_cap_flush *prealloc_cf; 149761f68816SYan, Zheng struct page *page = vmf->page; 14986285bc23SAlex Elder loff_t off = page_offset(page); 149961f68816SYan, Zheng loff_t size = i_size_read(inode); 150061f68816SYan, Zheng size_t len; 150161f68816SYan, Zheng int want, got, ret; 15024f7e89f6SYan, Zheng sigset_t oldset; 15031d3576fdSSage Weil 1504f66fd9f0SYan, Zheng prealloc_cf = ceph_alloc_cap_flush(); 1505f66fd9f0SYan, Zheng if (!prealloc_cf) 15066ce026e4SYan, Zheng return VM_FAULT_OOM; 1507f66fd9f0SYan, Zheng 15084f7e89f6SYan, Zheng ceph_block_sigs(&oldset); 15091d3576fdSSage Weil 151028127bddSYan, Zheng if (ci->i_inline_version != CEPH_INLINE_NONE) { 151128127bddSYan, Zheng struct page *locked_page = NULL; 151228127bddSYan, Zheng if (off == 0) { 151328127bddSYan, Zheng lock_page(page); 151428127bddSYan, Zheng locked_page = page; 151528127bddSYan, Zheng } 151628127bddSYan, Zheng ret = ceph_uninline_data(vma->vm_file, locked_page); 151728127bddSYan, Zheng if (locked_page) 151828127bddSYan, Zheng unlock_page(locked_page); 15196ce026e4SYan, Zheng if (ret < 0) 1520f66fd9f0SYan, Zheng goto out_free; 1521f66fd9f0SYan, Zheng } 152228127bddSYan, Zheng 152309cbfeafSKirill A. Shutemov if (off + PAGE_SIZE <= size) 152409cbfeafSKirill A. Shutemov len = PAGE_SIZE; 15251d3576fdSSage Weil else 152609cbfeafSKirill A. Shutemov len = size & ~PAGE_MASK; 15271d3576fdSSage Weil 152861f68816SYan, Zheng dout("page_mkwrite %p %llx.%llx %llu~%zd getting caps i_size %llu\n", 152961f68816SYan, Zheng inode, ceph_vinop(inode), off, len, size); 153061f68816SYan, Zheng if (fi->fmode & CEPH_FILE_MODE_LAZY) 153161f68816SYan, Zheng want = CEPH_CAP_FILE_BUFFER | CEPH_CAP_FILE_LAZYIO; 153261f68816SYan, Zheng else 153361f68816SYan, Zheng want = CEPH_CAP_FILE_BUFFER; 15344f7e89f6SYan, Zheng 153561f68816SYan, Zheng got = 0; 15363738daa6SYan, Zheng ret = ceph_get_caps(ci, CEPH_CAP_FILE_WR, want, off + len, 15373738daa6SYan, Zheng &got, NULL); 15386ce026e4SYan, Zheng if (ret < 0) 1539f66fd9f0SYan, Zheng goto out_free; 15406ce026e4SYan, Zheng 154161f68816SYan, Zheng dout("page_mkwrite %p %llu~%zd got cap refs on %s\n", 154261f68816SYan, Zheng inode, off, len, ceph_cap_string(got)); 154361f68816SYan, Zheng 154461f68816SYan, Zheng /* Update time before taking page lock */ 154561f68816SYan, Zheng file_update_time(vma->vm_file); 15464af6b225SYehuda Sadeh 1547f0b33df5SYan, Zheng do { 15484af6b225SYehuda Sadeh lock_page(page); 15494af6b225SYehuda Sadeh 15506ce026e4SYan, Zheng if ((off > size) || (page->mapping != inode->i_mapping)) { 1551f9cac5acSYan, Zheng unlock_page(page); 15526ce026e4SYan, Zheng ret = VM_FAULT_NOPAGE; 1553f0b33df5SYan, Zheng break; 1554f9cac5acSYan, Zheng } 15554af6b225SYehuda Sadeh 15564af6b225SYehuda Sadeh ret = ceph_update_writeable_page(vma->vm_file, off, len, page); 1557f9cac5acSYan, Zheng if (ret >= 0) { 15584af6b225SYehuda Sadeh /* success. we'll keep the page locked. */ 15591d3576fdSSage Weil set_page_dirty(page); 15601d3576fdSSage Weil ret = VM_FAULT_LOCKED; 15611d3576fdSSage Weil } 1562f0b33df5SYan, Zheng } while (ret == -EAGAIN); 1563f0b33df5SYan, Zheng 156428127bddSYan, Zheng if (ret == VM_FAULT_LOCKED || 156528127bddSYan, Zheng ci->i_inline_version != CEPH_INLINE_NONE) { 156661f68816SYan, Zheng int dirty; 156761f68816SYan, Zheng spin_lock(&ci->i_ceph_lock); 156828127bddSYan, Zheng ci->i_inline_version = CEPH_INLINE_NONE; 1569f66fd9f0SYan, Zheng dirty = __ceph_mark_dirty_caps(ci, CEPH_CAP_FILE_WR, 1570f66fd9f0SYan, Zheng &prealloc_cf); 157161f68816SYan, Zheng spin_unlock(&ci->i_ceph_lock); 157261f68816SYan, Zheng if (dirty) 157361f68816SYan, Zheng __mark_inode_dirty(inode, dirty); 157461f68816SYan, Zheng } 157561f68816SYan, Zheng 157661f68816SYan, Zheng dout("page_mkwrite %p %llu~%zd dropping cap refs on %s ret %d\n", 157761f68816SYan, Zheng inode, off, len, ceph_cap_string(got), ret); 157861f68816SYan, Zheng ceph_put_cap_refs(ci, got); 1579f66fd9f0SYan, Zheng out_free: 15804f7e89f6SYan, Zheng ceph_restore_sigs(&oldset); 1581f66fd9f0SYan, Zheng ceph_free_cap_flush(prealloc_cf); 15826ce026e4SYan, Zheng if (ret < 0) 15836ce026e4SYan, Zheng ret = (ret == -ENOMEM) ? VM_FAULT_OOM : VM_FAULT_SIGBUS; 15841d3576fdSSage Weil return ret; 15851d3576fdSSage Weil } 15861d3576fdSSage Weil 158731c542a1SYan, Zheng void ceph_fill_inline_data(struct inode *inode, struct page *locked_page, 158831c542a1SYan, Zheng char *data, size_t len) 158931c542a1SYan, Zheng { 159031c542a1SYan, Zheng struct address_space *mapping = inode->i_mapping; 159131c542a1SYan, Zheng struct page *page; 159231c542a1SYan, Zheng 159331c542a1SYan, Zheng if (locked_page) { 159431c542a1SYan, Zheng page = locked_page; 159531c542a1SYan, Zheng } else { 159631c542a1SYan, Zheng if (i_size_read(inode) == 0) 159731c542a1SYan, Zheng return; 159831c542a1SYan, Zheng page = find_or_create_page(mapping, 0, 1599c62d2555SMichal Hocko mapping_gfp_constraint(mapping, 1600c62d2555SMichal Hocko ~__GFP_FS)); 160131c542a1SYan, Zheng if (!page) 160231c542a1SYan, Zheng return; 160331c542a1SYan, Zheng if (PageUptodate(page)) { 160431c542a1SYan, Zheng unlock_page(page); 160509cbfeafSKirill A. Shutemov put_page(page); 160631c542a1SYan, Zheng return; 160731c542a1SYan, Zheng } 160831c542a1SYan, Zheng } 160931c542a1SYan, Zheng 16100668ff52SIlya Dryomov dout("fill_inline_data %p %llx.%llx len %zu locked_page %p\n", 161131c542a1SYan, Zheng inode, ceph_vinop(inode), len, locked_page); 161231c542a1SYan, Zheng 161331c542a1SYan, Zheng if (len > 0) { 161431c542a1SYan, Zheng void *kaddr = kmap_atomic(page); 161531c542a1SYan, Zheng memcpy(kaddr, data, len); 161631c542a1SYan, Zheng kunmap_atomic(kaddr); 161731c542a1SYan, Zheng } 161831c542a1SYan, Zheng 161931c542a1SYan, Zheng if (page != locked_page) { 162009cbfeafSKirill A. Shutemov if (len < PAGE_SIZE) 162109cbfeafSKirill A. Shutemov zero_user_segment(page, len, PAGE_SIZE); 162231c542a1SYan, Zheng else 162331c542a1SYan, Zheng flush_dcache_page(page); 162431c542a1SYan, Zheng 162531c542a1SYan, Zheng SetPageUptodate(page); 162631c542a1SYan, Zheng unlock_page(page); 162709cbfeafSKirill A. Shutemov put_page(page); 162831c542a1SYan, Zheng } 162931c542a1SYan, Zheng } 163031c542a1SYan, Zheng 163128127bddSYan, Zheng int ceph_uninline_data(struct file *filp, struct page *locked_page) 163228127bddSYan, Zheng { 163328127bddSYan, Zheng struct inode *inode = file_inode(filp); 163428127bddSYan, Zheng struct ceph_inode_info *ci = ceph_inode(inode); 163528127bddSYan, Zheng struct ceph_fs_client *fsc = ceph_inode_to_client(inode); 163628127bddSYan, Zheng struct ceph_osd_request *req; 163728127bddSYan, Zheng struct page *page = NULL; 163828127bddSYan, Zheng u64 len, inline_version; 163928127bddSYan, Zheng int err = 0; 164028127bddSYan, Zheng bool from_pagecache = false; 164128127bddSYan, Zheng 164228127bddSYan, Zheng spin_lock(&ci->i_ceph_lock); 164328127bddSYan, Zheng inline_version = ci->i_inline_version; 164428127bddSYan, Zheng spin_unlock(&ci->i_ceph_lock); 164528127bddSYan, Zheng 164628127bddSYan, Zheng dout("uninline_data %p %llx.%llx inline_version %llu\n", 164728127bddSYan, Zheng inode, ceph_vinop(inode), inline_version); 164828127bddSYan, Zheng 164928127bddSYan, Zheng if (inline_version == 1 || /* initial version, no data */ 165028127bddSYan, Zheng inline_version == CEPH_INLINE_NONE) 165128127bddSYan, Zheng goto out; 165228127bddSYan, Zheng 165328127bddSYan, Zheng if (locked_page) { 165428127bddSYan, Zheng page = locked_page; 165528127bddSYan, Zheng WARN_ON(!PageUptodate(page)); 165628127bddSYan, Zheng } else if (ceph_caps_issued(ci) & 165728127bddSYan, Zheng (CEPH_CAP_FILE_CACHE|CEPH_CAP_FILE_LAZYIO)) { 165828127bddSYan, Zheng page = find_get_page(inode->i_mapping, 0); 165928127bddSYan, Zheng if (page) { 166028127bddSYan, Zheng if (PageUptodate(page)) { 166128127bddSYan, Zheng from_pagecache = true; 166228127bddSYan, Zheng lock_page(page); 166328127bddSYan, Zheng } else { 166409cbfeafSKirill A. Shutemov put_page(page); 166528127bddSYan, Zheng page = NULL; 166628127bddSYan, Zheng } 166728127bddSYan, Zheng } 166828127bddSYan, Zheng } 166928127bddSYan, Zheng 167028127bddSYan, Zheng if (page) { 167128127bddSYan, Zheng len = i_size_read(inode); 167209cbfeafSKirill A. Shutemov if (len > PAGE_SIZE) 167309cbfeafSKirill A. Shutemov len = PAGE_SIZE; 167428127bddSYan, Zheng } else { 167528127bddSYan, Zheng page = __page_cache_alloc(GFP_NOFS); 167628127bddSYan, Zheng if (!page) { 167728127bddSYan, Zheng err = -ENOMEM; 167828127bddSYan, Zheng goto out; 167928127bddSYan, Zheng } 168028127bddSYan, Zheng err = __ceph_do_getattr(inode, page, 168128127bddSYan, Zheng CEPH_STAT_CAP_INLINE_DATA, true); 168228127bddSYan, Zheng if (err < 0) { 168328127bddSYan, Zheng /* no inline data */ 168428127bddSYan, Zheng if (err == -ENODATA) 168528127bddSYan, Zheng err = 0; 168628127bddSYan, Zheng goto out; 168728127bddSYan, Zheng } 168828127bddSYan, Zheng len = err; 168928127bddSYan, Zheng } 169028127bddSYan, Zheng 169128127bddSYan, Zheng req = ceph_osdc_new_request(&fsc->client->osdc, &ci->i_layout, 169228127bddSYan, Zheng ceph_vino(inode), 0, &len, 0, 1, 169354ea0046SIlya Dryomov CEPH_OSD_OP_CREATE, CEPH_OSD_FLAG_WRITE, 169434b759b4SIlya Dryomov NULL, 0, 0, false); 169528127bddSYan, Zheng if (IS_ERR(req)) { 169628127bddSYan, Zheng err = PTR_ERR(req); 169728127bddSYan, Zheng goto out; 169828127bddSYan, Zheng } 169928127bddSYan, Zheng 1700bb873b53SIlya Dryomov req->r_mtime = inode->i_mtime; 170128127bddSYan, Zheng err = ceph_osdc_start_request(&fsc->client->osdc, req, false); 170228127bddSYan, Zheng if (!err) 170328127bddSYan, Zheng err = ceph_osdc_wait_request(&fsc->client->osdc, req); 170428127bddSYan, Zheng ceph_osdc_put_request(req); 170528127bddSYan, Zheng if (err < 0) 170628127bddSYan, Zheng goto out; 170728127bddSYan, Zheng 170828127bddSYan, Zheng req = ceph_osdc_new_request(&fsc->client->osdc, &ci->i_layout, 170928127bddSYan, Zheng ceph_vino(inode), 0, &len, 1, 3, 171054ea0046SIlya Dryomov CEPH_OSD_OP_WRITE, CEPH_OSD_FLAG_WRITE, 171134b759b4SIlya Dryomov NULL, ci->i_truncate_seq, 171234b759b4SIlya Dryomov ci->i_truncate_size, false); 171328127bddSYan, Zheng if (IS_ERR(req)) { 171428127bddSYan, Zheng err = PTR_ERR(req); 171528127bddSYan, Zheng goto out; 171628127bddSYan, Zheng } 171728127bddSYan, Zheng 171828127bddSYan, Zheng osd_req_op_extent_osd_data_pages(req, 1, &page, len, 0, false, false); 171928127bddSYan, Zheng 1720ec137c10SYan, Zheng { 1721ec137c10SYan, Zheng __le64 xattr_buf = cpu_to_le64(inline_version); 172228127bddSYan, Zheng err = osd_req_op_xattr_init(req, 0, CEPH_OSD_OP_CMPXATTR, 1723ec137c10SYan, Zheng "inline_version", &xattr_buf, 1724ec137c10SYan, Zheng sizeof(xattr_buf), 172528127bddSYan, Zheng CEPH_OSD_CMPXATTR_OP_GT, 172628127bddSYan, Zheng CEPH_OSD_CMPXATTR_MODE_U64); 172728127bddSYan, Zheng if (err) 172828127bddSYan, Zheng goto out_put; 1729ec137c10SYan, Zheng } 173028127bddSYan, Zheng 1731ec137c10SYan, Zheng { 1732ec137c10SYan, Zheng char xattr_buf[32]; 1733ec137c10SYan, Zheng int xattr_len = snprintf(xattr_buf, sizeof(xattr_buf), 1734ec137c10SYan, Zheng "%llu", inline_version); 173528127bddSYan, Zheng err = osd_req_op_xattr_init(req, 2, CEPH_OSD_OP_SETXATTR, 1736ec137c10SYan, Zheng "inline_version", 1737ec137c10SYan, Zheng xattr_buf, xattr_len, 0, 0); 173828127bddSYan, Zheng if (err) 173928127bddSYan, Zheng goto out_put; 1740ec137c10SYan, Zheng } 174128127bddSYan, Zheng 1742bb873b53SIlya Dryomov req->r_mtime = inode->i_mtime; 174328127bddSYan, Zheng err = ceph_osdc_start_request(&fsc->client->osdc, req, false); 174428127bddSYan, Zheng if (!err) 174528127bddSYan, Zheng err = ceph_osdc_wait_request(&fsc->client->osdc, req); 174628127bddSYan, Zheng out_put: 174728127bddSYan, Zheng ceph_osdc_put_request(req); 174828127bddSYan, Zheng if (err == -ECANCELED) 174928127bddSYan, Zheng err = 0; 175028127bddSYan, Zheng out: 175128127bddSYan, Zheng if (page && page != locked_page) { 175228127bddSYan, Zheng if (from_pagecache) { 175328127bddSYan, Zheng unlock_page(page); 175409cbfeafSKirill A. Shutemov put_page(page); 175528127bddSYan, Zheng } else 175628127bddSYan, Zheng __free_pages(page, 0); 175728127bddSYan, Zheng } 175828127bddSYan, Zheng 175928127bddSYan, Zheng dout("uninline_data %p %llx.%llx inline_version %llu = %d\n", 176028127bddSYan, Zheng inode, ceph_vinop(inode), inline_version, err); 176128127bddSYan, Zheng return err; 176228127bddSYan, Zheng } 176328127bddSYan, Zheng 17647cbea8dcSKirill A. Shutemov static const struct vm_operations_struct ceph_vmops = { 176561f68816SYan, Zheng .fault = ceph_filemap_fault, 17661d3576fdSSage Weil .page_mkwrite = ceph_page_mkwrite, 17671d3576fdSSage Weil }; 17681d3576fdSSage Weil 17691d3576fdSSage Weil int ceph_mmap(struct file *file, struct vm_area_struct *vma) 17701d3576fdSSage Weil { 17711d3576fdSSage Weil struct address_space *mapping = file->f_mapping; 17721d3576fdSSage Weil 17731d3576fdSSage Weil if (!mapping->a_ops->readpage) 17741d3576fdSSage Weil return -ENOEXEC; 17751d3576fdSSage Weil file_accessed(file); 17761d3576fdSSage Weil vma->vm_ops = &ceph_vmops; 17771d3576fdSSage Weil return 0; 17781d3576fdSSage Weil } 177910183a69SYan, Zheng 178010183a69SYan, Zheng enum { 178110183a69SYan, Zheng POOL_READ = 1, 178210183a69SYan, Zheng POOL_WRITE = 2, 178310183a69SYan, Zheng }; 178410183a69SYan, Zheng 1785779fe0fbSYan, Zheng static int __ceph_pool_perm_get(struct ceph_inode_info *ci, 1786779fe0fbSYan, Zheng s64 pool, struct ceph_string *pool_ns) 178710183a69SYan, Zheng { 178810183a69SYan, Zheng struct ceph_fs_client *fsc = ceph_inode_to_client(&ci->vfs_inode); 178910183a69SYan, Zheng struct ceph_mds_client *mdsc = fsc->mdsc; 179010183a69SYan, Zheng struct ceph_osd_request *rd_req = NULL, *wr_req = NULL; 179110183a69SYan, Zheng struct rb_node **p, *parent; 179210183a69SYan, Zheng struct ceph_pool_perm *perm; 179310183a69SYan, Zheng struct page **pages; 1794779fe0fbSYan, Zheng size_t pool_ns_len; 179510183a69SYan, Zheng int err = 0, err2 = 0, have = 0; 179610183a69SYan, Zheng 179710183a69SYan, Zheng down_read(&mdsc->pool_perm_rwsem); 179810183a69SYan, Zheng p = &mdsc->pool_perm_tree.rb_node; 179910183a69SYan, Zheng while (*p) { 180010183a69SYan, Zheng perm = rb_entry(*p, struct ceph_pool_perm, node); 180110183a69SYan, Zheng if (pool < perm->pool) 180210183a69SYan, Zheng p = &(*p)->rb_left; 180310183a69SYan, Zheng else if (pool > perm->pool) 180410183a69SYan, Zheng p = &(*p)->rb_right; 180510183a69SYan, Zheng else { 1806779fe0fbSYan, Zheng int ret = ceph_compare_string(pool_ns, 1807779fe0fbSYan, Zheng perm->pool_ns, 1808779fe0fbSYan, Zheng perm->pool_ns_len); 1809779fe0fbSYan, Zheng if (ret < 0) 1810779fe0fbSYan, Zheng p = &(*p)->rb_left; 1811779fe0fbSYan, Zheng else if (ret > 0) 1812779fe0fbSYan, Zheng p = &(*p)->rb_right; 1813779fe0fbSYan, Zheng else { 181410183a69SYan, Zheng have = perm->perm; 181510183a69SYan, Zheng break; 181610183a69SYan, Zheng } 181710183a69SYan, Zheng } 1818779fe0fbSYan, Zheng } 181910183a69SYan, Zheng up_read(&mdsc->pool_perm_rwsem); 182010183a69SYan, Zheng if (*p) 182110183a69SYan, Zheng goto out; 182210183a69SYan, Zheng 1823779fe0fbSYan, Zheng if (pool_ns) 1824779fe0fbSYan, Zheng dout("__ceph_pool_perm_get pool %lld ns %.*s no perm cached\n", 1825779fe0fbSYan, Zheng pool, (int)pool_ns->len, pool_ns->str); 1826779fe0fbSYan, Zheng else 18277627151eSYan, Zheng dout("__ceph_pool_perm_get pool %lld no perm cached\n", pool); 182810183a69SYan, Zheng 182910183a69SYan, Zheng down_write(&mdsc->pool_perm_rwsem); 1830779fe0fbSYan, Zheng p = &mdsc->pool_perm_tree.rb_node; 183110183a69SYan, Zheng parent = NULL; 183210183a69SYan, Zheng while (*p) { 183310183a69SYan, Zheng parent = *p; 183410183a69SYan, Zheng perm = rb_entry(parent, struct ceph_pool_perm, node); 183510183a69SYan, Zheng if (pool < perm->pool) 183610183a69SYan, Zheng p = &(*p)->rb_left; 183710183a69SYan, Zheng else if (pool > perm->pool) 183810183a69SYan, Zheng p = &(*p)->rb_right; 183910183a69SYan, Zheng else { 1840779fe0fbSYan, Zheng int ret = ceph_compare_string(pool_ns, 1841779fe0fbSYan, Zheng perm->pool_ns, 1842779fe0fbSYan, Zheng perm->pool_ns_len); 1843779fe0fbSYan, Zheng if (ret < 0) 1844779fe0fbSYan, Zheng p = &(*p)->rb_left; 1845779fe0fbSYan, Zheng else if (ret > 0) 1846779fe0fbSYan, Zheng p = &(*p)->rb_right; 1847779fe0fbSYan, Zheng else { 184810183a69SYan, Zheng have = perm->perm; 184910183a69SYan, Zheng break; 185010183a69SYan, Zheng } 185110183a69SYan, Zheng } 1852779fe0fbSYan, Zheng } 185310183a69SYan, Zheng if (*p) { 185410183a69SYan, Zheng up_write(&mdsc->pool_perm_rwsem); 185510183a69SYan, Zheng goto out; 185610183a69SYan, Zheng } 185710183a69SYan, Zheng 185834b759b4SIlya Dryomov rd_req = ceph_osdc_alloc_request(&fsc->client->osdc, NULL, 185910183a69SYan, Zheng 1, false, GFP_NOFS); 186010183a69SYan, Zheng if (!rd_req) { 186110183a69SYan, Zheng err = -ENOMEM; 186210183a69SYan, Zheng goto out_unlock; 186310183a69SYan, Zheng } 186410183a69SYan, Zheng 186510183a69SYan, Zheng rd_req->r_flags = CEPH_OSD_FLAG_READ; 186610183a69SYan, Zheng osd_req_op_init(rd_req, 0, CEPH_OSD_OP_STAT, 0); 186710183a69SYan, Zheng rd_req->r_base_oloc.pool = pool; 1868779fe0fbSYan, Zheng if (pool_ns) 1869779fe0fbSYan, Zheng rd_req->r_base_oloc.pool_ns = ceph_get_string(pool_ns); 1870d30291b9SIlya Dryomov ceph_oid_printf(&rd_req->r_base_oid, "%llx.00000000", ci->i_vino.ino); 187110183a69SYan, Zheng 187213d1ad16SIlya Dryomov err = ceph_osdc_alloc_messages(rd_req, GFP_NOFS); 187313d1ad16SIlya Dryomov if (err) 187413d1ad16SIlya Dryomov goto out_unlock; 187510183a69SYan, Zheng 187634b759b4SIlya Dryomov wr_req = ceph_osdc_alloc_request(&fsc->client->osdc, NULL, 187710183a69SYan, Zheng 1, false, GFP_NOFS); 187810183a69SYan, Zheng if (!wr_req) { 187910183a69SYan, Zheng err = -ENOMEM; 188010183a69SYan, Zheng goto out_unlock; 188110183a69SYan, Zheng } 188210183a69SYan, Zheng 188354ea0046SIlya Dryomov wr_req->r_flags = CEPH_OSD_FLAG_WRITE; 188410183a69SYan, Zheng osd_req_op_init(wr_req, 0, CEPH_OSD_OP_CREATE, CEPH_OSD_OP_FLAG_EXCL); 188563244fa1SIlya Dryomov ceph_oloc_copy(&wr_req->r_base_oloc, &rd_req->r_base_oloc); 1886d30291b9SIlya Dryomov ceph_oid_copy(&wr_req->r_base_oid, &rd_req->r_base_oid); 188710183a69SYan, Zheng 188813d1ad16SIlya Dryomov err = ceph_osdc_alloc_messages(wr_req, GFP_NOFS); 188913d1ad16SIlya Dryomov if (err) 189013d1ad16SIlya Dryomov goto out_unlock; 189110183a69SYan, Zheng 189210183a69SYan, Zheng /* one page should be large enough for STAT data */ 189310183a69SYan, Zheng pages = ceph_alloc_page_vector(1, GFP_KERNEL); 189410183a69SYan, Zheng if (IS_ERR(pages)) { 189510183a69SYan, Zheng err = PTR_ERR(pages); 189610183a69SYan, Zheng goto out_unlock; 189710183a69SYan, Zheng } 189810183a69SYan, Zheng 189910183a69SYan, Zheng osd_req_op_raw_data_in_pages(rd_req, 0, pages, PAGE_SIZE, 190010183a69SYan, Zheng 0, false, true); 190110183a69SYan, Zheng err = ceph_osdc_start_request(&fsc->client->osdc, rd_req, false); 190210183a69SYan, Zheng 1903bb873b53SIlya Dryomov wr_req->r_mtime = ci->vfs_inode.i_mtime; 1904a1f4020aSJeff Layton wr_req->r_abort_on_full = true; 190510183a69SYan, Zheng err2 = ceph_osdc_start_request(&fsc->client->osdc, wr_req, false); 190610183a69SYan, Zheng 190710183a69SYan, Zheng if (!err) 190810183a69SYan, Zheng err = ceph_osdc_wait_request(&fsc->client->osdc, rd_req); 190910183a69SYan, Zheng if (!err2) 191010183a69SYan, Zheng err2 = ceph_osdc_wait_request(&fsc->client->osdc, wr_req); 191110183a69SYan, Zheng 191210183a69SYan, Zheng if (err >= 0 || err == -ENOENT) 191310183a69SYan, Zheng have |= POOL_READ; 191410183a69SYan, Zheng else if (err != -EPERM) 191510183a69SYan, Zheng goto out_unlock; 191610183a69SYan, Zheng 191710183a69SYan, Zheng if (err2 == 0 || err2 == -EEXIST) 191810183a69SYan, Zheng have |= POOL_WRITE; 191910183a69SYan, Zheng else if (err2 != -EPERM) { 192010183a69SYan, Zheng err = err2; 192110183a69SYan, Zheng goto out_unlock; 192210183a69SYan, Zheng } 192310183a69SYan, Zheng 1924779fe0fbSYan, Zheng pool_ns_len = pool_ns ? pool_ns->len : 0; 1925779fe0fbSYan, Zheng perm = kmalloc(sizeof(*perm) + pool_ns_len + 1, GFP_NOFS); 192610183a69SYan, Zheng if (!perm) { 192710183a69SYan, Zheng err = -ENOMEM; 192810183a69SYan, Zheng goto out_unlock; 192910183a69SYan, Zheng } 193010183a69SYan, Zheng 193110183a69SYan, Zheng perm->pool = pool; 193210183a69SYan, Zheng perm->perm = have; 1933779fe0fbSYan, Zheng perm->pool_ns_len = pool_ns_len; 1934779fe0fbSYan, Zheng if (pool_ns_len > 0) 1935779fe0fbSYan, Zheng memcpy(perm->pool_ns, pool_ns->str, pool_ns_len); 1936779fe0fbSYan, Zheng perm->pool_ns[pool_ns_len] = 0; 1937779fe0fbSYan, Zheng 193810183a69SYan, Zheng rb_link_node(&perm->node, parent, p); 193910183a69SYan, Zheng rb_insert_color(&perm->node, &mdsc->pool_perm_tree); 194010183a69SYan, Zheng err = 0; 194110183a69SYan, Zheng out_unlock: 194210183a69SYan, Zheng up_write(&mdsc->pool_perm_rwsem); 194310183a69SYan, Zheng 194410183a69SYan, Zheng ceph_osdc_put_request(rd_req); 194510183a69SYan, Zheng ceph_osdc_put_request(wr_req); 194610183a69SYan, Zheng out: 194710183a69SYan, Zheng if (!err) 194810183a69SYan, Zheng err = have; 1949779fe0fbSYan, Zheng if (pool_ns) 1950779fe0fbSYan, Zheng dout("__ceph_pool_perm_get pool %lld ns %.*s result = %d\n", 1951779fe0fbSYan, Zheng pool, (int)pool_ns->len, pool_ns->str, err); 1952779fe0fbSYan, Zheng else 19537627151eSYan, Zheng dout("__ceph_pool_perm_get pool %lld result = %d\n", pool, err); 195410183a69SYan, Zheng return err; 195510183a69SYan, Zheng } 195610183a69SYan, Zheng 195710183a69SYan, Zheng int ceph_pool_perm_check(struct ceph_inode_info *ci, int need) 195810183a69SYan, Zheng { 19597627151eSYan, Zheng s64 pool; 1960779fe0fbSYan, Zheng struct ceph_string *pool_ns; 196110183a69SYan, Zheng int ret, flags; 196210183a69SYan, Zheng 196380e80fbbSYan, Zheng if (ci->i_vino.snap != CEPH_NOSNAP) { 196480e80fbbSYan, Zheng /* 196580e80fbbSYan, Zheng * Pool permission check needs to write to the first object. 196680e80fbbSYan, Zheng * But for snapshot, head of the first object may have alread 196780e80fbbSYan, Zheng * been deleted. Skip check to avoid creating orphan object. 196880e80fbbSYan, Zheng */ 196980e80fbbSYan, Zheng return 0; 197080e80fbbSYan, Zheng } 197180e80fbbSYan, Zheng 197210183a69SYan, Zheng if (ceph_test_mount_opt(ceph_inode_to_client(&ci->vfs_inode), 197310183a69SYan, Zheng NOPOOLPERM)) 197410183a69SYan, Zheng return 0; 197510183a69SYan, Zheng 197610183a69SYan, Zheng spin_lock(&ci->i_ceph_lock); 197710183a69SYan, Zheng flags = ci->i_ceph_flags; 19787627151eSYan, Zheng pool = ci->i_layout.pool_id; 197910183a69SYan, Zheng spin_unlock(&ci->i_ceph_lock); 198010183a69SYan, Zheng check: 198110183a69SYan, Zheng if (flags & CEPH_I_POOL_PERM) { 198210183a69SYan, Zheng if ((need & CEPH_CAP_FILE_RD) && !(flags & CEPH_I_POOL_RD)) { 19837627151eSYan, Zheng dout("ceph_pool_perm_check pool %lld no read perm\n", 198410183a69SYan, Zheng pool); 198510183a69SYan, Zheng return -EPERM; 198610183a69SYan, Zheng } 198710183a69SYan, Zheng if ((need & CEPH_CAP_FILE_WR) && !(flags & CEPH_I_POOL_WR)) { 19887627151eSYan, Zheng dout("ceph_pool_perm_check pool %lld no write perm\n", 198910183a69SYan, Zheng pool); 199010183a69SYan, Zheng return -EPERM; 199110183a69SYan, Zheng } 199210183a69SYan, Zheng return 0; 199310183a69SYan, Zheng } 199410183a69SYan, Zheng 1995779fe0fbSYan, Zheng pool_ns = ceph_try_get_string(ci->i_layout.pool_ns); 1996779fe0fbSYan, Zheng ret = __ceph_pool_perm_get(ci, pool, pool_ns); 1997779fe0fbSYan, Zheng ceph_put_string(pool_ns); 199810183a69SYan, Zheng if (ret < 0) 199910183a69SYan, Zheng return ret; 200010183a69SYan, Zheng 200110183a69SYan, Zheng flags = CEPH_I_POOL_PERM; 200210183a69SYan, Zheng if (ret & POOL_READ) 200310183a69SYan, Zheng flags |= CEPH_I_POOL_RD; 200410183a69SYan, Zheng if (ret & POOL_WRITE) 200510183a69SYan, Zheng flags |= CEPH_I_POOL_WR; 200610183a69SYan, Zheng 200710183a69SYan, Zheng spin_lock(&ci->i_ceph_lock); 2008779fe0fbSYan, Zheng if (pool == ci->i_layout.pool_id && 2009779fe0fbSYan, Zheng pool_ns == rcu_dereference_raw(ci->i_layout.pool_ns)) { 2010779fe0fbSYan, Zheng ci->i_ceph_flags |= flags; 201110183a69SYan, Zheng } else { 20127627151eSYan, Zheng pool = ci->i_layout.pool_id; 201310183a69SYan, Zheng flags = ci->i_ceph_flags; 201410183a69SYan, Zheng } 201510183a69SYan, Zheng spin_unlock(&ci->i_ceph_lock); 201610183a69SYan, Zheng goto check; 201710183a69SYan, Zheng } 201810183a69SYan, Zheng 201910183a69SYan, Zheng void ceph_pool_perm_destroy(struct ceph_mds_client *mdsc) 202010183a69SYan, Zheng { 202110183a69SYan, Zheng struct ceph_pool_perm *perm; 202210183a69SYan, Zheng struct rb_node *n; 202310183a69SYan, Zheng 202410183a69SYan, Zheng while (!RB_EMPTY_ROOT(&mdsc->pool_perm_tree)) { 202510183a69SYan, Zheng n = rb_first(&mdsc->pool_perm_tree); 202610183a69SYan, Zheng perm = rb_entry(n, struct ceph_pool_perm, node); 202710183a69SYan, Zheng rb_erase(n, &mdsc->pool_perm_tree); 202810183a69SYan, Zheng kfree(perm); 202910183a69SYan, Zheng } 203010183a69SYan, Zheng } 2031