5ffee90d9fb560c7fdbfd91bd0ff310602bdb947
[deliverable/linux.git] / fs / ceph / addr.c
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
57
58
59 /*
60 * Dirty a page. Optimistically adjust accounting, on the assumption
61 * that we won't race with invalidate. If we do, readjust.
62 */
63 static int ceph_set_page_dirty(struct page *page)
64 {
65 struct address_space *mapping = page->mapping;
66 struct inode *inode;
67 struct ceph_inode_info *ci;
68 int undo = 0;
69 struct ceph_snap_context *snapc;
70
71 if (unlikely(!mapping))
72 return !TestSetPageDirty(page);
73
74 if (TestSetPageDirty(page)) {
75 dout("%p set_page_dirty %p idx %lu -- already dirty\n",
76 mapping->host, page, page->index);
77 return 0;
78 }
79
80 inode = mapping->host;
81 ci = ceph_inode(inode);
82
83 /*
84 * Note that we're grabbing a snapc ref here without holding
85 * any locks!
86 */
87 snapc = ceph_get_snap_context(ci->i_snap_realm->cached_context);
88
89 /* dirty the head */
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)
95 ihold(inode);
96 ++ci->i_wrbuffer_ref;
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);
104
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);
112
113 /*
114 * Reference snap context in page->private. Also set
115 * PagePrivate so that we get invalidatepage callback.
116 */
117 page->private = (unsigned long)snapc;
118 SetPagePrivate(page);
119 } else {
120 dout("ANON set_page_dirty %p (raced truncate?)\n", page);
121 undo = 1;
122 }
123
124 spin_unlock_irq(&mapping->tree_lock);
125
126 if (undo)
127 /* whoops, we failed to dirty the page */
128 ceph_put_wrbuffer_cap_refs(ci, 1, snapc);
129
130 __mark_inode_dirty(mapping->host, I_DIRTY_PAGES);
131
132 BUG_ON(!PageDirty(page));
133 return 1;
134 }
135
136 /*
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
139 * data on the page.
140 */
141 static void ceph_invalidatepage(struct page *page, unsigned long offset)
142 {
143 struct inode *inode;
144 struct ceph_inode_info *ci;
145 struct ceph_snap_context *snapc = (void *)page->private;
146
147 BUG_ON(!PageLocked(page));
148 BUG_ON(!page->private);
149 BUG_ON(!PagePrivate(page));
150 BUG_ON(!page->mapping);
151
152 inode = page->mapping->host;
153
154 /*
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.
158 */
159 if (!PageDirty(page))
160 pr_err("%p invalidatepage %p page not dirty\n", inode, page);
161
162 if (offset == 0)
163 ClearPageChecked(page);
164
165 ci = ceph_inode(inode);
166 if (offset == 0) {
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);
171 page->private = 0;
172 ClearPagePrivate(page);
173 } else {
174 dout("%p invalidatepage %p idx %lu partial dirty page\n",
175 inode, page, page->index);
176 }
177 }
178
179 /* just a sanity check */
180 static int ceph_releasepage(struct page *page, gfp_t g)
181 {
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));
187 return 0;
188 }
189
190 /*
191 * read a single page, without unlocking it.
192 */
193 static int readpage_nounlock(struct file *filp, struct page *page)
194 {
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;
199 int err = 0;
200 u64 len = PAGE_CACHE_SIZE;
201
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,
207 &page, 1, 0);
208 if (err == -ENOENT)
209 err = 0;
210 if (err < 0) {
211 SetPageError(page);
212 goto out;
213 } else if (err < PAGE_CACHE_SIZE) {
214 /* zero fill remainder of page */
215 zero_user_segment(page, err, PAGE_CACHE_SIZE);
216 }
217 SetPageUptodate(page);
218
219 out:
220 return err < 0 ? err : 0;
221 }
222
223 static int ceph_readpage(struct file *filp, struct page *page)
224 {
225 int r = readpage_nounlock(filp, page);
226 unlock_page(page);
227 return r;
228 }
229
230 /*
231 * Finish an async read(ahead) op.
232 */
233 static void finish_read(struct ceph_osd_request *req, struct ceph_msg *msg)
234 {
235 struct inode *inode = req->r_inode;
236 struct ceph_osd_reply_head *replyhead;
237 int rc, bytes;
238 int i;
239
240 /* parse reply */
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);
245
246 dout("finish_read %p req %p rc %d bytes %d\n", inode, req, rc, bytes);
247
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];
251
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);
256 }
257 dout("finish_read %p uptodate %p idx %lu\n", inode, page,
258 page->index);
259 flush_dcache_page(page);
260 SetPageUptodate(page);
261 unlock_page(page);
262 page_cache_release(page);
263 }
264 kfree(req->r_pages);
265 }
266
267 /*
268 * start an async read(ahead) operation. return nr_pages we submitted
269 * a read for on success, or negative error code.
270 */
271 static int start_read(struct inode *inode, struct list_head *page_list, int max)
272 {
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;
278 u64 off;
279 u64 len;
280 int i;
281 struct page **pages;
282 pgoff_t next_index;
283 int nr_pages = 0;
284 int ret;
285
286 off = page->index << PAGE_CACHE_SHIFT;
287
288 /* count pages */
289 next_index = page->index;
290 list_for_each_entry_reverse(page, page_list, lru) {
291 if (page->index != next_index)
292 break;
293 nr_pages++;
294 next_index++;
295 if (max && nr_pages == max)
296 break;
297 }
298 len = nr_pages << PAGE_CACHE_SHIFT;
299 dout("start_read %p nr_pages %d is %lld~%lld\n", inode, nr_pages,
300 off, len);
301
302 req = ceph_osdc_new_request(osdc, &ci->i_layout, ceph_vino(inode),
303 off, &len,
304 CEPH_OSD_OP_READ, CEPH_OSD_FLAG_READ,
305 NULL, 0,
306 ci->i_truncate_seq, ci->i_truncate_size,
307 NULL, false, 1, 0);
308 if (!req)
309 return -ENOMEM;
310
311 /* build page vector */
312 nr_pages = len >> PAGE_CACHE_SHIFT;
313 pages = kmalloc(sizeof(*pages) * nr_pages, GFP_NOFS);
314 ret = -ENOMEM;
315 if (!pages)
316 goto out;
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);
321
322 dout("start_read %p adding %p idx %lu\n", inode, page,
323 page->index);
324 if (add_to_page_cache_lru(page, &inode->i_data, page->index,
325 GFP_NOFS)) {
326 page_cache_release(page);
327 dout("start_read %p add_to_page_cache failed %p\n",
328 inode, page);
329 nr_pages = i;
330 goto out_pages;
331 }
332 pages[i] = page;
333 }
334 req->r_pages = pages;
335 req->r_num_pages = nr_pages;
336 req->r_callback = finish_read;
337 req->r_inode = inode;
338
339 dout("start_read %p starting %p %lld~%lld\n", inode, req, off, len);
340 ret = ceph_osdc_start_request(osdc, req, false);
341 if (ret < 0)
342 goto out_pages;
343 ceph_osdc_put_request(req);
344 return nr_pages;
345
346 out_pages:
347 ceph_release_page_vector(pages, nr_pages);
348 kfree(pages);
349 out:
350 ceph_osdc_put_request(req);
351 return ret;
352 }
353
354
355 /*
356 * Read multiple pages. Leave pages we don't read + unlock in page_list;
357 * the caller (VM) cleans them up.
358 */
359 static int ceph_readpages(struct file *file, struct address_space *mapping,
360 struct list_head *page_list, unsigned nr_pages)
361 {
362 struct inode *inode = file->f_dentry->d_inode;
363 struct ceph_fs_client *fsc = ceph_inode_to_client(inode);
364 int rc = 0;
365 int max = 0;
366
367 if (fsc->mount_options->rsize >= PAGE_CACHE_SIZE)
368 max = (fsc->mount_options->rsize + PAGE_CACHE_SIZE - 1)
369 >> PAGE_SHIFT;
370
371 dout("readpages %p file %p nr_pages %d max %d\n", inode, file, nr_pages,
372 max);
373 while (!list_empty(page_list)) {
374 rc = start_read(inode, page_list, max);
375 if (rc < 0)
376 goto out;
377 BUG_ON(rc == 0);
378 }
379 out:
380 dout("readpages %p file %p ret %d\n", inode, file, rc);
381 return rc;
382 }
383
384 /*
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.
387 */
388 static struct ceph_snap_context *get_oldest_context(struct inode *inode,
389 u64 *snap_size)
390 {
391 struct ceph_inode_info *ci = ceph_inode(inode);
392 struct ceph_snap_context *snapc = NULL;
393 struct ceph_cap_snap *capsnap = NULL;
394
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);
401 if (snap_size)
402 *snap_size = capsnap->size;
403 break;
404 }
405 }
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);
410 }
411 spin_unlock(&inode->i_lock);
412 return snapc;
413 }
414
415 /*
416 * Write a single page, but leave the page locked.
417 *
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).
420 */
421 static int writepage_nounlock(struct page *page, struct writeback_control *wbc)
422 {
423 struct inode *inode;
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;
429 loff_t i_size;
430 int err = 0;
431 struct ceph_snap_context *snapc, *oldest;
432 u64 snap_size = 0;
433 long writeback_stat;
434
435 dout("writepage %p idx %lu\n", page, page->index);
436
437 if (!page->mapping || !page->mapping->host) {
438 dout("writepage %p - no mapping\n", page);
439 return -EFAULT;
440 }
441 inode = page->mapping->host;
442 ci = ceph_inode(inode);
443 fsc = ceph_inode_to_client(inode);
444 osdc = &fsc->client->osdc;
445
446 /* verify this is a writeable snap context */
447 snapc = (void *)page->private;
448 if (snapc == NULL) {
449 dout("writepage %p page %p not dirty?\n", inode, page);
450 goto out;
451 }
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);
459 goto out;
460 }
461 ceph_put_snap_context(oldest);
462
463 /* is this a partial page at end of file? */
464 if (snap_size)
465 i_size = snap_size;
466 else
467 i_size = i_size_read(inode);
468 if (i_size < page_off + len)
469 len = i_size - page_off;
470
471 dout("writepage %p page %p index %lu on %llu~%u snapc %p\n",
472 inode, page, page->index, page_off, len, snapc);
473
474 writeback_stat = atomic_long_inc_return(&fsc->writeback_count);
475 if (writeback_stat >
476 CONGESTION_ON_THRESH(fsc->mount_options->congestion_kb))
477 set_bdi_congested(&fsc->backing_dev_info, BLK_RW_ASYNC);
478
479 set_page_writeback(page);
480 err = ceph_osdc_writepages(osdc, ceph_vino(inode),
481 &ci->i_layout, snapc,
482 page_off, len,
483 ci->i_truncate_seq, ci->i_truncate_size,
484 &inode->i_mtime,
485 &page, 1, 0, 0, true);
486 if (err < 0) {
487 dout("writepage setting page/mapping error %d %p\n", err, page);
488 SetPageError(page);
489 mapping_set_error(&inode->i_data, err);
490 if (wbc)
491 wbc->pages_skipped++;
492 } else {
493 dout("writepage cleaned page %p\n", page);
494 err = 0; /* vfs expects us to return 0 */
495 }
496 page->private = 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 */
501 out:
502 return err;
503 }
504
505 static int ceph_writepage(struct page *page, struct writeback_control *wbc)
506 {
507 int err;
508 struct inode *inode = page->mapping->host;
509 BUG_ON(!inode);
510 ihold(inode);
511 err = writepage_nounlock(page, wbc);
512 unlock_page(page);
513 iput(inode);
514 return err;
515 }
516
517
518 /*
519 * lame release_pages helper. release_pages() isn't exported to
520 * modules.
521 */
522 static void ceph_release_pages(struct page **pages, int num)
523 {
524 struct pagevec pvec;
525 int i;
526
527 pagevec_init(&pvec, 0);
528 for (i = 0; i < num; i++) {
529 if (pagevec_add(&pvec, pages[i]) == 0)
530 pagevec_release(&pvec);
531 }
532 pagevec_release(&pvec);
533 }
534
535
536 /*
537 * async writeback completion handler.
538 *
539 * If we get an error, set the mapping error bit, but not the individual
540 * page error bits.
541 */
542 static void writepages_finish(struct ceph_osd_request *req,
543 struct ceph_msg *msg)
544 {
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);
549 unsigned wrote;
550 struct page *page;
551 int i;
552 struct ceph_snap_context *snapc = req->r_snapc;
553 struct address_space *mapping = inode->i_mapping;
554 __s32 rc = -EIO;
555 u64 bytes = 0;
556 struct ceph_fs_client *fsc = ceph_inode_to_client(inode);
557 long writeback_stat;
558 unsigned issued = ceph_caps_issued(ci);
559
560 /* parse reply */
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);
566
567 if (rc >= 0) {
568 /*
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.
573 */
574 wrote = req->r_num_pages;
575 } else {
576 wrote = 0;
577 mapping_set_error(mapping, rc);
578 }
579 dout("writepages_finish %p rc %d bytes %llu wrote %d (pages)\n",
580 inode, rc, bytes, wrote);
581
582 /* clean all pages */
583 for (i = 0; i < req->r_num_pages; i++) {
584 page = req->r_pages[i];
585 BUG_ON(!page);
586 WARN_ON(!PageUptodate(page));
587
588 writeback_stat =
589 atomic_long_dec_return(&fsc->writeback_count);
590 if (writeback_stat <
591 CONGESTION_OFF_THRESH(fsc->mount_options->congestion_kb))
592 clear_bdi_congested(&fsc->backing_dev_info,
593 BLK_RW_ASYNC);
594
595 ceph_put_snap_context((void *)page->private);
596 page->private = 0;
597 ClearPagePrivate(page);
598 dout("unlocking %d %p\n", i, page);
599 end_page_writeback(page);
600
601 /*
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
605 * raced its way in
606 */
607 if ((issued & (CEPH_CAP_FILE_CACHE|CEPH_CAP_FILE_LAZYIO)) == 0)
608 generic_error_remove_page(inode->i_mapping, page);
609
610 unlock_page(page);
611 }
612 dout("%p wrote+cleaned %d pages\n", inode, wrote);
613 ceph_put_wrbuffer_cap_refs(ci, req->r_num_pages, snapc);
614
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);
619 else
620 kfree(req->r_pages);
621 ceph_osdc_put_request(req);
622 }
623
624 /*
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.
628 */
629 static void alloc_page_vec(struct ceph_fs_client *fsc,
630 struct ceph_osd_request *req)
631 {
632 req->r_pages = kmalloc(sizeof(struct page *) * req->r_num_pages,
633 GFP_NOFS);
634 if (!req->r_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);
638 }
639 }
640
641 /*
642 * initiate async writeback
643 */
644 static int ceph_writepages_start(struct address_space *mapping,
645 struct writeback_control *wbc)
646 {
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;
651 int range_whole = 0;
652 int should_loop = 1;
653 pgoff_t max_pages = 0, max_pages_ever = 0;
654 struct ceph_snap_context *snapc = NULL, *last_snapc = NULL, *pgsnapc;
655 struct pagevec pvec;
656 int done = 0;
657 int rc = 0;
658 unsigned wsize = 1 << inode->i_blkbits;
659 struct ceph_osd_request *req = NULL;
660 int do_sync;
661 u64 snap_size = 0;
662
663 /*
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.
667 */
668 do_sync = wbc->sync_mode == WB_SYNC_ALL;
669 if (ceph_caps_revoking(ci, CEPH_CAP_FILE_BUFFER))
670 do_sync = 1;
671 dout("writepages_start %p dosync=%d (mode=%s)\n",
672 inode, do_sync,
673 wbc->sync_mode == WB_SYNC_NONE ? "NONE" :
674 (wbc->sync_mode == WB_SYNC_ALL ? "ALL" : "HOLD"));
675
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! */
680 }
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;
686
687 pagevec_init(&pvec, 0);
688
689 /* where to start/end? */
690 if (wbc->range_cyclic) {
691 start = mapping->writeback_index; /* Start from prev offset */
692 end = -1;
693 dout(" cyclic, start at %lu\n", start);
694 } else {
695 start = wbc->range_start >> PAGE_CACHE_SHIFT;
696 end = wbc->range_end >> PAGE_CACHE_SHIFT;
697 if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX)
698 range_whole = 1;
699 should_loop = 0;
700 dout(" not cyclic, %lu to %lu\n", start, end);
701 }
702 index = start;
703
704 retry:
705 /* find oldest snap context with dirty data */
706 ceph_put_snap_context(snapc);
707 snapc = get_oldest_context(inode, &snap_size);
708 if (!snapc) {
709 /* hmm, why does writepages get called when there
710 is no dirty data? */
711 dout(" no snap context with dirty data?\n");
712 goto out;
713 }
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",
720 index);
721 index = start;
722 }
723 last_snapc = snapc;
724
725 while (!done && index <= end) {
726 unsigned i;
727 int first;
728 pgoff_t next;
729 int pvec_pages, locked_pages;
730 struct page *page;
731 int want;
732 u64 offset, len;
733 struct ceph_osd_request_head *reqhead;
734 struct ceph_osd_op *op;
735 long writeback_stat;
736
737 next = 0;
738 locked_pages = 0;
739 max_pages = max_pages_ever;
740
741 get_more_pages:
742 first = -1;
743 want = min(end - index,
744 min((pgoff_t)PAGEVEC_SIZE,
745 max_pages - (pgoff_t)locked_pages) - 1)
746 + 1;
747 pvec_pages = pagevec_lookup_tag(&pvec, mapping, &index,
748 PAGECACHE_TAG_DIRTY,
749 want);
750 dout("pagevec_lookup_tag got %d\n", pvec_pages);
751 if (!pvec_pages && !locked_pages)
752 break;
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))
759 break;
760
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);
765 unlock_page(page);
766 break;
767 }
768 if (!wbc->range_cyclic && page->index > end) {
769 dout("end of range %p\n", page);
770 done = 1;
771 unlock_page(page);
772 break;
773 }
774 if (next && (page->index != next)) {
775 dout("not consecutive %p\n", page);
776 unlock_page(page);
777 break;
778 }
779 if (wbc->sync_mode != WB_SYNC_NONE) {
780 dout("waiting on writeback %p\n", page);
781 wait_on_page_writeback(page);
782 }
783 if ((snap_size && page_offset(page) > snap_size) ||
784 (!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));
788 done = 1;
789 unlock_page(page);
790 break;
791 }
792 if (PageWriteback(page)) {
793 dout("%p under writeback\n", page);
794 unlock_page(page);
795 break;
796 }
797
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);
803 unlock_page(page);
804 if (!locked_pages)
805 continue; /* keep looking for snap */
806 break;
807 }
808
809 if (!clear_page_dirty_for_io(page)) {
810 dout("%p !clear_page_dirty_for_io\n", page);
811 unlock_page(page);
812 break;
813 }
814
815 /* ok */
816 if (locked_pages == 0) {
817 /* prepare async write request */
818 offset = (unsigned long long)page->index
819 << PAGE_CACHE_SHIFT;
820 len = wsize;
821 req = ceph_osdc_new_request(&fsc->client->osdc,
822 &ci->i_layout,
823 ceph_vino(inode),
824 offset, &len,
825 CEPH_OSD_OP_WRITE,
826 CEPH_OSD_FLAG_WRITE |
827 CEPH_OSD_FLAG_ONDISK,
828 snapc, do_sync,
829 ci->i_truncate_seq,
830 ci->i_truncate_size,
831 &inode->i_mtime, true, 1, 0);
832
833 if (!req) {
834 rc = -ENOMEM;
835 unlock_page(page);
836 break;
837 }
838
839 max_pages = req->r_num_pages;
840
841 alloc_page_vec(fsc, req);
842 req->r_callback = writepages_finish;
843 req->r_inode = inode;
844 }
845
846 /* note position of first page in pvec */
847 if (first < 0)
848 first = i;
849 dout("%p will write page %p idx %lu\n",
850 inode, page, page->index);
851
852 writeback_stat =
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,
857 BLK_RW_ASYNC);
858 }
859
860 set_page_writeback(page);
861 req->r_pages[locked_pages] = page;
862 locked_pages++;
863 next = page->index + 1;
864 }
865
866 /* did we get anything? */
867 if (!locked_pages)
868 goto release_pvec_pages;
869 if (i) {
870 int j;
871 BUG_ON(!locked_pages || first < 0);
872
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);
877 goto get_more_pages;
878 }
879
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",
884 pvec.pages[j]);
885 pvec.pages[j-i+first] = pvec.pages[j];
886 }
887 pvec.nr -= i-first;
888 }
889
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);
896
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);
904
905 rc = ceph_osdc_start_request(&fsc->client->osdc, req, true);
906 BUG_ON(rc);
907 req = NULL;
908
909 /* continue? */
910 index = next;
911 wbc->nr_to_write -= locked_pages;
912 if (wbc->nr_to_write <= 0)
913 done = 1;
914
915 release_pvec_pages:
916 dout("pagevec_release on %d pages (%p)\n", (int)pvec.nr,
917 pvec.nr ? pvec.pages[0] : NULL);
918 pagevec_release(&pvec);
919
920 if (locked_pages && !done)
921 goto retry;
922 }
923
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");
927 should_loop = 0;
928 index = 0;
929 goto retry;
930 }
931
932 if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0))
933 mapping->writeback_index = index;
934
935 out:
936 if (req)
937 ceph_osdc_put_request(req);
938 ceph_put_snap_context(snapc);
939 dout("writepages done, rc = %d\n", rc);
940 return rc;
941 }
942
943
944
945 /*
946 * See if a given @snapc is either writeable, or already written.
947 */
948 static int context_is_writeable_or_written(struct inode *inode,
949 struct ceph_snap_context *snapc)
950 {
951 struct ceph_snap_context *oldest = get_oldest_context(inode, NULL);
952 int ret = !oldest || snapc->seq <= oldest->seq;
953
954 ceph_put_snap_context(oldest);
955 return ret;
956 }
957
958 /*
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.
961 *
962 * called with page locked.
963 * return success with page locked,
964 * or any failure (incl -EAGAIN) with page unlocked.
965 */
966 static int ceph_update_writeable_page(struct file *file,
967 loff_t pos, unsigned len,
968 struct page *page)
969 {
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;
976 loff_t i_size;
977 int r;
978 struct ceph_snap_context *snapc, *oldest;
979
980 retry_locked:
981 /* writepages currently holds page lock, but if we change that later, */
982 wait_on_page_writeback(page);
983
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) {
990 /*
991 * this page is already dirty in another (older) snap
992 * context! is it writeable now?
993 */
994 oldest = get_oldest_context(inode, NULL);
995 up_read(&mdsc->snap_rwsem);
996
997 if (snapc->seq > oldest->seq) {
998 ceph_put_snap_context(oldest);
999 dout(" page %p snapc %p not current or oldest\n",
1000 page, snapc);
1001 /*
1002 * queue for writeback, and wait for snapc to
1003 * be writeable or written
1004 */
1005 snapc = ceph_get_snap_context(snapc);
1006 unlock_page(page);
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)
1012 return r;
1013 return -EAGAIN;
1014 }
1015 ceph_put_snap_context(oldest);
1016
1017 /* yay, writeable, do it now (without dropping page lock) */
1018 dout(" page %p snapc %p not current, but oldest\n",
1019 page, snapc);
1020 if (!clear_page_dirty_for_io(page))
1021 goto retry_locked;
1022 r = writepage_nounlock(page, NULL);
1023 if (r < 0)
1024 goto fail_nosnap;
1025 goto retry_locked;
1026 }
1027
1028 if (PageUptodate(page)) {
1029 dout(" page %p already uptodate\n", page);
1030 return 0;
1031 }
1032
1033 /* full page? */
1034 if (pos_in_page == 0 && len == PAGE_CACHE_SIZE)
1035 return 0;
1036
1037 /* past end of file? */
1038 i_size = inode->i_size; /* caller holds i_mutex */
1039
1040 if (i_size + len > inode->i_sb->s_maxbytes) {
1041 /* file is too big */
1042 r = -EINVAL;
1043 goto fail;
1044 }
1045
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,
1052 0, pos_in_page,
1053 end_in_page, PAGE_CACHE_SIZE);
1054 return 0;
1055 }
1056
1057 /* we need to read it. */
1058 up_read(&mdsc->snap_rwsem);
1059 r = readpage_nounlock(file, page);
1060 if (r < 0)
1061 goto fail_nosnap;
1062 goto retry_locked;
1063
1064 fail:
1065 up_read(&mdsc->snap_rwsem);
1066 fail_nosnap:
1067 unlock_page(page);
1068 return r;
1069 }
1070
1071 /*
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.
1074 */
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)
1078 {
1079 struct inode *inode = file->f_dentry->d_inode;
1080 struct page *page;
1081 pgoff_t index = pos >> PAGE_CACHE_SHIFT;
1082 int r;
1083
1084 do {
1085 /* get a page */
1086 page = grab_cache_page_write_begin(mapping, index, 0);
1087 if (!page)
1088 return -ENOMEM;
1089 *pagep = page;
1090
1091 dout("write_begin file %p inode %p page %p %d~%d\n", file,
1092 inode, page, (int)pos, (int)len);
1093
1094 r = ceph_update_writeable_page(file, pos, len, page);
1095 } while (r == -EAGAIN);
1096
1097 return r;
1098 }
1099
1100 /*
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
1103 * mdsc->snap_rwsem.
1104 */
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)
1108 {
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);
1113 int check_cap = 0;
1114
1115 dout("write_end file %p inode %p page %p %d~%d (%d)\n", file,
1116 inode, page, (int)pos, (int)copied, (int)len);
1117
1118 /* zero the stale part of the page if we did a short copy */
1119 if (copied < len)
1120 zero_user_segment(page, from+copied, len);
1121
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);
1126
1127 if (!PageUptodate(page))
1128 SetPageUptodate(page);
1129
1130 set_page_dirty(page);
1131
1132 unlock_page(page);
1133 up_read(&mdsc->snap_rwsem);
1134 page_cache_release(page);
1135
1136 if (check_cap)
1137 ceph_check_caps(ceph_inode(inode), CHECK_CAPS_AUTHONLY, NULL);
1138
1139 return copied;
1140 }
1141
1142 /*
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
1145 * never get called.
1146 */
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)
1150 {
1151 WARN_ON(1);
1152 return -EINVAL;
1153 }
1154
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,
1166 };
1167
1168
1169 /*
1170 * vm ops
1171 */
1172
1173 /*
1174 * Reuse write_begin here for simplicity.
1175 */
1176 static int ceph_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
1177 {
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;
1182 loff_t size, len;
1183 int ret;
1184
1185 size = i_size_read(inode);
1186 if (off + PAGE_CACHE_SIZE <= size)
1187 len = PAGE_CACHE_SIZE;
1188 else
1189 len = size & ~PAGE_CACHE_MASK;
1190
1191 dout("page_mkwrite %p %llu~%llu page %p idx %lu\n", inode,
1192 off, len, page, page->index);
1193
1194 lock_page(page);
1195
1196 ret = VM_FAULT_NOPAGE;
1197 if ((off > size) ||
1198 (page->mapping != inode->i_mapping))
1199 goto out;
1200
1201 ret = ceph_update_writeable_page(vma->vm_file, off, len, page);
1202 if (ret == 0) {
1203 /* success. we'll keep the page locked. */
1204 set_page_dirty(page);
1205 up_read(&mdsc->snap_rwsem);
1206 ret = VM_FAULT_LOCKED;
1207 } else {
1208 if (ret == -ENOMEM)
1209 ret = VM_FAULT_OOM;
1210 else
1211 ret = VM_FAULT_SIGBUS;
1212 }
1213 out:
1214 dout("page_mkwrite %p %llu~%llu = %d\n", inode, off, len, ret);
1215 if (ret != VM_FAULT_LOCKED)
1216 unlock_page(page);
1217 return ret;
1218 }
1219
1220 static struct vm_operations_struct ceph_vmops = {
1221 .fault = filemap_fault,
1222 .page_mkwrite = ceph_page_mkwrite,
1223 };
1224
1225 int ceph_mmap(struct file *file, struct vm_area_struct *vma)
1226 {
1227 struct address_space *mapping = file->f_mapping;
1228
1229 if (!mapping->a_ops->readpage)
1230 return -ENOEXEC;
1231 file_accessed(file);
1232 vma->vm_ops = &ceph_vmops;
1233 vma->vm_flags |= VM_CAN_NONLINEAR;
1234 return 0;
1235 }
This page took 0.070187 seconds and 4 git commands to generate.