xref: /openbmc/linux/fs/ceph/addr.c (revision e4b731cc)
1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
23d14c5d2SYehuda Sadeh #include <linux/ceph/ceph_debug.h>
31d3576fdSSage Weil 
41d3576fdSSage Weil #include <linux/backing-dev.h>
51d3576fdSSage Weil #include <linux/fs.h>
61d3576fdSSage Weil #include <linux/mm.h>
7d7bdba1cSDavid Howells #include <linux/swap.h>
81d3576fdSSage Weil #include <linux/pagemap.h>
95a0e3ad6STejun Heo #include <linux/slab.h>
101d3576fdSSage Weil #include <linux/pagevec.h>
111d3576fdSSage Weil #include <linux/task_io_accounting_ops.h>
12f361bf4aSIngo Molnar #include <linux/signal.h>
135c308356SJeff Layton #include <linux/iversion.h>
1497e27aaaSXiubo Li #include <linux/ktime.h>
15f0702876SJeff Layton #include <linux/netfs.h>
161d3576fdSSage Weil 
171d3576fdSSage Weil #include "super.h"
183d14c5d2SYehuda Sadeh #include "mds_client.h"
1999ccbd22SMilosz Tanski #include "cache.h"
2097e27aaaSXiubo Li #include "metric.h"
213d14c5d2SYehuda Sadeh #include <linux/ceph/osd_client.h>
2208c1ac50SIlya Dryomov #include <linux/ceph/striper.h>
231d3576fdSSage Weil 
241d3576fdSSage Weil /*
251d3576fdSSage Weil  * Ceph address space ops.
261d3576fdSSage Weil  *
271d3576fdSSage Weil  * There are a few funny things going on here.
281d3576fdSSage Weil  *
291d3576fdSSage Weil  * The page->private field is used to reference a struct
301d3576fdSSage Weil  * ceph_snap_context for _every_ dirty page.  This indicates which
311d3576fdSSage Weil  * snapshot the page was logically dirtied in, and thus which snap
321d3576fdSSage Weil  * context needs to be associated with the osd write during writeback.
331d3576fdSSage Weil  *
341d3576fdSSage Weil  * Similarly, struct ceph_inode_info maintains a set of counters to
3525985edcSLucas De Marchi  * count dirty pages on the inode.  In the absence of snapshots,
361d3576fdSSage Weil  * i_wrbuffer_ref == i_wrbuffer_ref_head == the dirty page count.
371d3576fdSSage Weil  *
381d3576fdSSage Weil  * When a snapshot is taken (that is, when the client receives
391d3576fdSSage Weil  * notification that a snapshot was taken), each inode with caps and
401d3576fdSSage Weil  * with dirty pages (dirty pages implies there is a cap) gets a new
411d3576fdSSage Weil  * ceph_cap_snap in the i_cap_snaps list (which is sorted in ascending
421d3576fdSSage Weil  * order, new snaps go to the tail).  The i_wrbuffer_ref_head count is
431d3576fdSSage Weil  * moved to capsnap->dirty. (Unless a sync write is currently in
441d3576fdSSage Weil  * progress.  In that case, the capsnap is said to be "pending", new
451d3576fdSSage Weil  * writes cannot start, and the capsnap isn't "finalized" until the
461d3576fdSSage Weil  * write completes (or fails) and a final size/mtime for the inode for
471d3576fdSSage Weil  * that snap can be settled upon.)  i_wrbuffer_ref_head is reset to 0.
481d3576fdSSage Weil  *
491d3576fdSSage Weil  * On writeback, we must submit writes to the osd IN SNAP ORDER.  So,
501d3576fdSSage Weil  * we look for the first capsnap in i_cap_snaps and write out pages in
511d3576fdSSage Weil  * that snap context _only_.  Then we move on to the next capsnap,
521d3576fdSSage Weil  * eventually reaching the "live" or "head" context (i.e., pages that
531d3576fdSSage Weil  * are not yet snapped) and are writing the most recently dirtied
541d3576fdSSage Weil  * pages.
551d3576fdSSage Weil  *
561d3576fdSSage Weil  * Invalidate and so forth must take care to ensure the dirty page
571d3576fdSSage Weil  * accounting is preserved.
581d3576fdSSage Weil  */
591d3576fdSSage Weil 
602baba250SYehuda Sadeh #define CONGESTION_ON_THRESH(congestion_kb) (congestion_kb >> (PAGE_SHIFT-10))
612baba250SYehuda Sadeh #define CONGESTION_OFF_THRESH(congestion_kb)				\
622baba250SYehuda Sadeh 	(CONGESTION_ON_THRESH(congestion_kb) -				\
632baba250SYehuda Sadeh 	 (CONGESTION_ON_THRESH(congestion_kb) >> 2))
642baba250SYehuda Sadeh 
65d801327dSJeff Layton static int ceph_netfs_check_write_begin(struct file *file, loff_t pos, unsigned int len,
66fac47b43SXiubo Li 					struct folio **foliop, void **_fsdata);
67d801327dSJeff Layton 
6861600ef8SYan, Zheng static inline struct ceph_snap_context *page_snap_context(struct page *page)
6961600ef8SYan, Zheng {
7061600ef8SYan, Zheng 	if (PagePrivate(page))
7161600ef8SYan, Zheng 		return (void *)page->private;
7261600ef8SYan, Zheng 	return NULL;
7361600ef8SYan, Zheng }
741d3576fdSSage Weil 
751d3576fdSSage Weil /*
761d3576fdSSage Weil  * Dirty a page.  Optimistically adjust accounting, on the assumption
771d3576fdSSage Weil  * that we won't race with invalidate.  If we do, readjust.
781d3576fdSSage Weil  */
798fb72b4aSMatthew Wilcox (Oracle) static bool ceph_dirty_folio(struct address_space *mapping, struct folio *folio)
801d3576fdSSage Weil {
811d3576fdSSage Weil 	struct inode *inode;
821d3576fdSSage Weil 	struct ceph_inode_info *ci;
831d3576fdSSage Weil 	struct ceph_snap_context *snapc;
841d3576fdSSage Weil 
858fb72b4aSMatthew Wilcox (Oracle) 	if (folio_test_dirty(folio)) {
868fb72b4aSMatthew Wilcox (Oracle) 		dout("%p dirty_folio %p idx %lu -- already dirty\n",
878fb72b4aSMatthew Wilcox (Oracle) 		     mapping->host, folio, folio->index);
88642d51fbSXiubo Li 		VM_BUG_ON_FOLIO(!folio_test_private(folio), folio);
898fb72b4aSMatthew Wilcox (Oracle) 		return false;
901d3576fdSSage Weil 	}
911d3576fdSSage Weil 
921d3576fdSSage Weil 	inode = mapping->host;
931d3576fdSSage Weil 	ci = ceph_inode(inode);
941d3576fdSSage Weil 
951d3576fdSSage Weil 	/* dirty the head */
96be655596SSage Weil 	spin_lock(&ci->i_ceph_lock);
975dda377cSYan, Zheng 	BUG_ON(ci->i_wr_ref == 0); // caller should hold Fw reference
985dda377cSYan, Zheng 	if (__ceph_have_pending_cap_snap(ci)) {
995dda377cSYan, Zheng 		struct ceph_cap_snap *capsnap =
1005dda377cSYan, Zheng 				list_last_entry(&ci->i_cap_snaps,
1015dda377cSYan, Zheng 						struct ceph_cap_snap,
1025dda377cSYan, Zheng 						ci_item);
1035dda377cSYan, Zheng 		snapc = ceph_get_snap_context(capsnap->context);
1045dda377cSYan, Zheng 		capsnap->dirty_pages++;
1055dda377cSYan, Zheng 	} else {
1065dda377cSYan, Zheng 		BUG_ON(!ci->i_head_snapc);
1075dda377cSYan, Zheng 		snapc = ceph_get_snap_context(ci->i_head_snapc);
1081d3576fdSSage Weil 		++ci->i_wrbuffer_ref_head;
1095dda377cSYan, Zheng 	}
1101d3576fdSSage Weil 	if (ci->i_wrbuffer_ref == 0)
1110444d76aSDave Chinner 		ihold(inode);
1121d3576fdSSage Weil 	++ci->i_wrbuffer_ref;
1138fb72b4aSMatthew Wilcox (Oracle) 	dout("%p dirty_folio %p idx %lu head %d/%d -> %d/%d "
1141d3576fdSSage Weil 	     "snapc %p seq %lld (%d snaps)\n",
1158fb72b4aSMatthew Wilcox (Oracle) 	     mapping->host, folio, folio->index,
1161d3576fdSSage Weil 	     ci->i_wrbuffer_ref-1, ci->i_wrbuffer_ref_head-1,
1171d3576fdSSage Weil 	     ci->i_wrbuffer_ref, ci->i_wrbuffer_ref_head,
1181d3576fdSSage Weil 	     snapc, snapc->seq, snapc->num_snaps);
119be655596SSage Weil 	spin_unlock(&ci->i_ceph_lock);
1201d3576fdSSage Weil 
1211d3576fdSSage Weil 	/*
1228fb72b4aSMatthew Wilcox (Oracle) 	 * Reference snap context in folio->private.  Also set
1239872f4deSMatthew Wilcox (Oracle) 	 * PagePrivate so that we get invalidate_folio callback.
1241d3576fdSSage Weil 	 */
125020bc44aSJeff Layton 	VM_WARN_ON_FOLIO(folio->private, folio);
1268fb72b4aSMatthew Wilcox (Oracle) 	folio_attach_private(folio, snapc);
1271d3576fdSSage Weil 
1288fb72b4aSMatthew Wilcox (Oracle) 	return ceph_fscache_dirty_folio(mapping, folio);
1291d3576fdSSage Weil }
1301d3576fdSSage Weil 
1311d3576fdSSage Weil /*
1329872f4deSMatthew Wilcox (Oracle)  * If we are truncating the full folio (i.e. offset == 0), adjust the
1339872f4deSMatthew Wilcox (Oracle)  * dirty folio counters appropriately.  Only called if there is private
1349872f4deSMatthew Wilcox (Oracle)  * data on the folio.
1351d3576fdSSage Weil  */
1369872f4deSMatthew Wilcox (Oracle) static void ceph_invalidate_folio(struct folio *folio, size_t offset,
1379872f4deSMatthew Wilcox (Oracle) 				size_t length)
1381d3576fdSSage Weil {
1394ce1e9adSAlexander Beregalov 	struct inode *inode;
1401d3576fdSSage Weil 	struct ceph_inode_info *ci;
141379fc7faSJeff Layton 	struct ceph_snap_context *snapc;
1421d3576fdSSage Weil 
1439872f4deSMatthew Wilcox (Oracle) 	inode = folio->mapping->host;
144b150f5c1SMilosz Tanski 	ci = ceph_inode(inode);
145b150f5c1SMilosz Tanski 
1469872f4deSMatthew Wilcox (Oracle) 	if (offset != 0 || length != folio_size(folio)) {
1479872f4deSMatthew Wilcox (Oracle) 		dout("%p invalidate_folio idx %lu partial dirty page %zu~%zu\n",
1489872f4deSMatthew Wilcox (Oracle) 		     inode, folio->index, offset, length);
149b150f5c1SMilosz Tanski 		return;
150b150f5c1SMilosz Tanski 	}
1514ce1e9adSAlexander Beregalov 
1529872f4deSMatthew Wilcox (Oracle) 	WARN_ON(!folio_test_locked(folio));
153642d51fbSXiubo Li 	if (folio_test_private(folio)) {
1549872f4deSMatthew Wilcox (Oracle) 		dout("%p invalidate_folio idx %lu full dirty page\n",
1559872f4deSMatthew Wilcox (Oracle) 		     inode, folio->index);
156b150f5c1SMilosz Tanski 
1579872f4deSMatthew Wilcox (Oracle) 		snapc = folio_detach_private(folio);
1581d3576fdSSage Weil 		ceph_put_wrbuffer_cap_refs(ci, 1, snapc);
1591d3576fdSSage Weil 		ceph_put_snap_context(snapc);
1601d3576fdSSage Weil 	}
1611d3576fdSSage Weil 
1629872f4deSMatthew Wilcox (Oracle) 	folio_wait_fscache(folio);
163400e1286SJeff Layton }
164400e1286SJeff Layton 
1655e414655SMatthew Wilcox (Oracle) static bool ceph_release_folio(struct folio *folio, gfp_t gfp)
1661d3576fdSSage Weil {
1675e414655SMatthew Wilcox (Oracle) 	struct inode *inode = folio->mapping->host;
168400e1286SJeff Layton 
1695e414655SMatthew Wilcox (Oracle) 	dout("%llx:%llx release_folio idx %lu (%sdirty)\n",
1705e414655SMatthew Wilcox (Oracle) 	     ceph_vinop(inode),
1715e414655SMatthew Wilcox (Oracle) 	     folio->index, folio_test_dirty(folio) ? "" : "not ");
172400e1286SJeff Layton 
1735e414655SMatthew Wilcox (Oracle) 	if (folio_test_private(folio))
1745e414655SMatthew Wilcox (Oracle) 		return false;
17599ccbd22SMilosz Tanski 
1765e414655SMatthew Wilcox (Oracle) 	if (folio_test_fscache(folio)) {
177d7bdba1cSDavid Howells 		if (current_is_kswapd() || !(gfp & __GFP_FS))
1785e414655SMatthew Wilcox (Oracle) 			return false;
1795e414655SMatthew Wilcox (Oracle) 		folio_wait_fscache(folio);
1807c46b318SJeff Layton 	}
181400e1286SJeff Layton 	ceph_fscache_note_page_release(inode);
1825e414655SMatthew Wilcox (Oracle) 	return true;
1831d3576fdSSage Weil }
1841d3576fdSSage Weil 
1856a19114bSDavid Howells static void ceph_netfs_expand_readahead(struct netfs_io_request *rreq)
186f0702876SJeff Layton {
187a25cedb4SJeff Layton 	struct inode *inode = rreq->inode;
188f0702876SJeff Layton 	struct ceph_inode_info *ci = ceph_inode(inode);
189f0702876SJeff Layton 	struct ceph_file_layout *lo = &ci->i_layout;
190f0702876SJeff Layton 	u32 blockoff;
191f0702876SJeff Layton 	u64 blockno;
192f0702876SJeff Layton 
193f0702876SJeff Layton 	/* Expand the start downward */
194f0702876SJeff Layton 	blockno = div_u64_rem(rreq->start, lo->stripe_unit, &blockoff);
195f0702876SJeff Layton 	rreq->start = blockno * lo->stripe_unit;
196f0702876SJeff Layton 	rreq->len += blockoff;
197f0702876SJeff Layton 
198f0702876SJeff Layton 	/* Now, round up the length to the next block */
199f0702876SJeff Layton 	rreq->len = roundup(rreq->len, lo->stripe_unit);
200f0702876SJeff Layton }
201f0702876SJeff Layton 
2026a19114bSDavid Howells static bool ceph_netfs_clamp_length(struct netfs_io_subrequest *subreq)
203f0702876SJeff Layton {
204a25cedb4SJeff Layton 	struct inode *inode = subreq->rreq->inode;
205f0702876SJeff Layton 	struct ceph_fs_client *fsc = ceph_inode_to_client(inode);
206f0702876SJeff Layton 	struct ceph_inode_info *ci = ceph_inode(inode);
207f0702876SJeff Layton 	u64 objno, objoff;
208f0702876SJeff Layton 	u32 xlen;
209f0702876SJeff Layton 
210f0702876SJeff Layton 	/* Truncate the extent at the end of the current block */
211f0702876SJeff Layton 	ceph_calc_file_object_mapping(&ci->i_layout, subreq->start, subreq->len,
212f0702876SJeff Layton 				      &objno, &objoff, &xlen);
213f0702876SJeff Layton 	subreq->len = min(xlen, fsc->mount_options->rsize);
214f0702876SJeff Layton 	return true;
215f0702876SJeff Layton }
216f0702876SJeff Layton 
217f0702876SJeff Layton static void finish_netfs_read(struct ceph_osd_request *req)
218f0702876SJeff Layton {
219f0702876SJeff Layton 	struct ceph_fs_client *fsc = ceph_inode_to_client(req->r_inode);
220f0702876SJeff Layton 	struct ceph_osd_data *osd_data = osd_req_op_extent_osd_data(req, 0);
2216a19114bSDavid Howells 	struct netfs_io_subrequest *subreq = req->r_priv;
222f0702876SJeff Layton 	int num_pages;
223f0702876SJeff Layton 	int err = req->r_result;
224f0702876SJeff Layton 
2258ae99ae2SXiubo Li 	ceph_update_read_metrics(&fsc->mdsc->metric, req->r_start_latency,
226903f4fecSXiubo Li 				 req->r_end_latency, osd_data->length, err);
227f0702876SJeff Layton 
228f0702876SJeff Layton 	dout("%s: result %d subreq->len=%zu i_size=%lld\n", __func__, req->r_result,
229f0702876SJeff Layton 	     subreq->len, i_size_read(req->r_inode));
230f0702876SJeff Layton 
231f0702876SJeff Layton 	/* no object means success but no data */
232f0702876SJeff Layton 	if (err == -ENOENT)
233f0702876SJeff Layton 		err = 0;
234f0702876SJeff Layton 	else if (err == -EBLOCKLISTED)
235f0702876SJeff Layton 		fsc->blocklisted = true;
236f0702876SJeff Layton 
237f0702876SJeff Layton 	if (err >= 0 && err < subreq->len)
238f0702876SJeff Layton 		__set_bit(NETFS_SREQ_CLEAR_TAIL, &subreq->flags);
239f0702876SJeff Layton 
2407467b044SJeff Layton 	netfs_subreq_terminated(subreq, err, false);
241f0702876SJeff Layton 
242f0702876SJeff Layton 	num_pages = calc_pages_for(osd_data->alignment, osd_data->length);
243f0702876SJeff Layton 	ceph_put_page_vector(osd_data->pages, num_pages, false);
244f0702876SJeff Layton 	iput(req->r_inode);
245f0702876SJeff Layton }
246f0702876SJeff Layton 
2476a19114bSDavid Howells static bool ceph_netfs_issue_op_inline(struct netfs_io_subrequest *subreq)
2485b19f1ebSDavid Howells {
2496a19114bSDavid Howells 	struct netfs_io_request *rreq = subreq->rreq;
2505b19f1ebSDavid Howells 	struct inode *inode = rreq->inode;
2515b19f1ebSDavid Howells 	struct ceph_mds_reply_info_parsed *rinfo;
2525b19f1ebSDavid Howells 	struct ceph_mds_reply_info_in *iinfo;
2535b19f1ebSDavid Howells 	struct ceph_mds_request *req;
2545b19f1ebSDavid Howells 	struct ceph_mds_client *mdsc = ceph_sb_to_mdsc(inode->i_sb);
2555b19f1ebSDavid Howells 	struct ceph_inode_info *ci = ceph_inode(inode);
2565b19f1ebSDavid Howells 	struct iov_iter iter;
2575b19f1ebSDavid Howells 	ssize_t err = 0;
2585b19f1ebSDavid Howells 	size_t len;
2595eed80fbSXiubo Li 	int mode;
2605b19f1ebSDavid Howells 
2615b19f1ebSDavid Howells 	__set_bit(NETFS_SREQ_CLEAR_TAIL, &subreq->flags);
262f18a3785SDavid Howells 	__clear_bit(NETFS_SREQ_COPY_TO_CACHE, &subreq->flags);
2635b19f1ebSDavid Howells 
2645b19f1ebSDavid Howells 	if (subreq->start >= inode->i_size)
2655b19f1ebSDavid Howells 		goto out;
2665b19f1ebSDavid Howells 
2675b19f1ebSDavid Howells 	/* We need to fetch the inline data. */
2685eed80fbSXiubo Li 	mode = ceph_try_to_choose_auth_mds(inode, CEPH_STAT_CAP_INLINE_DATA);
2695eed80fbSXiubo Li 	req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_GETATTR, mode);
2705b19f1ebSDavid Howells 	if (IS_ERR(req)) {
2715b19f1ebSDavid Howells 		err = PTR_ERR(req);
2725b19f1ebSDavid Howells 		goto out;
2735b19f1ebSDavid Howells 	}
2745b19f1ebSDavid Howells 	req->r_ino1 = ci->i_vino;
2755b19f1ebSDavid Howells 	req->r_args.getattr.mask = cpu_to_le32(CEPH_STAT_CAP_INLINE_DATA);
2765b19f1ebSDavid Howells 	req->r_num_caps = 2;
2775b19f1ebSDavid Howells 
2785b19f1ebSDavid Howells 	err = ceph_mdsc_do_request(mdsc, NULL, req);
2795b19f1ebSDavid Howells 	if (err < 0)
2805b19f1ebSDavid Howells 		goto out;
2815b19f1ebSDavid Howells 
2825b19f1ebSDavid Howells 	rinfo = &req->r_reply_info;
2835b19f1ebSDavid Howells 	iinfo = &rinfo->targeti;
2845b19f1ebSDavid Howells 	if (iinfo->inline_version == CEPH_INLINE_NONE) {
2855b19f1ebSDavid Howells 		/* The data got uninlined */
2865b19f1ebSDavid Howells 		ceph_mdsc_put_request(req);
2875b19f1ebSDavid Howells 		return false;
2885b19f1ebSDavid Howells 	}
2895b19f1ebSDavid Howells 
2905b19f1ebSDavid Howells 	len = min_t(size_t, iinfo->inline_len - subreq->start, subreq->len);
2915b19f1ebSDavid Howells 	iov_iter_xarray(&iter, READ, &rreq->mapping->i_pages, subreq->start, len);
2925b19f1ebSDavid Howells 	err = copy_to_iter(iinfo->inline_data + subreq->start, len, &iter);
2935b19f1ebSDavid Howells 	if (err == 0)
2945b19f1ebSDavid Howells 		err = -EFAULT;
2955b19f1ebSDavid Howells 
2965b19f1ebSDavid Howells 	ceph_mdsc_put_request(req);
2975b19f1ebSDavid Howells out:
2985b19f1ebSDavid Howells 	netfs_subreq_terminated(subreq, err, false);
2995b19f1ebSDavid Howells 	return true;
3005b19f1ebSDavid Howells }
3015b19f1ebSDavid Howells 
302f18a3785SDavid Howells static void ceph_netfs_issue_read(struct netfs_io_subrequest *subreq)
303f0702876SJeff Layton {
3046a19114bSDavid Howells 	struct netfs_io_request *rreq = subreq->rreq;
305a25cedb4SJeff Layton 	struct inode *inode = rreq->inode;
306f0702876SJeff Layton 	struct ceph_inode_info *ci = ceph_inode(inode);
307f0702876SJeff Layton 	struct ceph_fs_client *fsc = ceph_inode_to_client(inode);
308f0702876SJeff Layton 	struct ceph_osd_request *req;
309f0702876SJeff Layton 	struct ceph_vino vino = ceph_vino(inode);
310f0702876SJeff Layton 	struct iov_iter iter;
311f0702876SJeff Layton 	struct page **pages;
312f0702876SJeff Layton 	size_t page_off;
313f0702876SJeff Layton 	int err = 0;
314f0702876SJeff Layton 	u64 len = subreq->len;
315f0702876SJeff Layton 
31648490776SXiubo Li 	if (ceph_has_inline_data(ci) && ceph_netfs_issue_op_inline(subreq))
3175b19f1ebSDavid Howells 		return;
3185b19f1ebSDavid Howells 
319f0702876SJeff Layton 	req = ceph_osdc_new_request(&fsc->client->osdc, &ci->i_layout, vino, subreq->start, &len,
320f0702876SJeff Layton 			0, 1, CEPH_OSD_OP_READ,
321f0702876SJeff Layton 			CEPH_OSD_FLAG_READ | fsc->client->osdc.client->options->read_from_replica,
322f0702876SJeff Layton 			NULL, ci->i_truncate_seq, ci->i_truncate_size, false);
323f0702876SJeff Layton 	if (IS_ERR(req)) {
324f0702876SJeff Layton 		err = PTR_ERR(req);
325f0702876SJeff Layton 		req = NULL;
326f0702876SJeff Layton 		goto out;
327f0702876SJeff Layton 	}
328f0702876SJeff Layton 
329f0702876SJeff Layton 	dout("%s: pos=%llu orig_len=%zu len=%llu\n", __func__, subreq->start, subreq->len, len);
330f0702876SJeff Layton 	iov_iter_xarray(&iter, READ, &rreq->mapping->i_pages, subreq->start, len);
331b5358992SAl Viro 	err = iov_iter_get_pages_alloc2(&iter, &pages, len, &page_off);
332f0702876SJeff Layton 	if (err < 0) {
333f0702876SJeff Layton 		dout("%s: iov_ter_get_pages_alloc returned %d\n", __func__, err);
334f0702876SJeff Layton 		goto out;
335f0702876SJeff Layton 	}
336f0702876SJeff Layton 
337f0702876SJeff Layton 	/* should always give us a page-aligned read */
338f0702876SJeff Layton 	WARN_ON_ONCE(page_off);
339f0702876SJeff Layton 	len = err;
340a8af0d68SJeff Layton 	err = 0;
341f0702876SJeff Layton 
342f0702876SJeff Layton 	osd_req_op_extent_osd_data_pages(req, 0, pages, len, 0, false, false);
343f0702876SJeff Layton 	req->r_callback = finish_netfs_read;
344f0702876SJeff Layton 	req->r_priv = subreq;
345f0702876SJeff Layton 	req->r_inode = inode;
346f0702876SJeff Layton 	ihold(inode);
347f0702876SJeff Layton 
348a8af0d68SJeff Layton 	ceph_osdc_start_request(req->r_osdc, req);
349f0702876SJeff Layton out:
350f0702876SJeff Layton 	ceph_osdc_put_request(req);
351f0702876SJeff Layton 	if (err)
352f0702876SJeff Layton 		netfs_subreq_terminated(subreq, err, false);
353f0702876SJeff Layton 	dout("%s: result %d\n", __func__, err);
354f0702876SJeff Layton }
355f0702876SJeff Layton 
356a5c9dc44SDavid Howells static int ceph_init_request(struct netfs_io_request *rreq, struct file *file)
357a5c9dc44SDavid Howells {
358a5c9dc44SDavid Howells 	struct inode *inode = rreq->inode;
359a5c9dc44SDavid Howells 	int got = 0, want = CEPH_CAP_FILE_CACHE;
360a5c9dc44SDavid Howells 	int ret = 0;
361a5c9dc44SDavid Howells 
362a5c9dc44SDavid Howells 	if (rreq->origin != NETFS_READAHEAD)
363a5c9dc44SDavid Howells 		return 0;
364a5c9dc44SDavid Howells 
365a5c9dc44SDavid Howells 	if (file) {
366a5c9dc44SDavid Howells 		struct ceph_rw_context *rw_ctx;
367a5c9dc44SDavid Howells 		struct ceph_file_info *fi = file->private_data;
368a5c9dc44SDavid Howells 
369a5c9dc44SDavid Howells 		rw_ctx = ceph_find_rw_context(fi);
370a5c9dc44SDavid Howells 		if (rw_ctx)
371a5c9dc44SDavid Howells 			return 0;
372a5c9dc44SDavid Howells 	}
373a5c9dc44SDavid Howells 
374a5c9dc44SDavid Howells 	/*
375a5c9dc44SDavid Howells 	 * readahead callers do not necessarily hold Fcb caps
376a5c9dc44SDavid Howells 	 * (e.g. fadvise, madvise).
377a5c9dc44SDavid Howells 	 */
378a5c9dc44SDavid Howells 	ret = ceph_try_get_caps(inode, CEPH_CAP_FILE_RD, want, true, &got);
379a5c9dc44SDavid Howells 	if (ret < 0) {
380a5c9dc44SDavid Howells 		dout("start_read %p, error getting cap\n", inode);
381a5c9dc44SDavid Howells 		return ret;
382a5c9dc44SDavid Howells 	}
383a5c9dc44SDavid Howells 
384a5c9dc44SDavid Howells 	if (!(got & want)) {
385a5c9dc44SDavid Howells 		dout("start_read %p, no cache cap\n", inode);
386a5c9dc44SDavid Howells 		return -EACCES;
387a5c9dc44SDavid Howells 	}
388a5c9dc44SDavid Howells 	if (ret == 0)
389a5c9dc44SDavid Howells 		return -EACCES;
390a5c9dc44SDavid Howells 
391a5c9dc44SDavid Howells 	rreq->netfs_priv = (void *)(uintptr_t)got;
392a5c9dc44SDavid Howells 	return 0;
393a5c9dc44SDavid Howells }
394a5c9dc44SDavid Howells 
39540a81101SDavid Howells static void ceph_netfs_free_request(struct netfs_io_request *rreq)
39649870056SJeff Layton {
39740a81101SDavid Howells 	struct ceph_inode_info *ci = ceph_inode(rreq->inode);
39840a81101SDavid Howells 	int got = (uintptr_t)rreq->netfs_priv;
39949870056SJeff Layton 
40049870056SJeff Layton 	if (got)
40149870056SJeff Layton 		ceph_put_cap_refs(ci, got);
40249870056SJeff Layton }
40349870056SJeff Layton 
404bc899ee1SDavid Howells const struct netfs_request_ops ceph_netfs_ops = {
405a5c9dc44SDavid Howells 	.init_request		= ceph_init_request,
40640a81101SDavid Howells 	.free_request		= ceph_netfs_free_request,
407f0702876SJeff Layton 	.begin_cache_operation	= ceph_begin_cache_operation,
408f18a3785SDavid Howells 	.issue_read		= ceph_netfs_issue_read,
409f0702876SJeff Layton 	.expand_readahead	= ceph_netfs_expand_readahead,
410f0702876SJeff Layton 	.clamp_length		= ceph_netfs_clamp_length,
411d801327dSJeff Layton 	.check_write_begin	= ceph_netfs_check_write_begin,
412f0702876SJeff Layton };
413f0702876SJeff Layton 
4141702e797SJeff Layton #ifdef CONFIG_CEPH_FSCACHE
4151702e797SJeff Layton static void ceph_set_page_fscache(struct page *page)
4161702e797SJeff Layton {
4171702e797SJeff Layton 	set_page_fscache(page);
4181702e797SJeff Layton }
4191702e797SJeff Layton 
4201702e797SJeff Layton static void ceph_fscache_write_terminated(void *priv, ssize_t error, bool was_async)
4211702e797SJeff Layton {
4221702e797SJeff Layton 	struct inode *inode = priv;
4231702e797SJeff Layton 
4241702e797SJeff Layton 	if (IS_ERR_VALUE(error) && error != -ENOBUFS)
4251702e797SJeff Layton 		ceph_fscache_invalidate(inode, false);
4261702e797SJeff Layton }
4271702e797SJeff Layton 
4281702e797SJeff Layton static void ceph_fscache_write_to_cache(struct inode *inode, u64 off, u64 len, bool caching)
4291702e797SJeff Layton {
4301702e797SJeff Layton 	struct ceph_inode_info *ci = ceph_inode(inode);
4311702e797SJeff Layton 	struct fscache_cookie *cookie = ceph_fscache_cookie(ci);
4321702e797SJeff Layton 
4331702e797SJeff Layton 	fscache_write_to_cache(cookie, inode->i_mapping, off, len, i_size_read(inode),
4341702e797SJeff Layton 			       ceph_fscache_write_terminated, inode, caching);
4351702e797SJeff Layton }
4361702e797SJeff Layton #else
4371702e797SJeff Layton static inline void ceph_set_page_fscache(struct page *page)
4381702e797SJeff Layton {
4391702e797SJeff Layton }
4401702e797SJeff Layton 
4411702e797SJeff Layton static inline void ceph_fscache_write_to_cache(struct inode *inode, u64 off, u64 len, bool caching)
4421702e797SJeff Layton {
4431702e797SJeff Layton }
4441702e797SJeff Layton #endif /* CONFIG_CEPH_FSCACHE */
4451702e797SJeff Layton 
4461f934b00SYan, Zheng struct ceph_writeback_ctl
4471f934b00SYan, Zheng {
4481f934b00SYan, Zheng 	loff_t i_size;
4491f934b00SYan, Zheng 	u64 truncate_size;
4501f934b00SYan, Zheng 	u32 truncate_seq;
4511f934b00SYan, Zheng 	bool size_stable;
4522a2d927eSYan, Zheng 	bool head_snapc;
4531f934b00SYan, Zheng };
4541f934b00SYan, Zheng 
4551d3576fdSSage Weil /*
4561d3576fdSSage Weil  * Get ref for the oldest snapc for an inode with dirty data... that is, the
4571d3576fdSSage Weil  * only snap context we are allowed to write back.
4581d3576fdSSage Weil  */
4591f934b00SYan, Zheng static struct ceph_snap_context *
46005455e11SYan, Zheng get_oldest_context(struct inode *inode, struct ceph_writeback_ctl *ctl,
46105455e11SYan, Zheng 		   struct ceph_snap_context *page_snapc)
4621d3576fdSSage Weil {
4631d3576fdSSage Weil 	struct ceph_inode_info *ci = ceph_inode(inode);
4641d3576fdSSage Weil 	struct ceph_snap_context *snapc = NULL;
4651d3576fdSSage Weil 	struct ceph_cap_snap *capsnap = NULL;
4661d3576fdSSage Weil 
467be655596SSage Weil 	spin_lock(&ci->i_ceph_lock);
4681d3576fdSSage Weil 	list_for_each_entry(capsnap, &ci->i_cap_snaps, ci_item) {
4691d3576fdSSage Weil 		dout(" cap_snap %p snapc %p has %d dirty pages\n", capsnap,
4701d3576fdSSage Weil 		     capsnap->context, capsnap->dirty_pages);
47105455e11SYan, Zheng 		if (!capsnap->dirty_pages)
47205455e11SYan, Zheng 			continue;
47305455e11SYan, Zheng 
47405455e11SYan, Zheng 		/* get i_size, truncate_{seq,size} for page_snapc? */
47505455e11SYan, Zheng 		if (snapc && capsnap->context != page_snapc)
47605455e11SYan, Zheng 			continue;
47705455e11SYan, Zheng 
4781f934b00SYan, Zheng 		if (ctl) {
4791f934b00SYan, Zheng 			if (capsnap->writing) {
4801f934b00SYan, Zheng 				ctl->i_size = i_size_read(inode);
4811f934b00SYan, Zheng 				ctl->size_stable = false;
4821f934b00SYan, Zheng 			} else {
4831f934b00SYan, Zheng 				ctl->i_size = capsnap->size;
4841f934b00SYan, Zheng 				ctl->size_stable = true;
4851f934b00SYan, Zheng 			}
4861f934b00SYan, Zheng 			ctl->truncate_size = capsnap->truncate_size;
4871f934b00SYan, Zheng 			ctl->truncate_seq = capsnap->truncate_seq;
4882a2d927eSYan, Zheng 			ctl->head_snapc = false;
4891f934b00SYan, Zheng 		}
49005455e11SYan, Zheng 
49105455e11SYan, Zheng 		if (snapc)
4921d3576fdSSage Weil 			break;
49305455e11SYan, Zheng 
49405455e11SYan, Zheng 		snapc = ceph_get_snap_context(capsnap->context);
49505455e11SYan, Zheng 		if (!page_snapc ||
49605455e11SYan, Zheng 		    page_snapc == snapc ||
49705455e11SYan, Zheng 		    page_snapc->seq > snapc->seq)
49805455e11SYan, Zheng 			break;
4991d3576fdSSage Weil 	}
5007d8cb26dSSage Weil 	if (!snapc && ci->i_wrbuffer_ref_head) {
50180e755feSSage Weil 		snapc = ceph_get_snap_context(ci->i_head_snapc);
5021d3576fdSSage Weil 		dout(" head snapc %p has %d dirty pages\n",
5031d3576fdSSage Weil 		     snapc, ci->i_wrbuffer_ref_head);
5041f934b00SYan, Zheng 		if (ctl) {
5051f934b00SYan, Zheng 			ctl->i_size = i_size_read(inode);
5061f934b00SYan, Zheng 			ctl->truncate_size = ci->i_truncate_size;
5071f934b00SYan, Zheng 			ctl->truncate_seq = ci->i_truncate_seq;
5081f934b00SYan, Zheng 			ctl->size_stable = false;
5092a2d927eSYan, Zheng 			ctl->head_snapc = true;
5101f934b00SYan, Zheng 		}
5111d3576fdSSage Weil 	}
512be655596SSage Weil 	spin_unlock(&ci->i_ceph_lock);
5131d3576fdSSage Weil 	return snapc;
5141d3576fdSSage Weil }
5151d3576fdSSage Weil 
5161f934b00SYan, Zheng static u64 get_writepages_data_length(struct inode *inode,
5171f934b00SYan, Zheng 				      struct page *page, u64 start)
5181f934b00SYan, Zheng {
5191f934b00SYan, Zheng 	struct ceph_inode_info *ci = ceph_inode(inode);
5201f934b00SYan, Zheng 	struct ceph_snap_context *snapc = page_snap_context(page);
5211f934b00SYan, Zheng 	struct ceph_cap_snap *capsnap = NULL;
5221f934b00SYan, Zheng 	u64 end = i_size_read(inode);
5231f934b00SYan, Zheng 
5241f934b00SYan, Zheng 	if (snapc != ci->i_head_snapc) {
5251f934b00SYan, Zheng 		bool found = false;
5261f934b00SYan, Zheng 		spin_lock(&ci->i_ceph_lock);
5271f934b00SYan, Zheng 		list_for_each_entry(capsnap, &ci->i_cap_snaps, ci_item) {
5281f934b00SYan, Zheng 			if (capsnap->context == snapc) {
5291f934b00SYan, Zheng 				if (!capsnap->writing)
5301f934b00SYan, Zheng 					end = capsnap->size;
5311f934b00SYan, Zheng 				found = true;
5321f934b00SYan, Zheng 				break;
5331f934b00SYan, Zheng 			}
5341f934b00SYan, Zheng 		}
5351f934b00SYan, Zheng 		spin_unlock(&ci->i_ceph_lock);
5361f934b00SYan, Zheng 		WARN_ON(!found);
5371f934b00SYan, Zheng 	}
5388ff2d290SJeff Layton 	if (end > page_offset(page) + thp_size(page))
5398ff2d290SJeff Layton 		end = page_offset(page) + thp_size(page);
5401f934b00SYan, Zheng 	return end > start ? end - start : 0;
5411f934b00SYan, Zheng }
5421f934b00SYan, Zheng 
5431d3576fdSSage Weil /*
5441d3576fdSSage Weil  * Write a single page, but leave the page locked.
5451d3576fdSSage Weil  *
546b72b13ebSJeff Layton  * If we get a write error, mark the mapping for error, but still adjust the
5471d3576fdSSage Weil  * dirty page accounting (i.e., page is no longer dirty).
5481d3576fdSSage Weil  */
5491d3576fdSSage Weil static int writepage_nounlock(struct page *page, struct writeback_control *wbc)
5501d3576fdSSage Weil {
551a628304eSMatthew Wilcox (Oracle) 	struct folio *folio = page_folio(page);
5526390987fSJeff Layton 	struct inode *inode = page->mapping->host;
5536390987fSJeff Layton 	struct ceph_inode_info *ci = ceph_inode(inode);
5546390987fSJeff Layton 	struct ceph_fs_client *fsc = ceph_inode_to_client(inode);
5556298a337SSage Weil 	struct ceph_snap_context *snapc, *oldest;
556fc2744aaSYan, Zheng 	loff_t page_off = page_offset(page);
5576390987fSJeff Layton 	int err;
5588ff2d290SJeff Layton 	loff_t len = thp_size(page);
5591f934b00SYan, Zheng 	struct ceph_writeback_ctl ceph_wbc;
5606390987fSJeff Layton 	struct ceph_osd_client *osdc = &fsc->client->osdc;
5616390987fSJeff Layton 	struct ceph_osd_request *req;
5621702e797SJeff Layton 	bool caching = ceph_is_cache_enabled(inode);
5631d3576fdSSage Weil 
5641d3576fdSSage Weil 	dout("writepage %p idx %lu\n", page, page->index);
5651d3576fdSSage Weil 
5661d3576fdSSage Weil 	/* verify this is a writeable snap context */
56761600ef8SYan, Zheng 	snapc = page_snap_context(page);
568d37b1d99SMarkus Elfring 	if (!snapc) {
5691d3576fdSSage Weil 		dout("writepage %p page %p not dirty?\n", inode, page);
57043986881SYan, Zheng 		return 0;
5711d3576fdSSage Weil 	}
57205455e11SYan, Zheng 	oldest = get_oldest_context(inode, &ceph_wbc, snapc);
5736298a337SSage Weil 	if (snapc->seq > oldest->seq) {
5741d3576fdSSage Weil 		dout("writepage %p page %p snapc %p not writeable - noop\n",
57561600ef8SYan, Zheng 		     inode, page, snapc);
5761d3576fdSSage Weil 		/* we should only noop if called by kswapd */
577fa71fefbSYan, Zheng 		WARN_ON(!(current->flags & PF_MEMALLOC));
5786298a337SSage Weil 		ceph_put_snap_context(oldest);
579fa71fefbSYan, Zheng 		redirty_page_for_writepage(wbc, page);
58043986881SYan, Zheng 		return 0;
5811d3576fdSSage Weil 	}
5826298a337SSage Weil 	ceph_put_snap_context(oldest);
5831d3576fdSSage Weil 
5841d3576fdSSage Weil 	/* is this a partial page at end of file? */
5851f934b00SYan, Zheng 	if (page_off >= ceph_wbc.i_size) {
586a628304eSMatthew Wilcox (Oracle) 		dout("folio at %lu beyond eof %llu\n", folio->index,
587a628304eSMatthew Wilcox (Oracle) 				ceph_wbc.i_size);
588a628304eSMatthew Wilcox (Oracle) 		folio_invalidate(folio, 0, folio_size(folio));
58943986881SYan, Zheng 		return 0;
590fc2744aaSYan, Zheng 	}
59143986881SYan, Zheng 
5921f934b00SYan, Zheng 	if (ceph_wbc.i_size < page_off + len)
5931f934b00SYan, Zheng 		len = ceph_wbc.i_size - page_off;
5941d3576fdSSage Weil 
5956390987fSJeff Layton 	dout("writepage %p page %p index %lu on %llu~%llu snapc %p seq %lld\n",
5961c0a9c2dSYan, Zheng 	     inode, page, page->index, page_off, len, snapc, snapc->seq);
5971d3576fdSSage Weil 
598314c4737SYan, Zheng 	if (atomic_long_inc_return(&fsc->writeback_count) >
5993d14c5d2SYehuda Sadeh 	    CONGESTION_ON_THRESH(fsc->mount_options->congestion_kb))
600503d4fa6SNeilBrown 		fsc->write_congested = true;
6012baba250SYehuda Sadeh 
6026390987fSJeff Layton 	req = ceph_osdc_new_request(osdc, &ci->i_layout, ceph_vino(inode), page_off, &len, 0, 1,
6036390987fSJeff Layton 				    CEPH_OSD_OP_WRITE, CEPH_OSD_FLAG_WRITE, snapc,
6046390987fSJeff Layton 				    ceph_wbc.truncate_seq, ceph_wbc.truncate_size,
6056390987fSJeff Layton 				    true);
6063459bd0cSXiubo Li 	if (IS_ERR(req)) {
6073459bd0cSXiubo Li 		redirty_page_for_writepage(wbc, page);
6086390987fSJeff Layton 		return PTR_ERR(req);
6093459bd0cSXiubo Li 	}
6101702e797SJeff Layton 
6111702e797SJeff Layton 	set_page_writeback(page);
6121702e797SJeff Layton 	if (caching)
6131702e797SJeff Layton 		ceph_set_page_fscache(page);
6141702e797SJeff Layton 	ceph_fscache_write_to_cache(inode, page_off, len, caching);
6156390987fSJeff Layton 
6166390987fSJeff Layton 	/* it may be a short write due to an object boundary */
6178ff2d290SJeff Layton 	WARN_ON_ONCE(len > thp_size(page));
6186390987fSJeff Layton 	osd_req_op_extent_osd_data_pages(req, 0, &page, len, 0, false, false);
6196390987fSJeff Layton 	dout("writepage %llu~%llu (%llu bytes)\n", page_off, len, len);
6206390987fSJeff Layton 
6216390987fSJeff Layton 	req->r_mtime = inode->i_mtime;
622a8af0d68SJeff Layton 	ceph_osdc_start_request(osdc, req);
6236390987fSJeff Layton 	err = ceph_osdc_wait_request(osdc, req);
6246390987fSJeff Layton 
6258ae99ae2SXiubo Li 	ceph_update_write_metrics(&fsc->mdsc->metric, req->r_start_latency,
626903f4fecSXiubo Li 				  req->r_end_latency, len, err);
6276390987fSJeff Layton 
6286390987fSJeff Layton 	ceph_osdc_put_request(req);
6296390987fSJeff Layton 	if (err == 0)
6306390987fSJeff Layton 		err = len;
6316390987fSJeff Layton 
6321d3576fdSSage Weil 	if (err < 0) {
633ad15ec06SYan, Zheng 		struct writeback_control tmp_wbc;
634ad15ec06SYan, Zheng 		if (!wbc)
635ad15ec06SYan, Zheng 			wbc = &tmp_wbc;
636ad15ec06SYan, Zheng 		if (err == -ERESTARTSYS) {
637ad15ec06SYan, Zheng 			/* killed by SIGKILL */
638ad15ec06SYan, Zheng 			dout("writepage interrupted page %p\n", page);
639ad15ec06SYan, Zheng 			redirty_page_for_writepage(wbc, page);
640ad15ec06SYan, Zheng 			end_page_writeback(page);
64143986881SYan, Zheng 			return err;
642ad15ec06SYan, Zheng 		}
6430b98acd6SIlya Dryomov 		if (err == -EBLOCKLISTED)
6440b98acd6SIlya Dryomov 			fsc->blocklisted = true;
645ad15ec06SYan, Zheng 		dout("writepage setting page/mapping error %d %p\n",
646ad15ec06SYan, Zheng 		     err, page);
6471d3576fdSSage Weil 		mapping_set_error(&inode->i_data, err);
6481d3576fdSSage Weil 		wbc->pages_skipped++;
6491d3576fdSSage Weil 	} else {
6501d3576fdSSage Weil 		dout("writepage cleaned page %p\n", page);
6511d3576fdSSage Weil 		err = 0;  /* vfs expects us to return 0 */
6521d3576fdSSage Weil 	}
653379fc7faSJeff Layton 	oldest = detach_page_private(page);
654379fc7faSJeff Layton 	WARN_ON_ONCE(oldest != snapc);
6551d3576fdSSage Weil 	end_page_writeback(page);
6561d3576fdSSage Weil 	ceph_put_wrbuffer_cap_refs(ci, 1, snapc);
6576298a337SSage Weil 	ceph_put_snap_context(snapc);  /* page's reference */
658314c4737SYan, Zheng 
659314c4737SYan, Zheng 	if (atomic_long_dec_return(&fsc->writeback_count) <
660314c4737SYan, Zheng 	    CONGESTION_OFF_THRESH(fsc->mount_options->congestion_kb))
661503d4fa6SNeilBrown 		fsc->write_congested = false;
662314c4737SYan, Zheng 
6631d3576fdSSage Weil 	return err;
6641d3576fdSSage Weil }
6651d3576fdSSage Weil 
6661d3576fdSSage Weil static int ceph_writepage(struct page *page, struct writeback_control *wbc)
6671d3576fdSSage Weil {
668dbd646a8SYehuda Sadeh 	int err;
669dbd646a8SYehuda Sadeh 	struct inode *inode = page->mapping->host;
670dbd646a8SYehuda Sadeh 	BUG_ON(!inode);
67170b666c3SSage Weil 	ihold(inode);
6721702e797SJeff Layton 
673503d4fa6SNeilBrown 	if (wbc->sync_mode == WB_SYNC_NONE &&
674503d4fa6SNeilBrown 	    ceph_inode_to_client(inode)->write_congested)
675503d4fa6SNeilBrown 		return AOP_WRITEPAGE_ACTIVATE;
676503d4fa6SNeilBrown 
6771702e797SJeff Layton 	wait_on_page_fscache(page);
6781702e797SJeff Layton 
679dbd646a8SYehuda Sadeh 	err = writepage_nounlock(page, wbc);
680ad15ec06SYan, Zheng 	if (err == -ERESTARTSYS) {
681ad15ec06SYan, Zheng 		/* direct memory reclaimer was killed by SIGKILL. return 0
682ad15ec06SYan, Zheng 		 * to prevent caller from setting mapping/page error */
683ad15ec06SYan, Zheng 		err = 0;
684ad15ec06SYan, Zheng 	}
6851d3576fdSSage Weil 	unlock_page(page);
686dbd646a8SYehuda Sadeh 	iput(inode);
6871d3576fdSSage Weil 	return err;
6881d3576fdSSage Weil }
6891d3576fdSSage Weil 
6901d3576fdSSage Weil /*
6911d3576fdSSage Weil  * async writeback completion handler.
6921d3576fdSSage Weil  *
6931d3576fdSSage Weil  * If we get an error, set the mapping error bit, but not the individual
6941d3576fdSSage Weil  * page error bits.
6951d3576fdSSage Weil  */
69685e084feSIlya Dryomov static void writepages_finish(struct ceph_osd_request *req)
6971d3576fdSSage Weil {
6981d3576fdSSage Weil 	struct inode *inode = req->r_inode;
6991d3576fdSSage Weil 	struct ceph_inode_info *ci = ceph_inode(inode);
70087060c10SAlex Elder 	struct ceph_osd_data *osd_data;
7011d3576fdSSage Weil 	struct page *page;
7025b64640cSYan, Zheng 	int num_pages, total_pages = 0;
7035b64640cSYan, Zheng 	int i, j;
7045b64640cSYan, Zheng 	int rc = req->r_result;
7051d3576fdSSage Weil 	struct ceph_snap_context *snapc = req->r_snapc;
7061d3576fdSSage Weil 	struct address_space *mapping = inode->i_mapping;
7073d14c5d2SYehuda Sadeh 	struct ceph_fs_client *fsc = ceph_inode_to_client(inode);
708903f4fecSXiubo Li 	unsigned int len = 0;
7095b64640cSYan, Zheng 	bool remove_page;
7101d3576fdSSage Weil 
7115b64640cSYan, Zheng 	dout("writepages_finish %p rc %d\n", inode, rc);
71226544c62SJeff Layton 	if (rc < 0) {
7131d3576fdSSage Weil 		mapping_set_error(mapping, rc);
71426544c62SJeff Layton 		ceph_set_error_write(ci);
7150b98acd6SIlya Dryomov 		if (rc == -EBLOCKLISTED)
7160b98acd6SIlya Dryomov 			fsc->blocklisted = true;
71726544c62SJeff Layton 	} else {
71826544c62SJeff Layton 		ceph_clear_error_write(ci);
71926544c62SJeff Layton 	}
720e63dc5c7SYehuda Sadeh 
721e63dc5c7SYehuda Sadeh 	/*
722e63dc5c7SYehuda Sadeh 	 * We lost the cache cap, need to truncate the page before
723e63dc5c7SYehuda Sadeh 	 * it is unlocked, otherwise we'd truncate it later in the
724e63dc5c7SYehuda Sadeh 	 * page truncation thread, possibly losing some data that
725e63dc5c7SYehuda Sadeh 	 * raced its way in
726e63dc5c7SYehuda Sadeh 	 */
7275b64640cSYan, Zheng 	remove_page = !(ceph_caps_issued(ci) &
7285b64640cSYan, Zheng 			(CEPH_CAP_FILE_CACHE|CEPH_CAP_FILE_LAZYIO));
7295b64640cSYan, Zheng 
7305b64640cSYan, Zheng 	/* clean all pages */
7315b64640cSYan, Zheng 	for (i = 0; i < req->r_num_ops; i++) {
732642d51fbSXiubo Li 		if (req->r_ops[i].op != CEPH_OSD_OP_WRITE) {
733642d51fbSXiubo Li 			pr_warn("%s incorrect op %d req %p index %d tid %llu\n",
734642d51fbSXiubo Li 				__func__, req->r_ops[i].op, req, i, req->r_tid);
7355b64640cSYan, Zheng 			break;
736642d51fbSXiubo Li 		}
7375b64640cSYan, Zheng 
7385b64640cSYan, Zheng 		osd_data = osd_req_op_extent_osd_data(req, i);
7395b64640cSYan, Zheng 		BUG_ON(osd_data->type != CEPH_OSD_DATA_TYPE_PAGES);
740903f4fecSXiubo Li 		len += osd_data->length;
7415b64640cSYan, Zheng 		num_pages = calc_pages_for((u64)osd_data->alignment,
7425b64640cSYan, Zheng 					   (u64)osd_data->length);
7435b64640cSYan, Zheng 		total_pages += num_pages;
7445b64640cSYan, Zheng 		for (j = 0; j < num_pages; j++) {
7455b64640cSYan, Zheng 			page = osd_data->pages[j];
7465b64640cSYan, Zheng 			BUG_ON(!page);
7475b64640cSYan, Zheng 			WARN_ON(!PageUptodate(page));
7485b64640cSYan, Zheng 
7495b64640cSYan, Zheng 			if (atomic_long_dec_return(&fsc->writeback_count) <
7505b64640cSYan, Zheng 			     CONGESTION_OFF_THRESH(
7515b64640cSYan, Zheng 					fsc->mount_options->congestion_kb))
752503d4fa6SNeilBrown 				fsc->write_congested = false;
7535b64640cSYan, Zheng 
754379fc7faSJeff Layton 			ceph_put_snap_context(detach_page_private(page));
7555b64640cSYan, Zheng 			end_page_writeback(page);
756379fc7faSJeff Layton 			dout("unlocking %p\n", page);
7575b64640cSYan, Zheng 
7585b64640cSYan, Zheng 			if (remove_page)
7595b64640cSYan, Zheng 				generic_error_remove_page(inode->i_mapping,
7605b64640cSYan, Zheng 							  page);
761e63dc5c7SYehuda Sadeh 
7621d3576fdSSage Weil 			unlock_page(page);
7631d3576fdSSage Weil 		}
7645b64640cSYan, Zheng 		dout("writepages_finish %p wrote %llu bytes cleaned %d pages\n",
7655b64640cSYan, Zheng 		     inode, osd_data->length, rc >= 0 ? num_pages : 0);
7661d3576fdSSage Weil 
76796ac9158SJohn Hubbard 		release_pages(osd_data->pages, num_pages);
7685b64640cSYan, Zheng 	}
7695b64640cSYan, Zheng 
770903f4fecSXiubo Li 	ceph_update_write_metrics(&fsc->mdsc->metric, req->r_start_latency,
771903f4fecSXiubo Li 				  req->r_end_latency, len, rc);
772903f4fecSXiubo Li 
7735b64640cSYan, Zheng 	ceph_put_wrbuffer_cap_refs(ci, total_pages, snapc);
7745b64640cSYan, Zheng 
7755b64640cSYan, Zheng 	osd_data = osd_req_op_extent_osd_data(req, 0);
77687060c10SAlex Elder 	if (osd_data->pages_from_pool)
777a0102bdaSJeff Layton 		mempool_free(osd_data->pages, ceph_wb_pagevec_pool);
7781d3576fdSSage Weil 	else
77987060c10SAlex Elder 		kfree(osd_data->pages);
7801d3576fdSSage Weil 	ceph_osdc_put_request(req);
7811d3576fdSSage Weil }
7821d3576fdSSage Weil 
7831d3576fdSSage Weil /*
7841d3576fdSSage Weil  * initiate async writeback
7851d3576fdSSage Weil  */
7861d3576fdSSage Weil static int ceph_writepages_start(struct address_space *mapping,
7871d3576fdSSage Weil 				 struct writeback_control *wbc)
7881d3576fdSSage Weil {
7891d3576fdSSage Weil 	struct inode *inode = mapping->host;
7901d3576fdSSage Weil 	struct ceph_inode_info *ci = ceph_inode(inode);
791fc2744aaSYan, Zheng 	struct ceph_fs_client *fsc = ceph_inode_to_client(inode);
792fc2744aaSYan, Zheng 	struct ceph_vino vino = ceph_vino(inode);
7932a2d927eSYan, Zheng 	pgoff_t index, start_index, end = -1;
79480e755feSSage Weil 	struct ceph_snap_context *snapc = NULL, *last_snapc = NULL, *pgsnapc;
7951d3576fdSSage Weil 	struct pagevec pvec;
7961d3576fdSSage Weil 	int rc = 0;
79793407472SFabian Frederick 	unsigned int wsize = i_blocksize(inode);
7981d3576fdSSage Weil 	struct ceph_osd_request *req = NULL;
7991f934b00SYan, Zheng 	struct ceph_writeback_ctl ceph_wbc;
800590e9d98SYan, Zheng 	bool should_loop, range_whole = false;
801af9cc401SYan, Zheng 	bool done = false;
8021702e797SJeff Layton 	bool caching = ceph_is_cache_enabled(inode);
8031d3576fdSSage Weil 
804503d4fa6SNeilBrown 	if (wbc->sync_mode == WB_SYNC_NONE &&
805503d4fa6SNeilBrown 	    fsc->write_congested)
806503d4fa6SNeilBrown 		return 0;
807503d4fa6SNeilBrown 
8083fb99d48SYanhu Cao 	dout("writepages_start %p (mode=%s)\n", inode,
8091d3576fdSSage Weil 	     wbc->sync_mode == WB_SYNC_NONE ? "NONE" :
8101d3576fdSSage Weil 	     (wbc->sync_mode == WB_SYNC_ALL ? "ALL" : "HOLD"));
8111d3576fdSSage Weil 
8125d6451b1SJeff Layton 	if (ceph_inode_is_shutdown(inode)) {
8136c93df5dSYan, Zheng 		if (ci->i_wrbuffer_ref > 0) {
8146c93df5dSYan, Zheng 			pr_warn_ratelimited(
8156c93df5dSYan, Zheng 				"writepage_start %p %lld forced umount\n",
8166c93df5dSYan, Zheng 				inode, ceph_ino(inode));
8176c93df5dSYan, Zheng 		}
818a341d4dfSYan, Zheng 		mapping_set_error(mapping, -EIO);
8191d3576fdSSage Weil 		return -EIO; /* we're in a forced umount, don't write! */
8201d3576fdSSage Weil 	}
82195cca2b4SYan, Zheng 	if (fsc->mount_options->wsize < wsize)
8223d14c5d2SYehuda Sadeh 		wsize = fsc->mount_options->wsize;
8231d3576fdSSage Weil 
82486679820SMel Gorman 	pagevec_init(&pvec);
8251d3576fdSSage Weil 
826590e9d98SYan, Zheng 	start_index = wbc->range_cyclic ? mapping->writeback_index : 0;
827590e9d98SYan, Zheng 	index = start_index;
8281d3576fdSSage Weil 
8291d3576fdSSage Weil retry:
8301d3576fdSSage Weil 	/* find oldest snap context with dirty data */
83105455e11SYan, Zheng 	snapc = get_oldest_context(inode, &ceph_wbc, NULL);
8321d3576fdSSage Weil 	if (!snapc) {
8331d3576fdSSage Weil 		/* hmm, why does writepages get called when there
8341d3576fdSSage Weil 		   is no dirty data? */
8351d3576fdSSage Weil 		dout(" no snap context with dirty data?\n");
8361d3576fdSSage Weil 		goto out;
8371d3576fdSSage Weil 	}
8381d3576fdSSage Weil 	dout(" oldest snapc is %p seq %lld (%d snaps)\n",
8391d3576fdSSage Weil 	     snapc, snapc->seq, snapc->num_snaps);
840fc2744aaSYan, Zheng 
8412a2d927eSYan, Zheng 	should_loop = false;
8422a2d927eSYan, Zheng 	if (ceph_wbc.head_snapc && snapc != last_snapc) {
8432a2d927eSYan, Zheng 		/* where to start/end? */
8442a2d927eSYan, Zheng 		if (wbc->range_cyclic) {
8452a2d927eSYan, Zheng 			index = start_index;
8462a2d927eSYan, Zheng 			end = -1;
8472a2d927eSYan, Zheng 			if (index > 0)
8482a2d927eSYan, Zheng 				should_loop = true;
8492a2d927eSYan, Zheng 			dout(" cyclic, start at %lu\n", index);
8502a2d927eSYan, Zheng 		} else {
8512a2d927eSYan, Zheng 			index = wbc->range_start >> PAGE_SHIFT;
8522a2d927eSYan, Zheng 			end = wbc->range_end >> PAGE_SHIFT;
8532a2d927eSYan, Zheng 			if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX)
8542a2d927eSYan, Zheng 				range_whole = true;
8552a2d927eSYan, Zheng 			dout(" not cyclic, %lu to %lu\n", index, end);
8561d3576fdSSage Weil 		}
8572a2d927eSYan, Zheng 	} else if (!ceph_wbc.head_snapc) {
8582a2d927eSYan, Zheng 		/* Do not respect wbc->range_{start,end}. Dirty pages
8592a2d927eSYan, Zheng 		 * in that range can be associated with newer snapc.
8602a2d927eSYan, Zheng 		 * They are not writeable until we write all dirty pages
8612a2d927eSYan, Zheng 		 * associated with 'snapc' get written */
8621582af2eSYan, Zheng 		if (index > 0)
8632a2d927eSYan, Zheng 			should_loop = true;
8642a2d927eSYan, Zheng 		dout(" non-head snapc, range whole\n");
8652a2d927eSYan, Zheng 	}
8662a2d927eSYan, Zheng 
8672a2d927eSYan, Zheng 	ceph_put_snap_context(last_snapc);
8681d3576fdSSage Weil 	last_snapc = snapc;
8691d3576fdSSage Weil 
870af9cc401SYan, Zheng 	while (!done && index <= end) {
8715b64640cSYan, Zheng 		int num_ops = 0, op_idx;
8720e5ecac7SYan, Zheng 		unsigned i, pvec_pages, max_pages, locked_pages = 0;
8735b64640cSYan, Zheng 		struct page **pages = NULL, **data_pages;
8741d3576fdSSage Weil 		struct page *page;
8750e5ecac7SYan, Zheng 		pgoff_t strip_unit_end = 0;
8765b64640cSYan, Zheng 		u64 offset = 0, len = 0;
877a0102bdaSJeff Layton 		bool from_pool = false;
8781d3576fdSSage Weil 
8790e5ecac7SYan, Zheng 		max_pages = wsize >> PAGE_SHIFT;
8801d3576fdSSage Weil 
8811d3576fdSSage Weil get_more_pages:
8822e169296SJeff Layton 		pvec_pages = pagevec_lookup_range_tag(&pvec, mapping, &index,
8832e169296SJeff Layton 						end, PAGECACHE_TAG_DIRTY);
8840ed75fc8SJan Kara 		dout("pagevec_lookup_range_tag got %d\n", pvec_pages);
8851d3576fdSSage Weil 		if (!pvec_pages && !locked_pages)
8861d3576fdSSage Weil 			break;
8871d3576fdSSage Weil 		for (i = 0; i < pvec_pages && locked_pages < max_pages; i++) {
8881d3576fdSSage Weil 			page = pvec.pages[i];
8891d3576fdSSage Weil 			dout("? %p idx %lu\n", page, page->index);
8901d3576fdSSage Weil 			if (locked_pages == 0)
8911d3576fdSSage Weil 				lock_page(page);  /* first page */
8921d3576fdSSage Weil 			else if (!trylock_page(page))
8931d3576fdSSage Weil 				break;
8941d3576fdSSage Weil 
8951d3576fdSSage Weil 			/* only dirty pages, or our accounting breaks */
8961d3576fdSSage Weil 			if (unlikely(!PageDirty(page)) ||
8971d3576fdSSage Weil 			    unlikely(page->mapping != mapping)) {
8981d3576fdSSage Weil 				dout("!dirty or !mapping %p\n", page);
8991d3576fdSSage Weil 				unlock_page(page);
9000713e5f2SYan, Zheng 				continue;
9011d3576fdSSage Weil 			}
902af9cc401SYan, Zheng 			/* only if matching snap context */
903af9cc401SYan, Zheng 			pgsnapc = page_snap_context(page);
904af9cc401SYan, Zheng 			if (pgsnapc != snapc) {
905af9cc401SYan, Zheng 				dout("page snapc %p %lld != oldest %p %lld\n",
906af9cc401SYan, Zheng 				     pgsnapc, pgsnapc->seq, snapc, snapc->seq);
9071582af2eSYan, Zheng 				if (!should_loop &&
9081582af2eSYan, Zheng 				    !ceph_wbc.head_snapc &&
9091582af2eSYan, Zheng 				    wbc->sync_mode != WB_SYNC_NONE)
9101582af2eSYan, Zheng 					should_loop = true;
9111d3576fdSSage Weil 				unlock_page(page);
912af9cc401SYan, Zheng 				continue;
9131d3576fdSSage Weil 			}
9141f934b00SYan, Zheng 			if (page_offset(page) >= ceph_wbc.i_size) {
915a628304eSMatthew Wilcox (Oracle) 				struct folio *folio = page_folio(page);
916a628304eSMatthew Wilcox (Oracle) 
917a628304eSMatthew Wilcox (Oracle) 				dout("folio at %lu beyond eof %llu\n",
918a628304eSMatthew Wilcox (Oracle) 				     folio->index, ceph_wbc.i_size);
919c95f1c5fSErqi Chen 				if ((ceph_wbc.size_stable ||
920a628304eSMatthew Wilcox (Oracle) 				    folio_pos(folio) >= i_size_read(inode)) &&
921a628304eSMatthew Wilcox (Oracle) 				    folio_clear_dirty_for_io(folio))
922a628304eSMatthew Wilcox (Oracle) 					folio_invalidate(folio, 0,
923a628304eSMatthew Wilcox (Oracle) 							folio_size(folio));
924a628304eSMatthew Wilcox (Oracle) 				folio_unlock(folio);
925af9cc401SYan, Zheng 				continue;
926af9cc401SYan, Zheng 			}
927af9cc401SYan, Zheng 			if (strip_unit_end && (page->index > strip_unit_end)) {
928af9cc401SYan, Zheng 				dout("end of strip unit %p\n", page);
9291d3576fdSSage Weil 				unlock_page(page);
9301d3576fdSSage Weil 				break;
9311d3576fdSSage Weil 			}
9321702e797SJeff Layton 			if (PageWriteback(page) || PageFsCache(page)) {
9330713e5f2SYan, Zheng 				if (wbc->sync_mode == WB_SYNC_NONE) {
9341d3576fdSSage Weil 					dout("%p under writeback\n", page);
9351d3576fdSSage Weil 					unlock_page(page);
9360713e5f2SYan, Zheng 					continue;
9370713e5f2SYan, Zheng 				}
9380713e5f2SYan, Zheng 				dout("waiting on writeback %p\n", page);
9390713e5f2SYan, Zheng 				wait_on_page_writeback(page);
9401702e797SJeff Layton 				wait_on_page_fscache(page);
9411d3576fdSSage Weil 			}
9421d3576fdSSage Weil 
9431d3576fdSSage Weil 			if (!clear_page_dirty_for_io(page)) {
9441d3576fdSSage Weil 				dout("%p !clear_page_dirty_for_io\n", page);
9451d3576fdSSage Weil 				unlock_page(page);
9460713e5f2SYan, Zheng 				continue;
9471d3576fdSSage Weil 			}
9481d3576fdSSage Weil 
949e5975c7cSAlex Elder 			/*
950e5975c7cSAlex Elder 			 * We have something to write.  If this is
951e5975c7cSAlex Elder 			 * the first locked page this time through,
9525b64640cSYan, Zheng 			 * calculate max possinle write size and
9535b64640cSYan, Zheng 			 * allocate a page array
954e5975c7cSAlex Elder 			 */
9551d3576fdSSage Weil 			if (locked_pages == 0) {
9565b64640cSYan, Zheng 				u64 objnum;
9575b64640cSYan, Zheng 				u64 objoff;
958dccbf080SIlya Dryomov 				u32 xlen;
9595b64640cSYan, Zheng 
9601d3576fdSSage Weil 				/* prepare async write request */
9616285bc23SAlex Elder 				offset = (u64)page_offset(page);
962dccbf080SIlya Dryomov 				ceph_calc_file_object_mapping(&ci->i_layout,
963dccbf080SIlya Dryomov 							      offset, wsize,
9645b64640cSYan, Zheng 							      &objnum, &objoff,
965dccbf080SIlya Dryomov 							      &xlen);
966dccbf080SIlya Dryomov 				len = xlen;
9678c71897bSHenry C Chang 
9683fb99d48SYanhu Cao 				num_ops = 1;
9695b64640cSYan, Zheng 				strip_unit_end = page->index +
97009cbfeafSKirill A. Shutemov 					((len - 1) >> PAGE_SHIFT);
971715e4cd4SYan, Zheng 
9725b64640cSYan, Zheng 				BUG_ON(pages);
97388486957SAlex Elder 				max_pages = calc_pages_for(0, (u64)len);
9746da2ec56SKees Cook 				pages = kmalloc_array(max_pages,
9756da2ec56SKees Cook 						      sizeof(*pages),
976fc2744aaSYan, Zheng 						      GFP_NOFS);
97788486957SAlex Elder 				if (!pages) {
978a0102bdaSJeff Layton 					from_pool = true;
979a0102bdaSJeff Layton 					pages = mempool_alloc(ceph_wb_pagevec_pool, GFP_NOFS);
980e5975c7cSAlex Elder 					BUG_ON(!pages);
98188486957SAlex Elder 				}
9825b64640cSYan, Zheng 
9835b64640cSYan, Zheng 				len = 0;
9845b64640cSYan, Zheng 			} else if (page->index !=
98509cbfeafSKirill A. Shutemov 				   (offset + len) >> PAGE_SHIFT) {
986a0102bdaSJeff Layton 				if (num_ops >= (from_pool ?  CEPH_OSD_SLAB_OPS :
9875b64640cSYan, Zheng 							     CEPH_OSD_MAX_OPS)) {
9885b64640cSYan, Zheng 					redirty_page_for_writepage(wbc, page);
9895b64640cSYan, Zheng 					unlock_page(page);
9905b64640cSYan, Zheng 					break;
9915b64640cSYan, Zheng 				}
9925b64640cSYan, Zheng 
9935b64640cSYan, Zheng 				num_ops++;
9945b64640cSYan, Zheng 				offset = (u64)page_offset(page);
9955b64640cSYan, Zheng 				len = 0;
9961d3576fdSSage Weil 			}
9971d3576fdSSage Weil 
9981d3576fdSSage Weil 			/* note position of first page in pvec */
9991d3576fdSSage Weil 			dout("%p will write page %p idx %lu\n",
10001d3576fdSSage Weil 			     inode, page, page->index);
10012baba250SYehuda Sadeh 
10025b64640cSYan, Zheng 			if (atomic_long_inc_return(&fsc->writeback_count) >
10035b64640cSYan, Zheng 			    CONGESTION_ON_THRESH(
1004503d4fa6SNeilBrown 				    fsc->mount_options->congestion_kb))
1005503d4fa6SNeilBrown 				fsc->write_congested = true;
10060713e5f2SYan, Zheng 
10070713e5f2SYan, Zheng 			pages[locked_pages++] = page;
10080713e5f2SYan, Zheng 			pvec.pages[i] = NULL;
10090713e5f2SYan, Zheng 
10108ff2d290SJeff Layton 			len += thp_size(page);
10111d3576fdSSage Weil 		}
10121d3576fdSSage Weil 
10131d3576fdSSage Weil 		/* did we get anything? */
10141d3576fdSSage Weil 		if (!locked_pages)
10151d3576fdSSage Weil 			goto release_pvec_pages;
10161d3576fdSSage Weil 		if (i) {
10170713e5f2SYan, Zheng 			unsigned j, n = 0;
10180713e5f2SYan, Zheng 			/* shift unused page to beginning of pvec */
10190713e5f2SYan, Zheng 			for (j = 0; j < pvec_pages; j++) {
10200713e5f2SYan, Zheng 				if (!pvec.pages[j])
10210713e5f2SYan, Zheng 					continue;
10220713e5f2SYan, Zheng 				if (n < j)
10230713e5f2SYan, Zheng 					pvec.pages[n] = pvec.pages[j];
10240713e5f2SYan, Zheng 				n++;
10250713e5f2SYan, Zheng 			}
10260713e5f2SYan, Zheng 			pvec.nr = n;
10271d3576fdSSage Weil 
10281d3576fdSSage Weil 			if (pvec_pages && i == pvec_pages &&
10291d3576fdSSage Weil 			    locked_pages < max_pages) {
10301d3576fdSSage Weil 				dout("reached end pvec, trying for more\n");
10310713e5f2SYan, Zheng 				pagevec_release(&pvec);
10321d3576fdSSage Weil 				goto get_more_pages;
10331d3576fdSSage Weil 			}
10341d3576fdSSage Weil 		}
10351d3576fdSSage Weil 
10365b64640cSYan, Zheng new_request:
1037e5975c7cSAlex Elder 		offset = page_offset(pages[0]);
10385b64640cSYan, Zheng 		len = wsize;
10395b64640cSYan, Zheng 
10405b64640cSYan, Zheng 		req = ceph_osdc_new_request(&fsc->client->osdc,
10415b64640cSYan, Zheng 					&ci->i_layout, vino,
10425b64640cSYan, Zheng 					offset, &len, 0, num_ops,
10431f934b00SYan, Zheng 					CEPH_OSD_OP_WRITE, CEPH_OSD_FLAG_WRITE,
10441f934b00SYan, Zheng 					snapc, ceph_wbc.truncate_seq,
10451f934b00SYan, Zheng 					ceph_wbc.truncate_size, false);
10465b64640cSYan, Zheng 		if (IS_ERR(req)) {
10475b64640cSYan, Zheng 			req = ceph_osdc_new_request(&fsc->client->osdc,
10485b64640cSYan, Zheng 						&ci->i_layout, vino,
10495b64640cSYan, Zheng 						offset, &len, 0,
10505b64640cSYan, Zheng 						min(num_ops,
10515b64640cSYan, Zheng 						    CEPH_OSD_SLAB_OPS),
10525b64640cSYan, Zheng 						CEPH_OSD_OP_WRITE,
105354ea0046SIlya Dryomov 						CEPH_OSD_FLAG_WRITE,
10541f934b00SYan, Zheng 						snapc, ceph_wbc.truncate_seq,
10551f934b00SYan, Zheng 						ceph_wbc.truncate_size, true);
10565b64640cSYan, Zheng 			BUG_ON(IS_ERR(req));
10575b64640cSYan, Zheng 		}
10585b64640cSYan, Zheng 		BUG_ON(len < page_offset(pages[locked_pages - 1]) +
10598ff2d290SJeff Layton 			     thp_size(page) - offset);
10605b64640cSYan, Zheng 
10615b64640cSYan, Zheng 		req->r_callback = writepages_finish;
10625b64640cSYan, Zheng 		req->r_inode = inode;
10635b64640cSYan, Zheng 
10645b64640cSYan, Zheng 		/* Format the osd request message and submit the write */
10655b64640cSYan, Zheng 		len = 0;
10665b64640cSYan, Zheng 		data_pages = pages;
10675b64640cSYan, Zheng 		op_idx = 0;
10685b64640cSYan, Zheng 		for (i = 0; i < locked_pages; i++) {
10695b64640cSYan, Zheng 			u64 cur_offset = page_offset(pages[i]);
10701702e797SJeff Layton 			/*
10711702e797SJeff Layton 			 * Discontinuity in page range? Ceph can handle that by just passing
10721702e797SJeff Layton 			 * multiple extents in the write op.
10731702e797SJeff Layton 			 */
10745b64640cSYan, Zheng 			if (offset + len != cur_offset) {
10751702e797SJeff Layton 				/* If it's full, stop here */
10763fb99d48SYanhu Cao 				if (op_idx + 1 == req->r_num_ops)
10775b64640cSYan, Zheng 					break;
10781702e797SJeff Layton 
10791702e797SJeff Layton 				/* Kick off an fscache write with what we have so far. */
10801702e797SJeff Layton 				ceph_fscache_write_to_cache(inode, offset, len, caching);
10811702e797SJeff Layton 
10821702e797SJeff Layton 				/* Start a new extent */
10835b64640cSYan, Zheng 				osd_req_op_extent_dup_last(req, op_idx,
10845b64640cSYan, Zheng 							   cur_offset - offset);
10855b64640cSYan, Zheng 				dout("writepages got pages at %llu~%llu\n",
10865b64640cSYan, Zheng 				     offset, len);
10875b64640cSYan, Zheng 				osd_req_op_extent_osd_data_pages(req, op_idx,
10885b64640cSYan, Zheng 							data_pages, len, 0,
1089a0102bdaSJeff Layton 							from_pool, false);
10905b64640cSYan, Zheng 				osd_req_op_extent_update(req, op_idx, len);
10915b64640cSYan, Zheng 
10925b64640cSYan, Zheng 				len = 0;
10935b64640cSYan, Zheng 				offset = cur_offset;
10945b64640cSYan, Zheng 				data_pages = pages + i;
10955b64640cSYan, Zheng 				op_idx++;
10965b64640cSYan, Zheng 			}
10975b64640cSYan, Zheng 
10985b64640cSYan, Zheng 			set_page_writeback(pages[i]);
10991702e797SJeff Layton 			if (caching)
11001702e797SJeff Layton 				ceph_set_page_fscache(pages[i]);
11018ff2d290SJeff Layton 			len += thp_size(page);
11025b64640cSYan, Zheng 		}
11031702e797SJeff Layton 		ceph_fscache_write_to_cache(inode, offset, len, caching);
11045b64640cSYan, Zheng 
11051f934b00SYan, Zheng 		if (ceph_wbc.size_stable) {
11061f934b00SYan, Zheng 			len = min(len, ceph_wbc.i_size - offset);
11075b64640cSYan, Zheng 		} else if (i == locked_pages) {
1108e1966b49SYan, Zheng 			/* writepages_finish() clears writeback pages
1109e1966b49SYan, Zheng 			 * according to the data length, so make sure
1110e1966b49SYan, Zheng 			 * data length covers all locked pages */
11118ff2d290SJeff Layton 			u64 min_len = len + 1 - thp_size(page);
11121f934b00SYan, Zheng 			len = get_writepages_data_length(inode, pages[i - 1],
11131f934b00SYan, Zheng 							 offset);
11145b64640cSYan, Zheng 			len = max(len, min_len);
1115e1966b49SYan, Zheng 		}
11165b64640cSYan, Zheng 		dout("writepages got pages at %llu~%llu\n", offset, len);
11171d3576fdSSage Weil 
11185b64640cSYan, Zheng 		osd_req_op_extent_osd_data_pages(req, op_idx, data_pages, len,
1119a0102bdaSJeff Layton 						 0, from_pool, false);
11205b64640cSYan, Zheng 		osd_req_op_extent_update(req, op_idx, len);
1121e5975c7cSAlex Elder 
11225b64640cSYan, Zheng 		BUG_ON(op_idx + 1 != req->r_num_ops);
11235b64640cSYan, Zheng 
1124a0102bdaSJeff Layton 		from_pool = false;
11255b64640cSYan, Zheng 		if (i < locked_pages) {
11265b64640cSYan, Zheng 			BUG_ON(num_ops <= req->r_num_ops);
11275b64640cSYan, Zheng 			num_ops -= req->r_num_ops;
11285b64640cSYan, Zheng 			locked_pages -= i;
1129e5975c7cSAlex Elder 
11305b64640cSYan, Zheng 			/* allocate new pages array for next request */
11315b64640cSYan, Zheng 			data_pages = pages;
11326da2ec56SKees Cook 			pages = kmalloc_array(locked_pages, sizeof(*pages),
11335b64640cSYan, Zheng 					      GFP_NOFS);
11345b64640cSYan, Zheng 			if (!pages) {
1135a0102bdaSJeff Layton 				from_pool = true;
1136a0102bdaSJeff Layton 				pages = mempool_alloc(ceph_wb_pagevec_pool, GFP_NOFS);
11375b64640cSYan, Zheng 				BUG_ON(!pages);
11385b64640cSYan, Zheng 			}
11395b64640cSYan, Zheng 			memcpy(pages, data_pages + i,
11405b64640cSYan, Zheng 			       locked_pages * sizeof(*pages));
11415b64640cSYan, Zheng 			memset(data_pages + i, 0,
11425b64640cSYan, Zheng 			       locked_pages * sizeof(*pages));
11435b64640cSYan, Zheng 		} else {
11445b64640cSYan, Zheng 			BUG_ON(num_ops != req->r_num_ops);
11455b64640cSYan, Zheng 			index = pages[i - 1]->index + 1;
11465b64640cSYan, Zheng 			/* request message now owns the pages array */
11475b64640cSYan, Zheng 			pages = NULL;
11485b64640cSYan, Zheng 		}
1149e5975c7cSAlex Elder 
1150fac02ddfSArnd Bergmann 		req->r_mtime = inode->i_mtime;
1151a8af0d68SJeff Layton 		ceph_osdc_start_request(&fsc->client->osdc, req);
11521d3576fdSSage Weil 		req = NULL;
11531d3576fdSSage Weil 
11545b64640cSYan, Zheng 		wbc->nr_to_write -= i;
11555b64640cSYan, Zheng 		if (pages)
11565b64640cSYan, Zheng 			goto new_request;
11575b64640cSYan, Zheng 
11582a2d927eSYan, Zheng 		/*
11592a2d927eSYan, Zheng 		 * We stop writing back only if we are not doing
11602a2d927eSYan, Zheng 		 * integrity sync. In case of integrity sync we have to
11612a2d927eSYan, Zheng 		 * keep going until we have written all the pages
11622a2d927eSYan, Zheng 		 * we tagged for writeback prior to entering this loop.
11632a2d927eSYan, Zheng 		 */
11642a2d927eSYan, Zheng 		if (wbc->nr_to_write <= 0 && wbc->sync_mode == WB_SYNC_NONE)
1165af9cc401SYan, Zheng 			done = true;
11661d3576fdSSage Weil 
11671d3576fdSSage Weil release_pvec_pages:
11681d3576fdSSage Weil 		dout("pagevec_release on %d pages (%p)\n", (int)pvec.nr,
11691d3576fdSSage Weil 		     pvec.nr ? pvec.pages[0] : NULL);
11701d3576fdSSage Weil 		pagevec_release(&pvec);
11711d3576fdSSage Weil 	}
11721d3576fdSSage Weil 
11731d3576fdSSage Weil 	if (should_loop && !done) {
11741d3576fdSSage Weil 		/* more to do; loop back to beginning of file */
11751d3576fdSSage Weil 		dout("writepages looping back to beginning of file\n");
11762a2d927eSYan, Zheng 		end = start_index - 1; /* OK even when start_index == 0 */
1177f275635eSYan, Zheng 
1178f275635eSYan, Zheng 		/* to write dirty pages associated with next snapc,
1179f275635eSYan, Zheng 		 * we need to wait until current writes complete */
1180f275635eSYan, Zheng 		if (wbc->sync_mode != WB_SYNC_NONE &&
1181f275635eSYan, Zheng 		    start_index == 0 && /* all dirty pages were checked */
1182f275635eSYan, Zheng 		    !ceph_wbc.head_snapc) {
1183f275635eSYan, Zheng 			struct page *page;
1184f275635eSYan, Zheng 			unsigned i, nr;
1185f275635eSYan, Zheng 			index = 0;
1186f275635eSYan, Zheng 			while ((index <= end) &&
1187f275635eSYan, Zheng 			       (nr = pagevec_lookup_tag(&pvec, mapping, &index,
118867fd707fSJan Kara 						PAGECACHE_TAG_WRITEBACK))) {
1189f275635eSYan, Zheng 				for (i = 0; i < nr; i++) {
1190f275635eSYan, Zheng 					page = pvec.pages[i];
1191f275635eSYan, Zheng 					if (page_snap_context(page) != snapc)
1192f275635eSYan, Zheng 						continue;
1193f275635eSYan, Zheng 					wait_on_page_writeback(page);
1194f275635eSYan, Zheng 				}
1195f275635eSYan, Zheng 				pagevec_release(&pvec);
1196f275635eSYan, Zheng 				cond_resched();
1197f275635eSYan, Zheng 			}
1198f275635eSYan, Zheng 		}
1199f275635eSYan, Zheng 
12002a2d927eSYan, Zheng 		start_index = 0;
12011d3576fdSSage Weil 		index = 0;
12021d3576fdSSage Weil 		goto retry;
12031d3576fdSSage Weil 	}
12041d3576fdSSage Weil 
12051d3576fdSSage Weil 	if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0))
12061d3576fdSSage Weil 		mapping->writeback_index = index;
12071d3576fdSSage Weil 
12081d3576fdSSage Weil out:
12091d3576fdSSage Weil 	ceph_osdc_put_request(req);
12102a2d927eSYan, Zheng 	ceph_put_snap_context(last_snapc);
12112a2d927eSYan, Zheng 	dout("writepages dend - startone, rc = %d\n", rc);
12121d3576fdSSage Weil 	return rc;
12131d3576fdSSage Weil }
12141d3576fdSSage Weil 
12151d3576fdSSage Weil 
12161d3576fdSSage Weil 
12171d3576fdSSage Weil /*
12181d3576fdSSage Weil  * See if a given @snapc is either writeable, or already written.
12191d3576fdSSage Weil  */
12201d3576fdSSage Weil static int context_is_writeable_or_written(struct inode *inode,
12211d3576fdSSage Weil 					   struct ceph_snap_context *snapc)
12221d3576fdSSage Weil {
122305455e11SYan, Zheng 	struct ceph_snap_context *oldest = get_oldest_context(inode, NULL, NULL);
12246298a337SSage Weil 	int ret = !oldest || snapc->seq <= oldest->seq;
12256298a337SSage Weil 
12266298a337SSage Weil 	ceph_put_snap_context(oldest);
12276298a337SSage Weil 	return ret;
12281d3576fdSSage Weil }
12291d3576fdSSage Weil 
123018d620f0SJeff Layton /**
123118d620f0SJeff Layton  * ceph_find_incompatible - find an incompatible context and return it
123218d620f0SJeff Layton  * @page: page being dirtied
123318d620f0SJeff Layton  *
123418d620f0SJeff Layton  * We are only allowed to write into/dirty a page if the page is
123518d620f0SJeff Layton  * clean, or already dirty within the same snap context. Returns a
123618d620f0SJeff Layton  * conflicting context if there is one, NULL if there isn't, or a
123718d620f0SJeff Layton  * negative error code on other errors.
123818d620f0SJeff Layton  *
123918d620f0SJeff Layton  * Must be called with page lock held.
124018d620f0SJeff Layton  */
124118d620f0SJeff Layton static struct ceph_snap_context *
1242d45156bfSJeff Layton ceph_find_incompatible(struct page *page)
124318d620f0SJeff Layton {
1244d45156bfSJeff Layton 	struct inode *inode = page->mapping->host;
124518d620f0SJeff Layton 	struct ceph_inode_info *ci = ceph_inode(inode);
124618d620f0SJeff Layton 
12475d6451b1SJeff Layton 	if (ceph_inode_is_shutdown(inode)) {
12485d6451b1SJeff Layton 		dout(" page %p %llx:%llx is shutdown\n", page,
12495d6451b1SJeff Layton 		     ceph_vinop(inode));
12505d6451b1SJeff Layton 		return ERR_PTR(-ESTALE);
125118d620f0SJeff Layton 	}
125218d620f0SJeff Layton 
125318d620f0SJeff Layton 	for (;;) {
125418d620f0SJeff Layton 		struct ceph_snap_context *snapc, *oldest;
125518d620f0SJeff Layton 
125618d620f0SJeff Layton 		wait_on_page_writeback(page);
125718d620f0SJeff Layton 
125818d620f0SJeff Layton 		snapc = page_snap_context(page);
125918d620f0SJeff Layton 		if (!snapc || snapc == ci->i_head_snapc)
126018d620f0SJeff Layton 			break;
126118d620f0SJeff Layton 
126218d620f0SJeff Layton 		/*
126318d620f0SJeff Layton 		 * this page is already dirty in another (older) snap
126418d620f0SJeff Layton 		 * context!  is it writeable now?
126518d620f0SJeff Layton 		 */
126618d620f0SJeff Layton 		oldest = get_oldest_context(inode, NULL, NULL);
126718d620f0SJeff Layton 		if (snapc->seq > oldest->seq) {
126818d620f0SJeff Layton 			/* not writeable -- return it for the caller to deal with */
126918d620f0SJeff Layton 			ceph_put_snap_context(oldest);
127018d620f0SJeff Layton 			dout(" page %p snapc %p not current or oldest\n", page, snapc);
127118d620f0SJeff Layton 			return ceph_get_snap_context(snapc);
127218d620f0SJeff Layton 		}
127318d620f0SJeff Layton 		ceph_put_snap_context(oldest);
127418d620f0SJeff Layton 
127518d620f0SJeff Layton 		/* yay, writeable, do it now (without dropping page lock) */
127618d620f0SJeff Layton 		dout(" page %p snapc %p not current, but oldest\n", page, snapc);
127718d620f0SJeff Layton 		if (clear_page_dirty_for_io(page)) {
127818d620f0SJeff Layton 			int r = writepage_nounlock(page, NULL);
127918d620f0SJeff Layton 			if (r < 0)
128018d620f0SJeff Layton 				return ERR_PTR(r);
128118d620f0SJeff Layton 		}
128218d620f0SJeff Layton 	}
128318d620f0SJeff Layton 	return NULL;
128418d620f0SJeff Layton }
128518d620f0SJeff Layton 
1286d801327dSJeff Layton static int ceph_netfs_check_write_begin(struct file *file, loff_t pos, unsigned int len,
1287fac47b43SXiubo Li 					struct folio **foliop, void **_fsdata)
1288d801327dSJeff Layton {
1289d801327dSJeff Layton 	struct inode *inode = file_inode(file);
1290d801327dSJeff Layton 	struct ceph_inode_info *ci = ceph_inode(inode);
1291d801327dSJeff Layton 	struct ceph_snap_context *snapc;
1292d801327dSJeff Layton 
1293fac47b43SXiubo Li 	snapc = ceph_find_incompatible(folio_page(*foliop, 0));
1294d801327dSJeff Layton 	if (snapc) {
1295d801327dSJeff Layton 		int r;
1296d801327dSJeff Layton 
1297fac47b43SXiubo Li 		folio_unlock(*foliop);
1298fac47b43SXiubo Li 		folio_put(*foliop);
1299fac47b43SXiubo Li 		*foliop = NULL;
1300d801327dSJeff Layton 		if (IS_ERR(snapc))
1301d801327dSJeff Layton 			return PTR_ERR(snapc);
1302d801327dSJeff Layton 
1303d801327dSJeff Layton 		ceph_queue_writeback(inode);
1304d801327dSJeff Layton 		r = wait_event_killable(ci->i_cap_wq,
1305d801327dSJeff Layton 					context_is_writeable_or_written(inode, snapc));
1306d801327dSJeff Layton 		ceph_put_snap_context(snapc);
1307d801327dSJeff Layton 		return r == 0 ? -EAGAIN : r;
1308d801327dSJeff Layton 	}
1309d801327dSJeff Layton 	return 0;
1310d801327dSJeff Layton }
1311d801327dSJeff Layton 
13121d3576fdSSage Weil /*
13131d3576fdSSage Weil  * We are only allowed to write into/dirty the page if the page is
13141d3576fdSSage Weil  * clean, or already dirty within the same snap context.
13154af6b225SYehuda Sadeh  */
13164af6b225SYehuda Sadeh static int ceph_write_begin(struct file *file, struct address_space *mapping,
13179d6b0cd7SMatthew Wilcox (Oracle) 			    loff_t pos, unsigned len,
13184af6b225SYehuda Sadeh 			    struct page **pagep, void **fsdata)
13194af6b225SYehuda Sadeh {
1320496ad9aaSAl Viro 	struct inode *inode = file_inode(file);
1321e81fb419SLinus Torvalds 	struct ceph_inode_info *ci = ceph_inode(inode);
132278525c74SDavid Howells 	struct folio *folio = NULL;
1323d801327dSJeff Layton 	int r;
13244af6b225SYehuda Sadeh 
1325e81fb419SLinus Torvalds 	r = netfs_write_begin(&ci->netfs, file, inode->i_mapping, pos, len, &folio, NULL);
1326c460f4e4SXiubo Li 	if (r < 0)
1327c460f4e4SXiubo Li 		return r;
1328c460f4e4SXiubo Li 
132978525c74SDavid Howells 	folio_wait_fscache(folio);
133078525c74SDavid Howells 	WARN_ON_ONCE(!folio_test_locked(folio));
133178525c74SDavid Howells 	*pagep = &folio->page;
1332c460f4e4SXiubo Li 	return 0;
13334af6b225SYehuda Sadeh }
13344af6b225SYehuda Sadeh 
13354af6b225SYehuda Sadeh /*
13361d3576fdSSage Weil  * we don't do anything in here that simple_write_end doesn't do
13375dda377cSYan, Zheng  * except adjust dirty page accounting
13381d3576fdSSage Weil  */
13391d3576fdSSage Weil static int ceph_write_end(struct file *file, struct address_space *mapping,
13401d3576fdSSage Weil 			  loff_t pos, unsigned len, unsigned copied,
134178525c74SDavid Howells 			  struct page *subpage, void *fsdata)
13421d3576fdSSage Weil {
134378525c74SDavid Howells 	struct folio *folio = page_folio(subpage);
1344496ad9aaSAl Viro 	struct inode *inode = file_inode(file);
1345efb0ca76SYan, Zheng 	bool check_cap = false;
13461d3576fdSSage Weil 
134778525c74SDavid Howells 	dout("write_end file %p inode %p folio %p %d~%d (%d)\n", file,
134878525c74SDavid Howells 	     inode, folio, (int)pos, (int)copied, (int)len);
13491d3576fdSSage Weil 
135078525c74SDavid Howells 	if (!folio_test_uptodate(folio)) {
1351ce3a8732SJeff Layton 		/* just return that nothing was copied on a short copy */
1352b9de313cSAl Viro 		if (copied < len) {
1353b9de313cSAl Viro 			copied = 0;
1354b9de313cSAl Viro 			goto out;
1355b9de313cSAl Viro 		}
135678525c74SDavid Howells 		folio_mark_uptodate(folio);
1357b9de313cSAl Viro 	}
13581d3576fdSSage Weil 
13591d3576fdSSage Weil 	/* did file size increase? */
136099c88e69SYan, Zheng 	if (pos+copied > i_size_read(inode))
13611d3576fdSSage Weil 		check_cap = ceph_inode_set_size(inode, pos+copied);
13621d3576fdSSage Weil 
136378525c74SDavid Howells 	folio_mark_dirty(folio);
13641d3576fdSSage Weil 
1365b9de313cSAl Viro out:
136678525c74SDavid Howells 	folio_unlock(folio);
136778525c74SDavid Howells 	folio_put(folio);
13681d3576fdSSage Weil 
13691d3576fdSSage Weil 	if (check_cap)
1370*e4b731ccSXiubo Li 		ceph_check_caps(ceph_inode(inode), CHECK_CAPS_AUTHONLY);
13711d3576fdSSage Weil 
13721d3576fdSSage Weil 	return copied;
13731d3576fdSSage Weil }
13741d3576fdSSage Weil 
13751d3576fdSSage Weil const struct address_space_operations ceph_aops = {
13766c62371bSMatthew Wilcox (Oracle) 	.read_folio = netfs_read_folio,
1377bc899ee1SDavid Howells 	.readahead = netfs_readahead,
13781d3576fdSSage Weil 	.writepage = ceph_writepage,
13791d3576fdSSage Weil 	.writepages = ceph_writepages_start,
13801d3576fdSSage Weil 	.write_begin = ceph_write_begin,
13811d3576fdSSage Weil 	.write_end = ceph_write_end,
13828fb72b4aSMatthew Wilcox (Oracle) 	.dirty_folio = ceph_dirty_folio,
13839872f4deSMatthew Wilcox (Oracle) 	.invalidate_folio = ceph_invalidate_folio,
13845e414655SMatthew Wilcox (Oracle) 	.release_folio = ceph_release_folio,
13859c43ff44SJeff Layton 	.direct_IO = noop_direct_IO,
13861d3576fdSSage Weil };
13871d3576fdSSage Weil 
13884f7e89f6SYan, Zheng static void ceph_block_sigs(sigset_t *oldset)
13894f7e89f6SYan, Zheng {
13904f7e89f6SYan, Zheng 	sigset_t mask;
13914f7e89f6SYan, Zheng 	siginitsetinv(&mask, sigmask(SIGKILL));
13924f7e89f6SYan, Zheng 	sigprocmask(SIG_BLOCK, &mask, oldset);
13934f7e89f6SYan, Zheng }
13944f7e89f6SYan, Zheng 
13954f7e89f6SYan, Zheng static void ceph_restore_sigs(sigset_t *oldset)
13964f7e89f6SYan, Zheng {
13974f7e89f6SYan, Zheng 	sigprocmask(SIG_SETMASK, oldset, NULL);
13984f7e89f6SYan, Zheng }
13991d3576fdSSage Weil 
14001d3576fdSSage Weil /*
14011d3576fdSSage Weil  * vm ops
14021d3576fdSSage Weil  */
140324499847SSouptick Joarder static vm_fault_t ceph_filemap_fault(struct vm_fault *vmf)
140461f68816SYan, Zheng {
140511bac800SDave Jiang 	struct vm_area_struct *vma = vmf->vma;
140661f68816SYan, Zheng 	struct inode *inode = file_inode(vma->vm_file);
140761f68816SYan, Zheng 	struct ceph_inode_info *ci = ceph_inode(inode);
140861f68816SYan, Zheng 	struct ceph_file_info *fi = vma->vm_file->private_data;
1409c403c3a2SMatthew Wilcox (Oracle) 	loff_t off = (loff_t)vmf->pgoff << PAGE_SHIFT;
141024499847SSouptick Joarder 	int want, got, err;
14114f7e89f6SYan, Zheng 	sigset_t oldset;
141224499847SSouptick Joarder 	vm_fault_t ret = VM_FAULT_SIGBUS;
14134f7e89f6SYan, Zheng 
14145d6451b1SJeff Layton 	if (ceph_inode_is_shutdown(inode))
14155d6451b1SJeff Layton 		return ret;
14165d6451b1SJeff Layton 
14174f7e89f6SYan, Zheng 	ceph_block_sigs(&oldset);
141861f68816SYan, Zheng 
14198ff2d290SJeff Layton 	dout("filemap_fault %p %llx.%llx %llu trying to get caps\n",
14208ff2d290SJeff Layton 	     inode, ceph_vinop(inode), off);
142161f68816SYan, Zheng 	if (fi->fmode & CEPH_FILE_MODE_LAZY)
142261f68816SYan, Zheng 		want = CEPH_CAP_FILE_CACHE | CEPH_CAP_FILE_LAZYIO;
142361f68816SYan, Zheng 	else
142461f68816SYan, Zheng 		want = CEPH_CAP_FILE_CACHE;
14254f7e89f6SYan, Zheng 
142661f68816SYan, Zheng 	got = 0;
1427e72968e1SJeff Layton 	err = ceph_get_caps(vma->vm_file, CEPH_CAP_FILE_RD, want, -1, &got);
142824499847SSouptick Joarder 	if (err < 0)
14294f7e89f6SYan, Zheng 		goto out_restore;
14306ce026e4SYan, Zheng 
14318ff2d290SJeff Layton 	dout("filemap_fault %p %llu got cap refs on %s\n",
14328ff2d290SJeff Layton 	     inode, off, ceph_cap_string(got));
143361f68816SYan, Zheng 
143483701246SYan, Zheng 	if ((got & (CEPH_CAP_FILE_CACHE | CEPH_CAP_FILE_LAZYIO)) ||
143548490776SXiubo Li 	    !ceph_has_inline_data(ci)) {
14365d988308SYan, Zheng 		CEPH_DEFINE_RW_CONTEXT(rw_ctx, got);
14375d988308SYan, Zheng 		ceph_add_rw_context(fi, &rw_ctx);
143811bac800SDave Jiang 		ret = filemap_fault(vmf);
14395d988308SYan, Zheng 		ceph_del_rw_context(fi, &rw_ctx);
14408ff2d290SJeff Layton 		dout("filemap_fault %p %llu drop cap refs %s ret %x\n",
14418ff2d290SJeff Layton 		     inode, off, ceph_cap_string(got), ret);
14422b1ac852SYan, Zheng 	} else
144324499847SSouptick Joarder 		err = -EAGAIN;
144461f68816SYan, Zheng 
144561f68816SYan, Zheng 	ceph_put_cap_refs(ci, got);
144661f68816SYan, Zheng 
144724499847SSouptick Joarder 	if (err != -EAGAIN)
14484f7e89f6SYan, Zheng 		goto out_restore;
144983701246SYan, Zheng 
145083701246SYan, Zheng 	/* read inline data */
145109cbfeafSKirill A. Shutemov 	if (off >= PAGE_SIZE) {
145283701246SYan, Zheng 		/* does not support inline data > PAGE_SIZE */
145383701246SYan, Zheng 		ret = VM_FAULT_SIGBUS;
145483701246SYan, Zheng 	} else {
145583701246SYan, Zheng 		struct address_space *mapping = inode->i_mapping;
1456057ba5b2SJan Kara 		struct page *page;
1457057ba5b2SJan Kara 
1458057ba5b2SJan Kara 		filemap_invalidate_lock_shared(mapping);
1459057ba5b2SJan Kara 		page = find_or_create_page(mapping, 0,
1460057ba5b2SJan Kara 				mapping_gfp_constraint(mapping, ~__GFP_FS));
146183701246SYan, Zheng 		if (!page) {
146283701246SYan, Zheng 			ret = VM_FAULT_OOM;
14634f7e89f6SYan, Zheng 			goto out_inline;
146483701246SYan, Zheng 		}
146524499847SSouptick Joarder 		err = __ceph_do_getattr(inode, page,
146683701246SYan, Zheng 					 CEPH_STAT_CAP_INLINE_DATA, true);
146724499847SSouptick Joarder 		if (err < 0 || off >= i_size_read(inode)) {
146883701246SYan, Zheng 			unlock_page(page);
146909cbfeafSKirill A. Shutemov 			put_page(page);
1470c64a2b05SSouptick Joarder 			ret = vmf_error(err);
14714f7e89f6SYan, Zheng 			goto out_inline;
147283701246SYan, Zheng 		}
147324499847SSouptick Joarder 		if (err < PAGE_SIZE)
147424499847SSouptick Joarder 			zero_user_segment(page, err, PAGE_SIZE);
147583701246SYan, Zheng 		else
147683701246SYan, Zheng 			flush_dcache_page(page);
147783701246SYan, Zheng 		SetPageUptodate(page);
147883701246SYan, Zheng 		vmf->page = page;
147983701246SYan, Zheng 		ret = VM_FAULT_MAJOR | VM_FAULT_LOCKED;
14804f7e89f6SYan, Zheng out_inline:
1481057ba5b2SJan Kara 		filemap_invalidate_unlock_shared(mapping);
14828ff2d290SJeff Layton 		dout("filemap_fault %p %llu read inline data ret %x\n",
14838ff2d290SJeff Layton 		     inode, off, ret);
14844f7e89f6SYan, Zheng 	}
14854f7e89f6SYan, Zheng out_restore:
14864f7e89f6SYan, Zheng 	ceph_restore_sigs(&oldset);
148724499847SSouptick Joarder 	if (err < 0)
148824499847SSouptick Joarder 		ret = vmf_error(err);
14896ce026e4SYan, Zheng 
149061f68816SYan, Zheng 	return ret;
149161f68816SYan, Zheng }
14921d3576fdSSage Weil 
149324499847SSouptick Joarder static vm_fault_t ceph_page_mkwrite(struct vm_fault *vmf)
14941d3576fdSSage Weil {
149511bac800SDave Jiang 	struct vm_area_struct *vma = vmf->vma;
1496496ad9aaSAl Viro 	struct inode *inode = file_inode(vma->vm_file);
149761f68816SYan, Zheng 	struct ceph_inode_info *ci = ceph_inode(inode);
149861f68816SYan, Zheng 	struct ceph_file_info *fi = vma->vm_file->private_data;
1499f66fd9f0SYan, Zheng 	struct ceph_cap_flush *prealloc_cf;
150061f68816SYan, Zheng 	struct page *page = vmf->page;
15016285bc23SAlex Elder 	loff_t off = page_offset(page);
150261f68816SYan, Zheng 	loff_t size = i_size_read(inode);
150361f68816SYan, Zheng 	size_t len;
150424499847SSouptick Joarder 	int want, got, err;
15054f7e89f6SYan, Zheng 	sigset_t oldset;
150624499847SSouptick Joarder 	vm_fault_t ret = VM_FAULT_SIGBUS;
15071d3576fdSSage Weil 
15085d6451b1SJeff Layton 	if (ceph_inode_is_shutdown(inode))
15095d6451b1SJeff Layton 		return ret;
15105d6451b1SJeff Layton 
1511f66fd9f0SYan, Zheng 	prealloc_cf = ceph_alloc_cap_flush();
1512f66fd9f0SYan, Zheng 	if (!prealloc_cf)
15136ce026e4SYan, Zheng 		return VM_FAULT_OOM;
1514f66fd9f0SYan, Zheng 
1515249c1df5SJeff Layton 	sb_start_pagefault(inode->i_sb);
15164f7e89f6SYan, Zheng 	ceph_block_sigs(&oldset);
15171d3576fdSSage Weil 
15188ff2d290SJeff Layton 	if (off + thp_size(page) <= size)
15198ff2d290SJeff Layton 		len = thp_size(page);
15201d3576fdSSage Weil 	else
15218ff2d290SJeff Layton 		len = offset_in_thp(page, size);
15221d3576fdSSage Weil 
152361f68816SYan, Zheng 	dout("page_mkwrite %p %llx.%llx %llu~%zd getting caps i_size %llu\n",
152461f68816SYan, Zheng 	     inode, ceph_vinop(inode), off, len, size);
152561f68816SYan, Zheng 	if (fi->fmode & CEPH_FILE_MODE_LAZY)
152661f68816SYan, Zheng 		want = CEPH_CAP_FILE_BUFFER | CEPH_CAP_FILE_LAZYIO;
152761f68816SYan, Zheng 	else
152861f68816SYan, Zheng 		want = CEPH_CAP_FILE_BUFFER;
15294f7e89f6SYan, Zheng 
153061f68816SYan, Zheng 	got = 0;
1531e72968e1SJeff Layton 	err = ceph_get_caps(vma->vm_file, CEPH_CAP_FILE_WR, want, off + len, &got);
153224499847SSouptick Joarder 	if (err < 0)
1533f66fd9f0SYan, Zheng 		goto out_free;
15346ce026e4SYan, Zheng 
153561f68816SYan, Zheng 	dout("page_mkwrite %p %llu~%zd got cap refs on %s\n",
153661f68816SYan, Zheng 	     inode, off, len, ceph_cap_string(got));
153761f68816SYan, Zheng 
153861f68816SYan, Zheng 	/* Update time before taking page lock */
153961f68816SYan, Zheng 	file_update_time(vma->vm_file);
15405c308356SJeff Layton 	inode_inc_iversion_raw(inode);
15414af6b225SYehuda Sadeh 
1542f0b33df5SYan, Zheng 	do {
1543d45156bfSJeff Layton 		struct ceph_snap_context *snapc;
1544d45156bfSJeff Layton 
15454af6b225SYehuda Sadeh 		lock_page(page);
15464af6b225SYehuda Sadeh 
1547cb03c143SAndreas Gruenbacher 		if (page_mkwrite_check_truncate(page, inode) < 0) {
1548f9cac5acSYan, Zheng 			unlock_page(page);
15496ce026e4SYan, Zheng 			ret = VM_FAULT_NOPAGE;
1550f0b33df5SYan, Zheng 			break;
1551f9cac5acSYan, Zheng 		}
15524af6b225SYehuda Sadeh 
1553d45156bfSJeff Layton 		snapc = ceph_find_incompatible(page);
1554d45156bfSJeff Layton 		if (!snapc) {
15554af6b225SYehuda Sadeh 			/* success.  we'll keep the page locked. */
15561d3576fdSSage Weil 			set_page_dirty(page);
15571d3576fdSSage Weil 			ret = VM_FAULT_LOCKED;
1558d45156bfSJeff Layton 			break;
15591d3576fdSSage Weil 		}
1560d45156bfSJeff Layton 
1561d45156bfSJeff Layton 		unlock_page(page);
1562d45156bfSJeff Layton 
1563d45156bfSJeff Layton 		if (IS_ERR(snapc)) {
1564d45156bfSJeff Layton 			ret = VM_FAULT_SIGBUS;
1565d45156bfSJeff Layton 			break;
1566d45156bfSJeff Layton 		}
1567d45156bfSJeff Layton 
1568d45156bfSJeff Layton 		ceph_queue_writeback(inode);
1569d45156bfSJeff Layton 		err = wait_event_killable(ci->i_cap_wq,
1570d45156bfSJeff Layton 				context_is_writeable_or_written(inode, snapc));
1571d45156bfSJeff Layton 		ceph_put_snap_context(snapc);
1572d45156bfSJeff Layton 	} while (err == 0);
1573f0b33df5SYan, Zheng 
1574083db6fdSDavid Howells 	if (ret == VM_FAULT_LOCKED) {
157561f68816SYan, Zheng 		int dirty;
157661f68816SYan, Zheng 		spin_lock(&ci->i_ceph_lock);
1577f66fd9f0SYan, Zheng 		dirty = __ceph_mark_dirty_caps(ci, CEPH_CAP_FILE_WR,
1578f66fd9f0SYan, Zheng 					       &prealloc_cf);
157961f68816SYan, Zheng 		spin_unlock(&ci->i_ceph_lock);
158061f68816SYan, Zheng 		if (dirty)
158161f68816SYan, Zheng 			__mark_inode_dirty(inode, dirty);
158261f68816SYan, Zheng 	}
158361f68816SYan, Zheng 
158424499847SSouptick Joarder 	dout("page_mkwrite %p %llu~%zd dropping cap refs on %s ret %x\n",
158561f68816SYan, Zheng 	     inode, off, len, ceph_cap_string(got), ret);
1586a8810cdcSJeff Layton 	ceph_put_cap_refs_async(ci, got);
1587f66fd9f0SYan, Zheng out_free:
15884f7e89f6SYan, Zheng 	ceph_restore_sigs(&oldset);
1589249c1df5SJeff Layton 	sb_end_pagefault(inode->i_sb);
1590f66fd9f0SYan, Zheng 	ceph_free_cap_flush(prealloc_cf);
159124499847SSouptick Joarder 	if (err < 0)
159224499847SSouptick Joarder 		ret = vmf_error(err);
15931d3576fdSSage Weil 	return ret;
15941d3576fdSSage Weil }
15951d3576fdSSage Weil 
159631c542a1SYan, Zheng void ceph_fill_inline_data(struct inode *inode, struct page *locked_page,
159731c542a1SYan, Zheng 			   char	*data, size_t len)
159831c542a1SYan, Zheng {
159931c542a1SYan, Zheng 	struct address_space *mapping = inode->i_mapping;
160031c542a1SYan, Zheng 	struct page *page;
160131c542a1SYan, Zheng 
160231c542a1SYan, Zheng 	if (locked_page) {
160331c542a1SYan, Zheng 		page = locked_page;
160431c542a1SYan, Zheng 	} else {
160531c542a1SYan, Zheng 		if (i_size_read(inode) == 0)
160631c542a1SYan, Zheng 			return;
160731c542a1SYan, Zheng 		page = find_or_create_page(mapping, 0,
1608c62d2555SMichal Hocko 					   mapping_gfp_constraint(mapping,
1609c62d2555SMichal Hocko 					   ~__GFP_FS));
161031c542a1SYan, Zheng 		if (!page)
161131c542a1SYan, Zheng 			return;
161231c542a1SYan, Zheng 		if (PageUptodate(page)) {
161331c542a1SYan, Zheng 			unlock_page(page);
161409cbfeafSKirill A. Shutemov 			put_page(page);
161531c542a1SYan, Zheng 			return;
161631c542a1SYan, Zheng 		}
161731c542a1SYan, Zheng 	}
161831c542a1SYan, Zheng 
16190668ff52SIlya Dryomov 	dout("fill_inline_data %p %llx.%llx len %zu locked_page %p\n",
162031c542a1SYan, Zheng 	     inode, ceph_vinop(inode), len, locked_page);
162131c542a1SYan, Zheng 
162231c542a1SYan, Zheng 	if (len > 0) {
162331c542a1SYan, Zheng 		void *kaddr = kmap_atomic(page);
162431c542a1SYan, Zheng 		memcpy(kaddr, data, len);
162531c542a1SYan, Zheng 		kunmap_atomic(kaddr);
162631c542a1SYan, Zheng 	}
162731c542a1SYan, Zheng 
162831c542a1SYan, Zheng 	if (page != locked_page) {
162909cbfeafSKirill A. Shutemov 		if (len < PAGE_SIZE)
163009cbfeafSKirill A. Shutemov 			zero_user_segment(page, len, PAGE_SIZE);
163131c542a1SYan, Zheng 		else
163231c542a1SYan, Zheng 			flush_dcache_page(page);
163331c542a1SYan, Zheng 
163431c542a1SYan, Zheng 		SetPageUptodate(page);
163531c542a1SYan, Zheng 		unlock_page(page);
163609cbfeafSKirill A. Shutemov 		put_page(page);
163731c542a1SYan, Zheng 	}
163831c542a1SYan, Zheng }
163931c542a1SYan, Zheng 
1640083db6fdSDavid Howells int ceph_uninline_data(struct file *file)
164128127bddSYan, Zheng {
1642083db6fdSDavid Howells 	struct inode *inode = file_inode(file);
164328127bddSYan, Zheng 	struct ceph_inode_info *ci = ceph_inode(inode);
164428127bddSYan, Zheng 	struct ceph_fs_client *fsc = ceph_inode_to_client(inode);
1645825978fdSXiubo Li 	struct ceph_osd_request *req = NULL;
1646083db6fdSDavid Howells 	struct ceph_cap_flush *prealloc_cf;
1647083db6fdSDavid Howells 	struct folio *folio = NULL;
1648c38af982SDan Carpenter 	u64 inline_version = CEPH_INLINE_NONE;
1649083db6fdSDavid Howells 	struct page *pages[1];
165028127bddSYan, Zheng 	int err = 0;
1651c38af982SDan Carpenter 	u64 len;
1652083db6fdSDavid Howells 
165328127bddSYan, Zheng 	spin_lock(&ci->i_ceph_lock);
165428127bddSYan, Zheng 	inline_version = ci->i_inline_version;
165528127bddSYan, Zheng 	spin_unlock(&ci->i_ceph_lock);
165628127bddSYan, Zheng 
165728127bddSYan, Zheng 	dout("uninline_data %p %llx.%llx inline_version %llu\n",
165828127bddSYan, Zheng 	     inode, ceph_vinop(inode), inline_version);
165928127bddSYan, Zheng 
1660825978fdSXiubo Li 	if (inline_version == CEPH_INLINE_NONE)
1661825978fdSXiubo Li 		return 0;
1662825978fdSXiubo Li 
1663825978fdSXiubo Li 	prealloc_cf = ceph_alloc_cap_flush();
1664825978fdSXiubo Li 	if (!prealloc_cf)
1665825978fdSXiubo Li 		return -ENOMEM;
1666825978fdSXiubo Li 
1667825978fdSXiubo Li 	if (inline_version == 1) /* initial version, no data */
1668825978fdSXiubo Li 		goto out_uninline;
1669825978fdSXiubo Li 
1670825978fdSXiubo Li 	folio = read_mapping_folio(inode->i_mapping, 0, file);
1671825978fdSXiubo Li 	if (IS_ERR(folio)) {
1672825978fdSXiubo Li 		err = PTR_ERR(folio);
1673825978fdSXiubo Li 		goto out;
1674825978fdSXiubo Li 	}
1675825978fdSXiubo Li 
1676825978fdSXiubo Li 	folio_lock(folio);
167728127bddSYan, Zheng 
167828127bddSYan, Zheng 	len = i_size_read(inode);
1679083db6fdSDavid Howells 	if (len > folio_size(folio))
1680083db6fdSDavid Howells 		len = folio_size(folio);
168128127bddSYan, Zheng 
168228127bddSYan, Zheng 	req = ceph_osdc_new_request(&fsc->client->osdc, &ci->i_layout,
168328127bddSYan, Zheng 				    ceph_vino(inode), 0, &len, 0, 1,
168454ea0046SIlya Dryomov 				    CEPH_OSD_OP_CREATE, CEPH_OSD_FLAG_WRITE,
168534b759b4SIlya Dryomov 				    NULL, 0, 0, false);
168628127bddSYan, Zheng 	if (IS_ERR(req)) {
168728127bddSYan, Zheng 		err = PTR_ERR(req);
1688083db6fdSDavid Howells 		goto out_unlock;
168928127bddSYan, Zheng 	}
169028127bddSYan, Zheng 
1691fac02ddfSArnd Bergmann 	req->r_mtime = inode->i_mtime;
1692a8af0d68SJeff Layton 	ceph_osdc_start_request(&fsc->client->osdc, req);
169328127bddSYan, Zheng 	err = ceph_osdc_wait_request(&fsc->client->osdc, req);
169428127bddSYan, Zheng 	ceph_osdc_put_request(req);
169528127bddSYan, Zheng 	if (err < 0)
1696083db6fdSDavid Howells 		goto out_unlock;
169728127bddSYan, Zheng 
169828127bddSYan, Zheng 	req = ceph_osdc_new_request(&fsc->client->osdc, &ci->i_layout,
169928127bddSYan, Zheng 				    ceph_vino(inode), 0, &len, 1, 3,
170054ea0046SIlya Dryomov 				    CEPH_OSD_OP_WRITE, CEPH_OSD_FLAG_WRITE,
170134b759b4SIlya Dryomov 				    NULL, ci->i_truncate_seq,
170234b759b4SIlya Dryomov 				    ci->i_truncate_size, false);
170328127bddSYan, Zheng 	if (IS_ERR(req)) {
170428127bddSYan, Zheng 		err = PTR_ERR(req);
1705083db6fdSDavid Howells 		goto out_unlock;
170628127bddSYan, Zheng 	}
170728127bddSYan, Zheng 
1708083db6fdSDavid Howells 	pages[0] = folio_page(folio, 0);
1709083db6fdSDavid Howells 	osd_req_op_extent_osd_data_pages(req, 1, pages, len, 0, false, false);
171028127bddSYan, Zheng 
1711ec137c10SYan, Zheng 	{
1712ec137c10SYan, Zheng 		__le64 xattr_buf = cpu_to_le64(inline_version);
171328127bddSYan, Zheng 		err = osd_req_op_xattr_init(req, 0, CEPH_OSD_OP_CMPXATTR,
1714ec137c10SYan, Zheng 					    "inline_version", &xattr_buf,
1715ec137c10SYan, Zheng 					    sizeof(xattr_buf),
171628127bddSYan, Zheng 					    CEPH_OSD_CMPXATTR_OP_GT,
171728127bddSYan, Zheng 					    CEPH_OSD_CMPXATTR_MODE_U64);
171828127bddSYan, Zheng 		if (err)
1719083db6fdSDavid Howells 			goto out_put_req;
1720ec137c10SYan, Zheng 	}
172128127bddSYan, Zheng 
1722ec137c10SYan, Zheng 	{
1723ec137c10SYan, Zheng 		char xattr_buf[32];
1724ec137c10SYan, Zheng 		int xattr_len = snprintf(xattr_buf, sizeof(xattr_buf),
1725ec137c10SYan, Zheng 					 "%llu", inline_version);
172628127bddSYan, Zheng 		err = osd_req_op_xattr_init(req, 2, CEPH_OSD_OP_SETXATTR,
1727ec137c10SYan, Zheng 					    "inline_version",
1728ec137c10SYan, Zheng 					    xattr_buf, xattr_len, 0, 0);
172928127bddSYan, Zheng 		if (err)
1730083db6fdSDavid Howells 			goto out_put_req;
1731ec137c10SYan, Zheng 	}
173228127bddSYan, Zheng 
1733fac02ddfSArnd Bergmann 	req->r_mtime = inode->i_mtime;
1734a8af0d68SJeff Layton 	ceph_osdc_start_request(&fsc->client->osdc, req);
173528127bddSYan, Zheng 	err = ceph_osdc_wait_request(&fsc->client->osdc, req);
173697e27aaaSXiubo Li 
17378ae99ae2SXiubo Li 	ceph_update_write_metrics(&fsc->mdsc->metric, req->r_start_latency,
1738903f4fecSXiubo Li 				  req->r_end_latency, len, err);
173997e27aaaSXiubo Li 
1740825978fdSXiubo Li out_uninline:
1741083db6fdSDavid Howells 	if (!err) {
1742083db6fdSDavid Howells 		int dirty;
1743083db6fdSDavid Howells 
1744083db6fdSDavid Howells 		/* Set to CAP_INLINE_NONE and dirty the caps */
1745083db6fdSDavid Howells 		down_read(&fsc->mdsc->snap_rwsem);
1746083db6fdSDavid Howells 		spin_lock(&ci->i_ceph_lock);
1747083db6fdSDavid Howells 		ci->i_inline_version = CEPH_INLINE_NONE;
1748083db6fdSDavid Howells 		dirty = __ceph_mark_dirty_caps(ci, CEPH_CAP_FILE_WR, &prealloc_cf);
1749083db6fdSDavid Howells 		spin_unlock(&ci->i_ceph_lock);
1750083db6fdSDavid Howells 		up_read(&fsc->mdsc->snap_rwsem);
1751083db6fdSDavid Howells 		if (dirty)
1752083db6fdSDavid Howells 			__mark_inode_dirty(inode, dirty);
1753083db6fdSDavid Howells 	}
1754083db6fdSDavid Howells out_put_req:
175528127bddSYan, Zheng 	ceph_osdc_put_request(req);
175628127bddSYan, Zheng 	if (err == -ECANCELED)
175728127bddSYan, Zheng 		err = 0;
1758083db6fdSDavid Howells out_unlock:
1759825978fdSXiubo Li 	if (folio) {
1760083db6fdSDavid Howells 		folio_unlock(folio);
1761083db6fdSDavid Howells 		folio_put(folio);
1762825978fdSXiubo Li 	}
176328127bddSYan, Zheng out:
1764083db6fdSDavid Howells 	ceph_free_cap_flush(prealloc_cf);
176528127bddSYan, Zheng 	dout("uninline_data %p %llx.%llx inline_version %llu = %d\n",
176628127bddSYan, Zheng 	     inode, ceph_vinop(inode), inline_version, err);
176728127bddSYan, Zheng 	return err;
176828127bddSYan, Zheng }
176928127bddSYan, Zheng 
17707cbea8dcSKirill A. Shutemov static const struct vm_operations_struct ceph_vmops = {
177161f68816SYan, Zheng 	.fault		= ceph_filemap_fault,
17721d3576fdSSage Weil 	.page_mkwrite	= ceph_page_mkwrite,
17731d3576fdSSage Weil };
17741d3576fdSSage Weil 
17751d3576fdSSage Weil int ceph_mmap(struct file *file, struct vm_area_struct *vma)
17761d3576fdSSage Weil {
17771d3576fdSSage Weil 	struct address_space *mapping = file->f_mapping;
17781d3576fdSSage Weil 
17797e0a1265SMatthew Wilcox (Oracle) 	if (!mapping->a_ops->read_folio)
17801d3576fdSSage Weil 		return -ENOEXEC;
17811d3576fdSSage Weil 	vma->vm_ops = &ceph_vmops;
17821d3576fdSSage Weil 	return 0;
17831d3576fdSSage Weil }
178410183a69SYan, Zheng 
178510183a69SYan, Zheng enum {
178610183a69SYan, Zheng 	POOL_READ	= 1,
178710183a69SYan, Zheng 	POOL_WRITE	= 2,
178810183a69SYan, Zheng };
178910183a69SYan, Zheng 
1790779fe0fbSYan, Zheng static int __ceph_pool_perm_get(struct ceph_inode_info *ci,
1791779fe0fbSYan, Zheng 				s64 pool, struct ceph_string *pool_ns)
179210183a69SYan, Zheng {
1793874c8ca1SDavid Howells 	struct ceph_fs_client *fsc = ceph_inode_to_client(&ci->netfs.inode);
179410183a69SYan, Zheng 	struct ceph_mds_client *mdsc = fsc->mdsc;
179510183a69SYan, Zheng 	struct ceph_osd_request *rd_req = NULL, *wr_req = NULL;
179610183a69SYan, Zheng 	struct rb_node **p, *parent;
179710183a69SYan, Zheng 	struct ceph_pool_perm *perm;
179810183a69SYan, Zheng 	struct page **pages;
1799779fe0fbSYan, Zheng 	size_t pool_ns_len;
180010183a69SYan, Zheng 	int err = 0, err2 = 0, have = 0;
180110183a69SYan, Zheng 
180210183a69SYan, Zheng 	down_read(&mdsc->pool_perm_rwsem);
180310183a69SYan, Zheng 	p = &mdsc->pool_perm_tree.rb_node;
180410183a69SYan, Zheng 	while (*p) {
180510183a69SYan, Zheng 		perm = rb_entry(*p, struct ceph_pool_perm, node);
180610183a69SYan, Zheng 		if (pool < perm->pool)
180710183a69SYan, Zheng 			p = &(*p)->rb_left;
180810183a69SYan, Zheng 		else if (pool > perm->pool)
180910183a69SYan, Zheng 			p = &(*p)->rb_right;
181010183a69SYan, Zheng 		else {
1811779fe0fbSYan, Zheng 			int ret = ceph_compare_string(pool_ns,
1812779fe0fbSYan, Zheng 						perm->pool_ns,
1813779fe0fbSYan, Zheng 						perm->pool_ns_len);
1814779fe0fbSYan, Zheng 			if (ret < 0)
1815779fe0fbSYan, Zheng 				p = &(*p)->rb_left;
1816779fe0fbSYan, Zheng 			else if (ret > 0)
1817779fe0fbSYan, Zheng 				p = &(*p)->rb_right;
1818779fe0fbSYan, Zheng 			else {
181910183a69SYan, Zheng 				have = perm->perm;
182010183a69SYan, Zheng 				break;
182110183a69SYan, Zheng 			}
182210183a69SYan, Zheng 		}
1823779fe0fbSYan, Zheng 	}
182410183a69SYan, Zheng 	up_read(&mdsc->pool_perm_rwsem);
182510183a69SYan, Zheng 	if (*p)
182610183a69SYan, Zheng 		goto out;
182710183a69SYan, Zheng 
1828779fe0fbSYan, Zheng 	if (pool_ns)
1829779fe0fbSYan, Zheng 		dout("__ceph_pool_perm_get pool %lld ns %.*s no perm cached\n",
1830779fe0fbSYan, Zheng 		     pool, (int)pool_ns->len, pool_ns->str);
1831779fe0fbSYan, Zheng 	else
18327627151eSYan, Zheng 		dout("__ceph_pool_perm_get pool %lld no perm cached\n", pool);
183310183a69SYan, Zheng 
183410183a69SYan, Zheng 	down_write(&mdsc->pool_perm_rwsem);
1835779fe0fbSYan, Zheng 	p = &mdsc->pool_perm_tree.rb_node;
183610183a69SYan, Zheng 	parent = NULL;
183710183a69SYan, Zheng 	while (*p) {
183810183a69SYan, Zheng 		parent = *p;
183910183a69SYan, Zheng 		perm = rb_entry(parent, struct ceph_pool_perm, node);
184010183a69SYan, Zheng 		if (pool < perm->pool)
184110183a69SYan, Zheng 			p = &(*p)->rb_left;
184210183a69SYan, Zheng 		else if (pool > perm->pool)
184310183a69SYan, Zheng 			p = &(*p)->rb_right;
184410183a69SYan, Zheng 		else {
1845779fe0fbSYan, Zheng 			int ret = ceph_compare_string(pool_ns,
1846779fe0fbSYan, Zheng 						perm->pool_ns,
1847779fe0fbSYan, Zheng 						perm->pool_ns_len);
1848779fe0fbSYan, Zheng 			if (ret < 0)
1849779fe0fbSYan, Zheng 				p = &(*p)->rb_left;
1850779fe0fbSYan, Zheng 			else if (ret > 0)
1851779fe0fbSYan, Zheng 				p = &(*p)->rb_right;
1852779fe0fbSYan, Zheng 			else {
185310183a69SYan, Zheng 				have = perm->perm;
185410183a69SYan, Zheng 				break;
185510183a69SYan, Zheng 			}
185610183a69SYan, Zheng 		}
1857779fe0fbSYan, Zheng 	}
185810183a69SYan, Zheng 	if (*p) {
185910183a69SYan, Zheng 		up_write(&mdsc->pool_perm_rwsem);
186010183a69SYan, Zheng 		goto out;
186110183a69SYan, Zheng 	}
186210183a69SYan, Zheng 
186334b759b4SIlya Dryomov 	rd_req = ceph_osdc_alloc_request(&fsc->client->osdc, NULL,
186410183a69SYan, Zheng 					 1, false, GFP_NOFS);
186510183a69SYan, Zheng 	if (!rd_req) {
186610183a69SYan, Zheng 		err = -ENOMEM;
186710183a69SYan, Zheng 		goto out_unlock;
186810183a69SYan, Zheng 	}
186910183a69SYan, Zheng 
187010183a69SYan, Zheng 	rd_req->r_flags = CEPH_OSD_FLAG_READ;
187110183a69SYan, Zheng 	osd_req_op_init(rd_req, 0, CEPH_OSD_OP_STAT, 0);
187210183a69SYan, Zheng 	rd_req->r_base_oloc.pool = pool;
1873779fe0fbSYan, Zheng 	if (pool_ns)
1874779fe0fbSYan, Zheng 		rd_req->r_base_oloc.pool_ns = ceph_get_string(pool_ns);
1875d30291b9SIlya Dryomov 	ceph_oid_printf(&rd_req->r_base_oid, "%llx.00000000", ci->i_vino.ino);
187610183a69SYan, Zheng 
187713d1ad16SIlya Dryomov 	err = ceph_osdc_alloc_messages(rd_req, GFP_NOFS);
187813d1ad16SIlya Dryomov 	if (err)
187913d1ad16SIlya Dryomov 		goto out_unlock;
188010183a69SYan, Zheng 
188134b759b4SIlya Dryomov 	wr_req = ceph_osdc_alloc_request(&fsc->client->osdc, NULL,
188210183a69SYan, Zheng 					 1, false, GFP_NOFS);
188310183a69SYan, Zheng 	if (!wr_req) {
188410183a69SYan, Zheng 		err = -ENOMEM;
188510183a69SYan, Zheng 		goto out_unlock;
188610183a69SYan, Zheng 	}
188710183a69SYan, Zheng 
188854ea0046SIlya Dryomov 	wr_req->r_flags = CEPH_OSD_FLAG_WRITE;
188910183a69SYan, Zheng 	osd_req_op_init(wr_req, 0, CEPH_OSD_OP_CREATE, CEPH_OSD_OP_FLAG_EXCL);
189063244fa1SIlya Dryomov 	ceph_oloc_copy(&wr_req->r_base_oloc, &rd_req->r_base_oloc);
1891d30291b9SIlya Dryomov 	ceph_oid_copy(&wr_req->r_base_oid, &rd_req->r_base_oid);
189210183a69SYan, Zheng 
189313d1ad16SIlya Dryomov 	err = ceph_osdc_alloc_messages(wr_req, GFP_NOFS);
189413d1ad16SIlya Dryomov 	if (err)
189513d1ad16SIlya Dryomov 		goto out_unlock;
189610183a69SYan, Zheng 
189710183a69SYan, Zheng 	/* one page should be large enough for STAT data */
189810183a69SYan, Zheng 	pages = ceph_alloc_page_vector(1, GFP_KERNEL);
189910183a69SYan, Zheng 	if (IS_ERR(pages)) {
190010183a69SYan, Zheng 		err = PTR_ERR(pages);
190110183a69SYan, Zheng 		goto out_unlock;
190210183a69SYan, Zheng 	}
190310183a69SYan, Zheng 
190410183a69SYan, Zheng 	osd_req_op_raw_data_in_pages(rd_req, 0, pages, PAGE_SIZE,
190510183a69SYan, Zheng 				     0, false, true);
1906a8af0d68SJeff Layton 	ceph_osdc_start_request(&fsc->client->osdc, rd_req);
190710183a69SYan, Zheng 
1908874c8ca1SDavid Howells 	wr_req->r_mtime = ci->netfs.inode.i_mtime;
1909a8af0d68SJeff Layton 	ceph_osdc_start_request(&fsc->client->osdc, wr_req);
191010183a69SYan, Zheng 
191110183a69SYan, Zheng 	err = ceph_osdc_wait_request(&fsc->client->osdc, rd_req);
191210183a69SYan, Zheng 	err2 = ceph_osdc_wait_request(&fsc->client->osdc, wr_req);
191310183a69SYan, Zheng 
191410183a69SYan, Zheng 	if (err >= 0 || err == -ENOENT)
191510183a69SYan, Zheng 		have |= POOL_READ;
1916131d7eb4SYan, Zheng 	else if (err != -EPERM) {
19170b98acd6SIlya Dryomov 		if (err == -EBLOCKLISTED)
19180b98acd6SIlya Dryomov 			fsc->blocklisted = true;
191910183a69SYan, Zheng 		goto out_unlock;
1920131d7eb4SYan, Zheng 	}
192110183a69SYan, Zheng 
192210183a69SYan, Zheng 	if (err2 == 0 || err2 == -EEXIST)
192310183a69SYan, Zheng 		have |= POOL_WRITE;
192410183a69SYan, Zheng 	else if (err2 != -EPERM) {
19250b98acd6SIlya Dryomov 		if (err2 == -EBLOCKLISTED)
19260b98acd6SIlya Dryomov 			fsc->blocklisted = true;
192710183a69SYan, Zheng 		err = err2;
192810183a69SYan, Zheng 		goto out_unlock;
192910183a69SYan, Zheng 	}
193010183a69SYan, Zheng 
1931779fe0fbSYan, Zheng 	pool_ns_len = pool_ns ? pool_ns->len : 0;
1932779fe0fbSYan, Zheng 	perm = kmalloc(sizeof(*perm) + pool_ns_len + 1, GFP_NOFS);
193310183a69SYan, Zheng 	if (!perm) {
193410183a69SYan, Zheng 		err = -ENOMEM;
193510183a69SYan, Zheng 		goto out_unlock;
193610183a69SYan, Zheng 	}
193710183a69SYan, Zheng 
193810183a69SYan, Zheng 	perm->pool = pool;
193910183a69SYan, Zheng 	perm->perm = have;
1940779fe0fbSYan, Zheng 	perm->pool_ns_len = pool_ns_len;
1941779fe0fbSYan, Zheng 	if (pool_ns_len > 0)
1942779fe0fbSYan, Zheng 		memcpy(perm->pool_ns, pool_ns->str, pool_ns_len);
1943779fe0fbSYan, Zheng 	perm->pool_ns[pool_ns_len] = 0;
1944779fe0fbSYan, Zheng 
194510183a69SYan, Zheng 	rb_link_node(&perm->node, parent, p);
194610183a69SYan, Zheng 	rb_insert_color(&perm->node, &mdsc->pool_perm_tree);
194710183a69SYan, Zheng 	err = 0;
194810183a69SYan, Zheng out_unlock:
194910183a69SYan, Zheng 	up_write(&mdsc->pool_perm_rwsem);
195010183a69SYan, Zheng 
195110183a69SYan, Zheng 	ceph_osdc_put_request(rd_req);
195210183a69SYan, Zheng 	ceph_osdc_put_request(wr_req);
195310183a69SYan, Zheng out:
195410183a69SYan, Zheng 	if (!err)
195510183a69SYan, Zheng 		err = have;
1956779fe0fbSYan, Zheng 	if (pool_ns)
1957779fe0fbSYan, Zheng 		dout("__ceph_pool_perm_get pool %lld ns %.*s result = %d\n",
1958779fe0fbSYan, Zheng 		     pool, (int)pool_ns->len, pool_ns->str, err);
1959779fe0fbSYan, Zheng 	else
19607627151eSYan, Zheng 		dout("__ceph_pool_perm_get pool %lld result = %d\n", pool, err);
196110183a69SYan, Zheng 	return err;
196210183a69SYan, Zheng }
196310183a69SYan, Zheng 
19645e3ded1bSYan, Zheng int ceph_pool_perm_check(struct inode *inode, int need)
196510183a69SYan, Zheng {
19665e3ded1bSYan, Zheng 	struct ceph_inode_info *ci = ceph_inode(inode);
1967779fe0fbSYan, Zheng 	struct ceph_string *pool_ns;
19685e3ded1bSYan, Zheng 	s64 pool;
196910183a69SYan, Zheng 	int ret, flags;
197010183a69SYan, Zheng 
1971e9b22501SJeff Layton 	/* Only need to do this for regular files */
1972e9b22501SJeff Layton 	if (!S_ISREG(inode->i_mode))
1973e9b22501SJeff Layton 		return 0;
1974e9b22501SJeff Layton 
197580e80fbbSYan, Zheng 	if (ci->i_vino.snap != CEPH_NOSNAP) {
197680e80fbbSYan, Zheng 		/*
197780e80fbbSYan, Zheng 		 * Pool permission check needs to write to the first object.
197880e80fbbSYan, Zheng 		 * But for snapshot, head of the first object may have alread
197980e80fbbSYan, Zheng 		 * been deleted. Skip check to avoid creating orphan object.
198080e80fbbSYan, Zheng 		 */
198180e80fbbSYan, Zheng 		return 0;
198280e80fbbSYan, Zheng 	}
198380e80fbbSYan, Zheng 
19845e3ded1bSYan, Zheng 	if (ceph_test_mount_opt(ceph_inode_to_client(inode),
198510183a69SYan, Zheng 				NOPOOLPERM))
198610183a69SYan, Zheng 		return 0;
198710183a69SYan, Zheng 
198810183a69SYan, Zheng 	spin_lock(&ci->i_ceph_lock);
198910183a69SYan, Zheng 	flags = ci->i_ceph_flags;
19907627151eSYan, Zheng 	pool = ci->i_layout.pool_id;
199110183a69SYan, Zheng 	spin_unlock(&ci->i_ceph_lock);
199210183a69SYan, Zheng check:
199310183a69SYan, Zheng 	if (flags & CEPH_I_POOL_PERM) {
199410183a69SYan, Zheng 		if ((need & CEPH_CAP_FILE_RD) && !(flags & CEPH_I_POOL_RD)) {
19957627151eSYan, Zheng 			dout("ceph_pool_perm_check pool %lld no read perm\n",
199610183a69SYan, Zheng 			     pool);
199710183a69SYan, Zheng 			return -EPERM;
199810183a69SYan, Zheng 		}
199910183a69SYan, Zheng 		if ((need & CEPH_CAP_FILE_WR) && !(flags & CEPH_I_POOL_WR)) {
20007627151eSYan, Zheng 			dout("ceph_pool_perm_check pool %lld no write perm\n",
200110183a69SYan, Zheng 			     pool);
200210183a69SYan, Zheng 			return -EPERM;
200310183a69SYan, Zheng 		}
200410183a69SYan, Zheng 		return 0;
200510183a69SYan, Zheng 	}
200610183a69SYan, Zheng 
2007779fe0fbSYan, Zheng 	pool_ns = ceph_try_get_string(ci->i_layout.pool_ns);
2008779fe0fbSYan, Zheng 	ret = __ceph_pool_perm_get(ci, pool, pool_ns);
2009779fe0fbSYan, Zheng 	ceph_put_string(pool_ns);
201010183a69SYan, Zheng 	if (ret < 0)
201110183a69SYan, Zheng 		return ret;
201210183a69SYan, Zheng 
201310183a69SYan, Zheng 	flags = CEPH_I_POOL_PERM;
201410183a69SYan, Zheng 	if (ret & POOL_READ)
201510183a69SYan, Zheng 		flags |= CEPH_I_POOL_RD;
201610183a69SYan, Zheng 	if (ret & POOL_WRITE)
201710183a69SYan, Zheng 		flags |= CEPH_I_POOL_WR;
201810183a69SYan, Zheng 
201910183a69SYan, Zheng 	spin_lock(&ci->i_ceph_lock);
2020779fe0fbSYan, Zheng 	if (pool == ci->i_layout.pool_id &&
2021779fe0fbSYan, Zheng 	    pool_ns == rcu_dereference_raw(ci->i_layout.pool_ns)) {
2022779fe0fbSYan, Zheng 		ci->i_ceph_flags |= flags;
202310183a69SYan, Zheng         } else {
20247627151eSYan, Zheng 		pool = ci->i_layout.pool_id;
202510183a69SYan, Zheng 		flags = ci->i_ceph_flags;
202610183a69SYan, Zheng 	}
202710183a69SYan, Zheng 	spin_unlock(&ci->i_ceph_lock);
202810183a69SYan, Zheng 	goto check;
202910183a69SYan, Zheng }
203010183a69SYan, Zheng 
203110183a69SYan, Zheng void ceph_pool_perm_destroy(struct ceph_mds_client *mdsc)
203210183a69SYan, Zheng {
203310183a69SYan, Zheng 	struct ceph_pool_perm *perm;
203410183a69SYan, Zheng 	struct rb_node *n;
203510183a69SYan, Zheng 
203610183a69SYan, Zheng 	while (!RB_EMPTY_ROOT(&mdsc->pool_perm_tree)) {
203710183a69SYan, Zheng 		n = rb_first(&mdsc->pool_perm_tree);
203810183a69SYan, Zheng 		perm = rb_entry(n, struct ceph_pool_perm, node);
203910183a69SYan, Zheng 		rb_erase(n, &mdsc->pool_perm_tree);
204010183a69SYan, Zheng 		kfree(perm);
204110183a69SYan, Zheng 	}
204210183a69SYan, Zheng }
2043