1 // SPDX-License-Identifier: GPL-2.0 2 #include <linux/ceph/ceph_debug.h> 3 4 #include <linux/fs.h> 5 #include <linux/kernel.h> 6 #include <linux/sched/signal.h> 7 #include <linux/slab.h> 8 #include <linux/vmalloc.h> 9 #include <linux/wait.h> 10 #include <linux/writeback.h> 11 #include <linux/iversion.h> 12 #include <linux/filelock.h> 13 #include <linux/jiffies.h> 14 15 #include "super.h" 16 #include "mds_client.h" 17 #include "cache.h" 18 #include "crypto.h" 19 #include <linux/ceph/decode.h> 20 #include <linux/ceph/messenger.h> 21 #include <trace/events/ceph.h> 22 23 /* 24 * Capability management 25 * 26 * The Ceph metadata servers control client access to inode metadata 27 * and file data by issuing capabilities, granting clients permission 28 * to read and/or write both inode field and file data to OSDs 29 * (storage nodes). Each capability consists of a set of bits 30 * indicating which operations are allowed. 31 * 32 * If the client holds a *_SHARED cap, the client has a coherent value 33 * that can be safely read from the cached inode. 34 * 35 * In the case of a *_EXCL (exclusive) or FILE_WR capabilities, the 36 * client is allowed to change inode attributes (e.g., file size, 37 * mtime), note its dirty state in the ceph_cap, and asynchronously 38 * flush that metadata change to the MDS. 39 * 40 * In the event of a conflicting operation (perhaps by another 41 * client), the MDS will revoke the conflicting client capabilities. 42 * 43 * In order for a client to cache an inode, it must hold a capability 44 * with at least one MDS server. When inodes are released, release 45 * notifications are batched and periodically sent en masse to the MDS 46 * cluster to release server state. 47 */ 48 49 static u64 __get_oldest_flush_tid(struct ceph_mds_client *mdsc); 50 static void __kick_flushing_caps(struct ceph_mds_client *mdsc, 51 struct ceph_mds_session *session, 52 struct ceph_inode_info *ci, 53 u64 oldest_flush_tid); 54 55 /* 56 * Generate readable cap strings for debugging output. 57 */ 58 #define MAX_CAP_STR 20 59 static char cap_str[MAX_CAP_STR][40]; 60 static DEFINE_SPINLOCK(cap_str_lock); 61 static int last_cap_str; 62 63 static char *gcap_string(char *s, int c) 64 { 65 if (c & CEPH_CAP_GSHARED) 66 *s++ = 's'; 67 if (c & CEPH_CAP_GEXCL) 68 *s++ = 'x'; 69 if (c & CEPH_CAP_GCACHE) 70 *s++ = 'c'; 71 if (c & CEPH_CAP_GRD) 72 *s++ = 'r'; 73 if (c & CEPH_CAP_GWR) 74 *s++ = 'w'; 75 if (c & CEPH_CAP_GBUFFER) 76 *s++ = 'b'; 77 if (c & CEPH_CAP_GWREXTEND) 78 *s++ = 'a'; 79 if (c & CEPH_CAP_GLAZYIO) 80 *s++ = 'l'; 81 return s; 82 } 83 84 const char *ceph_cap_string(int caps) 85 { 86 int i; 87 char *s; 88 int c; 89 90 spin_lock(&cap_str_lock); 91 i = last_cap_str++; 92 if (last_cap_str == MAX_CAP_STR) 93 last_cap_str = 0; 94 spin_unlock(&cap_str_lock); 95 96 s = cap_str[i]; 97 98 if (caps & CEPH_CAP_PIN) 99 *s++ = 'p'; 100 101 c = (caps >> CEPH_CAP_SAUTH) & 3; 102 if (c) { 103 *s++ = 'A'; 104 s = gcap_string(s, c); 105 } 106 107 c = (caps >> CEPH_CAP_SLINK) & 3; 108 if (c) { 109 *s++ = 'L'; 110 s = gcap_string(s, c); 111 } 112 113 c = (caps >> CEPH_CAP_SXATTR) & 3; 114 if (c) { 115 *s++ = 'X'; 116 s = gcap_string(s, c); 117 } 118 119 c = caps >> CEPH_CAP_SFILE; 120 if (c) { 121 *s++ = 'F'; 122 s = gcap_string(s, c); 123 } 124 125 if (s == cap_str[i]) 126 *s++ = '-'; 127 *s = 0; 128 return cap_str[i]; 129 } 130 131 void ceph_caps_init(struct ceph_mds_client *mdsc) 132 { 133 INIT_LIST_HEAD(&mdsc->caps_list); 134 spin_lock_init(&mdsc->caps_list_lock); 135 } 136 137 void ceph_caps_finalize(struct ceph_mds_client *mdsc) 138 { 139 struct ceph_cap *cap; 140 141 spin_lock(&mdsc->caps_list_lock); 142 while (!list_empty(&mdsc->caps_list)) { 143 cap = list_first_entry(&mdsc->caps_list, 144 struct ceph_cap, caps_item); 145 list_del(&cap->caps_item); 146 kmem_cache_free(ceph_cap_cachep, cap); 147 } 148 mdsc->caps_total_count = 0; 149 mdsc->caps_avail_count = 0; 150 mdsc->caps_use_count = 0; 151 mdsc->caps_reserve_count = 0; 152 mdsc->caps_min_count = 0; 153 spin_unlock(&mdsc->caps_list_lock); 154 } 155 156 void ceph_adjust_caps_max_min(struct ceph_mds_client *mdsc, 157 struct ceph_mount_options *fsopt) 158 { 159 spin_lock(&mdsc->caps_list_lock); 160 mdsc->caps_min_count = fsopt->max_readdir; 161 if (mdsc->caps_min_count < 1024) 162 mdsc->caps_min_count = 1024; 163 mdsc->caps_use_max = fsopt->caps_max; 164 if (mdsc->caps_use_max > 0 && 165 mdsc->caps_use_max < mdsc->caps_min_count) 166 mdsc->caps_use_max = mdsc->caps_min_count; 167 spin_unlock(&mdsc->caps_list_lock); 168 } 169 170 static void __ceph_unreserve_caps(struct ceph_mds_client *mdsc, int nr_caps) 171 { 172 struct ceph_cap *cap; 173 int i; 174 175 if (nr_caps) { 176 BUG_ON(mdsc->caps_reserve_count < nr_caps); 177 mdsc->caps_reserve_count -= nr_caps; 178 if (mdsc->caps_avail_count >= 179 mdsc->caps_reserve_count + mdsc->caps_min_count) { 180 mdsc->caps_total_count -= nr_caps; 181 for (i = 0; i < nr_caps; i++) { 182 cap = list_first_entry(&mdsc->caps_list, 183 struct ceph_cap, caps_item); 184 list_del(&cap->caps_item); 185 kmem_cache_free(ceph_cap_cachep, cap); 186 } 187 } else { 188 mdsc->caps_avail_count += nr_caps; 189 } 190 191 doutc(mdsc->fsc->client, 192 "caps %d = %d used + %d resv + %d avail\n", 193 mdsc->caps_total_count, mdsc->caps_use_count, 194 mdsc->caps_reserve_count, mdsc->caps_avail_count); 195 BUG_ON(mdsc->caps_total_count != mdsc->caps_use_count + 196 mdsc->caps_reserve_count + 197 mdsc->caps_avail_count); 198 } 199 } 200 201 /* 202 * Called under mdsc->mutex. 203 */ 204 int ceph_reserve_caps(struct ceph_mds_client *mdsc, 205 struct ceph_cap_reservation *ctx, int need) 206 { 207 struct ceph_client *cl = mdsc->fsc->client; 208 int i, j; 209 struct ceph_cap *cap; 210 int have; 211 int alloc = 0; 212 int max_caps; 213 int err = 0; 214 bool trimmed = false; 215 struct ceph_mds_session *s; 216 LIST_HEAD(newcaps); 217 218 doutc(cl, "ctx=%p need=%d\n", ctx, need); 219 220 /* first reserve any caps that are already allocated */ 221 spin_lock(&mdsc->caps_list_lock); 222 if (mdsc->caps_avail_count >= need) 223 have = need; 224 else 225 have = mdsc->caps_avail_count; 226 mdsc->caps_avail_count -= have; 227 mdsc->caps_reserve_count += have; 228 BUG_ON(mdsc->caps_total_count != mdsc->caps_use_count + 229 mdsc->caps_reserve_count + 230 mdsc->caps_avail_count); 231 spin_unlock(&mdsc->caps_list_lock); 232 233 for (i = have; i < need; ) { 234 cap = kmem_cache_alloc(ceph_cap_cachep, GFP_NOFS); 235 if (cap) { 236 list_add(&cap->caps_item, &newcaps); 237 alloc++; 238 i++; 239 continue; 240 } 241 242 if (!trimmed) { 243 for (j = 0; j < mdsc->max_sessions; j++) { 244 s = __ceph_lookup_mds_session(mdsc, j); 245 if (!s) 246 continue; 247 mutex_unlock(&mdsc->mutex); 248 249 mutex_lock(&s->s_mutex); 250 max_caps = s->s_nr_caps - (need - i); 251 ceph_trim_caps(mdsc, s, max_caps); 252 mutex_unlock(&s->s_mutex); 253 254 ceph_put_mds_session(s); 255 mutex_lock(&mdsc->mutex); 256 } 257 trimmed = true; 258 259 spin_lock(&mdsc->caps_list_lock); 260 if (mdsc->caps_avail_count) { 261 int more_have; 262 if (mdsc->caps_avail_count >= need - i) 263 more_have = need - i; 264 else 265 more_have = mdsc->caps_avail_count; 266 267 i += more_have; 268 have += more_have; 269 mdsc->caps_avail_count -= more_have; 270 mdsc->caps_reserve_count += more_have; 271 272 } 273 spin_unlock(&mdsc->caps_list_lock); 274 275 continue; 276 } 277 278 pr_warn_client(cl, "ctx=%p ENOMEM need=%d got=%d\n", ctx, need, 279 have + alloc); 280 err = -ENOMEM; 281 break; 282 } 283 284 if (!err) { 285 BUG_ON(have + alloc != need); 286 ctx->count = need; 287 ctx->used = 0; 288 } 289 290 spin_lock(&mdsc->caps_list_lock); 291 mdsc->caps_total_count += alloc; 292 mdsc->caps_reserve_count += alloc; 293 list_splice(&newcaps, &mdsc->caps_list); 294 295 BUG_ON(mdsc->caps_total_count != mdsc->caps_use_count + 296 mdsc->caps_reserve_count + 297 mdsc->caps_avail_count); 298 299 if (err) 300 __ceph_unreserve_caps(mdsc, have + alloc); 301 302 spin_unlock(&mdsc->caps_list_lock); 303 304 doutc(cl, "ctx=%p %d = %d used + %d resv + %d avail\n", ctx, 305 mdsc->caps_total_count, mdsc->caps_use_count, 306 mdsc->caps_reserve_count, mdsc->caps_avail_count); 307 return err; 308 } 309 310 void ceph_unreserve_caps(struct ceph_mds_client *mdsc, 311 struct ceph_cap_reservation *ctx) 312 { 313 struct ceph_client *cl = mdsc->fsc->client; 314 bool reclaim = false; 315 if (!ctx->count) 316 return; 317 318 doutc(cl, "ctx=%p count=%d\n", ctx, ctx->count); 319 spin_lock(&mdsc->caps_list_lock); 320 __ceph_unreserve_caps(mdsc, ctx->count); 321 ctx->count = 0; 322 323 if (mdsc->caps_use_max > 0 && 324 mdsc->caps_use_count > mdsc->caps_use_max) 325 reclaim = true; 326 spin_unlock(&mdsc->caps_list_lock); 327 328 if (reclaim) 329 ceph_reclaim_caps_nr(mdsc, ctx->used); 330 } 331 332 struct ceph_cap *ceph_get_cap(struct ceph_mds_client *mdsc, 333 struct ceph_cap_reservation *ctx) 334 { 335 struct ceph_client *cl = mdsc->fsc->client; 336 struct ceph_cap *cap = NULL; 337 338 /* temporary, until we do something about cap import/export */ 339 if (!ctx) { 340 cap = kmem_cache_alloc(ceph_cap_cachep, GFP_NOFS); 341 if (cap) { 342 spin_lock(&mdsc->caps_list_lock); 343 mdsc->caps_use_count++; 344 mdsc->caps_total_count++; 345 spin_unlock(&mdsc->caps_list_lock); 346 } else { 347 spin_lock(&mdsc->caps_list_lock); 348 if (mdsc->caps_avail_count) { 349 BUG_ON(list_empty(&mdsc->caps_list)); 350 351 mdsc->caps_avail_count--; 352 mdsc->caps_use_count++; 353 cap = list_first_entry(&mdsc->caps_list, 354 struct ceph_cap, caps_item); 355 list_del(&cap->caps_item); 356 357 BUG_ON(mdsc->caps_total_count != mdsc->caps_use_count + 358 mdsc->caps_reserve_count + mdsc->caps_avail_count); 359 } 360 spin_unlock(&mdsc->caps_list_lock); 361 } 362 363 return cap; 364 } 365 366 spin_lock(&mdsc->caps_list_lock); 367 doutc(cl, "ctx=%p (%d) %d = %d used + %d resv + %d avail\n", ctx, 368 ctx->count, mdsc->caps_total_count, mdsc->caps_use_count, 369 mdsc->caps_reserve_count, mdsc->caps_avail_count); 370 BUG_ON(!ctx->count); 371 BUG_ON(ctx->count > mdsc->caps_reserve_count); 372 BUG_ON(list_empty(&mdsc->caps_list)); 373 374 ctx->count--; 375 ctx->used++; 376 mdsc->caps_reserve_count--; 377 mdsc->caps_use_count++; 378 379 cap = list_first_entry(&mdsc->caps_list, struct ceph_cap, caps_item); 380 list_del(&cap->caps_item); 381 382 BUG_ON(mdsc->caps_total_count != mdsc->caps_use_count + 383 mdsc->caps_reserve_count + mdsc->caps_avail_count); 384 spin_unlock(&mdsc->caps_list_lock); 385 return cap; 386 } 387 388 void ceph_put_cap(struct ceph_mds_client *mdsc, struct ceph_cap *cap) 389 { 390 struct ceph_client *cl = mdsc->fsc->client; 391 392 spin_lock(&mdsc->caps_list_lock); 393 doutc(cl, "%p %d = %d used + %d resv + %d avail\n", cap, 394 mdsc->caps_total_count, mdsc->caps_use_count, 395 mdsc->caps_reserve_count, mdsc->caps_avail_count); 396 mdsc->caps_use_count--; 397 /* 398 * Keep some preallocated caps around (ceph_min_count), to 399 * avoid lots of free/alloc churn. 400 */ 401 if (mdsc->caps_avail_count >= mdsc->caps_reserve_count + 402 mdsc->caps_min_count) { 403 mdsc->caps_total_count--; 404 kmem_cache_free(ceph_cap_cachep, cap); 405 } else { 406 mdsc->caps_avail_count++; 407 list_add(&cap->caps_item, &mdsc->caps_list); 408 } 409 410 BUG_ON(mdsc->caps_total_count != mdsc->caps_use_count + 411 mdsc->caps_reserve_count + mdsc->caps_avail_count); 412 spin_unlock(&mdsc->caps_list_lock); 413 } 414 415 void ceph_reservation_status(struct ceph_fs_client *fsc, 416 int *total, int *avail, int *used, int *reserved, 417 int *min) 418 { 419 struct ceph_mds_client *mdsc = fsc->mdsc; 420 421 spin_lock(&mdsc->caps_list_lock); 422 423 if (total) 424 *total = mdsc->caps_total_count; 425 if (avail) 426 *avail = mdsc->caps_avail_count; 427 if (used) 428 *used = mdsc->caps_use_count; 429 if (reserved) 430 *reserved = mdsc->caps_reserve_count; 431 if (min) 432 *min = mdsc->caps_min_count; 433 434 spin_unlock(&mdsc->caps_list_lock); 435 } 436 437 /* 438 * Find ceph_cap for given mds, if any. 439 * 440 * Called with i_ceph_lock held. 441 */ 442 struct ceph_cap *__get_cap_for_mds(struct ceph_inode_info *ci, int mds) 443 { 444 struct ceph_cap *cap; 445 struct rb_node *n = ci->i_caps.rb_node; 446 447 while (n) { 448 cap = rb_entry(n, struct ceph_cap, ci_node); 449 if (mds < cap->mds) 450 n = n->rb_left; 451 else if (mds > cap->mds) 452 n = n->rb_right; 453 else 454 return cap; 455 } 456 return NULL; 457 } 458 459 struct ceph_cap *ceph_get_cap_for_mds(struct ceph_inode_info *ci, int mds) 460 { 461 struct ceph_cap *cap; 462 463 spin_lock(&ci->i_ceph_lock); 464 cap = __get_cap_for_mds(ci, mds); 465 spin_unlock(&ci->i_ceph_lock); 466 return cap; 467 } 468 469 /* 470 * Called under i_ceph_lock. 471 */ 472 static void __insert_cap_node(struct ceph_inode_info *ci, 473 struct ceph_cap *new) 474 { 475 struct rb_node **p = &ci->i_caps.rb_node; 476 struct rb_node *parent = NULL; 477 struct ceph_cap *cap = NULL; 478 479 while (*p) { 480 parent = *p; 481 cap = rb_entry(parent, struct ceph_cap, ci_node); 482 if (new->mds < cap->mds) 483 p = &(*p)->rb_left; 484 else if (new->mds > cap->mds) 485 p = &(*p)->rb_right; 486 else 487 BUG(); 488 } 489 490 rb_link_node(&new->ci_node, parent, p); 491 rb_insert_color(&new->ci_node, &ci->i_caps); 492 } 493 494 /* 495 * (re)set cap hold timeouts, which control the delayed release 496 * of unused caps back to the MDS. Should be called on cap use. 497 */ 498 static void __cap_set_timeouts(struct ceph_mds_client *mdsc, 499 struct ceph_inode_info *ci) 500 { 501 struct inode *inode = &ci->netfs.inode; 502 struct ceph_mount_options *opt = mdsc->fsc->mount_options; 503 504 ci->i_hold_caps_max = round_jiffies(jiffies + 505 opt->caps_wanted_delay_max * HZ); 506 doutc(mdsc->fsc->client, "%p %llx.%llx %lu\n", inode, 507 ceph_vinop(inode), ci->i_hold_caps_max - jiffies); 508 } 509 510 /* 511 * (Re)queue cap at the end of the delayed cap release list. 512 * 513 * If I_FLUSH is set, leave the inode at the front of the list. 514 * 515 * Caller holds i_ceph_lock 516 * -> we take mdsc->cap_delay_lock 517 */ 518 static void __cap_delay_requeue(struct ceph_mds_client *mdsc, 519 struct ceph_inode_info *ci) 520 { 521 struct inode *inode = &ci->netfs.inode; 522 523 doutc(mdsc->fsc->client, "%p %llx.%llx flags 0x%lx at %lu\n", 524 inode, ceph_vinop(inode), ci->i_ceph_flags, 525 ci->i_hold_caps_max); 526 if (!mdsc->stopping) { 527 spin_lock(&mdsc->cap_delay_lock); 528 if (!list_empty(&ci->i_cap_delay_list)) { 529 if (ci->i_ceph_flags & CEPH_I_FLUSH) 530 goto no_change; 531 list_del_init(&ci->i_cap_delay_list); 532 } 533 __cap_set_timeouts(mdsc, ci); 534 list_add_tail(&ci->i_cap_delay_list, &mdsc->cap_delay_list); 535 no_change: 536 spin_unlock(&mdsc->cap_delay_lock); 537 } 538 } 539 540 /* 541 * Queue an inode for immediate writeback. Mark inode with I_FLUSH, 542 * indicating we should send a cap message to flush dirty metadata 543 * asap, and move to the front of the delayed cap list. 544 */ 545 static void __cap_delay_requeue_front(struct ceph_mds_client *mdsc, 546 struct ceph_inode_info *ci) 547 { 548 struct inode *inode = &ci->netfs.inode; 549 550 doutc(mdsc->fsc->client, "%p %llx.%llx\n", inode, ceph_vinop(inode)); 551 spin_lock(&mdsc->cap_delay_lock); 552 set_bit(CEPH_I_FLUSH_BIT, &ci->i_ceph_flags); 553 if (!list_empty(&ci->i_cap_delay_list)) 554 list_del_init(&ci->i_cap_delay_list); 555 list_add(&ci->i_cap_delay_list, &mdsc->cap_delay_list); 556 spin_unlock(&mdsc->cap_delay_lock); 557 } 558 559 /* 560 * Cancel delayed work on cap. 561 * 562 * Caller must hold i_ceph_lock. 563 */ 564 static void __cap_delay_cancel(struct ceph_mds_client *mdsc, 565 struct ceph_inode_info *ci) 566 { 567 struct inode *inode = &ci->netfs.inode; 568 569 doutc(mdsc->fsc->client, "%p %llx.%llx\n", inode, ceph_vinop(inode)); 570 if (list_empty(&ci->i_cap_delay_list)) 571 return; 572 spin_lock(&mdsc->cap_delay_lock); 573 list_del_init(&ci->i_cap_delay_list); 574 spin_unlock(&mdsc->cap_delay_lock); 575 } 576 577 /* Common issue checks for add_cap, handle_cap_grant. */ 578 static void __check_cap_issue(struct ceph_inode_info *ci, struct ceph_cap *cap, 579 unsigned issued) 580 { 581 struct inode *inode = &ci->netfs.inode; 582 struct ceph_client *cl = ceph_inode_to_client(inode); 583 584 unsigned had = __ceph_caps_issued(ci, NULL); 585 586 lockdep_assert_held(&ci->i_ceph_lock); 587 588 /* 589 * Each time we receive FILE_CACHE anew, we increment 590 * i_rdcache_gen. 591 */ 592 if (S_ISREG(ci->netfs.inode.i_mode) && 593 (issued & (CEPH_CAP_FILE_CACHE|CEPH_CAP_FILE_LAZYIO)) && 594 (had & (CEPH_CAP_FILE_CACHE|CEPH_CAP_FILE_LAZYIO)) == 0) { 595 ci->i_rdcache_gen++; 596 } 597 598 /* 599 * If FILE_SHARED is newly issued, mark dir not complete. We don't 600 * know what happened to this directory while we didn't have the cap. 601 * If FILE_SHARED is being revoked, also mark dir not complete. It 602 * stops on-going cached readdir. 603 */ 604 if ((issued & CEPH_CAP_FILE_SHARED) != (had & CEPH_CAP_FILE_SHARED)) { 605 if (issued & CEPH_CAP_FILE_SHARED) 606 atomic_inc(&ci->i_shared_gen); 607 if (S_ISDIR(ci->netfs.inode.i_mode)) { 608 doutc(cl, " marking %p NOT complete\n", inode); 609 __ceph_dir_clear_complete(ci); 610 } 611 } 612 613 /* Wipe saved layout if we're losing DIR_CREATE caps */ 614 if (S_ISDIR(ci->netfs.inode.i_mode) && (had & CEPH_CAP_DIR_CREATE) && 615 !(issued & CEPH_CAP_DIR_CREATE)) { 616 ceph_put_string(rcu_dereference_raw(ci->i_cached_layout.pool_ns)); 617 memset(&ci->i_cached_layout, 0, sizeof(ci->i_cached_layout)); 618 } 619 } 620 621 /** 622 * change_auth_cap_ses - move inode to appropriate lists when auth caps change 623 * @ci: inode to be moved 624 * @session: new auth caps session 625 */ 626 void change_auth_cap_ses(struct ceph_inode_info *ci, 627 struct ceph_mds_session *session) 628 { 629 lockdep_assert_held(&ci->i_ceph_lock); 630 631 if (list_empty(&ci->i_dirty_item) && list_empty(&ci->i_flushing_item)) 632 return; 633 634 spin_lock(&session->s_mdsc->cap_dirty_lock); 635 if (!list_empty(&ci->i_dirty_item)) 636 list_move(&ci->i_dirty_item, &session->s_cap_dirty); 637 if (!list_empty(&ci->i_flushing_item)) 638 list_move_tail(&ci->i_flushing_item, &session->s_cap_flushing); 639 spin_unlock(&session->s_mdsc->cap_dirty_lock); 640 } 641 642 /* 643 * Add a capability under the given MDS session. 644 * 645 * Caller should hold session snap_rwsem (read) and ci->i_ceph_lock 646 * 647 * @fmode is the open file mode, if we are opening a file, otherwise 648 * it is < 0. (This is so we can atomically add the cap and add an 649 * open file reference to it.) 650 */ 651 void ceph_add_cap(struct inode *inode, 652 struct ceph_mds_session *session, u64 cap_id, 653 unsigned issued, unsigned wanted, 654 unsigned seq, unsigned mseq, u64 realmino, int flags, 655 struct ceph_cap **new_cap) 656 { 657 struct ceph_mds_client *mdsc = ceph_inode_to_fs_client(inode)->mdsc; 658 struct ceph_client *cl = ceph_inode_to_client(inode); 659 struct ceph_inode_info *ci = ceph_inode(inode); 660 struct ceph_cap *cap; 661 int mds = session->s_mds; 662 int actual_wanted; 663 u32 gen; 664 665 lockdep_assert_held(&ci->i_ceph_lock); 666 667 doutc(cl, "%p %llx.%llx mds%d cap %llx %s seq %d\n", inode, 668 ceph_vinop(inode), session->s_mds, cap_id, 669 ceph_cap_string(issued), seq); 670 671 gen = atomic_read(&session->s_cap_gen); 672 673 cap = __get_cap_for_mds(ci, mds); 674 if (!cap) { 675 cap = *new_cap; 676 *new_cap = NULL; 677 678 cap->issued = 0; 679 cap->implemented = 0; 680 cap->mds = mds; 681 cap->mds_wanted = 0; 682 cap->mseq = 0; 683 684 cap->ci = ci; 685 __insert_cap_node(ci, cap); 686 687 /* add to session cap list */ 688 cap->session = session; 689 spin_lock(&session->s_cap_lock); 690 list_add_tail(&cap->session_caps, &session->s_caps); 691 session->s_nr_caps++; 692 atomic64_inc(&mdsc->metric.total_caps); 693 spin_unlock(&session->s_cap_lock); 694 } else { 695 spin_lock(&session->s_cap_lock); 696 list_move_tail(&cap->session_caps, &session->s_caps); 697 spin_unlock(&session->s_cap_lock); 698 699 if (cap->cap_gen < gen) 700 cap->issued = cap->implemented = CEPH_CAP_PIN; 701 702 /* 703 * auth mds of the inode changed. we received the cap export 704 * message, but still haven't received the cap import message. 705 * handle_cap_export() updated the new auth MDS' cap. 706 * 707 * "ceph_seq_cmp(seq, cap->seq) <= 0" means we are processing 708 * a message that was send before the cap import message. So 709 * don't remove caps. 710 */ 711 if (ceph_seq_cmp(seq, cap->seq) <= 0) { 712 WARN_ON(cap != ci->i_auth_cap); 713 WARN_ON(cap->cap_id != cap_id); 714 seq = cap->seq; 715 mseq = cap->mseq; 716 issued |= cap->issued; 717 flags |= CEPH_CAP_FLAG_AUTH; 718 } 719 } 720 721 if (!ci->i_snap_realm || 722 ((flags & CEPH_CAP_FLAG_AUTH) && 723 realmino != (u64)-1 && ci->i_snap_realm->ino != realmino)) { 724 /* 725 * add this inode to the appropriate snap realm 726 */ 727 struct ceph_snap_realm *realm = ceph_lookup_snap_realm(mdsc, 728 realmino); 729 if (realm) 730 ceph_change_snap_realm(inode, realm); 731 else 732 WARN(1, "%s: couldn't find snap realm 0x%llx (ino 0x%llx oldrealm 0x%llx)\n", 733 __func__, realmino, ci->i_vino.ino, 734 ci->i_snap_realm ? ci->i_snap_realm->ino : 0); 735 } 736 737 __check_cap_issue(ci, cap, issued); 738 739 /* 740 * If we are issued caps we don't want, or the mds' wanted 741 * value appears to be off, queue a check so we'll release 742 * later and/or update the mds wanted value. 743 */ 744 actual_wanted = __ceph_caps_wanted(ci); 745 if ((wanted & ~actual_wanted) || 746 (issued & ~actual_wanted & CEPH_CAP_ANY_WR)) { 747 doutc(cl, "issued %s, mds wanted %s, actual %s, queueing\n", 748 ceph_cap_string(issued), ceph_cap_string(wanted), 749 ceph_cap_string(actual_wanted)); 750 __cap_delay_requeue(mdsc, ci); 751 } 752 753 if (flags & CEPH_CAP_FLAG_AUTH) { 754 if (!ci->i_auth_cap || 755 ceph_seq_cmp(ci->i_auth_cap->mseq, mseq) < 0) { 756 if (ci->i_auth_cap && 757 ci->i_auth_cap->session != cap->session) 758 change_auth_cap_ses(ci, cap->session); 759 ci->i_auth_cap = cap; 760 cap->mds_wanted = wanted; 761 } 762 } else { 763 WARN_ON(ci->i_auth_cap == cap); 764 } 765 766 doutc(cl, "inode %p %llx.%llx cap %p %s now %s seq %d mds%d\n", 767 inode, ceph_vinop(inode), cap, ceph_cap_string(issued), 768 ceph_cap_string(issued|cap->issued), seq, mds); 769 cap->cap_id = cap_id; 770 cap->issued = issued; 771 cap->implemented |= issued; 772 if (ceph_seq_cmp(mseq, cap->mseq) > 0) 773 cap->mds_wanted = wanted; 774 else 775 cap->mds_wanted |= wanted; 776 cap->seq = seq; 777 cap->issue_seq = seq; 778 cap->mseq = mseq; 779 cap->cap_gen = gen; 780 wake_up_all(&ci->i_cap_wq); 781 } 782 783 /* 784 * Return true if cap has not timed out and belongs to the current 785 * generation of the MDS session (i.e. has not gone 'stale' due to 786 * us losing touch with the mds). 787 */ 788 static int __cap_is_valid(struct ceph_cap *cap) 789 { 790 struct inode *inode = &cap->ci->netfs.inode; 791 struct ceph_client *cl = cap->session->s_mdsc->fsc->client; 792 unsigned long ttl; 793 u32 gen; 794 795 gen = atomic_read(&cap->session->s_cap_gen); 796 ttl = cap->session->s_cap_ttl; 797 798 if (cap->cap_gen < gen || time_after_eq(jiffies, ttl)) { 799 doutc(cl, "%p %llx.%llx cap %p issued %s but STALE (gen %u vs %u)\n", 800 inode, ceph_vinop(inode), cap, 801 ceph_cap_string(cap->issued), cap->cap_gen, gen); 802 return 0; 803 } 804 805 return 1; 806 } 807 808 /* 809 * Return set of valid cap bits issued to us. Note that caps time 810 * out, and may be invalidated in bulk if the client session times out 811 * and session->s_cap_gen is bumped. 812 */ 813 int __ceph_caps_issued(struct ceph_inode_info *ci, int *implemented) 814 { 815 struct inode *inode = &ci->netfs.inode; 816 struct ceph_client *cl = ceph_inode_to_client(inode); 817 int have = ci->i_snap_caps; 818 struct ceph_cap *cap; 819 struct rb_node *p; 820 821 if (implemented) 822 *implemented = 0; 823 for (p = rb_first(&ci->i_caps); p; p = rb_next(p)) { 824 cap = rb_entry(p, struct ceph_cap, ci_node); 825 if (!__cap_is_valid(cap)) 826 continue; 827 doutc(cl, "%p %llx.%llx cap %p issued %s\n", inode, 828 ceph_vinop(inode), cap, ceph_cap_string(cap->issued)); 829 have |= cap->issued; 830 if (implemented) 831 *implemented |= cap->implemented; 832 } 833 /* 834 * exclude caps issued by non-auth MDS, but are been revoking 835 * by the auth MDS. The non-auth MDS should be revoking/exporting 836 * these caps, but the message is delayed. 837 */ 838 if (ci->i_auth_cap) { 839 cap = ci->i_auth_cap; 840 have &= ~cap->implemented | cap->issued; 841 } 842 return have; 843 } 844 845 /* 846 * Get cap bits issued by caps other than @ocap 847 */ 848 int __ceph_caps_issued_other(struct ceph_inode_info *ci, struct ceph_cap *ocap) 849 { 850 int have = ci->i_snap_caps; 851 struct ceph_cap *cap; 852 struct rb_node *p; 853 854 for (p = rb_first(&ci->i_caps); p; p = rb_next(p)) { 855 cap = rb_entry(p, struct ceph_cap, ci_node); 856 if (cap == ocap) 857 continue; 858 if (!__cap_is_valid(cap)) 859 continue; 860 have |= cap->issued; 861 } 862 return have; 863 } 864 865 /* 866 * Move a cap to the end of the LRU (oldest caps at list head, newest 867 * at list tail). 868 */ 869 static void __touch_cap(struct ceph_cap *cap) 870 { 871 struct inode *inode = &cap->ci->netfs.inode; 872 struct ceph_mds_session *s = cap->session; 873 struct ceph_client *cl = s->s_mdsc->fsc->client; 874 875 spin_lock(&s->s_cap_lock); 876 if (!s->s_cap_iterator) { 877 doutc(cl, "%p %llx.%llx cap %p mds%d\n", inode, 878 ceph_vinop(inode), cap, s->s_mds); 879 list_move_tail(&cap->session_caps, &s->s_caps); 880 } else { 881 doutc(cl, "%p %llx.%llx cap %p mds%d NOP, iterating over caps\n", 882 inode, ceph_vinop(inode), cap, s->s_mds); 883 } 884 spin_unlock(&s->s_cap_lock); 885 } 886 887 /* 888 * Check if we hold the given mask. If so, move the cap(s) to the 889 * front of their respective LRUs. (This is the preferred way for 890 * callers to check for caps they want.) 891 */ 892 int __ceph_caps_issued_mask(struct ceph_inode_info *ci, int mask, int touch) 893 { 894 struct inode *inode = &ci->netfs.inode; 895 struct ceph_client *cl = ceph_inode_to_client(inode); 896 struct ceph_cap *cap; 897 struct rb_node *p; 898 int have = ci->i_snap_caps; 899 900 if ((have & mask) == mask) { 901 doutc(cl, "mask %p %llx.%llx snap issued %s (mask %s)\n", 902 inode, ceph_vinop(inode), ceph_cap_string(have), 903 ceph_cap_string(mask)); 904 return 1; 905 } 906 907 for (p = rb_first(&ci->i_caps); p; p = rb_next(p)) { 908 cap = rb_entry(p, struct ceph_cap, ci_node); 909 if (!__cap_is_valid(cap)) 910 continue; 911 if ((cap->issued & mask) == mask) { 912 doutc(cl, "mask %p %llx.%llx cap %p issued %s (mask %s)\n", 913 inode, ceph_vinop(inode), cap, 914 ceph_cap_string(cap->issued), 915 ceph_cap_string(mask)); 916 if (touch) 917 __touch_cap(cap); 918 return 1; 919 } 920 921 /* does a combination of caps satisfy mask? */ 922 have |= cap->issued; 923 if ((have & mask) == mask) { 924 doutc(cl, "mask %p %llx.%llx combo issued %s (mask %s)\n", 925 inode, ceph_vinop(inode), 926 ceph_cap_string(cap->issued), 927 ceph_cap_string(mask)); 928 if (touch) { 929 struct rb_node *q; 930 931 /* touch this + preceding caps */ 932 __touch_cap(cap); 933 for (q = rb_first(&ci->i_caps); q != p; 934 q = rb_next(q)) { 935 cap = rb_entry(q, struct ceph_cap, 936 ci_node); 937 if (!__cap_is_valid(cap)) 938 continue; 939 if (cap->issued & mask) 940 __touch_cap(cap); 941 } 942 } 943 return 1; 944 } 945 } 946 947 return 0; 948 } 949 950 int __ceph_caps_issued_mask_metric(struct ceph_inode_info *ci, int mask, 951 int touch) 952 { 953 struct ceph_fs_client *fsc = ceph_sb_to_fs_client(ci->netfs.inode.i_sb); 954 int r; 955 956 r = __ceph_caps_issued_mask(ci, mask, touch); 957 if (r) 958 ceph_update_cap_hit(&fsc->mdsc->metric); 959 else 960 ceph_update_cap_mis(&fsc->mdsc->metric); 961 return r; 962 } 963 964 /* 965 * Return true if mask caps are currently being revoked by an MDS. 966 */ 967 int __ceph_caps_revoking_other(struct ceph_inode_info *ci, 968 struct ceph_cap *ocap, int mask) 969 { 970 struct ceph_cap *cap; 971 struct rb_node *p; 972 973 for (p = rb_first(&ci->i_caps); p; p = rb_next(p)) { 974 cap = rb_entry(p, struct ceph_cap, ci_node); 975 if (cap != ocap && 976 (cap->implemented & ~cap->issued & mask)) 977 return 1; 978 } 979 return 0; 980 } 981 982 int __ceph_caps_used(struct ceph_inode_info *ci) 983 { 984 int used = 0; 985 if (ci->i_pin_ref) 986 used |= CEPH_CAP_PIN; 987 if (ci->i_rd_ref) 988 used |= CEPH_CAP_FILE_RD; 989 if (ci->i_rdcache_ref || 990 (S_ISREG(ci->netfs.inode.i_mode) && 991 ci->netfs.inode.i_data.nrpages)) 992 used |= CEPH_CAP_FILE_CACHE; 993 if (ci->i_wr_ref) 994 used |= CEPH_CAP_FILE_WR; 995 if (ci->i_wb_ref || ci->i_wrbuffer_ref) 996 used |= CEPH_CAP_FILE_BUFFER; 997 if (ci->i_fx_ref) 998 used |= CEPH_CAP_FILE_EXCL; 999 return used; 1000 } 1001 1002 #define FMODE_WAIT_BIAS 1000 1003 1004 /* 1005 * wanted, by virtue of open file modes 1006 */ 1007 int __ceph_caps_file_wanted(struct ceph_inode_info *ci) 1008 { 1009 const int PIN_SHIFT = ffs(CEPH_FILE_MODE_PIN); 1010 const int RD_SHIFT = ffs(CEPH_FILE_MODE_RD); 1011 const int WR_SHIFT = ffs(CEPH_FILE_MODE_WR); 1012 const int LAZY_SHIFT = ffs(CEPH_FILE_MODE_LAZY); 1013 struct ceph_mount_options *opt = 1014 ceph_inode_to_fs_client(&ci->netfs.inode)->mount_options; 1015 unsigned long used_cutoff = jiffies - opt->caps_wanted_delay_max * HZ; 1016 unsigned long idle_cutoff = jiffies - opt->caps_wanted_delay_min * HZ; 1017 1018 if (S_ISDIR(ci->netfs.inode.i_mode)) { 1019 int want = 0; 1020 1021 /* use used_cutoff here, to keep dir's wanted caps longer */ 1022 if (ci->i_nr_by_mode[RD_SHIFT] > 0 || 1023 time_after(ci->i_last_rd, used_cutoff)) 1024 want |= CEPH_CAP_ANY_SHARED; 1025 1026 if (ci->i_nr_by_mode[WR_SHIFT] > 0 || 1027 time_after(ci->i_last_wr, used_cutoff)) { 1028 want |= CEPH_CAP_ANY_SHARED | CEPH_CAP_FILE_EXCL; 1029 if (opt->flags & CEPH_MOUNT_OPT_ASYNC_DIROPS) 1030 want |= CEPH_CAP_ANY_DIR_OPS; 1031 } 1032 1033 if (want || ci->i_nr_by_mode[PIN_SHIFT] > 0) 1034 want |= CEPH_CAP_PIN; 1035 1036 return want; 1037 } else { 1038 int bits = 0; 1039 1040 if (ci->i_nr_by_mode[RD_SHIFT] > 0) { 1041 if (ci->i_nr_by_mode[RD_SHIFT] >= FMODE_WAIT_BIAS || 1042 time_after(ci->i_last_rd, used_cutoff)) 1043 bits |= 1 << RD_SHIFT; 1044 } else if (time_after(ci->i_last_rd, idle_cutoff)) { 1045 bits |= 1 << RD_SHIFT; 1046 } 1047 1048 if (ci->i_nr_by_mode[WR_SHIFT] > 0) { 1049 if (ci->i_nr_by_mode[WR_SHIFT] >= FMODE_WAIT_BIAS || 1050 time_after(ci->i_last_wr, used_cutoff)) 1051 bits |= 1 << WR_SHIFT; 1052 } else if (time_after(ci->i_last_wr, idle_cutoff)) { 1053 bits |= 1 << WR_SHIFT; 1054 } 1055 1056 /* check lazyio only when read/write is wanted */ 1057 if ((bits & (CEPH_FILE_MODE_RDWR << 1)) && 1058 ci->i_nr_by_mode[LAZY_SHIFT] > 0) 1059 bits |= 1 << LAZY_SHIFT; 1060 1061 return bits ? ceph_caps_for_mode(bits >> 1) : 0; 1062 } 1063 } 1064 1065 /* 1066 * wanted, by virtue of open file modes AND cap refs (buffered/cached data) 1067 */ 1068 int __ceph_caps_wanted(struct ceph_inode_info *ci) 1069 { 1070 int w = __ceph_caps_file_wanted(ci) | __ceph_caps_used(ci); 1071 if (S_ISDIR(ci->netfs.inode.i_mode)) { 1072 /* we want EXCL if holding caps of dir ops */ 1073 if (w & CEPH_CAP_ANY_DIR_OPS) 1074 w |= CEPH_CAP_FILE_EXCL; 1075 } else { 1076 /* we want EXCL if dirty data */ 1077 if (w & CEPH_CAP_FILE_BUFFER) 1078 w |= CEPH_CAP_FILE_EXCL; 1079 } 1080 return w; 1081 } 1082 1083 /* 1084 * Return caps we have registered with the MDS(s) as 'wanted'. 1085 */ 1086 int __ceph_caps_mds_wanted(struct ceph_inode_info *ci, bool check) 1087 { 1088 struct ceph_cap *cap; 1089 struct rb_node *p; 1090 int mds_wanted = 0; 1091 1092 for (p = rb_first(&ci->i_caps); p; p = rb_next(p)) { 1093 cap = rb_entry(p, struct ceph_cap, ci_node); 1094 if (check && !__cap_is_valid(cap)) 1095 continue; 1096 if (cap == ci->i_auth_cap) 1097 mds_wanted |= cap->mds_wanted; 1098 else 1099 mds_wanted |= (cap->mds_wanted & ~CEPH_CAP_ANY_FILE_WR); 1100 } 1101 return mds_wanted; 1102 } 1103 1104 int ceph_is_any_caps(struct inode *inode) 1105 { 1106 struct ceph_inode_info *ci = ceph_inode(inode); 1107 int ret; 1108 1109 spin_lock(&ci->i_ceph_lock); 1110 ret = __ceph_is_any_real_caps(ci); 1111 spin_unlock(&ci->i_ceph_lock); 1112 1113 return ret; 1114 } 1115 1116 /* 1117 * Remove a cap. Take steps to deal with a racing iterate_session_caps. 1118 * 1119 * caller should hold i_ceph_lock. 1120 * caller will not hold session s_mutex if called from destroy_inode. 1121 */ 1122 void __ceph_remove_cap(struct ceph_cap *cap, bool queue_release) 1123 { 1124 struct ceph_mds_session *session = cap->session; 1125 struct ceph_client *cl = session->s_mdsc->fsc->client; 1126 struct ceph_inode_info *ci = cap->ci; 1127 struct inode *inode = &ci->netfs.inode; 1128 struct ceph_mds_client *mdsc; 1129 int removed = 0; 1130 1131 /* 'ci' being NULL means the remove have already occurred */ 1132 if (!ci) { 1133 doutc(cl, "inode is NULL\n"); 1134 return; 1135 } 1136 1137 lockdep_assert_held(&ci->i_ceph_lock); 1138 1139 doutc(cl, "%p from %p %llx.%llx\n", cap, inode, ceph_vinop(inode)); 1140 1141 mdsc = ceph_inode_to_fs_client(&ci->netfs.inode)->mdsc; 1142 1143 /* remove from inode's cap rbtree, and clear auth cap */ 1144 rb_erase(&cap->ci_node, &ci->i_caps); 1145 if (ci->i_auth_cap == cap) 1146 ci->i_auth_cap = NULL; 1147 1148 /* remove from session list */ 1149 spin_lock(&session->s_cap_lock); 1150 if (session->s_cap_iterator == cap) { 1151 /* not yet, we are iterating over this very cap */ 1152 doutc(cl, "delaying %p removal from session %p\n", cap, 1153 cap->session); 1154 } else { 1155 list_del_init(&cap->session_caps); 1156 session->s_nr_caps--; 1157 atomic64_dec(&mdsc->metric.total_caps); 1158 cap->session = NULL; 1159 removed = 1; 1160 } 1161 /* protect backpointer with s_cap_lock: see iterate_session_caps */ 1162 cap->ci = NULL; 1163 1164 /* 1165 * s_cap_reconnect is protected by s_cap_lock. no one changes 1166 * s_cap_gen while session is in the reconnect state. 1167 */ 1168 if (queue_release && 1169 (!session->s_cap_reconnect || 1170 cap->cap_gen == atomic_read(&session->s_cap_gen))) { 1171 cap->queue_release = 1; 1172 if (removed) { 1173 __ceph_queue_cap_release(session, cap); 1174 removed = 0; 1175 } 1176 } else { 1177 cap->queue_release = 0; 1178 } 1179 cap->cap_ino = ci->i_vino.ino; 1180 1181 spin_unlock(&session->s_cap_lock); 1182 1183 if (removed) 1184 ceph_put_cap(mdsc, cap); 1185 1186 if (!__ceph_is_any_real_caps(ci)) { 1187 /* when reconnect denied, we remove session caps forcibly, 1188 * i_wr_ref can be non-zero. If there are ongoing write, 1189 * keep i_snap_realm. 1190 */ 1191 if (ci->i_wr_ref == 0 && ci->i_snap_realm) 1192 ceph_change_snap_realm(&ci->netfs.inode, NULL); 1193 1194 __cap_delay_cancel(mdsc, ci); 1195 } 1196 } 1197 1198 void ceph_remove_cap(struct ceph_mds_client *mdsc, struct ceph_cap *cap, 1199 bool queue_release) 1200 { 1201 struct ceph_inode_info *ci = cap->ci; 1202 struct ceph_fs_client *fsc; 1203 1204 /* 'ci' being NULL means the remove have already occurred */ 1205 if (!ci) { 1206 doutc(mdsc->fsc->client, "inode is NULL\n"); 1207 return; 1208 } 1209 1210 lockdep_assert_held(&ci->i_ceph_lock); 1211 1212 fsc = ceph_inode_to_fs_client(&ci->netfs.inode); 1213 WARN_ON_ONCE(ci->i_auth_cap == cap && 1214 !list_empty(&ci->i_dirty_item) && 1215 !fsc->blocklisted && 1216 !ceph_inode_is_shutdown(&ci->netfs.inode)); 1217 1218 __ceph_remove_cap(cap, queue_release); 1219 } 1220 1221 struct cap_msg_args { 1222 struct ceph_mds_session *session; 1223 u64 ino, cid, follows; 1224 u64 flush_tid, oldest_flush_tid, size, max_size; 1225 u64 xattr_version; 1226 u64 change_attr; 1227 struct ceph_buffer *xattr_buf; 1228 struct ceph_buffer *old_xattr_buf; 1229 struct timespec64 atime, mtime, ctime, btime; 1230 int op, caps, wanted, dirty; 1231 u32 seq, issue_seq, mseq, time_warp_seq; 1232 u32 flags; 1233 kuid_t uid; 1234 kgid_t gid; 1235 umode_t mode; 1236 bool inline_data; 1237 bool wake; 1238 bool encrypted; 1239 u32 fscrypt_auth_len; 1240 u8 fscrypt_auth[sizeof(struct ceph_fscrypt_auth)]; // for context 1241 }; 1242 1243 /* Marshal up the cap msg to the MDS */ 1244 static void encode_cap_msg(struct ceph_msg *msg, struct cap_msg_args *arg) 1245 { 1246 struct ceph_mds_caps *fc; 1247 void *p; 1248 struct ceph_mds_client *mdsc = arg->session->s_mdsc; 1249 struct ceph_osd_client *osdc = &mdsc->fsc->client->osdc; 1250 1251 doutc(mdsc->fsc->client, 1252 "%s %llx %llx caps %s wanted %s dirty %s seq %u/%u" 1253 " tid %llu/%llu mseq %u follows %lld size %llu/%llu" 1254 " xattr_ver %llu xattr_len %d\n", 1255 ceph_cap_op_name(arg->op), arg->cid, arg->ino, 1256 ceph_cap_string(arg->caps), ceph_cap_string(arg->wanted), 1257 ceph_cap_string(arg->dirty), arg->seq, arg->issue_seq, 1258 arg->flush_tid, arg->oldest_flush_tid, arg->mseq, arg->follows, 1259 arg->size, arg->max_size, arg->xattr_version, 1260 arg->xattr_buf ? (int)arg->xattr_buf->vec.iov_len : 0); 1261 1262 msg->hdr.version = cpu_to_le16(12); 1263 msg->hdr.tid = cpu_to_le64(arg->flush_tid); 1264 1265 fc = msg->front.iov_base; 1266 memset(fc, 0, sizeof(*fc)); 1267 1268 fc->cap_id = cpu_to_le64(arg->cid); 1269 fc->op = cpu_to_le32(arg->op); 1270 fc->seq = cpu_to_le32(arg->seq); 1271 fc->issue_seq = cpu_to_le32(arg->issue_seq); 1272 fc->migrate_seq = cpu_to_le32(arg->mseq); 1273 fc->caps = cpu_to_le32(arg->caps); 1274 fc->wanted = cpu_to_le32(arg->wanted); 1275 fc->dirty = cpu_to_le32(arg->dirty); 1276 fc->ino = cpu_to_le64(arg->ino); 1277 fc->snap_follows = cpu_to_le64(arg->follows); 1278 1279 #if IS_ENABLED(CONFIG_FS_ENCRYPTION) 1280 if (arg->encrypted) 1281 fc->size = cpu_to_le64(round_up(arg->size, 1282 CEPH_FSCRYPT_BLOCK_SIZE)); 1283 else 1284 #endif 1285 fc->size = cpu_to_le64(arg->size); 1286 fc->max_size = cpu_to_le64(arg->max_size); 1287 ceph_encode_timespec64(&fc->mtime, &arg->mtime); 1288 ceph_encode_timespec64(&fc->atime, &arg->atime); 1289 ceph_encode_timespec64(&fc->ctime, &arg->ctime); 1290 fc->time_warp_seq = cpu_to_le32(arg->time_warp_seq); 1291 1292 fc->uid = cpu_to_le32(from_kuid(&init_user_ns, arg->uid)); 1293 fc->gid = cpu_to_le32(from_kgid(&init_user_ns, arg->gid)); 1294 fc->mode = cpu_to_le32(arg->mode); 1295 1296 fc->xattr_version = cpu_to_le64(arg->xattr_version); 1297 if (arg->xattr_buf) { 1298 msg->middle = ceph_buffer_get(arg->xattr_buf); 1299 fc->xattr_len = cpu_to_le32(arg->xattr_buf->vec.iov_len); 1300 msg->hdr.middle_len = cpu_to_le32(arg->xattr_buf->vec.iov_len); 1301 } 1302 1303 p = fc + 1; 1304 /* flock buffer size (version 2) */ 1305 ceph_encode_32(&p, 0); 1306 /* inline version (version 4) */ 1307 ceph_encode_64(&p, arg->inline_data ? 0 : CEPH_INLINE_NONE); 1308 /* inline data size */ 1309 ceph_encode_32(&p, 0); 1310 /* 1311 * osd_epoch_barrier (version 5) 1312 * The epoch_barrier is protected osdc->lock, so READ_ONCE here in 1313 * case it was recently changed 1314 */ 1315 ceph_encode_32(&p, READ_ONCE(osdc->epoch_barrier)); 1316 /* oldest_flush_tid (version 6) */ 1317 ceph_encode_64(&p, arg->oldest_flush_tid); 1318 1319 /* 1320 * caller_uid/caller_gid (version 7) 1321 * 1322 * Currently, we don't properly track which caller dirtied the caps 1323 * last, and force a flush of them when there is a conflict. For now, 1324 * just set this to 0:0, to emulate how the MDS has worked up to now. 1325 */ 1326 ceph_encode_32(&p, 0); 1327 ceph_encode_32(&p, 0); 1328 1329 /* pool namespace (version 8) (mds always ignores this) */ 1330 ceph_encode_32(&p, 0); 1331 1332 /* btime and change_attr (version 9) */ 1333 ceph_encode_timespec64(p, &arg->btime); 1334 p += sizeof(struct ceph_timespec); 1335 ceph_encode_64(&p, arg->change_attr); 1336 1337 /* Advisory flags (version 10) */ 1338 ceph_encode_32(&p, arg->flags); 1339 1340 /* dirstats (version 11) - these are r/o on the client */ 1341 ceph_encode_64(&p, 0); 1342 ceph_encode_64(&p, 0); 1343 1344 #if IS_ENABLED(CONFIG_FS_ENCRYPTION) 1345 /* 1346 * fscrypt_auth and fscrypt_file (version 12) 1347 * 1348 * fscrypt_auth holds the crypto context (if any). fscrypt_file 1349 * tracks the real i_size as an __le64 field (and we use a rounded-up 1350 * i_size in the traditional size field). 1351 */ 1352 ceph_encode_32(&p, arg->fscrypt_auth_len); 1353 ceph_encode_copy(&p, arg->fscrypt_auth, arg->fscrypt_auth_len); 1354 ceph_encode_32(&p, sizeof(__le64)); 1355 ceph_encode_64(&p, arg->size); 1356 #else /* CONFIG_FS_ENCRYPTION */ 1357 ceph_encode_32(&p, 0); 1358 ceph_encode_32(&p, 0); 1359 #endif /* CONFIG_FS_ENCRYPTION */ 1360 } 1361 1362 /* 1363 * Queue cap releases when an inode is dropped from our cache. 1364 */ 1365 void __ceph_remove_caps(struct ceph_inode_info *ci) 1366 { 1367 struct inode *inode = &ci->netfs.inode; 1368 struct ceph_mds_client *mdsc = ceph_inode_to_fs_client(inode)->mdsc; 1369 struct rb_node *p; 1370 1371 /* lock i_ceph_lock, because ceph_d_revalidate(..., LOOKUP_RCU) 1372 * may call __ceph_caps_issued_mask() on a freeing inode. */ 1373 spin_lock(&ci->i_ceph_lock); 1374 p = rb_first(&ci->i_caps); 1375 while (p) { 1376 struct ceph_cap *cap = rb_entry(p, struct ceph_cap, ci_node); 1377 p = rb_next(p); 1378 ceph_remove_cap(mdsc, cap, true); 1379 } 1380 spin_unlock(&ci->i_ceph_lock); 1381 } 1382 1383 /* 1384 * Prepare to send a cap message to an MDS. Update the cap state, and populate 1385 * the arg struct with the parameters that will need to be sent. This should 1386 * be done under the i_ceph_lock to guard against changes to cap state. 1387 * 1388 * Make note of max_size reported/requested from mds, revoked caps 1389 * that have now been implemented. 1390 */ 1391 static void __prep_cap(struct cap_msg_args *arg, struct ceph_cap *cap, 1392 int op, int flags, int used, int want, int retain, 1393 int flushing, u64 flush_tid, u64 oldest_flush_tid) 1394 { 1395 struct ceph_inode_info *ci = cap->ci; 1396 struct inode *inode = &ci->netfs.inode; 1397 struct ceph_client *cl = ceph_inode_to_client(inode); 1398 int held, revoking; 1399 1400 lockdep_assert_held(&ci->i_ceph_lock); 1401 1402 held = cap->issued | cap->implemented; 1403 revoking = cap->implemented & ~cap->issued; 1404 retain &= ~revoking; 1405 1406 doutc(cl, "%p %llx.%llx cap %p session %p %s -> %s (revoking %s)\n", 1407 inode, ceph_vinop(inode), cap, cap->session, 1408 ceph_cap_string(held), ceph_cap_string(held & retain), 1409 ceph_cap_string(revoking)); 1410 BUG_ON((retain & CEPH_CAP_PIN) == 0); 1411 1412 clear_bit(CEPH_I_FLUSH_BIT, &ci->i_ceph_flags); 1413 1414 cap->issued &= retain; /* drop bits we don't want */ 1415 /* 1416 * Wake up any waiters on wanted -> needed transition. This is due to 1417 * the weird transition from buffered to sync IO... we need to flush 1418 * dirty pages _before_ allowing sync writes to avoid reordering. 1419 */ 1420 arg->wake = cap->implemented & ~cap->issued; 1421 cap->implemented &= cap->issued | used; 1422 cap->mds_wanted = want; 1423 1424 arg->session = cap->session; 1425 arg->ino = ceph_vino(inode).ino; 1426 arg->cid = cap->cap_id; 1427 arg->follows = flushing ? ci->i_head_snapc->seq : 0; 1428 arg->flush_tid = flush_tid; 1429 arg->oldest_flush_tid = oldest_flush_tid; 1430 arg->size = i_size_read(inode); 1431 ci->i_reported_size = arg->size; 1432 arg->max_size = ci->i_wanted_max_size; 1433 if (cap == ci->i_auth_cap) { 1434 if (want & CEPH_CAP_ANY_FILE_WR) 1435 ci->i_requested_max_size = arg->max_size; 1436 else 1437 ci->i_requested_max_size = 0; 1438 } 1439 1440 if (flushing & CEPH_CAP_XATTR_EXCL) { 1441 arg->old_xattr_buf = __ceph_build_xattrs_blob(ci); 1442 arg->xattr_version = ci->i_xattrs.version; 1443 arg->xattr_buf = ceph_buffer_get(ci->i_xattrs.blob); 1444 } else { 1445 arg->xattr_buf = NULL; 1446 arg->old_xattr_buf = NULL; 1447 } 1448 1449 arg->mtime = inode_get_mtime(inode); 1450 arg->atime = inode_get_atime(inode); 1451 arg->ctime = inode_get_ctime(inode); 1452 arg->btime = ci->i_btime; 1453 arg->change_attr = inode_peek_iversion_raw(inode); 1454 1455 arg->op = op; 1456 arg->caps = cap->implemented; 1457 arg->wanted = want; 1458 arg->dirty = flushing; 1459 1460 arg->seq = cap->seq; 1461 arg->issue_seq = cap->issue_seq; 1462 arg->mseq = cap->mseq; 1463 arg->time_warp_seq = ci->i_time_warp_seq; 1464 1465 arg->uid = inode->i_uid; 1466 arg->gid = inode->i_gid; 1467 arg->mode = inode->i_mode; 1468 1469 arg->inline_data = ci->i_inline_version != CEPH_INLINE_NONE; 1470 if (!(flags & CEPH_CLIENT_CAPS_PENDING_CAPSNAP) && 1471 !list_empty(&ci->i_cap_snaps)) { 1472 struct ceph_cap_snap *capsnap; 1473 list_for_each_entry_reverse(capsnap, &ci->i_cap_snaps, ci_item) { 1474 if (capsnap->cap_flush.tid) 1475 break; 1476 if (capsnap->need_flush) { 1477 flags |= CEPH_CLIENT_CAPS_PENDING_CAPSNAP; 1478 break; 1479 } 1480 } 1481 } 1482 arg->flags = flags; 1483 arg->encrypted = IS_ENCRYPTED(inode); 1484 #if IS_ENABLED(CONFIG_FS_ENCRYPTION) 1485 if (ci->fscrypt_auth_len && 1486 WARN_ON_ONCE(ci->fscrypt_auth_len > sizeof(struct ceph_fscrypt_auth))) { 1487 /* Don't set this if it's too big */ 1488 arg->fscrypt_auth_len = 0; 1489 } else { 1490 arg->fscrypt_auth_len = ci->fscrypt_auth_len; 1491 memcpy(arg->fscrypt_auth, ci->fscrypt_auth, 1492 min_t(size_t, ci->fscrypt_auth_len, 1493 sizeof(arg->fscrypt_auth))); 1494 } 1495 #endif /* CONFIG_FS_ENCRYPTION */ 1496 } 1497 1498 #if IS_ENABLED(CONFIG_FS_ENCRYPTION) 1499 #define CAP_MSG_FIXED_FIELDS (sizeof(struct ceph_mds_caps) + \ 1500 4 + 8 + 4 + 4 + 8 + 4 + 4 + 4 + 8 + 8 + 4 + 8 + 8 + 4 + 4 + 8) 1501 1502 static inline int cap_msg_size(struct cap_msg_args *arg) 1503 { 1504 return CAP_MSG_FIXED_FIELDS + arg->fscrypt_auth_len; 1505 } 1506 #else 1507 #define CAP_MSG_FIXED_FIELDS (sizeof(struct ceph_mds_caps) + \ 1508 4 + 8 + 4 + 4 + 8 + 4 + 4 + 4 + 8 + 8 + 4 + 8 + 8 + 4 + 4) 1509 1510 static inline int cap_msg_size(struct cap_msg_args *arg) 1511 { 1512 return CAP_MSG_FIXED_FIELDS; 1513 } 1514 #endif /* CONFIG_FS_ENCRYPTION */ 1515 1516 /* 1517 * Send a cap msg on the given inode. 1518 * 1519 * Caller should hold snap_rwsem (read), s_mutex. 1520 */ 1521 static void __send_cap(struct cap_msg_args *arg, struct ceph_inode_info *ci) 1522 { 1523 struct ceph_msg *msg; 1524 struct inode *inode = &ci->netfs.inode; 1525 struct ceph_client *cl = ceph_inode_to_client(inode); 1526 1527 msg = ceph_msg_new(CEPH_MSG_CLIENT_CAPS, cap_msg_size(arg), GFP_NOFS, 1528 false); 1529 if (!msg) { 1530 pr_err_client(cl, 1531 "error allocating cap msg: ino (%llx.%llx)" 1532 " flushing %s tid %llu, requeuing cap.\n", 1533 ceph_vinop(inode), ceph_cap_string(arg->dirty), 1534 arg->flush_tid); 1535 spin_lock(&ci->i_ceph_lock); 1536 __cap_delay_requeue(arg->session->s_mdsc, ci); 1537 spin_unlock(&ci->i_ceph_lock); 1538 return; 1539 } 1540 1541 encode_cap_msg(msg, arg); 1542 ceph_con_send(&arg->session->s_con, msg); 1543 ceph_buffer_put(arg->old_xattr_buf); 1544 ceph_buffer_put(arg->xattr_buf); 1545 if (arg->wake) 1546 wake_up_all(&ci->i_cap_wq); 1547 } 1548 1549 static inline int __send_flush_snap(struct inode *inode, 1550 struct ceph_mds_session *session, 1551 struct ceph_cap_snap *capsnap, 1552 u32 mseq, u64 oldest_flush_tid) 1553 { 1554 struct cap_msg_args arg; 1555 struct ceph_msg *msg; 1556 1557 arg.session = session; 1558 arg.ino = ceph_vino(inode).ino; 1559 arg.cid = 0; 1560 arg.follows = capsnap->follows; 1561 arg.flush_tid = capsnap->cap_flush.tid; 1562 arg.oldest_flush_tid = oldest_flush_tid; 1563 1564 arg.size = capsnap->size; 1565 arg.max_size = 0; 1566 arg.xattr_version = capsnap->xattr_version; 1567 arg.xattr_buf = capsnap->xattr_blob; 1568 arg.old_xattr_buf = NULL; 1569 1570 arg.atime = capsnap->atime; 1571 arg.mtime = capsnap->mtime; 1572 arg.ctime = capsnap->ctime; 1573 arg.btime = capsnap->btime; 1574 arg.change_attr = capsnap->change_attr; 1575 1576 arg.op = CEPH_CAP_OP_FLUSHSNAP; 1577 arg.caps = capsnap->issued; 1578 arg.wanted = 0; 1579 arg.dirty = capsnap->dirty; 1580 1581 arg.seq = 0; 1582 arg.issue_seq = 0; 1583 arg.mseq = mseq; 1584 arg.time_warp_seq = capsnap->time_warp_seq; 1585 1586 arg.uid = capsnap->uid; 1587 arg.gid = capsnap->gid; 1588 arg.mode = capsnap->mode; 1589 1590 arg.inline_data = capsnap->inline_data; 1591 arg.flags = 0; 1592 arg.wake = false; 1593 arg.encrypted = IS_ENCRYPTED(inode); 1594 1595 /* No fscrypt_auth changes from a capsnap.*/ 1596 arg.fscrypt_auth_len = 0; 1597 1598 msg = ceph_msg_new(CEPH_MSG_CLIENT_CAPS, cap_msg_size(&arg), 1599 GFP_NOFS, false); 1600 if (!msg) 1601 return -ENOMEM; 1602 1603 encode_cap_msg(msg, &arg); 1604 ceph_con_send(&arg.session->s_con, msg); 1605 return 0; 1606 } 1607 1608 /* 1609 * When a snapshot is taken, clients accumulate dirty metadata on 1610 * inodes with capabilities in ceph_cap_snaps to describe the file 1611 * state at the time the snapshot was taken. This must be flushed 1612 * asynchronously back to the MDS once sync writes complete and dirty 1613 * data is written out. 1614 * 1615 * Called under i_ceph_lock. 1616 */ 1617 static void __ceph_flush_snaps(struct ceph_inode_info *ci, 1618 struct ceph_mds_session *session) 1619 __releases(ci->i_ceph_lock) 1620 __acquires(ci->i_ceph_lock) 1621 { 1622 struct inode *inode = &ci->netfs.inode; 1623 struct ceph_mds_client *mdsc = session->s_mdsc; 1624 struct ceph_client *cl = mdsc->fsc->client; 1625 struct ceph_cap_snap *capsnap; 1626 u64 oldest_flush_tid = 0; 1627 u64 first_tid = 1, last_tid = 0; 1628 1629 doutc(cl, "%p %llx.%llx session %p\n", inode, ceph_vinop(inode), 1630 session); 1631 1632 list_for_each_entry(capsnap, &ci->i_cap_snaps, ci_item) { 1633 /* 1634 * we need to wait for sync writes to complete and for dirty 1635 * pages to be written out. 1636 */ 1637 if (capsnap->dirty_pages || capsnap->writing) 1638 break; 1639 1640 /* should be removed by ceph_try_drop_cap_snap() */ 1641 BUG_ON(!capsnap->need_flush); 1642 1643 /* only flush each capsnap once */ 1644 if (capsnap->cap_flush.tid > 0) { 1645 doutc(cl, "already flushed %p, skipping\n", capsnap); 1646 continue; 1647 } 1648 1649 spin_lock(&mdsc->cap_dirty_lock); 1650 capsnap->cap_flush.tid = ++mdsc->last_cap_flush_tid; 1651 capsnap->cap_flush.ci = ci; 1652 list_add_tail(&capsnap->cap_flush.g_list, 1653 &mdsc->cap_flush_list); 1654 if (oldest_flush_tid == 0) 1655 oldest_flush_tid = __get_oldest_flush_tid(mdsc); 1656 if (list_empty(&ci->i_flushing_item)) { 1657 list_add_tail(&ci->i_flushing_item, 1658 &session->s_cap_flushing); 1659 } 1660 spin_unlock(&mdsc->cap_dirty_lock); 1661 1662 list_add_tail(&capsnap->cap_flush.i_list, 1663 &ci->i_cap_flush_list); 1664 1665 if (first_tid == 1) 1666 first_tid = capsnap->cap_flush.tid; 1667 last_tid = capsnap->cap_flush.tid; 1668 } 1669 1670 clear_bit(CEPH_I_FLUSH_SNAPS_BIT, &ci->i_ceph_flags); 1671 1672 while (first_tid <= last_tid) { 1673 struct ceph_cap *cap = ci->i_auth_cap; 1674 struct ceph_cap_flush *cf = NULL, *iter; 1675 int ret; 1676 1677 if (!(cap && cap->session == session)) { 1678 doutc(cl, "%p %llx.%llx auth cap %p not mds%d, stop\n", 1679 inode, ceph_vinop(inode), cap, session->s_mds); 1680 break; 1681 } 1682 1683 ret = -ENOENT; 1684 list_for_each_entry(iter, &ci->i_cap_flush_list, i_list) { 1685 if (iter->tid >= first_tid) { 1686 cf = iter; 1687 ret = 0; 1688 break; 1689 } 1690 } 1691 if (ret < 0) 1692 break; 1693 1694 first_tid = cf->tid + 1; 1695 1696 capsnap = container_of(cf, struct ceph_cap_snap, cap_flush); 1697 refcount_inc(&capsnap->nref); 1698 spin_unlock(&ci->i_ceph_lock); 1699 1700 doutc(cl, "%p %llx.%llx capsnap %p tid %llu %s\n", inode, 1701 ceph_vinop(inode), capsnap, cf->tid, 1702 ceph_cap_string(capsnap->dirty)); 1703 1704 ret = __send_flush_snap(inode, session, capsnap, cap->mseq, 1705 oldest_flush_tid); 1706 if (ret < 0) { 1707 pr_err_client(cl, "error sending cap flushsnap, " 1708 "ino (%llx.%llx) tid %llu follows %llu\n", 1709 ceph_vinop(inode), cf->tid, 1710 capsnap->follows); 1711 } 1712 1713 ceph_put_cap_snap(capsnap); 1714 spin_lock(&ci->i_ceph_lock); 1715 } 1716 } 1717 1718 void ceph_flush_snaps(struct ceph_inode_info *ci, 1719 struct ceph_mds_session **psession) 1720 { 1721 struct inode *inode = &ci->netfs.inode; 1722 struct ceph_mds_client *mdsc = ceph_inode_to_fs_client(inode)->mdsc; 1723 struct ceph_client *cl = ceph_inode_to_client(inode); 1724 struct ceph_mds_session *session = NULL; 1725 bool need_put = false; 1726 int mds; 1727 1728 doutc(cl, "%p %llx.%llx\n", inode, ceph_vinop(inode)); 1729 if (psession) 1730 session = *psession; 1731 retry: 1732 spin_lock(&ci->i_ceph_lock); 1733 if (!(ci->i_ceph_flags & CEPH_I_FLUSH_SNAPS)) { 1734 doutc(cl, " no capsnap needs flush, doing nothing\n"); 1735 goto out; 1736 } 1737 if (!ci->i_auth_cap) { 1738 doutc(cl, " no auth cap (migrating?), doing nothing\n"); 1739 goto out; 1740 } 1741 1742 mds = ci->i_auth_cap->session->s_mds; 1743 if (session && session->s_mds != mds) { 1744 doutc(cl, " oops, wrong session %p mutex\n", session); 1745 ceph_put_mds_session(session); 1746 session = NULL; 1747 } 1748 if (!session) { 1749 spin_unlock(&ci->i_ceph_lock); 1750 mutex_lock(&mdsc->mutex); 1751 session = __ceph_lookup_mds_session(mdsc, mds); 1752 mutex_unlock(&mdsc->mutex); 1753 goto retry; 1754 } 1755 1756 // make sure flushsnap messages are sent in proper order. 1757 if (ci->i_ceph_flags & CEPH_I_KICK_FLUSH) 1758 __kick_flushing_caps(mdsc, session, ci, 0); 1759 1760 __ceph_flush_snaps(ci, session); 1761 out: 1762 spin_unlock(&ci->i_ceph_lock); 1763 1764 if (psession) 1765 *psession = session; 1766 else 1767 ceph_put_mds_session(session); 1768 /* we flushed them all; remove this inode from the queue */ 1769 spin_lock(&mdsc->snap_flush_lock); 1770 if (!list_empty(&ci->i_snap_flush_item)) 1771 need_put = true; 1772 list_del_init(&ci->i_snap_flush_item); 1773 spin_unlock(&mdsc->snap_flush_lock); 1774 1775 if (need_put) 1776 iput(inode); 1777 } 1778 1779 /* 1780 * Mark caps dirty. If inode is newly dirty, return the dirty flags. 1781 * Caller is then responsible for calling __mark_inode_dirty with the 1782 * returned flags value. 1783 */ 1784 int __ceph_mark_dirty_caps(struct ceph_inode_info *ci, int mask, 1785 struct ceph_cap_flush **pcf) 1786 { 1787 struct ceph_mds_client *mdsc = 1788 ceph_sb_to_fs_client(ci->netfs.inode.i_sb)->mdsc; 1789 struct inode *inode = &ci->netfs.inode; 1790 struct ceph_client *cl = ceph_inode_to_client(inode); 1791 int was = ci->i_dirty_caps; 1792 int dirty = 0; 1793 1794 lockdep_assert_held(&ci->i_ceph_lock); 1795 1796 if (!ci->i_auth_cap) { 1797 pr_warn_client(cl, "%p %llx.%llx mask %s, " 1798 "but no auth cap (session was closed?)\n", 1799 inode, ceph_vinop(inode), 1800 ceph_cap_string(mask)); 1801 return 0; 1802 } 1803 1804 doutc(cl, "%p %llx.%llx %s dirty %s -> %s\n", inode, 1805 ceph_vinop(inode), ceph_cap_string(mask), 1806 ceph_cap_string(was), ceph_cap_string(was | mask)); 1807 ci->i_dirty_caps |= mask; 1808 if (was == 0) { 1809 struct ceph_mds_session *session = ci->i_auth_cap->session; 1810 1811 WARN_ON_ONCE(ci->i_prealloc_cap_flush); 1812 swap(ci->i_prealloc_cap_flush, *pcf); 1813 1814 if (!ci->i_head_snapc) { 1815 WARN_ON_ONCE(!rwsem_is_locked(&mdsc->snap_rwsem)); 1816 ci->i_head_snapc = ceph_get_snap_context( 1817 ci->i_snap_realm->cached_context); 1818 } 1819 doutc(cl, "%p %llx.%llx now dirty snapc %p auth cap %p\n", 1820 inode, ceph_vinop(inode), ci->i_head_snapc, 1821 ci->i_auth_cap); 1822 BUG_ON(!list_empty(&ci->i_dirty_item)); 1823 spin_lock(&mdsc->cap_dirty_lock); 1824 list_add(&ci->i_dirty_item, &session->s_cap_dirty); 1825 spin_unlock(&mdsc->cap_dirty_lock); 1826 if (ci->i_flushing_caps == 0) { 1827 ihold(inode); 1828 dirty |= I_DIRTY_SYNC; 1829 } 1830 } else { 1831 WARN_ON_ONCE(!ci->i_prealloc_cap_flush); 1832 } 1833 BUG_ON(list_empty(&ci->i_dirty_item)); 1834 if (((was | ci->i_flushing_caps) & CEPH_CAP_FILE_BUFFER) && 1835 (mask & CEPH_CAP_FILE_BUFFER)) 1836 dirty |= I_DIRTY_DATASYNC; 1837 __cap_delay_requeue(mdsc, ci); 1838 return dirty; 1839 } 1840 1841 struct ceph_cap_flush *ceph_alloc_cap_flush(void) 1842 { 1843 struct ceph_cap_flush *cf; 1844 1845 cf = kmem_cache_alloc(ceph_cap_flush_cachep, GFP_KERNEL); 1846 if (!cf) 1847 return NULL; 1848 1849 cf->is_capsnap = false; 1850 cf->ci = NULL; 1851 return cf; 1852 } 1853 1854 void ceph_free_cap_flush(struct ceph_cap_flush *cf) 1855 { 1856 if (cf) 1857 kmem_cache_free(ceph_cap_flush_cachep, cf); 1858 } 1859 1860 static u64 __get_oldest_flush_tid(struct ceph_mds_client *mdsc) 1861 { 1862 if (!list_empty(&mdsc->cap_flush_list)) { 1863 struct ceph_cap_flush *cf = 1864 list_first_entry(&mdsc->cap_flush_list, 1865 struct ceph_cap_flush, g_list); 1866 return cf->tid; 1867 } 1868 return 0; 1869 } 1870 1871 /* 1872 * Remove cap_flush from the mdsc's or inode's flushing cap list. 1873 * Return true if caller needs to wake up flush waiters. 1874 */ 1875 static bool __detach_cap_flush_from_mdsc(struct ceph_mds_client *mdsc, 1876 struct ceph_cap_flush *cf) 1877 { 1878 struct ceph_cap_flush *prev; 1879 bool wake = cf->wake; 1880 1881 if (wake && cf->g_list.prev != &mdsc->cap_flush_list) { 1882 prev = list_prev_entry(cf, g_list); 1883 prev->wake = true; 1884 wake = false; 1885 } 1886 list_del_init(&cf->g_list); 1887 return wake; 1888 } 1889 1890 static bool __detach_cap_flush_from_ci(struct ceph_inode_info *ci, 1891 struct ceph_cap_flush *cf) 1892 { 1893 struct ceph_cap_flush *prev; 1894 bool wake = cf->wake; 1895 1896 if (wake && cf->i_list.prev != &ci->i_cap_flush_list) { 1897 prev = list_prev_entry(cf, i_list); 1898 prev->wake = true; 1899 wake = false; 1900 } 1901 list_del_init(&cf->i_list); 1902 return wake; 1903 } 1904 1905 /* 1906 * Add dirty inode to the flushing list. Assigned a seq number so we 1907 * can wait for caps to flush without starving. 1908 * 1909 * Called under i_ceph_lock. Returns the flush tid. 1910 */ 1911 static u64 __mark_caps_flushing(struct inode *inode, 1912 struct ceph_mds_session *session, bool wake, 1913 u64 *oldest_flush_tid) 1914 { 1915 struct ceph_mds_client *mdsc = ceph_sb_to_fs_client(inode->i_sb)->mdsc; 1916 struct ceph_client *cl = ceph_inode_to_client(inode); 1917 struct ceph_inode_info *ci = ceph_inode(inode); 1918 struct ceph_cap_flush *cf = NULL; 1919 int flushing; 1920 1921 lockdep_assert_held(&ci->i_ceph_lock); 1922 BUG_ON(ci->i_dirty_caps == 0); 1923 BUG_ON(list_empty(&ci->i_dirty_item)); 1924 BUG_ON(!ci->i_prealloc_cap_flush); 1925 1926 flushing = ci->i_dirty_caps; 1927 doutc(cl, "flushing %s, flushing_caps %s -> %s\n", 1928 ceph_cap_string(flushing), 1929 ceph_cap_string(ci->i_flushing_caps), 1930 ceph_cap_string(ci->i_flushing_caps | flushing)); 1931 ci->i_flushing_caps |= flushing; 1932 ci->i_dirty_caps = 0; 1933 doutc(cl, "%p %llx.%llx now !dirty\n", inode, ceph_vinop(inode)); 1934 1935 swap(cf, ci->i_prealloc_cap_flush); 1936 cf->ci = ci; 1937 cf->caps = flushing; 1938 cf->wake = wake; 1939 1940 spin_lock(&mdsc->cap_dirty_lock); 1941 list_del_init(&ci->i_dirty_item); 1942 1943 cf->tid = ++mdsc->last_cap_flush_tid; 1944 list_add_tail(&cf->g_list, &mdsc->cap_flush_list); 1945 *oldest_flush_tid = __get_oldest_flush_tid(mdsc); 1946 1947 if (list_empty(&ci->i_flushing_item)) { 1948 list_add_tail(&ci->i_flushing_item, &session->s_cap_flushing); 1949 mdsc->num_cap_flushing++; 1950 } 1951 spin_unlock(&mdsc->cap_dirty_lock); 1952 1953 list_add_tail(&cf->i_list, &ci->i_cap_flush_list); 1954 1955 return cf->tid; 1956 } 1957 1958 /* 1959 * try to invalidate mapping pages without blocking. 1960 */ 1961 static int try_nonblocking_invalidate(struct inode *inode) 1962 __releases(ci->i_ceph_lock) 1963 __acquires(ci->i_ceph_lock) 1964 { 1965 struct ceph_client *cl = ceph_inode_to_client(inode); 1966 struct ceph_inode_info *ci = ceph_inode(inode); 1967 u32 invalidating_gen = ci->i_rdcache_gen; 1968 1969 spin_unlock(&ci->i_ceph_lock); 1970 ceph_fscache_invalidate(inode, false); 1971 invalidate_mapping_pages(&inode->i_data, 0, -1); 1972 spin_lock(&ci->i_ceph_lock); 1973 1974 if (inode->i_data.nrpages == 0 && 1975 invalidating_gen == ci->i_rdcache_gen) { 1976 /* success. */ 1977 doutc(cl, "%p %llx.%llx success\n", inode, 1978 ceph_vinop(inode)); 1979 /* save any racing async invalidate some trouble */ 1980 ci->i_rdcache_revoking = ci->i_rdcache_gen - 1; 1981 return 0; 1982 } 1983 doutc(cl, "%p %llx.%llx failed\n", inode, ceph_vinop(inode)); 1984 return -1; 1985 } 1986 1987 bool __ceph_should_report_size(struct ceph_inode_info *ci) 1988 { 1989 loff_t size = i_size_read(&ci->netfs.inode); 1990 /* mds will adjust max size according to the reported size */ 1991 if (ci->i_flushing_caps & CEPH_CAP_FILE_WR) 1992 return false; 1993 if (size >= ci->i_max_size) 1994 return true; 1995 /* half of previous max_size increment has been used */ 1996 if (ci->i_max_size > ci->i_reported_size && 1997 (size << 1) >= ci->i_max_size + ci->i_reported_size) 1998 return true; 1999 return false; 2000 } 2001 2002 /* 2003 * Swiss army knife function to examine currently used and wanted 2004 * versus held caps. Release, flush, ack revoked caps to mds as 2005 * appropriate. 2006 * 2007 * CHECK_CAPS_AUTHONLY - we should only check the auth cap 2008 * CHECK_CAPS_FLUSH - we should flush any dirty caps immediately, without 2009 * further delay. 2010 * CHECK_CAPS_FLUSH_FORCE - we should flush any caps immediately, without 2011 * further delay. 2012 */ 2013 void ceph_check_caps(struct ceph_inode_info *ci, int flags) 2014 { 2015 struct inode *inode = &ci->netfs.inode; 2016 struct ceph_mds_client *mdsc = ceph_sb_to_mdsc(inode->i_sb); 2017 struct ceph_client *cl = ceph_inode_to_client(inode); 2018 struct ceph_cap *cap; 2019 u64 flush_tid, oldest_flush_tid; 2020 int file_wanted, used, cap_used; 2021 int issued, implemented, want, retain, revoking, flushing = 0; 2022 int mds = -1; /* keep track of how far we've gone through i_caps list 2023 to avoid an infinite loop on retry */ 2024 struct rb_node *p; 2025 bool queue_invalidate = false; 2026 bool tried_invalidate = false; 2027 bool queue_writeback = false; 2028 struct ceph_mds_session *session = NULL; 2029 2030 spin_lock(&ci->i_ceph_lock); 2031 if (ci->i_ceph_flags & CEPH_I_ASYNC_CREATE) { 2032 set_bit(CEPH_I_ASYNC_CHECK_CAPS_BIT, &ci->i_ceph_flags); 2033 2034 /* Don't send messages until we get async create reply */ 2035 spin_unlock(&ci->i_ceph_lock); 2036 return; 2037 } 2038 2039 if (ci->i_ceph_flags & CEPH_I_FLUSH) 2040 flags |= CHECK_CAPS_FLUSH; 2041 retry: 2042 /* Caps wanted by virtue of active open files. */ 2043 file_wanted = __ceph_caps_file_wanted(ci); 2044 2045 /* Caps which have active references against them */ 2046 used = __ceph_caps_used(ci); 2047 2048 /* 2049 * "issued" represents the current caps that the MDS wants us to have. 2050 * "implemented" is the set that we have been granted, and includes the 2051 * ones that have not yet been returned to the MDS (the "revoking" set, 2052 * usually because they have outstanding references). 2053 */ 2054 issued = __ceph_caps_issued(ci, &implemented); 2055 revoking = implemented & ~issued; 2056 2057 want = file_wanted; 2058 2059 /* The ones we currently want to retain (may be adjusted below) */ 2060 retain = file_wanted | used | CEPH_CAP_PIN; 2061 if (!mdsc->stopping && inode->i_nlink > 0) { 2062 if (file_wanted) { 2063 retain |= CEPH_CAP_ANY; /* be greedy */ 2064 } else if (S_ISDIR(inode->i_mode) && 2065 (issued & CEPH_CAP_FILE_SHARED) && 2066 __ceph_dir_is_complete(ci)) { 2067 /* 2068 * If a directory is complete, we want to keep 2069 * the exclusive cap. So that MDS does not end up 2070 * revoking the shared cap on every create/unlink 2071 * operation. 2072 */ 2073 if (IS_RDONLY(inode)) { 2074 want = CEPH_CAP_ANY_SHARED; 2075 } else { 2076 want |= CEPH_CAP_ANY_SHARED | CEPH_CAP_FILE_EXCL; 2077 } 2078 retain |= want; 2079 } else { 2080 2081 retain |= CEPH_CAP_ANY_SHARED; 2082 /* 2083 * keep RD only if we didn't have the file open RW, 2084 * because then the mds would revoke it anyway to 2085 * journal max_size=0. 2086 */ 2087 if (ci->i_max_size == 0) 2088 retain |= CEPH_CAP_ANY_RD; 2089 } 2090 } 2091 2092 doutc(cl, "%p %llx.%llx file_want %s used %s dirty %s " 2093 "flushing %s issued %s revoking %s retain %s %s%s%s%s\n", 2094 inode, ceph_vinop(inode), ceph_cap_string(file_wanted), 2095 ceph_cap_string(used), ceph_cap_string(ci->i_dirty_caps), 2096 ceph_cap_string(ci->i_flushing_caps), 2097 ceph_cap_string(issued), ceph_cap_string(revoking), 2098 ceph_cap_string(retain), 2099 (flags & CHECK_CAPS_AUTHONLY) ? " AUTHONLY" : "", 2100 (flags & CHECK_CAPS_FLUSH) ? " FLUSH" : "", 2101 (flags & CHECK_CAPS_NOINVAL) ? " NOINVAL" : "", 2102 (flags & CHECK_CAPS_FLUSH_FORCE) ? " FLUSH_FORCE" : ""); 2103 2104 /* 2105 * If we no longer need to hold onto old our caps, and we may 2106 * have cached pages, but don't want them, then try to invalidate. 2107 * If we fail, it's because pages are locked.... try again later. 2108 */ 2109 if ((!(flags & CHECK_CAPS_NOINVAL) || mdsc->stopping) && 2110 S_ISREG(inode->i_mode) && 2111 !(ci->i_wb_ref || ci->i_wrbuffer_ref) && /* no dirty pages... */ 2112 inode->i_data.nrpages && /* have cached pages */ 2113 (revoking & (CEPH_CAP_FILE_CACHE| 2114 CEPH_CAP_FILE_LAZYIO)) && /* or revoking cache */ 2115 !tried_invalidate) { 2116 doutc(cl, "trying to invalidate on %p %llx.%llx\n", 2117 inode, ceph_vinop(inode)); 2118 if (try_nonblocking_invalidate(inode) < 0) { 2119 doutc(cl, "queuing invalidate\n"); 2120 queue_invalidate = true; 2121 ci->i_rdcache_revoking = ci->i_rdcache_gen; 2122 } 2123 tried_invalidate = true; 2124 goto retry; 2125 } 2126 2127 for (p = rb_first(&ci->i_caps); p; p = rb_next(p)) { 2128 int mflags = 0; 2129 struct cap_msg_args arg; 2130 2131 cap = rb_entry(p, struct ceph_cap, ci_node); 2132 2133 /* avoid looping forever */ 2134 if (mds >= cap->mds || 2135 ((flags & CHECK_CAPS_AUTHONLY) && cap != ci->i_auth_cap)) 2136 continue; 2137 2138 /* 2139 * If we have an auth cap, we don't need to consider any 2140 * overlapping caps as used. 2141 */ 2142 cap_used = used; 2143 if (ci->i_auth_cap && cap != ci->i_auth_cap) 2144 cap_used &= ~ci->i_auth_cap->issued; 2145 2146 revoking = cap->implemented & ~cap->issued; 2147 doutc(cl, " mds%d cap %p used %s issued %s implemented %s revoking %s\n", 2148 cap->mds, cap, ceph_cap_string(cap_used), 2149 ceph_cap_string(cap->issued), 2150 ceph_cap_string(cap->implemented), 2151 ceph_cap_string(revoking)); 2152 2153 /* completed revocation? going down and there are no caps? */ 2154 if (revoking) { 2155 if ((revoking & cap_used) == 0) { 2156 doutc(cl, "completed revocation of %s\n", 2157 ceph_cap_string(cap->implemented & ~cap->issued)); 2158 goto ack; 2159 } 2160 2161 /* 2162 * If the "i_wrbuffer_ref" was increased by mmap or generic 2163 * cache write just before the ceph_check_caps() is called, 2164 * the Fb capability revoking will fail this time. Then we 2165 * must wait for the BDI's delayed work to flush the dirty 2166 * pages and to release the "i_wrbuffer_ref", which will cost 2167 * at most 5 seconds. That means the MDS needs to wait at 2168 * most 5 seconds to finished the Fb capability's revocation. 2169 * 2170 * Let's queue a writeback for it. 2171 */ 2172 if (S_ISREG(inode->i_mode) && ci->i_wrbuffer_ref && 2173 (revoking & CEPH_CAP_FILE_BUFFER)) 2174 queue_writeback = true; 2175 } 2176 2177 if (flags & CHECK_CAPS_FLUSH_FORCE) { 2178 doutc(cl, "force to flush caps\n"); 2179 goto ack; 2180 } 2181 2182 if (cap == ci->i_auth_cap && 2183 (cap->issued & CEPH_CAP_FILE_WR)) { 2184 /* request larger max_size from MDS? */ 2185 if (ci->i_wanted_max_size > ci->i_max_size && 2186 ci->i_wanted_max_size > ci->i_requested_max_size) { 2187 doutc(cl, "requesting new max_size\n"); 2188 goto ack; 2189 } 2190 2191 /* approaching file_max? */ 2192 if (__ceph_should_report_size(ci)) { 2193 doutc(cl, "i_size approaching max_size\n"); 2194 goto ack; 2195 } 2196 } 2197 /* flush anything dirty? */ 2198 if (cap == ci->i_auth_cap) { 2199 if ((flags & CHECK_CAPS_FLUSH) && ci->i_dirty_caps) { 2200 doutc(cl, "flushing dirty caps\n"); 2201 goto ack; 2202 } 2203 if (ci->i_ceph_flags & CEPH_I_FLUSH_SNAPS) { 2204 doutc(cl, "flushing snap caps\n"); 2205 goto ack; 2206 } 2207 } 2208 2209 /* want more caps from mds? */ 2210 if (want & ~cap->mds_wanted) { 2211 if (want & ~(cap->mds_wanted | cap->issued)) 2212 goto ack; 2213 if (!__cap_is_valid(cap)) 2214 goto ack; 2215 } 2216 2217 /* things we might delay */ 2218 if ((cap->issued & ~retain) == 0) 2219 continue; /* nope, all good */ 2220 2221 ack: 2222 ceph_put_mds_session(session); 2223 session = ceph_get_mds_session(cap->session); 2224 2225 /* kick flushing and flush snaps before sending normal 2226 * cap message */ 2227 if (cap == ci->i_auth_cap && 2228 (ci->i_ceph_flags & 2229 (CEPH_I_KICK_FLUSH | CEPH_I_FLUSH_SNAPS))) { 2230 if (ci->i_ceph_flags & CEPH_I_KICK_FLUSH) 2231 __kick_flushing_caps(mdsc, session, ci, 0); 2232 if (ci->i_ceph_flags & CEPH_I_FLUSH_SNAPS) 2233 __ceph_flush_snaps(ci, session); 2234 2235 goto retry; 2236 } 2237 2238 if (cap == ci->i_auth_cap && ci->i_dirty_caps) { 2239 flushing = ci->i_dirty_caps; 2240 flush_tid = __mark_caps_flushing(inode, session, false, 2241 &oldest_flush_tid); 2242 if (flags & CHECK_CAPS_FLUSH && 2243 list_empty(&session->s_cap_dirty)) 2244 mflags |= CEPH_CLIENT_CAPS_SYNC; 2245 } else { 2246 flushing = 0; 2247 flush_tid = 0; 2248 spin_lock(&mdsc->cap_dirty_lock); 2249 oldest_flush_tid = __get_oldest_flush_tid(mdsc); 2250 spin_unlock(&mdsc->cap_dirty_lock); 2251 } 2252 2253 mds = cap->mds; /* remember mds, so we don't repeat */ 2254 2255 __prep_cap(&arg, cap, CEPH_CAP_OP_UPDATE, mflags, cap_used, 2256 want, retain, flushing, flush_tid, oldest_flush_tid); 2257 2258 spin_unlock(&ci->i_ceph_lock); 2259 __send_cap(&arg, ci); 2260 spin_lock(&ci->i_ceph_lock); 2261 2262 goto retry; /* retake i_ceph_lock and restart our cap scan. */ 2263 } 2264 2265 /* periodically re-calculate caps wanted by open files */ 2266 if (__ceph_is_any_real_caps(ci) && 2267 list_empty(&ci->i_cap_delay_list) && 2268 (file_wanted & ~CEPH_CAP_PIN) && 2269 !(used & (CEPH_CAP_FILE_RD | CEPH_CAP_ANY_FILE_WR))) { 2270 __cap_delay_requeue(mdsc, ci); 2271 } 2272 2273 spin_unlock(&ci->i_ceph_lock); 2274 2275 ceph_put_mds_session(session); 2276 if (queue_writeback) 2277 ceph_queue_writeback(inode); 2278 if (queue_invalidate) 2279 ceph_queue_invalidate(inode); 2280 } 2281 2282 /* 2283 * Try to flush dirty caps back to the auth mds. 2284 */ 2285 static int try_flush_caps(struct inode *inode, u64 *ptid) 2286 { 2287 struct ceph_mds_client *mdsc = ceph_sb_to_fs_client(inode->i_sb)->mdsc; 2288 struct ceph_inode_info *ci = ceph_inode(inode); 2289 int flushing = 0; 2290 u64 flush_tid = 0, oldest_flush_tid = 0; 2291 2292 spin_lock(&ci->i_ceph_lock); 2293 retry_locked: 2294 if (ci->i_dirty_caps && ci->i_auth_cap) { 2295 struct ceph_cap *cap = ci->i_auth_cap; 2296 struct cap_msg_args arg; 2297 struct ceph_mds_session *session = cap->session; 2298 2299 if (session->s_state < CEPH_MDS_SESSION_OPEN) { 2300 spin_unlock(&ci->i_ceph_lock); 2301 goto out; 2302 } 2303 2304 if (ci->i_ceph_flags & 2305 (CEPH_I_KICK_FLUSH | CEPH_I_FLUSH_SNAPS)) { 2306 if (ci->i_ceph_flags & CEPH_I_KICK_FLUSH) 2307 __kick_flushing_caps(mdsc, session, ci, 0); 2308 if (ci->i_ceph_flags & CEPH_I_FLUSH_SNAPS) 2309 __ceph_flush_snaps(ci, session); 2310 goto retry_locked; 2311 } 2312 2313 flushing = ci->i_dirty_caps; 2314 flush_tid = __mark_caps_flushing(inode, session, true, 2315 &oldest_flush_tid); 2316 2317 __prep_cap(&arg, cap, CEPH_CAP_OP_FLUSH, CEPH_CLIENT_CAPS_SYNC, 2318 __ceph_caps_used(ci), __ceph_caps_wanted(ci), 2319 (cap->issued | cap->implemented), 2320 flushing, flush_tid, oldest_flush_tid); 2321 spin_unlock(&ci->i_ceph_lock); 2322 2323 __send_cap(&arg, ci); 2324 } else { 2325 if (!list_empty(&ci->i_cap_flush_list)) { 2326 struct ceph_cap_flush *cf = 2327 list_last_entry(&ci->i_cap_flush_list, 2328 struct ceph_cap_flush, i_list); 2329 cf->wake = true; 2330 flush_tid = cf->tid; 2331 } 2332 flushing = ci->i_flushing_caps; 2333 spin_unlock(&ci->i_ceph_lock); 2334 } 2335 out: 2336 *ptid = flush_tid; 2337 return flushing; 2338 } 2339 2340 /* 2341 * Return true if we've flushed caps through the given flush_tid. 2342 */ 2343 static int caps_are_flushed(struct inode *inode, u64 flush_tid) 2344 { 2345 struct ceph_inode_info *ci = ceph_inode(inode); 2346 int ret = 1; 2347 2348 spin_lock(&ci->i_ceph_lock); 2349 if (!list_empty(&ci->i_cap_flush_list)) { 2350 struct ceph_cap_flush * cf = 2351 list_first_entry(&ci->i_cap_flush_list, 2352 struct ceph_cap_flush, i_list); 2353 if (cf->tid <= flush_tid) 2354 ret = 0; 2355 } 2356 spin_unlock(&ci->i_ceph_lock); 2357 return ret; 2358 } 2359 2360 /* 2361 * flush the mdlog and wait for any unsafe requests to complete. 2362 */ 2363 static int flush_mdlog_and_wait_inode_unsafe_requests(struct inode *inode) 2364 { 2365 struct ceph_mds_client *mdsc = ceph_sb_to_fs_client(inode->i_sb)->mdsc; 2366 struct ceph_client *cl = ceph_inode_to_client(inode); 2367 struct ceph_inode_info *ci = ceph_inode(inode); 2368 struct ceph_mds_request *req1 = NULL, *req2 = NULL; 2369 int ret, err = 0; 2370 2371 spin_lock(&ci->i_unsafe_lock); 2372 if (S_ISDIR(inode->i_mode) && !list_empty(&ci->i_unsafe_dirops)) { 2373 req1 = list_last_entry(&ci->i_unsafe_dirops, 2374 struct ceph_mds_request, 2375 r_unsafe_dir_item); 2376 ceph_mdsc_get_request(req1); 2377 } 2378 if (!list_empty(&ci->i_unsafe_iops)) { 2379 req2 = list_last_entry(&ci->i_unsafe_iops, 2380 struct ceph_mds_request, 2381 r_unsafe_target_item); 2382 ceph_mdsc_get_request(req2); 2383 } 2384 spin_unlock(&ci->i_unsafe_lock); 2385 2386 /* 2387 * Trigger to flush the journal logs in all the relevant MDSes 2388 * manually, or in the worst case we must wait at most 5 seconds 2389 * to wait the journal logs to be flushed by the MDSes periodically. 2390 */ 2391 if (req1 || req2) { 2392 struct ceph_mds_request *req; 2393 struct ceph_mds_session **sessions; 2394 struct ceph_mds_session *s; 2395 unsigned int max_sessions; 2396 int i; 2397 2398 mutex_lock(&mdsc->mutex); 2399 max_sessions = mdsc->max_sessions; 2400 2401 sessions = kzalloc_objs(s, max_sessions); 2402 if (!sessions) { 2403 mutex_unlock(&mdsc->mutex); 2404 err = -ENOMEM; 2405 goto out; 2406 } 2407 2408 spin_lock(&ci->i_unsafe_lock); 2409 if (req1) { 2410 list_for_each_entry(req, &ci->i_unsafe_dirops, 2411 r_unsafe_dir_item) { 2412 s = req->r_session; 2413 if (!s) 2414 continue; 2415 if (!sessions[s->s_mds]) { 2416 s = ceph_get_mds_session(s); 2417 sessions[s->s_mds] = s; 2418 } 2419 } 2420 } 2421 if (req2) { 2422 list_for_each_entry(req, &ci->i_unsafe_iops, 2423 r_unsafe_target_item) { 2424 s = req->r_session; 2425 if (!s) 2426 continue; 2427 if (!sessions[s->s_mds]) { 2428 s = ceph_get_mds_session(s); 2429 sessions[s->s_mds] = s; 2430 } 2431 } 2432 } 2433 spin_unlock(&ci->i_unsafe_lock); 2434 2435 /* the auth MDS */ 2436 spin_lock(&ci->i_ceph_lock); 2437 if (ci->i_auth_cap) { 2438 s = ci->i_auth_cap->session; 2439 if (!sessions[s->s_mds]) 2440 sessions[s->s_mds] = ceph_get_mds_session(s); 2441 } 2442 spin_unlock(&ci->i_ceph_lock); 2443 mutex_unlock(&mdsc->mutex); 2444 2445 /* send flush mdlog request to MDSes */ 2446 for (i = 0; i < max_sessions; i++) { 2447 s = sessions[i]; 2448 if (s) { 2449 send_flush_mdlog(s); 2450 ceph_put_mds_session(s); 2451 } 2452 } 2453 kfree(sessions); 2454 } 2455 2456 doutc(cl, "%p %llx.%llx wait on tid %llu %llu\n", inode, 2457 ceph_vinop(inode), req1 ? req1->r_tid : 0ULL, 2458 req2 ? req2->r_tid : 0ULL); 2459 if (req1) { 2460 ret = !wait_for_completion_timeout(&req1->r_safe_completion, 2461 ceph_timeout_jiffies(req1->r_timeout)); 2462 if (ret) 2463 err = -EIO; 2464 } 2465 if (req2) { 2466 ret = !wait_for_completion_timeout(&req2->r_safe_completion, 2467 ceph_timeout_jiffies(req2->r_timeout)); 2468 if (ret) 2469 err = -EIO; 2470 } 2471 2472 out: 2473 if (req1) 2474 ceph_mdsc_put_request(req1); 2475 if (req2) 2476 ceph_mdsc_put_request(req2); 2477 return err; 2478 } 2479 2480 int ceph_fsync(struct file *file, loff_t start, loff_t end, int datasync) 2481 { 2482 struct inode *inode = file->f_mapping->host; 2483 struct ceph_inode_info *ci = ceph_inode(inode); 2484 struct ceph_client *cl = ceph_inode_to_client(inode); 2485 u64 flush_tid; 2486 int ret, err; 2487 int dirty; 2488 2489 doutc(cl, "%p %llx.%llx%s\n", inode, ceph_vinop(inode), 2490 datasync ? " datasync" : ""); 2491 2492 ret = file_write_and_wait_range(file, start, end); 2493 if (datasync) 2494 goto out; 2495 2496 ret = ceph_wait_on_async_create(inode); 2497 if (ret) 2498 goto out; 2499 2500 dirty = try_flush_caps(inode, &flush_tid); 2501 doutc(cl, "dirty caps are %s\n", ceph_cap_string(dirty)); 2502 2503 err = flush_mdlog_and_wait_inode_unsafe_requests(inode); 2504 2505 /* 2506 * only wait on non-file metadata writeback (the mds 2507 * can recover size and mtime, so we don't need to 2508 * wait for that) 2509 */ 2510 if (!err && (dirty & ~CEPH_CAP_ANY_FILE_WR)) { 2511 err = wait_event_interruptible(ci->i_cap_wq, 2512 caps_are_flushed(inode, flush_tid)); 2513 } 2514 2515 if (err < 0) 2516 ret = err; 2517 2518 err = file_check_and_advance_wb_err(file); 2519 if (err < 0) 2520 ret = err; 2521 out: 2522 doutc(cl, "%p %llx.%llx%s result=%d\n", inode, ceph_vinop(inode), 2523 datasync ? " datasync" : "", ret); 2524 return ret; 2525 } 2526 2527 /* 2528 * Flush any dirty caps back to the mds. If we aren't asked to wait, 2529 * queue inode for flush but don't do so immediately, because we can 2530 * get by with fewer MDS messages if we wait for data writeback to 2531 * complete first. 2532 */ 2533 int ceph_write_inode(struct inode *inode, struct writeback_control *wbc) 2534 { 2535 struct ceph_inode_info *ci = ceph_inode(inode); 2536 struct ceph_client *cl = ceph_inode_to_client(inode); 2537 u64 flush_tid; 2538 int err = 0; 2539 int dirty; 2540 int wait = (wbc->sync_mode == WB_SYNC_ALL && !wbc->for_sync); 2541 2542 doutc(cl, "%p %llx.%llx wait=%d\n", inode, ceph_vinop(inode), wait); 2543 ceph_fscache_unpin_writeback(inode, wbc); 2544 if (wait) { 2545 err = ceph_wait_on_async_create(inode); 2546 if (err) 2547 return err; 2548 dirty = try_flush_caps(inode, &flush_tid); 2549 if (dirty) 2550 err = wait_event_interruptible(ci->i_cap_wq, 2551 caps_are_flushed(inode, flush_tid)); 2552 } else { 2553 struct ceph_mds_client *mdsc = 2554 ceph_sb_to_fs_client(inode->i_sb)->mdsc; 2555 2556 spin_lock(&ci->i_ceph_lock); 2557 if (__ceph_caps_dirty(ci)) 2558 __cap_delay_requeue_front(mdsc, ci); 2559 spin_unlock(&ci->i_ceph_lock); 2560 } 2561 return err; 2562 } 2563 2564 static void __kick_flushing_caps(struct ceph_mds_client *mdsc, 2565 struct ceph_mds_session *session, 2566 struct ceph_inode_info *ci, 2567 u64 oldest_flush_tid) 2568 __releases(ci->i_ceph_lock) 2569 __acquires(ci->i_ceph_lock) 2570 { 2571 struct inode *inode = &ci->netfs.inode; 2572 struct ceph_client *cl = mdsc->fsc->client; 2573 struct ceph_cap *cap; 2574 struct ceph_cap_flush *cf; 2575 int ret; 2576 u64 first_tid = 0; 2577 u64 last_snap_flush = 0; 2578 2579 /* Don't do anything until create reply comes in */ 2580 if (ci->i_ceph_flags & CEPH_I_ASYNC_CREATE) 2581 return; 2582 2583 clear_bit(CEPH_I_KICK_FLUSH_BIT, &ci->i_ceph_flags); 2584 2585 list_for_each_entry_reverse(cf, &ci->i_cap_flush_list, i_list) { 2586 if (cf->is_capsnap) { 2587 last_snap_flush = cf->tid; 2588 break; 2589 } 2590 } 2591 2592 list_for_each_entry(cf, &ci->i_cap_flush_list, i_list) { 2593 if (cf->tid < first_tid) 2594 continue; 2595 2596 cap = ci->i_auth_cap; 2597 if (!(cap && cap->session == session)) { 2598 pr_err_client(cl, "%p auth cap %p not mds%d ???\n", 2599 inode, cap, session->s_mds); 2600 break; 2601 } 2602 2603 first_tid = cf->tid + 1; 2604 2605 if (!cf->is_capsnap) { 2606 struct cap_msg_args arg; 2607 2608 doutc(cl, "%p %llx.%llx cap %p tid %llu %s\n", 2609 inode, ceph_vinop(inode), cap, cf->tid, 2610 ceph_cap_string(cf->caps)); 2611 __prep_cap(&arg, cap, CEPH_CAP_OP_FLUSH, 2612 (cf->tid < last_snap_flush ? 2613 CEPH_CLIENT_CAPS_PENDING_CAPSNAP : 0), 2614 __ceph_caps_used(ci), 2615 __ceph_caps_wanted(ci), 2616 (cap->issued | cap->implemented), 2617 cf->caps, cf->tid, oldest_flush_tid); 2618 spin_unlock(&ci->i_ceph_lock); 2619 __send_cap(&arg, ci); 2620 } else { 2621 struct ceph_cap_snap *capsnap = 2622 container_of(cf, struct ceph_cap_snap, 2623 cap_flush); 2624 doutc(cl, "%p %llx.%llx capsnap %p tid %llu %s\n", 2625 inode, ceph_vinop(inode), capsnap, cf->tid, 2626 ceph_cap_string(capsnap->dirty)); 2627 2628 refcount_inc(&capsnap->nref); 2629 spin_unlock(&ci->i_ceph_lock); 2630 2631 ret = __send_flush_snap(inode, session, capsnap, cap->mseq, 2632 oldest_flush_tid); 2633 if (ret < 0) { 2634 pr_err_client(cl, "error sending cap flushsnap," 2635 " %p %llx.%llx tid %llu follows %llu\n", 2636 inode, ceph_vinop(inode), cf->tid, 2637 capsnap->follows); 2638 } 2639 2640 ceph_put_cap_snap(capsnap); 2641 } 2642 2643 spin_lock(&ci->i_ceph_lock); 2644 } 2645 } 2646 2647 void ceph_early_kick_flushing_caps(struct ceph_mds_client *mdsc, 2648 struct ceph_mds_session *session) 2649 { 2650 struct ceph_client *cl = mdsc->fsc->client; 2651 struct ceph_inode_info *ci; 2652 struct ceph_cap *cap; 2653 u64 oldest_flush_tid; 2654 2655 doutc(cl, "mds%d\n", session->s_mds); 2656 2657 spin_lock(&mdsc->cap_dirty_lock); 2658 oldest_flush_tid = __get_oldest_flush_tid(mdsc); 2659 spin_unlock(&mdsc->cap_dirty_lock); 2660 2661 list_for_each_entry(ci, &session->s_cap_flushing, i_flushing_item) { 2662 struct inode *inode = &ci->netfs.inode; 2663 2664 spin_lock(&ci->i_ceph_lock); 2665 cap = ci->i_auth_cap; 2666 if (!(cap && cap->session == session)) { 2667 pr_err_client(cl, "%p %llx.%llx auth cap %p not mds%d ???\n", 2668 inode, ceph_vinop(inode), cap, 2669 session->s_mds); 2670 spin_unlock(&ci->i_ceph_lock); 2671 continue; 2672 } 2673 2674 2675 /* 2676 * if flushing caps were revoked, we re-send the cap flush 2677 * in client reconnect stage. This guarantees MDS * processes 2678 * the cap flush message before issuing the flushing caps to 2679 * other client. 2680 */ 2681 if ((cap->issued & ci->i_flushing_caps) != 2682 ci->i_flushing_caps) { 2683 /* encode_caps_cb() also will reset these sequence 2684 * numbers. make sure sequence numbers in cap flush 2685 * message match later reconnect message */ 2686 cap->seq = 0; 2687 cap->issue_seq = 0; 2688 cap->mseq = 0; 2689 __kick_flushing_caps(mdsc, session, ci, 2690 oldest_flush_tid); 2691 } else { 2692 set_bit(CEPH_I_KICK_FLUSH_BIT, &ci->i_ceph_flags); 2693 } 2694 2695 spin_unlock(&ci->i_ceph_lock); 2696 } 2697 } 2698 2699 void ceph_kick_flushing_caps(struct ceph_mds_client *mdsc, 2700 struct ceph_mds_session *session) 2701 { 2702 struct ceph_client *cl = mdsc->fsc->client; 2703 struct ceph_inode_info *ci; 2704 struct ceph_cap *cap; 2705 u64 oldest_flush_tid; 2706 2707 lockdep_assert_held(&session->s_mutex); 2708 2709 doutc(cl, "mds%d\n", session->s_mds); 2710 2711 spin_lock(&mdsc->cap_dirty_lock); 2712 oldest_flush_tid = __get_oldest_flush_tid(mdsc); 2713 spin_unlock(&mdsc->cap_dirty_lock); 2714 2715 list_for_each_entry(ci, &session->s_cap_flushing, i_flushing_item) { 2716 struct inode *inode = &ci->netfs.inode; 2717 2718 spin_lock(&ci->i_ceph_lock); 2719 cap = ci->i_auth_cap; 2720 if (!(cap && cap->session == session)) { 2721 pr_err_client(cl, "%p %llx.%llx auth cap %p not mds%d ???\n", 2722 inode, ceph_vinop(inode), cap, 2723 session->s_mds); 2724 spin_unlock(&ci->i_ceph_lock); 2725 continue; 2726 } 2727 if (ci->i_ceph_flags & CEPH_I_KICK_FLUSH) { 2728 __kick_flushing_caps(mdsc, session, ci, 2729 oldest_flush_tid); 2730 } 2731 spin_unlock(&ci->i_ceph_lock); 2732 } 2733 } 2734 2735 void ceph_kick_flushing_inode_caps(struct ceph_mds_session *session, 2736 struct ceph_inode_info *ci) 2737 { 2738 struct ceph_mds_client *mdsc = session->s_mdsc; 2739 struct ceph_cap *cap = ci->i_auth_cap; 2740 struct inode *inode = &ci->netfs.inode; 2741 2742 lockdep_assert_held(&ci->i_ceph_lock); 2743 2744 doutc(mdsc->fsc->client, "%p %llx.%llx flushing %s\n", 2745 inode, ceph_vinop(inode), 2746 ceph_cap_string(ci->i_flushing_caps)); 2747 2748 if (!list_empty(&ci->i_cap_flush_list)) { 2749 u64 oldest_flush_tid; 2750 spin_lock(&mdsc->cap_dirty_lock); 2751 list_move_tail(&ci->i_flushing_item, 2752 &cap->session->s_cap_flushing); 2753 oldest_flush_tid = __get_oldest_flush_tid(mdsc); 2754 spin_unlock(&mdsc->cap_dirty_lock); 2755 2756 __kick_flushing_caps(mdsc, session, ci, oldest_flush_tid); 2757 } 2758 } 2759 2760 2761 /* 2762 * Take references to capabilities we hold, so that we don't release 2763 * them to the MDS prematurely. 2764 */ 2765 void ceph_take_cap_refs(struct ceph_inode_info *ci, int got, 2766 bool snap_rwsem_locked) 2767 { 2768 struct inode *inode = &ci->netfs.inode; 2769 struct ceph_client *cl = ceph_inode_to_client(inode); 2770 2771 lockdep_assert_held(&ci->i_ceph_lock); 2772 2773 if (got & CEPH_CAP_PIN) 2774 ci->i_pin_ref++; 2775 if (got & CEPH_CAP_FILE_RD) 2776 ci->i_rd_ref++; 2777 if (got & CEPH_CAP_FILE_CACHE) 2778 ci->i_rdcache_ref++; 2779 if (got & CEPH_CAP_FILE_EXCL) 2780 ci->i_fx_ref++; 2781 if (got & CEPH_CAP_FILE_WR) { 2782 if (ci->i_wr_ref == 0 && !ci->i_head_snapc) { 2783 BUG_ON(!snap_rwsem_locked); 2784 ci->i_head_snapc = ceph_get_snap_context( 2785 ci->i_snap_realm->cached_context); 2786 } 2787 ci->i_wr_ref++; 2788 } 2789 if (got & CEPH_CAP_FILE_BUFFER) { 2790 if (ci->i_wb_ref == 0) 2791 ihold(inode); 2792 ci->i_wb_ref++; 2793 doutc(cl, "%p %llx.%llx wb %d -> %d (?)\n", inode, 2794 ceph_vinop(inode), ci->i_wb_ref-1, ci->i_wb_ref); 2795 } 2796 } 2797 2798 /* 2799 * Try to grab cap references. Specify those refs we @want, and the 2800 * minimal set we @need. Also include the larger offset we are writing 2801 * to (when applicable), and check against max_size here as well. 2802 * Note that caller is responsible for ensuring max_size increases are 2803 * requested from the MDS. 2804 * 2805 * Returns 0 if caps were not able to be acquired (yet), 1 if succeed, 2806 * or a negative error code. There are 3 special error codes: 2807 * -EAGAIN: need to sleep but non-blocking is specified 2808 * -EFBIG: ask caller to call check_max_size() and try again. 2809 * -EUCLEAN: ask caller to call ceph_renew_caps() and try again. 2810 */ 2811 enum { 2812 /* first 8 bits are reserved for CEPH_FILE_MODE_FOO */ 2813 NON_BLOCKING = (1 << 8), 2814 CHECK_FILELOCK = (1 << 9), 2815 }; 2816 2817 static int try_get_cap_refs(struct inode *inode, int need, int want, 2818 loff_t endoff, int flags, int *got) 2819 { 2820 struct ceph_inode_info *ci = ceph_inode(inode); 2821 struct ceph_mds_client *mdsc = ceph_inode_to_fs_client(inode)->mdsc; 2822 struct ceph_client *cl = ceph_inode_to_client(inode); 2823 int ret = 0; 2824 int have, implemented; 2825 bool snap_rwsem_locked = false; 2826 2827 doutc(cl, "%p %llx.%llx need %s want %s\n", inode, 2828 ceph_vinop(inode), ceph_cap_string(need), 2829 ceph_cap_string(want)); 2830 2831 again: 2832 spin_lock(&ci->i_ceph_lock); 2833 2834 if ((flags & CHECK_FILELOCK) && 2835 test_bit(CEPH_I_ERROR_FILELOCK_BIT, &ci->i_ceph_flags)) { 2836 doutc(cl, "%p %llx.%llx error filelock\n", inode, 2837 ceph_vinop(inode)); 2838 ret = -EIO; 2839 goto out_unlock; 2840 } 2841 2842 /* finish pending truncate */ 2843 while (ci->i_truncate_pending) { 2844 spin_unlock(&ci->i_ceph_lock); 2845 if (snap_rwsem_locked) { 2846 up_read(&mdsc->snap_rwsem); 2847 snap_rwsem_locked = false; 2848 } 2849 __ceph_do_pending_vmtruncate(inode); 2850 spin_lock(&ci->i_ceph_lock); 2851 } 2852 2853 have = __ceph_caps_issued(ci, &implemented); 2854 2855 if (have & need & CEPH_CAP_FILE_WR) { 2856 if (endoff >= 0 && endoff > (loff_t)ci->i_max_size) { 2857 doutc(cl, "%p %llx.%llx endoff %llu > maxsize %llu\n", 2858 inode, ceph_vinop(inode), endoff, ci->i_max_size); 2859 if (endoff > ci->i_requested_max_size) 2860 ret = ci->i_auth_cap ? -EFBIG : -EUCLEAN; 2861 goto out_unlock; 2862 } 2863 /* 2864 * If a sync write is in progress, we must wait, so that we 2865 * can get a final snapshot value for size+mtime. 2866 */ 2867 if (__ceph_have_pending_cap_snap(ci)) { 2868 doutc(cl, "%p %llx.%llx cap_snap_pending\n", inode, 2869 ceph_vinop(inode)); 2870 goto out_unlock; 2871 } 2872 } 2873 2874 if ((have & need) == need) { 2875 /* 2876 * Look at (implemented & ~have & not) so that we keep waiting 2877 * on transition from wanted -> needed caps. This is needed 2878 * for WRBUFFER|WR -> WR to avoid a new WR sync write from 2879 * going before a prior buffered writeback happens. 2880 * 2881 * For RDCACHE|RD -> RD, there is not need to wait and we can 2882 * just exclude the revoking caps and force to sync read. 2883 */ 2884 int not = want & ~(have & need); 2885 int revoking = implemented & ~have; 2886 int exclude = revoking & not; 2887 doutc(cl, "%p %llx.%llx have %s but not %s (revoking %s)\n", 2888 inode, ceph_vinop(inode), ceph_cap_string(have), 2889 ceph_cap_string(not), ceph_cap_string(revoking)); 2890 if (!exclude || !(exclude & CEPH_CAP_FILE_BUFFER)) { 2891 if (!snap_rwsem_locked && 2892 !ci->i_head_snapc && 2893 (need & CEPH_CAP_FILE_WR)) { 2894 if (!down_read_trylock(&mdsc->snap_rwsem)) { 2895 /* 2896 * we can not call down_read() when 2897 * task isn't in TASK_RUNNING state 2898 */ 2899 if (flags & NON_BLOCKING) { 2900 ret = -EAGAIN; 2901 goto out_unlock; 2902 } 2903 2904 spin_unlock(&ci->i_ceph_lock); 2905 down_read(&mdsc->snap_rwsem); 2906 snap_rwsem_locked = true; 2907 goto again; 2908 } 2909 snap_rwsem_locked = true; 2910 } 2911 if ((have & want) == want) 2912 *got = need | (want & ~exclude); 2913 else 2914 *got = need; 2915 ceph_take_cap_refs(ci, *got, true); 2916 ret = 1; 2917 } 2918 } else { 2919 int session_readonly = false; 2920 int mds_wanted; 2921 if (ci->i_auth_cap && 2922 (need & (CEPH_CAP_FILE_WR | CEPH_CAP_FILE_EXCL))) { 2923 struct ceph_mds_session *s = ci->i_auth_cap->session; 2924 spin_lock(&s->s_cap_lock); 2925 session_readonly = s->s_readonly; 2926 spin_unlock(&s->s_cap_lock); 2927 } 2928 if (session_readonly) { 2929 doutc(cl, "%p %llx.%llx need %s but mds%d readonly\n", 2930 inode, ceph_vinop(inode), ceph_cap_string(need), 2931 ci->i_auth_cap->mds); 2932 ret = -EROFS; 2933 goto out_unlock; 2934 } 2935 2936 if (ceph_inode_is_shutdown(inode)) { 2937 doutc(cl, "%p %llx.%llx inode is shutdown\n", 2938 inode, ceph_vinop(inode)); 2939 ret = -ESTALE; 2940 goto out_unlock; 2941 } 2942 mds_wanted = __ceph_caps_mds_wanted(ci, false); 2943 if (need & ~mds_wanted) { 2944 doutc(cl, "%p %llx.%llx need %s > mds_wanted %s\n", 2945 inode, ceph_vinop(inode), ceph_cap_string(need), 2946 ceph_cap_string(mds_wanted)); 2947 ret = -EUCLEAN; 2948 goto out_unlock; 2949 } 2950 2951 doutc(cl, "%p %llx.%llx have %s need %s\n", inode, 2952 ceph_vinop(inode), ceph_cap_string(have), 2953 ceph_cap_string(need)); 2954 } 2955 out_unlock: 2956 2957 __ceph_touch_fmode(ci, mdsc, flags); 2958 2959 spin_unlock(&ci->i_ceph_lock); 2960 if (snap_rwsem_locked) 2961 up_read(&mdsc->snap_rwsem); 2962 2963 if (!ret) 2964 ceph_update_cap_mis(&mdsc->metric); 2965 else if (ret == 1) 2966 ceph_update_cap_hit(&mdsc->metric); 2967 2968 doutc(cl, "%p %llx.%llx ret %d got %s\n", inode, 2969 ceph_vinop(inode), ret, ceph_cap_string(*got)); 2970 return ret; 2971 } 2972 2973 /* 2974 * Check the offset we are writing up to against our current 2975 * max_size. If necessary, tell the MDS we want to write to 2976 * a larger offset. 2977 */ 2978 static void check_max_size(struct inode *inode, loff_t endoff) 2979 { 2980 struct ceph_inode_info *ci = ceph_inode(inode); 2981 struct ceph_client *cl = ceph_inode_to_client(inode); 2982 int check = 0; 2983 2984 /* do we need to explicitly request a larger max_size? */ 2985 spin_lock(&ci->i_ceph_lock); 2986 if (endoff >= ci->i_max_size && endoff > ci->i_wanted_max_size) { 2987 doutc(cl, "write %p %llx.%llx at large endoff %llu, req max_size\n", 2988 inode, ceph_vinop(inode), endoff); 2989 ci->i_wanted_max_size = endoff; 2990 } 2991 /* duplicate ceph_check_caps()'s logic */ 2992 if (ci->i_auth_cap && 2993 (ci->i_auth_cap->issued & CEPH_CAP_FILE_WR) && 2994 ci->i_wanted_max_size > ci->i_max_size && 2995 ci->i_wanted_max_size > ci->i_requested_max_size) 2996 check = 1; 2997 spin_unlock(&ci->i_ceph_lock); 2998 if (check) 2999 ceph_check_caps(ci, CHECK_CAPS_AUTHONLY); 3000 } 3001 3002 static inline int get_used_fmode(int caps) 3003 { 3004 int fmode = 0; 3005 if (caps & CEPH_CAP_FILE_RD) 3006 fmode |= CEPH_FILE_MODE_RD; 3007 if (caps & CEPH_CAP_FILE_WR) 3008 fmode |= CEPH_FILE_MODE_WR; 3009 return fmode; 3010 } 3011 3012 int ceph_try_get_caps(struct inode *inode, int need, int want, 3013 bool nonblock, int *got) 3014 { 3015 int ret, flags; 3016 3017 BUG_ON(need & ~CEPH_CAP_FILE_RD); 3018 BUG_ON(want & ~(CEPH_CAP_FILE_CACHE | CEPH_CAP_FILE_LAZYIO | 3019 CEPH_CAP_FILE_SHARED | CEPH_CAP_FILE_EXCL | 3020 CEPH_CAP_ANY_DIR_OPS)); 3021 if (need) { 3022 ret = ceph_pool_perm_check(inode, need); 3023 if (ret < 0) 3024 return ret; 3025 } 3026 3027 flags = get_used_fmode(need | want); 3028 if (nonblock) 3029 flags |= NON_BLOCKING; 3030 3031 ret = try_get_cap_refs(inode, need, want, 0, flags, got); 3032 /* three special error codes */ 3033 if (ret == -EAGAIN || ret == -EFBIG || ret == -EUCLEAN) 3034 ret = 0; 3035 return ret; 3036 } 3037 3038 /* 3039 * Wait for caps, and take cap references. If we can't get a WR cap 3040 * due to a small max_size, make sure we check_max_size (and possibly 3041 * ask the mds) so we don't get hung up indefinitely. 3042 */ 3043 int __ceph_get_caps(struct inode *inode, struct ceph_file_info *fi, int need, 3044 int want, loff_t endoff, int *got) 3045 { 3046 struct ceph_inode_info *ci = ceph_inode(inode); 3047 struct ceph_fs_client *fsc = ceph_inode_to_fs_client(inode); 3048 int ret, _got, flags; 3049 3050 ret = ceph_pool_perm_check(inode, need); 3051 if (ret < 0) 3052 return ret; 3053 3054 if (fi && (fi->fmode & CEPH_FILE_MODE_WR) && 3055 fi->filp_gen != READ_ONCE(fsc->filp_gen)) 3056 return -EBADF; 3057 3058 flags = get_used_fmode(need | want); 3059 3060 while (true) { 3061 flags &= CEPH_FILE_MODE_MASK; 3062 if (vfs_inode_has_locks(inode)) 3063 flags |= CHECK_FILELOCK; 3064 _got = 0; 3065 ret = try_get_cap_refs(inode, need, want, endoff, 3066 flags, &_got); 3067 WARN_ON_ONCE(ret == -EAGAIN); 3068 if (!ret) { 3069 #ifdef CONFIG_DEBUG_FS 3070 struct ceph_mds_client *mdsc = fsc->mdsc; 3071 struct cap_wait cw; 3072 #endif 3073 DEFINE_WAIT_FUNC(wait, woken_wake_function); 3074 3075 #ifdef CONFIG_DEBUG_FS 3076 cw.ino = ceph_ino(inode); 3077 cw.tgid = current->tgid; 3078 cw.need = need; 3079 cw.want = want; 3080 3081 spin_lock(&mdsc->caps_list_lock); 3082 list_add(&cw.list, &mdsc->cap_wait_list); 3083 spin_unlock(&mdsc->caps_list_lock); 3084 #endif 3085 3086 /* make sure used fmode not timeout */ 3087 ceph_get_fmode(ci, flags, FMODE_WAIT_BIAS); 3088 add_wait_queue(&ci->i_cap_wq, &wait); 3089 3090 flags |= NON_BLOCKING; 3091 while (!(ret = try_get_cap_refs(inode, need, want, 3092 endoff, flags, &_got))) { 3093 if (signal_pending(current)) { 3094 ret = -ERESTARTSYS; 3095 break; 3096 } 3097 3098 /* 3099 * If a cap update is lost after 3100 * mds_wanted was raised, waiting 3101 * forever will never make progress. 3102 * Retry the renew path periodically 3103 * so we can resend synchronously. 3104 */ 3105 if (!wait_woken(&wait, TASK_INTERRUPTIBLE, 3106 CEPH_GET_CAPS_WAIT_TIMEOUT)) { 3107 ret = -EUCLEAN; 3108 break; 3109 } 3110 } 3111 3112 remove_wait_queue(&ci->i_cap_wq, &wait); 3113 ceph_put_fmode(ci, flags, FMODE_WAIT_BIAS); 3114 3115 #ifdef CONFIG_DEBUG_FS 3116 spin_lock(&mdsc->caps_list_lock); 3117 list_del(&cw.list); 3118 spin_unlock(&mdsc->caps_list_lock); 3119 #endif 3120 3121 if (ret == -EAGAIN) 3122 continue; 3123 } 3124 3125 if (fi && (fi->fmode & CEPH_FILE_MODE_WR) && 3126 fi->filp_gen != READ_ONCE(fsc->filp_gen)) { 3127 if (ret >= 0 && _got) 3128 ceph_put_cap_refs(ci, _got); 3129 return -EBADF; 3130 } 3131 3132 if (ret < 0) { 3133 if (ret == -EFBIG || ret == -EUCLEAN) { 3134 int ret2 = ceph_wait_on_async_create(inode); 3135 if (ret2 < 0) 3136 return ret2; 3137 } 3138 if (ret == -EFBIG) { 3139 check_max_size(inode, endoff); 3140 continue; 3141 } 3142 if (ret == -EUCLEAN) { 3143 /* session was killed or a waited cap 3144 * request needs a retry */ 3145 ret = ceph_renew_caps(inode, flags); 3146 if (ret == 0) 3147 continue; 3148 } 3149 return ret; 3150 } 3151 3152 if (S_ISREG(ci->netfs.inode.i_mode) && 3153 ceph_has_inline_data(ci) && 3154 (_got & (CEPH_CAP_FILE_CACHE|CEPH_CAP_FILE_LAZYIO)) && 3155 i_size_read(inode) > 0) { 3156 struct page *page = 3157 find_get_page(inode->i_mapping, 0); 3158 if (page) { 3159 bool uptodate = PageUptodate(page); 3160 3161 put_page(page); 3162 if (uptodate) 3163 break; 3164 } 3165 /* 3166 * drop cap refs first because getattr while 3167 * holding * caps refs can cause deadlock. 3168 */ 3169 ceph_put_cap_refs(ci, _got); 3170 _got = 0; 3171 3172 /* 3173 * getattr request will bring inline data into 3174 * page cache 3175 */ 3176 ret = __ceph_do_getattr(inode, NULL, 3177 CEPH_STAT_CAP_INLINE_DATA, 3178 true); 3179 if (ret < 0) 3180 return ret; 3181 continue; 3182 } 3183 break; 3184 } 3185 *got = _got; 3186 return 0; 3187 } 3188 3189 int ceph_get_caps(struct file *filp, int need, int want, loff_t endoff, 3190 int *got) 3191 { 3192 struct ceph_file_info *fi = filp->private_data; 3193 struct inode *inode = file_inode(filp); 3194 3195 return __ceph_get_caps(inode, fi, need, want, endoff, got); 3196 } 3197 3198 /* 3199 * Take cap refs. Caller must already know we hold at least one ref 3200 * on the caps in question or we don't know this is safe. 3201 */ 3202 void ceph_get_cap_refs(struct ceph_inode_info *ci, int caps) 3203 { 3204 spin_lock(&ci->i_ceph_lock); 3205 ceph_take_cap_refs(ci, caps, false); 3206 spin_unlock(&ci->i_ceph_lock); 3207 } 3208 3209 3210 /* 3211 * drop cap_snap that is not associated with any snapshot. 3212 * we don't need to send FLUSHSNAP message for it. 3213 */ 3214 static int ceph_try_drop_cap_snap(struct ceph_inode_info *ci, 3215 struct ceph_cap_snap *capsnap) 3216 { 3217 struct inode *inode = &ci->netfs.inode; 3218 struct ceph_client *cl = ceph_inode_to_client(inode); 3219 3220 if (!capsnap->need_flush && 3221 !capsnap->writing && !capsnap->dirty_pages) { 3222 doutc(cl, "%p follows %llu\n", capsnap, capsnap->follows); 3223 BUG_ON(capsnap->cap_flush.tid > 0); 3224 ceph_put_snap_context(capsnap->context); 3225 if (!list_is_last(&capsnap->ci_item, &ci->i_cap_snaps)) 3226 set_bit(CEPH_I_FLUSH_SNAPS_BIT, &ci->i_ceph_flags); 3227 3228 list_del(&capsnap->ci_item); 3229 ceph_put_cap_snap(capsnap); 3230 return 1; 3231 } 3232 return 0; 3233 } 3234 3235 enum put_cap_refs_mode { 3236 PUT_CAP_REFS_SYNC = 0, 3237 PUT_CAP_REFS_ASYNC, 3238 }; 3239 3240 /* 3241 * Release cap refs. 3242 * 3243 * If we released the last ref on any given cap, call ceph_check_caps 3244 * to release (or schedule a release). 3245 * 3246 * If we are releasing a WR cap (from a sync write), finalize any affected 3247 * cap_snap, and wake up any waiters. 3248 */ 3249 static void __ceph_put_cap_refs(struct ceph_inode_info *ci, int had, 3250 enum put_cap_refs_mode mode) 3251 { 3252 struct inode *inode = &ci->netfs.inode; 3253 struct ceph_client *cl = ceph_inode_to_client(inode); 3254 int last = 0, put = 0, flushsnaps = 0, wake = 0; 3255 bool check_flushsnaps = false; 3256 3257 spin_lock(&ci->i_ceph_lock); 3258 if (had & CEPH_CAP_PIN) 3259 --ci->i_pin_ref; 3260 if (had & CEPH_CAP_FILE_RD) 3261 if (--ci->i_rd_ref == 0) 3262 last++; 3263 if (had & CEPH_CAP_FILE_CACHE) 3264 if (--ci->i_rdcache_ref == 0) 3265 last++; 3266 if (had & CEPH_CAP_FILE_EXCL) 3267 if (--ci->i_fx_ref == 0) 3268 last++; 3269 if (had & CEPH_CAP_FILE_BUFFER) { 3270 if (--ci->i_wb_ref == 0) { 3271 last++; 3272 /* put the ref held by ceph_take_cap_refs() */ 3273 put++; 3274 check_flushsnaps = true; 3275 } 3276 doutc(cl, "%p %llx.%llx wb %d -> %d (?)\n", inode, 3277 ceph_vinop(inode), ci->i_wb_ref+1, ci->i_wb_ref); 3278 } 3279 if (had & CEPH_CAP_FILE_WR) { 3280 if (--ci->i_wr_ref == 0) { 3281 /* 3282 * The Fb caps will always be took and released 3283 * together with the Fw caps. 3284 */ 3285 WARN_ON_ONCE(ci->i_wb_ref); 3286 3287 last++; 3288 check_flushsnaps = true; 3289 if (ci->i_wrbuffer_ref_head == 0 && 3290 ci->i_dirty_caps == 0 && 3291 ci->i_flushing_caps == 0) { 3292 BUG_ON(!ci->i_head_snapc); 3293 ceph_put_snap_context(ci->i_head_snapc); 3294 ci->i_head_snapc = NULL; 3295 } 3296 /* see comment in __ceph_remove_cap() */ 3297 if (!__ceph_is_any_real_caps(ci) && ci->i_snap_realm) 3298 ceph_change_snap_realm(inode, NULL); 3299 } 3300 } 3301 if (check_flushsnaps && __ceph_have_pending_cap_snap(ci)) { 3302 struct ceph_cap_snap *capsnap = 3303 list_last_entry(&ci->i_cap_snaps, 3304 struct ceph_cap_snap, 3305 ci_item); 3306 3307 capsnap->writing = 0; 3308 if (ceph_try_drop_cap_snap(ci, capsnap)) 3309 /* put the ref held by ceph_queue_cap_snap() */ 3310 put++; 3311 else if (__ceph_finish_cap_snap(ci, capsnap)) 3312 flushsnaps = 1; 3313 wake = 1; 3314 } 3315 spin_unlock(&ci->i_ceph_lock); 3316 3317 doutc(cl, "%p %llx.%llx had %s%s%s\n", inode, ceph_vinop(inode), 3318 ceph_cap_string(had), last ? " last" : "", put ? " put" : ""); 3319 3320 switch (mode) { 3321 case PUT_CAP_REFS_SYNC: 3322 if (last) 3323 ceph_check_caps(ci, 0); 3324 else if (flushsnaps) 3325 ceph_flush_snaps(ci, NULL); 3326 break; 3327 case PUT_CAP_REFS_ASYNC: 3328 if (last) 3329 ceph_queue_check_caps(inode); 3330 else if (flushsnaps) 3331 ceph_queue_flush_snaps(inode); 3332 break; 3333 default: 3334 break; 3335 } 3336 if (wake) 3337 wake_up_all(&ci->i_cap_wq); 3338 while (put-- > 0) 3339 iput(inode); 3340 } 3341 3342 void ceph_put_cap_refs(struct ceph_inode_info *ci, int had) 3343 { 3344 __ceph_put_cap_refs(ci, had, PUT_CAP_REFS_SYNC); 3345 } 3346 3347 void ceph_put_cap_refs_async(struct ceph_inode_info *ci, int had) 3348 { 3349 __ceph_put_cap_refs(ci, had, PUT_CAP_REFS_ASYNC); 3350 } 3351 3352 /* 3353 * Release @nr WRBUFFER refs on dirty pages for the given @snapc snap 3354 * context. Adjust per-snap dirty page accounting as appropriate. 3355 * Once all dirty data for a cap_snap is flushed, flush snapped file 3356 * metadata back to the MDS. If we dropped the last ref, call 3357 * ceph_check_caps. 3358 */ 3359 void ceph_put_wrbuffer_cap_refs(struct ceph_inode_info *ci, int nr, 3360 struct ceph_snap_context *snapc) 3361 { 3362 struct inode *inode = &ci->netfs.inode; 3363 struct ceph_client *cl = ceph_inode_to_client(inode); 3364 struct ceph_cap_snap *capsnap = NULL, *iter; 3365 int put = 0; 3366 bool last = false; 3367 bool flush_snaps = false; 3368 bool complete_capsnap = false; 3369 3370 spin_lock(&ci->i_ceph_lock); 3371 ci->i_wrbuffer_ref -= nr; 3372 if (ci->i_wrbuffer_ref == 0) { 3373 last = true; 3374 put++; 3375 } 3376 3377 if (ci->i_head_snapc == snapc) { 3378 ci->i_wrbuffer_ref_head -= nr; 3379 if (ci->i_wrbuffer_ref_head == 0 && 3380 ci->i_wr_ref == 0 && 3381 ci->i_dirty_caps == 0 && 3382 ci->i_flushing_caps == 0) { 3383 BUG_ON(!ci->i_head_snapc); 3384 ceph_put_snap_context(ci->i_head_snapc); 3385 ci->i_head_snapc = NULL; 3386 } 3387 doutc(cl, "on %p %llx.%llx head %d/%d -> %d/%d %s\n", 3388 inode, ceph_vinop(inode), ci->i_wrbuffer_ref+nr, 3389 ci->i_wrbuffer_ref_head+nr, ci->i_wrbuffer_ref, 3390 ci->i_wrbuffer_ref_head, last ? " LAST" : ""); 3391 } else { 3392 list_for_each_entry(iter, &ci->i_cap_snaps, ci_item) { 3393 if (iter->context == snapc) { 3394 capsnap = iter; 3395 break; 3396 } 3397 } 3398 3399 if (!capsnap) { 3400 /* 3401 * The capsnap should already be removed when removing 3402 * auth cap in the case of a forced unmount. 3403 */ 3404 WARN_ON_ONCE(ci->i_auth_cap); 3405 goto unlock; 3406 } 3407 3408 capsnap->dirty_pages -= nr; 3409 if (capsnap->dirty_pages == 0) { 3410 complete_capsnap = true; 3411 if (!capsnap->writing) { 3412 if (ceph_try_drop_cap_snap(ci, capsnap)) { 3413 put++; 3414 } else { 3415 set_bit(CEPH_I_FLUSH_SNAPS_BIT, &ci->i_ceph_flags); 3416 flush_snaps = true; 3417 } 3418 } 3419 } 3420 doutc(cl, "%p %llx.%llx cap_snap %p snap %lld %d/%d -> %d/%d %s%s\n", 3421 inode, ceph_vinop(inode), capsnap, capsnap->context->seq, 3422 ci->i_wrbuffer_ref+nr, capsnap->dirty_pages + nr, 3423 ci->i_wrbuffer_ref, capsnap->dirty_pages, 3424 last ? " (wrbuffer last)" : "", 3425 complete_capsnap ? " (complete capsnap)" : ""); 3426 } 3427 3428 unlock: 3429 spin_unlock(&ci->i_ceph_lock); 3430 3431 if (last) { 3432 ceph_check_caps(ci, 0); 3433 } else if (flush_snaps) { 3434 ceph_flush_snaps(ci, NULL); 3435 } 3436 if (complete_capsnap) 3437 wake_up_all(&ci->i_cap_wq); 3438 while (put-- > 0) { 3439 iput(inode); 3440 } 3441 } 3442 3443 /* 3444 * Invalidate unlinked inode's aliases, so we can drop the inode ASAP. 3445 */ 3446 static void invalidate_aliases(struct inode *inode) 3447 { 3448 struct ceph_client *cl = ceph_inode_to_client(inode); 3449 struct dentry *dn, *prev = NULL; 3450 3451 doutc(cl, "%p %llx.%llx\n", inode, ceph_vinop(inode)); 3452 d_prune_aliases(inode); 3453 /* 3454 * For non-directory inode, d_find_alias() only returns 3455 * hashed dentry. After calling d_invalidate(), the 3456 * dentry becomes unhashed. 3457 * 3458 * For directory inode, d_find_alias() can return 3459 * unhashed dentry. But directory inode should have 3460 * one alias at most. 3461 */ 3462 while ((dn = d_find_alias(inode))) { 3463 if (dn == prev) { 3464 dput(dn); 3465 break; 3466 } 3467 d_invalidate(dn); 3468 if (prev) 3469 dput(prev); 3470 prev = dn; 3471 } 3472 if (prev) 3473 dput(prev); 3474 } 3475 3476 struct cap_extra_info { 3477 struct ceph_string *pool_ns; 3478 /* inline data */ 3479 u64 inline_version; 3480 void *inline_data; 3481 u32 inline_len; 3482 /* dirstat */ 3483 bool dirstat_valid; 3484 u64 nfiles; 3485 u64 nsubdirs; 3486 u64 change_attr; 3487 /* currently issued */ 3488 int issued; 3489 struct timespec64 btime; 3490 u8 *fscrypt_auth; 3491 u32 fscrypt_auth_len; 3492 u64 fscrypt_file_size; 3493 }; 3494 3495 /* 3496 * Handle a cap GRANT message from the MDS. (Note that a GRANT may 3497 * actually be a revocation if it specifies a smaller cap set.) 3498 * 3499 * caller holds s_mutex and i_ceph_lock, we drop both. 3500 */ 3501 static void handle_cap_grant(struct inode *inode, 3502 struct ceph_mds_session *session, 3503 struct ceph_cap *cap, 3504 struct ceph_mds_caps *grant, 3505 struct ceph_buffer *xattr_buf, 3506 struct cap_extra_info *extra_info) 3507 __releases(ci->i_ceph_lock) 3508 __releases(session->s_mdsc->snap_rwsem) 3509 { 3510 struct ceph_client *cl = ceph_inode_to_client(inode); 3511 struct ceph_inode_info *ci = ceph_inode(inode); 3512 int seq = le32_to_cpu(grant->seq); 3513 int newcaps = le32_to_cpu(grant->caps); 3514 int used, wanted, dirty; 3515 u64 size = le64_to_cpu(grant->size); 3516 u64 max_size = le64_to_cpu(grant->max_size); 3517 unsigned char check_caps = 0; 3518 bool was_stale = cap->cap_gen < atomic_read(&session->s_cap_gen); 3519 bool wake = false; 3520 bool writeback = false; 3521 bool queue_trunc = false; 3522 bool queue_invalidate = false; 3523 bool deleted_inode = false; 3524 bool fill_inline = false; 3525 bool revoke_wait = false; 3526 int flags = 0; 3527 3528 /* 3529 * If there is at least one crypto block then we'll trust 3530 * fscrypt_file_size. If the real length of the file is 0, then 3531 * ignore it (it has probably been truncated down to 0 by the MDS). 3532 */ 3533 if (IS_ENCRYPTED(inode) && size) 3534 size = extra_info->fscrypt_file_size; 3535 3536 doutc(cl, "%p %llx.%llx cap %p mds%d seq %d %s\n", inode, 3537 ceph_vinop(inode), cap, session->s_mds, seq, 3538 ceph_cap_string(newcaps)); 3539 doutc(cl, " size %llu max_size %llu, i_size %llu\n", size, 3540 max_size, i_size_read(inode)); 3541 3542 3543 /* 3544 * If CACHE is being revoked, and we have no dirty buffers, 3545 * try to invalidate (once). (If there are dirty buffers, we 3546 * will invalidate _after_ writeback.) 3547 */ 3548 if (S_ISREG(inode->i_mode) && /* don't invalidate readdir cache */ 3549 ((cap->issued & ~newcaps) & CEPH_CAP_FILE_CACHE) && 3550 (newcaps & CEPH_CAP_FILE_LAZYIO) == 0 && 3551 !(ci->i_wrbuffer_ref || ci->i_wb_ref)) { 3552 if (try_nonblocking_invalidate(inode)) { 3553 /* there were locked pages.. invalidate later 3554 in a separate thread. */ 3555 if (ci->i_rdcache_revoking != ci->i_rdcache_gen) { 3556 queue_invalidate = true; 3557 ci->i_rdcache_revoking = ci->i_rdcache_gen; 3558 } 3559 } 3560 } 3561 3562 if (was_stale) 3563 cap->issued = cap->implemented = CEPH_CAP_PIN; 3564 3565 /* 3566 * auth mds of the inode changed. we received the cap export message, 3567 * but still haven't received the cap import message. handle_cap_export 3568 * updated the new auth MDS' cap. 3569 * 3570 * "ceph_seq_cmp(seq, cap->seq) <= 0" means we are processing a message 3571 * that was sent before the cap import message. So don't remove caps. 3572 */ 3573 if (ceph_seq_cmp(seq, cap->seq) <= 0) { 3574 WARN_ON(cap != ci->i_auth_cap); 3575 WARN_ON(cap->cap_id != le64_to_cpu(grant->cap_id)); 3576 seq = cap->seq; 3577 newcaps |= cap->issued; 3578 } 3579 3580 /* side effects now are allowed */ 3581 cap->cap_gen = atomic_read(&session->s_cap_gen); 3582 cap->seq = seq; 3583 3584 __check_cap_issue(ci, cap, newcaps); 3585 3586 inode_set_max_iversion_raw(inode, extra_info->change_attr); 3587 3588 if ((newcaps & CEPH_CAP_AUTH_SHARED) && 3589 (extra_info->issued & CEPH_CAP_AUTH_EXCL) == 0) { 3590 umode_t mode = le32_to_cpu(grant->mode); 3591 3592 if (inode_wrong_type(inode, mode)) 3593 pr_warn_once("inode type changed! (ino %llx.%llx is 0%o, mds says 0%o)\n", 3594 ceph_vinop(inode), inode->i_mode, mode); 3595 else 3596 inode->i_mode = mode; 3597 inode->i_uid = make_kuid(&init_user_ns, le32_to_cpu(grant->uid)); 3598 inode->i_gid = make_kgid(&init_user_ns, le32_to_cpu(grant->gid)); 3599 ci->i_btime = extra_info->btime; 3600 doutc(cl, "%p %llx.%llx mode 0%o uid.gid %d.%d\n", inode, 3601 ceph_vinop(inode), inode->i_mode, 3602 from_kuid(&init_user_ns, inode->i_uid), 3603 from_kgid(&init_user_ns, inode->i_gid)); 3604 #if IS_ENABLED(CONFIG_FS_ENCRYPTION) 3605 if (ci->fscrypt_auth_len != extra_info->fscrypt_auth_len || 3606 memcmp(ci->fscrypt_auth, extra_info->fscrypt_auth, 3607 ci->fscrypt_auth_len)) 3608 pr_warn_ratelimited_client(cl, 3609 "cap grant attempt to change fscrypt_auth on non-I_NEW inode (old len %d new len %d)\n", 3610 ci->fscrypt_auth_len, 3611 extra_info->fscrypt_auth_len); 3612 #endif 3613 } 3614 3615 if ((newcaps & CEPH_CAP_LINK_SHARED) && 3616 (extra_info->issued & CEPH_CAP_LINK_EXCL) == 0) { 3617 set_nlink(inode, le32_to_cpu(grant->nlink)); 3618 if (inode->i_nlink == 0) 3619 deleted_inode = true; 3620 } 3621 3622 if ((extra_info->issued & CEPH_CAP_XATTR_EXCL) == 0 && 3623 grant->xattr_len) { 3624 int len = le32_to_cpu(grant->xattr_len); 3625 u64 version = le64_to_cpu(grant->xattr_version); 3626 3627 if (version > ci->i_xattrs.version) { 3628 doutc(cl, " got new xattrs v%llu on %p %llx.%llx len %d\n", 3629 version, inode, ceph_vinop(inode), len); 3630 if (ci->i_xattrs.blob) 3631 ceph_buffer_put(ci->i_xattrs.blob); 3632 ci->i_xattrs.blob = ceph_buffer_get(xattr_buf); 3633 ci->i_xattrs.version = version; 3634 ceph_forget_all_cached_acls(inode); 3635 ceph_security_invalidate_secctx(inode); 3636 } 3637 } 3638 3639 if (newcaps & CEPH_CAP_ANY_RD) { 3640 struct timespec64 mtime, atime, ctime; 3641 /* ctime/mtime/atime? */ 3642 ceph_decode_timespec64(&mtime, &grant->mtime); 3643 ceph_decode_timespec64(&atime, &grant->atime); 3644 ceph_decode_timespec64(&ctime, &grant->ctime); 3645 ceph_fill_file_time(inode, extra_info->issued, 3646 le32_to_cpu(grant->time_warp_seq), 3647 &ctime, &mtime, &atime); 3648 } 3649 3650 if ((newcaps & CEPH_CAP_FILE_SHARED) && extra_info->dirstat_valid) { 3651 ci->i_files = extra_info->nfiles; 3652 ci->i_subdirs = extra_info->nsubdirs; 3653 } 3654 3655 if (newcaps & (CEPH_CAP_ANY_FILE_RD | CEPH_CAP_ANY_FILE_WR)) { 3656 /* file layout may have changed */ 3657 s64 old_pool = ci->i_layout.pool_id; 3658 struct ceph_string *old_ns; 3659 3660 ceph_file_layout_from_legacy(&ci->i_layout, &grant->layout); 3661 old_ns = rcu_dereference_protected(ci->i_layout.pool_ns, 3662 lockdep_is_held(&ci->i_ceph_lock)); 3663 rcu_assign_pointer(ci->i_layout.pool_ns, extra_info->pool_ns); 3664 3665 if (ci->i_layout.pool_id != old_pool || 3666 extra_info->pool_ns != old_ns) 3667 clear_bit(CEPH_I_POOL_PERM_BIT, &ci->i_ceph_flags); 3668 3669 extra_info->pool_ns = old_ns; 3670 3671 /* size/truncate_seq? */ 3672 queue_trunc = ceph_fill_file_size(inode, extra_info->issued, 3673 le32_to_cpu(grant->truncate_seq), 3674 le64_to_cpu(grant->truncate_size), 3675 size); 3676 } 3677 3678 if (ci->i_auth_cap == cap && (newcaps & CEPH_CAP_ANY_FILE_WR)) { 3679 if (max_size != ci->i_max_size) { 3680 doutc(cl, "max_size %lld -> %llu\n", ci->i_max_size, 3681 max_size); 3682 ci->i_max_size = max_size; 3683 if (max_size >= ci->i_wanted_max_size) { 3684 ci->i_wanted_max_size = 0; /* reset */ 3685 ci->i_requested_max_size = 0; 3686 } 3687 wake = true; 3688 } 3689 } 3690 3691 /* check cap bits */ 3692 wanted = __ceph_caps_wanted(ci); 3693 used = __ceph_caps_used(ci); 3694 dirty = __ceph_caps_dirty(ci); 3695 doutc(cl, " my wanted = %s, used = %s, dirty %s\n", 3696 ceph_cap_string(wanted), ceph_cap_string(used), 3697 ceph_cap_string(dirty)); 3698 3699 if ((was_stale || le32_to_cpu(grant->op) == CEPH_CAP_OP_IMPORT) && 3700 (wanted & ~(cap->mds_wanted | newcaps))) { 3701 /* 3702 * If mds is importing cap, prior cap messages that update 3703 * 'wanted' may get dropped by mds (migrate seq mismatch). 3704 * 3705 * We don't send cap message to update 'wanted' if what we 3706 * want are already issued. If mds revokes caps, cap message 3707 * that releases caps also tells mds what we want. But if 3708 * caps got revoked by mds forcedly (session stale). We may 3709 * haven't told mds what we want. 3710 */ 3711 check_caps = 1; 3712 } 3713 3714 /* revocation, grant, or no-op? */ 3715 if (cap->issued & ~newcaps) { 3716 int revoking = cap->issued & ~newcaps; 3717 3718 doutc(cl, "revocation: %s -> %s (revoking %s)\n", 3719 ceph_cap_string(cap->issued), ceph_cap_string(newcaps), 3720 ceph_cap_string(revoking)); 3721 if (S_ISREG(inode->i_mode) && 3722 (revoking & used & CEPH_CAP_FILE_BUFFER)) { 3723 writeback = true; /* initiate writeback; will delay ack */ 3724 revoke_wait = true; 3725 } else if (queue_invalidate && 3726 revoking == CEPH_CAP_FILE_CACHE && 3727 (newcaps & CEPH_CAP_FILE_LAZYIO) == 0) { 3728 revoke_wait = true; /* do nothing yet, invalidation will be queued */ 3729 } else if (cap == ci->i_auth_cap) { 3730 check_caps = 1; /* check auth cap only */ 3731 } else { 3732 check_caps = 2; /* check all caps */ 3733 } 3734 /* If there is new caps, try to wake up the waiters */ 3735 if (~cap->issued & newcaps) 3736 wake = true; 3737 cap->issued = newcaps; 3738 cap->implemented |= newcaps; 3739 } else if (cap->issued == newcaps) { 3740 doutc(cl, "caps unchanged: %s -> %s\n", 3741 ceph_cap_string(cap->issued), 3742 ceph_cap_string(newcaps)); 3743 } else { 3744 doutc(cl, "grant: %s -> %s\n", ceph_cap_string(cap->issued), 3745 ceph_cap_string(newcaps)); 3746 /* non-auth MDS is revoking the newly grant caps ? */ 3747 if (cap == ci->i_auth_cap && 3748 __ceph_caps_revoking_other(ci, cap, newcaps)) 3749 check_caps = 2; 3750 3751 cap->issued = newcaps; 3752 cap->implemented |= newcaps; /* add bits only, to 3753 * avoid stepping on a 3754 * pending revocation */ 3755 wake = true; 3756 } 3757 BUG_ON(cap->issued & ~cap->implemented); 3758 3759 /* don't let check_caps skip sending a response to MDS for revoke msgs */ 3760 if (!revoke_wait && le32_to_cpu(grant->op) == CEPH_CAP_OP_REVOKE) { 3761 cap->mds_wanted = 0; 3762 flags |= CHECK_CAPS_FLUSH_FORCE; 3763 if (cap == ci->i_auth_cap) 3764 check_caps = 1; /* check auth cap only */ 3765 else 3766 check_caps = 2; /* check all caps */ 3767 } 3768 3769 if (extra_info->inline_version > 0 && 3770 extra_info->inline_version >= ci->i_inline_version) { 3771 ci->i_inline_version = extra_info->inline_version; 3772 if (ci->i_inline_version != CEPH_INLINE_NONE && 3773 (newcaps & (CEPH_CAP_FILE_CACHE|CEPH_CAP_FILE_LAZYIO))) 3774 fill_inline = true; 3775 } 3776 3777 if (le32_to_cpu(grant->op) == CEPH_CAP_OP_IMPORT) { 3778 if (ci->i_auth_cap == cap) { 3779 if (newcaps & ~extra_info->issued) 3780 wake = true; 3781 3782 if (ci->i_requested_max_size > max_size || 3783 !(le32_to_cpu(grant->wanted) & CEPH_CAP_ANY_FILE_WR)) { 3784 /* re-request max_size if necessary */ 3785 ci->i_requested_max_size = 0; 3786 wake = true; 3787 } 3788 3789 ceph_kick_flushing_inode_caps(session, ci); 3790 } 3791 up_read(&session->s_mdsc->snap_rwsem); 3792 } 3793 spin_unlock(&ci->i_ceph_lock); 3794 3795 if (fill_inline) 3796 ceph_fill_inline_data(inode, NULL, extra_info->inline_data, 3797 extra_info->inline_len); 3798 3799 if (queue_trunc) 3800 ceph_queue_vmtruncate(inode); 3801 3802 if (writeback) 3803 /* 3804 * queue inode for writeback: we can't actually call 3805 * filemap_write_and_wait, etc. from message handler 3806 * context. 3807 */ 3808 ceph_queue_writeback(inode); 3809 if (queue_invalidate) 3810 ceph_queue_invalidate(inode); 3811 if (deleted_inode) 3812 invalidate_aliases(inode); 3813 if (wake) 3814 wake_up_all(&ci->i_cap_wq); 3815 3816 mutex_unlock(&session->s_mutex); 3817 if (check_caps == 1) 3818 ceph_check_caps(ci, flags | CHECK_CAPS_AUTHONLY | CHECK_CAPS_NOINVAL); 3819 else if (check_caps == 2) 3820 ceph_check_caps(ci, flags | CHECK_CAPS_NOINVAL); 3821 } 3822 3823 /* 3824 * Handle FLUSH_ACK from MDS, indicating that metadata we sent to the 3825 * MDS has been safely committed. 3826 */ 3827 static void handle_cap_flush_ack(struct inode *inode, u64 flush_tid, 3828 struct ceph_mds_caps *m, 3829 struct ceph_mds_session *session, 3830 struct ceph_cap *cap) 3831 __releases(ci->i_ceph_lock) 3832 { 3833 struct ceph_inode_info *ci = ceph_inode(inode); 3834 struct ceph_mds_client *mdsc = ceph_sb_to_fs_client(inode->i_sb)->mdsc; 3835 struct ceph_client *cl = mdsc->fsc->client; 3836 struct ceph_cap_flush *cf, *tmp_cf; 3837 LIST_HEAD(to_remove); 3838 unsigned seq = le32_to_cpu(m->seq); 3839 int dirty = le32_to_cpu(m->dirty); 3840 int cleaned = 0; 3841 bool drop = false; 3842 bool wake_ci = false; 3843 bool wake_mdsc = false; 3844 3845 /* 3846 * Flush tids are monotonically increasing and acks arrive in 3847 * order under i_ceph_lock, so this is always the latest tid. 3848 * Diagnostic readers use READ_ONCE() without holding the lock. 3849 */ 3850 WRITE_ONCE(ci->i_last_cap_flush_ack, flush_tid); 3851 3852 list_for_each_entry_safe(cf, tmp_cf, &ci->i_cap_flush_list, i_list) { 3853 /* Is this the one that was flushed? */ 3854 if (cf->tid == flush_tid) 3855 cleaned = cf->caps; 3856 3857 /* Is this a capsnap? */ 3858 if (cf->is_capsnap) 3859 continue; 3860 3861 if (cf->tid <= flush_tid) { 3862 /* 3863 * An earlier or current tid. The FLUSH_ACK should 3864 * represent a superset of this flush's caps. 3865 */ 3866 wake_ci |= __detach_cap_flush_from_ci(ci, cf); 3867 list_add_tail(&cf->i_list, &to_remove); 3868 } else { 3869 /* 3870 * This is a later one. Any caps in it are still dirty 3871 * so don't count them as cleaned. 3872 */ 3873 cleaned &= ~cf->caps; 3874 if (!cleaned) 3875 break; 3876 } 3877 } 3878 3879 doutc(cl, "%p %llx.%llx mds%d seq %d on %s cleaned %s, flushing %s -> %s\n", 3880 inode, ceph_vinop(inode), session->s_mds, seq, 3881 ceph_cap_string(dirty), ceph_cap_string(cleaned), 3882 ceph_cap_string(ci->i_flushing_caps), 3883 ceph_cap_string(ci->i_flushing_caps & ~cleaned)); 3884 3885 if (list_empty(&to_remove) && !cleaned) 3886 goto out; 3887 3888 ci->i_flushing_caps &= ~cleaned; 3889 3890 spin_lock(&mdsc->cap_dirty_lock); 3891 3892 list_for_each_entry(cf, &to_remove, i_list) 3893 wake_mdsc |= __detach_cap_flush_from_mdsc(mdsc, cf); 3894 3895 if (ci->i_flushing_caps == 0) { 3896 if (list_empty(&ci->i_cap_flush_list)) { 3897 list_del_init(&ci->i_flushing_item); 3898 if (!list_empty(&session->s_cap_flushing)) { 3899 struct inode *inode = 3900 &list_first_entry(&session->s_cap_flushing, 3901 struct ceph_inode_info, 3902 i_flushing_item)->netfs.inode; 3903 doutc(cl, " mds%d still flushing cap on %p %llx.%llx\n", 3904 session->s_mds, inode, ceph_vinop(inode)); 3905 } 3906 } 3907 mdsc->num_cap_flushing--; 3908 doutc(cl, " %p %llx.%llx now !flushing\n", inode, 3909 ceph_vinop(inode)); 3910 3911 if (ci->i_dirty_caps == 0) { 3912 doutc(cl, " %p %llx.%llx now clean\n", inode, 3913 ceph_vinop(inode)); 3914 BUG_ON(!list_empty(&ci->i_dirty_item)); 3915 drop = true; 3916 if (ci->i_wr_ref == 0 && 3917 ci->i_wrbuffer_ref_head == 0) { 3918 BUG_ON(!ci->i_head_snapc); 3919 ceph_put_snap_context(ci->i_head_snapc); 3920 ci->i_head_snapc = NULL; 3921 } 3922 } else { 3923 BUG_ON(list_empty(&ci->i_dirty_item)); 3924 } 3925 } 3926 spin_unlock(&mdsc->cap_dirty_lock); 3927 3928 out: 3929 spin_unlock(&ci->i_ceph_lock); 3930 3931 while (!list_empty(&to_remove)) { 3932 cf = list_first_entry(&to_remove, 3933 struct ceph_cap_flush, i_list); 3934 list_del_init(&cf->i_list); 3935 if (!cf->is_capsnap) 3936 ceph_free_cap_flush(cf); 3937 } 3938 3939 if (wake_ci) 3940 wake_up_all(&ci->i_cap_wq); 3941 if (wake_mdsc) 3942 wake_up_all(&mdsc->cap_flushing_wq); 3943 if (drop) 3944 iput(inode); 3945 } 3946 3947 void __ceph_remove_capsnap(struct inode *inode, struct ceph_cap_snap *capsnap, 3948 bool *wake_ci, bool *wake_mdsc) 3949 { 3950 struct ceph_inode_info *ci = ceph_inode(inode); 3951 struct ceph_mds_client *mdsc = ceph_sb_to_fs_client(inode->i_sb)->mdsc; 3952 struct ceph_client *cl = mdsc->fsc->client; 3953 bool ret; 3954 3955 lockdep_assert_held(&ci->i_ceph_lock); 3956 3957 doutc(cl, "removing capsnap %p, %p %llx.%llx ci %p\n", capsnap, 3958 inode, ceph_vinop(inode), ci); 3959 3960 list_del_init(&capsnap->ci_item); 3961 ret = __detach_cap_flush_from_ci(ci, &capsnap->cap_flush); 3962 if (wake_ci) 3963 *wake_ci = ret; 3964 3965 spin_lock(&mdsc->cap_dirty_lock); 3966 if (list_empty(&ci->i_cap_flush_list)) 3967 list_del_init(&ci->i_flushing_item); 3968 3969 ret = __detach_cap_flush_from_mdsc(mdsc, &capsnap->cap_flush); 3970 if (wake_mdsc) 3971 *wake_mdsc = ret; 3972 spin_unlock(&mdsc->cap_dirty_lock); 3973 } 3974 3975 void ceph_remove_capsnap(struct inode *inode, struct ceph_cap_snap *capsnap, 3976 bool *wake_ci, bool *wake_mdsc) 3977 { 3978 struct ceph_inode_info *ci = ceph_inode(inode); 3979 3980 lockdep_assert_held(&ci->i_ceph_lock); 3981 3982 WARN_ON_ONCE(capsnap->dirty_pages || capsnap->writing); 3983 __ceph_remove_capsnap(inode, capsnap, wake_ci, wake_mdsc); 3984 } 3985 3986 /* 3987 * Handle FLUSHSNAP_ACK. MDS has flushed snap data to disk and we can 3988 * throw away our cap_snap. 3989 * 3990 * Caller hold s_mutex. 3991 */ 3992 static void handle_cap_flushsnap_ack(struct inode *inode, u64 flush_tid, 3993 struct ceph_mds_caps *m, 3994 struct ceph_mds_session *session) 3995 { 3996 struct ceph_inode_info *ci = ceph_inode(inode); 3997 struct ceph_mds_client *mdsc = ceph_sb_to_fs_client(inode->i_sb)->mdsc; 3998 struct ceph_client *cl = mdsc->fsc->client; 3999 u64 follows = le64_to_cpu(m->snap_follows); 4000 struct ceph_cap_snap *capsnap = NULL, *iter; 4001 bool wake_ci = false; 4002 bool wake_mdsc = false; 4003 4004 doutc(cl, "%p %llx.%llx ci %p mds%d follows %lld\n", inode, 4005 ceph_vinop(inode), ci, session->s_mds, follows); 4006 4007 spin_lock(&ci->i_ceph_lock); 4008 list_for_each_entry(iter, &ci->i_cap_snaps, ci_item) { 4009 if (iter->follows == follows) { 4010 if (iter->cap_flush.tid != flush_tid) { 4011 doutc(cl, " cap_snap %p follows %lld " 4012 "tid %lld != %lld\n", iter, 4013 follows, flush_tid, 4014 iter->cap_flush.tid); 4015 break; 4016 } 4017 capsnap = iter; 4018 break; 4019 } else { 4020 doutc(cl, " skipping cap_snap %p follows %lld\n", 4021 iter, iter->follows); 4022 } 4023 } 4024 if (capsnap) 4025 ceph_remove_capsnap(inode, capsnap, &wake_ci, &wake_mdsc); 4026 spin_unlock(&ci->i_ceph_lock); 4027 4028 if (capsnap) { 4029 ceph_put_snap_context(capsnap->context); 4030 ceph_put_cap_snap(capsnap); 4031 if (wake_ci) 4032 wake_up_all(&ci->i_cap_wq); 4033 if (wake_mdsc) 4034 wake_up_all(&mdsc->cap_flushing_wq); 4035 iput(inode); 4036 } 4037 } 4038 4039 /* 4040 * Handle TRUNC from MDS, indicating file truncation. 4041 * 4042 * caller hold s_mutex. 4043 */ 4044 static bool handle_cap_trunc(struct inode *inode, 4045 struct ceph_mds_caps *trunc, 4046 struct ceph_mds_session *session, 4047 struct cap_extra_info *extra_info) 4048 { 4049 struct ceph_inode_info *ci = ceph_inode(inode); 4050 struct ceph_client *cl = ceph_inode_to_client(inode); 4051 int mds = session->s_mds; 4052 int seq = le32_to_cpu(trunc->seq); 4053 u32 truncate_seq = le32_to_cpu(trunc->truncate_seq); 4054 u64 truncate_size = le64_to_cpu(trunc->truncate_size); 4055 u64 size = le64_to_cpu(trunc->size); 4056 int implemented = 0; 4057 int dirty = __ceph_caps_dirty(ci); 4058 int issued = __ceph_caps_issued(ceph_inode(inode), &implemented); 4059 bool queue_trunc = false; 4060 4061 lockdep_assert_held(&ci->i_ceph_lock); 4062 4063 issued |= implemented | dirty; 4064 4065 /* 4066 * If there is at least one crypto block then we'll trust 4067 * fscrypt_file_size. If the real length of the file is 0, then 4068 * ignore it (it has probably been truncated down to 0 by the MDS). 4069 */ 4070 if (IS_ENCRYPTED(inode) && size) 4071 size = extra_info->fscrypt_file_size; 4072 4073 doutc(cl, "%p %llx.%llx mds%d seq %d to %lld truncate seq %d\n", 4074 inode, ceph_vinop(inode), mds, seq, truncate_size, truncate_seq); 4075 queue_trunc = ceph_fill_file_size(inode, issued, 4076 truncate_seq, truncate_size, size); 4077 return queue_trunc; 4078 } 4079 4080 /* 4081 * Handle EXPORT from MDS. Cap is being migrated _from_ this mds to a 4082 * different one. If we are the most recent migration we've seen (as 4083 * indicated by mseq), make note of the migrating cap bits for the 4084 * duration (until we see the corresponding IMPORT). 4085 * 4086 * caller holds s_mutex 4087 */ 4088 static void handle_cap_export(struct inode *inode, struct ceph_mds_caps *ex, 4089 struct ceph_mds_cap_peer *ph, 4090 struct ceph_mds_session *session) 4091 { 4092 struct ceph_mds_client *mdsc = ceph_inode_to_fs_client(inode)->mdsc; 4093 struct ceph_client *cl = mdsc->fsc->client; 4094 struct ceph_mds_session *tsession = NULL; 4095 struct ceph_cap *cap, *tcap, *new_cap = NULL; 4096 struct ceph_inode_info *ci = ceph_inode(inode); 4097 u64 t_cap_id; 4098 u32 t_issue_seq, t_mseq; 4099 int target, issued; 4100 int mds = session->s_mds; 4101 4102 if (ph) { 4103 t_cap_id = le64_to_cpu(ph->cap_id); 4104 t_issue_seq = le32_to_cpu(ph->issue_seq); 4105 t_mseq = le32_to_cpu(ph->mseq); 4106 target = le32_to_cpu(ph->mds); 4107 } else { 4108 t_cap_id = t_issue_seq = t_mseq = 0; 4109 target = -1; 4110 } 4111 4112 doutc(cl, " cap %llx.%llx export to peer %d piseq %u pmseq %u\n", 4113 ceph_vinop(inode), target, t_issue_seq, t_mseq); 4114 retry: 4115 down_read(&mdsc->snap_rwsem); 4116 spin_lock(&ci->i_ceph_lock); 4117 cap = __get_cap_for_mds(ci, mds); 4118 if (!cap || cap->cap_id != le64_to_cpu(ex->cap_id)) 4119 goto out_unlock; 4120 4121 if (target < 0) { 4122 ceph_remove_cap(mdsc, cap, false); 4123 goto out_unlock; 4124 } 4125 4126 /* 4127 * now we know we haven't received the cap import message yet 4128 * because the exported cap still exist. 4129 */ 4130 4131 issued = cap->issued; 4132 if (issued != cap->implemented) 4133 pr_err_ratelimited_client(cl, "issued != implemented: " 4134 "%p %llx.%llx mds%d seq %d mseq %d" 4135 " issued %s implemented %s\n", 4136 inode, ceph_vinop(inode), mds, 4137 cap->seq, cap->mseq, 4138 ceph_cap_string(issued), 4139 ceph_cap_string(cap->implemented)); 4140 4141 4142 tcap = __get_cap_for_mds(ci, target); 4143 if (tcap) { 4144 /* already have caps from the target */ 4145 if (tcap->cap_id == t_cap_id && 4146 ceph_seq_cmp(tcap->seq, t_issue_seq) < 0) { 4147 doutc(cl, " updating import cap %p mds%d\n", tcap, 4148 target); 4149 tcap->cap_id = t_cap_id; 4150 tcap->seq = t_issue_seq - 1; 4151 tcap->issue_seq = t_issue_seq - 1; 4152 tcap->issued |= issued; 4153 tcap->implemented |= issued; 4154 if (cap == ci->i_auth_cap) { 4155 ci->i_auth_cap = tcap; 4156 change_auth_cap_ses(ci, tcap->session); 4157 } 4158 } 4159 ceph_remove_cap(mdsc, cap, false); 4160 goto out_unlock; 4161 } else if (tsession) { 4162 /* add placeholder for the export target */ 4163 int flag = (cap == ci->i_auth_cap) ? CEPH_CAP_FLAG_AUTH : 0; 4164 tcap = new_cap; 4165 ceph_add_cap(inode, tsession, t_cap_id, issued, 0, 4166 t_issue_seq - 1, t_mseq, (u64)-1, flag, &new_cap); 4167 4168 if (!list_empty(&ci->i_cap_flush_list) && 4169 ci->i_auth_cap == tcap) { 4170 spin_lock(&mdsc->cap_dirty_lock); 4171 list_move_tail(&ci->i_flushing_item, 4172 &tcap->session->s_cap_flushing); 4173 spin_unlock(&mdsc->cap_dirty_lock); 4174 } 4175 4176 ceph_remove_cap(mdsc, cap, false); 4177 goto out_unlock; 4178 } 4179 4180 spin_unlock(&ci->i_ceph_lock); 4181 up_read(&mdsc->snap_rwsem); 4182 mutex_unlock(&session->s_mutex); 4183 4184 /* open target session */ 4185 tsession = ceph_mdsc_open_export_target_session(mdsc, target); 4186 if (!IS_ERR(tsession)) { 4187 if (mds > target) { 4188 mutex_lock(&session->s_mutex); 4189 mutex_lock_nested(&tsession->s_mutex, 4190 SINGLE_DEPTH_NESTING); 4191 } else { 4192 mutex_lock(&tsession->s_mutex); 4193 mutex_lock_nested(&session->s_mutex, 4194 SINGLE_DEPTH_NESTING); 4195 } 4196 new_cap = ceph_get_cap(mdsc, NULL); 4197 } else { 4198 WARN_ON(1); 4199 tsession = NULL; 4200 target = -1; 4201 mutex_lock(&session->s_mutex); 4202 } 4203 goto retry; 4204 4205 out_unlock: 4206 spin_unlock(&ci->i_ceph_lock); 4207 up_read(&mdsc->snap_rwsem); 4208 mutex_unlock(&session->s_mutex); 4209 if (tsession) { 4210 mutex_unlock(&tsession->s_mutex); 4211 ceph_put_mds_session(tsession); 4212 } 4213 if (new_cap) 4214 ceph_put_cap(mdsc, new_cap); 4215 } 4216 4217 /* 4218 * Handle cap IMPORT. 4219 * 4220 * caller holds s_mutex. acquires i_ceph_lock 4221 */ 4222 static void handle_cap_import(struct ceph_mds_client *mdsc, 4223 struct inode *inode, struct ceph_mds_caps *im, 4224 struct ceph_mds_cap_peer *ph, 4225 struct ceph_mds_session *session, 4226 struct ceph_cap **target_cap, int *old_issued) 4227 { 4228 struct ceph_inode_info *ci = ceph_inode(inode); 4229 struct ceph_client *cl = mdsc->fsc->client; 4230 struct ceph_cap *cap, *ocap, *new_cap = NULL; 4231 int mds = session->s_mds; 4232 int issued; 4233 unsigned caps = le32_to_cpu(im->caps); 4234 unsigned wanted = le32_to_cpu(im->wanted); 4235 unsigned seq = le32_to_cpu(im->seq); 4236 unsigned mseq = le32_to_cpu(im->migrate_seq); 4237 u64 realmino = le64_to_cpu(im->realm); 4238 u64 cap_id = le64_to_cpu(im->cap_id); 4239 u64 p_cap_id; 4240 u32 piseq = 0; 4241 u32 pmseq = 0; 4242 int peer; 4243 4244 if (ph) { 4245 p_cap_id = le64_to_cpu(ph->cap_id); 4246 peer = le32_to_cpu(ph->mds); 4247 piseq = le32_to_cpu(ph->issue_seq); 4248 pmseq = le32_to_cpu(ph->mseq); 4249 } else { 4250 p_cap_id = 0; 4251 peer = -1; 4252 } 4253 4254 doutc(cl, " cap %llx.%llx import from peer %d piseq %u pmseq %u\n", 4255 ceph_vinop(inode), peer, piseq, pmseq); 4256 retry: 4257 cap = __get_cap_for_mds(ci, mds); 4258 if (!cap) { 4259 if (!new_cap) { 4260 spin_unlock(&ci->i_ceph_lock); 4261 new_cap = ceph_get_cap(mdsc, NULL); 4262 spin_lock(&ci->i_ceph_lock); 4263 goto retry; 4264 } 4265 cap = new_cap; 4266 } else { 4267 if (new_cap) { 4268 ceph_put_cap(mdsc, new_cap); 4269 new_cap = NULL; 4270 } 4271 } 4272 4273 __ceph_caps_issued(ci, &issued); 4274 issued |= __ceph_caps_dirty(ci); 4275 4276 ceph_add_cap(inode, session, cap_id, caps, wanted, seq, mseq, 4277 realmino, CEPH_CAP_FLAG_AUTH, &new_cap); 4278 4279 ocap = peer >= 0 ? __get_cap_for_mds(ci, peer) : NULL; 4280 if (ocap && ocap->cap_id == p_cap_id) { 4281 doutc(cl, " remove export cap %p mds%d flags %d\n", 4282 ocap, peer, ph->flags); 4283 if ((ph->flags & CEPH_CAP_FLAG_AUTH) && 4284 (ocap->seq != piseq || 4285 ocap->mseq != pmseq)) { 4286 pr_err_ratelimited_client(cl, "mismatched seq/mseq: " 4287 "%p %llx.%llx mds%d seq %d mseq %d" 4288 " importer mds%d has peer seq %d mseq %d\n", 4289 inode, ceph_vinop(inode), peer, 4290 ocap->seq, ocap->mseq, mds, piseq, pmseq); 4291 } 4292 ceph_remove_cap(mdsc, ocap, (ph->flags & CEPH_CAP_FLAG_RELEASE)); 4293 } 4294 4295 *old_issued = issued; 4296 *target_cap = cap; 4297 } 4298 4299 #ifdef CONFIG_FS_ENCRYPTION 4300 static int parse_fscrypt_fields(void **p, void *end, 4301 struct cap_extra_info *extra) 4302 { 4303 u32 len; 4304 4305 ceph_decode_32_safe(p, end, extra->fscrypt_auth_len, bad); 4306 if (extra->fscrypt_auth_len) { 4307 ceph_decode_need(p, end, extra->fscrypt_auth_len, bad); 4308 extra->fscrypt_auth = kmalloc(extra->fscrypt_auth_len, 4309 GFP_KERNEL); 4310 if (!extra->fscrypt_auth) 4311 return -ENOMEM; 4312 ceph_decode_copy_safe(p, end, extra->fscrypt_auth, 4313 extra->fscrypt_auth_len, bad); 4314 } 4315 4316 ceph_decode_32_safe(p, end, len, bad); 4317 if (len >= sizeof(u64)) { 4318 ceph_decode_64_safe(p, end, extra->fscrypt_file_size, bad); 4319 len -= sizeof(u64); 4320 } 4321 ceph_decode_skip_n(p, end, len, bad); 4322 return 0; 4323 bad: 4324 return -EIO; 4325 } 4326 #else 4327 static int parse_fscrypt_fields(void **p, void *end, 4328 struct cap_extra_info *extra) 4329 { 4330 u32 len; 4331 4332 /* Don't care about these fields unless we're encryption-capable */ 4333 ceph_decode_32_safe(p, end, len, bad); 4334 if (len) 4335 ceph_decode_skip_n(p, end, len, bad); 4336 ceph_decode_32_safe(p, end, len, bad); 4337 if (len) 4338 ceph_decode_skip_n(p, end, len, bad); 4339 return 0; 4340 bad: 4341 return -EIO; 4342 } 4343 #endif 4344 4345 /* 4346 * Handle a caps message from the MDS. 4347 * 4348 * Identify the appropriate session, inode, and call the right handler 4349 * based on the cap op. 4350 */ 4351 void ceph_handle_caps(struct ceph_mds_session *session, 4352 struct ceph_msg *msg) 4353 { 4354 struct ceph_mds_client *mdsc = session->s_mdsc; 4355 struct ceph_client *cl = mdsc->fsc->client; 4356 struct inode *inode; 4357 struct ceph_inode_info *ci; 4358 struct ceph_cap *cap; 4359 struct ceph_mds_caps *h; 4360 struct ceph_mds_cap_peer *peer = NULL; 4361 struct ceph_snap_realm *realm = NULL; 4362 int op; 4363 int msg_version = le16_to_cpu(msg->hdr.version); 4364 u32 seq, mseq, issue_seq; 4365 struct ceph_vino vino; 4366 void *snaptrace; 4367 size_t snaptrace_len; 4368 void *p, *end; 4369 struct cap_extra_info extra_info = {}; 4370 bool queue_trunc; 4371 bool close_sessions = false; 4372 bool do_cap_release = false; 4373 4374 if (!ceph_inc_mds_stopping_blocker(mdsc, session)) 4375 return; 4376 4377 /* decode */ 4378 end = msg->front.iov_base + msg->front.iov_len; 4379 if (msg->front.iov_len < sizeof(*h)) 4380 goto bad; 4381 h = msg->front.iov_base; 4382 op = le32_to_cpu(h->op); 4383 vino.ino = le64_to_cpu(h->ino); 4384 vino.snap = CEPH_NOSNAP; 4385 seq = le32_to_cpu(h->seq); 4386 mseq = le32_to_cpu(h->migrate_seq); 4387 issue_seq = le32_to_cpu(h->issue_seq); 4388 4389 snaptrace = h + 1; 4390 snaptrace_len = le32_to_cpu(h->snap_trace_len); 4391 ceph_decode_need(&snaptrace, end, snaptrace_len, bad); 4392 p = snaptrace + snaptrace_len; 4393 4394 if (msg_version >= 2) { 4395 u32 flock_len; 4396 ceph_decode_32_safe(&p, end, flock_len, bad); 4397 if (p + flock_len > end) 4398 goto bad; 4399 p += flock_len; 4400 } 4401 4402 if (msg_version >= 3) { 4403 if (op == CEPH_CAP_OP_IMPORT) { 4404 if (p + sizeof(*peer) > end) 4405 goto bad; 4406 peer = p; 4407 p += sizeof(*peer); 4408 } else if (op == CEPH_CAP_OP_EXPORT) { 4409 /* recorded in unused fields */ 4410 peer = (void *)&h->size; 4411 } 4412 } 4413 4414 if (msg_version >= 4) { 4415 ceph_decode_64_safe(&p, end, extra_info.inline_version, bad); 4416 ceph_decode_32_safe(&p, end, extra_info.inline_len, bad); 4417 if (p + extra_info.inline_len > end) 4418 goto bad; 4419 extra_info.inline_data = p; 4420 p += extra_info.inline_len; 4421 } 4422 4423 if (msg_version >= 5) { 4424 struct ceph_osd_client *osdc = &mdsc->fsc->client->osdc; 4425 u32 epoch_barrier; 4426 4427 ceph_decode_32_safe(&p, end, epoch_barrier, bad); 4428 ceph_osdc_update_epoch_barrier(osdc, epoch_barrier); 4429 } 4430 4431 if (msg_version >= 8) { 4432 u32 pool_ns_len; 4433 4434 /* version >= 6 */ 4435 ceph_decode_skip_64(&p, end, bad); // flush_tid 4436 /* version >= 7 */ 4437 ceph_decode_skip_32(&p, end, bad); // caller_uid 4438 ceph_decode_skip_32(&p, end, bad); // caller_gid 4439 /* version >= 8 */ 4440 ceph_decode_32_safe(&p, end, pool_ns_len, bad); 4441 if (pool_ns_len > 0) { 4442 ceph_decode_need(&p, end, pool_ns_len, bad); 4443 extra_info.pool_ns = 4444 ceph_find_or_create_string(p, pool_ns_len); 4445 p += pool_ns_len; 4446 } 4447 } 4448 4449 if (msg_version >= 9) { 4450 struct ceph_timespec *btime; 4451 4452 if (p + sizeof(*btime) > end) 4453 goto bad; 4454 btime = p; 4455 ceph_decode_timespec64(&extra_info.btime, btime); 4456 p += sizeof(*btime); 4457 ceph_decode_64_safe(&p, end, extra_info.change_attr, bad); 4458 } 4459 4460 if (msg_version >= 11) { 4461 /* version >= 10 */ 4462 ceph_decode_skip_32(&p, end, bad); // flags 4463 /* version >= 11 */ 4464 extra_info.dirstat_valid = true; 4465 ceph_decode_64_safe(&p, end, extra_info.nfiles, bad); 4466 ceph_decode_64_safe(&p, end, extra_info.nsubdirs, bad); 4467 } 4468 4469 if (msg_version >= 12) { 4470 if (parse_fscrypt_fields(&p, end, &extra_info)) 4471 goto bad; 4472 } 4473 4474 /* lookup ino */ 4475 inode = ceph_find_inode(mdsc->fsc->sb, vino); 4476 doutc(cl, " caps mds%d op %s ino %llx.%llx inode %p seq %u iseq %u mseq %u\n", 4477 session->s_mds, ceph_cap_op_name(op), vino.ino, vino.snap, inode, 4478 seq, issue_seq, mseq); 4479 4480 trace_ceph_handle_caps(mdsc, session, op, &vino, ceph_inode(inode), 4481 seq, issue_seq, mseq); 4482 4483 mutex_lock(&session->s_mutex); 4484 4485 if (!inode) { 4486 doutc(cl, " i don't have ino %llx\n", vino.ino); 4487 4488 switch (op) { 4489 case CEPH_CAP_OP_IMPORT: 4490 case CEPH_CAP_OP_REVOKE: 4491 case CEPH_CAP_OP_GRANT: 4492 do_cap_release = true; 4493 break; 4494 default: 4495 break; 4496 } 4497 goto flush_cap_releases; 4498 } 4499 ci = ceph_inode(inode); 4500 4501 /* these will work even if we don't have a cap yet */ 4502 switch (op) { 4503 case CEPH_CAP_OP_FLUSHSNAP_ACK: 4504 handle_cap_flushsnap_ack(inode, le64_to_cpu(msg->hdr.tid), 4505 h, session); 4506 goto done; 4507 4508 case CEPH_CAP_OP_EXPORT: 4509 handle_cap_export(inode, h, peer, session); 4510 goto done_unlocked; 4511 4512 case CEPH_CAP_OP_IMPORT: 4513 realm = NULL; 4514 if (snaptrace_len) { 4515 down_write(&mdsc->snap_rwsem); 4516 if (ceph_update_snap_trace(mdsc, snaptrace, 4517 snaptrace + snaptrace_len, 4518 false, &realm)) { 4519 up_write(&mdsc->snap_rwsem); 4520 close_sessions = true; 4521 goto done; 4522 } 4523 downgrade_write(&mdsc->snap_rwsem); 4524 } else { 4525 down_read(&mdsc->snap_rwsem); 4526 } 4527 spin_lock(&ci->i_ceph_lock); 4528 handle_cap_import(mdsc, inode, h, peer, session, 4529 &cap, &extra_info.issued); 4530 handle_cap_grant(inode, session, cap, 4531 h, msg->middle, &extra_info); 4532 if (realm) 4533 ceph_put_snap_realm(mdsc, realm); 4534 goto done_unlocked; 4535 } 4536 4537 /* the rest require a cap */ 4538 spin_lock(&ci->i_ceph_lock); 4539 cap = __get_cap_for_mds(ceph_inode(inode), session->s_mds); 4540 if (!cap) { 4541 doutc(cl, " no cap on %p ino %llx.%llx from mds%d\n", 4542 inode, ceph_ino(inode), ceph_snap(inode), 4543 session->s_mds); 4544 spin_unlock(&ci->i_ceph_lock); 4545 switch (op) { 4546 case CEPH_CAP_OP_REVOKE: 4547 case CEPH_CAP_OP_GRANT: 4548 do_cap_release = true; 4549 break; 4550 default: 4551 break; 4552 } 4553 goto flush_cap_releases; 4554 } 4555 4556 /* note that each of these drops i_ceph_lock for us */ 4557 switch (op) { 4558 case CEPH_CAP_OP_REVOKE: 4559 case CEPH_CAP_OP_GRANT: 4560 __ceph_caps_issued(ci, &extra_info.issued); 4561 extra_info.issued |= __ceph_caps_dirty(ci); 4562 handle_cap_grant(inode, session, cap, 4563 h, msg->middle, &extra_info); 4564 goto done_unlocked; 4565 4566 case CEPH_CAP_OP_FLUSH_ACK: 4567 handle_cap_flush_ack(inode, le64_to_cpu(msg->hdr.tid), 4568 h, session, cap); 4569 break; 4570 4571 case CEPH_CAP_OP_TRUNC: 4572 queue_trunc = handle_cap_trunc(inode, h, session, 4573 &extra_info); 4574 spin_unlock(&ci->i_ceph_lock); 4575 if (queue_trunc) 4576 ceph_queue_vmtruncate(inode); 4577 break; 4578 4579 default: 4580 spin_unlock(&ci->i_ceph_lock); 4581 pr_err_client(cl, "unknown cap op %d %s\n", op, 4582 ceph_cap_op_name(op)); 4583 } 4584 4585 done: 4586 mutex_unlock(&session->s_mutex); 4587 done_unlocked: 4588 iput(inode); 4589 out: 4590 ceph_dec_mds_stopping_blocker(mdsc); 4591 4592 ceph_put_string(extra_info.pool_ns); 4593 4594 /* Defer closing the sessions after s_mutex lock being released */ 4595 if (close_sessions) 4596 ceph_mdsc_close_sessions(mdsc); 4597 4598 kfree(extra_info.fscrypt_auth); 4599 return; 4600 4601 flush_cap_releases: 4602 /* 4603 * send any cap release message to try to move things 4604 * along for the mds (who clearly thinks we still have this 4605 * cap). 4606 */ 4607 if (do_cap_release) { 4608 cap = ceph_get_cap(mdsc, NULL); 4609 cap->cap_ino = vino.ino; 4610 cap->queue_release = 1; 4611 cap->cap_id = le64_to_cpu(h->cap_id); 4612 cap->mseq = mseq; 4613 cap->seq = seq; 4614 cap->issue_seq = seq; 4615 spin_lock(&session->s_cap_lock); 4616 __ceph_queue_cap_release(session, cap); 4617 spin_unlock(&session->s_cap_lock); 4618 } 4619 ceph_flush_session_cap_releases(mdsc, session); 4620 goto done; 4621 4622 bad: 4623 pr_err_client(cl, "corrupt message\n"); 4624 ceph_msg_dump(msg); 4625 goto out; 4626 } 4627 4628 /* 4629 * Delayed work handler to process end of delayed cap release LRU list. 4630 * 4631 * If new caps are added to the list while processing it, these won't get 4632 * processed in this run. In this case, the ci->i_hold_caps_max will be 4633 * returned so that the work can be scheduled accordingly. 4634 */ 4635 unsigned long ceph_check_delayed_caps(struct ceph_mds_client *mdsc) 4636 { 4637 struct ceph_client *cl = mdsc->fsc->client; 4638 struct inode *inode; 4639 struct ceph_inode_info *ci; 4640 struct ceph_mount_options *opt = mdsc->fsc->mount_options; 4641 unsigned long delay_max = opt->caps_wanted_delay_max * HZ; 4642 unsigned long loop_start = jiffies; 4643 unsigned long delay = 0; 4644 4645 doutc(cl, "begin\n"); 4646 spin_lock(&mdsc->cap_delay_lock); 4647 while (!list_empty(&mdsc->cap_delay_list)) { 4648 ci = list_first_entry(&mdsc->cap_delay_list, 4649 struct ceph_inode_info, 4650 i_cap_delay_list); 4651 if (time_before(loop_start, ci->i_hold_caps_max - delay_max)) { 4652 doutc(cl, "caps added recently. Exiting loop"); 4653 delay = ci->i_hold_caps_max; 4654 break; 4655 } 4656 if ((ci->i_ceph_flags & CEPH_I_FLUSH) == 0 && 4657 time_before(jiffies, ci->i_hold_caps_max)) 4658 break; 4659 list_del_init(&ci->i_cap_delay_list); 4660 4661 inode = igrab(&ci->netfs.inode); 4662 if (inode) { 4663 spin_unlock(&mdsc->cap_delay_lock); 4664 doutc(cl, "on %p %llx.%llx\n", inode, 4665 ceph_vinop(inode)); 4666 ceph_check_caps(ci, 0); 4667 iput(inode); 4668 spin_lock(&mdsc->cap_delay_lock); 4669 } 4670 4671 /* 4672 * Make sure too many dirty caps or general 4673 * slowness doesn't block mdsc delayed work, 4674 * preventing send_renew_caps() from running. 4675 */ 4676 if (time_after_eq(jiffies, loop_start + 5 * HZ)) 4677 break; 4678 } 4679 spin_unlock(&mdsc->cap_delay_lock); 4680 doutc(cl, "done\n"); 4681 4682 return delay; 4683 } 4684 4685 /* 4686 * Flush all dirty caps to the mds 4687 */ 4688 static void flush_dirty_session_caps(struct ceph_mds_session *s) 4689 { 4690 struct ceph_mds_client *mdsc = s->s_mdsc; 4691 struct ceph_client *cl = mdsc->fsc->client; 4692 struct ceph_inode_info *ci; 4693 struct inode *inode; 4694 4695 doutc(cl, "begin\n"); 4696 spin_lock(&mdsc->cap_dirty_lock); 4697 while (!list_empty(&s->s_cap_dirty)) { 4698 ci = list_first_entry(&s->s_cap_dirty, struct ceph_inode_info, 4699 i_dirty_item); 4700 inode = &ci->netfs.inode; 4701 ihold(inode); 4702 doutc(cl, "%p %llx.%llx\n", inode, ceph_vinop(inode)); 4703 spin_unlock(&mdsc->cap_dirty_lock); 4704 ceph_wait_on_async_create(inode); 4705 ceph_check_caps(ci, CHECK_CAPS_FLUSH); 4706 iput(inode); 4707 spin_lock(&mdsc->cap_dirty_lock); 4708 } 4709 spin_unlock(&mdsc->cap_dirty_lock); 4710 doutc(cl, "done\n"); 4711 } 4712 4713 void ceph_flush_dirty_caps(struct ceph_mds_client *mdsc) 4714 { 4715 ceph_mdsc_iterate_sessions(mdsc, flush_dirty_session_caps, true); 4716 } 4717 4718 /* 4719 * Flush all cap releases to the mds 4720 */ 4721 static void flush_cap_releases(struct ceph_mds_session *s) 4722 { 4723 struct ceph_mds_client *mdsc = s->s_mdsc; 4724 struct ceph_client *cl = mdsc->fsc->client; 4725 4726 doutc(cl, "begin\n"); 4727 spin_lock(&s->s_cap_lock); 4728 if (s->s_num_cap_releases) 4729 ceph_flush_session_cap_releases(mdsc, s); 4730 spin_unlock(&s->s_cap_lock); 4731 doutc(cl, "done\n"); 4732 4733 } 4734 4735 void ceph_flush_cap_releases(struct ceph_mds_client *mdsc) 4736 { 4737 ceph_mdsc_iterate_sessions(mdsc, flush_cap_releases, true); 4738 } 4739 4740 void __ceph_touch_fmode(struct ceph_inode_info *ci, 4741 struct ceph_mds_client *mdsc, int fmode) 4742 { 4743 unsigned long now = jiffies; 4744 if (fmode & CEPH_FILE_MODE_RD) 4745 ci->i_last_rd = now; 4746 if (fmode & CEPH_FILE_MODE_WR) 4747 ci->i_last_wr = now; 4748 /* queue periodic check */ 4749 if (fmode && 4750 __ceph_is_any_real_caps(ci) && 4751 list_empty(&ci->i_cap_delay_list)) 4752 __cap_delay_requeue(mdsc, ci); 4753 } 4754 4755 void ceph_get_fmode(struct ceph_inode_info *ci, int fmode, int count) 4756 { 4757 struct ceph_mds_client *mdsc = ceph_sb_to_mdsc(ci->netfs.inode.i_sb); 4758 int bits = (fmode << 1) | 1; 4759 bool already_opened = false; 4760 int i; 4761 4762 if (count == 1) 4763 atomic64_inc(&mdsc->metric.opened_files); 4764 4765 spin_lock(&ci->i_ceph_lock); 4766 for (i = 0; i < CEPH_FILE_MODE_BITS; i++) { 4767 /* 4768 * If any of the mode ref is larger than 0, 4769 * that means it has been already opened by 4770 * others. Just skip checking the PIN ref. 4771 */ 4772 if (i && ci->i_nr_by_mode[i]) 4773 already_opened = true; 4774 4775 if (bits & (1 << i)) 4776 ci->i_nr_by_mode[i] += count; 4777 } 4778 4779 if (!already_opened) 4780 percpu_counter_inc(&mdsc->metric.opened_inodes); 4781 spin_unlock(&ci->i_ceph_lock); 4782 } 4783 4784 /* 4785 * Drop open file reference. If we were the last open file, 4786 * we may need to release capabilities to the MDS (or schedule 4787 * their delayed release). 4788 */ 4789 void ceph_put_fmode(struct ceph_inode_info *ci, int fmode, int count) 4790 { 4791 struct ceph_mds_client *mdsc = ceph_sb_to_mdsc(ci->netfs.inode.i_sb); 4792 int bits = (fmode << 1) | 1; 4793 bool is_closed = true; 4794 int i; 4795 4796 if (count == 1) 4797 atomic64_dec(&mdsc->metric.opened_files); 4798 4799 spin_lock(&ci->i_ceph_lock); 4800 for (i = 0; i < CEPH_FILE_MODE_BITS; i++) { 4801 if (bits & (1 << i)) { 4802 BUG_ON(ci->i_nr_by_mode[i] < count); 4803 ci->i_nr_by_mode[i] -= count; 4804 } 4805 4806 /* 4807 * If any of the mode ref is not 0 after 4808 * decreased, that means it is still opened 4809 * by others. Just skip checking the PIN ref. 4810 */ 4811 if (i && ci->i_nr_by_mode[i]) 4812 is_closed = false; 4813 } 4814 4815 if (is_closed) 4816 percpu_counter_dec(&mdsc->metric.opened_inodes); 4817 spin_unlock(&ci->i_ceph_lock); 4818 } 4819 4820 /* 4821 * For a soon-to-be unlinked file, drop the LINK caps. If it 4822 * looks like the link count will hit 0, drop any other caps (other 4823 * than PIN) we don't specifically want (due to the file still being 4824 * open). 4825 */ 4826 int ceph_drop_caps_for_unlink(struct inode *inode) 4827 { 4828 struct ceph_inode_info *ci = ceph_inode(inode); 4829 int drop = CEPH_CAP_LINK_SHARED | CEPH_CAP_LINK_EXCL; 4830 4831 spin_lock(&ci->i_ceph_lock); 4832 if (inode->i_nlink == 1) { 4833 drop |= ~(__ceph_caps_wanted(ci) | CEPH_CAP_PIN); 4834 4835 if (__ceph_caps_dirty(ci)) { 4836 struct ceph_mds_client *mdsc = 4837 ceph_inode_to_fs_client(inode)->mdsc; 4838 4839 doutc(mdsc->fsc->client, "%p %llx.%llx\n", inode, 4840 ceph_vinop(inode)); 4841 spin_lock(&mdsc->cap_delay_lock); 4842 set_bit(CEPH_I_FLUSH_BIT, &ci->i_ceph_flags); 4843 if (!list_empty(&ci->i_cap_delay_list)) 4844 list_del_init(&ci->i_cap_delay_list); 4845 list_add_tail(&ci->i_cap_delay_list, 4846 &mdsc->cap_unlink_delay_list); 4847 spin_unlock(&mdsc->cap_delay_lock); 4848 4849 /* 4850 * Fire the work immediately, because the MDS maybe 4851 * waiting for caps release. 4852 */ 4853 ceph_queue_cap_unlink_work(mdsc); 4854 } 4855 } 4856 spin_unlock(&ci->i_ceph_lock); 4857 return drop; 4858 } 4859 4860 /* 4861 * Helpers for embedding cap and dentry lease releases into mds 4862 * requests. 4863 * 4864 * @force is used by dentry_release (below) to force inclusion of a 4865 * record for the directory inode, even when there aren't any caps to 4866 * drop. 4867 */ 4868 int ceph_encode_inode_release(void **p, struct inode *inode, 4869 int mds, int drop, int unless, int force) 4870 { 4871 struct ceph_inode_info *ci = ceph_inode(inode); 4872 struct ceph_client *cl = ceph_inode_to_client(inode); 4873 struct ceph_cap *cap; 4874 struct ceph_mds_request_release *rel = *p; 4875 int used, dirty; 4876 int ret = 0; 4877 4878 spin_lock(&ci->i_ceph_lock); 4879 used = __ceph_caps_used(ci); 4880 dirty = __ceph_caps_dirty(ci); 4881 4882 doutc(cl, "%p %llx.%llx mds%d used|dirty %s drop %s unless %s\n", 4883 inode, ceph_vinop(inode), mds, ceph_cap_string(used|dirty), 4884 ceph_cap_string(drop), ceph_cap_string(unless)); 4885 4886 /* only drop unused, clean caps */ 4887 drop &= ~(used | dirty); 4888 4889 cap = __get_cap_for_mds(ci, mds); 4890 if (cap && __cap_is_valid(cap)) { 4891 unless &= cap->issued; 4892 if (unless) { 4893 if (unless & CEPH_CAP_AUTH_EXCL) 4894 drop &= ~CEPH_CAP_AUTH_SHARED; 4895 if (unless & CEPH_CAP_LINK_EXCL) 4896 drop &= ~CEPH_CAP_LINK_SHARED; 4897 if (unless & CEPH_CAP_XATTR_EXCL) 4898 drop &= ~CEPH_CAP_XATTR_SHARED; 4899 if (unless & CEPH_CAP_FILE_EXCL) 4900 drop &= ~CEPH_CAP_FILE_SHARED; 4901 } 4902 4903 if (force || (cap->issued & drop)) { 4904 if (cap->issued & drop) { 4905 int wanted = __ceph_caps_wanted(ci); 4906 doutc(cl, "%p %llx.%llx cap %p %s -> %s, " 4907 "wanted %s -> %s\n", inode, 4908 ceph_vinop(inode), cap, 4909 ceph_cap_string(cap->issued), 4910 ceph_cap_string(cap->issued & ~drop), 4911 ceph_cap_string(cap->mds_wanted), 4912 ceph_cap_string(wanted)); 4913 4914 cap->issued &= ~drop; 4915 cap->implemented &= ~drop; 4916 cap->mds_wanted = wanted; 4917 if (cap == ci->i_auth_cap && 4918 !(wanted & CEPH_CAP_ANY_FILE_WR)) 4919 ci->i_requested_max_size = 0; 4920 } else { 4921 doutc(cl, "%p %llx.%llx cap %p %s (force)\n", 4922 inode, ceph_vinop(inode), cap, 4923 ceph_cap_string(cap->issued)); 4924 } 4925 4926 rel->ino = cpu_to_le64(ceph_ino(inode)); 4927 rel->cap_id = cpu_to_le64(cap->cap_id); 4928 rel->seq = cpu_to_le32(cap->seq); 4929 rel->issue_seq = cpu_to_le32(cap->issue_seq); 4930 rel->mseq = cpu_to_le32(cap->mseq); 4931 rel->caps = cpu_to_le32(cap->implemented); 4932 rel->wanted = cpu_to_le32(cap->mds_wanted); 4933 rel->dname_len = 0; 4934 rel->dname_seq = 0; 4935 *p += sizeof(*rel); 4936 ret = 1; 4937 } else { 4938 doutc(cl, "%p %llx.%llx cap %p %s (noop)\n", 4939 inode, ceph_vinop(inode), cap, 4940 ceph_cap_string(cap->issued)); 4941 } 4942 } 4943 spin_unlock(&ci->i_ceph_lock); 4944 return ret; 4945 } 4946 4947 /** 4948 * ceph_encode_dentry_release - encode a dentry release into an outgoing request 4949 * @p: outgoing request buffer 4950 * @dentry: dentry to release 4951 * @dir: dir to release it from 4952 * @mds: mds that we're speaking to 4953 * @drop: caps being dropped 4954 * @unless: unless we have these caps 4955 * 4956 * Encode a dentry release into an outgoing request buffer. Returns 1 if the 4957 * thing was released, or a negative error code otherwise. 4958 */ 4959 int ceph_encode_dentry_release(void **p, struct dentry *dentry, 4960 struct inode *dir, 4961 int mds, int drop, int unless) 4962 { 4963 struct ceph_mds_request_release *rel = *p; 4964 struct ceph_dentry_info *di = ceph_dentry(dentry); 4965 struct ceph_client *cl; 4966 int force = 0; 4967 int ret; 4968 4969 /* This shouldn't happen */ 4970 BUG_ON(!dir); 4971 4972 /* 4973 * force an record for the directory caps if we have a dentry lease. 4974 * this is racy (can't take i_ceph_lock and d_lock together), but it 4975 * doesn't have to be perfect; the mds will revoke anything we don't 4976 * release. 4977 */ 4978 spin_lock(&dentry->d_lock); 4979 if (di->lease_session && di->lease_session->s_mds == mds) 4980 force = 1; 4981 spin_unlock(&dentry->d_lock); 4982 4983 ret = ceph_encode_inode_release(p, dir, mds, drop, unless, force); 4984 4985 cl = ceph_inode_to_client(dir); 4986 spin_lock(&dentry->d_lock); 4987 if (ret && di->lease_session && di->lease_session->s_mds == mds) { 4988 int len = dentry->d_name.len; 4989 doutc(cl, "%p mds%d seq %d\n", dentry, mds, 4990 (int)di->lease_seq); 4991 rel->dname_seq = cpu_to_le32(di->lease_seq); 4992 __ceph_mdsc_drop_dentry_lease(dentry); 4993 memcpy(*p, dentry->d_name.name, len); 4994 spin_unlock(&dentry->d_lock); 4995 if (IS_ENCRYPTED(dir) && fscrypt_has_encryption_key(dir)) { 4996 len = ceph_encode_encrypted_dname(dir, *p, len); 4997 if (len < 0) 4998 return len; 4999 } 5000 rel->dname_len = cpu_to_le32(len); 5001 *p += len; 5002 } else { 5003 spin_unlock(&dentry->d_lock); 5004 } 5005 return ret; 5006 } 5007 5008 static int remove_capsnaps(struct ceph_mds_client *mdsc, struct inode *inode) 5009 { 5010 struct ceph_inode_info *ci = ceph_inode(inode); 5011 struct ceph_client *cl = mdsc->fsc->client; 5012 struct ceph_cap_snap *capsnap; 5013 int capsnap_release = 0; 5014 5015 lockdep_assert_held(&ci->i_ceph_lock); 5016 5017 doutc(cl, "removing capsnaps, ci is %p, %p %llx.%llx\n", 5018 ci, inode, ceph_vinop(inode)); 5019 5020 while (!list_empty(&ci->i_cap_snaps)) { 5021 capsnap = list_first_entry(&ci->i_cap_snaps, 5022 struct ceph_cap_snap, ci_item); 5023 __ceph_remove_capsnap(inode, capsnap, NULL, NULL); 5024 ceph_put_snap_context(capsnap->context); 5025 ceph_put_cap_snap(capsnap); 5026 capsnap_release++; 5027 } 5028 wake_up_all(&ci->i_cap_wq); 5029 wake_up_all(&mdsc->cap_flushing_wq); 5030 return capsnap_release; 5031 } 5032 5033 int ceph_purge_inode_cap(struct inode *inode, struct ceph_cap *cap, bool *invalidate) 5034 { 5035 struct ceph_fs_client *fsc = ceph_inode_to_fs_client(inode); 5036 struct ceph_mds_client *mdsc = fsc->mdsc; 5037 struct ceph_client *cl = fsc->client; 5038 struct ceph_inode_info *ci = ceph_inode(inode); 5039 bool is_auth; 5040 bool dirty_dropped = false; 5041 int iputs = 0; 5042 5043 lockdep_assert_held(&ci->i_ceph_lock); 5044 5045 doutc(cl, "removing cap %p, ci is %p, %p %llx.%llx\n", 5046 cap, ci, inode, ceph_vinop(inode)); 5047 5048 is_auth = (cap == ci->i_auth_cap); 5049 __ceph_remove_cap(cap, false); 5050 if (is_auth) { 5051 struct ceph_cap_flush *cf; 5052 5053 if (ceph_inode_is_shutdown(inode)) { 5054 if (inode->i_data.nrpages > 0) 5055 *invalidate = true; 5056 if (ci->i_wrbuffer_ref > 0) 5057 mapping_set_error(&inode->i_data, -EIO); 5058 } 5059 5060 spin_lock(&mdsc->cap_dirty_lock); 5061 5062 /* trash all of the cap flushes for this inode */ 5063 while (!list_empty(&ci->i_cap_flush_list)) { 5064 cf = list_first_entry(&ci->i_cap_flush_list, 5065 struct ceph_cap_flush, i_list); 5066 list_del_init(&cf->g_list); 5067 list_del_init(&cf->i_list); 5068 if (!cf->is_capsnap) 5069 ceph_free_cap_flush(cf); 5070 } 5071 5072 if (!list_empty(&ci->i_dirty_item)) { 5073 pr_warn_ratelimited_client(cl, 5074 " dropping dirty %s state for %p %llx.%llx\n", 5075 ceph_cap_string(ci->i_dirty_caps), 5076 inode, ceph_vinop(inode)); 5077 ci->i_dirty_caps = 0; 5078 list_del_init(&ci->i_dirty_item); 5079 dirty_dropped = true; 5080 } 5081 if (!list_empty(&ci->i_flushing_item)) { 5082 pr_warn_ratelimited_client(cl, 5083 " dropping dirty+flushing %s state for %p %llx.%llx\n", 5084 ceph_cap_string(ci->i_flushing_caps), 5085 inode, ceph_vinop(inode)); 5086 ci->i_flushing_caps = 0; 5087 list_del_init(&ci->i_flushing_item); 5088 mdsc->num_cap_flushing--; 5089 dirty_dropped = true; 5090 } 5091 spin_unlock(&mdsc->cap_dirty_lock); 5092 5093 if (dirty_dropped) { 5094 mapping_set_error(inode->i_mapping, -EIO); 5095 5096 if (ci->i_wrbuffer_ref_head == 0 && 5097 ci->i_wr_ref == 0 && 5098 ci->i_dirty_caps == 0 && 5099 ci->i_flushing_caps == 0) { 5100 ceph_put_snap_context(ci->i_head_snapc); 5101 ci->i_head_snapc = NULL; 5102 } 5103 } 5104 5105 if (atomic_read(&ci->i_filelock_ref) > 0) { 5106 /* make further file lock syscall return -EIO */ 5107 set_bit(CEPH_I_ERROR_FILELOCK_BIT, &ci->i_ceph_flags); 5108 pr_warn_ratelimited_client(cl, 5109 " dropping file locks for %p %llx.%llx\n", 5110 inode, ceph_vinop(inode)); 5111 } 5112 5113 if (!ci->i_dirty_caps && ci->i_prealloc_cap_flush) { 5114 cf = ci->i_prealloc_cap_flush; 5115 ci->i_prealloc_cap_flush = NULL; 5116 if (!cf->is_capsnap) 5117 ceph_free_cap_flush(cf); 5118 } 5119 5120 if (!list_empty(&ci->i_cap_snaps)) 5121 iputs = remove_capsnaps(mdsc, inode); 5122 } 5123 if (dirty_dropped) 5124 ++iputs; 5125 return iputs; 5126 } 5127