1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/ceph/ceph_debug.h>
4 #include <linux/sort.h>
5 #include <linux/slab.h>
6 #include <linux/iversion.h>
8 #include "mds_client.h"
9 #include <linux/ceph/decode.h>
11 /* unused map expires after 5 minutes */
12 #define CEPH_SNAPID_MAP_TIMEOUT (5 * 60 * HZ)
15 * Snapshots in ceph are driven in large part by cooperation from the
16 * client. In contrast to local file systems or file servers that
17 * implement snapshots at a single point in the system, ceph's
18 * distributed access to storage requires clients to help decide
19 * whether a write logically occurs before or after a recently created
22 * This provides a perfect instantanous client-wide snapshot. Between
23 * clients, however, snapshots may appear to be applied at slightly
24 * different points in time, depending on delays in delivering the
25 * snapshot notification.
27 * Snapshots are _not_ file system-wide. Instead, each snapshot
28 * applies to the subdirectory nested beneath some directory. This
29 * effectively divides the hierarchy into multiple "realms," where all
30 * of the files contained by each realm share the same set of
31 * snapshots. An individual realm's snap set contains snapshots
32 * explicitly created on that realm, as well as any snaps in its
33 * parent's snap set _after_ the point at which the parent became it's
34 * parent (due to, say, a rename). Similarly, snaps from prior parents
35 * during the time intervals during which they were the parent are included.
37 * The client is spared most of this detail, fortunately... it must only
38 * maintains a hierarchy of realms reflecting the current parent/child
39 * realm relationship, and for each realm has an explicit list of snaps
40 * inherited from prior parents.
42 * A snap_realm struct is maintained for realms containing every inode
43 * with an open cap in the system. (The needed snap realm information is
44 * provided by the MDS whenever a cap is issued, i.e., on open.) A 'seq'
45 * version number is used to ensure that as realm parameters change (new
46 * snapshot, new parent, etc.) the client's realm hierarchy is updated.
48 * The realm hierarchy drives the generation of a 'snap context' for each
49 * realm, which simply lists the resulting set of snaps for the realm. This
50 * is attached to any writes sent to OSDs.
53 * Unfortunately error handling is a bit mixed here. If we get a snap
54 * update, but don't have enough memory to update our realm hierarchy,
55 * it's not clear what we can do about it (besides complaining to the
61 * increase ref count for the realm
63 * caller must hold snap_rwsem.
65 void ceph_get_snap_realm(struct ceph_mds_client *mdsc,
66 struct ceph_snap_realm *realm)
68 lockdep_assert_held(&mdsc->snap_rwsem);
71 * The 0->1 and 1->0 transitions must take the snap_empty_lock
72 * atomically with the refcount change. Go ahead and bump the
73 * nref here, unless it's 0, in which case we take the spinlock
74 * and then do the increment and remove it from the list.
76 if (atomic_inc_not_zero(&realm->nref))
79 spin_lock(&mdsc->snap_empty_lock);
80 if (atomic_inc_return(&realm->nref) == 1)
81 list_del_init(&realm->empty_item);
82 spin_unlock(&mdsc->snap_empty_lock);
85 static void __insert_snap_realm(struct rb_root *root,
86 struct ceph_snap_realm *new)
88 struct rb_node **p = &root->rb_node;
89 struct rb_node *parent = NULL;
90 struct ceph_snap_realm *r = NULL;
94 r = rb_entry(parent, struct ceph_snap_realm, node);
95 if (new->ino < r->ino)
97 else if (new->ino > r->ino)
103 rb_link_node(&new->node, parent, p);
104 rb_insert_color(&new->node, root);
108 * create and get the realm rooted at @ino and bump its ref count.
110 * caller must hold snap_rwsem for write.
112 static struct ceph_snap_realm *ceph_create_snap_realm(
113 struct ceph_mds_client *mdsc,
116 struct ceph_snap_realm *realm;
118 lockdep_assert_held_write(&mdsc->snap_rwsem);
120 realm = kzalloc(sizeof(*realm), GFP_NOFS);
122 return ERR_PTR(-ENOMEM);
124 /* Do not release the global dummy snaprealm until unmouting */
125 if (ino == CEPH_INO_GLOBAL_SNAPREALM)
126 atomic_set(&realm->nref, 2);
128 atomic_set(&realm->nref, 1);
130 INIT_LIST_HEAD(&realm->children);
131 INIT_LIST_HEAD(&realm->child_item);
132 INIT_LIST_HEAD(&realm->empty_item);
133 INIT_LIST_HEAD(&realm->dirty_item);
134 INIT_LIST_HEAD(&realm->rebuild_item);
135 INIT_LIST_HEAD(&realm->inodes_with_caps);
136 spin_lock_init(&realm->inodes_with_caps_lock);
137 __insert_snap_realm(&mdsc->snap_realms, realm);
138 mdsc->num_snap_realms++;
140 dout("create_snap_realm %llx %p\n", realm->ino, realm);
145 * lookup the realm rooted at @ino.
147 * caller must hold snap_rwsem.
149 static struct ceph_snap_realm *__lookup_snap_realm(struct ceph_mds_client *mdsc,
152 struct rb_node *n = mdsc->snap_realms.rb_node;
153 struct ceph_snap_realm *r;
155 lockdep_assert_held(&mdsc->snap_rwsem);
158 r = rb_entry(n, struct ceph_snap_realm, node);
161 else if (ino > r->ino)
164 dout("lookup_snap_realm %llx %p\n", r->ino, r);
171 struct ceph_snap_realm *ceph_lookup_snap_realm(struct ceph_mds_client *mdsc,
174 struct ceph_snap_realm *r;
175 r = __lookup_snap_realm(mdsc, ino);
177 ceph_get_snap_realm(mdsc, r);
181 static void __put_snap_realm(struct ceph_mds_client *mdsc,
182 struct ceph_snap_realm *realm);
185 * called with snap_rwsem (write)
187 static void __destroy_snap_realm(struct ceph_mds_client *mdsc,
188 struct ceph_snap_realm *realm)
190 lockdep_assert_held_write(&mdsc->snap_rwsem);
192 dout("__destroy_snap_realm %p %llx\n", realm, realm->ino);
194 rb_erase(&realm->node, &mdsc->snap_realms);
195 mdsc->num_snap_realms--;
198 list_del_init(&realm->child_item);
199 __put_snap_realm(mdsc, realm->parent);
202 kfree(realm->prior_parent_snaps);
204 ceph_put_snap_context(realm->cached_context);
209 * caller holds snap_rwsem (write)
211 static void __put_snap_realm(struct ceph_mds_client *mdsc,
212 struct ceph_snap_realm *realm)
214 lockdep_assert_held_write(&mdsc->snap_rwsem);
217 * We do not require the snap_empty_lock here, as any caller that
218 * increments the value must hold the snap_rwsem.
220 if (atomic_dec_and_test(&realm->nref))
221 __destroy_snap_realm(mdsc, realm);
225 * See comments in ceph_get_snap_realm. Caller needn't hold any locks.
227 void ceph_put_snap_realm(struct ceph_mds_client *mdsc,
228 struct ceph_snap_realm *realm)
230 if (!atomic_dec_and_lock(&realm->nref, &mdsc->snap_empty_lock))
233 if (down_write_trylock(&mdsc->snap_rwsem)) {
234 spin_unlock(&mdsc->snap_empty_lock);
235 __destroy_snap_realm(mdsc, realm);
236 up_write(&mdsc->snap_rwsem);
238 list_add(&realm->empty_item, &mdsc->snap_empty);
239 spin_unlock(&mdsc->snap_empty_lock);
244 * Clean up any realms whose ref counts have dropped to zero. Note
245 * that this does not include realms who were created but not yet
248 * Called under snap_rwsem (write)
250 static void __cleanup_empty_realms(struct ceph_mds_client *mdsc)
252 struct ceph_snap_realm *realm;
254 lockdep_assert_held_write(&mdsc->snap_rwsem);
256 spin_lock(&mdsc->snap_empty_lock);
257 while (!list_empty(&mdsc->snap_empty)) {
258 realm = list_first_entry(&mdsc->snap_empty,
259 struct ceph_snap_realm, empty_item);
260 list_del(&realm->empty_item);
261 spin_unlock(&mdsc->snap_empty_lock);
262 __destroy_snap_realm(mdsc, realm);
263 spin_lock(&mdsc->snap_empty_lock);
265 spin_unlock(&mdsc->snap_empty_lock);
268 void ceph_cleanup_global_and_empty_realms(struct ceph_mds_client *mdsc)
270 struct ceph_snap_realm *global_realm;
272 down_write(&mdsc->snap_rwsem);
273 global_realm = __lookup_snap_realm(mdsc, CEPH_INO_GLOBAL_SNAPREALM);
275 ceph_put_snap_realm(mdsc, global_realm);
276 __cleanup_empty_realms(mdsc);
277 up_write(&mdsc->snap_rwsem);
281 * adjust the parent realm of a given @realm. adjust child list, and parent
282 * pointers, and ref counts appropriately.
284 * return true if parent was changed, 0 if unchanged, <0 on error.
286 * caller must hold snap_rwsem for write.
288 static int adjust_snap_realm_parent(struct ceph_mds_client *mdsc,
289 struct ceph_snap_realm *realm,
292 struct ceph_snap_realm *parent;
294 lockdep_assert_held_write(&mdsc->snap_rwsem);
296 if (realm->parent_ino == parentino)
299 parent = ceph_lookup_snap_realm(mdsc, parentino);
301 parent = ceph_create_snap_realm(mdsc, parentino);
303 return PTR_ERR(parent);
305 dout("adjust_snap_realm_parent %llx %p: %llx %p -> %llx %p\n",
306 realm->ino, realm, realm->parent_ino, realm->parent,
309 list_del_init(&realm->child_item);
310 ceph_put_snap_realm(mdsc, realm->parent);
312 realm->parent_ino = parentino;
313 realm->parent = parent;
314 list_add(&realm->child_item, &parent->children);
319 static int cmpu64_rev(const void *a, const void *b)
321 if (*(u64 *)a < *(u64 *)b)
323 if (*(u64 *)a > *(u64 *)b)
330 * build the snap context for a given realm.
332 static int build_snap_context(struct ceph_snap_realm *realm,
333 struct list_head *realm_queue,
334 struct list_head *dirty_realms)
336 struct ceph_snap_realm *parent = realm->parent;
337 struct ceph_snap_context *snapc;
339 u32 num = realm->num_prior_parent_snaps + realm->num_snaps;
342 * build parent context, if it hasn't been built.
343 * conservatively estimate that all parent snaps might be
347 if (!parent->cached_context) {
348 /* add to the queue head */
349 list_add(&parent->rebuild_item, realm_queue);
352 num += parent->cached_context->num_snaps;
355 /* do i actually need to update? not if my context seq
356 matches realm seq, and my parents' does to. (this works
357 because we rebuild_snap_realms() works _downward_ in
358 hierarchy after each update.) */
359 if (realm->cached_context &&
360 realm->cached_context->seq == realm->seq &&
362 realm->cached_context->seq >= parent->cached_context->seq)) {
363 dout("build_snap_context %llx %p: %p seq %lld (%u snaps)"
365 realm->ino, realm, realm->cached_context,
366 realm->cached_context->seq,
367 (unsigned int)realm->cached_context->num_snaps);
371 /* alloc new snap context */
373 if (num > (SIZE_MAX - sizeof(*snapc)) / sizeof(u64))
375 snapc = ceph_create_snap_context(num, GFP_NOFS);
379 /* build (reverse sorted) snap vector */
381 snapc->seq = realm->seq;
385 /* include any of parent's snaps occurring _after_ my
386 parent became my parent */
387 for (i = 0; i < parent->cached_context->num_snaps; i++)
388 if (parent->cached_context->snaps[i] >=
390 snapc->snaps[num++] =
391 parent->cached_context->snaps[i];
392 if (parent->cached_context->seq > snapc->seq)
393 snapc->seq = parent->cached_context->seq;
395 memcpy(snapc->snaps + num, realm->snaps,
396 sizeof(u64)*realm->num_snaps);
397 num += realm->num_snaps;
398 memcpy(snapc->snaps + num, realm->prior_parent_snaps,
399 sizeof(u64)*realm->num_prior_parent_snaps);
400 num += realm->num_prior_parent_snaps;
402 sort(snapc->snaps, num, sizeof(u64), cmpu64_rev, NULL);
403 snapc->num_snaps = num;
404 dout("build_snap_context %llx %p: %p seq %lld (%u snaps)\n",
405 realm->ino, realm, snapc, snapc->seq,
406 (unsigned int) snapc->num_snaps);
408 ceph_put_snap_context(realm->cached_context);
409 realm->cached_context = snapc;
410 /* queue realm for cap_snap creation */
411 list_add_tail(&realm->dirty_item, dirty_realms);
416 * if we fail, clear old (incorrect) cached_context... hopefully
417 * we'll have better luck building it later
419 if (realm->cached_context) {
420 ceph_put_snap_context(realm->cached_context);
421 realm->cached_context = NULL;
423 pr_err("build_snap_context %llx %p fail %d\n", realm->ino,
429 * rebuild snap context for the given realm and all of its children.
431 static void rebuild_snap_realms(struct ceph_snap_realm *realm,
432 struct list_head *dirty_realms)
434 LIST_HEAD(realm_queue);
438 list_add_tail(&realm->rebuild_item, &realm_queue);
440 while (!list_empty(&realm_queue)) {
441 struct ceph_snap_realm *_realm, *child;
443 _realm = list_first_entry(&realm_queue,
444 struct ceph_snap_realm,
448 * If the last building failed dues to memory
449 * issue, just empty the realm_queue and return
450 * to avoid infinite loop.
453 list_del_init(&_realm->rebuild_item);
457 last = build_snap_context(_realm, &realm_queue, dirty_realms);
458 dout("rebuild_snap_realms %llx %p, %s\n", _realm->ino, _realm,
459 last > 0 ? "is deferred" : !last ? "succeeded" : "failed");
461 /* is any child in the list ? */
462 list_for_each_entry(child, &_realm->children, child_item) {
463 if (!list_empty(&child->rebuild_item)) {
470 list_for_each_entry(child, &_realm->children, child_item)
471 list_add_tail(&child->rebuild_item, &realm_queue);
474 /* last == 1 means need to build parent first */
476 list_del_init(&_realm->rebuild_item);
482 * helper to allocate and decode an array of snapids. free prior
485 static int dup_array(u64 **dst, __le64 *src, u32 num)
491 *dst = kcalloc(num, sizeof(u64), GFP_NOFS);
494 for (i = 0; i < num; i++)
495 (*dst)[i] = get_unaligned_le64(src + i);
502 static bool has_new_snaps(struct ceph_snap_context *o,
503 struct ceph_snap_context *n)
505 if (n->num_snaps == 0)
507 /* snaps are in descending order */
508 return n->snaps[0] > o->seq;
512 * When a snapshot is applied, the size/mtime inode metadata is queued
513 * in a ceph_cap_snap (one for each snapshot) until writeback
514 * completes and the metadata can be flushed back to the MDS.
516 * However, if a (sync) write is currently in-progress when we apply
517 * the snapshot, we have to wait until the write succeeds or fails
518 * (and a final size/mtime is known). In this case the
519 * cap_snap->writing = 1, and is said to be "pending." When the write
520 * finishes, we __ceph_finish_cap_snap().
522 * Caller must hold snap_rwsem for read (i.e., the realm topology won't
525 static void ceph_queue_cap_snap(struct ceph_inode_info *ci,
526 struct ceph_cap_snap **pcapsnap)
528 struct inode *inode = &ci->vfs_inode;
529 struct ceph_snap_context *old_snapc, *new_snapc;
530 struct ceph_cap_snap *capsnap = *pcapsnap;
531 struct ceph_buffer *old_blob = NULL;
534 spin_lock(&ci->i_ceph_lock);
535 used = __ceph_caps_used(ci);
536 dirty = __ceph_caps_dirty(ci);
538 old_snapc = ci->i_head_snapc;
539 new_snapc = ci->i_snap_realm->cached_context;
542 * If there is a write in progress, treat that as a dirty Fw,
543 * even though it hasn't completed yet; by the time we finish
544 * up this capsnap it will be.
546 if (used & CEPH_CAP_FILE_WR)
547 dirty |= CEPH_CAP_FILE_WR;
549 if (__ceph_have_pending_cap_snap(ci)) {
550 /* there is no point in queuing multiple "pending" cap_snaps,
551 as no new writes are allowed to start when pending, so any
552 writes in progress now were started before the previous
553 cap_snap. lucky us. */
554 dout("queue_cap_snap %p already pending\n", inode);
557 if (ci->i_wrbuffer_ref_head == 0 &&
558 !(dirty & (CEPH_CAP_ANY_EXCL|CEPH_CAP_FILE_WR))) {
559 dout("queue_cap_snap %p nothing dirty|writing\n", inode);
566 * There is no need to send FLUSHSNAP message to MDS if there is
567 * no new snapshot. But when there is dirty pages or on-going
568 * writes, we still need to create cap_snap. cap_snap is needed
569 * by the write path and page writeback path.
571 * also see ceph_try_drop_cap_snap()
573 if (has_new_snaps(old_snapc, new_snapc)) {
574 if (dirty & (CEPH_CAP_ANY_EXCL|CEPH_CAP_FILE_WR))
575 capsnap->need_flush = true;
577 if (!(used & CEPH_CAP_FILE_WR) &&
578 ci->i_wrbuffer_ref_head == 0) {
579 dout("queue_cap_snap %p "
580 "no new_snap|dirty_page|writing\n", inode);
585 dout("queue_cap_snap %p cap_snap %p queuing under %p %s %s\n",
586 inode, capsnap, old_snapc, ceph_cap_string(dirty),
587 capsnap->need_flush ? "" : "no_flush");
590 capsnap->follows = old_snapc->seq;
591 capsnap->issued = __ceph_caps_issued(ci, NULL);
592 capsnap->dirty = dirty;
594 capsnap->mode = inode->i_mode;
595 capsnap->uid = inode->i_uid;
596 capsnap->gid = inode->i_gid;
598 if (dirty & CEPH_CAP_XATTR_EXCL) {
599 old_blob = __ceph_build_xattrs_blob(ci);
600 capsnap->xattr_blob =
601 ceph_buffer_get(ci->i_xattrs.blob);
602 capsnap->xattr_version = ci->i_xattrs.version;
604 capsnap->xattr_blob = NULL;
605 capsnap->xattr_version = 0;
608 capsnap->inline_data = ci->i_inline_version != CEPH_INLINE_NONE;
610 /* dirty page count moved from _head to this cap_snap;
611 all subsequent writes page dirties occur _after_ this
613 capsnap->dirty_pages = ci->i_wrbuffer_ref_head;
614 ci->i_wrbuffer_ref_head = 0;
615 capsnap->context = old_snapc;
616 list_add_tail(&capsnap->ci_item, &ci->i_cap_snaps);
618 if (used & CEPH_CAP_FILE_WR) {
619 dout("queue_cap_snap %p cap_snap %p snapc %p"
620 " seq %llu used WR, now pending\n", inode,
621 capsnap, old_snapc, old_snapc->seq);
622 capsnap->writing = 1;
624 /* note mtime, size NOW. */
625 __ceph_finish_cap_snap(ci, capsnap);
631 if (ci->i_wrbuffer_ref_head == 0 &&
633 ci->i_dirty_caps == 0 &&
634 ci->i_flushing_caps == 0) {
635 ci->i_head_snapc = NULL;
637 ci->i_head_snapc = ceph_get_snap_context(new_snapc);
638 dout(" new snapc is %p\n", new_snapc);
640 spin_unlock(&ci->i_ceph_lock);
642 ceph_buffer_put(old_blob);
643 ceph_put_snap_context(old_snapc);
647 * Finalize the size, mtime for a cap_snap.. that is, settle on final values
648 * to be used for the snapshot, to be flushed back to the mds.
650 * If capsnap can now be flushed, add to snap_flush list, and return 1.
652 * Caller must hold i_ceph_lock.
654 int __ceph_finish_cap_snap(struct ceph_inode_info *ci,
655 struct ceph_cap_snap *capsnap)
657 struct inode *inode = &ci->vfs_inode;
658 struct ceph_mds_client *mdsc = ceph_sb_to_mdsc(inode->i_sb);
660 BUG_ON(capsnap->writing);
661 capsnap->size = i_size_read(inode);
662 capsnap->mtime = inode->i_mtime;
663 capsnap->atime = inode->i_atime;
664 capsnap->ctime = inode->i_ctime;
665 capsnap->btime = ci->i_btime;
666 capsnap->change_attr = inode_peek_iversion_raw(inode);
667 capsnap->time_warp_seq = ci->i_time_warp_seq;
668 capsnap->truncate_size = ci->i_truncate_size;
669 capsnap->truncate_seq = ci->i_truncate_seq;
670 if (capsnap->dirty_pages) {
671 dout("finish_cap_snap %p cap_snap %p snapc %p %llu %s s=%llu "
672 "still has %d dirty pages\n", inode, capsnap,
673 capsnap->context, capsnap->context->seq,
674 ceph_cap_string(capsnap->dirty), capsnap->size,
675 capsnap->dirty_pages);
679 /* Fb cap still in use, delay it */
681 dout("finish_cap_snap %p cap_snap %p snapc %p %llu %s s=%llu "
682 "used WRBUFFER, delaying\n", inode, capsnap,
683 capsnap->context, capsnap->context->seq,
684 ceph_cap_string(capsnap->dirty), capsnap->size);
685 capsnap->writing = 1;
689 ci->i_ceph_flags |= CEPH_I_FLUSH_SNAPS;
690 dout("finish_cap_snap %p cap_snap %p snapc %p %llu %s s=%llu\n",
691 inode, capsnap, capsnap->context,
692 capsnap->context->seq, ceph_cap_string(capsnap->dirty),
695 spin_lock(&mdsc->snap_flush_lock);
696 if (list_empty(&ci->i_snap_flush_item))
697 list_add_tail(&ci->i_snap_flush_item, &mdsc->snap_flush_list);
698 spin_unlock(&mdsc->snap_flush_lock);
699 return 1; /* caller may want to ceph_flush_snaps */
703 * Queue cap_snaps for snap writeback for this realm and its children.
704 * Called under snap_rwsem, so realm topology won't change.
706 static void queue_realm_cap_snaps(struct ceph_snap_realm *realm)
708 struct ceph_inode_info *ci;
709 struct inode *lastinode = NULL;
710 struct ceph_cap_snap *capsnap = NULL;
712 dout("queue_realm_cap_snaps %p %llx inodes\n", realm, realm->ino);
714 spin_lock(&realm->inodes_with_caps_lock);
715 list_for_each_entry(ci, &realm->inodes_with_caps, i_snap_realm_item) {
716 struct inode *inode = igrab(&ci->vfs_inode);
719 spin_unlock(&realm->inodes_with_caps_lock);
724 * Allocate the capsnap memory outside of ceph_queue_cap_snap()
725 * to reduce very possible but unnecessary frequently memory
726 * allocate/free in this loop.
729 capsnap = kmem_cache_zalloc(ceph_cap_snap_cachep, GFP_NOFS);
731 pr_err("ENOMEM allocating ceph_cap_snap on %p\n",
736 capsnap->cap_flush.is_capsnap = true;
737 refcount_set(&capsnap->nref, 1);
738 INIT_LIST_HEAD(&capsnap->cap_flush.i_list);
739 INIT_LIST_HEAD(&capsnap->cap_flush.g_list);
740 INIT_LIST_HEAD(&capsnap->ci_item);
742 ceph_queue_cap_snap(ci, &capsnap);
743 spin_lock(&realm->inodes_with_caps_lock);
745 spin_unlock(&realm->inodes_with_caps_lock);
749 kmem_cache_free(ceph_cap_snap_cachep, capsnap);
750 dout("queue_realm_cap_snaps %p %llx done\n", realm, realm->ino);
754 * Parse and apply a snapblob "snap trace" from the MDS. This specifies
755 * the snap realm parameters from a given realm and all of its ancestors,
758 * Caller must hold snap_rwsem for write.
760 int ceph_update_snap_trace(struct ceph_mds_client *mdsc,
761 void *p, void *e, bool deletion,
762 struct ceph_snap_realm **realm_ret)
764 struct ceph_mds_snap_realm *ri; /* encoded */
765 __le64 *snaps; /* encoded */
766 __le64 *prior_parent_snaps; /* encoded */
767 struct ceph_snap_realm *realm = NULL;
768 struct ceph_snap_realm *first_realm = NULL;
769 struct ceph_snap_realm *realm_to_rebuild = NULL;
772 LIST_HEAD(dirty_realms);
774 lockdep_assert_held_write(&mdsc->snap_rwsem);
776 dout("update_snap_trace deletion=%d\n", deletion);
779 ceph_decode_need(&p, e, sizeof(*ri), bad);
782 ceph_decode_need(&p, e, sizeof(u64)*(le32_to_cpu(ri->num_snaps) +
783 le32_to_cpu(ri->num_prior_parent_snaps)), bad);
785 p += sizeof(u64) * le32_to_cpu(ri->num_snaps);
786 prior_parent_snaps = p;
787 p += sizeof(u64) * le32_to_cpu(ri->num_prior_parent_snaps);
789 realm = ceph_lookup_snap_realm(mdsc, le64_to_cpu(ri->ino));
791 realm = ceph_create_snap_realm(mdsc, le64_to_cpu(ri->ino));
793 err = PTR_ERR(realm);
798 /* ensure the parent is correct */
799 err = adjust_snap_realm_parent(mdsc, realm, le64_to_cpu(ri->parent));
802 rebuild_snapcs += err;
804 if (le64_to_cpu(ri->seq) > realm->seq) {
805 dout("update_snap_trace updating %llx %p %lld -> %lld\n",
806 realm->ino, realm, realm->seq, le64_to_cpu(ri->seq));
807 /* update realm parameters, snap lists */
808 realm->seq = le64_to_cpu(ri->seq);
809 realm->created = le64_to_cpu(ri->created);
810 realm->parent_since = le64_to_cpu(ri->parent_since);
812 realm->num_snaps = le32_to_cpu(ri->num_snaps);
813 err = dup_array(&realm->snaps, snaps, realm->num_snaps);
817 realm->num_prior_parent_snaps =
818 le32_to_cpu(ri->num_prior_parent_snaps);
819 err = dup_array(&realm->prior_parent_snaps, prior_parent_snaps,
820 realm->num_prior_parent_snaps);
824 if (realm->seq > mdsc->last_snap_seq)
825 mdsc->last_snap_seq = realm->seq;
828 } else if (!realm->cached_context) {
829 dout("update_snap_trace %llx %p seq %lld new\n",
830 realm->ino, realm, realm->seq);
833 dout("update_snap_trace %llx %p seq %lld unchanged\n",
834 realm->ino, realm, realm->seq);
837 dout("done with %llx %p, rebuild_snapcs=%d, %p %p\n", realm->ino,
838 realm, rebuild_snapcs, p, e);
841 * this will always track the uppest parent realm from which
842 * we need to rebuild the snapshot contexts _downward_ in
846 realm_to_rebuild = realm;
848 /* rebuild_snapcs when we reach the _end_ (root) of the trace */
849 if (realm_to_rebuild && p >= e)
850 rebuild_snap_realms(realm_to_rebuild, &dirty_realms);
855 ceph_put_snap_realm(mdsc, realm);
861 * queue cap snaps _after_ we've built the new snap contexts,
862 * so that i_head_snapc can be set appropriately.
864 while (!list_empty(&dirty_realms)) {
865 realm = list_first_entry(&dirty_realms, struct ceph_snap_realm,
867 list_del_init(&realm->dirty_item);
868 queue_realm_cap_snaps(realm);
872 *realm_ret = first_realm;
874 ceph_put_snap_realm(mdsc, first_realm);
876 __cleanup_empty_realms(mdsc);
882 if (realm && !IS_ERR(realm))
883 ceph_put_snap_realm(mdsc, realm);
885 ceph_put_snap_realm(mdsc, first_realm);
886 pr_err("update_snap_trace error %d\n", err);
892 * Send any cap_snaps that are queued for flush. Try to carry
893 * s_mutex across multiple snap flushes to avoid locking overhead.
895 * Caller holds no locks.
897 static void flush_snaps(struct ceph_mds_client *mdsc)
899 struct ceph_inode_info *ci;
901 struct ceph_mds_session *session = NULL;
903 dout("flush_snaps\n");
904 spin_lock(&mdsc->snap_flush_lock);
905 while (!list_empty(&mdsc->snap_flush_list)) {
906 ci = list_first_entry(&mdsc->snap_flush_list,
907 struct ceph_inode_info, i_snap_flush_item);
908 inode = &ci->vfs_inode;
910 spin_unlock(&mdsc->snap_flush_lock);
911 ceph_flush_snaps(ci, &session);
913 spin_lock(&mdsc->snap_flush_lock);
915 spin_unlock(&mdsc->snap_flush_lock);
917 ceph_put_mds_session(session);
918 dout("flush_snaps done\n");
922 * ceph_change_snap_realm - change the snap_realm for an inode
923 * @inode: inode to move to new snap realm
924 * @realm: new realm to move inode into (may be NULL)
926 * Detach an inode from its old snaprealm (if any) and attach it to
927 * the new snaprealm (if any). The old snap realm reference held by
928 * the inode is put. If realm is non-NULL, then the caller's reference
929 * to it is taken over by the inode.
931 void ceph_change_snap_realm(struct inode *inode, struct ceph_snap_realm *realm)
933 struct ceph_inode_info *ci = ceph_inode(inode);
934 struct ceph_mds_client *mdsc = ceph_inode_to_client(inode)->mdsc;
935 struct ceph_snap_realm *oldrealm = ci->i_snap_realm;
937 lockdep_assert_held(&ci->i_ceph_lock);
940 spin_lock(&oldrealm->inodes_with_caps_lock);
941 list_del_init(&ci->i_snap_realm_item);
942 if (oldrealm->ino == ci->i_vino.ino)
943 oldrealm->inode = NULL;
944 spin_unlock(&oldrealm->inodes_with_caps_lock);
945 ceph_put_snap_realm(mdsc, oldrealm);
948 ci->i_snap_realm = realm;
951 spin_lock(&realm->inodes_with_caps_lock);
952 list_add(&ci->i_snap_realm_item, &realm->inodes_with_caps);
953 if (realm->ino == ci->i_vino.ino)
954 realm->inode = inode;
955 spin_unlock(&realm->inodes_with_caps_lock);
960 * Handle a snap notification from the MDS.
962 * This can take two basic forms: the simplest is just a snap creation
963 * or deletion notification on an existing realm. This should update the
964 * realm and its children.
966 * The more difficult case is realm creation, due to snap creation at a
967 * new point in the file hierarchy, or due to a rename that moves a file or
968 * directory into another realm.
970 void ceph_handle_snap(struct ceph_mds_client *mdsc,
971 struct ceph_mds_session *session,
972 struct ceph_msg *msg)
974 struct super_block *sb = mdsc->fsc->sb;
975 int mds = session->s_mds;
979 struct ceph_snap_realm *realm = NULL;
980 void *p = msg->front.iov_base;
981 void *e = p + msg->front.iov_len;
982 struct ceph_mds_snap_head *h;
983 int num_split_inos, num_split_realms;
984 __le64 *split_inos = NULL, *split_realms = NULL;
986 int locked_rwsem = 0;
989 if (msg->front.iov_len < sizeof(*h))
992 op = le32_to_cpu(h->op);
993 split = le64_to_cpu(h->split); /* non-zero if we are splitting an
995 num_split_inos = le32_to_cpu(h->num_split_inos);
996 num_split_realms = le32_to_cpu(h->num_split_realms);
997 trace_len = le32_to_cpu(h->trace_len);
1000 dout("handle_snap from mds%d op %s split %llx tracelen %d\n", mds,
1001 ceph_snap_op_name(op), split, trace_len);
1003 mutex_lock(&session->s_mutex);
1004 inc_session_sequence(session);
1005 mutex_unlock(&session->s_mutex);
1007 down_write(&mdsc->snap_rwsem);
1010 if (op == CEPH_SNAP_OP_SPLIT) {
1011 struct ceph_mds_snap_realm *ri;
1014 * A "split" breaks part of an existing realm off into
1015 * a new realm. The MDS provides a list of inodes
1016 * (with caps) and child realms that belong to the new
1020 p += sizeof(u64) * num_split_inos;
1022 p += sizeof(u64) * num_split_realms;
1023 ceph_decode_need(&p, e, sizeof(*ri), bad);
1024 /* we will peek at realm info here, but will _not_
1025 * advance p, as the realm update will occur below in
1026 * ceph_update_snap_trace. */
1029 realm = ceph_lookup_snap_realm(mdsc, split);
1031 realm = ceph_create_snap_realm(mdsc, split);
1036 dout("splitting snap_realm %llx %p\n", realm->ino, realm);
1037 for (i = 0; i < num_split_inos; i++) {
1038 struct ceph_vino vino = {
1039 .ino = le64_to_cpu(split_inos[i]),
1040 .snap = CEPH_NOSNAP,
1042 struct inode *inode = ceph_find_inode(sb, vino);
1043 struct ceph_inode_info *ci;
1047 ci = ceph_inode(inode);
1049 spin_lock(&ci->i_ceph_lock);
1050 if (!ci->i_snap_realm)
1053 * If this inode belongs to a realm that was
1054 * created after our new realm, we experienced
1055 * a race (due to another split notifications
1056 * arriving from a different MDS). So skip
1059 if (ci->i_snap_realm->created >
1060 le64_to_cpu(ri->created)) {
1061 dout(" leaving %p in newer realm %llx %p\n",
1062 inode, ci->i_snap_realm->ino,
1066 dout(" will move %p to split realm %llx %p\n",
1067 inode, realm->ino, realm);
1069 ceph_get_snap_realm(mdsc, realm);
1070 ceph_change_snap_realm(inode, realm);
1071 spin_unlock(&ci->i_ceph_lock);
1076 spin_unlock(&ci->i_ceph_lock);
1080 /* we may have taken some of the old realm's children. */
1081 for (i = 0; i < num_split_realms; i++) {
1082 struct ceph_snap_realm *child =
1083 __lookup_snap_realm(mdsc,
1084 le64_to_cpu(split_realms[i]));
1087 adjust_snap_realm_parent(mdsc, child, realm->ino);
1092 * update using the provided snap trace. if we are deleting a
1093 * snap, we can avoid queueing cap_snaps.
1095 ceph_update_snap_trace(mdsc, p, e,
1096 op == CEPH_SNAP_OP_DESTROY, NULL);
1098 if (op == CEPH_SNAP_OP_SPLIT)
1099 /* we took a reference when we created the realm, above */
1100 ceph_put_snap_realm(mdsc, realm);
1102 __cleanup_empty_realms(mdsc);
1104 up_write(&mdsc->snap_rwsem);
1110 pr_err("corrupt snap message from mds%d\n", mds);
1114 up_write(&mdsc->snap_rwsem);
1118 struct ceph_snapid_map* ceph_get_snapid_map(struct ceph_mds_client *mdsc,
1121 struct ceph_snapid_map *sm, *exist;
1122 struct rb_node **p, *parent;
1126 spin_lock(&mdsc->snapid_map_lock);
1127 p = &mdsc->snapid_map_tree.rb_node;
1129 exist = rb_entry(*p, struct ceph_snapid_map, node);
1130 if (snap > exist->snap) {
1132 } else if (snap < exist->snap) {
1133 p = &(*p)->rb_right;
1135 if (atomic_inc_return(&exist->ref) == 1)
1136 list_del_init(&exist->lru);
1141 spin_unlock(&mdsc->snapid_map_lock);
1143 dout("found snapid map %llx -> %x\n", exist->snap, exist->dev);
1147 sm = kmalloc(sizeof(*sm), GFP_NOFS);
1151 ret = get_anon_bdev(&sm->dev);
1157 INIT_LIST_HEAD(&sm->lru);
1158 atomic_set(&sm->ref, 1);
1163 p = &mdsc->snapid_map_tree.rb_node;
1164 spin_lock(&mdsc->snapid_map_lock);
1167 exist = rb_entry(*p, struct ceph_snapid_map, node);
1168 if (snap > exist->snap)
1170 else if (snap < exist->snap)
1171 p = &(*p)->rb_right;
1177 if (atomic_inc_return(&exist->ref) == 1)
1178 list_del_init(&exist->lru);
1180 rb_link_node(&sm->node, parent, p);
1181 rb_insert_color(&sm->node, &mdsc->snapid_map_tree);
1183 spin_unlock(&mdsc->snapid_map_lock);
1185 free_anon_bdev(sm->dev);
1187 dout("found snapid map %llx -> %x\n", exist->snap, exist->dev);
1191 dout("create snapid map %llx -> %x\n", sm->snap, sm->dev);
1195 void ceph_put_snapid_map(struct ceph_mds_client* mdsc,
1196 struct ceph_snapid_map *sm)
1200 if (atomic_dec_and_lock(&sm->ref, &mdsc->snapid_map_lock)) {
1201 if (!RB_EMPTY_NODE(&sm->node)) {
1202 sm->last_used = jiffies;
1203 list_add_tail(&sm->lru, &mdsc->snapid_map_lru);
1204 spin_unlock(&mdsc->snapid_map_lock);
1206 /* already cleaned up by
1207 * ceph_cleanup_snapid_map() */
1208 spin_unlock(&mdsc->snapid_map_lock);
1214 void ceph_trim_snapid_map(struct ceph_mds_client *mdsc)
1216 struct ceph_snapid_map *sm;
1220 spin_lock(&mdsc->snapid_map_lock);
1223 while (!list_empty(&mdsc->snapid_map_lru)) {
1224 sm = list_first_entry(&mdsc->snapid_map_lru,
1225 struct ceph_snapid_map, lru);
1226 if (time_after(sm->last_used + CEPH_SNAPID_MAP_TIMEOUT, now))
1229 rb_erase(&sm->node, &mdsc->snapid_map_tree);
1230 list_move(&sm->lru, &to_free);
1232 spin_unlock(&mdsc->snapid_map_lock);
1234 while (!list_empty(&to_free)) {
1235 sm = list_first_entry(&to_free, struct ceph_snapid_map, lru);
1237 dout("trim snapid map %llx -> %x\n", sm->snap, sm->dev);
1238 free_anon_bdev(sm->dev);
1243 void ceph_cleanup_snapid_map(struct ceph_mds_client *mdsc)
1245 struct ceph_snapid_map *sm;
1249 spin_lock(&mdsc->snapid_map_lock);
1250 while ((p = rb_first(&mdsc->snapid_map_tree))) {
1251 sm = rb_entry(p, struct ceph_snapid_map, node);
1252 rb_erase(p, &mdsc->snapid_map_tree);
1254 list_move(&sm->lru, &to_free);
1256 spin_unlock(&mdsc->snapid_map_lock);
1258 while (!list_empty(&to_free)) {
1259 sm = list_first_entry(&to_free, struct ceph_snapid_map, lru);
1261 free_anon_bdev(sm->dev);
1262 if (WARN_ON_ONCE(atomic_read(&sm->ref))) {
1263 pr_err("snapid map %llx -> %x still in use\n",