1 #include "ceph_debug.h" 2 3 #include <linux/fs.h> 4 #include <linux/kernel.h> 5 #include <linux/sched.h> 6 #include <linux/slab.h> 7 #include <linux/vmalloc.h> 8 #include <linux/wait.h> 9 #include <linux/writeback.h> 10 11 #include "super.h" 12 #include "decode.h" 13 #include "messenger.h" 14 15 /* 16 * Capability management 17 * 18 * The Ceph metadata servers control client access to inode metadata 19 * and file data by issuing capabilities, granting clients permission 20 * to read and/or write both inode field and file data to OSDs 21 * (storage nodes). Each capability consists of a set of bits 22 * indicating which operations are allowed. 23 * 24 * If the client holds a *_SHARED cap, the client has a coherent value 25 * that can be safely read from the cached inode. 26 * 27 * In the case of a *_EXCL (exclusive) or FILE_WR capabilities, the 28 * client is allowed to change inode attributes (e.g., file size, 29 * mtime), note its dirty state in the ceph_cap, and asynchronously 30 * flush that metadata change to the MDS. 31 * 32 * In the event of a conflicting operation (perhaps by another 33 * client), the MDS will revoke the conflicting client capabilities. 34 * 35 * In order for a client to cache an inode, it must hold a capability 36 * with at least one MDS server. When inodes are released, release 37 * notifications are batched and periodically sent en masse to the MDS 38 * cluster to release server state. 39 */ 40 41 42 /* 43 * Generate readable cap strings for debugging output. 44 */ 45 #define MAX_CAP_STR 20 46 static char cap_str[MAX_CAP_STR][40]; 47 static DEFINE_SPINLOCK(cap_str_lock); 48 static int last_cap_str; 49 50 static char *gcap_string(char *s, int c) 51 { 52 if (c & CEPH_CAP_GSHARED) 53 *s++ = 's'; 54 if (c & CEPH_CAP_GEXCL) 55 *s++ = 'x'; 56 if (c & CEPH_CAP_GCACHE) 57 *s++ = 'c'; 58 if (c & CEPH_CAP_GRD) 59 *s++ = 'r'; 60 if (c & CEPH_CAP_GWR) 61 *s++ = 'w'; 62 if (c & CEPH_CAP_GBUFFER) 63 *s++ = 'b'; 64 if (c & CEPH_CAP_GLAZYIO) 65 *s++ = 'l'; 66 return s; 67 } 68 69 const char *ceph_cap_string(int caps) 70 { 71 int i; 72 char *s; 73 int c; 74 75 spin_lock(&cap_str_lock); 76 i = last_cap_str++; 77 if (last_cap_str == MAX_CAP_STR) 78 last_cap_str = 0; 79 spin_unlock(&cap_str_lock); 80 81 s = cap_str[i]; 82 83 if (caps & CEPH_CAP_PIN) 84 *s++ = 'p'; 85 86 c = (caps >> CEPH_CAP_SAUTH) & 3; 87 if (c) { 88 *s++ = 'A'; 89 s = gcap_string(s, c); 90 } 91 92 c = (caps >> CEPH_CAP_SLINK) & 3; 93 if (c) { 94 *s++ = 'L'; 95 s = gcap_string(s, c); 96 } 97 98 c = (caps >> CEPH_CAP_SXATTR) & 3; 99 if (c) { 100 *s++ = 'X'; 101 s = gcap_string(s, c); 102 } 103 104 c = caps >> CEPH_CAP_SFILE; 105 if (c) { 106 *s++ = 'F'; 107 s = gcap_string(s, c); 108 } 109 110 if (s == cap_str[i]) 111 *s++ = '-'; 112 *s = 0; 113 return cap_str[i]; 114 } 115 116 /* 117 * Cap reservations 118 * 119 * Maintain a global pool of preallocated struct ceph_caps, referenced 120 * by struct ceph_caps_reservations. This ensures that we preallocate 121 * memory needed to successfully process an MDS response. (If an MDS 122 * sends us cap information and we fail to process it, we will have 123 * problems due to the client and MDS being out of sync.) 124 * 125 * Reservations are 'owned' by a ceph_cap_reservation context. 126 */ 127 static spinlock_t caps_list_lock; 128 static struct list_head caps_list; /* unused (reserved or unreserved) */ 129 static int caps_total_count; /* total caps allocated */ 130 static int caps_use_count; /* in use */ 131 static int caps_reserve_count; /* unused, reserved */ 132 static int caps_avail_count; /* unused, unreserved */ 133 static int caps_min_count; /* keep at least this many (unreserved) */ 134 135 void __init ceph_caps_init(void) 136 { 137 INIT_LIST_HEAD(&caps_list); 138 spin_lock_init(&caps_list_lock); 139 } 140 141 void ceph_caps_finalize(void) 142 { 143 struct ceph_cap *cap; 144 145 spin_lock(&caps_list_lock); 146 while (!list_empty(&caps_list)) { 147 cap = list_first_entry(&caps_list, struct ceph_cap, caps_item); 148 list_del(&cap->caps_item); 149 kmem_cache_free(ceph_cap_cachep, cap); 150 } 151 caps_total_count = 0; 152 caps_avail_count = 0; 153 caps_use_count = 0; 154 caps_reserve_count = 0; 155 caps_min_count = 0; 156 spin_unlock(&caps_list_lock); 157 } 158 159 void ceph_adjust_min_caps(int delta) 160 { 161 spin_lock(&caps_list_lock); 162 caps_min_count += delta; 163 BUG_ON(caps_min_count < 0); 164 spin_unlock(&caps_list_lock); 165 } 166 167 int ceph_reserve_caps(struct ceph_cap_reservation *ctx, int need) 168 { 169 int i; 170 struct ceph_cap *cap; 171 int have; 172 int alloc = 0; 173 LIST_HEAD(newcaps); 174 int ret = 0; 175 176 dout("reserve caps ctx=%p need=%d\n", ctx, need); 177 178 /* first reserve any caps that are already allocated */ 179 spin_lock(&caps_list_lock); 180 if (caps_avail_count >= need) 181 have = need; 182 else 183 have = caps_avail_count; 184 caps_avail_count -= have; 185 caps_reserve_count += have; 186 BUG_ON(caps_total_count != caps_use_count + caps_reserve_count + 187 caps_avail_count); 188 spin_unlock(&caps_list_lock); 189 190 for (i = have; i < need; i++) { 191 cap = kmem_cache_alloc(ceph_cap_cachep, GFP_NOFS); 192 if (!cap) { 193 ret = -ENOMEM; 194 goto out_alloc_count; 195 } 196 list_add(&cap->caps_item, &newcaps); 197 alloc++; 198 } 199 BUG_ON(have + alloc != need); 200 201 spin_lock(&caps_list_lock); 202 caps_total_count += alloc; 203 caps_reserve_count += alloc; 204 list_splice(&newcaps, &caps_list); 205 206 BUG_ON(caps_total_count != caps_use_count + caps_reserve_count + 207 caps_avail_count); 208 spin_unlock(&caps_list_lock); 209 210 ctx->count = need; 211 dout("reserve caps ctx=%p %d = %d used + %d resv + %d avail\n", 212 ctx, caps_total_count, caps_use_count, caps_reserve_count, 213 caps_avail_count); 214 return 0; 215 216 out_alloc_count: 217 /* we didn't manage to reserve as much as we needed */ 218 pr_warning("reserve caps ctx=%p ENOMEM need=%d got=%d\n", 219 ctx, need, have); 220 return ret; 221 } 222 223 int ceph_unreserve_caps(struct ceph_cap_reservation *ctx) 224 { 225 dout("unreserve caps ctx=%p count=%d\n", ctx, ctx->count); 226 if (ctx->count) { 227 spin_lock(&caps_list_lock); 228 BUG_ON(caps_reserve_count < ctx->count); 229 caps_reserve_count -= ctx->count; 230 caps_avail_count += ctx->count; 231 ctx->count = 0; 232 dout("unreserve caps %d = %d used + %d resv + %d avail\n", 233 caps_total_count, caps_use_count, caps_reserve_count, 234 caps_avail_count); 235 BUG_ON(caps_total_count != caps_use_count + caps_reserve_count + 236 caps_avail_count); 237 spin_unlock(&caps_list_lock); 238 } 239 return 0; 240 } 241 242 static struct ceph_cap *get_cap(struct ceph_cap_reservation *ctx) 243 { 244 struct ceph_cap *cap = NULL; 245 246 /* temporary, until we do something about cap import/export */ 247 if (!ctx) 248 return kmem_cache_alloc(ceph_cap_cachep, GFP_NOFS); 249 250 spin_lock(&caps_list_lock); 251 dout("get_cap ctx=%p (%d) %d = %d used + %d resv + %d avail\n", 252 ctx, ctx->count, caps_total_count, caps_use_count, 253 caps_reserve_count, caps_avail_count); 254 BUG_ON(!ctx->count); 255 BUG_ON(ctx->count > caps_reserve_count); 256 BUG_ON(list_empty(&caps_list)); 257 258 ctx->count--; 259 caps_reserve_count--; 260 caps_use_count++; 261 262 cap = list_first_entry(&caps_list, struct ceph_cap, caps_item); 263 list_del(&cap->caps_item); 264 265 BUG_ON(caps_total_count != caps_use_count + caps_reserve_count + 266 caps_avail_count); 267 spin_unlock(&caps_list_lock); 268 return cap; 269 } 270 271 void ceph_put_cap(struct ceph_cap *cap) 272 { 273 spin_lock(&caps_list_lock); 274 dout("put_cap %p %d = %d used + %d resv + %d avail\n", 275 cap, caps_total_count, caps_use_count, 276 caps_reserve_count, caps_avail_count); 277 caps_use_count--; 278 /* 279 * Keep some preallocated caps around (ceph_min_count), to 280 * avoid lots of free/alloc churn. 281 */ 282 if (caps_avail_count >= caps_reserve_count + caps_min_count) { 283 caps_total_count--; 284 kmem_cache_free(ceph_cap_cachep, cap); 285 } else { 286 caps_avail_count++; 287 list_add(&cap->caps_item, &caps_list); 288 } 289 290 BUG_ON(caps_total_count != caps_use_count + caps_reserve_count + 291 caps_avail_count); 292 spin_unlock(&caps_list_lock); 293 } 294 295 void ceph_reservation_status(struct ceph_client *client, 296 int *total, int *avail, int *used, int *reserved, 297 int *min) 298 { 299 if (total) 300 *total = caps_total_count; 301 if (avail) 302 *avail = caps_avail_count; 303 if (used) 304 *used = caps_use_count; 305 if (reserved) 306 *reserved = caps_reserve_count; 307 if (min) 308 *min = caps_min_count; 309 } 310 311 /* 312 * Find ceph_cap for given mds, if any. 313 * 314 * Called with i_lock held. 315 */ 316 static struct ceph_cap *__get_cap_for_mds(struct ceph_inode_info *ci, int mds) 317 { 318 struct ceph_cap *cap; 319 struct rb_node *n = ci->i_caps.rb_node; 320 321 while (n) { 322 cap = rb_entry(n, struct ceph_cap, ci_node); 323 if (mds < cap->mds) 324 n = n->rb_left; 325 else if (mds > cap->mds) 326 n = n->rb_right; 327 else 328 return cap; 329 } 330 return NULL; 331 } 332 333 /* 334 * Return id of any MDS with a cap, preferably FILE_WR|WRBUFFER|EXCL, else 335 * -1. 336 */ 337 static int __ceph_get_cap_mds(struct ceph_inode_info *ci, u32 *mseq) 338 { 339 struct ceph_cap *cap; 340 int mds = -1; 341 struct rb_node *p; 342 343 /* prefer mds with WR|WRBUFFER|EXCL caps */ 344 for (p = rb_first(&ci->i_caps); p; p = rb_next(p)) { 345 cap = rb_entry(p, struct ceph_cap, ci_node); 346 mds = cap->mds; 347 if (mseq) 348 *mseq = cap->mseq; 349 if (cap->issued & (CEPH_CAP_FILE_WR | 350 CEPH_CAP_FILE_BUFFER | 351 CEPH_CAP_FILE_EXCL)) 352 break; 353 } 354 return mds; 355 } 356 357 int ceph_get_cap_mds(struct inode *inode) 358 { 359 int mds; 360 spin_lock(&inode->i_lock); 361 mds = __ceph_get_cap_mds(ceph_inode(inode), NULL); 362 spin_unlock(&inode->i_lock); 363 return mds; 364 } 365 366 /* 367 * Called under i_lock. 368 */ 369 static void __insert_cap_node(struct ceph_inode_info *ci, 370 struct ceph_cap *new) 371 { 372 struct rb_node **p = &ci->i_caps.rb_node; 373 struct rb_node *parent = NULL; 374 struct ceph_cap *cap = NULL; 375 376 while (*p) { 377 parent = *p; 378 cap = rb_entry(parent, struct ceph_cap, ci_node); 379 if (new->mds < cap->mds) 380 p = &(*p)->rb_left; 381 else if (new->mds > cap->mds) 382 p = &(*p)->rb_right; 383 else 384 BUG(); 385 } 386 387 rb_link_node(&new->ci_node, parent, p); 388 rb_insert_color(&new->ci_node, &ci->i_caps); 389 } 390 391 /* 392 * (re)set cap hold timeouts, which control the delayed release 393 * of unused caps back to the MDS. Should be called on cap use. 394 */ 395 static void __cap_set_timeouts(struct ceph_mds_client *mdsc, 396 struct ceph_inode_info *ci) 397 { 398 struct ceph_mount_args *ma = mdsc->client->mount_args; 399 400 ci->i_hold_caps_min = round_jiffies(jiffies + 401 ma->caps_wanted_delay_min * HZ); 402 ci->i_hold_caps_max = round_jiffies(jiffies + 403 ma->caps_wanted_delay_max * HZ); 404 dout("__cap_set_timeouts %p min %lu max %lu\n", &ci->vfs_inode, 405 ci->i_hold_caps_min - jiffies, ci->i_hold_caps_max - jiffies); 406 } 407 408 /* 409 * (Re)queue cap at the end of the delayed cap release list. 410 * 411 * If I_FLUSH is set, leave the inode at the front of the list. 412 * 413 * Caller holds i_lock 414 * -> we take mdsc->cap_delay_lock 415 */ 416 static void __cap_delay_requeue(struct ceph_mds_client *mdsc, 417 struct ceph_inode_info *ci) 418 { 419 __cap_set_timeouts(mdsc, ci); 420 dout("__cap_delay_requeue %p flags %d at %lu\n", &ci->vfs_inode, 421 ci->i_ceph_flags, ci->i_hold_caps_max); 422 if (!mdsc->stopping) { 423 spin_lock(&mdsc->cap_delay_lock); 424 if (!list_empty(&ci->i_cap_delay_list)) { 425 if (ci->i_ceph_flags & CEPH_I_FLUSH) 426 goto no_change; 427 list_del_init(&ci->i_cap_delay_list); 428 } 429 list_add_tail(&ci->i_cap_delay_list, &mdsc->cap_delay_list); 430 no_change: 431 spin_unlock(&mdsc->cap_delay_lock); 432 } 433 } 434 435 /* 436 * Queue an inode for immediate writeback. Mark inode with I_FLUSH, 437 * indicating we should send a cap message to flush dirty metadata 438 * asap, and move to the front of the delayed cap list. 439 */ 440 static void __cap_delay_requeue_front(struct ceph_mds_client *mdsc, 441 struct ceph_inode_info *ci) 442 { 443 dout("__cap_delay_requeue_front %p\n", &ci->vfs_inode); 444 spin_lock(&mdsc->cap_delay_lock); 445 ci->i_ceph_flags |= CEPH_I_FLUSH; 446 if (!list_empty(&ci->i_cap_delay_list)) 447 list_del_init(&ci->i_cap_delay_list); 448 list_add(&ci->i_cap_delay_list, &mdsc->cap_delay_list); 449 spin_unlock(&mdsc->cap_delay_lock); 450 } 451 452 /* 453 * Cancel delayed work on cap. 454 * 455 * Caller must hold i_lock. 456 */ 457 static void __cap_delay_cancel(struct ceph_mds_client *mdsc, 458 struct ceph_inode_info *ci) 459 { 460 dout("__cap_delay_cancel %p\n", &ci->vfs_inode); 461 if (list_empty(&ci->i_cap_delay_list)) 462 return; 463 spin_lock(&mdsc->cap_delay_lock); 464 list_del_init(&ci->i_cap_delay_list); 465 spin_unlock(&mdsc->cap_delay_lock); 466 } 467 468 /* 469 * Common issue checks for add_cap, handle_cap_grant. 470 */ 471 static void __check_cap_issue(struct ceph_inode_info *ci, struct ceph_cap *cap, 472 unsigned issued) 473 { 474 unsigned had = __ceph_caps_issued(ci, NULL); 475 476 /* 477 * Each time we receive FILE_CACHE anew, we increment 478 * i_rdcache_gen. 479 */ 480 if ((issued & CEPH_CAP_FILE_CACHE) && 481 (had & CEPH_CAP_FILE_CACHE) == 0) 482 ci->i_rdcache_gen++; 483 484 /* 485 * if we are newly issued FILE_SHARED, clear I_COMPLETE; we 486 * don't know what happened to this directory while we didn't 487 * have the cap. 488 */ 489 if ((issued & CEPH_CAP_FILE_SHARED) && 490 (had & CEPH_CAP_FILE_SHARED) == 0) { 491 ci->i_shared_gen++; 492 if (S_ISDIR(ci->vfs_inode.i_mode)) { 493 dout(" marking %p NOT complete\n", &ci->vfs_inode); 494 ci->i_ceph_flags &= ~CEPH_I_COMPLETE; 495 } 496 } 497 } 498 499 /* 500 * Add a capability under the given MDS session. 501 * 502 * Caller should hold session snap_rwsem (read) and s_mutex. 503 * 504 * @fmode is the open file mode, if we are opening a file, otherwise 505 * it is < 0. (This is so we can atomically add the cap and add an 506 * open file reference to it.) 507 */ 508 int ceph_add_cap(struct inode *inode, 509 struct ceph_mds_session *session, u64 cap_id, 510 int fmode, unsigned issued, unsigned wanted, 511 unsigned seq, unsigned mseq, u64 realmino, int flags, 512 struct ceph_cap_reservation *caps_reservation) 513 { 514 struct ceph_mds_client *mdsc = &ceph_inode_to_client(inode)->mdsc; 515 struct ceph_inode_info *ci = ceph_inode(inode); 516 struct ceph_cap *new_cap = NULL; 517 struct ceph_cap *cap; 518 int mds = session->s_mds; 519 int actual_wanted; 520 521 dout("add_cap %p mds%d cap %llx %s seq %d\n", inode, 522 session->s_mds, cap_id, ceph_cap_string(issued), seq); 523 524 /* 525 * If we are opening the file, include file mode wanted bits 526 * in wanted. 527 */ 528 if (fmode >= 0) 529 wanted |= ceph_caps_for_mode(fmode); 530 531 retry: 532 spin_lock(&inode->i_lock); 533 cap = __get_cap_for_mds(ci, mds); 534 if (!cap) { 535 if (new_cap) { 536 cap = new_cap; 537 new_cap = NULL; 538 } else { 539 spin_unlock(&inode->i_lock); 540 new_cap = get_cap(caps_reservation); 541 if (new_cap == NULL) 542 return -ENOMEM; 543 goto retry; 544 } 545 546 cap->issued = 0; 547 cap->implemented = 0; 548 cap->mds = mds; 549 cap->mds_wanted = 0; 550 551 cap->ci = ci; 552 __insert_cap_node(ci, cap); 553 554 /* clear out old exporting info? (i.e. on cap import) */ 555 if (ci->i_cap_exporting_mds == mds) { 556 ci->i_cap_exporting_issued = 0; 557 ci->i_cap_exporting_mseq = 0; 558 ci->i_cap_exporting_mds = -1; 559 } 560 561 /* add to session cap list */ 562 cap->session = session; 563 spin_lock(&session->s_cap_lock); 564 list_add_tail(&cap->session_caps, &session->s_caps); 565 session->s_nr_caps++; 566 spin_unlock(&session->s_cap_lock); 567 } 568 569 if (!ci->i_snap_realm) { 570 /* 571 * add this inode to the appropriate snap realm 572 */ 573 struct ceph_snap_realm *realm = ceph_lookup_snap_realm(mdsc, 574 realmino); 575 if (realm) { 576 ceph_get_snap_realm(mdsc, realm); 577 spin_lock(&realm->inodes_with_caps_lock); 578 ci->i_snap_realm = realm; 579 list_add(&ci->i_snap_realm_item, 580 &realm->inodes_with_caps); 581 spin_unlock(&realm->inodes_with_caps_lock); 582 } else { 583 pr_err("ceph_add_cap: couldn't find snap realm %llx\n", 584 realmino); 585 } 586 } 587 588 __check_cap_issue(ci, cap, issued); 589 590 /* 591 * If we are issued caps we don't want, or the mds' wanted 592 * value appears to be off, queue a check so we'll release 593 * later and/or update the mds wanted value. 594 */ 595 actual_wanted = __ceph_caps_wanted(ci); 596 if ((wanted & ~actual_wanted) || 597 (issued & ~actual_wanted & CEPH_CAP_ANY_WR)) { 598 dout(" issued %s, mds wanted %s, actual %s, queueing\n", 599 ceph_cap_string(issued), ceph_cap_string(wanted), 600 ceph_cap_string(actual_wanted)); 601 __cap_delay_requeue(mdsc, ci); 602 } 603 604 if (flags & CEPH_CAP_FLAG_AUTH) 605 ci->i_auth_cap = cap; 606 else if (ci->i_auth_cap == cap) 607 ci->i_auth_cap = NULL; 608 609 dout("add_cap inode %p (%llx.%llx) cap %p %s now %s seq %d mds%d\n", 610 inode, ceph_vinop(inode), cap, ceph_cap_string(issued), 611 ceph_cap_string(issued|cap->issued), seq, mds); 612 cap->cap_id = cap_id; 613 cap->issued = issued; 614 cap->implemented |= issued; 615 cap->mds_wanted |= wanted; 616 cap->seq = seq; 617 cap->issue_seq = seq; 618 cap->mseq = mseq; 619 cap->cap_gen = session->s_cap_gen; 620 621 if (fmode >= 0) 622 __ceph_get_fmode(ci, fmode); 623 spin_unlock(&inode->i_lock); 624 wake_up(&ci->i_cap_wq); 625 return 0; 626 } 627 628 /* 629 * Return true if cap has not timed out and belongs to the current 630 * generation of the MDS session (i.e. has not gone 'stale' due to 631 * us losing touch with the mds). 632 */ 633 static int __cap_is_valid(struct ceph_cap *cap) 634 { 635 unsigned long ttl; 636 u32 gen; 637 638 spin_lock(&cap->session->s_cap_lock); 639 gen = cap->session->s_cap_gen; 640 ttl = cap->session->s_cap_ttl; 641 spin_unlock(&cap->session->s_cap_lock); 642 643 if (cap->cap_gen < gen || time_after_eq(jiffies, ttl)) { 644 dout("__cap_is_valid %p cap %p issued %s " 645 "but STALE (gen %u vs %u)\n", &cap->ci->vfs_inode, 646 cap, ceph_cap_string(cap->issued), cap->cap_gen, gen); 647 return 0; 648 } 649 650 return 1; 651 } 652 653 /* 654 * Return set of valid cap bits issued to us. Note that caps time 655 * out, and may be invalidated in bulk if the client session times out 656 * and session->s_cap_gen is bumped. 657 */ 658 int __ceph_caps_issued(struct ceph_inode_info *ci, int *implemented) 659 { 660 int have = ci->i_snap_caps | ci->i_cap_exporting_issued; 661 struct ceph_cap *cap; 662 struct rb_node *p; 663 664 if (implemented) 665 *implemented = 0; 666 for (p = rb_first(&ci->i_caps); p; p = rb_next(p)) { 667 cap = rb_entry(p, struct ceph_cap, ci_node); 668 if (!__cap_is_valid(cap)) 669 continue; 670 dout("__ceph_caps_issued %p cap %p issued %s\n", 671 &ci->vfs_inode, cap, ceph_cap_string(cap->issued)); 672 have |= cap->issued; 673 if (implemented) 674 *implemented |= cap->implemented; 675 } 676 return have; 677 } 678 679 /* 680 * Get cap bits issued by caps other than @ocap 681 */ 682 int __ceph_caps_issued_other(struct ceph_inode_info *ci, struct ceph_cap *ocap) 683 { 684 int have = ci->i_snap_caps; 685 struct ceph_cap *cap; 686 struct rb_node *p; 687 688 for (p = rb_first(&ci->i_caps); p; p = rb_next(p)) { 689 cap = rb_entry(p, struct ceph_cap, ci_node); 690 if (cap == ocap) 691 continue; 692 if (!__cap_is_valid(cap)) 693 continue; 694 have |= cap->issued; 695 } 696 return have; 697 } 698 699 /* 700 * Move a cap to the end of the LRU (oldest caps at list head, newest 701 * at list tail). 702 */ 703 static void __touch_cap(struct ceph_cap *cap) 704 { 705 struct ceph_mds_session *s = cap->session; 706 707 spin_lock(&s->s_cap_lock); 708 if (s->s_cap_iterator == NULL) { 709 dout("__touch_cap %p cap %p mds%d\n", &cap->ci->vfs_inode, cap, 710 s->s_mds); 711 list_move_tail(&cap->session_caps, &s->s_caps); 712 } else { 713 dout("__touch_cap %p cap %p mds%d NOP, iterating over caps\n", 714 &cap->ci->vfs_inode, cap, s->s_mds); 715 } 716 spin_unlock(&s->s_cap_lock); 717 } 718 719 /* 720 * Check if we hold the given mask. If so, move the cap(s) to the 721 * front of their respective LRUs. (This is the preferred way for 722 * callers to check for caps they want.) 723 */ 724 int __ceph_caps_issued_mask(struct ceph_inode_info *ci, int mask, int touch) 725 { 726 struct ceph_cap *cap; 727 struct rb_node *p; 728 int have = ci->i_snap_caps; 729 730 if ((have & mask) == mask) { 731 dout("__ceph_caps_issued_mask %p snap issued %s" 732 " (mask %s)\n", &ci->vfs_inode, 733 ceph_cap_string(have), 734 ceph_cap_string(mask)); 735 return 1; 736 } 737 738 for (p = rb_first(&ci->i_caps); p; p = rb_next(p)) { 739 cap = rb_entry(p, struct ceph_cap, ci_node); 740 if (!__cap_is_valid(cap)) 741 continue; 742 if ((cap->issued & mask) == mask) { 743 dout("__ceph_caps_issued_mask %p cap %p issued %s" 744 " (mask %s)\n", &ci->vfs_inode, cap, 745 ceph_cap_string(cap->issued), 746 ceph_cap_string(mask)); 747 if (touch) 748 __touch_cap(cap); 749 return 1; 750 } 751 752 /* does a combination of caps satisfy mask? */ 753 have |= cap->issued; 754 if ((have & mask) == mask) { 755 dout("__ceph_caps_issued_mask %p combo issued %s" 756 " (mask %s)\n", &ci->vfs_inode, 757 ceph_cap_string(cap->issued), 758 ceph_cap_string(mask)); 759 if (touch) { 760 struct rb_node *q; 761 762 /* touch this + preceeding caps */ 763 __touch_cap(cap); 764 for (q = rb_first(&ci->i_caps); q != p; 765 q = rb_next(q)) { 766 cap = rb_entry(q, struct ceph_cap, 767 ci_node); 768 if (!__cap_is_valid(cap)) 769 continue; 770 __touch_cap(cap); 771 } 772 } 773 return 1; 774 } 775 } 776 777 return 0; 778 } 779 780 /* 781 * Return true if mask caps are currently being revoked by an MDS. 782 */ 783 int ceph_caps_revoking(struct ceph_inode_info *ci, int mask) 784 { 785 struct inode *inode = &ci->vfs_inode; 786 struct ceph_cap *cap; 787 struct rb_node *p; 788 int ret = 0; 789 790 spin_lock(&inode->i_lock); 791 for (p = rb_first(&ci->i_caps); p; p = rb_next(p)) { 792 cap = rb_entry(p, struct ceph_cap, ci_node); 793 if (__cap_is_valid(cap) && 794 (cap->implemented & ~cap->issued & mask)) { 795 ret = 1; 796 break; 797 } 798 } 799 spin_unlock(&inode->i_lock); 800 dout("ceph_caps_revoking %p %s = %d\n", inode, 801 ceph_cap_string(mask), ret); 802 return ret; 803 } 804 805 int __ceph_caps_used(struct ceph_inode_info *ci) 806 { 807 int used = 0; 808 if (ci->i_pin_ref) 809 used |= CEPH_CAP_PIN; 810 if (ci->i_rd_ref) 811 used |= CEPH_CAP_FILE_RD; 812 if (ci->i_rdcache_ref || ci->i_rdcache_gen) 813 used |= CEPH_CAP_FILE_CACHE; 814 if (ci->i_wr_ref) 815 used |= CEPH_CAP_FILE_WR; 816 if (ci->i_wrbuffer_ref) 817 used |= CEPH_CAP_FILE_BUFFER; 818 return used; 819 } 820 821 /* 822 * wanted, by virtue of open file modes 823 */ 824 int __ceph_caps_file_wanted(struct ceph_inode_info *ci) 825 { 826 int want = 0; 827 int mode; 828 for (mode = 0; mode < 4; mode++) 829 if (ci->i_nr_by_mode[mode]) 830 want |= ceph_caps_for_mode(mode); 831 return want; 832 } 833 834 /* 835 * Return caps we have registered with the MDS(s) as 'wanted'. 836 */ 837 int __ceph_caps_mds_wanted(struct ceph_inode_info *ci) 838 { 839 struct ceph_cap *cap; 840 struct rb_node *p; 841 int mds_wanted = 0; 842 843 for (p = rb_first(&ci->i_caps); p; p = rb_next(p)) { 844 cap = rb_entry(p, struct ceph_cap, ci_node); 845 if (!__cap_is_valid(cap)) 846 continue; 847 mds_wanted |= cap->mds_wanted; 848 } 849 return mds_wanted; 850 } 851 852 /* 853 * called under i_lock 854 */ 855 static int __ceph_is_any_caps(struct ceph_inode_info *ci) 856 { 857 return !RB_EMPTY_ROOT(&ci->i_caps) || ci->i_cap_exporting_mds >= 0; 858 } 859 860 /* 861 * Remove a cap. Take steps to deal with a racing iterate_session_caps. 862 * 863 * caller should hold i_lock. 864 * caller will not hold session s_mutex if called from destroy_inode. 865 */ 866 void __ceph_remove_cap(struct ceph_cap *cap) 867 { 868 struct ceph_mds_session *session = cap->session; 869 struct ceph_inode_info *ci = cap->ci; 870 struct ceph_mds_client *mdsc = 871 &ceph_sb_to_client(ci->vfs_inode.i_sb)->mdsc; 872 int removed = 0; 873 874 dout("__ceph_remove_cap %p from %p\n", cap, &ci->vfs_inode); 875 876 /* remove from session list */ 877 spin_lock(&session->s_cap_lock); 878 if (session->s_cap_iterator == cap) { 879 /* not yet, we are iterating over this very cap */ 880 dout("__ceph_remove_cap delaying %p removal from session %p\n", 881 cap, cap->session); 882 } else { 883 list_del_init(&cap->session_caps); 884 session->s_nr_caps--; 885 cap->session = NULL; 886 removed = 1; 887 } 888 /* protect backpointer with s_cap_lock: see iterate_session_caps */ 889 cap->ci = NULL; 890 spin_unlock(&session->s_cap_lock); 891 892 /* remove from inode list */ 893 rb_erase(&cap->ci_node, &ci->i_caps); 894 if (ci->i_auth_cap == cap) 895 ci->i_auth_cap = NULL; 896 897 if (removed) 898 ceph_put_cap(cap); 899 900 if (!__ceph_is_any_caps(ci) && ci->i_snap_realm) { 901 struct ceph_snap_realm *realm = ci->i_snap_realm; 902 spin_lock(&realm->inodes_with_caps_lock); 903 list_del_init(&ci->i_snap_realm_item); 904 ci->i_snap_realm_counter++; 905 ci->i_snap_realm = NULL; 906 spin_unlock(&realm->inodes_with_caps_lock); 907 ceph_put_snap_realm(mdsc, realm); 908 } 909 if (!__ceph_is_any_real_caps(ci)) 910 __cap_delay_cancel(mdsc, ci); 911 } 912 913 /* 914 * Build and send a cap message to the given MDS. 915 * 916 * Caller should be holding s_mutex. 917 */ 918 static int send_cap_msg(struct ceph_mds_session *session, 919 u64 ino, u64 cid, int op, 920 int caps, int wanted, int dirty, 921 u32 seq, u64 flush_tid, u32 issue_seq, u32 mseq, 922 u64 size, u64 max_size, 923 struct timespec *mtime, struct timespec *atime, 924 u64 time_warp_seq, 925 uid_t uid, gid_t gid, mode_t mode, 926 u64 xattr_version, 927 struct ceph_buffer *xattrs_buf, 928 u64 follows) 929 { 930 struct ceph_mds_caps *fc; 931 struct ceph_msg *msg; 932 933 dout("send_cap_msg %s %llx %llx caps %s wanted %s dirty %s" 934 " seq %u/%u mseq %u follows %lld size %llu/%llu" 935 " xattr_ver %llu xattr_len %d\n", ceph_cap_op_name(op), 936 cid, ino, ceph_cap_string(caps), ceph_cap_string(wanted), 937 ceph_cap_string(dirty), 938 seq, issue_seq, mseq, follows, size, max_size, 939 xattr_version, xattrs_buf ? (int)xattrs_buf->vec.iov_len : 0); 940 941 msg = ceph_msg_new(CEPH_MSG_CLIENT_CAPS, sizeof(*fc), GFP_NOFS); 942 if (!msg) 943 return -ENOMEM; 944 945 msg->hdr.tid = cpu_to_le64(flush_tid); 946 947 fc = msg->front.iov_base; 948 memset(fc, 0, sizeof(*fc)); 949 950 fc->cap_id = cpu_to_le64(cid); 951 fc->op = cpu_to_le32(op); 952 fc->seq = cpu_to_le32(seq); 953 fc->issue_seq = cpu_to_le32(issue_seq); 954 fc->migrate_seq = cpu_to_le32(mseq); 955 fc->caps = cpu_to_le32(caps); 956 fc->wanted = cpu_to_le32(wanted); 957 fc->dirty = cpu_to_le32(dirty); 958 fc->ino = cpu_to_le64(ino); 959 fc->snap_follows = cpu_to_le64(follows); 960 961 fc->size = cpu_to_le64(size); 962 fc->max_size = cpu_to_le64(max_size); 963 if (mtime) 964 ceph_encode_timespec(&fc->mtime, mtime); 965 if (atime) 966 ceph_encode_timespec(&fc->atime, atime); 967 fc->time_warp_seq = cpu_to_le32(time_warp_seq); 968 969 fc->uid = cpu_to_le32(uid); 970 fc->gid = cpu_to_le32(gid); 971 fc->mode = cpu_to_le32(mode); 972 973 fc->xattr_version = cpu_to_le64(xattr_version); 974 if (xattrs_buf) { 975 msg->middle = ceph_buffer_get(xattrs_buf); 976 fc->xattr_len = cpu_to_le32(xattrs_buf->vec.iov_len); 977 msg->hdr.middle_len = cpu_to_le32(xattrs_buf->vec.iov_len); 978 } 979 980 ceph_con_send(&session->s_con, msg); 981 return 0; 982 } 983 984 static void __queue_cap_release(struct ceph_mds_session *session, 985 u64 ino, u64 cap_id, u32 migrate_seq, 986 u32 issue_seq) 987 { 988 struct ceph_msg *msg; 989 struct ceph_mds_cap_release *head; 990 struct ceph_mds_cap_item *item; 991 992 spin_lock(&session->s_cap_lock); 993 BUG_ON(!session->s_num_cap_releases); 994 msg = list_first_entry(&session->s_cap_releases, 995 struct ceph_msg, list_head); 996 997 dout(" adding %llx release to mds%d msg %p (%d left)\n", 998 ino, session->s_mds, msg, session->s_num_cap_releases); 999 1000 BUG_ON(msg->front.iov_len + sizeof(*item) > PAGE_CACHE_SIZE); 1001 head = msg->front.iov_base; 1002 head->num = cpu_to_le32(le32_to_cpu(head->num) + 1); 1003 item = msg->front.iov_base + msg->front.iov_len; 1004 item->ino = cpu_to_le64(ino); 1005 item->cap_id = cpu_to_le64(cap_id); 1006 item->migrate_seq = cpu_to_le32(migrate_seq); 1007 item->seq = cpu_to_le32(issue_seq); 1008 1009 session->s_num_cap_releases--; 1010 1011 msg->front.iov_len += sizeof(*item); 1012 if (le32_to_cpu(head->num) == CEPH_CAPS_PER_RELEASE) { 1013 dout(" release msg %p full\n", msg); 1014 list_move_tail(&msg->list_head, &session->s_cap_releases_done); 1015 } else { 1016 dout(" release msg %p at %d/%d (%d)\n", msg, 1017 (int)le32_to_cpu(head->num), 1018 (int)CEPH_CAPS_PER_RELEASE, 1019 (int)msg->front.iov_len); 1020 } 1021 spin_unlock(&session->s_cap_lock); 1022 } 1023 1024 /* 1025 * Queue cap releases when an inode is dropped from our cache. Since 1026 * inode is about to be destroyed, there is no need for i_lock. 1027 */ 1028 void ceph_queue_caps_release(struct inode *inode) 1029 { 1030 struct ceph_inode_info *ci = ceph_inode(inode); 1031 struct rb_node *p; 1032 1033 p = rb_first(&ci->i_caps); 1034 while (p) { 1035 struct ceph_cap *cap = rb_entry(p, struct ceph_cap, ci_node); 1036 struct ceph_mds_session *session = cap->session; 1037 1038 __queue_cap_release(session, ceph_ino(inode), cap->cap_id, 1039 cap->mseq, cap->issue_seq); 1040 p = rb_next(p); 1041 __ceph_remove_cap(cap); 1042 } 1043 } 1044 1045 /* 1046 * Send a cap msg on the given inode. Update our caps state, then 1047 * drop i_lock and send the message. 1048 * 1049 * Make note of max_size reported/requested from mds, revoked caps 1050 * that have now been implemented. 1051 * 1052 * Make half-hearted attempt ot to invalidate page cache if we are 1053 * dropping RDCACHE. Note that this will leave behind locked pages 1054 * that we'll then need to deal with elsewhere. 1055 * 1056 * Return non-zero if delayed release, or we experienced an error 1057 * such that the caller should requeue + retry later. 1058 * 1059 * called with i_lock, then drops it. 1060 * caller should hold snap_rwsem (read), s_mutex. 1061 */ 1062 static int __send_cap(struct ceph_mds_client *mdsc, struct ceph_cap *cap, 1063 int op, int used, int want, int retain, int flushing, 1064 unsigned *pflush_tid) 1065 __releases(cap->ci->vfs_inode->i_lock) 1066 { 1067 struct ceph_inode_info *ci = cap->ci; 1068 struct inode *inode = &ci->vfs_inode; 1069 u64 cap_id = cap->cap_id; 1070 int held, revoking, dropping, keep; 1071 u64 seq, issue_seq, mseq, time_warp_seq, follows; 1072 u64 size, max_size; 1073 struct timespec mtime, atime; 1074 int wake = 0; 1075 mode_t mode; 1076 uid_t uid; 1077 gid_t gid; 1078 struct ceph_mds_session *session; 1079 u64 xattr_version = 0; 1080 int delayed = 0; 1081 u64 flush_tid = 0; 1082 int i; 1083 int ret; 1084 1085 held = cap->issued | cap->implemented; 1086 revoking = cap->implemented & ~cap->issued; 1087 retain &= ~revoking; 1088 dropping = cap->issued & ~retain; 1089 1090 dout("__send_cap %p cap %p session %p %s -> %s (revoking %s)\n", 1091 inode, cap, cap->session, 1092 ceph_cap_string(held), ceph_cap_string(held & retain), 1093 ceph_cap_string(revoking)); 1094 BUG_ON((retain & CEPH_CAP_PIN) == 0); 1095 1096 session = cap->session; 1097 1098 /* don't release wanted unless we've waited a bit. */ 1099 if ((ci->i_ceph_flags & CEPH_I_NODELAY) == 0 && 1100 time_before(jiffies, ci->i_hold_caps_min)) { 1101 dout(" delaying issued %s -> %s, wanted %s -> %s on send\n", 1102 ceph_cap_string(cap->issued), 1103 ceph_cap_string(cap->issued & retain), 1104 ceph_cap_string(cap->mds_wanted), 1105 ceph_cap_string(want)); 1106 want |= cap->mds_wanted; 1107 retain |= cap->issued; 1108 delayed = 1; 1109 } 1110 ci->i_ceph_flags &= ~(CEPH_I_NODELAY | CEPH_I_FLUSH); 1111 1112 cap->issued &= retain; /* drop bits we don't want */ 1113 if (cap->implemented & ~cap->issued) { 1114 /* 1115 * Wake up any waiters on wanted -> needed transition. 1116 * This is due to the weird transition from buffered 1117 * to sync IO... we need to flush dirty pages _before_ 1118 * allowing sync writes to avoid reordering. 1119 */ 1120 wake = 1; 1121 } 1122 cap->implemented &= cap->issued | used; 1123 cap->mds_wanted = want; 1124 1125 if (flushing) { 1126 /* 1127 * assign a tid for flush operations so we can avoid 1128 * flush1 -> dirty1 -> flush2 -> flushack1 -> mark 1129 * clean type races. track latest tid for every bit 1130 * so we can handle flush AxFw, flush Fw, and have the 1131 * first ack clean Ax. 1132 */ 1133 flush_tid = ++ci->i_cap_flush_last_tid; 1134 if (pflush_tid) 1135 *pflush_tid = flush_tid; 1136 dout(" cap_flush_tid %d\n", (int)flush_tid); 1137 for (i = 0; i < CEPH_CAP_BITS; i++) 1138 if (flushing & (1 << i)) 1139 ci->i_cap_flush_tid[i] = flush_tid; 1140 } 1141 1142 keep = cap->implemented; 1143 seq = cap->seq; 1144 issue_seq = cap->issue_seq; 1145 mseq = cap->mseq; 1146 size = inode->i_size; 1147 ci->i_reported_size = size; 1148 max_size = ci->i_wanted_max_size; 1149 ci->i_requested_max_size = max_size; 1150 mtime = inode->i_mtime; 1151 atime = inode->i_atime; 1152 time_warp_seq = ci->i_time_warp_seq; 1153 follows = ci->i_snap_realm->cached_context->seq; 1154 uid = inode->i_uid; 1155 gid = inode->i_gid; 1156 mode = inode->i_mode; 1157 1158 if (dropping & CEPH_CAP_XATTR_EXCL) { 1159 __ceph_build_xattrs_blob(ci); 1160 xattr_version = ci->i_xattrs.version + 1; 1161 } 1162 1163 spin_unlock(&inode->i_lock); 1164 1165 ret = send_cap_msg(session, ceph_vino(inode).ino, cap_id, 1166 op, keep, want, flushing, seq, flush_tid, issue_seq, mseq, 1167 size, max_size, &mtime, &atime, time_warp_seq, 1168 uid, gid, mode, 1169 xattr_version, 1170 (flushing & CEPH_CAP_XATTR_EXCL) ? ci->i_xattrs.blob : NULL, 1171 follows); 1172 if (ret < 0) { 1173 dout("error sending cap msg, must requeue %p\n", inode); 1174 delayed = 1; 1175 } 1176 1177 if (wake) 1178 wake_up(&ci->i_cap_wq); 1179 1180 return delayed; 1181 } 1182 1183 /* 1184 * When a snapshot is taken, clients accumulate dirty metadata on 1185 * inodes with capabilities in ceph_cap_snaps to describe the file 1186 * state at the time the snapshot was taken. This must be flushed 1187 * asynchronously back to the MDS once sync writes complete and dirty 1188 * data is written out. 1189 * 1190 * Called under i_lock. Takes s_mutex as needed. 1191 */ 1192 void __ceph_flush_snaps(struct ceph_inode_info *ci, 1193 struct ceph_mds_session **psession) 1194 { 1195 struct inode *inode = &ci->vfs_inode; 1196 int mds; 1197 struct ceph_cap_snap *capsnap; 1198 u32 mseq; 1199 struct ceph_mds_client *mdsc = &ceph_inode_to_client(inode)->mdsc; 1200 struct ceph_mds_session *session = NULL; /* if session != NULL, we hold 1201 session->s_mutex */ 1202 u64 next_follows = 0; /* keep track of how far we've gotten through the 1203 i_cap_snaps list, and skip these entries next time 1204 around to avoid an infinite loop */ 1205 1206 if (psession) 1207 session = *psession; 1208 1209 dout("__flush_snaps %p\n", inode); 1210 retry: 1211 list_for_each_entry(capsnap, &ci->i_cap_snaps, ci_item) { 1212 /* avoid an infiniute loop after retry */ 1213 if (capsnap->follows < next_follows) 1214 continue; 1215 /* 1216 * we need to wait for sync writes to complete and for dirty 1217 * pages to be written out. 1218 */ 1219 if (capsnap->dirty_pages || capsnap->writing) 1220 continue; 1221 1222 /* 1223 * if cap writeback already occurred, we should have dropped 1224 * the capsnap in ceph_put_wrbuffer_cap_refs. 1225 */ 1226 BUG_ON(capsnap->dirty == 0); 1227 1228 /* pick mds, take s_mutex */ 1229 mds = __ceph_get_cap_mds(ci, &mseq); 1230 if (session && session->s_mds != mds) { 1231 dout("oops, wrong session %p mutex\n", session); 1232 mutex_unlock(&session->s_mutex); 1233 ceph_put_mds_session(session); 1234 session = NULL; 1235 } 1236 if (!session) { 1237 spin_unlock(&inode->i_lock); 1238 mutex_lock(&mdsc->mutex); 1239 session = __ceph_lookup_mds_session(mdsc, mds); 1240 mutex_unlock(&mdsc->mutex); 1241 if (session) { 1242 dout("inverting session/ino locks on %p\n", 1243 session); 1244 mutex_lock(&session->s_mutex); 1245 } 1246 /* 1247 * if session == NULL, we raced against a cap 1248 * deletion. retry, and we'll get a better 1249 * @mds value next time. 1250 */ 1251 spin_lock(&inode->i_lock); 1252 goto retry; 1253 } 1254 1255 capsnap->flush_tid = ++ci->i_cap_flush_last_tid; 1256 atomic_inc(&capsnap->nref); 1257 if (!list_empty(&capsnap->flushing_item)) 1258 list_del_init(&capsnap->flushing_item); 1259 list_add_tail(&capsnap->flushing_item, 1260 &session->s_cap_snaps_flushing); 1261 spin_unlock(&inode->i_lock); 1262 1263 dout("flush_snaps %p cap_snap %p follows %lld size %llu\n", 1264 inode, capsnap, next_follows, capsnap->size); 1265 send_cap_msg(session, ceph_vino(inode).ino, 0, 1266 CEPH_CAP_OP_FLUSHSNAP, capsnap->issued, 0, 1267 capsnap->dirty, 0, capsnap->flush_tid, 0, mseq, 1268 capsnap->size, 0, 1269 &capsnap->mtime, &capsnap->atime, 1270 capsnap->time_warp_seq, 1271 capsnap->uid, capsnap->gid, capsnap->mode, 1272 0, NULL, 1273 capsnap->follows); 1274 1275 next_follows = capsnap->follows + 1; 1276 ceph_put_cap_snap(capsnap); 1277 1278 spin_lock(&inode->i_lock); 1279 goto retry; 1280 } 1281 1282 /* we flushed them all; remove this inode from the queue */ 1283 spin_lock(&mdsc->snap_flush_lock); 1284 list_del_init(&ci->i_snap_flush_item); 1285 spin_unlock(&mdsc->snap_flush_lock); 1286 1287 if (psession) 1288 *psession = session; 1289 else if (session) { 1290 mutex_unlock(&session->s_mutex); 1291 ceph_put_mds_session(session); 1292 } 1293 } 1294 1295 static void ceph_flush_snaps(struct ceph_inode_info *ci) 1296 { 1297 struct inode *inode = &ci->vfs_inode; 1298 1299 spin_lock(&inode->i_lock); 1300 __ceph_flush_snaps(ci, NULL); 1301 spin_unlock(&inode->i_lock); 1302 } 1303 1304 /* 1305 * Mark caps dirty. If inode is newly dirty, add to the global dirty 1306 * list. 1307 */ 1308 void __ceph_mark_dirty_caps(struct ceph_inode_info *ci, int mask) 1309 { 1310 struct ceph_mds_client *mdsc = 1311 &ceph_sb_to_client(ci->vfs_inode.i_sb)->mdsc; 1312 struct inode *inode = &ci->vfs_inode; 1313 int was = ci->i_dirty_caps; 1314 int dirty = 0; 1315 1316 dout("__mark_dirty_caps %p %s dirty %s -> %s\n", &ci->vfs_inode, 1317 ceph_cap_string(mask), ceph_cap_string(was), 1318 ceph_cap_string(was | mask)); 1319 ci->i_dirty_caps |= mask; 1320 if (was == 0) { 1321 dout(" inode %p now dirty\n", &ci->vfs_inode); 1322 BUG_ON(!list_empty(&ci->i_dirty_item)); 1323 spin_lock(&mdsc->cap_dirty_lock); 1324 list_add(&ci->i_dirty_item, &mdsc->cap_dirty); 1325 spin_unlock(&mdsc->cap_dirty_lock); 1326 if (ci->i_flushing_caps == 0) { 1327 igrab(inode); 1328 dirty |= I_DIRTY_SYNC; 1329 } 1330 } 1331 BUG_ON(list_empty(&ci->i_dirty_item)); 1332 if (((was | ci->i_flushing_caps) & CEPH_CAP_FILE_BUFFER) && 1333 (mask & CEPH_CAP_FILE_BUFFER)) 1334 dirty |= I_DIRTY_DATASYNC; 1335 if (dirty) 1336 __mark_inode_dirty(inode, dirty); 1337 __cap_delay_requeue(mdsc, ci); 1338 } 1339 1340 /* 1341 * Add dirty inode to the flushing list. Assigned a seq number so we 1342 * can wait for caps to flush without starving. 1343 * 1344 * Called under i_lock. 1345 */ 1346 static int __mark_caps_flushing(struct inode *inode, 1347 struct ceph_mds_session *session) 1348 { 1349 struct ceph_mds_client *mdsc = &ceph_sb_to_client(inode->i_sb)->mdsc; 1350 struct ceph_inode_info *ci = ceph_inode(inode); 1351 int flushing; 1352 1353 BUG_ON(ci->i_dirty_caps == 0); 1354 BUG_ON(list_empty(&ci->i_dirty_item)); 1355 1356 flushing = ci->i_dirty_caps; 1357 dout("__mark_caps_flushing flushing %s, flushing_caps %s -> %s\n", 1358 ceph_cap_string(flushing), 1359 ceph_cap_string(ci->i_flushing_caps), 1360 ceph_cap_string(ci->i_flushing_caps | flushing)); 1361 ci->i_flushing_caps |= flushing; 1362 ci->i_dirty_caps = 0; 1363 dout(" inode %p now !dirty\n", inode); 1364 1365 spin_lock(&mdsc->cap_dirty_lock); 1366 list_del_init(&ci->i_dirty_item); 1367 1368 ci->i_cap_flush_seq = ++mdsc->cap_flush_seq; 1369 if (list_empty(&ci->i_flushing_item)) { 1370 list_add_tail(&ci->i_flushing_item, &session->s_cap_flushing); 1371 mdsc->num_cap_flushing++; 1372 dout(" inode %p now flushing seq %lld\n", inode, 1373 ci->i_cap_flush_seq); 1374 } else { 1375 list_move_tail(&ci->i_flushing_item, &session->s_cap_flushing); 1376 dout(" inode %p now flushing (more) seq %lld\n", inode, 1377 ci->i_cap_flush_seq); 1378 } 1379 spin_unlock(&mdsc->cap_dirty_lock); 1380 1381 return flushing; 1382 } 1383 1384 /* 1385 * try to invalidate mapping pages without blocking. 1386 */ 1387 static int mapping_is_empty(struct address_space *mapping) 1388 { 1389 struct page *page = find_get_page(mapping, 0); 1390 1391 if (!page) 1392 return 1; 1393 1394 put_page(page); 1395 return 0; 1396 } 1397 1398 static int try_nonblocking_invalidate(struct inode *inode) 1399 { 1400 struct ceph_inode_info *ci = ceph_inode(inode); 1401 u32 invalidating_gen = ci->i_rdcache_gen; 1402 1403 spin_unlock(&inode->i_lock); 1404 invalidate_mapping_pages(&inode->i_data, 0, -1); 1405 spin_lock(&inode->i_lock); 1406 1407 if (mapping_is_empty(&inode->i_data) && 1408 invalidating_gen == ci->i_rdcache_gen) { 1409 /* success. */ 1410 dout("try_nonblocking_invalidate %p success\n", inode); 1411 ci->i_rdcache_gen = 0; 1412 ci->i_rdcache_revoking = 0; 1413 return 0; 1414 } 1415 dout("try_nonblocking_invalidate %p failed\n", inode); 1416 return -1; 1417 } 1418 1419 /* 1420 * Swiss army knife function to examine currently used and wanted 1421 * versus held caps. Release, flush, ack revoked caps to mds as 1422 * appropriate. 1423 * 1424 * CHECK_CAPS_NODELAY - caller is delayed work and we should not delay 1425 * cap release further. 1426 * CHECK_CAPS_AUTHONLY - we should only check the auth cap 1427 * CHECK_CAPS_FLUSH - we should flush any dirty caps immediately, without 1428 * further delay. 1429 */ 1430 void ceph_check_caps(struct ceph_inode_info *ci, int flags, 1431 struct ceph_mds_session *session) 1432 __releases(session->s_mutex) 1433 { 1434 struct ceph_client *client = ceph_inode_to_client(&ci->vfs_inode); 1435 struct ceph_mds_client *mdsc = &client->mdsc; 1436 struct inode *inode = &ci->vfs_inode; 1437 struct ceph_cap *cap; 1438 int file_wanted, used; 1439 int took_snap_rwsem = 0; /* true if mdsc->snap_rwsem held */ 1440 int issued, implemented, want, retain, revoking, flushing = 0; 1441 int mds = -1; /* keep track of how far we've gone through i_caps list 1442 to avoid an infinite loop on retry */ 1443 struct rb_node *p; 1444 int tried_invalidate = 0; 1445 int delayed = 0, sent = 0, force_requeue = 0, num; 1446 int queue_invalidate = 0; 1447 int is_delayed = flags & CHECK_CAPS_NODELAY; 1448 1449 /* if we are unmounting, flush any unused caps immediately. */ 1450 if (mdsc->stopping) 1451 is_delayed = 1; 1452 1453 spin_lock(&inode->i_lock); 1454 1455 if (ci->i_ceph_flags & CEPH_I_FLUSH) 1456 flags |= CHECK_CAPS_FLUSH; 1457 1458 /* flush snaps first time around only */ 1459 if (!list_empty(&ci->i_cap_snaps)) 1460 __ceph_flush_snaps(ci, &session); 1461 goto retry_locked; 1462 retry: 1463 spin_lock(&inode->i_lock); 1464 retry_locked: 1465 file_wanted = __ceph_caps_file_wanted(ci); 1466 used = __ceph_caps_used(ci); 1467 want = file_wanted | used; 1468 issued = __ceph_caps_issued(ci, &implemented); 1469 revoking = implemented & ~issued; 1470 1471 retain = want | CEPH_CAP_PIN; 1472 if (!mdsc->stopping && inode->i_nlink > 0) { 1473 if (want) { 1474 retain |= CEPH_CAP_ANY; /* be greedy */ 1475 } else { 1476 retain |= CEPH_CAP_ANY_SHARED; 1477 /* 1478 * keep RD only if we didn't have the file open RW, 1479 * because then the mds would revoke it anyway to 1480 * journal max_size=0. 1481 */ 1482 if (ci->i_max_size == 0) 1483 retain |= CEPH_CAP_ANY_RD; 1484 } 1485 } 1486 1487 dout("check_caps %p file_want %s used %s dirty %s flushing %s" 1488 " issued %s revoking %s retain %s %s%s%s\n", inode, 1489 ceph_cap_string(file_wanted), 1490 ceph_cap_string(used), ceph_cap_string(ci->i_dirty_caps), 1491 ceph_cap_string(ci->i_flushing_caps), 1492 ceph_cap_string(issued), ceph_cap_string(revoking), 1493 ceph_cap_string(retain), 1494 (flags & CHECK_CAPS_AUTHONLY) ? " AUTHONLY" : "", 1495 (flags & CHECK_CAPS_NODELAY) ? " NODELAY" : "", 1496 (flags & CHECK_CAPS_FLUSH) ? " FLUSH" : ""); 1497 1498 /* 1499 * If we no longer need to hold onto old our caps, and we may 1500 * have cached pages, but don't want them, then try to invalidate. 1501 * If we fail, it's because pages are locked.... try again later. 1502 */ 1503 if ((!is_delayed || mdsc->stopping) && 1504 ci->i_wrbuffer_ref == 0 && /* no dirty pages... */ 1505 ci->i_rdcache_gen && /* may have cached pages */ 1506 (file_wanted == 0 || /* no open files */ 1507 (revoking & CEPH_CAP_FILE_CACHE)) && /* or revoking cache */ 1508 !tried_invalidate) { 1509 dout("check_caps trying to invalidate on %p\n", inode); 1510 if (try_nonblocking_invalidate(inode) < 0) { 1511 if (revoking & CEPH_CAP_FILE_CACHE) { 1512 dout("check_caps queuing invalidate\n"); 1513 queue_invalidate = 1; 1514 ci->i_rdcache_revoking = ci->i_rdcache_gen; 1515 } else { 1516 dout("check_caps failed to invalidate pages\n"); 1517 /* we failed to invalidate pages. check these 1518 caps again later. */ 1519 force_requeue = 1; 1520 __cap_set_timeouts(mdsc, ci); 1521 } 1522 } 1523 tried_invalidate = 1; 1524 goto retry_locked; 1525 } 1526 1527 num = 0; 1528 for (p = rb_first(&ci->i_caps); p; p = rb_next(p)) { 1529 cap = rb_entry(p, struct ceph_cap, ci_node); 1530 num++; 1531 1532 /* avoid looping forever */ 1533 if (mds >= cap->mds || 1534 ((flags & CHECK_CAPS_AUTHONLY) && cap != ci->i_auth_cap)) 1535 continue; 1536 1537 /* NOTE: no side-effects allowed, until we take s_mutex */ 1538 1539 revoking = cap->implemented & ~cap->issued; 1540 if (revoking) 1541 dout(" mds%d revoking %s\n", cap->mds, 1542 ceph_cap_string(revoking)); 1543 1544 if (cap == ci->i_auth_cap && 1545 (cap->issued & CEPH_CAP_FILE_WR)) { 1546 /* request larger max_size from MDS? */ 1547 if (ci->i_wanted_max_size > ci->i_max_size && 1548 ci->i_wanted_max_size > ci->i_requested_max_size) { 1549 dout("requesting new max_size\n"); 1550 goto ack; 1551 } 1552 1553 /* approaching file_max? */ 1554 if ((inode->i_size << 1) >= ci->i_max_size && 1555 (ci->i_reported_size << 1) < ci->i_max_size) { 1556 dout("i_size approaching max_size\n"); 1557 goto ack; 1558 } 1559 } 1560 /* flush anything dirty? */ 1561 if (cap == ci->i_auth_cap && (flags & CHECK_CAPS_FLUSH) && 1562 ci->i_dirty_caps) { 1563 dout("flushing dirty caps\n"); 1564 goto ack; 1565 } 1566 1567 /* completed revocation? going down and there are no caps? */ 1568 if (revoking && (revoking & used) == 0) { 1569 dout("completed revocation of %s\n", 1570 ceph_cap_string(cap->implemented & ~cap->issued)); 1571 goto ack; 1572 } 1573 1574 /* want more caps from mds? */ 1575 if (want & ~(cap->mds_wanted | cap->issued)) 1576 goto ack; 1577 1578 /* things we might delay */ 1579 if ((cap->issued & ~retain) == 0 && 1580 cap->mds_wanted == want) 1581 continue; /* nope, all good */ 1582 1583 if (is_delayed) 1584 goto ack; 1585 1586 /* delay? */ 1587 if ((ci->i_ceph_flags & CEPH_I_NODELAY) == 0 && 1588 time_before(jiffies, ci->i_hold_caps_max)) { 1589 dout(" delaying issued %s -> %s, wanted %s -> %s\n", 1590 ceph_cap_string(cap->issued), 1591 ceph_cap_string(cap->issued & retain), 1592 ceph_cap_string(cap->mds_wanted), 1593 ceph_cap_string(want)); 1594 delayed++; 1595 continue; 1596 } 1597 1598 ack: 1599 if (ci->i_ceph_flags & CEPH_I_NOFLUSH) { 1600 dout(" skipping %p I_NOFLUSH set\n", inode); 1601 continue; 1602 } 1603 1604 if (session && session != cap->session) { 1605 dout("oops, wrong session %p mutex\n", session); 1606 mutex_unlock(&session->s_mutex); 1607 session = NULL; 1608 } 1609 if (!session) { 1610 session = cap->session; 1611 if (mutex_trylock(&session->s_mutex) == 0) { 1612 dout("inverting session/ino locks on %p\n", 1613 session); 1614 spin_unlock(&inode->i_lock); 1615 if (took_snap_rwsem) { 1616 up_read(&mdsc->snap_rwsem); 1617 took_snap_rwsem = 0; 1618 } 1619 mutex_lock(&session->s_mutex); 1620 goto retry; 1621 } 1622 } 1623 /* take snap_rwsem after session mutex */ 1624 if (!took_snap_rwsem) { 1625 if (down_read_trylock(&mdsc->snap_rwsem) == 0) { 1626 dout("inverting snap/in locks on %p\n", 1627 inode); 1628 spin_unlock(&inode->i_lock); 1629 down_read(&mdsc->snap_rwsem); 1630 took_snap_rwsem = 1; 1631 goto retry; 1632 } 1633 took_snap_rwsem = 1; 1634 } 1635 1636 if (cap == ci->i_auth_cap && ci->i_dirty_caps) 1637 flushing = __mark_caps_flushing(inode, session); 1638 1639 mds = cap->mds; /* remember mds, so we don't repeat */ 1640 sent++; 1641 1642 /* __send_cap drops i_lock */ 1643 delayed += __send_cap(mdsc, cap, CEPH_CAP_OP_UPDATE, used, want, 1644 retain, flushing, NULL); 1645 goto retry; /* retake i_lock and restart our cap scan. */ 1646 } 1647 1648 /* 1649 * Reschedule delayed caps release if we delayed anything, 1650 * otherwise cancel. 1651 */ 1652 if (delayed && is_delayed) 1653 force_requeue = 1; /* __send_cap delayed release; requeue */ 1654 if (!delayed && !is_delayed) 1655 __cap_delay_cancel(mdsc, ci); 1656 else if (!is_delayed || force_requeue) 1657 __cap_delay_requeue(mdsc, ci); 1658 1659 spin_unlock(&inode->i_lock); 1660 1661 if (queue_invalidate) 1662 ceph_queue_invalidate(inode); 1663 1664 if (session) 1665 mutex_unlock(&session->s_mutex); 1666 if (took_snap_rwsem) 1667 up_read(&mdsc->snap_rwsem); 1668 } 1669 1670 /* 1671 * Try to flush dirty caps back to the auth mds. 1672 */ 1673 static int try_flush_caps(struct inode *inode, struct ceph_mds_session *session, 1674 unsigned *flush_tid) 1675 { 1676 struct ceph_mds_client *mdsc = &ceph_sb_to_client(inode->i_sb)->mdsc; 1677 struct ceph_inode_info *ci = ceph_inode(inode); 1678 int unlock_session = session ? 0 : 1; 1679 int flushing = 0; 1680 1681 retry: 1682 spin_lock(&inode->i_lock); 1683 if (ci->i_ceph_flags & CEPH_I_NOFLUSH) { 1684 dout("try_flush_caps skipping %p I_NOFLUSH set\n", inode); 1685 goto out; 1686 } 1687 if (ci->i_dirty_caps && ci->i_auth_cap) { 1688 struct ceph_cap *cap = ci->i_auth_cap; 1689 int used = __ceph_caps_used(ci); 1690 int want = __ceph_caps_wanted(ci); 1691 int delayed; 1692 1693 if (!session) { 1694 spin_unlock(&inode->i_lock); 1695 session = cap->session; 1696 mutex_lock(&session->s_mutex); 1697 goto retry; 1698 } 1699 BUG_ON(session != cap->session); 1700 if (cap->session->s_state < CEPH_MDS_SESSION_OPEN) 1701 goto out; 1702 1703 flushing = __mark_caps_flushing(inode, session); 1704 1705 /* __send_cap drops i_lock */ 1706 delayed = __send_cap(mdsc, cap, CEPH_CAP_OP_FLUSH, used, want, 1707 cap->issued | cap->implemented, flushing, 1708 flush_tid); 1709 if (!delayed) 1710 goto out_unlocked; 1711 1712 spin_lock(&inode->i_lock); 1713 __cap_delay_requeue(mdsc, ci); 1714 } 1715 out: 1716 spin_unlock(&inode->i_lock); 1717 out_unlocked: 1718 if (session && unlock_session) 1719 mutex_unlock(&session->s_mutex); 1720 return flushing; 1721 } 1722 1723 /* 1724 * Return true if we've flushed caps through the given flush_tid. 1725 */ 1726 static int caps_are_flushed(struct inode *inode, unsigned tid) 1727 { 1728 struct ceph_inode_info *ci = ceph_inode(inode); 1729 int i, ret = 1; 1730 1731 spin_lock(&inode->i_lock); 1732 for (i = 0; i < CEPH_CAP_BITS; i++) 1733 if ((ci->i_flushing_caps & (1 << i)) && 1734 ci->i_cap_flush_tid[i] <= tid) { 1735 /* still flushing this bit */ 1736 ret = 0; 1737 break; 1738 } 1739 spin_unlock(&inode->i_lock); 1740 return ret; 1741 } 1742 1743 /* 1744 * Wait on any unsafe replies for the given inode. First wait on the 1745 * newest request, and make that the upper bound. Then, if there are 1746 * more requests, keep waiting on the oldest as long as it is still older 1747 * than the original request. 1748 */ 1749 static void sync_write_wait(struct inode *inode) 1750 { 1751 struct ceph_inode_info *ci = ceph_inode(inode); 1752 struct list_head *head = &ci->i_unsafe_writes; 1753 struct ceph_osd_request *req; 1754 u64 last_tid; 1755 1756 spin_lock(&ci->i_unsafe_lock); 1757 if (list_empty(head)) 1758 goto out; 1759 1760 /* set upper bound as _last_ entry in chain */ 1761 req = list_entry(head->prev, struct ceph_osd_request, 1762 r_unsafe_item); 1763 last_tid = req->r_tid; 1764 1765 do { 1766 ceph_osdc_get_request(req); 1767 spin_unlock(&ci->i_unsafe_lock); 1768 dout("sync_write_wait on tid %llu (until %llu)\n", 1769 req->r_tid, last_tid); 1770 wait_for_completion(&req->r_safe_completion); 1771 spin_lock(&ci->i_unsafe_lock); 1772 ceph_osdc_put_request(req); 1773 1774 /* 1775 * from here on look at first entry in chain, since we 1776 * only want to wait for anything older than last_tid 1777 */ 1778 if (list_empty(head)) 1779 break; 1780 req = list_entry(head->next, struct ceph_osd_request, 1781 r_unsafe_item); 1782 } while (req->r_tid < last_tid); 1783 out: 1784 spin_unlock(&ci->i_unsafe_lock); 1785 } 1786 1787 int ceph_fsync(struct file *file, int datasync) 1788 { 1789 struct inode *inode = file->f_mapping->host; 1790 struct ceph_inode_info *ci = ceph_inode(inode); 1791 unsigned flush_tid; 1792 int ret; 1793 int dirty; 1794 1795 dout("fsync %p%s\n", inode, datasync ? " datasync" : ""); 1796 sync_write_wait(inode); 1797 1798 ret = filemap_write_and_wait(inode->i_mapping); 1799 if (ret < 0) 1800 return ret; 1801 1802 dirty = try_flush_caps(inode, NULL, &flush_tid); 1803 dout("fsync dirty caps are %s\n", ceph_cap_string(dirty)); 1804 1805 /* 1806 * only wait on non-file metadata writeback (the mds 1807 * can recover size and mtime, so we don't need to 1808 * wait for that) 1809 */ 1810 if (!datasync && (dirty & ~CEPH_CAP_ANY_FILE_WR)) { 1811 dout("fsync waiting for flush_tid %u\n", flush_tid); 1812 ret = wait_event_interruptible(ci->i_cap_wq, 1813 caps_are_flushed(inode, flush_tid)); 1814 } 1815 1816 dout("fsync %p%s done\n", inode, datasync ? " datasync" : ""); 1817 return ret; 1818 } 1819 1820 /* 1821 * Flush any dirty caps back to the mds. If we aren't asked to wait, 1822 * queue inode for flush but don't do so immediately, because we can 1823 * get by with fewer MDS messages if we wait for data writeback to 1824 * complete first. 1825 */ 1826 int ceph_write_inode(struct inode *inode, struct writeback_control *wbc) 1827 { 1828 struct ceph_inode_info *ci = ceph_inode(inode); 1829 unsigned flush_tid; 1830 int err = 0; 1831 int dirty; 1832 int wait = wbc->sync_mode == WB_SYNC_ALL; 1833 1834 dout("write_inode %p wait=%d\n", inode, wait); 1835 if (wait) { 1836 dirty = try_flush_caps(inode, NULL, &flush_tid); 1837 if (dirty) 1838 err = wait_event_interruptible(ci->i_cap_wq, 1839 caps_are_flushed(inode, flush_tid)); 1840 } else { 1841 struct ceph_mds_client *mdsc = 1842 &ceph_sb_to_client(inode->i_sb)->mdsc; 1843 1844 spin_lock(&inode->i_lock); 1845 if (__ceph_caps_dirty(ci)) 1846 __cap_delay_requeue_front(mdsc, ci); 1847 spin_unlock(&inode->i_lock); 1848 } 1849 return err; 1850 } 1851 1852 /* 1853 * After a recovering MDS goes active, we need to resend any caps 1854 * we were flushing. 1855 * 1856 * Caller holds session->s_mutex. 1857 */ 1858 static void kick_flushing_capsnaps(struct ceph_mds_client *mdsc, 1859 struct ceph_mds_session *session) 1860 { 1861 struct ceph_cap_snap *capsnap; 1862 1863 dout("kick_flushing_capsnaps mds%d\n", session->s_mds); 1864 list_for_each_entry(capsnap, &session->s_cap_snaps_flushing, 1865 flushing_item) { 1866 struct ceph_inode_info *ci = capsnap->ci; 1867 struct inode *inode = &ci->vfs_inode; 1868 struct ceph_cap *cap; 1869 1870 spin_lock(&inode->i_lock); 1871 cap = ci->i_auth_cap; 1872 if (cap && cap->session == session) { 1873 dout("kick_flushing_caps %p cap %p capsnap %p\n", inode, 1874 cap, capsnap); 1875 __ceph_flush_snaps(ci, &session); 1876 } else { 1877 pr_err("%p auth cap %p not mds%d ???\n", inode, 1878 cap, session->s_mds); 1879 } 1880 spin_unlock(&inode->i_lock); 1881 } 1882 } 1883 1884 void ceph_kick_flushing_caps(struct ceph_mds_client *mdsc, 1885 struct ceph_mds_session *session) 1886 { 1887 struct ceph_inode_info *ci; 1888 1889 kick_flushing_capsnaps(mdsc, session); 1890 1891 dout("kick_flushing_caps mds%d\n", session->s_mds); 1892 list_for_each_entry(ci, &session->s_cap_flushing, i_flushing_item) { 1893 struct inode *inode = &ci->vfs_inode; 1894 struct ceph_cap *cap; 1895 int delayed = 0; 1896 1897 spin_lock(&inode->i_lock); 1898 cap = ci->i_auth_cap; 1899 if (cap && cap->session == session) { 1900 dout("kick_flushing_caps %p cap %p %s\n", inode, 1901 cap, ceph_cap_string(ci->i_flushing_caps)); 1902 delayed = __send_cap(mdsc, cap, CEPH_CAP_OP_FLUSH, 1903 __ceph_caps_used(ci), 1904 __ceph_caps_wanted(ci), 1905 cap->issued | cap->implemented, 1906 ci->i_flushing_caps, NULL); 1907 if (delayed) { 1908 spin_lock(&inode->i_lock); 1909 __cap_delay_requeue(mdsc, ci); 1910 spin_unlock(&inode->i_lock); 1911 } 1912 } else { 1913 pr_err("%p auth cap %p not mds%d ???\n", inode, 1914 cap, session->s_mds); 1915 spin_unlock(&inode->i_lock); 1916 } 1917 } 1918 } 1919 1920 1921 /* 1922 * Take references to capabilities we hold, so that we don't release 1923 * them to the MDS prematurely. 1924 * 1925 * Protected by i_lock. 1926 */ 1927 static void __take_cap_refs(struct ceph_inode_info *ci, int got) 1928 { 1929 if (got & CEPH_CAP_PIN) 1930 ci->i_pin_ref++; 1931 if (got & CEPH_CAP_FILE_RD) 1932 ci->i_rd_ref++; 1933 if (got & CEPH_CAP_FILE_CACHE) 1934 ci->i_rdcache_ref++; 1935 if (got & CEPH_CAP_FILE_WR) 1936 ci->i_wr_ref++; 1937 if (got & CEPH_CAP_FILE_BUFFER) { 1938 if (ci->i_wrbuffer_ref == 0) 1939 igrab(&ci->vfs_inode); 1940 ci->i_wrbuffer_ref++; 1941 dout("__take_cap_refs %p wrbuffer %d -> %d (?)\n", 1942 &ci->vfs_inode, ci->i_wrbuffer_ref-1, ci->i_wrbuffer_ref); 1943 } 1944 } 1945 1946 /* 1947 * Try to grab cap references. Specify those refs we @want, and the 1948 * minimal set we @need. Also include the larger offset we are writing 1949 * to (when applicable), and check against max_size here as well. 1950 * Note that caller is responsible for ensuring max_size increases are 1951 * requested from the MDS. 1952 */ 1953 static int try_get_cap_refs(struct ceph_inode_info *ci, int need, int want, 1954 int *got, loff_t endoff, int *check_max, int *err) 1955 { 1956 struct inode *inode = &ci->vfs_inode; 1957 int ret = 0; 1958 int have, implemented; 1959 int file_wanted; 1960 1961 dout("get_cap_refs %p need %s want %s\n", inode, 1962 ceph_cap_string(need), ceph_cap_string(want)); 1963 spin_lock(&inode->i_lock); 1964 1965 /* make sure file is actually open */ 1966 file_wanted = __ceph_caps_file_wanted(ci); 1967 if ((file_wanted & need) == 0) { 1968 dout("try_get_cap_refs need %s file_wanted %s, EBADF\n", 1969 ceph_cap_string(need), ceph_cap_string(file_wanted)); 1970 *err = -EBADF; 1971 ret = 1; 1972 goto out; 1973 } 1974 1975 if (need & CEPH_CAP_FILE_WR) { 1976 if (endoff >= 0 && endoff > (loff_t)ci->i_max_size) { 1977 dout("get_cap_refs %p endoff %llu > maxsize %llu\n", 1978 inode, endoff, ci->i_max_size); 1979 if (endoff > ci->i_wanted_max_size) { 1980 *check_max = 1; 1981 ret = 1; 1982 } 1983 goto out; 1984 } 1985 /* 1986 * If a sync write is in progress, we must wait, so that we 1987 * can get a final snapshot value for size+mtime. 1988 */ 1989 if (__ceph_have_pending_cap_snap(ci)) { 1990 dout("get_cap_refs %p cap_snap_pending\n", inode); 1991 goto out; 1992 } 1993 } 1994 have = __ceph_caps_issued(ci, &implemented); 1995 1996 /* 1997 * disallow writes while a truncate is pending 1998 */ 1999 if (ci->i_truncate_pending) 2000 have &= ~CEPH_CAP_FILE_WR; 2001 2002 if ((have & need) == need) { 2003 /* 2004 * Look at (implemented & ~have & not) so that we keep waiting 2005 * on transition from wanted -> needed caps. This is needed 2006 * for WRBUFFER|WR -> WR to avoid a new WR sync write from 2007 * going before a prior buffered writeback happens. 2008 */ 2009 int not = want & ~(have & need); 2010 int revoking = implemented & ~have; 2011 dout("get_cap_refs %p have %s but not %s (revoking %s)\n", 2012 inode, ceph_cap_string(have), ceph_cap_string(not), 2013 ceph_cap_string(revoking)); 2014 if ((revoking & not) == 0) { 2015 *got = need | (have & want); 2016 __take_cap_refs(ci, *got); 2017 ret = 1; 2018 } 2019 } else { 2020 dout("get_cap_refs %p have %s needed %s\n", inode, 2021 ceph_cap_string(have), ceph_cap_string(need)); 2022 } 2023 out: 2024 spin_unlock(&inode->i_lock); 2025 dout("get_cap_refs %p ret %d got %s\n", inode, 2026 ret, ceph_cap_string(*got)); 2027 return ret; 2028 } 2029 2030 /* 2031 * Check the offset we are writing up to against our current 2032 * max_size. If necessary, tell the MDS we want to write to 2033 * a larger offset. 2034 */ 2035 static void check_max_size(struct inode *inode, loff_t endoff) 2036 { 2037 struct ceph_inode_info *ci = ceph_inode(inode); 2038 int check = 0; 2039 2040 /* do we need to explicitly request a larger max_size? */ 2041 spin_lock(&inode->i_lock); 2042 if ((endoff >= ci->i_max_size || 2043 endoff > (inode->i_size << 1)) && 2044 endoff > ci->i_wanted_max_size) { 2045 dout("write %p at large endoff %llu, req max_size\n", 2046 inode, endoff); 2047 ci->i_wanted_max_size = endoff; 2048 check = 1; 2049 } 2050 spin_unlock(&inode->i_lock); 2051 if (check) 2052 ceph_check_caps(ci, CHECK_CAPS_AUTHONLY, NULL); 2053 } 2054 2055 /* 2056 * Wait for caps, and take cap references. If we can't get a WR cap 2057 * due to a small max_size, make sure we check_max_size (and possibly 2058 * ask the mds) so we don't get hung up indefinitely. 2059 */ 2060 int ceph_get_caps(struct ceph_inode_info *ci, int need, int want, int *got, 2061 loff_t endoff) 2062 { 2063 int check_max, ret, err; 2064 2065 retry: 2066 if (endoff > 0) 2067 check_max_size(&ci->vfs_inode, endoff); 2068 check_max = 0; 2069 err = 0; 2070 ret = wait_event_interruptible(ci->i_cap_wq, 2071 try_get_cap_refs(ci, need, want, 2072 got, endoff, 2073 &check_max, &err)); 2074 if (err) 2075 ret = err; 2076 if (check_max) 2077 goto retry; 2078 return ret; 2079 } 2080 2081 /* 2082 * Take cap refs. Caller must already know we hold at least one ref 2083 * on the caps in question or we don't know this is safe. 2084 */ 2085 void ceph_get_cap_refs(struct ceph_inode_info *ci, int caps) 2086 { 2087 spin_lock(&ci->vfs_inode.i_lock); 2088 __take_cap_refs(ci, caps); 2089 spin_unlock(&ci->vfs_inode.i_lock); 2090 } 2091 2092 /* 2093 * Release cap refs. 2094 * 2095 * If we released the last ref on any given cap, call ceph_check_caps 2096 * to release (or schedule a release). 2097 * 2098 * If we are releasing a WR cap (from a sync write), finalize any affected 2099 * cap_snap, and wake up any waiters. 2100 */ 2101 void ceph_put_cap_refs(struct ceph_inode_info *ci, int had) 2102 { 2103 struct inode *inode = &ci->vfs_inode; 2104 int last = 0, put = 0, flushsnaps = 0, wake = 0; 2105 struct ceph_cap_snap *capsnap; 2106 2107 spin_lock(&inode->i_lock); 2108 if (had & CEPH_CAP_PIN) 2109 --ci->i_pin_ref; 2110 if (had & CEPH_CAP_FILE_RD) 2111 if (--ci->i_rd_ref == 0) 2112 last++; 2113 if (had & CEPH_CAP_FILE_CACHE) 2114 if (--ci->i_rdcache_ref == 0) 2115 last++; 2116 if (had & CEPH_CAP_FILE_BUFFER) { 2117 if (--ci->i_wrbuffer_ref == 0) { 2118 last++; 2119 put++; 2120 } 2121 dout("put_cap_refs %p wrbuffer %d -> %d (?)\n", 2122 inode, ci->i_wrbuffer_ref+1, ci->i_wrbuffer_ref); 2123 } 2124 if (had & CEPH_CAP_FILE_WR) 2125 if (--ci->i_wr_ref == 0) { 2126 last++; 2127 if (!list_empty(&ci->i_cap_snaps)) { 2128 capsnap = list_first_entry(&ci->i_cap_snaps, 2129 struct ceph_cap_snap, 2130 ci_item); 2131 if (capsnap->writing) { 2132 capsnap->writing = 0; 2133 flushsnaps = 2134 __ceph_finish_cap_snap(ci, 2135 capsnap); 2136 wake = 1; 2137 } 2138 } 2139 } 2140 spin_unlock(&inode->i_lock); 2141 2142 dout("put_cap_refs %p had %s%s%s\n", inode, ceph_cap_string(had), 2143 last ? " last" : "", put ? " put" : ""); 2144 2145 if (last && !flushsnaps) 2146 ceph_check_caps(ci, 0, NULL); 2147 else if (flushsnaps) 2148 ceph_flush_snaps(ci); 2149 if (wake) 2150 wake_up(&ci->i_cap_wq); 2151 if (put) 2152 iput(inode); 2153 } 2154 2155 /* 2156 * Release @nr WRBUFFER refs on dirty pages for the given @snapc snap 2157 * context. Adjust per-snap dirty page accounting as appropriate. 2158 * Once all dirty data for a cap_snap is flushed, flush snapped file 2159 * metadata back to the MDS. If we dropped the last ref, call 2160 * ceph_check_caps. 2161 */ 2162 void ceph_put_wrbuffer_cap_refs(struct ceph_inode_info *ci, int nr, 2163 struct ceph_snap_context *snapc) 2164 { 2165 struct inode *inode = &ci->vfs_inode; 2166 int last = 0; 2167 int complete_capsnap = 0; 2168 int drop_capsnap = 0; 2169 int found = 0; 2170 struct ceph_cap_snap *capsnap = NULL; 2171 2172 spin_lock(&inode->i_lock); 2173 ci->i_wrbuffer_ref -= nr; 2174 last = !ci->i_wrbuffer_ref; 2175 2176 if (ci->i_head_snapc == snapc) { 2177 ci->i_wrbuffer_ref_head -= nr; 2178 if (!ci->i_wrbuffer_ref_head) { 2179 ceph_put_snap_context(ci->i_head_snapc); 2180 ci->i_head_snapc = NULL; 2181 } 2182 dout("put_wrbuffer_cap_refs on %p head %d/%d -> %d/%d %s\n", 2183 inode, 2184 ci->i_wrbuffer_ref+nr, ci->i_wrbuffer_ref_head+nr, 2185 ci->i_wrbuffer_ref, ci->i_wrbuffer_ref_head, 2186 last ? " LAST" : ""); 2187 } else { 2188 list_for_each_entry(capsnap, &ci->i_cap_snaps, ci_item) { 2189 if (capsnap->context == snapc) { 2190 found = 1; 2191 break; 2192 } 2193 } 2194 BUG_ON(!found); 2195 capsnap->dirty_pages -= nr; 2196 if (capsnap->dirty_pages == 0) { 2197 complete_capsnap = 1; 2198 if (capsnap->dirty == 0) 2199 /* cap writeback completed before we created 2200 * the cap_snap; no FLUSHSNAP is needed */ 2201 drop_capsnap = 1; 2202 } 2203 dout("put_wrbuffer_cap_refs on %p cap_snap %p " 2204 " snap %lld %d/%d -> %d/%d %s%s%s\n", 2205 inode, capsnap, capsnap->context->seq, 2206 ci->i_wrbuffer_ref+nr, capsnap->dirty_pages + nr, 2207 ci->i_wrbuffer_ref, capsnap->dirty_pages, 2208 last ? " (wrbuffer last)" : "", 2209 complete_capsnap ? " (complete capsnap)" : "", 2210 drop_capsnap ? " (drop capsnap)" : ""); 2211 if (drop_capsnap) { 2212 ceph_put_snap_context(capsnap->context); 2213 list_del(&capsnap->ci_item); 2214 list_del(&capsnap->flushing_item); 2215 ceph_put_cap_snap(capsnap); 2216 } 2217 } 2218 2219 spin_unlock(&inode->i_lock); 2220 2221 if (last) { 2222 ceph_check_caps(ci, CHECK_CAPS_AUTHONLY, NULL); 2223 iput(inode); 2224 } else if (complete_capsnap) { 2225 ceph_flush_snaps(ci); 2226 wake_up(&ci->i_cap_wq); 2227 } 2228 if (drop_capsnap) 2229 iput(inode); 2230 } 2231 2232 /* 2233 * Handle a cap GRANT message from the MDS. (Note that a GRANT may 2234 * actually be a revocation if it specifies a smaller cap set.) 2235 * 2236 * caller holds s_mutex and i_lock, we drop both. 2237 * 2238 * return value: 2239 * 0 - ok 2240 * 1 - check_caps on auth cap only (writeback) 2241 * 2 - check_caps (ack revoke) 2242 */ 2243 static void handle_cap_grant(struct inode *inode, struct ceph_mds_caps *grant, 2244 struct ceph_mds_session *session, 2245 struct ceph_cap *cap, 2246 struct ceph_buffer *xattr_buf) 2247 __releases(inode->i_lock) 2248 __releases(session->s_mutex) 2249 { 2250 struct ceph_inode_info *ci = ceph_inode(inode); 2251 int mds = session->s_mds; 2252 int seq = le32_to_cpu(grant->seq); 2253 int newcaps = le32_to_cpu(grant->caps); 2254 int issued, implemented, used, wanted, dirty; 2255 u64 size = le64_to_cpu(grant->size); 2256 u64 max_size = le64_to_cpu(grant->max_size); 2257 struct timespec mtime, atime, ctime; 2258 int check_caps = 0; 2259 int wake = 0; 2260 int writeback = 0; 2261 int revoked_rdcache = 0; 2262 int queue_invalidate = 0; 2263 2264 dout("handle_cap_grant inode %p cap %p mds%d seq %d %s\n", 2265 inode, cap, mds, seq, ceph_cap_string(newcaps)); 2266 dout(" size %llu max_size %llu, i_size %llu\n", size, max_size, 2267 inode->i_size); 2268 2269 /* 2270 * If CACHE is being revoked, and we have no dirty buffers, 2271 * try to invalidate (once). (If there are dirty buffers, we 2272 * will invalidate _after_ writeback.) 2273 */ 2274 if (((cap->issued & ~newcaps) & CEPH_CAP_FILE_CACHE) && 2275 !ci->i_wrbuffer_ref) { 2276 if (try_nonblocking_invalidate(inode) == 0) { 2277 revoked_rdcache = 1; 2278 } else { 2279 /* there were locked pages.. invalidate later 2280 in a separate thread. */ 2281 if (ci->i_rdcache_revoking != ci->i_rdcache_gen) { 2282 queue_invalidate = 1; 2283 ci->i_rdcache_revoking = ci->i_rdcache_gen; 2284 } 2285 } 2286 } 2287 2288 /* side effects now are allowed */ 2289 2290 issued = __ceph_caps_issued(ci, &implemented); 2291 issued |= implemented | __ceph_caps_dirty(ci); 2292 2293 cap->cap_gen = session->s_cap_gen; 2294 2295 __check_cap_issue(ci, cap, newcaps); 2296 2297 if ((issued & CEPH_CAP_AUTH_EXCL) == 0) { 2298 inode->i_mode = le32_to_cpu(grant->mode); 2299 inode->i_uid = le32_to_cpu(grant->uid); 2300 inode->i_gid = le32_to_cpu(grant->gid); 2301 dout("%p mode 0%o uid.gid %d.%d\n", inode, inode->i_mode, 2302 inode->i_uid, inode->i_gid); 2303 } 2304 2305 if ((issued & CEPH_CAP_LINK_EXCL) == 0) 2306 inode->i_nlink = le32_to_cpu(grant->nlink); 2307 2308 if ((issued & CEPH_CAP_XATTR_EXCL) == 0 && grant->xattr_len) { 2309 int len = le32_to_cpu(grant->xattr_len); 2310 u64 version = le64_to_cpu(grant->xattr_version); 2311 2312 if (version > ci->i_xattrs.version) { 2313 dout(" got new xattrs v%llu on %p len %d\n", 2314 version, inode, len); 2315 if (ci->i_xattrs.blob) 2316 ceph_buffer_put(ci->i_xattrs.blob); 2317 ci->i_xattrs.blob = ceph_buffer_get(xattr_buf); 2318 ci->i_xattrs.version = version; 2319 } 2320 } 2321 2322 /* size/ctime/mtime/atime? */ 2323 ceph_fill_file_size(inode, issued, 2324 le32_to_cpu(grant->truncate_seq), 2325 le64_to_cpu(grant->truncate_size), size); 2326 ceph_decode_timespec(&mtime, &grant->mtime); 2327 ceph_decode_timespec(&atime, &grant->atime); 2328 ceph_decode_timespec(&ctime, &grant->ctime); 2329 ceph_fill_file_time(inode, issued, 2330 le32_to_cpu(grant->time_warp_seq), &ctime, &mtime, 2331 &atime); 2332 2333 /* max size increase? */ 2334 if (max_size != ci->i_max_size) { 2335 dout("max_size %lld -> %llu\n", ci->i_max_size, max_size); 2336 ci->i_max_size = max_size; 2337 if (max_size >= ci->i_wanted_max_size) { 2338 ci->i_wanted_max_size = 0; /* reset */ 2339 ci->i_requested_max_size = 0; 2340 } 2341 wake = 1; 2342 } 2343 2344 /* check cap bits */ 2345 wanted = __ceph_caps_wanted(ci); 2346 used = __ceph_caps_used(ci); 2347 dirty = __ceph_caps_dirty(ci); 2348 dout(" my wanted = %s, used = %s, dirty %s\n", 2349 ceph_cap_string(wanted), 2350 ceph_cap_string(used), 2351 ceph_cap_string(dirty)); 2352 if (wanted != le32_to_cpu(grant->wanted)) { 2353 dout("mds wanted %s -> %s\n", 2354 ceph_cap_string(le32_to_cpu(grant->wanted)), 2355 ceph_cap_string(wanted)); 2356 grant->wanted = cpu_to_le32(wanted); 2357 } 2358 2359 cap->seq = seq; 2360 2361 /* file layout may have changed */ 2362 ci->i_layout = grant->layout; 2363 2364 /* revocation, grant, or no-op? */ 2365 if (cap->issued & ~newcaps) { 2366 dout("revocation: %s -> %s\n", ceph_cap_string(cap->issued), 2367 ceph_cap_string(newcaps)); 2368 if ((used & ~newcaps) & CEPH_CAP_FILE_BUFFER) 2369 writeback = 1; /* will delay ack */ 2370 else if (dirty & ~newcaps) 2371 check_caps = 1; /* initiate writeback in check_caps */ 2372 else if (((used & ~newcaps) & CEPH_CAP_FILE_CACHE) == 0 || 2373 revoked_rdcache) 2374 check_caps = 2; /* send revoke ack in check_caps */ 2375 cap->issued = newcaps; 2376 cap->implemented |= newcaps; 2377 } else if (cap->issued == newcaps) { 2378 dout("caps unchanged: %s -> %s\n", 2379 ceph_cap_string(cap->issued), ceph_cap_string(newcaps)); 2380 } else { 2381 dout("grant: %s -> %s\n", ceph_cap_string(cap->issued), 2382 ceph_cap_string(newcaps)); 2383 cap->issued = newcaps; 2384 cap->implemented |= newcaps; /* add bits only, to 2385 * avoid stepping on a 2386 * pending revocation */ 2387 wake = 1; 2388 } 2389 BUG_ON(cap->issued & ~cap->implemented); 2390 2391 spin_unlock(&inode->i_lock); 2392 if (writeback) 2393 /* 2394 * queue inode for writeback: we can't actually call 2395 * filemap_write_and_wait, etc. from message handler 2396 * context. 2397 */ 2398 ceph_queue_writeback(inode); 2399 if (queue_invalidate) 2400 ceph_queue_invalidate(inode); 2401 if (wake) 2402 wake_up(&ci->i_cap_wq); 2403 2404 if (check_caps == 1) 2405 ceph_check_caps(ci, CHECK_CAPS_NODELAY|CHECK_CAPS_AUTHONLY, 2406 session); 2407 else if (check_caps == 2) 2408 ceph_check_caps(ci, CHECK_CAPS_NODELAY, session); 2409 else 2410 mutex_unlock(&session->s_mutex); 2411 } 2412 2413 /* 2414 * Handle FLUSH_ACK from MDS, indicating that metadata we sent to the 2415 * MDS has been safely committed. 2416 */ 2417 static void handle_cap_flush_ack(struct inode *inode, u64 flush_tid, 2418 struct ceph_mds_caps *m, 2419 struct ceph_mds_session *session, 2420 struct ceph_cap *cap) 2421 __releases(inode->i_lock) 2422 { 2423 struct ceph_inode_info *ci = ceph_inode(inode); 2424 struct ceph_mds_client *mdsc = &ceph_sb_to_client(inode->i_sb)->mdsc; 2425 unsigned seq = le32_to_cpu(m->seq); 2426 int dirty = le32_to_cpu(m->dirty); 2427 int cleaned = 0; 2428 int drop = 0; 2429 int i; 2430 2431 for (i = 0; i < CEPH_CAP_BITS; i++) 2432 if ((dirty & (1 << i)) && 2433 flush_tid == ci->i_cap_flush_tid[i]) 2434 cleaned |= 1 << i; 2435 2436 dout("handle_cap_flush_ack inode %p mds%d seq %d on %s cleaned %s," 2437 " flushing %s -> %s\n", 2438 inode, session->s_mds, seq, ceph_cap_string(dirty), 2439 ceph_cap_string(cleaned), ceph_cap_string(ci->i_flushing_caps), 2440 ceph_cap_string(ci->i_flushing_caps & ~cleaned)); 2441 2442 if (ci->i_flushing_caps == (ci->i_flushing_caps & ~cleaned)) 2443 goto out; 2444 2445 ci->i_flushing_caps &= ~cleaned; 2446 2447 spin_lock(&mdsc->cap_dirty_lock); 2448 if (ci->i_flushing_caps == 0) { 2449 list_del_init(&ci->i_flushing_item); 2450 if (!list_empty(&session->s_cap_flushing)) 2451 dout(" mds%d still flushing cap on %p\n", 2452 session->s_mds, 2453 &list_entry(session->s_cap_flushing.next, 2454 struct ceph_inode_info, 2455 i_flushing_item)->vfs_inode); 2456 mdsc->num_cap_flushing--; 2457 wake_up(&mdsc->cap_flushing_wq); 2458 dout(" inode %p now !flushing\n", inode); 2459 2460 if (ci->i_dirty_caps == 0) { 2461 dout(" inode %p now clean\n", inode); 2462 BUG_ON(!list_empty(&ci->i_dirty_item)); 2463 drop = 1; 2464 } else { 2465 BUG_ON(list_empty(&ci->i_dirty_item)); 2466 } 2467 } 2468 spin_unlock(&mdsc->cap_dirty_lock); 2469 wake_up(&ci->i_cap_wq); 2470 2471 out: 2472 spin_unlock(&inode->i_lock); 2473 if (drop) 2474 iput(inode); 2475 } 2476 2477 /* 2478 * Handle FLUSHSNAP_ACK. MDS has flushed snap data to disk and we can 2479 * throw away our cap_snap. 2480 * 2481 * Caller hold s_mutex. 2482 */ 2483 static void handle_cap_flushsnap_ack(struct inode *inode, u64 flush_tid, 2484 struct ceph_mds_caps *m, 2485 struct ceph_mds_session *session) 2486 { 2487 struct ceph_inode_info *ci = ceph_inode(inode); 2488 u64 follows = le64_to_cpu(m->snap_follows); 2489 struct ceph_cap_snap *capsnap; 2490 int drop = 0; 2491 2492 dout("handle_cap_flushsnap_ack inode %p ci %p mds%d follows %lld\n", 2493 inode, ci, session->s_mds, follows); 2494 2495 spin_lock(&inode->i_lock); 2496 list_for_each_entry(capsnap, &ci->i_cap_snaps, ci_item) { 2497 if (capsnap->follows == follows) { 2498 if (capsnap->flush_tid != flush_tid) { 2499 dout(" cap_snap %p follows %lld tid %lld !=" 2500 " %lld\n", capsnap, follows, 2501 flush_tid, capsnap->flush_tid); 2502 break; 2503 } 2504 WARN_ON(capsnap->dirty_pages || capsnap->writing); 2505 dout(" removing %p cap_snap %p follows %lld\n", 2506 inode, capsnap, follows); 2507 ceph_put_snap_context(capsnap->context); 2508 list_del(&capsnap->ci_item); 2509 list_del(&capsnap->flushing_item); 2510 ceph_put_cap_snap(capsnap); 2511 drop = 1; 2512 break; 2513 } else { 2514 dout(" skipping cap_snap %p follows %lld\n", 2515 capsnap, capsnap->follows); 2516 } 2517 } 2518 spin_unlock(&inode->i_lock); 2519 if (drop) 2520 iput(inode); 2521 } 2522 2523 /* 2524 * Handle TRUNC from MDS, indicating file truncation. 2525 * 2526 * caller hold s_mutex. 2527 */ 2528 static void handle_cap_trunc(struct inode *inode, 2529 struct ceph_mds_caps *trunc, 2530 struct ceph_mds_session *session) 2531 __releases(inode->i_lock) 2532 { 2533 struct ceph_inode_info *ci = ceph_inode(inode); 2534 int mds = session->s_mds; 2535 int seq = le32_to_cpu(trunc->seq); 2536 u32 truncate_seq = le32_to_cpu(trunc->truncate_seq); 2537 u64 truncate_size = le64_to_cpu(trunc->truncate_size); 2538 u64 size = le64_to_cpu(trunc->size); 2539 int implemented = 0; 2540 int dirty = __ceph_caps_dirty(ci); 2541 int issued = __ceph_caps_issued(ceph_inode(inode), &implemented); 2542 int queue_trunc = 0; 2543 2544 issued |= implemented | dirty; 2545 2546 dout("handle_cap_trunc inode %p mds%d seq %d to %lld seq %d\n", 2547 inode, mds, seq, truncate_size, truncate_seq); 2548 queue_trunc = ceph_fill_file_size(inode, issued, 2549 truncate_seq, truncate_size, size); 2550 spin_unlock(&inode->i_lock); 2551 2552 if (queue_trunc) 2553 ceph_queue_vmtruncate(inode); 2554 } 2555 2556 /* 2557 * Handle EXPORT from MDS. Cap is being migrated _from_ this mds to a 2558 * different one. If we are the most recent migration we've seen (as 2559 * indicated by mseq), make note of the migrating cap bits for the 2560 * duration (until we see the corresponding IMPORT). 2561 * 2562 * caller holds s_mutex 2563 */ 2564 static void handle_cap_export(struct inode *inode, struct ceph_mds_caps *ex, 2565 struct ceph_mds_session *session) 2566 { 2567 struct ceph_inode_info *ci = ceph_inode(inode); 2568 int mds = session->s_mds; 2569 unsigned mseq = le32_to_cpu(ex->migrate_seq); 2570 struct ceph_cap *cap = NULL, *t; 2571 struct rb_node *p; 2572 int remember = 1; 2573 2574 dout("handle_cap_export inode %p ci %p mds%d mseq %d\n", 2575 inode, ci, mds, mseq); 2576 2577 spin_lock(&inode->i_lock); 2578 2579 /* make sure we haven't seen a higher mseq */ 2580 for (p = rb_first(&ci->i_caps); p; p = rb_next(p)) { 2581 t = rb_entry(p, struct ceph_cap, ci_node); 2582 if (ceph_seq_cmp(t->mseq, mseq) > 0) { 2583 dout(" higher mseq on cap from mds%d\n", 2584 t->session->s_mds); 2585 remember = 0; 2586 } 2587 if (t->session->s_mds == mds) 2588 cap = t; 2589 } 2590 2591 if (cap) { 2592 if (remember) { 2593 /* make note */ 2594 ci->i_cap_exporting_mds = mds; 2595 ci->i_cap_exporting_mseq = mseq; 2596 ci->i_cap_exporting_issued = cap->issued; 2597 } 2598 __ceph_remove_cap(cap); 2599 } 2600 /* else, we already released it */ 2601 2602 spin_unlock(&inode->i_lock); 2603 } 2604 2605 /* 2606 * Handle cap IMPORT. If there are temp bits from an older EXPORT, 2607 * clean them up. 2608 * 2609 * caller holds s_mutex. 2610 */ 2611 static void handle_cap_import(struct ceph_mds_client *mdsc, 2612 struct inode *inode, struct ceph_mds_caps *im, 2613 struct ceph_mds_session *session, 2614 void *snaptrace, int snaptrace_len) 2615 { 2616 struct ceph_inode_info *ci = ceph_inode(inode); 2617 int mds = session->s_mds; 2618 unsigned issued = le32_to_cpu(im->caps); 2619 unsigned wanted = le32_to_cpu(im->wanted); 2620 unsigned seq = le32_to_cpu(im->seq); 2621 unsigned mseq = le32_to_cpu(im->migrate_seq); 2622 u64 realmino = le64_to_cpu(im->realm); 2623 u64 cap_id = le64_to_cpu(im->cap_id); 2624 2625 if (ci->i_cap_exporting_mds >= 0 && 2626 ceph_seq_cmp(ci->i_cap_exporting_mseq, mseq) < 0) { 2627 dout("handle_cap_import inode %p ci %p mds%d mseq %d" 2628 " - cleared exporting from mds%d\n", 2629 inode, ci, mds, mseq, 2630 ci->i_cap_exporting_mds); 2631 ci->i_cap_exporting_issued = 0; 2632 ci->i_cap_exporting_mseq = 0; 2633 ci->i_cap_exporting_mds = -1; 2634 } else { 2635 dout("handle_cap_import inode %p ci %p mds%d mseq %d\n", 2636 inode, ci, mds, mseq); 2637 } 2638 2639 down_write(&mdsc->snap_rwsem); 2640 ceph_update_snap_trace(mdsc, snaptrace, snaptrace+snaptrace_len, 2641 false); 2642 downgrade_write(&mdsc->snap_rwsem); 2643 ceph_add_cap(inode, session, cap_id, -1, 2644 issued, wanted, seq, mseq, realmino, CEPH_CAP_FLAG_AUTH, 2645 NULL /* no caps context */); 2646 try_flush_caps(inode, session, NULL); 2647 up_read(&mdsc->snap_rwsem); 2648 } 2649 2650 /* 2651 * Handle a caps message from the MDS. 2652 * 2653 * Identify the appropriate session, inode, and call the right handler 2654 * based on the cap op. 2655 */ 2656 void ceph_handle_caps(struct ceph_mds_session *session, 2657 struct ceph_msg *msg) 2658 { 2659 struct ceph_mds_client *mdsc = session->s_mdsc; 2660 struct super_block *sb = mdsc->client->sb; 2661 struct inode *inode; 2662 struct ceph_cap *cap; 2663 struct ceph_mds_caps *h; 2664 int mds = session->s_mds; 2665 int op; 2666 u32 seq, mseq; 2667 struct ceph_vino vino; 2668 u64 cap_id; 2669 u64 size, max_size; 2670 u64 tid; 2671 void *snaptrace; 2672 2673 dout("handle_caps from mds%d\n", mds); 2674 2675 /* decode */ 2676 tid = le64_to_cpu(msg->hdr.tid); 2677 if (msg->front.iov_len < sizeof(*h)) 2678 goto bad; 2679 h = msg->front.iov_base; 2680 snaptrace = h + 1; 2681 op = le32_to_cpu(h->op); 2682 vino.ino = le64_to_cpu(h->ino); 2683 vino.snap = CEPH_NOSNAP; 2684 cap_id = le64_to_cpu(h->cap_id); 2685 seq = le32_to_cpu(h->seq); 2686 mseq = le32_to_cpu(h->migrate_seq); 2687 size = le64_to_cpu(h->size); 2688 max_size = le64_to_cpu(h->max_size); 2689 2690 mutex_lock(&session->s_mutex); 2691 session->s_seq++; 2692 dout(" mds%d seq %lld cap seq %u\n", session->s_mds, session->s_seq, 2693 (unsigned)seq); 2694 2695 /* lookup ino */ 2696 inode = ceph_find_inode(sb, vino); 2697 dout(" op %s ino %llx.%llx inode %p\n", ceph_cap_op_name(op), vino.ino, 2698 vino.snap, inode); 2699 if (!inode) { 2700 dout(" i don't have ino %llx\n", vino.ino); 2701 2702 if (op == CEPH_CAP_OP_IMPORT) 2703 __queue_cap_release(session, vino.ino, cap_id, 2704 mseq, seq); 2705 2706 /* 2707 * send any full release message to try to move things 2708 * along for the mds (who clearly thinks we still have this 2709 * cap). 2710 */ 2711 ceph_add_cap_releases(mdsc, session, -1); 2712 ceph_send_cap_releases(mdsc, session); 2713 goto done; 2714 } 2715 2716 /* these will work even if we don't have a cap yet */ 2717 switch (op) { 2718 case CEPH_CAP_OP_FLUSHSNAP_ACK: 2719 handle_cap_flushsnap_ack(inode, tid, h, session); 2720 goto done; 2721 2722 case CEPH_CAP_OP_EXPORT: 2723 handle_cap_export(inode, h, session); 2724 goto done; 2725 2726 case CEPH_CAP_OP_IMPORT: 2727 handle_cap_import(mdsc, inode, h, session, 2728 snaptrace, le32_to_cpu(h->snap_trace_len)); 2729 ceph_check_caps(ceph_inode(inode), CHECK_CAPS_NODELAY, 2730 session); 2731 goto done_unlocked; 2732 } 2733 2734 /* the rest require a cap */ 2735 spin_lock(&inode->i_lock); 2736 cap = __get_cap_for_mds(ceph_inode(inode), mds); 2737 if (!cap) { 2738 dout(" no cap on %p ino %llx.%llx from mds%d\n", 2739 inode, ceph_ino(inode), ceph_snap(inode), mds); 2740 spin_unlock(&inode->i_lock); 2741 goto done; 2742 } 2743 2744 /* note that each of these drops i_lock for us */ 2745 switch (op) { 2746 case CEPH_CAP_OP_REVOKE: 2747 case CEPH_CAP_OP_GRANT: 2748 handle_cap_grant(inode, h, session, cap, msg->middle); 2749 goto done_unlocked; 2750 2751 case CEPH_CAP_OP_FLUSH_ACK: 2752 handle_cap_flush_ack(inode, tid, h, session, cap); 2753 break; 2754 2755 case CEPH_CAP_OP_TRUNC: 2756 handle_cap_trunc(inode, h, session); 2757 break; 2758 2759 default: 2760 spin_unlock(&inode->i_lock); 2761 pr_err("ceph_handle_caps: unknown cap op %d %s\n", op, 2762 ceph_cap_op_name(op)); 2763 } 2764 2765 done: 2766 mutex_unlock(&session->s_mutex); 2767 done_unlocked: 2768 if (inode) 2769 iput(inode); 2770 return; 2771 2772 bad: 2773 pr_err("ceph_handle_caps: corrupt message\n"); 2774 ceph_msg_dump(msg); 2775 return; 2776 } 2777 2778 /* 2779 * Delayed work handler to process end of delayed cap release LRU list. 2780 */ 2781 void ceph_check_delayed_caps(struct ceph_mds_client *mdsc) 2782 { 2783 struct ceph_inode_info *ci; 2784 int flags = CHECK_CAPS_NODELAY; 2785 2786 dout("check_delayed_caps\n"); 2787 while (1) { 2788 spin_lock(&mdsc->cap_delay_lock); 2789 if (list_empty(&mdsc->cap_delay_list)) 2790 break; 2791 ci = list_first_entry(&mdsc->cap_delay_list, 2792 struct ceph_inode_info, 2793 i_cap_delay_list); 2794 if ((ci->i_ceph_flags & CEPH_I_FLUSH) == 0 && 2795 time_before(jiffies, ci->i_hold_caps_max)) 2796 break; 2797 list_del_init(&ci->i_cap_delay_list); 2798 spin_unlock(&mdsc->cap_delay_lock); 2799 dout("check_delayed_caps on %p\n", &ci->vfs_inode); 2800 ceph_check_caps(ci, flags, NULL); 2801 } 2802 spin_unlock(&mdsc->cap_delay_lock); 2803 } 2804 2805 /* 2806 * Flush all dirty caps to the mds 2807 */ 2808 void ceph_flush_dirty_caps(struct ceph_mds_client *mdsc) 2809 { 2810 struct ceph_inode_info *ci, *nci = NULL; 2811 struct inode *inode, *ninode = NULL; 2812 struct list_head *p, *n; 2813 2814 dout("flush_dirty_caps\n"); 2815 spin_lock(&mdsc->cap_dirty_lock); 2816 list_for_each_safe(p, n, &mdsc->cap_dirty) { 2817 if (nci) { 2818 ci = nci; 2819 inode = ninode; 2820 ci->i_ceph_flags &= ~CEPH_I_NOFLUSH; 2821 dout("flush_dirty_caps inode %p (was next inode)\n", 2822 inode); 2823 } else { 2824 ci = list_entry(p, struct ceph_inode_info, 2825 i_dirty_item); 2826 inode = igrab(&ci->vfs_inode); 2827 BUG_ON(!inode); 2828 dout("flush_dirty_caps inode %p\n", inode); 2829 } 2830 if (n != &mdsc->cap_dirty) { 2831 nci = list_entry(n, struct ceph_inode_info, 2832 i_dirty_item); 2833 ninode = igrab(&nci->vfs_inode); 2834 BUG_ON(!ninode); 2835 nci->i_ceph_flags |= CEPH_I_NOFLUSH; 2836 dout("flush_dirty_caps next inode %p, noflush\n", 2837 ninode); 2838 } else { 2839 nci = NULL; 2840 ninode = NULL; 2841 } 2842 spin_unlock(&mdsc->cap_dirty_lock); 2843 if (inode) { 2844 ceph_check_caps(ci, CHECK_CAPS_NODELAY|CHECK_CAPS_FLUSH, 2845 NULL); 2846 iput(inode); 2847 } 2848 spin_lock(&mdsc->cap_dirty_lock); 2849 } 2850 spin_unlock(&mdsc->cap_dirty_lock); 2851 } 2852 2853 /* 2854 * Drop open file reference. If we were the last open file, 2855 * we may need to release capabilities to the MDS (or schedule 2856 * their delayed release). 2857 */ 2858 void ceph_put_fmode(struct ceph_inode_info *ci, int fmode) 2859 { 2860 struct inode *inode = &ci->vfs_inode; 2861 int last = 0; 2862 2863 spin_lock(&inode->i_lock); 2864 dout("put_fmode %p fmode %d %d -> %d\n", inode, fmode, 2865 ci->i_nr_by_mode[fmode], ci->i_nr_by_mode[fmode]-1); 2866 BUG_ON(ci->i_nr_by_mode[fmode] == 0); 2867 if (--ci->i_nr_by_mode[fmode] == 0) 2868 last++; 2869 spin_unlock(&inode->i_lock); 2870 2871 if (last && ci->i_vino.snap == CEPH_NOSNAP) 2872 ceph_check_caps(ci, 0, NULL); 2873 } 2874 2875 /* 2876 * Helpers for embedding cap and dentry lease releases into mds 2877 * requests. 2878 * 2879 * @force is used by dentry_release (below) to force inclusion of a 2880 * record for the directory inode, even when there aren't any caps to 2881 * drop. 2882 */ 2883 int ceph_encode_inode_release(void **p, struct inode *inode, 2884 int mds, int drop, int unless, int force) 2885 { 2886 struct ceph_inode_info *ci = ceph_inode(inode); 2887 struct ceph_cap *cap; 2888 struct ceph_mds_request_release *rel = *p; 2889 int ret = 0; 2890 int used = 0; 2891 2892 spin_lock(&inode->i_lock); 2893 used = __ceph_caps_used(ci); 2894 2895 dout("encode_inode_release %p mds%d used %s drop %s unless %s\n", inode, 2896 mds, ceph_cap_string(used), ceph_cap_string(drop), 2897 ceph_cap_string(unless)); 2898 2899 /* only drop unused caps */ 2900 drop &= ~used; 2901 2902 cap = __get_cap_for_mds(ci, mds); 2903 if (cap && __cap_is_valid(cap)) { 2904 if (force || 2905 ((cap->issued & drop) && 2906 (cap->issued & unless) == 0)) { 2907 if ((cap->issued & drop) && 2908 (cap->issued & unless) == 0) { 2909 dout("encode_inode_release %p cap %p %s -> " 2910 "%s\n", inode, cap, 2911 ceph_cap_string(cap->issued), 2912 ceph_cap_string(cap->issued & ~drop)); 2913 cap->issued &= ~drop; 2914 cap->implemented &= ~drop; 2915 if (ci->i_ceph_flags & CEPH_I_NODELAY) { 2916 int wanted = __ceph_caps_wanted(ci); 2917 dout(" wanted %s -> %s (act %s)\n", 2918 ceph_cap_string(cap->mds_wanted), 2919 ceph_cap_string(cap->mds_wanted & 2920 ~wanted), 2921 ceph_cap_string(wanted)); 2922 cap->mds_wanted &= wanted; 2923 } 2924 } else { 2925 dout("encode_inode_release %p cap %p %s" 2926 " (force)\n", inode, cap, 2927 ceph_cap_string(cap->issued)); 2928 } 2929 2930 rel->ino = cpu_to_le64(ceph_ino(inode)); 2931 rel->cap_id = cpu_to_le64(cap->cap_id); 2932 rel->seq = cpu_to_le32(cap->seq); 2933 rel->issue_seq = cpu_to_le32(cap->issue_seq), 2934 rel->mseq = cpu_to_le32(cap->mseq); 2935 rel->caps = cpu_to_le32(cap->issued); 2936 rel->wanted = cpu_to_le32(cap->mds_wanted); 2937 rel->dname_len = 0; 2938 rel->dname_seq = 0; 2939 *p += sizeof(*rel); 2940 ret = 1; 2941 } else { 2942 dout("encode_inode_release %p cap %p %s\n", 2943 inode, cap, ceph_cap_string(cap->issued)); 2944 } 2945 } 2946 spin_unlock(&inode->i_lock); 2947 return ret; 2948 } 2949 2950 int ceph_encode_dentry_release(void **p, struct dentry *dentry, 2951 int mds, int drop, int unless) 2952 { 2953 struct inode *dir = dentry->d_parent->d_inode; 2954 struct ceph_mds_request_release *rel = *p; 2955 struct ceph_dentry_info *di = ceph_dentry(dentry); 2956 int force = 0; 2957 int ret; 2958 2959 /* 2960 * force an record for the directory caps if we have a dentry lease. 2961 * this is racy (can't take i_lock and d_lock together), but it 2962 * doesn't have to be perfect; the mds will revoke anything we don't 2963 * release. 2964 */ 2965 spin_lock(&dentry->d_lock); 2966 if (di->lease_session && di->lease_session->s_mds == mds) 2967 force = 1; 2968 spin_unlock(&dentry->d_lock); 2969 2970 ret = ceph_encode_inode_release(p, dir, mds, drop, unless, force); 2971 2972 spin_lock(&dentry->d_lock); 2973 if (ret && di->lease_session && di->lease_session->s_mds == mds) { 2974 dout("encode_dentry_release %p mds%d seq %d\n", 2975 dentry, mds, (int)di->lease_seq); 2976 rel->dname_len = cpu_to_le32(dentry->d_name.len); 2977 memcpy(*p, dentry->d_name.name, dentry->d_name.len); 2978 *p += dentry->d_name.len; 2979 rel->dname_seq = cpu_to_le32(di->lease_seq); 2980 } 2981 spin_unlock(&dentry->d_lock); 2982 return ret; 2983 } 2984