xref: /linux/fs/ceph/snap.c (revision 38d46409)
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 
141*38d46409SXiubo Li 	doutc(mdsc->fsc->client, "%llx %p\n", 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 {
153*38d46409SXiubo Li 	struct ceph_client *cl = mdsc->fsc->client;
154a105f00cSSage Weil 	struct rb_node *n = mdsc->snap_realms.rb_node;
155a105f00cSSage Weil 	struct ceph_snap_realm *r;
156963b61ebSSage Weil 
157df2c0cb7SJeff Layton 	lockdep_assert_held(&mdsc->snap_rwsem);
158a6862e67SJeff Layton 
159a105f00cSSage Weil 	while (n) {
160a105f00cSSage Weil 		r = rb_entry(n, struct ceph_snap_realm, node);
161a105f00cSSage Weil 		if (ino < r->ino)
162a105f00cSSage Weil 			n = n->rb_left;
163a105f00cSSage Weil 		else if (ino > r->ino)
164a105f00cSSage Weil 			n = n->rb_right;
165a105f00cSSage Weil 		else {
166*38d46409SXiubo Li 			doutc(cl, "%llx %p\n", r->ino, r);
167a105f00cSSage Weil 			return r;
168a105f00cSSage Weil 		}
169a105f00cSSage Weil 	}
170a105f00cSSage Weil 	return NULL;
171963b61ebSSage Weil }
172963b61ebSSage Weil 
ceph_lookup_snap_realm(struct ceph_mds_client * mdsc,u64 ino)173982d6011SYan, Zheng struct ceph_snap_realm *ceph_lookup_snap_realm(struct ceph_mds_client *mdsc,
174982d6011SYan, Zheng 					       u64 ino)
175982d6011SYan, Zheng {
176982d6011SYan, Zheng 	struct ceph_snap_realm *r;
177982d6011SYan, Zheng 	r = __lookup_snap_realm(mdsc, ino);
178982d6011SYan, Zheng 	if (r)
179982d6011SYan, Zheng 		ceph_get_snap_realm(mdsc, r);
180982d6011SYan, Zheng 	return r;
181982d6011SYan, Zheng }
182982d6011SYan, Zheng 
183963b61ebSSage Weil static void __put_snap_realm(struct ceph_mds_client *mdsc,
184963b61ebSSage Weil 			     struct ceph_snap_realm *realm);
185963b61ebSSage Weil 
186963b61ebSSage Weil /*
187963b61ebSSage Weil  * called with snap_rwsem (write)
188963b61ebSSage Weil  */
__destroy_snap_realm(struct ceph_mds_client * mdsc,struct ceph_snap_realm * realm)189963b61ebSSage Weil static void __destroy_snap_realm(struct ceph_mds_client *mdsc,
190963b61ebSSage Weil 				 struct ceph_snap_realm *realm)
191963b61ebSSage Weil {
192*38d46409SXiubo Li 	struct ceph_client *cl = mdsc->fsc->client;
193a6862e67SJeff Layton 	lockdep_assert_held_write(&mdsc->snap_rwsem);
194a6862e67SJeff Layton 
195*38d46409SXiubo Li 	doutc(cl, "%p %llx\n", realm, realm->ino);
196963b61ebSSage Weil 
197a105f00cSSage Weil 	rb_erase(&realm->node, &mdsc->snap_realms);
19881c5a148SYan, Zheng 	mdsc->num_snap_realms--;
199963b61ebSSage Weil 
200963b61ebSSage Weil 	if (realm->parent) {
201963b61ebSSage Weil 		list_del_init(&realm->child_item);
202963b61ebSSage Weil 		__put_snap_realm(mdsc, realm->parent);
203963b61ebSSage Weil 	}
204963b61ebSSage Weil 
205963b61ebSSage Weil 	kfree(realm->prior_parent_snaps);
206963b61ebSSage Weil 	kfree(realm->snaps);
207963b61ebSSage Weil 	ceph_put_snap_context(realm->cached_context);
208963b61ebSSage Weil 	kfree(realm);
209963b61ebSSage Weil }
210963b61ebSSage Weil 
211963b61ebSSage Weil /*
212963b61ebSSage Weil  * caller holds snap_rwsem (write)
213963b61ebSSage Weil  */
__put_snap_realm(struct ceph_mds_client * mdsc,struct ceph_snap_realm * realm)214963b61ebSSage Weil static void __put_snap_realm(struct ceph_mds_client *mdsc,
215963b61ebSSage Weil 			     struct ceph_snap_realm *realm)
216963b61ebSSage Weil {
217a6862e67SJeff Layton 	lockdep_assert_held_write(&mdsc->snap_rwsem);
218a6862e67SJeff Layton 
2198434ffe7SJeff Layton 	/*
2208434ffe7SJeff Layton 	 * We do not require the snap_empty_lock here, as any caller that
2218434ffe7SJeff Layton 	 * increments the value must hold the snap_rwsem.
2228434ffe7SJeff Layton 	 */
223963b61ebSSage Weil 	if (atomic_dec_and_test(&realm->nref))
224963b61ebSSage Weil 		__destroy_snap_realm(mdsc, realm);
225963b61ebSSage Weil }
226963b61ebSSage Weil 
227963b61ebSSage Weil /*
2288434ffe7SJeff Layton  * See comments in ceph_get_snap_realm. Caller needn't hold any locks.
229963b61ebSSage Weil  */
ceph_put_snap_realm(struct ceph_mds_client * mdsc,struct ceph_snap_realm * realm)230963b61ebSSage Weil void ceph_put_snap_realm(struct ceph_mds_client *mdsc,
231963b61ebSSage Weil 			 struct ceph_snap_realm *realm)
232963b61ebSSage Weil {
2338434ffe7SJeff Layton 	if (!atomic_dec_and_lock(&realm->nref, &mdsc->snap_empty_lock))
234963b61ebSSage Weil 		return;
235963b61ebSSage Weil 
236963b61ebSSage Weil 	if (down_write_trylock(&mdsc->snap_rwsem)) {
2378434ffe7SJeff Layton 		spin_unlock(&mdsc->snap_empty_lock);
238963b61ebSSage Weil 		__destroy_snap_realm(mdsc, realm);
239963b61ebSSage Weil 		up_write(&mdsc->snap_rwsem);
240963b61ebSSage Weil 	} else {
241a26a185dSHenry C Chang 		list_add(&realm->empty_item, &mdsc->snap_empty);
242963b61ebSSage Weil 		spin_unlock(&mdsc->snap_empty_lock);
243963b61ebSSage Weil 	}
244963b61ebSSage Weil }
245963b61ebSSage Weil 
246963b61ebSSage Weil /*
247963b61ebSSage Weil  * Clean up any realms whose ref counts have dropped to zero.  Note
248963b61ebSSage Weil  * that this does not include realms who were created but not yet
249963b61ebSSage Weil  * used.
250963b61ebSSage Weil  *
251963b61ebSSage Weil  * Called under snap_rwsem (write)
252963b61ebSSage Weil  */
__cleanup_empty_realms(struct ceph_mds_client * mdsc)253963b61ebSSage Weil static void __cleanup_empty_realms(struct ceph_mds_client *mdsc)
254963b61ebSSage Weil {
255963b61ebSSage Weil 	struct ceph_snap_realm *realm;
256963b61ebSSage Weil 
257a6862e67SJeff Layton 	lockdep_assert_held_write(&mdsc->snap_rwsem);
258a6862e67SJeff Layton 
259963b61ebSSage Weil 	spin_lock(&mdsc->snap_empty_lock);
260963b61ebSSage Weil 	while (!list_empty(&mdsc->snap_empty)) {
261963b61ebSSage Weil 		realm = list_first_entry(&mdsc->snap_empty,
262963b61ebSSage Weil 				   struct ceph_snap_realm, empty_item);
263963b61ebSSage Weil 		list_del(&realm->empty_item);
264963b61ebSSage Weil 		spin_unlock(&mdsc->snap_empty_lock);
265963b61ebSSage Weil 		__destroy_snap_realm(mdsc, realm);
266963b61ebSSage Weil 		spin_lock(&mdsc->snap_empty_lock);
267963b61ebSSage Weil 	}
268963b61ebSSage Weil 	spin_unlock(&mdsc->snap_empty_lock);
269963b61ebSSage Weil }
270963b61ebSSage Weil 
ceph_cleanup_global_and_empty_realms(struct ceph_mds_client * mdsc)2715ed91587SXiubo Li void ceph_cleanup_global_and_empty_realms(struct ceph_mds_client *mdsc)
272963b61ebSSage Weil {
2735ed91587SXiubo Li 	struct ceph_snap_realm *global_realm;
2745ed91587SXiubo Li 
275963b61ebSSage Weil 	down_write(&mdsc->snap_rwsem);
2765ed91587SXiubo Li 	global_realm = __lookup_snap_realm(mdsc, CEPH_INO_GLOBAL_SNAPREALM);
2775ed91587SXiubo Li 	if (global_realm)
2785ed91587SXiubo Li 		ceph_put_snap_realm(mdsc, global_realm);
279963b61ebSSage Weil 	__cleanup_empty_realms(mdsc);
280963b61ebSSage Weil 	up_write(&mdsc->snap_rwsem);
281963b61ebSSage Weil }
282963b61ebSSage Weil 
283963b61ebSSage Weil /*
284963b61ebSSage Weil  * adjust the parent realm of a given @realm.  adjust child list, and parent
285963b61ebSSage Weil  * pointers, and ref counts appropriately.
286963b61ebSSage Weil  *
287963b61ebSSage Weil  * return true if parent was changed, 0 if unchanged, <0 on error.
288963b61ebSSage Weil  *
289963b61ebSSage Weil  * caller must hold snap_rwsem for write.
290963b61ebSSage Weil  */
adjust_snap_realm_parent(struct ceph_mds_client * mdsc,struct ceph_snap_realm * realm,u64 parentino)291963b61ebSSage Weil static int adjust_snap_realm_parent(struct ceph_mds_client *mdsc,
292963b61ebSSage Weil 				    struct ceph_snap_realm *realm,
293963b61ebSSage Weil 				    u64 parentino)
294963b61ebSSage Weil {
295*38d46409SXiubo Li 	struct ceph_client *cl = mdsc->fsc->client;
296963b61ebSSage Weil 	struct ceph_snap_realm *parent;
297963b61ebSSage Weil 
298a6862e67SJeff Layton 	lockdep_assert_held_write(&mdsc->snap_rwsem);
299a6862e67SJeff Layton 
300963b61ebSSage Weil 	if (realm->parent_ino == parentino)
301963b61ebSSage Weil 		return 0;
302963b61ebSSage Weil 
303963b61ebSSage Weil 	parent = ceph_lookup_snap_realm(mdsc, parentino);
304963b61ebSSage Weil 	if (!parent) {
305963b61ebSSage Weil 		parent = ceph_create_snap_realm(mdsc, parentino);
306963b61ebSSage Weil 		if (IS_ERR(parent))
307963b61ebSSage Weil 			return PTR_ERR(parent);
308963b61ebSSage Weil 	}
309*38d46409SXiubo Li 	doutc(cl, "%llx %p: %llx %p -> %llx %p\n", realm->ino, realm,
310*38d46409SXiubo Li 	      realm->parent_ino, realm->parent, parentino, parent);
311963b61ebSSage Weil 	if (realm->parent) {
312963b61ebSSage Weil 		list_del_init(&realm->child_item);
313963b61ebSSage Weil 		ceph_put_snap_realm(mdsc, realm->parent);
314963b61ebSSage Weil 	}
315963b61ebSSage Weil 	realm->parent_ino = parentino;
316963b61ebSSage Weil 	realm->parent = parent;
317963b61ebSSage Weil 	list_add(&realm->child_item, &parent->children);
318963b61ebSSage Weil 	return 1;
319963b61ebSSage Weil }
320963b61ebSSage Weil 
321963b61ebSSage Weil 
cmpu64_rev(const void * a,const void * b)322963b61ebSSage Weil static int cmpu64_rev(const void *a, const void *b)
323963b61ebSSage Weil {
324963b61ebSSage Weil 	if (*(u64 *)a < *(u64 *)b)
325963b61ebSSage Weil 		return 1;
326963b61ebSSage Weil 	if (*(u64 *)a > *(u64 *)b)
327963b61ebSSage Weil 		return -1;
328963b61ebSSage Weil 	return 0;
329963b61ebSSage Weil }
330963b61ebSSage Weil 
33197c85a82SYan, Zheng 
332963b61ebSSage Weil /*
333963b61ebSSage Weil  * build the snap context for a given realm.
334963b61ebSSage Weil  */
build_snap_context(struct ceph_mds_client * mdsc,struct ceph_snap_realm * realm,struct list_head * realm_queue,struct list_head * dirty_realms)335197b7d79SXiubo Li static int build_snap_context(struct ceph_mds_client *mdsc,
336197b7d79SXiubo Li 			      struct ceph_snap_realm *realm,
33774a31df4SXiubo Li 			      struct list_head *realm_queue,
3383ae0bebcSYan, Zheng 			      struct list_head *dirty_realms)
339963b61ebSSage Weil {
340*38d46409SXiubo Li 	struct ceph_client *cl = mdsc->fsc->client;
341963b61ebSSage Weil 	struct ceph_snap_realm *parent = realm->parent;
342963b61ebSSage Weil 	struct ceph_snap_context *snapc;
343963b61ebSSage Weil 	int err = 0;
344aa711ee3SAlex Elder 	u32 num = realm->num_prior_parent_snaps + realm->num_snaps;
345963b61ebSSage Weil 
346963b61ebSSage Weil 	/*
347963b61ebSSage Weil 	 * build parent context, if it hasn't been built.
348963b61ebSSage Weil 	 * conservatively estimate that all parent snaps might be
349963b61ebSSage Weil 	 * included by us.
350963b61ebSSage Weil 	 */
351963b61ebSSage Weil 	if (parent) {
352963b61ebSSage Weil 		if (!parent->cached_context) {
35374a31df4SXiubo Li 			/* add to the queue head */
35474a31df4SXiubo Li 			list_add(&parent->rebuild_item, realm_queue);
35574a31df4SXiubo Li 			return 1;
356963b61ebSSage Weil 		}
357963b61ebSSage Weil 		num += parent->cached_context->num_snaps;
358963b61ebSSage Weil 	}
359963b61ebSSage Weil 
360963b61ebSSage Weil 	/* do i actually need to update?  not if my context seq
361963b61ebSSage Weil 	   matches realm seq, and my parents' does to.  (this works
362963b61ebSSage Weil 	   because we rebuild_snap_realms() works _downward_ in
363963b61ebSSage Weil 	   hierarchy after each update.) */
364963b61ebSSage Weil 	if (realm->cached_context &&
365ec4318bcSSage Weil 	    realm->cached_context->seq == realm->seq &&
366963b61ebSSage Weil 	    (!parent ||
367ec4318bcSSage Weil 	     realm->cached_context->seq >= parent->cached_context->seq)) {
368*38d46409SXiubo Li 		doutc(cl, "%llx %p: %p seq %lld (%u snaps) (unchanged)\n",
369*38d46409SXiubo Li 		      realm->ino, realm, realm->cached_context,
370963b61ebSSage Weil 		      realm->cached_context->seq,
371aa711ee3SAlex Elder 		      (unsigned int)realm->cached_context->num_snaps);
372963b61ebSSage Weil 		return 0;
373963b61ebSSage Weil 	}
374963b61ebSSage Weil 
375963b61ebSSage Weil 	/* alloc new snap context */
376963b61ebSSage Weil 	err = -ENOMEM;
377a3860c1cSXi Wang 	if (num > (SIZE_MAX - sizeof(*snapc)) / sizeof(u64))
378963b61ebSSage Weil 		goto fail;
379812164f8SAlex Elder 	snapc = ceph_create_snap_context(num, GFP_NOFS);
380963b61ebSSage Weil 	if (!snapc)
381963b61ebSSage Weil 		goto fail;
382963b61ebSSage Weil 
383963b61ebSSage Weil 	/* build (reverse sorted) snap vector */
384963b61ebSSage Weil 	num = 0;
385963b61ebSSage Weil 	snapc->seq = realm->seq;
386963b61ebSSage Weil 	if (parent) {
387aa711ee3SAlex Elder 		u32 i;
388aa711ee3SAlex Elder 
38925985edcSLucas De Marchi 		/* include any of parent's snaps occurring _after_ my
390963b61ebSSage Weil 		   parent became my parent */
391963b61ebSSage Weil 		for (i = 0; i < parent->cached_context->num_snaps; i++)
392963b61ebSSage Weil 			if (parent->cached_context->snaps[i] >=
393963b61ebSSage Weil 			    realm->parent_since)
394963b61ebSSage Weil 				snapc->snaps[num++] =
395963b61ebSSage Weil 					parent->cached_context->snaps[i];
396963b61ebSSage Weil 		if (parent->cached_context->seq > snapc->seq)
397963b61ebSSage Weil 			snapc->seq = parent->cached_context->seq;
398963b61ebSSage Weil 	}
399963b61ebSSage Weil 	memcpy(snapc->snaps + num, realm->snaps,
400963b61ebSSage Weil 	       sizeof(u64)*realm->num_snaps);
401963b61ebSSage Weil 	num += realm->num_snaps;
402963b61ebSSage Weil 	memcpy(snapc->snaps + num, realm->prior_parent_snaps,
403963b61ebSSage Weil 	       sizeof(u64)*realm->num_prior_parent_snaps);
404963b61ebSSage Weil 	num += realm->num_prior_parent_snaps;
405963b61ebSSage Weil 
406963b61ebSSage Weil 	sort(snapc->snaps, num, sizeof(u64), cmpu64_rev, NULL);
407963b61ebSSage Weil 	snapc->num_snaps = num;
408*38d46409SXiubo Li 	doutc(cl, "%llx %p: %p seq %lld (%u snaps)\n", realm->ino, realm,
409*38d46409SXiubo Li 	      snapc, snapc->seq, (unsigned int) snapc->num_snaps);
410963b61ebSSage Weil 
411963b61ebSSage Weil 	ceph_put_snap_context(realm->cached_context);
4129f4057fcSYan, Zheng 	realm->cached_context = snapc;
4133ae0bebcSYan, Zheng 	/* queue realm for cap_snap creation */
4143ae0bebcSYan, Zheng 	list_add_tail(&realm->dirty_item, dirty_realms);
415963b61ebSSage Weil 	return 0;
416963b61ebSSage Weil 
417963b61ebSSage Weil fail:
418963b61ebSSage Weil 	/*
419963b61ebSSage Weil 	 * if we fail, clear old (incorrect) cached_context... hopefully
420963b61ebSSage Weil 	 * we'll have better luck building it later
421963b61ebSSage Weil 	 */
422963b61ebSSage Weil 	if (realm->cached_context) {
423963b61ebSSage Weil 		ceph_put_snap_context(realm->cached_context);
424963b61ebSSage Weil 		realm->cached_context = NULL;
425963b61ebSSage Weil 	}
426*38d46409SXiubo Li 	pr_err_client(cl, "%llx %p fail %d\n", realm->ino, realm, err);
427963b61ebSSage Weil 	return err;
428963b61ebSSage Weil }
429963b61ebSSage Weil 
430963b61ebSSage Weil /*
431963b61ebSSage Weil  * rebuild snap context for the given realm and all of its children.
432963b61ebSSage Weil  */
rebuild_snap_realms(struct ceph_mds_client * mdsc,struct ceph_snap_realm * realm,struct list_head * dirty_realms)433197b7d79SXiubo Li static void rebuild_snap_realms(struct ceph_mds_client *mdsc,
434197b7d79SXiubo Li 				struct ceph_snap_realm *realm,
4353ae0bebcSYan, Zheng 				struct list_head *dirty_realms)
436963b61ebSSage Weil {
437*38d46409SXiubo Li 	struct ceph_client *cl = mdsc->fsc->client;
43874a31df4SXiubo Li 	LIST_HEAD(realm_queue);
43974a31df4SXiubo Li 	int last = 0;
44074a31df4SXiubo Li 	bool skip = false;
441963b61ebSSage Weil 
44274a31df4SXiubo Li 	list_add_tail(&realm->rebuild_item, &realm_queue);
443963b61ebSSage Weil 
44474a31df4SXiubo Li 	while (!list_empty(&realm_queue)) {
44574a31df4SXiubo Li 		struct ceph_snap_realm *_realm, *child;
44674a31df4SXiubo Li 
44774a31df4SXiubo Li 		_realm = list_first_entry(&realm_queue,
44874a31df4SXiubo Li 					  struct ceph_snap_realm,
44974a31df4SXiubo Li 					  rebuild_item);
45074a31df4SXiubo Li 
45174a31df4SXiubo Li 		/*
45274a31df4SXiubo Li 		 * If the last building failed dues to memory
45374a31df4SXiubo Li 		 * issue, just empty the realm_queue and return
45474a31df4SXiubo Li 		 * to avoid infinite loop.
45574a31df4SXiubo Li 		 */
45674a31df4SXiubo Li 		if (last < 0) {
45774a31df4SXiubo Li 			list_del_init(&_realm->rebuild_item);
45874a31df4SXiubo Li 			continue;
45974a31df4SXiubo Li 		}
46074a31df4SXiubo Li 
461197b7d79SXiubo Li 		last = build_snap_context(mdsc, _realm, &realm_queue,
462197b7d79SXiubo Li 					  dirty_realms);
463*38d46409SXiubo Li 		doutc(cl, "%llx %p, %s\n", realm->ino, realm,
46474a31df4SXiubo Li 		      last > 0 ? "is deferred" : !last ? "succeeded" : "failed");
46574a31df4SXiubo Li 
46674a31df4SXiubo Li 		/* is any child in the list ? */
46774a31df4SXiubo Li 		list_for_each_entry(child, &_realm->children, child_item) {
46874a31df4SXiubo Li 			if (!list_empty(&child->rebuild_item)) {
46974a31df4SXiubo Li 				skip = true;
47074a31df4SXiubo Li 				break;
47174a31df4SXiubo Li 			}
47274a31df4SXiubo Li 		}
47374a31df4SXiubo Li 
47474a31df4SXiubo Li 		if (!skip) {
47574a31df4SXiubo Li 			list_for_each_entry(child, &_realm->children, child_item)
47674a31df4SXiubo Li 				list_add_tail(&child->rebuild_item, &realm_queue);
47774a31df4SXiubo Li 		}
47874a31df4SXiubo Li 
47974a31df4SXiubo Li 		/* last == 1 means need to build parent first */
48074a31df4SXiubo Li 		if (last <= 0)
48174a31df4SXiubo Li 			list_del_init(&_realm->rebuild_item);
48274a31df4SXiubo Li 	}
483963b61ebSSage Weil }
484963b61ebSSage Weil 
485963b61ebSSage Weil 
486963b61ebSSage Weil /*
487963b61ebSSage Weil  * helper to allocate and decode an array of snapids.  free prior
488963b61ebSSage Weil  * instance, if any.
489963b61ebSSage Weil  */
dup_array(u64 ** dst,__le64 * src,u32 num)490aa711ee3SAlex Elder static int dup_array(u64 **dst, __le64 *src, u32 num)
491963b61ebSSage Weil {
492aa711ee3SAlex Elder 	u32 i;
493963b61ebSSage Weil 
494963b61ebSSage Weil 	kfree(*dst);
495963b61ebSSage Weil 	if (num) {
496963b61ebSSage Weil 		*dst = kcalloc(num, sizeof(u64), GFP_NOFS);
497963b61ebSSage Weil 		if (!*dst)
498963b61ebSSage Weil 			return -ENOMEM;
499963b61ebSSage Weil 		for (i = 0; i < num; i++)
500963b61ebSSage Weil 			(*dst)[i] = get_unaligned_le64(src + i);
501963b61ebSSage Weil 	} else {
502963b61ebSSage Weil 		*dst = NULL;
503963b61ebSSage Weil 	}
504963b61ebSSage Weil 	return 0;
505963b61ebSSage Weil }
506963b61ebSSage Weil 
has_new_snaps(struct ceph_snap_context * o,struct ceph_snap_context * n)50786056090SYan, Zheng static bool has_new_snaps(struct ceph_snap_context *o,
50886056090SYan, Zheng 			  struct ceph_snap_context *n)
50986056090SYan, Zheng {
51086056090SYan, Zheng 	if (n->num_snaps == 0)
51186056090SYan, Zheng 		return false;
51286056090SYan, Zheng 	/* snaps are in descending order */
51386056090SYan, Zheng 	return n->snaps[0] > o->seq;
51486056090SYan, Zheng }
515963b61ebSSage Weil 
516963b61ebSSage Weil /*
517963b61ebSSage Weil  * When a snapshot is applied, the size/mtime inode metadata is queued
518963b61ebSSage Weil  * in a ceph_cap_snap (one for each snapshot) until writeback
519963b61ebSSage Weil  * completes and the metadata can be flushed back to the MDS.
520963b61ebSSage Weil  *
521963b61ebSSage Weil  * However, if a (sync) write is currently in-progress when we apply
522963b61ebSSage Weil  * the snapshot, we have to wait until the write succeeds or fails
523963b61ebSSage Weil  * (and a final size/mtime is known).  In this case the
524963b61ebSSage Weil  * cap_snap->writing = 1, and is said to be "pending."  When the write
525963b61ebSSage Weil  * finishes, we __ceph_finish_cap_snap().
526963b61ebSSage Weil  *
527963b61ebSSage Weil  * Caller must hold snap_rwsem for read (i.e., the realm topology won't
528963b61ebSSage Weil  * change).
529963b61ebSSage Weil  */
ceph_queue_cap_snap(struct ceph_inode_info * ci,struct ceph_cap_snap ** pcapsnap)5301ab36c9dSXiubo Li static void ceph_queue_cap_snap(struct ceph_inode_info *ci,
5311ab36c9dSXiubo Li 				struct ceph_cap_snap **pcapsnap)
532963b61ebSSage Weil {
533874c8ca1SDavid Howells 	struct inode *inode = &ci->netfs.inode;
534*38d46409SXiubo Li 	struct ceph_client *cl = ceph_inode_to_client(inode);
53586056090SYan, Zheng 	struct ceph_snap_context *old_snapc, *new_snapc;
5361ab36c9dSXiubo Li 	struct ceph_cap_snap *capsnap = *pcapsnap;
53712fe3ddaSLuis Henriques 	struct ceph_buffer *old_blob = NULL;
5384a625be4SSage Weil 	int used, dirty;
539963b61ebSSage Weil 
540be655596SSage Weil 	spin_lock(&ci->i_ceph_lock);
541963b61ebSSage Weil 	used = __ceph_caps_used(ci);
5424a625be4SSage Weil 	dirty = __ceph_caps_dirty(ci);
543af0ed569SSage Weil 
5445dda377cSYan, Zheng 	old_snapc = ci->i_head_snapc;
54586056090SYan, Zheng 	new_snapc = ci->i_snap_realm->cached_context;
5465dda377cSYan, Zheng 
547af0ed569SSage Weil 	/*
548af0ed569SSage Weil 	 * If there is a write in progress, treat that as a dirty Fw,
549af0ed569SSage Weil 	 * even though it hasn't completed yet; by the time we finish
550af0ed569SSage Weil 	 * up this capsnap it will be.
551af0ed569SSage Weil 	 */
552af0ed569SSage Weil 	if (used & CEPH_CAP_FILE_WR)
553af0ed569SSage Weil 		dirty |= CEPH_CAP_FILE_WR;
554af0ed569SSage Weil 
555963b61ebSSage Weil 	if (__ceph_have_pending_cap_snap(ci)) {
556963b61ebSSage Weil 		/* there is no point in queuing multiple "pending" cap_snaps,
557963b61ebSSage Weil 		   as no new writes are allowed to start when pending, so any
558963b61ebSSage Weil 		   writes in progress now were started before the previous
559963b61ebSSage Weil 		   cap_snap.  lucky us. */
560*38d46409SXiubo Li 		doutc(cl, "%p %llx.%llx already pending\n", inode,
561*38d46409SXiubo Li 		      ceph_vinop(inode));
5625dda377cSYan, Zheng 		goto update_snapc;
5635dda377cSYan, Zheng 	}
56486056090SYan, Zheng 	if (ci->i_wrbuffer_ref_head == 0 &&
56586056090SYan, Zheng 	    !(dirty & (CEPH_CAP_ANY_EXCL|CEPH_CAP_FILE_WR))) {
566*38d46409SXiubo Li 		doutc(cl, "%p %llx.%llx nothing dirty|writing\n", inode,
567*38d46409SXiubo Li 		      ceph_vinop(inode));
5685dda377cSYan, Zheng 		goto update_snapc;
5695dda377cSYan, Zheng 	}
570fc837c8fSSage Weil 
5715dda377cSYan, Zheng 	BUG_ON(!old_snapc);
572af0ed569SSage Weil 
57386056090SYan, Zheng 	/*
57486056090SYan, Zheng 	 * There is no need to send FLUSHSNAP message to MDS if there is
57586056090SYan, Zheng 	 * no new snapshot. But when there is dirty pages or on-going
57686056090SYan, Zheng 	 * writes, we still need to create cap_snap. cap_snap is needed
57786056090SYan, Zheng 	 * by the write path and page writeback path.
57886056090SYan, Zheng 	 *
57986056090SYan, Zheng 	 * also see ceph_try_drop_cap_snap()
58086056090SYan, Zheng 	 */
58186056090SYan, Zheng 	if (has_new_snaps(old_snapc, new_snapc)) {
58286056090SYan, Zheng 		if (dirty & (CEPH_CAP_ANY_EXCL|CEPH_CAP_FILE_WR))
58386056090SYan, Zheng 			capsnap->need_flush = true;
58486056090SYan, Zheng 	} else {
58586056090SYan, Zheng 		if (!(used & CEPH_CAP_FILE_WR) &&
58686056090SYan, Zheng 		    ci->i_wrbuffer_ref_head == 0) {
587*38d46409SXiubo Li 			doutc(cl, "%p %llx.%llx no new_snap|dirty_page|writing\n",
588*38d46409SXiubo Li 			      inode, ceph_vinop(inode));
58986056090SYan, Zheng 			goto update_snapc;
59086056090SYan, Zheng 		}
59186056090SYan, Zheng 	}
59286056090SYan, Zheng 
593*38d46409SXiubo Li 	doutc(cl, "%p %llx.%llx cap_snap %p queuing under %p %s %s\n",
594*38d46409SXiubo Li 	      inode, ceph_vinop(inode), capsnap, old_snapc,
595ad5255c1SXiubo Li 	      ceph_cap_string(dirty), capsnap->need_flush ? "" : "no_flush");
5960444d76aSDave Chinner 	ihold(inode);
597963b61ebSSage Weil 
5985dda377cSYan, Zheng 	capsnap->follows = old_snapc->seq;
599963b61ebSSage Weil 	capsnap->issued = __ceph_caps_issued(ci, NULL);
6004a625be4SSage Weil 	capsnap->dirty = dirty;
601963b61ebSSage Weil 
602963b61ebSSage Weil 	capsnap->mode = inode->i_mode;
603963b61ebSSage Weil 	capsnap->uid = inode->i_uid;
604963b61ebSSage Weil 	capsnap->gid = inode->i_gid;
605963b61ebSSage Weil 
6064a625be4SSage Weil 	if (dirty & CEPH_CAP_XATTR_EXCL) {
60712fe3ddaSLuis Henriques 		old_blob = __ceph_build_xattrs_blob(ci);
6084a625be4SSage Weil 		capsnap->xattr_blob =
6094a625be4SSage Weil 			ceph_buffer_get(ci->i_xattrs.blob);
6104a625be4SSage Weil 		capsnap->xattr_version = ci->i_xattrs.version;
6114a625be4SSage Weil 	} else {
612963b61ebSSage Weil 		capsnap->xattr_blob = NULL;
6134a625be4SSage Weil 		capsnap->xattr_version = 0;
6144a625be4SSage Weil 	}
615963b61ebSSage Weil 
616e20d258dSYan, Zheng 	capsnap->inline_data = ci->i_inline_version != CEPH_INLINE_NONE;
617e20d258dSYan, Zheng 
618963b61ebSSage Weil 	/* dirty page count moved from _head to this cap_snap;
619963b61ebSSage Weil 	   all subsequent writes page dirties occur _after_ this
620963b61ebSSage Weil 	   snapshot. */
621963b61ebSSage Weil 	capsnap->dirty_pages = ci->i_wrbuffer_ref_head;
622963b61ebSSage Weil 	ci->i_wrbuffer_ref_head = 0;
6235dda377cSYan, Zheng 	capsnap->context = old_snapc;
624963b61ebSSage Weil 	list_add_tail(&capsnap->ci_item, &ci->i_cap_snaps);
625963b61ebSSage Weil 
626963b61ebSSage Weil 	if (used & CEPH_CAP_FILE_WR) {
627*38d46409SXiubo Li 		doutc(cl, "%p %llx.%llx cap_snap %p snapc %p seq %llu used WR,"
628*38d46409SXiubo Li 		      " now pending\n", inode, ceph_vinop(inode), capsnap,
629*38d46409SXiubo Li 		      old_snapc, old_snapc->seq);
630963b61ebSSage Weil 		capsnap->writing = 1;
631963b61ebSSage Weil 	} else {
632963b61ebSSage Weil 		/* note mtime, size NOW. */
633963b61ebSSage Weil 		__ceph_finish_cap_snap(ci, capsnap);
634963b61ebSSage Weil 	}
6351ab36c9dSXiubo Li 	*pcapsnap = NULL;
636fce85157SYan, Zheng 	old_snapc = NULL;
637963b61ebSSage Weil 
6385dda377cSYan, Zheng update_snapc:
63937659182SYan, Zheng 	if (ci->i_wrbuffer_ref_head == 0 &&
64037659182SYan, Zheng 	    ci->i_wr_ref == 0 &&
64137659182SYan, Zheng 	    ci->i_dirty_caps == 0 &&
64237659182SYan, Zheng 	    ci->i_flushing_caps == 0) {
64337659182SYan, Zheng 		ci->i_head_snapc = NULL;
64437659182SYan, Zheng 	} else {
64586056090SYan, Zheng 		ci->i_head_snapc = ceph_get_snap_context(new_snapc);
646*38d46409SXiubo Li 		doutc(cl, " new snapc is %p\n", new_snapc);
6475dda377cSYan, Zheng 	}
648be655596SSage Weil 	spin_unlock(&ci->i_ceph_lock);
6495dda377cSYan, Zheng 
65012fe3ddaSLuis Henriques 	ceph_buffer_put(old_blob);
6515dda377cSYan, Zheng 	ceph_put_snap_context(old_snapc);
652963b61ebSSage Weil }
653963b61ebSSage Weil 
654963b61ebSSage Weil /*
655963b61ebSSage Weil  * Finalize the size, mtime for a cap_snap.. that is, settle on final values
656963b61ebSSage Weil  * to be used for the snapshot, to be flushed back to the mds.
657963b61ebSSage Weil  *
658963b61ebSSage Weil  * If capsnap can now be flushed, add to snap_flush list, and return 1.
659963b61ebSSage Weil  *
660be655596SSage Weil  * Caller must hold i_ceph_lock.
661963b61ebSSage Weil  */
__ceph_finish_cap_snap(struct ceph_inode_info * ci,struct ceph_cap_snap * capsnap)662963b61ebSSage Weil int __ceph_finish_cap_snap(struct ceph_inode_info *ci,
663963b61ebSSage Weil 			    struct ceph_cap_snap *capsnap)
664963b61ebSSage Weil {
665874c8ca1SDavid Howells 	struct inode *inode = &ci->netfs.inode;
6662678da88SXiubo Li 	struct ceph_mds_client *mdsc = ceph_sb_to_mdsc(inode->i_sb);
667*38d46409SXiubo Li 	struct ceph_client *cl = mdsc->fsc->client;
668963b61ebSSage Weil 
669963b61ebSSage Weil 	BUG_ON(capsnap->writing);
6702d6795fbSJeff Layton 	capsnap->size = i_size_read(inode);
6719bbeab41SArnd Bergmann 	capsnap->mtime = inode_get_mtime(inode);
6729bbeab41SArnd Bergmann 	capsnap->atime = inode_get_atime(inode);
6737795aef0SJeff Layton 	capsnap->ctime = inode_get_ctime(inode);
674ec62b894SJeff Layton 	capsnap->btime = ci->i_btime;
675176c77c9SJeff Layton 	capsnap->change_attr = inode_peek_iversion_raw(inode);
676963b61ebSSage Weil 	capsnap->time_warp_seq = ci->i_time_warp_seq;
6775f743e45SYan, Zheng 	capsnap->truncate_size = ci->i_truncate_size;
6785f743e45SYan, Zheng 	capsnap->truncate_seq = ci->i_truncate_seq;
679963b61ebSSage Weil 	if (capsnap->dirty_pages) {
680*38d46409SXiubo Li 		doutc(cl, "%p %llx.%llx cap_snap %p snapc %p %llu %s "
681*38d46409SXiubo Li 		      "s=%llu still has %d dirty pages\n", inode,
682ad5255c1SXiubo Li 		      ceph_vinop(inode), capsnap, capsnap->context,
683*38d46409SXiubo Li 		      capsnap->context->seq,
684*38d46409SXiubo Li 		      ceph_cap_string(capsnap->dirty),
685ad5255c1SXiubo Li 		      capsnap->size, capsnap->dirty_pages);
686963b61ebSSage Weil 		return 0;
687963b61ebSSage Weil 	}
68870220ac8SYan, Zheng 
6892d12ad95SXiubo Li 	/*
6902d12ad95SXiubo Li 	 * Defer flushing the capsnap if the dirty buffer not flushed yet.
6912d12ad95SXiubo Li 	 * And trigger to flush the buffer immediately.
6922d12ad95SXiubo Li 	 */
6932d12ad95SXiubo Li 	if (ci->i_wrbuffer_ref) {
694*38d46409SXiubo Li 		doutc(cl, "%p %llx.%llx cap_snap %p snapc %p %llu %s "
695*38d46409SXiubo Li 		      "s=%llu used WRBUFFER, delaying\n", inode,
696ad5255c1SXiubo Li 		      ceph_vinop(inode), capsnap, capsnap->context,
697ad5255c1SXiubo Li 		      capsnap->context->seq, ceph_cap_string(capsnap->dirty),
698ad5255c1SXiubo Li 		      capsnap->size);
6992d12ad95SXiubo Li 		ceph_queue_writeback(inode);
700558b4510SXiubo Li 		return 0;
701558b4510SXiubo Li 	}
702558b4510SXiubo Li 
70370220ac8SYan, Zheng 	ci->i_ceph_flags |= CEPH_I_FLUSH_SNAPS;
704*38d46409SXiubo Li 	doutc(cl, "%p %llx.%llx cap_snap %p snapc %p %llu %s s=%llu\n",
705*38d46409SXiubo Li 	      inode, ceph_vinop(inode), capsnap, capsnap->context,
706819ccbfaSSage Weil 	      capsnap->context->seq, ceph_cap_string(capsnap->dirty),
707819ccbfaSSage Weil 	      capsnap->size);
708963b61ebSSage Weil 
709963b61ebSSage Weil 	spin_lock(&mdsc->snap_flush_lock);
710409e873eSXiubo Li 	if (list_empty(&ci->i_snap_flush_item)) {
711409e873eSXiubo Li 		ihold(inode);
712963b61ebSSage Weil 		list_add_tail(&ci->i_snap_flush_item, &mdsc->snap_flush_list);
713409e873eSXiubo Li 	}
714963b61ebSSage Weil 	spin_unlock(&mdsc->snap_flush_lock);
715963b61ebSSage Weil 	return 1;  /* caller may want to ceph_flush_snaps */
716963b61ebSSage Weil }
717963b61ebSSage Weil 
718ed326044SSage Weil /*
719ed326044SSage Weil  * Queue cap_snaps for snap writeback for this realm and its children.
720ed326044SSage Weil  * Called under snap_rwsem, so realm topology won't change.
721ed326044SSage Weil  */
queue_realm_cap_snaps(struct ceph_mds_client * mdsc,struct ceph_snap_realm * realm)722197b7d79SXiubo Li static void queue_realm_cap_snaps(struct ceph_mds_client *mdsc,
723197b7d79SXiubo Li 				  struct ceph_snap_realm *realm)
724ed326044SSage Weil {
725*38d46409SXiubo Li 	struct ceph_client *cl = mdsc->fsc->client;
726ed326044SSage Weil 	struct ceph_inode_info *ci;
727ed326044SSage Weil 	struct inode *lastinode = NULL;
7281ab36c9dSXiubo Li 	struct ceph_cap_snap *capsnap = NULL;
729ed326044SSage Weil 
730*38d46409SXiubo Li 	doutc(cl, "%p %llx inode\n", realm, realm->ino);
731ed326044SSage Weil 
732ed326044SSage Weil 	spin_lock(&realm->inodes_with_caps_lock);
7333ae0bebcSYan, Zheng 	list_for_each_entry(ci, &realm->inodes_with_caps, i_snap_realm_item) {
734874c8ca1SDavid Howells 		struct inode *inode = igrab(&ci->netfs.inode);
735ed326044SSage Weil 		if (!inode)
736ed326044SSage Weil 			continue;
737ed326044SSage Weil 		spin_unlock(&realm->inodes_with_caps_lock);
73823c2c76eSJeff Layton 		iput(lastinode);
739ed326044SSage Weil 		lastinode = inode;
7401ab36c9dSXiubo Li 
7411ab36c9dSXiubo Li 		/*
7421ab36c9dSXiubo Li 		 * Allocate the capsnap memory outside of ceph_queue_cap_snap()
7431ab36c9dSXiubo Li 		 * to reduce very possible but unnecessary frequently memory
7441ab36c9dSXiubo Li 		 * allocate/free in this loop.
7451ab36c9dSXiubo Li 		 */
7461ab36c9dSXiubo Li 		if (!capsnap) {
7471ab36c9dSXiubo Li 			capsnap = kmem_cache_zalloc(ceph_cap_snap_cachep, GFP_NOFS);
7481ab36c9dSXiubo Li 			if (!capsnap) {
749*38d46409SXiubo Li 				pr_err_client(cl,
750*38d46409SXiubo Li 					"ENOMEM allocating ceph_cap_snap on %p\n",
7511ab36c9dSXiubo Li 					inode);
7521ab36c9dSXiubo Li 				return;
7531ab36c9dSXiubo Li 			}
7541ab36c9dSXiubo Li 		}
7551ab36c9dSXiubo Li 		capsnap->cap_flush.is_capsnap = true;
7561ab36c9dSXiubo Li 		refcount_set(&capsnap->nref, 1);
7571ab36c9dSXiubo Li 		INIT_LIST_HEAD(&capsnap->cap_flush.i_list);
7581ab36c9dSXiubo Li 		INIT_LIST_HEAD(&capsnap->cap_flush.g_list);
7591ab36c9dSXiubo Li 		INIT_LIST_HEAD(&capsnap->ci_item);
7601ab36c9dSXiubo Li 
7611ab36c9dSXiubo Li 		ceph_queue_cap_snap(ci, &capsnap);
762ed326044SSage Weil 		spin_lock(&realm->inodes_with_caps_lock);
763ed326044SSage Weil 	}
764ed326044SSage Weil 	spin_unlock(&realm->inodes_with_caps_lock);
76523c2c76eSJeff Layton 	iput(lastinode);
766ed326044SSage Weil 
7671ab36c9dSXiubo Li 	if (capsnap)
7681ab36c9dSXiubo Li 		kmem_cache_free(ceph_cap_snap_cachep, capsnap);
769*38d46409SXiubo Li 	doutc(cl, "%p %llx done\n", realm, realm->ino);
770ed326044SSage Weil }
771963b61ebSSage Weil 
772963b61ebSSage Weil /*
773963b61ebSSage Weil  * Parse and apply a snapblob "snap trace" from the MDS.  This specifies
774963b61ebSSage Weil  * the snap realm parameters from a given realm and all of its ancestors,
775963b61ebSSage Weil  * up to the root.
776963b61ebSSage Weil  *
777963b61ebSSage Weil  * Caller must hold snap_rwsem for write.
778963b61ebSSage Weil  */
ceph_update_snap_trace(struct ceph_mds_client * mdsc,void * p,void * e,bool deletion,struct ceph_snap_realm ** realm_ret)779963b61ebSSage Weil int ceph_update_snap_trace(struct ceph_mds_client *mdsc,
780982d6011SYan, Zheng 			   void *p, void *e, bool deletion,
781982d6011SYan, Zheng 			   struct ceph_snap_realm **realm_ret)
782963b61ebSSage Weil {
783*38d46409SXiubo Li 	struct ceph_client *cl = mdsc->fsc->client;
784963b61ebSSage Weil 	struct ceph_mds_snap_realm *ri;    /* encoded */
785963b61ebSSage Weil 	__le64 *snaps;                     /* encoded */
786963b61ebSSage Weil 	__le64 *prior_parent_snaps;        /* encoded */
78751884d15SXiubo Li 	struct ceph_snap_realm *realm;
788982d6011SYan, Zheng 	struct ceph_snap_realm *first_realm = NULL;
7892e586641SXiubo Li 	struct ceph_snap_realm *realm_to_rebuild = NULL;
790a68e564aSXiubo Li 	struct ceph_client *client = mdsc->fsc->client;
7912e586641SXiubo Li 	int rebuild_snapcs;
792963b61ebSSage Weil 	int err = -ENOMEM;
793a68e564aSXiubo Li 	int ret;
794ae00d4f3SSage Weil 	LIST_HEAD(dirty_realms);
795963b61ebSSage Weil 
796a6862e67SJeff Layton 	lockdep_assert_held_write(&mdsc->snap_rwsem);
797a6862e67SJeff Layton 
798*38d46409SXiubo Li 	doutc(cl, "deletion=%d\n", deletion);
799963b61ebSSage Weil more:
80051884d15SXiubo Li 	realm = NULL;
8012e586641SXiubo Li 	rebuild_snapcs = 0;
802963b61ebSSage Weil 	ceph_decode_need(&p, e, sizeof(*ri), bad);
803963b61ebSSage Weil 	ri = p;
804963b61ebSSage Weil 	p += sizeof(*ri);
805963b61ebSSage Weil 	ceph_decode_need(&p, e, sizeof(u64)*(le32_to_cpu(ri->num_snaps) +
806963b61ebSSage Weil 			    le32_to_cpu(ri->num_prior_parent_snaps)), bad);
807963b61ebSSage Weil 	snaps = p;
808963b61ebSSage Weil 	p += sizeof(u64) * le32_to_cpu(ri->num_snaps);
809963b61ebSSage Weil 	prior_parent_snaps = p;
810963b61ebSSage Weil 	p += sizeof(u64) * le32_to_cpu(ri->num_prior_parent_snaps);
811963b61ebSSage Weil 
812963b61ebSSage Weil 	realm = ceph_lookup_snap_realm(mdsc, le64_to_cpu(ri->ino));
813963b61ebSSage Weil 	if (!realm) {
814963b61ebSSage Weil 		realm = ceph_create_snap_realm(mdsc, le64_to_cpu(ri->ino));
815963b61ebSSage Weil 		if (IS_ERR(realm)) {
816963b61ebSSage Weil 			err = PTR_ERR(realm);
817963b61ebSSage Weil 			goto fail;
818963b61ebSSage Weil 		}
819963b61ebSSage Weil 	}
820963b61ebSSage Weil 
821963b61ebSSage Weil 	/* ensure the parent is correct */
822963b61ebSSage Weil 	err = adjust_snap_realm_parent(mdsc, realm, le64_to_cpu(ri->parent));
823963b61ebSSage Weil 	if (err < 0)
824963b61ebSSage Weil 		goto fail;
8252e586641SXiubo Li 	rebuild_snapcs += err;
826963b61ebSSage Weil 
827963b61ebSSage Weil 	if (le64_to_cpu(ri->seq) > realm->seq) {
828*38d46409SXiubo Li 		doutc(cl, "updating %llx %p %lld -> %lld\n", realm->ino,
829*38d46409SXiubo Li 		      realm, realm->seq, le64_to_cpu(ri->seq));
830963b61ebSSage Weil 		/* update realm parameters, snap lists */
831963b61ebSSage Weil 		realm->seq = le64_to_cpu(ri->seq);
832963b61ebSSage Weil 		realm->created = le64_to_cpu(ri->created);
833963b61ebSSage Weil 		realm->parent_since = le64_to_cpu(ri->parent_since);
834963b61ebSSage Weil 
835963b61ebSSage Weil 		realm->num_snaps = le32_to_cpu(ri->num_snaps);
836963b61ebSSage Weil 		err = dup_array(&realm->snaps, snaps, realm->num_snaps);
837963b61ebSSage Weil 		if (err < 0)
838963b61ebSSage Weil 			goto fail;
839963b61ebSSage Weil 
840963b61ebSSage Weil 		realm->num_prior_parent_snaps =
841963b61ebSSage Weil 			le32_to_cpu(ri->num_prior_parent_snaps);
842963b61ebSSage Weil 		err = dup_array(&realm->prior_parent_snaps, prior_parent_snaps,
843963b61ebSSage Weil 				realm->num_prior_parent_snaps);
844963b61ebSSage Weil 		if (err < 0)
845963b61ebSSage Weil 			goto fail;
846963b61ebSSage Weil 
847affbc19aSYan, Zheng 		if (realm->seq > mdsc->last_snap_seq)
848affbc19aSYan, Zheng 			mdsc->last_snap_seq = realm->seq;
849ae00d4f3SSage Weil 
8502e586641SXiubo Li 		rebuild_snapcs = 1;
851963b61ebSSage Weil 	} else if (!realm->cached_context) {
852*38d46409SXiubo Li 		doutc(cl, "%llx %p seq %lld new\n", realm->ino, realm,
853*38d46409SXiubo Li 		      realm->seq);
8542e586641SXiubo Li 		rebuild_snapcs = 1;
855ae00d4f3SSage Weil 	} else {
856*38d46409SXiubo Li 		doutc(cl, "%llx %p seq %lld unchanged\n", realm->ino, realm,
857*38d46409SXiubo Li 		      realm->seq);
858963b61ebSSage Weil 	}
859963b61ebSSage Weil 
860*38d46409SXiubo Li 	doutc(cl, "done with %llx %p, rebuild_snapcs=%d, %p %p\n", realm->ino,
8612e586641SXiubo Li 	      realm, rebuild_snapcs, p, e);
862963b61ebSSage Weil 
8632e586641SXiubo Li 	/*
8642e586641SXiubo Li 	 * this will always track the uppest parent realm from which
8652e586641SXiubo Li 	 * we need to rebuild the snapshot contexts _downward_ in
8662e586641SXiubo Li 	 * hierarchy.
8672e586641SXiubo Li 	 */
8682e586641SXiubo Li 	if (rebuild_snapcs)
8692e586641SXiubo Li 		realm_to_rebuild = realm;
8702e586641SXiubo Li 
8712e586641SXiubo Li 	/* rebuild_snapcs when we reach the _end_ (root) of the trace */
8722e586641SXiubo Li 	if (realm_to_rebuild && p >= e)
873197b7d79SXiubo Li 		rebuild_snap_realms(mdsc, realm_to_rebuild, &dirty_realms);
874982d6011SYan, Zheng 
875982d6011SYan, Zheng 	if (!first_realm)
876982d6011SYan, Zheng 		first_realm = realm;
877982d6011SYan, Zheng 	else
878982d6011SYan, Zheng 		ceph_put_snap_realm(mdsc, realm);
879982d6011SYan, Zheng 
880963b61ebSSage Weil 	if (p < e)
881963b61ebSSage Weil 		goto more;
882963b61ebSSage Weil 
883ae00d4f3SSage Weil 	/*
884ae00d4f3SSage Weil 	 * queue cap snaps _after_ we've built the new snap contexts,
885ae00d4f3SSage Weil 	 * so that i_head_snapc can be set appropriately.
886ae00d4f3SSage Weil 	 */
887e8e1ba96SSage Weil 	while (!list_empty(&dirty_realms)) {
888e8e1ba96SSage Weil 		realm = list_first_entry(&dirty_realms, struct ceph_snap_realm,
889e8e1ba96SSage Weil 					 dirty_item);
8903ae0bebcSYan, Zheng 		list_del_init(&realm->dirty_item);
891197b7d79SXiubo Li 		queue_realm_cap_snaps(mdsc, realm);
892ae00d4f3SSage Weil 	}
893ae00d4f3SSage Weil 
894982d6011SYan, Zheng 	if (realm_ret)
895982d6011SYan, Zheng 		*realm_ret = first_realm;
896982d6011SYan, Zheng 	else
897982d6011SYan, Zheng 		ceph_put_snap_realm(mdsc, first_realm);
898982d6011SYan, Zheng 
899963b61ebSSage Weil 	__cleanup_empty_realms(mdsc);
900963b61ebSSage Weil 	return 0;
901963b61ebSSage Weil 
902963b61ebSSage Weil bad:
903f3fd3ea6SJeff Layton 	err = -EIO;
904963b61ebSSage Weil fail:
905982d6011SYan, Zheng 	if (realm && !IS_ERR(realm))
906982d6011SYan, Zheng 		ceph_put_snap_realm(mdsc, realm);
907982d6011SYan, Zheng 	if (first_realm)
908982d6011SYan, Zheng 		ceph_put_snap_realm(mdsc, first_realm);
909*38d46409SXiubo Li 	pr_err_client(cl, "error %d\n", err);
910a68e564aSXiubo Li 
911a68e564aSXiubo Li 	/*
912a68e564aSXiubo Li 	 * When receiving a corrupted snap trace we don't know what
913a68e564aSXiubo Li 	 * exactly has happened in MDS side. And we shouldn't continue
914a68e564aSXiubo Li 	 * writing to OSD, which may corrupt the snapshot contents.
915a68e564aSXiubo Li 	 *
916a68e564aSXiubo Li 	 * Just try to blocklist this kclient and then this kclient
917a68e564aSXiubo Li 	 * must be remounted to continue after the corrupted metadata
918a68e564aSXiubo Li 	 * fixed in the MDS side.
919a68e564aSXiubo Li 	 */
920a68e564aSXiubo Li 	WRITE_ONCE(mdsc->fsc->mount_state, CEPH_MOUNT_FENCE_IO);
921a68e564aSXiubo Li 	ret = ceph_monc_blocklist_add(&client->monc, &client->msgr.inst.addr);
922a68e564aSXiubo Li 	if (ret)
923*38d46409SXiubo Li 		pr_err_client(cl, "failed to blocklist %s: %d\n",
924a68e564aSXiubo Li 			      ceph_pr_addr(&client->msgr.inst.addr), ret);
925a68e564aSXiubo Li 
926*38d46409SXiubo Li 	WARN(1, "[client.%lld] %s %s%sdo remount to continue%s",
927*38d46409SXiubo Li 	     client->monc.auth->global_id, __func__,
928*38d46409SXiubo Li 	     ret ? "" : ceph_pr_addr(&client->msgr.inst.addr),
929a68e564aSXiubo Li 	     ret ? "" : " was blocklisted, ",
930a68e564aSXiubo Li 	     err == -EIO ? " after corrupted snaptrace is fixed" : "");
931a68e564aSXiubo Li 
932963b61ebSSage Weil 	return err;
933963b61ebSSage Weil }
934963b61ebSSage Weil 
935963b61ebSSage Weil 
936963b61ebSSage Weil /*
937963b61ebSSage Weil  * Send any cap_snaps that are queued for flush.  Try to carry
938963b61ebSSage Weil  * s_mutex across multiple snap flushes to avoid locking overhead.
939963b61ebSSage Weil  *
940963b61ebSSage Weil  * Caller holds no locks.
941963b61ebSSage Weil  */
flush_snaps(struct ceph_mds_client * mdsc)942963b61ebSSage Weil static void flush_snaps(struct ceph_mds_client *mdsc)
943963b61ebSSage Weil {
944*38d46409SXiubo Li 	struct ceph_client *cl = mdsc->fsc->client;
945963b61ebSSage Weil 	struct ceph_inode_info *ci;
946963b61ebSSage Weil 	struct inode *inode;
947963b61ebSSage Weil 	struct ceph_mds_session *session = NULL;
948963b61ebSSage Weil 
949*38d46409SXiubo Li 	doutc(cl, "begin\n");
950963b61ebSSage Weil 	spin_lock(&mdsc->snap_flush_lock);
951963b61ebSSage Weil 	while (!list_empty(&mdsc->snap_flush_list)) {
952963b61ebSSage Weil 		ci = list_first_entry(&mdsc->snap_flush_list,
953963b61ebSSage Weil 				struct ceph_inode_info, i_snap_flush_item);
954874c8ca1SDavid Howells 		inode = &ci->netfs.inode;
95570b666c3SSage Weil 		ihold(inode);
956963b61ebSSage Weil 		spin_unlock(&mdsc->snap_flush_lock);
957ed9b430cSYan, Zheng 		ceph_flush_snaps(ci, &session);
95823c2c76eSJeff Layton 		iput(inode);
959963b61ebSSage Weil 		spin_lock(&mdsc->snap_flush_lock);
960963b61ebSSage Weil 	}
961963b61ebSSage Weil 	spin_unlock(&mdsc->snap_flush_lock);
962963b61ebSSage Weil 
963963b61ebSSage Weil 	ceph_put_mds_session(session);
964*38d46409SXiubo Li 	doutc(cl, "done\n");
965963b61ebSSage Weil }
966963b61ebSSage Weil 
9670ba92e1cSJeff Layton /**
9680ba92e1cSJeff Layton  * ceph_change_snap_realm - change the snap_realm for an inode
9690ba92e1cSJeff Layton  * @inode: inode to move to new snap realm
9700ba92e1cSJeff Layton  * @realm: new realm to move inode into (may be NULL)
9710ba92e1cSJeff Layton  *
9720ba92e1cSJeff Layton  * Detach an inode from its old snaprealm (if any) and attach it to
9730ba92e1cSJeff Layton  * the new snaprealm (if any). The old snap realm reference held by
9740ba92e1cSJeff Layton  * the inode is put. If realm is non-NULL, then the caller's reference
9750ba92e1cSJeff Layton  * to it is taken over by the inode.
9760ba92e1cSJeff Layton  */
ceph_change_snap_realm(struct inode * inode,struct ceph_snap_realm * realm)9770ba92e1cSJeff Layton void ceph_change_snap_realm(struct inode *inode, struct ceph_snap_realm *realm)
9780ba92e1cSJeff Layton {
9790ba92e1cSJeff Layton 	struct ceph_inode_info *ci = ceph_inode(inode);
9805995d90dSXiubo Li 	struct ceph_mds_client *mdsc = ceph_inode_to_fs_client(inode)->mdsc;
9810ba92e1cSJeff Layton 	struct ceph_snap_realm *oldrealm = ci->i_snap_realm;
9820ba92e1cSJeff Layton 
9830ba92e1cSJeff Layton 	lockdep_assert_held(&ci->i_ceph_lock);
9840ba92e1cSJeff Layton 
9850ba92e1cSJeff Layton 	if (oldrealm) {
9860ba92e1cSJeff Layton 		spin_lock(&oldrealm->inodes_with_caps_lock);
9870ba92e1cSJeff Layton 		list_del_init(&ci->i_snap_realm_item);
9880ba92e1cSJeff Layton 		if (oldrealm->ino == ci->i_vino.ino)
9890ba92e1cSJeff Layton 			oldrealm->inode = NULL;
9900ba92e1cSJeff Layton 		spin_unlock(&oldrealm->inodes_with_caps_lock);
9910ba92e1cSJeff Layton 		ceph_put_snap_realm(mdsc, oldrealm);
9920ba92e1cSJeff Layton 	}
9930ba92e1cSJeff Layton 
9940ba92e1cSJeff Layton 	ci->i_snap_realm = realm;
9950ba92e1cSJeff Layton 
9960ba92e1cSJeff Layton 	if (realm) {
9970ba92e1cSJeff Layton 		spin_lock(&realm->inodes_with_caps_lock);
9980ba92e1cSJeff Layton 		list_add(&ci->i_snap_realm_item, &realm->inodes_with_caps);
9990ba92e1cSJeff Layton 		if (realm->ino == ci->i_vino.ino)
10000ba92e1cSJeff Layton 			realm->inode = inode;
10010ba92e1cSJeff Layton 		spin_unlock(&realm->inodes_with_caps_lock);
10020ba92e1cSJeff Layton 	}
10030ba92e1cSJeff Layton }
1004963b61ebSSage Weil 
1005963b61ebSSage Weil /*
1006963b61ebSSage Weil  * Handle a snap notification from the MDS.
1007963b61ebSSage Weil  *
1008963b61ebSSage Weil  * This can take two basic forms: the simplest is just a snap creation
1009963b61ebSSage Weil  * or deletion notification on an existing realm.  This should update the
1010963b61ebSSage Weil  * realm and its children.
1011963b61ebSSage Weil  *
1012963b61ebSSage Weil  * The more difficult case is realm creation, due to snap creation at a
1013963b61ebSSage Weil  * new point in the file hierarchy, or due to a rename that moves a file or
1014963b61ebSSage Weil  * directory into another realm.
1015963b61ebSSage Weil  */
ceph_handle_snap(struct ceph_mds_client * mdsc,struct ceph_mds_session * session,struct ceph_msg * msg)1016963b61ebSSage Weil void ceph_handle_snap(struct ceph_mds_client *mdsc,
10172600d2ddSSage Weil 		      struct ceph_mds_session *session,
1018963b61ebSSage Weil 		      struct ceph_msg *msg)
1019963b61ebSSage Weil {
1020*38d46409SXiubo Li 	struct ceph_client *cl = mdsc->fsc->client;
10213d14c5d2SYehuda Sadeh 	struct super_block *sb = mdsc->fsc->sb;
10222600d2ddSSage Weil 	int mds = session->s_mds;
1023963b61ebSSage Weil 	u64 split;
1024963b61ebSSage Weil 	int op;
1025963b61ebSSage Weil 	int trace_len;
1026963b61ebSSage Weil 	struct ceph_snap_realm *realm = NULL;
1027963b61ebSSage Weil 	void *p = msg->front.iov_base;
1028963b61ebSSage Weil 	void *e = p + msg->front.iov_len;
1029963b61ebSSage Weil 	struct ceph_mds_snap_head *h;
1030963b61ebSSage Weil 	int num_split_inos, num_split_realms;
1031963b61ebSSage Weil 	__le64 *split_inos = NULL, *split_realms = NULL;
1032963b61ebSSage Weil 	int i;
1033963b61ebSSage Weil 	int locked_rwsem = 0;
1034a68e564aSXiubo Li 	bool close_sessions = false;
1035963b61ebSSage Weil 
1036e3dfcab2SXiubo Li 	if (!ceph_inc_mds_stopping_blocker(mdsc, session))
1037e3dfcab2SXiubo Li 		return;
1038e3dfcab2SXiubo Li 
1039963b61ebSSage Weil 	/* decode */
1040963b61ebSSage Weil 	if (msg->front.iov_len < sizeof(*h))
1041963b61ebSSage Weil 		goto bad;
1042963b61ebSSage Weil 	h = p;
1043963b61ebSSage Weil 	op = le32_to_cpu(h->op);
1044963b61ebSSage Weil 	split = le64_to_cpu(h->split);   /* non-zero if we are splitting an
1045963b61ebSSage Weil 					  * existing realm */
1046963b61ebSSage Weil 	num_split_inos = le32_to_cpu(h->num_split_inos);
1047963b61ebSSage Weil 	num_split_realms = le32_to_cpu(h->num_split_realms);
1048963b61ebSSage Weil 	trace_len = le32_to_cpu(h->trace_len);
1049963b61ebSSage Weil 	p += sizeof(*h);
1050963b61ebSSage Weil 
1051*38d46409SXiubo Li 	doutc(cl, "from mds%d op %s split %llx tracelen %d\n", mds,
1052*38d46409SXiubo Li 	      ceph_snap_op_name(op), split, trace_len);
1053963b61ebSSage Weil 
1054963b61ebSSage Weil 	down_write(&mdsc->snap_rwsem);
1055963b61ebSSage Weil 	locked_rwsem = 1;
1056963b61ebSSage Weil 
1057963b61ebSSage Weil 	if (op == CEPH_SNAP_OP_SPLIT) {
1058963b61ebSSage Weil 		struct ceph_mds_snap_realm *ri;
1059963b61ebSSage Weil 
1060963b61ebSSage Weil 		/*
1061963b61ebSSage Weil 		 * A "split" breaks part of an existing realm off into
1062963b61ebSSage Weil 		 * a new realm.  The MDS provides a list of inodes
1063963b61ebSSage Weil 		 * (with caps) and child realms that belong to the new
1064963b61ebSSage Weil 		 * child.
1065963b61ebSSage Weil 		 */
1066963b61ebSSage Weil 		split_inos = p;
1067963b61ebSSage Weil 		p += sizeof(u64) * num_split_inos;
1068963b61ebSSage Weil 		split_realms = p;
1069963b61ebSSage Weil 		p += sizeof(u64) * num_split_realms;
1070963b61ebSSage Weil 		ceph_decode_need(&p, e, sizeof(*ri), bad);
1071963b61ebSSage Weil 		/* we will peek at realm info here, but will _not_
1072963b61ebSSage Weil 		 * advance p, as the realm update will occur below in
1073963b61ebSSage Weil 		 * ceph_update_snap_trace. */
1074963b61ebSSage Weil 		ri = p;
1075963b61ebSSage Weil 
1076963b61ebSSage Weil 		realm = ceph_lookup_snap_realm(mdsc, split);
1077963b61ebSSage Weil 		if (!realm) {
1078963b61ebSSage Weil 			realm = ceph_create_snap_realm(mdsc, split);
1079963b61ebSSage Weil 			if (IS_ERR(realm))
1080963b61ebSSage Weil 				goto out;
1081963b61ebSSage Weil 		}
1082963b61ebSSage Weil 
1083*38d46409SXiubo Li 		doutc(cl, "splitting snap_realm %llx %p\n", realm->ino, realm);
1084963b61ebSSage Weil 		for (i = 0; i < num_split_inos; i++) {
1085963b61ebSSage Weil 			struct ceph_vino vino = {
1086963b61ebSSage Weil 				.ino = le64_to_cpu(split_inos[i]),
1087963b61ebSSage Weil 				.snap = CEPH_NOSNAP,
1088963b61ebSSage Weil 			};
1089963b61ebSSage Weil 			struct inode *inode = ceph_find_inode(sb, vino);
1090963b61ebSSage Weil 			struct ceph_inode_info *ci;
1091963b61ebSSage Weil 
1092963b61ebSSage Weil 			if (!inode)
1093963b61ebSSage Weil 				continue;
1094963b61ebSSage Weil 			ci = ceph_inode(inode);
1095963b61ebSSage Weil 
1096be655596SSage Weil 			spin_lock(&ci->i_ceph_lock);
1097963b61ebSSage Weil 			if (!ci->i_snap_realm)
1098963b61ebSSage Weil 				goto skip_inode;
1099963b61ebSSage Weil 			/*
1100963b61ebSSage Weil 			 * If this inode belongs to a realm that was
1101963b61ebSSage Weil 			 * created after our new realm, we experienced
1102963b61ebSSage Weil 			 * a race (due to another split notifications
1103963b61ebSSage Weil 			 * arriving from a different MDS).  So skip
1104963b61ebSSage Weil 			 * this inode.
1105963b61ebSSage Weil 			 */
1106963b61ebSSage Weil 			if (ci->i_snap_realm->created >
1107963b61ebSSage Weil 			    le64_to_cpu(ri->created)) {
1108*38d46409SXiubo Li 				doutc(cl, " leaving %p %llx.%llx in newer realm %llx %p\n",
1109ad5255c1SXiubo Li 				      inode, ceph_vinop(inode), ci->i_snap_realm->ino,
1110963b61ebSSage Weil 				      ci->i_snap_realm);
1111963b61ebSSage Weil 				goto skip_inode;
1112963b61ebSSage Weil 			}
1113*38d46409SXiubo Li 			doutc(cl, " will move %p %llx.%llx to split realm %llx %p\n",
1114ad5255c1SXiubo Li 			      inode, ceph_vinop(inode), realm->ino, realm);
1115963b61ebSSage Weil 
1116ae00d4f3SSage Weil 			ceph_get_snap_realm(mdsc, realm);
11170ba92e1cSJeff Layton 			ceph_change_snap_realm(inode, realm);
11180ba92e1cSJeff Layton 			spin_unlock(&ci->i_ceph_lock);
111923c2c76eSJeff Layton 			iput(inode);
1120963b61ebSSage Weil 			continue;
1121963b61ebSSage Weil 
1122963b61ebSSage Weil skip_inode:
1123be655596SSage Weil 			spin_unlock(&ci->i_ceph_lock);
112423c2c76eSJeff Layton 			iput(inode);
1125963b61ebSSage Weil 		}
1126963b61ebSSage Weil 
1127963b61ebSSage Weil 		/* we may have taken some of the old realm's children. */
1128963b61ebSSage Weil 		for (i = 0; i < num_split_realms; i++) {
1129963b61ebSSage Weil 			struct ceph_snap_realm *child =
1130982d6011SYan, Zheng 				__lookup_snap_realm(mdsc,
1131963b61ebSSage Weil 					   le64_to_cpu(split_realms[i]));
1132963b61ebSSage Weil 			if (!child)
1133963b61ebSSage Weil 				continue;
1134963b61ebSSage Weil 			adjust_snap_realm_parent(mdsc, child, realm->ino);
1135963b61ebSSage Weil 		}
11364cafd040SXiubo Li 	} else {
11374cafd040SXiubo Li 		/*
11384cafd040SXiubo Li 		 * In the non-split case both 'num_split_inos' and
11394cafd040SXiubo Li 		 * 'num_split_realms' should be 0, making this a no-op.
11404cafd040SXiubo Li 		 * However the MDS happens to populate 'split_realms' list
11414cafd040SXiubo Li 		 * in one of the UPDATE op cases by mistake.
11424cafd040SXiubo Li 		 *
11434cafd040SXiubo Li 		 * Skip both lists just in case to ensure that 'p' is
11444cafd040SXiubo Li 		 * positioned at the start of realm info, as expected by
11454cafd040SXiubo Li 		 * ceph_update_snap_trace().
11464cafd040SXiubo Li 		 */
11474cafd040SXiubo Li 		p += sizeof(u64) * num_split_inos;
11484cafd040SXiubo Li 		p += sizeof(u64) * num_split_realms;
1149963b61ebSSage Weil 	}
1150963b61ebSSage Weil 
1151963b61ebSSage Weil 	/*
1152963b61ebSSage Weil 	 * update using the provided snap trace. if we are deleting a
1153963b61ebSSage Weil 	 * snap, we can avoid queueing cap_snaps.
1154963b61ebSSage Weil 	 */
1155a68e564aSXiubo Li 	if (ceph_update_snap_trace(mdsc, p, e,
1156a68e564aSXiubo Li 				   op == CEPH_SNAP_OP_DESTROY,
1157a68e564aSXiubo Li 				   NULL)) {
1158a68e564aSXiubo Li 		close_sessions = true;
1159a68e564aSXiubo Li 		goto bad;
1160a68e564aSXiubo Li 	}
1161963b61ebSSage Weil 
1162ae00d4f3SSage Weil 	if (op == CEPH_SNAP_OP_SPLIT)
1163963b61ebSSage Weil 		/* we took a reference when we created the realm, above */
1164963b61ebSSage Weil 		ceph_put_snap_realm(mdsc, realm);
1165963b61ebSSage Weil 
1166963b61ebSSage Weil 	__cleanup_empty_realms(mdsc);
1167963b61ebSSage Weil 
1168963b61ebSSage Weil 	up_write(&mdsc->snap_rwsem);
1169963b61ebSSage Weil 
1170963b61ebSSage Weil 	flush_snaps(mdsc);
1171e3dfcab2SXiubo Li 	ceph_dec_mds_stopping_blocker(mdsc);
1172963b61ebSSage Weil 	return;
1173963b61ebSSage Weil 
1174963b61ebSSage Weil bad:
1175*38d46409SXiubo Li 	pr_err_client(cl, "corrupt snap message from mds%d\n", mds);
11769ec7cab1SSage Weil 	ceph_msg_dump(msg);
1177963b61ebSSage Weil out:
1178963b61ebSSage Weil 	if (locked_rwsem)
1179963b61ebSSage Weil 		up_write(&mdsc->snap_rwsem);
1180a68e564aSXiubo Li 
1181e3dfcab2SXiubo Li 	ceph_dec_mds_stopping_blocker(mdsc);
1182e3dfcab2SXiubo Li 
1183a68e564aSXiubo Li 	if (close_sessions)
1184a68e564aSXiubo Li 		ceph_mdsc_close_sessions(mdsc);
1185963b61ebSSage Weil 	return;
1186963b61ebSSage Weil }
118775c9627eSYan, Zheng 
ceph_get_snapid_map(struct ceph_mds_client * mdsc,u64 snap)118875c9627eSYan, Zheng struct ceph_snapid_map* ceph_get_snapid_map(struct ceph_mds_client *mdsc,
118975c9627eSYan, Zheng 					    u64 snap)
119075c9627eSYan, Zheng {
1191*38d46409SXiubo Li 	struct ceph_client *cl = mdsc->fsc->client;
119275c9627eSYan, Zheng 	struct ceph_snapid_map *sm, *exist;
119375c9627eSYan, Zheng 	struct rb_node **p, *parent;
119475c9627eSYan, Zheng 	int ret;
119575c9627eSYan, Zheng 
119675c9627eSYan, Zheng 	exist = NULL;
119775c9627eSYan, Zheng 	spin_lock(&mdsc->snapid_map_lock);
119875c9627eSYan, Zheng 	p = &mdsc->snapid_map_tree.rb_node;
119975c9627eSYan, Zheng 	while (*p) {
120075c9627eSYan, Zheng 		exist = rb_entry(*p, struct ceph_snapid_map, node);
120175c9627eSYan, Zheng 		if (snap > exist->snap) {
120275c9627eSYan, Zheng 			p = &(*p)->rb_left;
120375c9627eSYan, Zheng 		} else if (snap < exist->snap) {
120475c9627eSYan, Zheng 			p = &(*p)->rb_right;
120575c9627eSYan, Zheng 		} else {
120675c9627eSYan, Zheng 			if (atomic_inc_return(&exist->ref) == 1)
120775c9627eSYan, Zheng 				list_del_init(&exist->lru);
120875c9627eSYan, Zheng 			break;
120975c9627eSYan, Zheng 		}
121075c9627eSYan, Zheng 		exist = NULL;
121175c9627eSYan, Zheng 	}
121275c9627eSYan, Zheng 	spin_unlock(&mdsc->snapid_map_lock);
121375c9627eSYan, Zheng 	if (exist) {
1214*38d46409SXiubo Li 		doutc(cl, "found snapid map %llx -> %x\n", exist->snap,
1215*38d46409SXiubo Li 		      exist->dev);
121675c9627eSYan, Zheng 		return exist;
121775c9627eSYan, Zheng 	}
121875c9627eSYan, Zheng 
121975c9627eSYan, Zheng 	sm = kmalloc(sizeof(*sm), GFP_NOFS);
122075c9627eSYan, Zheng 	if (!sm)
122175c9627eSYan, Zheng 		return NULL;
122275c9627eSYan, Zheng 
122375c9627eSYan, Zheng 	ret = get_anon_bdev(&sm->dev);
122475c9627eSYan, Zheng 	if (ret < 0) {
122575c9627eSYan, Zheng 		kfree(sm);
122675c9627eSYan, Zheng 		return NULL;
122775c9627eSYan, Zheng 	}
122875c9627eSYan, Zheng 
122975c9627eSYan, Zheng 	INIT_LIST_HEAD(&sm->lru);
123075c9627eSYan, Zheng 	atomic_set(&sm->ref, 1);
123175c9627eSYan, Zheng 	sm->snap = snap;
123275c9627eSYan, Zheng 
123375c9627eSYan, Zheng 	exist = NULL;
123475c9627eSYan, Zheng 	parent = NULL;
123575c9627eSYan, Zheng 	p = &mdsc->snapid_map_tree.rb_node;
123675c9627eSYan, Zheng 	spin_lock(&mdsc->snapid_map_lock);
123775c9627eSYan, Zheng 	while (*p) {
123875c9627eSYan, Zheng 		parent = *p;
123975c9627eSYan, Zheng 		exist = rb_entry(*p, struct ceph_snapid_map, node);
124075c9627eSYan, Zheng 		if (snap > exist->snap)
124175c9627eSYan, Zheng 			p = &(*p)->rb_left;
124275c9627eSYan, Zheng 		else if (snap < exist->snap)
124375c9627eSYan, Zheng 			p = &(*p)->rb_right;
124475c9627eSYan, Zheng 		else
124575c9627eSYan, Zheng 			break;
124675c9627eSYan, Zheng 		exist = NULL;
124775c9627eSYan, Zheng 	}
124875c9627eSYan, Zheng 	if (exist) {
124975c9627eSYan, Zheng 		if (atomic_inc_return(&exist->ref) == 1)
125075c9627eSYan, Zheng 			list_del_init(&exist->lru);
125175c9627eSYan, Zheng 	} else {
125275c9627eSYan, Zheng 		rb_link_node(&sm->node, parent, p);
125375c9627eSYan, Zheng 		rb_insert_color(&sm->node, &mdsc->snapid_map_tree);
125475c9627eSYan, Zheng 	}
125575c9627eSYan, Zheng 	spin_unlock(&mdsc->snapid_map_lock);
125675c9627eSYan, Zheng 	if (exist) {
125775c9627eSYan, Zheng 		free_anon_bdev(sm->dev);
125875c9627eSYan, Zheng 		kfree(sm);
1259*38d46409SXiubo Li 		doutc(cl, "found snapid map %llx -> %x\n", exist->snap,
1260*38d46409SXiubo Li 		      exist->dev);
126175c9627eSYan, Zheng 		return exist;
126275c9627eSYan, Zheng 	}
126375c9627eSYan, Zheng 
1264*38d46409SXiubo Li 	doutc(cl, "create snapid map %llx -> %x\n", sm->snap, sm->dev);
126575c9627eSYan, Zheng 	return sm;
126675c9627eSYan, Zheng }
126775c9627eSYan, Zheng 
ceph_put_snapid_map(struct ceph_mds_client * mdsc,struct ceph_snapid_map * sm)126875c9627eSYan, Zheng void ceph_put_snapid_map(struct ceph_mds_client* mdsc,
126975c9627eSYan, Zheng 			 struct ceph_snapid_map *sm)
127075c9627eSYan, Zheng {
127175c9627eSYan, Zheng 	if (!sm)
127275c9627eSYan, Zheng 		return;
127375c9627eSYan, Zheng 	if (atomic_dec_and_lock(&sm->ref, &mdsc->snapid_map_lock)) {
127475c9627eSYan, Zheng 		if (!RB_EMPTY_NODE(&sm->node)) {
127575c9627eSYan, Zheng 			sm->last_used = jiffies;
127675c9627eSYan, Zheng 			list_add_tail(&sm->lru, &mdsc->snapid_map_lru);
127775c9627eSYan, Zheng 			spin_unlock(&mdsc->snapid_map_lock);
127875c9627eSYan, Zheng 		} else {
127975c9627eSYan, Zheng 			/* already cleaned up by
128075c9627eSYan, Zheng 			 * ceph_cleanup_snapid_map() */
128175c9627eSYan, Zheng 			spin_unlock(&mdsc->snapid_map_lock);
128275c9627eSYan, Zheng 			kfree(sm);
128375c9627eSYan, Zheng 		}
128475c9627eSYan, Zheng 	}
128575c9627eSYan, Zheng }
128675c9627eSYan, Zheng 
ceph_trim_snapid_map(struct ceph_mds_client * mdsc)128775c9627eSYan, Zheng void ceph_trim_snapid_map(struct ceph_mds_client *mdsc)
128875c9627eSYan, Zheng {
1289*38d46409SXiubo Li 	struct ceph_client *cl = mdsc->fsc->client;
129075c9627eSYan, Zheng 	struct ceph_snapid_map *sm;
129175c9627eSYan, Zheng 	unsigned long now;
129275c9627eSYan, Zheng 	LIST_HEAD(to_free);
129375c9627eSYan, Zheng 
129475c9627eSYan, Zheng 	spin_lock(&mdsc->snapid_map_lock);
129575c9627eSYan, Zheng 	now = jiffies;
129675c9627eSYan, Zheng 
129775c9627eSYan, Zheng 	while (!list_empty(&mdsc->snapid_map_lru)) {
129875c9627eSYan, Zheng 		sm = list_first_entry(&mdsc->snapid_map_lru,
129975c9627eSYan, Zheng 				      struct ceph_snapid_map, lru);
130075c9627eSYan, Zheng 		if (time_after(sm->last_used + CEPH_SNAPID_MAP_TIMEOUT, now))
130175c9627eSYan, Zheng 			break;
130275c9627eSYan, Zheng 
130375c9627eSYan, Zheng 		rb_erase(&sm->node, &mdsc->snapid_map_tree);
130475c9627eSYan, Zheng 		list_move(&sm->lru, &to_free);
130575c9627eSYan, Zheng 	}
130675c9627eSYan, Zheng 	spin_unlock(&mdsc->snapid_map_lock);
130775c9627eSYan, Zheng 
130875c9627eSYan, Zheng 	while (!list_empty(&to_free)) {
130975c9627eSYan, Zheng 		sm = list_first_entry(&to_free, struct ceph_snapid_map, lru);
131075c9627eSYan, Zheng 		list_del(&sm->lru);
1311*38d46409SXiubo Li 		doutc(cl, "trim snapid map %llx -> %x\n", sm->snap, sm->dev);
131275c9627eSYan, Zheng 		free_anon_bdev(sm->dev);
131375c9627eSYan, Zheng 		kfree(sm);
131475c9627eSYan, Zheng 	}
131575c9627eSYan, Zheng }
131675c9627eSYan, Zheng 
ceph_cleanup_snapid_map(struct ceph_mds_client * mdsc)131775c9627eSYan, Zheng void ceph_cleanup_snapid_map(struct ceph_mds_client *mdsc)
131875c9627eSYan, Zheng {
1319*38d46409SXiubo Li 	struct ceph_client *cl = mdsc->fsc->client;
132075c9627eSYan, Zheng 	struct ceph_snapid_map *sm;
132175c9627eSYan, Zheng 	struct rb_node *p;
132275c9627eSYan, Zheng 	LIST_HEAD(to_free);
132375c9627eSYan, Zheng 
132475c9627eSYan, Zheng 	spin_lock(&mdsc->snapid_map_lock);
132575c9627eSYan, Zheng 	while ((p = rb_first(&mdsc->snapid_map_tree))) {
132675c9627eSYan, Zheng 		sm = rb_entry(p, struct ceph_snapid_map, node);
132775c9627eSYan, Zheng 		rb_erase(p, &mdsc->snapid_map_tree);
132875c9627eSYan, Zheng 		RB_CLEAR_NODE(p);
132975c9627eSYan, Zheng 		list_move(&sm->lru, &to_free);
133075c9627eSYan, Zheng 	}
133175c9627eSYan, Zheng 	spin_unlock(&mdsc->snapid_map_lock);
133275c9627eSYan, Zheng 
133375c9627eSYan, Zheng 	while (!list_empty(&to_free)) {
133475c9627eSYan, Zheng 		sm = list_first_entry(&to_free, struct ceph_snapid_map, lru);
133575c9627eSYan, Zheng 		list_del(&sm->lru);
133675c9627eSYan, Zheng 		free_anon_bdev(sm->dev);
133775c9627eSYan, Zheng 		if (WARN_ON_ONCE(atomic_read(&sm->ref))) {
1338*38d46409SXiubo Li 			pr_err_client(cl, "snapid map %llx -> %x still in use\n",
133975c9627eSYan, Zheng 				      sm->snap, sm->dev);
134075c9627eSYan, Zheng 		}
1341c8d6ee01SLuis Henriques 		kfree(sm);
134275c9627eSYan, Zheng 	}
134375c9627eSYan, Zheng }
1344