1 #include <linux/ceph/ceph_debug.h>
3 #include <linux/backing-dev.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>
13 #include "mds_client.h"
14 #include <linux/ceph/osd_client.h>
17 * Ceph address space ops.
19 * There are a few funny things going on here.
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.
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.
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.
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
48 * Invalidate and so forth must take care to ensure the dirty page
49 * accounting is preserved.
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))
60 * Dirty a page. Optimistically adjust accounting, on the assumption
61 * that we won't race with invalidate. If we do, readjust.
63 static int ceph_set_page_dirty(struct page
*page
)
65 struct address_space
*mapping
= page
->mapping
;
67 struct ceph_inode_info
*ci
;
69 struct ceph_snap_context
*snapc
;
71 if (unlikely(!mapping
))
72 return !TestSetPageDirty(page
);
74 if (TestSetPageDirty(page
)) {
75 dout("%p set_page_dirty %p idx %lu -- already dirty\n",
76 mapping
->host
, page
, page
->index
);
80 inode
= mapping
->host
;
81 ci
= ceph_inode(inode
);
84 * Note that we're grabbing a snapc ref here without holding
87 snapc
= ceph_get_snap_context(ci
->i_snap_realm
->cached_context
);
90 spin_lock(&inode
->i_lock
);
91 if (ci
->i_head_snapc
== NULL
)
92 ci
->i_head_snapc
= ceph_get_snap_context(snapc
);
93 ++ci
->i_wrbuffer_ref_head
;
94 if (ci
->i_wrbuffer_ref
== 0)
97 dout("%p set_page_dirty %p idx %lu head %d/%d -> %d/%d "
98 "snapc %p seq %lld (%d snaps)\n",
99 mapping
->host
, page
, page
->index
,
100 ci
->i_wrbuffer_ref
-1, ci
->i_wrbuffer_ref_head
-1,
101 ci
->i_wrbuffer_ref
, ci
->i_wrbuffer_ref_head
,
102 snapc
, snapc
->seq
, snapc
->num_snaps
);
103 spin_unlock(&inode
->i_lock
);
105 /* now adjust page */
106 spin_lock_irq(&mapping
->tree_lock
);
107 if (page
->mapping
) { /* Race with truncate? */
108 WARN_ON_ONCE(!PageUptodate(page
));
109 account_page_dirtied(page
, page
->mapping
);
110 radix_tree_tag_set(&mapping
->page_tree
,
111 page_index(page
), PAGECACHE_TAG_DIRTY
);
114 * Reference snap context in page->private. Also set
115 * PagePrivate so that we get invalidatepage callback.
117 page
->private = (unsigned long)snapc
;
118 SetPagePrivate(page
);
120 dout("ANON set_page_dirty %p (raced truncate?)\n", page
);
124 spin_unlock_irq(&mapping
->tree_lock
);
127 /* whoops, we failed to dirty the page */
128 ceph_put_wrbuffer_cap_refs(ci
, 1, snapc
);
130 __mark_inode_dirty(mapping
->host
, I_DIRTY_PAGES
);
132 BUG_ON(!PageDirty(page
));
137 * If we are truncating the full page (i.e. offset == 0), adjust the
138 * dirty page counters appropriately. Only called if there is private
141 static void ceph_invalidatepage(struct page
*page
, unsigned long offset
)
144 struct ceph_inode_info
*ci
;
145 struct ceph_snap_context
*snapc
= (void *)page
->private;
147 BUG_ON(!PageLocked(page
));
148 BUG_ON(!page
->private);
149 BUG_ON(!PagePrivate(page
));
150 BUG_ON(!page
->mapping
);
152 inode
= page
->mapping
->host
;
155 * We can get non-dirty pages here due to races between
156 * set_page_dirty and truncate_complete_page; just spit out a
157 * warning, in case we end up with accounting problems later.
159 if (!PageDirty(page
))
160 pr_err("%p invalidatepage %p page not dirty\n", inode
, page
);
163 ClearPageChecked(page
);
165 ci
= ceph_inode(inode
);
167 dout("%p invalidatepage %p idx %lu full dirty page %lu\n",
168 inode
, page
, page
->index
, offset
);
169 ceph_put_wrbuffer_cap_refs(ci
, 1, snapc
);
170 ceph_put_snap_context(snapc
);
172 ClearPagePrivate(page
);
174 dout("%p invalidatepage %p idx %lu partial dirty page\n",
175 inode
, page
, page
->index
);
179 /* just a sanity check */
180 static int ceph_releasepage(struct page
*page
, gfp_t g
)
182 struct inode
*inode
= page
->mapping
? page
->mapping
->host
: NULL
;
183 dout("%p releasepage %p idx %lu\n", inode
, page
, page
->index
);
184 WARN_ON(PageDirty(page
));
185 WARN_ON(page
->private);
186 WARN_ON(PagePrivate(page
));
191 * read a single page, without unlocking it.
193 static int readpage_nounlock(struct file
*filp
, struct page
*page
)
195 struct inode
*inode
= filp
->f_dentry
->d_inode
;
196 struct ceph_inode_info
*ci
= ceph_inode(inode
);
197 struct ceph_osd_client
*osdc
=
198 &ceph_inode_to_client(inode
)->client
->osdc
;
200 u64 len
= PAGE_CACHE_SIZE
;
202 dout("readpage inode %p file %p page %p index %lu\n",
203 inode
, filp
, page
, page
->index
);
204 err
= ceph_osdc_readpages(osdc
, ceph_vino(inode
), &ci
->i_layout
,
205 page
->index
<< PAGE_CACHE_SHIFT
, &len
,
206 ci
->i_truncate_seq
, ci
->i_truncate_size
,
213 } else if (err
< PAGE_CACHE_SIZE
) {
214 /* zero fill remainder of page */
215 zero_user_segment(page
, err
, PAGE_CACHE_SIZE
);
217 SetPageUptodate(page
);
220 return err
< 0 ? err
: 0;
223 static int ceph_readpage(struct file
*filp
, struct page
*page
)
225 int r
= readpage_nounlock(filp
, page
);
231 * Finish an async read(ahead) op.
233 static void finish_read(struct ceph_osd_request
*req
, struct ceph_msg
*msg
)
235 struct inode
*inode
= req
->r_inode
;
236 struct ceph_osd_reply_head
*replyhead
;
241 replyhead
= msg
->front
.iov_base
;
242 WARN_ON(le32_to_cpu(replyhead
->num_ops
) == 0);
243 rc
= le32_to_cpu(replyhead
->result
);
244 bytes
= le32_to_cpu(msg
->hdr
.data_len
);
246 dout("finish_read %p req %p rc %d bytes %d\n", inode
, req
, rc
, bytes
);
248 /* unlock all pages, zeroing any data we didn't read */
249 for (i
= 0; i
< req
->r_num_pages
; i
++, bytes
-= PAGE_CACHE_SIZE
) {
250 struct page
*page
= req
->r_pages
[i
];
252 if (bytes
< (int)PAGE_CACHE_SIZE
) {
253 /* zero (remainder of) page */
254 int s
= bytes
< 0 ? 0 : bytes
;
255 zero_user_segment(page
, s
, PAGE_CACHE_SIZE
);
257 dout("finish_read %p uptodate %p idx %lu\n", inode
, page
,
259 flush_dcache_page(page
);
260 SetPageUptodate(page
);
262 page_cache_release(page
);
268 * start an async read(ahead) operation. return nr_pages we submitted
269 * a read for on success, or negative error code.
271 static int start_read(struct inode
*inode
, struct list_head
*page_list
, int max
)
273 struct ceph_osd_client
*osdc
=
274 &ceph_inode_to_client(inode
)->client
->osdc
;
275 struct ceph_inode_info
*ci
= ceph_inode(inode
);
276 struct page
*page
= list_entry(page_list
->prev
, struct page
, lru
);
277 struct ceph_osd_request
*req
;
286 off
= page
->index
<< PAGE_CACHE_SHIFT
;
289 next_index
= page
->index
;
290 list_for_each_entry_reverse(page
, page_list
, lru
) {
291 if (page
->index
!= next_index
)
295 if (max
&& nr_pages
== max
)
298 len
= nr_pages
<< PAGE_CACHE_SHIFT
;
299 dout("start_read %p nr_pages %d is %lld~%lld\n", inode
, nr_pages
,
302 req
= ceph_osdc_new_request(osdc
, &ci
->i_layout
, ceph_vino(inode
),
304 CEPH_OSD_OP_READ
, CEPH_OSD_FLAG_READ
,
306 ci
->i_truncate_seq
, ci
->i_truncate_size
,
311 /* build page vector */
312 nr_pages
= len
>> PAGE_CACHE_SHIFT
;
313 pages
= kmalloc(sizeof(*pages
) * nr_pages
, GFP_NOFS
);
317 for (i
= 0; i
< nr_pages
; ++i
) {
318 page
= list_entry(page_list
->prev
, struct page
, lru
);
319 BUG_ON(PageLocked(page
));
320 list_del(&page
->lru
);
322 dout("start_read %p adding %p idx %lu\n", inode
, page
,
324 if (add_to_page_cache_lru(page
, &inode
->i_data
, page
->index
,
326 page_cache_release(page
);
327 dout("start_read %p add_to_page_cache failed %p\n",
334 req
->r_pages
= pages
;
335 req
->r_num_pages
= nr_pages
;
336 req
->r_callback
= finish_read
;
337 req
->r_inode
= inode
;
339 dout("start_read %p starting %p %lld~%lld\n", inode
, req
, off
, len
);
340 ret
= ceph_osdc_start_request(osdc
, req
, false);
343 ceph_osdc_put_request(req
);
347 ceph_release_page_vector(pages
, nr_pages
);
350 ceph_osdc_put_request(req
);
356 * Read multiple pages. Leave pages we don't read + unlock in page_list;
357 * the caller (VM) cleans them up.
359 static int ceph_readpages(struct file
*file
, struct address_space
*mapping
,
360 struct list_head
*page_list
, unsigned nr_pages
)
362 struct inode
*inode
= file
->f_dentry
->d_inode
;
363 struct ceph_fs_client
*fsc
= ceph_inode_to_client(inode
);
367 if (fsc
->mount_options
->rsize
>= PAGE_CACHE_SIZE
)
368 max
= (fsc
->mount_options
->rsize
+ PAGE_CACHE_SIZE
- 1)
371 dout("readpages %p file %p nr_pages %d max %d\n", inode
, file
, nr_pages
,
373 while (!list_empty(page_list
)) {
374 rc
= start_read(inode
, page_list
, max
);
380 dout("readpages %p file %p ret %d\n", inode
, file
, rc
);
385 * Get ref for the oldest snapc for an inode with dirty data... that is, the
386 * only snap context we are allowed to write back.
388 static struct ceph_snap_context
*get_oldest_context(struct inode
*inode
,
391 struct ceph_inode_info
*ci
= ceph_inode(inode
);
392 struct ceph_snap_context
*snapc
= NULL
;
393 struct ceph_cap_snap
*capsnap
= NULL
;
395 spin_lock(&inode
->i_lock
);
396 list_for_each_entry(capsnap
, &ci
->i_cap_snaps
, ci_item
) {
397 dout(" cap_snap %p snapc %p has %d dirty pages\n", capsnap
,
398 capsnap
->context
, capsnap
->dirty_pages
);
399 if (capsnap
->dirty_pages
) {
400 snapc
= ceph_get_snap_context(capsnap
->context
);
402 *snap_size
= capsnap
->size
;
406 if (!snapc
&& ci
->i_wrbuffer_ref_head
) {
407 snapc
= ceph_get_snap_context(ci
->i_head_snapc
);
408 dout(" head snapc %p has %d dirty pages\n",
409 snapc
, ci
->i_wrbuffer_ref_head
);
411 spin_unlock(&inode
->i_lock
);
416 * Write a single page, but leave the page locked.
418 * If we get a write error, set the page error bit, but still adjust the
419 * dirty page accounting (i.e., page is no longer dirty).
421 static int writepage_nounlock(struct page
*page
, struct writeback_control
*wbc
)
424 struct ceph_inode_info
*ci
;
425 struct ceph_fs_client
*fsc
;
426 struct ceph_osd_client
*osdc
;
427 loff_t page_off
= page
->index
<< PAGE_CACHE_SHIFT
;
428 int len
= PAGE_CACHE_SIZE
;
431 struct ceph_snap_context
*snapc
, *oldest
;
435 dout("writepage %p idx %lu\n", page
, page
->index
);
437 if (!page
->mapping
|| !page
->mapping
->host
) {
438 dout("writepage %p - no mapping\n", page
);
441 inode
= page
->mapping
->host
;
442 ci
= ceph_inode(inode
);
443 fsc
= ceph_inode_to_client(inode
);
444 osdc
= &fsc
->client
->osdc
;
446 /* verify this is a writeable snap context */
447 snapc
= (void *)page
->private;
449 dout("writepage %p page %p not dirty?\n", inode
, page
);
452 oldest
= get_oldest_context(inode
, &snap_size
);
453 if (snapc
->seq
> oldest
->seq
) {
454 dout("writepage %p page %p snapc %p not writeable - noop\n",
455 inode
, page
, (void *)page
->private);
456 /* we should only noop if called by kswapd */
457 WARN_ON((current
->flags
& PF_MEMALLOC
) == 0);
458 ceph_put_snap_context(oldest
);
461 ceph_put_snap_context(oldest
);
463 /* is this a partial page at end of file? */
467 i_size
= i_size_read(inode
);
468 if (i_size
< page_off
+ len
)
469 len
= i_size
- page_off
;
471 dout("writepage %p page %p index %lu on %llu~%u snapc %p\n",
472 inode
, page
, page
->index
, page_off
, len
, snapc
);
474 writeback_stat
= atomic_long_inc_return(&fsc
->writeback_count
);
476 CONGESTION_ON_THRESH(fsc
->mount_options
->congestion_kb
))
477 set_bdi_congested(&fsc
->backing_dev_info
, BLK_RW_ASYNC
);
479 set_page_writeback(page
);
480 err
= ceph_osdc_writepages(osdc
, ceph_vino(inode
),
481 &ci
->i_layout
, snapc
,
483 ci
->i_truncate_seq
, ci
->i_truncate_size
,
485 &page
, 1, 0, 0, true);
487 dout("writepage setting page/mapping error %d %p\n", err
, page
);
489 mapping_set_error(&inode
->i_data
, err
);
491 wbc
->pages_skipped
++;
493 dout("writepage cleaned page %p\n", page
);
494 err
= 0; /* vfs expects us to return 0 */
497 ClearPagePrivate(page
);
498 end_page_writeback(page
);
499 ceph_put_wrbuffer_cap_refs(ci
, 1, snapc
);
500 ceph_put_snap_context(snapc
); /* page's reference */
505 static int ceph_writepage(struct page
*page
, struct writeback_control
*wbc
)
508 struct inode
*inode
= page
->mapping
->host
;
511 err
= writepage_nounlock(page
, wbc
);
519 * lame release_pages helper. release_pages() isn't exported to
522 static void ceph_release_pages(struct page
**pages
, int num
)
527 pagevec_init(&pvec
, 0);
528 for (i
= 0; i
< num
; i
++) {
529 if (pagevec_add(&pvec
, pages
[i
]) == 0)
530 pagevec_release(&pvec
);
532 pagevec_release(&pvec
);
537 * async writeback completion handler.
539 * If we get an error, set the mapping error bit, but not the individual
542 static void writepages_finish(struct ceph_osd_request
*req
,
543 struct ceph_msg
*msg
)
545 struct inode
*inode
= req
->r_inode
;
546 struct ceph_osd_reply_head
*replyhead
;
547 struct ceph_osd_op
*op
;
548 struct ceph_inode_info
*ci
= ceph_inode(inode
);
552 struct ceph_snap_context
*snapc
= req
->r_snapc
;
553 struct address_space
*mapping
= inode
->i_mapping
;
556 struct ceph_fs_client
*fsc
= ceph_inode_to_client(inode
);
558 unsigned issued
= ceph_caps_issued(ci
);
561 replyhead
= msg
->front
.iov_base
;
562 WARN_ON(le32_to_cpu(replyhead
->num_ops
) == 0);
563 op
= (void *)(replyhead
+ 1);
564 rc
= le32_to_cpu(replyhead
->result
);
565 bytes
= le64_to_cpu(op
->extent
.length
);
569 * Assume we wrote the pages we originally sent. The
570 * osd might reply with fewer pages if our writeback
571 * raced with a truncation and was adjusted at the osd,
572 * so don't believe the reply.
574 wrote
= req
->r_num_pages
;
577 mapping_set_error(mapping
, rc
);
579 dout("writepages_finish %p rc %d bytes %llu wrote %d (pages)\n",
580 inode
, rc
, bytes
, wrote
);
582 /* clean all pages */
583 for (i
= 0; i
< req
->r_num_pages
; i
++) {
584 page
= req
->r_pages
[i
];
586 WARN_ON(!PageUptodate(page
));
589 atomic_long_dec_return(&fsc
->writeback_count
);
591 CONGESTION_OFF_THRESH(fsc
->mount_options
->congestion_kb
))
592 clear_bdi_congested(&fsc
->backing_dev_info
,
595 ceph_put_snap_context((void *)page
->private);
597 ClearPagePrivate(page
);
598 dout("unlocking %d %p\n", i
, page
);
599 end_page_writeback(page
);
602 * We lost the cache cap, need to truncate the page before
603 * it is unlocked, otherwise we'd truncate it later in the
604 * page truncation thread, possibly losing some data that
607 if ((issued
& (CEPH_CAP_FILE_CACHE
|CEPH_CAP_FILE_LAZYIO
)) == 0)
608 generic_error_remove_page(inode
->i_mapping
, page
);
612 dout("%p wrote+cleaned %d pages\n", inode
, wrote
);
613 ceph_put_wrbuffer_cap_refs(ci
, req
->r_num_pages
, snapc
);
615 ceph_release_pages(req
->r_pages
, req
->r_num_pages
);
616 if (req
->r_pages_from_pool
)
617 mempool_free(req
->r_pages
,
618 ceph_sb_to_client(inode
->i_sb
)->wb_pagevec_pool
);
621 ceph_osdc_put_request(req
);
625 * allocate a page vec, either directly, or if necessary, via a the
626 * mempool. we avoid the mempool if we can because req->r_num_pages
627 * may be less than the maximum write size.
629 static void alloc_page_vec(struct ceph_fs_client
*fsc
,
630 struct ceph_osd_request
*req
)
632 req
->r_pages
= kmalloc(sizeof(struct page
*) * req
->r_num_pages
,
635 req
->r_pages
= mempool_alloc(fsc
->wb_pagevec_pool
, GFP_NOFS
);
636 req
->r_pages_from_pool
= 1;
637 WARN_ON(!req
->r_pages
);
642 * initiate async writeback
644 static int ceph_writepages_start(struct address_space
*mapping
,
645 struct writeback_control
*wbc
)
647 struct inode
*inode
= mapping
->host
;
648 struct ceph_inode_info
*ci
= ceph_inode(inode
);
649 struct ceph_fs_client
*fsc
;
650 pgoff_t index
, start
, end
;
653 pgoff_t max_pages
= 0, max_pages_ever
= 0;
654 struct ceph_snap_context
*snapc
= NULL
, *last_snapc
= NULL
, *pgsnapc
;
658 unsigned wsize
= 1 << inode
->i_blkbits
;
659 struct ceph_osd_request
*req
= NULL
;
664 * Include a 'sync' in the OSD request if this is a data
665 * integrity write (e.g., O_SYNC write or fsync()), or if our
666 * cap is being revoked.
668 do_sync
= wbc
->sync_mode
== WB_SYNC_ALL
;
669 if (ceph_caps_revoking(ci
, CEPH_CAP_FILE_BUFFER
))
671 dout("writepages_start %p dosync=%d (mode=%s)\n",
673 wbc
->sync_mode
== WB_SYNC_NONE
? "NONE" :
674 (wbc
->sync_mode
== WB_SYNC_ALL
? "ALL" : "HOLD"));
676 fsc
= ceph_inode_to_client(inode
);
677 if (fsc
->mount_state
== CEPH_MOUNT_SHUTDOWN
) {
678 pr_warning("writepage_start %p on forced umount\n", inode
);
679 return -EIO
; /* we're in a forced umount, don't write! */
681 if (fsc
->mount_options
->wsize
&& fsc
->mount_options
->wsize
< wsize
)
682 wsize
= fsc
->mount_options
->wsize
;
683 if (wsize
< PAGE_CACHE_SIZE
)
684 wsize
= PAGE_CACHE_SIZE
;
685 max_pages_ever
= wsize
>> PAGE_CACHE_SHIFT
;
687 pagevec_init(&pvec
, 0);
689 /* where to start/end? */
690 if (wbc
->range_cyclic
) {
691 start
= mapping
->writeback_index
; /* Start from prev offset */
693 dout(" cyclic, start at %lu\n", start
);
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
)
700 dout(" not cyclic, %lu to %lu\n", start
, end
);
705 /* find oldest snap context with dirty data */
706 ceph_put_snap_context(snapc
);
707 snapc
= get_oldest_context(inode
, &snap_size
);
709 /* hmm, why does writepages get called when there
711 dout(" no snap context with dirty data?\n");
714 dout(" oldest snapc is %p seq %lld (%d snaps)\n",
715 snapc
, snapc
->seq
, snapc
->num_snaps
);
716 if (last_snapc
&& snapc
!= last_snapc
) {
717 /* if we switched to a newer snapc, restart our scan at the
718 * start of the original file range. */
719 dout(" snapc differs from last pass, restarting at %lu\n",
725 while (!done
&& index
<= end
) {
729 int pvec_pages
, locked_pages
;
733 struct ceph_osd_request_head
*reqhead
;
734 struct ceph_osd_op
*op
;
739 max_pages
= max_pages_ever
;
743 want
= min(end
- index
,
744 min((pgoff_t
)PAGEVEC_SIZE
,
745 max_pages
- (pgoff_t
)locked_pages
) - 1)
747 pvec_pages
= pagevec_lookup_tag(&pvec
, mapping
, &index
,
750 dout("pagevec_lookup_tag got %d\n", pvec_pages
);
751 if (!pvec_pages
&& !locked_pages
)
753 for (i
= 0; i
< pvec_pages
&& locked_pages
< max_pages
; i
++) {
754 page
= pvec
.pages
[i
];
755 dout("? %p idx %lu\n", page
, page
->index
);
756 if (locked_pages
== 0)
757 lock_page(page
); /* first page */
758 else if (!trylock_page(page
))
761 /* only dirty pages, or our accounting breaks */
762 if (unlikely(!PageDirty(page
)) ||
763 unlikely(page
->mapping
!= mapping
)) {
764 dout("!dirty or !mapping %p\n", page
);
768 if (!wbc
->range_cyclic
&& page
->index
> end
) {
769 dout("end of range %p\n", page
);
774 if (next
&& (page
->index
!= next
)) {
775 dout("not consecutive %p\n", page
);
779 if (wbc
->sync_mode
!= WB_SYNC_NONE
) {
780 dout("waiting on writeback %p\n", page
);
781 wait_on_page_writeback(page
);
783 if ((snap_size
&& page_offset(page
) > snap_size
) ||
785 page_offset(page
) > i_size_read(inode
))) {
786 dout("%p page eof %llu\n", page
, snap_size
?
787 snap_size
: i_size_read(inode
));
792 if (PageWriteback(page
)) {
793 dout("%p under writeback\n", page
);
798 /* only if matching snap context */
799 pgsnapc
= (void *)page
->private;
800 if (pgsnapc
->seq
> snapc
->seq
) {
801 dout("page snapc %p %lld > oldest %p %lld\n",
802 pgsnapc
, pgsnapc
->seq
, snapc
, snapc
->seq
);
805 continue; /* keep looking for snap */
809 if (!clear_page_dirty_for_io(page
)) {
810 dout("%p !clear_page_dirty_for_io\n", page
);
816 if (locked_pages
== 0) {
817 /* prepare async write request */
818 offset
= (unsigned long long)page
->index
821 req
= ceph_osdc_new_request(&fsc
->client
->osdc
,
826 CEPH_OSD_FLAG_WRITE
|
827 CEPH_OSD_FLAG_ONDISK
,
831 &inode
->i_mtime
, true, 1, 0);
839 max_pages
= req
->r_num_pages
;
841 alloc_page_vec(fsc
, req
);
842 req
->r_callback
= writepages_finish
;
843 req
->r_inode
= inode
;
846 /* note position of first page in pvec */
849 dout("%p will write page %p idx %lu\n",
850 inode
, page
, page
->index
);
853 atomic_long_inc_return(&fsc
->writeback_count
);
854 if (writeback_stat
> CONGESTION_ON_THRESH(
855 fsc
->mount_options
->congestion_kb
)) {
856 set_bdi_congested(&fsc
->backing_dev_info
,
860 set_page_writeback(page
);
861 req
->r_pages
[locked_pages
] = page
;
863 next
= page
->index
+ 1;
866 /* did we get anything? */
868 goto release_pvec_pages
;
871 BUG_ON(!locked_pages
|| first
< 0);
873 if (pvec_pages
&& i
== pvec_pages
&&
874 locked_pages
< max_pages
) {
875 dout("reached end pvec, trying for more\n");
876 pagevec_reinit(&pvec
);
880 /* shift unused pages over in the pvec... we
881 * will need to release them below. */
882 for (j
= i
; j
< pvec_pages
; j
++) {
883 dout(" pvec leftover page %p\n",
885 pvec
.pages
[j
-i
+first
] = pvec
.pages
[j
];
890 /* submit the write */
891 offset
= req
->r_pages
[0]->index
<< PAGE_CACHE_SHIFT
;
892 len
= min((snap_size
? snap_size
: i_size_read(inode
)) - offset
,
893 (u64
)locked_pages
<< PAGE_CACHE_SHIFT
);
894 dout("writepages got %d pages at %llu~%llu\n",
895 locked_pages
, offset
, len
);
897 /* revise final length, page count */
898 req
->r_num_pages
= locked_pages
;
899 reqhead
= req
->r_request
->front
.iov_base
;
900 op
= (void *)(reqhead
+ 1);
901 op
->extent
.length
= cpu_to_le64(len
);
902 op
->payload_len
= cpu_to_le32(len
);
903 req
->r_request
->hdr
.data_len
= cpu_to_le32(len
);
905 rc
= ceph_osdc_start_request(&fsc
->client
->osdc
, req
, true);
911 wbc
->nr_to_write
-= locked_pages
;
912 if (wbc
->nr_to_write
<= 0)
916 dout("pagevec_release on %d pages (%p)\n", (int)pvec
.nr
,
917 pvec
.nr
? pvec
.pages
[0] : NULL
);
918 pagevec_release(&pvec
);
920 if (locked_pages
&& !done
)
924 if (should_loop
&& !done
) {
925 /* more to do; loop back to beginning of file */
926 dout("writepages looping back to beginning of file\n");
932 if (wbc
->range_cyclic
|| (range_whole
&& wbc
->nr_to_write
> 0))
933 mapping
->writeback_index
= index
;
937 ceph_osdc_put_request(req
);
938 ceph_put_snap_context(snapc
);
939 dout("writepages done, rc = %d\n", rc
);
946 * See if a given @snapc is either writeable, or already written.
948 static int context_is_writeable_or_written(struct inode
*inode
,
949 struct ceph_snap_context
*snapc
)
951 struct ceph_snap_context
*oldest
= get_oldest_context(inode
, NULL
);
952 int ret
= !oldest
|| snapc
->seq
<= oldest
->seq
;
954 ceph_put_snap_context(oldest
);
959 * We are only allowed to write into/dirty the page if the page is
960 * clean, or already dirty within the same snap context.
962 * called with page locked.
963 * return success with page locked,
964 * or any failure (incl -EAGAIN) with page unlocked.
966 static int ceph_update_writeable_page(struct file
*file
,
967 loff_t pos
, unsigned len
,
970 struct inode
*inode
= file
->f_dentry
->d_inode
;
971 struct ceph_inode_info
*ci
= ceph_inode(inode
);
972 struct ceph_mds_client
*mdsc
= ceph_inode_to_client(inode
)->mdsc
;
973 loff_t page_off
= pos
& PAGE_CACHE_MASK
;
974 int pos_in_page
= pos
& ~PAGE_CACHE_MASK
;
975 int end_in_page
= pos_in_page
+ len
;
978 struct ceph_snap_context
*snapc
, *oldest
;
981 /* writepages currently holds page lock, but if we change that later, */
982 wait_on_page_writeback(page
);
984 /* check snap context */
985 BUG_ON(!ci
->i_snap_realm
);
986 down_read(&mdsc
->snap_rwsem
);
987 BUG_ON(!ci
->i_snap_realm
->cached_context
);
988 snapc
= (void *)page
->private;
989 if (snapc
&& snapc
!= ci
->i_head_snapc
) {
991 * this page is already dirty in another (older) snap
992 * context! is it writeable now?
994 oldest
= get_oldest_context(inode
, NULL
);
995 up_read(&mdsc
->snap_rwsem
);
997 if (snapc
->seq
> oldest
->seq
) {
998 ceph_put_snap_context(oldest
);
999 dout(" page %p snapc %p not current or oldest\n",
1002 * queue for writeback, and wait for snapc to
1003 * be writeable or written
1005 snapc
= ceph_get_snap_context(snapc
);
1007 ceph_queue_writeback(inode
);
1008 r
= wait_event_interruptible(ci
->i_cap_wq
,
1009 context_is_writeable_or_written(inode
, snapc
));
1010 ceph_put_snap_context(snapc
);
1011 if (r
== -ERESTARTSYS
)
1015 ceph_put_snap_context(oldest
);
1017 /* yay, writeable, do it now (without dropping page lock) */
1018 dout(" page %p snapc %p not current, but oldest\n",
1020 if (!clear_page_dirty_for_io(page
))
1022 r
= writepage_nounlock(page
, NULL
);
1028 if (PageUptodate(page
)) {
1029 dout(" page %p already uptodate\n", page
);
1034 if (pos_in_page
== 0 && len
== PAGE_CACHE_SIZE
)
1037 /* past end of file? */
1038 i_size
= inode
->i_size
; /* caller holds i_mutex */
1040 if (i_size
+ len
> inode
->i_sb
->s_maxbytes
) {
1041 /* file is too big */
1046 if (page_off
>= i_size
||
1047 (pos_in_page
== 0 && (pos
+len
) >= i_size
&&
1048 end_in_page
- pos_in_page
!= PAGE_CACHE_SIZE
)) {
1049 dout(" zeroing %p 0 - %d and %d - %d\n",
1050 page
, pos_in_page
, end_in_page
, (int)PAGE_CACHE_SIZE
);
1051 zero_user_segments(page
,
1053 end_in_page
, PAGE_CACHE_SIZE
);
1057 /* we need to read it. */
1058 up_read(&mdsc
->snap_rwsem
);
1059 r
= readpage_nounlock(file
, page
);
1065 up_read(&mdsc
->snap_rwsem
);
1072 * We are only allowed to write into/dirty the page if the page is
1073 * clean, or already dirty within the same snap context.
1075 static int ceph_write_begin(struct file
*file
, struct address_space
*mapping
,
1076 loff_t pos
, unsigned len
, unsigned flags
,
1077 struct page
**pagep
, void **fsdata
)
1079 struct inode
*inode
= file
->f_dentry
->d_inode
;
1081 pgoff_t index
= pos
>> PAGE_CACHE_SHIFT
;
1086 page
= grab_cache_page_write_begin(mapping
, index
, 0);
1091 dout("write_begin file %p inode %p page %p %d~%d\n", file
,
1092 inode
, page
, (int)pos
, (int)len
);
1094 r
= ceph_update_writeable_page(file
, pos
, len
, page
);
1095 } while (r
== -EAGAIN
);
1101 * we don't do anything in here that simple_write_end doesn't do
1102 * except adjust dirty page accounting and drop read lock on
1105 static int ceph_write_end(struct file
*file
, struct address_space
*mapping
,
1106 loff_t pos
, unsigned len
, unsigned copied
,
1107 struct page
*page
, void *fsdata
)
1109 struct inode
*inode
= file
->f_dentry
->d_inode
;
1110 struct ceph_fs_client
*fsc
= ceph_inode_to_client(inode
);
1111 struct ceph_mds_client
*mdsc
= fsc
->mdsc
;
1112 unsigned from
= pos
& (PAGE_CACHE_SIZE
- 1);
1115 dout("write_end file %p inode %p page %p %d~%d (%d)\n", file
,
1116 inode
, page
, (int)pos
, (int)copied
, (int)len
);
1118 /* zero the stale part of the page if we did a short copy */
1120 zero_user_segment(page
, from
+copied
, len
);
1122 /* did file size increase? */
1123 /* (no need for i_size_read(); we caller holds i_mutex */
1124 if (pos
+copied
> inode
->i_size
)
1125 check_cap
= ceph_inode_set_size(inode
, pos
+copied
);
1127 if (!PageUptodate(page
))
1128 SetPageUptodate(page
);
1130 set_page_dirty(page
);
1133 up_read(&mdsc
->snap_rwsem
);
1134 page_cache_release(page
);
1137 ceph_check_caps(ceph_inode(inode
), CHECK_CAPS_AUTHONLY
, NULL
);
1143 * we set .direct_IO to indicate direct io is supported, but since we
1144 * intercept O_DIRECT reads and writes early, this function should
1147 static ssize_t
ceph_direct_io(int rw
, struct kiocb
*iocb
,
1148 const struct iovec
*iov
,
1149 loff_t pos
, unsigned long nr_segs
)
1155 const struct address_space_operations ceph_aops
= {
1156 .readpage
= ceph_readpage
,
1157 .readpages
= ceph_readpages
,
1158 .writepage
= ceph_writepage
,
1159 .writepages
= ceph_writepages_start
,
1160 .write_begin
= ceph_write_begin
,
1161 .write_end
= ceph_write_end
,
1162 .set_page_dirty
= ceph_set_page_dirty
,
1163 .invalidatepage
= ceph_invalidatepage
,
1164 .releasepage
= ceph_releasepage
,
1165 .direct_IO
= ceph_direct_io
,
1174 * Reuse write_begin here for simplicity.
1176 static int ceph_page_mkwrite(struct vm_area_struct
*vma
, struct vm_fault
*vmf
)
1178 struct inode
*inode
= vma
->vm_file
->f_dentry
->d_inode
;
1179 struct page
*page
= vmf
->page
;
1180 struct ceph_mds_client
*mdsc
= ceph_inode_to_client(inode
)->mdsc
;
1181 loff_t off
= page
->index
<< PAGE_CACHE_SHIFT
;
1185 size
= i_size_read(inode
);
1186 if (off
+ PAGE_CACHE_SIZE
<= size
)
1187 len
= PAGE_CACHE_SIZE
;
1189 len
= size
& ~PAGE_CACHE_MASK
;
1191 dout("page_mkwrite %p %llu~%llu page %p idx %lu\n", inode
,
1192 off
, len
, page
, page
->index
);
1196 ret
= VM_FAULT_NOPAGE
;
1198 (page
->mapping
!= inode
->i_mapping
))
1201 ret
= ceph_update_writeable_page(vma
->vm_file
, off
, len
, page
);
1203 /* success. we'll keep the page locked. */
1204 set_page_dirty(page
);
1205 up_read(&mdsc
->snap_rwsem
);
1206 ret
= VM_FAULT_LOCKED
;
1211 ret
= VM_FAULT_SIGBUS
;
1214 dout("page_mkwrite %p %llu~%llu = %d\n", inode
, off
, len
, ret
);
1215 if (ret
!= VM_FAULT_LOCKED
)
1220 static struct vm_operations_struct ceph_vmops
= {
1221 .fault
= filemap_fault
,
1222 .page_mkwrite
= ceph_page_mkwrite
,
1225 int ceph_mmap(struct file
*file
, struct vm_area_struct
*vma
)
1227 struct address_space
*mapping
= file
->f_mapping
;
1229 if (!mapping
->a_ops
->readpage
)
1231 file_accessed(file
);
1232 vma
->vm_ops
= &ceph_vmops
;
1233 vma
->vm_flags
|= VM_CAN_NONLINEAR
;