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