blob: 6ecb920602edc871946cf6beab778b66adb7b79b [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;
Sha Zhengju7d6e1f52013-08-21 16:27:34 +080075 int ret;
Sage Weil1d3576f2009-10-06 11:31:09 -070076
77 if (unlikely(!mapping))
78 return !TestSetPageDirty(page);
79
Sha Zhengju7d6e1f52013-08-21 16:27:34 +080080 if (PageDirty(page)) {
Sage Weil1d3576f2009-10-06 11:31:09 -070081 dout("%p set_page_dirty %p idx %lu -- already dirty\n",
82 mapping->host, page, page->index);
Sha Zhengju7d6e1f52013-08-21 16:27:34 +080083 BUG_ON(!PagePrivate(page));
Sage Weil1d3576f2009-10-06 11:31:09 -070084 return 0;
85 }
86
87 inode = mapping->host;
88 ci = ceph_inode(inode);
89
Sage Weil1d3576f2009-10-06 11:31:09 -070090 /* dirty the head */
Sage Weilbe655592011-11-30 09:47:09 -080091 spin_lock(&ci->i_ceph_lock);
Yan, Zheng5dda377c2015-04-30 14:40:54 +080092 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 Weil1d3576f2009-10-06 11:31:09 -0700105 if (ci->i_wrbuffer_ref == 0)
Dave Chinner0444d762011-03-29 18:08:50 +1100106 ihold(inode);
Sage Weil1d3576f2009-10-06 11:31:09 -0700107 ++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 Weilbe655592011-11-30 09:47:09 -0800114 spin_unlock(&ci->i_ceph_lock);
Sage Weil1d3576f2009-10-06 11:31:09 -0700115
Sha Zhengju7d6e1f52013-08-21 16:27:34 +0800116 /*
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 Weil1d3576f2009-10-06 11:31:09 -0700123
Sha Zhengju7d6e1f52013-08-21 16:27:34 +0800124 ret = __set_page_dirty_nobuffers(page);
125 WARN_ON(!PageLocked(page));
126 WARN_ON(!page->mapping);
Sage Weil1d3576f2009-10-06 11:31:09 -0700127
Sha Zhengju7d6e1f52013-08-21 16:27:34 +0800128 return ret;
Sage Weil1d3576f2009-10-06 11:31:09 -0700129}
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 Czernerd47992f2013-05-21 23:17:23 -0400136static void ceph_invalidatepage(struct page *page, unsigned int offset,
137 unsigned int length)
Sage Weil1d3576f2009-10-06 11:31:09 -0700138{
Alexander Beregalov4ce1e9a2010-02-22 17:17:44 +0300139 struct inode *inode;
Sage Weil1d3576f2009-10-06 11:31:09 -0700140 struct ceph_inode_info *ci;
Yan, Zheng61600ef2012-05-28 14:44:30 +0800141 struct ceph_snap_context *snapc = page_snap_context(page);
Sage Weil1d3576f2009-10-06 11:31:09 -0700142
Alexander Beregalov4ce1e9a2010-02-22 17:17:44 +0300143 inode = page->mapping->host;
Milosz Tanskib150f5c12013-08-09 12:59:55 -0400144 ci = ceph_inode(inode);
145
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300146 if (offset != 0 || length != PAGE_SIZE) {
Milosz Tanskib150f5c12013-08-09 12:59:55 -0400147 dout("%p invalidatepage %p idx %lu partial dirty page %u~%u\n",
148 inode, page, page->index, offset, length);
149 return;
150 }
Alexander Beregalov4ce1e9a2010-02-22 17:17:44 +0300151
Milosz Tanski99ccbd22013-08-21 17:29:54 -0400152 ceph_invalidate_fscache_page(inode, page);
153
154 if (!PagePrivate(page))
155 return;
156
Sage Weil1d3576f2009-10-06 11:31:09 -0700157 /*
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 Tanskib150f5c12013-08-09 12:59:55 -0400165 ClearPageChecked(page);
Sage Weil1d3576f2009-10-06 11:31:09 -0700166
Milosz Tanskib150f5c12013-08-09 12:59:55 -0400167 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 Weil1d3576f2009-10-06 11:31:09 -0700174}
175
Sage Weil1d3576f2009-10-06 11:31:09 -0700176static int ceph_releasepage(struct page *page, gfp_t g)
177{
NeilBrowne55f1a12016-08-31 12:59:29 +1000178 dout("%p releasepage %p idx %lu (%sdirty)\n", page->mapping->host,
179 page, page->index, PageDirty(page) ? "" : "not ");
Milosz Tanski99ccbd22013-08-21 17:29:54 -0400180
181 /* Can we release the page from the cache? */
182 if (!ceph_release_fscache_page(page, g))
183 return 0;
184
185 return !PagePrivate(page);
Sage Weil1d3576f2009-10-06 11:31:09 -0700186}
187
188/*
189 * read a single page, without unlocking it.
190 */
191static int readpage_nounlock(struct file *filp, struct page *page)
192{
Al Viro496ad9a2013-01-23 17:07:38 -0500193 struct inode *inode = file_inode(filp);
Sage Weil1d3576f2009-10-06 11:31:09 -0700194 struct ceph_inode_info *ci = ceph_inode(inode);
Milosz Tanski99ccbd22013-08-21 17:29:54 -0400195 struct ceph_osd_client *osdc =
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700196 &ceph_inode_to_client(inode)->client->osdc;
Sage Weil1d3576f2009-10-06 11:31:09 -0700197 int err = 0;
Yan, Zheng83701242014-11-14 22:36:18 +0800198 u64 off = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300199 u64 len = PAGE_SIZE;
Sage Weil1d3576f2009-10-06 11:31:09 -0700200
Yan, Zheng83701242014-11-14 22:36:18 +0800201 if (off >= i_size_read(inode)) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300202 zero_user_segment(page, 0, PAGE_SIZE);
Yan, Zheng83701242014-11-14 22:36:18 +0800203 SetPageUptodate(page);
204 return 0;
205 }
Milosz Tanski99ccbd22013-08-21 17:29:54 -0400206
Yan, Zhengfcc02d22015-01-10 11:43:12 +0800207 if (ci->i_inline_version != CEPH_INLINE_NONE) {
208 /*
209 * Uptodate inline data should have been added
210 * into page cache while getting Fcr caps.
211 */
212 if (off == 0)
213 return -EINVAL;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300214 zero_user_segment(page, 0, PAGE_SIZE);
Yan, Zhengfcc02d22015-01-10 11:43:12 +0800215 SetPageUptodate(page);
216 return 0;
217 }
Yan, Zheng83701242014-11-14 22:36:18 +0800218
219 err = ceph_readpage_from_fscache(inode, page);
Milosz Tanski99ccbd22013-08-21 17:29:54 -0400220 if (err == 0)
221 goto out;
222
Sage Weil1d3576f2009-10-06 11:31:09 -0700223 dout("readpage inode %p file %p page %p index %lu\n",
224 inode, filp, page, page->index);
225 err = ceph_osdc_readpages(osdc, ceph_vino(inode), &ci->i_layout,
Yan, Zheng83701242014-11-14 22:36:18 +0800226 off, &len,
Sage Weil1d3576f2009-10-06 11:31:09 -0700227 ci->i_truncate_seq, ci->i_truncate_size,
Sage Weilb7495fc2010-11-09 12:43:12 -0800228 &page, 1, 0);
Sage Weil1d3576f2009-10-06 11:31:09 -0700229 if (err == -ENOENT)
230 err = 0;
231 if (err < 0) {
232 SetPageError(page);
Li Wang18302802013-12-19 06:03:49 -0800233 ceph_fscache_readpage_cancel(inode, page);
Sage Weil1d3576f2009-10-06 11:31:09 -0700234 goto out;
Sage Weil1d3576f2009-10-06 11:31:09 -0700235 }
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300236 if (err < PAGE_SIZE)
Zhang Zhen23cd5732014-05-28 15:09:55 +0800237 /* zero fill remainder of page */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300238 zero_user_segment(page, err, PAGE_SIZE);
Zhang Zhen23cd5732014-05-28 15:09:55 +0800239 else
240 flush_dcache_page(page);
Sage Weil1d3576f2009-10-06 11:31:09 -0700241
Zhang Zhen23cd5732014-05-28 15:09:55 +0800242 SetPageUptodate(page);
243 ceph_readpage_to_fscache(inode, page);
Milosz Tanski99ccbd22013-08-21 17:29:54 -0400244
Sage Weil1d3576f2009-10-06 11:31:09 -0700245out:
246 return err < 0 ? err : 0;
247}
248
249static int ceph_readpage(struct file *filp, struct page *page)
250{
251 int r = readpage_nounlock(filp, page);
252 unlock_page(page);
253 return r;
254}
255
256/*
Sage Weil7c272192011-08-03 09:58:09 -0700257 * Finish an async read(ahead) op.
Sage Weil1d3576f2009-10-06 11:31:09 -0700258 */
Ilya Dryomov85e084f2016-04-28 16:07:24 +0200259static void finish_read(struct ceph_osd_request *req)
Sage Weil1d3576f2009-10-06 11:31:09 -0700260{
Sage Weil7c272192011-08-03 09:58:09 -0700261 struct inode *inode = req->r_inode;
Alex Elder87060c12013-04-03 01:28:58 -0500262 struct ceph_osd_data *osd_data;
Ilya Dryomov85e084f2016-04-28 16:07:24 +0200263 int rc = req->r_result <= 0 ? req->r_result : 0;
264 int bytes = req->r_result >= 0 ? req->r_result : 0;
Alex Eldere0c59482013-03-07 15:38:25 -0600265 int num_pages;
Sage Weil7c272192011-08-03 09:58:09 -0700266 int i;
267
Sage Weil7c272192011-08-03 09:58:09 -0700268 dout("finish_read %p req %p rc %d bytes %d\n", inode, req, rc, bytes);
269
270 /* unlock all pages, zeroing any data we didn't read */
Alex Elder406e2c92013-04-15 14:50:36 -0500271 osd_data = osd_req_op_extent_osd_data(req, 0);
Alex Elder87060c12013-04-03 01:28:58 -0500272 BUG_ON(osd_data->type != CEPH_OSD_DATA_TYPE_PAGES);
273 num_pages = calc_pages_for((u64)osd_data->alignment,
274 (u64)osd_data->length);
Alex Eldere0c59482013-03-07 15:38:25 -0600275 for (i = 0; i < num_pages; i++) {
Alex Elder87060c12013-04-03 01:28:58 -0500276 struct page *page = osd_data->pages[i];
Sage Weil7c272192011-08-03 09:58:09 -0700277
Yan, Zheng368e3582016-05-17 11:58:02 +0800278 if (rc < 0 && rc != -ENOENT) {
279 ceph_fscache_readpage_cancel(inode, page);
Li Wangf36132a2013-11-27 22:28:13 +0800280 goto unlock;
Yan, Zheng368e3582016-05-17 11:58:02 +0800281 }
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300282 if (bytes < (int)PAGE_SIZE) {
Sage Weil7c272192011-08-03 09:58:09 -0700283 /* zero (remainder of) page */
284 int s = bytes < 0 ? 0 : bytes;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300285 zero_user_segment(page, s, PAGE_SIZE);
Sage Weil7c272192011-08-03 09:58:09 -0700286 }
287 dout("finish_read %p uptodate %p idx %lu\n", inode, page,
288 page->index);
289 flush_dcache_page(page);
290 SetPageUptodate(page);
Milosz Tanski99ccbd22013-08-21 17:29:54 -0400291 ceph_readpage_to_fscache(inode, page);
Li Wangf36132a2013-11-27 22:28:13 +0800292unlock:
Sage Weil7c272192011-08-03 09:58:09 -0700293 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300294 put_page(page);
295 bytes -= PAGE_SIZE;
Sage Weil7c272192011-08-03 09:58:09 -0700296 }
Alex Elder87060c12013-04-03 01:28:58 -0500297 kfree(osd_data->pages);
Sage Weil7c272192011-08-03 09:58:09 -0700298}
299
300/*
301 * start an async read(ahead) operation. return nr_pages we submitted
302 * a read for on success, or negative error code.
303 */
Sage Weil0d66a482011-08-04 08:21:30 -0700304static int start_read(struct inode *inode, struct list_head *page_list, int max)
Sage Weil7c272192011-08-03 09:58:09 -0700305{
306 struct ceph_osd_client *osdc =
307 &ceph_inode_to_client(inode)->client->osdc;
308 struct ceph_inode_info *ci = ceph_inode(inode);
309 struct page *page = list_entry(page_list->prev, struct page, lru);
Alex Elderacead002013-03-14 14:09:05 -0500310 struct ceph_vino vino;
Sage Weil7c272192011-08-03 09:58:09 -0700311 struct ceph_osd_request *req;
312 u64 off;
313 u64 len;
314 int i;
Sage Weil1d3576f2009-10-06 11:31:09 -0700315 struct page **pages;
Sage Weil7c272192011-08-03 09:58:09 -0700316 pgoff_t next_index;
317 int nr_pages = 0;
Yan, Zheng2b1ac852016-10-25 10:51:55 +0800318 int got = 0;
319 int ret = 0;
320
321 if (!current->journal_info) {
322 /* caller of readpages does not hold buffer and read caps
323 * (fadvise, madvise and readahead cases) */
324 int want = CEPH_CAP_FILE_CACHE;
325 ret = ceph_try_get_caps(ci, CEPH_CAP_FILE_RD, want, &got);
326 if (ret < 0) {
327 dout("start_read %p, error getting cap\n", inode);
328 } else if (!(got & want)) {
329 dout("start_read %p, no cache cap\n", inode);
330 ret = 0;
331 }
332 if (ret <= 0) {
333 if (got)
334 ceph_put_cap_refs(ci, got);
335 while (!list_empty(page_list)) {
336 page = list_entry(page_list->prev,
337 struct page, lru);
338 list_del(&page->lru);
339 put_page(page);
340 }
341 return ret;
342 }
343 }
Sage Weil7c272192011-08-03 09:58:09 -0700344
Alex Elder6285bc22012-10-02 10:25:51 -0500345 off = (u64) page_offset(page);
Sage Weil7c272192011-08-03 09:58:09 -0700346
347 /* count pages */
348 next_index = page->index;
349 list_for_each_entry_reverse(page, page_list, lru) {
350 if (page->index != next_index)
351 break;
352 nr_pages++;
353 next_index++;
Sage Weil0d66a482011-08-04 08:21:30 -0700354 if (max && nr_pages == max)
355 break;
Sage Weil7c272192011-08-03 09:58:09 -0700356 }
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300357 len = nr_pages << PAGE_SHIFT;
Sage Weil7c272192011-08-03 09:58:09 -0700358 dout("start_read %p nr_pages %d is %lld~%lld\n", inode, nr_pages,
359 off, len);
Alex Elderacead002013-03-14 14:09:05 -0500360 vino = ceph_vino(inode);
361 req = ceph_osdc_new_request(osdc, &ci->i_layout, vino, off, &len,
Yan, Zheng715e4cd2014-11-13 14:40:37 +0800362 0, 1, CEPH_OSD_OP_READ,
Alex Elderacead002013-03-14 14:09:05 -0500363 CEPH_OSD_FLAG_READ, NULL,
Sage Weil7c272192011-08-03 09:58:09 -0700364 ci->i_truncate_seq, ci->i_truncate_size,
Alex Elderacead002013-03-14 14:09:05 -0500365 false);
Yan, Zheng2b1ac852016-10-25 10:51:55 +0800366 if (IS_ERR(req)) {
367 ret = PTR_ERR(req);
368 goto out;
369 }
Sage Weil1d3576f2009-10-06 11:31:09 -0700370
371 /* build page vector */
Alex Eldercf7b7e142013-03-01 18:00:15 -0600372 nr_pages = calc_pages_for(0, len);
Yan, Zheng687265e2015-06-13 17:27:05 +0800373 pages = kmalloc(sizeof(*pages) * nr_pages, GFP_KERNEL);
Yan, Zheng2b1ac852016-10-25 10:51:55 +0800374 if (!pages) {
375 ret = -ENOMEM;
376 goto out_put;
377 }
Sage Weil7c272192011-08-03 09:58:09 -0700378 for (i = 0; i < nr_pages; ++i) {
379 page = list_entry(page_list->prev, struct page, lru);
380 BUG_ON(PageLocked(page));
381 list_del(&page->lru);
Milosz Tanski99ccbd22013-08-21 17:29:54 -0400382
Sage Weil7c272192011-08-03 09:58:09 -0700383 dout("start_read %p adding %p idx %lu\n", inode, page,
384 page->index);
385 if (add_to_page_cache_lru(page, &inode->i_data, page->index,
Yan, Zheng687265e2015-06-13 17:27:05 +0800386 GFP_KERNEL)) {
Milosz Tanskid4d3aa32013-09-03 19:11:17 -0400387 ceph_fscache_uncache_page(inode, page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300388 put_page(page);
Sage Weil7c272192011-08-03 09:58:09 -0700389 dout("start_read %p add_to_page_cache failed %p\n",
390 inode, page);
391 nr_pages = i;
Yan, Zheng1afe4782016-08-24 11:33:46 +0800392 if (nr_pages > 0) {
393 len = nr_pages << PAGE_SHIFT;
Yan, Zhengd641df82017-01-19 11:21:29 +0800394 osd_req_op_extent_update(req, 0, len);
Yan, Zheng1afe4782016-08-24 11:33:46 +0800395 break;
396 }
Sage Weil7c272192011-08-03 09:58:09 -0700397 goto out_pages;
Sage Weil1d3576f2009-10-06 11:31:09 -0700398 }
Sage Weil7c272192011-08-03 09:58:09 -0700399 pages[i] = page;
Sage Weil1d3576f2009-10-06 11:31:09 -0700400 }
Alex Elder406e2c92013-04-15 14:50:36 -0500401 osd_req_op_extent_osd_data_pages(req, 0, pages, len, 0, false, false);
Sage Weil7c272192011-08-03 09:58:09 -0700402 req->r_callback = finish_read;
403 req->r_inode = inode;
404
405 dout("start_read %p starting %p %lld~%lld\n", inode, req, off, len);
406 ret = ceph_osdc_start_request(osdc, req, false);
407 if (ret < 0)
408 goto out_pages;
409 ceph_osdc_put_request(req);
Yan, Zheng2b1ac852016-10-25 10:51:55 +0800410
411 /* After adding locked pages to page cache, the inode holds cache cap.
412 * So we can drop our cap refs. */
413 if (got)
414 ceph_put_cap_refs(ci, got);
415
Sage Weil7c272192011-08-03 09:58:09 -0700416 return nr_pages;
417
418out_pages:
Yan, Zheng1afe4782016-08-24 11:33:46 +0800419 for (i = 0; i < nr_pages; ++i) {
420 ceph_fscache_readpage_cancel(inode, pages[i]);
421 unlock_page(pages[i]);
422 }
423 ceph_put_page_vector(pages, nr_pages, false);
Yan, Zheng2b1ac852016-10-25 10:51:55 +0800424out_put:
Sage Weil7c272192011-08-03 09:58:09 -0700425 ceph_osdc_put_request(req);
Yan, Zheng2b1ac852016-10-25 10:51:55 +0800426out:
427 if (got)
428 ceph_put_cap_refs(ci, got);
Sage Weil7c272192011-08-03 09:58:09 -0700429 return ret;
Sage Weil1d3576f2009-10-06 11:31:09 -0700430}
431
Sage Weil7c272192011-08-03 09:58:09 -0700432
Sage Weil1d3576f2009-10-06 11:31:09 -0700433/*
434 * Read multiple pages. Leave pages we don't read + unlock in page_list;
435 * the caller (VM) cleans them up.
436 */
437static int ceph_readpages(struct file *file, struct address_space *mapping,
438 struct list_head *page_list, unsigned nr_pages)
439{
Al Viro496ad9a2013-01-23 17:07:38 -0500440 struct inode *inode = file_inode(file);
Sage Weil0d66a482011-08-04 08:21:30 -0700441 struct ceph_fs_client *fsc = ceph_inode_to_client(inode);
Sage Weil1d3576f2009-10-06 11:31:09 -0700442 int rc = 0;
Sage Weil0d66a482011-08-04 08:21:30 -0700443 int max = 0;
Sage Weil1d3576f2009-10-06 11:31:09 -0700444
Yan, Zheng83701242014-11-14 22:36:18 +0800445 if (ceph_inode(inode)->i_inline_version != CEPH_INLINE_NONE)
446 return -EINVAL;
447
Milosz Tanski99ccbd22013-08-21 17:29:54 -0400448 rc = ceph_readpages_from_fscache(mapping->host, mapping, page_list,
449 &nr_pages);
450
451 if (rc == 0)
452 goto out;
453
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300454 if (fsc->mount_options->rsize >= PAGE_SIZE)
455 max = (fsc->mount_options->rsize + PAGE_SIZE - 1)
Sage Weil0d66a482011-08-04 08:21:30 -0700456 >> PAGE_SHIFT;
457
Alex Elder2794a822013-02-14 12:16:43 -0600458 dout("readpages %p file %p nr_pages %d max %d\n", inode,
459 file, nr_pages,
Sage Weil0d66a482011-08-04 08:21:30 -0700460 max);
Sage Weil7c272192011-08-03 09:58:09 -0700461 while (!list_empty(page_list)) {
Sage Weil0d66a482011-08-04 08:21:30 -0700462 rc = start_read(inode, page_list, max);
Sage Weil7c272192011-08-03 09:58:09 -0700463 if (rc < 0)
464 goto out;
Sage Weil1d3576f2009-10-06 11:31:09 -0700465 }
Sage Weil1d3576f2009-10-06 11:31:09 -0700466out:
Milosz Tanski76be7782013-08-21 17:30:27 -0400467 ceph_fscache_readpages_cancel(inode, page_list);
468
Sage Weil7c272192011-08-03 09:58:09 -0700469 dout("readpages %p file %p ret %d\n", inode, file, rc);
Sage Weil1d3576f2009-10-06 11:31:09 -0700470 return rc;
471}
472
473/*
474 * Get ref for the oldest snapc for an inode with dirty data... that is, the
475 * only snap context we are allowed to write back.
Sage Weil1d3576f2009-10-06 11:31:09 -0700476 */
Sage Weil6298a332010-03-31 22:01:38 -0700477static struct ceph_snap_context *get_oldest_context(struct inode *inode,
Yan, Zheng5f743e42016-11-15 16:04:37 +0800478 loff_t *snap_size,
479 u64 *truncate_size,
480 u32 *truncate_seq)
Sage Weil1d3576f2009-10-06 11:31:09 -0700481{
482 struct ceph_inode_info *ci = ceph_inode(inode);
483 struct ceph_snap_context *snapc = NULL;
484 struct ceph_cap_snap *capsnap = NULL;
485
Sage Weilbe655592011-11-30 09:47:09 -0800486 spin_lock(&ci->i_ceph_lock);
Sage Weil1d3576f2009-10-06 11:31:09 -0700487 list_for_each_entry(capsnap, &ci->i_cap_snaps, ci_item) {
488 dout(" cap_snap %p snapc %p has %d dirty pages\n", capsnap,
489 capsnap->context, capsnap->dirty_pages);
490 if (capsnap->dirty_pages) {
491 snapc = ceph_get_snap_context(capsnap->context);
492 if (snap_size)
493 *snap_size = capsnap->size;
Yan, Zheng5f743e42016-11-15 16:04:37 +0800494 if (truncate_size)
495 *truncate_size = capsnap->truncate_size;
496 if (truncate_seq)
497 *truncate_seq = capsnap->truncate_seq;
Sage Weil1d3576f2009-10-06 11:31:09 -0700498 break;
499 }
500 }
Sage Weil7d8cb262010-08-24 08:44:16 -0700501 if (!snapc && ci->i_wrbuffer_ref_head) {
Sage Weil80e755f2010-03-31 21:52:10 -0700502 snapc = ceph_get_snap_context(ci->i_head_snapc);
Sage Weil1d3576f2009-10-06 11:31:09 -0700503 dout(" head snapc %p has %d dirty pages\n",
504 snapc, ci->i_wrbuffer_ref_head);
Yan, Zheng5f743e42016-11-15 16:04:37 +0800505 if (truncate_size)
Geng, Jichao84fcc2d2017-01-05 16:50:39 +0800506 *truncate_size = ci->i_truncate_size;
Yan, Zheng5f743e42016-11-15 16:04:37 +0800507 if (truncate_seq)
Geng, Jichao84fcc2d2017-01-05 16:50:39 +0800508 *truncate_seq = ci->i_truncate_seq;
Sage Weil1d3576f2009-10-06 11:31:09 -0700509 }
Sage Weilbe655592011-11-30 09:47:09 -0800510 spin_unlock(&ci->i_ceph_lock);
Sage Weil1d3576f2009-10-06 11:31:09 -0700511 return snapc;
512}
513
514/*
515 * Write a single page, but leave the page locked.
516 *
517 * If we get a write error, set the page error bit, but still adjust the
518 * dirty page accounting (i.e., page is no longer dirty).
519 */
520static int writepage_nounlock(struct page *page, struct writeback_control *wbc)
521{
522 struct inode *inode;
523 struct ceph_inode_info *ci;
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700524 struct ceph_fs_client *fsc;
Sage Weil1d3576f2009-10-06 11:31:09 -0700525 struct ceph_osd_client *osdc;
Sage Weil6298a332010-03-31 22:01:38 -0700526 struct ceph_snap_context *snapc, *oldest;
Yan, Zhengfc2744a2013-05-31 16:48:29 +0800527 loff_t page_off = page_offset(page);
Yan, Zhenge1966b42015-06-18 03:10:58 +0800528 loff_t snap_size = -1;
Yehuda Sadeh2baba252009-12-18 13:51:57 -0800529 long writeback_stat;
Yan, Zhenge1966b42015-06-18 03:10:58 +0800530 u64 truncate_size;
Yan, Zhengfc2744a2013-05-31 16:48:29 +0800531 u32 truncate_seq;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300532 int err = 0, len = PAGE_SIZE;
Sage Weil1d3576f2009-10-06 11:31:09 -0700533
534 dout("writepage %p idx %lu\n", page, page->index);
535
536 if (!page->mapping || !page->mapping->host) {
537 dout("writepage %p - no mapping\n", page);
538 return -EFAULT;
539 }
540 inode = page->mapping->host;
541 ci = ceph_inode(inode);
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700542 fsc = ceph_inode_to_client(inode);
543 osdc = &fsc->client->osdc;
Sage Weil1d3576f2009-10-06 11:31:09 -0700544
545 /* verify this is a writeable snap context */
Yan, Zheng61600ef2012-05-28 14:44:30 +0800546 snapc = page_snap_context(page);
Sage Weil1d3576f2009-10-06 11:31:09 -0700547 if (snapc == NULL) {
548 dout("writepage %p page %p not dirty?\n", inode, page);
549 goto out;
550 }
Yan, Zheng5f743e42016-11-15 16:04:37 +0800551 oldest = get_oldest_context(inode, &snap_size,
552 &truncate_size, &truncate_seq);
Sage Weil6298a332010-03-31 22:01:38 -0700553 if (snapc->seq > oldest->seq) {
Sage Weil1d3576f2009-10-06 11:31:09 -0700554 dout("writepage %p page %p snapc %p not writeable - noop\n",
Yan, Zheng61600ef2012-05-28 14:44:30 +0800555 inode, page, snapc);
Sage Weil1d3576f2009-10-06 11:31:09 -0700556 /* we should only noop if called by kswapd */
557 WARN_ON((current->flags & PF_MEMALLOC) == 0);
Sage Weil6298a332010-03-31 22:01:38 -0700558 ceph_put_snap_context(oldest);
Sage Weil1d3576f2009-10-06 11:31:09 -0700559 goto out;
560 }
Sage Weil6298a332010-03-31 22:01:38 -0700561 ceph_put_snap_context(oldest);
Sage Weil1d3576f2009-10-06 11:31:09 -0700562
Yan, Zhenge1966b42015-06-18 03:10:58 +0800563 if (snap_size == -1)
Yan, Zhengfc2744a2013-05-31 16:48:29 +0800564 snap_size = i_size_read(inode);
Yan, Zhengfc2744a2013-05-31 16:48:29 +0800565
Sage Weil1d3576f2009-10-06 11:31:09 -0700566 /* is this a partial page at end of file? */
Yan, Zhengfc2744a2013-05-31 16:48:29 +0800567 if (page_off >= snap_size) {
568 dout("%p page eof %llu\n", page, snap_size);
569 goto out;
570 }
571 if (snap_size < page_off + len)
572 len = snap_size - page_off;
Sage Weil1d3576f2009-10-06 11:31:09 -0700573
Sage Weilae00d4f2010-09-16 16:26:51 -0700574 dout("writepage %p page %p index %lu on %llu~%u snapc %p\n",
575 inode, page, page->index, page_off, len, snapc);
Sage Weil1d3576f2009-10-06 11:31:09 -0700576
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700577 writeback_stat = atomic_long_inc_return(&fsc->writeback_count);
Yehuda Sadeh2baba252009-12-18 13:51:57 -0800578 if (writeback_stat >
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700579 CONGESTION_ON_THRESH(fsc->mount_options->congestion_kb))
580 set_bdi_congested(&fsc->backing_dev_info, BLK_RW_ASYNC);
Yehuda Sadeh2baba252009-12-18 13:51:57 -0800581
Sage Weil1d3576f2009-10-06 11:31:09 -0700582 set_page_writeback(page);
583 err = ceph_osdc_writepages(osdc, ceph_vino(inode),
584 &ci->i_layout, snapc,
585 page_off, len,
Yan, Zhengfc2744a2013-05-31 16:48:29 +0800586 truncate_seq, truncate_size,
Alex Elder24808822013-02-15 11:42:29 -0600587 &inode->i_mtime, &page, 1);
Sage Weil1d3576f2009-10-06 11:31:09 -0700588 if (err < 0) {
Yan, Zhengad15ec02016-05-13 17:29:51 +0800589 struct writeback_control tmp_wbc;
590 if (!wbc)
591 wbc = &tmp_wbc;
592 if (err == -ERESTARTSYS) {
593 /* killed by SIGKILL */
594 dout("writepage interrupted page %p\n", page);
595 redirty_page_for_writepage(wbc, page);
596 end_page_writeback(page);
597 goto out;
598 }
599 dout("writepage setting page/mapping error %d %p\n",
600 err, page);
Sage Weil1d3576f2009-10-06 11:31:09 -0700601 SetPageError(page);
602 mapping_set_error(&inode->i_data, err);
Yan, Zhengad15ec02016-05-13 17:29:51 +0800603 wbc->pages_skipped++;
Sage Weil1d3576f2009-10-06 11:31:09 -0700604 } else {
605 dout("writepage cleaned page %p\n", page);
606 err = 0; /* vfs expects us to return 0 */
607 }
608 page->private = 0;
609 ClearPagePrivate(page);
610 end_page_writeback(page);
611 ceph_put_wrbuffer_cap_refs(ci, 1, snapc);
Sage Weil6298a332010-03-31 22:01:38 -0700612 ceph_put_snap_context(snapc); /* page's reference */
Sage Weil1d3576f2009-10-06 11:31:09 -0700613out:
614 return err;
615}
616
617static int ceph_writepage(struct page *page, struct writeback_control *wbc)
618{
Yehuda Sadehdbd646a2009-12-16 14:51:06 -0800619 int err;
620 struct inode *inode = page->mapping->host;
621 BUG_ON(!inode);
Sage Weil70b666c2011-05-27 09:24:26 -0700622 ihold(inode);
Yehuda Sadehdbd646a2009-12-16 14:51:06 -0800623 err = writepage_nounlock(page, wbc);
Yan, Zhengad15ec02016-05-13 17:29:51 +0800624 if (err == -ERESTARTSYS) {
625 /* direct memory reclaimer was killed by SIGKILL. return 0
626 * to prevent caller from setting mapping/page error */
627 err = 0;
628 }
Sage Weil1d3576f2009-10-06 11:31:09 -0700629 unlock_page(page);
Yehuda Sadehdbd646a2009-12-16 14:51:06 -0800630 iput(inode);
Sage Weil1d3576f2009-10-06 11:31:09 -0700631 return err;
632}
633
Sage Weil1d3576f2009-10-06 11:31:09 -0700634/*
635 * lame release_pages helper. release_pages() isn't exported to
636 * modules.
637 */
638static void ceph_release_pages(struct page **pages, int num)
639{
640 struct pagevec pvec;
641 int i;
642
643 pagevec_init(&pvec, 0);
644 for (i = 0; i < num; i++) {
645 if (pagevec_add(&pvec, pages[i]) == 0)
646 pagevec_release(&pvec);
647 }
648 pagevec_release(&pvec);
649}
650
Sage Weil1d3576f2009-10-06 11:31:09 -0700651/*
652 * async writeback completion handler.
653 *
654 * If we get an error, set the mapping error bit, but not the individual
655 * page error bits.
656 */
Ilya Dryomov85e084f2016-04-28 16:07:24 +0200657static void writepages_finish(struct ceph_osd_request *req)
Sage Weil1d3576f2009-10-06 11:31:09 -0700658{
659 struct inode *inode = req->r_inode;
Sage Weil1d3576f2009-10-06 11:31:09 -0700660 struct ceph_inode_info *ci = ceph_inode(inode);
Alex Elder87060c12013-04-03 01:28:58 -0500661 struct ceph_osd_data *osd_data;
Sage Weil1d3576f2009-10-06 11:31:09 -0700662 struct page *page;
Yan, Zheng5b646402016-01-07 16:00:17 +0800663 int num_pages, total_pages = 0;
664 int i, j;
665 int rc = req->r_result;
Sage Weil1d3576f2009-10-06 11:31:09 -0700666 struct ceph_snap_context *snapc = req->r_snapc;
667 struct address_space *mapping = inode->i_mapping;
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700668 struct ceph_fs_client *fsc = ceph_inode_to_client(inode);
Yan, Zheng5b646402016-01-07 16:00:17 +0800669 bool remove_page;
Sage Weil1d3576f2009-10-06 11:31:09 -0700670
Yan, Zheng5b646402016-01-07 16:00:17 +0800671 dout("writepages_finish %p rc %d\n", inode, rc);
672 if (rc < 0)
Sage Weil1d3576f2009-10-06 11:31:09 -0700673 mapping_set_error(mapping, rc);
Yan, Zheng5b646402016-01-07 16:00:17 +0800674
675 /*
676 * We lost the cache cap, need to truncate the page before
677 * it is unlocked, otherwise we'd truncate it later in the
678 * page truncation thread, possibly losing some data that
679 * raced its way in
680 */
681 remove_page = !(ceph_caps_issued(ci) &
682 (CEPH_CAP_FILE_CACHE|CEPH_CAP_FILE_LAZYIO));
Sage Weil1d3576f2009-10-06 11:31:09 -0700683
684 /* clean all pages */
Yan, Zheng5b646402016-01-07 16:00:17 +0800685 for (i = 0; i < req->r_num_ops; i++) {
686 if (req->r_ops[i].op != CEPH_OSD_OP_WRITE)
687 break;
Sage Weil1d3576f2009-10-06 11:31:09 -0700688
Yan, Zheng5b646402016-01-07 16:00:17 +0800689 osd_data = osd_req_op_extent_osd_data(req, i);
690 BUG_ON(osd_data->type != CEPH_OSD_DATA_TYPE_PAGES);
691 num_pages = calc_pages_for((u64)osd_data->alignment,
692 (u64)osd_data->length);
693 total_pages += num_pages;
694 for (j = 0; j < num_pages; j++) {
695 page = osd_data->pages[j];
696 BUG_ON(!page);
697 WARN_ON(!PageUptodate(page));
Yehuda Sadeh2baba252009-12-18 13:51:57 -0800698
Yan, Zheng5b646402016-01-07 16:00:17 +0800699 if (atomic_long_dec_return(&fsc->writeback_count) <
700 CONGESTION_OFF_THRESH(
701 fsc->mount_options->congestion_kb))
702 clear_bdi_congested(&fsc->backing_dev_info,
703 BLK_RW_ASYNC);
Yehuda Sadehe63dc5c2010-02-19 00:07:01 +0000704
Yan, Zhengb109eec62016-05-13 17:54:17 +0800705 if (rc < 0)
706 SetPageError(page);
707
Yan, Zheng5b646402016-01-07 16:00:17 +0800708 ceph_put_snap_context(page_snap_context(page));
709 page->private = 0;
710 ClearPagePrivate(page);
711 dout("unlocking %p\n", page);
712 end_page_writeback(page);
Yehuda Sadehe63dc5c2010-02-19 00:07:01 +0000713
Yan, Zheng5b646402016-01-07 16:00:17 +0800714 if (remove_page)
715 generic_error_remove_page(inode->i_mapping,
716 page);
717
718 unlock_page(page);
719 }
720 dout("writepages_finish %p wrote %llu bytes cleaned %d pages\n",
721 inode, osd_data->length, rc >= 0 ? num_pages : 0);
722
723 ceph_release_pages(osd_data->pages, num_pages);
Sage Weil1d3576f2009-10-06 11:31:09 -0700724 }
Sage Weil1d3576f2009-10-06 11:31:09 -0700725
Yan, Zheng5b646402016-01-07 16:00:17 +0800726 ceph_put_wrbuffer_cap_refs(ci, total_pages, snapc);
727
728 osd_data = osd_req_op_extent_osd_data(req, 0);
Alex Elder87060c12013-04-03 01:28:58 -0500729 if (osd_data->pages_from_pool)
730 mempool_free(osd_data->pages,
Cheng Renquan640ef792010-03-26 17:40:33 +0800731 ceph_sb_to_client(inode->i_sb)->wb_pagevec_pool);
Sage Weil1d3576f2009-10-06 11:31:09 -0700732 else
Alex Elder87060c12013-04-03 01:28:58 -0500733 kfree(osd_data->pages);
Sage Weil1d3576f2009-10-06 11:31:09 -0700734 ceph_osdc_put_request(req);
735}
736
Sage Weil1d3576f2009-10-06 11:31:09 -0700737/*
738 * initiate async writeback
739 */
740static int ceph_writepages_start(struct address_space *mapping,
741 struct writeback_control *wbc)
742{
743 struct inode *inode = mapping->host;
Sage Weil1d3576f2009-10-06 11:31:09 -0700744 struct ceph_inode_info *ci = ceph_inode(inode);
Yan, Zhengfc2744a2013-05-31 16:48:29 +0800745 struct ceph_fs_client *fsc = ceph_inode_to_client(inode);
746 struct ceph_vino vino = ceph_vino(inode);
Sage Weil1d3576f2009-10-06 11:31:09 -0700747 pgoff_t index, start, end;
748 int range_whole = 0;
749 int should_loop = 1;
750 pgoff_t max_pages = 0, max_pages_ever = 0;
Sage Weil80e755f2010-03-31 21:52:10 -0700751 struct ceph_snap_context *snapc = NULL, *last_snapc = NULL, *pgsnapc;
Sage Weil1d3576f2009-10-06 11:31:09 -0700752 struct pagevec pvec;
753 int done = 0;
754 int rc = 0;
755 unsigned wsize = 1 << inode->i_blkbits;
756 struct ceph_osd_request *req = NULL;
Dan Carpenter021b77b2014-11-28 11:33:34 +0300757 int do_sync = 0;
Yan, Zhenge1966b42015-06-18 03:10:58 +0800758 loff_t snap_size, i_size;
759 u64 truncate_size;
Yan, Zhengfc2744a2013-05-31 16:48:29 +0800760 u32 truncate_seq;
Sage Weil1d3576f2009-10-06 11:31:09 -0700761
762 /*
763 * Include a 'sync' in the OSD request if this is a data
764 * integrity write (e.g., O_SYNC write or fsync()), or if our
765 * cap is being revoked.
766 */
majianpengc62988e2013-06-19 15:12:06 +0800767 if ((wbc->sync_mode == WB_SYNC_ALL) ||
768 ceph_caps_revoking(ci, CEPH_CAP_FILE_BUFFER))
Sage Weil1d3576f2009-10-06 11:31:09 -0700769 do_sync = 1;
770 dout("writepages_start %p dosync=%d (mode=%s)\n",
771 inode, do_sync,
772 wbc->sync_mode == WB_SYNC_NONE ? "NONE" :
773 (wbc->sync_mode == WB_SYNC_ALL ? "ALL" : "HOLD"));
774
Seraphime Kirkovski52953d52016-12-26 10:26:34 +0100775 if (READ_ONCE(fsc->mount_state) == CEPH_MOUNT_SHUTDOWN) {
Yan, Zheng6c93df52016-04-15 13:56:12 +0800776 if (ci->i_wrbuffer_ref > 0) {
777 pr_warn_ratelimited(
778 "writepage_start %p %lld forced umount\n",
779 inode, ceph_ino(inode));
780 }
Yan, Zhenga341d4d2015-07-01 17:03:23 +0800781 mapping_set_error(mapping, -EIO);
Sage Weil1d3576f2009-10-06 11:31:09 -0700782 return -EIO; /* we're in a forced umount, don't write! */
783 }
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700784 if (fsc->mount_options->wsize && fsc->mount_options->wsize < wsize)
785 wsize = fsc->mount_options->wsize;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300786 if (wsize < PAGE_SIZE)
787 wsize = PAGE_SIZE;
788 max_pages_ever = wsize >> PAGE_SHIFT;
Sage Weil1d3576f2009-10-06 11:31:09 -0700789
790 pagevec_init(&pvec, 0);
791
Sage Weil1d3576f2009-10-06 11:31:09 -0700792 /* where to start/end? */
793 if (wbc->range_cyclic) {
794 start = mapping->writeback_index; /* Start from prev offset */
795 end = -1;
796 dout(" cyclic, start at %lu\n", start);
797 } else {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300798 start = wbc->range_start >> PAGE_SHIFT;
799 end = wbc->range_end >> PAGE_SHIFT;
Sage Weil1d3576f2009-10-06 11:31:09 -0700800 if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX)
801 range_whole = 1;
802 should_loop = 0;
803 dout(" not cyclic, %lu to %lu\n", start, end);
804 }
805 index = start;
806
807retry:
808 /* find oldest snap context with dirty data */
809 ceph_put_snap_context(snapc);
Yan, Zhenge1966b42015-06-18 03:10:58 +0800810 snap_size = -1;
Yan, Zheng5f743e42016-11-15 16:04:37 +0800811 snapc = get_oldest_context(inode, &snap_size,
812 &truncate_size, &truncate_seq);
Sage Weil1d3576f2009-10-06 11:31:09 -0700813 if (!snapc) {
814 /* hmm, why does writepages get called when there
815 is no dirty data? */
816 dout(" no snap context with dirty data?\n");
817 goto out;
818 }
819 dout(" oldest snapc is %p seq %lld (%d snaps)\n",
820 snapc, snapc->seq, snapc->num_snaps);
Yan, Zhengfc2744a2013-05-31 16:48:29 +0800821
Yan, Zhenge1966b42015-06-18 03:10:58 +0800822 i_size = i_size_read(inode);
Yan, Zhengfc2744a2013-05-31 16:48:29 +0800823
Sage Weil1d3576f2009-10-06 11:31:09 -0700824 if (last_snapc && snapc != last_snapc) {
825 /* if we switched to a newer snapc, restart our scan at the
826 * start of the original file range. */
827 dout(" snapc differs from last pass, restarting at %lu\n",
828 index);
829 index = start;
830 }
831 last_snapc = snapc;
832
833 while (!done && index <= end) {
834 unsigned i;
835 int first;
Yan, Zheng5b646402016-01-07 16:00:17 +0800836 pgoff_t strip_unit_end = 0;
837 int num_ops = 0, op_idx;
838 int pvec_pages, locked_pages = 0;
839 struct page **pages = NULL, **data_pages;
Alex Eldere5975c72013-03-14 14:09:05 -0500840 mempool_t *pool = NULL; /* Becomes non-null if mempool used */
Sage Weil1d3576f2009-10-06 11:31:09 -0700841 struct page *page;
842 int want;
Yan, Zheng5b646402016-01-07 16:00:17 +0800843 u64 offset = 0, len = 0;
Sage Weil1d3576f2009-10-06 11:31:09 -0700844
Sage Weil1d3576f2009-10-06 11:31:09 -0700845 max_pages = max_pages_ever;
846
847get_more_pages:
848 first = -1;
849 want = min(end - index,
850 min((pgoff_t)PAGEVEC_SIZE,
851 max_pages - (pgoff_t)locked_pages) - 1)
852 + 1;
853 pvec_pages = pagevec_lookup_tag(&pvec, mapping, &index,
854 PAGECACHE_TAG_DIRTY,
855 want);
856 dout("pagevec_lookup_tag got %d\n", pvec_pages);
857 if (!pvec_pages && !locked_pages)
858 break;
859 for (i = 0; i < pvec_pages && locked_pages < max_pages; i++) {
860 page = pvec.pages[i];
861 dout("? %p idx %lu\n", page, page->index);
862 if (locked_pages == 0)
863 lock_page(page); /* first page */
864 else if (!trylock_page(page))
865 break;
866
867 /* only dirty pages, or our accounting breaks */
868 if (unlikely(!PageDirty(page)) ||
869 unlikely(page->mapping != mapping)) {
870 dout("!dirty or !mapping %p\n", page);
871 unlock_page(page);
872 break;
873 }
874 if (!wbc->range_cyclic && page->index > end) {
875 dout("end of range %p\n", page);
876 done = 1;
877 unlock_page(page);
878 break;
879 }
Yan, Zheng5b646402016-01-07 16:00:17 +0800880 if (strip_unit_end && (page->index > strip_unit_end)) {
881 dout("end of strip unit %p\n", page);
Sage Weil1d3576f2009-10-06 11:31:09 -0700882 unlock_page(page);
883 break;
884 }
885 if (wbc->sync_mode != WB_SYNC_NONE) {
886 dout("waiting on writeback %p\n", page);
887 wait_on_page_writeback(page);
888 }
Yan, Zhenge1966b42015-06-18 03:10:58 +0800889 if (page_offset(page) >=
890 (snap_size == -1 ? i_size : snap_size)) {
891 dout("%p page eof %llu\n", page,
892 (snap_size == -1 ? i_size : snap_size));
Sage Weil1d3576f2009-10-06 11:31:09 -0700893 done = 1;
894 unlock_page(page);
895 break;
896 }
897 if (PageWriteback(page)) {
898 dout("%p under writeback\n", page);
899 unlock_page(page);
900 break;
901 }
902
903 /* only if matching snap context */
Yan, Zheng61600ef2012-05-28 14:44:30 +0800904 pgsnapc = page_snap_context(page);
Sage Weil80e755f2010-03-31 21:52:10 -0700905 if (pgsnapc->seq > snapc->seq) {
906 dout("page snapc %p %lld > oldest %p %lld\n",
907 pgsnapc, pgsnapc->seq, snapc, snapc->seq);
Sage Weil1d3576f2009-10-06 11:31:09 -0700908 unlock_page(page);
909 if (!locked_pages)
910 continue; /* keep looking for snap */
911 break;
912 }
913
914 if (!clear_page_dirty_for_io(page)) {
915 dout("%p !clear_page_dirty_for_io\n", page);
916 unlock_page(page);
917 break;
918 }
919
Alex Eldere5975c72013-03-14 14:09:05 -0500920 /*
921 * We have something to write. If this is
922 * the first locked page this time through,
Yan, Zheng5b646402016-01-07 16:00:17 +0800923 * calculate max possinle write size and
924 * allocate a page array
Alex Eldere5975c72013-03-14 14:09:05 -0500925 */
Sage Weil1d3576f2009-10-06 11:31:09 -0700926 if (locked_pages == 0) {
Yan, Zheng5b646402016-01-07 16:00:17 +0800927 u64 objnum;
928 u64 objoff;
929
Sage Weil1d3576f2009-10-06 11:31:09 -0700930 /* prepare async write request */
Alex Eldere5975c72013-03-14 14:09:05 -0500931 offset = (u64)page_offset(page);
Sage Weil1d3576f2009-10-06 11:31:09 -0700932 len = wsize;
Yan, Zheng5b646402016-01-07 16:00:17 +0800933
934 rc = ceph_calc_file_object_mapping(&ci->i_layout,
935 offset, len,
936 &objnum, &objoff,
937 &len);
938 if (rc < 0) {
Henry C Chang8c718972011-05-03 09:45:16 +0000939 unlock_page(page);
940 break;
941 }
942
Yan, Zheng5b646402016-01-07 16:00:17 +0800943 num_ops = 1 + do_sync;
944 strip_unit_end = page->index +
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300945 ((len - 1) >> PAGE_SHIFT);
Yan, Zheng715e4cd2014-11-13 14:40:37 +0800946
Yan, Zheng5b646402016-01-07 16:00:17 +0800947 BUG_ON(pages);
Alex Elder88486952013-03-14 14:09:05 -0500948 max_pages = calc_pages_for(0, (u64)len);
Yan, Zhengfc2744a2013-05-31 16:48:29 +0800949 pages = kmalloc(max_pages * sizeof (*pages),
950 GFP_NOFS);
Alex Elder88486952013-03-14 14:09:05 -0500951 if (!pages) {
952 pool = fsc->wb_pagevec_pool;
Alex Elder88486952013-03-14 14:09:05 -0500953 pages = mempool_alloc(pool, GFP_NOFS);
Alex Eldere5975c72013-03-14 14:09:05 -0500954 BUG_ON(!pages);
Alex Elder88486952013-03-14 14:09:05 -0500955 }
Yan, Zheng5b646402016-01-07 16:00:17 +0800956
957 len = 0;
958 } else if (page->index !=
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300959 (offset + len) >> PAGE_SHIFT) {
Yan, Zheng5b646402016-01-07 16:00:17 +0800960 if (num_ops >= (pool ? CEPH_OSD_SLAB_OPS :
961 CEPH_OSD_MAX_OPS)) {
962 redirty_page_for_writepage(wbc, page);
963 unlock_page(page);
964 break;
965 }
966
967 num_ops++;
968 offset = (u64)page_offset(page);
969 len = 0;
Sage Weil1d3576f2009-10-06 11:31:09 -0700970 }
971
972 /* note position of first page in pvec */
973 if (first < 0)
974 first = i;
975 dout("%p will write page %p idx %lu\n",
976 inode, page, page->index);
Yehuda Sadeh2baba252009-12-18 13:51:57 -0800977
Yan, Zheng5b646402016-01-07 16:00:17 +0800978 if (atomic_long_inc_return(&fsc->writeback_count) >
979 CONGESTION_ON_THRESH(
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700980 fsc->mount_options->congestion_kb)) {
981 set_bdi_congested(&fsc->backing_dev_info,
Sage Weil213c99e2010-08-03 10:25:11 -0700982 BLK_RW_ASYNC);
Yehuda Sadeh2baba252009-12-18 13:51:57 -0800983 }
984
Alex Eldere5975c72013-03-14 14:09:05 -0500985 pages[locked_pages] = page;
Sage Weil1d3576f2009-10-06 11:31:09 -0700986 locked_pages++;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300987 len += PAGE_SIZE;
Sage Weil1d3576f2009-10-06 11:31:09 -0700988 }
989
990 /* did we get anything? */
991 if (!locked_pages)
992 goto release_pvec_pages;
993 if (i) {
994 int j;
995 BUG_ON(!locked_pages || first < 0);
996
997 if (pvec_pages && i == pvec_pages &&
998 locked_pages < max_pages) {
999 dout("reached end pvec, trying for more\n");
1000 pagevec_reinit(&pvec);
1001 goto get_more_pages;
1002 }
1003
1004 /* shift unused pages over in the pvec... we
1005 * will need to release them below. */
1006 for (j = i; j < pvec_pages; j++) {
Yan, Zheng5b646402016-01-07 16:00:17 +08001007 dout(" pvec leftover page %p\n", pvec.pages[j]);
Sage Weil1d3576f2009-10-06 11:31:09 -07001008 pvec.pages[j-i+first] = pvec.pages[j];
1009 }
1010 pvec.nr -= i-first;
1011 }
1012
Yan, Zheng5b646402016-01-07 16:00:17 +08001013new_request:
Alex Eldere5975c72013-03-14 14:09:05 -05001014 offset = page_offset(pages[0]);
Yan, Zheng5b646402016-01-07 16:00:17 +08001015 len = wsize;
1016
1017 req = ceph_osdc_new_request(&fsc->client->osdc,
1018 &ci->i_layout, vino,
1019 offset, &len, 0, num_ops,
1020 CEPH_OSD_OP_WRITE,
Ilya Dryomov54ea0042017-02-11 18:48:41 +01001021 CEPH_OSD_FLAG_WRITE,
Yan, Zheng5b646402016-01-07 16:00:17 +08001022 snapc, truncate_seq,
1023 truncate_size, false);
1024 if (IS_ERR(req)) {
1025 req = ceph_osdc_new_request(&fsc->client->osdc,
1026 &ci->i_layout, vino,
1027 offset, &len, 0,
1028 min(num_ops,
1029 CEPH_OSD_SLAB_OPS),
1030 CEPH_OSD_OP_WRITE,
Ilya Dryomov54ea0042017-02-11 18:48:41 +01001031 CEPH_OSD_FLAG_WRITE,
Yan, Zheng5b646402016-01-07 16:00:17 +08001032 snapc, truncate_seq,
1033 truncate_size, true);
1034 BUG_ON(IS_ERR(req));
Yan, Zhenge1966b42015-06-18 03:10:58 +08001035 }
Yan, Zheng5b646402016-01-07 16:00:17 +08001036 BUG_ON(len < page_offset(pages[locked_pages - 1]) +
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001037 PAGE_SIZE - offset);
Sage Weil1d3576f2009-10-06 11:31:09 -07001038
Yan, Zheng5b646402016-01-07 16:00:17 +08001039 req->r_callback = writepages_finish;
1040 req->r_inode = inode;
1041
1042 /* Format the osd request message and submit the write */
1043 len = 0;
1044 data_pages = pages;
1045 op_idx = 0;
1046 for (i = 0; i < locked_pages; i++) {
1047 u64 cur_offset = page_offset(pages[i]);
1048 if (offset + len != cur_offset) {
1049 if (op_idx + do_sync + 1 == req->r_num_ops)
1050 break;
1051 osd_req_op_extent_dup_last(req, op_idx,
1052 cur_offset - offset);
1053 dout("writepages got pages at %llu~%llu\n",
1054 offset, len);
1055 osd_req_op_extent_osd_data_pages(req, op_idx,
1056 data_pages, len, 0,
Alex Eldera4ce40a2013-04-05 01:27:12 -05001057 !!pool, false);
Yan, Zheng5b646402016-01-07 16:00:17 +08001058 osd_req_op_extent_update(req, op_idx, len);
Alex Eldere5975c72013-03-14 14:09:05 -05001059
Yan, Zheng5b646402016-01-07 16:00:17 +08001060 len = 0;
1061 offset = cur_offset;
1062 data_pages = pages + i;
1063 op_idx++;
1064 }
1065
1066 set_page_writeback(pages[i]);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001067 len += PAGE_SIZE;
Yan, Zheng5b646402016-01-07 16:00:17 +08001068 }
1069
1070 if (snap_size != -1) {
1071 len = min(len, snap_size - offset);
1072 } else if (i == locked_pages) {
1073 /* writepages_finish() clears writeback pages
1074 * according to the data length, so make sure
1075 * data length covers all locked pages */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001076 u64 min_len = len + 1 - PAGE_SIZE;
Yan, Zheng5b646402016-01-07 16:00:17 +08001077 len = min(len, (u64)i_size_read(inode) - offset);
1078 len = max(len, min_len);
1079 }
1080 dout("writepages got pages at %llu~%llu\n", offset, len);
1081
1082 osd_req_op_extent_osd_data_pages(req, op_idx, data_pages, len,
1083 0, !!pool, false);
1084 osd_req_op_extent_update(req, op_idx, len);
1085
1086 if (do_sync) {
1087 op_idx++;
1088 osd_req_op_init(req, op_idx, CEPH_OSD_OP_STARTSYNC, 0);
1089 }
1090 BUG_ON(op_idx + 1 != req->r_num_ops);
1091
Alex Eldere5975c72013-03-14 14:09:05 -05001092 pool = NULL;
Yan, Zheng5b646402016-01-07 16:00:17 +08001093 if (i < locked_pages) {
1094 BUG_ON(num_ops <= req->r_num_ops);
1095 num_ops -= req->r_num_ops;
1096 num_ops += do_sync;
1097 locked_pages -= i;
Alex Eldere5975c72013-03-14 14:09:05 -05001098
Yan, Zheng5b646402016-01-07 16:00:17 +08001099 /* allocate new pages array for next request */
1100 data_pages = pages;
1101 pages = kmalloc(locked_pages * sizeof (*pages),
1102 GFP_NOFS);
1103 if (!pages) {
1104 pool = fsc->wb_pagevec_pool;
1105 pages = mempool_alloc(pool, GFP_NOFS);
1106 BUG_ON(!pages);
1107 }
1108 memcpy(pages, data_pages + i,
1109 locked_pages * sizeof(*pages));
1110 memset(data_pages + i, 0,
1111 locked_pages * sizeof(*pages));
1112 } else {
1113 BUG_ON(num_ops != req->r_num_ops);
1114 index = pages[i - 1]->index + 1;
1115 /* request message now owns the pages array */
1116 pages = NULL;
1117 }
Alex Eldere5975c72013-03-14 14:09:05 -05001118
Ilya Dryomovbb873b5392016-05-26 00:29:52 +02001119 req->r_mtime = inode->i_mtime;
Sage Weil9d6fcb02011-05-12 15:48:16 -07001120 rc = ceph_osdc_start_request(&fsc->client->osdc, req, true);
1121 BUG_ON(rc);
Sage Weil1d3576f2009-10-06 11:31:09 -07001122 req = NULL;
1123
Yan, Zheng5b646402016-01-07 16:00:17 +08001124 wbc->nr_to_write -= i;
1125 if (pages)
1126 goto new_request;
1127
Sage Weil1d3576f2009-10-06 11:31:09 -07001128 if (wbc->nr_to_write <= 0)
1129 done = 1;
1130
1131release_pvec_pages:
1132 dout("pagevec_release on %d pages (%p)\n", (int)pvec.nr,
1133 pvec.nr ? pvec.pages[0] : NULL);
1134 pagevec_release(&pvec);
1135
1136 if (locked_pages && !done)
1137 goto retry;
1138 }
1139
1140 if (should_loop && !done) {
1141 /* more to do; loop back to beginning of file */
1142 dout("writepages looping back to beginning of file\n");
1143 should_loop = 0;
1144 index = 0;
1145 goto retry;
1146 }
1147
1148 if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0))
1149 mapping->writeback_index = index;
1150
1151out:
Ilya Dryomov3ed97d62016-04-26 15:05:29 +02001152 ceph_osdc_put_request(req);
Sage Weil1d3576f2009-10-06 11:31:09 -07001153 ceph_put_snap_context(snapc);
1154 dout("writepages done, rc = %d\n", rc);
Sage Weil1d3576f2009-10-06 11:31:09 -07001155 return rc;
1156}
1157
1158
1159
1160/*
1161 * See if a given @snapc is either writeable, or already written.
1162 */
1163static int context_is_writeable_or_written(struct inode *inode,
1164 struct ceph_snap_context *snapc)
1165{
Yan, Zheng5f743e42016-11-15 16:04:37 +08001166 struct ceph_snap_context *oldest = get_oldest_context(inode, NULL,
1167 NULL, NULL);
Sage Weil6298a332010-03-31 22:01:38 -07001168 int ret = !oldest || snapc->seq <= oldest->seq;
1169
1170 ceph_put_snap_context(oldest);
1171 return ret;
Sage Weil1d3576f2009-10-06 11:31:09 -07001172}
1173
1174/*
1175 * We are only allowed to write into/dirty the page if the page is
1176 * clean, or already dirty within the same snap context.
Sage Weil8f883c22010-03-19 13:27:53 -07001177 *
1178 * called with page locked.
1179 * return success with page locked,
1180 * or any failure (incl -EAGAIN) with page unlocked.
Sage Weil1d3576f2009-10-06 11:31:09 -07001181 */
Yehuda Sadeh4af6b222010-02-09 11:02:51 -08001182static int ceph_update_writeable_page(struct file *file,
1183 loff_t pos, unsigned len,
1184 struct page *page)
Sage Weil1d3576f2009-10-06 11:31:09 -07001185{
Al Viro496ad9a2013-01-23 17:07:38 -05001186 struct inode *inode = file_inode(file);
Yan, Zheng6c93df52016-04-15 13:56:12 +08001187 struct ceph_fs_client *fsc = ceph_inode_to_client(inode);
Sage Weil1d3576f2009-10-06 11:31:09 -07001188 struct ceph_inode_info *ci = ceph_inode(inode);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001189 loff_t page_off = pos & PAGE_MASK;
1190 int pos_in_page = pos & ~PAGE_MASK;
Sage Weil1d3576f2009-10-06 11:31:09 -07001191 int end_in_page = pos_in_page + len;
1192 loff_t i_size;
Sage Weil1d3576f2009-10-06 11:31:09 -07001193 int r;
Sage Weil80e755f2010-03-31 21:52:10 -07001194 struct ceph_snap_context *snapc, *oldest;
Sage Weil1d3576f2009-10-06 11:31:09 -07001195
Seraphime Kirkovski52953d52016-12-26 10:26:34 +01001196 if (READ_ONCE(fsc->mount_state) == CEPH_MOUNT_SHUTDOWN) {
Yan, Zheng6c93df52016-04-15 13:56:12 +08001197 dout(" page %p forced umount\n", page);
1198 unlock_page(page);
1199 return -EIO;
1200 }
1201
Sage Weil1d3576f2009-10-06 11:31:09 -07001202retry_locked:
1203 /* writepages currently holds page lock, but if we change that later, */
1204 wait_on_page_writeback(page);
1205
Yan, Zheng61600ef2012-05-28 14:44:30 +08001206 snapc = page_snap_context(page);
Sage Weil80e755f2010-03-31 21:52:10 -07001207 if (snapc && snapc != ci->i_head_snapc) {
Sage Weil1d3576f2009-10-06 11:31:09 -07001208 /*
1209 * this page is already dirty in another (older) snap
1210 * context! is it writeable now?
1211 */
Yan, Zheng5f743e42016-11-15 16:04:37 +08001212 oldest = get_oldest_context(inode, NULL, NULL, NULL);
Sage Weil1d3576f2009-10-06 11:31:09 -07001213
Sage Weil80e755f2010-03-31 21:52:10 -07001214 if (snapc->seq > oldest->seq) {
Sage Weil6298a332010-03-31 22:01:38 -07001215 ceph_put_snap_context(oldest);
Sage Weil1d3576f2009-10-06 11:31:09 -07001216 dout(" page %p snapc %p not current or oldest\n",
Sage Weil6298a332010-03-31 22:01:38 -07001217 page, snapc);
Sage Weil1d3576f2009-10-06 11:31:09 -07001218 /*
1219 * queue for writeback, and wait for snapc to
1220 * be writeable or written
1221 */
Sage Weil6298a332010-03-31 22:01:38 -07001222 snapc = ceph_get_snap_context(snapc);
Sage Weil1d3576f2009-10-06 11:31:09 -07001223 unlock_page(page);
Sage Weil3c6f6b72010-02-09 15:24:44 -08001224 ceph_queue_writeback(inode);
Yan, Zhenga78bbd42016-05-13 11:30:24 +08001225 r = wait_event_killable(ci->i_cap_wq,
Sage Weil1d3576f2009-10-06 11:31:09 -07001226 context_is_writeable_or_written(inode, snapc));
1227 ceph_put_snap_context(snapc);
Sage Weil8f883c22010-03-19 13:27:53 -07001228 if (r == -ERESTARTSYS)
1229 return r;
Yehuda Sadeh4af6b222010-02-09 11:02:51 -08001230 return -EAGAIN;
Sage Weil1d3576f2009-10-06 11:31:09 -07001231 }
Sage Weil6298a332010-03-31 22:01:38 -07001232 ceph_put_snap_context(oldest);
Sage Weil1d3576f2009-10-06 11:31:09 -07001233
1234 /* yay, writeable, do it now (without dropping page lock) */
1235 dout(" page %p snapc %p not current, but oldest\n",
1236 page, snapc);
1237 if (!clear_page_dirty_for_io(page))
1238 goto retry_locked;
1239 r = writepage_nounlock(page, NULL);
1240 if (r < 0)
1241 goto fail_nosnap;
1242 goto retry_locked;
1243 }
1244
1245 if (PageUptodate(page)) {
1246 dout(" page %p already uptodate\n", page);
1247 return 0;
1248 }
1249
1250 /* full page? */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001251 if (pos_in_page == 0 && len == PAGE_SIZE)
Sage Weil1d3576f2009-10-06 11:31:09 -07001252 return 0;
1253
1254 /* past end of file? */
Yan, Zheng99c88e62015-12-30 11:32:46 +08001255 i_size = i_size_read(inode);
Sage Weil1d3576f2009-10-06 11:31:09 -07001256
Sage Weil1d3576f2009-10-06 11:31:09 -07001257 if (page_off >= i_size ||
1258 (pos_in_page == 0 && (pos+len) >= i_size &&
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001259 end_in_page - pos_in_page != PAGE_SIZE)) {
Sage Weil1d3576f2009-10-06 11:31:09 -07001260 dout(" zeroing %p 0 - %d and %d - %d\n",
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001261 page, pos_in_page, end_in_page, (int)PAGE_SIZE);
Sage Weil1d3576f2009-10-06 11:31:09 -07001262 zero_user_segments(page,
1263 0, pos_in_page,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001264 end_in_page, PAGE_SIZE);
Sage Weil1d3576f2009-10-06 11:31:09 -07001265 return 0;
1266 }
1267
1268 /* we need to read it. */
Sage Weil1d3576f2009-10-06 11:31:09 -07001269 r = readpage_nounlock(file, page);
1270 if (r < 0)
1271 goto fail_nosnap;
1272 goto retry_locked;
Sage Weil1d3576f2009-10-06 11:31:09 -07001273fail_nosnap:
1274 unlock_page(page);
1275 return r;
1276}
1277
1278/*
Yehuda Sadeh4af6b222010-02-09 11:02:51 -08001279 * We are only allowed to write into/dirty the page if the page is
1280 * clean, or already dirty within the same snap context.
1281 */
1282static int ceph_write_begin(struct file *file, struct address_space *mapping,
1283 loff_t pos, unsigned len, unsigned flags,
1284 struct page **pagep, void **fsdata)
1285{
Al Viro496ad9a2013-01-23 17:07:38 -05001286 struct inode *inode = file_inode(file);
Yehuda Sadeh4af6b222010-02-09 11:02:51 -08001287 struct page *page;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001288 pgoff_t index = pos >> PAGE_SHIFT;
Sage Weil7971bd92013-05-01 21:15:58 -07001289 int r;
Yehuda Sadeh4af6b222010-02-09 11:02:51 -08001290
1291 do {
Sage Weil8f883c22010-03-19 13:27:53 -07001292 /* get a page */
Yehuda Sadeh4af6b222010-02-09 11:02:51 -08001293 page = grab_cache_page_write_begin(mapping, index, 0);
Sage Weil7971bd92013-05-01 21:15:58 -07001294 if (!page)
1295 return -ENOMEM;
Yehuda Sadeh4af6b222010-02-09 11:02:51 -08001296
1297 dout("write_begin file %p inode %p page %p %d~%d\n", file,
Sage Weil213c99e2010-08-03 10:25:11 -07001298 inode, page, (int)pos, (int)len);
Yehuda Sadeh4af6b222010-02-09 11:02:51 -08001299
1300 r = ceph_update_writeable_page(file, pos, len, page);
Taesoo Kimc1d00b22015-03-20 17:36:56 -04001301 if (r < 0)
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001302 put_page(page);
Taesoo Kimc1d00b22015-03-20 17:36:56 -04001303 else
1304 *pagep = page;
Yehuda Sadeh4af6b222010-02-09 11:02:51 -08001305 } while (r == -EAGAIN);
1306
1307 return r;
1308}
1309
1310/*
Sage Weil1d3576f2009-10-06 11:31:09 -07001311 * we don't do anything in here that simple_write_end doesn't do
Yan, Zheng5dda377c2015-04-30 14:40:54 +08001312 * except adjust dirty page accounting
Sage Weil1d3576f2009-10-06 11:31:09 -07001313 */
1314static int ceph_write_end(struct file *file, struct address_space *mapping,
1315 loff_t pos, unsigned len, unsigned copied,
1316 struct page *page, void *fsdata)
1317{
Al Viro496ad9a2013-01-23 17:07:38 -05001318 struct inode *inode = file_inode(file);
Sage Weil1d3576f2009-10-06 11:31:09 -07001319 int check_cap = 0;
1320
1321 dout("write_end file %p inode %p page %p %d~%d (%d)\n", file,
1322 inode, page, (int)pos, (int)copied, (int)len);
1323
1324 /* zero the stale part of the page if we did a short copy */
Al Virob9de3132016-09-05 22:20:03 -04001325 if (!PageUptodate(page)) {
1326 if (copied < len) {
1327 copied = 0;
1328 goto out;
1329 }
1330 SetPageUptodate(page);
1331 }
Sage Weil1d3576f2009-10-06 11:31:09 -07001332
1333 /* did file size increase? */
Yan, Zheng99c88e62015-12-30 11:32:46 +08001334 if (pos+copied > i_size_read(inode))
Sage Weil1d3576f2009-10-06 11:31:09 -07001335 check_cap = ceph_inode_set_size(inode, pos+copied);
1336
Sage Weil1d3576f2009-10-06 11:31:09 -07001337 set_page_dirty(page);
1338
Al Virob9de3132016-09-05 22:20:03 -04001339out:
Sage Weil1d3576f2009-10-06 11:31:09 -07001340 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001341 put_page(page);
Sage Weil1d3576f2009-10-06 11:31:09 -07001342
1343 if (check_cap)
1344 ceph_check_caps(ceph_inode(inode), CHECK_CAPS_AUTHONLY, NULL);
1345
1346 return copied;
1347}
1348
1349/*
1350 * we set .direct_IO to indicate direct io is supported, but since we
1351 * intercept O_DIRECT reads and writes early, this function should
1352 * never get called.
1353 */
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07001354static ssize_t ceph_direct_io(struct kiocb *iocb, struct iov_iter *iter)
Sage Weil1d3576f2009-10-06 11:31:09 -07001355{
1356 WARN_ON(1);
1357 return -EINVAL;
1358}
1359
1360const struct address_space_operations ceph_aops = {
1361 .readpage = ceph_readpage,
1362 .readpages = ceph_readpages,
1363 .writepage = ceph_writepage,
1364 .writepages = ceph_writepages_start,
1365 .write_begin = ceph_write_begin,
1366 .write_end = ceph_write_end,
1367 .set_page_dirty = ceph_set_page_dirty,
1368 .invalidatepage = ceph_invalidatepage,
1369 .releasepage = ceph_releasepage,
1370 .direct_IO = ceph_direct_io,
1371};
1372
Yan, Zheng4f7e89f2016-05-10 18:40:28 +08001373static void ceph_block_sigs(sigset_t *oldset)
1374{
1375 sigset_t mask;
1376 siginitsetinv(&mask, sigmask(SIGKILL));
1377 sigprocmask(SIG_BLOCK, &mask, oldset);
1378}
1379
1380static void ceph_restore_sigs(sigset_t *oldset)
1381{
1382 sigprocmask(SIG_SETMASK, oldset, NULL);
1383}
Sage Weil1d3576f2009-10-06 11:31:09 -07001384
1385/*
1386 * vm ops
1387 */
Yan, Zheng61f68812013-11-28 14:28:14 +08001388static int ceph_filemap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
1389{
1390 struct inode *inode = file_inode(vma->vm_file);
1391 struct ceph_inode_info *ci = ceph_inode(inode);
1392 struct ceph_file_info *fi = vma->vm_file->private_data;
Yan, Zheng3738daa2014-11-14 22:10:07 +08001393 struct page *pinned_page = NULL;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001394 loff_t off = vmf->pgoff << PAGE_SHIFT;
Yan, Zheng61f68812013-11-28 14:28:14 +08001395 int want, got, ret;
Yan, Zheng4f7e89f2016-05-10 18:40:28 +08001396 sigset_t oldset;
1397
1398 ceph_block_sigs(&oldset);
Yan, Zheng61f68812013-11-28 14:28:14 +08001399
1400 dout("filemap_fault %p %llx.%llx %llu~%zd trying to get caps\n",
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001401 inode, ceph_vinop(inode), off, (size_t)PAGE_SIZE);
Yan, Zheng61f68812013-11-28 14:28:14 +08001402 if (fi->fmode & CEPH_FILE_MODE_LAZY)
1403 want = CEPH_CAP_FILE_CACHE | CEPH_CAP_FILE_LAZYIO;
1404 else
1405 want = CEPH_CAP_FILE_CACHE;
Yan, Zheng4f7e89f2016-05-10 18:40:28 +08001406
1407 got = 0;
1408 ret = ceph_get_caps(ci, CEPH_CAP_FILE_RD, want, -1, &got, &pinned_page);
Yan, Zheng6ce026e2016-05-10 18:59:13 +08001409 if (ret < 0)
Yan, Zheng4f7e89f2016-05-10 18:40:28 +08001410 goto out_restore;
Yan, Zheng6ce026e2016-05-10 18:59:13 +08001411
Yan, Zheng61f68812013-11-28 14:28:14 +08001412 dout("filemap_fault %p %llu~%zd got cap refs on %s\n",
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001413 inode, off, (size_t)PAGE_SIZE, ceph_cap_string(got));
Yan, Zheng61f68812013-11-28 14:28:14 +08001414
Yan, Zheng83701242014-11-14 22:36:18 +08001415 if ((got & (CEPH_CAP_FILE_CACHE | CEPH_CAP_FILE_LAZYIO)) ||
Yan, Zheng2b1ac852016-10-25 10:51:55 +08001416 ci->i_inline_version == CEPH_INLINE_NONE) {
1417 current->journal_info = vma->vm_file;
Yan, Zheng83701242014-11-14 22:36:18 +08001418 ret = filemap_fault(vma, vmf);
Yan, Zheng2b1ac852016-10-25 10:51:55 +08001419 current->journal_info = NULL;
1420 } else
Yan, Zheng83701242014-11-14 22:36:18 +08001421 ret = -EAGAIN;
Yan, Zheng61f68812013-11-28 14:28:14 +08001422
1423 dout("filemap_fault %p %llu~%zd dropping cap refs on %s ret %d\n",
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001424 inode, off, (size_t)PAGE_SIZE, ceph_cap_string(got), ret);
Yan, Zheng3738daa2014-11-14 22:10:07 +08001425 if (pinned_page)
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001426 put_page(pinned_page);
Yan, Zheng61f68812013-11-28 14:28:14 +08001427 ceph_put_cap_refs(ci, got);
1428
Yan, Zheng83701242014-11-14 22:36:18 +08001429 if (ret != -EAGAIN)
Yan, Zheng4f7e89f2016-05-10 18:40:28 +08001430 goto out_restore;
Yan, Zheng83701242014-11-14 22:36:18 +08001431
1432 /* read inline data */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001433 if (off >= PAGE_SIZE) {
Yan, Zheng83701242014-11-14 22:36:18 +08001434 /* does not support inline data > PAGE_SIZE */
1435 ret = VM_FAULT_SIGBUS;
1436 } else {
1437 int ret1;
1438 struct address_space *mapping = inode->i_mapping;
1439 struct page *page = find_or_create_page(mapping, 0,
Michal Hockoc62d2552015-11-06 16:28:49 -08001440 mapping_gfp_constraint(mapping,
1441 ~__GFP_FS));
Yan, Zheng83701242014-11-14 22:36:18 +08001442 if (!page) {
1443 ret = VM_FAULT_OOM;
Yan, Zheng4f7e89f2016-05-10 18:40:28 +08001444 goto out_inline;
Yan, Zheng83701242014-11-14 22:36:18 +08001445 }
1446 ret1 = __ceph_do_getattr(inode, page,
1447 CEPH_STAT_CAP_INLINE_DATA, true);
1448 if (ret1 < 0 || off >= i_size_read(inode)) {
1449 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001450 put_page(page);
Yan, Zheng6ce026e2016-05-10 18:59:13 +08001451 if (ret1 < 0)
1452 ret = ret1;
1453 else
1454 ret = VM_FAULT_SIGBUS;
Yan, Zheng4f7e89f2016-05-10 18:40:28 +08001455 goto out_inline;
Yan, Zheng83701242014-11-14 22:36:18 +08001456 }
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001457 if (ret1 < PAGE_SIZE)
1458 zero_user_segment(page, ret1, PAGE_SIZE);
Yan, Zheng83701242014-11-14 22:36:18 +08001459 else
1460 flush_dcache_page(page);
1461 SetPageUptodate(page);
1462 vmf->page = page;
1463 ret = VM_FAULT_MAJOR | VM_FAULT_LOCKED;
Yan, Zheng4f7e89f2016-05-10 18:40:28 +08001464out_inline:
1465 dout("filemap_fault %p %llu~%zd read inline data ret %d\n",
1466 inode, off, (size_t)PAGE_SIZE, ret);
Yan, Zheng83701242014-11-14 22:36:18 +08001467 }
Yan, Zheng4f7e89f2016-05-10 18:40:28 +08001468out_restore:
1469 ceph_restore_sigs(&oldset);
Yan, Zheng6ce026e2016-05-10 18:59:13 +08001470 if (ret < 0)
1471 ret = (ret == -ENOMEM) ? VM_FAULT_OOM : VM_FAULT_SIGBUS;
1472
Yan, Zheng61f68812013-11-28 14:28:14 +08001473 return ret;
1474}
Sage Weil1d3576f2009-10-06 11:31:09 -07001475
1476/*
1477 * Reuse write_begin here for simplicity.
1478 */
1479static int ceph_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
1480{
Al Viro496ad9a2013-01-23 17:07:38 -05001481 struct inode *inode = file_inode(vma->vm_file);
Yan, Zheng61f68812013-11-28 14:28:14 +08001482 struct ceph_inode_info *ci = ceph_inode(inode);
1483 struct ceph_file_info *fi = vma->vm_file->private_data;
Yan, Zhengf66fd9f2015-06-10 17:26:13 +08001484 struct ceph_cap_flush *prealloc_cf;
Yan, Zheng61f68812013-11-28 14:28:14 +08001485 struct page *page = vmf->page;
Alex Elder6285bc22012-10-02 10:25:51 -05001486 loff_t off = page_offset(page);
Yan, Zheng61f68812013-11-28 14:28:14 +08001487 loff_t size = i_size_read(inode);
1488 size_t len;
1489 int want, got, ret;
Yan, Zheng4f7e89f2016-05-10 18:40:28 +08001490 sigset_t oldset;
Sage Weil1d3576f2009-10-06 11:31:09 -07001491
Yan, Zhengf66fd9f2015-06-10 17:26:13 +08001492 prealloc_cf = ceph_alloc_cap_flush();
1493 if (!prealloc_cf)
Yan, Zheng6ce026e2016-05-10 18:59:13 +08001494 return VM_FAULT_OOM;
Yan, Zhengf66fd9f2015-06-10 17:26:13 +08001495
Yan, Zheng4f7e89f2016-05-10 18:40:28 +08001496 ceph_block_sigs(&oldset);
Sage Weil1d3576f2009-10-06 11:31:09 -07001497
Yan, Zheng28127bd2014-11-14 22:38:29 +08001498 if (ci->i_inline_version != CEPH_INLINE_NONE) {
1499 struct page *locked_page = NULL;
1500 if (off == 0) {
1501 lock_page(page);
1502 locked_page = page;
1503 }
1504 ret = ceph_uninline_data(vma->vm_file, locked_page);
1505 if (locked_page)
1506 unlock_page(locked_page);
Yan, Zheng6ce026e2016-05-10 18:59:13 +08001507 if (ret < 0)
Yan, Zhengf66fd9f2015-06-10 17:26:13 +08001508 goto out_free;
Yan, Zheng28127bd2014-11-14 22:38:29 +08001509 }
1510
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001511 if (off + PAGE_SIZE <= size)
1512 len = PAGE_SIZE;
Sage Weil1d3576f2009-10-06 11:31:09 -07001513 else
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001514 len = size & ~PAGE_MASK;
Sage Weil1d3576f2009-10-06 11:31:09 -07001515
Yan, Zheng61f68812013-11-28 14:28:14 +08001516 dout("page_mkwrite %p %llx.%llx %llu~%zd getting caps i_size %llu\n",
1517 inode, ceph_vinop(inode), off, len, size);
1518 if (fi->fmode & CEPH_FILE_MODE_LAZY)
1519 want = CEPH_CAP_FILE_BUFFER | CEPH_CAP_FILE_LAZYIO;
1520 else
1521 want = CEPH_CAP_FILE_BUFFER;
Yan, Zheng4f7e89f2016-05-10 18:40:28 +08001522
1523 got = 0;
1524 ret = ceph_get_caps(ci, CEPH_CAP_FILE_WR, want, off + len,
1525 &got, NULL);
Yan, Zheng6ce026e2016-05-10 18:59:13 +08001526 if (ret < 0)
Yan, Zheng4f7e89f2016-05-10 18:40:28 +08001527 goto out_free;
Yan, Zheng6ce026e2016-05-10 18:59:13 +08001528
Yan, Zheng61f68812013-11-28 14:28:14 +08001529 dout("page_mkwrite %p %llu~%zd got cap refs on %s\n",
1530 inode, off, len, ceph_cap_string(got));
1531
1532 /* Update time before taking page lock */
1533 file_update_time(vma->vm_file);
Yehuda Sadeh4af6b222010-02-09 11:02:51 -08001534
Yan, Zhengf0b33df2016-05-10 19:09:06 +08001535 do {
1536 lock_page(page);
Yehuda Sadeh4af6b222010-02-09 11:02:51 -08001537
Yan, Zhengf0b33df2016-05-10 19:09:06 +08001538 if ((off > size) || (page->mapping != inode->i_mapping)) {
1539 unlock_page(page);
1540 ret = VM_FAULT_NOPAGE;
1541 break;
1542 }
Yehuda Sadeh4af6b222010-02-09 11:02:51 -08001543
Yan, Zhengf0b33df2016-05-10 19:09:06 +08001544 ret = ceph_update_writeable_page(vma->vm_file, off, len, page);
1545 if (ret >= 0) {
1546 /* success. we'll keep the page locked. */
1547 set_page_dirty(page);
1548 ret = VM_FAULT_LOCKED;
1549 }
1550 } while (ret == -EAGAIN);
1551
Yan, Zheng28127bd2014-11-14 22:38:29 +08001552 if (ret == VM_FAULT_LOCKED ||
1553 ci->i_inline_version != CEPH_INLINE_NONE) {
Yan, Zheng61f68812013-11-28 14:28:14 +08001554 int dirty;
1555 spin_lock(&ci->i_ceph_lock);
Yan, Zheng28127bd2014-11-14 22:38:29 +08001556 ci->i_inline_version = CEPH_INLINE_NONE;
Yan, Zhengf66fd9f2015-06-10 17:26:13 +08001557 dirty = __ceph_mark_dirty_caps(ci, CEPH_CAP_FILE_WR,
1558 &prealloc_cf);
Yan, Zheng61f68812013-11-28 14:28:14 +08001559 spin_unlock(&ci->i_ceph_lock);
1560 if (dirty)
1561 __mark_inode_dirty(inode, dirty);
1562 }
1563
1564 dout("page_mkwrite %p %llu~%zd dropping cap refs on %s ret %d\n",
1565 inode, off, len, ceph_cap_string(got), ret);
1566 ceph_put_cap_refs(ci, got);
Yan, Zhengf66fd9f2015-06-10 17:26:13 +08001567out_free:
Yan, Zheng4f7e89f2016-05-10 18:40:28 +08001568 ceph_restore_sigs(&oldset);
Yan, Zhengf66fd9f2015-06-10 17:26:13 +08001569 ceph_free_cap_flush(prealloc_cf);
Yan, Zheng6ce026e2016-05-10 18:59:13 +08001570 if (ret < 0)
1571 ret = (ret == -ENOMEM) ? VM_FAULT_OOM : VM_FAULT_SIGBUS;
Sage Weil1d3576f2009-10-06 11:31:09 -07001572 return ret;
1573}
1574
Yan, Zheng31c542a2014-11-14 21:41:55 +08001575void ceph_fill_inline_data(struct inode *inode, struct page *locked_page,
1576 char *data, size_t len)
1577{
1578 struct address_space *mapping = inode->i_mapping;
1579 struct page *page;
1580
1581 if (locked_page) {
1582 page = locked_page;
1583 } else {
1584 if (i_size_read(inode) == 0)
1585 return;
1586 page = find_or_create_page(mapping, 0,
Michal Hockoc62d2552015-11-06 16:28:49 -08001587 mapping_gfp_constraint(mapping,
1588 ~__GFP_FS));
Yan, Zheng31c542a2014-11-14 21:41:55 +08001589 if (!page)
1590 return;
1591 if (PageUptodate(page)) {
1592 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001593 put_page(page);
Yan, Zheng31c542a2014-11-14 21:41:55 +08001594 return;
1595 }
1596 }
1597
Ilya Dryomov0668ff52014-12-19 13:10:10 +03001598 dout("fill_inline_data %p %llx.%llx len %zu locked_page %p\n",
Yan, Zheng31c542a2014-11-14 21:41:55 +08001599 inode, ceph_vinop(inode), len, locked_page);
1600
1601 if (len > 0) {
1602 void *kaddr = kmap_atomic(page);
1603 memcpy(kaddr, data, len);
1604 kunmap_atomic(kaddr);
1605 }
1606
1607 if (page != locked_page) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001608 if (len < PAGE_SIZE)
1609 zero_user_segment(page, len, PAGE_SIZE);
Yan, Zheng31c542a2014-11-14 21:41:55 +08001610 else
1611 flush_dcache_page(page);
1612
1613 SetPageUptodate(page);
1614 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001615 put_page(page);
Yan, Zheng31c542a2014-11-14 21:41:55 +08001616 }
1617}
1618
Yan, Zheng28127bd2014-11-14 22:38:29 +08001619int ceph_uninline_data(struct file *filp, struct page *locked_page)
1620{
1621 struct inode *inode = file_inode(filp);
1622 struct ceph_inode_info *ci = ceph_inode(inode);
1623 struct ceph_fs_client *fsc = ceph_inode_to_client(inode);
1624 struct ceph_osd_request *req;
1625 struct page *page = NULL;
1626 u64 len, inline_version;
1627 int err = 0;
1628 bool from_pagecache = false;
1629
1630 spin_lock(&ci->i_ceph_lock);
1631 inline_version = ci->i_inline_version;
1632 spin_unlock(&ci->i_ceph_lock);
1633
1634 dout("uninline_data %p %llx.%llx inline_version %llu\n",
1635 inode, ceph_vinop(inode), inline_version);
1636
1637 if (inline_version == 1 || /* initial version, no data */
1638 inline_version == CEPH_INLINE_NONE)
1639 goto out;
1640
1641 if (locked_page) {
1642 page = locked_page;
1643 WARN_ON(!PageUptodate(page));
1644 } else if (ceph_caps_issued(ci) &
1645 (CEPH_CAP_FILE_CACHE|CEPH_CAP_FILE_LAZYIO)) {
1646 page = find_get_page(inode->i_mapping, 0);
1647 if (page) {
1648 if (PageUptodate(page)) {
1649 from_pagecache = true;
1650 lock_page(page);
1651 } else {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001652 put_page(page);
Yan, Zheng28127bd2014-11-14 22:38:29 +08001653 page = NULL;
1654 }
1655 }
1656 }
1657
1658 if (page) {
1659 len = i_size_read(inode);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001660 if (len > PAGE_SIZE)
1661 len = PAGE_SIZE;
Yan, Zheng28127bd2014-11-14 22:38:29 +08001662 } else {
1663 page = __page_cache_alloc(GFP_NOFS);
1664 if (!page) {
1665 err = -ENOMEM;
1666 goto out;
1667 }
1668 err = __ceph_do_getattr(inode, page,
1669 CEPH_STAT_CAP_INLINE_DATA, true);
1670 if (err < 0) {
1671 /* no inline data */
1672 if (err == -ENODATA)
1673 err = 0;
1674 goto out;
1675 }
1676 len = err;
1677 }
1678
1679 req = ceph_osdc_new_request(&fsc->client->osdc, &ci->i_layout,
1680 ceph_vino(inode), 0, &len, 0, 1,
Ilya Dryomov54ea0042017-02-11 18:48:41 +01001681 CEPH_OSD_OP_CREATE, CEPH_OSD_FLAG_WRITE,
Ilya Dryomov34b759b2016-02-16 15:00:24 +01001682 NULL, 0, 0, false);
Yan, Zheng28127bd2014-11-14 22:38:29 +08001683 if (IS_ERR(req)) {
1684 err = PTR_ERR(req);
1685 goto out;
1686 }
1687
Ilya Dryomovbb873b5392016-05-26 00:29:52 +02001688 req->r_mtime = inode->i_mtime;
Yan, Zheng28127bd2014-11-14 22:38:29 +08001689 err = ceph_osdc_start_request(&fsc->client->osdc, req, false);
1690 if (!err)
1691 err = ceph_osdc_wait_request(&fsc->client->osdc, req);
1692 ceph_osdc_put_request(req);
1693 if (err < 0)
1694 goto out;
1695
1696 req = ceph_osdc_new_request(&fsc->client->osdc, &ci->i_layout,
1697 ceph_vino(inode), 0, &len, 1, 3,
Ilya Dryomov54ea0042017-02-11 18:48:41 +01001698 CEPH_OSD_OP_WRITE, CEPH_OSD_FLAG_WRITE,
Ilya Dryomov34b759b2016-02-16 15:00:24 +01001699 NULL, ci->i_truncate_seq,
1700 ci->i_truncate_size, false);
Yan, Zheng28127bd2014-11-14 22:38:29 +08001701 if (IS_ERR(req)) {
1702 err = PTR_ERR(req);
1703 goto out;
1704 }
1705
1706 osd_req_op_extent_osd_data_pages(req, 1, &page, len, 0, false, false);
1707
Yan, Zhengec137c12015-04-13 11:25:07 +08001708 {
1709 __le64 xattr_buf = cpu_to_le64(inline_version);
1710 err = osd_req_op_xattr_init(req, 0, CEPH_OSD_OP_CMPXATTR,
1711 "inline_version", &xattr_buf,
1712 sizeof(xattr_buf),
1713 CEPH_OSD_CMPXATTR_OP_GT,
1714 CEPH_OSD_CMPXATTR_MODE_U64);
1715 if (err)
1716 goto out_put;
1717 }
Yan, Zheng28127bd2014-11-14 22:38:29 +08001718
Yan, Zhengec137c12015-04-13 11:25:07 +08001719 {
1720 char xattr_buf[32];
1721 int xattr_len = snprintf(xattr_buf, sizeof(xattr_buf),
1722 "%llu", inline_version);
1723 err = osd_req_op_xattr_init(req, 2, CEPH_OSD_OP_SETXATTR,
1724 "inline_version",
1725 xattr_buf, xattr_len, 0, 0);
1726 if (err)
1727 goto out_put;
1728 }
Yan, Zheng28127bd2014-11-14 22:38:29 +08001729
Ilya Dryomovbb873b5392016-05-26 00:29:52 +02001730 req->r_mtime = inode->i_mtime;
Yan, Zheng28127bd2014-11-14 22:38:29 +08001731 err = ceph_osdc_start_request(&fsc->client->osdc, req, false);
1732 if (!err)
1733 err = ceph_osdc_wait_request(&fsc->client->osdc, req);
1734out_put:
1735 ceph_osdc_put_request(req);
1736 if (err == -ECANCELED)
1737 err = 0;
1738out:
1739 if (page && page != locked_page) {
1740 if (from_pagecache) {
1741 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001742 put_page(page);
Yan, Zheng28127bd2014-11-14 22:38:29 +08001743 } else
1744 __free_pages(page, 0);
1745 }
1746
1747 dout("uninline_data %p %llx.%llx inline_version %llu = %d\n",
1748 inode, ceph_vinop(inode), inline_version, err);
1749 return err;
1750}
1751
Kirill A. Shutemov7cbea8d2015-09-09 15:39:26 -07001752static const struct vm_operations_struct ceph_vmops = {
Yan, Zheng61f68812013-11-28 14:28:14 +08001753 .fault = ceph_filemap_fault,
Sage Weil1d3576f2009-10-06 11:31:09 -07001754 .page_mkwrite = ceph_page_mkwrite,
1755};
1756
1757int ceph_mmap(struct file *file, struct vm_area_struct *vma)
1758{
1759 struct address_space *mapping = file->f_mapping;
1760
1761 if (!mapping->a_ops->readpage)
1762 return -ENOEXEC;
1763 file_accessed(file);
1764 vma->vm_ops = &ceph_vmops;
Sage Weil1d3576f2009-10-06 11:31:09 -07001765 return 0;
1766}
Yan, Zheng10183a62015-04-27 15:33:28 +08001767
1768enum {
1769 POOL_READ = 1,
1770 POOL_WRITE = 2,
1771};
1772
Yan, Zheng779fe0f2016-03-07 09:35:06 +08001773static int __ceph_pool_perm_get(struct ceph_inode_info *ci,
1774 s64 pool, struct ceph_string *pool_ns)
Yan, Zheng10183a62015-04-27 15:33:28 +08001775{
1776 struct ceph_fs_client *fsc = ceph_inode_to_client(&ci->vfs_inode);
1777 struct ceph_mds_client *mdsc = fsc->mdsc;
1778 struct ceph_osd_request *rd_req = NULL, *wr_req = NULL;
1779 struct rb_node **p, *parent;
1780 struct ceph_pool_perm *perm;
1781 struct page **pages;
Yan, Zheng779fe0f2016-03-07 09:35:06 +08001782 size_t pool_ns_len;
Yan, Zheng10183a62015-04-27 15:33:28 +08001783 int err = 0, err2 = 0, have = 0;
1784
1785 down_read(&mdsc->pool_perm_rwsem);
1786 p = &mdsc->pool_perm_tree.rb_node;
1787 while (*p) {
1788 perm = rb_entry(*p, struct ceph_pool_perm, node);
1789 if (pool < perm->pool)
1790 p = &(*p)->rb_left;
1791 else if (pool > perm->pool)
1792 p = &(*p)->rb_right;
1793 else {
Yan, Zheng779fe0f2016-03-07 09:35:06 +08001794 int ret = ceph_compare_string(pool_ns,
1795 perm->pool_ns,
1796 perm->pool_ns_len);
1797 if (ret < 0)
1798 p = &(*p)->rb_left;
1799 else if (ret > 0)
1800 p = &(*p)->rb_right;
1801 else {
1802 have = perm->perm;
1803 break;
1804 }
Yan, Zheng10183a62015-04-27 15:33:28 +08001805 }
1806 }
1807 up_read(&mdsc->pool_perm_rwsem);
1808 if (*p)
1809 goto out;
1810
Yan, Zheng779fe0f2016-03-07 09:35:06 +08001811 if (pool_ns)
1812 dout("__ceph_pool_perm_get pool %lld ns %.*s no perm cached\n",
1813 pool, (int)pool_ns->len, pool_ns->str);
1814 else
1815 dout("__ceph_pool_perm_get pool %lld no perm cached\n", pool);
Yan, Zheng10183a62015-04-27 15:33:28 +08001816
1817 down_write(&mdsc->pool_perm_rwsem);
Yan, Zheng779fe0f2016-03-07 09:35:06 +08001818 p = &mdsc->pool_perm_tree.rb_node;
Yan, Zheng10183a62015-04-27 15:33:28 +08001819 parent = NULL;
1820 while (*p) {
1821 parent = *p;
1822 perm = rb_entry(parent, struct ceph_pool_perm, node);
1823 if (pool < perm->pool)
1824 p = &(*p)->rb_left;
1825 else if (pool > perm->pool)
1826 p = &(*p)->rb_right;
1827 else {
Yan, Zheng779fe0f2016-03-07 09:35:06 +08001828 int ret = ceph_compare_string(pool_ns,
1829 perm->pool_ns,
1830 perm->pool_ns_len);
1831 if (ret < 0)
1832 p = &(*p)->rb_left;
1833 else if (ret > 0)
1834 p = &(*p)->rb_right;
1835 else {
1836 have = perm->perm;
1837 break;
1838 }
Yan, Zheng10183a62015-04-27 15:33:28 +08001839 }
1840 }
1841 if (*p) {
1842 up_write(&mdsc->pool_perm_rwsem);
1843 goto out;
1844 }
1845
Ilya Dryomov34b759b2016-02-16 15:00:24 +01001846 rd_req = ceph_osdc_alloc_request(&fsc->client->osdc, NULL,
Yan, Zheng10183a62015-04-27 15:33:28 +08001847 1, false, GFP_NOFS);
1848 if (!rd_req) {
1849 err = -ENOMEM;
1850 goto out_unlock;
1851 }
1852
1853 rd_req->r_flags = CEPH_OSD_FLAG_READ;
1854 osd_req_op_init(rd_req, 0, CEPH_OSD_OP_STAT, 0);
1855 rd_req->r_base_oloc.pool = pool;
Yan, Zheng779fe0f2016-03-07 09:35:06 +08001856 if (pool_ns)
1857 rd_req->r_base_oloc.pool_ns = ceph_get_string(pool_ns);
Ilya Dryomovd30291b2016-04-29 19:54:20 +02001858 ceph_oid_printf(&rd_req->r_base_oid, "%llx.00000000", ci->i_vino.ino);
Yan, Zheng10183a62015-04-27 15:33:28 +08001859
Ilya Dryomov13d1ad12016-04-27 14:15:51 +02001860 err = ceph_osdc_alloc_messages(rd_req, GFP_NOFS);
1861 if (err)
1862 goto out_unlock;
Yan, Zheng10183a62015-04-27 15:33:28 +08001863
Ilya Dryomov34b759b2016-02-16 15:00:24 +01001864 wr_req = ceph_osdc_alloc_request(&fsc->client->osdc, NULL,
Yan, Zheng10183a62015-04-27 15:33:28 +08001865 1, false, GFP_NOFS);
1866 if (!wr_req) {
1867 err = -ENOMEM;
1868 goto out_unlock;
1869 }
1870
Ilya Dryomov54ea0042017-02-11 18:48:41 +01001871 wr_req->r_flags = CEPH_OSD_FLAG_WRITE;
Yan, Zheng10183a62015-04-27 15:33:28 +08001872 osd_req_op_init(wr_req, 0, CEPH_OSD_OP_CREATE, CEPH_OSD_OP_FLAG_EXCL);
Ilya Dryomov63244fa2016-04-28 16:07:23 +02001873 ceph_oloc_copy(&wr_req->r_base_oloc, &rd_req->r_base_oloc);
Ilya Dryomovd30291b2016-04-29 19:54:20 +02001874 ceph_oid_copy(&wr_req->r_base_oid, &rd_req->r_base_oid);
Yan, Zheng10183a62015-04-27 15:33:28 +08001875
Ilya Dryomov13d1ad12016-04-27 14:15:51 +02001876 err = ceph_osdc_alloc_messages(wr_req, GFP_NOFS);
1877 if (err)
1878 goto out_unlock;
Yan, Zheng10183a62015-04-27 15:33:28 +08001879
1880 /* one page should be large enough for STAT data */
1881 pages = ceph_alloc_page_vector(1, GFP_KERNEL);
1882 if (IS_ERR(pages)) {
1883 err = PTR_ERR(pages);
1884 goto out_unlock;
1885 }
1886
1887 osd_req_op_raw_data_in_pages(rd_req, 0, pages, PAGE_SIZE,
1888 0, false, true);
Yan, Zheng10183a62015-04-27 15:33:28 +08001889 err = ceph_osdc_start_request(&fsc->client->osdc, rd_req, false);
1890
Ilya Dryomovbb873b5392016-05-26 00:29:52 +02001891 wr_req->r_mtime = ci->vfs_inode.i_mtime;
Yan, Zheng10183a62015-04-27 15:33:28 +08001892 err2 = ceph_osdc_start_request(&fsc->client->osdc, wr_req, false);
1893
1894 if (!err)
1895 err = ceph_osdc_wait_request(&fsc->client->osdc, rd_req);
1896 if (!err2)
1897 err2 = ceph_osdc_wait_request(&fsc->client->osdc, wr_req);
1898
1899 if (err >= 0 || err == -ENOENT)
1900 have |= POOL_READ;
1901 else if (err != -EPERM)
1902 goto out_unlock;
1903
1904 if (err2 == 0 || err2 == -EEXIST)
1905 have |= POOL_WRITE;
1906 else if (err2 != -EPERM) {
1907 err = err2;
1908 goto out_unlock;
1909 }
1910
Yan, Zheng779fe0f2016-03-07 09:35:06 +08001911 pool_ns_len = pool_ns ? pool_ns->len : 0;
1912 perm = kmalloc(sizeof(*perm) + pool_ns_len + 1, GFP_NOFS);
Yan, Zheng10183a62015-04-27 15:33:28 +08001913 if (!perm) {
1914 err = -ENOMEM;
1915 goto out_unlock;
1916 }
1917
1918 perm->pool = pool;
1919 perm->perm = have;
Yan, Zheng779fe0f2016-03-07 09:35:06 +08001920 perm->pool_ns_len = pool_ns_len;
1921 if (pool_ns_len > 0)
1922 memcpy(perm->pool_ns, pool_ns->str, pool_ns_len);
1923 perm->pool_ns[pool_ns_len] = 0;
1924
Yan, Zheng10183a62015-04-27 15:33:28 +08001925 rb_link_node(&perm->node, parent, p);
1926 rb_insert_color(&perm->node, &mdsc->pool_perm_tree);
1927 err = 0;
1928out_unlock:
1929 up_write(&mdsc->pool_perm_rwsem);
1930
Ilya Dryomov3ed97d62016-04-26 15:05:29 +02001931 ceph_osdc_put_request(rd_req);
1932 ceph_osdc_put_request(wr_req);
Yan, Zheng10183a62015-04-27 15:33:28 +08001933out:
1934 if (!err)
1935 err = have;
Yan, Zheng779fe0f2016-03-07 09:35:06 +08001936 if (pool_ns)
1937 dout("__ceph_pool_perm_get pool %lld ns %.*s result = %d\n",
1938 pool, (int)pool_ns->len, pool_ns->str, err);
1939 else
1940 dout("__ceph_pool_perm_get pool %lld result = %d\n", pool, err);
Yan, Zheng10183a62015-04-27 15:33:28 +08001941 return err;
1942}
1943
1944int ceph_pool_perm_check(struct ceph_inode_info *ci, int need)
1945{
Yan, Zheng76271512016-02-03 21:24:49 +08001946 s64 pool;
Yan, Zheng779fe0f2016-03-07 09:35:06 +08001947 struct ceph_string *pool_ns;
Yan, Zheng10183a62015-04-27 15:33:28 +08001948 int ret, flags;
1949
Yan, Zheng80e80fb2016-12-13 16:03:26 +08001950 if (ci->i_vino.snap != CEPH_NOSNAP) {
1951 /*
1952 * Pool permission check needs to write to the first object.
1953 * But for snapshot, head of the first object may have alread
1954 * been deleted. Skip check to avoid creating orphan object.
1955 */
1956 return 0;
1957 }
1958
Yan, Zheng10183a62015-04-27 15:33:28 +08001959 if (ceph_test_mount_opt(ceph_inode_to_client(&ci->vfs_inode),
1960 NOPOOLPERM))
1961 return 0;
1962
1963 spin_lock(&ci->i_ceph_lock);
1964 flags = ci->i_ceph_flags;
Yan, Zheng76271512016-02-03 21:24:49 +08001965 pool = ci->i_layout.pool_id;
Yan, Zheng10183a62015-04-27 15:33:28 +08001966 spin_unlock(&ci->i_ceph_lock);
1967check:
1968 if (flags & CEPH_I_POOL_PERM) {
1969 if ((need & CEPH_CAP_FILE_RD) && !(flags & CEPH_I_POOL_RD)) {
Yan, Zheng76271512016-02-03 21:24:49 +08001970 dout("ceph_pool_perm_check pool %lld no read perm\n",
Yan, Zheng10183a62015-04-27 15:33:28 +08001971 pool);
1972 return -EPERM;
1973 }
1974 if ((need & CEPH_CAP_FILE_WR) && !(flags & CEPH_I_POOL_WR)) {
Yan, Zheng76271512016-02-03 21:24:49 +08001975 dout("ceph_pool_perm_check pool %lld no write perm\n",
Yan, Zheng10183a62015-04-27 15:33:28 +08001976 pool);
1977 return -EPERM;
1978 }
1979 return 0;
1980 }
1981
Yan, Zheng779fe0f2016-03-07 09:35:06 +08001982 pool_ns = ceph_try_get_string(ci->i_layout.pool_ns);
1983 ret = __ceph_pool_perm_get(ci, pool, pool_ns);
1984 ceph_put_string(pool_ns);
Yan, Zheng10183a62015-04-27 15:33:28 +08001985 if (ret < 0)
1986 return ret;
1987
1988 flags = CEPH_I_POOL_PERM;
1989 if (ret & POOL_READ)
1990 flags |= CEPH_I_POOL_RD;
1991 if (ret & POOL_WRITE)
1992 flags |= CEPH_I_POOL_WR;
1993
1994 spin_lock(&ci->i_ceph_lock);
Yan, Zheng779fe0f2016-03-07 09:35:06 +08001995 if (pool == ci->i_layout.pool_id &&
1996 pool_ns == rcu_dereference_raw(ci->i_layout.pool_ns)) {
1997 ci->i_ceph_flags |= flags;
Yan, Zheng10183a62015-04-27 15:33:28 +08001998 } else {
Yan, Zheng76271512016-02-03 21:24:49 +08001999 pool = ci->i_layout.pool_id;
Yan, Zheng10183a62015-04-27 15:33:28 +08002000 flags = ci->i_ceph_flags;
2001 }
2002 spin_unlock(&ci->i_ceph_lock);
2003 goto check;
2004}
2005
2006void ceph_pool_perm_destroy(struct ceph_mds_client *mdsc)
2007{
2008 struct ceph_pool_perm *perm;
2009 struct rb_node *n;
2010
2011 while (!RB_EMPTY_ROOT(&mdsc->pool_perm_tree)) {
2012 n = rb_first(&mdsc->pool_perm_tree);
2013 perm = rb_entry(n, struct ceph_pool_perm, node);
2014 rb_erase(n, &mdsc->pool_perm_tree);
2015 kfree(perm);
2016 }
2017}