From: Alex Elder <elder@inktank.com>
To: ceph-devel@vger.kernel.org
Subject: [PATCH 5/9] rbd: get the snapshot context for a v2 image
Date: Fri, 07 Sep 2012 16:13:56 -0500 [thread overview]
Message-ID: <504A6394.203@inktank.com> (raw)
In-Reply-To: <504A6273.7030807@inktank.com>
Fetch the snapshot context for an rbd format 2 image by calling
the "get_snapcontext" method on its header object.
Signed-off-by: Alex Elder <elder@inktank.com>
---
drivers/block/rbd.c | 85
+++++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 85 insertions(+)
diff --git a/drivers/block/rbd.c b/drivers/block/rbd.c
index d48f025..8ff84fd 100644
--- a/drivers/block/rbd.c
+++ b/drivers/block/rbd.c
@@ -62,6 +62,7 @@
#define RBD_MINORS_PER_MAJOR 256 /* max minors per blkdev */
#define RBD_MAX_SNAP_NAME_LEN 32
+#define RBD_MAX_SNAP_COUNT 510 /* allows max snapc to fit in 4KB */
#define RBD_MAX_OPT_LEN 1024
#define RBD_SNAP_HEAD_NAME "-"
@@ -2234,6 +2235,84 @@ static int rbd_dev_v2_features(struct rbd_device
*rbd_dev)
&rbd_dev->header.features);
}
+static int rbd_dev_v2_snap_context(struct rbd_device *rbd_dev)
+{
+ size_t size;
+ int ret;
+ void *reply_buf;
+ void *p;
+ void *end;
+ u64 seq;
+ u32 snap_count;
+ struct ceph_snap_context *snapc;
+ u32 i;
+
+ /*
+ * We'll need room for the seq value (maximum snapshot id),
+ * snapshot count, and array of that many snapshot ids.
+ * For now we have a fixed upper limit on the number we're
+ * prepared to receive.
+ */
+ size = sizeof (__le64) + sizeof (__le32) +
+ RBD_MAX_SNAP_COUNT * sizeof (__le64);
+ reply_buf = kzalloc(size, GFP_KERNEL);
+ if (!reply_buf)
+ return -ENOMEM;
+
+ ret = rbd_req_sync_exec(rbd_dev, rbd_dev->header_name,
+ "rbd", "get_snapcontext",
+ NULL, 0,
+ reply_buf, size,
+ CEPH_OSD_FLAG_READ, NULL);
+ dout("%s: rbd_req_sync_exec returned %d\n", __func__, ret);
+ if (ret < 0)
+ goto out;
+
+ ret = -ERANGE;
+ p = reply_buf;
+ end = (char *) reply_buf + size;
+ ceph_decode_64_safe(&p, end, seq, out);
+ ceph_decode_32_safe(&p, end, snap_count, out);
+
+ /*
+ * Make sure the reported number of snapshot ids wouldn't go
+ * beyond the end of our buffer. But before checking that,
+ * make sure the computed size of the snapshot context we
+ * allocate is representable in a size_t.
+ */
+ if (snap_count > (SIZE_MAX - sizeof (struct ceph_snap_context))
+ / sizeof (u64)) {
+ ret = -EINVAL;
+ goto out;
+ }
+ if (!ceph_has_room(&p, end, snap_count * sizeof (__le64)))
+ goto out;
+
+ size = sizeof (struct ceph_snap_context) +
+ snap_count * sizeof (snapc->snaps[0]);
+ snapc = kmalloc(size, GFP_KERNEL);
+ if (!snapc) {
+ ret = -ENOMEM;
+ goto out;
+ }
+
+ atomic_set(&snapc->nref, 1);
+ snapc->seq = seq;
+ snapc->num_snaps = snap_count;
+ for (i = 0; i < snap_count; i++)
+ snapc->snaps[i] = ceph_decode_64(&p);
+
+ rbd_dev->header.snapc = snapc;
+
+ dout(" snap context seq = %llu, snap_count = %u\n",
+ (unsigned long long) seq, (unsigned int) snap_count);
+
+out:
+ kfree(reply_buf);
+
+ return 0;
+}
+
/*
* Scan the rbd device's current snapshot list and compare it to the
* newly-received snapshot context. Remove any existing snapshots
@@ -2773,6 +2852,12 @@ static int rbd_dev_v2_probe(struct rbd_device
*rbd_dev)
ret = rbd_dev_v2_features(rbd_dev);
if (ret < 0)
goto out_err;
+
+ /* Get the snapshot context */
+
+ ret = rbd_dev_v2_snap_context(rbd_dev);
+ if (ret)
+ goto out_err;
rbd_dev->image_format = 2;
dout("discovered version 2 image, header name is %s\n",
--
1.7.9.5
next prev parent reply other threads:[~2012-09-07 21:13 UTC|newest]
Thread overview: 20+ messages / expand[flat|nested] mbox.gz Atom feed top
2012-09-07 21:09 [PATCH 0/9] rbd: activate v2 image support Alex Elder
2012-09-07 21:13 ` [PATCH 1/9] rbd: lay out header probe infrastructure Alex Elder
2012-09-19 18:35 ` Josh Durgin
2012-09-07 21:13 ` [PATCH 2/9] rbd: add code to get the size of a v2 rbd image Alex Elder
2012-09-19 18:52 ` Josh Durgin
2012-09-07 21:13 ` [PATCH 3/9] rbd: get the object prefix for " Alex Elder
2012-09-19 18:57 ` Josh Durgin
2012-09-07 21:13 ` [PATCH 4/9] rbd: get image features for a v2 image Alex Elder
2012-09-19 19:02 ` Josh Durgin
2012-09-07 21:13 ` Alex Elder [this message]
2012-09-19 19:17 ` [PATCH 5/9] rbd: get the snapshot context " Josh Durgin
2012-09-07 21:14 ` [PATCH 6/9] rbd: get snapshot name " Alex Elder
2012-09-19 19:31 ` Josh Durgin
2012-09-07 21:15 ` [PATCH 7/9] rbd: update remaining header fields for v2 Alex Elder
2012-09-19 19:38 ` Josh Durgin
2012-09-07 21:15 ` [PATCH 8/9] rbd: define rbd_dev_v2_snapc_refresh() Alex Elder
2012-09-19 21:00 ` Josh Durgin
2012-09-07 21:15 ` [PATCH 9/9] rbd: activate v2 image support Alex Elder
2012-09-19 19:56 ` Josh Durgin
2012-09-19 19:57 ` Josh Durgin
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=504A6394.203@inktank.com \
--to=elder@inktank.com \
--cc=ceph-devel@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.