blob: 890c50971a690472f6dc795b00fd0bcfbcdf1a39 [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
146 if (offset != 0 || length != PAGE_CACHE_SIZE) {
147 dout("%p invalidatepage %p idx %lu partial dirty page %u~%u\n",
148 inode, page, page->index, offset, length);
149 return;
150 }
Alexander 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{
178 struct inode *inode = page->mapping ? page->mapping->host : NULL;
179 dout("%p releasepage %p idx %lu\n", inode, page, page->index);
180 WARN_ON(PageDirty(page));
Milosz Tanski99ccbd22013-08-21 17:29:54 -0400181
182 /* Can we release the page from the cache? */
183 if (!ceph_release_fscache_page(page, g))
184 return 0;
185
186 return !PagePrivate(page);
Sage Weil1d3576f2009-10-06 11:31:09 -0700187}
188
189/*
190 * read a single page, without unlocking it.
191 */
192static int readpage_nounlock(struct file *filp, struct page *page)
193{
Al Viro496ad9a2013-01-23 17:07:38 -0500194 struct inode *inode = file_inode(filp);
Sage Weil1d3576f2009-10-06 11:31:09 -0700195 struct ceph_inode_info *ci = ceph_inode(inode);
Milosz Tanski99ccbd22013-08-21 17:29:54 -0400196 struct ceph_osd_client *osdc =
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700197 &ceph_inode_to_client(inode)->client->osdc;
Sage Weil1d3576f2009-10-06 11:31:09 -0700198 int err = 0;
Yan, Zheng83701242014-11-14 22:36:18 +0800199 u64 off = page_offset(page);
Sage Weil1d3576f2009-10-06 11:31:09 -0700200 u64 len = PAGE_CACHE_SIZE;
201
Yan, Zheng83701242014-11-14 22:36:18 +0800202 if (off >= i_size_read(inode)) {
Yan, Zhengfcc02d22015-01-10 11:43:12 +0800203 zero_user_segment(page, 0, PAGE_CACHE_SIZE);
Yan, Zheng83701242014-11-14 22:36:18 +0800204 SetPageUptodate(page);
205 return 0;
206 }
Milosz Tanski99ccbd22013-08-21 17:29:54 -0400207
Yan, Zhengfcc02d22015-01-10 11:43:12 +0800208 if (ci->i_inline_version != CEPH_INLINE_NONE) {
209 /*
210 * Uptodate inline data should have been added
211 * into page cache while getting Fcr caps.
212 */
213 if (off == 0)
214 return -EINVAL;
215 zero_user_segment(page, 0, PAGE_CACHE_SIZE);
216 SetPageUptodate(page);
217 return 0;
218 }
Yan, Zheng83701242014-11-14 22:36:18 +0800219
220 err = ceph_readpage_from_fscache(inode, page);
Milosz Tanski99ccbd22013-08-21 17:29:54 -0400221 if (err == 0)
222 goto out;
223
Sage Weil1d3576f2009-10-06 11:31:09 -0700224 dout("readpage inode %p file %p page %p index %lu\n",
225 inode, filp, page, page->index);
226 err = ceph_osdc_readpages(osdc, ceph_vino(inode), &ci->i_layout,
Yan, Zheng83701242014-11-14 22:36:18 +0800227 off, &len,
Sage Weil1d3576f2009-10-06 11:31:09 -0700228 ci->i_truncate_seq, ci->i_truncate_size,
Sage Weilb7495fc2010-11-09 12:43:12 -0800229 &page, 1, 0);
Sage Weil1d3576f2009-10-06 11:31:09 -0700230 if (err == -ENOENT)
231 err = 0;
232 if (err < 0) {
233 SetPageError(page);
Li Wang18302802013-12-19 06:03:49 -0800234 ceph_fscache_readpage_cancel(inode, page);
Sage Weil1d3576f2009-10-06 11:31:09 -0700235 goto out;
Sage Weil1d3576f2009-10-06 11:31:09 -0700236 }
Zhang Zhen23cd5732014-05-28 15:09:55 +0800237 if (err < PAGE_CACHE_SIZE)
238 /* zero fill remainder of page */
239 zero_user_segment(page, err, PAGE_CACHE_SIZE);
240 else
241 flush_dcache_page(page);
Sage Weil1d3576f2009-10-06 11:31:09 -0700242
Zhang Zhen23cd5732014-05-28 15:09:55 +0800243 SetPageUptodate(page);
244 ceph_readpage_to_fscache(inode, page);
Milosz Tanski99ccbd22013-08-21 17:29:54 -0400245
Sage Weil1d3576f2009-10-06 11:31:09 -0700246out:
247 return err < 0 ? err : 0;
248}
249
250static int ceph_readpage(struct file *filp, struct page *page)
251{
252 int r = readpage_nounlock(filp, page);
253 unlock_page(page);
254 return r;
255}
256
257/*
Sage Weil7c272192011-08-03 09:58:09 -0700258 * Finish an async read(ahead) op.
Sage Weil1d3576f2009-10-06 11:31:09 -0700259 */
Sage Weil7c272192011-08-03 09:58:09 -0700260static void finish_read(struct ceph_osd_request *req, struct ceph_msg *msg)
Sage Weil1d3576f2009-10-06 11:31:09 -0700261{
Sage Weil7c272192011-08-03 09:58:09 -0700262 struct inode *inode = req->r_inode;
Alex Elder87060c12013-04-03 01:28:58 -0500263 struct ceph_osd_data *osd_data;
Sage Weil1b83bef2013-02-25 16:11:12 -0800264 int rc = req->r_result;
265 int bytes = le32_to_cpu(msg->hdr.data_len);
Alex Eldere0c59482013-03-07 15:38:25 -0600266 int num_pages;
Sage Weil7c272192011-08-03 09:58:09 -0700267 int i;
268
Sage Weil7c272192011-08-03 09:58:09 -0700269 dout("finish_read %p req %p rc %d bytes %d\n", inode, req, rc, bytes);
270
271 /* unlock all pages, zeroing any data we didn't read */
Alex Elder406e2c92013-04-15 14:50:36 -0500272 osd_data = osd_req_op_extent_osd_data(req, 0);
Alex Elder87060c12013-04-03 01:28:58 -0500273 BUG_ON(osd_data->type != CEPH_OSD_DATA_TYPE_PAGES);
274 num_pages = calc_pages_for((u64)osd_data->alignment,
275 (u64)osd_data->length);
Alex Eldere0c59482013-03-07 15:38:25 -0600276 for (i = 0; i < num_pages; i++) {
Alex Elder87060c12013-04-03 01:28:58 -0500277 struct page *page = osd_data->pages[i];
Sage Weil7c272192011-08-03 09:58:09 -0700278
Li Wangf36132a2013-11-27 22:28:13 +0800279 if (rc < 0)
280 goto unlock;
Sage Weil7c272192011-08-03 09:58:09 -0700281 if (bytes < (int)PAGE_CACHE_SIZE) {
282 /* zero (remainder of) page */
283 int s = bytes < 0 ? 0 : bytes;
284 zero_user_segment(page, s, PAGE_CACHE_SIZE);
285 }
286 dout("finish_read %p uptodate %p idx %lu\n", inode, page,
287 page->index);
288 flush_dcache_page(page);
289 SetPageUptodate(page);
Milosz Tanski99ccbd22013-08-21 17:29:54 -0400290 ceph_readpage_to_fscache(inode, page);
Li Wangf36132a2013-11-27 22:28:13 +0800291unlock:
Sage Weil7c272192011-08-03 09:58:09 -0700292 unlock_page(page);
293 page_cache_release(page);
Alex Elder0fff87e2013-02-14 12:16:43 -0600294 bytes -= PAGE_CACHE_SIZE;
Sage Weil7c272192011-08-03 09:58:09 -0700295 }
Alex Elder87060c12013-04-03 01:28:58 -0500296 kfree(osd_data->pages);
Sage Weil7c272192011-08-03 09:58:09 -0700297}
298
David Zafman8884d532012-12-03 19:14:05 -0800299static void ceph_unlock_page_vector(struct page **pages, int num_pages)
300{
301 int i;
302
303 for (i = 0; i < num_pages; i++)
304 unlock_page(pages[i]);
305}
306
Sage Weil7c272192011-08-03 09:58:09 -0700307/*
308 * start an async read(ahead) operation. return nr_pages we submitted
309 * a read for on success, or negative error code.
310 */
Sage Weil0d66a482011-08-04 08:21:30 -0700311static int start_read(struct inode *inode, struct list_head *page_list, int max)
Sage Weil7c272192011-08-03 09:58:09 -0700312{
313 struct ceph_osd_client *osdc =
314 &ceph_inode_to_client(inode)->client->osdc;
315 struct ceph_inode_info *ci = ceph_inode(inode);
316 struct page *page = list_entry(page_list->prev, struct page, lru);
Alex Elderacead002013-03-14 14:09:05 -0500317 struct ceph_vino vino;
Sage Weil7c272192011-08-03 09:58:09 -0700318 struct ceph_osd_request *req;
319 u64 off;
320 u64 len;
321 int i;
Sage Weil1d3576f2009-10-06 11:31:09 -0700322 struct page **pages;
Sage Weil7c272192011-08-03 09:58:09 -0700323 pgoff_t next_index;
324 int nr_pages = 0;
325 int ret;
326
Alex Elder6285bc22012-10-02 10:25:51 -0500327 off = (u64) page_offset(page);
Sage Weil7c272192011-08-03 09:58:09 -0700328
329 /* count pages */
330 next_index = page->index;
331 list_for_each_entry_reverse(page, page_list, lru) {
332 if (page->index != next_index)
333 break;
334 nr_pages++;
335 next_index++;
Sage Weil0d66a482011-08-04 08:21:30 -0700336 if (max && nr_pages == max)
337 break;
Sage Weil7c272192011-08-03 09:58:09 -0700338 }
339 len = nr_pages << PAGE_CACHE_SHIFT;
340 dout("start_read %p nr_pages %d is %lld~%lld\n", inode, nr_pages,
341 off, len);
Alex Elderacead002013-03-14 14:09:05 -0500342 vino = ceph_vino(inode);
343 req = ceph_osdc_new_request(osdc, &ci->i_layout, vino, off, &len,
Yan, Zheng715e4cd2014-11-13 14:40:37 +0800344 0, 1, CEPH_OSD_OP_READ,
Alex Elderacead002013-03-14 14:09:05 -0500345 CEPH_OSD_FLAG_READ, NULL,
Sage Weil7c272192011-08-03 09:58:09 -0700346 ci->i_truncate_seq, ci->i_truncate_size,
Alex Elderacead002013-03-14 14:09:05 -0500347 false);
Sage Weil68162822012-09-24 21:01:02 -0700348 if (IS_ERR(req))
349 return PTR_ERR(req);
Sage Weil1d3576f2009-10-06 11:31:09 -0700350
351 /* build page vector */
Alex Eldercf7b7e12013-03-01 18:00:15 -0600352 nr_pages = calc_pages_for(0, len);
Yan, Zheng687265e2015-06-13 17:27:05 +0800353 pages = kmalloc(sizeof(*pages) * nr_pages, GFP_KERNEL);
Sage Weil7c272192011-08-03 09:58:09 -0700354 ret = -ENOMEM;
Sage Weil1d3576f2009-10-06 11:31:09 -0700355 if (!pages)
Sage Weil7c272192011-08-03 09:58:09 -0700356 goto out;
357 for (i = 0; i < nr_pages; ++i) {
358 page = list_entry(page_list->prev, struct page, lru);
359 BUG_ON(PageLocked(page));
360 list_del(&page->lru);
Milosz Tanski99ccbd22013-08-21 17:29:54 -0400361
Sage Weil7c272192011-08-03 09:58:09 -0700362 dout("start_read %p adding %p idx %lu\n", inode, page,
363 page->index);
364 if (add_to_page_cache_lru(page, &inode->i_data, page->index,
Yan, Zheng687265e2015-06-13 17:27:05 +0800365 GFP_KERNEL)) {
Milosz Tanskid4d3aa32013-09-03 19:11:17 -0400366 ceph_fscache_uncache_page(inode, page);
Sage Weil7c272192011-08-03 09:58:09 -0700367 page_cache_release(page);
368 dout("start_read %p add_to_page_cache failed %p\n",
369 inode, page);
370 nr_pages = i;
371 goto out_pages;
Sage Weil1d3576f2009-10-06 11:31:09 -0700372 }
Sage Weil7c272192011-08-03 09:58:09 -0700373 pages[i] = page;
Sage Weil1d3576f2009-10-06 11:31:09 -0700374 }
Alex Elder406e2c92013-04-15 14:50:36 -0500375 osd_req_op_extent_osd_data_pages(req, 0, pages, len, 0, false, false);
Sage Weil7c272192011-08-03 09:58:09 -0700376 req->r_callback = finish_read;
377 req->r_inode = inode;
378
Alex Elder79528732013-04-03 21:32:51 -0500379 ceph_osdc_build_request(req, off, NULL, vino.snap, NULL);
Alex Elder02ee07d2013-03-14 14:09:06 -0500380
Sage Weil7c272192011-08-03 09:58:09 -0700381 dout("start_read %p starting %p %lld~%lld\n", inode, req, off, len);
382 ret = ceph_osdc_start_request(osdc, req, false);
383 if (ret < 0)
384 goto out_pages;
385 ceph_osdc_put_request(req);
386 return nr_pages;
387
388out_pages:
David Zafman8884d532012-12-03 19:14:05 -0800389 ceph_unlock_page_vector(pages, nr_pages);
Sage Weil7c272192011-08-03 09:58:09 -0700390 ceph_release_page_vector(pages, nr_pages);
Sage Weil7c272192011-08-03 09:58:09 -0700391out:
392 ceph_osdc_put_request(req);
393 return ret;
Sage Weil1d3576f2009-10-06 11:31:09 -0700394}
395
Sage Weil7c272192011-08-03 09:58:09 -0700396
Sage Weil1d3576f2009-10-06 11:31:09 -0700397/*
398 * Read multiple pages. Leave pages we don't read + unlock in page_list;
399 * the caller (VM) cleans them up.
400 */
401static int ceph_readpages(struct file *file, struct address_space *mapping,
402 struct list_head *page_list, unsigned nr_pages)
403{
Al Viro496ad9a2013-01-23 17:07:38 -0500404 struct inode *inode = file_inode(file);
Sage Weil0d66a482011-08-04 08:21:30 -0700405 struct ceph_fs_client *fsc = ceph_inode_to_client(inode);
Sage Weil1d3576f2009-10-06 11:31:09 -0700406 int rc = 0;
Sage Weil0d66a482011-08-04 08:21:30 -0700407 int max = 0;
Sage Weil1d3576f2009-10-06 11:31:09 -0700408
Yan, Zheng83701242014-11-14 22:36:18 +0800409 if (ceph_inode(inode)->i_inline_version != CEPH_INLINE_NONE)
410 return -EINVAL;
411
Milosz Tanski99ccbd22013-08-21 17:29:54 -0400412 rc = ceph_readpages_from_fscache(mapping->host, mapping, page_list,
413 &nr_pages);
414
415 if (rc == 0)
416 goto out;
417
Sage Weil0d66a482011-08-04 08:21:30 -0700418 if (fsc->mount_options->rsize >= PAGE_CACHE_SIZE)
419 max = (fsc->mount_options->rsize + PAGE_CACHE_SIZE - 1)
420 >> PAGE_SHIFT;
421
Alex Elder2794a822013-02-14 12:16:43 -0600422 dout("readpages %p file %p nr_pages %d max %d\n", inode,
423 file, nr_pages,
Sage Weil0d66a482011-08-04 08:21:30 -0700424 max);
Sage Weil7c272192011-08-03 09:58:09 -0700425 while (!list_empty(page_list)) {
Sage Weil0d66a482011-08-04 08:21:30 -0700426 rc = start_read(inode, page_list, max);
Sage Weil7c272192011-08-03 09:58:09 -0700427 if (rc < 0)
428 goto out;
429 BUG_ON(rc == 0);
Sage Weil1d3576f2009-10-06 11:31:09 -0700430 }
Sage Weil1d3576f2009-10-06 11:31:09 -0700431out:
Milosz Tanski76be7782013-08-21 17:30:27 -0400432 ceph_fscache_readpages_cancel(inode, page_list);
433
Sage Weil7c272192011-08-03 09:58:09 -0700434 dout("readpages %p file %p ret %d\n", inode, file, rc);
Sage Weil1d3576f2009-10-06 11:31:09 -0700435 return rc;
436}
437
438/*
439 * Get ref for the oldest snapc for an inode with dirty data... that is, the
440 * only snap context we are allowed to write back.
Sage Weil1d3576f2009-10-06 11:31:09 -0700441 */
Sage Weil6298a332010-03-31 22:01:38 -0700442static struct ceph_snap_context *get_oldest_context(struct inode *inode,
Yan, Zhenge1966b42015-06-18 03:10:58 +0800443 loff_t *snap_size)
Sage Weil1d3576f2009-10-06 11:31:09 -0700444{
445 struct ceph_inode_info *ci = ceph_inode(inode);
446 struct ceph_snap_context *snapc = NULL;
447 struct ceph_cap_snap *capsnap = NULL;
448
Sage Weilbe655592011-11-30 09:47:09 -0800449 spin_lock(&ci->i_ceph_lock);
Sage Weil1d3576f2009-10-06 11:31:09 -0700450 list_for_each_entry(capsnap, &ci->i_cap_snaps, ci_item) {
451 dout(" cap_snap %p snapc %p has %d dirty pages\n", capsnap,
452 capsnap->context, capsnap->dirty_pages);
453 if (capsnap->dirty_pages) {
454 snapc = ceph_get_snap_context(capsnap->context);
455 if (snap_size)
456 *snap_size = capsnap->size;
457 break;
458 }
459 }
Sage Weil7d8cb262010-08-24 08:44:16 -0700460 if (!snapc && ci->i_wrbuffer_ref_head) {
Sage Weil80e755f2010-03-31 21:52:10 -0700461 snapc = ceph_get_snap_context(ci->i_head_snapc);
Sage Weil1d3576f2009-10-06 11:31:09 -0700462 dout(" head snapc %p has %d dirty pages\n",
463 snapc, ci->i_wrbuffer_ref_head);
464 }
Sage Weilbe655592011-11-30 09:47:09 -0800465 spin_unlock(&ci->i_ceph_lock);
Sage Weil1d3576f2009-10-06 11:31:09 -0700466 return snapc;
467}
468
469/*
470 * Write a single page, but leave the page locked.
471 *
472 * If we get a write error, set the page error bit, but still adjust the
473 * dirty page accounting (i.e., page is no longer dirty).
474 */
475static int writepage_nounlock(struct page *page, struct writeback_control *wbc)
476{
477 struct inode *inode;
478 struct ceph_inode_info *ci;
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700479 struct ceph_fs_client *fsc;
Sage Weil1d3576f2009-10-06 11:31:09 -0700480 struct ceph_osd_client *osdc;
Sage Weil6298a332010-03-31 22:01:38 -0700481 struct ceph_snap_context *snapc, *oldest;
Yan, Zhengfc2744a2013-05-31 16:48:29 +0800482 loff_t page_off = page_offset(page);
Yan, Zhenge1966b42015-06-18 03:10:58 +0800483 loff_t snap_size = -1;
Yehuda Sadeh2baba252009-12-18 13:51:57 -0800484 long writeback_stat;
Yan, Zhenge1966b42015-06-18 03:10:58 +0800485 u64 truncate_size;
Yan, Zhengfc2744a2013-05-31 16:48:29 +0800486 u32 truncate_seq;
487 int err = 0, len = PAGE_CACHE_SIZE;
Sage Weil1d3576f2009-10-06 11:31:09 -0700488
489 dout("writepage %p idx %lu\n", page, page->index);
490
491 if (!page->mapping || !page->mapping->host) {
492 dout("writepage %p - no mapping\n", page);
493 return -EFAULT;
494 }
495 inode = page->mapping->host;
496 ci = ceph_inode(inode);
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700497 fsc = ceph_inode_to_client(inode);
498 osdc = &fsc->client->osdc;
Sage Weil1d3576f2009-10-06 11:31:09 -0700499
500 /* verify this is a writeable snap context */
Yan, Zheng61600ef2012-05-28 14:44:30 +0800501 snapc = page_snap_context(page);
Sage Weil1d3576f2009-10-06 11:31:09 -0700502 if (snapc == NULL) {
503 dout("writepage %p page %p not dirty?\n", inode, page);
504 goto out;
505 }
Sage Weil6298a332010-03-31 22:01:38 -0700506 oldest = get_oldest_context(inode, &snap_size);
507 if (snapc->seq > oldest->seq) {
Sage Weil1d3576f2009-10-06 11:31:09 -0700508 dout("writepage %p page %p snapc %p not writeable - noop\n",
Yan, Zheng61600ef2012-05-28 14:44:30 +0800509 inode, page, snapc);
Sage Weil1d3576f2009-10-06 11:31:09 -0700510 /* we should only noop if called by kswapd */
511 WARN_ON((current->flags & PF_MEMALLOC) == 0);
Sage Weil6298a332010-03-31 22:01:38 -0700512 ceph_put_snap_context(oldest);
Sage Weil1d3576f2009-10-06 11:31:09 -0700513 goto out;
514 }
Sage Weil6298a332010-03-31 22:01:38 -0700515 ceph_put_snap_context(oldest);
Sage Weil1d3576f2009-10-06 11:31:09 -0700516
Yan, Zhengfc2744a2013-05-31 16:48:29 +0800517 spin_lock(&ci->i_ceph_lock);
518 truncate_seq = ci->i_truncate_seq;
519 truncate_size = ci->i_truncate_size;
Yan, Zhenge1966b42015-06-18 03:10:58 +0800520 if (snap_size == -1)
Yan, Zhengfc2744a2013-05-31 16:48:29 +0800521 snap_size = i_size_read(inode);
522 spin_unlock(&ci->i_ceph_lock);
523
Sage Weil1d3576f2009-10-06 11:31:09 -0700524 /* is this a partial page at end of file? */
Yan, Zhengfc2744a2013-05-31 16:48:29 +0800525 if (page_off >= snap_size) {
526 dout("%p page eof %llu\n", page, snap_size);
527 goto out;
528 }
529 if (snap_size < page_off + len)
530 len = snap_size - page_off;
Sage Weil1d3576f2009-10-06 11:31:09 -0700531
Sage Weilae00d4f2010-09-16 16:26:51 -0700532 dout("writepage %p page %p index %lu on %llu~%u snapc %p\n",
533 inode, page, page->index, page_off, len, snapc);
Sage Weil1d3576f2009-10-06 11:31:09 -0700534
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700535 writeback_stat = atomic_long_inc_return(&fsc->writeback_count);
Yehuda Sadeh2baba252009-12-18 13:51:57 -0800536 if (writeback_stat >
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700537 CONGESTION_ON_THRESH(fsc->mount_options->congestion_kb))
538 set_bdi_congested(&fsc->backing_dev_info, BLK_RW_ASYNC);
Yehuda Sadeh2baba252009-12-18 13:51:57 -0800539
Milosz Tanski99ccbd22013-08-21 17:29:54 -0400540 ceph_readpage_to_fscache(inode, page);
541
Sage Weil1d3576f2009-10-06 11:31:09 -0700542 set_page_writeback(page);
543 err = ceph_osdc_writepages(osdc, ceph_vino(inode),
544 &ci->i_layout, snapc,
545 page_off, len,
Yan, Zhengfc2744a2013-05-31 16:48:29 +0800546 truncate_seq, truncate_size,
Alex Elder24808822013-02-15 11:42:29 -0600547 &inode->i_mtime, &page, 1);
Sage Weil1d3576f2009-10-06 11:31:09 -0700548 if (err < 0) {
549 dout("writepage setting page/mapping error %d %p\n", err, page);
550 SetPageError(page);
551 mapping_set_error(&inode->i_data, err);
552 if (wbc)
553 wbc->pages_skipped++;
554 } else {
555 dout("writepage cleaned page %p\n", page);
556 err = 0; /* vfs expects us to return 0 */
557 }
558 page->private = 0;
559 ClearPagePrivate(page);
560 end_page_writeback(page);
561 ceph_put_wrbuffer_cap_refs(ci, 1, snapc);
Sage Weil6298a332010-03-31 22:01:38 -0700562 ceph_put_snap_context(snapc); /* page's reference */
Sage Weil1d3576f2009-10-06 11:31:09 -0700563out:
564 return err;
565}
566
567static int ceph_writepage(struct page *page, struct writeback_control *wbc)
568{
Yehuda Sadehdbd646a2009-12-16 14:51:06 -0800569 int err;
570 struct inode *inode = page->mapping->host;
571 BUG_ON(!inode);
Sage Weil70b666c2011-05-27 09:24:26 -0700572 ihold(inode);
Yehuda Sadehdbd646a2009-12-16 14:51:06 -0800573 err = writepage_nounlock(page, wbc);
Sage Weil1d3576f2009-10-06 11:31:09 -0700574 unlock_page(page);
Yehuda Sadehdbd646a2009-12-16 14:51:06 -0800575 iput(inode);
Sage Weil1d3576f2009-10-06 11:31:09 -0700576 return err;
577}
578
579
580/*
581 * lame release_pages helper. release_pages() isn't exported to
582 * modules.
583 */
584static void ceph_release_pages(struct page **pages, int num)
585{
586 struct pagevec pvec;
587 int i;
588
589 pagevec_init(&pvec, 0);
590 for (i = 0; i < num; i++) {
591 if (pagevec_add(&pvec, pages[i]) == 0)
592 pagevec_release(&pvec);
593 }
594 pagevec_release(&pvec);
595}
596
Sage Weil1d3576f2009-10-06 11:31:09 -0700597/*
598 * async writeback completion handler.
599 *
600 * If we get an error, set the mapping error bit, but not the individual
601 * page error bits.
602 */
603static void writepages_finish(struct ceph_osd_request *req,
604 struct ceph_msg *msg)
605{
606 struct inode *inode = req->r_inode;
Sage Weil1d3576f2009-10-06 11:31:09 -0700607 struct ceph_inode_info *ci = ceph_inode(inode);
Alex Elder87060c12013-04-03 01:28:58 -0500608 struct ceph_osd_data *osd_data;
Sage Weil1d3576f2009-10-06 11:31:09 -0700609 unsigned wrote;
Sage Weil1d3576f2009-10-06 11:31:09 -0700610 struct page *page;
Alex Eldere0c59482013-03-07 15:38:25 -0600611 int num_pages;
Sage Weil1d3576f2009-10-06 11:31:09 -0700612 int i;
613 struct ceph_snap_context *snapc = req->r_snapc;
614 struct address_space *mapping = inode->i_mapping;
Sage Weil1b83bef2013-02-25 16:11:12 -0800615 int rc = req->r_result;
Alex Elder79528732013-04-03 21:32:51 -0500616 u64 bytes = req->r_ops[0].extent.length;
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700617 struct ceph_fs_client *fsc = ceph_inode_to_client(inode);
Yehuda Sadeh2baba252009-12-18 13:51:57 -0800618 long writeback_stat;
Sage Weil7ff899d2010-04-23 10:25:33 -0700619 unsigned issued = ceph_caps_issued(ci);
Sage Weil1d3576f2009-10-06 11:31:09 -0700620
Alex Elder406e2c92013-04-15 14:50:36 -0500621 osd_data = osd_req_op_extent_osd_data(req, 0);
Alex Elder87060c12013-04-03 01:28:58 -0500622 BUG_ON(osd_data->type != CEPH_OSD_DATA_TYPE_PAGES);
623 num_pages = calc_pages_for((u64)osd_data->alignment,
624 (u64)osd_data->length);
Sage Weil1d3576f2009-10-06 11:31:09 -0700625 if (rc >= 0) {
Sage Weil79788c62010-02-02 16:34:04 -0800626 /*
627 * Assume we wrote the pages we originally sent. The
628 * osd might reply with fewer pages if our writeback
629 * raced with a truncation and was adjusted at the osd,
630 * so don't believe the reply.
631 */
Alex Eldere0c59482013-03-07 15:38:25 -0600632 wrote = num_pages;
Sage Weil1d3576f2009-10-06 11:31:09 -0700633 } else {
634 wrote = 0;
635 mapping_set_error(mapping, rc);
636 }
637 dout("writepages_finish %p rc %d bytes %llu wrote %d (pages)\n",
638 inode, rc, bytes, wrote);
639
640 /* clean all pages */
Alex Eldere0c59482013-03-07 15:38:25 -0600641 for (i = 0; i < num_pages; i++) {
Alex Elder87060c12013-04-03 01:28:58 -0500642 page = osd_data->pages[i];
Sage Weil1d3576f2009-10-06 11:31:09 -0700643 BUG_ON(!page);
644 WARN_ON(!PageUptodate(page));
645
Yehuda Sadeh2baba252009-12-18 13:51:57 -0800646 writeback_stat =
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700647 atomic_long_dec_return(&fsc->writeback_count);
Yehuda Sadeh2baba252009-12-18 13:51:57 -0800648 if (writeback_stat <
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700649 CONGESTION_OFF_THRESH(fsc->mount_options->congestion_kb))
650 clear_bdi_congested(&fsc->backing_dev_info,
Yehuda Sadeh2baba252009-12-18 13:51:57 -0800651 BLK_RW_ASYNC);
652
Yan, Zheng61600ef2012-05-28 14:44:30 +0800653 ceph_put_snap_context(page_snap_context(page));
Sage Weil1d3576f2009-10-06 11:31:09 -0700654 page->private = 0;
655 ClearPagePrivate(page);
Sage Weil1d3576f2009-10-06 11:31:09 -0700656 dout("unlocking %d %p\n", i, page);
657 end_page_writeback(page);
Yehuda Sadehe63dc5c2010-02-19 00:07:01 +0000658
659 /*
660 * We lost the cache cap, need to truncate the page before
661 * it is unlocked, otherwise we'd truncate it later in the
662 * page truncation thread, possibly losing some data that
663 * raced its way in
664 */
Sage Weil29625072010-05-27 10:40:43 -0700665 if ((issued & (CEPH_CAP_FILE_CACHE|CEPH_CAP_FILE_LAZYIO)) == 0)
Yehuda Sadehe63dc5c2010-02-19 00:07:01 +0000666 generic_error_remove_page(inode->i_mapping, page);
667
Sage Weil1d3576f2009-10-06 11:31:09 -0700668 unlock_page(page);
669 }
670 dout("%p wrote+cleaned %d pages\n", inode, wrote);
Alex Eldere0c59482013-03-07 15:38:25 -0600671 ceph_put_wrbuffer_cap_refs(ci, num_pages, snapc);
Sage Weil1d3576f2009-10-06 11:31:09 -0700672
Alex Elder87060c12013-04-03 01:28:58 -0500673 ceph_release_pages(osd_data->pages, num_pages);
674 if (osd_data->pages_from_pool)
675 mempool_free(osd_data->pages,
Cheng Renquan640ef792010-03-26 17:40:33 +0800676 ceph_sb_to_client(inode->i_sb)->wb_pagevec_pool);
Sage Weil1d3576f2009-10-06 11:31:09 -0700677 else
Alex Elder87060c12013-04-03 01:28:58 -0500678 kfree(osd_data->pages);
Sage Weil1d3576f2009-10-06 11:31:09 -0700679 ceph_osdc_put_request(req);
680}
681
Sage Weil1d3576f2009-10-06 11:31:09 -0700682/*
683 * initiate async writeback
684 */
685static int ceph_writepages_start(struct address_space *mapping,
686 struct writeback_control *wbc)
687{
688 struct inode *inode = mapping->host;
Sage Weil1d3576f2009-10-06 11:31:09 -0700689 struct ceph_inode_info *ci = ceph_inode(inode);
Yan, Zhengfc2744a2013-05-31 16:48:29 +0800690 struct ceph_fs_client *fsc = ceph_inode_to_client(inode);
691 struct ceph_vino vino = ceph_vino(inode);
Sage Weil1d3576f2009-10-06 11:31:09 -0700692 pgoff_t index, start, end;
693 int range_whole = 0;
694 int should_loop = 1;
695 pgoff_t max_pages = 0, max_pages_ever = 0;
Sage Weil80e755f2010-03-31 21:52:10 -0700696 struct ceph_snap_context *snapc = NULL, *last_snapc = NULL, *pgsnapc;
Sage Weil1d3576f2009-10-06 11:31:09 -0700697 struct pagevec pvec;
698 int done = 0;
699 int rc = 0;
700 unsigned wsize = 1 << inode->i_blkbits;
701 struct ceph_osd_request *req = NULL;
Dan Carpenter021b77b2014-11-28 11:33:34 +0300702 int do_sync = 0;
Yan, Zhenge1966b42015-06-18 03:10:58 +0800703 loff_t snap_size, i_size;
704 u64 truncate_size;
Yan, Zhengfc2744a2013-05-31 16:48:29 +0800705 u32 truncate_seq;
Sage Weil1d3576f2009-10-06 11:31:09 -0700706
707 /*
708 * Include a 'sync' in the OSD request if this is a data
709 * integrity write (e.g., O_SYNC write or fsync()), or if our
710 * cap is being revoked.
711 */
majianpengc62988e2013-06-19 15:12:06 +0800712 if ((wbc->sync_mode == WB_SYNC_ALL) ||
713 ceph_caps_revoking(ci, CEPH_CAP_FILE_BUFFER))
Sage Weil1d3576f2009-10-06 11:31:09 -0700714 do_sync = 1;
715 dout("writepages_start %p dosync=%d (mode=%s)\n",
716 inode, do_sync,
717 wbc->sync_mode == WB_SYNC_NONE ? "NONE" :
718 (wbc->sync_mode == WB_SYNC_ALL ? "ALL" : "HOLD"));
719
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700720 if (fsc->mount_state == CEPH_MOUNT_SHUTDOWN) {
Fabian Frederickf3ae1b92014-06-06 14:35:37 -0700721 pr_warn("writepage_start %p on forced umount\n", inode);
Sage Weil1d3576f2009-10-06 11:31:09 -0700722 return -EIO; /* we're in a forced umount, don't write! */
723 }
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700724 if (fsc->mount_options->wsize && fsc->mount_options->wsize < wsize)
725 wsize = fsc->mount_options->wsize;
Sage Weil1d3576f2009-10-06 11:31:09 -0700726 if (wsize < PAGE_CACHE_SIZE)
727 wsize = PAGE_CACHE_SIZE;
728 max_pages_ever = wsize >> PAGE_CACHE_SHIFT;
729
730 pagevec_init(&pvec, 0);
731
Sage Weil1d3576f2009-10-06 11:31:09 -0700732 /* where to start/end? */
733 if (wbc->range_cyclic) {
734 start = mapping->writeback_index; /* Start from prev offset */
735 end = -1;
736 dout(" cyclic, start at %lu\n", start);
737 } else {
738 start = wbc->range_start >> PAGE_CACHE_SHIFT;
739 end = wbc->range_end >> PAGE_CACHE_SHIFT;
740 if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX)
741 range_whole = 1;
742 should_loop = 0;
743 dout(" not cyclic, %lu to %lu\n", start, end);
744 }
745 index = start;
746
747retry:
748 /* find oldest snap context with dirty data */
749 ceph_put_snap_context(snapc);
Yan, Zhenge1966b42015-06-18 03:10:58 +0800750 snap_size = -1;
Sage Weil1d3576f2009-10-06 11:31:09 -0700751 snapc = get_oldest_context(inode, &snap_size);
752 if (!snapc) {
753 /* hmm, why does writepages get called when there
754 is no dirty data? */
755 dout(" no snap context with dirty data?\n");
756 goto out;
757 }
758 dout(" oldest snapc is %p seq %lld (%d snaps)\n",
759 snapc, snapc->seq, snapc->num_snaps);
Yan, Zhengfc2744a2013-05-31 16:48:29 +0800760
761 spin_lock(&ci->i_ceph_lock);
762 truncate_seq = ci->i_truncate_seq;
763 truncate_size = ci->i_truncate_size;
Yan, Zhenge1966b42015-06-18 03:10:58 +0800764 i_size = i_size_read(inode);
Yan, Zhengfc2744a2013-05-31 16:48:29 +0800765 spin_unlock(&ci->i_ceph_lock);
766
Sage Weil1d3576f2009-10-06 11:31:09 -0700767 if (last_snapc && snapc != last_snapc) {
768 /* if we switched to a newer snapc, restart our scan at the
769 * start of the original file range. */
770 dout(" snapc differs from last pass, restarting at %lu\n",
771 index);
772 index = start;
773 }
774 last_snapc = snapc;
775
776 while (!done && index <= end) {
777 unsigned i;
778 int first;
779 pgoff_t next;
780 int pvec_pages, locked_pages;
Alex Eldere5975c72013-03-14 14:09:05 -0500781 struct page **pages = NULL;
782 mempool_t *pool = NULL; /* Becomes non-null if mempool used */
Sage Weil1d3576f2009-10-06 11:31:09 -0700783 struct page *page;
784 int want;
785 u64 offset, len;
Yehuda Sadeh2baba252009-12-18 13:51:57 -0800786 long writeback_stat;
Sage Weil1d3576f2009-10-06 11:31:09 -0700787
788 next = 0;
789 locked_pages = 0;
790 max_pages = max_pages_ever;
791
792get_more_pages:
793 first = -1;
794 want = min(end - index,
795 min((pgoff_t)PAGEVEC_SIZE,
796 max_pages - (pgoff_t)locked_pages) - 1)
797 + 1;
798 pvec_pages = pagevec_lookup_tag(&pvec, mapping, &index,
799 PAGECACHE_TAG_DIRTY,
800 want);
801 dout("pagevec_lookup_tag got %d\n", pvec_pages);
802 if (!pvec_pages && !locked_pages)
803 break;
804 for (i = 0; i < pvec_pages && locked_pages < max_pages; i++) {
805 page = pvec.pages[i];
806 dout("? %p idx %lu\n", page, page->index);
807 if (locked_pages == 0)
808 lock_page(page); /* first page */
809 else if (!trylock_page(page))
810 break;
811
812 /* only dirty pages, or our accounting breaks */
813 if (unlikely(!PageDirty(page)) ||
814 unlikely(page->mapping != mapping)) {
815 dout("!dirty or !mapping %p\n", page);
816 unlock_page(page);
817 break;
818 }
819 if (!wbc->range_cyclic && page->index > end) {
820 dout("end of range %p\n", page);
821 done = 1;
822 unlock_page(page);
823 break;
824 }
825 if (next && (page->index != next)) {
826 dout("not consecutive %p\n", page);
827 unlock_page(page);
828 break;
829 }
830 if (wbc->sync_mode != WB_SYNC_NONE) {
831 dout("waiting on writeback %p\n", page);
832 wait_on_page_writeback(page);
833 }
Yan, Zhenge1966b42015-06-18 03:10:58 +0800834 if (page_offset(page) >=
835 (snap_size == -1 ? i_size : snap_size)) {
836 dout("%p page eof %llu\n", page,
837 (snap_size == -1 ? i_size : snap_size));
Sage Weil1d3576f2009-10-06 11:31:09 -0700838 done = 1;
839 unlock_page(page);
840 break;
841 }
842 if (PageWriteback(page)) {
843 dout("%p under writeback\n", page);
844 unlock_page(page);
845 break;
846 }
847
848 /* only if matching snap context */
Yan, Zheng61600ef2012-05-28 14:44:30 +0800849 pgsnapc = page_snap_context(page);
Sage Weil80e755f2010-03-31 21:52:10 -0700850 if (pgsnapc->seq > snapc->seq) {
851 dout("page snapc %p %lld > oldest %p %lld\n",
852 pgsnapc, pgsnapc->seq, snapc, snapc->seq);
Sage Weil1d3576f2009-10-06 11:31:09 -0700853 unlock_page(page);
854 if (!locked_pages)
855 continue; /* keep looking for snap */
856 break;
857 }
858
859 if (!clear_page_dirty_for_io(page)) {
860 dout("%p !clear_page_dirty_for_io\n", page);
861 unlock_page(page);
862 break;
863 }
864
Alex Eldere5975c72013-03-14 14:09:05 -0500865 /*
866 * We have something to write. If this is
867 * the first locked page this time through,
868 * allocate an osd request and a page array
869 * that it will use.
870 */
Sage Weil1d3576f2009-10-06 11:31:09 -0700871 if (locked_pages == 0) {
Alex Eldere5975c72013-03-14 14:09:05 -0500872 BUG_ON(pages);
Sage Weil1d3576f2009-10-06 11:31:09 -0700873 /* prepare async write request */
Alex Eldere5975c72013-03-14 14:09:05 -0500874 offset = (u64)page_offset(page);
Sage Weil1d3576f2009-10-06 11:31:09 -0700875 len = wsize;
Yan, Zhengfc2744a2013-05-31 16:48:29 +0800876 req = ceph_osdc_new_request(&fsc->client->osdc,
877 &ci->i_layout, vino,
Yan, Zheng715e4cd2014-11-13 14:40:37 +0800878 offset, &len, 0,
879 do_sync ? 2 : 1,
Yan, Zhengfc2744a2013-05-31 16:48:29 +0800880 CEPH_OSD_OP_WRITE,
881 CEPH_OSD_FLAG_WRITE |
882 CEPH_OSD_FLAG_ONDISK,
883 snapc, truncate_seq,
884 truncate_size, true);
Sage Weil68162822012-09-24 21:01:02 -0700885 if (IS_ERR(req)) {
886 rc = PTR_ERR(req);
Henry C Chang8c718972011-05-03 09:45:16 +0000887 unlock_page(page);
888 break;
889 }
890
Yan, Zheng715e4cd2014-11-13 14:40:37 +0800891 if (do_sync)
Yan, Zheng144cba12015-04-27 11:09:54 +0800892 osd_req_op_init(req, 1,
893 CEPH_OSD_OP_STARTSYNC, 0);
Yan, Zheng715e4cd2014-11-13 14:40:37 +0800894
Alex Elder88486952013-03-14 14:09:05 -0500895 req->r_callback = writepages_finish;
896 req->r_inode = inode;
897
898 max_pages = calc_pages_for(0, (u64)len);
Yan, Zhengfc2744a2013-05-31 16:48:29 +0800899 pages = kmalloc(max_pages * sizeof (*pages),
900 GFP_NOFS);
Alex Elder88486952013-03-14 14:09:05 -0500901 if (!pages) {
902 pool = fsc->wb_pagevec_pool;
Alex Elder88486952013-03-14 14:09:05 -0500903 pages = mempool_alloc(pool, GFP_NOFS);
Alex Eldere5975c72013-03-14 14:09:05 -0500904 BUG_ON(!pages);
Alex Elder88486952013-03-14 14:09:05 -0500905 }
Sage Weil1d3576f2009-10-06 11:31:09 -0700906 }
907
908 /* note position of first page in pvec */
909 if (first < 0)
910 first = i;
911 dout("%p will write page %p idx %lu\n",
912 inode, page, page->index);
Yehuda Sadeh2baba252009-12-18 13:51:57 -0800913
Sage Weil213c99e2010-08-03 10:25:11 -0700914 writeback_stat =
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700915 atomic_long_inc_return(&fsc->writeback_count);
Sage Weil213c99e2010-08-03 10:25:11 -0700916 if (writeback_stat > CONGESTION_ON_THRESH(
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700917 fsc->mount_options->congestion_kb)) {
918 set_bdi_congested(&fsc->backing_dev_info,
Sage Weil213c99e2010-08-03 10:25:11 -0700919 BLK_RW_ASYNC);
Yehuda Sadeh2baba252009-12-18 13:51:57 -0800920 }
921
Sage Weil1d3576f2009-10-06 11:31:09 -0700922 set_page_writeback(page);
Alex Eldere5975c72013-03-14 14:09:05 -0500923 pages[locked_pages] = page;
Sage Weil1d3576f2009-10-06 11:31:09 -0700924 locked_pages++;
925 next = page->index + 1;
926 }
927
928 /* did we get anything? */
929 if (!locked_pages)
930 goto release_pvec_pages;
931 if (i) {
932 int j;
933 BUG_ON(!locked_pages || first < 0);
934
935 if (pvec_pages && i == pvec_pages &&
936 locked_pages < max_pages) {
937 dout("reached end pvec, trying for more\n");
938 pagevec_reinit(&pvec);
939 goto get_more_pages;
940 }
941
942 /* shift unused pages over in the pvec... we
943 * will need to release them below. */
944 for (j = i; j < pvec_pages; j++) {
945 dout(" pvec leftover page %p\n",
946 pvec.pages[j]);
947 pvec.pages[j-i+first] = pvec.pages[j];
948 }
949 pvec.nr -= i-first;
950 }
951
Alex Eldere5975c72013-03-14 14:09:05 -0500952 /* Format the osd request message and submit the write */
Alex Eldere5975c72013-03-14 14:09:05 -0500953 offset = page_offset(pages[0]);
Yan, Zhenge1966b42015-06-18 03:10:58 +0800954 len = (u64)locked_pages << PAGE_CACHE_SHIFT;
955 if (snap_size == -1) {
956 len = min(len, (u64)i_size_read(inode) - offset);
957 /* writepages_finish() clears writeback pages
958 * according to the data length, so make sure
959 * data length covers all locked pages */
960 len = max(len, 1 +
961 ((u64)(locked_pages - 1) << PAGE_CACHE_SHIFT));
962 } else {
963 len = min(len, snap_size - offset);
964 }
Sage Weil1d3576f2009-10-06 11:31:09 -0700965 dout("writepages got %d pages at %llu~%llu\n",
966 locked_pages, offset, len);
967
Alex Elder406e2c92013-04-15 14:50:36 -0500968 osd_req_op_extent_osd_data_pages(req, 0, pages, len, 0,
Alex Eldera4ce40a2013-04-05 01:27:12 -0500969 !!pool, false);
Alex Eldere5975c72013-03-14 14:09:05 -0500970
971 pages = NULL; /* request message now owns the pages array */
972 pool = NULL;
973
974 /* Update the write op length in case we changed it */
975
Alex Elderc99d2d42013-04-05 01:27:11 -0500976 osd_req_op_extent_update(req, 0, len);
Alex Eldere5975c72013-03-14 14:09:05 -0500977
978 vino = ceph_vino(inode);
Alex Elder79528732013-04-03 21:32:51 -0500979 ceph_osdc_build_request(req, offset, snapc, vino.snap,
980 &inode->i_mtime);
Sage Weil1d3576f2009-10-06 11:31:09 -0700981
Sage Weil9d6fcb02011-05-12 15:48:16 -0700982 rc = ceph_osdc_start_request(&fsc->client->osdc, req, true);
983 BUG_ON(rc);
Sage Weil1d3576f2009-10-06 11:31:09 -0700984 req = NULL;
985
986 /* continue? */
987 index = next;
988 wbc->nr_to_write -= locked_pages;
989 if (wbc->nr_to_write <= 0)
990 done = 1;
991
992release_pvec_pages:
993 dout("pagevec_release on %d pages (%p)\n", (int)pvec.nr,
994 pvec.nr ? pvec.pages[0] : NULL);
995 pagevec_release(&pvec);
996
997 if (locked_pages && !done)
998 goto retry;
999 }
1000
1001 if (should_loop && !done) {
1002 /* more to do; loop back to beginning of file */
1003 dout("writepages looping back to beginning of file\n");
1004 should_loop = 0;
1005 index = 0;
1006 goto retry;
1007 }
1008
1009 if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0))
1010 mapping->writeback_index = index;
1011
1012out:
1013 if (req)
1014 ceph_osdc_put_request(req);
Sage Weil1d3576f2009-10-06 11:31:09 -07001015 ceph_put_snap_context(snapc);
1016 dout("writepages done, rc = %d\n", rc);
Sage Weil1d3576f2009-10-06 11:31:09 -07001017 return rc;
1018}
1019
1020
1021
1022/*
1023 * See if a given @snapc is either writeable, or already written.
1024 */
1025static int context_is_writeable_or_written(struct inode *inode,
1026 struct ceph_snap_context *snapc)
1027{
1028 struct ceph_snap_context *oldest = get_oldest_context(inode, NULL);
Sage Weil6298a332010-03-31 22:01:38 -07001029 int ret = !oldest || snapc->seq <= oldest->seq;
1030
1031 ceph_put_snap_context(oldest);
1032 return ret;
Sage Weil1d3576f2009-10-06 11:31:09 -07001033}
1034
1035/*
1036 * We are only allowed to write into/dirty the page if the page is
1037 * clean, or already dirty within the same snap context.
Sage Weil8f883c22010-03-19 13:27:53 -07001038 *
1039 * called with page locked.
1040 * return success with page locked,
1041 * or any failure (incl -EAGAIN) with page unlocked.
Sage Weil1d3576f2009-10-06 11:31:09 -07001042 */
Yehuda Sadeh4af6b222010-02-09 11:02:51 -08001043static int ceph_update_writeable_page(struct file *file,
1044 loff_t pos, unsigned len,
1045 struct page *page)
Sage Weil1d3576f2009-10-06 11:31:09 -07001046{
Al Viro496ad9a2013-01-23 17:07:38 -05001047 struct inode *inode = file_inode(file);
Sage Weil1d3576f2009-10-06 11:31:09 -07001048 struct ceph_inode_info *ci = ceph_inode(inode);
Sage Weil1d3576f2009-10-06 11:31:09 -07001049 loff_t page_off = pos & PAGE_CACHE_MASK;
1050 int pos_in_page = pos & ~PAGE_CACHE_MASK;
1051 int end_in_page = pos_in_page + len;
1052 loff_t i_size;
Sage Weil1d3576f2009-10-06 11:31:09 -07001053 int r;
Sage Weil80e755f2010-03-31 21:52:10 -07001054 struct ceph_snap_context *snapc, *oldest;
Sage Weil1d3576f2009-10-06 11:31:09 -07001055
Sage Weil1d3576f2009-10-06 11:31:09 -07001056retry_locked:
1057 /* writepages currently holds page lock, but if we change that later, */
1058 wait_on_page_writeback(page);
1059
Yan, Zheng61600ef2012-05-28 14:44:30 +08001060 snapc = page_snap_context(page);
Sage Weil80e755f2010-03-31 21:52:10 -07001061 if (snapc && snapc != ci->i_head_snapc) {
Sage Weil1d3576f2009-10-06 11:31:09 -07001062 /*
1063 * this page is already dirty in another (older) snap
1064 * context! is it writeable now?
1065 */
Sage Weil80e755f2010-03-31 21:52:10 -07001066 oldest = get_oldest_context(inode, NULL);
Sage Weil1d3576f2009-10-06 11:31:09 -07001067
Sage Weil80e755f2010-03-31 21:52:10 -07001068 if (snapc->seq > oldest->seq) {
Sage Weil6298a332010-03-31 22:01:38 -07001069 ceph_put_snap_context(oldest);
Sage Weil1d3576f2009-10-06 11:31:09 -07001070 dout(" page %p snapc %p not current or oldest\n",
Sage Weil6298a332010-03-31 22:01:38 -07001071 page, snapc);
Sage Weil1d3576f2009-10-06 11:31:09 -07001072 /*
1073 * queue for writeback, and wait for snapc to
1074 * be writeable or written
1075 */
Sage Weil6298a332010-03-31 22:01:38 -07001076 snapc = ceph_get_snap_context(snapc);
Sage Weil1d3576f2009-10-06 11:31:09 -07001077 unlock_page(page);
Sage Weil3c6f6b72010-02-09 15:24:44 -08001078 ceph_queue_writeback(inode);
Sage Weil8f883c22010-03-19 13:27:53 -07001079 r = wait_event_interruptible(ci->i_cap_wq,
Sage Weil1d3576f2009-10-06 11:31:09 -07001080 context_is_writeable_or_written(inode, snapc));
1081 ceph_put_snap_context(snapc);
Sage Weil8f883c22010-03-19 13:27:53 -07001082 if (r == -ERESTARTSYS)
1083 return r;
Yehuda Sadeh4af6b222010-02-09 11:02:51 -08001084 return -EAGAIN;
Sage Weil1d3576f2009-10-06 11:31:09 -07001085 }
Sage Weil6298a332010-03-31 22:01:38 -07001086 ceph_put_snap_context(oldest);
Sage Weil1d3576f2009-10-06 11:31:09 -07001087
1088 /* yay, writeable, do it now (without dropping page lock) */
1089 dout(" page %p snapc %p not current, but oldest\n",
1090 page, snapc);
1091 if (!clear_page_dirty_for_io(page))
1092 goto retry_locked;
1093 r = writepage_nounlock(page, NULL);
1094 if (r < 0)
1095 goto fail_nosnap;
1096 goto retry_locked;
1097 }
1098
1099 if (PageUptodate(page)) {
1100 dout(" page %p already uptodate\n", page);
1101 return 0;
1102 }
1103
1104 /* full page? */
1105 if (pos_in_page == 0 && len == PAGE_CACHE_SIZE)
1106 return 0;
1107
1108 /* past end of file? */
1109 i_size = inode->i_size; /* caller holds i_mutex */
1110
Sage Weil1d3576f2009-10-06 11:31:09 -07001111 if (page_off >= i_size ||
1112 (pos_in_page == 0 && (pos+len) >= i_size &&
1113 end_in_page - pos_in_page != PAGE_CACHE_SIZE)) {
1114 dout(" zeroing %p 0 - %d and %d - %d\n",
1115 page, pos_in_page, end_in_page, (int)PAGE_CACHE_SIZE);
1116 zero_user_segments(page,
1117 0, pos_in_page,
1118 end_in_page, PAGE_CACHE_SIZE);
1119 return 0;
1120 }
1121
1122 /* we need to read it. */
Sage Weil1d3576f2009-10-06 11:31:09 -07001123 r = readpage_nounlock(file, page);
1124 if (r < 0)
1125 goto fail_nosnap;
1126 goto retry_locked;
Sage Weil1d3576f2009-10-06 11:31:09 -07001127fail_nosnap:
1128 unlock_page(page);
1129 return r;
1130}
1131
1132/*
Yehuda Sadeh4af6b222010-02-09 11:02:51 -08001133 * We are only allowed to write into/dirty the page if the page is
1134 * clean, or already dirty within the same snap context.
1135 */
1136static int ceph_write_begin(struct file *file, struct address_space *mapping,
1137 loff_t pos, unsigned len, unsigned flags,
1138 struct page **pagep, void **fsdata)
1139{
Al Viro496ad9a2013-01-23 17:07:38 -05001140 struct inode *inode = file_inode(file);
Yehuda Sadeh4af6b222010-02-09 11:02:51 -08001141 struct page *page;
1142 pgoff_t index = pos >> PAGE_CACHE_SHIFT;
Sage Weil7971bd92013-05-01 21:15:58 -07001143 int r;
Yehuda Sadeh4af6b222010-02-09 11:02:51 -08001144
1145 do {
Sage Weil8f883c22010-03-19 13:27:53 -07001146 /* get a page */
Yehuda Sadeh4af6b222010-02-09 11:02:51 -08001147 page = grab_cache_page_write_begin(mapping, index, 0);
Sage Weil7971bd92013-05-01 21:15:58 -07001148 if (!page)
1149 return -ENOMEM;
1150 *pagep = page;
Yehuda Sadeh4af6b222010-02-09 11:02:51 -08001151
1152 dout("write_begin file %p inode %p page %p %d~%d\n", file,
Sage Weil213c99e2010-08-03 10:25:11 -07001153 inode, page, (int)pos, (int)len);
Yehuda Sadeh4af6b222010-02-09 11:02:51 -08001154
1155 r = ceph_update_writeable_page(file, pos, len, page);
Taesoo Kimc1d00b22015-03-20 17:36:56 -04001156 if (r < 0)
1157 page_cache_release(page);
1158 else
1159 *pagep = page;
Yehuda Sadeh4af6b222010-02-09 11:02:51 -08001160 } while (r == -EAGAIN);
1161
1162 return r;
1163}
1164
1165/*
Sage Weil1d3576f2009-10-06 11:31:09 -07001166 * we don't do anything in here that simple_write_end doesn't do
Yan, Zheng5dda377c2015-04-30 14:40:54 +08001167 * except adjust dirty page accounting
Sage Weil1d3576f2009-10-06 11:31:09 -07001168 */
1169static int ceph_write_end(struct file *file, struct address_space *mapping,
1170 loff_t pos, unsigned len, unsigned copied,
1171 struct page *page, void *fsdata)
1172{
Al Viro496ad9a2013-01-23 17:07:38 -05001173 struct inode *inode = file_inode(file);
Sage Weil1d3576f2009-10-06 11:31:09 -07001174 unsigned from = pos & (PAGE_CACHE_SIZE - 1);
1175 int check_cap = 0;
1176
1177 dout("write_end file %p inode %p page %p %d~%d (%d)\n", file,
1178 inode, page, (int)pos, (int)copied, (int)len);
1179
1180 /* zero the stale part of the page if we did a short copy */
1181 if (copied < len)
1182 zero_user_segment(page, from+copied, len);
1183
1184 /* did file size increase? */
1185 /* (no need for i_size_read(); we caller holds i_mutex */
1186 if (pos+copied > inode->i_size)
1187 check_cap = ceph_inode_set_size(inode, pos+copied);
1188
1189 if (!PageUptodate(page))
1190 SetPageUptodate(page);
1191
1192 set_page_dirty(page);
1193
1194 unlock_page(page);
Sage Weil1d3576f2009-10-06 11:31:09 -07001195 page_cache_release(page);
1196
1197 if (check_cap)
1198 ceph_check_caps(ceph_inode(inode), CHECK_CAPS_AUTHONLY, NULL);
1199
1200 return copied;
1201}
1202
1203/*
1204 * we set .direct_IO to indicate direct io is supported, but since we
1205 * intercept O_DIRECT reads and writes early, this function should
1206 * never get called.
1207 */
Omar Sandoval22c61862015-03-16 04:33:53 -07001208static ssize_t ceph_direct_io(struct kiocb *iocb, struct iov_iter *iter,
Al Virod8d3d942014-03-04 21:27:34 -05001209 loff_t pos)
Sage Weil1d3576f2009-10-06 11:31:09 -07001210{
1211 WARN_ON(1);
1212 return -EINVAL;
1213}
1214
1215const struct address_space_operations ceph_aops = {
1216 .readpage = ceph_readpage,
1217 .readpages = ceph_readpages,
1218 .writepage = ceph_writepage,
1219 .writepages = ceph_writepages_start,
1220 .write_begin = ceph_write_begin,
1221 .write_end = ceph_write_end,
1222 .set_page_dirty = ceph_set_page_dirty,
1223 .invalidatepage = ceph_invalidatepage,
1224 .releasepage = ceph_releasepage,
1225 .direct_IO = ceph_direct_io,
1226};
1227
1228
1229/*
1230 * vm ops
1231 */
Yan, Zheng61f68812013-11-28 14:28:14 +08001232static int ceph_filemap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
1233{
1234 struct inode *inode = file_inode(vma->vm_file);
1235 struct ceph_inode_info *ci = ceph_inode(inode);
1236 struct ceph_file_info *fi = vma->vm_file->private_data;
Yan, Zheng3738daa2014-11-14 22:10:07 +08001237 struct page *pinned_page = NULL;
Yan, Zheng61f68812013-11-28 14:28:14 +08001238 loff_t off = vmf->pgoff << PAGE_CACHE_SHIFT;
1239 int want, got, ret;
1240
1241 dout("filemap_fault %p %llx.%llx %llu~%zd trying to get caps\n",
Ilya Dryomov37b52fe2014-01-28 18:29:29 +02001242 inode, ceph_vinop(inode), off, (size_t)PAGE_CACHE_SIZE);
Yan, Zheng61f68812013-11-28 14:28:14 +08001243 if (fi->fmode & CEPH_FILE_MODE_LAZY)
1244 want = CEPH_CAP_FILE_CACHE | CEPH_CAP_FILE_LAZYIO;
1245 else
1246 want = CEPH_CAP_FILE_CACHE;
1247 while (1) {
1248 got = 0;
Yan, Zheng83701242014-11-14 22:36:18 +08001249 ret = ceph_get_caps(ci, CEPH_CAP_FILE_RD, want,
1250 -1, &got, &pinned_page);
Yan, Zheng61f68812013-11-28 14:28:14 +08001251 if (ret == 0)
1252 break;
1253 if (ret != -ERESTARTSYS) {
1254 WARN_ON(1);
1255 return VM_FAULT_SIGBUS;
1256 }
1257 }
1258 dout("filemap_fault %p %llu~%zd got cap refs on %s\n",
Ilya Dryomov37b52fe2014-01-28 18:29:29 +02001259 inode, off, (size_t)PAGE_CACHE_SIZE, ceph_cap_string(got));
Yan, Zheng61f68812013-11-28 14:28:14 +08001260
Yan, Zheng83701242014-11-14 22:36:18 +08001261 if ((got & (CEPH_CAP_FILE_CACHE | CEPH_CAP_FILE_LAZYIO)) ||
1262 ci->i_inline_version == CEPH_INLINE_NONE)
1263 ret = filemap_fault(vma, vmf);
1264 else
1265 ret = -EAGAIN;
Yan, Zheng61f68812013-11-28 14:28:14 +08001266
1267 dout("filemap_fault %p %llu~%zd dropping cap refs on %s ret %d\n",
Ilya Dryomov37b52fe2014-01-28 18:29:29 +02001268 inode, off, (size_t)PAGE_CACHE_SIZE, ceph_cap_string(got), ret);
Yan, Zheng3738daa2014-11-14 22:10:07 +08001269 if (pinned_page)
1270 page_cache_release(pinned_page);
Yan, Zheng61f68812013-11-28 14:28:14 +08001271 ceph_put_cap_refs(ci, got);
1272
Yan, Zheng83701242014-11-14 22:36:18 +08001273 if (ret != -EAGAIN)
1274 return ret;
1275
1276 /* read inline data */
1277 if (off >= PAGE_CACHE_SIZE) {
1278 /* does not support inline data > PAGE_SIZE */
1279 ret = VM_FAULT_SIGBUS;
1280 } else {
1281 int ret1;
1282 struct address_space *mapping = inode->i_mapping;
1283 struct page *page = find_or_create_page(mapping, 0,
1284 mapping_gfp_mask(mapping) &
1285 ~__GFP_FS);
1286 if (!page) {
1287 ret = VM_FAULT_OOM;
1288 goto out;
1289 }
1290 ret1 = __ceph_do_getattr(inode, page,
1291 CEPH_STAT_CAP_INLINE_DATA, true);
1292 if (ret1 < 0 || off >= i_size_read(inode)) {
1293 unlock_page(page);
1294 page_cache_release(page);
1295 ret = VM_FAULT_SIGBUS;
1296 goto out;
1297 }
1298 if (ret1 < PAGE_CACHE_SIZE)
1299 zero_user_segment(page, ret1, PAGE_CACHE_SIZE);
1300 else
1301 flush_dcache_page(page);
1302 SetPageUptodate(page);
1303 vmf->page = page;
1304 ret = VM_FAULT_MAJOR | VM_FAULT_LOCKED;
1305 }
1306out:
1307 dout("filemap_fault %p %llu~%zd read inline data ret %d\n",
1308 inode, off, (size_t)PAGE_CACHE_SIZE, ret);
Yan, Zheng61f68812013-11-28 14:28:14 +08001309 return ret;
1310}
Sage Weil1d3576f2009-10-06 11:31:09 -07001311
1312/*
1313 * Reuse write_begin here for simplicity.
1314 */
1315static int ceph_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
1316{
Al Viro496ad9a2013-01-23 17:07:38 -05001317 struct inode *inode = file_inode(vma->vm_file);
Yan, Zheng61f68812013-11-28 14:28:14 +08001318 struct ceph_inode_info *ci = ceph_inode(inode);
1319 struct ceph_file_info *fi = vma->vm_file->private_data;
Yan, Zhengf66fd9f2015-06-10 17:26:13 +08001320 struct ceph_cap_flush *prealloc_cf;
Yan, Zheng61f68812013-11-28 14:28:14 +08001321 struct page *page = vmf->page;
Alex Elder6285bc22012-10-02 10:25:51 -05001322 loff_t off = page_offset(page);
Yan, Zheng61f68812013-11-28 14:28:14 +08001323 loff_t size = i_size_read(inode);
1324 size_t len;
1325 int want, got, ret;
Sage Weil1d3576f2009-10-06 11:31:09 -07001326
Yan, Zhengf66fd9f2015-06-10 17:26:13 +08001327 prealloc_cf = ceph_alloc_cap_flush();
1328 if (!prealloc_cf)
1329 return VM_FAULT_SIGBUS;
1330
Yan, Zheng28127bd2014-11-14 22:38:29 +08001331 if (ci->i_inline_version != CEPH_INLINE_NONE) {
1332 struct page *locked_page = NULL;
1333 if (off == 0) {
1334 lock_page(page);
1335 locked_page = page;
1336 }
1337 ret = ceph_uninline_data(vma->vm_file, locked_page);
1338 if (locked_page)
1339 unlock_page(locked_page);
Yan, Zhengf66fd9f2015-06-10 17:26:13 +08001340 if (ret < 0) {
1341 ret = VM_FAULT_SIGBUS;
1342 goto out_free;
1343 }
Yan, Zheng28127bd2014-11-14 22:38:29 +08001344 }
1345
Sage Weil1d3576f2009-10-06 11:31:09 -07001346 if (off + PAGE_CACHE_SIZE <= size)
1347 len = PAGE_CACHE_SIZE;
1348 else
1349 len = size & ~PAGE_CACHE_MASK;
1350
Yan, Zheng61f68812013-11-28 14:28:14 +08001351 dout("page_mkwrite %p %llx.%llx %llu~%zd getting caps i_size %llu\n",
1352 inode, ceph_vinop(inode), off, len, size);
1353 if (fi->fmode & CEPH_FILE_MODE_LAZY)
1354 want = CEPH_CAP_FILE_BUFFER | CEPH_CAP_FILE_LAZYIO;
1355 else
1356 want = CEPH_CAP_FILE_BUFFER;
1357 while (1) {
1358 got = 0;
Yan, Zheng3738daa2014-11-14 22:10:07 +08001359 ret = ceph_get_caps(ci, CEPH_CAP_FILE_WR, want, off + len,
1360 &got, NULL);
Yan, Zheng61f68812013-11-28 14:28:14 +08001361 if (ret == 0)
1362 break;
1363 if (ret != -ERESTARTSYS) {
1364 WARN_ON(1);
Yan, Zhengf66fd9f2015-06-10 17:26:13 +08001365 ret = VM_FAULT_SIGBUS;
1366 goto out_free;
Yan, Zheng61f68812013-11-28 14:28:14 +08001367 }
1368 }
1369 dout("page_mkwrite %p %llu~%zd got cap refs on %s\n",
1370 inode, off, len, ceph_cap_string(got));
1371
1372 /* Update time before taking page lock */
1373 file_update_time(vma->vm_file);
Yehuda Sadeh4af6b222010-02-09 11:02:51 -08001374
1375 lock_page(page);
1376
1377 ret = VM_FAULT_NOPAGE;
1378 if ((off > size) ||
1379 (page->mapping != inode->i_mapping))
1380 goto out;
1381
1382 ret = ceph_update_writeable_page(vma->vm_file, off, len, page);
1383 if (ret == 0) {
1384 /* success. we'll keep the page locked. */
Sage Weil1d3576f2009-10-06 11:31:09 -07001385 set_page_dirty(page);
Sage Weil1d3576f2009-10-06 11:31:09 -07001386 ret = VM_FAULT_LOCKED;
1387 } else {
Yehuda Sadeh4af6b222010-02-09 11:02:51 -08001388 if (ret == -ENOMEM)
1389 ret = VM_FAULT_OOM;
1390 else
1391 ret = VM_FAULT_SIGBUS;
Sage Weil1d3576f2009-10-06 11:31:09 -07001392 }
Yehuda Sadeh4af6b222010-02-09 11:02:51 -08001393out:
Yan, Zheng28127bd2014-11-14 22:38:29 +08001394 if (ret != VM_FAULT_LOCKED)
Yehuda Sadeh4af6b222010-02-09 11:02:51 -08001395 unlock_page(page);
Yan, Zheng28127bd2014-11-14 22:38:29 +08001396 if (ret == VM_FAULT_LOCKED ||
1397 ci->i_inline_version != CEPH_INLINE_NONE) {
Yan, Zheng61f68812013-11-28 14:28:14 +08001398 int dirty;
1399 spin_lock(&ci->i_ceph_lock);
Yan, Zheng28127bd2014-11-14 22:38:29 +08001400 ci->i_inline_version = CEPH_INLINE_NONE;
Yan, Zhengf66fd9f2015-06-10 17:26:13 +08001401 dirty = __ceph_mark_dirty_caps(ci, CEPH_CAP_FILE_WR,
1402 &prealloc_cf);
Yan, Zheng61f68812013-11-28 14:28:14 +08001403 spin_unlock(&ci->i_ceph_lock);
1404 if (dirty)
1405 __mark_inode_dirty(inode, dirty);
1406 }
1407
1408 dout("page_mkwrite %p %llu~%zd dropping cap refs on %s ret %d\n",
1409 inode, off, len, ceph_cap_string(got), ret);
1410 ceph_put_cap_refs(ci, got);
Yan, Zhengf66fd9f2015-06-10 17:26:13 +08001411out_free:
1412 ceph_free_cap_flush(prealloc_cf);
Yan, Zheng61f68812013-11-28 14:28:14 +08001413
Sage Weil1d3576f2009-10-06 11:31:09 -07001414 return ret;
1415}
1416
Yan, Zheng31c542a2014-11-14 21:41:55 +08001417void ceph_fill_inline_data(struct inode *inode, struct page *locked_page,
1418 char *data, size_t len)
1419{
1420 struct address_space *mapping = inode->i_mapping;
1421 struct page *page;
1422
1423 if (locked_page) {
1424 page = locked_page;
1425 } else {
1426 if (i_size_read(inode) == 0)
1427 return;
1428 page = find_or_create_page(mapping, 0,
1429 mapping_gfp_mask(mapping) & ~__GFP_FS);
1430 if (!page)
1431 return;
1432 if (PageUptodate(page)) {
1433 unlock_page(page);
1434 page_cache_release(page);
1435 return;
1436 }
1437 }
1438
Ilya Dryomov0668ff52014-12-19 13:10:10 +03001439 dout("fill_inline_data %p %llx.%llx len %zu locked_page %p\n",
Yan, Zheng31c542a2014-11-14 21:41:55 +08001440 inode, ceph_vinop(inode), len, locked_page);
1441
1442 if (len > 0) {
1443 void *kaddr = kmap_atomic(page);
1444 memcpy(kaddr, data, len);
1445 kunmap_atomic(kaddr);
1446 }
1447
1448 if (page != locked_page) {
1449 if (len < PAGE_CACHE_SIZE)
1450 zero_user_segment(page, len, PAGE_CACHE_SIZE);
1451 else
1452 flush_dcache_page(page);
1453
1454 SetPageUptodate(page);
1455 unlock_page(page);
1456 page_cache_release(page);
1457 }
1458}
1459
Yan, Zheng28127bd2014-11-14 22:38:29 +08001460int ceph_uninline_data(struct file *filp, struct page *locked_page)
1461{
1462 struct inode *inode = file_inode(filp);
1463 struct ceph_inode_info *ci = ceph_inode(inode);
1464 struct ceph_fs_client *fsc = ceph_inode_to_client(inode);
1465 struct ceph_osd_request *req;
1466 struct page *page = NULL;
1467 u64 len, inline_version;
1468 int err = 0;
1469 bool from_pagecache = false;
1470
1471 spin_lock(&ci->i_ceph_lock);
1472 inline_version = ci->i_inline_version;
1473 spin_unlock(&ci->i_ceph_lock);
1474
1475 dout("uninline_data %p %llx.%llx inline_version %llu\n",
1476 inode, ceph_vinop(inode), inline_version);
1477
1478 if (inline_version == 1 || /* initial version, no data */
1479 inline_version == CEPH_INLINE_NONE)
1480 goto out;
1481
1482 if (locked_page) {
1483 page = locked_page;
1484 WARN_ON(!PageUptodate(page));
1485 } else if (ceph_caps_issued(ci) &
1486 (CEPH_CAP_FILE_CACHE|CEPH_CAP_FILE_LAZYIO)) {
1487 page = find_get_page(inode->i_mapping, 0);
1488 if (page) {
1489 if (PageUptodate(page)) {
1490 from_pagecache = true;
1491 lock_page(page);
1492 } else {
1493 page_cache_release(page);
1494 page = NULL;
1495 }
1496 }
1497 }
1498
1499 if (page) {
1500 len = i_size_read(inode);
1501 if (len > PAGE_CACHE_SIZE)
1502 len = PAGE_CACHE_SIZE;
1503 } else {
1504 page = __page_cache_alloc(GFP_NOFS);
1505 if (!page) {
1506 err = -ENOMEM;
1507 goto out;
1508 }
1509 err = __ceph_do_getattr(inode, page,
1510 CEPH_STAT_CAP_INLINE_DATA, true);
1511 if (err < 0) {
1512 /* no inline data */
1513 if (err == -ENODATA)
1514 err = 0;
1515 goto out;
1516 }
1517 len = err;
1518 }
1519
1520 req = ceph_osdc_new_request(&fsc->client->osdc, &ci->i_layout,
1521 ceph_vino(inode), 0, &len, 0, 1,
1522 CEPH_OSD_OP_CREATE,
1523 CEPH_OSD_FLAG_ONDISK | CEPH_OSD_FLAG_WRITE,
Yan, Zheng7b06a822015-05-01 10:03:40 +08001524 ceph_empty_snapc, 0, 0, false);
Yan, Zheng28127bd2014-11-14 22:38:29 +08001525 if (IS_ERR(req)) {
1526 err = PTR_ERR(req);
1527 goto out;
1528 }
1529
1530 ceph_osdc_build_request(req, 0, NULL, CEPH_NOSNAP, &inode->i_mtime);
1531 err = ceph_osdc_start_request(&fsc->client->osdc, req, false);
1532 if (!err)
1533 err = ceph_osdc_wait_request(&fsc->client->osdc, req);
1534 ceph_osdc_put_request(req);
1535 if (err < 0)
1536 goto out;
1537
1538 req = ceph_osdc_new_request(&fsc->client->osdc, &ci->i_layout,
1539 ceph_vino(inode), 0, &len, 1, 3,
1540 CEPH_OSD_OP_WRITE,
1541 CEPH_OSD_FLAG_ONDISK | CEPH_OSD_FLAG_WRITE,
Yan, Zheng7b06a822015-05-01 10:03:40 +08001542 ceph_empty_snapc,
Yan, Zheng28127bd2014-11-14 22:38:29 +08001543 ci->i_truncate_seq, ci->i_truncate_size,
1544 false);
1545 if (IS_ERR(req)) {
1546 err = PTR_ERR(req);
1547 goto out;
1548 }
1549
1550 osd_req_op_extent_osd_data_pages(req, 1, &page, len, 0, false, false);
1551
Yan, Zhengec137c12015-04-13 11:25:07 +08001552 {
1553 __le64 xattr_buf = cpu_to_le64(inline_version);
1554 err = osd_req_op_xattr_init(req, 0, CEPH_OSD_OP_CMPXATTR,
1555 "inline_version", &xattr_buf,
1556 sizeof(xattr_buf),
1557 CEPH_OSD_CMPXATTR_OP_GT,
1558 CEPH_OSD_CMPXATTR_MODE_U64);
1559 if (err)
1560 goto out_put;
1561 }
Yan, Zheng28127bd2014-11-14 22:38:29 +08001562
Yan, Zhengec137c12015-04-13 11:25:07 +08001563 {
1564 char xattr_buf[32];
1565 int xattr_len = snprintf(xattr_buf, sizeof(xattr_buf),
1566 "%llu", inline_version);
1567 err = osd_req_op_xattr_init(req, 2, CEPH_OSD_OP_SETXATTR,
1568 "inline_version",
1569 xattr_buf, xattr_len, 0, 0);
1570 if (err)
1571 goto out_put;
1572 }
Yan, Zheng28127bd2014-11-14 22:38:29 +08001573
1574 ceph_osdc_build_request(req, 0, NULL, CEPH_NOSNAP, &inode->i_mtime);
1575 err = ceph_osdc_start_request(&fsc->client->osdc, req, false);
1576 if (!err)
1577 err = ceph_osdc_wait_request(&fsc->client->osdc, req);
1578out_put:
1579 ceph_osdc_put_request(req);
1580 if (err == -ECANCELED)
1581 err = 0;
1582out:
1583 if (page && page != locked_page) {
1584 if (from_pagecache) {
1585 unlock_page(page);
1586 page_cache_release(page);
1587 } else
1588 __free_pages(page, 0);
1589 }
1590
1591 dout("uninline_data %p %llx.%llx inline_version %llu = %d\n",
1592 inode, ceph_vinop(inode), inline_version, err);
1593 return err;
1594}
1595
Sage Weil1d3576f2009-10-06 11:31:09 -07001596static struct vm_operations_struct ceph_vmops = {
Yan, Zheng61f68812013-11-28 14:28:14 +08001597 .fault = ceph_filemap_fault,
Sage Weil1d3576f2009-10-06 11:31:09 -07001598 .page_mkwrite = ceph_page_mkwrite,
1599};
1600
1601int ceph_mmap(struct file *file, struct vm_area_struct *vma)
1602{
1603 struct address_space *mapping = file->f_mapping;
1604
1605 if (!mapping->a_ops->readpage)
1606 return -ENOEXEC;
1607 file_accessed(file);
1608 vma->vm_ops = &ceph_vmops;
Sage Weil1d3576f2009-10-06 11:31:09 -07001609 return 0;
1610}
Yan, Zheng10183a62015-04-27 15:33:28 +08001611
1612enum {
1613 POOL_READ = 1,
1614 POOL_WRITE = 2,
1615};
1616
1617static int __ceph_pool_perm_get(struct ceph_inode_info *ci, u32 pool)
1618{
1619 struct ceph_fs_client *fsc = ceph_inode_to_client(&ci->vfs_inode);
1620 struct ceph_mds_client *mdsc = fsc->mdsc;
1621 struct ceph_osd_request *rd_req = NULL, *wr_req = NULL;
1622 struct rb_node **p, *parent;
1623 struct ceph_pool_perm *perm;
1624 struct page **pages;
1625 int err = 0, err2 = 0, have = 0;
1626
1627 down_read(&mdsc->pool_perm_rwsem);
1628 p = &mdsc->pool_perm_tree.rb_node;
1629 while (*p) {
1630 perm = rb_entry(*p, struct ceph_pool_perm, node);
1631 if (pool < perm->pool)
1632 p = &(*p)->rb_left;
1633 else if (pool > perm->pool)
1634 p = &(*p)->rb_right;
1635 else {
1636 have = perm->perm;
1637 break;
1638 }
1639 }
1640 up_read(&mdsc->pool_perm_rwsem);
1641 if (*p)
1642 goto out;
1643
1644 dout("__ceph_pool_perm_get pool %u no perm cached\n", pool);
1645
1646 down_write(&mdsc->pool_perm_rwsem);
1647 parent = NULL;
1648 while (*p) {
1649 parent = *p;
1650 perm = rb_entry(parent, struct ceph_pool_perm, node);
1651 if (pool < perm->pool)
1652 p = &(*p)->rb_left;
1653 else if (pool > perm->pool)
1654 p = &(*p)->rb_right;
1655 else {
1656 have = perm->perm;
1657 break;
1658 }
1659 }
1660 if (*p) {
1661 up_write(&mdsc->pool_perm_rwsem);
1662 goto out;
1663 }
1664
1665 rd_req = ceph_osdc_alloc_request(&fsc->client->osdc,
Yan, Zheng7b06a822015-05-01 10:03:40 +08001666 ceph_empty_snapc,
Yan, Zheng10183a62015-04-27 15:33:28 +08001667 1, false, GFP_NOFS);
1668 if (!rd_req) {
1669 err = -ENOMEM;
1670 goto out_unlock;
1671 }
1672
1673 rd_req->r_flags = CEPH_OSD_FLAG_READ;
1674 osd_req_op_init(rd_req, 0, CEPH_OSD_OP_STAT, 0);
1675 rd_req->r_base_oloc.pool = pool;
1676 snprintf(rd_req->r_base_oid.name, sizeof(rd_req->r_base_oid.name),
1677 "%llx.00000000", ci->i_vino.ino);
1678 rd_req->r_base_oid.name_len = strlen(rd_req->r_base_oid.name);
1679
1680 wr_req = ceph_osdc_alloc_request(&fsc->client->osdc,
Yan, Zheng7b06a822015-05-01 10:03:40 +08001681 ceph_empty_snapc,
Yan, Zheng10183a62015-04-27 15:33:28 +08001682 1, false, GFP_NOFS);
1683 if (!wr_req) {
1684 err = -ENOMEM;
1685 goto out_unlock;
1686 }
1687
1688 wr_req->r_flags = CEPH_OSD_FLAG_WRITE |
1689 CEPH_OSD_FLAG_ACK | CEPH_OSD_FLAG_ONDISK;
1690 osd_req_op_init(wr_req, 0, CEPH_OSD_OP_CREATE, CEPH_OSD_OP_FLAG_EXCL);
1691 wr_req->r_base_oloc.pool = pool;
1692 wr_req->r_base_oid = rd_req->r_base_oid;
1693
1694 /* one page should be large enough for STAT data */
1695 pages = ceph_alloc_page_vector(1, GFP_KERNEL);
1696 if (IS_ERR(pages)) {
1697 err = PTR_ERR(pages);
1698 goto out_unlock;
1699 }
1700
1701 osd_req_op_raw_data_in_pages(rd_req, 0, pages, PAGE_SIZE,
1702 0, false, true);
1703 ceph_osdc_build_request(rd_req, 0, NULL, CEPH_NOSNAP,
1704 &ci->vfs_inode.i_mtime);
1705 err = ceph_osdc_start_request(&fsc->client->osdc, rd_req, false);
1706
1707 ceph_osdc_build_request(wr_req, 0, NULL, CEPH_NOSNAP,
1708 &ci->vfs_inode.i_mtime);
1709 err2 = ceph_osdc_start_request(&fsc->client->osdc, wr_req, false);
1710
1711 if (!err)
1712 err = ceph_osdc_wait_request(&fsc->client->osdc, rd_req);
1713 if (!err2)
1714 err2 = ceph_osdc_wait_request(&fsc->client->osdc, wr_req);
1715
1716 if (err >= 0 || err == -ENOENT)
1717 have |= POOL_READ;
1718 else if (err != -EPERM)
1719 goto out_unlock;
1720
1721 if (err2 == 0 || err2 == -EEXIST)
1722 have |= POOL_WRITE;
1723 else if (err2 != -EPERM) {
1724 err = err2;
1725 goto out_unlock;
1726 }
1727
1728 perm = kmalloc(sizeof(*perm), GFP_NOFS);
1729 if (!perm) {
1730 err = -ENOMEM;
1731 goto out_unlock;
1732 }
1733
1734 perm->pool = pool;
1735 perm->perm = have;
1736 rb_link_node(&perm->node, parent, p);
1737 rb_insert_color(&perm->node, &mdsc->pool_perm_tree);
1738 err = 0;
1739out_unlock:
1740 up_write(&mdsc->pool_perm_rwsem);
1741
1742 if (rd_req)
1743 ceph_osdc_put_request(rd_req);
1744 if (wr_req)
1745 ceph_osdc_put_request(wr_req);
1746out:
1747 if (!err)
1748 err = have;
1749 dout("__ceph_pool_perm_get pool %u result = %d\n", pool, err);
1750 return err;
1751}
1752
1753int ceph_pool_perm_check(struct ceph_inode_info *ci, int need)
1754{
1755 u32 pool;
1756 int ret, flags;
1757
1758 if (ceph_test_mount_opt(ceph_inode_to_client(&ci->vfs_inode),
1759 NOPOOLPERM))
1760 return 0;
1761
1762 spin_lock(&ci->i_ceph_lock);
1763 flags = ci->i_ceph_flags;
1764 pool = ceph_file_layout_pg_pool(ci->i_layout);
1765 spin_unlock(&ci->i_ceph_lock);
1766check:
1767 if (flags & CEPH_I_POOL_PERM) {
1768 if ((need & CEPH_CAP_FILE_RD) && !(flags & CEPH_I_POOL_RD)) {
1769 dout("ceph_pool_perm_check pool %u no read perm\n",
1770 pool);
1771 return -EPERM;
1772 }
1773 if ((need & CEPH_CAP_FILE_WR) && !(flags & CEPH_I_POOL_WR)) {
1774 dout("ceph_pool_perm_check pool %u no write perm\n",
1775 pool);
1776 return -EPERM;
1777 }
1778 return 0;
1779 }
1780
1781 ret = __ceph_pool_perm_get(ci, pool);
1782 if (ret < 0)
1783 return ret;
1784
1785 flags = CEPH_I_POOL_PERM;
1786 if (ret & POOL_READ)
1787 flags |= CEPH_I_POOL_RD;
1788 if (ret & POOL_WRITE)
1789 flags |= CEPH_I_POOL_WR;
1790
1791 spin_lock(&ci->i_ceph_lock);
1792 if (pool == ceph_file_layout_pg_pool(ci->i_layout)) {
1793 ci->i_ceph_flags = flags;
1794 } else {
1795 pool = ceph_file_layout_pg_pool(ci->i_layout);
1796 flags = ci->i_ceph_flags;
1797 }
1798 spin_unlock(&ci->i_ceph_lock);
1799 goto check;
1800}
1801
1802void ceph_pool_perm_destroy(struct ceph_mds_client *mdsc)
1803{
1804 struct ceph_pool_perm *perm;
1805 struct rb_node *n;
1806
1807 while (!RB_EMPTY_ROOT(&mdsc->pool_perm_tree)) {
1808 n = rb_first(&mdsc->pool_perm_tree);
1809 perm = rb_entry(n, struct ceph_pool_perm, node);
1810 rb_erase(n, &mdsc->pool_perm_tree);
1811 kfree(perm);
1812 }
1813}