xref: /openbmc/linux/fs/ceph/addr.c (revision dbfb5232)
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"
21f0fe1e54SJeff Layton #include "crypto.h"
223d14c5d2SYehuda Sadeh #include <linux/ceph/osd_client.h>
2308c1ac50SIlya Dryomov #include <linux/ceph/striper.h>
241d3576fdSSage Weil 
251d3576fdSSage Weil /*
261d3576fdSSage Weil  * Ceph address space ops.
271d3576fdSSage Weil  *
281d3576fdSSage Weil  * There are a few funny things going on here.
291d3576fdSSage Weil  *
301d3576fdSSage Weil  * The page->private field is used to reference a struct
311d3576fdSSage Weil  * ceph_snap_context for _every_ dirty page.  This indicates which
321d3576fdSSage Weil  * snapshot the page was logically dirtied in, and thus which snap
331d3576fdSSage Weil  * context needs to be associated with the osd write during writeback.
341d3576fdSSage Weil  *
351d3576fdSSage Weil  * Similarly, struct ceph_inode_info maintains a set of counters to
3625985edcSLucas De Marchi  * count dirty pages on the inode.  In the absence of snapshots,
371d3576fdSSage Weil  * i_wrbuffer_ref == i_wrbuffer_ref_head == the dirty page count.
381d3576fdSSage Weil  *
391d3576fdSSage Weil  * When a snapshot is taken (that is, when the client receives
401d3576fdSSage Weil  * notification that a snapshot was taken), each inode with caps and
411d3576fdSSage Weil  * with dirty pages (dirty pages implies there is a cap) gets a new
421d3576fdSSage Weil  * ceph_cap_snap in the i_cap_snaps list (which is sorted in ascending
431d3576fdSSage Weil  * order, new snaps go to the tail).  The i_wrbuffer_ref_head count is
441d3576fdSSage Weil  * moved to capsnap->dirty. (Unless a sync write is currently in
451d3576fdSSage Weil  * progress.  In that case, the capsnap is said to be "pending", new
461d3576fdSSage Weil  * writes cannot start, and the capsnap isn't "finalized" until the
471d3576fdSSage Weil  * write completes (or fails) and a final size/mtime for the inode for
481d3576fdSSage Weil  * that snap can be settled upon.)  i_wrbuffer_ref_head is reset to 0.
491d3576fdSSage Weil  *
501d3576fdSSage Weil  * On writeback, we must submit writes to the osd IN SNAP ORDER.  So,
511d3576fdSSage Weil  * we look for the first capsnap in i_cap_snaps and write out pages in
521d3576fdSSage Weil  * that snap context _only_.  Then we move on to the next capsnap,
531d3576fdSSage Weil  * eventually reaching the "live" or "head" context (i.e., pages that
541d3576fdSSage Weil  * are not yet snapped) and are writing the most recently dirtied
551d3576fdSSage Weil  * pages.
561d3576fdSSage Weil  *
571d3576fdSSage Weil  * Invalidate and so forth must take care to ensure the dirty page
581d3576fdSSage Weil  * accounting is preserved.
591d3576fdSSage Weil  */
601d3576fdSSage Weil 
612baba250SYehuda Sadeh #define CONGESTION_ON_THRESH(congestion_kb) (congestion_kb >> (PAGE_SHIFT-10))
622baba250SYehuda Sadeh #define CONGESTION_OFF_THRESH(congestion_kb)				\
632baba250SYehuda Sadeh 	(CONGESTION_ON_THRESH(congestion_kb) -				\
642baba250SYehuda Sadeh 	 (CONGESTION_ON_THRESH(congestion_kb) >> 2))
652baba250SYehuda Sadeh 
66d801327dSJeff Layton static int ceph_netfs_check_write_begin(struct file *file, loff_t pos, unsigned int len,
67fac47b43SXiubo Li 					struct folio **foliop, void **_fsdata);
68d801327dSJeff Layton 
page_snap_context(struct page * page)6961600ef8SYan, Zheng static inline struct ceph_snap_context *page_snap_context(struct page *page)
7061600ef8SYan, Zheng {
7161600ef8SYan, Zheng 	if (PagePrivate(page))
7261600ef8SYan, Zheng 		return (void *)page->private;
7361600ef8SYan, Zheng 	return NULL;
7461600ef8SYan, Zheng }
751d3576fdSSage Weil 
761d3576fdSSage Weil /*
771d3576fdSSage Weil  * Dirty a page.  Optimistically adjust accounting, on the assumption
781d3576fdSSage Weil  * that we won't race with invalidate.  If we do, readjust.
791d3576fdSSage Weil  */
ceph_dirty_folio(struct address_space * mapping,struct folio * folio)808fb72b4aSMatthew Wilcox (Oracle) static bool ceph_dirty_folio(struct address_space *mapping, struct folio *folio)
811d3576fdSSage Weil {
821d3576fdSSage Weil 	struct inode *inode;
831d3576fdSSage Weil 	struct ceph_inode_info *ci;
841d3576fdSSage Weil 	struct ceph_snap_context *snapc;
851d3576fdSSage Weil 
868fb72b4aSMatthew Wilcox (Oracle) 	if (folio_test_dirty(folio)) {
878fb72b4aSMatthew Wilcox (Oracle) 		dout("%p dirty_folio %p idx %lu -- already dirty\n",
888fb72b4aSMatthew Wilcox (Oracle) 		     mapping->host, folio, folio->index);
89642d51fbSXiubo Li 		VM_BUG_ON_FOLIO(!folio_test_private(folio), folio);
908fb72b4aSMatthew Wilcox (Oracle) 		return false;
911d3576fdSSage Weil 	}
921d3576fdSSage Weil 
931d3576fdSSage Weil 	inode = mapping->host;
941d3576fdSSage Weil 	ci = ceph_inode(inode);
951d3576fdSSage Weil 
961d3576fdSSage Weil 	/* dirty the head */
97be655596SSage Weil 	spin_lock(&ci->i_ceph_lock);
985dda377cSYan, Zheng 	BUG_ON(ci->i_wr_ref == 0); // caller should hold Fw reference
995dda377cSYan, Zheng 	if (__ceph_have_pending_cap_snap(ci)) {
1005dda377cSYan, Zheng 		struct ceph_cap_snap *capsnap =
1015dda377cSYan, Zheng 				list_last_entry(&ci->i_cap_snaps,
1025dda377cSYan, Zheng 						struct ceph_cap_snap,
1035dda377cSYan, Zheng 						ci_item);
1045dda377cSYan, Zheng 		snapc = ceph_get_snap_context(capsnap->context);
1055dda377cSYan, Zheng 		capsnap->dirty_pages++;
1065dda377cSYan, Zheng 	} else {
1075dda377cSYan, Zheng 		BUG_ON(!ci->i_head_snapc);
1085dda377cSYan, Zheng 		snapc = ceph_get_snap_context(ci->i_head_snapc);
1091d3576fdSSage Weil 		++ci->i_wrbuffer_ref_head;
1105dda377cSYan, Zheng 	}
1111d3576fdSSage Weil 	if (ci->i_wrbuffer_ref == 0)
1120444d76aSDave Chinner 		ihold(inode);
1131d3576fdSSage Weil 	++ci->i_wrbuffer_ref;
1148fb72b4aSMatthew Wilcox (Oracle) 	dout("%p dirty_folio %p idx %lu head %d/%d -> %d/%d "
1151d3576fdSSage Weil 	     "snapc %p seq %lld (%d snaps)\n",
1168fb72b4aSMatthew Wilcox (Oracle) 	     mapping->host, folio, folio->index,
1171d3576fdSSage Weil 	     ci->i_wrbuffer_ref-1, ci->i_wrbuffer_ref_head-1,
1181d3576fdSSage Weil 	     ci->i_wrbuffer_ref, ci->i_wrbuffer_ref_head,
1191d3576fdSSage Weil 	     snapc, snapc->seq, snapc->num_snaps);
120be655596SSage Weil 	spin_unlock(&ci->i_ceph_lock);
1211d3576fdSSage Weil 
1221d3576fdSSage Weil 	/*
1238fb72b4aSMatthew Wilcox (Oracle) 	 * Reference snap context in folio->private.  Also set
1249872f4deSMatthew Wilcox (Oracle) 	 * PagePrivate so that we get invalidate_folio callback.
1251d3576fdSSage Weil 	 */
126020bc44aSJeff Layton 	VM_WARN_ON_FOLIO(folio->private, folio);
1278fb72b4aSMatthew Wilcox (Oracle) 	folio_attach_private(folio, snapc);
1281d3576fdSSage Weil 
1298fb72b4aSMatthew Wilcox (Oracle) 	return ceph_fscache_dirty_folio(mapping, folio);
1301d3576fdSSage Weil }
1311d3576fdSSage Weil 
1321d3576fdSSage Weil /*
1339872f4deSMatthew Wilcox (Oracle)  * If we are truncating the full folio (i.e. offset == 0), adjust the
1349872f4deSMatthew Wilcox (Oracle)  * dirty folio counters appropriately.  Only called if there is private
1359872f4deSMatthew Wilcox (Oracle)  * data on the folio.
1361d3576fdSSage Weil  */
ceph_invalidate_folio(struct folio * folio,size_t offset,size_t length)1379872f4deSMatthew Wilcox (Oracle) static void ceph_invalidate_folio(struct folio *folio, size_t offset,
1389872f4deSMatthew Wilcox (Oracle) 				size_t length)
1391d3576fdSSage Weil {
1404ce1e9adSAlexander Beregalov 	struct inode *inode;
1411d3576fdSSage Weil 	struct ceph_inode_info *ci;
142379fc7faSJeff Layton 	struct ceph_snap_context *snapc;
1431d3576fdSSage Weil 
1449872f4deSMatthew Wilcox (Oracle) 	inode = folio->mapping->host;
145b150f5c1SMilosz Tanski 	ci = ceph_inode(inode);
146b150f5c1SMilosz Tanski 
1479872f4deSMatthew Wilcox (Oracle) 	if (offset != 0 || length != folio_size(folio)) {
1489872f4deSMatthew Wilcox (Oracle) 		dout("%p invalidate_folio idx %lu partial dirty page %zu~%zu\n",
1499872f4deSMatthew Wilcox (Oracle) 		     inode, folio->index, offset, length);
150b150f5c1SMilosz Tanski 		return;
151b150f5c1SMilosz Tanski 	}
1524ce1e9adSAlexander Beregalov 
1539872f4deSMatthew Wilcox (Oracle) 	WARN_ON(!folio_test_locked(folio));
154642d51fbSXiubo Li 	if (folio_test_private(folio)) {
1559872f4deSMatthew Wilcox (Oracle) 		dout("%p invalidate_folio idx %lu full dirty page\n",
1569872f4deSMatthew Wilcox (Oracle) 		     inode, folio->index);
157b150f5c1SMilosz Tanski 
1589872f4deSMatthew Wilcox (Oracle) 		snapc = folio_detach_private(folio);
1591d3576fdSSage Weil 		ceph_put_wrbuffer_cap_refs(ci, 1, snapc);
1601d3576fdSSage Weil 		ceph_put_snap_context(snapc);
1611d3576fdSSage Weil 	}
1621d3576fdSSage Weil 
1639872f4deSMatthew Wilcox (Oracle) 	folio_wait_fscache(folio);
164400e1286SJeff Layton }
165400e1286SJeff Layton 
ceph_release_folio(struct folio * folio,gfp_t gfp)1665e414655SMatthew Wilcox (Oracle) static bool ceph_release_folio(struct folio *folio, gfp_t gfp)
1671d3576fdSSage Weil {
1685e414655SMatthew Wilcox (Oracle) 	struct inode *inode = folio->mapping->host;
169400e1286SJeff Layton 
1705e414655SMatthew Wilcox (Oracle) 	dout("%llx:%llx release_folio idx %lu (%sdirty)\n",
1715e414655SMatthew Wilcox (Oracle) 	     ceph_vinop(inode),
1725e414655SMatthew Wilcox (Oracle) 	     folio->index, folio_test_dirty(folio) ? "" : "not ");
173400e1286SJeff Layton 
1745e414655SMatthew Wilcox (Oracle) 	if (folio_test_private(folio))
1755e414655SMatthew Wilcox (Oracle) 		return false;
17699ccbd22SMilosz Tanski 
1775e414655SMatthew Wilcox (Oracle) 	if (folio_test_fscache(folio)) {
178d7bdba1cSDavid Howells 		if (current_is_kswapd() || !(gfp & __GFP_FS))
1795e414655SMatthew Wilcox (Oracle) 			return false;
1805e414655SMatthew Wilcox (Oracle) 		folio_wait_fscache(folio);
1817c46b318SJeff Layton 	}
182400e1286SJeff Layton 	ceph_fscache_note_page_release(inode);
1835e414655SMatthew Wilcox (Oracle) 	return true;
1841d3576fdSSage Weil }
1851d3576fdSSage Weil 
ceph_netfs_expand_readahead(struct netfs_io_request * rreq)1866a19114bSDavid Howells static void ceph_netfs_expand_readahead(struct netfs_io_request *rreq)
187f0702876SJeff Layton {
188a25cedb4SJeff Layton 	struct inode *inode = rreq->inode;
189f0702876SJeff Layton 	struct ceph_inode_info *ci = ceph_inode(inode);
190f0702876SJeff Layton 	struct ceph_file_layout *lo = &ci->i_layout;
191dc94bb8fSXiubo Li 	unsigned long max_pages = inode->i_sb->s_bdi->ra_pages;
192dc94bb8fSXiubo Li 	loff_t end = rreq->start + rreq->len, new_end;
193dc94bb8fSXiubo Li 	struct ceph_netfs_request_data *priv = rreq->netfs_priv;
194dc94bb8fSXiubo Li 	unsigned long max_len;
195f0702876SJeff Layton 	u32 blockoff;
196f0702876SJeff Layton 
197dc94bb8fSXiubo Li 	if (priv) {
198dc94bb8fSXiubo Li 		/* Readahead is disabled by posix_fadvise POSIX_FADV_RANDOM */
199dc94bb8fSXiubo Li 		if (priv->file_ra_disabled)
200dc94bb8fSXiubo Li 			max_pages = 0;
201dc94bb8fSXiubo Li 		else
202dc94bb8fSXiubo Li 			max_pages = priv->file_ra_pages;
203dc94bb8fSXiubo Li 
204dc94bb8fSXiubo Li 	}
205dc94bb8fSXiubo Li 
206dc94bb8fSXiubo Li 	/* Readahead is disabled */
207dc94bb8fSXiubo Li 	if (!max_pages)
208dc94bb8fSXiubo Li 		return;
209dc94bb8fSXiubo Li 
210dc94bb8fSXiubo Li 	max_len = max_pages << PAGE_SHIFT;
211dc94bb8fSXiubo Li 
212dc94bb8fSXiubo Li 	/*
213dc94bb8fSXiubo Li 	 * Try to expand the length forward by rounding up it to the next
214dc94bb8fSXiubo Li 	 * block, but do not exceed the file size, unless the original
215dc94bb8fSXiubo Li 	 * request already exceeds it.
216dc94bb8fSXiubo Li 	 */
217dc94bb8fSXiubo Li 	new_end = min(round_up(end, lo->stripe_unit), rreq->i_size);
218dc94bb8fSXiubo Li 	if (new_end > end && new_end <= rreq->start + max_len)
219dc94bb8fSXiubo Li 		rreq->len = new_end - rreq->start;
220dc94bb8fSXiubo Li 
221dc94bb8fSXiubo Li 	/* Try to expand the start downward */
222dc94bb8fSXiubo Li 	div_u64_rem(rreq->start, lo->stripe_unit, &blockoff);
223dc94bb8fSXiubo Li 	if (rreq->len + blockoff <= max_len) {
224dc94bb8fSXiubo Li 		rreq->start -= blockoff;
225f0702876SJeff Layton 		rreq->len += blockoff;
226dc94bb8fSXiubo Li 	}
227f0702876SJeff Layton }
228f0702876SJeff Layton 
ceph_netfs_clamp_length(struct netfs_io_subrequest * subreq)2296a19114bSDavid Howells static bool ceph_netfs_clamp_length(struct netfs_io_subrequest *subreq)
230f0702876SJeff Layton {
231a25cedb4SJeff Layton 	struct inode *inode = subreq->rreq->inode;
232985b9ee8SXiubo Li 	struct ceph_fs_client *fsc = ceph_inode_to_fs_client(inode);
233f0702876SJeff Layton 	struct ceph_inode_info *ci = ceph_inode(inode);
234f0702876SJeff Layton 	u64 objno, objoff;
235f0702876SJeff Layton 	u32 xlen;
236f0702876SJeff Layton 
237f0702876SJeff Layton 	/* Truncate the extent at the end of the current block */
238f0702876SJeff Layton 	ceph_calc_file_object_mapping(&ci->i_layout, subreq->start, subreq->len,
239f0702876SJeff Layton 				      &objno, &objoff, &xlen);
240f0702876SJeff Layton 	subreq->len = min(xlen, fsc->mount_options->rsize);
241f0702876SJeff Layton 	return true;
242f0702876SJeff Layton }
243f0702876SJeff Layton 
finish_netfs_read(struct ceph_osd_request * req)244f0702876SJeff Layton static void finish_netfs_read(struct ceph_osd_request *req)
245f0702876SJeff Layton {
246f0fe1e54SJeff Layton 	struct inode *inode = req->r_inode;
247985b9ee8SXiubo Li 	struct ceph_fs_client *fsc = ceph_inode_to_fs_client(inode);
248f0702876SJeff Layton 	struct ceph_osd_data *osd_data = osd_req_op_extent_osd_data(req, 0);
2496a19114bSDavid Howells 	struct netfs_io_subrequest *subreq = req->r_priv;
25003bc06c7SJeff Layton 	struct ceph_osd_req_op *op = &req->r_ops[0];
251f0702876SJeff Layton 	int err = req->r_result;
25203bc06c7SJeff Layton 	bool sparse = (op->op == CEPH_OSD_OP_SPARSE_READ);
253f0702876SJeff Layton 
2548ae99ae2SXiubo Li 	ceph_update_read_metrics(&fsc->mdsc->metric, req->r_start_latency,
255903f4fecSXiubo Li 				 req->r_end_latency, osd_data->length, err);
256f0702876SJeff Layton 
257f0702876SJeff Layton 	dout("%s: result %d subreq->len=%zu i_size=%lld\n", __func__, req->r_result,
258f0702876SJeff Layton 	     subreq->len, i_size_read(req->r_inode));
259f0702876SJeff Layton 
260f0702876SJeff Layton 	/* no object means success but no data */
261f0fe1e54SJeff Layton 	if (err == -ENOENT)
262f0702876SJeff Layton 		err = 0;
263f0702876SJeff Layton 	else if (err == -EBLOCKLISTED)
264f0702876SJeff Layton 		fsc->blocklisted = true;
265f0702876SJeff Layton 
266f0fe1e54SJeff Layton 	if (err >= 0) {
267f0fe1e54SJeff Layton 		if (sparse && err > 0)
268f0fe1e54SJeff Layton 			err = ceph_sparse_ext_map_end(op);
269f0fe1e54SJeff Layton 		if (err < subreq->len)
270f0702876SJeff Layton 			__set_bit(NETFS_SREQ_CLEAR_TAIL, &subreq->flags);
271f0fe1e54SJeff Layton 		if (IS_ENCRYPTED(inode) && err > 0) {
272f0fe1e54SJeff Layton 			err = ceph_fscrypt_decrypt_extents(inode,
273f0fe1e54SJeff Layton 					osd_data->pages, subreq->start,
274f0fe1e54SJeff Layton 					op->extent.sparse_ext,
275f0fe1e54SJeff Layton 					op->extent.sparse_ext_cnt);
276f0fe1e54SJeff Layton 			if (err > subreq->len)
277f0fe1e54SJeff Layton 				err = subreq->len;
278f0fe1e54SJeff Layton 		}
279f0fe1e54SJeff Layton 	}
280f0702876SJeff Layton 
281f0fe1e54SJeff Layton 	if (osd_data->type == CEPH_OSD_DATA_TYPE_PAGES) {
282f0fe1e54SJeff Layton 		ceph_put_page_vector(osd_data->pages,
283f0fe1e54SJeff Layton 				     calc_pages_for(osd_data->alignment,
284f0fe1e54SJeff Layton 					osd_data->length), false);
285f0fe1e54SJeff Layton 	}
2867467b044SJeff Layton 	netfs_subreq_terminated(subreq, err, false);
287f0702876SJeff Layton 	iput(req->r_inode);
2881464de9fSXiubo Li 	ceph_dec_osd_stopping_blocker(fsc->mdsc);
289f0702876SJeff Layton }
290f0702876SJeff Layton 
ceph_netfs_issue_op_inline(struct netfs_io_subrequest * subreq)2916a19114bSDavid Howells static bool ceph_netfs_issue_op_inline(struct netfs_io_subrequest *subreq)
2925b19f1ebSDavid Howells {
2936a19114bSDavid Howells 	struct netfs_io_request *rreq = subreq->rreq;
2945b19f1ebSDavid Howells 	struct inode *inode = rreq->inode;
2955b19f1ebSDavid Howells 	struct ceph_mds_reply_info_parsed *rinfo;
2965b19f1ebSDavid Howells 	struct ceph_mds_reply_info_in *iinfo;
2975b19f1ebSDavid Howells 	struct ceph_mds_request *req;
2985b19f1ebSDavid Howells 	struct ceph_mds_client *mdsc = ceph_sb_to_mdsc(inode->i_sb);
2995b19f1ebSDavid Howells 	struct ceph_inode_info *ci = ceph_inode(inode);
3005b19f1ebSDavid Howells 	struct iov_iter iter;
3015b19f1ebSDavid Howells 	ssize_t err = 0;
3025b19f1ebSDavid Howells 	size_t len;
3035eed80fbSXiubo Li 	int mode;
3045b19f1ebSDavid Howells 
3055b19f1ebSDavid Howells 	__set_bit(NETFS_SREQ_CLEAR_TAIL, &subreq->flags);
306f18a3785SDavid Howells 	__clear_bit(NETFS_SREQ_COPY_TO_CACHE, &subreq->flags);
3075b19f1ebSDavid Howells 
3085b19f1ebSDavid Howells 	if (subreq->start >= inode->i_size)
3095b19f1ebSDavid Howells 		goto out;
3105b19f1ebSDavid Howells 
3115b19f1ebSDavid Howells 	/* We need to fetch the inline data. */
3125eed80fbSXiubo Li 	mode = ceph_try_to_choose_auth_mds(inode, CEPH_STAT_CAP_INLINE_DATA);
3135eed80fbSXiubo Li 	req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_GETATTR, mode);
3145b19f1ebSDavid Howells 	if (IS_ERR(req)) {
3155b19f1ebSDavid Howells 		err = PTR_ERR(req);
3165b19f1ebSDavid Howells 		goto out;
3175b19f1ebSDavid Howells 	}
3185b19f1ebSDavid Howells 	req->r_ino1 = ci->i_vino;
3195b19f1ebSDavid Howells 	req->r_args.getattr.mask = cpu_to_le32(CEPH_STAT_CAP_INLINE_DATA);
3205b19f1ebSDavid Howells 	req->r_num_caps = 2;
3215b19f1ebSDavid Howells 
3225b19f1ebSDavid Howells 	err = ceph_mdsc_do_request(mdsc, NULL, req);
3235b19f1ebSDavid Howells 	if (err < 0)
3245b19f1ebSDavid Howells 		goto out;
3255b19f1ebSDavid Howells 
3265b19f1ebSDavid Howells 	rinfo = &req->r_reply_info;
3275b19f1ebSDavid Howells 	iinfo = &rinfo->targeti;
3285b19f1ebSDavid Howells 	if (iinfo->inline_version == CEPH_INLINE_NONE) {
3295b19f1ebSDavid Howells 		/* The data got uninlined */
3305b19f1ebSDavid Howells 		ceph_mdsc_put_request(req);
3315b19f1ebSDavid Howells 		return false;
3325b19f1ebSDavid Howells 	}
3335b19f1ebSDavid Howells 
3345b19f1ebSDavid Howells 	len = min_t(size_t, iinfo->inline_len - subreq->start, subreq->len);
335de4eda9dSAl Viro 	iov_iter_xarray(&iter, ITER_DEST, &rreq->mapping->i_pages, subreq->start, len);
3365b19f1ebSDavid Howells 	err = copy_to_iter(iinfo->inline_data + subreq->start, len, &iter);
3375b19f1ebSDavid Howells 	if (err == 0)
3385b19f1ebSDavid Howells 		err = -EFAULT;
3395b19f1ebSDavid Howells 
3405b19f1ebSDavid Howells 	ceph_mdsc_put_request(req);
3415b19f1ebSDavid Howells out:
3425b19f1ebSDavid Howells 	netfs_subreq_terminated(subreq, err, false);
3435b19f1ebSDavid Howells 	return true;
3445b19f1ebSDavid Howells }
3455b19f1ebSDavid Howells 
ceph_netfs_issue_read(struct netfs_io_subrequest * subreq)346f18a3785SDavid Howells static void ceph_netfs_issue_read(struct netfs_io_subrequest *subreq)
347f0702876SJeff Layton {
3486a19114bSDavid Howells 	struct netfs_io_request *rreq = subreq->rreq;
349a25cedb4SJeff Layton 	struct inode *inode = rreq->inode;
350f0702876SJeff Layton 	struct ceph_inode_info *ci = ceph_inode(inode);
351985b9ee8SXiubo Li 	struct ceph_fs_client *fsc = ceph_inode_to_fs_client(inode);
352a68e564aSXiubo Li 	struct ceph_osd_request *req = NULL;
353f0702876SJeff Layton 	struct ceph_vino vino = ceph_vino(inode);
354f0702876SJeff Layton 	struct iov_iter iter;
355f0702876SJeff Layton 	int err = 0;
356f0702876SJeff Layton 	u64 len = subreq->len;
357f0fe1e54SJeff Layton 	bool sparse = IS_ENCRYPTED(inode) || ceph_test_mount_opt(fsc, SPARSEREAD);
358f0fe1e54SJeff Layton 	u64 off = subreq->start;
359f0702876SJeff Layton 
360a68e564aSXiubo Li 	if (ceph_inode_is_shutdown(inode)) {
361a68e564aSXiubo Li 		err = -EIO;
362a68e564aSXiubo Li 		goto out;
363a68e564aSXiubo Li 	}
364a68e564aSXiubo Li 
36548490776SXiubo Li 	if (ceph_has_inline_data(ci) && ceph_netfs_issue_op_inline(subreq))
3665b19f1ebSDavid Howells 		return;
3675b19f1ebSDavid Howells 
368f0fe1e54SJeff Layton 	ceph_fscrypt_adjust_off_and_len(inode, &off, &len);
369f0fe1e54SJeff Layton 
370f0fe1e54SJeff Layton 	req = ceph_osdc_new_request(&fsc->client->osdc, &ci->i_layout, vino,
371f0fe1e54SJeff Layton 			off, &len, 0, 1, sparse ? CEPH_OSD_OP_SPARSE_READ : CEPH_OSD_OP_READ,
372f0702876SJeff Layton 			CEPH_OSD_FLAG_READ | fsc->client->osdc.client->options->read_from_replica,
373f0702876SJeff Layton 			NULL, ci->i_truncate_seq, ci->i_truncate_size, false);
374f0702876SJeff Layton 	if (IS_ERR(req)) {
375f0702876SJeff Layton 		err = PTR_ERR(req);
376f0702876SJeff Layton 		req = NULL;
377f0702876SJeff Layton 		goto out;
378f0702876SJeff Layton 	}
379f0702876SJeff Layton 
38003bc06c7SJeff Layton 	if (sparse) {
38103bc06c7SJeff Layton 		err = ceph_alloc_sparse_ext_map(&req->r_ops[0]);
38203bc06c7SJeff Layton 		if (err)
38303bc06c7SJeff Layton 			goto out;
38403bc06c7SJeff Layton 	}
38503bc06c7SJeff Layton 
386f0702876SJeff Layton 	dout("%s: pos=%llu orig_len=%zu len=%llu\n", __func__, subreq->start, subreq->len, len);
387f0fe1e54SJeff Layton 
388de4eda9dSAl Viro 	iov_iter_xarray(&iter, ITER_DEST, &rreq->mapping->i_pages, subreq->start, len);
389f0fe1e54SJeff Layton 
390f0fe1e54SJeff Layton 	/*
391f0fe1e54SJeff Layton 	 * FIXME: For now, use CEPH_OSD_DATA_TYPE_PAGES instead of _ITER for
392f0fe1e54SJeff Layton 	 * encrypted inodes. We'd need infrastructure that handles an iov_iter
393f0fe1e54SJeff Layton 	 * instead of page arrays, and we don't have that as of yet. Once the
394f0fe1e54SJeff Layton 	 * dust settles on the write helpers and encrypt/decrypt routines for
395f0fe1e54SJeff Layton 	 * netfs, we should be able to rework this.
396f0fe1e54SJeff Layton 	 */
397f0fe1e54SJeff Layton 	if (IS_ENCRYPTED(inode)) {
398f0fe1e54SJeff Layton 		struct page **pages;
399f0fe1e54SJeff Layton 		size_t page_off;
400f0fe1e54SJeff Layton 
401f0fe1e54SJeff Layton 		err = iov_iter_get_pages_alloc2(&iter, &pages, len, &page_off);
402f0fe1e54SJeff Layton 		if (err < 0) {
403f0fe1e54SJeff Layton 			dout("%s: iov_ter_get_pages_alloc returned %d\n",
404f0fe1e54SJeff Layton 			     __func__, err);
405f0fe1e54SJeff Layton 			goto out;
406f0fe1e54SJeff Layton 		}
407f0fe1e54SJeff Layton 
408f0fe1e54SJeff Layton 		/* should always give us a page-aligned read */
409f0fe1e54SJeff Layton 		WARN_ON_ONCE(page_off);
410f0fe1e54SJeff Layton 		len = err;
411f0fe1e54SJeff Layton 		err = 0;
412f0fe1e54SJeff Layton 
413f0fe1e54SJeff Layton 		osd_req_op_extent_osd_data_pages(req, 0, pages, len, 0, false,
414f0fe1e54SJeff Layton 						 false);
415f0fe1e54SJeff Layton 	} else {
4164de77f25SJeff Layton 		osd_req_op_extent_osd_iter(req, 0, &iter);
417f0fe1e54SJeff Layton 	}
4181464de9fSXiubo Li 	if (!ceph_inc_osd_stopping_blocker(fsc->mdsc)) {
4191464de9fSXiubo Li 		err = -EIO;
4201464de9fSXiubo Li 		goto out;
4211464de9fSXiubo Li 	}
422f0702876SJeff Layton 	req->r_callback = finish_netfs_read;
423f0702876SJeff Layton 	req->r_priv = subreq;
424f0702876SJeff Layton 	req->r_inode = inode;
425f0702876SJeff Layton 	ihold(inode);
426f0702876SJeff Layton 
427a8af0d68SJeff Layton 	ceph_osdc_start_request(req->r_osdc, req);
428f0702876SJeff Layton out:
429f0702876SJeff Layton 	ceph_osdc_put_request(req);
430f0702876SJeff Layton 	if (err)
431f0702876SJeff Layton 		netfs_subreq_terminated(subreq, err, false);
432f0702876SJeff Layton 	dout("%s: result %d\n", __func__, err);
433f0702876SJeff Layton }
434f0702876SJeff Layton 
ceph_init_request(struct netfs_io_request * rreq,struct file * file)435a5c9dc44SDavid Howells static int ceph_init_request(struct netfs_io_request *rreq, struct file *file)
436a5c9dc44SDavid Howells {
437a5c9dc44SDavid Howells 	struct inode *inode = rreq->inode;
438a5c9dc44SDavid Howells 	int got = 0, want = CEPH_CAP_FILE_CACHE;
43923ee27dcSXiubo Li 	struct ceph_netfs_request_data *priv;
440a5c9dc44SDavid Howells 	int ret = 0;
441a5c9dc44SDavid Howells 
442a5c9dc44SDavid Howells 	if (rreq->origin != NETFS_READAHEAD)
443a5c9dc44SDavid Howells 		return 0;
444a5c9dc44SDavid Howells 
44523ee27dcSXiubo Li 	priv = kzalloc(sizeof(*priv), GFP_NOFS);
44623ee27dcSXiubo Li 	if (!priv)
44723ee27dcSXiubo Li 		return -ENOMEM;
44823ee27dcSXiubo Li 
449a5c9dc44SDavid Howells 	if (file) {
450a5c9dc44SDavid Howells 		struct ceph_rw_context *rw_ctx;
451a5c9dc44SDavid Howells 		struct ceph_file_info *fi = file->private_data;
452a5c9dc44SDavid Howells 
45323ee27dcSXiubo Li 		priv->file_ra_pages = file->f_ra.ra_pages;
45423ee27dcSXiubo Li 		priv->file_ra_disabled = file->f_mode & FMODE_RANDOM;
45523ee27dcSXiubo Li 
456a5c9dc44SDavid Howells 		rw_ctx = ceph_find_rw_context(fi);
45723ee27dcSXiubo Li 		if (rw_ctx) {
45823ee27dcSXiubo Li 			rreq->netfs_priv = priv;
459a5c9dc44SDavid Howells 			return 0;
460a5c9dc44SDavid Howells 		}
46123ee27dcSXiubo Li 	}
462a5c9dc44SDavid Howells 
463a5c9dc44SDavid Howells 	/*
464a5c9dc44SDavid Howells 	 * readahead callers do not necessarily hold Fcb caps
465a5c9dc44SDavid Howells 	 * (e.g. fadvise, madvise).
466a5c9dc44SDavid Howells 	 */
467a5c9dc44SDavid Howells 	ret = ceph_try_get_caps(inode, CEPH_CAP_FILE_RD, want, true, &got);
468a5c9dc44SDavid Howells 	if (ret < 0) {
469a5c9dc44SDavid Howells 		dout("start_read %p, error getting cap\n", inode);
47023ee27dcSXiubo Li 		goto out;
471a5c9dc44SDavid Howells 	}
472a5c9dc44SDavid Howells 
473a5c9dc44SDavid Howells 	if (!(got & want)) {
474a5c9dc44SDavid Howells 		dout("start_read %p, no cache cap\n", inode);
47523ee27dcSXiubo Li 		ret = -EACCES;
47623ee27dcSXiubo Li 		goto out;
477a5c9dc44SDavid Howells 	}
47823ee27dcSXiubo Li 	if (ret == 0) {
47923ee27dcSXiubo Li 		ret = -EACCES;
48023ee27dcSXiubo Li 		goto out;
48123ee27dcSXiubo Li 	}
482a5c9dc44SDavid Howells 
48323ee27dcSXiubo Li 	priv->caps = got;
48423ee27dcSXiubo Li 	rreq->netfs_priv = priv;
48523ee27dcSXiubo Li 
48623ee27dcSXiubo Li out:
48723ee27dcSXiubo Li 	if (ret < 0)
48823ee27dcSXiubo Li 		kfree(priv);
48923ee27dcSXiubo Li 
49023ee27dcSXiubo Li 	return ret;
491a5c9dc44SDavid Howells }
492a5c9dc44SDavid Howells 
ceph_netfs_free_request(struct netfs_io_request * rreq)49340a81101SDavid Howells static void ceph_netfs_free_request(struct netfs_io_request *rreq)
49449870056SJeff Layton {
49523ee27dcSXiubo Li 	struct ceph_netfs_request_data *priv = rreq->netfs_priv;
49649870056SJeff Layton 
49723ee27dcSXiubo Li 	if (!priv)
49823ee27dcSXiubo Li 		return;
49923ee27dcSXiubo Li 
50023ee27dcSXiubo Li 	if (priv->caps)
50123ee27dcSXiubo Li 		ceph_put_cap_refs(ceph_inode(rreq->inode), priv->caps);
50223ee27dcSXiubo Li 	kfree(priv);
50323ee27dcSXiubo Li 	rreq->netfs_priv = NULL;
50449870056SJeff Layton }
50549870056SJeff Layton 
506bc899ee1SDavid Howells const struct netfs_request_ops ceph_netfs_ops = {
507a5c9dc44SDavid Howells 	.init_request		= ceph_init_request,
50840a81101SDavid Howells 	.free_request		= ceph_netfs_free_request,
509f0702876SJeff Layton 	.begin_cache_operation	= ceph_begin_cache_operation,
510f18a3785SDavid Howells 	.issue_read		= ceph_netfs_issue_read,
511f0702876SJeff Layton 	.expand_readahead	= ceph_netfs_expand_readahead,
512f0702876SJeff Layton 	.clamp_length		= ceph_netfs_clamp_length,
513d801327dSJeff Layton 	.check_write_begin	= ceph_netfs_check_write_begin,
514f0702876SJeff Layton };
515f0702876SJeff Layton 
5161702e797SJeff Layton #ifdef CONFIG_CEPH_FSCACHE
ceph_set_page_fscache(struct page * page)5171702e797SJeff Layton static void ceph_set_page_fscache(struct page *page)
5181702e797SJeff Layton {
5191702e797SJeff Layton 	set_page_fscache(page);
5201702e797SJeff Layton }
5211702e797SJeff Layton 
ceph_fscache_write_terminated(void * priv,ssize_t error,bool was_async)5221702e797SJeff Layton static void ceph_fscache_write_terminated(void *priv, ssize_t error, bool was_async)
5231702e797SJeff Layton {
5241702e797SJeff Layton 	struct inode *inode = priv;
5251702e797SJeff Layton 
5261702e797SJeff Layton 	if (IS_ERR_VALUE(error) && error != -ENOBUFS)
5271702e797SJeff Layton 		ceph_fscache_invalidate(inode, false);
5281702e797SJeff Layton }
5291702e797SJeff Layton 
ceph_fscache_write_to_cache(struct inode * inode,u64 off,u64 len,bool caching)5301702e797SJeff Layton static void ceph_fscache_write_to_cache(struct inode *inode, u64 off, u64 len, bool caching)
5311702e797SJeff Layton {
5321702e797SJeff Layton 	struct ceph_inode_info *ci = ceph_inode(inode);
5331702e797SJeff Layton 	struct fscache_cookie *cookie = ceph_fscache_cookie(ci);
5341702e797SJeff Layton 
5351702e797SJeff Layton 	fscache_write_to_cache(cookie, inode->i_mapping, off, len, i_size_read(inode),
5361702e797SJeff Layton 			       ceph_fscache_write_terminated, inode, caching);
5371702e797SJeff Layton }
5381702e797SJeff Layton #else
ceph_set_page_fscache(struct page * page)5391702e797SJeff Layton static inline void ceph_set_page_fscache(struct page *page)
5401702e797SJeff Layton {
5411702e797SJeff Layton }
5421702e797SJeff Layton 
ceph_fscache_write_to_cache(struct inode * inode,u64 off,u64 len,bool caching)5431702e797SJeff Layton static inline void ceph_fscache_write_to_cache(struct inode *inode, u64 off, u64 len, bool caching)
5441702e797SJeff Layton {
5451702e797SJeff Layton }
5461702e797SJeff Layton #endif /* CONFIG_CEPH_FSCACHE */
5471702e797SJeff Layton 
5481f934b00SYan, Zheng struct ceph_writeback_ctl
5491f934b00SYan, Zheng {
5501f934b00SYan, Zheng 	loff_t i_size;
5511f934b00SYan, Zheng 	u64 truncate_size;
5521f934b00SYan, Zheng 	u32 truncate_seq;
5531f934b00SYan, Zheng 	bool size_stable;
5542a2d927eSYan, Zheng 	bool head_snapc;
5551f934b00SYan, Zheng };
5561f934b00SYan, Zheng 
5571d3576fdSSage Weil /*
5581d3576fdSSage Weil  * Get ref for the oldest snapc for an inode with dirty data... that is, the
5591d3576fdSSage Weil  * only snap context we are allowed to write back.
5601d3576fdSSage Weil  */
5611f934b00SYan, Zheng static struct ceph_snap_context *
get_oldest_context(struct inode * inode,struct ceph_writeback_ctl * ctl,struct ceph_snap_context * page_snapc)56205455e11SYan, Zheng get_oldest_context(struct inode *inode, struct ceph_writeback_ctl *ctl,
56305455e11SYan, Zheng 		   struct ceph_snap_context *page_snapc)
5641d3576fdSSage Weil {
5651d3576fdSSage Weil 	struct ceph_inode_info *ci = ceph_inode(inode);
5661d3576fdSSage Weil 	struct ceph_snap_context *snapc = NULL;
5671d3576fdSSage Weil 	struct ceph_cap_snap *capsnap = NULL;
5681d3576fdSSage Weil 
569be655596SSage Weil 	spin_lock(&ci->i_ceph_lock);
5701d3576fdSSage Weil 	list_for_each_entry(capsnap, &ci->i_cap_snaps, ci_item) {
5711d3576fdSSage Weil 		dout(" cap_snap %p snapc %p has %d dirty pages\n", capsnap,
5721d3576fdSSage Weil 		     capsnap->context, capsnap->dirty_pages);
57305455e11SYan, Zheng 		if (!capsnap->dirty_pages)
57405455e11SYan, Zheng 			continue;
57505455e11SYan, Zheng 
57605455e11SYan, Zheng 		/* get i_size, truncate_{seq,size} for page_snapc? */
57705455e11SYan, Zheng 		if (snapc && capsnap->context != page_snapc)
57805455e11SYan, Zheng 			continue;
57905455e11SYan, Zheng 
5801f934b00SYan, Zheng 		if (ctl) {
5811f934b00SYan, Zheng 			if (capsnap->writing) {
5821f934b00SYan, Zheng 				ctl->i_size = i_size_read(inode);
5831f934b00SYan, Zheng 				ctl->size_stable = false;
5841f934b00SYan, Zheng 			} else {
5851f934b00SYan, Zheng 				ctl->i_size = capsnap->size;
5861f934b00SYan, Zheng 				ctl->size_stable = true;
5871f934b00SYan, Zheng 			}
5881f934b00SYan, Zheng 			ctl->truncate_size = capsnap->truncate_size;
5891f934b00SYan, Zheng 			ctl->truncate_seq = capsnap->truncate_seq;
5902a2d927eSYan, Zheng 			ctl->head_snapc = false;
5911f934b00SYan, Zheng 		}
59205455e11SYan, Zheng 
59305455e11SYan, Zheng 		if (snapc)
5941d3576fdSSage Weil 			break;
59505455e11SYan, Zheng 
59605455e11SYan, Zheng 		snapc = ceph_get_snap_context(capsnap->context);
59705455e11SYan, Zheng 		if (!page_snapc ||
59805455e11SYan, Zheng 		    page_snapc == snapc ||
59905455e11SYan, Zheng 		    page_snapc->seq > snapc->seq)
60005455e11SYan, Zheng 			break;
6011d3576fdSSage Weil 	}
6027d8cb26dSSage Weil 	if (!snapc && ci->i_wrbuffer_ref_head) {
60380e755feSSage Weil 		snapc = ceph_get_snap_context(ci->i_head_snapc);
6041d3576fdSSage Weil 		dout(" head snapc %p has %d dirty pages\n",
6051d3576fdSSage Weil 		     snapc, ci->i_wrbuffer_ref_head);
6061f934b00SYan, Zheng 		if (ctl) {
6071f934b00SYan, Zheng 			ctl->i_size = i_size_read(inode);
6081f934b00SYan, Zheng 			ctl->truncate_size = ci->i_truncate_size;
6091f934b00SYan, Zheng 			ctl->truncate_seq = ci->i_truncate_seq;
6101f934b00SYan, Zheng 			ctl->size_stable = false;
6112a2d927eSYan, Zheng 			ctl->head_snapc = true;
6121f934b00SYan, Zheng 		}
6131d3576fdSSage Weil 	}
614be655596SSage Weil 	spin_unlock(&ci->i_ceph_lock);
6151d3576fdSSage Weil 	return snapc;
6161d3576fdSSage Weil }
6171d3576fdSSage Weil 
get_writepages_data_length(struct inode * inode,struct page * page,u64 start)6181f934b00SYan, Zheng static u64 get_writepages_data_length(struct inode *inode,
6191f934b00SYan, Zheng 				      struct page *page, u64 start)
6201f934b00SYan, Zheng {
6211f934b00SYan, Zheng 	struct ceph_inode_info *ci = ceph_inode(inode);
622d5520771SJeff Layton 	struct ceph_snap_context *snapc;
6231f934b00SYan, Zheng 	struct ceph_cap_snap *capsnap = NULL;
6241f934b00SYan, Zheng 	u64 end = i_size_read(inode);
625d5520771SJeff Layton 	u64 ret;
6261f934b00SYan, Zheng 
627d5520771SJeff Layton 	snapc = page_snap_context(ceph_fscrypt_pagecache_page(page));
6281f934b00SYan, Zheng 	if (snapc != ci->i_head_snapc) {
6291f934b00SYan, Zheng 		bool found = false;
6301f934b00SYan, Zheng 		spin_lock(&ci->i_ceph_lock);
6311f934b00SYan, Zheng 		list_for_each_entry(capsnap, &ci->i_cap_snaps, ci_item) {
6321f934b00SYan, Zheng 			if (capsnap->context == snapc) {
6331f934b00SYan, Zheng 				if (!capsnap->writing)
6341f934b00SYan, Zheng 					end = capsnap->size;
6351f934b00SYan, Zheng 				found = true;
6361f934b00SYan, Zheng 				break;
6371f934b00SYan, Zheng 			}
6381f934b00SYan, Zheng 		}
6391f934b00SYan, Zheng 		spin_unlock(&ci->i_ceph_lock);
6401f934b00SYan, Zheng 		WARN_ON(!found);
6411f934b00SYan, Zheng 	}
642d5520771SJeff Layton 	if (end > ceph_fscrypt_page_offset(page) + thp_size(page))
643d5520771SJeff Layton 		end = ceph_fscrypt_page_offset(page) + thp_size(page);
644d5520771SJeff Layton 	ret = end > start ? end - start : 0;
645d5520771SJeff Layton 	if (ret && fscrypt_is_bounce_page(page))
646d5520771SJeff Layton 		ret = round_up(ret, CEPH_FSCRYPT_BLOCK_SIZE);
647d5520771SJeff Layton 	return ret;
6481f934b00SYan, Zheng }
6491f934b00SYan, Zheng 
6501d3576fdSSage Weil /*
6511d3576fdSSage Weil  * Write a single page, but leave the page locked.
6521d3576fdSSage Weil  *
653b72b13ebSJeff Layton  * If we get a write error, mark the mapping for error, but still adjust the
6541d3576fdSSage Weil  * dirty page accounting (i.e., page is no longer dirty).
6551d3576fdSSage Weil  */
writepage_nounlock(struct page * page,struct writeback_control * wbc)6561d3576fdSSage Weil static int writepage_nounlock(struct page *page, struct writeback_control *wbc)
6571d3576fdSSage Weil {
658a628304eSMatthew Wilcox (Oracle) 	struct folio *folio = page_folio(page);
6596390987fSJeff Layton 	struct inode *inode = page->mapping->host;
6606390987fSJeff Layton 	struct ceph_inode_info *ci = ceph_inode(inode);
661985b9ee8SXiubo Li 	struct ceph_fs_client *fsc = ceph_inode_to_fs_client(inode);
6626298a337SSage Weil 	struct ceph_snap_context *snapc, *oldest;
663fc2744aaSYan, Zheng 	loff_t page_off = page_offset(page);
6646390987fSJeff Layton 	int err;
6658ff2d290SJeff Layton 	loff_t len = thp_size(page);
666d5520771SJeff Layton 	loff_t wlen;
6671f934b00SYan, Zheng 	struct ceph_writeback_ctl ceph_wbc;
6686390987fSJeff Layton 	struct ceph_osd_client *osdc = &fsc->client->osdc;
6696390987fSJeff Layton 	struct ceph_osd_request *req;
6701702e797SJeff Layton 	bool caching = ceph_is_cache_enabled(inode);
671d5520771SJeff Layton 	struct page *bounce_page = NULL;
6721d3576fdSSage Weil 
6731d3576fdSSage Weil 	dout("writepage %p idx %lu\n", page, page->index);
6741d3576fdSSage Weil 
675a68e564aSXiubo Li 	if (ceph_inode_is_shutdown(inode))
676a68e564aSXiubo Li 		return -EIO;
677a68e564aSXiubo Li 
6781d3576fdSSage Weil 	/* verify this is a writeable snap context */
67961600ef8SYan, Zheng 	snapc = page_snap_context(page);
680d37b1d99SMarkus Elfring 	if (!snapc) {
6811d3576fdSSage Weil 		dout("writepage %p page %p not dirty?\n", inode, page);
68243986881SYan, Zheng 		return 0;
6831d3576fdSSage Weil 	}
68405455e11SYan, Zheng 	oldest = get_oldest_context(inode, &ceph_wbc, snapc);
6856298a337SSage Weil 	if (snapc->seq > oldest->seq) {
6861d3576fdSSage Weil 		dout("writepage %p page %p snapc %p not writeable - noop\n",
68761600ef8SYan, Zheng 		     inode, page, snapc);
6881d3576fdSSage Weil 		/* we should only noop if called by kswapd */
689fa71fefbSYan, Zheng 		WARN_ON(!(current->flags & PF_MEMALLOC));
6906298a337SSage Weil 		ceph_put_snap_context(oldest);
691fa71fefbSYan, Zheng 		redirty_page_for_writepage(wbc, page);
69243986881SYan, Zheng 		return 0;
6931d3576fdSSage Weil 	}
6946298a337SSage Weil 	ceph_put_snap_context(oldest);
6951d3576fdSSage Weil 
6961d3576fdSSage Weil 	/* is this a partial page at end of file? */
6971f934b00SYan, Zheng 	if (page_off >= ceph_wbc.i_size) {
698a628304eSMatthew Wilcox (Oracle) 		dout("folio at %lu beyond eof %llu\n", folio->index,
699a628304eSMatthew Wilcox (Oracle) 				ceph_wbc.i_size);
700a628304eSMatthew Wilcox (Oracle) 		folio_invalidate(folio, 0, folio_size(folio));
70143986881SYan, Zheng 		return 0;
702fc2744aaSYan, Zheng 	}
70343986881SYan, Zheng 
7041f934b00SYan, Zheng 	if (ceph_wbc.i_size < page_off + len)
7051f934b00SYan, Zheng 		len = ceph_wbc.i_size - page_off;
7061d3576fdSSage Weil 
707d5520771SJeff Layton 	wlen = IS_ENCRYPTED(inode) ? round_up(len, CEPH_FSCRYPT_BLOCK_SIZE) : len;
7086390987fSJeff Layton 	dout("writepage %p page %p index %lu on %llu~%llu snapc %p seq %lld\n",
709d5520771SJeff Layton 	     inode, page, page->index, page_off, wlen, snapc, snapc->seq);
7101d3576fdSSage Weil 
711314c4737SYan, Zheng 	if (atomic_long_inc_return(&fsc->writeback_count) >
7123d14c5d2SYehuda Sadeh 	    CONGESTION_ON_THRESH(fsc->mount_options->congestion_kb))
713503d4fa6SNeilBrown 		fsc->write_congested = true;
7142baba250SYehuda Sadeh 
715d5520771SJeff Layton 	req = ceph_osdc_new_request(osdc, &ci->i_layout, ceph_vino(inode),
716d5520771SJeff Layton 				    page_off, &wlen, 0, 1, CEPH_OSD_OP_WRITE,
717d5520771SJeff Layton 				    CEPH_OSD_FLAG_WRITE, snapc,
718d5520771SJeff Layton 				    ceph_wbc.truncate_seq,
719d5520771SJeff Layton 				    ceph_wbc.truncate_size, true);
7203459bd0cSXiubo Li 	if (IS_ERR(req)) {
7213459bd0cSXiubo Li 		redirty_page_for_writepage(wbc, page);
7226390987fSJeff Layton 		return PTR_ERR(req);
7233459bd0cSXiubo Li 	}
7241702e797SJeff Layton 
725d5520771SJeff Layton 	if (wlen < len)
726d5520771SJeff Layton 		len = wlen;
727d5520771SJeff Layton 
7281702e797SJeff Layton 	set_page_writeback(page);
7291702e797SJeff Layton 	if (caching)
7301702e797SJeff Layton 		ceph_set_page_fscache(page);
7311702e797SJeff Layton 	ceph_fscache_write_to_cache(inode, page_off, len, caching);
7326390987fSJeff Layton 
733d5520771SJeff Layton 	if (IS_ENCRYPTED(inode)) {
734d5520771SJeff Layton 		bounce_page = fscrypt_encrypt_pagecache_blocks(page,
735d5520771SJeff Layton 						    CEPH_FSCRYPT_BLOCK_SIZE, 0,
736d5520771SJeff Layton 						    GFP_NOFS);
737d5520771SJeff Layton 		if (IS_ERR(bounce_page)) {
738d5520771SJeff Layton 			redirty_page_for_writepage(wbc, page);
739d5520771SJeff Layton 			end_page_writeback(page);
740d5520771SJeff Layton 			ceph_osdc_put_request(req);
741d5520771SJeff Layton 			return PTR_ERR(bounce_page);
742d5520771SJeff Layton 		}
743d5520771SJeff Layton 	}
744d5520771SJeff Layton 
7456390987fSJeff Layton 	/* it may be a short write due to an object boundary */
7468ff2d290SJeff Layton 	WARN_ON_ONCE(len > thp_size(page));
747d5520771SJeff Layton 	osd_req_op_extent_osd_data_pages(req, 0,
748d5520771SJeff Layton 			bounce_page ? &bounce_page : &page, wlen, 0,
749d5520771SJeff Layton 			false, false);
750d5520771SJeff Layton 	dout("writepage %llu~%llu (%llu bytes, %sencrypted)\n",
751d5520771SJeff Layton 	     page_off, len, wlen, IS_ENCRYPTED(inode) ? "" : "not ");
7526390987fSJeff Layton 
7536390987fSJeff Layton 	req->r_mtime = inode->i_mtime;
754a8af0d68SJeff Layton 	ceph_osdc_start_request(osdc, req);
7556390987fSJeff Layton 	err = ceph_osdc_wait_request(osdc, req);
7566390987fSJeff Layton 
7578ae99ae2SXiubo Li 	ceph_update_write_metrics(&fsc->mdsc->metric, req->r_start_latency,
758903f4fecSXiubo Li 				  req->r_end_latency, len, err);
759d5520771SJeff Layton 	fscrypt_free_bounce_page(bounce_page);
7606390987fSJeff Layton 	ceph_osdc_put_request(req);
7616390987fSJeff Layton 	if (err == 0)
7626390987fSJeff Layton 		err = len;
7636390987fSJeff Layton 
7641d3576fdSSage Weil 	if (err < 0) {
765ad15ec06SYan, Zheng 		struct writeback_control tmp_wbc;
766ad15ec06SYan, Zheng 		if (!wbc)
767ad15ec06SYan, Zheng 			wbc = &tmp_wbc;
768ad15ec06SYan, Zheng 		if (err == -ERESTARTSYS) {
769ad15ec06SYan, Zheng 			/* killed by SIGKILL */
770ad15ec06SYan, Zheng 			dout("writepage interrupted page %p\n", page);
771ad15ec06SYan, Zheng 			redirty_page_for_writepage(wbc, page);
772ad15ec06SYan, Zheng 			end_page_writeback(page);
77343986881SYan, Zheng 			return err;
774ad15ec06SYan, Zheng 		}
7750b98acd6SIlya Dryomov 		if (err == -EBLOCKLISTED)
7760b98acd6SIlya Dryomov 			fsc->blocklisted = true;
777ad15ec06SYan, Zheng 		dout("writepage setting page/mapping error %d %p\n",
778ad15ec06SYan, Zheng 		     err, page);
7791d3576fdSSage Weil 		mapping_set_error(&inode->i_data, err);
7801d3576fdSSage Weil 		wbc->pages_skipped++;
7811d3576fdSSage Weil 	} else {
7821d3576fdSSage Weil 		dout("writepage cleaned page %p\n", page);
7831d3576fdSSage Weil 		err = 0;  /* vfs expects us to return 0 */
7841d3576fdSSage Weil 	}
785379fc7faSJeff Layton 	oldest = detach_page_private(page);
786379fc7faSJeff Layton 	WARN_ON_ONCE(oldest != snapc);
7871d3576fdSSage Weil 	end_page_writeback(page);
7881d3576fdSSage Weil 	ceph_put_wrbuffer_cap_refs(ci, 1, snapc);
7896298a337SSage Weil 	ceph_put_snap_context(snapc);  /* page's reference */
790314c4737SYan, Zheng 
791314c4737SYan, Zheng 	if (atomic_long_dec_return(&fsc->writeback_count) <
792314c4737SYan, Zheng 	    CONGESTION_OFF_THRESH(fsc->mount_options->congestion_kb))
793503d4fa6SNeilBrown 		fsc->write_congested = false;
794314c4737SYan, Zheng 
7951d3576fdSSage Weil 	return err;
7961d3576fdSSage Weil }
7971d3576fdSSage Weil 
ceph_writepage(struct page * page,struct writeback_control * wbc)7981d3576fdSSage Weil static int ceph_writepage(struct page *page, struct writeback_control *wbc)
7991d3576fdSSage Weil {
800dbd646a8SYehuda Sadeh 	int err;
801dbd646a8SYehuda Sadeh 	struct inode *inode = page->mapping->host;
802dbd646a8SYehuda Sadeh 	BUG_ON(!inode);
80370b666c3SSage Weil 	ihold(inode);
8041702e797SJeff Layton 
805503d4fa6SNeilBrown 	if (wbc->sync_mode == WB_SYNC_NONE &&
806dbfb5232SNeilBrown 	    ceph_inode_to_fs_client(inode)->write_congested) {
807dbfb5232SNeilBrown 		redirty_page_for_writepage(wbc, page);
808503d4fa6SNeilBrown 		return AOP_WRITEPAGE_ACTIVATE;
809dbfb5232SNeilBrown 	}
810503d4fa6SNeilBrown 
8111702e797SJeff Layton 	wait_on_page_fscache(page);
8121702e797SJeff Layton 
813dbd646a8SYehuda Sadeh 	err = writepage_nounlock(page, wbc);
814ad15ec06SYan, Zheng 	if (err == -ERESTARTSYS) {
815ad15ec06SYan, Zheng 		/* direct memory reclaimer was killed by SIGKILL. return 0
816ad15ec06SYan, Zheng 		 * to prevent caller from setting mapping/page error */
817ad15ec06SYan, Zheng 		err = 0;
818ad15ec06SYan, Zheng 	}
8191d3576fdSSage Weil 	unlock_page(page);
820dbd646a8SYehuda Sadeh 	iput(inode);
8211d3576fdSSage Weil 	return err;
8221d3576fdSSage Weil }
8231d3576fdSSage Weil 
8241d3576fdSSage Weil /*
8251d3576fdSSage Weil  * async writeback completion handler.
8261d3576fdSSage Weil  *
8271d3576fdSSage Weil  * If we get an error, set the mapping error bit, but not the individual
8281d3576fdSSage Weil  * page error bits.
8291d3576fdSSage Weil  */
writepages_finish(struct ceph_osd_request * req)83085e084feSIlya Dryomov static void writepages_finish(struct ceph_osd_request *req)
8311d3576fdSSage Weil {
8321d3576fdSSage Weil 	struct inode *inode = req->r_inode;
8331d3576fdSSage Weil 	struct ceph_inode_info *ci = ceph_inode(inode);
83487060c10SAlex Elder 	struct ceph_osd_data *osd_data;
8351d3576fdSSage Weil 	struct page *page;
8365b64640cSYan, Zheng 	int num_pages, total_pages = 0;
8375b64640cSYan, Zheng 	int i, j;
8385b64640cSYan, Zheng 	int rc = req->r_result;
8391d3576fdSSage Weil 	struct ceph_snap_context *snapc = req->r_snapc;
8401d3576fdSSage Weil 	struct address_space *mapping = inode->i_mapping;
841985b9ee8SXiubo Li 	struct ceph_fs_client *fsc = ceph_inode_to_fs_client(inode);
842903f4fecSXiubo Li 	unsigned int len = 0;
8435b64640cSYan, Zheng 	bool remove_page;
8441d3576fdSSage Weil 
8455b64640cSYan, Zheng 	dout("writepages_finish %p rc %d\n", inode, rc);
84626544c62SJeff Layton 	if (rc < 0) {
8471d3576fdSSage Weil 		mapping_set_error(mapping, rc);
84826544c62SJeff Layton 		ceph_set_error_write(ci);
8490b98acd6SIlya Dryomov 		if (rc == -EBLOCKLISTED)
8500b98acd6SIlya Dryomov 			fsc->blocklisted = true;
85126544c62SJeff Layton 	} else {
85226544c62SJeff Layton 		ceph_clear_error_write(ci);
85326544c62SJeff Layton 	}
854e63dc5c7SYehuda Sadeh 
855e63dc5c7SYehuda Sadeh 	/*
856e63dc5c7SYehuda Sadeh 	 * We lost the cache cap, need to truncate the page before
857e63dc5c7SYehuda Sadeh 	 * it is unlocked, otherwise we'd truncate it later in the
858e63dc5c7SYehuda Sadeh 	 * page truncation thread, possibly losing some data that
859e63dc5c7SYehuda Sadeh 	 * raced its way in
860e63dc5c7SYehuda Sadeh 	 */
8615b64640cSYan, Zheng 	remove_page = !(ceph_caps_issued(ci) &
8625b64640cSYan, Zheng 			(CEPH_CAP_FILE_CACHE|CEPH_CAP_FILE_LAZYIO));
8635b64640cSYan, Zheng 
8645b64640cSYan, Zheng 	/* clean all pages */
8655b64640cSYan, Zheng 	for (i = 0; i < req->r_num_ops; i++) {
866642d51fbSXiubo Li 		if (req->r_ops[i].op != CEPH_OSD_OP_WRITE) {
867642d51fbSXiubo Li 			pr_warn("%s incorrect op %d req %p index %d tid %llu\n",
868642d51fbSXiubo Li 				__func__, req->r_ops[i].op, req, i, req->r_tid);
8695b64640cSYan, Zheng 			break;
870642d51fbSXiubo Li 		}
8715b64640cSYan, Zheng 
8725b64640cSYan, Zheng 		osd_data = osd_req_op_extent_osd_data(req, i);
8735b64640cSYan, Zheng 		BUG_ON(osd_data->type != CEPH_OSD_DATA_TYPE_PAGES);
874903f4fecSXiubo Li 		len += osd_data->length;
8755b64640cSYan, Zheng 		num_pages = calc_pages_for((u64)osd_data->alignment,
8765b64640cSYan, Zheng 					   (u64)osd_data->length);
8775b64640cSYan, Zheng 		total_pages += num_pages;
8785b64640cSYan, Zheng 		for (j = 0; j < num_pages; j++) {
8795b64640cSYan, Zheng 			page = osd_data->pages[j];
880d5520771SJeff Layton 			if (fscrypt_is_bounce_page(page)) {
881d5520771SJeff Layton 				page = fscrypt_pagecache_page(page);
882d5520771SJeff Layton 				fscrypt_free_bounce_page(osd_data->pages[j]);
883d5520771SJeff Layton 				osd_data->pages[j] = page;
884d5520771SJeff Layton 			}
8855b64640cSYan, Zheng 			BUG_ON(!page);
8865b64640cSYan, Zheng 			WARN_ON(!PageUptodate(page));
8875b64640cSYan, Zheng 
8885b64640cSYan, Zheng 			if (atomic_long_dec_return(&fsc->writeback_count) <
8895b64640cSYan, Zheng 			     CONGESTION_OFF_THRESH(
8905b64640cSYan, Zheng 					fsc->mount_options->congestion_kb))
891503d4fa6SNeilBrown 				fsc->write_congested = false;
8925b64640cSYan, Zheng 
893379fc7faSJeff Layton 			ceph_put_snap_context(detach_page_private(page));
8945b64640cSYan, Zheng 			end_page_writeback(page);
895379fc7faSJeff Layton 			dout("unlocking %p\n", page);
8965b64640cSYan, Zheng 
8975b64640cSYan, Zheng 			if (remove_page)
8985b64640cSYan, Zheng 				generic_error_remove_page(inode->i_mapping,
8995b64640cSYan, Zheng 							  page);
900e63dc5c7SYehuda Sadeh 
9011d3576fdSSage Weil 			unlock_page(page);
9021d3576fdSSage Weil 		}
9035b64640cSYan, Zheng 		dout("writepages_finish %p wrote %llu bytes cleaned %d pages\n",
9045b64640cSYan, Zheng 		     inode, osd_data->length, rc >= 0 ? num_pages : 0);
9051d3576fdSSage Weil 
90696ac9158SJohn Hubbard 		release_pages(osd_data->pages, num_pages);
9075b64640cSYan, Zheng 	}
9085b64640cSYan, Zheng 
909903f4fecSXiubo Li 	ceph_update_write_metrics(&fsc->mdsc->metric, req->r_start_latency,
910903f4fecSXiubo Li 				  req->r_end_latency, len, rc);
911903f4fecSXiubo Li 
9125b64640cSYan, Zheng 	ceph_put_wrbuffer_cap_refs(ci, total_pages, snapc);
9135b64640cSYan, Zheng 
9145b64640cSYan, Zheng 	osd_data = osd_req_op_extent_osd_data(req, 0);
91587060c10SAlex Elder 	if (osd_data->pages_from_pool)
916a0102bdaSJeff Layton 		mempool_free(osd_data->pages, ceph_wb_pagevec_pool);
9171d3576fdSSage Weil 	else
91887060c10SAlex Elder 		kfree(osd_data->pages);
9191d3576fdSSage Weil 	ceph_osdc_put_request(req);
9201464de9fSXiubo Li 	ceph_dec_osd_stopping_blocker(fsc->mdsc);
9211d3576fdSSage Weil }
9221d3576fdSSage Weil 
9231d3576fdSSage Weil /*
9241d3576fdSSage Weil  * initiate async writeback
9251d3576fdSSage Weil  */
ceph_writepages_start(struct address_space * mapping,struct writeback_control * wbc)9261d3576fdSSage Weil static int ceph_writepages_start(struct address_space *mapping,
9271d3576fdSSage Weil 				 struct writeback_control *wbc)
9281d3576fdSSage Weil {
9291d3576fdSSage Weil 	struct inode *inode = mapping->host;
9301d3576fdSSage Weil 	struct ceph_inode_info *ci = ceph_inode(inode);
931985b9ee8SXiubo Li 	struct ceph_fs_client *fsc = ceph_inode_to_fs_client(inode);
932fc2744aaSYan, Zheng 	struct ceph_vino vino = ceph_vino(inode);
9332a2d927eSYan, Zheng 	pgoff_t index, start_index, end = -1;
93480e755feSSage Weil 	struct ceph_snap_context *snapc = NULL, *last_snapc = NULL, *pgsnapc;
935590a2b5fSVishal Moola (Oracle) 	struct folio_batch fbatch;
9361d3576fdSSage Weil 	int rc = 0;
93793407472SFabian Frederick 	unsigned int wsize = i_blocksize(inode);
9381d3576fdSSage Weil 	struct ceph_osd_request *req = NULL;
9391f934b00SYan, Zheng 	struct ceph_writeback_ctl ceph_wbc;
940590e9d98SYan, Zheng 	bool should_loop, range_whole = false;
941af9cc401SYan, Zheng 	bool done = false;
9421702e797SJeff Layton 	bool caching = ceph_is_cache_enabled(inode);
9437d41870dSXiubo Li 	xa_mark_t tag;
9441d3576fdSSage Weil 
945503d4fa6SNeilBrown 	if (wbc->sync_mode == WB_SYNC_NONE &&
946503d4fa6SNeilBrown 	    fsc->write_congested)
947503d4fa6SNeilBrown 		return 0;
948503d4fa6SNeilBrown 
9493fb99d48SYanhu Cao 	dout("writepages_start %p (mode=%s)\n", inode,
9501d3576fdSSage Weil 	     wbc->sync_mode == WB_SYNC_NONE ? "NONE" :
9511d3576fdSSage Weil 	     (wbc->sync_mode == WB_SYNC_ALL ? "ALL" : "HOLD"));
9521d3576fdSSage Weil 
9535d6451b1SJeff Layton 	if (ceph_inode_is_shutdown(inode)) {
9546c93df5dSYan, Zheng 		if (ci->i_wrbuffer_ref > 0) {
9556c93df5dSYan, Zheng 			pr_warn_ratelimited(
9566c93df5dSYan, Zheng 				"writepage_start %p %lld forced umount\n",
9576c93df5dSYan, Zheng 				inode, ceph_ino(inode));
9586c93df5dSYan, Zheng 		}
959a341d4dfSYan, Zheng 		mapping_set_error(mapping, -EIO);
9601d3576fdSSage Weil 		return -EIO; /* we're in a forced umount, don't write! */
9611d3576fdSSage Weil 	}
96295cca2b4SYan, Zheng 	if (fsc->mount_options->wsize < wsize)
9633d14c5d2SYehuda Sadeh 		wsize = fsc->mount_options->wsize;
9641d3576fdSSage Weil 
965590a2b5fSVishal Moola (Oracle) 	folio_batch_init(&fbatch);
9661d3576fdSSage Weil 
967590e9d98SYan, Zheng 	start_index = wbc->range_cyclic ? mapping->writeback_index : 0;
968590e9d98SYan, Zheng 	index = start_index;
9691d3576fdSSage Weil 
9707d41870dSXiubo Li 	if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages) {
9717d41870dSXiubo Li 		tag = PAGECACHE_TAG_TOWRITE;
9727d41870dSXiubo Li 	} else {
9737d41870dSXiubo Li 		tag = PAGECACHE_TAG_DIRTY;
9747d41870dSXiubo Li 	}
9751d3576fdSSage Weil retry:
9761d3576fdSSage Weil 	/* find oldest snap context with dirty data */
97705455e11SYan, Zheng 	snapc = get_oldest_context(inode, &ceph_wbc, NULL);
9781d3576fdSSage Weil 	if (!snapc) {
9791d3576fdSSage Weil 		/* hmm, why does writepages get called when there
9801d3576fdSSage Weil 		   is no dirty data? */
9811d3576fdSSage Weil 		dout(" no snap context with dirty data?\n");
9821d3576fdSSage Weil 		goto out;
9831d3576fdSSage Weil 	}
9841d3576fdSSage Weil 	dout(" oldest snapc is %p seq %lld (%d snaps)\n",
9851d3576fdSSage Weil 	     snapc, snapc->seq, snapc->num_snaps);
986fc2744aaSYan, Zheng 
9872a2d927eSYan, Zheng 	should_loop = false;
9882a2d927eSYan, Zheng 	if (ceph_wbc.head_snapc && snapc != last_snapc) {
9892a2d927eSYan, Zheng 		/* where to start/end? */
9902a2d927eSYan, Zheng 		if (wbc->range_cyclic) {
9912a2d927eSYan, Zheng 			index = start_index;
9922a2d927eSYan, Zheng 			end = -1;
9932a2d927eSYan, Zheng 			if (index > 0)
9942a2d927eSYan, Zheng 				should_loop = true;
9952a2d927eSYan, Zheng 			dout(" cyclic, start at %lu\n", index);
9962a2d927eSYan, Zheng 		} else {
9972a2d927eSYan, Zheng 			index = wbc->range_start >> PAGE_SHIFT;
9982a2d927eSYan, Zheng 			end = wbc->range_end >> PAGE_SHIFT;
9992a2d927eSYan, Zheng 			if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX)
10002a2d927eSYan, Zheng 				range_whole = true;
10012a2d927eSYan, Zheng 			dout(" not cyclic, %lu to %lu\n", index, end);
10021d3576fdSSage Weil 		}
10032a2d927eSYan, Zheng 	} else if (!ceph_wbc.head_snapc) {
10042a2d927eSYan, Zheng 		/* Do not respect wbc->range_{start,end}. Dirty pages
10052a2d927eSYan, Zheng 		 * in that range can be associated with newer snapc.
10062a2d927eSYan, Zheng 		 * They are not writeable until we write all dirty pages
10072a2d927eSYan, Zheng 		 * associated with 'snapc' get written */
10081582af2eSYan, Zheng 		if (index > 0)
10092a2d927eSYan, Zheng 			should_loop = true;
10102a2d927eSYan, Zheng 		dout(" non-head snapc, range whole\n");
10112a2d927eSYan, Zheng 	}
10122a2d927eSYan, Zheng 
10137d41870dSXiubo Li 	if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages)
10147d41870dSXiubo Li 		tag_pages_for_writeback(mapping, index, end);
10157d41870dSXiubo Li 
10162a2d927eSYan, Zheng 	ceph_put_snap_context(last_snapc);
10171d3576fdSSage Weil 	last_snapc = snapc;
10181d3576fdSSage Weil 
1019af9cc401SYan, Zheng 	while (!done && index <= end) {
10205b64640cSYan, Zheng 		int num_ops = 0, op_idx;
1021590a2b5fSVishal Moola (Oracle) 		unsigned i, nr_folios, max_pages, locked_pages = 0;
10225b64640cSYan, Zheng 		struct page **pages = NULL, **data_pages;
10231d3576fdSSage Weil 		struct page *page;
10240e5ecac7SYan, Zheng 		pgoff_t strip_unit_end = 0;
10255b64640cSYan, Zheng 		u64 offset = 0, len = 0;
1026a0102bdaSJeff Layton 		bool from_pool = false;
10271d3576fdSSage Weil 
10280e5ecac7SYan, Zheng 		max_pages = wsize >> PAGE_SHIFT;
10291d3576fdSSage Weil 
10301d3576fdSSage Weil get_more_pages:
1031590a2b5fSVishal Moola (Oracle) 		nr_folios = filemap_get_folios_tag(mapping, &index,
10327d41870dSXiubo Li 						   end, tag, &fbatch);
1033590a2b5fSVishal Moola (Oracle) 		dout("pagevec_lookup_range_tag got %d\n", nr_folios);
1034590a2b5fSVishal Moola (Oracle) 		if (!nr_folios && !locked_pages)
10351d3576fdSSage Weil 			break;
1036590a2b5fSVishal Moola (Oracle) 		for (i = 0; i < nr_folios && locked_pages < max_pages; i++) {
1037590a2b5fSVishal Moola (Oracle) 			page = &fbatch.folios[i]->page;
10381d3576fdSSage Weil 			dout("? %p idx %lu\n", page, page->index);
10391d3576fdSSage Weil 			if (locked_pages == 0)
10401d3576fdSSage Weil 				lock_page(page);  /* first page */
10411d3576fdSSage Weil 			else if (!trylock_page(page))
10421d3576fdSSage Weil 				break;
10431d3576fdSSage Weil 
10441d3576fdSSage Weil 			/* only dirty pages, or our accounting breaks */
10451d3576fdSSage Weil 			if (unlikely(!PageDirty(page)) ||
10461d3576fdSSage Weil 			    unlikely(page->mapping != mapping)) {
10471d3576fdSSage Weil 				dout("!dirty or !mapping %p\n", page);
10481d3576fdSSage Weil 				unlock_page(page);
10490713e5f2SYan, Zheng 				continue;
10501d3576fdSSage Weil 			}
1051af9cc401SYan, Zheng 			/* only if matching snap context */
1052af9cc401SYan, Zheng 			pgsnapc = page_snap_context(page);
1053af9cc401SYan, Zheng 			if (pgsnapc != snapc) {
1054af9cc401SYan, Zheng 				dout("page snapc %p %lld != oldest %p %lld\n",
1055af9cc401SYan, Zheng 				     pgsnapc, pgsnapc->seq, snapc, snapc->seq);
10561582af2eSYan, Zheng 				if (!should_loop &&
10571582af2eSYan, Zheng 				    !ceph_wbc.head_snapc &&
10581582af2eSYan, Zheng 				    wbc->sync_mode != WB_SYNC_NONE)
10591582af2eSYan, Zheng 					should_loop = true;
10601d3576fdSSage Weil 				unlock_page(page);
1061af9cc401SYan, Zheng 				continue;
10621d3576fdSSage Weil 			}
10631f934b00SYan, Zheng 			if (page_offset(page) >= ceph_wbc.i_size) {
1064a628304eSMatthew Wilcox (Oracle) 				struct folio *folio = page_folio(page);
1065a628304eSMatthew Wilcox (Oracle) 
1066a628304eSMatthew Wilcox (Oracle) 				dout("folio at %lu beyond eof %llu\n",
1067a628304eSMatthew Wilcox (Oracle) 				     folio->index, ceph_wbc.i_size);
1068c95f1c5fSErqi Chen 				if ((ceph_wbc.size_stable ||
1069a628304eSMatthew Wilcox (Oracle) 				    folio_pos(folio) >= i_size_read(inode)) &&
1070a628304eSMatthew Wilcox (Oracle) 				    folio_clear_dirty_for_io(folio))
1071a628304eSMatthew Wilcox (Oracle) 					folio_invalidate(folio, 0,
1072a628304eSMatthew Wilcox (Oracle) 							folio_size(folio));
1073a628304eSMatthew Wilcox (Oracle) 				folio_unlock(folio);
1074af9cc401SYan, Zheng 				continue;
1075af9cc401SYan, Zheng 			}
1076af9cc401SYan, Zheng 			if (strip_unit_end && (page->index > strip_unit_end)) {
1077af9cc401SYan, Zheng 				dout("end of strip unit %p\n", page);
10781d3576fdSSage Weil 				unlock_page(page);
10791d3576fdSSage Weil 				break;
10801d3576fdSSage Weil 			}
10811702e797SJeff Layton 			if (PageWriteback(page) || PageFsCache(page)) {
10820713e5f2SYan, Zheng 				if (wbc->sync_mode == WB_SYNC_NONE) {
10831d3576fdSSage Weil 					dout("%p under writeback\n", page);
10841d3576fdSSage Weil 					unlock_page(page);
10850713e5f2SYan, Zheng 					continue;
10860713e5f2SYan, Zheng 				}
10870713e5f2SYan, Zheng 				dout("waiting on writeback %p\n", page);
10880713e5f2SYan, Zheng 				wait_on_page_writeback(page);
10891702e797SJeff Layton 				wait_on_page_fscache(page);
10901d3576fdSSage Weil 			}
10911d3576fdSSage Weil 
10921d3576fdSSage Weil 			if (!clear_page_dirty_for_io(page)) {
10931d3576fdSSage Weil 				dout("%p !clear_page_dirty_for_io\n", page);
10941d3576fdSSage Weil 				unlock_page(page);
10950713e5f2SYan, Zheng 				continue;
10961d3576fdSSage Weil 			}
10971d3576fdSSage Weil 
1098e5975c7cSAlex Elder 			/*
1099e5975c7cSAlex Elder 			 * We have something to write.  If this is
1100e5975c7cSAlex Elder 			 * the first locked page this time through,
11015b64640cSYan, Zheng 			 * calculate max possinle write size and
11025b64640cSYan, Zheng 			 * allocate a page array
1103e5975c7cSAlex Elder 			 */
11041d3576fdSSage Weil 			if (locked_pages == 0) {
11055b64640cSYan, Zheng 				u64 objnum;
11065b64640cSYan, Zheng 				u64 objoff;
1107dccbf080SIlya Dryomov 				u32 xlen;
11085b64640cSYan, Zheng 
11091d3576fdSSage Weil 				/* prepare async write request */
11106285bc23SAlex Elder 				offset = (u64)page_offset(page);
1111dccbf080SIlya Dryomov 				ceph_calc_file_object_mapping(&ci->i_layout,
1112dccbf080SIlya Dryomov 							      offset, wsize,
11135b64640cSYan, Zheng 							      &objnum, &objoff,
1114dccbf080SIlya Dryomov 							      &xlen);
1115dccbf080SIlya Dryomov 				len = xlen;
11168c71897bSHenry C Chang 
11173fb99d48SYanhu Cao 				num_ops = 1;
11185b64640cSYan, Zheng 				strip_unit_end = page->index +
111909cbfeafSKirill A. Shutemov 					((len - 1) >> PAGE_SHIFT);
1120715e4cd4SYan, Zheng 
11215b64640cSYan, Zheng 				BUG_ON(pages);
112288486957SAlex Elder 				max_pages = calc_pages_for(0, (u64)len);
11236da2ec56SKees Cook 				pages = kmalloc_array(max_pages,
11246da2ec56SKees Cook 						      sizeof(*pages),
1125fc2744aaSYan, Zheng 						      GFP_NOFS);
112688486957SAlex Elder 				if (!pages) {
1127a0102bdaSJeff Layton 					from_pool = true;
1128a0102bdaSJeff Layton 					pages = mempool_alloc(ceph_wb_pagevec_pool, GFP_NOFS);
1129e5975c7cSAlex Elder 					BUG_ON(!pages);
113088486957SAlex Elder 				}
11315b64640cSYan, Zheng 
11325b64640cSYan, Zheng 				len = 0;
11335b64640cSYan, Zheng 			} else if (page->index !=
113409cbfeafSKirill A. Shutemov 				   (offset + len) >> PAGE_SHIFT) {
1135a0102bdaSJeff Layton 				if (num_ops >= (from_pool ?  CEPH_OSD_SLAB_OPS :
11365b64640cSYan, Zheng 							     CEPH_OSD_MAX_OPS)) {
11375b64640cSYan, Zheng 					redirty_page_for_writepage(wbc, page);
11385b64640cSYan, Zheng 					unlock_page(page);
11395b64640cSYan, Zheng 					break;
11405b64640cSYan, Zheng 				}
11415b64640cSYan, Zheng 
11425b64640cSYan, Zheng 				num_ops++;
11435b64640cSYan, Zheng 				offset = (u64)page_offset(page);
11445b64640cSYan, Zheng 				len = 0;
11451d3576fdSSage Weil 			}
11461d3576fdSSage Weil 
1147590a2b5fSVishal Moola (Oracle) 			/* note position of first page in fbatch */
11481d3576fdSSage Weil 			dout("%p will write page %p idx %lu\n",
11491d3576fdSSage Weil 			     inode, page, page->index);
11502baba250SYehuda Sadeh 
11515b64640cSYan, Zheng 			if (atomic_long_inc_return(&fsc->writeback_count) >
11525b64640cSYan, Zheng 			    CONGESTION_ON_THRESH(
1153503d4fa6SNeilBrown 				    fsc->mount_options->congestion_kb))
1154503d4fa6SNeilBrown 				fsc->write_congested = true;
11550713e5f2SYan, Zheng 
1156d5520771SJeff Layton 			if (IS_ENCRYPTED(inode)) {
1157d5520771SJeff Layton 				pages[locked_pages] =
1158d5520771SJeff Layton 					fscrypt_encrypt_pagecache_blocks(page,
1159d5520771SJeff Layton 						PAGE_SIZE, 0,
1160d5520771SJeff Layton 						locked_pages ? GFP_NOWAIT : GFP_NOFS);
1161d5520771SJeff Layton 				if (IS_ERR(pages[locked_pages])) {
1162d5520771SJeff Layton 					if (PTR_ERR(pages[locked_pages]) == -EINVAL)
1163d5520771SJeff Layton 						pr_err("%s: inode->i_blkbits=%hhu\n",
1164d5520771SJeff Layton 							__func__, inode->i_blkbits);
1165d5520771SJeff Layton 					/* better not fail on first page! */
1166d5520771SJeff Layton 					BUG_ON(locked_pages == 0);
1167d5520771SJeff Layton 					pages[locked_pages] = NULL;
1168d5520771SJeff Layton 					redirty_page_for_writepage(wbc, page);
1169d5520771SJeff Layton 					unlock_page(page);
1170d5520771SJeff Layton 					break;
1171d5520771SJeff Layton 				}
1172d5520771SJeff Layton 				++locked_pages;
1173d5520771SJeff Layton 			} else {
11740713e5f2SYan, Zheng 				pages[locked_pages++] = page;
1175d5520771SJeff Layton 			}
11760713e5f2SYan, Zheng 
1177d5520771SJeff Layton 			fbatch.folios[i] = NULL;
11788ff2d290SJeff Layton 			len += thp_size(page);
11791d3576fdSSage Weil 		}
11801d3576fdSSage Weil 
11811d3576fdSSage Weil 		/* did we get anything? */
11821d3576fdSSage Weil 		if (!locked_pages)
1183590a2b5fSVishal Moola (Oracle) 			goto release_folios;
11841d3576fdSSage Weil 		if (i) {
11850713e5f2SYan, Zheng 			unsigned j, n = 0;
1186590a2b5fSVishal Moola (Oracle) 			/* shift unused page to beginning of fbatch */
1187590a2b5fSVishal Moola (Oracle) 			for (j = 0; j < nr_folios; j++) {
1188590a2b5fSVishal Moola (Oracle) 				if (!fbatch.folios[j])
11890713e5f2SYan, Zheng 					continue;
11900713e5f2SYan, Zheng 				if (n < j)
1191590a2b5fSVishal Moola (Oracle) 					fbatch.folios[n] = fbatch.folios[j];
11920713e5f2SYan, Zheng 				n++;
11930713e5f2SYan, Zheng 			}
1194590a2b5fSVishal Moola (Oracle) 			fbatch.nr = n;
11951d3576fdSSage Weil 
1196590a2b5fSVishal Moola (Oracle) 			if (nr_folios && i == nr_folios &&
11971d3576fdSSage Weil 			    locked_pages < max_pages) {
1198590a2b5fSVishal Moola (Oracle) 				dout("reached end fbatch, trying for more\n");
1199590a2b5fSVishal Moola (Oracle) 				folio_batch_release(&fbatch);
12001d3576fdSSage Weil 				goto get_more_pages;
12011d3576fdSSage Weil 			}
12021d3576fdSSage Weil 		}
12031d3576fdSSage Weil 
12045b64640cSYan, Zheng new_request:
1205d5520771SJeff Layton 		offset = ceph_fscrypt_page_offset(pages[0]);
12065b64640cSYan, Zheng 		len = wsize;
12075b64640cSYan, Zheng 
12085b64640cSYan, Zheng 		req = ceph_osdc_new_request(&fsc->client->osdc,
12095b64640cSYan, Zheng 					&ci->i_layout, vino,
12105b64640cSYan, Zheng 					offset, &len, 0, num_ops,
12111f934b00SYan, Zheng 					CEPH_OSD_OP_WRITE, CEPH_OSD_FLAG_WRITE,
12121f934b00SYan, Zheng 					snapc, ceph_wbc.truncate_seq,
12131f934b00SYan, Zheng 					ceph_wbc.truncate_size, false);
12145b64640cSYan, Zheng 		if (IS_ERR(req)) {
12155b64640cSYan, Zheng 			req = ceph_osdc_new_request(&fsc->client->osdc,
12165b64640cSYan, Zheng 						&ci->i_layout, vino,
12175b64640cSYan, Zheng 						offset, &len, 0,
12185b64640cSYan, Zheng 						min(num_ops,
12195b64640cSYan, Zheng 						    CEPH_OSD_SLAB_OPS),
12205b64640cSYan, Zheng 						CEPH_OSD_OP_WRITE,
122154ea0046SIlya Dryomov 						CEPH_OSD_FLAG_WRITE,
12221f934b00SYan, Zheng 						snapc, ceph_wbc.truncate_seq,
12231f934b00SYan, Zheng 						ceph_wbc.truncate_size, true);
12245b64640cSYan, Zheng 			BUG_ON(IS_ERR(req));
12255b64640cSYan, Zheng 		}
1226d5520771SJeff Layton 		BUG_ON(len < ceph_fscrypt_page_offset(pages[locked_pages - 1]) +
1227d5520771SJeff Layton 			     thp_size(pages[locked_pages - 1]) - offset);
12285b64640cSYan, Zheng 
12291464de9fSXiubo Li 		if (!ceph_inc_osd_stopping_blocker(fsc->mdsc)) {
12301464de9fSXiubo Li 			rc = -EIO;
12311464de9fSXiubo Li 			goto release_folios;
12321464de9fSXiubo Li 		}
12335b64640cSYan, Zheng 		req->r_callback = writepages_finish;
12345b64640cSYan, Zheng 		req->r_inode = inode;
12355b64640cSYan, Zheng 
12365b64640cSYan, Zheng 		/* Format the osd request message and submit the write */
12375b64640cSYan, Zheng 		len = 0;
12385b64640cSYan, Zheng 		data_pages = pages;
12395b64640cSYan, Zheng 		op_idx = 0;
12405b64640cSYan, Zheng 		for (i = 0; i < locked_pages; i++) {
1241d5520771SJeff Layton 			struct page *page = ceph_fscrypt_pagecache_page(pages[i]);
1242d5520771SJeff Layton 
1243d5520771SJeff Layton 			u64 cur_offset = page_offset(page);
12441702e797SJeff Layton 			/*
12451702e797SJeff Layton 			 * Discontinuity in page range? Ceph can handle that by just passing
12461702e797SJeff Layton 			 * multiple extents in the write op.
12471702e797SJeff Layton 			 */
12485b64640cSYan, Zheng 			if (offset + len != cur_offset) {
12491702e797SJeff Layton 				/* If it's full, stop here */
12503fb99d48SYanhu Cao 				if (op_idx + 1 == req->r_num_ops)
12515b64640cSYan, Zheng 					break;
12521702e797SJeff Layton 
12531702e797SJeff Layton 				/* Kick off an fscache write with what we have so far. */
12541702e797SJeff Layton 				ceph_fscache_write_to_cache(inode, offset, len, caching);
12551702e797SJeff Layton 
12561702e797SJeff Layton 				/* Start a new extent */
12575b64640cSYan, Zheng 				osd_req_op_extent_dup_last(req, op_idx,
12585b64640cSYan, Zheng 							   cur_offset - offset);
12595b64640cSYan, Zheng 				dout("writepages got pages at %llu~%llu\n",
12605b64640cSYan, Zheng 				     offset, len);
12615b64640cSYan, Zheng 				osd_req_op_extent_osd_data_pages(req, op_idx,
12625b64640cSYan, Zheng 							data_pages, len, 0,
1263a0102bdaSJeff Layton 							from_pool, false);
12645b64640cSYan, Zheng 				osd_req_op_extent_update(req, op_idx, len);
12655b64640cSYan, Zheng 
12665b64640cSYan, Zheng 				len = 0;
12675b64640cSYan, Zheng 				offset = cur_offset;
12685b64640cSYan, Zheng 				data_pages = pages + i;
12695b64640cSYan, Zheng 				op_idx++;
12705b64640cSYan, Zheng 			}
12715b64640cSYan, Zheng 
1272d5520771SJeff Layton 			set_page_writeback(page);
12731702e797SJeff Layton 			if (caching)
1274d5520771SJeff Layton 				ceph_set_page_fscache(page);
12758ff2d290SJeff Layton 			len += thp_size(page);
12765b64640cSYan, Zheng 		}
12771702e797SJeff Layton 		ceph_fscache_write_to_cache(inode, offset, len, caching);
12785b64640cSYan, Zheng 
12791f934b00SYan, Zheng 		if (ceph_wbc.size_stable) {
12801f934b00SYan, Zheng 			len = min(len, ceph_wbc.i_size - offset);
12815b64640cSYan, Zheng 		} else if (i == locked_pages) {
1282e1966b49SYan, Zheng 			/* writepages_finish() clears writeback pages
1283e1966b49SYan, Zheng 			 * according to the data length, so make sure
1284e1966b49SYan, Zheng 			 * data length covers all locked pages */
12858ff2d290SJeff Layton 			u64 min_len = len + 1 - thp_size(page);
12861f934b00SYan, Zheng 			len = get_writepages_data_length(inode, pages[i - 1],
12871f934b00SYan, Zheng 							 offset);
12885b64640cSYan, Zheng 			len = max(len, min_len);
1289e1966b49SYan, Zheng 		}
1290d5520771SJeff Layton 		if (IS_ENCRYPTED(inode))
1291d5520771SJeff Layton 			len = round_up(len, CEPH_FSCRYPT_BLOCK_SIZE);
1292d5520771SJeff Layton 
12935b64640cSYan, Zheng 		dout("writepages got pages at %llu~%llu\n", offset, len);
12941d3576fdSSage Weil 
1295d5520771SJeff Layton 		if (IS_ENCRYPTED(inode) &&
1296d5520771SJeff Layton 		    ((offset | len) & ~CEPH_FSCRYPT_BLOCK_MASK))
1297d5520771SJeff Layton 			pr_warn("%s: bad encrypted write offset=%lld len=%llu\n",
1298d5520771SJeff Layton 				__func__, offset, len);
1299d5520771SJeff Layton 
13005b64640cSYan, Zheng 		osd_req_op_extent_osd_data_pages(req, op_idx, data_pages, len,
1301a0102bdaSJeff Layton 						 0, from_pool, false);
13025b64640cSYan, Zheng 		osd_req_op_extent_update(req, op_idx, len);
1303e5975c7cSAlex Elder 
13045b64640cSYan, Zheng 		BUG_ON(op_idx + 1 != req->r_num_ops);
13055b64640cSYan, Zheng 
1306a0102bdaSJeff Layton 		from_pool = false;
13075b64640cSYan, Zheng 		if (i < locked_pages) {
13085b64640cSYan, Zheng 			BUG_ON(num_ops <= req->r_num_ops);
13095b64640cSYan, Zheng 			num_ops -= req->r_num_ops;
13105b64640cSYan, Zheng 			locked_pages -= i;
1311e5975c7cSAlex Elder 
13125b64640cSYan, Zheng 			/* allocate new pages array for next request */
13135b64640cSYan, Zheng 			data_pages = pages;
13146da2ec56SKees Cook 			pages = kmalloc_array(locked_pages, sizeof(*pages),
13155b64640cSYan, Zheng 					      GFP_NOFS);
13165b64640cSYan, Zheng 			if (!pages) {
1317a0102bdaSJeff Layton 				from_pool = true;
1318a0102bdaSJeff Layton 				pages = mempool_alloc(ceph_wb_pagevec_pool, GFP_NOFS);
13195b64640cSYan, Zheng 				BUG_ON(!pages);
13205b64640cSYan, Zheng 			}
13215b64640cSYan, Zheng 			memcpy(pages, data_pages + i,
13225b64640cSYan, Zheng 			       locked_pages * sizeof(*pages));
13235b64640cSYan, Zheng 			memset(data_pages + i, 0,
13245b64640cSYan, Zheng 			       locked_pages * sizeof(*pages));
13255b64640cSYan, Zheng 		} else {
13265b64640cSYan, Zheng 			BUG_ON(num_ops != req->r_num_ops);
13275b64640cSYan, Zheng 			index = pages[i - 1]->index + 1;
13285b64640cSYan, Zheng 			/* request message now owns the pages array */
13295b64640cSYan, Zheng 			pages = NULL;
13305b64640cSYan, Zheng 		}
1331e5975c7cSAlex Elder 
1332fac02ddfSArnd Bergmann 		req->r_mtime = inode->i_mtime;
1333a8af0d68SJeff Layton 		ceph_osdc_start_request(&fsc->client->osdc, req);
13341d3576fdSSage Weil 		req = NULL;
13351d3576fdSSage Weil 
13365b64640cSYan, Zheng 		wbc->nr_to_write -= i;
13375b64640cSYan, Zheng 		if (pages)
13385b64640cSYan, Zheng 			goto new_request;
13395b64640cSYan, Zheng 
13402a2d927eSYan, Zheng 		/*
13412a2d927eSYan, Zheng 		 * We stop writing back only if we are not doing
13422a2d927eSYan, Zheng 		 * integrity sync. In case of integrity sync we have to
13432a2d927eSYan, Zheng 		 * keep going until we have written all the pages
13442a2d927eSYan, Zheng 		 * we tagged for writeback prior to entering this loop.
13452a2d927eSYan, Zheng 		 */
13462a2d927eSYan, Zheng 		if (wbc->nr_to_write <= 0 && wbc->sync_mode == WB_SYNC_NONE)
1347af9cc401SYan, Zheng 			done = true;
13481d3576fdSSage Weil 
1349590a2b5fSVishal Moola (Oracle) release_folios:
1350590a2b5fSVishal Moola (Oracle) 		dout("folio_batch release on %d folios (%p)\n", (int)fbatch.nr,
1351590a2b5fSVishal Moola (Oracle) 		     fbatch.nr ? fbatch.folios[0] : NULL);
1352590a2b5fSVishal Moola (Oracle) 		folio_batch_release(&fbatch);
13531d3576fdSSage Weil 	}
13541d3576fdSSage Weil 
13551d3576fdSSage Weil 	if (should_loop && !done) {
13561d3576fdSSage Weil 		/* more to do; loop back to beginning of file */
13571d3576fdSSage Weil 		dout("writepages looping back to beginning of file\n");
13582a2d927eSYan, Zheng 		end = start_index - 1; /* OK even when start_index == 0 */
1359f275635eSYan, Zheng 
1360f275635eSYan, Zheng 		/* to write dirty pages associated with next snapc,
1361f275635eSYan, Zheng 		 * we need to wait until current writes complete */
1362f275635eSYan, Zheng 		if (wbc->sync_mode != WB_SYNC_NONE &&
1363f275635eSYan, Zheng 		    start_index == 0 && /* all dirty pages were checked */
1364f275635eSYan, Zheng 		    !ceph_wbc.head_snapc) {
1365f275635eSYan, Zheng 			struct page *page;
1366f275635eSYan, Zheng 			unsigned i, nr;
1367f275635eSYan, Zheng 			index = 0;
1368f275635eSYan, Zheng 			while ((index <= end) &&
1369590a2b5fSVishal Moola (Oracle) 			       (nr = filemap_get_folios_tag(mapping, &index,
1370590a2b5fSVishal Moola (Oracle) 						(pgoff_t)-1,
1371590a2b5fSVishal Moola (Oracle) 						PAGECACHE_TAG_WRITEBACK,
1372590a2b5fSVishal Moola (Oracle) 						&fbatch))) {
1373f275635eSYan, Zheng 				for (i = 0; i < nr; i++) {
1374590a2b5fSVishal Moola (Oracle) 					page = &fbatch.folios[i]->page;
1375f275635eSYan, Zheng 					if (page_snap_context(page) != snapc)
1376f275635eSYan, Zheng 						continue;
1377f275635eSYan, Zheng 					wait_on_page_writeback(page);
1378f275635eSYan, Zheng 				}
1379590a2b5fSVishal Moola (Oracle) 				folio_batch_release(&fbatch);
1380f275635eSYan, Zheng 				cond_resched();
1381f275635eSYan, Zheng 			}
1382f275635eSYan, Zheng 		}
1383f275635eSYan, Zheng 
13842a2d927eSYan, Zheng 		start_index = 0;
13851d3576fdSSage Weil 		index = 0;
13861d3576fdSSage Weil 		goto retry;
13871d3576fdSSage Weil 	}
13881d3576fdSSage Weil 
13891d3576fdSSage Weil 	if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0))
13901d3576fdSSage Weil 		mapping->writeback_index = index;
13911d3576fdSSage Weil 
13921d3576fdSSage Weil out:
13931d3576fdSSage Weil 	ceph_osdc_put_request(req);
13942a2d927eSYan, Zheng 	ceph_put_snap_context(last_snapc);
13952a2d927eSYan, Zheng 	dout("writepages dend - startone, rc = %d\n", rc);
13961d3576fdSSage Weil 	return rc;
13971d3576fdSSage Weil }
13981d3576fdSSage Weil 
13991d3576fdSSage Weil 
14001d3576fdSSage Weil 
14011d3576fdSSage Weil /*
14021d3576fdSSage Weil  * See if a given @snapc is either writeable, or already written.
14031d3576fdSSage Weil  */
context_is_writeable_or_written(struct inode * inode,struct ceph_snap_context * snapc)14041d3576fdSSage Weil static int context_is_writeable_or_written(struct inode *inode,
14051d3576fdSSage Weil 					   struct ceph_snap_context *snapc)
14061d3576fdSSage Weil {
140705455e11SYan, Zheng 	struct ceph_snap_context *oldest = get_oldest_context(inode, NULL, NULL);
14086298a337SSage Weil 	int ret = !oldest || snapc->seq <= oldest->seq;
14096298a337SSage Weil 
14106298a337SSage Weil 	ceph_put_snap_context(oldest);
14116298a337SSage Weil 	return ret;
14121d3576fdSSage Weil }
14131d3576fdSSage Weil 
141418d620f0SJeff Layton /**
141518d620f0SJeff Layton  * ceph_find_incompatible - find an incompatible context and return it
141618d620f0SJeff Layton  * @page: page being dirtied
141718d620f0SJeff Layton  *
141818d620f0SJeff Layton  * We are only allowed to write into/dirty a page if the page is
141918d620f0SJeff Layton  * clean, or already dirty within the same snap context. Returns a
142018d620f0SJeff Layton  * conflicting context if there is one, NULL if there isn't, or a
142118d620f0SJeff Layton  * negative error code on other errors.
142218d620f0SJeff Layton  *
142318d620f0SJeff Layton  * Must be called with page lock held.
142418d620f0SJeff Layton  */
142518d620f0SJeff Layton static struct ceph_snap_context *
ceph_find_incompatible(struct page * page)1426d45156bfSJeff Layton ceph_find_incompatible(struct page *page)
142718d620f0SJeff Layton {
1428d45156bfSJeff Layton 	struct inode *inode = page->mapping->host;
142918d620f0SJeff Layton 	struct ceph_inode_info *ci = ceph_inode(inode);
143018d620f0SJeff Layton 
14315d6451b1SJeff Layton 	if (ceph_inode_is_shutdown(inode)) {
14325d6451b1SJeff Layton 		dout(" page %p %llx:%llx is shutdown\n", page,
14335d6451b1SJeff Layton 		     ceph_vinop(inode));
14345d6451b1SJeff Layton 		return ERR_PTR(-ESTALE);
143518d620f0SJeff Layton 	}
143618d620f0SJeff Layton 
143718d620f0SJeff Layton 	for (;;) {
143818d620f0SJeff Layton 		struct ceph_snap_context *snapc, *oldest;
143918d620f0SJeff Layton 
144018d620f0SJeff Layton 		wait_on_page_writeback(page);
144118d620f0SJeff Layton 
144218d620f0SJeff Layton 		snapc = page_snap_context(page);
144318d620f0SJeff Layton 		if (!snapc || snapc == ci->i_head_snapc)
144418d620f0SJeff Layton 			break;
144518d620f0SJeff Layton 
144618d620f0SJeff Layton 		/*
144718d620f0SJeff Layton 		 * this page is already dirty in another (older) snap
144818d620f0SJeff Layton 		 * context!  is it writeable now?
144918d620f0SJeff Layton 		 */
145018d620f0SJeff Layton 		oldest = get_oldest_context(inode, NULL, NULL);
145118d620f0SJeff Layton 		if (snapc->seq > oldest->seq) {
145218d620f0SJeff Layton 			/* not writeable -- return it for the caller to deal with */
145318d620f0SJeff Layton 			ceph_put_snap_context(oldest);
145418d620f0SJeff Layton 			dout(" page %p snapc %p not current or oldest\n", page, snapc);
145518d620f0SJeff Layton 			return ceph_get_snap_context(snapc);
145618d620f0SJeff Layton 		}
145718d620f0SJeff Layton 		ceph_put_snap_context(oldest);
145818d620f0SJeff Layton 
145918d620f0SJeff Layton 		/* yay, writeable, do it now (without dropping page lock) */
146018d620f0SJeff Layton 		dout(" page %p snapc %p not current, but oldest\n", page, snapc);
146118d620f0SJeff Layton 		if (clear_page_dirty_for_io(page)) {
146218d620f0SJeff Layton 			int r = writepage_nounlock(page, NULL);
146318d620f0SJeff Layton 			if (r < 0)
146418d620f0SJeff Layton 				return ERR_PTR(r);
146518d620f0SJeff Layton 		}
146618d620f0SJeff Layton 	}
146718d620f0SJeff Layton 	return NULL;
146818d620f0SJeff Layton }
146918d620f0SJeff Layton 
ceph_netfs_check_write_begin(struct file * file,loff_t pos,unsigned int len,struct folio ** foliop,void ** _fsdata)1470d801327dSJeff Layton static int ceph_netfs_check_write_begin(struct file *file, loff_t pos, unsigned int len,
1471fac47b43SXiubo Li 					struct folio **foliop, void **_fsdata)
1472d801327dSJeff Layton {
1473d801327dSJeff Layton 	struct inode *inode = file_inode(file);
1474d801327dSJeff Layton 	struct ceph_inode_info *ci = ceph_inode(inode);
1475d801327dSJeff Layton 	struct ceph_snap_context *snapc;
1476d801327dSJeff Layton 
1477fac47b43SXiubo Li 	snapc = ceph_find_incompatible(folio_page(*foliop, 0));
1478d801327dSJeff Layton 	if (snapc) {
1479d801327dSJeff Layton 		int r;
1480d801327dSJeff Layton 
1481fac47b43SXiubo Li 		folio_unlock(*foliop);
1482fac47b43SXiubo Li 		folio_put(*foliop);
1483fac47b43SXiubo Li 		*foliop = NULL;
1484d801327dSJeff Layton 		if (IS_ERR(snapc))
1485d801327dSJeff Layton 			return PTR_ERR(snapc);
1486d801327dSJeff Layton 
1487d801327dSJeff Layton 		ceph_queue_writeback(inode);
1488d801327dSJeff Layton 		r = wait_event_killable(ci->i_cap_wq,
1489d801327dSJeff Layton 					context_is_writeable_or_written(inode, snapc));
1490d801327dSJeff Layton 		ceph_put_snap_context(snapc);
1491d801327dSJeff Layton 		return r == 0 ? -EAGAIN : r;
1492d801327dSJeff Layton 	}
1493d801327dSJeff Layton 	return 0;
1494d801327dSJeff Layton }
1495d801327dSJeff Layton 
14961d3576fdSSage Weil /*
14971d3576fdSSage Weil  * We are only allowed to write into/dirty the page if the page is
14981d3576fdSSage Weil  * clean, or already dirty within the same snap context.
14994af6b225SYehuda Sadeh  */
ceph_write_begin(struct file * file,struct address_space * mapping,loff_t pos,unsigned len,struct page ** pagep,void ** fsdata)15004af6b225SYehuda Sadeh static int ceph_write_begin(struct file *file, struct address_space *mapping,
15019d6b0cd7SMatthew Wilcox (Oracle) 			    loff_t pos, unsigned len,
15024af6b225SYehuda Sadeh 			    struct page **pagep, void **fsdata)
15034af6b225SYehuda Sadeh {
1504496ad9aaSAl Viro 	struct inode *inode = file_inode(file);
1505e81fb419SLinus Torvalds 	struct ceph_inode_info *ci = ceph_inode(inode);
150678525c74SDavid Howells 	struct folio *folio = NULL;
1507d801327dSJeff Layton 	int r;
15084af6b225SYehuda Sadeh 
1509e81fb419SLinus Torvalds 	r = netfs_write_begin(&ci->netfs, file, inode->i_mapping, pos, len, &folio, NULL);
1510c460f4e4SXiubo Li 	if (r < 0)
1511c460f4e4SXiubo Li 		return r;
1512c460f4e4SXiubo Li 
151378525c74SDavid Howells 	folio_wait_fscache(folio);
151478525c74SDavid Howells 	WARN_ON_ONCE(!folio_test_locked(folio));
151578525c74SDavid Howells 	*pagep = &folio->page;
1516c460f4e4SXiubo Li 	return 0;
15174af6b225SYehuda Sadeh }
15184af6b225SYehuda Sadeh 
15194af6b225SYehuda Sadeh /*
15201d3576fdSSage Weil  * we don't do anything in here that simple_write_end doesn't do
15215dda377cSYan, Zheng  * except adjust dirty page accounting
15221d3576fdSSage Weil  */
ceph_write_end(struct file * file,struct address_space * mapping,loff_t pos,unsigned len,unsigned copied,struct page * subpage,void * fsdata)15231d3576fdSSage Weil static int ceph_write_end(struct file *file, struct address_space *mapping,
15241d3576fdSSage Weil 			  loff_t pos, unsigned len, unsigned copied,
152578525c74SDavid Howells 			  struct page *subpage, void *fsdata)
15261d3576fdSSage Weil {
152778525c74SDavid Howells 	struct folio *folio = page_folio(subpage);
1528496ad9aaSAl Viro 	struct inode *inode = file_inode(file);
1529efb0ca76SYan, Zheng 	bool check_cap = false;
15301d3576fdSSage Weil 
153178525c74SDavid Howells 	dout("write_end file %p inode %p folio %p %d~%d (%d)\n", file,
153278525c74SDavid Howells 	     inode, folio, (int)pos, (int)copied, (int)len);
15331d3576fdSSage Weil 
153478525c74SDavid Howells 	if (!folio_test_uptodate(folio)) {
1535ce3a8732SJeff Layton 		/* just return that nothing was copied on a short copy */
1536b9de313cSAl Viro 		if (copied < len) {
1537b9de313cSAl Viro 			copied = 0;
1538b9de313cSAl Viro 			goto out;
1539b9de313cSAl Viro 		}
154078525c74SDavid Howells 		folio_mark_uptodate(folio);
1541b9de313cSAl Viro 	}
15421d3576fdSSage Weil 
15431d3576fdSSage Weil 	/* did file size increase? */
154499c88e69SYan, Zheng 	if (pos+copied > i_size_read(inode))
15451d3576fdSSage Weil 		check_cap = ceph_inode_set_size(inode, pos+copied);
15461d3576fdSSage Weil 
154778525c74SDavid Howells 	folio_mark_dirty(folio);
15481d3576fdSSage Weil 
1549b9de313cSAl Viro out:
155078525c74SDavid Howells 	folio_unlock(folio);
155178525c74SDavid Howells 	folio_put(folio);
15521d3576fdSSage Weil 
15531d3576fdSSage Weil 	if (check_cap)
1554e4b731ccSXiubo Li 		ceph_check_caps(ceph_inode(inode), CHECK_CAPS_AUTHONLY);
15551d3576fdSSage Weil 
15561d3576fdSSage Weil 	return copied;
15571d3576fdSSage Weil }
15581d3576fdSSage Weil 
15591d3576fdSSage Weil const struct address_space_operations ceph_aops = {
15606c62371bSMatthew Wilcox (Oracle) 	.read_folio = netfs_read_folio,
1561bc899ee1SDavid Howells 	.readahead = netfs_readahead,
15621d3576fdSSage Weil 	.writepage = ceph_writepage,
15631d3576fdSSage Weil 	.writepages = ceph_writepages_start,
15641d3576fdSSage Weil 	.write_begin = ceph_write_begin,
15651d3576fdSSage Weil 	.write_end = ceph_write_end,
15668fb72b4aSMatthew Wilcox (Oracle) 	.dirty_folio = ceph_dirty_folio,
15679872f4deSMatthew Wilcox (Oracle) 	.invalidate_folio = ceph_invalidate_folio,
15685e414655SMatthew Wilcox (Oracle) 	.release_folio = ceph_release_folio,
15699c43ff44SJeff Layton 	.direct_IO = noop_direct_IO,
15701d3576fdSSage Weil };
15711d3576fdSSage Weil 
ceph_block_sigs(sigset_t * oldset)15724f7e89f6SYan, Zheng static void ceph_block_sigs(sigset_t *oldset)
15734f7e89f6SYan, Zheng {
15744f7e89f6SYan, Zheng 	sigset_t mask;
15754f7e89f6SYan, Zheng 	siginitsetinv(&mask, sigmask(SIGKILL));
15764f7e89f6SYan, Zheng 	sigprocmask(SIG_BLOCK, &mask, oldset);
15774f7e89f6SYan, Zheng }
15784f7e89f6SYan, Zheng 
ceph_restore_sigs(sigset_t * oldset)15794f7e89f6SYan, Zheng static void ceph_restore_sigs(sigset_t *oldset)
15804f7e89f6SYan, Zheng {
15814f7e89f6SYan, Zheng 	sigprocmask(SIG_SETMASK, oldset, NULL);
15824f7e89f6SYan, Zheng }
15831d3576fdSSage Weil 
15841d3576fdSSage Weil /*
15851d3576fdSSage Weil  * vm ops
15861d3576fdSSage Weil  */
ceph_filemap_fault(struct vm_fault * vmf)158724499847SSouptick Joarder static vm_fault_t ceph_filemap_fault(struct vm_fault *vmf)
158861f68816SYan, Zheng {
158911bac800SDave Jiang 	struct vm_area_struct *vma = vmf->vma;
159061f68816SYan, Zheng 	struct inode *inode = file_inode(vma->vm_file);
159161f68816SYan, Zheng 	struct ceph_inode_info *ci = ceph_inode(inode);
159261f68816SYan, Zheng 	struct ceph_file_info *fi = vma->vm_file->private_data;
1593c403c3a2SMatthew Wilcox (Oracle) 	loff_t off = (loff_t)vmf->pgoff << PAGE_SHIFT;
159424499847SSouptick Joarder 	int want, got, err;
15954f7e89f6SYan, Zheng 	sigset_t oldset;
159624499847SSouptick Joarder 	vm_fault_t ret = VM_FAULT_SIGBUS;
15974f7e89f6SYan, Zheng 
15985d6451b1SJeff Layton 	if (ceph_inode_is_shutdown(inode))
15995d6451b1SJeff Layton 		return ret;
16005d6451b1SJeff Layton 
16014f7e89f6SYan, Zheng 	ceph_block_sigs(&oldset);
160261f68816SYan, Zheng 
16038ff2d290SJeff Layton 	dout("filemap_fault %p %llx.%llx %llu trying to get caps\n",
16048ff2d290SJeff Layton 	     inode, ceph_vinop(inode), off);
160561f68816SYan, Zheng 	if (fi->fmode & CEPH_FILE_MODE_LAZY)
160661f68816SYan, Zheng 		want = CEPH_CAP_FILE_CACHE | CEPH_CAP_FILE_LAZYIO;
160761f68816SYan, Zheng 	else
160861f68816SYan, Zheng 		want = CEPH_CAP_FILE_CACHE;
16094f7e89f6SYan, Zheng 
161061f68816SYan, Zheng 	got = 0;
1611e72968e1SJeff Layton 	err = ceph_get_caps(vma->vm_file, CEPH_CAP_FILE_RD, want, -1, &got);
161224499847SSouptick Joarder 	if (err < 0)
16134f7e89f6SYan, Zheng 		goto out_restore;
16146ce026e4SYan, Zheng 
16158ff2d290SJeff Layton 	dout("filemap_fault %p %llu got cap refs on %s\n",
16168ff2d290SJeff Layton 	     inode, off, ceph_cap_string(got));
161761f68816SYan, Zheng 
161883701246SYan, Zheng 	if ((got & (CEPH_CAP_FILE_CACHE | CEPH_CAP_FILE_LAZYIO)) ||
161948490776SXiubo Li 	    !ceph_has_inline_data(ci)) {
16205d988308SYan, Zheng 		CEPH_DEFINE_RW_CONTEXT(rw_ctx, got);
16215d988308SYan, Zheng 		ceph_add_rw_context(fi, &rw_ctx);
162211bac800SDave Jiang 		ret = filemap_fault(vmf);
16235d988308SYan, Zheng 		ceph_del_rw_context(fi, &rw_ctx);
16248ff2d290SJeff Layton 		dout("filemap_fault %p %llu drop cap refs %s ret %x\n",
16258ff2d290SJeff Layton 		     inode, off, ceph_cap_string(got), ret);
16262b1ac852SYan, Zheng 	} else
162724499847SSouptick Joarder 		err = -EAGAIN;
162861f68816SYan, Zheng 
162961f68816SYan, Zheng 	ceph_put_cap_refs(ci, got);
163061f68816SYan, Zheng 
163124499847SSouptick Joarder 	if (err != -EAGAIN)
16324f7e89f6SYan, Zheng 		goto out_restore;
163383701246SYan, Zheng 
163483701246SYan, Zheng 	/* read inline data */
163509cbfeafSKirill A. Shutemov 	if (off >= PAGE_SIZE) {
163683701246SYan, Zheng 		/* does not support inline data > PAGE_SIZE */
163783701246SYan, Zheng 		ret = VM_FAULT_SIGBUS;
163883701246SYan, Zheng 	} else {
163983701246SYan, Zheng 		struct address_space *mapping = inode->i_mapping;
1640057ba5b2SJan Kara 		struct page *page;
1641057ba5b2SJan Kara 
1642057ba5b2SJan Kara 		filemap_invalidate_lock_shared(mapping);
1643057ba5b2SJan Kara 		page = find_or_create_page(mapping, 0,
1644057ba5b2SJan Kara 				mapping_gfp_constraint(mapping, ~__GFP_FS));
164583701246SYan, Zheng 		if (!page) {
164683701246SYan, Zheng 			ret = VM_FAULT_OOM;
16474f7e89f6SYan, Zheng 			goto out_inline;
164883701246SYan, Zheng 		}
164924499847SSouptick Joarder 		err = __ceph_do_getattr(inode, page,
165083701246SYan, Zheng 					 CEPH_STAT_CAP_INLINE_DATA, true);
165124499847SSouptick Joarder 		if (err < 0 || off >= i_size_read(inode)) {
165283701246SYan, Zheng 			unlock_page(page);
165309cbfeafSKirill A. Shutemov 			put_page(page);
1654c64a2b05SSouptick Joarder 			ret = vmf_error(err);
16554f7e89f6SYan, Zheng 			goto out_inline;
165683701246SYan, Zheng 		}
165724499847SSouptick Joarder 		if (err < PAGE_SIZE)
165824499847SSouptick Joarder 			zero_user_segment(page, err, PAGE_SIZE);
165983701246SYan, Zheng 		else
166083701246SYan, Zheng 			flush_dcache_page(page);
166183701246SYan, Zheng 		SetPageUptodate(page);
166283701246SYan, Zheng 		vmf->page = page;
166383701246SYan, Zheng 		ret = VM_FAULT_MAJOR | VM_FAULT_LOCKED;
16644f7e89f6SYan, Zheng out_inline:
1665057ba5b2SJan Kara 		filemap_invalidate_unlock_shared(mapping);
16668ff2d290SJeff Layton 		dout("filemap_fault %p %llu read inline data ret %x\n",
16678ff2d290SJeff Layton 		     inode, off, ret);
16684f7e89f6SYan, Zheng 	}
16694f7e89f6SYan, Zheng out_restore:
16704f7e89f6SYan, Zheng 	ceph_restore_sigs(&oldset);
167124499847SSouptick Joarder 	if (err < 0)
167224499847SSouptick Joarder 		ret = vmf_error(err);
16736ce026e4SYan, Zheng 
167461f68816SYan, Zheng 	return ret;
167561f68816SYan, Zheng }
16761d3576fdSSage Weil 
ceph_page_mkwrite(struct vm_fault * vmf)167724499847SSouptick Joarder static vm_fault_t ceph_page_mkwrite(struct vm_fault *vmf)
16781d3576fdSSage Weil {
167911bac800SDave Jiang 	struct vm_area_struct *vma = vmf->vma;
1680496ad9aaSAl Viro 	struct inode *inode = file_inode(vma->vm_file);
168161f68816SYan, Zheng 	struct ceph_inode_info *ci = ceph_inode(inode);
168261f68816SYan, Zheng 	struct ceph_file_info *fi = vma->vm_file->private_data;
1683f66fd9f0SYan, Zheng 	struct ceph_cap_flush *prealloc_cf;
168461f68816SYan, Zheng 	struct page *page = vmf->page;
16856285bc23SAlex Elder 	loff_t off = page_offset(page);
168661f68816SYan, Zheng 	loff_t size = i_size_read(inode);
168761f68816SYan, Zheng 	size_t len;
168824499847SSouptick Joarder 	int want, got, err;
16894f7e89f6SYan, Zheng 	sigset_t oldset;
169024499847SSouptick Joarder 	vm_fault_t ret = VM_FAULT_SIGBUS;
16911d3576fdSSage Weil 
16925d6451b1SJeff Layton 	if (ceph_inode_is_shutdown(inode))
16935d6451b1SJeff Layton 		return ret;
16945d6451b1SJeff Layton 
1695f66fd9f0SYan, Zheng 	prealloc_cf = ceph_alloc_cap_flush();
1696f66fd9f0SYan, Zheng 	if (!prealloc_cf)
16976ce026e4SYan, Zheng 		return VM_FAULT_OOM;
1698f66fd9f0SYan, Zheng 
1699249c1df5SJeff Layton 	sb_start_pagefault(inode->i_sb);
17004f7e89f6SYan, Zheng 	ceph_block_sigs(&oldset);
17011d3576fdSSage Weil 
17028ff2d290SJeff Layton 	if (off + thp_size(page) <= size)
17038ff2d290SJeff Layton 		len = thp_size(page);
17041d3576fdSSage Weil 	else
17058ff2d290SJeff Layton 		len = offset_in_thp(page, size);
17061d3576fdSSage Weil 
170761f68816SYan, Zheng 	dout("page_mkwrite %p %llx.%llx %llu~%zd getting caps i_size %llu\n",
170861f68816SYan, Zheng 	     inode, ceph_vinop(inode), off, len, size);
170961f68816SYan, Zheng 	if (fi->fmode & CEPH_FILE_MODE_LAZY)
171061f68816SYan, Zheng 		want = CEPH_CAP_FILE_BUFFER | CEPH_CAP_FILE_LAZYIO;
171161f68816SYan, Zheng 	else
171261f68816SYan, Zheng 		want = CEPH_CAP_FILE_BUFFER;
17134f7e89f6SYan, Zheng 
171461f68816SYan, Zheng 	got = 0;
1715e72968e1SJeff Layton 	err = ceph_get_caps(vma->vm_file, CEPH_CAP_FILE_WR, want, off + len, &got);
171624499847SSouptick Joarder 	if (err < 0)
1717f66fd9f0SYan, Zheng 		goto out_free;
17186ce026e4SYan, Zheng 
171961f68816SYan, Zheng 	dout("page_mkwrite %p %llu~%zd got cap refs on %s\n",
172061f68816SYan, Zheng 	     inode, off, len, ceph_cap_string(got));
172161f68816SYan, Zheng 
172261f68816SYan, Zheng 	/* Update time before taking page lock */
172361f68816SYan, Zheng 	file_update_time(vma->vm_file);
17245c308356SJeff Layton 	inode_inc_iversion_raw(inode);
17254af6b225SYehuda Sadeh 
1726f0b33df5SYan, Zheng 	do {
1727d45156bfSJeff Layton 		struct ceph_snap_context *snapc;
1728d45156bfSJeff Layton 
17294af6b225SYehuda Sadeh 		lock_page(page);
17304af6b225SYehuda Sadeh 
1731cb03c143SAndreas Gruenbacher 		if (page_mkwrite_check_truncate(page, inode) < 0) {
1732f9cac5acSYan, Zheng 			unlock_page(page);
17336ce026e4SYan, Zheng 			ret = VM_FAULT_NOPAGE;
1734f0b33df5SYan, Zheng 			break;
1735f9cac5acSYan, Zheng 		}
17364af6b225SYehuda Sadeh 
1737d45156bfSJeff Layton 		snapc = ceph_find_incompatible(page);
1738d45156bfSJeff Layton 		if (!snapc) {
17394af6b225SYehuda Sadeh 			/* success.  we'll keep the page locked. */
17401d3576fdSSage Weil 			set_page_dirty(page);
17411d3576fdSSage Weil 			ret = VM_FAULT_LOCKED;
1742d45156bfSJeff Layton 			break;
17431d3576fdSSage Weil 		}
1744d45156bfSJeff Layton 
1745d45156bfSJeff Layton 		unlock_page(page);
1746d45156bfSJeff Layton 
1747d45156bfSJeff Layton 		if (IS_ERR(snapc)) {
1748d45156bfSJeff Layton 			ret = VM_FAULT_SIGBUS;
1749d45156bfSJeff Layton 			break;
1750d45156bfSJeff Layton 		}
1751d45156bfSJeff Layton 
1752d45156bfSJeff Layton 		ceph_queue_writeback(inode);
1753d45156bfSJeff Layton 		err = wait_event_killable(ci->i_cap_wq,
1754d45156bfSJeff Layton 				context_is_writeable_or_written(inode, snapc));
1755d45156bfSJeff Layton 		ceph_put_snap_context(snapc);
1756d45156bfSJeff Layton 	} while (err == 0);
1757f0b33df5SYan, Zheng 
1758083db6fdSDavid Howells 	if (ret == VM_FAULT_LOCKED) {
175961f68816SYan, Zheng 		int dirty;
176061f68816SYan, Zheng 		spin_lock(&ci->i_ceph_lock);
1761f66fd9f0SYan, Zheng 		dirty = __ceph_mark_dirty_caps(ci, CEPH_CAP_FILE_WR,
1762f66fd9f0SYan, Zheng 					       &prealloc_cf);
176361f68816SYan, Zheng 		spin_unlock(&ci->i_ceph_lock);
176461f68816SYan, Zheng 		if (dirty)
176561f68816SYan, Zheng 			__mark_inode_dirty(inode, dirty);
176661f68816SYan, Zheng 	}
176761f68816SYan, Zheng 
176824499847SSouptick Joarder 	dout("page_mkwrite %p %llu~%zd dropping cap refs on %s ret %x\n",
176961f68816SYan, Zheng 	     inode, off, len, ceph_cap_string(got), ret);
1770a8810cdcSJeff Layton 	ceph_put_cap_refs_async(ci, got);
1771f66fd9f0SYan, Zheng out_free:
17724f7e89f6SYan, Zheng 	ceph_restore_sigs(&oldset);
1773249c1df5SJeff Layton 	sb_end_pagefault(inode->i_sb);
1774f66fd9f0SYan, Zheng 	ceph_free_cap_flush(prealloc_cf);
177524499847SSouptick Joarder 	if (err < 0)
177624499847SSouptick Joarder 		ret = vmf_error(err);
17771d3576fdSSage Weil 	return ret;
17781d3576fdSSage Weil }
17791d3576fdSSage Weil 
ceph_fill_inline_data(struct inode * inode,struct page * locked_page,char * data,size_t len)178031c542a1SYan, Zheng void ceph_fill_inline_data(struct inode *inode, struct page *locked_page,
178131c542a1SYan, Zheng 			   char	*data, size_t len)
178231c542a1SYan, Zheng {
178331c542a1SYan, Zheng 	struct address_space *mapping = inode->i_mapping;
178431c542a1SYan, Zheng 	struct page *page;
178531c542a1SYan, Zheng 
178631c542a1SYan, Zheng 	if (locked_page) {
178731c542a1SYan, Zheng 		page = locked_page;
178831c542a1SYan, Zheng 	} else {
178931c542a1SYan, Zheng 		if (i_size_read(inode) == 0)
179031c542a1SYan, Zheng 			return;
179131c542a1SYan, Zheng 		page = find_or_create_page(mapping, 0,
1792c62d2555SMichal Hocko 					   mapping_gfp_constraint(mapping,
1793c62d2555SMichal Hocko 					   ~__GFP_FS));
179431c542a1SYan, Zheng 		if (!page)
179531c542a1SYan, Zheng 			return;
179631c542a1SYan, Zheng 		if (PageUptodate(page)) {
179731c542a1SYan, Zheng 			unlock_page(page);
179809cbfeafSKirill A. Shutemov 			put_page(page);
179931c542a1SYan, Zheng 			return;
180031c542a1SYan, Zheng 		}
180131c542a1SYan, Zheng 	}
180231c542a1SYan, Zheng 
18030668ff52SIlya Dryomov 	dout("fill_inline_data %p %llx.%llx len %zu locked_page %p\n",
180431c542a1SYan, Zheng 	     inode, ceph_vinop(inode), len, locked_page);
180531c542a1SYan, Zheng 
180631c542a1SYan, Zheng 	if (len > 0) {
180731c542a1SYan, Zheng 		void *kaddr = kmap_atomic(page);
180831c542a1SYan, Zheng 		memcpy(kaddr, data, len);
180931c542a1SYan, Zheng 		kunmap_atomic(kaddr);
181031c542a1SYan, Zheng 	}
181131c542a1SYan, Zheng 
181231c542a1SYan, Zheng 	if (page != locked_page) {
181309cbfeafSKirill A. Shutemov 		if (len < PAGE_SIZE)
181409cbfeafSKirill A. Shutemov 			zero_user_segment(page, len, PAGE_SIZE);
181531c542a1SYan, Zheng 		else
181631c542a1SYan, Zheng 			flush_dcache_page(page);
181731c542a1SYan, Zheng 
181831c542a1SYan, Zheng 		SetPageUptodate(page);
181931c542a1SYan, Zheng 		unlock_page(page);
182009cbfeafSKirill A. Shutemov 		put_page(page);
182131c542a1SYan, Zheng 	}
182231c542a1SYan, Zheng }
182331c542a1SYan, Zheng 
ceph_uninline_data(struct file * file)1824083db6fdSDavid Howells int ceph_uninline_data(struct file *file)
182528127bddSYan, Zheng {
1826083db6fdSDavid Howells 	struct inode *inode = file_inode(file);
182728127bddSYan, Zheng 	struct ceph_inode_info *ci = ceph_inode(inode);
1828985b9ee8SXiubo Li 	struct ceph_fs_client *fsc = ceph_inode_to_fs_client(inode);
1829825978fdSXiubo Li 	struct ceph_osd_request *req = NULL;
1830a68e564aSXiubo Li 	struct ceph_cap_flush *prealloc_cf = NULL;
1831083db6fdSDavid Howells 	struct folio *folio = NULL;
1832c38af982SDan Carpenter 	u64 inline_version = CEPH_INLINE_NONE;
1833083db6fdSDavid Howells 	struct page *pages[1];
183428127bddSYan, Zheng 	int err = 0;
1835c38af982SDan Carpenter 	u64 len;
1836083db6fdSDavid Howells 
183728127bddSYan, Zheng 	spin_lock(&ci->i_ceph_lock);
183828127bddSYan, Zheng 	inline_version = ci->i_inline_version;
183928127bddSYan, Zheng 	spin_unlock(&ci->i_ceph_lock);
184028127bddSYan, Zheng 
184128127bddSYan, Zheng 	dout("uninline_data %p %llx.%llx inline_version %llu\n",
184228127bddSYan, Zheng 	     inode, ceph_vinop(inode), inline_version);
184328127bddSYan, Zheng 
1844a68e564aSXiubo Li 	if (ceph_inode_is_shutdown(inode)) {
1845a68e564aSXiubo Li 		err = -EIO;
1846a68e564aSXiubo Li 		goto out;
1847a68e564aSXiubo Li 	}
1848a68e564aSXiubo Li 
1849825978fdSXiubo Li 	if (inline_version == CEPH_INLINE_NONE)
1850825978fdSXiubo Li 		return 0;
1851825978fdSXiubo Li 
1852825978fdSXiubo Li 	prealloc_cf = ceph_alloc_cap_flush();
1853825978fdSXiubo Li 	if (!prealloc_cf)
1854825978fdSXiubo Li 		return -ENOMEM;
1855825978fdSXiubo Li 
1856825978fdSXiubo Li 	if (inline_version == 1) /* initial version, no data */
1857825978fdSXiubo Li 		goto out_uninline;
1858825978fdSXiubo Li 
1859825978fdSXiubo Li 	folio = read_mapping_folio(inode->i_mapping, 0, file);
1860825978fdSXiubo Li 	if (IS_ERR(folio)) {
1861825978fdSXiubo Li 		err = PTR_ERR(folio);
1862825978fdSXiubo Li 		goto out;
1863825978fdSXiubo Li 	}
1864825978fdSXiubo Li 
1865825978fdSXiubo Li 	folio_lock(folio);
186628127bddSYan, Zheng 
186728127bddSYan, Zheng 	len = i_size_read(inode);
1868083db6fdSDavid Howells 	if (len > folio_size(folio))
1869083db6fdSDavid Howells 		len = folio_size(folio);
187028127bddSYan, Zheng 
187128127bddSYan, Zheng 	req = ceph_osdc_new_request(&fsc->client->osdc, &ci->i_layout,
187228127bddSYan, Zheng 				    ceph_vino(inode), 0, &len, 0, 1,
187354ea0046SIlya Dryomov 				    CEPH_OSD_OP_CREATE, CEPH_OSD_FLAG_WRITE,
187434b759b4SIlya Dryomov 				    NULL, 0, 0, false);
187528127bddSYan, Zheng 	if (IS_ERR(req)) {
187628127bddSYan, Zheng 		err = PTR_ERR(req);
1877083db6fdSDavid Howells 		goto out_unlock;
187828127bddSYan, Zheng 	}
187928127bddSYan, Zheng 
1880fac02ddfSArnd Bergmann 	req->r_mtime = inode->i_mtime;
1881a8af0d68SJeff Layton 	ceph_osdc_start_request(&fsc->client->osdc, req);
188228127bddSYan, Zheng 	err = ceph_osdc_wait_request(&fsc->client->osdc, req);
188328127bddSYan, Zheng 	ceph_osdc_put_request(req);
188428127bddSYan, Zheng 	if (err < 0)
1885083db6fdSDavid Howells 		goto out_unlock;
188628127bddSYan, Zheng 
188728127bddSYan, Zheng 	req = ceph_osdc_new_request(&fsc->client->osdc, &ci->i_layout,
188828127bddSYan, Zheng 				    ceph_vino(inode), 0, &len, 1, 3,
188954ea0046SIlya Dryomov 				    CEPH_OSD_OP_WRITE, CEPH_OSD_FLAG_WRITE,
189034b759b4SIlya Dryomov 				    NULL, ci->i_truncate_seq,
189134b759b4SIlya Dryomov 				    ci->i_truncate_size, false);
189228127bddSYan, Zheng 	if (IS_ERR(req)) {
189328127bddSYan, Zheng 		err = PTR_ERR(req);
1894083db6fdSDavid Howells 		goto out_unlock;
189528127bddSYan, Zheng 	}
189628127bddSYan, Zheng 
1897083db6fdSDavid Howells 	pages[0] = folio_page(folio, 0);
1898083db6fdSDavid Howells 	osd_req_op_extent_osd_data_pages(req, 1, pages, len, 0, false, false);
189928127bddSYan, Zheng 
1900ec137c10SYan, Zheng 	{
1901ec137c10SYan, Zheng 		__le64 xattr_buf = cpu_to_le64(inline_version);
190228127bddSYan, Zheng 		err = osd_req_op_xattr_init(req, 0, CEPH_OSD_OP_CMPXATTR,
1903ec137c10SYan, Zheng 					    "inline_version", &xattr_buf,
1904ec137c10SYan, Zheng 					    sizeof(xattr_buf),
190528127bddSYan, Zheng 					    CEPH_OSD_CMPXATTR_OP_GT,
190628127bddSYan, Zheng 					    CEPH_OSD_CMPXATTR_MODE_U64);
190728127bddSYan, Zheng 		if (err)
1908083db6fdSDavid Howells 			goto out_put_req;
1909ec137c10SYan, Zheng 	}
191028127bddSYan, Zheng 
1911ec137c10SYan, Zheng 	{
1912ec137c10SYan, Zheng 		char xattr_buf[32];
1913ec137c10SYan, Zheng 		int xattr_len = snprintf(xattr_buf, sizeof(xattr_buf),
1914ec137c10SYan, Zheng 					 "%llu", inline_version);
191528127bddSYan, Zheng 		err = osd_req_op_xattr_init(req, 2, CEPH_OSD_OP_SETXATTR,
1916ec137c10SYan, Zheng 					    "inline_version",
1917ec137c10SYan, Zheng 					    xattr_buf, xattr_len, 0, 0);
191828127bddSYan, Zheng 		if (err)
1919083db6fdSDavid Howells 			goto out_put_req;
1920ec137c10SYan, Zheng 	}
192128127bddSYan, Zheng 
1922fac02ddfSArnd Bergmann 	req->r_mtime = inode->i_mtime;
1923a8af0d68SJeff Layton 	ceph_osdc_start_request(&fsc->client->osdc, req);
192428127bddSYan, Zheng 	err = ceph_osdc_wait_request(&fsc->client->osdc, req);
192597e27aaaSXiubo Li 
19268ae99ae2SXiubo Li 	ceph_update_write_metrics(&fsc->mdsc->metric, req->r_start_latency,
1927903f4fecSXiubo Li 				  req->r_end_latency, len, err);
192897e27aaaSXiubo Li 
1929825978fdSXiubo Li out_uninline:
1930083db6fdSDavid Howells 	if (!err) {
1931083db6fdSDavid Howells 		int dirty;
1932083db6fdSDavid Howells 
1933083db6fdSDavid Howells 		/* Set to CAP_INLINE_NONE and dirty the caps */
1934083db6fdSDavid Howells 		down_read(&fsc->mdsc->snap_rwsem);
1935083db6fdSDavid Howells 		spin_lock(&ci->i_ceph_lock);
1936083db6fdSDavid Howells 		ci->i_inline_version = CEPH_INLINE_NONE;
1937083db6fdSDavid Howells 		dirty = __ceph_mark_dirty_caps(ci, CEPH_CAP_FILE_WR, &prealloc_cf);
1938083db6fdSDavid Howells 		spin_unlock(&ci->i_ceph_lock);
1939083db6fdSDavid Howells 		up_read(&fsc->mdsc->snap_rwsem);
1940083db6fdSDavid Howells 		if (dirty)
1941083db6fdSDavid Howells 			__mark_inode_dirty(inode, dirty);
1942083db6fdSDavid Howells 	}
1943083db6fdSDavid Howells out_put_req:
194428127bddSYan, Zheng 	ceph_osdc_put_request(req);
194528127bddSYan, Zheng 	if (err == -ECANCELED)
194628127bddSYan, Zheng 		err = 0;
1947083db6fdSDavid Howells out_unlock:
1948825978fdSXiubo Li 	if (folio) {
1949083db6fdSDavid Howells 		folio_unlock(folio);
1950083db6fdSDavid Howells 		folio_put(folio);
1951825978fdSXiubo Li 	}
195228127bddSYan, Zheng out:
1953083db6fdSDavid Howells 	ceph_free_cap_flush(prealloc_cf);
195428127bddSYan, Zheng 	dout("uninline_data %p %llx.%llx inline_version %llu = %d\n",
195528127bddSYan, Zheng 	     inode, ceph_vinop(inode), inline_version, err);
195628127bddSYan, Zheng 	return err;
195728127bddSYan, Zheng }
195828127bddSYan, Zheng 
19597cbea8dcSKirill A. Shutemov static const struct vm_operations_struct ceph_vmops = {
196061f68816SYan, Zheng 	.fault		= ceph_filemap_fault,
19611d3576fdSSage Weil 	.page_mkwrite	= ceph_page_mkwrite,
19621d3576fdSSage Weil };
19631d3576fdSSage Weil 
ceph_mmap(struct file * file,struct vm_area_struct * vma)19641d3576fdSSage Weil int ceph_mmap(struct file *file, struct vm_area_struct *vma)
19651d3576fdSSage Weil {
19661d3576fdSSage Weil 	struct address_space *mapping = file->f_mapping;
19671d3576fdSSage Weil 
19687e0a1265SMatthew Wilcox (Oracle) 	if (!mapping->a_ops->read_folio)
19691d3576fdSSage Weil 		return -ENOEXEC;
19701d3576fdSSage Weil 	vma->vm_ops = &ceph_vmops;
19711d3576fdSSage Weil 	return 0;
19721d3576fdSSage Weil }
197310183a69SYan, Zheng 
197410183a69SYan, Zheng enum {
197510183a69SYan, Zheng 	POOL_READ	= 1,
197610183a69SYan, Zheng 	POOL_WRITE	= 2,
197710183a69SYan, Zheng };
197810183a69SYan, Zheng 
__ceph_pool_perm_get(struct ceph_inode_info * ci,s64 pool,struct ceph_string * pool_ns)1979779fe0fbSYan, Zheng static int __ceph_pool_perm_get(struct ceph_inode_info *ci,
1980779fe0fbSYan, Zheng 				s64 pool, struct ceph_string *pool_ns)
198110183a69SYan, Zheng {
1982985b9ee8SXiubo Li 	struct ceph_fs_client *fsc = ceph_inode_to_fs_client(&ci->netfs.inode);
198310183a69SYan, Zheng 	struct ceph_mds_client *mdsc = fsc->mdsc;
198410183a69SYan, Zheng 	struct ceph_osd_request *rd_req = NULL, *wr_req = NULL;
198510183a69SYan, Zheng 	struct rb_node **p, *parent;
198610183a69SYan, Zheng 	struct ceph_pool_perm *perm;
198710183a69SYan, Zheng 	struct page **pages;
1988779fe0fbSYan, Zheng 	size_t pool_ns_len;
198910183a69SYan, Zheng 	int err = 0, err2 = 0, have = 0;
199010183a69SYan, Zheng 
199110183a69SYan, Zheng 	down_read(&mdsc->pool_perm_rwsem);
199210183a69SYan, Zheng 	p = &mdsc->pool_perm_tree.rb_node;
199310183a69SYan, Zheng 	while (*p) {
199410183a69SYan, Zheng 		perm = rb_entry(*p, struct ceph_pool_perm, node);
199510183a69SYan, Zheng 		if (pool < perm->pool)
199610183a69SYan, Zheng 			p = &(*p)->rb_left;
199710183a69SYan, Zheng 		else if (pool > perm->pool)
199810183a69SYan, Zheng 			p = &(*p)->rb_right;
199910183a69SYan, Zheng 		else {
2000779fe0fbSYan, Zheng 			int ret = ceph_compare_string(pool_ns,
2001779fe0fbSYan, Zheng 						perm->pool_ns,
2002779fe0fbSYan, Zheng 						perm->pool_ns_len);
2003779fe0fbSYan, Zheng 			if (ret < 0)
2004779fe0fbSYan, Zheng 				p = &(*p)->rb_left;
2005779fe0fbSYan, Zheng 			else if (ret > 0)
2006779fe0fbSYan, Zheng 				p = &(*p)->rb_right;
2007779fe0fbSYan, Zheng 			else {
200810183a69SYan, Zheng 				have = perm->perm;
200910183a69SYan, Zheng 				break;
201010183a69SYan, Zheng 			}
201110183a69SYan, Zheng 		}
2012779fe0fbSYan, Zheng 	}
201310183a69SYan, Zheng 	up_read(&mdsc->pool_perm_rwsem);
201410183a69SYan, Zheng 	if (*p)
201510183a69SYan, Zheng 		goto out;
201610183a69SYan, Zheng 
2017779fe0fbSYan, Zheng 	if (pool_ns)
2018779fe0fbSYan, Zheng 		dout("__ceph_pool_perm_get pool %lld ns %.*s no perm cached\n",
2019779fe0fbSYan, Zheng 		     pool, (int)pool_ns->len, pool_ns->str);
2020779fe0fbSYan, Zheng 	else
20217627151eSYan, Zheng 		dout("__ceph_pool_perm_get pool %lld no perm cached\n", pool);
202210183a69SYan, Zheng 
202310183a69SYan, Zheng 	down_write(&mdsc->pool_perm_rwsem);
2024779fe0fbSYan, Zheng 	p = &mdsc->pool_perm_tree.rb_node;
202510183a69SYan, Zheng 	parent = NULL;
202610183a69SYan, Zheng 	while (*p) {
202710183a69SYan, Zheng 		parent = *p;
202810183a69SYan, Zheng 		perm = rb_entry(parent, struct ceph_pool_perm, node);
202910183a69SYan, Zheng 		if (pool < perm->pool)
203010183a69SYan, Zheng 			p = &(*p)->rb_left;
203110183a69SYan, Zheng 		else if (pool > perm->pool)
203210183a69SYan, Zheng 			p = &(*p)->rb_right;
203310183a69SYan, Zheng 		else {
2034779fe0fbSYan, Zheng 			int ret = ceph_compare_string(pool_ns,
2035779fe0fbSYan, Zheng 						perm->pool_ns,
2036779fe0fbSYan, Zheng 						perm->pool_ns_len);
2037779fe0fbSYan, Zheng 			if (ret < 0)
2038779fe0fbSYan, Zheng 				p = &(*p)->rb_left;
2039779fe0fbSYan, Zheng 			else if (ret > 0)
2040779fe0fbSYan, Zheng 				p = &(*p)->rb_right;
2041779fe0fbSYan, Zheng 			else {
204210183a69SYan, Zheng 				have = perm->perm;
204310183a69SYan, Zheng 				break;
204410183a69SYan, Zheng 			}
204510183a69SYan, Zheng 		}
2046779fe0fbSYan, Zheng 	}
204710183a69SYan, Zheng 	if (*p) {
204810183a69SYan, Zheng 		up_write(&mdsc->pool_perm_rwsem);
204910183a69SYan, Zheng 		goto out;
205010183a69SYan, Zheng 	}
205110183a69SYan, Zheng 
205234b759b4SIlya Dryomov 	rd_req = ceph_osdc_alloc_request(&fsc->client->osdc, NULL,
205310183a69SYan, Zheng 					 1, false, GFP_NOFS);
205410183a69SYan, Zheng 	if (!rd_req) {
205510183a69SYan, Zheng 		err = -ENOMEM;
205610183a69SYan, Zheng 		goto out_unlock;
205710183a69SYan, Zheng 	}
205810183a69SYan, Zheng 
205910183a69SYan, Zheng 	rd_req->r_flags = CEPH_OSD_FLAG_READ;
206010183a69SYan, Zheng 	osd_req_op_init(rd_req, 0, CEPH_OSD_OP_STAT, 0);
206110183a69SYan, Zheng 	rd_req->r_base_oloc.pool = pool;
2062779fe0fbSYan, Zheng 	if (pool_ns)
2063779fe0fbSYan, Zheng 		rd_req->r_base_oloc.pool_ns = ceph_get_string(pool_ns);
2064d30291b9SIlya Dryomov 	ceph_oid_printf(&rd_req->r_base_oid, "%llx.00000000", ci->i_vino.ino);
206510183a69SYan, Zheng 
206613d1ad16SIlya Dryomov 	err = ceph_osdc_alloc_messages(rd_req, GFP_NOFS);
206713d1ad16SIlya Dryomov 	if (err)
206813d1ad16SIlya Dryomov 		goto out_unlock;
206910183a69SYan, Zheng 
207034b759b4SIlya Dryomov 	wr_req = ceph_osdc_alloc_request(&fsc->client->osdc, NULL,
207110183a69SYan, Zheng 					 1, false, GFP_NOFS);
207210183a69SYan, Zheng 	if (!wr_req) {
207310183a69SYan, Zheng 		err = -ENOMEM;
207410183a69SYan, Zheng 		goto out_unlock;
207510183a69SYan, Zheng 	}
207610183a69SYan, Zheng 
207754ea0046SIlya Dryomov 	wr_req->r_flags = CEPH_OSD_FLAG_WRITE;
207810183a69SYan, Zheng 	osd_req_op_init(wr_req, 0, CEPH_OSD_OP_CREATE, CEPH_OSD_OP_FLAG_EXCL);
207963244fa1SIlya Dryomov 	ceph_oloc_copy(&wr_req->r_base_oloc, &rd_req->r_base_oloc);
2080d30291b9SIlya Dryomov 	ceph_oid_copy(&wr_req->r_base_oid, &rd_req->r_base_oid);
208110183a69SYan, Zheng 
208213d1ad16SIlya Dryomov 	err = ceph_osdc_alloc_messages(wr_req, GFP_NOFS);
208313d1ad16SIlya Dryomov 	if (err)
208413d1ad16SIlya Dryomov 		goto out_unlock;
208510183a69SYan, Zheng 
208610183a69SYan, Zheng 	/* one page should be large enough for STAT data */
208710183a69SYan, Zheng 	pages = ceph_alloc_page_vector(1, GFP_KERNEL);
208810183a69SYan, Zheng 	if (IS_ERR(pages)) {
208910183a69SYan, Zheng 		err = PTR_ERR(pages);
209010183a69SYan, Zheng 		goto out_unlock;
209110183a69SYan, Zheng 	}
209210183a69SYan, Zheng 
209310183a69SYan, Zheng 	osd_req_op_raw_data_in_pages(rd_req, 0, pages, PAGE_SIZE,
209410183a69SYan, Zheng 				     0, false, true);
2095a8af0d68SJeff Layton 	ceph_osdc_start_request(&fsc->client->osdc, rd_req);
209610183a69SYan, Zheng 
2097874c8ca1SDavid Howells 	wr_req->r_mtime = ci->netfs.inode.i_mtime;
2098a8af0d68SJeff Layton 	ceph_osdc_start_request(&fsc->client->osdc, wr_req);
209910183a69SYan, Zheng 
210010183a69SYan, Zheng 	err = ceph_osdc_wait_request(&fsc->client->osdc, rd_req);
210110183a69SYan, Zheng 	err2 = ceph_osdc_wait_request(&fsc->client->osdc, wr_req);
210210183a69SYan, Zheng 
210310183a69SYan, Zheng 	if (err >= 0 || err == -ENOENT)
210410183a69SYan, Zheng 		have |= POOL_READ;
2105131d7eb4SYan, Zheng 	else if (err != -EPERM) {
21060b98acd6SIlya Dryomov 		if (err == -EBLOCKLISTED)
21070b98acd6SIlya Dryomov 			fsc->blocklisted = true;
210810183a69SYan, Zheng 		goto out_unlock;
2109131d7eb4SYan, Zheng 	}
211010183a69SYan, Zheng 
211110183a69SYan, Zheng 	if (err2 == 0 || err2 == -EEXIST)
211210183a69SYan, Zheng 		have |= POOL_WRITE;
211310183a69SYan, Zheng 	else if (err2 != -EPERM) {
21140b98acd6SIlya Dryomov 		if (err2 == -EBLOCKLISTED)
21150b98acd6SIlya Dryomov 			fsc->blocklisted = true;
211610183a69SYan, Zheng 		err = err2;
211710183a69SYan, Zheng 		goto out_unlock;
211810183a69SYan, Zheng 	}
211910183a69SYan, Zheng 
2120779fe0fbSYan, Zheng 	pool_ns_len = pool_ns ? pool_ns->len : 0;
2121779fe0fbSYan, Zheng 	perm = kmalloc(sizeof(*perm) + pool_ns_len + 1, GFP_NOFS);
212210183a69SYan, Zheng 	if (!perm) {
212310183a69SYan, Zheng 		err = -ENOMEM;
212410183a69SYan, Zheng 		goto out_unlock;
212510183a69SYan, Zheng 	}
212610183a69SYan, Zheng 
212710183a69SYan, Zheng 	perm->pool = pool;
212810183a69SYan, Zheng 	perm->perm = have;
2129779fe0fbSYan, Zheng 	perm->pool_ns_len = pool_ns_len;
2130779fe0fbSYan, Zheng 	if (pool_ns_len > 0)
2131779fe0fbSYan, Zheng 		memcpy(perm->pool_ns, pool_ns->str, pool_ns_len);
2132779fe0fbSYan, Zheng 	perm->pool_ns[pool_ns_len] = 0;
2133779fe0fbSYan, Zheng 
213410183a69SYan, Zheng 	rb_link_node(&perm->node, parent, p);
213510183a69SYan, Zheng 	rb_insert_color(&perm->node, &mdsc->pool_perm_tree);
213610183a69SYan, Zheng 	err = 0;
213710183a69SYan, Zheng out_unlock:
213810183a69SYan, Zheng 	up_write(&mdsc->pool_perm_rwsem);
213910183a69SYan, Zheng 
214010183a69SYan, Zheng 	ceph_osdc_put_request(rd_req);
214110183a69SYan, Zheng 	ceph_osdc_put_request(wr_req);
214210183a69SYan, Zheng out:
214310183a69SYan, Zheng 	if (!err)
214410183a69SYan, Zheng 		err = have;
2145779fe0fbSYan, Zheng 	if (pool_ns)
2146779fe0fbSYan, Zheng 		dout("__ceph_pool_perm_get pool %lld ns %.*s result = %d\n",
2147779fe0fbSYan, Zheng 		     pool, (int)pool_ns->len, pool_ns->str, err);
2148779fe0fbSYan, Zheng 	else
21497627151eSYan, Zheng 		dout("__ceph_pool_perm_get pool %lld result = %d\n", pool, err);
215010183a69SYan, Zheng 	return err;
215110183a69SYan, Zheng }
215210183a69SYan, Zheng 
ceph_pool_perm_check(struct inode * inode,int need)21535e3ded1bSYan, Zheng int ceph_pool_perm_check(struct inode *inode, int need)
215410183a69SYan, Zheng {
21555e3ded1bSYan, Zheng 	struct ceph_inode_info *ci = ceph_inode(inode);
2156779fe0fbSYan, Zheng 	struct ceph_string *pool_ns;
21575e3ded1bSYan, Zheng 	s64 pool;
215810183a69SYan, Zheng 	int ret, flags;
215910183a69SYan, Zheng 
2160e9b22501SJeff Layton 	/* Only need to do this for regular files */
2161e9b22501SJeff Layton 	if (!S_ISREG(inode->i_mode))
2162e9b22501SJeff Layton 		return 0;
2163e9b22501SJeff Layton 
216480e80fbbSYan, Zheng 	if (ci->i_vino.snap != CEPH_NOSNAP) {
216580e80fbbSYan, Zheng 		/*
216680e80fbbSYan, Zheng 		 * Pool permission check needs to write to the first object.
216780e80fbbSYan, Zheng 		 * But for snapshot, head of the first object may have alread
216880e80fbbSYan, Zheng 		 * been deleted. Skip check to avoid creating orphan object.
216980e80fbbSYan, Zheng 		 */
217080e80fbbSYan, Zheng 		return 0;
217180e80fbbSYan, Zheng 	}
217280e80fbbSYan, Zheng 
2173985b9ee8SXiubo Li 	if (ceph_test_mount_opt(ceph_inode_to_fs_client(inode),
217410183a69SYan, Zheng 				NOPOOLPERM))
217510183a69SYan, Zheng 		return 0;
217610183a69SYan, Zheng 
217710183a69SYan, Zheng 	spin_lock(&ci->i_ceph_lock);
217810183a69SYan, Zheng 	flags = ci->i_ceph_flags;
21797627151eSYan, Zheng 	pool = ci->i_layout.pool_id;
218010183a69SYan, Zheng 	spin_unlock(&ci->i_ceph_lock);
218110183a69SYan, Zheng check:
218210183a69SYan, Zheng 	if (flags & CEPH_I_POOL_PERM) {
218310183a69SYan, Zheng 		if ((need & CEPH_CAP_FILE_RD) && !(flags & CEPH_I_POOL_RD)) {
21847627151eSYan, Zheng 			dout("ceph_pool_perm_check pool %lld no read perm\n",
218510183a69SYan, Zheng 			     pool);
218610183a69SYan, Zheng 			return -EPERM;
218710183a69SYan, Zheng 		}
218810183a69SYan, Zheng 		if ((need & CEPH_CAP_FILE_WR) && !(flags & CEPH_I_POOL_WR)) {
21897627151eSYan, Zheng 			dout("ceph_pool_perm_check pool %lld no write perm\n",
219010183a69SYan, Zheng 			     pool);
219110183a69SYan, Zheng 			return -EPERM;
219210183a69SYan, Zheng 		}
219310183a69SYan, Zheng 		return 0;
219410183a69SYan, Zheng 	}
219510183a69SYan, Zheng 
2196779fe0fbSYan, Zheng 	pool_ns = ceph_try_get_string(ci->i_layout.pool_ns);
2197779fe0fbSYan, Zheng 	ret = __ceph_pool_perm_get(ci, pool, pool_ns);
2198779fe0fbSYan, Zheng 	ceph_put_string(pool_ns);
219910183a69SYan, Zheng 	if (ret < 0)
220010183a69SYan, Zheng 		return ret;
220110183a69SYan, Zheng 
220210183a69SYan, Zheng 	flags = CEPH_I_POOL_PERM;
220310183a69SYan, Zheng 	if (ret & POOL_READ)
220410183a69SYan, Zheng 		flags |= CEPH_I_POOL_RD;
220510183a69SYan, Zheng 	if (ret & POOL_WRITE)
220610183a69SYan, Zheng 		flags |= CEPH_I_POOL_WR;
220710183a69SYan, Zheng 
220810183a69SYan, Zheng 	spin_lock(&ci->i_ceph_lock);
2209779fe0fbSYan, Zheng 	if (pool == ci->i_layout.pool_id &&
2210779fe0fbSYan, Zheng 	    pool_ns == rcu_dereference_raw(ci->i_layout.pool_ns)) {
2211779fe0fbSYan, Zheng 		ci->i_ceph_flags |= flags;
221210183a69SYan, Zheng         } else {
22137627151eSYan, Zheng 		pool = ci->i_layout.pool_id;
221410183a69SYan, Zheng 		flags = ci->i_ceph_flags;
221510183a69SYan, Zheng 	}
221610183a69SYan, Zheng 	spin_unlock(&ci->i_ceph_lock);
221710183a69SYan, Zheng 	goto check;
221810183a69SYan, Zheng }
221910183a69SYan, Zheng 
ceph_pool_perm_destroy(struct ceph_mds_client * mdsc)222010183a69SYan, Zheng void ceph_pool_perm_destroy(struct ceph_mds_client *mdsc)
222110183a69SYan, Zheng {
222210183a69SYan, Zheng 	struct ceph_pool_perm *perm;
222310183a69SYan, Zheng 	struct rb_node *n;
222410183a69SYan, Zheng 
222510183a69SYan, Zheng 	while (!RB_EMPTY_ROOT(&mdsc->pool_perm_tree)) {
222610183a69SYan, Zheng 		n = rb_first(&mdsc->pool_perm_tree);
222710183a69SYan, Zheng 		perm = rb_entry(n, struct ceph_pool_perm, node);
222810183a69SYan, Zheng 		rb_erase(n, &mdsc->pool_perm_tree);
222910183a69SYan, Zheng 		kfree(perm);
223010183a69SYan, Zheng 	}
223110183a69SYan, Zheng }
2232