Merge git://git.kernel.org/pub/scm/linux/kernel/git/jejb/scsi-rc-fixes-2.6
[pandora-kernel.git] / fs / ceph / snap.c
1 #include "ceph_debug.h"
2
3 #include <linux/sort.h>
4 #include <linux/slab.h>
5
6 #include "super.h"
7 #include "decode.h"
8
9 /*
10  * Snapshots in ceph are driven in large part by cooperation from the
11  * client.  In contrast to local file systems or file servers that
12  * implement snapshots at a single point in the system, ceph's
13  * distributed access to storage requires clients to help decide
14  * whether a write logically occurs before or after a recently created
15  * snapshot.
16  *
17  * This provides a perfect instantanous client-wide snapshot.  Between
18  * clients, however, snapshots may appear to be applied at slightly
19  * different points in time, depending on delays in delivering the
20  * snapshot notification.
21  *
22  * Snapshots are _not_ file system-wide.  Instead, each snapshot
23  * applies to the subdirectory nested beneath some directory.  This
24  * effectively divides the hierarchy into multiple "realms," where all
25  * of the files contained by each realm share the same set of
26  * snapshots.  An individual realm's snap set contains snapshots
27  * explicitly created on that realm, as well as any snaps in its
28  * parent's snap set _after_ the point at which the parent became it's
29  * parent (due to, say, a rename).  Similarly, snaps from prior parents
30  * during the time intervals during which they were the parent are included.
31  *
32  * The client is spared most of this detail, fortunately... it must only
33  * maintains a hierarchy of realms reflecting the current parent/child
34  * realm relationship, and for each realm has an explicit list of snaps
35  * inherited from prior parents.
36  *
37  * A snap_realm struct is maintained for realms containing every inode
38  * with an open cap in the system.  (The needed snap realm information is
39  * provided by the MDS whenever a cap is issued, i.e., on open.)  A 'seq'
40  * version number is used to ensure that as realm parameters change (new
41  * snapshot, new parent, etc.) the client's realm hierarchy is updated.
42  *
43  * The realm hierarchy drives the generation of a 'snap context' for each
44  * realm, which simply lists the resulting set of snaps for the realm.  This
45  * is attached to any writes sent to OSDs.
46  */
47 /*
48  * Unfortunately error handling is a bit mixed here.  If we get a snap
49  * update, but don't have enough memory to update our realm hierarchy,
50  * it's not clear what we can do about it (besides complaining to the
51  * console).
52  */
53
54
55 /*
56  * increase ref count for the realm
57  *
58  * caller must hold snap_rwsem for write.
59  */
60 void ceph_get_snap_realm(struct ceph_mds_client *mdsc,
61                          struct ceph_snap_realm *realm)
62 {
63         dout("get_realm %p %d -> %d\n", realm,
64              atomic_read(&realm->nref), atomic_read(&realm->nref)+1);
65         /*
66          * since we _only_ increment realm refs or empty the empty
67          * list with snap_rwsem held, adjusting the empty list here is
68          * safe.  we do need to protect against concurrent empty list
69          * additions, however.
70          */
71         if (atomic_read(&realm->nref) == 0) {
72                 spin_lock(&mdsc->snap_empty_lock);
73                 list_del_init(&realm->empty_item);
74                 spin_unlock(&mdsc->snap_empty_lock);
75         }
76
77         atomic_inc(&realm->nref);
78 }
79
80 static void __insert_snap_realm(struct rb_root *root,
81                                 struct ceph_snap_realm *new)
82 {
83         struct rb_node **p = &root->rb_node;
84         struct rb_node *parent = NULL;
85         struct ceph_snap_realm *r = NULL;
86
87         while (*p) {
88                 parent = *p;
89                 r = rb_entry(parent, struct ceph_snap_realm, node);
90                 if (new->ino < r->ino)
91                         p = &(*p)->rb_left;
92                 else if (new->ino > r->ino)
93                         p = &(*p)->rb_right;
94                 else
95                         BUG();
96         }
97
98         rb_link_node(&new->node, parent, p);
99         rb_insert_color(&new->node, root);
100 }
101
102 /*
103  * create and get the realm rooted at @ino and bump its ref count.
104  *
105  * caller must hold snap_rwsem for write.
106  */
107 static struct ceph_snap_realm *ceph_create_snap_realm(
108         struct ceph_mds_client *mdsc,
109         u64 ino)
110 {
111         struct ceph_snap_realm *realm;
112
113         realm = kzalloc(sizeof(*realm), GFP_NOFS);
114         if (!realm)
115                 return ERR_PTR(-ENOMEM);
116
117         atomic_set(&realm->nref, 0);    /* tree does not take a ref */
118         realm->ino = ino;
119         INIT_LIST_HEAD(&realm->children);
120         INIT_LIST_HEAD(&realm->child_item);
121         INIT_LIST_HEAD(&realm->empty_item);
122         INIT_LIST_HEAD(&realm->dirty_item);
123         INIT_LIST_HEAD(&realm->inodes_with_caps);
124         spin_lock_init(&realm->inodes_with_caps_lock);
125         __insert_snap_realm(&mdsc->snap_realms, realm);
126         dout("create_snap_realm %llx %p\n", realm->ino, realm);
127         return realm;
128 }
129
130 /*
131  * lookup the realm rooted at @ino.
132  *
133  * caller must hold snap_rwsem for write.
134  */
135 struct ceph_snap_realm *ceph_lookup_snap_realm(struct ceph_mds_client *mdsc,
136                                                u64 ino)
137 {
138         struct rb_node *n = mdsc->snap_realms.rb_node;
139         struct ceph_snap_realm *r;
140
141         while (n) {
142                 r = rb_entry(n, struct ceph_snap_realm, node);
143                 if (ino < r->ino)
144                         n = n->rb_left;
145                 else if (ino > r->ino)
146                         n = n->rb_right;
147                 else {
148                         dout("lookup_snap_realm %llx %p\n", r->ino, r);
149                         return r;
150                 }
151         }
152         return NULL;
153 }
154
155 static void __put_snap_realm(struct ceph_mds_client *mdsc,
156                              struct ceph_snap_realm *realm);
157
158 /*
159  * called with snap_rwsem (write)
160  */
161 static void __destroy_snap_realm(struct ceph_mds_client *mdsc,
162                                  struct ceph_snap_realm *realm)
163 {
164         dout("__destroy_snap_realm %p %llx\n", realm, realm->ino);
165
166         rb_erase(&realm->node, &mdsc->snap_realms);
167
168         if (realm->parent) {
169                 list_del_init(&realm->child_item);
170                 __put_snap_realm(mdsc, realm->parent);
171         }
172
173         kfree(realm->prior_parent_snaps);
174         kfree(realm->snaps);
175         ceph_put_snap_context(realm->cached_context);
176         kfree(realm);
177 }
178
179 /*
180  * caller holds snap_rwsem (write)
181  */
182 static void __put_snap_realm(struct ceph_mds_client *mdsc,
183                              struct ceph_snap_realm *realm)
184 {
185         dout("__put_snap_realm %llx %p %d -> %d\n", realm->ino, realm,
186              atomic_read(&realm->nref), atomic_read(&realm->nref)-1);
187         if (atomic_dec_and_test(&realm->nref))
188                 __destroy_snap_realm(mdsc, realm);
189 }
190
191 /*
192  * caller needn't hold any locks
193  */
194 void ceph_put_snap_realm(struct ceph_mds_client *mdsc,
195                          struct ceph_snap_realm *realm)
196 {
197         dout("put_snap_realm %llx %p %d -> %d\n", realm->ino, realm,
198              atomic_read(&realm->nref), atomic_read(&realm->nref)-1);
199         if (!atomic_dec_and_test(&realm->nref))
200                 return;
201
202         if (down_write_trylock(&mdsc->snap_rwsem)) {
203                 __destroy_snap_realm(mdsc, realm);
204                 up_write(&mdsc->snap_rwsem);
205         } else {
206                 spin_lock(&mdsc->snap_empty_lock);
207                 list_add(&mdsc->snap_empty, &realm->empty_item);
208                 spin_unlock(&mdsc->snap_empty_lock);
209         }
210 }
211
212 /*
213  * Clean up any realms whose ref counts have dropped to zero.  Note
214  * that this does not include realms who were created but not yet
215  * used.
216  *
217  * Called under snap_rwsem (write)
218  */
219 static void __cleanup_empty_realms(struct ceph_mds_client *mdsc)
220 {
221         struct ceph_snap_realm *realm;
222
223         spin_lock(&mdsc->snap_empty_lock);
224         while (!list_empty(&mdsc->snap_empty)) {
225                 realm = list_first_entry(&mdsc->snap_empty,
226                                    struct ceph_snap_realm, empty_item);
227                 list_del(&realm->empty_item);
228                 spin_unlock(&mdsc->snap_empty_lock);
229                 __destroy_snap_realm(mdsc, realm);
230                 spin_lock(&mdsc->snap_empty_lock);
231         }
232         spin_unlock(&mdsc->snap_empty_lock);
233 }
234
235 void ceph_cleanup_empty_realms(struct ceph_mds_client *mdsc)
236 {
237         down_write(&mdsc->snap_rwsem);
238         __cleanup_empty_realms(mdsc);
239         up_write(&mdsc->snap_rwsem);
240 }
241
242 /*
243  * adjust the parent realm of a given @realm.  adjust child list, and parent
244  * pointers, and ref counts appropriately.
245  *
246  * return true if parent was changed, 0 if unchanged, <0 on error.
247  *
248  * caller must hold snap_rwsem for write.
249  */
250 static int adjust_snap_realm_parent(struct ceph_mds_client *mdsc,
251                                     struct ceph_snap_realm *realm,
252                                     u64 parentino)
253 {
254         struct ceph_snap_realm *parent;
255
256         if (realm->parent_ino == parentino)
257                 return 0;
258
259         parent = ceph_lookup_snap_realm(mdsc, parentino);
260         if (!parent) {
261                 parent = ceph_create_snap_realm(mdsc, parentino);
262                 if (IS_ERR(parent))
263                         return PTR_ERR(parent);
264         }
265         dout("adjust_snap_realm_parent %llx %p: %llx %p -> %llx %p\n",
266              realm->ino, realm, realm->parent_ino, realm->parent,
267              parentino, parent);
268         if (realm->parent) {
269                 list_del_init(&realm->child_item);
270                 ceph_put_snap_realm(mdsc, realm->parent);
271         }
272         realm->parent_ino = parentino;
273         realm->parent = parent;
274         ceph_get_snap_realm(mdsc, parent);
275         list_add(&realm->child_item, &parent->children);
276         return 1;
277 }
278
279
280 static int cmpu64_rev(const void *a, const void *b)
281 {
282         if (*(u64 *)a < *(u64 *)b)
283                 return 1;
284         if (*(u64 *)a > *(u64 *)b)
285                 return -1;
286         return 0;
287 }
288
289 /*
290  * build the snap context for a given realm.
291  */
292 static int build_snap_context(struct ceph_snap_realm *realm)
293 {
294         struct ceph_snap_realm *parent = realm->parent;
295         struct ceph_snap_context *snapc;
296         int err = 0;
297         int i;
298         int num = realm->num_prior_parent_snaps + realm->num_snaps;
299
300         /*
301          * build parent context, if it hasn't been built.
302          * conservatively estimate that all parent snaps might be
303          * included by us.
304          */
305         if (parent) {
306                 if (!parent->cached_context) {
307                         err = build_snap_context(parent);
308                         if (err)
309                                 goto fail;
310                 }
311                 num += parent->cached_context->num_snaps;
312         }
313
314         /* do i actually need to update?  not if my context seq
315            matches realm seq, and my parents' does to.  (this works
316            because we rebuild_snap_realms() works _downward_ in
317            hierarchy after each update.) */
318         if (realm->cached_context &&
319             realm->cached_context->seq == realm->seq &&
320             (!parent ||
321              realm->cached_context->seq >= parent->cached_context->seq)) {
322                 dout("build_snap_context %llx %p: %p seq %lld (%d snaps)"
323                      " (unchanged)\n",
324                      realm->ino, realm, realm->cached_context,
325                      realm->cached_context->seq,
326                      realm->cached_context->num_snaps);
327                 return 0;
328         }
329
330         /* alloc new snap context */
331         err = -ENOMEM;
332         if (num > ULONG_MAX / sizeof(u64) - sizeof(*snapc))
333                 goto fail;
334         snapc = kzalloc(sizeof(*snapc) + num*sizeof(u64), GFP_NOFS);
335         if (!snapc)
336                 goto fail;
337         atomic_set(&snapc->nref, 1);
338
339         /* build (reverse sorted) snap vector */
340         num = 0;
341         snapc->seq = realm->seq;
342         if (parent) {
343                 /* include any of parent's snaps occuring _after_ my
344                    parent became my parent */
345                 for (i = 0; i < parent->cached_context->num_snaps; i++)
346                         if (parent->cached_context->snaps[i] >=
347                             realm->parent_since)
348                                 snapc->snaps[num++] =
349                                         parent->cached_context->snaps[i];
350                 if (parent->cached_context->seq > snapc->seq)
351                         snapc->seq = parent->cached_context->seq;
352         }
353         memcpy(snapc->snaps + num, realm->snaps,
354                sizeof(u64)*realm->num_snaps);
355         num += realm->num_snaps;
356         memcpy(snapc->snaps + num, realm->prior_parent_snaps,
357                sizeof(u64)*realm->num_prior_parent_snaps);
358         num += realm->num_prior_parent_snaps;
359
360         sort(snapc->snaps, num, sizeof(u64), cmpu64_rev, NULL);
361         snapc->num_snaps = num;
362         dout("build_snap_context %llx %p: %p seq %lld (%d snaps)\n",
363              realm->ino, realm, snapc, snapc->seq, snapc->num_snaps);
364
365         if (realm->cached_context)
366                 ceph_put_snap_context(realm->cached_context);
367         realm->cached_context = snapc;
368         return 0;
369
370 fail:
371         /*
372          * if we fail, clear old (incorrect) cached_context... hopefully
373          * we'll have better luck building it later
374          */
375         if (realm->cached_context) {
376                 ceph_put_snap_context(realm->cached_context);
377                 realm->cached_context = NULL;
378         }
379         pr_err("build_snap_context %llx %p fail %d\n", realm->ino,
380                realm, err);
381         return err;
382 }
383
384 /*
385  * rebuild snap context for the given realm and all of its children.
386  */
387 static void rebuild_snap_realms(struct ceph_snap_realm *realm)
388 {
389         struct ceph_snap_realm *child;
390
391         dout("rebuild_snap_realms %llx %p\n", realm->ino, realm);
392         build_snap_context(realm);
393
394         list_for_each_entry(child, &realm->children, child_item)
395                 rebuild_snap_realms(child);
396 }
397
398
399 /*
400  * helper to allocate and decode an array of snapids.  free prior
401  * instance, if any.
402  */
403 static int dup_array(u64 **dst, __le64 *src, int num)
404 {
405         int i;
406
407         kfree(*dst);
408         if (num) {
409                 *dst = kcalloc(num, sizeof(u64), GFP_NOFS);
410                 if (!*dst)
411                         return -ENOMEM;
412                 for (i = 0; i < num; i++)
413                         (*dst)[i] = get_unaligned_le64(src + i);
414         } else {
415                 *dst = NULL;
416         }
417         return 0;
418 }
419
420
421 /*
422  * When a snapshot is applied, the size/mtime inode metadata is queued
423  * in a ceph_cap_snap (one for each snapshot) until writeback
424  * completes and the metadata can be flushed back to the MDS.
425  *
426  * However, if a (sync) write is currently in-progress when we apply
427  * the snapshot, we have to wait until the write succeeds or fails
428  * (and a final size/mtime is known).  In this case the
429  * cap_snap->writing = 1, and is said to be "pending."  When the write
430  * finishes, we __ceph_finish_cap_snap().
431  *
432  * Caller must hold snap_rwsem for read (i.e., the realm topology won't
433  * change).
434  */
435 void ceph_queue_cap_snap(struct ceph_inode_info *ci)
436 {
437         struct inode *inode = &ci->vfs_inode;
438         struct ceph_cap_snap *capsnap;
439         int used, dirty;
440
441         capsnap = kzalloc(sizeof(*capsnap), GFP_NOFS);
442         if (!capsnap) {
443                 pr_err("ENOMEM allocating ceph_cap_snap on %p\n", inode);
444                 return;
445         }
446
447         spin_lock(&inode->i_lock);
448         used = __ceph_caps_used(ci);
449         dirty = __ceph_caps_dirty(ci);
450         if (__ceph_have_pending_cap_snap(ci)) {
451                 /* there is no point in queuing multiple "pending" cap_snaps,
452                    as no new writes are allowed to start when pending, so any
453                    writes in progress now were started before the previous
454                    cap_snap.  lucky us. */
455                 dout("queue_cap_snap %p already pending\n", inode);
456                 kfree(capsnap);
457         } else if (ci->i_wrbuffer_ref_head || (used & CEPH_CAP_FILE_WR) ||
458                    (dirty & (CEPH_CAP_AUTH_EXCL|CEPH_CAP_XATTR_EXCL|
459                              CEPH_CAP_FILE_EXCL|CEPH_CAP_FILE_WR))) {
460                 struct ceph_snap_context *snapc = ci->i_head_snapc;
461
462                 dout("queue_cap_snap %p cap_snap %p queuing under %p\n", inode,
463                      capsnap, snapc);
464                 igrab(inode);
465                 
466                 atomic_set(&capsnap->nref, 1);
467                 capsnap->ci = ci;
468                 INIT_LIST_HEAD(&capsnap->ci_item);
469                 INIT_LIST_HEAD(&capsnap->flushing_item);
470
471                 capsnap->follows = snapc->seq;
472                 capsnap->issued = __ceph_caps_issued(ci, NULL);
473                 capsnap->dirty = dirty;
474
475                 capsnap->mode = inode->i_mode;
476                 capsnap->uid = inode->i_uid;
477                 capsnap->gid = inode->i_gid;
478
479                 if (dirty & CEPH_CAP_XATTR_EXCL) {
480                         __ceph_build_xattrs_blob(ci);
481                         capsnap->xattr_blob =
482                                 ceph_buffer_get(ci->i_xattrs.blob);
483                         capsnap->xattr_version = ci->i_xattrs.version;
484                 } else {
485                         capsnap->xattr_blob = NULL;
486                         capsnap->xattr_version = 0;
487                 }
488
489                 /* dirty page count moved from _head to this cap_snap;
490                    all subsequent writes page dirties occur _after_ this
491                    snapshot. */
492                 capsnap->dirty_pages = ci->i_wrbuffer_ref_head;
493                 ci->i_wrbuffer_ref_head = 0;
494                 capsnap->context = snapc;
495                 ci->i_head_snapc =
496                         ceph_get_snap_context(ci->i_snap_realm->cached_context);
497                 dout(" new snapc is %p\n", ci->i_head_snapc);
498                 list_add_tail(&capsnap->ci_item, &ci->i_cap_snaps);
499
500                 if (used & CEPH_CAP_FILE_WR) {
501                         dout("queue_cap_snap %p cap_snap %p snapc %p"
502                              " seq %llu used WR, now pending\n", inode,
503                              capsnap, snapc, snapc->seq);
504                         capsnap->writing = 1;
505                 } else {
506                         /* note mtime, size NOW. */
507                         __ceph_finish_cap_snap(ci, capsnap);
508                 }
509         } else {
510                 dout("queue_cap_snap %p nothing dirty|writing\n", inode);
511                 kfree(capsnap);
512         }
513
514         spin_unlock(&inode->i_lock);
515 }
516
517 /*
518  * Finalize the size, mtime for a cap_snap.. that is, settle on final values
519  * to be used for the snapshot, to be flushed back to the mds.
520  *
521  * If capsnap can now be flushed, add to snap_flush list, and return 1.
522  *
523  * Caller must hold i_lock.
524  */
525 int __ceph_finish_cap_snap(struct ceph_inode_info *ci,
526                             struct ceph_cap_snap *capsnap)
527 {
528         struct inode *inode = &ci->vfs_inode;
529         struct ceph_mds_client *mdsc = &ceph_sb_to_client(inode->i_sb)->mdsc;
530
531         BUG_ON(capsnap->writing);
532         capsnap->size = inode->i_size;
533         capsnap->mtime = inode->i_mtime;
534         capsnap->atime = inode->i_atime;
535         capsnap->ctime = inode->i_ctime;
536         capsnap->time_warp_seq = ci->i_time_warp_seq;
537         if (capsnap->dirty_pages) {
538                 dout("finish_cap_snap %p cap_snap %p snapc %p %llu %s s=%llu "
539                      "still has %d dirty pages\n", inode, capsnap,
540                      capsnap->context, capsnap->context->seq,
541                      ceph_cap_string(capsnap->dirty), capsnap->size,
542                      capsnap->dirty_pages);
543                 return 0;
544         }
545         dout("finish_cap_snap %p cap_snap %p snapc %p %llu %s s=%llu\n",
546              inode, capsnap, capsnap->context,
547              capsnap->context->seq, ceph_cap_string(capsnap->dirty),
548              capsnap->size);
549
550         spin_lock(&mdsc->snap_flush_lock);
551         list_add_tail(&ci->i_snap_flush_item, &mdsc->snap_flush_list);
552         spin_unlock(&mdsc->snap_flush_lock);
553         return 1;  /* caller may want to ceph_flush_snaps */
554 }
555
556 /*
557  * Queue cap_snaps for snap writeback for this realm and its children.
558  * Called under snap_rwsem, so realm topology won't change.
559  */
560 static void queue_realm_cap_snaps(struct ceph_snap_realm *realm)
561 {
562         struct ceph_inode_info *ci;
563         struct inode *lastinode = NULL;
564         struct ceph_snap_realm *child;
565
566         dout("queue_realm_cap_snaps %p %llx inodes\n", realm, realm->ino);
567
568         spin_lock(&realm->inodes_with_caps_lock);
569         list_for_each_entry(ci, &realm->inodes_with_caps,
570                             i_snap_realm_item) {
571                 struct inode *inode = igrab(&ci->vfs_inode);
572                 if (!inode)
573                         continue;
574                 spin_unlock(&realm->inodes_with_caps_lock);
575                 if (lastinode)
576                         iput(lastinode);
577                 lastinode = inode;
578                 ceph_queue_cap_snap(ci);
579                 spin_lock(&realm->inodes_with_caps_lock);
580         }
581         spin_unlock(&realm->inodes_with_caps_lock);
582         if (lastinode)
583                 iput(lastinode);
584
585         dout("queue_realm_cap_snaps %p %llx children\n", realm, realm->ino);
586         list_for_each_entry(child, &realm->children, child_item)
587                 queue_realm_cap_snaps(child);
588
589         dout("queue_realm_cap_snaps %p %llx done\n", realm, realm->ino);
590 }
591
592 /*
593  * Parse and apply a snapblob "snap trace" from the MDS.  This specifies
594  * the snap realm parameters from a given realm and all of its ancestors,
595  * up to the root.
596  *
597  * Caller must hold snap_rwsem for write.
598  */
599 int ceph_update_snap_trace(struct ceph_mds_client *mdsc,
600                            void *p, void *e, bool deletion)
601 {
602         struct ceph_mds_snap_realm *ri;    /* encoded */
603         __le64 *snaps;                     /* encoded */
604         __le64 *prior_parent_snaps;        /* encoded */
605         struct ceph_snap_realm *realm;
606         int invalidate = 0;
607         int err = -ENOMEM;
608         LIST_HEAD(dirty_realms);
609
610         dout("update_snap_trace deletion=%d\n", deletion);
611 more:
612         ceph_decode_need(&p, e, sizeof(*ri), bad);
613         ri = p;
614         p += sizeof(*ri);
615         ceph_decode_need(&p, e, sizeof(u64)*(le32_to_cpu(ri->num_snaps) +
616                             le32_to_cpu(ri->num_prior_parent_snaps)), bad);
617         snaps = p;
618         p += sizeof(u64) * le32_to_cpu(ri->num_snaps);
619         prior_parent_snaps = p;
620         p += sizeof(u64) * le32_to_cpu(ri->num_prior_parent_snaps);
621
622         realm = ceph_lookup_snap_realm(mdsc, le64_to_cpu(ri->ino));
623         if (!realm) {
624                 realm = ceph_create_snap_realm(mdsc, le64_to_cpu(ri->ino));
625                 if (IS_ERR(realm)) {
626                         err = PTR_ERR(realm);
627                         goto fail;
628                 }
629         }
630
631         /* ensure the parent is correct */
632         err = adjust_snap_realm_parent(mdsc, realm, le64_to_cpu(ri->parent));
633         if (err < 0)
634                 goto fail;
635         invalidate += err;
636
637         if (le64_to_cpu(ri->seq) > realm->seq) {
638                 dout("update_snap_trace updating %llx %p %lld -> %lld\n",
639                      realm->ino, realm, realm->seq, le64_to_cpu(ri->seq));
640                 /* update realm parameters, snap lists */
641                 realm->seq = le64_to_cpu(ri->seq);
642                 realm->created = le64_to_cpu(ri->created);
643                 realm->parent_since = le64_to_cpu(ri->parent_since);
644
645                 realm->num_snaps = le32_to_cpu(ri->num_snaps);
646                 err = dup_array(&realm->snaps, snaps, realm->num_snaps);
647                 if (err < 0)
648                         goto fail;
649
650                 realm->num_prior_parent_snaps =
651                         le32_to_cpu(ri->num_prior_parent_snaps);
652                 err = dup_array(&realm->prior_parent_snaps, prior_parent_snaps,
653                                 realm->num_prior_parent_snaps);
654                 if (err < 0)
655                         goto fail;
656
657                 /* queue realm for cap_snap creation */
658                 list_add(&realm->dirty_item, &dirty_realms);
659
660                 invalidate = 1;
661         } else if (!realm->cached_context) {
662                 dout("update_snap_trace %llx %p seq %lld new\n",
663                      realm->ino, realm, realm->seq);
664                 invalidate = 1;
665         } else {
666                 dout("update_snap_trace %llx %p seq %lld unchanged\n",
667                      realm->ino, realm, realm->seq);
668         }
669
670         dout("done with %llx %p, invalidated=%d, %p %p\n", realm->ino,
671              realm, invalidate, p, e);
672
673         if (p < e)
674                 goto more;
675
676         /* invalidate when we reach the _end_ (root) of the trace */
677         if (invalidate)
678                 rebuild_snap_realms(realm);
679
680         /*
681          * queue cap snaps _after_ we've built the new snap contexts,
682          * so that i_head_snapc can be set appropriately.
683          */
684         list_for_each_entry(realm, &dirty_realms, dirty_item) {
685                 queue_realm_cap_snaps(realm);
686         }
687
688         __cleanup_empty_realms(mdsc);
689         return 0;
690
691 bad:
692         err = -EINVAL;
693 fail:
694         pr_err("update_snap_trace error %d\n", err);
695         return err;
696 }
697
698
699 /*
700  * Send any cap_snaps that are queued for flush.  Try to carry
701  * s_mutex across multiple snap flushes to avoid locking overhead.
702  *
703  * Caller holds no locks.
704  */
705 static void flush_snaps(struct ceph_mds_client *mdsc)
706 {
707         struct ceph_inode_info *ci;
708         struct inode *inode;
709         struct ceph_mds_session *session = NULL;
710
711         dout("flush_snaps\n");
712         spin_lock(&mdsc->snap_flush_lock);
713         while (!list_empty(&mdsc->snap_flush_list)) {
714                 ci = list_first_entry(&mdsc->snap_flush_list,
715                                 struct ceph_inode_info, i_snap_flush_item);
716                 inode = &ci->vfs_inode;
717                 igrab(inode);
718                 spin_unlock(&mdsc->snap_flush_lock);
719                 spin_lock(&inode->i_lock);
720                 __ceph_flush_snaps(ci, &session, 0);
721                 spin_unlock(&inode->i_lock);
722                 iput(inode);
723                 spin_lock(&mdsc->snap_flush_lock);
724         }
725         spin_unlock(&mdsc->snap_flush_lock);
726
727         if (session) {
728                 mutex_unlock(&session->s_mutex);
729                 ceph_put_mds_session(session);
730         }
731         dout("flush_snaps done\n");
732 }
733
734
735 /*
736  * Handle a snap notification from the MDS.
737  *
738  * This can take two basic forms: the simplest is just a snap creation
739  * or deletion notification on an existing realm.  This should update the
740  * realm and its children.
741  *
742  * The more difficult case is realm creation, due to snap creation at a
743  * new point in the file hierarchy, or due to a rename that moves a file or
744  * directory into another realm.
745  */
746 void ceph_handle_snap(struct ceph_mds_client *mdsc,
747                       struct ceph_mds_session *session,
748                       struct ceph_msg *msg)
749 {
750         struct super_block *sb = mdsc->client->sb;
751         int mds = session->s_mds;
752         u64 split;
753         int op;
754         int trace_len;
755         struct ceph_snap_realm *realm = NULL;
756         void *p = msg->front.iov_base;
757         void *e = p + msg->front.iov_len;
758         struct ceph_mds_snap_head *h;
759         int num_split_inos, num_split_realms;
760         __le64 *split_inos = NULL, *split_realms = NULL;
761         int i;
762         int locked_rwsem = 0;
763
764         /* decode */
765         if (msg->front.iov_len < sizeof(*h))
766                 goto bad;
767         h = p;
768         op = le32_to_cpu(h->op);
769         split = le64_to_cpu(h->split);   /* non-zero if we are splitting an
770                                           * existing realm */
771         num_split_inos = le32_to_cpu(h->num_split_inos);
772         num_split_realms = le32_to_cpu(h->num_split_realms);
773         trace_len = le32_to_cpu(h->trace_len);
774         p += sizeof(*h);
775
776         dout("handle_snap from mds%d op %s split %llx tracelen %d\n", mds,
777              ceph_snap_op_name(op), split, trace_len);
778
779         mutex_lock(&session->s_mutex);
780         session->s_seq++;
781         mutex_unlock(&session->s_mutex);
782
783         down_write(&mdsc->snap_rwsem);
784         locked_rwsem = 1;
785
786         if (op == CEPH_SNAP_OP_SPLIT) {
787                 struct ceph_mds_snap_realm *ri;
788
789                 /*
790                  * A "split" breaks part of an existing realm off into
791                  * a new realm.  The MDS provides a list of inodes
792                  * (with caps) and child realms that belong to the new
793                  * child.
794                  */
795                 split_inos = p;
796                 p += sizeof(u64) * num_split_inos;
797                 split_realms = p;
798                 p += sizeof(u64) * num_split_realms;
799                 ceph_decode_need(&p, e, sizeof(*ri), bad);
800                 /* we will peek at realm info here, but will _not_
801                  * advance p, as the realm update will occur below in
802                  * ceph_update_snap_trace. */
803                 ri = p;
804
805                 realm = ceph_lookup_snap_realm(mdsc, split);
806                 if (!realm) {
807                         realm = ceph_create_snap_realm(mdsc, split);
808                         if (IS_ERR(realm))
809                                 goto out;
810                 }
811                 ceph_get_snap_realm(mdsc, realm);
812
813                 dout("splitting snap_realm %llx %p\n", realm->ino, realm);
814                 for (i = 0; i < num_split_inos; i++) {
815                         struct ceph_vino vino = {
816                                 .ino = le64_to_cpu(split_inos[i]),
817                                 .snap = CEPH_NOSNAP,
818                         };
819                         struct inode *inode = ceph_find_inode(sb, vino);
820                         struct ceph_inode_info *ci;
821                         struct ceph_snap_realm *oldrealm;
822
823                         if (!inode)
824                                 continue;
825                         ci = ceph_inode(inode);
826
827                         spin_lock(&inode->i_lock);
828                         if (!ci->i_snap_realm)
829                                 goto skip_inode;
830                         /*
831                          * If this inode belongs to a realm that was
832                          * created after our new realm, we experienced
833                          * a race (due to another split notifications
834                          * arriving from a different MDS).  So skip
835                          * this inode.
836                          */
837                         if (ci->i_snap_realm->created >
838                             le64_to_cpu(ri->created)) {
839                                 dout(" leaving %p in newer realm %llx %p\n",
840                                      inode, ci->i_snap_realm->ino,
841                                      ci->i_snap_realm);
842                                 goto skip_inode;
843                         }
844                         dout(" will move %p to split realm %llx %p\n",
845                              inode, realm->ino, realm);
846                         /*
847                          * Move the inode to the new realm
848                          */
849                         spin_lock(&realm->inodes_with_caps_lock);
850                         list_del_init(&ci->i_snap_realm_item);
851                         list_add(&ci->i_snap_realm_item,
852                                  &realm->inodes_with_caps);
853                         oldrealm = ci->i_snap_realm;
854                         ci->i_snap_realm = realm;
855                         spin_unlock(&realm->inodes_with_caps_lock);
856                         spin_unlock(&inode->i_lock);
857
858                         ceph_get_snap_realm(mdsc, realm);
859                         ceph_put_snap_realm(mdsc, oldrealm);
860
861                         iput(inode);
862                         continue;
863
864 skip_inode:
865                         spin_unlock(&inode->i_lock);
866                         iput(inode);
867                 }
868
869                 /* we may have taken some of the old realm's children. */
870                 for (i = 0; i < num_split_realms; i++) {
871                         struct ceph_snap_realm *child =
872                                 ceph_lookup_snap_realm(mdsc,
873                                            le64_to_cpu(split_realms[i]));
874                         if (!child)
875                                 continue;
876                         adjust_snap_realm_parent(mdsc, child, realm->ino);
877                 }
878         }
879
880         /*
881          * update using the provided snap trace. if we are deleting a
882          * snap, we can avoid queueing cap_snaps.
883          */
884         ceph_update_snap_trace(mdsc, p, e,
885                                op == CEPH_SNAP_OP_DESTROY);
886
887         if (op == CEPH_SNAP_OP_SPLIT)
888                 /* we took a reference when we created the realm, above */
889                 ceph_put_snap_realm(mdsc, realm);
890
891         __cleanup_empty_realms(mdsc);
892
893         up_write(&mdsc->snap_rwsem);
894
895         flush_snaps(mdsc);
896         return;
897
898 bad:
899         pr_err("corrupt snap message from mds%d\n", mds);
900         ceph_msg_dump(msg);
901 out:
902         if (locked_rwsem)
903                 up_write(&mdsc->snap_rwsem);
904         return;
905 }
906
907
908