1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/ceph/ceph_debug.h>
3
4 #include <linux/backing-dev.h>
5 #include <linux/fs.h>
6 #include <linux/mm.h>
7 #include <linux/swap.h>
8 #include <linux/pagemap.h>
9 #include <linux/slab.h>
10 #include <linux/folio_batch.h>
11 #include <linux/task_io_accounting_ops.h>
12 #include <linux/signal.h>
13 #include <linux/iversion.h>
14 #include <linux/ktime.h>
15 #include <linux/netfs.h>
16 #include <trace/events/netfs.h>
17
18 #include "super.h"
19 #include "mds_client.h"
20 #include "cache.h"
21 #include "metric.h"
22 #include "subvolume_metrics.h"
23 #include "crypto.h"
24 #include <linux/ceph/osd_client.h>
25 #include <linux/ceph/striper.h>
26
27 /*
28 * Ceph address space ops.
29 *
30 * There are a few funny things going on here.
31 *
32 * The page->private field is used to reference a struct
33 * ceph_snap_context for _every_ dirty page. This indicates which
34 * snapshot the page was logically dirtied in, and thus which snap
35 * context needs to be associated with the osd write during writeback.
36 *
37 * Similarly, struct ceph_inode_info maintains a set of counters to
38 * count dirty pages on the inode. In the absence of snapshots,
39 * i_wrbuffer_ref == i_wrbuffer_ref_head == the dirty page count.
40 *
41 * When a snapshot is taken (that is, when the client receives
42 * notification that a snapshot was taken), each inode with caps and
43 * with dirty pages (dirty pages implies there is a cap) gets a new
44 * ceph_cap_snap in the i_cap_snaps list (which is sorted in ascending
45 * order, new snaps go to the tail). The i_wrbuffer_ref_head count is
46 * moved to capsnap->dirty. (Unless a sync write is currently in
47 * progress. In that case, the capsnap is said to be "pending", new
48 * writes cannot start, and the capsnap isn't "finalized" until the
49 * write completes (or fails) and a final size/mtime for the inode for
50 * that snap can be settled upon.) i_wrbuffer_ref_head is reset to 0.
51 *
52 * On writeback, we must submit writes to the osd IN SNAP ORDER. So,
53 * we look for the first capsnap in i_cap_snaps and write out pages in
54 * that snap context _only_. Then we move on to the next capsnap,
55 * eventually reaching the "live" or "head" context (i.e., pages that
56 * are not yet snapped) and are writing the most recently dirtied
57 * pages.
58 *
59 * Invalidate and so forth must take care to ensure the dirty page
60 * accounting is preserved.
61 */
62
63 #define CONGESTION_ON_THRESH(congestion_kb) (congestion_kb >> (PAGE_SHIFT-10))
64 #define CONGESTION_OFF_THRESH(congestion_kb) \
65 (CONGESTION_ON_THRESH(congestion_kb) - \
66 (CONGESTION_ON_THRESH(congestion_kb) >> 2))
67
68 static int ceph_netfs_check_write_begin(struct file *file, loff_t pos, unsigned int len,
69 struct folio **foliop, void **_fsdata);
70
page_snap_context(struct page * page)71 static inline struct ceph_snap_context *page_snap_context(struct page *page)
72 {
73 if (PagePrivate(page))
74 return (void *)page->private;
75 return NULL;
76 }
77
78 /*
79 * Dirty a page. Optimistically adjust accounting, on the assumption
80 * that we won't race with invalidate. If we do, readjust.
81 */
ceph_dirty_folio(struct address_space * mapping,struct folio * folio)82 static bool ceph_dirty_folio(struct address_space *mapping, struct folio *folio)
83 {
84 struct inode *inode = mapping->host;
85 struct ceph_client *cl = ceph_inode_to_client(inode);
86 struct ceph_mds_client *mdsc = ceph_sb_to_mdsc(inode->i_sb);
87 struct ceph_inode_info *ci;
88 struct ceph_snap_context *snapc;
89
90 if (folio_test_dirty(folio)) {
91 doutc(cl, "%llx.%llx %p idx %lu -- already dirty\n",
92 ceph_vinop(inode), folio, folio->index);
93 VM_BUG_ON_FOLIO(!folio_test_private(folio), folio);
94 return false;
95 }
96
97 atomic64_inc(&mdsc->dirty_folios);
98
99 ci = ceph_inode(inode);
100
101 /* dirty the head */
102 spin_lock(&ci->i_ceph_lock);
103 if (__ceph_have_pending_cap_snap(ci)) {
104 struct ceph_cap_snap *capsnap =
105 list_last_entry(&ci->i_cap_snaps,
106 struct ceph_cap_snap,
107 ci_item);
108 snapc = ceph_get_snap_context(capsnap->context);
109 capsnap->dirty_pages++;
110 } else {
111 BUG_ON(!ci->i_head_snapc);
112 snapc = ceph_get_snap_context(ci->i_head_snapc);
113 ++ci->i_wrbuffer_ref_head;
114 }
115 if (ci->i_wrbuffer_ref == 0)
116 ihold(inode);
117 ++ci->i_wrbuffer_ref;
118 doutc(cl, "%llx.%llx %p idx %lu head %d/%d -> %d/%d "
119 "snapc %p seq %lld (%d snaps)\n",
120 ceph_vinop(inode), folio, folio->index,
121 ci->i_wrbuffer_ref-1, ci->i_wrbuffer_ref_head-1,
122 ci->i_wrbuffer_ref, ci->i_wrbuffer_ref_head,
123 snapc, snapc->seq, snapc->num_snaps);
124 spin_unlock(&ci->i_ceph_lock);
125
126 /*
127 * Reference snap context in folio->private. Also set
128 * PagePrivate so that we get invalidate_folio callback.
129 */
130 VM_WARN_ON_FOLIO(folio->private, folio);
131 folio_attach_private(folio, snapc);
132
133 return ceph_fscache_dirty_folio(mapping, folio);
134 }
135
136 /*
137 * If we are truncating the full folio (i.e. offset == 0), adjust the
138 * dirty folio counters appropriately. Only called if there is private
139 * data on the folio.
140 */
ceph_invalidate_folio(struct folio * folio,size_t offset,size_t length)141 static void ceph_invalidate_folio(struct folio *folio, size_t offset,
142 size_t length)
143 {
144 struct inode *inode = folio->mapping->host;
145 struct ceph_client *cl = ceph_inode_to_client(inode);
146 struct ceph_inode_info *ci = ceph_inode(inode);
147 struct ceph_snap_context *snapc;
148
149
150 if (offset != 0 || length != folio_size(folio)) {
151 doutc(cl, "%llx.%llx idx %lu partial dirty page %zu~%zu\n",
152 ceph_vinop(inode), folio->index, offset, length);
153 return;
154 }
155
156 WARN_ON(!folio_test_locked(folio));
157 if (folio_test_private(folio)) {
158 doutc(cl, "%llx.%llx idx %lu full dirty page\n",
159 ceph_vinop(inode), folio->index);
160
161 snapc = folio_detach_private(folio);
162 ceph_put_wrbuffer_cap_refs(ci, 1, snapc);
163 ceph_put_snap_context(snapc);
164 }
165
166 netfs_invalidate_folio(folio, offset, length);
167 }
168
ceph_netfs_expand_readahead(struct netfs_io_request * rreq)169 static void ceph_netfs_expand_readahead(struct netfs_io_request *rreq)
170 {
171 struct inode *inode = rreq->inode;
172 struct ceph_inode_info *ci = ceph_inode(inode);
173 struct ceph_file_layout *lo = &ci->i_layout;
174 unsigned long max_pages = inode->i_sb->s_bdi->ra_pages;
175 loff_t end = rreq->start + rreq->len, new_end;
176 struct ceph_netfs_request_data *priv = rreq->netfs_priv;
177 unsigned long max_len;
178 u32 blockoff;
179
180 if (priv) {
181 /* Readahead is disabled by posix_fadvise POSIX_FADV_RANDOM */
182 if (priv->file_ra_disabled)
183 max_pages = 0;
184 else
185 max_pages = priv->file_ra_pages;
186
187 }
188
189 /* Readahead is disabled */
190 if (!max_pages)
191 return;
192
193 max_len = max_pages << PAGE_SHIFT;
194
195 /*
196 * Try to expand the length forward by rounding up it to the next
197 * block, but do not exceed the file size, unless the original
198 * request already exceeds it.
199 */
200 new_end = umin(round_up(end, lo->stripe_unit), rreq->i_size);
201 if (new_end > end && new_end <= rreq->start + max_len)
202 rreq->len = new_end - rreq->start;
203
204 /* Try to expand the start downward */
205 div_u64_rem(rreq->start, lo->stripe_unit, &blockoff);
206 if (rreq->len + blockoff <= max_len) {
207 rreq->start -= blockoff;
208 rreq->len += blockoff;
209 }
210 }
211
finish_netfs_read(struct ceph_osd_request * req)212 static void finish_netfs_read(struct ceph_osd_request *req)
213 {
214 struct inode *inode = req->r_inode;
215 struct ceph_fs_client *fsc = ceph_inode_to_fs_client(inode);
216 struct ceph_client *cl = fsc->client;
217 struct ceph_osd_data *osd_data = osd_req_op_extent_osd_data(req, 0);
218 struct netfs_io_subrequest *subreq = req->r_priv;
219 struct ceph_osd_req_op *op = &req->r_ops[0];
220 int err = req->r_result;
221 bool sparse = (op->op == CEPH_OSD_OP_SPARSE_READ);
222
223 ceph_update_read_metrics(&fsc->mdsc->metric, req->r_start_latency,
224 req->r_end_latency, osd_data->length, err);
225
226 doutc(cl, "result %d subreq->len=%zu i_size=%lld\n", req->r_result,
227 subreq->len, i_size_read(req->r_inode));
228
229 /* no object means success but no data */
230 if (err == -ENOENT) {
231 __set_bit(NETFS_SREQ_CLEAR_TAIL, &subreq->flags);
232 __set_bit(NETFS_SREQ_MADE_PROGRESS, &subreq->flags);
233 err = 0;
234 } else if (err == -EBLOCKLISTED) {
235 fsc->blocklisted = true;
236 }
237
238 if (err >= 0) {
239 if (sparse && err > 0)
240 err = ceph_sparse_ext_map_end(op);
241 if (err < subreq->len &&
242 subreq->rreq->origin != NETFS_UNBUFFERED_READ &&
243 subreq->rreq->origin != NETFS_DIO_READ)
244 __set_bit(NETFS_SREQ_CLEAR_TAIL, &subreq->flags);
245 if (IS_ENCRYPTED(inode) && err > 0) {
246 err = ceph_fscrypt_decrypt_extents(inode,
247 osd_data->pages, subreq->start,
248 op->extent.sparse_ext,
249 op->extent.sparse_ext_cnt);
250 if (err > subreq->len)
251 err = subreq->len;
252 }
253 if (err > 0)
254 __set_bit(NETFS_SREQ_CLEAR_TAIL, &subreq->flags);
255 }
256
257 if (osd_data->type == CEPH_OSD_DATA_TYPE_PAGES) {
258 ceph_put_page_vector(osd_data->pages,
259 calc_pages_for(osd_data->alignment,
260 osd_data->length), false);
261 }
262 if (err > 0) {
263 ceph_subvolume_metrics_record_io(fsc->mdsc, ceph_inode(inode),
264 false, err,
265 req->r_start_latency,
266 req->r_end_latency);
267 subreq->transferred = err;
268 err = 0;
269 }
270 subreq->error = err;
271 trace_netfs_sreq(subreq, netfs_sreq_trace_io_progress);
272 netfs_read_subreq_terminated(subreq);
273 iput(req->r_inode);
274 ceph_dec_osd_stopping_blocker(fsc->mdsc);
275 }
276
ceph_netfs_issue_op_inline(struct netfs_io_subrequest * subreq)277 static bool ceph_netfs_issue_op_inline(struct netfs_io_subrequest *subreq)
278 {
279 struct netfs_io_request *rreq = subreq->rreq;
280 struct inode *inode = rreq->inode;
281 struct ceph_mds_reply_info_parsed *rinfo;
282 struct ceph_mds_reply_info_in *iinfo;
283 struct ceph_mds_request *req;
284 struct ceph_mds_client *mdsc = ceph_sb_to_mdsc(inode->i_sb);
285 struct ceph_inode_info *ci = ceph_inode(inode);
286 ssize_t err = 0;
287 size_t len;
288 int mode;
289
290 if (rreq->origin != NETFS_UNBUFFERED_READ &&
291 rreq->origin != NETFS_DIO_READ)
292 __set_bit(NETFS_SREQ_CLEAR_TAIL, &subreq->flags);
293 __clear_bit(NETFS_SREQ_COPY_TO_CACHE, &subreq->flags);
294
295 if (subreq->start >= inode->i_size)
296 goto out;
297
298 /* We need to fetch the inline data. */
299 mode = ceph_try_to_choose_auth_mds(inode, CEPH_STAT_CAP_INLINE_DATA);
300 req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_GETATTR, mode);
301 if (IS_ERR(req)) {
302 err = PTR_ERR(req);
303 goto out;
304 }
305 req->r_ino1 = ci->i_vino;
306 req->r_args.getattr.mask = cpu_to_le32(CEPH_STAT_CAP_INLINE_DATA);
307 req->r_num_caps = 2;
308
309 trace_netfs_sreq(subreq, netfs_sreq_trace_submit);
310 err = ceph_mdsc_do_request(mdsc, NULL, req);
311 if (err < 0)
312 goto out;
313
314 rinfo = &req->r_reply_info;
315 iinfo = &rinfo->targeti;
316 if (iinfo->inline_version == CEPH_INLINE_NONE) {
317 /* The data got uninlined */
318 ceph_mdsc_put_request(req);
319 return false;
320 }
321
322 len = min_t(size_t, iinfo->inline_len - subreq->start, subreq->len);
323 err = copy_to_iter(iinfo->inline_data + subreq->start, len, &subreq->io_iter);
324 if (err == 0) {
325 err = -EFAULT;
326 } else {
327 subreq->transferred += err;
328 err = 0;
329 }
330
331 ceph_mdsc_put_request(req);
332 out:
333 subreq->error = err;
334 trace_netfs_sreq(subreq, netfs_sreq_trace_io_progress);
335 netfs_read_subreq_terminated(subreq);
336 return true;
337 }
338
ceph_netfs_prepare_read(struct netfs_io_subrequest * subreq)339 static int ceph_netfs_prepare_read(struct netfs_io_subrequest *subreq)
340 {
341 struct netfs_io_request *rreq = subreq->rreq;
342 struct inode *inode = rreq->inode;
343 struct ceph_inode_info *ci = ceph_inode(inode);
344 struct ceph_fs_client *fsc = ceph_inode_to_fs_client(inode);
345 u64 objno, objoff;
346 u32 xlen;
347
348 /* Truncate the extent at the end of the current block */
349 ceph_calc_file_object_mapping(&ci->i_layout, subreq->start, subreq->len,
350 &objno, &objoff, &xlen);
351 rreq->io_streams[0].sreq_max_len = umin(xlen, fsc->mount_options->rsize);
352 return 0;
353 }
354
ceph_netfs_issue_read(struct netfs_io_subrequest * subreq)355 static void ceph_netfs_issue_read(struct netfs_io_subrequest *subreq)
356 {
357 struct netfs_io_request *rreq = subreq->rreq;
358 struct inode *inode = rreq->inode;
359 struct ceph_inode_info *ci = ceph_inode(inode);
360 struct ceph_fs_client *fsc = ceph_inode_to_fs_client(inode);
361 struct ceph_client *cl = fsc->client;
362 struct ceph_osd_request *req = NULL;
363 struct ceph_vino vino = ceph_vino(inode);
364 int err;
365 u64 len;
366 bool sparse = IS_ENCRYPTED(inode) || ceph_test_mount_opt(fsc, SPARSEREAD);
367 u64 off = subreq->start;
368 int extent_cnt;
369
370 if (ceph_inode_is_shutdown(inode)) {
371 err = -EIO;
372 goto out;
373 }
374
375 if (ceph_has_inline_data(ci) && ceph_netfs_issue_op_inline(subreq))
376 return;
377
378 // TODO: This rounding here is slightly dodgy. It *should* work, for
379 // now, as the cache only deals in blocks that are a multiple of
380 // PAGE_SIZE and fscrypt blocks are at most PAGE_SIZE. What needs to
381 // happen is for the fscrypt driving to be moved into netfslib and the
382 // data in the cache also to be stored encrypted.
383 len = subreq->len;
384 ceph_fscrypt_adjust_off_and_len(inode, &off, &len);
385
386 req = ceph_osdc_new_request(&fsc->client->osdc, &ci->i_layout, vino,
387 off, &len, 0, 1, sparse ? CEPH_OSD_OP_SPARSE_READ : CEPH_OSD_OP_READ,
388 CEPH_OSD_FLAG_READ, NULL, ci->i_truncate_seq,
389 ci->i_truncate_size, false);
390 if (IS_ERR(req)) {
391 err = PTR_ERR(req);
392 req = NULL;
393 goto out;
394 }
395
396 if (sparse) {
397 extent_cnt = __ceph_sparse_read_ext_count(inode, len);
398 err = ceph_alloc_sparse_ext_map(&req->r_ops[0], extent_cnt);
399 if (err)
400 goto out;
401 }
402
403 doutc(cl, "%llx.%llx pos=%llu orig_len=%zu len=%llu\n",
404 ceph_vinop(inode), subreq->start, subreq->len, len);
405
406 /*
407 * FIXME: For now, use CEPH_OSD_DATA_TYPE_PAGES instead of _ITER for
408 * encrypted inodes. We'd need infrastructure that handles an iov_iter
409 * instead of page arrays, and we don't have that as of yet. Once the
410 * dust settles on the write helpers and encrypt/decrypt routines for
411 * netfs, we should be able to rework this.
412 */
413 if (IS_ENCRYPTED(inode)) {
414 struct page **pages;
415 size_t page_off;
416
417 /*
418 * FIXME: io_iter.count needs to be corrected to aligned
419 * length. Otherwise, iov_iter_get_pages_alloc2() operates
420 * with the initial unaligned length value. As a result,
421 * ceph_msg_data_cursor_init() triggers BUG_ON() in the case
422 * if msg->sparse_read_total > msg->data_length.
423 */
424 subreq->io_iter.count = len;
425
426 err = iov_iter_get_pages_alloc2(&subreq->io_iter, &pages, len, &page_off);
427 if (err < 0) {
428 doutc(cl, "%llx.%llx failed to allocate pages, %d\n",
429 ceph_vinop(inode), err);
430 goto out;
431 }
432
433 /* should always give us a page-aligned read */
434 WARN_ON_ONCE(page_off);
435 len = err;
436 err = 0;
437
438 osd_req_op_extent_osd_data_pages(req, 0, pages, len, 0, false,
439 false);
440 } else {
441 osd_req_op_extent_osd_iter(req, 0, &subreq->io_iter);
442 }
443 if (!ceph_inc_osd_stopping_blocker(fsc->mdsc)) {
444 err = -EIO;
445 goto out;
446 }
447 req->r_callback = finish_netfs_read;
448 req->r_priv = subreq;
449 req->r_inode = inode;
450 ihold(inode);
451
452 trace_netfs_sreq(subreq, netfs_sreq_trace_submit);
453 ceph_osdc_start_request(req->r_osdc, req);
454 out:
455 ceph_osdc_put_request(req);
456 if (err) {
457 subreq->error = err;
458 netfs_read_subreq_terminated(subreq);
459 }
460 doutc(cl, "%llx.%llx result %d\n", ceph_vinop(inode), err);
461 }
462
ceph_init_request(struct netfs_io_request * rreq,struct file * file)463 static int ceph_init_request(struct netfs_io_request *rreq, struct file *file)
464 {
465 struct inode *inode = rreq->inode;
466 struct ceph_fs_client *fsc = ceph_inode_to_fs_client(inode);
467 struct ceph_client *cl = ceph_inode_to_client(inode);
468 int got = 0, want = CEPH_CAP_FILE_CACHE;
469 struct ceph_netfs_request_data *priv;
470 int ret = 0;
471
472 /* [DEPRECATED] Use PG_private_2 to mark folio being written to the cache. */
473 __set_bit(NETFS_RREQ_USE_PGPRIV2, &rreq->flags);
474
475 if (rreq->origin != NETFS_READAHEAD)
476 return 0;
477
478 priv = kzalloc_obj(*priv, GFP_NOFS);
479 if (!priv)
480 return -ENOMEM;
481
482 if (file) {
483 struct ceph_rw_context *rw_ctx;
484 struct ceph_file_info *fi = file->private_data;
485
486 priv->file_ra_pages = file->f_ra.ra_pages;
487 priv->file_ra_disabled = file->f_mode & FMODE_RANDOM;
488
489 rw_ctx = ceph_find_rw_context(fi);
490 if (rw_ctx) {
491 rreq->netfs_priv = priv;
492 return 0;
493 }
494 }
495
496 /*
497 * readahead callers do not necessarily hold Fcb caps
498 * (e.g. fadvise, madvise).
499 */
500 ret = ceph_try_get_caps(inode, CEPH_CAP_FILE_RD, want, true, &got);
501 if (ret < 0) {
502 doutc(cl, "%llx.%llx, error getting cap\n", ceph_vinop(inode));
503 goto out;
504 }
505
506 if (!(got & want)) {
507 doutc(cl, "%llx.%llx, no cache cap\n", ceph_vinop(inode));
508 ret = -EACCES;
509 goto out;
510 }
511 if (ret == 0) {
512 ret = -EACCES;
513 goto out;
514 }
515
516 priv->caps = got;
517 rreq->netfs_priv = priv;
518 rreq->io_streams[0].sreq_max_len = fsc->mount_options->rsize;
519
520 out:
521 if (ret < 0) {
522 if (got)
523 ceph_put_cap_refs(ceph_inode(inode), got);
524 kfree(priv);
525 }
526
527 return ret;
528 }
529
ceph_netfs_free_request(struct netfs_io_request * rreq)530 static void ceph_netfs_free_request(struct netfs_io_request *rreq)
531 {
532 struct ceph_netfs_request_data *priv = rreq->netfs_priv;
533
534 if (!priv)
535 return;
536
537 if (priv->caps)
538 ceph_put_cap_refs(ceph_inode(rreq->inode), priv->caps);
539 kfree(priv);
540 rreq->netfs_priv = NULL;
541 }
542
543 const struct netfs_request_ops ceph_netfs_ops = {
544 .init_request = ceph_init_request,
545 .free_request = ceph_netfs_free_request,
546 .prepare_read = ceph_netfs_prepare_read,
547 .issue_read = ceph_netfs_issue_read,
548 .expand_readahead = ceph_netfs_expand_readahead,
549 .check_write_begin = ceph_netfs_check_write_begin,
550 };
551
552 #ifdef CONFIG_CEPH_FSCACHE
ceph_set_page_fscache(struct page * page)553 static void ceph_set_page_fscache(struct page *page)
554 {
555 folio_start_private_2(page_folio(page)); /* [DEPRECATED] */
556 }
557
ceph_fscache_write_terminated(void * priv,ssize_t error)558 static void ceph_fscache_write_terminated(void *priv, ssize_t error)
559 {
560 struct inode *inode = priv;
561
562 if (IS_ERR_VALUE(error) && error != -ENOBUFS)
563 ceph_fscache_invalidate(inode, false);
564 }
565
ceph_fscache_write_to_cache(struct inode * inode,u64 off,u64 len,bool caching)566 static void ceph_fscache_write_to_cache(struct inode *inode, u64 off, u64 len, bool caching)
567 {
568 struct ceph_inode_info *ci = ceph_inode(inode);
569 struct fscache_cookie *cookie = ceph_fscache_cookie(ci);
570
571 fscache_write_to_cache(cookie, inode->i_mapping, off, len, i_size_read(inode),
572 ceph_fscache_write_terminated, inode, true, caching);
573 }
574 #else
ceph_set_page_fscache(struct page * page)575 static inline void ceph_set_page_fscache(struct page *page)
576 {
577 }
578
ceph_fscache_write_to_cache(struct inode * inode,u64 off,u64 len,bool caching)579 static inline void ceph_fscache_write_to_cache(struct inode *inode, u64 off, u64 len, bool caching)
580 {
581 }
582 #endif /* CONFIG_CEPH_FSCACHE */
583
584 struct ceph_writeback_ctl
585 {
586 loff_t i_size;
587 u64 truncate_size;
588 u32 truncate_seq;
589 bool size_stable;
590
591 bool head_snapc;
592 struct ceph_snap_context *snapc;
593 struct ceph_snap_context *last_snapc;
594
595 bool done;
596 bool should_loop;
597 bool range_whole;
598 pgoff_t start_index;
599 pgoff_t index;
600 pgoff_t end;
601 xa_mark_t tag;
602
603 pgoff_t strip_unit_end;
604 unsigned int wsize;
605 unsigned int nr_folios;
606 unsigned int max_pages;
607 unsigned int locked_pages;
608
609 int op_idx;
610 int num_ops;
611 u64 offset;
612 u64 len;
613
614 struct folio_batch fbatch;
615 unsigned int processed_in_fbatch;
616
617 bool from_pool;
618 struct page **pages;
619 struct page **data_pages;
620 };
621
622 /*
623 * Get ref for the oldest snapc for an inode with dirty data... that is, the
624 * only snap context we are allowed to write back.
625 */
626 static struct ceph_snap_context *
get_oldest_context(struct inode * inode,struct ceph_writeback_ctl * ctl,struct ceph_snap_context * page_snapc)627 get_oldest_context(struct inode *inode, struct ceph_writeback_ctl *ctl,
628 struct ceph_snap_context *page_snapc)
629 {
630 struct ceph_inode_info *ci = ceph_inode(inode);
631 struct ceph_client *cl = ceph_inode_to_client(inode);
632 struct ceph_snap_context *snapc = NULL;
633 struct ceph_cap_snap *capsnap = NULL;
634
635 spin_lock(&ci->i_ceph_lock);
636 list_for_each_entry(capsnap, &ci->i_cap_snaps, ci_item) {
637 doutc(cl, " capsnap %p snapc %p has %d dirty pages\n",
638 capsnap, capsnap->context, capsnap->dirty_pages);
639 if (!capsnap->dirty_pages)
640 continue;
641
642 /* get i_size, truncate_{seq,size} for page_snapc? */
643 if (snapc && capsnap->context != page_snapc)
644 continue;
645
646 if (ctl) {
647 if (capsnap->writing) {
648 ctl->i_size = i_size_read(inode);
649 ctl->size_stable = false;
650 } else {
651 ctl->i_size = capsnap->size;
652 ctl->size_stable = true;
653 }
654 ctl->truncate_size = capsnap->truncate_size;
655 ctl->truncate_seq = capsnap->truncate_seq;
656 ctl->head_snapc = false;
657 }
658
659 if (snapc)
660 break;
661
662 snapc = ceph_get_snap_context(capsnap->context);
663 if (!page_snapc ||
664 page_snapc == snapc ||
665 page_snapc->seq > snapc->seq)
666 break;
667 }
668 if (!snapc && ci->i_wrbuffer_ref_head) {
669 snapc = ceph_get_snap_context(ci->i_head_snapc);
670 doutc(cl, " head snapc %p has %d dirty pages\n", snapc,
671 ci->i_wrbuffer_ref_head);
672 if (ctl) {
673 ctl->i_size = i_size_read(inode);
674 ctl->truncate_size = ci->i_truncate_size;
675 ctl->truncate_seq = ci->i_truncate_seq;
676 ctl->size_stable = false;
677 ctl->head_snapc = true;
678 }
679 }
680 spin_unlock(&ci->i_ceph_lock);
681 return snapc;
682 }
683
get_writepages_data_length(struct inode * inode,struct page * page,u64 start)684 static u64 get_writepages_data_length(struct inode *inode,
685 struct page *page, u64 start)
686 {
687 struct ceph_inode_info *ci = ceph_inode(inode);
688 struct ceph_snap_context *snapc;
689 struct ceph_cap_snap *capsnap = NULL;
690 u64 end = i_size_read(inode);
691 u64 ret;
692
693 snapc = page_snap_context(ceph_fscrypt_pagecache_page(page));
694 if (snapc != ci->i_head_snapc) {
695 bool found = false;
696 spin_lock(&ci->i_ceph_lock);
697 list_for_each_entry(capsnap, &ci->i_cap_snaps, ci_item) {
698 if (capsnap->context == snapc) {
699 if (!capsnap->writing)
700 end = capsnap->size;
701 found = true;
702 break;
703 }
704 }
705 spin_unlock(&ci->i_ceph_lock);
706 WARN_ON(!found);
707 }
708 if (end > ceph_fscrypt_page_offset(page) + thp_size(page))
709 end = ceph_fscrypt_page_offset(page) + thp_size(page);
710 ret = end > start ? end - start : 0;
711 if (ret && fscrypt_is_bounce_page(page))
712 ret = round_up(ret, CEPH_FSCRYPT_BLOCK_SIZE);
713 return ret;
714 }
715
716 /*
717 * Write a folio, but leave it locked.
718 *
719 * If we get a write error, mark the mapping for error, but still adjust the
720 * dirty page accounting (i.e., folio is no longer dirty).
721 */
write_folio_nounlock(struct folio * folio,struct writeback_control * wbc)722 static int write_folio_nounlock(struct folio *folio,
723 struct writeback_control *wbc)
724 {
725 struct page *page = &folio->page;
726 struct inode *inode = folio->mapping->host;
727 struct ceph_inode_info *ci = ceph_inode(inode);
728 struct ceph_fs_client *fsc = ceph_inode_to_fs_client(inode);
729 struct ceph_client *cl = fsc->client;
730 struct ceph_snap_context *snapc, *oldest;
731 loff_t page_off = folio_pos(folio);
732 int err;
733 loff_t len = folio_size(folio);
734 loff_t wlen;
735 struct ceph_writeback_ctl ceph_wbc;
736 struct ceph_osd_client *osdc = &fsc->client->osdc;
737 struct ceph_osd_request *req;
738 bool caching = ceph_is_cache_enabled(inode);
739 struct page *bounce_page = NULL;
740
741 doutc(cl, "%llx.%llx folio %p idx %lu\n", ceph_vinop(inode), folio,
742 folio->index);
743
744 if (ceph_inode_is_shutdown(inode))
745 return -EIO;
746
747 /* verify this is a writeable snap context */
748 snapc = page_snap_context(&folio->page);
749 if (!snapc) {
750 doutc(cl, "%llx.%llx folio %p not dirty?\n", ceph_vinop(inode),
751 folio);
752 return 0;
753 }
754 oldest = get_oldest_context(inode, &ceph_wbc, snapc);
755 if (snapc->seq > oldest->seq) {
756 doutc(cl, "%llx.%llx folio %p snapc %p not writeable - noop\n",
757 ceph_vinop(inode), folio, snapc);
758 /* we should only noop if called by kswapd */
759 WARN_ON(!(current->flags & PF_MEMALLOC));
760 ceph_put_snap_context(oldest);
761 folio_redirty_for_writepage(wbc, folio);
762 return 0;
763 }
764 ceph_put_snap_context(oldest);
765
766 /* is this a partial page at end of file? */
767 if (page_off >= ceph_wbc.i_size) {
768 doutc(cl, "%llx.%llx folio at %lu beyond eof %llu\n",
769 ceph_vinop(inode), folio->index, ceph_wbc.i_size);
770 folio_invalidate(folio, 0, folio_size(folio));
771 return 0;
772 }
773
774 if (ceph_wbc.i_size < page_off + len)
775 len = ceph_wbc.i_size - page_off;
776
777 wlen = IS_ENCRYPTED(inode) ? round_up(len, CEPH_FSCRYPT_BLOCK_SIZE) : len;
778 doutc(cl, "%llx.%llx folio %p index %lu on %llu~%llu snapc %p seq %lld\n",
779 ceph_vinop(inode), folio, folio->index, page_off, wlen, snapc,
780 snapc->seq);
781
782 if (atomic_long_inc_return(&fsc->writeback_count) >
783 CONGESTION_ON_THRESH(fsc->mount_options->congestion_kb))
784 fsc->write_congested = true;
785
786 req = ceph_osdc_new_request(osdc, &ci->i_layout, ceph_vino(inode),
787 page_off, &wlen, 0, 1, CEPH_OSD_OP_WRITE,
788 CEPH_OSD_FLAG_WRITE, snapc,
789 ceph_wbc.truncate_seq,
790 ceph_wbc.truncate_size, true);
791 if (IS_ERR(req)) {
792 folio_redirty_for_writepage(wbc, folio);
793 if (atomic_long_dec_return(&fsc->writeback_count) <
794 CONGESTION_OFF_THRESH(fsc->mount_options->congestion_kb))
795 fsc->write_congested = false;
796 return PTR_ERR(req);
797 }
798
799 if (wlen < len)
800 len = wlen;
801
802 folio_start_writeback(folio);
803 if (caching)
804 ceph_set_page_fscache(&folio->page);
805 ceph_fscache_write_to_cache(inode, page_off, len, caching);
806
807 if (IS_ENCRYPTED(inode)) {
808 bounce_page = fscrypt_encrypt_pagecache_blocks(folio,
809 CEPH_FSCRYPT_BLOCK_SIZE, 0,
810 GFP_NOFS);
811 if (IS_ERR(bounce_page)) {
812 folio_redirty_for_writepage(wbc, folio);
813 folio_end_writeback(folio);
814 ceph_osdc_put_request(req);
815 if (atomic_long_dec_return(&fsc->writeback_count) <
816 CONGESTION_OFF_THRESH(fsc->mount_options->congestion_kb))
817 fsc->write_congested = false;
818 return PTR_ERR(bounce_page);
819 }
820 }
821
822 /* it may be a short write due to an object boundary */
823 WARN_ON_ONCE(len > folio_size(folio));
824 osd_req_op_extent_osd_data_pages(req, 0,
825 bounce_page ? &bounce_page : &page, wlen, 0,
826 false, false);
827 doutc(cl, "%llx.%llx %llu~%llu (%llu bytes, %sencrypted)\n",
828 ceph_vinop(inode), page_off, len, wlen,
829 IS_ENCRYPTED(inode) ? "" : "not ");
830
831 req->r_mtime = inode_get_mtime(inode);
832 ceph_osdc_start_request(osdc, req);
833 err = ceph_osdc_wait_request(osdc, req);
834
835 ceph_update_write_metrics(&fsc->mdsc->metric, req->r_start_latency,
836 req->r_end_latency, len, err);
837 if (err >= 0 && len > 0)
838 ceph_subvolume_metrics_record_io(fsc->mdsc, ci, true, len,
839 req->r_start_latency,
840 req->r_end_latency);
841 fscrypt_free_bounce_page(bounce_page);
842 ceph_osdc_put_request(req);
843 if (err == 0)
844 err = len;
845
846 if (err < 0) {
847 struct writeback_control tmp_wbc;
848 if (!wbc)
849 wbc = &tmp_wbc;
850 if (err == -ERESTARTSYS) {
851 /* killed by SIGKILL */
852 doutc(cl, "%llx.%llx interrupted page %p\n",
853 ceph_vinop(inode), folio);
854 folio_redirty_for_writepage(wbc, folio);
855 folio_end_writeback(folio);
856 if (atomic_long_dec_return(&fsc->writeback_count) <
857 CONGESTION_OFF_THRESH(fsc->mount_options->congestion_kb))
858 fsc->write_congested = false;
859 return err;
860 }
861 if (err == -EBLOCKLISTED)
862 fsc->blocklisted = true;
863 doutc(cl, "%llx.%llx setting mapping error %d %p\n",
864 ceph_vinop(inode), err, folio);
865 mapping_set_error(&inode->i_data, err);
866 wbc->pages_skipped++;
867 } else {
868 doutc(cl, "%llx.%llx cleaned page %p\n",
869 ceph_vinop(inode), folio);
870 err = 0; /* vfs expects us to return 0 */
871 }
872 oldest = folio_detach_private(folio);
873 WARN_ON_ONCE(oldest != snapc);
874 folio_end_writeback(folio);
875 ceph_put_wrbuffer_cap_refs(ci, 1, snapc);
876 ceph_put_snap_context(snapc); /* page's reference */
877
878 if (atomic_long_dec_return(&fsc->writeback_count) <
879 CONGESTION_OFF_THRESH(fsc->mount_options->congestion_kb))
880 fsc->write_congested = false;
881
882 return err;
883 }
884
885 /*
886 * async writeback completion handler.
887 *
888 * If we get an error, set the mapping error bit, but not the individual
889 * page error bits.
890 */
writepages_finish(struct ceph_osd_request * req)891 static void writepages_finish(struct ceph_osd_request *req)
892 {
893 struct inode *inode = req->r_inode;
894 struct ceph_inode_info *ci = ceph_inode(inode);
895 struct ceph_client *cl = ceph_inode_to_client(inode);
896 struct ceph_osd_data *osd_data;
897 struct page *page;
898 int num_pages, total_pages = 0;
899 int i, j;
900 int rc = req->r_result;
901 struct ceph_snap_context *snapc = req->r_snapc;
902 struct address_space *mapping = inode->i_mapping;
903 struct ceph_fs_client *fsc = ceph_inode_to_fs_client(inode);
904 struct ceph_mds_client *mdsc = ceph_sb_to_mdsc(inode->i_sb);
905 unsigned int len = 0;
906 bool remove_page;
907
908 doutc(cl, "%llx.%llx rc %d\n", ceph_vinop(inode), rc);
909 if (rc < 0) {
910 mapping_set_error(mapping, rc);
911 ceph_set_error_write(ci);
912 if (rc == -EBLOCKLISTED)
913 fsc->blocklisted = true;
914 } else {
915 ceph_clear_error_write(ci);
916 }
917
918 /*
919 * We lost the cache cap, need to truncate the page before
920 * it is unlocked, otherwise we'd truncate it later in the
921 * page truncation thread, possibly losing some data that
922 * raced its way in
923 */
924 remove_page = !(ceph_caps_issued(ci) &
925 (CEPH_CAP_FILE_CACHE|CEPH_CAP_FILE_LAZYIO));
926
927 /* clean all pages */
928 for (i = 0; i < req->r_num_ops; i++) {
929 if (req->r_ops[i].op != CEPH_OSD_OP_WRITE) {
930 pr_warn_client(cl,
931 "%llx.%llx incorrect op %d req %p index %d tid %llu\n",
932 ceph_vinop(inode), req->r_ops[i].op, req, i,
933 req->r_tid);
934 break;
935 }
936
937 osd_data = osd_req_op_extent_osd_data(req, i);
938 BUG_ON(osd_data->type != CEPH_OSD_DATA_TYPE_PAGES);
939 len += osd_data->length;
940 num_pages = calc_pages_for((u64)osd_data->alignment,
941 (u64)osd_data->length);
942 total_pages += num_pages;
943 for (j = 0; j < num_pages; j++) {
944 page = osd_data->pages[j];
945 if (fscrypt_is_bounce_page(page)) {
946 page = fscrypt_pagecache_page(page);
947 fscrypt_free_bounce_page(osd_data->pages[j]);
948 osd_data->pages[j] = page;
949 }
950 BUG_ON(!page);
951 WARN_ON(!PageUptodate(page));
952
953 if (atomic_long_dec_return(&fsc->writeback_count) <
954 CONGESTION_OFF_THRESH(
955 fsc->mount_options->congestion_kb))
956 fsc->write_congested = false;
957
958 ceph_put_snap_context(detach_page_private(page));
959 end_page_writeback(page);
960
961 if (atomic64_dec_return(&mdsc->dirty_folios) <= 0) {
962 wake_up_all(&mdsc->flush_end_wq);
963 WARN_ON(atomic64_read(&mdsc->dirty_folios) < 0);
964 }
965
966 doutc(cl, "unlocking %p\n", page);
967
968 if (remove_page)
969 generic_error_remove_folio(inode->i_mapping,
970 page_folio(page));
971
972 unlock_page(page);
973 }
974 doutc(cl, "%llx.%llx wrote %llu bytes cleaned %d pages\n",
975 ceph_vinop(inode), osd_data->length,
976 rc >= 0 ? num_pages : 0);
977
978 release_pages(osd_data->pages, num_pages);
979 }
980
981 ceph_update_write_metrics(&fsc->mdsc->metric, req->r_start_latency,
982 req->r_end_latency, len, rc);
983
984 if (rc >= 0 && len > 0)
985 ceph_subvolume_metrics_record_io(mdsc, ci, true, len,
986 req->r_start_latency,
987 req->r_end_latency);
988
989 ceph_put_wrbuffer_cap_refs(ci, total_pages, snapc);
990
991 osd_data = osd_req_op_extent_osd_data(req, 0);
992 if (osd_data->pages_from_pool)
993 mempool_free(osd_data->pages, ceph_wb_pagevec_pool);
994 else
995 kfree(osd_data->pages);
996 ceph_osdc_put_request(req);
997 ceph_dec_osd_stopping_blocker(fsc->mdsc);
998 }
999
1000 static inline
is_forced_umount(struct address_space * mapping)1001 bool is_forced_umount(struct address_space *mapping)
1002 {
1003 struct inode *inode = mapping->host;
1004 struct ceph_inode_info *ci = ceph_inode(inode);
1005 struct ceph_fs_client *fsc = ceph_inode_to_fs_client(inode);
1006 struct ceph_client *cl = fsc->client;
1007
1008 if (ceph_inode_is_shutdown(inode)) {
1009 if (ci->i_wrbuffer_ref > 0) {
1010 pr_warn_ratelimited_client(cl,
1011 "%llx.%llx %lld forced umount\n",
1012 ceph_vinop(inode), ceph_ino(inode));
1013 }
1014 mapping_set_error(mapping, -EIO);
1015 return true;
1016 }
1017
1018 return false;
1019 }
1020
1021 static inline
ceph_define_write_size(struct address_space * mapping)1022 unsigned int ceph_define_write_size(struct address_space *mapping)
1023 {
1024 struct inode *inode = mapping->host;
1025 struct ceph_fs_client *fsc = ceph_inode_to_fs_client(inode);
1026 struct ceph_inode_info *ci = ceph_inode(inode);
1027 unsigned int wsize = ci->i_layout.stripe_unit;
1028
1029 if (fsc->mount_options->wsize < wsize)
1030 wsize = fsc->mount_options->wsize;
1031
1032 return wsize;
1033 }
1034
1035 static inline
ceph_folio_batch_init(struct ceph_writeback_ctl * ceph_wbc)1036 void ceph_folio_batch_init(struct ceph_writeback_ctl *ceph_wbc)
1037 {
1038 folio_batch_init(&ceph_wbc->fbatch);
1039 ceph_wbc->processed_in_fbatch = 0;
1040 }
1041
1042 static inline
ceph_folio_batch_reinit(struct ceph_writeback_ctl * ceph_wbc)1043 void ceph_folio_batch_reinit(struct ceph_writeback_ctl *ceph_wbc)
1044 {
1045 folio_batch_release(&ceph_wbc->fbatch);
1046 ceph_folio_batch_init(ceph_wbc);
1047 }
1048
1049 static inline
ceph_init_writeback_ctl(struct address_space * mapping,struct writeback_control * wbc,struct ceph_writeback_ctl * ceph_wbc)1050 void ceph_init_writeback_ctl(struct address_space *mapping,
1051 struct writeback_control *wbc,
1052 struct ceph_writeback_ctl *ceph_wbc)
1053 {
1054 ceph_wbc->snapc = NULL;
1055 ceph_wbc->last_snapc = NULL;
1056
1057 ceph_wbc->strip_unit_end = 0;
1058 ceph_wbc->wsize = ceph_define_write_size(mapping);
1059
1060 ceph_wbc->nr_folios = 0;
1061 ceph_wbc->max_pages = 0;
1062 ceph_wbc->locked_pages = 0;
1063
1064 ceph_wbc->done = false;
1065 ceph_wbc->should_loop = false;
1066 ceph_wbc->range_whole = false;
1067
1068 ceph_wbc->start_index = wbc->range_cyclic ? mapping->writeback_index : 0;
1069 ceph_wbc->index = ceph_wbc->start_index;
1070 ceph_wbc->end = -1;
1071
1072 ceph_wbc->tag = wbc_to_tag(wbc);
1073
1074 ceph_wbc->op_idx = -1;
1075 ceph_wbc->num_ops = 0;
1076 ceph_wbc->offset = 0;
1077 ceph_wbc->len = 0;
1078 ceph_wbc->from_pool = false;
1079
1080 ceph_folio_batch_init(ceph_wbc);
1081
1082 ceph_wbc->pages = NULL;
1083 ceph_wbc->data_pages = NULL;
1084 }
1085
1086 static inline
ceph_define_writeback_range(struct address_space * mapping,struct writeback_control * wbc,struct ceph_writeback_ctl * ceph_wbc)1087 int ceph_define_writeback_range(struct address_space *mapping,
1088 struct writeback_control *wbc,
1089 struct ceph_writeback_ctl *ceph_wbc)
1090 {
1091 struct inode *inode = mapping->host;
1092 struct ceph_fs_client *fsc = ceph_inode_to_fs_client(inode);
1093 struct ceph_client *cl = fsc->client;
1094
1095 /* find oldest snap context with dirty data */
1096 ceph_wbc->snapc = get_oldest_context(inode, ceph_wbc, NULL);
1097 if (!ceph_wbc->snapc) {
1098 /* hmm, why does writepages get called when there
1099 is no dirty data? */
1100 doutc(cl, " no snap context with dirty data?\n");
1101 return -ENODATA;
1102 }
1103
1104 doutc(cl, " oldest snapc is %p seq %lld (%d snaps)\n",
1105 ceph_wbc->snapc, ceph_wbc->snapc->seq,
1106 ceph_wbc->snapc->num_snaps);
1107
1108 ceph_wbc->should_loop = false;
1109
1110 if (ceph_wbc->head_snapc && ceph_wbc->snapc != ceph_wbc->last_snapc) {
1111 /* where to start/end? */
1112 if (wbc->range_cyclic) {
1113 ceph_wbc->index = ceph_wbc->start_index;
1114 ceph_wbc->end = -1;
1115 if (ceph_wbc->index > 0)
1116 ceph_wbc->should_loop = true;
1117 doutc(cl, " cyclic, start at %lu\n", ceph_wbc->index);
1118 } else {
1119 ceph_wbc->index = wbc->range_start >> PAGE_SHIFT;
1120 ceph_wbc->end = wbc->range_end >> PAGE_SHIFT;
1121 if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX)
1122 ceph_wbc->range_whole = true;
1123 doutc(cl, " not cyclic, %lu to %lu\n",
1124 ceph_wbc->index, ceph_wbc->end);
1125 }
1126 } else if (!ceph_wbc->head_snapc) {
1127 /* Do not respect wbc->range_{start,end}. Dirty pages
1128 * in that range can be associated with newer snapc.
1129 * They are not writeable until we write all dirty pages
1130 * associated with 'snapc' get written */
1131 if (ceph_wbc->index > 0)
1132 ceph_wbc->should_loop = true;
1133 doutc(cl, " non-head snapc, range whole\n");
1134 }
1135
1136 ceph_put_snap_context(ceph_wbc->last_snapc);
1137 ceph_wbc->last_snapc = ceph_wbc->snapc;
1138
1139 return 0;
1140 }
1141
1142 static inline
has_writeback_done(struct ceph_writeback_ctl * ceph_wbc)1143 bool has_writeback_done(struct ceph_writeback_ctl *ceph_wbc)
1144 {
1145 return ceph_wbc->done && ceph_wbc->index > ceph_wbc->end;
1146 }
1147
1148 static inline
can_next_page_be_processed(struct ceph_writeback_ctl * ceph_wbc,unsigned index)1149 bool can_next_page_be_processed(struct ceph_writeback_ctl *ceph_wbc,
1150 unsigned index)
1151 {
1152 return index < ceph_wbc->nr_folios &&
1153 ceph_wbc->locked_pages < ceph_wbc->max_pages;
1154 }
1155
1156 static
ceph_check_page_before_write(struct address_space * mapping,struct writeback_control * wbc,struct ceph_writeback_ctl * ceph_wbc,struct folio * folio)1157 int ceph_check_page_before_write(struct address_space *mapping,
1158 struct writeback_control *wbc,
1159 struct ceph_writeback_ctl *ceph_wbc,
1160 struct folio *folio)
1161 {
1162 struct inode *inode = mapping->host;
1163 struct ceph_fs_client *fsc = ceph_inode_to_fs_client(inode);
1164 struct ceph_client *cl = fsc->client;
1165 struct ceph_snap_context *pgsnapc;
1166
1167 /* only dirty folios, or our accounting breaks */
1168 if (unlikely(!folio_test_dirty(folio) || folio->mapping != mapping)) {
1169 doutc(cl, "!dirty or !mapping %p\n", folio);
1170 return -ENODATA;
1171 }
1172
1173 /* only if matching snap context */
1174 pgsnapc = page_snap_context(&folio->page);
1175 if (pgsnapc != ceph_wbc->snapc) {
1176 doutc(cl, "folio snapc %p %lld != oldest %p %lld\n",
1177 pgsnapc, pgsnapc->seq,
1178 ceph_wbc->snapc, ceph_wbc->snapc->seq);
1179
1180 if (!ceph_wbc->should_loop && !ceph_wbc->head_snapc &&
1181 wbc->sync_mode != WB_SYNC_NONE)
1182 ceph_wbc->should_loop = true;
1183
1184 return -ENODATA;
1185 }
1186
1187 if (folio_pos(folio) >= ceph_wbc->i_size) {
1188 doutc(cl, "folio at %lu beyond eof %llu\n",
1189 folio->index, ceph_wbc->i_size);
1190
1191 if ((ceph_wbc->size_stable ||
1192 folio_pos(folio) >= i_size_read(inode)) &&
1193 folio_clear_dirty_for_io(folio))
1194 folio_invalidate(folio, 0, folio_size(folio));
1195
1196 return -ENODATA;
1197 }
1198
1199 if (ceph_wbc->strip_unit_end &&
1200 (folio->index > ceph_wbc->strip_unit_end)) {
1201 doutc(cl, "end of strip unit %p\n", folio);
1202 return -E2BIG;
1203 }
1204
1205 return 0;
1206 }
1207
1208 static inline
__ceph_allocate_page_array(struct ceph_writeback_ctl * ceph_wbc,unsigned int max_pages)1209 void __ceph_allocate_page_array(struct ceph_writeback_ctl *ceph_wbc,
1210 unsigned int max_pages)
1211 {
1212 ceph_wbc->pages = kmalloc_objs(*ceph_wbc->pages, max_pages, GFP_NOFS);
1213 if (!ceph_wbc->pages) {
1214 ceph_wbc->from_pool = true;
1215 ceph_wbc->pages = mempool_alloc(ceph_wb_pagevec_pool, GFP_NOFS);
1216 BUG_ON(!ceph_wbc->pages);
1217 }
1218 }
1219
1220 static inline
ceph_allocate_page_array(struct address_space * mapping,struct ceph_writeback_ctl * ceph_wbc,struct folio * folio)1221 void ceph_allocate_page_array(struct address_space *mapping,
1222 struct ceph_writeback_ctl *ceph_wbc,
1223 struct folio *folio)
1224 {
1225 struct inode *inode = mapping->host;
1226 struct ceph_inode_info *ci = ceph_inode(inode);
1227 u64 objnum;
1228 u64 objoff;
1229 u32 xlen;
1230
1231 /* prepare async write request */
1232 ceph_wbc->offset = (u64)folio_pos(folio);
1233 ceph_calc_file_object_mapping(&ci->i_layout,
1234 ceph_wbc->offset, ceph_wbc->wsize,
1235 &objnum, &objoff, &xlen);
1236
1237 ceph_wbc->num_ops = 1;
1238 ceph_wbc->strip_unit_end = folio->index + ((xlen - 1) >> PAGE_SHIFT);
1239
1240 BUG_ON(ceph_wbc->pages);
1241 ceph_wbc->max_pages = calc_pages_for(0, (u64)xlen);
1242 __ceph_allocate_page_array(ceph_wbc, ceph_wbc->max_pages);
1243
1244 ceph_wbc->len = 0;
1245 }
1246
1247 static inline
is_folio_index_contiguous(const struct ceph_writeback_ctl * ceph_wbc,const struct folio * folio)1248 bool is_folio_index_contiguous(const struct ceph_writeback_ctl *ceph_wbc,
1249 const struct folio *folio)
1250 {
1251 return folio->index == (ceph_wbc->offset + ceph_wbc->len) >> PAGE_SHIFT;
1252 }
1253
1254 static inline
is_num_ops_too_big(struct ceph_writeback_ctl * ceph_wbc)1255 bool is_num_ops_too_big(struct ceph_writeback_ctl *ceph_wbc)
1256 {
1257 return ceph_wbc->num_ops >=
1258 (ceph_wbc->from_pool ? CEPH_OSD_SLAB_OPS : CEPH_OSD_MAX_OPS);
1259 }
1260
1261 static inline
is_write_congestion_happened(struct ceph_fs_client * fsc)1262 bool is_write_congestion_happened(struct ceph_fs_client *fsc)
1263 {
1264 return atomic_long_inc_return(&fsc->writeback_count) >
1265 CONGESTION_ON_THRESH(fsc->mount_options->congestion_kb);
1266 }
1267
move_dirty_folio_in_page_array(struct address_space * mapping,struct writeback_control * wbc,struct ceph_writeback_ctl * ceph_wbc,struct folio * folio)1268 static inline int move_dirty_folio_in_page_array(struct address_space *mapping,
1269 struct writeback_control *wbc,
1270 struct ceph_writeback_ctl *ceph_wbc, struct folio *folio)
1271 {
1272 struct inode *inode = mapping->host;
1273 struct ceph_fs_client *fsc = ceph_inode_to_fs_client(inode);
1274 struct ceph_client *cl = fsc->client;
1275 struct page **pages = ceph_wbc->pages;
1276 unsigned int index = ceph_wbc->locked_pages;
1277 gfp_t gfp_flags = ceph_wbc->locked_pages ? GFP_NOWAIT : GFP_NOFS;
1278
1279 if (IS_ENCRYPTED(inode)) {
1280 pages[index] = fscrypt_encrypt_pagecache_blocks(folio,
1281 PAGE_SIZE,
1282 0,
1283 gfp_flags);
1284 if (IS_ERR(pages[index])) {
1285 int err = PTR_ERR(pages[index]);
1286
1287 if (err == -EINVAL) {
1288 pr_err_client(cl, "inode->i_blkbits=%hhu\n",
1289 inode->i_blkbits);
1290 }
1291
1292 /* better not fail on first page! */
1293 BUG_ON(ceph_wbc->locked_pages == 0);
1294
1295 pages[index] = NULL;
1296 return err;
1297 }
1298 } else {
1299 pages[index] = &folio->page;
1300 }
1301
1302 ceph_wbc->locked_pages++;
1303
1304 return 0;
1305 }
1306
1307 static
ceph_process_folio_batch(struct address_space * mapping,struct writeback_control * wbc,struct ceph_writeback_ctl * ceph_wbc)1308 void ceph_process_folio_batch(struct address_space *mapping,
1309 struct writeback_control *wbc,
1310 struct ceph_writeback_ctl *ceph_wbc)
1311 {
1312 struct inode *inode = mapping->host;
1313 struct ceph_fs_client *fsc = ceph_inode_to_fs_client(inode);
1314 struct ceph_client *cl = fsc->client;
1315 struct folio *folio = NULL;
1316 unsigned i;
1317 int rc;
1318
1319 for (i = 0; can_next_page_be_processed(ceph_wbc, i); i++) {
1320 folio = ceph_wbc->fbatch.folios[i];
1321
1322 if (!folio)
1323 continue;
1324
1325 doutc(cl, "? %p idx %lu, folio_test_writeback %#x, "
1326 "folio_test_dirty %#x, folio_test_locked %#x\n",
1327 folio, folio->index, folio_test_writeback(folio),
1328 folio_test_dirty(folio),
1329 folio_test_locked(folio));
1330
1331 if (folio_test_writeback(folio) ||
1332 folio_test_private_2(folio) /* [DEPRECATED] */) {
1333 doutc(cl, "waiting on writeback %p\n", folio);
1334 folio_wait_writeback(folio);
1335 folio_wait_private_2(folio); /* [DEPRECATED] */
1336 continue;
1337 }
1338
1339 if (ceph_wbc->locked_pages == 0)
1340 folio_lock(folio);
1341 else if (!folio_trylock(folio))
1342 break;
1343
1344 rc = ceph_check_page_before_write(mapping, wbc,
1345 ceph_wbc, folio);
1346 if (rc == -ENODATA) {
1347 folio_unlock(folio);
1348 folio_put(folio);
1349 ceph_wbc->fbatch.folios[i] = NULL;
1350 continue;
1351 } else if (rc == -E2BIG) {
1352 folio_unlock(folio);
1353 break;
1354 }
1355
1356 if (!folio_clear_dirty_for_io(folio)) {
1357 doutc(cl, "%p !folio_clear_dirty_for_io\n", folio);
1358 folio_unlock(folio);
1359 folio_put(folio);
1360 ceph_wbc->fbatch.folios[i] = NULL;
1361 continue;
1362 }
1363
1364 /*
1365 * We have something to write. If this is
1366 * the first locked page this time through,
1367 * calculate max possible write size and
1368 * allocate a page array
1369 */
1370 if (ceph_wbc->locked_pages == 0) {
1371 ceph_allocate_page_array(mapping, ceph_wbc, folio);
1372 } else if (!is_folio_index_contiguous(ceph_wbc, folio)) {
1373 if (is_num_ops_too_big(ceph_wbc)) {
1374 folio_redirty_for_writepage(wbc, folio);
1375 folio_unlock(folio);
1376 break;
1377 }
1378
1379 ceph_wbc->num_ops++;
1380 ceph_wbc->offset = (u64)folio_pos(folio);
1381 ceph_wbc->len = 0;
1382 }
1383
1384 /* note position of first page in fbatch */
1385 doutc(cl, "%llx.%llx will write folio %p idx %lu\n",
1386 ceph_vinop(inode), folio, folio->index);
1387
1388 fsc->write_congested = is_write_congestion_happened(fsc);
1389
1390 rc = move_dirty_folio_in_page_array(mapping, wbc, ceph_wbc,
1391 folio);
1392 if (rc) {
1393 /* Did we just begin a new contiguous op? Nevermind! */
1394 if (ceph_wbc->len == 0)
1395 ceph_wbc->num_ops--;
1396
1397 folio_redirty_for_writepage(wbc, folio);
1398 folio_unlock(folio);
1399 break;
1400 }
1401
1402 ceph_wbc->fbatch.folios[i] = NULL;
1403 ceph_wbc->len += folio_size(folio);
1404 }
1405
1406 ceph_wbc->processed_in_fbatch = i;
1407 }
1408
1409 static inline
ceph_shift_unused_folios_left(struct folio_batch * fbatch)1410 void ceph_shift_unused_folios_left(struct folio_batch *fbatch)
1411 {
1412 unsigned j, n = 0;
1413
1414 /* shift unused page to beginning of fbatch */
1415 for (j = 0; j < folio_batch_count(fbatch); j++) {
1416 if (!fbatch->folios[j])
1417 continue;
1418
1419 if (n < j) {
1420 fbatch->folios[n] = fbatch->folios[j];
1421 }
1422
1423 n++;
1424 }
1425
1426 fbatch->nr = n;
1427 }
1428
1429 static
ceph_submit_write(struct address_space * mapping,struct writeback_control * wbc,struct ceph_writeback_ctl * ceph_wbc)1430 int ceph_submit_write(struct address_space *mapping,
1431 struct writeback_control *wbc,
1432 struct ceph_writeback_ctl *ceph_wbc)
1433 {
1434 struct inode *inode = mapping->host;
1435 struct ceph_inode_info *ci = ceph_inode(inode);
1436 struct ceph_fs_client *fsc = ceph_inode_to_fs_client(inode);
1437 struct ceph_client *cl = fsc->client;
1438 struct ceph_vino vino = ceph_vino(inode);
1439 struct ceph_osd_request *req = NULL;
1440 struct page *page = NULL;
1441 bool caching = ceph_is_cache_enabled(inode);
1442 u64 offset;
1443 u64 len;
1444 unsigned i;
1445
1446 new_request:
1447 offset = ceph_fscrypt_page_offset(ceph_wbc->pages[0]);
1448 len = ceph_wbc->wsize;
1449
1450 req = ceph_osdc_new_request(&fsc->client->osdc,
1451 &ci->i_layout, vino,
1452 offset, &len, 0, ceph_wbc->num_ops,
1453 CEPH_OSD_OP_WRITE, CEPH_OSD_FLAG_WRITE,
1454 ceph_wbc->snapc, ceph_wbc->truncate_seq,
1455 ceph_wbc->truncate_size, false);
1456 if (IS_ERR(req)) {
1457 req = ceph_osdc_new_request(&fsc->client->osdc,
1458 &ci->i_layout, vino,
1459 offset, &len, 0,
1460 min(ceph_wbc->num_ops,
1461 CEPH_OSD_SLAB_OPS),
1462 CEPH_OSD_OP_WRITE,
1463 CEPH_OSD_FLAG_WRITE,
1464 ceph_wbc->snapc,
1465 ceph_wbc->truncate_seq,
1466 ceph_wbc->truncate_size,
1467 true);
1468 BUG_ON(IS_ERR(req));
1469 }
1470
1471 page = ceph_wbc->pages[ceph_wbc->locked_pages - 1];
1472 BUG_ON(len < ceph_fscrypt_page_offset(page) + thp_size(page) - offset);
1473
1474 if (!ceph_inc_osd_stopping_blocker(fsc->mdsc)) {
1475 for (i = 0; i < folio_batch_count(&ceph_wbc->fbatch); i++) {
1476 struct folio *folio = ceph_wbc->fbatch.folios[i];
1477
1478 if (!folio)
1479 continue;
1480
1481 page = &folio->page;
1482 redirty_page_for_writepage(wbc, page);
1483 unlock_page(page);
1484 }
1485
1486 for (i = 0; i < ceph_wbc->locked_pages; i++) {
1487 page = ceph_fscrypt_pagecache_page(ceph_wbc->pages[i]);
1488
1489 if (!page)
1490 continue;
1491
1492 redirty_page_for_writepage(wbc, page);
1493 unlock_page(page);
1494 }
1495
1496 ceph_osdc_put_request(req);
1497 return -EIO;
1498 }
1499
1500 req->r_callback = writepages_finish;
1501 req->r_inode = inode;
1502
1503 /* Format the osd request message and submit the write */
1504 len = 0;
1505 ceph_wbc->data_pages = ceph_wbc->pages;
1506 ceph_wbc->op_idx = 0;
1507 for (i = 0; i < ceph_wbc->locked_pages; i++) {
1508 u64 cur_offset;
1509
1510 page = ceph_fscrypt_pagecache_page(ceph_wbc->pages[i]);
1511 cur_offset = page_offset(page);
1512
1513 /*
1514 * Discontinuity in page range? Ceph can handle that by just passing
1515 * multiple extents in the write op.
1516 */
1517 if (offset + len != cur_offset) {
1518 /* If it's full, stop here */
1519 if (ceph_wbc->op_idx + 1 == req->r_num_ops)
1520 break;
1521
1522 /* Kick off an fscache write with what we have so far. */
1523 ceph_fscache_write_to_cache(inode, offset, len, caching);
1524
1525 /* Start a new extent */
1526 osd_req_op_extent_dup_last(req, ceph_wbc->op_idx,
1527 cur_offset - offset);
1528
1529 doutc(cl, "got pages at %llu~%llu\n", offset, len);
1530
1531 osd_req_op_extent_osd_data_pages(req, ceph_wbc->op_idx,
1532 ceph_wbc->data_pages,
1533 len, 0,
1534 ceph_wbc->from_pool,
1535 false);
1536 osd_req_op_extent_update(req, ceph_wbc->op_idx, len);
1537
1538 len = 0;
1539 offset = cur_offset;
1540 ceph_wbc->data_pages = ceph_wbc->pages + i;
1541 ceph_wbc->op_idx++;
1542 }
1543
1544 set_page_writeback(page);
1545
1546 if (caching)
1547 ceph_set_page_fscache(page);
1548
1549 len += thp_size(page);
1550 }
1551
1552 ceph_fscache_write_to_cache(inode, offset, len, caching);
1553
1554 if (ceph_wbc->size_stable) {
1555 len = min(len, ceph_wbc->i_size - offset);
1556 } else if (i == ceph_wbc->locked_pages) {
1557 /* writepages_finish() clears writeback pages
1558 * according to the data length, so make sure
1559 * data length covers all locked pages */
1560 u64 min_len = len + 1 - thp_size(page);
1561 len = get_writepages_data_length(inode,
1562 ceph_wbc->pages[i - 1],
1563 offset);
1564 len = max(len, min_len);
1565 }
1566
1567 if (IS_ENCRYPTED(inode))
1568 len = round_up(len, CEPH_FSCRYPT_BLOCK_SIZE);
1569
1570 doutc(cl, "got pages at %llu~%llu\n", offset, len);
1571
1572 if (IS_ENCRYPTED(inode) &&
1573 ((offset | len) & ~CEPH_FSCRYPT_BLOCK_MASK)) {
1574 pr_warn_client(cl,
1575 "bad encrypted write offset=%lld len=%llu\n",
1576 offset, len);
1577 }
1578
1579 osd_req_op_extent_osd_data_pages(req, ceph_wbc->op_idx,
1580 ceph_wbc->data_pages, len,
1581 0, ceph_wbc->from_pool, false);
1582 osd_req_op_extent_update(req, ceph_wbc->op_idx, len);
1583
1584 BUG_ON(ceph_wbc->op_idx + 1 != req->r_num_ops);
1585
1586 ceph_wbc->from_pool = false;
1587 if (i < ceph_wbc->locked_pages) {
1588 BUG_ON(ceph_wbc->num_ops <= req->r_num_ops);
1589 ceph_wbc->num_ops -= req->r_num_ops;
1590 ceph_wbc->locked_pages -= i;
1591
1592 /* allocate new pages array for next request */
1593 ceph_wbc->data_pages = ceph_wbc->pages;
1594 __ceph_allocate_page_array(ceph_wbc, ceph_wbc->locked_pages);
1595 memcpy(ceph_wbc->pages, ceph_wbc->data_pages + i,
1596 ceph_wbc->locked_pages * sizeof(*ceph_wbc->pages));
1597 memset(ceph_wbc->data_pages + i, 0,
1598 ceph_wbc->locked_pages * sizeof(*ceph_wbc->pages));
1599 } else {
1600 BUG_ON(ceph_wbc->num_ops != req->r_num_ops);
1601 /* request message now owns the pages array */
1602 ceph_wbc->pages = NULL;
1603 }
1604
1605 req->r_mtime = inode_get_mtime(inode);
1606 ceph_osdc_start_request(&fsc->client->osdc, req);
1607 req = NULL;
1608
1609 wbc->nr_to_write -= i;
1610 if (ceph_wbc->pages)
1611 goto new_request;
1612
1613 return 0;
1614 }
1615
1616 static
ceph_wait_until_current_writes_complete(struct address_space * mapping,struct writeback_control * wbc,struct ceph_writeback_ctl * ceph_wbc)1617 void ceph_wait_until_current_writes_complete(struct address_space *mapping,
1618 struct writeback_control *wbc,
1619 struct ceph_writeback_ctl *ceph_wbc)
1620 {
1621 struct page *page;
1622 unsigned i, nr;
1623
1624 if (wbc->sync_mode != WB_SYNC_NONE &&
1625 ceph_wbc->start_index == 0 && /* all dirty pages were checked */
1626 !ceph_wbc->head_snapc) {
1627 ceph_wbc->index = 0;
1628
1629 while ((ceph_wbc->index <= ceph_wbc->end) &&
1630 (nr = filemap_get_folios_tag(mapping,
1631 &ceph_wbc->index,
1632 (pgoff_t)-1,
1633 PAGECACHE_TAG_WRITEBACK,
1634 &ceph_wbc->fbatch))) {
1635 for (i = 0; i < nr; i++) {
1636 page = &ceph_wbc->fbatch.folios[i]->page;
1637 if (page_snap_context(page) != ceph_wbc->snapc)
1638 continue;
1639 wait_on_page_writeback(page);
1640 }
1641
1642 folio_batch_release(&ceph_wbc->fbatch);
1643 cond_resched();
1644 }
1645 }
1646 }
1647
1648 /*
1649 * initiate async writeback
1650 */
ceph_writepages_start(struct address_space * mapping,struct writeback_control * wbc)1651 static int ceph_writepages_start(struct address_space *mapping,
1652 struct writeback_control *wbc)
1653 {
1654 struct inode *inode = mapping->host;
1655 struct ceph_fs_client *fsc = ceph_inode_to_fs_client(inode);
1656 struct ceph_client *cl = fsc->client;
1657 struct ceph_writeback_ctl ceph_wbc;
1658 int rc = 0;
1659
1660 if (wbc->sync_mode == WB_SYNC_NONE && fsc->write_congested)
1661 return 0;
1662
1663 doutc(cl, "%llx.%llx (mode=%s)\n", ceph_vinop(inode),
1664 wbc->sync_mode == WB_SYNC_NONE ? "NONE" :
1665 (wbc->sync_mode == WB_SYNC_ALL ? "ALL" : "HOLD"));
1666
1667 if (is_forced_umount(mapping)) {
1668 /* we're in a forced umount, don't write! */
1669 return -EIO;
1670 }
1671
1672 ceph_init_writeback_ctl(mapping, wbc, &ceph_wbc);
1673
1674 if (!ceph_inc_osd_stopping_blocker(fsc->mdsc)) {
1675 rc = -EIO;
1676 goto out;
1677 }
1678
1679 retry:
1680 rc = ceph_define_writeback_range(mapping, wbc, &ceph_wbc);
1681 if (rc == -ENODATA) {
1682 /* hmm, why does writepages get called when there
1683 is no dirty data? */
1684 rc = 0;
1685 goto dec_osd_stopping_blocker;
1686 }
1687
1688 if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages)
1689 tag_pages_for_writeback(mapping, ceph_wbc.index, ceph_wbc.end);
1690
1691 while (!has_writeback_done(&ceph_wbc)) {
1692 BUG_ON(ceph_wbc.locked_pages);
1693 BUG_ON(ceph_wbc.pages);
1694
1695 ceph_wbc.max_pages = ceph_wbc.wsize >> PAGE_SHIFT;
1696
1697 get_more_pages:
1698 ceph_folio_batch_reinit(&ceph_wbc);
1699
1700 ceph_wbc.nr_folios = filemap_get_folios_tag(mapping,
1701 &ceph_wbc.index,
1702 ceph_wbc.end,
1703 ceph_wbc.tag,
1704 &ceph_wbc.fbatch);
1705 doutc(cl, "pagevec_lookup_range_tag for tag %#x got %d\n",
1706 ceph_wbc.tag, ceph_wbc.nr_folios);
1707
1708 if (!ceph_wbc.nr_folios && !ceph_wbc.locked_pages)
1709 break;
1710
1711 process_folio_batch:
1712 ceph_process_folio_batch(mapping, wbc, &ceph_wbc);
1713 ceph_shift_unused_folios_left(&ceph_wbc.fbatch);
1714
1715 /* did we get anything? */
1716 if (!ceph_wbc.locked_pages)
1717 goto release_folios;
1718
1719 if (ceph_wbc.processed_in_fbatch) {
1720 if (folio_batch_count(&ceph_wbc.fbatch) == 0 &&
1721 ceph_wbc.locked_pages < ceph_wbc.max_pages) {
1722 doutc(cl, "reached end fbatch, trying for more\n");
1723 goto get_more_pages;
1724 }
1725 }
1726
1727 rc = ceph_submit_write(mapping, wbc, &ceph_wbc);
1728 if (rc)
1729 goto release_folios;
1730
1731 ceph_wbc.locked_pages = 0;
1732 ceph_wbc.strip_unit_end = 0;
1733
1734 if (folio_batch_count(&ceph_wbc.fbatch) > 0) {
1735 ceph_wbc.nr_folios =
1736 folio_batch_count(&ceph_wbc.fbatch);
1737 goto process_folio_batch;
1738 }
1739
1740 /*
1741 * We stop writing back only if we are not doing
1742 * integrity sync. In case of integrity sync we have to
1743 * keep going until we have written all the pages
1744 * we tagged for writeback prior to entering this loop.
1745 */
1746 if (wbc->nr_to_write <= 0 && wbc->sync_mode == WB_SYNC_NONE)
1747 ceph_wbc.done = true;
1748
1749 release_folios:
1750 doutc(cl, "folio_batch release on %d folios (%p)\n",
1751 (int)ceph_wbc.fbatch.nr,
1752 ceph_wbc.fbatch.nr ? ceph_wbc.fbatch.folios[0] : NULL);
1753 folio_batch_release(&ceph_wbc.fbatch);
1754 }
1755
1756 if (ceph_wbc.should_loop && !ceph_wbc.done) {
1757 /* more to do; loop back to beginning of file */
1758 doutc(cl, "looping back to beginning of file\n");
1759 /* OK even when start_index == 0 */
1760 ceph_wbc.end = ceph_wbc.start_index - 1;
1761
1762 /* to write dirty pages associated with next snapc,
1763 * we need to wait until current writes complete */
1764 ceph_wait_until_current_writes_complete(mapping, wbc, &ceph_wbc);
1765
1766 ceph_wbc.start_index = 0;
1767 ceph_wbc.index = 0;
1768 goto retry;
1769 }
1770
1771 if (wbc->range_cyclic || (ceph_wbc.range_whole && wbc->nr_to_write > 0))
1772 mapping->writeback_index = ceph_wbc.index;
1773
1774 dec_osd_stopping_blocker:
1775 ceph_dec_osd_stopping_blocker(fsc->mdsc);
1776
1777 out:
1778 ceph_put_snap_context(ceph_wbc.last_snapc);
1779 doutc(cl, "%llx.%llx dend - startone, rc = %d\n", ceph_vinop(inode),
1780 rc);
1781
1782 return rc;
1783 }
1784
1785 /*
1786 * See if a given @snapc is either writeable, or already written.
1787 */
context_is_writeable_or_written(struct inode * inode,struct ceph_snap_context * snapc)1788 static int context_is_writeable_or_written(struct inode *inode,
1789 struct ceph_snap_context *snapc)
1790 {
1791 struct ceph_snap_context *oldest = get_oldest_context(inode, NULL, NULL);
1792 int ret = !oldest || snapc->seq <= oldest->seq;
1793
1794 ceph_put_snap_context(oldest);
1795 return ret;
1796 }
1797
1798 /**
1799 * ceph_find_incompatible - find an incompatible context and return it
1800 * @folio: folio being dirtied
1801 *
1802 * We are only allowed to write into/dirty a folio if the folio is
1803 * clean, or already dirty within the same snap context. Returns a
1804 * conflicting context if there is one, NULL if there isn't, or a
1805 * negative error code on other errors.
1806 *
1807 * Must be called with folio lock held.
1808 */
1809 static struct ceph_snap_context *
ceph_find_incompatible(struct folio * folio)1810 ceph_find_incompatible(struct folio *folio)
1811 {
1812 struct inode *inode = folio->mapping->host;
1813 struct ceph_client *cl = ceph_inode_to_client(inode);
1814 struct ceph_inode_info *ci = ceph_inode(inode);
1815
1816 if (ceph_inode_is_shutdown(inode)) {
1817 doutc(cl, " %llx.%llx folio %p is shutdown\n",
1818 ceph_vinop(inode), folio);
1819 return ERR_PTR(-ESTALE);
1820 }
1821
1822 for (;;) {
1823 struct ceph_snap_context *snapc, *oldest;
1824
1825 folio_wait_writeback(folio);
1826
1827 snapc = page_snap_context(&folio->page);
1828 if (!snapc || snapc == ci->i_head_snapc)
1829 break;
1830
1831 /*
1832 * this folio is already dirty in another (older) snap
1833 * context! is it writeable now?
1834 */
1835 oldest = get_oldest_context(inode, NULL, NULL);
1836 if (snapc->seq > oldest->seq) {
1837 /* not writeable -- return it for the caller to deal with */
1838 ceph_put_snap_context(oldest);
1839 doutc(cl, " %llx.%llx folio %p snapc %p not current or oldest\n",
1840 ceph_vinop(inode), folio, snapc);
1841 return ceph_get_snap_context(snapc);
1842 }
1843 ceph_put_snap_context(oldest);
1844
1845 /* yay, writeable, do it now (without dropping folio lock) */
1846 doutc(cl, " %llx.%llx folio %p snapc %p not current, but oldest\n",
1847 ceph_vinop(inode), folio, snapc);
1848 if (folio_clear_dirty_for_io(folio)) {
1849 int r = write_folio_nounlock(folio, NULL);
1850 if (r < 0)
1851 return ERR_PTR(r);
1852 }
1853 }
1854 return NULL;
1855 }
1856
ceph_netfs_check_write_begin(struct file * file,loff_t pos,unsigned int len,struct folio ** foliop,void ** _fsdata)1857 static int ceph_netfs_check_write_begin(struct file *file, loff_t pos, unsigned int len,
1858 struct folio **foliop, void **_fsdata)
1859 {
1860 struct inode *inode = file_inode(file);
1861 struct ceph_inode_info *ci = ceph_inode(inode);
1862 struct ceph_snap_context *snapc;
1863
1864 snapc = ceph_find_incompatible(*foliop);
1865 if (snapc) {
1866 int r;
1867
1868 folio_unlock(*foliop);
1869 folio_put(*foliop);
1870 *foliop = NULL;
1871 if (IS_ERR(snapc))
1872 return PTR_ERR(snapc);
1873
1874 ceph_queue_writeback(inode);
1875 r = wait_event_killable(ci->i_cap_wq,
1876 context_is_writeable_or_written(inode, snapc));
1877 ceph_put_snap_context(snapc);
1878 return r == 0 ? -EAGAIN : r;
1879 }
1880 return 0;
1881 }
1882
1883 /*
1884 * We are only allowed to write into/dirty the page if the page is
1885 * clean, or already dirty within the same snap context.
1886 */
ceph_write_begin(const struct kiocb * iocb,struct address_space * mapping,loff_t pos,unsigned len,struct folio ** foliop,void ** fsdata)1887 static int ceph_write_begin(const struct kiocb *iocb,
1888 struct address_space *mapping,
1889 loff_t pos, unsigned len,
1890 struct folio **foliop, void **fsdata)
1891 {
1892 struct file *file = iocb->ki_filp;
1893 struct inode *inode = file_inode(file);
1894 struct ceph_inode_info *ci = ceph_inode(inode);
1895 int r;
1896
1897 r = netfs_write_begin(&ci->netfs, file, inode->i_mapping, pos, len, foliop, NULL);
1898 if (r < 0)
1899 return r;
1900
1901 folio_wait_private_2(*foliop); /* [DEPRECATED] */
1902 WARN_ON_ONCE(!folio_test_locked(*foliop));
1903 return 0;
1904 }
1905
1906 /*
1907 * we don't do anything in here that simple_write_end doesn't do
1908 * except adjust dirty page accounting
1909 */
ceph_write_end(const struct kiocb * iocb,struct address_space * mapping,loff_t pos,unsigned len,unsigned copied,struct folio * folio,void * fsdata)1910 static int ceph_write_end(const struct kiocb *iocb,
1911 struct address_space *mapping, loff_t pos,
1912 unsigned len, unsigned copied,
1913 struct folio *folio, void *fsdata)
1914 {
1915 struct file *file = iocb->ki_filp;
1916 struct inode *inode = file_inode(file);
1917 struct ceph_client *cl = ceph_inode_to_client(inode);
1918 bool check_cap = false;
1919
1920 doutc(cl, "%llx.%llx file %p folio %p %d~%d (%d)\n", ceph_vinop(inode),
1921 file, folio, (int)pos, (int)copied, (int)len);
1922
1923 if (!folio_test_uptodate(folio)) {
1924 /* just return that nothing was copied on a short copy */
1925 if (copied < len) {
1926 copied = 0;
1927 goto out;
1928 }
1929 folio_mark_uptodate(folio);
1930 }
1931
1932 /* did file size increase? */
1933 if (pos+copied > i_size_read(inode))
1934 check_cap = ceph_inode_set_size(inode, pos+copied);
1935
1936 folio_mark_dirty(folio);
1937
1938 out:
1939 folio_unlock(folio);
1940 folio_put(folio);
1941
1942 if (check_cap)
1943 ceph_check_caps(ceph_inode(inode), CHECK_CAPS_AUTHONLY);
1944
1945 return copied;
1946 }
1947
1948 const struct address_space_operations ceph_aops = {
1949 .read_folio = netfs_read_folio,
1950 .readahead = netfs_readahead,
1951 .writepages = ceph_writepages_start,
1952 .write_begin = ceph_write_begin,
1953 .write_end = ceph_write_end,
1954 .dirty_folio = ceph_dirty_folio,
1955 .invalidate_folio = ceph_invalidate_folio,
1956 .release_folio = netfs_release_folio,
1957 .direct_IO = noop_direct_IO,
1958 .migrate_folio = filemap_migrate_folio,
1959 };
1960
ceph_block_sigs(sigset_t * oldset)1961 static void ceph_block_sigs(sigset_t *oldset)
1962 {
1963 sigset_t mask;
1964 siginitsetinv(&mask, sigmask(SIGKILL));
1965 sigprocmask(SIG_BLOCK, &mask, oldset);
1966 }
1967
ceph_restore_sigs(sigset_t * oldset)1968 static void ceph_restore_sigs(sigset_t *oldset)
1969 {
1970 sigprocmask(SIG_SETMASK, oldset, NULL);
1971 }
1972
1973 /*
1974 * vm ops
1975 */
ceph_filemap_fault(struct vm_fault * vmf)1976 static vm_fault_t ceph_filemap_fault(struct vm_fault *vmf)
1977 {
1978 struct vm_area_struct *vma = vmf->vma;
1979 struct inode *inode = file_inode(vma->vm_file);
1980 struct ceph_inode_info *ci = ceph_inode(inode);
1981 struct ceph_client *cl = ceph_inode_to_client(inode);
1982 struct ceph_file_info *fi = vma->vm_file->private_data;
1983 loff_t off = (loff_t)vmf->pgoff << PAGE_SHIFT;
1984 int want, got, err;
1985 sigset_t oldset;
1986 vm_fault_t ret = VM_FAULT_SIGBUS;
1987
1988 if (ceph_inode_is_shutdown(inode))
1989 return ret;
1990
1991 ceph_block_sigs(&oldset);
1992
1993 doutc(cl, "%llx.%llx %llu trying to get caps\n",
1994 ceph_vinop(inode), off);
1995 if (fi->fmode & CEPH_FILE_MODE_LAZY)
1996 want = CEPH_CAP_FILE_CACHE | CEPH_CAP_FILE_LAZYIO;
1997 else
1998 want = CEPH_CAP_FILE_CACHE;
1999
2000 got = 0;
2001 err = ceph_get_caps(vma->vm_file, CEPH_CAP_FILE_RD, want, -1, &got);
2002 if (err < 0)
2003 goto out_restore;
2004
2005 doutc(cl, "%llx.%llx %llu got cap refs on %s\n", ceph_vinop(inode),
2006 off, ceph_cap_string(got));
2007
2008 if ((got & (CEPH_CAP_FILE_CACHE | CEPH_CAP_FILE_LAZYIO)) ||
2009 !ceph_has_inline_data(ci)) {
2010 CEPH_DEFINE_RW_CONTEXT(rw_ctx, got);
2011 ceph_add_rw_context(fi, &rw_ctx);
2012 ret = filemap_fault(vmf);
2013 ceph_del_rw_context(fi, &rw_ctx);
2014 doutc(cl, "%llx.%llx %llu drop cap refs %s ret %x\n",
2015 ceph_vinop(inode), off, ceph_cap_string(got), ret);
2016 } else
2017 err = -EAGAIN;
2018
2019 ceph_put_cap_refs(ci, got);
2020
2021 if (err != -EAGAIN)
2022 goto out_restore;
2023
2024 /* read inline data */
2025 if (off >= PAGE_SIZE) {
2026 /* does not support inline data > PAGE_SIZE */
2027 ret = VM_FAULT_SIGBUS;
2028 } else {
2029 struct address_space *mapping = inode->i_mapping;
2030 struct page *page;
2031
2032 filemap_invalidate_lock_shared(mapping);
2033 page = find_or_create_page(mapping, 0,
2034 mapping_gfp_constraint(mapping, ~__GFP_FS));
2035 if (!page) {
2036 ret = VM_FAULT_OOM;
2037 goto out_inline;
2038 }
2039 err = __ceph_do_getattr(inode, page,
2040 CEPH_STAT_CAP_INLINE_DATA, true);
2041 if (err < 0 || off >= i_size_read(inode)) {
2042 unlock_page(page);
2043 put_page(page);
2044 ret = vmf_error(err);
2045 goto out_inline;
2046 }
2047 if (err < PAGE_SIZE)
2048 zero_user_segment(page, err, PAGE_SIZE);
2049 else
2050 flush_dcache_page(page);
2051 SetPageUptodate(page);
2052 vmf->page = page;
2053 ret = VM_FAULT_MAJOR | VM_FAULT_LOCKED;
2054 out_inline:
2055 filemap_invalidate_unlock_shared(mapping);
2056 doutc(cl, "%llx.%llx %llu read inline data ret %x\n",
2057 ceph_vinop(inode), off, ret);
2058 }
2059 out_restore:
2060 ceph_restore_sigs(&oldset);
2061 if (err < 0)
2062 ret = vmf_error(err);
2063
2064 return ret;
2065 }
2066
ceph_page_mkwrite(struct vm_fault * vmf)2067 static vm_fault_t ceph_page_mkwrite(struct vm_fault *vmf)
2068 {
2069 struct vm_area_struct *vma = vmf->vma;
2070 struct inode *inode = file_inode(vma->vm_file);
2071 struct ceph_client *cl = ceph_inode_to_client(inode);
2072 struct ceph_inode_info *ci = ceph_inode(inode);
2073 struct ceph_file_info *fi = vma->vm_file->private_data;
2074 struct ceph_cap_flush *prealloc_cf;
2075 struct folio *folio = page_folio(vmf->page);
2076 loff_t off = folio_pos(folio);
2077 loff_t size = i_size_read(inode);
2078 size_t len;
2079 int want, got, err;
2080 sigset_t oldset;
2081 vm_fault_t ret = VM_FAULT_SIGBUS;
2082
2083 if (ceph_inode_is_shutdown(inode))
2084 return ret;
2085
2086 prealloc_cf = ceph_alloc_cap_flush();
2087 if (!prealloc_cf)
2088 return VM_FAULT_OOM;
2089
2090 sb_start_pagefault(inode->i_sb);
2091 ceph_block_sigs(&oldset);
2092
2093 if (off + folio_size(folio) <= size)
2094 len = folio_size(folio);
2095 else
2096 len = offset_in_folio(folio, size);
2097
2098 doutc(cl, "%llx.%llx %llu~%zd getting caps i_size %llu\n",
2099 ceph_vinop(inode), off, len, size);
2100 if (fi->fmode & CEPH_FILE_MODE_LAZY)
2101 want = CEPH_CAP_FILE_BUFFER | CEPH_CAP_FILE_LAZYIO;
2102 else
2103 want = CEPH_CAP_FILE_BUFFER;
2104
2105 got = 0;
2106 err = ceph_get_caps(vma->vm_file, CEPH_CAP_FILE_WR, want, off + len, &got);
2107 if (err < 0)
2108 goto out_free;
2109
2110 doutc(cl, "%llx.%llx %llu~%zd got cap refs on %s\n", ceph_vinop(inode),
2111 off, len, ceph_cap_string(got));
2112
2113 /* Update time before taking folio lock */
2114 file_update_time(vma->vm_file);
2115 inode_inc_iversion_raw(inode);
2116
2117 do {
2118 struct ceph_snap_context *snapc;
2119
2120 folio_lock(folio);
2121
2122 if (folio_mkwrite_check_truncate(folio, inode) < 0) {
2123 folio_unlock(folio);
2124 ret = VM_FAULT_NOPAGE;
2125 break;
2126 }
2127
2128 snapc = ceph_find_incompatible(folio);
2129 if (!snapc) {
2130 /* success. we'll keep the folio locked. */
2131 folio_mark_dirty(folio);
2132 ret = VM_FAULT_LOCKED;
2133 break;
2134 }
2135
2136 folio_unlock(folio);
2137
2138 if (IS_ERR(snapc)) {
2139 ret = VM_FAULT_SIGBUS;
2140 break;
2141 }
2142
2143 ceph_queue_writeback(inode);
2144 err = wait_event_killable(ci->i_cap_wq,
2145 context_is_writeable_or_written(inode, snapc));
2146 ceph_put_snap_context(snapc);
2147 } while (err == 0);
2148
2149 if (ret == VM_FAULT_LOCKED) {
2150 int dirty;
2151 spin_lock(&ci->i_ceph_lock);
2152 dirty = __ceph_mark_dirty_caps(ci, CEPH_CAP_FILE_WR,
2153 &prealloc_cf);
2154 spin_unlock(&ci->i_ceph_lock);
2155 if (dirty)
2156 __mark_inode_dirty(inode, dirty);
2157 }
2158
2159 doutc(cl, "%llx.%llx %llu~%zd dropping cap refs on %s ret %x\n",
2160 ceph_vinop(inode), off, len, ceph_cap_string(got), ret);
2161 ceph_put_cap_refs_async(ci, got);
2162 out_free:
2163 ceph_restore_sigs(&oldset);
2164 sb_end_pagefault(inode->i_sb);
2165 ceph_free_cap_flush(prealloc_cf);
2166 if (err < 0)
2167 ret = vmf_error(err);
2168 return ret;
2169 }
2170
ceph_fill_inline_data(struct inode * inode,struct page * locked_page,char * data,size_t len)2171 void ceph_fill_inline_data(struct inode *inode, struct page *locked_page,
2172 char *data, size_t len)
2173 {
2174 struct ceph_client *cl = ceph_inode_to_client(inode);
2175 struct address_space *mapping = inode->i_mapping;
2176 struct page *page;
2177
2178 if (locked_page) {
2179 page = locked_page;
2180 } else {
2181 if (i_size_read(inode) == 0)
2182 return;
2183 page = find_or_create_page(mapping, 0,
2184 mapping_gfp_constraint(mapping,
2185 ~__GFP_FS));
2186 if (!page)
2187 return;
2188 if (PageUptodate(page)) {
2189 unlock_page(page);
2190 put_page(page);
2191 return;
2192 }
2193 }
2194
2195 doutc(cl, "%p %llx.%llx len %zu locked_page %p\n", inode,
2196 ceph_vinop(inode), len, locked_page);
2197
2198 if (len > 0) {
2199 void *kaddr = kmap_atomic(page);
2200 memcpy(kaddr, data, len);
2201 kunmap_atomic(kaddr);
2202 }
2203
2204 if (page != locked_page) {
2205 if (len < PAGE_SIZE)
2206 zero_user_segment(page, len, PAGE_SIZE);
2207 else
2208 flush_dcache_page(page);
2209
2210 SetPageUptodate(page);
2211 unlock_page(page);
2212 put_page(page);
2213 }
2214 }
2215
ceph_uninline_data(struct file * file)2216 int ceph_uninline_data(struct file *file)
2217 {
2218 struct inode *inode = file_inode(file);
2219 struct ceph_inode_info *ci = ceph_inode(inode);
2220 struct ceph_fs_client *fsc = ceph_inode_to_fs_client(inode);
2221 struct ceph_client *cl = fsc->client;
2222 struct ceph_osd_request *req = NULL;
2223 struct ceph_cap_flush *prealloc_cf = NULL;
2224 struct folio *folio = NULL;
2225 struct ceph_snap_context *snapc = NULL;
2226 u64 inline_version = CEPH_INLINE_NONE;
2227 struct page *pages[1];
2228 int err = 0;
2229 u64 len;
2230
2231 spin_lock(&ci->i_ceph_lock);
2232 inline_version = ci->i_inline_version;
2233 spin_unlock(&ci->i_ceph_lock);
2234
2235 doutc(cl, "%llx.%llx inline_version %llu\n", ceph_vinop(inode),
2236 inline_version);
2237
2238 if (ceph_inode_is_shutdown(inode)) {
2239 err = -EIO;
2240 goto out;
2241 }
2242
2243 if (inline_version == CEPH_INLINE_NONE)
2244 return 0;
2245
2246 prealloc_cf = ceph_alloc_cap_flush();
2247 if (!prealloc_cf)
2248 return -ENOMEM;
2249
2250 if (inline_version == 1) /* initial version, no data */
2251 goto out_uninline;
2252
2253 down_read(&fsc->mdsc->snap_rwsem);
2254 spin_lock(&ci->i_ceph_lock);
2255 if (__ceph_have_pending_cap_snap(ci)) {
2256 struct ceph_cap_snap *capsnap =
2257 list_last_entry(&ci->i_cap_snaps,
2258 struct ceph_cap_snap,
2259 ci_item);
2260 snapc = ceph_get_snap_context(capsnap->context);
2261 } else {
2262 if (!ci->i_head_snapc) {
2263 ci->i_head_snapc = ceph_get_snap_context(
2264 ci->i_snap_realm->cached_context);
2265 }
2266 snapc = ceph_get_snap_context(ci->i_head_snapc);
2267 }
2268 spin_unlock(&ci->i_ceph_lock);
2269 up_read(&fsc->mdsc->snap_rwsem);
2270
2271 folio = read_mapping_folio(inode->i_mapping, 0, file);
2272 if (IS_ERR(folio)) {
2273 err = PTR_ERR(folio);
2274 goto out;
2275 }
2276
2277 folio_lock(folio);
2278
2279 len = i_size_read(inode);
2280 if (len > folio_size(folio))
2281 len = folio_size(folio);
2282
2283 req = ceph_osdc_new_request(&fsc->client->osdc, &ci->i_layout,
2284 ceph_vino(inode), 0, &len, 0, 1,
2285 CEPH_OSD_OP_CREATE, CEPH_OSD_FLAG_WRITE,
2286 snapc, 0, 0, false);
2287 if (IS_ERR(req)) {
2288 err = PTR_ERR(req);
2289 goto out_unlock;
2290 }
2291
2292 req->r_mtime = inode_get_mtime(inode);
2293 ceph_osdc_start_request(&fsc->client->osdc, req);
2294 err = ceph_osdc_wait_request(&fsc->client->osdc, req);
2295 ceph_osdc_put_request(req);
2296 if (err < 0)
2297 goto out_unlock;
2298
2299 req = ceph_osdc_new_request(&fsc->client->osdc, &ci->i_layout,
2300 ceph_vino(inode), 0, &len, 1, 3,
2301 CEPH_OSD_OP_WRITE, CEPH_OSD_FLAG_WRITE,
2302 snapc, ci->i_truncate_seq,
2303 ci->i_truncate_size, false);
2304 if (IS_ERR(req)) {
2305 err = PTR_ERR(req);
2306 goto out_unlock;
2307 }
2308
2309 pages[0] = folio_page(folio, 0);
2310 osd_req_op_extent_osd_data_pages(req, 1, pages, len, 0, false, false);
2311
2312 {
2313 __le64 xattr_buf = cpu_to_le64(inline_version);
2314 err = osd_req_op_xattr_init(req, 0, CEPH_OSD_OP_CMPXATTR,
2315 "inline_version", &xattr_buf,
2316 sizeof(xattr_buf),
2317 CEPH_OSD_CMPXATTR_OP_GT,
2318 CEPH_OSD_CMPXATTR_MODE_U64);
2319 if (err)
2320 goto out_put_req;
2321 }
2322
2323 {
2324 char xattr_buf[32];
2325 int xattr_len = snprintf(xattr_buf, sizeof(xattr_buf),
2326 "%llu", inline_version);
2327 err = osd_req_op_xattr_init(req, 2, CEPH_OSD_OP_SETXATTR,
2328 "inline_version",
2329 xattr_buf, xattr_len, 0, 0);
2330 if (err)
2331 goto out_put_req;
2332 }
2333
2334 req->r_mtime = inode_get_mtime(inode);
2335 ceph_osdc_start_request(&fsc->client->osdc, req);
2336 err = ceph_osdc_wait_request(&fsc->client->osdc, req);
2337
2338 ceph_update_write_metrics(&fsc->mdsc->metric, req->r_start_latency,
2339 req->r_end_latency, len, err);
2340
2341 out_uninline:
2342 if (!err) {
2343 int dirty;
2344
2345 /* Set to CAP_INLINE_NONE and dirty the caps */
2346 down_read(&fsc->mdsc->snap_rwsem);
2347 spin_lock(&ci->i_ceph_lock);
2348 ci->i_inline_version = CEPH_INLINE_NONE;
2349 dirty = __ceph_mark_dirty_caps(ci, CEPH_CAP_FILE_WR, &prealloc_cf);
2350 spin_unlock(&ci->i_ceph_lock);
2351 up_read(&fsc->mdsc->snap_rwsem);
2352 if (dirty)
2353 __mark_inode_dirty(inode, dirty);
2354 }
2355 out_put_req:
2356 ceph_osdc_put_request(req);
2357 if (err == -ECANCELED)
2358 err = 0;
2359 out_unlock:
2360 if (folio) {
2361 folio_unlock(folio);
2362 folio_put(folio);
2363 }
2364 out:
2365 ceph_put_snap_context(snapc);
2366 ceph_free_cap_flush(prealloc_cf);
2367 doutc(cl, "%llx.%llx inline_version %llu = %d\n",
2368 ceph_vinop(inode), inline_version, err);
2369 return err;
2370 }
2371
2372 static const struct vm_operations_struct ceph_vmops = {
2373 .fault = ceph_filemap_fault,
2374 .page_mkwrite = ceph_page_mkwrite,
2375 };
2376
ceph_mmap_prepare(struct vm_area_desc * desc)2377 int ceph_mmap_prepare(struct vm_area_desc *desc)
2378 {
2379 struct address_space *mapping = desc->file->f_mapping;
2380
2381 if (!mapping->a_ops->read_folio)
2382 return -ENOEXEC;
2383 desc->vm_ops = &ceph_vmops;
2384 return 0;
2385 }
2386
2387 enum {
2388 POOL_READ = 1,
2389 POOL_WRITE = 2,
2390 };
2391
__ceph_pool_perm_get(struct ceph_inode_info * ci,s64 pool,struct ceph_string * pool_ns)2392 static int __ceph_pool_perm_get(struct ceph_inode_info *ci,
2393 s64 pool, struct ceph_string *pool_ns)
2394 {
2395 struct ceph_fs_client *fsc = ceph_inode_to_fs_client(&ci->netfs.inode);
2396 struct ceph_mds_client *mdsc = fsc->mdsc;
2397 struct ceph_client *cl = fsc->client;
2398 struct ceph_osd_request *rd_req = NULL, *wr_req = NULL;
2399 struct rb_node **p, *parent;
2400 struct ceph_pool_perm *perm;
2401 struct page **pages;
2402 size_t pool_ns_len;
2403 int err = 0, err2 = 0, have = 0;
2404
2405 down_read(&mdsc->pool_perm_rwsem);
2406 p = &mdsc->pool_perm_tree.rb_node;
2407 while (*p) {
2408 perm = rb_entry(*p, struct ceph_pool_perm, node);
2409 if (pool < perm->pool)
2410 p = &(*p)->rb_left;
2411 else if (pool > perm->pool)
2412 p = &(*p)->rb_right;
2413 else {
2414 int ret = ceph_compare_string(pool_ns,
2415 perm->pool_ns,
2416 perm->pool_ns_len);
2417 if (ret < 0)
2418 p = &(*p)->rb_left;
2419 else if (ret > 0)
2420 p = &(*p)->rb_right;
2421 else {
2422 have = perm->perm;
2423 break;
2424 }
2425 }
2426 }
2427 up_read(&mdsc->pool_perm_rwsem);
2428 if (*p)
2429 goto out;
2430
2431 if (pool_ns)
2432 doutc(cl, "pool %lld ns %.*s no perm cached\n", pool,
2433 (int)pool_ns->len, pool_ns->str);
2434 else
2435 doutc(cl, "pool %lld no perm cached\n", pool);
2436
2437 down_write(&mdsc->pool_perm_rwsem);
2438 p = &mdsc->pool_perm_tree.rb_node;
2439 parent = NULL;
2440 while (*p) {
2441 parent = *p;
2442 perm = rb_entry(parent, struct ceph_pool_perm, node);
2443 if (pool < perm->pool)
2444 p = &(*p)->rb_left;
2445 else if (pool > perm->pool)
2446 p = &(*p)->rb_right;
2447 else {
2448 int ret = ceph_compare_string(pool_ns,
2449 perm->pool_ns,
2450 perm->pool_ns_len);
2451 if (ret < 0)
2452 p = &(*p)->rb_left;
2453 else if (ret > 0)
2454 p = &(*p)->rb_right;
2455 else {
2456 have = perm->perm;
2457 break;
2458 }
2459 }
2460 }
2461 if (*p) {
2462 up_write(&mdsc->pool_perm_rwsem);
2463 goto out;
2464 }
2465
2466 rd_req = ceph_osdc_alloc_request(&fsc->client->osdc, NULL,
2467 1, false, GFP_NOFS);
2468 if (!rd_req) {
2469 err = -ENOMEM;
2470 goto out_unlock;
2471 }
2472
2473 rd_req->r_flags = CEPH_OSD_FLAG_READ;
2474 osd_req_op_init(rd_req, 0, CEPH_OSD_OP_STAT, 0);
2475 rd_req->r_base_oloc.pool = pool;
2476 if (pool_ns)
2477 rd_req->r_base_oloc.pool_ns = ceph_get_string(pool_ns);
2478 ceph_oid_printf(&rd_req->r_base_oid, "%llx.00000000", ci->i_vino.ino);
2479
2480 err = ceph_osdc_alloc_messages(rd_req, GFP_NOFS);
2481 if (err)
2482 goto out_unlock;
2483
2484 wr_req = ceph_osdc_alloc_request(&fsc->client->osdc, NULL,
2485 1, false, GFP_NOFS);
2486 if (!wr_req) {
2487 err = -ENOMEM;
2488 goto out_unlock;
2489 }
2490
2491 wr_req->r_flags = CEPH_OSD_FLAG_WRITE;
2492 osd_req_op_init(wr_req, 0, CEPH_OSD_OP_CREATE, CEPH_OSD_OP_FLAG_EXCL);
2493 ceph_oloc_copy(&wr_req->r_base_oloc, &rd_req->r_base_oloc);
2494 ceph_oid_copy(&wr_req->r_base_oid, &rd_req->r_base_oid);
2495
2496 err = ceph_osdc_alloc_messages(wr_req, GFP_NOFS);
2497 if (err)
2498 goto out_unlock;
2499
2500 /* one page should be large enough for STAT data */
2501 pages = ceph_alloc_page_vector(1, GFP_KERNEL);
2502 if (IS_ERR(pages)) {
2503 err = PTR_ERR(pages);
2504 goto out_unlock;
2505 }
2506
2507 osd_req_op_raw_data_in_pages(rd_req, 0, pages, PAGE_SIZE,
2508 0, false, true);
2509 ceph_osdc_start_request(&fsc->client->osdc, rd_req);
2510
2511 wr_req->r_mtime = inode_get_mtime(&ci->netfs.inode);
2512 ceph_osdc_start_request(&fsc->client->osdc, wr_req);
2513
2514 err = ceph_osdc_wait_request(&fsc->client->osdc, rd_req);
2515 err2 = ceph_osdc_wait_request(&fsc->client->osdc, wr_req);
2516
2517 if (err >= 0 || err == -ENOENT)
2518 have |= POOL_READ;
2519 else if (err != -EPERM) {
2520 if (err == -EBLOCKLISTED)
2521 fsc->blocklisted = true;
2522 goto out_unlock;
2523 }
2524
2525 if (err2 == 0 || err2 == -EEXIST)
2526 have |= POOL_WRITE;
2527 else if (err2 != -EPERM) {
2528 if (err2 == -EBLOCKLISTED)
2529 fsc->blocklisted = true;
2530 err = err2;
2531 goto out_unlock;
2532 }
2533
2534 pool_ns_len = pool_ns ? pool_ns->len : 0;
2535 perm = kmalloc_flex(*perm, pool_ns, pool_ns_len + 1, GFP_NOFS);
2536 if (!perm) {
2537 err = -ENOMEM;
2538 goto out_unlock;
2539 }
2540
2541 perm->pool = pool;
2542 perm->perm = have;
2543 perm->pool_ns_len = pool_ns_len;
2544 if (pool_ns_len > 0)
2545 memcpy(perm->pool_ns, pool_ns->str, pool_ns_len);
2546 perm->pool_ns[pool_ns_len] = 0;
2547
2548 rb_link_node(&perm->node, parent, p);
2549 rb_insert_color(&perm->node, &mdsc->pool_perm_tree);
2550 err = 0;
2551 out_unlock:
2552 up_write(&mdsc->pool_perm_rwsem);
2553
2554 ceph_osdc_put_request(rd_req);
2555 ceph_osdc_put_request(wr_req);
2556 out:
2557 if (!err)
2558 err = have;
2559 if (pool_ns)
2560 doutc(cl, "pool %lld ns %.*s result = %d\n", pool,
2561 (int)pool_ns->len, pool_ns->str, err);
2562 else
2563 doutc(cl, "pool %lld result = %d\n", pool, err);
2564 return err;
2565 }
2566
ceph_pool_perm_check(struct inode * inode,int need)2567 int ceph_pool_perm_check(struct inode *inode, int need)
2568 {
2569 struct ceph_client *cl = ceph_inode_to_client(inode);
2570 struct ceph_inode_info *ci = ceph_inode(inode);
2571 struct ceph_string *pool_ns;
2572 s64 pool;
2573 int ret;
2574 unsigned long flags;
2575
2576 /* Only need to do this for regular files */
2577 if (!S_ISREG(inode->i_mode))
2578 return 0;
2579
2580 if (ci->i_vino.snap != CEPH_NOSNAP) {
2581 /*
2582 * Pool permission check needs to write to the first object.
2583 * But for snapshot, head of the first object may have already
2584 * been deleted. Skip check to avoid creating orphan object.
2585 */
2586 return 0;
2587 }
2588
2589 if (ceph_test_mount_opt(ceph_inode_to_fs_client(inode),
2590 NOPOOLPERM))
2591 return 0;
2592
2593 spin_lock(&ci->i_ceph_lock);
2594 flags = ci->i_ceph_flags;
2595 pool = ci->i_layout.pool_id;
2596 spin_unlock(&ci->i_ceph_lock);
2597 check:
2598 if (flags & CEPH_I_POOL_PERM) {
2599 if ((need & CEPH_CAP_FILE_RD) && !(flags & CEPH_I_POOL_RD)) {
2600 doutc(cl, "pool %lld no read perm\n", pool);
2601 return -EPERM;
2602 }
2603 if ((need & CEPH_CAP_FILE_WR) && !(flags & CEPH_I_POOL_WR)) {
2604 doutc(cl, "pool %lld no write perm\n", pool);
2605 return -EPERM;
2606 }
2607 return 0;
2608 }
2609
2610 pool_ns = ceph_try_get_string(ci->i_layout.pool_ns);
2611 ret = __ceph_pool_perm_get(ci, pool, pool_ns);
2612 ceph_put_string(pool_ns);
2613 if (ret < 0)
2614 return ret;
2615
2616 spin_lock(&ci->i_ceph_lock);
2617 if (pool == ci->i_layout.pool_id &&
2618 pool_ns == rcu_dereference_raw(ci->i_layout.pool_ns)) {
2619 set_bit(CEPH_I_POOL_PERM_BIT, &ci->i_ceph_flags);
2620 if (ret & POOL_READ)
2621 set_bit(CEPH_I_POOL_RD_BIT, &ci->i_ceph_flags);
2622 if (ret & POOL_WRITE)
2623 set_bit(CEPH_I_POOL_WR_BIT, &ci->i_ceph_flags);
2624 } else {
2625 pool = ci->i_layout.pool_id;
2626 }
2627 /* Re-read flags under the lock so check: sees the updated bits. */
2628 flags = ci->i_ceph_flags;
2629 spin_unlock(&ci->i_ceph_lock);
2630 goto check;
2631 }
2632
ceph_pool_perm_destroy(struct ceph_mds_client * mdsc)2633 void ceph_pool_perm_destroy(struct ceph_mds_client *mdsc)
2634 {
2635 struct ceph_pool_perm *perm;
2636 struct rb_node *n;
2637
2638 while (!RB_EMPTY_ROOT(&mdsc->pool_perm_tree)) {
2639 n = rb_first(&mdsc->pool_perm_tree);
2640 perm = rb_entry(n, struct ceph_pool_perm, node);
2641 rb_erase(n, &mdsc->pool_perm_tree);
2642 kfree(perm);
2643 }
2644 }
2645