blob: 72ff09d0438cfdbee4858f6ff96bea9544d5af2f [file] [log] [blame]
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001#include <linux/ceph/ceph_debug.h>
Sage Weil1d3576f2009-10-06 11:31:09 -07002
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 Heo5a0e3ad2010-03-24 17:04:11 +09008#include <linux/slab.h>
Sage Weil1d3576f2009-10-06 11:31:09 -07009#include <linux/pagevec.h>
10#include <linux/task_io_accounting_ops.h>
11
12#include "super.h"
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -070013#include "mds_client.h"
Milosz Tanski99ccbd22013-08-21 17:29:54 -040014#include "cache.h"
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -070015#include <linux/ceph/osd_client.h>
Sage Weil1d3576f2009-10-06 11:31:09 -070016
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 Marchi25985ed2011-03-30 22:57:33 -030028 * count dirty pages on the inode. In the absence of snapshots,
Sage Weil1d3576f2009-10-06 11:31:09 -070029 * 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 Sadeh2baba252009-12-18 13:51:57 -080053#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, Zheng61600ef2012-05-28 14:44:30 +080058static 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 Weil1d3576f2009-10-06 11:31:09 -070064
65/*
66 * Dirty a page. Optimistically adjust accounting, on the assumption
67 * that we won't race with invalidate. If we do, readjust.
68 */
69static 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 Weil1d3576f2009-10-06 11:31:09 -070074 struct ceph_snap_context *snapc;
Sage Weil1d3576f2009-10-06 11:31:09 -070075
Sha Zhengju7d6e1f52013-08-21 16:27:34 +080076 if (PageDirty(page)) {
Sage Weil1d3576f2009-10-06 11:31:09 -070077 dout("%p set_page_dirty %p idx %lu -- already dirty\n",
78 mapping->host, page, page->index);
Sha Zhengju7d6e1f52013-08-21 16:27:34 +080079 BUG_ON(!PagePrivate(page));
Sage Weil1d3576f2009-10-06 11:31:09 -070080 return 0;
81 }
82
83 inode = mapping->host;
84 ci = ceph_inode(inode);
85
Sage Weil1d3576f2009-10-06 11:31:09 -070086 /* dirty the head */
Sage Weilbe655592011-11-30 09:47:09 -080087 spin_lock(&ci->i_ceph_lock);
Yan, Zheng5dda377c2015-04-30 14:40:54 +080088 BUG_ON(ci->i_wr_ref == 0); // caller should hold Fw reference
89 if (__ceph_have_pending_cap_snap(ci)) {
90 struct ceph_cap_snap *capsnap =
91 list_last_entry(&ci->i_cap_snaps,
92 struct ceph_cap_snap,
93 ci_item);
94 snapc = ceph_get_snap_context(capsnap->context);
95 capsnap->dirty_pages++;
96 } else {
97 BUG_ON(!ci->i_head_snapc);
98 snapc = ceph_get_snap_context(ci->i_head_snapc);
99 ++ci->i_wrbuffer_ref_head;
100 }
Sage Weil1d3576f2009-10-06 11:31:09 -0700101 if (ci->i_wrbuffer_ref == 0)
Dave Chinner0444d762011-03-29 18:08:50 +1100102 ihold(inode);
Sage Weil1d3576f2009-10-06 11:31:09 -0700103 ++ci->i_wrbuffer_ref;
104 dout("%p set_page_dirty %p idx %lu head %d/%d -> %d/%d "
105 "snapc %p seq %lld (%d snaps)\n",
106 mapping->host, page, page->index,
107 ci->i_wrbuffer_ref-1, ci->i_wrbuffer_ref_head-1,
108 ci->i_wrbuffer_ref, ci->i_wrbuffer_ref_head,
109 snapc, snapc->seq, snapc->num_snaps);
Sage Weilbe655592011-11-30 09:47:09 -0800110 spin_unlock(&ci->i_ceph_lock);
Sage Weil1d3576f2009-10-06 11:31:09 -0700111
Sha Zhengju7d6e1f52013-08-21 16:27:34 +0800112 /*
113 * Reference snap context in page->private. Also set
114 * PagePrivate so that we get invalidatepage callback.
115 */
116 BUG_ON(PagePrivate(page));
117 page->private = (unsigned long)snapc;
118 SetPagePrivate(page);
Sage Weil1d3576f2009-10-06 11:31:09 -0700119
Jeff Layton58f8bcff2021-05-04 10:08:30 -0400120 return __set_page_dirty_nobuffers(page);
Sage Weil1d3576f2009-10-06 11:31:09 -0700121}
122
123/*
124 * If we are truncating the full page (i.e. offset == 0), adjust the
125 * dirty page counters appropriately. Only called if there is private
126 * data on the page.
127 */
Lukas Czernerd47992f2013-05-21 23:17:23 -0400128static void ceph_invalidatepage(struct page *page, unsigned int offset,
129 unsigned int length)
Sage Weil1d3576f2009-10-06 11:31:09 -0700130{
Alexander Beregalov4ce1e9a2010-02-22 17:17:44 +0300131 struct inode *inode;
Sage Weil1d3576f2009-10-06 11:31:09 -0700132 struct ceph_inode_info *ci;
Yan, Zheng61600ef2012-05-28 14:44:30 +0800133 struct ceph_snap_context *snapc = page_snap_context(page);
Sage Weil1d3576f2009-10-06 11:31:09 -0700134
Alexander Beregalov4ce1e9a2010-02-22 17:17:44 +0300135 inode = page->mapping->host;
Milosz Tanskib150f5c12013-08-09 12:59:55 -0400136 ci = ceph_inode(inode);
137
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300138 if (offset != 0 || length != PAGE_SIZE) {
Milosz Tanskib150f5c12013-08-09 12:59:55 -0400139 dout("%p invalidatepage %p idx %lu partial dirty page %u~%u\n",
140 inode, page, page->index, offset, length);
141 return;
142 }
Alexander Beregalov4ce1e9a2010-02-22 17:17:44 +0300143
Milosz Tanski99ccbd22013-08-21 17:29:54 -0400144 ceph_invalidate_fscache_page(inode, page);
145
146 if (!PagePrivate(page))
147 return;
148
Sage Weil1d3576f2009-10-06 11:31:09 -0700149 /*
150 * We can get non-dirty pages here due to races between
151 * set_page_dirty and truncate_complete_page; just spit out a
152 * warning, in case we end up with accounting problems later.
153 */
154 if (!PageDirty(page))
155 pr_err("%p invalidatepage %p page not dirty\n", inode, page);
156
Milosz Tanskib150f5c12013-08-09 12:59:55 -0400157 ClearPageChecked(page);
Sage Weil1d3576f2009-10-06 11:31:09 -0700158
Milosz Tanskib150f5c12013-08-09 12:59:55 -0400159 dout("%p invalidatepage %p idx %lu full dirty page\n",
160 inode, page, page->index);
161
162 ceph_put_wrbuffer_cap_refs(ci, 1, snapc);
163 ceph_put_snap_context(snapc);
164 page->private = 0;
165 ClearPagePrivate(page);
Sage Weil1d3576f2009-10-06 11:31:09 -0700166}
167
Sage Weil1d3576f2009-10-06 11:31:09 -0700168static int ceph_releasepage(struct page *page, gfp_t g)
169{
NeilBrowne55f1a12016-08-31 12:59:29 +1000170 dout("%p releasepage %p idx %lu (%sdirty)\n", page->mapping->host,
171 page, page->index, PageDirty(page) ? "" : "not ");
Milosz Tanski99ccbd22013-08-21 17:29:54 -0400172
173 /* Can we release the page from the cache? */
174 if (!ceph_release_fscache_page(page, g))
175 return 0;
176
177 return !PagePrivate(page);
Sage Weil1d3576f2009-10-06 11:31:09 -0700178}
179
180/*
181 * read a single page, without unlocking it.
182 */
Yan, Zheng71584922017-08-04 11:22:31 +0800183static int ceph_do_readpage(struct file *filp, struct page *page)
Sage Weil1d3576f2009-10-06 11:31:09 -0700184{
Al Viro496ad9a2013-01-23 17:07:38 -0500185 struct inode *inode = file_inode(filp);
Sage Weil1d3576f2009-10-06 11:31:09 -0700186 struct ceph_inode_info *ci = ceph_inode(inode);
Milosz Tanski99ccbd22013-08-21 17:29:54 -0400187 struct ceph_osd_client *osdc =
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700188 &ceph_inode_to_client(inode)->client->osdc;
Sage Weil1d3576f2009-10-06 11:31:09 -0700189 int err = 0;
Yan, Zheng83701242014-11-14 22:36:18 +0800190 u64 off = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300191 u64 len = PAGE_SIZE;
Sage Weil1d3576f2009-10-06 11:31:09 -0700192
Yan, Zheng83701242014-11-14 22:36:18 +0800193 if (off >= i_size_read(inode)) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300194 zero_user_segment(page, 0, PAGE_SIZE);
Yan, Zheng83701242014-11-14 22:36:18 +0800195 SetPageUptodate(page);
196 return 0;
197 }
Milosz Tanski99ccbd22013-08-21 17:29:54 -0400198
Yan, Zhengfcc02d22015-01-10 11:43:12 +0800199 if (ci->i_inline_version != CEPH_INLINE_NONE) {
200 /*
201 * Uptodate inline data should have been added
202 * into page cache while getting Fcr caps.
203 */
204 if (off == 0)
205 return -EINVAL;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300206 zero_user_segment(page, 0, PAGE_SIZE);
Yan, Zhengfcc02d22015-01-10 11:43:12 +0800207 SetPageUptodate(page);
208 return 0;
209 }
Yan, Zheng83701242014-11-14 22:36:18 +0800210
211 err = ceph_readpage_from_fscache(inode, page);
Milosz Tanski99ccbd22013-08-21 17:29:54 -0400212 if (err == 0)
Yan, Zheng71584922017-08-04 11:22:31 +0800213 return -EINPROGRESS;
Milosz Tanski99ccbd22013-08-21 17:29:54 -0400214
Sage Weil1d3576f2009-10-06 11:31:09 -0700215 dout("readpage inode %p file %p page %p index %lu\n",
216 inode, filp, page, page->index);
217 err = ceph_osdc_readpages(osdc, ceph_vino(inode), &ci->i_layout,
Yan, Zheng83701242014-11-14 22:36:18 +0800218 off, &len,
Sage Weil1d3576f2009-10-06 11:31:09 -0700219 ci->i_truncate_seq, ci->i_truncate_size,
Sage Weilb7495fc2010-11-09 12:43:12 -0800220 &page, 1, 0);
Sage Weil1d3576f2009-10-06 11:31:09 -0700221 if (err == -ENOENT)
222 err = 0;
223 if (err < 0) {
224 SetPageError(page);
Li Wang18302802013-12-19 06:03:49 -0800225 ceph_fscache_readpage_cancel(inode, page);
Sage Weil1d3576f2009-10-06 11:31:09 -0700226 goto out;
Sage Weil1d3576f2009-10-06 11:31:09 -0700227 }
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300228 if (err < PAGE_SIZE)
Zhang Zhen23cd5732014-05-28 15:09:55 +0800229 /* zero fill remainder of page */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300230 zero_user_segment(page, err, PAGE_SIZE);
Zhang Zhen23cd5732014-05-28 15:09:55 +0800231 else
232 flush_dcache_page(page);
Sage Weil1d3576f2009-10-06 11:31:09 -0700233
Zhang Zhen23cd5732014-05-28 15:09:55 +0800234 SetPageUptodate(page);
235 ceph_readpage_to_fscache(inode, page);
Milosz Tanski99ccbd22013-08-21 17:29:54 -0400236
Sage Weil1d3576f2009-10-06 11:31:09 -0700237out:
238 return err < 0 ? err : 0;
239}
240
241static int ceph_readpage(struct file *filp, struct page *page)
242{
Yan, Zheng71584922017-08-04 11:22:31 +0800243 int r = ceph_do_readpage(filp, page);
244 if (r != -EINPROGRESS)
245 unlock_page(page);
246 else
247 r = 0;
Sage Weil1d3576f2009-10-06 11:31:09 -0700248 return r;
249}
250
251/*
Sage Weil7c272192011-08-03 09:58:09 -0700252 * Finish an async read(ahead) op.
Sage Weil1d3576f2009-10-06 11:31:09 -0700253 */
Ilya Dryomov85e084f2016-04-28 16:07:24 +0200254static void finish_read(struct ceph_osd_request *req)
Sage Weil1d3576f2009-10-06 11:31:09 -0700255{
Sage Weil7c272192011-08-03 09:58:09 -0700256 struct inode *inode = req->r_inode;
Alex Elder87060c12013-04-03 01:28:58 -0500257 struct ceph_osd_data *osd_data;
Ilya Dryomov85e084f2016-04-28 16:07:24 +0200258 int rc = req->r_result <= 0 ? req->r_result : 0;
259 int bytes = req->r_result >= 0 ? req->r_result : 0;
Alex Eldere0c59482013-03-07 15:38:25 -0600260 int num_pages;
Sage Weil7c272192011-08-03 09:58:09 -0700261 int i;
262
Sage Weil7c272192011-08-03 09:58:09 -0700263 dout("finish_read %p req %p rc %d bytes %d\n", inode, req, rc, bytes);
264
265 /* unlock all pages, zeroing any data we didn't read */
Alex Elder406e2c92013-04-15 14:50:36 -0500266 osd_data = osd_req_op_extent_osd_data(req, 0);
Alex Elder87060c12013-04-03 01:28:58 -0500267 BUG_ON(osd_data->type != CEPH_OSD_DATA_TYPE_PAGES);
268 num_pages = calc_pages_for((u64)osd_data->alignment,
269 (u64)osd_data->length);
Alex Eldere0c59482013-03-07 15:38:25 -0600270 for (i = 0; i < num_pages; i++) {
Alex Elder87060c12013-04-03 01:28:58 -0500271 struct page *page = osd_data->pages[i];
Sage Weil7c272192011-08-03 09:58:09 -0700272
Yan, Zheng368e3582016-05-17 11:58:02 +0800273 if (rc < 0 && rc != -ENOENT) {
274 ceph_fscache_readpage_cancel(inode, page);
Li Wangf36132a2013-11-27 22:28:13 +0800275 goto unlock;
Yan, Zheng368e3582016-05-17 11:58:02 +0800276 }
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300277 if (bytes < (int)PAGE_SIZE) {
Sage Weil7c272192011-08-03 09:58:09 -0700278 /* zero (remainder of) page */
279 int s = bytes < 0 ? 0 : bytes;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300280 zero_user_segment(page, s, PAGE_SIZE);
Sage Weil7c272192011-08-03 09:58:09 -0700281 }
282 dout("finish_read %p uptodate %p idx %lu\n", inode, page,
283 page->index);
284 flush_dcache_page(page);
285 SetPageUptodate(page);
Milosz Tanski99ccbd22013-08-21 17:29:54 -0400286 ceph_readpage_to_fscache(inode, page);
Li Wangf36132a2013-11-27 22:28:13 +0800287unlock:
Sage Weil7c272192011-08-03 09:58:09 -0700288 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300289 put_page(page);
290 bytes -= PAGE_SIZE;
Sage Weil7c272192011-08-03 09:58:09 -0700291 }
Alex Elder87060c12013-04-03 01:28:58 -0500292 kfree(osd_data->pages);
Sage Weil7c272192011-08-03 09:58:09 -0700293}
294
295/*
296 * start an async read(ahead) operation. return nr_pages we submitted
297 * a read for on success, or negative error code.
298 */
Sage Weil0d66a482011-08-04 08:21:30 -0700299static int start_read(struct inode *inode, struct list_head *page_list, int max)
Sage Weil7c272192011-08-03 09:58:09 -0700300{
301 struct ceph_osd_client *osdc =
302 &ceph_inode_to_client(inode)->client->osdc;
303 struct ceph_inode_info *ci = ceph_inode(inode);
304 struct page *page = list_entry(page_list->prev, struct page, lru);
Alex Elderacead002013-03-14 14:09:05 -0500305 struct ceph_vino vino;
Sage Weil7c272192011-08-03 09:58:09 -0700306 struct ceph_osd_request *req;
307 u64 off;
308 u64 len;
309 int i;
Sage Weil1d3576f2009-10-06 11:31:09 -0700310 struct page **pages;
Sage Weil7c272192011-08-03 09:58:09 -0700311 pgoff_t next_index;
312 int nr_pages = 0;
Yan, Zhenge99b0ea2016-10-25 10:51:55 +0800313 int got = 0;
314 int ret = 0;
315
316 if (!current->journal_info) {
317 /* caller of readpages does not hold buffer and read caps
318 * (fadvise, madvise and readahead cases) */
319 int want = CEPH_CAP_FILE_CACHE;
320 ret = ceph_try_get_caps(ci, CEPH_CAP_FILE_RD, want, &got);
321 if (ret < 0) {
322 dout("start_read %p, error getting cap\n", inode);
323 } else if (!(got & want)) {
324 dout("start_read %p, no cache cap\n", inode);
325 ret = 0;
326 }
327 if (ret <= 0) {
328 if (got)
329 ceph_put_cap_refs(ci, got);
330 while (!list_empty(page_list)) {
331 page = list_entry(page_list->prev,
332 struct page, lru);
333 list_del(&page->lru);
334 put_page(page);
335 }
336 return ret;
337 }
338 }
Sage Weil7c272192011-08-03 09:58:09 -0700339
Alex Elder6285bc22012-10-02 10:25:51 -0500340 off = (u64) page_offset(page);
Sage Weil7c272192011-08-03 09:58:09 -0700341
342 /* count pages */
343 next_index = page->index;
344 list_for_each_entry_reverse(page, page_list, lru) {
345 if (page->index != next_index)
346 break;
347 nr_pages++;
348 next_index++;
Sage Weil0d66a482011-08-04 08:21:30 -0700349 if (max && nr_pages == max)
350 break;
Sage Weil7c272192011-08-03 09:58:09 -0700351 }
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300352 len = nr_pages << PAGE_SHIFT;
Sage Weil7c272192011-08-03 09:58:09 -0700353 dout("start_read %p nr_pages %d is %lld~%lld\n", inode, nr_pages,
354 off, len);
Alex Elderacead002013-03-14 14:09:05 -0500355 vino = ceph_vino(inode);
356 req = ceph_osdc_new_request(osdc, &ci->i_layout, vino, off, &len,
Yan, Zheng715e4cd2014-11-13 14:40:37 +0800357 0, 1, CEPH_OSD_OP_READ,
Alex Elderacead002013-03-14 14:09:05 -0500358 CEPH_OSD_FLAG_READ, NULL,
Sage Weil7c272192011-08-03 09:58:09 -0700359 ci->i_truncate_seq, ci->i_truncate_size,
Alex Elderacead002013-03-14 14:09:05 -0500360 false);
Yan, Zhenge99b0ea2016-10-25 10:51:55 +0800361 if (IS_ERR(req)) {
362 ret = PTR_ERR(req);
363 goto out;
364 }
Sage Weil1d3576f2009-10-06 11:31:09 -0700365
366 /* build page vector */
Alex Eldercf7b7e12013-03-01 18:00:15 -0600367 nr_pages = calc_pages_for(0, len);
Yan, Zheng687265e2015-06-13 17:27:05 +0800368 pages = kmalloc(sizeof(*pages) * nr_pages, GFP_KERNEL);
Yan, Zhenge99b0ea2016-10-25 10:51:55 +0800369 if (!pages) {
370 ret = -ENOMEM;
371 goto out_put;
372 }
Sage Weil7c272192011-08-03 09:58:09 -0700373 for (i = 0; i < nr_pages; ++i) {
374 page = list_entry(page_list->prev, struct page, lru);
375 BUG_ON(PageLocked(page));
376 list_del(&page->lru);
Milosz Tanski99ccbd22013-08-21 17:29:54 -0400377
Sage Weil7c272192011-08-03 09:58:09 -0700378 dout("start_read %p adding %p idx %lu\n", inode, page,
379 page->index);
380 if (add_to_page_cache_lru(page, &inode->i_data, page->index,
Yan, Zheng687265e2015-06-13 17:27:05 +0800381 GFP_KERNEL)) {
Milosz Tanskid4d3aa32013-09-03 19:11:17 -0400382 ceph_fscache_uncache_page(inode, page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300383 put_page(page);
Sage Weil7c272192011-08-03 09:58:09 -0700384 dout("start_read %p add_to_page_cache failed %p\n",
385 inode, page);
386 nr_pages = i;
Yan, Zheng1afe4782016-08-24 11:33:46 +0800387 if (nr_pages > 0) {
388 len = nr_pages << PAGE_SHIFT;
Yan, Zhengdc8470f2017-01-19 11:21:29 +0800389 osd_req_op_extent_update(req, 0, len);
Yan, Zheng1afe4782016-08-24 11:33:46 +0800390 break;
391 }
Sage Weil7c272192011-08-03 09:58:09 -0700392 goto out_pages;
Sage Weil1d3576f2009-10-06 11:31:09 -0700393 }
Sage Weil7c272192011-08-03 09:58:09 -0700394 pages[i] = page;
Sage Weil1d3576f2009-10-06 11:31:09 -0700395 }
Alex Elder406e2c92013-04-15 14:50:36 -0500396 osd_req_op_extent_osd_data_pages(req, 0, pages, len, 0, false, false);
Sage Weil7c272192011-08-03 09:58:09 -0700397 req->r_callback = finish_read;
398 req->r_inode = inode;
399
400 dout("start_read %p starting %p %lld~%lld\n", inode, req, off, len);
401 ret = ceph_osdc_start_request(osdc, req, false);
402 if (ret < 0)
403 goto out_pages;
404 ceph_osdc_put_request(req);
Yan, Zhenge99b0ea2016-10-25 10:51:55 +0800405
406 /* After adding locked pages to page cache, the inode holds cache cap.
407 * So we can drop our cap refs. */
408 if (got)
409 ceph_put_cap_refs(ci, got);
410
Sage Weil7c272192011-08-03 09:58:09 -0700411 return nr_pages;
412
413out_pages:
Yan, Zheng1afe4782016-08-24 11:33:46 +0800414 for (i = 0; i < nr_pages; ++i) {
415 ceph_fscache_readpage_cancel(inode, pages[i]);
416 unlock_page(pages[i]);
417 }
418 ceph_put_page_vector(pages, nr_pages, false);
Yan, Zhenge99b0ea2016-10-25 10:51:55 +0800419out_put:
Sage Weil7c272192011-08-03 09:58:09 -0700420 ceph_osdc_put_request(req);
Yan, Zhenge99b0ea2016-10-25 10:51:55 +0800421out:
422 if (got)
423 ceph_put_cap_refs(ci, got);
Sage Weil7c272192011-08-03 09:58:09 -0700424 return ret;
Sage Weil1d3576f2009-10-06 11:31:09 -0700425}
426
Sage Weil7c272192011-08-03 09:58:09 -0700427
Sage Weil1d3576f2009-10-06 11:31:09 -0700428/*
429 * Read multiple pages. Leave pages we don't read + unlock in page_list;
430 * the caller (VM) cleans them up.
431 */
432static int ceph_readpages(struct file *file, struct address_space *mapping,
433 struct list_head *page_list, unsigned nr_pages)
434{
Al Viro496ad9a2013-01-23 17:07:38 -0500435 struct inode *inode = file_inode(file);
Sage Weil0d66a482011-08-04 08:21:30 -0700436 struct ceph_fs_client *fsc = ceph_inode_to_client(inode);
Sage Weil1d3576f2009-10-06 11:31:09 -0700437 int rc = 0;
Sage Weil0d66a482011-08-04 08:21:30 -0700438 int max = 0;
Sage Weil1d3576f2009-10-06 11:31:09 -0700439
Yan, Zheng83701242014-11-14 22:36:18 +0800440 if (ceph_inode(inode)->i_inline_version != CEPH_INLINE_NONE)
441 return -EINVAL;
442
Milosz Tanski99ccbd22013-08-21 17:29:54 -0400443 rc = ceph_readpages_from_fscache(mapping->host, mapping, page_list,
444 &nr_pages);
445
446 if (rc == 0)
447 goto out;
448
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300449 if (fsc->mount_options->rsize >= PAGE_SIZE)
450 max = (fsc->mount_options->rsize + PAGE_SIZE - 1)
Sage Weil0d66a482011-08-04 08:21:30 -0700451 >> PAGE_SHIFT;
452
Alex Elder2794a822013-02-14 12:16:43 -0600453 dout("readpages %p file %p nr_pages %d max %d\n", inode,
454 file, nr_pages,
Sage Weil0d66a482011-08-04 08:21:30 -0700455 max);
Sage Weil7c272192011-08-03 09:58:09 -0700456 while (!list_empty(page_list)) {
Sage Weil0d66a482011-08-04 08:21:30 -0700457 rc = start_read(inode, page_list, max);
Sage Weil7c272192011-08-03 09:58:09 -0700458 if (rc < 0)
459 goto out;
Sage Weil1d3576f2009-10-06 11:31:09 -0700460 }
Sage Weil1d3576f2009-10-06 11:31:09 -0700461out:
Milosz Tanski76be7782013-08-21 17:30:27 -0400462 ceph_fscache_readpages_cancel(inode, page_list);
463
Sage Weil7c272192011-08-03 09:58:09 -0700464 dout("readpages %p file %p ret %d\n", inode, file, rc);
Sage Weil1d3576f2009-10-06 11:31:09 -0700465 return rc;
466}
467
468/*
469 * Get ref for the oldest snapc for an inode with dirty data... that is, the
470 * only snap context we are allowed to write back.
Sage Weil1d3576f2009-10-06 11:31:09 -0700471 */
Sage Weil6298a332010-03-31 22:01:38 -0700472static struct ceph_snap_context *get_oldest_context(struct inode *inode,
Yan, Zhenge1966b42015-06-18 03:10:58 +0800473 loff_t *snap_size)
Sage Weil1d3576f2009-10-06 11:31:09 -0700474{
475 struct ceph_inode_info *ci = ceph_inode(inode);
476 struct ceph_snap_context *snapc = NULL;
477 struct ceph_cap_snap *capsnap = NULL;
478
Sage Weilbe655592011-11-30 09:47:09 -0800479 spin_lock(&ci->i_ceph_lock);
Sage Weil1d3576f2009-10-06 11:31:09 -0700480 list_for_each_entry(capsnap, &ci->i_cap_snaps, ci_item) {
481 dout(" cap_snap %p snapc %p has %d dirty pages\n", capsnap,
482 capsnap->context, capsnap->dirty_pages);
483 if (capsnap->dirty_pages) {
484 snapc = ceph_get_snap_context(capsnap->context);
485 if (snap_size)
486 *snap_size = capsnap->size;
487 break;
488 }
489 }
Sage Weil7d8cb262010-08-24 08:44:16 -0700490 if (!snapc && ci->i_wrbuffer_ref_head) {
Sage Weil80e755f2010-03-31 21:52:10 -0700491 snapc = ceph_get_snap_context(ci->i_head_snapc);
Sage Weil1d3576f2009-10-06 11:31:09 -0700492 dout(" head snapc %p has %d dirty pages\n",
493 snapc, ci->i_wrbuffer_ref_head);
494 }
Sage Weilbe655592011-11-30 09:47:09 -0800495 spin_unlock(&ci->i_ceph_lock);
Sage Weil1d3576f2009-10-06 11:31:09 -0700496 return snapc;
497}
498
499/*
500 * Write a single page, but leave the page locked.
501 *
502 * If we get a write error, set the page error bit, but still adjust the
503 * dirty page accounting (i.e., page is no longer dirty).
504 */
505static int writepage_nounlock(struct page *page, struct writeback_control *wbc)
506{
507 struct inode *inode;
508 struct ceph_inode_info *ci;
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700509 struct ceph_fs_client *fsc;
Sage Weil1d3576f2009-10-06 11:31:09 -0700510 struct ceph_osd_client *osdc;
Sage Weil6298a332010-03-31 22:01:38 -0700511 struct ceph_snap_context *snapc, *oldest;
Yan, Zhengfc2744a2013-05-31 16:48:29 +0800512 loff_t page_off = page_offset(page);
Yan, Zhenge1966b42015-06-18 03:10:58 +0800513 loff_t snap_size = -1;
Yehuda Sadeh2baba252009-12-18 13:51:57 -0800514 long writeback_stat;
Yan, Zhenge1966b42015-06-18 03:10:58 +0800515 u64 truncate_size;
Yan, Zhengfc2744a2013-05-31 16:48:29 +0800516 u32 truncate_seq;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300517 int err = 0, len = PAGE_SIZE;
Sage Weil1d3576f2009-10-06 11:31:09 -0700518
519 dout("writepage %p idx %lu\n", page, page->index);
520
521 if (!page->mapping || !page->mapping->host) {
522 dout("writepage %p - no mapping\n", page);
523 return -EFAULT;
524 }
525 inode = page->mapping->host;
526 ci = ceph_inode(inode);
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700527 fsc = ceph_inode_to_client(inode);
528 osdc = &fsc->client->osdc;
Sage Weil1d3576f2009-10-06 11:31:09 -0700529
530 /* verify this is a writeable snap context */
Yan, Zheng61600ef2012-05-28 14:44:30 +0800531 snapc = page_snap_context(page);
Sage Weil1d3576f2009-10-06 11:31:09 -0700532 if (snapc == NULL) {
533 dout("writepage %p page %p not dirty?\n", inode, page);
534 goto out;
535 }
Sage Weil6298a332010-03-31 22:01:38 -0700536 oldest = get_oldest_context(inode, &snap_size);
537 if (snapc->seq > oldest->seq) {
Sage Weil1d3576f2009-10-06 11:31:09 -0700538 dout("writepage %p page %p snapc %p not writeable - noop\n",
Yan, Zheng61600ef2012-05-28 14:44:30 +0800539 inode, page, snapc);
Sage Weil1d3576f2009-10-06 11:31:09 -0700540 /* we should only noop if called by kswapd */
541 WARN_ON((current->flags & PF_MEMALLOC) == 0);
Sage Weil6298a332010-03-31 22:01:38 -0700542 ceph_put_snap_context(oldest);
Sage Weil1d3576f2009-10-06 11:31:09 -0700543 goto out;
544 }
Sage Weil6298a332010-03-31 22:01:38 -0700545 ceph_put_snap_context(oldest);
Sage Weil1d3576f2009-10-06 11:31:09 -0700546
Yan, Zhengfc2744a2013-05-31 16:48:29 +0800547 spin_lock(&ci->i_ceph_lock);
548 truncate_seq = ci->i_truncate_seq;
549 truncate_size = ci->i_truncate_size;
Yan, Zhenge1966b42015-06-18 03:10:58 +0800550 if (snap_size == -1)
Yan, Zhengfc2744a2013-05-31 16:48:29 +0800551 snap_size = i_size_read(inode);
552 spin_unlock(&ci->i_ceph_lock);
553
Sage Weil1d3576f2009-10-06 11:31:09 -0700554 /* is this a partial page at end of file? */
Yan, Zhengfc2744a2013-05-31 16:48:29 +0800555 if (page_off >= snap_size) {
556 dout("%p page eof %llu\n", page, snap_size);
557 goto out;
558 }
559 if (snap_size < page_off + len)
560 len = snap_size - page_off;
Sage Weil1d3576f2009-10-06 11:31:09 -0700561
Sage Weilae00d4f2010-09-16 16:26:51 -0700562 dout("writepage %p page %p index %lu on %llu~%u snapc %p\n",
563 inode, page, page->index, page_off, len, snapc);
Sage Weil1d3576f2009-10-06 11:31:09 -0700564
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700565 writeback_stat = atomic_long_inc_return(&fsc->writeback_count);
Yehuda Sadeh2baba252009-12-18 13:51:57 -0800566 if (writeback_stat >
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700567 CONGESTION_ON_THRESH(fsc->mount_options->congestion_kb))
568 set_bdi_congested(&fsc->backing_dev_info, BLK_RW_ASYNC);
Yehuda Sadeh2baba252009-12-18 13:51:57 -0800569
Sage Weil1d3576f2009-10-06 11:31:09 -0700570 set_page_writeback(page);
571 err = ceph_osdc_writepages(osdc, ceph_vino(inode),
572 &ci->i_layout, snapc,
573 page_off, len,
Yan, Zhengfc2744a2013-05-31 16:48:29 +0800574 truncate_seq, truncate_size,
Alex Elder24808822013-02-15 11:42:29 -0600575 &inode->i_mtime, &page, 1);
Sage Weil1d3576f2009-10-06 11:31:09 -0700576 if (err < 0) {
Yan, Zhengad15ec02016-05-13 17:29:51 +0800577 struct writeback_control tmp_wbc;
578 if (!wbc)
579 wbc = &tmp_wbc;
580 if (err == -ERESTARTSYS) {
581 /* killed by SIGKILL */
582 dout("writepage interrupted page %p\n", page);
583 redirty_page_for_writepage(wbc, page);
584 end_page_writeback(page);
585 goto out;
586 }
587 dout("writepage setting page/mapping error %d %p\n",
588 err, page);
Sage Weil1d3576f2009-10-06 11:31:09 -0700589 SetPageError(page);
590 mapping_set_error(&inode->i_data, err);
Yan, Zhengad15ec02016-05-13 17:29:51 +0800591 wbc->pages_skipped++;
Sage Weil1d3576f2009-10-06 11:31:09 -0700592 } else {
593 dout("writepage cleaned page %p\n", page);
594 err = 0; /* vfs expects us to return 0 */
595 }
596 page->private = 0;
597 ClearPagePrivate(page);
598 end_page_writeback(page);
599 ceph_put_wrbuffer_cap_refs(ci, 1, snapc);
Sage Weil6298a332010-03-31 22:01:38 -0700600 ceph_put_snap_context(snapc); /* page's reference */
Sage Weil1d3576f2009-10-06 11:31:09 -0700601out:
602 return err;
603}
604
605static int ceph_writepage(struct page *page, struct writeback_control *wbc)
606{
Yehuda Sadehdbd646a2009-12-16 14:51:06 -0800607 int err;
608 struct inode *inode = page->mapping->host;
609 BUG_ON(!inode);
Sage Weil70b666c2011-05-27 09:24:26 -0700610 ihold(inode);
Yehuda Sadehdbd646a2009-12-16 14:51:06 -0800611 err = writepage_nounlock(page, wbc);
Yan, Zhengad15ec02016-05-13 17:29:51 +0800612 if (err == -ERESTARTSYS) {
613 /* direct memory reclaimer was killed by SIGKILL. return 0
614 * to prevent caller from setting mapping/page error */
615 err = 0;
616 }
Sage Weil1d3576f2009-10-06 11:31:09 -0700617 unlock_page(page);
Yehuda Sadehdbd646a2009-12-16 14:51:06 -0800618 iput(inode);
Sage Weil1d3576f2009-10-06 11:31:09 -0700619 return err;
620}
621
Sage Weil1d3576f2009-10-06 11:31:09 -0700622/*
623 * lame release_pages helper. release_pages() isn't exported to
624 * modules.
625 */
626static void ceph_release_pages(struct page **pages, int num)
627{
628 struct pagevec pvec;
629 int i;
630
631 pagevec_init(&pvec, 0);
632 for (i = 0; i < num; i++) {
633 if (pagevec_add(&pvec, pages[i]) == 0)
634 pagevec_release(&pvec);
635 }
636 pagevec_release(&pvec);
637}
638
Sage Weil1d3576f2009-10-06 11:31:09 -0700639/*
640 * async writeback completion handler.
641 *
642 * If we get an error, set the mapping error bit, but not the individual
643 * page error bits.
644 */
Ilya Dryomov85e084f2016-04-28 16:07:24 +0200645static void writepages_finish(struct ceph_osd_request *req)
Sage Weil1d3576f2009-10-06 11:31:09 -0700646{
647 struct inode *inode = req->r_inode;
Sage Weil1d3576f2009-10-06 11:31:09 -0700648 struct ceph_inode_info *ci = ceph_inode(inode);
Alex Elder87060c12013-04-03 01:28:58 -0500649 struct ceph_osd_data *osd_data;
Sage Weil1d3576f2009-10-06 11:31:09 -0700650 struct page *page;
Yan, Zheng5b646402016-01-07 16:00:17 +0800651 int num_pages, total_pages = 0;
652 int i, j;
653 int rc = req->r_result;
Sage Weil1d3576f2009-10-06 11:31:09 -0700654 struct ceph_snap_context *snapc = req->r_snapc;
655 struct address_space *mapping = inode->i_mapping;
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700656 struct ceph_fs_client *fsc = ceph_inode_to_client(inode);
Yan, Zheng5b646402016-01-07 16:00:17 +0800657 bool remove_page;
Sage Weil1d3576f2009-10-06 11:31:09 -0700658
Yan, Zheng5b646402016-01-07 16:00:17 +0800659 dout("writepages_finish %p rc %d\n", inode, rc);
660 if (rc < 0)
Sage Weil1d3576f2009-10-06 11:31:09 -0700661 mapping_set_error(mapping, rc);
Yan, Zheng5b646402016-01-07 16:00:17 +0800662
663 /*
664 * We lost the cache cap, need to truncate the page before
665 * it is unlocked, otherwise we'd truncate it later in the
666 * page truncation thread, possibly losing some data that
667 * raced its way in
668 */
669 remove_page = !(ceph_caps_issued(ci) &
670 (CEPH_CAP_FILE_CACHE|CEPH_CAP_FILE_LAZYIO));
Sage Weil1d3576f2009-10-06 11:31:09 -0700671
672 /* clean all pages */
Yan, Zheng5b646402016-01-07 16:00:17 +0800673 for (i = 0; i < req->r_num_ops; i++) {
674 if (req->r_ops[i].op != CEPH_OSD_OP_WRITE)
675 break;
Sage Weil1d3576f2009-10-06 11:31:09 -0700676
Yan, Zheng5b646402016-01-07 16:00:17 +0800677 osd_data = osd_req_op_extent_osd_data(req, i);
678 BUG_ON(osd_data->type != CEPH_OSD_DATA_TYPE_PAGES);
679 num_pages = calc_pages_for((u64)osd_data->alignment,
680 (u64)osd_data->length);
681 total_pages += num_pages;
682 for (j = 0; j < num_pages; j++) {
683 page = osd_data->pages[j];
684 BUG_ON(!page);
685 WARN_ON(!PageUptodate(page));
Yehuda Sadeh2baba252009-12-18 13:51:57 -0800686
Yan, Zheng5b646402016-01-07 16:00:17 +0800687 if (atomic_long_dec_return(&fsc->writeback_count) <
688 CONGESTION_OFF_THRESH(
689 fsc->mount_options->congestion_kb))
690 clear_bdi_congested(&fsc->backing_dev_info,
691 BLK_RW_ASYNC);
Yehuda Sadehe63dc5c2010-02-19 00:07:01 +0000692
Yan, Zhengb109eec62016-05-13 17:54:17 +0800693 if (rc < 0)
694 SetPageError(page);
695
Yan, Zheng5b646402016-01-07 16:00:17 +0800696 ceph_put_snap_context(page_snap_context(page));
697 page->private = 0;
698 ClearPagePrivate(page);
699 dout("unlocking %p\n", page);
700 end_page_writeback(page);
Yehuda Sadehe63dc5c2010-02-19 00:07:01 +0000701
Yan, Zheng5b646402016-01-07 16:00:17 +0800702 if (remove_page)
703 generic_error_remove_page(inode->i_mapping,
704 page);
705
706 unlock_page(page);
707 }
708 dout("writepages_finish %p wrote %llu bytes cleaned %d pages\n",
709 inode, osd_data->length, rc >= 0 ? num_pages : 0);
710
711 ceph_release_pages(osd_data->pages, num_pages);
Sage Weil1d3576f2009-10-06 11:31:09 -0700712 }
Sage Weil1d3576f2009-10-06 11:31:09 -0700713
Yan, Zheng5b646402016-01-07 16:00:17 +0800714 ceph_put_wrbuffer_cap_refs(ci, total_pages, snapc);
715
716 osd_data = osd_req_op_extent_osd_data(req, 0);
Alex Elder87060c12013-04-03 01:28:58 -0500717 if (osd_data->pages_from_pool)
718 mempool_free(osd_data->pages,
Cheng Renquan640ef792010-03-26 17:40:33 +0800719 ceph_sb_to_client(inode->i_sb)->wb_pagevec_pool);
Sage Weil1d3576f2009-10-06 11:31:09 -0700720 else
Alex Elder87060c12013-04-03 01:28:58 -0500721 kfree(osd_data->pages);
Sage Weil1d3576f2009-10-06 11:31:09 -0700722 ceph_osdc_put_request(req);
723}
724
Sage Weil1d3576f2009-10-06 11:31:09 -0700725/*
726 * initiate async writeback
727 */
728static int ceph_writepages_start(struct address_space *mapping,
729 struct writeback_control *wbc)
730{
731 struct inode *inode = mapping->host;
Sage Weil1d3576f2009-10-06 11:31:09 -0700732 struct ceph_inode_info *ci = ceph_inode(inode);
Yan, Zhengfc2744a2013-05-31 16:48:29 +0800733 struct ceph_fs_client *fsc = ceph_inode_to_client(inode);
734 struct ceph_vino vino = ceph_vino(inode);
Sage Weil1d3576f2009-10-06 11:31:09 -0700735 pgoff_t index, start, end;
736 int range_whole = 0;
737 int should_loop = 1;
738 pgoff_t max_pages = 0, max_pages_ever = 0;
Sage Weil80e755f2010-03-31 21:52:10 -0700739 struct ceph_snap_context *snapc = NULL, *last_snapc = NULL, *pgsnapc;
Sage Weil1d3576f2009-10-06 11:31:09 -0700740 struct pagevec pvec;
741 int done = 0;
742 int rc = 0;
Fabian Frederick61604a22017-02-27 14:28:32 -0800743 unsigned int wsize = i_blocksize(inode);
Sage Weil1d3576f2009-10-06 11:31:09 -0700744 struct ceph_osd_request *req = NULL;
Dan Carpenter021b77b2014-11-28 11:33:34 +0300745 int do_sync = 0;
Yan, Zhenge1966b42015-06-18 03:10:58 +0800746 loff_t snap_size, i_size;
747 u64 truncate_size;
Yan, Zhengfc2744a2013-05-31 16:48:29 +0800748 u32 truncate_seq;
Sage Weil1d3576f2009-10-06 11:31:09 -0700749
750 /*
751 * Include a 'sync' in the OSD request if this is a data
752 * integrity write (e.g., O_SYNC write or fsync()), or if our
753 * cap is being revoked.
754 */
majianpengc62988e2013-06-19 15:12:06 +0800755 if ((wbc->sync_mode == WB_SYNC_ALL) ||
756 ceph_caps_revoking(ci, CEPH_CAP_FILE_BUFFER))
Sage Weil1d3576f2009-10-06 11:31:09 -0700757 do_sync = 1;
758 dout("writepages_start %p dosync=%d (mode=%s)\n",
759 inode, do_sync,
760 wbc->sync_mode == WB_SYNC_NONE ? "NONE" :
761 (wbc->sync_mode == WB_SYNC_ALL ? "ALL" : "HOLD"));
762
Yan, Zheng48fec5d2015-07-01 16:27:46 +0800763 if (ACCESS_ONCE(fsc->mount_state) == CEPH_MOUNT_SHUTDOWN) {
Yan, Zheng6c93df52016-04-15 13:56:12 +0800764 if (ci->i_wrbuffer_ref > 0) {
765 pr_warn_ratelimited(
766 "writepage_start %p %lld forced umount\n",
767 inode, ceph_ino(inode));
768 }
Yan, Zhenga341d4d2015-07-01 17:03:23 +0800769 mapping_set_error(mapping, -EIO);
Sage Weil1d3576f2009-10-06 11:31:09 -0700770 return -EIO; /* we're in a forced umount, don't write! */
771 }
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700772 if (fsc->mount_options->wsize && fsc->mount_options->wsize < wsize)
773 wsize = fsc->mount_options->wsize;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300774 if (wsize < PAGE_SIZE)
775 wsize = PAGE_SIZE;
776 max_pages_ever = wsize >> PAGE_SHIFT;
Sage Weil1d3576f2009-10-06 11:31:09 -0700777
778 pagevec_init(&pvec, 0);
779
Sage Weil1d3576f2009-10-06 11:31:09 -0700780 /* where to start/end? */
781 if (wbc->range_cyclic) {
782 start = mapping->writeback_index; /* Start from prev offset */
783 end = -1;
784 dout(" cyclic, start at %lu\n", start);
785 } else {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300786 start = wbc->range_start >> PAGE_SHIFT;
787 end = wbc->range_end >> PAGE_SHIFT;
Sage Weil1d3576f2009-10-06 11:31:09 -0700788 if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX)
789 range_whole = 1;
790 should_loop = 0;
791 dout(" not cyclic, %lu to %lu\n", start, end);
792 }
793 index = start;
794
795retry:
796 /* find oldest snap context with dirty data */
797 ceph_put_snap_context(snapc);
Yan, Zhenge1966b42015-06-18 03:10:58 +0800798 snap_size = -1;
Sage Weil1d3576f2009-10-06 11:31:09 -0700799 snapc = get_oldest_context(inode, &snap_size);
800 if (!snapc) {
801 /* hmm, why does writepages get called when there
802 is no dirty data? */
803 dout(" no snap context with dirty data?\n");
804 goto out;
805 }
806 dout(" oldest snapc is %p seq %lld (%d snaps)\n",
807 snapc, snapc->seq, snapc->num_snaps);
Yan, Zhengfc2744a2013-05-31 16:48:29 +0800808
809 spin_lock(&ci->i_ceph_lock);
810 truncate_seq = ci->i_truncate_seq;
811 truncate_size = ci->i_truncate_size;
Yan, Zhenge1966b42015-06-18 03:10:58 +0800812 i_size = i_size_read(inode);
Yan, Zhengfc2744a2013-05-31 16:48:29 +0800813 spin_unlock(&ci->i_ceph_lock);
814
Sage Weil1d3576f2009-10-06 11:31:09 -0700815 if (last_snapc && snapc != last_snapc) {
816 /* if we switched to a newer snapc, restart our scan at the
817 * start of the original file range. */
818 dout(" snapc differs from last pass, restarting at %lu\n",
819 index);
820 index = start;
821 }
822 last_snapc = snapc;
823
824 while (!done && index <= end) {
825 unsigned i;
826 int first;
Yan, Zheng5b646402016-01-07 16:00:17 +0800827 pgoff_t strip_unit_end = 0;
828 int num_ops = 0, op_idx;
829 int pvec_pages, locked_pages = 0;
830 struct page **pages = NULL, **data_pages;
Alex Eldere5975c72013-03-14 14:09:05 -0500831 mempool_t *pool = NULL; /* Becomes non-null if mempool used */
Sage Weil1d3576f2009-10-06 11:31:09 -0700832 struct page *page;
Yan, Zheng5b646402016-01-07 16:00:17 +0800833 u64 offset = 0, len = 0;
Sage Weil1d3576f2009-10-06 11:31:09 -0700834
Sage Weil1d3576f2009-10-06 11:31:09 -0700835 max_pages = max_pages_ever;
836
837get_more_pages:
838 first = -1;
Jan Karab4331512017-11-15 17:34:41 -0800839 pvec_pages = pagevec_lookup_range_tag(&pvec, mapping, &index,
Jan Kara4d93a432017-11-15 17:35:19 -0800840 end, PAGECACHE_TAG_DIRTY);
Jan Karab4331512017-11-15 17:34:41 -0800841 dout("pagevec_lookup_range_tag got %d\n", pvec_pages);
Sage Weil1d3576f2009-10-06 11:31:09 -0700842 if (!pvec_pages && !locked_pages)
843 break;
844 for (i = 0; i < pvec_pages && locked_pages < max_pages; i++) {
845 page = pvec.pages[i];
846 dout("? %p idx %lu\n", page, page->index);
847 if (locked_pages == 0)
848 lock_page(page); /* first page */
849 else if (!trylock_page(page))
850 break;
851
852 /* only dirty pages, or our accounting breaks */
853 if (unlikely(!PageDirty(page)) ||
854 unlikely(page->mapping != mapping)) {
855 dout("!dirty or !mapping %p\n", page);
856 unlock_page(page);
857 break;
858 }
Yan, Zheng5b646402016-01-07 16:00:17 +0800859 if (strip_unit_end && (page->index > strip_unit_end)) {
860 dout("end of strip unit %p\n", page);
Sage Weil1d3576f2009-10-06 11:31:09 -0700861 unlock_page(page);
862 break;
863 }
864 if (wbc->sync_mode != WB_SYNC_NONE) {
865 dout("waiting on writeback %p\n", page);
866 wait_on_page_writeback(page);
867 }
Yan, Zhenge1966b42015-06-18 03:10:58 +0800868 if (page_offset(page) >=
869 (snap_size == -1 ? i_size : snap_size)) {
870 dout("%p page eof %llu\n", page,
871 (snap_size == -1 ? i_size : snap_size));
Sage Weil1d3576f2009-10-06 11:31:09 -0700872 done = 1;
873 unlock_page(page);
874 break;
875 }
876 if (PageWriteback(page)) {
877 dout("%p under writeback\n", page);
878 unlock_page(page);
879 break;
880 }
881
882 /* only if matching snap context */
Yan, Zheng61600ef2012-05-28 14:44:30 +0800883 pgsnapc = page_snap_context(page);
Sage Weil80e755f2010-03-31 21:52:10 -0700884 if (pgsnapc->seq > snapc->seq) {
885 dout("page snapc %p %lld > oldest %p %lld\n",
886 pgsnapc, pgsnapc->seq, snapc, snapc->seq);
Sage Weil1d3576f2009-10-06 11:31:09 -0700887 unlock_page(page);
888 if (!locked_pages)
889 continue; /* keep looking for snap */
890 break;
891 }
892
893 if (!clear_page_dirty_for_io(page)) {
894 dout("%p !clear_page_dirty_for_io\n", page);
895 unlock_page(page);
896 break;
897 }
898
Alex Eldere5975c72013-03-14 14:09:05 -0500899 /*
900 * We have something to write. If this is
901 * the first locked page this time through,
Yan, Zheng5b646402016-01-07 16:00:17 +0800902 * calculate max possinle write size and
903 * allocate a page array
Alex Eldere5975c72013-03-14 14:09:05 -0500904 */
Sage Weil1d3576f2009-10-06 11:31:09 -0700905 if (locked_pages == 0) {
Yan, Zheng5b646402016-01-07 16:00:17 +0800906 u64 objnum;
907 u64 objoff;
908
Sage Weil1d3576f2009-10-06 11:31:09 -0700909 /* prepare async write request */
Alex Eldere5975c72013-03-14 14:09:05 -0500910 offset = (u64)page_offset(page);
Sage Weil1d3576f2009-10-06 11:31:09 -0700911 len = wsize;
Yan, Zheng5b646402016-01-07 16:00:17 +0800912
913 rc = ceph_calc_file_object_mapping(&ci->i_layout,
914 offset, len,
915 &objnum, &objoff,
916 &len);
917 if (rc < 0) {
Henry C Chang8c718972011-05-03 09:45:16 +0000918 unlock_page(page);
919 break;
920 }
921
Yan, Zheng5b646402016-01-07 16:00:17 +0800922 num_ops = 1 + do_sync;
923 strip_unit_end = page->index +
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300924 ((len - 1) >> PAGE_SHIFT);
Yan, Zheng715e4cd2014-11-13 14:40:37 +0800925
Yan, Zheng5b646402016-01-07 16:00:17 +0800926 BUG_ON(pages);
Alex Elder88486952013-03-14 14:09:05 -0500927 max_pages = calc_pages_for(0, (u64)len);
Yan, Zhengfc2744a2013-05-31 16:48:29 +0800928 pages = kmalloc(max_pages * sizeof (*pages),
929 GFP_NOFS);
Alex Elder88486952013-03-14 14:09:05 -0500930 if (!pages) {
931 pool = fsc->wb_pagevec_pool;
Alex Elder88486952013-03-14 14:09:05 -0500932 pages = mempool_alloc(pool, GFP_NOFS);
Alex Eldere5975c72013-03-14 14:09:05 -0500933 BUG_ON(!pages);
Alex Elder88486952013-03-14 14:09:05 -0500934 }
Yan, Zheng5b646402016-01-07 16:00:17 +0800935
936 len = 0;
937 } else if (page->index !=
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300938 (offset + len) >> PAGE_SHIFT) {
Yan, Zheng5b646402016-01-07 16:00:17 +0800939 if (num_ops >= (pool ? CEPH_OSD_SLAB_OPS :
940 CEPH_OSD_MAX_OPS)) {
941 redirty_page_for_writepage(wbc, page);
942 unlock_page(page);
943 break;
944 }
945
946 num_ops++;
947 offset = (u64)page_offset(page);
948 len = 0;
Sage Weil1d3576f2009-10-06 11:31:09 -0700949 }
950
951 /* note position of first page in pvec */
952 if (first < 0)
953 first = i;
954 dout("%p will write page %p idx %lu\n",
955 inode, page, page->index);
Yehuda Sadeh2baba252009-12-18 13:51:57 -0800956
Yan, Zheng5b646402016-01-07 16:00:17 +0800957 if (atomic_long_inc_return(&fsc->writeback_count) >
958 CONGESTION_ON_THRESH(
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700959 fsc->mount_options->congestion_kb)) {
960 set_bdi_congested(&fsc->backing_dev_info,
Sage Weil213c99e2010-08-03 10:25:11 -0700961 BLK_RW_ASYNC);
Yehuda Sadeh2baba252009-12-18 13:51:57 -0800962 }
963
Alex Eldere5975c72013-03-14 14:09:05 -0500964 pages[locked_pages] = page;
Sage Weil1d3576f2009-10-06 11:31:09 -0700965 locked_pages++;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300966 len += PAGE_SIZE;
Sage Weil1d3576f2009-10-06 11:31:09 -0700967 }
968
969 /* did we get anything? */
970 if (!locked_pages)
971 goto release_pvec_pages;
972 if (i) {
973 int j;
974 BUG_ON(!locked_pages || first < 0);
975
976 if (pvec_pages && i == pvec_pages &&
977 locked_pages < max_pages) {
978 dout("reached end pvec, trying for more\n");
979 pagevec_reinit(&pvec);
980 goto get_more_pages;
981 }
982
983 /* shift unused pages over in the pvec... we
984 * will need to release them below. */
985 for (j = i; j < pvec_pages; j++) {
Yan, Zheng5b646402016-01-07 16:00:17 +0800986 dout(" pvec leftover page %p\n", pvec.pages[j]);
Sage Weil1d3576f2009-10-06 11:31:09 -0700987 pvec.pages[j-i+first] = pvec.pages[j];
988 }
989 pvec.nr -= i-first;
990 }
991
Yan, Zheng5b646402016-01-07 16:00:17 +0800992new_request:
Alex Eldere5975c72013-03-14 14:09:05 -0500993 offset = page_offset(pages[0]);
Yan, Zheng5b646402016-01-07 16:00:17 +0800994 len = wsize;
995
996 req = ceph_osdc_new_request(&fsc->client->osdc,
997 &ci->i_layout, vino,
998 offset, &len, 0, num_ops,
999 CEPH_OSD_OP_WRITE,
1000 CEPH_OSD_FLAG_WRITE |
1001 CEPH_OSD_FLAG_ONDISK,
1002 snapc, truncate_seq,
1003 truncate_size, false);
1004 if (IS_ERR(req)) {
1005 req = ceph_osdc_new_request(&fsc->client->osdc,
1006 &ci->i_layout, vino,
1007 offset, &len, 0,
1008 min(num_ops,
1009 CEPH_OSD_SLAB_OPS),
1010 CEPH_OSD_OP_WRITE,
1011 CEPH_OSD_FLAG_WRITE |
1012 CEPH_OSD_FLAG_ONDISK,
1013 snapc, truncate_seq,
1014 truncate_size, true);
1015 BUG_ON(IS_ERR(req));
Yan, Zhenge1966b42015-06-18 03:10:58 +08001016 }
Yan, Zheng5b646402016-01-07 16:00:17 +08001017 BUG_ON(len < page_offset(pages[locked_pages - 1]) +
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001018 PAGE_SIZE - offset);
Sage Weil1d3576f2009-10-06 11:31:09 -07001019
Yan, Zheng5b646402016-01-07 16:00:17 +08001020 req->r_callback = writepages_finish;
1021 req->r_inode = inode;
1022
1023 /* Format the osd request message and submit the write */
1024 len = 0;
1025 data_pages = pages;
1026 op_idx = 0;
1027 for (i = 0; i < locked_pages; i++) {
1028 u64 cur_offset = page_offset(pages[i]);
1029 if (offset + len != cur_offset) {
1030 if (op_idx + do_sync + 1 == req->r_num_ops)
1031 break;
1032 osd_req_op_extent_dup_last(req, op_idx,
1033 cur_offset - offset);
1034 dout("writepages got pages at %llu~%llu\n",
1035 offset, len);
1036 osd_req_op_extent_osd_data_pages(req, op_idx,
1037 data_pages, len, 0,
Alex Eldera4ce40a2013-04-05 01:27:12 -05001038 !!pool, false);
Yan, Zheng5b646402016-01-07 16:00:17 +08001039 osd_req_op_extent_update(req, op_idx, len);
Alex Eldere5975c72013-03-14 14:09:05 -05001040
Yan, Zheng5b646402016-01-07 16:00:17 +08001041 len = 0;
1042 offset = cur_offset;
1043 data_pages = pages + i;
1044 op_idx++;
1045 }
1046
1047 set_page_writeback(pages[i]);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001048 len += PAGE_SIZE;
Yan, Zheng5b646402016-01-07 16:00:17 +08001049 }
1050
1051 if (snap_size != -1) {
1052 len = min(len, snap_size - offset);
1053 } else if (i == locked_pages) {
1054 /* writepages_finish() clears writeback pages
1055 * according to the data length, so make sure
1056 * data length covers all locked pages */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001057 u64 min_len = len + 1 - PAGE_SIZE;
Yan, Zheng5b646402016-01-07 16:00:17 +08001058 len = min(len, (u64)i_size_read(inode) - offset);
1059 len = max(len, min_len);
1060 }
1061 dout("writepages got pages at %llu~%llu\n", offset, len);
1062
1063 osd_req_op_extent_osd_data_pages(req, op_idx, data_pages, len,
1064 0, !!pool, false);
1065 osd_req_op_extent_update(req, op_idx, len);
1066
1067 if (do_sync) {
1068 op_idx++;
1069 osd_req_op_init(req, op_idx, CEPH_OSD_OP_STARTSYNC, 0);
1070 }
1071 BUG_ON(op_idx + 1 != req->r_num_ops);
1072
Alex Eldere5975c72013-03-14 14:09:05 -05001073 pool = NULL;
Yan, Zheng5b646402016-01-07 16:00:17 +08001074 if (i < locked_pages) {
1075 BUG_ON(num_ops <= req->r_num_ops);
1076 num_ops -= req->r_num_ops;
1077 num_ops += do_sync;
1078 locked_pages -= i;
Alex Eldere5975c72013-03-14 14:09:05 -05001079
Yan, Zheng5b646402016-01-07 16:00:17 +08001080 /* allocate new pages array for next request */
1081 data_pages = pages;
1082 pages = kmalloc(locked_pages * sizeof (*pages),
1083 GFP_NOFS);
1084 if (!pages) {
1085 pool = fsc->wb_pagevec_pool;
1086 pages = mempool_alloc(pool, GFP_NOFS);
1087 BUG_ON(!pages);
1088 }
1089 memcpy(pages, data_pages + i,
1090 locked_pages * sizeof(*pages));
1091 memset(data_pages + i, 0,
1092 locked_pages * sizeof(*pages));
1093 } else {
1094 BUG_ON(num_ops != req->r_num_ops);
1095 index = pages[i - 1]->index + 1;
1096 /* request message now owns the pages array */
1097 pages = NULL;
1098 }
Alex Eldere5975c72013-03-14 14:09:05 -05001099
Ilya Dryomovbb873b52016-05-26 00:29:52 +02001100 req->r_mtime = inode->i_mtime;
Sage Weil9d6fcb02011-05-12 15:48:16 -07001101 rc = ceph_osdc_start_request(&fsc->client->osdc, req, true);
1102 BUG_ON(rc);
Sage Weil1d3576f2009-10-06 11:31:09 -07001103 req = NULL;
1104
Yan, Zheng5b646402016-01-07 16:00:17 +08001105 wbc->nr_to_write -= i;
1106 if (pages)
1107 goto new_request;
1108
Sage Weil1d3576f2009-10-06 11:31:09 -07001109 if (wbc->nr_to_write <= 0)
1110 done = 1;
1111
1112release_pvec_pages:
1113 dout("pagevec_release on %d pages (%p)\n", (int)pvec.nr,
1114 pvec.nr ? pvec.pages[0] : NULL);
1115 pagevec_release(&pvec);
1116
1117 if (locked_pages && !done)
1118 goto retry;
1119 }
1120
1121 if (should_loop && !done) {
1122 /* more to do; loop back to beginning of file */
1123 dout("writepages looping back to beginning of file\n");
1124 should_loop = 0;
1125 index = 0;
1126 goto retry;
1127 }
1128
1129 if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0))
1130 mapping->writeback_index = index;
1131
1132out:
Ilya Dryomov3ed97d62016-04-26 15:05:29 +02001133 ceph_osdc_put_request(req);
Sage Weil1d3576f2009-10-06 11:31:09 -07001134 ceph_put_snap_context(snapc);
1135 dout("writepages done, rc = %d\n", rc);
Sage Weil1d3576f2009-10-06 11:31:09 -07001136 return rc;
1137}
1138
1139
1140
1141/*
1142 * See if a given @snapc is either writeable, or already written.
1143 */
1144static int context_is_writeable_or_written(struct inode *inode,
1145 struct ceph_snap_context *snapc)
1146{
1147 struct ceph_snap_context *oldest = get_oldest_context(inode, NULL);
Sage Weil6298a332010-03-31 22:01:38 -07001148 int ret = !oldest || snapc->seq <= oldest->seq;
1149
1150 ceph_put_snap_context(oldest);
1151 return ret;
Sage Weil1d3576f2009-10-06 11:31:09 -07001152}
1153
1154/*
1155 * We are only allowed to write into/dirty the page if the page is
1156 * clean, or already dirty within the same snap context.
Sage Weil8f883c22010-03-19 13:27:53 -07001157 *
1158 * called with page locked.
1159 * return success with page locked,
1160 * or any failure (incl -EAGAIN) with page unlocked.
Sage Weil1d3576f2009-10-06 11:31:09 -07001161 */
Yehuda Sadeh4af6b222010-02-09 11:02:51 -08001162static int ceph_update_writeable_page(struct file *file,
1163 loff_t pos, unsigned len,
1164 struct page *page)
Sage Weil1d3576f2009-10-06 11:31:09 -07001165{
Al Viro496ad9a2013-01-23 17:07:38 -05001166 struct inode *inode = file_inode(file);
Yan, Zheng6c93df52016-04-15 13:56:12 +08001167 struct ceph_fs_client *fsc = ceph_inode_to_client(inode);
Sage Weil1d3576f2009-10-06 11:31:09 -07001168 struct ceph_inode_info *ci = ceph_inode(inode);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001169 loff_t page_off = pos & PAGE_MASK;
1170 int pos_in_page = pos & ~PAGE_MASK;
Sage Weil1d3576f2009-10-06 11:31:09 -07001171 int end_in_page = pos_in_page + len;
1172 loff_t i_size;
Sage Weil1d3576f2009-10-06 11:31:09 -07001173 int r;
Sage Weil80e755f2010-03-31 21:52:10 -07001174 struct ceph_snap_context *snapc, *oldest;
Sage Weil1d3576f2009-10-06 11:31:09 -07001175
Yan, Zheng6c93df52016-04-15 13:56:12 +08001176 if (ACCESS_ONCE(fsc->mount_state) == CEPH_MOUNT_SHUTDOWN) {
1177 dout(" page %p forced umount\n", page);
1178 unlock_page(page);
1179 return -EIO;
1180 }
1181
Sage Weil1d3576f2009-10-06 11:31:09 -07001182retry_locked:
1183 /* writepages currently holds page lock, but if we change that later, */
1184 wait_on_page_writeback(page);
1185
Yan, Zheng61600ef2012-05-28 14:44:30 +08001186 snapc = page_snap_context(page);
Sage Weil80e755f2010-03-31 21:52:10 -07001187 if (snapc && snapc != ci->i_head_snapc) {
Sage Weil1d3576f2009-10-06 11:31:09 -07001188 /*
1189 * this page is already dirty in another (older) snap
1190 * context! is it writeable now?
1191 */
Sage Weil80e755f2010-03-31 21:52:10 -07001192 oldest = get_oldest_context(inode, NULL);
Sage Weil1d3576f2009-10-06 11:31:09 -07001193
Sage Weil80e755f2010-03-31 21:52:10 -07001194 if (snapc->seq > oldest->seq) {
Sage Weil6298a332010-03-31 22:01:38 -07001195 ceph_put_snap_context(oldest);
Sage Weil1d3576f2009-10-06 11:31:09 -07001196 dout(" page %p snapc %p not current or oldest\n",
Sage Weil6298a332010-03-31 22:01:38 -07001197 page, snapc);
Sage Weil1d3576f2009-10-06 11:31:09 -07001198 /*
1199 * queue for writeback, and wait for snapc to
1200 * be writeable or written
1201 */
Sage Weil6298a332010-03-31 22:01:38 -07001202 snapc = ceph_get_snap_context(snapc);
Sage Weil1d3576f2009-10-06 11:31:09 -07001203 unlock_page(page);
Sage Weil3c6f6b72010-02-09 15:24:44 -08001204 ceph_queue_writeback(inode);
Yan, Zhenga78bbd42016-05-13 11:30:24 +08001205 r = wait_event_killable(ci->i_cap_wq,
Sage Weil1d3576f2009-10-06 11:31:09 -07001206 context_is_writeable_or_written(inode, snapc));
1207 ceph_put_snap_context(snapc);
Sage Weil8f883c22010-03-19 13:27:53 -07001208 if (r == -ERESTARTSYS)
1209 return r;
Yehuda Sadeh4af6b222010-02-09 11:02:51 -08001210 return -EAGAIN;
Sage Weil1d3576f2009-10-06 11:31:09 -07001211 }
Sage Weil6298a332010-03-31 22:01:38 -07001212 ceph_put_snap_context(oldest);
Sage Weil1d3576f2009-10-06 11:31:09 -07001213
1214 /* yay, writeable, do it now (without dropping page lock) */
1215 dout(" page %p snapc %p not current, but oldest\n",
1216 page, snapc);
1217 if (!clear_page_dirty_for_io(page))
1218 goto retry_locked;
1219 r = writepage_nounlock(page, NULL);
1220 if (r < 0)
Yan, Zheng71584922017-08-04 11:22:31 +08001221 goto fail_unlock;
Sage Weil1d3576f2009-10-06 11:31:09 -07001222 goto retry_locked;
1223 }
1224
1225 if (PageUptodate(page)) {
1226 dout(" page %p already uptodate\n", page);
1227 return 0;
1228 }
1229
1230 /* full page? */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001231 if (pos_in_page == 0 && len == PAGE_SIZE)
Sage Weil1d3576f2009-10-06 11:31:09 -07001232 return 0;
1233
1234 /* past end of file? */
Yan, Zheng99c88e62015-12-30 11:32:46 +08001235 i_size = i_size_read(inode);
Sage Weil1d3576f2009-10-06 11:31:09 -07001236
Sage Weil1d3576f2009-10-06 11:31:09 -07001237 if (page_off >= i_size ||
1238 (pos_in_page == 0 && (pos+len) >= i_size &&
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001239 end_in_page - pos_in_page != PAGE_SIZE)) {
Sage Weil1d3576f2009-10-06 11:31:09 -07001240 dout(" zeroing %p 0 - %d and %d - %d\n",
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001241 page, pos_in_page, end_in_page, (int)PAGE_SIZE);
Sage Weil1d3576f2009-10-06 11:31:09 -07001242 zero_user_segments(page,
1243 0, pos_in_page,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001244 end_in_page, PAGE_SIZE);
Sage Weil1d3576f2009-10-06 11:31:09 -07001245 return 0;
1246 }
1247
1248 /* we need to read it. */
Yan, Zheng71584922017-08-04 11:22:31 +08001249 r = ceph_do_readpage(file, page);
1250 if (r < 0) {
1251 if (r == -EINPROGRESS)
1252 return -EAGAIN;
1253 goto fail_unlock;
1254 }
Sage Weil1d3576f2009-10-06 11:31:09 -07001255 goto retry_locked;
Yan, Zheng71584922017-08-04 11:22:31 +08001256fail_unlock:
Sage Weil1d3576f2009-10-06 11:31:09 -07001257 unlock_page(page);
1258 return r;
1259}
1260
1261/*
Yehuda Sadeh4af6b222010-02-09 11:02:51 -08001262 * We are only allowed to write into/dirty the page if the page is
1263 * clean, or already dirty within the same snap context.
1264 */
1265static int ceph_write_begin(struct file *file, struct address_space *mapping,
1266 loff_t pos, unsigned len, unsigned flags,
1267 struct page **pagep, void **fsdata)
1268{
Al Viro496ad9a2013-01-23 17:07:38 -05001269 struct inode *inode = file_inode(file);
Yehuda Sadeh4af6b222010-02-09 11:02:51 -08001270 struct page *page;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001271 pgoff_t index = pos >> PAGE_SHIFT;
Sage Weil7971bd92013-05-01 21:15:58 -07001272 int r;
Yehuda Sadeh4af6b222010-02-09 11:02:51 -08001273
1274 do {
Sage Weil8f883c22010-03-19 13:27:53 -07001275 /* get a page */
Yehuda Sadeh4af6b222010-02-09 11:02:51 -08001276 page = grab_cache_page_write_begin(mapping, index, 0);
Sage Weil7971bd92013-05-01 21:15:58 -07001277 if (!page)
1278 return -ENOMEM;
Yehuda Sadeh4af6b222010-02-09 11:02:51 -08001279
1280 dout("write_begin file %p inode %p page %p %d~%d\n", file,
Sage Weil213c99e2010-08-03 10:25:11 -07001281 inode, page, (int)pos, (int)len);
Yehuda Sadeh4af6b222010-02-09 11:02:51 -08001282
1283 r = ceph_update_writeable_page(file, pos, len, page);
Taesoo Kimc1d00b22015-03-20 17:36:56 -04001284 if (r < 0)
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001285 put_page(page);
Taesoo Kimc1d00b22015-03-20 17:36:56 -04001286 else
1287 *pagep = page;
Yehuda Sadeh4af6b222010-02-09 11:02:51 -08001288 } while (r == -EAGAIN);
1289
1290 return r;
1291}
1292
1293/*
Sage Weil1d3576f2009-10-06 11:31:09 -07001294 * we don't do anything in here that simple_write_end doesn't do
Yan, Zheng5dda377c2015-04-30 14:40:54 +08001295 * except adjust dirty page accounting
Sage Weil1d3576f2009-10-06 11:31:09 -07001296 */
1297static int ceph_write_end(struct file *file, struct address_space *mapping,
1298 loff_t pos, unsigned len, unsigned copied,
1299 struct page *page, void *fsdata)
1300{
Al Viro496ad9a2013-01-23 17:07:38 -05001301 struct inode *inode = file_inode(file);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001302 unsigned from = pos & (PAGE_SIZE - 1);
Sage Weil1d3576f2009-10-06 11:31:09 -07001303 int check_cap = 0;
1304
1305 dout("write_end file %p inode %p page %p %d~%d (%d)\n", file,
1306 inode, page, (int)pos, (int)copied, (int)len);
1307
1308 /* zero the stale part of the page if we did a short copy */
1309 if (copied < len)
1310 zero_user_segment(page, from+copied, len);
1311
1312 /* did file size increase? */
Yan, Zheng99c88e62015-12-30 11:32:46 +08001313 if (pos+copied > i_size_read(inode))
Sage Weil1d3576f2009-10-06 11:31:09 -07001314 check_cap = ceph_inode_set_size(inode, pos+copied);
1315
1316 if (!PageUptodate(page))
1317 SetPageUptodate(page);
1318
1319 set_page_dirty(page);
1320
1321 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001322 put_page(page);
Sage Weil1d3576f2009-10-06 11:31:09 -07001323
1324 if (check_cap)
1325 ceph_check_caps(ceph_inode(inode), CHECK_CAPS_AUTHONLY, NULL);
1326
1327 return copied;
1328}
1329
1330/*
1331 * we set .direct_IO to indicate direct io is supported, but since we
1332 * intercept O_DIRECT reads and writes early, this function should
1333 * never get called.
1334 */
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07001335static ssize_t ceph_direct_io(struct kiocb *iocb, struct iov_iter *iter)
Sage Weil1d3576f2009-10-06 11:31:09 -07001336{
1337 WARN_ON(1);
1338 return -EINVAL;
1339}
1340
1341const struct address_space_operations ceph_aops = {
1342 .readpage = ceph_readpage,
1343 .readpages = ceph_readpages,
1344 .writepage = ceph_writepage,
1345 .writepages = ceph_writepages_start,
1346 .write_begin = ceph_write_begin,
1347 .write_end = ceph_write_end,
1348 .set_page_dirty = ceph_set_page_dirty,
1349 .invalidatepage = ceph_invalidatepage,
1350 .releasepage = ceph_releasepage,
1351 .direct_IO = ceph_direct_io,
1352};
1353
Yan, Zheng4f7e89f2016-05-10 18:40:28 +08001354static void ceph_block_sigs(sigset_t *oldset)
1355{
1356 sigset_t mask;
1357 siginitsetinv(&mask, sigmask(SIGKILL));
1358 sigprocmask(SIG_BLOCK, &mask, oldset);
1359}
1360
1361static void ceph_restore_sigs(sigset_t *oldset)
1362{
1363 sigprocmask(SIG_SETMASK, oldset, NULL);
1364}
Sage Weil1d3576f2009-10-06 11:31:09 -07001365
1366/*
1367 * vm ops
1368 */
Yan, Zheng61f68812013-11-28 14:28:14 +08001369static int ceph_filemap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
1370{
1371 struct inode *inode = file_inode(vma->vm_file);
1372 struct ceph_inode_info *ci = ceph_inode(inode);
1373 struct ceph_file_info *fi = vma->vm_file->private_data;
Yan, Zheng3738daa2014-11-14 22:10:07 +08001374 struct page *pinned_page = NULL;
Matthew Wilcox (Oracle)fab99222020-10-04 19:04:24 +01001375 loff_t off = (loff_t)vmf->pgoff << PAGE_SHIFT;
Yan, Zheng61f68812013-11-28 14:28:14 +08001376 int want, got, ret;
Yan, Zheng4f7e89f2016-05-10 18:40:28 +08001377 sigset_t oldset;
1378
1379 ceph_block_sigs(&oldset);
Yan, Zheng61f68812013-11-28 14:28:14 +08001380
1381 dout("filemap_fault %p %llx.%llx %llu~%zd trying to get caps\n",
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001382 inode, ceph_vinop(inode), off, (size_t)PAGE_SIZE);
Yan, Zheng61f68812013-11-28 14:28:14 +08001383 if (fi->fmode & CEPH_FILE_MODE_LAZY)
1384 want = CEPH_CAP_FILE_CACHE | CEPH_CAP_FILE_LAZYIO;
1385 else
1386 want = CEPH_CAP_FILE_CACHE;
Yan, Zheng4f7e89f2016-05-10 18:40:28 +08001387
1388 got = 0;
1389 ret = ceph_get_caps(ci, CEPH_CAP_FILE_RD, want, -1, &got, &pinned_page);
Yan, Zheng6ce026e2016-05-10 18:59:13 +08001390 if (ret < 0)
Yan, Zheng4f7e89f2016-05-10 18:40:28 +08001391 goto out_restore;
Yan, Zheng6ce026e2016-05-10 18:59:13 +08001392
Yan, Zheng61f68812013-11-28 14:28:14 +08001393 dout("filemap_fault %p %llu~%zd got cap refs on %s\n",
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001394 inode, off, (size_t)PAGE_SIZE, ceph_cap_string(got));
Yan, Zheng61f68812013-11-28 14:28:14 +08001395
Yan, Zheng83701242014-11-14 22:36:18 +08001396 if ((got & (CEPH_CAP_FILE_CACHE | CEPH_CAP_FILE_LAZYIO)) ||
Yan, Zhenge99b0ea2016-10-25 10:51:55 +08001397 ci->i_inline_version == CEPH_INLINE_NONE) {
1398 current->journal_info = vma->vm_file;
Yan, Zheng83701242014-11-14 22:36:18 +08001399 ret = filemap_fault(vma, vmf);
Yan, Zhenge99b0ea2016-10-25 10:51:55 +08001400 current->journal_info = NULL;
1401 } else
Yan, Zheng83701242014-11-14 22:36:18 +08001402 ret = -EAGAIN;
Yan, Zheng61f68812013-11-28 14:28:14 +08001403
1404 dout("filemap_fault %p %llu~%zd dropping cap refs on %s ret %d\n",
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001405 inode, off, (size_t)PAGE_SIZE, ceph_cap_string(got), ret);
Yan, Zheng3738daa2014-11-14 22:10:07 +08001406 if (pinned_page)
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001407 put_page(pinned_page);
Yan, Zheng61f68812013-11-28 14:28:14 +08001408 ceph_put_cap_refs(ci, got);
1409
Yan, Zheng83701242014-11-14 22:36:18 +08001410 if (ret != -EAGAIN)
Yan, Zheng4f7e89f2016-05-10 18:40:28 +08001411 goto out_restore;
Yan, Zheng83701242014-11-14 22:36:18 +08001412
1413 /* read inline data */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001414 if (off >= PAGE_SIZE) {
Yan, Zheng83701242014-11-14 22:36:18 +08001415 /* does not support inline data > PAGE_SIZE */
1416 ret = VM_FAULT_SIGBUS;
1417 } else {
1418 int ret1;
1419 struct address_space *mapping = inode->i_mapping;
1420 struct page *page = find_or_create_page(mapping, 0,
Michal Hockoc62d2552015-11-06 16:28:49 -08001421 mapping_gfp_constraint(mapping,
1422 ~__GFP_FS));
Yan, Zheng83701242014-11-14 22:36:18 +08001423 if (!page) {
1424 ret = VM_FAULT_OOM;
Yan, Zheng4f7e89f2016-05-10 18:40:28 +08001425 goto out_inline;
Yan, Zheng83701242014-11-14 22:36:18 +08001426 }
1427 ret1 = __ceph_do_getattr(inode, page,
1428 CEPH_STAT_CAP_INLINE_DATA, true);
1429 if (ret1 < 0 || off >= i_size_read(inode)) {
1430 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001431 put_page(page);
Yan, Zheng6ce026e2016-05-10 18:59:13 +08001432 if (ret1 < 0)
1433 ret = ret1;
1434 else
1435 ret = VM_FAULT_SIGBUS;
Yan, Zheng4f7e89f2016-05-10 18:40:28 +08001436 goto out_inline;
Yan, Zheng83701242014-11-14 22:36:18 +08001437 }
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001438 if (ret1 < PAGE_SIZE)
1439 zero_user_segment(page, ret1, PAGE_SIZE);
Yan, Zheng83701242014-11-14 22:36:18 +08001440 else
1441 flush_dcache_page(page);
1442 SetPageUptodate(page);
1443 vmf->page = page;
1444 ret = VM_FAULT_MAJOR | VM_FAULT_LOCKED;
Yan, Zheng4f7e89f2016-05-10 18:40:28 +08001445out_inline:
1446 dout("filemap_fault %p %llu~%zd read inline data ret %d\n",
1447 inode, off, (size_t)PAGE_SIZE, ret);
Yan, Zheng83701242014-11-14 22:36:18 +08001448 }
Yan, Zheng4f7e89f2016-05-10 18:40:28 +08001449out_restore:
1450 ceph_restore_sigs(&oldset);
Yan, Zheng6ce026e2016-05-10 18:59:13 +08001451 if (ret < 0)
1452 ret = (ret == -ENOMEM) ? VM_FAULT_OOM : VM_FAULT_SIGBUS;
1453
Yan, Zheng61f68812013-11-28 14:28:14 +08001454 return ret;
1455}
Sage Weil1d3576f2009-10-06 11:31:09 -07001456
1457/*
1458 * Reuse write_begin here for simplicity.
1459 */
1460static int ceph_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
1461{
Al Viro496ad9a2013-01-23 17:07:38 -05001462 struct inode *inode = file_inode(vma->vm_file);
Yan, Zheng61f68812013-11-28 14:28:14 +08001463 struct ceph_inode_info *ci = ceph_inode(inode);
1464 struct ceph_file_info *fi = vma->vm_file->private_data;
Yan, Zhengf66fd9f2015-06-10 17:26:13 +08001465 struct ceph_cap_flush *prealloc_cf;
Yan, Zheng61f68812013-11-28 14:28:14 +08001466 struct page *page = vmf->page;
Alex Elder6285bc22012-10-02 10:25:51 -05001467 loff_t off = page_offset(page);
Yan, Zheng61f68812013-11-28 14:28:14 +08001468 loff_t size = i_size_read(inode);
1469 size_t len;
1470 int want, got, ret;
Yan, Zheng4f7e89f2016-05-10 18:40:28 +08001471 sigset_t oldset;
Sage Weil1d3576f2009-10-06 11:31:09 -07001472
Yan, Zhengf66fd9f2015-06-10 17:26:13 +08001473 prealloc_cf = ceph_alloc_cap_flush();
1474 if (!prealloc_cf)
Yan, Zheng6ce026e2016-05-10 18:59:13 +08001475 return VM_FAULT_OOM;
Yan, Zhengf66fd9f2015-06-10 17:26:13 +08001476
Yan, Zheng4f7e89f2016-05-10 18:40:28 +08001477 ceph_block_sigs(&oldset);
Sage Weil1d3576f2009-10-06 11:31:09 -07001478
Yan, Zheng28127bd2014-11-14 22:38:29 +08001479 if (ci->i_inline_version != CEPH_INLINE_NONE) {
1480 struct page *locked_page = NULL;
1481 if (off == 0) {
1482 lock_page(page);
1483 locked_page = page;
1484 }
1485 ret = ceph_uninline_data(vma->vm_file, locked_page);
1486 if (locked_page)
1487 unlock_page(locked_page);
Yan, Zheng6ce026e2016-05-10 18:59:13 +08001488 if (ret < 0)
Yan, Zhengf66fd9f2015-06-10 17:26:13 +08001489 goto out_free;
Yan, Zheng28127bd2014-11-14 22:38:29 +08001490 }
1491
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001492 if (off + PAGE_SIZE <= size)
1493 len = PAGE_SIZE;
Sage Weil1d3576f2009-10-06 11:31:09 -07001494 else
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001495 len = size & ~PAGE_MASK;
Sage Weil1d3576f2009-10-06 11:31:09 -07001496
Yan, Zheng61f68812013-11-28 14:28:14 +08001497 dout("page_mkwrite %p %llx.%llx %llu~%zd getting caps i_size %llu\n",
1498 inode, ceph_vinop(inode), off, len, size);
1499 if (fi->fmode & CEPH_FILE_MODE_LAZY)
1500 want = CEPH_CAP_FILE_BUFFER | CEPH_CAP_FILE_LAZYIO;
1501 else
1502 want = CEPH_CAP_FILE_BUFFER;
Yan, Zheng4f7e89f2016-05-10 18:40:28 +08001503
1504 got = 0;
1505 ret = ceph_get_caps(ci, CEPH_CAP_FILE_WR, want, off + len,
1506 &got, NULL);
Yan, Zheng6ce026e2016-05-10 18:59:13 +08001507 if (ret < 0)
Yan, Zheng4f7e89f2016-05-10 18:40:28 +08001508 goto out_free;
Yan, Zheng6ce026e2016-05-10 18:59:13 +08001509
Yan, Zheng61f68812013-11-28 14:28:14 +08001510 dout("page_mkwrite %p %llu~%zd got cap refs on %s\n",
1511 inode, off, len, ceph_cap_string(got));
1512
1513 /* Update time before taking page lock */
1514 file_update_time(vma->vm_file);
Yehuda Sadeh4af6b222010-02-09 11:02:51 -08001515
Yan, Zhengf0b33df2016-05-10 19:09:06 +08001516 do {
1517 lock_page(page);
Yehuda Sadeh4af6b222010-02-09 11:02:51 -08001518
Yan, Zhengf0b33df2016-05-10 19:09:06 +08001519 if ((off > size) || (page->mapping != inode->i_mapping)) {
1520 unlock_page(page);
1521 ret = VM_FAULT_NOPAGE;
1522 break;
1523 }
Yehuda Sadeh4af6b222010-02-09 11:02:51 -08001524
Yan, Zhengf0b33df2016-05-10 19:09:06 +08001525 ret = ceph_update_writeable_page(vma->vm_file, off, len, page);
1526 if (ret >= 0) {
1527 /* success. we'll keep the page locked. */
1528 set_page_dirty(page);
1529 ret = VM_FAULT_LOCKED;
1530 }
1531 } while (ret == -EAGAIN);
1532
Yan, Zheng28127bd2014-11-14 22:38:29 +08001533 if (ret == VM_FAULT_LOCKED ||
1534 ci->i_inline_version != CEPH_INLINE_NONE) {
Yan, Zheng61f68812013-11-28 14:28:14 +08001535 int dirty;
1536 spin_lock(&ci->i_ceph_lock);
Yan, Zheng28127bd2014-11-14 22:38:29 +08001537 ci->i_inline_version = CEPH_INLINE_NONE;
Yan, Zhengf66fd9f2015-06-10 17:26:13 +08001538 dirty = __ceph_mark_dirty_caps(ci, CEPH_CAP_FILE_WR,
1539 &prealloc_cf);
Yan, Zheng61f68812013-11-28 14:28:14 +08001540 spin_unlock(&ci->i_ceph_lock);
1541 if (dirty)
1542 __mark_inode_dirty(inode, dirty);
1543 }
1544
1545 dout("page_mkwrite %p %llu~%zd dropping cap refs on %s ret %d\n",
1546 inode, off, len, ceph_cap_string(got), ret);
1547 ceph_put_cap_refs(ci, got);
Yan, Zhengf66fd9f2015-06-10 17:26:13 +08001548out_free:
Yan, Zheng4f7e89f2016-05-10 18:40:28 +08001549 ceph_restore_sigs(&oldset);
Yan, Zhengf66fd9f2015-06-10 17:26:13 +08001550 ceph_free_cap_flush(prealloc_cf);
Yan, Zheng6ce026e2016-05-10 18:59:13 +08001551 if (ret < 0)
1552 ret = (ret == -ENOMEM) ? VM_FAULT_OOM : VM_FAULT_SIGBUS;
Sage Weil1d3576f2009-10-06 11:31:09 -07001553 return ret;
1554}
1555
Yan, Zheng31c542a2014-11-14 21:41:55 +08001556void ceph_fill_inline_data(struct inode *inode, struct page *locked_page,
1557 char *data, size_t len)
1558{
1559 struct address_space *mapping = inode->i_mapping;
1560 struct page *page;
1561
1562 if (locked_page) {
1563 page = locked_page;
1564 } else {
1565 if (i_size_read(inode) == 0)
1566 return;
1567 page = find_or_create_page(mapping, 0,
Michal Hockoc62d2552015-11-06 16:28:49 -08001568 mapping_gfp_constraint(mapping,
1569 ~__GFP_FS));
Yan, Zheng31c542a2014-11-14 21:41:55 +08001570 if (!page)
1571 return;
1572 if (PageUptodate(page)) {
1573 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001574 put_page(page);
Yan, Zheng31c542a2014-11-14 21:41:55 +08001575 return;
1576 }
1577 }
1578
Ilya Dryomov0668ff52014-12-19 13:10:10 +03001579 dout("fill_inline_data %p %llx.%llx len %zu locked_page %p\n",
Yan, Zheng31c542a2014-11-14 21:41:55 +08001580 inode, ceph_vinop(inode), len, locked_page);
1581
1582 if (len > 0) {
1583 void *kaddr = kmap_atomic(page);
1584 memcpy(kaddr, data, len);
1585 kunmap_atomic(kaddr);
1586 }
1587
1588 if (page != locked_page) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001589 if (len < PAGE_SIZE)
1590 zero_user_segment(page, len, PAGE_SIZE);
Yan, Zheng31c542a2014-11-14 21:41:55 +08001591 else
1592 flush_dcache_page(page);
1593
1594 SetPageUptodate(page);
1595 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001596 put_page(page);
Yan, Zheng31c542a2014-11-14 21:41:55 +08001597 }
1598}
1599
Yan, Zheng28127bd2014-11-14 22:38:29 +08001600int ceph_uninline_data(struct file *filp, struct page *locked_page)
1601{
1602 struct inode *inode = file_inode(filp);
1603 struct ceph_inode_info *ci = ceph_inode(inode);
1604 struct ceph_fs_client *fsc = ceph_inode_to_client(inode);
1605 struct ceph_osd_request *req;
1606 struct page *page = NULL;
1607 u64 len, inline_version;
1608 int err = 0;
1609 bool from_pagecache = false;
1610
1611 spin_lock(&ci->i_ceph_lock);
1612 inline_version = ci->i_inline_version;
1613 spin_unlock(&ci->i_ceph_lock);
1614
1615 dout("uninline_data %p %llx.%llx inline_version %llu\n",
1616 inode, ceph_vinop(inode), inline_version);
1617
1618 if (inline_version == 1 || /* initial version, no data */
1619 inline_version == CEPH_INLINE_NONE)
1620 goto out;
1621
1622 if (locked_page) {
1623 page = locked_page;
1624 WARN_ON(!PageUptodate(page));
1625 } else if (ceph_caps_issued(ci) &
1626 (CEPH_CAP_FILE_CACHE|CEPH_CAP_FILE_LAZYIO)) {
1627 page = find_get_page(inode->i_mapping, 0);
1628 if (page) {
1629 if (PageUptodate(page)) {
1630 from_pagecache = true;
1631 lock_page(page);
1632 } else {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001633 put_page(page);
Yan, Zheng28127bd2014-11-14 22:38:29 +08001634 page = NULL;
1635 }
1636 }
1637 }
1638
1639 if (page) {
1640 len = i_size_read(inode);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001641 if (len > PAGE_SIZE)
1642 len = PAGE_SIZE;
Yan, Zheng28127bd2014-11-14 22:38:29 +08001643 } else {
1644 page = __page_cache_alloc(GFP_NOFS);
1645 if (!page) {
1646 err = -ENOMEM;
1647 goto out;
1648 }
1649 err = __ceph_do_getattr(inode, page,
1650 CEPH_STAT_CAP_INLINE_DATA, true);
1651 if (err < 0) {
1652 /* no inline data */
1653 if (err == -ENODATA)
1654 err = 0;
1655 goto out;
1656 }
1657 len = err;
1658 }
1659
1660 req = ceph_osdc_new_request(&fsc->client->osdc, &ci->i_layout,
1661 ceph_vino(inode), 0, &len, 0, 1,
1662 CEPH_OSD_OP_CREATE,
1663 CEPH_OSD_FLAG_ONDISK | CEPH_OSD_FLAG_WRITE,
Ilya Dryomov34b759b2016-02-16 15:00:24 +01001664 NULL, 0, 0, false);
Yan, Zheng28127bd2014-11-14 22:38:29 +08001665 if (IS_ERR(req)) {
1666 err = PTR_ERR(req);
1667 goto out;
1668 }
1669
Ilya Dryomovbb873b52016-05-26 00:29:52 +02001670 req->r_mtime = inode->i_mtime;
Yan, Zheng28127bd2014-11-14 22:38:29 +08001671 err = ceph_osdc_start_request(&fsc->client->osdc, req, false);
1672 if (!err)
1673 err = ceph_osdc_wait_request(&fsc->client->osdc, req);
1674 ceph_osdc_put_request(req);
1675 if (err < 0)
1676 goto out;
1677
1678 req = ceph_osdc_new_request(&fsc->client->osdc, &ci->i_layout,
1679 ceph_vino(inode), 0, &len, 1, 3,
1680 CEPH_OSD_OP_WRITE,
1681 CEPH_OSD_FLAG_ONDISK | CEPH_OSD_FLAG_WRITE,
Ilya Dryomov34b759b2016-02-16 15:00:24 +01001682 NULL, ci->i_truncate_seq,
1683 ci->i_truncate_size, false);
Yan, Zheng28127bd2014-11-14 22:38:29 +08001684 if (IS_ERR(req)) {
1685 err = PTR_ERR(req);
1686 goto out;
1687 }
1688
1689 osd_req_op_extent_osd_data_pages(req, 1, &page, len, 0, false, false);
1690
Yan, Zhengec137c12015-04-13 11:25:07 +08001691 {
1692 __le64 xattr_buf = cpu_to_le64(inline_version);
1693 err = osd_req_op_xattr_init(req, 0, CEPH_OSD_OP_CMPXATTR,
1694 "inline_version", &xattr_buf,
1695 sizeof(xattr_buf),
1696 CEPH_OSD_CMPXATTR_OP_GT,
1697 CEPH_OSD_CMPXATTR_MODE_U64);
1698 if (err)
1699 goto out_put;
1700 }
Yan, Zheng28127bd2014-11-14 22:38:29 +08001701
Yan, Zhengec137c12015-04-13 11:25:07 +08001702 {
1703 char xattr_buf[32];
1704 int xattr_len = snprintf(xattr_buf, sizeof(xattr_buf),
1705 "%llu", inline_version);
1706 err = osd_req_op_xattr_init(req, 2, CEPH_OSD_OP_SETXATTR,
1707 "inline_version",
1708 xattr_buf, xattr_len, 0, 0);
1709 if (err)
1710 goto out_put;
1711 }
Yan, Zheng28127bd2014-11-14 22:38:29 +08001712
Ilya Dryomovbb873b52016-05-26 00:29:52 +02001713 req->r_mtime = inode->i_mtime;
Yan, Zheng28127bd2014-11-14 22:38:29 +08001714 err = ceph_osdc_start_request(&fsc->client->osdc, req, false);
1715 if (!err)
1716 err = ceph_osdc_wait_request(&fsc->client->osdc, req);
1717out_put:
1718 ceph_osdc_put_request(req);
1719 if (err == -ECANCELED)
1720 err = 0;
1721out:
1722 if (page && page != locked_page) {
1723 if (from_pagecache) {
1724 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001725 put_page(page);
Yan, Zheng28127bd2014-11-14 22:38:29 +08001726 } else
1727 __free_pages(page, 0);
1728 }
1729
1730 dout("uninline_data %p %llx.%llx inline_version %llu = %d\n",
1731 inode, ceph_vinop(inode), inline_version, err);
1732 return err;
1733}
1734
Kirill A. Shutemov7cbea8d2015-09-09 15:39:26 -07001735static const struct vm_operations_struct ceph_vmops = {
Yan, Zheng61f68812013-11-28 14:28:14 +08001736 .fault = ceph_filemap_fault,
Sage Weil1d3576f2009-10-06 11:31:09 -07001737 .page_mkwrite = ceph_page_mkwrite,
1738};
1739
1740int ceph_mmap(struct file *file, struct vm_area_struct *vma)
1741{
1742 struct address_space *mapping = file->f_mapping;
1743
1744 if (!mapping->a_ops->readpage)
1745 return -ENOEXEC;
1746 file_accessed(file);
1747 vma->vm_ops = &ceph_vmops;
Sage Weil1d3576f2009-10-06 11:31:09 -07001748 return 0;
1749}
Yan, Zheng10183a62015-04-27 15:33:28 +08001750
1751enum {
1752 POOL_READ = 1,
1753 POOL_WRITE = 2,
1754};
1755
Yan, Zheng779fe0f2016-03-07 09:35:06 +08001756static int __ceph_pool_perm_get(struct ceph_inode_info *ci,
1757 s64 pool, struct ceph_string *pool_ns)
Yan, Zheng10183a62015-04-27 15:33:28 +08001758{
1759 struct ceph_fs_client *fsc = ceph_inode_to_client(&ci->vfs_inode);
1760 struct ceph_mds_client *mdsc = fsc->mdsc;
1761 struct ceph_osd_request *rd_req = NULL, *wr_req = NULL;
1762 struct rb_node **p, *parent;
1763 struct ceph_pool_perm *perm;
1764 struct page **pages;
Yan, Zheng779fe0f2016-03-07 09:35:06 +08001765 size_t pool_ns_len;
Yan, Zheng10183a62015-04-27 15:33:28 +08001766 int err = 0, err2 = 0, have = 0;
1767
1768 down_read(&mdsc->pool_perm_rwsem);
1769 p = &mdsc->pool_perm_tree.rb_node;
1770 while (*p) {
1771 perm = rb_entry(*p, struct ceph_pool_perm, node);
1772 if (pool < perm->pool)
1773 p = &(*p)->rb_left;
1774 else if (pool > perm->pool)
1775 p = &(*p)->rb_right;
1776 else {
Yan, Zheng779fe0f2016-03-07 09:35:06 +08001777 int ret = ceph_compare_string(pool_ns,
1778 perm->pool_ns,
1779 perm->pool_ns_len);
1780 if (ret < 0)
1781 p = &(*p)->rb_left;
1782 else if (ret > 0)
1783 p = &(*p)->rb_right;
1784 else {
1785 have = perm->perm;
1786 break;
1787 }
Yan, Zheng10183a62015-04-27 15:33:28 +08001788 }
1789 }
1790 up_read(&mdsc->pool_perm_rwsem);
1791 if (*p)
1792 goto out;
1793
Yan, Zheng779fe0f2016-03-07 09:35:06 +08001794 if (pool_ns)
1795 dout("__ceph_pool_perm_get pool %lld ns %.*s no perm cached\n",
1796 pool, (int)pool_ns->len, pool_ns->str);
1797 else
1798 dout("__ceph_pool_perm_get pool %lld no perm cached\n", pool);
Yan, Zheng10183a62015-04-27 15:33:28 +08001799
1800 down_write(&mdsc->pool_perm_rwsem);
Yan, Zheng779fe0f2016-03-07 09:35:06 +08001801 p = &mdsc->pool_perm_tree.rb_node;
Yan, Zheng10183a62015-04-27 15:33:28 +08001802 parent = NULL;
1803 while (*p) {
1804 parent = *p;
1805 perm = rb_entry(parent, struct ceph_pool_perm, node);
1806 if (pool < perm->pool)
1807 p = &(*p)->rb_left;
1808 else if (pool > perm->pool)
1809 p = &(*p)->rb_right;
1810 else {
Yan, Zheng779fe0f2016-03-07 09:35:06 +08001811 int ret = ceph_compare_string(pool_ns,
1812 perm->pool_ns,
1813 perm->pool_ns_len);
1814 if (ret < 0)
1815 p = &(*p)->rb_left;
1816 else if (ret > 0)
1817 p = &(*p)->rb_right;
1818 else {
1819 have = perm->perm;
1820 break;
1821 }
Yan, Zheng10183a62015-04-27 15:33:28 +08001822 }
1823 }
1824 if (*p) {
1825 up_write(&mdsc->pool_perm_rwsem);
1826 goto out;
1827 }
1828
Ilya Dryomov34b759b2016-02-16 15:00:24 +01001829 rd_req = ceph_osdc_alloc_request(&fsc->client->osdc, NULL,
Yan, Zheng10183a62015-04-27 15:33:28 +08001830 1, false, GFP_NOFS);
1831 if (!rd_req) {
1832 err = -ENOMEM;
1833 goto out_unlock;
1834 }
1835
1836 rd_req->r_flags = CEPH_OSD_FLAG_READ;
1837 osd_req_op_init(rd_req, 0, CEPH_OSD_OP_STAT, 0);
1838 rd_req->r_base_oloc.pool = pool;
Yan, Zheng779fe0f2016-03-07 09:35:06 +08001839 if (pool_ns)
1840 rd_req->r_base_oloc.pool_ns = ceph_get_string(pool_ns);
Ilya Dryomovd30291b2016-04-29 19:54:20 +02001841 ceph_oid_printf(&rd_req->r_base_oid, "%llx.00000000", ci->i_vino.ino);
Yan, Zheng10183a62015-04-27 15:33:28 +08001842
Ilya Dryomov13d1ad12016-04-27 14:15:51 +02001843 err = ceph_osdc_alloc_messages(rd_req, GFP_NOFS);
1844 if (err)
1845 goto out_unlock;
Yan, Zheng10183a62015-04-27 15:33:28 +08001846
Ilya Dryomov34b759b2016-02-16 15:00:24 +01001847 wr_req = ceph_osdc_alloc_request(&fsc->client->osdc, NULL,
Yan, Zheng10183a62015-04-27 15:33:28 +08001848 1, false, GFP_NOFS);
1849 if (!wr_req) {
1850 err = -ENOMEM;
1851 goto out_unlock;
1852 }
1853
Ilya Dryomovfe5da052016-04-28 16:07:24 +02001854 wr_req->r_flags = CEPH_OSD_FLAG_WRITE | CEPH_OSD_FLAG_ACK;
Yan, Zheng10183a62015-04-27 15:33:28 +08001855 osd_req_op_init(wr_req, 0, CEPH_OSD_OP_CREATE, CEPH_OSD_OP_FLAG_EXCL);
Ilya Dryomov63244fa2016-04-28 16:07:23 +02001856 ceph_oloc_copy(&wr_req->r_base_oloc, &rd_req->r_base_oloc);
Ilya Dryomovd30291b2016-04-29 19:54:20 +02001857 ceph_oid_copy(&wr_req->r_base_oid, &rd_req->r_base_oid);
Yan, Zheng10183a62015-04-27 15:33:28 +08001858
Ilya Dryomov13d1ad12016-04-27 14:15:51 +02001859 err = ceph_osdc_alloc_messages(wr_req, GFP_NOFS);
1860 if (err)
1861 goto out_unlock;
Yan, Zheng10183a62015-04-27 15:33:28 +08001862
1863 /* one page should be large enough for STAT data */
1864 pages = ceph_alloc_page_vector(1, GFP_KERNEL);
1865 if (IS_ERR(pages)) {
1866 err = PTR_ERR(pages);
1867 goto out_unlock;
1868 }
1869
1870 osd_req_op_raw_data_in_pages(rd_req, 0, pages, PAGE_SIZE,
1871 0, false, true);
Yan, Zheng10183a62015-04-27 15:33:28 +08001872 err = ceph_osdc_start_request(&fsc->client->osdc, rd_req, false);
1873
Ilya Dryomovbb873b52016-05-26 00:29:52 +02001874 wr_req->r_mtime = ci->vfs_inode.i_mtime;
Yan, Zheng10183a62015-04-27 15:33:28 +08001875 err2 = ceph_osdc_start_request(&fsc->client->osdc, wr_req, false);
1876
1877 if (!err)
1878 err = ceph_osdc_wait_request(&fsc->client->osdc, rd_req);
1879 if (!err2)
1880 err2 = ceph_osdc_wait_request(&fsc->client->osdc, wr_req);
1881
1882 if (err >= 0 || err == -ENOENT)
1883 have |= POOL_READ;
1884 else if (err != -EPERM)
1885 goto out_unlock;
1886
1887 if (err2 == 0 || err2 == -EEXIST)
1888 have |= POOL_WRITE;
1889 else if (err2 != -EPERM) {
1890 err = err2;
1891 goto out_unlock;
1892 }
1893
Yan, Zheng779fe0f2016-03-07 09:35:06 +08001894 pool_ns_len = pool_ns ? pool_ns->len : 0;
1895 perm = kmalloc(sizeof(*perm) + pool_ns_len + 1, GFP_NOFS);
Yan, Zheng10183a62015-04-27 15:33:28 +08001896 if (!perm) {
1897 err = -ENOMEM;
1898 goto out_unlock;
1899 }
1900
1901 perm->pool = pool;
1902 perm->perm = have;
Yan, Zheng779fe0f2016-03-07 09:35:06 +08001903 perm->pool_ns_len = pool_ns_len;
1904 if (pool_ns_len > 0)
1905 memcpy(perm->pool_ns, pool_ns->str, pool_ns_len);
1906 perm->pool_ns[pool_ns_len] = 0;
1907
Yan, Zheng10183a62015-04-27 15:33:28 +08001908 rb_link_node(&perm->node, parent, p);
1909 rb_insert_color(&perm->node, &mdsc->pool_perm_tree);
1910 err = 0;
1911out_unlock:
1912 up_write(&mdsc->pool_perm_rwsem);
1913
Ilya Dryomov3ed97d62016-04-26 15:05:29 +02001914 ceph_osdc_put_request(rd_req);
1915 ceph_osdc_put_request(wr_req);
Yan, Zheng10183a62015-04-27 15:33:28 +08001916out:
1917 if (!err)
1918 err = have;
Yan, Zheng779fe0f2016-03-07 09:35:06 +08001919 if (pool_ns)
1920 dout("__ceph_pool_perm_get pool %lld ns %.*s result = %d\n",
1921 pool, (int)pool_ns->len, pool_ns->str, err);
1922 else
1923 dout("__ceph_pool_perm_get pool %lld result = %d\n", pool, err);
Yan, Zheng10183a62015-04-27 15:33:28 +08001924 return err;
1925}
1926
1927int ceph_pool_perm_check(struct ceph_inode_info *ci, int need)
1928{
Yan, Zheng76271512016-02-03 21:24:49 +08001929 s64 pool;
Yan, Zheng779fe0f2016-03-07 09:35:06 +08001930 struct ceph_string *pool_ns;
Yan, Zheng10183a62015-04-27 15:33:28 +08001931 int ret, flags;
1932
1933 if (ceph_test_mount_opt(ceph_inode_to_client(&ci->vfs_inode),
1934 NOPOOLPERM))
1935 return 0;
1936
1937 spin_lock(&ci->i_ceph_lock);
1938 flags = ci->i_ceph_flags;
Yan, Zheng76271512016-02-03 21:24:49 +08001939 pool = ci->i_layout.pool_id;
Yan, Zheng10183a62015-04-27 15:33:28 +08001940 spin_unlock(&ci->i_ceph_lock);
1941check:
1942 if (flags & CEPH_I_POOL_PERM) {
1943 if ((need & CEPH_CAP_FILE_RD) && !(flags & CEPH_I_POOL_RD)) {
Yan, Zheng76271512016-02-03 21:24:49 +08001944 dout("ceph_pool_perm_check pool %lld no read perm\n",
Yan, Zheng10183a62015-04-27 15:33:28 +08001945 pool);
1946 return -EPERM;
1947 }
1948 if ((need & CEPH_CAP_FILE_WR) && !(flags & CEPH_I_POOL_WR)) {
Yan, Zheng76271512016-02-03 21:24:49 +08001949 dout("ceph_pool_perm_check pool %lld no write perm\n",
Yan, Zheng10183a62015-04-27 15:33:28 +08001950 pool);
1951 return -EPERM;
1952 }
1953 return 0;
1954 }
1955
Yan, Zheng779fe0f2016-03-07 09:35:06 +08001956 pool_ns = ceph_try_get_string(ci->i_layout.pool_ns);
1957 ret = __ceph_pool_perm_get(ci, pool, pool_ns);
1958 ceph_put_string(pool_ns);
Yan, Zheng10183a62015-04-27 15:33:28 +08001959 if (ret < 0)
1960 return ret;
1961
1962 flags = CEPH_I_POOL_PERM;
1963 if (ret & POOL_READ)
1964 flags |= CEPH_I_POOL_RD;
1965 if (ret & POOL_WRITE)
1966 flags |= CEPH_I_POOL_WR;
1967
1968 spin_lock(&ci->i_ceph_lock);
Yan, Zheng779fe0f2016-03-07 09:35:06 +08001969 if (pool == ci->i_layout.pool_id &&
1970 pool_ns == rcu_dereference_raw(ci->i_layout.pool_ns)) {
1971 ci->i_ceph_flags |= flags;
Yan, Zheng10183a62015-04-27 15:33:28 +08001972 } else {
Yan, Zheng76271512016-02-03 21:24:49 +08001973 pool = ci->i_layout.pool_id;
Yan, Zheng10183a62015-04-27 15:33:28 +08001974 flags = ci->i_ceph_flags;
1975 }
1976 spin_unlock(&ci->i_ceph_lock);
1977 goto check;
1978}
1979
1980void ceph_pool_perm_destroy(struct ceph_mds_client *mdsc)
1981{
1982 struct ceph_pool_perm *perm;
1983 struct rb_node *n;
1984
1985 while (!RB_EMPTY_ROOT(&mdsc->pool_perm_tree)) {
1986 n = rb_first(&mdsc->pool_perm_tree);
1987 perm = rb_entry(n, struct ceph_pool_perm, node);
1988 rb_erase(n, &mdsc->pool_perm_tree);
1989 kfree(perm);
1990 }
1991}