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>
15 #include "mds_client.h"
17 #include <linux/ceph/osd_client.h>
18 #include <linux/ceph/striper.h>
21 * Ceph address space ops.
23 * There are a few funny things going on here.
25 * The page->private field is used to reference a struct
26 * ceph_snap_context for _every_ dirty page. This indicates which
27 * snapshot the page was logically dirtied in, and thus which snap
28 * context needs to be associated with the osd write during writeback.
30 * Similarly, struct ceph_inode_info maintains a set of counters to
31 * count dirty pages on the inode. In the absence of snapshots,
32 * i_wrbuffer_ref == i_wrbuffer_ref_head == the dirty page count.
34 * When a snapshot is taken (that is, when the client receives
35 * notification that a snapshot was taken), each inode with caps and
36 * with dirty pages (dirty pages implies there is a cap) gets a new
37 * ceph_cap_snap in the i_cap_snaps list (which is sorted in ascending
38 * order, new snaps go to the tail). The i_wrbuffer_ref_head count is
39 * moved to capsnap->dirty. (Unless a sync write is currently in
40 * progress. In that case, the capsnap is said to be "pending", new
41 * writes cannot start, and the capsnap isn't "finalized" until the
42 * write completes (or fails) and a final size/mtime for the inode for
43 * that snap can be settled upon.) i_wrbuffer_ref_head is reset to 0.
45 * On writeback, we must submit writes to the osd IN SNAP ORDER. So,
46 * we look for the first capsnap in i_cap_snaps and write out pages in
47 * that snap context _only_. Then we move on to the next capsnap,
48 * eventually reaching the "live" or "head" context (i.e., pages that
49 * are not yet snapped) and are writing the most recently dirtied
52 * Invalidate and so forth must take care to ensure the dirty page
53 * accounting is preserved.
56 #define CONGESTION_ON_THRESH(congestion_kb) (congestion_kb >> (PAGE_SHIFT-10))
57 #define CONGESTION_OFF_THRESH(congestion_kb) \
58 (CONGESTION_ON_THRESH(congestion_kb) - \
59 (CONGESTION_ON_THRESH(congestion_kb) >> 2))
61 static inline struct ceph_snap_context *page_snap_context(struct page *page)
63 if (PagePrivate(page))
64 return (void *)page->private;
69 * Dirty a page. Optimistically adjust accounting, on the assumption
70 * that we won't race with invalidate. If we do, readjust.
72 static int ceph_set_page_dirty(struct page *page)
74 struct address_space *mapping = page->mapping;
76 struct ceph_inode_info *ci;
77 struct ceph_snap_context *snapc;
80 if (unlikely(!mapping))
81 return !TestSetPageDirty(page);
83 if (PageDirty(page)) {
84 dout("%p set_page_dirty %p idx %lu -- already dirty\n",
85 mapping->host, page, page->index);
86 BUG_ON(!PagePrivate(page));
90 inode = mapping->host;
91 ci = ceph_inode(inode);
94 spin_lock(&ci->i_ceph_lock);
95 BUG_ON(ci->i_wr_ref == 0); // caller should hold Fw reference
96 if (__ceph_have_pending_cap_snap(ci)) {
97 struct ceph_cap_snap *capsnap =
98 list_last_entry(&ci->i_cap_snaps,
101 snapc = ceph_get_snap_context(capsnap->context);
102 capsnap->dirty_pages++;
104 BUG_ON(!ci->i_head_snapc);
105 snapc = ceph_get_snap_context(ci->i_head_snapc);
106 ++ci->i_wrbuffer_ref_head;
108 if (ci->i_wrbuffer_ref == 0)
110 ++ci->i_wrbuffer_ref;
111 dout("%p set_page_dirty %p idx %lu head %d/%d -> %d/%d "
112 "snapc %p seq %lld (%d snaps)\n",
113 mapping->host, page, page->index,
114 ci->i_wrbuffer_ref-1, ci->i_wrbuffer_ref_head-1,
115 ci->i_wrbuffer_ref, ci->i_wrbuffer_ref_head,
116 snapc, snapc->seq, snapc->num_snaps);
117 spin_unlock(&ci->i_ceph_lock);
120 * Reference snap context in page->private. Also set
121 * PagePrivate so that we get invalidatepage callback.
123 BUG_ON(PagePrivate(page));
124 page->private = (unsigned long)snapc;
125 SetPagePrivate(page);
127 ret = __set_page_dirty_nobuffers(page);
128 WARN_ON(!PageLocked(page));
129 WARN_ON(!page->mapping);
135 * If we are truncating the full page (i.e. offset == 0), adjust the
136 * dirty page counters appropriately. Only called if there is private
139 static void ceph_invalidatepage(struct page *page, unsigned int offset,
143 struct ceph_inode_info *ci;
144 struct ceph_snap_context *snapc = page_snap_context(page);
146 inode = page->mapping->host;
147 ci = ceph_inode(inode);
149 if (offset != 0 || length != PAGE_SIZE) {
150 dout("%p invalidatepage %p idx %lu partial dirty page %u~%u\n",
151 inode, page, page->index, offset, length);
155 ceph_invalidate_fscache_page(inode, page);
157 WARN_ON(!PageLocked(page));
158 if (!PagePrivate(page))
161 ClearPageChecked(page);
163 dout("%p invalidatepage %p idx %lu full dirty page\n",
164 inode, page, page->index);
166 ceph_put_wrbuffer_cap_refs(ci, 1, snapc);
167 ceph_put_snap_context(snapc);
169 ClearPagePrivate(page);
172 static int ceph_releasepage(struct page *page, gfp_t g)
174 dout("%p releasepage %p idx %lu (%sdirty)\n", page->mapping->host,
175 page, page->index, PageDirty(page) ? "" : "not ");
177 /* Can we release the page from the cache? */
178 if (!ceph_release_fscache_page(page, g))
181 return !PagePrivate(page);
185 * 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_osd_client *osdc =
192 &ceph_inode_to_client(inode)->client->osdc;
194 u64 off = page_offset(page);
197 if (off >= i_size_read(inode)) {
198 zero_user_segment(page, 0, PAGE_SIZE);
199 SetPageUptodate(page);
203 if (ci->i_inline_version != CEPH_INLINE_NONE) {
205 * Uptodate inline data should have been added
206 * into page cache while getting Fcr caps.
210 zero_user_segment(page, 0, PAGE_SIZE);
211 SetPageUptodate(page);
215 err = ceph_readpage_from_fscache(inode, page);
219 dout("readpage inode %p file %p page %p index %lu\n",
220 inode, filp, page, page->index);
221 err = ceph_osdc_readpages(osdc, ceph_vino(inode), &ci->i_layout,
223 ci->i_truncate_seq, ci->i_truncate_size,
229 ceph_fscache_readpage_cancel(inode, page);
233 /* zero fill remainder of page */
234 zero_user_segment(page, err, PAGE_SIZE);
236 flush_dcache_page(page);
238 SetPageUptodate(page);
239 ceph_readpage_to_fscache(inode, page);
242 return err < 0 ? err : 0;
245 static int ceph_readpage(struct file *filp, struct page *page)
247 int r = ceph_do_readpage(filp, page);
248 if (r != -EINPROGRESS)
256 * Finish an async read(ahead) op.
258 static void finish_read(struct ceph_osd_request *req)
260 struct inode *inode = req->r_inode;
261 struct ceph_osd_data *osd_data;
262 int rc = req->r_result <= 0 ? req->r_result : 0;
263 int bytes = req->r_result >= 0 ? req->r_result : 0;
267 dout("finish_read %p req %p rc %d bytes %d\n", inode, req, rc, bytes);
269 /* unlock all pages, zeroing any data we didn't read */
270 osd_data = osd_req_op_extent_osd_data(req, 0);
271 BUG_ON(osd_data->type != CEPH_OSD_DATA_TYPE_PAGES);
272 num_pages = calc_pages_for((u64)osd_data->alignment,
273 (u64)osd_data->length);
274 for (i = 0; i < num_pages; i++) {
275 struct page *page = osd_data->pages[i];
277 if (rc < 0 && rc != -ENOENT) {
278 ceph_fscache_readpage_cancel(inode, page);
281 if (bytes < (int)PAGE_SIZE) {
282 /* zero (remainder of) page */
283 int s = bytes < 0 ? 0 : bytes;
284 zero_user_segment(page, s, PAGE_SIZE);
286 dout("finish_read %p uptodate %p idx %lu\n", inode, page,
288 flush_dcache_page(page);
289 SetPageUptodate(page);
290 ceph_readpage_to_fscache(inode, page);
296 kfree(osd_data->pages);
300 * start an async read(ahead) operation. return nr_pages we submitted
301 * a read for on success, or negative error code.
303 static int start_read(struct inode *inode, struct ceph_rw_context *rw_ctx,
304 struct list_head *page_list, int max)
306 struct ceph_osd_client *osdc =
307 &ceph_inode_to_client(inode)->client->osdc;
308 struct ceph_inode_info *ci = ceph_inode(inode);
309 struct page *page = lru_to_page(page_list);
310 struct ceph_vino vino;
311 struct ceph_osd_request *req;
322 /* caller of readpages does not hold buffer and read caps
323 * (fadvise, madvise and readahead cases) */
324 int want = CEPH_CAP_FILE_CACHE;
325 ret = ceph_try_get_caps(ci, CEPH_CAP_FILE_RD, want, true, &got);
327 dout("start_read %p, error getting cap\n", inode);
328 } else if (!(got & want)) {
329 dout("start_read %p, no cache cap\n", inode);
334 ceph_put_cap_refs(ci, got);
335 while (!list_empty(page_list)) {
336 page = lru_to_page(page_list);
337 list_del(&page->lru);
344 off = (u64) page_offset(page);
347 next_index = page->index;
348 list_for_each_entry_reverse(page, page_list, lru) {
349 if (page->index != next_index)
353 if (max && nr_pages == max)
356 len = nr_pages << PAGE_SHIFT;
357 dout("start_read %p nr_pages %d is %lld~%lld\n", inode, nr_pages,
359 vino = ceph_vino(inode);
360 req = ceph_osdc_new_request(osdc, &ci->i_layout, vino, off, &len,
361 0, 1, CEPH_OSD_OP_READ,
362 CEPH_OSD_FLAG_READ, NULL,
363 ci->i_truncate_seq, ci->i_truncate_size,
370 /* build page vector */
371 nr_pages = calc_pages_for(0, len);
372 pages = kmalloc_array(nr_pages, sizeof(*pages), GFP_KERNEL);
377 for (i = 0; i < nr_pages; ++i) {
378 page = list_entry(page_list->prev, struct page, lru);
379 BUG_ON(PageLocked(page));
380 list_del(&page->lru);
382 dout("start_read %p adding %p idx %lu\n", inode, page,
384 if (add_to_page_cache_lru(page, &inode->i_data, page->index,
386 ceph_fscache_uncache_page(inode, page);
388 dout("start_read %p add_to_page_cache failed %p\n",
392 len = nr_pages << PAGE_SHIFT;
393 osd_req_op_extent_update(req, 0, len);
400 osd_req_op_extent_osd_data_pages(req, 0, pages, len, 0, false, false);
401 req->r_callback = finish_read;
402 req->r_inode = inode;
404 dout("start_read %p starting %p %lld~%lld\n", inode, req, off, len);
405 ret = ceph_osdc_start_request(osdc, req, false);
408 ceph_osdc_put_request(req);
410 /* After adding locked pages to page cache, the inode holds cache cap.
411 * So we can drop our cap refs. */
413 ceph_put_cap_refs(ci, got);
418 for (i = 0; i < nr_pages; ++i) {
419 ceph_fscache_readpage_cancel(inode, pages[i]);
420 unlock_page(pages[i]);
422 ceph_put_page_vector(pages, nr_pages, false);
424 ceph_osdc_put_request(req);
427 ceph_put_cap_refs(ci, got);
433 * Read multiple pages. Leave pages we don't read + unlock in page_list;
434 * the caller (VM) cleans them up.
436 static int ceph_readpages(struct file *file, struct address_space *mapping,
437 struct list_head *page_list, unsigned nr_pages)
439 struct inode *inode = file_inode(file);
440 struct ceph_fs_client *fsc = ceph_inode_to_client(inode);
441 struct ceph_file_info *fi = file->private_data;
442 struct ceph_rw_context *rw_ctx;
446 if (ceph_inode(inode)->i_inline_version != CEPH_INLINE_NONE)
449 rc = ceph_readpages_from_fscache(mapping->host, mapping, page_list,
455 rw_ctx = ceph_find_rw_context(fi);
456 max = fsc->mount_options->rsize >> PAGE_SHIFT;
457 dout("readpages %p file %p ctx %p nr_pages %d max %d\n",
458 inode, file, rw_ctx, nr_pages, max);
459 while (!list_empty(page_list)) {
460 rc = start_read(inode, rw_ctx, page_list, max);
465 ceph_fscache_readpages_cancel(inode, page_list);
467 dout("readpages %p file %p ret %d\n", inode, file, rc);
471 struct ceph_writeback_ctl
481 * Get ref for the oldest snapc for an inode with dirty data... that is, the
482 * only snap context we are allowed to write back.
484 static struct ceph_snap_context *
485 get_oldest_context(struct inode *inode, struct ceph_writeback_ctl *ctl,
486 struct ceph_snap_context *page_snapc)
488 struct ceph_inode_info *ci = ceph_inode(inode);
489 struct ceph_snap_context *snapc = NULL;
490 struct ceph_cap_snap *capsnap = NULL;
492 spin_lock(&ci->i_ceph_lock);
493 list_for_each_entry(capsnap, &ci->i_cap_snaps, ci_item) {
494 dout(" cap_snap %p snapc %p has %d dirty pages\n", capsnap,
495 capsnap->context, capsnap->dirty_pages);
496 if (!capsnap->dirty_pages)
499 /* get i_size, truncate_{seq,size} for page_snapc? */
500 if (snapc && capsnap->context != page_snapc)
504 if (capsnap->writing) {
505 ctl->i_size = i_size_read(inode);
506 ctl->size_stable = false;
508 ctl->i_size = capsnap->size;
509 ctl->size_stable = true;
511 ctl->truncate_size = capsnap->truncate_size;
512 ctl->truncate_seq = capsnap->truncate_seq;
513 ctl->head_snapc = false;
519 snapc = ceph_get_snap_context(capsnap->context);
521 page_snapc == snapc ||
522 page_snapc->seq > snapc->seq)
525 if (!snapc && ci->i_wrbuffer_ref_head) {
526 snapc = ceph_get_snap_context(ci->i_head_snapc);
527 dout(" head snapc %p has %d dirty pages\n",
528 snapc, ci->i_wrbuffer_ref_head);
530 ctl->i_size = i_size_read(inode);
531 ctl->truncate_size = ci->i_truncate_size;
532 ctl->truncate_seq = ci->i_truncate_seq;
533 ctl->size_stable = false;
534 ctl->head_snapc = true;
537 spin_unlock(&ci->i_ceph_lock);
541 static u64 get_writepages_data_length(struct inode *inode,
542 struct page *page, u64 start)
544 struct ceph_inode_info *ci = ceph_inode(inode);
545 struct ceph_snap_context *snapc = page_snap_context(page);
546 struct ceph_cap_snap *capsnap = NULL;
547 u64 end = i_size_read(inode);
549 if (snapc != ci->i_head_snapc) {
551 spin_lock(&ci->i_ceph_lock);
552 list_for_each_entry(capsnap, &ci->i_cap_snaps, ci_item) {
553 if (capsnap->context == snapc) {
554 if (!capsnap->writing)
560 spin_unlock(&ci->i_ceph_lock);
563 if (end > page_offset(page) + PAGE_SIZE)
564 end = page_offset(page) + PAGE_SIZE;
565 return end > start ? end - start : 0;
569 * Write a single page, but leave the page locked.
571 * If we get a write error, set the page error bit, but still adjust the
572 * dirty page accounting (i.e., page is no longer dirty).
574 static int writepage_nounlock(struct page *page, struct writeback_control *wbc)
577 struct ceph_inode_info *ci;
578 struct ceph_fs_client *fsc;
579 struct ceph_snap_context *snapc, *oldest;
580 loff_t page_off = page_offset(page);
581 int err, len = PAGE_SIZE;
582 struct ceph_writeback_ctl ceph_wbc;
584 dout("writepage %p idx %lu\n", page, page->index);
586 inode = page->mapping->host;
587 ci = ceph_inode(inode);
588 fsc = ceph_inode_to_client(inode);
590 /* verify this is a writeable snap context */
591 snapc = page_snap_context(page);
593 dout("writepage %p page %p not dirty?\n", inode, page);
596 oldest = get_oldest_context(inode, &ceph_wbc, snapc);
597 if (snapc->seq > oldest->seq) {
598 dout("writepage %p page %p snapc %p not writeable - noop\n",
600 /* we should only noop if called by kswapd */
601 WARN_ON(!(current->flags & PF_MEMALLOC));
602 ceph_put_snap_context(oldest);
603 redirty_page_for_writepage(wbc, page);
606 ceph_put_snap_context(oldest);
608 /* is this a partial page at end of file? */
609 if (page_off >= ceph_wbc.i_size) {
610 dout("%p page eof %llu\n", page, ceph_wbc.i_size);
611 page->mapping->a_ops->invalidatepage(page, 0, PAGE_SIZE);
615 if (ceph_wbc.i_size < page_off + len)
616 len = ceph_wbc.i_size - page_off;
618 dout("writepage %p page %p index %lu on %llu~%u snapc %p seq %lld\n",
619 inode, page, page->index, page_off, len, snapc, snapc->seq);
621 if (atomic_long_inc_return(&fsc->writeback_count) >
622 CONGESTION_ON_THRESH(fsc->mount_options->congestion_kb))
623 set_bdi_congested(inode_to_bdi(inode), BLK_RW_ASYNC);
625 set_page_writeback(page);
626 err = ceph_osdc_writepages(&fsc->client->osdc, ceph_vino(inode),
627 &ci->i_layout, snapc, page_off, len,
628 ceph_wbc.truncate_seq,
629 ceph_wbc.truncate_size,
630 &inode->i_mtime, &page, 1);
632 struct writeback_control tmp_wbc;
635 if (err == -ERESTARTSYS) {
636 /* killed by SIGKILL */
637 dout("writepage interrupted page %p\n", page);
638 redirty_page_for_writepage(wbc, page);
639 end_page_writeback(page);
642 dout("writepage setting page/mapping error %d %p\n",
645 mapping_set_error(&inode->i_data, err);
646 wbc->pages_skipped++;
648 dout("writepage cleaned page %p\n", page);
649 err = 0; /* vfs expects us to return 0 */
652 ClearPagePrivate(page);
653 end_page_writeback(page);
654 ceph_put_wrbuffer_cap_refs(ci, 1, snapc);
655 ceph_put_snap_context(snapc); /* page's reference */
657 if (atomic_long_dec_return(&fsc->writeback_count) <
658 CONGESTION_OFF_THRESH(fsc->mount_options->congestion_kb))
659 clear_bdi_congested(inode_to_bdi(inode), BLK_RW_ASYNC);
664 static int ceph_writepage(struct page *page, struct writeback_control *wbc)
667 struct inode *inode = page->mapping->host;
670 err = writepage_nounlock(page, wbc);
671 if (err == -ERESTARTSYS) {
672 /* direct memory reclaimer was killed by SIGKILL. return 0
673 * to prevent caller from setting mapping/page error */
682 * lame release_pages helper. release_pages() isn't exported to
685 static void ceph_release_pages(struct page **pages, int num)
691 for (i = 0; i < num; i++) {
692 if (pagevec_add(&pvec, pages[i]) == 0)
693 pagevec_release(&pvec);
695 pagevec_release(&pvec);
699 * async writeback completion handler.
701 * If we get an error, set the mapping error bit, but not the individual
704 static void writepages_finish(struct ceph_osd_request *req)
706 struct inode *inode = req->r_inode;
707 struct ceph_inode_info *ci = ceph_inode(inode);
708 struct ceph_osd_data *osd_data;
710 int num_pages, total_pages = 0;
712 int rc = req->r_result;
713 struct ceph_snap_context *snapc = req->r_snapc;
714 struct address_space *mapping = inode->i_mapping;
715 struct ceph_fs_client *fsc = ceph_inode_to_client(inode);
718 dout("writepages_finish %p rc %d\n", inode, rc);
720 mapping_set_error(mapping, rc);
721 ceph_set_error_write(ci);
723 ceph_clear_error_write(ci);
727 * We lost the cache cap, need to truncate the page before
728 * it is unlocked, otherwise we'd truncate it later in the
729 * page truncation thread, possibly losing some data that
732 remove_page = !(ceph_caps_issued(ci) &
733 (CEPH_CAP_FILE_CACHE|CEPH_CAP_FILE_LAZYIO));
735 /* clean all pages */
736 for (i = 0; i < req->r_num_ops; i++) {
737 if (req->r_ops[i].op != CEPH_OSD_OP_WRITE)
740 osd_data = osd_req_op_extent_osd_data(req, i);
741 BUG_ON(osd_data->type != CEPH_OSD_DATA_TYPE_PAGES);
742 num_pages = calc_pages_for((u64)osd_data->alignment,
743 (u64)osd_data->length);
744 total_pages += num_pages;
745 for (j = 0; j < num_pages; j++) {
746 page = osd_data->pages[j];
748 WARN_ON(!PageUptodate(page));
750 if (atomic_long_dec_return(&fsc->writeback_count) <
751 CONGESTION_OFF_THRESH(
752 fsc->mount_options->congestion_kb))
753 clear_bdi_congested(inode_to_bdi(inode),
756 ceph_put_snap_context(page_snap_context(page));
758 ClearPagePrivate(page);
759 dout("unlocking %p\n", page);
760 end_page_writeback(page);
763 generic_error_remove_page(inode->i_mapping,
768 dout("writepages_finish %p wrote %llu bytes cleaned %d pages\n",
769 inode, osd_data->length, rc >= 0 ? num_pages : 0);
771 ceph_release_pages(osd_data->pages, num_pages);
774 ceph_put_wrbuffer_cap_refs(ci, total_pages, snapc);
776 osd_data = osd_req_op_extent_osd_data(req, 0);
777 if (osd_data->pages_from_pool)
778 mempool_free(osd_data->pages,
779 ceph_sb_to_client(inode->i_sb)->wb_pagevec_pool);
781 kfree(osd_data->pages);
782 ceph_osdc_put_request(req);
786 * initiate async writeback
788 static int ceph_writepages_start(struct address_space *mapping,
789 struct writeback_control *wbc)
791 struct inode *inode = mapping->host;
792 struct ceph_inode_info *ci = ceph_inode(inode);
793 struct ceph_fs_client *fsc = ceph_inode_to_client(inode);
794 struct ceph_vino vino = ceph_vino(inode);
795 pgoff_t index, start_index, end = -1;
796 struct ceph_snap_context *snapc = NULL, *last_snapc = NULL, *pgsnapc;
799 unsigned int wsize = i_blocksize(inode);
800 struct ceph_osd_request *req = NULL;
801 struct ceph_writeback_ctl ceph_wbc;
802 bool should_loop, range_whole = false;
805 dout("writepages_start %p (mode=%s)\n", inode,
806 wbc->sync_mode == WB_SYNC_NONE ? "NONE" :
807 (wbc->sync_mode == WB_SYNC_ALL ? "ALL" : "HOLD"));
809 if (READ_ONCE(fsc->mount_state) == CEPH_MOUNT_SHUTDOWN) {
810 if (ci->i_wrbuffer_ref > 0) {
812 "writepage_start %p %lld forced umount\n",
813 inode, ceph_ino(inode));
815 mapping_set_error(mapping, -EIO);
816 return -EIO; /* we're in a forced umount, don't write! */
818 if (fsc->mount_options->wsize < wsize)
819 wsize = fsc->mount_options->wsize;
823 start_index = wbc->range_cyclic ? mapping->writeback_index : 0;
827 /* find oldest snap context with dirty data */
828 snapc = get_oldest_context(inode, &ceph_wbc, NULL);
830 /* hmm, why does writepages get called when there
832 dout(" no snap context with dirty data?\n");
835 dout(" oldest snapc is %p seq %lld (%d snaps)\n",
836 snapc, snapc->seq, snapc->num_snaps);
839 if (ceph_wbc.head_snapc && snapc != last_snapc) {
840 /* where to start/end? */
841 if (wbc->range_cyclic) {
846 dout(" cyclic, start at %lu\n", index);
848 index = wbc->range_start >> PAGE_SHIFT;
849 end = wbc->range_end >> PAGE_SHIFT;
850 if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX)
852 dout(" not cyclic, %lu to %lu\n", index, end);
854 } else if (!ceph_wbc.head_snapc) {
855 /* Do not respect wbc->range_{start,end}. Dirty pages
856 * in that range can be associated with newer snapc.
857 * They are not writeable until we write all dirty pages
858 * associated with 'snapc' get written */
861 dout(" non-head snapc, range whole\n");
864 ceph_put_snap_context(last_snapc);
867 while (!done && index <= end) {
868 int num_ops = 0, op_idx;
869 unsigned i, pvec_pages, max_pages, locked_pages = 0;
870 struct page **pages = NULL, **data_pages;
871 mempool_t *pool = NULL; /* Becomes non-null if mempool used */
873 pgoff_t strip_unit_end = 0;
874 u64 offset = 0, len = 0;
876 max_pages = wsize >> PAGE_SHIFT;
879 pvec_pages = pagevec_lookup_range_nr_tag(&pvec, mapping, &index,
880 end, PAGECACHE_TAG_DIRTY,
881 max_pages - locked_pages);
882 dout("pagevec_lookup_range_tag got %d\n", pvec_pages);
883 if (!pvec_pages && !locked_pages)
885 for (i = 0; i < pvec_pages && locked_pages < max_pages; i++) {
886 page = pvec.pages[i];
887 dout("? %p idx %lu\n", page, page->index);
888 if (locked_pages == 0)
889 lock_page(page); /* first page */
890 else if (!trylock_page(page))
893 /* only dirty pages, or our accounting breaks */
894 if (unlikely(!PageDirty(page)) ||
895 unlikely(page->mapping != mapping)) {
896 dout("!dirty or !mapping %p\n", page);
900 /* only if matching snap context */
901 pgsnapc = page_snap_context(page);
902 if (pgsnapc != snapc) {
903 dout("page snapc %p %lld != oldest %p %lld\n",
904 pgsnapc, pgsnapc->seq, snapc, snapc->seq);
906 !ceph_wbc.head_snapc &&
907 wbc->sync_mode != WB_SYNC_NONE)
912 if (page_offset(page) >= ceph_wbc.i_size) {
913 dout("%p page eof %llu\n",
914 page, ceph_wbc.i_size);
915 if (ceph_wbc.size_stable ||
916 page_offset(page) >= i_size_read(inode))
917 mapping->a_ops->invalidatepage(page,
922 if (strip_unit_end && (page->index > strip_unit_end)) {
923 dout("end of strip unit %p\n", page);
927 if (PageWriteback(page)) {
928 if (wbc->sync_mode == WB_SYNC_NONE) {
929 dout("%p under writeback\n", page);
933 dout("waiting on writeback %p\n", page);
934 wait_on_page_writeback(page);
937 if (!clear_page_dirty_for_io(page)) {
938 dout("%p !clear_page_dirty_for_io\n", page);
944 * We have something to write. If this is
945 * the first locked page this time through,
946 * calculate max possinle write size and
947 * allocate a page array
949 if (locked_pages == 0) {
954 /* prepare async write request */
955 offset = (u64)page_offset(page);
956 ceph_calc_file_object_mapping(&ci->i_layout,
963 strip_unit_end = page->index +
964 ((len - 1) >> PAGE_SHIFT);
967 max_pages = calc_pages_for(0, (u64)len);
968 pages = kmalloc_array(max_pages,
972 pool = fsc->wb_pagevec_pool;
973 pages = mempool_alloc(pool, GFP_NOFS);
978 } else if (page->index !=
979 (offset + len) >> PAGE_SHIFT) {
980 if (num_ops >= (pool ? CEPH_OSD_SLAB_OPS :
982 redirty_page_for_writepage(wbc, page);
988 offset = (u64)page_offset(page);
992 /* note position of first page in pvec */
993 dout("%p will write page %p idx %lu\n",
994 inode, page, page->index);
996 if (atomic_long_inc_return(&fsc->writeback_count) >
997 CONGESTION_ON_THRESH(
998 fsc->mount_options->congestion_kb)) {
999 set_bdi_congested(inode_to_bdi(inode),
1004 pages[locked_pages++] = page;
1005 pvec.pages[i] = NULL;
1010 /* did we get anything? */
1012 goto release_pvec_pages;
1015 /* shift unused page to beginning of pvec */
1016 for (j = 0; j < pvec_pages; j++) {
1020 pvec.pages[n] = pvec.pages[j];
1025 if (pvec_pages && i == pvec_pages &&
1026 locked_pages < max_pages) {
1027 dout("reached end pvec, trying for more\n");
1028 pagevec_release(&pvec);
1029 goto get_more_pages;
1034 offset = page_offset(pages[0]);
1037 req = ceph_osdc_new_request(&fsc->client->osdc,
1038 &ci->i_layout, vino,
1039 offset, &len, 0, num_ops,
1040 CEPH_OSD_OP_WRITE, CEPH_OSD_FLAG_WRITE,
1041 snapc, ceph_wbc.truncate_seq,
1042 ceph_wbc.truncate_size, false);
1044 req = ceph_osdc_new_request(&fsc->client->osdc,
1045 &ci->i_layout, vino,
1050 CEPH_OSD_FLAG_WRITE,
1051 snapc, ceph_wbc.truncate_seq,
1052 ceph_wbc.truncate_size, true);
1053 BUG_ON(IS_ERR(req));
1055 BUG_ON(len < page_offset(pages[locked_pages - 1]) +
1056 PAGE_SIZE - offset);
1058 req->r_callback = writepages_finish;
1059 req->r_inode = inode;
1061 /* Format the osd request message and submit the write */
1065 for (i = 0; i < locked_pages; i++) {
1066 u64 cur_offset = page_offset(pages[i]);
1067 if (offset + len != cur_offset) {
1068 if (op_idx + 1 == req->r_num_ops)
1070 osd_req_op_extent_dup_last(req, op_idx,
1071 cur_offset - offset);
1072 dout("writepages got pages at %llu~%llu\n",
1074 osd_req_op_extent_osd_data_pages(req, op_idx,
1077 osd_req_op_extent_update(req, op_idx, len);
1080 offset = cur_offset;
1081 data_pages = pages + i;
1085 set_page_writeback(pages[i]);
1089 if (ceph_wbc.size_stable) {
1090 len = min(len, ceph_wbc.i_size - offset);
1091 } else if (i == locked_pages) {
1092 /* writepages_finish() clears writeback pages
1093 * according to the data length, so make sure
1094 * data length covers all locked pages */
1095 u64 min_len = len + 1 - PAGE_SIZE;
1096 len = get_writepages_data_length(inode, pages[i - 1],
1098 len = max(len, min_len);
1100 dout("writepages got pages at %llu~%llu\n", offset, len);
1102 osd_req_op_extent_osd_data_pages(req, op_idx, data_pages, len,
1104 osd_req_op_extent_update(req, op_idx, len);
1106 BUG_ON(op_idx + 1 != req->r_num_ops);
1109 if (i < locked_pages) {
1110 BUG_ON(num_ops <= req->r_num_ops);
1111 num_ops -= req->r_num_ops;
1114 /* allocate new pages array for next request */
1116 pages = kmalloc_array(locked_pages, sizeof(*pages),
1119 pool = fsc->wb_pagevec_pool;
1120 pages = mempool_alloc(pool, GFP_NOFS);
1123 memcpy(pages, data_pages + i,
1124 locked_pages * sizeof(*pages));
1125 memset(data_pages + i, 0,
1126 locked_pages * sizeof(*pages));
1128 BUG_ON(num_ops != req->r_num_ops);
1129 index = pages[i - 1]->index + 1;
1130 /* request message now owns the pages array */
1134 req->r_mtime = inode->i_mtime;
1135 rc = ceph_osdc_start_request(&fsc->client->osdc, req, true);
1139 wbc->nr_to_write -= i;
1144 * We stop writing back only if we are not doing
1145 * integrity sync. In case of integrity sync we have to
1146 * keep going until we have written all the pages
1147 * we tagged for writeback prior to entering this loop.
1149 if (wbc->nr_to_write <= 0 && wbc->sync_mode == WB_SYNC_NONE)
1153 dout("pagevec_release on %d pages (%p)\n", (int)pvec.nr,
1154 pvec.nr ? pvec.pages[0] : NULL);
1155 pagevec_release(&pvec);
1158 if (should_loop && !done) {
1159 /* more to do; loop back to beginning of file */
1160 dout("writepages looping back to beginning of file\n");
1161 end = start_index - 1; /* OK even when start_index == 0 */
1163 /* to write dirty pages associated with next snapc,
1164 * we need to wait until current writes complete */
1165 if (wbc->sync_mode != WB_SYNC_NONE &&
1166 start_index == 0 && /* all dirty pages were checked */
1167 !ceph_wbc.head_snapc) {
1171 while ((index <= end) &&
1172 (nr = pagevec_lookup_tag(&pvec, mapping, &index,
1173 PAGECACHE_TAG_WRITEBACK))) {
1174 for (i = 0; i < nr; i++) {
1175 page = pvec.pages[i];
1176 if (page_snap_context(page) != snapc)
1178 wait_on_page_writeback(page);
1180 pagevec_release(&pvec);
1190 if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0))
1191 mapping->writeback_index = index;
1194 ceph_osdc_put_request(req);
1195 ceph_put_snap_context(last_snapc);
1196 dout("writepages dend - startone, rc = %d\n", rc);
1203 * See if a given @snapc is either writeable, or already written.
1205 static int context_is_writeable_or_written(struct inode *inode,
1206 struct ceph_snap_context *snapc)
1208 struct ceph_snap_context *oldest = get_oldest_context(inode, NULL, NULL);
1209 int ret = !oldest || snapc->seq <= oldest->seq;
1211 ceph_put_snap_context(oldest);
1216 * We are only allowed to write into/dirty the page if the page is
1217 * clean, or already dirty within the same snap context.
1219 * called with page locked.
1220 * return success with page locked,
1221 * or any failure (incl -EAGAIN) with page unlocked.
1223 static int ceph_update_writeable_page(struct file *file,
1224 loff_t pos, unsigned len,
1227 struct inode *inode = file_inode(file);
1228 struct ceph_fs_client *fsc = ceph_inode_to_client(inode);
1229 struct ceph_inode_info *ci = ceph_inode(inode);
1230 loff_t page_off = pos & PAGE_MASK;
1231 int pos_in_page = pos & ~PAGE_MASK;
1232 int end_in_page = pos_in_page + len;
1235 struct ceph_snap_context *snapc, *oldest;
1237 if (READ_ONCE(fsc->mount_state) == CEPH_MOUNT_SHUTDOWN) {
1238 dout(" page %p forced umount\n", page);
1244 /* writepages currently holds page lock, but if we change that later, */
1245 wait_on_page_writeback(page);
1247 snapc = page_snap_context(page);
1248 if (snapc && snapc != ci->i_head_snapc) {
1250 * this page is already dirty in another (older) snap
1251 * context! is it writeable now?
1253 oldest = get_oldest_context(inode, NULL, NULL);
1254 if (snapc->seq > oldest->seq) {
1255 ceph_put_snap_context(oldest);
1256 dout(" page %p snapc %p not current or oldest\n",
1259 * queue for writeback, and wait for snapc to
1260 * be writeable or written
1262 snapc = ceph_get_snap_context(snapc);
1264 ceph_queue_writeback(inode);
1265 r = wait_event_killable(ci->i_cap_wq,
1266 context_is_writeable_or_written(inode, snapc));
1267 ceph_put_snap_context(snapc);
1268 if (r == -ERESTARTSYS)
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",
1277 if (!clear_page_dirty_for_io(page))
1279 r = writepage_nounlock(page, NULL);
1285 if (PageUptodate(page)) {
1286 dout(" page %p already uptodate\n", page);
1291 if (pos_in_page == 0 && len == PAGE_SIZE)
1294 /* past end of file? */
1295 i_size = i_size_read(inode);
1297 if (page_off >= i_size ||
1298 (pos_in_page == 0 && (pos+len) >= i_size &&
1299 end_in_page - pos_in_page != PAGE_SIZE)) {
1300 dout(" zeroing %p 0 - %d and %d - %d\n",
1301 page, pos_in_page, end_in_page, (int)PAGE_SIZE);
1302 zero_user_segments(page,
1304 end_in_page, PAGE_SIZE);
1308 /* we need to read it. */
1309 r = ceph_do_readpage(file, page);
1311 if (r == -EINPROGRESS)
1322 * We are only allowed to write into/dirty the page if the page is
1323 * clean, or already dirty within the same snap context.
1325 static int ceph_write_begin(struct file *file, struct address_space *mapping,
1326 loff_t pos, unsigned len, unsigned flags,
1327 struct page **pagep, void **fsdata)
1329 struct inode *inode = file_inode(file);
1331 pgoff_t index = pos >> PAGE_SHIFT;
1336 page = grab_cache_page_write_begin(mapping, index, 0);
1340 dout("write_begin file %p inode %p page %p %d~%d\n", file,
1341 inode, page, (int)pos, (int)len);
1343 r = ceph_update_writeable_page(file, pos, len, page);
1348 } while (r == -EAGAIN);
1354 * we don't do anything in here that simple_write_end doesn't do
1355 * except adjust dirty page accounting
1357 static int ceph_write_end(struct file *file, struct address_space *mapping,
1358 loff_t pos, unsigned len, unsigned copied,
1359 struct page *page, void *fsdata)
1361 struct inode *inode = file_inode(file);
1362 bool check_cap = false;
1364 dout("write_end file %p inode %p page %p %d~%d (%d)\n", file,
1365 inode, page, (int)pos, (int)copied, (int)len);
1367 /* zero the stale part of the page if we did a short copy */
1368 if (!PageUptodate(page)) {
1373 SetPageUptodate(page);
1376 /* did file size increase? */
1377 if (pos+copied > i_size_read(inode))
1378 check_cap = ceph_inode_set_size(inode, pos+copied);
1380 set_page_dirty(page);
1387 ceph_check_caps(ceph_inode(inode), CHECK_CAPS_AUTHONLY, NULL);
1393 * we set .direct_IO to indicate direct io is supported, but since we
1394 * intercept O_DIRECT reads and writes early, this function should
1397 static ssize_t ceph_direct_io(struct kiocb *iocb, struct iov_iter *iter)
1403 const struct address_space_operations ceph_aops = {
1404 .readpage = ceph_readpage,
1405 .readpages = ceph_readpages,
1406 .writepage = ceph_writepage,
1407 .writepages = ceph_writepages_start,
1408 .write_begin = ceph_write_begin,
1409 .write_end = ceph_write_end,
1410 .set_page_dirty = ceph_set_page_dirty,
1411 .invalidatepage = ceph_invalidatepage,
1412 .releasepage = ceph_releasepage,
1413 .direct_IO = ceph_direct_io,
1416 static void ceph_block_sigs(sigset_t *oldset)
1419 siginitsetinv(&mask, sigmask(SIGKILL));
1420 sigprocmask(SIG_BLOCK, &mask, oldset);
1423 static void ceph_restore_sigs(sigset_t *oldset)
1425 sigprocmask(SIG_SETMASK, oldset, NULL);
1431 static vm_fault_t ceph_filemap_fault(struct vm_fault *vmf)
1433 struct vm_area_struct *vma = vmf->vma;
1434 struct inode *inode = file_inode(vma->vm_file);
1435 struct ceph_inode_info *ci = ceph_inode(inode);
1436 struct ceph_file_info *fi = vma->vm_file->private_data;
1437 struct page *pinned_page = NULL;
1438 loff_t off = vmf->pgoff << PAGE_SHIFT;
1441 vm_fault_t ret = VM_FAULT_SIGBUS;
1443 ceph_block_sigs(&oldset);
1445 dout("filemap_fault %p %llx.%llx %llu~%zd trying to get caps\n",
1446 inode, ceph_vinop(inode), off, (size_t)PAGE_SIZE);
1447 if (fi->fmode & CEPH_FILE_MODE_LAZY)
1448 want = CEPH_CAP_FILE_CACHE | CEPH_CAP_FILE_LAZYIO;
1450 want = CEPH_CAP_FILE_CACHE;
1453 err = ceph_get_caps(ci, CEPH_CAP_FILE_RD, want, -1, &got, &pinned_page);
1457 dout("filemap_fault %p %llu~%zd got cap refs on %s\n",
1458 inode, off, (size_t)PAGE_SIZE, ceph_cap_string(got));
1460 if ((got & (CEPH_CAP_FILE_CACHE | CEPH_CAP_FILE_LAZYIO)) ||
1461 ci->i_inline_version == CEPH_INLINE_NONE) {
1462 CEPH_DEFINE_RW_CONTEXT(rw_ctx, got);
1463 ceph_add_rw_context(fi, &rw_ctx);
1464 ret = filemap_fault(vmf);
1465 ceph_del_rw_context(fi, &rw_ctx);
1466 dout("filemap_fault %p %llu~%zd drop cap refs %s ret %x\n",
1467 inode, off, (size_t)PAGE_SIZE,
1468 ceph_cap_string(got), ret);
1473 put_page(pinned_page);
1474 ceph_put_cap_refs(ci, got);
1479 /* read inline data */
1480 if (off >= PAGE_SIZE) {
1481 /* does not support inline data > PAGE_SIZE */
1482 ret = VM_FAULT_SIGBUS;
1484 struct address_space *mapping = inode->i_mapping;
1485 struct page *page = find_or_create_page(mapping, 0,
1486 mapping_gfp_constraint(mapping,
1492 err = __ceph_do_getattr(inode, page,
1493 CEPH_STAT_CAP_INLINE_DATA, true);
1494 if (err < 0 || off >= i_size_read(inode)) {
1497 ret = vmf_error(err);
1500 if (err < PAGE_SIZE)
1501 zero_user_segment(page, err, PAGE_SIZE);
1503 flush_dcache_page(page);
1504 SetPageUptodate(page);
1506 ret = VM_FAULT_MAJOR | VM_FAULT_LOCKED;
1508 dout("filemap_fault %p %llu~%zd read inline data ret %x\n",
1509 inode, off, (size_t)PAGE_SIZE, ret);
1512 ceph_restore_sigs(&oldset);
1514 ret = vmf_error(err);
1520 * Reuse write_begin here for simplicity.
1522 static vm_fault_t ceph_page_mkwrite(struct vm_fault *vmf)
1524 struct vm_area_struct *vma = vmf->vma;
1525 struct inode *inode = file_inode(vma->vm_file);
1526 struct ceph_inode_info *ci = ceph_inode(inode);
1527 struct ceph_file_info *fi = vma->vm_file->private_data;
1528 struct ceph_cap_flush *prealloc_cf;
1529 struct page *page = vmf->page;
1530 loff_t off = page_offset(page);
1531 loff_t size = i_size_read(inode);
1535 vm_fault_t ret = VM_FAULT_SIGBUS;
1537 prealloc_cf = ceph_alloc_cap_flush();
1539 return VM_FAULT_OOM;
1541 ceph_block_sigs(&oldset);
1543 if (ci->i_inline_version != CEPH_INLINE_NONE) {
1544 struct page *locked_page = NULL;
1549 err = ceph_uninline_data(vma->vm_file, locked_page);
1551 unlock_page(locked_page);
1556 if (off + PAGE_SIZE <= size)
1559 len = size & ~PAGE_MASK;
1561 dout("page_mkwrite %p %llx.%llx %llu~%zd getting caps i_size %llu\n",
1562 inode, ceph_vinop(inode), off, len, size);
1563 if (fi->fmode & CEPH_FILE_MODE_LAZY)
1564 want = CEPH_CAP_FILE_BUFFER | CEPH_CAP_FILE_LAZYIO;
1566 want = CEPH_CAP_FILE_BUFFER;
1569 err = ceph_get_caps(ci, CEPH_CAP_FILE_WR, want, off + len,
1574 dout("page_mkwrite %p %llu~%zd got cap refs on %s\n",
1575 inode, off, len, ceph_cap_string(got));
1577 /* Update time before taking page lock */
1578 file_update_time(vma->vm_file);
1583 if ((off > size) || (page->mapping != inode->i_mapping)) {
1585 ret = VM_FAULT_NOPAGE;
1589 err = ceph_update_writeable_page(vma->vm_file, off, len, page);
1591 /* success. we'll keep the page locked. */
1592 set_page_dirty(page);
1593 ret = VM_FAULT_LOCKED;
1595 } while (err == -EAGAIN);
1597 if (ret == VM_FAULT_LOCKED ||
1598 ci->i_inline_version != CEPH_INLINE_NONE) {
1600 spin_lock(&ci->i_ceph_lock);
1601 ci->i_inline_version = CEPH_INLINE_NONE;
1602 dirty = __ceph_mark_dirty_caps(ci, CEPH_CAP_FILE_WR,
1604 spin_unlock(&ci->i_ceph_lock);
1606 __mark_inode_dirty(inode, dirty);
1609 dout("page_mkwrite %p %llu~%zd dropping cap refs on %s ret %x\n",
1610 inode, off, len, ceph_cap_string(got), ret);
1611 ceph_put_cap_refs(ci, got);
1613 ceph_restore_sigs(&oldset);
1614 ceph_free_cap_flush(prealloc_cf);
1616 ret = vmf_error(err);
1620 void ceph_fill_inline_data(struct inode *inode, struct page *locked_page,
1621 char *data, size_t len)
1623 struct address_space *mapping = inode->i_mapping;
1629 if (i_size_read(inode) == 0)
1631 page = find_or_create_page(mapping, 0,
1632 mapping_gfp_constraint(mapping,
1636 if (PageUptodate(page)) {
1643 dout("fill_inline_data %p %llx.%llx len %zu locked_page %p\n",
1644 inode, ceph_vinop(inode), len, locked_page);
1647 void *kaddr = kmap_atomic(page);
1648 memcpy(kaddr, data, len);
1649 kunmap_atomic(kaddr);
1652 if (page != locked_page) {
1653 if (len < PAGE_SIZE)
1654 zero_user_segment(page, len, PAGE_SIZE);
1656 flush_dcache_page(page);
1658 SetPageUptodate(page);
1664 int ceph_uninline_data(struct file *filp, struct page *locked_page)
1666 struct inode *inode = file_inode(filp);
1667 struct ceph_inode_info *ci = ceph_inode(inode);
1668 struct ceph_fs_client *fsc = ceph_inode_to_client(inode);
1669 struct ceph_osd_request *req;
1670 struct page *page = NULL;
1671 u64 len, inline_version;
1673 bool from_pagecache = false;
1675 spin_lock(&ci->i_ceph_lock);
1676 inline_version = ci->i_inline_version;
1677 spin_unlock(&ci->i_ceph_lock);
1679 dout("uninline_data %p %llx.%llx inline_version %llu\n",
1680 inode, ceph_vinop(inode), inline_version);
1682 if (inline_version == 1 || /* initial version, no data */
1683 inline_version == CEPH_INLINE_NONE)
1688 WARN_ON(!PageUptodate(page));
1689 } else if (ceph_caps_issued(ci) &
1690 (CEPH_CAP_FILE_CACHE|CEPH_CAP_FILE_LAZYIO)) {
1691 page = find_get_page(inode->i_mapping, 0);
1693 if (PageUptodate(page)) {
1694 from_pagecache = true;
1704 len = i_size_read(inode);
1705 if (len > PAGE_SIZE)
1708 page = __page_cache_alloc(GFP_NOFS);
1713 err = __ceph_do_getattr(inode, page,
1714 CEPH_STAT_CAP_INLINE_DATA, true);
1716 /* no inline data */
1717 if (err == -ENODATA)
1724 req = ceph_osdc_new_request(&fsc->client->osdc, &ci->i_layout,
1725 ceph_vino(inode), 0, &len, 0, 1,
1726 CEPH_OSD_OP_CREATE, CEPH_OSD_FLAG_WRITE,
1733 req->r_mtime = inode->i_mtime;
1734 err = ceph_osdc_start_request(&fsc->client->osdc, req, false);
1736 err = ceph_osdc_wait_request(&fsc->client->osdc, req);
1737 ceph_osdc_put_request(req);
1741 req = ceph_osdc_new_request(&fsc->client->osdc, &ci->i_layout,
1742 ceph_vino(inode), 0, &len, 1, 3,
1743 CEPH_OSD_OP_WRITE, CEPH_OSD_FLAG_WRITE,
1744 NULL, ci->i_truncate_seq,
1745 ci->i_truncate_size, false);
1751 osd_req_op_extent_osd_data_pages(req, 1, &page, len, 0, false, false);
1754 __le64 xattr_buf = cpu_to_le64(inline_version);
1755 err = osd_req_op_xattr_init(req, 0, CEPH_OSD_OP_CMPXATTR,
1756 "inline_version", &xattr_buf,
1758 CEPH_OSD_CMPXATTR_OP_GT,
1759 CEPH_OSD_CMPXATTR_MODE_U64);
1766 int xattr_len = snprintf(xattr_buf, sizeof(xattr_buf),
1767 "%llu", inline_version);
1768 err = osd_req_op_xattr_init(req, 2, CEPH_OSD_OP_SETXATTR,
1770 xattr_buf, xattr_len, 0, 0);
1775 req->r_mtime = inode->i_mtime;
1776 err = ceph_osdc_start_request(&fsc->client->osdc, req, false);
1778 err = ceph_osdc_wait_request(&fsc->client->osdc, req);
1780 ceph_osdc_put_request(req);
1781 if (err == -ECANCELED)
1784 if (page && page != locked_page) {
1785 if (from_pagecache) {
1789 __free_pages(page, 0);
1792 dout("uninline_data %p %llx.%llx inline_version %llu = %d\n",
1793 inode, ceph_vinop(inode), inline_version, err);
1797 static const struct vm_operations_struct ceph_vmops = {
1798 .fault = ceph_filemap_fault,
1799 .page_mkwrite = ceph_page_mkwrite,
1802 int ceph_mmap(struct file *file, struct vm_area_struct *vma)
1804 struct address_space *mapping = file->f_mapping;
1806 if (!mapping->a_ops->readpage)
1808 file_accessed(file);
1809 vma->vm_ops = &ceph_vmops;
1818 static int __ceph_pool_perm_get(struct ceph_inode_info *ci,
1819 s64 pool, struct ceph_string *pool_ns)
1821 struct ceph_fs_client *fsc = ceph_inode_to_client(&ci->vfs_inode);
1822 struct ceph_mds_client *mdsc = fsc->mdsc;
1823 struct ceph_osd_request *rd_req = NULL, *wr_req = NULL;
1824 struct rb_node **p, *parent;
1825 struct ceph_pool_perm *perm;
1826 struct page **pages;
1828 int err = 0, err2 = 0, have = 0;
1830 down_read(&mdsc->pool_perm_rwsem);
1831 p = &mdsc->pool_perm_tree.rb_node;
1833 perm = rb_entry(*p, struct ceph_pool_perm, node);
1834 if (pool < perm->pool)
1836 else if (pool > perm->pool)
1837 p = &(*p)->rb_right;
1839 int ret = ceph_compare_string(pool_ns,
1845 p = &(*p)->rb_right;
1852 up_read(&mdsc->pool_perm_rwsem);
1857 dout("__ceph_pool_perm_get pool %lld ns %.*s no perm cached\n",
1858 pool, (int)pool_ns->len, pool_ns->str);
1860 dout("__ceph_pool_perm_get pool %lld no perm cached\n", pool);
1862 down_write(&mdsc->pool_perm_rwsem);
1863 p = &mdsc->pool_perm_tree.rb_node;
1867 perm = rb_entry(parent, struct ceph_pool_perm, node);
1868 if (pool < perm->pool)
1870 else if (pool > perm->pool)
1871 p = &(*p)->rb_right;
1873 int ret = ceph_compare_string(pool_ns,
1879 p = &(*p)->rb_right;
1887 up_write(&mdsc->pool_perm_rwsem);
1891 rd_req = ceph_osdc_alloc_request(&fsc->client->osdc, NULL,
1892 1, false, GFP_NOFS);
1898 rd_req->r_flags = CEPH_OSD_FLAG_READ;
1899 osd_req_op_init(rd_req, 0, CEPH_OSD_OP_STAT, 0);
1900 rd_req->r_base_oloc.pool = pool;
1902 rd_req->r_base_oloc.pool_ns = ceph_get_string(pool_ns);
1903 ceph_oid_printf(&rd_req->r_base_oid, "%llx.00000000", ci->i_vino.ino);
1905 err = ceph_osdc_alloc_messages(rd_req, GFP_NOFS);
1909 wr_req = ceph_osdc_alloc_request(&fsc->client->osdc, NULL,
1910 1, false, GFP_NOFS);
1916 wr_req->r_flags = CEPH_OSD_FLAG_WRITE;
1917 osd_req_op_init(wr_req, 0, CEPH_OSD_OP_CREATE, CEPH_OSD_OP_FLAG_EXCL);
1918 ceph_oloc_copy(&wr_req->r_base_oloc, &rd_req->r_base_oloc);
1919 ceph_oid_copy(&wr_req->r_base_oid, &rd_req->r_base_oid);
1921 err = ceph_osdc_alloc_messages(wr_req, GFP_NOFS);
1925 /* one page should be large enough for STAT data */
1926 pages = ceph_alloc_page_vector(1, GFP_KERNEL);
1927 if (IS_ERR(pages)) {
1928 err = PTR_ERR(pages);
1932 osd_req_op_raw_data_in_pages(rd_req, 0, pages, PAGE_SIZE,
1934 err = ceph_osdc_start_request(&fsc->client->osdc, rd_req, false);
1936 wr_req->r_mtime = ci->vfs_inode.i_mtime;
1937 err2 = ceph_osdc_start_request(&fsc->client->osdc, wr_req, false);
1940 err = ceph_osdc_wait_request(&fsc->client->osdc, rd_req);
1942 err2 = ceph_osdc_wait_request(&fsc->client->osdc, wr_req);
1944 if (err >= 0 || err == -ENOENT)
1946 else if (err != -EPERM)
1949 if (err2 == 0 || err2 == -EEXIST)
1951 else if (err2 != -EPERM) {
1956 pool_ns_len = pool_ns ? pool_ns->len : 0;
1957 perm = kmalloc(sizeof(*perm) + pool_ns_len + 1, GFP_NOFS);
1965 perm->pool_ns_len = pool_ns_len;
1966 if (pool_ns_len > 0)
1967 memcpy(perm->pool_ns, pool_ns->str, pool_ns_len);
1968 perm->pool_ns[pool_ns_len] = 0;
1970 rb_link_node(&perm->node, parent, p);
1971 rb_insert_color(&perm->node, &mdsc->pool_perm_tree);
1974 up_write(&mdsc->pool_perm_rwsem);
1976 ceph_osdc_put_request(rd_req);
1977 ceph_osdc_put_request(wr_req);
1982 dout("__ceph_pool_perm_get pool %lld ns %.*s result = %d\n",
1983 pool, (int)pool_ns->len, pool_ns->str, err);
1985 dout("__ceph_pool_perm_get pool %lld result = %d\n", pool, err);
1989 int ceph_pool_perm_check(struct ceph_inode_info *ci, int need)
1992 struct ceph_string *pool_ns;
1995 if (ci->i_vino.snap != CEPH_NOSNAP) {
1997 * Pool permission check needs to write to the first object.
1998 * But for snapshot, head of the first object may have alread
1999 * been deleted. Skip check to avoid creating orphan object.
2004 if (ceph_test_mount_opt(ceph_inode_to_client(&ci->vfs_inode),
2008 spin_lock(&ci->i_ceph_lock);
2009 flags = ci->i_ceph_flags;
2010 pool = ci->i_layout.pool_id;
2011 spin_unlock(&ci->i_ceph_lock);
2013 if (flags & CEPH_I_POOL_PERM) {
2014 if ((need & CEPH_CAP_FILE_RD) && !(flags & CEPH_I_POOL_RD)) {
2015 dout("ceph_pool_perm_check pool %lld no read perm\n",
2019 if ((need & CEPH_CAP_FILE_WR) && !(flags & CEPH_I_POOL_WR)) {
2020 dout("ceph_pool_perm_check pool %lld no write perm\n",
2027 pool_ns = ceph_try_get_string(ci->i_layout.pool_ns);
2028 ret = __ceph_pool_perm_get(ci, pool, pool_ns);
2029 ceph_put_string(pool_ns);
2033 flags = CEPH_I_POOL_PERM;
2034 if (ret & POOL_READ)
2035 flags |= CEPH_I_POOL_RD;
2036 if (ret & POOL_WRITE)
2037 flags |= CEPH_I_POOL_WR;
2039 spin_lock(&ci->i_ceph_lock);
2040 if (pool == ci->i_layout.pool_id &&
2041 pool_ns == rcu_dereference_raw(ci->i_layout.pool_ns)) {
2042 ci->i_ceph_flags |= flags;
2044 pool = ci->i_layout.pool_id;
2045 flags = ci->i_ceph_flags;
2047 spin_unlock(&ci->i_ceph_lock);
2051 void ceph_pool_perm_destroy(struct ceph_mds_client *mdsc)
2053 struct ceph_pool_perm *perm;
2056 while (!RB_EMPTY_ROOT(&mdsc->pool_perm_tree)) {
2057 n = rb_first(&mdsc->pool_perm_tree);
2058 perm = rb_entry(n, struct ceph_pool_perm, node);
2059 rb_erase(n, &mdsc->pool_perm_tree);