xref: /openbmc/linux/fs/ceph/snap.c (revision c845428b7a9157523103100806bc8130d64769c8)
1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
23d14c5d2SYehuda Sadeh #include <linux/ceph/ceph_debug.h>
3963b61ebSSage Weil 
4a68e564aSXiubo Li #include <linux/fs.h>
5963b61ebSSage Weil #include <linux/sort.h>
65a0e3ad6STejun Heo #include <linux/slab.h>
7176c77c9SJeff Layton #include <linux/iversion.h>
8963b61ebSSage Weil #include "super.h"
93d14c5d2SYehuda Sadeh #include "mds_client.h"
103d14c5d2SYehuda Sadeh #include <linux/ceph/decode.h>
11963b61ebSSage Weil 
1275c9627eSYan, Zheng /* unused map expires after 5 minutes */
1375c9627eSYan, Zheng #define CEPH_SNAPID_MAP_TIMEOUT	(5 * 60 * HZ)
1475c9627eSYan, Zheng 
15963b61ebSSage Weil /*
16963b61ebSSage Weil  * Snapshots in ceph are driven in large part by cooperation from the
17963b61ebSSage Weil  * client.  In contrast to local file systems or file servers that
18963b61ebSSage Weil  * implement snapshots at a single point in the system, ceph's
19963b61ebSSage Weil  * distributed access to storage requires clients to help decide
20963b61ebSSage Weil  * whether a write logically occurs before or after a recently created
21963b61ebSSage Weil  * snapshot.
22963b61ebSSage Weil  *
23963b61ebSSage Weil  * This provides a perfect instantanous client-wide snapshot.  Between
24963b61ebSSage Weil  * clients, however, snapshots may appear to be applied at slightly
25963b61ebSSage Weil  * different points in time, depending on delays in delivering the
26963b61ebSSage Weil  * snapshot notification.
27963b61ebSSage Weil  *
28963b61ebSSage Weil  * Snapshots are _not_ file system-wide.  Instead, each snapshot
29963b61ebSSage Weil  * applies to the subdirectory nested beneath some directory.  This
30963b61ebSSage Weil  * effectively divides the hierarchy into multiple "realms," where all
31963b61ebSSage Weil  * of the files contained by each realm share the same set of
32963b61ebSSage Weil  * snapshots.  An individual realm's snap set contains snapshots
33963b61ebSSage Weil  * explicitly created on that realm, as well as any snaps in its
34963b61ebSSage Weil  * parent's snap set _after_ the point at which the parent became it's
35963b61ebSSage Weil  * parent (due to, say, a rename).  Similarly, snaps from prior parents
36963b61ebSSage Weil  * during the time intervals during which they were the parent are included.
37963b61ebSSage Weil  *
38963b61ebSSage Weil  * The client is spared most of this detail, fortunately... it must only
39963b61ebSSage Weil  * maintains a hierarchy of realms reflecting the current parent/child
40963b61ebSSage Weil  * realm relationship, and for each realm has an explicit list of snaps
41963b61ebSSage Weil  * inherited from prior parents.
42963b61ebSSage Weil  *
43963b61ebSSage Weil  * A snap_realm struct is maintained for realms containing every inode
44963b61ebSSage Weil  * with an open cap in the system.  (The needed snap realm information is
45963b61ebSSage Weil  * provided by the MDS whenever a cap is issued, i.e., on open.)  A 'seq'
46963b61ebSSage Weil  * version number is used to ensure that as realm parameters change (new
47963b61ebSSage Weil  * snapshot, new parent, etc.) the client's realm hierarchy is updated.
48963b61ebSSage Weil  *
49963b61ebSSage Weil  * The realm hierarchy drives the generation of a 'snap context' for each
50963b61ebSSage Weil  * realm, which simply lists the resulting set of snaps for the realm.  This
51963b61ebSSage Weil  * is attached to any writes sent to OSDs.
52963b61ebSSage Weil  */
53963b61ebSSage Weil /*
54963b61ebSSage Weil  * Unfortunately error handling is a bit mixed here.  If we get a snap
55963b61ebSSage Weil  * update, but don't have enough memory to update our realm hierarchy,
56963b61ebSSage Weil  * it's not clear what we can do about it (besides complaining to the
57963b61ebSSage Weil  * console).
58963b61ebSSage Weil  */
59963b61ebSSage Weil 
60963b61ebSSage Weil 
61963b61ebSSage Weil /*
62963b61ebSSage Weil  * increase ref count for the realm
63963b61ebSSage Weil  *
64df2c0cb7SJeff Layton  * caller must hold snap_rwsem.
65963b61ebSSage Weil  */
ceph_get_snap_realm(struct ceph_mds_client * mdsc,struct ceph_snap_realm * realm)66963b61ebSSage Weil void ceph_get_snap_realm(struct ceph_mds_client *mdsc,
67963b61ebSSage Weil 			 struct ceph_snap_realm *realm)
68963b61ebSSage Weil {
69df2c0cb7SJeff Layton 	lockdep_assert_held(&mdsc->snap_rwsem);
70a6862e67SJeff Layton 
71963b61ebSSage Weil 	/*
728434ffe7SJeff Layton 	 * The 0->1 and 1->0 transitions must take the snap_empty_lock
738434ffe7SJeff Layton 	 * atomically with the refcount change. Go ahead and bump the
748434ffe7SJeff Layton 	 * nref here, unless it's 0, in which case we take the spinlock
758434ffe7SJeff Layton 	 * and then do the increment and remove it from the list.
76963b61ebSSage Weil 	 */
778434ffe7SJeff Layton 	if (atomic_inc_not_zero(&realm->nref))
788434ffe7SJeff Layton 		return;
798434ffe7SJeff Layton 
80963b61ebSSage Weil 	spin_lock(&mdsc->snap_empty_lock);
818434ffe7SJeff Layton 	if (atomic_inc_return(&realm->nref) == 1)
82963b61ebSSage Weil 		list_del_init(&realm->empty_item);
83963b61ebSSage Weil 	spin_unlock(&mdsc->snap_empty_lock);
84963b61ebSSage Weil }
85963b61ebSSage Weil 
__insert_snap_realm(struct rb_root * root,struct ceph_snap_realm * new)86a105f00cSSage Weil static void __insert_snap_realm(struct rb_root *root,
87a105f00cSSage Weil 				struct ceph_snap_realm *new)
88a105f00cSSage Weil {
89a105f00cSSage Weil 	struct rb_node **p = &root->rb_node;
90a105f00cSSage Weil 	struct rb_node *parent = NULL;
91a105f00cSSage Weil 	struct ceph_snap_realm *r = NULL;
92a105f00cSSage Weil 
93a105f00cSSage Weil 	while (*p) {
94a105f00cSSage Weil 		parent = *p;
95a105f00cSSage Weil 		r = rb_entry(parent, struct ceph_snap_realm, node);
96a105f00cSSage Weil 		if (new->ino < r->ino)
97a105f00cSSage Weil 			p = &(*p)->rb_left;
98a105f00cSSage Weil 		else if (new->ino > r->ino)
99a105f00cSSage Weil 			p = &(*p)->rb_right;
100a105f00cSSage Weil 		else
101a105f00cSSage Weil 			BUG();
102a105f00cSSage Weil 	}
103a105f00cSSage Weil 
104a105f00cSSage Weil 	rb_link_node(&new->node, parent, p);
105a105f00cSSage Weil 	rb_insert_color(&new->node, root);
106a105f00cSSage Weil }
107a105f00cSSage Weil 
108963b61ebSSage Weil /*
109963b61ebSSage Weil  * create and get the realm rooted at @ino and bump its ref count.
110963b61ebSSage Weil  *
111963b61ebSSage Weil  * caller must hold snap_rwsem for write.
112963b61ebSSage Weil  */
ceph_create_snap_realm(struct ceph_mds_client * mdsc,u64 ino)113963b61ebSSage Weil static struct ceph_snap_realm *ceph_create_snap_realm(
114963b61ebSSage Weil 	struct ceph_mds_client *mdsc,
115963b61ebSSage Weil 	u64 ino)
116963b61ebSSage Weil {
117963b61ebSSage Weil 	struct ceph_snap_realm *realm;
118963b61ebSSage Weil 
119a6862e67SJeff Layton 	lockdep_assert_held_write(&mdsc->snap_rwsem);
120a6862e67SJeff Layton 
121963b61ebSSage Weil 	realm = kzalloc(sizeof(*realm), GFP_NOFS);
122963b61ebSSage Weil 	if (!realm)
123963b61ebSSage Weil 		return ERR_PTR(-ENOMEM);
124963b61ebSSage Weil 
1255ed91587SXiubo Li 	/* Do not release the global dummy snaprealm until unmouting */
1265ed91587SXiubo Li 	if (ino == CEPH_INO_GLOBAL_SNAPREALM)
1275ed91587SXiubo Li 		atomic_set(&realm->nref, 2);
1285ed91587SXiubo Li 	else
1295ed91587SXiubo Li 		atomic_set(&realm->nref, 1);
130963b61ebSSage Weil 	realm->ino = ino;
131963b61ebSSage Weil 	INIT_LIST_HEAD(&realm->children);
132963b61ebSSage Weil 	INIT_LIST_HEAD(&realm->child_item);
133963b61ebSSage Weil 	INIT_LIST_HEAD(&realm->empty_item);
134ae00d4f3SSage Weil 	INIT_LIST_HEAD(&realm->dirty_item);
13574a31df4SXiubo Li 	INIT_LIST_HEAD(&realm->rebuild_item);
136963b61ebSSage Weil 	INIT_LIST_HEAD(&realm->inodes_with_caps);
137963b61ebSSage Weil 	spin_lock_init(&realm->inodes_with_caps_lock);
138a105f00cSSage Weil 	__insert_snap_realm(&mdsc->snap_realms, realm);
13981c5a148SYan, Zheng 	mdsc->num_snap_realms++;
14081c5a148SYan, Zheng 
141ad5255c1SXiubo Li 	dout("%s %llx %p\n", __func__, realm->ino, realm);
142963b61ebSSage Weil 	return realm;
143963b61ebSSage Weil }
144963b61ebSSage Weil 
145963b61ebSSage Weil /*
146a105f00cSSage Weil  * lookup the realm rooted at @ino.
147963b61ebSSage Weil  *
148df2c0cb7SJeff Layton  * caller must hold snap_rwsem.
149963b61ebSSage Weil  */
__lookup_snap_realm(struct ceph_mds_client * mdsc,u64 ino)150982d6011SYan, Zheng static struct ceph_snap_realm *__lookup_snap_realm(struct ceph_mds_client *mdsc,
151963b61ebSSage Weil 						   u64 ino)
152963b61ebSSage Weil {
153a105f00cSSage Weil 	struct rb_node *n = mdsc->snap_realms.rb_node;
154a105f00cSSage Weil 	struct ceph_snap_realm *r;
155963b61ebSSage Weil 
156df2c0cb7SJeff Layton 	lockdep_assert_held(&mdsc->snap_rwsem);
157a6862e67SJeff Layton 
158a105f00cSSage Weil 	while (n) {
159a105f00cSSage Weil 		r = rb_entry(n, struct ceph_snap_realm, node);
160a105f00cSSage Weil 		if (ino < r->ino)
161a105f00cSSage Weil 			n = n->rb_left;
162a105f00cSSage Weil 		else if (ino > r->ino)
163a105f00cSSage Weil 			n = n->rb_right;
164a105f00cSSage Weil 		else {
165ad5255c1SXiubo Li 			dout("%s %llx %p\n", __func__, r->ino, r);
166a105f00cSSage Weil 			return r;
167a105f00cSSage Weil 		}
168a105f00cSSage Weil 	}
169a105f00cSSage Weil 	return NULL;
170963b61ebSSage Weil }
171963b61ebSSage Weil 
ceph_lookup_snap_realm(struct ceph_mds_client * mdsc,u64 ino)172982d6011SYan, Zheng struct ceph_snap_realm *ceph_lookup_snap_realm(struct ceph_mds_client *mdsc,
173982d6011SYan, Zheng 					       u64 ino)
174982d6011SYan, Zheng {
175982d6011SYan, Zheng 	struct ceph_snap_realm *r;
176982d6011SYan, Zheng 	r = __lookup_snap_realm(mdsc, ino);
177982d6011SYan, Zheng 	if (r)
178982d6011SYan, Zheng 		ceph_get_snap_realm(mdsc, r);
179982d6011SYan, Zheng 	return r;
180982d6011SYan, Zheng }
181982d6011SYan, Zheng 
182963b61ebSSage Weil static void __put_snap_realm(struct ceph_mds_client *mdsc,
183963b61ebSSage Weil 			     struct ceph_snap_realm *realm);
184963b61ebSSage Weil 
185963b61ebSSage Weil /*
186963b61ebSSage Weil  * called with snap_rwsem (write)
187963b61ebSSage Weil  */
__destroy_snap_realm(struct ceph_mds_client * mdsc,struct ceph_snap_realm * realm)188963b61ebSSage Weil static void __destroy_snap_realm(struct ceph_mds_client *mdsc,
189963b61ebSSage Weil 				 struct ceph_snap_realm *realm)
190963b61ebSSage Weil {
191a6862e67SJeff Layton 	lockdep_assert_held_write(&mdsc->snap_rwsem);
192a6862e67SJeff Layton 
193ad5255c1SXiubo Li 	dout("%s %p %llx\n", __func__, realm, realm->ino);
194963b61ebSSage Weil 
195a105f00cSSage Weil 	rb_erase(&realm->node, &mdsc->snap_realms);
19681c5a148SYan, Zheng 	mdsc->num_snap_realms--;
197963b61ebSSage Weil 
198963b61ebSSage Weil 	if (realm->parent) {
199963b61ebSSage Weil 		list_del_init(&realm->child_item);
200963b61ebSSage Weil 		__put_snap_realm(mdsc, realm->parent);
201963b61ebSSage Weil 	}
202963b61ebSSage Weil 
203963b61ebSSage Weil 	kfree(realm->prior_parent_snaps);
204963b61ebSSage Weil 	kfree(realm->snaps);
205963b61ebSSage Weil 	ceph_put_snap_context(realm->cached_context);
206963b61ebSSage Weil 	kfree(realm);
207963b61ebSSage Weil }
208963b61ebSSage Weil 
209963b61ebSSage Weil /*
210963b61ebSSage Weil  * caller holds snap_rwsem (write)
211963b61ebSSage Weil  */
__put_snap_realm(struct ceph_mds_client * mdsc,struct ceph_snap_realm * realm)212963b61ebSSage Weil static void __put_snap_realm(struct ceph_mds_client *mdsc,
213963b61ebSSage Weil 			     struct ceph_snap_realm *realm)
214963b61ebSSage Weil {
215a6862e67SJeff Layton 	lockdep_assert_held_write(&mdsc->snap_rwsem);
216a6862e67SJeff Layton 
2178434ffe7SJeff Layton 	/*
2188434ffe7SJeff Layton 	 * We do not require the snap_empty_lock here, as any caller that
2198434ffe7SJeff Layton 	 * increments the value must hold the snap_rwsem.
2208434ffe7SJeff Layton 	 */
221963b61ebSSage Weil 	if (atomic_dec_and_test(&realm->nref))
222963b61ebSSage Weil 		__destroy_snap_realm(mdsc, realm);
223963b61ebSSage Weil }
224963b61ebSSage Weil 
225963b61ebSSage Weil /*
2268434ffe7SJeff Layton  * See comments in ceph_get_snap_realm. Caller needn't hold any locks.
227963b61ebSSage Weil  */
ceph_put_snap_realm(struct ceph_mds_client * mdsc,struct ceph_snap_realm * realm)228963b61ebSSage Weil void ceph_put_snap_realm(struct ceph_mds_client *mdsc,
229963b61ebSSage Weil 			 struct ceph_snap_realm *realm)
230963b61ebSSage Weil {
2318434ffe7SJeff Layton 	if (!atomic_dec_and_lock(&realm->nref, &mdsc->snap_empty_lock))
232963b61ebSSage Weil 		return;
233963b61ebSSage Weil 
234963b61ebSSage Weil 	if (down_write_trylock(&mdsc->snap_rwsem)) {
2358434ffe7SJeff Layton 		spin_unlock(&mdsc->snap_empty_lock);
236963b61ebSSage Weil 		__destroy_snap_realm(mdsc, realm);
237963b61ebSSage Weil 		up_write(&mdsc->snap_rwsem);
238963b61ebSSage Weil 	} else {
239a26a185dSHenry C Chang 		list_add(&realm->empty_item, &mdsc->snap_empty);
240963b61ebSSage Weil 		spin_unlock(&mdsc->snap_empty_lock);
241963b61ebSSage Weil 	}
242963b61ebSSage Weil }
243963b61ebSSage Weil 
244963b61ebSSage Weil /*
245963b61ebSSage Weil  * Clean up any realms whose ref counts have dropped to zero.  Note
246963b61ebSSage Weil  * that this does not include realms who were created but not yet
247963b61ebSSage Weil  * used.
248963b61ebSSage Weil  *
249963b61ebSSage Weil  * Called under snap_rwsem (write)
250963b61ebSSage Weil  */
__cleanup_empty_realms(struct ceph_mds_client * mdsc)251963b61ebSSage Weil static void __cleanup_empty_realms(struct ceph_mds_client *mdsc)
252963b61ebSSage Weil {
253963b61ebSSage Weil 	struct ceph_snap_realm *realm;
254963b61ebSSage Weil 
255a6862e67SJeff Layton 	lockdep_assert_held_write(&mdsc->snap_rwsem);
256a6862e67SJeff Layton 
257963b61ebSSage Weil 	spin_lock(&mdsc->snap_empty_lock);
258963b61ebSSage Weil 	while (!list_empty(&mdsc->snap_empty)) {
259963b61ebSSage Weil 		realm = list_first_entry(&mdsc->snap_empty,
260963b61ebSSage Weil 				   struct ceph_snap_realm, empty_item);
261963b61ebSSage Weil 		list_del(&realm->empty_item);
262963b61ebSSage Weil 		spin_unlock(&mdsc->snap_empty_lock);
263963b61ebSSage Weil 		__destroy_snap_realm(mdsc, realm);
264963b61ebSSage Weil 		spin_lock(&mdsc->snap_empty_lock);
265963b61ebSSage Weil 	}
266963b61ebSSage Weil 	spin_unlock(&mdsc->snap_empty_lock);
267963b61ebSSage Weil }
268963b61ebSSage Weil 
ceph_cleanup_global_and_empty_realms(struct ceph_mds_client * mdsc)2695ed91587SXiubo Li void ceph_cleanup_global_and_empty_realms(struct ceph_mds_client *mdsc)
270963b61ebSSage Weil {
2715ed91587SXiubo Li 	struct ceph_snap_realm *global_realm;
2725ed91587SXiubo Li 
273963b61ebSSage Weil 	down_write(&mdsc->snap_rwsem);
2745ed91587SXiubo Li 	global_realm = __lookup_snap_realm(mdsc, CEPH_INO_GLOBAL_SNAPREALM);
2755ed91587SXiubo Li 	if (global_realm)
2765ed91587SXiubo Li 		ceph_put_snap_realm(mdsc, global_realm);
277963b61ebSSage Weil 	__cleanup_empty_realms(mdsc);
278963b61ebSSage Weil 	up_write(&mdsc->snap_rwsem);
279963b61ebSSage Weil }
280963b61ebSSage Weil 
281963b61ebSSage Weil /*
282963b61ebSSage Weil  * adjust the parent realm of a given @realm.  adjust child list, and parent
283963b61ebSSage Weil  * pointers, and ref counts appropriately.
284963b61ebSSage Weil  *
285963b61ebSSage Weil  * return true if parent was changed, 0 if unchanged, <0 on error.
286963b61ebSSage Weil  *
287963b61ebSSage Weil  * caller must hold snap_rwsem for write.
288963b61ebSSage Weil  */
adjust_snap_realm_parent(struct ceph_mds_client * mdsc,struct ceph_snap_realm * realm,u64 parentino)289963b61ebSSage Weil static int adjust_snap_realm_parent(struct ceph_mds_client *mdsc,
290963b61ebSSage Weil 				    struct ceph_snap_realm *realm,
291963b61ebSSage Weil 				    u64 parentino)
292963b61ebSSage Weil {
293963b61ebSSage Weil 	struct ceph_snap_realm *parent;
294963b61ebSSage Weil 
295a6862e67SJeff Layton 	lockdep_assert_held_write(&mdsc->snap_rwsem);
296a6862e67SJeff Layton 
297963b61ebSSage Weil 	if (realm->parent_ino == parentino)
298963b61ebSSage Weil 		return 0;
299963b61ebSSage Weil 
300963b61ebSSage Weil 	parent = ceph_lookup_snap_realm(mdsc, parentino);
301963b61ebSSage Weil 	if (!parent) {
302963b61ebSSage Weil 		parent = ceph_create_snap_realm(mdsc, parentino);
303963b61ebSSage Weil 		if (IS_ERR(parent))
304963b61ebSSage Weil 			return PTR_ERR(parent);
305963b61ebSSage Weil 	}
306ad5255c1SXiubo Li 	dout("%s %llx %p: %llx %p -> %llx %p\n", __func__, realm->ino,
307ad5255c1SXiubo Li 	     realm, realm->parent_ino, realm->parent, parentino, parent);
308963b61ebSSage Weil 	if (realm->parent) {
309963b61ebSSage Weil 		list_del_init(&realm->child_item);
310963b61ebSSage Weil 		ceph_put_snap_realm(mdsc, realm->parent);
311963b61ebSSage Weil 	}
312963b61ebSSage Weil 	realm->parent_ino = parentino;
313963b61ebSSage Weil 	realm->parent = parent;
314963b61ebSSage Weil 	list_add(&realm->child_item, &parent->children);
315963b61ebSSage Weil 	return 1;
316963b61ebSSage Weil }
317963b61ebSSage Weil 
318963b61ebSSage Weil 
cmpu64_rev(const void * a,const void * b)319963b61ebSSage Weil static int cmpu64_rev(const void *a, const void *b)
320963b61ebSSage Weil {
321963b61ebSSage Weil 	if (*(u64 *)a < *(u64 *)b)
322963b61ebSSage Weil 		return 1;
323963b61ebSSage Weil 	if (*(u64 *)a > *(u64 *)b)
324963b61ebSSage Weil 		return -1;
325963b61ebSSage Weil 	return 0;
326963b61ebSSage Weil }
327963b61ebSSage Weil 
32897c85a82SYan, Zheng 
329963b61ebSSage Weil /*
330963b61ebSSage Weil  * build the snap context for a given realm.
331963b61ebSSage Weil  */
build_snap_context(struct ceph_mds_client * mdsc,struct ceph_snap_realm * realm,struct list_head * realm_queue,struct list_head * dirty_realms)3322e2023e9SXiubo Li static int build_snap_context(struct ceph_mds_client *mdsc,
3332e2023e9SXiubo Li 			      struct ceph_snap_realm *realm,
33474a31df4SXiubo Li 			      struct list_head *realm_queue,
3353ae0bebcSYan, Zheng 			      struct list_head *dirty_realms)
336963b61ebSSage Weil {
337963b61ebSSage Weil 	struct ceph_snap_realm *parent = realm->parent;
338963b61ebSSage Weil 	struct ceph_snap_context *snapc;
339963b61ebSSage Weil 	int err = 0;
340aa711ee3SAlex Elder 	u32 num = realm->num_prior_parent_snaps + realm->num_snaps;
341963b61ebSSage Weil 
342963b61ebSSage Weil 	/*
343963b61ebSSage Weil 	 * build parent context, if it hasn't been built.
344963b61ebSSage Weil 	 * conservatively estimate that all parent snaps might be
345963b61ebSSage Weil 	 * included by us.
346963b61ebSSage Weil 	 */
347963b61ebSSage Weil 	if (parent) {
348963b61ebSSage Weil 		if (!parent->cached_context) {
34974a31df4SXiubo Li 			/* add to the queue head */
35074a31df4SXiubo Li 			list_add(&parent->rebuild_item, realm_queue);
35174a31df4SXiubo Li 			return 1;
352963b61ebSSage Weil 		}
353963b61ebSSage Weil 		num += parent->cached_context->num_snaps;
354963b61ebSSage Weil 	}
355963b61ebSSage Weil 
356963b61ebSSage Weil 	/* do i actually need to update?  not if my context seq
357963b61ebSSage Weil 	   matches realm seq, and my parents' does to.  (this works
358963b61ebSSage Weil 	   because we rebuild_snap_realms() works _downward_ in
359963b61ebSSage Weil 	   hierarchy after each update.) */
360963b61ebSSage Weil 	if (realm->cached_context &&
361ec4318bcSSage Weil 	    realm->cached_context->seq == realm->seq &&
362963b61ebSSage Weil 	    (!parent ||
363ec4318bcSSage Weil 	     realm->cached_context->seq >= parent->cached_context->seq)) {
364ad5255c1SXiubo Li 		dout("%s %llx %p: %p seq %lld (%u snaps) (unchanged)\n",
365ad5255c1SXiubo Li 		     __func__, realm->ino, realm, realm->cached_context,
366963b61ebSSage Weil 		     realm->cached_context->seq,
367aa711ee3SAlex Elder 		     (unsigned int)realm->cached_context->num_snaps);
368963b61ebSSage Weil 		return 0;
369963b61ebSSage Weil 	}
370963b61ebSSage Weil 
371963b61ebSSage Weil 	/* alloc new snap context */
372963b61ebSSage Weil 	err = -ENOMEM;
373a3860c1cSXi Wang 	if (num > (SIZE_MAX - sizeof(*snapc)) / sizeof(u64))
374963b61ebSSage Weil 		goto fail;
375812164f8SAlex Elder 	snapc = ceph_create_snap_context(num, GFP_NOFS);
376963b61ebSSage Weil 	if (!snapc)
377963b61ebSSage Weil 		goto fail;
378963b61ebSSage Weil 
379963b61ebSSage Weil 	/* build (reverse sorted) snap vector */
380963b61ebSSage Weil 	num = 0;
381963b61ebSSage Weil 	snapc->seq = realm->seq;
382963b61ebSSage Weil 	if (parent) {
383aa711ee3SAlex Elder 		u32 i;
384aa711ee3SAlex Elder 
38525985edcSLucas De Marchi 		/* include any of parent's snaps occurring _after_ my
386963b61ebSSage Weil 		   parent became my parent */
387963b61ebSSage Weil 		for (i = 0; i < parent->cached_context->num_snaps; i++)
388963b61ebSSage Weil 			if (parent->cached_context->snaps[i] >=
389963b61ebSSage Weil 			    realm->parent_since)
390963b61ebSSage Weil 				snapc->snaps[num++] =
391963b61ebSSage Weil 					parent->cached_context->snaps[i];
392963b61ebSSage Weil 		if (parent->cached_context->seq > snapc->seq)
393963b61ebSSage Weil 			snapc->seq = parent->cached_context->seq;
394963b61ebSSage Weil 	}
395963b61ebSSage Weil 	memcpy(snapc->snaps + num, realm->snaps,
396963b61ebSSage Weil 	       sizeof(u64)*realm->num_snaps);
397963b61ebSSage Weil 	num += realm->num_snaps;
398963b61ebSSage Weil 	memcpy(snapc->snaps + num, realm->prior_parent_snaps,
399963b61ebSSage Weil 	       sizeof(u64)*realm->num_prior_parent_snaps);
400963b61ebSSage Weil 	num += realm->num_prior_parent_snaps;
401963b61ebSSage Weil 
402963b61ebSSage Weil 	sort(snapc->snaps, num, sizeof(u64), cmpu64_rev, NULL);
403963b61ebSSage Weil 	snapc->num_snaps = num;
404ad5255c1SXiubo Li 	dout("%s %llx %p: %p seq %lld (%u snaps)\n", __func__, realm->ino,
405ad5255c1SXiubo Li 	     realm, snapc, snapc->seq, (unsigned int) snapc->num_snaps);
406963b61ebSSage Weil 
407963b61ebSSage Weil 	ceph_put_snap_context(realm->cached_context);
4089f4057fcSYan, Zheng 	realm->cached_context = snapc;
4093ae0bebcSYan, Zheng 	/* queue realm for cap_snap creation */
4103ae0bebcSYan, Zheng 	list_add_tail(&realm->dirty_item, dirty_realms);
411963b61ebSSage Weil 	return 0;
412963b61ebSSage Weil 
413963b61ebSSage Weil fail:
414963b61ebSSage Weil 	/*
415963b61ebSSage Weil 	 * if we fail, clear old (incorrect) cached_context... hopefully
416963b61ebSSage Weil 	 * we'll have better luck building it later
417963b61ebSSage Weil 	 */
418963b61ebSSage Weil 	if (realm->cached_context) {
419963b61ebSSage Weil 		ceph_put_snap_context(realm->cached_context);
420963b61ebSSage Weil 		realm->cached_context = NULL;
421963b61ebSSage Weil 	}
422ad5255c1SXiubo Li 	pr_err("%s %llx %p fail %d\n", __func__, realm->ino, realm, err);
423963b61ebSSage Weil 	return err;
424963b61ebSSage Weil }
425963b61ebSSage Weil 
426963b61ebSSage Weil /*
427963b61ebSSage Weil  * rebuild snap context for the given realm and all of its children.
428963b61ebSSage Weil  */
rebuild_snap_realms(struct ceph_mds_client * mdsc,struct ceph_snap_realm * realm,struct list_head * dirty_realms)4292e2023e9SXiubo Li static void rebuild_snap_realms(struct ceph_mds_client *mdsc,
4302e2023e9SXiubo Li 				struct ceph_snap_realm *realm,
4313ae0bebcSYan, Zheng 				struct list_head *dirty_realms)
432963b61ebSSage Weil {
43374a31df4SXiubo Li 	LIST_HEAD(realm_queue);
43474a31df4SXiubo Li 	int last = 0;
43574a31df4SXiubo Li 	bool skip = false;
436963b61ebSSage Weil 
43774a31df4SXiubo Li 	list_add_tail(&realm->rebuild_item, &realm_queue);
438963b61ebSSage Weil 
43974a31df4SXiubo Li 	while (!list_empty(&realm_queue)) {
44074a31df4SXiubo Li 		struct ceph_snap_realm *_realm, *child;
44174a31df4SXiubo Li 
44274a31df4SXiubo Li 		_realm = list_first_entry(&realm_queue,
44374a31df4SXiubo Li 					  struct ceph_snap_realm,
44474a31df4SXiubo Li 					  rebuild_item);
44574a31df4SXiubo Li 
44674a31df4SXiubo Li 		/*
44774a31df4SXiubo Li 		 * If the last building failed dues to memory
44874a31df4SXiubo Li 		 * issue, just empty the realm_queue and return
44974a31df4SXiubo Li 		 * to avoid infinite loop.
45074a31df4SXiubo Li 		 */
45174a31df4SXiubo Li 		if (last < 0) {
45274a31df4SXiubo Li 			list_del_init(&_realm->rebuild_item);
45374a31df4SXiubo Li 			continue;
45474a31df4SXiubo Li 		}
45574a31df4SXiubo Li 
4562e2023e9SXiubo Li 		last = build_snap_context(mdsc, _realm, &realm_queue,
4572e2023e9SXiubo Li 					  dirty_realms);
458ad5255c1SXiubo Li 		dout("%s %llx %p, %s\n", __func__, _realm->ino, _realm,
45974a31df4SXiubo Li 		     last > 0 ? "is deferred" : !last ? "succeeded" : "failed");
46074a31df4SXiubo Li 
46174a31df4SXiubo Li 		/* is any child in the list ? */
46274a31df4SXiubo Li 		list_for_each_entry(child, &_realm->children, child_item) {
46374a31df4SXiubo Li 			if (!list_empty(&child->rebuild_item)) {
46474a31df4SXiubo Li 				skip = true;
46574a31df4SXiubo Li 				break;
46674a31df4SXiubo Li 			}
46774a31df4SXiubo Li 		}
46874a31df4SXiubo Li 
46974a31df4SXiubo Li 		if (!skip) {
47074a31df4SXiubo Li 			list_for_each_entry(child, &_realm->children, child_item)
47174a31df4SXiubo Li 				list_add_tail(&child->rebuild_item, &realm_queue);
47274a31df4SXiubo Li 		}
47374a31df4SXiubo Li 
47474a31df4SXiubo Li 		/* last == 1 means need to build parent first */
47574a31df4SXiubo Li 		if (last <= 0)
47674a31df4SXiubo Li 			list_del_init(&_realm->rebuild_item);
47774a31df4SXiubo Li 	}
478963b61ebSSage Weil }
479963b61ebSSage Weil 
480963b61ebSSage Weil 
481963b61ebSSage Weil /*
482963b61ebSSage Weil  * helper to allocate and decode an array of snapids.  free prior
483963b61ebSSage Weil  * instance, if any.
484963b61ebSSage Weil  */
dup_array(u64 ** dst,__le64 * src,u32 num)485aa711ee3SAlex Elder static int dup_array(u64 **dst, __le64 *src, u32 num)
486963b61ebSSage Weil {
487aa711ee3SAlex Elder 	u32 i;
488963b61ebSSage Weil 
489963b61ebSSage Weil 	kfree(*dst);
490963b61ebSSage Weil 	if (num) {
491963b61ebSSage Weil 		*dst = kcalloc(num, sizeof(u64), GFP_NOFS);
492963b61ebSSage Weil 		if (!*dst)
493963b61ebSSage Weil 			return -ENOMEM;
494963b61ebSSage Weil 		for (i = 0; i < num; i++)
495963b61ebSSage Weil 			(*dst)[i] = get_unaligned_le64(src + i);
496963b61ebSSage Weil 	} else {
497963b61ebSSage Weil 		*dst = NULL;
498963b61ebSSage Weil 	}
499963b61ebSSage Weil 	return 0;
500963b61ebSSage Weil }
501963b61ebSSage Weil 
has_new_snaps(struct ceph_snap_context * o,struct ceph_snap_context * n)50286056090SYan, Zheng static bool has_new_snaps(struct ceph_snap_context *o,
50386056090SYan, Zheng 			  struct ceph_snap_context *n)
50486056090SYan, Zheng {
50586056090SYan, Zheng 	if (n->num_snaps == 0)
50686056090SYan, Zheng 		return false;
50786056090SYan, Zheng 	/* snaps are in descending order */
50886056090SYan, Zheng 	return n->snaps[0] > o->seq;
50986056090SYan, Zheng }
510963b61ebSSage Weil 
511963b61ebSSage Weil /*
512963b61ebSSage Weil  * When a snapshot is applied, the size/mtime inode metadata is queued
513963b61ebSSage Weil  * in a ceph_cap_snap (one for each snapshot) until writeback
514963b61ebSSage Weil  * completes and the metadata can be flushed back to the MDS.
515963b61ebSSage Weil  *
516963b61ebSSage Weil  * However, if a (sync) write is currently in-progress when we apply
517963b61ebSSage Weil  * the snapshot, we have to wait until the write succeeds or fails
518963b61ebSSage Weil  * (and a final size/mtime is known).  In this case the
519963b61ebSSage Weil  * cap_snap->writing = 1, and is said to be "pending."  When the write
520963b61ebSSage Weil  * finishes, we __ceph_finish_cap_snap().
521963b61ebSSage Weil  *
522963b61ebSSage Weil  * Caller must hold snap_rwsem for read (i.e., the realm topology won't
523963b61ebSSage Weil  * change).
524963b61ebSSage Weil  */
ceph_queue_cap_snap(struct ceph_inode_info * ci,struct ceph_cap_snap ** pcapsnap)5251ab36c9dSXiubo Li static void ceph_queue_cap_snap(struct ceph_inode_info *ci,
5261ab36c9dSXiubo Li 				struct ceph_cap_snap **pcapsnap)
527963b61ebSSage Weil {
528874c8ca1SDavid Howells 	struct inode *inode = &ci->netfs.inode;
52986056090SYan, Zheng 	struct ceph_snap_context *old_snapc, *new_snapc;
5301ab36c9dSXiubo Li 	struct ceph_cap_snap *capsnap = *pcapsnap;
53112fe3ddaSLuis Henriques 	struct ceph_buffer *old_blob = NULL;
5324a625be4SSage Weil 	int used, dirty;
533963b61ebSSage Weil 
534be655596SSage Weil 	spin_lock(&ci->i_ceph_lock);
535963b61ebSSage Weil 	used = __ceph_caps_used(ci);
5364a625be4SSage Weil 	dirty = __ceph_caps_dirty(ci);
537af0ed569SSage Weil 
5385dda377cSYan, Zheng 	old_snapc = ci->i_head_snapc;
53986056090SYan, Zheng 	new_snapc = ci->i_snap_realm->cached_context;
5405dda377cSYan, Zheng 
541af0ed569SSage Weil 	/*
542af0ed569SSage Weil 	 * If there is a write in progress, treat that as a dirty Fw,
543af0ed569SSage Weil 	 * even though it hasn't completed yet; by the time we finish
544af0ed569SSage Weil 	 * up this capsnap it will be.
545af0ed569SSage Weil 	 */
546af0ed569SSage Weil 	if (used & CEPH_CAP_FILE_WR)
547af0ed569SSage Weil 		dirty |= CEPH_CAP_FILE_WR;
548af0ed569SSage Weil 
549963b61ebSSage Weil 	if (__ceph_have_pending_cap_snap(ci)) {
550963b61ebSSage Weil 		/* there is no point in queuing multiple "pending" cap_snaps,
551963b61ebSSage Weil 		   as no new writes are allowed to start when pending, so any
552963b61ebSSage Weil 		   writes in progress now were started before the previous
553963b61ebSSage Weil 		   cap_snap.  lucky us. */
554ad5255c1SXiubo Li 		dout("%s %p %llx.%llx already pending\n",
555ad5255c1SXiubo Li 		     __func__, inode, ceph_vinop(inode));
5565dda377cSYan, Zheng 		goto update_snapc;
5575dda377cSYan, Zheng 	}
55886056090SYan, Zheng 	if (ci->i_wrbuffer_ref_head == 0 &&
55986056090SYan, Zheng 	    !(dirty & (CEPH_CAP_ANY_EXCL|CEPH_CAP_FILE_WR))) {
560ad5255c1SXiubo Li 		dout("%s %p %llx.%llx nothing dirty|writing\n",
561ad5255c1SXiubo Li 		     __func__, inode, ceph_vinop(inode));
5625dda377cSYan, Zheng 		goto update_snapc;
5635dda377cSYan, Zheng 	}
564fc837c8fSSage Weil 
5655dda377cSYan, Zheng 	BUG_ON(!old_snapc);
566af0ed569SSage Weil 
56786056090SYan, Zheng 	/*
56886056090SYan, Zheng 	 * There is no need to send FLUSHSNAP message to MDS if there is
56986056090SYan, Zheng 	 * no new snapshot. But when there is dirty pages or on-going
57086056090SYan, Zheng 	 * writes, we still need to create cap_snap. cap_snap is needed
57186056090SYan, Zheng 	 * by the write path and page writeback path.
57286056090SYan, Zheng 	 *
57386056090SYan, Zheng 	 * also see ceph_try_drop_cap_snap()
57486056090SYan, Zheng 	 */
57586056090SYan, Zheng 	if (has_new_snaps(old_snapc, new_snapc)) {
57686056090SYan, Zheng 		if (dirty & (CEPH_CAP_ANY_EXCL|CEPH_CAP_FILE_WR))
57786056090SYan, Zheng 			capsnap->need_flush = true;
57886056090SYan, Zheng 	} else {
57986056090SYan, Zheng 		if (!(used & CEPH_CAP_FILE_WR) &&
58086056090SYan, Zheng 		    ci->i_wrbuffer_ref_head == 0) {
581ad5255c1SXiubo Li 			dout("%s %p %llx.%llx no new_snap|dirty_page|writing\n",
582ad5255c1SXiubo Li 			     __func__, inode, ceph_vinop(inode));
58386056090SYan, Zheng 			goto update_snapc;
58486056090SYan, Zheng 		}
58586056090SYan, Zheng 	}
58686056090SYan, Zheng 
587ad5255c1SXiubo Li 	dout("%s %p %llx.%llx cap_snap %p queuing under %p %s %s\n",
588ad5255c1SXiubo Li 	     __func__, inode, ceph_vinop(inode), capsnap, old_snapc,
589ad5255c1SXiubo Li 	     ceph_cap_string(dirty), capsnap->need_flush ? "" : "no_flush");
5900444d76aSDave Chinner 	ihold(inode);
591963b61ebSSage Weil 
5925dda377cSYan, Zheng 	capsnap->follows = old_snapc->seq;
593963b61ebSSage Weil 	capsnap->issued = __ceph_caps_issued(ci, NULL);
5944a625be4SSage Weil 	capsnap->dirty = dirty;
595963b61ebSSage Weil 
596963b61ebSSage Weil 	capsnap->mode = inode->i_mode;
597963b61ebSSage Weil 	capsnap->uid = inode->i_uid;
598963b61ebSSage Weil 	capsnap->gid = inode->i_gid;
599963b61ebSSage Weil 
6004a625be4SSage Weil 	if (dirty & CEPH_CAP_XATTR_EXCL) {
60112fe3ddaSLuis Henriques 		old_blob = __ceph_build_xattrs_blob(ci);
6024a625be4SSage Weil 		capsnap->xattr_blob =
6034a625be4SSage Weil 			ceph_buffer_get(ci->i_xattrs.blob);
6044a625be4SSage Weil 		capsnap->xattr_version = ci->i_xattrs.version;
6054a625be4SSage Weil 	} else {
606963b61ebSSage Weil 		capsnap->xattr_blob = NULL;
6074a625be4SSage Weil 		capsnap->xattr_version = 0;
6084a625be4SSage Weil 	}
609963b61ebSSage Weil 
610e20d258dSYan, Zheng 	capsnap->inline_data = ci->i_inline_version != CEPH_INLINE_NONE;
611e20d258dSYan, Zheng 
612963b61ebSSage Weil 	/* dirty page count moved from _head to this cap_snap;
613963b61ebSSage Weil 	   all subsequent writes page dirties occur _after_ this
614963b61ebSSage Weil 	   snapshot. */
615963b61ebSSage Weil 	capsnap->dirty_pages = ci->i_wrbuffer_ref_head;
616963b61ebSSage Weil 	ci->i_wrbuffer_ref_head = 0;
6175dda377cSYan, Zheng 	capsnap->context = old_snapc;
618963b61ebSSage Weil 	list_add_tail(&capsnap->ci_item, &ci->i_cap_snaps);
619963b61ebSSage Weil 
620963b61ebSSage Weil 	if (used & CEPH_CAP_FILE_WR) {
621ad5255c1SXiubo Li 		dout("%s %p %llx.%llx cap_snap %p snapc %p seq %llu used WR,"
622ad5255c1SXiubo Li 		     " now pending\n", __func__, inode, ceph_vinop(inode),
6235dda377cSYan, Zheng 		     capsnap, old_snapc, old_snapc->seq);
624963b61ebSSage Weil 		capsnap->writing = 1;
625963b61ebSSage Weil 	} else {
626963b61ebSSage Weil 		/* note mtime, size NOW. */
627963b61ebSSage Weil 		__ceph_finish_cap_snap(ci, capsnap);
628963b61ebSSage Weil 	}
6291ab36c9dSXiubo Li 	*pcapsnap = NULL;
630fce85157SYan, Zheng 	old_snapc = NULL;
631963b61ebSSage Weil 
6325dda377cSYan, Zheng update_snapc:
63337659182SYan, Zheng 	if (ci->i_wrbuffer_ref_head == 0 &&
63437659182SYan, Zheng 	    ci->i_wr_ref == 0 &&
63537659182SYan, Zheng 	    ci->i_dirty_caps == 0 &&
63637659182SYan, Zheng 	    ci->i_flushing_caps == 0) {
63737659182SYan, Zheng 		ci->i_head_snapc = NULL;
63837659182SYan, Zheng 	} else {
63986056090SYan, Zheng 		ci->i_head_snapc = ceph_get_snap_context(new_snapc);
64086056090SYan, Zheng 		dout(" new snapc is %p\n", new_snapc);
6415dda377cSYan, Zheng 	}
642be655596SSage Weil 	spin_unlock(&ci->i_ceph_lock);
6435dda377cSYan, Zheng 
64412fe3ddaSLuis Henriques 	ceph_buffer_put(old_blob);
6455dda377cSYan, Zheng 	ceph_put_snap_context(old_snapc);
646963b61ebSSage Weil }
647963b61ebSSage Weil 
648963b61ebSSage Weil /*
649963b61ebSSage Weil  * Finalize the size, mtime for a cap_snap.. that is, settle on final values
650963b61ebSSage Weil  * to be used for the snapshot, to be flushed back to the mds.
651963b61ebSSage Weil  *
652963b61ebSSage Weil  * If capsnap can now be flushed, add to snap_flush list, and return 1.
653963b61ebSSage Weil  *
654be655596SSage Weil  * Caller must hold i_ceph_lock.
655963b61ebSSage Weil  */
__ceph_finish_cap_snap(struct ceph_inode_info * ci,struct ceph_cap_snap * capsnap)656963b61ebSSage Weil int __ceph_finish_cap_snap(struct ceph_inode_info *ci,
657963b61ebSSage Weil 			    struct ceph_cap_snap *capsnap)
658963b61ebSSage Weil {
659874c8ca1SDavid Howells 	struct inode *inode = &ci->netfs.inode;
6602678da88SXiubo Li 	struct ceph_mds_client *mdsc = ceph_sb_to_mdsc(inode->i_sb);
661963b61ebSSage Weil 
662963b61ebSSage Weil 	BUG_ON(capsnap->writing);
6632d6795fbSJeff Layton 	capsnap->size = i_size_read(inode);
6649bbeab41SArnd Bergmann 	capsnap->mtime = inode->i_mtime;
6659bbeab41SArnd Bergmann 	capsnap->atime = inode->i_atime;
6667795aef0SJeff Layton 	capsnap->ctime = inode_get_ctime(inode);
667ec62b894SJeff Layton 	capsnap->btime = ci->i_btime;
668176c77c9SJeff Layton 	capsnap->change_attr = inode_peek_iversion_raw(inode);
669963b61ebSSage Weil 	capsnap->time_warp_seq = ci->i_time_warp_seq;
6705f743e45SYan, Zheng 	capsnap->truncate_size = ci->i_truncate_size;
6715f743e45SYan, Zheng 	capsnap->truncate_seq = ci->i_truncate_seq;
672963b61ebSSage Weil 	if (capsnap->dirty_pages) {
673ad5255c1SXiubo Li 		dout("%s %p %llx.%llx cap_snap %p snapc %p %llu %s s=%llu "
674ad5255c1SXiubo Li 		     "still has %d dirty pages\n", __func__, inode,
675ad5255c1SXiubo Li 		     ceph_vinop(inode), capsnap, capsnap->context,
676ad5255c1SXiubo Li 		     capsnap->context->seq, ceph_cap_string(capsnap->dirty),
677ad5255c1SXiubo Li 		     capsnap->size, capsnap->dirty_pages);
678963b61ebSSage Weil 		return 0;
679963b61ebSSage Weil 	}
68070220ac8SYan, Zheng 
6812d12ad95SXiubo Li 	/*
6822d12ad95SXiubo Li 	 * Defer flushing the capsnap if the dirty buffer not flushed yet.
6832d12ad95SXiubo Li 	 * And trigger to flush the buffer immediately.
6842d12ad95SXiubo Li 	 */
6852d12ad95SXiubo Li 	if (ci->i_wrbuffer_ref) {
686ad5255c1SXiubo Li 		dout("%s %p %llx.%llx cap_snap %p snapc %p %llu %s s=%llu "
687ad5255c1SXiubo Li 		     "used WRBUFFER, delaying\n", __func__, inode,
688ad5255c1SXiubo Li 		     ceph_vinop(inode), capsnap, capsnap->context,
689ad5255c1SXiubo Li 		     capsnap->context->seq, ceph_cap_string(capsnap->dirty),
690ad5255c1SXiubo Li 		     capsnap->size);
6912d12ad95SXiubo Li 		ceph_queue_writeback(inode);
692558b4510SXiubo Li 		return 0;
693558b4510SXiubo Li 	}
694558b4510SXiubo Li 
69570220ac8SYan, Zheng 	ci->i_ceph_flags |= CEPH_I_FLUSH_SNAPS;
696ad5255c1SXiubo Li 	dout("%s %p %llx.%llx cap_snap %p snapc %p %llu %s s=%llu\n",
697ad5255c1SXiubo Li 	     __func__, inode, ceph_vinop(inode), capsnap, capsnap->context,
698819ccbfaSSage Weil 	     capsnap->context->seq, ceph_cap_string(capsnap->dirty),
699819ccbfaSSage Weil 	     capsnap->size);
700963b61ebSSage Weil 
701963b61ebSSage Weil 	spin_lock(&mdsc->snap_flush_lock);
702409e873eSXiubo Li 	if (list_empty(&ci->i_snap_flush_item)) {
703409e873eSXiubo Li 		ihold(inode);
704963b61ebSSage Weil 		list_add_tail(&ci->i_snap_flush_item, &mdsc->snap_flush_list);
705409e873eSXiubo Li 	}
706963b61ebSSage Weil 	spin_unlock(&mdsc->snap_flush_lock);
707963b61ebSSage Weil 	return 1;  /* caller may want to ceph_flush_snaps */
708963b61ebSSage Weil }
709963b61ebSSage Weil 
710ed326044SSage Weil /*
711ed326044SSage Weil  * Queue cap_snaps for snap writeback for this realm and its children.
712ed326044SSage Weil  * Called under snap_rwsem, so realm topology won't change.
713ed326044SSage Weil  */
queue_realm_cap_snaps(struct ceph_mds_client * mdsc,struct ceph_snap_realm * realm)7142e2023e9SXiubo Li static void queue_realm_cap_snaps(struct ceph_mds_client *mdsc,
7152e2023e9SXiubo Li 				  struct ceph_snap_realm *realm)
716ed326044SSage Weil {
717ed326044SSage Weil 	struct ceph_inode_info *ci;
718ed326044SSage Weil 	struct inode *lastinode = NULL;
7191ab36c9dSXiubo Li 	struct ceph_cap_snap *capsnap = NULL;
720ed326044SSage Weil 
721ad5255c1SXiubo Li 	dout("%s %p %llx inode\n", __func__, realm, realm->ino);
722ed326044SSage Weil 
723ed326044SSage Weil 	spin_lock(&realm->inodes_with_caps_lock);
7243ae0bebcSYan, Zheng 	list_for_each_entry(ci, &realm->inodes_with_caps, i_snap_realm_item) {
725874c8ca1SDavid Howells 		struct inode *inode = igrab(&ci->netfs.inode);
726ed326044SSage Weil 		if (!inode)
727ed326044SSage Weil 			continue;
728ed326044SSage Weil 		spin_unlock(&realm->inodes_with_caps_lock);
72923c2c76eSJeff Layton 		iput(lastinode);
730ed326044SSage Weil 		lastinode = inode;
7311ab36c9dSXiubo Li 
7321ab36c9dSXiubo Li 		/*
7331ab36c9dSXiubo Li 		 * Allocate the capsnap memory outside of ceph_queue_cap_snap()
7341ab36c9dSXiubo Li 		 * to reduce very possible but unnecessary frequently memory
7351ab36c9dSXiubo Li 		 * allocate/free in this loop.
7361ab36c9dSXiubo Li 		 */
7371ab36c9dSXiubo Li 		if (!capsnap) {
7381ab36c9dSXiubo Li 			capsnap = kmem_cache_zalloc(ceph_cap_snap_cachep, GFP_NOFS);
7391ab36c9dSXiubo Li 			if (!capsnap) {
7401ab36c9dSXiubo Li 				pr_err("ENOMEM allocating ceph_cap_snap on %p\n",
7411ab36c9dSXiubo Li 				       inode);
7421ab36c9dSXiubo Li 				return;
7431ab36c9dSXiubo Li 			}
7441ab36c9dSXiubo Li 		}
7451ab36c9dSXiubo Li 		capsnap->cap_flush.is_capsnap = true;
7461ab36c9dSXiubo Li 		refcount_set(&capsnap->nref, 1);
7471ab36c9dSXiubo Li 		INIT_LIST_HEAD(&capsnap->cap_flush.i_list);
7481ab36c9dSXiubo Li 		INIT_LIST_HEAD(&capsnap->cap_flush.g_list);
7491ab36c9dSXiubo Li 		INIT_LIST_HEAD(&capsnap->ci_item);
7501ab36c9dSXiubo Li 
7511ab36c9dSXiubo Li 		ceph_queue_cap_snap(ci, &capsnap);
752ed326044SSage Weil 		spin_lock(&realm->inodes_with_caps_lock);
753ed326044SSage Weil 	}
754ed326044SSage Weil 	spin_unlock(&realm->inodes_with_caps_lock);
75523c2c76eSJeff Layton 	iput(lastinode);
756ed326044SSage Weil 
7571ab36c9dSXiubo Li 	if (capsnap)
7581ab36c9dSXiubo Li 		kmem_cache_free(ceph_cap_snap_cachep, capsnap);
759ad5255c1SXiubo Li 	dout("%s %p %llx done\n", __func__, realm, realm->ino);
760ed326044SSage Weil }
761963b61ebSSage Weil 
762963b61ebSSage Weil /*
763963b61ebSSage Weil  * Parse and apply a snapblob "snap trace" from the MDS.  This specifies
764963b61ebSSage Weil  * the snap realm parameters from a given realm and all of its ancestors,
765963b61ebSSage Weil  * up to the root.
766963b61ebSSage Weil  *
767963b61ebSSage Weil  * Caller must hold snap_rwsem for write.
768963b61ebSSage Weil  */
ceph_update_snap_trace(struct ceph_mds_client * mdsc,void * p,void * e,bool deletion,struct ceph_snap_realm ** realm_ret)769963b61ebSSage Weil int ceph_update_snap_trace(struct ceph_mds_client *mdsc,
770982d6011SYan, Zheng 			   void *p, void *e, bool deletion,
771982d6011SYan, Zheng 			   struct ceph_snap_realm **realm_ret)
772963b61ebSSage Weil {
773963b61ebSSage Weil 	struct ceph_mds_snap_realm *ri;    /* encoded */
774963b61ebSSage Weil 	__le64 *snaps;                     /* encoded */
775963b61ebSSage Weil 	__le64 *prior_parent_snaps;        /* encoded */
77651884d15SXiubo Li 	struct ceph_snap_realm *realm;
777982d6011SYan, Zheng 	struct ceph_snap_realm *first_realm = NULL;
7782e586641SXiubo Li 	struct ceph_snap_realm *realm_to_rebuild = NULL;
779a68e564aSXiubo Li 	struct ceph_client *client = mdsc->fsc->client;
7802e586641SXiubo Li 	int rebuild_snapcs;
781963b61ebSSage Weil 	int err = -ENOMEM;
782a68e564aSXiubo Li 	int ret;
783ae00d4f3SSage Weil 	LIST_HEAD(dirty_realms);
784963b61ebSSage Weil 
785a6862e67SJeff Layton 	lockdep_assert_held_write(&mdsc->snap_rwsem);
786a6862e67SJeff Layton 
787ad5255c1SXiubo Li 	dout("%s deletion=%d\n", __func__, deletion);
788963b61ebSSage Weil more:
78951884d15SXiubo Li 	realm = NULL;
7902e586641SXiubo Li 	rebuild_snapcs = 0;
791963b61ebSSage Weil 	ceph_decode_need(&p, e, sizeof(*ri), bad);
792963b61ebSSage Weil 	ri = p;
793963b61ebSSage Weil 	p += sizeof(*ri);
794963b61ebSSage Weil 	ceph_decode_need(&p, e, sizeof(u64)*(le32_to_cpu(ri->num_snaps) +
795963b61ebSSage Weil 			    le32_to_cpu(ri->num_prior_parent_snaps)), bad);
796963b61ebSSage Weil 	snaps = p;
797963b61ebSSage Weil 	p += sizeof(u64) * le32_to_cpu(ri->num_snaps);
798963b61ebSSage Weil 	prior_parent_snaps = p;
799963b61ebSSage Weil 	p += sizeof(u64) * le32_to_cpu(ri->num_prior_parent_snaps);
800963b61ebSSage Weil 
801963b61ebSSage Weil 	realm = ceph_lookup_snap_realm(mdsc, le64_to_cpu(ri->ino));
802963b61ebSSage Weil 	if (!realm) {
803963b61ebSSage Weil 		realm = ceph_create_snap_realm(mdsc, le64_to_cpu(ri->ino));
804963b61ebSSage Weil 		if (IS_ERR(realm)) {
805963b61ebSSage Weil 			err = PTR_ERR(realm);
806963b61ebSSage Weil 			goto fail;
807963b61ebSSage Weil 		}
808963b61ebSSage Weil 	}
809963b61ebSSage Weil 
810963b61ebSSage Weil 	/* ensure the parent is correct */
811963b61ebSSage Weil 	err = adjust_snap_realm_parent(mdsc, realm, le64_to_cpu(ri->parent));
812963b61ebSSage Weil 	if (err < 0)
813963b61ebSSage Weil 		goto fail;
8142e586641SXiubo Li 	rebuild_snapcs += err;
815963b61ebSSage Weil 
816963b61ebSSage Weil 	if (le64_to_cpu(ri->seq) > realm->seq) {
817ad5255c1SXiubo Li 		dout("%s updating %llx %p %lld -> %lld\n", __func__,
818ae00d4f3SSage Weil 		     realm->ino, realm, realm->seq, le64_to_cpu(ri->seq));
819963b61ebSSage Weil 		/* update realm parameters, snap lists */
820963b61ebSSage Weil 		realm->seq = le64_to_cpu(ri->seq);
821963b61ebSSage Weil 		realm->created = le64_to_cpu(ri->created);
822963b61ebSSage Weil 		realm->parent_since = le64_to_cpu(ri->parent_since);
823963b61ebSSage Weil 
824963b61ebSSage Weil 		realm->num_snaps = le32_to_cpu(ri->num_snaps);
825963b61ebSSage Weil 		err = dup_array(&realm->snaps, snaps, realm->num_snaps);
826963b61ebSSage Weil 		if (err < 0)
827963b61ebSSage Weil 			goto fail;
828963b61ebSSage Weil 
829963b61ebSSage Weil 		realm->num_prior_parent_snaps =
830963b61ebSSage Weil 			le32_to_cpu(ri->num_prior_parent_snaps);
831963b61ebSSage Weil 		err = dup_array(&realm->prior_parent_snaps, prior_parent_snaps,
832963b61ebSSage Weil 				realm->num_prior_parent_snaps);
833963b61ebSSage Weil 		if (err < 0)
834963b61ebSSage Weil 			goto fail;
835963b61ebSSage Weil 
836affbc19aSYan, Zheng 		if (realm->seq > mdsc->last_snap_seq)
837affbc19aSYan, Zheng 			mdsc->last_snap_seq = realm->seq;
838ae00d4f3SSage Weil 
8392e586641SXiubo Li 		rebuild_snapcs = 1;
840963b61ebSSage Weil 	} else if (!realm->cached_context) {
841ad5255c1SXiubo Li 		dout("%s %llx %p seq %lld new\n", __func__,
842ae00d4f3SSage Weil 		     realm->ino, realm, realm->seq);
8432e586641SXiubo Li 		rebuild_snapcs = 1;
844ae00d4f3SSage Weil 	} else {
845ad5255c1SXiubo Li 		dout("%s %llx %p seq %lld unchanged\n", __func__,
846ae00d4f3SSage Weil 		     realm->ino, realm, realm->seq);
847963b61ebSSage Weil 	}
848963b61ebSSage Weil 
8492e586641SXiubo Li 	dout("done with %llx %p, rebuild_snapcs=%d, %p %p\n", realm->ino,
8502e586641SXiubo Li 	     realm, rebuild_snapcs, p, e);
851963b61ebSSage Weil 
8522e586641SXiubo Li 	/*
8532e586641SXiubo Li 	 * this will always track the uppest parent realm from which
8542e586641SXiubo Li 	 * we need to rebuild the snapshot contexts _downward_ in
8552e586641SXiubo Li 	 * hierarchy.
8562e586641SXiubo Li 	 */
8572e586641SXiubo Li 	if (rebuild_snapcs)
8582e586641SXiubo Li 		realm_to_rebuild = realm;
8592e586641SXiubo Li 
8602e586641SXiubo Li 	/* rebuild_snapcs when we reach the _end_ (root) of the trace */
8612e586641SXiubo Li 	if (realm_to_rebuild && p >= e)
8622e2023e9SXiubo Li 		rebuild_snap_realms(mdsc, realm_to_rebuild, &dirty_realms);
863982d6011SYan, Zheng 
864982d6011SYan, Zheng 	if (!first_realm)
865982d6011SYan, Zheng 		first_realm = realm;
866982d6011SYan, Zheng 	else
867982d6011SYan, Zheng 		ceph_put_snap_realm(mdsc, realm);
868982d6011SYan, Zheng 
869963b61ebSSage Weil 	if (p < e)
870963b61ebSSage Weil 		goto more;
871963b61ebSSage Weil 
872ae00d4f3SSage Weil 	/*
873ae00d4f3SSage Weil 	 * queue cap snaps _after_ we've built the new snap contexts,
874ae00d4f3SSage Weil 	 * so that i_head_snapc can be set appropriately.
875ae00d4f3SSage Weil 	 */
876e8e1ba96SSage Weil 	while (!list_empty(&dirty_realms)) {
877e8e1ba96SSage Weil 		realm = list_first_entry(&dirty_realms, struct ceph_snap_realm,
878e8e1ba96SSage Weil 					 dirty_item);
8793ae0bebcSYan, Zheng 		list_del_init(&realm->dirty_item);
8802e2023e9SXiubo Li 		queue_realm_cap_snaps(mdsc, realm);
881ae00d4f3SSage Weil 	}
882ae00d4f3SSage Weil 
883982d6011SYan, Zheng 	if (realm_ret)
884982d6011SYan, Zheng 		*realm_ret = first_realm;
885982d6011SYan, Zheng 	else
886982d6011SYan, Zheng 		ceph_put_snap_realm(mdsc, first_realm);
887982d6011SYan, Zheng 
888963b61ebSSage Weil 	__cleanup_empty_realms(mdsc);
889963b61ebSSage Weil 	return 0;
890963b61ebSSage Weil 
891963b61ebSSage Weil bad:
892f3fd3ea6SJeff Layton 	err = -EIO;
893963b61ebSSage Weil fail:
894982d6011SYan, Zheng 	if (realm && !IS_ERR(realm))
895982d6011SYan, Zheng 		ceph_put_snap_realm(mdsc, realm);
896982d6011SYan, Zheng 	if (first_realm)
897982d6011SYan, Zheng 		ceph_put_snap_realm(mdsc, first_realm);
898ad5255c1SXiubo Li 	pr_err("%s error %d\n", __func__, err);
899a68e564aSXiubo Li 
900a68e564aSXiubo Li 	/*
901a68e564aSXiubo Li 	 * When receiving a corrupted snap trace we don't know what
902a68e564aSXiubo Li 	 * exactly has happened in MDS side. And we shouldn't continue
903a68e564aSXiubo Li 	 * writing to OSD, which may corrupt the snapshot contents.
904a68e564aSXiubo Li 	 *
905a68e564aSXiubo Li 	 * Just try to blocklist this kclient and then this kclient
906a68e564aSXiubo Li 	 * must be remounted to continue after the corrupted metadata
907a68e564aSXiubo Li 	 * fixed in the MDS side.
908a68e564aSXiubo Li 	 */
909a68e564aSXiubo Li 	WRITE_ONCE(mdsc->fsc->mount_state, CEPH_MOUNT_FENCE_IO);
910a68e564aSXiubo Li 	ret = ceph_monc_blocklist_add(&client->monc, &client->msgr.inst.addr);
911a68e564aSXiubo Li 	if (ret)
912a68e564aSXiubo Li 		pr_err("%s failed to blocklist %s: %d\n", __func__,
913a68e564aSXiubo Li 		       ceph_pr_addr(&client->msgr.inst.addr), ret);
914a68e564aSXiubo Li 
915a68e564aSXiubo Li 	WARN(1, "%s: %s%sdo remount to continue%s",
916a68e564aSXiubo Li 	     __func__, ret ? "" : ceph_pr_addr(&client->msgr.inst.addr),
917a68e564aSXiubo Li 	     ret ? "" : " was blocklisted, ",
918a68e564aSXiubo Li 	     err == -EIO ? " after corrupted snaptrace is fixed" : "");
919a68e564aSXiubo Li 
920963b61ebSSage Weil 	return err;
921963b61ebSSage Weil }
922963b61ebSSage Weil 
923963b61ebSSage Weil 
924963b61ebSSage Weil /*
925963b61ebSSage Weil  * Send any cap_snaps that are queued for flush.  Try to carry
926963b61ebSSage Weil  * s_mutex across multiple snap flushes to avoid locking overhead.
927963b61ebSSage Weil  *
928963b61ebSSage Weil  * Caller holds no locks.
929963b61ebSSage Weil  */
flush_snaps(struct ceph_mds_client * mdsc)930963b61ebSSage Weil static void flush_snaps(struct ceph_mds_client *mdsc)
931963b61ebSSage Weil {
932963b61ebSSage Weil 	struct ceph_inode_info *ci;
933963b61ebSSage Weil 	struct inode *inode;
934963b61ebSSage Weil 	struct ceph_mds_session *session = NULL;
935963b61ebSSage Weil 
936ad5255c1SXiubo Li 	dout("%s\n", __func__);
937963b61ebSSage Weil 	spin_lock(&mdsc->snap_flush_lock);
938963b61ebSSage Weil 	while (!list_empty(&mdsc->snap_flush_list)) {
939963b61ebSSage Weil 		ci = list_first_entry(&mdsc->snap_flush_list,
940963b61ebSSage Weil 				struct ceph_inode_info, i_snap_flush_item);
941874c8ca1SDavid Howells 		inode = &ci->netfs.inode;
94270b666c3SSage Weil 		ihold(inode);
943963b61ebSSage Weil 		spin_unlock(&mdsc->snap_flush_lock);
944ed9b430cSYan, Zheng 		ceph_flush_snaps(ci, &session);
94523c2c76eSJeff Layton 		iput(inode);
946963b61ebSSage Weil 		spin_lock(&mdsc->snap_flush_lock);
947963b61ebSSage Weil 	}
948963b61ebSSage Weil 	spin_unlock(&mdsc->snap_flush_lock);
949963b61ebSSage Weil 
950963b61ebSSage Weil 	ceph_put_mds_session(session);
951ad5255c1SXiubo Li 	dout("%s done\n", __func__);
952963b61ebSSage Weil }
953963b61ebSSage Weil 
9540ba92e1cSJeff Layton /**
9550ba92e1cSJeff Layton  * ceph_change_snap_realm - change the snap_realm for an inode
9560ba92e1cSJeff Layton  * @inode: inode to move to new snap realm
9570ba92e1cSJeff Layton  * @realm: new realm to move inode into (may be NULL)
9580ba92e1cSJeff Layton  *
9590ba92e1cSJeff Layton  * Detach an inode from its old snaprealm (if any) and attach it to
9600ba92e1cSJeff Layton  * the new snaprealm (if any). The old snap realm reference held by
9610ba92e1cSJeff Layton  * the inode is put. If realm is non-NULL, then the caller's reference
9620ba92e1cSJeff Layton  * to it is taken over by the inode.
9630ba92e1cSJeff Layton  */
ceph_change_snap_realm(struct inode * inode,struct ceph_snap_realm * realm)9640ba92e1cSJeff Layton void ceph_change_snap_realm(struct inode *inode, struct ceph_snap_realm *realm)
9650ba92e1cSJeff Layton {
9660ba92e1cSJeff Layton 	struct ceph_inode_info *ci = ceph_inode(inode);
967*985b9ee8SXiubo Li 	struct ceph_mds_client *mdsc = ceph_inode_to_fs_client(inode)->mdsc;
9680ba92e1cSJeff Layton 	struct ceph_snap_realm *oldrealm = ci->i_snap_realm;
9690ba92e1cSJeff Layton 
9700ba92e1cSJeff Layton 	lockdep_assert_held(&ci->i_ceph_lock);
9710ba92e1cSJeff Layton 
9720ba92e1cSJeff Layton 	if (oldrealm) {
9730ba92e1cSJeff Layton 		spin_lock(&oldrealm->inodes_with_caps_lock);
9740ba92e1cSJeff Layton 		list_del_init(&ci->i_snap_realm_item);
9750ba92e1cSJeff Layton 		if (oldrealm->ino == ci->i_vino.ino)
9760ba92e1cSJeff Layton 			oldrealm->inode = NULL;
9770ba92e1cSJeff Layton 		spin_unlock(&oldrealm->inodes_with_caps_lock);
9780ba92e1cSJeff Layton 		ceph_put_snap_realm(mdsc, oldrealm);
9790ba92e1cSJeff Layton 	}
9800ba92e1cSJeff Layton 
9810ba92e1cSJeff Layton 	ci->i_snap_realm = realm;
9820ba92e1cSJeff Layton 
9830ba92e1cSJeff Layton 	if (realm) {
9840ba92e1cSJeff Layton 		spin_lock(&realm->inodes_with_caps_lock);
9850ba92e1cSJeff Layton 		list_add(&ci->i_snap_realm_item, &realm->inodes_with_caps);
9860ba92e1cSJeff Layton 		if (realm->ino == ci->i_vino.ino)
9870ba92e1cSJeff Layton 			realm->inode = inode;
9880ba92e1cSJeff Layton 		spin_unlock(&realm->inodes_with_caps_lock);
9890ba92e1cSJeff Layton 	}
9900ba92e1cSJeff Layton }
991963b61ebSSage Weil 
992963b61ebSSage Weil /*
993963b61ebSSage Weil  * Handle a snap notification from the MDS.
994963b61ebSSage Weil  *
995963b61ebSSage Weil  * This can take two basic forms: the simplest is just a snap creation
996963b61ebSSage Weil  * or deletion notification on an existing realm.  This should update the
997963b61ebSSage Weil  * realm and its children.
998963b61ebSSage Weil  *
999963b61ebSSage Weil  * The more difficult case is realm creation, due to snap creation at a
1000963b61ebSSage Weil  * new point in the file hierarchy, or due to a rename that moves a file or
1001963b61ebSSage Weil  * directory into another realm.
1002963b61ebSSage Weil  */
ceph_handle_snap(struct ceph_mds_client * mdsc,struct ceph_mds_session * session,struct ceph_msg * msg)1003963b61ebSSage Weil void ceph_handle_snap(struct ceph_mds_client *mdsc,
10042600d2ddSSage Weil 		      struct ceph_mds_session *session,
1005963b61ebSSage Weil 		      struct ceph_msg *msg)
1006963b61ebSSage Weil {
10073d14c5d2SYehuda Sadeh 	struct super_block *sb = mdsc->fsc->sb;
10082600d2ddSSage Weil 	int mds = session->s_mds;
1009963b61ebSSage Weil 	u64 split;
1010963b61ebSSage Weil 	int op;
1011963b61ebSSage Weil 	int trace_len;
1012963b61ebSSage Weil 	struct ceph_snap_realm *realm = NULL;
1013963b61ebSSage Weil 	void *p = msg->front.iov_base;
1014963b61ebSSage Weil 	void *e = p + msg->front.iov_len;
1015963b61ebSSage Weil 	struct ceph_mds_snap_head *h;
1016963b61ebSSage Weil 	int num_split_inos, num_split_realms;
1017963b61ebSSage Weil 	__le64 *split_inos = NULL, *split_realms = NULL;
1018963b61ebSSage Weil 	int i;
1019963b61ebSSage Weil 	int locked_rwsem = 0;
1020a68e564aSXiubo Li 	bool close_sessions = false;
1021963b61ebSSage Weil 
1022e3dfcab2SXiubo Li 	if (!ceph_inc_mds_stopping_blocker(mdsc, session))
1023e3dfcab2SXiubo Li 		return;
1024e3dfcab2SXiubo Li 
1025963b61ebSSage Weil 	/* decode */
1026963b61ebSSage Weil 	if (msg->front.iov_len < sizeof(*h))
1027963b61ebSSage Weil 		goto bad;
1028963b61ebSSage Weil 	h = p;
1029963b61ebSSage Weil 	op = le32_to_cpu(h->op);
1030963b61ebSSage Weil 	split = le64_to_cpu(h->split);   /* non-zero if we are splitting an
1031963b61ebSSage Weil 					  * existing realm */
1032963b61ebSSage Weil 	num_split_inos = le32_to_cpu(h->num_split_inos);
1033963b61ebSSage Weil 	num_split_realms = le32_to_cpu(h->num_split_realms);
1034963b61ebSSage Weil 	trace_len = le32_to_cpu(h->trace_len);
1035963b61ebSSage Weil 	p += sizeof(*h);
1036963b61ebSSage Weil 
1037ad5255c1SXiubo Li 	dout("%s from mds%d op %s split %llx tracelen %d\n", __func__,
1038ad5255c1SXiubo Li 	     mds, ceph_snap_op_name(op), split, trace_len);
1039963b61ebSSage Weil 
1040963b61ebSSage Weil 	down_write(&mdsc->snap_rwsem);
1041963b61ebSSage Weil 	locked_rwsem = 1;
1042963b61ebSSage Weil 
1043963b61ebSSage Weil 	if (op == CEPH_SNAP_OP_SPLIT) {
1044963b61ebSSage Weil 		struct ceph_mds_snap_realm *ri;
1045963b61ebSSage Weil 
1046963b61ebSSage Weil 		/*
1047963b61ebSSage Weil 		 * A "split" breaks part of an existing realm off into
1048963b61ebSSage Weil 		 * a new realm.  The MDS provides a list of inodes
1049963b61ebSSage Weil 		 * (with caps) and child realms that belong to the new
1050963b61ebSSage Weil 		 * child.
1051963b61ebSSage Weil 		 */
1052963b61ebSSage Weil 		split_inos = p;
1053963b61ebSSage Weil 		p += sizeof(u64) * num_split_inos;
1054963b61ebSSage Weil 		split_realms = p;
1055963b61ebSSage Weil 		p += sizeof(u64) * num_split_realms;
1056963b61ebSSage Weil 		ceph_decode_need(&p, e, sizeof(*ri), bad);
1057963b61ebSSage Weil 		/* we will peek at realm info here, but will _not_
1058963b61ebSSage Weil 		 * advance p, as the realm update will occur below in
1059963b61ebSSage Weil 		 * ceph_update_snap_trace. */
1060963b61ebSSage Weil 		ri = p;
1061963b61ebSSage Weil 
1062963b61ebSSage Weil 		realm = ceph_lookup_snap_realm(mdsc, split);
1063963b61ebSSage Weil 		if (!realm) {
1064963b61ebSSage Weil 			realm = ceph_create_snap_realm(mdsc, split);
1065963b61ebSSage Weil 			if (IS_ERR(realm))
1066963b61ebSSage Weil 				goto out;
1067963b61ebSSage Weil 		}
1068963b61ebSSage Weil 
1069963b61ebSSage Weil 		dout("splitting snap_realm %llx %p\n", realm->ino, realm);
1070963b61ebSSage Weil 		for (i = 0; i < num_split_inos; i++) {
1071963b61ebSSage Weil 			struct ceph_vino vino = {
1072963b61ebSSage Weil 				.ino = le64_to_cpu(split_inos[i]),
1073963b61ebSSage Weil 				.snap = CEPH_NOSNAP,
1074963b61ebSSage Weil 			};
1075963b61ebSSage Weil 			struct inode *inode = ceph_find_inode(sb, vino);
1076963b61ebSSage Weil 			struct ceph_inode_info *ci;
1077963b61ebSSage Weil 
1078963b61ebSSage Weil 			if (!inode)
1079963b61ebSSage Weil 				continue;
1080963b61ebSSage Weil 			ci = ceph_inode(inode);
1081963b61ebSSage Weil 
1082be655596SSage Weil 			spin_lock(&ci->i_ceph_lock);
1083963b61ebSSage Weil 			if (!ci->i_snap_realm)
1084963b61ebSSage Weil 				goto skip_inode;
1085963b61ebSSage Weil 			/*
1086963b61ebSSage Weil 			 * If this inode belongs to a realm that was
1087963b61ebSSage Weil 			 * created after our new realm, we experienced
1088963b61ebSSage Weil 			 * a race (due to another split notifications
1089963b61ebSSage Weil 			 * arriving from a different MDS).  So skip
1090963b61ebSSage Weil 			 * this inode.
1091963b61ebSSage Weil 			 */
1092963b61ebSSage Weil 			if (ci->i_snap_realm->created >
1093963b61ebSSage Weil 			    le64_to_cpu(ri->created)) {
1094ad5255c1SXiubo Li 				dout(" leaving %p %llx.%llx in newer realm %llx %p\n",
1095ad5255c1SXiubo Li 				     inode, ceph_vinop(inode), ci->i_snap_realm->ino,
1096963b61ebSSage Weil 				     ci->i_snap_realm);
1097963b61ebSSage Weil 				goto skip_inode;
1098963b61ebSSage Weil 			}
1099ad5255c1SXiubo Li 			dout(" will move %p %llx.%llx to split realm %llx %p\n",
1100ad5255c1SXiubo Li 			     inode, ceph_vinop(inode), realm->ino, realm);
1101963b61ebSSage Weil 
1102ae00d4f3SSage Weil 			ceph_get_snap_realm(mdsc, realm);
11030ba92e1cSJeff Layton 			ceph_change_snap_realm(inode, realm);
11040ba92e1cSJeff Layton 			spin_unlock(&ci->i_ceph_lock);
110523c2c76eSJeff Layton 			iput(inode);
1106963b61ebSSage Weil 			continue;
1107963b61ebSSage Weil 
1108963b61ebSSage Weil skip_inode:
1109be655596SSage Weil 			spin_unlock(&ci->i_ceph_lock);
111023c2c76eSJeff Layton 			iput(inode);
1111963b61ebSSage Weil 		}
1112963b61ebSSage Weil 
1113963b61ebSSage Weil 		/* we may have taken some of the old realm's children. */
1114963b61ebSSage Weil 		for (i = 0; i < num_split_realms; i++) {
1115963b61ebSSage Weil 			struct ceph_snap_realm *child =
1116982d6011SYan, Zheng 				__lookup_snap_realm(mdsc,
1117963b61ebSSage Weil 					   le64_to_cpu(split_realms[i]));
1118963b61ebSSage Weil 			if (!child)
1119963b61ebSSage Weil 				continue;
1120963b61ebSSage Weil 			adjust_snap_realm_parent(mdsc, child, realm->ino);
1121963b61ebSSage Weil 		}
11224cafd040SXiubo Li 	} else {
11234cafd040SXiubo Li 		/*
11244cafd040SXiubo Li 		 * In the non-split case both 'num_split_inos' and
11254cafd040SXiubo Li 		 * 'num_split_realms' should be 0, making this a no-op.
11264cafd040SXiubo Li 		 * However the MDS happens to populate 'split_realms' list
11274cafd040SXiubo Li 		 * in one of the UPDATE op cases by mistake.
11284cafd040SXiubo Li 		 *
11294cafd040SXiubo Li 		 * Skip both lists just in case to ensure that 'p' is
11304cafd040SXiubo Li 		 * positioned at the start of realm info, as expected by
11314cafd040SXiubo Li 		 * ceph_update_snap_trace().
11324cafd040SXiubo Li 		 */
11334cafd040SXiubo Li 		p += sizeof(u64) * num_split_inos;
11344cafd040SXiubo Li 		p += sizeof(u64) * num_split_realms;
1135963b61ebSSage Weil 	}
1136963b61ebSSage Weil 
1137963b61ebSSage Weil 	/*
1138963b61ebSSage Weil 	 * update using the provided snap trace. if we are deleting a
1139963b61ebSSage Weil 	 * snap, we can avoid queueing cap_snaps.
1140963b61ebSSage Weil 	 */
1141a68e564aSXiubo Li 	if (ceph_update_snap_trace(mdsc, p, e,
1142a68e564aSXiubo Li 				   op == CEPH_SNAP_OP_DESTROY,
1143a68e564aSXiubo Li 				   NULL)) {
1144a68e564aSXiubo Li 		close_sessions = true;
1145a68e564aSXiubo Li 		goto bad;
1146a68e564aSXiubo Li 	}
1147963b61ebSSage Weil 
1148ae00d4f3SSage Weil 	if (op == CEPH_SNAP_OP_SPLIT)
1149963b61ebSSage Weil 		/* we took a reference when we created the realm, above */
1150963b61ebSSage Weil 		ceph_put_snap_realm(mdsc, realm);
1151963b61ebSSage Weil 
1152963b61ebSSage Weil 	__cleanup_empty_realms(mdsc);
1153963b61ebSSage Weil 
1154963b61ebSSage Weil 	up_write(&mdsc->snap_rwsem);
1155963b61ebSSage Weil 
1156963b61ebSSage Weil 	flush_snaps(mdsc);
1157e3dfcab2SXiubo Li 	ceph_dec_mds_stopping_blocker(mdsc);
1158963b61ebSSage Weil 	return;
1159963b61ebSSage Weil 
1160963b61ebSSage Weil bad:
1161ad5255c1SXiubo Li 	pr_err("%s corrupt snap message from mds%d\n", __func__, mds);
11629ec7cab1SSage Weil 	ceph_msg_dump(msg);
1163963b61ebSSage Weil out:
1164963b61ebSSage Weil 	if (locked_rwsem)
1165963b61ebSSage Weil 		up_write(&mdsc->snap_rwsem);
1166a68e564aSXiubo Li 
1167e3dfcab2SXiubo Li 	ceph_dec_mds_stopping_blocker(mdsc);
1168e3dfcab2SXiubo Li 
1169a68e564aSXiubo Li 	if (close_sessions)
1170a68e564aSXiubo Li 		ceph_mdsc_close_sessions(mdsc);
1171963b61ebSSage Weil 	return;
1172963b61ebSSage Weil }
117375c9627eSYan, Zheng 
ceph_get_snapid_map(struct ceph_mds_client * mdsc,u64 snap)117475c9627eSYan, Zheng struct ceph_snapid_map* ceph_get_snapid_map(struct ceph_mds_client *mdsc,
117575c9627eSYan, Zheng 					    u64 snap)
117675c9627eSYan, Zheng {
117775c9627eSYan, Zheng 	struct ceph_snapid_map *sm, *exist;
117875c9627eSYan, Zheng 	struct rb_node **p, *parent;
117975c9627eSYan, Zheng 	int ret;
118075c9627eSYan, Zheng 
118175c9627eSYan, Zheng 	exist = NULL;
118275c9627eSYan, Zheng 	spin_lock(&mdsc->snapid_map_lock);
118375c9627eSYan, Zheng 	p = &mdsc->snapid_map_tree.rb_node;
118475c9627eSYan, Zheng 	while (*p) {
118575c9627eSYan, Zheng 		exist = rb_entry(*p, struct ceph_snapid_map, node);
118675c9627eSYan, Zheng 		if (snap > exist->snap) {
118775c9627eSYan, Zheng 			p = &(*p)->rb_left;
118875c9627eSYan, Zheng 		} else if (snap < exist->snap) {
118975c9627eSYan, Zheng 			p = &(*p)->rb_right;
119075c9627eSYan, Zheng 		} else {
119175c9627eSYan, Zheng 			if (atomic_inc_return(&exist->ref) == 1)
119275c9627eSYan, Zheng 				list_del_init(&exist->lru);
119375c9627eSYan, Zheng 			break;
119475c9627eSYan, Zheng 		}
119575c9627eSYan, Zheng 		exist = NULL;
119675c9627eSYan, Zheng 	}
119775c9627eSYan, Zheng 	spin_unlock(&mdsc->snapid_map_lock);
119875c9627eSYan, Zheng 	if (exist) {
1199ad5255c1SXiubo Li 		dout("%s found snapid map %llx -> %x\n", __func__,
1200ad5255c1SXiubo Li 		     exist->snap, exist->dev);
120175c9627eSYan, Zheng 		return exist;
120275c9627eSYan, Zheng 	}
120375c9627eSYan, Zheng 
120475c9627eSYan, Zheng 	sm = kmalloc(sizeof(*sm), GFP_NOFS);
120575c9627eSYan, Zheng 	if (!sm)
120675c9627eSYan, Zheng 		return NULL;
120775c9627eSYan, Zheng 
120875c9627eSYan, Zheng 	ret = get_anon_bdev(&sm->dev);
120975c9627eSYan, Zheng 	if (ret < 0) {
121075c9627eSYan, Zheng 		kfree(sm);
121175c9627eSYan, Zheng 		return NULL;
121275c9627eSYan, Zheng 	}
121375c9627eSYan, Zheng 
121475c9627eSYan, Zheng 	INIT_LIST_HEAD(&sm->lru);
121575c9627eSYan, Zheng 	atomic_set(&sm->ref, 1);
121675c9627eSYan, Zheng 	sm->snap = snap;
121775c9627eSYan, Zheng 
121875c9627eSYan, Zheng 	exist = NULL;
121975c9627eSYan, Zheng 	parent = NULL;
122075c9627eSYan, Zheng 	p = &mdsc->snapid_map_tree.rb_node;
122175c9627eSYan, Zheng 	spin_lock(&mdsc->snapid_map_lock);
122275c9627eSYan, Zheng 	while (*p) {
122375c9627eSYan, Zheng 		parent = *p;
122475c9627eSYan, Zheng 		exist = rb_entry(*p, struct ceph_snapid_map, node);
122575c9627eSYan, Zheng 		if (snap > exist->snap)
122675c9627eSYan, Zheng 			p = &(*p)->rb_left;
122775c9627eSYan, Zheng 		else if (snap < exist->snap)
122875c9627eSYan, Zheng 			p = &(*p)->rb_right;
122975c9627eSYan, Zheng 		else
123075c9627eSYan, Zheng 			break;
123175c9627eSYan, Zheng 		exist = NULL;
123275c9627eSYan, Zheng 	}
123375c9627eSYan, Zheng 	if (exist) {
123475c9627eSYan, Zheng 		if (atomic_inc_return(&exist->ref) == 1)
123575c9627eSYan, Zheng 			list_del_init(&exist->lru);
123675c9627eSYan, Zheng 	} else {
123775c9627eSYan, Zheng 		rb_link_node(&sm->node, parent, p);
123875c9627eSYan, Zheng 		rb_insert_color(&sm->node, &mdsc->snapid_map_tree);
123975c9627eSYan, Zheng 	}
124075c9627eSYan, Zheng 	spin_unlock(&mdsc->snapid_map_lock);
124175c9627eSYan, Zheng 	if (exist) {
124275c9627eSYan, Zheng 		free_anon_bdev(sm->dev);
124375c9627eSYan, Zheng 		kfree(sm);
1244ad5255c1SXiubo Li 		dout("%s found snapid map %llx -> %x\n", __func__,
1245ad5255c1SXiubo Li 		     exist->snap, exist->dev);
124675c9627eSYan, Zheng 		return exist;
124775c9627eSYan, Zheng 	}
124875c9627eSYan, Zheng 
1249ad5255c1SXiubo Li 	dout("%s create snapid map %llx -> %x\n", __func__,
1250ad5255c1SXiubo Li 	     sm->snap, sm->dev);
125175c9627eSYan, Zheng 	return sm;
125275c9627eSYan, Zheng }
125375c9627eSYan, Zheng 
ceph_put_snapid_map(struct ceph_mds_client * mdsc,struct ceph_snapid_map * sm)125475c9627eSYan, Zheng void ceph_put_snapid_map(struct ceph_mds_client* mdsc,
125575c9627eSYan, Zheng 			 struct ceph_snapid_map *sm)
125675c9627eSYan, Zheng {
125775c9627eSYan, Zheng 	if (!sm)
125875c9627eSYan, Zheng 		return;
125975c9627eSYan, Zheng 	if (atomic_dec_and_lock(&sm->ref, &mdsc->snapid_map_lock)) {
126075c9627eSYan, Zheng 		if (!RB_EMPTY_NODE(&sm->node)) {
126175c9627eSYan, Zheng 			sm->last_used = jiffies;
126275c9627eSYan, Zheng 			list_add_tail(&sm->lru, &mdsc->snapid_map_lru);
126375c9627eSYan, Zheng 			spin_unlock(&mdsc->snapid_map_lock);
126475c9627eSYan, Zheng 		} else {
126575c9627eSYan, Zheng 			/* already cleaned up by
126675c9627eSYan, Zheng 			 * ceph_cleanup_snapid_map() */
126775c9627eSYan, Zheng 			spin_unlock(&mdsc->snapid_map_lock);
126875c9627eSYan, Zheng 			kfree(sm);
126975c9627eSYan, Zheng 		}
127075c9627eSYan, Zheng 	}
127175c9627eSYan, Zheng }
127275c9627eSYan, Zheng 
ceph_trim_snapid_map(struct ceph_mds_client * mdsc)127375c9627eSYan, Zheng void ceph_trim_snapid_map(struct ceph_mds_client *mdsc)
127475c9627eSYan, Zheng {
127575c9627eSYan, Zheng 	struct ceph_snapid_map *sm;
127675c9627eSYan, Zheng 	unsigned long now;
127775c9627eSYan, Zheng 	LIST_HEAD(to_free);
127875c9627eSYan, Zheng 
127975c9627eSYan, Zheng 	spin_lock(&mdsc->snapid_map_lock);
128075c9627eSYan, Zheng 	now = jiffies;
128175c9627eSYan, Zheng 
128275c9627eSYan, Zheng 	while (!list_empty(&mdsc->snapid_map_lru)) {
128375c9627eSYan, Zheng 		sm = list_first_entry(&mdsc->snapid_map_lru,
128475c9627eSYan, Zheng 				      struct ceph_snapid_map, lru);
128575c9627eSYan, Zheng 		if (time_after(sm->last_used + CEPH_SNAPID_MAP_TIMEOUT, now))
128675c9627eSYan, Zheng 			break;
128775c9627eSYan, Zheng 
128875c9627eSYan, Zheng 		rb_erase(&sm->node, &mdsc->snapid_map_tree);
128975c9627eSYan, Zheng 		list_move(&sm->lru, &to_free);
129075c9627eSYan, Zheng 	}
129175c9627eSYan, Zheng 	spin_unlock(&mdsc->snapid_map_lock);
129275c9627eSYan, Zheng 
129375c9627eSYan, Zheng 	while (!list_empty(&to_free)) {
129475c9627eSYan, Zheng 		sm = list_first_entry(&to_free, struct ceph_snapid_map, lru);
129575c9627eSYan, Zheng 		list_del(&sm->lru);
129675c9627eSYan, Zheng 		dout("trim snapid map %llx -> %x\n", sm->snap, sm->dev);
129775c9627eSYan, Zheng 		free_anon_bdev(sm->dev);
129875c9627eSYan, Zheng 		kfree(sm);
129975c9627eSYan, Zheng 	}
130075c9627eSYan, Zheng }
130175c9627eSYan, Zheng 
ceph_cleanup_snapid_map(struct ceph_mds_client * mdsc)130275c9627eSYan, Zheng void ceph_cleanup_snapid_map(struct ceph_mds_client *mdsc)
130375c9627eSYan, Zheng {
130475c9627eSYan, Zheng 	struct ceph_snapid_map *sm;
130575c9627eSYan, Zheng 	struct rb_node *p;
130675c9627eSYan, Zheng 	LIST_HEAD(to_free);
130775c9627eSYan, Zheng 
130875c9627eSYan, Zheng 	spin_lock(&mdsc->snapid_map_lock);
130975c9627eSYan, Zheng 	while ((p = rb_first(&mdsc->snapid_map_tree))) {
131075c9627eSYan, Zheng 		sm = rb_entry(p, struct ceph_snapid_map, node);
131175c9627eSYan, Zheng 		rb_erase(p, &mdsc->snapid_map_tree);
131275c9627eSYan, Zheng 		RB_CLEAR_NODE(p);
131375c9627eSYan, Zheng 		list_move(&sm->lru, &to_free);
131475c9627eSYan, Zheng 	}
131575c9627eSYan, Zheng 	spin_unlock(&mdsc->snapid_map_lock);
131675c9627eSYan, Zheng 
131775c9627eSYan, Zheng 	while (!list_empty(&to_free)) {
131875c9627eSYan, Zheng 		sm = list_first_entry(&to_free, struct ceph_snapid_map, lru);
131975c9627eSYan, Zheng 		list_del(&sm->lru);
132075c9627eSYan, Zheng 		free_anon_bdev(sm->dev);
132175c9627eSYan, Zheng 		if (WARN_ON_ONCE(atomic_read(&sm->ref))) {
132275c9627eSYan, Zheng 			pr_err("snapid map %llx -> %x still in use\n",
132375c9627eSYan, Zheng 			       sm->snap, sm->dev);
132475c9627eSYan, Zheng 		}
1325c8d6ee01SLuis Henriques 		kfree(sm);
132675c9627eSYan, Zheng 	}
132775c9627eSYan, Zheng }
1328