1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/ceph/ceph_debug.h>
4 #include <linux/backing-dev.h>
7 #include <linux/pagemap.h>
8 #include <linux/writeback.h> /* generic_writepages */
9 #include <linux/slab.h>
10 #include <linux/pagevec.h>
11 #include <linux/task_io_accounting_ops.h>
12 #include <linux/signal.h>
13 #include <linux/iversion.h>
14 #include <linux/ktime.h>
17 #include "mds_client.h"
20 #include <linux/ceph/osd_client.h>
21 #include <linux/ceph/striper.h>
24 * Ceph address space ops.
26 * There are a few funny things going on here.
28 * The page->private field is used to reference a struct
29 * ceph_snap_context for _every_ dirty page. This indicates which
30 * snapshot the page was logically dirtied in, and thus which snap
31 * context needs to be associated with the osd write during writeback.
33 * Similarly, struct ceph_inode_info maintains a set of counters to
34 * count dirty pages on the inode. In the absence of snapshots,
35 * i_wrbuffer_ref == i_wrbuffer_ref_head == the dirty page count.
37 * When a snapshot is taken (that is, when the client receives
38 * notification that a snapshot was taken), each inode with caps and
39 * with dirty pages (dirty pages implies there is a cap) gets a new
40 * ceph_cap_snap in the i_cap_snaps list (which is sorted in ascending
41 * order, new snaps go to the tail). The i_wrbuffer_ref_head count is
42 * moved to capsnap->dirty. (Unless a sync write is currently in
43 * progress. In that case, the capsnap is said to be "pending", new
44 * writes cannot start, and the capsnap isn't "finalized" until the
45 * write completes (or fails) and a final size/mtime for the inode for
46 * that snap can be settled upon.) i_wrbuffer_ref_head is reset to 0.
48 * On writeback, we must submit writes to the osd IN SNAP ORDER. So,
49 * we look for the first capsnap in i_cap_snaps and write out pages in
50 * that snap context _only_. Then we move on to the next capsnap,
51 * eventually reaching the "live" or "head" context (i.e., pages that
52 * are not yet snapped) and are writing the most recently dirtied
55 * Invalidate and so forth must take care to ensure the dirty page
56 * accounting is preserved.
59 #define CONGESTION_ON_THRESH(congestion_kb) (congestion_kb >> (PAGE_SHIFT-10))
60 #define CONGESTION_OFF_THRESH(congestion_kb) \
61 (CONGESTION_ON_THRESH(congestion_kb) - \
62 (CONGESTION_ON_THRESH(congestion_kb) >> 2))
64 static inline struct ceph_snap_context *page_snap_context(struct page *page)
66 if (PagePrivate(page))
67 return (void *)page->private;
72 * Dirty a page. Optimistically adjust accounting, on the assumption
73 * that we won't race with invalidate. If we do, readjust.
75 static int ceph_set_page_dirty(struct page *page)
77 struct address_space *mapping = page->mapping;
79 struct ceph_inode_info *ci;
80 struct ceph_snap_context *snapc;
83 if (unlikely(!mapping))
84 return !TestSetPageDirty(page);
86 if (PageDirty(page)) {
87 dout("%p set_page_dirty %p idx %lu -- already dirty\n",
88 mapping->host, page, page->index);
89 BUG_ON(!PagePrivate(page));
93 inode = mapping->host;
94 ci = ceph_inode(inode);
97 spin_lock(&ci->i_ceph_lock);
98 BUG_ON(ci->i_wr_ref == 0); // caller should hold Fw reference
99 if (__ceph_have_pending_cap_snap(ci)) {
100 struct ceph_cap_snap *capsnap =
101 list_last_entry(&ci->i_cap_snaps,
102 struct ceph_cap_snap,
104 snapc = ceph_get_snap_context(capsnap->context);
105 capsnap->dirty_pages++;
107 BUG_ON(!ci->i_head_snapc);
108 snapc = ceph_get_snap_context(ci->i_head_snapc);
109 ++ci->i_wrbuffer_ref_head;
111 if (ci->i_wrbuffer_ref == 0)
113 ++ci->i_wrbuffer_ref;
114 dout("%p set_page_dirty %p idx %lu head %d/%d -> %d/%d "
115 "snapc %p seq %lld (%d snaps)\n",
116 mapping->host, page, page->index,
117 ci->i_wrbuffer_ref-1, ci->i_wrbuffer_ref_head-1,
118 ci->i_wrbuffer_ref, ci->i_wrbuffer_ref_head,
119 snapc, snapc->seq, snapc->num_snaps);
120 spin_unlock(&ci->i_ceph_lock);
123 * Reference snap context in page->private. Also set
124 * PagePrivate so that we get invalidatepage callback.
126 BUG_ON(PagePrivate(page));
127 page->private = (unsigned long)snapc;
128 SetPagePrivate(page);
130 ret = __set_page_dirty_nobuffers(page);
131 WARN_ON(!PageLocked(page));
132 WARN_ON(!page->mapping);
138 * If we are truncating the full page (i.e. offset == 0), adjust the
139 * dirty page counters appropriately. Only called if there is private
142 static void ceph_invalidatepage(struct page *page, unsigned int offset,
146 struct ceph_inode_info *ci;
147 struct ceph_snap_context *snapc = page_snap_context(page);
149 wait_on_page_fscache(page);
151 inode = page->mapping->host;
152 ci = ceph_inode(inode);
154 if (offset != 0 || length != PAGE_SIZE) {
155 dout("%p invalidatepage %p idx %lu partial dirty page %u~%u\n",
156 inode, page, page->index, offset, length);
160 WARN_ON(!PageLocked(page));
161 if (!PagePrivate(page))
164 dout("%p invalidatepage %p idx %lu full dirty page\n",
165 inode, page, page->index);
167 ceph_put_wrbuffer_cap_refs(ci, 1, snapc);
168 ceph_put_snap_context(snapc);
170 ClearPagePrivate(page);
173 static int ceph_releasepage(struct page *page, gfp_t gfp)
175 dout("%p releasepage %p idx %lu (%sdirty)\n", page->mapping->host,
176 page, page->index, PageDirty(page) ? "" : "not ");
178 if (PageFsCache(page)) {
179 if (!(gfp & __GFP_DIRECT_RECLAIM) || !(gfp & __GFP_FS))
181 wait_on_page_fscache(page);
183 return !PagePrivate(page);
186 /* read a single page, without unlocking it. */
187 static int ceph_do_readpage(struct file *filp, struct page *page)
189 struct inode *inode = file_inode(filp);
190 struct ceph_inode_info *ci = ceph_inode(inode);
191 struct ceph_fs_client *fsc = ceph_inode_to_client(inode);
192 struct ceph_osd_client *osdc = &fsc->client->osdc;
193 struct ceph_osd_request *req;
194 struct ceph_vino vino = ceph_vino(inode);
196 u64 off = page_offset(page);
199 if (off >= i_size_read(inode)) {
200 zero_user_segment(page, 0, PAGE_SIZE);
201 SetPageUptodate(page);
205 if (ci->i_inline_version != CEPH_INLINE_NONE) {
207 * Uptodate inline data should have been added
208 * into page cache while getting Fcr caps.
212 zero_user_segment(page, 0, PAGE_SIZE);
213 SetPageUptodate(page);
217 dout("readpage ino %llx.%llx file %p off %llu len %llu page %p index %lu\n",
218 vino.ino, vino.snap, filp, off, len, page, page->index);
219 req = ceph_osdc_new_request(osdc, &ci->i_layout, vino, off, &len, 0, 1,
220 CEPH_OSD_OP_READ, CEPH_OSD_FLAG_READ, NULL,
221 ci->i_truncate_seq, ci->i_truncate_size,
226 osd_req_op_extent_osd_data_pages(req, 0, &page, len, 0, false, false);
228 err = ceph_osdc_start_request(osdc, req, false);
230 err = ceph_osdc_wait_request(osdc, req);
232 ceph_update_read_latency(&fsc->mdsc->metric, req->r_start_latency,
233 req->r_end_latency, err);
235 ceph_osdc_put_request(req);
236 dout("readpage result %d\n", err);
241 if (err == -EBLOCKLISTED)
242 fsc->blocklisted = true;
246 /* zero fill remainder of page */
247 zero_user_segment(page, err, PAGE_SIZE);
249 flush_dcache_page(page);
251 SetPageUptodate(page);
253 return err < 0 ? err : 0;
256 static int ceph_readpage(struct file *filp, struct page *page)
258 int r = ceph_do_readpage(filp, page);
259 if (r != -EINPROGRESS)
267 * Finish an async read(ahead) op.
269 static void finish_read(struct ceph_osd_request *req)
271 struct inode *inode = req->r_inode;
272 struct ceph_fs_client *fsc = ceph_inode_to_client(inode);
273 struct ceph_osd_data *osd_data;
274 int rc = req->r_result <= 0 ? req->r_result : 0;
275 int bytes = req->r_result >= 0 ? req->r_result : 0;
279 dout("finish_read %p req %p rc %d bytes %d\n", inode, req, rc, bytes);
280 if (rc == -EBLOCKLISTED)
281 ceph_inode_to_client(inode)->blocklisted = true;
283 /* unlock all pages, zeroing any data we didn't read */
284 osd_data = osd_req_op_extent_osd_data(req, 0);
285 BUG_ON(osd_data->type != CEPH_OSD_DATA_TYPE_PAGES);
286 num_pages = calc_pages_for((u64)osd_data->alignment,
287 (u64)osd_data->length);
288 for (i = 0; i < num_pages; i++) {
289 struct page *page = osd_data->pages[i];
291 if (rc < 0 && rc != -ENOENT)
293 if (bytes < (int)PAGE_SIZE) {
294 /* zero (remainder of) page */
295 int s = bytes < 0 ? 0 : bytes;
296 zero_user_segment(page, s, PAGE_SIZE);
298 dout("finish_read %p uptodate %p idx %lu\n", inode, page,
300 flush_dcache_page(page);
301 SetPageUptodate(page);
308 ceph_update_read_latency(&fsc->mdsc->metric, req->r_start_latency,
309 req->r_end_latency, rc);
311 kfree(osd_data->pages);
315 * start an async read(ahead) operation. return nr_pages we submitted
316 * a read for on success, or negative error code.
318 static int start_read(struct inode *inode, struct ceph_rw_context *rw_ctx,
319 struct list_head *page_list, int max)
321 struct ceph_osd_client *osdc =
322 &ceph_inode_to_client(inode)->client->osdc;
323 struct ceph_inode_info *ci = ceph_inode(inode);
324 struct page *page = lru_to_page(page_list);
325 struct ceph_vino vino;
326 struct ceph_osd_request *req;
337 /* caller of readpages does not hold buffer and read caps
338 * (fadvise, madvise and readahead cases) */
339 int want = CEPH_CAP_FILE_CACHE;
340 ret = ceph_try_get_caps(inode, CEPH_CAP_FILE_RD, want,
343 dout("start_read %p, error getting cap\n", inode);
344 } else if (!(got & want)) {
345 dout("start_read %p, no cache cap\n", inode);
350 ceph_put_cap_refs(ci, got);
351 while (!list_empty(page_list)) {
352 page = lru_to_page(page_list);
353 list_del(&page->lru);
360 off = (u64) page_offset(page);
363 next_index = page->index;
364 list_for_each_entry_reverse(page, page_list, lru) {
365 if (page->index != next_index)
369 if (max && nr_pages == max)
372 len = nr_pages << PAGE_SHIFT;
373 dout("start_read %p nr_pages %d is %lld~%lld\n", inode, nr_pages,
375 vino = ceph_vino(inode);
376 req = ceph_osdc_new_request(osdc, &ci->i_layout, vino, off, &len,
377 0, 1, CEPH_OSD_OP_READ,
378 CEPH_OSD_FLAG_READ, NULL,
379 ci->i_truncate_seq, ci->i_truncate_size,
386 /* build page vector */
387 nr_pages = calc_pages_for(0, len);
388 pages = kmalloc_array(nr_pages, sizeof(*pages), GFP_KERNEL);
393 for (i = 0; i < nr_pages; ++i) {
394 page = list_entry(page_list->prev, struct page, lru);
395 BUG_ON(PageLocked(page));
396 list_del(&page->lru);
398 dout("start_read %p adding %p idx %lu\n", inode, page,
400 if (add_to_page_cache_lru(page, &inode->i_data, page->index,
403 dout("start_read %p add_to_page_cache failed %p\n",
407 len = nr_pages << PAGE_SHIFT;
408 osd_req_op_extent_update(req, 0, len);
415 osd_req_op_extent_osd_data_pages(req, 0, pages, len, 0, false, false);
416 req->r_callback = finish_read;
417 req->r_inode = inode;
419 dout("start_read %p starting %p %lld~%lld\n", inode, req, off, len);
420 ret = ceph_osdc_start_request(osdc, req, false);
423 ceph_osdc_put_request(req);
425 /* After adding locked pages to page cache, the inode holds cache cap.
426 * So we can drop our cap refs. */
428 ceph_put_cap_refs(ci, got);
433 for (i = 0; i < nr_pages; ++i)
434 unlock_page(pages[i]);
435 ceph_put_page_vector(pages, nr_pages, false);
437 ceph_osdc_put_request(req);
440 ceph_put_cap_refs(ci, got);
446 * Read multiple pages. Leave pages we don't read + unlock in page_list;
447 * the caller (VM) cleans them up.
449 static int ceph_readpages(struct file *file, struct address_space *mapping,
450 struct list_head *page_list, unsigned nr_pages)
452 struct inode *inode = file_inode(file);
453 struct ceph_fs_client *fsc = ceph_inode_to_client(inode);
454 struct ceph_file_info *fi = file->private_data;
455 struct ceph_rw_context *rw_ctx;
459 if (ceph_inode(inode)->i_inline_version != CEPH_INLINE_NONE)
462 rw_ctx = ceph_find_rw_context(fi);
463 max = fsc->mount_options->rsize >> PAGE_SHIFT;
464 dout("readpages %p file %p ctx %p nr_pages %d max %d\n",
465 inode, file, rw_ctx, nr_pages, max);
466 while (!list_empty(page_list)) {
467 rc = start_read(inode, rw_ctx, page_list, max);
472 dout("readpages %p file %p ret %d\n", inode, file, rc);
476 struct ceph_writeback_ctl
486 * Get ref for the oldest snapc for an inode with dirty data... that is, the
487 * only snap context we are allowed to write back.
489 static struct ceph_snap_context *
490 get_oldest_context(struct inode *inode, struct ceph_writeback_ctl *ctl,
491 struct ceph_snap_context *page_snapc)
493 struct ceph_inode_info *ci = ceph_inode(inode);
494 struct ceph_snap_context *snapc = NULL;
495 struct ceph_cap_snap *capsnap = NULL;
497 spin_lock(&ci->i_ceph_lock);
498 list_for_each_entry(capsnap, &ci->i_cap_snaps, ci_item) {
499 dout(" cap_snap %p snapc %p has %d dirty pages\n", capsnap,
500 capsnap->context, capsnap->dirty_pages);
501 if (!capsnap->dirty_pages)
504 /* get i_size, truncate_{seq,size} for page_snapc? */
505 if (snapc && capsnap->context != page_snapc)
509 if (capsnap->writing) {
510 ctl->i_size = i_size_read(inode);
511 ctl->size_stable = false;
513 ctl->i_size = capsnap->size;
514 ctl->size_stable = true;
516 ctl->truncate_size = capsnap->truncate_size;
517 ctl->truncate_seq = capsnap->truncate_seq;
518 ctl->head_snapc = false;
524 snapc = ceph_get_snap_context(capsnap->context);
526 page_snapc == snapc ||
527 page_snapc->seq > snapc->seq)
530 if (!snapc && ci->i_wrbuffer_ref_head) {
531 snapc = ceph_get_snap_context(ci->i_head_snapc);
532 dout(" head snapc %p has %d dirty pages\n",
533 snapc, ci->i_wrbuffer_ref_head);
535 ctl->i_size = i_size_read(inode);
536 ctl->truncate_size = ci->i_truncate_size;
537 ctl->truncate_seq = ci->i_truncate_seq;
538 ctl->size_stable = false;
539 ctl->head_snapc = true;
542 spin_unlock(&ci->i_ceph_lock);
546 static u64 get_writepages_data_length(struct inode *inode,
547 struct page *page, u64 start)
549 struct ceph_inode_info *ci = ceph_inode(inode);
550 struct ceph_snap_context *snapc = page_snap_context(page);
551 struct ceph_cap_snap *capsnap = NULL;
552 u64 end = i_size_read(inode);
554 if (snapc != ci->i_head_snapc) {
556 spin_lock(&ci->i_ceph_lock);
557 list_for_each_entry(capsnap, &ci->i_cap_snaps, ci_item) {
558 if (capsnap->context == snapc) {
559 if (!capsnap->writing)
565 spin_unlock(&ci->i_ceph_lock);
568 if (end > page_offset(page) + PAGE_SIZE)
569 end = page_offset(page) + PAGE_SIZE;
570 return end > start ? end - start : 0;
574 * Write a single page, but leave the page locked.
576 * If we get a write error, mark the mapping for error, but still adjust the
577 * dirty page accounting (i.e., page is no longer dirty).
579 static int writepage_nounlock(struct page *page, struct writeback_control *wbc)
581 struct inode *inode = page->mapping->host;
582 struct ceph_inode_info *ci = ceph_inode(inode);
583 struct ceph_fs_client *fsc = ceph_inode_to_client(inode);
584 struct ceph_snap_context *snapc, *oldest;
585 loff_t page_off = page_offset(page);
587 loff_t len = PAGE_SIZE;
588 struct ceph_writeback_ctl ceph_wbc;
589 struct ceph_osd_client *osdc = &fsc->client->osdc;
590 struct ceph_osd_request *req;
592 dout("writepage %p idx %lu\n", page, page->index);
594 /* verify this is a writeable snap context */
595 snapc = page_snap_context(page);
597 dout("writepage %p page %p not dirty?\n", inode, page);
600 oldest = get_oldest_context(inode, &ceph_wbc, snapc);
601 if (snapc->seq > oldest->seq) {
602 dout("writepage %p page %p snapc %p not writeable - noop\n",
604 /* we should only noop if called by kswapd */
605 WARN_ON(!(current->flags & PF_MEMALLOC));
606 ceph_put_snap_context(oldest);
607 redirty_page_for_writepage(wbc, page);
610 ceph_put_snap_context(oldest);
612 /* is this a partial page at end of file? */
613 if (page_off >= ceph_wbc.i_size) {
614 dout("%p page eof %llu\n", page, ceph_wbc.i_size);
615 page->mapping->a_ops->invalidatepage(page, 0, PAGE_SIZE);
619 if (ceph_wbc.i_size < page_off + len)
620 len = ceph_wbc.i_size - page_off;
622 dout("writepage %p page %p index %lu on %llu~%llu snapc %p seq %lld\n",
623 inode, page, page->index, page_off, len, snapc, snapc->seq);
625 if (atomic_long_inc_return(&fsc->writeback_count) >
626 CONGESTION_ON_THRESH(fsc->mount_options->congestion_kb))
627 set_bdi_congested(inode_to_bdi(inode), BLK_RW_ASYNC);
629 set_page_writeback(page);
630 req = ceph_osdc_new_request(osdc, &ci->i_layout, ceph_vino(inode), page_off, &len, 0, 1,
631 CEPH_OSD_OP_WRITE, CEPH_OSD_FLAG_WRITE, snapc,
632 ceph_wbc.truncate_seq, ceph_wbc.truncate_size,
635 redirty_page_for_writepage(wbc, page);
636 end_page_writeback(page);
640 /* it may be a short write due to an object boundary */
641 WARN_ON_ONCE(len > PAGE_SIZE);
642 osd_req_op_extent_osd_data_pages(req, 0, &page, len, 0, false, false);
643 dout("writepage %llu~%llu (%llu bytes)\n", page_off, len, len);
645 req->r_mtime = inode->i_mtime;
646 err = ceph_osdc_start_request(osdc, req, true);
648 err = ceph_osdc_wait_request(osdc, req);
650 ceph_update_write_latency(&fsc->mdsc->metric, req->r_start_latency,
651 req->r_end_latency, err);
653 ceph_osdc_put_request(req);
658 struct writeback_control tmp_wbc;
661 if (err == -ERESTARTSYS) {
662 /* killed by SIGKILL */
663 dout("writepage interrupted page %p\n", page);
664 redirty_page_for_writepage(wbc, page);
665 end_page_writeback(page);
668 if (err == -EBLOCKLISTED)
669 fsc->blocklisted = true;
670 dout("writepage setting page/mapping error %d %p\n",
672 mapping_set_error(&inode->i_data, err);
673 wbc->pages_skipped++;
675 dout("writepage cleaned page %p\n", page);
676 err = 0; /* vfs expects us to return 0 */
679 ClearPagePrivate(page);
680 end_page_writeback(page);
681 ceph_put_wrbuffer_cap_refs(ci, 1, snapc);
682 ceph_put_snap_context(snapc); /* page's reference */
684 if (atomic_long_dec_return(&fsc->writeback_count) <
685 CONGESTION_OFF_THRESH(fsc->mount_options->congestion_kb))
686 clear_bdi_congested(inode_to_bdi(inode), BLK_RW_ASYNC);
691 static int ceph_writepage(struct page *page, struct writeback_control *wbc)
694 struct inode *inode = page->mapping->host;
697 err = writepage_nounlock(page, wbc);
698 if (err == -ERESTARTSYS) {
699 /* direct memory reclaimer was killed by SIGKILL. return 0
700 * to prevent caller from setting mapping/page error */
709 * async writeback completion handler.
711 * If we get an error, set the mapping error bit, but not the individual
714 static void writepages_finish(struct ceph_osd_request *req)
716 struct inode *inode = req->r_inode;
717 struct ceph_inode_info *ci = ceph_inode(inode);
718 struct ceph_osd_data *osd_data;
720 int num_pages, total_pages = 0;
722 int rc = req->r_result;
723 struct ceph_snap_context *snapc = req->r_snapc;
724 struct address_space *mapping = inode->i_mapping;
725 struct ceph_fs_client *fsc = ceph_inode_to_client(inode);
728 dout("writepages_finish %p rc %d\n", inode, rc);
730 mapping_set_error(mapping, rc);
731 ceph_set_error_write(ci);
732 if (rc == -EBLOCKLISTED)
733 fsc->blocklisted = true;
735 ceph_clear_error_write(ci);
738 ceph_update_write_latency(&fsc->mdsc->metric, req->r_start_latency,
739 req->r_end_latency, rc);
742 * We lost the cache cap, need to truncate the page before
743 * it is unlocked, otherwise we'd truncate it later in the
744 * page truncation thread, possibly losing some data that
747 remove_page = !(ceph_caps_issued(ci) &
748 (CEPH_CAP_FILE_CACHE|CEPH_CAP_FILE_LAZYIO));
750 /* clean all pages */
751 for (i = 0; i < req->r_num_ops; i++) {
752 if (req->r_ops[i].op != CEPH_OSD_OP_WRITE)
755 osd_data = osd_req_op_extent_osd_data(req, i);
756 BUG_ON(osd_data->type != CEPH_OSD_DATA_TYPE_PAGES);
757 num_pages = calc_pages_for((u64)osd_data->alignment,
758 (u64)osd_data->length);
759 total_pages += num_pages;
760 for (j = 0; j < num_pages; j++) {
761 page = osd_data->pages[j];
763 WARN_ON(!PageUptodate(page));
765 if (atomic_long_dec_return(&fsc->writeback_count) <
766 CONGESTION_OFF_THRESH(
767 fsc->mount_options->congestion_kb))
768 clear_bdi_congested(inode_to_bdi(inode),
771 ceph_put_snap_context(page_snap_context(page));
773 ClearPagePrivate(page);
774 dout("unlocking %p\n", page);
775 end_page_writeback(page);
778 generic_error_remove_page(inode->i_mapping,
783 dout("writepages_finish %p wrote %llu bytes cleaned %d pages\n",
784 inode, osd_data->length, rc >= 0 ? num_pages : 0);
786 release_pages(osd_data->pages, num_pages);
789 ceph_put_wrbuffer_cap_refs(ci, total_pages, snapc);
791 osd_data = osd_req_op_extent_osd_data(req, 0);
792 if (osd_data->pages_from_pool)
793 mempool_free(osd_data->pages, ceph_wb_pagevec_pool);
795 kfree(osd_data->pages);
796 ceph_osdc_put_request(req);
800 * initiate async writeback
802 static int ceph_writepages_start(struct address_space *mapping,
803 struct writeback_control *wbc)
805 struct inode *inode = mapping->host;
806 struct ceph_inode_info *ci = ceph_inode(inode);
807 struct ceph_fs_client *fsc = ceph_inode_to_client(inode);
808 struct ceph_vino vino = ceph_vino(inode);
809 pgoff_t index, start_index, end = -1;
810 struct ceph_snap_context *snapc = NULL, *last_snapc = NULL, *pgsnapc;
813 unsigned int wsize = i_blocksize(inode);
814 struct ceph_osd_request *req = NULL;
815 struct ceph_writeback_ctl ceph_wbc;
816 bool should_loop, range_whole = false;
819 dout("writepages_start %p (mode=%s)\n", inode,
820 wbc->sync_mode == WB_SYNC_NONE ? "NONE" :
821 (wbc->sync_mode == WB_SYNC_ALL ? "ALL" : "HOLD"));
823 if (READ_ONCE(fsc->mount_state) >= CEPH_MOUNT_SHUTDOWN) {
824 if (ci->i_wrbuffer_ref > 0) {
826 "writepage_start %p %lld forced umount\n",
827 inode, ceph_ino(inode));
829 mapping_set_error(mapping, -EIO);
830 return -EIO; /* we're in a forced umount, don't write! */
832 if (fsc->mount_options->wsize < wsize)
833 wsize = fsc->mount_options->wsize;
837 start_index = wbc->range_cyclic ? mapping->writeback_index : 0;
841 /* find oldest snap context with dirty data */
842 snapc = get_oldest_context(inode, &ceph_wbc, NULL);
844 /* hmm, why does writepages get called when there
846 dout(" no snap context with dirty data?\n");
849 dout(" oldest snapc is %p seq %lld (%d snaps)\n",
850 snapc, snapc->seq, snapc->num_snaps);
853 if (ceph_wbc.head_snapc && snapc != last_snapc) {
854 /* where to start/end? */
855 if (wbc->range_cyclic) {
860 dout(" cyclic, start at %lu\n", index);
862 index = wbc->range_start >> PAGE_SHIFT;
863 end = wbc->range_end >> PAGE_SHIFT;
864 if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX)
866 dout(" not cyclic, %lu to %lu\n", index, end);
868 } else if (!ceph_wbc.head_snapc) {
869 /* Do not respect wbc->range_{start,end}. Dirty pages
870 * in that range can be associated with newer snapc.
871 * They are not writeable until we write all dirty pages
872 * associated with 'snapc' get written */
875 dout(" non-head snapc, range whole\n");
878 ceph_put_snap_context(last_snapc);
881 while (!done && index <= end) {
882 int num_ops = 0, op_idx;
883 unsigned i, pvec_pages, max_pages, locked_pages = 0;
884 struct page **pages = NULL, **data_pages;
886 pgoff_t strip_unit_end = 0;
887 u64 offset = 0, len = 0;
888 bool from_pool = false;
890 max_pages = wsize >> PAGE_SHIFT;
893 pvec_pages = pagevec_lookup_range_tag(&pvec, mapping, &index,
894 end, PAGECACHE_TAG_DIRTY);
895 dout("pagevec_lookup_range_tag got %d\n", pvec_pages);
896 if (!pvec_pages && !locked_pages)
898 for (i = 0; i < pvec_pages && locked_pages < max_pages; i++) {
899 page = pvec.pages[i];
900 dout("? %p idx %lu\n", page, page->index);
901 if (locked_pages == 0)
902 lock_page(page); /* first page */
903 else if (!trylock_page(page))
906 /* only dirty pages, or our accounting breaks */
907 if (unlikely(!PageDirty(page)) ||
908 unlikely(page->mapping != mapping)) {
909 dout("!dirty or !mapping %p\n", page);
913 /* only if matching snap context */
914 pgsnapc = page_snap_context(page);
915 if (pgsnapc != snapc) {
916 dout("page snapc %p %lld != oldest %p %lld\n",
917 pgsnapc, pgsnapc->seq, snapc, snapc->seq);
919 !ceph_wbc.head_snapc &&
920 wbc->sync_mode != WB_SYNC_NONE)
925 if (page_offset(page) >= ceph_wbc.i_size) {
926 dout("%p page eof %llu\n",
927 page, ceph_wbc.i_size);
928 if ((ceph_wbc.size_stable ||
929 page_offset(page) >= i_size_read(inode)) &&
930 clear_page_dirty_for_io(page))
931 mapping->a_ops->invalidatepage(page,
936 if (strip_unit_end && (page->index > strip_unit_end)) {
937 dout("end of strip unit %p\n", page);
941 if (PageWriteback(page)) {
942 if (wbc->sync_mode == WB_SYNC_NONE) {
943 dout("%p under writeback\n", page);
947 dout("waiting on writeback %p\n", page);
948 wait_on_page_writeback(page);
951 if (!clear_page_dirty_for_io(page)) {
952 dout("%p !clear_page_dirty_for_io\n", page);
958 * We have something to write. If this is
959 * the first locked page this time through,
960 * calculate max possinle write size and
961 * allocate a page array
963 if (locked_pages == 0) {
968 /* prepare async write request */
969 offset = (u64)page_offset(page);
970 ceph_calc_file_object_mapping(&ci->i_layout,
977 strip_unit_end = page->index +
978 ((len - 1) >> PAGE_SHIFT);
981 max_pages = calc_pages_for(0, (u64)len);
982 pages = kmalloc_array(max_pages,
987 pages = mempool_alloc(ceph_wb_pagevec_pool, GFP_NOFS);
992 } else if (page->index !=
993 (offset + len) >> PAGE_SHIFT) {
994 if (num_ops >= (from_pool ? CEPH_OSD_SLAB_OPS :
996 redirty_page_for_writepage(wbc, page);
1002 offset = (u64)page_offset(page);
1006 /* note position of first page in pvec */
1007 dout("%p will write page %p idx %lu\n",
1008 inode, page, page->index);
1010 if (atomic_long_inc_return(&fsc->writeback_count) >
1011 CONGESTION_ON_THRESH(
1012 fsc->mount_options->congestion_kb)) {
1013 set_bdi_congested(inode_to_bdi(inode),
1018 pages[locked_pages++] = page;
1019 pvec.pages[i] = NULL;
1024 /* did we get anything? */
1026 goto release_pvec_pages;
1029 /* shift unused page to beginning of pvec */
1030 for (j = 0; j < pvec_pages; j++) {
1034 pvec.pages[n] = pvec.pages[j];
1039 if (pvec_pages && i == pvec_pages &&
1040 locked_pages < max_pages) {
1041 dout("reached end pvec, trying for more\n");
1042 pagevec_release(&pvec);
1043 goto get_more_pages;
1048 offset = page_offset(pages[0]);
1051 req = ceph_osdc_new_request(&fsc->client->osdc,
1052 &ci->i_layout, vino,
1053 offset, &len, 0, num_ops,
1054 CEPH_OSD_OP_WRITE, CEPH_OSD_FLAG_WRITE,
1055 snapc, ceph_wbc.truncate_seq,
1056 ceph_wbc.truncate_size, false);
1058 req = ceph_osdc_new_request(&fsc->client->osdc,
1059 &ci->i_layout, vino,
1064 CEPH_OSD_FLAG_WRITE,
1065 snapc, ceph_wbc.truncate_seq,
1066 ceph_wbc.truncate_size, true);
1067 BUG_ON(IS_ERR(req));
1069 BUG_ON(len < page_offset(pages[locked_pages - 1]) +
1070 PAGE_SIZE - offset);
1072 req->r_callback = writepages_finish;
1073 req->r_inode = inode;
1075 /* Format the osd request message and submit the write */
1079 for (i = 0; i < locked_pages; i++) {
1080 u64 cur_offset = page_offset(pages[i]);
1081 if (offset + len != cur_offset) {
1082 if (op_idx + 1 == req->r_num_ops)
1084 osd_req_op_extent_dup_last(req, op_idx,
1085 cur_offset - offset);
1086 dout("writepages got pages at %llu~%llu\n",
1088 osd_req_op_extent_osd_data_pages(req, op_idx,
1091 osd_req_op_extent_update(req, op_idx, len);
1094 offset = cur_offset;
1095 data_pages = pages + i;
1099 set_page_writeback(pages[i]);
1103 if (ceph_wbc.size_stable) {
1104 len = min(len, ceph_wbc.i_size - offset);
1105 } else if (i == locked_pages) {
1106 /* writepages_finish() clears writeback pages
1107 * according to the data length, so make sure
1108 * data length covers all locked pages */
1109 u64 min_len = len + 1 - PAGE_SIZE;
1110 len = get_writepages_data_length(inode, pages[i - 1],
1112 len = max(len, min_len);
1114 dout("writepages got pages at %llu~%llu\n", offset, len);
1116 osd_req_op_extent_osd_data_pages(req, op_idx, data_pages, len,
1117 0, from_pool, false);
1118 osd_req_op_extent_update(req, op_idx, len);
1120 BUG_ON(op_idx + 1 != req->r_num_ops);
1123 if (i < locked_pages) {
1124 BUG_ON(num_ops <= req->r_num_ops);
1125 num_ops -= req->r_num_ops;
1128 /* allocate new pages array for next request */
1130 pages = kmalloc_array(locked_pages, sizeof(*pages),
1134 pages = mempool_alloc(ceph_wb_pagevec_pool, GFP_NOFS);
1137 memcpy(pages, data_pages + i,
1138 locked_pages * sizeof(*pages));
1139 memset(data_pages + i, 0,
1140 locked_pages * sizeof(*pages));
1142 BUG_ON(num_ops != req->r_num_ops);
1143 index = pages[i - 1]->index + 1;
1144 /* request message now owns the pages array */
1148 req->r_mtime = inode->i_mtime;
1149 rc = ceph_osdc_start_request(&fsc->client->osdc, req, true);
1153 wbc->nr_to_write -= i;
1158 * We stop writing back only if we are not doing
1159 * integrity sync. In case of integrity sync we have to
1160 * keep going until we have written all the pages
1161 * we tagged for writeback prior to entering this loop.
1163 if (wbc->nr_to_write <= 0 && wbc->sync_mode == WB_SYNC_NONE)
1167 dout("pagevec_release on %d pages (%p)\n", (int)pvec.nr,
1168 pvec.nr ? pvec.pages[0] : NULL);
1169 pagevec_release(&pvec);
1172 if (should_loop && !done) {
1173 /* more to do; loop back to beginning of file */
1174 dout("writepages looping back to beginning of file\n");
1175 end = start_index - 1; /* OK even when start_index == 0 */
1177 /* to write dirty pages associated with next snapc,
1178 * we need to wait until current writes complete */
1179 if (wbc->sync_mode != WB_SYNC_NONE &&
1180 start_index == 0 && /* all dirty pages were checked */
1181 !ceph_wbc.head_snapc) {
1185 while ((index <= end) &&
1186 (nr = pagevec_lookup_tag(&pvec, mapping, &index,
1187 PAGECACHE_TAG_WRITEBACK))) {
1188 for (i = 0; i < nr; i++) {
1189 page = pvec.pages[i];
1190 if (page_snap_context(page) != snapc)
1192 wait_on_page_writeback(page);
1194 pagevec_release(&pvec);
1204 if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0))
1205 mapping->writeback_index = index;
1208 ceph_osdc_put_request(req);
1209 ceph_put_snap_context(last_snapc);
1210 dout("writepages dend - startone, rc = %d\n", rc);
1217 * See if a given @snapc is either writeable, or already written.
1219 static int context_is_writeable_or_written(struct inode *inode,
1220 struct ceph_snap_context *snapc)
1222 struct ceph_snap_context *oldest = get_oldest_context(inode, NULL, NULL);
1223 int ret = !oldest || snapc->seq <= oldest->seq;
1225 ceph_put_snap_context(oldest);
1230 * ceph_find_incompatible - find an incompatible context and return it
1231 * @page: page being dirtied
1233 * We are only allowed to write into/dirty a page if the page is
1234 * clean, or already dirty within the same snap context. Returns a
1235 * conflicting context if there is one, NULL if there isn't, or a
1236 * negative error code on other errors.
1238 * Must be called with page lock held.
1240 static struct ceph_snap_context *
1241 ceph_find_incompatible(struct page *page)
1243 struct inode *inode = page->mapping->host;
1244 struct ceph_fs_client *fsc = ceph_inode_to_client(inode);
1245 struct ceph_inode_info *ci = ceph_inode(inode);
1247 if (READ_ONCE(fsc->mount_state) >= CEPH_MOUNT_SHUTDOWN) {
1248 dout(" page %p forced umount\n", page);
1249 return ERR_PTR(-EIO);
1253 struct ceph_snap_context *snapc, *oldest;
1255 wait_on_page_writeback(page);
1257 snapc = page_snap_context(page);
1258 if (!snapc || snapc == ci->i_head_snapc)
1262 * this page is already dirty in another (older) snap
1263 * context! is it writeable now?
1265 oldest = get_oldest_context(inode, NULL, NULL);
1266 if (snapc->seq > oldest->seq) {
1267 /* not writeable -- return it for the caller to deal with */
1268 ceph_put_snap_context(oldest);
1269 dout(" page %p snapc %p not current or oldest\n", page, snapc);
1270 return ceph_get_snap_context(snapc);
1272 ceph_put_snap_context(oldest);
1274 /* yay, writeable, do it now (without dropping page lock) */
1275 dout(" page %p snapc %p not current, but oldest\n", page, snapc);
1276 if (clear_page_dirty_for_io(page)) {
1277 int r = writepage_nounlock(page, NULL);
1286 * We are only allowed to write into/dirty the page if the page is
1287 * clean, or already dirty within the same snap context.
1289 static int ceph_write_begin(struct file *file, struct address_space *mapping,
1290 loff_t pos, unsigned len, unsigned flags,
1291 struct page **pagep, void **fsdata)
1293 struct inode *inode = file_inode(file);
1294 struct ceph_inode_info *ci = ceph_inode(inode);
1295 struct ceph_snap_context *snapc;
1296 struct page *page = NULL;
1297 pgoff_t index = pos >> PAGE_SHIFT;
1298 int pos_in_page = pos & ~PAGE_MASK;
1301 dout("write_begin file %p inode %p page %p %d~%d\n", file, inode, page, (int)pos, (int)len);
1304 page = grab_cache_page_write_begin(mapping, index, flags);
1310 snapc = ceph_find_incompatible(page);
1312 if (IS_ERR(snapc)) {
1319 ceph_queue_writeback(inode);
1320 r = wait_event_killable(ci->i_cap_wq,
1321 context_is_writeable_or_written(inode, snapc));
1322 ceph_put_snap_context(snapc);
1328 if (PageUptodate(page)) {
1329 dout(" page %p already uptodate\n", page);
1334 * In some cases we don't need to read at all:
1336 * - write that lies completely beyond EOF
1337 * - write that covers the the page from start to EOF or beyond it
1339 if ((pos_in_page == 0 && len == PAGE_SIZE) ||
1340 (pos >= i_size_read(inode)) ||
1341 (pos_in_page == 0 && (pos + len) >= i_size_read(inode))) {
1342 zero_user_segments(page, 0, pos_in_page,
1343 pos_in_page + len, PAGE_SIZE);
1348 * We need to read it. If we get back -EINPROGRESS, then the page was
1349 * handed off to fscache and it will be unlocked when the read completes.
1350 * Refind the page in that case so we can reacquire the page lock. Otherwise
1351 * we got a hard error or the read was completed synchronously.
1353 r = ceph_do_readpage(file, page);
1354 if (r != -EINPROGRESS)
1370 * we don't do anything in here that simple_write_end doesn't do
1371 * except adjust dirty page accounting
1373 static int ceph_write_end(struct file *file, struct address_space *mapping,
1374 loff_t pos, unsigned len, unsigned copied,
1375 struct page *page, void *fsdata)
1377 struct inode *inode = file_inode(file);
1378 bool check_cap = false;
1380 dout("write_end file %p inode %p page %p %d~%d (%d)\n", file,
1381 inode, page, (int)pos, (int)copied, (int)len);
1383 /* zero the stale part of the page if we did a short copy */
1384 if (!PageUptodate(page)) {
1389 SetPageUptodate(page);
1392 /* did file size increase? */
1393 if (pos+copied > i_size_read(inode))
1394 check_cap = ceph_inode_set_size(inode, pos+copied);
1396 set_page_dirty(page);
1403 ceph_check_caps(ceph_inode(inode), CHECK_CAPS_AUTHONLY, NULL);
1409 * we set .direct_IO to indicate direct io is supported, but since we
1410 * intercept O_DIRECT reads and writes early, this function should
1413 static ssize_t ceph_direct_io(struct kiocb *iocb, struct iov_iter *iter)
1419 const struct address_space_operations ceph_aops = {
1420 .readpage = ceph_readpage,
1421 .readpages = ceph_readpages,
1422 .writepage = ceph_writepage,
1423 .writepages = ceph_writepages_start,
1424 .write_begin = ceph_write_begin,
1425 .write_end = ceph_write_end,
1426 .set_page_dirty = ceph_set_page_dirty,
1427 .invalidatepage = ceph_invalidatepage,
1428 .releasepage = ceph_releasepage,
1429 .direct_IO = ceph_direct_io,
1432 static void ceph_block_sigs(sigset_t *oldset)
1435 siginitsetinv(&mask, sigmask(SIGKILL));
1436 sigprocmask(SIG_BLOCK, &mask, oldset);
1439 static void ceph_restore_sigs(sigset_t *oldset)
1441 sigprocmask(SIG_SETMASK, oldset, NULL);
1447 static vm_fault_t ceph_filemap_fault(struct vm_fault *vmf)
1449 struct vm_area_struct *vma = vmf->vma;
1450 struct inode *inode = file_inode(vma->vm_file);
1451 struct ceph_inode_info *ci = ceph_inode(inode);
1452 struct ceph_file_info *fi = vma->vm_file->private_data;
1453 struct page *pinned_page = NULL;
1454 loff_t off = (loff_t)vmf->pgoff << PAGE_SHIFT;
1457 vm_fault_t ret = VM_FAULT_SIGBUS;
1459 ceph_block_sigs(&oldset);
1461 dout("filemap_fault %p %llx.%llx %llu~%zd trying to get caps\n",
1462 inode, ceph_vinop(inode), off, (size_t)PAGE_SIZE);
1463 if (fi->fmode & CEPH_FILE_MODE_LAZY)
1464 want = CEPH_CAP_FILE_CACHE | CEPH_CAP_FILE_LAZYIO;
1466 want = CEPH_CAP_FILE_CACHE;
1469 err = ceph_get_caps(vma->vm_file, CEPH_CAP_FILE_RD, want, -1,
1470 &got, &pinned_page);
1474 dout("filemap_fault %p %llu~%zd got cap refs on %s\n",
1475 inode, off, (size_t)PAGE_SIZE, ceph_cap_string(got));
1477 if ((got & (CEPH_CAP_FILE_CACHE | CEPH_CAP_FILE_LAZYIO)) ||
1478 ci->i_inline_version == CEPH_INLINE_NONE) {
1479 CEPH_DEFINE_RW_CONTEXT(rw_ctx, got);
1480 ceph_add_rw_context(fi, &rw_ctx);
1481 ret = filemap_fault(vmf);
1482 ceph_del_rw_context(fi, &rw_ctx);
1483 dout("filemap_fault %p %llu~%zd drop cap refs %s ret %x\n",
1484 inode, off, (size_t)PAGE_SIZE,
1485 ceph_cap_string(got), ret);
1490 put_page(pinned_page);
1491 ceph_put_cap_refs(ci, got);
1496 /* read inline data */
1497 if (off >= PAGE_SIZE) {
1498 /* does not support inline data > PAGE_SIZE */
1499 ret = VM_FAULT_SIGBUS;
1501 struct address_space *mapping = inode->i_mapping;
1502 struct page *page = find_or_create_page(mapping, 0,
1503 mapping_gfp_constraint(mapping,
1509 err = __ceph_do_getattr(inode, page,
1510 CEPH_STAT_CAP_INLINE_DATA, true);
1511 if (err < 0 || off >= i_size_read(inode)) {
1514 ret = vmf_error(err);
1517 if (err < PAGE_SIZE)
1518 zero_user_segment(page, err, PAGE_SIZE);
1520 flush_dcache_page(page);
1521 SetPageUptodate(page);
1523 ret = VM_FAULT_MAJOR | VM_FAULT_LOCKED;
1525 dout("filemap_fault %p %llu~%zd read inline data ret %x\n",
1526 inode, off, (size_t)PAGE_SIZE, ret);
1529 ceph_restore_sigs(&oldset);
1531 ret = vmf_error(err);
1537 * Reuse write_begin here for simplicity.
1539 static vm_fault_t ceph_page_mkwrite(struct vm_fault *vmf)
1541 struct vm_area_struct *vma = vmf->vma;
1542 struct inode *inode = file_inode(vma->vm_file);
1543 struct ceph_inode_info *ci = ceph_inode(inode);
1544 struct ceph_file_info *fi = vma->vm_file->private_data;
1545 struct ceph_cap_flush *prealloc_cf;
1546 struct page *page = vmf->page;
1547 loff_t off = page_offset(page);
1548 loff_t size = i_size_read(inode);
1552 vm_fault_t ret = VM_FAULT_SIGBUS;
1554 prealloc_cf = ceph_alloc_cap_flush();
1556 return VM_FAULT_OOM;
1558 sb_start_pagefault(inode->i_sb);
1559 ceph_block_sigs(&oldset);
1561 if (ci->i_inline_version != CEPH_INLINE_NONE) {
1562 struct page *locked_page = NULL;
1567 err = ceph_uninline_data(vma->vm_file, locked_page);
1569 unlock_page(locked_page);
1574 if (off + PAGE_SIZE <= size)
1577 len = size & ~PAGE_MASK;
1579 dout("page_mkwrite %p %llx.%llx %llu~%zd getting caps i_size %llu\n",
1580 inode, ceph_vinop(inode), off, len, size);
1581 if (fi->fmode & CEPH_FILE_MODE_LAZY)
1582 want = CEPH_CAP_FILE_BUFFER | CEPH_CAP_FILE_LAZYIO;
1584 want = CEPH_CAP_FILE_BUFFER;
1587 err = ceph_get_caps(vma->vm_file, CEPH_CAP_FILE_WR, want, off + len,
1592 dout("page_mkwrite %p %llu~%zd got cap refs on %s\n",
1593 inode, off, len, ceph_cap_string(got));
1595 /* Update time before taking page lock */
1596 file_update_time(vma->vm_file);
1597 inode_inc_iversion_raw(inode);
1600 struct ceph_snap_context *snapc;
1604 if (page_mkwrite_check_truncate(page, inode) < 0) {
1606 ret = VM_FAULT_NOPAGE;
1610 snapc = ceph_find_incompatible(page);
1612 /* success. we'll keep the page locked. */
1613 set_page_dirty(page);
1614 ret = VM_FAULT_LOCKED;
1620 if (IS_ERR(snapc)) {
1621 ret = VM_FAULT_SIGBUS;
1625 ceph_queue_writeback(inode);
1626 err = wait_event_killable(ci->i_cap_wq,
1627 context_is_writeable_or_written(inode, snapc));
1628 ceph_put_snap_context(snapc);
1631 if (ret == VM_FAULT_LOCKED ||
1632 ci->i_inline_version != CEPH_INLINE_NONE) {
1634 spin_lock(&ci->i_ceph_lock);
1635 ci->i_inline_version = CEPH_INLINE_NONE;
1636 dirty = __ceph_mark_dirty_caps(ci, CEPH_CAP_FILE_WR,
1638 spin_unlock(&ci->i_ceph_lock);
1640 __mark_inode_dirty(inode, dirty);
1643 dout("page_mkwrite %p %llu~%zd dropping cap refs on %s ret %x\n",
1644 inode, off, len, ceph_cap_string(got), ret);
1645 ceph_put_cap_refs_async(ci, got);
1647 ceph_restore_sigs(&oldset);
1648 sb_end_pagefault(inode->i_sb);
1649 ceph_free_cap_flush(prealloc_cf);
1651 ret = vmf_error(err);
1655 void ceph_fill_inline_data(struct inode *inode, struct page *locked_page,
1656 char *data, size_t len)
1658 struct address_space *mapping = inode->i_mapping;
1664 if (i_size_read(inode) == 0)
1666 page = find_or_create_page(mapping, 0,
1667 mapping_gfp_constraint(mapping,
1671 if (PageUptodate(page)) {
1678 dout("fill_inline_data %p %llx.%llx len %zu locked_page %p\n",
1679 inode, ceph_vinop(inode), len, locked_page);
1682 void *kaddr = kmap_atomic(page);
1683 memcpy(kaddr, data, len);
1684 kunmap_atomic(kaddr);
1687 if (page != locked_page) {
1688 if (len < PAGE_SIZE)
1689 zero_user_segment(page, len, PAGE_SIZE);
1691 flush_dcache_page(page);
1693 SetPageUptodate(page);
1699 int ceph_uninline_data(struct file *filp, struct page *locked_page)
1701 struct inode *inode = file_inode(filp);
1702 struct ceph_inode_info *ci = ceph_inode(inode);
1703 struct ceph_fs_client *fsc = ceph_inode_to_client(inode);
1704 struct ceph_osd_request *req;
1705 struct page *page = NULL;
1706 u64 len, inline_version;
1708 bool from_pagecache = false;
1710 spin_lock(&ci->i_ceph_lock);
1711 inline_version = ci->i_inline_version;
1712 spin_unlock(&ci->i_ceph_lock);
1714 dout("uninline_data %p %llx.%llx inline_version %llu\n",
1715 inode, ceph_vinop(inode), inline_version);
1717 if (inline_version == 1 || /* initial version, no data */
1718 inline_version == CEPH_INLINE_NONE)
1723 WARN_ON(!PageUptodate(page));
1724 } else if (ceph_caps_issued(ci) &
1725 (CEPH_CAP_FILE_CACHE|CEPH_CAP_FILE_LAZYIO)) {
1726 page = find_get_page(inode->i_mapping, 0);
1728 if (PageUptodate(page)) {
1729 from_pagecache = true;
1739 len = i_size_read(inode);
1740 if (len > PAGE_SIZE)
1743 page = __page_cache_alloc(GFP_NOFS);
1748 err = __ceph_do_getattr(inode, page,
1749 CEPH_STAT_CAP_INLINE_DATA, true);
1751 /* no inline data */
1752 if (err == -ENODATA)
1759 req = ceph_osdc_new_request(&fsc->client->osdc, &ci->i_layout,
1760 ceph_vino(inode), 0, &len, 0, 1,
1761 CEPH_OSD_OP_CREATE, CEPH_OSD_FLAG_WRITE,
1768 req->r_mtime = inode->i_mtime;
1769 err = ceph_osdc_start_request(&fsc->client->osdc, req, false);
1771 err = ceph_osdc_wait_request(&fsc->client->osdc, req);
1772 ceph_osdc_put_request(req);
1776 req = ceph_osdc_new_request(&fsc->client->osdc, &ci->i_layout,
1777 ceph_vino(inode), 0, &len, 1, 3,
1778 CEPH_OSD_OP_WRITE, CEPH_OSD_FLAG_WRITE,
1779 NULL, ci->i_truncate_seq,
1780 ci->i_truncate_size, false);
1786 osd_req_op_extent_osd_data_pages(req, 1, &page, len, 0, false, false);
1789 __le64 xattr_buf = cpu_to_le64(inline_version);
1790 err = osd_req_op_xattr_init(req, 0, CEPH_OSD_OP_CMPXATTR,
1791 "inline_version", &xattr_buf,
1793 CEPH_OSD_CMPXATTR_OP_GT,
1794 CEPH_OSD_CMPXATTR_MODE_U64);
1801 int xattr_len = snprintf(xattr_buf, sizeof(xattr_buf),
1802 "%llu", inline_version);
1803 err = osd_req_op_xattr_init(req, 2, CEPH_OSD_OP_SETXATTR,
1805 xattr_buf, xattr_len, 0, 0);
1810 req->r_mtime = inode->i_mtime;
1811 err = ceph_osdc_start_request(&fsc->client->osdc, req, false);
1813 err = ceph_osdc_wait_request(&fsc->client->osdc, req);
1815 ceph_update_write_latency(&fsc->mdsc->metric, req->r_start_latency,
1816 req->r_end_latency, err);
1819 ceph_osdc_put_request(req);
1820 if (err == -ECANCELED)
1823 if (page && page != locked_page) {
1824 if (from_pagecache) {
1828 __free_pages(page, 0);
1831 dout("uninline_data %p %llx.%llx inline_version %llu = %d\n",
1832 inode, ceph_vinop(inode), inline_version, err);
1836 static const struct vm_operations_struct ceph_vmops = {
1837 .fault = ceph_filemap_fault,
1838 .page_mkwrite = ceph_page_mkwrite,
1841 int ceph_mmap(struct file *file, struct vm_area_struct *vma)
1843 struct address_space *mapping = file->f_mapping;
1845 if (!mapping->a_ops->readpage)
1847 file_accessed(file);
1848 vma->vm_ops = &ceph_vmops;
1857 static int __ceph_pool_perm_get(struct ceph_inode_info *ci,
1858 s64 pool, struct ceph_string *pool_ns)
1860 struct ceph_fs_client *fsc = ceph_inode_to_client(&ci->vfs_inode);
1861 struct ceph_mds_client *mdsc = fsc->mdsc;
1862 struct ceph_osd_request *rd_req = NULL, *wr_req = NULL;
1863 struct rb_node **p, *parent;
1864 struct ceph_pool_perm *perm;
1865 struct page **pages;
1867 int err = 0, err2 = 0, have = 0;
1869 down_read(&mdsc->pool_perm_rwsem);
1870 p = &mdsc->pool_perm_tree.rb_node;
1872 perm = rb_entry(*p, struct ceph_pool_perm, node);
1873 if (pool < perm->pool)
1875 else if (pool > perm->pool)
1876 p = &(*p)->rb_right;
1878 int ret = ceph_compare_string(pool_ns,
1884 p = &(*p)->rb_right;
1891 up_read(&mdsc->pool_perm_rwsem);
1896 dout("__ceph_pool_perm_get pool %lld ns %.*s no perm cached\n",
1897 pool, (int)pool_ns->len, pool_ns->str);
1899 dout("__ceph_pool_perm_get pool %lld no perm cached\n", pool);
1901 down_write(&mdsc->pool_perm_rwsem);
1902 p = &mdsc->pool_perm_tree.rb_node;
1906 perm = rb_entry(parent, struct ceph_pool_perm, node);
1907 if (pool < perm->pool)
1909 else if (pool > perm->pool)
1910 p = &(*p)->rb_right;
1912 int ret = ceph_compare_string(pool_ns,
1918 p = &(*p)->rb_right;
1926 up_write(&mdsc->pool_perm_rwsem);
1930 rd_req = ceph_osdc_alloc_request(&fsc->client->osdc, NULL,
1931 1, false, GFP_NOFS);
1937 rd_req->r_flags = CEPH_OSD_FLAG_READ;
1938 osd_req_op_init(rd_req, 0, CEPH_OSD_OP_STAT, 0);
1939 rd_req->r_base_oloc.pool = pool;
1941 rd_req->r_base_oloc.pool_ns = ceph_get_string(pool_ns);
1942 ceph_oid_printf(&rd_req->r_base_oid, "%llx.00000000", ci->i_vino.ino);
1944 err = ceph_osdc_alloc_messages(rd_req, GFP_NOFS);
1948 wr_req = ceph_osdc_alloc_request(&fsc->client->osdc, NULL,
1949 1, false, GFP_NOFS);
1955 wr_req->r_flags = CEPH_OSD_FLAG_WRITE;
1956 osd_req_op_init(wr_req, 0, CEPH_OSD_OP_CREATE, CEPH_OSD_OP_FLAG_EXCL);
1957 ceph_oloc_copy(&wr_req->r_base_oloc, &rd_req->r_base_oloc);
1958 ceph_oid_copy(&wr_req->r_base_oid, &rd_req->r_base_oid);
1960 err = ceph_osdc_alloc_messages(wr_req, GFP_NOFS);
1964 /* one page should be large enough for STAT data */
1965 pages = ceph_alloc_page_vector(1, GFP_KERNEL);
1966 if (IS_ERR(pages)) {
1967 err = PTR_ERR(pages);
1971 osd_req_op_raw_data_in_pages(rd_req, 0, pages, PAGE_SIZE,
1973 err = ceph_osdc_start_request(&fsc->client->osdc, rd_req, false);
1975 wr_req->r_mtime = ci->vfs_inode.i_mtime;
1976 err2 = ceph_osdc_start_request(&fsc->client->osdc, wr_req, false);
1979 err = ceph_osdc_wait_request(&fsc->client->osdc, rd_req);
1981 err2 = ceph_osdc_wait_request(&fsc->client->osdc, wr_req);
1983 if (err >= 0 || err == -ENOENT)
1985 else if (err != -EPERM) {
1986 if (err == -EBLOCKLISTED)
1987 fsc->blocklisted = true;
1991 if (err2 == 0 || err2 == -EEXIST)
1993 else if (err2 != -EPERM) {
1994 if (err2 == -EBLOCKLISTED)
1995 fsc->blocklisted = true;
2000 pool_ns_len = pool_ns ? pool_ns->len : 0;
2001 perm = kmalloc(sizeof(*perm) + pool_ns_len + 1, GFP_NOFS);
2009 perm->pool_ns_len = pool_ns_len;
2010 if (pool_ns_len > 0)
2011 memcpy(perm->pool_ns, pool_ns->str, pool_ns_len);
2012 perm->pool_ns[pool_ns_len] = 0;
2014 rb_link_node(&perm->node, parent, p);
2015 rb_insert_color(&perm->node, &mdsc->pool_perm_tree);
2018 up_write(&mdsc->pool_perm_rwsem);
2020 ceph_osdc_put_request(rd_req);
2021 ceph_osdc_put_request(wr_req);
2026 dout("__ceph_pool_perm_get pool %lld ns %.*s result = %d\n",
2027 pool, (int)pool_ns->len, pool_ns->str, err);
2029 dout("__ceph_pool_perm_get pool %lld result = %d\n", pool, err);
2033 int ceph_pool_perm_check(struct inode *inode, int need)
2035 struct ceph_inode_info *ci = ceph_inode(inode);
2036 struct ceph_string *pool_ns;
2040 if (ci->i_vino.snap != CEPH_NOSNAP) {
2042 * Pool permission check needs to write to the first object.
2043 * But for snapshot, head of the first object may have alread
2044 * been deleted. Skip check to avoid creating orphan object.
2049 if (ceph_test_mount_opt(ceph_inode_to_client(inode),
2053 spin_lock(&ci->i_ceph_lock);
2054 flags = ci->i_ceph_flags;
2055 pool = ci->i_layout.pool_id;
2056 spin_unlock(&ci->i_ceph_lock);
2058 if (flags & CEPH_I_POOL_PERM) {
2059 if ((need & CEPH_CAP_FILE_RD) && !(flags & CEPH_I_POOL_RD)) {
2060 dout("ceph_pool_perm_check pool %lld no read perm\n",
2064 if ((need & CEPH_CAP_FILE_WR) && !(flags & CEPH_I_POOL_WR)) {
2065 dout("ceph_pool_perm_check pool %lld no write perm\n",
2072 pool_ns = ceph_try_get_string(ci->i_layout.pool_ns);
2073 ret = __ceph_pool_perm_get(ci, pool, pool_ns);
2074 ceph_put_string(pool_ns);
2078 flags = CEPH_I_POOL_PERM;
2079 if (ret & POOL_READ)
2080 flags |= CEPH_I_POOL_RD;
2081 if (ret & POOL_WRITE)
2082 flags |= CEPH_I_POOL_WR;
2084 spin_lock(&ci->i_ceph_lock);
2085 if (pool == ci->i_layout.pool_id &&
2086 pool_ns == rcu_dereference_raw(ci->i_layout.pool_ns)) {
2087 ci->i_ceph_flags |= flags;
2089 pool = ci->i_layout.pool_id;
2090 flags = ci->i_ceph_flags;
2092 spin_unlock(&ci->i_ceph_lock);
2096 void ceph_pool_perm_destroy(struct ceph_mds_client *mdsc)
2098 struct ceph_pool_perm *perm;
2101 while (!RB_EMPTY_ROOT(&mdsc->pool_perm_tree)) {
2102 n = rb_first(&mdsc->pool_perm_tree);
2103 perm = rb_entry(n, struct ceph_pool_perm, node);
2104 rb_erase(n, &mdsc->pool_perm_tree);