ceph: fix sleeping function called from invalid context.
[deliverable/linux.git] / fs / ceph / addr.c
CommitLineData
3d14c5d2 1#include <linux/ceph/ceph_debug.h>
1d3576fd
SW
2
3#include <linux/backing-dev.h>
4#include <linux/fs.h>
5#include <linux/mm.h>
6#include <linux/pagemap.h>
7#include <linux/writeback.h> /* generic_writepages */
5a0e3ad6 8#include <linux/slab.h>
1d3576fd
SW
9#include <linux/pagevec.h>
10#include <linux/task_io_accounting_ops.h>
11
12#include "super.h"
3d14c5d2
YS
13#include "mds_client.h"
14#include <linux/ceph/osd_client.h>
1d3576fd
SW
15
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
25985edc 27 * count dirty pages on the inode. In the absence of snapshots,
1d3576fd
SW
28 * 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
2baba250
YS
52#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
61600ef8
YZ
57static 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}
1d3576fd
SW
63
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;
73 int undo = 0;
74 struct ceph_snap_context *snapc;
75
76 if (unlikely(!mapping))
77 return !TestSetPageDirty(page);
78
79 if (TestSetPageDirty(page)) {
80 dout("%p set_page_dirty %p idx %lu -- already dirty\n",
81 mapping->host, page, page->index);
82 return 0;
83 }
84
85 inode = mapping->host;
86 ci = ceph_inode(inode);
87
88 /*
89 * Note that we're grabbing a snapc ref here without holding
90 * any locks!
91 */
92 snapc = ceph_get_snap_context(ci->i_snap_realm->cached_context);
93
94 /* dirty the head */
be655596 95 spin_lock(&ci->i_ceph_lock);
7d8cb26d 96 if (ci->i_head_snapc == NULL)
1d3576fd
SW
97 ci->i_head_snapc = ceph_get_snap_context(snapc);
98 ++ci->i_wrbuffer_ref_head;
99 if (ci->i_wrbuffer_ref == 0)
0444d76a 100 ihold(inode);
1d3576fd
SW
101 ++ci->i_wrbuffer_ref;
102 dout("%p set_page_dirty %p idx %lu head %d/%d -> %d/%d "
103 "snapc %p seq %lld (%d snaps)\n",
104 mapping->host, page, page->index,
105 ci->i_wrbuffer_ref-1, ci->i_wrbuffer_ref_head-1,
106 ci->i_wrbuffer_ref, ci->i_wrbuffer_ref_head,
107 snapc, snapc->seq, snapc->num_snaps);
be655596 108 spin_unlock(&ci->i_ceph_lock);
1d3576fd
SW
109
110 /* now adjust page */
111 spin_lock_irq(&mapping->tree_lock);
112 if (page->mapping) { /* Race with truncate? */
113 WARN_ON_ONCE(!PageUptodate(page));
679ceace 114 account_page_dirtied(page, page->mapping);
1d3576fd
SW
115 radix_tree_tag_set(&mapping->page_tree,
116 page_index(page), PAGECACHE_TAG_DIRTY);
117
118 /*
119 * Reference snap context in page->private. Also set
120 * PagePrivate so that we get invalidatepage callback.
121 */
122 page->private = (unsigned long)snapc;
123 SetPagePrivate(page);
124 } else {
125 dout("ANON set_page_dirty %p (raced truncate?)\n", page);
126 undo = 1;
127 }
128
129 spin_unlock_irq(&mapping->tree_lock);
130
131 if (undo)
132 /* whoops, we failed to dirty the page */
133 ceph_put_wrbuffer_cap_refs(ci, 1, snapc);
134
135 __mark_inode_dirty(mapping->host, I_DIRTY_PAGES);
136
137 BUG_ON(!PageDirty(page));
138 return 1;
139}
140
141/*
142 * If we are truncating the full page (i.e. offset == 0), adjust the
143 * dirty page counters appropriately. Only called if there is private
144 * data on the page.
145 */
146static void ceph_invalidatepage(struct page *page, unsigned long offset)
147{
4ce1e9ad 148 struct inode *inode;
1d3576fd 149 struct ceph_inode_info *ci;
61600ef8 150 struct ceph_snap_context *snapc = page_snap_context(page);
1d3576fd
SW
151
152 BUG_ON(!PageLocked(page));
1d3576fd
SW
153 BUG_ON(!PagePrivate(page));
154 BUG_ON(!page->mapping);
155
4ce1e9ad
AB
156 inode = page->mapping->host;
157
1d3576fd
SW
158 /*
159 * We can get non-dirty pages here due to races between
160 * set_page_dirty and truncate_complete_page; just spit out a
161 * warning, in case we end up with accounting problems later.
162 */
163 if (!PageDirty(page))
164 pr_err("%p invalidatepage %p page not dirty\n", inode, page);
165
166 if (offset == 0)
167 ClearPageChecked(page);
168
169 ci = ceph_inode(inode);
170 if (offset == 0) {
171 dout("%p invalidatepage %p idx %lu full dirty page %lu\n",
172 inode, page, page->index, offset);
173 ceph_put_wrbuffer_cap_refs(ci, 1, snapc);
174 ceph_put_snap_context(snapc);
175 page->private = 0;
176 ClearPagePrivate(page);
177 } else {
178 dout("%p invalidatepage %p idx %lu partial dirty page\n",
179 inode, page, page->index);
180 }
181}
182
183/* just a sanity check */
184static int ceph_releasepage(struct page *page, gfp_t g)
185{
186 struct inode *inode = page->mapping ? page->mapping->host : NULL;
187 dout("%p releasepage %p idx %lu\n", inode, page, page->index);
188 WARN_ON(PageDirty(page));
1d3576fd
SW
189 WARN_ON(PagePrivate(page));
190 return 0;
191}
192
193/*
194 * read a single page, without unlocking it.
195 */
196static int readpage_nounlock(struct file *filp, struct page *page)
197{
496ad9aa 198 struct inode *inode = file_inode(filp);
1d3576fd 199 struct ceph_inode_info *ci = ceph_inode(inode);
3d14c5d2
YS
200 struct ceph_osd_client *osdc =
201 &ceph_inode_to_client(inode)->client->osdc;
1d3576fd
SW
202 int err = 0;
203 u64 len = PAGE_CACHE_SIZE;
204
205 dout("readpage inode %p file %p page %p index %lu\n",
206 inode, filp, page, page->index);
207 err = ceph_osdc_readpages(osdc, ceph_vino(inode), &ci->i_layout,
6285bc23 208 (u64) page_offset(page), &len,
1d3576fd 209 ci->i_truncate_seq, ci->i_truncate_size,
b7495fc2 210 &page, 1, 0);
1d3576fd
SW
211 if (err == -ENOENT)
212 err = 0;
213 if (err < 0) {
214 SetPageError(page);
215 goto out;
216 } else if (err < PAGE_CACHE_SIZE) {
217 /* zero fill remainder of page */
218 zero_user_segment(page, err, PAGE_CACHE_SIZE);
219 }
220 SetPageUptodate(page);
221
222out:
223 return err < 0 ? err : 0;
224}
225
226static int ceph_readpage(struct file *filp, struct page *page)
227{
228 int r = readpage_nounlock(filp, page);
229 unlock_page(page);
230 return r;
231}
232
233/*
7c272194 234 * Finish an async read(ahead) op.
1d3576fd 235 */
7c272194 236static void finish_read(struct ceph_osd_request *req, struct ceph_msg *msg)
1d3576fd 237{
7c272194 238 struct inode *inode = req->r_inode;
87060c10 239 struct ceph_osd_data *osd_data;
1b83bef2
SW
240 int rc = req->r_result;
241 int bytes = le32_to_cpu(msg->hdr.data_len);
e0c59487 242 int num_pages;
7c272194 243 int i;
1d3576fd 244
7c272194
SW
245 dout("finish_read %p req %p rc %d bytes %d\n", inode, req, rc, bytes);
246
247 /* unlock all pages, zeroing any data we didn't read */
406e2c9f 248 osd_data = osd_req_op_extent_osd_data(req, 0);
87060c10
AE
249 BUG_ON(osd_data->type != CEPH_OSD_DATA_TYPE_PAGES);
250 num_pages = calc_pages_for((u64)osd_data->alignment,
251 (u64)osd_data->length);
e0c59487 252 for (i = 0; i < num_pages; i++) {
87060c10 253 struct page *page = osd_data->pages[i];
7c272194
SW
254
255 if (bytes < (int)PAGE_CACHE_SIZE) {
256 /* zero (remainder of) page */
257 int s = bytes < 0 ? 0 : bytes;
258 zero_user_segment(page, s, PAGE_CACHE_SIZE);
1d3576fd 259 }
7c272194
SW
260 dout("finish_read %p uptodate %p idx %lu\n", inode, page,
261 page->index);
262 flush_dcache_page(page);
263 SetPageUptodate(page);
264 unlock_page(page);
265 page_cache_release(page);
0fff87ec 266 bytes -= PAGE_CACHE_SIZE;
1d3576fd 267 }
87060c10 268 kfree(osd_data->pages);
1d3576fd
SW
269}
270
8884d53d
DZ
271static void ceph_unlock_page_vector(struct page **pages, int num_pages)
272{
273 int i;
274
275 for (i = 0; i < num_pages; i++)
276 unlock_page(pages[i]);
277}
278
1d3576fd 279/*
7c272194
SW
280 * start an async read(ahead) operation. return nr_pages we submitted
281 * a read for on success, or negative error code.
1d3576fd 282 */
0d66a487 283static int start_read(struct inode *inode, struct list_head *page_list, int max)
1d3576fd 284{
3d14c5d2
YS
285 struct ceph_osd_client *osdc =
286 &ceph_inode_to_client(inode)->client->osdc;
7c272194
SW
287 struct ceph_inode_info *ci = ceph_inode(inode);
288 struct page *page = list_entry(page_list->prev, struct page, lru);
acead002 289 struct ceph_vino vino;
7c272194
SW
290 struct ceph_osd_request *req;
291 u64 off;
1d3576fd 292 u64 len;
7c272194
SW
293 int i;
294 struct page **pages;
295 pgoff_t next_index;
296 int nr_pages = 0;
297 int ret;
1d3576fd 298
6285bc23 299 off = (u64) page_offset(page);
1d3576fd 300
7c272194
SW
301 /* count pages */
302 next_index = page->index;
303 list_for_each_entry_reverse(page, page_list, lru) {
304 if (page->index != next_index)
305 break;
306 nr_pages++;
307 next_index++;
0d66a487
SW
308 if (max && nr_pages == max)
309 break;
7c272194 310 }
1d3576fd 311 len = nr_pages << PAGE_CACHE_SHIFT;
7c272194
SW
312 dout("start_read %p nr_pages %d is %lld~%lld\n", inode, nr_pages,
313 off, len);
acead002
AE
314 vino = ceph_vino(inode);
315 req = ceph_osdc_new_request(osdc, &ci->i_layout, vino, off, &len,
79528734 316 1, CEPH_OSD_OP_READ,
acead002 317 CEPH_OSD_FLAG_READ, NULL,
7c272194 318 ci->i_truncate_seq, ci->i_truncate_size,
acead002 319 false);
6816282d
SW
320 if (IS_ERR(req))
321 return PTR_ERR(req);
1d3576fd 322
7c272194 323 /* build page vector */
cf7b7e14 324 nr_pages = calc_pages_for(0, len);
7c272194
SW
325 pages = kmalloc(sizeof(*pages) * nr_pages, GFP_NOFS);
326 ret = -ENOMEM;
327 if (!pages)
328 goto out;
329 for (i = 0; i < nr_pages; ++i) {
330 page = list_entry(page_list->prev, struct page, lru);
331 BUG_ON(PageLocked(page));
1d3576fd 332 list_del(&page->lru);
7c272194
SW
333
334 dout("start_read %p adding %p idx %lu\n", inode, page,
335 page->index);
336 if (add_to_page_cache_lru(page, &inode->i_data, page->index,
213c99ee 337 GFP_NOFS)) {
1d3576fd 338 page_cache_release(page);
7c272194 339 dout("start_read %p add_to_page_cache failed %p\n",
1d3576fd 340 inode, page);
7c272194
SW
341 nr_pages = i;
342 goto out_pages;
1d3576fd 343 }
7c272194 344 pages[i] = page;
1d3576fd 345 }
406e2c9f 346 osd_req_op_extent_osd_data_pages(req, 0, pages, len, 0, false, false);
7c272194
SW
347 req->r_callback = finish_read;
348 req->r_inode = inode;
349
79528734 350 ceph_osdc_build_request(req, off, NULL, vino.snap, NULL);
02ee07d3 351
7c272194
SW
352 dout("start_read %p starting %p %lld~%lld\n", inode, req, off, len);
353 ret = ceph_osdc_start_request(osdc, req, false);
354 if (ret < 0)
355 goto out_pages;
356 ceph_osdc_put_request(req);
357 return nr_pages;
358
359out_pages:
8884d53d 360 ceph_unlock_page_vector(pages, nr_pages);
7c272194 361 ceph_release_page_vector(pages, nr_pages);
7c272194
SW
362out:
363 ceph_osdc_put_request(req);
364 return ret;
365}
1d3576fd 366
7c272194
SW
367
368/*
369 * Read multiple pages. Leave pages we don't read + unlock in page_list;
370 * the caller (VM) cleans them up.
371 */
372static int ceph_readpages(struct file *file, struct address_space *mapping,
373 struct list_head *page_list, unsigned nr_pages)
374{
496ad9aa 375 struct inode *inode = file_inode(file);
0d66a487 376 struct ceph_fs_client *fsc = ceph_inode_to_client(inode);
7c272194 377 int rc = 0;
0d66a487
SW
378 int max = 0;
379
380 if (fsc->mount_options->rsize >= PAGE_CACHE_SIZE)
381 max = (fsc->mount_options->rsize + PAGE_CACHE_SIZE - 1)
382 >> PAGE_SHIFT;
7c272194 383
2794a82a
AE
384 dout("readpages %p file %p nr_pages %d max %d\n", inode,
385 file, nr_pages,
0d66a487 386 max);
7c272194 387 while (!list_empty(page_list)) {
0d66a487 388 rc = start_read(inode, page_list, max);
7c272194
SW
389 if (rc < 0)
390 goto out;
391 BUG_ON(rc == 0);
392 }
1d3576fd 393out:
7c272194 394 dout("readpages %p file %p ret %d\n", inode, file, rc);
1d3576fd
SW
395 return rc;
396}
397
398/*
399 * Get ref for the oldest snapc for an inode with dirty data... that is, the
400 * only snap context we are allowed to write back.
1d3576fd 401 */
6298a337
SW
402static struct ceph_snap_context *get_oldest_context(struct inode *inode,
403 u64 *snap_size)
1d3576fd
SW
404{
405 struct ceph_inode_info *ci = ceph_inode(inode);
406 struct ceph_snap_context *snapc = NULL;
407 struct ceph_cap_snap *capsnap = NULL;
408
be655596 409 spin_lock(&ci->i_ceph_lock);
1d3576fd
SW
410 list_for_each_entry(capsnap, &ci->i_cap_snaps, ci_item) {
411 dout(" cap_snap %p snapc %p has %d dirty pages\n", capsnap,
412 capsnap->context, capsnap->dirty_pages);
413 if (capsnap->dirty_pages) {
414 snapc = ceph_get_snap_context(capsnap->context);
415 if (snap_size)
416 *snap_size = capsnap->size;
417 break;
418 }
419 }
7d8cb26d 420 if (!snapc && ci->i_wrbuffer_ref_head) {
80e755fe 421 snapc = ceph_get_snap_context(ci->i_head_snapc);
1d3576fd
SW
422 dout(" head snapc %p has %d dirty pages\n",
423 snapc, ci->i_wrbuffer_ref_head);
424 }
be655596 425 spin_unlock(&ci->i_ceph_lock);
1d3576fd
SW
426 return snapc;
427}
428
429/*
430 * Write a single page, but leave the page locked.
431 *
432 * If we get a write error, set the page error bit, but still adjust the
433 * dirty page accounting (i.e., page is no longer dirty).
434 */
435static int writepage_nounlock(struct page *page, struct writeback_control *wbc)
436{
437 struct inode *inode;
438 struct ceph_inode_info *ci;
3d14c5d2 439 struct ceph_fs_client *fsc;
1d3576fd 440 struct ceph_osd_client *osdc;
6298a337 441 struct ceph_snap_context *snapc, *oldest;
fc2744aa 442 loff_t page_off = page_offset(page);
2baba250 443 long writeback_stat;
fc2744aa
YZ
444 u64 truncate_size, snap_size = 0;
445 u32 truncate_seq;
446 int err = 0, len = PAGE_CACHE_SIZE;
1d3576fd
SW
447
448 dout("writepage %p idx %lu\n", page, page->index);
449
450 if (!page->mapping || !page->mapping->host) {
451 dout("writepage %p - no mapping\n", page);
452 return -EFAULT;
453 }
454 inode = page->mapping->host;
455 ci = ceph_inode(inode);
3d14c5d2
YS
456 fsc = ceph_inode_to_client(inode);
457 osdc = &fsc->client->osdc;
1d3576fd
SW
458
459 /* verify this is a writeable snap context */
61600ef8 460 snapc = page_snap_context(page);
1d3576fd
SW
461 if (snapc == NULL) {
462 dout("writepage %p page %p not dirty?\n", inode, page);
463 goto out;
464 }
6298a337
SW
465 oldest = get_oldest_context(inode, &snap_size);
466 if (snapc->seq > oldest->seq) {
1d3576fd 467 dout("writepage %p page %p snapc %p not writeable - noop\n",
61600ef8 468 inode, page, snapc);
1d3576fd
SW
469 /* we should only noop if called by kswapd */
470 WARN_ON((current->flags & PF_MEMALLOC) == 0);
6298a337 471 ceph_put_snap_context(oldest);
1d3576fd
SW
472 goto out;
473 }
6298a337 474 ceph_put_snap_context(oldest);
1d3576fd 475
fc2744aa
YZ
476 spin_lock(&ci->i_ceph_lock);
477 truncate_seq = ci->i_truncate_seq;
478 truncate_size = ci->i_truncate_size;
479 if (!snap_size)
480 snap_size = i_size_read(inode);
481 spin_unlock(&ci->i_ceph_lock);
482
1d3576fd 483 /* is this a partial page at end of file? */
fc2744aa
YZ
484 if (page_off >= snap_size) {
485 dout("%p page eof %llu\n", page, snap_size);
486 goto out;
487 }
488 if (snap_size < page_off + len)
489 len = snap_size - page_off;
1d3576fd 490
ae00d4f3
SW
491 dout("writepage %p page %p index %lu on %llu~%u snapc %p\n",
492 inode, page, page->index, page_off, len, snapc);
1d3576fd 493
3d14c5d2 494 writeback_stat = atomic_long_inc_return(&fsc->writeback_count);
2baba250 495 if (writeback_stat >
3d14c5d2
YS
496 CONGESTION_ON_THRESH(fsc->mount_options->congestion_kb))
497 set_bdi_congested(&fsc->backing_dev_info, BLK_RW_ASYNC);
2baba250 498
1d3576fd
SW
499 set_page_writeback(page);
500 err = ceph_osdc_writepages(osdc, ceph_vino(inode),
501 &ci->i_layout, snapc,
502 page_off, len,
fc2744aa 503 truncate_seq, truncate_size,
24808826 504 &inode->i_mtime, &page, 1);
1d3576fd
SW
505 if (err < 0) {
506 dout("writepage setting page/mapping error %d %p\n", err, page);
507 SetPageError(page);
508 mapping_set_error(&inode->i_data, err);
509 if (wbc)
510 wbc->pages_skipped++;
511 } else {
512 dout("writepage cleaned page %p\n", page);
513 err = 0; /* vfs expects us to return 0 */
514 }
515 page->private = 0;
516 ClearPagePrivate(page);
517 end_page_writeback(page);
518 ceph_put_wrbuffer_cap_refs(ci, 1, snapc);
6298a337 519 ceph_put_snap_context(snapc); /* page's reference */
1d3576fd
SW
520out:
521 return err;
522}
523
524static int ceph_writepage(struct page *page, struct writeback_control *wbc)
525{
dbd646a8
YS
526 int err;
527 struct inode *inode = page->mapping->host;
528 BUG_ON(!inode);
70b666c3 529 ihold(inode);
dbd646a8 530 err = writepage_nounlock(page, wbc);
1d3576fd 531 unlock_page(page);
dbd646a8 532 iput(inode);
1d3576fd
SW
533 return err;
534}
535
536
537/*
538 * lame release_pages helper. release_pages() isn't exported to
539 * modules.
540 */
541static void ceph_release_pages(struct page **pages, int num)
542{
543 struct pagevec pvec;
544 int i;
545
546 pagevec_init(&pvec, 0);
547 for (i = 0; i < num; i++) {
548 if (pagevec_add(&pvec, pages[i]) == 0)
549 pagevec_release(&pvec);
550 }
551 pagevec_release(&pvec);
552}
553
554
555/*
556 * async writeback completion handler.
557 *
558 * If we get an error, set the mapping error bit, but not the individual
559 * page error bits.
560 */
561static void writepages_finish(struct ceph_osd_request *req,
562 struct ceph_msg *msg)
563{
564 struct inode *inode = req->r_inode;
1d3576fd 565 struct ceph_inode_info *ci = ceph_inode(inode);
87060c10 566 struct ceph_osd_data *osd_data;
1d3576fd 567 unsigned wrote;
1d3576fd 568 struct page *page;
e0c59487 569 int num_pages;
1d3576fd
SW
570 int i;
571 struct ceph_snap_context *snapc = req->r_snapc;
572 struct address_space *mapping = inode->i_mapping;
1b83bef2 573 int rc = req->r_result;
79528734 574 u64 bytes = req->r_ops[0].extent.length;
3d14c5d2 575 struct ceph_fs_client *fsc = ceph_inode_to_client(inode);
2baba250 576 long writeback_stat;
7ff899da 577 unsigned issued = ceph_caps_issued(ci);
1d3576fd 578
406e2c9f 579 osd_data = osd_req_op_extent_osd_data(req, 0);
87060c10
AE
580 BUG_ON(osd_data->type != CEPH_OSD_DATA_TYPE_PAGES);
581 num_pages = calc_pages_for((u64)osd_data->alignment,
582 (u64)osd_data->length);
1d3576fd 583 if (rc >= 0) {
79788c69
SW
584 /*
585 * Assume we wrote the pages we originally sent. The
586 * osd might reply with fewer pages if our writeback
587 * raced with a truncation and was adjusted at the osd,
588 * so don't believe the reply.
589 */
e0c59487 590 wrote = num_pages;
1d3576fd
SW
591 } else {
592 wrote = 0;
593 mapping_set_error(mapping, rc);
594 }
595 dout("writepages_finish %p rc %d bytes %llu wrote %d (pages)\n",
596 inode, rc, bytes, wrote);
597
598 /* clean all pages */
e0c59487 599 for (i = 0; i < num_pages; i++) {
87060c10 600 page = osd_data->pages[i];
1d3576fd
SW
601 BUG_ON(!page);
602 WARN_ON(!PageUptodate(page));
603
2baba250 604 writeback_stat =
3d14c5d2 605 atomic_long_dec_return(&fsc->writeback_count);
2baba250 606 if (writeback_stat <
3d14c5d2
YS
607 CONGESTION_OFF_THRESH(fsc->mount_options->congestion_kb))
608 clear_bdi_congested(&fsc->backing_dev_info,
2baba250
YS
609 BLK_RW_ASYNC);
610
61600ef8 611 ceph_put_snap_context(page_snap_context(page));
1d3576fd
SW
612 page->private = 0;
613 ClearPagePrivate(page);
1d3576fd
SW
614 dout("unlocking %d %p\n", i, page);
615 end_page_writeback(page);
e63dc5c7
YS
616
617 /*
618 * We lost the cache cap, need to truncate the page before
619 * it is unlocked, otherwise we'd truncate it later in the
620 * page truncation thread, possibly losing some data that
621 * raced its way in
622 */
2962507c 623 if ((issued & (CEPH_CAP_FILE_CACHE|CEPH_CAP_FILE_LAZYIO)) == 0)
e63dc5c7
YS
624 generic_error_remove_page(inode->i_mapping, page);
625
1d3576fd
SW
626 unlock_page(page);
627 }
628 dout("%p wrote+cleaned %d pages\n", inode, wrote);
e0c59487 629 ceph_put_wrbuffer_cap_refs(ci, num_pages, snapc);
1d3576fd 630
87060c10
AE
631 ceph_release_pages(osd_data->pages, num_pages);
632 if (osd_data->pages_from_pool)
633 mempool_free(osd_data->pages,
640ef79d 634 ceph_sb_to_client(inode->i_sb)->wb_pagevec_pool);
1d3576fd 635 else
87060c10 636 kfree(osd_data->pages);
1d3576fd
SW
637 ceph_osdc_put_request(req);
638}
639
1d3576fd
SW
640/*
641 * initiate async writeback
642 */
643static int ceph_writepages_start(struct address_space *mapping,
644 struct writeback_control *wbc)
645{
646 struct inode *inode = mapping->host;
1d3576fd 647 struct ceph_inode_info *ci = ceph_inode(inode);
fc2744aa
YZ
648 struct ceph_fs_client *fsc = ceph_inode_to_client(inode);
649 struct ceph_vino vino = ceph_vino(inode);
1d3576fd
SW
650 pgoff_t index, start, end;
651 int range_whole = 0;
652 int should_loop = 1;
653 pgoff_t max_pages = 0, max_pages_ever = 0;
80e755fe 654 struct ceph_snap_context *snapc = NULL, *last_snapc = NULL, *pgsnapc;
1d3576fd
SW
655 struct pagevec pvec;
656 int done = 0;
657 int rc = 0;
658 unsigned wsize = 1 << inode->i_blkbits;
659 struct ceph_osd_request *req = NULL;
660 int do_sync;
fc2744aa
YZ
661 u64 truncate_size, snap_size;
662 u32 truncate_seq;
1d3576fd
SW
663
664 /*
665 * Include a 'sync' in the OSD request if this is a data
666 * integrity write (e.g., O_SYNC write or fsync()), or if our
667 * cap is being revoked.
668 */
669 do_sync = wbc->sync_mode == WB_SYNC_ALL;
670 if (ceph_caps_revoking(ci, CEPH_CAP_FILE_BUFFER))
671 do_sync = 1;
672 dout("writepages_start %p dosync=%d (mode=%s)\n",
673 inode, do_sync,
674 wbc->sync_mode == WB_SYNC_NONE ? "NONE" :
675 (wbc->sync_mode == WB_SYNC_ALL ? "ALL" : "HOLD"));
676
3d14c5d2 677 if (fsc->mount_state == CEPH_MOUNT_SHUTDOWN) {
1d3576fd
SW
678 pr_warning("writepage_start %p on forced umount\n", inode);
679 return -EIO; /* we're in a forced umount, don't write! */
680 }
3d14c5d2
YS
681 if (fsc->mount_options->wsize && fsc->mount_options->wsize < wsize)
682 wsize = fsc->mount_options->wsize;
1d3576fd
SW
683 if (wsize < PAGE_CACHE_SIZE)
684 wsize = PAGE_CACHE_SIZE;
685 max_pages_ever = wsize >> PAGE_CACHE_SHIFT;
686
687 pagevec_init(&pvec, 0);
688
1d3576fd
SW
689 /* where to start/end? */
690 if (wbc->range_cyclic) {
691 start = mapping->writeback_index; /* Start from prev offset */
692 end = -1;
693 dout(" cyclic, start at %lu\n", start);
694 } else {
695 start = wbc->range_start >> PAGE_CACHE_SHIFT;
696 end = wbc->range_end >> PAGE_CACHE_SHIFT;
697 if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX)
698 range_whole = 1;
699 should_loop = 0;
700 dout(" not cyclic, %lu to %lu\n", start, end);
701 }
702 index = start;
703
704retry:
705 /* find oldest snap context with dirty data */
706 ceph_put_snap_context(snapc);
1ac0fc8a 707 snap_size = 0;
1d3576fd
SW
708 snapc = get_oldest_context(inode, &snap_size);
709 if (!snapc) {
710 /* hmm, why does writepages get called when there
711 is no dirty data? */
712 dout(" no snap context with dirty data?\n");
713 goto out;
714 }
1ac0fc8a
YZ
715 if (snap_size == 0)
716 snap_size = i_size_read(inode);
1d3576fd
SW
717 dout(" oldest snapc is %p seq %lld (%d snaps)\n",
718 snapc, snapc->seq, snapc->num_snaps);
fc2744aa
YZ
719
720 spin_lock(&ci->i_ceph_lock);
721 truncate_seq = ci->i_truncate_seq;
722 truncate_size = ci->i_truncate_size;
723 if (!snap_size)
724 snap_size = i_size_read(inode);
725 spin_unlock(&ci->i_ceph_lock);
726
1d3576fd
SW
727 if (last_snapc && snapc != last_snapc) {
728 /* if we switched to a newer snapc, restart our scan at the
729 * start of the original file range. */
730 dout(" snapc differs from last pass, restarting at %lu\n",
731 index);
732 index = start;
733 }
734 last_snapc = snapc;
735
736 while (!done && index <= end) {
e5975c7c 737 int num_ops = do_sync ? 2 : 1;
1d3576fd
SW
738 unsigned i;
739 int first;
740 pgoff_t next;
741 int pvec_pages, locked_pages;
e5975c7c
AE
742 struct page **pages = NULL;
743 mempool_t *pool = NULL; /* Becomes non-null if mempool used */
1d3576fd
SW
744 struct page *page;
745 int want;
746 u64 offset, len;
2baba250 747 long writeback_stat;
1d3576fd
SW
748
749 next = 0;
750 locked_pages = 0;
751 max_pages = max_pages_ever;
752
753get_more_pages:
754 first = -1;
755 want = min(end - index,
756 min((pgoff_t)PAGEVEC_SIZE,
757 max_pages - (pgoff_t)locked_pages) - 1)
758 + 1;
759 pvec_pages = pagevec_lookup_tag(&pvec, mapping, &index,
760 PAGECACHE_TAG_DIRTY,
761 want);
762 dout("pagevec_lookup_tag got %d\n", pvec_pages);
763 if (!pvec_pages && !locked_pages)
764 break;
765 for (i = 0; i < pvec_pages && locked_pages < max_pages; i++) {
766 page = pvec.pages[i];
767 dout("? %p idx %lu\n", page, page->index);
768 if (locked_pages == 0)
769 lock_page(page); /* first page */
770 else if (!trylock_page(page))
771 break;
772
773 /* only dirty pages, or our accounting breaks */
774 if (unlikely(!PageDirty(page)) ||
775 unlikely(page->mapping != mapping)) {
776 dout("!dirty or !mapping %p\n", page);
777 unlock_page(page);
778 break;
779 }
780 if (!wbc->range_cyclic && page->index > end) {
781 dout("end of range %p\n", page);
782 done = 1;
783 unlock_page(page);
784 break;
785 }
786 if (next && (page->index != next)) {
787 dout("not consecutive %p\n", page);
788 unlock_page(page);
789 break;
790 }
791 if (wbc->sync_mode != WB_SYNC_NONE) {
792 dout("waiting on writeback %p\n", page);
793 wait_on_page_writeback(page);
794 }
1ac0fc8a
YZ
795 if (page_offset(page) >= snap_size) {
796 dout("%p page eof %llu\n", page, snap_size);
1d3576fd
SW
797 done = 1;
798 unlock_page(page);
799 break;
800 }
801 if (PageWriteback(page)) {
802 dout("%p under writeback\n", page);
803 unlock_page(page);
804 break;
805 }
806
807 /* only if matching snap context */
61600ef8 808 pgsnapc = page_snap_context(page);
80e755fe
SW
809 if (pgsnapc->seq > snapc->seq) {
810 dout("page snapc %p %lld > oldest %p %lld\n",
811 pgsnapc, pgsnapc->seq, snapc, snapc->seq);
1d3576fd
SW
812 unlock_page(page);
813 if (!locked_pages)
814 continue; /* keep looking for snap */
815 break;
816 }
817
818 if (!clear_page_dirty_for_io(page)) {
819 dout("%p !clear_page_dirty_for_io\n", page);
820 unlock_page(page);
821 break;
822 }
823
e5975c7c
AE
824 /*
825 * We have something to write. If this is
826 * the first locked page this time through,
827 * allocate an osd request and a page array
828 * that it will use.
829 */
1d3576fd 830 if (locked_pages == 0) {
e5975c7c 831 BUG_ON(pages);
1d3576fd 832 /* prepare async write request */
e5975c7c 833 offset = (u64)page_offset(page);
1d3576fd 834 len = wsize;
fc2744aa
YZ
835 req = ceph_osdc_new_request(&fsc->client->osdc,
836 &ci->i_layout, vino,
837 offset, &len, num_ops,
838 CEPH_OSD_OP_WRITE,
839 CEPH_OSD_FLAG_WRITE |
840 CEPH_OSD_FLAG_ONDISK,
841 snapc, truncate_seq,
842 truncate_size, true);
6816282d
SW
843 if (IS_ERR(req)) {
844 rc = PTR_ERR(req);
8c71897b
HC
845 unlock_page(page);
846 break;
847 }
848
88486957
AE
849 req->r_callback = writepages_finish;
850 req->r_inode = inode;
851
852 max_pages = calc_pages_for(0, (u64)len);
fc2744aa
YZ
853 pages = kmalloc(max_pages * sizeof (*pages),
854 GFP_NOFS);
88486957
AE
855 if (!pages) {
856 pool = fsc->wb_pagevec_pool;
88486957 857 pages = mempool_alloc(pool, GFP_NOFS);
e5975c7c 858 BUG_ON(!pages);
88486957 859 }
1d3576fd
SW
860 }
861
862 /* note position of first page in pvec */
863 if (first < 0)
864 first = i;
865 dout("%p will write page %p idx %lu\n",
866 inode, page, page->index);
2baba250 867
213c99ee 868 writeback_stat =
3d14c5d2 869 atomic_long_inc_return(&fsc->writeback_count);
213c99ee 870 if (writeback_stat > CONGESTION_ON_THRESH(
3d14c5d2
YS
871 fsc->mount_options->congestion_kb)) {
872 set_bdi_congested(&fsc->backing_dev_info,
213c99ee 873 BLK_RW_ASYNC);
2baba250
YS
874 }
875
1d3576fd 876 set_page_writeback(page);
e5975c7c 877 pages[locked_pages] = page;
1d3576fd
SW
878 locked_pages++;
879 next = page->index + 1;
880 }
881
882 /* did we get anything? */
883 if (!locked_pages)
884 goto release_pvec_pages;
885 if (i) {
886 int j;
887 BUG_ON(!locked_pages || first < 0);
888
889 if (pvec_pages && i == pvec_pages &&
890 locked_pages < max_pages) {
891 dout("reached end pvec, trying for more\n");
892 pagevec_reinit(&pvec);
893 goto get_more_pages;
894 }
895
896 /* shift unused pages over in the pvec... we
897 * will need to release them below. */
898 for (j = i; j < pvec_pages; j++) {
899 dout(" pvec leftover page %p\n",
900 pvec.pages[j]);
901 pvec.pages[j-i+first] = pvec.pages[j];
902 }
903 pvec.nr -= i-first;
904 }
905
e5975c7c
AE
906 /* Format the osd request message and submit the write */
907
908 offset = page_offset(pages[0]);
1ac0fc8a 909 len = min(snap_size - offset,
1d3576fd
SW
910 (u64)locked_pages << PAGE_CACHE_SHIFT);
911 dout("writepages got %d pages at %llu~%llu\n",
912 locked_pages, offset, len);
913
406e2c9f 914 osd_req_op_extent_osd_data_pages(req, 0, pages, len, 0,
a4ce40a9 915 !!pool, false);
e5975c7c
AE
916
917 pages = NULL; /* request message now owns the pages array */
918 pool = NULL;
919
920 /* Update the write op length in case we changed it */
921
c99d2d4a 922 osd_req_op_extent_update(req, 0, len);
e5975c7c
AE
923
924 vino = ceph_vino(inode);
79528734
AE
925 ceph_osdc_build_request(req, offset, snapc, vino.snap,
926 &inode->i_mtime);
1d3576fd 927
9d6fcb08
SW
928 rc = ceph_osdc_start_request(&fsc->client->osdc, req, true);
929 BUG_ON(rc);
1d3576fd
SW
930 req = NULL;
931
932 /* continue? */
933 index = next;
934 wbc->nr_to_write -= locked_pages;
935 if (wbc->nr_to_write <= 0)
936 done = 1;
937
938release_pvec_pages:
939 dout("pagevec_release on %d pages (%p)\n", (int)pvec.nr,
940 pvec.nr ? pvec.pages[0] : NULL);
941 pagevec_release(&pvec);
942
943 if (locked_pages && !done)
944 goto retry;
945 }
946
947 if (should_loop && !done) {
948 /* more to do; loop back to beginning of file */
949 dout("writepages looping back to beginning of file\n");
950 should_loop = 0;
951 index = 0;
952 goto retry;
953 }
954
955 if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0))
956 mapping->writeback_index = index;
957
958out:
959 if (req)
960 ceph_osdc_put_request(req);
1d3576fd
SW
961 ceph_put_snap_context(snapc);
962 dout("writepages done, rc = %d\n", rc);
1d3576fd
SW
963 return rc;
964}
965
966
967
968/*
969 * See if a given @snapc is either writeable, or already written.
970 */
971static int context_is_writeable_or_written(struct inode *inode,
972 struct ceph_snap_context *snapc)
973{
974 struct ceph_snap_context *oldest = get_oldest_context(inode, NULL);
6298a337
SW
975 int ret = !oldest || snapc->seq <= oldest->seq;
976
977 ceph_put_snap_context(oldest);
978 return ret;
1d3576fd
SW
979}
980
981/*
982 * We are only allowed to write into/dirty the page if the page is
983 * clean, or already dirty within the same snap context.
8f883c24
SW
984 *
985 * called with page locked.
986 * return success with page locked,
987 * or any failure (incl -EAGAIN) with page unlocked.
1d3576fd 988 */
4af6b225
YS
989static int ceph_update_writeable_page(struct file *file,
990 loff_t pos, unsigned len,
991 struct page *page)
1d3576fd 992{
496ad9aa 993 struct inode *inode = file_inode(file);
1d3576fd 994 struct ceph_inode_info *ci = ceph_inode(inode);
3d14c5d2 995 struct ceph_mds_client *mdsc = ceph_inode_to_client(inode)->mdsc;
1d3576fd
SW
996 loff_t page_off = pos & PAGE_CACHE_MASK;
997 int pos_in_page = pos & ~PAGE_CACHE_MASK;
998 int end_in_page = pos_in_page + len;
999 loff_t i_size;
1d3576fd 1000 int r;
80e755fe 1001 struct ceph_snap_context *snapc, *oldest;
1d3576fd 1002
1d3576fd
SW
1003retry_locked:
1004 /* writepages currently holds page lock, but if we change that later, */
1005 wait_on_page_writeback(page);
1006
1007 /* check snap context */
1008 BUG_ON(!ci->i_snap_realm);
1009 down_read(&mdsc->snap_rwsem);
1010 BUG_ON(!ci->i_snap_realm->cached_context);
61600ef8 1011 snapc = page_snap_context(page);
80e755fe 1012 if (snapc && snapc != ci->i_head_snapc) {
1d3576fd
SW
1013 /*
1014 * this page is already dirty in another (older) snap
1015 * context! is it writeable now?
1016 */
80e755fe 1017 oldest = get_oldest_context(inode, NULL);
1d3576fd
SW
1018 up_read(&mdsc->snap_rwsem);
1019
80e755fe 1020 if (snapc->seq > oldest->seq) {
6298a337 1021 ceph_put_snap_context(oldest);
1d3576fd 1022 dout(" page %p snapc %p not current or oldest\n",
6298a337 1023 page, snapc);
1d3576fd
SW
1024 /*
1025 * queue for writeback, and wait for snapc to
1026 * be writeable or written
1027 */
6298a337 1028 snapc = ceph_get_snap_context(snapc);
1d3576fd 1029 unlock_page(page);
3c6f6b79 1030 ceph_queue_writeback(inode);
8f883c24 1031 r = wait_event_interruptible(ci->i_cap_wq,
1d3576fd
SW
1032 context_is_writeable_or_written(inode, snapc));
1033 ceph_put_snap_context(snapc);
8f883c24
SW
1034 if (r == -ERESTARTSYS)
1035 return r;
4af6b225 1036 return -EAGAIN;
1d3576fd 1037 }
6298a337 1038 ceph_put_snap_context(oldest);
1d3576fd
SW
1039
1040 /* yay, writeable, do it now (without dropping page lock) */
1041 dout(" page %p snapc %p not current, but oldest\n",
1042 page, snapc);
1043 if (!clear_page_dirty_for_io(page))
1044 goto retry_locked;
1045 r = writepage_nounlock(page, NULL);
1046 if (r < 0)
1047 goto fail_nosnap;
1048 goto retry_locked;
1049 }
1050
1051 if (PageUptodate(page)) {
1052 dout(" page %p already uptodate\n", page);
1053 return 0;
1054 }
1055
1056 /* full page? */
1057 if (pos_in_page == 0 && len == PAGE_CACHE_SIZE)
1058 return 0;
1059
1060 /* past end of file? */
1061 i_size = inode->i_size; /* caller holds i_mutex */
1062
1063 if (i_size + len > inode->i_sb->s_maxbytes) {
1064 /* file is too big */
1065 r = -EINVAL;
1066 goto fail;
1067 }
1068
1069 if (page_off >= i_size ||
1070 (pos_in_page == 0 && (pos+len) >= i_size &&
1071 end_in_page - pos_in_page != PAGE_CACHE_SIZE)) {
1072 dout(" zeroing %p 0 - %d and %d - %d\n",
1073 page, pos_in_page, end_in_page, (int)PAGE_CACHE_SIZE);
1074 zero_user_segments(page,
1075 0, pos_in_page,
1076 end_in_page, PAGE_CACHE_SIZE);
1077 return 0;
1078 }
1079
1080 /* we need to read it. */
1081 up_read(&mdsc->snap_rwsem);
1082 r = readpage_nounlock(file, page);
1083 if (r < 0)
1084 goto fail_nosnap;
1085 goto retry_locked;
1086
1087fail:
1088 up_read(&mdsc->snap_rwsem);
1089fail_nosnap:
1090 unlock_page(page);
1091 return r;
1092}
1093
4af6b225
YS
1094/*
1095 * We are only allowed to write into/dirty the page if the page is
1096 * clean, or already dirty within the same snap context.
1097 */
1098static int ceph_write_begin(struct file *file, struct address_space *mapping,
1099 loff_t pos, unsigned len, unsigned flags,
1100 struct page **pagep, void **fsdata)
1101{
496ad9aa 1102 struct inode *inode = file_inode(file);
4af6b225
YS
1103 struct page *page;
1104 pgoff_t index = pos >> PAGE_CACHE_SHIFT;
7971bd92 1105 int r;
4af6b225
YS
1106
1107 do {
8f883c24 1108 /* get a page */
4af6b225 1109 page = grab_cache_page_write_begin(mapping, index, 0);
7971bd92
SW
1110 if (!page)
1111 return -ENOMEM;
1112 *pagep = page;
4af6b225
YS
1113
1114 dout("write_begin file %p inode %p page %p %d~%d\n", file,
213c99ee 1115 inode, page, (int)pos, (int)len);
4af6b225
YS
1116
1117 r = ceph_update_writeable_page(file, pos, len, page);
1118 } while (r == -EAGAIN);
1119
1120 return r;
1121}
1122
1d3576fd
SW
1123/*
1124 * we don't do anything in here that simple_write_end doesn't do
1125 * except adjust dirty page accounting and drop read lock on
1126 * mdsc->snap_rwsem.
1127 */
1128static int ceph_write_end(struct file *file, struct address_space *mapping,
1129 loff_t pos, unsigned len, unsigned copied,
1130 struct page *page, void *fsdata)
1131{
496ad9aa 1132 struct inode *inode = file_inode(file);
3d14c5d2
YS
1133 struct ceph_fs_client *fsc = ceph_inode_to_client(inode);
1134 struct ceph_mds_client *mdsc = fsc->mdsc;
1d3576fd
SW
1135 unsigned from = pos & (PAGE_CACHE_SIZE - 1);
1136 int check_cap = 0;
1137
1138 dout("write_end file %p inode %p page %p %d~%d (%d)\n", file,
1139 inode, page, (int)pos, (int)copied, (int)len);
1140
1141 /* zero the stale part of the page if we did a short copy */
1142 if (copied < len)
1143 zero_user_segment(page, from+copied, len);
1144
1145 /* did file size increase? */
1146 /* (no need for i_size_read(); we caller holds i_mutex */
1147 if (pos+copied > inode->i_size)
1148 check_cap = ceph_inode_set_size(inode, pos+copied);
1149
1150 if (!PageUptodate(page))
1151 SetPageUptodate(page);
1152
1153 set_page_dirty(page);
1154
1155 unlock_page(page);
1156 up_read(&mdsc->snap_rwsem);
1157 page_cache_release(page);
1158
1159 if (check_cap)
1160 ceph_check_caps(ceph_inode(inode), CHECK_CAPS_AUTHONLY, NULL);
1161
1162 return copied;
1163}
1164
1165/*
1166 * we set .direct_IO to indicate direct io is supported, but since we
1167 * intercept O_DIRECT reads and writes early, this function should
1168 * never get called.
1169 */
1170static ssize_t ceph_direct_io(int rw, struct kiocb *iocb,
1171 const struct iovec *iov,
1172 loff_t pos, unsigned long nr_segs)
1173{
1174 WARN_ON(1);
1175 return -EINVAL;
1176}
1177
1178const struct address_space_operations ceph_aops = {
1179 .readpage = ceph_readpage,
1180 .readpages = ceph_readpages,
1181 .writepage = ceph_writepage,
1182 .writepages = ceph_writepages_start,
1183 .write_begin = ceph_write_begin,
1184 .write_end = ceph_write_end,
1185 .set_page_dirty = ceph_set_page_dirty,
1186 .invalidatepage = ceph_invalidatepage,
1187 .releasepage = ceph_releasepage,
1188 .direct_IO = ceph_direct_io,
1189};
1190
1191
1192/*
1193 * vm ops
1194 */
1195
1196/*
1197 * Reuse write_begin here for simplicity.
1198 */
1199static int ceph_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
1200{
496ad9aa 1201 struct inode *inode = file_inode(vma->vm_file);
1d3576fd 1202 struct page *page = vmf->page;
3d14c5d2 1203 struct ceph_mds_client *mdsc = ceph_inode_to_client(inode)->mdsc;
6285bc23 1204 loff_t off = page_offset(page);
1d3576fd 1205 loff_t size, len;
1d3576fd
SW
1206 int ret;
1207
3ca9c3bd
JK
1208 /* Update time before taking page lock */
1209 file_update_time(vma->vm_file);
1210
1d3576fd
SW
1211 size = i_size_read(inode);
1212 if (off + PAGE_CACHE_SIZE <= size)
1213 len = PAGE_CACHE_SIZE;
1214 else
1215 len = size & ~PAGE_CACHE_MASK;
1216
1217 dout("page_mkwrite %p %llu~%llu page %p idx %lu\n", inode,
1218 off, len, page, page->index);
4af6b225
YS
1219
1220 lock_page(page);
1221
1222 ret = VM_FAULT_NOPAGE;
1223 if ((off > size) ||
1224 (page->mapping != inode->i_mapping))
1225 goto out;
1226
1227 ret = ceph_update_writeable_page(vma->vm_file, off, len, page);
1228 if (ret == 0) {
1229 /* success. we'll keep the page locked. */
1d3576fd
SW
1230 set_page_dirty(page);
1231 up_read(&mdsc->snap_rwsem);
1d3576fd
SW
1232 ret = VM_FAULT_LOCKED;
1233 } else {
4af6b225
YS
1234 if (ret == -ENOMEM)
1235 ret = VM_FAULT_OOM;
1236 else
1237 ret = VM_FAULT_SIGBUS;
1d3576fd 1238 }
4af6b225 1239out:
1d3576fd 1240 dout("page_mkwrite %p %llu~%llu = %d\n", inode, off, len, ret);
4af6b225
YS
1241 if (ret != VM_FAULT_LOCKED)
1242 unlock_page(page);
1d3576fd
SW
1243 return ret;
1244}
1245
1246static struct vm_operations_struct ceph_vmops = {
1247 .fault = filemap_fault,
1248 .page_mkwrite = ceph_page_mkwrite,
0b173bc4 1249 .remap_pages = generic_file_remap_pages,
1d3576fd
SW
1250};
1251
1252int ceph_mmap(struct file *file, struct vm_area_struct *vma)
1253{
1254 struct address_space *mapping = file->f_mapping;
1255
1256 if (!mapping->a_ops->readpage)
1257 return -ENOEXEC;
1258 file_accessed(file);
1259 vma->vm_ops = &ceph_vmops;
1d3576fd
SW
1260 return 0;
1261}
This page took 0.251895 seconds and 5 git commands to generate.