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 | 2b1ac85 | 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 | 2b1ac85 | 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 | cf7b7e14 | 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 | 2b1ac85 | 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 | d641df8 | 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 | 2b1ac85 | 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 | 2b1ac85 | 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 | 2b1ac85 | 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 | 5f743e4 | 2016-11-15 16:04:37 +0800 | [diff] [blame] | 478 | loff_t *snap_size, |
| 479 | u64 *truncate_size, |
| 480 | u32 *truncate_seq) |
Sage Weil | 1d3576f | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 481 | { |
| 482 | struct ceph_inode_info *ci = ceph_inode(inode); |
| 483 | struct ceph_snap_context *snapc = NULL; |
| 484 | struct ceph_cap_snap *capsnap = NULL; |
| 485 | |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 486 | spin_lock(&ci->i_ceph_lock); |
Sage Weil | 1d3576f | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 487 | list_for_each_entry(capsnap, &ci->i_cap_snaps, ci_item) { |
| 488 | dout(" cap_snap %p snapc %p has %d dirty pages\n", capsnap, |
| 489 | capsnap->context, capsnap->dirty_pages); |
| 490 | if (capsnap->dirty_pages) { |
| 491 | snapc = ceph_get_snap_context(capsnap->context); |
| 492 | if (snap_size) |
| 493 | *snap_size = capsnap->size; |
Yan, Zheng | 5f743e4 | 2016-11-15 16:04:37 +0800 | [diff] [blame] | 494 | if (truncate_size) |
| 495 | *truncate_size = capsnap->truncate_size; |
| 496 | if (truncate_seq) |
| 497 | *truncate_seq = capsnap->truncate_seq; |
Sage Weil | 1d3576f | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 498 | break; |
| 499 | } |
| 500 | } |
Sage Weil | 7d8cb26 | 2010-08-24 08:44:16 -0700 | [diff] [blame] | 501 | if (!snapc && ci->i_wrbuffer_ref_head) { |
Sage Weil | 80e755f | 2010-03-31 21:52:10 -0700 | [diff] [blame] | 502 | snapc = ceph_get_snap_context(ci->i_head_snapc); |
Sage Weil | 1d3576f | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 503 | dout(" head snapc %p has %d dirty pages\n", |
| 504 | snapc, ci->i_wrbuffer_ref_head); |
Yan, Zheng | 5f743e4 | 2016-11-15 16:04:37 +0800 | [diff] [blame] | 505 | if (truncate_size) |
Geng, Jichao | 84fcc2d | 2017-01-05 16:50:39 +0800 | [diff] [blame] | 506 | *truncate_size = ci->i_truncate_size; |
Yan, Zheng | 5f743e4 | 2016-11-15 16:04:37 +0800 | [diff] [blame] | 507 | if (truncate_seq) |
Geng, Jichao | 84fcc2d | 2017-01-05 16:50:39 +0800 | [diff] [blame] | 508 | *truncate_seq = ci->i_truncate_seq; |
Sage Weil | 1d3576f | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 509 | } |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 510 | spin_unlock(&ci->i_ceph_lock); |
Sage Weil | 1d3576f | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 511 | return snapc; |
| 512 | } |
| 513 | |
| 514 | /* |
| 515 | * Write a single page, but leave the page locked. |
| 516 | * |
| 517 | * If we get a write error, set the page error bit, but still adjust the |
| 518 | * dirty page accounting (i.e., page is no longer dirty). |
| 519 | */ |
| 520 | static int writepage_nounlock(struct page *page, struct writeback_control *wbc) |
| 521 | { |
| 522 | struct inode *inode; |
| 523 | struct ceph_inode_info *ci; |
Yehuda Sadeh | 3d14c5d | 2010-04-06 15:14:15 -0700 | [diff] [blame] | 524 | struct ceph_fs_client *fsc; |
Sage Weil | 1d3576f | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 525 | struct ceph_osd_client *osdc; |
Sage Weil | 6298a33 | 2010-03-31 22:01:38 -0700 | [diff] [blame] | 526 | struct ceph_snap_context *snapc, *oldest; |
Yan, Zheng | fc2744a | 2013-05-31 16:48:29 +0800 | [diff] [blame] | 527 | loff_t page_off = page_offset(page); |
Yan, Zheng | e1966b4 | 2015-06-18 03:10:58 +0800 | [diff] [blame] | 528 | loff_t snap_size = -1; |
Yehuda Sadeh | 2baba25 | 2009-12-18 13:51:57 -0800 | [diff] [blame] | 529 | long writeback_stat; |
Yan, Zheng | e1966b4 | 2015-06-18 03:10:58 +0800 | [diff] [blame] | 530 | u64 truncate_size; |
Yan, Zheng | fc2744a | 2013-05-31 16:48:29 +0800 | [diff] [blame] | 531 | u32 truncate_seq; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 532 | int err = 0, len = PAGE_SIZE; |
Sage Weil | 1d3576f | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 533 | |
| 534 | dout("writepage %p idx %lu\n", page, page->index); |
| 535 | |
| 536 | if (!page->mapping || !page->mapping->host) { |
| 537 | dout("writepage %p - no mapping\n", page); |
| 538 | return -EFAULT; |
| 539 | } |
| 540 | inode = page->mapping->host; |
| 541 | ci = ceph_inode(inode); |
Yehuda Sadeh | 3d14c5d | 2010-04-06 15:14:15 -0700 | [diff] [blame] | 542 | fsc = ceph_inode_to_client(inode); |
| 543 | osdc = &fsc->client->osdc; |
Sage Weil | 1d3576f | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 544 | |
| 545 | /* verify this is a writeable snap context */ |
Yan, Zheng | 61600ef | 2012-05-28 14:44:30 +0800 | [diff] [blame] | 546 | snapc = page_snap_context(page); |
Sage Weil | 1d3576f | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 547 | if (snapc == NULL) { |
| 548 | dout("writepage %p page %p not dirty?\n", inode, page); |
| 549 | goto out; |
| 550 | } |
Yan, Zheng | 5f743e4 | 2016-11-15 16:04:37 +0800 | [diff] [blame] | 551 | oldest = get_oldest_context(inode, &snap_size, |
| 552 | &truncate_size, &truncate_seq); |
Sage Weil | 6298a33 | 2010-03-31 22:01:38 -0700 | [diff] [blame] | 553 | if (snapc->seq > oldest->seq) { |
Sage Weil | 1d3576f | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 554 | dout("writepage %p page %p snapc %p not writeable - noop\n", |
Yan, Zheng | 61600ef | 2012-05-28 14:44:30 +0800 | [diff] [blame] | 555 | inode, page, snapc); |
Sage Weil | 1d3576f | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 556 | /* we should only noop if called by kswapd */ |
| 557 | WARN_ON((current->flags & PF_MEMALLOC) == 0); |
Sage Weil | 6298a33 | 2010-03-31 22:01:38 -0700 | [diff] [blame] | 558 | ceph_put_snap_context(oldest); |
Sage Weil | 1d3576f | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 559 | goto out; |
| 560 | } |
Sage Weil | 6298a33 | 2010-03-31 22:01:38 -0700 | [diff] [blame] | 561 | ceph_put_snap_context(oldest); |
Sage Weil | 1d3576f | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 562 | |
Yan, Zheng | e1966b4 | 2015-06-18 03:10:58 +0800 | [diff] [blame] | 563 | if (snap_size == -1) |
Yan, Zheng | fc2744a | 2013-05-31 16:48:29 +0800 | [diff] [blame] | 564 | snap_size = i_size_read(inode); |
Yan, Zheng | fc2744a | 2013-05-31 16:48:29 +0800 | [diff] [blame] | 565 | |
Sage Weil | 1d3576f | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 566 | /* is this a partial page at end of file? */ |
Yan, Zheng | fc2744a | 2013-05-31 16:48:29 +0800 | [diff] [blame] | 567 | if (page_off >= snap_size) { |
| 568 | dout("%p page eof %llu\n", page, snap_size); |
| 569 | goto out; |
| 570 | } |
| 571 | if (snap_size < page_off + len) |
| 572 | len = snap_size - page_off; |
Sage Weil | 1d3576f | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 573 | |
Sage Weil | ae00d4f | 2010-09-16 16:26:51 -0700 | [diff] [blame] | 574 | dout("writepage %p page %p index %lu on %llu~%u snapc %p\n", |
| 575 | inode, page, page->index, page_off, len, snapc); |
Sage Weil | 1d3576f | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 576 | |
Yehuda Sadeh | 3d14c5d | 2010-04-06 15:14:15 -0700 | [diff] [blame] | 577 | writeback_stat = atomic_long_inc_return(&fsc->writeback_count); |
Yehuda Sadeh | 2baba25 | 2009-12-18 13:51:57 -0800 | [diff] [blame] | 578 | if (writeback_stat > |
Yehuda Sadeh | 3d14c5d | 2010-04-06 15:14:15 -0700 | [diff] [blame] | 579 | CONGESTION_ON_THRESH(fsc->mount_options->congestion_kb)) |
| 580 | set_bdi_congested(&fsc->backing_dev_info, BLK_RW_ASYNC); |
Yehuda Sadeh | 2baba25 | 2009-12-18 13:51:57 -0800 | [diff] [blame] | 581 | |
Sage Weil | 1d3576f | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 582 | set_page_writeback(page); |
| 583 | err = ceph_osdc_writepages(osdc, ceph_vino(inode), |
| 584 | &ci->i_layout, snapc, |
| 585 | page_off, len, |
Yan, Zheng | fc2744a | 2013-05-31 16:48:29 +0800 | [diff] [blame] | 586 | truncate_seq, truncate_size, |
Alex Elder | 2480882 | 2013-02-15 11:42:29 -0600 | [diff] [blame] | 587 | &inode->i_mtime, &page, 1); |
Sage Weil | 1d3576f | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 588 | if (err < 0) { |
Yan, Zheng | ad15ec0 | 2016-05-13 17:29:51 +0800 | [diff] [blame] | 589 | struct writeback_control tmp_wbc; |
| 590 | if (!wbc) |
| 591 | wbc = &tmp_wbc; |
| 592 | if (err == -ERESTARTSYS) { |
| 593 | /* killed by SIGKILL */ |
| 594 | dout("writepage interrupted page %p\n", page); |
| 595 | redirty_page_for_writepage(wbc, page); |
| 596 | end_page_writeback(page); |
| 597 | goto out; |
| 598 | } |
| 599 | dout("writepage setting page/mapping error %d %p\n", |
| 600 | err, page); |
Sage Weil | 1d3576f | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 601 | SetPageError(page); |
| 602 | mapping_set_error(&inode->i_data, err); |
Yan, Zheng | ad15ec0 | 2016-05-13 17:29:51 +0800 | [diff] [blame] | 603 | wbc->pages_skipped++; |
Sage Weil | 1d3576f | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 604 | } else { |
| 605 | dout("writepage cleaned page %p\n", page); |
| 606 | err = 0; /* vfs expects us to return 0 */ |
| 607 | } |
| 608 | page->private = 0; |
| 609 | ClearPagePrivate(page); |
| 610 | end_page_writeback(page); |
| 611 | ceph_put_wrbuffer_cap_refs(ci, 1, snapc); |
Sage Weil | 6298a33 | 2010-03-31 22:01:38 -0700 | [diff] [blame] | 612 | ceph_put_snap_context(snapc); /* page's reference */ |
Sage Weil | 1d3576f | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 613 | out: |
| 614 | return err; |
| 615 | } |
| 616 | |
| 617 | static int ceph_writepage(struct page *page, struct writeback_control *wbc) |
| 618 | { |
Yehuda Sadeh | dbd646a | 2009-12-16 14:51:06 -0800 | [diff] [blame] | 619 | int err; |
| 620 | struct inode *inode = page->mapping->host; |
| 621 | BUG_ON(!inode); |
Sage Weil | 70b666c | 2011-05-27 09:24:26 -0700 | [diff] [blame] | 622 | ihold(inode); |
Yehuda Sadeh | dbd646a | 2009-12-16 14:51:06 -0800 | [diff] [blame] | 623 | err = writepage_nounlock(page, wbc); |
Yan, Zheng | ad15ec0 | 2016-05-13 17:29:51 +0800 | [diff] [blame] | 624 | if (err == -ERESTARTSYS) { |
| 625 | /* direct memory reclaimer was killed by SIGKILL. return 0 |
| 626 | * to prevent caller from setting mapping/page error */ |
| 627 | err = 0; |
| 628 | } |
Sage Weil | 1d3576f | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 629 | unlock_page(page); |
Yehuda Sadeh | dbd646a | 2009-12-16 14:51:06 -0800 | [diff] [blame] | 630 | iput(inode); |
Sage Weil | 1d3576f | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 631 | return err; |
| 632 | } |
| 633 | |
Sage Weil | 1d3576f | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 634 | /* |
| 635 | * lame release_pages helper. release_pages() isn't exported to |
| 636 | * modules. |
| 637 | */ |
| 638 | static void ceph_release_pages(struct page **pages, int num) |
| 639 | { |
| 640 | struct pagevec pvec; |
| 641 | int i; |
| 642 | |
| 643 | pagevec_init(&pvec, 0); |
| 644 | for (i = 0; i < num; i++) { |
| 645 | if (pagevec_add(&pvec, pages[i]) == 0) |
| 646 | pagevec_release(&pvec); |
| 647 | } |
| 648 | pagevec_release(&pvec); |
| 649 | } |
| 650 | |
Sage Weil | 1d3576f | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 651 | /* |
| 652 | * async writeback completion handler. |
| 653 | * |
| 654 | * If we get an error, set the mapping error bit, but not the individual |
| 655 | * page error bits. |
| 656 | */ |
Ilya Dryomov | 85e084f | 2016-04-28 16:07:24 +0200 | [diff] [blame] | 657 | static void writepages_finish(struct ceph_osd_request *req) |
Sage Weil | 1d3576f | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 658 | { |
| 659 | struct inode *inode = req->r_inode; |
Sage Weil | 1d3576f | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 660 | struct ceph_inode_info *ci = ceph_inode(inode); |
Alex Elder | 87060c1 | 2013-04-03 01:28:58 -0500 | [diff] [blame] | 661 | struct ceph_osd_data *osd_data; |
Sage Weil | 1d3576f | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 662 | struct page *page; |
Yan, Zheng | 5b64640 | 2016-01-07 16:00:17 +0800 | [diff] [blame] | 663 | int num_pages, total_pages = 0; |
| 664 | int i, j; |
| 665 | int rc = req->r_result; |
Sage Weil | 1d3576f | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 666 | struct ceph_snap_context *snapc = req->r_snapc; |
| 667 | struct address_space *mapping = inode->i_mapping; |
Yehuda Sadeh | 3d14c5d | 2010-04-06 15:14:15 -0700 | [diff] [blame] | 668 | struct ceph_fs_client *fsc = ceph_inode_to_client(inode); |
Yan, Zheng | 5b64640 | 2016-01-07 16:00:17 +0800 | [diff] [blame] | 669 | bool remove_page; |
Sage Weil | 1d3576f | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 670 | |
Yan, Zheng | 5b64640 | 2016-01-07 16:00:17 +0800 | [diff] [blame] | 671 | dout("writepages_finish %p rc %d\n", inode, rc); |
| 672 | if (rc < 0) |
Sage Weil | 1d3576f | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 673 | mapping_set_error(mapping, rc); |
Yan, Zheng | 5b64640 | 2016-01-07 16:00:17 +0800 | [diff] [blame] | 674 | |
| 675 | /* |
| 676 | * We lost the cache cap, need to truncate the page before |
| 677 | * it is unlocked, otherwise we'd truncate it later in the |
| 678 | * page truncation thread, possibly losing some data that |
| 679 | * raced its way in |
| 680 | */ |
| 681 | remove_page = !(ceph_caps_issued(ci) & |
| 682 | (CEPH_CAP_FILE_CACHE|CEPH_CAP_FILE_LAZYIO)); |
Sage Weil | 1d3576f | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 683 | |
| 684 | /* clean all pages */ |
Yan, Zheng | 5b64640 | 2016-01-07 16:00:17 +0800 | [diff] [blame] | 685 | for (i = 0; i < req->r_num_ops; i++) { |
| 686 | if (req->r_ops[i].op != CEPH_OSD_OP_WRITE) |
| 687 | break; |
Sage Weil | 1d3576f | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 688 | |
Yan, Zheng | 5b64640 | 2016-01-07 16:00:17 +0800 | [diff] [blame] | 689 | osd_data = osd_req_op_extent_osd_data(req, i); |
| 690 | BUG_ON(osd_data->type != CEPH_OSD_DATA_TYPE_PAGES); |
| 691 | num_pages = calc_pages_for((u64)osd_data->alignment, |
| 692 | (u64)osd_data->length); |
| 693 | total_pages += num_pages; |
| 694 | for (j = 0; j < num_pages; j++) { |
| 695 | page = osd_data->pages[j]; |
| 696 | BUG_ON(!page); |
| 697 | WARN_ON(!PageUptodate(page)); |
Yehuda Sadeh | 2baba25 | 2009-12-18 13:51:57 -0800 | [diff] [blame] | 698 | |
Yan, Zheng | 5b64640 | 2016-01-07 16:00:17 +0800 | [diff] [blame] | 699 | if (atomic_long_dec_return(&fsc->writeback_count) < |
| 700 | CONGESTION_OFF_THRESH( |
| 701 | fsc->mount_options->congestion_kb)) |
| 702 | clear_bdi_congested(&fsc->backing_dev_info, |
| 703 | BLK_RW_ASYNC); |
Yehuda Sadeh | e63dc5c | 2010-02-19 00:07:01 +0000 | [diff] [blame] | 704 | |
Yan, Zheng | b109eec6 | 2016-05-13 17:54:17 +0800 | [diff] [blame] | 705 | if (rc < 0) |
| 706 | SetPageError(page); |
| 707 | |
Yan, Zheng | 5b64640 | 2016-01-07 16:00:17 +0800 | [diff] [blame] | 708 | ceph_put_snap_context(page_snap_context(page)); |
| 709 | page->private = 0; |
| 710 | ClearPagePrivate(page); |
| 711 | dout("unlocking %p\n", page); |
| 712 | end_page_writeback(page); |
Yehuda Sadeh | e63dc5c | 2010-02-19 00:07:01 +0000 | [diff] [blame] | 713 | |
Yan, Zheng | 5b64640 | 2016-01-07 16:00:17 +0800 | [diff] [blame] | 714 | if (remove_page) |
| 715 | generic_error_remove_page(inode->i_mapping, |
| 716 | page); |
| 717 | |
| 718 | unlock_page(page); |
| 719 | } |
| 720 | dout("writepages_finish %p wrote %llu bytes cleaned %d pages\n", |
| 721 | inode, osd_data->length, rc >= 0 ? num_pages : 0); |
| 722 | |
| 723 | ceph_release_pages(osd_data->pages, num_pages); |
Sage Weil | 1d3576f | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 724 | } |
Sage Weil | 1d3576f | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 725 | |
Yan, Zheng | 5b64640 | 2016-01-07 16:00:17 +0800 | [diff] [blame] | 726 | ceph_put_wrbuffer_cap_refs(ci, total_pages, snapc); |
| 727 | |
| 728 | osd_data = osd_req_op_extent_osd_data(req, 0); |
Alex Elder | 87060c1 | 2013-04-03 01:28:58 -0500 | [diff] [blame] | 729 | if (osd_data->pages_from_pool) |
| 730 | mempool_free(osd_data->pages, |
Cheng Renquan | 640ef79 | 2010-03-26 17:40:33 +0800 | [diff] [blame] | 731 | ceph_sb_to_client(inode->i_sb)->wb_pagevec_pool); |
Sage Weil | 1d3576f | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 732 | else |
Alex Elder | 87060c1 | 2013-04-03 01:28:58 -0500 | [diff] [blame] | 733 | kfree(osd_data->pages); |
Sage Weil | 1d3576f | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 734 | ceph_osdc_put_request(req); |
| 735 | } |
| 736 | |
Sage Weil | 1d3576f | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 737 | /* |
| 738 | * initiate async writeback |
| 739 | */ |
| 740 | static int ceph_writepages_start(struct address_space *mapping, |
| 741 | struct writeback_control *wbc) |
| 742 | { |
| 743 | struct inode *inode = mapping->host; |
Sage Weil | 1d3576f | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 744 | struct ceph_inode_info *ci = ceph_inode(inode); |
Yan, Zheng | fc2744a | 2013-05-31 16:48:29 +0800 | [diff] [blame] | 745 | struct ceph_fs_client *fsc = ceph_inode_to_client(inode); |
| 746 | struct ceph_vino vino = ceph_vino(inode); |
Sage Weil | 1d3576f | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 747 | pgoff_t index, start, end; |
| 748 | int range_whole = 0; |
| 749 | int should_loop = 1; |
| 750 | pgoff_t max_pages = 0, max_pages_ever = 0; |
Sage Weil | 80e755f | 2010-03-31 21:52:10 -0700 | [diff] [blame] | 751 | struct ceph_snap_context *snapc = NULL, *last_snapc = NULL, *pgsnapc; |
Sage Weil | 1d3576f | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 752 | struct pagevec pvec; |
| 753 | int done = 0; |
| 754 | int rc = 0; |
| 755 | unsigned wsize = 1 << inode->i_blkbits; |
| 756 | struct ceph_osd_request *req = NULL; |
Dan Carpenter | 021b77b | 2014-11-28 11:33:34 +0300 | [diff] [blame] | 757 | int do_sync = 0; |
Yan, Zheng | e1966b4 | 2015-06-18 03:10:58 +0800 | [diff] [blame] | 758 | loff_t snap_size, i_size; |
| 759 | u64 truncate_size; |
Yan, Zheng | fc2744a | 2013-05-31 16:48:29 +0800 | [diff] [blame] | 760 | u32 truncate_seq; |
Sage Weil | 1d3576f | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 761 | |
| 762 | /* |
| 763 | * Include a 'sync' in the OSD request if this is a data |
| 764 | * integrity write (e.g., O_SYNC write or fsync()), or if our |
| 765 | * cap is being revoked. |
| 766 | */ |
majianpeng | c62988e | 2013-06-19 15:12:06 +0800 | [diff] [blame] | 767 | if ((wbc->sync_mode == WB_SYNC_ALL) || |
| 768 | ceph_caps_revoking(ci, CEPH_CAP_FILE_BUFFER)) |
Sage Weil | 1d3576f | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 769 | do_sync = 1; |
| 770 | dout("writepages_start %p dosync=%d (mode=%s)\n", |
| 771 | inode, do_sync, |
| 772 | wbc->sync_mode == WB_SYNC_NONE ? "NONE" : |
| 773 | (wbc->sync_mode == WB_SYNC_ALL ? "ALL" : "HOLD")); |
| 774 | |
Seraphime Kirkovski | 52953d5 | 2016-12-26 10:26:34 +0100 | [diff] [blame] | 775 | if (READ_ONCE(fsc->mount_state) == CEPH_MOUNT_SHUTDOWN) { |
Yan, Zheng | 6c93df5 | 2016-04-15 13:56:12 +0800 | [diff] [blame] | 776 | if (ci->i_wrbuffer_ref > 0) { |
| 777 | pr_warn_ratelimited( |
| 778 | "writepage_start %p %lld forced umount\n", |
| 779 | inode, ceph_ino(inode)); |
| 780 | } |
Yan, Zheng | a341d4d | 2015-07-01 17:03:23 +0800 | [diff] [blame] | 781 | mapping_set_error(mapping, -EIO); |
Sage Weil | 1d3576f | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 782 | return -EIO; /* we're in a forced umount, don't write! */ |
| 783 | } |
Yehuda Sadeh | 3d14c5d | 2010-04-06 15:14:15 -0700 | [diff] [blame] | 784 | if (fsc->mount_options->wsize && fsc->mount_options->wsize < wsize) |
| 785 | wsize = fsc->mount_options->wsize; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 786 | if (wsize < PAGE_SIZE) |
| 787 | wsize = PAGE_SIZE; |
| 788 | max_pages_ever = wsize >> PAGE_SHIFT; |
Sage Weil | 1d3576f | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 789 | |
| 790 | pagevec_init(&pvec, 0); |
| 791 | |
Sage Weil | 1d3576f | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 792 | /* where to start/end? */ |
| 793 | if (wbc->range_cyclic) { |
| 794 | start = mapping->writeback_index; /* Start from prev offset */ |
| 795 | end = -1; |
| 796 | dout(" cyclic, start at %lu\n", start); |
| 797 | } else { |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 798 | start = wbc->range_start >> PAGE_SHIFT; |
| 799 | end = wbc->range_end >> PAGE_SHIFT; |
Sage Weil | 1d3576f | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 800 | if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX) |
| 801 | range_whole = 1; |
| 802 | should_loop = 0; |
| 803 | dout(" not cyclic, %lu to %lu\n", start, end); |
| 804 | } |
| 805 | index = start; |
| 806 | |
| 807 | retry: |
| 808 | /* find oldest snap context with dirty data */ |
| 809 | ceph_put_snap_context(snapc); |
Yan, Zheng | e1966b4 | 2015-06-18 03:10:58 +0800 | [diff] [blame] | 810 | snap_size = -1; |
Yan, Zheng | 5f743e4 | 2016-11-15 16:04:37 +0800 | [diff] [blame] | 811 | snapc = get_oldest_context(inode, &snap_size, |
| 812 | &truncate_size, &truncate_seq); |
Sage Weil | 1d3576f | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 813 | if (!snapc) { |
| 814 | /* hmm, why does writepages get called when there |
| 815 | is no dirty data? */ |
| 816 | dout(" no snap context with dirty data?\n"); |
| 817 | goto out; |
| 818 | } |
| 819 | dout(" oldest snapc is %p seq %lld (%d snaps)\n", |
| 820 | snapc, snapc->seq, snapc->num_snaps); |
Yan, Zheng | fc2744a | 2013-05-31 16:48:29 +0800 | [diff] [blame] | 821 | |
Yan, Zheng | e1966b4 | 2015-06-18 03:10:58 +0800 | [diff] [blame] | 822 | i_size = i_size_read(inode); |
Yan, Zheng | fc2744a | 2013-05-31 16:48:29 +0800 | [diff] [blame] | 823 | |
Sage Weil | 1d3576f | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 824 | if (last_snapc && snapc != last_snapc) { |
| 825 | /* if we switched to a newer snapc, restart our scan at the |
| 826 | * start of the original file range. */ |
| 827 | dout(" snapc differs from last pass, restarting at %lu\n", |
| 828 | index); |
| 829 | index = start; |
| 830 | } |
| 831 | last_snapc = snapc; |
| 832 | |
| 833 | while (!done && index <= end) { |
| 834 | unsigned i; |
| 835 | int first; |
Yan, Zheng | 5b64640 | 2016-01-07 16:00:17 +0800 | [diff] [blame] | 836 | pgoff_t strip_unit_end = 0; |
| 837 | int num_ops = 0, op_idx; |
| 838 | int pvec_pages, locked_pages = 0; |
| 839 | struct page **pages = NULL, **data_pages; |
Alex Elder | e5975c7 | 2013-03-14 14:09:05 -0500 | [diff] [blame] | 840 | mempool_t *pool = NULL; /* Becomes non-null if mempool used */ |
Sage Weil | 1d3576f | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 841 | struct page *page; |
| 842 | int want; |
Yan, Zheng | 5b64640 | 2016-01-07 16:00:17 +0800 | [diff] [blame] | 843 | u64 offset = 0, len = 0; |
Sage Weil | 1d3576f | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 844 | |
Sage Weil | 1d3576f | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 845 | max_pages = max_pages_ever; |
| 846 | |
| 847 | get_more_pages: |
| 848 | first = -1; |
| 849 | want = min(end - index, |
| 850 | min((pgoff_t)PAGEVEC_SIZE, |
| 851 | max_pages - (pgoff_t)locked_pages) - 1) |
| 852 | + 1; |
| 853 | pvec_pages = pagevec_lookup_tag(&pvec, mapping, &index, |
| 854 | PAGECACHE_TAG_DIRTY, |
| 855 | want); |
| 856 | dout("pagevec_lookup_tag got %d\n", pvec_pages); |
| 857 | if (!pvec_pages && !locked_pages) |
| 858 | break; |
| 859 | for (i = 0; i < pvec_pages && locked_pages < max_pages; i++) { |
| 860 | page = pvec.pages[i]; |
| 861 | dout("? %p idx %lu\n", page, page->index); |
| 862 | if (locked_pages == 0) |
| 863 | lock_page(page); /* first page */ |
| 864 | else if (!trylock_page(page)) |
| 865 | break; |
| 866 | |
| 867 | /* only dirty pages, or our accounting breaks */ |
| 868 | if (unlikely(!PageDirty(page)) || |
| 869 | unlikely(page->mapping != mapping)) { |
| 870 | dout("!dirty or !mapping %p\n", page); |
| 871 | unlock_page(page); |
| 872 | break; |
| 873 | } |
| 874 | if (!wbc->range_cyclic && page->index > end) { |
| 875 | dout("end of range %p\n", page); |
| 876 | done = 1; |
| 877 | unlock_page(page); |
| 878 | break; |
| 879 | } |
Yan, Zheng | 5b64640 | 2016-01-07 16:00:17 +0800 | [diff] [blame] | 880 | if (strip_unit_end && (page->index > strip_unit_end)) { |
| 881 | dout("end of strip unit %p\n", page); |
Sage Weil | 1d3576f | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 882 | unlock_page(page); |
| 883 | break; |
| 884 | } |
| 885 | if (wbc->sync_mode != WB_SYNC_NONE) { |
| 886 | dout("waiting on writeback %p\n", page); |
| 887 | wait_on_page_writeback(page); |
| 888 | } |
Yan, Zheng | e1966b4 | 2015-06-18 03:10:58 +0800 | [diff] [blame] | 889 | if (page_offset(page) >= |
| 890 | (snap_size == -1 ? i_size : snap_size)) { |
| 891 | dout("%p page eof %llu\n", page, |
| 892 | (snap_size == -1 ? i_size : snap_size)); |
Sage Weil | 1d3576f | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 893 | done = 1; |
| 894 | unlock_page(page); |
| 895 | break; |
| 896 | } |
| 897 | if (PageWriteback(page)) { |
| 898 | dout("%p under writeback\n", page); |
| 899 | unlock_page(page); |
| 900 | break; |
| 901 | } |
| 902 | |
| 903 | /* only if matching snap context */ |
Yan, Zheng | 61600ef | 2012-05-28 14:44:30 +0800 | [diff] [blame] | 904 | pgsnapc = page_snap_context(page); |
Sage Weil | 80e755f | 2010-03-31 21:52:10 -0700 | [diff] [blame] | 905 | if (pgsnapc->seq > snapc->seq) { |
| 906 | dout("page snapc %p %lld > oldest %p %lld\n", |
| 907 | pgsnapc, pgsnapc->seq, snapc, snapc->seq); |
Sage Weil | 1d3576f | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 908 | unlock_page(page); |
| 909 | if (!locked_pages) |
| 910 | continue; /* keep looking for snap */ |
| 911 | break; |
| 912 | } |
| 913 | |
| 914 | if (!clear_page_dirty_for_io(page)) { |
| 915 | dout("%p !clear_page_dirty_for_io\n", page); |
| 916 | unlock_page(page); |
| 917 | break; |
| 918 | } |
| 919 | |
Alex Elder | e5975c7 | 2013-03-14 14:09:05 -0500 | [diff] [blame] | 920 | /* |
| 921 | * We have something to write. If this is |
| 922 | * the first locked page this time through, |
Yan, Zheng | 5b64640 | 2016-01-07 16:00:17 +0800 | [diff] [blame] | 923 | * calculate max possinle write size and |
| 924 | * allocate a page array |
Alex Elder | e5975c7 | 2013-03-14 14:09:05 -0500 | [diff] [blame] | 925 | */ |
Sage Weil | 1d3576f | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 926 | if (locked_pages == 0) { |
Yan, Zheng | 5b64640 | 2016-01-07 16:00:17 +0800 | [diff] [blame] | 927 | u64 objnum; |
| 928 | u64 objoff; |
| 929 | |
Sage Weil | 1d3576f | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 930 | /* prepare async write request */ |
Alex Elder | e5975c7 | 2013-03-14 14:09:05 -0500 | [diff] [blame] | 931 | offset = (u64)page_offset(page); |
Sage Weil | 1d3576f | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 932 | len = wsize; |
Yan, Zheng | 5b64640 | 2016-01-07 16:00:17 +0800 | [diff] [blame] | 933 | |
| 934 | rc = ceph_calc_file_object_mapping(&ci->i_layout, |
| 935 | offset, len, |
| 936 | &objnum, &objoff, |
| 937 | &len); |
| 938 | if (rc < 0) { |
Henry C Chang | 8c71897 | 2011-05-03 09:45:16 +0000 | [diff] [blame] | 939 | unlock_page(page); |
| 940 | break; |
| 941 | } |
| 942 | |
Yan, Zheng | 5b64640 | 2016-01-07 16:00:17 +0800 | [diff] [blame] | 943 | num_ops = 1 + do_sync; |
| 944 | strip_unit_end = page->index + |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 945 | ((len - 1) >> PAGE_SHIFT); |
Yan, Zheng | 715e4cd | 2014-11-13 14:40:37 +0800 | [diff] [blame] | 946 | |
Yan, Zheng | 5b64640 | 2016-01-07 16:00:17 +0800 | [diff] [blame] | 947 | BUG_ON(pages); |
Alex Elder | 8848695 | 2013-03-14 14:09:05 -0500 | [diff] [blame] | 948 | max_pages = calc_pages_for(0, (u64)len); |
Yan, Zheng | fc2744a | 2013-05-31 16:48:29 +0800 | [diff] [blame] | 949 | pages = kmalloc(max_pages * sizeof (*pages), |
| 950 | GFP_NOFS); |
Alex Elder | 8848695 | 2013-03-14 14:09:05 -0500 | [diff] [blame] | 951 | if (!pages) { |
| 952 | pool = fsc->wb_pagevec_pool; |
Alex Elder | 8848695 | 2013-03-14 14:09:05 -0500 | [diff] [blame] | 953 | pages = mempool_alloc(pool, GFP_NOFS); |
Alex Elder | e5975c7 | 2013-03-14 14:09:05 -0500 | [diff] [blame] | 954 | BUG_ON(!pages); |
Alex Elder | 8848695 | 2013-03-14 14:09:05 -0500 | [diff] [blame] | 955 | } |
Yan, Zheng | 5b64640 | 2016-01-07 16:00:17 +0800 | [diff] [blame] | 956 | |
| 957 | len = 0; |
| 958 | } else if (page->index != |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 959 | (offset + len) >> PAGE_SHIFT) { |
Yan, Zheng | 5b64640 | 2016-01-07 16:00:17 +0800 | [diff] [blame] | 960 | if (num_ops >= (pool ? CEPH_OSD_SLAB_OPS : |
| 961 | CEPH_OSD_MAX_OPS)) { |
| 962 | redirty_page_for_writepage(wbc, page); |
| 963 | unlock_page(page); |
| 964 | break; |
| 965 | } |
| 966 | |
| 967 | num_ops++; |
| 968 | offset = (u64)page_offset(page); |
| 969 | len = 0; |
Sage Weil | 1d3576f | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 970 | } |
| 971 | |
| 972 | /* note position of first page in pvec */ |
| 973 | if (first < 0) |
| 974 | first = i; |
| 975 | dout("%p will write page %p idx %lu\n", |
| 976 | inode, page, page->index); |
Yehuda Sadeh | 2baba25 | 2009-12-18 13:51:57 -0800 | [diff] [blame] | 977 | |
Yan, Zheng | 5b64640 | 2016-01-07 16:00:17 +0800 | [diff] [blame] | 978 | if (atomic_long_inc_return(&fsc->writeback_count) > |
| 979 | CONGESTION_ON_THRESH( |
Yehuda Sadeh | 3d14c5d | 2010-04-06 15:14:15 -0700 | [diff] [blame] | 980 | fsc->mount_options->congestion_kb)) { |
| 981 | set_bdi_congested(&fsc->backing_dev_info, |
Sage Weil | 213c99e | 2010-08-03 10:25:11 -0700 | [diff] [blame] | 982 | BLK_RW_ASYNC); |
Yehuda Sadeh | 2baba25 | 2009-12-18 13:51:57 -0800 | [diff] [blame] | 983 | } |
| 984 | |
Alex Elder | e5975c7 | 2013-03-14 14:09:05 -0500 | [diff] [blame] | 985 | pages[locked_pages] = page; |
Sage Weil | 1d3576f | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 986 | locked_pages++; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 987 | len += PAGE_SIZE; |
Sage Weil | 1d3576f | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 988 | } |
| 989 | |
| 990 | /* did we get anything? */ |
| 991 | if (!locked_pages) |
| 992 | goto release_pvec_pages; |
| 993 | if (i) { |
| 994 | int j; |
| 995 | BUG_ON(!locked_pages || first < 0); |
| 996 | |
| 997 | if (pvec_pages && i == pvec_pages && |
| 998 | locked_pages < max_pages) { |
| 999 | dout("reached end pvec, trying for more\n"); |
| 1000 | pagevec_reinit(&pvec); |
| 1001 | goto get_more_pages; |
| 1002 | } |
| 1003 | |
| 1004 | /* shift unused pages over in the pvec... we |
| 1005 | * will need to release them below. */ |
| 1006 | for (j = i; j < pvec_pages; j++) { |
Yan, Zheng | 5b64640 | 2016-01-07 16:00:17 +0800 | [diff] [blame] | 1007 | dout(" pvec leftover page %p\n", pvec.pages[j]); |
Sage Weil | 1d3576f | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1008 | pvec.pages[j-i+first] = pvec.pages[j]; |
| 1009 | } |
| 1010 | pvec.nr -= i-first; |
| 1011 | } |
| 1012 | |
Yan, Zheng | 5b64640 | 2016-01-07 16:00:17 +0800 | [diff] [blame] | 1013 | new_request: |
Alex Elder | e5975c7 | 2013-03-14 14:09:05 -0500 | [diff] [blame] | 1014 | offset = page_offset(pages[0]); |
Yan, Zheng | 5b64640 | 2016-01-07 16:00:17 +0800 | [diff] [blame] | 1015 | len = wsize; |
| 1016 | |
| 1017 | req = ceph_osdc_new_request(&fsc->client->osdc, |
| 1018 | &ci->i_layout, vino, |
| 1019 | offset, &len, 0, num_ops, |
| 1020 | CEPH_OSD_OP_WRITE, |
Ilya Dryomov | 54ea004 | 2017-02-11 18:48:41 +0100 | [diff] [blame^] | 1021 | CEPH_OSD_FLAG_WRITE, |
Yan, Zheng | 5b64640 | 2016-01-07 16:00:17 +0800 | [diff] [blame] | 1022 | snapc, truncate_seq, |
| 1023 | truncate_size, false); |
| 1024 | if (IS_ERR(req)) { |
| 1025 | req = ceph_osdc_new_request(&fsc->client->osdc, |
| 1026 | &ci->i_layout, vino, |
| 1027 | offset, &len, 0, |
| 1028 | min(num_ops, |
| 1029 | CEPH_OSD_SLAB_OPS), |
| 1030 | CEPH_OSD_OP_WRITE, |
Ilya Dryomov | 54ea004 | 2017-02-11 18:48:41 +0100 | [diff] [blame^] | 1031 | CEPH_OSD_FLAG_WRITE, |
Yan, Zheng | 5b64640 | 2016-01-07 16:00:17 +0800 | [diff] [blame] | 1032 | snapc, truncate_seq, |
| 1033 | truncate_size, true); |
| 1034 | BUG_ON(IS_ERR(req)); |
Yan, Zheng | e1966b4 | 2015-06-18 03:10:58 +0800 | [diff] [blame] | 1035 | } |
Yan, Zheng | 5b64640 | 2016-01-07 16:00:17 +0800 | [diff] [blame] | 1036 | BUG_ON(len < page_offset(pages[locked_pages - 1]) + |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1037 | PAGE_SIZE - offset); |
Sage Weil | 1d3576f | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1038 | |
Yan, Zheng | 5b64640 | 2016-01-07 16:00:17 +0800 | [diff] [blame] | 1039 | req->r_callback = writepages_finish; |
| 1040 | req->r_inode = inode; |
| 1041 | |
| 1042 | /* Format the osd request message and submit the write */ |
| 1043 | len = 0; |
| 1044 | data_pages = pages; |
| 1045 | op_idx = 0; |
| 1046 | for (i = 0; i < locked_pages; i++) { |
| 1047 | u64 cur_offset = page_offset(pages[i]); |
| 1048 | if (offset + len != cur_offset) { |
| 1049 | if (op_idx + do_sync + 1 == req->r_num_ops) |
| 1050 | break; |
| 1051 | osd_req_op_extent_dup_last(req, op_idx, |
| 1052 | cur_offset - offset); |
| 1053 | dout("writepages got pages at %llu~%llu\n", |
| 1054 | offset, len); |
| 1055 | osd_req_op_extent_osd_data_pages(req, op_idx, |
| 1056 | data_pages, len, 0, |
Alex Elder | a4ce40a | 2013-04-05 01:27:12 -0500 | [diff] [blame] | 1057 | !!pool, false); |
Yan, Zheng | 5b64640 | 2016-01-07 16:00:17 +0800 | [diff] [blame] | 1058 | osd_req_op_extent_update(req, op_idx, len); |
Alex Elder | e5975c7 | 2013-03-14 14:09:05 -0500 | [diff] [blame] | 1059 | |
Yan, Zheng | 5b64640 | 2016-01-07 16:00:17 +0800 | [diff] [blame] | 1060 | len = 0; |
| 1061 | offset = cur_offset; |
| 1062 | data_pages = pages + i; |
| 1063 | op_idx++; |
| 1064 | } |
| 1065 | |
| 1066 | set_page_writeback(pages[i]); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1067 | len += PAGE_SIZE; |
Yan, Zheng | 5b64640 | 2016-01-07 16:00:17 +0800 | [diff] [blame] | 1068 | } |
| 1069 | |
| 1070 | if (snap_size != -1) { |
| 1071 | len = min(len, snap_size - offset); |
| 1072 | } else if (i == locked_pages) { |
| 1073 | /* writepages_finish() clears writeback pages |
| 1074 | * according to the data length, so make sure |
| 1075 | * data length covers all locked pages */ |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1076 | u64 min_len = len + 1 - PAGE_SIZE; |
Yan, Zheng | 5b64640 | 2016-01-07 16:00:17 +0800 | [diff] [blame] | 1077 | len = min(len, (u64)i_size_read(inode) - offset); |
| 1078 | len = max(len, min_len); |
| 1079 | } |
| 1080 | dout("writepages got pages at %llu~%llu\n", offset, len); |
| 1081 | |
| 1082 | osd_req_op_extent_osd_data_pages(req, op_idx, data_pages, len, |
| 1083 | 0, !!pool, false); |
| 1084 | osd_req_op_extent_update(req, op_idx, len); |
| 1085 | |
| 1086 | if (do_sync) { |
| 1087 | op_idx++; |
| 1088 | osd_req_op_init(req, op_idx, CEPH_OSD_OP_STARTSYNC, 0); |
| 1089 | } |
| 1090 | BUG_ON(op_idx + 1 != req->r_num_ops); |
| 1091 | |
Alex Elder | e5975c7 | 2013-03-14 14:09:05 -0500 | [diff] [blame] | 1092 | pool = NULL; |
Yan, Zheng | 5b64640 | 2016-01-07 16:00:17 +0800 | [diff] [blame] | 1093 | if (i < locked_pages) { |
| 1094 | BUG_ON(num_ops <= req->r_num_ops); |
| 1095 | num_ops -= req->r_num_ops; |
| 1096 | num_ops += do_sync; |
| 1097 | locked_pages -= i; |
Alex Elder | e5975c7 | 2013-03-14 14:09:05 -0500 | [diff] [blame] | 1098 | |
Yan, Zheng | 5b64640 | 2016-01-07 16:00:17 +0800 | [diff] [blame] | 1099 | /* allocate new pages array for next request */ |
| 1100 | data_pages = pages; |
| 1101 | pages = kmalloc(locked_pages * sizeof (*pages), |
| 1102 | GFP_NOFS); |
| 1103 | if (!pages) { |
| 1104 | pool = fsc->wb_pagevec_pool; |
| 1105 | pages = mempool_alloc(pool, GFP_NOFS); |
| 1106 | BUG_ON(!pages); |
| 1107 | } |
| 1108 | memcpy(pages, data_pages + i, |
| 1109 | locked_pages * sizeof(*pages)); |
| 1110 | memset(data_pages + i, 0, |
| 1111 | locked_pages * sizeof(*pages)); |
| 1112 | } else { |
| 1113 | BUG_ON(num_ops != req->r_num_ops); |
| 1114 | index = pages[i - 1]->index + 1; |
| 1115 | /* request message now owns the pages array */ |
| 1116 | pages = NULL; |
| 1117 | } |
Alex Elder | e5975c7 | 2013-03-14 14:09:05 -0500 | [diff] [blame] | 1118 | |
Ilya Dryomov | bb873b539 | 2016-05-26 00:29:52 +0200 | [diff] [blame] | 1119 | req->r_mtime = inode->i_mtime; |
Sage Weil | 9d6fcb0 | 2011-05-12 15:48:16 -0700 | [diff] [blame] | 1120 | rc = ceph_osdc_start_request(&fsc->client->osdc, req, true); |
| 1121 | BUG_ON(rc); |
Sage Weil | 1d3576f | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1122 | req = NULL; |
| 1123 | |
Yan, Zheng | 5b64640 | 2016-01-07 16:00:17 +0800 | [diff] [blame] | 1124 | wbc->nr_to_write -= i; |
| 1125 | if (pages) |
| 1126 | goto new_request; |
| 1127 | |
Sage Weil | 1d3576f | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1128 | if (wbc->nr_to_write <= 0) |
| 1129 | done = 1; |
| 1130 | |
| 1131 | release_pvec_pages: |
| 1132 | dout("pagevec_release on %d pages (%p)\n", (int)pvec.nr, |
| 1133 | pvec.nr ? pvec.pages[0] : NULL); |
| 1134 | pagevec_release(&pvec); |
| 1135 | |
| 1136 | if (locked_pages && !done) |
| 1137 | goto retry; |
| 1138 | } |
| 1139 | |
| 1140 | if (should_loop && !done) { |
| 1141 | /* more to do; loop back to beginning of file */ |
| 1142 | dout("writepages looping back to beginning of file\n"); |
| 1143 | should_loop = 0; |
| 1144 | index = 0; |
| 1145 | goto retry; |
| 1146 | } |
| 1147 | |
| 1148 | if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0)) |
| 1149 | mapping->writeback_index = index; |
| 1150 | |
| 1151 | out: |
Ilya Dryomov | 3ed97d6 | 2016-04-26 15:05:29 +0200 | [diff] [blame] | 1152 | ceph_osdc_put_request(req); |
Sage Weil | 1d3576f | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1153 | ceph_put_snap_context(snapc); |
| 1154 | dout("writepages done, rc = %d\n", rc); |
Sage Weil | 1d3576f | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1155 | return rc; |
| 1156 | } |
| 1157 | |
| 1158 | |
| 1159 | |
| 1160 | /* |
| 1161 | * See if a given @snapc is either writeable, or already written. |
| 1162 | */ |
| 1163 | static int context_is_writeable_or_written(struct inode *inode, |
| 1164 | struct ceph_snap_context *snapc) |
| 1165 | { |
Yan, Zheng | 5f743e4 | 2016-11-15 16:04:37 +0800 | [diff] [blame] | 1166 | struct ceph_snap_context *oldest = get_oldest_context(inode, NULL, |
| 1167 | NULL, NULL); |
Sage Weil | 6298a33 | 2010-03-31 22:01:38 -0700 | [diff] [blame] | 1168 | int ret = !oldest || snapc->seq <= oldest->seq; |
| 1169 | |
| 1170 | ceph_put_snap_context(oldest); |
| 1171 | return ret; |
Sage Weil | 1d3576f | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1172 | } |
| 1173 | |
| 1174 | /* |
| 1175 | * We are only allowed to write into/dirty the page if the page is |
| 1176 | * clean, or already dirty within the same snap context. |
Sage Weil | 8f883c2 | 2010-03-19 13:27:53 -0700 | [diff] [blame] | 1177 | * |
| 1178 | * called with page locked. |
| 1179 | * return success with page locked, |
| 1180 | * or any failure (incl -EAGAIN) with page unlocked. |
Sage Weil | 1d3576f | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1181 | */ |
Yehuda Sadeh | 4af6b22 | 2010-02-09 11:02:51 -0800 | [diff] [blame] | 1182 | static int ceph_update_writeable_page(struct file *file, |
| 1183 | loff_t pos, unsigned len, |
| 1184 | struct page *page) |
Sage Weil | 1d3576f | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1185 | { |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 1186 | struct inode *inode = file_inode(file); |
Yan, Zheng | 6c93df5 | 2016-04-15 13:56:12 +0800 | [diff] [blame] | 1187 | struct ceph_fs_client *fsc = ceph_inode_to_client(inode); |
Sage Weil | 1d3576f | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1188 | struct ceph_inode_info *ci = ceph_inode(inode); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1189 | loff_t page_off = pos & PAGE_MASK; |
| 1190 | int pos_in_page = pos & ~PAGE_MASK; |
Sage Weil | 1d3576f | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1191 | int end_in_page = pos_in_page + len; |
| 1192 | loff_t i_size; |
Sage Weil | 1d3576f | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1193 | int r; |
Sage Weil | 80e755f | 2010-03-31 21:52:10 -0700 | [diff] [blame] | 1194 | struct ceph_snap_context *snapc, *oldest; |
Sage Weil | 1d3576f | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1195 | |
Seraphime Kirkovski | 52953d5 | 2016-12-26 10:26:34 +0100 | [diff] [blame] | 1196 | if (READ_ONCE(fsc->mount_state) == CEPH_MOUNT_SHUTDOWN) { |
Yan, Zheng | 6c93df5 | 2016-04-15 13:56:12 +0800 | [diff] [blame] | 1197 | dout(" page %p forced umount\n", page); |
| 1198 | unlock_page(page); |
| 1199 | return -EIO; |
| 1200 | } |
| 1201 | |
Sage Weil | 1d3576f | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1202 | retry_locked: |
| 1203 | /* writepages currently holds page lock, but if we change that later, */ |
| 1204 | wait_on_page_writeback(page); |
| 1205 | |
Yan, Zheng | 61600ef | 2012-05-28 14:44:30 +0800 | [diff] [blame] | 1206 | snapc = page_snap_context(page); |
Sage Weil | 80e755f | 2010-03-31 21:52:10 -0700 | [diff] [blame] | 1207 | if (snapc && snapc != ci->i_head_snapc) { |
Sage Weil | 1d3576f | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1208 | /* |
| 1209 | * this page is already dirty in another (older) snap |
| 1210 | * context! is it writeable now? |
| 1211 | */ |
Yan, Zheng | 5f743e4 | 2016-11-15 16:04:37 +0800 | [diff] [blame] | 1212 | oldest = get_oldest_context(inode, NULL, NULL, NULL); |
Sage Weil | 1d3576f | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1213 | |
Sage Weil | 80e755f | 2010-03-31 21:52:10 -0700 | [diff] [blame] | 1214 | if (snapc->seq > oldest->seq) { |
Sage Weil | 6298a33 | 2010-03-31 22:01:38 -0700 | [diff] [blame] | 1215 | ceph_put_snap_context(oldest); |
Sage Weil | 1d3576f | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1216 | dout(" page %p snapc %p not current or oldest\n", |
Sage Weil | 6298a33 | 2010-03-31 22:01:38 -0700 | [diff] [blame] | 1217 | page, snapc); |
Sage Weil | 1d3576f | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1218 | /* |
| 1219 | * queue for writeback, and wait for snapc to |
| 1220 | * be writeable or written |
| 1221 | */ |
Sage Weil | 6298a33 | 2010-03-31 22:01:38 -0700 | [diff] [blame] | 1222 | snapc = ceph_get_snap_context(snapc); |
Sage Weil | 1d3576f | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1223 | unlock_page(page); |
Sage Weil | 3c6f6b7 | 2010-02-09 15:24:44 -0800 | [diff] [blame] | 1224 | ceph_queue_writeback(inode); |
Yan, Zheng | a78bbd4 | 2016-05-13 11:30:24 +0800 | [diff] [blame] | 1225 | r = wait_event_killable(ci->i_cap_wq, |
Sage Weil | 1d3576f | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1226 | context_is_writeable_or_written(inode, snapc)); |
| 1227 | ceph_put_snap_context(snapc); |
Sage Weil | 8f883c2 | 2010-03-19 13:27:53 -0700 | [diff] [blame] | 1228 | if (r == -ERESTARTSYS) |
| 1229 | return r; |
Yehuda Sadeh | 4af6b22 | 2010-02-09 11:02:51 -0800 | [diff] [blame] | 1230 | return -EAGAIN; |
Sage Weil | 1d3576f | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1231 | } |
Sage Weil | 6298a33 | 2010-03-31 22:01:38 -0700 | [diff] [blame] | 1232 | ceph_put_snap_context(oldest); |
Sage Weil | 1d3576f | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1233 | |
| 1234 | /* yay, writeable, do it now (without dropping page lock) */ |
| 1235 | dout(" page %p snapc %p not current, but oldest\n", |
| 1236 | page, snapc); |
| 1237 | if (!clear_page_dirty_for_io(page)) |
| 1238 | goto retry_locked; |
| 1239 | r = writepage_nounlock(page, NULL); |
| 1240 | if (r < 0) |
| 1241 | goto fail_nosnap; |
| 1242 | goto retry_locked; |
| 1243 | } |
| 1244 | |
| 1245 | if (PageUptodate(page)) { |
| 1246 | dout(" page %p already uptodate\n", page); |
| 1247 | return 0; |
| 1248 | } |
| 1249 | |
| 1250 | /* full page? */ |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1251 | if (pos_in_page == 0 && len == PAGE_SIZE) |
Sage Weil | 1d3576f | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1252 | return 0; |
| 1253 | |
| 1254 | /* past end of file? */ |
Yan, Zheng | 99c88e6 | 2015-12-30 11:32:46 +0800 | [diff] [blame] | 1255 | i_size = i_size_read(inode); |
Sage Weil | 1d3576f | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1256 | |
Sage Weil | 1d3576f | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1257 | if (page_off >= i_size || |
| 1258 | (pos_in_page == 0 && (pos+len) >= i_size && |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1259 | end_in_page - pos_in_page != PAGE_SIZE)) { |
Sage Weil | 1d3576f | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1260 | dout(" zeroing %p 0 - %d and %d - %d\n", |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1261 | page, pos_in_page, end_in_page, (int)PAGE_SIZE); |
Sage Weil | 1d3576f | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1262 | zero_user_segments(page, |
| 1263 | 0, pos_in_page, |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1264 | end_in_page, PAGE_SIZE); |
Sage Weil | 1d3576f | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1265 | return 0; |
| 1266 | } |
| 1267 | |
| 1268 | /* we need to read it. */ |
Sage Weil | 1d3576f | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1269 | r = readpage_nounlock(file, page); |
| 1270 | if (r < 0) |
| 1271 | goto fail_nosnap; |
| 1272 | goto retry_locked; |
Sage Weil | 1d3576f | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1273 | fail_nosnap: |
| 1274 | unlock_page(page); |
| 1275 | return r; |
| 1276 | } |
| 1277 | |
| 1278 | /* |
Yehuda Sadeh | 4af6b22 | 2010-02-09 11:02:51 -0800 | [diff] [blame] | 1279 | * We are only allowed to write into/dirty the page if the page is |
| 1280 | * clean, or already dirty within the same snap context. |
| 1281 | */ |
| 1282 | static int ceph_write_begin(struct file *file, struct address_space *mapping, |
| 1283 | loff_t pos, unsigned len, unsigned flags, |
| 1284 | struct page **pagep, void **fsdata) |
| 1285 | { |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 1286 | struct inode *inode = file_inode(file); |
Yehuda Sadeh | 4af6b22 | 2010-02-09 11:02:51 -0800 | [diff] [blame] | 1287 | struct page *page; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1288 | pgoff_t index = pos >> PAGE_SHIFT; |
Sage Weil | 7971bd9 | 2013-05-01 21:15:58 -0700 | [diff] [blame] | 1289 | int r; |
Yehuda Sadeh | 4af6b22 | 2010-02-09 11:02:51 -0800 | [diff] [blame] | 1290 | |
| 1291 | do { |
Sage Weil | 8f883c2 | 2010-03-19 13:27:53 -0700 | [diff] [blame] | 1292 | /* get a page */ |
Yehuda Sadeh | 4af6b22 | 2010-02-09 11:02:51 -0800 | [diff] [blame] | 1293 | page = grab_cache_page_write_begin(mapping, index, 0); |
Sage Weil | 7971bd9 | 2013-05-01 21:15:58 -0700 | [diff] [blame] | 1294 | if (!page) |
| 1295 | return -ENOMEM; |
Yehuda Sadeh | 4af6b22 | 2010-02-09 11:02:51 -0800 | [diff] [blame] | 1296 | |
| 1297 | 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] | 1298 | inode, page, (int)pos, (int)len); |
Yehuda Sadeh | 4af6b22 | 2010-02-09 11:02:51 -0800 | [diff] [blame] | 1299 | |
| 1300 | r = ceph_update_writeable_page(file, pos, len, page); |
Taesoo Kim | c1d00b2 | 2015-03-20 17:36:56 -0400 | [diff] [blame] | 1301 | if (r < 0) |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1302 | put_page(page); |
Taesoo Kim | c1d00b2 | 2015-03-20 17:36:56 -0400 | [diff] [blame] | 1303 | else |
| 1304 | *pagep = page; |
Yehuda Sadeh | 4af6b22 | 2010-02-09 11:02:51 -0800 | [diff] [blame] | 1305 | } while (r == -EAGAIN); |
| 1306 | |
| 1307 | return r; |
| 1308 | } |
| 1309 | |
| 1310 | /* |
Sage Weil | 1d3576f | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1311 | * 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] | 1312 | * except adjust dirty page accounting |
Sage Weil | 1d3576f | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1313 | */ |
| 1314 | static int ceph_write_end(struct file *file, struct address_space *mapping, |
| 1315 | loff_t pos, unsigned len, unsigned copied, |
| 1316 | struct page *page, void *fsdata) |
| 1317 | { |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 1318 | struct inode *inode = file_inode(file); |
Sage Weil | 1d3576f | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1319 | int check_cap = 0; |
| 1320 | |
| 1321 | dout("write_end file %p inode %p page %p %d~%d (%d)\n", file, |
| 1322 | inode, page, (int)pos, (int)copied, (int)len); |
| 1323 | |
| 1324 | /* zero the stale part of the page if we did a short copy */ |
Al Viro | b9de313 | 2016-09-05 22:20:03 -0400 | [diff] [blame] | 1325 | if (!PageUptodate(page)) { |
| 1326 | if (copied < len) { |
| 1327 | copied = 0; |
| 1328 | goto out; |
| 1329 | } |
| 1330 | SetPageUptodate(page); |
| 1331 | } |
Sage Weil | 1d3576f | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1332 | |
| 1333 | /* did file size increase? */ |
Yan, Zheng | 99c88e6 | 2015-12-30 11:32:46 +0800 | [diff] [blame] | 1334 | if (pos+copied > i_size_read(inode)) |
Sage Weil | 1d3576f | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1335 | check_cap = ceph_inode_set_size(inode, pos+copied); |
| 1336 | |
Sage Weil | 1d3576f | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1337 | set_page_dirty(page); |
| 1338 | |
Al Viro | b9de313 | 2016-09-05 22:20:03 -0400 | [diff] [blame] | 1339 | out: |
Sage Weil | 1d3576f | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1340 | unlock_page(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1341 | put_page(page); |
Sage Weil | 1d3576f | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1342 | |
| 1343 | if (check_cap) |
| 1344 | ceph_check_caps(ceph_inode(inode), CHECK_CAPS_AUTHONLY, NULL); |
| 1345 | |
| 1346 | return copied; |
| 1347 | } |
| 1348 | |
| 1349 | /* |
| 1350 | * we set .direct_IO to indicate direct io is supported, but since we |
| 1351 | * intercept O_DIRECT reads and writes early, this function should |
| 1352 | * never get called. |
| 1353 | */ |
Christoph Hellwig | c8b8e32 | 2016-04-07 08:51:58 -0700 | [diff] [blame] | 1354 | 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] | 1355 | { |
| 1356 | WARN_ON(1); |
| 1357 | return -EINVAL; |
| 1358 | } |
| 1359 | |
| 1360 | const struct address_space_operations ceph_aops = { |
| 1361 | .readpage = ceph_readpage, |
| 1362 | .readpages = ceph_readpages, |
| 1363 | .writepage = ceph_writepage, |
| 1364 | .writepages = ceph_writepages_start, |
| 1365 | .write_begin = ceph_write_begin, |
| 1366 | .write_end = ceph_write_end, |
| 1367 | .set_page_dirty = ceph_set_page_dirty, |
| 1368 | .invalidatepage = ceph_invalidatepage, |
| 1369 | .releasepage = ceph_releasepage, |
| 1370 | .direct_IO = ceph_direct_io, |
| 1371 | }; |
| 1372 | |
Yan, Zheng | 4f7e89f | 2016-05-10 18:40:28 +0800 | [diff] [blame] | 1373 | static void ceph_block_sigs(sigset_t *oldset) |
| 1374 | { |
| 1375 | sigset_t mask; |
| 1376 | siginitsetinv(&mask, sigmask(SIGKILL)); |
| 1377 | sigprocmask(SIG_BLOCK, &mask, oldset); |
| 1378 | } |
| 1379 | |
| 1380 | static void ceph_restore_sigs(sigset_t *oldset) |
| 1381 | { |
| 1382 | sigprocmask(SIG_SETMASK, oldset, NULL); |
| 1383 | } |
Sage Weil | 1d3576f | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1384 | |
| 1385 | /* |
| 1386 | * vm ops |
| 1387 | */ |
Yan, Zheng | 61f6881 | 2013-11-28 14:28:14 +0800 | [diff] [blame] | 1388 | static int ceph_filemap_fault(struct vm_area_struct *vma, struct vm_fault *vmf) |
| 1389 | { |
| 1390 | struct inode *inode = file_inode(vma->vm_file); |
| 1391 | struct ceph_inode_info *ci = ceph_inode(inode); |
| 1392 | struct ceph_file_info *fi = vma->vm_file->private_data; |
Yan, Zheng | 3738daa | 2014-11-14 22:10:07 +0800 | [diff] [blame] | 1393 | struct page *pinned_page = NULL; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1394 | loff_t off = vmf->pgoff << PAGE_SHIFT; |
Yan, Zheng | 61f6881 | 2013-11-28 14:28:14 +0800 | [diff] [blame] | 1395 | int want, got, ret; |
Yan, Zheng | 4f7e89f | 2016-05-10 18:40:28 +0800 | [diff] [blame] | 1396 | sigset_t oldset; |
| 1397 | |
| 1398 | ceph_block_sigs(&oldset); |
Yan, Zheng | 61f6881 | 2013-11-28 14:28:14 +0800 | [diff] [blame] | 1399 | |
| 1400 | 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] | 1401 | inode, ceph_vinop(inode), off, (size_t)PAGE_SIZE); |
Yan, Zheng | 61f6881 | 2013-11-28 14:28:14 +0800 | [diff] [blame] | 1402 | if (fi->fmode & CEPH_FILE_MODE_LAZY) |
| 1403 | want = CEPH_CAP_FILE_CACHE | CEPH_CAP_FILE_LAZYIO; |
| 1404 | else |
| 1405 | want = CEPH_CAP_FILE_CACHE; |
Yan, Zheng | 4f7e89f | 2016-05-10 18:40:28 +0800 | [diff] [blame] | 1406 | |
| 1407 | got = 0; |
| 1408 | 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] | 1409 | if (ret < 0) |
Yan, Zheng | 4f7e89f | 2016-05-10 18:40:28 +0800 | [diff] [blame] | 1410 | goto out_restore; |
Yan, Zheng | 6ce026e | 2016-05-10 18:59:13 +0800 | [diff] [blame] | 1411 | |
Yan, Zheng | 61f6881 | 2013-11-28 14:28:14 +0800 | [diff] [blame] | 1412 | 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] | 1413 | inode, off, (size_t)PAGE_SIZE, ceph_cap_string(got)); |
Yan, Zheng | 61f6881 | 2013-11-28 14:28:14 +0800 | [diff] [blame] | 1414 | |
Yan, Zheng | 8370124 | 2014-11-14 22:36:18 +0800 | [diff] [blame] | 1415 | if ((got & (CEPH_CAP_FILE_CACHE | CEPH_CAP_FILE_LAZYIO)) || |
Yan, Zheng | 2b1ac85 | 2016-10-25 10:51:55 +0800 | [diff] [blame] | 1416 | ci->i_inline_version == CEPH_INLINE_NONE) { |
| 1417 | current->journal_info = vma->vm_file; |
Yan, Zheng | 8370124 | 2014-11-14 22:36:18 +0800 | [diff] [blame] | 1418 | ret = filemap_fault(vma, vmf); |
Yan, Zheng | 2b1ac85 | 2016-10-25 10:51:55 +0800 | [diff] [blame] | 1419 | current->journal_info = NULL; |
| 1420 | } else |
Yan, Zheng | 8370124 | 2014-11-14 22:36:18 +0800 | [diff] [blame] | 1421 | ret = -EAGAIN; |
Yan, Zheng | 61f6881 | 2013-11-28 14:28:14 +0800 | [diff] [blame] | 1422 | |
| 1423 | 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] | 1424 | inode, off, (size_t)PAGE_SIZE, ceph_cap_string(got), ret); |
Yan, Zheng | 3738daa | 2014-11-14 22:10:07 +0800 | [diff] [blame] | 1425 | if (pinned_page) |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1426 | put_page(pinned_page); |
Yan, Zheng | 61f6881 | 2013-11-28 14:28:14 +0800 | [diff] [blame] | 1427 | ceph_put_cap_refs(ci, got); |
| 1428 | |
Yan, Zheng | 8370124 | 2014-11-14 22:36:18 +0800 | [diff] [blame] | 1429 | if (ret != -EAGAIN) |
Yan, Zheng | 4f7e89f | 2016-05-10 18:40:28 +0800 | [diff] [blame] | 1430 | goto out_restore; |
Yan, Zheng | 8370124 | 2014-11-14 22:36:18 +0800 | [diff] [blame] | 1431 | |
| 1432 | /* read inline data */ |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1433 | if (off >= PAGE_SIZE) { |
Yan, Zheng | 8370124 | 2014-11-14 22:36:18 +0800 | [diff] [blame] | 1434 | /* does not support inline data > PAGE_SIZE */ |
| 1435 | ret = VM_FAULT_SIGBUS; |
| 1436 | } else { |
| 1437 | int ret1; |
| 1438 | struct address_space *mapping = inode->i_mapping; |
| 1439 | struct page *page = find_or_create_page(mapping, 0, |
Michal Hocko | c62d255 | 2015-11-06 16:28:49 -0800 | [diff] [blame] | 1440 | mapping_gfp_constraint(mapping, |
| 1441 | ~__GFP_FS)); |
Yan, Zheng | 8370124 | 2014-11-14 22:36:18 +0800 | [diff] [blame] | 1442 | if (!page) { |
| 1443 | ret = VM_FAULT_OOM; |
Yan, Zheng | 4f7e89f | 2016-05-10 18:40:28 +0800 | [diff] [blame] | 1444 | goto out_inline; |
Yan, Zheng | 8370124 | 2014-11-14 22:36:18 +0800 | [diff] [blame] | 1445 | } |
| 1446 | ret1 = __ceph_do_getattr(inode, page, |
| 1447 | CEPH_STAT_CAP_INLINE_DATA, true); |
| 1448 | if (ret1 < 0 || off >= i_size_read(inode)) { |
| 1449 | unlock_page(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1450 | put_page(page); |
Yan, Zheng | 6ce026e | 2016-05-10 18:59:13 +0800 | [diff] [blame] | 1451 | if (ret1 < 0) |
| 1452 | ret = ret1; |
| 1453 | else |
| 1454 | ret = VM_FAULT_SIGBUS; |
Yan, Zheng | 4f7e89f | 2016-05-10 18:40:28 +0800 | [diff] [blame] | 1455 | goto out_inline; |
Yan, Zheng | 8370124 | 2014-11-14 22:36:18 +0800 | [diff] [blame] | 1456 | } |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1457 | if (ret1 < PAGE_SIZE) |
| 1458 | zero_user_segment(page, ret1, PAGE_SIZE); |
Yan, Zheng | 8370124 | 2014-11-14 22:36:18 +0800 | [diff] [blame] | 1459 | else |
| 1460 | flush_dcache_page(page); |
| 1461 | SetPageUptodate(page); |
| 1462 | vmf->page = page; |
| 1463 | ret = VM_FAULT_MAJOR | VM_FAULT_LOCKED; |
Yan, Zheng | 4f7e89f | 2016-05-10 18:40:28 +0800 | [diff] [blame] | 1464 | out_inline: |
| 1465 | dout("filemap_fault %p %llu~%zd read inline data ret %d\n", |
| 1466 | inode, off, (size_t)PAGE_SIZE, ret); |
Yan, Zheng | 8370124 | 2014-11-14 22:36:18 +0800 | [diff] [blame] | 1467 | } |
Yan, Zheng | 4f7e89f | 2016-05-10 18:40:28 +0800 | [diff] [blame] | 1468 | out_restore: |
| 1469 | ceph_restore_sigs(&oldset); |
Yan, Zheng | 6ce026e | 2016-05-10 18:59:13 +0800 | [diff] [blame] | 1470 | if (ret < 0) |
| 1471 | ret = (ret == -ENOMEM) ? VM_FAULT_OOM : VM_FAULT_SIGBUS; |
| 1472 | |
Yan, Zheng | 61f6881 | 2013-11-28 14:28:14 +0800 | [diff] [blame] | 1473 | return ret; |
| 1474 | } |
Sage Weil | 1d3576f | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1475 | |
| 1476 | /* |
| 1477 | * Reuse write_begin here for simplicity. |
| 1478 | */ |
| 1479 | static int ceph_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf) |
| 1480 | { |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 1481 | struct inode *inode = file_inode(vma->vm_file); |
Yan, Zheng | 61f6881 | 2013-11-28 14:28:14 +0800 | [diff] [blame] | 1482 | struct ceph_inode_info *ci = ceph_inode(inode); |
| 1483 | struct ceph_file_info *fi = vma->vm_file->private_data; |
Yan, Zheng | f66fd9f | 2015-06-10 17:26:13 +0800 | [diff] [blame] | 1484 | struct ceph_cap_flush *prealloc_cf; |
Yan, Zheng | 61f6881 | 2013-11-28 14:28:14 +0800 | [diff] [blame] | 1485 | struct page *page = vmf->page; |
Alex Elder | 6285bc2 | 2012-10-02 10:25:51 -0500 | [diff] [blame] | 1486 | loff_t off = page_offset(page); |
Yan, Zheng | 61f6881 | 2013-11-28 14:28:14 +0800 | [diff] [blame] | 1487 | loff_t size = i_size_read(inode); |
| 1488 | size_t len; |
| 1489 | int want, got, ret; |
Yan, Zheng | 4f7e89f | 2016-05-10 18:40:28 +0800 | [diff] [blame] | 1490 | sigset_t oldset; |
Sage Weil | 1d3576f | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1491 | |
Yan, Zheng | f66fd9f | 2015-06-10 17:26:13 +0800 | [diff] [blame] | 1492 | prealloc_cf = ceph_alloc_cap_flush(); |
| 1493 | if (!prealloc_cf) |
Yan, Zheng | 6ce026e | 2016-05-10 18:59:13 +0800 | [diff] [blame] | 1494 | return VM_FAULT_OOM; |
Yan, Zheng | f66fd9f | 2015-06-10 17:26:13 +0800 | [diff] [blame] | 1495 | |
Yan, Zheng | 4f7e89f | 2016-05-10 18:40:28 +0800 | [diff] [blame] | 1496 | ceph_block_sigs(&oldset); |
Sage Weil | 1d3576f | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1497 | |
Yan, Zheng | 28127bd | 2014-11-14 22:38:29 +0800 | [diff] [blame] | 1498 | if (ci->i_inline_version != CEPH_INLINE_NONE) { |
| 1499 | struct page *locked_page = NULL; |
| 1500 | if (off == 0) { |
| 1501 | lock_page(page); |
| 1502 | locked_page = page; |
| 1503 | } |
| 1504 | ret = ceph_uninline_data(vma->vm_file, locked_page); |
| 1505 | if (locked_page) |
| 1506 | unlock_page(locked_page); |
Yan, Zheng | 6ce026e | 2016-05-10 18:59:13 +0800 | [diff] [blame] | 1507 | if (ret < 0) |
Yan, Zheng | f66fd9f | 2015-06-10 17:26:13 +0800 | [diff] [blame] | 1508 | goto out_free; |
Yan, Zheng | 28127bd | 2014-11-14 22:38:29 +0800 | [diff] [blame] | 1509 | } |
| 1510 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1511 | if (off + PAGE_SIZE <= size) |
| 1512 | len = PAGE_SIZE; |
Sage Weil | 1d3576f | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1513 | else |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1514 | len = size & ~PAGE_MASK; |
Sage Weil | 1d3576f | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1515 | |
Yan, Zheng | 61f6881 | 2013-11-28 14:28:14 +0800 | [diff] [blame] | 1516 | dout("page_mkwrite %p %llx.%llx %llu~%zd getting caps i_size %llu\n", |
| 1517 | inode, ceph_vinop(inode), off, len, size); |
| 1518 | if (fi->fmode & CEPH_FILE_MODE_LAZY) |
| 1519 | want = CEPH_CAP_FILE_BUFFER | CEPH_CAP_FILE_LAZYIO; |
| 1520 | else |
| 1521 | want = CEPH_CAP_FILE_BUFFER; |
Yan, Zheng | 4f7e89f | 2016-05-10 18:40:28 +0800 | [diff] [blame] | 1522 | |
| 1523 | got = 0; |
| 1524 | ret = ceph_get_caps(ci, CEPH_CAP_FILE_WR, want, off + len, |
| 1525 | &got, NULL); |
Yan, Zheng | 6ce026e | 2016-05-10 18:59:13 +0800 | [diff] [blame] | 1526 | if (ret < 0) |
Yan, Zheng | 4f7e89f | 2016-05-10 18:40:28 +0800 | [diff] [blame] | 1527 | goto out_free; |
Yan, Zheng | 6ce026e | 2016-05-10 18:59:13 +0800 | [diff] [blame] | 1528 | |
Yan, Zheng | 61f6881 | 2013-11-28 14:28:14 +0800 | [diff] [blame] | 1529 | dout("page_mkwrite %p %llu~%zd got cap refs on %s\n", |
| 1530 | inode, off, len, ceph_cap_string(got)); |
| 1531 | |
| 1532 | /* Update time before taking page lock */ |
| 1533 | file_update_time(vma->vm_file); |
Yehuda Sadeh | 4af6b22 | 2010-02-09 11:02:51 -0800 | [diff] [blame] | 1534 | |
Yan, Zheng | f0b33df | 2016-05-10 19:09:06 +0800 | [diff] [blame] | 1535 | do { |
| 1536 | lock_page(page); |
Yehuda Sadeh | 4af6b22 | 2010-02-09 11:02:51 -0800 | [diff] [blame] | 1537 | |
Yan, Zheng | f0b33df | 2016-05-10 19:09:06 +0800 | [diff] [blame] | 1538 | if ((off > size) || (page->mapping != inode->i_mapping)) { |
| 1539 | unlock_page(page); |
| 1540 | ret = VM_FAULT_NOPAGE; |
| 1541 | break; |
| 1542 | } |
Yehuda Sadeh | 4af6b22 | 2010-02-09 11:02:51 -0800 | [diff] [blame] | 1543 | |
Yan, Zheng | f0b33df | 2016-05-10 19:09:06 +0800 | [diff] [blame] | 1544 | ret = ceph_update_writeable_page(vma->vm_file, off, len, page); |
| 1545 | if (ret >= 0) { |
| 1546 | /* success. we'll keep the page locked. */ |
| 1547 | set_page_dirty(page); |
| 1548 | ret = VM_FAULT_LOCKED; |
| 1549 | } |
| 1550 | } while (ret == -EAGAIN); |
| 1551 | |
Yan, Zheng | 28127bd | 2014-11-14 22:38:29 +0800 | [diff] [blame] | 1552 | if (ret == VM_FAULT_LOCKED || |
| 1553 | ci->i_inline_version != CEPH_INLINE_NONE) { |
Yan, Zheng | 61f6881 | 2013-11-28 14:28:14 +0800 | [diff] [blame] | 1554 | int dirty; |
| 1555 | spin_lock(&ci->i_ceph_lock); |
Yan, Zheng | 28127bd | 2014-11-14 22:38:29 +0800 | [diff] [blame] | 1556 | ci->i_inline_version = CEPH_INLINE_NONE; |
Yan, Zheng | f66fd9f | 2015-06-10 17:26:13 +0800 | [diff] [blame] | 1557 | dirty = __ceph_mark_dirty_caps(ci, CEPH_CAP_FILE_WR, |
| 1558 | &prealloc_cf); |
Yan, Zheng | 61f6881 | 2013-11-28 14:28:14 +0800 | [diff] [blame] | 1559 | spin_unlock(&ci->i_ceph_lock); |
| 1560 | if (dirty) |
| 1561 | __mark_inode_dirty(inode, dirty); |
| 1562 | } |
| 1563 | |
| 1564 | dout("page_mkwrite %p %llu~%zd dropping cap refs on %s ret %d\n", |
| 1565 | inode, off, len, ceph_cap_string(got), ret); |
| 1566 | ceph_put_cap_refs(ci, got); |
Yan, Zheng | f66fd9f | 2015-06-10 17:26:13 +0800 | [diff] [blame] | 1567 | out_free: |
Yan, Zheng | 4f7e89f | 2016-05-10 18:40:28 +0800 | [diff] [blame] | 1568 | ceph_restore_sigs(&oldset); |
Yan, Zheng | f66fd9f | 2015-06-10 17:26:13 +0800 | [diff] [blame] | 1569 | ceph_free_cap_flush(prealloc_cf); |
Yan, Zheng | 6ce026e | 2016-05-10 18:59:13 +0800 | [diff] [blame] | 1570 | if (ret < 0) |
| 1571 | ret = (ret == -ENOMEM) ? VM_FAULT_OOM : VM_FAULT_SIGBUS; |
Sage Weil | 1d3576f | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1572 | return ret; |
| 1573 | } |
| 1574 | |
Yan, Zheng | 31c542a | 2014-11-14 21:41:55 +0800 | [diff] [blame] | 1575 | void ceph_fill_inline_data(struct inode *inode, struct page *locked_page, |
| 1576 | char *data, size_t len) |
| 1577 | { |
| 1578 | struct address_space *mapping = inode->i_mapping; |
| 1579 | struct page *page; |
| 1580 | |
| 1581 | if (locked_page) { |
| 1582 | page = locked_page; |
| 1583 | } else { |
| 1584 | if (i_size_read(inode) == 0) |
| 1585 | return; |
| 1586 | page = find_or_create_page(mapping, 0, |
Michal Hocko | c62d255 | 2015-11-06 16:28:49 -0800 | [diff] [blame] | 1587 | mapping_gfp_constraint(mapping, |
| 1588 | ~__GFP_FS)); |
Yan, Zheng | 31c542a | 2014-11-14 21:41:55 +0800 | [diff] [blame] | 1589 | if (!page) |
| 1590 | return; |
| 1591 | if (PageUptodate(page)) { |
| 1592 | unlock_page(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1593 | put_page(page); |
Yan, Zheng | 31c542a | 2014-11-14 21:41:55 +0800 | [diff] [blame] | 1594 | return; |
| 1595 | } |
| 1596 | } |
| 1597 | |
Ilya Dryomov | 0668ff5 | 2014-12-19 13:10:10 +0300 | [diff] [blame] | 1598 | 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] | 1599 | inode, ceph_vinop(inode), len, locked_page); |
| 1600 | |
| 1601 | if (len > 0) { |
| 1602 | void *kaddr = kmap_atomic(page); |
| 1603 | memcpy(kaddr, data, len); |
| 1604 | kunmap_atomic(kaddr); |
| 1605 | } |
| 1606 | |
| 1607 | if (page != locked_page) { |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1608 | if (len < PAGE_SIZE) |
| 1609 | zero_user_segment(page, len, PAGE_SIZE); |
Yan, Zheng | 31c542a | 2014-11-14 21:41:55 +0800 | [diff] [blame] | 1610 | else |
| 1611 | flush_dcache_page(page); |
| 1612 | |
| 1613 | SetPageUptodate(page); |
| 1614 | unlock_page(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1615 | put_page(page); |
Yan, Zheng | 31c542a | 2014-11-14 21:41:55 +0800 | [diff] [blame] | 1616 | } |
| 1617 | } |
| 1618 | |
Yan, Zheng | 28127bd | 2014-11-14 22:38:29 +0800 | [diff] [blame] | 1619 | int ceph_uninline_data(struct file *filp, struct page *locked_page) |
| 1620 | { |
| 1621 | struct inode *inode = file_inode(filp); |
| 1622 | struct ceph_inode_info *ci = ceph_inode(inode); |
| 1623 | struct ceph_fs_client *fsc = ceph_inode_to_client(inode); |
| 1624 | struct ceph_osd_request *req; |
| 1625 | struct page *page = NULL; |
| 1626 | u64 len, inline_version; |
| 1627 | int err = 0; |
| 1628 | bool from_pagecache = false; |
| 1629 | |
| 1630 | spin_lock(&ci->i_ceph_lock); |
| 1631 | inline_version = ci->i_inline_version; |
| 1632 | spin_unlock(&ci->i_ceph_lock); |
| 1633 | |
| 1634 | dout("uninline_data %p %llx.%llx inline_version %llu\n", |
| 1635 | inode, ceph_vinop(inode), inline_version); |
| 1636 | |
| 1637 | if (inline_version == 1 || /* initial version, no data */ |
| 1638 | inline_version == CEPH_INLINE_NONE) |
| 1639 | goto out; |
| 1640 | |
| 1641 | if (locked_page) { |
| 1642 | page = locked_page; |
| 1643 | WARN_ON(!PageUptodate(page)); |
| 1644 | } else if (ceph_caps_issued(ci) & |
| 1645 | (CEPH_CAP_FILE_CACHE|CEPH_CAP_FILE_LAZYIO)) { |
| 1646 | page = find_get_page(inode->i_mapping, 0); |
| 1647 | if (page) { |
| 1648 | if (PageUptodate(page)) { |
| 1649 | from_pagecache = true; |
| 1650 | lock_page(page); |
| 1651 | } else { |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1652 | put_page(page); |
Yan, Zheng | 28127bd | 2014-11-14 22:38:29 +0800 | [diff] [blame] | 1653 | page = NULL; |
| 1654 | } |
| 1655 | } |
| 1656 | } |
| 1657 | |
| 1658 | if (page) { |
| 1659 | len = i_size_read(inode); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1660 | if (len > PAGE_SIZE) |
| 1661 | len = PAGE_SIZE; |
Yan, Zheng | 28127bd | 2014-11-14 22:38:29 +0800 | [diff] [blame] | 1662 | } else { |
| 1663 | page = __page_cache_alloc(GFP_NOFS); |
| 1664 | if (!page) { |
| 1665 | err = -ENOMEM; |
| 1666 | goto out; |
| 1667 | } |
| 1668 | err = __ceph_do_getattr(inode, page, |
| 1669 | CEPH_STAT_CAP_INLINE_DATA, true); |
| 1670 | if (err < 0) { |
| 1671 | /* no inline data */ |
| 1672 | if (err == -ENODATA) |
| 1673 | err = 0; |
| 1674 | goto out; |
| 1675 | } |
| 1676 | len = err; |
| 1677 | } |
| 1678 | |
| 1679 | req = ceph_osdc_new_request(&fsc->client->osdc, &ci->i_layout, |
| 1680 | ceph_vino(inode), 0, &len, 0, 1, |
Ilya Dryomov | 54ea004 | 2017-02-11 18:48:41 +0100 | [diff] [blame^] | 1681 | CEPH_OSD_OP_CREATE, CEPH_OSD_FLAG_WRITE, |
Ilya Dryomov | 34b759b | 2016-02-16 15:00:24 +0100 | [diff] [blame] | 1682 | NULL, 0, 0, false); |
Yan, Zheng | 28127bd | 2014-11-14 22:38:29 +0800 | [diff] [blame] | 1683 | if (IS_ERR(req)) { |
| 1684 | err = PTR_ERR(req); |
| 1685 | goto out; |
| 1686 | } |
| 1687 | |
Ilya Dryomov | bb873b539 | 2016-05-26 00:29:52 +0200 | [diff] [blame] | 1688 | req->r_mtime = inode->i_mtime; |
Yan, Zheng | 28127bd | 2014-11-14 22:38:29 +0800 | [diff] [blame] | 1689 | err = ceph_osdc_start_request(&fsc->client->osdc, req, false); |
| 1690 | if (!err) |
| 1691 | err = ceph_osdc_wait_request(&fsc->client->osdc, req); |
| 1692 | ceph_osdc_put_request(req); |
| 1693 | if (err < 0) |
| 1694 | goto out; |
| 1695 | |
| 1696 | req = ceph_osdc_new_request(&fsc->client->osdc, &ci->i_layout, |
| 1697 | ceph_vino(inode), 0, &len, 1, 3, |
Ilya Dryomov | 54ea004 | 2017-02-11 18:48:41 +0100 | [diff] [blame^] | 1698 | CEPH_OSD_OP_WRITE, CEPH_OSD_FLAG_WRITE, |
Ilya Dryomov | 34b759b | 2016-02-16 15:00:24 +0100 | [diff] [blame] | 1699 | NULL, ci->i_truncate_seq, |
| 1700 | ci->i_truncate_size, false); |
Yan, Zheng | 28127bd | 2014-11-14 22:38:29 +0800 | [diff] [blame] | 1701 | if (IS_ERR(req)) { |
| 1702 | err = PTR_ERR(req); |
| 1703 | goto out; |
| 1704 | } |
| 1705 | |
| 1706 | osd_req_op_extent_osd_data_pages(req, 1, &page, len, 0, false, false); |
| 1707 | |
Yan, Zheng | ec137c1 | 2015-04-13 11:25:07 +0800 | [diff] [blame] | 1708 | { |
| 1709 | __le64 xattr_buf = cpu_to_le64(inline_version); |
| 1710 | err = osd_req_op_xattr_init(req, 0, CEPH_OSD_OP_CMPXATTR, |
| 1711 | "inline_version", &xattr_buf, |
| 1712 | sizeof(xattr_buf), |
| 1713 | CEPH_OSD_CMPXATTR_OP_GT, |
| 1714 | CEPH_OSD_CMPXATTR_MODE_U64); |
| 1715 | if (err) |
| 1716 | goto out_put; |
| 1717 | } |
Yan, Zheng | 28127bd | 2014-11-14 22:38:29 +0800 | [diff] [blame] | 1718 | |
Yan, Zheng | ec137c1 | 2015-04-13 11:25:07 +0800 | [diff] [blame] | 1719 | { |
| 1720 | char xattr_buf[32]; |
| 1721 | int xattr_len = snprintf(xattr_buf, sizeof(xattr_buf), |
| 1722 | "%llu", inline_version); |
| 1723 | err = osd_req_op_xattr_init(req, 2, CEPH_OSD_OP_SETXATTR, |
| 1724 | "inline_version", |
| 1725 | xattr_buf, xattr_len, 0, 0); |
| 1726 | if (err) |
| 1727 | goto out_put; |
| 1728 | } |
Yan, Zheng | 28127bd | 2014-11-14 22:38:29 +0800 | [diff] [blame] | 1729 | |
Ilya Dryomov | bb873b539 | 2016-05-26 00:29:52 +0200 | [diff] [blame] | 1730 | req->r_mtime = inode->i_mtime; |
Yan, Zheng | 28127bd | 2014-11-14 22:38:29 +0800 | [diff] [blame] | 1731 | err = ceph_osdc_start_request(&fsc->client->osdc, req, false); |
| 1732 | if (!err) |
| 1733 | err = ceph_osdc_wait_request(&fsc->client->osdc, req); |
| 1734 | out_put: |
| 1735 | ceph_osdc_put_request(req); |
| 1736 | if (err == -ECANCELED) |
| 1737 | err = 0; |
| 1738 | out: |
| 1739 | if (page && page != locked_page) { |
| 1740 | if (from_pagecache) { |
| 1741 | unlock_page(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1742 | put_page(page); |
Yan, Zheng | 28127bd | 2014-11-14 22:38:29 +0800 | [diff] [blame] | 1743 | } else |
| 1744 | __free_pages(page, 0); |
| 1745 | } |
| 1746 | |
| 1747 | dout("uninline_data %p %llx.%llx inline_version %llu = %d\n", |
| 1748 | inode, ceph_vinop(inode), inline_version, err); |
| 1749 | return err; |
| 1750 | } |
| 1751 | |
Kirill A. Shutemov | 7cbea8d | 2015-09-09 15:39:26 -0700 | [diff] [blame] | 1752 | static const struct vm_operations_struct ceph_vmops = { |
Yan, Zheng | 61f6881 | 2013-11-28 14:28:14 +0800 | [diff] [blame] | 1753 | .fault = ceph_filemap_fault, |
Sage Weil | 1d3576f | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1754 | .page_mkwrite = ceph_page_mkwrite, |
| 1755 | }; |
| 1756 | |
| 1757 | int ceph_mmap(struct file *file, struct vm_area_struct *vma) |
| 1758 | { |
| 1759 | struct address_space *mapping = file->f_mapping; |
| 1760 | |
| 1761 | if (!mapping->a_ops->readpage) |
| 1762 | return -ENOEXEC; |
| 1763 | file_accessed(file); |
| 1764 | vma->vm_ops = &ceph_vmops; |
Sage Weil | 1d3576f | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1765 | return 0; |
| 1766 | } |
Yan, Zheng | 10183a6 | 2015-04-27 15:33:28 +0800 | [diff] [blame] | 1767 | |
| 1768 | enum { |
| 1769 | POOL_READ = 1, |
| 1770 | POOL_WRITE = 2, |
| 1771 | }; |
| 1772 | |
Yan, Zheng | 779fe0f | 2016-03-07 09:35:06 +0800 | [diff] [blame] | 1773 | static int __ceph_pool_perm_get(struct ceph_inode_info *ci, |
| 1774 | s64 pool, struct ceph_string *pool_ns) |
Yan, Zheng | 10183a6 | 2015-04-27 15:33:28 +0800 | [diff] [blame] | 1775 | { |
| 1776 | struct ceph_fs_client *fsc = ceph_inode_to_client(&ci->vfs_inode); |
| 1777 | struct ceph_mds_client *mdsc = fsc->mdsc; |
| 1778 | struct ceph_osd_request *rd_req = NULL, *wr_req = NULL; |
| 1779 | struct rb_node **p, *parent; |
| 1780 | struct ceph_pool_perm *perm; |
| 1781 | struct page **pages; |
Yan, Zheng | 779fe0f | 2016-03-07 09:35:06 +0800 | [diff] [blame] | 1782 | size_t pool_ns_len; |
Yan, Zheng | 10183a6 | 2015-04-27 15:33:28 +0800 | [diff] [blame] | 1783 | int err = 0, err2 = 0, have = 0; |
| 1784 | |
| 1785 | down_read(&mdsc->pool_perm_rwsem); |
| 1786 | p = &mdsc->pool_perm_tree.rb_node; |
| 1787 | while (*p) { |
| 1788 | perm = rb_entry(*p, struct ceph_pool_perm, node); |
| 1789 | if (pool < perm->pool) |
| 1790 | p = &(*p)->rb_left; |
| 1791 | else if (pool > perm->pool) |
| 1792 | p = &(*p)->rb_right; |
| 1793 | else { |
Yan, Zheng | 779fe0f | 2016-03-07 09:35:06 +0800 | [diff] [blame] | 1794 | int ret = ceph_compare_string(pool_ns, |
| 1795 | perm->pool_ns, |
| 1796 | perm->pool_ns_len); |
| 1797 | if (ret < 0) |
| 1798 | p = &(*p)->rb_left; |
| 1799 | else if (ret > 0) |
| 1800 | p = &(*p)->rb_right; |
| 1801 | else { |
| 1802 | have = perm->perm; |
| 1803 | break; |
| 1804 | } |
Yan, Zheng | 10183a6 | 2015-04-27 15:33:28 +0800 | [diff] [blame] | 1805 | } |
| 1806 | } |
| 1807 | up_read(&mdsc->pool_perm_rwsem); |
| 1808 | if (*p) |
| 1809 | goto out; |
| 1810 | |
Yan, Zheng | 779fe0f | 2016-03-07 09:35:06 +0800 | [diff] [blame] | 1811 | if (pool_ns) |
| 1812 | dout("__ceph_pool_perm_get pool %lld ns %.*s no perm cached\n", |
| 1813 | pool, (int)pool_ns->len, pool_ns->str); |
| 1814 | else |
| 1815 | dout("__ceph_pool_perm_get pool %lld no perm cached\n", pool); |
Yan, Zheng | 10183a6 | 2015-04-27 15:33:28 +0800 | [diff] [blame] | 1816 | |
| 1817 | down_write(&mdsc->pool_perm_rwsem); |
Yan, Zheng | 779fe0f | 2016-03-07 09:35:06 +0800 | [diff] [blame] | 1818 | p = &mdsc->pool_perm_tree.rb_node; |
Yan, Zheng | 10183a6 | 2015-04-27 15:33:28 +0800 | [diff] [blame] | 1819 | parent = NULL; |
| 1820 | while (*p) { |
| 1821 | parent = *p; |
| 1822 | perm = rb_entry(parent, struct ceph_pool_perm, node); |
| 1823 | if (pool < perm->pool) |
| 1824 | p = &(*p)->rb_left; |
| 1825 | else if (pool > perm->pool) |
| 1826 | p = &(*p)->rb_right; |
| 1827 | else { |
Yan, Zheng | 779fe0f | 2016-03-07 09:35:06 +0800 | [diff] [blame] | 1828 | int ret = ceph_compare_string(pool_ns, |
| 1829 | perm->pool_ns, |
| 1830 | perm->pool_ns_len); |
| 1831 | if (ret < 0) |
| 1832 | p = &(*p)->rb_left; |
| 1833 | else if (ret > 0) |
| 1834 | p = &(*p)->rb_right; |
| 1835 | else { |
| 1836 | have = perm->perm; |
| 1837 | break; |
| 1838 | } |
Yan, Zheng | 10183a6 | 2015-04-27 15:33:28 +0800 | [diff] [blame] | 1839 | } |
| 1840 | } |
| 1841 | if (*p) { |
| 1842 | up_write(&mdsc->pool_perm_rwsem); |
| 1843 | goto out; |
| 1844 | } |
| 1845 | |
Ilya Dryomov | 34b759b | 2016-02-16 15:00:24 +0100 | [diff] [blame] | 1846 | rd_req = ceph_osdc_alloc_request(&fsc->client->osdc, NULL, |
Yan, Zheng | 10183a6 | 2015-04-27 15:33:28 +0800 | [diff] [blame] | 1847 | 1, false, GFP_NOFS); |
| 1848 | if (!rd_req) { |
| 1849 | err = -ENOMEM; |
| 1850 | goto out_unlock; |
| 1851 | } |
| 1852 | |
| 1853 | rd_req->r_flags = CEPH_OSD_FLAG_READ; |
| 1854 | osd_req_op_init(rd_req, 0, CEPH_OSD_OP_STAT, 0); |
| 1855 | rd_req->r_base_oloc.pool = pool; |
Yan, Zheng | 779fe0f | 2016-03-07 09:35:06 +0800 | [diff] [blame] | 1856 | if (pool_ns) |
| 1857 | rd_req->r_base_oloc.pool_ns = ceph_get_string(pool_ns); |
Ilya Dryomov | d30291b | 2016-04-29 19:54:20 +0200 | [diff] [blame] | 1858 | 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] | 1859 | |
Ilya Dryomov | 13d1ad1 | 2016-04-27 14:15:51 +0200 | [diff] [blame] | 1860 | err = ceph_osdc_alloc_messages(rd_req, GFP_NOFS); |
| 1861 | if (err) |
| 1862 | goto out_unlock; |
Yan, Zheng | 10183a6 | 2015-04-27 15:33:28 +0800 | [diff] [blame] | 1863 | |
Ilya Dryomov | 34b759b | 2016-02-16 15:00:24 +0100 | [diff] [blame] | 1864 | wr_req = ceph_osdc_alloc_request(&fsc->client->osdc, NULL, |
Yan, Zheng | 10183a6 | 2015-04-27 15:33:28 +0800 | [diff] [blame] | 1865 | 1, false, GFP_NOFS); |
| 1866 | if (!wr_req) { |
| 1867 | err = -ENOMEM; |
| 1868 | goto out_unlock; |
| 1869 | } |
| 1870 | |
Ilya Dryomov | 54ea004 | 2017-02-11 18:48:41 +0100 | [diff] [blame^] | 1871 | wr_req->r_flags = CEPH_OSD_FLAG_WRITE; |
Yan, Zheng | 10183a6 | 2015-04-27 15:33:28 +0800 | [diff] [blame] | 1872 | 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] | 1873 | 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] | 1874 | 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] | 1875 | |
Ilya Dryomov | 13d1ad1 | 2016-04-27 14:15:51 +0200 | [diff] [blame] | 1876 | err = ceph_osdc_alloc_messages(wr_req, GFP_NOFS); |
| 1877 | if (err) |
| 1878 | goto out_unlock; |
Yan, Zheng | 10183a6 | 2015-04-27 15:33:28 +0800 | [diff] [blame] | 1879 | |
| 1880 | /* one page should be large enough for STAT data */ |
| 1881 | pages = ceph_alloc_page_vector(1, GFP_KERNEL); |
| 1882 | if (IS_ERR(pages)) { |
| 1883 | err = PTR_ERR(pages); |
| 1884 | goto out_unlock; |
| 1885 | } |
| 1886 | |
| 1887 | osd_req_op_raw_data_in_pages(rd_req, 0, pages, PAGE_SIZE, |
| 1888 | 0, false, true); |
Yan, Zheng | 10183a6 | 2015-04-27 15:33:28 +0800 | [diff] [blame] | 1889 | err = ceph_osdc_start_request(&fsc->client->osdc, rd_req, false); |
| 1890 | |
Ilya Dryomov | bb873b539 | 2016-05-26 00:29:52 +0200 | [diff] [blame] | 1891 | wr_req->r_mtime = ci->vfs_inode.i_mtime; |
Yan, Zheng | 10183a6 | 2015-04-27 15:33:28 +0800 | [diff] [blame] | 1892 | err2 = ceph_osdc_start_request(&fsc->client->osdc, wr_req, false); |
| 1893 | |
| 1894 | if (!err) |
| 1895 | err = ceph_osdc_wait_request(&fsc->client->osdc, rd_req); |
| 1896 | if (!err2) |
| 1897 | err2 = ceph_osdc_wait_request(&fsc->client->osdc, wr_req); |
| 1898 | |
| 1899 | if (err >= 0 || err == -ENOENT) |
| 1900 | have |= POOL_READ; |
| 1901 | else if (err != -EPERM) |
| 1902 | goto out_unlock; |
| 1903 | |
| 1904 | if (err2 == 0 || err2 == -EEXIST) |
| 1905 | have |= POOL_WRITE; |
| 1906 | else if (err2 != -EPERM) { |
| 1907 | err = err2; |
| 1908 | goto out_unlock; |
| 1909 | } |
| 1910 | |
Yan, Zheng | 779fe0f | 2016-03-07 09:35:06 +0800 | [diff] [blame] | 1911 | pool_ns_len = pool_ns ? pool_ns->len : 0; |
| 1912 | perm = kmalloc(sizeof(*perm) + pool_ns_len + 1, GFP_NOFS); |
Yan, Zheng | 10183a6 | 2015-04-27 15:33:28 +0800 | [diff] [blame] | 1913 | if (!perm) { |
| 1914 | err = -ENOMEM; |
| 1915 | goto out_unlock; |
| 1916 | } |
| 1917 | |
| 1918 | perm->pool = pool; |
| 1919 | perm->perm = have; |
Yan, Zheng | 779fe0f | 2016-03-07 09:35:06 +0800 | [diff] [blame] | 1920 | perm->pool_ns_len = pool_ns_len; |
| 1921 | if (pool_ns_len > 0) |
| 1922 | memcpy(perm->pool_ns, pool_ns->str, pool_ns_len); |
| 1923 | perm->pool_ns[pool_ns_len] = 0; |
| 1924 | |
Yan, Zheng | 10183a6 | 2015-04-27 15:33:28 +0800 | [diff] [blame] | 1925 | rb_link_node(&perm->node, parent, p); |
| 1926 | rb_insert_color(&perm->node, &mdsc->pool_perm_tree); |
| 1927 | err = 0; |
| 1928 | out_unlock: |
| 1929 | up_write(&mdsc->pool_perm_rwsem); |
| 1930 | |
Ilya Dryomov | 3ed97d6 | 2016-04-26 15:05:29 +0200 | [diff] [blame] | 1931 | ceph_osdc_put_request(rd_req); |
| 1932 | ceph_osdc_put_request(wr_req); |
Yan, Zheng | 10183a6 | 2015-04-27 15:33:28 +0800 | [diff] [blame] | 1933 | out: |
| 1934 | if (!err) |
| 1935 | err = have; |
Yan, Zheng | 779fe0f | 2016-03-07 09:35:06 +0800 | [diff] [blame] | 1936 | if (pool_ns) |
| 1937 | dout("__ceph_pool_perm_get pool %lld ns %.*s result = %d\n", |
| 1938 | pool, (int)pool_ns->len, pool_ns->str, err); |
| 1939 | else |
| 1940 | dout("__ceph_pool_perm_get pool %lld result = %d\n", pool, err); |
Yan, Zheng | 10183a6 | 2015-04-27 15:33:28 +0800 | [diff] [blame] | 1941 | return err; |
| 1942 | } |
| 1943 | |
| 1944 | int ceph_pool_perm_check(struct ceph_inode_info *ci, int need) |
| 1945 | { |
Yan, Zheng | 7627151 | 2016-02-03 21:24:49 +0800 | [diff] [blame] | 1946 | s64 pool; |
Yan, Zheng | 779fe0f | 2016-03-07 09:35:06 +0800 | [diff] [blame] | 1947 | struct ceph_string *pool_ns; |
Yan, Zheng | 10183a6 | 2015-04-27 15:33:28 +0800 | [diff] [blame] | 1948 | int ret, flags; |
| 1949 | |
Yan, Zheng | 80e80fb | 2016-12-13 16:03:26 +0800 | [diff] [blame] | 1950 | if (ci->i_vino.snap != CEPH_NOSNAP) { |
| 1951 | /* |
| 1952 | * Pool permission check needs to write to the first object. |
| 1953 | * But for snapshot, head of the first object may have alread |
| 1954 | * been deleted. Skip check to avoid creating orphan object. |
| 1955 | */ |
| 1956 | return 0; |
| 1957 | } |
| 1958 | |
Yan, Zheng | 10183a6 | 2015-04-27 15:33:28 +0800 | [diff] [blame] | 1959 | if (ceph_test_mount_opt(ceph_inode_to_client(&ci->vfs_inode), |
| 1960 | NOPOOLPERM)) |
| 1961 | return 0; |
| 1962 | |
| 1963 | spin_lock(&ci->i_ceph_lock); |
| 1964 | flags = ci->i_ceph_flags; |
Yan, Zheng | 7627151 | 2016-02-03 21:24:49 +0800 | [diff] [blame] | 1965 | pool = ci->i_layout.pool_id; |
Yan, Zheng | 10183a6 | 2015-04-27 15:33:28 +0800 | [diff] [blame] | 1966 | spin_unlock(&ci->i_ceph_lock); |
| 1967 | check: |
| 1968 | if (flags & CEPH_I_POOL_PERM) { |
| 1969 | if ((need & CEPH_CAP_FILE_RD) && !(flags & CEPH_I_POOL_RD)) { |
Yan, Zheng | 7627151 | 2016-02-03 21:24:49 +0800 | [diff] [blame] | 1970 | dout("ceph_pool_perm_check pool %lld no read perm\n", |
Yan, Zheng | 10183a6 | 2015-04-27 15:33:28 +0800 | [diff] [blame] | 1971 | pool); |
| 1972 | return -EPERM; |
| 1973 | } |
| 1974 | if ((need & CEPH_CAP_FILE_WR) && !(flags & CEPH_I_POOL_WR)) { |
Yan, Zheng | 7627151 | 2016-02-03 21:24:49 +0800 | [diff] [blame] | 1975 | dout("ceph_pool_perm_check pool %lld no write perm\n", |
Yan, Zheng | 10183a6 | 2015-04-27 15:33:28 +0800 | [diff] [blame] | 1976 | pool); |
| 1977 | return -EPERM; |
| 1978 | } |
| 1979 | return 0; |
| 1980 | } |
| 1981 | |
Yan, Zheng | 779fe0f | 2016-03-07 09:35:06 +0800 | [diff] [blame] | 1982 | pool_ns = ceph_try_get_string(ci->i_layout.pool_ns); |
| 1983 | ret = __ceph_pool_perm_get(ci, pool, pool_ns); |
| 1984 | ceph_put_string(pool_ns); |
Yan, Zheng | 10183a6 | 2015-04-27 15:33:28 +0800 | [diff] [blame] | 1985 | if (ret < 0) |
| 1986 | return ret; |
| 1987 | |
| 1988 | flags = CEPH_I_POOL_PERM; |
| 1989 | if (ret & POOL_READ) |
| 1990 | flags |= CEPH_I_POOL_RD; |
| 1991 | if (ret & POOL_WRITE) |
| 1992 | flags |= CEPH_I_POOL_WR; |
| 1993 | |
| 1994 | spin_lock(&ci->i_ceph_lock); |
Yan, Zheng | 779fe0f | 2016-03-07 09:35:06 +0800 | [diff] [blame] | 1995 | if (pool == ci->i_layout.pool_id && |
| 1996 | pool_ns == rcu_dereference_raw(ci->i_layout.pool_ns)) { |
| 1997 | ci->i_ceph_flags |= flags; |
Yan, Zheng | 10183a6 | 2015-04-27 15:33:28 +0800 | [diff] [blame] | 1998 | } else { |
Yan, Zheng | 7627151 | 2016-02-03 21:24:49 +0800 | [diff] [blame] | 1999 | pool = ci->i_layout.pool_id; |
Yan, Zheng | 10183a6 | 2015-04-27 15:33:28 +0800 | [diff] [blame] | 2000 | flags = ci->i_ceph_flags; |
| 2001 | } |
| 2002 | spin_unlock(&ci->i_ceph_lock); |
| 2003 | goto check; |
| 2004 | } |
| 2005 | |
| 2006 | void ceph_pool_perm_destroy(struct ceph_mds_client *mdsc) |
| 2007 | { |
| 2008 | struct ceph_pool_perm *perm; |
| 2009 | struct rb_node *n; |
| 2010 | |
| 2011 | while (!RB_EMPTY_ROOT(&mdsc->pool_perm_tree)) { |
| 2012 | n = rb_first(&mdsc->pool_perm_tree); |
| 2013 | perm = rb_entry(n, struct ceph_pool_perm, node); |
| 2014 | rb_erase(n, &mdsc->pool_perm_tree); |
| 2015 | kfree(perm); |
| 2016 | } |
| 2017 | } |