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