Yehuda Sadeh | 3d14c5d | 2010-04-06 15:14:15 -0700 | [diff] [blame] | 1 | #include <linux/ceph/ceph_debug.h> |
Sage Weil | 1d3576f | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2 | |
| 3 | #include <linux/backing-dev.h> |
| 4 | #include <linux/fs.h> |
| 5 | #include <linux/mm.h> |
| 6 | #include <linux/pagemap.h> |
| 7 | #include <linux/writeback.h> /* generic_writepages */ |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 8 | #include <linux/slab.h> |
Sage Weil | 1d3576f | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 9 | #include <linux/pagevec.h> |
| 10 | #include <linux/task_io_accounting_ops.h> |
| 11 | |
| 12 | #include "super.h" |
Yehuda Sadeh | 3d14c5d | 2010-04-06 15:14:15 -0700 | [diff] [blame] | 13 | #include "mds_client.h" |
Milosz Tanski | 99ccbd2 | 2013-08-21 17:29:54 -0400 | [diff] [blame] | 14 | #include "cache.h" |
Yehuda Sadeh | 3d14c5d | 2010-04-06 15:14:15 -0700 | [diff] [blame] | 15 | #include <linux/ceph/osd_client.h> |
Sage Weil | 1d3576f | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 16 | |
| 17 | /* |
| 18 | * Ceph address space ops. |
| 19 | * |
| 20 | * There are a few funny things going on here. |
| 21 | * |
| 22 | * The page->private field is used to reference a struct |
| 23 | * ceph_snap_context for _every_ dirty page. This indicates which |
| 24 | * snapshot the page was logically dirtied in, and thus which snap |
| 25 | * context needs to be associated with the osd write during writeback. |
| 26 | * |
| 27 | * Similarly, struct ceph_inode_info maintains a set of counters to |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 28 | * count dirty pages on the inode. In the absence of snapshots, |
Sage Weil | 1d3576f | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 29 | * i_wrbuffer_ref == i_wrbuffer_ref_head == the dirty page count. |
| 30 | * |
| 31 | * When a snapshot is taken (that is, when the client receives |
| 32 | * notification that a snapshot was taken), each inode with caps and |
| 33 | * with dirty pages (dirty pages implies there is a cap) gets a new |
| 34 | * ceph_cap_snap in the i_cap_snaps list (which is sorted in ascending |
| 35 | * order, new snaps go to the tail). The i_wrbuffer_ref_head count is |
| 36 | * moved to capsnap->dirty. (Unless a sync write is currently in |
| 37 | * progress. In that case, the capsnap is said to be "pending", new |
| 38 | * writes cannot start, and the capsnap isn't "finalized" until the |
| 39 | * write completes (or fails) and a final size/mtime for the inode for |
| 40 | * that snap can be settled upon.) i_wrbuffer_ref_head is reset to 0. |
| 41 | * |
| 42 | * On writeback, we must submit writes to the osd IN SNAP ORDER. So, |
| 43 | * we look for the first capsnap in i_cap_snaps and write out pages in |
| 44 | * that snap context _only_. Then we move on to the next capsnap, |
| 45 | * eventually reaching the "live" or "head" context (i.e., pages that |
| 46 | * are not yet snapped) and are writing the most recently dirtied |
| 47 | * pages. |
| 48 | * |
| 49 | * Invalidate and so forth must take care to ensure the dirty page |
| 50 | * accounting is preserved. |
| 51 | */ |
| 52 | |
Yehuda Sadeh | 2baba25 | 2009-12-18 13:51:57 -0800 | [diff] [blame] | 53 | #define CONGESTION_ON_THRESH(congestion_kb) (congestion_kb >> (PAGE_SHIFT-10)) |
| 54 | #define CONGESTION_OFF_THRESH(congestion_kb) \ |
| 55 | (CONGESTION_ON_THRESH(congestion_kb) - \ |
| 56 | (CONGESTION_ON_THRESH(congestion_kb) >> 2)) |
| 57 | |
Yan, Zheng | 61600ef | 2012-05-28 14:44:30 +0800 | [diff] [blame] | 58 | static inline struct ceph_snap_context *page_snap_context(struct page *page) |
| 59 | { |
| 60 | if (PagePrivate(page)) |
| 61 | return (void *)page->private; |
| 62 | return NULL; |
| 63 | } |
Sage Weil | 1d3576f | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 64 | |
| 65 | /* |
| 66 | * Dirty a page. Optimistically adjust accounting, on the assumption |
| 67 | * that we won't race with invalidate. If we do, readjust. |
| 68 | */ |
| 69 | static int ceph_set_page_dirty(struct page *page) |
| 70 | { |
| 71 | struct address_space *mapping = page->mapping; |
| 72 | struct inode *inode; |
| 73 | struct ceph_inode_info *ci; |
Sage Weil | 1d3576f | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 74 | struct ceph_snap_context *snapc; |
Sha Zhengju | 7d6e1f5 | 2013-08-21 16:27:34 +0800 | [diff] [blame] | 75 | int ret; |
Sage Weil | 1d3576f | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 76 | |
| 77 | if (unlikely(!mapping)) |
| 78 | return !TestSetPageDirty(page); |
| 79 | |
Sha Zhengju | 7d6e1f5 | 2013-08-21 16:27:34 +0800 | [diff] [blame] | 80 | if (PageDirty(page)) { |
Sage Weil | 1d3576f | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 81 | dout("%p set_page_dirty %p idx %lu -- already dirty\n", |
| 82 | mapping->host, page, page->index); |
Sha Zhengju | 7d6e1f5 | 2013-08-21 16:27:34 +0800 | [diff] [blame] | 83 | BUG_ON(!PagePrivate(page)); |
Sage Weil | 1d3576f | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 84 | return 0; |
| 85 | } |
| 86 | |
| 87 | inode = mapping->host; |
| 88 | ci = ceph_inode(inode); |
| 89 | |
Sage Weil | 1d3576f | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 90 | /* dirty the head */ |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 91 | spin_lock(&ci->i_ceph_lock); |
Yan, Zheng | 5dda377c | 2015-04-30 14:40:54 +0800 | [diff] [blame] | 92 | BUG_ON(ci->i_wr_ref == 0); // caller should hold Fw reference |
| 93 | if (__ceph_have_pending_cap_snap(ci)) { |
| 94 | struct ceph_cap_snap *capsnap = |
| 95 | list_last_entry(&ci->i_cap_snaps, |
| 96 | struct ceph_cap_snap, |
| 97 | ci_item); |
| 98 | snapc = ceph_get_snap_context(capsnap->context); |
| 99 | capsnap->dirty_pages++; |
| 100 | } else { |
| 101 | BUG_ON(!ci->i_head_snapc); |
| 102 | snapc = ceph_get_snap_context(ci->i_head_snapc); |
| 103 | ++ci->i_wrbuffer_ref_head; |
| 104 | } |
Sage Weil | 1d3576f | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 105 | if (ci->i_wrbuffer_ref == 0) |
Dave Chinner | 0444d76 | 2011-03-29 18:08:50 +1100 | [diff] [blame] | 106 | ihold(inode); |
Sage Weil | 1d3576f | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 107 | ++ci->i_wrbuffer_ref; |
| 108 | dout("%p set_page_dirty %p idx %lu head %d/%d -> %d/%d " |
| 109 | "snapc %p seq %lld (%d snaps)\n", |
| 110 | mapping->host, page, page->index, |
| 111 | ci->i_wrbuffer_ref-1, ci->i_wrbuffer_ref_head-1, |
| 112 | ci->i_wrbuffer_ref, ci->i_wrbuffer_ref_head, |
| 113 | snapc, snapc->seq, snapc->num_snaps); |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 114 | spin_unlock(&ci->i_ceph_lock); |
Sage Weil | 1d3576f | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 115 | |
Sha Zhengju | 7d6e1f5 | 2013-08-21 16:27:34 +0800 | [diff] [blame] | 116 | /* |
| 117 | * Reference snap context in page->private. Also set |
| 118 | * PagePrivate so that we get invalidatepage callback. |
| 119 | */ |
| 120 | BUG_ON(PagePrivate(page)); |
| 121 | page->private = (unsigned long)snapc; |
| 122 | SetPagePrivate(page); |
Sage Weil | 1d3576f | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 123 | |
Sha Zhengju | 7d6e1f5 | 2013-08-21 16:27:34 +0800 | [diff] [blame] | 124 | ret = __set_page_dirty_nobuffers(page); |
| 125 | WARN_ON(!PageLocked(page)); |
| 126 | WARN_ON(!page->mapping); |
Sage Weil | 1d3576f | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 127 | |
Sha Zhengju | 7d6e1f5 | 2013-08-21 16:27:34 +0800 | [diff] [blame] | 128 | return ret; |
Sage Weil | 1d3576f | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | /* |
| 132 | * If we are truncating the full page (i.e. offset == 0), adjust the |
| 133 | * dirty page counters appropriately. Only called if there is private |
| 134 | * data on the page. |
| 135 | */ |
Lukas Czerner | d47992f | 2013-05-21 23:17:23 -0400 | [diff] [blame] | 136 | static void ceph_invalidatepage(struct page *page, unsigned int offset, |
| 137 | unsigned int length) |
Sage Weil | 1d3576f | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 138 | { |
Alexander Beregalov | 4ce1e9a | 2010-02-22 17:17:44 +0300 | [diff] [blame] | 139 | struct inode *inode; |
Sage Weil | 1d3576f | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 140 | struct ceph_inode_info *ci; |
Yan, Zheng | 61600ef | 2012-05-28 14:44:30 +0800 | [diff] [blame] | 141 | struct ceph_snap_context *snapc = page_snap_context(page); |
Sage Weil | 1d3576f | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 142 | |
Alexander Beregalov | 4ce1e9a | 2010-02-22 17:17:44 +0300 | [diff] [blame] | 143 | inode = page->mapping->host; |
Milosz Tanski | b150f5c1 | 2013-08-09 12:59:55 -0400 | [diff] [blame] | 144 | ci = ceph_inode(inode); |
| 145 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 146 | if (offset != 0 || length != PAGE_SIZE) { |
Milosz Tanski | b150f5c1 | 2013-08-09 12:59:55 -0400 | [diff] [blame] | 147 | dout("%p invalidatepage %p idx %lu partial dirty page %u~%u\n", |
| 148 | inode, page, page->index, offset, length); |
| 149 | return; |
| 150 | } |
Alexander Beregalov | 4ce1e9a | 2010-02-22 17:17:44 +0300 | [diff] [blame] | 151 | |
Milosz Tanski | 99ccbd2 | 2013-08-21 17:29:54 -0400 | [diff] [blame] | 152 | ceph_invalidate_fscache_page(inode, page); |
| 153 | |
| 154 | if (!PagePrivate(page)) |
| 155 | return; |
| 156 | |
Sage Weil | 1d3576f | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 157 | /* |
| 158 | * We can get non-dirty pages here due to races between |
| 159 | * set_page_dirty and truncate_complete_page; just spit out a |
| 160 | * warning, in case we end up with accounting problems later. |
| 161 | */ |
| 162 | if (!PageDirty(page)) |
| 163 | pr_err("%p invalidatepage %p page not dirty\n", inode, page); |
| 164 | |
Milosz Tanski | b150f5c1 | 2013-08-09 12:59:55 -0400 | [diff] [blame] | 165 | ClearPageChecked(page); |
Sage Weil | 1d3576f | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 166 | |
Milosz Tanski | b150f5c1 | 2013-08-09 12:59:55 -0400 | [diff] [blame] | 167 | dout("%p invalidatepage %p idx %lu full dirty page\n", |
| 168 | inode, page, page->index); |
| 169 | |
| 170 | ceph_put_wrbuffer_cap_refs(ci, 1, snapc); |
| 171 | ceph_put_snap_context(snapc); |
| 172 | page->private = 0; |
| 173 | ClearPagePrivate(page); |
Sage Weil | 1d3576f | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 174 | } |
| 175 | |
Sage Weil | 1d3576f | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 176 | static int ceph_releasepage(struct page *page, gfp_t g) |
| 177 | { |
NeilBrown | e55f1a1 | 2016-08-31 12:59:29 +1000 | [diff] [blame] | 178 | dout("%p releasepage %p idx %lu (%sdirty)\n", page->mapping->host, |
| 179 | page, page->index, PageDirty(page) ? "" : "not "); |
Milosz Tanski | 99ccbd2 | 2013-08-21 17:29:54 -0400 | [diff] [blame] | 180 | |
| 181 | /* Can we release the page from the cache? */ |
| 182 | if (!ceph_release_fscache_page(page, g)) |
| 183 | return 0; |
| 184 | |
| 185 | return !PagePrivate(page); |
Sage Weil | 1d3576f | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | /* |
| 189 | * read a single page, without unlocking it. |
| 190 | */ |
| 191 | static int readpage_nounlock(struct file *filp, struct page *page) |
| 192 | { |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 193 | struct inode *inode = file_inode(filp); |
Sage Weil | 1d3576f | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 194 | struct ceph_inode_info *ci = ceph_inode(inode); |
Milosz Tanski | 99ccbd2 | 2013-08-21 17:29:54 -0400 | [diff] [blame] | 195 | struct ceph_osd_client *osdc = |
Yehuda Sadeh | 3d14c5d | 2010-04-06 15:14:15 -0700 | [diff] [blame] | 196 | &ceph_inode_to_client(inode)->client->osdc; |
Sage Weil | 1d3576f | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 197 | int err = 0; |
Yan, Zheng | 8370124 | 2014-11-14 22:36:18 +0800 | [diff] [blame] | 198 | u64 off = page_offset(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 199 | u64 len = PAGE_SIZE; |
Sage Weil | 1d3576f | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 200 | |
Yan, Zheng | 8370124 | 2014-11-14 22:36:18 +0800 | [diff] [blame] | 201 | if (off >= i_size_read(inode)) { |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 202 | zero_user_segment(page, 0, PAGE_SIZE); |
Yan, Zheng | 8370124 | 2014-11-14 22:36:18 +0800 | [diff] [blame] | 203 | SetPageUptodate(page); |
| 204 | return 0; |
| 205 | } |
Milosz Tanski | 99ccbd2 | 2013-08-21 17:29:54 -0400 | [diff] [blame] | 206 | |
Yan, Zheng | fcc02d2 | 2015-01-10 11:43:12 +0800 | [diff] [blame] | 207 | if (ci->i_inline_version != CEPH_INLINE_NONE) { |
| 208 | /* |
| 209 | * Uptodate inline data should have been added |
| 210 | * into page cache while getting Fcr caps. |
| 211 | */ |
| 212 | if (off == 0) |
| 213 | return -EINVAL; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 214 | zero_user_segment(page, 0, PAGE_SIZE); |
Yan, Zheng | fcc02d2 | 2015-01-10 11:43:12 +0800 | [diff] [blame] | 215 | SetPageUptodate(page); |
| 216 | return 0; |
| 217 | } |
Yan, Zheng | 8370124 | 2014-11-14 22:36:18 +0800 | [diff] [blame] | 218 | |
| 219 | err = ceph_readpage_from_fscache(inode, page); |
Milosz Tanski | 99ccbd2 | 2013-08-21 17:29:54 -0400 | [diff] [blame] | 220 | if (err == 0) |
| 221 | goto out; |
| 222 | |
Sage Weil | 1d3576f | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 223 | dout("readpage inode %p file %p page %p index %lu\n", |
| 224 | inode, filp, page, page->index); |
| 225 | err = ceph_osdc_readpages(osdc, ceph_vino(inode), &ci->i_layout, |
Yan, Zheng | 8370124 | 2014-11-14 22:36:18 +0800 | [diff] [blame] | 226 | off, &len, |
Sage Weil | 1d3576f | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 227 | ci->i_truncate_seq, ci->i_truncate_size, |
Sage Weil | b7495fc | 2010-11-09 12:43:12 -0800 | [diff] [blame] | 228 | &page, 1, 0); |
Sage Weil | 1d3576f | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 229 | if (err == -ENOENT) |
| 230 | err = 0; |
| 231 | if (err < 0) { |
| 232 | SetPageError(page); |
Li Wang | 1830280 | 2013-12-19 06:03:49 -0800 | [diff] [blame] | 233 | ceph_fscache_readpage_cancel(inode, page); |
Sage Weil | 1d3576f | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 234 | goto out; |
Sage Weil | 1d3576f | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 235 | } |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 236 | if (err < PAGE_SIZE) |
Zhang Zhen | 23cd573 | 2014-05-28 15:09:55 +0800 | [diff] [blame] | 237 | /* zero fill remainder of page */ |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 238 | zero_user_segment(page, err, PAGE_SIZE); |
Zhang Zhen | 23cd573 | 2014-05-28 15:09:55 +0800 | [diff] [blame] | 239 | else |
| 240 | flush_dcache_page(page); |
Sage Weil | 1d3576f | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 241 | |
Zhang Zhen | 23cd573 | 2014-05-28 15:09:55 +0800 | [diff] [blame] | 242 | SetPageUptodate(page); |
| 243 | ceph_readpage_to_fscache(inode, page); |
Milosz Tanski | 99ccbd2 | 2013-08-21 17:29:54 -0400 | [diff] [blame] | 244 | |
Sage Weil | 1d3576f | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 245 | out: |
| 246 | return err < 0 ? err : 0; |
| 247 | } |
| 248 | |
| 249 | static int ceph_readpage(struct file *filp, struct page *page) |
| 250 | { |
| 251 | int r = readpage_nounlock(filp, page); |
| 252 | unlock_page(page); |
| 253 | return r; |
| 254 | } |
| 255 | |
| 256 | /* |
Sage Weil | 7c27219 | 2011-08-03 09:58:09 -0700 | [diff] [blame] | 257 | * Finish an async read(ahead) op. |
Sage Weil | 1d3576f | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 258 | */ |
Ilya Dryomov | 85e084f | 2016-04-28 16:07:24 +0200 | [diff] [blame] | 259 | static void finish_read(struct ceph_osd_request *req) |
Sage Weil | 1d3576f | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 260 | { |
Sage Weil | 7c27219 | 2011-08-03 09:58:09 -0700 | [diff] [blame] | 261 | struct inode *inode = req->r_inode; |
Alex Elder | 87060c1 | 2013-04-03 01:28:58 -0500 | [diff] [blame] | 262 | struct ceph_osd_data *osd_data; |
Ilya Dryomov | 85e084f | 2016-04-28 16:07:24 +0200 | [diff] [blame] | 263 | int rc = req->r_result <= 0 ? req->r_result : 0; |
| 264 | int bytes = req->r_result >= 0 ? req->r_result : 0; |
Alex Elder | e0c5948 | 2013-03-07 15:38:25 -0600 | [diff] [blame] | 265 | int num_pages; |
Sage Weil | 7c27219 | 2011-08-03 09:58:09 -0700 | [diff] [blame] | 266 | int i; |
| 267 | |
Sage Weil | 7c27219 | 2011-08-03 09:58:09 -0700 | [diff] [blame] | 268 | dout("finish_read %p req %p rc %d bytes %d\n", inode, req, rc, bytes); |
| 269 | |
| 270 | /* unlock all pages, zeroing any data we didn't read */ |
Alex Elder | 406e2c9 | 2013-04-15 14:50:36 -0500 | [diff] [blame] | 271 | osd_data = osd_req_op_extent_osd_data(req, 0); |
Alex Elder | 87060c1 | 2013-04-03 01:28:58 -0500 | [diff] [blame] | 272 | BUG_ON(osd_data->type != CEPH_OSD_DATA_TYPE_PAGES); |
| 273 | num_pages = calc_pages_for((u64)osd_data->alignment, |
| 274 | (u64)osd_data->length); |
Alex Elder | e0c5948 | 2013-03-07 15:38:25 -0600 | [diff] [blame] | 275 | for (i = 0; i < num_pages; i++) { |
Alex Elder | 87060c1 | 2013-04-03 01:28:58 -0500 | [diff] [blame] | 276 | struct page *page = osd_data->pages[i]; |
Sage Weil | 7c27219 | 2011-08-03 09:58:09 -0700 | [diff] [blame] | 277 | |
Yan, Zheng | 368e358 | 2016-05-17 11:58:02 +0800 | [diff] [blame] | 278 | if (rc < 0 && rc != -ENOENT) { |
| 279 | ceph_fscache_readpage_cancel(inode, page); |
Li Wang | f36132a | 2013-11-27 22:28:13 +0800 | [diff] [blame] | 280 | goto unlock; |
Yan, Zheng | 368e358 | 2016-05-17 11:58:02 +0800 | [diff] [blame] | 281 | } |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 282 | if (bytes < (int)PAGE_SIZE) { |
Sage Weil | 7c27219 | 2011-08-03 09:58:09 -0700 | [diff] [blame] | 283 | /* zero (remainder of) page */ |
| 284 | int s = bytes < 0 ? 0 : bytes; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 285 | zero_user_segment(page, s, PAGE_SIZE); |
Sage Weil | 7c27219 | 2011-08-03 09:58:09 -0700 | [diff] [blame] | 286 | } |
| 287 | dout("finish_read %p uptodate %p idx %lu\n", inode, page, |
| 288 | page->index); |
| 289 | flush_dcache_page(page); |
| 290 | SetPageUptodate(page); |
Milosz Tanski | 99ccbd2 | 2013-08-21 17:29:54 -0400 | [diff] [blame] | 291 | ceph_readpage_to_fscache(inode, page); |
Li Wang | f36132a | 2013-11-27 22:28:13 +0800 | [diff] [blame] | 292 | unlock: |
Sage Weil | 7c27219 | 2011-08-03 09:58:09 -0700 | [diff] [blame] | 293 | unlock_page(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 294 | put_page(page); |
| 295 | bytes -= PAGE_SIZE; |
Sage Weil | 7c27219 | 2011-08-03 09:58:09 -0700 | [diff] [blame] | 296 | } |
Alex Elder | 87060c1 | 2013-04-03 01:28:58 -0500 | [diff] [blame] | 297 | kfree(osd_data->pages); |
Sage Weil | 7c27219 | 2011-08-03 09:58:09 -0700 | [diff] [blame] | 298 | } |
| 299 | |
| 300 | /* |
| 301 | * start an async read(ahead) operation. return nr_pages we submitted |
| 302 | * a read for on success, or negative error code. |
| 303 | */ |
Sage Weil | 0d66a48 | 2011-08-04 08:21:30 -0700 | [diff] [blame] | 304 | static int start_read(struct inode *inode, struct list_head *page_list, int max) |
Sage Weil | 7c27219 | 2011-08-03 09:58:09 -0700 | [diff] [blame] | 305 | { |
| 306 | struct ceph_osd_client *osdc = |
| 307 | &ceph_inode_to_client(inode)->client->osdc; |
| 308 | struct ceph_inode_info *ci = ceph_inode(inode); |
| 309 | struct page *page = list_entry(page_list->prev, struct page, lru); |
Alex Elder | acead00 | 2013-03-14 14:09:05 -0500 | [diff] [blame] | 310 | struct ceph_vino vino; |
Sage Weil | 7c27219 | 2011-08-03 09:58:09 -0700 | [diff] [blame] | 311 | struct ceph_osd_request *req; |
| 312 | u64 off; |
| 313 | u64 len; |
| 314 | int i; |
Sage Weil | 1d3576f | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 315 | struct page **pages; |
Sage Weil | 7c27219 | 2011-08-03 09:58:09 -0700 | [diff] [blame] | 316 | pgoff_t next_index; |
| 317 | int nr_pages = 0; |
Yan, Zheng | e99b0ea | 2016-10-25 10:51:55 +0800 | [diff] [blame] | 318 | int got = 0; |
| 319 | int ret = 0; |
| 320 | |
| 321 | if (!current->journal_info) { |
| 322 | /* caller of readpages does not hold buffer and read caps |
| 323 | * (fadvise, madvise and readahead cases) */ |
| 324 | int want = CEPH_CAP_FILE_CACHE; |
| 325 | ret = ceph_try_get_caps(ci, CEPH_CAP_FILE_RD, want, &got); |
| 326 | if (ret < 0) { |
| 327 | dout("start_read %p, error getting cap\n", inode); |
| 328 | } else if (!(got & want)) { |
| 329 | dout("start_read %p, no cache cap\n", inode); |
| 330 | ret = 0; |
| 331 | } |
| 332 | if (ret <= 0) { |
| 333 | if (got) |
| 334 | ceph_put_cap_refs(ci, got); |
| 335 | while (!list_empty(page_list)) { |
| 336 | page = list_entry(page_list->prev, |
| 337 | struct page, lru); |
| 338 | list_del(&page->lru); |
| 339 | put_page(page); |
| 340 | } |
| 341 | return ret; |
| 342 | } |
| 343 | } |
Sage Weil | 7c27219 | 2011-08-03 09:58:09 -0700 | [diff] [blame] | 344 | |
Alex Elder | 6285bc2 | 2012-10-02 10:25:51 -0500 | [diff] [blame] | 345 | off = (u64) page_offset(page); |
Sage Weil | 7c27219 | 2011-08-03 09:58:09 -0700 | [diff] [blame] | 346 | |
| 347 | /* count pages */ |
| 348 | next_index = page->index; |
| 349 | list_for_each_entry_reverse(page, page_list, lru) { |
| 350 | if (page->index != next_index) |
| 351 | break; |
| 352 | nr_pages++; |
| 353 | next_index++; |
Sage Weil | 0d66a48 | 2011-08-04 08:21:30 -0700 | [diff] [blame] | 354 | if (max && nr_pages == max) |
| 355 | break; |
Sage Weil | 7c27219 | 2011-08-03 09:58:09 -0700 | [diff] [blame] | 356 | } |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 357 | len = nr_pages << PAGE_SHIFT; |
Sage Weil | 7c27219 | 2011-08-03 09:58:09 -0700 | [diff] [blame] | 358 | dout("start_read %p nr_pages %d is %lld~%lld\n", inode, nr_pages, |
| 359 | off, len); |
Alex Elder | acead00 | 2013-03-14 14:09:05 -0500 | [diff] [blame] | 360 | vino = ceph_vino(inode); |
| 361 | req = ceph_osdc_new_request(osdc, &ci->i_layout, vino, off, &len, |
Yan, Zheng | 715e4cd | 2014-11-13 14:40:37 +0800 | [diff] [blame] | 362 | 0, 1, CEPH_OSD_OP_READ, |
Alex Elder | acead00 | 2013-03-14 14:09:05 -0500 | [diff] [blame] | 363 | CEPH_OSD_FLAG_READ, NULL, |
Sage Weil | 7c27219 | 2011-08-03 09:58:09 -0700 | [diff] [blame] | 364 | ci->i_truncate_seq, ci->i_truncate_size, |
Alex Elder | acead00 | 2013-03-14 14:09:05 -0500 | [diff] [blame] | 365 | false); |
Yan, Zheng | e99b0ea | 2016-10-25 10:51:55 +0800 | [diff] [blame] | 366 | if (IS_ERR(req)) { |
| 367 | ret = PTR_ERR(req); |
| 368 | goto out; |
| 369 | } |
Sage Weil | 1d3576f | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 370 | |
| 371 | /* build page vector */ |
Alex Elder | cf7b7e1 | 2013-03-01 18:00:15 -0600 | [diff] [blame] | 372 | nr_pages = calc_pages_for(0, len); |
Yan, Zheng | 687265e | 2015-06-13 17:27:05 +0800 | [diff] [blame] | 373 | pages = kmalloc(sizeof(*pages) * nr_pages, GFP_KERNEL); |
Yan, Zheng | e99b0ea | 2016-10-25 10:51:55 +0800 | [diff] [blame] | 374 | if (!pages) { |
| 375 | ret = -ENOMEM; |
| 376 | goto out_put; |
| 377 | } |
Sage Weil | 7c27219 | 2011-08-03 09:58:09 -0700 | [diff] [blame] | 378 | for (i = 0; i < nr_pages; ++i) { |
| 379 | page = list_entry(page_list->prev, struct page, lru); |
| 380 | BUG_ON(PageLocked(page)); |
| 381 | list_del(&page->lru); |
Milosz Tanski | 99ccbd2 | 2013-08-21 17:29:54 -0400 | [diff] [blame] | 382 | |
Sage Weil | 7c27219 | 2011-08-03 09:58:09 -0700 | [diff] [blame] | 383 | dout("start_read %p adding %p idx %lu\n", inode, page, |
| 384 | page->index); |
| 385 | if (add_to_page_cache_lru(page, &inode->i_data, page->index, |
Yan, Zheng | 687265e | 2015-06-13 17:27:05 +0800 | [diff] [blame] | 386 | GFP_KERNEL)) { |
Milosz Tanski | d4d3aa3 | 2013-09-03 19:11:17 -0400 | [diff] [blame] | 387 | ceph_fscache_uncache_page(inode, page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 388 | put_page(page); |
Sage Weil | 7c27219 | 2011-08-03 09:58:09 -0700 | [diff] [blame] | 389 | dout("start_read %p add_to_page_cache failed %p\n", |
| 390 | inode, page); |
| 391 | nr_pages = i; |
Yan, Zheng | 1afe478 | 2016-08-24 11:33:46 +0800 | [diff] [blame] | 392 | if (nr_pages > 0) { |
| 393 | len = nr_pages << PAGE_SHIFT; |
Yan, Zheng | dc8470f | 2017-01-19 11:21:29 +0800 | [diff] [blame] | 394 | osd_req_op_extent_update(req, 0, len); |
Yan, Zheng | 1afe478 | 2016-08-24 11:33:46 +0800 | [diff] [blame] | 395 | break; |
| 396 | } |
Sage Weil | 7c27219 | 2011-08-03 09:58:09 -0700 | [diff] [blame] | 397 | goto out_pages; |
Sage Weil | 1d3576f | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 398 | } |
Sage Weil | 7c27219 | 2011-08-03 09:58:09 -0700 | [diff] [blame] | 399 | pages[i] = page; |
Sage Weil | 1d3576f | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 400 | } |
Alex Elder | 406e2c9 | 2013-04-15 14:50:36 -0500 | [diff] [blame] | 401 | osd_req_op_extent_osd_data_pages(req, 0, pages, len, 0, false, false); |
Sage Weil | 7c27219 | 2011-08-03 09:58:09 -0700 | [diff] [blame] | 402 | req->r_callback = finish_read; |
| 403 | req->r_inode = inode; |
| 404 | |
| 405 | dout("start_read %p starting %p %lld~%lld\n", inode, req, off, len); |
| 406 | ret = ceph_osdc_start_request(osdc, req, false); |
| 407 | if (ret < 0) |
| 408 | goto out_pages; |
| 409 | ceph_osdc_put_request(req); |
Yan, Zheng | e99b0ea | 2016-10-25 10:51:55 +0800 | [diff] [blame] | 410 | |
| 411 | /* After adding locked pages to page cache, the inode holds cache cap. |
| 412 | * So we can drop our cap refs. */ |
| 413 | if (got) |
| 414 | ceph_put_cap_refs(ci, got); |
| 415 | |
Sage Weil | 7c27219 | 2011-08-03 09:58:09 -0700 | [diff] [blame] | 416 | return nr_pages; |
| 417 | |
| 418 | out_pages: |
Yan, Zheng | 1afe478 | 2016-08-24 11:33:46 +0800 | [diff] [blame] | 419 | for (i = 0; i < nr_pages; ++i) { |
| 420 | ceph_fscache_readpage_cancel(inode, pages[i]); |
| 421 | unlock_page(pages[i]); |
| 422 | } |
| 423 | ceph_put_page_vector(pages, nr_pages, false); |
Yan, Zheng | e99b0ea | 2016-10-25 10:51:55 +0800 | [diff] [blame] | 424 | out_put: |
Sage Weil | 7c27219 | 2011-08-03 09:58:09 -0700 | [diff] [blame] | 425 | ceph_osdc_put_request(req); |
Yan, Zheng | e99b0ea | 2016-10-25 10:51:55 +0800 | [diff] [blame] | 426 | out: |
| 427 | if (got) |
| 428 | ceph_put_cap_refs(ci, got); |
Sage Weil | 7c27219 | 2011-08-03 09:58:09 -0700 | [diff] [blame] | 429 | return ret; |
Sage Weil | 1d3576f | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 430 | } |
| 431 | |
Sage Weil | 7c27219 | 2011-08-03 09:58:09 -0700 | [diff] [blame] | 432 | |
Sage Weil | 1d3576f | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 433 | /* |
| 434 | * Read multiple pages. Leave pages we don't read + unlock in page_list; |
| 435 | * the caller (VM) cleans them up. |
| 436 | */ |
| 437 | static int ceph_readpages(struct file *file, struct address_space *mapping, |
| 438 | struct list_head *page_list, unsigned nr_pages) |
| 439 | { |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 440 | struct inode *inode = file_inode(file); |
Sage Weil | 0d66a48 | 2011-08-04 08:21:30 -0700 | [diff] [blame] | 441 | struct ceph_fs_client *fsc = ceph_inode_to_client(inode); |
Sage Weil | 1d3576f | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 442 | int rc = 0; |
Sage Weil | 0d66a48 | 2011-08-04 08:21:30 -0700 | [diff] [blame] | 443 | int max = 0; |
Sage Weil | 1d3576f | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 444 | |
Yan, Zheng | 8370124 | 2014-11-14 22:36:18 +0800 | [diff] [blame] | 445 | if (ceph_inode(inode)->i_inline_version != CEPH_INLINE_NONE) |
| 446 | return -EINVAL; |
| 447 | |
Milosz Tanski | 99ccbd2 | 2013-08-21 17:29:54 -0400 | [diff] [blame] | 448 | rc = ceph_readpages_from_fscache(mapping->host, mapping, page_list, |
| 449 | &nr_pages); |
| 450 | |
| 451 | if (rc == 0) |
| 452 | goto out; |
| 453 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 454 | if (fsc->mount_options->rsize >= PAGE_SIZE) |
| 455 | max = (fsc->mount_options->rsize + PAGE_SIZE - 1) |
Sage Weil | 0d66a48 | 2011-08-04 08:21:30 -0700 | [diff] [blame] | 456 | >> PAGE_SHIFT; |
| 457 | |
Alex Elder | 2794a82 | 2013-02-14 12:16:43 -0600 | [diff] [blame] | 458 | dout("readpages %p file %p nr_pages %d max %d\n", inode, |
| 459 | file, nr_pages, |
Sage Weil | 0d66a48 | 2011-08-04 08:21:30 -0700 | [diff] [blame] | 460 | max); |
Sage Weil | 7c27219 | 2011-08-03 09:58:09 -0700 | [diff] [blame] | 461 | while (!list_empty(page_list)) { |
Sage Weil | 0d66a48 | 2011-08-04 08:21:30 -0700 | [diff] [blame] | 462 | rc = start_read(inode, page_list, max); |
Sage Weil | 7c27219 | 2011-08-03 09:58:09 -0700 | [diff] [blame] | 463 | if (rc < 0) |
| 464 | goto out; |
Sage Weil | 1d3576f | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 465 | } |
Sage Weil | 1d3576f | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 466 | out: |
Milosz Tanski | 76be778 | 2013-08-21 17:30:27 -0400 | [diff] [blame] | 467 | ceph_fscache_readpages_cancel(inode, page_list); |
| 468 | |
Sage Weil | 7c27219 | 2011-08-03 09:58:09 -0700 | [diff] [blame] | 469 | dout("readpages %p file %p ret %d\n", inode, file, rc); |
Sage Weil | 1d3576f | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 470 | return rc; |
| 471 | } |
| 472 | |
| 473 | /* |
| 474 | * Get ref for the oldest snapc for an inode with dirty data... that is, the |
| 475 | * only snap context we are allowed to write back. |
Sage Weil | 1d3576f | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 476 | */ |
Sage Weil | 6298a33 | 2010-03-31 22:01:38 -0700 | [diff] [blame] | 477 | static struct ceph_snap_context *get_oldest_context(struct inode *inode, |
Yan, Zheng | e1966b4 | 2015-06-18 03:10:58 +0800 | [diff] [blame] | 478 | loff_t *snap_size) |
Sage Weil | 1d3576f | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 479 | { |
| 480 | struct ceph_inode_info *ci = ceph_inode(inode); |
| 481 | struct ceph_snap_context *snapc = NULL; |
| 482 | struct ceph_cap_snap *capsnap = NULL; |
| 483 | |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 484 | spin_lock(&ci->i_ceph_lock); |
Sage Weil | 1d3576f | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 485 | list_for_each_entry(capsnap, &ci->i_cap_snaps, ci_item) { |
| 486 | dout(" cap_snap %p snapc %p has %d dirty pages\n", capsnap, |
| 487 | capsnap->context, capsnap->dirty_pages); |
| 488 | if (capsnap->dirty_pages) { |
| 489 | snapc = ceph_get_snap_context(capsnap->context); |
| 490 | if (snap_size) |
| 491 | *snap_size = capsnap->size; |
| 492 | break; |
| 493 | } |
| 494 | } |
Sage Weil | 7d8cb26 | 2010-08-24 08:44:16 -0700 | [diff] [blame] | 495 | if (!snapc && ci->i_wrbuffer_ref_head) { |
Sage Weil | 80e755f | 2010-03-31 21:52:10 -0700 | [diff] [blame] | 496 | snapc = ceph_get_snap_context(ci->i_head_snapc); |
Sage Weil | 1d3576f | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 497 | dout(" head snapc %p has %d dirty pages\n", |
| 498 | snapc, ci->i_wrbuffer_ref_head); |
| 499 | } |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 500 | spin_unlock(&ci->i_ceph_lock); |
Sage Weil | 1d3576f | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 501 | return snapc; |
| 502 | } |
| 503 | |
| 504 | /* |
| 505 | * Write a single page, but leave the page locked. |
| 506 | * |
| 507 | * If we get a write error, set the page error bit, but still adjust the |
| 508 | * dirty page accounting (i.e., page is no longer dirty). |
| 509 | */ |
| 510 | static int writepage_nounlock(struct page *page, struct writeback_control *wbc) |
| 511 | { |
| 512 | struct inode *inode; |
| 513 | struct ceph_inode_info *ci; |
Yehuda Sadeh | 3d14c5d | 2010-04-06 15:14:15 -0700 | [diff] [blame] | 514 | struct ceph_fs_client *fsc; |
Sage Weil | 1d3576f | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 515 | struct ceph_osd_client *osdc; |
Sage Weil | 6298a33 | 2010-03-31 22:01:38 -0700 | [diff] [blame] | 516 | struct ceph_snap_context *snapc, *oldest; |
Yan, Zheng | fc2744a | 2013-05-31 16:48:29 +0800 | [diff] [blame] | 517 | loff_t page_off = page_offset(page); |
Yan, Zheng | e1966b4 | 2015-06-18 03:10:58 +0800 | [diff] [blame] | 518 | loff_t snap_size = -1; |
Yehuda Sadeh | 2baba25 | 2009-12-18 13:51:57 -0800 | [diff] [blame] | 519 | long writeback_stat; |
Yan, Zheng | e1966b4 | 2015-06-18 03:10:58 +0800 | [diff] [blame] | 520 | u64 truncate_size; |
Yan, Zheng | fc2744a | 2013-05-31 16:48:29 +0800 | [diff] [blame] | 521 | u32 truncate_seq; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 522 | int err = 0, len = PAGE_SIZE; |
Sage Weil | 1d3576f | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 523 | |
| 524 | dout("writepage %p idx %lu\n", page, page->index); |
| 525 | |
| 526 | if (!page->mapping || !page->mapping->host) { |
| 527 | dout("writepage %p - no mapping\n", page); |
| 528 | return -EFAULT; |
| 529 | } |
| 530 | inode = page->mapping->host; |
| 531 | ci = ceph_inode(inode); |
Yehuda Sadeh | 3d14c5d | 2010-04-06 15:14:15 -0700 | [diff] [blame] | 532 | fsc = ceph_inode_to_client(inode); |
| 533 | osdc = &fsc->client->osdc; |
Sage Weil | 1d3576f | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 534 | |
| 535 | /* verify this is a writeable snap context */ |
Yan, Zheng | 61600ef | 2012-05-28 14:44:30 +0800 | [diff] [blame] | 536 | snapc = page_snap_context(page); |
Sage Weil | 1d3576f | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 537 | if (snapc == NULL) { |
| 538 | dout("writepage %p page %p not dirty?\n", inode, page); |
| 539 | goto out; |
| 540 | } |
Sage Weil | 6298a33 | 2010-03-31 22:01:38 -0700 | [diff] [blame] | 541 | oldest = get_oldest_context(inode, &snap_size); |
| 542 | if (snapc->seq > oldest->seq) { |
Sage Weil | 1d3576f | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 543 | dout("writepage %p page %p snapc %p not writeable - noop\n", |
Yan, Zheng | 61600ef | 2012-05-28 14:44:30 +0800 | [diff] [blame] | 544 | inode, page, snapc); |
Sage Weil | 1d3576f | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 545 | /* we should only noop if called by kswapd */ |
| 546 | WARN_ON((current->flags & PF_MEMALLOC) == 0); |
Sage Weil | 6298a33 | 2010-03-31 22:01:38 -0700 | [diff] [blame] | 547 | ceph_put_snap_context(oldest); |
Sage Weil | 1d3576f | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 548 | goto out; |
| 549 | } |
Sage Weil | 6298a33 | 2010-03-31 22:01:38 -0700 | [diff] [blame] | 550 | ceph_put_snap_context(oldest); |
Sage Weil | 1d3576f | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 551 | |
Yan, Zheng | fc2744a | 2013-05-31 16:48:29 +0800 | [diff] [blame] | 552 | spin_lock(&ci->i_ceph_lock); |
| 553 | truncate_seq = ci->i_truncate_seq; |
| 554 | truncate_size = ci->i_truncate_size; |
Yan, Zheng | e1966b4 | 2015-06-18 03:10:58 +0800 | [diff] [blame] | 555 | if (snap_size == -1) |
Yan, Zheng | fc2744a | 2013-05-31 16:48:29 +0800 | [diff] [blame] | 556 | snap_size = i_size_read(inode); |
| 557 | spin_unlock(&ci->i_ceph_lock); |
| 558 | |
Sage Weil | 1d3576f | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 559 | /* is this a partial page at end of file? */ |
Yan, Zheng | fc2744a | 2013-05-31 16:48:29 +0800 | [diff] [blame] | 560 | if (page_off >= snap_size) { |
| 561 | dout("%p page eof %llu\n", page, snap_size); |
| 562 | goto out; |
| 563 | } |
| 564 | if (snap_size < page_off + len) |
| 565 | len = snap_size - page_off; |
Sage Weil | 1d3576f | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 566 | |
Sage Weil | ae00d4f | 2010-09-16 16:26:51 -0700 | [diff] [blame] | 567 | dout("writepage %p page %p index %lu on %llu~%u snapc %p\n", |
| 568 | inode, page, page->index, page_off, len, snapc); |
Sage Weil | 1d3576f | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 569 | |
Yehuda Sadeh | 3d14c5d | 2010-04-06 15:14:15 -0700 | [diff] [blame] | 570 | writeback_stat = atomic_long_inc_return(&fsc->writeback_count); |
Yehuda Sadeh | 2baba25 | 2009-12-18 13:51:57 -0800 | [diff] [blame] | 571 | if (writeback_stat > |
Yehuda Sadeh | 3d14c5d | 2010-04-06 15:14:15 -0700 | [diff] [blame] | 572 | CONGESTION_ON_THRESH(fsc->mount_options->congestion_kb)) |
| 573 | set_bdi_congested(&fsc->backing_dev_info, BLK_RW_ASYNC); |
Yehuda Sadeh | 2baba25 | 2009-12-18 13:51:57 -0800 | [diff] [blame] | 574 | |
Sage Weil | 1d3576f | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 575 | set_page_writeback(page); |
| 576 | err = ceph_osdc_writepages(osdc, ceph_vino(inode), |
| 577 | &ci->i_layout, snapc, |
| 578 | page_off, len, |
Yan, Zheng | fc2744a | 2013-05-31 16:48:29 +0800 | [diff] [blame] | 579 | truncate_seq, truncate_size, |
Alex Elder | 2480882 | 2013-02-15 11:42:29 -0600 | [diff] [blame] | 580 | &inode->i_mtime, &page, 1); |
Sage Weil | 1d3576f | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 581 | if (err < 0) { |
Yan, Zheng | ad15ec0 | 2016-05-13 17:29:51 +0800 | [diff] [blame] | 582 | struct writeback_control tmp_wbc; |
| 583 | if (!wbc) |
| 584 | wbc = &tmp_wbc; |
| 585 | if (err == -ERESTARTSYS) { |
| 586 | /* killed by SIGKILL */ |
| 587 | dout("writepage interrupted page %p\n", page); |
| 588 | redirty_page_for_writepage(wbc, page); |
| 589 | end_page_writeback(page); |
| 590 | goto out; |
| 591 | } |
| 592 | dout("writepage setting page/mapping error %d %p\n", |
| 593 | err, page); |
Sage Weil | 1d3576f | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 594 | SetPageError(page); |
| 595 | mapping_set_error(&inode->i_data, err); |
Yan, Zheng | ad15ec0 | 2016-05-13 17:29:51 +0800 | [diff] [blame] | 596 | wbc->pages_skipped++; |
Sage Weil | 1d3576f | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 597 | } else { |
| 598 | dout("writepage cleaned page %p\n", page); |
| 599 | err = 0; /* vfs expects us to return 0 */ |
| 600 | } |
| 601 | page->private = 0; |
| 602 | ClearPagePrivate(page); |
| 603 | end_page_writeback(page); |
| 604 | ceph_put_wrbuffer_cap_refs(ci, 1, snapc); |
Sage Weil | 6298a33 | 2010-03-31 22:01:38 -0700 | [diff] [blame] | 605 | ceph_put_snap_context(snapc); /* page's reference */ |
Sage Weil | 1d3576f | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 606 | out: |
| 607 | return err; |
| 608 | } |
| 609 | |
| 610 | static int ceph_writepage(struct page *page, struct writeback_control *wbc) |
| 611 | { |
Yehuda Sadeh | dbd646a | 2009-12-16 14:51:06 -0800 | [diff] [blame] | 612 | int err; |
| 613 | struct inode *inode = page->mapping->host; |
| 614 | BUG_ON(!inode); |
Sage Weil | 70b666c | 2011-05-27 09:24:26 -0700 | [diff] [blame] | 615 | ihold(inode); |
Yehuda Sadeh | dbd646a | 2009-12-16 14:51:06 -0800 | [diff] [blame] | 616 | err = writepage_nounlock(page, wbc); |
Yan, Zheng | ad15ec0 | 2016-05-13 17:29:51 +0800 | [diff] [blame] | 617 | if (err == -ERESTARTSYS) { |
| 618 | /* direct memory reclaimer was killed by SIGKILL. return 0 |
| 619 | * to prevent caller from setting mapping/page error */ |
| 620 | err = 0; |
| 621 | } |
Sage Weil | 1d3576f | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 622 | unlock_page(page); |
Yehuda Sadeh | dbd646a | 2009-12-16 14:51:06 -0800 | [diff] [blame] | 623 | iput(inode); |
Sage Weil | 1d3576f | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 624 | return err; |
| 625 | } |
| 626 | |
Sage Weil | 1d3576f | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 627 | /* |
| 628 | * lame release_pages helper. release_pages() isn't exported to |
| 629 | * modules. |
| 630 | */ |
| 631 | static void ceph_release_pages(struct page **pages, int num) |
| 632 | { |
| 633 | struct pagevec pvec; |
| 634 | int i; |
| 635 | |
| 636 | pagevec_init(&pvec, 0); |
| 637 | for (i = 0; i < num; i++) { |
| 638 | if (pagevec_add(&pvec, pages[i]) == 0) |
| 639 | pagevec_release(&pvec); |
| 640 | } |
| 641 | pagevec_release(&pvec); |
| 642 | } |
| 643 | |
Sage Weil | 1d3576f | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 644 | /* |
| 645 | * async writeback completion handler. |
| 646 | * |
| 647 | * If we get an error, set the mapping error bit, but not the individual |
| 648 | * page error bits. |
| 649 | */ |
Ilya Dryomov | 85e084f | 2016-04-28 16:07:24 +0200 | [diff] [blame] | 650 | static void writepages_finish(struct ceph_osd_request *req) |
Sage Weil | 1d3576f | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 651 | { |
| 652 | struct inode *inode = req->r_inode; |
Sage Weil | 1d3576f | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 653 | struct ceph_inode_info *ci = ceph_inode(inode); |
Alex Elder | 87060c1 | 2013-04-03 01:28:58 -0500 | [diff] [blame] | 654 | struct ceph_osd_data *osd_data; |
Sage Weil | 1d3576f | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 655 | struct page *page; |
Yan, Zheng | 5b64640 | 2016-01-07 16:00:17 +0800 | [diff] [blame] | 656 | int num_pages, total_pages = 0; |
| 657 | int i, j; |
| 658 | int rc = req->r_result; |
Sage Weil | 1d3576f | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 659 | struct ceph_snap_context *snapc = req->r_snapc; |
| 660 | struct address_space *mapping = inode->i_mapping; |
Yehuda Sadeh | 3d14c5d | 2010-04-06 15:14:15 -0700 | [diff] [blame] | 661 | struct ceph_fs_client *fsc = ceph_inode_to_client(inode); |
Yan, Zheng | 5b64640 | 2016-01-07 16:00:17 +0800 | [diff] [blame] | 662 | bool remove_page; |
Sage Weil | 1d3576f | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 663 | |
Yan, Zheng | 5b64640 | 2016-01-07 16:00:17 +0800 | [diff] [blame] | 664 | dout("writepages_finish %p rc %d\n", inode, rc); |
| 665 | if (rc < 0) |
Sage Weil | 1d3576f | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 666 | mapping_set_error(mapping, rc); |
Yan, Zheng | 5b64640 | 2016-01-07 16:00:17 +0800 | [diff] [blame] | 667 | |
| 668 | /* |
| 669 | * We lost the cache cap, need to truncate the page before |
| 670 | * it is unlocked, otherwise we'd truncate it later in the |
| 671 | * page truncation thread, possibly losing some data that |
| 672 | * raced its way in |
| 673 | */ |
| 674 | remove_page = !(ceph_caps_issued(ci) & |
| 675 | (CEPH_CAP_FILE_CACHE|CEPH_CAP_FILE_LAZYIO)); |
Sage Weil | 1d3576f | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 676 | |
| 677 | /* clean all pages */ |
Yan, Zheng | 5b64640 | 2016-01-07 16:00:17 +0800 | [diff] [blame] | 678 | for (i = 0; i < req->r_num_ops; i++) { |
| 679 | if (req->r_ops[i].op != CEPH_OSD_OP_WRITE) |
| 680 | break; |
Sage Weil | 1d3576f | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 681 | |
Yan, Zheng | 5b64640 | 2016-01-07 16:00:17 +0800 | [diff] [blame] | 682 | osd_data = osd_req_op_extent_osd_data(req, i); |
| 683 | BUG_ON(osd_data->type != CEPH_OSD_DATA_TYPE_PAGES); |
| 684 | num_pages = calc_pages_for((u64)osd_data->alignment, |
| 685 | (u64)osd_data->length); |
| 686 | total_pages += num_pages; |
| 687 | for (j = 0; j < num_pages; j++) { |
| 688 | page = osd_data->pages[j]; |
| 689 | BUG_ON(!page); |
| 690 | WARN_ON(!PageUptodate(page)); |
Yehuda Sadeh | 2baba25 | 2009-12-18 13:51:57 -0800 | [diff] [blame] | 691 | |
Yan, Zheng | 5b64640 | 2016-01-07 16:00:17 +0800 | [diff] [blame] | 692 | if (atomic_long_dec_return(&fsc->writeback_count) < |
| 693 | CONGESTION_OFF_THRESH( |
| 694 | fsc->mount_options->congestion_kb)) |
| 695 | clear_bdi_congested(&fsc->backing_dev_info, |
| 696 | BLK_RW_ASYNC); |
Yehuda Sadeh | e63dc5c | 2010-02-19 00:07:01 +0000 | [diff] [blame] | 697 | |
Yan, Zheng | b109eec6 | 2016-05-13 17:54:17 +0800 | [diff] [blame] | 698 | if (rc < 0) |
| 699 | SetPageError(page); |
| 700 | |
Yan, Zheng | 5b64640 | 2016-01-07 16:00:17 +0800 | [diff] [blame] | 701 | ceph_put_snap_context(page_snap_context(page)); |
| 702 | page->private = 0; |
| 703 | ClearPagePrivate(page); |
| 704 | dout("unlocking %p\n", page); |
| 705 | end_page_writeback(page); |
Yehuda Sadeh | e63dc5c | 2010-02-19 00:07:01 +0000 | [diff] [blame] | 706 | |
Yan, Zheng | 5b64640 | 2016-01-07 16:00:17 +0800 | [diff] [blame] | 707 | if (remove_page) |
| 708 | generic_error_remove_page(inode->i_mapping, |
| 709 | page); |
| 710 | |
| 711 | unlock_page(page); |
| 712 | } |
| 713 | dout("writepages_finish %p wrote %llu bytes cleaned %d pages\n", |
| 714 | inode, osd_data->length, rc >= 0 ? num_pages : 0); |
| 715 | |
| 716 | ceph_release_pages(osd_data->pages, num_pages); |
Sage Weil | 1d3576f | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 717 | } |
Sage Weil | 1d3576f | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 718 | |
Yan, Zheng | 5b64640 | 2016-01-07 16:00:17 +0800 | [diff] [blame] | 719 | ceph_put_wrbuffer_cap_refs(ci, total_pages, snapc); |
| 720 | |
| 721 | osd_data = osd_req_op_extent_osd_data(req, 0); |
Alex Elder | 87060c1 | 2013-04-03 01:28:58 -0500 | [diff] [blame] | 722 | if (osd_data->pages_from_pool) |
| 723 | mempool_free(osd_data->pages, |
Cheng Renquan | 640ef79 | 2010-03-26 17:40:33 +0800 | [diff] [blame] | 724 | ceph_sb_to_client(inode->i_sb)->wb_pagevec_pool); |
Sage Weil | 1d3576f | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 725 | else |
Alex Elder | 87060c1 | 2013-04-03 01:28:58 -0500 | [diff] [blame] | 726 | kfree(osd_data->pages); |
Sage Weil | 1d3576f | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 727 | ceph_osdc_put_request(req); |
| 728 | } |
| 729 | |
Sage Weil | 1d3576f | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 730 | /* |
| 731 | * initiate async writeback |
| 732 | */ |
| 733 | static int ceph_writepages_start(struct address_space *mapping, |
| 734 | struct writeback_control *wbc) |
| 735 | { |
| 736 | struct inode *inode = mapping->host; |
Sage Weil | 1d3576f | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 737 | struct ceph_inode_info *ci = ceph_inode(inode); |
Yan, Zheng | fc2744a | 2013-05-31 16:48:29 +0800 | [diff] [blame] | 738 | struct ceph_fs_client *fsc = ceph_inode_to_client(inode); |
| 739 | struct ceph_vino vino = ceph_vino(inode); |
Sage Weil | 1d3576f | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 740 | pgoff_t index, start, end; |
| 741 | int range_whole = 0; |
| 742 | int should_loop = 1; |
| 743 | pgoff_t max_pages = 0, max_pages_ever = 0; |
Sage Weil | 80e755f | 2010-03-31 21:52:10 -0700 | [diff] [blame] | 744 | struct ceph_snap_context *snapc = NULL, *last_snapc = NULL, *pgsnapc; |
Sage Weil | 1d3576f | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 745 | struct pagevec pvec; |
| 746 | int done = 0; |
| 747 | int rc = 0; |
Fabian Frederick | 61604a2 | 2017-02-27 14:28:32 -0800 | [diff] [blame] | 748 | unsigned int wsize = i_blocksize(inode); |
Sage Weil | 1d3576f | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 749 | struct ceph_osd_request *req = NULL; |
Dan Carpenter | 021b77b | 2014-11-28 11:33:34 +0300 | [diff] [blame] | 750 | int do_sync = 0; |
Yan, Zheng | e1966b4 | 2015-06-18 03:10:58 +0800 | [diff] [blame] | 751 | loff_t snap_size, i_size; |
| 752 | u64 truncate_size; |
Yan, Zheng | fc2744a | 2013-05-31 16:48:29 +0800 | [diff] [blame] | 753 | u32 truncate_seq; |
Sage Weil | 1d3576f | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 754 | |
| 755 | /* |
| 756 | * Include a 'sync' in the OSD request if this is a data |
| 757 | * integrity write (e.g., O_SYNC write or fsync()), or if our |
| 758 | * cap is being revoked. |
| 759 | */ |
majianpeng | c62988e | 2013-06-19 15:12:06 +0800 | [diff] [blame] | 760 | if ((wbc->sync_mode == WB_SYNC_ALL) || |
| 761 | ceph_caps_revoking(ci, CEPH_CAP_FILE_BUFFER)) |
Sage Weil | 1d3576f | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 762 | do_sync = 1; |
| 763 | dout("writepages_start %p dosync=%d (mode=%s)\n", |
| 764 | inode, do_sync, |
| 765 | wbc->sync_mode == WB_SYNC_NONE ? "NONE" : |
| 766 | (wbc->sync_mode == WB_SYNC_ALL ? "ALL" : "HOLD")); |
| 767 | |
Yan, Zheng | 48fec5d | 2015-07-01 16:27:46 +0800 | [diff] [blame] | 768 | if (ACCESS_ONCE(fsc->mount_state) == CEPH_MOUNT_SHUTDOWN) { |
Yan, Zheng | 6c93df5 | 2016-04-15 13:56:12 +0800 | [diff] [blame] | 769 | if (ci->i_wrbuffer_ref > 0) { |
| 770 | pr_warn_ratelimited( |
| 771 | "writepage_start %p %lld forced umount\n", |
| 772 | inode, ceph_ino(inode)); |
| 773 | } |
Yan, Zheng | a341d4d | 2015-07-01 17:03:23 +0800 | [diff] [blame] | 774 | mapping_set_error(mapping, -EIO); |
Sage Weil | 1d3576f | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 775 | return -EIO; /* we're in a forced umount, don't write! */ |
| 776 | } |
Yehuda Sadeh | 3d14c5d | 2010-04-06 15:14:15 -0700 | [diff] [blame] | 777 | if (fsc->mount_options->wsize && fsc->mount_options->wsize < wsize) |
| 778 | wsize = fsc->mount_options->wsize; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 779 | if (wsize < PAGE_SIZE) |
| 780 | wsize = PAGE_SIZE; |
| 781 | max_pages_ever = wsize >> PAGE_SHIFT; |
Sage Weil | 1d3576f | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 782 | |
| 783 | pagevec_init(&pvec, 0); |
| 784 | |
Sage Weil | 1d3576f | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 785 | /* where to start/end? */ |
| 786 | if (wbc->range_cyclic) { |
| 787 | start = mapping->writeback_index; /* Start from prev offset */ |
| 788 | end = -1; |
| 789 | dout(" cyclic, start at %lu\n", start); |
| 790 | } else { |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 791 | start = wbc->range_start >> PAGE_SHIFT; |
| 792 | end = wbc->range_end >> PAGE_SHIFT; |
Sage Weil | 1d3576f | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 793 | if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX) |
| 794 | range_whole = 1; |
| 795 | should_loop = 0; |
| 796 | dout(" not cyclic, %lu to %lu\n", start, end); |
| 797 | } |
| 798 | index = start; |
| 799 | |
| 800 | retry: |
| 801 | /* find oldest snap context with dirty data */ |
| 802 | ceph_put_snap_context(snapc); |
Yan, Zheng | e1966b4 | 2015-06-18 03:10:58 +0800 | [diff] [blame] | 803 | snap_size = -1; |
Sage Weil | 1d3576f | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 804 | snapc = get_oldest_context(inode, &snap_size); |
| 805 | if (!snapc) { |
| 806 | /* hmm, why does writepages get called when there |
| 807 | is no dirty data? */ |
| 808 | dout(" no snap context with dirty data?\n"); |
| 809 | goto out; |
| 810 | } |
| 811 | dout(" oldest snapc is %p seq %lld (%d snaps)\n", |
| 812 | snapc, snapc->seq, snapc->num_snaps); |
Yan, Zheng | fc2744a | 2013-05-31 16:48:29 +0800 | [diff] [blame] | 813 | |
| 814 | spin_lock(&ci->i_ceph_lock); |
| 815 | truncate_seq = ci->i_truncate_seq; |
| 816 | truncate_size = ci->i_truncate_size; |
Yan, Zheng | e1966b4 | 2015-06-18 03:10:58 +0800 | [diff] [blame] | 817 | i_size = i_size_read(inode); |
Yan, Zheng | fc2744a | 2013-05-31 16:48:29 +0800 | [diff] [blame] | 818 | spin_unlock(&ci->i_ceph_lock); |
| 819 | |
Sage Weil | 1d3576f | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 820 | if (last_snapc && snapc != last_snapc) { |
| 821 | /* if we switched to a newer snapc, restart our scan at the |
| 822 | * start of the original file range. */ |
| 823 | dout(" snapc differs from last pass, restarting at %lu\n", |
| 824 | index); |
| 825 | index = start; |
| 826 | } |
| 827 | last_snapc = snapc; |
| 828 | |
| 829 | while (!done && index <= end) { |
| 830 | unsigned i; |
| 831 | int first; |
Yan, Zheng | 5b64640 | 2016-01-07 16:00:17 +0800 | [diff] [blame] | 832 | pgoff_t strip_unit_end = 0; |
| 833 | int num_ops = 0, op_idx; |
| 834 | int pvec_pages, locked_pages = 0; |
| 835 | struct page **pages = NULL, **data_pages; |
Alex Elder | e5975c7 | 2013-03-14 14:09:05 -0500 | [diff] [blame] | 836 | mempool_t *pool = NULL; /* Becomes non-null if mempool used */ |
Sage Weil | 1d3576f | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 837 | struct page *page; |
| 838 | int want; |
Yan, Zheng | 5b64640 | 2016-01-07 16:00:17 +0800 | [diff] [blame] | 839 | u64 offset = 0, len = 0; |
Sage Weil | 1d3576f | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 840 | |
Sage Weil | 1d3576f | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 841 | max_pages = max_pages_ever; |
| 842 | |
| 843 | get_more_pages: |
| 844 | first = -1; |
| 845 | want = min(end - index, |
| 846 | min((pgoff_t)PAGEVEC_SIZE, |
| 847 | max_pages - (pgoff_t)locked_pages) - 1) |
| 848 | + 1; |
| 849 | pvec_pages = pagevec_lookup_tag(&pvec, mapping, &index, |
| 850 | PAGECACHE_TAG_DIRTY, |
| 851 | want); |
| 852 | dout("pagevec_lookup_tag got %d\n", pvec_pages); |
| 853 | if (!pvec_pages && !locked_pages) |
| 854 | break; |
| 855 | for (i = 0; i < pvec_pages && locked_pages < max_pages; i++) { |
| 856 | page = pvec.pages[i]; |
| 857 | dout("? %p idx %lu\n", page, page->index); |
| 858 | if (locked_pages == 0) |
| 859 | lock_page(page); /* first page */ |
| 860 | else if (!trylock_page(page)) |
| 861 | break; |
| 862 | |
| 863 | /* only dirty pages, or our accounting breaks */ |
| 864 | if (unlikely(!PageDirty(page)) || |
| 865 | unlikely(page->mapping != mapping)) { |
| 866 | dout("!dirty or !mapping %p\n", page); |
| 867 | unlock_page(page); |
| 868 | break; |
| 869 | } |
| 870 | if (!wbc->range_cyclic && page->index > end) { |
| 871 | dout("end of range %p\n", page); |
| 872 | done = 1; |
| 873 | unlock_page(page); |
| 874 | break; |
| 875 | } |
Yan, Zheng | 5b64640 | 2016-01-07 16:00:17 +0800 | [diff] [blame] | 876 | if (strip_unit_end && (page->index > strip_unit_end)) { |
| 877 | dout("end of strip unit %p\n", page); |
Sage Weil | 1d3576f | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 878 | unlock_page(page); |
| 879 | break; |
| 880 | } |
| 881 | if (wbc->sync_mode != WB_SYNC_NONE) { |
| 882 | dout("waiting on writeback %p\n", page); |
| 883 | wait_on_page_writeback(page); |
| 884 | } |
Yan, Zheng | e1966b4 | 2015-06-18 03:10:58 +0800 | [diff] [blame] | 885 | if (page_offset(page) >= |
| 886 | (snap_size == -1 ? i_size : snap_size)) { |
| 887 | dout("%p page eof %llu\n", page, |
| 888 | (snap_size == -1 ? i_size : snap_size)); |
Sage Weil | 1d3576f | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 889 | done = 1; |
| 890 | unlock_page(page); |
| 891 | break; |
| 892 | } |
| 893 | if (PageWriteback(page)) { |
| 894 | dout("%p under writeback\n", page); |
| 895 | unlock_page(page); |
| 896 | break; |
| 897 | } |
| 898 | |
| 899 | /* only if matching snap context */ |
Yan, Zheng | 61600ef | 2012-05-28 14:44:30 +0800 | [diff] [blame] | 900 | pgsnapc = page_snap_context(page); |
Sage Weil | 80e755f | 2010-03-31 21:52:10 -0700 | [diff] [blame] | 901 | if (pgsnapc->seq > snapc->seq) { |
| 902 | dout("page snapc %p %lld > oldest %p %lld\n", |
| 903 | pgsnapc, pgsnapc->seq, snapc, snapc->seq); |
Sage Weil | 1d3576f | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 904 | unlock_page(page); |
| 905 | if (!locked_pages) |
| 906 | continue; /* keep looking for snap */ |
| 907 | break; |
| 908 | } |
| 909 | |
| 910 | if (!clear_page_dirty_for_io(page)) { |
| 911 | dout("%p !clear_page_dirty_for_io\n", page); |
| 912 | unlock_page(page); |
| 913 | break; |
| 914 | } |
| 915 | |
Alex Elder | e5975c7 | 2013-03-14 14:09:05 -0500 | [diff] [blame] | 916 | /* |
| 917 | * We have something to write. If this is |
| 918 | * the first locked page this time through, |
Yan, Zheng | 5b64640 | 2016-01-07 16:00:17 +0800 | [diff] [blame] | 919 | * calculate max possinle write size and |
| 920 | * allocate a page array |
Alex Elder | e5975c7 | 2013-03-14 14:09:05 -0500 | [diff] [blame] | 921 | */ |
Sage Weil | 1d3576f | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 922 | if (locked_pages == 0) { |
Yan, Zheng | 5b64640 | 2016-01-07 16:00:17 +0800 | [diff] [blame] | 923 | u64 objnum; |
| 924 | u64 objoff; |
| 925 | |
Sage Weil | 1d3576f | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 926 | /* prepare async write request */ |
Alex Elder | e5975c7 | 2013-03-14 14:09:05 -0500 | [diff] [blame] | 927 | offset = (u64)page_offset(page); |
Sage Weil | 1d3576f | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 928 | len = wsize; |
Yan, Zheng | 5b64640 | 2016-01-07 16:00:17 +0800 | [diff] [blame] | 929 | |
| 930 | rc = ceph_calc_file_object_mapping(&ci->i_layout, |
| 931 | offset, len, |
| 932 | &objnum, &objoff, |
| 933 | &len); |
| 934 | if (rc < 0) { |
Henry C Chang | 8c71897 | 2011-05-03 09:45:16 +0000 | [diff] [blame] | 935 | unlock_page(page); |
| 936 | break; |
| 937 | } |
| 938 | |
Yan, Zheng | 5b64640 | 2016-01-07 16:00:17 +0800 | [diff] [blame] | 939 | num_ops = 1 + do_sync; |
| 940 | strip_unit_end = page->index + |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 941 | ((len - 1) >> PAGE_SHIFT); |
Yan, Zheng | 715e4cd | 2014-11-13 14:40:37 +0800 | [diff] [blame] | 942 | |
Yan, Zheng | 5b64640 | 2016-01-07 16:00:17 +0800 | [diff] [blame] | 943 | BUG_ON(pages); |
Alex Elder | 8848695 | 2013-03-14 14:09:05 -0500 | [diff] [blame] | 944 | max_pages = calc_pages_for(0, (u64)len); |
Yan, Zheng | fc2744a | 2013-05-31 16:48:29 +0800 | [diff] [blame] | 945 | pages = kmalloc(max_pages * sizeof (*pages), |
| 946 | GFP_NOFS); |
Alex Elder | 8848695 | 2013-03-14 14:09:05 -0500 | [diff] [blame] | 947 | if (!pages) { |
| 948 | pool = fsc->wb_pagevec_pool; |
Alex Elder | 8848695 | 2013-03-14 14:09:05 -0500 | [diff] [blame] | 949 | pages = mempool_alloc(pool, GFP_NOFS); |
Alex Elder | e5975c7 | 2013-03-14 14:09:05 -0500 | [diff] [blame] | 950 | BUG_ON(!pages); |
Alex Elder | 8848695 | 2013-03-14 14:09:05 -0500 | [diff] [blame] | 951 | } |
Yan, Zheng | 5b64640 | 2016-01-07 16:00:17 +0800 | [diff] [blame] | 952 | |
| 953 | len = 0; |
| 954 | } else if (page->index != |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 955 | (offset + len) >> PAGE_SHIFT) { |
Yan, Zheng | 5b64640 | 2016-01-07 16:00:17 +0800 | [diff] [blame] | 956 | if (num_ops >= (pool ? CEPH_OSD_SLAB_OPS : |
| 957 | CEPH_OSD_MAX_OPS)) { |
| 958 | redirty_page_for_writepage(wbc, page); |
| 959 | unlock_page(page); |
| 960 | break; |
| 961 | } |
| 962 | |
| 963 | num_ops++; |
| 964 | offset = (u64)page_offset(page); |
| 965 | len = 0; |
Sage Weil | 1d3576f | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 966 | } |
| 967 | |
| 968 | /* note position of first page in pvec */ |
| 969 | if (first < 0) |
| 970 | first = i; |
| 971 | dout("%p will write page %p idx %lu\n", |
| 972 | inode, page, page->index); |
Yehuda Sadeh | 2baba25 | 2009-12-18 13:51:57 -0800 | [diff] [blame] | 973 | |
Yan, Zheng | 5b64640 | 2016-01-07 16:00:17 +0800 | [diff] [blame] | 974 | if (atomic_long_inc_return(&fsc->writeback_count) > |
| 975 | CONGESTION_ON_THRESH( |
Yehuda Sadeh | 3d14c5d | 2010-04-06 15:14:15 -0700 | [diff] [blame] | 976 | fsc->mount_options->congestion_kb)) { |
| 977 | set_bdi_congested(&fsc->backing_dev_info, |
Sage Weil | 213c99e | 2010-08-03 10:25:11 -0700 | [diff] [blame] | 978 | BLK_RW_ASYNC); |
Yehuda Sadeh | 2baba25 | 2009-12-18 13:51:57 -0800 | [diff] [blame] | 979 | } |
| 980 | |
Alex Elder | e5975c7 | 2013-03-14 14:09:05 -0500 | [diff] [blame] | 981 | pages[locked_pages] = page; |
Sage Weil | 1d3576f | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 982 | locked_pages++; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 983 | len += PAGE_SIZE; |
Sage Weil | 1d3576f | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 984 | } |
| 985 | |
| 986 | /* did we get anything? */ |
| 987 | if (!locked_pages) |
| 988 | goto release_pvec_pages; |
| 989 | if (i) { |
| 990 | int j; |
| 991 | BUG_ON(!locked_pages || first < 0); |
| 992 | |
| 993 | if (pvec_pages && i == pvec_pages && |
| 994 | locked_pages < max_pages) { |
| 995 | dout("reached end pvec, trying for more\n"); |
| 996 | pagevec_reinit(&pvec); |
| 997 | goto get_more_pages; |
| 998 | } |
| 999 | |
| 1000 | /* shift unused pages over in the pvec... we |
| 1001 | * will need to release them below. */ |
| 1002 | for (j = i; j < pvec_pages; j++) { |
Yan, Zheng | 5b64640 | 2016-01-07 16:00:17 +0800 | [diff] [blame] | 1003 | dout(" pvec leftover page %p\n", pvec.pages[j]); |
Sage Weil | 1d3576f | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1004 | pvec.pages[j-i+first] = pvec.pages[j]; |
| 1005 | } |
| 1006 | pvec.nr -= i-first; |
| 1007 | } |
| 1008 | |
Yan, Zheng | 5b64640 | 2016-01-07 16:00:17 +0800 | [diff] [blame] | 1009 | new_request: |
Alex Elder | e5975c7 | 2013-03-14 14:09:05 -0500 | [diff] [blame] | 1010 | offset = page_offset(pages[0]); |
Yan, Zheng | 5b64640 | 2016-01-07 16:00:17 +0800 | [diff] [blame] | 1011 | len = wsize; |
| 1012 | |
| 1013 | req = ceph_osdc_new_request(&fsc->client->osdc, |
| 1014 | &ci->i_layout, vino, |
| 1015 | offset, &len, 0, num_ops, |
| 1016 | CEPH_OSD_OP_WRITE, |
| 1017 | CEPH_OSD_FLAG_WRITE | |
| 1018 | CEPH_OSD_FLAG_ONDISK, |
| 1019 | snapc, truncate_seq, |
| 1020 | truncate_size, false); |
| 1021 | if (IS_ERR(req)) { |
| 1022 | req = ceph_osdc_new_request(&fsc->client->osdc, |
| 1023 | &ci->i_layout, vino, |
| 1024 | offset, &len, 0, |
| 1025 | min(num_ops, |
| 1026 | CEPH_OSD_SLAB_OPS), |
| 1027 | CEPH_OSD_OP_WRITE, |
| 1028 | CEPH_OSD_FLAG_WRITE | |
| 1029 | CEPH_OSD_FLAG_ONDISK, |
| 1030 | snapc, truncate_seq, |
| 1031 | truncate_size, true); |
| 1032 | BUG_ON(IS_ERR(req)); |
Yan, Zheng | e1966b4 | 2015-06-18 03:10:58 +0800 | [diff] [blame] | 1033 | } |
Yan, Zheng | 5b64640 | 2016-01-07 16:00:17 +0800 | [diff] [blame] | 1034 | BUG_ON(len < page_offset(pages[locked_pages - 1]) + |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1035 | PAGE_SIZE - offset); |
Sage Weil | 1d3576f | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1036 | |
Yan, Zheng | 5b64640 | 2016-01-07 16:00:17 +0800 | [diff] [blame] | 1037 | req->r_callback = writepages_finish; |
| 1038 | req->r_inode = inode; |
| 1039 | |
| 1040 | /* Format the osd request message and submit the write */ |
| 1041 | len = 0; |
| 1042 | data_pages = pages; |
| 1043 | op_idx = 0; |
| 1044 | for (i = 0; i < locked_pages; i++) { |
| 1045 | u64 cur_offset = page_offset(pages[i]); |
| 1046 | if (offset + len != cur_offset) { |
| 1047 | if (op_idx + do_sync + 1 == req->r_num_ops) |
| 1048 | break; |
| 1049 | osd_req_op_extent_dup_last(req, op_idx, |
| 1050 | cur_offset - offset); |
| 1051 | dout("writepages got pages at %llu~%llu\n", |
| 1052 | offset, len); |
| 1053 | osd_req_op_extent_osd_data_pages(req, op_idx, |
| 1054 | data_pages, len, 0, |
Alex Elder | a4ce40a | 2013-04-05 01:27:12 -0500 | [diff] [blame] | 1055 | !!pool, false); |
Yan, Zheng | 5b64640 | 2016-01-07 16:00:17 +0800 | [diff] [blame] | 1056 | osd_req_op_extent_update(req, op_idx, len); |
Alex Elder | e5975c7 | 2013-03-14 14:09:05 -0500 | [diff] [blame] | 1057 | |
Yan, Zheng | 5b64640 | 2016-01-07 16:00:17 +0800 | [diff] [blame] | 1058 | len = 0; |
| 1059 | offset = cur_offset; |
| 1060 | data_pages = pages + i; |
| 1061 | op_idx++; |
| 1062 | } |
| 1063 | |
| 1064 | set_page_writeback(pages[i]); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1065 | len += PAGE_SIZE; |
Yan, Zheng | 5b64640 | 2016-01-07 16:00:17 +0800 | [diff] [blame] | 1066 | } |
| 1067 | |
| 1068 | if (snap_size != -1) { |
| 1069 | len = min(len, snap_size - offset); |
| 1070 | } else if (i == locked_pages) { |
| 1071 | /* writepages_finish() clears writeback pages |
| 1072 | * according to the data length, so make sure |
| 1073 | * data length covers all locked pages */ |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1074 | u64 min_len = len + 1 - PAGE_SIZE; |
Yan, Zheng | 5b64640 | 2016-01-07 16:00:17 +0800 | [diff] [blame] | 1075 | len = min(len, (u64)i_size_read(inode) - offset); |
| 1076 | len = max(len, min_len); |
| 1077 | } |
| 1078 | dout("writepages got pages at %llu~%llu\n", offset, len); |
| 1079 | |
| 1080 | osd_req_op_extent_osd_data_pages(req, op_idx, data_pages, len, |
| 1081 | 0, !!pool, false); |
| 1082 | osd_req_op_extent_update(req, op_idx, len); |
| 1083 | |
| 1084 | if (do_sync) { |
| 1085 | op_idx++; |
| 1086 | osd_req_op_init(req, op_idx, CEPH_OSD_OP_STARTSYNC, 0); |
| 1087 | } |
| 1088 | BUG_ON(op_idx + 1 != req->r_num_ops); |
| 1089 | |
Alex Elder | e5975c7 | 2013-03-14 14:09:05 -0500 | [diff] [blame] | 1090 | pool = NULL; |
Yan, Zheng | 5b64640 | 2016-01-07 16:00:17 +0800 | [diff] [blame] | 1091 | if (i < locked_pages) { |
| 1092 | BUG_ON(num_ops <= req->r_num_ops); |
| 1093 | num_ops -= req->r_num_ops; |
| 1094 | num_ops += do_sync; |
| 1095 | locked_pages -= i; |
Alex Elder | e5975c7 | 2013-03-14 14:09:05 -0500 | [diff] [blame] | 1096 | |
Yan, Zheng | 5b64640 | 2016-01-07 16:00:17 +0800 | [diff] [blame] | 1097 | /* allocate new pages array for next request */ |
| 1098 | data_pages = pages; |
| 1099 | pages = kmalloc(locked_pages * sizeof (*pages), |
| 1100 | GFP_NOFS); |
| 1101 | if (!pages) { |
| 1102 | pool = fsc->wb_pagevec_pool; |
| 1103 | pages = mempool_alloc(pool, GFP_NOFS); |
| 1104 | BUG_ON(!pages); |
| 1105 | } |
| 1106 | memcpy(pages, data_pages + i, |
| 1107 | locked_pages * sizeof(*pages)); |
| 1108 | memset(data_pages + i, 0, |
| 1109 | locked_pages * sizeof(*pages)); |
| 1110 | } else { |
| 1111 | BUG_ON(num_ops != req->r_num_ops); |
| 1112 | index = pages[i - 1]->index + 1; |
| 1113 | /* request message now owns the pages array */ |
| 1114 | pages = NULL; |
| 1115 | } |
Alex Elder | e5975c7 | 2013-03-14 14:09:05 -0500 | [diff] [blame] | 1116 | |
Ilya Dryomov | bb873b5 | 2016-05-26 00:29:52 +0200 | [diff] [blame] | 1117 | req->r_mtime = inode->i_mtime; |
Sage Weil | 9d6fcb0 | 2011-05-12 15:48:16 -0700 | [diff] [blame] | 1118 | rc = ceph_osdc_start_request(&fsc->client->osdc, req, true); |
| 1119 | BUG_ON(rc); |
Sage Weil | 1d3576f | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1120 | req = NULL; |
| 1121 | |
Yan, Zheng | 5b64640 | 2016-01-07 16:00:17 +0800 | [diff] [blame] | 1122 | wbc->nr_to_write -= i; |
| 1123 | if (pages) |
| 1124 | goto new_request; |
| 1125 | |
Sage Weil | 1d3576f | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1126 | if (wbc->nr_to_write <= 0) |
| 1127 | done = 1; |
| 1128 | |
| 1129 | release_pvec_pages: |
| 1130 | dout("pagevec_release on %d pages (%p)\n", (int)pvec.nr, |
| 1131 | pvec.nr ? pvec.pages[0] : NULL); |
| 1132 | pagevec_release(&pvec); |
| 1133 | |
| 1134 | if (locked_pages && !done) |
| 1135 | goto retry; |
| 1136 | } |
| 1137 | |
| 1138 | if (should_loop && !done) { |
| 1139 | /* more to do; loop back to beginning of file */ |
| 1140 | dout("writepages looping back to beginning of file\n"); |
| 1141 | should_loop = 0; |
| 1142 | index = 0; |
| 1143 | goto retry; |
| 1144 | } |
| 1145 | |
| 1146 | if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0)) |
| 1147 | mapping->writeback_index = index; |
| 1148 | |
| 1149 | out: |
Ilya Dryomov | 3ed97d6 | 2016-04-26 15:05:29 +0200 | [diff] [blame] | 1150 | ceph_osdc_put_request(req); |
Sage Weil | 1d3576f | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1151 | ceph_put_snap_context(snapc); |
| 1152 | dout("writepages done, rc = %d\n", rc); |
Sage Weil | 1d3576f | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1153 | return rc; |
| 1154 | } |
| 1155 | |
| 1156 | |
| 1157 | |
| 1158 | /* |
| 1159 | * See if a given @snapc is either writeable, or already written. |
| 1160 | */ |
| 1161 | static int context_is_writeable_or_written(struct inode *inode, |
| 1162 | struct ceph_snap_context *snapc) |
| 1163 | { |
| 1164 | struct ceph_snap_context *oldest = get_oldest_context(inode, NULL); |
Sage Weil | 6298a33 | 2010-03-31 22:01:38 -0700 | [diff] [blame] | 1165 | int ret = !oldest || snapc->seq <= oldest->seq; |
| 1166 | |
| 1167 | ceph_put_snap_context(oldest); |
| 1168 | return ret; |
Sage Weil | 1d3576f | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1169 | } |
| 1170 | |
| 1171 | /* |
| 1172 | * We are only allowed to write into/dirty the page if the page is |
| 1173 | * clean, or already dirty within the same snap context. |
Sage Weil | 8f883c2 | 2010-03-19 13:27:53 -0700 | [diff] [blame] | 1174 | * |
| 1175 | * called with page locked. |
| 1176 | * return success with page locked, |
| 1177 | * or any failure (incl -EAGAIN) with page unlocked. |
Sage Weil | 1d3576f | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1178 | */ |
Yehuda Sadeh | 4af6b22 | 2010-02-09 11:02:51 -0800 | [diff] [blame] | 1179 | static int ceph_update_writeable_page(struct file *file, |
| 1180 | loff_t pos, unsigned len, |
| 1181 | struct page *page) |
Sage Weil | 1d3576f | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1182 | { |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 1183 | struct inode *inode = file_inode(file); |
Yan, Zheng | 6c93df5 | 2016-04-15 13:56:12 +0800 | [diff] [blame] | 1184 | struct ceph_fs_client *fsc = ceph_inode_to_client(inode); |
Sage Weil | 1d3576f | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1185 | struct ceph_inode_info *ci = ceph_inode(inode); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1186 | loff_t page_off = pos & PAGE_MASK; |
| 1187 | int pos_in_page = pos & ~PAGE_MASK; |
Sage Weil | 1d3576f | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1188 | int end_in_page = pos_in_page + len; |
| 1189 | loff_t i_size; |
Sage Weil | 1d3576f | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1190 | int r; |
Sage Weil | 80e755f | 2010-03-31 21:52:10 -0700 | [diff] [blame] | 1191 | struct ceph_snap_context *snapc, *oldest; |
Sage Weil | 1d3576f | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1192 | |
Yan, Zheng | 6c93df5 | 2016-04-15 13:56:12 +0800 | [diff] [blame] | 1193 | if (ACCESS_ONCE(fsc->mount_state) == CEPH_MOUNT_SHUTDOWN) { |
| 1194 | dout(" page %p forced umount\n", page); |
| 1195 | unlock_page(page); |
| 1196 | return -EIO; |
| 1197 | } |
| 1198 | |
Sage Weil | 1d3576f | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1199 | retry_locked: |
| 1200 | /* writepages currently holds page lock, but if we change that later, */ |
| 1201 | wait_on_page_writeback(page); |
| 1202 | |
Yan, Zheng | 61600ef | 2012-05-28 14:44:30 +0800 | [diff] [blame] | 1203 | snapc = page_snap_context(page); |
Sage Weil | 80e755f | 2010-03-31 21:52:10 -0700 | [diff] [blame] | 1204 | if (snapc && snapc != ci->i_head_snapc) { |
Sage Weil | 1d3576f | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1205 | /* |
| 1206 | * this page is already dirty in another (older) snap |
| 1207 | * context! is it writeable now? |
| 1208 | */ |
Sage Weil | 80e755f | 2010-03-31 21:52:10 -0700 | [diff] [blame] | 1209 | oldest = get_oldest_context(inode, NULL); |
Sage Weil | 1d3576f | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1210 | |
Sage Weil | 80e755f | 2010-03-31 21:52:10 -0700 | [diff] [blame] | 1211 | if (snapc->seq > oldest->seq) { |
Sage Weil | 6298a33 | 2010-03-31 22:01:38 -0700 | [diff] [blame] | 1212 | ceph_put_snap_context(oldest); |
Sage Weil | 1d3576f | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1213 | dout(" page %p snapc %p not current or oldest\n", |
Sage Weil | 6298a33 | 2010-03-31 22:01:38 -0700 | [diff] [blame] | 1214 | page, snapc); |
Sage Weil | 1d3576f | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1215 | /* |
| 1216 | * queue for writeback, and wait for snapc to |
| 1217 | * be writeable or written |
| 1218 | */ |
Sage Weil | 6298a33 | 2010-03-31 22:01:38 -0700 | [diff] [blame] | 1219 | snapc = ceph_get_snap_context(snapc); |
Sage Weil | 1d3576f | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1220 | unlock_page(page); |
Sage Weil | 3c6f6b7 | 2010-02-09 15:24:44 -0800 | [diff] [blame] | 1221 | ceph_queue_writeback(inode); |
Yan, Zheng | a78bbd4 | 2016-05-13 11:30:24 +0800 | [diff] [blame] | 1222 | r = wait_event_killable(ci->i_cap_wq, |
Sage Weil | 1d3576f | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1223 | context_is_writeable_or_written(inode, snapc)); |
| 1224 | ceph_put_snap_context(snapc); |
Sage Weil | 8f883c2 | 2010-03-19 13:27:53 -0700 | [diff] [blame] | 1225 | if (r == -ERESTARTSYS) |
| 1226 | return r; |
Yehuda Sadeh | 4af6b22 | 2010-02-09 11:02:51 -0800 | [diff] [blame] | 1227 | return -EAGAIN; |
Sage Weil | 1d3576f | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1228 | } |
Sage Weil | 6298a33 | 2010-03-31 22:01:38 -0700 | [diff] [blame] | 1229 | ceph_put_snap_context(oldest); |
Sage Weil | 1d3576f | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1230 | |
| 1231 | /* yay, writeable, do it now (without dropping page lock) */ |
| 1232 | dout(" page %p snapc %p not current, but oldest\n", |
| 1233 | page, snapc); |
| 1234 | if (!clear_page_dirty_for_io(page)) |
| 1235 | goto retry_locked; |
| 1236 | r = writepage_nounlock(page, NULL); |
| 1237 | if (r < 0) |
| 1238 | goto fail_nosnap; |
| 1239 | goto retry_locked; |
| 1240 | } |
| 1241 | |
| 1242 | if (PageUptodate(page)) { |
| 1243 | dout(" page %p already uptodate\n", page); |
| 1244 | return 0; |
| 1245 | } |
| 1246 | |
| 1247 | /* full page? */ |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1248 | if (pos_in_page == 0 && len == PAGE_SIZE) |
Sage Weil | 1d3576f | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1249 | return 0; |
| 1250 | |
| 1251 | /* past end of file? */ |
Yan, Zheng | 99c88e6 | 2015-12-30 11:32:46 +0800 | [diff] [blame] | 1252 | i_size = i_size_read(inode); |
Sage Weil | 1d3576f | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1253 | |
Sage Weil | 1d3576f | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1254 | if (page_off >= i_size || |
| 1255 | (pos_in_page == 0 && (pos+len) >= i_size && |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1256 | end_in_page - pos_in_page != PAGE_SIZE)) { |
Sage Weil | 1d3576f | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1257 | dout(" zeroing %p 0 - %d and %d - %d\n", |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1258 | page, pos_in_page, end_in_page, (int)PAGE_SIZE); |
Sage Weil | 1d3576f | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1259 | zero_user_segments(page, |
| 1260 | 0, pos_in_page, |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1261 | end_in_page, PAGE_SIZE); |
Sage Weil | 1d3576f | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1262 | return 0; |
| 1263 | } |
| 1264 | |
| 1265 | /* we need to read it. */ |
Sage Weil | 1d3576f | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1266 | r = readpage_nounlock(file, page); |
| 1267 | if (r < 0) |
| 1268 | goto fail_nosnap; |
| 1269 | goto retry_locked; |
Sage Weil | 1d3576f | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1270 | fail_nosnap: |
| 1271 | unlock_page(page); |
| 1272 | return r; |
| 1273 | } |
| 1274 | |
| 1275 | /* |
Yehuda Sadeh | 4af6b22 | 2010-02-09 11:02:51 -0800 | [diff] [blame] | 1276 | * We are only allowed to write into/dirty the page if the page is |
| 1277 | * clean, or already dirty within the same snap context. |
| 1278 | */ |
| 1279 | static int ceph_write_begin(struct file *file, struct address_space *mapping, |
| 1280 | loff_t pos, unsigned len, unsigned flags, |
| 1281 | struct page **pagep, void **fsdata) |
| 1282 | { |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 1283 | struct inode *inode = file_inode(file); |
Yehuda Sadeh | 4af6b22 | 2010-02-09 11:02:51 -0800 | [diff] [blame] | 1284 | struct page *page; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1285 | pgoff_t index = pos >> PAGE_SHIFT; |
Sage Weil | 7971bd9 | 2013-05-01 21:15:58 -0700 | [diff] [blame] | 1286 | int r; |
Yehuda Sadeh | 4af6b22 | 2010-02-09 11:02:51 -0800 | [diff] [blame] | 1287 | |
| 1288 | do { |
Sage Weil | 8f883c2 | 2010-03-19 13:27:53 -0700 | [diff] [blame] | 1289 | /* get a page */ |
Yehuda Sadeh | 4af6b22 | 2010-02-09 11:02:51 -0800 | [diff] [blame] | 1290 | page = grab_cache_page_write_begin(mapping, index, 0); |
Sage Weil | 7971bd9 | 2013-05-01 21:15:58 -0700 | [diff] [blame] | 1291 | if (!page) |
| 1292 | return -ENOMEM; |
Yehuda Sadeh | 4af6b22 | 2010-02-09 11:02:51 -0800 | [diff] [blame] | 1293 | |
| 1294 | dout("write_begin file %p inode %p page %p %d~%d\n", file, |
Sage Weil | 213c99e | 2010-08-03 10:25:11 -0700 | [diff] [blame] | 1295 | inode, page, (int)pos, (int)len); |
Yehuda Sadeh | 4af6b22 | 2010-02-09 11:02:51 -0800 | [diff] [blame] | 1296 | |
| 1297 | r = ceph_update_writeable_page(file, pos, len, page); |
Taesoo Kim | c1d00b2 | 2015-03-20 17:36:56 -0400 | [diff] [blame] | 1298 | if (r < 0) |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1299 | put_page(page); |
Taesoo Kim | c1d00b2 | 2015-03-20 17:36:56 -0400 | [diff] [blame] | 1300 | else |
| 1301 | *pagep = page; |
Yehuda Sadeh | 4af6b22 | 2010-02-09 11:02:51 -0800 | [diff] [blame] | 1302 | } while (r == -EAGAIN); |
| 1303 | |
| 1304 | return r; |
| 1305 | } |
| 1306 | |
| 1307 | /* |
Sage Weil | 1d3576f | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1308 | * we don't do anything in here that simple_write_end doesn't do |
Yan, Zheng | 5dda377c | 2015-04-30 14:40:54 +0800 | [diff] [blame] | 1309 | * except adjust dirty page accounting |
Sage Weil | 1d3576f | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1310 | */ |
| 1311 | static int ceph_write_end(struct file *file, struct address_space *mapping, |
| 1312 | loff_t pos, unsigned len, unsigned copied, |
| 1313 | struct page *page, void *fsdata) |
| 1314 | { |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 1315 | struct inode *inode = file_inode(file); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1316 | unsigned from = pos & (PAGE_SIZE - 1); |
Sage Weil | 1d3576f | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1317 | int check_cap = 0; |
| 1318 | |
| 1319 | dout("write_end file %p inode %p page %p %d~%d (%d)\n", file, |
| 1320 | inode, page, (int)pos, (int)copied, (int)len); |
| 1321 | |
| 1322 | /* zero the stale part of the page if we did a short copy */ |
| 1323 | if (copied < len) |
| 1324 | zero_user_segment(page, from+copied, len); |
| 1325 | |
| 1326 | /* did file size increase? */ |
Yan, Zheng | 99c88e6 | 2015-12-30 11:32:46 +0800 | [diff] [blame] | 1327 | if (pos+copied > i_size_read(inode)) |
Sage Weil | 1d3576f | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1328 | check_cap = ceph_inode_set_size(inode, pos+copied); |
| 1329 | |
| 1330 | if (!PageUptodate(page)) |
| 1331 | SetPageUptodate(page); |
| 1332 | |
| 1333 | set_page_dirty(page); |
| 1334 | |
| 1335 | unlock_page(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1336 | put_page(page); |
Sage Weil | 1d3576f | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1337 | |
| 1338 | if (check_cap) |
| 1339 | ceph_check_caps(ceph_inode(inode), CHECK_CAPS_AUTHONLY, NULL); |
| 1340 | |
| 1341 | return copied; |
| 1342 | } |
| 1343 | |
| 1344 | /* |
| 1345 | * we set .direct_IO to indicate direct io is supported, but since we |
| 1346 | * intercept O_DIRECT reads and writes early, this function should |
| 1347 | * never get called. |
| 1348 | */ |
Christoph Hellwig | c8b8e32 | 2016-04-07 08:51:58 -0700 | [diff] [blame] | 1349 | static ssize_t ceph_direct_io(struct kiocb *iocb, struct iov_iter *iter) |
Sage Weil | 1d3576f | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1350 | { |
| 1351 | WARN_ON(1); |
| 1352 | return -EINVAL; |
| 1353 | } |
| 1354 | |
| 1355 | const struct address_space_operations ceph_aops = { |
| 1356 | .readpage = ceph_readpage, |
| 1357 | .readpages = ceph_readpages, |
| 1358 | .writepage = ceph_writepage, |
| 1359 | .writepages = ceph_writepages_start, |
| 1360 | .write_begin = ceph_write_begin, |
| 1361 | .write_end = ceph_write_end, |
| 1362 | .set_page_dirty = ceph_set_page_dirty, |
| 1363 | .invalidatepage = ceph_invalidatepage, |
| 1364 | .releasepage = ceph_releasepage, |
| 1365 | .direct_IO = ceph_direct_io, |
| 1366 | }; |
| 1367 | |
Yan, Zheng | 4f7e89f | 2016-05-10 18:40:28 +0800 | [diff] [blame] | 1368 | static void ceph_block_sigs(sigset_t *oldset) |
| 1369 | { |
| 1370 | sigset_t mask; |
| 1371 | siginitsetinv(&mask, sigmask(SIGKILL)); |
| 1372 | sigprocmask(SIG_BLOCK, &mask, oldset); |
| 1373 | } |
| 1374 | |
| 1375 | static void ceph_restore_sigs(sigset_t *oldset) |
| 1376 | { |
| 1377 | sigprocmask(SIG_SETMASK, oldset, NULL); |
| 1378 | } |
Sage Weil | 1d3576f | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1379 | |
| 1380 | /* |
| 1381 | * vm ops |
| 1382 | */ |
Yan, Zheng | 61f6881 | 2013-11-28 14:28:14 +0800 | [diff] [blame] | 1383 | static int ceph_filemap_fault(struct vm_area_struct *vma, struct vm_fault *vmf) |
| 1384 | { |
| 1385 | struct inode *inode = file_inode(vma->vm_file); |
| 1386 | struct ceph_inode_info *ci = ceph_inode(inode); |
| 1387 | struct ceph_file_info *fi = vma->vm_file->private_data; |
Yan, Zheng | 3738daa | 2014-11-14 22:10:07 +0800 | [diff] [blame] | 1388 | struct page *pinned_page = NULL; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1389 | loff_t off = vmf->pgoff << PAGE_SHIFT; |
Yan, Zheng | 61f6881 | 2013-11-28 14:28:14 +0800 | [diff] [blame] | 1390 | int want, got, ret; |
Yan, Zheng | 4f7e89f | 2016-05-10 18:40:28 +0800 | [diff] [blame] | 1391 | sigset_t oldset; |
| 1392 | |
| 1393 | ceph_block_sigs(&oldset); |
Yan, Zheng | 61f6881 | 2013-11-28 14:28:14 +0800 | [diff] [blame] | 1394 | |
| 1395 | dout("filemap_fault %p %llx.%llx %llu~%zd trying to get caps\n", |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1396 | inode, ceph_vinop(inode), off, (size_t)PAGE_SIZE); |
Yan, Zheng | 61f6881 | 2013-11-28 14:28:14 +0800 | [diff] [blame] | 1397 | if (fi->fmode & CEPH_FILE_MODE_LAZY) |
| 1398 | want = CEPH_CAP_FILE_CACHE | CEPH_CAP_FILE_LAZYIO; |
| 1399 | else |
| 1400 | want = CEPH_CAP_FILE_CACHE; |
Yan, Zheng | 4f7e89f | 2016-05-10 18:40:28 +0800 | [diff] [blame] | 1401 | |
| 1402 | got = 0; |
| 1403 | ret = ceph_get_caps(ci, CEPH_CAP_FILE_RD, want, -1, &got, &pinned_page); |
Yan, Zheng | 6ce026e | 2016-05-10 18:59:13 +0800 | [diff] [blame] | 1404 | if (ret < 0) |
Yan, Zheng | 4f7e89f | 2016-05-10 18:40:28 +0800 | [diff] [blame] | 1405 | goto out_restore; |
Yan, Zheng | 6ce026e | 2016-05-10 18:59:13 +0800 | [diff] [blame] | 1406 | |
Yan, Zheng | 61f6881 | 2013-11-28 14:28:14 +0800 | [diff] [blame] | 1407 | dout("filemap_fault %p %llu~%zd got cap refs on %s\n", |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1408 | inode, off, (size_t)PAGE_SIZE, ceph_cap_string(got)); |
Yan, Zheng | 61f6881 | 2013-11-28 14:28:14 +0800 | [diff] [blame] | 1409 | |
Yan, Zheng | 8370124 | 2014-11-14 22:36:18 +0800 | [diff] [blame] | 1410 | if ((got & (CEPH_CAP_FILE_CACHE | CEPH_CAP_FILE_LAZYIO)) || |
Yan, Zheng | e99b0ea | 2016-10-25 10:51:55 +0800 | [diff] [blame] | 1411 | ci->i_inline_version == CEPH_INLINE_NONE) { |
| 1412 | current->journal_info = vma->vm_file; |
Yan, Zheng | 8370124 | 2014-11-14 22:36:18 +0800 | [diff] [blame] | 1413 | ret = filemap_fault(vma, vmf); |
Yan, Zheng | e99b0ea | 2016-10-25 10:51:55 +0800 | [diff] [blame] | 1414 | current->journal_info = NULL; |
| 1415 | } else |
Yan, Zheng | 8370124 | 2014-11-14 22:36:18 +0800 | [diff] [blame] | 1416 | ret = -EAGAIN; |
Yan, Zheng | 61f6881 | 2013-11-28 14:28:14 +0800 | [diff] [blame] | 1417 | |
| 1418 | dout("filemap_fault %p %llu~%zd dropping cap refs on %s ret %d\n", |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1419 | inode, off, (size_t)PAGE_SIZE, ceph_cap_string(got), ret); |
Yan, Zheng | 3738daa | 2014-11-14 22:10:07 +0800 | [diff] [blame] | 1420 | if (pinned_page) |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1421 | put_page(pinned_page); |
Yan, Zheng | 61f6881 | 2013-11-28 14:28:14 +0800 | [diff] [blame] | 1422 | ceph_put_cap_refs(ci, got); |
| 1423 | |
Yan, Zheng | 8370124 | 2014-11-14 22:36:18 +0800 | [diff] [blame] | 1424 | if (ret != -EAGAIN) |
Yan, Zheng | 4f7e89f | 2016-05-10 18:40:28 +0800 | [diff] [blame] | 1425 | goto out_restore; |
Yan, Zheng | 8370124 | 2014-11-14 22:36:18 +0800 | [diff] [blame] | 1426 | |
| 1427 | /* read inline data */ |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1428 | if (off >= PAGE_SIZE) { |
Yan, Zheng | 8370124 | 2014-11-14 22:36:18 +0800 | [diff] [blame] | 1429 | /* does not support inline data > PAGE_SIZE */ |
| 1430 | ret = VM_FAULT_SIGBUS; |
| 1431 | } else { |
| 1432 | int ret1; |
| 1433 | struct address_space *mapping = inode->i_mapping; |
| 1434 | struct page *page = find_or_create_page(mapping, 0, |
Michal Hocko | c62d255 | 2015-11-06 16:28:49 -0800 | [diff] [blame] | 1435 | mapping_gfp_constraint(mapping, |
| 1436 | ~__GFP_FS)); |
Yan, Zheng | 8370124 | 2014-11-14 22:36:18 +0800 | [diff] [blame] | 1437 | if (!page) { |
| 1438 | ret = VM_FAULT_OOM; |
Yan, Zheng | 4f7e89f | 2016-05-10 18:40:28 +0800 | [diff] [blame] | 1439 | goto out_inline; |
Yan, Zheng | 8370124 | 2014-11-14 22:36:18 +0800 | [diff] [blame] | 1440 | } |
| 1441 | ret1 = __ceph_do_getattr(inode, page, |
| 1442 | CEPH_STAT_CAP_INLINE_DATA, true); |
| 1443 | if (ret1 < 0 || off >= i_size_read(inode)) { |
| 1444 | unlock_page(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1445 | put_page(page); |
Yan, Zheng | 6ce026e | 2016-05-10 18:59:13 +0800 | [diff] [blame] | 1446 | if (ret1 < 0) |
| 1447 | ret = ret1; |
| 1448 | else |
| 1449 | ret = VM_FAULT_SIGBUS; |
Yan, Zheng | 4f7e89f | 2016-05-10 18:40:28 +0800 | [diff] [blame] | 1450 | goto out_inline; |
Yan, Zheng | 8370124 | 2014-11-14 22:36:18 +0800 | [diff] [blame] | 1451 | } |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1452 | if (ret1 < PAGE_SIZE) |
| 1453 | zero_user_segment(page, ret1, PAGE_SIZE); |
Yan, Zheng | 8370124 | 2014-11-14 22:36:18 +0800 | [diff] [blame] | 1454 | else |
| 1455 | flush_dcache_page(page); |
| 1456 | SetPageUptodate(page); |
| 1457 | vmf->page = page; |
| 1458 | ret = VM_FAULT_MAJOR | VM_FAULT_LOCKED; |
Yan, Zheng | 4f7e89f | 2016-05-10 18:40:28 +0800 | [diff] [blame] | 1459 | out_inline: |
| 1460 | dout("filemap_fault %p %llu~%zd read inline data ret %d\n", |
| 1461 | inode, off, (size_t)PAGE_SIZE, ret); |
Yan, Zheng | 8370124 | 2014-11-14 22:36:18 +0800 | [diff] [blame] | 1462 | } |
Yan, Zheng | 4f7e89f | 2016-05-10 18:40:28 +0800 | [diff] [blame] | 1463 | out_restore: |
| 1464 | ceph_restore_sigs(&oldset); |
Yan, Zheng | 6ce026e | 2016-05-10 18:59:13 +0800 | [diff] [blame] | 1465 | if (ret < 0) |
| 1466 | ret = (ret == -ENOMEM) ? VM_FAULT_OOM : VM_FAULT_SIGBUS; |
| 1467 | |
Yan, Zheng | 61f6881 | 2013-11-28 14:28:14 +0800 | [diff] [blame] | 1468 | return ret; |
| 1469 | } |
Sage Weil | 1d3576f | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1470 | |
| 1471 | /* |
| 1472 | * Reuse write_begin here for simplicity. |
| 1473 | */ |
| 1474 | static int ceph_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf) |
| 1475 | { |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 1476 | struct inode *inode = file_inode(vma->vm_file); |
Yan, Zheng | 61f6881 | 2013-11-28 14:28:14 +0800 | [diff] [blame] | 1477 | struct ceph_inode_info *ci = ceph_inode(inode); |
| 1478 | struct ceph_file_info *fi = vma->vm_file->private_data; |
Yan, Zheng | f66fd9f | 2015-06-10 17:26:13 +0800 | [diff] [blame] | 1479 | struct ceph_cap_flush *prealloc_cf; |
Yan, Zheng | 61f6881 | 2013-11-28 14:28:14 +0800 | [diff] [blame] | 1480 | struct page *page = vmf->page; |
Alex Elder | 6285bc2 | 2012-10-02 10:25:51 -0500 | [diff] [blame] | 1481 | loff_t off = page_offset(page); |
Yan, Zheng | 61f6881 | 2013-11-28 14:28:14 +0800 | [diff] [blame] | 1482 | loff_t size = i_size_read(inode); |
| 1483 | size_t len; |
| 1484 | int want, got, ret; |
Yan, Zheng | 4f7e89f | 2016-05-10 18:40:28 +0800 | [diff] [blame] | 1485 | sigset_t oldset; |
Sage Weil | 1d3576f | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1486 | |
Yan, Zheng | f66fd9f | 2015-06-10 17:26:13 +0800 | [diff] [blame] | 1487 | prealloc_cf = ceph_alloc_cap_flush(); |
| 1488 | if (!prealloc_cf) |
Yan, Zheng | 6ce026e | 2016-05-10 18:59:13 +0800 | [diff] [blame] | 1489 | return VM_FAULT_OOM; |
Yan, Zheng | f66fd9f | 2015-06-10 17:26:13 +0800 | [diff] [blame] | 1490 | |
Yan, Zheng | 4f7e89f | 2016-05-10 18:40:28 +0800 | [diff] [blame] | 1491 | ceph_block_sigs(&oldset); |
Sage Weil | 1d3576f | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1492 | |
Yan, Zheng | 28127bd | 2014-11-14 22:38:29 +0800 | [diff] [blame] | 1493 | if (ci->i_inline_version != CEPH_INLINE_NONE) { |
| 1494 | struct page *locked_page = NULL; |
| 1495 | if (off == 0) { |
| 1496 | lock_page(page); |
| 1497 | locked_page = page; |
| 1498 | } |
| 1499 | ret = ceph_uninline_data(vma->vm_file, locked_page); |
| 1500 | if (locked_page) |
| 1501 | unlock_page(locked_page); |
Yan, Zheng | 6ce026e | 2016-05-10 18:59:13 +0800 | [diff] [blame] | 1502 | if (ret < 0) |
Yan, Zheng | f66fd9f | 2015-06-10 17:26:13 +0800 | [diff] [blame] | 1503 | goto out_free; |
Yan, Zheng | 28127bd | 2014-11-14 22:38:29 +0800 | [diff] [blame] | 1504 | } |
| 1505 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1506 | if (off + PAGE_SIZE <= size) |
| 1507 | len = PAGE_SIZE; |
Sage Weil | 1d3576f | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1508 | else |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1509 | len = size & ~PAGE_MASK; |
Sage Weil | 1d3576f | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1510 | |
Yan, Zheng | 61f6881 | 2013-11-28 14:28:14 +0800 | [diff] [blame] | 1511 | dout("page_mkwrite %p %llx.%llx %llu~%zd getting caps i_size %llu\n", |
| 1512 | inode, ceph_vinop(inode), off, len, size); |
| 1513 | if (fi->fmode & CEPH_FILE_MODE_LAZY) |
| 1514 | want = CEPH_CAP_FILE_BUFFER | CEPH_CAP_FILE_LAZYIO; |
| 1515 | else |
| 1516 | want = CEPH_CAP_FILE_BUFFER; |
Yan, Zheng | 4f7e89f | 2016-05-10 18:40:28 +0800 | [diff] [blame] | 1517 | |
| 1518 | got = 0; |
| 1519 | ret = ceph_get_caps(ci, CEPH_CAP_FILE_WR, want, off + len, |
| 1520 | &got, NULL); |
Yan, Zheng | 6ce026e | 2016-05-10 18:59:13 +0800 | [diff] [blame] | 1521 | if (ret < 0) |
Yan, Zheng | 4f7e89f | 2016-05-10 18:40:28 +0800 | [diff] [blame] | 1522 | goto out_free; |
Yan, Zheng | 6ce026e | 2016-05-10 18:59:13 +0800 | [diff] [blame] | 1523 | |
Yan, Zheng | 61f6881 | 2013-11-28 14:28:14 +0800 | [diff] [blame] | 1524 | dout("page_mkwrite %p %llu~%zd got cap refs on %s\n", |
| 1525 | inode, off, len, ceph_cap_string(got)); |
| 1526 | |
| 1527 | /* Update time before taking page lock */ |
| 1528 | file_update_time(vma->vm_file); |
Yehuda Sadeh | 4af6b22 | 2010-02-09 11:02:51 -0800 | [diff] [blame] | 1529 | |
Yan, Zheng | f0b33df | 2016-05-10 19:09:06 +0800 | [diff] [blame] | 1530 | do { |
| 1531 | lock_page(page); |
Yehuda Sadeh | 4af6b22 | 2010-02-09 11:02:51 -0800 | [diff] [blame] | 1532 | |
Yan, Zheng | f0b33df | 2016-05-10 19:09:06 +0800 | [diff] [blame] | 1533 | if ((off > size) || (page->mapping != inode->i_mapping)) { |
| 1534 | unlock_page(page); |
| 1535 | ret = VM_FAULT_NOPAGE; |
| 1536 | break; |
| 1537 | } |
Yehuda Sadeh | 4af6b22 | 2010-02-09 11:02:51 -0800 | [diff] [blame] | 1538 | |
Yan, Zheng | f0b33df | 2016-05-10 19:09:06 +0800 | [diff] [blame] | 1539 | ret = ceph_update_writeable_page(vma->vm_file, off, len, page); |
| 1540 | if (ret >= 0) { |
| 1541 | /* success. we'll keep the page locked. */ |
| 1542 | set_page_dirty(page); |
| 1543 | ret = VM_FAULT_LOCKED; |
| 1544 | } |
| 1545 | } while (ret == -EAGAIN); |
| 1546 | |
Yan, Zheng | 28127bd | 2014-11-14 22:38:29 +0800 | [diff] [blame] | 1547 | if (ret == VM_FAULT_LOCKED || |
| 1548 | ci->i_inline_version != CEPH_INLINE_NONE) { |
Yan, Zheng | 61f6881 | 2013-11-28 14:28:14 +0800 | [diff] [blame] | 1549 | int dirty; |
| 1550 | spin_lock(&ci->i_ceph_lock); |
Yan, Zheng | 28127bd | 2014-11-14 22:38:29 +0800 | [diff] [blame] | 1551 | ci->i_inline_version = CEPH_INLINE_NONE; |
Yan, Zheng | f66fd9f | 2015-06-10 17:26:13 +0800 | [diff] [blame] | 1552 | dirty = __ceph_mark_dirty_caps(ci, CEPH_CAP_FILE_WR, |
| 1553 | &prealloc_cf); |
Yan, Zheng | 61f6881 | 2013-11-28 14:28:14 +0800 | [diff] [blame] | 1554 | spin_unlock(&ci->i_ceph_lock); |
| 1555 | if (dirty) |
| 1556 | __mark_inode_dirty(inode, dirty); |
| 1557 | } |
| 1558 | |
| 1559 | dout("page_mkwrite %p %llu~%zd dropping cap refs on %s ret %d\n", |
| 1560 | inode, off, len, ceph_cap_string(got), ret); |
| 1561 | ceph_put_cap_refs(ci, got); |
Yan, Zheng | f66fd9f | 2015-06-10 17:26:13 +0800 | [diff] [blame] | 1562 | out_free: |
Yan, Zheng | 4f7e89f | 2016-05-10 18:40:28 +0800 | [diff] [blame] | 1563 | ceph_restore_sigs(&oldset); |
Yan, Zheng | f66fd9f | 2015-06-10 17:26:13 +0800 | [diff] [blame] | 1564 | ceph_free_cap_flush(prealloc_cf); |
Yan, Zheng | 6ce026e | 2016-05-10 18:59:13 +0800 | [diff] [blame] | 1565 | if (ret < 0) |
| 1566 | ret = (ret == -ENOMEM) ? VM_FAULT_OOM : VM_FAULT_SIGBUS; |
Sage Weil | 1d3576f | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1567 | return ret; |
| 1568 | } |
| 1569 | |
Yan, Zheng | 31c542a | 2014-11-14 21:41:55 +0800 | [diff] [blame] | 1570 | void ceph_fill_inline_data(struct inode *inode, struct page *locked_page, |
| 1571 | char *data, size_t len) |
| 1572 | { |
| 1573 | struct address_space *mapping = inode->i_mapping; |
| 1574 | struct page *page; |
| 1575 | |
| 1576 | if (locked_page) { |
| 1577 | page = locked_page; |
| 1578 | } else { |
| 1579 | if (i_size_read(inode) == 0) |
| 1580 | return; |
| 1581 | page = find_or_create_page(mapping, 0, |
Michal Hocko | c62d255 | 2015-11-06 16:28:49 -0800 | [diff] [blame] | 1582 | mapping_gfp_constraint(mapping, |
| 1583 | ~__GFP_FS)); |
Yan, Zheng | 31c542a | 2014-11-14 21:41:55 +0800 | [diff] [blame] | 1584 | if (!page) |
| 1585 | return; |
| 1586 | if (PageUptodate(page)) { |
| 1587 | unlock_page(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1588 | put_page(page); |
Yan, Zheng | 31c542a | 2014-11-14 21:41:55 +0800 | [diff] [blame] | 1589 | return; |
| 1590 | } |
| 1591 | } |
| 1592 | |
Ilya Dryomov | 0668ff5 | 2014-12-19 13:10:10 +0300 | [diff] [blame] | 1593 | dout("fill_inline_data %p %llx.%llx len %zu locked_page %p\n", |
Yan, Zheng | 31c542a | 2014-11-14 21:41:55 +0800 | [diff] [blame] | 1594 | inode, ceph_vinop(inode), len, locked_page); |
| 1595 | |
| 1596 | if (len > 0) { |
| 1597 | void *kaddr = kmap_atomic(page); |
| 1598 | memcpy(kaddr, data, len); |
| 1599 | kunmap_atomic(kaddr); |
| 1600 | } |
| 1601 | |
| 1602 | if (page != locked_page) { |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1603 | if (len < PAGE_SIZE) |
| 1604 | zero_user_segment(page, len, PAGE_SIZE); |
Yan, Zheng | 31c542a | 2014-11-14 21:41:55 +0800 | [diff] [blame] | 1605 | else |
| 1606 | flush_dcache_page(page); |
| 1607 | |
| 1608 | SetPageUptodate(page); |
| 1609 | unlock_page(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1610 | put_page(page); |
Yan, Zheng | 31c542a | 2014-11-14 21:41:55 +0800 | [diff] [blame] | 1611 | } |
| 1612 | } |
| 1613 | |
Yan, Zheng | 28127bd | 2014-11-14 22:38:29 +0800 | [diff] [blame] | 1614 | int ceph_uninline_data(struct file *filp, struct page *locked_page) |
| 1615 | { |
| 1616 | struct inode *inode = file_inode(filp); |
| 1617 | struct ceph_inode_info *ci = ceph_inode(inode); |
| 1618 | struct ceph_fs_client *fsc = ceph_inode_to_client(inode); |
| 1619 | struct ceph_osd_request *req; |
| 1620 | struct page *page = NULL; |
| 1621 | u64 len, inline_version; |
| 1622 | int err = 0; |
| 1623 | bool from_pagecache = false; |
| 1624 | |
| 1625 | spin_lock(&ci->i_ceph_lock); |
| 1626 | inline_version = ci->i_inline_version; |
| 1627 | spin_unlock(&ci->i_ceph_lock); |
| 1628 | |
| 1629 | dout("uninline_data %p %llx.%llx inline_version %llu\n", |
| 1630 | inode, ceph_vinop(inode), inline_version); |
| 1631 | |
| 1632 | if (inline_version == 1 || /* initial version, no data */ |
| 1633 | inline_version == CEPH_INLINE_NONE) |
| 1634 | goto out; |
| 1635 | |
| 1636 | if (locked_page) { |
| 1637 | page = locked_page; |
| 1638 | WARN_ON(!PageUptodate(page)); |
| 1639 | } else if (ceph_caps_issued(ci) & |
| 1640 | (CEPH_CAP_FILE_CACHE|CEPH_CAP_FILE_LAZYIO)) { |
| 1641 | page = find_get_page(inode->i_mapping, 0); |
| 1642 | if (page) { |
| 1643 | if (PageUptodate(page)) { |
| 1644 | from_pagecache = true; |
| 1645 | lock_page(page); |
| 1646 | } else { |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1647 | put_page(page); |
Yan, Zheng | 28127bd | 2014-11-14 22:38:29 +0800 | [diff] [blame] | 1648 | page = NULL; |
| 1649 | } |
| 1650 | } |
| 1651 | } |
| 1652 | |
| 1653 | if (page) { |
| 1654 | len = i_size_read(inode); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1655 | if (len > PAGE_SIZE) |
| 1656 | len = PAGE_SIZE; |
Yan, Zheng | 28127bd | 2014-11-14 22:38:29 +0800 | [diff] [blame] | 1657 | } else { |
| 1658 | page = __page_cache_alloc(GFP_NOFS); |
| 1659 | if (!page) { |
| 1660 | err = -ENOMEM; |
| 1661 | goto out; |
| 1662 | } |
| 1663 | err = __ceph_do_getattr(inode, page, |
| 1664 | CEPH_STAT_CAP_INLINE_DATA, true); |
| 1665 | if (err < 0) { |
| 1666 | /* no inline data */ |
| 1667 | if (err == -ENODATA) |
| 1668 | err = 0; |
| 1669 | goto out; |
| 1670 | } |
| 1671 | len = err; |
| 1672 | } |
| 1673 | |
| 1674 | req = ceph_osdc_new_request(&fsc->client->osdc, &ci->i_layout, |
| 1675 | ceph_vino(inode), 0, &len, 0, 1, |
| 1676 | CEPH_OSD_OP_CREATE, |
| 1677 | CEPH_OSD_FLAG_ONDISK | CEPH_OSD_FLAG_WRITE, |
Ilya Dryomov | 34b759b | 2016-02-16 15:00:24 +0100 | [diff] [blame] | 1678 | NULL, 0, 0, false); |
Yan, Zheng | 28127bd | 2014-11-14 22:38:29 +0800 | [diff] [blame] | 1679 | if (IS_ERR(req)) { |
| 1680 | err = PTR_ERR(req); |
| 1681 | goto out; |
| 1682 | } |
| 1683 | |
Ilya Dryomov | bb873b5 | 2016-05-26 00:29:52 +0200 | [diff] [blame] | 1684 | req->r_mtime = inode->i_mtime; |
Yan, Zheng | 28127bd | 2014-11-14 22:38:29 +0800 | [diff] [blame] | 1685 | err = ceph_osdc_start_request(&fsc->client->osdc, req, false); |
| 1686 | if (!err) |
| 1687 | err = ceph_osdc_wait_request(&fsc->client->osdc, req); |
| 1688 | ceph_osdc_put_request(req); |
| 1689 | if (err < 0) |
| 1690 | goto out; |
| 1691 | |
| 1692 | req = ceph_osdc_new_request(&fsc->client->osdc, &ci->i_layout, |
| 1693 | ceph_vino(inode), 0, &len, 1, 3, |
| 1694 | CEPH_OSD_OP_WRITE, |
| 1695 | CEPH_OSD_FLAG_ONDISK | CEPH_OSD_FLAG_WRITE, |
Ilya Dryomov | 34b759b | 2016-02-16 15:00:24 +0100 | [diff] [blame] | 1696 | NULL, ci->i_truncate_seq, |
| 1697 | ci->i_truncate_size, false); |
Yan, Zheng | 28127bd | 2014-11-14 22:38:29 +0800 | [diff] [blame] | 1698 | if (IS_ERR(req)) { |
| 1699 | err = PTR_ERR(req); |
| 1700 | goto out; |
| 1701 | } |
| 1702 | |
| 1703 | osd_req_op_extent_osd_data_pages(req, 1, &page, len, 0, false, false); |
| 1704 | |
Yan, Zheng | ec137c1 | 2015-04-13 11:25:07 +0800 | [diff] [blame] | 1705 | { |
| 1706 | __le64 xattr_buf = cpu_to_le64(inline_version); |
| 1707 | err = osd_req_op_xattr_init(req, 0, CEPH_OSD_OP_CMPXATTR, |
| 1708 | "inline_version", &xattr_buf, |
| 1709 | sizeof(xattr_buf), |
| 1710 | CEPH_OSD_CMPXATTR_OP_GT, |
| 1711 | CEPH_OSD_CMPXATTR_MODE_U64); |
| 1712 | if (err) |
| 1713 | goto out_put; |
| 1714 | } |
Yan, Zheng | 28127bd | 2014-11-14 22:38:29 +0800 | [diff] [blame] | 1715 | |
Yan, Zheng | ec137c1 | 2015-04-13 11:25:07 +0800 | [diff] [blame] | 1716 | { |
| 1717 | char xattr_buf[32]; |
| 1718 | int xattr_len = snprintf(xattr_buf, sizeof(xattr_buf), |
| 1719 | "%llu", inline_version); |
| 1720 | err = osd_req_op_xattr_init(req, 2, CEPH_OSD_OP_SETXATTR, |
| 1721 | "inline_version", |
| 1722 | xattr_buf, xattr_len, 0, 0); |
| 1723 | if (err) |
| 1724 | goto out_put; |
| 1725 | } |
Yan, Zheng | 28127bd | 2014-11-14 22:38:29 +0800 | [diff] [blame] | 1726 | |
Ilya Dryomov | bb873b5 | 2016-05-26 00:29:52 +0200 | [diff] [blame] | 1727 | req->r_mtime = inode->i_mtime; |
Yan, Zheng | 28127bd | 2014-11-14 22:38:29 +0800 | [diff] [blame] | 1728 | err = ceph_osdc_start_request(&fsc->client->osdc, req, false); |
| 1729 | if (!err) |
| 1730 | err = ceph_osdc_wait_request(&fsc->client->osdc, req); |
| 1731 | out_put: |
| 1732 | ceph_osdc_put_request(req); |
| 1733 | if (err == -ECANCELED) |
| 1734 | err = 0; |
| 1735 | out: |
| 1736 | if (page && page != locked_page) { |
| 1737 | if (from_pagecache) { |
| 1738 | unlock_page(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1739 | put_page(page); |
Yan, Zheng | 28127bd | 2014-11-14 22:38:29 +0800 | [diff] [blame] | 1740 | } else |
| 1741 | __free_pages(page, 0); |
| 1742 | } |
| 1743 | |
| 1744 | dout("uninline_data %p %llx.%llx inline_version %llu = %d\n", |
| 1745 | inode, ceph_vinop(inode), inline_version, err); |
| 1746 | return err; |
| 1747 | } |
| 1748 | |
Kirill A. Shutemov | 7cbea8d | 2015-09-09 15:39:26 -0700 | [diff] [blame] | 1749 | static const struct vm_operations_struct ceph_vmops = { |
Yan, Zheng | 61f6881 | 2013-11-28 14:28:14 +0800 | [diff] [blame] | 1750 | .fault = ceph_filemap_fault, |
Sage Weil | 1d3576f | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1751 | .page_mkwrite = ceph_page_mkwrite, |
| 1752 | }; |
| 1753 | |
| 1754 | int ceph_mmap(struct file *file, struct vm_area_struct *vma) |
| 1755 | { |
| 1756 | struct address_space *mapping = file->f_mapping; |
| 1757 | |
| 1758 | if (!mapping->a_ops->readpage) |
| 1759 | return -ENOEXEC; |
| 1760 | file_accessed(file); |
| 1761 | vma->vm_ops = &ceph_vmops; |
Sage Weil | 1d3576f | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1762 | return 0; |
| 1763 | } |
Yan, Zheng | 10183a6 | 2015-04-27 15:33:28 +0800 | [diff] [blame] | 1764 | |
| 1765 | enum { |
| 1766 | POOL_READ = 1, |
| 1767 | POOL_WRITE = 2, |
| 1768 | }; |
| 1769 | |
Yan, Zheng | 779fe0f | 2016-03-07 09:35:06 +0800 | [diff] [blame] | 1770 | static int __ceph_pool_perm_get(struct ceph_inode_info *ci, |
| 1771 | s64 pool, struct ceph_string *pool_ns) |
Yan, Zheng | 10183a6 | 2015-04-27 15:33:28 +0800 | [diff] [blame] | 1772 | { |
| 1773 | struct ceph_fs_client *fsc = ceph_inode_to_client(&ci->vfs_inode); |
| 1774 | struct ceph_mds_client *mdsc = fsc->mdsc; |
| 1775 | struct ceph_osd_request *rd_req = NULL, *wr_req = NULL; |
| 1776 | struct rb_node **p, *parent; |
| 1777 | struct ceph_pool_perm *perm; |
| 1778 | struct page **pages; |
Yan, Zheng | 779fe0f | 2016-03-07 09:35:06 +0800 | [diff] [blame] | 1779 | size_t pool_ns_len; |
Yan, Zheng | 10183a6 | 2015-04-27 15:33:28 +0800 | [diff] [blame] | 1780 | int err = 0, err2 = 0, have = 0; |
| 1781 | |
| 1782 | down_read(&mdsc->pool_perm_rwsem); |
| 1783 | p = &mdsc->pool_perm_tree.rb_node; |
| 1784 | while (*p) { |
| 1785 | perm = rb_entry(*p, struct ceph_pool_perm, node); |
| 1786 | if (pool < perm->pool) |
| 1787 | p = &(*p)->rb_left; |
| 1788 | else if (pool > perm->pool) |
| 1789 | p = &(*p)->rb_right; |
| 1790 | else { |
Yan, Zheng | 779fe0f | 2016-03-07 09:35:06 +0800 | [diff] [blame] | 1791 | int ret = ceph_compare_string(pool_ns, |
| 1792 | perm->pool_ns, |
| 1793 | perm->pool_ns_len); |
| 1794 | if (ret < 0) |
| 1795 | p = &(*p)->rb_left; |
| 1796 | else if (ret > 0) |
| 1797 | p = &(*p)->rb_right; |
| 1798 | else { |
| 1799 | have = perm->perm; |
| 1800 | break; |
| 1801 | } |
Yan, Zheng | 10183a6 | 2015-04-27 15:33:28 +0800 | [diff] [blame] | 1802 | } |
| 1803 | } |
| 1804 | up_read(&mdsc->pool_perm_rwsem); |
| 1805 | if (*p) |
| 1806 | goto out; |
| 1807 | |
Yan, Zheng | 779fe0f | 2016-03-07 09:35:06 +0800 | [diff] [blame] | 1808 | if (pool_ns) |
| 1809 | dout("__ceph_pool_perm_get pool %lld ns %.*s no perm cached\n", |
| 1810 | pool, (int)pool_ns->len, pool_ns->str); |
| 1811 | else |
| 1812 | dout("__ceph_pool_perm_get pool %lld no perm cached\n", pool); |
Yan, Zheng | 10183a6 | 2015-04-27 15:33:28 +0800 | [diff] [blame] | 1813 | |
| 1814 | down_write(&mdsc->pool_perm_rwsem); |
Yan, Zheng | 779fe0f | 2016-03-07 09:35:06 +0800 | [diff] [blame] | 1815 | p = &mdsc->pool_perm_tree.rb_node; |
Yan, Zheng | 10183a6 | 2015-04-27 15:33:28 +0800 | [diff] [blame] | 1816 | parent = NULL; |
| 1817 | while (*p) { |
| 1818 | parent = *p; |
| 1819 | perm = rb_entry(parent, struct ceph_pool_perm, node); |
| 1820 | if (pool < perm->pool) |
| 1821 | p = &(*p)->rb_left; |
| 1822 | else if (pool > perm->pool) |
| 1823 | p = &(*p)->rb_right; |
| 1824 | else { |
Yan, Zheng | 779fe0f | 2016-03-07 09:35:06 +0800 | [diff] [blame] | 1825 | int ret = ceph_compare_string(pool_ns, |
| 1826 | perm->pool_ns, |
| 1827 | perm->pool_ns_len); |
| 1828 | if (ret < 0) |
| 1829 | p = &(*p)->rb_left; |
| 1830 | else if (ret > 0) |
| 1831 | p = &(*p)->rb_right; |
| 1832 | else { |
| 1833 | have = perm->perm; |
| 1834 | break; |
| 1835 | } |
Yan, Zheng | 10183a6 | 2015-04-27 15:33:28 +0800 | [diff] [blame] | 1836 | } |
| 1837 | } |
| 1838 | if (*p) { |
| 1839 | up_write(&mdsc->pool_perm_rwsem); |
| 1840 | goto out; |
| 1841 | } |
| 1842 | |
Ilya Dryomov | 34b759b | 2016-02-16 15:00:24 +0100 | [diff] [blame] | 1843 | rd_req = ceph_osdc_alloc_request(&fsc->client->osdc, NULL, |
Yan, Zheng | 10183a6 | 2015-04-27 15:33:28 +0800 | [diff] [blame] | 1844 | 1, false, GFP_NOFS); |
| 1845 | if (!rd_req) { |
| 1846 | err = -ENOMEM; |
| 1847 | goto out_unlock; |
| 1848 | } |
| 1849 | |
| 1850 | rd_req->r_flags = CEPH_OSD_FLAG_READ; |
| 1851 | osd_req_op_init(rd_req, 0, CEPH_OSD_OP_STAT, 0); |
| 1852 | rd_req->r_base_oloc.pool = pool; |
Yan, Zheng | 779fe0f | 2016-03-07 09:35:06 +0800 | [diff] [blame] | 1853 | if (pool_ns) |
| 1854 | rd_req->r_base_oloc.pool_ns = ceph_get_string(pool_ns); |
Ilya Dryomov | d30291b | 2016-04-29 19:54:20 +0200 | [diff] [blame] | 1855 | ceph_oid_printf(&rd_req->r_base_oid, "%llx.00000000", ci->i_vino.ino); |
Yan, Zheng | 10183a6 | 2015-04-27 15:33:28 +0800 | [diff] [blame] | 1856 | |
Ilya Dryomov | 13d1ad1 | 2016-04-27 14:15:51 +0200 | [diff] [blame] | 1857 | err = ceph_osdc_alloc_messages(rd_req, GFP_NOFS); |
| 1858 | if (err) |
| 1859 | goto out_unlock; |
Yan, Zheng | 10183a6 | 2015-04-27 15:33:28 +0800 | [diff] [blame] | 1860 | |
Ilya Dryomov | 34b759b | 2016-02-16 15:00:24 +0100 | [diff] [blame] | 1861 | wr_req = ceph_osdc_alloc_request(&fsc->client->osdc, NULL, |
Yan, Zheng | 10183a6 | 2015-04-27 15:33:28 +0800 | [diff] [blame] | 1862 | 1, false, GFP_NOFS); |
| 1863 | if (!wr_req) { |
| 1864 | err = -ENOMEM; |
| 1865 | goto out_unlock; |
| 1866 | } |
| 1867 | |
Ilya Dryomov | fe5da05 | 2016-04-28 16:07:24 +0200 | [diff] [blame] | 1868 | wr_req->r_flags = CEPH_OSD_FLAG_WRITE | CEPH_OSD_FLAG_ACK; |
Yan, Zheng | 10183a6 | 2015-04-27 15:33:28 +0800 | [diff] [blame] | 1869 | osd_req_op_init(wr_req, 0, CEPH_OSD_OP_CREATE, CEPH_OSD_OP_FLAG_EXCL); |
Ilya Dryomov | 63244fa | 2016-04-28 16:07:23 +0200 | [diff] [blame] | 1870 | ceph_oloc_copy(&wr_req->r_base_oloc, &rd_req->r_base_oloc); |
Ilya Dryomov | d30291b | 2016-04-29 19:54:20 +0200 | [diff] [blame] | 1871 | ceph_oid_copy(&wr_req->r_base_oid, &rd_req->r_base_oid); |
Yan, Zheng | 10183a6 | 2015-04-27 15:33:28 +0800 | [diff] [blame] | 1872 | |
Ilya Dryomov | 13d1ad1 | 2016-04-27 14:15:51 +0200 | [diff] [blame] | 1873 | err = ceph_osdc_alloc_messages(wr_req, GFP_NOFS); |
| 1874 | if (err) |
| 1875 | goto out_unlock; |
Yan, Zheng | 10183a6 | 2015-04-27 15:33:28 +0800 | [diff] [blame] | 1876 | |
| 1877 | /* one page should be large enough for STAT data */ |
| 1878 | pages = ceph_alloc_page_vector(1, GFP_KERNEL); |
| 1879 | if (IS_ERR(pages)) { |
| 1880 | err = PTR_ERR(pages); |
| 1881 | goto out_unlock; |
| 1882 | } |
| 1883 | |
| 1884 | osd_req_op_raw_data_in_pages(rd_req, 0, pages, PAGE_SIZE, |
| 1885 | 0, false, true); |
Yan, Zheng | 10183a6 | 2015-04-27 15:33:28 +0800 | [diff] [blame] | 1886 | err = ceph_osdc_start_request(&fsc->client->osdc, rd_req, false); |
| 1887 | |
Ilya Dryomov | bb873b5 | 2016-05-26 00:29:52 +0200 | [diff] [blame] | 1888 | wr_req->r_mtime = ci->vfs_inode.i_mtime; |
Yan, Zheng | 10183a6 | 2015-04-27 15:33:28 +0800 | [diff] [blame] | 1889 | err2 = ceph_osdc_start_request(&fsc->client->osdc, wr_req, false); |
| 1890 | |
| 1891 | if (!err) |
| 1892 | err = ceph_osdc_wait_request(&fsc->client->osdc, rd_req); |
| 1893 | if (!err2) |
| 1894 | err2 = ceph_osdc_wait_request(&fsc->client->osdc, wr_req); |
| 1895 | |
| 1896 | if (err >= 0 || err == -ENOENT) |
| 1897 | have |= POOL_READ; |
| 1898 | else if (err != -EPERM) |
| 1899 | goto out_unlock; |
| 1900 | |
| 1901 | if (err2 == 0 || err2 == -EEXIST) |
| 1902 | have |= POOL_WRITE; |
| 1903 | else if (err2 != -EPERM) { |
| 1904 | err = err2; |
| 1905 | goto out_unlock; |
| 1906 | } |
| 1907 | |
Yan, Zheng | 779fe0f | 2016-03-07 09:35:06 +0800 | [diff] [blame] | 1908 | pool_ns_len = pool_ns ? pool_ns->len : 0; |
| 1909 | perm = kmalloc(sizeof(*perm) + pool_ns_len + 1, GFP_NOFS); |
Yan, Zheng | 10183a6 | 2015-04-27 15:33:28 +0800 | [diff] [blame] | 1910 | if (!perm) { |
| 1911 | err = -ENOMEM; |
| 1912 | goto out_unlock; |
| 1913 | } |
| 1914 | |
| 1915 | perm->pool = pool; |
| 1916 | perm->perm = have; |
Yan, Zheng | 779fe0f | 2016-03-07 09:35:06 +0800 | [diff] [blame] | 1917 | perm->pool_ns_len = pool_ns_len; |
| 1918 | if (pool_ns_len > 0) |
| 1919 | memcpy(perm->pool_ns, pool_ns->str, pool_ns_len); |
| 1920 | perm->pool_ns[pool_ns_len] = 0; |
| 1921 | |
Yan, Zheng | 10183a6 | 2015-04-27 15:33:28 +0800 | [diff] [blame] | 1922 | rb_link_node(&perm->node, parent, p); |
| 1923 | rb_insert_color(&perm->node, &mdsc->pool_perm_tree); |
| 1924 | err = 0; |
| 1925 | out_unlock: |
| 1926 | up_write(&mdsc->pool_perm_rwsem); |
| 1927 | |
Ilya Dryomov | 3ed97d6 | 2016-04-26 15:05:29 +0200 | [diff] [blame] | 1928 | ceph_osdc_put_request(rd_req); |
| 1929 | ceph_osdc_put_request(wr_req); |
Yan, Zheng | 10183a6 | 2015-04-27 15:33:28 +0800 | [diff] [blame] | 1930 | out: |
| 1931 | if (!err) |
| 1932 | err = have; |
Yan, Zheng | 779fe0f | 2016-03-07 09:35:06 +0800 | [diff] [blame] | 1933 | if (pool_ns) |
| 1934 | dout("__ceph_pool_perm_get pool %lld ns %.*s result = %d\n", |
| 1935 | pool, (int)pool_ns->len, pool_ns->str, err); |
| 1936 | else |
| 1937 | dout("__ceph_pool_perm_get pool %lld result = %d\n", pool, err); |
Yan, Zheng | 10183a6 | 2015-04-27 15:33:28 +0800 | [diff] [blame] | 1938 | return err; |
| 1939 | } |
| 1940 | |
| 1941 | int ceph_pool_perm_check(struct ceph_inode_info *ci, int need) |
| 1942 | { |
Yan, Zheng | 7627151 | 2016-02-03 21:24:49 +0800 | [diff] [blame] | 1943 | s64 pool; |
Yan, Zheng | 779fe0f | 2016-03-07 09:35:06 +0800 | [diff] [blame] | 1944 | struct ceph_string *pool_ns; |
Yan, Zheng | 10183a6 | 2015-04-27 15:33:28 +0800 | [diff] [blame] | 1945 | int ret, flags; |
| 1946 | |
| 1947 | if (ceph_test_mount_opt(ceph_inode_to_client(&ci->vfs_inode), |
| 1948 | NOPOOLPERM)) |
| 1949 | return 0; |
| 1950 | |
| 1951 | spin_lock(&ci->i_ceph_lock); |
| 1952 | flags = ci->i_ceph_flags; |
Yan, Zheng | 7627151 | 2016-02-03 21:24:49 +0800 | [diff] [blame] | 1953 | pool = ci->i_layout.pool_id; |
Yan, Zheng | 10183a6 | 2015-04-27 15:33:28 +0800 | [diff] [blame] | 1954 | spin_unlock(&ci->i_ceph_lock); |
| 1955 | check: |
| 1956 | if (flags & CEPH_I_POOL_PERM) { |
| 1957 | if ((need & CEPH_CAP_FILE_RD) && !(flags & CEPH_I_POOL_RD)) { |
Yan, Zheng | 7627151 | 2016-02-03 21:24:49 +0800 | [diff] [blame] | 1958 | dout("ceph_pool_perm_check pool %lld no read perm\n", |
Yan, Zheng | 10183a6 | 2015-04-27 15:33:28 +0800 | [diff] [blame] | 1959 | pool); |
| 1960 | return -EPERM; |
| 1961 | } |
| 1962 | if ((need & CEPH_CAP_FILE_WR) && !(flags & CEPH_I_POOL_WR)) { |
Yan, Zheng | 7627151 | 2016-02-03 21:24:49 +0800 | [diff] [blame] | 1963 | dout("ceph_pool_perm_check pool %lld no write perm\n", |
Yan, Zheng | 10183a6 | 2015-04-27 15:33:28 +0800 | [diff] [blame] | 1964 | pool); |
| 1965 | return -EPERM; |
| 1966 | } |
| 1967 | return 0; |
| 1968 | } |
| 1969 | |
Yan, Zheng | 779fe0f | 2016-03-07 09:35:06 +0800 | [diff] [blame] | 1970 | pool_ns = ceph_try_get_string(ci->i_layout.pool_ns); |
| 1971 | ret = __ceph_pool_perm_get(ci, pool, pool_ns); |
| 1972 | ceph_put_string(pool_ns); |
Yan, Zheng | 10183a6 | 2015-04-27 15:33:28 +0800 | [diff] [blame] | 1973 | if (ret < 0) |
| 1974 | return ret; |
| 1975 | |
| 1976 | flags = CEPH_I_POOL_PERM; |
| 1977 | if (ret & POOL_READ) |
| 1978 | flags |= CEPH_I_POOL_RD; |
| 1979 | if (ret & POOL_WRITE) |
| 1980 | flags |= CEPH_I_POOL_WR; |
| 1981 | |
| 1982 | spin_lock(&ci->i_ceph_lock); |
Yan, Zheng | 779fe0f | 2016-03-07 09:35:06 +0800 | [diff] [blame] | 1983 | if (pool == ci->i_layout.pool_id && |
| 1984 | pool_ns == rcu_dereference_raw(ci->i_layout.pool_ns)) { |
| 1985 | ci->i_ceph_flags |= flags; |
Yan, Zheng | 10183a6 | 2015-04-27 15:33:28 +0800 | [diff] [blame] | 1986 | } else { |
Yan, Zheng | 7627151 | 2016-02-03 21:24:49 +0800 | [diff] [blame] | 1987 | pool = ci->i_layout.pool_id; |
Yan, Zheng | 10183a6 | 2015-04-27 15:33:28 +0800 | [diff] [blame] | 1988 | flags = ci->i_ceph_flags; |
| 1989 | } |
| 1990 | spin_unlock(&ci->i_ceph_lock); |
| 1991 | goto check; |
| 1992 | } |
| 1993 | |
| 1994 | void ceph_pool_perm_destroy(struct ceph_mds_client *mdsc) |
| 1995 | { |
| 1996 | struct ceph_pool_perm *perm; |
| 1997 | struct rb_node *n; |
| 1998 | |
| 1999 | while (!RB_EMPTY_ROOT(&mdsc->pool_perm_tree)) { |
| 2000 | n = rb_first(&mdsc->pool_perm_tree); |
| 2001 | perm = rb_entry(n, struct ceph_pool_perm, node); |
| 2002 | rb_erase(n, &mdsc->pool_perm_tree); |
| 2003 | kfree(perm); |
| 2004 | } |
| 2005 | } |