Merge tag 'fs_for_v5.19-rc2' of git://git.kernel.org/pub/scm/linux/kernel/git/jack...
[linux-block.git] / fs / ceph / addr.c
CommitLineData
b2441318 1// SPDX-License-Identifier: GPL-2.0
3d14c5d2 2#include <linux/ceph/ceph_debug.h>
1d3576fd
SW
3
4#include <linux/backing-dev.h>
5#include <linux/fs.h>
6#include <linux/mm.h>
d7bdba1c 7#include <linux/swap.h>
1d3576fd 8#include <linux/pagemap.h>
5a0e3ad6 9#include <linux/slab.h>
1d3576fd
SW
10#include <linux/pagevec.h>
11#include <linux/task_io_accounting_ops.h>
f361bf4a 12#include <linux/signal.h>
5c308356 13#include <linux/iversion.h>
97e27aaa 14#include <linux/ktime.h>
f0702876 15#include <linux/netfs.h>
1d3576fd
SW
16
17#include "super.h"
3d14c5d2 18#include "mds_client.h"
99ccbd22 19#include "cache.h"
97e27aaa 20#include "metric.h"
3d14c5d2 21#include <linux/ceph/osd_client.h>
08c1ac50 22#include <linux/ceph/striper.h>
1d3576fd
SW
23
24/*
25 * Ceph address space ops.
26 *
27 * There are a few funny things going on here.
28 *
29 * The page->private field is used to reference a struct
30 * ceph_snap_context for _every_ dirty page. This indicates which
31 * snapshot the page was logically dirtied in, and thus which snap
32 * context needs to be associated with the osd write during writeback.
33 *
34 * Similarly, struct ceph_inode_info maintains a set of counters to
25985edc 35 * count dirty pages on the inode. In the absence of snapshots,
1d3576fd
SW
36 * i_wrbuffer_ref == i_wrbuffer_ref_head == the dirty page count.
37 *
38 * When a snapshot is taken (that is, when the client receives
39 * notification that a snapshot was taken), each inode with caps and
40 * with dirty pages (dirty pages implies there is a cap) gets a new
41 * ceph_cap_snap in the i_cap_snaps list (which is sorted in ascending
42 * order, new snaps go to the tail). The i_wrbuffer_ref_head count is
43 * moved to capsnap->dirty. (Unless a sync write is currently in
44 * progress. In that case, the capsnap is said to be "pending", new
45 * writes cannot start, and the capsnap isn't "finalized" until the
46 * write completes (or fails) and a final size/mtime for the inode for
47 * that snap can be settled upon.) i_wrbuffer_ref_head is reset to 0.
48 *
49 * On writeback, we must submit writes to the osd IN SNAP ORDER. So,
50 * we look for the first capsnap in i_cap_snaps and write out pages in
51 * that snap context _only_. Then we move on to the next capsnap,
52 * eventually reaching the "live" or "head" context (i.e., pages that
53 * are not yet snapped) and are writing the most recently dirtied
54 * pages.
55 *
56 * Invalidate and so forth must take care to ensure the dirty page
57 * accounting is preserved.
58 */
59
2baba250
YS
60#define CONGESTION_ON_THRESH(congestion_kb) (congestion_kb >> (PAGE_SHIFT-10))
61#define CONGESTION_OFF_THRESH(congestion_kb) \
62 (CONGESTION_ON_THRESH(congestion_kb) - \
63 (CONGESTION_ON_THRESH(congestion_kb) >> 2))
64
d801327d 65static int ceph_netfs_check_write_begin(struct file *file, loff_t pos, unsigned int len,
78525c74 66 struct folio *folio, void **_fsdata);
d801327d 67
61600ef8
YZ
68static inline struct ceph_snap_context *page_snap_context(struct page *page)
69{
70 if (PagePrivate(page))
71 return (void *)page->private;
72 return NULL;
73}
1d3576fd
SW
74
75/*
76 * Dirty a page. Optimistically adjust accounting, on the assumption
77 * that we won't race with invalidate. If we do, readjust.
78 */
8fb72b4a 79static bool ceph_dirty_folio(struct address_space *mapping, struct folio *folio)
1d3576fd 80{
1d3576fd
SW
81 struct inode *inode;
82 struct ceph_inode_info *ci;
1d3576fd 83 struct ceph_snap_context *snapc;
1d3576fd 84
8fb72b4a
MWO
85 if (folio_test_dirty(folio)) {
86 dout("%p dirty_folio %p idx %lu -- already dirty\n",
87 mapping->host, folio, folio->index);
642d51fb 88 VM_BUG_ON_FOLIO(!folio_test_private(folio), folio);
8fb72b4a 89 return false;
1d3576fd
SW
90 }
91
92 inode = mapping->host;
93 ci = ceph_inode(inode);
94
1d3576fd 95 /* dirty the head */
be655596 96 spin_lock(&ci->i_ceph_lock);
5dda377c
YZ
97 BUG_ON(ci->i_wr_ref == 0); // caller should hold Fw reference
98 if (__ceph_have_pending_cap_snap(ci)) {
99 struct ceph_cap_snap *capsnap =
100 list_last_entry(&ci->i_cap_snaps,
101 struct ceph_cap_snap,
102 ci_item);
103 snapc = ceph_get_snap_context(capsnap->context);
104 capsnap->dirty_pages++;
105 } else {
106 BUG_ON(!ci->i_head_snapc);
107 snapc = ceph_get_snap_context(ci->i_head_snapc);
108 ++ci->i_wrbuffer_ref_head;
109 }
1d3576fd 110 if (ci->i_wrbuffer_ref == 0)
0444d76a 111 ihold(inode);
1d3576fd 112 ++ci->i_wrbuffer_ref;
8fb72b4a 113 dout("%p dirty_folio %p idx %lu head %d/%d -> %d/%d "
1d3576fd 114 "snapc %p seq %lld (%d snaps)\n",
8fb72b4a 115 mapping->host, folio, folio->index,
1d3576fd
SW
116 ci->i_wrbuffer_ref-1, ci->i_wrbuffer_ref_head-1,
117 ci->i_wrbuffer_ref, ci->i_wrbuffer_ref_head,
118 snapc, snapc->seq, snapc->num_snaps);
be655596 119 spin_unlock(&ci->i_ceph_lock);
1d3576fd 120
7d6e1f54 121 /*
8fb72b4a 122 * Reference snap context in folio->private. Also set
9872f4de 123 * PagePrivate so that we get invalidate_folio callback.
7d6e1f54 124 */
642d51fb 125 VM_BUG_ON_FOLIO(folio_test_private(folio), folio);
8fb72b4a 126 folio_attach_private(folio, snapc);
1d3576fd 127
8fb72b4a 128 return ceph_fscache_dirty_folio(mapping, folio);
1d3576fd
SW
129}
130
131/*
9872f4de
MWO
132 * If we are truncating the full folio (i.e. offset == 0), adjust the
133 * dirty folio counters appropriately. Only called if there is private
134 * data on the folio.
1d3576fd 135 */
9872f4de
MWO
136static void ceph_invalidate_folio(struct folio *folio, size_t offset,
137 size_t length)
1d3576fd 138{
4ce1e9ad 139 struct inode *inode;
1d3576fd 140 struct ceph_inode_info *ci;
379fc7fa 141 struct ceph_snap_context *snapc;
1d3576fd 142
9872f4de 143 inode = folio->mapping->host;
b150f5c1
MT
144 ci = ceph_inode(inode);
145
9872f4de
MWO
146 if (offset != 0 || length != folio_size(folio)) {
147 dout("%p invalidate_folio idx %lu partial dirty page %zu~%zu\n",
148 inode, folio->index, offset, length);
b150f5c1
MT
149 return;
150 }
4ce1e9ad 151
9872f4de 152 WARN_ON(!folio_test_locked(folio));
642d51fb 153 if (folio_test_private(folio)) {
9872f4de
MWO
154 dout("%p invalidate_folio idx %lu full dirty page\n",
155 inode, folio->index);
99ccbd22 156
9872f4de 157 snapc = folio_detach_private(folio);
400e1286
JL
158 ceph_put_wrbuffer_cap_refs(ci, 1, snapc);
159 ceph_put_snap_context(snapc);
160 }
b150f5c1 161
9872f4de 162 folio_wait_fscache(folio);
1d3576fd
SW
163}
164
5e414655 165static bool ceph_release_folio(struct folio *folio, gfp_t gfp)
1d3576fd 166{
5e414655 167 struct inode *inode = folio->mapping->host;
400e1286 168
5e414655
MWO
169 dout("%llx:%llx release_folio idx %lu (%sdirty)\n",
170 ceph_vinop(inode),
171 folio->index, folio_test_dirty(folio) ? "" : "not ");
400e1286 172
5e414655
MWO
173 if (folio_test_private(folio))
174 return false;
99ccbd22 175
5e414655 176 if (folio_test_fscache(folio)) {
d7bdba1c 177 if (current_is_kswapd() || !(gfp & __GFP_FS))
5e414655
MWO
178 return false;
179 folio_wait_fscache(folio);
7c46b318 180 }
400e1286 181 ceph_fscache_note_page_release(inode);
5e414655 182 return true;
1d3576fd
SW
183}
184
6a19114b 185static void ceph_netfs_expand_readahead(struct netfs_io_request *rreq)
f0702876 186{
a25cedb4 187 struct inode *inode = rreq->inode;
f0702876
JL
188 struct ceph_inode_info *ci = ceph_inode(inode);
189 struct ceph_file_layout *lo = &ci->i_layout;
190 u32 blockoff;
191 u64 blockno;
192
193 /* Expand the start downward */
194 blockno = div_u64_rem(rreq->start, lo->stripe_unit, &blockoff);
195 rreq->start = blockno * lo->stripe_unit;
196 rreq->len += blockoff;
197
198 /* Now, round up the length to the next block */
199 rreq->len = roundup(rreq->len, lo->stripe_unit);
200}
201
6a19114b 202static bool ceph_netfs_clamp_length(struct netfs_io_subrequest *subreq)
f0702876 203{
a25cedb4 204 struct inode *inode = subreq->rreq->inode;
f0702876
JL
205 struct ceph_fs_client *fsc = ceph_inode_to_client(inode);
206 struct ceph_inode_info *ci = ceph_inode(inode);
207 u64 objno, objoff;
208 u32 xlen;
209
210 /* Truncate the extent at the end of the current block */
211 ceph_calc_file_object_mapping(&ci->i_layout, subreq->start, subreq->len,
212 &objno, &objoff, &xlen);
213 subreq->len = min(xlen, fsc->mount_options->rsize);
214 return true;
215}
216
217static void finish_netfs_read(struct ceph_osd_request *req)
218{
219 struct ceph_fs_client *fsc = ceph_inode_to_client(req->r_inode);
220 struct ceph_osd_data *osd_data = osd_req_op_extent_osd_data(req, 0);
6a19114b 221 struct netfs_io_subrequest *subreq = req->r_priv;
f0702876
JL
222 int num_pages;
223 int err = req->r_result;
224
8ae99ae2 225 ceph_update_read_metrics(&fsc->mdsc->metric, req->r_start_latency,
903f4fec 226 req->r_end_latency, osd_data->length, err);
f0702876
JL
227
228 dout("%s: result %d subreq->len=%zu i_size=%lld\n", __func__, req->r_result,
229 subreq->len, i_size_read(req->r_inode));
230
231 /* no object means success but no data */
232 if (err == -ENOENT)
233 err = 0;
234 else if (err == -EBLOCKLISTED)
235 fsc->blocklisted = true;
236
237 if (err >= 0 && err < subreq->len)
238 __set_bit(NETFS_SREQ_CLEAR_TAIL, &subreq->flags);
239
240 netfs_subreq_terminated(subreq, err, true);
241
242 num_pages = calc_pages_for(osd_data->alignment, osd_data->length);
243 ceph_put_page_vector(osd_data->pages, num_pages, false);
244 iput(req->r_inode);
245}
246
6a19114b 247static bool ceph_netfs_issue_op_inline(struct netfs_io_subrequest *subreq)
5b19f1eb 248{
6a19114b 249 struct netfs_io_request *rreq = subreq->rreq;
5b19f1eb
DH
250 struct inode *inode = rreq->inode;
251 struct ceph_mds_reply_info_parsed *rinfo;
252 struct ceph_mds_reply_info_in *iinfo;
253 struct ceph_mds_request *req;
254 struct ceph_mds_client *mdsc = ceph_sb_to_mdsc(inode->i_sb);
255 struct ceph_inode_info *ci = ceph_inode(inode);
256 struct iov_iter iter;
257 ssize_t err = 0;
258 size_t len;
5eed80fb 259 int mode;
5b19f1eb
DH
260
261 __set_bit(NETFS_SREQ_CLEAR_TAIL, &subreq->flags);
f18a3785 262 __clear_bit(NETFS_SREQ_COPY_TO_CACHE, &subreq->flags);
5b19f1eb
DH
263
264 if (subreq->start >= inode->i_size)
265 goto out;
266
267 /* We need to fetch the inline data. */
5eed80fb
XL
268 mode = ceph_try_to_choose_auth_mds(inode, CEPH_STAT_CAP_INLINE_DATA);
269 req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_GETATTR, mode);
5b19f1eb
DH
270 if (IS_ERR(req)) {
271 err = PTR_ERR(req);
272 goto out;
273 }
274 req->r_ino1 = ci->i_vino;
275 req->r_args.getattr.mask = cpu_to_le32(CEPH_STAT_CAP_INLINE_DATA);
276 req->r_num_caps = 2;
277
278 err = ceph_mdsc_do_request(mdsc, NULL, req);
279 if (err < 0)
280 goto out;
281
282 rinfo = &req->r_reply_info;
283 iinfo = &rinfo->targeti;
284 if (iinfo->inline_version == CEPH_INLINE_NONE) {
285 /* The data got uninlined */
286 ceph_mdsc_put_request(req);
287 return false;
288 }
289
290 len = min_t(size_t, iinfo->inline_len - subreq->start, subreq->len);
291 iov_iter_xarray(&iter, READ, &rreq->mapping->i_pages, subreq->start, len);
292 err = copy_to_iter(iinfo->inline_data + subreq->start, len, &iter);
293 if (err == 0)
294 err = -EFAULT;
295
296 ceph_mdsc_put_request(req);
297out:
298 netfs_subreq_terminated(subreq, err, false);
299 return true;
300}
301
f18a3785 302static void ceph_netfs_issue_read(struct netfs_io_subrequest *subreq)
f0702876 303{
6a19114b 304 struct netfs_io_request *rreq = subreq->rreq;
a25cedb4 305 struct inode *inode = rreq->inode;
f0702876
JL
306 struct ceph_inode_info *ci = ceph_inode(inode);
307 struct ceph_fs_client *fsc = ceph_inode_to_client(inode);
308 struct ceph_osd_request *req;
309 struct ceph_vino vino = ceph_vino(inode);
310 struct iov_iter iter;
311 struct page **pages;
312 size_t page_off;
313 int err = 0;
314 u64 len = subreq->len;
315
5b19f1eb
DH
316 if (ci->i_inline_version != CEPH_INLINE_NONE &&
317 ceph_netfs_issue_op_inline(subreq))
318 return;
319
f0702876
JL
320 req = ceph_osdc_new_request(&fsc->client->osdc, &ci->i_layout, vino, subreq->start, &len,
321 0, 1, CEPH_OSD_OP_READ,
322 CEPH_OSD_FLAG_READ | fsc->client->osdc.client->options->read_from_replica,
323 NULL, ci->i_truncate_seq, ci->i_truncate_size, false);
324 if (IS_ERR(req)) {
325 err = PTR_ERR(req);
326 req = NULL;
327 goto out;
328 }
329
330 dout("%s: pos=%llu orig_len=%zu len=%llu\n", __func__, subreq->start, subreq->len, len);
331 iov_iter_xarray(&iter, READ, &rreq->mapping->i_pages, subreq->start, len);
332 err = iov_iter_get_pages_alloc(&iter, &pages, len, &page_off);
333 if (err < 0) {
334 dout("%s: iov_ter_get_pages_alloc returned %d\n", __func__, err);
335 goto out;
336 }
337
338 /* should always give us a page-aligned read */
339 WARN_ON_ONCE(page_off);
340 len = err;
341
342 osd_req_op_extent_osd_data_pages(req, 0, pages, len, 0, false, false);
343 req->r_callback = finish_netfs_read;
344 req->r_priv = subreq;
345 req->r_inode = inode;
346 ihold(inode);
347
348 err = ceph_osdc_start_request(req->r_osdc, req, false);
349 if (err)
350 iput(inode);
351out:
352 ceph_osdc_put_request(req);
353 if (err)
354 netfs_subreq_terminated(subreq, err, false);
355 dout("%s: result %d\n", __func__, err);
356}
357
a5c9dc44
DH
358static int ceph_init_request(struct netfs_io_request *rreq, struct file *file)
359{
360 struct inode *inode = rreq->inode;
361 int got = 0, want = CEPH_CAP_FILE_CACHE;
362 int ret = 0;
363
364 if (rreq->origin != NETFS_READAHEAD)
365 return 0;
366
367 if (file) {
368 struct ceph_rw_context *rw_ctx;
369 struct ceph_file_info *fi = file->private_data;
370
371 rw_ctx = ceph_find_rw_context(fi);
372 if (rw_ctx)
373 return 0;
374 }
375
376 /*
377 * readahead callers do not necessarily hold Fcb caps
378 * (e.g. fadvise, madvise).
379 */
380 ret = ceph_try_get_caps(inode, CEPH_CAP_FILE_RD, want, true, &got);
381 if (ret < 0) {
382 dout("start_read %p, error getting cap\n", inode);
383 return ret;
384 }
385
386 if (!(got & want)) {
387 dout("start_read %p, no cache cap\n", inode);
388 return -EACCES;
389 }
390 if (ret == 0)
391 return -EACCES;
392
393 rreq->netfs_priv = (void *)(uintptr_t)got;
394 return 0;
395}
396
49870056
JL
397static void ceph_readahead_cleanup(struct address_space *mapping, void *priv)
398{
399 struct inode *inode = mapping->host;
400 struct ceph_inode_info *ci = ceph_inode(inode);
401 int got = (uintptr_t)priv;
402
403 if (got)
404 ceph_put_cap_refs(ci, got);
405}
406
bc899ee1 407const struct netfs_request_ops ceph_netfs_ops = {
a5c9dc44 408 .init_request = ceph_init_request,
f0702876 409 .begin_cache_operation = ceph_begin_cache_operation,
f18a3785 410 .issue_read = ceph_netfs_issue_read,
f0702876
JL
411 .expand_readahead = ceph_netfs_expand_readahead,
412 .clamp_length = ceph_netfs_clamp_length,
d801327d 413 .check_write_begin = ceph_netfs_check_write_begin,
49870056 414 .cleanup = ceph_readahead_cleanup,
f0702876
JL
415};
416
1702e797
JL
417#ifdef CONFIG_CEPH_FSCACHE
418static void ceph_set_page_fscache(struct page *page)
419{
420 set_page_fscache(page);
421}
422
423static void ceph_fscache_write_terminated(void *priv, ssize_t error, bool was_async)
424{
425 struct inode *inode = priv;
426
427 if (IS_ERR_VALUE(error) && error != -ENOBUFS)
428 ceph_fscache_invalidate(inode, false);
429}
430
431static void ceph_fscache_write_to_cache(struct inode *inode, u64 off, u64 len, bool caching)
432{
433 struct ceph_inode_info *ci = ceph_inode(inode);
434 struct fscache_cookie *cookie = ceph_fscache_cookie(ci);
435
436 fscache_write_to_cache(cookie, inode->i_mapping, off, len, i_size_read(inode),
437 ceph_fscache_write_terminated, inode, caching);
438}
439#else
440static inline void ceph_set_page_fscache(struct page *page)
441{
442}
443
444static inline void ceph_fscache_write_to_cache(struct inode *inode, u64 off, u64 len, bool caching)
445{
446}
447#endif /* CONFIG_CEPH_FSCACHE */
448
1f934b00
YZ
449struct ceph_writeback_ctl
450{
451 loff_t i_size;
452 u64 truncate_size;
453 u32 truncate_seq;
454 bool size_stable;
2a2d927e 455 bool head_snapc;
1f934b00
YZ
456};
457
1d3576fd
SW
458/*
459 * Get ref for the oldest snapc for an inode with dirty data... that is, the
460 * only snap context we are allowed to write back.
1d3576fd 461 */
1f934b00 462static struct ceph_snap_context *
05455e11
YZ
463get_oldest_context(struct inode *inode, struct ceph_writeback_ctl *ctl,
464 struct ceph_snap_context *page_snapc)
1d3576fd
SW
465{
466 struct ceph_inode_info *ci = ceph_inode(inode);
467 struct ceph_snap_context *snapc = NULL;
468 struct ceph_cap_snap *capsnap = NULL;
469
be655596 470 spin_lock(&ci->i_ceph_lock);
1d3576fd
SW
471 list_for_each_entry(capsnap, &ci->i_cap_snaps, ci_item) {
472 dout(" cap_snap %p snapc %p has %d dirty pages\n", capsnap,
473 capsnap->context, capsnap->dirty_pages);
05455e11
YZ
474 if (!capsnap->dirty_pages)
475 continue;
476
477 /* get i_size, truncate_{seq,size} for page_snapc? */
478 if (snapc && capsnap->context != page_snapc)
479 continue;
480
481 if (ctl) {
482 if (capsnap->writing) {
483 ctl->i_size = i_size_read(inode);
484 ctl->size_stable = false;
485 } else {
486 ctl->i_size = capsnap->size;
487 ctl->size_stable = true;
1f934b00 488 }
05455e11
YZ
489 ctl->truncate_size = capsnap->truncate_size;
490 ctl->truncate_seq = capsnap->truncate_seq;
2a2d927e 491 ctl->head_snapc = false;
1d3576fd 492 }
05455e11
YZ
493
494 if (snapc)
495 break;
496
497 snapc = ceph_get_snap_context(capsnap->context);
498 if (!page_snapc ||
499 page_snapc == snapc ||
500 page_snapc->seq > snapc->seq)
501 break;
1d3576fd 502 }
7d8cb26d 503 if (!snapc && ci->i_wrbuffer_ref_head) {
80e755fe 504 snapc = ceph_get_snap_context(ci->i_head_snapc);
1d3576fd
SW
505 dout(" head snapc %p has %d dirty pages\n",
506 snapc, ci->i_wrbuffer_ref_head);
1f934b00
YZ
507 if (ctl) {
508 ctl->i_size = i_size_read(inode);
509 ctl->truncate_size = ci->i_truncate_size;
510 ctl->truncate_seq = ci->i_truncate_seq;
511 ctl->size_stable = false;
2a2d927e 512 ctl->head_snapc = true;
1f934b00 513 }
1d3576fd 514 }
be655596 515 spin_unlock(&ci->i_ceph_lock);
1d3576fd
SW
516 return snapc;
517}
518
1f934b00
YZ
519static u64 get_writepages_data_length(struct inode *inode,
520 struct page *page, u64 start)
521{
522 struct ceph_inode_info *ci = ceph_inode(inode);
523 struct ceph_snap_context *snapc = page_snap_context(page);
524 struct ceph_cap_snap *capsnap = NULL;
525 u64 end = i_size_read(inode);
526
527 if (snapc != ci->i_head_snapc) {
528 bool found = false;
529 spin_lock(&ci->i_ceph_lock);
530 list_for_each_entry(capsnap, &ci->i_cap_snaps, ci_item) {
531 if (capsnap->context == snapc) {
532 if (!capsnap->writing)
533 end = capsnap->size;
534 found = true;
535 break;
536 }
537 }
538 spin_unlock(&ci->i_ceph_lock);
539 WARN_ON(!found);
540 }
8ff2d290
JL
541 if (end > page_offset(page) + thp_size(page))
542 end = page_offset(page) + thp_size(page);
1f934b00
YZ
543 return end > start ? end - start : 0;
544}
545
1d3576fd
SW
546/*
547 * Write a single page, but leave the page locked.
548 *
b72b13eb 549 * If we get a write error, mark the mapping for error, but still adjust the
1d3576fd
SW
550 * dirty page accounting (i.e., page is no longer dirty).
551 */
552static int writepage_nounlock(struct page *page, struct writeback_control *wbc)
553{
a628304e 554 struct folio *folio = page_folio(page);
6390987f
JL
555 struct inode *inode = page->mapping->host;
556 struct ceph_inode_info *ci = ceph_inode(inode);
557 struct ceph_fs_client *fsc = ceph_inode_to_client(inode);
6298a337 558 struct ceph_snap_context *snapc, *oldest;
fc2744aa 559 loff_t page_off = page_offset(page);
6390987f 560 int err;
8ff2d290 561 loff_t len = thp_size(page);
1f934b00 562 struct ceph_writeback_ctl ceph_wbc;
6390987f
JL
563 struct ceph_osd_client *osdc = &fsc->client->osdc;
564 struct ceph_osd_request *req;
1702e797 565 bool caching = ceph_is_cache_enabled(inode);
1d3576fd
SW
566
567 dout("writepage %p idx %lu\n", page, page->index);
568
1d3576fd 569 /* verify this is a writeable snap context */
61600ef8 570 snapc = page_snap_context(page);
d37b1d99 571 if (!snapc) {
1d3576fd 572 dout("writepage %p page %p not dirty?\n", inode, page);
43986881 573 return 0;
1d3576fd 574 }
05455e11 575 oldest = get_oldest_context(inode, &ceph_wbc, snapc);
6298a337 576 if (snapc->seq > oldest->seq) {
1d3576fd 577 dout("writepage %p page %p snapc %p not writeable - noop\n",
61600ef8 578 inode, page, snapc);
1d3576fd 579 /* we should only noop if called by kswapd */
fa71fefb 580 WARN_ON(!(current->flags & PF_MEMALLOC));
6298a337 581 ceph_put_snap_context(oldest);
fa71fefb 582 redirty_page_for_writepage(wbc, page);
43986881 583 return 0;
1d3576fd 584 }
6298a337 585 ceph_put_snap_context(oldest);
1d3576fd
SW
586
587 /* is this a partial page at end of file? */
1f934b00 588 if (page_off >= ceph_wbc.i_size) {
a628304e
MWO
589 dout("folio at %lu beyond eof %llu\n", folio->index,
590 ceph_wbc.i_size);
591 folio_invalidate(folio, 0, folio_size(folio));
43986881 592 return 0;
fc2744aa 593 }
43986881 594
1f934b00
YZ
595 if (ceph_wbc.i_size < page_off + len)
596 len = ceph_wbc.i_size - page_off;
1d3576fd 597
6390987f 598 dout("writepage %p page %p index %lu on %llu~%llu snapc %p seq %lld\n",
1c0a9c2d 599 inode, page, page->index, page_off, len, snapc, snapc->seq);
1d3576fd 600
314c4737 601 if (atomic_long_inc_return(&fsc->writeback_count) >
3d14c5d2 602 CONGESTION_ON_THRESH(fsc->mount_options->congestion_kb))
503d4fa6 603 fsc->write_congested = true;
2baba250 604
6390987f
JL
605 req = ceph_osdc_new_request(osdc, &ci->i_layout, ceph_vino(inode), page_off, &len, 0, 1,
606 CEPH_OSD_OP_WRITE, CEPH_OSD_FLAG_WRITE, snapc,
607 ceph_wbc.truncate_seq, ceph_wbc.truncate_size,
608 true);
3459bd0c
XL
609 if (IS_ERR(req)) {
610 redirty_page_for_writepage(wbc, page);
6390987f 611 return PTR_ERR(req);
3459bd0c 612 }
1702e797
JL
613
614 set_page_writeback(page);
615 if (caching)
616 ceph_set_page_fscache(page);
617 ceph_fscache_write_to_cache(inode, page_off, len, caching);
6390987f
JL
618
619 /* it may be a short write due to an object boundary */
8ff2d290 620 WARN_ON_ONCE(len > thp_size(page));
6390987f
JL
621 osd_req_op_extent_osd_data_pages(req, 0, &page, len, 0, false, false);
622 dout("writepage %llu~%llu (%llu bytes)\n", page_off, len, len);
623
624 req->r_mtime = inode->i_mtime;
625 err = ceph_osdc_start_request(osdc, req, true);
626 if (!err)
627 err = ceph_osdc_wait_request(osdc, req);
628
8ae99ae2 629 ceph_update_write_metrics(&fsc->mdsc->metric, req->r_start_latency,
903f4fec 630 req->r_end_latency, len, err);
6390987f
JL
631
632 ceph_osdc_put_request(req);
633 if (err == 0)
634 err = len;
635
1d3576fd 636 if (err < 0) {
ad15ec06
YZ
637 struct writeback_control tmp_wbc;
638 if (!wbc)
639 wbc = &tmp_wbc;
640 if (err == -ERESTARTSYS) {
641 /* killed by SIGKILL */
642 dout("writepage interrupted page %p\n", page);
643 redirty_page_for_writepage(wbc, page);
644 end_page_writeback(page);
43986881 645 return err;
ad15ec06 646 }
0b98acd6
ID
647 if (err == -EBLOCKLISTED)
648 fsc->blocklisted = true;
ad15ec06
YZ
649 dout("writepage setting page/mapping error %d %p\n",
650 err, page);
1d3576fd 651 mapping_set_error(&inode->i_data, err);
ad15ec06 652 wbc->pages_skipped++;
1d3576fd
SW
653 } else {
654 dout("writepage cleaned page %p\n", page);
655 err = 0; /* vfs expects us to return 0 */
656 }
379fc7fa
JL
657 oldest = detach_page_private(page);
658 WARN_ON_ONCE(oldest != snapc);
1d3576fd
SW
659 end_page_writeback(page);
660 ceph_put_wrbuffer_cap_refs(ci, 1, snapc);
6298a337 661 ceph_put_snap_context(snapc); /* page's reference */
314c4737
YZ
662
663 if (atomic_long_dec_return(&fsc->writeback_count) <
664 CONGESTION_OFF_THRESH(fsc->mount_options->congestion_kb))
503d4fa6 665 fsc->write_congested = false;
314c4737 666
1d3576fd
SW
667 return err;
668}
669
670static int ceph_writepage(struct page *page, struct writeback_control *wbc)
671{
dbd646a8
YS
672 int err;
673 struct inode *inode = page->mapping->host;
674 BUG_ON(!inode);
70b666c3 675 ihold(inode);
1702e797 676
503d4fa6
N
677 if (wbc->sync_mode == WB_SYNC_NONE &&
678 ceph_inode_to_client(inode)->write_congested)
679 return AOP_WRITEPAGE_ACTIVATE;
680
1702e797
JL
681 wait_on_page_fscache(page);
682
dbd646a8 683 err = writepage_nounlock(page, wbc);
ad15ec06
YZ
684 if (err == -ERESTARTSYS) {
685 /* direct memory reclaimer was killed by SIGKILL. return 0
686 * to prevent caller from setting mapping/page error */
687 err = 0;
688 }
1d3576fd 689 unlock_page(page);
dbd646a8 690 iput(inode);
1d3576fd
SW
691 return err;
692}
693
1d3576fd
SW
694/*
695 * async writeback completion handler.
696 *
697 * If we get an error, set the mapping error bit, but not the individual
698 * page error bits.
699 */
85e084fe 700static void writepages_finish(struct ceph_osd_request *req)
1d3576fd
SW
701{
702 struct inode *inode = req->r_inode;
1d3576fd 703 struct ceph_inode_info *ci = ceph_inode(inode);
87060c10 704 struct ceph_osd_data *osd_data;
1d3576fd 705 struct page *page;
5b64640c
YZ
706 int num_pages, total_pages = 0;
707 int i, j;
708 int rc = req->r_result;
1d3576fd
SW
709 struct ceph_snap_context *snapc = req->r_snapc;
710 struct address_space *mapping = inode->i_mapping;
3d14c5d2 711 struct ceph_fs_client *fsc = ceph_inode_to_client(inode);
903f4fec 712 unsigned int len = 0;
5b64640c 713 bool remove_page;
1d3576fd 714
5b64640c 715 dout("writepages_finish %p rc %d\n", inode, rc);
26544c62 716 if (rc < 0) {
1d3576fd 717 mapping_set_error(mapping, rc);
26544c62 718 ceph_set_error_write(ci);
0b98acd6
ID
719 if (rc == -EBLOCKLISTED)
720 fsc->blocklisted = true;
26544c62
JL
721 } else {
722 ceph_clear_error_write(ci);
723 }
5b64640c
YZ
724
725 /*
726 * We lost the cache cap, need to truncate the page before
727 * it is unlocked, otherwise we'd truncate it later in the
728 * page truncation thread, possibly losing some data that
729 * raced its way in
730 */
731 remove_page = !(ceph_caps_issued(ci) &
732 (CEPH_CAP_FILE_CACHE|CEPH_CAP_FILE_LAZYIO));
1d3576fd
SW
733
734 /* clean all pages */
5b64640c 735 for (i = 0; i < req->r_num_ops; i++) {
642d51fb
XL
736 if (req->r_ops[i].op != CEPH_OSD_OP_WRITE) {
737 pr_warn("%s incorrect op %d req %p index %d tid %llu\n",
738 __func__, req->r_ops[i].op, req, i, req->r_tid);
5b64640c 739 break;
642d51fb 740 }
e63dc5c7 741
5b64640c
YZ
742 osd_data = osd_req_op_extent_osd_data(req, i);
743 BUG_ON(osd_data->type != CEPH_OSD_DATA_TYPE_PAGES);
903f4fec 744 len += osd_data->length;
5b64640c
YZ
745 num_pages = calc_pages_for((u64)osd_data->alignment,
746 (u64)osd_data->length);
747 total_pages += num_pages;
748 for (j = 0; j < num_pages; j++) {
749 page = osd_data->pages[j];
750 BUG_ON(!page);
751 WARN_ON(!PageUptodate(page));
752
753 if (atomic_long_dec_return(&fsc->writeback_count) <
754 CONGESTION_OFF_THRESH(
755 fsc->mount_options->congestion_kb))
503d4fa6 756 fsc->write_congested = false;
5b64640c 757
379fc7fa 758 ceph_put_snap_context(detach_page_private(page));
5b64640c 759 end_page_writeback(page);
379fc7fa 760 dout("unlocking %p\n", page);
5b64640c
YZ
761
762 if (remove_page)
763 generic_error_remove_page(inode->i_mapping,
764 page);
765
766 unlock_page(page);
767 }
768 dout("writepages_finish %p wrote %llu bytes cleaned %d pages\n",
769 inode, osd_data->length, rc >= 0 ? num_pages : 0);
e63dc5c7 770
96ac9158 771 release_pages(osd_data->pages, num_pages);
1d3576fd 772 }
1d3576fd 773
903f4fec
XL
774 ceph_update_write_metrics(&fsc->mdsc->metric, req->r_start_latency,
775 req->r_end_latency, len, rc);
776
5b64640c
YZ
777 ceph_put_wrbuffer_cap_refs(ci, total_pages, snapc);
778
779 osd_data = osd_req_op_extent_osd_data(req, 0);
87060c10 780 if (osd_data->pages_from_pool)
a0102bda 781 mempool_free(osd_data->pages, ceph_wb_pagevec_pool);
1d3576fd 782 else
87060c10 783 kfree(osd_data->pages);
1d3576fd
SW
784 ceph_osdc_put_request(req);
785}
786
1d3576fd
SW
787/*
788 * initiate async writeback
789 */
790static int ceph_writepages_start(struct address_space *mapping,
791 struct writeback_control *wbc)
792{
793 struct inode *inode = mapping->host;
1d3576fd 794 struct ceph_inode_info *ci = ceph_inode(inode);
fc2744aa
YZ
795 struct ceph_fs_client *fsc = ceph_inode_to_client(inode);
796 struct ceph_vino vino = ceph_vino(inode);
2a2d927e 797 pgoff_t index, start_index, end = -1;
80e755fe 798 struct ceph_snap_context *snapc = NULL, *last_snapc = NULL, *pgsnapc;
1d3576fd 799 struct pagevec pvec;
1d3576fd 800 int rc = 0;
93407472 801 unsigned int wsize = i_blocksize(inode);
1d3576fd 802 struct ceph_osd_request *req = NULL;
1f934b00 803 struct ceph_writeback_ctl ceph_wbc;
590e9d98 804 bool should_loop, range_whole = false;
af9cc401 805 bool done = false;
1702e797 806 bool caching = ceph_is_cache_enabled(inode);
1d3576fd 807
503d4fa6
N
808 if (wbc->sync_mode == WB_SYNC_NONE &&
809 fsc->write_congested)
810 return 0;
811
3fb99d48 812 dout("writepages_start %p (mode=%s)\n", inode,
1d3576fd
SW
813 wbc->sync_mode == WB_SYNC_NONE ? "NONE" :
814 (wbc->sync_mode == WB_SYNC_ALL ? "ALL" : "HOLD"));
815
5d6451b1 816 if (ceph_inode_is_shutdown(inode)) {
6c93df5d
YZ
817 if (ci->i_wrbuffer_ref > 0) {
818 pr_warn_ratelimited(
819 "writepage_start %p %lld forced umount\n",
820 inode, ceph_ino(inode));
821 }
a341d4df 822 mapping_set_error(mapping, -EIO);
1d3576fd
SW
823 return -EIO; /* we're in a forced umount, don't write! */
824 }
95cca2b4 825 if (fsc->mount_options->wsize < wsize)
3d14c5d2 826 wsize = fsc->mount_options->wsize;
1d3576fd 827
86679820 828 pagevec_init(&pvec);
1d3576fd 829
590e9d98 830 start_index = wbc->range_cyclic ? mapping->writeback_index : 0;
2a2d927e 831 index = start_index;
1d3576fd
SW
832
833retry:
834 /* find oldest snap context with dirty data */
05455e11 835 snapc = get_oldest_context(inode, &ceph_wbc, NULL);
1d3576fd
SW
836 if (!snapc) {
837 /* hmm, why does writepages get called when there
838 is no dirty data? */
839 dout(" no snap context with dirty data?\n");
840 goto out;
841 }
842 dout(" oldest snapc is %p seq %lld (%d snaps)\n",
843 snapc, snapc->seq, snapc->num_snaps);
fc2744aa 844
2a2d927e
YZ
845 should_loop = false;
846 if (ceph_wbc.head_snapc && snapc != last_snapc) {
847 /* where to start/end? */
848 if (wbc->range_cyclic) {
849 index = start_index;
850 end = -1;
851 if (index > 0)
852 should_loop = true;
853 dout(" cyclic, start at %lu\n", index);
854 } else {
855 index = wbc->range_start >> PAGE_SHIFT;
856 end = wbc->range_end >> PAGE_SHIFT;
857 if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX)
858 range_whole = true;
859 dout(" not cyclic, %lu to %lu\n", index, end);
860 }
861 } else if (!ceph_wbc.head_snapc) {
862 /* Do not respect wbc->range_{start,end}. Dirty pages
863 * in that range can be associated with newer snapc.
864 * They are not writeable until we write all dirty pages
865 * associated with 'snapc' get written */
1582af2e 866 if (index > 0)
2a2d927e
YZ
867 should_loop = true;
868 dout(" non-head snapc, range whole\n");
1d3576fd 869 }
2a2d927e
YZ
870
871 ceph_put_snap_context(last_snapc);
1d3576fd
SW
872 last_snapc = snapc;
873
af9cc401 874 while (!done && index <= end) {
5b64640c 875 int num_ops = 0, op_idx;
0e5ecac7 876 unsigned i, pvec_pages, max_pages, locked_pages = 0;
5b64640c 877 struct page **pages = NULL, **data_pages;
1d3576fd 878 struct page *page;
0e5ecac7 879 pgoff_t strip_unit_end = 0;
5b64640c 880 u64 offset = 0, len = 0;
a0102bda 881 bool from_pool = false;
1d3576fd 882
0e5ecac7 883 max_pages = wsize >> PAGE_SHIFT;
1d3576fd
SW
884
885get_more_pages:
2e169296
JL
886 pvec_pages = pagevec_lookup_range_tag(&pvec, mapping, &index,
887 end, PAGECACHE_TAG_DIRTY);
0ed75fc8 888 dout("pagevec_lookup_range_tag got %d\n", pvec_pages);
1d3576fd
SW
889 if (!pvec_pages && !locked_pages)
890 break;
891 for (i = 0; i < pvec_pages && locked_pages < max_pages; i++) {
892 page = pvec.pages[i];
893 dout("? %p idx %lu\n", page, page->index);
894 if (locked_pages == 0)
895 lock_page(page); /* first page */
896 else if (!trylock_page(page))
897 break;
898
899 /* only dirty pages, or our accounting breaks */
900 if (unlikely(!PageDirty(page)) ||
901 unlikely(page->mapping != mapping)) {
902 dout("!dirty or !mapping %p\n", page);
903 unlock_page(page);
0713e5f2 904 continue;
1d3576fd 905 }
af9cc401
YZ
906 /* only if matching snap context */
907 pgsnapc = page_snap_context(page);
908 if (pgsnapc != snapc) {
909 dout("page snapc %p %lld != oldest %p %lld\n",
910 pgsnapc, pgsnapc->seq, snapc, snapc->seq);
1582af2e
YZ
911 if (!should_loop &&
912 !ceph_wbc.head_snapc &&
913 wbc->sync_mode != WB_SYNC_NONE)
914 should_loop = true;
1d3576fd 915 unlock_page(page);
af9cc401 916 continue;
1d3576fd 917 }
1f934b00 918 if (page_offset(page) >= ceph_wbc.i_size) {
a628304e
MWO
919 struct folio *folio = page_folio(page);
920
921 dout("folio at %lu beyond eof %llu\n",
922 folio->index, ceph_wbc.i_size);
c95f1c5f 923 if ((ceph_wbc.size_stable ||
a628304e
MWO
924 folio_pos(folio) >= i_size_read(inode)) &&
925 folio_clear_dirty_for_io(folio))
926 folio_invalidate(folio, 0,
927 folio_size(folio));
928 folio_unlock(folio);
af9cc401
YZ
929 continue;
930 }
931 if (strip_unit_end && (page->index > strip_unit_end)) {
932 dout("end of strip unit %p\n", page);
1d3576fd
SW
933 unlock_page(page);
934 break;
935 }
1702e797 936 if (PageWriteback(page) || PageFsCache(page)) {
0713e5f2
YZ
937 if (wbc->sync_mode == WB_SYNC_NONE) {
938 dout("%p under writeback\n", page);
939 unlock_page(page);
940 continue;
941 }
942 dout("waiting on writeback %p\n", page);
943 wait_on_page_writeback(page);
1702e797 944 wait_on_page_fscache(page);
1d3576fd
SW
945 }
946
1d3576fd
SW
947 if (!clear_page_dirty_for_io(page)) {
948 dout("%p !clear_page_dirty_for_io\n", page);
949 unlock_page(page);
0713e5f2 950 continue;
1d3576fd
SW
951 }
952
e5975c7c
AE
953 /*
954 * We have something to write. If this is
955 * the first locked page this time through,
5b64640c
YZ
956 * calculate max possinle write size and
957 * allocate a page array
e5975c7c 958 */
1d3576fd 959 if (locked_pages == 0) {
5b64640c
YZ
960 u64 objnum;
961 u64 objoff;
dccbf080 962 u32 xlen;
5b64640c 963
1d3576fd 964 /* prepare async write request */
e5975c7c 965 offset = (u64)page_offset(page);
dccbf080
ID
966 ceph_calc_file_object_mapping(&ci->i_layout,
967 offset, wsize,
968 &objnum, &objoff,
969 &xlen);
970 len = xlen;
8c71897b 971
3fb99d48 972 num_ops = 1;
5b64640c 973 strip_unit_end = page->index +
09cbfeaf 974 ((len - 1) >> PAGE_SHIFT);
88486957 975
5b64640c 976 BUG_ON(pages);
88486957 977 max_pages = calc_pages_for(0, (u64)len);
6da2ec56
KC
978 pages = kmalloc_array(max_pages,
979 sizeof(*pages),
980 GFP_NOFS);
88486957 981 if (!pages) {
a0102bda
JL
982 from_pool = true;
983 pages = mempool_alloc(ceph_wb_pagevec_pool, GFP_NOFS);
e5975c7c 984 BUG_ON(!pages);
88486957 985 }
5b64640c
YZ
986
987 len = 0;
988 } else if (page->index !=
09cbfeaf 989 (offset + len) >> PAGE_SHIFT) {
a0102bda
JL
990 if (num_ops >= (from_pool ? CEPH_OSD_SLAB_OPS :
991 CEPH_OSD_MAX_OPS)) {
5b64640c
YZ
992 redirty_page_for_writepage(wbc, page);
993 unlock_page(page);
994 break;
995 }
996
997 num_ops++;
998 offset = (u64)page_offset(page);
999 len = 0;
1d3576fd
SW
1000 }
1001
1002 /* note position of first page in pvec */
1d3576fd
SW
1003 dout("%p will write page %p idx %lu\n",
1004 inode, page, page->index);
2baba250 1005
5b64640c
YZ
1006 if (atomic_long_inc_return(&fsc->writeback_count) >
1007 CONGESTION_ON_THRESH(
503d4fa6
N
1008 fsc->mount_options->congestion_kb))
1009 fsc->write_congested = true;
0713e5f2
YZ
1010
1011 pages[locked_pages++] = page;
1012 pvec.pages[i] = NULL;
1013
8ff2d290 1014 len += thp_size(page);
1d3576fd
SW
1015 }
1016
1017 /* did we get anything? */
1018 if (!locked_pages)
1019 goto release_pvec_pages;
1020 if (i) {
0713e5f2
YZ
1021 unsigned j, n = 0;
1022 /* shift unused page to beginning of pvec */
1023 for (j = 0; j < pvec_pages; j++) {
1024 if (!pvec.pages[j])
1025 continue;
1026 if (n < j)
1027 pvec.pages[n] = pvec.pages[j];
1028 n++;
1029 }
1030 pvec.nr = n;
1d3576fd
SW
1031
1032 if (pvec_pages && i == pvec_pages &&
1033 locked_pages < max_pages) {
1034 dout("reached end pvec, trying for more\n");
0713e5f2 1035 pagevec_release(&pvec);
1d3576fd
SW
1036 goto get_more_pages;
1037 }
1d3576fd
SW
1038 }
1039
5b64640c 1040new_request:
e5975c7c 1041 offset = page_offset(pages[0]);
5b64640c
YZ
1042 len = wsize;
1043
1044 req = ceph_osdc_new_request(&fsc->client->osdc,
1045 &ci->i_layout, vino,
1046 offset, &len, 0, num_ops,
1f934b00
YZ
1047 CEPH_OSD_OP_WRITE, CEPH_OSD_FLAG_WRITE,
1048 snapc, ceph_wbc.truncate_seq,
1049 ceph_wbc.truncate_size, false);
5b64640c
YZ
1050 if (IS_ERR(req)) {
1051 req = ceph_osdc_new_request(&fsc->client->osdc,
1052 &ci->i_layout, vino,
1053 offset, &len, 0,
1054 min(num_ops,
1055 CEPH_OSD_SLAB_OPS),
1056 CEPH_OSD_OP_WRITE,
54ea0046 1057 CEPH_OSD_FLAG_WRITE,
1f934b00
YZ
1058 snapc, ceph_wbc.truncate_seq,
1059 ceph_wbc.truncate_size, true);
5b64640c 1060 BUG_ON(IS_ERR(req));
e1966b49 1061 }
5b64640c 1062 BUG_ON(len < page_offset(pages[locked_pages - 1]) +
8ff2d290 1063 thp_size(page) - offset);
5b64640c
YZ
1064
1065 req->r_callback = writepages_finish;
1066 req->r_inode = inode;
1d3576fd 1067
5b64640c
YZ
1068 /* Format the osd request message and submit the write */
1069 len = 0;
1070 data_pages = pages;
1071 op_idx = 0;
1072 for (i = 0; i < locked_pages; i++) {
1073 u64 cur_offset = page_offset(pages[i]);
1702e797
JL
1074 /*
1075 * Discontinuity in page range? Ceph can handle that by just passing
1076 * multiple extents in the write op.
1077 */
5b64640c 1078 if (offset + len != cur_offset) {
1702e797 1079 /* If it's full, stop here */
3fb99d48 1080 if (op_idx + 1 == req->r_num_ops)
5b64640c 1081 break;
1702e797
JL
1082
1083 /* Kick off an fscache write with what we have so far. */
1084 ceph_fscache_write_to_cache(inode, offset, len, caching);
1085
1086 /* Start a new extent */
5b64640c
YZ
1087 osd_req_op_extent_dup_last(req, op_idx,
1088 cur_offset - offset);
1089 dout("writepages got pages at %llu~%llu\n",
1090 offset, len);
1091 osd_req_op_extent_osd_data_pages(req, op_idx,
1092 data_pages, len, 0,
a0102bda 1093 from_pool, false);
5b64640c 1094 osd_req_op_extent_update(req, op_idx, len);
e5975c7c 1095
5b64640c 1096 len = 0;
1702e797 1097 offset = cur_offset;
5b64640c
YZ
1098 data_pages = pages + i;
1099 op_idx++;
1100 }
1101
1102 set_page_writeback(pages[i]);
1702e797
JL
1103 if (caching)
1104 ceph_set_page_fscache(pages[i]);
8ff2d290 1105 len += thp_size(page);
5b64640c 1106 }
1702e797 1107 ceph_fscache_write_to_cache(inode, offset, len, caching);
5b64640c 1108
1f934b00
YZ
1109 if (ceph_wbc.size_stable) {
1110 len = min(len, ceph_wbc.i_size - offset);
5b64640c
YZ
1111 } else if (i == locked_pages) {
1112 /* writepages_finish() clears writeback pages
1113 * according to the data length, so make sure
1114 * data length covers all locked pages */
8ff2d290 1115 u64 min_len = len + 1 - thp_size(page);
1f934b00
YZ
1116 len = get_writepages_data_length(inode, pages[i - 1],
1117 offset);
5b64640c
YZ
1118 len = max(len, min_len);
1119 }
1120 dout("writepages got pages at %llu~%llu\n", offset, len);
e5975c7c 1121
5b64640c 1122 osd_req_op_extent_osd_data_pages(req, op_idx, data_pages, len,
a0102bda 1123 0, from_pool, false);
5b64640c 1124 osd_req_op_extent_update(req, op_idx, len);
e5975c7c 1125
5b64640c
YZ
1126 BUG_ON(op_idx + 1 != req->r_num_ops);
1127
a0102bda 1128 from_pool = false;
5b64640c
YZ
1129 if (i < locked_pages) {
1130 BUG_ON(num_ops <= req->r_num_ops);
1131 num_ops -= req->r_num_ops;
5b64640c
YZ
1132 locked_pages -= i;
1133
1134 /* allocate new pages array for next request */
1135 data_pages = pages;
6da2ec56
KC
1136 pages = kmalloc_array(locked_pages, sizeof(*pages),
1137 GFP_NOFS);
5b64640c 1138 if (!pages) {
a0102bda
JL
1139 from_pool = true;
1140 pages = mempool_alloc(ceph_wb_pagevec_pool, GFP_NOFS);
5b64640c
YZ
1141 BUG_ON(!pages);
1142 }
1143 memcpy(pages, data_pages + i,
1144 locked_pages * sizeof(*pages));
1145 memset(data_pages + i, 0,
1146 locked_pages * sizeof(*pages));
1147 } else {
1148 BUG_ON(num_ops != req->r_num_ops);
1149 index = pages[i - 1]->index + 1;
1150 /* request message now owns the pages array */
1151 pages = NULL;
1152 }
e5975c7c 1153
fac02ddf 1154 req->r_mtime = inode->i_mtime;
9d6fcb08
SW
1155 rc = ceph_osdc_start_request(&fsc->client->osdc, req, true);
1156 BUG_ON(rc);
1d3576fd
SW
1157 req = NULL;
1158
5b64640c
YZ
1159 wbc->nr_to_write -= i;
1160 if (pages)
1161 goto new_request;
1162
2a2d927e
YZ
1163 /*
1164 * We stop writing back only if we are not doing
1165 * integrity sync. In case of integrity sync we have to
1166 * keep going until we have written all the pages
1167 * we tagged for writeback prior to entering this loop.
1168 */
1169 if (wbc->nr_to_write <= 0 && wbc->sync_mode == WB_SYNC_NONE)
af9cc401 1170 done = true;
1d3576fd
SW
1171
1172release_pvec_pages:
1173 dout("pagevec_release on %d pages (%p)\n", (int)pvec.nr,
1174 pvec.nr ? pvec.pages[0] : NULL);
1175 pagevec_release(&pvec);
1d3576fd
SW
1176 }
1177
1178 if (should_loop && !done) {
1179 /* more to do; loop back to beginning of file */
1180 dout("writepages looping back to beginning of file\n");
2a2d927e 1181 end = start_index - 1; /* OK even when start_index == 0 */
f275635e
YZ
1182
1183 /* to write dirty pages associated with next snapc,
1184 * we need to wait until current writes complete */
1185 if (wbc->sync_mode != WB_SYNC_NONE &&
1186 start_index == 0 && /* all dirty pages were checked */
1187 !ceph_wbc.head_snapc) {
1188 struct page *page;
1189 unsigned i, nr;
1190 index = 0;
1191 while ((index <= end) &&
1192 (nr = pagevec_lookup_tag(&pvec, mapping, &index,
67fd707f 1193 PAGECACHE_TAG_WRITEBACK))) {
f275635e
YZ
1194 for (i = 0; i < nr; i++) {
1195 page = pvec.pages[i];
1196 if (page_snap_context(page) != snapc)
1197 continue;
1198 wait_on_page_writeback(page);
1199 }
1200 pagevec_release(&pvec);
1201 cond_resched();
1202 }
1203 }
1204
2a2d927e 1205 start_index = 0;
1d3576fd
SW
1206 index = 0;
1207 goto retry;
1208 }
1209
1210 if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0))
1211 mapping->writeback_index = index;
1212
1213out:
3ed97d63 1214 ceph_osdc_put_request(req);
2a2d927e
YZ
1215 ceph_put_snap_context(last_snapc);
1216 dout("writepages dend - startone, rc = %d\n", rc);
1d3576fd
SW
1217 return rc;
1218}
1219
1220
1221
1222/*
1223 * See if a given @snapc is either writeable, or already written.
1224 */
1225static int context_is_writeable_or_written(struct inode *inode,
1226 struct ceph_snap_context *snapc)
1227{
05455e11 1228 struct ceph_snap_context *oldest = get_oldest_context(inode, NULL, NULL);
6298a337
SW
1229 int ret = !oldest || snapc->seq <= oldest->seq;
1230
1231 ceph_put_snap_context(oldest);
1232 return ret;
1d3576fd
SW
1233}
1234
18d620f0
JL
1235/**
1236 * ceph_find_incompatible - find an incompatible context and return it
18d620f0 1237 * @page: page being dirtied
8f883c24 1238 *
18d620f0
JL
1239 * We are only allowed to write into/dirty a page if the page is
1240 * clean, or already dirty within the same snap context. Returns a
1241 * conflicting context if there is one, NULL if there isn't, or a
1242 * negative error code on other errors.
1243 *
1244 * Must be called with page lock held.
1d3576fd 1245 */
18d620f0 1246static struct ceph_snap_context *
d45156bf 1247ceph_find_incompatible(struct page *page)
1d3576fd 1248{
d45156bf 1249 struct inode *inode = page->mapping->host;
1d3576fd 1250 struct ceph_inode_info *ci = ceph_inode(inode);
1d3576fd 1251
5d6451b1
JL
1252 if (ceph_inode_is_shutdown(inode)) {
1253 dout(" page %p %llx:%llx is shutdown\n", page,
1254 ceph_vinop(inode));
1255 return ERR_PTR(-ESTALE);
6c93df5d
YZ
1256 }
1257
18d620f0
JL
1258 for (;;) {
1259 struct ceph_snap_context *snapc, *oldest;
1260
1261 wait_on_page_writeback(page);
1262
1263 snapc = page_snap_context(page);
1264 if (!snapc || snapc == ci->i_head_snapc)
1265 break;
1d3576fd 1266
1d3576fd
SW
1267 /*
1268 * this page is already dirty in another (older) snap
1269 * context! is it writeable now?
1270 */
05455e11 1271 oldest = get_oldest_context(inode, NULL, NULL);
80e755fe 1272 if (snapc->seq > oldest->seq) {
18d620f0 1273 /* not writeable -- return it for the caller to deal with */
6298a337 1274 ceph_put_snap_context(oldest);
18d620f0
JL
1275 dout(" page %p snapc %p not current or oldest\n", page, snapc);
1276 return ceph_get_snap_context(snapc);
1d3576fd 1277 }
6298a337 1278 ceph_put_snap_context(oldest);
1d3576fd
SW
1279
1280 /* yay, writeable, do it now (without dropping page lock) */
18d620f0
JL
1281 dout(" page %p snapc %p not current, but oldest\n", page, snapc);
1282 if (clear_page_dirty_for_io(page)) {
1283 int r = writepage_nounlock(page, NULL);
1284 if (r < 0)
1285 return ERR_PTR(r);
1286 }
1287 }
1288 return NULL;
1289}
1290
d801327d 1291static int ceph_netfs_check_write_begin(struct file *file, loff_t pos, unsigned int len,
78525c74 1292 struct folio *folio, void **_fsdata)
d801327d
JL
1293{
1294 struct inode *inode = file_inode(file);
1295 struct ceph_inode_info *ci = ceph_inode(inode);
1296 struct ceph_snap_context *snapc;
1297
78525c74 1298 snapc = ceph_find_incompatible(folio_page(folio, 0));
d801327d
JL
1299 if (snapc) {
1300 int r;
1301
78525c74
DH
1302 folio_unlock(folio);
1303 folio_put(folio);
d801327d
JL
1304 if (IS_ERR(snapc))
1305 return PTR_ERR(snapc);
1306
1307 ceph_queue_writeback(inode);
1308 r = wait_event_killable(ci->i_cap_wq,
1309 context_is_writeable_or_written(inode, snapc));
1310 ceph_put_snap_context(snapc);
1311 return r == 0 ? -EAGAIN : r;
1312 }
1313 return 0;
1314}
1315
18d620f0
JL
1316/*
1317 * We are only allowed to write into/dirty the page if the page is
1318 * clean, or already dirty within the same snap context.
18d620f0 1319 */
1cc16990 1320static int ceph_write_begin(struct file *file, struct address_space *mapping,
9d6b0cd7 1321 loff_t pos, unsigned len,
1cc16990 1322 struct page **pagep, void **fsdata)
18d620f0
JL
1323{
1324 struct inode *inode = file_inode(file);
78525c74 1325 struct folio *folio = NULL;
d801327d 1326 int r;
1d3576fd 1327
de2a9311 1328 r = netfs_write_begin(file, inode->i_mapping, pos, len, &folio, NULL);
d801327d 1329 if (r == 0)
78525c74 1330 folio_wait_fscache(folio);
1cc16990 1331 if (r < 0) {
78525c74
DH
1332 if (folio)
1333 folio_put(folio);
1cc16990 1334 } else {
78525c74
DH
1335 WARN_ON_ONCE(!folio_test_locked(folio));
1336 *pagep = &folio->page;
1cc16990 1337 }
4af6b225
YS
1338 return r;
1339}
1340
1d3576fd
SW
1341/*
1342 * we don't do anything in here that simple_write_end doesn't do
5dda377c 1343 * except adjust dirty page accounting
1d3576fd
SW
1344 */
1345static int ceph_write_end(struct file *file, struct address_space *mapping,
1346 loff_t pos, unsigned len, unsigned copied,
78525c74 1347 struct page *subpage, void *fsdata)
1d3576fd 1348{
78525c74 1349 struct folio *folio = page_folio(subpage);
496ad9aa 1350 struct inode *inode = file_inode(file);
efb0ca76 1351 bool check_cap = false;
1d3576fd 1352
78525c74
DH
1353 dout("write_end file %p inode %p folio %p %d~%d (%d)\n", file,
1354 inode, folio, (int)pos, (int)copied, (int)len);
1d3576fd 1355
78525c74 1356 if (!folio_test_uptodate(folio)) {
ce3a8732 1357 /* just return that nothing was copied on a short copy */
b9de313c
AV
1358 if (copied < len) {
1359 copied = 0;
1360 goto out;
1361 }
78525c74 1362 folio_mark_uptodate(folio);
b9de313c 1363 }
1d3576fd
SW
1364
1365 /* did file size increase? */
99c88e69 1366 if (pos+copied > i_size_read(inode))
1d3576fd
SW
1367 check_cap = ceph_inode_set_size(inode, pos+copied);
1368
78525c74 1369 folio_mark_dirty(folio);
1d3576fd 1370
b9de313c 1371out:
78525c74
DH
1372 folio_unlock(folio);
1373 folio_put(folio);
1d3576fd
SW
1374
1375 if (check_cap)
1376 ceph_check_caps(ceph_inode(inode), CHECK_CAPS_AUTHONLY, NULL);
1377
1378 return copied;
1379}
1380
1d3576fd 1381const struct address_space_operations ceph_aops = {
6c62371b 1382 .read_folio = netfs_read_folio,
bc899ee1 1383 .readahead = netfs_readahead,
1d3576fd
SW
1384 .writepage = ceph_writepage,
1385 .writepages = ceph_writepages_start,
1386 .write_begin = ceph_write_begin,
1387 .write_end = ceph_write_end,
8fb72b4a 1388 .dirty_folio = ceph_dirty_folio,
9872f4de 1389 .invalidate_folio = ceph_invalidate_folio,
5e414655 1390 .release_folio = ceph_release_folio,
9c43ff44 1391 .direct_IO = noop_direct_IO,
1d3576fd
SW
1392};
1393
4f7e89f6
YZ
1394static void ceph_block_sigs(sigset_t *oldset)
1395{
1396 sigset_t mask;
1397 siginitsetinv(&mask, sigmask(SIGKILL));
1398 sigprocmask(SIG_BLOCK, &mask, oldset);
1399}
1400
1401static void ceph_restore_sigs(sigset_t *oldset)
1402{
1403 sigprocmask(SIG_SETMASK, oldset, NULL);
1404}
1d3576fd
SW
1405
1406/*
1407 * vm ops
1408 */
24499847 1409static vm_fault_t ceph_filemap_fault(struct vm_fault *vmf)
61f68816 1410{
11bac800 1411 struct vm_area_struct *vma = vmf->vma;
61f68816
YZ
1412 struct inode *inode = file_inode(vma->vm_file);
1413 struct ceph_inode_info *ci = ceph_inode(inode);
1414 struct ceph_file_info *fi = vma->vm_file->private_data;
c403c3a2 1415 loff_t off = (loff_t)vmf->pgoff << PAGE_SHIFT;
24499847 1416 int want, got, err;
4f7e89f6 1417 sigset_t oldset;
24499847 1418 vm_fault_t ret = VM_FAULT_SIGBUS;
4f7e89f6 1419
5d6451b1
JL
1420 if (ceph_inode_is_shutdown(inode))
1421 return ret;
1422
4f7e89f6 1423 ceph_block_sigs(&oldset);
61f68816 1424
8ff2d290
JL
1425 dout("filemap_fault %p %llx.%llx %llu trying to get caps\n",
1426 inode, ceph_vinop(inode), off);
61f68816
YZ
1427 if (fi->fmode & CEPH_FILE_MODE_LAZY)
1428 want = CEPH_CAP_FILE_CACHE | CEPH_CAP_FILE_LAZYIO;
1429 else
1430 want = CEPH_CAP_FILE_CACHE;
4f7e89f6
YZ
1431
1432 got = 0;
e72968e1 1433 err = ceph_get_caps(vma->vm_file, CEPH_CAP_FILE_RD, want, -1, &got);
24499847 1434 if (err < 0)
4f7e89f6 1435 goto out_restore;
6ce026e4 1436
8ff2d290
JL
1437 dout("filemap_fault %p %llu got cap refs on %s\n",
1438 inode, off, ceph_cap_string(got));
61f68816 1439
83701246 1440 if ((got & (CEPH_CAP_FILE_CACHE | CEPH_CAP_FILE_LAZYIO)) ||
2b1ac852 1441 ci->i_inline_version == CEPH_INLINE_NONE) {
5d988308
YZ
1442 CEPH_DEFINE_RW_CONTEXT(rw_ctx, got);
1443 ceph_add_rw_context(fi, &rw_ctx);
11bac800 1444 ret = filemap_fault(vmf);
5d988308 1445 ceph_del_rw_context(fi, &rw_ctx);
8ff2d290
JL
1446 dout("filemap_fault %p %llu drop cap refs %s ret %x\n",
1447 inode, off, ceph_cap_string(got), ret);
2b1ac852 1448 } else
24499847 1449 err = -EAGAIN;
61f68816 1450
61f68816
YZ
1451 ceph_put_cap_refs(ci, got);
1452
24499847 1453 if (err != -EAGAIN)
4f7e89f6 1454 goto out_restore;
83701246
YZ
1455
1456 /* read inline data */
09cbfeaf 1457 if (off >= PAGE_SIZE) {
83701246
YZ
1458 /* does not support inline data > PAGE_SIZE */
1459 ret = VM_FAULT_SIGBUS;
1460 } else {
83701246 1461 struct address_space *mapping = inode->i_mapping;
057ba5b2
JK
1462 struct page *page;
1463
1464 filemap_invalidate_lock_shared(mapping);
1465 page = find_or_create_page(mapping, 0,
1466 mapping_gfp_constraint(mapping, ~__GFP_FS));
83701246
YZ
1467 if (!page) {
1468 ret = VM_FAULT_OOM;
4f7e89f6 1469 goto out_inline;
83701246 1470 }
24499847 1471 err = __ceph_do_getattr(inode, page,
83701246 1472 CEPH_STAT_CAP_INLINE_DATA, true);
24499847 1473 if (err < 0 || off >= i_size_read(inode)) {
83701246 1474 unlock_page(page);
09cbfeaf 1475 put_page(page);
c64a2b05 1476 ret = vmf_error(err);
4f7e89f6 1477 goto out_inline;
83701246 1478 }
24499847
SJ
1479 if (err < PAGE_SIZE)
1480 zero_user_segment(page, err, PAGE_SIZE);
83701246
YZ
1481 else
1482 flush_dcache_page(page);
1483 SetPageUptodate(page);
1484 vmf->page = page;
1485 ret = VM_FAULT_MAJOR | VM_FAULT_LOCKED;
4f7e89f6 1486out_inline:
057ba5b2 1487 filemap_invalidate_unlock_shared(mapping);
8ff2d290
JL
1488 dout("filemap_fault %p %llu read inline data ret %x\n",
1489 inode, off, ret);
83701246 1490 }
4f7e89f6
YZ
1491out_restore:
1492 ceph_restore_sigs(&oldset);
24499847
SJ
1493 if (err < 0)
1494 ret = vmf_error(err);
6ce026e4 1495
61f68816
YZ
1496 return ret;
1497}
1d3576fd 1498
24499847 1499static vm_fault_t ceph_page_mkwrite(struct vm_fault *vmf)
1d3576fd 1500{
11bac800 1501 struct vm_area_struct *vma = vmf->vma;
496ad9aa 1502 struct inode *inode = file_inode(vma->vm_file);
61f68816
YZ
1503 struct ceph_inode_info *ci = ceph_inode(inode);
1504 struct ceph_file_info *fi = vma->vm_file->private_data;
f66fd9f0 1505 struct ceph_cap_flush *prealloc_cf;
61f68816 1506 struct page *page = vmf->page;
6285bc23 1507 loff_t off = page_offset(page);
61f68816
YZ
1508 loff_t size = i_size_read(inode);
1509 size_t len;
24499847 1510 int want, got, err;
4f7e89f6 1511 sigset_t oldset;
24499847 1512 vm_fault_t ret = VM_FAULT_SIGBUS;
3ca9c3bd 1513
5d6451b1
JL
1514 if (ceph_inode_is_shutdown(inode))
1515 return ret;
1516
f66fd9f0
YZ
1517 prealloc_cf = ceph_alloc_cap_flush();
1518 if (!prealloc_cf)
6ce026e4 1519 return VM_FAULT_OOM;
f66fd9f0 1520
249c1df5 1521 sb_start_pagefault(inode->i_sb);
4f7e89f6 1522 ceph_block_sigs(&oldset);
f66fd9f0 1523
8ff2d290
JL
1524 if (off + thp_size(page) <= size)
1525 len = thp_size(page);
1d3576fd 1526 else
8ff2d290 1527 len = offset_in_thp(page, size);
1d3576fd 1528
61f68816
YZ
1529 dout("page_mkwrite %p %llx.%llx %llu~%zd getting caps i_size %llu\n",
1530 inode, ceph_vinop(inode), off, len, size);
1531 if (fi->fmode & CEPH_FILE_MODE_LAZY)
1532 want = CEPH_CAP_FILE_BUFFER | CEPH_CAP_FILE_LAZYIO;
1533 else
1534 want = CEPH_CAP_FILE_BUFFER;
4f7e89f6
YZ
1535
1536 got = 0;
e72968e1 1537 err = ceph_get_caps(vma->vm_file, CEPH_CAP_FILE_WR, want, off + len, &got);
24499847 1538 if (err < 0)
4f7e89f6 1539 goto out_free;
6ce026e4 1540
61f68816
YZ
1541 dout("page_mkwrite %p %llu~%zd got cap refs on %s\n",
1542 inode, off, len, ceph_cap_string(got));
1543
1544 /* Update time before taking page lock */
1545 file_update_time(vma->vm_file);
5c308356 1546 inode_inc_iversion_raw(inode);
4af6b225 1547
f0b33df5 1548 do {
d45156bf
JL
1549 struct ceph_snap_context *snapc;
1550
f0b33df5 1551 lock_page(page);
4af6b225 1552
cb03c143 1553 if (page_mkwrite_check_truncate(page, inode) < 0) {
f0b33df5
YZ
1554 unlock_page(page);
1555 ret = VM_FAULT_NOPAGE;
1556 break;
1557 }
1558
d45156bf
JL
1559 snapc = ceph_find_incompatible(page);
1560 if (!snapc) {
f0b33df5
YZ
1561 /* success. we'll keep the page locked. */
1562 set_page_dirty(page);
1563 ret = VM_FAULT_LOCKED;
d45156bf
JL
1564 break;
1565 }
1566
1567 unlock_page(page);
1568
1569 if (IS_ERR(snapc)) {
1570 ret = VM_FAULT_SIGBUS;
1571 break;
f0b33df5 1572 }
d45156bf
JL
1573
1574 ceph_queue_writeback(inode);
1575 err = wait_event_killable(ci->i_cap_wq,
1576 context_is_writeable_or_written(inode, snapc));
1577 ceph_put_snap_context(snapc);
1578 } while (err == 0);
4af6b225 1579
083db6fd 1580 if (ret == VM_FAULT_LOCKED) {
61f68816
YZ
1581 int dirty;
1582 spin_lock(&ci->i_ceph_lock);
f66fd9f0
YZ
1583 dirty = __ceph_mark_dirty_caps(ci, CEPH_CAP_FILE_WR,
1584 &prealloc_cf);
61f68816
YZ
1585 spin_unlock(&ci->i_ceph_lock);
1586 if (dirty)
1587 __mark_inode_dirty(inode, dirty);
1588 }
1589
24499847 1590 dout("page_mkwrite %p %llu~%zd dropping cap refs on %s ret %x\n",
61f68816 1591 inode, off, len, ceph_cap_string(got), ret);
a8810cdc 1592 ceph_put_cap_refs_async(ci, got);
f66fd9f0 1593out_free:
4f7e89f6 1594 ceph_restore_sigs(&oldset);
249c1df5 1595 sb_end_pagefault(inode->i_sb);
f66fd9f0 1596 ceph_free_cap_flush(prealloc_cf);
24499847
SJ
1597 if (err < 0)
1598 ret = vmf_error(err);
1d3576fd
SW
1599 return ret;
1600}
1601
31c542a1
YZ
1602void ceph_fill_inline_data(struct inode *inode, struct page *locked_page,
1603 char *data, size_t len)
1604{
1605 struct address_space *mapping = inode->i_mapping;
1606 struct page *page;
1607
1608 if (locked_page) {
1609 page = locked_page;
1610 } else {
1611 if (i_size_read(inode) == 0)
1612 return;
1613 page = find_or_create_page(mapping, 0,
c62d2555
MH
1614 mapping_gfp_constraint(mapping,
1615 ~__GFP_FS));
31c542a1
YZ
1616 if (!page)
1617 return;
1618 if (PageUptodate(page)) {
1619 unlock_page(page);
09cbfeaf 1620 put_page(page);
31c542a1
YZ
1621 return;
1622 }
1623 }
1624
0668ff52 1625 dout("fill_inline_data %p %llx.%llx len %zu locked_page %p\n",
31c542a1
YZ
1626 inode, ceph_vinop(inode), len, locked_page);
1627
1628 if (len > 0) {
1629 void *kaddr = kmap_atomic(page);
1630 memcpy(kaddr, data, len);
1631 kunmap_atomic(kaddr);
1632 }
1633
1634 if (page != locked_page) {
09cbfeaf
KS
1635 if (len < PAGE_SIZE)
1636 zero_user_segment(page, len, PAGE_SIZE);
31c542a1
YZ
1637 else
1638 flush_dcache_page(page);
1639
1640 SetPageUptodate(page);
1641 unlock_page(page);
09cbfeaf 1642 put_page(page);
31c542a1
YZ
1643 }
1644}
1645
083db6fd 1646int ceph_uninline_data(struct file *file)
28127bdd 1647{
083db6fd 1648 struct inode *inode = file_inode(file);
28127bdd
YZ
1649 struct ceph_inode_info *ci = ceph_inode(inode);
1650 struct ceph_fs_client *fsc = ceph_inode_to_client(inode);
825978fd 1651 struct ceph_osd_request *req = NULL;
083db6fd
DH
1652 struct ceph_cap_flush *prealloc_cf;
1653 struct folio *folio = NULL;
c38af982 1654 u64 inline_version = CEPH_INLINE_NONE;
083db6fd 1655 struct page *pages[1];
28127bdd 1656 int err = 0;
c38af982 1657 u64 len;
083db6fd 1658
825978fd
XL
1659 spin_lock(&ci->i_ceph_lock);
1660 inline_version = ci->i_inline_version;
1661 spin_unlock(&ci->i_ceph_lock);
1662
1663 dout("uninline_data %p %llx.%llx inline_version %llu\n",
1664 inode, ceph_vinop(inode), inline_version);
1665
1666 if (inline_version == CEPH_INLINE_NONE)
1667 return 0;
1668
083db6fd
DH
1669 prealloc_cf = ceph_alloc_cap_flush();
1670 if (!prealloc_cf)
1671 return -ENOMEM;
1672
825978fd
XL
1673 if (inline_version == 1) /* initial version, no data */
1674 goto out_uninline;
1675
083db6fd
DH
1676 folio = read_mapping_folio(inode->i_mapping, 0, file);
1677 if (IS_ERR(folio)) {
1678 err = PTR_ERR(folio);
1679 goto out;
1680 }
1681
1682 folio_lock(folio);
28127bdd 1683
083db6fd
DH
1684 len = i_size_read(inode);
1685 if (len > folio_size(folio))
1686 len = folio_size(folio);
28127bdd
YZ
1687
1688 req = ceph_osdc_new_request(&fsc->client->osdc, &ci->i_layout,
1689 ceph_vino(inode), 0, &len, 0, 1,
54ea0046 1690 CEPH_OSD_OP_CREATE, CEPH_OSD_FLAG_WRITE,
34b759b4 1691 NULL, 0, 0, false);
28127bdd
YZ
1692 if (IS_ERR(req)) {
1693 err = PTR_ERR(req);
083db6fd 1694 goto out_unlock;
28127bdd
YZ
1695 }
1696
fac02ddf 1697 req->r_mtime = inode->i_mtime;
28127bdd
YZ
1698 err = ceph_osdc_start_request(&fsc->client->osdc, req, false);
1699 if (!err)
1700 err = ceph_osdc_wait_request(&fsc->client->osdc, req);
1701 ceph_osdc_put_request(req);
1702 if (err < 0)
083db6fd 1703 goto out_unlock;
28127bdd
YZ
1704
1705 req = ceph_osdc_new_request(&fsc->client->osdc, &ci->i_layout,
1706 ceph_vino(inode), 0, &len, 1, 3,
54ea0046 1707 CEPH_OSD_OP_WRITE, CEPH_OSD_FLAG_WRITE,
34b759b4
ID
1708 NULL, ci->i_truncate_seq,
1709 ci->i_truncate_size, false);
28127bdd
YZ
1710 if (IS_ERR(req)) {
1711 err = PTR_ERR(req);
083db6fd 1712 goto out_unlock;
28127bdd
YZ
1713 }
1714
083db6fd
DH
1715 pages[0] = folio_page(folio, 0);
1716 osd_req_op_extent_osd_data_pages(req, 1, pages, len, 0, false, false);
28127bdd 1717
ec137c10
YZ
1718 {
1719 __le64 xattr_buf = cpu_to_le64(inline_version);
1720 err = osd_req_op_xattr_init(req, 0, CEPH_OSD_OP_CMPXATTR,
1721 "inline_version", &xattr_buf,
1722 sizeof(xattr_buf),
1723 CEPH_OSD_CMPXATTR_OP_GT,
1724 CEPH_OSD_CMPXATTR_MODE_U64);
1725 if (err)
083db6fd 1726 goto out_put_req;
ec137c10
YZ
1727 }
1728
1729 {
1730 char xattr_buf[32];
1731 int xattr_len = snprintf(xattr_buf, sizeof(xattr_buf),
1732 "%llu", inline_version);
1733 err = osd_req_op_xattr_init(req, 2, CEPH_OSD_OP_SETXATTR,
1734 "inline_version",
1735 xattr_buf, xattr_len, 0, 0);
1736 if (err)
083db6fd 1737 goto out_put_req;
ec137c10 1738 }
28127bdd 1739
fac02ddf 1740 req->r_mtime = inode->i_mtime;
28127bdd
YZ
1741 err = ceph_osdc_start_request(&fsc->client->osdc, req, false);
1742 if (!err)
1743 err = ceph_osdc_wait_request(&fsc->client->osdc, req);
97e27aaa 1744
8ae99ae2 1745 ceph_update_write_metrics(&fsc->mdsc->metric, req->r_start_latency,
903f4fec 1746 req->r_end_latency, len, err);
97e27aaa 1747
825978fd 1748out_uninline:
083db6fd
DH
1749 if (!err) {
1750 int dirty;
1751
1752 /* Set to CAP_INLINE_NONE and dirty the caps */
1753 down_read(&fsc->mdsc->snap_rwsem);
1754 spin_lock(&ci->i_ceph_lock);
1755 ci->i_inline_version = CEPH_INLINE_NONE;
1756 dirty = __ceph_mark_dirty_caps(ci, CEPH_CAP_FILE_WR, &prealloc_cf);
1757 spin_unlock(&ci->i_ceph_lock);
1758 up_read(&fsc->mdsc->snap_rwsem);
1759 if (dirty)
1760 __mark_inode_dirty(inode, dirty);
1761 }
1762out_put_req:
28127bdd
YZ
1763 ceph_osdc_put_request(req);
1764 if (err == -ECANCELED)
1765 err = 0;
083db6fd 1766out_unlock:
825978fd
XL
1767 if (folio) {
1768 folio_unlock(folio);
1769 folio_put(folio);
1770 }
28127bdd 1771out:
083db6fd 1772 ceph_free_cap_flush(prealloc_cf);
28127bdd
YZ
1773 dout("uninline_data %p %llx.%llx inline_version %llu = %d\n",
1774 inode, ceph_vinop(inode), inline_version, err);
1775 return err;
1776}
1777
7cbea8dc 1778static const struct vm_operations_struct ceph_vmops = {
61f68816 1779 .fault = ceph_filemap_fault,
1d3576fd
SW
1780 .page_mkwrite = ceph_page_mkwrite,
1781};
1782
1783int ceph_mmap(struct file *file, struct vm_area_struct *vma)
1784{
1785 struct address_space *mapping = file->f_mapping;
1786
7e0a1265 1787 if (!mapping->a_ops->read_folio)
1d3576fd 1788 return -ENOEXEC;
1d3576fd 1789 vma->vm_ops = &ceph_vmops;
1d3576fd
SW
1790 return 0;
1791}
10183a69
YZ
1792
1793enum {
1794 POOL_READ = 1,
1795 POOL_WRITE = 2,
1796};
1797
779fe0fb
YZ
1798static int __ceph_pool_perm_get(struct ceph_inode_info *ci,
1799 s64 pool, struct ceph_string *pool_ns)
10183a69
YZ
1800{
1801 struct ceph_fs_client *fsc = ceph_inode_to_client(&ci->vfs_inode);
1802 struct ceph_mds_client *mdsc = fsc->mdsc;
1803 struct ceph_osd_request *rd_req = NULL, *wr_req = NULL;
1804 struct rb_node **p, *parent;
1805 struct ceph_pool_perm *perm;
1806 struct page **pages;
779fe0fb 1807 size_t pool_ns_len;
10183a69
YZ
1808 int err = 0, err2 = 0, have = 0;
1809
1810 down_read(&mdsc->pool_perm_rwsem);
1811 p = &mdsc->pool_perm_tree.rb_node;
1812 while (*p) {
1813 perm = rb_entry(*p, struct ceph_pool_perm, node);
1814 if (pool < perm->pool)
1815 p = &(*p)->rb_left;
1816 else if (pool > perm->pool)
1817 p = &(*p)->rb_right;
1818 else {
779fe0fb
YZ
1819 int ret = ceph_compare_string(pool_ns,
1820 perm->pool_ns,
1821 perm->pool_ns_len);
1822 if (ret < 0)
1823 p = &(*p)->rb_left;
1824 else if (ret > 0)
1825 p = &(*p)->rb_right;
1826 else {
1827 have = perm->perm;
1828 break;
1829 }
10183a69
YZ
1830 }
1831 }
1832 up_read(&mdsc->pool_perm_rwsem);
1833 if (*p)
1834 goto out;
1835
779fe0fb
YZ
1836 if (pool_ns)
1837 dout("__ceph_pool_perm_get pool %lld ns %.*s no perm cached\n",
1838 pool, (int)pool_ns->len, pool_ns->str);
1839 else
1840 dout("__ceph_pool_perm_get pool %lld no perm cached\n", pool);
10183a69
YZ
1841
1842 down_write(&mdsc->pool_perm_rwsem);
779fe0fb 1843 p = &mdsc->pool_perm_tree.rb_node;
10183a69
YZ
1844 parent = NULL;
1845 while (*p) {
1846 parent = *p;
1847 perm = rb_entry(parent, struct ceph_pool_perm, node);
1848 if (pool < perm->pool)
1849 p = &(*p)->rb_left;
1850 else if (pool > perm->pool)
1851 p = &(*p)->rb_right;
1852 else {
779fe0fb
YZ
1853 int ret = ceph_compare_string(pool_ns,
1854 perm->pool_ns,
1855 perm->pool_ns_len);
1856 if (ret < 0)
1857 p = &(*p)->rb_left;
1858 else if (ret > 0)
1859 p = &(*p)->rb_right;
1860 else {
1861 have = perm->perm;
1862 break;
1863 }
10183a69
YZ
1864 }
1865 }
1866 if (*p) {
1867 up_write(&mdsc->pool_perm_rwsem);
1868 goto out;
1869 }
1870
34b759b4 1871 rd_req = ceph_osdc_alloc_request(&fsc->client->osdc, NULL,
10183a69
YZ
1872 1, false, GFP_NOFS);
1873 if (!rd_req) {
1874 err = -ENOMEM;
1875 goto out_unlock;
1876 }
1877
1878 rd_req->r_flags = CEPH_OSD_FLAG_READ;
1879 osd_req_op_init(rd_req, 0, CEPH_OSD_OP_STAT, 0);
1880 rd_req->r_base_oloc.pool = pool;
779fe0fb
YZ
1881 if (pool_ns)
1882 rd_req->r_base_oloc.pool_ns = ceph_get_string(pool_ns);
d30291b9 1883 ceph_oid_printf(&rd_req->r_base_oid, "%llx.00000000", ci->i_vino.ino);
10183a69 1884
13d1ad16
ID
1885 err = ceph_osdc_alloc_messages(rd_req, GFP_NOFS);
1886 if (err)
1887 goto out_unlock;
10183a69 1888
34b759b4 1889 wr_req = ceph_osdc_alloc_request(&fsc->client->osdc, NULL,
10183a69
YZ
1890 1, false, GFP_NOFS);
1891 if (!wr_req) {
1892 err = -ENOMEM;
1893 goto out_unlock;
1894 }
1895
54ea0046 1896 wr_req->r_flags = CEPH_OSD_FLAG_WRITE;
10183a69 1897 osd_req_op_init(wr_req, 0, CEPH_OSD_OP_CREATE, CEPH_OSD_OP_FLAG_EXCL);
63244fa1 1898 ceph_oloc_copy(&wr_req->r_base_oloc, &rd_req->r_base_oloc);
d30291b9 1899 ceph_oid_copy(&wr_req->r_base_oid, &rd_req->r_base_oid);
10183a69 1900
13d1ad16
ID
1901 err = ceph_osdc_alloc_messages(wr_req, GFP_NOFS);
1902 if (err)
1903 goto out_unlock;
10183a69
YZ
1904
1905 /* one page should be large enough for STAT data */
1906 pages = ceph_alloc_page_vector(1, GFP_KERNEL);
1907 if (IS_ERR(pages)) {
1908 err = PTR_ERR(pages);
1909 goto out_unlock;
1910 }
1911
1912 osd_req_op_raw_data_in_pages(rd_req, 0, pages, PAGE_SIZE,
1913 0, false, true);
10183a69
YZ
1914 err = ceph_osdc_start_request(&fsc->client->osdc, rd_req, false);
1915
fac02ddf 1916 wr_req->r_mtime = ci->vfs_inode.i_mtime;
10183a69
YZ
1917 err2 = ceph_osdc_start_request(&fsc->client->osdc, wr_req, false);
1918
1919 if (!err)
1920 err = ceph_osdc_wait_request(&fsc->client->osdc, rd_req);
1921 if (!err2)
1922 err2 = ceph_osdc_wait_request(&fsc->client->osdc, wr_req);
1923
1924 if (err >= 0 || err == -ENOENT)
1925 have |= POOL_READ;
131d7eb4 1926 else if (err != -EPERM) {
0b98acd6
ID
1927 if (err == -EBLOCKLISTED)
1928 fsc->blocklisted = true;
10183a69 1929 goto out_unlock;
131d7eb4 1930 }
10183a69
YZ
1931
1932 if (err2 == 0 || err2 == -EEXIST)
1933 have |= POOL_WRITE;
1934 else if (err2 != -EPERM) {
0b98acd6
ID
1935 if (err2 == -EBLOCKLISTED)
1936 fsc->blocklisted = true;
10183a69
YZ
1937 err = err2;
1938 goto out_unlock;
1939 }
1940
779fe0fb
YZ
1941 pool_ns_len = pool_ns ? pool_ns->len : 0;
1942 perm = kmalloc(sizeof(*perm) + pool_ns_len + 1, GFP_NOFS);
10183a69
YZ
1943 if (!perm) {
1944 err = -ENOMEM;
1945 goto out_unlock;
1946 }
1947
1948 perm->pool = pool;
1949 perm->perm = have;
779fe0fb
YZ
1950 perm->pool_ns_len = pool_ns_len;
1951 if (pool_ns_len > 0)
1952 memcpy(perm->pool_ns, pool_ns->str, pool_ns_len);
1953 perm->pool_ns[pool_ns_len] = 0;
1954
10183a69
YZ
1955 rb_link_node(&perm->node, parent, p);
1956 rb_insert_color(&perm->node, &mdsc->pool_perm_tree);
1957 err = 0;
1958out_unlock:
1959 up_write(&mdsc->pool_perm_rwsem);
1960
3ed97d63
ID
1961 ceph_osdc_put_request(rd_req);
1962 ceph_osdc_put_request(wr_req);
10183a69
YZ
1963out:
1964 if (!err)
1965 err = have;
779fe0fb
YZ
1966 if (pool_ns)
1967 dout("__ceph_pool_perm_get pool %lld ns %.*s result = %d\n",
1968 pool, (int)pool_ns->len, pool_ns->str, err);
1969 else
1970 dout("__ceph_pool_perm_get pool %lld result = %d\n", pool, err);
10183a69
YZ
1971 return err;
1972}
1973
5e3ded1b 1974int ceph_pool_perm_check(struct inode *inode, int need)
10183a69 1975{
5e3ded1b 1976 struct ceph_inode_info *ci = ceph_inode(inode);
779fe0fb 1977 struct ceph_string *pool_ns;
5e3ded1b 1978 s64 pool;
10183a69
YZ
1979 int ret, flags;
1980
e9b22501
JL
1981 /* Only need to do this for regular files */
1982 if (!S_ISREG(inode->i_mode))
1983 return 0;
1984
80e80fbb
YZ
1985 if (ci->i_vino.snap != CEPH_NOSNAP) {
1986 /*
1987 * Pool permission check needs to write to the first object.
1988 * But for snapshot, head of the first object may have alread
1989 * been deleted. Skip check to avoid creating orphan object.
1990 */
1991 return 0;
1992 }
1993
5e3ded1b 1994 if (ceph_test_mount_opt(ceph_inode_to_client(inode),
10183a69
YZ
1995 NOPOOLPERM))
1996 return 0;
1997
1998 spin_lock(&ci->i_ceph_lock);
1999 flags = ci->i_ceph_flags;
7627151e 2000 pool = ci->i_layout.pool_id;
10183a69
YZ
2001 spin_unlock(&ci->i_ceph_lock);
2002check:
2003 if (flags & CEPH_I_POOL_PERM) {
2004 if ((need & CEPH_CAP_FILE_RD) && !(flags & CEPH_I_POOL_RD)) {
7627151e 2005 dout("ceph_pool_perm_check pool %lld no read perm\n",
10183a69
YZ
2006 pool);
2007 return -EPERM;
2008 }
2009 if ((need & CEPH_CAP_FILE_WR) && !(flags & CEPH_I_POOL_WR)) {
7627151e 2010 dout("ceph_pool_perm_check pool %lld no write perm\n",
10183a69
YZ
2011 pool);
2012 return -EPERM;
2013 }
2014 return 0;
2015 }
2016
779fe0fb
YZ
2017 pool_ns = ceph_try_get_string(ci->i_layout.pool_ns);
2018 ret = __ceph_pool_perm_get(ci, pool, pool_ns);
2019 ceph_put_string(pool_ns);
10183a69
YZ
2020 if (ret < 0)
2021 return ret;
2022
2023 flags = CEPH_I_POOL_PERM;
2024 if (ret & POOL_READ)
2025 flags |= CEPH_I_POOL_RD;
2026 if (ret & POOL_WRITE)
2027 flags |= CEPH_I_POOL_WR;
2028
2029 spin_lock(&ci->i_ceph_lock);
779fe0fb
YZ
2030 if (pool == ci->i_layout.pool_id &&
2031 pool_ns == rcu_dereference_raw(ci->i_layout.pool_ns)) {
2032 ci->i_ceph_flags |= flags;
10183a69 2033 } else {
7627151e 2034 pool = ci->i_layout.pool_id;
10183a69
YZ
2035 flags = ci->i_ceph_flags;
2036 }
2037 spin_unlock(&ci->i_ceph_lock);
2038 goto check;
2039}
2040
2041void ceph_pool_perm_destroy(struct ceph_mds_client *mdsc)
2042{
2043 struct ceph_pool_perm *perm;
2044 struct rb_node *n;
2045
2046 while (!RB_EMPTY_ROOT(&mdsc->pool_perm_tree)) {
2047 n = rb_first(&mdsc->pool_perm_tree);
2048 perm = rb_entry(n, struct ceph_pool_perm, node);
2049 rb_erase(n, &mdsc->pool_perm_tree);
2050 kfree(perm);
2051 }
2052}