1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/ceph/ceph_debug.h>
4 #include <linux/sort.h>
5 #include <linux/slab.h>
7 #include "mds_client.h"
8 #include <linux/ceph/decode.h>
10 /* unused map expires after 5 minutes */
11 #define CEPH_SNAPID_MAP_TIMEOUT (5 * 60 * HZ)
14 * Snapshots in ceph are driven in large part by cooperation from the
15 * client. In contrast to local file systems or file servers that
16 * implement snapshots at a single point in the system, ceph's
17 * distributed access to storage requires clients to help decide
18 * whether a write logically occurs before or after a recently created
21 * This provides a perfect instantanous client-wide snapshot. Between
22 * clients, however, snapshots may appear to be applied at slightly
23 * different points in time, depending on delays in delivering the
24 * snapshot notification.
26 * Snapshots are _not_ file system-wide. Instead, each snapshot
27 * applies to the subdirectory nested beneath some directory. This
28 * effectively divides the hierarchy into multiple "realms," where all
29 * of the files contained by each realm share the same set of
30 * snapshots. An individual realm's snap set contains snapshots
31 * explicitly created on that realm, as well as any snaps in its
32 * parent's snap set _after_ the point at which the parent became it's
33 * parent (due to, say, a rename). Similarly, snaps from prior parents
34 * during the time intervals during which they were the parent are included.
36 * The client is spared most of this detail, fortunately... it must only
37 * maintains a hierarchy of realms reflecting the current parent/child
38 * realm relationship, and for each realm has an explicit list of snaps
39 * inherited from prior parents.
41 * A snap_realm struct is maintained for realms containing every inode
42 * with an open cap in the system. (The needed snap realm information is
43 * provided by the MDS whenever a cap is issued, i.e., on open.) A 'seq'
44 * version number is used to ensure that as realm parameters change (new
45 * snapshot, new parent, etc.) the client's realm hierarchy is updated.
47 * The realm hierarchy drives the generation of a 'snap context' for each
48 * realm, which simply lists the resulting set of snaps for the realm. This
49 * is attached to any writes sent to OSDs.
52 * Unfortunately error handling is a bit mixed here. If we get a snap
53 * update, but don't have enough memory to update our realm hierarchy,
54 * it's not clear what we can do about it (besides complaining to the
60 * increase ref count for the realm
62 * caller must hold snap_rwsem for write.
64 void ceph_get_snap_realm(struct ceph_mds_client *mdsc,
65 struct ceph_snap_realm *realm)
67 dout("get_realm %p %d -> %d\n", realm,
68 atomic_read(&realm->nref), atomic_read(&realm->nref)+1);
70 * since we _only_ increment realm refs or empty the empty
71 * list with snap_rwsem held, adjusting the empty list here is
72 * safe. we do need to protect against concurrent empty list
75 if (atomic_inc_return(&realm->nref) == 1) {
76 spin_lock(&mdsc->snap_empty_lock);
77 list_del_init(&realm->empty_item);
78 spin_unlock(&mdsc->snap_empty_lock);
82 static void __insert_snap_realm(struct rb_root *root,
83 struct ceph_snap_realm *new)
85 struct rb_node **p = &root->rb_node;
86 struct rb_node *parent = NULL;
87 struct ceph_snap_realm *r = NULL;
91 r = rb_entry(parent, struct ceph_snap_realm, node);
92 if (new->ino < r->ino)
94 else if (new->ino > r->ino)
100 rb_link_node(&new->node, parent, p);
101 rb_insert_color(&new->node, root);
105 * create and get the realm rooted at @ino and bump its ref count.
107 * caller must hold snap_rwsem for write.
109 static struct ceph_snap_realm *ceph_create_snap_realm(
110 struct ceph_mds_client *mdsc,
113 struct ceph_snap_realm *realm;
115 realm = kzalloc(sizeof(*realm), GFP_NOFS);
117 return ERR_PTR(-ENOMEM);
119 atomic_set(&realm->nref, 1); /* for caller */
121 INIT_LIST_HEAD(&realm->children);
122 INIT_LIST_HEAD(&realm->child_item);
123 INIT_LIST_HEAD(&realm->empty_item);
124 INIT_LIST_HEAD(&realm->dirty_item);
125 INIT_LIST_HEAD(&realm->inodes_with_caps);
126 spin_lock_init(&realm->inodes_with_caps_lock);
127 __insert_snap_realm(&mdsc->snap_realms, realm);
128 mdsc->num_snap_realms++;
130 dout("create_snap_realm %llx %p\n", realm->ino, realm);
135 * lookup the realm rooted at @ino.
137 * caller must hold snap_rwsem for write.
139 static struct ceph_snap_realm *__lookup_snap_realm(struct ceph_mds_client *mdsc,
142 struct rb_node *n = mdsc->snap_realms.rb_node;
143 struct ceph_snap_realm *r;
146 r = rb_entry(n, struct ceph_snap_realm, node);
149 else if (ino > r->ino)
152 dout("lookup_snap_realm %llx %p\n", r->ino, r);
159 struct ceph_snap_realm *ceph_lookup_snap_realm(struct ceph_mds_client *mdsc,
162 struct ceph_snap_realm *r;
163 r = __lookup_snap_realm(mdsc, ino);
165 ceph_get_snap_realm(mdsc, r);
169 static void __put_snap_realm(struct ceph_mds_client *mdsc,
170 struct ceph_snap_realm *realm);
173 * called with snap_rwsem (write)
175 static void __destroy_snap_realm(struct ceph_mds_client *mdsc,
176 struct ceph_snap_realm *realm)
178 dout("__destroy_snap_realm %p %llx\n", realm, realm->ino);
180 rb_erase(&realm->node, &mdsc->snap_realms);
181 mdsc->num_snap_realms--;
184 list_del_init(&realm->child_item);
185 __put_snap_realm(mdsc, realm->parent);
188 kfree(realm->prior_parent_snaps);
190 ceph_put_snap_context(realm->cached_context);
195 * caller holds snap_rwsem (write)
197 static void __put_snap_realm(struct ceph_mds_client *mdsc,
198 struct ceph_snap_realm *realm)
200 dout("__put_snap_realm %llx %p %d -> %d\n", realm->ino, realm,
201 atomic_read(&realm->nref), atomic_read(&realm->nref)-1);
202 if (atomic_dec_and_test(&realm->nref))
203 __destroy_snap_realm(mdsc, realm);
207 * caller needn't hold any locks
209 void ceph_put_snap_realm(struct ceph_mds_client *mdsc,
210 struct ceph_snap_realm *realm)
212 dout("put_snap_realm %llx %p %d -> %d\n", realm->ino, realm,
213 atomic_read(&realm->nref), atomic_read(&realm->nref)-1);
214 if (!atomic_dec_and_test(&realm->nref))
217 if (down_write_trylock(&mdsc->snap_rwsem)) {
218 __destroy_snap_realm(mdsc, realm);
219 up_write(&mdsc->snap_rwsem);
221 spin_lock(&mdsc->snap_empty_lock);
222 list_add(&realm->empty_item, &mdsc->snap_empty);
223 spin_unlock(&mdsc->snap_empty_lock);
228 * Clean up any realms whose ref counts have dropped to zero. Note
229 * that this does not include realms who were created but not yet
232 * Called under snap_rwsem (write)
234 static void __cleanup_empty_realms(struct ceph_mds_client *mdsc)
236 struct ceph_snap_realm *realm;
238 spin_lock(&mdsc->snap_empty_lock);
239 while (!list_empty(&mdsc->snap_empty)) {
240 realm = list_first_entry(&mdsc->snap_empty,
241 struct ceph_snap_realm, empty_item);
242 list_del(&realm->empty_item);
243 spin_unlock(&mdsc->snap_empty_lock);
244 __destroy_snap_realm(mdsc, realm);
245 spin_lock(&mdsc->snap_empty_lock);
247 spin_unlock(&mdsc->snap_empty_lock);
250 void ceph_cleanup_empty_realms(struct ceph_mds_client *mdsc)
252 down_write(&mdsc->snap_rwsem);
253 __cleanup_empty_realms(mdsc);
254 up_write(&mdsc->snap_rwsem);
258 * adjust the parent realm of a given @realm. adjust child list, and parent
259 * pointers, and ref counts appropriately.
261 * return true if parent was changed, 0 if unchanged, <0 on error.
263 * caller must hold snap_rwsem for write.
265 static int adjust_snap_realm_parent(struct ceph_mds_client *mdsc,
266 struct ceph_snap_realm *realm,
269 struct ceph_snap_realm *parent;
271 if (realm->parent_ino == parentino)
274 parent = ceph_lookup_snap_realm(mdsc, parentino);
276 parent = ceph_create_snap_realm(mdsc, parentino);
278 return PTR_ERR(parent);
280 dout("adjust_snap_realm_parent %llx %p: %llx %p -> %llx %p\n",
281 realm->ino, realm, realm->parent_ino, realm->parent,
284 list_del_init(&realm->child_item);
285 ceph_put_snap_realm(mdsc, realm->parent);
287 realm->parent_ino = parentino;
288 realm->parent = parent;
289 list_add(&realm->child_item, &parent->children);
294 static int cmpu64_rev(const void *a, const void *b)
296 if (*(u64 *)a < *(u64 *)b)
298 if (*(u64 *)a > *(u64 *)b)
305 * build the snap context for a given realm.
307 static int build_snap_context(struct ceph_snap_realm *realm,
308 struct list_head* dirty_realms)
310 struct ceph_snap_realm *parent = realm->parent;
311 struct ceph_snap_context *snapc;
313 u32 num = realm->num_prior_parent_snaps + realm->num_snaps;
316 * build parent context, if it hasn't been built.
317 * conservatively estimate that all parent snaps might be
321 if (!parent->cached_context) {
322 err = build_snap_context(parent, dirty_realms);
326 num += parent->cached_context->num_snaps;
329 /* do i actually need to update? not if my context seq
330 matches realm seq, and my parents' does to. (this works
331 because we rebuild_snap_realms() works _downward_ in
332 hierarchy after each update.) */
333 if (realm->cached_context &&
334 realm->cached_context->seq == realm->seq &&
336 realm->cached_context->seq >= parent->cached_context->seq)) {
337 dout("build_snap_context %llx %p: %p seq %lld (%u snaps)"
339 realm->ino, realm, realm->cached_context,
340 realm->cached_context->seq,
341 (unsigned int)realm->cached_context->num_snaps);
345 /* alloc new snap context */
347 if (num > (SIZE_MAX - sizeof(*snapc)) / sizeof(u64))
349 snapc = ceph_create_snap_context(num, GFP_NOFS);
353 /* build (reverse sorted) snap vector */
355 snapc->seq = realm->seq;
359 /* include any of parent's snaps occurring _after_ my
360 parent became my parent */
361 for (i = 0; i < parent->cached_context->num_snaps; i++)
362 if (parent->cached_context->snaps[i] >=
364 snapc->snaps[num++] =
365 parent->cached_context->snaps[i];
366 if (parent->cached_context->seq > snapc->seq)
367 snapc->seq = parent->cached_context->seq;
369 memcpy(snapc->snaps + num, realm->snaps,
370 sizeof(u64)*realm->num_snaps);
371 num += realm->num_snaps;
372 memcpy(snapc->snaps + num, realm->prior_parent_snaps,
373 sizeof(u64)*realm->num_prior_parent_snaps);
374 num += realm->num_prior_parent_snaps;
376 sort(snapc->snaps, num, sizeof(u64), cmpu64_rev, NULL);
377 snapc->num_snaps = num;
378 dout("build_snap_context %llx %p: %p seq %lld (%u snaps)\n",
379 realm->ino, realm, snapc, snapc->seq,
380 (unsigned int) snapc->num_snaps);
382 ceph_put_snap_context(realm->cached_context);
383 realm->cached_context = snapc;
384 /* queue realm for cap_snap creation */
385 list_add_tail(&realm->dirty_item, dirty_realms);
390 * if we fail, clear old (incorrect) cached_context... hopefully
391 * we'll have better luck building it later
393 if (realm->cached_context) {
394 ceph_put_snap_context(realm->cached_context);
395 realm->cached_context = NULL;
397 pr_err("build_snap_context %llx %p fail %d\n", realm->ino,
403 * rebuild snap context for the given realm and all of its children.
405 static void rebuild_snap_realms(struct ceph_snap_realm *realm,
406 struct list_head *dirty_realms)
408 struct ceph_snap_realm *child;
410 dout("rebuild_snap_realms %llx %p\n", realm->ino, realm);
411 build_snap_context(realm, dirty_realms);
413 list_for_each_entry(child, &realm->children, child_item)
414 rebuild_snap_realms(child, dirty_realms);
419 * helper to allocate and decode an array of snapids. free prior
422 static int dup_array(u64 **dst, __le64 *src, u32 num)
428 *dst = kcalloc(num, sizeof(u64), GFP_NOFS);
431 for (i = 0; i < num; i++)
432 (*dst)[i] = get_unaligned_le64(src + i);
439 static bool has_new_snaps(struct ceph_snap_context *o,
440 struct ceph_snap_context *n)
442 if (n->num_snaps == 0)
444 /* snaps are in descending order */
445 return n->snaps[0] > o->seq;
449 * When a snapshot is applied, the size/mtime inode metadata is queued
450 * in a ceph_cap_snap (one for each snapshot) until writeback
451 * completes and the metadata can be flushed back to the MDS.
453 * However, if a (sync) write is currently in-progress when we apply
454 * the snapshot, we have to wait until the write succeeds or fails
455 * (and a final size/mtime is known). In this case the
456 * cap_snap->writing = 1, and is said to be "pending." When the write
457 * finishes, we __ceph_finish_cap_snap().
459 * Caller must hold snap_rwsem for read (i.e., the realm topology won't
462 void ceph_queue_cap_snap(struct ceph_inode_info *ci)
464 struct inode *inode = &ci->vfs_inode;
465 struct ceph_cap_snap *capsnap;
466 struct ceph_snap_context *old_snapc, *new_snapc;
469 capsnap = kzalloc(sizeof(*capsnap), GFP_NOFS);
471 pr_err("ENOMEM allocating ceph_cap_snap on %p\n", inode);
475 spin_lock(&ci->i_ceph_lock);
476 used = __ceph_caps_used(ci);
477 dirty = __ceph_caps_dirty(ci);
479 old_snapc = ci->i_head_snapc;
480 new_snapc = ci->i_snap_realm->cached_context;
483 * If there is a write in progress, treat that as a dirty Fw,
484 * even though it hasn't completed yet; by the time we finish
485 * up this capsnap it will be.
487 if (used & CEPH_CAP_FILE_WR)
488 dirty |= CEPH_CAP_FILE_WR;
490 if (__ceph_have_pending_cap_snap(ci)) {
491 /* there is no point in queuing multiple "pending" cap_snaps,
492 as no new writes are allowed to start when pending, so any
493 writes in progress now were started before the previous
494 cap_snap. lucky us. */
495 dout("queue_cap_snap %p already pending\n", inode);
498 if (ci->i_wrbuffer_ref_head == 0 &&
499 !(dirty & (CEPH_CAP_ANY_EXCL|CEPH_CAP_FILE_WR))) {
500 dout("queue_cap_snap %p nothing dirty|writing\n", inode);
507 * There is no need to send FLUSHSNAP message to MDS if there is
508 * no new snapshot. But when there is dirty pages or on-going
509 * writes, we still need to create cap_snap. cap_snap is needed
510 * by the write path and page writeback path.
512 * also see ceph_try_drop_cap_snap()
514 if (has_new_snaps(old_snapc, new_snapc)) {
515 if (dirty & (CEPH_CAP_ANY_EXCL|CEPH_CAP_FILE_WR))
516 capsnap->need_flush = true;
518 if (!(used & CEPH_CAP_FILE_WR) &&
519 ci->i_wrbuffer_ref_head == 0) {
520 dout("queue_cap_snap %p "
521 "no new_snap|dirty_page|writing\n", inode);
526 dout("queue_cap_snap %p cap_snap %p queuing under %p %s %s\n",
527 inode, capsnap, old_snapc, ceph_cap_string(dirty),
528 capsnap->need_flush ? "" : "no_flush");
531 refcount_set(&capsnap->nref, 1);
532 INIT_LIST_HEAD(&capsnap->ci_item);
534 capsnap->follows = old_snapc->seq;
535 capsnap->issued = __ceph_caps_issued(ci, NULL);
536 capsnap->dirty = dirty;
538 capsnap->mode = inode->i_mode;
539 capsnap->uid = inode->i_uid;
540 capsnap->gid = inode->i_gid;
542 if (dirty & CEPH_CAP_XATTR_EXCL) {
543 __ceph_build_xattrs_blob(ci);
544 capsnap->xattr_blob =
545 ceph_buffer_get(ci->i_xattrs.blob);
546 capsnap->xattr_version = ci->i_xattrs.version;
548 capsnap->xattr_blob = NULL;
549 capsnap->xattr_version = 0;
552 capsnap->inline_data = ci->i_inline_version != CEPH_INLINE_NONE;
554 /* dirty page count moved from _head to this cap_snap;
555 all subsequent writes page dirties occur _after_ this
557 capsnap->dirty_pages = ci->i_wrbuffer_ref_head;
558 ci->i_wrbuffer_ref_head = 0;
559 capsnap->context = old_snapc;
560 list_add_tail(&capsnap->ci_item, &ci->i_cap_snaps);
562 if (used & CEPH_CAP_FILE_WR) {
563 dout("queue_cap_snap %p cap_snap %p snapc %p"
564 " seq %llu used WR, now pending\n", inode,
565 capsnap, old_snapc, old_snapc->seq);
566 capsnap->writing = 1;
568 /* note mtime, size NOW. */
569 __ceph_finish_cap_snap(ci, capsnap);
575 if (ci->i_head_snapc) {
576 ci->i_head_snapc = ceph_get_snap_context(new_snapc);
577 dout(" new snapc is %p\n", new_snapc);
579 spin_unlock(&ci->i_ceph_lock);
582 ceph_put_snap_context(old_snapc);
586 * Finalize the size, mtime for a cap_snap.. that is, settle on final values
587 * to be used for the snapshot, to be flushed back to the mds.
589 * If capsnap can now be flushed, add to snap_flush list, and return 1.
591 * Caller must hold i_ceph_lock.
593 int __ceph_finish_cap_snap(struct ceph_inode_info *ci,
594 struct ceph_cap_snap *capsnap)
596 struct inode *inode = &ci->vfs_inode;
597 struct ceph_mds_client *mdsc = ceph_sb_to_client(inode->i_sb)->mdsc;
599 BUG_ON(capsnap->writing);
600 capsnap->size = inode->i_size;
601 capsnap->mtime = inode->i_mtime;
602 capsnap->atime = inode->i_atime;
603 capsnap->ctime = inode->i_ctime;
604 capsnap->time_warp_seq = ci->i_time_warp_seq;
605 capsnap->truncate_size = ci->i_truncate_size;
606 capsnap->truncate_seq = ci->i_truncate_seq;
607 if (capsnap->dirty_pages) {
608 dout("finish_cap_snap %p cap_snap %p snapc %p %llu %s s=%llu "
609 "still has %d dirty pages\n", inode, capsnap,
610 capsnap->context, capsnap->context->seq,
611 ceph_cap_string(capsnap->dirty), capsnap->size,
612 capsnap->dirty_pages);
616 ci->i_ceph_flags |= CEPH_I_FLUSH_SNAPS;
617 dout("finish_cap_snap %p cap_snap %p snapc %p %llu %s s=%llu\n",
618 inode, capsnap, capsnap->context,
619 capsnap->context->seq, ceph_cap_string(capsnap->dirty),
622 spin_lock(&mdsc->snap_flush_lock);
623 if (list_empty(&ci->i_snap_flush_item))
624 list_add_tail(&ci->i_snap_flush_item, &mdsc->snap_flush_list);
625 spin_unlock(&mdsc->snap_flush_lock);
626 return 1; /* caller may want to ceph_flush_snaps */
630 * Queue cap_snaps for snap writeback for this realm and its children.
631 * Called under snap_rwsem, so realm topology won't change.
633 static void queue_realm_cap_snaps(struct ceph_snap_realm *realm)
635 struct ceph_inode_info *ci;
636 struct inode *lastinode = NULL;
638 dout("queue_realm_cap_snaps %p %llx inodes\n", realm, realm->ino);
640 spin_lock(&realm->inodes_with_caps_lock);
641 list_for_each_entry(ci, &realm->inodes_with_caps, i_snap_realm_item) {
642 struct inode *inode = igrab(&ci->vfs_inode);
645 spin_unlock(&realm->inodes_with_caps_lock);
648 ceph_queue_cap_snap(ci);
649 spin_lock(&realm->inodes_with_caps_lock);
651 spin_unlock(&realm->inodes_with_caps_lock);
654 dout("queue_realm_cap_snaps %p %llx done\n", realm, realm->ino);
658 * Parse and apply a snapblob "snap trace" from the MDS. This specifies
659 * the snap realm parameters from a given realm and all of its ancestors,
662 * Caller must hold snap_rwsem for write.
664 int ceph_update_snap_trace(struct ceph_mds_client *mdsc,
665 void *p, void *e, bool deletion,
666 struct ceph_snap_realm **realm_ret)
668 struct ceph_mds_snap_realm *ri; /* encoded */
669 __le64 *snaps; /* encoded */
670 __le64 *prior_parent_snaps; /* encoded */
671 struct ceph_snap_realm *realm = NULL;
672 struct ceph_snap_realm *first_realm = NULL;
675 LIST_HEAD(dirty_realms);
677 dout("update_snap_trace deletion=%d\n", deletion);
679 ceph_decode_need(&p, e, sizeof(*ri), bad);
682 ceph_decode_need(&p, e, sizeof(u64)*(le32_to_cpu(ri->num_snaps) +
683 le32_to_cpu(ri->num_prior_parent_snaps)), bad);
685 p += sizeof(u64) * le32_to_cpu(ri->num_snaps);
686 prior_parent_snaps = p;
687 p += sizeof(u64) * le32_to_cpu(ri->num_prior_parent_snaps);
689 realm = ceph_lookup_snap_realm(mdsc, le64_to_cpu(ri->ino));
691 realm = ceph_create_snap_realm(mdsc, le64_to_cpu(ri->ino));
693 err = PTR_ERR(realm);
698 /* ensure the parent is correct */
699 err = adjust_snap_realm_parent(mdsc, realm, le64_to_cpu(ri->parent));
704 if (le64_to_cpu(ri->seq) > realm->seq) {
705 dout("update_snap_trace updating %llx %p %lld -> %lld\n",
706 realm->ino, realm, realm->seq, le64_to_cpu(ri->seq));
707 /* update realm parameters, snap lists */
708 realm->seq = le64_to_cpu(ri->seq);
709 realm->created = le64_to_cpu(ri->created);
710 realm->parent_since = le64_to_cpu(ri->parent_since);
712 realm->num_snaps = le32_to_cpu(ri->num_snaps);
713 err = dup_array(&realm->snaps, snaps, realm->num_snaps);
717 realm->num_prior_parent_snaps =
718 le32_to_cpu(ri->num_prior_parent_snaps);
719 err = dup_array(&realm->prior_parent_snaps, prior_parent_snaps,
720 realm->num_prior_parent_snaps);
724 if (realm->seq > mdsc->last_snap_seq)
725 mdsc->last_snap_seq = realm->seq;
728 } else if (!realm->cached_context) {
729 dout("update_snap_trace %llx %p seq %lld new\n",
730 realm->ino, realm, realm->seq);
733 dout("update_snap_trace %llx %p seq %lld unchanged\n",
734 realm->ino, realm, realm->seq);
737 dout("done with %llx %p, invalidated=%d, %p %p\n", realm->ino,
738 realm, invalidate, p, e);
740 /* invalidate when we reach the _end_ (root) of the trace */
741 if (invalidate && p >= e)
742 rebuild_snap_realms(realm, &dirty_realms);
747 ceph_put_snap_realm(mdsc, realm);
753 * queue cap snaps _after_ we've built the new snap contexts,
754 * so that i_head_snapc can be set appropriately.
756 while (!list_empty(&dirty_realms)) {
757 realm = list_first_entry(&dirty_realms, struct ceph_snap_realm,
759 list_del_init(&realm->dirty_item);
760 queue_realm_cap_snaps(realm);
764 *realm_ret = first_realm;
766 ceph_put_snap_realm(mdsc, first_realm);
768 __cleanup_empty_realms(mdsc);
774 if (realm && !IS_ERR(realm))
775 ceph_put_snap_realm(mdsc, realm);
777 ceph_put_snap_realm(mdsc, first_realm);
778 pr_err("update_snap_trace error %d\n", err);
784 * Send any cap_snaps that are queued for flush. Try to carry
785 * s_mutex across multiple snap flushes to avoid locking overhead.
787 * Caller holds no locks.
789 static void flush_snaps(struct ceph_mds_client *mdsc)
791 struct ceph_inode_info *ci;
793 struct ceph_mds_session *session = NULL;
795 dout("flush_snaps\n");
796 spin_lock(&mdsc->snap_flush_lock);
797 while (!list_empty(&mdsc->snap_flush_list)) {
798 ci = list_first_entry(&mdsc->snap_flush_list,
799 struct ceph_inode_info, i_snap_flush_item);
800 inode = &ci->vfs_inode;
802 spin_unlock(&mdsc->snap_flush_lock);
803 ceph_flush_snaps(ci, &session);
805 spin_lock(&mdsc->snap_flush_lock);
807 spin_unlock(&mdsc->snap_flush_lock);
810 mutex_unlock(&session->s_mutex);
811 ceph_put_mds_session(session);
813 dout("flush_snaps done\n");
818 * Handle a snap notification from the MDS.
820 * This can take two basic forms: the simplest is just a snap creation
821 * or deletion notification on an existing realm. This should update the
822 * realm and its children.
824 * The more difficult case is realm creation, due to snap creation at a
825 * new point in the file hierarchy, or due to a rename that moves a file or
826 * directory into another realm.
828 void ceph_handle_snap(struct ceph_mds_client *mdsc,
829 struct ceph_mds_session *session,
830 struct ceph_msg *msg)
832 struct super_block *sb = mdsc->fsc->sb;
833 int mds = session->s_mds;
837 struct ceph_snap_realm *realm = NULL;
838 void *p = msg->front.iov_base;
839 void *e = p + msg->front.iov_len;
840 struct ceph_mds_snap_head *h;
841 int num_split_inos, num_split_realms;
842 __le64 *split_inos = NULL, *split_realms = NULL;
844 int locked_rwsem = 0;
847 if (msg->front.iov_len < sizeof(*h))
850 op = le32_to_cpu(h->op);
851 split = le64_to_cpu(h->split); /* non-zero if we are splitting an
853 num_split_inos = le32_to_cpu(h->num_split_inos);
854 num_split_realms = le32_to_cpu(h->num_split_realms);
855 trace_len = le32_to_cpu(h->trace_len);
858 dout("handle_snap from mds%d op %s split %llx tracelen %d\n", mds,
859 ceph_snap_op_name(op), split, trace_len);
861 mutex_lock(&session->s_mutex);
863 mutex_unlock(&session->s_mutex);
865 down_write(&mdsc->snap_rwsem);
868 if (op == CEPH_SNAP_OP_SPLIT) {
869 struct ceph_mds_snap_realm *ri;
872 * A "split" breaks part of an existing realm off into
873 * a new realm. The MDS provides a list of inodes
874 * (with caps) and child realms that belong to the new
878 p += sizeof(u64) * num_split_inos;
880 p += sizeof(u64) * num_split_realms;
881 ceph_decode_need(&p, e, sizeof(*ri), bad);
882 /* we will peek at realm info here, but will _not_
883 * advance p, as the realm update will occur below in
884 * ceph_update_snap_trace. */
887 realm = ceph_lookup_snap_realm(mdsc, split);
889 realm = ceph_create_snap_realm(mdsc, split);
894 dout("splitting snap_realm %llx %p\n", realm->ino, realm);
895 for (i = 0; i < num_split_inos; i++) {
896 struct ceph_vino vino = {
897 .ino = le64_to_cpu(split_inos[i]),
900 struct inode *inode = ceph_find_inode(sb, vino);
901 struct ceph_inode_info *ci;
902 struct ceph_snap_realm *oldrealm;
906 ci = ceph_inode(inode);
908 spin_lock(&ci->i_ceph_lock);
909 if (!ci->i_snap_realm)
912 * If this inode belongs to a realm that was
913 * created after our new realm, we experienced
914 * a race (due to another split notifications
915 * arriving from a different MDS). So skip
918 if (ci->i_snap_realm->created >
919 le64_to_cpu(ri->created)) {
920 dout(" leaving %p in newer realm %llx %p\n",
921 inode, ci->i_snap_realm->ino,
925 dout(" will move %p to split realm %llx %p\n",
926 inode, realm->ino, realm);
928 * Move the inode to the new realm
930 oldrealm = ci->i_snap_realm;
931 spin_lock(&oldrealm->inodes_with_caps_lock);
932 list_del_init(&ci->i_snap_realm_item);
933 spin_unlock(&oldrealm->inodes_with_caps_lock);
935 spin_lock(&realm->inodes_with_caps_lock);
936 list_add(&ci->i_snap_realm_item,
937 &realm->inodes_with_caps);
938 ci->i_snap_realm = realm;
939 if (realm->ino == ci->i_vino.ino)
940 realm->inode = inode;
941 spin_unlock(&realm->inodes_with_caps_lock);
943 spin_unlock(&ci->i_ceph_lock);
945 ceph_get_snap_realm(mdsc, realm);
946 ceph_put_snap_realm(mdsc, oldrealm);
952 spin_unlock(&ci->i_ceph_lock);
956 /* we may have taken some of the old realm's children. */
957 for (i = 0; i < num_split_realms; i++) {
958 struct ceph_snap_realm *child =
959 __lookup_snap_realm(mdsc,
960 le64_to_cpu(split_realms[i]));
963 adjust_snap_realm_parent(mdsc, child, realm->ino);
968 * update using the provided snap trace. if we are deleting a
969 * snap, we can avoid queueing cap_snaps.
971 ceph_update_snap_trace(mdsc, p, e,
972 op == CEPH_SNAP_OP_DESTROY, NULL);
974 if (op == CEPH_SNAP_OP_SPLIT)
975 /* we took a reference when we created the realm, above */
976 ceph_put_snap_realm(mdsc, realm);
978 __cleanup_empty_realms(mdsc);
980 up_write(&mdsc->snap_rwsem);
986 pr_err("corrupt snap message from mds%d\n", mds);
990 up_write(&mdsc->snap_rwsem);
994 struct ceph_snapid_map* ceph_get_snapid_map(struct ceph_mds_client *mdsc,
997 struct ceph_snapid_map *sm, *exist;
998 struct rb_node **p, *parent;
1002 spin_lock(&mdsc->snapid_map_lock);
1003 p = &mdsc->snapid_map_tree.rb_node;
1005 exist = rb_entry(*p, struct ceph_snapid_map, node);
1006 if (snap > exist->snap) {
1008 } else if (snap < exist->snap) {
1009 p = &(*p)->rb_right;
1011 if (atomic_inc_return(&exist->ref) == 1)
1012 list_del_init(&exist->lru);
1017 spin_unlock(&mdsc->snapid_map_lock);
1019 dout("found snapid map %llx -> %x\n", exist->snap, exist->dev);
1023 sm = kmalloc(sizeof(*sm), GFP_NOFS);
1027 ret = get_anon_bdev(&sm->dev);
1033 INIT_LIST_HEAD(&sm->lru);
1034 atomic_set(&sm->ref, 1);
1039 p = &mdsc->snapid_map_tree.rb_node;
1040 spin_lock(&mdsc->snapid_map_lock);
1043 exist = rb_entry(*p, struct ceph_snapid_map, node);
1044 if (snap > exist->snap)
1046 else if (snap < exist->snap)
1047 p = &(*p)->rb_right;
1053 if (atomic_inc_return(&exist->ref) == 1)
1054 list_del_init(&exist->lru);
1056 rb_link_node(&sm->node, parent, p);
1057 rb_insert_color(&sm->node, &mdsc->snapid_map_tree);
1059 spin_unlock(&mdsc->snapid_map_lock);
1061 free_anon_bdev(sm->dev);
1063 dout("found snapid map %llx -> %x\n", exist->snap, exist->dev);
1067 dout("create snapid map %llx -> %x\n", sm->snap, sm->dev);
1071 void ceph_put_snapid_map(struct ceph_mds_client* mdsc,
1072 struct ceph_snapid_map *sm)
1076 if (atomic_dec_and_lock(&sm->ref, &mdsc->snapid_map_lock)) {
1077 if (!RB_EMPTY_NODE(&sm->node)) {
1078 sm->last_used = jiffies;
1079 list_add_tail(&sm->lru, &mdsc->snapid_map_lru);
1080 spin_unlock(&mdsc->snapid_map_lock);
1082 /* already cleaned up by
1083 * ceph_cleanup_snapid_map() */
1084 spin_unlock(&mdsc->snapid_map_lock);
1090 void ceph_trim_snapid_map(struct ceph_mds_client *mdsc)
1092 struct ceph_snapid_map *sm;
1096 spin_lock(&mdsc->snapid_map_lock);
1099 while (!list_empty(&mdsc->snapid_map_lru)) {
1100 sm = list_first_entry(&mdsc->snapid_map_lru,
1101 struct ceph_snapid_map, lru);
1102 if (time_after(sm->last_used + CEPH_SNAPID_MAP_TIMEOUT, now))
1105 rb_erase(&sm->node, &mdsc->snapid_map_tree);
1106 list_move(&sm->lru, &to_free);
1108 spin_unlock(&mdsc->snapid_map_lock);
1110 while (!list_empty(&to_free)) {
1111 sm = list_first_entry(&to_free, struct ceph_snapid_map, lru);
1113 dout("trim snapid map %llx -> %x\n", sm->snap, sm->dev);
1114 free_anon_bdev(sm->dev);
1119 void ceph_cleanup_snapid_map(struct ceph_mds_client *mdsc)
1121 struct ceph_snapid_map *sm;
1125 spin_lock(&mdsc->snapid_map_lock);
1126 while ((p = rb_first(&mdsc->snapid_map_tree))) {
1127 sm = rb_entry(p, struct ceph_snapid_map, node);
1128 rb_erase(p, &mdsc->snapid_map_tree);
1130 list_move(&sm->lru, &to_free);
1132 spin_unlock(&mdsc->snapid_map_lock);
1134 while (!list_empty(&to_free)) {
1135 sm = list_first_entry(&to_free, struct ceph_snapid_map, lru);
1137 free_anon_bdev(sm->dev);
1138 if (WARN_ON_ONCE(atomic_read(&sm->ref))) {
1139 pr_err("snapid map %llx -> %x still in use\n",