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