1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/ceph/ceph_debug.h>
4 #include <linux/backing-dev.h>
7 #include <linux/swap.h>
8 #include <linux/pagemap.h>
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>
15 #include <linux/netfs.h>
18 #include "mds_client.h"
21 #include <linux/ceph/osd_client.h>
22 #include <linux/ceph/striper.h>
25 * Ceph address space ops.
27 * There are a few funny things going on here.
29 * The page->private field is used to reference a struct
30 * ceph_snap_context for _every_ dirty page. This indicates which
31 * snapshot the page was logically dirtied in, and thus which snap
32 * context needs to be associated with the osd write during writeback.
34 * Similarly, struct ceph_inode_info maintains a set of counters to
35 * count dirty pages on the inode. In the absence of snapshots,
36 * i_wrbuffer_ref == i_wrbuffer_ref_head == the dirty page count.
38 * When a snapshot is taken (that is, when the client receives
39 * notification that a snapshot was taken), each inode with caps and
40 * with dirty pages (dirty pages implies there is a cap) gets a new
41 * ceph_cap_snap in the i_cap_snaps list (which is sorted in ascending
42 * order, new snaps go to the tail). The i_wrbuffer_ref_head count is
43 * moved to capsnap->dirty. (Unless a sync write is currently in
44 * progress. In that case, the capsnap is said to be "pending", new
45 * writes cannot start, and the capsnap isn't "finalized" until the
46 * write completes (or fails) and a final size/mtime for the inode for
47 * that snap can be settled upon.) i_wrbuffer_ref_head is reset to 0.
49 * On writeback, we must submit writes to the osd IN SNAP ORDER. So,
50 * we look for the first capsnap in i_cap_snaps and write out pages in
51 * that snap context _only_. Then we move on to the next capsnap,
52 * eventually reaching the "live" or "head" context (i.e., pages that
53 * are not yet snapped) and are writing the most recently dirtied
56 * Invalidate and so forth must take care to ensure the dirty page
57 * accounting is preserved.
60 #define CONGESTION_ON_THRESH(congestion_kb) (congestion_kb >> (PAGE_SHIFT-10))
61 #define CONGESTION_OFF_THRESH(congestion_kb) \
62 (CONGESTION_ON_THRESH(congestion_kb) - \
63 (CONGESTION_ON_THRESH(congestion_kb) >> 2))
65 static int ceph_netfs_check_write_begin(struct file *file, loff_t pos, unsigned int len,
66 struct folio *folio, void **_fsdata);
68 static inline struct ceph_snap_context *page_snap_context(struct page *page)
70 if (PagePrivate(page))
71 return (void *)page->private;
76 * Dirty a page. Optimistically adjust accounting, on the assumption
77 * that we won't race with invalidate. If we do, readjust.
79 static bool ceph_dirty_folio(struct address_space *mapping, struct folio *folio)
82 struct ceph_inode_info *ci;
83 struct ceph_snap_context *snapc;
85 if (folio_test_dirty(folio)) {
86 dout("%p dirty_folio %p idx %lu -- already dirty\n",
87 mapping->host, folio, folio->index);
88 VM_BUG_ON_FOLIO(!folio_test_private(folio), folio);
92 inode = mapping->host;
93 ci = ceph_inode(inode);
96 spin_lock(&ci->i_ceph_lock);
97 BUG_ON(ci->i_wr_ref == 0); // caller should hold Fw reference
98 if (__ceph_have_pending_cap_snap(ci)) {
99 struct ceph_cap_snap *capsnap =
100 list_last_entry(&ci->i_cap_snaps,
101 struct ceph_cap_snap,
103 snapc = ceph_get_snap_context(capsnap->context);
104 capsnap->dirty_pages++;
106 BUG_ON(!ci->i_head_snapc);
107 snapc = ceph_get_snap_context(ci->i_head_snapc);
108 ++ci->i_wrbuffer_ref_head;
110 if (ci->i_wrbuffer_ref == 0)
112 ++ci->i_wrbuffer_ref;
113 dout("%p dirty_folio %p idx %lu head %d/%d -> %d/%d "
114 "snapc %p seq %lld (%d snaps)\n",
115 mapping->host, folio, folio->index,
116 ci->i_wrbuffer_ref-1, ci->i_wrbuffer_ref_head-1,
117 ci->i_wrbuffer_ref, ci->i_wrbuffer_ref_head,
118 snapc, snapc->seq, snapc->num_snaps);
119 spin_unlock(&ci->i_ceph_lock);
122 * Reference snap context in folio->private. Also set
123 * PagePrivate so that we get invalidate_folio callback.
125 VM_BUG_ON_FOLIO(folio_test_private(folio), folio);
126 folio_attach_private(folio, snapc);
128 return ceph_fscache_dirty_folio(mapping, folio);
132 * If we are truncating the full folio (i.e. offset == 0), adjust the
133 * dirty folio counters appropriately. Only called if there is private
136 static void ceph_invalidate_folio(struct folio *folio, size_t offset,
140 struct ceph_inode_info *ci;
141 struct ceph_snap_context *snapc;
143 inode = folio->mapping->host;
144 ci = ceph_inode(inode);
146 if (offset != 0 || length != folio_size(folio)) {
147 dout("%p invalidate_folio idx %lu partial dirty page %zu~%zu\n",
148 inode, folio->index, offset, length);
152 WARN_ON(!folio_test_locked(folio));
153 if (folio_test_private(folio)) {
154 dout("%p invalidate_folio idx %lu full dirty page\n",
155 inode, folio->index);
157 snapc = folio_detach_private(folio);
158 ceph_put_wrbuffer_cap_refs(ci, 1, snapc);
159 ceph_put_snap_context(snapc);
162 folio_wait_fscache(folio);
165 static int ceph_releasepage(struct page *page, gfp_t gfp)
167 struct inode *inode = page->mapping->host;
169 dout("%llx:%llx releasepage %p idx %lu (%sdirty)\n",
170 ceph_vinop(inode), page,
171 page->index, PageDirty(page) ? "" : "not ");
173 if (PagePrivate(page))
176 if (PageFsCache(page)) {
177 if (current_is_kswapd() || !(gfp & __GFP_FS))
179 wait_on_page_fscache(page);
181 ceph_fscache_note_page_release(inode);
185 static void ceph_netfs_expand_readahead(struct netfs_io_request *rreq)
187 struct inode *inode = rreq->inode;
188 struct ceph_inode_info *ci = ceph_inode(inode);
189 struct ceph_file_layout *lo = &ci->i_layout;
193 /* Expand the start downward */
194 blockno = div_u64_rem(rreq->start, lo->stripe_unit, &blockoff);
195 rreq->start = blockno * lo->stripe_unit;
196 rreq->len += blockoff;
198 /* Now, round up the length to the next block */
199 rreq->len = roundup(rreq->len, lo->stripe_unit);
202 static bool ceph_netfs_clamp_length(struct netfs_io_subrequest *subreq)
204 struct inode *inode = subreq->rreq->inode;
205 struct ceph_fs_client *fsc = ceph_inode_to_client(inode);
206 struct ceph_inode_info *ci = ceph_inode(inode);
210 /* Truncate the extent at the end of the current block */
211 ceph_calc_file_object_mapping(&ci->i_layout, subreq->start, subreq->len,
212 &objno, &objoff, &xlen);
213 subreq->len = min(xlen, fsc->mount_options->rsize);
217 static void finish_netfs_read(struct ceph_osd_request *req)
219 struct ceph_fs_client *fsc = ceph_inode_to_client(req->r_inode);
220 struct ceph_osd_data *osd_data = osd_req_op_extent_osd_data(req, 0);
221 struct netfs_io_subrequest *subreq = req->r_priv;
223 int err = req->r_result;
225 ceph_update_read_metrics(&fsc->mdsc->metric, req->r_start_latency,
226 req->r_end_latency, osd_data->length, err);
228 dout("%s: result %d subreq->len=%zu i_size=%lld\n", __func__, req->r_result,
229 subreq->len, i_size_read(req->r_inode));
231 /* no object means success but no data */
234 else if (err == -EBLOCKLISTED)
235 fsc->blocklisted = true;
237 if (err >= 0 && err < subreq->len)
238 __set_bit(NETFS_SREQ_CLEAR_TAIL, &subreq->flags);
240 netfs_subreq_terminated(subreq, err, true);
242 num_pages = calc_pages_for(osd_data->alignment, osd_data->length);
243 ceph_put_page_vector(osd_data->pages, num_pages, false);
247 static bool ceph_netfs_issue_op_inline(struct netfs_io_subrequest *subreq)
249 struct netfs_io_request *rreq = subreq->rreq;
250 struct inode *inode = rreq->inode;
251 struct ceph_mds_reply_info_parsed *rinfo;
252 struct ceph_mds_reply_info_in *iinfo;
253 struct ceph_mds_request *req;
254 struct ceph_mds_client *mdsc = ceph_sb_to_mdsc(inode->i_sb);
255 struct ceph_inode_info *ci = ceph_inode(inode);
256 struct iov_iter iter;
261 __set_bit(NETFS_SREQ_CLEAR_TAIL, &subreq->flags);
262 __clear_bit(NETFS_SREQ_COPY_TO_CACHE, &subreq->flags);
264 if (subreq->start >= inode->i_size)
267 /* We need to fetch the inline data. */
268 mode = ceph_try_to_choose_auth_mds(inode, CEPH_STAT_CAP_INLINE_DATA);
269 req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_GETATTR, mode);
274 req->r_ino1 = ci->i_vino;
275 req->r_args.getattr.mask = cpu_to_le32(CEPH_STAT_CAP_INLINE_DATA);
278 err = ceph_mdsc_do_request(mdsc, NULL, req);
282 rinfo = &req->r_reply_info;
283 iinfo = &rinfo->targeti;
284 if (iinfo->inline_version == CEPH_INLINE_NONE) {
285 /* The data got uninlined */
286 ceph_mdsc_put_request(req);
290 len = min_t(size_t, iinfo->inline_len - subreq->start, subreq->len);
291 iov_iter_xarray(&iter, READ, &rreq->mapping->i_pages, subreq->start, len);
292 err = copy_to_iter(iinfo->inline_data + subreq->start, len, &iter);
296 ceph_mdsc_put_request(req);
298 netfs_subreq_terminated(subreq, err, false);
302 static void ceph_netfs_issue_read(struct netfs_io_subrequest *subreq)
304 struct netfs_io_request *rreq = subreq->rreq;
305 struct inode *inode = rreq->inode;
306 struct ceph_inode_info *ci = ceph_inode(inode);
307 struct ceph_fs_client *fsc = ceph_inode_to_client(inode);
308 struct ceph_osd_request *req;
309 struct ceph_vino vino = ceph_vino(inode);
310 struct iov_iter iter;
314 u64 len = subreq->len;
316 if (ci->i_inline_version != CEPH_INLINE_NONE &&
317 ceph_netfs_issue_op_inline(subreq))
320 req = ceph_osdc_new_request(&fsc->client->osdc, &ci->i_layout, vino, subreq->start, &len,
321 0, 1, CEPH_OSD_OP_READ,
322 CEPH_OSD_FLAG_READ | fsc->client->osdc.client->options->read_from_replica,
323 NULL, ci->i_truncate_seq, ci->i_truncate_size, false);
330 dout("%s: pos=%llu orig_len=%zu len=%llu\n", __func__, subreq->start, subreq->len, len);
331 iov_iter_xarray(&iter, READ, &rreq->mapping->i_pages, subreq->start, len);
332 err = iov_iter_get_pages_alloc(&iter, &pages, len, &page_off);
334 dout("%s: iov_ter_get_pages_alloc returned %d\n", __func__, err);
338 /* should always give us a page-aligned read */
339 WARN_ON_ONCE(page_off);
342 osd_req_op_extent_osd_data_pages(req, 0, pages, len, 0, false, false);
343 req->r_callback = finish_netfs_read;
344 req->r_priv = subreq;
345 req->r_inode = inode;
348 err = ceph_osdc_start_request(req->r_osdc, req, false);
352 ceph_osdc_put_request(req);
354 netfs_subreq_terminated(subreq, err, false);
355 dout("%s: result %d\n", __func__, err);
358 static int ceph_init_request(struct netfs_io_request *rreq, struct file *file)
360 struct inode *inode = rreq->inode;
361 int got = 0, want = CEPH_CAP_FILE_CACHE;
364 if (rreq->origin != NETFS_READAHEAD)
368 struct ceph_rw_context *rw_ctx;
369 struct ceph_file_info *fi = file->private_data;
371 rw_ctx = ceph_find_rw_context(fi);
377 * readahead callers do not necessarily hold Fcb caps
378 * (e.g. fadvise, madvise).
380 ret = ceph_try_get_caps(inode, CEPH_CAP_FILE_RD, want, true, &got);
382 dout("start_read %p, error getting cap\n", inode);
387 dout("start_read %p, no cache cap\n", inode);
393 rreq->netfs_priv = (void *)(uintptr_t)got;
397 static void ceph_readahead_cleanup(struct address_space *mapping, void *priv)
399 struct inode *inode = mapping->host;
400 struct ceph_inode_info *ci = ceph_inode(inode);
401 int got = (uintptr_t)priv;
404 ceph_put_cap_refs(ci, got);
407 const struct netfs_request_ops ceph_netfs_ops = {
408 .init_request = ceph_init_request,
409 .begin_cache_operation = ceph_begin_cache_operation,
410 .issue_read = ceph_netfs_issue_read,
411 .expand_readahead = ceph_netfs_expand_readahead,
412 .clamp_length = ceph_netfs_clamp_length,
413 .check_write_begin = ceph_netfs_check_write_begin,
414 .cleanup = ceph_readahead_cleanup,
417 #ifdef CONFIG_CEPH_FSCACHE
418 static void ceph_set_page_fscache(struct page *page)
420 set_page_fscache(page);
423 static void ceph_fscache_write_terminated(void *priv, ssize_t error, bool was_async)
425 struct inode *inode = priv;
427 if (IS_ERR_VALUE(error) && error != -ENOBUFS)
428 ceph_fscache_invalidate(inode, false);
431 static void ceph_fscache_write_to_cache(struct inode *inode, u64 off, u64 len, bool caching)
433 struct ceph_inode_info *ci = ceph_inode(inode);
434 struct fscache_cookie *cookie = ceph_fscache_cookie(ci);
436 fscache_write_to_cache(cookie, inode->i_mapping, off, len, i_size_read(inode),
437 ceph_fscache_write_terminated, inode, caching);
440 static inline void ceph_set_page_fscache(struct page *page)
444 static inline void ceph_fscache_write_to_cache(struct inode *inode, u64 off, u64 len, bool caching)
447 #endif /* CONFIG_CEPH_FSCACHE */
449 struct ceph_writeback_ctl
459 * Get ref for the oldest snapc for an inode with dirty data... that is, the
460 * only snap context we are allowed to write back.
462 static struct ceph_snap_context *
463 get_oldest_context(struct inode *inode, struct ceph_writeback_ctl *ctl,
464 struct ceph_snap_context *page_snapc)
466 struct ceph_inode_info *ci = ceph_inode(inode);
467 struct ceph_snap_context *snapc = NULL;
468 struct ceph_cap_snap *capsnap = NULL;
470 spin_lock(&ci->i_ceph_lock);
471 list_for_each_entry(capsnap, &ci->i_cap_snaps, ci_item) {
472 dout(" cap_snap %p snapc %p has %d dirty pages\n", capsnap,
473 capsnap->context, capsnap->dirty_pages);
474 if (!capsnap->dirty_pages)
477 /* get i_size, truncate_{seq,size} for page_snapc? */
478 if (snapc && capsnap->context != page_snapc)
482 if (capsnap->writing) {
483 ctl->i_size = i_size_read(inode);
484 ctl->size_stable = false;
486 ctl->i_size = capsnap->size;
487 ctl->size_stable = true;
489 ctl->truncate_size = capsnap->truncate_size;
490 ctl->truncate_seq = capsnap->truncate_seq;
491 ctl->head_snapc = false;
497 snapc = ceph_get_snap_context(capsnap->context);
499 page_snapc == snapc ||
500 page_snapc->seq > snapc->seq)
503 if (!snapc && ci->i_wrbuffer_ref_head) {
504 snapc = ceph_get_snap_context(ci->i_head_snapc);
505 dout(" head snapc %p has %d dirty pages\n",
506 snapc, ci->i_wrbuffer_ref_head);
508 ctl->i_size = i_size_read(inode);
509 ctl->truncate_size = ci->i_truncate_size;
510 ctl->truncate_seq = ci->i_truncate_seq;
511 ctl->size_stable = false;
512 ctl->head_snapc = true;
515 spin_unlock(&ci->i_ceph_lock);
519 static u64 get_writepages_data_length(struct inode *inode,
520 struct page *page, u64 start)
522 struct ceph_inode_info *ci = ceph_inode(inode);
523 struct ceph_snap_context *snapc = page_snap_context(page);
524 struct ceph_cap_snap *capsnap = NULL;
525 u64 end = i_size_read(inode);
527 if (snapc != ci->i_head_snapc) {
529 spin_lock(&ci->i_ceph_lock);
530 list_for_each_entry(capsnap, &ci->i_cap_snaps, ci_item) {
531 if (capsnap->context == snapc) {
532 if (!capsnap->writing)
538 spin_unlock(&ci->i_ceph_lock);
541 if (end > page_offset(page) + thp_size(page))
542 end = page_offset(page) + thp_size(page);
543 return end > start ? end - start : 0;
547 * Write a single page, but leave the page locked.
549 * If we get a write error, mark the mapping for error, but still adjust the
550 * dirty page accounting (i.e., page is no longer dirty).
552 static int writepage_nounlock(struct page *page, struct writeback_control *wbc)
554 struct folio *folio = page_folio(page);
555 struct inode *inode = page->mapping->host;
556 struct ceph_inode_info *ci = ceph_inode(inode);
557 struct ceph_fs_client *fsc = ceph_inode_to_client(inode);
558 struct ceph_snap_context *snapc, *oldest;
559 loff_t page_off = page_offset(page);
561 loff_t len = thp_size(page);
562 struct ceph_writeback_ctl ceph_wbc;
563 struct ceph_osd_client *osdc = &fsc->client->osdc;
564 struct ceph_osd_request *req;
565 bool caching = ceph_is_cache_enabled(inode);
567 dout("writepage %p idx %lu\n", page, page->index);
569 /* verify this is a writeable snap context */
570 snapc = page_snap_context(page);
572 dout("writepage %p page %p not dirty?\n", inode, page);
575 oldest = get_oldest_context(inode, &ceph_wbc, snapc);
576 if (snapc->seq > oldest->seq) {
577 dout("writepage %p page %p snapc %p not writeable - noop\n",
579 /* we should only noop if called by kswapd */
580 WARN_ON(!(current->flags & PF_MEMALLOC));
581 ceph_put_snap_context(oldest);
582 redirty_page_for_writepage(wbc, page);
585 ceph_put_snap_context(oldest);
587 /* is this a partial page at end of file? */
588 if (page_off >= ceph_wbc.i_size) {
589 dout("folio at %lu beyond eof %llu\n", folio->index,
591 folio_invalidate(folio, 0, folio_size(folio));
595 if (ceph_wbc.i_size < page_off + len)
596 len = ceph_wbc.i_size - page_off;
598 dout("writepage %p page %p index %lu on %llu~%llu snapc %p seq %lld\n",
599 inode, page, page->index, page_off, len, snapc, snapc->seq);
601 if (atomic_long_inc_return(&fsc->writeback_count) >
602 CONGESTION_ON_THRESH(fsc->mount_options->congestion_kb))
603 fsc->write_congested = true;
605 req = ceph_osdc_new_request(osdc, &ci->i_layout, ceph_vino(inode), page_off, &len, 0, 1,
606 CEPH_OSD_OP_WRITE, CEPH_OSD_FLAG_WRITE, snapc,
607 ceph_wbc.truncate_seq, ceph_wbc.truncate_size,
610 redirty_page_for_writepage(wbc, page);
614 set_page_writeback(page);
616 ceph_set_page_fscache(page);
617 ceph_fscache_write_to_cache(inode, page_off, len, caching);
619 /* it may be a short write due to an object boundary */
620 WARN_ON_ONCE(len > thp_size(page));
621 osd_req_op_extent_osd_data_pages(req, 0, &page, len, 0, false, false);
622 dout("writepage %llu~%llu (%llu bytes)\n", page_off, len, len);
624 req->r_mtime = inode->i_mtime;
625 err = ceph_osdc_start_request(osdc, req, true);
627 err = ceph_osdc_wait_request(osdc, req);
629 ceph_update_write_metrics(&fsc->mdsc->metric, req->r_start_latency,
630 req->r_end_latency, len, err);
632 ceph_osdc_put_request(req);
637 struct writeback_control tmp_wbc;
640 if (err == -ERESTARTSYS) {
641 /* killed by SIGKILL */
642 dout("writepage interrupted page %p\n", page);
643 redirty_page_for_writepage(wbc, page);
644 end_page_writeback(page);
647 if (err == -EBLOCKLISTED)
648 fsc->blocklisted = true;
649 dout("writepage setting page/mapping error %d %p\n",
651 mapping_set_error(&inode->i_data, err);
652 wbc->pages_skipped++;
654 dout("writepage cleaned page %p\n", page);
655 err = 0; /* vfs expects us to return 0 */
657 oldest = detach_page_private(page);
658 WARN_ON_ONCE(oldest != snapc);
659 end_page_writeback(page);
660 ceph_put_wrbuffer_cap_refs(ci, 1, snapc);
661 ceph_put_snap_context(snapc); /* page's reference */
663 if (atomic_long_dec_return(&fsc->writeback_count) <
664 CONGESTION_OFF_THRESH(fsc->mount_options->congestion_kb))
665 fsc->write_congested = false;
670 static int ceph_writepage(struct page *page, struct writeback_control *wbc)
673 struct inode *inode = page->mapping->host;
677 if (wbc->sync_mode == WB_SYNC_NONE &&
678 ceph_inode_to_client(inode)->write_congested)
679 return AOP_WRITEPAGE_ACTIVATE;
681 wait_on_page_fscache(page);
683 err = writepage_nounlock(page, wbc);
684 if (err == -ERESTARTSYS) {
685 /* direct memory reclaimer was killed by SIGKILL. return 0
686 * to prevent caller from setting mapping/page error */
695 * async writeback completion handler.
697 * If we get an error, set the mapping error bit, but not the individual
700 static void writepages_finish(struct ceph_osd_request *req)
702 struct inode *inode = req->r_inode;
703 struct ceph_inode_info *ci = ceph_inode(inode);
704 struct ceph_osd_data *osd_data;
706 int num_pages, total_pages = 0;
708 int rc = req->r_result;
709 struct ceph_snap_context *snapc = req->r_snapc;
710 struct address_space *mapping = inode->i_mapping;
711 struct ceph_fs_client *fsc = ceph_inode_to_client(inode);
712 unsigned int len = 0;
715 dout("writepages_finish %p rc %d\n", inode, rc);
717 mapping_set_error(mapping, rc);
718 ceph_set_error_write(ci);
719 if (rc == -EBLOCKLISTED)
720 fsc->blocklisted = true;
722 ceph_clear_error_write(ci);
726 * We lost the cache cap, need to truncate the page before
727 * it is unlocked, otherwise we'd truncate it later in the
728 * page truncation thread, possibly losing some data that
731 remove_page = !(ceph_caps_issued(ci) &
732 (CEPH_CAP_FILE_CACHE|CEPH_CAP_FILE_LAZYIO));
734 /* clean all pages */
735 for (i = 0; i < req->r_num_ops; i++) {
736 if (req->r_ops[i].op != CEPH_OSD_OP_WRITE) {
737 pr_warn("%s incorrect op %d req %p index %d tid %llu\n",
738 __func__, req->r_ops[i].op, req, i, req->r_tid);
742 osd_data = osd_req_op_extent_osd_data(req, i);
743 BUG_ON(osd_data->type != CEPH_OSD_DATA_TYPE_PAGES);
744 len += osd_data->length;
745 num_pages = calc_pages_for((u64)osd_data->alignment,
746 (u64)osd_data->length);
747 total_pages += num_pages;
748 for (j = 0; j < num_pages; j++) {
749 page = osd_data->pages[j];
751 WARN_ON(!PageUptodate(page));
753 if (atomic_long_dec_return(&fsc->writeback_count) <
754 CONGESTION_OFF_THRESH(
755 fsc->mount_options->congestion_kb))
756 fsc->write_congested = false;
758 ceph_put_snap_context(detach_page_private(page));
759 end_page_writeback(page);
760 dout("unlocking %p\n", 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 release_pages(osd_data->pages, num_pages);
774 ceph_update_write_metrics(&fsc->mdsc->metric, req->r_start_latency,
775 req->r_end_latency, len, rc);
777 ceph_put_wrbuffer_cap_refs(ci, total_pages, snapc);
779 osd_data = osd_req_op_extent_osd_data(req, 0);
780 if (osd_data->pages_from_pool)
781 mempool_free(osd_data->pages, ceph_wb_pagevec_pool);
783 kfree(osd_data->pages);
784 ceph_osdc_put_request(req);
788 * initiate async writeback
790 static int ceph_writepages_start(struct address_space *mapping,
791 struct writeback_control *wbc)
793 struct inode *inode = mapping->host;
794 struct ceph_inode_info *ci = ceph_inode(inode);
795 struct ceph_fs_client *fsc = ceph_inode_to_client(inode);
796 struct ceph_vino vino = ceph_vino(inode);
797 pgoff_t index, start_index, end = -1;
798 struct ceph_snap_context *snapc = NULL, *last_snapc = NULL, *pgsnapc;
801 unsigned int wsize = i_blocksize(inode);
802 struct ceph_osd_request *req = NULL;
803 struct ceph_writeback_ctl ceph_wbc;
804 bool should_loop, range_whole = false;
806 bool caching = ceph_is_cache_enabled(inode);
808 if (wbc->sync_mode == WB_SYNC_NONE &&
809 fsc->write_congested)
812 dout("writepages_start %p (mode=%s)\n", inode,
813 wbc->sync_mode == WB_SYNC_NONE ? "NONE" :
814 (wbc->sync_mode == WB_SYNC_ALL ? "ALL" : "HOLD"));
816 if (ceph_inode_is_shutdown(inode)) {
817 if (ci->i_wrbuffer_ref > 0) {
819 "writepage_start %p %lld forced umount\n",
820 inode, ceph_ino(inode));
822 mapping_set_error(mapping, -EIO);
823 return -EIO; /* we're in a forced umount, don't write! */
825 if (fsc->mount_options->wsize < wsize)
826 wsize = fsc->mount_options->wsize;
830 start_index = wbc->range_cyclic ? mapping->writeback_index : 0;
834 /* find oldest snap context with dirty data */
835 snapc = get_oldest_context(inode, &ceph_wbc, NULL);
837 /* hmm, why does writepages get called when there
839 dout(" no snap context with dirty data?\n");
842 dout(" oldest snapc is %p seq %lld (%d snaps)\n",
843 snapc, snapc->seq, snapc->num_snaps);
846 if (ceph_wbc.head_snapc && snapc != last_snapc) {
847 /* where to start/end? */
848 if (wbc->range_cyclic) {
853 dout(" cyclic, start at %lu\n", index);
855 index = wbc->range_start >> PAGE_SHIFT;
856 end = wbc->range_end >> PAGE_SHIFT;
857 if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX)
859 dout(" not cyclic, %lu to %lu\n", index, end);
861 } else if (!ceph_wbc.head_snapc) {
862 /* Do not respect wbc->range_{start,end}. Dirty pages
863 * in that range can be associated with newer snapc.
864 * They are not writeable until we write all dirty pages
865 * associated with 'snapc' get written */
868 dout(" non-head snapc, range whole\n");
871 ceph_put_snap_context(last_snapc);
874 while (!done && index <= end) {
875 int num_ops = 0, op_idx;
876 unsigned i, pvec_pages, max_pages, locked_pages = 0;
877 struct page **pages = NULL, **data_pages;
879 pgoff_t strip_unit_end = 0;
880 u64 offset = 0, len = 0;
881 bool from_pool = false;
883 max_pages = wsize >> PAGE_SHIFT;
886 pvec_pages = pagevec_lookup_range_tag(&pvec, mapping, &index,
887 end, PAGECACHE_TAG_DIRTY);
888 dout("pagevec_lookup_range_tag got %d\n", pvec_pages);
889 if (!pvec_pages && !locked_pages)
891 for (i = 0; i < pvec_pages && locked_pages < max_pages; i++) {
892 page = pvec.pages[i];
893 dout("? %p idx %lu\n", page, page->index);
894 if (locked_pages == 0)
895 lock_page(page); /* first page */
896 else if (!trylock_page(page))
899 /* only dirty pages, or our accounting breaks */
900 if (unlikely(!PageDirty(page)) ||
901 unlikely(page->mapping != mapping)) {
902 dout("!dirty or !mapping %p\n", page);
906 /* only if matching snap context */
907 pgsnapc = page_snap_context(page);
908 if (pgsnapc != snapc) {
909 dout("page snapc %p %lld != oldest %p %lld\n",
910 pgsnapc, pgsnapc->seq, snapc, snapc->seq);
912 !ceph_wbc.head_snapc &&
913 wbc->sync_mode != WB_SYNC_NONE)
918 if (page_offset(page) >= ceph_wbc.i_size) {
919 struct folio *folio = page_folio(page);
921 dout("folio at %lu beyond eof %llu\n",
922 folio->index, ceph_wbc.i_size);
923 if ((ceph_wbc.size_stable ||
924 folio_pos(folio) >= i_size_read(inode)) &&
925 folio_clear_dirty_for_io(folio))
926 folio_invalidate(folio, 0,
931 if (strip_unit_end && (page->index > strip_unit_end)) {
932 dout("end of strip unit %p\n", page);
936 if (PageWriteback(page) || PageFsCache(page)) {
937 if (wbc->sync_mode == WB_SYNC_NONE) {
938 dout("%p under writeback\n", page);
942 dout("waiting on writeback %p\n", page);
943 wait_on_page_writeback(page);
944 wait_on_page_fscache(page);
947 if (!clear_page_dirty_for_io(page)) {
948 dout("%p !clear_page_dirty_for_io\n", page);
954 * We have something to write. If this is
955 * the first locked page this time through,
956 * calculate max possinle write size and
957 * allocate a page array
959 if (locked_pages == 0) {
964 /* prepare async write request */
965 offset = (u64)page_offset(page);
966 ceph_calc_file_object_mapping(&ci->i_layout,
973 strip_unit_end = page->index +
974 ((len - 1) >> PAGE_SHIFT);
977 max_pages = calc_pages_for(0, (u64)len);
978 pages = kmalloc_array(max_pages,
983 pages = mempool_alloc(ceph_wb_pagevec_pool, GFP_NOFS);
988 } else if (page->index !=
989 (offset + len) >> PAGE_SHIFT) {
990 if (num_ops >= (from_pool ? CEPH_OSD_SLAB_OPS :
992 redirty_page_for_writepage(wbc, page);
998 offset = (u64)page_offset(page);
1002 /* note position of first page in pvec */
1003 dout("%p will write page %p idx %lu\n",
1004 inode, page, page->index);
1006 if (atomic_long_inc_return(&fsc->writeback_count) >
1007 CONGESTION_ON_THRESH(
1008 fsc->mount_options->congestion_kb))
1009 fsc->write_congested = true;
1011 pages[locked_pages++] = page;
1012 pvec.pages[i] = NULL;
1014 len += thp_size(page);
1017 /* did we get anything? */
1019 goto release_pvec_pages;
1022 /* shift unused page to beginning of pvec */
1023 for (j = 0; j < pvec_pages; j++) {
1027 pvec.pages[n] = pvec.pages[j];
1032 if (pvec_pages && i == pvec_pages &&
1033 locked_pages < max_pages) {
1034 dout("reached end pvec, trying for more\n");
1035 pagevec_release(&pvec);
1036 goto get_more_pages;
1041 offset = page_offset(pages[0]);
1044 req = ceph_osdc_new_request(&fsc->client->osdc,
1045 &ci->i_layout, vino,
1046 offset, &len, 0, num_ops,
1047 CEPH_OSD_OP_WRITE, CEPH_OSD_FLAG_WRITE,
1048 snapc, ceph_wbc.truncate_seq,
1049 ceph_wbc.truncate_size, false);
1051 req = ceph_osdc_new_request(&fsc->client->osdc,
1052 &ci->i_layout, vino,
1057 CEPH_OSD_FLAG_WRITE,
1058 snapc, ceph_wbc.truncate_seq,
1059 ceph_wbc.truncate_size, true);
1060 BUG_ON(IS_ERR(req));
1062 BUG_ON(len < page_offset(pages[locked_pages - 1]) +
1063 thp_size(page) - offset);
1065 req->r_callback = writepages_finish;
1066 req->r_inode = inode;
1068 /* Format the osd request message and submit the write */
1072 for (i = 0; i < locked_pages; i++) {
1073 u64 cur_offset = page_offset(pages[i]);
1075 * Discontinuity in page range? Ceph can handle that by just passing
1076 * multiple extents in the write op.
1078 if (offset + len != cur_offset) {
1079 /* If it's full, stop here */
1080 if (op_idx + 1 == req->r_num_ops)
1083 /* Kick off an fscache write with what we have so far. */
1084 ceph_fscache_write_to_cache(inode, offset, len, caching);
1086 /* Start a new extent */
1087 osd_req_op_extent_dup_last(req, op_idx,
1088 cur_offset - offset);
1089 dout("writepages got pages at %llu~%llu\n",
1091 osd_req_op_extent_osd_data_pages(req, op_idx,
1094 osd_req_op_extent_update(req, op_idx, len);
1097 offset = cur_offset;
1098 data_pages = pages + i;
1102 set_page_writeback(pages[i]);
1104 ceph_set_page_fscache(pages[i]);
1105 len += thp_size(page);
1107 ceph_fscache_write_to_cache(inode, offset, len, caching);
1109 if (ceph_wbc.size_stable) {
1110 len = min(len, ceph_wbc.i_size - offset);
1111 } else if (i == locked_pages) {
1112 /* writepages_finish() clears writeback pages
1113 * according to the data length, so make sure
1114 * data length covers all locked pages */
1115 u64 min_len = len + 1 - thp_size(page);
1116 len = get_writepages_data_length(inode, pages[i - 1],
1118 len = max(len, min_len);
1120 dout("writepages got pages at %llu~%llu\n", offset, len);
1122 osd_req_op_extent_osd_data_pages(req, op_idx, data_pages, len,
1123 0, from_pool, false);
1124 osd_req_op_extent_update(req, op_idx, len);
1126 BUG_ON(op_idx + 1 != req->r_num_ops);
1129 if (i < locked_pages) {
1130 BUG_ON(num_ops <= req->r_num_ops);
1131 num_ops -= req->r_num_ops;
1134 /* allocate new pages array for next request */
1136 pages = kmalloc_array(locked_pages, sizeof(*pages),
1140 pages = mempool_alloc(ceph_wb_pagevec_pool, GFP_NOFS);
1143 memcpy(pages, data_pages + i,
1144 locked_pages * sizeof(*pages));
1145 memset(data_pages + i, 0,
1146 locked_pages * sizeof(*pages));
1148 BUG_ON(num_ops != req->r_num_ops);
1149 index = pages[i - 1]->index + 1;
1150 /* request message now owns the pages array */
1154 req->r_mtime = inode->i_mtime;
1155 rc = ceph_osdc_start_request(&fsc->client->osdc, req, true);
1159 wbc->nr_to_write -= i;
1164 * We stop writing back only if we are not doing
1165 * integrity sync. In case of integrity sync we have to
1166 * keep going until we have written all the pages
1167 * we tagged for writeback prior to entering this loop.
1169 if (wbc->nr_to_write <= 0 && wbc->sync_mode == WB_SYNC_NONE)
1173 dout("pagevec_release on %d pages (%p)\n", (int)pvec.nr,
1174 pvec.nr ? pvec.pages[0] : NULL);
1175 pagevec_release(&pvec);
1178 if (should_loop && !done) {
1179 /* more to do; loop back to beginning of file */
1180 dout("writepages looping back to beginning of file\n");
1181 end = start_index - 1; /* OK even when start_index == 0 */
1183 /* to write dirty pages associated with next snapc,
1184 * we need to wait until current writes complete */
1185 if (wbc->sync_mode != WB_SYNC_NONE &&
1186 start_index == 0 && /* all dirty pages were checked */
1187 !ceph_wbc.head_snapc) {
1191 while ((index <= end) &&
1192 (nr = pagevec_lookup_tag(&pvec, mapping, &index,
1193 PAGECACHE_TAG_WRITEBACK))) {
1194 for (i = 0; i < nr; i++) {
1195 page = pvec.pages[i];
1196 if (page_snap_context(page) != snapc)
1198 wait_on_page_writeback(page);
1200 pagevec_release(&pvec);
1210 if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0))
1211 mapping->writeback_index = index;
1214 ceph_osdc_put_request(req);
1215 ceph_put_snap_context(last_snapc);
1216 dout("writepages dend - startone, rc = %d\n", rc);
1223 * See if a given @snapc is either writeable, or already written.
1225 static int context_is_writeable_or_written(struct inode *inode,
1226 struct ceph_snap_context *snapc)
1228 struct ceph_snap_context *oldest = get_oldest_context(inode, NULL, NULL);
1229 int ret = !oldest || snapc->seq <= oldest->seq;
1231 ceph_put_snap_context(oldest);
1236 * ceph_find_incompatible - find an incompatible context and return it
1237 * @page: page being dirtied
1239 * We are only allowed to write into/dirty a page if the page is
1240 * clean, or already dirty within the same snap context. Returns a
1241 * conflicting context if there is one, NULL if there isn't, or a
1242 * negative error code on other errors.
1244 * Must be called with page lock held.
1246 static struct ceph_snap_context *
1247 ceph_find_incompatible(struct page *page)
1249 struct inode *inode = page->mapping->host;
1250 struct ceph_inode_info *ci = ceph_inode(inode);
1252 if (ceph_inode_is_shutdown(inode)) {
1253 dout(" page %p %llx:%llx is shutdown\n", page,
1255 return ERR_PTR(-ESTALE);
1259 struct ceph_snap_context *snapc, *oldest;
1261 wait_on_page_writeback(page);
1263 snapc = page_snap_context(page);
1264 if (!snapc || snapc == ci->i_head_snapc)
1268 * this page is already dirty in another (older) snap
1269 * context! is it writeable now?
1271 oldest = get_oldest_context(inode, NULL, NULL);
1272 if (snapc->seq > oldest->seq) {
1273 /* not writeable -- return it for the caller to deal with */
1274 ceph_put_snap_context(oldest);
1275 dout(" page %p snapc %p not current or oldest\n", page, snapc);
1276 return ceph_get_snap_context(snapc);
1278 ceph_put_snap_context(oldest);
1280 /* yay, writeable, do it now (without dropping page lock) */
1281 dout(" page %p snapc %p not current, but oldest\n", page, snapc);
1282 if (clear_page_dirty_for_io(page)) {
1283 int r = writepage_nounlock(page, NULL);
1291 static int ceph_netfs_check_write_begin(struct file *file, loff_t pos, unsigned int len,
1292 struct folio *folio, void **_fsdata)
1294 struct inode *inode = file_inode(file);
1295 struct ceph_inode_info *ci = ceph_inode(inode);
1296 struct ceph_snap_context *snapc;
1298 snapc = ceph_find_incompatible(folio_page(folio, 0));
1302 folio_unlock(folio);
1305 return PTR_ERR(snapc);
1307 ceph_queue_writeback(inode);
1308 r = wait_event_killable(ci->i_cap_wq,
1309 context_is_writeable_or_written(inode, snapc));
1310 ceph_put_snap_context(snapc);
1311 return r == 0 ? -EAGAIN : r;
1317 * We are only allowed to write into/dirty the page if the page is
1318 * clean, or already dirty within the same snap context.
1320 static int ceph_write_begin(struct file *file, struct address_space *mapping,
1321 loff_t pos, unsigned len, unsigned aop_flags,
1322 struct page **pagep, void **fsdata)
1324 struct inode *inode = file_inode(file);
1325 struct folio *folio = NULL;
1328 r = netfs_write_begin(file, inode->i_mapping, pos, len, 0, &folio, NULL);
1330 folio_wait_fscache(folio);
1335 WARN_ON_ONCE(!folio_test_locked(folio));
1336 *pagep = &folio->page;
1342 * we don't do anything in here that simple_write_end doesn't do
1343 * except adjust dirty page accounting
1345 static int ceph_write_end(struct file *file, struct address_space *mapping,
1346 loff_t pos, unsigned len, unsigned copied,
1347 struct page *subpage, void *fsdata)
1349 struct folio *folio = page_folio(subpage);
1350 struct inode *inode = file_inode(file);
1351 bool check_cap = false;
1353 dout("write_end file %p inode %p folio %p %d~%d (%d)\n", file,
1354 inode, folio, (int)pos, (int)copied, (int)len);
1356 if (!folio_test_uptodate(folio)) {
1357 /* just return that nothing was copied on a short copy */
1362 folio_mark_uptodate(folio);
1365 /* did file size increase? */
1366 if (pos+copied > i_size_read(inode))
1367 check_cap = ceph_inode_set_size(inode, pos+copied);
1369 folio_mark_dirty(folio);
1372 folio_unlock(folio);
1376 ceph_check_caps(ceph_inode(inode), CHECK_CAPS_AUTHONLY, NULL);
1381 const struct address_space_operations ceph_aops = {
1382 .readpage = netfs_readpage,
1383 .readahead = netfs_readahead,
1384 .writepage = ceph_writepage,
1385 .writepages = ceph_writepages_start,
1386 .write_begin = ceph_write_begin,
1387 .write_end = ceph_write_end,
1388 .dirty_folio = ceph_dirty_folio,
1389 .invalidate_folio = ceph_invalidate_folio,
1390 .releasepage = ceph_releasepage,
1391 .direct_IO = noop_direct_IO,
1394 static void ceph_block_sigs(sigset_t *oldset)
1397 siginitsetinv(&mask, sigmask(SIGKILL));
1398 sigprocmask(SIG_BLOCK, &mask, oldset);
1401 static void ceph_restore_sigs(sigset_t *oldset)
1403 sigprocmask(SIG_SETMASK, oldset, NULL);
1409 static vm_fault_t ceph_filemap_fault(struct vm_fault *vmf)
1411 struct vm_area_struct *vma = vmf->vma;
1412 struct inode *inode = file_inode(vma->vm_file);
1413 struct ceph_inode_info *ci = ceph_inode(inode);
1414 struct ceph_file_info *fi = vma->vm_file->private_data;
1415 loff_t off = (loff_t)vmf->pgoff << PAGE_SHIFT;
1418 vm_fault_t ret = VM_FAULT_SIGBUS;
1420 if (ceph_inode_is_shutdown(inode))
1423 ceph_block_sigs(&oldset);
1425 dout("filemap_fault %p %llx.%llx %llu trying to get caps\n",
1426 inode, ceph_vinop(inode), off);
1427 if (fi->fmode & CEPH_FILE_MODE_LAZY)
1428 want = CEPH_CAP_FILE_CACHE | CEPH_CAP_FILE_LAZYIO;
1430 want = CEPH_CAP_FILE_CACHE;
1433 err = ceph_get_caps(vma->vm_file, CEPH_CAP_FILE_RD, want, -1, &got);
1437 dout("filemap_fault %p %llu got cap refs on %s\n",
1438 inode, off, ceph_cap_string(got));
1440 if ((got & (CEPH_CAP_FILE_CACHE | CEPH_CAP_FILE_LAZYIO)) ||
1441 ci->i_inline_version == CEPH_INLINE_NONE) {
1442 CEPH_DEFINE_RW_CONTEXT(rw_ctx, got);
1443 ceph_add_rw_context(fi, &rw_ctx);
1444 ret = filemap_fault(vmf);
1445 ceph_del_rw_context(fi, &rw_ctx);
1446 dout("filemap_fault %p %llu drop cap refs %s ret %x\n",
1447 inode, off, ceph_cap_string(got), ret);
1451 ceph_put_cap_refs(ci, got);
1456 /* read inline data */
1457 if (off >= PAGE_SIZE) {
1458 /* does not support inline data > PAGE_SIZE */
1459 ret = VM_FAULT_SIGBUS;
1461 struct address_space *mapping = inode->i_mapping;
1464 filemap_invalidate_lock_shared(mapping);
1465 page = find_or_create_page(mapping, 0,
1466 mapping_gfp_constraint(mapping, ~__GFP_FS));
1471 err = __ceph_do_getattr(inode, page,
1472 CEPH_STAT_CAP_INLINE_DATA, true);
1473 if (err < 0 || off >= i_size_read(inode)) {
1476 ret = vmf_error(err);
1479 if (err < PAGE_SIZE)
1480 zero_user_segment(page, err, PAGE_SIZE);
1482 flush_dcache_page(page);
1483 SetPageUptodate(page);
1485 ret = VM_FAULT_MAJOR | VM_FAULT_LOCKED;
1487 filemap_invalidate_unlock_shared(mapping);
1488 dout("filemap_fault %p %llu read inline data ret %x\n",
1492 ceph_restore_sigs(&oldset);
1494 ret = vmf_error(err);
1499 static vm_fault_t ceph_page_mkwrite(struct vm_fault *vmf)
1501 struct vm_area_struct *vma = vmf->vma;
1502 struct inode *inode = file_inode(vma->vm_file);
1503 struct ceph_inode_info *ci = ceph_inode(inode);
1504 struct ceph_file_info *fi = vma->vm_file->private_data;
1505 struct ceph_cap_flush *prealloc_cf;
1506 struct page *page = vmf->page;
1507 loff_t off = page_offset(page);
1508 loff_t size = i_size_read(inode);
1512 vm_fault_t ret = VM_FAULT_SIGBUS;
1514 if (ceph_inode_is_shutdown(inode))
1517 prealloc_cf = ceph_alloc_cap_flush();
1519 return VM_FAULT_OOM;
1521 sb_start_pagefault(inode->i_sb);
1522 ceph_block_sigs(&oldset);
1524 if (off + thp_size(page) <= size)
1525 len = thp_size(page);
1527 len = offset_in_thp(page, size);
1529 dout("page_mkwrite %p %llx.%llx %llu~%zd getting caps i_size %llu\n",
1530 inode, ceph_vinop(inode), off, len, size);
1531 if (fi->fmode & CEPH_FILE_MODE_LAZY)
1532 want = CEPH_CAP_FILE_BUFFER | CEPH_CAP_FILE_LAZYIO;
1534 want = CEPH_CAP_FILE_BUFFER;
1537 err = ceph_get_caps(vma->vm_file, CEPH_CAP_FILE_WR, want, off + len, &got);
1541 dout("page_mkwrite %p %llu~%zd got cap refs on %s\n",
1542 inode, off, len, ceph_cap_string(got));
1544 /* Update time before taking page lock */
1545 file_update_time(vma->vm_file);
1546 inode_inc_iversion_raw(inode);
1549 struct ceph_snap_context *snapc;
1553 if (page_mkwrite_check_truncate(page, inode) < 0) {
1555 ret = VM_FAULT_NOPAGE;
1559 snapc = ceph_find_incompatible(page);
1561 /* success. we'll keep the page locked. */
1562 set_page_dirty(page);
1563 ret = VM_FAULT_LOCKED;
1569 if (IS_ERR(snapc)) {
1570 ret = VM_FAULT_SIGBUS;
1574 ceph_queue_writeback(inode);
1575 err = wait_event_killable(ci->i_cap_wq,
1576 context_is_writeable_or_written(inode, snapc));
1577 ceph_put_snap_context(snapc);
1580 if (ret == VM_FAULT_LOCKED) {
1582 spin_lock(&ci->i_ceph_lock);
1583 dirty = __ceph_mark_dirty_caps(ci, CEPH_CAP_FILE_WR,
1585 spin_unlock(&ci->i_ceph_lock);
1587 __mark_inode_dirty(inode, dirty);
1590 dout("page_mkwrite %p %llu~%zd dropping cap refs on %s ret %x\n",
1591 inode, off, len, ceph_cap_string(got), ret);
1592 ceph_put_cap_refs_async(ci, got);
1594 ceph_restore_sigs(&oldset);
1595 sb_end_pagefault(inode->i_sb);
1596 ceph_free_cap_flush(prealloc_cf);
1598 ret = vmf_error(err);
1602 void ceph_fill_inline_data(struct inode *inode, struct page *locked_page,
1603 char *data, size_t len)
1605 struct address_space *mapping = inode->i_mapping;
1611 if (i_size_read(inode) == 0)
1613 page = find_or_create_page(mapping, 0,
1614 mapping_gfp_constraint(mapping,
1618 if (PageUptodate(page)) {
1625 dout("fill_inline_data %p %llx.%llx len %zu locked_page %p\n",
1626 inode, ceph_vinop(inode), len, locked_page);
1629 void *kaddr = kmap_atomic(page);
1630 memcpy(kaddr, data, len);
1631 kunmap_atomic(kaddr);
1634 if (page != locked_page) {
1635 if (len < PAGE_SIZE)
1636 zero_user_segment(page, len, PAGE_SIZE);
1638 flush_dcache_page(page);
1640 SetPageUptodate(page);
1646 int ceph_uninline_data(struct file *file)
1648 struct inode *inode = file_inode(file);
1649 struct ceph_inode_info *ci = ceph_inode(inode);
1650 struct ceph_fs_client *fsc = ceph_inode_to_client(inode);
1651 struct ceph_osd_request *req;
1652 struct ceph_cap_flush *prealloc_cf;
1653 struct folio *folio = NULL;
1654 u64 inline_version = CEPH_INLINE_NONE;
1655 struct page *pages[1];
1659 prealloc_cf = ceph_alloc_cap_flush();
1663 folio = read_mapping_folio(inode->i_mapping, 0, file);
1664 if (IS_ERR(folio)) {
1665 err = PTR_ERR(folio);
1671 spin_lock(&ci->i_ceph_lock);
1672 inline_version = ci->i_inline_version;
1673 spin_unlock(&ci->i_ceph_lock);
1675 dout("uninline_data %p %llx.%llx inline_version %llu\n",
1676 inode, ceph_vinop(inode), inline_version);
1678 if (inline_version == 1 || /* initial version, no data */
1679 inline_version == CEPH_INLINE_NONE)
1682 len = i_size_read(inode);
1683 if (len > folio_size(folio))
1684 len = folio_size(folio);
1686 req = ceph_osdc_new_request(&fsc->client->osdc, &ci->i_layout,
1687 ceph_vino(inode), 0, &len, 0, 1,
1688 CEPH_OSD_OP_CREATE, CEPH_OSD_FLAG_WRITE,
1695 req->r_mtime = inode->i_mtime;
1696 err = ceph_osdc_start_request(&fsc->client->osdc, req, false);
1698 err = ceph_osdc_wait_request(&fsc->client->osdc, req);
1699 ceph_osdc_put_request(req);
1703 req = ceph_osdc_new_request(&fsc->client->osdc, &ci->i_layout,
1704 ceph_vino(inode), 0, &len, 1, 3,
1705 CEPH_OSD_OP_WRITE, CEPH_OSD_FLAG_WRITE,
1706 NULL, ci->i_truncate_seq,
1707 ci->i_truncate_size, false);
1713 pages[0] = folio_page(folio, 0);
1714 osd_req_op_extent_osd_data_pages(req, 1, pages, len, 0, false, false);
1717 __le64 xattr_buf = cpu_to_le64(inline_version);
1718 err = osd_req_op_xattr_init(req, 0, CEPH_OSD_OP_CMPXATTR,
1719 "inline_version", &xattr_buf,
1721 CEPH_OSD_CMPXATTR_OP_GT,
1722 CEPH_OSD_CMPXATTR_MODE_U64);
1729 int xattr_len = snprintf(xattr_buf, sizeof(xattr_buf),
1730 "%llu", inline_version);
1731 err = osd_req_op_xattr_init(req, 2, CEPH_OSD_OP_SETXATTR,
1733 xattr_buf, xattr_len, 0, 0);
1738 req->r_mtime = inode->i_mtime;
1739 err = ceph_osdc_start_request(&fsc->client->osdc, req, false);
1741 err = ceph_osdc_wait_request(&fsc->client->osdc, req);
1743 ceph_update_write_metrics(&fsc->mdsc->metric, req->r_start_latency,
1744 req->r_end_latency, len, err);
1749 /* Set to CAP_INLINE_NONE and dirty the caps */
1750 down_read(&fsc->mdsc->snap_rwsem);
1751 spin_lock(&ci->i_ceph_lock);
1752 ci->i_inline_version = CEPH_INLINE_NONE;
1753 dirty = __ceph_mark_dirty_caps(ci, CEPH_CAP_FILE_WR, &prealloc_cf);
1754 spin_unlock(&ci->i_ceph_lock);
1755 up_read(&fsc->mdsc->snap_rwsem);
1757 __mark_inode_dirty(inode, dirty);
1760 ceph_osdc_put_request(req);
1761 if (err == -ECANCELED)
1764 folio_unlock(folio);
1767 ceph_free_cap_flush(prealloc_cf);
1768 dout("uninline_data %p %llx.%llx inline_version %llu = %d\n",
1769 inode, ceph_vinop(inode), inline_version, err);
1773 static const struct vm_operations_struct ceph_vmops = {
1774 .fault = ceph_filemap_fault,
1775 .page_mkwrite = ceph_page_mkwrite,
1778 int ceph_mmap(struct file *file, struct vm_area_struct *vma)
1780 struct address_space *mapping = file->f_mapping;
1782 if (!mapping->a_ops->readpage)
1784 vma->vm_ops = &ceph_vmops;
1793 static int __ceph_pool_perm_get(struct ceph_inode_info *ci,
1794 s64 pool, struct ceph_string *pool_ns)
1796 struct ceph_fs_client *fsc = ceph_inode_to_client(&ci->vfs_inode);
1797 struct ceph_mds_client *mdsc = fsc->mdsc;
1798 struct ceph_osd_request *rd_req = NULL, *wr_req = NULL;
1799 struct rb_node **p, *parent;
1800 struct ceph_pool_perm *perm;
1801 struct page **pages;
1803 int err = 0, err2 = 0, have = 0;
1805 down_read(&mdsc->pool_perm_rwsem);
1806 p = &mdsc->pool_perm_tree.rb_node;
1808 perm = rb_entry(*p, struct ceph_pool_perm, node);
1809 if (pool < perm->pool)
1811 else if (pool > perm->pool)
1812 p = &(*p)->rb_right;
1814 int ret = ceph_compare_string(pool_ns,
1820 p = &(*p)->rb_right;
1827 up_read(&mdsc->pool_perm_rwsem);
1832 dout("__ceph_pool_perm_get pool %lld ns %.*s no perm cached\n",
1833 pool, (int)pool_ns->len, pool_ns->str);
1835 dout("__ceph_pool_perm_get pool %lld no perm cached\n", pool);
1837 down_write(&mdsc->pool_perm_rwsem);
1838 p = &mdsc->pool_perm_tree.rb_node;
1842 perm = rb_entry(parent, struct ceph_pool_perm, node);
1843 if (pool < perm->pool)
1845 else if (pool > perm->pool)
1846 p = &(*p)->rb_right;
1848 int ret = ceph_compare_string(pool_ns,
1854 p = &(*p)->rb_right;
1862 up_write(&mdsc->pool_perm_rwsem);
1866 rd_req = ceph_osdc_alloc_request(&fsc->client->osdc, NULL,
1867 1, false, GFP_NOFS);
1873 rd_req->r_flags = CEPH_OSD_FLAG_READ;
1874 osd_req_op_init(rd_req, 0, CEPH_OSD_OP_STAT, 0);
1875 rd_req->r_base_oloc.pool = pool;
1877 rd_req->r_base_oloc.pool_ns = ceph_get_string(pool_ns);
1878 ceph_oid_printf(&rd_req->r_base_oid, "%llx.00000000", ci->i_vino.ino);
1880 err = ceph_osdc_alloc_messages(rd_req, GFP_NOFS);
1884 wr_req = ceph_osdc_alloc_request(&fsc->client->osdc, NULL,
1885 1, false, GFP_NOFS);
1891 wr_req->r_flags = CEPH_OSD_FLAG_WRITE;
1892 osd_req_op_init(wr_req, 0, CEPH_OSD_OP_CREATE, CEPH_OSD_OP_FLAG_EXCL);
1893 ceph_oloc_copy(&wr_req->r_base_oloc, &rd_req->r_base_oloc);
1894 ceph_oid_copy(&wr_req->r_base_oid, &rd_req->r_base_oid);
1896 err = ceph_osdc_alloc_messages(wr_req, GFP_NOFS);
1900 /* one page should be large enough for STAT data */
1901 pages = ceph_alloc_page_vector(1, GFP_KERNEL);
1902 if (IS_ERR(pages)) {
1903 err = PTR_ERR(pages);
1907 osd_req_op_raw_data_in_pages(rd_req, 0, pages, PAGE_SIZE,
1909 err = ceph_osdc_start_request(&fsc->client->osdc, rd_req, false);
1911 wr_req->r_mtime = ci->vfs_inode.i_mtime;
1912 err2 = ceph_osdc_start_request(&fsc->client->osdc, wr_req, false);
1915 err = ceph_osdc_wait_request(&fsc->client->osdc, rd_req);
1917 err2 = ceph_osdc_wait_request(&fsc->client->osdc, wr_req);
1919 if (err >= 0 || err == -ENOENT)
1921 else if (err != -EPERM) {
1922 if (err == -EBLOCKLISTED)
1923 fsc->blocklisted = true;
1927 if (err2 == 0 || err2 == -EEXIST)
1929 else if (err2 != -EPERM) {
1930 if (err2 == -EBLOCKLISTED)
1931 fsc->blocklisted = true;
1936 pool_ns_len = pool_ns ? pool_ns->len : 0;
1937 perm = kmalloc(sizeof(*perm) + pool_ns_len + 1, GFP_NOFS);
1945 perm->pool_ns_len = pool_ns_len;
1946 if (pool_ns_len > 0)
1947 memcpy(perm->pool_ns, pool_ns->str, pool_ns_len);
1948 perm->pool_ns[pool_ns_len] = 0;
1950 rb_link_node(&perm->node, parent, p);
1951 rb_insert_color(&perm->node, &mdsc->pool_perm_tree);
1954 up_write(&mdsc->pool_perm_rwsem);
1956 ceph_osdc_put_request(rd_req);
1957 ceph_osdc_put_request(wr_req);
1962 dout("__ceph_pool_perm_get pool %lld ns %.*s result = %d\n",
1963 pool, (int)pool_ns->len, pool_ns->str, err);
1965 dout("__ceph_pool_perm_get pool %lld result = %d\n", pool, err);
1969 int ceph_pool_perm_check(struct inode *inode, int need)
1971 struct ceph_inode_info *ci = ceph_inode(inode);
1972 struct ceph_string *pool_ns;
1976 /* Only need to do this for regular files */
1977 if (!S_ISREG(inode->i_mode))
1980 if (ci->i_vino.snap != CEPH_NOSNAP) {
1982 * Pool permission check needs to write to the first object.
1983 * But for snapshot, head of the first object may have alread
1984 * been deleted. Skip check to avoid creating orphan object.
1989 if (ceph_test_mount_opt(ceph_inode_to_client(inode),
1993 spin_lock(&ci->i_ceph_lock);
1994 flags = ci->i_ceph_flags;
1995 pool = ci->i_layout.pool_id;
1996 spin_unlock(&ci->i_ceph_lock);
1998 if (flags & CEPH_I_POOL_PERM) {
1999 if ((need & CEPH_CAP_FILE_RD) && !(flags & CEPH_I_POOL_RD)) {
2000 dout("ceph_pool_perm_check pool %lld no read perm\n",
2004 if ((need & CEPH_CAP_FILE_WR) && !(flags & CEPH_I_POOL_WR)) {
2005 dout("ceph_pool_perm_check pool %lld no write perm\n",
2012 pool_ns = ceph_try_get_string(ci->i_layout.pool_ns);
2013 ret = __ceph_pool_perm_get(ci, pool, pool_ns);
2014 ceph_put_string(pool_ns);
2018 flags = CEPH_I_POOL_PERM;
2019 if (ret & POOL_READ)
2020 flags |= CEPH_I_POOL_RD;
2021 if (ret & POOL_WRITE)
2022 flags |= CEPH_I_POOL_WR;
2024 spin_lock(&ci->i_ceph_lock);
2025 if (pool == ci->i_layout.pool_id &&
2026 pool_ns == rcu_dereference_raw(ci->i_layout.pool_ns)) {
2027 ci->i_ceph_flags |= flags;
2029 pool = ci->i_layout.pool_id;
2030 flags = ci->i_ceph_flags;
2032 spin_unlock(&ci->i_ceph_lock);
2036 void ceph_pool_perm_destroy(struct ceph_mds_client *mdsc)
2038 struct ceph_pool_perm *perm;
2041 while (!RB_EMPTY_ROOT(&mdsc->pool_perm_tree)) {
2042 n = rb_first(&mdsc->pool_perm_tree);
2043 perm = rb_entry(n, struct ceph_pool_perm, node);
2044 rb_erase(n, &mdsc->pool_perm_tree);