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;
249 int err = req->r_result;
251 ceph_update_read_metrics(&fsc->mdsc->metric, req->r_start_latency,
252 req->r_end_latency, osd_data->length, err);
254 dout("%s: result %d subreq->len=%zu i_size=%lld\n", __func__, req->r_result,
255 subreq->len, i_size_read(req->r_inode));
257 /* no object means success but no data */
260 else if (err == -EBLOCKLISTED)
261 fsc->blocklisted = true;
263 if (err >= 0 && err < subreq->len)
264 __set_bit(NETFS_SREQ_CLEAR_TAIL, &subreq->flags);
266 netfs_subreq_terminated(subreq, err, false);
268 num_pages = calc_pages_for(osd_data->alignment, osd_data->length);
269 ceph_put_page_vector(osd_data->pages, num_pages, 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;
340 u64 len = subreq->len;
342 if (ceph_inode_is_shutdown(inode)) {
347 if (ceph_has_inline_data(ci) && ceph_netfs_issue_op_inline(subreq))
350 req = ceph_osdc_new_request(&fsc->client->osdc, &ci->i_layout, vino, subreq->start, &len,
351 0, 1, CEPH_OSD_OP_READ,
352 CEPH_OSD_FLAG_READ | fsc->client->osdc.client->options->read_from_replica,
353 NULL, ci->i_truncate_seq, ci->i_truncate_size, false);
360 dout("%s: pos=%llu orig_len=%zu len=%llu\n", __func__, subreq->start, subreq->len, len);
361 iov_iter_xarray(&iter, ITER_DEST, &rreq->mapping->i_pages, subreq->start, len);
362 err = iov_iter_get_pages_alloc2(&iter, &pages, len, &page_off);
364 dout("%s: iov_ter_get_pages_alloc returned %d\n", __func__, err);
368 /* should always give us a page-aligned read */
369 WARN_ON_ONCE(page_off);
373 osd_req_op_extent_osd_data_pages(req, 0, pages, len, 0, false, false);
374 req->r_callback = finish_netfs_read;
375 req->r_priv = subreq;
376 req->r_inode = inode;
379 ceph_osdc_start_request(req->r_osdc, req);
381 ceph_osdc_put_request(req);
383 netfs_subreq_terminated(subreq, err, false);
384 dout("%s: result %d\n", __func__, err);
387 static int ceph_init_request(struct netfs_io_request *rreq, struct file *file)
389 struct inode *inode = rreq->inode;
390 int got = 0, want = CEPH_CAP_FILE_CACHE;
391 struct ceph_netfs_request_data *priv;
394 if (rreq->origin != NETFS_READAHEAD)
397 priv = kzalloc(sizeof(*priv), GFP_NOFS);
402 struct ceph_rw_context *rw_ctx;
403 struct ceph_file_info *fi = file->private_data;
405 priv->file_ra_pages = file->f_ra.ra_pages;
406 priv->file_ra_disabled = file->f_mode & FMODE_RANDOM;
408 rw_ctx = ceph_find_rw_context(fi);
410 rreq->netfs_priv = priv;
416 * readahead callers do not necessarily hold Fcb caps
417 * (e.g. fadvise, madvise).
419 ret = ceph_try_get_caps(inode, CEPH_CAP_FILE_RD, want, true, &got);
421 dout("start_read %p, error getting cap\n", inode);
426 dout("start_read %p, no cache cap\n", inode);
436 rreq->netfs_priv = priv;
445 static void ceph_netfs_free_request(struct netfs_io_request *rreq)
447 struct ceph_netfs_request_data *priv = rreq->netfs_priv;
453 ceph_put_cap_refs(ceph_inode(rreq->inode), priv->caps);
455 rreq->netfs_priv = NULL;
458 const struct netfs_request_ops ceph_netfs_ops = {
459 .init_request = ceph_init_request,
460 .free_request = ceph_netfs_free_request,
461 .begin_cache_operation = ceph_begin_cache_operation,
462 .issue_read = ceph_netfs_issue_read,
463 .expand_readahead = ceph_netfs_expand_readahead,
464 .clamp_length = ceph_netfs_clamp_length,
465 .check_write_begin = ceph_netfs_check_write_begin,
468 #ifdef CONFIG_CEPH_FSCACHE
469 static void ceph_set_page_fscache(struct page *page)
471 set_page_fscache(page);
474 static void ceph_fscache_write_terminated(void *priv, ssize_t error, bool was_async)
476 struct inode *inode = priv;
478 if (IS_ERR_VALUE(error) && error != -ENOBUFS)
479 ceph_fscache_invalidate(inode, false);
482 static void ceph_fscache_write_to_cache(struct inode *inode, u64 off, u64 len, bool caching)
484 struct ceph_inode_info *ci = ceph_inode(inode);
485 struct fscache_cookie *cookie = ceph_fscache_cookie(ci);
487 fscache_write_to_cache(cookie, inode->i_mapping, off, len, i_size_read(inode),
488 ceph_fscache_write_terminated, inode, caching);
491 static inline void ceph_set_page_fscache(struct page *page)
495 static inline void ceph_fscache_write_to_cache(struct inode *inode, u64 off, u64 len, bool caching)
498 #endif /* CONFIG_CEPH_FSCACHE */
500 struct ceph_writeback_ctl
510 * Get ref for the oldest snapc for an inode with dirty data... that is, the
511 * only snap context we are allowed to write back.
513 static struct ceph_snap_context *
514 get_oldest_context(struct inode *inode, struct ceph_writeback_ctl *ctl,
515 struct ceph_snap_context *page_snapc)
517 struct ceph_inode_info *ci = ceph_inode(inode);
518 struct ceph_snap_context *snapc = NULL;
519 struct ceph_cap_snap *capsnap = NULL;
521 spin_lock(&ci->i_ceph_lock);
522 list_for_each_entry(capsnap, &ci->i_cap_snaps, ci_item) {
523 dout(" cap_snap %p snapc %p has %d dirty pages\n", capsnap,
524 capsnap->context, capsnap->dirty_pages);
525 if (!capsnap->dirty_pages)
528 /* get i_size, truncate_{seq,size} for page_snapc? */
529 if (snapc && capsnap->context != page_snapc)
533 if (capsnap->writing) {
534 ctl->i_size = i_size_read(inode);
535 ctl->size_stable = false;
537 ctl->i_size = capsnap->size;
538 ctl->size_stable = true;
540 ctl->truncate_size = capsnap->truncate_size;
541 ctl->truncate_seq = capsnap->truncate_seq;
542 ctl->head_snapc = false;
548 snapc = ceph_get_snap_context(capsnap->context);
550 page_snapc == snapc ||
551 page_snapc->seq > snapc->seq)
554 if (!snapc && ci->i_wrbuffer_ref_head) {
555 snapc = ceph_get_snap_context(ci->i_head_snapc);
556 dout(" head snapc %p has %d dirty pages\n",
557 snapc, ci->i_wrbuffer_ref_head);
559 ctl->i_size = i_size_read(inode);
560 ctl->truncate_size = ci->i_truncate_size;
561 ctl->truncate_seq = ci->i_truncate_seq;
562 ctl->size_stable = false;
563 ctl->head_snapc = true;
566 spin_unlock(&ci->i_ceph_lock);
570 static u64 get_writepages_data_length(struct inode *inode,
571 struct page *page, u64 start)
573 struct ceph_inode_info *ci = ceph_inode(inode);
574 struct ceph_snap_context *snapc = page_snap_context(page);
575 struct ceph_cap_snap *capsnap = NULL;
576 u64 end = i_size_read(inode);
578 if (snapc != ci->i_head_snapc) {
580 spin_lock(&ci->i_ceph_lock);
581 list_for_each_entry(capsnap, &ci->i_cap_snaps, ci_item) {
582 if (capsnap->context == snapc) {
583 if (!capsnap->writing)
589 spin_unlock(&ci->i_ceph_lock);
592 if (end > page_offset(page) + thp_size(page))
593 end = page_offset(page) + thp_size(page);
594 return end > start ? end - start : 0;
598 * Write a single page, but leave the page locked.
600 * If we get a write error, mark the mapping for error, but still adjust the
601 * dirty page accounting (i.e., page is no longer dirty).
603 static int writepage_nounlock(struct page *page, struct writeback_control *wbc)
605 struct folio *folio = page_folio(page);
606 struct inode *inode = page->mapping->host;
607 struct ceph_inode_info *ci = ceph_inode(inode);
608 struct ceph_fs_client *fsc = ceph_inode_to_client(inode);
609 struct ceph_snap_context *snapc, *oldest;
610 loff_t page_off = page_offset(page);
612 loff_t len = thp_size(page);
613 struct ceph_writeback_ctl ceph_wbc;
614 struct ceph_osd_client *osdc = &fsc->client->osdc;
615 struct ceph_osd_request *req;
616 bool caching = ceph_is_cache_enabled(inode);
618 dout("writepage %p idx %lu\n", page, page->index);
620 if (ceph_inode_is_shutdown(inode))
623 /* verify this is a writeable snap context */
624 snapc = page_snap_context(page);
626 dout("writepage %p page %p not dirty?\n", inode, page);
629 oldest = get_oldest_context(inode, &ceph_wbc, snapc);
630 if (snapc->seq > oldest->seq) {
631 dout("writepage %p page %p snapc %p not writeable - noop\n",
633 /* we should only noop if called by kswapd */
634 WARN_ON(!(current->flags & PF_MEMALLOC));
635 ceph_put_snap_context(oldest);
636 redirty_page_for_writepage(wbc, page);
639 ceph_put_snap_context(oldest);
641 /* is this a partial page at end of file? */
642 if (page_off >= ceph_wbc.i_size) {
643 dout("folio at %lu beyond eof %llu\n", folio->index,
645 folio_invalidate(folio, 0, folio_size(folio));
649 if (ceph_wbc.i_size < page_off + len)
650 len = ceph_wbc.i_size - page_off;
652 dout("writepage %p page %p index %lu on %llu~%llu snapc %p seq %lld\n",
653 inode, page, page->index, page_off, len, snapc, snapc->seq);
655 if (atomic_long_inc_return(&fsc->writeback_count) >
656 CONGESTION_ON_THRESH(fsc->mount_options->congestion_kb))
657 fsc->write_congested = true;
659 req = ceph_osdc_new_request(osdc, &ci->i_layout, ceph_vino(inode), page_off, &len, 0, 1,
660 CEPH_OSD_OP_WRITE, CEPH_OSD_FLAG_WRITE, snapc,
661 ceph_wbc.truncate_seq, ceph_wbc.truncate_size,
664 redirty_page_for_writepage(wbc, page);
668 set_page_writeback(page);
670 ceph_set_page_fscache(page);
671 ceph_fscache_write_to_cache(inode, page_off, len, caching);
673 /* it may be a short write due to an object boundary */
674 WARN_ON_ONCE(len > thp_size(page));
675 osd_req_op_extent_osd_data_pages(req, 0, &page, len, 0, false, false);
676 dout("writepage %llu~%llu (%llu bytes)\n", page_off, len, len);
678 req->r_mtime = inode->i_mtime;
679 ceph_osdc_start_request(osdc, req);
680 err = ceph_osdc_wait_request(osdc, req);
682 ceph_update_write_metrics(&fsc->mdsc->metric, req->r_start_latency,
683 req->r_end_latency, len, err);
685 ceph_osdc_put_request(req);
690 struct writeback_control tmp_wbc;
693 if (err == -ERESTARTSYS) {
694 /* killed by SIGKILL */
695 dout("writepage interrupted page %p\n", page);
696 redirty_page_for_writepage(wbc, page);
697 end_page_writeback(page);
700 if (err == -EBLOCKLISTED)
701 fsc->blocklisted = true;
702 dout("writepage setting page/mapping error %d %p\n",
704 mapping_set_error(&inode->i_data, err);
705 wbc->pages_skipped++;
707 dout("writepage cleaned page %p\n", page);
708 err = 0; /* vfs expects us to return 0 */
710 oldest = detach_page_private(page);
711 WARN_ON_ONCE(oldest != snapc);
712 end_page_writeback(page);
713 ceph_put_wrbuffer_cap_refs(ci, 1, snapc);
714 ceph_put_snap_context(snapc); /* page's reference */
716 if (atomic_long_dec_return(&fsc->writeback_count) <
717 CONGESTION_OFF_THRESH(fsc->mount_options->congestion_kb))
718 fsc->write_congested = false;
723 static int ceph_writepage(struct page *page, struct writeback_control *wbc)
726 struct inode *inode = page->mapping->host;
730 if (wbc->sync_mode == WB_SYNC_NONE &&
731 ceph_inode_to_client(inode)->write_congested)
732 return AOP_WRITEPAGE_ACTIVATE;
734 wait_on_page_fscache(page);
736 err = writepage_nounlock(page, wbc);
737 if (err == -ERESTARTSYS) {
738 /* direct memory reclaimer was killed by SIGKILL. return 0
739 * to prevent caller from setting mapping/page error */
748 * async writeback completion handler.
750 * If we get an error, set the mapping error bit, but not the individual
753 static void writepages_finish(struct ceph_osd_request *req)
755 struct inode *inode = req->r_inode;
756 struct ceph_inode_info *ci = ceph_inode(inode);
757 struct ceph_osd_data *osd_data;
759 int num_pages, total_pages = 0;
761 int rc = req->r_result;
762 struct ceph_snap_context *snapc = req->r_snapc;
763 struct address_space *mapping = inode->i_mapping;
764 struct ceph_fs_client *fsc = ceph_inode_to_client(inode);
765 unsigned int len = 0;
768 dout("writepages_finish %p rc %d\n", inode, rc);
770 mapping_set_error(mapping, rc);
771 ceph_set_error_write(ci);
772 if (rc == -EBLOCKLISTED)
773 fsc->blocklisted = true;
775 ceph_clear_error_write(ci);
779 * We lost the cache cap, need to truncate the page before
780 * it is unlocked, otherwise we'd truncate it later in the
781 * page truncation thread, possibly losing some data that
784 remove_page = !(ceph_caps_issued(ci) &
785 (CEPH_CAP_FILE_CACHE|CEPH_CAP_FILE_LAZYIO));
787 /* clean all pages */
788 for (i = 0; i < req->r_num_ops; i++) {
789 if (req->r_ops[i].op != CEPH_OSD_OP_WRITE) {
790 pr_warn("%s incorrect op %d req %p index %d tid %llu\n",
791 __func__, req->r_ops[i].op, req, i, req->r_tid);
795 osd_data = osd_req_op_extent_osd_data(req, i);
796 BUG_ON(osd_data->type != CEPH_OSD_DATA_TYPE_PAGES);
797 len += osd_data->length;
798 num_pages = calc_pages_for((u64)osd_data->alignment,
799 (u64)osd_data->length);
800 total_pages += num_pages;
801 for (j = 0; j < num_pages; j++) {
802 page = osd_data->pages[j];
804 WARN_ON(!PageUptodate(page));
806 if (atomic_long_dec_return(&fsc->writeback_count) <
807 CONGESTION_OFF_THRESH(
808 fsc->mount_options->congestion_kb))
809 fsc->write_congested = false;
811 ceph_put_snap_context(detach_page_private(page));
812 end_page_writeback(page);
813 dout("unlocking %p\n", page);
816 generic_error_remove_page(inode->i_mapping,
821 dout("writepages_finish %p wrote %llu bytes cleaned %d pages\n",
822 inode, osd_data->length, rc >= 0 ? num_pages : 0);
824 release_pages(osd_data->pages, num_pages);
827 ceph_update_write_metrics(&fsc->mdsc->metric, req->r_start_latency,
828 req->r_end_latency, len, rc);
830 ceph_put_wrbuffer_cap_refs(ci, total_pages, snapc);
832 osd_data = osd_req_op_extent_osd_data(req, 0);
833 if (osd_data->pages_from_pool)
834 mempool_free(osd_data->pages, ceph_wb_pagevec_pool);
836 kfree(osd_data->pages);
837 ceph_osdc_put_request(req);
841 * initiate async writeback
843 static int ceph_writepages_start(struct address_space *mapping,
844 struct writeback_control *wbc)
846 struct inode *inode = mapping->host;
847 struct ceph_inode_info *ci = ceph_inode(inode);
848 struct ceph_fs_client *fsc = ceph_inode_to_client(inode);
849 struct ceph_vino vino = ceph_vino(inode);
850 pgoff_t index, start_index, end = -1;
851 struct ceph_snap_context *snapc = NULL, *last_snapc = NULL, *pgsnapc;
854 unsigned int wsize = i_blocksize(inode);
855 struct ceph_osd_request *req = NULL;
856 struct ceph_writeback_ctl ceph_wbc;
857 bool should_loop, range_whole = false;
859 bool caching = ceph_is_cache_enabled(inode);
861 if (wbc->sync_mode == WB_SYNC_NONE &&
862 fsc->write_congested)
865 dout("writepages_start %p (mode=%s)\n", inode,
866 wbc->sync_mode == WB_SYNC_NONE ? "NONE" :
867 (wbc->sync_mode == WB_SYNC_ALL ? "ALL" : "HOLD"));
869 if (ceph_inode_is_shutdown(inode)) {
870 if (ci->i_wrbuffer_ref > 0) {
872 "writepage_start %p %lld forced umount\n",
873 inode, ceph_ino(inode));
875 mapping_set_error(mapping, -EIO);
876 return -EIO; /* we're in a forced umount, don't write! */
878 if (fsc->mount_options->wsize < wsize)
879 wsize = fsc->mount_options->wsize;
883 start_index = wbc->range_cyclic ? mapping->writeback_index : 0;
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 ceph_put_snap_context(last_snapc);
927 while (!done && index <= end) {
928 int num_ops = 0, op_idx;
929 unsigned i, pvec_pages, max_pages, locked_pages = 0;
930 struct page **pages = NULL, **data_pages;
932 pgoff_t strip_unit_end = 0;
933 u64 offset = 0, len = 0;
934 bool from_pool = false;
936 max_pages = wsize >> PAGE_SHIFT;
939 pvec_pages = pagevec_lookup_range_tag(&pvec, mapping, &index,
940 end, PAGECACHE_TAG_DIRTY);
941 dout("pagevec_lookup_range_tag got %d\n", pvec_pages);
942 if (!pvec_pages && !locked_pages)
944 for (i = 0; i < pvec_pages && locked_pages < max_pages; i++) {
945 page = pvec.pages[i];
946 dout("? %p idx %lu\n", page, page->index);
947 if (locked_pages == 0)
948 lock_page(page); /* first page */
949 else if (!trylock_page(page))
952 /* only dirty pages, or our accounting breaks */
953 if (unlikely(!PageDirty(page)) ||
954 unlikely(page->mapping != mapping)) {
955 dout("!dirty or !mapping %p\n", page);
959 /* only if matching snap context */
960 pgsnapc = page_snap_context(page);
961 if (pgsnapc != snapc) {
962 dout("page snapc %p %lld != oldest %p %lld\n",
963 pgsnapc, pgsnapc->seq, snapc, snapc->seq);
965 !ceph_wbc.head_snapc &&
966 wbc->sync_mode != WB_SYNC_NONE)
971 if (page_offset(page) >= ceph_wbc.i_size) {
972 struct folio *folio = page_folio(page);
974 dout("folio at %lu beyond eof %llu\n",
975 folio->index, ceph_wbc.i_size);
976 if ((ceph_wbc.size_stable ||
977 folio_pos(folio) >= i_size_read(inode)) &&
978 folio_clear_dirty_for_io(folio))
979 folio_invalidate(folio, 0,
984 if (strip_unit_end && (page->index > strip_unit_end)) {
985 dout("end of strip unit %p\n", page);
989 if (PageWriteback(page) || PageFsCache(page)) {
990 if (wbc->sync_mode == WB_SYNC_NONE) {
991 dout("%p under writeback\n", page);
995 dout("waiting on writeback %p\n", page);
996 wait_on_page_writeback(page);
997 wait_on_page_fscache(page);
1000 if (!clear_page_dirty_for_io(page)) {
1001 dout("%p !clear_page_dirty_for_io\n", page);
1007 * We have something to write. If this is
1008 * the first locked page this time through,
1009 * calculate max possinle write size and
1010 * allocate a page array
1012 if (locked_pages == 0) {
1017 /* prepare async write request */
1018 offset = (u64)page_offset(page);
1019 ceph_calc_file_object_mapping(&ci->i_layout,
1026 strip_unit_end = page->index +
1027 ((len - 1) >> PAGE_SHIFT);
1030 max_pages = calc_pages_for(0, (u64)len);
1031 pages = kmalloc_array(max_pages,
1036 pages = mempool_alloc(ceph_wb_pagevec_pool, GFP_NOFS);
1041 } else if (page->index !=
1042 (offset + len) >> PAGE_SHIFT) {
1043 if (num_ops >= (from_pool ? CEPH_OSD_SLAB_OPS :
1044 CEPH_OSD_MAX_OPS)) {
1045 redirty_page_for_writepage(wbc, page);
1051 offset = (u64)page_offset(page);
1055 /* note position of first page in pvec */
1056 dout("%p will write page %p idx %lu\n",
1057 inode, page, page->index);
1059 if (atomic_long_inc_return(&fsc->writeback_count) >
1060 CONGESTION_ON_THRESH(
1061 fsc->mount_options->congestion_kb))
1062 fsc->write_congested = true;
1064 pages[locked_pages++] = page;
1065 pvec.pages[i] = NULL;
1067 len += thp_size(page);
1070 /* did we get anything? */
1072 goto release_pvec_pages;
1075 /* shift unused page to beginning of pvec */
1076 for (j = 0; j < pvec_pages; j++) {
1080 pvec.pages[n] = pvec.pages[j];
1085 if (pvec_pages && i == pvec_pages &&
1086 locked_pages < max_pages) {
1087 dout("reached end pvec, trying for more\n");
1088 pagevec_release(&pvec);
1089 goto get_more_pages;
1094 offset = page_offset(pages[0]);
1097 req = ceph_osdc_new_request(&fsc->client->osdc,
1098 &ci->i_layout, vino,
1099 offset, &len, 0, num_ops,
1100 CEPH_OSD_OP_WRITE, CEPH_OSD_FLAG_WRITE,
1101 snapc, ceph_wbc.truncate_seq,
1102 ceph_wbc.truncate_size, false);
1104 req = ceph_osdc_new_request(&fsc->client->osdc,
1105 &ci->i_layout, vino,
1110 CEPH_OSD_FLAG_WRITE,
1111 snapc, ceph_wbc.truncate_seq,
1112 ceph_wbc.truncate_size, true);
1113 BUG_ON(IS_ERR(req));
1115 BUG_ON(len < page_offset(pages[locked_pages - 1]) +
1116 thp_size(page) - offset);
1118 req->r_callback = writepages_finish;
1119 req->r_inode = inode;
1121 /* Format the osd request message and submit the write */
1125 for (i = 0; i < locked_pages; i++) {
1126 u64 cur_offset = page_offset(pages[i]);
1128 * Discontinuity in page range? Ceph can handle that by just passing
1129 * multiple extents in the write op.
1131 if (offset + len != cur_offset) {
1132 /* If it's full, stop here */
1133 if (op_idx + 1 == req->r_num_ops)
1136 /* Kick off an fscache write with what we have so far. */
1137 ceph_fscache_write_to_cache(inode, offset, len, caching);
1139 /* Start a new extent */
1140 osd_req_op_extent_dup_last(req, op_idx,
1141 cur_offset - offset);
1142 dout("writepages got pages at %llu~%llu\n",
1144 osd_req_op_extent_osd_data_pages(req, op_idx,
1147 osd_req_op_extent_update(req, op_idx, len);
1150 offset = cur_offset;
1151 data_pages = pages + i;
1155 set_page_writeback(pages[i]);
1157 ceph_set_page_fscache(pages[i]);
1158 len += thp_size(page);
1160 ceph_fscache_write_to_cache(inode, offset, len, caching);
1162 if (ceph_wbc.size_stable) {
1163 len = min(len, ceph_wbc.i_size - offset);
1164 } else if (i == locked_pages) {
1165 /* writepages_finish() clears writeback pages
1166 * according to the data length, so make sure
1167 * data length covers all locked pages */
1168 u64 min_len = len + 1 - thp_size(page);
1169 len = get_writepages_data_length(inode, pages[i - 1],
1171 len = max(len, min_len);
1173 dout("writepages got pages at %llu~%llu\n", offset, len);
1175 osd_req_op_extent_osd_data_pages(req, op_idx, data_pages, len,
1176 0, from_pool, false);
1177 osd_req_op_extent_update(req, op_idx, len);
1179 BUG_ON(op_idx + 1 != req->r_num_ops);
1182 if (i < locked_pages) {
1183 BUG_ON(num_ops <= req->r_num_ops);
1184 num_ops -= req->r_num_ops;
1187 /* allocate new pages array for next request */
1189 pages = kmalloc_array(locked_pages, sizeof(*pages),
1193 pages = mempool_alloc(ceph_wb_pagevec_pool, GFP_NOFS);
1196 memcpy(pages, data_pages + i,
1197 locked_pages * sizeof(*pages));
1198 memset(data_pages + i, 0,
1199 locked_pages * sizeof(*pages));
1201 BUG_ON(num_ops != req->r_num_ops);
1202 index = pages[i - 1]->index + 1;
1203 /* request message now owns the pages array */
1207 req->r_mtime = inode->i_mtime;
1208 ceph_osdc_start_request(&fsc->client->osdc, req);
1211 wbc->nr_to_write -= i;
1216 * We stop writing back only if we are not doing
1217 * integrity sync. In case of integrity sync we have to
1218 * keep going until we have written all the pages
1219 * we tagged for writeback prior to entering this loop.
1221 if (wbc->nr_to_write <= 0 && wbc->sync_mode == WB_SYNC_NONE)
1225 dout("pagevec_release on %d pages (%p)\n", (int)pvec.nr,
1226 pvec.nr ? pvec.pages[0] : NULL);
1227 pagevec_release(&pvec);
1230 if (should_loop && !done) {
1231 /* more to do; loop back to beginning of file */
1232 dout("writepages looping back to beginning of file\n");
1233 end = start_index - 1; /* OK even when start_index == 0 */
1235 /* to write dirty pages associated with next snapc,
1236 * we need to wait until current writes complete */
1237 if (wbc->sync_mode != WB_SYNC_NONE &&
1238 start_index == 0 && /* all dirty pages were checked */
1239 !ceph_wbc.head_snapc) {
1243 while ((index <= end) &&
1244 (nr = pagevec_lookup_tag(&pvec, mapping, &index,
1245 PAGECACHE_TAG_WRITEBACK))) {
1246 for (i = 0; i < nr; i++) {
1247 page = pvec.pages[i];
1248 if (page_snap_context(page) != snapc)
1250 wait_on_page_writeback(page);
1252 pagevec_release(&pvec);
1262 if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0))
1263 mapping->writeback_index = index;
1266 ceph_osdc_put_request(req);
1267 ceph_put_snap_context(last_snapc);
1268 dout("writepages dend - startone, rc = %d\n", rc);
1275 * See if a given @snapc is either writeable, or already written.
1277 static int context_is_writeable_or_written(struct inode *inode,
1278 struct ceph_snap_context *snapc)
1280 struct ceph_snap_context *oldest = get_oldest_context(inode, NULL, NULL);
1281 int ret = !oldest || snapc->seq <= oldest->seq;
1283 ceph_put_snap_context(oldest);
1288 * ceph_find_incompatible - find an incompatible context and return it
1289 * @page: page being dirtied
1291 * We are only allowed to write into/dirty a page if the page is
1292 * clean, or already dirty within the same snap context. Returns a
1293 * conflicting context if there is one, NULL if there isn't, or a
1294 * negative error code on other errors.
1296 * Must be called with page lock held.
1298 static struct ceph_snap_context *
1299 ceph_find_incompatible(struct page *page)
1301 struct inode *inode = page->mapping->host;
1302 struct ceph_inode_info *ci = ceph_inode(inode);
1304 if (ceph_inode_is_shutdown(inode)) {
1305 dout(" page %p %llx:%llx is shutdown\n", page,
1307 return ERR_PTR(-ESTALE);
1311 struct ceph_snap_context *snapc, *oldest;
1313 wait_on_page_writeback(page);
1315 snapc = page_snap_context(page);
1316 if (!snapc || snapc == ci->i_head_snapc)
1320 * this page is already dirty in another (older) snap
1321 * context! is it writeable now?
1323 oldest = get_oldest_context(inode, NULL, NULL);
1324 if (snapc->seq > oldest->seq) {
1325 /* not writeable -- return it for the caller to deal with */
1326 ceph_put_snap_context(oldest);
1327 dout(" page %p snapc %p not current or oldest\n", page, snapc);
1328 return ceph_get_snap_context(snapc);
1330 ceph_put_snap_context(oldest);
1332 /* yay, writeable, do it now (without dropping page lock) */
1333 dout(" page %p snapc %p not current, but oldest\n", page, snapc);
1334 if (clear_page_dirty_for_io(page)) {
1335 int r = writepage_nounlock(page, NULL);
1343 static int ceph_netfs_check_write_begin(struct file *file, loff_t pos, unsigned int len,
1344 struct folio **foliop, void **_fsdata)
1346 struct inode *inode = file_inode(file);
1347 struct ceph_inode_info *ci = ceph_inode(inode);
1348 struct ceph_snap_context *snapc;
1350 snapc = ceph_find_incompatible(folio_page(*foliop, 0));
1354 folio_unlock(*foliop);
1358 return PTR_ERR(snapc);
1360 ceph_queue_writeback(inode);
1361 r = wait_event_killable(ci->i_cap_wq,
1362 context_is_writeable_or_written(inode, snapc));
1363 ceph_put_snap_context(snapc);
1364 return r == 0 ? -EAGAIN : r;
1370 * We are only allowed to write into/dirty the page if the page is
1371 * clean, or already dirty within the same snap context.
1373 static int ceph_write_begin(struct file *file, struct address_space *mapping,
1374 loff_t pos, unsigned len,
1375 struct page **pagep, void **fsdata)
1377 struct inode *inode = file_inode(file);
1378 struct ceph_inode_info *ci = ceph_inode(inode);
1379 struct folio *folio = NULL;
1382 r = netfs_write_begin(&ci->netfs, file, inode->i_mapping, pos, len, &folio, NULL);
1386 folio_wait_fscache(folio);
1387 WARN_ON_ONCE(!folio_test_locked(folio));
1388 *pagep = &folio->page;
1393 * we don't do anything in here that simple_write_end doesn't do
1394 * except adjust dirty page accounting
1396 static int ceph_write_end(struct file *file, struct address_space *mapping,
1397 loff_t pos, unsigned len, unsigned copied,
1398 struct page *subpage, void *fsdata)
1400 struct folio *folio = page_folio(subpage);
1401 struct inode *inode = file_inode(file);
1402 bool check_cap = false;
1404 dout("write_end file %p inode %p folio %p %d~%d (%d)\n", file,
1405 inode, folio, (int)pos, (int)copied, (int)len);
1407 if (!folio_test_uptodate(folio)) {
1408 /* just return that nothing was copied on a short copy */
1413 folio_mark_uptodate(folio);
1416 /* did file size increase? */
1417 if (pos+copied > i_size_read(inode))
1418 check_cap = ceph_inode_set_size(inode, pos+copied);
1420 folio_mark_dirty(folio);
1423 folio_unlock(folio);
1427 ceph_check_caps(ceph_inode(inode), CHECK_CAPS_AUTHONLY, NULL);
1432 const struct address_space_operations ceph_aops = {
1433 .read_folio = netfs_read_folio,
1434 .readahead = netfs_readahead,
1435 .writepage = ceph_writepage,
1436 .writepages = ceph_writepages_start,
1437 .write_begin = ceph_write_begin,
1438 .write_end = ceph_write_end,
1439 .dirty_folio = ceph_dirty_folio,
1440 .invalidate_folio = ceph_invalidate_folio,
1441 .release_folio = ceph_release_folio,
1442 .direct_IO = noop_direct_IO,
1445 static void ceph_block_sigs(sigset_t *oldset)
1448 siginitsetinv(&mask, sigmask(SIGKILL));
1449 sigprocmask(SIG_BLOCK, &mask, oldset);
1452 static void ceph_restore_sigs(sigset_t *oldset)
1454 sigprocmask(SIG_SETMASK, oldset, NULL);
1460 static vm_fault_t ceph_filemap_fault(struct vm_fault *vmf)
1462 struct vm_area_struct *vma = vmf->vma;
1463 struct inode *inode = file_inode(vma->vm_file);
1464 struct ceph_inode_info *ci = ceph_inode(inode);
1465 struct ceph_file_info *fi = vma->vm_file->private_data;
1466 loff_t off = (loff_t)vmf->pgoff << PAGE_SHIFT;
1469 vm_fault_t ret = VM_FAULT_SIGBUS;
1471 if (ceph_inode_is_shutdown(inode))
1474 ceph_block_sigs(&oldset);
1476 dout("filemap_fault %p %llx.%llx %llu trying to get caps\n",
1477 inode, ceph_vinop(inode), off);
1478 if (fi->fmode & CEPH_FILE_MODE_LAZY)
1479 want = CEPH_CAP_FILE_CACHE | CEPH_CAP_FILE_LAZYIO;
1481 want = CEPH_CAP_FILE_CACHE;
1484 err = ceph_get_caps(vma->vm_file, CEPH_CAP_FILE_RD, want, -1, &got);
1488 dout("filemap_fault %p %llu got cap refs on %s\n",
1489 inode, off, ceph_cap_string(got));
1491 if ((got & (CEPH_CAP_FILE_CACHE | CEPH_CAP_FILE_LAZYIO)) ||
1492 !ceph_has_inline_data(ci)) {
1493 CEPH_DEFINE_RW_CONTEXT(rw_ctx, got);
1494 ceph_add_rw_context(fi, &rw_ctx);
1495 ret = filemap_fault(vmf);
1496 ceph_del_rw_context(fi, &rw_ctx);
1497 dout("filemap_fault %p %llu drop cap refs %s ret %x\n",
1498 inode, off, ceph_cap_string(got), ret);
1502 ceph_put_cap_refs(ci, got);
1507 /* read inline data */
1508 if (off >= PAGE_SIZE) {
1509 /* does not support inline data > PAGE_SIZE */
1510 ret = VM_FAULT_SIGBUS;
1512 struct address_space *mapping = inode->i_mapping;
1515 filemap_invalidate_lock_shared(mapping);
1516 page = find_or_create_page(mapping, 0,
1517 mapping_gfp_constraint(mapping, ~__GFP_FS));
1522 err = __ceph_do_getattr(inode, page,
1523 CEPH_STAT_CAP_INLINE_DATA, true);
1524 if (err < 0 || off >= i_size_read(inode)) {
1527 ret = vmf_error(err);
1530 if (err < PAGE_SIZE)
1531 zero_user_segment(page, err, PAGE_SIZE);
1533 flush_dcache_page(page);
1534 SetPageUptodate(page);
1536 ret = VM_FAULT_MAJOR | VM_FAULT_LOCKED;
1538 filemap_invalidate_unlock_shared(mapping);
1539 dout("filemap_fault %p %llu read inline data ret %x\n",
1543 ceph_restore_sigs(&oldset);
1545 ret = vmf_error(err);
1550 static vm_fault_t ceph_page_mkwrite(struct vm_fault *vmf)
1552 struct vm_area_struct *vma = vmf->vma;
1553 struct inode *inode = file_inode(vma->vm_file);
1554 struct ceph_inode_info *ci = ceph_inode(inode);
1555 struct ceph_file_info *fi = vma->vm_file->private_data;
1556 struct ceph_cap_flush *prealloc_cf;
1557 struct page *page = vmf->page;
1558 loff_t off = page_offset(page);
1559 loff_t size = i_size_read(inode);
1563 vm_fault_t ret = VM_FAULT_SIGBUS;
1565 if (ceph_inode_is_shutdown(inode))
1568 prealloc_cf = ceph_alloc_cap_flush();
1570 return VM_FAULT_OOM;
1572 sb_start_pagefault(inode->i_sb);
1573 ceph_block_sigs(&oldset);
1575 if (off + thp_size(page) <= size)
1576 len = thp_size(page);
1578 len = offset_in_thp(page, size);
1580 dout("page_mkwrite %p %llx.%llx %llu~%zd getting caps i_size %llu\n",
1581 inode, ceph_vinop(inode), off, len, size);
1582 if (fi->fmode & CEPH_FILE_MODE_LAZY)
1583 want = CEPH_CAP_FILE_BUFFER | CEPH_CAP_FILE_LAZYIO;
1585 want = CEPH_CAP_FILE_BUFFER;
1588 err = ceph_get_caps(vma->vm_file, CEPH_CAP_FILE_WR, want, off + len, &got);
1592 dout("page_mkwrite %p %llu~%zd got cap refs on %s\n",
1593 inode, off, len, ceph_cap_string(got));
1595 /* Update time before taking page lock */
1596 file_update_time(vma->vm_file);
1597 inode_inc_iversion_raw(inode);
1600 struct ceph_snap_context *snapc;
1604 if (page_mkwrite_check_truncate(page, inode) < 0) {
1606 ret = VM_FAULT_NOPAGE;
1610 snapc = ceph_find_incompatible(page);
1612 /* success. we'll keep the page locked. */
1613 set_page_dirty(page);
1614 ret = VM_FAULT_LOCKED;
1620 if (IS_ERR(snapc)) {
1621 ret = VM_FAULT_SIGBUS;
1625 ceph_queue_writeback(inode);
1626 err = wait_event_killable(ci->i_cap_wq,
1627 context_is_writeable_or_written(inode, snapc));
1628 ceph_put_snap_context(snapc);
1631 if (ret == VM_FAULT_LOCKED) {
1633 spin_lock(&ci->i_ceph_lock);
1634 dirty = __ceph_mark_dirty_caps(ci, CEPH_CAP_FILE_WR,
1636 spin_unlock(&ci->i_ceph_lock);
1638 __mark_inode_dirty(inode, dirty);
1641 dout("page_mkwrite %p %llu~%zd dropping cap refs on %s ret %x\n",
1642 inode, off, len, ceph_cap_string(got), ret);
1643 ceph_put_cap_refs_async(ci, got);
1645 ceph_restore_sigs(&oldset);
1646 sb_end_pagefault(inode->i_sb);
1647 ceph_free_cap_flush(prealloc_cf);
1649 ret = vmf_error(err);
1653 void ceph_fill_inline_data(struct inode *inode, struct page *locked_page,
1654 char *data, size_t len)
1656 struct address_space *mapping = inode->i_mapping;
1662 if (i_size_read(inode) == 0)
1664 page = find_or_create_page(mapping, 0,
1665 mapping_gfp_constraint(mapping,
1669 if (PageUptodate(page)) {
1676 dout("fill_inline_data %p %llx.%llx len %zu locked_page %p\n",
1677 inode, ceph_vinop(inode), len, locked_page);
1680 void *kaddr = kmap_atomic(page);
1681 memcpy(kaddr, data, len);
1682 kunmap_atomic(kaddr);
1685 if (page != locked_page) {
1686 if (len < PAGE_SIZE)
1687 zero_user_segment(page, len, PAGE_SIZE);
1689 flush_dcache_page(page);
1691 SetPageUptodate(page);
1697 int ceph_uninline_data(struct file *file)
1699 struct inode *inode = file_inode(file);
1700 struct ceph_inode_info *ci = ceph_inode(inode);
1701 struct ceph_fs_client *fsc = ceph_inode_to_client(inode);
1702 struct ceph_osd_request *req = NULL;
1703 struct ceph_cap_flush *prealloc_cf = NULL;
1704 struct folio *folio = NULL;
1705 u64 inline_version = CEPH_INLINE_NONE;
1706 struct page *pages[1];
1710 spin_lock(&ci->i_ceph_lock);
1711 inline_version = ci->i_inline_version;
1712 spin_unlock(&ci->i_ceph_lock);
1714 dout("uninline_data %p %llx.%llx inline_version %llu\n",
1715 inode, ceph_vinop(inode), inline_version);
1717 if (ceph_inode_is_shutdown(inode)) {
1722 if (inline_version == CEPH_INLINE_NONE)
1725 prealloc_cf = ceph_alloc_cap_flush();
1729 if (inline_version == 1) /* initial version, no data */
1732 folio = read_mapping_folio(inode->i_mapping, 0, file);
1733 if (IS_ERR(folio)) {
1734 err = PTR_ERR(folio);
1740 len = i_size_read(inode);
1741 if (len > folio_size(folio))
1742 len = folio_size(folio);
1744 req = ceph_osdc_new_request(&fsc->client->osdc, &ci->i_layout,
1745 ceph_vino(inode), 0, &len, 0, 1,
1746 CEPH_OSD_OP_CREATE, CEPH_OSD_FLAG_WRITE,
1753 req->r_mtime = inode->i_mtime;
1754 ceph_osdc_start_request(&fsc->client->osdc, req);
1755 err = ceph_osdc_wait_request(&fsc->client->osdc, req);
1756 ceph_osdc_put_request(req);
1760 req = ceph_osdc_new_request(&fsc->client->osdc, &ci->i_layout,
1761 ceph_vino(inode), 0, &len, 1, 3,
1762 CEPH_OSD_OP_WRITE, CEPH_OSD_FLAG_WRITE,
1763 NULL, ci->i_truncate_seq,
1764 ci->i_truncate_size, false);
1770 pages[0] = folio_page(folio, 0);
1771 osd_req_op_extent_osd_data_pages(req, 1, pages, len, 0, false, false);
1774 __le64 xattr_buf = cpu_to_le64(inline_version);
1775 err = osd_req_op_xattr_init(req, 0, CEPH_OSD_OP_CMPXATTR,
1776 "inline_version", &xattr_buf,
1778 CEPH_OSD_CMPXATTR_OP_GT,
1779 CEPH_OSD_CMPXATTR_MODE_U64);
1786 int xattr_len = snprintf(xattr_buf, sizeof(xattr_buf),
1787 "%llu", inline_version);
1788 err = osd_req_op_xattr_init(req, 2, CEPH_OSD_OP_SETXATTR,
1790 xattr_buf, xattr_len, 0, 0);
1795 req->r_mtime = inode->i_mtime;
1796 ceph_osdc_start_request(&fsc->client->osdc, req);
1797 err = ceph_osdc_wait_request(&fsc->client->osdc, req);
1799 ceph_update_write_metrics(&fsc->mdsc->metric, req->r_start_latency,
1800 req->r_end_latency, len, err);
1806 /* Set to CAP_INLINE_NONE and dirty the caps */
1807 down_read(&fsc->mdsc->snap_rwsem);
1808 spin_lock(&ci->i_ceph_lock);
1809 ci->i_inline_version = CEPH_INLINE_NONE;
1810 dirty = __ceph_mark_dirty_caps(ci, CEPH_CAP_FILE_WR, &prealloc_cf);
1811 spin_unlock(&ci->i_ceph_lock);
1812 up_read(&fsc->mdsc->snap_rwsem);
1814 __mark_inode_dirty(inode, dirty);
1817 ceph_osdc_put_request(req);
1818 if (err == -ECANCELED)
1822 folio_unlock(folio);
1826 ceph_free_cap_flush(prealloc_cf);
1827 dout("uninline_data %p %llx.%llx inline_version %llu = %d\n",
1828 inode, ceph_vinop(inode), inline_version, err);
1832 static const struct vm_operations_struct ceph_vmops = {
1833 .fault = ceph_filemap_fault,
1834 .page_mkwrite = ceph_page_mkwrite,
1837 int ceph_mmap(struct file *file, struct vm_area_struct *vma)
1839 struct address_space *mapping = file->f_mapping;
1841 if (!mapping->a_ops->read_folio)
1843 vma->vm_ops = &ceph_vmops;
1852 static int __ceph_pool_perm_get(struct ceph_inode_info *ci,
1853 s64 pool, struct ceph_string *pool_ns)
1855 struct ceph_fs_client *fsc = ceph_inode_to_client(&ci->netfs.inode);
1856 struct ceph_mds_client *mdsc = fsc->mdsc;
1857 struct ceph_osd_request *rd_req = NULL, *wr_req = NULL;
1858 struct rb_node **p, *parent;
1859 struct ceph_pool_perm *perm;
1860 struct page **pages;
1862 int err = 0, err2 = 0, have = 0;
1864 down_read(&mdsc->pool_perm_rwsem);
1865 p = &mdsc->pool_perm_tree.rb_node;
1867 perm = rb_entry(*p, struct ceph_pool_perm, node);
1868 if (pool < perm->pool)
1870 else if (pool > perm->pool)
1871 p = &(*p)->rb_right;
1873 int ret = ceph_compare_string(pool_ns,
1879 p = &(*p)->rb_right;
1886 up_read(&mdsc->pool_perm_rwsem);
1891 dout("__ceph_pool_perm_get pool %lld ns %.*s no perm cached\n",
1892 pool, (int)pool_ns->len, pool_ns->str);
1894 dout("__ceph_pool_perm_get pool %lld no perm cached\n", pool);
1896 down_write(&mdsc->pool_perm_rwsem);
1897 p = &mdsc->pool_perm_tree.rb_node;
1901 perm = rb_entry(parent, struct ceph_pool_perm, node);
1902 if (pool < perm->pool)
1904 else if (pool > perm->pool)
1905 p = &(*p)->rb_right;
1907 int ret = ceph_compare_string(pool_ns,
1913 p = &(*p)->rb_right;
1921 up_write(&mdsc->pool_perm_rwsem);
1925 rd_req = ceph_osdc_alloc_request(&fsc->client->osdc, NULL,
1926 1, false, GFP_NOFS);
1932 rd_req->r_flags = CEPH_OSD_FLAG_READ;
1933 osd_req_op_init(rd_req, 0, CEPH_OSD_OP_STAT, 0);
1934 rd_req->r_base_oloc.pool = pool;
1936 rd_req->r_base_oloc.pool_ns = ceph_get_string(pool_ns);
1937 ceph_oid_printf(&rd_req->r_base_oid, "%llx.00000000", ci->i_vino.ino);
1939 err = ceph_osdc_alloc_messages(rd_req, GFP_NOFS);
1943 wr_req = ceph_osdc_alloc_request(&fsc->client->osdc, NULL,
1944 1, false, GFP_NOFS);
1950 wr_req->r_flags = CEPH_OSD_FLAG_WRITE;
1951 osd_req_op_init(wr_req, 0, CEPH_OSD_OP_CREATE, CEPH_OSD_OP_FLAG_EXCL);
1952 ceph_oloc_copy(&wr_req->r_base_oloc, &rd_req->r_base_oloc);
1953 ceph_oid_copy(&wr_req->r_base_oid, &rd_req->r_base_oid);
1955 err = ceph_osdc_alloc_messages(wr_req, GFP_NOFS);
1959 /* one page should be large enough for STAT data */
1960 pages = ceph_alloc_page_vector(1, GFP_KERNEL);
1961 if (IS_ERR(pages)) {
1962 err = PTR_ERR(pages);
1966 osd_req_op_raw_data_in_pages(rd_req, 0, pages, PAGE_SIZE,
1968 ceph_osdc_start_request(&fsc->client->osdc, rd_req);
1970 wr_req->r_mtime = ci->netfs.inode.i_mtime;
1971 ceph_osdc_start_request(&fsc->client->osdc, wr_req);
1973 err = ceph_osdc_wait_request(&fsc->client->osdc, rd_req);
1974 err2 = ceph_osdc_wait_request(&fsc->client->osdc, wr_req);
1976 if (err >= 0 || err == -ENOENT)
1978 else if (err != -EPERM) {
1979 if (err == -EBLOCKLISTED)
1980 fsc->blocklisted = true;
1984 if (err2 == 0 || err2 == -EEXIST)
1986 else if (err2 != -EPERM) {
1987 if (err2 == -EBLOCKLISTED)
1988 fsc->blocklisted = true;
1993 pool_ns_len = pool_ns ? pool_ns->len : 0;
1994 perm = kmalloc(sizeof(*perm) + pool_ns_len + 1, GFP_NOFS);
2002 perm->pool_ns_len = pool_ns_len;
2003 if (pool_ns_len > 0)
2004 memcpy(perm->pool_ns, pool_ns->str, pool_ns_len);
2005 perm->pool_ns[pool_ns_len] = 0;
2007 rb_link_node(&perm->node, parent, p);
2008 rb_insert_color(&perm->node, &mdsc->pool_perm_tree);
2011 up_write(&mdsc->pool_perm_rwsem);
2013 ceph_osdc_put_request(rd_req);
2014 ceph_osdc_put_request(wr_req);
2019 dout("__ceph_pool_perm_get pool %lld ns %.*s result = %d\n",
2020 pool, (int)pool_ns->len, pool_ns->str, err);
2022 dout("__ceph_pool_perm_get pool %lld result = %d\n", pool, err);
2026 int ceph_pool_perm_check(struct inode *inode, int need)
2028 struct ceph_inode_info *ci = ceph_inode(inode);
2029 struct ceph_string *pool_ns;
2033 /* Only need to do this for regular files */
2034 if (!S_ISREG(inode->i_mode))
2037 if (ci->i_vino.snap != CEPH_NOSNAP) {
2039 * Pool permission check needs to write to the first object.
2040 * But for snapshot, head of the first object may have alread
2041 * been deleted. Skip check to avoid creating orphan object.
2046 if (ceph_test_mount_opt(ceph_inode_to_client(inode),
2050 spin_lock(&ci->i_ceph_lock);
2051 flags = ci->i_ceph_flags;
2052 pool = ci->i_layout.pool_id;
2053 spin_unlock(&ci->i_ceph_lock);
2055 if (flags & CEPH_I_POOL_PERM) {
2056 if ((need & CEPH_CAP_FILE_RD) && !(flags & CEPH_I_POOL_RD)) {
2057 dout("ceph_pool_perm_check pool %lld no read perm\n",
2061 if ((need & CEPH_CAP_FILE_WR) && !(flags & CEPH_I_POOL_WR)) {
2062 dout("ceph_pool_perm_check pool %lld no write perm\n",
2069 pool_ns = ceph_try_get_string(ci->i_layout.pool_ns);
2070 ret = __ceph_pool_perm_get(ci, pool, pool_ns);
2071 ceph_put_string(pool_ns);
2075 flags = CEPH_I_POOL_PERM;
2076 if (ret & POOL_READ)
2077 flags |= CEPH_I_POOL_RD;
2078 if (ret & POOL_WRITE)
2079 flags |= CEPH_I_POOL_WR;
2081 spin_lock(&ci->i_ceph_lock);
2082 if (pool == ci->i_layout.pool_id &&
2083 pool_ns == rcu_dereference_raw(ci->i_layout.pool_ns)) {
2084 ci->i_ceph_flags |= flags;
2086 pool = ci->i_layout.pool_id;
2087 flags = ci->i_ceph_flags;
2089 spin_unlock(&ci->i_ceph_lock);
2093 void ceph_pool_perm_destroy(struct ceph_mds_client *mdsc)
2095 struct ceph_pool_perm *perm;
2098 while (!RB_EMPTY_ROOT(&mdsc->pool_perm_tree)) {
2099 n = rb_first(&mdsc->pool_perm_tree);
2100 perm = rb_entry(n, struct ceph_pool_perm, node);
2101 rb_erase(n, &mdsc->pool_perm_tree);