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