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