blob: 3bed7da383263a3ba967c6158b2ff6bd1bf5c4af [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"
14#include <linux/ceph/osd_client.h>
Sage Weil1d3576f2009-10-06 11:31:09 -070015
16/*
17 * Ceph address space ops.
18 *
19 * There are a few funny things going on here.
20 *
21 * The page->private field is used to reference a struct
22 * ceph_snap_context for _every_ dirty page. This indicates which
23 * snapshot the page was logically dirtied in, and thus which snap
24 * context needs to be associated with the osd write during writeback.
25 *
26 * Similarly, struct ceph_inode_info maintains a set of counters to
Lucas De Marchi25985ed2011-03-30 22:57:33 -030027 * count dirty pages on the inode. In the absence of snapshots,
Sage Weil1d3576f2009-10-06 11:31:09 -070028 * i_wrbuffer_ref == i_wrbuffer_ref_head == the dirty page count.
29 *
30 * When a snapshot is taken (that is, when the client receives
31 * notification that a snapshot was taken), each inode with caps and
32 * with dirty pages (dirty pages implies there is a cap) gets a new
33 * ceph_cap_snap in the i_cap_snaps list (which is sorted in ascending
34 * order, new snaps go to the tail). The i_wrbuffer_ref_head count is
35 * moved to capsnap->dirty. (Unless a sync write is currently in
36 * progress. In that case, the capsnap is said to be "pending", new
37 * writes cannot start, and the capsnap isn't "finalized" until the
38 * write completes (or fails) and a final size/mtime for the inode for
39 * that snap can be settled upon.) i_wrbuffer_ref_head is reset to 0.
40 *
41 * On writeback, we must submit writes to the osd IN SNAP ORDER. So,
42 * we look for the first capsnap in i_cap_snaps and write out pages in
43 * that snap context _only_. Then we move on to the next capsnap,
44 * eventually reaching the "live" or "head" context (i.e., pages that
45 * are not yet snapped) and are writing the most recently dirtied
46 * pages.
47 *
48 * Invalidate and so forth must take care to ensure the dirty page
49 * accounting is preserved.
50 */
51
Yehuda Sadeh2baba252009-12-18 13:51:57 -080052#define CONGESTION_ON_THRESH(congestion_kb) (congestion_kb >> (PAGE_SHIFT-10))
53#define CONGESTION_OFF_THRESH(congestion_kb) \
54 (CONGESTION_ON_THRESH(congestion_kb) - \
55 (CONGESTION_ON_THRESH(congestion_kb) >> 2))
56
Yan, Zheng61600ef2012-05-28 14:44:30 +080057static inline struct ceph_snap_context *page_snap_context(struct page *page)
58{
59 if (PagePrivate(page))
60 return (void *)page->private;
61 return NULL;
62}
Sage Weil1d3576f2009-10-06 11:31:09 -070063
64/*
65 * Dirty a page. Optimistically adjust accounting, on the assumption
66 * that we won't race with invalidate. If we do, readjust.
67 */
68static int ceph_set_page_dirty(struct page *page)
69{
70 struct address_space *mapping = page->mapping;
71 struct inode *inode;
72 struct ceph_inode_info *ci;
Sage Weil1d3576f2009-10-06 11:31:09 -070073 struct ceph_snap_context *snapc;
Sha Zhengju7d6e1f52013-08-21 16:27:34 +080074 int ret;
Sage Weil1d3576f2009-10-06 11:31:09 -070075
76 if (unlikely(!mapping))
77 return !TestSetPageDirty(page);
78
Sha Zhengju7d6e1f52013-08-21 16:27:34 +080079 if (PageDirty(page)) {
Sage Weil1d3576f2009-10-06 11:31:09 -070080 dout("%p set_page_dirty %p idx %lu -- already dirty\n",
81 mapping->host, page, page->index);
Sha Zhengju7d6e1f52013-08-21 16:27:34 +080082 BUG_ON(!PagePrivate(page));
Sage Weil1d3576f2009-10-06 11:31:09 -070083 return 0;
84 }
85
86 inode = mapping->host;
87 ci = ceph_inode(inode);
88
89 /*
90 * Note that we're grabbing a snapc ref here without holding
91 * any locks!
92 */
93 snapc = ceph_get_snap_context(ci->i_snap_realm->cached_context);
94
95 /* dirty the head */
Sage Weilbe655592011-11-30 09:47:09 -080096 spin_lock(&ci->i_ceph_lock);
Sage Weil7d8cb262010-08-24 08:44:16 -070097 if (ci->i_head_snapc == NULL)
Sage Weil1d3576f2009-10-06 11:31:09 -070098 ci->i_head_snapc = ceph_get_snap_context(snapc);
99 ++ci->i_wrbuffer_ref_head;
100 if (ci->i_wrbuffer_ref == 0)
Dave Chinner0444d762011-03-29 18:08:50 +1100101 ihold(inode);
Sage Weil1d3576f2009-10-06 11:31:09 -0700102 ++ci->i_wrbuffer_ref;
103 dout("%p set_page_dirty %p idx %lu head %d/%d -> %d/%d "
104 "snapc %p seq %lld (%d snaps)\n",
105 mapping->host, page, page->index,
106 ci->i_wrbuffer_ref-1, ci->i_wrbuffer_ref_head-1,
107 ci->i_wrbuffer_ref, ci->i_wrbuffer_ref_head,
108 snapc, snapc->seq, snapc->num_snaps);
Sage Weilbe655592011-11-30 09:47:09 -0800109 spin_unlock(&ci->i_ceph_lock);
Sage Weil1d3576f2009-10-06 11:31:09 -0700110
Sha Zhengju7d6e1f52013-08-21 16:27:34 +0800111 /*
112 * Reference snap context in page->private. Also set
113 * PagePrivate so that we get invalidatepage callback.
114 */
115 BUG_ON(PagePrivate(page));
116 page->private = (unsigned long)snapc;
117 SetPagePrivate(page);
Sage Weil1d3576f2009-10-06 11:31:09 -0700118
Sha Zhengju7d6e1f52013-08-21 16:27:34 +0800119 ret = __set_page_dirty_nobuffers(page);
120 WARN_ON(!PageLocked(page));
121 WARN_ON(!page->mapping);
Sage Weil1d3576f2009-10-06 11:31:09 -0700122
Sha Zhengju7d6e1f52013-08-21 16:27:34 +0800123 return ret;
Sage Weil1d3576f2009-10-06 11:31:09 -0700124}
125
126/*
127 * If we are truncating the full page (i.e. offset == 0), adjust the
128 * dirty page counters appropriately. Only called if there is private
129 * data on the page.
130 */
Lukas Czernerd47992f2013-05-21 23:17:23 -0400131static void ceph_invalidatepage(struct page *page, unsigned int offset,
132 unsigned int length)
Sage Weil1d3576f2009-10-06 11:31:09 -0700133{
Alexander Beregalov4ce1e9a2010-02-22 17:17:44 +0300134 struct inode *inode;
Sage Weil1d3576f2009-10-06 11:31:09 -0700135 struct ceph_inode_info *ci;
Yan, Zheng61600ef2012-05-28 14:44:30 +0800136 struct ceph_snap_context *snapc = page_snap_context(page);
Sage Weil1d3576f2009-10-06 11:31:09 -0700137
Alexander Beregalov4ce1e9a2010-02-22 17:17:44 +0300138 inode = page->mapping->host;
Milosz Tanskib150f5c12013-08-09 12:59:55 -0400139 ci = ceph_inode(inode);
140
141 if (offset != 0 || length != PAGE_CACHE_SIZE) {
142 dout("%p invalidatepage %p idx %lu partial dirty page %u~%u\n",
143 inode, page, page->index, offset, length);
144 return;
145 }
Alexander Beregalov4ce1e9a2010-02-22 17:17:44 +0300146
Sage Weil1d3576f2009-10-06 11:31:09 -0700147 /*
148 * We can get non-dirty pages here due to races between
149 * set_page_dirty and truncate_complete_page; just spit out a
150 * warning, in case we end up with accounting problems later.
151 */
152 if (!PageDirty(page))
153 pr_err("%p invalidatepage %p page not dirty\n", inode, page);
154
Milosz Tanskib150f5c12013-08-09 12:59:55 -0400155 ClearPageChecked(page);
Sage Weil1d3576f2009-10-06 11:31:09 -0700156
Milosz Tanskib150f5c12013-08-09 12:59:55 -0400157 dout("%p invalidatepage %p idx %lu full dirty page\n",
158 inode, page, page->index);
159
160 ceph_put_wrbuffer_cap_refs(ci, 1, snapc);
161 ceph_put_snap_context(snapc);
162 page->private = 0;
163 ClearPagePrivate(page);
Sage Weil1d3576f2009-10-06 11:31:09 -0700164}
165
166/* just a sanity check */
167static int ceph_releasepage(struct page *page, gfp_t g)
168{
169 struct inode *inode = page->mapping ? page->mapping->host : NULL;
170 dout("%p releasepage %p idx %lu\n", inode, page, page->index);
171 WARN_ON(PageDirty(page));
Sage Weil1d3576f2009-10-06 11:31:09 -0700172 WARN_ON(PagePrivate(page));
173 return 0;
174}
175
176/*
177 * read a single page, without unlocking it.
178 */
179static int readpage_nounlock(struct file *filp, struct page *page)
180{
Al Viro496ad9a2013-01-23 17:07:38 -0500181 struct inode *inode = file_inode(filp);
Sage Weil1d3576f2009-10-06 11:31:09 -0700182 struct ceph_inode_info *ci = ceph_inode(inode);
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700183 struct ceph_osd_client *osdc =
184 &ceph_inode_to_client(inode)->client->osdc;
Sage Weil1d3576f2009-10-06 11:31:09 -0700185 int err = 0;
186 u64 len = PAGE_CACHE_SIZE;
187
188 dout("readpage inode %p file %p page %p index %lu\n",
189 inode, filp, page, page->index);
190 err = ceph_osdc_readpages(osdc, ceph_vino(inode), &ci->i_layout,
Alex Elder6285bc22012-10-02 10:25:51 -0500191 (u64) page_offset(page), &len,
Sage Weil1d3576f2009-10-06 11:31:09 -0700192 ci->i_truncate_seq, ci->i_truncate_size,
Sage Weilb7495fc2010-11-09 12:43:12 -0800193 &page, 1, 0);
Sage Weil1d3576f2009-10-06 11:31:09 -0700194 if (err == -ENOENT)
195 err = 0;
196 if (err < 0) {
197 SetPageError(page);
198 goto out;
199 } else if (err < PAGE_CACHE_SIZE) {
200 /* zero fill remainder of page */
201 zero_user_segment(page, err, PAGE_CACHE_SIZE);
202 }
203 SetPageUptodate(page);
204
205out:
206 return err < 0 ? err : 0;
207}
208
209static int ceph_readpage(struct file *filp, struct page *page)
210{
211 int r = readpage_nounlock(filp, page);
212 unlock_page(page);
213 return r;
214}
215
216/*
Sage Weil7c272192011-08-03 09:58:09 -0700217 * Finish an async read(ahead) op.
Sage Weil1d3576f2009-10-06 11:31:09 -0700218 */
Sage Weil7c272192011-08-03 09:58:09 -0700219static void finish_read(struct ceph_osd_request *req, struct ceph_msg *msg)
Sage Weil1d3576f2009-10-06 11:31:09 -0700220{
Sage Weil7c272192011-08-03 09:58:09 -0700221 struct inode *inode = req->r_inode;
Alex Elder87060c12013-04-03 01:28:58 -0500222 struct ceph_osd_data *osd_data;
Sage Weil1b83bef2013-02-25 16:11:12 -0800223 int rc = req->r_result;
224 int bytes = le32_to_cpu(msg->hdr.data_len);
Alex Eldere0c59482013-03-07 15:38:25 -0600225 int num_pages;
Sage Weil7c272192011-08-03 09:58:09 -0700226 int i;
227
Sage Weil7c272192011-08-03 09:58:09 -0700228 dout("finish_read %p req %p rc %d bytes %d\n", inode, req, rc, bytes);
229
230 /* unlock all pages, zeroing any data we didn't read */
Alex Elder406e2c92013-04-15 14:50:36 -0500231 osd_data = osd_req_op_extent_osd_data(req, 0);
Alex Elder87060c12013-04-03 01:28:58 -0500232 BUG_ON(osd_data->type != CEPH_OSD_DATA_TYPE_PAGES);
233 num_pages = calc_pages_for((u64)osd_data->alignment,
234 (u64)osd_data->length);
Alex Eldere0c59482013-03-07 15:38:25 -0600235 for (i = 0; i < num_pages; i++) {
Alex Elder87060c12013-04-03 01:28:58 -0500236 struct page *page = osd_data->pages[i];
Sage Weil7c272192011-08-03 09:58:09 -0700237
238 if (bytes < (int)PAGE_CACHE_SIZE) {
239 /* zero (remainder of) page */
240 int s = bytes < 0 ? 0 : bytes;
241 zero_user_segment(page, s, PAGE_CACHE_SIZE);
242 }
243 dout("finish_read %p uptodate %p idx %lu\n", inode, page,
244 page->index);
245 flush_dcache_page(page);
246 SetPageUptodate(page);
247 unlock_page(page);
248 page_cache_release(page);
Alex Elder0fff87e2013-02-14 12:16:43 -0600249 bytes -= PAGE_CACHE_SIZE;
Sage Weil7c272192011-08-03 09:58:09 -0700250 }
Alex Elder87060c12013-04-03 01:28:58 -0500251 kfree(osd_data->pages);
Sage Weil7c272192011-08-03 09:58:09 -0700252}
253
David Zafman8884d532012-12-03 19:14:05 -0800254static void ceph_unlock_page_vector(struct page **pages, int num_pages)
255{
256 int i;
257
258 for (i = 0; i < num_pages; i++)
259 unlock_page(pages[i]);
260}
261
Sage Weil7c272192011-08-03 09:58:09 -0700262/*
263 * start an async read(ahead) operation. return nr_pages we submitted
264 * a read for on success, or negative error code.
265 */
Sage Weil0d66a482011-08-04 08:21:30 -0700266static int start_read(struct inode *inode, struct list_head *page_list, int max)
Sage Weil7c272192011-08-03 09:58:09 -0700267{
268 struct ceph_osd_client *osdc =
269 &ceph_inode_to_client(inode)->client->osdc;
270 struct ceph_inode_info *ci = ceph_inode(inode);
271 struct page *page = list_entry(page_list->prev, struct page, lru);
Alex Elderacead002013-03-14 14:09:05 -0500272 struct ceph_vino vino;
Sage Weil7c272192011-08-03 09:58:09 -0700273 struct ceph_osd_request *req;
274 u64 off;
275 u64 len;
276 int i;
Sage Weil1d3576f2009-10-06 11:31:09 -0700277 struct page **pages;
Sage Weil7c272192011-08-03 09:58:09 -0700278 pgoff_t next_index;
279 int nr_pages = 0;
280 int ret;
281
Alex Elder6285bc22012-10-02 10:25:51 -0500282 off = (u64) page_offset(page);
Sage Weil7c272192011-08-03 09:58:09 -0700283
284 /* count pages */
285 next_index = page->index;
286 list_for_each_entry_reverse(page, page_list, lru) {
287 if (page->index != next_index)
288 break;
289 nr_pages++;
290 next_index++;
Sage Weil0d66a482011-08-04 08:21:30 -0700291 if (max && nr_pages == max)
292 break;
Sage Weil7c272192011-08-03 09:58:09 -0700293 }
294 len = nr_pages << PAGE_CACHE_SHIFT;
295 dout("start_read %p nr_pages %d is %lld~%lld\n", inode, nr_pages,
296 off, len);
Alex Elderacead002013-03-14 14:09:05 -0500297 vino = ceph_vino(inode);
298 req = ceph_osdc_new_request(osdc, &ci->i_layout, vino, off, &len,
Alex Elder79528732013-04-03 21:32:51 -0500299 1, CEPH_OSD_OP_READ,
Alex Elderacead002013-03-14 14:09:05 -0500300 CEPH_OSD_FLAG_READ, NULL,
Sage Weil7c272192011-08-03 09:58:09 -0700301 ci->i_truncate_seq, ci->i_truncate_size,
Alex Elderacead002013-03-14 14:09:05 -0500302 false);
Sage Weil68162822012-09-24 21:01:02 -0700303 if (IS_ERR(req))
304 return PTR_ERR(req);
Sage Weil1d3576f2009-10-06 11:31:09 -0700305
306 /* build page vector */
Alex Eldercf7b7e12013-03-01 18:00:15 -0600307 nr_pages = calc_pages_for(0, len);
Sage Weil7c272192011-08-03 09:58:09 -0700308 pages = kmalloc(sizeof(*pages) * nr_pages, GFP_NOFS);
309 ret = -ENOMEM;
Sage Weil1d3576f2009-10-06 11:31:09 -0700310 if (!pages)
Sage Weil7c272192011-08-03 09:58:09 -0700311 goto out;
312 for (i = 0; i < nr_pages; ++i) {
313 page = list_entry(page_list->prev, struct page, lru);
314 BUG_ON(PageLocked(page));
315 list_del(&page->lru);
316
317 dout("start_read %p adding %p idx %lu\n", inode, page,
318 page->index);
319 if (add_to_page_cache_lru(page, &inode->i_data, page->index,
320 GFP_NOFS)) {
321 page_cache_release(page);
322 dout("start_read %p add_to_page_cache failed %p\n",
323 inode, page);
324 nr_pages = i;
325 goto out_pages;
Sage Weil1d3576f2009-10-06 11:31:09 -0700326 }
Sage Weil7c272192011-08-03 09:58:09 -0700327 pages[i] = page;
Sage Weil1d3576f2009-10-06 11:31:09 -0700328 }
Alex Elder406e2c92013-04-15 14:50:36 -0500329 osd_req_op_extent_osd_data_pages(req, 0, pages, len, 0, false, false);
Sage Weil7c272192011-08-03 09:58:09 -0700330 req->r_callback = finish_read;
331 req->r_inode = inode;
332
Alex Elder79528732013-04-03 21:32:51 -0500333 ceph_osdc_build_request(req, off, NULL, vino.snap, NULL);
Alex Elder02ee07d2013-03-14 14:09:06 -0500334
Sage Weil7c272192011-08-03 09:58:09 -0700335 dout("start_read %p starting %p %lld~%lld\n", inode, req, off, len);
336 ret = ceph_osdc_start_request(osdc, req, false);
337 if (ret < 0)
338 goto out_pages;
339 ceph_osdc_put_request(req);
340 return nr_pages;
341
342out_pages:
David Zafman8884d532012-12-03 19:14:05 -0800343 ceph_unlock_page_vector(pages, nr_pages);
Sage Weil7c272192011-08-03 09:58:09 -0700344 ceph_release_page_vector(pages, nr_pages);
Sage Weil7c272192011-08-03 09:58:09 -0700345out:
346 ceph_osdc_put_request(req);
347 return ret;
Sage Weil1d3576f2009-10-06 11:31:09 -0700348}
349
Sage Weil7c272192011-08-03 09:58:09 -0700350
Sage Weil1d3576f2009-10-06 11:31:09 -0700351/*
352 * Read multiple pages. Leave pages we don't read + unlock in page_list;
353 * the caller (VM) cleans them up.
354 */
355static int ceph_readpages(struct file *file, struct address_space *mapping,
356 struct list_head *page_list, unsigned nr_pages)
357{
Al Viro496ad9a2013-01-23 17:07:38 -0500358 struct inode *inode = file_inode(file);
Sage Weil0d66a482011-08-04 08:21:30 -0700359 struct ceph_fs_client *fsc = ceph_inode_to_client(inode);
Sage Weil1d3576f2009-10-06 11:31:09 -0700360 int rc = 0;
Sage Weil0d66a482011-08-04 08:21:30 -0700361 int max = 0;
Sage Weil1d3576f2009-10-06 11:31:09 -0700362
Sage Weil0d66a482011-08-04 08:21:30 -0700363 if (fsc->mount_options->rsize >= PAGE_CACHE_SIZE)
364 max = (fsc->mount_options->rsize + PAGE_CACHE_SIZE - 1)
365 >> PAGE_SHIFT;
366
Alex Elder2794a822013-02-14 12:16:43 -0600367 dout("readpages %p file %p nr_pages %d max %d\n", inode,
368 file, nr_pages,
Sage Weil0d66a482011-08-04 08:21:30 -0700369 max);
Sage Weil7c272192011-08-03 09:58:09 -0700370 while (!list_empty(page_list)) {
Sage Weil0d66a482011-08-04 08:21:30 -0700371 rc = start_read(inode, page_list, max);
Sage Weil7c272192011-08-03 09:58:09 -0700372 if (rc < 0)
373 goto out;
374 BUG_ON(rc == 0);
Sage Weil1d3576f2009-10-06 11:31:09 -0700375 }
Sage Weil1d3576f2009-10-06 11:31:09 -0700376out:
Sage Weil7c272192011-08-03 09:58:09 -0700377 dout("readpages %p file %p ret %d\n", inode, file, rc);
Sage Weil1d3576f2009-10-06 11:31:09 -0700378 return rc;
379}
380
381/*
382 * Get ref for the oldest snapc for an inode with dirty data... that is, the
383 * only snap context we are allowed to write back.
Sage Weil1d3576f2009-10-06 11:31:09 -0700384 */
Sage Weil6298a332010-03-31 22:01:38 -0700385static struct ceph_snap_context *get_oldest_context(struct inode *inode,
386 u64 *snap_size)
Sage Weil1d3576f2009-10-06 11:31:09 -0700387{
388 struct ceph_inode_info *ci = ceph_inode(inode);
389 struct ceph_snap_context *snapc = NULL;
390 struct ceph_cap_snap *capsnap = NULL;
391
Sage Weilbe655592011-11-30 09:47:09 -0800392 spin_lock(&ci->i_ceph_lock);
Sage Weil1d3576f2009-10-06 11:31:09 -0700393 list_for_each_entry(capsnap, &ci->i_cap_snaps, ci_item) {
394 dout(" cap_snap %p snapc %p has %d dirty pages\n", capsnap,
395 capsnap->context, capsnap->dirty_pages);
396 if (capsnap->dirty_pages) {
397 snapc = ceph_get_snap_context(capsnap->context);
398 if (snap_size)
399 *snap_size = capsnap->size;
400 break;
401 }
402 }
Sage Weil7d8cb262010-08-24 08:44:16 -0700403 if (!snapc && ci->i_wrbuffer_ref_head) {
Sage Weil80e755f2010-03-31 21:52:10 -0700404 snapc = ceph_get_snap_context(ci->i_head_snapc);
Sage Weil1d3576f2009-10-06 11:31:09 -0700405 dout(" head snapc %p has %d dirty pages\n",
406 snapc, ci->i_wrbuffer_ref_head);
407 }
Sage Weilbe655592011-11-30 09:47:09 -0800408 spin_unlock(&ci->i_ceph_lock);
Sage Weil1d3576f2009-10-06 11:31:09 -0700409 return snapc;
410}
411
412/*
413 * Write a single page, but leave the page locked.
414 *
415 * If we get a write error, set the page error bit, but still adjust the
416 * dirty page accounting (i.e., page is no longer dirty).
417 */
418static int writepage_nounlock(struct page *page, struct writeback_control *wbc)
419{
420 struct inode *inode;
421 struct ceph_inode_info *ci;
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700422 struct ceph_fs_client *fsc;
Sage Weil1d3576f2009-10-06 11:31:09 -0700423 struct ceph_osd_client *osdc;
Sage Weil6298a332010-03-31 22:01:38 -0700424 struct ceph_snap_context *snapc, *oldest;
Yan, Zhengfc2744a2013-05-31 16:48:29 +0800425 loff_t page_off = page_offset(page);
Yehuda Sadeh2baba252009-12-18 13:51:57 -0800426 long writeback_stat;
Yan, Zhengfc2744a2013-05-31 16:48:29 +0800427 u64 truncate_size, snap_size = 0;
428 u32 truncate_seq;
429 int err = 0, len = PAGE_CACHE_SIZE;
Sage Weil1d3576f2009-10-06 11:31:09 -0700430
431 dout("writepage %p idx %lu\n", page, page->index);
432
433 if (!page->mapping || !page->mapping->host) {
434 dout("writepage %p - no mapping\n", page);
435 return -EFAULT;
436 }
437 inode = page->mapping->host;
438 ci = ceph_inode(inode);
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700439 fsc = ceph_inode_to_client(inode);
440 osdc = &fsc->client->osdc;
Sage Weil1d3576f2009-10-06 11:31:09 -0700441
442 /* verify this is a writeable snap context */
Yan, Zheng61600ef2012-05-28 14:44:30 +0800443 snapc = page_snap_context(page);
Sage Weil1d3576f2009-10-06 11:31:09 -0700444 if (snapc == NULL) {
445 dout("writepage %p page %p not dirty?\n", inode, page);
446 goto out;
447 }
Sage Weil6298a332010-03-31 22:01:38 -0700448 oldest = get_oldest_context(inode, &snap_size);
449 if (snapc->seq > oldest->seq) {
Sage Weil1d3576f2009-10-06 11:31:09 -0700450 dout("writepage %p page %p snapc %p not writeable - noop\n",
Yan, Zheng61600ef2012-05-28 14:44:30 +0800451 inode, page, snapc);
Sage Weil1d3576f2009-10-06 11:31:09 -0700452 /* we should only noop if called by kswapd */
453 WARN_ON((current->flags & PF_MEMALLOC) == 0);
Sage Weil6298a332010-03-31 22:01:38 -0700454 ceph_put_snap_context(oldest);
Sage Weil1d3576f2009-10-06 11:31:09 -0700455 goto out;
456 }
Sage Weil6298a332010-03-31 22:01:38 -0700457 ceph_put_snap_context(oldest);
Sage Weil1d3576f2009-10-06 11:31:09 -0700458
Yan, Zhengfc2744a2013-05-31 16:48:29 +0800459 spin_lock(&ci->i_ceph_lock);
460 truncate_seq = ci->i_truncate_seq;
461 truncate_size = ci->i_truncate_size;
462 if (!snap_size)
463 snap_size = i_size_read(inode);
464 spin_unlock(&ci->i_ceph_lock);
465
Sage Weil1d3576f2009-10-06 11:31:09 -0700466 /* is this a partial page at end of file? */
Yan, Zhengfc2744a2013-05-31 16:48:29 +0800467 if (page_off >= snap_size) {
468 dout("%p page eof %llu\n", page, snap_size);
469 goto out;
470 }
471 if (snap_size < page_off + len)
472 len = snap_size - page_off;
Sage Weil1d3576f2009-10-06 11:31:09 -0700473
Sage Weilae00d4f2010-09-16 16:26:51 -0700474 dout("writepage %p page %p index %lu on %llu~%u snapc %p\n",
475 inode, page, page->index, page_off, len, snapc);
Sage Weil1d3576f2009-10-06 11:31:09 -0700476
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700477 writeback_stat = atomic_long_inc_return(&fsc->writeback_count);
Yehuda Sadeh2baba252009-12-18 13:51:57 -0800478 if (writeback_stat >
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700479 CONGESTION_ON_THRESH(fsc->mount_options->congestion_kb))
480 set_bdi_congested(&fsc->backing_dev_info, BLK_RW_ASYNC);
Yehuda Sadeh2baba252009-12-18 13:51:57 -0800481
Sage Weil1d3576f2009-10-06 11:31:09 -0700482 set_page_writeback(page);
483 err = ceph_osdc_writepages(osdc, ceph_vino(inode),
484 &ci->i_layout, snapc,
485 page_off, len,
Yan, Zhengfc2744a2013-05-31 16:48:29 +0800486 truncate_seq, truncate_size,
Alex Elder24808822013-02-15 11:42:29 -0600487 &inode->i_mtime, &page, 1);
Sage Weil1d3576f2009-10-06 11:31:09 -0700488 if (err < 0) {
489 dout("writepage setting page/mapping error %d %p\n", err, page);
490 SetPageError(page);
491 mapping_set_error(&inode->i_data, err);
492 if (wbc)
493 wbc->pages_skipped++;
494 } else {
495 dout("writepage cleaned page %p\n", page);
496 err = 0; /* vfs expects us to return 0 */
497 }
498 page->private = 0;
499 ClearPagePrivate(page);
500 end_page_writeback(page);
501 ceph_put_wrbuffer_cap_refs(ci, 1, snapc);
Sage Weil6298a332010-03-31 22:01:38 -0700502 ceph_put_snap_context(snapc); /* page's reference */
Sage Weil1d3576f2009-10-06 11:31:09 -0700503out:
504 return err;
505}
506
507static int ceph_writepage(struct page *page, struct writeback_control *wbc)
508{
Yehuda Sadehdbd646a2009-12-16 14:51:06 -0800509 int err;
510 struct inode *inode = page->mapping->host;
511 BUG_ON(!inode);
Sage Weil70b666c2011-05-27 09:24:26 -0700512 ihold(inode);
Yehuda Sadehdbd646a2009-12-16 14:51:06 -0800513 err = writepage_nounlock(page, wbc);
Sage Weil1d3576f2009-10-06 11:31:09 -0700514 unlock_page(page);
Yehuda Sadehdbd646a2009-12-16 14:51:06 -0800515 iput(inode);
Sage Weil1d3576f2009-10-06 11:31:09 -0700516 return err;
517}
518
519
520/*
521 * lame release_pages helper. release_pages() isn't exported to
522 * modules.
523 */
524static void ceph_release_pages(struct page **pages, int num)
525{
526 struct pagevec pvec;
527 int i;
528
529 pagevec_init(&pvec, 0);
530 for (i = 0; i < num; i++) {
531 if (pagevec_add(&pvec, pages[i]) == 0)
532 pagevec_release(&pvec);
533 }
534 pagevec_release(&pvec);
535}
536
537
538/*
539 * async writeback completion handler.
540 *
541 * If we get an error, set the mapping error bit, but not the individual
542 * page error bits.
543 */
544static void writepages_finish(struct ceph_osd_request *req,
545 struct ceph_msg *msg)
546{
547 struct inode *inode = req->r_inode;
Sage Weil1d3576f2009-10-06 11:31:09 -0700548 struct ceph_inode_info *ci = ceph_inode(inode);
Alex Elder87060c12013-04-03 01:28:58 -0500549 struct ceph_osd_data *osd_data;
Sage Weil1d3576f2009-10-06 11:31:09 -0700550 unsigned wrote;
Sage Weil1d3576f2009-10-06 11:31:09 -0700551 struct page *page;
Alex Eldere0c59482013-03-07 15:38:25 -0600552 int num_pages;
Sage Weil1d3576f2009-10-06 11:31:09 -0700553 int i;
554 struct ceph_snap_context *snapc = req->r_snapc;
555 struct address_space *mapping = inode->i_mapping;
Sage Weil1b83bef2013-02-25 16:11:12 -0800556 int rc = req->r_result;
Alex Elder79528732013-04-03 21:32:51 -0500557 u64 bytes = req->r_ops[0].extent.length;
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700558 struct ceph_fs_client *fsc = ceph_inode_to_client(inode);
Yehuda Sadeh2baba252009-12-18 13:51:57 -0800559 long writeback_stat;
Sage Weil7ff899d2010-04-23 10:25:33 -0700560 unsigned issued = ceph_caps_issued(ci);
Sage Weil1d3576f2009-10-06 11:31:09 -0700561
Alex Elder406e2c92013-04-15 14:50:36 -0500562 osd_data = osd_req_op_extent_osd_data(req, 0);
Alex Elder87060c12013-04-03 01:28:58 -0500563 BUG_ON(osd_data->type != CEPH_OSD_DATA_TYPE_PAGES);
564 num_pages = calc_pages_for((u64)osd_data->alignment,
565 (u64)osd_data->length);
Sage Weil1d3576f2009-10-06 11:31:09 -0700566 if (rc >= 0) {
Sage Weil79788c62010-02-02 16:34:04 -0800567 /*
568 * Assume we wrote the pages we originally sent. The
569 * osd might reply with fewer pages if our writeback
570 * raced with a truncation and was adjusted at the osd,
571 * so don't believe the reply.
572 */
Alex Eldere0c59482013-03-07 15:38:25 -0600573 wrote = num_pages;
Sage Weil1d3576f2009-10-06 11:31:09 -0700574 } else {
575 wrote = 0;
576 mapping_set_error(mapping, rc);
577 }
578 dout("writepages_finish %p rc %d bytes %llu wrote %d (pages)\n",
579 inode, rc, bytes, wrote);
580
581 /* clean all pages */
Alex Eldere0c59482013-03-07 15:38:25 -0600582 for (i = 0; i < num_pages; i++) {
Alex Elder87060c12013-04-03 01:28:58 -0500583 page = osd_data->pages[i];
Sage Weil1d3576f2009-10-06 11:31:09 -0700584 BUG_ON(!page);
585 WARN_ON(!PageUptodate(page));
586
Yehuda Sadeh2baba252009-12-18 13:51:57 -0800587 writeback_stat =
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700588 atomic_long_dec_return(&fsc->writeback_count);
Yehuda Sadeh2baba252009-12-18 13:51:57 -0800589 if (writeback_stat <
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700590 CONGESTION_OFF_THRESH(fsc->mount_options->congestion_kb))
591 clear_bdi_congested(&fsc->backing_dev_info,
Yehuda Sadeh2baba252009-12-18 13:51:57 -0800592 BLK_RW_ASYNC);
593
Yan, Zheng61600ef2012-05-28 14:44:30 +0800594 ceph_put_snap_context(page_snap_context(page));
Sage Weil1d3576f2009-10-06 11:31:09 -0700595 page->private = 0;
596 ClearPagePrivate(page);
Sage Weil1d3576f2009-10-06 11:31:09 -0700597 dout("unlocking %d %p\n", i, page);
598 end_page_writeback(page);
Yehuda Sadehe63dc5c2010-02-19 00:07:01 +0000599
600 /*
601 * We lost the cache cap, need to truncate the page before
602 * it is unlocked, otherwise we'd truncate it later in the
603 * page truncation thread, possibly losing some data that
604 * raced its way in
605 */
Sage Weil29625072010-05-27 10:40:43 -0700606 if ((issued & (CEPH_CAP_FILE_CACHE|CEPH_CAP_FILE_LAZYIO)) == 0)
Yehuda Sadehe63dc5c2010-02-19 00:07:01 +0000607 generic_error_remove_page(inode->i_mapping, page);
608
Sage Weil1d3576f2009-10-06 11:31:09 -0700609 unlock_page(page);
610 }
611 dout("%p wrote+cleaned %d pages\n", inode, wrote);
Alex Eldere0c59482013-03-07 15:38:25 -0600612 ceph_put_wrbuffer_cap_refs(ci, num_pages, snapc);
Sage Weil1d3576f2009-10-06 11:31:09 -0700613
Alex Elder87060c12013-04-03 01:28:58 -0500614 ceph_release_pages(osd_data->pages, num_pages);
615 if (osd_data->pages_from_pool)
616 mempool_free(osd_data->pages,
Cheng Renquan640ef792010-03-26 17:40:33 +0800617 ceph_sb_to_client(inode->i_sb)->wb_pagevec_pool);
Sage Weil1d3576f2009-10-06 11:31:09 -0700618 else
Alex Elder87060c12013-04-03 01:28:58 -0500619 kfree(osd_data->pages);
Sage Weil1d3576f2009-10-06 11:31:09 -0700620 ceph_osdc_put_request(req);
621}
622
Sage Weil1d3576f2009-10-06 11:31:09 -0700623/*
624 * initiate async writeback
625 */
626static int ceph_writepages_start(struct address_space *mapping,
627 struct writeback_control *wbc)
628{
629 struct inode *inode = mapping->host;
Sage Weil1d3576f2009-10-06 11:31:09 -0700630 struct ceph_inode_info *ci = ceph_inode(inode);
Yan, Zhengfc2744a2013-05-31 16:48:29 +0800631 struct ceph_fs_client *fsc = ceph_inode_to_client(inode);
632 struct ceph_vino vino = ceph_vino(inode);
Sage Weil1d3576f2009-10-06 11:31:09 -0700633 pgoff_t index, start, end;
634 int range_whole = 0;
635 int should_loop = 1;
636 pgoff_t max_pages = 0, max_pages_ever = 0;
Sage Weil80e755f2010-03-31 21:52:10 -0700637 struct ceph_snap_context *snapc = NULL, *last_snapc = NULL, *pgsnapc;
Sage Weil1d3576f2009-10-06 11:31:09 -0700638 struct pagevec pvec;
639 int done = 0;
640 int rc = 0;
641 unsigned wsize = 1 << inode->i_blkbits;
642 struct ceph_osd_request *req = NULL;
643 int do_sync;
Yan, Zhengfc2744a2013-05-31 16:48:29 +0800644 u64 truncate_size, snap_size;
645 u32 truncate_seq;
Sage Weil1d3576f2009-10-06 11:31:09 -0700646
647 /*
648 * Include a 'sync' in the OSD request if this is a data
649 * integrity write (e.g., O_SYNC write or fsync()), or if our
650 * cap is being revoked.
651 */
majianpengc62988e2013-06-19 15:12:06 +0800652 if ((wbc->sync_mode == WB_SYNC_ALL) ||
653 ceph_caps_revoking(ci, CEPH_CAP_FILE_BUFFER))
Sage Weil1d3576f2009-10-06 11:31:09 -0700654 do_sync = 1;
655 dout("writepages_start %p dosync=%d (mode=%s)\n",
656 inode, do_sync,
657 wbc->sync_mode == WB_SYNC_NONE ? "NONE" :
658 (wbc->sync_mode == WB_SYNC_ALL ? "ALL" : "HOLD"));
659
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700660 if (fsc->mount_state == CEPH_MOUNT_SHUTDOWN) {
Sage Weil1d3576f2009-10-06 11:31:09 -0700661 pr_warning("writepage_start %p on forced umount\n", inode);
662 return -EIO; /* we're in a forced umount, don't write! */
663 }
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700664 if (fsc->mount_options->wsize && fsc->mount_options->wsize < wsize)
665 wsize = fsc->mount_options->wsize;
Sage Weil1d3576f2009-10-06 11:31:09 -0700666 if (wsize < PAGE_CACHE_SIZE)
667 wsize = PAGE_CACHE_SIZE;
668 max_pages_ever = wsize >> PAGE_CACHE_SHIFT;
669
670 pagevec_init(&pvec, 0);
671
Sage Weil1d3576f2009-10-06 11:31:09 -0700672 /* where to start/end? */
673 if (wbc->range_cyclic) {
674 start = mapping->writeback_index; /* Start from prev offset */
675 end = -1;
676 dout(" cyclic, start at %lu\n", start);
677 } else {
678 start = wbc->range_start >> PAGE_CACHE_SHIFT;
679 end = wbc->range_end >> PAGE_CACHE_SHIFT;
680 if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX)
681 range_whole = 1;
682 should_loop = 0;
683 dout(" not cyclic, %lu to %lu\n", start, end);
684 }
685 index = start;
686
687retry:
688 /* find oldest snap context with dirty data */
689 ceph_put_snap_context(snapc);
Yan, Zheng1ac0fc82013-04-12 21:45:42 +0800690 snap_size = 0;
Sage Weil1d3576f2009-10-06 11:31:09 -0700691 snapc = get_oldest_context(inode, &snap_size);
692 if (!snapc) {
693 /* hmm, why does writepages get called when there
694 is no dirty data? */
695 dout(" no snap context with dirty data?\n");
696 goto out;
697 }
Yan, Zheng1ac0fc82013-04-12 21:45:42 +0800698 if (snap_size == 0)
699 snap_size = i_size_read(inode);
Sage Weil1d3576f2009-10-06 11:31:09 -0700700 dout(" oldest snapc is %p seq %lld (%d snaps)\n",
701 snapc, snapc->seq, snapc->num_snaps);
Yan, Zhengfc2744a2013-05-31 16:48:29 +0800702
703 spin_lock(&ci->i_ceph_lock);
704 truncate_seq = ci->i_truncate_seq;
705 truncate_size = ci->i_truncate_size;
706 if (!snap_size)
707 snap_size = i_size_read(inode);
708 spin_unlock(&ci->i_ceph_lock);
709
Sage Weil1d3576f2009-10-06 11:31:09 -0700710 if (last_snapc && snapc != last_snapc) {
711 /* if we switched to a newer snapc, restart our scan at the
712 * start of the original file range. */
713 dout(" snapc differs from last pass, restarting at %lu\n",
714 index);
715 index = start;
716 }
717 last_snapc = snapc;
718
719 while (!done && index <= end) {
Alex Eldere5975c72013-03-14 14:09:05 -0500720 int num_ops = do_sync ? 2 : 1;
Sage Weil1d3576f2009-10-06 11:31:09 -0700721 unsigned i;
722 int first;
723 pgoff_t next;
724 int pvec_pages, locked_pages;
Alex Eldere5975c72013-03-14 14:09:05 -0500725 struct page **pages = NULL;
726 mempool_t *pool = NULL; /* Becomes non-null if mempool used */
Sage Weil1d3576f2009-10-06 11:31:09 -0700727 struct page *page;
728 int want;
729 u64 offset, len;
Yehuda Sadeh2baba252009-12-18 13:51:57 -0800730 long writeback_stat;
Sage Weil1d3576f2009-10-06 11:31:09 -0700731
732 next = 0;
733 locked_pages = 0;
734 max_pages = max_pages_ever;
735
736get_more_pages:
737 first = -1;
738 want = min(end - index,
739 min((pgoff_t)PAGEVEC_SIZE,
740 max_pages - (pgoff_t)locked_pages) - 1)
741 + 1;
742 pvec_pages = pagevec_lookup_tag(&pvec, mapping, &index,
743 PAGECACHE_TAG_DIRTY,
744 want);
745 dout("pagevec_lookup_tag got %d\n", pvec_pages);
746 if (!pvec_pages && !locked_pages)
747 break;
748 for (i = 0; i < pvec_pages && locked_pages < max_pages; i++) {
749 page = pvec.pages[i];
750 dout("? %p idx %lu\n", page, page->index);
751 if (locked_pages == 0)
752 lock_page(page); /* first page */
753 else if (!trylock_page(page))
754 break;
755
756 /* only dirty pages, or our accounting breaks */
757 if (unlikely(!PageDirty(page)) ||
758 unlikely(page->mapping != mapping)) {
759 dout("!dirty or !mapping %p\n", page);
760 unlock_page(page);
761 break;
762 }
763 if (!wbc->range_cyclic && page->index > end) {
764 dout("end of range %p\n", page);
765 done = 1;
766 unlock_page(page);
767 break;
768 }
769 if (next && (page->index != next)) {
770 dout("not consecutive %p\n", page);
771 unlock_page(page);
772 break;
773 }
774 if (wbc->sync_mode != WB_SYNC_NONE) {
775 dout("waiting on writeback %p\n", page);
776 wait_on_page_writeback(page);
777 }
Yan, Zheng1ac0fc82013-04-12 21:45:42 +0800778 if (page_offset(page) >= snap_size) {
779 dout("%p page eof %llu\n", page, snap_size);
Sage Weil1d3576f2009-10-06 11:31:09 -0700780 done = 1;
781 unlock_page(page);
782 break;
783 }
784 if (PageWriteback(page)) {
785 dout("%p under writeback\n", page);
786 unlock_page(page);
787 break;
788 }
789
790 /* only if matching snap context */
Yan, Zheng61600ef2012-05-28 14:44:30 +0800791 pgsnapc = page_snap_context(page);
Sage Weil80e755f2010-03-31 21:52:10 -0700792 if (pgsnapc->seq > snapc->seq) {
793 dout("page snapc %p %lld > oldest %p %lld\n",
794 pgsnapc, pgsnapc->seq, snapc, snapc->seq);
Sage Weil1d3576f2009-10-06 11:31:09 -0700795 unlock_page(page);
796 if (!locked_pages)
797 continue; /* keep looking for snap */
798 break;
799 }
800
801 if (!clear_page_dirty_for_io(page)) {
802 dout("%p !clear_page_dirty_for_io\n", page);
803 unlock_page(page);
804 break;
805 }
806
Alex Eldere5975c72013-03-14 14:09:05 -0500807 /*
808 * We have something to write. If this is
809 * the first locked page this time through,
810 * allocate an osd request and a page array
811 * that it will use.
812 */
Sage Weil1d3576f2009-10-06 11:31:09 -0700813 if (locked_pages == 0) {
Alex Eldere5975c72013-03-14 14:09:05 -0500814 BUG_ON(pages);
Sage Weil1d3576f2009-10-06 11:31:09 -0700815 /* prepare async write request */
Alex Eldere5975c72013-03-14 14:09:05 -0500816 offset = (u64)page_offset(page);
Sage Weil1d3576f2009-10-06 11:31:09 -0700817 len = wsize;
Yan, Zhengfc2744a2013-05-31 16:48:29 +0800818 req = ceph_osdc_new_request(&fsc->client->osdc,
819 &ci->i_layout, vino,
820 offset, &len, num_ops,
821 CEPH_OSD_OP_WRITE,
822 CEPH_OSD_FLAG_WRITE |
823 CEPH_OSD_FLAG_ONDISK,
824 snapc, truncate_seq,
825 truncate_size, true);
Sage Weil68162822012-09-24 21:01:02 -0700826 if (IS_ERR(req)) {
827 rc = PTR_ERR(req);
Henry C Chang8c718972011-05-03 09:45:16 +0000828 unlock_page(page);
829 break;
830 }
831
Alex Elder88486952013-03-14 14:09:05 -0500832 req->r_callback = writepages_finish;
833 req->r_inode = inode;
834
835 max_pages = calc_pages_for(0, (u64)len);
Yan, Zhengfc2744a2013-05-31 16:48:29 +0800836 pages = kmalloc(max_pages * sizeof (*pages),
837 GFP_NOFS);
Alex Elder88486952013-03-14 14:09:05 -0500838 if (!pages) {
839 pool = fsc->wb_pagevec_pool;
Alex Elder88486952013-03-14 14:09:05 -0500840 pages = mempool_alloc(pool, GFP_NOFS);
Alex Eldere5975c72013-03-14 14:09:05 -0500841 BUG_ON(!pages);
Alex Elder88486952013-03-14 14:09:05 -0500842 }
Sage Weil1d3576f2009-10-06 11:31:09 -0700843 }
844
845 /* note position of first page in pvec */
846 if (first < 0)
847 first = i;
848 dout("%p will write page %p idx %lu\n",
849 inode, page, page->index);
Yehuda Sadeh2baba252009-12-18 13:51:57 -0800850
Sage Weil213c99e2010-08-03 10:25:11 -0700851 writeback_stat =
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700852 atomic_long_inc_return(&fsc->writeback_count);
Sage Weil213c99e2010-08-03 10:25:11 -0700853 if (writeback_stat > CONGESTION_ON_THRESH(
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700854 fsc->mount_options->congestion_kb)) {
855 set_bdi_congested(&fsc->backing_dev_info,
Sage Weil213c99e2010-08-03 10:25:11 -0700856 BLK_RW_ASYNC);
Yehuda Sadeh2baba252009-12-18 13:51:57 -0800857 }
858
Sage Weil1d3576f2009-10-06 11:31:09 -0700859 set_page_writeback(page);
Alex Eldere5975c72013-03-14 14:09:05 -0500860 pages[locked_pages] = page;
Sage Weil1d3576f2009-10-06 11:31:09 -0700861 locked_pages++;
862 next = page->index + 1;
863 }
864
865 /* did we get anything? */
866 if (!locked_pages)
867 goto release_pvec_pages;
868 if (i) {
869 int j;
870 BUG_ON(!locked_pages || first < 0);
871
872 if (pvec_pages && i == pvec_pages &&
873 locked_pages < max_pages) {
874 dout("reached end pvec, trying for more\n");
875 pagevec_reinit(&pvec);
876 goto get_more_pages;
877 }
878
879 /* shift unused pages over in the pvec... we
880 * will need to release them below. */
881 for (j = i; j < pvec_pages; j++) {
882 dout(" pvec leftover page %p\n",
883 pvec.pages[j]);
884 pvec.pages[j-i+first] = pvec.pages[j];
885 }
886 pvec.nr -= i-first;
887 }
888
Alex Eldere5975c72013-03-14 14:09:05 -0500889 /* Format the osd request message and submit the write */
890
891 offset = page_offset(pages[0]);
Yan, Zheng1ac0fc82013-04-12 21:45:42 +0800892 len = min(snap_size - offset,
Sage Weil1d3576f2009-10-06 11:31:09 -0700893 (u64)locked_pages << PAGE_CACHE_SHIFT);
894 dout("writepages got %d pages at %llu~%llu\n",
895 locked_pages, offset, len);
896
Alex Elder406e2c92013-04-15 14:50:36 -0500897 osd_req_op_extent_osd_data_pages(req, 0, pages, len, 0,
Alex Eldera4ce40a2013-04-05 01:27:12 -0500898 !!pool, false);
Alex Eldere5975c72013-03-14 14:09:05 -0500899
900 pages = NULL; /* request message now owns the pages array */
901 pool = NULL;
902
903 /* Update the write op length in case we changed it */
904
Alex Elderc99d2d42013-04-05 01:27:11 -0500905 osd_req_op_extent_update(req, 0, len);
Alex Eldere5975c72013-03-14 14:09:05 -0500906
907 vino = ceph_vino(inode);
Alex Elder79528732013-04-03 21:32:51 -0500908 ceph_osdc_build_request(req, offset, snapc, vino.snap,
909 &inode->i_mtime);
Sage Weil1d3576f2009-10-06 11:31:09 -0700910
Sage Weil9d6fcb02011-05-12 15:48:16 -0700911 rc = ceph_osdc_start_request(&fsc->client->osdc, req, true);
912 BUG_ON(rc);
Sage Weil1d3576f2009-10-06 11:31:09 -0700913 req = NULL;
914
915 /* continue? */
916 index = next;
917 wbc->nr_to_write -= locked_pages;
918 if (wbc->nr_to_write <= 0)
919 done = 1;
920
921release_pvec_pages:
922 dout("pagevec_release on %d pages (%p)\n", (int)pvec.nr,
923 pvec.nr ? pvec.pages[0] : NULL);
924 pagevec_release(&pvec);
925
926 if (locked_pages && !done)
927 goto retry;
928 }
929
930 if (should_loop && !done) {
931 /* more to do; loop back to beginning of file */
932 dout("writepages looping back to beginning of file\n");
933 should_loop = 0;
934 index = 0;
935 goto retry;
936 }
937
938 if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0))
939 mapping->writeback_index = index;
940
941out:
942 if (req)
943 ceph_osdc_put_request(req);
Sage Weil1d3576f2009-10-06 11:31:09 -0700944 ceph_put_snap_context(snapc);
945 dout("writepages done, rc = %d\n", rc);
Sage Weil1d3576f2009-10-06 11:31:09 -0700946 return rc;
947}
948
949
950
951/*
952 * See if a given @snapc is either writeable, or already written.
953 */
954static int context_is_writeable_or_written(struct inode *inode,
955 struct ceph_snap_context *snapc)
956{
957 struct ceph_snap_context *oldest = get_oldest_context(inode, NULL);
Sage Weil6298a332010-03-31 22:01:38 -0700958 int ret = !oldest || snapc->seq <= oldest->seq;
959
960 ceph_put_snap_context(oldest);
961 return ret;
Sage Weil1d3576f2009-10-06 11:31:09 -0700962}
963
964/*
965 * We are only allowed to write into/dirty the page if the page is
966 * clean, or already dirty within the same snap context.
Sage Weil8f883c22010-03-19 13:27:53 -0700967 *
968 * called with page locked.
969 * return success with page locked,
970 * or any failure (incl -EAGAIN) with page unlocked.
Sage Weil1d3576f2009-10-06 11:31:09 -0700971 */
Yehuda Sadeh4af6b222010-02-09 11:02:51 -0800972static int ceph_update_writeable_page(struct file *file,
973 loff_t pos, unsigned len,
974 struct page *page)
Sage Weil1d3576f2009-10-06 11:31:09 -0700975{
Al Viro496ad9a2013-01-23 17:07:38 -0500976 struct inode *inode = file_inode(file);
Sage Weil1d3576f2009-10-06 11:31:09 -0700977 struct ceph_inode_info *ci = ceph_inode(inode);
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700978 struct ceph_mds_client *mdsc = ceph_inode_to_client(inode)->mdsc;
Sage Weil1d3576f2009-10-06 11:31:09 -0700979 loff_t page_off = pos & PAGE_CACHE_MASK;
980 int pos_in_page = pos & ~PAGE_CACHE_MASK;
981 int end_in_page = pos_in_page + len;
982 loff_t i_size;
Sage Weil1d3576f2009-10-06 11:31:09 -0700983 int r;
Sage Weil80e755f2010-03-31 21:52:10 -0700984 struct ceph_snap_context *snapc, *oldest;
Sage Weil1d3576f2009-10-06 11:31:09 -0700985
Sage Weil1d3576f2009-10-06 11:31:09 -0700986retry_locked:
987 /* writepages currently holds page lock, but if we change that later, */
988 wait_on_page_writeback(page);
989
990 /* check snap context */
991 BUG_ON(!ci->i_snap_realm);
992 down_read(&mdsc->snap_rwsem);
993 BUG_ON(!ci->i_snap_realm->cached_context);
Yan, Zheng61600ef2012-05-28 14:44:30 +0800994 snapc = page_snap_context(page);
Sage Weil80e755f2010-03-31 21:52:10 -0700995 if (snapc && snapc != ci->i_head_snapc) {
Sage Weil1d3576f2009-10-06 11:31:09 -0700996 /*
997 * this page is already dirty in another (older) snap
998 * context! is it writeable now?
999 */
Sage Weil80e755f2010-03-31 21:52:10 -07001000 oldest = get_oldest_context(inode, NULL);
Sage Weil1d3576f2009-10-06 11:31:09 -07001001 up_read(&mdsc->snap_rwsem);
1002
Sage Weil80e755f2010-03-31 21:52:10 -07001003 if (snapc->seq > oldest->seq) {
Sage Weil6298a332010-03-31 22:01:38 -07001004 ceph_put_snap_context(oldest);
Sage Weil1d3576f2009-10-06 11:31:09 -07001005 dout(" page %p snapc %p not current or oldest\n",
Sage Weil6298a332010-03-31 22:01:38 -07001006 page, snapc);
Sage Weil1d3576f2009-10-06 11:31:09 -07001007 /*
1008 * queue for writeback, and wait for snapc to
1009 * be writeable or written
1010 */
Sage Weil6298a332010-03-31 22:01:38 -07001011 snapc = ceph_get_snap_context(snapc);
Sage Weil1d3576f2009-10-06 11:31:09 -07001012 unlock_page(page);
Sage Weil3c6f6b72010-02-09 15:24:44 -08001013 ceph_queue_writeback(inode);
Sage Weil8f883c22010-03-19 13:27:53 -07001014 r = wait_event_interruptible(ci->i_cap_wq,
Sage Weil1d3576f2009-10-06 11:31:09 -07001015 context_is_writeable_or_written(inode, snapc));
1016 ceph_put_snap_context(snapc);
Sage Weil8f883c22010-03-19 13:27:53 -07001017 if (r == -ERESTARTSYS)
1018 return r;
Yehuda Sadeh4af6b222010-02-09 11:02:51 -08001019 return -EAGAIN;
Sage Weil1d3576f2009-10-06 11:31:09 -07001020 }
Sage Weil6298a332010-03-31 22:01:38 -07001021 ceph_put_snap_context(oldest);
Sage Weil1d3576f2009-10-06 11:31:09 -07001022
1023 /* yay, writeable, do it now (without dropping page lock) */
1024 dout(" page %p snapc %p not current, but oldest\n",
1025 page, snapc);
1026 if (!clear_page_dirty_for_io(page))
1027 goto retry_locked;
1028 r = writepage_nounlock(page, NULL);
1029 if (r < 0)
1030 goto fail_nosnap;
1031 goto retry_locked;
1032 }
1033
1034 if (PageUptodate(page)) {
1035 dout(" page %p already uptodate\n", page);
1036 return 0;
1037 }
1038
1039 /* full page? */
1040 if (pos_in_page == 0 && len == PAGE_CACHE_SIZE)
1041 return 0;
1042
1043 /* past end of file? */
1044 i_size = inode->i_size; /* caller holds i_mutex */
1045
1046 if (i_size + len > inode->i_sb->s_maxbytes) {
1047 /* file is too big */
1048 r = -EINVAL;
1049 goto fail;
1050 }
1051
1052 if (page_off >= i_size ||
1053 (pos_in_page == 0 && (pos+len) >= i_size &&
1054 end_in_page - pos_in_page != PAGE_CACHE_SIZE)) {
1055 dout(" zeroing %p 0 - %d and %d - %d\n",
1056 page, pos_in_page, end_in_page, (int)PAGE_CACHE_SIZE);
1057 zero_user_segments(page,
1058 0, pos_in_page,
1059 end_in_page, PAGE_CACHE_SIZE);
1060 return 0;
1061 }
1062
1063 /* we need to read it. */
1064 up_read(&mdsc->snap_rwsem);
1065 r = readpage_nounlock(file, page);
1066 if (r < 0)
1067 goto fail_nosnap;
1068 goto retry_locked;
1069
1070fail:
1071 up_read(&mdsc->snap_rwsem);
1072fail_nosnap:
1073 unlock_page(page);
1074 return r;
1075}
1076
1077/*
Yehuda Sadeh4af6b222010-02-09 11:02:51 -08001078 * We are only allowed to write into/dirty the page if the page is
1079 * clean, or already dirty within the same snap context.
1080 */
1081static int ceph_write_begin(struct file *file, struct address_space *mapping,
1082 loff_t pos, unsigned len, unsigned flags,
1083 struct page **pagep, void **fsdata)
1084{
Al Viro496ad9a2013-01-23 17:07:38 -05001085 struct inode *inode = file_inode(file);
Yehuda Sadeh4af6b222010-02-09 11:02:51 -08001086 struct page *page;
1087 pgoff_t index = pos >> PAGE_CACHE_SHIFT;
Sage Weil7971bd92013-05-01 21:15:58 -07001088 int r;
Yehuda Sadeh4af6b222010-02-09 11:02:51 -08001089
1090 do {
Sage Weil8f883c22010-03-19 13:27:53 -07001091 /* get a page */
Yehuda Sadeh4af6b222010-02-09 11:02:51 -08001092 page = grab_cache_page_write_begin(mapping, index, 0);
Sage Weil7971bd92013-05-01 21:15:58 -07001093 if (!page)
1094 return -ENOMEM;
1095 *pagep = page;
Yehuda Sadeh4af6b222010-02-09 11:02:51 -08001096
1097 dout("write_begin file %p inode %p page %p %d~%d\n", file,
Sage Weil213c99e2010-08-03 10:25:11 -07001098 inode, page, (int)pos, (int)len);
Yehuda Sadeh4af6b222010-02-09 11:02:51 -08001099
1100 r = ceph_update_writeable_page(file, pos, len, page);
1101 } while (r == -EAGAIN);
1102
1103 return r;
1104}
1105
1106/*
Sage Weil1d3576f2009-10-06 11:31:09 -07001107 * we don't do anything in here that simple_write_end doesn't do
1108 * except adjust dirty page accounting and drop read lock on
1109 * mdsc->snap_rwsem.
1110 */
1111static int ceph_write_end(struct file *file, struct address_space *mapping,
1112 loff_t pos, unsigned len, unsigned copied,
1113 struct page *page, void *fsdata)
1114{
Al Viro496ad9a2013-01-23 17:07:38 -05001115 struct inode *inode = file_inode(file);
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001116 struct ceph_fs_client *fsc = ceph_inode_to_client(inode);
1117 struct ceph_mds_client *mdsc = fsc->mdsc;
Sage Weil1d3576f2009-10-06 11:31:09 -07001118 unsigned from = pos & (PAGE_CACHE_SIZE - 1);
1119 int check_cap = 0;
1120
1121 dout("write_end file %p inode %p page %p %d~%d (%d)\n", file,
1122 inode, page, (int)pos, (int)copied, (int)len);
1123
1124 /* zero the stale part of the page if we did a short copy */
1125 if (copied < len)
1126 zero_user_segment(page, from+copied, len);
1127
1128 /* did file size increase? */
1129 /* (no need for i_size_read(); we caller holds i_mutex */
1130 if (pos+copied > inode->i_size)
1131 check_cap = ceph_inode_set_size(inode, pos+copied);
1132
1133 if (!PageUptodate(page))
1134 SetPageUptodate(page);
1135
1136 set_page_dirty(page);
1137
1138 unlock_page(page);
1139 up_read(&mdsc->snap_rwsem);
1140 page_cache_release(page);
1141
1142 if (check_cap)
1143 ceph_check_caps(ceph_inode(inode), CHECK_CAPS_AUTHONLY, NULL);
1144
1145 return copied;
1146}
1147
1148/*
1149 * we set .direct_IO to indicate direct io is supported, but since we
1150 * intercept O_DIRECT reads and writes early, this function should
1151 * never get called.
1152 */
1153static ssize_t ceph_direct_io(int rw, struct kiocb *iocb,
1154 const struct iovec *iov,
1155 loff_t pos, unsigned long nr_segs)
1156{
1157 WARN_ON(1);
1158 return -EINVAL;
1159}
1160
1161const struct address_space_operations ceph_aops = {
1162 .readpage = ceph_readpage,
1163 .readpages = ceph_readpages,
1164 .writepage = ceph_writepage,
1165 .writepages = ceph_writepages_start,
1166 .write_begin = ceph_write_begin,
1167 .write_end = ceph_write_end,
1168 .set_page_dirty = ceph_set_page_dirty,
1169 .invalidatepage = ceph_invalidatepage,
1170 .releasepage = ceph_releasepage,
1171 .direct_IO = ceph_direct_io,
1172};
1173
1174
1175/*
1176 * vm ops
1177 */
1178
1179/*
1180 * Reuse write_begin here for simplicity.
1181 */
1182static int ceph_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
1183{
Al Viro496ad9a2013-01-23 17:07:38 -05001184 struct inode *inode = file_inode(vma->vm_file);
Sage Weil1d3576f2009-10-06 11:31:09 -07001185 struct page *page = vmf->page;
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001186 struct ceph_mds_client *mdsc = ceph_inode_to_client(inode)->mdsc;
Alex Elder6285bc22012-10-02 10:25:51 -05001187 loff_t off = page_offset(page);
Sage Weil1d3576f2009-10-06 11:31:09 -07001188 loff_t size, len;
Sage Weil1d3576f2009-10-06 11:31:09 -07001189 int ret;
1190
Jan Kara3ca9c3b2012-06-12 16:20:24 +02001191 /* Update time before taking page lock */
1192 file_update_time(vma->vm_file);
1193
Sage Weil1d3576f2009-10-06 11:31:09 -07001194 size = i_size_read(inode);
1195 if (off + PAGE_CACHE_SIZE <= size)
1196 len = PAGE_CACHE_SIZE;
1197 else
1198 len = size & ~PAGE_CACHE_MASK;
1199
1200 dout("page_mkwrite %p %llu~%llu page %p idx %lu\n", inode,
1201 off, len, page, page->index);
Yehuda Sadeh4af6b222010-02-09 11:02:51 -08001202
1203 lock_page(page);
1204
1205 ret = VM_FAULT_NOPAGE;
1206 if ((off > size) ||
1207 (page->mapping != inode->i_mapping))
1208 goto out;
1209
1210 ret = ceph_update_writeable_page(vma->vm_file, off, len, page);
1211 if (ret == 0) {
1212 /* success. we'll keep the page locked. */
Sage Weil1d3576f2009-10-06 11:31:09 -07001213 set_page_dirty(page);
1214 up_read(&mdsc->snap_rwsem);
Sage Weil1d3576f2009-10-06 11:31:09 -07001215 ret = VM_FAULT_LOCKED;
1216 } else {
Yehuda Sadeh4af6b222010-02-09 11:02:51 -08001217 if (ret == -ENOMEM)
1218 ret = VM_FAULT_OOM;
1219 else
1220 ret = VM_FAULT_SIGBUS;
Sage Weil1d3576f2009-10-06 11:31:09 -07001221 }
Yehuda Sadeh4af6b222010-02-09 11:02:51 -08001222out:
Sage Weil1d3576f2009-10-06 11:31:09 -07001223 dout("page_mkwrite %p %llu~%llu = %d\n", inode, off, len, ret);
Yehuda Sadeh4af6b222010-02-09 11:02:51 -08001224 if (ret != VM_FAULT_LOCKED)
1225 unlock_page(page);
Sage Weil1d3576f2009-10-06 11:31:09 -07001226 return ret;
1227}
1228
1229static struct vm_operations_struct ceph_vmops = {
1230 .fault = filemap_fault,
1231 .page_mkwrite = ceph_page_mkwrite,
Konstantin Khlebnikov0b173bc2012-10-08 16:28:46 -07001232 .remap_pages = generic_file_remap_pages,
Sage Weil1d3576f2009-10-06 11:31:09 -07001233};
1234
1235int ceph_mmap(struct file *file, struct vm_area_struct *vma)
1236{
1237 struct address_space *mapping = file->f_mapping;
1238
1239 if (!mapping->a_ops->readpage)
1240 return -ENOEXEC;
1241 file_accessed(file);
1242 vma->vm_ops = &ceph_vmops;
Sage Weil1d3576f2009-10-06 11:31:09 -07001243 return 0;
1244}