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 **foliop, 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_WARN_ON_FOLIO(folio->private, 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 bool ceph_release_folio(struct folio *folio, gfp_t gfp)
167 struct inode *inode = folio->mapping->host;
169 dout("%llx:%llx release_folio idx %lu (%sdirty)\n",
171 folio->index, folio_test_dirty(folio) ? "" : "not ");
173 if (folio_test_private(folio))
176 if (folio_test_fscache(folio)) {
177 if (current_is_kswapd() || !(gfp & __GFP_FS))
179 folio_wait_fscache(folio);
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;
190 unsigned long max_pages = inode->i_sb->s_bdi->ra_pages;
191 loff_t end = rreq->start + rreq->len, new_end;
192 struct ceph_netfs_request_data *priv = rreq->netfs_priv;
193 unsigned long max_len;
197 /* Readahead is disabled by posix_fadvise POSIX_FADV_RANDOM */
198 if (priv->file_ra_disabled)
201 max_pages = priv->file_ra_pages;
205 /* Readahead is disabled */
209 max_len = max_pages << PAGE_SHIFT;
212 * Try to expand the length forward by rounding up it to the next
213 * block, but do not exceed the file size, unless the original
214 * request already exceeds it.
216 new_end = min(round_up(end, lo->stripe_unit), rreq->i_size);
217 if (new_end > end && new_end <= rreq->start + max_len)
218 rreq->len = new_end - rreq->start;
220 /* Try to expand the start downward */
221 div_u64_rem(rreq->start, lo->stripe_unit, &blockoff);
222 if (rreq->len + blockoff <= max_len) {
223 rreq->start -= blockoff;
224 rreq->len += blockoff;
228 static bool ceph_netfs_clamp_length(struct netfs_io_subrequest *subreq)
230 struct inode *inode = subreq->rreq->inode;
231 struct ceph_fs_client *fsc = ceph_inode_to_client(inode);
232 struct ceph_inode_info *ci = ceph_inode(inode);
236 /* Truncate the extent at the end of the current block */
237 ceph_calc_file_object_mapping(&ci->i_layout, subreq->start, subreq->len,
238 &objno, &objoff, &xlen);
239 subreq->len = min(xlen, fsc->mount_options->rsize);
243 static void finish_netfs_read(struct ceph_osd_request *req)
245 struct ceph_fs_client *fsc = ceph_inode_to_client(req->r_inode);
246 struct ceph_osd_data *osd_data = osd_req_op_extent_osd_data(req, 0);
247 struct netfs_io_subrequest *subreq = req->r_priv;
248 struct ceph_osd_req_op *op = &req->r_ops[0];
249 int err = req->r_result;
250 bool sparse = (op->op == CEPH_OSD_OP_SPARSE_READ);
252 ceph_update_read_metrics(&fsc->mdsc->metric, req->r_start_latency,
253 req->r_end_latency, osd_data->length, err);
255 dout("%s: result %d subreq->len=%zu i_size=%lld\n", __func__, req->r_result,
256 subreq->len, i_size_read(req->r_inode));
258 /* no object means success but no data */
259 if (sparse && err >= 0)
260 err = ceph_sparse_ext_map_end(op);
261 else if (err == -ENOENT)
263 else if (err == -EBLOCKLISTED)
264 fsc->blocklisted = true;
266 if (err >= 0 && err < subreq->len)
267 __set_bit(NETFS_SREQ_CLEAR_TAIL, &subreq->flags);
269 netfs_subreq_terminated(subreq, err, false);
273 static bool ceph_netfs_issue_op_inline(struct netfs_io_subrequest *subreq)
275 struct netfs_io_request *rreq = subreq->rreq;
276 struct inode *inode = rreq->inode;
277 struct ceph_mds_reply_info_parsed *rinfo;
278 struct ceph_mds_reply_info_in *iinfo;
279 struct ceph_mds_request *req;
280 struct ceph_mds_client *mdsc = ceph_sb_to_mdsc(inode->i_sb);
281 struct ceph_inode_info *ci = ceph_inode(inode);
282 struct iov_iter iter;
287 __set_bit(NETFS_SREQ_CLEAR_TAIL, &subreq->flags);
288 __clear_bit(NETFS_SREQ_COPY_TO_CACHE, &subreq->flags);
290 if (subreq->start >= inode->i_size)
293 /* We need to fetch the inline data. */
294 mode = ceph_try_to_choose_auth_mds(inode, CEPH_STAT_CAP_INLINE_DATA);
295 req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_GETATTR, mode);
300 req->r_ino1 = ci->i_vino;
301 req->r_args.getattr.mask = cpu_to_le32(CEPH_STAT_CAP_INLINE_DATA);
304 err = ceph_mdsc_do_request(mdsc, NULL, req);
308 rinfo = &req->r_reply_info;
309 iinfo = &rinfo->targeti;
310 if (iinfo->inline_version == CEPH_INLINE_NONE) {
311 /* The data got uninlined */
312 ceph_mdsc_put_request(req);
316 len = min_t(size_t, iinfo->inline_len - subreq->start, subreq->len);
317 iov_iter_xarray(&iter, ITER_DEST, &rreq->mapping->i_pages, subreq->start, len);
318 err = copy_to_iter(iinfo->inline_data + subreq->start, len, &iter);
322 ceph_mdsc_put_request(req);
324 netfs_subreq_terminated(subreq, err, false);
328 static void ceph_netfs_issue_read(struct netfs_io_subrequest *subreq)
330 struct netfs_io_request *rreq = subreq->rreq;
331 struct inode *inode = rreq->inode;
332 struct ceph_inode_info *ci = ceph_inode(inode);
333 struct ceph_fs_client *fsc = ceph_inode_to_client(inode);
334 struct ceph_osd_request *req = NULL;
335 struct ceph_vino vino = ceph_vino(inode);
336 struct iov_iter iter;
338 u64 len = subreq->len;
339 bool sparse = ceph_test_mount_opt(fsc, SPARSEREAD);
341 if (ceph_inode_is_shutdown(inode)) {
346 if (ceph_has_inline_data(ci) && ceph_netfs_issue_op_inline(subreq))
349 req = ceph_osdc_new_request(&fsc->client->osdc, &ci->i_layout, vino, subreq->start, &len,
350 0, 1, sparse ? CEPH_OSD_OP_SPARSE_READ : CEPH_OSD_OP_READ,
351 CEPH_OSD_FLAG_READ | fsc->client->osdc.client->options->read_from_replica,
352 NULL, ci->i_truncate_seq, ci->i_truncate_size, false);
360 err = ceph_alloc_sparse_ext_map(&req->r_ops[0]);
365 dout("%s: pos=%llu orig_len=%zu len=%llu\n", __func__, subreq->start, subreq->len, len);
366 iov_iter_xarray(&iter, ITER_DEST, &rreq->mapping->i_pages, subreq->start, len);
367 osd_req_op_extent_osd_iter(req, 0, &iter);
368 req->r_callback = finish_netfs_read;
369 req->r_priv = subreq;
370 req->r_inode = inode;
373 ceph_osdc_start_request(req->r_osdc, req);
375 ceph_osdc_put_request(req);
377 netfs_subreq_terminated(subreq, err, false);
378 dout("%s: result %d\n", __func__, err);
381 static int ceph_init_request(struct netfs_io_request *rreq, struct file *file)
383 struct inode *inode = rreq->inode;
384 int got = 0, want = CEPH_CAP_FILE_CACHE;
385 struct ceph_netfs_request_data *priv;
388 if (rreq->origin != NETFS_READAHEAD)
391 priv = kzalloc(sizeof(*priv), GFP_NOFS);
396 struct ceph_rw_context *rw_ctx;
397 struct ceph_file_info *fi = file->private_data;
399 priv->file_ra_pages = file->f_ra.ra_pages;
400 priv->file_ra_disabled = file->f_mode & FMODE_RANDOM;
402 rw_ctx = ceph_find_rw_context(fi);
404 rreq->netfs_priv = priv;
410 * readahead callers do not necessarily hold Fcb caps
411 * (e.g. fadvise, madvise).
413 ret = ceph_try_get_caps(inode, CEPH_CAP_FILE_RD, want, true, &got);
415 dout("start_read %p, error getting cap\n", inode);
420 dout("start_read %p, no cache cap\n", inode);
430 rreq->netfs_priv = priv;
439 static void ceph_netfs_free_request(struct netfs_io_request *rreq)
441 struct ceph_netfs_request_data *priv = rreq->netfs_priv;
447 ceph_put_cap_refs(ceph_inode(rreq->inode), priv->caps);
449 rreq->netfs_priv = NULL;
452 const struct netfs_request_ops ceph_netfs_ops = {
453 .init_request = ceph_init_request,
454 .free_request = ceph_netfs_free_request,
455 .begin_cache_operation = ceph_begin_cache_operation,
456 .issue_read = ceph_netfs_issue_read,
457 .expand_readahead = ceph_netfs_expand_readahead,
458 .clamp_length = ceph_netfs_clamp_length,
459 .check_write_begin = ceph_netfs_check_write_begin,
462 #ifdef CONFIG_CEPH_FSCACHE
463 static void ceph_set_page_fscache(struct page *page)
465 set_page_fscache(page);
468 static void ceph_fscache_write_terminated(void *priv, ssize_t error, bool was_async)
470 struct inode *inode = priv;
472 if (IS_ERR_VALUE(error) && error != -ENOBUFS)
473 ceph_fscache_invalidate(inode, false);
476 static void ceph_fscache_write_to_cache(struct inode *inode, u64 off, u64 len, bool caching)
478 struct ceph_inode_info *ci = ceph_inode(inode);
479 struct fscache_cookie *cookie = ceph_fscache_cookie(ci);
481 fscache_write_to_cache(cookie, inode->i_mapping, off, len, i_size_read(inode),
482 ceph_fscache_write_terminated, inode, caching);
485 static inline void ceph_set_page_fscache(struct page *page)
489 static inline void ceph_fscache_write_to_cache(struct inode *inode, u64 off, u64 len, bool caching)
492 #endif /* CONFIG_CEPH_FSCACHE */
494 struct ceph_writeback_ctl
504 * Get ref for the oldest snapc for an inode with dirty data... that is, the
505 * only snap context we are allowed to write back.
507 static struct ceph_snap_context *
508 get_oldest_context(struct inode *inode, struct ceph_writeback_ctl *ctl,
509 struct ceph_snap_context *page_snapc)
511 struct ceph_inode_info *ci = ceph_inode(inode);
512 struct ceph_snap_context *snapc = NULL;
513 struct ceph_cap_snap *capsnap = NULL;
515 spin_lock(&ci->i_ceph_lock);
516 list_for_each_entry(capsnap, &ci->i_cap_snaps, ci_item) {
517 dout(" cap_snap %p snapc %p has %d dirty pages\n", capsnap,
518 capsnap->context, capsnap->dirty_pages);
519 if (!capsnap->dirty_pages)
522 /* get i_size, truncate_{seq,size} for page_snapc? */
523 if (snapc && capsnap->context != page_snapc)
527 if (capsnap->writing) {
528 ctl->i_size = i_size_read(inode);
529 ctl->size_stable = false;
531 ctl->i_size = capsnap->size;
532 ctl->size_stable = true;
534 ctl->truncate_size = capsnap->truncate_size;
535 ctl->truncate_seq = capsnap->truncate_seq;
536 ctl->head_snapc = false;
542 snapc = ceph_get_snap_context(capsnap->context);
544 page_snapc == snapc ||
545 page_snapc->seq > snapc->seq)
548 if (!snapc && ci->i_wrbuffer_ref_head) {
549 snapc = ceph_get_snap_context(ci->i_head_snapc);
550 dout(" head snapc %p has %d dirty pages\n",
551 snapc, ci->i_wrbuffer_ref_head);
553 ctl->i_size = i_size_read(inode);
554 ctl->truncate_size = ci->i_truncate_size;
555 ctl->truncate_seq = ci->i_truncate_seq;
556 ctl->size_stable = false;
557 ctl->head_snapc = true;
560 spin_unlock(&ci->i_ceph_lock);
564 static u64 get_writepages_data_length(struct inode *inode,
565 struct page *page, u64 start)
567 struct ceph_inode_info *ci = ceph_inode(inode);
568 struct ceph_snap_context *snapc = page_snap_context(page);
569 struct ceph_cap_snap *capsnap = NULL;
570 u64 end = i_size_read(inode);
572 if (snapc != ci->i_head_snapc) {
574 spin_lock(&ci->i_ceph_lock);
575 list_for_each_entry(capsnap, &ci->i_cap_snaps, ci_item) {
576 if (capsnap->context == snapc) {
577 if (!capsnap->writing)
583 spin_unlock(&ci->i_ceph_lock);
586 if (end > page_offset(page) + thp_size(page))
587 end = page_offset(page) + thp_size(page);
588 return end > start ? end - start : 0;
592 * Write a single page, but leave the page locked.
594 * If we get a write error, mark the mapping for error, but still adjust the
595 * dirty page accounting (i.e., page is no longer dirty).
597 static int writepage_nounlock(struct page *page, struct writeback_control *wbc)
599 struct folio *folio = page_folio(page);
600 struct inode *inode = page->mapping->host;
601 struct ceph_inode_info *ci = ceph_inode(inode);
602 struct ceph_fs_client *fsc = ceph_inode_to_client(inode);
603 struct ceph_snap_context *snapc, *oldest;
604 loff_t page_off = page_offset(page);
606 loff_t len = thp_size(page);
607 struct ceph_writeback_ctl ceph_wbc;
608 struct ceph_osd_client *osdc = &fsc->client->osdc;
609 struct ceph_osd_request *req;
610 bool caching = ceph_is_cache_enabled(inode);
612 dout("writepage %p idx %lu\n", page, page->index);
614 if (ceph_inode_is_shutdown(inode))
617 /* verify this is a writeable snap context */
618 snapc = page_snap_context(page);
620 dout("writepage %p page %p not dirty?\n", inode, page);
623 oldest = get_oldest_context(inode, &ceph_wbc, snapc);
624 if (snapc->seq > oldest->seq) {
625 dout("writepage %p page %p snapc %p not writeable - noop\n",
627 /* we should only noop if called by kswapd */
628 WARN_ON(!(current->flags & PF_MEMALLOC));
629 ceph_put_snap_context(oldest);
630 redirty_page_for_writepage(wbc, page);
633 ceph_put_snap_context(oldest);
635 /* is this a partial page at end of file? */
636 if (page_off >= ceph_wbc.i_size) {
637 dout("folio at %lu beyond eof %llu\n", folio->index,
639 folio_invalidate(folio, 0, folio_size(folio));
643 if (ceph_wbc.i_size < page_off + len)
644 len = ceph_wbc.i_size - page_off;
646 dout("writepage %p page %p index %lu on %llu~%llu snapc %p seq %lld\n",
647 inode, page, page->index, page_off, len, snapc, snapc->seq);
649 if (atomic_long_inc_return(&fsc->writeback_count) >
650 CONGESTION_ON_THRESH(fsc->mount_options->congestion_kb))
651 fsc->write_congested = true;
653 req = ceph_osdc_new_request(osdc, &ci->i_layout, ceph_vino(inode), page_off, &len, 0, 1,
654 CEPH_OSD_OP_WRITE, CEPH_OSD_FLAG_WRITE, snapc,
655 ceph_wbc.truncate_seq, ceph_wbc.truncate_size,
658 redirty_page_for_writepage(wbc, page);
662 set_page_writeback(page);
664 ceph_set_page_fscache(page);
665 ceph_fscache_write_to_cache(inode, page_off, len, caching);
667 /* it may be a short write due to an object boundary */
668 WARN_ON_ONCE(len > thp_size(page));
669 osd_req_op_extent_osd_data_pages(req, 0, &page, len, 0, false, false);
670 dout("writepage %llu~%llu (%llu bytes)\n", page_off, len, len);
672 req->r_mtime = inode->i_mtime;
673 ceph_osdc_start_request(osdc, req);
674 err = ceph_osdc_wait_request(osdc, req);
676 ceph_update_write_metrics(&fsc->mdsc->metric, req->r_start_latency,
677 req->r_end_latency, len, err);
679 ceph_osdc_put_request(req);
684 struct writeback_control tmp_wbc;
687 if (err == -ERESTARTSYS) {
688 /* killed by SIGKILL */
689 dout("writepage interrupted page %p\n", page);
690 redirty_page_for_writepage(wbc, page);
691 end_page_writeback(page);
694 if (err == -EBLOCKLISTED)
695 fsc->blocklisted = true;
696 dout("writepage setting page/mapping error %d %p\n",
698 mapping_set_error(&inode->i_data, err);
699 wbc->pages_skipped++;
701 dout("writepage cleaned page %p\n", page);
702 err = 0; /* vfs expects us to return 0 */
704 oldest = detach_page_private(page);
705 WARN_ON_ONCE(oldest != snapc);
706 end_page_writeback(page);
707 ceph_put_wrbuffer_cap_refs(ci, 1, snapc);
708 ceph_put_snap_context(snapc); /* page's reference */
710 if (atomic_long_dec_return(&fsc->writeback_count) <
711 CONGESTION_OFF_THRESH(fsc->mount_options->congestion_kb))
712 fsc->write_congested = false;
717 static int ceph_writepage(struct page *page, struct writeback_control *wbc)
720 struct inode *inode = page->mapping->host;
724 if (wbc->sync_mode == WB_SYNC_NONE &&
725 ceph_inode_to_client(inode)->write_congested)
726 return AOP_WRITEPAGE_ACTIVATE;
728 wait_on_page_fscache(page);
730 err = writepage_nounlock(page, wbc);
731 if (err == -ERESTARTSYS) {
732 /* direct memory reclaimer was killed by SIGKILL. return 0
733 * to prevent caller from setting mapping/page error */
742 * async writeback completion handler.
744 * If we get an error, set the mapping error bit, but not the individual
747 static void writepages_finish(struct ceph_osd_request *req)
749 struct inode *inode = req->r_inode;
750 struct ceph_inode_info *ci = ceph_inode(inode);
751 struct ceph_osd_data *osd_data;
753 int num_pages, total_pages = 0;
755 int rc = req->r_result;
756 struct ceph_snap_context *snapc = req->r_snapc;
757 struct address_space *mapping = inode->i_mapping;
758 struct ceph_fs_client *fsc = ceph_inode_to_client(inode);
759 unsigned int len = 0;
762 dout("writepages_finish %p rc %d\n", inode, rc);
764 mapping_set_error(mapping, rc);
765 ceph_set_error_write(ci);
766 if (rc == -EBLOCKLISTED)
767 fsc->blocklisted = true;
769 ceph_clear_error_write(ci);
773 * We lost the cache cap, need to truncate the page before
774 * it is unlocked, otherwise we'd truncate it later in the
775 * page truncation thread, possibly losing some data that
778 remove_page = !(ceph_caps_issued(ci) &
779 (CEPH_CAP_FILE_CACHE|CEPH_CAP_FILE_LAZYIO));
781 /* clean all pages */
782 for (i = 0; i < req->r_num_ops; i++) {
783 if (req->r_ops[i].op != CEPH_OSD_OP_WRITE) {
784 pr_warn("%s incorrect op %d req %p index %d tid %llu\n",
785 __func__, req->r_ops[i].op, req, i, req->r_tid);
789 osd_data = osd_req_op_extent_osd_data(req, i);
790 BUG_ON(osd_data->type != CEPH_OSD_DATA_TYPE_PAGES);
791 len += osd_data->length;
792 num_pages = calc_pages_for((u64)osd_data->alignment,
793 (u64)osd_data->length);
794 total_pages += num_pages;
795 for (j = 0; j < num_pages; j++) {
796 page = osd_data->pages[j];
798 WARN_ON(!PageUptodate(page));
800 if (atomic_long_dec_return(&fsc->writeback_count) <
801 CONGESTION_OFF_THRESH(
802 fsc->mount_options->congestion_kb))
803 fsc->write_congested = false;
805 ceph_put_snap_context(detach_page_private(page));
806 end_page_writeback(page);
807 dout("unlocking %p\n", page);
810 generic_error_remove_page(inode->i_mapping,
815 dout("writepages_finish %p wrote %llu bytes cleaned %d pages\n",
816 inode, osd_data->length, rc >= 0 ? num_pages : 0);
818 release_pages(osd_data->pages, num_pages);
821 ceph_update_write_metrics(&fsc->mdsc->metric, req->r_start_latency,
822 req->r_end_latency, len, rc);
824 ceph_put_wrbuffer_cap_refs(ci, total_pages, snapc);
826 osd_data = osd_req_op_extent_osd_data(req, 0);
827 if (osd_data->pages_from_pool)
828 mempool_free(osd_data->pages, ceph_wb_pagevec_pool);
830 kfree(osd_data->pages);
831 ceph_osdc_put_request(req);
835 * initiate async writeback
837 static int ceph_writepages_start(struct address_space *mapping,
838 struct writeback_control *wbc)
840 struct inode *inode = mapping->host;
841 struct ceph_inode_info *ci = ceph_inode(inode);
842 struct ceph_fs_client *fsc = ceph_inode_to_client(inode);
843 struct ceph_vino vino = ceph_vino(inode);
844 pgoff_t index, start_index, end = -1;
845 struct ceph_snap_context *snapc = NULL, *last_snapc = NULL, *pgsnapc;
846 struct folio_batch fbatch;
848 unsigned int wsize = i_blocksize(inode);
849 struct ceph_osd_request *req = NULL;
850 struct ceph_writeback_ctl ceph_wbc;
851 bool should_loop, range_whole = false;
853 bool caching = ceph_is_cache_enabled(inode);
856 if (wbc->sync_mode == WB_SYNC_NONE &&
857 fsc->write_congested)
860 dout("writepages_start %p (mode=%s)\n", inode,
861 wbc->sync_mode == WB_SYNC_NONE ? "NONE" :
862 (wbc->sync_mode == WB_SYNC_ALL ? "ALL" : "HOLD"));
864 if (ceph_inode_is_shutdown(inode)) {
865 if (ci->i_wrbuffer_ref > 0) {
867 "writepage_start %p %lld forced umount\n",
868 inode, ceph_ino(inode));
870 mapping_set_error(mapping, -EIO);
871 return -EIO; /* we're in a forced umount, don't write! */
873 if (fsc->mount_options->wsize < wsize)
874 wsize = fsc->mount_options->wsize;
876 folio_batch_init(&fbatch);
878 start_index = wbc->range_cyclic ? mapping->writeback_index : 0;
881 if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages) {
882 tag = PAGECACHE_TAG_TOWRITE;
884 tag = PAGECACHE_TAG_DIRTY;
887 /* find oldest snap context with dirty data */
888 snapc = get_oldest_context(inode, &ceph_wbc, NULL);
890 /* hmm, why does writepages get called when there
892 dout(" no snap context with dirty data?\n");
895 dout(" oldest snapc is %p seq %lld (%d snaps)\n",
896 snapc, snapc->seq, snapc->num_snaps);
899 if (ceph_wbc.head_snapc && snapc != last_snapc) {
900 /* where to start/end? */
901 if (wbc->range_cyclic) {
906 dout(" cyclic, start at %lu\n", index);
908 index = wbc->range_start >> PAGE_SHIFT;
909 end = wbc->range_end >> PAGE_SHIFT;
910 if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX)
912 dout(" not cyclic, %lu to %lu\n", index, end);
914 } else if (!ceph_wbc.head_snapc) {
915 /* Do not respect wbc->range_{start,end}. Dirty pages
916 * in that range can be associated with newer snapc.
917 * They are not writeable until we write all dirty pages
918 * associated with 'snapc' get written */
921 dout(" non-head snapc, range whole\n");
924 if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages)
925 tag_pages_for_writeback(mapping, index, end);
927 ceph_put_snap_context(last_snapc);
930 while (!done && index <= end) {
931 int num_ops = 0, op_idx;
932 unsigned i, nr_folios, max_pages, locked_pages = 0;
933 struct page **pages = NULL, **data_pages;
935 pgoff_t strip_unit_end = 0;
936 u64 offset = 0, len = 0;
937 bool from_pool = false;
939 max_pages = wsize >> PAGE_SHIFT;
942 nr_folios = filemap_get_folios_tag(mapping, &index,
944 dout("pagevec_lookup_range_tag got %d\n", nr_folios);
945 if (!nr_folios && !locked_pages)
947 for (i = 0; i < nr_folios && locked_pages < max_pages; i++) {
948 page = &fbatch.folios[i]->page;
949 dout("? %p idx %lu\n", page, page->index);
950 if (locked_pages == 0)
951 lock_page(page); /* first page */
952 else if (!trylock_page(page))
955 /* only dirty pages, or our accounting breaks */
956 if (unlikely(!PageDirty(page)) ||
957 unlikely(page->mapping != mapping)) {
958 dout("!dirty or !mapping %p\n", page);
962 /* only if matching snap context */
963 pgsnapc = page_snap_context(page);
964 if (pgsnapc != snapc) {
965 dout("page snapc %p %lld != oldest %p %lld\n",
966 pgsnapc, pgsnapc->seq, snapc, snapc->seq);
968 !ceph_wbc.head_snapc &&
969 wbc->sync_mode != WB_SYNC_NONE)
974 if (page_offset(page) >= ceph_wbc.i_size) {
975 struct folio *folio = page_folio(page);
977 dout("folio at %lu beyond eof %llu\n",
978 folio->index, ceph_wbc.i_size);
979 if ((ceph_wbc.size_stable ||
980 folio_pos(folio) >= i_size_read(inode)) &&
981 folio_clear_dirty_for_io(folio))
982 folio_invalidate(folio, 0,
987 if (strip_unit_end && (page->index > strip_unit_end)) {
988 dout("end of strip unit %p\n", page);
992 if (PageWriteback(page) || PageFsCache(page)) {
993 if (wbc->sync_mode == WB_SYNC_NONE) {
994 dout("%p under writeback\n", page);
998 dout("waiting on writeback %p\n", page);
999 wait_on_page_writeback(page);
1000 wait_on_page_fscache(page);
1003 if (!clear_page_dirty_for_io(page)) {
1004 dout("%p !clear_page_dirty_for_io\n", page);
1010 * We have something to write. If this is
1011 * the first locked page this time through,
1012 * calculate max possinle write size and
1013 * allocate a page array
1015 if (locked_pages == 0) {
1020 /* prepare async write request */
1021 offset = (u64)page_offset(page);
1022 ceph_calc_file_object_mapping(&ci->i_layout,
1029 strip_unit_end = page->index +
1030 ((len - 1) >> PAGE_SHIFT);
1033 max_pages = calc_pages_for(0, (u64)len);
1034 pages = kmalloc_array(max_pages,
1039 pages = mempool_alloc(ceph_wb_pagevec_pool, GFP_NOFS);
1044 } else if (page->index !=
1045 (offset + len) >> PAGE_SHIFT) {
1046 if (num_ops >= (from_pool ? CEPH_OSD_SLAB_OPS :
1047 CEPH_OSD_MAX_OPS)) {
1048 redirty_page_for_writepage(wbc, page);
1054 offset = (u64)page_offset(page);
1058 /* note position of first page in fbatch */
1059 dout("%p will write page %p idx %lu\n",
1060 inode, page, page->index);
1062 if (atomic_long_inc_return(&fsc->writeback_count) >
1063 CONGESTION_ON_THRESH(
1064 fsc->mount_options->congestion_kb))
1065 fsc->write_congested = true;
1067 pages[locked_pages++] = page;
1068 fbatch.folios[i] = NULL;
1070 len += thp_size(page);
1073 /* did we get anything? */
1075 goto release_folios;
1078 /* shift unused page to beginning of fbatch */
1079 for (j = 0; j < nr_folios; j++) {
1080 if (!fbatch.folios[j])
1083 fbatch.folios[n] = fbatch.folios[j];
1088 if (nr_folios && i == nr_folios &&
1089 locked_pages < max_pages) {
1090 dout("reached end fbatch, trying for more\n");
1091 folio_batch_release(&fbatch);
1092 goto get_more_pages;
1097 offset = page_offset(pages[0]);
1100 req = ceph_osdc_new_request(&fsc->client->osdc,
1101 &ci->i_layout, vino,
1102 offset, &len, 0, num_ops,
1103 CEPH_OSD_OP_WRITE, CEPH_OSD_FLAG_WRITE,
1104 snapc, ceph_wbc.truncate_seq,
1105 ceph_wbc.truncate_size, false);
1107 req = ceph_osdc_new_request(&fsc->client->osdc,
1108 &ci->i_layout, vino,
1113 CEPH_OSD_FLAG_WRITE,
1114 snapc, ceph_wbc.truncate_seq,
1115 ceph_wbc.truncate_size, true);
1116 BUG_ON(IS_ERR(req));
1118 BUG_ON(len < page_offset(pages[locked_pages - 1]) +
1119 thp_size(page) - offset);
1121 req->r_callback = writepages_finish;
1122 req->r_inode = inode;
1124 /* Format the osd request message and submit the write */
1128 for (i = 0; i < locked_pages; i++) {
1129 u64 cur_offset = page_offset(pages[i]);
1131 * Discontinuity in page range? Ceph can handle that by just passing
1132 * multiple extents in the write op.
1134 if (offset + len != cur_offset) {
1135 /* If it's full, stop here */
1136 if (op_idx + 1 == req->r_num_ops)
1139 /* Kick off an fscache write with what we have so far. */
1140 ceph_fscache_write_to_cache(inode, offset, len, caching);
1142 /* Start a new extent */
1143 osd_req_op_extent_dup_last(req, op_idx,
1144 cur_offset - offset);
1145 dout("writepages got pages at %llu~%llu\n",
1147 osd_req_op_extent_osd_data_pages(req, op_idx,
1150 osd_req_op_extent_update(req, op_idx, len);
1153 offset = cur_offset;
1154 data_pages = pages + i;
1158 set_page_writeback(pages[i]);
1160 ceph_set_page_fscache(pages[i]);
1161 len += thp_size(page);
1163 ceph_fscache_write_to_cache(inode, offset, len, caching);
1165 if (ceph_wbc.size_stable) {
1166 len = min(len, ceph_wbc.i_size - offset);
1167 } else if (i == locked_pages) {
1168 /* writepages_finish() clears writeback pages
1169 * according to the data length, so make sure
1170 * data length covers all locked pages */
1171 u64 min_len = len + 1 - thp_size(page);
1172 len = get_writepages_data_length(inode, pages[i - 1],
1174 len = max(len, min_len);
1176 dout("writepages got pages at %llu~%llu\n", offset, len);
1178 osd_req_op_extent_osd_data_pages(req, op_idx, data_pages, len,
1179 0, from_pool, false);
1180 osd_req_op_extent_update(req, op_idx, len);
1182 BUG_ON(op_idx + 1 != req->r_num_ops);
1185 if (i < locked_pages) {
1186 BUG_ON(num_ops <= req->r_num_ops);
1187 num_ops -= req->r_num_ops;
1190 /* allocate new pages array for next request */
1192 pages = kmalloc_array(locked_pages, sizeof(*pages),
1196 pages = mempool_alloc(ceph_wb_pagevec_pool, GFP_NOFS);
1199 memcpy(pages, data_pages + i,
1200 locked_pages * sizeof(*pages));
1201 memset(data_pages + i, 0,
1202 locked_pages * sizeof(*pages));
1204 BUG_ON(num_ops != req->r_num_ops);
1205 index = pages[i - 1]->index + 1;
1206 /* request message now owns the pages array */
1210 req->r_mtime = inode->i_mtime;
1211 ceph_osdc_start_request(&fsc->client->osdc, req);
1214 wbc->nr_to_write -= i;
1219 * We stop writing back only if we are not doing
1220 * integrity sync. In case of integrity sync we have to
1221 * keep going until we have written all the pages
1222 * we tagged for writeback prior to entering this loop.
1224 if (wbc->nr_to_write <= 0 && wbc->sync_mode == WB_SYNC_NONE)
1228 dout("folio_batch release on %d folios (%p)\n", (int)fbatch.nr,
1229 fbatch.nr ? fbatch.folios[0] : NULL);
1230 folio_batch_release(&fbatch);
1233 if (should_loop && !done) {
1234 /* more to do; loop back to beginning of file */
1235 dout("writepages looping back to beginning of file\n");
1236 end = start_index - 1; /* OK even when start_index == 0 */
1238 /* to write dirty pages associated with next snapc,
1239 * we need to wait until current writes complete */
1240 if (wbc->sync_mode != WB_SYNC_NONE &&
1241 start_index == 0 && /* all dirty pages were checked */
1242 !ceph_wbc.head_snapc) {
1246 while ((index <= end) &&
1247 (nr = filemap_get_folios_tag(mapping, &index,
1249 PAGECACHE_TAG_WRITEBACK,
1251 for (i = 0; i < nr; i++) {
1252 page = &fbatch.folios[i]->page;
1253 if (page_snap_context(page) != snapc)
1255 wait_on_page_writeback(page);
1257 folio_batch_release(&fbatch);
1267 if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0))
1268 mapping->writeback_index = index;
1271 ceph_osdc_put_request(req);
1272 ceph_put_snap_context(last_snapc);
1273 dout("writepages dend - startone, rc = %d\n", rc);
1280 * See if a given @snapc is either writeable, or already written.
1282 static int context_is_writeable_or_written(struct inode *inode,
1283 struct ceph_snap_context *snapc)
1285 struct ceph_snap_context *oldest = get_oldest_context(inode, NULL, NULL);
1286 int ret = !oldest || snapc->seq <= oldest->seq;
1288 ceph_put_snap_context(oldest);
1293 * ceph_find_incompatible - find an incompatible context and return it
1294 * @page: page being dirtied
1296 * We are only allowed to write into/dirty a page if the page is
1297 * clean, or already dirty within the same snap context. Returns a
1298 * conflicting context if there is one, NULL if there isn't, or a
1299 * negative error code on other errors.
1301 * Must be called with page lock held.
1303 static struct ceph_snap_context *
1304 ceph_find_incompatible(struct page *page)
1306 struct inode *inode = page->mapping->host;
1307 struct ceph_inode_info *ci = ceph_inode(inode);
1309 if (ceph_inode_is_shutdown(inode)) {
1310 dout(" page %p %llx:%llx is shutdown\n", page,
1312 return ERR_PTR(-ESTALE);
1316 struct ceph_snap_context *snapc, *oldest;
1318 wait_on_page_writeback(page);
1320 snapc = page_snap_context(page);
1321 if (!snapc || snapc == ci->i_head_snapc)
1325 * this page is already dirty in another (older) snap
1326 * context! is it writeable now?
1328 oldest = get_oldest_context(inode, NULL, NULL);
1329 if (snapc->seq > oldest->seq) {
1330 /* not writeable -- return it for the caller to deal with */
1331 ceph_put_snap_context(oldest);
1332 dout(" page %p snapc %p not current or oldest\n", page, snapc);
1333 return ceph_get_snap_context(snapc);
1335 ceph_put_snap_context(oldest);
1337 /* yay, writeable, do it now (without dropping page lock) */
1338 dout(" page %p snapc %p not current, but oldest\n", page, snapc);
1339 if (clear_page_dirty_for_io(page)) {
1340 int r = writepage_nounlock(page, NULL);
1348 static int ceph_netfs_check_write_begin(struct file *file, loff_t pos, unsigned int len,
1349 struct folio **foliop, void **_fsdata)
1351 struct inode *inode = file_inode(file);
1352 struct ceph_inode_info *ci = ceph_inode(inode);
1353 struct ceph_snap_context *snapc;
1355 snapc = ceph_find_incompatible(folio_page(*foliop, 0));
1359 folio_unlock(*foliop);
1363 return PTR_ERR(snapc);
1365 ceph_queue_writeback(inode);
1366 r = wait_event_killable(ci->i_cap_wq,
1367 context_is_writeable_or_written(inode, snapc));
1368 ceph_put_snap_context(snapc);
1369 return r == 0 ? -EAGAIN : r;
1375 * We are only allowed to write into/dirty the page if the page is
1376 * clean, or already dirty within the same snap context.
1378 static int ceph_write_begin(struct file *file, struct address_space *mapping,
1379 loff_t pos, unsigned len,
1380 struct page **pagep, void **fsdata)
1382 struct inode *inode = file_inode(file);
1383 struct ceph_inode_info *ci = ceph_inode(inode);
1384 struct folio *folio = NULL;
1387 r = netfs_write_begin(&ci->netfs, file, inode->i_mapping, pos, len, &folio, NULL);
1391 folio_wait_fscache(folio);
1392 WARN_ON_ONCE(!folio_test_locked(folio));
1393 *pagep = &folio->page;
1398 * we don't do anything in here that simple_write_end doesn't do
1399 * except adjust dirty page accounting
1401 static int ceph_write_end(struct file *file, struct address_space *mapping,
1402 loff_t pos, unsigned len, unsigned copied,
1403 struct page *subpage, void *fsdata)
1405 struct folio *folio = page_folio(subpage);
1406 struct inode *inode = file_inode(file);
1407 bool check_cap = false;
1409 dout("write_end file %p inode %p folio %p %d~%d (%d)\n", file,
1410 inode, folio, (int)pos, (int)copied, (int)len);
1412 if (!folio_test_uptodate(folio)) {
1413 /* just return that nothing was copied on a short copy */
1418 folio_mark_uptodate(folio);
1421 /* did file size increase? */
1422 if (pos+copied > i_size_read(inode))
1423 check_cap = ceph_inode_set_size(inode, pos+copied);
1425 folio_mark_dirty(folio);
1428 folio_unlock(folio);
1432 ceph_check_caps(ceph_inode(inode), CHECK_CAPS_AUTHONLY);
1437 const struct address_space_operations ceph_aops = {
1438 .read_folio = netfs_read_folio,
1439 .readahead = netfs_readahead,
1440 .writepage = ceph_writepage,
1441 .writepages = ceph_writepages_start,
1442 .write_begin = ceph_write_begin,
1443 .write_end = ceph_write_end,
1444 .dirty_folio = ceph_dirty_folio,
1445 .invalidate_folio = ceph_invalidate_folio,
1446 .release_folio = ceph_release_folio,
1447 .direct_IO = noop_direct_IO,
1450 static void ceph_block_sigs(sigset_t *oldset)
1453 siginitsetinv(&mask, sigmask(SIGKILL));
1454 sigprocmask(SIG_BLOCK, &mask, oldset);
1457 static void ceph_restore_sigs(sigset_t *oldset)
1459 sigprocmask(SIG_SETMASK, oldset, NULL);
1465 static vm_fault_t ceph_filemap_fault(struct vm_fault *vmf)
1467 struct vm_area_struct *vma = vmf->vma;
1468 struct inode *inode = file_inode(vma->vm_file);
1469 struct ceph_inode_info *ci = ceph_inode(inode);
1470 struct ceph_file_info *fi = vma->vm_file->private_data;
1471 loff_t off = (loff_t)vmf->pgoff << PAGE_SHIFT;
1474 vm_fault_t ret = VM_FAULT_SIGBUS;
1476 if (ceph_inode_is_shutdown(inode))
1479 ceph_block_sigs(&oldset);
1481 dout("filemap_fault %p %llx.%llx %llu trying to get caps\n",
1482 inode, ceph_vinop(inode), off);
1483 if (fi->fmode & CEPH_FILE_MODE_LAZY)
1484 want = CEPH_CAP_FILE_CACHE | CEPH_CAP_FILE_LAZYIO;
1486 want = CEPH_CAP_FILE_CACHE;
1489 err = ceph_get_caps(vma->vm_file, CEPH_CAP_FILE_RD, want, -1, &got);
1493 dout("filemap_fault %p %llu got cap refs on %s\n",
1494 inode, off, ceph_cap_string(got));
1496 if ((got & (CEPH_CAP_FILE_CACHE | CEPH_CAP_FILE_LAZYIO)) ||
1497 !ceph_has_inline_data(ci)) {
1498 CEPH_DEFINE_RW_CONTEXT(rw_ctx, got);
1499 ceph_add_rw_context(fi, &rw_ctx);
1500 ret = filemap_fault(vmf);
1501 ceph_del_rw_context(fi, &rw_ctx);
1502 dout("filemap_fault %p %llu drop cap refs %s ret %x\n",
1503 inode, off, ceph_cap_string(got), ret);
1507 ceph_put_cap_refs(ci, got);
1512 /* read inline data */
1513 if (off >= PAGE_SIZE) {
1514 /* does not support inline data > PAGE_SIZE */
1515 ret = VM_FAULT_SIGBUS;
1517 struct address_space *mapping = inode->i_mapping;
1520 filemap_invalidate_lock_shared(mapping);
1521 page = find_or_create_page(mapping, 0,
1522 mapping_gfp_constraint(mapping, ~__GFP_FS));
1527 err = __ceph_do_getattr(inode, page,
1528 CEPH_STAT_CAP_INLINE_DATA, true);
1529 if (err < 0 || off >= i_size_read(inode)) {
1532 ret = vmf_error(err);
1535 if (err < PAGE_SIZE)
1536 zero_user_segment(page, err, PAGE_SIZE);
1538 flush_dcache_page(page);
1539 SetPageUptodate(page);
1541 ret = VM_FAULT_MAJOR | VM_FAULT_LOCKED;
1543 filemap_invalidate_unlock_shared(mapping);
1544 dout("filemap_fault %p %llu read inline data ret %x\n",
1548 ceph_restore_sigs(&oldset);
1550 ret = vmf_error(err);
1555 static vm_fault_t ceph_page_mkwrite(struct vm_fault *vmf)
1557 struct vm_area_struct *vma = vmf->vma;
1558 struct inode *inode = file_inode(vma->vm_file);
1559 struct ceph_inode_info *ci = ceph_inode(inode);
1560 struct ceph_file_info *fi = vma->vm_file->private_data;
1561 struct ceph_cap_flush *prealloc_cf;
1562 struct page *page = vmf->page;
1563 loff_t off = page_offset(page);
1564 loff_t size = i_size_read(inode);
1568 vm_fault_t ret = VM_FAULT_SIGBUS;
1570 if (ceph_inode_is_shutdown(inode))
1573 prealloc_cf = ceph_alloc_cap_flush();
1575 return VM_FAULT_OOM;
1577 sb_start_pagefault(inode->i_sb);
1578 ceph_block_sigs(&oldset);
1580 if (off + thp_size(page) <= size)
1581 len = thp_size(page);
1583 len = offset_in_thp(page, size);
1585 dout("page_mkwrite %p %llx.%llx %llu~%zd getting caps i_size %llu\n",
1586 inode, ceph_vinop(inode), off, len, size);
1587 if (fi->fmode & CEPH_FILE_MODE_LAZY)
1588 want = CEPH_CAP_FILE_BUFFER | CEPH_CAP_FILE_LAZYIO;
1590 want = CEPH_CAP_FILE_BUFFER;
1593 err = ceph_get_caps(vma->vm_file, CEPH_CAP_FILE_WR, want, off + len, &got);
1597 dout("page_mkwrite %p %llu~%zd got cap refs on %s\n",
1598 inode, off, len, ceph_cap_string(got));
1600 /* Update time before taking page lock */
1601 file_update_time(vma->vm_file);
1602 inode_inc_iversion_raw(inode);
1605 struct ceph_snap_context *snapc;
1609 if (page_mkwrite_check_truncate(page, inode) < 0) {
1611 ret = VM_FAULT_NOPAGE;
1615 snapc = ceph_find_incompatible(page);
1617 /* success. we'll keep the page locked. */
1618 set_page_dirty(page);
1619 ret = VM_FAULT_LOCKED;
1625 if (IS_ERR(snapc)) {
1626 ret = VM_FAULT_SIGBUS;
1630 ceph_queue_writeback(inode);
1631 err = wait_event_killable(ci->i_cap_wq,
1632 context_is_writeable_or_written(inode, snapc));
1633 ceph_put_snap_context(snapc);
1636 if (ret == VM_FAULT_LOCKED) {
1638 spin_lock(&ci->i_ceph_lock);
1639 dirty = __ceph_mark_dirty_caps(ci, CEPH_CAP_FILE_WR,
1641 spin_unlock(&ci->i_ceph_lock);
1643 __mark_inode_dirty(inode, dirty);
1646 dout("page_mkwrite %p %llu~%zd dropping cap refs on %s ret %x\n",
1647 inode, off, len, ceph_cap_string(got), ret);
1648 ceph_put_cap_refs_async(ci, got);
1650 ceph_restore_sigs(&oldset);
1651 sb_end_pagefault(inode->i_sb);
1652 ceph_free_cap_flush(prealloc_cf);
1654 ret = vmf_error(err);
1658 void ceph_fill_inline_data(struct inode *inode, struct page *locked_page,
1659 char *data, size_t len)
1661 struct address_space *mapping = inode->i_mapping;
1667 if (i_size_read(inode) == 0)
1669 page = find_or_create_page(mapping, 0,
1670 mapping_gfp_constraint(mapping,
1674 if (PageUptodate(page)) {
1681 dout("fill_inline_data %p %llx.%llx len %zu locked_page %p\n",
1682 inode, ceph_vinop(inode), len, locked_page);
1685 void *kaddr = kmap_atomic(page);
1686 memcpy(kaddr, data, len);
1687 kunmap_atomic(kaddr);
1690 if (page != locked_page) {
1691 if (len < PAGE_SIZE)
1692 zero_user_segment(page, len, PAGE_SIZE);
1694 flush_dcache_page(page);
1696 SetPageUptodate(page);
1702 int ceph_uninline_data(struct file *file)
1704 struct inode *inode = file_inode(file);
1705 struct ceph_inode_info *ci = ceph_inode(inode);
1706 struct ceph_fs_client *fsc = ceph_inode_to_client(inode);
1707 struct ceph_osd_request *req = NULL;
1708 struct ceph_cap_flush *prealloc_cf = NULL;
1709 struct folio *folio = NULL;
1710 u64 inline_version = CEPH_INLINE_NONE;
1711 struct page *pages[1];
1715 spin_lock(&ci->i_ceph_lock);
1716 inline_version = ci->i_inline_version;
1717 spin_unlock(&ci->i_ceph_lock);
1719 dout("uninline_data %p %llx.%llx inline_version %llu\n",
1720 inode, ceph_vinop(inode), inline_version);
1722 if (ceph_inode_is_shutdown(inode)) {
1727 if (inline_version == CEPH_INLINE_NONE)
1730 prealloc_cf = ceph_alloc_cap_flush();
1734 if (inline_version == 1) /* initial version, no data */
1737 folio = read_mapping_folio(inode->i_mapping, 0, file);
1738 if (IS_ERR(folio)) {
1739 err = PTR_ERR(folio);
1745 len = i_size_read(inode);
1746 if (len > folio_size(folio))
1747 len = folio_size(folio);
1749 req = ceph_osdc_new_request(&fsc->client->osdc, &ci->i_layout,
1750 ceph_vino(inode), 0, &len, 0, 1,
1751 CEPH_OSD_OP_CREATE, CEPH_OSD_FLAG_WRITE,
1758 req->r_mtime = inode->i_mtime;
1759 ceph_osdc_start_request(&fsc->client->osdc, req);
1760 err = ceph_osdc_wait_request(&fsc->client->osdc, req);
1761 ceph_osdc_put_request(req);
1765 req = ceph_osdc_new_request(&fsc->client->osdc, &ci->i_layout,
1766 ceph_vino(inode), 0, &len, 1, 3,
1767 CEPH_OSD_OP_WRITE, CEPH_OSD_FLAG_WRITE,
1768 NULL, ci->i_truncate_seq,
1769 ci->i_truncate_size, false);
1775 pages[0] = folio_page(folio, 0);
1776 osd_req_op_extent_osd_data_pages(req, 1, pages, len, 0, false, false);
1779 __le64 xattr_buf = cpu_to_le64(inline_version);
1780 err = osd_req_op_xattr_init(req, 0, CEPH_OSD_OP_CMPXATTR,
1781 "inline_version", &xattr_buf,
1783 CEPH_OSD_CMPXATTR_OP_GT,
1784 CEPH_OSD_CMPXATTR_MODE_U64);
1791 int xattr_len = snprintf(xattr_buf, sizeof(xattr_buf),
1792 "%llu", inline_version);
1793 err = osd_req_op_xattr_init(req, 2, CEPH_OSD_OP_SETXATTR,
1795 xattr_buf, xattr_len, 0, 0);
1800 req->r_mtime = inode->i_mtime;
1801 ceph_osdc_start_request(&fsc->client->osdc, req);
1802 err = ceph_osdc_wait_request(&fsc->client->osdc, req);
1804 ceph_update_write_metrics(&fsc->mdsc->metric, req->r_start_latency,
1805 req->r_end_latency, len, err);
1811 /* Set to CAP_INLINE_NONE and dirty the caps */
1812 down_read(&fsc->mdsc->snap_rwsem);
1813 spin_lock(&ci->i_ceph_lock);
1814 ci->i_inline_version = CEPH_INLINE_NONE;
1815 dirty = __ceph_mark_dirty_caps(ci, CEPH_CAP_FILE_WR, &prealloc_cf);
1816 spin_unlock(&ci->i_ceph_lock);
1817 up_read(&fsc->mdsc->snap_rwsem);
1819 __mark_inode_dirty(inode, dirty);
1822 ceph_osdc_put_request(req);
1823 if (err == -ECANCELED)
1827 folio_unlock(folio);
1831 ceph_free_cap_flush(prealloc_cf);
1832 dout("uninline_data %p %llx.%llx inline_version %llu = %d\n",
1833 inode, ceph_vinop(inode), inline_version, err);
1837 static const struct vm_operations_struct ceph_vmops = {
1838 .fault = ceph_filemap_fault,
1839 .page_mkwrite = ceph_page_mkwrite,
1842 int ceph_mmap(struct file *file, struct vm_area_struct *vma)
1844 struct address_space *mapping = file->f_mapping;
1846 if (!mapping->a_ops->read_folio)
1848 vma->vm_ops = &ceph_vmops;
1857 static int __ceph_pool_perm_get(struct ceph_inode_info *ci,
1858 s64 pool, struct ceph_string *pool_ns)
1860 struct ceph_fs_client *fsc = ceph_inode_to_client(&ci->netfs.inode);
1861 struct ceph_mds_client *mdsc = fsc->mdsc;
1862 struct ceph_osd_request *rd_req = NULL, *wr_req = NULL;
1863 struct rb_node **p, *parent;
1864 struct ceph_pool_perm *perm;
1865 struct page **pages;
1867 int err = 0, err2 = 0, have = 0;
1869 down_read(&mdsc->pool_perm_rwsem);
1870 p = &mdsc->pool_perm_tree.rb_node;
1872 perm = rb_entry(*p, struct ceph_pool_perm, node);
1873 if (pool < perm->pool)
1875 else if (pool > perm->pool)
1876 p = &(*p)->rb_right;
1878 int ret = ceph_compare_string(pool_ns,
1884 p = &(*p)->rb_right;
1891 up_read(&mdsc->pool_perm_rwsem);
1896 dout("__ceph_pool_perm_get pool %lld ns %.*s no perm cached\n",
1897 pool, (int)pool_ns->len, pool_ns->str);
1899 dout("__ceph_pool_perm_get pool %lld no perm cached\n", pool);
1901 down_write(&mdsc->pool_perm_rwsem);
1902 p = &mdsc->pool_perm_tree.rb_node;
1906 perm = rb_entry(parent, struct ceph_pool_perm, node);
1907 if (pool < perm->pool)
1909 else if (pool > perm->pool)
1910 p = &(*p)->rb_right;
1912 int ret = ceph_compare_string(pool_ns,
1918 p = &(*p)->rb_right;
1926 up_write(&mdsc->pool_perm_rwsem);
1930 rd_req = ceph_osdc_alloc_request(&fsc->client->osdc, NULL,
1931 1, false, GFP_NOFS);
1937 rd_req->r_flags = CEPH_OSD_FLAG_READ;
1938 osd_req_op_init(rd_req, 0, CEPH_OSD_OP_STAT, 0);
1939 rd_req->r_base_oloc.pool = pool;
1941 rd_req->r_base_oloc.pool_ns = ceph_get_string(pool_ns);
1942 ceph_oid_printf(&rd_req->r_base_oid, "%llx.00000000", ci->i_vino.ino);
1944 err = ceph_osdc_alloc_messages(rd_req, GFP_NOFS);
1948 wr_req = ceph_osdc_alloc_request(&fsc->client->osdc, NULL,
1949 1, false, GFP_NOFS);
1955 wr_req->r_flags = CEPH_OSD_FLAG_WRITE;
1956 osd_req_op_init(wr_req, 0, CEPH_OSD_OP_CREATE, CEPH_OSD_OP_FLAG_EXCL);
1957 ceph_oloc_copy(&wr_req->r_base_oloc, &rd_req->r_base_oloc);
1958 ceph_oid_copy(&wr_req->r_base_oid, &rd_req->r_base_oid);
1960 err = ceph_osdc_alloc_messages(wr_req, GFP_NOFS);
1964 /* one page should be large enough for STAT data */
1965 pages = ceph_alloc_page_vector(1, GFP_KERNEL);
1966 if (IS_ERR(pages)) {
1967 err = PTR_ERR(pages);
1971 osd_req_op_raw_data_in_pages(rd_req, 0, pages, PAGE_SIZE,
1973 ceph_osdc_start_request(&fsc->client->osdc, rd_req);
1975 wr_req->r_mtime = ci->netfs.inode.i_mtime;
1976 ceph_osdc_start_request(&fsc->client->osdc, wr_req);
1978 err = ceph_osdc_wait_request(&fsc->client->osdc, rd_req);
1979 err2 = ceph_osdc_wait_request(&fsc->client->osdc, wr_req);
1981 if (err >= 0 || err == -ENOENT)
1983 else if (err != -EPERM) {
1984 if (err == -EBLOCKLISTED)
1985 fsc->blocklisted = true;
1989 if (err2 == 0 || err2 == -EEXIST)
1991 else if (err2 != -EPERM) {
1992 if (err2 == -EBLOCKLISTED)
1993 fsc->blocklisted = true;
1998 pool_ns_len = pool_ns ? pool_ns->len : 0;
1999 perm = kmalloc(sizeof(*perm) + pool_ns_len + 1, GFP_NOFS);
2007 perm->pool_ns_len = pool_ns_len;
2008 if (pool_ns_len > 0)
2009 memcpy(perm->pool_ns, pool_ns->str, pool_ns_len);
2010 perm->pool_ns[pool_ns_len] = 0;
2012 rb_link_node(&perm->node, parent, p);
2013 rb_insert_color(&perm->node, &mdsc->pool_perm_tree);
2016 up_write(&mdsc->pool_perm_rwsem);
2018 ceph_osdc_put_request(rd_req);
2019 ceph_osdc_put_request(wr_req);
2024 dout("__ceph_pool_perm_get pool %lld ns %.*s result = %d\n",
2025 pool, (int)pool_ns->len, pool_ns->str, err);
2027 dout("__ceph_pool_perm_get pool %lld result = %d\n", pool, err);
2031 int ceph_pool_perm_check(struct inode *inode, int need)
2033 struct ceph_inode_info *ci = ceph_inode(inode);
2034 struct ceph_string *pool_ns;
2038 /* Only need to do this for regular files */
2039 if (!S_ISREG(inode->i_mode))
2042 if (ci->i_vino.snap != CEPH_NOSNAP) {
2044 * Pool permission check needs to write to the first object.
2045 * But for snapshot, head of the first object may have alread
2046 * been deleted. Skip check to avoid creating orphan object.
2051 if (ceph_test_mount_opt(ceph_inode_to_client(inode),
2055 spin_lock(&ci->i_ceph_lock);
2056 flags = ci->i_ceph_flags;
2057 pool = ci->i_layout.pool_id;
2058 spin_unlock(&ci->i_ceph_lock);
2060 if (flags & CEPH_I_POOL_PERM) {
2061 if ((need & CEPH_CAP_FILE_RD) && !(flags & CEPH_I_POOL_RD)) {
2062 dout("ceph_pool_perm_check pool %lld no read perm\n",
2066 if ((need & CEPH_CAP_FILE_WR) && !(flags & CEPH_I_POOL_WR)) {
2067 dout("ceph_pool_perm_check pool %lld no write perm\n",
2074 pool_ns = ceph_try_get_string(ci->i_layout.pool_ns);
2075 ret = __ceph_pool_perm_get(ci, pool, pool_ns);
2076 ceph_put_string(pool_ns);
2080 flags = CEPH_I_POOL_PERM;
2081 if (ret & POOL_READ)
2082 flags |= CEPH_I_POOL_RD;
2083 if (ret & POOL_WRITE)
2084 flags |= CEPH_I_POOL_WR;
2086 spin_lock(&ci->i_ceph_lock);
2087 if (pool == ci->i_layout.pool_id &&
2088 pool_ns == rcu_dereference_raw(ci->i_layout.pool_ns)) {
2089 ci->i_ceph_flags |= flags;
2091 pool = ci->i_layout.pool_id;
2092 flags = ci->i_ceph_flags;
2094 spin_unlock(&ci->i_ceph_lock);
2098 void ceph_pool_perm_destroy(struct ceph_mds_client *mdsc)
2100 struct ceph_pool_perm *perm;
2103 while (!RB_EMPTY_ROOT(&mdsc->pool_perm_tree)) {
2104 n = rb_first(&mdsc->pool_perm_tree);
2105 perm = rb_entry(n, struct ceph_pool_perm, node);
2106 rb_erase(n, &mdsc->pool_perm_tree);