libceph: redo callbacks and factor out MOSDOpReply decoding
[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 "cache.h"
15 #include <linux/ceph/osd_client.h>
16
17 /*
18 * Ceph address space ops.
19 *
20 * There are a few funny things going on here.
21 *
22 * The page->private field is used to reference a struct
23 * ceph_snap_context for _every_ dirty page. This indicates which
24 * snapshot the page was logically dirtied in, and thus which snap
25 * context needs to be associated with the osd write during writeback.
26 *
27 * Similarly, struct ceph_inode_info maintains a set of counters to
28 * count dirty pages on the inode. In the absence of snapshots,
29 * i_wrbuffer_ref == i_wrbuffer_ref_head == the dirty page count.
30 *
31 * When a snapshot is taken (that is, when the client receives
32 * notification that a snapshot was taken), each inode with caps and
33 * with dirty pages (dirty pages implies there is a cap) gets a new
34 * ceph_cap_snap in the i_cap_snaps list (which is sorted in ascending
35 * order, new snaps go to the tail). The i_wrbuffer_ref_head count is
36 * moved to capsnap->dirty. (Unless a sync write is currently in
37 * progress. In that case, the capsnap is said to be "pending", new
38 * writes cannot start, and the capsnap isn't "finalized" until the
39 * write completes (or fails) and a final size/mtime for the inode for
40 * that snap can be settled upon.) i_wrbuffer_ref_head is reset to 0.
41 *
42 * On writeback, we must submit writes to the osd IN SNAP ORDER. So,
43 * we look for the first capsnap in i_cap_snaps and write out pages in
44 * that snap context _only_. Then we move on to the next capsnap,
45 * eventually reaching the "live" or "head" context (i.e., pages that
46 * are not yet snapped) and are writing the most recently dirtied
47 * pages.
48 *
49 * Invalidate and so forth must take care to ensure the dirty page
50 * accounting is preserved.
51 */
52
53 #define CONGESTION_ON_THRESH(congestion_kb) (congestion_kb >> (PAGE_SHIFT-10))
54 #define CONGESTION_OFF_THRESH(congestion_kb) \
55 (CONGESTION_ON_THRESH(congestion_kb) - \
56 (CONGESTION_ON_THRESH(congestion_kb) >> 2))
57
58 static inline struct ceph_snap_context *page_snap_context(struct page *page)
59 {
60 if (PagePrivate(page))
61 return (void *)page->private;
62 return NULL;
63 }
64
65 /*
66 * Dirty a page. Optimistically adjust accounting, on the assumption
67 * that we won't race with invalidate. If we do, readjust.
68 */
69 static int ceph_set_page_dirty(struct page *page)
70 {
71 struct address_space *mapping = page->mapping;
72 struct inode *inode;
73 struct ceph_inode_info *ci;
74 struct ceph_snap_context *snapc;
75 int ret;
76
77 if (unlikely(!mapping))
78 return !TestSetPageDirty(page);
79
80 if (PageDirty(page)) {
81 dout("%p set_page_dirty %p idx %lu -- already dirty\n",
82 mapping->host, page, page->index);
83 BUG_ON(!PagePrivate(page));
84 return 0;
85 }
86
87 inode = mapping->host;
88 ci = ceph_inode(inode);
89
90 /* dirty the head */
91 spin_lock(&ci->i_ceph_lock);
92 BUG_ON(ci->i_wr_ref == 0); // caller should hold Fw reference
93 if (__ceph_have_pending_cap_snap(ci)) {
94 struct ceph_cap_snap *capsnap =
95 list_last_entry(&ci->i_cap_snaps,
96 struct ceph_cap_snap,
97 ci_item);
98 snapc = ceph_get_snap_context(capsnap->context);
99 capsnap->dirty_pages++;
100 } else {
101 BUG_ON(!ci->i_head_snapc);
102 snapc = ceph_get_snap_context(ci->i_head_snapc);
103 ++ci->i_wrbuffer_ref_head;
104 }
105 if (ci->i_wrbuffer_ref == 0)
106 ihold(inode);
107 ++ci->i_wrbuffer_ref;
108 dout("%p set_page_dirty %p idx %lu head %d/%d -> %d/%d "
109 "snapc %p seq %lld (%d snaps)\n",
110 mapping->host, page, page->index,
111 ci->i_wrbuffer_ref-1, ci->i_wrbuffer_ref_head-1,
112 ci->i_wrbuffer_ref, ci->i_wrbuffer_ref_head,
113 snapc, snapc->seq, snapc->num_snaps);
114 spin_unlock(&ci->i_ceph_lock);
115
116 /*
117 * Reference snap context in page->private. Also set
118 * PagePrivate so that we get invalidatepage callback.
119 */
120 BUG_ON(PagePrivate(page));
121 page->private = (unsigned long)snapc;
122 SetPagePrivate(page);
123
124 ret = __set_page_dirty_nobuffers(page);
125 WARN_ON(!PageLocked(page));
126 WARN_ON(!page->mapping);
127
128 return ret;
129 }
130
131 /*
132 * If we are truncating the full page (i.e. offset == 0), adjust the
133 * dirty page counters appropriately. Only called if there is private
134 * data on the page.
135 */
136 static void ceph_invalidatepage(struct page *page, unsigned int offset,
137 unsigned int length)
138 {
139 struct inode *inode;
140 struct ceph_inode_info *ci;
141 struct ceph_snap_context *snapc = page_snap_context(page);
142
143 inode = page->mapping->host;
144 ci = ceph_inode(inode);
145
146 if (offset != 0 || length != PAGE_SIZE) {
147 dout("%p invalidatepage %p idx %lu partial dirty page %u~%u\n",
148 inode, page, page->index, offset, length);
149 return;
150 }
151
152 ceph_invalidate_fscache_page(inode, page);
153
154 if (!PagePrivate(page))
155 return;
156
157 /*
158 * We can get non-dirty pages here due to races between
159 * set_page_dirty and truncate_complete_page; just spit out a
160 * warning, in case we end up with accounting problems later.
161 */
162 if (!PageDirty(page))
163 pr_err("%p invalidatepage %p page not dirty\n", inode, page);
164
165 ClearPageChecked(page);
166
167 dout("%p invalidatepage %p idx %lu full dirty page\n",
168 inode, page, page->index);
169
170 ceph_put_wrbuffer_cap_refs(ci, 1, snapc);
171 ceph_put_snap_context(snapc);
172 page->private = 0;
173 ClearPagePrivate(page);
174 }
175
176 static int ceph_releasepage(struct page *page, gfp_t g)
177 {
178 dout("%p releasepage %p idx %lu\n", page->mapping->host,
179 page, page->index);
180 WARN_ON(PageDirty(page));
181
182 /* Can we release the page from the cache? */
183 if (!ceph_release_fscache_page(page, g))
184 return 0;
185
186 return !PagePrivate(page);
187 }
188
189 /*
190 * read a single page, without unlocking it.
191 */
192 static int readpage_nounlock(struct file *filp, struct page *page)
193 {
194 struct inode *inode = file_inode(filp);
195 struct ceph_inode_info *ci = ceph_inode(inode);
196 struct ceph_osd_client *osdc =
197 &ceph_inode_to_client(inode)->client->osdc;
198 int err = 0;
199 u64 off = page_offset(page);
200 u64 len = PAGE_SIZE;
201
202 if (off >= i_size_read(inode)) {
203 zero_user_segment(page, 0, PAGE_SIZE);
204 SetPageUptodate(page);
205 return 0;
206 }
207
208 if (ci->i_inline_version != CEPH_INLINE_NONE) {
209 /*
210 * Uptodate inline data should have been added
211 * into page cache while getting Fcr caps.
212 */
213 if (off == 0)
214 return -EINVAL;
215 zero_user_segment(page, 0, PAGE_SIZE);
216 SetPageUptodate(page);
217 return 0;
218 }
219
220 err = ceph_readpage_from_fscache(inode, page);
221 if (err == 0)
222 goto out;
223
224 dout("readpage inode %p file %p page %p index %lu\n",
225 inode, filp, page, page->index);
226 err = ceph_osdc_readpages(osdc, ceph_vino(inode), &ci->i_layout,
227 off, &len,
228 ci->i_truncate_seq, ci->i_truncate_size,
229 &page, 1, 0);
230 if (err == -ENOENT)
231 err = 0;
232 if (err < 0) {
233 SetPageError(page);
234 ceph_fscache_readpage_cancel(inode, page);
235 goto out;
236 }
237 if (err < PAGE_SIZE)
238 /* zero fill remainder of page */
239 zero_user_segment(page, err, PAGE_SIZE);
240 else
241 flush_dcache_page(page);
242
243 SetPageUptodate(page);
244 ceph_readpage_to_fscache(inode, page);
245
246 out:
247 return err < 0 ? err : 0;
248 }
249
250 static int ceph_readpage(struct file *filp, struct page *page)
251 {
252 int r = readpage_nounlock(filp, page);
253 unlock_page(page);
254 return r;
255 }
256
257 /*
258 * Finish an async read(ahead) op.
259 */
260 static void finish_read(struct ceph_osd_request *req)
261 {
262 struct inode *inode = req->r_inode;
263 struct ceph_osd_data *osd_data;
264 int rc = req->r_result <= 0 ? req->r_result : 0;
265 int bytes = req->r_result >= 0 ? req->r_result : 0;
266 int num_pages;
267 int i;
268
269 dout("finish_read %p req %p rc %d bytes %d\n", inode, req, rc, bytes);
270
271 /* unlock all pages, zeroing any data we didn't read */
272 osd_data = osd_req_op_extent_osd_data(req, 0);
273 BUG_ON(osd_data->type != CEPH_OSD_DATA_TYPE_PAGES);
274 num_pages = calc_pages_for((u64)osd_data->alignment,
275 (u64)osd_data->length);
276 for (i = 0; i < num_pages; i++) {
277 struct page *page = osd_data->pages[i];
278
279 if (rc < 0 && rc != -ENOENT)
280 goto unlock;
281 if (bytes < (int)PAGE_SIZE) {
282 /* zero (remainder of) page */
283 int s = bytes < 0 ? 0 : bytes;
284 zero_user_segment(page, s, PAGE_SIZE);
285 }
286 dout("finish_read %p uptodate %p idx %lu\n", inode, page,
287 page->index);
288 flush_dcache_page(page);
289 SetPageUptodate(page);
290 ceph_readpage_to_fscache(inode, page);
291 unlock:
292 unlock_page(page);
293 put_page(page);
294 bytes -= PAGE_SIZE;
295 }
296 kfree(osd_data->pages);
297 }
298
299 static void ceph_unlock_page_vector(struct page **pages, int num_pages)
300 {
301 int i;
302
303 for (i = 0; i < num_pages; i++)
304 unlock_page(pages[i]);
305 }
306
307 /*
308 * start an async read(ahead) operation. return nr_pages we submitted
309 * a read for on success, or negative error code.
310 */
311 static int start_read(struct inode *inode, struct list_head *page_list, int max)
312 {
313 struct ceph_osd_client *osdc =
314 &ceph_inode_to_client(inode)->client->osdc;
315 struct ceph_inode_info *ci = ceph_inode(inode);
316 struct page *page = list_entry(page_list->prev, struct page, lru);
317 struct ceph_vino vino;
318 struct ceph_osd_request *req;
319 u64 off;
320 u64 len;
321 int i;
322 struct page **pages;
323 pgoff_t next_index;
324 int nr_pages = 0;
325 int ret;
326
327 off = (u64) page_offset(page);
328
329 /* count pages */
330 next_index = page->index;
331 list_for_each_entry_reverse(page, page_list, lru) {
332 if (page->index != next_index)
333 break;
334 nr_pages++;
335 next_index++;
336 if (max && nr_pages == max)
337 break;
338 }
339 len = nr_pages << PAGE_SHIFT;
340 dout("start_read %p nr_pages %d is %lld~%lld\n", inode, nr_pages,
341 off, len);
342 vino = ceph_vino(inode);
343 req = ceph_osdc_new_request(osdc, &ci->i_layout, vino, off, &len,
344 0, 1, CEPH_OSD_OP_READ,
345 CEPH_OSD_FLAG_READ, NULL,
346 ci->i_truncate_seq, ci->i_truncate_size,
347 false);
348 if (IS_ERR(req))
349 return PTR_ERR(req);
350
351 /* build page vector */
352 nr_pages = calc_pages_for(0, len);
353 pages = kmalloc(sizeof(*pages) * nr_pages, GFP_KERNEL);
354 ret = -ENOMEM;
355 if (!pages)
356 goto out;
357 for (i = 0; i < nr_pages; ++i) {
358 page = list_entry(page_list->prev, struct page, lru);
359 BUG_ON(PageLocked(page));
360 list_del(&page->lru);
361
362 dout("start_read %p adding %p idx %lu\n", inode, page,
363 page->index);
364 if (add_to_page_cache_lru(page, &inode->i_data, page->index,
365 GFP_KERNEL)) {
366 ceph_fscache_uncache_page(inode, page);
367 put_page(page);
368 dout("start_read %p add_to_page_cache failed %p\n",
369 inode, page);
370 nr_pages = i;
371 goto out_pages;
372 }
373 pages[i] = page;
374 }
375 osd_req_op_extent_osd_data_pages(req, 0, pages, len, 0, false, false);
376 req->r_callback = finish_read;
377 req->r_inode = inode;
378
379 dout("start_read %p starting %p %lld~%lld\n", inode, req, off, len);
380 ret = ceph_osdc_start_request(osdc, req, false);
381 if (ret < 0)
382 goto out_pages;
383 ceph_osdc_put_request(req);
384 return nr_pages;
385
386 out_pages:
387 ceph_unlock_page_vector(pages, nr_pages);
388 ceph_release_page_vector(pages, nr_pages);
389 out:
390 ceph_osdc_put_request(req);
391 return ret;
392 }
393
394
395 /*
396 * Read multiple pages. Leave pages we don't read + unlock in page_list;
397 * the caller (VM) cleans them up.
398 */
399 static int ceph_readpages(struct file *file, struct address_space *mapping,
400 struct list_head *page_list, unsigned nr_pages)
401 {
402 struct inode *inode = file_inode(file);
403 struct ceph_fs_client *fsc = ceph_inode_to_client(inode);
404 int rc = 0;
405 int max = 0;
406
407 if (ceph_inode(inode)->i_inline_version != CEPH_INLINE_NONE)
408 return -EINVAL;
409
410 rc = ceph_readpages_from_fscache(mapping->host, mapping, page_list,
411 &nr_pages);
412
413 if (rc == 0)
414 goto out;
415
416 if (fsc->mount_options->rsize >= PAGE_SIZE)
417 max = (fsc->mount_options->rsize + PAGE_SIZE - 1)
418 >> PAGE_SHIFT;
419
420 dout("readpages %p file %p nr_pages %d max %d\n", inode,
421 file, nr_pages,
422 max);
423 while (!list_empty(page_list)) {
424 rc = start_read(inode, page_list, max);
425 if (rc < 0)
426 goto out;
427 BUG_ON(rc == 0);
428 }
429 out:
430 ceph_fscache_readpages_cancel(inode, page_list);
431
432 dout("readpages %p file %p ret %d\n", inode, file, rc);
433 return rc;
434 }
435
436 /*
437 * Get ref for the oldest snapc for an inode with dirty data... that is, the
438 * only snap context we are allowed to write back.
439 */
440 static struct ceph_snap_context *get_oldest_context(struct inode *inode,
441 loff_t *snap_size)
442 {
443 struct ceph_inode_info *ci = ceph_inode(inode);
444 struct ceph_snap_context *snapc = NULL;
445 struct ceph_cap_snap *capsnap = NULL;
446
447 spin_lock(&ci->i_ceph_lock);
448 list_for_each_entry(capsnap, &ci->i_cap_snaps, ci_item) {
449 dout(" cap_snap %p snapc %p has %d dirty pages\n", capsnap,
450 capsnap->context, capsnap->dirty_pages);
451 if (capsnap->dirty_pages) {
452 snapc = ceph_get_snap_context(capsnap->context);
453 if (snap_size)
454 *snap_size = capsnap->size;
455 break;
456 }
457 }
458 if (!snapc && ci->i_wrbuffer_ref_head) {
459 snapc = ceph_get_snap_context(ci->i_head_snapc);
460 dout(" head snapc %p has %d dirty pages\n",
461 snapc, ci->i_wrbuffer_ref_head);
462 }
463 spin_unlock(&ci->i_ceph_lock);
464 return snapc;
465 }
466
467 /*
468 * Write a single page, but leave the page locked.
469 *
470 * If we get a write error, set the page error bit, but still adjust the
471 * dirty page accounting (i.e., page is no longer dirty).
472 */
473 static int writepage_nounlock(struct page *page, struct writeback_control *wbc)
474 {
475 struct inode *inode;
476 struct ceph_inode_info *ci;
477 struct ceph_fs_client *fsc;
478 struct ceph_osd_client *osdc;
479 struct ceph_snap_context *snapc, *oldest;
480 loff_t page_off = page_offset(page);
481 loff_t snap_size = -1;
482 long writeback_stat;
483 u64 truncate_size;
484 u32 truncate_seq;
485 int err = 0, len = PAGE_SIZE;
486
487 dout("writepage %p idx %lu\n", page, page->index);
488
489 if (!page->mapping || !page->mapping->host) {
490 dout("writepage %p - no mapping\n", page);
491 return -EFAULT;
492 }
493 inode = page->mapping->host;
494 ci = ceph_inode(inode);
495 fsc = ceph_inode_to_client(inode);
496 osdc = &fsc->client->osdc;
497
498 /* verify this is a writeable snap context */
499 snapc = page_snap_context(page);
500 if (snapc == NULL) {
501 dout("writepage %p page %p not dirty?\n", inode, page);
502 goto out;
503 }
504 oldest = get_oldest_context(inode, &snap_size);
505 if (snapc->seq > oldest->seq) {
506 dout("writepage %p page %p snapc %p not writeable - noop\n",
507 inode, page, snapc);
508 /* we should only noop if called by kswapd */
509 WARN_ON((current->flags & PF_MEMALLOC) == 0);
510 ceph_put_snap_context(oldest);
511 goto out;
512 }
513 ceph_put_snap_context(oldest);
514
515 spin_lock(&ci->i_ceph_lock);
516 truncate_seq = ci->i_truncate_seq;
517 truncate_size = ci->i_truncate_size;
518 if (snap_size == -1)
519 snap_size = i_size_read(inode);
520 spin_unlock(&ci->i_ceph_lock);
521
522 /* is this a partial page at end of file? */
523 if (page_off >= snap_size) {
524 dout("%p page eof %llu\n", page, snap_size);
525 goto out;
526 }
527 if (snap_size < page_off + len)
528 len = snap_size - page_off;
529
530 dout("writepage %p page %p index %lu on %llu~%u snapc %p\n",
531 inode, page, page->index, page_off, len, snapc);
532
533 writeback_stat = atomic_long_inc_return(&fsc->writeback_count);
534 if (writeback_stat >
535 CONGESTION_ON_THRESH(fsc->mount_options->congestion_kb))
536 set_bdi_congested(&fsc->backing_dev_info, BLK_RW_ASYNC);
537
538 ceph_readpage_to_fscache(inode, page);
539
540 set_page_writeback(page);
541 err = ceph_osdc_writepages(osdc, ceph_vino(inode),
542 &ci->i_layout, snapc,
543 page_off, len,
544 truncate_seq, truncate_size,
545 &inode->i_mtime, &page, 1);
546 if (err < 0) {
547 dout("writepage setting page/mapping error %d %p\n", err, page);
548 SetPageError(page);
549 mapping_set_error(&inode->i_data, err);
550 if (wbc)
551 wbc->pages_skipped++;
552 } else {
553 dout("writepage cleaned page %p\n", page);
554 err = 0; /* vfs expects us to return 0 */
555 }
556 page->private = 0;
557 ClearPagePrivate(page);
558 end_page_writeback(page);
559 ceph_put_wrbuffer_cap_refs(ci, 1, snapc);
560 ceph_put_snap_context(snapc); /* page's reference */
561 out:
562 return err;
563 }
564
565 static int ceph_writepage(struct page *page, struct writeback_control *wbc)
566 {
567 int err;
568 struct inode *inode = page->mapping->host;
569 BUG_ON(!inode);
570 ihold(inode);
571 err = writepage_nounlock(page, wbc);
572 unlock_page(page);
573 iput(inode);
574 return err;
575 }
576
577
578 /*
579 * lame release_pages helper. release_pages() isn't exported to
580 * modules.
581 */
582 static void ceph_release_pages(struct page **pages, int num)
583 {
584 struct pagevec pvec;
585 int i;
586
587 pagevec_init(&pvec, 0);
588 for (i = 0; i < num; i++) {
589 if (pagevec_add(&pvec, pages[i]) == 0)
590 pagevec_release(&pvec);
591 }
592 pagevec_release(&pvec);
593 }
594
595 /*
596 * async writeback completion handler.
597 *
598 * If we get an error, set the mapping error bit, but not the individual
599 * page error bits.
600 */
601 static void writepages_finish(struct ceph_osd_request *req)
602 {
603 struct inode *inode = req->r_inode;
604 struct ceph_inode_info *ci = ceph_inode(inode);
605 struct ceph_osd_data *osd_data;
606 struct page *page;
607 int num_pages, total_pages = 0;
608 int i, j;
609 int rc = req->r_result;
610 struct ceph_snap_context *snapc = req->r_snapc;
611 struct address_space *mapping = inode->i_mapping;
612 struct ceph_fs_client *fsc = ceph_inode_to_client(inode);
613 bool remove_page;
614
615
616 dout("writepages_finish %p rc %d\n", inode, rc);
617 if (rc < 0)
618 mapping_set_error(mapping, rc);
619
620 /*
621 * We lost the cache cap, need to truncate the page before
622 * it is unlocked, otherwise we'd truncate it later in the
623 * page truncation thread, possibly losing some data that
624 * raced its way in
625 */
626 remove_page = !(ceph_caps_issued(ci) &
627 (CEPH_CAP_FILE_CACHE|CEPH_CAP_FILE_LAZYIO));
628
629 /* clean all pages */
630 for (i = 0; i < req->r_num_ops; i++) {
631 if (req->r_ops[i].op != CEPH_OSD_OP_WRITE)
632 break;
633
634 osd_data = osd_req_op_extent_osd_data(req, i);
635 BUG_ON(osd_data->type != CEPH_OSD_DATA_TYPE_PAGES);
636 num_pages = calc_pages_for((u64)osd_data->alignment,
637 (u64)osd_data->length);
638 total_pages += num_pages;
639 for (j = 0; j < num_pages; j++) {
640 page = osd_data->pages[j];
641 BUG_ON(!page);
642 WARN_ON(!PageUptodate(page));
643
644 if (atomic_long_dec_return(&fsc->writeback_count) <
645 CONGESTION_OFF_THRESH(
646 fsc->mount_options->congestion_kb))
647 clear_bdi_congested(&fsc->backing_dev_info,
648 BLK_RW_ASYNC);
649
650 ceph_put_snap_context(page_snap_context(page));
651 page->private = 0;
652 ClearPagePrivate(page);
653 dout("unlocking %p\n", page);
654 end_page_writeback(page);
655
656 if (remove_page)
657 generic_error_remove_page(inode->i_mapping,
658 page);
659
660 unlock_page(page);
661 }
662 dout("writepages_finish %p wrote %llu bytes cleaned %d pages\n",
663 inode, osd_data->length, rc >= 0 ? num_pages : 0);
664
665 ceph_release_pages(osd_data->pages, num_pages);
666 }
667
668 ceph_put_wrbuffer_cap_refs(ci, total_pages, snapc);
669
670 osd_data = osd_req_op_extent_osd_data(req, 0);
671 if (osd_data->pages_from_pool)
672 mempool_free(osd_data->pages,
673 ceph_sb_to_client(inode->i_sb)->wb_pagevec_pool);
674 else
675 kfree(osd_data->pages);
676 ceph_osdc_put_request(req);
677 }
678
679 /*
680 * initiate async writeback
681 */
682 static int ceph_writepages_start(struct address_space *mapping,
683 struct writeback_control *wbc)
684 {
685 struct inode *inode = mapping->host;
686 struct ceph_inode_info *ci = ceph_inode(inode);
687 struct ceph_fs_client *fsc = ceph_inode_to_client(inode);
688 struct ceph_vino vino = ceph_vino(inode);
689 pgoff_t index, start, end;
690 int range_whole = 0;
691 int should_loop = 1;
692 pgoff_t max_pages = 0, max_pages_ever = 0;
693 struct ceph_snap_context *snapc = NULL, *last_snapc = NULL, *pgsnapc;
694 struct pagevec pvec;
695 int done = 0;
696 int rc = 0;
697 unsigned wsize = 1 << inode->i_blkbits;
698 struct ceph_osd_request *req = NULL;
699 int do_sync = 0;
700 loff_t snap_size, i_size;
701 u64 truncate_size;
702 u32 truncate_seq;
703
704 /*
705 * Include a 'sync' in the OSD request if this is a data
706 * integrity write (e.g., O_SYNC write or fsync()), or if our
707 * cap is being revoked.
708 */
709 if ((wbc->sync_mode == WB_SYNC_ALL) ||
710 ceph_caps_revoking(ci, CEPH_CAP_FILE_BUFFER))
711 do_sync = 1;
712 dout("writepages_start %p dosync=%d (mode=%s)\n",
713 inode, do_sync,
714 wbc->sync_mode == WB_SYNC_NONE ? "NONE" :
715 (wbc->sync_mode == WB_SYNC_ALL ? "ALL" : "HOLD"));
716
717 if (ACCESS_ONCE(fsc->mount_state) == CEPH_MOUNT_SHUTDOWN) {
718 pr_warn("writepage_start %p on forced umount\n", inode);
719 truncate_pagecache(inode, 0);
720 mapping_set_error(mapping, -EIO);
721 return -EIO; /* we're in a forced umount, don't write! */
722 }
723 if (fsc->mount_options->wsize && fsc->mount_options->wsize < wsize)
724 wsize = fsc->mount_options->wsize;
725 if (wsize < PAGE_SIZE)
726 wsize = PAGE_SIZE;
727 max_pages_ever = wsize >> PAGE_SHIFT;
728
729 pagevec_init(&pvec, 0);
730
731 /* where to start/end? */
732 if (wbc->range_cyclic) {
733 start = mapping->writeback_index; /* Start from prev offset */
734 end = -1;
735 dout(" cyclic, start at %lu\n", start);
736 } else {
737 start = wbc->range_start >> PAGE_SHIFT;
738 end = wbc->range_end >> PAGE_SHIFT;
739 if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX)
740 range_whole = 1;
741 should_loop = 0;
742 dout(" not cyclic, %lu to %lu\n", start, end);
743 }
744 index = start;
745
746 retry:
747 /* find oldest snap context with dirty data */
748 ceph_put_snap_context(snapc);
749 snap_size = -1;
750 snapc = get_oldest_context(inode, &snap_size);
751 if (!snapc) {
752 /* hmm, why does writepages get called when there
753 is no dirty data? */
754 dout(" no snap context with dirty data?\n");
755 goto out;
756 }
757 dout(" oldest snapc is %p seq %lld (%d snaps)\n",
758 snapc, snapc->seq, snapc->num_snaps);
759
760 spin_lock(&ci->i_ceph_lock);
761 truncate_seq = ci->i_truncate_seq;
762 truncate_size = ci->i_truncate_size;
763 i_size = i_size_read(inode);
764 spin_unlock(&ci->i_ceph_lock);
765
766 if (last_snapc && snapc != last_snapc) {
767 /* if we switched to a newer snapc, restart our scan at the
768 * start of the original file range. */
769 dout(" snapc differs from last pass, restarting at %lu\n",
770 index);
771 index = start;
772 }
773 last_snapc = snapc;
774
775 while (!done && index <= end) {
776 unsigned i;
777 int first;
778 pgoff_t strip_unit_end = 0;
779 int num_ops = 0, op_idx;
780 int pvec_pages, locked_pages = 0;
781 struct page **pages = NULL, **data_pages;
782 mempool_t *pool = NULL; /* Becomes non-null if mempool used */
783 struct page *page;
784 int want;
785 u64 offset = 0, len = 0;
786
787 max_pages = max_pages_ever;
788
789 get_more_pages:
790 first = -1;
791 want = min(end - index,
792 min((pgoff_t)PAGEVEC_SIZE,
793 max_pages - (pgoff_t)locked_pages) - 1)
794 + 1;
795 pvec_pages = pagevec_lookup_tag(&pvec, mapping, &index,
796 PAGECACHE_TAG_DIRTY,
797 want);
798 dout("pagevec_lookup_tag got %d\n", pvec_pages);
799 if (!pvec_pages && !locked_pages)
800 break;
801 for (i = 0; i < pvec_pages && locked_pages < max_pages; i++) {
802 page = pvec.pages[i];
803 dout("? %p idx %lu\n", page, page->index);
804 if (locked_pages == 0)
805 lock_page(page); /* first page */
806 else if (!trylock_page(page))
807 break;
808
809 /* only dirty pages, or our accounting breaks */
810 if (unlikely(!PageDirty(page)) ||
811 unlikely(page->mapping != mapping)) {
812 dout("!dirty or !mapping %p\n", page);
813 unlock_page(page);
814 break;
815 }
816 if (!wbc->range_cyclic && page->index > end) {
817 dout("end of range %p\n", page);
818 done = 1;
819 unlock_page(page);
820 break;
821 }
822 if (strip_unit_end && (page->index > strip_unit_end)) {
823 dout("end of strip unit %p\n", page);
824 unlock_page(page);
825 break;
826 }
827 if (wbc->sync_mode != WB_SYNC_NONE) {
828 dout("waiting on writeback %p\n", page);
829 wait_on_page_writeback(page);
830 }
831 if (page_offset(page) >=
832 (snap_size == -1 ? i_size : snap_size)) {
833 dout("%p page eof %llu\n", page,
834 (snap_size == -1 ? i_size : snap_size));
835 done = 1;
836 unlock_page(page);
837 break;
838 }
839 if (PageWriteback(page)) {
840 dout("%p under writeback\n", page);
841 unlock_page(page);
842 break;
843 }
844
845 /* only if matching snap context */
846 pgsnapc = page_snap_context(page);
847 if (pgsnapc->seq > snapc->seq) {
848 dout("page snapc %p %lld > oldest %p %lld\n",
849 pgsnapc, pgsnapc->seq, snapc, snapc->seq);
850 unlock_page(page);
851 if (!locked_pages)
852 continue; /* keep looking for snap */
853 break;
854 }
855
856 if (!clear_page_dirty_for_io(page)) {
857 dout("%p !clear_page_dirty_for_io\n", page);
858 unlock_page(page);
859 break;
860 }
861
862 /*
863 * We have something to write. If this is
864 * the first locked page this time through,
865 * calculate max possinle write size and
866 * allocate a page array
867 */
868 if (locked_pages == 0) {
869 u64 objnum;
870 u64 objoff;
871
872 /* prepare async write request */
873 offset = (u64)page_offset(page);
874 len = wsize;
875
876 rc = ceph_calc_file_object_mapping(&ci->i_layout,
877 offset, len,
878 &objnum, &objoff,
879 &len);
880 if (rc < 0) {
881 unlock_page(page);
882 break;
883 }
884
885 num_ops = 1 + do_sync;
886 strip_unit_end = page->index +
887 ((len - 1) >> PAGE_SHIFT);
888
889 BUG_ON(pages);
890 max_pages = calc_pages_for(0, (u64)len);
891 pages = kmalloc(max_pages * sizeof (*pages),
892 GFP_NOFS);
893 if (!pages) {
894 pool = fsc->wb_pagevec_pool;
895 pages = mempool_alloc(pool, GFP_NOFS);
896 BUG_ON(!pages);
897 }
898
899 len = 0;
900 } else if (page->index !=
901 (offset + len) >> PAGE_SHIFT) {
902 if (num_ops >= (pool ? CEPH_OSD_SLAB_OPS :
903 CEPH_OSD_MAX_OPS)) {
904 redirty_page_for_writepage(wbc, page);
905 unlock_page(page);
906 break;
907 }
908
909 num_ops++;
910 offset = (u64)page_offset(page);
911 len = 0;
912 }
913
914 /* note position of first page in pvec */
915 if (first < 0)
916 first = i;
917 dout("%p will write page %p idx %lu\n",
918 inode, page, page->index);
919
920 if (atomic_long_inc_return(&fsc->writeback_count) >
921 CONGESTION_ON_THRESH(
922 fsc->mount_options->congestion_kb)) {
923 set_bdi_congested(&fsc->backing_dev_info,
924 BLK_RW_ASYNC);
925 }
926
927 pages[locked_pages] = page;
928 locked_pages++;
929 len += PAGE_SIZE;
930 }
931
932 /* did we get anything? */
933 if (!locked_pages)
934 goto release_pvec_pages;
935 if (i) {
936 int j;
937 BUG_ON(!locked_pages || first < 0);
938
939 if (pvec_pages && i == pvec_pages &&
940 locked_pages < max_pages) {
941 dout("reached end pvec, trying for more\n");
942 pagevec_reinit(&pvec);
943 goto get_more_pages;
944 }
945
946 /* shift unused pages over in the pvec... we
947 * will need to release them below. */
948 for (j = i; j < pvec_pages; j++) {
949 dout(" pvec leftover page %p\n", pvec.pages[j]);
950 pvec.pages[j-i+first] = pvec.pages[j];
951 }
952 pvec.nr -= i-first;
953 }
954
955 new_request:
956 offset = page_offset(pages[0]);
957 len = wsize;
958
959 req = ceph_osdc_new_request(&fsc->client->osdc,
960 &ci->i_layout, vino,
961 offset, &len, 0, num_ops,
962 CEPH_OSD_OP_WRITE,
963 CEPH_OSD_FLAG_WRITE |
964 CEPH_OSD_FLAG_ONDISK,
965 snapc, truncate_seq,
966 truncate_size, false);
967 if (IS_ERR(req)) {
968 req = ceph_osdc_new_request(&fsc->client->osdc,
969 &ci->i_layout, vino,
970 offset, &len, 0,
971 min(num_ops,
972 CEPH_OSD_SLAB_OPS),
973 CEPH_OSD_OP_WRITE,
974 CEPH_OSD_FLAG_WRITE |
975 CEPH_OSD_FLAG_ONDISK,
976 snapc, truncate_seq,
977 truncate_size, true);
978 BUG_ON(IS_ERR(req));
979 }
980 BUG_ON(len < page_offset(pages[locked_pages - 1]) +
981 PAGE_SIZE - offset);
982
983 req->r_callback = writepages_finish;
984 req->r_inode = inode;
985
986 /* Format the osd request message and submit the write */
987 len = 0;
988 data_pages = pages;
989 op_idx = 0;
990 for (i = 0; i < locked_pages; i++) {
991 u64 cur_offset = page_offset(pages[i]);
992 if (offset + len != cur_offset) {
993 if (op_idx + do_sync + 1 == req->r_num_ops)
994 break;
995 osd_req_op_extent_dup_last(req, op_idx,
996 cur_offset - offset);
997 dout("writepages got pages at %llu~%llu\n",
998 offset, len);
999 osd_req_op_extent_osd_data_pages(req, op_idx,
1000 data_pages, len, 0,
1001 !!pool, false);
1002 osd_req_op_extent_update(req, op_idx, len);
1003
1004 len = 0;
1005 offset = cur_offset;
1006 data_pages = pages + i;
1007 op_idx++;
1008 }
1009
1010 set_page_writeback(pages[i]);
1011 len += PAGE_SIZE;
1012 }
1013
1014 if (snap_size != -1) {
1015 len = min(len, snap_size - offset);
1016 } else if (i == locked_pages) {
1017 /* writepages_finish() clears writeback pages
1018 * according to the data length, so make sure
1019 * data length covers all locked pages */
1020 u64 min_len = len + 1 - PAGE_SIZE;
1021 len = min(len, (u64)i_size_read(inode) - offset);
1022 len = max(len, min_len);
1023 }
1024 dout("writepages got pages at %llu~%llu\n", offset, len);
1025
1026 osd_req_op_extent_osd_data_pages(req, op_idx, data_pages, len,
1027 0, !!pool, false);
1028 osd_req_op_extent_update(req, op_idx, len);
1029
1030 if (do_sync) {
1031 op_idx++;
1032 osd_req_op_init(req, op_idx, CEPH_OSD_OP_STARTSYNC, 0);
1033 }
1034 BUG_ON(op_idx + 1 != req->r_num_ops);
1035
1036 pool = NULL;
1037 if (i < locked_pages) {
1038 BUG_ON(num_ops <= req->r_num_ops);
1039 num_ops -= req->r_num_ops;
1040 num_ops += do_sync;
1041 locked_pages -= i;
1042
1043 /* allocate new pages array for next request */
1044 data_pages = pages;
1045 pages = kmalloc(locked_pages * sizeof (*pages),
1046 GFP_NOFS);
1047 if (!pages) {
1048 pool = fsc->wb_pagevec_pool;
1049 pages = mempool_alloc(pool, GFP_NOFS);
1050 BUG_ON(!pages);
1051 }
1052 memcpy(pages, data_pages + i,
1053 locked_pages * sizeof(*pages));
1054 memset(data_pages + i, 0,
1055 locked_pages * sizeof(*pages));
1056 } else {
1057 BUG_ON(num_ops != req->r_num_ops);
1058 index = pages[i - 1]->index + 1;
1059 /* request message now owns the pages array */
1060 pages = NULL;
1061 }
1062
1063 req->r_mtime = inode->i_mtime;
1064 rc = ceph_osdc_start_request(&fsc->client->osdc, req, true);
1065 BUG_ON(rc);
1066 req = NULL;
1067
1068 wbc->nr_to_write -= i;
1069 if (pages)
1070 goto new_request;
1071
1072 if (wbc->nr_to_write <= 0)
1073 done = 1;
1074
1075 release_pvec_pages:
1076 dout("pagevec_release on %d pages (%p)\n", (int)pvec.nr,
1077 pvec.nr ? pvec.pages[0] : NULL);
1078 pagevec_release(&pvec);
1079
1080 if (locked_pages && !done)
1081 goto retry;
1082 }
1083
1084 if (should_loop && !done) {
1085 /* more to do; loop back to beginning of file */
1086 dout("writepages looping back to beginning of file\n");
1087 should_loop = 0;
1088 index = 0;
1089 goto retry;
1090 }
1091
1092 if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0))
1093 mapping->writeback_index = index;
1094
1095 out:
1096 ceph_osdc_put_request(req);
1097 ceph_put_snap_context(snapc);
1098 dout("writepages done, rc = %d\n", rc);
1099 return rc;
1100 }
1101
1102
1103
1104 /*
1105 * See if a given @snapc is either writeable, or already written.
1106 */
1107 static int context_is_writeable_or_written(struct inode *inode,
1108 struct ceph_snap_context *snapc)
1109 {
1110 struct ceph_snap_context *oldest = get_oldest_context(inode, NULL);
1111 int ret = !oldest || snapc->seq <= oldest->seq;
1112
1113 ceph_put_snap_context(oldest);
1114 return ret;
1115 }
1116
1117 /*
1118 * We are only allowed to write into/dirty the page if the page is
1119 * clean, or already dirty within the same snap context.
1120 *
1121 * called with page locked.
1122 * return success with page locked,
1123 * or any failure (incl -EAGAIN) with page unlocked.
1124 */
1125 static int ceph_update_writeable_page(struct file *file,
1126 loff_t pos, unsigned len,
1127 struct page *page)
1128 {
1129 struct inode *inode = file_inode(file);
1130 struct ceph_inode_info *ci = ceph_inode(inode);
1131 loff_t page_off = pos & PAGE_MASK;
1132 int pos_in_page = pos & ~PAGE_MASK;
1133 int end_in_page = pos_in_page + len;
1134 loff_t i_size;
1135 int r;
1136 struct ceph_snap_context *snapc, *oldest;
1137
1138 retry_locked:
1139 /* writepages currently holds page lock, but if we change that later, */
1140 wait_on_page_writeback(page);
1141
1142 snapc = page_snap_context(page);
1143 if (snapc && snapc != ci->i_head_snapc) {
1144 /*
1145 * this page is already dirty in another (older) snap
1146 * context! is it writeable now?
1147 */
1148 oldest = get_oldest_context(inode, NULL);
1149
1150 if (snapc->seq > oldest->seq) {
1151 ceph_put_snap_context(oldest);
1152 dout(" page %p snapc %p not current or oldest\n",
1153 page, snapc);
1154 /*
1155 * queue for writeback, and wait for snapc to
1156 * be writeable or written
1157 */
1158 snapc = ceph_get_snap_context(snapc);
1159 unlock_page(page);
1160 ceph_queue_writeback(inode);
1161 r = wait_event_interruptible(ci->i_cap_wq,
1162 context_is_writeable_or_written(inode, snapc));
1163 ceph_put_snap_context(snapc);
1164 if (r == -ERESTARTSYS)
1165 return r;
1166 return -EAGAIN;
1167 }
1168 ceph_put_snap_context(oldest);
1169
1170 /* yay, writeable, do it now (without dropping page lock) */
1171 dout(" page %p snapc %p not current, but oldest\n",
1172 page, snapc);
1173 if (!clear_page_dirty_for_io(page))
1174 goto retry_locked;
1175 r = writepage_nounlock(page, NULL);
1176 if (r < 0)
1177 goto fail_nosnap;
1178 goto retry_locked;
1179 }
1180
1181 if (PageUptodate(page)) {
1182 dout(" page %p already uptodate\n", page);
1183 return 0;
1184 }
1185
1186 /* full page? */
1187 if (pos_in_page == 0 && len == PAGE_SIZE)
1188 return 0;
1189
1190 /* past end of file? */
1191 i_size = i_size_read(inode);
1192
1193 if (page_off >= i_size ||
1194 (pos_in_page == 0 && (pos+len) >= i_size &&
1195 end_in_page - pos_in_page != PAGE_SIZE)) {
1196 dout(" zeroing %p 0 - %d and %d - %d\n",
1197 page, pos_in_page, end_in_page, (int)PAGE_SIZE);
1198 zero_user_segments(page,
1199 0, pos_in_page,
1200 end_in_page, PAGE_SIZE);
1201 return 0;
1202 }
1203
1204 /* we need to read it. */
1205 r = readpage_nounlock(file, page);
1206 if (r < 0)
1207 goto fail_nosnap;
1208 goto retry_locked;
1209 fail_nosnap:
1210 unlock_page(page);
1211 return r;
1212 }
1213
1214 /*
1215 * We are only allowed to write into/dirty the page if the page is
1216 * clean, or already dirty within the same snap context.
1217 */
1218 static int ceph_write_begin(struct file *file, struct address_space *mapping,
1219 loff_t pos, unsigned len, unsigned flags,
1220 struct page **pagep, void **fsdata)
1221 {
1222 struct inode *inode = file_inode(file);
1223 struct page *page;
1224 pgoff_t index = pos >> PAGE_SHIFT;
1225 int r;
1226
1227 do {
1228 /* get a page */
1229 page = grab_cache_page_write_begin(mapping, index, 0);
1230 if (!page)
1231 return -ENOMEM;
1232
1233 dout("write_begin file %p inode %p page %p %d~%d\n", file,
1234 inode, page, (int)pos, (int)len);
1235
1236 r = ceph_update_writeable_page(file, pos, len, page);
1237 if (r < 0)
1238 put_page(page);
1239 else
1240 *pagep = page;
1241 } while (r == -EAGAIN);
1242
1243 return r;
1244 }
1245
1246 /*
1247 * we don't do anything in here that simple_write_end doesn't do
1248 * except adjust dirty page accounting
1249 */
1250 static int ceph_write_end(struct file *file, struct address_space *mapping,
1251 loff_t pos, unsigned len, unsigned copied,
1252 struct page *page, void *fsdata)
1253 {
1254 struct inode *inode = file_inode(file);
1255 unsigned from = pos & (PAGE_SIZE - 1);
1256 int check_cap = 0;
1257
1258 dout("write_end file %p inode %p page %p %d~%d (%d)\n", file,
1259 inode, page, (int)pos, (int)copied, (int)len);
1260
1261 /* zero the stale part of the page if we did a short copy */
1262 if (copied < len)
1263 zero_user_segment(page, from+copied, len);
1264
1265 /* did file size increase? */
1266 if (pos+copied > i_size_read(inode))
1267 check_cap = ceph_inode_set_size(inode, pos+copied);
1268
1269 if (!PageUptodate(page))
1270 SetPageUptodate(page);
1271
1272 set_page_dirty(page);
1273
1274 unlock_page(page);
1275 put_page(page);
1276
1277 if (check_cap)
1278 ceph_check_caps(ceph_inode(inode), CHECK_CAPS_AUTHONLY, NULL);
1279
1280 return copied;
1281 }
1282
1283 /*
1284 * we set .direct_IO to indicate direct io is supported, but since we
1285 * intercept O_DIRECT reads and writes early, this function should
1286 * never get called.
1287 */
1288 static ssize_t ceph_direct_io(struct kiocb *iocb, struct iov_iter *iter,
1289 loff_t pos)
1290 {
1291 WARN_ON(1);
1292 return -EINVAL;
1293 }
1294
1295 const struct address_space_operations ceph_aops = {
1296 .readpage = ceph_readpage,
1297 .readpages = ceph_readpages,
1298 .writepage = ceph_writepage,
1299 .writepages = ceph_writepages_start,
1300 .write_begin = ceph_write_begin,
1301 .write_end = ceph_write_end,
1302 .set_page_dirty = ceph_set_page_dirty,
1303 .invalidatepage = ceph_invalidatepage,
1304 .releasepage = ceph_releasepage,
1305 .direct_IO = ceph_direct_io,
1306 };
1307
1308
1309 /*
1310 * vm ops
1311 */
1312 static int ceph_filemap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
1313 {
1314 struct inode *inode = file_inode(vma->vm_file);
1315 struct ceph_inode_info *ci = ceph_inode(inode);
1316 struct ceph_file_info *fi = vma->vm_file->private_data;
1317 struct page *pinned_page = NULL;
1318 loff_t off = vmf->pgoff << PAGE_SHIFT;
1319 int want, got, ret;
1320
1321 dout("filemap_fault %p %llx.%llx %llu~%zd trying to get caps\n",
1322 inode, ceph_vinop(inode), off, (size_t)PAGE_SIZE);
1323 if (fi->fmode & CEPH_FILE_MODE_LAZY)
1324 want = CEPH_CAP_FILE_CACHE | CEPH_CAP_FILE_LAZYIO;
1325 else
1326 want = CEPH_CAP_FILE_CACHE;
1327 while (1) {
1328 got = 0;
1329 ret = ceph_get_caps(ci, CEPH_CAP_FILE_RD, want,
1330 -1, &got, &pinned_page);
1331 if (ret == 0)
1332 break;
1333 if (ret != -ERESTARTSYS) {
1334 WARN_ON(1);
1335 return VM_FAULT_SIGBUS;
1336 }
1337 }
1338 dout("filemap_fault %p %llu~%zd got cap refs on %s\n",
1339 inode, off, (size_t)PAGE_SIZE, ceph_cap_string(got));
1340
1341 if ((got & (CEPH_CAP_FILE_CACHE | CEPH_CAP_FILE_LAZYIO)) ||
1342 ci->i_inline_version == CEPH_INLINE_NONE)
1343 ret = filemap_fault(vma, vmf);
1344 else
1345 ret = -EAGAIN;
1346
1347 dout("filemap_fault %p %llu~%zd dropping cap refs on %s ret %d\n",
1348 inode, off, (size_t)PAGE_SIZE, ceph_cap_string(got), ret);
1349 if (pinned_page)
1350 put_page(pinned_page);
1351 ceph_put_cap_refs(ci, got);
1352
1353 if (ret != -EAGAIN)
1354 return ret;
1355
1356 /* read inline data */
1357 if (off >= PAGE_SIZE) {
1358 /* does not support inline data > PAGE_SIZE */
1359 ret = VM_FAULT_SIGBUS;
1360 } else {
1361 int ret1;
1362 struct address_space *mapping = inode->i_mapping;
1363 struct page *page = find_or_create_page(mapping, 0,
1364 mapping_gfp_constraint(mapping,
1365 ~__GFP_FS));
1366 if (!page) {
1367 ret = VM_FAULT_OOM;
1368 goto out;
1369 }
1370 ret1 = __ceph_do_getattr(inode, page,
1371 CEPH_STAT_CAP_INLINE_DATA, true);
1372 if (ret1 < 0 || off >= i_size_read(inode)) {
1373 unlock_page(page);
1374 put_page(page);
1375 ret = VM_FAULT_SIGBUS;
1376 goto out;
1377 }
1378 if (ret1 < PAGE_SIZE)
1379 zero_user_segment(page, ret1, PAGE_SIZE);
1380 else
1381 flush_dcache_page(page);
1382 SetPageUptodate(page);
1383 vmf->page = page;
1384 ret = VM_FAULT_MAJOR | VM_FAULT_LOCKED;
1385 }
1386 out:
1387 dout("filemap_fault %p %llu~%zd read inline data ret %d\n",
1388 inode, off, (size_t)PAGE_SIZE, ret);
1389 return ret;
1390 }
1391
1392 /*
1393 * Reuse write_begin here for simplicity.
1394 */
1395 static int ceph_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
1396 {
1397 struct inode *inode = file_inode(vma->vm_file);
1398 struct ceph_inode_info *ci = ceph_inode(inode);
1399 struct ceph_file_info *fi = vma->vm_file->private_data;
1400 struct ceph_cap_flush *prealloc_cf;
1401 struct page *page = vmf->page;
1402 loff_t off = page_offset(page);
1403 loff_t size = i_size_read(inode);
1404 size_t len;
1405 int want, got, ret;
1406
1407 prealloc_cf = ceph_alloc_cap_flush();
1408 if (!prealloc_cf)
1409 return VM_FAULT_SIGBUS;
1410
1411 if (ci->i_inline_version != CEPH_INLINE_NONE) {
1412 struct page *locked_page = NULL;
1413 if (off == 0) {
1414 lock_page(page);
1415 locked_page = page;
1416 }
1417 ret = ceph_uninline_data(vma->vm_file, locked_page);
1418 if (locked_page)
1419 unlock_page(locked_page);
1420 if (ret < 0) {
1421 ret = VM_FAULT_SIGBUS;
1422 goto out_free;
1423 }
1424 }
1425
1426 if (off + PAGE_SIZE <= size)
1427 len = PAGE_SIZE;
1428 else
1429 len = size & ~PAGE_MASK;
1430
1431 dout("page_mkwrite %p %llx.%llx %llu~%zd getting caps i_size %llu\n",
1432 inode, ceph_vinop(inode), off, len, size);
1433 if (fi->fmode & CEPH_FILE_MODE_LAZY)
1434 want = CEPH_CAP_FILE_BUFFER | CEPH_CAP_FILE_LAZYIO;
1435 else
1436 want = CEPH_CAP_FILE_BUFFER;
1437 while (1) {
1438 got = 0;
1439 ret = ceph_get_caps(ci, CEPH_CAP_FILE_WR, want, off + len,
1440 &got, NULL);
1441 if (ret == 0)
1442 break;
1443 if (ret != -ERESTARTSYS) {
1444 WARN_ON(1);
1445 ret = VM_FAULT_SIGBUS;
1446 goto out_free;
1447 }
1448 }
1449 dout("page_mkwrite %p %llu~%zd got cap refs on %s\n",
1450 inode, off, len, ceph_cap_string(got));
1451
1452 /* Update time before taking page lock */
1453 file_update_time(vma->vm_file);
1454
1455 lock_page(page);
1456
1457 ret = VM_FAULT_NOPAGE;
1458 if ((off > size) ||
1459 (page->mapping != inode->i_mapping)) {
1460 unlock_page(page);
1461 goto out;
1462 }
1463
1464 ret = ceph_update_writeable_page(vma->vm_file, off, len, page);
1465 if (ret >= 0) {
1466 /* success. we'll keep the page locked. */
1467 set_page_dirty(page);
1468 ret = VM_FAULT_LOCKED;
1469 } else {
1470 if (ret == -ENOMEM)
1471 ret = VM_FAULT_OOM;
1472 else
1473 ret = VM_FAULT_SIGBUS;
1474 }
1475 out:
1476 if (ret == VM_FAULT_LOCKED ||
1477 ci->i_inline_version != CEPH_INLINE_NONE) {
1478 int dirty;
1479 spin_lock(&ci->i_ceph_lock);
1480 ci->i_inline_version = CEPH_INLINE_NONE;
1481 dirty = __ceph_mark_dirty_caps(ci, CEPH_CAP_FILE_WR,
1482 &prealloc_cf);
1483 spin_unlock(&ci->i_ceph_lock);
1484 if (dirty)
1485 __mark_inode_dirty(inode, dirty);
1486 }
1487
1488 dout("page_mkwrite %p %llu~%zd dropping cap refs on %s ret %d\n",
1489 inode, off, len, ceph_cap_string(got), ret);
1490 ceph_put_cap_refs(ci, got);
1491 out_free:
1492 ceph_free_cap_flush(prealloc_cf);
1493
1494 return ret;
1495 }
1496
1497 void ceph_fill_inline_data(struct inode *inode, struct page *locked_page,
1498 char *data, size_t len)
1499 {
1500 struct address_space *mapping = inode->i_mapping;
1501 struct page *page;
1502
1503 if (locked_page) {
1504 page = locked_page;
1505 } else {
1506 if (i_size_read(inode) == 0)
1507 return;
1508 page = find_or_create_page(mapping, 0,
1509 mapping_gfp_constraint(mapping,
1510 ~__GFP_FS));
1511 if (!page)
1512 return;
1513 if (PageUptodate(page)) {
1514 unlock_page(page);
1515 put_page(page);
1516 return;
1517 }
1518 }
1519
1520 dout("fill_inline_data %p %llx.%llx len %zu locked_page %p\n",
1521 inode, ceph_vinop(inode), len, locked_page);
1522
1523 if (len > 0) {
1524 void *kaddr = kmap_atomic(page);
1525 memcpy(kaddr, data, len);
1526 kunmap_atomic(kaddr);
1527 }
1528
1529 if (page != locked_page) {
1530 if (len < PAGE_SIZE)
1531 zero_user_segment(page, len, PAGE_SIZE);
1532 else
1533 flush_dcache_page(page);
1534
1535 SetPageUptodate(page);
1536 unlock_page(page);
1537 put_page(page);
1538 }
1539 }
1540
1541 int ceph_uninline_data(struct file *filp, struct page *locked_page)
1542 {
1543 struct inode *inode = file_inode(filp);
1544 struct ceph_inode_info *ci = ceph_inode(inode);
1545 struct ceph_fs_client *fsc = ceph_inode_to_client(inode);
1546 struct ceph_osd_request *req;
1547 struct page *page = NULL;
1548 u64 len, inline_version;
1549 int err = 0;
1550 bool from_pagecache = false;
1551
1552 spin_lock(&ci->i_ceph_lock);
1553 inline_version = ci->i_inline_version;
1554 spin_unlock(&ci->i_ceph_lock);
1555
1556 dout("uninline_data %p %llx.%llx inline_version %llu\n",
1557 inode, ceph_vinop(inode), inline_version);
1558
1559 if (inline_version == 1 || /* initial version, no data */
1560 inline_version == CEPH_INLINE_NONE)
1561 goto out;
1562
1563 if (locked_page) {
1564 page = locked_page;
1565 WARN_ON(!PageUptodate(page));
1566 } else if (ceph_caps_issued(ci) &
1567 (CEPH_CAP_FILE_CACHE|CEPH_CAP_FILE_LAZYIO)) {
1568 page = find_get_page(inode->i_mapping, 0);
1569 if (page) {
1570 if (PageUptodate(page)) {
1571 from_pagecache = true;
1572 lock_page(page);
1573 } else {
1574 put_page(page);
1575 page = NULL;
1576 }
1577 }
1578 }
1579
1580 if (page) {
1581 len = i_size_read(inode);
1582 if (len > PAGE_SIZE)
1583 len = PAGE_SIZE;
1584 } else {
1585 page = __page_cache_alloc(GFP_NOFS);
1586 if (!page) {
1587 err = -ENOMEM;
1588 goto out;
1589 }
1590 err = __ceph_do_getattr(inode, page,
1591 CEPH_STAT_CAP_INLINE_DATA, true);
1592 if (err < 0) {
1593 /* no inline data */
1594 if (err == -ENODATA)
1595 err = 0;
1596 goto out;
1597 }
1598 len = err;
1599 }
1600
1601 req = ceph_osdc_new_request(&fsc->client->osdc, &ci->i_layout,
1602 ceph_vino(inode), 0, &len, 0, 1,
1603 CEPH_OSD_OP_CREATE,
1604 CEPH_OSD_FLAG_ONDISK | CEPH_OSD_FLAG_WRITE,
1605 NULL, 0, 0, false);
1606 if (IS_ERR(req)) {
1607 err = PTR_ERR(req);
1608 goto out;
1609 }
1610
1611 req->r_mtime = inode->i_mtime;
1612 err = ceph_osdc_start_request(&fsc->client->osdc, req, false);
1613 if (!err)
1614 err = ceph_osdc_wait_request(&fsc->client->osdc, req);
1615 ceph_osdc_put_request(req);
1616 if (err < 0)
1617 goto out;
1618
1619 req = ceph_osdc_new_request(&fsc->client->osdc, &ci->i_layout,
1620 ceph_vino(inode), 0, &len, 1, 3,
1621 CEPH_OSD_OP_WRITE,
1622 CEPH_OSD_FLAG_ONDISK | CEPH_OSD_FLAG_WRITE,
1623 NULL, ci->i_truncate_seq,
1624 ci->i_truncate_size, false);
1625 if (IS_ERR(req)) {
1626 err = PTR_ERR(req);
1627 goto out;
1628 }
1629
1630 osd_req_op_extent_osd_data_pages(req, 1, &page, len, 0, false, false);
1631
1632 {
1633 __le64 xattr_buf = cpu_to_le64(inline_version);
1634 err = osd_req_op_xattr_init(req, 0, CEPH_OSD_OP_CMPXATTR,
1635 "inline_version", &xattr_buf,
1636 sizeof(xattr_buf),
1637 CEPH_OSD_CMPXATTR_OP_GT,
1638 CEPH_OSD_CMPXATTR_MODE_U64);
1639 if (err)
1640 goto out_put;
1641 }
1642
1643 {
1644 char xattr_buf[32];
1645 int xattr_len = snprintf(xattr_buf, sizeof(xattr_buf),
1646 "%llu", inline_version);
1647 err = osd_req_op_xattr_init(req, 2, CEPH_OSD_OP_SETXATTR,
1648 "inline_version",
1649 xattr_buf, xattr_len, 0, 0);
1650 if (err)
1651 goto out_put;
1652 }
1653
1654 req->r_mtime = inode->i_mtime;
1655 err = ceph_osdc_start_request(&fsc->client->osdc, req, false);
1656 if (!err)
1657 err = ceph_osdc_wait_request(&fsc->client->osdc, req);
1658 out_put:
1659 ceph_osdc_put_request(req);
1660 if (err == -ECANCELED)
1661 err = 0;
1662 out:
1663 if (page && page != locked_page) {
1664 if (from_pagecache) {
1665 unlock_page(page);
1666 put_page(page);
1667 } else
1668 __free_pages(page, 0);
1669 }
1670
1671 dout("uninline_data %p %llx.%llx inline_version %llu = %d\n",
1672 inode, ceph_vinop(inode), inline_version, err);
1673 return err;
1674 }
1675
1676 static const struct vm_operations_struct ceph_vmops = {
1677 .fault = ceph_filemap_fault,
1678 .page_mkwrite = ceph_page_mkwrite,
1679 };
1680
1681 int ceph_mmap(struct file *file, struct vm_area_struct *vma)
1682 {
1683 struct address_space *mapping = file->f_mapping;
1684
1685 if (!mapping->a_ops->readpage)
1686 return -ENOEXEC;
1687 file_accessed(file);
1688 vma->vm_ops = &ceph_vmops;
1689 return 0;
1690 }
1691
1692 enum {
1693 POOL_READ = 1,
1694 POOL_WRITE = 2,
1695 };
1696
1697 static int __ceph_pool_perm_get(struct ceph_inode_info *ci, u32 pool)
1698 {
1699 struct ceph_fs_client *fsc = ceph_inode_to_client(&ci->vfs_inode);
1700 struct ceph_mds_client *mdsc = fsc->mdsc;
1701 struct ceph_osd_request *rd_req = NULL, *wr_req = NULL;
1702 struct rb_node **p, *parent;
1703 struct ceph_pool_perm *perm;
1704 struct page **pages;
1705 int err = 0, err2 = 0, have = 0;
1706
1707 down_read(&mdsc->pool_perm_rwsem);
1708 p = &mdsc->pool_perm_tree.rb_node;
1709 while (*p) {
1710 perm = rb_entry(*p, struct ceph_pool_perm, node);
1711 if (pool < perm->pool)
1712 p = &(*p)->rb_left;
1713 else if (pool > perm->pool)
1714 p = &(*p)->rb_right;
1715 else {
1716 have = perm->perm;
1717 break;
1718 }
1719 }
1720 up_read(&mdsc->pool_perm_rwsem);
1721 if (*p)
1722 goto out;
1723
1724 dout("__ceph_pool_perm_get pool %u no perm cached\n", pool);
1725
1726 down_write(&mdsc->pool_perm_rwsem);
1727 parent = NULL;
1728 while (*p) {
1729 parent = *p;
1730 perm = rb_entry(parent, struct ceph_pool_perm, node);
1731 if (pool < perm->pool)
1732 p = &(*p)->rb_left;
1733 else if (pool > perm->pool)
1734 p = &(*p)->rb_right;
1735 else {
1736 have = perm->perm;
1737 break;
1738 }
1739 }
1740 if (*p) {
1741 up_write(&mdsc->pool_perm_rwsem);
1742 goto out;
1743 }
1744
1745 rd_req = ceph_osdc_alloc_request(&fsc->client->osdc, NULL,
1746 1, false, GFP_NOFS);
1747 if (!rd_req) {
1748 err = -ENOMEM;
1749 goto out_unlock;
1750 }
1751
1752 rd_req->r_flags = CEPH_OSD_FLAG_READ;
1753 osd_req_op_init(rd_req, 0, CEPH_OSD_OP_STAT, 0);
1754 rd_req->r_base_oloc.pool = pool;
1755 ceph_oid_printf(&rd_req->r_base_oid, "%llx.00000000", ci->i_vino.ino);
1756
1757 err = ceph_osdc_alloc_messages(rd_req, GFP_NOFS);
1758 if (err)
1759 goto out_unlock;
1760
1761 wr_req = ceph_osdc_alloc_request(&fsc->client->osdc, NULL,
1762 1, false, GFP_NOFS);
1763 if (!wr_req) {
1764 err = -ENOMEM;
1765 goto out_unlock;
1766 }
1767
1768 wr_req->r_flags = CEPH_OSD_FLAG_WRITE | CEPH_OSD_FLAG_ACK;
1769 osd_req_op_init(wr_req, 0, CEPH_OSD_OP_CREATE, CEPH_OSD_OP_FLAG_EXCL);
1770 ceph_oloc_copy(&wr_req->r_base_oloc, &rd_req->r_base_oloc);
1771 ceph_oid_copy(&wr_req->r_base_oid, &rd_req->r_base_oid);
1772
1773 err = ceph_osdc_alloc_messages(wr_req, GFP_NOFS);
1774 if (err)
1775 goto out_unlock;
1776
1777 /* one page should be large enough for STAT data */
1778 pages = ceph_alloc_page_vector(1, GFP_KERNEL);
1779 if (IS_ERR(pages)) {
1780 err = PTR_ERR(pages);
1781 goto out_unlock;
1782 }
1783
1784 osd_req_op_raw_data_in_pages(rd_req, 0, pages, PAGE_SIZE,
1785 0, false, true);
1786 err = ceph_osdc_start_request(&fsc->client->osdc, rd_req, false);
1787
1788 wr_req->r_mtime = ci->vfs_inode.i_mtime;
1789 err2 = ceph_osdc_start_request(&fsc->client->osdc, wr_req, false);
1790
1791 if (!err)
1792 err = ceph_osdc_wait_request(&fsc->client->osdc, rd_req);
1793 if (!err2)
1794 err2 = ceph_osdc_wait_request(&fsc->client->osdc, wr_req);
1795
1796 if (err >= 0 || err == -ENOENT)
1797 have |= POOL_READ;
1798 else if (err != -EPERM)
1799 goto out_unlock;
1800
1801 if (err2 == 0 || err2 == -EEXIST)
1802 have |= POOL_WRITE;
1803 else if (err2 != -EPERM) {
1804 err = err2;
1805 goto out_unlock;
1806 }
1807
1808 perm = kmalloc(sizeof(*perm), GFP_NOFS);
1809 if (!perm) {
1810 err = -ENOMEM;
1811 goto out_unlock;
1812 }
1813
1814 perm->pool = pool;
1815 perm->perm = have;
1816 rb_link_node(&perm->node, parent, p);
1817 rb_insert_color(&perm->node, &mdsc->pool_perm_tree);
1818 err = 0;
1819 out_unlock:
1820 up_write(&mdsc->pool_perm_rwsem);
1821
1822 ceph_osdc_put_request(rd_req);
1823 ceph_osdc_put_request(wr_req);
1824 out:
1825 if (!err)
1826 err = have;
1827 dout("__ceph_pool_perm_get pool %u result = %d\n", pool, err);
1828 return err;
1829 }
1830
1831 int ceph_pool_perm_check(struct ceph_inode_info *ci, int need)
1832 {
1833 u32 pool;
1834 int ret, flags;
1835
1836 /* does not support pool namespace yet */
1837 if (ci->i_pool_ns_len)
1838 return -EIO;
1839
1840 if (ceph_test_mount_opt(ceph_inode_to_client(&ci->vfs_inode),
1841 NOPOOLPERM))
1842 return 0;
1843
1844 spin_lock(&ci->i_ceph_lock);
1845 flags = ci->i_ceph_flags;
1846 pool = ceph_file_layout_pg_pool(ci->i_layout);
1847 spin_unlock(&ci->i_ceph_lock);
1848 check:
1849 if (flags & CEPH_I_POOL_PERM) {
1850 if ((need & CEPH_CAP_FILE_RD) && !(flags & CEPH_I_POOL_RD)) {
1851 dout("ceph_pool_perm_check pool %u no read perm\n",
1852 pool);
1853 return -EPERM;
1854 }
1855 if ((need & CEPH_CAP_FILE_WR) && !(flags & CEPH_I_POOL_WR)) {
1856 dout("ceph_pool_perm_check pool %u no write perm\n",
1857 pool);
1858 return -EPERM;
1859 }
1860 return 0;
1861 }
1862
1863 ret = __ceph_pool_perm_get(ci, pool);
1864 if (ret < 0)
1865 return ret;
1866
1867 flags = CEPH_I_POOL_PERM;
1868 if (ret & POOL_READ)
1869 flags |= CEPH_I_POOL_RD;
1870 if (ret & POOL_WRITE)
1871 flags |= CEPH_I_POOL_WR;
1872
1873 spin_lock(&ci->i_ceph_lock);
1874 if (pool == ceph_file_layout_pg_pool(ci->i_layout)) {
1875 ci->i_ceph_flags = flags;
1876 } else {
1877 pool = ceph_file_layout_pg_pool(ci->i_layout);
1878 flags = ci->i_ceph_flags;
1879 }
1880 spin_unlock(&ci->i_ceph_lock);
1881 goto check;
1882 }
1883
1884 void ceph_pool_perm_destroy(struct ceph_mds_client *mdsc)
1885 {
1886 struct ceph_pool_perm *perm;
1887 struct rb_node *n;
1888
1889 while (!RB_EMPTY_ROOT(&mdsc->pool_perm_tree)) {
1890 n = rb_first(&mdsc->pool_perm_tree);
1891 perm = rb_entry(n, struct ceph_pool_perm, node);
1892 rb_erase(n, &mdsc->pool_perm_tree);
1893 kfree(perm);
1894 }
1895 }
This page took 0.161871 seconds and 6 git commands to generate.