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