2 rbd.c -- Export ceph rados objects as a Linux block device
5 based on drivers/block/osdblk.c:
7 Copyright 2009 Red Hat, Inc.
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; see the file COPYING. If not, write to
20 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
24 For usage instructions, please refer to:
26 Documentation/ABI/testing/sysfs-bus-rbd
30 #include <linux/ceph/libceph.h>
31 #include <linux/ceph/osd_client.h>
32 #include <linux/ceph/mon_client.h>
33 #include <linux/ceph/decode.h>
34 #include <linux/parser.h>
36 #include <linux/kernel.h>
37 #include <linux/device.h>
38 #include <linux/module.h>
40 #include <linux/blkdev.h>
42 #include "rbd_types.h"
44 #define RBD_DEBUG /* Activate rbd_assert() calls */
47 * The basic unit of block I/O is a sector. It is interpreted in a
48 * number of contexts in Linux (blk, bio, genhd), but the default is
49 * universally 512 bytes. These symbols are just slightly more
50 * meaningful than the bare numbers they represent.
52 #define SECTOR_SHIFT 9
53 #define SECTOR_SIZE (1ULL << SECTOR_SHIFT)
55 /* It might be useful to have this defined elsewhere too */
57 #define U64_MAX ((u64) (~0ULL))
59 #define RBD_DRV_NAME "rbd"
60 #define RBD_DRV_NAME_LONG "rbd (rados block device)"
62 #define RBD_MINORS_PER_MAJOR 256 /* max minors per blkdev */
64 #define RBD_MAX_SNAP_NAME_LEN 32
65 #define RBD_MAX_SNAP_COUNT 510 /* allows max snapc to fit in 4KB */
66 #define RBD_MAX_OPT_LEN 1024
68 #define RBD_SNAP_HEAD_NAME "-"
70 #define RBD_IMAGE_ID_LEN_MAX 64
71 #define RBD_OBJ_PREFIX_LEN_MAX 64
75 #define RBD_FEATURE_LAYERING 1
77 /* Features supported by this (client software) implementation. */
79 #define RBD_FEATURES_ALL (0)
82 * An RBD device name will be "rbd#", where the "rbd" comes from
83 * RBD_DRV_NAME above, and # is a unique integer identifier.
84 * MAX_INT_FORMAT_WIDTH is used in ensuring DEV_NAME_LEN is big
85 * enough to hold all possible device names.
87 #define DEV_NAME_LEN 32
88 #define MAX_INT_FORMAT_WIDTH ((5 * sizeof (int)) / 2 + 1)
90 #define RBD_READ_ONLY_DEFAULT false
93 * block device image metadata (in-memory version)
95 struct rbd_image_header {
96 /* These four fields never change for a given rbd image */
103 /* The remaining fields need to be updated occasionally */
105 struct ceph_snap_context *snapc;
117 * an instance of the client. multiple devices may share an rbd client.
120 struct ceph_client *client;
122 struct list_head node;
126 * a request completion status
128 struct rbd_req_status {
135 * a collection of requests
137 struct rbd_req_coll {
141 struct rbd_req_status status[0];
145 * a single io request
148 struct request *rq; /* blk layer request */
149 struct bio *bio; /* cloned bio */
150 struct page **pages; /* list of used pages */
153 struct rbd_req_coll *coll;
160 struct list_head node;
178 int dev_id; /* blkdev unique id */
180 int major; /* blkdev assigned major */
181 struct gendisk *disk; /* blkdev's gendisk and rq */
183 u32 image_format; /* Either 1 or 2 */
184 struct rbd_options rbd_opts;
185 struct rbd_client *rbd_client;
187 char name[DEV_NAME_LEN]; /* blkdev name, e.g. rbd3 */
189 spinlock_t lock; /* queue lock */
191 struct rbd_image_header header;
195 size_t image_name_len;
200 struct ceph_osd_event *watch_event;
201 struct ceph_osd_request *watch_request;
203 /* protects updating the header */
204 struct rw_semaphore header_rwsem;
206 struct rbd_mapping mapping;
208 struct list_head node;
210 /* list of snapshots */
211 struct list_head snaps;
217 static DEFINE_MUTEX(ctl_mutex); /* Serialize open/close/setup/teardown */
219 static LIST_HEAD(rbd_dev_list); /* devices */
220 static DEFINE_SPINLOCK(rbd_dev_list_lock);
222 static LIST_HEAD(rbd_client_list); /* clients */
223 static DEFINE_SPINLOCK(rbd_client_list_lock);
225 static int rbd_dev_snaps_update(struct rbd_device *rbd_dev);
226 static int rbd_dev_snaps_register(struct rbd_device *rbd_dev);
228 static void rbd_dev_release(struct device *dev);
229 static void __rbd_remove_snap_dev(struct rbd_snap *snap);
231 static ssize_t rbd_add(struct bus_type *bus, const char *buf,
233 static ssize_t rbd_remove(struct bus_type *bus, const char *buf,
236 static struct bus_attribute rbd_bus_attrs[] = {
237 __ATTR(add, S_IWUSR, NULL, rbd_add),
238 __ATTR(remove, S_IWUSR, NULL, rbd_remove),
242 static struct bus_type rbd_bus_type = {
244 .bus_attrs = rbd_bus_attrs,
247 static void rbd_root_dev_release(struct device *dev)
251 static struct device rbd_root_dev = {
253 .release = rbd_root_dev_release,
257 #define rbd_assert(expr) \
258 if (unlikely(!(expr))) { \
259 printk(KERN_ERR "\nAssertion failure in %s() " \
261 "\trbd_assert(%s);\n\n", \
262 __func__, __LINE__, #expr); \
265 #else /* !RBD_DEBUG */
266 # define rbd_assert(expr) ((void) 0)
267 #endif /* !RBD_DEBUG */
269 static struct device *rbd_get_dev(struct rbd_device *rbd_dev)
271 return get_device(&rbd_dev->dev);
274 static void rbd_put_dev(struct rbd_device *rbd_dev)
276 put_device(&rbd_dev->dev);
279 static int rbd_dev_refresh(struct rbd_device *rbd_dev, u64 *hver);
280 static int rbd_dev_v2_refresh(struct rbd_device *rbd_dev, u64 *hver);
282 static int rbd_open(struct block_device *bdev, fmode_t mode)
284 struct rbd_device *rbd_dev = bdev->bd_disk->private_data;
286 if ((mode & FMODE_WRITE) && rbd_dev->mapping.read_only)
289 rbd_get_dev(rbd_dev);
290 set_device_ro(bdev, rbd_dev->mapping.read_only);
295 static int rbd_release(struct gendisk *disk, fmode_t mode)
297 struct rbd_device *rbd_dev = disk->private_data;
299 rbd_put_dev(rbd_dev);
304 static const struct block_device_operations rbd_bd_ops = {
305 .owner = THIS_MODULE,
307 .release = rbd_release,
311 * Initialize an rbd client instance.
314 static struct rbd_client *rbd_client_create(struct ceph_options *ceph_opts)
316 struct rbd_client *rbdc;
319 dout("rbd_client_create\n");
320 rbdc = kmalloc(sizeof(struct rbd_client), GFP_KERNEL);
324 kref_init(&rbdc->kref);
325 INIT_LIST_HEAD(&rbdc->node);
327 mutex_lock_nested(&ctl_mutex, SINGLE_DEPTH_NESTING);
329 rbdc->client = ceph_create_client(ceph_opts, rbdc, 0, 0);
330 if (IS_ERR(rbdc->client))
332 ceph_opts = NULL; /* Now rbdc->client is responsible for ceph_opts */
334 ret = ceph_open_session(rbdc->client);
338 spin_lock(&rbd_client_list_lock);
339 list_add_tail(&rbdc->node, &rbd_client_list);
340 spin_unlock(&rbd_client_list_lock);
342 mutex_unlock(&ctl_mutex);
344 dout("rbd_client_create created %p\n", rbdc);
348 ceph_destroy_client(rbdc->client);
350 mutex_unlock(&ctl_mutex);
354 ceph_destroy_options(ceph_opts);
359 * Find a ceph client with specific addr and configuration. If
360 * found, bump its reference count.
362 static struct rbd_client *rbd_client_find(struct ceph_options *ceph_opts)
364 struct rbd_client *client_node;
367 if (ceph_opts->flags & CEPH_OPT_NOSHARE)
370 spin_lock(&rbd_client_list_lock);
371 list_for_each_entry(client_node, &rbd_client_list, node) {
372 if (!ceph_compare_options(ceph_opts, client_node->client)) {
373 kref_get(&client_node->kref);
378 spin_unlock(&rbd_client_list_lock);
380 return found ? client_node : NULL;
390 /* string args above */
393 /* Boolean args above */
397 static match_table_t rbd_opts_tokens = {
399 /* string args above */
400 {Opt_read_only, "read_only"},
401 {Opt_read_only, "ro"}, /* Alternate spelling */
402 {Opt_read_write, "read_write"},
403 {Opt_read_write, "rw"}, /* Alternate spelling */
404 /* Boolean args above */
408 static int parse_rbd_opts_token(char *c, void *private)
410 struct rbd_options *rbd_opts = private;
411 substring_t argstr[MAX_OPT_ARGS];
412 int token, intval, ret;
414 token = match_token(c, rbd_opts_tokens, argstr);
418 if (token < Opt_last_int) {
419 ret = match_int(&argstr[0], &intval);
421 pr_err("bad mount option arg (not int) "
425 dout("got int token %d val %d\n", token, intval);
426 } else if (token > Opt_last_int && token < Opt_last_string) {
427 dout("got string token %d val %s\n", token,
429 } else if (token > Opt_last_string && token < Opt_last_bool) {
430 dout("got Boolean token %d\n", token);
432 dout("got token %d\n", token);
437 rbd_opts->read_only = true;
440 rbd_opts->read_only = false;
450 * Get a ceph client with specific addr and configuration, if one does
451 * not exist create it.
453 static int rbd_get_client(struct rbd_device *rbd_dev, const char *mon_addr,
454 size_t mon_addr_len, char *options)
456 struct rbd_options *rbd_opts = &rbd_dev->rbd_opts;
457 struct ceph_options *ceph_opts;
458 struct rbd_client *rbdc;
460 rbd_opts->read_only = RBD_READ_ONLY_DEFAULT;
462 ceph_opts = ceph_parse_options(options, mon_addr,
463 mon_addr + mon_addr_len,
464 parse_rbd_opts_token, rbd_opts);
465 if (IS_ERR(ceph_opts))
466 return PTR_ERR(ceph_opts);
468 rbdc = rbd_client_find(ceph_opts);
470 /* using an existing client */
471 ceph_destroy_options(ceph_opts);
473 rbdc = rbd_client_create(ceph_opts);
475 return PTR_ERR(rbdc);
477 rbd_dev->rbd_client = rbdc;
483 * Destroy ceph client
485 * Caller must hold rbd_client_list_lock.
487 static void rbd_client_release(struct kref *kref)
489 struct rbd_client *rbdc = container_of(kref, struct rbd_client, kref);
491 dout("rbd_release_client %p\n", rbdc);
492 spin_lock(&rbd_client_list_lock);
493 list_del(&rbdc->node);
494 spin_unlock(&rbd_client_list_lock);
496 ceph_destroy_client(rbdc->client);
501 * Drop reference to ceph client node. If it's not referenced anymore, release
504 static void rbd_put_client(struct rbd_device *rbd_dev)
506 kref_put(&rbd_dev->rbd_client->kref, rbd_client_release);
507 rbd_dev->rbd_client = NULL;
511 * Destroy requests collection
513 static void rbd_coll_release(struct kref *kref)
515 struct rbd_req_coll *coll =
516 container_of(kref, struct rbd_req_coll, kref);
518 dout("rbd_coll_release %p\n", coll);
522 static bool rbd_image_format_valid(u32 image_format)
524 return image_format == 1 || image_format == 2;
527 static bool rbd_dev_ondisk_valid(struct rbd_image_header_ondisk *ondisk)
532 /* The header has to start with the magic rbd header text */
533 if (memcmp(&ondisk->text, RBD_HEADER_TEXT, sizeof (RBD_HEADER_TEXT)))
537 * The size of a snapshot header has to fit in a size_t, and
538 * that limits the number of snapshots.
540 snap_count = le32_to_cpu(ondisk->snap_count);
541 size = SIZE_MAX - sizeof (struct ceph_snap_context);
542 if (snap_count > size / sizeof (__le64))
546 * Not only that, but the size of the entire the snapshot
547 * header must also be representable in a size_t.
549 size -= snap_count * sizeof (__le64);
550 if ((u64) size < le64_to_cpu(ondisk->snap_names_len))
557 * Create a new header structure, translate header format from the on-disk
560 static int rbd_header_from_disk(struct rbd_image_header *header,
561 struct rbd_image_header_ondisk *ondisk)
568 memset(header, 0, sizeof (*header));
570 snap_count = le32_to_cpu(ondisk->snap_count);
572 len = strnlen(ondisk->object_prefix, sizeof (ondisk->object_prefix));
573 header->object_prefix = kmalloc(len + 1, GFP_KERNEL);
574 if (!header->object_prefix)
576 memcpy(header->object_prefix, ondisk->object_prefix, len);
577 header->object_prefix[len] = '\0';
580 u64 snap_names_len = le64_to_cpu(ondisk->snap_names_len);
582 /* Save a copy of the snapshot names */
584 if (snap_names_len > (u64) SIZE_MAX)
586 header->snap_names = kmalloc(snap_names_len, GFP_KERNEL);
587 if (!header->snap_names)
590 * Note that rbd_dev_v1_header_read() guarantees
591 * the ondisk buffer we're working with has
592 * snap_names_len bytes beyond the end of the
593 * snapshot id array, this memcpy() is safe.
595 memcpy(header->snap_names, &ondisk->snaps[snap_count],
598 /* Record each snapshot's size */
600 size = snap_count * sizeof (*header->snap_sizes);
601 header->snap_sizes = kmalloc(size, GFP_KERNEL);
602 if (!header->snap_sizes)
604 for (i = 0; i < snap_count; i++)
605 header->snap_sizes[i] =
606 le64_to_cpu(ondisk->snaps[i].image_size);
608 WARN_ON(ondisk->snap_names_len);
609 header->snap_names = NULL;
610 header->snap_sizes = NULL;
613 header->features = 0; /* No features support in v1 images */
614 header->obj_order = ondisk->options.order;
615 header->crypt_type = ondisk->options.crypt_type;
616 header->comp_type = ondisk->options.comp_type;
618 /* Allocate and fill in the snapshot context */
620 header->image_size = le64_to_cpu(ondisk->image_size);
621 size = sizeof (struct ceph_snap_context);
622 size += snap_count * sizeof (header->snapc->snaps[0]);
623 header->snapc = kzalloc(size, GFP_KERNEL);
627 atomic_set(&header->snapc->nref, 1);
628 header->snapc->seq = le64_to_cpu(ondisk->snap_seq);
629 header->snapc->num_snaps = snap_count;
630 for (i = 0; i < snap_count; i++)
631 header->snapc->snaps[i] =
632 le64_to_cpu(ondisk->snaps[i].id);
637 kfree(header->snap_sizes);
638 header->snap_sizes = NULL;
639 kfree(header->snap_names);
640 header->snap_names = NULL;
641 kfree(header->object_prefix);
642 header->object_prefix = NULL;
647 static int snap_by_name(struct rbd_device *rbd_dev, const char *snap_name)
650 struct rbd_snap *snap;
652 list_for_each_entry(snap, &rbd_dev->snaps, node) {
653 if (!strcmp(snap_name, snap->name)) {
654 rbd_dev->mapping.snap_id = snap->id;
655 rbd_dev->mapping.size = snap->size;
656 rbd_dev->mapping.features = snap->features;
665 static int rbd_dev_set_mapping(struct rbd_device *rbd_dev, char *snap_name)
669 if (!memcmp(snap_name, RBD_SNAP_HEAD_NAME,
670 sizeof (RBD_SNAP_HEAD_NAME))) {
671 rbd_dev->mapping.snap_id = CEPH_NOSNAP;
672 rbd_dev->mapping.size = rbd_dev->header.image_size;
673 rbd_dev->mapping.features = rbd_dev->header.features;
674 rbd_dev->mapping.snap_exists = false;
675 rbd_dev->mapping.read_only = rbd_dev->rbd_opts.read_only;
678 ret = snap_by_name(rbd_dev, snap_name);
681 rbd_dev->mapping.snap_exists = true;
682 rbd_dev->mapping.read_only = true;
684 rbd_dev->mapping.snap_name = snap_name;
689 static void rbd_header_free(struct rbd_image_header *header)
691 kfree(header->object_prefix);
692 header->object_prefix = NULL;
693 kfree(header->snap_sizes);
694 header->snap_sizes = NULL;
695 kfree(header->snap_names);
696 header->snap_names = NULL;
697 ceph_put_snap_context(header->snapc);
698 header->snapc = NULL;
701 static char *rbd_segment_name(struct rbd_device *rbd_dev, u64 offset)
707 name = kmalloc(RBD_MAX_SEG_NAME_LEN + 1, GFP_NOIO);
710 segment = offset >> rbd_dev->header.obj_order;
711 ret = snprintf(name, RBD_MAX_SEG_NAME_LEN, "%s.%012llx",
712 rbd_dev->header.object_prefix, segment);
713 if (ret < 0 || ret >= RBD_MAX_SEG_NAME_LEN) {
714 pr_err("error formatting segment name for #%llu (%d)\n",
723 static u64 rbd_segment_offset(struct rbd_device *rbd_dev, u64 offset)
725 u64 segment_size = (u64) 1 << rbd_dev->header.obj_order;
727 return offset & (segment_size - 1);
730 static u64 rbd_segment_length(struct rbd_device *rbd_dev,
731 u64 offset, u64 length)
733 u64 segment_size = (u64) 1 << rbd_dev->header.obj_order;
735 offset &= segment_size - 1;
737 rbd_assert(length <= U64_MAX - offset);
738 if (offset + length > segment_size)
739 length = segment_size - offset;
744 static int rbd_get_num_segments(struct rbd_image_header *header,
752 if (len - 1 > U64_MAX - ofs)
755 start_seg = ofs >> header->obj_order;
756 end_seg = (ofs + len - 1) >> header->obj_order;
758 return end_seg - start_seg + 1;
762 * returns the size of an object in the image
764 static u64 rbd_obj_bytes(struct rbd_image_header *header)
766 return 1 << header->obj_order;
773 static void bio_chain_put(struct bio *chain)
779 chain = chain->bi_next;
785 * zeros a bio chain, starting at specific offset
787 static void zero_bio_chain(struct bio *chain, int start_ofs)
796 bio_for_each_segment(bv, chain, i) {
797 if (pos + bv->bv_len > start_ofs) {
798 int remainder = max(start_ofs - pos, 0);
799 buf = bvec_kmap_irq(bv, &flags);
800 memset(buf + remainder, 0,
801 bv->bv_len - remainder);
802 bvec_kunmap_irq(buf, &flags);
807 chain = chain->bi_next;
812 * bio_chain_clone - clone a chain of bios up to a certain length.
813 * might return a bio_pair that will need to be released.
815 static struct bio *bio_chain_clone(struct bio **old, struct bio **next,
816 struct bio_pair **bp,
817 int len, gfp_t gfpmask)
819 struct bio *old_chain = *old;
820 struct bio *new_chain = NULL;
825 bio_pair_release(*bp);
829 while (old_chain && (total < len)) {
832 tmp = bio_kmalloc(gfpmask, old_chain->bi_max_vecs);
835 gfpmask &= ~__GFP_WAIT; /* can't wait after the first */
837 if (total + old_chain->bi_size > len) {
841 * this split can only happen with a single paged bio,
842 * split_bio will BUG_ON if this is not the case
844 dout("bio_chain_clone split! total=%d remaining=%d"
846 total, len - total, old_chain->bi_size);
848 /* split the bio. We'll release it either in the next
849 call, or it will have to be released outside */
850 bp = bio_split(old_chain, (len - total) / SECTOR_SIZE);
854 __bio_clone(tmp, &bp->bio1);
858 __bio_clone(tmp, old_chain);
859 *next = old_chain->bi_next;
869 old_chain = old_chain->bi_next;
871 total += tmp->bi_size;
874 rbd_assert(total == len);
881 dout("bio_chain_clone with err\n");
882 bio_chain_put(new_chain);
887 * helpers for osd request op vectors.
889 static struct ceph_osd_req_op *rbd_create_rw_ops(int num_ops,
890 int opcode, u32 payload_len)
892 struct ceph_osd_req_op *ops;
894 ops = kzalloc(sizeof (*ops) * (num_ops + 1), GFP_NOIO);
901 * op extent offset and length will be set later on
902 * in calc_raw_layout()
904 ops[0].payload_len = payload_len;
909 static void rbd_destroy_ops(struct ceph_osd_req_op *ops)
914 static void rbd_coll_end_req_index(struct request *rq,
915 struct rbd_req_coll *coll,
919 struct request_queue *q;
922 dout("rbd_coll_end_req_index %p index %d ret %d len %llu\n",
923 coll, index, ret, (unsigned long long) len);
929 blk_end_request(rq, ret, len);
935 spin_lock_irq(q->queue_lock);
936 coll->status[index].done = 1;
937 coll->status[index].rc = ret;
938 coll->status[index].bytes = len;
939 max = min = coll->num_done;
940 while (max < coll->total && coll->status[max].done)
943 for (i = min; i<max; i++) {
944 __blk_end_request(rq, coll->status[i].rc,
945 coll->status[i].bytes);
947 kref_put(&coll->kref, rbd_coll_release);
949 spin_unlock_irq(q->queue_lock);
952 static void rbd_coll_end_req(struct rbd_request *req,
955 rbd_coll_end_req_index(req->rq, req->coll, req->coll_index, ret, len);
959 * Send ceph osd request
961 static int rbd_do_request(struct request *rq,
962 struct rbd_device *rbd_dev,
963 struct ceph_snap_context *snapc,
965 const char *object_name, u64 ofs, u64 len,
970 struct ceph_osd_req_op *ops,
971 struct rbd_req_coll *coll,
973 void (*rbd_cb)(struct ceph_osd_request *req,
974 struct ceph_msg *msg),
975 struct ceph_osd_request **linger_req,
978 struct ceph_osd_request *req;
979 struct ceph_file_layout *layout;
982 struct timespec mtime = CURRENT_TIME;
983 struct rbd_request *req_data;
984 struct ceph_osd_request_head *reqhead;
985 struct ceph_osd_client *osdc;
987 req_data = kzalloc(sizeof(*req_data), GFP_NOIO);
990 rbd_coll_end_req_index(rq, coll, coll_index,
996 req_data->coll = coll;
997 req_data->coll_index = coll_index;
1000 dout("rbd_do_request object_name=%s ofs=%llu len=%llu\n", object_name,
1001 (unsigned long long) ofs, (unsigned long long) len);
1003 osdc = &rbd_dev->rbd_client->client->osdc;
1004 req = ceph_osdc_alloc_request(osdc, flags, snapc, ops,
1005 false, GFP_NOIO, pages, bio);
1011 req->r_callback = rbd_cb;
1014 req_data->bio = bio;
1015 req_data->pages = pages;
1016 req_data->len = len;
1018 req->r_priv = req_data;
1020 reqhead = req->r_request->front.iov_base;
1021 reqhead->snapid = cpu_to_le64(CEPH_NOSNAP);
1023 strncpy(req->r_oid, object_name, sizeof(req->r_oid));
1024 req->r_oid_len = strlen(req->r_oid);
1026 layout = &req->r_file_layout;
1027 memset(layout, 0, sizeof(*layout));
1028 layout->fl_stripe_unit = cpu_to_le32(1 << RBD_MAX_OBJ_ORDER);
1029 layout->fl_stripe_count = cpu_to_le32(1);
1030 layout->fl_object_size = cpu_to_le32(1 << RBD_MAX_OBJ_ORDER);
1031 layout->fl_pg_pool = cpu_to_le32(rbd_dev->pool_id);
1032 ret = ceph_calc_raw_layout(osdc, layout, snapid, ofs, &len, &bno,
1034 rbd_assert(ret == 0);
1036 ceph_osdc_build_request(req, ofs, &len,
1040 req->r_oid, req->r_oid_len);
1043 ceph_osdc_set_request_linger(osdc, req);
1047 ret = ceph_osdc_start_request(osdc, req, false);
1052 ret = ceph_osdc_wait_request(osdc, req);
1054 *ver = le64_to_cpu(req->r_reassert_version.version);
1055 dout("reassert_ver=%llu\n",
1056 (unsigned long long)
1057 le64_to_cpu(req->r_reassert_version.version));
1058 ceph_osdc_put_request(req);
1063 bio_chain_put(req_data->bio);
1064 ceph_osdc_put_request(req);
1066 rbd_coll_end_req(req_data, ret, len);
1072 * Ceph osd op callback
1074 static void rbd_req_cb(struct ceph_osd_request *req, struct ceph_msg *msg)
1076 struct rbd_request *req_data = req->r_priv;
1077 struct ceph_osd_reply_head *replyhead;
1078 struct ceph_osd_op *op;
1084 replyhead = msg->front.iov_base;
1085 WARN_ON(le32_to_cpu(replyhead->num_ops) == 0);
1086 op = (void *)(replyhead + 1);
1087 rc = le32_to_cpu(replyhead->result);
1088 bytes = le64_to_cpu(op->extent.length);
1089 read_op = (le16_to_cpu(op->op) == CEPH_OSD_OP_READ);
1091 dout("rbd_req_cb bytes=%llu readop=%d rc=%d\n",
1092 (unsigned long long) bytes, read_op, (int) rc);
1094 if (rc == -ENOENT && read_op) {
1095 zero_bio_chain(req_data->bio, 0);
1097 } else if (rc == 0 && read_op && bytes < req_data->len) {
1098 zero_bio_chain(req_data->bio, bytes);
1099 bytes = req_data->len;
1102 rbd_coll_end_req(req_data, rc, bytes);
1105 bio_chain_put(req_data->bio);
1107 ceph_osdc_put_request(req);
1111 static void rbd_simple_req_cb(struct ceph_osd_request *req, struct ceph_msg *msg)
1113 ceph_osdc_put_request(req);
1117 * Do a synchronous ceph osd operation
1119 static int rbd_req_sync_op(struct rbd_device *rbd_dev,
1120 struct ceph_snap_context *snapc,
1123 struct ceph_osd_req_op *ops,
1124 const char *object_name,
1125 u64 ofs, u64 inbound_size,
1127 struct ceph_osd_request **linger_req,
1131 struct page **pages;
1134 rbd_assert(ops != NULL);
1136 num_pages = calc_pages_for(ofs, inbound_size);
1137 pages = ceph_alloc_page_vector(num_pages, GFP_KERNEL);
1139 return PTR_ERR(pages);
1141 ret = rbd_do_request(NULL, rbd_dev, snapc, snapid,
1142 object_name, ofs, inbound_size, NULL,
1152 if ((flags & CEPH_OSD_FLAG_READ) && inbound)
1153 ret = ceph_copy_from_page_vector(pages, inbound, ofs, ret);
1156 ceph_release_page_vector(pages, num_pages);
1161 * Do an asynchronous ceph osd operation
1163 static int rbd_do_op(struct request *rq,
1164 struct rbd_device *rbd_dev,
1165 struct ceph_snap_context *snapc,
1169 struct rbd_req_coll *coll,
1176 struct ceph_osd_req_op *ops;
1181 seg_name = rbd_segment_name(rbd_dev, ofs);
1184 seg_len = rbd_segment_length(rbd_dev, ofs, len);
1185 seg_ofs = rbd_segment_offset(rbd_dev, ofs);
1187 if (rq_data_dir(rq) == WRITE) {
1188 opcode = CEPH_OSD_OP_WRITE;
1189 flags = CEPH_OSD_FLAG_WRITE|CEPH_OSD_FLAG_ONDISK;
1190 payload_len = seg_len;
1192 opcode = CEPH_OSD_OP_READ;
1193 flags = CEPH_OSD_FLAG_READ;
1198 ops = rbd_create_rw_ops(1, opcode, payload_len);
1202 /* we've taken care of segment sizes earlier when we
1203 cloned the bios. We should never have a segment
1204 truncated at this point */
1205 rbd_assert(seg_len == len);
1207 ret = rbd_do_request(rq, rbd_dev, snapc, snapid,
1208 seg_name, seg_ofs, seg_len,
1214 rbd_req_cb, 0, NULL);
1216 rbd_destroy_ops(ops);
1223 * Request sync osd read
1225 static int rbd_req_sync_read(struct rbd_device *rbd_dev,
1227 const char *object_name,
1232 struct ceph_osd_req_op *ops;
1235 ops = rbd_create_rw_ops(1, CEPH_OSD_OP_READ, 0);
1239 ret = rbd_req_sync_op(rbd_dev, NULL,
1242 ops, object_name, ofs, len, buf, NULL, ver);
1243 rbd_destroy_ops(ops);
1249 * Request sync osd watch
1251 static int rbd_req_sync_notify_ack(struct rbd_device *rbd_dev,
1255 struct ceph_osd_req_op *ops;
1258 ops = rbd_create_rw_ops(1, CEPH_OSD_OP_NOTIFY_ACK, 0);
1262 ops[0].watch.ver = cpu_to_le64(ver);
1263 ops[0].watch.cookie = notify_id;
1264 ops[0].watch.flag = 0;
1266 ret = rbd_do_request(NULL, rbd_dev, NULL, CEPH_NOSNAP,
1267 rbd_dev->header_name, 0, 0, NULL,
1272 rbd_simple_req_cb, 0, NULL);
1274 rbd_destroy_ops(ops);
1278 static void rbd_watch_cb(u64 ver, u64 notify_id, u8 opcode, void *data)
1280 struct rbd_device *rbd_dev = (struct rbd_device *)data;
1287 dout("rbd_watch_cb %s notify_id=%llu opcode=%u\n",
1288 rbd_dev->header_name, (unsigned long long) notify_id,
1289 (unsigned int) opcode);
1290 rc = rbd_dev_refresh(rbd_dev, &hver);
1292 pr_warning(RBD_DRV_NAME "%d got notification but failed to "
1293 " update snaps: %d\n", rbd_dev->major, rc);
1295 rbd_req_sync_notify_ack(rbd_dev, hver, notify_id);
1299 * Request sync osd watch
1301 static int rbd_req_sync_watch(struct rbd_device *rbd_dev)
1303 struct ceph_osd_req_op *ops;
1304 struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc;
1307 ops = rbd_create_rw_ops(1, CEPH_OSD_OP_WATCH, 0);
1311 ret = ceph_osdc_create_event(osdc, rbd_watch_cb, 0,
1312 (void *)rbd_dev, &rbd_dev->watch_event);
1316 ops[0].watch.ver = cpu_to_le64(rbd_dev->header.obj_version);
1317 ops[0].watch.cookie = cpu_to_le64(rbd_dev->watch_event->cookie);
1318 ops[0].watch.flag = 1;
1320 ret = rbd_req_sync_op(rbd_dev, NULL,
1322 CEPH_OSD_FLAG_WRITE | CEPH_OSD_FLAG_ONDISK,
1324 rbd_dev->header_name,
1326 &rbd_dev->watch_request, NULL);
1331 rbd_destroy_ops(ops);
1335 ceph_osdc_cancel_event(rbd_dev->watch_event);
1336 rbd_dev->watch_event = NULL;
1338 rbd_destroy_ops(ops);
1343 * Request sync osd unwatch
1345 static int rbd_req_sync_unwatch(struct rbd_device *rbd_dev)
1347 struct ceph_osd_req_op *ops;
1350 ops = rbd_create_rw_ops(1, CEPH_OSD_OP_WATCH, 0);
1354 ops[0].watch.ver = 0;
1355 ops[0].watch.cookie = cpu_to_le64(rbd_dev->watch_event->cookie);
1356 ops[0].watch.flag = 0;
1358 ret = rbd_req_sync_op(rbd_dev, NULL,
1360 CEPH_OSD_FLAG_WRITE | CEPH_OSD_FLAG_ONDISK,
1362 rbd_dev->header_name,
1363 0, 0, NULL, NULL, NULL);
1366 rbd_destroy_ops(ops);
1367 ceph_osdc_cancel_event(rbd_dev->watch_event);
1368 rbd_dev->watch_event = NULL;
1373 * Synchronous osd object method call
1375 static int rbd_req_sync_exec(struct rbd_device *rbd_dev,
1376 const char *object_name,
1377 const char *class_name,
1378 const char *method_name,
1379 const char *outbound,
1380 size_t outbound_size,
1382 size_t inbound_size,
1386 struct ceph_osd_req_op *ops;
1387 int class_name_len = strlen(class_name);
1388 int method_name_len = strlen(method_name);
1393 * Any input parameters required by the method we're calling
1394 * will be sent along with the class and method names as
1395 * part of the message payload. That data and its size are
1396 * supplied via the indata and indata_len fields (named from
1397 * the perspective of the server side) in the OSD request
1400 payload_size = class_name_len + method_name_len + outbound_size;
1401 ops = rbd_create_rw_ops(1, CEPH_OSD_OP_CALL, payload_size);
1405 ops[0].cls.class_name = class_name;
1406 ops[0].cls.class_len = (__u8) class_name_len;
1407 ops[0].cls.method_name = method_name;
1408 ops[0].cls.method_len = (__u8) method_name_len;
1409 ops[0].cls.argc = 0;
1410 ops[0].cls.indata = outbound;
1411 ops[0].cls.indata_len = outbound_size;
1413 ret = rbd_req_sync_op(rbd_dev, NULL,
1416 object_name, 0, inbound_size, inbound,
1419 rbd_destroy_ops(ops);
1421 dout("cls_exec returned %d\n", ret);
1425 static struct rbd_req_coll *rbd_alloc_coll(int num_reqs)
1427 struct rbd_req_coll *coll =
1428 kzalloc(sizeof(struct rbd_req_coll) +
1429 sizeof(struct rbd_req_status) * num_reqs,
1434 coll->total = num_reqs;
1435 kref_init(&coll->kref);
1440 * block device queue callback
1442 static void rbd_rq_fn(struct request_queue *q)
1444 struct rbd_device *rbd_dev = q->queuedata;
1446 struct bio_pair *bp = NULL;
1448 while ((rq = blk_fetch_request(q))) {
1450 struct bio *rq_bio, *next_bio = NULL;
1455 int num_segs, cur_seg = 0;
1456 struct rbd_req_coll *coll;
1457 struct ceph_snap_context *snapc;
1459 dout("fetched request\n");
1461 /* filter out block requests we don't understand */
1462 if ((rq->cmd_type != REQ_TYPE_FS)) {
1463 __blk_end_request_all(rq, 0);
1467 /* deduce our operation (read, write) */
1468 do_write = (rq_data_dir(rq) == WRITE);
1470 size = blk_rq_bytes(rq);
1471 ofs = blk_rq_pos(rq) * SECTOR_SIZE;
1473 if (do_write && rbd_dev->mapping.read_only) {
1474 __blk_end_request_all(rq, -EROFS);
1478 spin_unlock_irq(q->queue_lock);
1480 down_read(&rbd_dev->header_rwsem);
1482 if (rbd_dev->mapping.snap_id != CEPH_NOSNAP &&
1483 !rbd_dev->mapping.snap_exists) {
1484 up_read(&rbd_dev->header_rwsem);
1485 dout("request for non-existent snapshot");
1486 spin_lock_irq(q->queue_lock);
1487 __blk_end_request_all(rq, -ENXIO);
1491 snapc = ceph_get_snap_context(rbd_dev->header.snapc);
1493 up_read(&rbd_dev->header_rwsem);
1495 dout("%s 0x%x bytes at 0x%llx\n",
1496 do_write ? "write" : "read",
1497 size, (unsigned long long) blk_rq_pos(rq) * SECTOR_SIZE);
1499 num_segs = rbd_get_num_segments(&rbd_dev->header, ofs, size);
1500 if (num_segs <= 0) {
1501 spin_lock_irq(q->queue_lock);
1502 __blk_end_request_all(rq, num_segs);
1503 ceph_put_snap_context(snapc);
1506 coll = rbd_alloc_coll(num_segs);
1508 spin_lock_irq(q->queue_lock);
1509 __blk_end_request_all(rq, -ENOMEM);
1510 ceph_put_snap_context(snapc);
1515 /* a bio clone to be passed down to OSD req */
1516 dout("rq->bio->bi_vcnt=%hu\n", rq->bio->bi_vcnt);
1517 op_size = rbd_segment_length(rbd_dev, ofs, size);
1518 kref_get(&coll->kref);
1519 bio = bio_chain_clone(&rq_bio, &next_bio, &bp,
1520 op_size, GFP_ATOMIC);
1522 rbd_coll_end_req_index(rq, coll, cur_seg,
1527 /* init OSD command: write or read */
1529 (void) rbd_do_op(rq, rbd_dev,
1534 (void) rbd_do_op(rq, rbd_dev,
1535 NULL, rbd_dev->mapping.snap_id,
1545 kref_put(&coll->kref, rbd_coll_release);
1548 bio_pair_release(bp);
1549 spin_lock_irq(q->queue_lock);
1551 ceph_put_snap_context(snapc);
1556 * a queue callback. Makes sure that we don't create a bio that spans across
1557 * multiple osd objects. One exception would be with a single page bios,
1558 * which we handle later at bio_chain_clone
1560 static int rbd_merge_bvec(struct request_queue *q, struct bvec_merge_data *bmd,
1561 struct bio_vec *bvec)
1563 struct rbd_device *rbd_dev = q->queuedata;
1564 unsigned int chunk_sectors;
1566 unsigned int bio_sectors;
1569 chunk_sectors = 1 << (rbd_dev->header.obj_order - SECTOR_SHIFT);
1570 sector = bmd->bi_sector + get_start_sect(bmd->bi_bdev);
1571 bio_sectors = bmd->bi_size >> SECTOR_SHIFT;
1573 max = (chunk_sectors - ((sector & (chunk_sectors - 1))
1574 + bio_sectors)) << SECTOR_SHIFT;
1576 max = 0; /* bio_add cannot handle a negative return */
1577 if (max <= bvec->bv_len && bio_sectors == 0)
1578 return bvec->bv_len;
1582 static void rbd_free_disk(struct rbd_device *rbd_dev)
1584 struct gendisk *disk = rbd_dev->disk;
1589 if (disk->flags & GENHD_FL_UP)
1592 blk_cleanup_queue(disk->queue);
1597 * Read the complete header for the given rbd device.
1599 * Returns a pointer to a dynamically-allocated buffer containing
1600 * the complete and validated header. Caller can pass the address
1601 * of a variable that will be filled in with the version of the
1602 * header object at the time it was read.
1604 * Returns a pointer-coded errno if a failure occurs.
1606 static struct rbd_image_header_ondisk *
1607 rbd_dev_v1_header_read(struct rbd_device *rbd_dev, u64 *version)
1609 struct rbd_image_header_ondisk *ondisk = NULL;
1616 * The complete header will include an array of its 64-bit
1617 * snapshot ids, followed by the names of those snapshots as
1618 * a contiguous block of NUL-terminated strings. Note that
1619 * the number of snapshots could change by the time we read
1620 * it in, in which case we re-read it.
1627 size = sizeof (*ondisk);
1628 size += snap_count * sizeof (struct rbd_image_snap_ondisk);
1630 ondisk = kmalloc(size, GFP_KERNEL);
1632 return ERR_PTR(-ENOMEM);
1634 ret = rbd_req_sync_read(rbd_dev, CEPH_NOSNAP,
1635 rbd_dev->header_name,
1637 (char *) ondisk, version);
1641 if (WARN_ON((size_t) ret < size)) {
1643 pr_warning("short header read for image %s"
1644 " (want %zd got %d)\n",
1645 rbd_dev->image_name, size, ret);
1648 if (!rbd_dev_ondisk_valid(ondisk)) {
1650 pr_warning("invalid header for image %s\n",
1651 rbd_dev->image_name);
1655 names_size = le64_to_cpu(ondisk->snap_names_len);
1656 want_count = snap_count;
1657 snap_count = le32_to_cpu(ondisk->snap_count);
1658 } while (snap_count != want_count);
1665 return ERR_PTR(ret);
1669 * reload the ondisk the header
1671 static int rbd_read_header(struct rbd_device *rbd_dev,
1672 struct rbd_image_header *header)
1674 struct rbd_image_header_ondisk *ondisk;
1678 ondisk = rbd_dev_v1_header_read(rbd_dev, &ver);
1680 return PTR_ERR(ondisk);
1681 ret = rbd_header_from_disk(header, ondisk);
1683 header->obj_version = ver;
1689 static void __rbd_remove_all_snaps(struct rbd_device *rbd_dev)
1691 struct rbd_snap *snap;
1692 struct rbd_snap *next;
1694 list_for_each_entry_safe(snap, next, &rbd_dev->snaps, node)
1695 __rbd_remove_snap_dev(snap);
1698 static void rbd_update_mapping_size(struct rbd_device *rbd_dev)
1702 if (rbd_dev->mapping.snap_id != CEPH_NOSNAP)
1705 size = (sector_t) rbd_dev->header.image_size / SECTOR_SIZE;
1706 dout("setting size to %llu sectors", (unsigned long long) size);
1707 rbd_dev->mapping.size = (u64) size;
1708 set_capacity(rbd_dev->disk, size);
1712 * only read the first part of the ondisk header, without the snaps info
1714 static int rbd_dev_v1_refresh(struct rbd_device *rbd_dev, u64 *hver)
1717 struct rbd_image_header h;
1719 ret = rbd_read_header(rbd_dev, &h);
1723 down_write(&rbd_dev->header_rwsem);
1725 /* Update image size, and check for resize of mapped image */
1726 rbd_dev->header.image_size = h.image_size;
1727 rbd_update_mapping_size(rbd_dev);
1729 /* rbd_dev->header.object_prefix shouldn't change */
1730 kfree(rbd_dev->header.snap_sizes);
1731 kfree(rbd_dev->header.snap_names);
1732 /* osd requests may still refer to snapc */
1733 ceph_put_snap_context(rbd_dev->header.snapc);
1736 *hver = h.obj_version;
1737 rbd_dev->header.obj_version = h.obj_version;
1738 rbd_dev->header.image_size = h.image_size;
1739 rbd_dev->header.snapc = h.snapc;
1740 rbd_dev->header.snap_names = h.snap_names;
1741 rbd_dev->header.snap_sizes = h.snap_sizes;
1742 /* Free the extra copy of the object prefix */
1743 WARN_ON(strcmp(rbd_dev->header.object_prefix, h.object_prefix));
1744 kfree(h.object_prefix);
1746 ret = rbd_dev_snaps_update(rbd_dev);
1748 ret = rbd_dev_snaps_register(rbd_dev);
1750 up_write(&rbd_dev->header_rwsem);
1755 static int rbd_dev_refresh(struct rbd_device *rbd_dev, u64 *hver)
1759 rbd_assert(rbd_image_format_valid(rbd_dev->image_format));
1760 mutex_lock_nested(&ctl_mutex, SINGLE_DEPTH_NESTING);
1761 if (rbd_dev->image_format == 1)
1762 ret = rbd_dev_v1_refresh(rbd_dev, hver);
1764 ret = rbd_dev_v2_refresh(rbd_dev, hver);
1765 mutex_unlock(&ctl_mutex);
1770 static int rbd_init_disk(struct rbd_device *rbd_dev)
1772 struct gendisk *disk;
1773 struct request_queue *q;
1776 /* create gendisk info */
1777 disk = alloc_disk(RBD_MINORS_PER_MAJOR);
1781 snprintf(disk->disk_name, sizeof(disk->disk_name), RBD_DRV_NAME "%d",
1783 disk->major = rbd_dev->major;
1784 disk->first_minor = 0;
1785 disk->fops = &rbd_bd_ops;
1786 disk->private_data = rbd_dev;
1789 q = blk_init_queue(rbd_rq_fn, &rbd_dev->lock);
1793 /* We use the default size, but let's be explicit about it. */
1794 blk_queue_physical_block_size(q, SECTOR_SIZE);
1796 /* set io sizes to object size */
1797 segment_size = rbd_obj_bytes(&rbd_dev->header);
1798 blk_queue_max_hw_sectors(q, segment_size / SECTOR_SIZE);
1799 blk_queue_max_segment_size(q, segment_size);
1800 blk_queue_io_min(q, segment_size);
1801 blk_queue_io_opt(q, segment_size);
1803 blk_queue_merge_bvec(q, rbd_merge_bvec);
1806 q->queuedata = rbd_dev;
1808 rbd_dev->disk = disk;
1810 set_capacity(rbd_dev->disk, rbd_dev->mapping.size / SECTOR_SIZE);
1823 static struct rbd_device *dev_to_rbd_dev(struct device *dev)
1825 return container_of(dev, struct rbd_device, dev);
1828 static ssize_t rbd_size_show(struct device *dev,
1829 struct device_attribute *attr, char *buf)
1831 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
1834 down_read(&rbd_dev->header_rwsem);
1835 size = get_capacity(rbd_dev->disk);
1836 up_read(&rbd_dev->header_rwsem);
1838 return sprintf(buf, "%llu\n", (unsigned long long) size * SECTOR_SIZE);
1842 * Note this shows the features for whatever's mapped, which is not
1843 * necessarily the base image.
1845 static ssize_t rbd_features_show(struct device *dev,
1846 struct device_attribute *attr, char *buf)
1848 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
1850 return sprintf(buf, "0x%016llx\n",
1851 (unsigned long long) rbd_dev->mapping.features);
1854 static ssize_t rbd_major_show(struct device *dev,
1855 struct device_attribute *attr, char *buf)
1857 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
1859 return sprintf(buf, "%d\n", rbd_dev->major);
1862 static ssize_t rbd_client_id_show(struct device *dev,
1863 struct device_attribute *attr, char *buf)
1865 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
1867 return sprintf(buf, "client%lld\n",
1868 ceph_client_id(rbd_dev->rbd_client->client));
1871 static ssize_t rbd_pool_show(struct device *dev,
1872 struct device_attribute *attr, char *buf)
1874 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
1876 return sprintf(buf, "%s\n", rbd_dev->pool_name);
1879 static ssize_t rbd_pool_id_show(struct device *dev,
1880 struct device_attribute *attr, char *buf)
1882 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
1884 return sprintf(buf, "%d\n", rbd_dev->pool_id);
1887 static ssize_t rbd_name_show(struct device *dev,
1888 struct device_attribute *attr, char *buf)
1890 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
1892 return sprintf(buf, "%s\n", rbd_dev->image_name);
1895 static ssize_t rbd_image_id_show(struct device *dev,
1896 struct device_attribute *attr, char *buf)
1898 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
1900 return sprintf(buf, "%s\n", rbd_dev->image_id);
1904 * Shows the name of the currently-mapped snapshot (or
1905 * RBD_SNAP_HEAD_NAME for the base image).
1907 static ssize_t rbd_snap_show(struct device *dev,
1908 struct device_attribute *attr,
1911 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
1913 return sprintf(buf, "%s\n", rbd_dev->mapping.snap_name);
1916 static ssize_t rbd_image_refresh(struct device *dev,
1917 struct device_attribute *attr,
1921 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
1924 ret = rbd_dev_refresh(rbd_dev, NULL);
1926 return ret < 0 ? ret : size;
1929 static DEVICE_ATTR(size, S_IRUGO, rbd_size_show, NULL);
1930 static DEVICE_ATTR(features, S_IRUGO, rbd_features_show, NULL);
1931 static DEVICE_ATTR(major, S_IRUGO, rbd_major_show, NULL);
1932 static DEVICE_ATTR(client_id, S_IRUGO, rbd_client_id_show, NULL);
1933 static DEVICE_ATTR(pool, S_IRUGO, rbd_pool_show, NULL);
1934 static DEVICE_ATTR(pool_id, S_IRUGO, rbd_pool_id_show, NULL);
1935 static DEVICE_ATTR(name, S_IRUGO, rbd_name_show, NULL);
1936 static DEVICE_ATTR(image_id, S_IRUGO, rbd_image_id_show, NULL);
1937 static DEVICE_ATTR(refresh, S_IWUSR, NULL, rbd_image_refresh);
1938 static DEVICE_ATTR(current_snap, S_IRUGO, rbd_snap_show, NULL);
1940 static struct attribute *rbd_attrs[] = {
1941 &dev_attr_size.attr,
1942 &dev_attr_features.attr,
1943 &dev_attr_major.attr,
1944 &dev_attr_client_id.attr,
1945 &dev_attr_pool.attr,
1946 &dev_attr_pool_id.attr,
1947 &dev_attr_name.attr,
1948 &dev_attr_image_id.attr,
1949 &dev_attr_current_snap.attr,
1950 &dev_attr_refresh.attr,
1954 static struct attribute_group rbd_attr_group = {
1958 static const struct attribute_group *rbd_attr_groups[] = {
1963 static void rbd_sysfs_dev_release(struct device *dev)
1967 static struct device_type rbd_device_type = {
1969 .groups = rbd_attr_groups,
1970 .release = rbd_sysfs_dev_release,
1978 static ssize_t rbd_snap_size_show(struct device *dev,
1979 struct device_attribute *attr,
1982 struct rbd_snap *snap = container_of(dev, struct rbd_snap, dev);
1984 return sprintf(buf, "%llu\n", (unsigned long long)snap->size);
1987 static ssize_t rbd_snap_id_show(struct device *dev,
1988 struct device_attribute *attr,
1991 struct rbd_snap *snap = container_of(dev, struct rbd_snap, dev);
1993 return sprintf(buf, "%llu\n", (unsigned long long)snap->id);
1996 static ssize_t rbd_snap_features_show(struct device *dev,
1997 struct device_attribute *attr,
2000 struct rbd_snap *snap = container_of(dev, struct rbd_snap, dev);
2002 return sprintf(buf, "0x%016llx\n",
2003 (unsigned long long) snap->features);
2006 static DEVICE_ATTR(snap_size, S_IRUGO, rbd_snap_size_show, NULL);
2007 static DEVICE_ATTR(snap_id, S_IRUGO, rbd_snap_id_show, NULL);
2008 static DEVICE_ATTR(snap_features, S_IRUGO, rbd_snap_features_show, NULL);
2010 static struct attribute *rbd_snap_attrs[] = {
2011 &dev_attr_snap_size.attr,
2012 &dev_attr_snap_id.attr,
2013 &dev_attr_snap_features.attr,
2017 static struct attribute_group rbd_snap_attr_group = {
2018 .attrs = rbd_snap_attrs,
2021 static void rbd_snap_dev_release(struct device *dev)
2023 struct rbd_snap *snap = container_of(dev, struct rbd_snap, dev);
2028 static const struct attribute_group *rbd_snap_attr_groups[] = {
2029 &rbd_snap_attr_group,
2033 static struct device_type rbd_snap_device_type = {
2034 .groups = rbd_snap_attr_groups,
2035 .release = rbd_snap_dev_release,
2038 static bool rbd_snap_registered(struct rbd_snap *snap)
2040 bool ret = snap->dev.type == &rbd_snap_device_type;
2041 bool reg = device_is_registered(&snap->dev);
2043 rbd_assert(!ret ^ reg);
2048 static void __rbd_remove_snap_dev(struct rbd_snap *snap)
2050 list_del(&snap->node);
2051 if (device_is_registered(&snap->dev))
2052 device_unregister(&snap->dev);
2055 static int rbd_register_snap_dev(struct rbd_snap *snap,
2056 struct device *parent)
2058 struct device *dev = &snap->dev;
2061 dev->type = &rbd_snap_device_type;
2062 dev->parent = parent;
2063 dev->release = rbd_snap_dev_release;
2064 dev_set_name(dev, "snap_%s", snap->name);
2065 dout("%s: registering device for snapshot %s\n", __func__, snap->name);
2067 ret = device_register(dev);
2072 static struct rbd_snap *__rbd_add_snap_dev(struct rbd_device *rbd_dev,
2073 const char *snap_name,
2074 u64 snap_id, u64 snap_size,
2077 struct rbd_snap *snap;
2080 snap = kzalloc(sizeof (*snap), GFP_KERNEL);
2082 return ERR_PTR(-ENOMEM);
2085 snap->name = kstrdup(snap_name, GFP_KERNEL);
2090 snap->size = snap_size;
2091 snap->features = snap_features;
2099 return ERR_PTR(ret);
2102 static char *rbd_dev_v1_snap_info(struct rbd_device *rbd_dev, u32 which,
2103 u64 *snap_size, u64 *snap_features)
2107 rbd_assert(which < rbd_dev->header.snapc->num_snaps);
2109 *snap_size = rbd_dev->header.snap_sizes[which];
2110 *snap_features = 0; /* No features for v1 */
2112 /* Skip over names until we find the one we are looking for */
2114 snap_name = rbd_dev->header.snap_names;
2116 snap_name += strlen(snap_name) + 1;
2122 * Get the size and object order for an image snapshot, or if
2123 * snap_id is CEPH_NOSNAP, gets this information for the base
2126 static int _rbd_dev_v2_snap_size(struct rbd_device *rbd_dev, u64 snap_id,
2127 u8 *order, u64 *snap_size)
2129 __le64 snapid = cpu_to_le64(snap_id);
2134 } __attribute__ ((packed)) size_buf = { 0 };
2136 ret = rbd_req_sync_exec(rbd_dev, rbd_dev->header_name,
2138 (char *) &snapid, sizeof (snapid),
2139 (char *) &size_buf, sizeof (size_buf),
2140 CEPH_OSD_FLAG_READ, NULL);
2141 dout("%s: rbd_req_sync_exec returned %d\n", __func__, ret);
2145 *order = size_buf.order;
2146 *snap_size = le64_to_cpu(size_buf.size);
2148 dout(" snap_id 0x%016llx order = %u, snap_size = %llu\n",
2149 (unsigned long long) snap_id, (unsigned int) *order,
2150 (unsigned long long) *snap_size);
2155 static int rbd_dev_v2_image_size(struct rbd_device *rbd_dev)
2157 return _rbd_dev_v2_snap_size(rbd_dev, CEPH_NOSNAP,
2158 &rbd_dev->header.obj_order,
2159 &rbd_dev->header.image_size);
2162 static int rbd_dev_v2_object_prefix(struct rbd_device *rbd_dev)
2168 reply_buf = kzalloc(RBD_OBJ_PREFIX_LEN_MAX, GFP_KERNEL);
2172 ret = rbd_req_sync_exec(rbd_dev, rbd_dev->header_name,
2173 "rbd", "get_object_prefix",
2175 reply_buf, RBD_OBJ_PREFIX_LEN_MAX,
2176 CEPH_OSD_FLAG_READ, NULL);
2177 dout("%s: rbd_req_sync_exec returned %d\n", __func__, ret);
2180 ret = 0; /* rbd_req_sync_exec() can return positive */
2183 rbd_dev->header.object_prefix = ceph_extract_encoded_string(&p,
2184 p + RBD_OBJ_PREFIX_LEN_MAX,
2187 if (IS_ERR(rbd_dev->header.object_prefix)) {
2188 ret = PTR_ERR(rbd_dev->header.object_prefix);
2189 rbd_dev->header.object_prefix = NULL;
2191 dout(" object_prefix = %s\n", rbd_dev->header.object_prefix);
2200 static int _rbd_dev_v2_snap_features(struct rbd_device *rbd_dev, u64 snap_id,
2203 __le64 snapid = cpu_to_le64(snap_id);
2207 } features_buf = { 0 };
2211 ret = rbd_req_sync_exec(rbd_dev, rbd_dev->header_name,
2212 "rbd", "get_features",
2213 (char *) &snapid, sizeof (snapid),
2214 (char *) &features_buf, sizeof (features_buf),
2215 CEPH_OSD_FLAG_READ, NULL);
2216 dout("%s: rbd_req_sync_exec returned %d\n", __func__, ret);
2220 incompat = le64_to_cpu(features_buf.incompat);
2221 if (incompat & ~RBD_FEATURES_ALL)
2224 *snap_features = le64_to_cpu(features_buf.features);
2226 dout(" snap_id 0x%016llx features = 0x%016llx incompat = 0x%016llx\n",
2227 (unsigned long long) snap_id,
2228 (unsigned long long) *snap_features,
2229 (unsigned long long) le64_to_cpu(features_buf.incompat));
2234 static int rbd_dev_v2_features(struct rbd_device *rbd_dev)
2236 return _rbd_dev_v2_snap_features(rbd_dev, CEPH_NOSNAP,
2237 &rbd_dev->header.features);
2240 static int rbd_dev_v2_snap_context(struct rbd_device *rbd_dev, u64 *ver)
2249 struct ceph_snap_context *snapc;
2253 * We'll need room for the seq value (maximum snapshot id),
2254 * snapshot count, and array of that many snapshot ids.
2255 * For now we have a fixed upper limit on the number we're
2256 * prepared to receive.
2258 size = sizeof (__le64) + sizeof (__le32) +
2259 RBD_MAX_SNAP_COUNT * sizeof (__le64);
2260 reply_buf = kzalloc(size, GFP_KERNEL);
2264 ret = rbd_req_sync_exec(rbd_dev, rbd_dev->header_name,
2265 "rbd", "get_snapcontext",
2268 CEPH_OSD_FLAG_READ, ver);
2269 dout("%s: rbd_req_sync_exec returned %d\n", __func__, ret);
2275 end = (char *) reply_buf + size;
2276 ceph_decode_64_safe(&p, end, seq, out);
2277 ceph_decode_32_safe(&p, end, snap_count, out);
2280 * Make sure the reported number of snapshot ids wouldn't go
2281 * beyond the end of our buffer. But before checking that,
2282 * make sure the computed size of the snapshot context we
2283 * allocate is representable in a size_t.
2285 if (snap_count > (SIZE_MAX - sizeof (struct ceph_snap_context))
2290 if (!ceph_has_room(&p, end, snap_count * sizeof (__le64)))
2293 size = sizeof (struct ceph_snap_context) +
2294 snap_count * sizeof (snapc->snaps[0]);
2295 snapc = kmalloc(size, GFP_KERNEL);
2301 atomic_set(&snapc->nref, 1);
2303 snapc->num_snaps = snap_count;
2304 for (i = 0; i < snap_count; i++)
2305 snapc->snaps[i] = ceph_decode_64(&p);
2307 rbd_dev->header.snapc = snapc;
2309 dout(" snap context seq = %llu, snap_count = %u\n",
2310 (unsigned long long) seq, (unsigned int) snap_count);
2318 static char *rbd_dev_v2_snap_name(struct rbd_device *rbd_dev, u32 which)
2326 size_t snap_name_len;
2329 size = sizeof (__le32) + RBD_MAX_SNAP_NAME_LEN;
2330 reply_buf = kmalloc(size, GFP_KERNEL);
2332 return ERR_PTR(-ENOMEM);
2334 snap_id = cpu_to_le64(rbd_dev->header.snapc->snaps[which]);
2335 ret = rbd_req_sync_exec(rbd_dev, rbd_dev->header_name,
2336 "rbd", "get_snapshot_name",
2337 (char *) &snap_id, sizeof (snap_id),
2339 CEPH_OSD_FLAG_READ, NULL);
2340 dout("%s: rbd_req_sync_exec returned %d\n", __func__, ret);
2345 end = (char *) reply_buf + size;
2347 snap_name = ceph_extract_encoded_string(&p, end, &snap_name_len,
2349 if (IS_ERR(snap_name)) {
2350 ret = PTR_ERR(snap_name);
2353 dout(" snap_id 0x%016llx snap_name = %s\n",
2354 (unsigned long long) le64_to_cpu(snap_id), snap_name);
2362 return ERR_PTR(ret);
2365 static char *rbd_dev_v2_snap_info(struct rbd_device *rbd_dev, u32 which,
2366 u64 *snap_size, u64 *snap_features)
2372 snap_id = rbd_dev->header.snapc->snaps[which];
2373 ret = _rbd_dev_v2_snap_size(rbd_dev, snap_id, &order, snap_size);
2375 return ERR_PTR(ret);
2376 ret = _rbd_dev_v2_snap_features(rbd_dev, snap_id, snap_features);
2378 return ERR_PTR(ret);
2380 return rbd_dev_v2_snap_name(rbd_dev, which);
2383 static char *rbd_dev_snap_info(struct rbd_device *rbd_dev, u32 which,
2384 u64 *snap_size, u64 *snap_features)
2386 if (rbd_dev->image_format == 1)
2387 return rbd_dev_v1_snap_info(rbd_dev, which,
2388 snap_size, snap_features);
2389 if (rbd_dev->image_format == 2)
2390 return rbd_dev_v2_snap_info(rbd_dev, which,
2391 snap_size, snap_features);
2392 return ERR_PTR(-EINVAL);
2395 static int rbd_dev_v2_refresh(struct rbd_device *rbd_dev, u64 *hver)
2400 down_write(&rbd_dev->header_rwsem);
2402 /* Grab old order first, to see if it changes */
2404 obj_order = rbd_dev->header.obj_order,
2405 ret = rbd_dev_v2_image_size(rbd_dev);
2408 if (rbd_dev->header.obj_order != obj_order) {
2412 rbd_update_mapping_size(rbd_dev);
2414 ret = rbd_dev_v2_snap_context(rbd_dev, hver);
2415 dout("rbd_dev_v2_snap_context returned %d\n", ret);
2418 ret = rbd_dev_snaps_update(rbd_dev);
2419 dout("rbd_dev_snaps_update returned %d\n", ret);
2422 ret = rbd_dev_snaps_register(rbd_dev);
2423 dout("rbd_dev_snaps_register returned %d\n", ret);
2425 up_write(&rbd_dev->header_rwsem);
2431 * Scan the rbd device's current snapshot list and compare it to the
2432 * newly-received snapshot context. Remove any existing snapshots
2433 * not present in the new snapshot context. Add a new snapshot for
2434 * any snaphots in the snapshot context not in the current list.
2435 * And verify there are no changes to snapshots we already know
2438 * Assumes the snapshots in the snapshot context are sorted by
2439 * snapshot id, highest id first. (Snapshots in the rbd_dev's list
2440 * are also maintained in that order.)
2442 static int rbd_dev_snaps_update(struct rbd_device *rbd_dev)
2444 struct ceph_snap_context *snapc = rbd_dev->header.snapc;
2445 const u32 snap_count = snapc->num_snaps;
2446 struct list_head *head = &rbd_dev->snaps;
2447 struct list_head *links = head->next;
2450 dout("%s: snap count is %u\n", __func__, (unsigned int) snap_count);
2451 while (index < snap_count || links != head) {
2453 struct rbd_snap *snap;
2456 u64 snap_features = 0;
2458 snap_id = index < snap_count ? snapc->snaps[index]
2460 snap = links != head ? list_entry(links, struct rbd_snap, node)
2462 rbd_assert(!snap || snap->id != CEPH_NOSNAP);
2464 if (snap_id == CEPH_NOSNAP || (snap && snap->id > snap_id)) {
2465 struct list_head *next = links->next;
2467 /* Existing snapshot not in the new snap context */
2469 if (rbd_dev->mapping.snap_id == snap->id)
2470 rbd_dev->mapping.snap_exists = false;
2471 __rbd_remove_snap_dev(snap);
2472 dout("%ssnap id %llu has been removed\n",
2473 rbd_dev->mapping.snap_id == snap->id ?
2475 (unsigned long long) snap->id);
2477 /* Done with this list entry; advance */
2483 snap_name = rbd_dev_snap_info(rbd_dev, index,
2484 &snap_size, &snap_features);
2485 if (IS_ERR(snap_name))
2486 return PTR_ERR(snap_name);
2488 dout("entry %u: snap_id = %llu\n", (unsigned int) snap_count,
2489 (unsigned long long) snap_id);
2490 if (!snap || (snap_id != CEPH_NOSNAP && snap->id < snap_id)) {
2491 struct rbd_snap *new_snap;
2493 /* We haven't seen this snapshot before */
2495 new_snap = __rbd_add_snap_dev(rbd_dev, snap_name,
2496 snap_id, snap_size, snap_features);
2497 if (IS_ERR(new_snap)) {
2498 int err = PTR_ERR(new_snap);
2500 dout(" failed to add dev, error %d\n", err);
2505 /* New goes before existing, or at end of list */
2507 dout(" added dev%s\n", snap ? "" : " at end\n");
2509 list_add_tail(&new_snap->node, &snap->node);
2511 list_add_tail(&new_snap->node, head);
2513 /* Already have this one */
2515 dout(" already present\n");
2517 rbd_assert(snap->size == snap_size);
2518 rbd_assert(!strcmp(snap->name, snap_name));
2519 rbd_assert(snap->features == snap_features);
2521 /* Done with this list entry; advance */
2523 links = links->next;
2526 /* Advance to the next entry in the snapshot context */
2530 dout("%s: done\n", __func__);
2536 * Scan the list of snapshots and register the devices for any that
2537 * have not already been registered.
2539 static int rbd_dev_snaps_register(struct rbd_device *rbd_dev)
2541 struct rbd_snap *snap;
2544 dout("%s called\n", __func__);
2545 if (WARN_ON(!device_is_registered(&rbd_dev->dev)))
2548 list_for_each_entry(snap, &rbd_dev->snaps, node) {
2549 if (!rbd_snap_registered(snap)) {
2550 ret = rbd_register_snap_dev(snap, &rbd_dev->dev);
2555 dout("%s: returning %d\n", __func__, ret);
2560 static int rbd_bus_add_dev(struct rbd_device *rbd_dev)
2565 mutex_lock_nested(&ctl_mutex, SINGLE_DEPTH_NESTING);
2567 dev = &rbd_dev->dev;
2568 dev->bus = &rbd_bus_type;
2569 dev->type = &rbd_device_type;
2570 dev->parent = &rbd_root_dev;
2571 dev->release = rbd_dev_release;
2572 dev_set_name(dev, "%d", rbd_dev->dev_id);
2573 ret = device_register(dev);
2575 mutex_unlock(&ctl_mutex);
2580 static void rbd_bus_del_dev(struct rbd_device *rbd_dev)
2582 device_unregister(&rbd_dev->dev);
2585 static int rbd_init_watch_dev(struct rbd_device *rbd_dev)
2590 ret = rbd_req_sync_watch(rbd_dev);
2591 if (ret == -ERANGE) {
2592 rc = rbd_dev_refresh(rbd_dev, NULL);
2596 } while (ret == -ERANGE);
2601 static atomic64_t rbd_dev_id_max = ATOMIC64_INIT(0);
2604 * Get a unique rbd identifier for the given new rbd_dev, and add
2605 * the rbd_dev to the global list. The minimum rbd id is 1.
2607 static void rbd_dev_id_get(struct rbd_device *rbd_dev)
2609 rbd_dev->dev_id = atomic64_inc_return(&rbd_dev_id_max);
2611 spin_lock(&rbd_dev_list_lock);
2612 list_add_tail(&rbd_dev->node, &rbd_dev_list);
2613 spin_unlock(&rbd_dev_list_lock);
2614 dout("rbd_dev %p given dev id %llu\n", rbd_dev,
2615 (unsigned long long) rbd_dev->dev_id);
2619 * Remove an rbd_dev from the global list, and record that its
2620 * identifier is no longer in use.
2622 static void rbd_dev_id_put(struct rbd_device *rbd_dev)
2624 struct list_head *tmp;
2625 int rbd_id = rbd_dev->dev_id;
2628 rbd_assert(rbd_id > 0);
2630 dout("rbd_dev %p released dev id %llu\n", rbd_dev,
2631 (unsigned long long) rbd_dev->dev_id);
2632 spin_lock(&rbd_dev_list_lock);
2633 list_del_init(&rbd_dev->node);
2636 * If the id being "put" is not the current maximum, there
2637 * is nothing special we need to do.
2639 if (rbd_id != atomic64_read(&rbd_dev_id_max)) {
2640 spin_unlock(&rbd_dev_list_lock);
2645 * We need to update the current maximum id. Search the
2646 * list to find out what it is. We're more likely to find
2647 * the maximum at the end, so search the list backward.
2650 list_for_each_prev(tmp, &rbd_dev_list) {
2651 struct rbd_device *rbd_dev;
2653 rbd_dev = list_entry(tmp, struct rbd_device, node);
2654 if (rbd_dev->dev_id > max_id)
2655 max_id = rbd_dev->dev_id;
2657 spin_unlock(&rbd_dev_list_lock);
2660 * The max id could have been updated by rbd_dev_id_get(), in
2661 * which case it now accurately reflects the new maximum.
2662 * Be careful not to overwrite the maximum value in that
2665 atomic64_cmpxchg(&rbd_dev_id_max, rbd_id, max_id);
2666 dout(" max dev id has been reset\n");
2670 * Skips over white space at *buf, and updates *buf to point to the
2671 * first found non-space character (if any). Returns the length of
2672 * the token (string of non-white space characters) found. Note
2673 * that *buf must be terminated with '\0'.
2675 static inline size_t next_token(const char **buf)
2678 * These are the characters that produce nonzero for
2679 * isspace() in the "C" and "POSIX" locales.
2681 const char *spaces = " \f\n\r\t\v";
2683 *buf += strspn(*buf, spaces); /* Find start of token */
2685 return strcspn(*buf, spaces); /* Return token length */
2689 * Finds the next token in *buf, and if the provided token buffer is
2690 * big enough, copies the found token into it. The result, if
2691 * copied, is guaranteed to be terminated with '\0'. Note that *buf
2692 * must be terminated with '\0' on entry.
2694 * Returns the length of the token found (not including the '\0').
2695 * Return value will be 0 if no token is found, and it will be >=
2696 * token_size if the token would not fit.
2698 * The *buf pointer will be updated to point beyond the end of the
2699 * found token. Note that this occurs even if the token buffer is
2700 * too small to hold it.
2702 static inline size_t copy_token(const char **buf,
2708 len = next_token(buf);
2709 if (len < token_size) {
2710 memcpy(token, *buf, len);
2711 *(token + len) = '\0';
2719 * Finds the next token in *buf, dynamically allocates a buffer big
2720 * enough to hold a copy of it, and copies the token into the new
2721 * buffer. The copy is guaranteed to be terminated with '\0'. Note
2722 * that a duplicate buffer is created even for a zero-length token.
2724 * Returns a pointer to the newly-allocated duplicate, or a null
2725 * pointer if memory for the duplicate was not available. If
2726 * the lenp argument is a non-null pointer, the length of the token
2727 * (not including the '\0') is returned in *lenp.
2729 * If successful, the *buf pointer will be updated to point beyond
2730 * the end of the found token.
2732 * Note: uses GFP_KERNEL for allocation.
2734 static inline char *dup_token(const char **buf, size_t *lenp)
2739 len = next_token(buf);
2740 dup = kmalloc(len + 1, GFP_KERNEL);
2744 memcpy(dup, *buf, len);
2745 *(dup + len) = '\0';
2755 * This fills in the pool_name, image_name, image_name_len, rbd_dev,
2756 * rbd_md_name, and name fields of the given rbd_dev, based on the
2757 * list of monitor addresses and other options provided via
2758 * /sys/bus/rbd/add. Returns a pointer to a dynamically-allocated
2759 * copy of the snapshot name to map if successful, or a
2760 * pointer-coded error otherwise.
2762 * Note: rbd_dev is assumed to have been initially zero-filled.
2764 static char *rbd_add_parse_args(struct rbd_device *rbd_dev,
2766 const char **mon_addrs,
2767 size_t *mon_addrs_size,
2769 size_t options_size)
2772 char *err_ptr = ERR_PTR(-EINVAL);
2775 /* The first four tokens are required */
2777 len = next_token(&buf);
2780 *mon_addrs_size = len + 1;
2785 len = copy_token(&buf, options, options_size);
2786 if (!len || len >= options_size)
2789 err_ptr = ERR_PTR(-ENOMEM);
2790 rbd_dev->pool_name = dup_token(&buf, NULL);
2791 if (!rbd_dev->pool_name)
2794 rbd_dev->image_name = dup_token(&buf, &rbd_dev->image_name_len);
2795 if (!rbd_dev->image_name)
2798 /* Snapshot name is optional */
2799 len = next_token(&buf);
2801 buf = RBD_SNAP_HEAD_NAME; /* No snapshot supplied */
2802 len = sizeof (RBD_SNAP_HEAD_NAME) - 1;
2804 snap_name = kmalloc(len + 1, GFP_KERNEL);
2807 memcpy(snap_name, buf, len);
2808 *(snap_name + len) = '\0';
2810 dout(" SNAP_NAME is <%s>, len is %zd\n", snap_name, len);
2815 kfree(rbd_dev->image_name);
2816 rbd_dev->image_name = NULL;
2817 rbd_dev->image_name_len = 0;
2818 kfree(rbd_dev->pool_name);
2819 rbd_dev->pool_name = NULL;
2825 * An rbd format 2 image has a unique identifier, distinct from the
2826 * name given to it by the user. Internally, that identifier is
2827 * what's used to specify the names of objects related to the image.
2829 * A special "rbd id" object is used to map an rbd image name to its
2830 * id. If that object doesn't exist, then there is no v2 rbd image
2831 * with the supplied name.
2833 * This function will record the given rbd_dev's image_id field if
2834 * it can be determined, and in that case will return 0. If any
2835 * errors occur a negative errno will be returned and the rbd_dev's
2836 * image_id field will be unchanged (and should be NULL).
2838 static int rbd_dev_image_id(struct rbd_device *rbd_dev)
2847 * First, see if the format 2 image id file exists, and if
2848 * so, get the image's persistent id from it.
2850 size = sizeof (RBD_ID_PREFIX) + rbd_dev->image_name_len;
2851 object_name = kmalloc(size, GFP_NOIO);
2854 sprintf(object_name, "%s%s", RBD_ID_PREFIX, rbd_dev->image_name);
2855 dout("rbd id object name is %s\n", object_name);
2857 /* Response will be an encoded string, which includes a length */
2859 size = sizeof (__le32) + RBD_IMAGE_ID_LEN_MAX;
2860 response = kzalloc(size, GFP_NOIO);
2866 ret = rbd_req_sync_exec(rbd_dev, object_name,
2869 response, RBD_IMAGE_ID_LEN_MAX,
2870 CEPH_OSD_FLAG_READ, NULL);
2871 dout("%s: rbd_req_sync_exec returned %d\n", __func__, ret);
2874 ret = 0; /* rbd_req_sync_exec() can return positive */
2877 rbd_dev->image_id = ceph_extract_encoded_string(&p,
2878 p + RBD_IMAGE_ID_LEN_MAX,
2879 &rbd_dev->image_id_len,
2881 if (IS_ERR(rbd_dev->image_id)) {
2882 ret = PTR_ERR(rbd_dev->image_id);
2883 rbd_dev->image_id = NULL;
2885 dout("image_id is %s\n", rbd_dev->image_id);
2894 static int rbd_dev_v1_probe(struct rbd_device *rbd_dev)
2899 /* Version 1 images have no id; empty string is used */
2901 rbd_dev->image_id = kstrdup("", GFP_KERNEL);
2902 if (!rbd_dev->image_id)
2904 rbd_dev->image_id_len = 0;
2906 /* Record the header object name for this rbd image. */
2908 size = rbd_dev->image_name_len + sizeof (RBD_SUFFIX);
2909 rbd_dev->header_name = kmalloc(size, GFP_KERNEL);
2910 if (!rbd_dev->header_name) {
2914 sprintf(rbd_dev->header_name, "%s%s", rbd_dev->image_name, RBD_SUFFIX);
2916 /* Populate rbd image metadata */
2918 ret = rbd_read_header(rbd_dev, &rbd_dev->header);
2921 rbd_dev->image_format = 1;
2923 dout("discovered version 1 image, header name is %s\n",
2924 rbd_dev->header_name);
2929 kfree(rbd_dev->header_name);
2930 rbd_dev->header_name = NULL;
2931 kfree(rbd_dev->image_id);
2932 rbd_dev->image_id = NULL;
2937 static int rbd_dev_v2_probe(struct rbd_device *rbd_dev)
2944 * Image id was filled in by the caller. Record the header
2945 * object name for this rbd image.
2947 size = sizeof (RBD_HEADER_PREFIX) + rbd_dev->image_id_len;
2948 rbd_dev->header_name = kmalloc(size, GFP_KERNEL);
2949 if (!rbd_dev->header_name)
2951 sprintf(rbd_dev->header_name, "%s%s",
2952 RBD_HEADER_PREFIX, rbd_dev->image_id);
2954 /* Get the size and object order for the image */
2956 ret = rbd_dev_v2_image_size(rbd_dev);
2960 /* Get the object prefix (a.k.a. block_name) for the image */
2962 ret = rbd_dev_v2_object_prefix(rbd_dev);
2966 /* Get the and check features for the image */
2968 ret = rbd_dev_v2_features(rbd_dev);
2972 /* crypto and compression type aren't (yet) supported for v2 images */
2974 rbd_dev->header.crypt_type = 0;
2975 rbd_dev->header.comp_type = 0;
2977 /* Get the snapshot context, plus the header version */
2979 ret = rbd_dev_v2_snap_context(rbd_dev, &ver);
2982 rbd_dev->header.obj_version = ver;
2984 rbd_dev->image_format = 2;
2986 dout("discovered version 2 image, header name is %s\n",
2987 rbd_dev->header_name);
2991 kfree(rbd_dev->header_name);
2992 rbd_dev->header_name = NULL;
2993 kfree(rbd_dev->header.object_prefix);
2994 rbd_dev->header.object_prefix = NULL;
3000 * Probe for the existence of the header object for the given rbd
3001 * device. For format 2 images this includes determining the image
3004 static int rbd_dev_probe(struct rbd_device *rbd_dev)
3009 * Get the id from the image id object. If it's not a
3010 * format 2 image, we'll get ENOENT back, and we'll assume
3011 * it's a format 1 image.
3013 ret = rbd_dev_image_id(rbd_dev);
3015 ret = rbd_dev_v1_probe(rbd_dev);
3017 ret = rbd_dev_v2_probe(rbd_dev);
3019 dout("probe failed, returning %d\n", ret);
3024 static ssize_t rbd_add(struct bus_type *bus,
3029 struct rbd_device *rbd_dev = NULL;
3030 const char *mon_addrs = NULL;
3031 size_t mon_addrs_size = 0;
3032 struct ceph_osd_client *osdc;
3036 if (!try_module_get(THIS_MODULE))
3039 options = kmalloc(count, GFP_KERNEL);
3042 rbd_dev = kzalloc(sizeof(*rbd_dev), GFP_KERNEL);
3046 /* static rbd_device initialization */
3047 spin_lock_init(&rbd_dev->lock);
3048 INIT_LIST_HEAD(&rbd_dev->node);
3049 INIT_LIST_HEAD(&rbd_dev->snaps);
3050 init_rwsem(&rbd_dev->header_rwsem);
3052 /* parse add command */
3053 snap_name = rbd_add_parse_args(rbd_dev, buf,
3054 &mon_addrs, &mon_addrs_size, options, count);
3055 if (IS_ERR(snap_name)) {
3056 rc = PTR_ERR(snap_name);
3060 rc = rbd_get_client(rbd_dev, mon_addrs, mon_addrs_size - 1, options);
3065 osdc = &rbd_dev->rbd_client->client->osdc;
3066 rc = ceph_pg_poolid_by_name(osdc->osdmap, rbd_dev->pool_name);
3068 goto err_out_client;
3069 rbd_dev->pool_id = rc;
3071 rc = rbd_dev_probe(rbd_dev);
3073 goto err_out_client;
3075 /* no need to lock here, as rbd_dev is not registered yet */
3076 rc = rbd_dev_snaps_update(rbd_dev);
3078 goto err_out_header;
3080 rc = rbd_dev_set_mapping(rbd_dev, snap_name);
3082 goto err_out_header;
3084 /* generate unique id: find highest unique id, add one */
3085 rbd_dev_id_get(rbd_dev);
3087 /* Fill in the device name, now that we have its id. */
3088 BUILD_BUG_ON(DEV_NAME_LEN
3089 < sizeof (RBD_DRV_NAME) + MAX_INT_FORMAT_WIDTH);
3090 sprintf(rbd_dev->name, "%s%d", RBD_DRV_NAME, rbd_dev->dev_id);
3092 /* Get our block major device number. */
3094 rc = register_blkdev(0, rbd_dev->name);
3097 rbd_dev->major = rc;
3099 /* Set up the blkdev mapping. */
3101 rc = rbd_init_disk(rbd_dev);
3103 goto err_out_blkdev;
3105 rc = rbd_bus_add_dev(rbd_dev);
3110 * At this point cleanup in the event of an error is the job
3111 * of the sysfs code (initiated by rbd_bus_del_dev()).
3114 down_write(&rbd_dev->header_rwsem);
3115 rc = rbd_dev_snaps_register(rbd_dev);
3116 up_write(&rbd_dev->header_rwsem);
3120 rc = rbd_init_watch_dev(rbd_dev);
3124 /* Everything's ready. Announce the disk to the world. */
3126 add_disk(rbd_dev->disk);
3128 pr_info("%s: added with size 0x%llx\n", rbd_dev->disk->disk_name,
3129 (unsigned long long) rbd_dev->mapping.size);
3134 /* this will also clean up rest of rbd_dev stuff */
3136 rbd_bus_del_dev(rbd_dev);
3141 rbd_free_disk(rbd_dev);
3143 unregister_blkdev(rbd_dev->major, rbd_dev->name);
3145 rbd_dev_id_put(rbd_dev);
3147 rbd_header_free(&rbd_dev->header);
3149 kfree(rbd_dev->header_name);
3150 rbd_put_client(rbd_dev);
3151 kfree(rbd_dev->image_id);
3153 kfree(rbd_dev->mapping.snap_name);
3154 kfree(rbd_dev->image_name);
3155 kfree(rbd_dev->pool_name);
3160 dout("Error adding device %s\n", buf);
3161 module_put(THIS_MODULE);
3163 return (ssize_t) rc;
3166 static struct rbd_device *__rbd_get_dev(unsigned long dev_id)
3168 struct list_head *tmp;
3169 struct rbd_device *rbd_dev;
3171 spin_lock(&rbd_dev_list_lock);
3172 list_for_each(tmp, &rbd_dev_list) {
3173 rbd_dev = list_entry(tmp, struct rbd_device, node);
3174 if (rbd_dev->dev_id == dev_id) {
3175 spin_unlock(&rbd_dev_list_lock);
3179 spin_unlock(&rbd_dev_list_lock);
3183 static void rbd_dev_release(struct device *dev)
3185 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
3187 if (rbd_dev->watch_request) {
3188 struct ceph_client *client = rbd_dev->rbd_client->client;
3190 ceph_osdc_unregister_linger_request(&client->osdc,
3191 rbd_dev->watch_request);
3193 if (rbd_dev->watch_event)
3194 rbd_req_sync_unwatch(rbd_dev);
3196 rbd_put_client(rbd_dev);
3198 /* clean up and free blkdev */
3199 rbd_free_disk(rbd_dev);
3200 unregister_blkdev(rbd_dev->major, rbd_dev->name);
3202 /* release allocated disk header fields */
3203 rbd_header_free(&rbd_dev->header);
3205 /* done with the id, and with the rbd_dev */
3206 kfree(rbd_dev->mapping.snap_name);
3207 kfree(rbd_dev->image_id);
3208 kfree(rbd_dev->header_name);
3209 kfree(rbd_dev->pool_name);
3210 kfree(rbd_dev->image_name);
3211 rbd_dev_id_put(rbd_dev);
3214 /* release module ref */
3215 module_put(THIS_MODULE);
3218 static ssize_t rbd_remove(struct bus_type *bus,
3222 struct rbd_device *rbd_dev = NULL;
3227 rc = strict_strtoul(buf, 10, &ul);
3231 /* convert to int; abort if we lost anything in the conversion */
3232 target_id = (int) ul;
3233 if (target_id != ul)
3236 mutex_lock_nested(&ctl_mutex, SINGLE_DEPTH_NESTING);
3238 rbd_dev = __rbd_get_dev(target_id);
3244 __rbd_remove_all_snaps(rbd_dev);
3245 rbd_bus_del_dev(rbd_dev);
3248 mutex_unlock(&ctl_mutex);
3254 * create control files in sysfs
3257 static int rbd_sysfs_init(void)
3261 ret = device_register(&rbd_root_dev);
3265 ret = bus_register(&rbd_bus_type);
3267 device_unregister(&rbd_root_dev);
3272 static void rbd_sysfs_cleanup(void)
3274 bus_unregister(&rbd_bus_type);
3275 device_unregister(&rbd_root_dev);
3278 int __init rbd_init(void)
3282 rc = rbd_sysfs_init();
3285 pr_info("loaded " RBD_DRV_NAME_LONG "\n");
3289 void __exit rbd_exit(void)
3291 rbd_sysfs_cleanup();
3294 module_init(rbd_init);
3295 module_exit(rbd_exit);
3297 MODULE_AUTHOR("Sage Weil <sage@newdream.net>");
3298 MODULE_AUTHOR("Yehuda Sadeh <yehuda@hq.newdream.net>");
3299 MODULE_DESCRIPTION("rados block device");
3301 /* following authorship retained from original osdblk.c */
3302 MODULE_AUTHOR("Jeff Garzik <jeff@garzik.org>");
3304 MODULE_LICENSE("GPL");