18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
28c2ecf20Sopenharmony_ci#include <linux/ceph/ceph_debug.h>
38c2ecf20Sopenharmony_ci
48c2ecf20Sopenharmony_ci#include <linux/sort.h>
58c2ecf20Sopenharmony_ci#include <linux/slab.h>
68c2ecf20Sopenharmony_ci#include <linux/iversion.h>
78c2ecf20Sopenharmony_ci#include "super.h"
88c2ecf20Sopenharmony_ci#include "mds_client.h"
98c2ecf20Sopenharmony_ci#include <linux/ceph/decode.h>
108c2ecf20Sopenharmony_ci
118c2ecf20Sopenharmony_ci/* unused map expires after 5 minutes */
128c2ecf20Sopenharmony_ci#define CEPH_SNAPID_MAP_TIMEOUT	(5 * 60 * HZ)
138c2ecf20Sopenharmony_ci
148c2ecf20Sopenharmony_ci/*
158c2ecf20Sopenharmony_ci * Snapshots in ceph are driven in large part by cooperation from the
168c2ecf20Sopenharmony_ci * client.  In contrast to local file systems or file servers that
178c2ecf20Sopenharmony_ci * implement snapshots at a single point in the system, ceph's
188c2ecf20Sopenharmony_ci * distributed access to storage requires clients to help decide
198c2ecf20Sopenharmony_ci * whether a write logically occurs before or after a recently created
208c2ecf20Sopenharmony_ci * snapshot.
218c2ecf20Sopenharmony_ci *
228c2ecf20Sopenharmony_ci * This provides a perfect instantanous client-wide snapshot.  Between
238c2ecf20Sopenharmony_ci * clients, however, snapshots may appear to be applied at slightly
248c2ecf20Sopenharmony_ci * different points in time, depending on delays in delivering the
258c2ecf20Sopenharmony_ci * snapshot notification.
268c2ecf20Sopenharmony_ci *
278c2ecf20Sopenharmony_ci * Snapshots are _not_ file system-wide.  Instead, each snapshot
288c2ecf20Sopenharmony_ci * applies to the subdirectory nested beneath some directory.  This
298c2ecf20Sopenharmony_ci * effectively divides the hierarchy into multiple "realms," where all
308c2ecf20Sopenharmony_ci * of the files contained by each realm share the same set of
318c2ecf20Sopenharmony_ci * snapshots.  An individual realm's snap set contains snapshots
328c2ecf20Sopenharmony_ci * explicitly created on that realm, as well as any snaps in its
338c2ecf20Sopenharmony_ci * parent's snap set _after_ the point at which the parent became it's
348c2ecf20Sopenharmony_ci * parent (due to, say, a rename).  Similarly, snaps from prior parents
358c2ecf20Sopenharmony_ci * during the time intervals during which they were the parent are included.
368c2ecf20Sopenharmony_ci *
378c2ecf20Sopenharmony_ci * The client is spared most of this detail, fortunately... it must only
388c2ecf20Sopenharmony_ci * maintains a hierarchy of realms reflecting the current parent/child
398c2ecf20Sopenharmony_ci * realm relationship, and for each realm has an explicit list of snaps
408c2ecf20Sopenharmony_ci * inherited from prior parents.
418c2ecf20Sopenharmony_ci *
428c2ecf20Sopenharmony_ci * A snap_realm struct is maintained for realms containing every inode
438c2ecf20Sopenharmony_ci * with an open cap in the system.  (The needed snap realm information is
448c2ecf20Sopenharmony_ci * provided by the MDS whenever a cap is issued, i.e., on open.)  A 'seq'
458c2ecf20Sopenharmony_ci * version number is used to ensure that as realm parameters change (new
468c2ecf20Sopenharmony_ci * snapshot, new parent, etc.) the client's realm hierarchy is updated.
478c2ecf20Sopenharmony_ci *
488c2ecf20Sopenharmony_ci * The realm hierarchy drives the generation of a 'snap context' for each
498c2ecf20Sopenharmony_ci * realm, which simply lists the resulting set of snaps for the realm.  This
508c2ecf20Sopenharmony_ci * is attached to any writes sent to OSDs.
518c2ecf20Sopenharmony_ci */
528c2ecf20Sopenharmony_ci/*
538c2ecf20Sopenharmony_ci * Unfortunately error handling is a bit mixed here.  If we get a snap
548c2ecf20Sopenharmony_ci * update, but don't have enough memory to update our realm hierarchy,
558c2ecf20Sopenharmony_ci * it's not clear what we can do about it (besides complaining to the
568c2ecf20Sopenharmony_ci * console).
578c2ecf20Sopenharmony_ci */
588c2ecf20Sopenharmony_ci
598c2ecf20Sopenharmony_ci
608c2ecf20Sopenharmony_ci/*
618c2ecf20Sopenharmony_ci * increase ref count for the realm
628c2ecf20Sopenharmony_ci *
638c2ecf20Sopenharmony_ci * caller must hold snap_rwsem.
648c2ecf20Sopenharmony_ci */
658c2ecf20Sopenharmony_civoid ceph_get_snap_realm(struct ceph_mds_client *mdsc,
668c2ecf20Sopenharmony_ci			 struct ceph_snap_realm *realm)
678c2ecf20Sopenharmony_ci{
688c2ecf20Sopenharmony_ci	lockdep_assert_held(&mdsc->snap_rwsem);
698c2ecf20Sopenharmony_ci
708c2ecf20Sopenharmony_ci	/*
718c2ecf20Sopenharmony_ci	 * The 0->1 and 1->0 transitions must take the snap_empty_lock
728c2ecf20Sopenharmony_ci	 * atomically with the refcount change. Go ahead and bump the
738c2ecf20Sopenharmony_ci	 * nref here, unless it's 0, in which case we take the spinlock
748c2ecf20Sopenharmony_ci	 * and then do the increment and remove it from the list.
758c2ecf20Sopenharmony_ci	 */
768c2ecf20Sopenharmony_ci	if (atomic_inc_not_zero(&realm->nref))
778c2ecf20Sopenharmony_ci		return;
788c2ecf20Sopenharmony_ci
798c2ecf20Sopenharmony_ci	spin_lock(&mdsc->snap_empty_lock);
808c2ecf20Sopenharmony_ci	if (atomic_inc_return(&realm->nref) == 1)
818c2ecf20Sopenharmony_ci		list_del_init(&realm->empty_item);
828c2ecf20Sopenharmony_ci	spin_unlock(&mdsc->snap_empty_lock);
838c2ecf20Sopenharmony_ci}
848c2ecf20Sopenharmony_ci
858c2ecf20Sopenharmony_cistatic void __insert_snap_realm(struct rb_root *root,
868c2ecf20Sopenharmony_ci				struct ceph_snap_realm *new)
878c2ecf20Sopenharmony_ci{
888c2ecf20Sopenharmony_ci	struct rb_node **p = &root->rb_node;
898c2ecf20Sopenharmony_ci	struct rb_node *parent = NULL;
908c2ecf20Sopenharmony_ci	struct ceph_snap_realm *r = NULL;
918c2ecf20Sopenharmony_ci
928c2ecf20Sopenharmony_ci	while (*p) {
938c2ecf20Sopenharmony_ci		parent = *p;
948c2ecf20Sopenharmony_ci		r = rb_entry(parent, struct ceph_snap_realm, node);
958c2ecf20Sopenharmony_ci		if (new->ino < r->ino)
968c2ecf20Sopenharmony_ci			p = &(*p)->rb_left;
978c2ecf20Sopenharmony_ci		else if (new->ino > r->ino)
988c2ecf20Sopenharmony_ci			p = &(*p)->rb_right;
998c2ecf20Sopenharmony_ci		else
1008c2ecf20Sopenharmony_ci			BUG();
1018c2ecf20Sopenharmony_ci	}
1028c2ecf20Sopenharmony_ci
1038c2ecf20Sopenharmony_ci	rb_link_node(&new->node, parent, p);
1048c2ecf20Sopenharmony_ci	rb_insert_color(&new->node, root);
1058c2ecf20Sopenharmony_ci}
1068c2ecf20Sopenharmony_ci
1078c2ecf20Sopenharmony_ci/*
1088c2ecf20Sopenharmony_ci * create and get the realm rooted at @ino and bump its ref count.
1098c2ecf20Sopenharmony_ci *
1108c2ecf20Sopenharmony_ci * caller must hold snap_rwsem for write.
1118c2ecf20Sopenharmony_ci */
1128c2ecf20Sopenharmony_cistatic struct ceph_snap_realm *ceph_create_snap_realm(
1138c2ecf20Sopenharmony_ci	struct ceph_mds_client *mdsc,
1148c2ecf20Sopenharmony_ci	u64 ino)
1158c2ecf20Sopenharmony_ci{
1168c2ecf20Sopenharmony_ci	struct ceph_snap_realm *realm;
1178c2ecf20Sopenharmony_ci
1188c2ecf20Sopenharmony_ci	lockdep_assert_held_write(&mdsc->snap_rwsem);
1198c2ecf20Sopenharmony_ci
1208c2ecf20Sopenharmony_ci	realm = kzalloc(sizeof(*realm), GFP_NOFS);
1218c2ecf20Sopenharmony_ci	if (!realm)
1228c2ecf20Sopenharmony_ci		return ERR_PTR(-ENOMEM);
1238c2ecf20Sopenharmony_ci
1248c2ecf20Sopenharmony_ci	atomic_set(&realm->nref, 1);    /* for caller */
1258c2ecf20Sopenharmony_ci	realm->ino = ino;
1268c2ecf20Sopenharmony_ci	INIT_LIST_HEAD(&realm->children);
1278c2ecf20Sopenharmony_ci	INIT_LIST_HEAD(&realm->child_item);
1288c2ecf20Sopenharmony_ci	INIT_LIST_HEAD(&realm->empty_item);
1298c2ecf20Sopenharmony_ci	INIT_LIST_HEAD(&realm->dirty_item);
1308c2ecf20Sopenharmony_ci	INIT_LIST_HEAD(&realm->inodes_with_caps);
1318c2ecf20Sopenharmony_ci	spin_lock_init(&realm->inodes_with_caps_lock);
1328c2ecf20Sopenharmony_ci	__insert_snap_realm(&mdsc->snap_realms, realm);
1338c2ecf20Sopenharmony_ci	mdsc->num_snap_realms++;
1348c2ecf20Sopenharmony_ci
1358c2ecf20Sopenharmony_ci	dout("create_snap_realm %llx %p\n", realm->ino, realm);
1368c2ecf20Sopenharmony_ci	return realm;
1378c2ecf20Sopenharmony_ci}
1388c2ecf20Sopenharmony_ci
1398c2ecf20Sopenharmony_ci/*
1408c2ecf20Sopenharmony_ci * lookup the realm rooted at @ino.
1418c2ecf20Sopenharmony_ci *
1428c2ecf20Sopenharmony_ci * caller must hold snap_rwsem.
1438c2ecf20Sopenharmony_ci */
1448c2ecf20Sopenharmony_cistatic struct ceph_snap_realm *__lookup_snap_realm(struct ceph_mds_client *mdsc,
1458c2ecf20Sopenharmony_ci						   u64 ino)
1468c2ecf20Sopenharmony_ci{
1478c2ecf20Sopenharmony_ci	struct rb_node *n = mdsc->snap_realms.rb_node;
1488c2ecf20Sopenharmony_ci	struct ceph_snap_realm *r;
1498c2ecf20Sopenharmony_ci
1508c2ecf20Sopenharmony_ci	lockdep_assert_held(&mdsc->snap_rwsem);
1518c2ecf20Sopenharmony_ci
1528c2ecf20Sopenharmony_ci	while (n) {
1538c2ecf20Sopenharmony_ci		r = rb_entry(n, struct ceph_snap_realm, node);
1548c2ecf20Sopenharmony_ci		if (ino < r->ino)
1558c2ecf20Sopenharmony_ci			n = n->rb_left;
1568c2ecf20Sopenharmony_ci		else if (ino > r->ino)
1578c2ecf20Sopenharmony_ci			n = n->rb_right;
1588c2ecf20Sopenharmony_ci		else {
1598c2ecf20Sopenharmony_ci			dout("lookup_snap_realm %llx %p\n", r->ino, r);
1608c2ecf20Sopenharmony_ci			return r;
1618c2ecf20Sopenharmony_ci		}
1628c2ecf20Sopenharmony_ci	}
1638c2ecf20Sopenharmony_ci	return NULL;
1648c2ecf20Sopenharmony_ci}
1658c2ecf20Sopenharmony_ci
1668c2ecf20Sopenharmony_cistruct ceph_snap_realm *ceph_lookup_snap_realm(struct ceph_mds_client *mdsc,
1678c2ecf20Sopenharmony_ci					       u64 ino)
1688c2ecf20Sopenharmony_ci{
1698c2ecf20Sopenharmony_ci	struct ceph_snap_realm *r;
1708c2ecf20Sopenharmony_ci	r = __lookup_snap_realm(mdsc, ino);
1718c2ecf20Sopenharmony_ci	if (r)
1728c2ecf20Sopenharmony_ci		ceph_get_snap_realm(mdsc, r);
1738c2ecf20Sopenharmony_ci	return r;
1748c2ecf20Sopenharmony_ci}
1758c2ecf20Sopenharmony_ci
1768c2ecf20Sopenharmony_cistatic void __put_snap_realm(struct ceph_mds_client *mdsc,
1778c2ecf20Sopenharmony_ci			     struct ceph_snap_realm *realm);
1788c2ecf20Sopenharmony_ci
1798c2ecf20Sopenharmony_ci/*
1808c2ecf20Sopenharmony_ci * called with snap_rwsem (write)
1818c2ecf20Sopenharmony_ci */
1828c2ecf20Sopenharmony_cistatic void __destroy_snap_realm(struct ceph_mds_client *mdsc,
1838c2ecf20Sopenharmony_ci				 struct ceph_snap_realm *realm)
1848c2ecf20Sopenharmony_ci{
1858c2ecf20Sopenharmony_ci	lockdep_assert_held_write(&mdsc->snap_rwsem);
1868c2ecf20Sopenharmony_ci
1878c2ecf20Sopenharmony_ci	dout("__destroy_snap_realm %p %llx\n", realm, realm->ino);
1888c2ecf20Sopenharmony_ci
1898c2ecf20Sopenharmony_ci	rb_erase(&realm->node, &mdsc->snap_realms);
1908c2ecf20Sopenharmony_ci	mdsc->num_snap_realms--;
1918c2ecf20Sopenharmony_ci
1928c2ecf20Sopenharmony_ci	if (realm->parent) {
1938c2ecf20Sopenharmony_ci		list_del_init(&realm->child_item);
1948c2ecf20Sopenharmony_ci		__put_snap_realm(mdsc, realm->parent);
1958c2ecf20Sopenharmony_ci	}
1968c2ecf20Sopenharmony_ci
1978c2ecf20Sopenharmony_ci	kfree(realm->prior_parent_snaps);
1988c2ecf20Sopenharmony_ci	kfree(realm->snaps);
1998c2ecf20Sopenharmony_ci	ceph_put_snap_context(realm->cached_context);
2008c2ecf20Sopenharmony_ci	kfree(realm);
2018c2ecf20Sopenharmony_ci}
2028c2ecf20Sopenharmony_ci
2038c2ecf20Sopenharmony_ci/*
2048c2ecf20Sopenharmony_ci * caller holds snap_rwsem (write)
2058c2ecf20Sopenharmony_ci */
2068c2ecf20Sopenharmony_cistatic void __put_snap_realm(struct ceph_mds_client *mdsc,
2078c2ecf20Sopenharmony_ci			     struct ceph_snap_realm *realm)
2088c2ecf20Sopenharmony_ci{
2098c2ecf20Sopenharmony_ci	lockdep_assert_held_write(&mdsc->snap_rwsem);
2108c2ecf20Sopenharmony_ci
2118c2ecf20Sopenharmony_ci	/*
2128c2ecf20Sopenharmony_ci	 * We do not require the snap_empty_lock here, as any caller that
2138c2ecf20Sopenharmony_ci	 * increments the value must hold the snap_rwsem.
2148c2ecf20Sopenharmony_ci	 */
2158c2ecf20Sopenharmony_ci	if (atomic_dec_and_test(&realm->nref))
2168c2ecf20Sopenharmony_ci		__destroy_snap_realm(mdsc, realm);
2178c2ecf20Sopenharmony_ci}
2188c2ecf20Sopenharmony_ci
2198c2ecf20Sopenharmony_ci/*
2208c2ecf20Sopenharmony_ci * See comments in ceph_get_snap_realm. Caller needn't hold any locks.
2218c2ecf20Sopenharmony_ci */
2228c2ecf20Sopenharmony_civoid ceph_put_snap_realm(struct ceph_mds_client *mdsc,
2238c2ecf20Sopenharmony_ci			 struct ceph_snap_realm *realm)
2248c2ecf20Sopenharmony_ci{
2258c2ecf20Sopenharmony_ci	if (!atomic_dec_and_lock(&realm->nref, &mdsc->snap_empty_lock))
2268c2ecf20Sopenharmony_ci		return;
2278c2ecf20Sopenharmony_ci
2288c2ecf20Sopenharmony_ci	if (down_write_trylock(&mdsc->snap_rwsem)) {
2298c2ecf20Sopenharmony_ci		spin_unlock(&mdsc->snap_empty_lock);
2308c2ecf20Sopenharmony_ci		__destroy_snap_realm(mdsc, realm);
2318c2ecf20Sopenharmony_ci		up_write(&mdsc->snap_rwsem);
2328c2ecf20Sopenharmony_ci	} else {
2338c2ecf20Sopenharmony_ci		list_add(&realm->empty_item, &mdsc->snap_empty);
2348c2ecf20Sopenharmony_ci		spin_unlock(&mdsc->snap_empty_lock);
2358c2ecf20Sopenharmony_ci	}
2368c2ecf20Sopenharmony_ci}
2378c2ecf20Sopenharmony_ci
2388c2ecf20Sopenharmony_ci/*
2398c2ecf20Sopenharmony_ci * Clean up any realms whose ref counts have dropped to zero.  Note
2408c2ecf20Sopenharmony_ci * that this does not include realms who were created but not yet
2418c2ecf20Sopenharmony_ci * used.
2428c2ecf20Sopenharmony_ci *
2438c2ecf20Sopenharmony_ci * Called under snap_rwsem (write)
2448c2ecf20Sopenharmony_ci */
2458c2ecf20Sopenharmony_cistatic void __cleanup_empty_realms(struct ceph_mds_client *mdsc)
2468c2ecf20Sopenharmony_ci{
2478c2ecf20Sopenharmony_ci	struct ceph_snap_realm *realm;
2488c2ecf20Sopenharmony_ci
2498c2ecf20Sopenharmony_ci	lockdep_assert_held_write(&mdsc->snap_rwsem);
2508c2ecf20Sopenharmony_ci
2518c2ecf20Sopenharmony_ci	spin_lock(&mdsc->snap_empty_lock);
2528c2ecf20Sopenharmony_ci	while (!list_empty(&mdsc->snap_empty)) {
2538c2ecf20Sopenharmony_ci		realm = list_first_entry(&mdsc->snap_empty,
2548c2ecf20Sopenharmony_ci				   struct ceph_snap_realm, empty_item);
2558c2ecf20Sopenharmony_ci		list_del(&realm->empty_item);
2568c2ecf20Sopenharmony_ci		spin_unlock(&mdsc->snap_empty_lock);
2578c2ecf20Sopenharmony_ci		__destroy_snap_realm(mdsc, realm);
2588c2ecf20Sopenharmony_ci		spin_lock(&mdsc->snap_empty_lock);
2598c2ecf20Sopenharmony_ci	}
2608c2ecf20Sopenharmony_ci	spin_unlock(&mdsc->snap_empty_lock);
2618c2ecf20Sopenharmony_ci}
2628c2ecf20Sopenharmony_ci
2638c2ecf20Sopenharmony_civoid ceph_cleanup_empty_realms(struct ceph_mds_client *mdsc)
2648c2ecf20Sopenharmony_ci{
2658c2ecf20Sopenharmony_ci	down_write(&mdsc->snap_rwsem);
2668c2ecf20Sopenharmony_ci	__cleanup_empty_realms(mdsc);
2678c2ecf20Sopenharmony_ci	up_write(&mdsc->snap_rwsem);
2688c2ecf20Sopenharmony_ci}
2698c2ecf20Sopenharmony_ci
2708c2ecf20Sopenharmony_ci/*
2718c2ecf20Sopenharmony_ci * adjust the parent realm of a given @realm.  adjust child list, and parent
2728c2ecf20Sopenharmony_ci * pointers, and ref counts appropriately.
2738c2ecf20Sopenharmony_ci *
2748c2ecf20Sopenharmony_ci * return true if parent was changed, 0 if unchanged, <0 on error.
2758c2ecf20Sopenharmony_ci *
2768c2ecf20Sopenharmony_ci * caller must hold snap_rwsem for write.
2778c2ecf20Sopenharmony_ci */
2788c2ecf20Sopenharmony_cistatic int adjust_snap_realm_parent(struct ceph_mds_client *mdsc,
2798c2ecf20Sopenharmony_ci				    struct ceph_snap_realm *realm,
2808c2ecf20Sopenharmony_ci				    u64 parentino)
2818c2ecf20Sopenharmony_ci{
2828c2ecf20Sopenharmony_ci	struct ceph_snap_realm *parent;
2838c2ecf20Sopenharmony_ci
2848c2ecf20Sopenharmony_ci	lockdep_assert_held_write(&mdsc->snap_rwsem);
2858c2ecf20Sopenharmony_ci
2868c2ecf20Sopenharmony_ci	if (realm->parent_ino == parentino)
2878c2ecf20Sopenharmony_ci		return 0;
2888c2ecf20Sopenharmony_ci
2898c2ecf20Sopenharmony_ci	parent = ceph_lookup_snap_realm(mdsc, parentino);
2908c2ecf20Sopenharmony_ci	if (!parent) {
2918c2ecf20Sopenharmony_ci		parent = ceph_create_snap_realm(mdsc, parentino);
2928c2ecf20Sopenharmony_ci		if (IS_ERR(parent))
2938c2ecf20Sopenharmony_ci			return PTR_ERR(parent);
2948c2ecf20Sopenharmony_ci	}
2958c2ecf20Sopenharmony_ci	dout("adjust_snap_realm_parent %llx %p: %llx %p -> %llx %p\n",
2968c2ecf20Sopenharmony_ci	     realm->ino, realm, realm->parent_ino, realm->parent,
2978c2ecf20Sopenharmony_ci	     parentino, parent);
2988c2ecf20Sopenharmony_ci	if (realm->parent) {
2998c2ecf20Sopenharmony_ci		list_del_init(&realm->child_item);
3008c2ecf20Sopenharmony_ci		ceph_put_snap_realm(mdsc, realm->parent);
3018c2ecf20Sopenharmony_ci	}
3028c2ecf20Sopenharmony_ci	realm->parent_ino = parentino;
3038c2ecf20Sopenharmony_ci	realm->parent = parent;
3048c2ecf20Sopenharmony_ci	list_add(&realm->child_item, &parent->children);
3058c2ecf20Sopenharmony_ci	return 1;
3068c2ecf20Sopenharmony_ci}
3078c2ecf20Sopenharmony_ci
3088c2ecf20Sopenharmony_ci
3098c2ecf20Sopenharmony_cistatic int cmpu64_rev(const void *a, const void *b)
3108c2ecf20Sopenharmony_ci{
3118c2ecf20Sopenharmony_ci	if (*(u64 *)a < *(u64 *)b)
3128c2ecf20Sopenharmony_ci		return 1;
3138c2ecf20Sopenharmony_ci	if (*(u64 *)a > *(u64 *)b)
3148c2ecf20Sopenharmony_ci		return -1;
3158c2ecf20Sopenharmony_ci	return 0;
3168c2ecf20Sopenharmony_ci}
3178c2ecf20Sopenharmony_ci
3188c2ecf20Sopenharmony_ci
3198c2ecf20Sopenharmony_ci/*
3208c2ecf20Sopenharmony_ci * build the snap context for a given realm.
3218c2ecf20Sopenharmony_ci */
3228c2ecf20Sopenharmony_cistatic int build_snap_context(struct ceph_snap_realm *realm,
3238c2ecf20Sopenharmony_ci			      struct list_head* dirty_realms)
3248c2ecf20Sopenharmony_ci{
3258c2ecf20Sopenharmony_ci	struct ceph_snap_realm *parent = realm->parent;
3268c2ecf20Sopenharmony_ci	struct ceph_snap_context *snapc;
3278c2ecf20Sopenharmony_ci	int err = 0;
3288c2ecf20Sopenharmony_ci	u32 num = realm->num_prior_parent_snaps + realm->num_snaps;
3298c2ecf20Sopenharmony_ci
3308c2ecf20Sopenharmony_ci	/*
3318c2ecf20Sopenharmony_ci	 * build parent context, if it hasn't been built.
3328c2ecf20Sopenharmony_ci	 * conservatively estimate that all parent snaps might be
3338c2ecf20Sopenharmony_ci	 * included by us.
3348c2ecf20Sopenharmony_ci	 */
3358c2ecf20Sopenharmony_ci	if (parent) {
3368c2ecf20Sopenharmony_ci		if (!parent->cached_context) {
3378c2ecf20Sopenharmony_ci			err = build_snap_context(parent, dirty_realms);
3388c2ecf20Sopenharmony_ci			if (err)
3398c2ecf20Sopenharmony_ci				goto fail;
3408c2ecf20Sopenharmony_ci		}
3418c2ecf20Sopenharmony_ci		num += parent->cached_context->num_snaps;
3428c2ecf20Sopenharmony_ci	}
3438c2ecf20Sopenharmony_ci
3448c2ecf20Sopenharmony_ci	/* do i actually need to update?  not if my context seq
3458c2ecf20Sopenharmony_ci	   matches realm seq, and my parents' does to.  (this works
3468c2ecf20Sopenharmony_ci	   because we rebuild_snap_realms() works _downward_ in
3478c2ecf20Sopenharmony_ci	   hierarchy after each update.) */
3488c2ecf20Sopenharmony_ci	if (realm->cached_context &&
3498c2ecf20Sopenharmony_ci	    realm->cached_context->seq == realm->seq &&
3508c2ecf20Sopenharmony_ci	    (!parent ||
3518c2ecf20Sopenharmony_ci	     realm->cached_context->seq >= parent->cached_context->seq)) {
3528c2ecf20Sopenharmony_ci		dout("build_snap_context %llx %p: %p seq %lld (%u snaps)"
3538c2ecf20Sopenharmony_ci		     " (unchanged)\n",
3548c2ecf20Sopenharmony_ci		     realm->ino, realm, realm->cached_context,
3558c2ecf20Sopenharmony_ci		     realm->cached_context->seq,
3568c2ecf20Sopenharmony_ci		     (unsigned int)realm->cached_context->num_snaps);
3578c2ecf20Sopenharmony_ci		return 0;
3588c2ecf20Sopenharmony_ci	}
3598c2ecf20Sopenharmony_ci
3608c2ecf20Sopenharmony_ci	/* alloc new snap context */
3618c2ecf20Sopenharmony_ci	err = -ENOMEM;
3628c2ecf20Sopenharmony_ci	if (num > (SIZE_MAX - sizeof(*snapc)) / sizeof(u64))
3638c2ecf20Sopenharmony_ci		goto fail;
3648c2ecf20Sopenharmony_ci	snapc = ceph_create_snap_context(num, GFP_NOFS);
3658c2ecf20Sopenharmony_ci	if (!snapc)
3668c2ecf20Sopenharmony_ci		goto fail;
3678c2ecf20Sopenharmony_ci
3688c2ecf20Sopenharmony_ci	/* build (reverse sorted) snap vector */
3698c2ecf20Sopenharmony_ci	num = 0;
3708c2ecf20Sopenharmony_ci	snapc->seq = realm->seq;
3718c2ecf20Sopenharmony_ci	if (parent) {
3728c2ecf20Sopenharmony_ci		u32 i;
3738c2ecf20Sopenharmony_ci
3748c2ecf20Sopenharmony_ci		/* include any of parent's snaps occurring _after_ my
3758c2ecf20Sopenharmony_ci		   parent became my parent */
3768c2ecf20Sopenharmony_ci		for (i = 0; i < parent->cached_context->num_snaps; i++)
3778c2ecf20Sopenharmony_ci			if (parent->cached_context->snaps[i] >=
3788c2ecf20Sopenharmony_ci			    realm->parent_since)
3798c2ecf20Sopenharmony_ci				snapc->snaps[num++] =
3808c2ecf20Sopenharmony_ci					parent->cached_context->snaps[i];
3818c2ecf20Sopenharmony_ci		if (parent->cached_context->seq > snapc->seq)
3828c2ecf20Sopenharmony_ci			snapc->seq = parent->cached_context->seq;
3838c2ecf20Sopenharmony_ci	}
3848c2ecf20Sopenharmony_ci	memcpy(snapc->snaps + num, realm->snaps,
3858c2ecf20Sopenharmony_ci	       sizeof(u64)*realm->num_snaps);
3868c2ecf20Sopenharmony_ci	num += realm->num_snaps;
3878c2ecf20Sopenharmony_ci	memcpy(snapc->snaps + num, realm->prior_parent_snaps,
3888c2ecf20Sopenharmony_ci	       sizeof(u64)*realm->num_prior_parent_snaps);
3898c2ecf20Sopenharmony_ci	num += realm->num_prior_parent_snaps;
3908c2ecf20Sopenharmony_ci
3918c2ecf20Sopenharmony_ci	sort(snapc->snaps, num, sizeof(u64), cmpu64_rev, NULL);
3928c2ecf20Sopenharmony_ci	snapc->num_snaps = num;
3938c2ecf20Sopenharmony_ci	dout("build_snap_context %llx %p: %p seq %lld (%u snaps)\n",
3948c2ecf20Sopenharmony_ci	     realm->ino, realm, snapc, snapc->seq,
3958c2ecf20Sopenharmony_ci	     (unsigned int) snapc->num_snaps);
3968c2ecf20Sopenharmony_ci
3978c2ecf20Sopenharmony_ci	ceph_put_snap_context(realm->cached_context);
3988c2ecf20Sopenharmony_ci	realm->cached_context = snapc;
3998c2ecf20Sopenharmony_ci	/* queue realm for cap_snap creation */
4008c2ecf20Sopenharmony_ci	list_add_tail(&realm->dirty_item, dirty_realms);
4018c2ecf20Sopenharmony_ci	return 0;
4028c2ecf20Sopenharmony_ci
4038c2ecf20Sopenharmony_cifail:
4048c2ecf20Sopenharmony_ci	/*
4058c2ecf20Sopenharmony_ci	 * if we fail, clear old (incorrect) cached_context... hopefully
4068c2ecf20Sopenharmony_ci	 * we'll have better luck building it later
4078c2ecf20Sopenharmony_ci	 */
4088c2ecf20Sopenharmony_ci	if (realm->cached_context) {
4098c2ecf20Sopenharmony_ci		ceph_put_snap_context(realm->cached_context);
4108c2ecf20Sopenharmony_ci		realm->cached_context = NULL;
4118c2ecf20Sopenharmony_ci	}
4128c2ecf20Sopenharmony_ci	pr_err("build_snap_context %llx %p fail %d\n", realm->ino,
4138c2ecf20Sopenharmony_ci	       realm, err);
4148c2ecf20Sopenharmony_ci	return err;
4158c2ecf20Sopenharmony_ci}
4168c2ecf20Sopenharmony_ci
4178c2ecf20Sopenharmony_ci/*
4188c2ecf20Sopenharmony_ci * rebuild snap context for the given realm and all of its children.
4198c2ecf20Sopenharmony_ci */
4208c2ecf20Sopenharmony_cistatic void rebuild_snap_realms(struct ceph_snap_realm *realm,
4218c2ecf20Sopenharmony_ci				struct list_head *dirty_realms)
4228c2ecf20Sopenharmony_ci{
4238c2ecf20Sopenharmony_ci	struct ceph_snap_realm *child;
4248c2ecf20Sopenharmony_ci
4258c2ecf20Sopenharmony_ci	dout("rebuild_snap_realms %llx %p\n", realm->ino, realm);
4268c2ecf20Sopenharmony_ci	build_snap_context(realm, dirty_realms);
4278c2ecf20Sopenharmony_ci
4288c2ecf20Sopenharmony_ci	list_for_each_entry(child, &realm->children, child_item)
4298c2ecf20Sopenharmony_ci		rebuild_snap_realms(child, dirty_realms);
4308c2ecf20Sopenharmony_ci}
4318c2ecf20Sopenharmony_ci
4328c2ecf20Sopenharmony_ci
4338c2ecf20Sopenharmony_ci/*
4348c2ecf20Sopenharmony_ci * helper to allocate and decode an array of snapids.  free prior
4358c2ecf20Sopenharmony_ci * instance, if any.
4368c2ecf20Sopenharmony_ci */
4378c2ecf20Sopenharmony_cistatic int dup_array(u64 **dst, __le64 *src, u32 num)
4388c2ecf20Sopenharmony_ci{
4398c2ecf20Sopenharmony_ci	u32 i;
4408c2ecf20Sopenharmony_ci
4418c2ecf20Sopenharmony_ci	kfree(*dst);
4428c2ecf20Sopenharmony_ci	if (num) {
4438c2ecf20Sopenharmony_ci		*dst = kcalloc(num, sizeof(u64), GFP_NOFS);
4448c2ecf20Sopenharmony_ci		if (!*dst)
4458c2ecf20Sopenharmony_ci			return -ENOMEM;
4468c2ecf20Sopenharmony_ci		for (i = 0; i < num; i++)
4478c2ecf20Sopenharmony_ci			(*dst)[i] = get_unaligned_le64(src + i);
4488c2ecf20Sopenharmony_ci	} else {
4498c2ecf20Sopenharmony_ci		*dst = NULL;
4508c2ecf20Sopenharmony_ci	}
4518c2ecf20Sopenharmony_ci	return 0;
4528c2ecf20Sopenharmony_ci}
4538c2ecf20Sopenharmony_ci
4548c2ecf20Sopenharmony_cistatic bool has_new_snaps(struct ceph_snap_context *o,
4558c2ecf20Sopenharmony_ci			  struct ceph_snap_context *n)
4568c2ecf20Sopenharmony_ci{
4578c2ecf20Sopenharmony_ci	if (n->num_snaps == 0)
4588c2ecf20Sopenharmony_ci		return false;
4598c2ecf20Sopenharmony_ci	/* snaps are in descending order */
4608c2ecf20Sopenharmony_ci	return n->snaps[0] > o->seq;
4618c2ecf20Sopenharmony_ci}
4628c2ecf20Sopenharmony_ci
4638c2ecf20Sopenharmony_ci/*
4648c2ecf20Sopenharmony_ci * When a snapshot is applied, the size/mtime inode metadata is queued
4658c2ecf20Sopenharmony_ci * in a ceph_cap_snap (one for each snapshot) until writeback
4668c2ecf20Sopenharmony_ci * completes and the metadata can be flushed back to the MDS.
4678c2ecf20Sopenharmony_ci *
4688c2ecf20Sopenharmony_ci * However, if a (sync) write is currently in-progress when we apply
4698c2ecf20Sopenharmony_ci * the snapshot, we have to wait until the write succeeds or fails
4708c2ecf20Sopenharmony_ci * (and a final size/mtime is known).  In this case the
4718c2ecf20Sopenharmony_ci * cap_snap->writing = 1, and is said to be "pending."  When the write
4728c2ecf20Sopenharmony_ci * finishes, we __ceph_finish_cap_snap().
4738c2ecf20Sopenharmony_ci *
4748c2ecf20Sopenharmony_ci * Caller must hold snap_rwsem for read (i.e., the realm topology won't
4758c2ecf20Sopenharmony_ci * change).
4768c2ecf20Sopenharmony_ci */
4778c2ecf20Sopenharmony_civoid ceph_queue_cap_snap(struct ceph_inode_info *ci)
4788c2ecf20Sopenharmony_ci{
4798c2ecf20Sopenharmony_ci	struct inode *inode = &ci->vfs_inode;
4808c2ecf20Sopenharmony_ci	struct ceph_cap_snap *capsnap;
4818c2ecf20Sopenharmony_ci	struct ceph_snap_context *old_snapc, *new_snapc;
4828c2ecf20Sopenharmony_ci	struct ceph_buffer *old_blob = NULL;
4838c2ecf20Sopenharmony_ci	int used, dirty;
4848c2ecf20Sopenharmony_ci
4858c2ecf20Sopenharmony_ci	capsnap = kzalloc(sizeof(*capsnap), GFP_NOFS);
4868c2ecf20Sopenharmony_ci	if (!capsnap) {
4878c2ecf20Sopenharmony_ci		pr_err("ENOMEM allocating ceph_cap_snap on %p\n", inode);
4888c2ecf20Sopenharmony_ci		return;
4898c2ecf20Sopenharmony_ci	}
4908c2ecf20Sopenharmony_ci	capsnap->cap_flush.is_capsnap = true;
4918c2ecf20Sopenharmony_ci	INIT_LIST_HEAD(&capsnap->cap_flush.i_list);
4928c2ecf20Sopenharmony_ci	INIT_LIST_HEAD(&capsnap->cap_flush.g_list);
4938c2ecf20Sopenharmony_ci
4948c2ecf20Sopenharmony_ci	spin_lock(&ci->i_ceph_lock);
4958c2ecf20Sopenharmony_ci	used = __ceph_caps_used(ci);
4968c2ecf20Sopenharmony_ci	dirty = __ceph_caps_dirty(ci);
4978c2ecf20Sopenharmony_ci
4988c2ecf20Sopenharmony_ci	old_snapc = ci->i_head_snapc;
4998c2ecf20Sopenharmony_ci	new_snapc = ci->i_snap_realm->cached_context;
5008c2ecf20Sopenharmony_ci
5018c2ecf20Sopenharmony_ci	/*
5028c2ecf20Sopenharmony_ci	 * If there is a write in progress, treat that as a dirty Fw,
5038c2ecf20Sopenharmony_ci	 * even though it hasn't completed yet; by the time we finish
5048c2ecf20Sopenharmony_ci	 * up this capsnap it will be.
5058c2ecf20Sopenharmony_ci	 */
5068c2ecf20Sopenharmony_ci	if (used & CEPH_CAP_FILE_WR)
5078c2ecf20Sopenharmony_ci		dirty |= CEPH_CAP_FILE_WR;
5088c2ecf20Sopenharmony_ci
5098c2ecf20Sopenharmony_ci	if (__ceph_have_pending_cap_snap(ci)) {
5108c2ecf20Sopenharmony_ci		/* there is no point in queuing multiple "pending" cap_snaps,
5118c2ecf20Sopenharmony_ci		   as no new writes are allowed to start when pending, so any
5128c2ecf20Sopenharmony_ci		   writes in progress now were started before the previous
5138c2ecf20Sopenharmony_ci		   cap_snap.  lucky us. */
5148c2ecf20Sopenharmony_ci		dout("queue_cap_snap %p already pending\n", inode);
5158c2ecf20Sopenharmony_ci		goto update_snapc;
5168c2ecf20Sopenharmony_ci	}
5178c2ecf20Sopenharmony_ci	if (ci->i_wrbuffer_ref_head == 0 &&
5188c2ecf20Sopenharmony_ci	    !(dirty & (CEPH_CAP_ANY_EXCL|CEPH_CAP_FILE_WR))) {
5198c2ecf20Sopenharmony_ci		dout("queue_cap_snap %p nothing dirty|writing\n", inode);
5208c2ecf20Sopenharmony_ci		goto update_snapc;
5218c2ecf20Sopenharmony_ci	}
5228c2ecf20Sopenharmony_ci
5238c2ecf20Sopenharmony_ci	BUG_ON(!old_snapc);
5248c2ecf20Sopenharmony_ci
5258c2ecf20Sopenharmony_ci	/*
5268c2ecf20Sopenharmony_ci	 * There is no need to send FLUSHSNAP message to MDS if there is
5278c2ecf20Sopenharmony_ci	 * no new snapshot. But when there is dirty pages or on-going
5288c2ecf20Sopenharmony_ci	 * writes, we still need to create cap_snap. cap_snap is needed
5298c2ecf20Sopenharmony_ci	 * by the write path and page writeback path.
5308c2ecf20Sopenharmony_ci	 *
5318c2ecf20Sopenharmony_ci	 * also see ceph_try_drop_cap_snap()
5328c2ecf20Sopenharmony_ci	 */
5338c2ecf20Sopenharmony_ci	if (has_new_snaps(old_snapc, new_snapc)) {
5348c2ecf20Sopenharmony_ci		if (dirty & (CEPH_CAP_ANY_EXCL|CEPH_CAP_FILE_WR))
5358c2ecf20Sopenharmony_ci			capsnap->need_flush = true;
5368c2ecf20Sopenharmony_ci	} else {
5378c2ecf20Sopenharmony_ci		if (!(used & CEPH_CAP_FILE_WR) &&
5388c2ecf20Sopenharmony_ci		    ci->i_wrbuffer_ref_head == 0) {
5398c2ecf20Sopenharmony_ci			dout("queue_cap_snap %p "
5408c2ecf20Sopenharmony_ci			     "no new_snap|dirty_page|writing\n", inode);
5418c2ecf20Sopenharmony_ci			goto update_snapc;
5428c2ecf20Sopenharmony_ci		}
5438c2ecf20Sopenharmony_ci	}
5448c2ecf20Sopenharmony_ci
5458c2ecf20Sopenharmony_ci	dout("queue_cap_snap %p cap_snap %p queuing under %p %s %s\n",
5468c2ecf20Sopenharmony_ci	     inode, capsnap, old_snapc, ceph_cap_string(dirty),
5478c2ecf20Sopenharmony_ci	     capsnap->need_flush ? "" : "no_flush");
5488c2ecf20Sopenharmony_ci	ihold(inode);
5498c2ecf20Sopenharmony_ci
5508c2ecf20Sopenharmony_ci	refcount_set(&capsnap->nref, 1);
5518c2ecf20Sopenharmony_ci	INIT_LIST_HEAD(&capsnap->ci_item);
5528c2ecf20Sopenharmony_ci
5538c2ecf20Sopenharmony_ci	capsnap->follows = old_snapc->seq;
5548c2ecf20Sopenharmony_ci	capsnap->issued = __ceph_caps_issued(ci, NULL);
5558c2ecf20Sopenharmony_ci	capsnap->dirty = dirty;
5568c2ecf20Sopenharmony_ci
5578c2ecf20Sopenharmony_ci	capsnap->mode = inode->i_mode;
5588c2ecf20Sopenharmony_ci	capsnap->uid = inode->i_uid;
5598c2ecf20Sopenharmony_ci	capsnap->gid = inode->i_gid;
5608c2ecf20Sopenharmony_ci
5618c2ecf20Sopenharmony_ci	if (dirty & CEPH_CAP_XATTR_EXCL) {
5628c2ecf20Sopenharmony_ci		old_blob = __ceph_build_xattrs_blob(ci);
5638c2ecf20Sopenharmony_ci		capsnap->xattr_blob =
5648c2ecf20Sopenharmony_ci			ceph_buffer_get(ci->i_xattrs.blob);
5658c2ecf20Sopenharmony_ci		capsnap->xattr_version = ci->i_xattrs.version;
5668c2ecf20Sopenharmony_ci	} else {
5678c2ecf20Sopenharmony_ci		capsnap->xattr_blob = NULL;
5688c2ecf20Sopenharmony_ci		capsnap->xattr_version = 0;
5698c2ecf20Sopenharmony_ci	}
5708c2ecf20Sopenharmony_ci
5718c2ecf20Sopenharmony_ci	capsnap->inline_data = ci->i_inline_version != CEPH_INLINE_NONE;
5728c2ecf20Sopenharmony_ci
5738c2ecf20Sopenharmony_ci	/* dirty page count moved from _head to this cap_snap;
5748c2ecf20Sopenharmony_ci	   all subsequent writes page dirties occur _after_ this
5758c2ecf20Sopenharmony_ci	   snapshot. */
5768c2ecf20Sopenharmony_ci	capsnap->dirty_pages = ci->i_wrbuffer_ref_head;
5778c2ecf20Sopenharmony_ci	ci->i_wrbuffer_ref_head = 0;
5788c2ecf20Sopenharmony_ci	capsnap->context = old_snapc;
5798c2ecf20Sopenharmony_ci	list_add_tail(&capsnap->ci_item, &ci->i_cap_snaps);
5808c2ecf20Sopenharmony_ci
5818c2ecf20Sopenharmony_ci	if (used & CEPH_CAP_FILE_WR) {
5828c2ecf20Sopenharmony_ci		dout("queue_cap_snap %p cap_snap %p snapc %p"
5838c2ecf20Sopenharmony_ci		     " seq %llu used WR, now pending\n", inode,
5848c2ecf20Sopenharmony_ci		     capsnap, old_snapc, old_snapc->seq);
5858c2ecf20Sopenharmony_ci		capsnap->writing = 1;
5868c2ecf20Sopenharmony_ci	} else {
5878c2ecf20Sopenharmony_ci		/* note mtime, size NOW. */
5888c2ecf20Sopenharmony_ci		__ceph_finish_cap_snap(ci, capsnap);
5898c2ecf20Sopenharmony_ci	}
5908c2ecf20Sopenharmony_ci	capsnap = NULL;
5918c2ecf20Sopenharmony_ci	old_snapc = NULL;
5928c2ecf20Sopenharmony_ci
5938c2ecf20Sopenharmony_ciupdate_snapc:
5948c2ecf20Sopenharmony_ci       if (ci->i_wrbuffer_ref_head == 0 &&
5958c2ecf20Sopenharmony_ci           ci->i_wr_ref == 0 &&
5968c2ecf20Sopenharmony_ci           ci->i_dirty_caps == 0 &&
5978c2ecf20Sopenharmony_ci           ci->i_flushing_caps == 0) {
5988c2ecf20Sopenharmony_ci               ci->i_head_snapc = NULL;
5998c2ecf20Sopenharmony_ci       } else {
6008c2ecf20Sopenharmony_ci		ci->i_head_snapc = ceph_get_snap_context(new_snapc);
6018c2ecf20Sopenharmony_ci		dout(" new snapc is %p\n", new_snapc);
6028c2ecf20Sopenharmony_ci	}
6038c2ecf20Sopenharmony_ci	spin_unlock(&ci->i_ceph_lock);
6048c2ecf20Sopenharmony_ci
6058c2ecf20Sopenharmony_ci	ceph_buffer_put(old_blob);
6068c2ecf20Sopenharmony_ci	kfree(capsnap);
6078c2ecf20Sopenharmony_ci	ceph_put_snap_context(old_snapc);
6088c2ecf20Sopenharmony_ci}
6098c2ecf20Sopenharmony_ci
6108c2ecf20Sopenharmony_ci/*
6118c2ecf20Sopenharmony_ci * Finalize the size, mtime for a cap_snap.. that is, settle on final values
6128c2ecf20Sopenharmony_ci * to be used for the snapshot, to be flushed back to the mds.
6138c2ecf20Sopenharmony_ci *
6148c2ecf20Sopenharmony_ci * If capsnap can now be flushed, add to snap_flush list, and return 1.
6158c2ecf20Sopenharmony_ci *
6168c2ecf20Sopenharmony_ci * Caller must hold i_ceph_lock.
6178c2ecf20Sopenharmony_ci */
6188c2ecf20Sopenharmony_ciint __ceph_finish_cap_snap(struct ceph_inode_info *ci,
6198c2ecf20Sopenharmony_ci			    struct ceph_cap_snap *capsnap)
6208c2ecf20Sopenharmony_ci{
6218c2ecf20Sopenharmony_ci	struct inode *inode = &ci->vfs_inode;
6228c2ecf20Sopenharmony_ci	struct ceph_mds_client *mdsc = ceph_sb_to_mdsc(inode->i_sb);
6238c2ecf20Sopenharmony_ci
6248c2ecf20Sopenharmony_ci	BUG_ON(capsnap->writing);
6258c2ecf20Sopenharmony_ci	capsnap->size = inode->i_size;
6268c2ecf20Sopenharmony_ci	capsnap->mtime = inode->i_mtime;
6278c2ecf20Sopenharmony_ci	capsnap->atime = inode->i_atime;
6288c2ecf20Sopenharmony_ci	capsnap->ctime = inode->i_ctime;
6298c2ecf20Sopenharmony_ci	capsnap->btime = ci->i_btime;
6308c2ecf20Sopenharmony_ci	capsnap->change_attr = inode_peek_iversion_raw(inode);
6318c2ecf20Sopenharmony_ci	capsnap->time_warp_seq = ci->i_time_warp_seq;
6328c2ecf20Sopenharmony_ci	capsnap->truncate_size = ci->i_truncate_size;
6338c2ecf20Sopenharmony_ci	capsnap->truncate_seq = ci->i_truncate_seq;
6348c2ecf20Sopenharmony_ci	if (capsnap->dirty_pages) {
6358c2ecf20Sopenharmony_ci		dout("finish_cap_snap %p cap_snap %p snapc %p %llu %s s=%llu "
6368c2ecf20Sopenharmony_ci		     "still has %d dirty pages\n", inode, capsnap,
6378c2ecf20Sopenharmony_ci		     capsnap->context, capsnap->context->seq,
6388c2ecf20Sopenharmony_ci		     ceph_cap_string(capsnap->dirty), capsnap->size,
6398c2ecf20Sopenharmony_ci		     capsnap->dirty_pages);
6408c2ecf20Sopenharmony_ci		return 0;
6418c2ecf20Sopenharmony_ci	}
6428c2ecf20Sopenharmony_ci
6438c2ecf20Sopenharmony_ci	ci->i_ceph_flags |= CEPH_I_FLUSH_SNAPS;
6448c2ecf20Sopenharmony_ci	dout("finish_cap_snap %p cap_snap %p snapc %p %llu %s s=%llu\n",
6458c2ecf20Sopenharmony_ci	     inode, capsnap, capsnap->context,
6468c2ecf20Sopenharmony_ci	     capsnap->context->seq, ceph_cap_string(capsnap->dirty),
6478c2ecf20Sopenharmony_ci	     capsnap->size);
6488c2ecf20Sopenharmony_ci
6498c2ecf20Sopenharmony_ci	spin_lock(&mdsc->snap_flush_lock);
6508c2ecf20Sopenharmony_ci	if (list_empty(&ci->i_snap_flush_item)) {
6518c2ecf20Sopenharmony_ci		ihold(inode);
6528c2ecf20Sopenharmony_ci		list_add_tail(&ci->i_snap_flush_item, &mdsc->snap_flush_list);
6538c2ecf20Sopenharmony_ci	}
6548c2ecf20Sopenharmony_ci	spin_unlock(&mdsc->snap_flush_lock);
6558c2ecf20Sopenharmony_ci	return 1;  /* caller may want to ceph_flush_snaps */
6568c2ecf20Sopenharmony_ci}
6578c2ecf20Sopenharmony_ci
6588c2ecf20Sopenharmony_ci/*
6598c2ecf20Sopenharmony_ci * Queue cap_snaps for snap writeback for this realm and its children.
6608c2ecf20Sopenharmony_ci * Called under snap_rwsem, so realm topology won't change.
6618c2ecf20Sopenharmony_ci */
6628c2ecf20Sopenharmony_cistatic void queue_realm_cap_snaps(struct ceph_snap_realm *realm)
6638c2ecf20Sopenharmony_ci{
6648c2ecf20Sopenharmony_ci	struct ceph_inode_info *ci;
6658c2ecf20Sopenharmony_ci	struct inode *lastinode = NULL;
6668c2ecf20Sopenharmony_ci
6678c2ecf20Sopenharmony_ci	dout("queue_realm_cap_snaps %p %llx inodes\n", realm, realm->ino);
6688c2ecf20Sopenharmony_ci
6698c2ecf20Sopenharmony_ci	spin_lock(&realm->inodes_with_caps_lock);
6708c2ecf20Sopenharmony_ci	list_for_each_entry(ci, &realm->inodes_with_caps, i_snap_realm_item) {
6718c2ecf20Sopenharmony_ci		struct inode *inode = igrab(&ci->vfs_inode);
6728c2ecf20Sopenharmony_ci		if (!inode)
6738c2ecf20Sopenharmony_ci			continue;
6748c2ecf20Sopenharmony_ci		spin_unlock(&realm->inodes_with_caps_lock);
6758c2ecf20Sopenharmony_ci		/* avoid calling iput_final() while holding
6768c2ecf20Sopenharmony_ci		 * mdsc->snap_rwsem or in mds dispatch threads */
6778c2ecf20Sopenharmony_ci		ceph_async_iput(lastinode);
6788c2ecf20Sopenharmony_ci		lastinode = inode;
6798c2ecf20Sopenharmony_ci		ceph_queue_cap_snap(ci);
6808c2ecf20Sopenharmony_ci		spin_lock(&realm->inodes_with_caps_lock);
6818c2ecf20Sopenharmony_ci	}
6828c2ecf20Sopenharmony_ci	spin_unlock(&realm->inodes_with_caps_lock);
6838c2ecf20Sopenharmony_ci	ceph_async_iput(lastinode);
6848c2ecf20Sopenharmony_ci
6858c2ecf20Sopenharmony_ci	dout("queue_realm_cap_snaps %p %llx done\n", realm, realm->ino);
6868c2ecf20Sopenharmony_ci}
6878c2ecf20Sopenharmony_ci
6888c2ecf20Sopenharmony_ci/*
6898c2ecf20Sopenharmony_ci * Parse and apply a snapblob "snap trace" from the MDS.  This specifies
6908c2ecf20Sopenharmony_ci * the snap realm parameters from a given realm and all of its ancestors,
6918c2ecf20Sopenharmony_ci * up to the root.
6928c2ecf20Sopenharmony_ci *
6938c2ecf20Sopenharmony_ci * Caller must hold snap_rwsem for write.
6948c2ecf20Sopenharmony_ci */
6958c2ecf20Sopenharmony_ciint ceph_update_snap_trace(struct ceph_mds_client *mdsc,
6968c2ecf20Sopenharmony_ci			   void *p, void *e, bool deletion,
6978c2ecf20Sopenharmony_ci			   struct ceph_snap_realm **realm_ret)
6988c2ecf20Sopenharmony_ci{
6998c2ecf20Sopenharmony_ci	struct ceph_mds_snap_realm *ri;    /* encoded */
7008c2ecf20Sopenharmony_ci	__le64 *snaps;                     /* encoded */
7018c2ecf20Sopenharmony_ci	__le64 *prior_parent_snaps;        /* encoded */
7028c2ecf20Sopenharmony_ci	struct ceph_snap_realm *realm;
7038c2ecf20Sopenharmony_ci	struct ceph_snap_realm *first_realm = NULL;
7048c2ecf20Sopenharmony_ci	struct ceph_snap_realm *realm_to_rebuild = NULL;
7058c2ecf20Sopenharmony_ci	int rebuild_snapcs;
7068c2ecf20Sopenharmony_ci	int err = -ENOMEM;
7078c2ecf20Sopenharmony_ci	LIST_HEAD(dirty_realms);
7088c2ecf20Sopenharmony_ci
7098c2ecf20Sopenharmony_ci	lockdep_assert_held_write(&mdsc->snap_rwsem);
7108c2ecf20Sopenharmony_ci
7118c2ecf20Sopenharmony_ci	dout("update_snap_trace deletion=%d\n", deletion);
7128c2ecf20Sopenharmony_cimore:
7138c2ecf20Sopenharmony_ci	realm = NULL;
7148c2ecf20Sopenharmony_ci	rebuild_snapcs = 0;
7158c2ecf20Sopenharmony_ci	ceph_decode_need(&p, e, sizeof(*ri), bad);
7168c2ecf20Sopenharmony_ci	ri = p;
7178c2ecf20Sopenharmony_ci	p += sizeof(*ri);
7188c2ecf20Sopenharmony_ci	ceph_decode_need(&p, e, sizeof(u64)*(le32_to_cpu(ri->num_snaps) +
7198c2ecf20Sopenharmony_ci			    le32_to_cpu(ri->num_prior_parent_snaps)), bad);
7208c2ecf20Sopenharmony_ci	snaps = p;
7218c2ecf20Sopenharmony_ci	p += sizeof(u64) * le32_to_cpu(ri->num_snaps);
7228c2ecf20Sopenharmony_ci	prior_parent_snaps = p;
7238c2ecf20Sopenharmony_ci	p += sizeof(u64) * le32_to_cpu(ri->num_prior_parent_snaps);
7248c2ecf20Sopenharmony_ci
7258c2ecf20Sopenharmony_ci	realm = ceph_lookup_snap_realm(mdsc, le64_to_cpu(ri->ino));
7268c2ecf20Sopenharmony_ci	if (!realm) {
7278c2ecf20Sopenharmony_ci		realm = ceph_create_snap_realm(mdsc, le64_to_cpu(ri->ino));
7288c2ecf20Sopenharmony_ci		if (IS_ERR(realm)) {
7298c2ecf20Sopenharmony_ci			err = PTR_ERR(realm);
7308c2ecf20Sopenharmony_ci			goto fail;
7318c2ecf20Sopenharmony_ci		}
7328c2ecf20Sopenharmony_ci	}
7338c2ecf20Sopenharmony_ci
7348c2ecf20Sopenharmony_ci	/* ensure the parent is correct */
7358c2ecf20Sopenharmony_ci	err = adjust_snap_realm_parent(mdsc, realm, le64_to_cpu(ri->parent));
7368c2ecf20Sopenharmony_ci	if (err < 0)
7378c2ecf20Sopenharmony_ci		goto fail;
7388c2ecf20Sopenharmony_ci	rebuild_snapcs += err;
7398c2ecf20Sopenharmony_ci
7408c2ecf20Sopenharmony_ci	if (le64_to_cpu(ri->seq) > realm->seq) {
7418c2ecf20Sopenharmony_ci		dout("update_snap_trace updating %llx %p %lld -> %lld\n",
7428c2ecf20Sopenharmony_ci		     realm->ino, realm, realm->seq, le64_to_cpu(ri->seq));
7438c2ecf20Sopenharmony_ci		/* update realm parameters, snap lists */
7448c2ecf20Sopenharmony_ci		realm->seq = le64_to_cpu(ri->seq);
7458c2ecf20Sopenharmony_ci		realm->created = le64_to_cpu(ri->created);
7468c2ecf20Sopenharmony_ci		realm->parent_since = le64_to_cpu(ri->parent_since);
7478c2ecf20Sopenharmony_ci
7488c2ecf20Sopenharmony_ci		realm->num_snaps = le32_to_cpu(ri->num_snaps);
7498c2ecf20Sopenharmony_ci		err = dup_array(&realm->snaps, snaps, realm->num_snaps);
7508c2ecf20Sopenharmony_ci		if (err < 0)
7518c2ecf20Sopenharmony_ci			goto fail;
7528c2ecf20Sopenharmony_ci
7538c2ecf20Sopenharmony_ci		realm->num_prior_parent_snaps =
7548c2ecf20Sopenharmony_ci			le32_to_cpu(ri->num_prior_parent_snaps);
7558c2ecf20Sopenharmony_ci		err = dup_array(&realm->prior_parent_snaps, prior_parent_snaps,
7568c2ecf20Sopenharmony_ci				realm->num_prior_parent_snaps);
7578c2ecf20Sopenharmony_ci		if (err < 0)
7588c2ecf20Sopenharmony_ci			goto fail;
7598c2ecf20Sopenharmony_ci
7608c2ecf20Sopenharmony_ci		if (realm->seq > mdsc->last_snap_seq)
7618c2ecf20Sopenharmony_ci			mdsc->last_snap_seq = realm->seq;
7628c2ecf20Sopenharmony_ci
7638c2ecf20Sopenharmony_ci		rebuild_snapcs = 1;
7648c2ecf20Sopenharmony_ci	} else if (!realm->cached_context) {
7658c2ecf20Sopenharmony_ci		dout("update_snap_trace %llx %p seq %lld new\n",
7668c2ecf20Sopenharmony_ci		     realm->ino, realm, realm->seq);
7678c2ecf20Sopenharmony_ci		rebuild_snapcs = 1;
7688c2ecf20Sopenharmony_ci	} else {
7698c2ecf20Sopenharmony_ci		dout("update_snap_trace %llx %p seq %lld unchanged\n",
7708c2ecf20Sopenharmony_ci		     realm->ino, realm, realm->seq);
7718c2ecf20Sopenharmony_ci	}
7728c2ecf20Sopenharmony_ci
7738c2ecf20Sopenharmony_ci	dout("done with %llx %p, rebuild_snapcs=%d, %p %p\n", realm->ino,
7748c2ecf20Sopenharmony_ci	     realm, rebuild_snapcs, p, e);
7758c2ecf20Sopenharmony_ci
7768c2ecf20Sopenharmony_ci	/*
7778c2ecf20Sopenharmony_ci	 * this will always track the uppest parent realm from which
7788c2ecf20Sopenharmony_ci	 * we need to rebuild the snapshot contexts _downward_ in
7798c2ecf20Sopenharmony_ci	 * hierarchy.
7808c2ecf20Sopenharmony_ci	 */
7818c2ecf20Sopenharmony_ci	if (rebuild_snapcs)
7828c2ecf20Sopenharmony_ci		realm_to_rebuild = realm;
7838c2ecf20Sopenharmony_ci
7848c2ecf20Sopenharmony_ci	/* rebuild_snapcs when we reach the _end_ (root) of the trace */
7858c2ecf20Sopenharmony_ci	if (realm_to_rebuild && p >= e)
7868c2ecf20Sopenharmony_ci		rebuild_snap_realms(realm_to_rebuild, &dirty_realms);
7878c2ecf20Sopenharmony_ci
7888c2ecf20Sopenharmony_ci	if (!first_realm)
7898c2ecf20Sopenharmony_ci		first_realm = realm;
7908c2ecf20Sopenharmony_ci	else
7918c2ecf20Sopenharmony_ci		ceph_put_snap_realm(mdsc, realm);
7928c2ecf20Sopenharmony_ci
7938c2ecf20Sopenharmony_ci	if (p < e)
7948c2ecf20Sopenharmony_ci		goto more;
7958c2ecf20Sopenharmony_ci
7968c2ecf20Sopenharmony_ci	/*
7978c2ecf20Sopenharmony_ci	 * queue cap snaps _after_ we've built the new snap contexts,
7988c2ecf20Sopenharmony_ci	 * so that i_head_snapc can be set appropriately.
7998c2ecf20Sopenharmony_ci	 */
8008c2ecf20Sopenharmony_ci	while (!list_empty(&dirty_realms)) {
8018c2ecf20Sopenharmony_ci		realm = list_first_entry(&dirty_realms, struct ceph_snap_realm,
8028c2ecf20Sopenharmony_ci					 dirty_item);
8038c2ecf20Sopenharmony_ci		list_del_init(&realm->dirty_item);
8048c2ecf20Sopenharmony_ci		queue_realm_cap_snaps(realm);
8058c2ecf20Sopenharmony_ci	}
8068c2ecf20Sopenharmony_ci
8078c2ecf20Sopenharmony_ci	if (realm_ret)
8088c2ecf20Sopenharmony_ci		*realm_ret = first_realm;
8098c2ecf20Sopenharmony_ci	else
8108c2ecf20Sopenharmony_ci		ceph_put_snap_realm(mdsc, first_realm);
8118c2ecf20Sopenharmony_ci
8128c2ecf20Sopenharmony_ci	__cleanup_empty_realms(mdsc);
8138c2ecf20Sopenharmony_ci	return 0;
8148c2ecf20Sopenharmony_ci
8158c2ecf20Sopenharmony_cibad:
8168c2ecf20Sopenharmony_ci	err = -EINVAL;
8178c2ecf20Sopenharmony_cifail:
8188c2ecf20Sopenharmony_ci	if (realm && !IS_ERR(realm))
8198c2ecf20Sopenharmony_ci		ceph_put_snap_realm(mdsc, realm);
8208c2ecf20Sopenharmony_ci	if (first_realm)
8218c2ecf20Sopenharmony_ci		ceph_put_snap_realm(mdsc, first_realm);
8228c2ecf20Sopenharmony_ci	pr_err("update_snap_trace error %d\n", err);
8238c2ecf20Sopenharmony_ci	return err;
8248c2ecf20Sopenharmony_ci}
8258c2ecf20Sopenharmony_ci
8268c2ecf20Sopenharmony_ci
8278c2ecf20Sopenharmony_ci/*
8288c2ecf20Sopenharmony_ci * Send any cap_snaps that are queued for flush.  Try to carry
8298c2ecf20Sopenharmony_ci * s_mutex across multiple snap flushes to avoid locking overhead.
8308c2ecf20Sopenharmony_ci *
8318c2ecf20Sopenharmony_ci * Caller holds no locks.
8328c2ecf20Sopenharmony_ci */
8338c2ecf20Sopenharmony_cistatic void flush_snaps(struct ceph_mds_client *mdsc)
8348c2ecf20Sopenharmony_ci{
8358c2ecf20Sopenharmony_ci	struct ceph_inode_info *ci;
8368c2ecf20Sopenharmony_ci	struct inode *inode;
8378c2ecf20Sopenharmony_ci	struct ceph_mds_session *session = NULL;
8388c2ecf20Sopenharmony_ci
8398c2ecf20Sopenharmony_ci	dout("flush_snaps\n");
8408c2ecf20Sopenharmony_ci	spin_lock(&mdsc->snap_flush_lock);
8418c2ecf20Sopenharmony_ci	while (!list_empty(&mdsc->snap_flush_list)) {
8428c2ecf20Sopenharmony_ci		ci = list_first_entry(&mdsc->snap_flush_list,
8438c2ecf20Sopenharmony_ci				struct ceph_inode_info, i_snap_flush_item);
8448c2ecf20Sopenharmony_ci		inode = &ci->vfs_inode;
8458c2ecf20Sopenharmony_ci		ihold(inode);
8468c2ecf20Sopenharmony_ci		spin_unlock(&mdsc->snap_flush_lock);
8478c2ecf20Sopenharmony_ci		ceph_flush_snaps(ci, &session);
8488c2ecf20Sopenharmony_ci		/* avoid calling iput_final() while holding
8498c2ecf20Sopenharmony_ci		 * session->s_mutex or in mds dispatch threads */
8508c2ecf20Sopenharmony_ci		ceph_async_iput(inode);
8518c2ecf20Sopenharmony_ci		spin_lock(&mdsc->snap_flush_lock);
8528c2ecf20Sopenharmony_ci	}
8538c2ecf20Sopenharmony_ci	spin_unlock(&mdsc->snap_flush_lock);
8548c2ecf20Sopenharmony_ci
8558c2ecf20Sopenharmony_ci	if (session) {
8568c2ecf20Sopenharmony_ci		mutex_unlock(&session->s_mutex);
8578c2ecf20Sopenharmony_ci		ceph_put_mds_session(session);
8588c2ecf20Sopenharmony_ci	}
8598c2ecf20Sopenharmony_ci	dout("flush_snaps done\n");
8608c2ecf20Sopenharmony_ci}
8618c2ecf20Sopenharmony_ci
8628c2ecf20Sopenharmony_ci
8638c2ecf20Sopenharmony_ci/*
8648c2ecf20Sopenharmony_ci * Handle a snap notification from the MDS.
8658c2ecf20Sopenharmony_ci *
8668c2ecf20Sopenharmony_ci * This can take two basic forms: the simplest is just a snap creation
8678c2ecf20Sopenharmony_ci * or deletion notification on an existing realm.  This should update the
8688c2ecf20Sopenharmony_ci * realm and its children.
8698c2ecf20Sopenharmony_ci *
8708c2ecf20Sopenharmony_ci * The more difficult case is realm creation, due to snap creation at a
8718c2ecf20Sopenharmony_ci * new point in the file hierarchy, or due to a rename that moves a file or
8728c2ecf20Sopenharmony_ci * directory into another realm.
8738c2ecf20Sopenharmony_ci */
8748c2ecf20Sopenharmony_civoid ceph_handle_snap(struct ceph_mds_client *mdsc,
8758c2ecf20Sopenharmony_ci		      struct ceph_mds_session *session,
8768c2ecf20Sopenharmony_ci		      struct ceph_msg *msg)
8778c2ecf20Sopenharmony_ci{
8788c2ecf20Sopenharmony_ci	struct super_block *sb = mdsc->fsc->sb;
8798c2ecf20Sopenharmony_ci	int mds = session->s_mds;
8808c2ecf20Sopenharmony_ci	u64 split;
8818c2ecf20Sopenharmony_ci	int op;
8828c2ecf20Sopenharmony_ci	int trace_len;
8838c2ecf20Sopenharmony_ci	struct ceph_snap_realm *realm = NULL;
8848c2ecf20Sopenharmony_ci	void *p = msg->front.iov_base;
8858c2ecf20Sopenharmony_ci	void *e = p + msg->front.iov_len;
8868c2ecf20Sopenharmony_ci	struct ceph_mds_snap_head *h;
8878c2ecf20Sopenharmony_ci	int num_split_inos, num_split_realms;
8888c2ecf20Sopenharmony_ci	__le64 *split_inos = NULL, *split_realms = NULL;
8898c2ecf20Sopenharmony_ci	int i;
8908c2ecf20Sopenharmony_ci	int locked_rwsem = 0;
8918c2ecf20Sopenharmony_ci
8928c2ecf20Sopenharmony_ci	/* decode */
8938c2ecf20Sopenharmony_ci	if (msg->front.iov_len < sizeof(*h))
8948c2ecf20Sopenharmony_ci		goto bad;
8958c2ecf20Sopenharmony_ci	h = p;
8968c2ecf20Sopenharmony_ci	op = le32_to_cpu(h->op);
8978c2ecf20Sopenharmony_ci	split = le64_to_cpu(h->split);   /* non-zero if we are splitting an
8988c2ecf20Sopenharmony_ci					  * existing realm */
8998c2ecf20Sopenharmony_ci	num_split_inos = le32_to_cpu(h->num_split_inos);
9008c2ecf20Sopenharmony_ci	num_split_realms = le32_to_cpu(h->num_split_realms);
9018c2ecf20Sopenharmony_ci	trace_len = le32_to_cpu(h->trace_len);
9028c2ecf20Sopenharmony_ci	p += sizeof(*h);
9038c2ecf20Sopenharmony_ci
9048c2ecf20Sopenharmony_ci	dout("handle_snap from mds%d op %s split %llx tracelen %d\n", mds,
9058c2ecf20Sopenharmony_ci	     ceph_snap_op_name(op), split, trace_len);
9068c2ecf20Sopenharmony_ci
9078c2ecf20Sopenharmony_ci	mutex_lock(&session->s_mutex);
9088c2ecf20Sopenharmony_ci	inc_session_sequence(session);
9098c2ecf20Sopenharmony_ci	mutex_unlock(&session->s_mutex);
9108c2ecf20Sopenharmony_ci
9118c2ecf20Sopenharmony_ci	down_write(&mdsc->snap_rwsem);
9128c2ecf20Sopenharmony_ci	locked_rwsem = 1;
9138c2ecf20Sopenharmony_ci
9148c2ecf20Sopenharmony_ci	if (op == CEPH_SNAP_OP_SPLIT) {
9158c2ecf20Sopenharmony_ci		struct ceph_mds_snap_realm *ri;
9168c2ecf20Sopenharmony_ci
9178c2ecf20Sopenharmony_ci		/*
9188c2ecf20Sopenharmony_ci		 * A "split" breaks part of an existing realm off into
9198c2ecf20Sopenharmony_ci		 * a new realm.  The MDS provides a list of inodes
9208c2ecf20Sopenharmony_ci		 * (with caps) and child realms that belong to the new
9218c2ecf20Sopenharmony_ci		 * child.
9228c2ecf20Sopenharmony_ci		 */
9238c2ecf20Sopenharmony_ci		split_inos = p;
9248c2ecf20Sopenharmony_ci		p += sizeof(u64) * num_split_inos;
9258c2ecf20Sopenharmony_ci		split_realms = p;
9268c2ecf20Sopenharmony_ci		p += sizeof(u64) * num_split_realms;
9278c2ecf20Sopenharmony_ci		ceph_decode_need(&p, e, sizeof(*ri), bad);
9288c2ecf20Sopenharmony_ci		/* we will peek at realm info here, but will _not_
9298c2ecf20Sopenharmony_ci		 * advance p, as the realm update will occur below in
9308c2ecf20Sopenharmony_ci		 * ceph_update_snap_trace. */
9318c2ecf20Sopenharmony_ci		ri = p;
9328c2ecf20Sopenharmony_ci
9338c2ecf20Sopenharmony_ci		realm = ceph_lookup_snap_realm(mdsc, split);
9348c2ecf20Sopenharmony_ci		if (!realm) {
9358c2ecf20Sopenharmony_ci			realm = ceph_create_snap_realm(mdsc, split);
9368c2ecf20Sopenharmony_ci			if (IS_ERR(realm))
9378c2ecf20Sopenharmony_ci				goto out;
9388c2ecf20Sopenharmony_ci		}
9398c2ecf20Sopenharmony_ci
9408c2ecf20Sopenharmony_ci		dout("splitting snap_realm %llx %p\n", realm->ino, realm);
9418c2ecf20Sopenharmony_ci		for (i = 0; i < num_split_inos; i++) {
9428c2ecf20Sopenharmony_ci			struct ceph_vino vino = {
9438c2ecf20Sopenharmony_ci				.ino = le64_to_cpu(split_inos[i]),
9448c2ecf20Sopenharmony_ci				.snap = CEPH_NOSNAP,
9458c2ecf20Sopenharmony_ci			};
9468c2ecf20Sopenharmony_ci			struct inode *inode = ceph_find_inode(sb, vino);
9478c2ecf20Sopenharmony_ci			struct ceph_inode_info *ci;
9488c2ecf20Sopenharmony_ci			struct ceph_snap_realm *oldrealm;
9498c2ecf20Sopenharmony_ci
9508c2ecf20Sopenharmony_ci			if (!inode)
9518c2ecf20Sopenharmony_ci				continue;
9528c2ecf20Sopenharmony_ci			ci = ceph_inode(inode);
9538c2ecf20Sopenharmony_ci
9548c2ecf20Sopenharmony_ci			spin_lock(&ci->i_ceph_lock);
9558c2ecf20Sopenharmony_ci			if (!ci->i_snap_realm)
9568c2ecf20Sopenharmony_ci				goto skip_inode;
9578c2ecf20Sopenharmony_ci			/*
9588c2ecf20Sopenharmony_ci			 * If this inode belongs to a realm that was
9598c2ecf20Sopenharmony_ci			 * created after our new realm, we experienced
9608c2ecf20Sopenharmony_ci			 * a race (due to another split notifications
9618c2ecf20Sopenharmony_ci			 * arriving from a different MDS).  So skip
9628c2ecf20Sopenharmony_ci			 * this inode.
9638c2ecf20Sopenharmony_ci			 */
9648c2ecf20Sopenharmony_ci			if (ci->i_snap_realm->created >
9658c2ecf20Sopenharmony_ci			    le64_to_cpu(ri->created)) {
9668c2ecf20Sopenharmony_ci				dout(" leaving %p in newer realm %llx %p\n",
9678c2ecf20Sopenharmony_ci				     inode, ci->i_snap_realm->ino,
9688c2ecf20Sopenharmony_ci				     ci->i_snap_realm);
9698c2ecf20Sopenharmony_ci				goto skip_inode;
9708c2ecf20Sopenharmony_ci			}
9718c2ecf20Sopenharmony_ci			dout(" will move %p to split realm %llx %p\n",
9728c2ecf20Sopenharmony_ci			     inode, realm->ino, realm);
9738c2ecf20Sopenharmony_ci			/*
9748c2ecf20Sopenharmony_ci			 * Move the inode to the new realm
9758c2ecf20Sopenharmony_ci			 */
9768c2ecf20Sopenharmony_ci			oldrealm = ci->i_snap_realm;
9778c2ecf20Sopenharmony_ci			spin_lock(&oldrealm->inodes_with_caps_lock);
9788c2ecf20Sopenharmony_ci			list_del_init(&ci->i_snap_realm_item);
9798c2ecf20Sopenharmony_ci			spin_unlock(&oldrealm->inodes_with_caps_lock);
9808c2ecf20Sopenharmony_ci
9818c2ecf20Sopenharmony_ci			spin_lock(&realm->inodes_with_caps_lock);
9828c2ecf20Sopenharmony_ci			list_add(&ci->i_snap_realm_item,
9838c2ecf20Sopenharmony_ci				 &realm->inodes_with_caps);
9848c2ecf20Sopenharmony_ci			ci->i_snap_realm = realm;
9858c2ecf20Sopenharmony_ci			if (realm->ino == ci->i_vino.ino)
9868c2ecf20Sopenharmony_ci                                realm->inode = inode;
9878c2ecf20Sopenharmony_ci			spin_unlock(&realm->inodes_with_caps_lock);
9888c2ecf20Sopenharmony_ci
9898c2ecf20Sopenharmony_ci			spin_unlock(&ci->i_ceph_lock);
9908c2ecf20Sopenharmony_ci
9918c2ecf20Sopenharmony_ci			ceph_get_snap_realm(mdsc, realm);
9928c2ecf20Sopenharmony_ci			ceph_put_snap_realm(mdsc, oldrealm);
9938c2ecf20Sopenharmony_ci
9948c2ecf20Sopenharmony_ci			/* avoid calling iput_final() while holding
9958c2ecf20Sopenharmony_ci			 * mdsc->snap_rwsem or mds in dispatch threads */
9968c2ecf20Sopenharmony_ci			ceph_async_iput(inode);
9978c2ecf20Sopenharmony_ci			continue;
9988c2ecf20Sopenharmony_ci
9998c2ecf20Sopenharmony_ciskip_inode:
10008c2ecf20Sopenharmony_ci			spin_unlock(&ci->i_ceph_lock);
10018c2ecf20Sopenharmony_ci			ceph_async_iput(inode);
10028c2ecf20Sopenharmony_ci		}
10038c2ecf20Sopenharmony_ci
10048c2ecf20Sopenharmony_ci		/* we may have taken some of the old realm's children. */
10058c2ecf20Sopenharmony_ci		for (i = 0; i < num_split_realms; i++) {
10068c2ecf20Sopenharmony_ci			struct ceph_snap_realm *child =
10078c2ecf20Sopenharmony_ci				__lookup_snap_realm(mdsc,
10088c2ecf20Sopenharmony_ci					   le64_to_cpu(split_realms[i]));
10098c2ecf20Sopenharmony_ci			if (!child)
10108c2ecf20Sopenharmony_ci				continue;
10118c2ecf20Sopenharmony_ci			adjust_snap_realm_parent(mdsc, child, realm->ino);
10128c2ecf20Sopenharmony_ci		}
10138c2ecf20Sopenharmony_ci	} else {
10148c2ecf20Sopenharmony_ci		/*
10158c2ecf20Sopenharmony_ci		 * In the non-split case both 'num_split_inos' and
10168c2ecf20Sopenharmony_ci		 * 'num_split_realms' should be 0, making this a no-op.
10178c2ecf20Sopenharmony_ci		 * However the MDS happens to populate 'split_realms' list
10188c2ecf20Sopenharmony_ci		 * in one of the UPDATE op cases by mistake.
10198c2ecf20Sopenharmony_ci		 *
10208c2ecf20Sopenharmony_ci		 * Skip both lists just in case to ensure that 'p' is
10218c2ecf20Sopenharmony_ci		 * positioned at the start of realm info, as expected by
10228c2ecf20Sopenharmony_ci		 * ceph_update_snap_trace().
10238c2ecf20Sopenharmony_ci		 */
10248c2ecf20Sopenharmony_ci		p += sizeof(u64) * num_split_inos;
10258c2ecf20Sopenharmony_ci		p += sizeof(u64) * num_split_realms;
10268c2ecf20Sopenharmony_ci	}
10278c2ecf20Sopenharmony_ci
10288c2ecf20Sopenharmony_ci	/*
10298c2ecf20Sopenharmony_ci	 * update using the provided snap trace. if we are deleting a
10308c2ecf20Sopenharmony_ci	 * snap, we can avoid queueing cap_snaps.
10318c2ecf20Sopenharmony_ci	 */
10328c2ecf20Sopenharmony_ci	ceph_update_snap_trace(mdsc, p, e,
10338c2ecf20Sopenharmony_ci			       op == CEPH_SNAP_OP_DESTROY, NULL);
10348c2ecf20Sopenharmony_ci
10358c2ecf20Sopenharmony_ci	if (op == CEPH_SNAP_OP_SPLIT)
10368c2ecf20Sopenharmony_ci		/* we took a reference when we created the realm, above */
10378c2ecf20Sopenharmony_ci		ceph_put_snap_realm(mdsc, realm);
10388c2ecf20Sopenharmony_ci
10398c2ecf20Sopenharmony_ci	__cleanup_empty_realms(mdsc);
10408c2ecf20Sopenharmony_ci
10418c2ecf20Sopenharmony_ci	up_write(&mdsc->snap_rwsem);
10428c2ecf20Sopenharmony_ci
10438c2ecf20Sopenharmony_ci	flush_snaps(mdsc);
10448c2ecf20Sopenharmony_ci	return;
10458c2ecf20Sopenharmony_ci
10468c2ecf20Sopenharmony_cibad:
10478c2ecf20Sopenharmony_ci	pr_err("corrupt snap message from mds%d\n", mds);
10488c2ecf20Sopenharmony_ci	ceph_msg_dump(msg);
10498c2ecf20Sopenharmony_ciout:
10508c2ecf20Sopenharmony_ci	if (locked_rwsem)
10518c2ecf20Sopenharmony_ci		up_write(&mdsc->snap_rwsem);
10528c2ecf20Sopenharmony_ci	return;
10538c2ecf20Sopenharmony_ci}
10548c2ecf20Sopenharmony_ci
10558c2ecf20Sopenharmony_cistruct ceph_snapid_map* ceph_get_snapid_map(struct ceph_mds_client *mdsc,
10568c2ecf20Sopenharmony_ci					    u64 snap)
10578c2ecf20Sopenharmony_ci{
10588c2ecf20Sopenharmony_ci	struct ceph_snapid_map *sm, *exist;
10598c2ecf20Sopenharmony_ci	struct rb_node **p, *parent;
10608c2ecf20Sopenharmony_ci	int ret;
10618c2ecf20Sopenharmony_ci
10628c2ecf20Sopenharmony_ci	exist = NULL;
10638c2ecf20Sopenharmony_ci	spin_lock(&mdsc->snapid_map_lock);
10648c2ecf20Sopenharmony_ci	p = &mdsc->snapid_map_tree.rb_node;
10658c2ecf20Sopenharmony_ci	while (*p) {
10668c2ecf20Sopenharmony_ci		exist = rb_entry(*p, struct ceph_snapid_map, node);
10678c2ecf20Sopenharmony_ci		if (snap > exist->snap) {
10688c2ecf20Sopenharmony_ci			p = &(*p)->rb_left;
10698c2ecf20Sopenharmony_ci		} else if (snap < exist->snap) {
10708c2ecf20Sopenharmony_ci			p = &(*p)->rb_right;
10718c2ecf20Sopenharmony_ci		} else {
10728c2ecf20Sopenharmony_ci			if (atomic_inc_return(&exist->ref) == 1)
10738c2ecf20Sopenharmony_ci				list_del_init(&exist->lru);
10748c2ecf20Sopenharmony_ci			break;
10758c2ecf20Sopenharmony_ci		}
10768c2ecf20Sopenharmony_ci		exist = NULL;
10778c2ecf20Sopenharmony_ci	}
10788c2ecf20Sopenharmony_ci	spin_unlock(&mdsc->snapid_map_lock);
10798c2ecf20Sopenharmony_ci	if (exist) {
10808c2ecf20Sopenharmony_ci		dout("found snapid map %llx -> %x\n", exist->snap, exist->dev);
10818c2ecf20Sopenharmony_ci		return exist;
10828c2ecf20Sopenharmony_ci	}
10838c2ecf20Sopenharmony_ci
10848c2ecf20Sopenharmony_ci	sm = kmalloc(sizeof(*sm), GFP_NOFS);
10858c2ecf20Sopenharmony_ci	if (!sm)
10868c2ecf20Sopenharmony_ci		return NULL;
10878c2ecf20Sopenharmony_ci
10888c2ecf20Sopenharmony_ci	ret = get_anon_bdev(&sm->dev);
10898c2ecf20Sopenharmony_ci	if (ret < 0) {
10908c2ecf20Sopenharmony_ci		kfree(sm);
10918c2ecf20Sopenharmony_ci		return NULL;
10928c2ecf20Sopenharmony_ci	}
10938c2ecf20Sopenharmony_ci
10948c2ecf20Sopenharmony_ci	INIT_LIST_HEAD(&sm->lru);
10958c2ecf20Sopenharmony_ci	atomic_set(&sm->ref, 1);
10968c2ecf20Sopenharmony_ci	sm->snap = snap;
10978c2ecf20Sopenharmony_ci
10988c2ecf20Sopenharmony_ci	exist = NULL;
10998c2ecf20Sopenharmony_ci	parent = NULL;
11008c2ecf20Sopenharmony_ci	p = &mdsc->snapid_map_tree.rb_node;
11018c2ecf20Sopenharmony_ci	spin_lock(&mdsc->snapid_map_lock);
11028c2ecf20Sopenharmony_ci	while (*p) {
11038c2ecf20Sopenharmony_ci		parent = *p;
11048c2ecf20Sopenharmony_ci		exist = rb_entry(*p, struct ceph_snapid_map, node);
11058c2ecf20Sopenharmony_ci		if (snap > exist->snap)
11068c2ecf20Sopenharmony_ci			p = &(*p)->rb_left;
11078c2ecf20Sopenharmony_ci		else if (snap < exist->snap)
11088c2ecf20Sopenharmony_ci			p = &(*p)->rb_right;
11098c2ecf20Sopenharmony_ci		else
11108c2ecf20Sopenharmony_ci			break;
11118c2ecf20Sopenharmony_ci		exist = NULL;
11128c2ecf20Sopenharmony_ci	}
11138c2ecf20Sopenharmony_ci	if (exist) {
11148c2ecf20Sopenharmony_ci		if (atomic_inc_return(&exist->ref) == 1)
11158c2ecf20Sopenharmony_ci			list_del_init(&exist->lru);
11168c2ecf20Sopenharmony_ci	} else {
11178c2ecf20Sopenharmony_ci		rb_link_node(&sm->node, parent, p);
11188c2ecf20Sopenharmony_ci		rb_insert_color(&sm->node, &mdsc->snapid_map_tree);
11198c2ecf20Sopenharmony_ci	}
11208c2ecf20Sopenharmony_ci	spin_unlock(&mdsc->snapid_map_lock);
11218c2ecf20Sopenharmony_ci	if (exist) {
11228c2ecf20Sopenharmony_ci		free_anon_bdev(sm->dev);
11238c2ecf20Sopenharmony_ci		kfree(sm);
11248c2ecf20Sopenharmony_ci		dout("found snapid map %llx -> %x\n", exist->snap, exist->dev);
11258c2ecf20Sopenharmony_ci		return exist;
11268c2ecf20Sopenharmony_ci	}
11278c2ecf20Sopenharmony_ci
11288c2ecf20Sopenharmony_ci	dout("create snapid map %llx -> %x\n", sm->snap, sm->dev);
11298c2ecf20Sopenharmony_ci	return sm;
11308c2ecf20Sopenharmony_ci}
11318c2ecf20Sopenharmony_ci
11328c2ecf20Sopenharmony_civoid ceph_put_snapid_map(struct ceph_mds_client* mdsc,
11338c2ecf20Sopenharmony_ci			 struct ceph_snapid_map *sm)
11348c2ecf20Sopenharmony_ci{
11358c2ecf20Sopenharmony_ci	if (!sm)
11368c2ecf20Sopenharmony_ci		return;
11378c2ecf20Sopenharmony_ci	if (atomic_dec_and_lock(&sm->ref, &mdsc->snapid_map_lock)) {
11388c2ecf20Sopenharmony_ci		if (!RB_EMPTY_NODE(&sm->node)) {
11398c2ecf20Sopenharmony_ci			sm->last_used = jiffies;
11408c2ecf20Sopenharmony_ci			list_add_tail(&sm->lru, &mdsc->snapid_map_lru);
11418c2ecf20Sopenharmony_ci			spin_unlock(&mdsc->snapid_map_lock);
11428c2ecf20Sopenharmony_ci		} else {
11438c2ecf20Sopenharmony_ci			/* already cleaned up by
11448c2ecf20Sopenharmony_ci			 * ceph_cleanup_snapid_map() */
11458c2ecf20Sopenharmony_ci			spin_unlock(&mdsc->snapid_map_lock);
11468c2ecf20Sopenharmony_ci			kfree(sm);
11478c2ecf20Sopenharmony_ci		}
11488c2ecf20Sopenharmony_ci	}
11498c2ecf20Sopenharmony_ci}
11508c2ecf20Sopenharmony_ci
11518c2ecf20Sopenharmony_civoid ceph_trim_snapid_map(struct ceph_mds_client *mdsc)
11528c2ecf20Sopenharmony_ci{
11538c2ecf20Sopenharmony_ci	struct ceph_snapid_map *sm;
11548c2ecf20Sopenharmony_ci	unsigned long now;
11558c2ecf20Sopenharmony_ci	LIST_HEAD(to_free);
11568c2ecf20Sopenharmony_ci
11578c2ecf20Sopenharmony_ci	spin_lock(&mdsc->snapid_map_lock);
11588c2ecf20Sopenharmony_ci	now = jiffies;
11598c2ecf20Sopenharmony_ci
11608c2ecf20Sopenharmony_ci	while (!list_empty(&mdsc->snapid_map_lru)) {
11618c2ecf20Sopenharmony_ci		sm = list_first_entry(&mdsc->snapid_map_lru,
11628c2ecf20Sopenharmony_ci				      struct ceph_snapid_map, lru);
11638c2ecf20Sopenharmony_ci		if (time_after(sm->last_used + CEPH_SNAPID_MAP_TIMEOUT, now))
11648c2ecf20Sopenharmony_ci			break;
11658c2ecf20Sopenharmony_ci
11668c2ecf20Sopenharmony_ci		rb_erase(&sm->node, &mdsc->snapid_map_tree);
11678c2ecf20Sopenharmony_ci		list_move(&sm->lru, &to_free);
11688c2ecf20Sopenharmony_ci	}
11698c2ecf20Sopenharmony_ci	spin_unlock(&mdsc->snapid_map_lock);
11708c2ecf20Sopenharmony_ci
11718c2ecf20Sopenharmony_ci	while (!list_empty(&to_free)) {
11728c2ecf20Sopenharmony_ci		sm = list_first_entry(&to_free, struct ceph_snapid_map, lru);
11738c2ecf20Sopenharmony_ci		list_del(&sm->lru);
11748c2ecf20Sopenharmony_ci		dout("trim snapid map %llx -> %x\n", sm->snap, sm->dev);
11758c2ecf20Sopenharmony_ci		free_anon_bdev(sm->dev);
11768c2ecf20Sopenharmony_ci		kfree(sm);
11778c2ecf20Sopenharmony_ci	}
11788c2ecf20Sopenharmony_ci}
11798c2ecf20Sopenharmony_ci
11808c2ecf20Sopenharmony_civoid ceph_cleanup_snapid_map(struct ceph_mds_client *mdsc)
11818c2ecf20Sopenharmony_ci{
11828c2ecf20Sopenharmony_ci	struct ceph_snapid_map *sm;
11838c2ecf20Sopenharmony_ci	struct rb_node *p;
11848c2ecf20Sopenharmony_ci	LIST_HEAD(to_free);
11858c2ecf20Sopenharmony_ci
11868c2ecf20Sopenharmony_ci	spin_lock(&mdsc->snapid_map_lock);
11878c2ecf20Sopenharmony_ci	while ((p = rb_first(&mdsc->snapid_map_tree))) {
11888c2ecf20Sopenharmony_ci		sm = rb_entry(p, struct ceph_snapid_map, node);
11898c2ecf20Sopenharmony_ci		rb_erase(p, &mdsc->snapid_map_tree);
11908c2ecf20Sopenharmony_ci		RB_CLEAR_NODE(p);
11918c2ecf20Sopenharmony_ci		list_move(&sm->lru, &to_free);
11928c2ecf20Sopenharmony_ci	}
11938c2ecf20Sopenharmony_ci	spin_unlock(&mdsc->snapid_map_lock);
11948c2ecf20Sopenharmony_ci
11958c2ecf20Sopenharmony_ci	while (!list_empty(&to_free)) {
11968c2ecf20Sopenharmony_ci		sm = list_first_entry(&to_free, struct ceph_snapid_map, lru);
11978c2ecf20Sopenharmony_ci		list_del(&sm->lru);
11988c2ecf20Sopenharmony_ci		free_anon_bdev(sm->dev);
11998c2ecf20Sopenharmony_ci		if (WARN_ON_ONCE(atomic_read(&sm->ref))) {
12008c2ecf20Sopenharmony_ci			pr_err("snapid map %llx -> %x still in use\n",
12018c2ecf20Sopenharmony_ci			       sm->snap, sm->dev);
12028c2ecf20Sopenharmony_ci		}
12038c2ecf20Sopenharmony_ci		kfree(sm);
12048c2ecf20Sopenharmony_ci	}
12058c2ecf20Sopenharmony_ci}
1206