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