GUI: Fix Tomato RAF theme for all builds. Compilation typo.
[tomato.git] / release / src-rt-6.x.4708 / linux / linux-2.6.36 / fs / ceph / snap.c
blobbe1d64187d5c5cce0279142c6ccad74f8c6cbbcf
1 #include "ceph_debug.h"
3 #include <linux/sort.h>
4 #include <linux/slab.h>
6 #include "super.h"
7 #include "decode.h"
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.
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.
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.
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.
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.
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.
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).
56 * increase ref count for the realm
58 * caller must hold snap_rwsem for write.
60 void ceph_get_snap_realm(struct ceph_mds_client *mdsc,
61 struct ceph_snap_realm *realm)
63 dout("get_realm %p %d -> %d\n", realm,
64 atomic_read(&realm->nref), atomic_read(&realm->nref)+1);
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.
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);
77 atomic_inc(&realm->nref);
80 static void __insert_snap_realm(struct rb_root *root,
81 struct ceph_snap_realm *new)
83 struct rb_node **p = &root->rb_node;
84 struct rb_node *parent = NULL;
85 struct ceph_snap_realm *r = NULL;
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();
98 rb_link_node(&new->node, parent, p);
99 rb_insert_color(&new->node, root);
103 * create and get the realm rooted at @ino and bump its ref count.
105 * caller must hold snap_rwsem for write.
107 static struct ceph_snap_realm *ceph_create_snap_realm(
108 struct ceph_mds_client *mdsc,
109 u64 ino)
111 struct ceph_snap_realm *realm;
113 realm = kzalloc(sizeof(*realm), GFP_NOFS);
114 if (!realm)
115 return ERR_PTR(-ENOMEM);
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;
131 * lookup the realm rooted at @ino.
133 * caller must hold snap_rwsem for write.
135 struct ceph_snap_realm *ceph_lookup_snap_realm(struct ceph_mds_client *mdsc,
136 u64 ino)
138 struct rb_node *n = mdsc->snap_realms.rb_node;
139 struct ceph_snap_realm *r;
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;
152 return NULL;
155 static void __put_snap_realm(struct ceph_mds_client *mdsc,
156 struct ceph_snap_realm *realm);
159 * called with snap_rwsem (write)
161 static void __destroy_snap_realm(struct ceph_mds_client *mdsc,
162 struct ceph_snap_realm *realm)
164 dout("__destroy_snap_realm %p %llx\n", realm, realm->ino);
166 rb_erase(&realm->node, &mdsc->snap_realms);
168 if (realm->parent) {
169 list_del_init(&realm->child_item);
170 __put_snap_realm(mdsc, realm->parent);
173 kfree(realm->prior_parent_snaps);
174 kfree(realm->snaps);
175 ceph_put_snap_context(realm->cached_context);
176 kfree(realm);
180 * caller holds snap_rwsem (write)
182 static void __put_snap_realm(struct ceph_mds_client *mdsc,
183 struct ceph_snap_realm *realm)
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);
192 * caller needn't hold any locks
194 void ceph_put_snap_realm(struct ceph_mds_client *mdsc,
195 struct ceph_snap_realm *realm)
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;
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);
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.
217 * Called under snap_rwsem (write)
219 static void __cleanup_empty_realms(struct ceph_mds_client *mdsc)
221 struct ceph_snap_realm *realm;
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);
232 spin_unlock(&mdsc->snap_empty_lock);
235 void ceph_cleanup_empty_realms(struct ceph_mds_client *mdsc)
237 down_write(&mdsc->snap_rwsem);
238 __cleanup_empty_realms(mdsc);
239 up_write(&mdsc->snap_rwsem);
243 * adjust the parent realm of a given @realm. adjust child list, and parent
244 * pointers, and ref counts appropriately.
246 * return true if parent was changed, 0 if unchanged, <0 on error.
248 * caller must hold snap_rwsem for write.
250 static int adjust_snap_realm_parent(struct ceph_mds_client *mdsc,
251 struct ceph_snap_realm *realm,
252 u64 parentino)
254 struct ceph_snap_realm *parent;
256 if (realm->parent_ino == parentino)
257 return 0;
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);
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);
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;
280 static int cmpu64_rev(const void *a, const void *b)
282 if (*(u64 *)a < *(u64 *)b)
283 return 1;
284 if (*(u64 *)a > *(u64 *)b)
285 return -1;
286 return 0;
290 * build the snap context for a given realm.
292 static int build_snap_context(struct ceph_snap_realm *realm)
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;
301 * build parent context, if it hasn't been built.
302 * conservatively estimate that all parent snaps might be
303 * included by us.
305 if (parent) {
306 if (!parent->cached_context) {
307 err = build_snap_context(parent);
308 if (err)
309 goto fail;
311 num += parent->cached_context->num_snaps;
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;
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);
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;
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;
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);
365 if (realm->cached_context)
366 ceph_put_snap_context(realm->cached_context);
367 realm->cached_context = snapc;
368 return 0;
370 fail:
372 * if we fail, clear old (incorrect) cached_context... hopefully
373 * we'll have better luck building it later
375 if (realm->cached_context) {
376 ceph_put_snap_context(realm->cached_context);
377 realm->cached_context = NULL;
379 pr_err("build_snap_context %llx %p fail %d\n", realm->ino,
380 realm, err);
381 return err;
385 * rebuild snap context for the given realm and all of its children.
387 static void rebuild_snap_realms(struct ceph_snap_realm *realm)
389 struct ceph_snap_realm *child;
391 dout("rebuild_snap_realms %llx %p\n", realm->ino, realm);
392 build_snap_context(realm);
394 list_for_each_entry(child, &realm->children, child_item)
395 rebuild_snap_realms(child);
400 * helper to allocate and decode an array of snapids. free prior
401 * instance, if any.
403 static int dup_array(u64 **dst, __le64 *src, int num)
405 int i;
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;
417 return 0;
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.
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().
432 * Caller must hold snap_rwsem for read (i.e., the realm topology won't
433 * change).
435 void ceph_queue_cap_snap(struct ceph_inode_info *ci)
437 struct inode *inode = &ci->vfs_inode;
438 struct ceph_cap_snap *capsnap;
439 int used, dirty;
441 capsnap = kzalloc(sizeof(*capsnap), GFP_NOFS);
442 if (!capsnap) {
443 pr_err("ENOMEM allocating ceph_cap_snap on %p\n", inode);
444 return;
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;
462 dout("queue_cap_snap %p cap_snap %p queuing under %p\n", inode,
463 capsnap, snapc);
464 igrab(inode);
466 atomic_set(&capsnap->nref, 1);
467 capsnap->ci = ci;
468 INIT_LIST_HEAD(&capsnap->ci_item);
469 INIT_LIST_HEAD(&capsnap->flushing_item);
471 capsnap->follows = snapc->seq;
472 capsnap->issued = __ceph_caps_issued(ci, NULL);
473 capsnap->dirty = dirty;
475 capsnap->mode = inode->i_mode;
476 capsnap->uid = inode->i_uid;
477 capsnap->gid = inode->i_gid;
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;
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);
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);
509 } else {
510 dout("queue_cap_snap %p nothing dirty|writing\n", inode);
511 kfree(capsnap);
514 spin_unlock(&inode->i_lock);
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.
521 * If capsnap can now be flushed, add to snap_flush list, and return 1.
523 * Caller must hold i_lock.
525 int __ceph_finish_cap_snap(struct ceph_inode_info *ci,
526 struct ceph_cap_snap *capsnap)
528 struct inode *inode = &ci->vfs_inode;
529 struct ceph_mds_client *mdsc = &ceph_sb_to_client(inode->i_sb)->mdsc;
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;
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);
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 */
557 * Queue cap_snaps for snap writeback for this realm and its children.
558 * Called under snap_rwsem, so realm topology won't change.
560 static void queue_realm_cap_snaps(struct ceph_snap_realm *realm)
562 struct ceph_inode_info *ci;
563 struct inode *lastinode = NULL;
564 struct ceph_snap_realm *child;
566 dout("queue_realm_cap_snaps %p %llx inodes\n", realm, realm->ino);
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);
581 spin_unlock(&realm->inodes_with_caps_lock);
582 if (lastinode)
583 iput(lastinode);
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);
589 dout("queue_realm_cap_snaps %p %llx done\n", realm, realm->ino);
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.
597 * Caller must hold snap_rwsem for write.
599 int ceph_update_snap_trace(struct ceph_mds_client *mdsc,
600 void *p, void *e, bool deletion)
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);
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);
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;
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;
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);
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;
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;
657 /* queue realm for cap_snap creation */
658 list_add(&realm->dirty_item, &dirty_realms);
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);
670 dout("done with %llx %p, invalidated=%d, %p %p\n", realm->ino,
671 realm, invalidate, p, e);
673 if (p < e)
674 goto more;
676 /* invalidate when we reach the _end_ (root) of the trace */
677 if (invalidate)
678 rebuild_snap_realms(realm);
681 * queue cap snaps _after_ we've built the new snap contexts,
682 * so that i_head_snapc can be set appropriately.
684 list_for_each_entry(realm, &dirty_realms, dirty_item) {
685 queue_realm_cap_snaps(realm);
688 __cleanup_empty_realms(mdsc);
689 return 0;
691 bad:
692 err = -EINVAL;
693 fail:
694 pr_err("update_snap_trace error %d\n", err);
695 return err;
700 * Send any cap_snaps that are queued for flush. Try to carry
701 * s_mutex across multiple snap flushes to avoid locking overhead.
703 * Caller holds no locks.
705 static void flush_snaps(struct ceph_mds_client *mdsc)
707 struct ceph_inode_info *ci;
708 struct inode *inode;
709 struct ceph_mds_session *session = NULL;
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);
725 spin_unlock(&mdsc->snap_flush_lock);
727 if (session) {
728 mutex_unlock(&session->s_mutex);
729 ceph_put_mds_session(session);
731 dout("flush_snaps done\n");
736 * Handle a snap notification from the MDS.
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.
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.
746 void ceph_handle_snap(struct ceph_mds_client *mdsc,
747 struct ceph_mds_session *session,
748 struct ceph_msg *msg)
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;
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);
776 dout("handle_snap from mds%d op %s split %llx tracelen %d\n", mds,
777 ceph_snap_op_name(op), split, trace_len);
779 mutex_lock(&session->s_mutex);
780 session->s_seq++;
781 mutex_unlock(&session->s_mutex);
783 down_write(&mdsc->snap_rwsem);
784 locked_rwsem = 1;
786 if (op == CEPH_SNAP_OP_SPLIT) {
787 struct ceph_mds_snap_realm *ri;
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.
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;
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;
811 ceph_get_snap_realm(mdsc, realm);
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,
819 struct inode *inode = ceph_find_inode(sb, vino);
820 struct ceph_inode_info *ci;
821 struct ceph_snap_realm *oldrealm;
823 if (!inode)
824 continue;
825 ci = ceph_inode(inode);
827 spin_lock(&inode->i_lock);
828 if (!ci->i_snap_realm)
829 goto skip_inode;
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.
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;
844 dout(" will move %p to split realm %llx %p\n",
845 inode, realm->ino, realm);
847 * Move the inode to the new realm
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);
858 ceph_get_snap_realm(mdsc, realm);
859 ceph_put_snap_realm(mdsc, oldrealm);
861 iput(inode);
862 continue;
864 skip_inode:
865 spin_unlock(&inode->i_lock);
866 iput(inode);
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);
881 * update using the provided snap trace. if we are deleting a
882 * snap, we can avoid queueing cap_snaps.
884 ceph_update_snap_trace(mdsc, p, e,
885 op == CEPH_SNAP_OP_DESTROY);
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);
891 __cleanup_empty_realms(mdsc);
893 up_write(&mdsc->snap_rwsem);
895 flush_snaps(mdsc);
896 return;
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;