3 rbd.c -- Export ceph rados objects as a Linux block device
6 based on drivers/block/osdblk.c:
8 Copyright 2009 Red Hat, Inc.
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; see the file COPYING. If not, write to
21 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
25 For usage instructions, please refer to:
27 Documentation/ABI/testing/sysfs-bus-rbd
31 #include <linux/ceph/libceph.h>
32 #include <linux/ceph/osd_client.h>
33 #include <linux/ceph/mon_client.h>
34 #include <linux/ceph/decode.h>
35 #include <linux/parser.h>
37 #include <linux/kernel.h>
38 #include <linux/device.h>
39 #include <linux/module.h>
41 #include <linux/blkdev.h>
43 #include "rbd_types.h"
45 #define RBD_DEBUG /* Activate rbd_assert() calls */
48 * The basic unit of block I/O is a sector. It is interpreted in a
49 * number of contexts in Linux (blk, bio, genhd), but the default is
50 * universally 512 bytes. These symbols are just slightly more
51 * meaningful than the bare numbers they represent.
53 #define SECTOR_SHIFT 9
54 #define SECTOR_SIZE (1ULL << SECTOR_SHIFT)
56 #define RBD_DRV_NAME "rbd"
57 #define RBD_DRV_NAME_LONG "rbd (rados block device)"
59 #define RBD_MINORS_PER_MAJOR 256 /* max minors per blkdev */
61 #define RBD_SNAP_DEV_NAME_PREFIX "snap_"
62 #define RBD_MAX_SNAP_NAME_LEN \
63 (NAME_MAX - (sizeof (RBD_SNAP_DEV_NAME_PREFIX) - 1))
65 #define RBD_MAX_SNAP_COUNT 510 /* allows max snapc to fit in 4KB */
67 #define RBD_SNAP_HEAD_NAME "-"
69 #define BAD_SNAP_INDEX U32_MAX /* invalid index into snap array */
71 /* This allows a single page to hold an image name sent by OSD */
72 #define RBD_IMAGE_NAME_LEN_MAX (PAGE_SIZE - sizeof (__le32) - 1)
73 #define RBD_IMAGE_ID_LEN_MAX 64
75 #define RBD_OBJ_PREFIX_LEN_MAX 64
79 #define RBD_FEATURE_LAYERING (1<<0)
80 #define RBD_FEATURE_STRIPINGV2 (1<<1)
81 #define RBD_FEATURES_ALL \
82 (RBD_FEATURE_LAYERING | RBD_FEATURE_STRIPINGV2)
84 /* Features supported by this (client software) implementation. */
86 #define RBD_FEATURES_SUPPORTED (RBD_FEATURES_ALL)
89 * An RBD device name will be "rbd#", where the "rbd" comes from
90 * RBD_DRV_NAME above, and # is a unique integer identifier.
91 * MAX_INT_FORMAT_WIDTH is used in ensuring DEV_NAME_LEN is big
92 * enough to hold all possible device names.
94 #define DEV_NAME_LEN 32
95 #define MAX_INT_FORMAT_WIDTH ((5 * sizeof (int)) / 2 + 1)
98 * block device image metadata (in-memory version)
100 struct rbd_image_header {
101 /* These four fields never change for a given rbd image */
108 /* The remaining fields need to be updated occasionally */
110 struct ceph_snap_context *snapc;
119 * An rbd image specification.
121 * The tuple (pool_id, image_id, snap_id) is sufficient to uniquely
122 * identify an image. Each rbd_dev structure includes a pointer to
123 * an rbd_spec structure that encapsulates this identity.
125 * Each of the id's in an rbd_spec has an associated name. For a
126 * user-mapped image, the names are supplied and the id's associated
127 * with them are looked up. For a layered image, a parent image is
128 * defined by the tuple, and the names are looked up.
130 * An rbd_dev structure contains a parent_spec pointer which is
131 * non-null if the image it represents is a child in a layered
132 * image. This pointer will refer to the rbd_spec structure used
133 * by the parent rbd_dev for its own identity (i.e., the structure
134 * is shared between the parent and child).
136 * Since these structures are populated once, during the discovery
137 * phase of image construction, they are effectively immutable so
138 * we make no effort to synchronize access to them.
140 * Note that code herein does not assume the image name is known (it
141 * could be a null pointer).
145 const char *pool_name;
147 const char *image_id;
148 const char *image_name;
151 const char *snap_name;
157 * an instance of the client. multiple devices may share an rbd client.
160 struct ceph_client *client;
162 struct list_head node;
165 struct rbd_img_request;
166 typedef void (*rbd_img_callback_t)(struct rbd_img_request *);
168 #define BAD_WHICH U32_MAX /* Good which or bad which, which? */
170 struct rbd_obj_request;
171 typedef void (*rbd_obj_callback_t)(struct rbd_obj_request *);
173 enum obj_request_type {
174 OBJ_REQUEST_NODATA, OBJ_REQUEST_BIO, OBJ_REQUEST_PAGES
178 OBJ_REQ_DONE, /* completion flag: not done = 0, done = 1 */
179 OBJ_REQ_IMG_DATA, /* object usage: standalone = 0, image = 1 */
180 OBJ_REQ_KNOWN, /* EXISTS flag valid: no = 0, yes = 1 */
181 OBJ_REQ_EXISTS, /* target exists: no = 0, yes = 1 */
184 struct rbd_obj_request {
185 const char *object_name;
186 u64 offset; /* object start byte */
187 u64 length; /* bytes from offset */
191 * An object request associated with an image will have its
192 * img_data flag set; a standalone object request will not.
194 * A standalone object request will have which == BAD_WHICH
195 * and a null obj_request pointer.
197 * An object request initiated in support of a layered image
198 * object (to check for its existence before a write) will
199 * have which == BAD_WHICH and a non-null obj_request pointer.
201 * Finally, an object request for rbd image data will have
202 * which != BAD_WHICH, and will have a non-null img_request
203 * pointer. The value of which will be in the range
204 * 0..(img_request->obj_request_count-1).
207 struct rbd_obj_request *obj_request; /* STAT op */
209 struct rbd_img_request *img_request;
211 /* links for img_request->obj_requests list */
212 struct list_head links;
215 u32 which; /* posn image request list */
217 enum obj_request_type type;
219 struct bio *bio_list;
225 struct page **copyup_pages;
227 struct ceph_osd_request *osd_req;
229 u64 xferred; /* bytes transferred */
232 rbd_obj_callback_t callback;
233 struct completion completion;
239 IMG_REQ_WRITE, /* I/O direction: read = 0, write = 1 */
240 IMG_REQ_CHILD, /* initiator: block = 0, child image = 1 */
241 IMG_REQ_LAYERED, /* ENOENT handling: normal = 0, layered = 1 */
244 struct rbd_img_request {
245 struct rbd_device *rbd_dev;
246 u64 offset; /* starting image byte offset */
247 u64 length; /* byte count from offset */
250 u64 snap_id; /* for reads */
251 struct ceph_snap_context *snapc; /* for writes */
254 struct request *rq; /* block request */
255 struct rbd_obj_request *obj_request; /* obj req initiator */
257 struct page **copyup_pages;
258 spinlock_t completion_lock;/* protects next_completion */
260 rbd_img_callback_t callback;
261 u64 xferred;/* aggregate bytes transferred */
262 int result; /* first nonzero obj_request result */
264 u32 obj_request_count;
265 struct list_head obj_requests; /* rbd_obj_request structs */
270 #define for_each_obj_request(ireq, oreq) \
271 list_for_each_entry(oreq, &(ireq)->obj_requests, links)
272 #define for_each_obj_request_from(ireq, oreq) \
273 list_for_each_entry_from(oreq, &(ireq)->obj_requests, links)
274 #define for_each_obj_request_safe(ireq, oreq, n) \
275 list_for_each_entry_safe_reverse(oreq, n, &(ireq)->obj_requests, links)
287 int dev_id; /* blkdev unique id */
289 int major; /* blkdev assigned major */
290 struct gendisk *disk; /* blkdev's gendisk and rq */
292 u32 image_format; /* Either 1 or 2 */
293 struct rbd_client *rbd_client;
295 char name[DEV_NAME_LEN]; /* blkdev name, e.g. rbd3 */
297 spinlock_t lock; /* queue, flags, open_count */
299 struct rbd_image_header header;
300 unsigned long flags; /* possibly lock protected */
301 struct rbd_spec *spec;
305 struct ceph_file_layout layout;
307 struct ceph_osd_event *watch_event;
308 struct rbd_obj_request *watch_request;
310 struct rbd_spec *parent_spec;
312 struct rbd_device *parent;
314 /* protects updating the header */
315 struct rw_semaphore header_rwsem;
317 struct rbd_mapping mapping;
319 struct list_head node;
323 unsigned long open_count; /* protected by lock */
327 * Flag bits for rbd_dev->flags. If atomicity is required,
328 * rbd_dev->lock is used to protect access.
330 * Currently, only the "removing" flag (which is coupled with the
331 * "open_count" field) requires atomic access.
334 RBD_DEV_FLAG_EXISTS, /* mapped snapshot has not been deleted */
335 RBD_DEV_FLAG_REMOVING, /* this mapping is being removed */
338 static DEFINE_MUTEX(ctl_mutex); /* Serialize open/close/setup/teardown */
340 static LIST_HEAD(rbd_dev_list); /* devices */
341 static DEFINE_SPINLOCK(rbd_dev_list_lock);
343 static LIST_HEAD(rbd_client_list); /* clients */
344 static DEFINE_SPINLOCK(rbd_client_list_lock);
346 static int rbd_img_request_submit(struct rbd_img_request *img_request);
348 static void rbd_dev_device_release(struct device *dev);
350 static ssize_t rbd_add(struct bus_type *bus, const char *buf,
352 static ssize_t rbd_remove(struct bus_type *bus, const char *buf,
354 static int rbd_dev_image_probe(struct rbd_device *rbd_dev);
356 static struct bus_attribute rbd_bus_attrs[] = {
357 __ATTR(add, S_IWUSR, NULL, rbd_add),
358 __ATTR(remove, S_IWUSR, NULL, rbd_remove),
362 static struct bus_type rbd_bus_type = {
364 .bus_attrs = rbd_bus_attrs,
367 static void rbd_root_dev_release(struct device *dev)
371 static struct device rbd_root_dev = {
373 .release = rbd_root_dev_release,
376 static __printf(2, 3)
377 void rbd_warn(struct rbd_device *rbd_dev, const char *fmt, ...)
379 struct va_format vaf;
387 printk(KERN_WARNING "%s: %pV\n", RBD_DRV_NAME, &vaf);
388 else if (rbd_dev->disk)
389 printk(KERN_WARNING "%s: %s: %pV\n",
390 RBD_DRV_NAME, rbd_dev->disk->disk_name, &vaf);
391 else if (rbd_dev->spec && rbd_dev->spec->image_name)
392 printk(KERN_WARNING "%s: image %s: %pV\n",
393 RBD_DRV_NAME, rbd_dev->spec->image_name, &vaf);
394 else if (rbd_dev->spec && rbd_dev->spec->image_id)
395 printk(KERN_WARNING "%s: id %s: %pV\n",
396 RBD_DRV_NAME, rbd_dev->spec->image_id, &vaf);
398 printk(KERN_WARNING "%s: rbd_dev %p: %pV\n",
399 RBD_DRV_NAME, rbd_dev, &vaf);
404 #define rbd_assert(expr) \
405 if (unlikely(!(expr))) { \
406 printk(KERN_ERR "\nAssertion failure in %s() " \
408 "\trbd_assert(%s);\n\n", \
409 __func__, __LINE__, #expr); \
412 #else /* !RBD_DEBUG */
413 # define rbd_assert(expr) ((void) 0)
414 #endif /* !RBD_DEBUG */
416 static int rbd_img_obj_request_submit(struct rbd_obj_request *obj_request);
417 static void rbd_img_parent_read(struct rbd_obj_request *obj_request);
418 static void rbd_dev_remove_parent(struct rbd_device *rbd_dev);
420 static int rbd_dev_refresh(struct rbd_device *rbd_dev);
421 static int rbd_dev_v2_refresh(struct rbd_device *rbd_dev);
422 static const char *rbd_dev_v2_snap_name(struct rbd_device *rbd_dev,
424 static int _rbd_dev_v2_snap_size(struct rbd_device *rbd_dev, u64 snap_id,
425 u8 *order, u64 *snap_size);
426 static int _rbd_dev_v2_snap_features(struct rbd_device *rbd_dev, u64 snap_id,
428 static u64 rbd_snap_id_by_name(struct rbd_device *rbd_dev, const char *name);
430 static int rbd_open(struct block_device *bdev, fmode_t mode)
432 struct rbd_device *rbd_dev = bdev->bd_disk->private_data;
433 bool removing = false;
435 if ((mode & FMODE_WRITE) && rbd_dev->mapping.read_only)
438 spin_lock_irq(&rbd_dev->lock);
439 if (test_bit(RBD_DEV_FLAG_REMOVING, &rbd_dev->flags))
442 rbd_dev->open_count++;
443 spin_unlock_irq(&rbd_dev->lock);
447 mutex_lock_nested(&ctl_mutex, SINGLE_DEPTH_NESTING);
448 (void) get_device(&rbd_dev->dev);
449 set_device_ro(bdev, rbd_dev->mapping.read_only);
450 mutex_unlock(&ctl_mutex);
455 static int rbd_release(struct gendisk *disk, fmode_t mode)
457 struct rbd_device *rbd_dev = disk->private_data;
458 unsigned long open_count_before;
460 spin_lock_irq(&rbd_dev->lock);
461 open_count_before = rbd_dev->open_count--;
462 spin_unlock_irq(&rbd_dev->lock);
463 rbd_assert(open_count_before > 0);
465 mutex_lock_nested(&ctl_mutex, SINGLE_DEPTH_NESTING);
466 put_device(&rbd_dev->dev);
467 mutex_unlock(&ctl_mutex);
472 static const struct block_device_operations rbd_bd_ops = {
473 .owner = THIS_MODULE,
475 .release = rbd_release,
479 * Initialize an rbd client instance.
482 static struct rbd_client *rbd_client_create(struct ceph_options *ceph_opts)
484 struct rbd_client *rbdc;
487 dout("%s:\n", __func__);
488 rbdc = kmalloc(sizeof(struct rbd_client), GFP_KERNEL);
492 kref_init(&rbdc->kref);
493 INIT_LIST_HEAD(&rbdc->node);
495 mutex_lock_nested(&ctl_mutex, SINGLE_DEPTH_NESTING);
497 rbdc->client = ceph_create_client(ceph_opts, rbdc, 0, 0);
498 if (IS_ERR(rbdc->client))
500 ceph_opts = NULL; /* Now rbdc->client is responsible for ceph_opts */
502 ret = ceph_open_session(rbdc->client);
506 spin_lock(&rbd_client_list_lock);
507 list_add_tail(&rbdc->node, &rbd_client_list);
508 spin_unlock(&rbd_client_list_lock);
510 mutex_unlock(&ctl_mutex);
511 dout("%s: rbdc %p\n", __func__, rbdc);
516 ceph_destroy_client(rbdc->client);
518 mutex_unlock(&ctl_mutex);
522 ceph_destroy_options(ceph_opts);
523 dout("%s: error %d\n", __func__, ret);
528 static struct rbd_client *__rbd_get_client(struct rbd_client *rbdc)
530 kref_get(&rbdc->kref);
536 * Find a ceph client with specific addr and configuration. If
537 * found, bump its reference count.
539 static struct rbd_client *rbd_client_find(struct ceph_options *ceph_opts)
541 struct rbd_client *client_node;
544 if (ceph_opts->flags & CEPH_OPT_NOSHARE)
547 spin_lock(&rbd_client_list_lock);
548 list_for_each_entry(client_node, &rbd_client_list, node) {
549 if (!ceph_compare_options(ceph_opts, client_node->client)) {
550 __rbd_get_client(client_node);
556 spin_unlock(&rbd_client_list_lock);
558 return found ? client_node : NULL;
568 /* string args above */
571 /* Boolean args above */
575 static match_table_t rbd_opts_tokens = {
577 /* string args above */
578 {Opt_read_only, "read_only"},
579 {Opt_read_only, "ro"}, /* Alternate spelling */
580 {Opt_read_write, "read_write"},
581 {Opt_read_write, "rw"}, /* Alternate spelling */
582 /* Boolean args above */
590 #define RBD_READ_ONLY_DEFAULT false
592 static int parse_rbd_opts_token(char *c, void *private)
594 struct rbd_options *rbd_opts = private;
595 substring_t argstr[MAX_OPT_ARGS];
596 int token, intval, ret;
598 token = match_token(c, rbd_opts_tokens, argstr);
602 if (token < Opt_last_int) {
603 ret = match_int(&argstr[0], &intval);
605 pr_err("bad mount option arg (not int) "
609 dout("got int token %d val %d\n", token, intval);
610 } else if (token > Opt_last_int && token < Opt_last_string) {
611 dout("got string token %d val %s\n", token,
613 } else if (token > Opt_last_string && token < Opt_last_bool) {
614 dout("got Boolean token %d\n", token);
616 dout("got token %d\n", token);
621 rbd_opts->read_only = true;
624 rbd_opts->read_only = false;
634 * Get a ceph client with specific addr and configuration, if one does
635 * not exist create it.
637 static struct rbd_client *rbd_get_client(struct ceph_options *ceph_opts)
639 struct rbd_client *rbdc;
641 rbdc = rbd_client_find(ceph_opts);
642 if (rbdc) /* using an existing client */
643 ceph_destroy_options(ceph_opts);
645 rbdc = rbd_client_create(ceph_opts);
651 * Destroy ceph client
653 * Caller must hold rbd_client_list_lock.
655 static void rbd_client_release(struct kref *kref)
657 struct rbd_client *rbdc = container_of(kref, struct rbd_client, kref);
659 dout("%s: rbdc %p\n", __func__, rbdc);
660 spin_lock(&rbd_client_list_lock);
661 list_del(&rbdc->node);
662 spin_unlock(&rbd_client_list_lock);
664 ceph_destroy_client(rbdc->client);
669 * Drop reference to ceph client node. If it's not referenced anymore, release
672 static void rbd_put_client(struct rbd_client *rbdc)
675 kref_put(&rbdc->kref, rbd_client_release);
678 static bool rbd_image_format_valid(u32 image_format)
680 return image_format == 1 || image_format == 2;
683 static bool rbd_dev_ondisk_valid(struct rbd_image_header_ondisk *ondisk)
688 /* The header has to start with the magic rbd header text */
689 if (memcmp(&ondisk->text, RBD_HEADER_TEXT, sizeof (RBD_HEADER_TEXT)))
692 /* The bio layer requires at least sector-sized I/O */
694 if (ondisk->options.order < SECTOR_SHIFT)
697 /* If we use u64 in a few spots we may be able to loosen this */
699 if (ondisk->options.order > 8 * sizeof (int) - 1)
703 * The size of a snapshot header has to fit in a size_t, and
704 * that limits the number of snapshots.
706 snap_count = le32_to_cpu(ondisk->snap_count);
707 size = SIZE_MAX - sizeof (struct ceph_snap_context);
708 if (snap_count > size / sizeof (__le64))
712 * Not only that, but the size of the entire the snapshot
713 * header must also be representable in a size_t.
715 size -= snap_count * sizeof (__le64);
716 if ((u64) size < le64_to_cpu(ondisk->snap_names_len))
723 * Create a new header structure, translate header format from the on-disk
726 static int rbd_header_from_disk(struct rbd_image_header *header,
727 struct rbd_image_header_ondisk *ondisk)
734 memset(header, 0, sizeof (*header));
736 snap_count = le32_to_cpu(ondisk->snap_count);
738 len = strnlen(ondisk->object_prefix, sizeof (ondisk->object_prefix));
739 header->object_prefix = kmalloc(len + 1, GFP_KERNEL);
740 if (!header->object_prefix)
742 memcpy(header->object_prefix, ondisk->object_prefix, len);
743 header->object_prefix[len] = '\0';
746 u64 snap_names_len = le64_to_cpu(ondisk->snap_names_len);
748 /* Save a copy of the snapshot names */
750 if (snap_names_len > (u64) SIZE_MAX)
752 header->snap_names = kmalloc(snap_names_len, GFP_KERNEL);
753 if (!header->snap_names)
756 * Note that rbd_dev_v1_header_read() guarantees
757 * the ondisk buffer we're working with has
758 * snap_names_len bytes beyond the end of the
759 * snapshot id array, this memcpy() is safe.
761 memcpy(header->snap_names, &ondisk->snaps[snap_count],
764 /* Record each snapshot's size */
766 size = snap_count * sizeof (*header->snap_sizes);
767 header->snap_sizes = kmalloc(size, GFP_KERNEL);
768 if (!header->snap_sizes)
770 for (i = 0; i < snap_count; i++)
771 header->snap_sizes[i] =
772 le64_to_cpu(ondisk->snaps[i].image_size);
774 header->snap_names = NULL;
775 header->snap_sizes = NULL;
778 header->features = 0; /* No features support in v1 images */
779 header->obj_order = ondisk->options.order;
780 header->crypt_type = ondisk->options.crypt_type;
781 header->comp_type = ondisk->options.comp_type;
783 /* Allocate and fill in the snapshot context */
785 header->image_size = le64_to_cpu(ondisk->image_size);
787 header->snapc = ceph_create_snap_context(snap_count, GFP_KERNEL);
790 header->snapc->seq = le64_to_cpu(ondisk->snap_seq);
791 for (i = 0; i < snap_count; i++)
792 header->snapc->snaps[i] = le64_to_cpu(ondisk->snaps[i].id);
797 kfree(header->snap_sizes);
798 header->snap_sizes = NULL;
799 kfree(header->snap_names);
800 header->snap_names = NULL;
801 kfree(header->object_prefix);
802 header->object_prefix = NULL;
807 static const char *_rbd_dev_v1_snap_name(struct rbd_device *rbd_dev, u32 which)
809 const char *snap_name;
811 rbd_assert(which < rbd_dev->header.snapc->num_snaps);
813 /* Skip over names until we find the one we are looking for */
815 snap_name = rbd_dev->header.snap_names;
817 snap_name += strlen(snap_name) + 1;
819 return kstrdup(snap_name, GFP_KERNEL);
822 static u32 rbd_dev_snap_index(struct rbd_device *rbd_dev, u64 snap_id)
824 struct ceph_snap_context *snapc = rbd_dev->header.snapc;
827 for (which = 0; which < snapc->num_snaps; which++)
828 if (snapc->snaps[which] == snap_id)
831 return BAD_SNAP_INDEX;
834 static const char *rbd_dev_v1_snap_name(struct rbd_device *rbd_dev,
839 which = rbd_dev_snap_index(rbd_dev, snap_id);
840 if (which == BAD_SNAP_INDEX)
843 return _rbd_dev_v1_snap_name(rbd_dev, which);
846 static const char *rbd_snap_name(struct rbd_device *rbd_dev, u64 snap_id)
848 if (snap_id == CEPH_NOSNAP)
849 return RBD_SNAP_HEAD_NAME;
851 rbd_assert(rbd_image_format_valid(rbd_dev->image_format));
852 if (rbd_dev->image_format == 1)
853 return rbd_dev_v1_snap_name(rbd_dev, snap_id);
855 return rbd_dev_v2_snap_name(rbd_dev, snap_id);
858 static int rbd_snap_size(struct rbd_device *rbd_dev, u64 snap_id,
861 rbd_assert(rbd_image_format_valid(rbd_dev->image_format));
862 if (snap_id == CEPH_NOSNAP) {
863 *snap_size = rbd_dev->header.image_size;
864 } else if (rbd_dev->image_format == 1) {
867 which = rbd_dev_snap_index(rbd_dev, snap_id);
868 if (which == BAD_SNAP_INDEX)
871 *snap_size = rbd_dev->header.snap_sizes[which];
876 ret = _rbd_dev_v2_snap_size(rbd_dev, snap_id, NULL, &size);
885 static int rbd_snap_features(struct rbd_device *rbd_dev, u64 snap_id,
888 rbd_assert(rbd_image_format_valid(rbd_dev->image_format));
889 if (snap_id == CEPH_NOSNAP) {
890 *snap_features = rbd_dev->header.features;
891 } else if (rbd_dev->image_format == 1) {
892 *snap_features = 0; /* No features for format 1 */
897 ret = _rbd_dev_v2_snap_features(rbd_dev, snap_id, &features);
901 *snap_features = features;
906 static int rbd_dev_mapping_set(struct rbd_device *rbd_dev)
908 const char *snap_name = rbd_dev->spec->snap_name;
914 if (strcmp(snap_name, RBD_SNAP_HEAD_NAME)) {
915 snap_id = rbd_snap_id_by_name(rbd_dev, snap_name);
916 if (snap_id == CEPH_NOSNAP)
919 snap_id = CEPH_NOSNAP;
922 ret = rbd_snap_size(rbd_dev, snap_id, &size);
925 ret = rbd_snap_features(rbd_dev, snap_id, &features);
929 rbd_dev->mapping.size = size;
930 rbd_dev->mapping.features = features;
932 /* If we are mapping a snapshot it must be marked read-only */
934 if (snap_id != CEPH_NOSNAP)
935 rbd_dev->mapping.read_only = true;
940 static void rbd_dev_mapping_clear(struct rbd_device *rbd_dev)
942 rbd_dev->mapping.size = 0;
943 rbd_dev->mapping.features = 0;
944 rbd_dev->mapping.read_only = true;
947 static void rbd_dev_clear_mapping(struct rbd_device *rbd_dev)
949 rbd_dev->mapping.size = 0;
950 rbd_dev->mapping.features = 0;
951 rbd_dev->mapping.read_only = true;
954 static const char *rbd_segment_name(struct rbd_device *rbd_dev, u64 offset)
960 name = kmalloc(MAX_OBJ_NAME_SIZE + 1, GFP_NOIO);
963 segment = offset >> rbd_dev->header.obj_order;
964 ret = snprintf(name, MAX_OBJ_NAME_SIZE + 1, "%s.%012llx",
965 rbd_dev->header.object_prefix, segment);
966 if (ret < 0 || ret > MAX_OBJ_NAME_SIZE) {
967 pr_err("error formatting segment name for #%llu (%d)\n",
976 static u64 rbd_segment_offset(struct rbd_device *rbd_dev, u64 offset)
978 u64 segment_size = (u64) 1 << rbd_dev->header.obj_order;
980 return offset & (segment_size - 1);
983 static u64 rbd_segment_length(struct rbd_device *rbd_dev,
984 u64 offset, u64 length)
986 u64 segment_size = (u64) 1 << rbd_dev->header.obj_order;
988 offset &= segment_size - 1;
990 rbd_assert(length <= U64_MAX - offset);
991 if (offset + length > segment_size)
992 length = segment_size - offset;
998 * returns the size of an object in the image
1000 static u64 rbd_obj_bytes(struct rbd_image_header *header)
1002 return 1 << header->obj_order;
1009 static void bio_chain_put(struct bio *chain)
1015 chain = chain->bi_next;
1021 * zeros a bio chain, starting at specific offset
1023 static void zero_bio_chain(struct bio *chain, int start_ofs)
1026 unsigned long flags;
1032 bio_for_each_segment(bv, chain, i) {
1033 if (pos + bv->bv_len > start_ofs) {
1034 int remainder = max(start_ofs - pos, 0);
1035 buf = bvec_kmap_irq(bv, &flags);
1036 memset(buf + remainder, 0,
1037 bv->bv_len - remainder);
1038 bvec_kunmap_irq(buf, &flags);
1043 chain = chain->bi_next;
1048 * similar to zero_bio_chain(), zeros data defined by a page array,
1049 * starting at the given byte offset from the start of the array and
1050 * continuing up to the given end offset. The pages array is
1051 * assumed to be big enough to hold all bytes up to the end.
1053 static void zero_pages(struct page **pages, u64 offset, u64 end)
1055 struct page **page = &pages[offset >> PAGE_SHIFT];
1057 rbd_assert(end > offset);
1058 rbd_assert(end - offset <= (u64)SIZE_MAX);
1059 while (offset < end) {
1062 unsigned long flags;
1065 page_offset = (size_t)(offset & ~PAGE_MASK);
1066 length = min(PAGE_SIZE - page_offset, (size_t)(end - offset));
1067 local_irq_save(flags);
1068 kaddr = kmap_atomic(*page);
1069 memset(kaddr + page_offset, 0, length);
1070 kunmap_atomic(kaddr);
1071 local_irq_restore(flags);
1079 * Clone a portion of a bio, starting at the given byte offset
1080 * and continuing for the number of bytes indicated.
1082 static struct bio *bio_clone_range(struct bio *bio_src,
1083 unsigned int offset,
1091 unsigned short end_idx;
1092 unsigned short vcnt;
1095 /* Handle the easy case for the caller */
1097 if (!offset && len == bio_src->bi_size)
1098 return bio_clone(bio_src, gfpmask);
1100 if (WARN_ON_ONCE(!len))
1102 if (WARN_ON_ONCE(len > bio_src->bi_size))
1104 if (WARN_ON_ONCE(offset > bio_src->bi_size - len))
1107 /* Find first affected segment... */
1110 __bio_for_each_segment(bv, bio_src, idx, 0) {
1111 if (resid < bv->bv_len)
1113 resid -= bv->bv_len;
1117 /* ...and the last affected segment */
1120 __bio_for_each_segment(bv, bio_src, end_idx, idx) {
1121 if (resid <= bv->bv_len)
1123 resid -= bv->bv_len;
1125 vcnt = end_idx - idx + 1;
1127 /* Build the clone */
1129 bio = bio_alloc(gfpmask, (unsigned int) vcnt);
1131 return NULL; /* ENOMEM */
1133 bio->bi_bdev = bio_src->bi_bdev;
1134 bio->bi_sector = bio_src->bi_sector + (offset >> SECTOR_SHIFT);
1135 bio->bi_rw = bio_src->bi_rw;
1136 bio->bi_flags |= 1 << BIO_CLONED;
1139 * Copy over our part of the bio_vec, then update the first
1140 * and last (or only) entries.
1142 memcpy(&bio->bi_io_vec[0], &bio_src->bi_io_vec[idx],
1143 vcnt * sizeof (struct bio_vec));
1144 bio->bi_io_vec[0].bv_offset += voff;
1146 bio->bi_io_vec[0].bv_len -= voff;
1147 bio->bi_io_vec[vcnt - 1].bv_len = resid;
1149 bio->bi_io_vec[0].bv_len = len;
1152 bio->bi_vcnt = vcnt;
1160 * Clone a portion of a bio chain, starting at the given byte offset
1161 * into the first bio in the source chain and continuing for the
1162 * number of bytes indicated. The result is another bio chain of
1163 * exactly the given length, or a null pointer on error.
1165 * The bio_src and offset parameters are both in-out. On entry they
1166 * refer to the first source bio and the offset into that bio where
1167 * the start of data to be cloned is located.
1169 * On return, bio_src is updated to refer to the bio in the source
1170 * chain that contains first un-cloned byte, and *offset will
1171 * contain the offset of that byte within that bio.
1173 static struct bio *bio_chain_clone_range(struct bio **bio_src,
1174 unsigned int *offset,
1178 struct bio *bi = *bio_src;
1179 unsigned int off = *offset;
1180 struct bio *chain = NULL;
1183 /* Build up a chain of clone bios up to the limit */
1185 if (!bi || off >= bi->bi_size || !len)
1186 return NULL; /* Nothing to clone */
1190 unsigned int bi_size;
1194 rbd_warn(NULL, "bio_chain exhausted with %u left", len);
1195 goto out_err; /* EINVAL; ran out of bio's */
1197 bi_size = min_t(unsigned int, bi->bi_size - off, len);
1198 bio = bio_clone_range(bi, off, bi_size, gfpmask);
1200 goto out_err; /* ENOMEM */
1203 end = &bio->bi_next;
1206 if (off == bi->bi_size) {
1217 bio_chain_put(chain);
1223 * The default/initial value for all object request flags is 0. For
1224 * each flag, once its value is set to 1 it is never reset to 0
1227 static void obj_request_img_data_set(struct rbd_obj_request *obj_request)
1229 if (test_and_set_bit(OBJ_REQ_IMG_DATA, &obj_request->flags)) {
1230 struct rbd_device *rbd_dev;
1232 rbd_dev = obj_request->img_request->rbd_dev;
1233 rbd_warn(rbd_dev, "obj_request %p already marked img_data\n",
1238 static bool obj_request_img_data_test(struct rbd_obj_request *obj_request)
1241 return test_bit(OBJ_REQ_IMG_DATA, &obj_request->flags) != 0;
1244 static void obj_request_done_set(struct rbd_obj_request *obj_request)
1246 if (test_and_set_bit(OBJ_REQ_DONE, &obj_request->flags)) {
1247 struct rbd_device *rbd_dev = NULL;
1249 if (obj_request_img_data_test(obj_request))
1250 rbd_dev = obj_request->img_request->rbd_dev;
1251 rbd_warn(rbd_dev, "obj_request %p already marked done\n",
1256 static bool obj_request_done_test(struct rbd_obj_request *obj_request)
1259 return test_bit(OBJ_REQ_DONE, &obj_request->flags) != 0;
1263 * This sets the KNOWN flag after (possibly) setting the EXISTS
1264 * flag. The latter is set based on the "exists" value provided.
1266 * Note that for our purposes once an object exists it never goes
1267 * away again. It's possible that the response from two existence
1268 * checks are separated by the creation of the target object, and
1269 * the first ("doesn't exist") response arrives *after* the second
1270 * ("does exist"). In that case we ignore the second one.
1272 static void obj_request_existence_set(struct rbd_obj_request *obj_request,
1276 set_bit(OBJ_REQ_EXISTS, &obj_request->flags);
1277 set_bit(OBJ_REQ_KNOWN, &obj_request->flags);
1281 static bool obj_request_known_test(struct rbd_obj_request *obj_request)
1284 return test_bit(OBJ_REQ_KNOWN, &obj_request->flags) != 0;
1287 static bool obj_request_exists_test(struct rbd_obj_request *obj_request)
1290 return test_bit(OBJ_REQ_EXISTS, &obj_request->flags) != 0;
1293 static void rbd_obj_request_get(struct rbd_obj_request *obj_request)
1295 dout("%s: obj %p (was %d)\n", __func__, obj_request,
1296 atomic_read(&obj_request->kref.refcount));
1297 kref_get(&obj_request->kref);
1300 static void rbd_obj_request_destroy(struct kref *kref);
1301 static void rbd_obj_request_put(struct rbd_obj_request *obj_request)
1303 rbd_assert(obj_request != NULL);
1304 dout("%s: obj %p (was %d)\n", __func__, obj_request,
1305 atomic_read(&obj_request->kref.refcount));
1306 kref_put(&obj_request->kref, rbd_obj_request_destroy);
1309 static void rbd_img_request_get(struct rbd_img_request *img_request)
1311 dout("%s: img %p (was %d)\n", __func__, img_request,
1312 atomic_read(&img_request->kref.refcount));
1313 kref_get(&img_request->kref);
1316 static void rbd_img_request_destroy(struct kref *kref);
1317 static void rbd_img_request_put(struct rbd_img_request *img_request)
1319 rbd_assert(img_request != NULL);
1320 dout("%s: img %p (was %d)\n", __func__, img_request,
1321 atomic_read(&img_request->kref.refcount));
1322 kref_put(&img_request->kref, rbd_img_request_destroy);
1325 static inline void rbd_img_obj_request_add(struct rbd_img_request *img_request,
1326 struct rbd_obj_request *obj_request)
1328 rbd_assert(obj_request->img_request == NULL);
1330 /* Image request now owns object's original reference */
1331 obj_request->img_request = img_request;
1332 obj_request->which = img_request->obj_request_count;
1333 rbd_assert(!obj_request_img_data_test(obj_request));
1334 obj_request_img_data_set(obj_request);
1335 rbd_assert(obj_request->which != BAD_WHICH);
1336 img_request->obj_request_count++;
1337 list_add_tail(&obj_request->links, &img_request->obj_requests);
1338 dout("%s: img %p obj %p w=%u\n", __func__, img_request, obj_request,
1339 obj_request->which);
1342 static inline void rbd_img_obj_request_del(struct rbd_img_request *img_request,
1343 struct rbd_obj_request *obj_request)
1345 rbd_assert(obj_request->which != BAD_WHICH);
1347 dout("%s: img %p obj %p w=%u\n", __func__, img_request, obj_request,
1348 obj_request->which);
1349 list_del(&obj_request->links);
1350 rbd_assert(img_request->obj_request_count > 0);
1351 img_request->obj_request_count--;
1352 rbd_assert(obj_request->which == img_request->obj_request_count);
1353 obj_request->which = BAD_WHICH;
1354 rbd_assert(obj_request_img_data_test(obj_request));
1355 rbd_assert(obj_request->img_request == img_request);
1356 obj_request->img_request = NULL;
1357 obj_request->callback = NULL;
1358 rbd_obj_request_put(obj_request);
1361 static bool obj_request_type_valid(enum obj_request_type type)
1364 case OBJ_REQUEST_NODATA:
1365 case OBJ_REQUEST_BIO:
1366 case OBJ_REQUEST_PAGES:
1373 static int rbd_obj_request_submit(struct ceph_osd_client *osdc,
1374 struct rbd_obj_request *obj_request)
1376 dout("%s: osdc %p obj %p\n", __func__, osdc, obj_request);
1378 return ceph_osdc_start_request(osdc, obj_request->osd_req, false);
1381 static void rbd_img_request_complete(struct rbd_img_request *img_request)
1384 dout("%s: img %p\n", __func__, img_request);
1387 * If no error occurred, compute the aggregate transfer
1388 * count for the image request. We could instead use
1389 * atomic64_cmpxchg() to update it as each object request
1390 * completes; not clear which way is better off hand.
1392 if (!img_request->result) {
1393 struct rbd_obj_request *obj_request;
1396 for_each_obj_request(img_request, obj_request)
1397 xferred += obj_request->xferred;
1398 img_request->xferred = xferred;
1401 if (img_request->callback)
1402 img_request->callback(img_request);
1404 rbd_img_request_put(img_request);
1407 /* Caller is responsible for rbd_obj_request_destroy(obj_request) */
1409 static int rbd_obj_request_wait(struct rbd_obj_request *obj_request)
1411 dout("%s: obj %p\n", __func__, obj_request);
1413 return wait_for_completion_interruptible(&obj_request->completion);
1417 * The default/initial value for all image request flags is 0. Each
1418 * is conditionally set to 1 at image request initialization time
1419 * and currently never change thereafter.
1421 static void img_request_write_set(struct rbd_img_request *img_request)
1423 set_bit(IMG_REQ_WRITE, &img_request->flags);
1427 static bool img_request_write_test(struct rbd_img_request *img_request)
1430 return test_bit(IMG_REQ_WRITE, &img_request->flags) != 0;
1433 static void img_request_child_set(struct rbd_img_request *img_request)
1435 set_bit(IMG_REQ_CHILD, &img_request->flags);
1439 static bool img_request_child_test(struct rbd_img_request *img_request)
1442 return test_bit(IMG_REQ_CHILD, &img_request->flags) != 0;
1445 static void img_request_layered_set(struct rbd_img_request *img_request)
1447 set_bit(IMG_REQ_LAYERED, &img_request->flags);
1451 static bool img_request_layered_test(struct rbd_img_request *img_request)
1454 return test_bit(IMG_REQ_LAYERED, &img_request->flags) != 0;
1458 rbd_img_obj_request_read_callback(struct rbd_obj_request *obj_request)
1460 u64 xferred = obj_request->xferred;
1461 u64 length = obj_request->length;
1463 dout("%s: obj %p img %p result %d %llu/%llu\n", __func__,
1464 obj_request, obj_request->img_request, obj_request->result,
1467 * ENOENT means a hole in the image. We zero-fill the
1468 * entire length of the request. A short read also implies
1469 * zero-fill to the end of the request. Either way we
1470 * update the xferred count to indicate the whole request
1473 rbd_assert(obj_request->type != OBJ_REQUEST_NODATA);
1474 if (obj_request->result == -ENOENT) {
1475 if (obj_request->type == OBJ_REQUEST_BIO)
1476 zero_bio_chain(obj_request->bio_list, 0);
1478 zero_pages(obj_request->pages, 0, length);
1479 obj_request->result = 0;
1480 obj_request->xferred = length;
1481 } else if (xferred < length && !obj_request->result) {
1482 if (obj_request->type == OBJ_REQUEST_BIO)
1483 zero_bio_chain(obj_request->bio_list, xferred);
1485 zero_pages(obj_request->pages, xferred, length);
1486 obj_request->xferred = length;
1488 obj_request_done_set(obj_request);
1491 static void rbd_obj_request_complete(struct rbd_obj_request *obj_request)
1493 dout("%s: obj %p cb %p\n", __func__, obj_request,
1494 obj_request->callback);
1495 if (obj_request->callback)
1496 obj_request->callback(obj_request);
1498 complete_all(&obj_request->completion);
1501 static void rbd_osd_trivial_callback(struct rbd_obj_request *obj_request)
1503 dout("%s: obj %p\n", __func__, obj_request);
1504 obj_request_done_set(obj_request);
1507 static void rbd_osd_read_callback(struct rbd_obj_request *obj_request)
1509 struct rbd_img_request *img_request = NULL;
1510 struct rbd_device *rbd_dev = NULL;
1511 bool layered = false;
1513 if (obj_request_img_data_test(obj_request)) {
1514 img_request = obj_request->img_request;
1515 layered = img_request && img_request_layered_test(img_request);
1516 rbd_dev = img_request->rbd_dev;
1519 dout("%s: obj %p img %p result %d %llu/%llu\n", __func__,
1520 obj_request, img_request, obj_request->result,
1521 obj_request->xferred, obj_request->length);
1522 if (layered && obj_request->result == -ENOENT &&
1523 obj_request->img_offset < rbd_dev->parent_overlap)
1524 rbd_img_parent_read(obj_request);
1525 else if (img_request)
1526 rbd_img_obj_request_read_callback(obj_request);
1528 obj_request_done_set(obj_request);
1531 static void rbd_osd_write_callback(struct rbd_obj_request *obj_request)
1533 dout("%s: obj %p result %d %llu\n", __func__, obj_request,
1534 obj_request->result, obj_request->length);
1536 * There is no such thing as a successful short write. Set
1537 * it to our originally-requested length.
1539 obj_request->xferred = obj_request->length;
1540 obj_request_done_set(obj_request);
1544 * For a simple stat call there's nothing to do. We'll do more if
1545 * this is part of a write sequence for a layered image.
1547 static void rbd_osd_stat_callback(struct rbd_obj_request *obj_request)
1549 dout("%s: obj %p\n", __func__, obj_request);
1550 obj_request_done_set(obj_request);
1553 static void rbd_osd_req_callback(struct ceph_osd_request *osd_req,
1554 struct ceph_msg *msg)
1556 struct rbd_obj_request *obj_request = osd_req->r_priv;
1559 dout("%s: osd_req %p msg %p\n", __func__, osd_req, msg);
1560 rbd_assert(osd_req == obj_request->osd_req);
1561 if (obj_request_img_data_test(obj_request)) {
1562 rbd_assert(obj_request->img_request);
1563 rbd_assert(obj_request->which != BAD_WHICH);
1565 rbd_assert(obj_request->which == BAD_WHICH);
1568 if (osd_req->r_result < 0)
1569 obj_request->result = osd_req->r_result;
1571 BUG_ON(osd_req->r_num_ops > 2);
1574 * We support a 64-bit length, but ultimately it has to be
1575 * passed to blk_end_request(), which takes an unsigned int.
1577 obj_request->xferred = osd_req->r_reply_op_len[0];
1578 rbd_assert(obj_request->xferred < (u64)UINT_MAX);
1579 opcode = osd_req->r_ops[0].op;
1581 case CEPH_OSD_OP_READ:
1582 rbd_osd_read_callback(obj_request);
1584 case CEPH_OSD_OP_WRITE:
1585 rbd_osd_write_callback(obj_request);
1587 case CEPH_OSD_OP_STAT:
1588 rbd_osd_stat_callback(obj_request);
1590 case CEPH_OSD_OP_CALL:
1591 case CEPH_OSD_OP_NOTIFY_ACK:
1592 case CEPH_OSD_OP_WATCH:
1593 rbd_osd_trivial_callback(obj_request);
1596 rbd_warn(NULL, "%s: unsupported op %hu\n",
1597 obj_request->object_name, (unsigned short) opcode);
1601 if (obj_request_done_test(obj_request))
1602 rbd_obj_request_complete(obj_request);
1605 static void rbd_osd_req_format_read(struct rbd_obj_request *obj_request)
1607 struct rbd_img_request *img_request = obj_request->img_request;
1608 struct ceph_osd_request *osd_req = obj_request->osd_req;
1611 rbd_assert(osd_req != NULL);
1613 snap_id = img_request ? img_request->snap_id : CEPH_NOSNAP;
1614 ceph_osdc_build_request(osd_req, obj_request->offset,
1615 NULL, snap_id, NULL);
1618 static void rbd_osd_req_format_write(struct rbd_obj_request *obj_request)
1620 struct rbd_img_request *img_request = obj_request->img_request;
1621 struct ceph_osd_request *osd_req = obj_request->osd_req;
1622 struct ceph_snap_context *snapc;
1623 struct timespec mtime = CURRENT_TIME;
1625 rbd_assert(osd_req != NULL);
1627 snapc = img_request ? img_request->snapc : NULL;
1628 ceph_osdc_build_request(osd_req, obj_request->offset,
1629 snapc, CEPH_NOSNAP, &mtime);
1632 static struct ceph_osd_request *rbd_osd_req_create(
1633 struct rbd_device *rbd_dev,
1635 struct rbd_obj_request *obj_request)
1637 struct ceph_snap_context *snapc = NULL;
1638 struct ceph_osd_client *osdc;
1639 struct ceph_osd_request *osd_req;
1641 if (obj_request_img_data_test(obj_request)) {
1642 struct rbd_img_request *img_request = obj_request->img_request;
1644 rbd_assert(write_request ==
1645 img_request_write_test(img_request));
1647 snapc = img_request->snapc;
1650 /* Allocate and initialize the request, for the single op */
1652 osdc = &rbd_dev->rbd_client->client->osdc;
1653 osd_req = ceph_osdc_alloc_request(osdc, snapc, 1, false, GFP_ATOMIC);
1655 return NULL; /* ENOMEM */
1658 osd_req->r_flags = CEPH_OSD_FLAG_WRITE | CEPH_OSD_FLAG_ONDISK;
1660 osd_req->r_flags = CEPH_OSD_FLAG_READ;
1662 osd_req->r_callback = rbd_osd_req_callback;
1663 osd_req->r_priv = obj_request;
1665 osd_req->r_oid_len = strlen(obj_request->object_name);
1666 rbd_assert(osd_req->r_oid_len < sizeof (osd_req->r_oid));
1667 memcpy(osd_req->r_oid, obj_request->object_name, osd_req->r_oid_len);
1669 osd_req->r_file_layout = rbd_dev->layout; /* struct */
1675 * Create a copyup osd request based on the information in the
1676 * object request supplied. A copyup request has two osd ops,
1677 * a copyup method call, and a "normal" write request.
1679 static struct ceph_osd_request *
1680 rbd_osd_req_create_copyup(struct rbd_obj_request *obj_request)
1682 struct rbd_img_request *img_request;
1683 struct ceph_snap_context *snapc;
1684 struct rbd_device *rbd_dev;
1685 struct ceph_osd_client *osdc;
1686 struct ceph_osd_request *osd_req;
1688 rbd_assert(obj_request_img_data_test(obj_request));
1689 img_request = obj_request->img_request;
1690 rbd_assert(img_request);
1691 rbd_assert(img_request_write_test(img_request));
1693 /* Allocate and initialize the request, for the two ops */
1695 snapc = img_request->snapc;
1696 rbd_dev = img_request->rbd_dev;
1697 osdc = &rbd_dev->rbd_client->client->osdc;
1698 osd_req = ceph_osdc_alloc_request(osdc, snapc, 2, false, GFP_ATOMIC);
1700 return NULL; /* ENOMEM */
1702 osd_req->r_flags = CEPH_OSD_FLAG_WRITE | CEPH_OSD_FLAG_ONDISK;
1703 osd_req->r_callback = rbd_osd_req_callback;
1704 osd_req->r_priv = obj_request;
1706 osd_req->r_oid_len = strlen(obj_request->object_name);
1707 rbd_assert(osd_req->r_oid_len < sizeof (osd_req->r_oid));
1708 memcpy(osd_req->r_oid, obj_request->object_name, osd_req->r_oid_len);
1710 osd_req->r_file_layout = rbd_dev->layout; /* struct */
1716 static void rbd_osd_req_destroy(struct ceph_osd_request *osd_req)
1718 ceph_osdc_put_request(osd_req);
1721 /* object_name is assumed to be a non-null pointer and NUL-terminated */
1723 static struct rbd_obj_request *rbd_obj_request_create(const char *object_name,
1724 u64 offset, u64 length,
1725 enum obj_request_type type)
1727 struct rbd_obj_request *obj_request;
1731 rbd_assert(obj_request_type_valid(type));
1733 size = strlen(object_name) + 1;
1734 obj_request = kzalloc(sizeof (*obj_request) + size, GFP_KERNEL);
1738 name = (char *)(obj_request + 1);
1739 obj_request->object_name = memcpy(name, object_name, size);
1740 obj_request->offset = offset;
1741 obj_request->length = length;
1742 obj_request->flags = 0;
1743 obj_request->which = BAD_WHICH;
1744 obj_request->type = type;
1745 INIT_LIST_HEAD(&obj_request->links);
1746 init_completion(&obj_request->completion);
1747 kref_init(&obj_request->kref);
1749 dout("%s: \"%s\" %llu/%llu %d -> obj %p\n", __func__, object_name,
1750 offset, length, (int)type, obj_request);
1755 static void rbd_obj_request_destroy(struct kref *kref)
1757 struct rbd_obj_request *obj_request;
1759 obj_request = container_of(kref, struct rbd_obj_request, kref);
1761 dout("%s: obj %p\n", __func__, obj_request);
1763 rbd_assert(obj_request->img_request == NULL);
1764 rbd_assert(obj_request->which == BAD_WHICH);
1766 if (obj_request->osd_req)
1767 rbd_osd_req_destroy(obj_request->osd_req);
1769 rbd_assert(obj_request_type_valid(obj_request->type));
1770 switch (obj_request->type) {
1771 case OBJ_REQUEST_NODATA:
1772 break; /* Nothing to do */
1773 case OBJ_REQUEST_BIO:
1774 if (obj_request->bio_list)
1775 bio_chain_put(obj_request->bio_list);
1777 case OBJ_REQUEST_PAGES:
1778 if (obj_request->pages)
1779 ceph_release_page_vector(obj_request->pages,
1780 obj_request->page_count);
1788 * Caller is responsible for filling in the list of object requests
1789 * that comprises the image request, and the Linux request pointer
1790 * (if there is one).
1792 static struct rbd_img_request *rbd_img_request_create(
1793 struct rbd_device *rbd_dev,
1794 u64 offset, u64 length,
1798 struct rbd_img_request *img_request;
1800 img_request = kmalloc(sizeof (*img_request), GFP_ATOMIC);
1804 if (write_request) {
1805 down_read(&rbd_dev->header_rwsem);
1806 ceph_get_snap_context(rbd_dev->header.snapc);
1807 up_read(&rbd_dev->header_rwsem);
1810 img_request->rq = NULL;
1811 img_request->rbd_dev = rbd_dev;
1812 img_request->offset = offset;
1813 img_request->length = length;
1814 img_request->flags = 0;
1815 if (write_request) {
1816 img_request_write_set(img_request);
1817 img_request->snapc = rbd_dev->header.snapc;
1819 img_request->snap_id = rbd_dev->spec->snap_id;
1822 img_request_child_set(img_request);
1823 if (rbd_dev->parent_spec)
1824 img_request_layered_set(img_request);
1825 spin_lock_init(&img_request->completion_lock);
1826 img_request->next_completion = 0;
1827 img_request->callback = NULL;
1828 img_request->result = 0;
1829 img_request->obj_request_count = 0;
1830 INIT_LIST_HEAD(&img_request->obj_requests);
1831 kref_init(&img_request->kref);
1833 rbd_img_request_get(img_request); /* Avoid a warning */
1834 rbd_img_request_put(img_request); /* TEMPORARY */
1836 dout("%s: rbd_dev %p %s %llu/%llu -> img %p\n", __func__, rbd_dev,
1837 write_request ? "write" : "read", offset, length,
1843 static void rbd_img_request_destroy(struct kref *kref)
1845 struct rbd_img_request *img_request;
1846 struct rbd_obj_request *obj_request;
1847 struct rbd_obj_request *next_obj_request;
1849 img_request = container_of(kref, struct rbd_img_request, kref);
1851 dout("%s: img %p\n", __func__, img_request);
1853 for_each_obj_request_safe(img_request, obj_request, next_obj_request)
1854 rbd_img_obj_request_del(img_request, obj_request);
1855 rbd_assert(img_request->obj_request_count == 0);
1857 if (img_request_write_test(img_request))
1858 ceph_put_snap_context(img_request->snapc);
1860 if (img_request_child_test(img_request))
1861 rbd_obj_request_put(img_request->obj_request);
1866 static bool rbd_img_obj_end_request(struct rbd_obj_request *obj_request)
1868 struct rbd_img_request *img_request;
1869 unsigned int xferred;
1873 rbd_assert(obj_request_img_data_test(obj_request));
1874 img_request = obj_request->img_request;
1876 rbd_assert(obj_request->xferred <= (u64)UINT_MAX);
1877 xferred = (unsigned int)obj_request->xferred;
1878 result = obj_request->result;
1880 struct rbd_device *rbd_dev = img_request->rbd_dev;
1882 rbd_warn(rbd_dev, "%s %llx at %llx (%llx)\n",
1883 img_request_write_test(img_request) ? "write" : "read",
1884 obj_request->length, obj_request->img_offset,
1885 obj_request->offset);
1886 rbd_warn(rbd_dev, " result %d xferred %x\n",
1888 if (!img_request->result)
1889 img_request->result = result;
1892 /* Image object requests don't own their page array */
1894 if (obj_request->type == OBJ_REQUEST_PAGES) {
1895 obj_request->pages = NULL;
1896 obj_request->page_count = 0;
1899 if (img_request_child_test(img_request)) {
1900 rbd_assert(img_request->obj_request != NULL);
1901 more = obj_request->which < img_request->obj_request_count - 1;
1903 rbd_assert(img_request->rq != NULL);
1904 more = blk_end_request(img_request->rq, result, xferred);
1910 static void rbd_img_obj_callback(struct rbd_obj_request *obj_request)
1912 struct rbd_img_request *img_request;
1913 u32 which = obj_request->which;
1916 rbd_assert(obj_request_img_data_test(obj_request));
1917 img_request = obj_request->img_request;
1919 dout("%s: img %p obj %p\n", __func__, img_request, obj_request);
1920 rbd_assert(img_request != NULL);
1921 rbd_assert(img_request->obj_request_count > 0);
1922 rbd_assert(which != BAD_WHICH);
1923 rbd_assert(which < img_request->obj_request_count);
1924 rbd_assert(which >= img_request->next_completion);
1926 spin_lock_irq(&img_request->completion_lock);
1927 if (which != img_request->next_completion)
1930 for_each_obj_request_from(img_request, obj_request) {
1932 rbd_assert(which < img_request->obj_request_count);
1934 if (!obj_request_done_test(obj_request))
1936 more = rbd_img_obj_end_request(obj_request);
1940 rbd_assert(more ^ (which == img_request->obj_request_count));
1941 img_request->next_completion = which;
1943 spin_unlock_irq(&img_request->completion_lock);
1946 rbd_img_request_complete(img_request);
1950 * Split up an image request into one or more object requests, each
1951 * to a different object. The "type" parameter indicates whether
1952 * "data_desc" is the pointer to the head of a list of bio
1953 * structures, or the base of a page array. In either case this
1954 * function assumes data_desc describes memory sufficient to hold
1955 * all data described by the image request.
1957 static int rbd_img_request_fill(struct rbd_img_request *img_request,
1958 enum obj_request_type type,
1961 struct rbd_device *rbd_dev = img_request->rbd_dev;
1962 struct rbd_obj_request *obj_request = NULL;
1963 struct rbd_obj_request *next_obj_request;
1964 bool write_request = img_request_write_test(img_request);
1965 struct bio *bio_list;
1966 unsigned int bio_offset = 0;
1967 struct page **pages;
1972 dout("%s: img %p type %d data_desc %p\n", __func__, img_request,
1973 (int)type, data_desc);
1975 opcode = write_request ? CEPH_OSD_OP_WRITE : CEPH_OSD_OP_READ;
1976 img_offset = img_request->offset;
1977 resid = img_request->length;
1978 rbd_assert(resid > 0);
1980 if (type == OBJ_REQUEST_BIO) {
1981 bio_list = data_desc;
1982 rbd_assert(img_offset == bio_list->bi_sector << SECTOR_SHIFT);
1984 rbd_assert(type == OBJ_REQUEST_PAGES);
1989 struct ceph_osd_request *osd_req;
1990 const char *object_name;
1994 object_name = rbd_segment_name(rbd_dev, img_offset);
1997 offset = rbd_segment_offset(rbd_dev, img_offset);
1998 length = rbd_segment_length(rbd_dev, img_offset, resid);
1999 obj_request = rbd_obj_request_create(object_name,
2000 offset, length, type);
2001 kfree(object_name); /* object request has its own copy */
2005 if (type == OBJ_REQUEST_BIO) {
2006 unsigned int clone_size;
2008 rbd_assert(length <= (u64)UINT_MAX);
2009 clone_size = (unsigned int)length;
2010 obj_request->bio_list =
2011 bio_chain_clone_range(&bio_list,
2015 if (!obj_request->bio_list)
2018 unsigned int page_count;
2020 obj_request->pages = pages;
2021 page_count = (u32)calc_pages_for(offset, length);
2022 obj_request->page_count = page_count;
2023 if ((offset + length) & ~PAGE_MASK)
2024 page_count--; /* more on last page */
2025 pages += page_count;
2028 osd_req = rbd_osd_req_create(rbd_dev, write_request,
2032 obj_request->osd_req = osd_req;
2033 obj_request->callback = rbd_img_obj_callback;
2035 osd_req_op_extent_init(osd_req, 0, opcode, offset, length,
2037 if (type == OBJ_REQUEST_BIO)
2038 osd_req_op_extent_osd_data_bio(osd_req, 0,
2039 obj_request->bio_list, length);
2041 osd_req_op_extent_osd_data_pages(osd_req, 0,
2042 obj_request->pages, length,
2043 offset & ~PAGE_MASK, false, false);
2046 rbd_osd_req_format_write(obj_request);
2048 rbd_osd_req_format_read(obj_request);
2050 obj_request->img_offset = img_offset;
2051 rbd_img_obj_request_add(img_request, obj_request);
2053 img_offset += length;
2060 rbd_obj_request_put(obj_request);
2062 for_each_obj_request_safe(img_request, obj_request, next_obj_request)
2063 rbd_obj_request_put(obj_request);
2069 rbd_img_obj_copyup_callback(struct rbd_obj_request *obj_request)
2071 struct rbd_img_request *img_request;
2072 struct rbd_device *rbd_dev;
2076 rbd_assert(obj_request->type == OBJ_REQUEST_BIO);
2077 rbd_assert(obj_request_img_data_test(obj_request));
2078 img_request = obj_request->img_request;
2079 rbd_assert(img_request);
2081 rbd_dev = img_request->rbd_dev;
2082 rbd_assert(rbd_dev);
2083 length = (u64)1 << rbd_dev->header.obj_order;
2084 page_count = (u32)calc_pages_for(0, length);
2086 rbd_assert(obj_request->copyup_pages);
2087 ceph_release_page_vector(obj_request->copyup_pages, page_count);
2088 obj_request->copyup_pages = NULL;
2091 * We want the transfer count to reflect the size of the
2092 * original write request. There is no such thing as a
2093 * successful short write, so if the request was successful
2094 * we can just set it to the originally-requested length.
2096 if (!obj_request->result)
2097 obj_request->xferred = obj_request->length;
2099 /* Finish up with the normal image object callback */
2101 rbd_img_obj_callback(obj_request);
2105 rbd_img_obj_parent_read_full_callback(struct rbd_img_request *img_request)
2107 struct rbd_obj_request *orig_request;
2108 struct ceph_osd_request *osd_req;
2109 struct ceph_osd_client *osdc;
2110 struct rbd_device *rbd_dev;
2111 struct page **pages;
2116 rbd_assert(img_request_child_test(img_request));
2118 /* First get what we need from the image request */
2120 pages = img_request->copyup_pages;
2121 rbd_assert(pages != NULL);
2122 img_request->copyup_pages = NULL;
2124 orig_request = img_request->obj_request;
2125 rbd_assert(orig_request != NULL);
2126 rbd_assert(orig_request->type == OBJ_REQUEST_BIO);
2127 result = img_request->result;
2128 obj_size = img_request->length;
2129 xferred = img_request->xferred;
2131 rbd_dev = img_request->rbd_dev;
2132 rbd_assert(rbd_dev);
2133 rbd_assert(obj_size == (u64)1 << rbd_dev->header.obj_order);
2135 rbd_img_request_put(img_request);
2140 /* Allocate the new copyup osd request for the original request */
2143 rbd_assert(!orig_request->osd_req);
2144 osd_req = rbd_osd_req_create_copyup(orig_request);
2147 orig_request->osd_req = osd_req;
2148 orig_request->copyup_pages = pages;
2150 /* Initialize the copyup op */
2152 osd_req_op_cls_init(osd_req, 0, CEPH_OSD_OP_CALL, "rbd", "copyup");
2153 osd_req_op_cls_request_data_pages(osd_req, 0, pages, obj_size, 0,
2156 /* Then the original write request op */
2158 osd_req_op_extent_init(osd_req, 1, CEPH_OSD_OP_WRITE,
2159 orig_request->offset,
2160 orig_request->length, 0, 0);
2161 osd_req_op_extent_osd_data_bio(osd_req, 1, orig_request->bio_list,
2162 orig_request->length);
2164 rbd_osd_req_format_write(orig_request);
2166 /* All set, send it off. */
2168 orig_request->callback = rbd_img_obj_copyup_callback;
2169 osdc = &rbd_dev->rbd_client->client->osdc;
2170 result = rbd_obj_request_submit(osdc, orig_request);
2174 /* Record the error code and complete the request */
2176 orig_request->result = result;
2177 orig_request->xferred = 0;
2178 obj_request_done_set(orig_request);
2179 rbd_obj_request_complete(orig_request);
2183 * Read from the parent image the range of data that covers the
2184 * entire target of the given object request. This is used for
2185 * satisfying a layered image write request when the target of an
2186 * object request from the image request does not exist.
2188 * A page array big enough to hold the returned data is allocated
2189 * and supplied to rbd_img_request_fill() as the "data descriptor."
2190 * When the read completes, this page array will be transferred to
2191 * the original object request for the copyup operation.
2193 * If an error occurs, record it as the result of the original
2194 * object request and mark it done so it gets completed.
2196 static int rbd_img_obj_parent_read_full(struct rbd_obj_request *obj_request)
2198 struct rbd_img_request *img_request = NULL;
2199 struct rbd_img_request *parent_request = NULL;
2200 struct rbd_device *rbd_dev;
2203 struct page **pages = NULL;
2207 rbd_assert(obj_request_img_data_test(obj_request));
2208 rbd_assert(obj_request->type == OBJ_REQUEST_BIO);
2210 img_request = obj_request->img_request;
2211 rbd_assert(img_request != NULL);
2212 rbd_dev = img_request->rbd_dev;
2213 rbd_assert(rbd_dev->parent != NULL);
2216 * First things first. The original osd request is of no
2217 * use to use any more, we'll need a new one that can hold
2218 * the two ops in a copyup request. We'll get that later,
2219 * but for now we can release the old one.
2221 rbd_osd_req_destroy(obj_request->osd_req);
2222 obj_request->osd_req = NULL;
2225 * Determine the byte range covered by the object in the
2226 * child image to which the original request was to be sent.
2228 img_offset = obj_request->img_offset - obj_request->offset;
2229 length = (u64)1 << rbd_dev->header.obj_order;
2232 * There is no defined parent data beyond the parent
2233 * overlap, so limit what we read at that boundary if
2236 if (img_offset + length > rbd_dev->parent_overlap) {
2237 rbd_assert(img_offset < rbd_dev->parent_overlap);
2238 length = rbd_dev->parent_overlap - img_offset;
2242 * Allocate a page array big enough to receive the data read
2245 page_count = (u32)calc_pages_for(0, length);
2246 pages = ceph_alloc_page_vector(page_count, GFP_KERNEL);
2247 if (IS_ERR(pages)) {
2248 result = PTR_ERR(pages);
2254 parent_request = rbd_img_request_create(rbd_dev->parent,
2257 if (!parent_request)
2259 rbd_obj_request_get(obj_request);
2260 parent_request->obj_request = obj_request;
2262 result = rbd_img_request_fill(parent_request, OBJ_REQUEST_PAGES, pages);
2265 parent_request->copyup_pages = pages;
2267 parent_request->callback = rbd_img_obj_parent_read_full_callback;
2268 result = rbd_img_request_submit(parent_request);
2272 parent_request->copyup_pages = NULL;
2273 parent_request->obj_request = NULL;
2274 rbd_obj_request_put(obj_request);
2277 ceph_release_page_vector(pages, page_count);
2279 rbd_img_request_put(parent_request);
2280 obj_request->result = result;
2281 obj_request->xferred = 0;
2282 obj_request_done_set(obj_request);
2287 static void rbd_img_obj_exists_callback(struct rbd_obj_request *obj_request)
2289 struct rbd_obj_request *orig_request;
2292 rbd_assert(!obj_request_img_data_test(obj_request));
2295 * All we need from the object request is the original
2296 * request and the result of the STAT op. Grab those, then
2297 * we're done with the request.
2299 orig_request = obj_request->obj_request;
2300 obj_request->obj_request = NULL;
2301 rbd_assert(orig_request);
2302 rbd_assert(orig_request->img_request);
2304 result = obj_request->result;
2305 obj_request->result = 0;
2307 dout("%s: obj %p for obj %p result %d %llu/%llu\n", __func__,
2308 obj_request, orig_request, result,
2309 obj_request->xferred, obj_request->length);
2310 rbd_obj_request_put(obj_request);
2312 rbd_assert(orig_request);
2313 rbd_assert(orig_request->img_request);
2316 * Our only purpose here is to determine whether the object
2317 * exists, and we don't want to treat the non-existence as
2318 * an error. If something else comes back, transfer the
2319 * error to the original request and complete it now.
2322 obj_request_existence_set(orig_request, true);
2323 } else if (result == -ENOENT) {
2324 obj_request_existence_set(orig_request, false);
2325 } else if (result) {
2326 orig_request->result = result;
2331 * Resubmit the original request now that we have recorded
2332 * whether the target object exists.
2334 orig_request->result = rbd_img_obj_request_submit(orig_request);
2336 if (orig_request->result)
2337 rbd_obj_request_complete(orig_request);
2338 rbd_obj_request_put(orig_request);
2341 static int rbd_img_obj_exists_submit(struct rbd_obj_request *obj_request)
2343 struct rbd_obj_request *stat_request;
2344 struct rbd_device *rbd_dev;
2345 struct ceph_osd_client *osdc;
2346 struct page **pages = NULL;
2352 * The response data for a STAT call consists of:
2359 size = sizeof (__le64) + sizeof (__le32) + sizeof (__le32);
2360 page_count = (u32)calc_pages_for(0, size);
2361 pages = ceph_alloc_page_vector(page_count, GFP_KERNEL);
2363 return PTR_ERR(pages);
2366 stat_request = rbd_obj_request_create(obj_request->object_name, 0, 0,
2371 rbd_obj_request_get(obj_request);
2372 stat_request->obj_request = obj_request;
2373 stat_request->pages = pages;
2374 stat_request->page_count = page_count;
2376 rbd_assert(obj_request->img_request);
2377 rbd_dev = obj_request->img_request->rbd_dev;
2378 stat_request->osd_req = rbd_osd_req_create(rbd_dev, false,
2380 if (!stat_request->osd_req)
2382 stat_request->callback = rbd_img_obj_exists_callback;
2384 osd_req_op_init(stat_request->osd_req, 0, CEPH_OSD_OP_STAT);
2385 osd_req_op_raw_data_in_pages(stat_request->osd_req, 0, pages, size, 0,
2387 rbd_osd_req_format_read(stat_request);
2389 osdc = &rbd_dev->rbd_client->client->osdc;
2390 ret = rbd_obj_request_submit(osdc, stat_request);
2393 rbd_obj_request_put(obj_request);
2398 static int rbd_img_obj_request_submit(struct rbd_obj_request *obj_request)
2400 struct rbd_img_request *img_request;
2401 struct rbd_device *rbd_dev;
2404 rbd_assert(obj_request_img_data_test(obj_request));
2406 img_request = obj_request->img_request;
2407 rbd_assert(img_request);
2408 rbd_dev = img_request->rbd_dev;
2411 * Only writes to layered images need special handling.
2412 * Reads and non-layered writes are simple object requests.
2413 * Layered writes that start beyond the end of the overlap
2414 * with the parent have no parent data, so they too are
2415 * simple object requests. Finally, if the target object is
2416 * known to already exist, its parent data has already been
2417 * copied, so a write to the object can also be handled as a
2418 * simple object request.
2420 if (!img_request_write_test(img_request) ||
2421 !img_request_layered_test(img_request) ||
2422 rbd_dev->parent_overlap <= obj_request->img_offset ||
2423 ((known = obj_request_known_test(obj_request)) &&
2424 obj_request_exists_test(obj_request))) {
2426 struct rbd_device *rbd_dev;
2427 struct ceph_osd_client *osdc;
2429 rbd_dev = obj_request->img_request->rbd_dev;
2430 osdc = &rbd_dev->rbd_client->client->osdc;
2432 return rbd_obj_request_submit(osdc, obj_request);
2436 * It's a layered write. The target object might exist but
2437 * we may not know that yet. If we know it doesn't exist,
2438 * start by reading the data for the full target object from
2439 * the parent so we can use it for a copyup to the target.
2442 return rbd_img_obj_parent_read_full(obj_request);
2444 /* We don't know whether the target exists. Go find out. */
2446 return rbd_img_obj_exists_submit(obj_request);
2449 static int rbd_img_request_submit(struct rbd_img_request *img_request)
2451 struct rbd_obj_request *obj_request;
2452 struct rbd_obj_request *next_obj_request;
2454 dout("%s: img %p\n", __func__, img_request);
2455 for_each_obj_request_safe(img_request, obj_request, next_obj_request) {
2458 ret = rbd_img_obj_request_submit(obj_request);
2466 static void rbd_img_parent_read_callback(struct rbd_img_request *img_request)
2468 struct rbd_obj_request *obj_request;
2469 struct rbd_device *rbd_dev;
2472 rbd_assert(img_request_child_test(img_request));
2474 obj_request = img_request->obj_request;
2475 rbd_assert(obj_request);
2476 rbd_assert(obj_request->img_request);
2478 obj_request->result = img_request->result;
2479 if (obj_request->result)
2483 * We need to zero anything beyond the parent overlap
2484 * boundary. Since rbd_img_obj_request_read_callback()
2485 * will zero anything beyond the end of a short read, an
2486 * easy way to do this is to pretend the data from the
2487 * parent came up short--ending at the overlap boundary.
2489 rbd_assert(obj_request->img_offset < U64_MAX - obj_request->length);
2490 obj_end = obj_request->img_offset + obj_request->length;
2491 rbd_dev = obj_request->img_request->rbd_dev;
2492 if (obj_end > rbd_dev->parent_overlap) {
2495 if (obj_request->img_offset < rbd_dev->parent_overlap)
2496 xferred = rbd_dev->parent_overlap -
2497 obj_request->img_offset;
2499 obj_request->xferred = min(img_request->xferred, xferred);
2501 obj_request->xferred = img_request->xferred;
2504 rbd_img_obj_request_read_callback(obj_request);
2505 rbd_obj_request_complete(obj_request);
2508 static void rbd_img_parent_read(struct rbd_obj_request *obj_request)
2510 struct rbd_device *rbd_dev;
2511 struct rbd_img_request *img_request;
2514 rbd_assert(obj_request_img_data_test(obj_request));
2515 rbd_assert(obj_request->img_request != NULL);
2516 rbd_assert(obj_request->result == (s32) -ENOENT);
2517 rbd_assert(obj_request->type == OBJ_REQUEST_BIO);
2519 rbd_dev = obj_request->img_request->rbd_dev;
2520 rbd_assert(rbd_dev->parent != NULL);
2521 /* rbd_read_finish(obj_request, obj_request->length); */
2522 img_request = rbd_img_request_create(rbd_dev->parent,
2523 obj_request->img_offset,
2524 obj_request->length,
2530 rbd_obj_request_get(obj_request);
2531 img_request->obj_request = obj_request;
2533 result = rbd_img_request_fill(img_request, OBJ_REQUEST_BIO,
2534 obj_request->bio_list);
2538 img_request->callback = rbd_img_parent_read_callback;
2539 result = rbd_img_request_submit(img_request);
2546 rbd_img_request_put(img_request);
2547 obj_request->result = result;
2548 obj_request->xferred = 0;
2549 obj_request_done_set(obj_request);
2552 static int rbd_obj_notify_ack(struct rbd_device *rbd_dev, u64 notify_id)
2554 struct rbd_obj_request *obj_request;
2555 struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc;
2558 obj_request = rbd_obj_request_create(rbd_dev->header_name, 0, 0,
2559 OBJ_REQUEST_NODATA);
2564 obj_request->osd_req = rbd_osd_req_create(rbd_dev, false, obj_request);
2565 if (!obj_request->osd_req)
2567 obj_request->callback = rbd_obj_request_put;
2569 osd_req_op_watch_init(obj_request->osd_req, 0, CEPH_OSD_OP_NOTIFY_ACK,
2571 rbd_osd_req_format_read(obj_request);
2573 ret = rbd_obj_request_submit(osdc, obj_request);
2576 rbd_obj_request_put(obj_request);
2581 static void rbd_watch_cb(u64 ver, u64 notify_id, u8 opcode, void *data)
2583 struct rbd_device *rbd_dev = (struct rbd_device *)data;
2588 dout("%s: \"%s\" notify_id %llu opcode %u\n", __func__,
2589 rbd_dev->header_name, (unsigned long long)notify_id,
2590 (unsigned int)opcode);
2591 (void)rbd_dev_refresh(rbd_dev);
2593 rbd_obj_notify_ack(rbd_dev, notify_id);
2597 * Request sync osd watch/unwatch. The value of "start" determines
2598 * whether a watch request is being initiated or torn down.
2600 static int rbd_dev_header_watch_sync(struct rbd_device *rbd_dev, int start)
2602 struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc;
2603 struct rbd_obj_request *obj_request;
2606 rbd_assert(start ^ !!rbd_dev->watch_event);
2607 rbd_assert(start ^ !!rbd_dev->watch_request);
2610 ret = ceph_osdc_create_event(osdc, rbd_watch_cb, rbd_dev,
2611 &rbd_dev->watch_event);
2614 rbd_assert(rbd_dev->watch_event != NULL);
2618 obj_request = rbd_obj_request_create(rbd_dev->header_name, 0, 0,
2619 OBJ_REQUEST_NODATA);
2623 obj_request->osd_req = rbd_osd_req_create(rbd_dev, true, obj_request);
2624 if (!obj_request->osd_req)
2628 ceph_osdc_set_request_linger(osdc, obj_request->osd_req);
2630 ceph_osdc_unregister_linger_request(osdc,
2631 rbd_dev->watch_request->osd_req);
2633 osd_req_op_watch_init(obj_request->osd_req, 0, CEPH_OSD_OP_WATCH,
2634 rbd_dev->watch_event->cookie, 0, start);
2635 rbd_osd_req_format_write(obj_request);
2637 ret = rbd_obj_request_submit(osdc, obj_request);
2640 ret = rbd_obj_request_wait(obj_request);
2643 ret = obj_request->result;
2648 * A watch request is set to linger, so the underlying osd
2649 * request won't go away until we unregister it. We retain
2650 * a pointer to the object request during that time (in
2651 * rbd_dev->watch_request), so we'll keep a reference to
2652 * it. We'll drop that reference (below) after we've
2656 rbd_dev->watch_request = obj_request;
2661 /* We have successfully torn down the watch request */
2663 rbd_obj_request_put(rbd_dev->watch_request);
2664 rbd_dev->watch_request = NULL;
2666 /* Cancel the event if we're tearing down, or on error */
2667 ceph_osdc_cancel_event(rbd_dev->watch_event);
2668 rbd_dev->watch_event = NULL;
2670 rbd_obj_request_put(obj_request);
2676 * Synchronous osd object method call. Returns the number of bytes
2677 * returned in the outbound buffer, or a negative error code.
2679 static int rbd_obj_method_sync(struct rbd_device *rbd_dev,
2680 const char *object_name,
2681 const char *class_name,
2682 const char *method_name,
2683 const void *outbound,
2684 size_t outbound_size,
2686 size_t inbound_size)
2688 struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc;
2689 struct rbd_obj_request *obj_request;
2690 struct page **pages;
2695 * Method calls are ultimately read operations. The result
2696 * should placed into the inbound buffer provided. They
2697 * also supply outbound data--parameters for the object
2698 * method. Currently if this is present it will be a
2701 page_count = (u32)calc_pages_for(0, inbound_size);
2702 pages = ceph_alloc_page_vector(page_count, GFP_KERNEL);
2704 return PTR_ERR(pages);
2707 obj_request = rbd_obj_request_create(object_name, 0, inbound_size,
2712 obj_request->pages = pages;
2713 obj_request->page_count = page_count;
2715 obj_request->osd_req = rbd_osd_req_create(rbd_dev, false, obj_request);
2716 if (!obj_request->osd_req)
2719 osd_req_op_cls_init(obj_request->osd_req, 0, CEPH_OSD_OP_CALL,
2720 class_name, method_name);
2721 if (outbound_size) {
2722 struct ceph_pagelist *pagelist;
2724 pagelist = kmalloc(sizeof (*pagelist), GFP_NOFS);
2728 ceph_pagelist_init(pagelist);
2729 ceph_pagelist_append(pagelist, outbound, outbound_size);
2730 osd_req_op_cls_request_data_pagelist(obj_request->osd_req, 0,
2733 osd_req_op_cls_response_data_pages(obj_request->osd_req, 0,
2734 obj_request->pages, inbound_size,
2736 rbd_osd_req_format_read(obj_request);
2738 ret = rbd_obj_request_submit(osdc, obj_request);
2741 ret = rbd_obj_request_wait(obj_request);
2745 ret = obj_request->result;
2749 rbd_assert(obj_request->xferred < (u64)INT_MAX);
2750 ret = (int)obj_request->xferred;
2751 ceph_copy_from_page_vector(pages, inbound, 0, obj_request->xferred);
2754 rbd_obj_request_put(obj_request);
2756 ceph_release_page_vector(pages, page_count);
2761 static void rbd_request_fn(struct request_queue *q)
2762 __releases(q->queue_lock) __acquires(q->queue_lock)
2764 struct rbd_device *rbd_dev = q->queuedata;
2765 bool read_only = rbd_dev->mapping.read_only;
2769 while ((rq = blk_fetch_request(q))) {
2770 bool write_request = rq_data_dir(rq) == WRITE;
2771 struct rbd_img_request *img_request;
2775 /* Ignore any non-FS requests that filter through. */
2777 if (rq->cmd_type != REQ_TYPE_FS) {
2778 dout("%s: non-fs request type %d\n", __func__,
2779 (int) rq->cmd_type);
2780 __blk_end_request_all(rq, 0);
2784 /* Ignore/skip any zero-length requests */
2786 offset = (u64) blk_rq_pos(rq) << SECTOR_SHIFT;
2787 length = (u64) blk_rq_bytes(rq);
2790 dout("%s: zero-length request\n", __func__);
2791 __blk_end_request_all(rq, 0);
2795 spin_unlock_irq(q->queue_lock);
2797 /* Disallow writes to a read-only device */
2799 if (write_request) {
2803 rbd_assert(rbd_dev->spec->snap_id == CEPH_NOSNAP);
2807 * Quit early if the mapped snapshot no longer
2808 * exists. It's still possible the snapshot will
2809 * have disappeared by the time our request arrives
2810 * at the osd, but there's no sense in sending it if
2813 if (!test_bit(RBD_DEV_FLAG_EXISTS, &rbd_dev->flags)) {
2814 dout("request for non-existent snapshot");
2815 rbd_assert(rbd_dev->spec->snap_id != CEPH_NOSNAP);
2821 if (offset && length > U64_MAX - offset + 1) {
2822 rbd_warn(rbd_dev, "bad request range (%llu~%llu)\n",
2824 goto end_request; /* Shouldn't happen */
2828 img_request = rbd_img_request_create(rbd_dev, offset, length,
2829 write_request, false);
2833 img_request->rq = rq;
2835 result = rbd_img_request_fill(img_request, OBJ_REQUEST_BIO,
2838 result = rbd_img_request_submit(img_request);
2840 rbd_img_request_put(img_request);
2842 spin_lock_irq(q->queue_lock);
2844 rbd_warn(rbd_dev, "%s %llx at %llx result %d\n",
2845 write_request ? "write" : "read",
2846 length, offset, result);
2848 __blk_end_request_all(rq, result);
2854 * a queue callback. Makes sure that we don't create a bio that spans across
2855 * multiple osd objects. One exception would be with a single page bios,
2856 * which we handle later at bio_chain_clone_range()
2858 static int rbd_merge_bvec(struct request_queue *q, struct bvec_merge_data *bmd,
2859 struct bio_vec *bvec)
2861 struct rbd_device *rbd_dev = q->queuedata;
2862 sector_t sector_offset;
2863 sector_t sectors_per_obj;
2864 sector_t obj_sector_offset;
2868 * Find how far into its rbd object the partition-relative
2869 * bio start sector is to offset relative to the enclosing
2872 sector_offset = get_start_sect(bmd->bi_bdev) + bmd->bi_sector;
2873 sectors_per_obj = 1 << (rbd_dev->header.obj_order - SECTOR_SHIFT);
2874 obj_sector_offset = sector_offset & (sectors_per_obj - 1);
2877 * Compute the number of bytes from that offset to the end
2878 * of the object. Account for what's already used by the bio.
2880 ret = (int) (sectors_per_obj - obj_sector_offset) << SECTOR_SHIFT;
2881 if (ret > bmd->bi_size)
2882 ret -= bmd->bi_size;
2887 * Don't send back more than was asked for. And if the bio
2888 * was empty, let the whole thing through because: "Note
2889 * that a block device *must* allow a single page to be
2890 * added to an empty bio."
2892 rbd_assert(bvec->bv_len <= PAGE_SIZE);
2893 if (ret > (int) bvec->bv_len || !bmd->bi_size)
2894 ret = (int) bvec->bv_len;
2899 static void rbd_free_disk(struct rbd_device *rbd_dev)
2901 struct gendisk *disk = rbd_dev->disk;
2906 rbd_dev->disk = NULL;
2907 if (disk->flags & GENHD_FL_UP) {
2910 blk_cleanup_queue(disk->queue);
2915 static int rbd_obj_read_sync(struct rbd_device *rbd_dev,
2916 const char *object_name,
2917 u64 offset, u64 length, void *buf)
2920 struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc;
2921 struct rbd_obj_request *obj_request;
2922 struct page **pages = NULL;
2927 page_count = (u32) calc_pages_for(offset, length);
2928 pages = ceph_alloc_page_vector(page_count, GFP_KERNEL);
2930 ret = PTR_ERR(pages);
2933 obj_request = rbd_obj_request_create(object_name, offset, length,
2938 obj_request->pages = pages;
2939 obj_request->page_count = page_count;
2941 obj_request->osd_req = rbd_osd_req_create(rbd_dev, false, obj_request);
2942 if (!obj_request->osd_req)
2945 osd_req_op_extent_init(obj_request->osd_req, 0, CEPH_OSD_OP_READ,
2946 offset, length, 0, 0);
2947 osd_req_op_extent_osd_data_pages(obj_request->osd_req, 0,
2949 obj_request->length,
2950 obj_request->offset & ~PAGE_MASK,
2952 rbd_osd_req_format_read(obj_request);
2954 ret = rbd_obj_request_submit(osdc, obj_request);
2957 ret = rbd_obj_request_wait(obj_request);
2961 ret = obj_request->result;
2965 rbd_assert(obj_request->xferred <= (u64) SIZE_MAX);
2966 size = (size_t) obj_request->xferred;
2967 ceph_copy_from_page_vector(pages, buf, 0, size);
2968 rbd_assert(size <= (size_t)INT_MAX);
2972 rbd_obj_request_put(obj_request);
2974 ceph_release_page_vector(pages, page_count);
2980 * Read the complete header for the given rbd device.
2982 * Returns a pointer to a dynamically-allocated buffer containing
2983 * the complete and validated header. Caller can pass the address
2984 * of a variable that will be filled in with the version of the
2985 * header object at the time it was read.
2987 * Returns a pointer-coded errno if a failure occurs.
2989 static struct rbd_image_header_ondisk *
2990 rbd_dev_v1_header_read(struct rbd_device *rbd_dev)
2992 struct rbd_image_header_ondisk *ondisk = NULL;
2999 * The complete header will include an array of its 64-bit
3000 * snapshot ids, followed by the names of those snapshots as
3001 * a contiguous block of NUL-terminated strings. Note that
3002 * the number of snapshots could change by the time we read
3003 * it in, in which case we re-read it.
3010 size = sizeof (*ondisk);
3011 size += snap_count * sizeof (struct rbd_image_snap_ondisk);
3013 ondisk = kmalloc(size, GFP_KERNEL);
3015 return ERR_PTR(-ENOMEM);
3017 ret = rbd_obj_read_sync(rbd_dev, rbd_dev->header_name,
3021 if ((size_t)ret < size) {
3023 rbd_warn(rbd_dev, "short header read (want %zd got %d)",
3027 if (!rbd_dev_ondisk_valid(ondisk)) {
3029 rbd_warn(rbd_dev, "invalid header");
3033 names_size = le64_to_cpu(ondisk->snap_names_len);
3034 want_count = snap_count;
3035 snap_count = le32_to_cpu(ondisk->snap_count);
3036 } while (snap_count != want_count);
3043 return ERR_PTR(ret);
3047 * reload the ondisk the header
3049 static int rbd_read_header(struct rbd_device *rbd_dev,
3050 struct rbd_image_header *header)
3052 struct rbd_image_header_ondisk *ondisk;
3055 ondisk = rbd_dev_v1_header_read(rbd_dev);
3057 return PTR_ERR(ondisk);
3058 ret = rbd_header_from_disk(header, ondisk);
3064 static void rbd_update_mapping_size(struct rbd_device *rbd_dev)
3066 if (rbd_dev->spec->snap_id != CEPH_NOSNAP)
3069 if (rbd_dev->mapping.size != rbd_dev->header.image_size) {
3072 rbd_dev->mapping.size = rbd_dev->header.image_size;
3073 size = (sector_t)rbd_dev->mapping.size / SECTOR_SIZE;
3074 dout("setting size to %llu sectors", (unsigned long long)size);
3075 set_capacity(rbd_dev->disk, size);
3080 * only read the first part of the ondisk header, without the snaps info
3082 static int rbd_dev_v1_refresh(struct rbd_device *rbd_dev)
3085 struct rbd_image_header h;
3087 ret = rbd_read_header(rbd_dev, &h);
3091 down_write(&rbd_dev->header_rwsem);
3093 /* Update image size, and check for resize of mapped image */
3094 rbd_dev->header.image_size = h.image_size;
3095 rbd_update_mapping_size(rbd_dev);
3097 /* rbd_dev->header.object_prefix shouldn't change */
3098 kfree(rbd_dev->header.snap_sizes);
3099 kfree(rbd_dev->header.snap_names);
3100 /* osd requests may still refer to snapc */
3101 ceph_put_snap_context(rbd_dev->header.snapc);
3103 rbd_dev->header.image_size = h.image_size;
3104 rbd_dev->header.snapc = h.snapc;
3105 rbd_dev->header.snap_names = h.snap_names;
3106 rbd_dev->header.snap_sizes = h.snap_sizes;
3107 /* Free the extra copy of the object prefix */
3108 if (strcmp(rbd_dev->header.object_prefix, h.object_prefix))
3109 rbd_warn(rbd_dev, "object prefix changed (ignoring)");
3110 kfree(h.object_prefix);
3112 up_write(&rbd_dev->header_rwsem);
3118 * Clear the rbd device's EXISTS flag if the snapshot it's mapped to
3119 * has disappeared from the (just updated) snapshot context.
3121 static void rbd_exists_validate(struct rbd_device *rbd_dev)
3125 if (!test_bit(RBD_DEV_FLAG_EXISTS, &rbd_dev->flags))
3128 snap_id = rbd_dev->spec->snap_id;
3129 if (snap_id == CEPH_NOSNAP)
3132 if (rbd_dev_snap_index(rbd_dev, snap_id) == BAD_SNAP_INDEX)
3133 clear_bit(RBD_DEV_FLAG_EXISTS, &rbd_dev->flags);
3136 static int rbd_dev_refresh(struct rbd_device *rbd_dev)
3141 rbd_assert(rbd_image_format_valid(rbd_dev->image_format));
3142 image_size = rbd_dev->header.image_size;
3143 mutex_lock_nested(&ctl_mutex, SINGLE_DEPTH_NESTING);
3144 if (rbd_dev->image_format == 1)
3145 ret = rbd_dev_v1_refresh(rbd_dev);
3147 ret = rbd_dev_v2_refresh(rbd_dev);
3149 /* If it's a mapped snapshot, validate its EXISTS flag */
3151 rbd_exists_validate(rbd_dev);
3152 mutex_unlock(&ctl_mutex);
3154 rbd_warn(rbd_dev, "got notification but failed to "
3155 " update snaps: %d\n", ret);
3156 if (image_size != rbd_dev->header.image_size)
3157 revalidate_disk(rbd_dev->disk);
3162 static int rbd_init_disk(struct rbd_device *rbd_dev)
3164 struct gendisk *disk;
3165 struct request_queue *q;
3168 /* create gendisk info */
3169 disk = alloc_disk(RBD_MINORS_PER_MAJOR);
3173 snprintf(disk->disk_name, sizeof(disk->disk_name), RBD_DRV_NAME "%d",
3175 disk->major = rbd_dev->major;
3176 disk->first_minor = 0;
3177 disk->fops = &rbd_bd_ops;
3178 disk->private_data = rbd_dev;
3180 q = blk_init_queue(rbd_request_fn, &rbd_dev->lock);
3184 /* We use the default size, but let's be explicit about it. */
3185 blk_queue_physical_block_size(q, SECTOR_SIZE);
3187 /* set io sizes to object size */
3188 segment_size = rbd_obj_bytes(&rbd_dev->header);
3189 blk_queue_max_hw_sectors(q, segment_size / SECTOR_SIZE);
3190 blk_queue_max_segment_size(q, segment_size);
3191 blk_queue_io_min(q, segment_size);
3192 blk_queue_io_opt(q, segment_size);
3194 blk_queue_merge_bvec(q, rbd_merge_bvec);
3197 q->queuedata = rbd_dev;
3199 rbd_dev->disk = disk;
3212 static struct rbd_device *dev_to_rbd_dev(struct device *dev)
3214 return container_of(dev, struct rbd_device, dev);
3217 static ssize_t rbd_size_show(struct device *dev,
3218 struct device_attribute *attr, char *buf)
3220 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
3222 return sprintf(buf, "%llu\n",
3223 (unsigned long long)rbd_dev->mapping.size);
3227 * Note this shows the features for whatever's mapped, which is not
3228 * necessarily the base image.
3230 static ssize_t rbd_features_show(struct device *dev,
3231 struct device_attribute *attr, char *buf)
3233 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
3235 return sprintf(buf, "0x%016llx\n",
3236 (unsigned long long)rbd_dev->mapping.features);
3239 static ssize_t rbd_major_show(struct device *dev,
3240 struct device_attribute *attr, char *buf)
3242 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
3245 return sprintf(buf, "%d\n", rbd_dev->major);
3247 return sprintf(buf, "(none)\n");
3251 static ssize_t rbd_client_id_show(struct device *dev,
3252 struct device_attribute *attr, char *buf)
3254 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
3256 return sprintf(buf, "client%lld\n",
3257 ceph_client_id(rbd_dev->rbd_client->client));
3260 static ssize_t rbd_pool_show(struct device *dev,
3261 struct device_attribute *attr, char *buf)
3263 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
3265 return sprintf(buf, "%s\n", rbd_dev->spec->pool_name);
3268 static ssize_t rbd_pool_id_show(struct device *dev,
3269 struct device_attribute *attr, char *buf)
3271 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
3273 return sprintf(buf, "%llu\n",
3274 (unsigned long long) rbd_dev->spec->pool_id);
3277 static ssize_t rbd_name_show(struct device *dev,
3278 struct device_attribute *attr, char *buf)
3280 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
3282 if (rbd_dev->spec->image_name)
3283 return sprintf(buf, "%s\n", rbd_dev->spec->image_name);
3285 return sprintf(buf, "(unknown)\n");
3288 static ssize_t rbd_image_id_show(struct device *dev,
3289 struct device_attribute *attr, char *buf)
3291 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
3293 return sprintf(buf, "%s\n", rbd_dev->spec->image_id);
3297 * Shows the name of the currently-mapped snapshot (or
3298 * RBD_SNAP_HEAD_NAME for the base image).
3300 static ssize_t rbd_snap_show(struct device *dev,
3301 struct device_attribute *attr,
3304 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
3306 return sprintf(buf, "%s\n", rbd_dev->spec->snap_name);
3310 * For an rbd v2 image, shows the pool id, image id, and snapshot id
3311 * for the parent image. If there is no parent, simply shows
3312 * "(no parent image)".
3314 static ssize_t rbd_parent_show(struct device *dev,
3315 struct device_attribute *attr,
3318 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
3319 struct rbd_spec *spec = rbd_dev->parent_spec;
3324 return sprintf(buf, "(no parent image)\n");
3326 count = sprintf(bufp, "pool_id %llu\npool_name %s\n",
3327 (unsigned long long) spec->pool_id, spec->pool_name);
3332 count = sprintf(bufp, "image_id %s\nimage_name %s\n", spec->image_id,
3333 spec->image_name ? spec->image_name : "(unknown)");
3338 count = sprintf(bufp, "snap_id %llu\nsnap_name %s\n",
3339 (unsigned long long) spec->snap_id, spec->snap_name);
3344 count = sprintf(bufp, "overlap %llu\n", rbd_dev->parent_overlap);
3349 return (ssize_t) (bufp - buf);
3352 static ssize_t rbd_image_refresh(struct device *dev,
3353 struct device_attribute *attr,
3357 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
3360 ret = rbd_dev_refresh(rbd_dev);
3362 return ret < 0 ? ret : size;
3365 static DEVICE_ATTR(size, S_IRUGO, rbd_size_show, NULL);
3366 static DEVICE_ATTR(features, S_IRUGO, rbd_features_show, NULL);
3367 static DEVICE_ATTR(major, S_IRUGO, rbd_major_show, NULL);
3368 static DEVICE_ATTR(client_id, S_IRUGO, rbd_client_id_show, NULL);
3369 static DEVICE_ATTR(pool, S_IRUGO, rbd_pool_show, NULL);
3370 static DEVICE_ATTR(pool_id, S_IRUGO, rbd_pool_id_show, NULL);
3371 static DEVICE_ATTR(name, S_IRUGO, rbd_name_show, NULL);
3372 static DEVICE_ATTR(image_id, S_IRUGO, rbd_image_id_show, NULL);
3373 static DEVICE_ATTR(refresh, S_IWUSR, NULL, rbd_image_refresh);
3374 static DEVICE_ATTR(current_snap, S_IRUGO, rbd_snap_show, NULL);
3375 static DEVICE_ATTR(parent, S_IRUGO, rbd_parent_show, NULL);
3377 static struct attribute *rbd_attrs[] = {
3378 &dev_attr_size.attr,
3379 &dev_attr_features.attr,
3380 &dev_attr_major.attr,
3381 &dev_attr_client_id.attr,
3382 &dev_attr_pool.attr,
3383 &dev_attr_pool_id.attr,
3384 &dev_attr_name.attr,
3385 &dev_attr_image_id.attr,
3386 &dev_attr_current_snap.attr,
3387 &dev_attr_parent.attr,
3388 &dev_attr_refresh.attr,
3392 static struct attribute_group rbd_attr_group = {
3396 static const struct attribute_group *rbd_attr_groups[] = {
3401 static void rbd_sysfs_dev_release(struct device *dev)
3405 static struct device_type rbd_device_type = {
3407 .groups = rbd_attr_groups,
3408 .release = rbd_sysfs_dev_release,
3411 static struct rbd_spec *rbd_spec_get(struct rbd_spec *spec)
3413 kref_get(&spec->kref);
3418 static void rbd_spec_free(struct kref *kref);
3419 static void rbd_spec_put(struct rbd_spec *spec)
3422 kref_put(&spec->kref, rbd_spec_free);
3425 static struct rbd_spec *rbd_spec_alloc(void)
3427 struct rbd_spec *spec;
3429 spec = kzalloc(sizeof (*spec), GFP_KERNEL);
3432 kref_init(&spec->kref);
3437 static void rbd_spec_free(struct kref *kref)
3439 struct rbd_spec *spec = container_of(kref, struct rbd_spec, kref);
3441 kfree(spec->pool_name);
3442 kfree(spec->image_id);
3443 kfree(spec->image_name);
3444 kfree(spec->snap_name);
3448 static struct rbd_device *rbd_dev_create(struct rbd_client *rbdc,
3449 struct rbd_spec *spec)
3451 struct rbd_device *rbd_dev;
3453 rbd_dev = kzalloc(sizeof (*rbd_dev), GFP_KERNEL);
3457 spin_lock_init(&rbd_dev->lock);
3459 INIT_LIST_HEAD(&rbd_dev->node);
3460 init_rwsem(&rbd_dev->header_rwsem);
3462 rbd_dev->spec = spec;
3463 rbd_dev->rbd_client = rbdc;
3465 /* Initialize the layout used for all rbd requests */
3467 rbd_dev->layout.fl_stripe_unit = cpu_to_le32(1 << RBD_MAX_OBJ_ORDER);
3468 rbd_dev->layout.fl_stripe_count = cpu_to_le32(1);
3469 rbd_dev->layout.fl_object_size = cpu_to_le32(1 << RBD_MAX_OBJ_ORDER);
3470 rbd_dev->layout.fl_pg_pool = cpu_to_le32((u32) spec->pool_id);
3475 static void rbd_dev_destroy(struct rbd_device *rbd_dev)
3477 rbd_put_client(rbd_dev->rbd_client);
3478 rbd_spec_put(rbd_dev->spec);
3483 * Get the size and object order for an image snapshot, or if
3484 * snap_id is CEPH_NOSNAP, gets this information for the base
3487 static int _rbd_dev_v2_snap_size(struct rbd_device *rbd_dev, u64 snap_id,
3488 u8 *order, u64 *snap_size)
3490 __le64 snapid = cpu_to_le64(snap_id);
3495 } __attribute__ ((packed)) size_buf = { 0 };
3497 ret = rbd_obj_method_sync(rbd_dev, rbd_dev->header_name,
3499 &snapid, sizeof (snapid),
3500 &size_buf, sizeof (size_buf));
3501 dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret);
3504 if (ret < sizeof (size_buf))
3508 *order = size_buf.order;
3509 *snap_size = le64_to_cpu(size_buf.size);
3511 dout(" snap_id 0x%016llx order = %u, snap_size = %llu\n",
3512 (unsigned long long)snap_id, (unsigned int)*order,
3513 (unsigned long long)*snap_size);
3518 static int rbd_dev_v2_image_size(struct rbd_device *rbd_dev)
3520 return _rbd_dev_v2_snap_size(rbd_dev, CEPH_NOSNAP,
3521 &rbd_dev->header.obj_order,
3522 &rbd_dev->header.image_size);
3525 static int rbd_dev_v2_object_prefix(struct rbd_device *rbd_dev)
3531 reply_buf = kzalloc(RBD_OBJ_PREFIX_LEN_MAX, GFP_KERNEL);
3535 ret = rbd_obj_method_sync(rbd_dev, rbd_dev->header_name,
3536 "rbd", "get_object_prefix", NULL, 0,
3537 reply_buf, RBD_OBJ_PREFIX_LEN_MAX);
3538 dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret);
3543 rbd_dev->header.object_prefix = ceph_extract_encoded_string(&p,
3544 p + ret, NULL, GFP_NOIO);
3547 if (IS_ERR(rbd_dev->header.object_prefix)) {
3548 ret = PTR_ERR(rbd_dev->header.object_prefix);
3549 rbd_dev->header.object_prefix = NULL;
3551 dout(" object_prefix = %s\n", rbd_dev->header.object_prefix);
3559 static int _rbd_dev_v2_snap_features(struct rbd_device *rbd_dev, u64 snap_id,
3562 __le64 snapid = cpu_to_le64(snap_id);
3566 } __attribute__ ((packed)) features_buf = { 0 };
3570 ret = rbd_obj_method_sync(rbd_dev, rbd_dev->header_name,
3571 "rbd", "get_features",
3572 &snapid, sizeof (snapid),
3573 &features_buf, sizeof (features_buf));
3574 dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret);
3577 if (ret < sizeof (features_buf))
3580 incompat = le64_to_cpu(features_buf.incompat);
3581 if (incompat & ~RBD_FEATURES_SUPPORTED)
3584 *snap_features = le64_to_cpu(features_buf.features);
3586 dout(" snap_id 0x%016llx features = 0x%016llx incompat = 0x%016llx\n",
3587 (unsigned long long)snap_id,
3588 (unsigned long long)*snap_features,
3589 (unsigned long long)le64_to_cpu(features_buf.incompat));
3594 static int rbd_dev_v2_features(struct rbd_device *rbd_dev)
3596 return _rbd_dev_v2_snap_features(rbd_dev, CEPH_NOSNAP,
3597 &rbd_dev->header.features);
3600 static int rbd_dev_v2_parent_info(struct rbd_device *rbd_dev)
3602 struct rbd_spec *parent_spec;
3604 void *reply_buf = NULL;
3612 parent_spec = rbd_spec_alloc();
3616 size = sizeof (__le64) + /* pool_id */
3617 sizeof (__le32) + RBD_IMAGE_ID_LEN_MAX + /* image_id */
3618 sizeof (__le64) + /* snap_id */
3619 sizeof (__le64); /* overlap */
3620 reply_buf = kmalloc(size, GFP_KERNEL);
3626 snapid = cpu_to_le64(CEPH_NOSNAP);
3627 ret = rbd_obj_method_sync(rbd_dev, rbd_dev->header_name,
3628 "rbd", "get_parent",
3629 &snapid, sizeof (snapid),
3631 dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret);
3636 end = reply_buf + ret;
3638 ceph_decode_64_safe(&p, end, parent_spec->pool_id, out_err);
3639 if (parent_spec->pool_id == CEPH_NOPOOL)
3640 goto out; /* No parent? No problem. */
3642 /* The ceph file layout needs to fit pool id in 32 bits */
3645 if (parent_spec->pool_id > (u64)U32_MAX) {
3646 rbd_warn(NULL, "parent pool id too large (%llu > %u)\n",
3647 (unsigned long long)parent_spec->pool_id, U32_MAX);
3651 image_id = ceph_extract_encoded_string(&p, end, NULL, GFP_KERNEL);
3652 if (IS_ERR(image_id)) {
3653 ret = PTR_ERR(image_id);
3656 parent_spec->image_id = image_id;
3657 ceph_decode_64_safe(&p, end, parent_spec->snap_id, out_err);
3658 ceph_decode_64_safe(&p, end, overlap, out_err);
3660 rbd_dev->parent_overlap = overlap;
3661 rbd_dev->parent_spec = parent_spec;
3662 parent_spec = NULL; /* rbd_dev now owns this */
3667 rbd_spec_put(parent_spec);
3672 static int rbd_dev_v2_striping_info(struct rbd_device *rbd_dev)
3676 __le64 stripe_count;
3677 } __attribute__ ((packed)) striping_info_buf = { 0 };
3678 size_t size = sizeof (striping_info_buf);
3685 ret = rbd_obj_method_sync(rbd_dev, rbd_dev->header_name,
3686 "rbd", "get_stripe_unit_count", NULL, 0,
3687 (char *)&striping_info_buf, size);
3688 dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret);
3695 * We don't actually support the "fancy striping" feature
3696 * (STRIPINGV2) yet, but if the striping sizes are the
3697 * defaults the behavior is the same as before. So find
3698 * out, and only fail if the image has non-default values.
3701 obj_size = (u64)1 << rbd_dev->header.obj_order;
3702 p = &striping_info_buf;
3703 stripe_unit = ceph_decode_64(&p);
3704 if (stripe_unit != obj_size) {
3705 rbd_warn(rbd_dev, "unsupported stripe unit "
3706 "(got %llu want %llu)",
3707 stripe_unit, obj_size);
3710 stripe_count = ceph_decode_64(&p);
3711 if (stripe_count != 1) {
3712 rbd_warn(rbd_dev, "unsupported stripe count "
3713 "(got %llu want 1)", stripe_count);
3716 rbd_dev->header.stripe_unit = stripe_unit;
3717 rbd_dev->header.stripe_count = stripe_count;
3722 static char *rbd_dev_image_name(struct rbd_device *rbd_dev)
3724 size_t image_id_size;
3729 void *reply_buf = NULL;
3731 char *image_name = NULL;
3734 rbd_assert(!rbd_dev->spec->image_name);
3736 len = strlen(rbd_dev->spec->image_id);
3737 image_id_size = sizeof (__le32) + len;
3738 image_id = kmalloc(image_id_size, GFP_KERNEL);
3743 end = image_id + image_id_size;
3744 ceph_encode_string(&p, end, rbd_dev->spec->image_id, (u32)len);
3746 size = sizeof (__le32) + RBD_IMAGE_NAME_LEN_MAX;
3747 reply_buf = kmalloc(size, GFP_KERNEL);
3751 ret = rbd_obj_method_sync(rbd_dev, RBD_DIRECTORY,
3752 "rbd", "dir_get_name",
3753 image_id, image_id_size,
3758 end = reply_buf + ret;
3760 image_name = ceph_extract_encoded_string(&p, end, &len, GFP_KERNEL);
3761 if (IS_ERR(image_name))
3764 dout("%s: name is %s len is %zd\n", __func__, image_name, len);
3772 static u64 rbd_v1_snap_id_by_name(struct rbd_device *rbd_dev, const char *name)
3774 struct ceph_snap_context *snapc = rbd_dev->header.snapc;
3775 const char *snap_name;
3778 /* Skip over names until we find the one we are looking for */
3780 snap_name = rbd_dev->header.snap_names;
3781 while (which < snapc->num_snaps) {
3782 if (!strcmp(name, snap_name))
3783 return snapc->snaps[which];
3784 snap_name += strlen(snap_name) + 1;
3790 static u64 rbd_v2_snap_id_by_name(struct rbd_device *rbd_dev, const char *name)
3792 struct ceph_snap_context *snapc = rbd_dev->header.snapc;
3797 for (which = 0; !found && which < snapc->num_snaps; which++) {
3798 const char *snap_name;
3800 snap_id = snapc->snaps[which];
3801 snap_name = rbd_dev_v2_snap_name(rbd_dev, snap_id);
3802 if (IS_ERR(snap_name))
3804 found = !strcmp(name, snap_name);
3807 return found ? snap_id : CEPH_NOSNAP;
3811 * Assumes name is never RBD_SNAP_HEAD_NAME; returns CEPH_NOSNAP if
3812 * no snapshot by that name is found, or if an error occurs.
3814 static u64 rbd_snap_id_by_name(struct rbd_device *rbd_dev, const char *name)
3816 if (rbd_dev->image_format == 1)
3817 return rbd_v1_snap_id_by_name(rbd_dev, name);
3819 return rbd_v2_snap_id_by_name(rbd_dev, name);
3823 * When an rbd image has a parent image, it is identified by the
3824 * pool, image, and snapshot ids (not names). This function fills
3825 * in the names for those ids. (It's OK if we can't figure out the
3826 * name for an image id, but the pool and snapshot ids should always
3827 * exist and have names.) All names in an rbd spec are dynamically
3830 * When an image being mapped (not a parent) is probed, we have the
3831 * pool name and pool id, image name and image id, and the snapshot
3832 * name. The only thing we're missing is the snapshot id.
3834 static int rbd_dev_spec_update(struct rbd_device *rbd_dev)
3836 struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc;
3837 struct rbd_spec *spec = rbd_dev->spec;
3838 const char *pool_name;
3839 const char *image_name;
3840 const char *snap_name;
3844 * An image being mapped will have the pool name (etc.), but
3845 * we need to look up the snapshot id.
3847 if (spec->pool_name) {
3848 if (strcmp(spec->snap_name, RBD_SNAP_HEAD_NAME)) {
3851 snap_id = rbd_snap_id_by_name(rbd_dev, spec->snap_name);
3852 if (snap_id == CEPH_NOSNAP)
3854 spec->snap_id = snap_id;
3856 spec->snap_id = CEPH_NOSNAP;
3862 /* Get the pool name; we have to make our own copy of this */
3864 pool_name = ceph_pg_pool_name_by_id(osdc->osdmap, spec->pool_id);
3866 rbd_warn(rbd_dev, "no pool with id %llu", spec->pool_id);
3869 pool_name = kstrdup(pool_name, GFP_KERNEL);
3873 /* Fetch the image name; tolerate failure here */
3875 image_name = rbd_dev_image_name(rbd_dev);
3877 rbd_warn(rbd_dev, "unable to get image name");
3879 /* Look up the snapshot name, and make a copy */
3881 snap_name = rbd_snap_name(rbd_dev, spec->snap_id);
3887 spec->pool_name = pool_name;
3888 spec->image_name = image_name;
3889 spec->snap_name = snap_name;
3899 static int rbd_dev_v2_snap_context(struct rbd_device *rbd_dev)
3908 struct ceph_snap_context *snapc;
3912 * We'll need room for the seq value (maximum snapshot id),
3913 * snapshot count, and array of that many snapshot ids.
3914 * For now we have a fixed upper limit on the number we're
3915 * prepared to receive.
3917 size = sizeof (__le64) + sizeof (__le32) +
3918 RBD_MAX_SNAP_COUNT * sizeof (__le64);
3919 reply_buf = kzalloc(size, GFP_KERNEL);
3923 ret = rbd_obj_method_sync(rbd_dev, rbd_dev->header_name,
3924 "rbd", "get_snapcontext", NULL, 0,
3926 dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret);
3931 end = reply_buf + ret;
3933 ceph_decode_64_safe(&p, end, seq, out);
3934 ceph_decode_32_safe(&p, end, snap_count, out);
3937 * Make sure the reported number of snapshot ids wouldn't go
3938 * beyond the end of our buffer. But before checking that,
3939 * make sure the computed size of the snapshot context we
3940 * allocate is representable in a size_t.
3942 if (snap_count > (SIZE_MAX - sizeof (struct ceph_snap_context))
3947 if (!ceph_has_room(&p, end, snap_count * sizeof (__le64)))
3951 snapc = ceph_create_snap_context(snap_count, GFP_KERNEL);
3957 for (i = 0; i < snap_count; i++)
3958 snapc->snaps[i] = ceph_decode_64(&p);
3960 rbd_dev->header.snapc = snapc;
3962 dout(" snap context seq = %llu, snap_count = %u\n",
3963 (unsigned long long)seq, (unsigned int)snap_count);
3970 static const char *rbd_dev_v2_snap_name(struct rbd_device *rbd_dev,
3981 size = sizeof (__le32) + RBD_MAX_SNAP_NAME_LEN;
3982 reply_buf = kmalloc(size, GFP_KERNEL);
3984 return ERR_PTR(-ENOMEM);
3986 snapid = cpu_to_le64(snap_id);
3987 ret = rbd_obj_method_sync(rbd_dev, rbd_dev->header_name,
3988 "rbd", "get_snapshot_name",
3989 &snapid, sizeof (snapid),
3991 dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret);
3993 snap_name = ERR_PTR(ret);
3998 end = reply_buf + ret;
3999 snap_name = ceph_extract_encoded_string(&p, end, NULL, GFP_KERNEL);
4000 if (IS_ERR(snap_name))
4003 dout(" snap_id 0x%016llx snap_name = %s\n",
4004 (unsigned long long)snap_id, snap_name);
4011 static int rbd_dev_v2_refresh(struct rbd_device *rbd_dev)
4015 down_write(&rbd_dev->header_rwsem);
4017 ret = rbd_dev_v2_image_size(rbd_dev);
4020 rbd_update_mapping_size(rbd_dev);
4022 ret = rbd_dev_v2_snap_context(rbd_dev);
4023 dout("rbd_dev_v2_snap_context returned %d\n", ret);
4027 up_write(&rbd_dev->header_rwsem);
4032 static int rbd_bus_add_dev(struct rbd_device *rbd_dev)
4037 mutex_lock_nested(&ctl_mutex, SINGLE_DEPTH_NESTING);
4039 dev = &rbd_dev->dev;
4040 dev->bus = &rbd_bus_type;
4041 dev->type = &rbd_device_type;
4042 dev->parent = &rbd_root_dev;
4043 dev->release = rbd_dev_device_release;
4044 dev_set_name(dev, "%d", rbd_dev->dev_id);
4045 ret = device_register(dev);
4047 mutex_unlock(&ctl_mutex);
4052 static void rbd_bus_del_dev(struct rbd_device *rbd_dev)
4054 device_unregister(&rbd_dev->dev);
4057 static atomic64_t rbd_dev_id_max = ATOMIC64_INIT(0);
4060 * Get a unique rbd identifier for the given new rbd_dev, and add
4061 * the rbd_dev to the global list. The minimum rbd id is 1.
4063 static void rbd_dev_id_get(struct rbd_device *rbd_dev)
4065 rbd_dev->dev_id = atomic64_inc_return(&rbd_dev_id_max);
4067 spin_lock(&rbd_dev_list_lock);
4068 list_add_tail(&rbd_dev->node, &rbd_dev_list);
4069 spin_unlock(&rbd_dev_list_lock);
4070 dout("rbd_dev %p given dev id %llu\n", rbd_dev,
4071 (unsigned long long) rbd_dev->dev_id);
4075 * Remove an rbd_dev from the global list, and record that its
4076 * identifier is no longer in use.
4078 static void rbd_dev_id_put(struct rbd_device *rbd_dev)
4080 struct list_head *tmp;
4081 int rbd_id = rbd_dev->dev_id;
4084 rbd_assert(rbd_id > 0);
4086 dout("rbd_dev %p released dev id %llu\n", rbd_dev,
4087 (unsigned long long) rbd_dev->dev_id);
4088 spin_lock(&rbd_dev_list_lock);
4089 list_del_init(&rbd_dev->node);
4092 * If the id being "put" is not the current maximum, there
4093 * is nothing special we need to do.
4095 if (rbd_id != atomic64_read(&rbd_dev_id_max)) {
4096 spin_unlock(&rbd_dev_list_lock);
4101 * We need to update the current maximum id. Search the
4102 * list to find out what it is. We're more likely to find
4103 * the maximum at the end, so search the list backward.
4106 list_for_each_prev(tmp, &rbd_dev_list) {
4107 struct rbd_device *rbd_dev;
4109 rbd_dev = list_entry(tmp, struct rbd_device, node);
4110 if (rbd_dev->dev_id > max_id)
4111 max_id = rbd_dev->dev_id;
4113 spin_unlock(&rbd_dev_list_lock);
4116 * The max id could have been updated by rbd_dev_id_get(), in
4117 * which case it now accurately reflects the new maximum.
4118 * Be careful not to overwrite the maximum value in that
4121 atomic64_cmpxchg(&rbd_dev_id_max, rbd_id, max_id);
4122 dout(" max dev id has been reset\n");
4126 * Skips over white space at *buf, and updates *buf to point to the
4127 * first found non-space character (if any). Returns the length of
4128 * the token (string of non-white space characters) found. Note
4129 * that *buf must be terminated with '\0'.
4131 static inline size_t next_token(const char **buf)
4134 * These are the characters that produce nonzero for
4135 * isspace() in the "C" and "POSIX" locales.
4137 const char *spaces = " \f\n\r\t\v";
4139 *buf += strspn(*buf, spaces); /* Find start of token */
4141 return strcspn(*buf, spaces); /* Return token length */
4145 * Finds the next token in *buf, and if the provided token buffer is
4146 * big enough, copies the found token into it. The result, if
4147 * copied, is guaranteed to be terminated with '\0'. Note that *buf
4148 * must be terminated with '\0' on entry.
4150 * Returns the length of the token found (not including the '\0').
4151 * Return value will be 0 if no token is found, and it will be >=
4152 * token_size if the token would not fit.
4154 * The *buf pointer will be updated to point beyond the end of the
4155 * found token. Note that this occurs even if the token buffer is
4156 * too small to hold it.
4158 static inline size_t copy_token(const char **buf,
4164 len = next_token(buf);
4165 if (len < token_size) {
4166 memcpy(token, *buf, len);
4167 *(token + len) = '\0';
4175 * Finds the next token in *buf, dynamically allocates a buffer big
4176 * enough to hold a copy of it, and copies the token into the new
4177 * buffer. The copy is guaranteed to be terminated with '\0'. Note
4178 * that a duplicate buffer is created even for a zero-length token.
4180 * Returns a pointer to the newly-allocated duplicate, or a null
4181 * pointer if memory for the duplicate was not available. If
4182 * the lenp argument is a non-null pointer, the length of the token
4183 * (not including the '\0') is returned in *lenp.
4185 * If successful, the *buf pointer will be updated to point beyond
4186 * the end of the found token.
4188 * Note: uses GFP_KERNEL for allocation.
4190 static inline char *dup_token(const char **buf, size_t *lenp)
4195 len = next_token(buf);
4196 dup = kmemdup(*buf, len + 1, GFP_KERNEL);
4199 *(dup + len) = '\0';
4209 * Parse the options provided for an "rbd add" (i.e., rbd image
4210 * mapping) request. These arrive via a write to /sys/bus/rbd/add,
4211 * and the data written is passed here via a NUL-terminated buffer.
4212 * Returns 0 if successful or an error code otherwise.
4214 * The information extracted from these options is recorded in
4215 * the other parameters which return dynamically-allocated
4218 * The address of a pointer that will refer to a ceph options
4219 * structure. Caller must release the returned pointer using
4220 * ceph_destroy_options() when it is no longer needed.
4222 * Address of an rbd options pointer. Fully initialized by
4223 * this function; caller must release with kfree().
4225 * Address of an rbd image specification pointer. Fully
4226 * initialized by this function based on parsed options.
4227 * Caller must release with rbd_spec_put().
4229 * The options passed take this form:
4230 * <mon_addrs> <options> <pool_name> <image_name> [<snap_id>]
4233 * A comma-separated list of one or more monitor addresses.
4234 * A monitor address is an ip address, optionally followed
4235 * by a port number (separated by a colon).
4236 * I.e.: ip1[:port1][,ip2[:port2]...]
4238 * A comma-separated list of ceph and/or rbd options.
4240 * The name of the rados pool containing the rbd image.
4242 * The name of the image in that pool to map.
4244 * An optional snapshot id. If provided, the mapping will
4245 * present data from the image at the time that snapshot was
4246 * created. The image head is used if no snapshot id is
4247 * provided. Snapshot mappings are always read-only.
4249 static int rbd_add_parse_args(const char *buf,
4250 struct ceph_options **ceph_opts,
4251 struct rbd_options **opts,
4252 struct rbd_spec **rbd_spec)
4256 const char *mon_addrs;
4258 size_t mon_addrs_size;
4259 struct rbd_spec *spec = NULL;
4260 struct rbd_options *rbd_opts = NULL;
4261 struct ceph_options *copts;
4264 /* The first four tokens are required */
4266 len = next_token(&buf);
4268 rbd_warn(NULL, "no monitor address(es) provided");
4272 mon_addrs_size = len + 1;
4276 options = dup_token(&buf, NULL);
4280 rbd_warn(NULL, "no options provided");
4284 spec = rbd_spec_alloc();
4288 spec->pool_name = dup_token(&buf, NULL);
4289 if (!spec->pool_name)
4291 if (!*spec->pool_name) {
4292 rbd_warn(NULL, "no pool name provided");
4296 spec->image_name = dup_token(&buf, NULL);
4297 if (!spec->image_name)
4299 if (!*spec->image_name) {
4300 rbd_warn(NULL, "no image name provided");
4305 * Snapshot name is optional; default is to use "-"
4306 * (indicating the head/no snapshot).
4308 len = next_token(&buf);
4310 buf = RBD_SNAP_HEAD_NAME; /* No snapshot supplied */
4311 len = sizeof (RBD_SNAP_HEAD_NAME) - 1;
4312 } else if (len > RBD_MAX_SNAP_NAME_LEN) {
4313 ret = -ENAMETOOLONG;
4316 snap_name = kmemdup(buf, len + 1, GFP_KERNEL);
4319 *(snap_name + len) = '\0';
4320 spec->snap_name = snap_name;
4322 /* Initialize all rbd options to the defaults */
4324 rbd_opts = kzalloc(sizeof (*rbd_opts), GFP_KERNEL);
4328 rbd_opts->read_only = RBD_READ_ONLY_DEFAULT;
4330 copts = ceph_parse_options(options, mon_addrs,
4331 mon_addrs + mon_addrs_size - 1,
4332 parse_rbd_opts_token, rbd_opts);
4333 if (IS_ERR(copts)) {
4334 ret = PTR_ERR(copts);
4355 * An rbd format 2 image has a unique identifier, distinct from the
4356 * name given to it by the user. Internally, that identifier is
4357 * what's used to specify the names of objects related to the image.
4359 * A special "rbd id" object is used to map an rbd image name to its
4360 * id. If that object doesn't exist, then there is no v2 rbd image
4361 * with the supplied name.
4363 * This function will record the given rbd_dev's image_id field if
4364 * it can be determined, and in that case will return 0. If any
4365 * errors occur a negative errno will be returned and the rbd_dev's
4366 * image_id field will be unchanged (and should be NULL).
4368 static int rbd_dev_image_id(struct rbd_device *rbd_dev)
4377 * When probing a parent image, the image id is already
4378 * known (and the image name likely is not). There's no
4379 * need to fetch the image id again in this case. We
4380 * do still need to set the image format though.
4382 if (rbd_dev->spec->image_id) {
4383 rbd_dev->image_format = *rbd_dev->spec->image_id ? 2 : 1;
4389 * First, see if the format 2 image id file exists, and if
4390 * so, get the image's persistent id from it.
4392 size = sizeof (RBD_ID_PREFIX) + strlen(rbd_dev->spec->image_name);
4393 object_name = kmalloc(size, GFP_NOIO);
4396 sprintf(object_name, "%s%s", RBD_ID_PREFIX, rbd_dev->spec->image_name);
4397 dout("rbd id object name is %s\n", object_name);
4399 /* Response will be an encoded string, which includes a length */
4401 size = sizeof (__le32) + RBD_IMAGE_ID_LEN_MAX;
4402 response = kzalloc(size, GFP_NOIO);
4408 /* If it doesn't exist we'll assume it's a format 1 image */
4410 ret = rbd_obj_method_sync(rbd_dev, object_name,
4411 "rbd", "get_id", NULL, 0,
4412 response, RBD_IMAGE_ID_LEN_MAX);
4413 dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret);
4414 if (ret == -ENOENT) {
4415 image_id = kstrdup("", GFP_KERNEL);
4416 ret = image_id ? 0 : -ENOMEM;
4418 rbd_dev->image_format = 1;
4419 } else if (ret > sizeof (__le32)) {
4422 image_id = ceph_extract_encoded_string(&p, p + ret,
4424 ret = IS_ERR(image_id) ? PTR_ERR(image_id) : 0;
4426 rbd_dev->image_format = 2;
4432 rbd_dev->spec->image_id = image_id;
4433 dout("image_id is %s\n", image_id);
4442 /* Undo whatever state changes are made by v1 or v2 image probe */
4444 static void rbd_dev_unprobe(struct rbd_device *rbd_dev)
4446 struct rbd_image_header *header;
4448 rbd_dev_remove_parent(rbd_dev);
4449 rbd_spec_put(rbd_dev->parent_spec);
4450 rbd_dev->parent_spec = NULL;
4451 rbd_dev->parent_overlap = 0;
4453 /* Free dynamic fields from the header, then zero it out */
4455 header = &rbd_dev->header;
4456 ceph_put_snap_context(header->snapc);
4457 kfree(header->snap_sizes);
4458 kfree(header->snap_names);
4459 kfree(header->object_prefix);
4460 memset(header, 0, sizeof (*header));
4463 static int rbd_dev_v1_probe(struct rbd_device *rbd_dev)
4467 /* Populate rbd image metadata */
4469 ret = rbd_read_header(rbd_dev, &rbd_dev->header);
4473 /* Version 1 images have no parent (no layering) */
4475 rbd_dev->parent_spec = NULL;
4476 rbd_dev->parent_overlap = 0;
4478 dout("discovered version 1 image, header name is %s\n",
4479 rbd_dev->header_name);
4484 kfree(rbd_dev->header_name);
4485 rbd_dev->header_name = NULL;
4486 kfree(rbd_dev->spec->image_id);
4487 rbd_dev->spec->image_id = NULL;
4492 static int rbd_dev_v2_probe(struct rbd_device *rbd_dev)
4496 ret = rbd_dev_v2_image_size(rbd_dev);
4500 /* Get the object prefix (a.k.a. block_name) for the image */
4502 ret = rbd_dev_v2_object_prefix(rbd_dev);
4506 /* Get the and check features for the image */
4508 ret = rbd_dev_v2_features(rbd_dev);
4512 /* If the image supports layering, get the parent info */
4514 if (rbd_dev->header.features & RBD_FEATURE_LAYERING) {
4515 ret = rbd_dev_v2_parent_info(rbd_dev);
4520 * Don't print a warning for parent images. We can
4521 * tell this point because we won't know its pool
4522 * name yet (just its pool id).
4524 if (rbd_dev->spec->pool_name)
4525 rbd_warn(rbd_dev, "WARNING: kernel layering "
4526 "is EXPERIMENTAL!");
4529 /* If the image supports fancy striping, get its parameters */
4531 if (rbd_dev->header.features & RBD_FEATURE_STRIPINGV2) {
4532 ret = rbd_dev_v2_striping_info(rbd_dev);
4537 /* crypto and compression type aren't (yet) supported for v2 images */
4539 rbd_dev->header.crypt_type = 0;
4540 rbd_dev->header.comp_type = 0;
4542 /* Get the snapshot context, plus the header version */
4544 ret = rbd_dev_v2_snap_context(rbd_dev);
4548 dout("discovered version 2 image, header name is %s\n",
4549 rbd_dev->header_name);
4553 rbd_dev->parent_overlap = 0;
4554 rbd_spec_put(rbd_dev->parent_spec);
4555 rbd_dev->parent_spec = NULL;
4556 kfree(rbd_dev->header_name);
4557 rbd_dev->header_name = NULL;
4558 kfree(rbd_dev->header.object_prefix);
4559 rbd_dev->header.object_prefix = NULL;
4564 static int rbd_dev_probe_parent(struct rbd_device *rbd_dev)
4566 struct rbd_device *parent = NULL;
4567 struct rbd_spec *parent_spec;
4568 struct rbd_client *rbdc;
4571 if (!rbd_dev->parent_spec)
4574 * We need to pass a reference to the client and the parent
4575 * spec when creating the parent rbd_dev. Images related by
4576 * parent/child relationships always share both.
4578 parent_spec = rbd_spec_get(rbd_dev->parent_spec);
4579 rbdc = __rbd_get_client(rbd_dev->rbd_client);
4582 parent = rbd_dev_create(rbdc, parent_spec);
4586 ret = rbd_dev_image_probe(parent);
4589 rbd_dev->parent = parent;
4594 rbd_spec_put(rbd_dev->parent_spec);
4595 kfree(rbd_dev->header_name);
4596 rbd_dev_destroy(parent);
4598 rbd_put_client(rbdc);
4599 rbd_spec_put(parent_spec);
4605 static int rbd_dev_device_setup(struct rbd_device *rbd_dev)
4609 ret = rbd_dev_mapping_set(rbd_dev);
4613 /* generate unique id: find highest unique id, add one */
4614 rbd_dev_id_get(rbd_dev);
4616 /* Fill in the device name, now that we have its id. */
4617 BUILD_BUG_ON(DEV_NAME_LEN
4618 < sizeof (RBD_DRV_NAME) + MAX_INT_FORMAT_WIDTH);
4619 sprintf(rbd_dev->name, "%s%d", RBD_DRV_NAME, rbd_dev->dev_id);
4621 /* Get our block major device number. */
4623 ret = register_blkdev(0, rbd_dev->name);
4626 rbd_dev->major = ret;
4628 /* Set up the blkdev mapping. */
4630 ret = rbd_init_disk(rbd_dev);
4632 goto err_out_blkdev;
4634 ret = rbd_bus_add_dev(rbd_dev);
4638 /* Everything's ready. Announce the disk to the world. */
4640 set_capacity(rbd_dev->disk, rbd_dev->mapping.size / SECTOR_SIZE);
4641 set_bit(RBD_DEV_FLAG_EXISTS, &rbd_dev->flags);
4642 add_disk(rbd_dev->disk);
4644 pr_info("%s: added with size 0x%llx\n", rbd_dev->disk->disk_name,
4645 (unsigned long long) rbd_dev->mapping.size);
4650 rbd_free_disk(rbd_dev);
4652 unregister_blkdev(rbd_dev->major, rbd_dev->name);
4654 rbd_dev_id_put(rbd_dev);
4655 rbd_dev_mapping_clear(rbd_dev);
4660 static int rbd_dev_header_name(struct rbd_device *rbd_dev)
4662 struct rbd_spec *spec = rbd_dev->spec;
4665 /* Record the header object name for this rbd image. */
4667 rbd_assert(rbd_image_format_valid(rbd_dev->image_format));
4669 if (rbd_dev->image_format == 1)
4670 size = strlen(spec->image_name) + sizeof (RBD_SUFFIX);
4672 size = sizeof (RBD_HEADER_PREFIX) + strlen(spec->image_id);
4674 rbd_dev->header_name = kmalloc(size, GFP_KERNEL);
4675 if (!rbd_dev->header_name)
4678 if (rbd_dev->image_format == 1)
4679 sprintf(rbd_dev->header_name, "%s%s",
4680 spec->image_name, RBD_SUFFIX);
4682 sprintf(rbd_dev->header_name, "%s%s",
4683 RBD_HEADER_PREFIX, spec->image_id);
4687 static void rbd_dev_image_release(struct rbd_device *rbd_dev)
4691 rbd_dev_unprobe(rbd_dev);
4692 ret = rbd_dev_header_watch_sync(rbd_dev, 0);
4694 rbd_warn(rbd_dev, "failed to cancel watch event (%d)\n", ret);
4695 kfree(rbd_dev->header_name);
4696 rbd_dev->header_name = NULL;
4697 rbd_dev->image_format = 0;
4698 kfree(rbd_dev->spec->image_id);
4699 rbd_dev->spec->image_id = NULL;
4701 rbd_dev_destroy(rbd_dev);
4705 * Probe for the existence of the header object for the given rbd
4706 * device. For format 2 images this includes determining the image
4709 static int rbd_dev_image_probe(struct rbd_device *rbd_dev)
4715 * Get the id from the image id object. If it's not a
4716 * format 2 image, we'll get ENOENT back, and we'll assume
4717 * it's a format 1 image.
4719 ret = rbd_dev_image_id(rbd_dev);
4722 rbd_assert(rbd_dev->spec->image_id);
4723 rbd_assert(rbd_image_format_valid(rbd_dev->image_format));
4725 ret = rbd_dev_header_name(rbd_dev);
4727 goto err_out_format;
4729 ret = rbd_dev_header_watch_sync(rbd_dev, 1);
4731 goto out_header_name;
4733 if (rbd_dev->image_format == 1)
4734 ret = rbd_dev_v1_probe(rbd_dev);
4736 ret = rbd_dev_v2_probe(rbd_dev);
4740 ret = rbd_dev_spec_update(rbd_dev);
4744 ret = rbd_dev_probe_parent(rbd_dev);
4749 rbd_dev_unprobe(rbd_dev);
4751 tmp = rbd_dev_header_watch_sync(rbd_dev, 0);
4753 rbd_warn(rbd_dev, "unable to tear down watch request\n");
4755 kfree(rbd_dev->header_name);
4756 rbd_dev->header_name = NULL;
4758 rbd_dev->image_format = 0;
4759 kfree(rbd_dev->spec->image_id);
4760 rbd_dev->spec->image_id = NULL;
4762 dout("probe failed, returning %d\n", ret);
4767 static ssize_t rbd_add(struct bus_type *bus,
4771 struct rbd_device *rbd_dev = NULL;
4772 struct ceph_options *ceph_opts = NULL;
4773 struct rbd_options *rbd_opts = NULL;
4774 struct rbd_spec *spec = NULL;
4775 struct rbd_client *rbdc;
4776 struct ceph_osd_client *osdc;
4779 if (!try_module_get(THIS_MODULE))
4782 /* parse add command */
4783 rc = rbd_add_parse_args(buf, &ceph_opts, &rbd_opts, &spec);
4785 goto err_out_module;
4787 rbdc = rbd_get_client(ceph_opts);
4792 ceph_opts = NULL; /* rbd_dev client now owns this */
4795 osdc = &rbdc->client->osdc;
4796 rc = ceph_pg_poolid_by_name(osdc->osdmap, spec->pool_name);
4798 goto err_out_client;
4799 spec->pool_id = (u64)rc;
4801 /* The ceph file layout needs to fit pool id in 32 bits */
4803 if (spec->pool_id > (u64)U32_MAX) {
4804 rbd_warn(NULL, "pool id too large (%llu > %u)\n",
4805 (unsigned long long)spec->pool_id, U32_MAX);
4807 goto err_out_client;
4810 rbd_dev = rbd_dev_create(rbdc, spec);
4812 goto err_out_client;
4813 rbdc = NULL; /* rbd_dev now owns this */
4814 spec = NULL; /* rbd_dev now owns this */
4816 rbd_dev->mapping.read_only = rbd_opts->read_only;
4818 rbd_opts = NULL; /* done with this */
4820 rc = rbd_dev_image_probe(rbd_dev);
4822 goto err_out_rbd_dev;
4824 rc = rbd_dev_device_setup(rbd_dev);
4828 rbd_dev_image_release(rbd_dev);
4830 rbd_dev_destroy(rbd_dev);
4832 rbd_put_client(rbdc);
4835 ceph_destroy_options(ceph_opts);
4839 module_put(THIS_MODULE);
4841 dout("Error adding device %s\n", buf);
4846 static struct rbd_device *__rbd_get_dev(unsigned long dev_id)
4848 struct list_head *tmp;
4849 struct rbd_device *rbd_dev;
4851 spin_lock(&rbd_dev_list_lock);
4852 list_for_each(tmp, &rbd_dev_list) {
4853 rbd_dev = list_entry(tmp, struct rbd_device, node);
4854 if (rbd_dev->dev_id == dev_id) {
4855 spin_unlock(&rbd_dev_list_lock);
4859 spin_unlock(&rbd_dev_list_lock);
4863 static void rbd_dev_device_release(struct device *dev)
4865 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
4867 rbd_free_disk(rbd_dev);
4868 clear_bit(RBD_DEV_FLAG_EXISTS, &rbd_dev->flags);
4869 rbd_dev_clear_mapping(rbd_dev);
4870 unregister_blkdev(rbd_dev->major, rbd_dev->name);
4872 rbd_dev_id_put(rbd_dev);
4873 rbd_dev_mapping_clear(rbd_dev);
4876 static void rbd_dev_remove_parent(struct rbd_device *rbd_dev)
4878 while (rbd_dev->parent) {
4879 struct rbd_device *first = rbd_dev;
4880 struct rbd_device *second = first->parent;
4881 struct rbd_device *third;
4884 * Follow to the parent with no grandparent and
4887 while (second && (third = second->parent)) {
4892 rbd_dev_image_release(second);
4893 first->parent = NULL;
4894 first->parent_overlap = 0;
4896 rbd_assert(first->parent_spec);
4897 rbd_spec_put(first->parent_spec);
4898 first->parent_spec = NULL;
4902 static ssize_t rbd_remove(struct bus_type *bus,
4906 struct rbd_device *rbd_dev = NULL;
4911 ret = strict_strtoul(buf, 10, &ul);
4915 /* convert to int; abort if we lost anything in the conversion */
4916 target_id = (int) ul;
4917 if (target_id != ul)
4920 mutex_lock_nested(&ctl_mutex, SINGLE_DEPTH_NESTING);
4922 rbd_dev = __rbd_get_dev(target_id);
4928 spin_lock_irq(&rbd_dev->lock);
4929 if (rbd_dev->open_count)
4932 set_bit(RBD_DEV_FLAG_REMOVING, &rbd_dev->flags);
4933 spin_unlock_irq(&rbd_dev->lock);
4937 rbd_bus_del_dev(rbd_dev);
4938 rbd_dev_image_release(rbd_dev);
4939 module_put(THIS_MODULE);
4941 mutex_unlock(&ctl_mutex);
4947 * create control files in sysfs
4950 static int rbd_sysfs_init(void)
4954 ret = device_register(&rbd_root_dev);
4958 ret = bus_register(&rbd_bus_type);
4960 device_unregister(&rbd_root_dev);
4965 static void rbd_sysfs_cleanup(void)
4967 bus_unregister(&rbd_bus_type);
4968 device_unregister(&rbd_root_dev);
4971 static int __init rbd_init(void)
4975 if (!libceph_compatible(NULL)) {
4976 rbd_warn(NULL, "libceph incompatibility (quitting)");
4980 rc = rbd_sysfs_init();
4983 pr_info("loaded " RBD_DRV_NAME_LONG "\n");
4987 static void __exit rbd_exit(void)
4989 rbd_sysfs_cleanup();
4992 module_init(rbd_init);
4993 module_exit(rbd_exit);
4995 MODULE_AUTHOR("Sage Weil <sage@newdream.net>");
4996 MODULE_AUTHOR("Yehuda Sadeh <yehuda@hq.newdream.net>");
4997 MODULE_DESCRIPTION("rados block device");
4999 /* following authorship retained from original osdblk.c */
5000 MODULE_AUTHOR("Jeff Garzik <jeff@garzik.org>");
5002 MODULE_LICENSE("GPL");