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