1 #include "ceph_debug.h"
3 #include <linux/backing-dev.h>
6 #include <linux/pagemap.h>
7 #include <linux/writeback.h> /* generic_writepages */
8 #include <linux/pagevec.h>
9 #include <linux/task_io_accounting_ops.h>
12 #include "osd_client.h"
15 * Ceph address space ops.
17 * There are a few funny things going on here.
19 * The page->private field is used to reference a struct
20 * ceph_snap_context for _every_ dirty page. This indicates which
21 * snapshot the page was logically dirtied in, and thus which snap
22 * context needs to be associated with the osd write during writeback.
24 * Similarly, struct ceph_inode_info maintains a set of counters to
25 * count dirty pages on the inode. In the absense of snapshots,
26 * i_wrbuffer_ref == i_wrbuffer_ref_head == the dirty page count.
28 * When a snapshot is taken (that is, when the client receives
29 * notification that a snapshot was taken), each inode with caps and
30 * with dirty pages (dirty pages implies there is a cap) gets a new
31 * ceph_cap_snap in the i_cap_snaps list (which is sorted in ascending
32 * order, new snaps go to the tail). The i_wrbuffer_ref_head count is
33 * moved to capsnap->dirty. (Unless a sync write is currently in
34 * progress. In that case, the capsnap is said to be "pending", new
35 * writes cannot start, and the capsnap isn't "finalized" until the
36 * write completes (or fails) and a final size/mtime for the inode for
37 * that snap can be settled upon.) i_wrbuffer_ref_head is reset to 0.
39 * On writeback, we must submit writes to the osd IN SNAP ORDER. So,
40 * we look for the first capsnap in i_cap_snaps and write out pages in
41 * that snap context _only_. Then we move on to the next capsnap,
42 * eventually reaching the "live" or "head" context (i.e., pages that
43 * are not yet snapped) and are writing the most recently dirtied
46 * Invalidate and so forth must take care to ensure the dirty page
47 * accounting is preserved.
50 #define CONGESTION_ON_THRESH(congestion_kb) (congestion_kb >> (PAGE_SHIFT-10))
51 #define CONGESTION_OFF_THRESH(congestion_kb) \
52 (CONGESTION_ON_THRESH(congestion_kb) - \
53 (CONGESTION_ON_THRESH(congestion_kb) >> 2))
58 * Dirty a page. Optimistically adjust accounting, on the assumption
59 * that we won't race with invalidate. If we do, readjust.
61 static int ceph_set_page_dirty(struct page *page)
63 struct address_space *mapping = page->mapping;
65 struct ceph_inode_info *ci;
67 struct ceph_snap_context *snapc;
69 if (unlikely(!mapping))
70 return !TestSetPageDirty(page);
72 if (TestSetPageDirty(page)) {
73 dout("%p set_page_dirty %p idx %lu -- already dirty\n",
74 mapping->host, page, page->index);
78 inode = mapping->host;
79 ci = ceph_inode(inode);
82 * Note that we're grabbing a snapc ref here without holding
85 snapc = ceph_get_snap_context(ci->i_snap_realm->cached_context);
88 spin_lock(&inode->i_lock);
89 if (ci->i_wrbuffer_ref_head == 0)
90 ci->i_head_snapc = ceph_get_snap_context(snapc);
91 ++ci->i_wrbuffer_ref_head;
92 if (ci->i_wrbuffer_ref == 0)
95 dout("%p set_page_dirty %p idx %lu head %d/%d -> %d/%d "
96 "snapc %p seq %lld (%d snaps)\n",
97 mapping->host, page, page->index,
98 ci->i_wrbuffer_ref-1, ci->i_wrbuffer_ref_head-1,
99 ci->i_wrbuffer_ref, ci->i_wrbuffer_ref_head,
100 snapc, snapc->seq, snapc->num_snaps);
101 spin_unlock(&inode->i_lock);
103 /* now adjust page */
104 spin_lock_irq(&mapping->tree_lock);
105 if (page->mapping) { /* Race with truncate? */
106 WARN_ON_ONCE(!PageUptodate(page));
108 if (mapping_cap_account_dirty(mapping)) {
109 __inc_zone_page_state(page, NR_FILE_DIRTY);
110 __inc_bdi_stat(mapping->backing_dev_info,
112 task_io_account_write(PAGE_CACHE_SIZE);
114 radix_tree_tag_set(&mapping->page_tree,
115 page_index(page), PAGECACHE_TAG_DIRTY);
118 * Reference snap context in page->private. Also set
119 * PagePrivate so that we get invalidatepage callback.
121 page->private = (unsigned long)snapc;
122 SetPagePrivate(page);
124 dout("ANON set_page_dirty %p (raced truncate?)\n", page);
128 spin_unlock_irq(&mapping->tree_lock);
131 /* whoops, we failed to dirty the page */
132 ceph_put_wrbuffer_cap_refs(ci, 1, snapc);
134 __mark_inode_dirty(mapping->host, I_DIRTY_PAGES);
136 BUG_ON(!PageDirty(page));
141 * If we are truncating the full page (i.e. offset == 0), adjust the
142 * dirty page counters appropriately. Only called if there is private
145 static void ceph_invalidatepage(struct page *page, unsigned long offset)
148 struct ceph_inode_info *ci;
149 struct ceph_snap_context *snapc = (void *)page->private;
151 BUG_ON(!PageLocked(page));
152 BUG_ON(!page->private);
153 BUG_ON(!PagePrivate(page));
154 BUG_ON(!page->mapping);
156 inode = page->mapping->host;
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.
163 if (!PageDirty(page))
164 pr_err("%p invalidatepage %p page not dirty\n", inode, page);
167 ClearPageChecked(page);
169 ci = ceph_inode(inode);
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);
176 ClearPagePrivate(page);
178 dout("%p invalidatepage %p idx %lu partial dirty page\n",
179 inode, page, page->index);
183 /* just a sanity check */
184 static int ceph_releasepage(struct page *page, gfp_t g)
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(page->private);
190 WARN_ON(PagePrivate(page));
195 * read a single page, without unlocking it.
197 static int readpage_nounlock(struct file *filp, struct page *page)
199 struct inode *inode = filp->f_dentry->d_inode;
200 struct ceph_inode_info *ci = ceph_inode(inode);
201 struct ceph_osd_client *osdc = &ceph_inode_to_client(inode)->osdc;
203 u64 len = PAGE_CACHE_SIZE;
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 page->index << PAGE_CACHE_SHIFT, &len,
209 ci->i_truncate_seq, ci->i_truncate_size,
216 } else if (err < PAGE_CACHE_SIZE) {
217 /* zero fill remainder of page */
218 zero_user_segment(page, err, PAGE_CACHE_SIZE);
220 SetPageUptodate(page);
223 return err < 0 ? err : 0;
226 static int ceph_readpage(struct file *filp, struct page *page)
228 int r = readpage_nounlock(filp, page);
234 * Build a vector of contiguous pages from the provided page list.
236 static struct page **page_vector_from_list(struct list_head *page_list,
241 int next_index, contig_pages = 0;
243 /* build page vector */
244 pages = kmalloc(sizeof(*pages) * *nr_pages, GFP_NOFS);
246 return ERR_PTR(-ENOMEM);
248 BUG_ON(list_empty(page_list));
249 next_index = list_entry(page_list->prev, struct page, lru)->index;
250 list_for_each_entry_reverse(page, page_list, lru) {
251 if (page->index == next_index) {
252 dout("readpages page %d %p\n", contig_pages, page);
253 pages[contig_pages] = page;
260 *nr_pages = contig_pages;
265 * Read multiple pages. Leave pages we don't read + unlock in page_list;
266 * the caller (VM) cleans them up.
268 static int ceph_readpages(struct file *file, struct address_space *mapping,
269 struct list_head *page_list, unsigned nr_pages)
271 struct inode *inode = file->f_dentry->d_inode;
272 struct ceph_inode_info *ci = ceph_inode(inode);
273 struct ceph_osd_client *osdc = &ceph_inode_to_client(inode)->osdc;
280 dout("readpages %p file %p nr_pages %d\n",
281 inode, file, nr_pages);
283 pages = page_vector_from_list(page_list, &nr_pages);
285 return PTR_ERR(pages);
287 /* guess read extent */
288 offset = pages[0]->index << PAGE_CACHE_SHIFT;
289 len = nr_pages << PAGE_CACHE_SHIFT;
290 rc = ceph_osdc_readpages(osdc, ceph_vino(inode), &ci->i_layout,
292 ci->i_truncate_seq, ci->i_truncate_size,
299 /* set uptodate and add to lru in pagevec-sized chunks */
300 pagevec_init(&pvec, 0);
301 for (; !list_empty(page_list) && len > 0;
302 rc -= PAGE_CACHE_SIZE, len -= PAGE_CACHE_SIZE) {
304 list_entry(page_list->prev, struct page, lru);
306 list_del(&page->lru);
308 if (rc < (int)PAGE_CACHE_SIZE) {
309 /* zero (remainder of) page */
310 int s = rc < 0 ? 0 : rc;
311 zero_user_segment(page, s, PAGE_CACHE_SIZE);
314 if (add_to_page_cache(page, mapping, page->index, GFP_NOFS)) {
315 page_cache_release(page);
316 dout("readpages %p add_to_page_cache failed %p\n",
320 dout("readpages %p adding %p idx %lu\n", inode, page,
322 flush_dcache_page(page);
323 SetPageUptodate(page);
325 if (pagevec_add(&pvec, page) == 0)
326 pagevec_lru_add_file(&pvec); /* add to lru */
328 pagevec_lru_add_file(&pvec);
337 * Get ref for the oldest snapc for an inode with dirty data... that is, the
338 * only snap context we are allowed to write back.
340 static struct ceph_snap_context *get_oldest_context(struct inode *inode,
343 struct ceph_inode_info *ci = ceph_inode(inode);
344 struct ceph_snap_context *snapc = NULL;
345 struct ceph_cap_snap *capsnap = NULL;
347 spin_lock(&inode->i_lock);
348 list_for_each_entry(capsnap, &ci->i_cap_snaps, ci_item) {
349 dout(" cap_snap %p snapc %p has %d dirty pages\n", capsnap,
350 capsnap->context, capsnap->dirty_pages);
351 if (capsnap->dirty_pages) {
352 snapc = ceph_get_snap_context(capsnap->context);
354 *snap_size = capsnap->size;
358 if (!snapc && ci->i_head_snapc) {
359 snapc = ceph_get_snap_context(ci->i_head_snapc);
360 dout(" head snapc %p has %d dirty pages\n",
361 snapc, ci->i_wrbuffer_ref_head);
363 spin_unlock(&inode->i_lock);
368 * Write a single page, but leave the page locked.
370 * If we get a write error, set the page error bit, but still adjust the
371 * dirty page accounting (i.e., page is no longer dirty).
373 static int writepage_nounlock(struct page *page, struct writeback_control *wbc)
376 struct ceph_inode_info *ci;
377 struct ceph_client *client;
378 struct ceph_osd_client *osdc;
379 loff_t page_off = page->index << PAGE_CACHE_SHIFT;
380 int len = PAGE_CACHE_SIZE;
383 struct ceph_snap_context *snapc, *oldest;
387 dout("writepage %p idx %lu\n", page, page->index);
389 if (!page->mapping || !page->mapping->host) {
390 dout("writepage %p - no mapping\n", page);
393 inode = page->mapping->host;
394 ci = ceph_inode(inode);
395 client = ceph_inode_to_client(inode);
396 osdc = &client->osdc;
398 /* verify this is a writeable snap context */
399 snapc = (void *)page->private;
401 dout("writepage %p page %p not dirty?\n", inode, page);
404 oldest = get_oldest_context(inode, &snap_size);
405 if (snapc->seq > oldest->seq) {
406 dout("writepage %p page %p snapc %p not writeable - noop\n",
407 inode, page, (void *)page->private);
408 /* we should only noop if called by kswapd */
409 WARN_ON((current->flags & PF_MEMALLOC) == 0);
410 ceph_put_snap_context(oldest);
413 ceph_put_snap_context(oldest);
415 /* is this a partial page at end of file? */
419 i_size = i_size_read(inode);
420 if (i_size < page_off + len)
421 len = i_size - page_off;
423 dout("writepage %p page %p index %lu on %llu~%u\n",
424 inode, page, page->index, page_off, len);
426 writeback_stat = atomic_long_inc_return(&client->writeback_count);
428 CONGESTION_ON_THRESH(client->mount_args->congestion_kb))
429 set_bdi_congested(&client->backing_dev_info, BLK_RW_ASYNC);
431 set_page_writeback(page);
432 err = ceph_osdc_writepages(osdc, ceph_vino(inode),
433 &ci->i_layout, snapc,
435 ci->i_truncate_seq, ci->i_truncate_size,
437 &page, 1, 0, 0, true);
439 dout("writepage setting page/mapping error %d %p\n", err, page);
441 mapping_set_error(&inode->i_data, err);
443 wbc->pages_skipped++;
445 dout("writepage cleaned page %p\n", page);
446 err = 0; /* vfs expects us to return 0 */
449 ClearPagePrivate(page);
450 end_page_writeback(page);
451 ceph_put_wrbuffer_cap_refs(ci, 1, snapc);
452 ceph_put_snap_context(snapc); /* page's reference */
457 static int ceph_writepage(struct page *page, struct writeback_control *wbc)
460 struct inode *inode = page->mapping->host;
463 err = writepage_nounlock(page, wbc);
471 * lame release_pages helper. release_pages() isn't exported to
474 static void ceph_release_pages(struct page **pages, int num)
479 pagevec_init(&pvec, 0);
480 for (i = 0; i < num; i++) {
481 if (pagevec_add(&pvec, pages[i]) == 0)
482 pagevec_release(&pvec);
484 pagevec_release(&pvec);
489 * async writeback completion handler.
491 * If we get an error, set the mapping error bit, but not the individual
494 static void writepages_finish(struct ceph_osd_request *req,
495 struct ceph_msg *msg)
497 struct inode *inode = req->r_inode;
498 struct ceph_osd_reply_head *replyhead;
499 struct ceph_osd_op *op;
500 struct ceph_inode_info *ci = ceph_inode(inode);
504 struct ceph_snap_context *snapc = req->r_snapc;
505 struct address_space *mapping = inode->i_mapping;
506 struct writeback_control *wbc = req->r_wbc;
509 struct ceph_client *client = ceph_inode_to_client(inode);
511 unsigned issued = __ceph_caps_issued(ci, NULL);
514 replyhead = msg->front.iov_base;
515 WARN_ON(le32_to_cpu(replyhead->num_ops) == 0);
516 op = (void *)(replyhead + 1);
517 rc = le32_to_cpu(replyhead->result);
518 bytes = le64_to_cpu(op->extent.length);
522 * Assume we wrote the pages we originally sent. The
523 * osd might reply with fewer pages if our writeback
524 * raced with a truncation and was adjusted at the osd,
525 * so don't believe the reply.
527 wrote = req->r_num_pages;
530 mapping_set_error(mapping, rc);
532 dout("writepages_finish %p rc %d bytes %llu wrote %d (pages)\n",
533 inode, rc, bytes, wrote);
535 /* clean all pages */
536 for (i = 0; i < req->r_num_pages; i++) {
537 page = req->r_pages[i];
539 WARN_ON(!PageUptodate(page));
542 atomic_long_dec_return(&client->writeback_count);
544 CONGESTION_OFF_THRESH(client->mount_args->congestion_kb))
545 clear_bdi_congested(&client->backing_dev_info,
549 dout("inode %p skipping page %p\n", inode, page);
550 wbc->pages_skipped++;
552 ceph_put_snap_context((void *)page->private);
554 ClearPagePrivate(page);
555 dout("unlocking %d %p\n", i, page);
556 end_page_writeback(page);
559 * We lost the cache cap, need to truncate the page before
560 * it is unlocked, otherwise we'd truncate it later in the
561 * page truncation thread, possibly losing some data that
564 if ((issued & CEPH_CAP_FILE_CACHE) == 0)
565 generic_error_remove_page(inode->i_mapping, page);
569 dout("%p wrote+cleaned %d pages\n", inode, wrote);
570 ceph_put_wrbuffer_cap_refs(ci, req->r_num_pages, snapc);
572 ceph_release_pages(req->r_pages, req->r_num_pages);
573 if (req->r_pages_from_pool)
574 mempool_free(req->r_pages,
575 ceph_client(inode->i_sb)->wb_pagevec_pool);
578 ceph_osdc_put_request(req);
582 * allocate a page vec, either directly, or if necessary, via a the
583 * mempool. we avoid the mempool if we can because req->r_num_pages
584 * may be less than the maximum write size.
586 static void alloc_page_vec(struct ceph_client *client,
587 struct ceph_osd_request *req)
589 req->r_pages = kmalloc(sizeof(struct page *) * req->r_num_pages,
592 req->r_pages = mempool_alloc(client->wb_pagevec_pool, GFP_NOFS);
593 req->r_pages_from_pool = 1;
594 WARN_ON(!req->r_pages);
599 * initiate async writeback
601 static int ceph_writepages_start(struct address_space *mapping,
602 struct writeback_control *wbc)
604 struct inode *inode = mapping->host;
605 struct backing_dev_info *bdi = mapping->backing_dev_info;
606 struct ceph_inode_info *ci = ceph_inode(inode);
607 struct ceph_client *client;
608 pgoff_t index, start, end;
611 pgoff_t max_pages = 0, max_pages_ever = 0;
612 struct ceph_snap_context *snapc = NULL, *last_snapc = NULL, *pgsnapc;
616 unsigned wsize = 1 << inode->i_blkbits;
617 struct ceph_osd_request *req = NULL;
622 * Include a 'sync' in the OSD request if this is a data
623 * integrity write (e.g., O_SYNC write or fsync()), or if our
624 * cap is being revoked.
626 do_sync = wbc->sync_mode == WB_SYNC_ALL;
627 if (ceph_caps_revoking(ci, CEPH_CAP_FILE_BUFFER))
629 dout("writepages_start %p dosync=%d (mode=%s)\n",
631 wbc->sync_mode == WB_SYNC_NONE ? "NONE" :
632 (wbc->sync_mode == WB_SYNC_ALL ? "ALL" : "HOLD"));
634 client = ceph_inode_to_client(inode);
635 if (client->mount_state == CEPH_MOUNT_SHUTDOWN) {
636 pr_warning("writepage_start %p on forced umount\n", inode);
637 return -EIO; /* we're in a forced umount, don't write! */
639 if (client->mount_args->wsize && client->mount_args->wsize < wsize)
640 wsize = client->mount_args->wsize;
641 if (wsize < PAGE_CACHE_SIZE)
642 wsize = PAGE_CACHE_SIZE;
643 max_pages_ever = wsize >> PAGE_CACHE_SHIFT;
645 pagevec_init(&pvec, 0);
648 if (wbc->nonblocking && bdi_write_congested(bdi)) {
649 dout(" writepages congested\n");
650 wbc->encountered_congestion = 1;
654 /* where to start/end? */
655 if (wbc->range_cyclic) {
656 start = mapping->writeback_index; /* Start from prev offset */
658 dout(" cyclic, start at %lu\n", start);
660 start = wbc->range_start >> PAGE_CACHE_SHIFT;
661 end = wbc->range_end >> PAGE_CACHE_SHIFT;
662 if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX)
665 dout(" not cyclic, %lu to %lu\n", start, end);
670 /* find oldest snap context with dirty data */
671 ceph_put_snap_context(snapc);
672 snapc = get_oldest_context(inode, &snap_size);
674 /* hmm, why does writepages get called when there
676 dout(" no snap context with dirty data?\n");
679 dout(" oldest snapc is %p seq %lld (%d snaps)\n",
680 snapc, snapc->seq, snapc->num_snaps);
681 if (last_snapc && snapc != last_snapc) {
682 /* if we switched to a newer snapc, restart our scan at the
683 * start of the original file range. */
684 dout(" snapc differs from last pass, restarting at %lu\n",
690 while (!done && index <= end) {
694 int pvec_pages, locked_pages;
698 struct ceph_osd_request_head *reqhead;
699 struct ceph_osd_op *op;
704 max_pages = max_pages_ever;
708 want = min(end - index,
709 min((pgoff_t)PAGEVEC_SIZE,
710 max_pages - (pgoff_t)locked_pages) - 1)
712 pvec_pages = pagevec_lookup_tag(&pvec, mapping, &index,
715 dout("pagevec_lookup_tag got %d\n", pvec_pages);
716 if (!pvec_pages && !locked_pages)
718 for (i = 0; i < pvec_pages && locked_pages < max_pages; i++) {
719 page = pvec.pages[i];
720 dout("? %p idx %lu\n", page, page->index);
721 if (locked_pages == 0)
722 lock_page(page); /* first page */
723 else if (!trylock_page(page))
726 /* only dirty pages, or our accounting breaks */
727 if (unlikely(!PageDirty(page)) ||
728 unlikely(page->mapping != mapping)) {
729 dout("!dirty or !mapping %p\n", page);
733 if (!wbc->range_cyclic && page->index > end) {
734 dout("end of range %p\n", page);
739 if (next && (page->index != next)) {
740 dout("not consecutive %p\n", page);
744 if (wbc->sync_mode != WB_SYNC_NONE) {
745 dout("waiting on writeback %p\n", page);
746 wait_on_page_writeback(page);
748 if ((snap_size && page_offset(page) > snap_size) ||
750 page_offset(page) > i_size_read(inode))) {
751 dout("%p page eof %llu\n", page, snap_size ?
752 snap_size : i_size_read(inode));
757 if (PageWriteback(page)) {
758 dout("%p under writeback\n", page);
763 /* only if matching snap context */
764 pgsnapc = (void *)page->private;
765 if (pgsnapc->seq > snapc->seq) {
766 dout("page snapc %p %lld > oldest %p %lld\n",
767 pgsnapc, pgsnapc->seq, snapc, snapc->seq);
770 continue; /* keep looking for snap */
774 if (!clear_page_dirty_for_io(page)) {
775 dout("%p !clear_page_dirty_for_io\n", page);
781 if (locked_pages == 0) {
782 /* prepare async write request */
783 offset = page->index << PAGE_CACHE_SHIFT;
785 req = ceph_osdc_new_request(&client->osdc,
790 CEPH_OSD_FLAG_WRITE |
791 CEPH_OSD_FLAG_ONDISK,
795 &inode->i_mtime, true, 1);
796 max_pages = req->r_num_pages;
798 alloc_page_vec(client, req);
799 req->r_callback = writepages_finish;
800 req->r_inode = inode;
804 /* note position of first page in pvec */
807 dout("%p will write page %p idx %lu\n",
808 inode, page, page->index);
810 writeback_stat = atomic_long_inc_return(&client->writeback_count);
811 if (writeback_stat > CONGESTION_ON_THRESH(client->mount_args->congestion_kb)) {
812 set_bdi_congested(&client->backing_dev_info, BLK_RW_ASYNC);
815 set_page_writeback(page);
816 req->r_pages[locked_pages] = page;
818 next = page->index + 1;
821 /* did we get anything? */
823 goto release_pvec_pages;
826 BUG_ON(!locked_pages || first < 0);
828 if (pvec_pages && i == pvec_pages &&
829 locked_pages < max_pages) {
830 dout("reached end pvec, trying for more\n");
831 pagevec_reinit(&pvec);
835 /* shift unused pages over in the pvec... we
836 * will need to release them below. */
837 for (j = i; j < pvec_pages; j++) {
838 dout(" pvec leftover page %p\n",
840 pvec.pages[j-i+first] = pvec.pages[j];
845 /* submit the write */
846 offset = req->r_pages[0]->index << PAGE_CACHE_SHIFT;
847 len = min((snap_size ? snap_size : i_size_read(inode)) - offset,
848 (u64)locked_pages << PAGE_CACHE_SHIFT);
849 dout("writepages got %d pages at %llu~%llu\n",
850 locked_pages, offset, len);
852 /* revise final length, page count */
853 req->r_num_pages = locked_pages;
854 reqhead = req->r_request->front.iov_base;
855 op = (void *)(reqhead + 1);
856 op->extent.length = cpu_to_le64(len);
857 op->payload_len = cpu_to_le32(len);
858 req->r_request->hdr.data_len = cpu_to_le32(len);
860 ceph_osdc_start_request(&client->osdc, req, true);
865 wbc->nr_to_write -= locked_pages;
866 if (wbc->nr_to_write <= 0)
870 dout("pagevec_release on %d pages (%p)\n", (int)pvec.nr,
871 pvec.nr ? pvec.pages[0] : NULL);
872 pagevec_release(&pvec);
874 if (locked_pages && !done)
878 if (should_loop && !done) {
879 /* more to do; loop back to beginning of file */
880 dout("writepages looping back to beginning of file\n");
886 if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0))
887 mapping->writeback_index = index;
891 ceph_osdc_put_request(req);
893 rc = 0; /* vfs expects us to return 0 */
894 ceph_put_snap_context(snapc);
895 dout("writepages done, rc = %d\n", rc);
903 * See if a given @snapc is either writeable, or already written.
905 static int context_is_writeable_or_written(struct inode *inode,
906 struct ceph_snap_context *snapc)
908 struct ceph_snap_context *oldest = get_oldest_context(inode, NULL);
909 int ret = !oldest || snapc->seq <= oldest->seq;
911 ceph_put_snap_context(oldest);
916 * We are only allowed to write into/dirty the page if the page is
917 * clean, or already dirty within the same snap context.
919 * called with page locked.
920 * return success with page locked,
921 * or any failure (incl -EAGAIN) with page unlocked.
923 static int ceph_update_writeable_page(struct file *file,
924 loff_t pos, unsigned len,
927 struct inode *inode = file->f_dentry->d_inode;
928 struct ceph_inode_info *ci = ceph_inode(inode);
929 struct ceph_mds_client *mdsc = &ceph_inode_to_client(inode)->mdsc;
930 loff_t page_off = pos & PAGE_CACHE_MASK;
931 int pos_in_page = pos & ~PAGE_CACHE_MASK;
932 int end_in_page = pos_in_page + len;
935 struct ceph_snap_context *snapc, *oldest;
938 /* writepages currently holds page lock, but if we change that later, */
939 wait_on_page_writeback(page);
941 /* check snap context */
942 BUG_ON(!ci->i_snap_realm);
943 down_read(&mdsc->snap_rwsem);
944 BUG_ON(!ci->i_snap_realm->cached_context);
945 snapc = (void *)page->private;
946 if (snapc && snapc != ci->i_head_snapc) {
948 * this page is already dirty in another (older) snap
949 * context! is it writeable now?
951 oldest = get_oldest_context(inode, NULL);
952 up_read(&mdsc->snap_rwsem);
954 if (snapc->seq > oldest->seq) {
955 ceph_put_snap_context(oldest);
956 dout(" page %p snapc %p not current or oldest\n",
959 * queue for writeback, and wait for snapc to
960 * be writeable or written
962 snapc = ceph_get_snap_context(snapc);
964 ceph_queue_writeback(inode);
965 r = wait_event_interruptible(ci->i_cap_wq,
966 context_is_writeable_or_written(inode, snapc));
967 ceph_put_snap_context(snapc);
968 if (r == -ERESTARTSYS)
972 ceph_put_snap_context(oldest);
974 /* yay, writeable, do it now (without dropping page lock) */
975 dout(" page %p snapc %p not current, but oldest\n",
977 if (!clear_page_dirty_for_io(page))
979 r = writepage_nounlock(page, NULL);
985 if (PageUptodate(page)) {
986 dout(" page %p already uptodate\n", page);
991 if (pos_in_page == 0 && len == PAGE_CACHE_SIZE)
994 /* past end of file? */
995 i_size = inode->i_size; /* caller holds i_mutex */
997 if (i_size + len > inode->i_sb->s_maxbytes) {
998 /* file is too big */
1003 if (page_off >= i_size ||
1004 (pos_in_page == 0 && (pos+len) >= i_size &&
1005 end_in_page - pos_in_page != PAGE_CACHE_SIZE)) {
1006 dout(" zeroing %p 0 - %d and %d - %d\n",
1007 page, pos_in_page, end_in_page, (int)PAGE_CACHE_SIZE);
1008 zero_user_segments(page,
1010 end_in_page, PAGE_CACHE_SIZE);
1014 /* we need to read it. */
1015 up_read(&mdsc->snap_rwsem);
1016 r = readpage_nounlock(file, page);
1022 up_read(&mdsc->snap_rwsem);
1029 * We are only allowed to write into/dirty the page if the page is
1030 * clean, or already dirty within the same snap context.
1032 static int ceph_write_begin(struct file *file, struct address_space *mapping,
1033 loff_t pos, unsigned len, unsigned flags,
1034 struct page **pagep, void **fsdata)
1036 struct inode *inode = file->f_dentry->d_inode;
1038 pgoff_t index = pos >> PAGE_CACHE_SHIFT;
1043 page = grab_cache_page_write_begin(mapping, index, 0);
1048 dout("write_begin file %p inode %p page %p %d~%d\n", file,
1049 inode, page, (int)pos, (int)len);
1051 r = ceph_update_writeable_page(file, pos, len, page);
1052 } while (r == -EAGAIN);
1058 * we don't do anything in here that simple_write_end doesn't do
1059 * except adjust dirty page accounting and drop read lock on
1062 static int ceph_write_end(struct file *file, struct address_space *mapping,
1063 loff_t pos, unsigned len, unsigned copied,
1064 struct page *page, void *fsdata)
1066 struct inode *inode = file->f_dentry->d_inode;
1067 struct ceph_client *client = ceph_inode_to_client(inode);
1068 struct ceph_mds_client *mdsc = &client->mdsc;
1069 unsigned from = pos & (PAGE_CACHE_SIZE - 1);
1072 dout("write_end file %p inode %p page %p %d~%d (%d)\n", file,
1073 inode, page, (int)pos, (int)copied, (int)len);
1075 /* zero the stale part of the page if we did a short copy */
1077 zero_user_segment(page, from+copied, len);
1079 /* did file size increase? */
1080 /* (no need for i_size_read(); we caller holds i_mutex */
1081 if (pos+copied > inode->i_size)
1082 check_cap = ceph_inode_set_size(inode, pos+copied);
1084 if (!PageUptodate(page))
1085 SetPageUptodate(page);
1087 set_page_dirty(page);
1090 up_read(&mdsc->snap_rwsem);
1091 page_cache_release(page);
1094 ceph_check_caps(ceph_inode(inode), CHECK_CAPS_AUTHONLY, NULL);
1100 * we set .direct_IO to indicate direct io is supported, but since we
1101 * intercept O_DIRECT reads and writes early, this function should
1104 static ssize_t ceph_direct_io(int rw, struct kiocb *iocb,
1105 const struct iovec *iov,
1106 loff_t pos, unsigned long nr_segs)
1112 const struct address_space_operations ceph_aops = {
1113 .readpage = ceph_readpage,
1114 .readpages = ceph_readpages,
1115 .writepage = ceph_writepage,
1116 .writepages = ceph_writepages_start,
1117 .write_begin = ceph_write_begin,
1118 .write_end = ceph_write_end,
1119 .set_page_dirty = ceph_set_page_dirty,
1120 .invalidatepage = ceph_invalidatepage,
1121 .releasepage = ceph_releasepage,
1122 .direct_IO = ceph_direct_io,
1131 * Reuse write_begin here for simplicity.
1133 static int ceph_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
1135 struct inode *inode = vma->vm_file->f_dentry->d_inode;
1136 struct page *page = vmf->page;
1137 struct ceph_mds_client *mdsc = &ceph_inode_to_client(inode)->mdsc;
1138 loff_t off = page->index << PAGE_CACHE_SHIFT;
1142 size = i_size_read(inode);
1143 if (off + PAGE_CACHE_SIZE <= size)
1144 len = PAGE_CACHE_SIZE;
1146 len = size & ~PAGE_CACHE_MASK;
1148 dout("page_mkwrite %p %llu~%llu page %p idx %lu\n", inode,
1149 off, len, page, page->index);
1153 ret = VM_FAULT_NOPAGE;
1155 (page->mapping != inode->i_mapping))
1158 ret = ceph_update_writeable_page(vma->vm_file, off, len, page);
1160 /* success. we'll keep the page locked. */
1161 set_page_dirty(page);
1162 up_read(&mdsc->snap_rwsem);
1163 ret = VM_FAULT_LOCKED;
1168 ret = VM_FAULT_SIGBUS;
1171 dout("page_mkwrite %p %llu~%llu = %d\n", inode, off, len, ret);
1172 if (ret != VM_FAULT_LOCKED)
1177 static struct vm_operations_struct ceph_vmops = {
1178 .fault = filemap_fault,
1179 .page_mkwrite = ceph_page_mkwrite,
1182 int ceph_mmap(struct file *file, struct vm_area_struct *vma)
1184 struct address_space *mapping = file->f_mapping;
1186 if (!mapping->a_ops->readpage)
1188 file_accessed(file);
1189 vma->vm_ops = &ceph_vmops;
1190 vma->vm_flags |= VM_CAN_NONLINEAR;