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 if (__ceph_have_pending_cap_snap(ci)) {
995dda377cSYan, Zheng struct ceph_cap_snap *capsnap =
1005dda377cSYan, Zheng list_last_entry(&ci->i_cap_snaps,
1015dda377cSYan, Zheng struct ceph_cap_snap,
1025dda377cSYan, Zheng ci_item);
1035dda377cSYan, Zheng snapc = ceph_get_snap_context(capsnap->context);
1045dda377cSYan, Zheng capsnap->dirty_pages++;
1055dda377cSYan, Zheng } else {
1065dda377cSYan, Zheng BUG_ON(!ci->i_head_snapc);
1075dda377cSYan, Zheng snapc = ceph_get_snap_context(ci->i_head_snapc);
1081d3576fdSSage Weil ++ci->i_wrbuffer_ref_head;
1095dda377cSYan, Zheng }
1101d3576fdSSage Weil if (ci->i_wrbuffer_ref == 0)
1110444d76aSDave Chinner ihold(inode);
1121d3576fdSSage Weil ++ci->i_wrbuffer_ref;
1138fb72b4aSMatthew Wilcox (Oracle) dout("%p dirty_folio %p idx %lu head %d/%d -> %d/%d "
1141d3576fdSSage Weil "snapc %p seq %lld (%d snaps)\n",
1158fb72b4aSMatthew Wilcox (Oracle) mapping->host, folio, folio->index,
1161d3576fdSSage Weil ci->i_wrbuffer_ref-1, ci->i_wrbuffer_ref_head-1,
1171d3576fdSSage Weil ci->i_wrbuffer_ref, ci->i_wrbuffer_ref_head,
1181d3576fdSSage Weil snapc, snapc->seq, snapc->num_snaps);
119be655596SSage Weil spin_unlock(&ci->i_ceph_lock);
1201d3576fdSSage Weil
1211d3576fdSSage Weil /*
1228fb72b4aSMatthew Wilcox (Oracle) * Reference snap context in folio->private. Also set
1239872f4deSMatthew Wilcox (Oracle) * PagePrivate so that we get invalidate_folio callback.
1241d3576fdSSage Weil */
125020bc44aSJeff Layton VM_WARN_ON_FOLIO(folio->private, folio);
1268fb72b4aSMatthew Wilcox (Oracle) folio_attach_private(folio, snapc);
1271d3576fdSSage Weil
1288fb72b4aSMatthew Wilcox (Oracle) return ceph_fscache_dirty_folio(mapping, folio);
1291d3576fdSSage Weil }
1301d3576fdSSage Weil
1311d3576fdSSage Weil /*
1329872f4deSMatthew Wilcox (Oracle) * If we are truncating the full folio (i.e. offset == 0), adjust the
1339872f4deSMatthew Wilcox (Oracle) * dirty folio counters appropriately. Only called if there is private
1349872f4deSMatthew Wilcox (Oracle) * data on the folio.
1351d3576fdSSage Weil */
ceph_invalidate_folio(struct folio * folio,size_t offset,size_t length)1369872f4deSMatthew Wilcox (Oracle) static void ceph_invalidate_folio(struct folio *folio, size_t offset,
1379872f4deSMatthew Wilcox (Oracle) size_t length)
1381d3576fdSSage Weil {
1394ce1e9adSAlexander Beregalov struct inode *inode;
1401d3576fdSSage Weil struct ceph_inode_info *ci;
141379fc7faSJeff Layton struct ceph_snap_context *snapc;
1421d3576fdSSage Weil
1439872f4deSMatthew Wilcox (Oracle) inode = folio->mapping->host;
144b150f5c1SMilosz Tanski ci = ceph_inode(inode);
145b150f5c1SMilosz Tanski
1469872f4deSMatthew Wilcox (Oracle) if (offset != 0 || length != folio_size(folio)) {
1479872f4deSMatthew Wilcox (Oracle) dout("%p invalidate_folio idx %lu partial dirty page %zu~%zu\n",
1489872f4deSMatthew Wilcox (Oracle) inode, folio->index, offset, length);
149b150f5c1SMilosz Tanski return;
150b150f5c1SMilosz Tanski }
1514ce1e9adSAlexander Beregalov
1529872f4deSMatthew Wilcox (Oracle) WARN_ON(!folio_test_locked(folio));
153642d51fbSXiubo Li if (folio_test_private(folio)) {
1549872f4deSMatthew Wilcox (Oracle) dout("%p invalidate_folio idx %lu full dirty page\n",
1559872f4deSMatthew Wilcox (Oracle) inode, folio->index);
156b150f5c1SMilosz Tanski
1579872f4deSMatthew Wilcox (Oracle) snapc = folio_detach_private(folio);
1581d3576fdSSage Weil ceph_put_wrbuffer_cap_refs(ci, 1, snapc);
1591d3576fdSSage Weil ceph_put_snap_context(snapc);
1601d3576fdSSage Weil }
1611d3576fdSSage Weil
1629872f4deSMatthew Wilcox (Oracle) folio_wait_fscache(folio);
163400e1286SJeff Layton }
164400e1286SJeff Layton
ceph_release_folio(struct folio * folio,gfp_t gfp)1655e414655SMatthew Wilcox (Oracle) static bool ceph_release_folio(struct folio *folio, gfp_t gfp)
1661d3576fdSSage Weil {
1675e414655SMatthew Wilcox (Oracle) struct inode *inode = folio->mapping->host;
168400e1286SJeff Layton
1695e414655SMatthew Wilcox (Oracle) dout("%llx:%llx release_folio idx %lu (%sdirty)\n",
1705e414655SMatthew Wilcox (Oracle) ceph_vinop(inode),
1715e414655SMatthew Wilcox (Oracle) folio->index, folio_test_dirty(folio) ? "" : "not ");
172400e1286SJeff Layton
1735e414655SMatthew Wilcox (Oracle) if (folio_test_private(folio))
1745e414655SMatthew Wilcox (Oracle) return false;
17599ccbd22SMilosz Tanski
1765e414655SMatthew Wilcox (Oracle) if (folio_test_fscache(folio)) {
177d7bdba1cSDavid Howells if (current_is_kswapd() || !(gfp & __GFP_FS))
1785e414655SMatthew Wilcox (Oracle) return false;
1795e414655SMatthew Wilcox (Oracle) folio_wait_fscache(folio);
1807c46b318SJeff Layton }
181400e1286SJeff Layton ceph_fscache_note_page_release(inode);
1825e414655SMatthew Wilcox (Oracle) return true;
1831d3576fdSSage Weil }
1841d3576fdSSage Weil
ceph_netfs_expand_readahead(struct netfs_io_request * rreq)1856a19114bSDavid Howells static void ceph_netfs_expand_readahead(struct netfs_io_request *rreq)
186f0702876SJeff Layton {
187a25cedb4SJeff Layton struct inode *inode = rreq->inode;
188f0702876SJeff Layton struct ceph_inode_info *ci = ceph_inode(inode);
189f0702876SJeff Layton struct ceph_file_layout *lo = &ci->i_layout;
190dc94bb8fSXiubo Li unsigned long max_pages = inode->i_sb->s_bdi->ra_pages;
191dc94bb8fSXiubo Li loff_t end = rreq->start + rreq->len, new_end;
192dc94bb8fSXiubo Li struct ceph_netfs_request_data *priv = rreq->netfs_priv;
193dc94bb8fSXiubo Li unsigned long max_len;
194f0702876SJeff Layton u32 blockoff;
195f0702876SJeff Layton
196dc94bb8fSXiubo Li if (priv) {
197dc94bb8fSXiubo Li /* Readahead is disabled by posix_fadvise POSIX_FADV_RANDOM */
198dc94bb8fSXiubo Li if (priv->file_ra_disabled)
199dc94bb8fSXiubo Li max_pages = 0;
200dc94bb8fSXiubo Li else
201dc94bb8fSXiubo Li max_pages = priv->file_ra_pages;
202dc94bb8fSXiubo Li
203dc94bb8fSXiubo Li }
204dc94bb8fSXiubo Li
205dc94bb8fSXiubo Li /* Readahead is disabled */
206dc94bb8fSXiubo Li if (!max_pages)
207dc94bb8fSXiubo Li return;
208dc94bb8fSXiubo Li
209dc94bb8fSXiubo Li max_len = max_pages << PAGE_SHIFT;
210dc94bb8fSXiubo Li
211dc94bb8fSXiubo Li /*
212dc94bb8fSXiubo Li * Try to expand the length forward by rounding up it to the next
213dc94bb8fSXiubo Li * block, but do not exceed the file size, unless the original
214dc94bb8fSXiubo Li * request already exceeds it.
215dc94bb8fSXiubo Li */
216dc94bb8fSXiubo Li new_end = min(round_up(end, lo->stripe_unit), rreq->i_size);
217dc94bb8fSXiubo Li if (new_end > end && new_end <= rreq->start + max_len)
218dc94bb8fSXiubo Li rreq->len = new_end - rreq->start;
219dc94bb8fSXiubo Li
220dc94bb8fSXiubo Li /* Try to expand the start downward */
221dc94bb8fSXiubo Li div_u64_rem(rreq->start, lo->stripe_unit, &blockoff);
222dc94bb8fSXiubo Li if (rreq->len + blockoff <= max_len) {
223dc94bb8fSXiubo Li rreq->start -= blockoff;
224f0702876SJeff Layton rreq->len += blockoff;
225dc94bb8fSXiubo Li }
226f0702876SJeff Layton }
227f0702876SJeff Layton
ceph_netfs_clamp_length(struct netfs_io_subrequest * subreq)2286a19114bSDavid Howells static bool ceph_netfs_clamp_length(struct netfs_io_subrequest *subreq)
229f0702876SJeff Layton {
230a25cedb4SJeff Layton struct inode *inode = subreq->rreq->inode;
231985b9ee8SXiubo Li struct ceph_fs_client *fsc = ceph_inode_to_fs_client(inode);
232f0702876SJeff Layton struct ceph_inode_info *ci = ceph_inode(inode);
233f0702876SJeff Layton u64 objno, objoff;
234f0702876SJeff Layton u32 xlen;
235f0702876SJeff Layton
236f0702876SJeff Layton /* Truncate the extent at the end of the current block */
237f0702876SJeff Layton ceph_calc_file_object_mapping(&ci->i_layout, subreq->start, subreq->len,
238f0702876SJeff Layton &objno, &objoff, &xlen);
239f0702876SJeff Layton subreq->len = min(xlen, fsc->mount_options->rsize);
240f0702876SJeff Layton return true;
241f0702876SJeff Layton }
242f0702876SJeff Layton
finish_netfs_read(struct ceph_osd_request * req)243f0702876SJeff Layton static void finish_netfs_read(struct ceph_osd_request *req)
244f0702876SJeff Layton {
245f0fe1e54SJeff Layton struct inode *inode = req->r_inode;
246985b9ee8SXiubo Li struct ceph_fs_client *fsc = ceph_inode_to_fs_client(inode);
247f0702876SJeff Layton struct ceph_osd_data *osd_data = osd_req_op_extent_osd_data(req, 0);
2486a19114bSDavid Howells struct netfs_io_subrequest *subreq = req->r_priv;
24903bc06c7SJeff Layton struct ceph_osd_req_op *op = &req->r_ops[0];
250f0702876SJeff Layton int err = req->r_result;
25103bc06c7SJeff Layton bool sparse = (op->op == CEPH_OSD_OP_SPARSE_READ);
252f0702876SJeff Layton
2538ae99ae2SXiubo Li ceph_update_read_metrics(&fsc->mdsc->metric, req->r_start_latency,
254903f4fecSXiubo Li req->r_end_latency, osd_data->length, err);
255f0702876SJeff Layton
256f0702876SJeff Layton dout("%s: result %d subreq->len=%zu i_size=%lld\n", __func__, req->r_result,
257f0702876SJeff Layton subreq->len, i_size_read(req->r_inode));
258f0702876SJeff Layton
259f0702876SJeff Layton /* no object means success but no data */
260f0fe1e54SJeff Layton if (err == -ENOENT)
261f0702876SJeff Layton err = 0;
262f0702876SJeff Layton else if (err == -EBLOCKLISTED)
263f0702876SJeff Layton fsc->blocklisted = true;
264f0702876SJeff Layton
265f0fe1e54SJeff Layton if (err >= 0) {
266f0fe1e54SJeff Layton if (sparse && err > 0)
267f0fe1e54SJeff Layton err = ceph_sparse_ext_map_end(op);
268f0fe1e54SJeff Layton if (err < subreq->len)
269f0702876SJeff Layton __set_bit(NETFS_SREQ_CLEAR_TAIL, &subreq->flags);
270f0fe1e54SJeff Layton if (IS_ENCRYPTED(inode) && err > 0) {
271f0fe1e54SJeff Layton err = ceph_fscrypt_decrypt_extents(inode,
272f0fe1e54SJeff Layton osd_data->pages, subreq->start,
273f0fe1e54SJeff Layton op->extent.sparse_ext,
274f0fe1e54SJeff Layton op->extent.sparse_ext_cnt);
275f0fe1e54SJeff Layton if (err > subreq->len)
276f0fe1e54SJeff Layton err = subreq->len;
277f0fe1e54SJeff Layton }
278f0fe1e54SJeff Layton }
279f0702876SJeff Layton
280f0fe1e54SJeff Layton if (osd_data->type == CEPH_OSD_DATA_TYPE_PAGES) {
281f0fe1e54SJeff Layton ceph_put_page_vector(osd_data->pages,
282f0fe1e54SJeff Layton calc_pages_for(osd_data->alignment,
283f0fe1e54SJeff Layton osd_data->length), false);
284f0fe1e54SJeff Layton }
2857467b044SJeff Layton netfs_subreq_terminated(subreq, err, false);
286f0702876SJeff Layton iput(req->r_inode);
2871464de9fSXiubo Li ceph_dec_osd_stopping_blocker(fsc->mdsc);
288f0702876SJeff Layton }
289f0702876SJeff Layton
ceph_netfs_issue_op_inline(struct netfs_io_subrequest * subreq)2906a19114bSDavid Howells static bool ceph_netfs_issue_op_inline(struct netfs_io_subrequest *subreq)
2915b19f1ebSDavid Howells {
2926a19114bSDavid Howells struct netfs_io_request *rreq = subreq->rreq;
2935b19f1ebSDavid Howells struct inode *inode = rreq->inode;
2945b19f1ebSDavid Howells struct ceph_mds_reply_info_parsed *rinfo;
2955b19f1ebSDavid Howells struct ceph_mds_reply_info_in *iinfo;
2965b19f1ebSDavid Howells struct ceph_mds_request *req;
2975b19f1ebSDavid Howells struct ceph_mds_client *mdsc = ceph_sb_to_mdsc(inode->i_sb);
2985b19f1ebSDavid Howells struct ceph_inode_info *ci = ceph_inode(inode);
2995b19f1ebSDavid Howells struct iov_iter iter;
3005b19f1ebSDavid Howells ssize_t err = 0;
3015b19f1ebSDavid Howells size_t len;
3025eed80fbSXiubo Li int mode;
3035b19f1ebSDavid Howells
3045b19f1ebSDavid Howells __set_bit(NETFS_SREQ_CLEAR_TAIL, &subreq->flags);
305f18a3785SDavid Howells __clear_bit(NETFS_SREQ_COPY_TO_CACHE, &subreq->flags);
3065b19f1ebSDavid Howells
3075b19f1ebSDavid Howells if (subreq->start >= inode->i_size)
3085b19f1ebSDavid Howells goto out;
3095b19f1ebSDavid Howells
3105b19f1ebSDavid Howells /* We need to fetch the inline data. */
3115eed80fbSXiubo Li mode = ceph_try_to_choose_auth_mds(inode, CEPH_STAT_CAP_INLINE_DATA);
3125eed80fbSXiubo Li req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_GETATTR, mode);
3135b19f1ebSDavid Howells if (IS_ERR(req)) {
3145b19f1ebSDavid Howells err = PTR_ERR(req);
3155b19f1ebSDavid Howells goto out;
3165b19f1ebSDavid Howells }
3175b19f1ebSDavid Howells req->r_ino1 = ci->i_vino;
3185b19f1ebSDavid Howells req->r_args.getattr.mask = cpu_to_le32(CEPH_STAT_CAP_INLINE_DATA);
3195b19f1ebSDavid Howells req->r_num_caps = 2;
3205b19f1ebSDavid Howells
3215b19f1ebSDavid Howells err = ceph_mdsc_do_request(mdsc, NULL, req);
3225b19f1ebSDavid Howells if (err < 0)
3235b19f1ebSDavid Howells goto out;
3245b19f1ebSDavid Howells
3255b19f1ebSDavid Howells rinfo = &req->r_reply_info;
3265b19f1ebSDavid Howells iinfo = &rinfo->targeti;
3275b19f1ebSDavid Howells if (iinfo->inline_version == CEPH_INLINE_NONE) {
3285b19f1ebSDavid Howells /* The data got uninlined */
3295b19f1ebSDavid Howells ceph_mdsc_put_request(req);
3305b19f1ebSDavid Howells return false;
3315b19f1ebSDavid Howells }
3325b19f1ebSDavid Howells
3335b19f1ebSDavid Howells len = min_t(size_t, iinfo->inline_len - subreq->start, subreq->len);
334de4eda9dSAl Viro iov_iter_xarray(&iter, ITER_DEST, &rreq->mapping->i_pages, subreq->start, len);
3355b19f1ebSDavid Howells err = copy_to_iter(iinfo->inline_data + subreq->start, len, &iter);
3365b19f1ebSDavid Howells if (err == 0)
3375b19f1ebSDavid Howells err = -EFAULT;
3385b19f1ebSDavid Howells
3395b19f1ebSDavid Howells ceph_mdsc_put_request(req);
3405b19f1ebSDavid Howells out:
3415b19f1ebSDavid Howells netfs_subreq_terminated(subreq, err, false);
3425b19f1ebSDavid Howells return true;
3435b19f1ebSDavid Howells }
3445b19f1ebSDavid Howells
ceph_netfs_issue_read(struct netfs_io_subrequest * subreq)345f18a3785SDavid Howells static void ceph_netfs_issue_read(struct netfs_io_subrequest *subreq)
346f0702876SJeff Layton {
3476a19114bSDavid Howells struct netfs_io_request *rreq = subreq->rreq;
348a25cedb4SJeff Layton struct inode *inode = rreq->inode;
349f0702876SJeff Layton struct ceph_inode_info *ci = ceph_inode(inode);
350985b9ee8SXiubo Li struct ceph_fs_client *fsc = ceph_inode_to_fs_client(inode);
351a68e564aSXiubo Li struct ceph_osd_request *req = NULL;
352f0702876SJeff Layton struct ceph_vino vino = ceph_vino(inode);
353f0702876SJeff Layton struct iov_iter iter;
354f0702876SJeff Layton int err = 0;
355f0702876SJeff Layton u64 len = subreq->len;
356f0fe1e54SJeff Layton bool sparse = IS_ENCRYPTED(inode) || ceph_test_mount_opt(fsc, SPARSEREAD);
357f0fe1e54SJeff Layton u64 off = subreq->start;
358*fb98248fSXiubo Li int extent_cnt;
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) {
381*fb98248fSXiubo Li extent_cnt = __ceph_sparse_read_ext_count(inode, len);
382*fb98248fSXiubo Li err = ceph_alloc_sparse_ext_map(&req->r_ops[0], extent_cnt);
38303bc06c7SJeff Layton if (err)
38403bc06c7SJeff Layton goto out;
38503bc06c7SJeff Layton }
38603bc06c7SJeff Layton
387f0702876SJeff Layton dout("%s: pos=%llu orig_len=%zu len=%llu\n", __func__, subreq->start, subreq->len, len);
388f0fe1e54SJeff Layton
389de4eda9dSAl Viro iov_iter_xarray(&iter, ITER_DEST, &rreq->mapping->i_pages, subreq->start, len);
390f0fe1e54SJeff Layton
391f0fe1e54SJeff Layton /*
392f0fe1e54SJeff Layton * FIXME: For now, use CEPH_OSD_DATA_TYPE_PAGES instead of _ITER for
393f0fe1e54SJeff Layton * encrypted inodes. We'd need infrastructure that handles an iov_iter
394f0fe1e54SJeff Layton * instead of page arrays, and we don't have that as of yet. Once the
395f0fe1e54SJeff Layton * dust settles on the write helpers and encrypt/decrypt routines for
396f0fe1e54SJeff Layton * netfs, we should be able to rework this.
397f0fe1e54SJeff Layton */
398f0fe1e54SJeff Layton if (IS_ENCRYPTED(inode)) {
399f0fe1e54SJeff Layton struct page **pages;
400f0fe1e54SJeff Layton size_t page_off;
401f0fe1e54SJeff Layton
402f0fe1e54SJeff Layton err = iov_iter_get_pages_alloc2(&iter, &pages, len, &page_off);
403f0fe1e54SJeff Layton if (err < 0) {
404f0fe1e54SJeff Layton dout("%s: iov_ter_get_pages_alloc returned %d\n",
405f0fe1e54SJeff Layton __func__, err);
406f0fe1e54SJeff Layton goto out;
407f0fe1e54SJeff Layton }
408f0fe1e54SJeff Layton
409f0fe1e54SJeff Layton /* should always give us a page-aligned read */
410f0fe1e54SJeff Layton WARN_ON_ONCE(page_off);
411f0fe1e54SJeff Layton len = err;
412f0fe1e54SJeff Layton err = 0;
413f0fe1e54SJeff Layton
414f0fe1e54SJeff Layton osd_req_op_extent_osd_data_pages(req, 0, pages, len, 0, false,
415f0fe1e54SJeff Layton false);
416f0fe1e54SJeff Layton } else {
4174de77f25SJeff Layton osd_req_op_extent_osd_iter(req, 0, &iter);
418f0fe1e54SJeff Layton }
4191464de9fSXiubo Li if (!ceph_inc_osd_stopping_blocker(fsc->mdsc)) {
4201464de9fSXiubo Li err = -EIO;
4211464de9fSXiubo Li goto out;
4221464de9fSXiubo Li }
423f0702876SJeff Layton req->r_callback = finish_netfs_read;
424f0702876SJeff Layton req->r_priv = subreq;
425f0702876SJeff Layton req->r_inode = inode;
426f0702876SJeff Layton ihold(inode);
427f0702876SJeff Layton
428a8af0d68SJeff Layton ceph_osdc_start_request(req->r_osdc, req);
429f0702876SJeff Layton out:
430f0702876SJeff Layton ceph_osdc_put_request(req);
431f0702876SJeff Layton if (err)
432f0702876SJeff Layton netfs_subreq_terminated(subreq, err, false);
433f0702876SJeff Layton dout("%s: result %d\n", __func__, err);
434f0702876SJeff Layton }
435f0702876SJeff Layton
ceph_init_request(struct netfs_io_request * rreq,struct file * file)436a5c9dc44SDavid Howells static int ceph_init_request(struct netfs_io_request *rreq, struct file *file)
437a5c9dc44SDavid Howells {
438a5c9dc44SDavid Howells struct inode *inode = rreq->inode;
439a5c9dc44SDavid Howells int got = 0, want = CEPH_CAP_FILE_CACHE;
44023ee27dcSXiubo Li struct ceph_netfs_request_data *priv;
441a5c9dc44SDavid Howells int ret = 0;
442a5c9dc44SDavid Howells
443a5c9dc44SDavid Howells if (rreq->origin != NETFS_READAHEAD)
444a5c9dc44SDavid Howells return 0;
445a5c9dc44SDavid Howells
44623ee27dcSXiubo Li priv = kzalloc(sizeof(*priv), GFP_NOFS);
44723ee27dcSXiubo Li if (!priv)
44823ee27dcSXiubo Li return -ENOMEM;
44923ee27dcSXiubo Li
450a5c9dc44SDavid Howells if (file) {
451a5c9dc44SDavid Howells struct ceph_rw_context *rw_ctx;
452a5c9dc44SDavid Howells struct ceph_file_info *fi = file->private_data;
453a5c9dc44SDavid Howells
45423ee27dcSXiubo Li priv->file_ra_pages = file->f_ra.ra_pages;
45523ee27dcSXiubo Li priv->file_ra_disabled = file->f_mode & FMODE_RANDOM;
45623ee27dcSXiubo Li
457a5c9dc44SDavid Howells rw_ctx = ceph_find_rw_context(fi);
45823ee27dcSXiubo Li if (rw_ctx) {
45923ee27dcSXiubo Li rreq->netfs_priv = priv;
460a5c9dc44SDavid Howells return 0;
461a5c9dc44SDavid Howells }
46223ee27dcSXiubo Li }
463a5c9dc44SDavid Howells
464a5c9dc44SDavid Howells /*
465a5c9dc44SDavid Howells * readahead callers do not necessarily hold Fcb caps
466a5c9dc44SDavid Howells * (e.g. fadvise, madvise).
467a5c9dc44SDavid Howells */
468a5c9dc44SDavid Howells ret = ceph_try_get_caps(inode, CEPH_CAP_FILE_RD, want, true, &got);
469a5c9dc44SDavid Howells if (ret < 0) {
470a5c9dc44SDavid Howells dout("start_read %p, error getting cap\n", inode);
47123ee27dcSXiubo Li goto out;
472a5c9dc44SDavid Howells }
473a5c9dc44SDavid Howells
474a5c9dc44SDavid Howells if (!(got & want)) {
475a5c9dc44SDavid Howells dout("start_read %p, no cache cap\n", inode);
47623ee27dcSXiubo Li ret = -EACCES;
47723ee27dcSXiubo Li goto out;
478a5c9dc44SDavid Howells }
47923ee27dcSXiubo Li if (ret == 0) {
48023ee27dcSXiubo Li ret = -EACCES;
48123ee27dcSXiubo Li goto out;
48223ee27dcSXiubo Li }
483a5c9dc44SDavid Howells
48423ee27dcSXiubo Li priv->caps = got;
48523ee27dcSXiubo Li rreq->netfs_priv = priv;
48623ee27dcSXiubo Li
48723ee27dcSXiubo Li out:
488b8c118c2SPatrick Donnelly if (ret < 0) {
489b8c118c2SPatrick Donnelly if (got)
490b8c118c2SPatrick Donnelly ceph_put_cap_refs(ceph_inode(inode), got);
49123ee27dcSXiubo Li kfree(priv);
492b8c118c2SPatrick Donnelly }
49323ee27dcSXiubo Li
49423ee27dcSXiubo Li return ret;
495a5c9dc44SDavid Howells }
496a5c9dc44SDavid Howells
ceph_netfs_free_request(struct netfs_io_request * rreq)49740a81101SDavid Howells static void ceph_netfs_free_request(struct netfs_io_request *rreq)
49849870056SJeff Layton {
49923ee27dcSXiubo Li struct ceph_netfs_request_data *priv = rreq->netfs_priv;
50049870056SJeff Layton
50123ee27dcSXiubo Li if (!priv)
50223ee27dcSXiubo Li return;
50323ee27dcSXiubo Li
50423ee27dcSXiubo Li if (priv->caps)
50523ee27dcSXiubo Li ceph_put_cap_refs(ceph_inode(rreq->inode), priv->caps);
50623ee27dcSXiubo Li kfree(priv);
50723ee27dcSXiubo Li rreq->netfs_priv = NULL;
50849870056SJeff Layton }
50949870056SJeff Layton
510bc899ee1SDavid Howells const struct netfs_request_ops ceph_netfs_ops = {
511a5c9dc44SDavid Howells .init_request = ceph_init_request,
51240a81101SDavid Howells .free_request = ceph_netfs_free_request,
513f0702876SJeff Layton .begin_cache_operation = ceph_begin_cache_operation,
514f18a3785SDavid Howells .issue_read = ceph_netfs_issue_read,
515f0702876SJeff Layton .expand_readahead = ceph_netfs_expand_readahead,
516f0702876SJeff Layton .clamp_length = ceph_netfs_clamp_length,
517d801327dSJeff Layton .check_write_begin = ceph_netfs_check_write_begin,
518f0702876SJeff Layton };
519f0702876SJeff Layton
5201702e797SJeff Layton #ifdef CONFIG_CEPH_FSCACHE
ceph_set_page_fscache(struct page * page)5211702e797SJeff Layton static void ceph_set_page_fscache(struct page *page)
5221702e797SJeff Layton {
5231702e797SJeff Layton set_page_fscache(page);
5241702e797SJeff Layton }
5251702e797SJeff Layton
ceph_fscache_write_terminated(void * priv,ssize_t error,bool was_async)5261702e797SJeff Layton static void ceph_fscache_write_terminated(void *priv, ssize_t error, bool was_async)
5271702e797SJeff Layton {
5281702e797SJeff Layton struct inode *inode = priv;
5291702e797SJeff Layton
5301702e797SJeff Layton if (IS_ERR_VALUE(error) && error != -ENOBUFS)
5311702e797SJeff Layton ceph_fscache_invalidate(inode, false);
5321702e797SJeff Layton }
5331702e797SJeff Layton
ceph_fscache_write_to_cache(struct inode * inode,u64 off,u64 len,bool caching)5341702e797SJeff Layton static void ceph_fscache_write_to_cache(struct inode *inode, u64 off, u64 len, bool caching)
5351702e797SJeff Layton {
5361702e797SJeff Layton struct ceph_inode_info *ci = ceph_inode(inode);
5371702e797SJeff Layton struct fscache_cookie *cookie = ceph_fscache_cookie(ci);
5381702e797SJeff Layton
5391702e797SJeff Layton fscache_write_to_cache(cookie, inode->i_mapping, off, len, i_size_read(inode),
5401702e797SJeff Layton ceph_fscache_write_terminated, inode, caching);
5411702e797SJeff Layton }
5421702e797SJeff Layton #else
ceph_set_page_fscache(struct page * page)5431702e797SJeff Layton static inline void ceph_set_page_fscache(struct page *page)
5441702e797SJeff Layton {
5451702e797SJeff Layton }
5461702e797SJeff Layton
ceph_fscache_write_to_cache(struct inode * inode,u64 off,u64 len,bool caching)5471702e797SJeff Layton static inline void ceph_fscache_write_to_cache(struct inode *inode, u64 off, u64 len, bool caching)
5481702e797SJeff Layton {
5491702e797SJeff Layton }
5501702e797SJeff Layton #endif /* CONFIG_CEPH_FSCACHE */
5511702e797SJeff Layton
5521f934b00SYan, Zheng struct ceph_writeback_ctl
5531f934b00SYan, Zheng {
5541f934b00SYan, Zheng loff_t i_size;
5551f934b00SYan, Zheng u64 truncate_size;
5561f934b00SYan, Zheng u32 truncate_seq;
5571f934b00SYan, Zheng bool size_stable;
5582a2d927eSYan, Zheng bool head_snapc;
5591f934b00SYan, Zheng };
5601f934b00SYan, Zheng
5611d3576fdSSage Weil /*
5621d3576fdSSage Weil * Get ref for the oldest snapc for an inode with dirty data... that is, the
5631d3576fdSSage Weil * only snap context we are allowed to write back.
5641d3576fdSSage Weil */
5651f934b00SYan, Zheng static struct ceph_snap_context *
get_oldest_context(struct inode * inode,struct ceph_writeback_ctl * ctl,struct ceph_snap_context * page_snapc)56605455e11SYan, Zheng get_oldest_context(struct inode *inode, struct ceph_writeback_ctl *ctl,
56705455e11SYan, Zheng struct ceph_snap_context *page_snapc)
5681d3576fdSSage Weil {
5691d3576fdSSage Weil struct ceph_inode_info *ci = ceph_inode(inode);
5701d3576fdSSage Weil struct ceph_snap_context *snapc = NULL;
5711d3576fdSSage Weil struct ceph_cap_snap *capsnap = NULL;
5721d3576fdSSage Weil
573be655596SSage Weil spin_lock(&ci->i_ceph_lock);
5741d3576fdSSage Weil list_for_each_entry(capsnap, &ci->i_cap_snaps, ci_item) {
5751d3576fdSSage Weil dout(" cap_snap %p snapc %p has %d dirty pages\n", capsnap,
5761d3576fdSSage Weil capsnap->context, capsnap->dirty_pages);
57705455e11SYan, Zheng if (!capsnap->dirty_pages)
57805455e11SYan, Zheng continue;
57905455e11SYan, Zheng
58005455e11SYan, Zheng /* get i_size, truncate_{seq,size} for page_snapc? */
58105455e11SYan, Zheng if (snapc && capsnap->context != page_snapc)
58205455e11SYan, Zheng continue;
58305455e11SYan, Zheng
5841f934b00SYan, Zheng if (ctl) {
5851f934b00SYan, Zheng if (capsnap->writing) {
5861f934b00SYan, Zheng ctl->i_size = i_size_read(inode);
5871f934b00SYan, Zheng ctl->size_stable = false;
5881f934b00SYan, Zheng } else {
5891f934b00SYan, Zheng ctl->i_size = capsnap->size;
5901f934b00SYan, Zheng ctl->size_stable = true;
5911f934b00SYan, Zheng }
5921f934b00SYan, Zheng ctl->truncate_size = capsnap->truncate_size;
5931f934b00SYan, Zheng ctl->truncate_seq = capsnap->truncate_seq;
5942a2d927eSYan, Zheng ctl->head_snapc = false;
5951f934b00SYan, Zheng }
59605455e11SYan, Zheng
59705455e11SYan, Zheng if (snapc)
5981d3576fdSSage Weil break;
59905455e11SYan, Zheng
60005455e11SYan, Zheng snapc = ceph_get_snap_context(capsnap->context);
60105455e11SYan, Zheng if (!page_snapc ||
60205455e11SYan, Zheng page_snapc == snapc ||
60305455e11SYan, Zheng page_snapc->seq > snapc->seq)
60405455e11SYan, Zheng break;
6051d3576fdSSage Weil }
6067d8cb26dSSage Weil if (!snapc && ci->i_wrbuffer_ref_head) {
60780e755feSSage Weil snapc = ceph_get_snap_context(ci->i_head_snapc);
6081d3576fdSSage Weil dout(" head snapc %p has %d dirty pages\n",
6091d3576fdSSage Weil snapc, ci->i_wrbuffer_ref_head);
6101f934b00SYan, Zheng if (ctl) {
6111f934b00SYan, Zheng ctl->i_size = i_size_read(inode);
6121f934b00SYan, Zheng ctl->truncate_size = ci->i_truncate_size;
6131f934b00SYan, Zheng ctl->truncate_seq = ci->i_truncate_seq;
6141f934b00SYan, Zheng ctl->size_stable = false;
6152a2d927eSYan, Zheng ctl->head_snapc = true;
6161f934b00SYan, Zheng }
6171d3576fdSSage Weil }
618be655596SSage Weil spin_unlock(&ci->i_ceph_lock);
6191d3576fdSSage Weil return snapc;
6201d3576fdSSage Weil }
6211d3576fdSSage Weil
get_writepages_data_length(struct inode * inode,struct page * page,u64 start)6221f934b00SYan, Zheng static u64 get_writepages_data_length(struct inode *inode,
6231f934b00SYan, Zheng struct page *page, u64 start)
6241f934b00SYan, Zheng {
6251f934b00SYan, Zheng struct ceph_inode_info *ci = ceph_inode(inode);
626d5520771SJeff Layton struct ceph_snap_context *snapc;
6271f934b00SYan, Zheng struct ceph_cap_snap *capsnap = NULL;
6281f934b00SYan, Zheng u64 end = i_size_read(inode);
629d5520771SJeff Layton u64 ret;
6301f934b00SYan, Zheng
631d5520771SJeff Layton snapc = page_snap_context(ceph_fscrypt_pagecache_page(page));
6321f934b00SYan, Zheng if (snapc != ci->i_head_snapc) {
6331f934b00SYan, Zheng bool found = false;
6341f934b00SYan, Zheng spin_lock(&ci->i_ceph_lock);
6351f934b00SYan, Zheng list_for_each_entry(capsnap, &ci->i_cap_snaps, ci_item) {
6361f934b00SYan, Zheng if (capsnap->context == snapc) {
6371f934b00SYan, Zheng if (!capsnap->writing)
6381f934b00SYan, Zheng end = capsnap->size;
6391f934b00SYan, Zheng found = true;
6401f934b00SYan, Zheng break;
6411f934b00SYan, Zheng }
6421f934b00SYan, Zheng }
6431f934b00SYan, Zheng spin_unlock(&ci->i_ceph_lock);
6441f934b00SYan, Zheng WARN_ON(!found);
6451f934b00SYan, Zheng }
646d5520771SJeff Layton if (end > ceph_fscrypt_page_offset(page) + thp_size(page))
647d5520771SJeff Layton end = ceph_fscrypt_page_offset(page) + thp_size(page);
648d5520771SJeff Layton ret = end > start ? end - start : 0;
649d5520771SJeff Layton if (ret && fscrypt_is_bounce_page(page))
650d5520771SJeff Layton ret = round_up(ret, CEPH_FSCRYPT_BLOCK_SIZE);
651d5520771SJeff Layton return ret;
6521f934b00SYan, Zheng }
6531f934b00SYan, Zheng
6541d3576fdSSage Weil /*
6551d3576fdSSage Weil * Write a single page, but leave the page locked.
6561d3576fdSSage Weil *
657b72b13ebSJeff Layton * If we get a write error, mark the mapping for error, but still adjust the
6581d3576fdSSage Weil * dirty page accounting (i.e., page is no longer dirty).
6591d3576fdSSage Weil */
writepage_nounlock(struct page * page,struct writeback_control * wbc)6601d3576fdSSage Weil static int writepage_nounlock(struct page *page, struct writeback_control *wbc)
6611d3576fdSSage Weil {
662a628304eSMatthew Wilcox (Oracle) struct folio *folio = page_folio(page);
6636390987fSJeff Layton struct inode *inode = page->mapping->host;
6646390987fSJeff Layton struct ceph_inode_info *ci = ceph_inode(inode);
665985b9ee8SXiubo Li struct ceph_fs_client *fsc = ceph_inode_to_fs_client(inode);
6666298a337SSage Weil struct ceph_snap_context *snapc, *oldest;
667fc2744aaSYan, Zheng loff_t page_off = page_offset(page);
6686390987fSJeff Layton int err;
6698ff2d290SJeff Layton loff_t len = thp_size(page);
670d5520771SJeff Layton loff_t wlen;
6711f934b00SYan, Zheng struct ceph_writeback_ctl ceph_wbc;
6726390987fSJeff Layton struct ceph_osd_client *osdc = &fsc->client->osdc;
6736390987fSJeff Layton struct ceph_osd_request *req;
6741702e797SJeff Layton bool caching = ceph_is_cache_enabled(inode);
675d5520771SJeff Layton struct page *bounce_page = NULL;
6761d3576fdSSage Weil
6771d3576fdSSage Weil dout("writepage %p idx %lu\n", page, page->index);
6781d3576fdSSage Weil
679a68e564aSXiubo Li if (ceph_inode_is_shutdown(inode))
680a68e564aSXiubo Li return -EIO;
681a68e564aSXiubo Li
6821d3576fdSSage Weil /* verify this is a writeable snap context */
68361600ef8SYan, Zheng snapc = page_snap_context(page);
684d37b1d99SMarkus Elfring if (!snapc) {
6851d3576fdSSage Weil dout("writepage %p page %p not dirty?\n", inode, page);
68643986881SYan, Zheng return 0;
6871d3576fdSSage Weil }
68805455e11SYan, Zheng oldest = get_oldest_context(inode, &ceph_wbc, snapc);
6896298a337SSage Weil if (snapc->seq > oldest->seq) {
6901d3576fdSSage Weil dout("writepage %p page %p snapc %p not writeable - noop\n",
69161600ef8SYan, Zheng inode, page, snapc);
6921d3576fdSSage Weil /* we should only noop if called by kswapd */
693fa71fefbSYan, Zheng WARN_ON(!(current->flags & PF_MEMALLOC));
6946298a337SSage Weil ceph_put_snap_context(oldest);
695fa71fefbSYan, Zheng redirty_page_for_writepage(wbc, page);
69643986881SYan, Zheng return 0;
6971d3576fdSSage Weil }
6986298a337SSage Weil ceph_put_snap_context(oldest);
6991d3576fdSSage Weil
7001d3576fdSSage Weil /* is this a partial page at end of file? */
7011f934b00SYan, Zheng if (page_off >= ceph_wbc.i_size) {
702a628304eSMatthew Wilcox (Oracle) dout("folio at %lu beyond eof %llu\n", folio->index,
703a628304eSMatthew Wilcox (Oracle) ceph_wbc.i_size);
704a628304eSMatthew Wilcox (Oracle) folio_invalidate(folio, 0, folio_size(folio));
70543986881SYan, Zheng return 0;
706fc2744aaSYan, Zheng }
70743986881SYan, Zheng
7081f934b00SYan, Zheng if (ceph_wbc.i_size < page_off + len)
7091f934b00SYan, Zheng len = ceph_wbc.i_size - page_off;
7101d3576fdSSage Weil
711d5520771SJeff Layton wlen = IS_ENCRYPTED(inode) ? round_up(len, CEPH_FSCRYPT_BLOCK_SIZE) : len;
7126390987fSJeff Layton dout("writepage %p page %p index %lu on %llu~%llu snapc %p seq %lld\n",
713d5520771SJeff Layton inode, page, page->index, page_off, wlen, snapc, snapc->seq);
7141d3576fdSSage Weil
715314c4737SYan, Zheng if (atomic_long_inc_return(&fsc->writeback_count) >
7163d14c5d2SYehuda Sadeh CONGESTION_ON_THRESH(fsc->mount_options->congestion_kb))
717503d4fa6SNeilBrown fsc->write_congested = true;
7182baba250SYehuda Sadeh
719d5520771SJeff Layton req = ceph_osdc_new_request(osdc, &ci->i_layout, ceph_vino(inode),
720d5520771SJeff Layton page_off, &wlen, 0, 1, CEPH_OSD_OP_WRITE,
721d5520771SJeff Layton CEPH_OSD_FLAG_WRITE, snapc,
722d5520771SJeff Layton ceph_wbc.truncate_seq,
723d5520771SJeff Layton ceph_wbc.truncate_size, true);
7243459bd0cSXiubo Li if (IS_ERR(req)) {
7253459bd0cSXiubo Li redirty_page_for_writepage(wbc, page);
7266390987fSJeff Layton return PTR_ERR(req);
7273459bd0cSXiubo Li }
7281702e797SJeff Layton
729d5520771SJeff Layton if (wlen < len)
730d5520771SJeff Layton len = wlen;
731d5520771SJeff Layton
7321702e797SJeff Layton set_page_writeback(page);
7331702e797SJeff Layton if (caching)
7341702e797SJeff Layton ceph_set_page_fscache(page);
7351702e797SJeff Layton ceph_fscache_write_to_cache(inode, page_off, len, caching);
7366390987fSJeff Layton
737d5520771SJeff Layton if (IS_ENCRYPTED(inode)) {
738d5520771SJeff Layton bounce_page = fscrypt_encrypt_pagecache_blocks(page,
739d5520771SJeff Layton CEPH_FSCRYPT_BLOCK_SIZE, 0,
740d5520771SJeff Layton GFP_NOFS);
741d5520771SJeff Layton if (IS_ERR(bounce_page)) {
742d5520771SJeff Layton redirty_page_for_writepage(wbc, page);
743d5520771SJeff Layton end_page_writeback(page);
744d5520771SJeff Layton ceph_osdc_put_request(req);
745d5520771SJeff Layton return PTR_ERR(bounce_page);
746d5520771SJeff Layton }
747d5520771SJeff Layton }
748d5520771SJeff Layton
7496390987fSJeff Layton /* it may be a short write due to an object boundary */
7508ff2d290SJeff Layton WARN_ON_ONCE(len > thp_size(page));
751d5520771SJeff Layton osd_req_op_extent_osd_data_pages(req, 0,
752d5520771SJeff Layton bounce_page ? &bounce_page : &page, wlen, 0,
753d5520771SJeff Layton false, false);
754d5520771SJeff Layton dout("writepage %llu~%llu (%llu bytes, %sencrypted)\n",
755d5520771SJeff Layton page_off, len, wlen, IS_ENCRYPTED(inode) ? "" : "not ");
7566390987fSJeff Layton
7576390987fSJeff Layton req->r_mtime = inode->i_mtime;
758a8af0d68SJeff Layton ceph_osdc_start_request(osdc, req);
7596390987fSJeff Layton err = ceph_osdc_wait_request(osdc, req);
7606390987fSJeff Layton
7618ae99ae2SXiubo Li ceph_update_write_metrics(&fsc->mdsc->metric, req->r_start_latency,
762903f4fecSXiubo Li req->r_end_latency, len, err);
763d5520771SJeff Layton fscrypt_free_bounce_page(bounce_page);
7646390987fSJeff Layton ceph_osdc_put_request(req);
7656390987fSJeff Layton if (err == 0)
7666390987fSJeff Layton err = len;
7676390987fSJeff Layton
7681d3576fdSSage Weil if (err < 0) {
769ad15ec06SYan, Zheng struct writeback_control tmp_wbc;
770ad15ec06SYan, Zheng if (!wbc)
771ad15ec06SYan, Zheng wbc = &tmp_wbc;
772ad15ec06SYan, Zheng if (err == -ERESTARTSYS) {
773ad15ec06SYan, Zheng /* killed by SIGKILL */
774ad15ec06SYan, Zheng dout("writepage interrupted page %p\n", page);
775ad15ec06SYan, Zheng redirty_page_for_writepage(wbc, page);
776ad15ec06SYan, Zheng end_page_writeback(page);
77743986881SYan, Zheng return err;
778ad15ec06SYan, Zheng }
7790b98acd6SIlya Dryomov if (err == -EBLOCKLISTED)
7800b98acd6SIlya Dryomov fsc->blocklisted = true;
781ad15ec06SYan, Zheng dout("writepage setting page/mapping error %d %p\n",
782ad15ec06SYan, Zheng err, page);
7831d3576fdSSage Weil mapping_set_error(&inode->i_data, err);
7841d3576fdSSage Weil wbc->pages_skipped++;
7851d3576fdSSage Weil } else {
7861d3576fdSSage Weil dout("writepage cleaned page %p\n", page);
7871d3576fdSSage Weil err = 0; /* vfs expects us to return 0 */
7881d3576fdSSage Weil }
789379fc7faSJeff Layton oldest = detach_page_private(page);
790379fc7faSJeff Layton WARN_ON_ONCE(oldest != snapc);
7911d3576fdSSage Weil end_page_writeback(page);
7921d3576fdSSage Weil ceph_put_wrbuffer_cap_refs(ci, 1, snapc);
7936298a337SSage Weil ceph_put_snap_context(snapc); /* page's reference */
794314c4737SYan, Zheng
795314c4737SYan, Zheng if (atomic_long_dec_return(&fsc->writeback_count) <
796314c4737SYan, Zheng CONGESTION_OFF_THRESH(fsc->mount_options->congestion_kb))
797503d4fa6SNeilBrown fsc->write_congested = false;
798314c4737SYan, Zheng
7991d3576fdSSage Weil return err;
8001d3576fdSSage Weil }
8011d3576fdSSage Weil
ceph_writepage(struct page * page,struct writeback_control * wbc)8021d3576fdSSage Weil static int ceph_writepage(struct page *page, struct writeback_control *wbc)
8031d3576fdSSage Weil {
804dbd646a8SYehuda Sadeh int err;
805dbd646a8SYehuda Sadeh struct inode *inode = page->mapping->host;
806dbd646a8SYehuda Sadeh BUG_ON(!inode);
80770b666c3SSage Weil ihold(inode);
8081702e797SJeff Layton
809503d4fa6SNeilBrown if (wbc->sync_mode == WB_SYNC_NONE &&
810dbfb5232SNeilBrown ceph_inode_to_fs_client(inode)->write_congested) {
811dbfb5232SNeilBrown redirty_page_for_writepage(wbc, page);
812503d4fa6SNeilBrown return AOP_WRITEPAGE_ACTIVATE;
813dbfb5232SNeilBrown }
814503d4fa6SNeilBrown
8151702e797SJeff Layton wait_on_page_fscache(page);
8161702e797SJeff Layton
817dbd646a8SYehuda Sadeh err = writepage_nounlock(page, wbc);
818ad15ec06SYan, Zheng if (err == -ERESTARTSYS) {
819ad15ec06SYan, Zheng /* direct memory reclaimer was killed by SIGKILL. return 0
820ad15ec06SYan, Zheng * to prevent caller from setting mapping/page error */
821ad15ec06SYan, Zheng err = 0;
822ad15ec06SYan, Zheng }
8231d3576fdSSage Weil unlock_page(page);
824dbd646a8SYehuda Sadeh iput(inode);
8251d3576fdSSage Weil return err;
8261d3576fdSSage Weil }
8271d3576fdSSage Weil
8281d3576fdSSage Weil /*
8291d3576fdSSage Weil * async writeback completion handler.
8301d3576fdSSage Weil *
8311d3576fdSSage Weil * If we get an error, set the mapping error bit, but not the individual
8321d3576fdSSage Weil * page error bits.
8331d3576fdSSage Weil */
writepages_finish(struct ceph_osd_request * req)83485e084feSIlya Dryomov static void writepages_finish(struct ceph_osd_request *req)
8351d3576fdSSage Weil {
8361d3576fdSSage Weil struct inode *inode = req->r_inode;
8371d3576fdSSage Weil struct ceph_inode_info *ci = ceph_inode(inode);
83887060c10SAlex Elder struct ceph_osd_data *osd_data;
8391d3576fdSSage Weil struct page *page;
8405b64640cSYan, Zheng int num_pages, total_pages = 0;
8415b64640cSYan, Zheng int i, j;
8425b64640cSYan, Zheng int rc = req->r_result;
8431d3576fdSSage Weil struct ceph_snap_context *snapc = req->r_snapc;
8441d3576fdSSage Weil struct address_space *mapping = inode->i_mapping;
845985b9ee8SXiubo Li struct ceph_fs_client *fsc = ceph_inode_to_fs_client(inode);
846903f4fecSXiubo Li unsigned int len = 0;
8475b64640cSYan, Zheng bool remove_page;
8481d3576fdSSage Weil
8495b64640cSYan, Zheng dout("writepages_finish %p rc %d\n", inode, rc);
85026544c62SJeff Layton if (rc < 0) {
8511d3576fdSSage Weil mapping_set_error(mapping, rc);
85226544c62SJeff Layton ceph_set_error_write(ci);
8530b98acd6SIlya Dryomov if (rc == -EBLOCKLISTED)
8540b98acd6SIlya Dryomov fsc->blocklisted = true;
85526544c62SJeff Layton } else {
85626544c62SJeff Layton ceph_clear_error_write(ci);
85726544c62SJeff Layton }
858e63dc5c7SYehuda Sadeh
859e63dc5c7SYehuda Sadeh /*
860e63dc5c7SYehuda Sadeh * We lost the cache cap, need to truncate the page before
861e63dc5c7SYehuda Sadeh * it is unlocked, otherwise we'd truncate it later in the
862e63dc5c7SYehuda Sadeh * page truncation thread, possibly losing some data that
863e63dc5c7SYehuda Sadeh * raced its way in
864e63dc5c7SYehuda Sadeh */
8655b64640cSYan, Zheng remove_page = !(ceph_caps_issued(ci) &
8665b64640cSYan, Zheng (CEPH_CAP_FILE_CACHE|CEPH_CAP_FILE_LAZYIO));
8675b64640cSYan, Zheng
8685b64640cSYan, Zheng /* clean all pages */
8695b64640cSYan, Zheng for (i = 0; i < req->r_num_ops; i++) {
870642d51fbSXiubo Li if (req->r_ops[i].op != CEPH_OSD_OP_WRITE) {
871642d51fbSXiubo Li pr_warn("%s incorrect op %d req %p index %d tid %llu\n",
872642d51fbSXiubo Li __func__, req->r_ops[i].op, req, i, req->r_tid);
8735b64640cSYan, Zheng break;
874642d51fbSXiubo Li }
8755b64640cSYan, Zheng
8765b64640cSYan, Zheng osd_data = osd_req_op_extent_osd_data(req, i);
8775b64640cSYan, Zheng BUG_ON(osd_data->type != CEPH_OSD_DATA_TYPE_PAGES);
878903f4fecSXiubo Li len += osd_data->length;
8795b64640cSYan, Zheng num_pages = calc_pages_for((u64)osd_data->alignment,
8805b64640cSYan, Zheng (u64)osd_data->length);
8815b64640cSYan, Zheng total_pages += num_pages;
8825b64640cSYan, Zheng for (j = 0; j < num_pages; j++) {
8835b64640cSYan, Zheng page = osd_data->pages[j];
884d5520771SJeff Layton if (fscrypt_is_bounce_page(page)) {
885d5520771SJeff Layton page = fscrypt_pagecache_page(page);
886d5520771SJeff Layton fscrypt_free_bounce_page(osd_data->pages[j]);
887d5520771SJeff Layton osd_data->pages[j] = page;
888d5520771SJeff Layton }
8895b64640cSYan, Zheng BUG_ON(!page);
8905b64640cSYan, Zheng WARN_ON(!PageUptodate(page));
8915b64640cSYan, Zheng
8925b64640cSYan, Zheng if (atomic_long_dec_return(&fsc->writeback_count) <
8935b64640cSYan, Zheng CONGESTION_OFF_THRESH(
8945b64640cSYan, Zheng fsc->mount_options->congestion_kb))
895503d4fa6SNeilBrown fsc->write_congested = false;
8965b64640cSYan, Zheng
897379fc7faSJeff Layton ceph_put_snap_context(detach_page_private(page));
8985b64640cSYan, Zheng end_page_writeback(page);
899379fc7faSJeff Layton dout("unlocking %p\n", page);
9005b64640cSYan, Zheng
9015b64640cSYan, Zheng if (remove_page)
9025b64640cSYan, Zheng generic_error_remove_page(inode->i_mapping,
9035b64640cSYan, Zheng page);
904e63dc5c7SYehuda Sadeh
9051d3576fdSSage Weil unlock_page(page);
9061d3576fdSSage Weil }
9075b64640cSYan, Zheng dout("writepages_finish %p wrote %llu bytes cleaned %d pages\n",
9085b64640cSYan, Zheng inode, osd_data->length, rc >= 0 ? num_pages : 0);
9091d3576fdSSage Weil
91096ac9158SJohn Hubbard release_pages(osd_data->pages, num_pages);
9115b64640cSYan, Zheng }
9125b64640cSYan, Zheng
913903f4fecSXiubo Li ceph_update_write_metrics(&fsc->mdsc->metric, req->r_start_latency,
914903f4fecSXiubo Li req->r_end_latency, len, rc);
915903f4fecSXiubo Li
9165b64640cSYan, Zheng ceph_put_wrbuffer_cap_refs(ci, total_pages, snapc);
9175b64640cSYan, Zheng
9185b64640cSYan, Zheng osd_data = osd_req_op_extent_osd_data(req, 0);
91987060c10SAlex Elder if (osd_data->pages_from_pool)
920a0102bdaSJeff Layton mempool_free(osd_data->pages, ceph_wb_pagevec_pool);
9211d3576fdSSage Weil else
92287060c10SAlex Elder kfree(osd_data->pages);
9231d3576fdSSage Weil ceph_osdc_put_request(req);
9241464de9fSXiubo Li ceph_dec_osd_stopping_blocker(fsc->mdsc);
9251d3576fdSSage Weil }
9261d3576fdSSage Weil
9271d3576fdSSage Weil /*
9281d3576fdSSage Weil * initiate async writeback
9291d3576fdSSage Weil */
ceph_writepages_start(struct address_space * mapping,struct writeback_control * wbc)9301d3576fdSSage Weil static int ceph_writepages_start(struct address_space *mapping,
9311d3576fdSSage Weil struct writeback_control *wbc)
9321d3576fdSSage Weil {
9331d3576fdSSage Weil struct inode *inode = mapping->host;
9341d3576fdSSage Weil struct ceph_inode_info *ci = ceph_inode(inode);
935985b9ee8SXiubo Li struct ceph_fs_client *fsc = ceph_inode_to_fs_client(inode);
936fc2744aaSYan, Zheng struct ceph_vino vino = ceph_vino(inode);
9372a2d927eSYan, Zheng pgoff_t index, start_index, end = -1;
93880e755feSSage Weil struct ceph_snap_context *snapc = NULL, *last_snapc = NULL, *pgsnapc;
939590a2b5fSVishal Moola (Oracle) struct folio_batch fbatch;
9401d3576fdSSage Weil int rc = 0;
94193407472SFabian Frederick unsigned int wsize = i_blocksize(inode);
9421d3576fdSSage Weil struct ceph_osd_request *req = NULL;
9431f934b00SYan, Zheng struct ceph_writeback_ctl ceph_wbc;
944590e9d98SYan, Zheng bool should_loop, range_whole = false;
945af9cc401SYan, Zheng bool done = false;
9461702e797SJeff Layton bool caching = ceph_is_cache_enabled(inode);
9477d41870dSXiubo Li xa_mark_t tag;
9481d3576fdSSage Weil
949503d4fa6SNeilBrown if (wbc->sync_mode == WB_SYNC_NONE &&
950503d4fa6SNeilBrown fsc->write_congested)
951503d4fa6SNeilBrown return 0;
952503d4fa6SNeilBrown
9533fb99d48SYanhu Cao dout("writepages_start %p (mode=%s)\n", inode,
9541d3576fdSSage Weil wbc->sync_mode == WB_SYNC_NONE ? "NONE" :
9551d3576fdSSage Weil (wbc->sync_mode == WB_SYNC_ALL ? "ALL" : "HOLD"));
9561d3576fdSSage Weil
9575d6451b1SJeff Layton if (ceph_inode_is_shutdown(inode)) {
9586c93df5dSYan, Zheng if (ci->i_wrbuffer_ref > 0) {
9596c93df5dSYan, Zheng pr_warn_ratelimited(
9606c93df5dSYan, Zheng "writepage_start %p %lld forced umount\n",
9616c93df5dSYan, Zheng inode, ceph_ino(inode));
9626c93df5dSYan, Zheng }
963a341d4dfSYan, Zheng mapping_set_error(mapping, -EIO);
9641d3576fdSSage Weil return -EIO; /* we're in a forced umount, don't write! */
9651d3576fdSSage Weil }
96695cca2b4SYan, Zheng if (fsc->mount_options->wsize < wsize)
9673d14c5d2SYehuda Sadeh wsize = fsc->mount_options->wsize;
9681d3576fdSSage Weil
969590a2b5fSVishal Moola (Oracle) folio_batch_init(&fbatch);
9701d3576fdSSage Weil
971590e9d98SYan, Zheng start_index = wbc->range_cyclic ? mapping->writeback_index : 0;
972590e9d98SYan, Zheng index = start_index;
9731d3576fdSSage Weil
9747d41870dSXiubo Li if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages) {
9757d41870dSXiubo Li tag = PAGECACHE_TAG_TOWRITE;
9767d41870dSXiubo Li } else {
9777d41870dSXiubo Li tag = PAGECACHE_TAG_DIRTY;
9787d41870dSXiubo Li }
9791d3576fdSSage Weil retry:
9801d3576fdSSage Weil /* find oldest snap context with dirty data */
98105455e11SYan, Zheng snapc = get_oldest_context(inode, &ceph_wbc, NULL);
9821d3576fdSSage Weil if (!snapc) {
9831d3576fdSSage Weil /* hmm, why does writepages get called when there
9841d3576fdSSage Weil is no dirty data? */
9851d3576fdSSage Weil dout(" no snap context with dirty data?\n");
9861d3576fdSSage Weil goto out;
9871d3576fdSSage Weil }
9881d3576fdSSage Weil dout(" oldest snapc is %p seq %lld (%d snaps)\n",
9891d3576fdSSage Weil snapc, snapc->seq, snapc->num_snaps);
990fc2744aaSYan, Zheng
9912a2d927eSYan, Zheng should_loop = false;
9922a2d927eSYan, Zheng if (ceph_wbc.head_snapc && snapc != last_snapc) {
9932a2d927eSYan, Zheng /* where to start/end? */
9942a2d927eSYan, Zheng if (wbc->range_cyclic) {
9952a2d927eSYan, Zheng index = start_index;
9962a2d927eSYan, Zheng end = -1;
9972a2d927eSYan, Zheng if (index > 0)
9982a2d927eSYan, Zheng should_loop = true;
9992a2d927eSYan, Zheng dout(" cyclic, start at %lu\n", index);
10002a2d927eSYan, Zheng } else {
10012a2d927eSYan, Zheng index = wbc->range_start >> PAGE_SHIFT;
10022a2d927eSYan, Zheng end = wbc->range_end >> PAGE_SHIFT;
10032a2d927eSYan, Zheng if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX)
10042a2d927eSYan, Zheng range_whole = true;
10052a2d927eSYan, Zheng dout(" not cyclic, %lu to %lu\n", index, end);
10061d3576fdSSage Weil }
10072a2d927eSYan, Zheng } else if (!ceph_wbc.head_snapc) {
10082a2d927eSYan, Zheng /* Do not respect wbc->range_{start,end}. Dirty pages
10092a2d927eSYan, Zheng * in that range can be associated with newer snapc.
10102a2d927eSYan, Zheng * They are not writeable until we write all dirty pages
10112a2d927eSYan, Zheng * associated with 'snapc' get written */
10121582af2eSYan, Zheng if (index > 0)
10132a2d927eSYan, Zheng should_loop = true;
10142a2d927eSYan, Zheng dout(" non-head snapc, range whole\n");
10152a2d927eSYan, Zheng }
10162a2d927eSYan, Zheng
10177d41870dSXiubo Li if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages)
10187d41870dSXiubo Li tag_pages_for_writeback(mapping, index, end);
10197d41870dSXiubo Li
10202a2d927eSYan, Zheng ceph_put_snap_context(last_snapc);
10211d3576fdSSage Weil last_snapc = snapc;
10221d3576fdSSage Weil
1023af9cc401SYan, Zheng while (!done && index <= end) {
10245b64640cSYan, Zheng int num_ops = 0, op_idx;
1025590a2b5fSVishal Moola (Oracle) unsigned i, nr_folios, max_pages, locked_pages = 0;
10265b64640cSYan, Zheng struct page **pages = NULL, **data_pages;
10271d3576fdSSage Weil struct page *page;
10280e5ecac7SYan, Zheng pgoff_t strip_unit_end = 0;
10295b64640cSYan, Zheng u64 offset = 0, len = 0;
1030a0102bdaSJeff Layton bool from_pool = false;
10311d3576fdSSage Weil
10320e5ecac7SYan, Zheng max_pages = wsize >> PAGE_SHIFT;
10331d3576fdSSage Weil
10341d3576fdSSage Weil get_more_pages:
1035590a2b5fSVishal Moola (Oracle) nr_folios = filemap_get_folios_tag(mapping, &index,
10367d41870dSXiubo Li end, tag, &fbatch);
1037590a2b5fSVishal Moola (Oracle) dout("pagevec_lookup_range_tag got %d\n", nr_folios);
1038590a2b5fSVishal Moola (Oracle) if (!nr_folios && !locked_pages)
10391d3576fdSSage Weil break;
1040590a2b5fSVishal Moola (Oracle) for (i = 0; i < nr_folios && locked_pages < max_pages; i++) {
1041590a2b5fSVishal Moola (Oracle) page = &fbatch.folios[i]->page;
10421d3576fdSSage Weil dout("? %p idx %lu\n", page, page->index);
10431d3576fdSSage Weil if (locked_pages == 0)
10441d3576fdSSage Weil lock_page(page); /* first page */
10451d3576fdSSage Weil else if (!trylock_page(page))
10461d3576fdSSage Weil break;
10471d3576fdSSage Weil
10481d3576fdSSage Weil /* only dirty pages, or our accounting breaks */
10491d3576fdSSage Weil if (unlikely(!PageDirty(page)) ||
10501d3576fdSSage Weil unlikely(page->mapping != mapping)) {
10511d3576fdSSage Weil dout("!dirty or !mapping %p\n", page);
10521d3576fdSSage Weil unlock_page(page);
10530713e5f2SYan, Zheng continue;
10541d3576fdSSage Weil }
1055af9cc401SYan, Zheng /* only if matching snap context */
1056af9cc401SYan, Zheng pgsnapc = page_snap_context(page);
1057af9cc401SYan, Zheng if (pgsnapc != snapc) {
1058af9cc401SYan, Zheng dout("page snapc %p %lld != oldest %p %lld\n",
1059af9cc401SYan, Zheng pgsnapc, pgsnapc->seq, snapc, snapc->seq);
10601582af2eSYan, Zheng if (!should_loop &&
10611582af2eSYan, Zheng !ceph_wbc.head_snapc &&
10621582af2eSYan, Zheng wbc->sync_mode != WB_SYNC_NONE)
10631582af2eSYan, Zheng should_loop = true;
10641d3576fdSSage Weil unlock_page(page);
1065af9cc401SYan, Zheng continue;
10661d3576fdSSage Weil }
10671f934b00SYan, Zheng if (page_offset(page) >= ceph_wbc.i_size) {
1068a628304eSMatthew Wilcox (Oracle) struct folio *folio = page_folio(page);
1069a628304eSMatthew Wilcox (Oracle)
1070a628304eSMatthew Wilcox (Oracle) dout("folio at %lu beyond eof %llu\n",
1071a628304eSMatthew Wilcox (Oracle) folio->index, ceph_wbc.i_size);
1072c95f1c5fSErqi Chen if ((ceph_wbc.size_stable ||
1073a628304eSMatthew Wilcox (Oracle) folio_pos(folio) >= i_size_read(inode)) &&
1074a628304eSMatthew Wilcox (Oracle) folio_clear_dirty_for_io(folio))
1075a628304eSMatthew Wilcox (Oracle) folio_invalidate(folio, 0,
1076a628304eSMatthew Wilcox (Oracle) folio_size(folio));
1077a628304eSMatthew Wilcox (Oracle) folio_unlock(folio);
1078af9cc401SYan, Zheng continue;
1079af9cc401SYan, Zheng }
1080af9cc401SYan, Zheng if (strip_unit_end && (page->index > strip_unit_end)) {
1081af9cc401SYan, Zheng dout("end of strip unit %p\n", page);
10821d3576fdSSage Weil unlock_page(page);
10831d3576fdSSage Weil break;
10841d3576fdSSage Weil }
10851702e797SJeff Layton if (PageWriteback(page) || PageFsCache(page)) {
10860713e5f2SYan, Zheng if (wbc->sync_mode == WB_SYNC_NONE) {
10871d3576fdSSage Weil dout("%p under writeback\n", page);
10881d3576fdSSage Weil unlock_page(page);
10890713e5f2SYan, Zheng continue;
10900713e5f2SYan, Zheng }
10910713e5f2SYan, Zheng dout("waiting on writeback %p\n", page);
10920713e5f2SYan, Zheng wait_on_page_writeback(page);
10931702e797SJeff Layton wait_on_page_fscache(page);
10941d3576fdSSage Weil }
10951d3576fdSSage Weil
10961d3576fdSSage Weil if (!clear_page_dirty_for_io(page)) {
10971d3576fdSSage Weil dout("%p !clear_page_dirty_for_io\n", page);
10981d3576fdSSage Weil unlock_page(page);
10990713e5f2SYan, Zheng continue;
11001d3576fdSSage Weil }
11011d3576fdSSage Weil
1102e5975c7cSAlex Elder /*
1103e5975c7cSAlex Elder * We have something to write. If this is
1104e5975c7cSAlex Elder * the first locked page this time through,
11055b64640cSYan, Zheng * calculate max possinle write size and
11065b64640cSYan, Zheng * allocate a page array
1107e5975c7cSAlex Elder */
11081d3576fdSSage Weil if (locked_pages == 0) {
11095b64640cSYan, Zheng u64 objnum;
11105b64640cSYan, Zheng u64 objoff;
1111dccbf080SIlya Dryomov u32 xlen;
11125b64640cSYan, Zheng
11131d3576fdSSage Weil /* prepare async write request */
11146285bc23SAlex Elder offset = (u64)page_offset(page);
1115dccbf080SIlya Dryomov ceph_calc_file_object_mapping(&ci->i_layout,
1116dccbf080SIlya Dryomov offset, wsize,
11175b64640cSYan, Zheng &objnum, &objoff,
1118dccbf080SIlya Dryomov &xlen);
1119dccbf080SIlya Dryomov len = xlen;
11208c71897bSHenry C Chang
11213fb99d48SYanhu Cao num_ops = 1;
11225b64640cSYan, Zheng strip_unit_end = page->index +
112309cbfeafSKirill A. Shutemov ((len - 1) >> PAGE_SHIFT);
1124715e4cd4SYan, Zheng
11255b64640cSYan, Zheng BUG_ON(pages);
112688486957SAlex Elder max_pages = calc_pages_for(0, (u64)len);
11276da2ec56SKees Cook pages = kmalloc_array(max_pages,
11286da2ec56SKees Cook sizeof(*pages),
1129fc2744aaSYan, Zheng GFP_NOFS);
113088486957SAlex Elder if (!pages) {
1131a0102bdaSJeff Layton from_pool = true;
1132a0102bdaSJeff Layton pages = mempool_alloc(ceph_wb_pagevec_pool, GFP_NOFS);
1133e5975c7cSAlex Elder BUG_ON(!pages);
113488486957SAlex Elder }
11355b64640cSYan, Zheng
11365b64640cSYan, Zheng len = 0;
11375b64640cSYan, Zheng } else if (page->index !=
113809cbfeafSKirill A. Shutemov (offset + len) >> PAGE_SHIFT) {
1139a0102bdaSJeff Layton if (num_ops >= (from_pool ? CEPH_OSD_SLAB_OPS :
11405b64640cSYan, Zheng CEPH_OSD_MAX_OPS)) {
11415b64640cSYan, Zheng redirty_page_for_writepage(wbc, page);
11425b64640cSYan, Zheng unlock_page(page);
11435b64640cSYan, Zheng break;
11445b64640cSYan, Zheng }
11455b64640cSYan, Zheng
11465b64640cSYan, Zheng num_ops++;
11475b64640cSYan, Zheng offset = (u64)page_offset(page);
11485b64640cSYan, Zheng len = 0;
11491d3576fdSSage Weil }
11501d3576fdSSage Weil
1151590a2b5fSVishal Moola (Oracle) /* note position of first page in fbatch */
11521d3576fdSSage Weil dout("%p will write page %p idx %lu\n",
11531d3576fdSSage Weil inode, page, page->index);
11542baba250SYehuda Sadeh
11555b64640cSYan, Zheng if (atomic_long_inc_return(&fsc->writeback_count) >
11565b64640cSYan, Zheng CONGESTION_ON_THRESH(
1157503d4fa6SNeilBrown fsc->mount_options->congestion_kb))
1158503d4fa6SNeilBrown fsc->write_congested = true;
11590713e5f2SYan, Zheng
1160d5520771SJeff Layton if (IS_ENCRYPTED(inode)) {
1161d5520771SJeff Layton pages[locked_pages] =
1162d5520771SJeff Layton fscrypt_encrypt_pagecache_blocks(page,
1163d5520771SJeff Layton PAGE_SIZE, 0,
1164d5520771SJeff Layton locked_pages ? GFP_NOWAIT : GFP_NOFS);
1165d5520771SJeff Layton if (IS_ERR(pages[locked_pages])) {
1166d5520771SJeff Layton if (PTR_ERR(pages[locked_pages]) == -EINVAL)
1167d5520771SJeff Layton pr_err("%s: inode->i_blkbits=%hhu\n",
1168d5520771SJeff Layton __func__, inode->i_blkbits);
1169d5520771SJeff Layton /* better not fail on first page! */
1170d5520771SJeff Layton BUG_ON(locked_pages == 0);
1171d5520771SJeff Layton pages[locked_pages] = NULL;
1172d5520771SJeff Layton redirty_page_for_writepage(wbc, page);
1173d5520771SJeff Layton unlock_page(page);
1174d5520771SJeff Layton break;
1175d5520771SJeff Layton }
1176d5520771SJeff Layton ++locked_pages;
1177d5520771SJeff Layton } else {
11780713e5f2SYan, Zheng pages[locked_pages++] = page;
1179d5520771SJeff Layton }
11800713e5f2SYan, Zheng
1181d5520771SJeff Layton fbatch.folios[i] = NULL;
11828ff2d290SJeff Layton len += thp_size(page);
11831d3576fdSSage Weil }
11841d3576fdSSage Weil
11851d3576fdSSage Weil /* did we get anything? */
11861d3576fdSSage Weil if (!locked_pages)
1187590a2b5fSVishal Moola (Oracle) goto release_folios;
11881d3576fdSSage Weil if (i) {
11890713e5f2SYan, Zheng unsigned j, n = 0;
1190590a2b5fSVishal Moola (Oracle) /* shift unused page to beginning of fbatch */
1191590a2b5fSVishal Moola (Oracle) for (j = 0; j < nr_folios; j++) {
1192590a2b5fSVishal Moola (Oracle) if (!fbatch.folios[j])
11930713e5f2SYan, Zheng continue;
11940713e5f2SYan, Zheng if (n < j)
1195590a2b5fSVishal Moola (Oracle) fbatch.folios[n] = fbatch.folios[j];
11960713e5f2SYan, Zheng n++;
11970713e5f2SYan, Zheng }
1198590a2b5fSVishal Moola (Oracle) fbatch.nr = n;
11991d3576fdSSage Weil
1200590a2b5fSVishal Moola (Oracle) if (nr_folios && i == nr_folios &&
12011d3576fdSSage Weil locked_pages < max_pages) {
1202590a2b5fSVishal Moola (Oracle) dout("reached end fbatch, trying for more\n");
1203590a2b5fSVishal Moola (Oracle) folio_batch_release(&fbatch);
12041d3576fdSSage Weil goto get_more_pages;
12051d3576fdSSage Weil }
12061d3576fdSSage Weil }
12071d3576fdSSage Weil
12085b64640cSYan, Zheng new_request:
1209d5520771SJeff Layton offset = ceph_fscrypt_page_offset(pages[0]);
12105b64640cSYan, Zheng len = wsize;
12115b64640cSYan, Zheng
12125b64640cSYan, Zheng req = ceph_osdc_new_request(&fsc->client->osdc,
12135b64640cSYan, Zheng &ci->i_layout, vino,
12145b64640cSYan, Zheng offset, &len, 0, num_ops,
12151f934b00SYan, Zheng CEPH_OSD_OP_WRITE, CEPH_OSD_FLAG_WRITE,
12161f934b00SYan, Zheng snapc, ceph_wbc.truncate_seq,
12171f934b00SYan, Zheng ceph_wbc.truncate_size, false);
12185b64640cSYan, Zheng if (IS_ERR(req)) {
12195b64640cSYan, Zheng req = ceph_osdc_new_request(&fsc->client->osdc,
12205b64640cSYan, Zheng &ci->i_layout, vino,
12215b64640cSYan, Zheng offset, &len, 0,
12225b64640cSYan, Zheng min(num_ops,
12235b64640cSYan, Zheng CEPH_OSD_SLAB_OPS),
12245b64640cSYan, Zheng CEPH_OSD_OP_WRITE,
122554ea0046SIlya Dryomov CEPH_OSD_FLAG_WRITE,
12261f934b00SYan, Zheng snapc, ceph_wbc.truncate_seq,
12271f934b00SYan, Zheng ceph_wbc.truncate_size, true);
12285b64640cSYan, Zheng BUG_ON(IS_ERR(req));
12295b64640cSYan, Zheng }
1230d5520771SJeff Layton BUG_ON(len < ceph_fscrypt_page_offset(pages[locked_pages - 1]) +
1231d5520771SJeff Layton thp_size(pages[locked_pages - 1]) - offset);
12325b64640cSYan, Zheng
12331464de9fSXiubo Li if (!ceph_inc_osd_stopping_blocker(fsc->mdsc)) {
12341464de9fSXiubo Li rc = -EIO;
12351464de9fSXiubo Li goto release_folios;
12361464de9fSXiubo Li }
12375b64640cSYan, Zheng req->r_callback = writepages_finish;
12385b64640cSYan, Zheng req->r_inode = inode;
12395b64640cSYan, Zheng
12405b64640cSYan, Zheng /* Format the osd request message and submit the write */
12415b64640cSYan, Zheng len = 0;
12425b64640cSYan, Zheng data_pages = pages;
12435b64640cSYan, Zheng op_idx = 0;
12445b64640cSYan, Zheng for (i = 0; i < locked_pages; i++) {
1245d5520771SJeff Layton struct page *page = ceph_fscrypt_pagecache_page(pages[i]);
1246d5520771SJeff Layton
1247d5520771SJeff Layton u64 cur_offset = page_offset(page);
12481702e797SJeff Layton /*
12491702e797SJeff Layton * Discontinuity in page range? Ceph can handle that by just passing
12501702e797SJeff Layton * multiple extents in the write op.
12511702e797SJeff Layton */
12525b64640cSYan, Zheng if (offset + len != cur_offset) {
12531702e797SJeff Layton /* If it's full, stop here */
12543fb99d48SYanhu Cao if (op_idx + 1 == req->r_num_ops)
12555b64640cSYan, Zheng break;
12561702e797SJeff Layton
12571702e797SJeff Layton /* Kick off an fscache write with what we have so far. */
12581702e797SJeff Layton ceph_fscache_write_to_cache(inode, offset, len, caching);
12591702e797SJeff Layton
12601702e797SJeff Layton /* Start a new extent */
12615b64640cSYan, Zheng osd_req_op_extent_dup_last(req, op_idx,
12625b64640cSYan, Zheng cur_offset - offset);
12635b64640cSYan, Zheng dout("writepages got pages at %llu~%llu\n",
12645b64640cSYan, Zheng offset, len);
12655b64640cSYan, Zheng osd_req_op_extent_osd_data_pages(req, op_idx,
12665b64640cSYan, Zheng data_pages, len, 0,
1267a0102bdaSJeff Layton from_pool, false);
12685b64640cSYan, Zheng osd_req_op_extent_update(req, op_idx, len);
12695b64640cSYan, Zheng
12705b64640cSYan, Zheng len = 0;
12715b64640cSYan, Zheng offset = cur_offset;
12725b64640cSYan, Zheng data_pages = pages + i;
12735b64640cSYan, Zheng op_idx++;
12745b64640cSYan, Zheng }
12755b64640cSYan, Zheng
1276d5520771SJeff Layton set_page_writeback(page);
12771702e797SJeff Layton if (caching)
1278d5520771SJeff Layton ceph_set_page_fscache(page);
12798ff2d290SJeff Layton len += thp_size(page);
12805b64640cSYan, Zheng }
12811702e797SJeff Layton ceph_fscache_write_to_cache(inode, offset, len, caching);
12825b64640cSYan, Zheng
12831f934b00SYan, Zheng if (ceph_wbc.size_stable) {
12841f934b00SYan, Zheng len = min(len, ceph_wbc.i_size - offset);
12855b64640cSYan, Zheng } else if (i == locked_pages) {
1286e1966b49SYan, Zheng /* writepages_finish() clears writeback pages
1287e1966b49SYan, Zheng * according to the data length, so make sure
1288e1966b49SYan, Zheng * data length covers all locked pages */
12898ff2d290SJeff Layton u64 min_len = len + 1 - thp_size(page);
12901f934b00SYan, Zheng len = get_writepages_data_length(inode, pages[i - 1],
12911f934b00SYan, Zheng offset);
12925b64640cSYan, Zheng len = max(len, min_len);
1293e1966b49SYan, Zheng }
1294d5520771SJeff Layton if (IS_ENCRYPTED(inode))
1295d5520771SJeff Layton len = round_up(len, CEPH_FSCRYPT_BLOCK_SIZE);
1296d5520771SJeff Layton
12975b64640cSYan, Zheng dout("writepages got pages at %llu~%llu\n", offset, len);
12981d3576fdSSage Weil
1299d5520771SJeff Layton if (IS_ENCRYPTED(inode) &&
1300d5520771SJeff Layton ((offset | len) & ~CEPH_FSCRYPT_BLOCK_MASK))
1301d5520771SJeff Layton pr_warn("%s: bad encrypted write offset=%lld len=%llu\n",
1302d5520771SJeff Layton __func__, offset, len);
1303d5520771SJeff Layton
13045b64640cSYan, Zheng osd_req_op_extent_osd_data_pages(req, op_idx, data_pages, len,
1305a0102bdaSJeff Layton 0, from_pool, false);
13065b64640cSYan, Zheng osd_req_op_extent_update(req, op_idx, len);
1307e5975c7cSAlex Elder
13085b64640cSYan, Zheng BUG_ON(op_idx + 1 != req->r_num_ops);
13095b64640cSYan, Zheng
1310a0102bdaSJeff Layton from_pool = false;
13115b64640cSYan, Zheng if (i < locked_pages) {
13125b64640cSYan, Zheng BUG_ON(num_ops <= req->r_num_ops);
13135b64640cSYan, Zheng num_ops -= req->r_num_ops;
13145b64640cSYan, Zheng locked_pages -= i;
1315e5975c7cSAlex Elder
13165b64640cSYan, Zheng /* allocate new pages array for next request */
13175b64640cSYan, Zheng data_pages = pages;
13186da2ec56SKees Cook pages = kmalloc_array(locked_pages, sizeof(*pages),
13195b64640cSYan, Zheng GFP_NOFS);
13205b64640cSYan, Zheng if (!pages) {
1321a0102bdaSJeff Layton from_pool = true;
1322a0102bdaSJeff Layton pages = mempool_alloc(ceph_wb_pagevec_pool, GFP_NOFS);
13235b64640cSYan, Zheng BUG_ON(!pages);
13245b64640cSYan, Zheng }
13255b64640cSYan, Zheng memcpy(pages, data_pages + i,
13265b64640cSYan, Zheng locked_pages * sizeof(*pages));
13275b64640cSYan, Zheng memset(data_pages + i, 0,
13285b64640cSYan, Zheng locked_pages * sizeof(*pages));
13295b64640cSYan, Zheng } else {
13305b64640cSYan, Zheng BUG_ON(num_ops != req->r_num_ops);
13315b64640cSYan, Zheng index = pages[i - 1]->index + 1;
13325b64640cSYan, Zheng /* request message now owns the pages array */
13335b64640cSYan, Zheng pages = NULL;
13345b64640cSYan, Zheng }
1335e5975c7cSAlex Elder
1336fac02ddfSArnd Bergmann req->r_mtime = inode->i_mtime;
1337a8af0d68SJeff Layton ceph_osdc_start_request(&fsc->client->osdc, req);
13381d3576fdSSage Weil req = NULL;
13391d3576fdSSage Weil
13405b64640cSYan, Zheng wbc->nr_to_write -= i;
13415b64640cSYan, Zheng if (pages)
13425b64640cSYan, Zheng goto new_request;
13435b64640cSYan, Zheng
13442a2d927eSYan, Zheng /*
13452a2d927eSYan, Zheng * We stop writing back only if we are not doing
13462a2d927eSYan, Zheng * integrity sync. In case of integrity sync we have to
13472a2d927eSYan, Zheng * keep going until we have written all the pages
13482a2d927eSYan, Zheng * we tagged for writeback prior to entering this loop.
13492a2d927eSYan, Zheng */
13502a2d927eSYan, Zheng if (wbc->nr_to_write <= 0 && wbc->sync_mode == WB_SYNC_NONE)
1351af9cc401SYan, Zheng done = true;
13521d3576fdSSage Weil
1353590a2b5fSVishal Moola (Oracle) release_folios:
1354590a2b5fSVishal Moola (Oracle) dout("folio_batch release on %d folios (%p)\n", (int)fbatch.nr,
1355590a2b5fSVishal Moola (Oracle) fbatch.nr ? fbatch.folios[0] : NULL);
1356590a2b5fSVishal Moola (Oracle) folio_batch_release(&fbatch);
13571d3576fdSSage Weil }
13581d3576fdSSage Weil
13591d3576fdSSage Weil if (should_loop && !done) {
13601d3576fdSSage Weil /* more to do; loop back to beginning of file */
13611d3576fdSSage Weil dout("writepages looping back to beginning of file\n");
13622a2d927eSYan, Zheng end = start_index - 1; /* OK even when start_index == 0 */
1363f275635eSYan, Zheng
1364f275635eSYan, Zheng /* to write dirty pages associated with next snapc,
1365f275635eSYan, Zheng * we need to wait until current writes complete */
1366f275635eSYan, Zheng if (wbc->sync_mode != WB_SYNC_NONE &&
1367f275635eSYan, Zheng start_index == 0 && /* all dirty pages were checked */
1368f275635eSYan, Zheng !ceph_wbc.head_snapc) {
1369f275635eSYan, Zheng struct page *page;
1370f275635eSYan, Zheng unsigned i, nr;
1371f275635eSYan, Zheng index = 0;
1372f275635eSYan, Zheng while ((index <= end) &&
1373590a2b5fSVishal Moola (Oracle) (nr = filemap_get_folios_tag(mapping, &index,
1374590a2b5fSVishal Moola (Oracle) (pgoff_t)-1,
1375590a2b5fSVishal Moola (Oracle) PAGECACHE_TAG_WRITEBACK,
1376590a2b5fSVishal Moola (Oracle) &fbatch))) {
1377f275635eSYan, Zheng for (i = 0; i < nr; i++) {
1378590a2b5fSVishal Moola (Oracle) page = &fbatch.folios[i]->page;
1379f275635eSYan, Zheng if (page_snap_context(page) != snapc)
1380f275635eSYan, Zheng continue;
1381f275635eSYan, Zheng wait_on_page_writeback(page);
1382f275635eSYan, Zheng }
1383590a2b5fSVishal Moola (Oracle) folio_batch_release(&fbatch);
1384f275635eSYan, Zheng cond_resched();
1385f275635eSYan, Zheng }
1386f275635eSYan, Zheng }
1387f275635eSYan, Zheng
13882a2d927eSYan, Zheng start_index = 0;
13891d3576fdSSage Weil index = 0;
13901d3576fdSSage Weil goto retry;
13911d3576fdSSage Weil }
13921d3576fdSSage Weil
13931d3576fdSSage Weil if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0))
13941d3576fdSSage Weil mapping->writeback_index = index;
13951d3576fdSSage Weil
13961d3576fdSSage Weil out:
13971d3576fdSSage Weil ceph_osdc_put_request(req);
13982a2d927eSYan, Zheng ceph_put_snap_context(last_snapc);
13992a2d927eSYan, Zheng dout("writepages dend - startone, rc = %d\n", rc);
14001d3576fdSSage Weil return rc;
14011d3576fdSSage Weil }
14021d3576fdSSage Weil
14031d3576fdSSage Weil
14041d3576fdSSage Weil
14051d3576fdSSage Weil /*
14061d3576fdSSage Weil * See if a given @snapc is either writeable, or already written.
14071d3576fdSSage Weil */
context_is_writeable_or_written(struct inode * inode,struct ceph_snap_context * snapc)14081d3576fdSSage Weil static int context_is_writeable_or_written(struct inode *inode,
14091d3576fdSSage Weil struct ceph_snap_context *snapc)
14101d3576fdSSage Weil {
141105455e11SYan, Zheng struct ceph_snap_context *oldest = get_oldest_context(inode, NULL, NULL);
14126298a337SSage Weil int ret = !oldest || snapc->seq <= oldest->seq;
14136298a337SSage Weil
14146298a337SSage Weil ceph_put_snap_context(oldest);
14156298a337SSage Weil return ret;
14161d3576fdSSage Weil }
14171d3576fdSSage Weil
141818d620f0SJeff Layton /**
141918d620f0SJeff Layton * ceph_find_incompatible - find an incompatible context and return it
142018d620f0SJeff Layton * @page: page being dirtied
142118d620f0SJeff Layton *
142218d620f0SJeff Layton * We are only allowed to write into/dirty a page if the page is
142318d620f0SJeff Layton * clean, or already dirty within the same snap context. Returns a
142418d620f0SJeff Layton * conflicting context if there is one, NULL if there isn't, or a
142518d620f0SJeff Layton * negative error code on other errors.
142618d620f0SJeff Layton *
142718d620f0SJeff Layton * Must be called with page lock held.
142818d620f0SJeff Layton */
142918d620f0SJeff Layton static struct ceph_snap_context *
ceph_find_incompatible(struct page * page)1430d45156bfSJeff Layton ceph_find_incompatible(struct page *page)
143118d620f0SJeff Layton {
1432d45156bfSJeff Layton struct inode *inode = page->mapping->host;
143318d620f0SJeff Layton struct ceph_inode_info *ci = ceph_inode(inode);
143418d620f0SJeff Layton
14355d6451b1SJeff Layton if (ceph_inode_is_shutdown(inode)) {
14365d6451b1SJeff Layton dout(" page %p %llx:%llx is shutdown\n", page,
14375d6451b1SJeff Layton ceph_vinop(inode));
14385d6451b1SJeff Layton return ERR_PTR(-ESTALE);
143918d620f0SJeff Layton }
144018d620f0SJeff Layton
144118d620f0SJeff Layton for (;;) {
144218d620f0SJeff Layton struct ceph_snap_context *snapc, *oldest;
144318d620f0SJeff Layton
144418d620f0SJeff Layton wait_on_page_writeback(page);
144518d620f0SJeff Layton
144618d620f0SJeff Layton snapc = page_snap_context(page);
144718d620f0SJeff Layton if (!snapc || snapc == ci->i_head_snapc)
144818d620f0SJeff Layton break;
144918d620f0SJeff Layton
145018d620f0SJeff Layton /*
145118d620f0SJeff Layton * this page is already dirty in another (older) snap
145218d620f0SJeff Layton * context! is it writeable now?
145318d620f0SJeff Layton */
145418d620f0SJeff Layton oldest = get_oldest_context(inode, NULL, NULL);
145518d620f0SJeff Layton if (snapc->seq > oldest->seq) {
145618d620f0SJeff Layton /* not writeable -- return it for the caller to deal with */
145718d620f0SJeff Layton ceph_put_snap_context(oldest);
145818d620f0SJeff Layton dout(" page %p snapc %p not current or oldest\n", page, snapc);
145918d620f0SJeff Layton return ceph_get_snap_context(snapc);
146018d620f0SJeff Layton }
146118d620f0SJeff Layton ceph_put_snap_context(oldest);
146218d620f0SJeff Layton
146318d620f0SJeff Layton /* yay, writeable, do it now (without dropping page lock) */
146418d620f0SJeff Layton dout(" page %p snapc %p not current, but oldest\n", page, snapc);
146518d620f0SJeff Layton if (clear_page_dirty_for_io(page)) {
146618d620f0SJeff Layton int r = writepage_nounlock(page, NULL);
146718d620f0SJeff Layton if (r < 0)
146818d620f0SJeff Layton return ERR_PTR(r);
146918d620f0SJeff Layton }
147018d620f0SJeff Layton }
147118d620f0SJeff Layton return NULL;
147218d620f0SJeff Layton }
147318d620f0SJeff Layton
ceph_netfs_check_write_begin(struct file * file,loff_t pos,unsigned int len,struct folio ** foliop,void ** _fsdata)1474d801327dSJeff Layton static int ceph_netfs_check_write_begin(struct file *file, loff_t pos, unsigned int len,
1475fac47b43SXiubo Li struct folio **foliop, void **_fsdata)
1476d801327dSJeff Layton {
1477d801327dSJeff Layton struct inode *inode = file_inode(file);
1478d801327dSJeff Layton struct ceph_inode_info *ci = ceph_inode(inode);
1479d801327dSJeff Layton struct ceph_snap_context *snapc;
1480d801327dSJeff Layton
1481fac47b43SXiubo Li snapc = ceph_find_incompatible(folio_page(*foliop, 0));
1482d801327dSJeff Layton if (snapc) {
1483d801327dSJeff Layton int r;
1484d801327dSJeff Layton
1485fac47b43SXiubo Li folio_unlock(*foliop);
1486fac47b43SXiubo Li folio_put(*foliop);
1487fac47b43SXiubo Li *foliop = NULL;
1488d801327dSJeff Layton if (IS_ERR(snapc))
1489d801327dSJeff Layton return PTR_ERR(snapc);
1490d801327dSJeff Layton
1491d801327dSJeff Layton ceph_queue_writeback(inode);
1492d801327dSJeff Layton r = wait_event_killable(ci->i_cap_wq,
1493d801327dSJeff Layton context_is_writeable_or_written(inode, snapc));
1494d801327dSJeff Layton ceph_put_snap_context(snapc);
1495d801327dSJeff Layton return r == 0 ? -EAGAIN : r;
1496d801327dSJeff Layton }
1497d801327dSJeff Layton return 0;
1498d801327dSJeff Layton }
1499d801327dSJeff Layton
15001d3576fdSSage Weil /*
15011d3576fdSSage Weil * We are only allowed to write into/dirty the page if the page is
15021d3576fdSSage Weil * clean, or already dirty within the same snap context.
15034af6b225SYehuda Sadeh */
ceph_write_begin(struct file * file,struct address_space * mapping,loff_t pos,unsigned len,struct page ** pagep,void ** fsdata)15044af6b225SYehuda Sadeh static int ceph_write_begin(struct file *file, struct address_space *mapping,
15059d6b0cd7SMatthew Wilcox (Oracle) loff_t pos, unsigned len,
15064af6b225SYehuda Sadeh struct page **pagep, void **fsdata)
15074af6b225SYehuda Sadeh {
1508496ad9aaSAl Viro struct inode *inode = file_inode(file);
1509e81fb419SLinus Torvalds struct ceph_inode_info *ci = ceph_inode(inode);
151078525c74SDavid Howells struct folio *folio = NULL;
1511d801327dSJeff Layton int r;
15124af6b225SYehuda Sadeh
1513e81fb419SLinus Torvalds r = netfs_write_begin(&ci->netfs, file, inode->i_mapping, pos, len, &folio, NULL);
1514c460f4e4SXiubo Li if (r < 0)
1515c460f4e4SXiubo Li return r;
1516c460f4e4SXiubo Li
151778525c74SDavid Howells folio_wait_fscache(folio);
151878525c74SDavid Howells WARN_ON_ONCE(!folio_test_locked(folio));
151978525c74SDavid Howells *pagep = &folio->page;
1520c460f4e4SXiubo Li return 0;
15214af6b225SYehuda Sadeh }
15224af6b225SYehuda Sadeh
15234af6b225SYehuda Sadeh /*
15241d3576fdSSage Weil * we don't do anything in here that simple_write_end doesn't do
15255dda377cSYan, Zheng * except adjust dirty page accounting
15261d3576fdSSage Weil */
ceph_write_end(struct file * file,struct address_space * mapping,loff_t pos,unsigned len,unsigned copied,struct page * subpage,void * fsdata)15271d3576fdSSage Weil static int ceph_write_end(struct file *file, struct address_space *mapping,
15281d3576fdSSage Weil loff_t pos, unsigned len, unsigned copied,
152978525c74SDavid Howells struct page *subpage, void *fsdata)
15301d3576fdSSage Weil {
153178525c74SDavid Howells struct folio *folio = page_folio(subpage);
1532496ad9aaSAl Viro struct inode *inode = file_inode(file);
1533efb0ca76SYan, Zheng bool check_cap = false;
15341d3576fdSSage Weil
153578525c74SDavid Howells dout("write_end file %p inode %p folio %p %d~%d (%d)\n", file,
153678525c74SDavid Howells inode, folio, (int)pos, (int)copied, (int)len);
15371d3576fdSSage Weil
153878525c74SDavid Howells if (!folio_test_uptodate(folio)) {
1539ce3a8732SJeff Layton /* just return that nothing was copied on a short copy */
1540b9de313cSAl Viro if (copied < len) {
1541b9de313cSAl Viro copied = 0;
1542b9de313cSAl Viro goto out;
1543b9de313cSAl Viro }
154478525c74SDavid Howells folio_mark_uptodate(folio);
1545b9de313cSAl Viro }
15461d3576fdSSage Weil
15471d3576fdSSage Weil /* did file size increase? */
154899c88e69SYan, Zheng if (pos+copied > i_size_read(inode))
15491d3576fdSSage Weil check_cap = ceph_inode_set_size(inode, pos+copied);
15501d3576fdSSage Weil
155178525c74SDavid Howells folio_mark_dirty(folio);
15521d3576fdSSage Weil
1553b9de313cSAl Viro out:
155478525c74SDavid Howells folio_unlock(folio);
155578525c74SDavid Howells folio_put(folio);
15561d3576fdSSage Weil
15571d3576fdSSage Weil if (check_cap)
1558e4b731ccSXiubo Li ceph_check_caps(ceph_inode(inode), CHECK_CAPS_AUTHONLY);
15591d3576fdSSage Weil
15601d3576fdSSage Weil return copied;
15611d3576fdSSage Weil }
15621d3576fdSSage Weil
15631d3576fdSSage Weil const struct address_space_operations ceph_aops = {
15646c62371bSMatthew Wilcox (Oracle) .read_folio = netfs_read_folio,
1565bc899ee1SDavid Howells .readahead = netfs_readahead,
15661d3576fdSSage Weil .writepage = ceph_writepage,
15671d3576fdSSage Weil .writepages = ceph_writepages_start,
15681d3576fdSSage Weil .write_begin = ceph_write_begin,
15691d3576fdSSage Weil .write_end = ceph_write_end,
15708fb72b4aSMatthew Wilcox (Oracle) .dirty_folio = ceph_dirty_folio,
15719872f4deSMatthew Wilcox (Oracle) .invalidate_folio = ceph_invalidate_folio,
15725e414655SMatthew Wilcox (Oracle) .release_folio = ceph_release_folio,
15739c43ff44SJeff Layton .direct_IO = noop_direct_IO,
15741d3576fdSSage Weil };
15751d3576fdSSage Weil
ceph_block_sigs(sigset_t * oldset)15764f7e89f6SYan, Zheng static void ceph_block_sigs(sigset_t *oldset)
15774f7e89f6SYan, Zheng {
15784f7e89f6SYan, Zheng sigset_t mask;
15794f7e89f6SYan, Zheng siginitsetinv(&mask, sigmask(SIGKILL));
15804f7e89f6SYan, Zheng sigprocmask(SIG_BLOCK, &mask, oldset);
15814f7e89f6SYan, Zheng }
15824f7e89f6SYan, Zheng
ceph_restore_sigs(sigset_t * oldset)15834f7e89f6SYan, Zheng static void ceph_restore_sigs(sigset_t *oldset)
15844f7e89f6SYan, Zheng {
15854f7e89f6SYan, Zheng sigprocmask(SIG_SETMASK, oldset, NULL);
15864f7e89f6SYan, Zheng }
15871d3576fdSSage Weil
15881d3576fdSSage Weil /*
15891d3576fdSSage Weil * vm ops
15901d3576fdSSage Weil */
ceph_filemap_fault(struct vm_fault * vmf)159124499847SSouptick Joarder static vm_fault_t ceph_filemap_fault(struct vm_fault *vmf)
159261f68816SYan, Zheng {
159311bac800SDave Jiang struct vm_area_struct *vma = vmf->vma;
159461f68816SYan, Zheng struct inode *inode = file_inode(vma->vm_file);
159561f68816SYan, Zheng struct ceph_inode_info *ci = ceph_inode(inode);
159661f68816SYan, Zheng struct ceph_file_info *fi = vma->vm_file->private_data;
1597c403c3a2SMatthew Wilcox (Oracle) loff_t off = (loff_t)vmf->pgoff << PAGE_SHIFT;
159824499847SSouptick Joarder int want, got, err;
15994f7e89f6SYan, Zheng sigset_t oldset;
160024499847SSouptick Joarder vm_fault_t ret = VM_FAULT_SIGBUS;
16014f7e89f6SYan, Zheng
16025d6451b1SJeff Layton if (ceph_inode_is_shutdown(inode))
16035d6451b1SJeff Layton return ret;
16045d6451b1SJeff Layton
16054f7e89f6SYan, Zheng ceph_block_sigs(&oldset);
160661f68816SYan, Zheng
16078ff2d290SJeff Layton dout("filemap_fault %p %llx.%llx %llu trying to get caps\n",
16088ff2d290SJeff Layton inode, ceph_vinop(inode), off);
160961f68816SYan, Zheng if (fi->fmode & CEPH_FILE_MODE_LAZY)
161061f68816SYan, Zheng want = CEPH_CAP_FILE_CACHE | CEPH_CAP_FILE_LAZYIO;
161161f68816SYan, Zheng else
161261f68816SYan, Zheng want = CEPH_CAP_FILE_CACHE;
16134f7e89f6SYan, Zheng
161461f68816SYan, Zheng got = 0;
1615e72968e1SJeff Layton err = ceph_get_caps(vma->vm_file, CEPH_CAP_FILE_RD, want, -1, &got);
161624499847SSouptick Joarder if (err < 0)
16174f7e89f6SYan, Zheng goto out_restore;
16186ce026e4SYan, Zheng
16198ff2d290SJeff Layton dout("filemap_fault %p %llu got cap refs on %s\n",
16208ff2d290SJeff Layton inode, off, ceph_cap_string(got));
162161f68816SYan, Zheng
162283701246SYan, Zheng if ((got & (CEPH_CAP_FILE_CACHE | CEPH_CAP_FILE_LAZYIO)) ||
162348490776SXiubo Li !ceph_has_inline_data(ci)) {
16245d988308SYan, Zheng CEPH_DEFINE_RW_CONTEXT(rw_ctx, got);
16255d988308SYan, Zheng ceph_add_rw_context(fi, &rw_ctx);
162611bac800SDave Jiang ret = filemap_fault(vmf);
16275d988308SYan, Zheng ceph_del_rw_context(fi, &rw_ctx);
16288ff2d290SJeff Layton dout("filemap_fault %p %llu drop cap refs %s ret %x\n",
16298ff2d290SJeff Layton inode, off, ceph_cap_string(got), ret);
16302b1ac852SYan, Zheng } else
163124499847SSouptick Joarder err = -EAGAIN;
163261f68816SYan, Zheng
163361f68816SYan, Zheng ceph_put_cap_refs(ci, got);
163461f68816SYan, Zheng
163524499847SSouptick Joarder if (err != -EAGAIN)
16364f7e89f6SYan, Zheng goto out_restore;
163783701246SYan, Zheng
163883701246SYan, Zheng /* read inline data */
163909cbfeafSKirill A. Shutemov if (off >= PAGE_SIZE) {
164083701246SYan, Zheng /* does not support inline data > PAGE_SIZE */
164183701246SYan, Zheng ret = VM_FAULT_SIGBUS;
164283701246SYan, Zheng } else {
164383701246SYan, Zheng struct address_space *mapping = inode->i_mapping;
1644057ba5b2SJan Kara struct page *page;
1645057ba5b2SJan Kara
1646057ba5b2SJan Kara filemap_invalidate_lock_shared(mapping);
1647057ba5b2SJan Kara page = find_or_create_page(mapping, 0,
1648057ba5b2SJan Kara mapping_gfp_constraint(mapping, ~__GFP_FS));
164983701246SYan, Zheng if (!page) {
165083701246SYan, Zheng ret = VM_FAULT_OOM;
16514f7e89f6SYan, Zheng goto out_inline;
165283701246SYan, Zheng }
165324499847SSouptick Joarder err = __ceph_do_getattr(inode, page,
165483701246SYan, Zheng CEPH_STAT_CAP_INLINE_DATA, true);
165524499847SSouptick Joarder if (err < 0 || off >= i_size_read(inode)) {
165683701246SYan, Zheng unlock_page(page);
165709cbfeafSKirill A. Shutemov put_page(page);
1658c64a2b05SSouptick Joarder ret = vmf_error(err);
16594f7e89f6SYan, Zheng goto out_inline;
166083701246SYan, Zheng }
166124499847SSouptick Joarder if (err < PAGE_SIZE)
166224499847SSouptick Joarder zero_user_segment(page, err, PAGE_SIZE);
166383701246SYan, Zheng else
166483701246SYan, Zheng flush_dcache_page(page);
166583701246SYan, Zheng SetPageUptodate(page);
166683701246SYan, Zheng vmf->page = page;
166783701246SYan, Zheng ret = VM_FAULT_MAJOR | VM_FAULT_LOCKED;
16684f7e89f6SYan, Zheng out_inline:
1669057ba5b2SJan Kara filemap_invalidate_unlock_shared(mapping);
16708ff2d290SJeff Layton dout("filemap_fault %p %llu read inline data ret %x\n",
16718ff2d290SJeff Layton inode, off, ret);
16724f7e89f6SYan, Zheng }
16734f7e89f6SYan, Zheng out_restore:
16744f7e89f6SYan, Zheng ceph_restore_sigs(&oldset);
167524499847SSouptick Joarder if (err < 0)
167624499847SSouptick Joarder ret = vmf_error(err);
16776ce026e4SYan, Zheng
167861f68816SYan, Zheng return ret;
167961f68816SYan, Zheng }
16801d3576fdSSage Weil
ceph_page_mkwrite(struct vm_fault * vmf)168124499847SSouptick Joarder static vm_fault_t ceph_page_mkwrite(struct vm_fault *vmf)
16821d3576fdSSage Weil {
168311bac800SDave Jiang struct vm_area_struct *vma = vmf->vma;
1684496ad9aaSAl Viro struct inode *inode = file_inode(vma->vm_file);
168561f68816SYan, Zheng struct ceph_inode_info *ci = ceph_inode(inode);
168661f68816SYan, Zheng struct ceph_file_info *fi = vma->vm_file->private_data;
1687f66fd9f0SYan, Zheng struct ceph_cap_flush *prealloc_cf;
168861f68816SYan, Zheng struct page *page = vmf->page;
16896285bc23SAlex Elder loff_t off = page_offset(page);
169061f68816SYan, Zheng loff_t size = i_size_read(inode);
169161f68816SYan, Zheng size_t len;
169224499847SSouptick Joarder int want, got, err;
16934f7e89f6SYan, Zheng sigset_t oldset;
169424499847SSouptick Joarder vm_fault_t ret = VM_FAULT_SIGBUS;
16951d3576fdSSage Weil
16965d6451b1SJeff Layton if (ceph_inode_is_shutdown(inode))
16975d6451b1SJeff Layton return ret;
16985d6451b1SJeff Layton
1699f66fd9f0SYan, Zheng prealloc_cf = ceph_alloc_cap_flush();
1700f66fd9f0SYan, Zheng if (!prealloc_cf)
17016ce026e4SYan, Zheng return VM_FAULT_OOM;
1702f66fd9f0SYan, Zheng
1703249c1df5SJeff Layton sb_start_pagefault(inode->i_sb);
17044f7e89f6SYan, Zheng ceph_block_sigs(&oldset);
17051d3576fdSSage Weil
17068ff2d290SJeff Layton if (off + thp_size(page) <= size)
17078ff2d290SJeff Layton len = thp_size(page);
17081d3576fdSSage Weil else
17098ff2d290SJeff Layton len = offset_in_thp(page, size);
17101d3576fdSSage Weil
171161f68816SYan, Zheng dout("page_mkwrite %p %llx.%llx %llu~%zd getting caps i_size %llu\n",
171261f68816SYan, Zheng inode, ceph_vinop(inode), off, len, size);
171361f68816SYan, Zheng if (fi->fmode & CEPH_FILE_MODE_LAZY)
171461f68816SYan, Zheng want = CEPH_CAP_FILE_BUFFER | CEPH_CAP_FILE_LAZYIO;
171561f68816SYan, Zheng else
171661f68816SYan, Zheng want = CEPH_CAP_FILE_BUFFER;
17174f7e89f6SYan, Zheng
171861f68816SYan, Zheng got = 0;
1719e72968e1SJeff Layton err = ceph_get_caps(vma->vm_file, CEPH_CAP_FILE_WR, want, off + len, &got);
172024499847SSouptick Joarder if (err < 0)
1721f66fd9f0SYan, Zheng goto out_free;
17226ce026e4SYan, Zheng
172361f68816SYan, Zheng dout("page_mkwrite %p %llu~%zd got cap refs on %s\n",
172461f68816SYan, Zheng inode, off, len, ceph_cap_string(got));
172561f68816SYan, Zheng
172661f68816SYan, Zheng /* Update time before taking page lock */
172761f68816SYan, Zheng file_update_time(vma->vm_file);
17285c308356SJeff Layton inode_inc_iversion_raw(inode);
17294af6b225SYehuda Sadeh
1730f0b33df5SYan, Zheng do {
1731d45156bfSJeff Layton struct ceph_snap_context *snapc;
1732d45156bfSJeff Layton
17334af6b225SYehuda Sadeh lock_page(page);
17344af6b225SYehuda Sadeh
1735cb03c143SAndreas Gruenbacher if (page_mkwrite_check_truncate(page, inode) < 0) {
1736f9cac5acSYan, Zheng unlock_page(page);
17376ce026e4SYan, Zheng ret = VM_FAULT_NOPAGE;
1738f0b33df5SYan, Zheng break;
1739f9cac5acSYan, Zheng }
17404af6b225SYehuda Sadeh
1741d45156bfSJeff Layton snapc = ceph_find_incompatible(page);
1742d45156bfSJeff Layton if (!snapc) {
17434af6b225SYehuda Sadeh /* success. we'll keep the page locked. */
17441d3576fdSSage Weil set_page_dirty(page);
17451d3576fdSSage Weil ret = VM_FAULT_LOCKED;
1746d45156bfSJeff Layton break;
17471d3576fdSSage Weil }
1748d45156bfSJeff Layton
1749d45156bfSJeff Layton unlock_page(page);
1750d45156bfSJeff Layton
1751d45156bfSJeff Layton if (IS_ERR(snapc)) {
1752d45156bfSJeff Layton ret = VM_FAULT_SIGBUS;
1753d45156bfSJeff Layton break;
1754d45156bfSJeff Layton }
1755d45156bfSJeff Layton
1756d45156bfSJeff Layton ceph_queue_writeback(inode);
1757d45156bfSJeff Layton err = wait_event_killable(ci->i_cap_wq,
1758d45156bfSJeff Layton context_is_writeable_or_written(inode, snapc));
1759d45156bfSJeff Layton ceph_put_snap_context(snapc);
1760d45156bfSJeff Layton } while (err == 0);
1761f0b33df5SYan, Zheng
1762083db6fdSDavid Howells if (ret == VM_FAULT_LOCKED) {
176361f68816SYan, Zheng int dirty;
176461f68816SYan, Zheng spin_lock(&ci->i_ceph_lock);
1765f66fd9f0SYan, Zheng dirty = __ceph_mark_dirty_caps(ci, CEPH_CAP_FILE_WR,
1766f66fd9f0SYan, Zheng &prealloc_cf);
176761f68816SYan, Zheng spin_unlock(&ci->i_ceph_lock);
176861f68816SYan, Zheng if (dirty)
176961f68816SYan, Zheng __mark_inode_dirty(inode, dirty);
177061f68816SYan, Zheng }
177161f68816SYan, Zheng
177224499847SSouptick Joarder dout("page_mkwrite %p %llu~%zd dropping cap refs on %s ret %x\n",
177361f68816SYan, Zheng inode, off, len, ceph_cap_string(got), ret);
1774a8810cdcSJeff Layton ceph_put_cap_refs_async(ci, got);
1775f66fd9f0SYan, Zheng out_free:
17764f7e89f6SYan, Zheng ceph_restore_sigs(&oldset);
1777249c1df5SJeff Layton sb_end_pagefault(inode->i_sb);
1778f66fd9f0SYan, Zheng ceph_free_cap_flush(prealloc_cf);
177924499847SSouptick Joarder if (err < 0)
178024499847SSouptick Joarder ret = vmf_error(err);
17811d3576fdSSage Weil return ret;
17821d3576fdSSage Weil }
17831d3576fdSSage Weil
ceph_fill_inline_data(struct inode * inode,struct page * locked_page,char * data,size_t len)178431c542a1SYan, Zheng void ceph_fill_inline_data(struct inode *inode, struct page *locked_page,
178531c542a1SYan, Zheng char *data, size_t len)
178631c542a1SYan, Zheng {
178731c542a1SYan, Zheng struct address_space *mapping = inode->i_mapping;
178831c542a1SYan, Zheng struct page *page;
178931c542a1SYan, Zheng
179031c542a1SYan, Zheng if (locked_page) {
179131c542a1SYan, Zheng page = locked_page;
179231c542a1SYan, Zheng } else {
179331c542a1SYan, Zheng if (i_size_read(inode) == 0)
179431c542a1SYan, Zheng return;
179531c542a1SYan, Zheng page = find_or_create_page(mapping, 0,
1796c62d2555SMichal Hocko mapping_gfp_constraint(mapping,
1797c62d2555SMichal Hocko ~__GFP_FS));
179831c542a1SYan, Zheng if (!page)
179931c542a1SYan, Zheng return;
180031c542a1SYan, Zheng if (PageUptodate(page)) {
180131c542a1SYan, Zheng unlock_page(page);
180209cbfeafSKirill A. Shutemov put_page(page);
180331c542a1SYan, Zheng return;
180431c542a1SYan, Zheng }
180531c542a1SYan, Zheng }
180631c542a1SYan, Zheng
18070668ff52SIlya Dryomov dout("fill_inline_data %p %llx.%llx len %zu locked_page %p\n",
180831c542a1SYan, Zheng inode, ceph_vinop(inode), len, locked_page);
180931c542a1SYan, Zheng
181031c542a1SYan, Zheng if (len > 0) {
181131c542a1SYan, Zheng void *kaddr = kmap_atomic(page);
181231c542a1SYan, Zheng memcpy(kaddr, data, len);
181331c542a1SYan, Zheng kunmap_atomic(kaddr);
181431c542a1SYan, Zheng }
181531c542a1SYan, Zheng
181631c542a1SYan, Zheng if (page != locked_page) {
181709cbfeafSKirill A. Shutemov if (len < PAGE_SIZE)
181809cbfeafSKirill A. Shutemov zero_user_segment(page, len, PAGE_SIZE);
181931c542a1SYan, Zheng else
182031c542a1SYan, Zheng flush_dcache_page(page);
182131c542a1SYan, Zheng
182231c542a1SYan, Zheng SetPageUptodate(page);
182331c542a1SYan, Zheng unlock_page(page);
182409cbfeafSKirill A. Shutemov put_page(page);
182531c542a1SYan, Zheng }
182631c542a1SYan, Zheng }
182731c542a1SYan, Zheng
ceph_uninline_data(struct file * file)1828083db6fdSDavid Howells int ceph_uninline_data(struct file *file)
182928127bddSYan, Zheng {
1830083db6fdSDavid Howells struct inode *inode = file_inode(file);
183128127bddSYan, Zheng struct ceph_inode_info *ci = ceph_inode(inode);
1832985b9ee8SXiubo Li struct ceph_fs_client *fsc = ceph_inode_to_fs_client(inode);
1833825978fdSXiubo Li struct ceph_osd_request *req = NULL;
1834a68e564aSXiubo Li struct ceph_cap_flush *prealloc_cf = NULL;
1835083db6fdSDavid Howells struct folio *folio = NULL;
1836c38af982SDan Carpenter u64 inline_version = CEPH_INLINE_NONE;
1837083db6fdSDavid Howells struct page *pages[1];
183828127bddSYan, Zheng int err = 0;
1839c38af982SDan Carpenter u64 len;
1840083db6fdSDavid Howells
184128127bddSYan, Zheng spin_lock(&ci->i_ceph_lock);
184228127bddSYan, Zheng inline_version = ci->i_inline_version;
184328127bddSYan, Zheng spin_unlock(&ci->i_ceph_lock);
184428127bddSYan, Zheng
184528127bddSYan, Zheng dout("uninline_data %p %llx.%llx inline_version %llu\n",
184628127bddSYan, Zheng inode, ceph_vinop(inode), inline_version);
184728127bddSYan, Zheng
1848a68e564aSXiubo Li if (ceph_inode_is_shutdown(inode)) {
1849a68e564aSXiubo Li err = -EIO;
1850a68e564aSXiubo Li goto out;
1851a68e564aSXiubo Li }
1852a68e564aSXiubo Li
1853825978fdSXiubo Li if (inline_version == CEPH_INLINE_NONE)
1854825978fdSXiubo Li return 0;
1855825978fdSXiubo Li
1856825978fdSXiubo Li prealloc_cf = ceph_alloc_cap_flush();
1857825978fdSXiubo Li if (!prealloc_cf)
1858825978fdSXiubo Li return -ENOMEM;
1859825978fdSXiubo Li
1860825978fdSXiubo Li if (inline_version == 1) /* initial version, no data */
1861825978fdSXiubo Li goto out_uninline;
1862825978fdSXiubo Li
1863825978fdSXiubo Li folio = read_mapping_folio(inode->i_mapping, 0, file);
1864825978fdSXiubo Li if (IS_ERR(folio)) {
1865825978fdSXiubo Li err = PTR_ERR(folio);
1866825978fdSXiubo Li goto out;
1867825978fdSXiubo Li }
1868825978fdSXiubo Li
1869825978fdSXiubo Li folio_lock(folio);
187028127bddSYan, Zheng
187128127bddSYan, Zheng len = i_size_read(inode);
1872083db6fdSDavid Howells if (len > folio_size(folio))
1873083db6fdSDavid Howells len = folio_size(folio);
187428127bddSYan, Zheng
187528127bddSYan, Zheng req = ceph_osdc_new_request(&fsc->client->osdc, &ci->i_layout,
187628127bddSYan, Zheng ceph_vino(inode), 0, &len, 0, 1,
187754ea0046SIlya Dryomov CEPH_OSD_OP_CREATE, CEPH_OSD_FLAG_WRITE,
187834b759b4SIlya Dryomov NULL, 0, 0, false);
187928127bddSYan, Zheng if (IS_ERR(req)) {
188028127bddSYan, Zheng err = PTR_ERR(req);
1881083db6fdSDavid Howells goto out_unlock;
188228127bddSYan, Zheng }
188328127bddSYan, Zheng
1884fac02ddfSArnd Bergmann req->r_mtime = inode->i_mtime;
1885a8af0d68SJeff Layton ceph_osdc_start_request(&fsc->client->osdc, req);
188628127bddSYan, Zheng err = ceph_osdc_wait_request(&fsc->client->osdc, req);
188728127bddSYan, Zheng ceph_osdc_put_request(req);
188828127bddSYan, Zheng if (err < 0)
1889083db6fdSDavid Howells goto out_unlock;
189028127bddSYan, Zheng
189128127bddSYan, Zheng req = ceph_osdc_new_request(&fsc->client->osdc, &ci->i_layout,
189228127bddSYan, Zheng ceph_vino(inode), 0, &len, 1, 3,
189354ea0046SIlya Dryomov CEPH_OSD_OP_WRITE, CEPH_OSD_FLAG_WRITE,
189434b759b4SIlya Dryomov NULL, ci->i_truncate_seq,
189534b759b4SIlya Dryomov ci->i_truncate_size, false);
189628127bddSYan, Zheng if (IS_ERR(req)) {
189728127bddSYan, Zheng err = PTR_ERR(req);
1898083db6fdSDavid Howells goto out_unlock;
189928127bddSYan, Zheng }
190028127bddSYan, Zheng
1901083db6fdSDavid Howells pages[0] = folio_page(folio, 0);
1902083db6fdSDavid Howells osd_req_op_extent_osd_data_pages(req, 1, pages, len, 0, false, false);
190328127bddSYan, Zheng
1904ec137c10SYan, Zheng {
1905ec137c10SYan, Zheng __le64 xattr_buf = cpu_to_le64(inline_version);
190628127bddSYan, Zheng err = osd_req_op_xattr_init(req, 0, CEPH_OSD_OP_CMPXATTR,
1907ec137c10SYan, Zheng "inline_version", &xattr_buf,
1908ec137c10SYan, Zheng sizeof(xattr_buf),
190928127bddSYan, Zheng CEPH_OSD_CMPXATTR_OP_GT,
191028127bddSYan, Zheng CEPH_OSD_CMPXATTR_MODE_U64);
191128127bddSYan, Zheng if (err)
1912083db6fdSDavid Howells goto out_put_req;
1913ec137c10SYan, Zheng }
191428127bddSYan, Zheng
1915ec137c10SYan, Zheng {
1916ec137c10SYan, Zheng char xattr_buf[32];
1917ec137c10SYan, Zheng int xattr_len = snprintf(xattr_buf, sizeof(xattr_buf),
1918ec137c10SYan, Zheng "%llu", inline_version);
191928127bddSYan, Zheng err = osd_req_op_xattr_init(req, 2, CEPH_OSD_OP_SETXATTR,
1920ec137c10SYan, Zheng "inline_version",
1921ec137c10SYan, Zheng xattr_buf, xattr_len, 0, 0);
192228127bddSYan, Zheng if (err)
1923083db6fdSDavid Howells goto out_put_req;
1924ec137c10SYan, Zheng }
192528127bddSYan, Zheng
1926fac02ddfSArnd Bergmann req->r_mtime = inode->i_mtime;
1927a8af0d68SJeff Layton ceph_osdc_start_request(&fsc->client->osdc, req);
192828127bddSYan, Zheng err = ceph_osdc_wait_request(&fsc->client->osdc, req);
192997e27aaaSXiubo Li
19308ae99ae2SXiubo Li ceph_update_write_metrics(&fsc->mdsc->metric, req->r_start_latency,
1931903f4fecSXiubo Li req->r_end_latency, len, err);
193297e27aaaSXiubo Li
1933825978fdSXiubo Li out_uninline:
1934083db6fdSDavid Howells if (!err) {
1935083db6fdSDavid Howells int dirty;
1936083db6fdSDavid Howells
1937083db6fdSDavid Howells /* Set to CAP_INLINE_NONE and dirty the caps */
1938083db6fdSDavid Howells down_read(&fsc->mdsc->snap_rwsem);
1939083db6fdSDavid Howells spin_lock(&ci->i_ceph_lock);
1940083db6fdSDavid Howells ci->i_inline_version = CEPH_INLINE_NONE;
1941083db6fdSDavid Howells dirty = __ceph_mark_dirty_caps(ci, CEPH_CAP_FILE_WR, &prealloc_cf);
1942083db6fdSDavid Howells spin_unlock(&ci->i_ceph_lock);
1943083db6fdSDavid Howells up_read(&fsc->mdsc->snap_rwsem);
1944083db6fdSDavid Howells if (dirty)
1945083db6fdSDavid Howells __mark_inode_dirty(inode, dirty);
1946083db6fdSDavid Howells }
1947083db6fdSDavid Howells out_put_req:
194828127bddSYan, Zheng ceph_osdc_put_request(req);
194928127bddSYan, Zheng if (err == -ECANCELED)
195028127bddSYan, Zheng err = 0;
1951083db6fdSDavid Howells out_unlock:
1952825978fdSXiubo Li if (folio) {
1953083db6fdSDavid Howells folio_unlock(folio);
1954083db6fdSDavid Howells folio_put(folio);
1955825978fdSXiubo Li }
195628127bddSYan, Zheng out:
1957083db6fdSDavid Howells ceph_free_cap_flush(prealloc_cf);
195828127bddSYan, Zheng dout("uninline_data %p %llx.%llx inline_version %llu = %d\n",
195928127bddSYan, Zheng inode, ceph_vinop(inode), inline_version, err);
196028127bddSYan, Zheng return err;
196128127bddSYan, Zheng }
196228127bddSYan, Zheng
19637cbea8dcSKirill A. Shutemov static const struct vm_operations_struct ceph_vmops = {
196461f68816SYan, Zheng .fault = ceph_filemap_fault,
19651d3576fdSSage Weil .page_mkwrite = ceph_page_mkwrite,
19661d3576fdSSage Weil };
19671d3576fdSSage Weil
ceph_mmap(struct file * file,struct vm_area_struct * vma)19681d3576fdSSage Weil int ceph_mmap(struct file *file, struct vm_area_struct *vma)
19691d3576fdSSage Weil {
19701d3576fdSSage Weil struct address_space *mapping = file->f_mapping;
19711d3576fdSSage Weil
19727e0a1265SMatthew Wilcox (Oracle) if (!mapping->a_ops->read_folio)
19731d3576fdSSage Weil return -ENOEXEC;
19741d3576fdSSage Weil vma->vm_ops = &ceph_vmops;
19751d3576fdSSage Weil return 0;
19761d3576fdSSage Weil }
197710183a69SYan, Zheng
197810183a69SYan, Zheng enum {
197910183a69SYan, Zheng POOL_READ = 1,
198010183a69SYan, Zheng POOL_WRITE = 2,
198110183a69SYan, Zheng };
198210183a69SYan, Zheng
__ceph_pool_perm_get(struct ceph_inode_info * ci,s64 pool,struct ceph_string * pool_ns)1983779fe0fbSYan, Zheng static int __ceph_pool_perm_get(struct ceph_inode_info *ci,
1984779fe0fbSYan, Zheng s64 pool, struct ceph_string *pool_ns)
198510183a69SYan, Zheng {
1986985b9ee8SXiubo Li struct ceph_fs_client *fsc = ceph_inode_to_fs_client(&ci->netfs.inode);
198710183a69SYan, Zheng struct ceph_mds_client *mdsc = fsc->mdsc;
198810183a69SYan, Zheng struct ceph_osd_request *rd_req = NULL, *wr_req = NULL;
198910183a69SYan, Zheng struct rb_node **p, *parent;
199010183a69SYan, Zheng struct ceph_pool_perm *perm;
199110183a69SYan, Zheng struct page **pages;
1992779fe0fbSYan, Zheng size_t pool_ns_len;
199310183a69SYan, Zheng int err = 0, err2 = 0, have = 0;
199410183a69SYan, Zheng
199510183a69SYan, Zheng down_read(&mdsc->pool_perm_rwsem);
199610183a69SYan, Zheng p = &mdsc->pool_perm_tree.rb_node;
199710183a69SYan, Zheng while (*p) {
199810183a69SYan, Zheng perm = rb_entry(*p, struct ceph_pool_perm, node);
199910183a69SYan, Zheng if (pool < perm->pool)
200010183a69SYan, Zheng p = &(*p)->rb_left;
200110183a69SYan, Zheng else if (pool > perm->pool)
200210183a69SYan, Zheng p = &(*p)->rb_right;
200310183a69SYan, Zheng else {
2004779fe0fbSYan, Zheng int ret = ceph_compare_string(pool_ns,
2005779fe0fbSYan, Zheng perm->pool_ns,
2006779fe0fbSYan, Zheng perm->pool_ns_len);
2007779fe0fbSYan, Zheng if (ret < 0)
2008779fe0fbSYan, Zheng p = &(*p)->rb_left;
2009779fe0fbSYan, Zheng else if (ret > 0)
2010779fe0fbSYan, Zheng p = &(*p)->rb_right;
2011779fe0fbSYan, Zheng else {
201210183a69SYan, Zheng have = perm->perm;
201310183a69SYan, Zheng break;
201410183a69SYan, Zheng }
201510183a69SYan, Zheng }
2016779fe0fbSYan, Zheng }
201710183a69SYan, Zheng up_read(&mdsc->pool_perm_rwsem);
201810183a69SYan, Zheng if (*p)
201910183a69SYan, Zheng goto out;
202010183a69SYan, Zheng
2021779fe0fbSYan, Zheng if (pool_ns)
2022779fe0fbSYan, Zheng dout("__ceph_pool_perm_get pool %lld ns %.*s no perm cached\n",
2023779fe0fbSYan, Zheng pool, (int)pool_ns->len, pool_ns->str);
2024779fe0fbSYan, Zheng else
20257627151eSYan, Zheng dout("__ceph_pool_perm_get pool %lld no perm cached\n", pool);
202610183a69SYan, Zheng
202710183a69SYan, Zheng down_write(&mdsc->pool_perm_rwsem);
2028779fe0fbSYan, Zheng p = &mdsc->pool_perm_tree.rb_node;
202910183a69SYan, Zheng parent = NULL;
203010183a69SYan, Zheng while (*p) {
203110183a69SYan, Zheng parent = *p;
203210183a69SYan, Zheng perm = rb_entry(parent, struct ceph_pool_perm, node);
203310183a69SYan, Zheng if (pool < perm->pool)
203410183a69SYan, Zheng p = &(*p)->rb_left;
203510183a69SYan, Zheng else if (pool > perm->pool)
203610183a69SYan, Zheng p = &(*p)->rb_right;
203710183a69SYan, Zheng else {
2038779fe0fbSYan, Zheng int ret = ceph_compare_string(pool_ns,
2039779fe0fbSYan, Zheng perm->pool_ns,
2040779fe0fbSYan, Zheng perm->pool_ns_len);
2041779fe0fbSYan, Zheng if (ret < 0)
2042779fe0fbSYan, Zheng p = &(*p)->rb_left;
2043779fe0fbSYan, Zheng else if (ret > 0)
2044779fe0fbSYan, Zheng p = &(*p)->rb_right;
2045779fe0fbSYan, Zheng else {
204610183a69SYan, Zheng have = perm->perm;
204710183a69SYan, Zheng break;
204810183a69SYan, Zheng }
204910183a69SYan, Zheng }
2050779fe0fbSYan, Zheng }
205110183a69SYan, Zheng if (*p) {
205210183a69SYan, Zheng up_write(&mdsc->pool_perm_rwsem);
205310183a69SYan, Zheng goto out;
205410183a69SYan, Zheng }
205510183a69SYan, Zheng
205634b759b4SIlya Dryomov rd_req = ceph_osdc_alloc_request(&fsc->client->osdc, NULL,
205710183a69SYan, Zheng 1, false, GFP_NOFS);
205810183a69SYan, Zheng if (!rd_req) {
205910183a69SYan, Zheng err = -ENOMEM;
206010183a69SYan, Zheng goto out_unlock;
206110183a69SYan, Zheng }
206210183a69SYan, Zheng
206310183a69SYan, Zheng rd_req->r_flags = CEPH_OSD_FLAG_READ;
206410183a69SYan, Zheng osd_req_op_init(rd_req, 0, CEPH_OSD_OP_STAT, 0);
206510183a69SYan, Zheng rd_req->r_base_oloc.pool = pool;
2066779fe0fbSYan, Zheng if (pool_ns)
2067779fe0fbSYan, Zheng rd_req->r_base_oloc.pool_ns = ceph_get_string(pool_ns);
2068d30291b9SIlya Dryomov ceph_oid_printf(&rd_req->r_base_oid, "%llx.00000000", ci->i_vino.ino);
206910183a69SYan, Zheng
207013d1ad16SIlya Dryomov err = ceph_osdc_alloc_messages(rd_req, GFP_NOFS);
207113d1ad16SIlya Dryomov if (err)
207213d1ad16SIlya Dryomov goto out_unlock;
207310183a69SYan, Zheng
207434b759b4SIlya Dryomov wr_req = ceph_osdc_alloc_request(&fsc->client->osdc, NULL,
207510183a69SYan, Zheng 1, false, GFP_NOFS);
207610183a69SYan, Zheng if (!wr_req) {
207710183a69SYan, Zheng err = -ENOMEM;
207810183a69SYan, Zheng goto out_unlock;
207910183a69SYan, Zheng }
208010183a69SYan, Zheng
208154ea0046SIlya Dryomov wr_req->r_flags = CEPH_OSD_FLAG_WRITE;
208210183a69SYan, Zheng osd_req_op_init(wr_req, 0, CEPH_OSD_OP_CREATE, CEPH_OSD_OP_FLAG_EXCL);
208363244fa1SIlya Dryomov ceph_oloc_copy(&wr_req->r_base_oloc, &rd_req->r_base_oloc);
2084d30291b9SIlya Dryomov ceph_oid_copy(&wr_req->r_base_oid, &rd_req->r_base_oid);
208510183a69SYan, Zheng
208613d1ad16SIlya Dryomov err = ceph_osdc_alloc_messages(wr_req, GFP_NOFS);
208713d1ad16SIlya Dryomov if (err)
208813d1ad16SIlya Dryomov goto out_unlock;
208910183a69SYan, Zheng
209010183a69SYan, Zheng /* one page should be large enough for STAT data */
209110183a69SYan, Zheng pages = ceph_alloc_page_vector(1, GFP_KERNEL);
209210183a69SYan, Zheng if (IS_ERR(pages)) {
209310183a69SYan, Zheng err = PTR_ERR(pages);
209410183a69SYan, Zheng goto out_unlock;
209510183a69SYan, Zheng }
209610183a69SYan, Zheng
209710183a69SYan, Zheng osd_req_op_raw_data_in_pages(rd_req, 0, pages, PAGE_SIZE,
209810183a69SYan, Zheng 0, false, true);
2099a8af0d68SJeff Layton ceph_osdc_start_request(&fsc->client->osdc, rd_req);
210010183a69SYan, Zheng
2101874c8ca1SDavid Howells wr_req->r_mtime = ci->netfs.inode.i_mtime;
2102a8af0d68SJeff Layton ceph_osdc_start_request(&fsc->client->osdc, wr_req);
210310183a69SYan, Zheng
210410183a69SYan, Zheng err = ceph_osdc_wait_request(&fsc->client->osdc, rd_req);
210510183a69SYan, Zheng err2 = ceph_osdc_wait_request(&fsc->client->osdc, wr_req);
210610183a69SYan, Zheng
210710183a69SYan, Zheng if (err >= 0 || err == -ENOENT)
210810183a69SYan, Zheng have |= POOL_READ;
2109131d7eb4SYan, Zheng else if (err != -EPERM) {
21100b98acd6SIlya Dryomov if (err == -EBLOCKLISTED)
21110b98acd6SIlya Dryomov fsc->blocklisted = true;
211210183a69SYan, Zheng goto out_unlock;
2113131d7eb4SYan, Zheng }
211410183a69SYan, Zheng
211510183a69SYan, Zheng if (err2 == 0 || err2 == -EEXIST)
211610183a69SYan, Zheng have |= POOL_WRITE;
211710183a69SYan, Zheng else if (err2 != -EPERM) {
21180b98acd6SIlya Dryomov if (err2 == -EBLOCKLISTED)
21190b98acd6SIlya Dryomov fsc->blocklisted = true;
212010183a69SYan, Zheng err = err2;
212110183a69SYan, Zheng goto out_unlock;
212210183a69SYan, Zheng }
212310183a69SYan, Zheng
2124779fe0fbSYan, Zheng pool_ns_len = pool_ns ? pool_ns->len : 0;
2125779fe0fbSYan, Zheng perm = kmalloc(sizeof(*perm) + pool_ns_len + 1, GFP_NOFS);
212610183a69SYan, Zheng if (!perm) {
212710183a69SYan, Zheng err = -ENOMEM;
212810183a69SYan, Zheng goto out_unlock;
212910183a69SYan, Zheng }
213010183a69SYan, Zheng
213110183a69SYan, Zheng perm->pool = pool;
213210183a69SYan, Zheng perm->perm = have;
2133779fe0fbSYan, Zheng perm->pool_ns_len = pool_ns_len;
2134779fe0fbSYan, Zheng if (pool_ns_len > 0)
2135779fe0fbSYan, Zheng memcpy(perm->pool_ns, pool_ns->str, pool_ns_len);
2136779fe0fbSYan, Zheng perm->pool_ns[pool_ns_len] = 0;
2137779fe0fbSYan, Zheng
213810183a69SYan, Zheng rb_link_node(&perm->node, parent, p);
213910183a69SYan, Zheng rb_insert_color(&perm->node, &mdsc->pool_perm_tree);
214010183a69SYan, Zheng err = 0;
214110183a69SYan, Zheng out_unlock:
214210183a69SYan, Zheng up_write(&mdsc->pool_perm_rwsem);
214310183a69SYan, Zheng
214410183a69SYan, Zheng ceph_osdc_put_request(rd_req);
214510183a69SYan, Zheng ceph_osdc_put_request(wr_req);
214610183a69SYan, Zheng out:
214710183a69SYan, Zheng if (!err)
214810183a69SYan, Zheng err = have;
2149779fe0fbSYan, Zheng if (pool_ns)
2150779fe0fbSYan, Zheng dout("__ceph_pool_perm_get pool %lld ns %.*s result = %d\n",
2151779fe0fbSYan, Zheng pool, (int)pool_ns->len, pool_ns->str, err);
2152779fe0fbSYan, Zheng else
21537627151eSYan, Zheng dout("__ceph_pool_perm_get pool %lld result = %d\n", pool, err);
215410183a69SYan, Zheng return err;
215510183a69SYan, Zheng }
215610183a69SYan, Zheng
ceph_pool_perm_check(struct inode * inode,int need)21575e3ded1bSYan, Zheng int ceph_pool_perm_check(struct inode *inode, int need)
215810183a69SYan, Zheng {
21595e3ded1bSYan, Zheng struct ceph_inode_info *ci = ceph_inode(inode);
2160779fe0fbSYan, Zheng struct ceph_string *pool_ns;
21615e3ded1bSYan, Zheng s64 pool;
216210183a69SYan, Zheng int ret, flags;
216310183a69SYan, Zheng
2164e9b22501SJeff Layton /* Only need to do this for regular files */
2165e9b22501SJeff Layton if (!S_ISREG(inode->i_mode))
2166e9b22501SJeff Layton return 0;
2167e9b22501SJeff Layton
216880e80fbbSYan, Zheng if (ci->i_vino.snap != CEPH_NOSNAP) {
216980e80fbbSYan, Zheng /*
217080e80fbbSYan, Zheng * Pool permission check needs to write to the first object.
217180e80fbbSYan, Zheng * But for snapshot, head of the first object may have alread
217280e80fbbSYan, Zheng * been deleted. Skip check to avoid creating orphan object.
217380e80fbbSYan, Zheng */
217480e80fbbSYan, Zheng return 0;
217580e80fbbSYan, Zheng }
217680e80fbbSYan, Zheng
2177985b9ee8SXiubo Li if (ceph_test_mount_opt(ceph_inode_to_fs_client(inode),
217810183a69SYan, Zheng NOPOOLPERM))
217910183a69SYan, Zheng return 0;
218010183a69SYan, Zheng
218110183a69SYan, Zheng spin_lock(&ci->i_ceph_lock);
218210183a69SYan, Zheng flags = ci->i_ceph_flags;
21837627151eSYan, Zheng pool = ci->i_layout.pool_id;
218410183a69SYan, Zheng spin_unlock(&ci->i_ceph_lock);
218510183a69SYan, Zheng check:
218610183a69SYan, Zheng if (flags & CEPH_I_POOL_PERM) {
218710183a69SYan, Zheng if ((need & CEPH_CAP_FILE_RD) && !(flags & CEPH_I_POOL_RD)) {
21887627151eSYan, Zheng dout("ceph_pool_perm_check pool %lld no read perm\n",
218910183a69SYan, Zheng pool);
219010183a69SYan, Zheng return -EPERM;
219110183a69SYan, Zheng }
219210183a69SYan, Zheng if ((need & CEPH_CAP_FILE_WR) && !(flags & CEPH_I_POOL_WR)) {
21937627151eSYan, Zheng dout("ceph_pool_perm_check pool %lld no write perm\n",
219410183a69SYan, Zheng pool);
219510183a69SYan, Zheng return -EPERM;
219610183a69SYan, Zheng }
219710183a69SYan, Zheng return 0;
219810183a69SYan, Zheng }
219910183a69SYan, Zheng
2200779fe0fbSYan, Zheng pool_ns = ceph_try_get_string(ci->i_layout.pool_ns);
2201779fe0fbSYan, Zheng ret = __ceph_pool_perm_get(ci, pool, pool_ns);
2202779fe0fbSYan, Zheng ceph_put_string(pool_ns);
220310183a69SYan, Zheng if (ret < 0)
220410183a69SYan, Zheng return ret;
220510183a69SYan, Zheng
220610183a69SYan, Zheng flags = CEPH_I_POOL_PERM;
220710183a69SYan, Zheng if (ret & POOL_READ)
220810183a69SYan, Zheng flags |= CEPH_I_POOL_RD;
220910183a69SYan, Zheng if (ret & POOL_WRITE)
221010183a69SYan, Zheng flags |= CEPH_I_POOL_WR;
221110183a69SYan, Zheng
221210183a69SYan, Zheng spin_lock(&ci->i_ceph_lock);
2213779fe0fbSYan, Zheng if (pool == ci->i_layout.pool_id &&
2214779fe0fbSYan, Zheng pool_ns == rcu_dereference_raw(ci->i_layout.pool_ns)) {
2215779fe0fbSYan, Zheng ci->i_ceph_flags |= flags;
221610183a69SYan, Zheng } else {
22177627151eSYan, Zheng pool = ci->i_layout.pool_id;
221810183a69SYan, Zheng flags = ci->i_ceph_flags;
221910183a69SYan, Zheng }
222010183a69SYan, Zheng spin_unlock(&ci->i_ceph_lock);
222110183a69SYan, Zheng goto check;
222210183a69SYan, Zheng }
222310183a69SYan, Zheng
ceph_pool_perm_destroy(struct ceph_mds_client * mdsc)222410183a69SYan, Zheng void ceph_pool_perm_destroy(struct ceph_mds_client *mdsc)
222510183a69SYan, Zheng {
222610183a69SYan, Zheng struct ceph_pool_perm *perm;
222710183a69SYan, Zheng struct rb_node *n;
222810183a69SYan, Zheng
222910183a69SYan, Zheng while (!RB_EMPTY_ROOT(&mdsc->pool_perm_tree)) {
223010183a69SYan, Zheng n = rb_first(&mdsc->pool_perm_tree);
223110183a69SYan, Zheng perm = rb_entry(n, struct ceph_pool_perm, node);
223210183a69SYan, Zheng rb_erase(n, &mdsc->pool_perm_tree);
223310183a69SYan, Zheng kfree(perm);
223410183a69SYan, Zheng }
223510183a69SYan, Zheng }
2236