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