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