1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0
2*4882a593Smuzhiyun #include <linux/ceph/ceph_debug.h>
3*4882a593Smuzhiyun
4*4882a593Smuzhiyun #include <linux/sort.h>
5*4882a593Smuzhiyun #include <linux/slab.h>
6*4882a593Smuzhiyun #include <linux/iversion.h>
7*4882a593Smuzhiyun #include "super.h"
8*4882a593Smuzhiyun #include "mds_client.h"
9*4882a593Smuzhiyun #include <linux/ceph/decode.h>
10*4882a593Smuzhiyun
11*4882a593Smuzhiyun /* unused map expires after 5 minutes */
12*4882a593Smuzhiyun #define CEPH_SNAPID_MAP_TIMEOUT (5 * 60 * HZ)
13*4882a593Smuzhiyun
14*4882a593Smuzhiyun /*
15*4882a593Smuzhiyun * Snapshots in ceph are driven in large part by cooperation from the
16*4882a593Smuzhiyun * client. In contrast to local file systems or file servers that
17*4882a593Smuzhiyun * implement snapshots at a single point in the system, ceph's
18*4882a593Smuzhiyun * distributed access to storage requires clients to help decide
19*4882a593Smuzhiyun * whether a write logically occurs before or after a recently created
20*4882a593Smuzhiyun * snapshot.
21*4882a593Smuzhiyun *
22*4882a593Smuzhiyun * This provides a perfect instantanous client-wide snapshot. Between
23*4882a593Smuzhiyun * clients, however, snapshots may appear to be applied at slightly
24*4882a593Smuzhiyun * different points in time, depending on delays in delivering the
25*4882a593Smuzhiyun * snapshot notification.
26*4882a593Smuzhiyun *
27*4882a593Smuzhiyun * Snapshots are _not_ file system-wide. Instead, each snapshot
28*4882a593Smuzhiyun * applies to the subdirectory nested beneath some directory. This
29*4882a593Smuzhiyun * effectively divides the hierarchy into multiple "realms," where all
30*4882a593Smuzhiyun * of the files contained by each realm share the same set of
31*4882a593Smuzhiyun * snapshots. An individual realm's snap set contains snapshots
32*4882a593Smuzhiyun * explicitly created on that realm, as well as any snaps in its
33*4882a593Smuzhiyun * parent's snap set _after_ the point at which the parent became it's
34*4882a593Smuzhiyun * parent (due to, say, a rename). Similarly, snaps from prior parents
35*4882a593Smuzhiyun * during the time intervals during which they were the parent are included.
36*4882a593Smuzhiyun *
37*4882a593Smuzhiyun * The client is spared most of this detail, fortunately... it must only
38*4882a593Smuzhiyun * maintains a hierarchy of realms reflecting the current parent/child
39*4882a593Smuzhiyun * realm relationship, and for each realm has an explicit list of snaps
40*4882a593Smuzhiyun * inherited from prior parents.
41*4882a593Smuzhiyun *
42*4882a593Smuzhiyun * A snap_realm struct is maintained for realms containing every inode
43*4882a593Smuzhiyun * with an open cap in the system. (The needed snap realm information is
44*4882a593Smuzhiyun * provided by the MDS whenever a cap is issued, i.e., on open.) A 'seq'
45*4882a593Smuzhiyun * version number is used to ensure that as realm parameters change (new
46*4882a593Smuzhiyun * snapshot, new parent, etc.) the client's realm hierarchy is updated.
47*4882a593Smuzhiyun *
48*4882a593Smuzhiyun * The realm hierarchy drives the generation of a 'snap context' for each
49*4882a593Smuzhiyun * realm, which simply lists the resulting set of snaps for the realm. This
50*4882a593Smuzhiyun * is attached to any writes sent to OSDs.
51*4882a593Smuzhiyun */
52*4882a593Smuzhiyun /*
53*4882a593Smuzhiyun * Unfortunately error handling is a bit mixed here. If we get a snap
54*4882a593Smuzhiyun * update, but don't have enough memory to update our realm hierarchy,
55*4882a593Smuzhiyun * it's not clear what we can do about it (besides complaining to the
56*4882a593Smuzhiyun * console).
57*4882a593Smuzhiyun */
58*4882a593Smuzhiyun
59*4882a593Smuzhiyun
60*4882a593Smuzhiyun /*
61*4882a593Smuzhiyun * increase ref count for the realm
62*4882a593Smuzhiyun *
63*4882a593Smuzhiyun * caller must hold snap_rwsem.
64*4882a593Smuzhiyun */
ceph_get_snap_realm(struct ceph_mds_client * mdsc,struct ceph_snap_realm * realm)65*4882a593Smuzhiyun void ceph_get_snap_realm(struct ceph_mds_client *mdsc,
66*4882a593Smuzhiyun struct ceph_snap_realm *realm)
67*4882a593Smuzhiyun {
68*4882a593Smuzhiyun lockdep_assert_held(&mdsc->snap_rwsem);
69*4882a593Smuzhiyun
70*4882a593Smuzhiyun /*
71*4882a593Smuzhiyun * The 0->1 and 1->0 transitions must take the snap_empty_lock
72*4882a593Smuzhiyun * atomically with the refcount change. Go ahead and bump the
73*4882a593Smuzhiyun * nref here, unless it's 0, in which case we take the spinlock
74*4882a593Smuzhiyun * and then do the increment and remove it from the list.
75*4882a593Smuzhiyun */
76*4882a593Smuzhiyun if (atomic_inc_not_zero(&realm->nref))
77*4882a593Smuzhiyun return;
78*4882a593Smuzhiyun
79*4882a593Smuzhiyun spin_lock(&mdsc->snap_empty_lock);
80*4882a593Smuzhiyun if (atomic_inc_return(&realm->nref) == 1)
81*4882a593Smuzhiyun list_del_init(&realm->empty_item);
82*4882a593Smuzhiyun spin_unlock(&mdsc->snap_empty_lock);
83*4882a593Smuzhiyun }
84*4882a593Smuzhiyun
__insert_snap_realm(struct rb_root * root,struct ceph_snap_realm * new)85*4882a593Smuzhiyun static void __insert_snap_realm(struct rb_root *root,
86*4882a593Smuzhiyun struct ceph_snap_realm *new)
87*4882a593Smuzhiyun {
88*4882a593Smuzhiyun struct rb_node **p = &root->rb_node;
89*4882a593Smuzhiyun struct rb_node *parent = NULL;
90*4882a593Smuzhiyun struct ceph_snap_realm *r = NULL;
91*4882a593Smuzhiyun
92*4882a593Smuzhiyun while (*p) {
93*4882a593Smuzhiyun parent = *p;
94*4882a593Smuzhiyun r = rb_entry(parent, struct ceph_snap_realm, node);
95*4882a593Smuzhiyun if (new->ino < r->ino)
96*4882a593Smuzhiyun p = &(*p)->rb_left;
97*4882a593Smuzhiyun else if (new->ino > r->ino)
98*4882a593Smuzhiyun p = &(*p)->rb_right;
99*4882a593Smuzhiyun else
100*4882a593Smuzhiyun BUG();
101*4882a593Smuzhiyun }
102*4882a593Smuzhiyun
103*4882a593Smuzhiyun rb_link_node(&new->node, parent, p);
104*4882a593Smuzhiyun rb_insert_color(&new->node, root);
105*4882a593Smuzhiyun }
106*4882a593Smuzhiyun
107*4882a593Smuzhiyun /*
108*4882a593Smuzhiyun * create and get the realm rooted at @ino and bump its ref count.
109*4882a593Smuzhiyun *
110*4882a593Smuzhiyun * caller must hold snap_rwsem for write.
111*4882a593Smuzhiyun */
ceph_create_snap_realm(struct ceph_mds_client * mdsc,u64 ino)112*4882a593Smuzhiyun static struct ceph_snap_realm *ceph_create_snap_realm(
113*4882a593Smuzhiyun struct ceph_mds_client *mdsc,
114*4882a593Smuzhiyun u64 ino)
115*4882a593Smuzhiyun {
116*4882a593Smuzhiyun struct ceph_snap_realm *realm;
117*4882a593Smuzhiyun
118*4882a593Smuzhiyun lockdep_assert_held_write(&mdsc->snap_rwsem);
119*4882a593Smuzhiyun
120*4882a593Smuzhiyun realm = kzalloc(sizeof(*realm), GFP_NOFS);
121*4882a593Smuzhiyun if (!realm)
122*4882a593Smuzhiyun return ERR_PTR(-ENOMEM);
123*4882a593Smuzhiyun
124*4882a593Smuzhiyun atomic_set(&realm->nref, 1); /* for caller */
125*4882a593Smuzhiyun realm->ino = ino;
126*4882a593Smuzhiyun INIT_LIST_HEAD(&realm->children);
127*4882a593Smuzhiyun INIT_LIST_HEAD(&realm->child_item);
128*4882a593Smuzhiyun INIT_LIST_HEAD(&realm->empty_item);
129*4882a593Smuzhiyun INIT_LIST_HEAD(&realm->dirty_item);
130*4882a593Smuzhiyun INIT_LIST_HEAD(&realm->inodes_with_caps);
131*4882a593Smuzhiyun spin_lock_init(&realm->inodes_with_caps_lock);
132*4882a593Smuzhiyun __insert_snap_realm(&mdsc->snap_realms, realm);
133*4882a593Smuzhiyun mdsc->num_snap_realms++;
134*4882a593Smuzhiyun
135*4882a593Smuzhiyun dout("create_snap_realm %llx %p\n", realm->ino, realm);
136*4882a593Smuzhiyun return realm;
137*4882a593Smuzhiyun }
138*4882a593Smuzhiyun
139*4882a593Smuzhiyun /*
140*4882a593Smuzhiyun * lookup the realm rooted at @ino.
141*4882a593Smuzhiyun *
142*4882a593Smuzhiyun * caller must hold snap_rwsem.
143*4882a593Smuzhiyun */
__lookup_snap_realm(struct ceph_mds_client * mdsc,u64 ino)144*4882a593Smuzhiyun static struct ceph_snap_realm *__lookup_snap_realm(struct ceph_mds_client *mdsc,
145*4882a593Smuzhiyun u64 ino)
146*4882a593Smuzhiyun {
147*4882a593Smuzhiyun struct rb_node *n = mdsc->snap_realms.rb_node;
148*4882a593Smuzhiyun struct ceph_snap_realm *r;
149*4882a593Smuzhiyun
150*4882a593Smuzhiyun lockdep_assert_held(&mdsc->snap_rwsem);
151*4882a593Smuzhiyun
152*4882a593Smuzhiyun while (n) {
153*4882a593Smuzhiyun r = rb_entry(n, struct ceph_snap_realm, node);
154*4882a593Smuzhiyun if (ino < r->ino)
155*4882a593Smuzhiyun n = n->rb_left;
156*4882a593Smuzhiyun else if (ino > r->ino)
157*4882a593Smuzhiyun n = n->rb_right;
158*4882a593Smuzhiyun else {
159*4882a593Smuzhiyun dout("lookup_snap_realm %llx %p\n", r->ino, r);
160*4882a593Smuzhiyun return r;
161*4882a593Smuzhiyun }
162*4882a593Smuzhiyun }
163*4882a593Smuzhiyun return NULL;
164*4882a593Smuzhiyun }
165*4882a593Smuzhiyun
ceph_lookup_snap_realm(struct ceph_mds_client * mdsc,u64 ino)166*4882a593Smuzhiyun struct ceph_snap_realm *ceph_lookup_snap_realm(struct ceph_mds_client *mdsc,
167*4882a593Smuzhiyun u64 ino)
168*4882a593Smuzhiyun {
169*4882a593Smuzhiyun struct ceph_snap_realm *r;
170*4882a593Smuzhiyun r = __lookup_snap_realm(mdsc, ino);
171*4882a593Smuzhiyun if (r)
172*4882a593Smuzhiyun ceph_get_snap_realm(mdsc, r);
173*4882a593Smuzhiyun return r;
174*4882a593Smuzhiyun }
175*4882a593Smuzhiyun
176*4882a593Smuzhiyun static void __put_snap_realm(struct ceph_mds_client *mdsc,
177*4882a593Smuzhiyun struct ceph_snap_realm *realm);
178*4882a593Smuzhiyun
179*4882a593Smuzhiyun /*
180*4882a593Smuzhiyun * called with snap_rwsem (write)
181*4882a593Smuzhiyun */
__destroy_snap_realm(struct ceph_mds_client * mdsc,struct ceph_snap_realm * realm)182*4882a593Smuzhiyun static void __destroy_snap_realm(struct ceph_mds_client *mdsc,
183*4882a593Smuzhiyun struct ceph_snap_realm *realm)
184*4882a593Smuzhiyun {
185*4882a593Smuzhiyun lockdep_assert_held_write(&mdsc->snap_rwsem);
186*4882a593Smuzhiyun
187*4882a593Smuzhiyun dout("__destroy_snap_realm %p %llx\n", realm, realm->ino);
188*4882a593Smuzhiyun
189*4882a593Smuzhiyun rb_erase(&realm->node, &mdsc->snap_realms);
190*4882a593Smuzhiyun mdsc->num_snap_realms--;
191*4882a593Smuzhiyun
192*4882a593Smuzhiyun if (realm->parent) {
193*4882a593Smuzhiyun list_del_init(&realm->child_item);
194*4882a593Smuzhiyun __put_snap_realm(mdsc, realm->parent);
195*4882a593Smuzhiyun }
196*4882a593Smuzhiyun
197*4882a593Smuzhiyun kfree(realm->prior_parent_snaps);
198*4882a593Smuzhiyun kfree(realm->snaps);
199*4882a593Smuzhiyun ceph_put_snap_context(realm->cached_context);
200*4882a593Smuzhiyun kfree(realm);
201*4882a593Smuzhiyun }
202*4882a593Smuzhiyun
203*4882a593Smuzhiyun /*
204*4882a593Smuzhiyun * caller holds snap_rwsem (write)
205*4882a593Smuzhiyun */
__put_snap_realm(struct ceph_mds_client * mdsc,struct ceph_snap_realm * realm)206*4882a593Smuzhiyun static void __put_snap_realm(struct ceph_mds_client *mdsc,
207*4882a593Smuzhiyun struct ceph_snap_realm *realm)
208*4882a593Smuzhiyun {
209*4882a593Smuzhiyun lockdep_assert_held_write(&mdsc->snap_rwsem);
210*4882a593Smuzhiyun
211*4882a593Smuzhiyun /*
212*4882a593Smuzhiyun * We do not require the snap_empty_lock here, as any caller that
213*4882a593Smuzhiyun * increments the value must hold the snap_rwsem.
214*4882a593Smuzhiyun */
215*4882a593Smuzhiyun if (atomic_dec_and_test(&realm->nref))
216*4882a593Smuzhiyun __destroy_snap_realm(mdsc, realm);
217*4882a593Smuzhiyun }
218*4882a593Smuzhiyun
219*4882a593Smuzhiyun /*
220*4882a593Smuzhiyun * See comments in ceph_get_snap_realm. Caller needn't hold any locks.
221*4882a593Smuzhiyun */
ceph_put_snap_realm(struct ceph_mds_client * mdsc,struct ceph_snap_realm * realm)222*4882a593Smuzhiyun void ceph_put_snap_realm(struct ceph_mds_client *mdsc,
223*4882a593Smuzhiyun struct ceph_snap_realm *realm)
224*4882a593Smuzhiyun {
225*4882a593Smuzhiyun if (!atomic_dec_and_lock(&realm->nref, &mdsc->snap_empty_lock))
226*4882a593Smuzhiyun return;
227*4882a593Smuzhiyun
228*4882a593Smuzhiyun if (down_write_trylock(&mdsc->snap_rwsem)) {
229*4882a593Smuzhiyun spin_unlock(&mdsc->snap_empty_lock);
230*4882a593Smuzhiyun __destroy_snap_realm(mdsc, realm);
231*4882a593Smuzhiyun up_write(&mdsc->snap_rwsem);
232*4882a593Smuzhiyun } else {
233*4882a593Smuzhiyun list_add(&realm->empty_item, &mdsc->snap_empty);
234*4882a593Smuzhiyun spin_unlock(&mdsc->snap_empty_lock);
235*4882a593Smuzhiyun }
236*4882a593Smuzhiyun }
237*4882a593Smuzhiyun
238*4882a593Smuzhiyun /*
239*4882a593Smuzhiyun * Clean up any realms whose ref counts have dropped to zero. Note
240*4882a593Smuzhiyun * that this does not include realms who were created but not yet
241*4882a593Smuzhiyun * used.
242*4882a593Smuzhiyun *
243*4882a593Smuzhiyun * Called under snap_rwsem (write)
244*4882a593Smuzhiyun */
__cleanup_empty_realms(struct ceph_mds_client * mdsc)245*4882a593Smuzhiyun static void __cleanup_empty_realms(struct ceph_mds_client *mdsc)
246*4882a593Smuzhiyun {
247*4882a593Smuzhiyun struct ceph_snap_realm *realm;
248*4882a593Smuzhiyun
249*4882a593Smuzhiyun lockdep_assert_held_write(&mdsc->snap_rwsem);
250*4882a593Smuzhiyun
251*4882a593Smuzhiyun spin_lock(&mdsc->snap_empty_lock);
252*4882a593Smuzhiyun while (!list_empty(&mdsc->snap_empty)) {
253*4882a593Smuzhiyun realm = list_first_entry(&mdsc->snap_empty,
254*4882a593Smuzhiyun struct ceph_snap_realm, empty_item);
255*4882a593Smuzhiyun list_del(&realm->empty_item);
256*4882a593Smuzhiyun spin_unlock(&mdsc->snap_empty_lock);
257*4882a593Smuzhiyun __destroy_snap_realm(mdsc, realm);
258*4882a593Smuzhiyun spin_lock(&mdsc->snap_empty_lock);
259*4882a593Smuzhiyun }
260*4882a593Smuzhiyun spin_unlock(&mdsc->snap_empty_lock);
261*4882a593Smuzhiyun }
262*4882a593Smuzhiyun
ceph_cleanup_empty_realms(struct ceph_mds_client * mdsc)263*4882a593Smuzhiyun void ceph_cleanup_empty_realms(struct ceph_mds_client *mdsc)
264*4882a593Smuzhiyun {
265*4882a593Smuzhiyun down_write(&mdsc->snap_rwsem);
266*4882a593Smuzhiyun __cleanup_empty_realms(mdsc);
267*4882a593Smuzhiyun up_write(&mdsc->snap_rwsem);
268*4882a593Smuzhiyun }
269*4882a593Smuzhiyun
270*4882a593Smuzhiyun /*
271*4882a593Smuzhiyun * adjust the parent realm of a given @realm. adjust child list, and parent
272*4882a593Smuzhiyun * pointers, and ref counts appropriately.
273*4882a593Smuzhiyun *
274*4882a593Smuzhiyun * return true if parent was changed, 0 if unchanged, <0 on error.
275*4882a593Smuzhiyun *
276*4882a593Smuzhiyun * caller must hold snap_rwsem for write.
277*4882a593Smuzhiyun */
adjust_snap_realm_parent(struct ceph_mds_client * mdsc,struct ceph_snap_realm * realm,u64 parentino)278*4882a593Smuzhiyun static int adjust_snap_realm_parent(struct ceph_mds_client *mdsc,
279*4882a593Smuzhiyun struct ceph_snap_realm *realm,
280*4882a593Smuzhiyun u64 parentino)
281*4882a593Smuzhiyun {
282*4882a593Smuzhiyun struct ceph_snap_realm *parent;
283*4882a593Smuzhiyun
284*4882a593Smuzhiyun lockdep_assert_held_write(&mdsc->snap_rwsem);
285*4882a593Smuzhiyun
286*4882a593Smuzhiyun if (realm->parent_ino == parentino)
287*4882a593Smuzhiyun return 0;
288*4882a593Smuzhiyun
289*4882a593Smuzhiyun parent = ceph_lookup_snap_realm(mdsc, parentino);
290*4882a593Smuzhiyun if (!parent) {
291*4882a593Smuzhiyun parent = ceph_create_snap_realm(mdsc, parentino);
292*4882a593Smuzhiyun if (IS_ERR(parent))
293*4882a593Smuzhiyun return PTR_ERR(parent);
294*4882a593Smuzhiyun }
295*4882a593Smuzhiyun dout("adjust_snap_realm_parent %llx %p: %llx %p -> %llx %p\n",
296*4882a593Smuzhiyun realm->ino, realm, realm->parent_ino, realm->parent,
297*4882a593Smuzhiyun parentino, parent);
298*4882a593Smuzhiyun if (realm->parent) {
299*4882a593Smuzhiyun list_del_init(&realm->child_item);
300*4882a593Smuzhiyun ceph_put_snap_realm(mdsc, realm->parent);
301*4882a593Smuzhiyun }
302*4882a593Smuzhiyun realm->parent_ino = parentino;
303*4882a593Smuzhiyun realm->parent = parent;
304*4882a593Smuzhiyun list_add(&realm->child_item, &parent->children);
305*4882a593Smuzhiyun return 1;
306*4882a593Smuzhiyun }
307*4882a593Smuzhiyun
308*4882a593Smuzhiyun
cmpu64_rev(const void * a,const void * b)309*4882a593Smuzhiyun static int cmpu64_rev(const void *a, const void *b)
310*4882a593Smuzhiyun {
311*4882a593Smuzhiyun if (*(u64 *)a < *(u64 *)b)
312*4882a593Smuzhiyun return 1;
313*4882a593Smuzhiyun if (*(u64 *)a > *(u64 *)b)
314*4882a593Smuzhiyun return -1;
315*4882a593Smuzhiyun return 0;
316*4882a593Smuzhiyun }
317*4882a593Smuzhiyun
318*4882a593Smuzhiyun
319*4882a593Smuzhiyun /*
320*4882a593Smuzhiyun * build the snap context for a given realm.
321*4882a593Smuzhiyun */
build_snap_context(struct ceph_snap_realm * realm,struct list_head * dirty_realms)322*4882a593Smuzhiyun static int build_snap_context(struct ceph_snap_realm *realm,
323*4882a593Smuzhiyun struct list_head* dirty_realms)
324*4882a593Smuzhiyun {
325*4882a593Smuzhiyun struct ceph_snap_realm *parent = realm->parent;
326*4882a593Smuzhiyun struct ceph_snap_context *snapc;
327*4882a593Smuzhiyun int err = 0;
328*4882a593Smuzhiyun u32 num = realm->num_prior_parent_snaps + realm->num_snaps;
329*4882a593Smuzhiyun
330*4882a593Smuzhiyun /*
331*4882a593Smuzhiyun * build parent context, if it hasn't been built.
332*4882a593Smuzhiyun * conservatively estimate that all parent snaps might be
333*4882a593Smuzhiyun * included by us.
334*4882a593Smuzhiyun */
335*4882a593Smuzhiyun if (parent) {
336*4882a593Smuzhiyun if (!parent->cached_context) {
337*4882a593Smuzhiyun err = build_snap_context(parent, dirty_realms);
338*4882a593Smuzhiyun if (err)
339*4882a593Smuzhiyun goto fail;
340*4882a593Smuzhiyun }
341*4882a593Smuzhiyun num += parent->cached_context->num_snaps;
342*4882a593Smuzhiyun }
343*4882a593Smuzhiyun
344*4882a593Smuzhiyun /* do i actually need to update? not if my context seq
345*4882a593Smuzhiyun matches realm seq, and my parents' does to. (this works
346*4882a593Smuzhiyun because we rebuild_snap_realms() works _downward_ in
347*4882a593Smuzhiyun hierarchy after each update.) */
348*4882a593Smuzhiyun if (realm->cached_context &&
349*4882a593Smuzhiyun realm->cached_context->seq == realm->seq &&
350*4882a593Smuzhiyun (!parent ||
351*4882a593Smuzhiyun realm->cached_context->seq >= parent->cached_context->seq)) {
352*4882a593Smuzhiyun dout("build_snap_context %llx %p: %p seq %lld (%u snaps)"
353*4882a593Smuzhiyun " (unchanged)\n",
354*4882a593Smuzhiyun realm->ino, realm, realm->cached_context,
355*4882a593Smuzhiyun realm->cached_context->seq,
356*4882a593Smuzhiyun (unsigned int)realm->cached_context->num_snaps);
357*4882a593Smuzhiyun return 0;
358*4882a593Smuzhiyun }
359*4882a593Smuzhiyun
360*4882a593Smuzhiyun /* alloc new snap context */
361*4882a593Smuzhiyun err = -ENOMEM;
362*4882a593Smuzhiyun if (num > (SIZE_MAX - sizeof(*snapc)) / sizeof(u64))
363*4882a593Smuzhiyun goto fail;
364*4882a593Smuzhiyun snapc = ceph_create_snap_context(num, GFP_NOFS);
365*4882a593Smuzhiyun if (!snapc)
366*4882a593Smuzhiyun goto fail;
367*4882a593Smuzhiyun
368*4882a593Smuzhiyun /* build (reverse sorted) snap vector */
369*4882a593Smuzhiyun num = 0;
370*4882a593Smuzhiyun snapc->seq = realm->seq;
371*4882a593Smuzhiyun if (parent) {
372*4882a593Smuzhiyun u32 i;
373*4882a593Smuzhiyun
374*4882a593Smuzhiyun /* include any of parent's snaps occurring _after_ my
375*4882a593Smuzhiyun parent became my parent */
376*4882a593Smuzhiyun for (i = 0; i < parent->cached_context->num_snaps; i++)
377*4882a593Smuzhiyun if (parent->cached_context->snaps[i] >=
378*4882a593Smuzhiyun realm->parent_since)
379*4882a593Smuzhiyun snapc->snaps[num++] =
380*4882a593Smuzhiyun parent->cached_context->snaps[i];
381*4882a593Smuzhiyun if (parent->cached_context->seq > snapc->seq)
382*4882a593Smuzhiyun snapc->seq = parent->cached_context->seq;
383*4882a593Smuzhiyun }
384*4882a593Smuzhiyun memcpy(snapc->snaps + num, realm->snaps,
385*4882a593Smuzhiyun sizeof(u64)*realm->num_snaps);
386*4882a593Smuzhiyun num += realm->num_snaps;
387*4882a593Smuzhiyun memcpy(snapc->snaps + num, realm->prior_parent_snaps,
388*4882a593Smuzhiyun sizeof(u64)*realm->num_prior_parent_snaps);
389*4882a593Smuzhiyun num += realm->num_prior_parent_snaps;
390*4882a593Smuzhiyun
391*4882a593Smuzhiyun sort(snapc->snaps, num, sizeof(u64), cmpu64_rev, NULL);
392*4882a593Smuzhiyun snapc->num_snaps = num;
393*4882a593Smuzhiyun dout("build_snap_context %llx %p: %p seq %lld (%u snaps)\n",
394*4882a593Smuzhiyun realm->ino, realm, snapc, snapc->seq,
395*4882a593Smuzhiyun (unsigned int) snapc->num_snaps);
396*4882a593Smuzhiyun
397*4882a593Smuzhiyun ceph_put_snap_context(realm->cached_context);
398*4882a593Smuzhiyun realm->cached_context = snapc;
399*4882a593Smuzhiyun /* queue realm for cap_snap creation */
400*4882a593Smuzhiyun list_add_tail(&realm->dirty_item, dirty_realms);
401*4882a593Smuzhiyun return 0;
402*4882a593Smuzhiyun
403*4882a593Smuzhiyun fail:
404*4882a593Smuzhiyun /*
405*4882a593Smuzhiyun * if we fail, clear old (incorrect) cached_context... hopefully
406*4882a593Smuzhiyun * we'll have better luck building it later
407*4882a593Smuzhiyun */
408*4882a593Smuzhiyun if (realm->cached_context) {
409*4882a593Smuzhiyun ceph_put_snap_context(realm->cached_context);
410*4882a593Smuzhiyun realm->cached_context = NULL;
411*4882a593Smuzhiyun }
412*4882a593Smuzhiyun pr_err("build_snap_context %llx %p fail %d\n", realm->ino,
413*4882a593Smuzhiyun realm, err);
414*4882a593Smuzhiyun return err;
415*4882a593Smuzhiyun }
416*4882a593Smuzhiyun
417*4882a593Smuzhiyun /*
418*4882a593Smuzhiyun * rebuild snap context for the given realm and all of its children.
419*4882a593Smuzhiyun */
rebuild_snap_realms(struct ceph_snap_realm * realm,struct list_head * dirty_realms)420*4882a593Smuzhiyun static void rebuild_snap_realms(struct ceph_snap_realm *realm,
421*4882a593Smuzhiyun struct list_head *dirty_realms)
422*4882a593Smuzhiyun {
423*4882a593Smuzhiyun struct ceph_snap_realm *child;
424*4882a593Smuzhiyun
425*4882a593Smuzhiyun dout("rebuild_snap_realms %llx %p\n", realm->ino, realm);
426*4882a593Smuzhiyun build_snap_context(realm, dirty_realms);
427*4882a593Smuzhiyun
428*4882a593Smuzhiyun list_for_each_entry(child, &realm->children, child_item)
429*4882a593Smuzhiyun rebuild_snap_realms(child, dirty_realms);
430*4882a593Smuzhiyun }
431*4882a593Smuzhiyun
432*4882a593Smuzhiyun
433*4882a593Smuzhiyun /*
434*4882a593Smuzhiyun * helper to allocate and decode an array of snapids. free prior
435*4882a593Smuzhiyun * instance, if any.
436*4882a593Smuzhiyun */
dup_array(u64 ** dst,__le64 * src,u32 num)437*4882a593Smuzhiyun static int dup_array(u64 **dst, __le64 *src, u32 num)
438*4882a593Smuzhiyun {
439*4882a593Smuzhiyun u32 i;
440*4882a593Smuzhiyun
441*4882a593Smuzhiyun kfree(*dst);
442*4882a593Smuzhiyun if (num) {
443*4882a593Smuzhiyun *dst = kcalloc(num, sizeof(u64), GFP_NOFS);
444*4882a593Smuzhiyun if (!*dst)
445*4882a593Smuzhiyun return -ENOMEM;
446*4882a593Smuzhiyun for (i = 0; i < num; i++)
447*4882a593Smuzhiyun (*dst)[i] = get_unaligned_le64(src + i);
448*4882a593Smuzhiyun } else {
449*4882a593Smuzhiyun *dst = NULL;
450*4882a593Smuzhiyun }
451*4882a593Smuzhiyun return 0;
452*4882a593Smuzhiyun }
453*4882a593Smuzhiyun
has_new_snaps(struct ceph_snap_context * o,struct ceph_snap_context * n)454*4882a593Smuzhiyun static bool has_new_snaps(struct ceph_snap_context *o,
455*4882a593Smuzhiyun struct ceph_snap_context *n)
456*4882a593Smuzhiyun {
457*4882a593Smuzhiyun if (n->num_snaps == 0)
458*4882a593Smuzhiyun return false;
459*4882a593Smuzhiyun /* snaps are in descending order */
460*4882a593Smuzhiyun return n->snaps[0] > o->seq;
461*4882a593Smuzhiyun }
462*4882a593Smuzhiyun
463*4882a593Smuzhiyun /*
464*4882a593Smuzhiyun * When a snapshot is applied, the size/mtime inode metadata is queued
465*4882a593Smuzhiyun * in a ceph_cap_snap (one for each snapshot) until writeback
466*4882a593Smuzhiyun * completes and the metadata can be flushed back to the MDS.
467*4882a593Smuzhiyun *
468*4882a593Smuzhiyun * However, if a (sync) write is currently in-progress when we apply
469*4882a593Smuzhiyun * the snapshot, we have to wait until the write succeeds or fails
470*4882a593Smuzhiyun * (and a final size/mtime is known). In this case the
471*4882a593Smuzhiyun * cap_snap->writing = 1, and is said to be "pending." When the write
472*4882a593Smuzhiyun * finishes, we __ceph_finish_cap_snap().
473*4882a593Smuzhiyun *
474*4882a593Smuzhiyun * Caller must hold snap_rwsem for read (i.e., the realm topology won't
475*4882a593Smuzhiyun * change).
476*4882a593Smuzhiyun */
ceph_queue_cap_snap(struct ceph_inode_info * ci)477*4882a593Smuzhiyun void ceph_queue_cap_snap(struct ceph_inode_info *ci)
478*4882a593Smuzhiyun {
479*4882a593Smuzhiyun struct inode *inode = &ci->vfs_inode;
480*4882a593Smuzhiyun struct ceph_cap_snap *capsnap;
481*4882a593Smuzhiyun struct ceph_snap_context *old_snapc, *new_snapc;
482*4882a593Smuzhiyun struct ceph_buffer *old_blob = NULL;
483*4882a593Smuzhiyun int used, dirty;
484*4882a593Smuzhiyun
485*4882a593Smuzhiyun capsnap = kzalloc(sizeof(*capsnap), GFP_NOFS);
486*4882a593Smuzhiyun if (!capsnap) {
487*4882a593Smuzhiyun pr_err("ENOMEM allocating ceph_cap_snap on %p\n", inode);
488*4882a593Smuzhiyun return;
489*4882a593Smuzhiyun }
490*4882a593Smuzhiyun capsnap->cap_flush.is_capsnap = true;
491*4882a593Smuzhiyun INIT_LIST_HEAD(&capsnap->cap_flush.i_list);
492*4882a593Smuzhiyun INIT_LIST_HEAD(&capsnap->cap_flush.g_list);
493*4882a593Smuzhiyun
494*4882a593Smuzhiyun spin_lock(&ci->i_ceph_lock);
495*4882a593Smuzhiyun used = __ceph_caps_used(ci);
496*4882a593Smuzhiyun dirty = __ceph_caps_dirty(ci);
497*4882a593Smuzhiyun
498*4882a593Smuzhiyun old_snapc = ci->i_head_snapc;
499*4882a593Smuzhiyun new_snapc = ci->i_snap_realm->cached_context;
500*4882a593Smuzhiyun
501*4882a593Smuzhiyun /*
502*4882a593Smuzhiyun * If there is a write in progress, treat that as a dirty Fw,
503*4882a593Smuzhiyun * even though it hasn't completed yet; by the time we finish
504*4882a593Smuzhiyun * up this capsnap it will be.
505*4882a593Smuzhiyun */
506*4882a593Smuzhiyun if (used & CEPH_CAP_FILE_WR)
507*4882a593Smuzhiyun dirty |= CEPH_CAP_FILE_WR;
508*4882a593Smuzhiyun
509*4882a593Smuzhiyun if (__ceph_have_pending_cap_snap(ci)) {
510*4882a593Smuzhiyun /* there is no point in queuing multiple "pending" cap_snaps,
511*4882a593Smuzhiyun as no new writes are allowed to start when pending, so any
512*4882a593Smuzhiyun writes in progress now were started before the previous
513*4882a593Smuzhiyun cap_snap. lucky us. */
514*4882a593Smuzhiyun dout("queue_cap_snap %p already pending\n", inode);
515*4882a593Smuzhiyun goto update_snapc;
516*4882a593Smuzhiyun }
517*4882a593Smuzhiyun if (ci->i_wrbuffer_ref_head == 0 &&
518*4882a593Smuzhiyun !(dirty & (CEPH_CAP_ANY_EXCL|CEPH_CAP_FILE_WR))) {
519*4882a593Smuzhiyun dout("queue_cap_snap %p nothing dirty|writing\n", inode);
520*4882a593Smuzhiyun goto update_snapc;
521*4882a593Smuzhiyun }
522*4882a593Smuzhiyun
523*4882a593Smuzhiyun BUG_ON(!old_snapc);
524*4882a593Smuzhiyun
525*4882a593Smuzhiyun /*
526*4882a593Smuzhiyun * There is no need to send FLUSHSNAP message to MDS if there is
527*4882a593Smuzhiyun * no new snapshot. But when there is dirty pages or on-going
528*4882a593Smuzhiyun * writes, we still need to create cap_snap. cap_snap is needed
529*4882a593Smuzhiyun * by the write path and page writeback path.
530*4882a593Smuzhiyun *
531*4882a593Smuzhiyun * also see ceph_try_drop_cap_snap()
532*4882a593Smuzhiyun */
533*4882a593Smuzhiyun if (has_new_snaps(old_snapc, new_snapc)) {
534*4882a593Smuzhiyun if (dirty & (CEPH_CAP_ANY_EXCL|CEPH_CAP_FILE_WR))
535*4882a593Smuzhiyun capsnap->need_flush = true;
536*4882a593Smuzhiyun } else {
537*4882a593Smuzhiyun if (!(used & CEPH_CAP_FILE_WR) &&
538*4882a593Smuzhiyun ci->i_wrbuffer_ref_head == 0) {
539*4882a593Smuzhiyun dout("queue_cap_snap %p "
540*4882a593Smuzhiyun "no new_snap|dirty_page|writing\n", inode);
541*4882a593Smuzhiyun goto update_snapc;
542*4882a593Smuzhiyun }
543*4882a593Smuzhiyun }
544*4882a593Smuzhiyun
545*4882a593Smuzhiyun dout("queue_cap_snap %p cap_snap %p queuing under %p %s %s\n",
546*4882a593Smuzhiyun inode, capsnap, old_snapc, ceph_cap_string(dirty),
547*4882a593Smuzhiyun capsnap->need_flush ? "" : "no_flush");
548*4882a593Smuzhiyun ihold(inode);
549*4882a593Smuzhiyun
550*4882a593Smuzhiyun refcount_set(&capsnap->nref, 1);
551*4882a593Smuzhiyun INIT_LIST_HEAD(&capsnap->ci_item);
552*4882a593Smuzhiyun
553*4882a593Smuzhiyun capsnap->follows = old_snapc->seq;
554*4882a593Smuzhiyun capsnap->issued = __ceph_caps_issued(ci, NULL);
555*4882a593Smuzhiyun capsnap->dirty = dirty;
556*4882a593Smuzhiyun
557*4882a593Smuzhiyun capsnap->mode = inode->i_mode;
558*4882a593Smuzhiyun capsnap->uid = inode->i_uid;
559*4882a593Smuzhiyun capsnap->gid = inode->i_gid;
560*4882a593Smuzhiyun
561*4882a593Smuzhiyun if (dirty & CEPH_CAP_XATTR_EXCL) {
562*4882a593Smuzhiyun old_blob = __ceph_build_xattrs_blob(ci);
563*4882a593Smuzhiyun capsnap->xattr_blob =
564*4882a593Smuzhiyun ceph_buffer_get(ci->i_xattrs.blob);
565*4882a593Smuzhiyun capsnap->xattr_version = ci->i_xattrs.version;
566*4882a593Smuzhiyun } else {
567*4882a593Smuzhiyun capsnap->xattr_blob = NULL;
568*4882a593Smuzhiyun capsnap->xattr_version = 0;
569*4882a593Smuzhiyun }
570*4882a593Smuzhiyun
571*4882a593Smuzhiyun capsnap->inline_data = ci->i_inline_version != CEPH_INLINE_NONE;
572*4882a593Smuzhiyun
573*4882a593Smuzhiyun /* dirty page count moved from _head to this cap_snap;
574*4882a593Smuzhiyun all subsequent writes page dirties occur _after_ this
575*4882a593Smuzhiyun snapshot. */
576*4882a593Smuzhiyun capsnap->dirty_pages = ci->i_wrbuffer_ref_head;
577*4882a593Smuzhiyun ci->i_wrbuffer_ref_head = 0;
578*4882a593Smuzhiyun capsnap->context = old_snapc;
579*4882a593Smuzhiyun list_add_tail(&capsnap->ci_item, &ci->i_cap_snaps);
580*4882a593Smuzhiyun
581*4882a593Smuzhiyun if (used & CEPH_CAP_FILE_WR) {
582*4882a593Smuzhiyun dout("queue_cap_snap %p cap_snap %p snapc %p"
583*4882a593Smuzhiyun " seq %llu used WR, now pending\n", inode,
584*4882a593Smuzhiyun capsnap, old_snapc, old_snapc->seq);
585*4882a593Smuzhiyun capsnap->writing = 1;
586*4882a593Smuzhiyun } else {
587*4882a593Smuzhiyun /* note mtime, size NOW. */
588*4882a593Smuzhiyun __ceph_finish_cap_snap(ci, capsnap);
589*4882a593Smuzhiyun }
590*4882a593Smuzhiyun capsnap = NULL;
591*4882a593Smuzhiyun old_snapc = NULL;
592*4882a593Smuzhiyun
593*4882a593Smuzhiyun update_snapc:
594*4882a593Smuzhiyun if (ci->i_wrbuffer_ref_head == 0 &&
595*4882a593Smuzhiyun ci->i_wr_ref == 0 &&
596*4882a593Smuzhiyun ci->i_dirty_caps == 0 &&
597*4882a593Smuzhiyun ci->i_flushing_caps == 0) {
598*4882a593Smuzhiyun ci->i_head_snapc = NULL;
599*4882a593Smuzhiyun } else {
600*4882a593Smuzhiyun ci->i_head_snapc = ceph_get_snap_context(new_snapc);
601*4882a593Smuzhiyun dout(" new snapc is %p\n", new_snapc);
602*4882a593Smuzhiyun }
603*4882a593Smuzhiyun spin_unlock(&ci->i_ceph_lock);
604*4882a593Smuzhiyun
605*4882a593Smuzhiyun ceph_buffer_put(old_blob);
606*4882a593Smuzhiyun kfree(capsnap);
607*4882a593Smuzhiyun ceph_put_snap_context(old_snapc);
608*4882a593Smuzhiyun }
609*4882a593Smuzhiyun
610*4882a593Smuzhiyun /*
611*4882a593Smuzhiyun * Finalize the size, mtime for a cap_snap.. that is, settle on final values
612*4882a593Smuzhiyun * to be used for the snapshot, to be flushed back to the mds.
613*4882a593Smuzhiyun *
614*4882a593Smuzhiyun * If capsnap can now be flushed, add to snap_flush list, and return 1.
615*4882a593Smuzhiyun *
616*4882a593Smuzhiyun * Caller must hold i_ceph_lock.
617*4882a593Smuzhiyun */
__ceph_finish_cap_snap(struct ceph_inode_info * ci,struct ceph_cap_snap * capsnap)618*4882a593Smuzhiyun int __ceph_finish_cap_snap(struct ceph_inode_info *ci,
619*4882a593Smuzhiyun struct ceph_cap_snap *capsnap)
620*4882a593Smuzhiyun {
621*4882a593Smuzhiyun struct inode *inode = &ci->vfs_inode;
622*4882a593Smuzhiyun struct ceph_mds_client *mdsc = ceph_sb_to_mdsc(inode->i_sb);
623*4882a593Smuzhiyun
624*4882a593Smuzhiyun BUG_ON(capsnap->writing);
625*4882a593Smuzhiyun capsnap->size = inode->i_size;
626*4882a593Smuzhiyun capsnap->mtime = inode->i_mtime;
627*4882a593Smuzhiyun capsnap->atime = inode->i_atime;
628*4882a593Smuzhiyun capsnap->ctime = inode->i_ctime;
629*4882a593Smuzhiyun capsnap->btime = ci->i_btime;
630*4882a593Smuzhiyun capsnap->change_attr = inode_peek_iversion_raw(inode);
631*4882a593Smuzhiyun capsnap->time_warp_seq = ci->i_time_warp_seq;
632*4882a593Smuzhiyun capsnap->truncate_size = ci->i_truncate_size;
633*4882a593Smuzhiyun capsnap->truncate_seq = ci->i_truncate_seq;
634*4882a593Smuzhiyun if (capsnap->dirty_pages) {
635*4882a593Smuzhiyun dout("finish_cap_snap %p cap_snap %p snapc %p %llu %s s=%llu "
636*4882a593Smuzhiyun "still has %d dirty pages\n", inode, capsnap,
637*4882a593Smuzhiyun capsnap->context, capsnap->context->seq,
638*4882a593Smuzhiyun ceph_cap_string(capsnap->dirty), capsnap->size,
639*4882a593Smuzhiyun capsnap->dirty_pages);
640*4882a593Smuzhiyun return 0;
641*4882a593Smuzhiyun }
642*4882a593Smuzhiyun
643*4882a593Smuzhiyun ci->i_ceph_flags |= CEPH_I_FLUSH_SNAPS;
644*4882a593Smuzhiyun dout("finish_cap_snap %p cap_snap %p snapc %p %llu %s s=%llu\n",
645*4882a593Smuzhiyun inode, capsnap, capsnap->context,
646*4882a593Smuzhiyun capsnap->context->seq, ceph_cap_string(capsnap->dirty),
647*4882a593Smuzhiyun capsnap->size);
648*4882a593Smuzhiyun
649*4882a593Smuzhiyun spin_lock(&mdsc->snap_flush_lock);
650*4882a593Smuzhiyun if (list_empty(&ci->i_snap_flush_item))
651*4882a593Smuzhiyun list_add_tail(&ci->i_snap_flush_item, &mdsc->snap_flush_list);
652*4882a593Smuzhiyun spin_unlock(&mdsc->snap_flush_lock);
653*4882a593Smuzhiyun return 1; /* caller may want to ceph_flush_snaps */
654*4882a593Smuzhiyun }
655*4882a593Smuzhiyun
656*4882a593Smuzhiyun /*
657*4882a593Smuzhiyun * Queue cap_snaps for snap writeback for this realm and its children.
658*4882a593Smuzhiyun * Called under snap_rwsem, so realm topology won't change.
659*4882a593Smuzhiyun */
queue_realm_cap_snaps(struct ceph_snap_realm * realm)660*4882a593Smuzhiyun static void queue_realm_cap_snaps(struct ceph_snap_realm *realm)
661*4882a593Smuzhiyun {
662*4882a593Smuzhiyun struct ceph_inode_info *ci;
663*4882a593Smuzhiyun struct inode *lastinode = NULL;
664*4882a593Smuzhiyun
665*4882a593Smuzhiyun dout("queue_realm_cap_snaps %p %llx inodes\n", realm, realm->ino);
666*4882a593Smuzhiyun
667*4882a593Smuzhiyun spin_lock(&realm->inodes_with_caps_lock);
668*4882a593Smuzhiyun list_for_each_entry(ci, &realm->inodes_with_caps, i_snap_realm_item) {
669*4882a593Smuzhiyun struct inode *inode = igrab(&ci->vfs_inode);
670*4882a593Smuzhiyun if (!inode)
671*4882a593Smuzhiyun continue;
672*4882a593Smuzhiyun spin_unlock(&realm->inodes_with_caps_lock);
673*4882a593Smuzhiyun /* avoid calling iput_final() while holding
674*4882a593Smuzhiyun * mdsc->snap_rwsem or in mds dispatch threads */
675*4882a593Smuzhiyun ceph_async_iput(lastinode);
676*4882a593Smuzhiyun lastinode = inode;
677*4882a593Smuzhiyun ceph_queue_cap_snap(ci);
678*4882a593Smuzhiyun spin_lock(&realm->inodes_with_caps_lock);
679*4882a593Smuzhiyun }
680*4882a593Smuzhiyun spin_unlock(&realm->inodes_with_caps_lock);
681*4882a593Smuzhiyun ceph_async_iput(lastinode);
682*4882a593Smuzhiyun
683*4882a593Smuzhiyun dout("queue_realm_cap_snaps %p %llx done\n", realm, realm->ino);
684*4882a593Smuzhiyun }
685*4882a593Smuzhiyun
686*4882a593Smuzhiyun /*
687*4882a593Smuzhiyun * Parse and apply a snapblob "snap trace" from the MDS. This specifies
688*4882a593Smuzhiyun * the snap realm parameters from a given realm and all of its ancestors,
689*4882a593Smuzhiyun * up to the root.
690*4882a593Smuzhiyun *
691*4882a593Smuzhiyun * Caller must hold snap_rwsem for write.
692*4882a593Smuzhiyun */
ceph_update_snap_trace(struct ceph_mds_client * mdsc,void * p,void * e,bool deletion,struct ceph_snap_realm ** realm_ret)693*4882a593Smuzhiyun int ceph_update_snap_trace(struct ceph_mds_client *mdsc,
694*4882a593Smuzhiyun void *p, void *e, bool deletion,
695*4882a593Smuzhiyun struct ceph_snap_realm **realm_ret)
696*4882a593Smuzhiyun {
697*4882a593Smuzhiyun struct ceph_mds_snap_realm *ri; /* encoded */
698*4882a593Smuzhiyun __le64 *snaps; /* encoded */
699*4882a593Smuzhiyun __le64 *prior_parent_snaps; /* encoded */
700*4882a593Smuzhiyun struct ceph_snap_realm *realm;
701*4882a593Smuzhiyun struct ceph_snap_realm *first_realm = NULL;
702*4882a593Smuzhiyun struct ceph_snap_realm *realm_to_rebuild = NULL;
703*4882a593Smuzhiyun int rebuild_snapcs;
704*4882a593Smuzhiyun int err = -ENOMEM;
705*4882a593Smuzhiyun LIST_HEAD(dirty_realms);
706*4882a593Smuzhiyun
707*4882a593Smuzhiyun lockdep_assert_held_write(&mdsc->snap_rwsem);
708*4882a593Smuzhiyun
709*4882a593Smuzhiyun dout("update_snap_trace deletion=%d\n", deletion);
710*4882a593Smuzhiyun more:
711*4882a593Smuzhiyun realm = NULL;
712*4882a593Smuzhiyun rebuild_snapcs = 0;
713*4882a593Smuzhiyun ceph_decode_need(&p, e, sizeof(*ri), bad);
714*4882a593Smuzhiyun ri = p;
715*4882a593Smuzhiyun p += sizeof(*ri);
716*4882a593Smuzhiyun ceph_decode_need(&p, e, sizeof(u64)*(le32_to_cpu(ri->num_snaps) +
717*4882a593Smuzhiyun le32_to_cpu(ri->num_prior_parent_snaps)), bad);
718*4882a593Smuzhiyun snaps = p;
719*4882a593Smuzhiyun p += sizeof(u64) * le32_to_cpu(ri->num_snaps);
720*4882a593Smuzhiyun prior_parent_snaps = p;
721*4882a593Smuzhiyun p += sizeof(u64) * le32_to_cpu(ri->num_prior_parent_snaps);
722*4882a593Smuzhiyun
723*4882a593Smuzhiyun realm = ceph_lookup_snap_realm(mdsc, le64_to_cpu(ri->ino));
724*4882a593Smuzhiyun if (!realm) {
725*4882a593Smuzhiyun realm = ceph_create_snap_realm(mdsc, le64_to_cpu(ri->ino));
726*4882a593Smuzhiyun if (IS_ERR(realm)) {
727*4882a593Smuzhiyun err = PTR_ERR(realm);
728*4882a593Smuzhiyun goto fail;
729*4882a593Smuzhiyun }
730*4882a593Smuzhiyun }
731*4882a593Smuzhiyun
732*4882a593Smuzhiyun /* ensure the parent is correct */
733*4882a593Smuzhiyun err = adjust_snap_realm_parent(mdsc, realm, le64_to_cpu(ri->parent));
734*4882a593Smuzhiyun if (err < 0)
735*4882a593Smuzhiyun goto fail;
736*4882a593Smuzhiyun rebuild_snapcs += err;
737*4882a593Smuzhiyun
738*4882a593Smuzhiyun if (le64_to_cpu(ri->seq) > realm->seq) {
739*4882a593Smuzhiyun dout("update_snap_trace updating %llx %p %lld -> %lld\n",
740*4882a593Smuzhiyun realm->ino, realm, realm->seq, le64_to_cpu(ri->seq));
741*4882a593Smuzhiyun /* update realm parameters, snap lists */
742*4882a593Smuzhiyun realm->seq = le64_to_cpu(ri->seq);
743*4882a593Smuzhiyun realm->created = le64_to_cpu(ri->created);
744*4882a593Smuzhiyun realm->parent_since = le64_to_cpu(ri->parent_since);
745*4882a593Smuzhiyun
746*4882a593Smuzhiyun realm->num_snaps = le32_to_cpu(ri->num_snaps);
747*4882a593Smuzhiyun err = dup_array(&realm->snaps, snaps, realm->num_snaps);
748*4882a593Smuzhiyun if (err < 0)
749*4882a593Smuzhiyun goto fail;
750*4882a593Smuzhiyun
751*4882a593Smuzhiyun realm->num_prior_parent_snaps =
752*4882a593Smuzhiyun le32_to_cpu(ri->num_prior_parent_snaps);
753*4882a593Smuzhiyun err = dup_array(&realm->prior_parent_snaps, prior_parent_snaps,
754*4882a593Smuzhiyun realm->num_prior_parent_snaps);
755*4882a593Smuzhiyun if (err < 0)
756*4882a593Smuzhiyun goto fail;
757*4882a593Smuzhiyun
758*4882a593Smuzhiyun if (realm->seq > mdsc->last_snap_seq)
759*4882a593Smuzhiyun mdsc->last_snap_seq = realm->seq;
760*4882a593Smuzhiyun
761*4882a593Smuzhiyun rebuild_snapcs = 1;
762*4882a593Smuzhiyun } else if (!realm->cached_context) {
763*4882a593Smuzhiyun dout("update_snap_trace %llx %p seq %lld new\n",
764*4882a593Smuzhiyun realm->ino, realm, realm->seq);
765*4882a593Smuzhiyun rebuild_snapcs = 1;
766*4882a593Smuzhiyun } else {
767*4882a593Smuzhiyun dout("update_snap_trace %llx %p seq %lld unchanged\n",
768*4882a593Smuzhiyun realm->ino, realm, realm->seq);
769*4882a593Smuzhiyun }
770*4882a593Smuzhiyun
771*4882a593Smuzhiyun dout("done with %llx %p, rebuild_snapcs=%d, %p %p\n", realm->ino,
772*4882a593Smuzhiyun realm, rebuild_snapcs, p, e);
773*4882a593Smuzhiyun
774*4882a593Smuzhiyun /*
775*4882a593Smuzhiyun * this will always track the uppest parent realm from which
776*4882a593Smuzhiyun * we need to rebuild the snapshot contexts _downward_ in
777*4882a593Smuzhiyun * hierarchy.
778*4882a593Smuzhiyun */
779*4882a593Smuzhiyun if (rebuild_snapcs)
780*4882a593Smuzhiyun realm_to_rebuild = realm;
781*4882a593Smuzhiyun
782*4882a593Smuzhiyun /* rebuild_snapcs when we reach the _end_ (root) of the trace */
783*4882a593Smuzhiyun if (realm_to_rebuild && p >= e)
784*4882a593Smuzhiyun rebuild_snap_realms(realm_to_rebuild, &dirty_realms);
785*4882a593Smuzhiyun
786*4882a593Smuzhiyun if (!first_realm)
787*4882a593Smuzhiyun first_realm = realm;
788*4882a593Smuzhiyun else
789*4882a593Smuzhiyun ceph_put_snap_realm(mdsc, realm);
790*4882a593Smuzhiyun
791*4882a593Smuzhiyun if (p < e)
792*4882a593Smuzhiyun goto more;
793*4882a593Smuzhiyun
794*4882a593Smuzhiyun /*
795*4882a593Smuzhiyun * queue cap snaps _after_ we've built the new snap contexts,
796*4882a593Smuzhiyun * so that i_head_snapc can be set appropriately.
797*4882a593Smuzhiyun */
798*4882a593Smuzhiyun while (!list_empty(&dirty_realms)) {
799*4882a593Smuzhiyun realm = list_first_entry(&dirty_realms, struct ceph_snap_realm,
800*4882a593Smuzhiyun dirty_item);
801*4882a593Smuzhiyun list_del_init(&realm->dirty_item);
802*4882a593Smuzhiyun queue_realm_cap_snaps(realm);
803*4882a593Smuzhiyun }
804*4882a593Smuzhiyun
805*4882a593Smuzhiyun if (realm_ret)
806*4882a593Smuzhiyun *realm_ret = first_realm;
807*4882a593Smuzhiyun else
808*4882a593Smuzhiyun ceph_put_snap_realm(mdsc, first_realm);
809*4882a593Smuzhiyun
810*4882a593Smuzhiyun __cleanup_empty_realms(mdsc);
811*4882a593Smuzhiyun return 0;
812*4882a593Smuzhiyun
813*4882a593Smuzhiyun bad:
814*4882a593Smuzhiyun err = -EINVAL;
815*4882a593Smuzhiyun fail:
816*4882a593Smuzhiyun if (realm && !IS_ERR(realm))
817*4882a593Smuzhiyun ceph_put_snap_realm(mdsc, realm);
818*4882a593Smuzhiyun if (first_realm)
819*4882a593Smuzhiyun ceph_put_snap_realm(mdsc, first_realm);
820*4882a593Smuzhiyun pr_err("update_snap_trace error %d\n", err);
821*4882a593Smuzhiyun return err;
822*4882a593Smuzhiyun }
823*4882a593Smuzhiyun
824*4882a593Smuzhiyun
825*4882a593Smuzhiyun /*
826*4882a593Smuzhiyun * Send any cap_snaps that are queued for flush. Try to carry
827*4882a593Smuzhiyun * s_mutex across multiple snap flushes to avoid locking overhead.
828*4882a593Smuzhiyun *
829*4882a593Smuzhiyun * Caller holds no locks.
830*4882a593Smuzhiyun */
flush_snaps(struct ceph_mds_client * mdsc)831*4882a593Smuzhiyun static void flush_snaps(struct ceph_mds_client *mdsc)
832*4882a593Smuzhiyun {
833*4882a593Smuzhiyun struct ceph_inode_info *ci;
834*4882a593Smuzhiyun struct inode *inode;
835*4882a593Smuzhiyun struct ceph_mds_session *session = NULL;
836*4882a593Smuzhiyun
837*4882a593Smuzhiyun dout("flush_snaps\n");
838*4882a593Smuzhiyun spin_lock(&mdsc->snap_flush_lock);
839*4882a593Smuzhiyun while (!list_empty(&mdsc->snap_flush_list)) {
840*4882a593Smuzhiyun ci = list_first_entry(&mdsc->snap_flush_list,
841*4882a593Smuzhiyun struct ceph_inode_info, i_snap_flush_item);
842*4882a593Smuzhiyun inode = &ci->vfs_inode;
843*4882a593Smuzhiyun ihold(inode);
844*4882a593Smuzhiyun spin_unlock(&mdsc->snap_flush_lock);
845*4882a593Smuzhiyun ceph_flush_snaps(ci, &session);
846*4882a593Smuzhiyun /* avoid calling iput_final() while holding
847*4882a593Smuzhiyun * session->s_mutex or in mds dispatch threads */
848*4882a593Smuzhiyun ceph_async_iput(inode);
849*4882a593Smuzhiyun spin_lock(&mdsc->snap_flush_lock);
850*4882a593Smuzhiyun }
851*4882a593Smuzhiyun spin_unlock(&mdsc->snap_flush_lock);
852*4882a593Smuzhiyun
853*4882a593Smuzhiyun if (session) {
854*4882a593Smuzhiyun mutex_unlock(&session->s_mutex);
855*4882a593Smuzhiyun ceph_put_mds_session(session);
856*4882a593Smuzhiyun }
857*4882a593Smuzhiyun dout("flush_snaps done\n");
858*4882a593Smuzhiyun }
859*4882a593Smuzhiyun
860*4882a593Smuzhiyun
861*4882a593Smuzhiyun /*
862*4882a593Smuzhiyun * Handle a snap notification from the MDS.
863*4882a593Smuzhiyun *
864*4882a593Smuzhiyun * This can take two basic forms: the simplest is just a snap creation
865*4882a593Smuzhiyun * or deletion notification on an existing realm. This should update the
866*4882a593Smuzhiyun * realm and its children.
867*4882a593Smuzhiyun *
868*4882a593Smuzhiyun * The more difficult case is realm creation, due to snap creation at a
869*4882a593Smuzhiyun * new point in the file hierarchy, or due to a rename that moves a file or
870*4882a593Smuzhiyun * directory into another realm.
871*4882a593Smuzhiyun */
ceph_handle_snap(struct ceph_mds_client * mdsc,struct ceph_mds_session * session,struct ceph_msg * msg)872*4882a593Smuzhiyun void ceph_handle_snap(struct ceph_mds_client *mdsc,
873*4882a593Smuzhiyun struct ceph_mds_session *session,
874*4882a593Smuzhiyun struct ceph_msg *msg)
875*4882a593Smuzhiyun {
876*4882a593Smuzhiyun struct super_block *sb = mdsc->fsc->sb;
877*4882a593Smuzhiyun int mds = session->s_mds;
878*4882a593Smuzhiyun u64 split;
879*4882a593Smuzhiyun int op;
880*4882a593Smuzhiyun int trace_len;
881*4882a593Smuzhiyun struct ceph_snap_realm *realm = NULL;
882*4882a593Smuzhiyun void *p = msg->front.iov_base;
883*4882a593Smuzhiyun void *e = p + msg->front.iov_len;
884*4882a593Smuzhiyun struct ceph_mds_snap_head *h;
885*4882a593Smuzhiyun int num_split_inos, num_split_realms;
886*4882a593Smuzhiyun __le64 *split_inos = NULL, *split_realms = NULL;
887*4882a593Smuzhiyun int i;
888*4882a593Smuzhiyun int locked_rwsem = 0;
889*4882a593Smuzhiyun
890*4882a593Smuzhiyun /* decode */
891*4882a593Smuzhiyun if (msg->front.iov_len < sizeof(*h))
892*4882a593Smuzhiyun goto bad;
893*4882a593Smuzhiyun h = p;
894*4882a593Smuzhiyun op = le32_to_cpu(h->op);
895*4882a593Smuzhiyun split = le64_to_cpu(h->split); /* non-zero if we are splitting an
896*4882a593Smuzhiyun * existing realm */
897*4882a593Smuzhiyun num_split_inos = le32_to_cpu(h->num_split_inos);
898*4882a593Smuzhiyun num_split_realms = le32_to_cpu(h->num_split_realms);
899*4882a593Smuzhiyun trace_len = le32_to_cpu(h->trace_len);
900*4882a593Smuzhiyun p += sizeof(*h);
901*4882a593Smuzhiyun
902*4882a593Smuzhiyun dout("handle_snap from mds%d op %s split %llx tracelen %d\n", mds,
903*4882a593Smuzhiyun ceph_snap_op_name(op), split, trace_len);
904*4882a593Smuzhiyun
905*4882a593Smuzhiyun mutex_lock(&session->s_mutex);
906*4882a593Smuzhiyun inc_session_sequence(session);
907*4882a593Smuzhiyun mutex_unlock(&session->s_mutex);
908*4882a593Smuzhiyun
909*4882a593Smuzhiyun down_write(&mdsc->snap_rwsem);
910*4882a593Smuzhiyun locked_rwsem = 1;
911*4882a593Smuzhiyun
912*4882a593Smuzhiyun if (op == CEPH_SNAP_OP_SPLIT) {
913*4882a593Smuzhiyun struct ceph_mds_snap_realm *ri;
914*4882a593Smuzhiyun
915*4882a593Smuzhiyun /*
916*4882a593Smuzhiyun * A "split" breaks part of an existing realm off into
917*4882a593Smuzhiyun * a new realm. The MDS provides a list of inodes
918*4882a593Smuzhiyun * (with caps) and child realms that belong to the new
919*4882a593Smuzhiyun * child.
920*4882a593Smuzhiyun */
921*4882a593Smuzhiyun split_inos = p;
922*4882a593Smuzhiyun p += sizeof(u64) * num_split_inos;
923*4882a593Smuzhiyun split_realms = p;
924*4882a593Smuzhiyun p += sizeof(u64) * num_split_realms;
925*4882a593Smuzhiyun ceph_decode_need(&p, e, sizeof(*ri), bad);
926*4882a593Smuzhiyun /* we will peek at realm info here, but will _not_
927*4882a593Smuzhiyun * advance p, as the realm update will occur below in
928*4882a593Smuzhiyun * ceph_update_snap_trace. */
929*4882a593Smuzhiyun ri = p;
930*4882a593Smuzhiyun
931*4882a593Smuzhiyun realm = ceph_lookup_snap_realm(mdsc, split);
932*4882a593Smuzhiyun if (!realm) {
933*4882a593Smuzhiyun realm = ceph_create_snap_realm(mdsc, split);
934*4882a593Smuzhiyun if (IS_ERR(realm))
935*4882a593Smuzhiyun goto out;
936*4882a593Smuzhiyun }
937*4882a593Smuzhiyun
938*4882a593Smuzhiyun dout("splitting snap_realm %llx %p\n", realm->ino, realm);
939*4882a593Smuzhiyun for (i = 0; i < num_split_inos; i++) {
940*4882a593Smuzhiyun struct ceph_vino vino = {
941*4882a593Smuzhiyun .ino = le64_to_cpu(split_inos[i]),
942*4882a593Smuzhiyun .snap = CEPH_NOSNAP,
943*4882a593Smuzhiyun };
944*4882a593Smuzhiyun struct inode *inode = ceph_find_inode(sb, vino);
945*4882a593Smuzhiyun struct ceph_inode_info *ci;
946*4882a593Smuzhiyun struct ceph_snap_realm *oldrealm;
947*4882a593Smuzhiyun
948*4882a593Smuzhiyun if (!inode)
949*4882a593Smuzhiyun continue;
950*4882a593Smuzhiyun ci = ceph_inode(inode);
951*4882a593Smuzhiyun
952*4882a593Smuzhiyun spin_lock(&ci->i_ceph_lock);
953*4882a593Smuzhiyun if (!ci->i_snap_realm)
954*4882a593Smuzhiyun goto skip_inode;
955*4882a593Smuzhiyun /*
956*4882a593Smuzhiyun * If this inode belongs to a realm that was
957*4882a593Smuzhiyun * created after our new realm, we experienced
958*4882a593Smuzhiyun * a race (due to another split notifications
959*4882a593Smuzhiyun * arriving from a different MDS). So skip
960*4882a593Smuzhiyun * this inode.
961*4882a593Smuzhiyun */
962*4882a593Smuzhiyun if (ci->i_snap_realm->created >
963*4882a593Smuzhiyun le64_to_cpu(ri->created)) {
964*4882a593Smuzhiyun dout(" leaving %p in newer realm %llx %p\n",
965*4882a593Smuzhiyun inode, ci->i_snap_realm->ino,
966*4882a593Smuzhiyun ci->i_snap_realm);
967*4882a593Smuzhiyun goto skip_inode;
968*4882a593Smuzhiyun }
969*4882a593Smuzhiyun dout(" will move %p to split realm %llx %p\n",
970*4882a593Smuzhiyun inode, realm->ino, realm);
971*4882a593Smuzhiyun /*
972*4882a593Smuzhiyun * Move the inode to the new realm
973*4882a593Smuzhiyun */
974*4882a593Smuzhiyun oldrealm = ci->i_snap_realm;
975*4882a593Smuzhiyun spin_lock(&oldrealm->inodes_with_caps_lock);
976*4882a593Smuzhiyun list_del_init(&ci->i_snap_realm_item);
977*4882a593Smuzhiyun spin_unlock(&oldrealm->inodes_with_caps_lock);
978*4882a593Smuzhiyun
979*4882a593Smuzhiyun spin_lock(&realm->inodes_with_caps_lock);
980*4882a593Smuzhiyun list_add(&ci->i_snap_realm_item,
981*4882a593Smuzhiyun &realm->inodes_with_caps);
982*4882a593Smuzhiyun ci->i_snap_realm = realm;
983*4882a593Smuzhiyun if (realm->ino == ci->i_vino.ino)
984*4882a593Smuzhiyun realm->inode = inode;
985*4882a593Smuzhiyun spin_unlock(&realm->inodes_with_caps_lock);
986*4882a593Smuzhiyun
987*4882a593Smuzhiyun spin_unlock(&ci->i_ceph_lock);
988*4882a593Smuzhiyun
989*4882a593Smuzhiyun ceph_get_snap_realm(mdsc, realm);
990*4882a593Smuzhiyun ceph_put_snap_realm(mdsc, oldrealm);
991*4882a593Smuzhiyun
992*4882a593Smuzhiyun /* avoid calling iput_final() while holding
993*4882a593Smuzhiyun * mdsc->snap_rwsem or mds in dispatch threads */
994*4882a593Smuzhiyun ceph_async_iput(inode);
995*4882a593Smuzhiyun continue;
996*4882a593Smuzhiyun
997*4882a593Smuzhiyun skip_inode:
998*4882a593Smuzhiyun spin_unlock(&ci->i_ceph_lock);
999*4882a593Smuzhiyun ceph_async_iput(inode);
1000*4882a593Smuzhiyun }
1001*4882a593Smuzhiyun
1002*4882a593Smuzhiyun /* we may have taken some of the old realm's children. */
1003*4882a593Smuzhiyun for (i = 0; i < num_split_realms; i++) {
1004*4882a593Smuzhiyun struct ceph_snap_realm *child =
1005*4882a593Smuzhiyun __lookup_snap_realm(mdsc,
1006*4882a593Smuzhiyun le64_to_cpu(split_realms[i]));
1007*4882a593Smuzhiyun if (!child)
1008*4882a593Smuzhiyun continue;
1009*4882a593Smuzhiyun adjust_snap_realm_parent(mdsc, child, realm->ino);
1010*4882a593Smuzhiyun }
1011*4882a593Smuzhiyun }
1012*4882a593Smuzhiyun
1013*4882a593Smuzhiyun /*
1014*4882a593Smuzhiyun * update using the provided snap trace. if we are deleting a
1015*4882a593Smuzhiyun * snap, we can avoid queueing cap_snaps.
1016*4882a593Smuzhiyun */
1017*4882a593Smuzhiyun ceph_update_snap_trace(mdsc, p, e,
1018*4882a593Smuzhiyun op == CEPH_SNAP_OP_DESTROY, NULL);
1019*4882a593Smuzhiyun
1020*4882a593Smuzhiyun if (op == CEPH_SNAP_OP_SPLIT)
1021*4882a593Smuzhiyun /* we took a reference when we created the realm, above */
1022*4882a593Smuzhiyun ceph_put_snap_realm(mdsc, realm);
1023*4882a593Smuzhiyun
1024*4882a593Smuzhiyun __cleanup_empty_realms(mdsc);
1025*4882a593Smuzhiyun
1026*4882a593Smuzhiyun up_write(&mdsc->snap_rwsem);
1027*4882a593Smuzhiyun
1028*4882a593Smuzhiyun flush_snaps(mdsc);
1029*4882a593Smuzhiyun return;
1030*4882a593Smuzhiyun
1031*4882a593Smuzhiyun bad:
1032*4882a593Smuzhiyun pr_err("corrupt snap message from mds%d\n", mds);
1033*4882a593Smuzhiyun ceph_msg_dump(msg);
1034*4882a593Smuzhiyun out:
1035*4882a593Smuzhiyun if (locked_rwsem)
1036*4882a593Smuzhiyun up_write(&mdsc->snap_rwsem);
1037*4882a593Smuzhiyun return;
1038*4882a593Smuzhiyun }
1039*4882a593Smuzhiyun
ceph_get_snapid_map(struct ceph_mds_client * mdsc,u64 snap)1040*4882a593Smuzhiyun struct ceph_snapid_map* ceph_get_snapid_map(struct ceph_mds_client *mdsc,
1041*4882a593Smuzhiyun u64 snap)
1042*4882a593Smuzhiyun {
1043*4882a593Smuzhiyun struct ceph_snapid_map *sm, *exist;
1044*4882a593Smuzhiyun struct rb_node **p, *parent;
1045*4882a593Smuzhiyun int ret;
1046*4882a593Smuzhiyun
1047*4882a593Smuzhiyun exist = NULL;
1048*4882a593Smuzhiyun spin_lock(&mdsc->snapid_map_lock);
1049*4882a593Smuzhiyun p = &mdsc->snapid_map_tree.rb_node;
1050*4882a593Smuzhiyun while (*p) {
1051*4882a593Smuzhiyun exist = rb_entry(*p, struct ceph_snapid_map, node);
1052*4882a593Smuzhiyun if (snap > exist->snap) {
1053*4882a593Smuzhiyun p = &(*p)->rb_left;
1054*4882a593Smuzhiyun } else if (snap < exist->snap) {
1055*4882a593Smuzhiyun p = &(*p)->rb_right;
1056*4882a593Smuzhiyun } else {
1057*4882a593Smuzhiyun if (atomic_inc_return(&exist->ref) == 1)
1058*4882a593Smuzhiyun list_del_init(&exist->lru);
1059*4882a593Smuzhiyun break;
1060*4882a593Smuzhiyun }
1061*4882a593Smuzhiyun exist = NULL;
1062*4882a593Smuzhiyun }
1063*4882a593Smuzhiyun spin_unlock(&mdsc->snapid_map_lock);
1064*4882a593Smuzhiyun if (exist) {
1065*4882a593Smuzhiyun dout("found snapid map %llx -> %x\n", exist->snap, exist->dev);
1066*4882a593Smuzhiyun return exist;
1067*4882a593Smuzhiyun }
1068*4882a593Smuzhiyun
1069*4882a593Smuzhiyun sm = kmalloc(sizeof(*sm), GFP_NOFS);
1070*4882a593Smuzhiyun if (!sm)
1071*4882a593Smuzhiyun return NULL;
1072*4882a593Smuzhiyun
1073*4882a593Smuzhiyun ret = get_anon_bdev(&sm->dev);
1074*4882a593Smuzhiyun if (ret < 0) {
1075*4882a593Smuzhiyun kfree(sm);
1076*4882a593Smuzhiyun return NULL;
1077*4882a593Smuzhiyun }
1078*4882a593Smuzhiyun
1079*4882a593Smuzhiyun INIT_LIST_HEAD(&sm->lru);
1080*4882a593Smuzhiyun atomic_set(&sm->ref, 1);
1081*4882a593Smuzhiyun sm->snap = snap;
1082*4882a593Smuzhiyun
1083*4882a593Smuzhiyun exist = NULL;
1084*4882a593Smuzhiyun parent = NULL;
1085*4882a593Smuzhiyun p = &mdsc->snapid_map_tree.rb_node;
1086*4882a593Smuzhiyun spin_lock(&mdsc->snapid_map_lock);
1087*4882a593Smuzhiyun while (*p) {
1088*4882a593Smuzhiyun parent = *p;
1089*4882a593Smuzhiyun exist = rb_entry(*p, struct ceph_snapid_map, node);
1090*4882a593Smuzhiyun if (snap > exist->snap)
1091*4882a593Smuzhiyun p = &(*p)->rb_left;
1092*4882a593Smuzhiyun else if (snap < exist->snap)
1093*4882a593Smuzhiyun p = &(*p)->rb_right;
1094*4882a593Smuzhiyun else
1095*4882a593Smuzhiyun break;
1096*4882a593Smuzhiyun exist = NULL;
1097*4882a593Smuzhiyun }
1098*4882a593Smuzhiyun if (exist) {
1099*4882a593Smuzhiyun if (atomic_inc_return(&exist->ref) == 1)
1100*4882a593Smuzhiyun list_del_init(&exist->lru);
1101*4882a593Smuzhiyun } else {
1102*4882a593Smuzhiyun rb_link_node(&sm->node, parent, p);
1103*4882a593Smuzhiyun rb_insert_color(&sm->node, &mdsc->snapid_map_tree);
1104*4882a593Smuzhiyun }
1105*4882a593Smuzhiyun spin_unlock(&mdsc->snapid_map_lock);
1106*4882a593Smuzhiyun if (exist) {
1107*4882a593Smuzhiyun free_anon_bdev(sm->dev);
1108*4882a593Smuzhiyun kfree(sm);
1109*4882a593Smuzhiyun dout("found snapid map %llx -> %x\n", exist->snap, exist->dev);
1110*4882a593Smuzhiyun return exist;
1111*4882a593Smuzhiyun }
1112*4882a593Smuzhiyun
1113*4882a593Smuzhiyun dout("create snapid map %llx -> %x\n", sm->snap, sm->dev);
1114*4882a593Smuzhiyun return sm;
1115*4882a593Smuzhiyun }
1116*4882a593Smuzhiyun
ceph_put_snapid_map(struct ceph_mds_client * mdsc,struct ceph_snapid_map * sm)1117*4882a593Smuzhiyun void ceph_put_snapid_map(struct ceph_mds_client* mdsc,
1118*4882a593Smuzhiyun struct ceph_snapid_map *sm)
1119*4882a593Smuzhiyun {
1120*4882a593Smuzhiyun if (!sm)
1121*4882a593Smuzhiyun return;
1122*4882a593Smuzhiyun if (atomic_dec_and_lock(&sm->ref, &mdsc->snapid_map_lock)) {
1123*4882a593Smuzhiyun if (!RB_EMPTY_NODE(&sm->node)) {
1124*4882a593Smuzhiyun sm->last_used = jiffies;
1125*4882a593Smuzhiyun list_add_tail(&sm->lru, &mdsc->snapid_map_lru);
1126*4882a593Smuzhiyun spin_unlock(&mdsc->snapid_map_lock);
1127*4882a593Smuzhiyun } else {
1128*4882a593Smuzhiyun /* already cleaned up by
1129*4882a593Smuzhiyun * ceph_cleanup_snapid_map() */
1130*4882a593Smuzhiyun spin_unlock(&mdsc->snapid_map_lock);
1131*4882a593Smuzhiyun kfree(sm);
1132*4882a593Smuzhiyun }
1133*4882a593Smuzhiyun }
1134*4882a593Smuzhiyun }
1135*4882a593Smuzhiyun
ceph_trim_snapid_map(struct ceph_mds_client * mdsc)1136*4882a593Smuzhiyun void ceph_trim_snapid_map(struct ceph_mds_client *mdsc)
1137*4882a593Smuzhiyun {
1138*4882a593Smuzhiyun struct ceph_snapid_map *sm;
1139*4882a593Smuzhiyun unsigned long now;
1140*4882a593Smuzhiyun LIST_HEAD(to_free);
1141*4882a593Smuzhiyun
1142*4882a593Smuzhiyun spin_lock(&mdsc->snapid_map_lock);
1143*4882a593Smuzhiyun now = jiffies;
1144*4882a593Smuzhiyun
1145*4882a593Smuzhiyun while (!list_empty(&mdsc->snapid_map_lru)) {
1146*4882a593Smuzhiyun sm = list_first_entry(&mdsc->snapid_map_lru,
1147*4882a593Smuzhiyun struct ceph_snapid_map, lru);
1148*4882a593Smuzhiyun if (time_after(sm->last_used + CEPH_SNAPID_MAP_TIMEOUT, now))
1149*4882a593Smuzhiyun break;
1150*4882a593Smuzhiyun
1151*4882a593Smuzhiyun rb_erase(&sm->node, &mdsc->snapid_map_tree);
1152*4882a593Smuzhiyun list_move(&sm->lru, &to_free);
1153*4882a593Smuzhiyun }
1154*4882a593Smuzhiyun spin_unlock(&mdsc->snapid_map_lock);
1155*4882a593Smuzhiyun
1156*4882a593Smuzhiyun while (!list_empty(&to_free)) {
1157*4882a593Smuzhiyun sm = list_first_entry(&to_free, struct ceph_snapid_map, lru);
1158*4882a593Smuzhiyun list_del(&sm->lru);
1159*4882a593Smuzhiyun dout("trim snapid map %llx -> %x\n", sm->snap, sm->dev);
1160*4882a593Smuzhiyun free_anon_bdev(sm->dev);
1161*4882a593Smuzhiyun kfree(sm);
1162*4882a593Smuzhiyun }
1163*4882a593Smuzhiyun }
1164*4882a593Smuzhiyun
ceph_cleanup_snapid_map(struct ceph_mds_client * mdsc)1165*4882a593Smuzhiyun void ceph_cleanup_snapid_map(struct ceph_mds_client *mdsc)
1166*4882a593Smuzhiyun {
1167*4882a593Smuzhiyun struct ceph_snapid_map *sm;
1168*4882a593Smuzhiyun struct rb_node *p;
1169*4882a593Smuzhiyun LIST_HEAD(to_free);
1170*4882a593Smuzhiyun
1171*4882a593Smuzhiyun spin_lock(&mdsc->snapid_map_lock);
1172*4882a593Smuzhiyun while ((p = rb_first(&mdsc->snapid_map_tree))) {
1173*4882a593Smuzhiyun sm = rb_entry(p, struct ceph_snapid_map, node);
1174*4882a593Smuzhiyun rb_erase(p, &mdsc->snapid_map_tree);
1175*4882a593Smuzhiyun RB_CLEAR_NODE(p);
1176*4882a593Smuzhiyun list_move(&sm->lru, &to_free);
1177*4882a593Smuzhiyun }
1178*4882a593Smuzhiyun spin_unlock(&mdsc->snapid_map_lock);
1179*4882a593Smuzhiyun
1180*4882a593Smuzhiyun while (!list_empty(&to_free)) {
1181*4882a593Smuzhiyun sm = list_first_entry(&to_free, struct ceph_snapid_map, lru);
1182*4882a593Smuzhiyun list_del(&sm->lru);
1183*4882a593Smuzhiyun free_anon_bdev(sm->dev);
1184*4882a593Smuzhiyun if (WARN_ON_ONCE(atomic_read(&sm->ref))) {
1185*4882a593Smuzhiyun pr_err("snapid map %llx -> %x still in use\n",
1186*4882a593Smuzhiyun sm->snap, sm->dev);
1187*4882a593Smuzhiyun }
1188*4882a593Smuzhiyun kfree(sm);
1189*4882a593Smuzhiyun }
1190*4882a593Smuzhiyun }
1191