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