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 inode = page->mapping->host;
150 ci = ceph_inode(inode);
152 if (offset != 0 || length != PAGE_SIZE) {
153 dout("%p invalidatepage %p idx %lu partial dirty page %u~%u\n",
154 inode, page, page->index, offset, length);
158 ceph_invalidate_fscache_page(inode, page);
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 g)
175 dout("%p releasepage %p idx %lu (%sdirty)\n", page->mapping->host,
176 page, page->index, PageDirty(page) ? "" : "not ");
178 /* Can we release the page from the cache? */
179 if (!ceph_release_fscache_page(page, g))
182 return !PagePrivate(page);
186 * Read some contiguous pages. If we cross a stripe boundary, shorten
187 * *plen. Return number of bytes read, or error.
189 static int ceph_sync_readpages(struct ceph_fs_client *fsc,
190 struct ceph_vino vino,
191 struct ceph_file_layout *layout,
193 u32 truncate_seq, u64 truncate_size,
194 struct page **pages, int num_pages,
197 struct ceph_osd_client *osdc = &fsc->client->osdc;
198 struct ceph_osd_request *req;
201 dout("readpages on ino %llx.%llx on %llu~%llu\n", vino.ino,
202 vino.snap, off, *plen);
203 req = ceph_osdc_new_request(osdc, layout, vino, off, plen, 0, 1,
204 CEPH_OSD_OP_READ, CEPH_OSD_FLAG_READ,
205 NULL, truncate_seq, truncate_size,
210 /* it may be a short read due to an object boundary */
211 osd_req_op_extent_osd_data_pages(req, 0,
212 pages, *plen, page_align, false, false);
214 dout("readpages final extent is %llu~%llu (%llu bytes align %d)\n",
215 off, *plen, *plen, page_align);
217 rc = ceph_osdc_start_request(osdc, req, false);
219 rc = ceph_osdc_wait_request(osdc, req);
221 ceph_update_read_latency(&fsc->mdsc->metric, req->r_start_latency,
222 req->r_end_latency, rc);
224 ceph_osdc_put_request(req);
225 dout("readpages result %d\n", rc);
230 * read a single page, without unlocking it.
232 static int ceph_do_readpage(struct file *filp, struct page *page)
234 struct inode *inode = file_inode(filp);
235 struct ceph_inode_info *ci = ceph_inode(inode);
236 struct ceph_fs_client *fsc = ceph_inode_to_client(inode);
238 u64 off = page_offset(page);
241 if (off >= i_size_read(inode)) {
242 zero_user_segment(page, 0, PAGE_SIZE);
243 SetPageUptodate(page);
247 if (ci->i_inline_version != CEPH_INLINE_NONE) {
249 * Uptodate inline data should have been added
250 * into page cache while getting Fcr caps.
254 zero_user_segment(page, 0, PAGE_SIZE);
255 SetPageUptodate(page);
259 err = ceph_readpage_from_fscache(inode, page);
263 dout("readpage inode %p file %p page %p index %lu\n",
264 inode, filp, page, page->index);
265 err = ceph_sync_readpages(fsc, ceph_vino(inode),
266 &ci->i_layout, off, &len,
267 ci->i_truncate_seq, ci->i_truncate_size,
273 ceph_fscache_readpage_cancel(inode, page);
274 if (err == -EBLACKLISTED)
275 fsc->blacklisted = true;
279 /* zero fill remainder of page */
280 zero_user_segment(page, err, PAGE_SIZE);
282 flush_dcache_page(page);
284 SetPageUptodate(page);
285 ceph_readpage_to_fscache(inode, page);
288 return err < 0 ? err : 0;
291 static int ceph_readpage(struct file *filp, struct page *page)
293 int r = ceph_do_readpage(filp, page);
294 if (r != -EINPROGRESS)
302 * Finish an async read(ahead) op.
304 static void finish_read(struct ceph_osd_request *req)
306 struct inode *inode = req->r_inode;
307 struct ceph_fs_client *fsc = ceph_inode_to_client(inode);
308 struct ceph_osd_data *osd_data;
309 int rc = req->r_result <= 0 ? req->r_result : 0;
310 int bytes = req->r_result >= 0 ? req->r_result : 0;
314 dout("finish_read %p req %p rc %d bytes %d\n", inode, req, rc, bytes);
315 if (rc == -EBLACKLISTED)
316 ceph_inode_to_client(inode)->blacklisted = true;
318 /* unlock all pages, zeroing any data we didn't read */
319 osd_data = osd_req_op_extent_osd_data(req, 0);
320 BUG_ON(osd_data->type != CEPH_OSD_DATA_TYPE_PAGES);
321 num_pages = calc_pages_for((u64)osd_data->alignment,
322 (u64)osd_data->length);
323 for (i = 0; i < num_pages; i++) {
324 struct page *page = osd_data->pages[i];
326 if (rc < 0 && rc != -ENOENT) {
327 ceph_fscache_readpage_cancel(inode, page);
330 if (bytes < (int)PAGE_SIZE) {
331 /* zero (remainder of) page */
332 int s = bytes < 0 ? 0 : bytes;
333 zero_user_segment(page, s, PAGE_SIZE);
335 dout("finish_read %p uptodate %p idx %lu\n", inode, page,
337 flush_dcache_page(page);
338 SetPageUptodate(page);
339 ceph_readpage_to_fscache(inode, page);
346 ceph_update_read_latency(&fsc->mdsc->metric, req->r_start_latency,
347 req->r_end_latency, rc);
349 kfree(osd_data->pages);
353 * start an async read(ahead) operation. return nr_pages we submitted
354 * a read for on success, or negative error code.
356 static int start_read(struct inode *inode, struct ceph_rw_context *rw_ctx,
357 struct list_head *page_list, int max)
359 struct ceph_osd_client *osdc =
360 &ceph_inode_to_client(inode)->client->osdc;
361 struct ceph_inode_info *ci = ceph_inode(inode);
362 struct page *page = lru_to_page(page_list);
363 struct ceph_vino vino;
364 struct ceph_osd_request *req;
375 /* caller of readpages does not hold buffer and read caps
376 * (fadvise, madvise and readahead cases) */
377 int want = CEPH_CAP_FILE_CACHE;
378 ret = ceph_try_get_caps(inode, CEPH_CAP_FILE_RD, want,
381 dout("start_read %p, error getting cap\n", inode);
382 } else if (!(got & want)) {
383 dout("start_read %p, no cache cap\n", inode);
388 ceph_put_cap_refs(ci, got);
389 while (!list_empty(page_list)) {
390 page = lru_to_page(page_list);
391 list_del(&page->lru);
398 off = (u64) page_offset(page);
401 next_index = page->index;
402 list_for_each_entry_reverse(page, page_list, lru) {
403 if (page->index != next_index)
407 if (max && nr_pages == max)
410 len = nr_pages << PAGE_SHIFT;
411 dout("start_read %p nr_pages %d is %lld~%lld\n", inode, nr_pages,
413 vino = ceph_vino(inode);
414 req = ceph_osdc_new_request(osdc, &ci->i_layout, vino, off, &len,
415 0, 1, CEPH_OSD_OP_READ,
416 CEPH_OSD_FLAG_READ, NULL,
417 ci->i_truncate_seq, ci->i_truncate_size,
424 /* build page vector */
425 nr_pages = calc_pages_for(0, len);
426 pages = kmalloc_array(nr_pages, sizeof(*pages), GFP_KERNEL);
431 for (i = 0; i < nr_pages; ++i) {
432 page = list_entry(page_list->prev, struct page, lru);
433 BUG_ON(PageLocked(page));
434 list_del(&page->lru);
436 dout("start_read %p adding %p idx %lu\n", inode, page,
438 if (add_to_page_cache_lru(page, &inode->i_data, page->index,
440 ceph_fscache_uncache_page(inode, page);
442 dout("start_read %p add_to_page_cache failed %p\n",
446 len = nr_pages << PAGE_SHIFT;
447 osd_req_op_extent_update(req, 0, len);
454 osd_req_op_extent_osd_data_pages(req, 0, pages, len, 0, false, false);
455 req->r_callback = finish_read;
456 req->r_inode = inode;
458 dout("start_read %p starting %p %lld~%lld\n", inode, req, off, len);
459 ret = ceph_osdc_start_request(osdc, req, false);
462 ceph_osdc_put_request(req);
464 /* After adding locked pages to page cache, the inode holds cache cap.
465 * So we can drop our cap refs. */
467 ceph_put_cap_refs(ci, got);
472 for (i = 0; i < nr_pages; ++i) {
473 ceph_fscache_readpage_cancel(inode, pages[i]);
474 unlock_page(pages[i]);
476 ceph_put_page_vector(pages, nr_pages, false);
478 ceph_osdc_put_request(req);
481 ceph_put_cap_refs(ci, got);
487 * Read multiple pages. Leave pages we don't read + unlock in page_list;
488 * the caller (VM) cleans them up.
490 static int ceph_readpages(struct file *file, struct address_space *mapping,
491 struct list_head *page_list, unsigned nr_pages)
493 struct inode *inode = file_inode(file);
494 struct ceph_fs_client *fsc = ceph_inode_to_client(inode);
495 struct ceph_file_info *fi = file->private_data;
496 struct ceph_rw_context *rw_ctx;
500 if (ceph_inode(inode)->i_inline_version != CEPH_INLINE_NONE)
503 rc = ceph_readpages_from_fscache(mapping->host, mapping, page_list,
509 rw_ctx = ceph_find_rw_context(fi);
510 max = fsc->mount_options->rsize >> PAGE_SHIFT;
511 dout("readpages %p file %p ctx %p nr_pages %d max %d\n",
512 inode, file, rw_ctx, nr_pages, max);
513 while (!list_empty(page_list)) {
514 rc = start_read(inode, rw_ctx, page_list, max);
519 ceph_fscache_readpages_cancel(inode, page_list);
521 dout("readpages %p file %p ret %d\n", inode, file, rc);
525 struct ceph_writeback_ctl
535 * Get ref for the oldest snapc for an inode with dirty data... that is, the
536 * only snap context we are allowed to write back.
538 static struct ceph_snap_context *
539 get_oldest_context(struct inode *inode, struct ceph_writeback_ctl *ctl,
540 struct ceph_snap_context *page_snapc)
542 struct ceph_inode_info *ci = ceph_inode(inode);
543 struct ceph_snap_context *snapc = NULL;
544 struct ceph_cap_snap *capsnap = NULL;
546 spin_lock(&ci->i_ceph_lock);
547 list_for_each_entry(capsnap, &ci->i_cap_snaps, ci_item) {
548 dout(" cap_snap %p snapc %p has %d dirty pages\n", capsnap,
549 capsnap->context, capsnap->dirty_pages);
550 if (!capsnap->dirty_pages)
553 /* get i_size, truncate_{seq,size} for page_snapc? */
554 if (snapc && capsnap->context != page_snapc)
558 if (capsnap->writing) {
559 ctl->i_size = i_size_read(inode);
560 ctl->size_stable = false;
562 ctl->i_size = capsnap->size;
563 ctl->size_stable = true;
565 ctl->truncate_size = capsnap->truncate_size;
566 ctl->truncate_seq = capsnap->truncate_seq;
567 ctl->head_snapc = false;
573 snapc = ceph_get_snap_context(capsnap->context);
575 page_snapc == snapc ||
576 page_snapc->seq > snapc->seq)
579 if (!snapc && ci->i_wrbuffer_ref_head) {
580 snapc = ceph_get_snap_context(ci->i_head_snapc);
581 dout(" head snapc %p has %d dirty pages\n",
582 snapc, ci->i_wrbuffer_ref_head);
584 ctl->i_size = i_size_read(inode);
585 ctl->truncate_size = ci->i_truncate_size;
586 ctl->truncate_seq = ci->i_truncate_seq;
587 ctl->size_stable = false;
588 ctl->head_snapc = true;
591 spin_unlock(&ci->i_ceph_lock);
595 static u64 get_writepages_data_length(struct inode *inode,
596 struct page *page, u64 start)
598 struct ceph_inode_info *ci = ceph_inode(inode);
599 struct ceph_snap_context *snapc = page_snap_context(page);
600 struct ceph_cap_snap *capsnap = NULL;
601 u64 end = i_size_read(inode);
603 if (snapc != ci->i_head_snapc) {
605 spin_lock(&ci->i_ceph_lock);
606 list_for_each_entry(capsnap, &ci->i_cap_snaps, ci_item) {
607 if (capsnap->context == snapc) {
608 if (!capsnap->writing)
614 spin_unlock(&ci->i_ceph_lock);
617 if (end > page_offset(page) + PAGE_SIZE)
618 end = page_offset(page) + PAGE_SIZE;
619 return end > start ? end - start : 0;
623 * do a synchronous write on N pages
625 static int ceph_sync_writepages(struct ceph_fs_client *fsc,
626 struct ceph_vino vino,
627 struct ceph_file_layout *layout,
628 struct ceph_snap_context *snapc,
630 u32 truncate_seq, u64 truncate_size,
631 struct timespec64 *mtime,
632 struct page **pages, int num_pages)
634 struct ceph_osd_client *osdc = &fsc->client->osdc;
635 struct ceph_osd_request *req;
637 int page_align = off & ~PAGE_MASK;
639 req = ceph_osdc_new_request(osdc, layout, vino, off, &len, 0, 1,
640 CEPH_OSD_OP_WRITE, CEPH_OSD_FLAG_WRITE,
641 snapc, truncate_seq, truncate_size,
646 /* it may be a short write due to an object boundary */
647 osd_req_op_extent_osd_data_pages(req, 0, pages, len, page_align,
649 dout("writepages %llu~%llu (%llu bytes)\n", off, len, len);
651 req->r_mtime = *mtime;
652 rc = ceph_osdc_start_request(osdc, req, true);
654 rc = ceph_osdc_wait_request(osdc, req);
656 ceph_update_write_latency(&fsc->mdsc->metric, req->r_start_latency,
657 req->r_end_latency, rc);
659 ceph_osdc_put_request(req);
662 dout("writepages result %d\n", rc);
667 * Write a single page, but leave the page locked.
669 * If we get a write error, mark the mapping for error, but still adjust the
670 * dirty page accounting (i.e., page is no longer dirty).
672 static int writepage_nounlock(struct page *page, struct writeback_control *wbc)
675 struct ceph_inode_info *ci;
676 struct ceph_fs_client *fsc;
677 struct ceph_snap_context *snapc, *oldest;
678 loff_t page_off = page_offset(page);
679 int err, len = PAGE_SIZE;
680 struct ceph_writeback_ctl ceph_wbc;
682 dout("writepage %p idx %lu\n", page, page->index);
684 inode = page->mapping->host;
685 ci = ceph_inode(inode);
686 fsc = ceph_inode_to_client(inode);
688 /* verify this is a writeable snap context */
689 snapc = page_snap_context(page);
691 dout("writepage %p page %p not dirty?\n", inode, page);
694 oldest = get_oldest_context(inode, &ceph_wbc, snapc);
695 if (snapc->seq > oldest->seq) {
696 dout("writepage %p page %p snapc %p not writeable - noop\n",
698 /* we should only noop if called by kswapd */
699 WARN_ON(!(current->flags & PF_MEMALLOC));
700 ceph_put_snap_context(oldest);
701 redirty_page_for_writepage(wbc, page);
704 ceph_put_snap_context(oldest);
706 /* is this a partial page at end of file? */
707 if (page_off >= ceph_wbc.i_size) {
708 dout("%p page eof %llu\n", page, ceph_wbc.i_size);
709 page->mapping->a_ops->invalidatepage(page, 0, PAGE_SIZE);
713 if (ceph_wbc.i_size < page_off + len)
714 len = ceph_wbc.i_size - page_off;
716 dout("writepage %p page %p index %lu on %llu~%u snapc %p seq %lld\n",
717 inode, page, page->index, page_off, len, snapc, snapc->seq);
719 if (atomic_long_inc_return(&fsc->writeback_count) >
720 CONGESTION_ON_THRESH(fsc->mount_options->congestion_kb))
721 set_bdi_congested(inode_to_bdi(inode), BLK_RW_ASYNC);
723 set_page_writeback(page);
724 err = ceph_sync_writepages(fsc, ceph_vino(inode),
725 &ci->i_layout, snapc, page_off, len,
726 ceph_wbc.truncate_seq,
727 ceph_wbc.truncate_size,
728 &inode->i_mtime, &page, 1);
730 struct writeback_control tmp_wbc;
733 if (err == -ERESTARTSYS) {
734 /* killed by SIGKILL */
735 dout("writepage interrupted page %p\n", page);
736 redirty_page_for_writepage(wbc, page);
737 end_page_writeback(page);
740 if (err == -EBLACKLISTED)
741 fsc->blacklisted = true;
742 dout("writepage setting page/mapping error %d %p\n",
744 mapping_set_error(&inode->i_data, err);
745 wbc->pages_skipped++;
747 dout("writepage cleaned page %p\n", page);
748 err = 0; /* vfs expects us to return 0 */
751 ClearPagePrivate(page);
752 end_page_writeback(page);
753 ceph_put_wrbuffer_cap_refs(ci, 1, snapc);
754 ceph_put_snap_context(snapc); /* page's reference */
756 if (atomic_long_dec_return(&fsc->writeback_count) <
757 CONGESTION_OFF_THRESH(fsc->mount_options->congestion_kb))
758 clear_bdi_congested(inode_to_bdi(inode), BLK_RW_ASYNC);
763 static int ceph_writepage(struct page *page, struct writeback_control *wbc)
766 struct inode *inode = page->mapping->host;
769 err = writepage_nounlock(page, wbc);
770 if (err == -ERESTARTSYS) {
771 /* direct memory reclaimer was killed by SIGKILL. return 0
772 * to prevent caller from setting mapping/page error */
781 * async writeback completion handler.
783 * If we get an error, set the mapping error bit, but not the individual
786 static void writepages_finish(struct ceph_osd_request *req)
788 struct inode *inode = req->r_inode;
789 struct ceph_inode_info *ci = ceph_inode(inode);
790 struct ceph_osd_data *osd_data;
792 int num_pages, total_pages = 0;
794 int rc = req->r_result;
795 struct ceph_snap_context *snapc = req->r_snapc;
796 struct address_space *mapping = inode->i_mapping;
797 struct ceph_fs_client *fsc = ceph_inode_to_client(inode);
800 dout("writepages_finish %p rc %d\n", inode, rc);
802 mapping_set_error(mapping, rc);
803 ceph_set_error_write(ci);
804 if (rc == -EBLACKLISTED)
805 fsc->blacklisted = true;
807 ceph_clear_error_write(ci);
810 ceph_update_write_latency(&fsc->mdsc->metric, req->r_start_latency,
811 req->r_end_latency, rc);
814 * We lost the cache cap, need to truncate the page before
815 * it is unlocked, otherwise we'd truncate it later in the
816 * page truncation thread, possibly losing some data that
819 remove_page = !(ceph_caps_issued(ci) &
820 (CEPH_CAP_FILE_CACHE|CEPH_CAP_FILE_LAZYIO));
822 /* clean all pages */
823 for (i = 0; i < req->r_num_ops; i++) {
824 if (req->r_ops[i].op != CEPH_OSD_OP_WRITE)
827 osd_data = osd_req_op_extent_osd_data(req, i);
828 BUG_ON(osd_data->type != CEPH_OSD_DATA_TYPE_PAGES);
829 num_pages = calc_pages_for((u64)osd_data->alignment,
830 (u64)osd_data->length);
831 total_pages += num_pages;
832 for (j = 0; j < num_pages; j++) {
833 page = osd_data->pages[j];
835 WARN_ON(!PageUptodate(page));
837 if (atomic_long_dec_return(&fsc->writeback_count) <
838 CONGESTION_OFF_THRESH(
839 fsc->mount_options->congestion_kb))
840 clear_bdi_congested(inode_to_bdi(inode),
843 ceph_put_snap_context(page_snap_context(page));
845 ClearPagePrivate(page);
846 dout("unlocking %p\n", page);
847 end_page_writeback(page);
850 generic_error_remove_page(inode->i_mapping,
855 dout("writepages_finish %p wrote %llu bytes cleaned %d pages\n",
856 inode, osd_data->length, rc >= 0 ? num_pages : 0);
858 release_pages(osd_data->pages, num_pages);
861 ceph_put_wrbuffer_cap_refs(ci, total_pages, snapc);
863 osd_data = osd_req_op_extent_osd_data(req, 0);
864 if (osd_data->pages_from_pool)
865 mempool_free(osd_data->pages,
866 ceph_sb_to_client(inode->i_sb)->wb_pagevec_pool);
868 kfree(osd_data->pages);
869 ceph_osdc_put_request(req);
873 * initiate async writeback
875 static int ceph_writepages_start(struct address_space *mapping,
876 struct writeback_control *wbc)
878 struct inode *inode = mapping->host;
879 struct ceph_inode_info *ci = ceph_inode(inode);
880 struct ceph_fs_client *fsc = ceph_inode_to_client(inode);
881 struct ceph_vino vino = ceph_vino(inode);
882 pgoff_t index, start_index, end = -1;
883 struct ceph_snap_context *snapc = NULL, *last_snapc = NULL, *pgsnapc;
886 unsigned int wsize = i_blocksize(inode);
887 struct ceph_osd_request *req = NULL;
888 struct ceph_writeback_ctl ceph_wbc;
889 bool should_loop, range_whole = false;
892 dout("writepages_start %p (mode=%s)\n", inode,
893 wbc->sync_mode == WB_SYNC_NONE ? "NONE" :
894 (wbc->sync_mode == WB_SYNC_ALL ? "ALL" : "HOLD"));
896 if (READ_ONCE(fsc->mount_state) == CEPH_MOUNT_SHUTDOWN) {
897 if (ci->i_wrbuffer_ref > 0) {
899 "writepage_start %p %lld forced umount\n",
900 inode, ceph_ino(inode));
902 mapping_set_error(mapping, -EIO);
903 return -EIO; /* we're in a forced umount, don't write! */
905 if (fsc->mount_options->wsize < wsize)
906 wsize = fsc->mount_options->wsize;
910 start_index = wbc->range_cyclic ? mapping->writeback_index : 0;
914 /* find oldest snap context with dirty data */
915 snapc = get_oldest_context(inode, &ceph_wbc, NULL);
917 /* hmm, why does writepages get called when there
919 dout(" no snap context with dirty data?\n");
922 dout(" oldest snapc is %p seq %lld (%d snaps)\n",
923 snapc, snapc->seq, snapc->num_snaps);
926 if (ceph_wbc.head_snapc && snapc != last_snapc) {
927 /* where to start/end? */
928 if (wbc->range_cyclic) {
933 dout(" cyclic, start at %lu\n", index);
935 index = wbc->range_start >> PAGE_SHIFT;
936 end = wbc->range_end >> PAGE_SHIFT;
937 if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX)
939 dout(" not cyclic, %lu to %lu\n", index, end);
941 } else if (!ceph_wbc.head_snapc) {
942 /* Do not respect wbc->range_{start,end}. Dirty pages
943 * in that range can be associated with newer snapc.
944 * They are not writeable until we write all dirty pages
945 * associated with 'snapc' get written */
948 dout(" non-head snapc, range whole\n");
951 ceph_put_snap_context(last_snapc);
954 while (!done && index <= end) {
955 int num_ops = 0, op_idx;
956 unsigned i, pvec_pages, max_pages, locked_pages = 0;
957 struct page **pages = NULL, **data_pages;
958 mempool_t *pool = NULL; /* Becomes non-null if mempool used */
960 pgoff_t strip_unit_end = 0;
961 u64 offset = 0, len = 0;
963 max_pages = wsize >> PAGE_SHIFT;
966 pvec_pages = pagevec_lookup_range_nr_tag(&pvec, mapping, &index,
967 end, PAGECACHE_TAG_DIRTY,
968 max_pages - locked_pages);
969 dout("pagevec_lookup_range_tag got %d\n", pvec_pages);
970 if (!pvec_pages && !locked_pages)
972 for (i = 0; i < pvec_pages && locked_pages < max_pages; i++) {
973 page = pvec.pages[i];
974 dout("? %p idx %lu\n", page, page->index);
975 if (locked_pages == 0)
976 lock_page(page); /* first page */
977 else if (!trylock_page(page))
980 /* only dirty pages, or our accounting breaks */
981 if (unlikely(!PageDirty(page)) ||
982 unlikely(page->mapping != mapping)) {
983 dout("!dirty or !mapping %p\n", page);
987 /* only if matching snap context */
988 pgsnapc = page_snap_context(page);
989 if (pgsnapc != snapc) {
990 dout("page snapc %p %lld != oldest %p %lld\n",
991 pgsnapc, pgsnapc->seq, snapc, snapc->seq);
993 !ceph_wbc.head_snapc &&
994 wbc->sync_mode != WB_SYNC_NONE)
999 if (page_offset(page) >= ceph_wbc.i_size) {
1000 dout("%p page eof %llu\n",
1001 page, ceph_wbc.i_size);
1002 if ((ceph_wbc.size_stable ||
1003 page_offset(page) >= i_size_read(inode)) &&
1004 clear_page_dirty_for_io(page))
1005 mapping->a_ops->invalidatepage(page,
1010 if (strip_unit_end && (page->index > strip_unit_end)) {
1011 dout("end of strip unit %p\n", page);
1015 if (PageWriteback(page)) {
1016 if (wbc->sync_mode == WB_SYNC_NONE) {
1017 dout("%p under writeback\n", page);
1021 dout("waiting on writeback %p\n", page);
1022 wait_on_page_writeback(page);
1025 if (!clear_page_dirty_for_io(page)) {
1026 dout("%p !clear_page_dirty_for_io\n", page);
1032 * We have something to write. If this is
1033 * the first locked page this time through,
1034 * calculate max possinle write size and
1035 * allocate a page array
1037 if (locked_pages == 0) {
1042 /* prepare async write request */
1043 offset = (u64)page_offset(page);
1044 ceph_calc_file_object_mapping(&ci->i_layout,
1051 strip_unit_end = page->index +
1052 ((len - 1) >> PAGE_SHIFT);
1055 max_pages = calc_pages_for(0, (u64)len);
1056 pages = kmalloc_array(max_pages,
1060 pool = fsc->wb_pagevec_pool;
1061 pages = mempool_alloc(pool, GFP_NOFS);
1066 } else if (page->index !=
1067 (offset + len) >> PAGE_SHIFT) {
1068 if (num_ops >= (pool ? CEPH_OSD_SLAB_OPS :
1069 CEPH_OSD_MAX_OPS)) {
1070 redirty_page_for_writepage(wbc, page);
1076 offset = (u64)page_offset(page);
1080 /* note position of first page in pvec */
1081 dout("%p will write page %p idx %lu\n",
1082 inode, page, page->index);
1084 if (atomic_long_inc_return(&fsc->writeback_count) >
1085 CONGESTION_ON_THRESH(
1086 fsc->mount_options->congestion_kb)) {
1087 set_bdi_congested(inode_to_bdi(inode),
1092 pages[locked_pages++] = page;
1093 pvec.pages[i] = NULL;
1098 /* did we get anything? */
1100 goto release_pvec_pages;
1103 /* shift unused page to beginning of pvec */
1104 for (j = 0; j < pvec_pages; j++) {
1108 pvec.pages[n] = pvec.pages[j];
1113 if (pvec_pages && i == pvec_pages &&
1114 locked_pages < max_pages) {
1115 dout("reached end pvec, trying for more\n");
1116 pagevec_release(&pvec);
1117 goto get_more_pages;
1122 offset = page_offset(pages[0]);
1125 req = ceph_osdc_new_request(&fsc->client->osdc,
1126 &ci->i_layout, vino,
1127 offset, &len, 0, num_ops,
1128 CEPH_OSD_OP_WRITE, CEPH_OSD_FLAG_WRITE,
1129 snapc, ceph_wbc.truncate_seq,
1130 ceph_wbc.truncate_size, false);
1132 req = ceph_osdc_new_request(&fsc->client->osdc,
1133 &ci->i_layout, vino,
1138 CEPH_OSD_FLAG_WRITE,
1139 snapc, ceph_wbc.truncate_seq,
1140 ceph_wbc.truncate_size, true);
1141 BUG_ON(IS_ERR(req));
1143 BUG_ON(len < page_offset(pages[locked_pages - 1]) +
1144 PAGE_SIZE - offset);
1146 req->r_callback = writepages_finish;
1147 req->r_inode = inode;
1149 /* Format the osd request message and submit the write */
1153 for (i = 0; i < locked_pages; i++) {
1154 u64 cur_offset = page_offset(pages[i]);
1155 if (offset + len != cur_offset) {
1156 if (op_idx + 1 == req->r_num_ops)
1158 osd_req_op_extent_dup_last(req, op_idx,
1159 cur_offset - offset);
1160 dout("writepages got pages at %llu~%llu\n",
1162 osd_req_op_extent_osd_data_pages(req, op_idx,
1165 osd_req_op_extent_update(req, op_idx, len);
1168 offset = cur_offset;
1169 data_pages = pages + i;
1173 set_page_writeback(pages[i]);
1177 if (ceph_wbc.size_stable) {
1178 len = min(len, ceph_wbc.i_size - offset);
1179 } else if (i == locked_pages) {
1180 /* writepages_finish() clears writeback pages
1181 * according to the data length, so make sure
1182 * data length covers all locked pages */
1183 u64 min_len = len + 1 - PAGE_SIZE;
1184 len = get_writepages_data_length(inode, pages[i - 1],
1186 len = max(len, min_len);
1188 dout("writepages got pages at %llu~%llu\n", offset, len);
1190 osd_req_op_extent_osd_data_pages(req, op_idx, data_pages, len,
1192 osd_req_op_extent_update(req, op_idx, len);
1194 BUG_ON(op_idx + 1 != req->r_num_ops);
1197 if (i < locked_pages) {
1198 BUG_ON(num_ops <= req->r_num_ops);
1199 num_ops -= req->r_num_ops;
1202 /* allocate new pages array for next request */
1204 pages = kmalloc_array(locked_pages, sizeof(*pages),
1207 pool = fsc->wb_pagevec_pool;
1208 pages = mempool_alloc(pool, GFP_NOFS);
1211 memcpy(pages, data_pages + i,
1212 locked_pages * sizeof(*pages));
1213 memset(data_pages + i, 0,
1214 locked_pages * sizeof(*pages));
1216 BUG_ON(num_ops != req->r_num_ops);
1217 index = pages[i - 1]->index + 1;
1218 /* request message now owns the pages array */
1222 req->r_mtime = inode->i_mtime;
1223 rc = ceph_osdc_start_request(&fsc->client->osdc, req, true);
1227 wbc->nr_to_write -= i;
1232 * We stop writing back only if we are not doing
1233 * integrity sync. In case of integrity sync we have to
1234 * keep going until we have written all the pages
1235 * we tagged for writeback prior to entering this loop.
1237 if (wbc->nr_to_write <= 0 && wbc->sync_mode == WB_SYNC_NONE)
1241 dout("pagevec_release on %d pages (%p)\n", (int)pvec.nr,
1242 pvec.nr ? pvec.pages[0] : NULL);
1243 pagevec_release(&pvec);
1246 if (should_loop && !done) {
1247 /* more to do; loop back to beginning of file */
1248 dout("writepages looping back to beginning of file\n");
1249 end = start_index - 1; /* OK even when start_index == 0 */
1251 /* to write dirty pages associated with next snapc,
1252 * we need to wait until current writes complete */
1253 if (wbc->sync_mode != WB_SYNC_NONE &&
1254 start_index == 0 && /* all dirty pages were checked */
1255 !ceph_wbc.head_snapc) {
1259 while ((index <= end) &&
1260 (nr = pagevec_lookup_tag(&pvec, mapping, &index,
1261 PAGECACHE_TAG_WRITEBACK))) {
1262 for (i = 0; i < nr; i++) {
1263 page = pvec.pages[i];
1264 if (page_snap_context(page) != snapc)
1266 wait_on_page_writeback(page);
1268 pagevec_release(&pvec);
1278 if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0))
1279 mapping->writeback_index = index;
1282 ceph_osdc_put_request(req);
1283 ceph_put_snap_context(last_snapc);
1284 dout("writepages dend - startone, rc = %d\n", rc);
1291 * See if a given @snapc is either writeable, or already written.
1293 static int context_is_writeable_or_written(struct inode *inode,
1294 struct ceph_snap_context *snapc)
1296 struct ceph_snap_context *oldest = get_oldest_context(inode, NULL, NULL);
1297 int ret = !oldest || snapc->seq <= oldest->seq;
1299 ceph_put_snap_context(oldest);
1304 * We are only allowed to write into/dirty the page if the page is
1305 * clean, or already dirty within the same snap context.
1307 * called with page locked.
1308 * return success with page locked,
1309 * or any failure (incl -EAGAIN) with page unlocked.
1311 static int ceph_update_writeable_page(struct file *file,
1312 loff_t pos, unsigned len,
1315 struct inode *inode = file_inode(file);
1316 struct ceph_fs_client *fsc = ceph_inode_to_client(inode);
1317 struct ceph_inode_info *ci = ceph_inode(inode);
1318 loff_t page_off = pos & PAGE_MASK;
1319 int pos_in_page = pos & ~PAGE_MASK;
1320 int end_in_page = pos_in_page + len;
1323 struct ceph_snap_context *snapc, *oldest;
1325 if (READ_ONCE(fsc->mount_state) == CEPH_MOUNT_SHUTDOWN) {
1326 dout(" page %p forced umount\n", page);
1332 /* writepages currently holds page lock, but if we change that later, */
1333 wait_on_page_writeback(page);
1335 snapc = page_snap_context(page);
1336 if (snapc && snapc != ci->i_head_snapc) {
1338 * this page is already dirty in another (older) snap
1339 * context! is it writeable now?
1341 oldest = get_oldest_context(inode, NULL, NULL);
1342 if (snapc->seq > oldest->seq) {
1343 ceph_put_snap_context(oldest);
1344 dout(" page %p snapc %p not current or oldest\n",
1347 * queue for writeback, and wait for snapc to
1348 * be writeable or written
1350 snapc = ceph_get_snap_context(snapc);
1352 ceph_queue_writeback(inode);
1353 r = wait_event_killable(ci->i_cap_wq,
1354 context_is_writeable_or_written(inode, snapc));
1355 ceph_put_snap_context(snapc);
1356 if (r == -ERESTARTSYS)
1360 ceph_put_snap_context(oldest);
1362 /* yay, writeable, do it now (without dropping page lock) */
1363 dout(" page %p snapc %p not current, but oldest\n",
1365 if (!clear_page_dirty_for_io(page))
1367 r = writepage_nounlock(page, NULL);
1373 if (PageUptodate(page)) {
1374 dout(" page %p already uptodate\n", page);
1379 if (pos_in_page == 0 && len == PAGE_SIZE)
1382 /* past end of file? */
1383 i_size = i_size_read(inode);
1385 if (page_off >= i_size ||
1386 (pos_in_page == 0 && (pos+len) >= i_size &&
1387 end_in_page - pos_in_page != PAGE_SIZE)) {
1388 dout(" zeroing %p 0 - %d and %d - %d\n",
1389 page, pos_in_page, end_in_page, (int)PAGE_SIZE);
1390 zero_user_segments(page,
1392 end_in_page, PAGE_SIZE);
1396 /* we need to read it. */
1397 r = ceph_do_readpage(file, page);
1399 if (r == -EINPROGRESS)
1410 * We are only allowed to write into/dirty the page if the page is
1411 * clean, or already dirty within the same snap context.
1413 static int ceph_write_begin(struct file *file, struct address_space *mapping,
1414 loff_t pos, unsigned len, unsigned flags,
1415 struct page **pagep, void **fsdata)
1417 struct inode *inode = file_inode(file);
1419 pgoff_t index = pos >> PAGE_SHIFT;
1424 page = grab_cache_page_write_begin(mapping, index, 0);
1428 dout("write_begin file %p inode %p page %p %d~%d\n", file,
1429 inode, page, (int)pos, (int)len);
1431 r = ceph_update_writeable_page(file, pos, len, page);
1436 } while (r == -EAGAIN);
1442 * we don't do anything in here that simple_write_end doesn't do
1443 * except adjust dirty page accounting
1445 static int ceph_write_end(struct file *file, struct address_space *mapping,
1446 loff_t pos, unsigned len, unsigned copied,
1447 struct page *page, void *fsdata)
1449 struct inode *inode = file_inode(file);
1450 bool check_cap = false;
1452 dout("write_end file %p inode %p page %p %d~%d (%d)\n", file,
1453 inode, page, (int)pos, (int)copied, (int)len);
1455 /* zero the stale part of the page if we did a short copy */
1456 if (!PageUptodate(page)) {
1461 SetPageUptodate(page);
1464 /* did file size increase? */
1465 if (pos+copied > i_size_read(inode))
1466 check_cap = ceph_inode_set_size(inode, pos+copied);
1468 set_page_dirty(page);
1475 ceph_check_caps(ceph_inode(inode), CHECK_CAPS_AUTHONLY, NULL);
1481 * we set .direct_IO to indicate direct io is supported, but since we
1482 * intercept O_DIRECT reads and writes early, this function should
1485 static ssize_t ceph_direct_io(struct kiocb *iocb, struct iov_iter *iter)
1491 const struct address_space_operations ceph_aops = {
1492 .readpage = ceph_readpage,
1493 .readpages = ceph_readpages,
1494 .writepage = ceph_writepage,
1495 .writepages = ceph_writepages_start,
1496 .write_begin = ceph_write_begin,
1497 .write_end = ceph_write_end,
1498 .set_page_dirty = ceph_set_page_dirty,
1499 .invalidatepage = ceph_invalidatepage,
1500 .releasepage = ceph_releasepage,
1501 .direct_IO = ceph_direct_io,
1504 static void ceph_block_sigs(sigset_t *oldset)
1507 siginitsetinv(&mask, sigmask(SIGKILL));
1508 sigprocmask(SIG_BLOCK, &mask, oldset);
1511 static void ceph_restore_sigs(sigset_t *oldset)
1513 sigprocmask(SIG_SETMASK, oldset, NULL);
1519 static vm_fault_t ceph_filemap_fault(struct vm_fault *vmf)
1521 struct vm_area_struct *vma = vmf->vma;
1522 struct inode *inode = file_inode(vma->vm_file);
1523 struct ceph_inode_info *ci = ceph_inode(inode);
1524 struct ceph_file_info *fi = vma->vm_file->private_data;
1525 struct page *pinned_page = NULL;
1526 loff_t off = vmf->pgoff << PAGE_SHIFT;
1529 vm_fault_t ret = VM_FAULT_SIGBUS;
1531 ceph_block_sigs(&oldset);
1533 dout("filemap_fault %p %llx.%llx %llu~%zd trying to get caps\n",
1534 inode, ceph_vinop(inode), off, (size_t)PAGE_SIZE);
1535 if (fi->fmode & CEPH_FILE_MODE_LAZY)
1536 want = CEPH_CAP_FILE_CACHE | CEPH_CAP_FILE_LAZYIO;
1538 want = CEPH_CAP_FILE_CACHE;
1541 err = ceph_get_caps(vma->vm_file, CEPH_CAP_FILE_RD, want, -1,
1542 &got, &pinned_page);
1546 dout("filemap_fault %p %llu~%zd got cap refs on %s\n",
1547 inode, off, (size_t)PAGE_SIZE, ceph_cap_string(got));
1549 if ((got & (CEPH_CAP_FILE_CACHE | CEPH_CAP_FILE_LAZYIO)) ||
1550 ci->i_inline_version == CEPH_INLINE_NONE) {
1551 CEPH_DEFINE_RW_CONTEXT(rw_ctx, got);
1552 ceph_add_rw_context(fi, &rw_ctx);
1553 ret = filemap_fault(vmf);
1554 ceph_del_rw_context(fi, &rw_ctx);
1555 dout("filemap_fault %p %llu~%zd drop cap refs %s ret %x\n",
1556 inode, off, (size_t)PAGE_SIZE,
1557 ceph_cap_string(got), ret);
1562 put_page(pinned_page);
1563 ceph_put_cap_refs(ci, got);
1568 /* read inline data */
1569 if (off >= PAGE_SIZE) {
1570 /* does not support inline data > PAGE_SIZE */
1571 ret = VM_FAULT_SIGBUS;
1573 struct address_space *mapping = inode->i_mapping;
1574 struct page *page = find_or_create_page(mapping, 0,
1575 mapping_gfp_constraint(mapping,
1581 err = __ceph_do_getattr(inode, page,
1582 CEPH_STAT_CAP_INLINE_DATA, true);
1583 if (err < 0 || off >= i_size_read(inode)) {
1586 ret = vmf_error(err);
1589 if (err < PAGE_SIZE)
1590 zero_user_segment(page, err, PAGE_SIZE);
1592 flush_dcache_page(page);
1593 SetPageUptodate(page);
1595 ret = VM_FAULT_MAJOR | VM_FAULT_LOCKED;
1597 dout("filemap_fault %p %llu~%zd read inline data ret %x\n",
1598 inode, off, (size_t)PAGE_SIZE, ret);
1601 ceph_restore_sigs(&oldset);
1603 ret = vmf_error(err);
1609 * Reuse write_begin here for simplicity.
1611 static vm_fault_t ceph_page_mkwrite(struct vm_fault *vmf)
1613 struct vm_area_struct *vma = vmf->vma;
1614 struct inode *inode = file_inode(vma->vm_file);
1615 struct ceph_inode_info *ci = ceph_inode(inode);
1616 struct ceph_file_info *fi = vma->vm_file->private_data;
1617 struct ceph_cap_flush *prealloc_cf;
1618 struct page *page = vmf->page;
1619 loff_t off = page_offset(page);
1620 loff_t size = i_size_read(inode);
1624 vm_fault_t ret = VM_FAULT_SIGBUS;
1626 prealloc_cf = ceph_alloc_cap_flush();
1628 return VM_FAULT_OOM;
1630 sb_start_pagefault(inode->i_sb);
1631 ceph_block_sigs(&oldset);
1633 if (ci->i_inline_version != CEPH_INLINE_NONE) {
1634 struct page *locked_page = NULL;
1639 err = ceph_uninline_data(vma->vm_file, locked_page);
1641 unlock_page(locked_page);
1646 if (off + PAGE_SIZE <= size)
1649 len = size & ~PAGE_MASK;
1651 dout("page_mkwrite %p %llx.%llx %llu~%zd getting caps i_size %llu\n",
1652 inode, ceph_vinop(inode), off, len, size);
1653 if (fi->fmode & CEPH_FILE_MODE_LAZY)
1654 want = CEPH_CAP_FILE_BUFFER | CEPH_CAP_FILE_LAZYIO;
1656 want = CEPH_CAP_FILE_BUFFER;
1659 err = ceph_get_caps(vma->vm_file, CEPH_CAP_FILE_WR, want, off + len,
1664 dout("page_mkwrite %p %llu~%zd got cap refs on %s\n",
1665 inode, off, len, ceph_cap_string(got));
1667 /* Update time before taking page lock */
1668 file_update_time(vma->vm_file);
1669 inode_inc_iversion_raw(inode);
1674 if (page_mkwrite_check_truncate(page, inode) < 0) {
1676 ret = VM_FAULT_NOPAGE;
1680 err = ceph_update_writeable_page(vma->vm_file, off, len, page);
1682 /* success. we'll keep the page locked. */
1683 set_page_dirty(page);
1684 ret = VM_FAULT_LOCKED;
1686 } while (err == -EAGAIN);
1688 if (ret == VM_FAULT_LOCKED ||
1689 ci->i_inline_version != CEPH_INLINE_NONE) {
1691 spin_lock(&ci->i_ceph_lock);
1692 ci->i_inline_version = CEPH_INLINE_NONE;
1693 dirty = __ceph_mark_dirty_caps(ci, CEPH_CAP_FILE_WR,
1695 spin_unlock(&ci->i_ceph_lock);
1697 __mark_inode_dirty(inode, dirty);
1700 dout("page_mkwrite %p %llu~%zd dropping cap refs on %s ret %x\n",
1701 inode, off, len, ceph_cap_string(got), ret);
1702 ceph_put_cap_refs(ci, got);
1704 ceph_restore_sigs(&oldset);
1705 sb_end_pagefault(inode->i_sb);
1706 ceph_free_cap_flush(prealloc_cf);
1708 ret = vmf_error(err);
1712 void ceph_fill_inline_data(struct inode *inode, struct page *locked_page,
1713 char *data, size_t len)
1715 struct address_space *mapping = inode->i_mapping;
1721 if (i_size_read(inode) == 0)
1723 page = find_or_create_page(mapping, 0,
1724 mapping_gfp_constraint(mapping,
1728 if (PageUptodate(page)) {
1735 dout("fill_inline_data %p %llx.%llx len %zu locked_page %p\n",
1736 inode, ceph_vinop(inode), len, locked_page);
1739 void *kaddr = kmap_atomic(page);
1740 memcpy(kaddr, data, len);
1741 kunmap_atomic(kaddr);
1744 if (page != locked_page) {
1745 if (len < PAGE_SIZE)
1746 zero_user_segment(page, len, PAGE_SIZE);
1748 flush_dcache_page(page);
1750 SetPageUptodate(page);
1756 int ceph_uninline_data(struct file *filp, struct page *locked_page)
1758 struct inode *inode = file_inode(filp);
1759 struct ceph_inode_info *ci = ceph_inode(inode);
1760 struct ceph_fs_client *fsc = ceph_inode_to_client(inode);
1761 struct ceph_osd_request *req;
1762 struct page *page = NULL;
1763 u64 len, inline_version;
1765 bool from_pagecache = false;
1767 spin_lock(&ci->i_ceph_lock);
1768 inline_version = ci->i_inline_version;
1769 spin_unlock(&ci->i_ceph_lock);
1771 dout("uninline_data %p %llx.%llx inline_version %llu\n",
1772 inode, ceph_vinop(inode), inline_version);
1774 if (inline_version == 1 || /* initial version, no data */
1775 inline_version == CEPH_INLINE_NONE)
1780 WARN_ON(!PageUptodate(page));
1781 } else if (ceph_caps_issued(ci) &
1782 (CEPH_CAP_FILE_CACHE|CEPH_CAP_FILE_LAZYIO)) {
1783 page = find_get_page(inode->i_mapping, 0);
1785 if (PageUptodate(page)) {
1786 from_pagecache = true;
1796 len = i_size_read(inode);
1797 if (len > PAGE_SIZE)
1800 page = __page_cache_alloc(GFP_NOFS);
1805 err = __ceph_do_getattr(inode, page,
1806 CEPH_STAT_CAP_INLINE_DATA, true);
1808 /* no inline data */
1809 if (err == -ENODATA)
1816 req = ceph_osdc_new_request(&fsc->client->osdc, &ci->i_layout,
1817 ceph_vino(inode), 0, &len, 0, 1,
1818 CEPH_OSD_OP_CREATE, CEPH_OSD_FLAG_WRITE,
1825 req->r_mtime = inode->i_mtime;
1826 err = ceph_osdc_start_request(&fsc->client->osdc, req, false);
1828 err = ceph_osdc_wait_request(&fsc->client->osdc, req);
1829 ceph_osdc_put_request(req);
1833 req = ceph_osdc_new_request(&fsc->client->osdc, &ci->i_layout,
1834 ceph_vino(inode), 0, &len, 1, 3,
1835 CEPH_OSD_OP_WRITE, CEPH_OSD_FLAG_WRITE,
1836 NULL, ci->i_truncate_seq,
1837 ci->i_truncate_size, false);
1843 osd_req_op_extent_osd_data_pages(req, 1, &page, len, 0, false, false);
1846 __le64 xattr_buf = cpu_to_le64(inline_version);
1847 err = osd_req_op_xattr_init(req, 0, CEPH_OSD_OP_CMPXATTR,
1848 "inline_version", &xattr_buf,
1850 CEPH_OSD_CMPXATTR_OP_GT,
1851 CEPH_OSD_CMPXATTR_MODE_U64);
1858 int xattr_len = snprintf(xattr_buf, sizeof(xattr_buf),
1859 "%llu", inline_version);
1860 err = osd_req_op_xattr_init(req, 2, CEPH_OSD_OP_SETXATTR,
1862 xattr_buf, xattr_len, 0, 0);
1867 req->r_mtime = inode->i_mtime;
1868 err = ceph_osdc_start_request(&fsc->client->osdc, req, false);
1870 err = ceph_osdc_wait_request(&fsc->client->osdc, req);
1872 ceph_update_write_latency(&fsc->mdsc->metric, req->r_start_latency,
1873 req->r_end_latency, err);
1876 ceph_osdc_put_request(req);
1877 if (err == -ECANCELED)
1880 if (page && page != locked_page) {
1881 if (from_pagecache) {
1885 __free_pages(page, 0);
1888 dout("uninline_data %p %llx.%llx inline_version %llu = %d\n",
1889 inode, ceph_vinop(inode), inline_version, err);
1893 static const struct vm_operations_struct ceph_vmops = {
1894 .fault = ceph_filemap_fault,
1895 .page_mkwrite = ceph_page_mkwrite,
1898 int ceph_mmap(struct file *file, struct vm_area_struct *vma)
1900 struct address_space *mapping = file->f_mapping;
1902 if (!mapping->a_ops->readpage)
1904 file_accessed(file);
1905 vma->vm_ops = &ceph_vmops;
1914 static int __ceph_pool_perm_get(struct ceph_inode_info *ci,
1915 s64 pool, struct ceph_string *pool_ns)
1917 struct ceph_fs_client *fsc = ceph_inode_to_client(&ci->vfs_inode);
1918 struct ceph_mds_client *mdsc = fsc->mdsc;
1919 struct ceph_osd_request *rd_req = NULL, *wr_req = NULL;
1920 struct rb_node **p, *parent;
1921 struct ceph_pool_perm *perm;
1922 struct page **pages;
1924 int err = 0, err2 = 0, have = 0;
1926 down_read(&mdsc->pool_perm_rwsem);
1927 p = &mdsc->pool_perm_tree.rb_node;
1929 perm = rb_entry(*p, struct ceph_pool_perm, node);
1930 if (pool < perm->pool)
1932 else if (pool > perm->pool)
1933 p = &(*p)->rb_right;
1935 int ret = ceph_compare_string(pool_ns,
1941 p = &(*p)->rb_right;
1948 up_read(&mdsc->pool_perm_rwsem);
1953 dout("__ceph_pool_perm_get pool %lld ns %.*s no perm cached\n",
1954 pool, (int)pool_ns->len, pool_ns->str);
1956 dout("__ceph_pool_perm_get pool %lld no perm cached\n", pool);
1958 down_write(&mdsc->pool_perm_rwsem);
1959 p = &mdsc->pool_perm_tree.rb_node;
1963 perm = rb_entry(parent, struct ceph_pool_perm, node);
1964 if (pool < perm->pool)
1966 else if (pool > perm->pool)
1967 p = &(*p)->rb_right;
1969 int ret = ceph_compare_string(pool_ns,
1975 p = &(*p)->rb_right;
1983 up_write(&mdsc->pool_perm_rwsem);
1987 rd_req = ceph_osdc_alloc_request(&fsc->client->osdc, NULL,
1988 1, false, GFP_NOFS);
1994 rd_req->r_flags = CEPH_OSD_FLAG_READ;
1995 osd_req_op_init(rd_req, 0, CEPH_OSD_OP_STAT, 0);
1996 rd_req->r_base_oloc.pool = pool;
1998 rd_req->r_base_oloc.pool_ns = ceph_get_string(pool_ns);
1999 ceph_oid_printf(&rd_req->r_base_oid, "%llx.00000000", ci->i_vino.ino);
2001 err = ceph_osdc_alloc_messages(rd_req, GFP_NOFS);
2005 wr_req = ceph_osdc_alloc_request(&fsc->client->osdc, NULL,
2006 1, false, GFP_NOFS);
2012 wr_req->r_flags = CEPH_OSD_FLAG_WRITE;
2013 osd_req_op_init(wr_req, 0, CEPH_OSD_OP_CREATE, CEPH_OSD_OP_FLAG_EXCL);
2014 ceph_oloc_copy(&wr_req->r_base_oloc, &rd_req->r_base_oloc);
2015 ceph_oid_copy(&wr_req->r_base_oid, &rd_req->r_base_oid);
2017 err = ceph_osdc_alloc_messages(wr_req, GFP_NOFS);
2021 /* one page should be large enough for STAT data */
2022 pages = ceph_alloc_page_vector(1, GFP_KERNEL);
2023 if (IS_ERR(pages)) {
2024 err = PTR_ERR(pages);
2028 osd_req_op_raw_data_in_pages(rd_req, 0, pages, PAGE_SIZE,
2030 err = ceph_osdc_start_request(&fsc->client->osdc, rd_req, false);
2032 wr_req->r_mtime = ci->vfs_inode.i_mtime;
2033 err2 = ceph_osdc_start_request(&fsc->client->osdc, wr_req, false);
2036 err = ceph_osdc_wait_request(&fsc->client->osdc, rd_req);
2038 err2 = ceph_osdc_wait_request(&fsc->client->osdc, wr_req);
2040 if (err >= 0 || err == -ENOENT)
2042 else if (err != -EPERM) {
2043 if (err == -EBLACKLISTED)
2044 fsc->blacklisted = true;
2048 if (err2 == 0 || err2 == -EEXIST)
2050 else if (err2 != -EPERM) {
2051 if (err2 == -EBLACKLISTED)
2052 fsc->blacklisted = true;
2057 pool_ns_len = pool_ns ? pool_ns->len : 0;
2058 perm = kmalloc(sizeof(*perm) + pool_ns_len + 1, GFP_NOFS);
2066 perm->pool_ns_len = pool_ns_len;
2067 if (pool_ns_len > 0)
2068 memcpy(perm->pool_ns, pool_ns->str, pool_ns_len);
2069 perm->pool_ns[pool_ns_len] = 0;
2071 rb_link_node(&perm->node, parent, p);
2072 rb_insert_color(&perm->node, &mdsc->pool_perm_tree);
2075 up_write(&mdsc->pool_perm_rwsem);
2077 ceph_osdc_put_request(rd_req);
2078 ceph_osdc_put_request(wr_req);
2083 dout("__ceph_pool_perm_get pool %lld ns %.*s result = %d\n",
2084 pool, (int)pool_ns->len, pool_ns->str, err);
2086 dout("__ceph_pool_perm_get pool %lld result = %d\n", pool, err);
2090 int ceph_pool_perm_check(struct inode *inode, int need)
2092 struct ceph_inode_info *ci = ceph_inode(inode);
2093 struct ceph_string *pool_ns;
2097 if (ci->i_vino.snap != CEPH_NOSNAP) {
2099 * Pool permission check needs to write to the first object.
2100 * But for snapshot, head of the first object may have alread
2101 * been deleted. Skip check to avoid creating orphan object.
2106 if (ceph_test_mount_opt(ceph_inode_to_client(inode),
2110 spin_lock(&ci->i_ceph_lock);
2111 flags = ci->i_ceph_flags;
2112 pool = ci->i_layout.pool_id;
2113 spin_unlock(&ci->i_ceph_lock);
2115 if (flags & CEPH_I_POOL_PERM) {
2116 if ((need & CEPH_CAP_FILE_RD) && !(flags & CEPH_I_POOL_RD)) {
2117 dout("ceph_pool_perm_check pool %lld no read perm\n",
2121 if ((need & CEPH_CAP_FILE_WR) && !(flags & CEPH_I_POOL_WR)) {
2122 dout("ceph_pool_perm_check pool %lld no write perm\n",
2129 pool_ns = ceph_try_get_string(ci->i_layout.pool_ns);
2130 ret = __ceph_pool_perm_get(ci, pool, pool_ns);
2131 ceph_put_string(pool_ns);
2135 flags = CEPH_I_POOL_PERM;
2136 if (ret & POOL_READ)
2137 flags |= CEPH_I_POOL_RD;
2138 if (ret & POOL_WRITE)
2139 flags |= CEPH_I_POOL_WR;
2141 spin_lock(&ci->i_ceph_lock);
2142 if (pool == ci->i_layout.pool_id &&
2143 pool_ns == rcu_dereference_raw(ci->i_layout.pool_ns)) {
2144 ci->i_ceph_flags |= flags;
2146 pool = ci->i_layout.pool_id;
2147 flags = ci->i_ceph_flags;
2149 spin_unlock(&ci->i_ceph_lock);
2153 void ceph_pool_perm_destroy(struct ceph_mds_client *mdsc)
2155 struct ceph_pool_perm *perm;
2158 while (!RB_EMPTY_ROOT(&mdsc->pool_perm_tree)) {
2159 n = rb_first(&mdsc->pool_perm_tree);
2160 perm = rb_entry(n, struct ceph_pool_perm, node);
2161 rb_erase(n, &mdsc->pool_perm_tree);