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