1 #include "ceph_debug.h" 2 3 #include <linux/sort.h> 4 #include <linux/slab.h> 5 6 #include "super.h" 7 #include "decode.h" 8 9 /* 10 * Snapshots in ceph are driven in large part by cooperation from the 11 * client. In contrast to local file systems or file servers that 12 * implement snapshots at a single point in the system, ceph's 13 * distributed access to storage requires clients to help decide 14 * whether a write logically occurs before or after a recently created 15 * snapshot. 16 * 17 * This provides a perfect instantanous client-wide snapshot. Between 18 * clients, however, snapshots may appear to be applied at slightly 19 * different points in time, depending on delays in delivering the 20 * snapshot notification. 21 * 22 * Snapshots are _not_ file system-wide. Instead, each snapshot 23 * applies to the subdirectory nested beneath some directory. This 24 * effectively divides the hierarchy into multiple "realms," where all 25 * of the files contained by each realm share the same set of 26 * snapshots. An individual realm's snap set contains snapshots 27 * explicitly created on that realm, as well as any snaps in its 28 * parent's snap set _after_ the point at which the parent became it's 29 * parent (due to, say, a rename). Similarly, snaps from prior parents 30 * during the time intervals during which they were the parent are included. 31 * 32 * The client is spared most of this detail, fortunately... it must only 33 * maintains a hierarchy of realms reflecting the current parent/child 34 * realm relationship, and for each realm has an explicit list of snaps 35 * inherited from prior parents. 36 * 37 * A snap_realm struct is maintained for realms containing every inode 38 * with an open cap in the system. (The needed snap realm information is 39 * provided by the MDS whenever a cap is issued, i.e., on open.) A 'seq' 40 * version number is used to ensure that as realm parameters change (new 41 * snapshot, new parent, etc.) the client's realm hierarchy is updated. 42 * 43 * The realm hierarchy drives the generation of a 'snap context' for each 44 * realm, which simply lists the resulting set of snaps for the realm. This 45 * is attached to any writes sent to OSDs. 46 */ 47 /* 48 * Unfortunately error handling is a bit mixed here. If we get a snap 49 * update, but don't have enough memory to update our realm hierarchy, 50 * it's not clear what we can do about it (besides complaining to the 51 * console). 52 */ 53 54 55 /* 56 * increase ref count for the realm 57 * 58 * caller must hold snap_rwsem for write. 59 */ 60 void ceph_get_snap_realm(struct ceph_mds_client *mdsc, 61 struct ceph_snap_realm *realm) 62 { 63 dout("get_realm %p %d -> %d\n", realm, 64 atomic_read(&realm->nref), atomic_read(&realm->nref)+1); 65 /* 66 * since we _only_ increment realm refs or empty the empty 67 * list with snap_rwsem held, adjusting the empty list here is 68 * safe. we do need to protect against concurrent empty list 69 * additions, however. 70 */ 71 if (atomic_read(&realm->nref) == 0) { 72 spin_lock(&mdsc->snap_empty_lock); 73 list_del_init(&realm->empty_item); 74 spin_unlock(&mdsc->snap_empty_lock); 75 } 76 77 atomic_inc(&realm->nref); 78 } 79 80 static void __insert_snap_realm(struct rb_root *root, 81 struct ceph_snap_realm *new) 82 { 83 struct rb_node **p = &root->rb_node; 84 struct rb_node *parent = NULL; 85 struct ceph_snap_realm *r = NULL; 86 87 while (*p) { 88 parent = *p; 89 r = rb_entry(parent, struct ceph_snap_realm, node); 90 if (new->ino < r->ino) 91 p = &(*p)->rb_left; 92 else if (new->ino > r->ino) 93 p = &(*p)->rb_right; 94 else 95 BUG(); 96 } 97 98 rb_link_node(&new->node, parent, p); 99 rb_insert_color(&new->node, root); 100 } 101 102 /* 103 * create and get the realm rooted at @ino and bump its ref count. 104 * 105 * caller must hold snap_rwsem for write. 106 */ 107 static struct ceph_snap_realm *ceph_create_snap_realm( 108 struct ceph_mds_client *mdsc, 109 u64 ino) 110 { 111 struct ceph_snap_realm *realm; 112 113 realm = kzalloc(sizeof(*realm), GFP_NOFS); 114 if (!realm) 115 return ERR_PTR(-ENOMEM); 116 117 atomic_set(&realm->nref, 0); /* tree does not take a ref */ 118 realm->ino = ino; 119 INIT_LIST_HEAD(&realm->children); 120 INIT_LIST_HEAD(&realm->child_item); 121 INIT_LIST_HEAD(&realm->empty_item); 122 INIT_LIST_HEAD(&realm->inodes_with_caps); 123 spin_lock_init(&realm->inodes_with_caps_lock); 124 __insert_snap_realm(&mdsc->snap_realms, realm); 125 dout("create_snap_realm %llx %p\n", realm->ino, realm); 126 return realm; 127 } 128 129 /* 130 * lookup the realm rooted at @ino. 131 * 132 * caller must hold snap_rwsem for write. 133 */ 134 struct ceph_snap_realm *ceph_lookup_snap_realm(struct ceph_mds_client *mdsc, 135 u64 ino) 136 { 137 struct rb_node *n = mdsc->snap_realms.rb_node; 138 struct ceph_snap_realm *r; 139 140 while (n) { 141 r = rb_entry(n, struct ceph_snap_realm, node); 142 if (ino < r->ino) 143 n = n->rb_left; 144 else if (ino > r->ino) 145 n = n->rb_right; 146 else { 147 dout("lookup_snap_realm %llx %p\n", r->ino, r); 148 return r; 149 } 150 } 151 return NULL; 152 } 153 154 static void __put_snap_realm(struct ceph_mds_client *mdsc, 155 struct ceph_snap_realm *realm); 156 157 /* 158 * called with snap_rwsem (write) 159 */ 160 static void __destroy_snap_realm(struct ceph_mds_client *mdsc, 161 struct ceph_snap_realm *realm) 162 { 163 dout("__destroy_snap_realm %p %llx\n", realm, realm->ino); 164 165 rb_erase(&realm->node, &mdsc->snap_realms); 166 167 if (realm->parent) { 168 list_del_init(&realm->child_item); 169 __put_snap_realm(mdsc, realm->parent); 170 } 171 172 kfree(realm->prior_parent_snaps); 173 kfree(realm->snaps); 174 ceph_put_snap_context(realm->cached_context); 175 kfree(realm); 176 } 177 178 /* 179 * caller holds snap_rwsem (write) 180 */ 181 static void __put_snap_realm(struct ceph_mds_client *mdsc, 182 struct ceph_snap_realm *realm) 183 { 184 dout("__put_snap_realm %llx %p %d -> %d\n", realm->ino, realm, 185 atomic_read(&realm->nref), atomic_read(&realm->nref)-1); 186 if (atomic_dec_and_test(&realm->nref)) 187 __destroy_snap_realm(mdsc, realm); 188 } 189 190 /* 191 * caller needn't hold any locks 192 */ 193 void ceph_put_snap_realm(struct ceph_mds_client *mdsc, 194 struct ceph_snap_realm *realm) 195 { 196 dout("put_snap_realm %llx %p %d -> %d\n", realm->ino, realm, 197 atomic_read(&realm->nref), atomic_read(&realm->nref)-1); 198 if (!atomic_dec_and_test(&realm->nref)) 199 return; 200 201 if (down_write_trylock(&mdsc->snap_rwsem)) { 202 __destroy_snap_realm(mdsc, realm); 203 up_write(&mdsc->snap_rwsem); 204 } else { 205 spin_lock(&mdsc->snap_empty_lock); 206 list_add(&mdsc->snap_empty, &realm->empty_item); 207 spin_unlock(&mdsc->snap_empty_lock); 208 } 209 } 210 211 /* 212 * Clean up any realms whose ref counts have dropped to zero. Note 213 * that this does not include realms who were created but not yet 214 * used. 215 * 216 * Called under snap_rwsem (write) 217 */ 218 static void __cleanup_empty_realms(struct ceph_mds_client *mdsc) 219 { 220 struct ceph_snap_realm *realm; 221 222 spin_lock(&mdsc->snap_empty_lock); 223 while (!list_empty(&mdsc->snap_empty)) { 224 realm = list_first_entry(&mdsc->snap_empty, 225 struct ceph_snap_realm, empty_item); 226 list_del(&realm->empty_item); 227 spin_unlock(&mdsc->snap_empty_lock); 228 __destroy_snap_realm(mdsc, realm); 229 spin_lock(&mdsc->snap_empty_lock); 230 } 231 spin_unlock(&mdsc->snap_empty_lock); 232 } 233 234 void ceph_cleanup_empty_realms(struct ceph_mds_client *mdsc) 235 { 236 down_write(&mdsc->snap_rwsem); 237 __cleanup_empty_realms(mdsc); 238 up_write(&mdsc->snap_rwsem); 239 } 240 241 /* 242 * adjust the parent realm of a given @realm. adjust child list, and parent 243 * pointers, and ref counts appropriately. 244 * 245 * return true if parent was changed, 0 if unchanged, <0 on error. 246 * 247 * caller must hold snap_rwsem for write. 248 */ 249 static int adjust_snap_realm_parent(struct ceph_mds_client *mdsc, 250 struct ceph_snap_realm *realm, 251 u64 parentino) 252 { 253 struct ceph_snap_realm *parent; 254 255 if (realm->parent_ino == parentino) 256 return 0; 257 258 parent = ceph_lookup_snap_realm(mdsc, parentino); 259 if (!parent) { 260 parent = ceph_create_snap_realm(mdsc, parentino); 261 if (IS_ERR(parent)) 262 return PTR_ERR(parent); 263 } 264 dout("adjust_snap_realm_parent %llx %p: %llx %p -> %llx %p\n", 265 realm->ino, realm, realm->parent_ino, realm->parent, 266 parentino, parent); 267 if (realm->parent) { 268 list_del_init(&realm->child_item); 269 ceph_put_snap_realm(mdsc, realm->parent); 270 } 271 realm->parent_ino = parentino; 272 realm->parent = parent; 273 ceph_get_snap_realm(mdsc, parent); 274 list_add(&realm->child_item, &parent->children); 275 return 1; 276 } 277 278 279 static int cmpu64_rev(const void *a, const void *b) 280 { 281 if (*(u64 *)a < *(u64 *)b) 282 return 1; 283 if (*(u64 *)a > *(u64 *)b) 284 return -1; 285 return 0; 286 } 287 288 /* 289 * build the snap context for a given realm. 290 */ 291 static int build_snap_context(struct ceph_snap_realm *realm) 292 { 293 struct ceph_snap_realm *parent = realm->parent; 294 struct ceph_snap_context *snapc; 295 int err = 0; 296 int i; 297 int num = realm->num_prior_parent_snaps + realm->num_snaps; 298 299 /* 300 * build parent context, if it hasn't been built. 301 * conservatively estimate that all parent snaps might be 302 * included by us. 303 */ 304 if (parent) { 305 if (!parent->cached_context) { 306 err = build_snap_context(parent); 307 if (err) 308 goto fail; 309 } 310 num += parent->cached_context->num_snaps; 311 } 312 313 /* do i actually need to update? not if my context seq 314 matches realm seq, and my parents' does to. (this works 315 because we rebuild_snap_realms() works _downward_ in 316 hierarchy after each update.) */ 317 if (realm->cached_context && 318 realm->cached_context->seq == realm->seq && 319 (!parent || 320 realm->cached_context->seq >= parent->cached_context->seq)) { 321 dout("build_snap_context %llx %p: %p seq %lld (%d snaps)" 322 " (unchanged)\n", 323 realm->ino, realm, realm->cached_context, 324 realm->cached_context->seq, 325 realm->cached_context->num_snaps); 326 return 0; 327 } 328 329 /* alloc new snap context */ 330 err = -ENOMEM; 331 if (num > ULONG_MAX / sizeof(u64) - sizeof(*snapc)) 332 goto fail; 333 snapc = kzalloc(sizeof(*snapc) + num*sizeof(u64), GFP_NOFS); 334 if (!snapc) 335 goto fail; 336 atomic_set(&snapc->nref, 1); 337 338 /* build (reverse sorted) snap vector */ 339 num = 0; 340 snapc->seq = realm->seq; 341 if (parent) { 342 /* include any of parent's snaps occuring _after_ my 343 parent became my parent */ 344 for (i = 0; i < parent->cached_context->num_snaps; i++) 345 if (parent->cached_context->snaps[i] >= 346 realm->parent_since) 347 snapc->snaps[num++] = 348 parent->cached_context->snaps[i]; 349 if (parent->cached_context->seq > snapc->seq) 350 snapc->seq = parent->cached_context->seq; 351 } 352 memcpy(snapc->snaps + num, realm->snaps, 353 sizeof(u64)*realm->num_snaps); 354 num += realm->num_snaps; 355 memcpy(snapc->snaps + num, realm->prior_parent_snaps, 356 sizeof(u64)*realm->num_prior_parent_snaps); 357 num += realm->num_prior_parent_snaps; 358 359 sort(snapc->snaps, num, sizeof(u64), cmpu64_rev, NULL); 360 snapc->num_snaps = num; 361 dout("build_snap_context %llx %p: %p seq %lld (%d snaps)\n", 362 realm->ino, realm, snapc, snapc->seq, snapc->num_snaps); 363 364 if (realm->cached_context) 365 ceph_put_snap_context(realm->cached_context); 366 realm->cached_context = snapc; 367 return 0; 368 369 fail: 370 /* 371 * if we fail, clear old (incorrect) cached_context... hopefully 372 * we'll have better luck building it later 373 */ 374 if (realm->cached_context) { 375 ceph_put_snap_context(realm->cached_context); 376 realm->cached_context = NULL; 377 } 378 pr_err("build_snap_context %llx %p fail %d\n", realm->ino, 379 realm, err); 380 return err; 381 } 382 383 /* 384 * rebuild snap context for the given realm and all of its children. 385 */ 386 static void rebuild_snap_realms(struct ceph_snap_realm *realm) 387 { 388 struct ceph_snap_realm *child; 389 390 dout("rebuild_snap_realms %llx %p\n", realm->ino, realm); 391 build_snap_context(realm); 392 393 list_for_each_entry(child, &realm->children, child_item) 394 rebuild_snap_realms(child); 395 } 396 397 398 /* 399 * helper to allocate and decode an array of snapids. free prior 400 * instance, if any. 401 */ 402 static int dup_array(u64 **dst, __le64 *src, int num) 403 { 404 int i; 405 406 kfree(*dst); 407 if (num) { 408 *dst = kcalloc(num, sizeof(u64), GFP_NOFS); 409 if (!*dst) 410 return -ENOMEM; 411 for (i = 0; i < num; i++) 412 (*dst)[i] = get_unaligned_le64(src + i); 413 } else { 414 *dst = NULL; 415 } 416 return 0; 417 } 418 419 420 /* 421 * When a snapshot is applied, the size/mtime inode metadata is queued 422 * in a ceph_cap_snap (one for each snapshot) until writeback 423 * completes and the metadata can be flushed back to the MDS. 424 * 425 * However, if a (sync) write is currently in-progress when we apply 426 * the snapshot, we have to wait until the write succeeds or fails 427 * (and a final size/mtime is known). In this case the 428 * cap_snap->writing = 1, and is said to be "pending." When the write 429 * finishes, we __ceph_finish_cap_snap(). 430 * 431 * Caller must hold snap_rwsem for read (i.e., the realm topology won't 432 * change). 433 */ 434 void ceph_queue_cap_snap(struct ceph_inode_info *ci) 435 { 436 struct inode *inode = &ci->vfs_inode; 437 struct ceph_cap_snap *capsnap; 438 int used, dirty; 439 440 capsnap = kzalloc(sizeof(*capsnap), GFP_NOFS); 441 if (!capsnap) { 442 pr_err("ENOMEM allocating ceph_cap_snap on %p\n", inode); 443 return; 444 } 445 446 spin_lock(&inode->i_lock); 447 used = __ceph_caps_used(ci); 448 dirty = __ceph_caps_dirty(ci); 449 if (__ceph_have_pending_cap_snap(ci)) { 450 /* there is no point in queuing multiple "pending" cap_snaps, 451 as no new writes are allowed to start when pending, so any 452 writes in progress now were started before the previous 453 cap_snap. lucky us. */ 454 dout("queue_cap_snap %p already pending\n", inode); 455 kfree(capsnap); 456 } else if (ci->i_wrbuffer_ref_head || (used & CEPH_CAP_FILE_WR) || 457 (dirty & (CEPH_CAP_AUTH_EXCL|CEPH_CAP_XATTR_EXCL| 458 CEPH_CAP_FILE_EXCL|CEPH_CAP_FILE_WR))) { 459 struct ceph_snap_context *snapc = ci->i_head_snapc; 460 461 dout("queue_cap_snap %p cap_snap %p queuing under %p\n", inode, 462 capsnap, snapc); 463 igrab(inode); 464 465 atomic_set(&capsnap->nref, 1); 466 capsnap->ci = ci; 467 INIT_LIST_HEAD(&capsnap->ci_item); 468 INIT_LIST_HEAD(&capsnap->flushing_item); 469 470 capsnap->follows = snapc->seq - 1; 471 capsnap->issued = __ceph_caps_issued(ci, NULL); 472 capsnap->dirty = dirty; 473 474 capsnap->mode = inode->i_mode; 475 capsnap->uid = inode->i_uid; 476 capsnap->gid = inode->i_gid; 477 478 if (dirty & CEPH_CAP_XATTR_EXCL) { 479 __ceph_build_xattrs_blob(ci); 480 capsnap->xattr_blob = 481 ceph_buffer_get(ci->i_xattrs.blob); 482 capsnap->xattr_version = ci->i_xattrs.version; 483 } else { 484 capsnap->xattr_blob = NULL; 485 capsnap->xattr_version = 0; 486 } 487 488 /* dirty page count moved from _head to this cap_snap; 489 all subsequent writes page dirties occur _after_ this 490 snapshot. */ 491 capsnap->dirty_pages = ci->i_wrbuffer_ref_head; 492 ci->i_wrbuffer_ref_head = 0; 493 capsnap->context = snapc; 494 ci->i_head_snapc = 495 ceph_get_snap_context(ci->i_snap_realm->cached_context); 496 dout(" new snapc is %p\n", ci->i_head_snapc); 497 list_add_tail(&capsnap->ci_item, &ci->i_cap_snaps); 498 499 if (used & CEPH_CAP_FILE_WR) { 500 dout("queue_cap_snap %p cap_snap %p snapc %p" 501 " seq %llu used WR, now pending\n", inode, 502 capsnap, snapc, snapc->seq); 503 capsnap->writing = 1; 504 } else { 505 /* note mtime, size NOW. */ 506 __ceph_finish_cap_snap(ci, capsnap); 507 } 508 } else { 509 dout("queue_cap_snap %p nothing dirty|writing\n", inode); 510 kfree(capsnap); 511 } 512 513 spin_unlock(&inode->i_lock); 514 } 515 516 /* 517 * Finalize the size, mtime for a cap_snap.. that is, settle on final values 518 * to be used for the snapshot, to be flushed back to the mds. 519 * 520 * If capsnap can now be flushed, add to snap_flush list, and return 1. 521 * 522 * Caller must hold i_lock. 523 */ 524 int __ceph_finish_cap_snap(struct ceph_inode_info *ci, 525 struct ceph_cap_snap *capsnap) 526 { 527 struct inode *inode = &ci->vfs_inode; 528 struct ceph_mds_client *mdsc = &ceph_sb_to_client(inode->i_sb)->mdsc; 529 530 BUG_ON(capsnap->writing); 531 capsnap->size = inode->i_size; 532 capsnap->mtime = inode->i_mtime; 533 capsnap->atime = inode->i_atime; 534 capsnap->ctime = inode->i_ctime; 535 capsnap->time_warp_seq = ci->i_time_warp_seq; 536 if (capsnap->dirty_pages) { 537 dout("finish_cap_snap %p cap_snap %p snapc %p %llu %s s=%llu " 538 "still has %d dirty pages\n", inode, capsnap, 539 capsnap->context, capsnap->context->seq, 540 ceph_cap_string(capsnap->dirty), capsnap->size, 541 capsnap->dirty_pages); 542 return 0; 543 } 544 dout("finish_cap_snap %p cap_snap %p snapc %p %llu %s s=%llu\n", 545 inode, capsnap, capsnap->context, 546 capsnap->context->seq, ceph_cap_string(capsnap->dirty), 547 capsnap->size); 548 549 spin_lock(&mdsc->snap_flush_lock); 550 list_add_tail(&ci->i_snap_flush_item, &mdsc->snap_flush_list); 551 spin_unlock(&mdsc->snap_flush_lock); 552 return 1; /* caller may want to ceph_flush_snaps */ 553 } 554 555 /* 556 * Queue cap_snaps for snap writeback for this realm and its children. 557 * Called under snap_rwsem, so realm topology won't change. 558 */ 559 static void queue_realm_cap_snaps(struct ceph_snap_realm *realm) 560 { 561 struct ceph_inode_info *ci; 562 struct inode *lastinode = NULL; 563 struct ceph_snap_realm *child; 564 565 dout("queue_realm_cap_snaps %p %llx inodes\n", realm, realm->ino); 566 567 spin_lock(&realm->inodes_with_caps_lock); 568 list_for_each_entry(ci, &realm->inodes_with_caps, 569 i_snap_realm_item) { 570 struct inode *inode = igrab(&ci->vfs_inode); 571 if (!inode) 572 continue; 573 spin_unlock(&realm->inodes_with_caps_lock); 574 if (lastinode) 575 iput(lastinode); 576 lastinode = inode; 577 ceph_queue_cap_snap(ci); 578 spin_lock(&realm->inodes_with_caps_lock); 579 } 580 spin_unlock(&realm->inodes_with_caps_lock); 581 if (lastinode) 582 iput(lastinode); 583 584 dout("queue_realm_cap_snaps %p %llx children\n", realm, realm->ino); 585 list_for_each_entry(child, &realm->children, child_item) 586 queue_realm_cap_snaps(child); 587 588 dout("queue_realm_cap_snaps %p %llx done\n", realm, realm->ino); 589 } 590 591 /* 592 * Parse and apply a snapblob "snap trace" from the MDS. This specifies 593 * the snap realm parameters from a given realm and all of its ancestors, 594 * up to the root. 595 * 596 * Caller must hold snap_rwsem for write. 597 */ 598 int ceph_update_snap_trace(struct ceph_mds_client *mdsc, 599 void *p, void *e, bool deletion) 600 { 601 struct ceph_mds_snap_realm *ri; /* encoded */ 602 __le64 *snaps; /* encoded */ 603 __le64 *prior_parent_snaps; /* encoded */ 604 struct ceph_snap_realm *realm; 605 int invalidate = 0; 606 int err = -ENOMEM; 607 608 dout("update_snap_trace deletion=%d\n", deletion); 609 more: 610 ceph_decode_need(&p, e, sizeof(*ri), bad); 611 ri = p; 612 p += sizeof(*ri); 613 ceph_decode_need(&p, e, sizeof(u64)*(le32_to_cpu(ri->num_snaps) + 614 le32_to_cpu(ri->num_prior_parent_snaps)), bad); 615 snaps = p; 616 p += sizeof(u64) * le32_to_cpu(ri->num_snaps); 617 prior_parent_snaps = p; 618 p += sizeof(u64) * le32_to_cpu(ri->num_prior_parent_snaps); 619 620 realm = ceph_lookup_snap_realm(mdsc, le64_to_cpu(ri->ino)); 621 if (!realm) { 622 realm = ceph_create_snap_realm(mdsc, le64_to_cpu(ri->ino)); 623 if (IS_ERR(realm)) { 624 err = PTR_ERR(realm); 625 goto fail; 626 } 627 } 628 629 if (le64_to_cpu(ri->seq) > realm->seq) { 630 dout("update_snap_trace updating %llx %p %lld -> %lld\n", 631 realm->ino, realm, realm->seq, le64_to_cpu(ri->seq)); 632 /* 633 * if the realm seq has changed, queue a cap_snap for every 634 * inode with open caps. we do this _before_ we update 635 * the realm info so that we prepare for writeback under the 636 * _previous_ snap context. 637 * 638 * ...unless it's a snap deletion! 639 */ 640 if (!deletion) 641 queue_realm_cap_snaps(realm); 642 } else { 643 dout("update_snap_trace %llx %p seq %lld unchanged\n", 644 realm->ino, realm, realm->seq); 645 } 646 647 /* ensure the parent is correct */ 648 err = adjust_snap_realm_parent(mdsc, realm, le64_to_cpu(ri->parent)); 649 if (err < 0) 650 goto fail; 651 invalidate += err; 652 653 if (le64_to_cpu(ri->seq) > realm->seq) { 654 /* update realm parameters, snap lists */ 655 realm->seq = le64_to_cpu(ri->seq); 656 realm->created = le64_to_cpu(ri->created); 657 realm->parent_since = le64_to_cpu(ri->parent_since); 658 659 realm->num_snaps = le32_to_cpu(ri->num_snaps); 660 err = dup_array(&realm->snaps, snaps, realm->num_snaps); 661 if (err < 0) 662 goto fail; 663 664 realm->num_prior_parent_snaps = 665 le32_to_cpu(ri->num_prior_parent_snaps); 666 err = dup_array(&realm->prior_parent_snaps, prior_parent_snaps, 667 realm->num_prior_parent_snaps); 668 if (err < 0) 669 goto fail; 670 671 invalidate = 1; 672 } else if (!realm->cached_context) { 673 invalidate = 1; 674 } 675 676 dout("done with %llx %p, invalidated=%d, %p %p\n", realm->ino, 677 realm, invalidate, p, e); 678 679 if (p < e) 680 goto more; 681 682 /* invalidate when we reach the _end_ (root) of the trace */ 683 if (invalidate) 684 rebuild_snap_realms(realm); 685 686 __cleanup_empty_realms(mdsc); 687 return 0; 688 689 bad: 690 err = -EINVAL; 691 fail: 692 pr_err("update_snap_trace error %d\n", err); 693 return err; 694 } 695 696 697 /* 698 * Send any cap_snaps that are queued for flush. Try to carry 699 * s_mutex across multiple snap flushes to avoid locking overhead. 700 * 701 * Caller holds no locks. 702 */ 703 static void flush_snaps(struct ceph_mds_client *mdsc) 704 { 705 struct ceph_inode_info *ci; 706 struct inode *inode; 707 struct ceph_mds_session *session = NULL; 708 709 dout("flush_snaps\n"); 710 spin_lock(&mdsc->snap_flush_lock); 711 while (!list_empty(&mdsc->snap_flush_list)) { 712 ci = list_first_entry(&mdsc->snap_flush_list, 713 struct ceph_inode_info, i_snap_flush_item); 714 inode = &ci->vfs_inode; 715 igrab(inode); 716 spin_unlock(&mdsc->snap_flush_lock); 717 spin_lock(&inode->i_lock); 718 __ceph_flush_snaps(ci, &session); 719 spin_unlock(&inode->i_lock); 720 iput(inode); 721 spin_lock(&mdsc->snap_flush_lock); 722 } 723 spin_unlock(&mdsc->snap_flush_lock); 724 725 if (session) { 726 mutex_unlock(&session->s_mutex); 727 ceph_put_mds_session(session); 728 } 729 dout("flush_snaps done\n"); 730 } 731 732 733 /* 734 * Handle a snap notification from the MDS. 735 * 736 * This can take two basic forms: the simplest is just a snap creation 737 * or deletion notification on an existing realm. This should update the 738 * realm and its children. 739 * 740 * The more difficult case is realm creation, due to snap creation at a 741 * new point in the file hierarchy, or due to a rename that moves a file or 742 * directory into another realm. 743 */ 744 void ceph_handle_snap(struct ceph_mds_client *mdsc, 745 struct ceph_mds_session *session, 746 struct ceph_msg *msg) 747 { 748 struct super_block *sb = mdsc->client->sb; 749 int mds = session->s_mds; 750 u64 split; 751 int op; 752 int trace_len; 753 struct ceph_snap_realm *realm = NULL; 754 void *p = msg->front.iov_base; 755 void *e = p + msg->front.iov_len; 756 struct ceph_mds_snap_head *h; 757 int num_split_inos, num_split_realms; 758 __le64 *split_inos = NULL, *split_realms = NULL; 759 int i; 760 int locked_rwsem = 0; 761 762 /* decode */ 763 if (msg->front.iov_len < sizeof(*h)) 764 goto bad; 765 h = p; 766 op = le32_to_cpu(h->op); 767 split = le64_to_cpu(h->split); /* non-zero if we are splitting an 768 * existing realm */ 769 num_split_inos = le32_to_cpu(h->num_split_inos); 770 num_split_realms = le32_to_cpu(h->num_split_realms); 771 trace_len = le32_to_cpu(h->trace_len); 772 p += sizeof(*h); 773 774 dout("handle_snap from mds%d op %s split %llx tracelen %d\n", mds, 775 ceph_snap_op_name(op), split, trace_len); 776 777 mutex_lock(&session->s_mutex); 778 session->s_seq++; 779 mutex_unlock(&session->s_mutex); 780 781 down_write(&mdsc->snap_rwsem); 782 locked_rwsem = 1; 783 784 if (op == CEPH_SNAP_OP_SPLIT) { 785 struct ceph_mds_snap_realm *ri; 786 787 /* 788 * A "split" breaks part of an existing realm off into 789 * a new realm. The MDS provides a list of inodes 790 * (with caps) and child realms that belong to the new 791 * child. 792 */ 793 split_inos = p; 794 p += sizeof(u64) * num_split_inos; 795 split_realms = p; 796 p += sizeof(u64) * num_split_realms; 797 ceph_decode_need(&p, e, sizeof(*ri), bad); 798 /* we will peek at realm info here, but will _not_ 799 * advance p, as the realm update will occur below in 800 * ceph_update_snap_trace. */ 801 ri = p; 802 803 realm = ceph_lookup_snap_realm(mdsc, split); 804 if (!realm) { 805 realm = ceph_create_snap_realm(mdsc, split); 806 if (IS_ERR(realm)) 807 goto out; 808 } 809 ceph_get_snap_realm(mdsc, realm); 810 811 dout("splitting snap_realm %llx %p\n", realm->ino, realm); 812 for (i = 0; i < num_split_inos; i++) { 813 struct ceph_vino vino = { 814 .ino = le64_to_cpu(split_inos[i]), 815 .snap = CEPH_NOSNAP, 816 }; 817 struct inode *inode = ceph_find_inode(sb, vino); 818 struct ceph_inode_info *ci; 819 820 if (!inode) 821 continue; 822 ci = ceph_inode(inode); 823 824 spin_lock(&inode->i_lock); 825 if (!ci->i_snap_realm) 826 goto skip_inode; 827 /* 828 * If this inode belongs to a realm that was 829 * created after our new realm, we experienced 830 * a race (due to another split notifications 831 * arriving from a different MDS). So skip 832 * this inode. 833 */ 834 if (ci->i_snap_realm->created > 835 le64_to_cpu(ri->created)) { 836 dout(" leaving %p in newer realm %llx %p\n", 837 inode, ci->i_snap_realm->ino, 838 ci->i_snap_realm); 839 goto skip_inode; 840 } 841 dout(" will move %p to split realm %llx %p\n", 842 inode, realm->ino, realm); 843 /* 844 * Remove the inode from the realm's inode 845 * list, but don't add it to the new realm 846 * yet. We don't want the cap_snap to be 847 * queued (again) by ceph_update_snap_trace() 848 * below. Queue it _now_, under the old context. 849 */ 850 spin_lock(&realm->inodes_with_caps_lock); 851 list_del_init(&ci->i_snap_realm_item); 852 spin_unlock(&realm->inodes_with_caps_lock); 853 spin_unlock(&inode->i_lock); 854 855 ceph_queue_cap_snap(ci); 856 857 iput(inode); 858 continue; 859 860 skip_inode: 861 spin_unlock(&inode->i_lock); 862 iput(inode); 863 } 864 865 /* we may have taken some of the old realm's children. */ 866 for (i = 0; i < num_split_realms; i++) { 867 struct ceph_snap_realm *child = 868 ceph_lookup_snap_realm(mdsc, 869 le64_to_cpu(split_realms[i])); 870 if (!child) 871 continue; 872 adjust_snap_realm_parent(mdsc, child, realm->ino); 873 } 874 } 875 876 /* 877 * update using the provided snap trace. if we are deleting a 878 * snap, we can avoid queueing cap_snaps. 879 */ 880 ceph_update_snap_trace(mdsc, p, e, 881 op == CEPH_SNAP_OP_DESTROY); 882 883 if (op == CEPH_SNAP_OP_SPLIT) { 884 /* 885 * ok, _now_ add the inodes into the new realm. 886 */ 887 for (i = 0; i < num_split_inos; i++) { 888 struct ceph_vino vino = { 889 .ino = le64_to_cpu(split_inos[i]), 890 .snap = CEPH_NOSNAP, 891 }; 892 struct inode *inode = ceph_find_inode(sb, vino); 893 struct ceph_inode_info *ci; 894 895 if (!inode) 896 continue; 897 ci = ceph_inode(inode); 898 spin_lock(&inode->i_lock); 899 if (list_empty(&ci->i_snap_realm_item)) { 900 struct ceph_snap_realm *oldrealm = 901 ci->i_snap_realm; 902 903 dout(" moving %p to split realm %llx %p\n", 904 inode, realm->ino, realm); 905 spin_lock(&realm->inodes_with_caps_lock); 906 list_add(&ci->i_snap_realm_item, 907 &realm->inodes_with_caps); 908 ci->i_snap_realm = realm; 909 spin_unlock(&realm->inodes_with_caps_lock); 910 ceph_get_snap_realm(mdsc, realm); 911 ceph_put_snap_realm(mdsc, oldrealm); 912 } 913 spin_unlock(&inode->i_lock); 914 iput(inode); 915 } 916 917 /* we took a reference when we created the realm, above */ 918 ceph_put_snap_realm(mdsc, realm); 919 } 920 921 __cleanup_empty_realms(mdsc); 922 923 up_write(&mdsc->snap_rwsem); 924 925 flush_snaps(mdsc); 926 return; 927 928 bad: 929 pr_err("corrupt snap message from mds%d\n", mds); 930 ceph_msg_dump(msg); 931 out: 932 if (locked_rwsem) 933 up_write(&mdsc->snap_rwsem); 934 return; 935 } 936 937 938 939