/* rbd.c -- Export ceph rados objects as a Linux block device based on drivers/block/osdblk.c: Copyright 2009 Red Hat, Inc. This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; see the file COPYING. If not, write to the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. For usage instructions, please refer to: Documentation/ABI/testing/sysfs-bus-rbd */ #include <linux/ceph/libceph.h>#include <linux/ceph/osd_client.h>#include <linux/ceph/mon_client.h>#include <linux/ceph/decode.h>#include <linux/parser.h>#include <linux/bsearch.h> #include <linux/kernel.h>#include <linux/device.h>#include <linux/module.h>#include <linux/fs.h>#include <linux/blkdev.h>#include <linux/slab.h> #include "rbd_types.h" #define RBD_DEBUG /* Activate rbd_assert() calls */ /* * The basic unit of block I/O is a sector. It is interpreted in a * number of contexts in Linux (blk, bio, genhd), but the default is * universally 512 bytes. These symbols are just slightly more * meaningful than the bare numbers they represent. */#define SECTOR_SHIFT 9#define SECTOR_SIZE (1ULL << SECTOR_SHIFT) /* * Increment the given counter and return its updated value. * If the counter is already 0 it will not be incremented. * If the counter is already at its maximum value returns * -EINVAL without updating it. */static int atomic_inc_return_safe(atomic_t *v){ unsigned int counter; counter = (unsigned int)__atomic_add_unless(v, 1, 0); if (counter <= (unsigned int)INT_MAX) return (int)counter; atomic_dec(v); return -EINVAL;} /* Decrement the counter. Return the resulting value, or -EINVAL */static int atomic_dec_return_safe(atomic_t *v){ int counter; counter = atomic_dec_return(v); if (counter >= 0) return counter; atomic_inc(v); return -EINVAL;} #define RBD_DRV_NAME "rbd"#define RBD_DRV_NAME_LONG "rbd (rados block device)" #define RBD_MINORS_PER_MAJOR 256 /* max minors per blkdev */ #define RBD_SNAP_DEV_NAME_PREFIX "snap_"#define RBD_MAX_SNAP_NAME_LEN \ (NAME_MAX - (sizeof (RBD_SNAP_DEV_NAME_PREFIX) - 1)) #define RBD_MAX_SNAP_COUNT 510 /* allows max snapc to fit in 4KB */ #define RBD_SNAP_HEAD_NAME "-" #define BAD_SNAP_INDEX U32_MAX /* invalid index into snap array */ /* This allows a single page to hold an image name sent by OSD */#define RBD_IMAGE_NAME_LEN_MAX (PAGE_SIZE - sizeof (__le32) - 1)#define RBD_IMAGE_ID_LEN_MAX 64 #define RBD_OBJ_PREFIX_LEN_MAX 64 /* Feature bits */ #define RBD_FEATURE_LAYERING (1<<0)#define RBD_FEATURE_STRIPINGV2 (1<<1)#define RBD_FEATURES_ALL \ (RBD_FEATURE_LAYERING | RBD_FEATURE_STRIPINGV2) /* Features supported by this (client software) implementation. */ #define RBD_FEATURES_SUPPORTED (RBD_FEATURES_ALL) /* * An RBD device name will be "rbd#", where the "rbd" comes from * RBD_DRV_NAME above, and # is a unique integer identifier. * MAX_INT_FORMAT_WIDTH is used in ensuring DEV_NAME_LEN is big * enough to hold all possible device names. */#define DEV_NAME_LEN 32#define MAX_INT_FORMAT_WIDTH ((5 * sizeof (int)) / 2 + 1) /* * block device image metadata (in-memory version) */struct rbd_image_header { /* These six fields never change for a given rbd image */ char *object_prefix; __u8 obj_order; __u8 crypt_type; __u8 comp_type; u64 stripe_unit; u64 stripe_count; u64 features; /* Might be changeable someday? */ /* The remaining fields need to be updated occasionally */ u64 image_size; struct ceph_snap_context *snapc; char *snap_names; /* format 1 only */ u64 *snap_sizes; /* format 1 only */}; /* * An rbd image specification. * * The tuple (pool_id, image_id, snap_id) is sufficient to uniquely * identify an image. Each rbd_dev structure includes a pointer to * an rbd_spec structure that encapsulates this identity. * * Each of the id's in an rbd_spec has an associated name. For a * user-mapped image, the names are supplied and the id's associated * with them are looked up. For a layered image, a parent image is * defined by the tuple, and the names are looked up. * * An rbd_dev structure contains a parent_spec pointer which is * non-null if the image it represents is a child in a layered * image. This pointer will refer to the rbd_spec structure used * by the parent rbd_dev for its own identity (i.e., the structure * is shared between the parent and child). * * Since these structures are populated once, during the discovery * phase of image construction, they are effectively immutable so * we make no effort to synchronize access to them. * * Note that code herein does not assume the image name is known (it * could be a null pointer). */struct rbd_spec { u64 pool_id; const char *pool_name; const char *image_id; const char *image_name; u64 snap_id; const char *snap_name; struct kref kref;}; /* * an instance of the client. multiple devices may share an rbd client. */struct rbd_client { struct ceph_client *client; struct kref kref; struct list_head node;}; struct rbd_img_request;typedef void (*rbd_img_callback_t)(struct rbd_img_request *); #define BAD_WHICH U32_MAX /* Good which or bad which, which? */ struct rbd_obj_request;typedef void (*rbd_obj_callback_t)(struct rbd_obj_request *); enum obj_request_type { OBJ_REQUEST_NODATA, OBJ_REQUEST_BIO, OBJ_REQUEST_PAGES}; enum obj_req_flags { OBJ_REQ_DONE, /* completion flag: not done = 0, done = 1 */ OBJ_REQ_IMG_DATA, /* object usage: standalone = 0, image = 1 */ OBJ_REQ_KNOWN, /* EXISTS flag valid: no = 0, yes = 1 */ OBJ_REQ_EXISTS, /* target exists: no = 0, yes = 1 */}; struct rbd_obj_request { const char *object_name; u64 offset; /* object start byte */ u64 length; /* bytes from offset */ unsigned long flags; /* * An object request associated with an image will have its * img_data flag set; a standalone object request will not. * * A standalone object request will have which == BAD_WHICH * and a null obj_request pointer. * * An object request initiated in support of a layered image * object (to check for its existence before a write) will * have which == BAD_WHICH and a non-null obj_request pointer. * * Finally, an object request for rbd image data will have * which != BAD_WHICH, and will have a non-null img_request * pointer. The value of which will be in the range * 0..(img_request->obj_request_count-1). */ union { struct rbd_obj_request *obj_request; /* STAT op */ struct { struct rbd_img_request *img_request; u64 img_offset; /* links for img_request->obj_requests list */ struct list_head links; }; }; u32 which; /* posn image request list */ enum obj_request_type type; union { struct bio *bio_list; struct { struct page **pages; u32 page_count; }; }; struct page **copyup_pages; u32 copyup_page_count; struct ceph_osd_request *osd_req; u64 xferred; /* bytes transferred */ int result; rbd_obj_callback_t callback; struct completion completion; struct kref kref;}; enum img_req_flags { IMG_REQ_WRITE, /* I/O direction: read = 0, write = 1 */ IMG_REQ_CHILD, /* initiator: block = 0, child image = 1 */ IMG_REQ_LAYERED, /* ENOENT handling: normal = 0, layered = 1 */}; struct rbd_img_request { struct rbd_device *rbd_dev; u64 offset; /* starting image byte offset */ u64 length; /* byte count from offset */ unsigned long flags; union { u64 snap_id; /* for reads */ struct ceph_snap_context *snapc; /* for writes */ }; union { struct request *rq; /* block request */ struct rbd_obj_request *obj_request; /* obj req initiator */ }; struct page **copyup_pages; u32 copyup_page_count; spinlock_t completion_lock;/* protects next_completion */ u32 next_completion; rbd_img_callback_t callback; u64 xferred;/* aggregate bytes transferred */ int result; /* first nonzero obj_request result */ u32 obj_request_count; struct list_head obj_requests; /* rbd_obj_request structs */ struct kref kref;}; #define for_each_obj_request(ireq, oreq) \ list_for_each_entry(oreq, &(ireq)->obj_requests, links)#define for_each_obj_request_from(ireq, oreq) \ list_for_each_entry_from(oreq, &(ireq)->obj_requests, links)#define for_each_obj_request_safe(ireq, oreq, n) \ list_for_each_entry_safe_reverse(oreq, n, &(ireq)->obj_requests, links) struct rbd_mapping { u64 size; u64 features; bool read_only;}; /* * a single device */struct rbd_device { int dev_id; /* blkdev unique id */ int major; /* blkdev assigned major */ struct gendisk *disk; /* blkdev's gendisk and rq */ u32 image_format; /* Either 1 or 2 */ struct rbd_client *rbd_client; char name[DEV_NAME_LEN]; /* blkdev name, e.g. rbd3 */ spinlock_t lock; /* queue, flags, open_count */ struct rbd_image_header header; unsigned long flags; /* possibly lock protected */ struct rbd_spec *spec; char *header_name; struct ceph_file_layout layout; struct ceph_osd_event *watch_event; struct rbd_obj_request *watch_request; struct rbd_spec *parent_spec; u64 parent_overlap; atomic_t parent_ref; struct rbd_device *parent; /* protects updating the header */ struct rw_semaphore header_rwsem; struct rbd_mapping mapping; struct list_head node; /* sysfs related */ struct device dev; unsigned long open_count; /* protected by lock */}; /* * Flag bits for rbd_dev->flags. If atomicity is required, * rbd_dev->lock is used to protect access. * * Currently, only the "removing" flag (which is coupled with the * "open_count" field) requires atomic access. */enum rbd_dev_flags { RBD_DEV_FLAG_EXISTS, /* mapped snapshot has not been deleted */ RBD_DEV_FLAG_REMOVING, /* this mapping is being removed */}; static DEFINE_MUTEX(client_mutex); /* Serialize client creation */ static LIST_HEAD(rbd_dev_list); /* devices */static DEFINE_SPINLOCK(rbd_dev_list_lock); static LIST_HEAD(rbd_client_list); /* clients */static DEFINE_SPINLOCK(rbd_client_list_lock); /* Slab caches for frequently-allocated structures */ static struct kmem_cache *rbd_img_request_cache;static struct kmem_cache *rbd_obj_request_cache;static struct kmem_cache *rbd_segment_name_cache; static int rbd_img_request_submit(struct rbd_img_request *img_request); static void rbd_dev_device_release(struct device *dev); static ssize_t rbd_add(struct bus_type *bus, const char *buf, size_t count);static ssize_t rbd_remove(struct bus_type *bus, const char *buf, size_t count);static int rbd_dev_image_probe(struct rbd_device *rbd_dev, bool mapping);static void rbd_spec_put(struct rbd_spec *spec); static BUS_ATTR(add, S_IWUSR, NULL, rbd_add);static BUS_ATTR(remove, S_IWUSR, NULL, rbd_remove); static struct attribute *rbd_bus_attrs[] = { &bus_attr_add.attr, &bus_attr_remove.attr, NULL,};ATTRIBUTE_GROUPS(rbd_bus); static struct bus_type rbd_bus_type = { .name = "rbd", .bus_groups = rbd_bus_groups,}; static void rbd_root_dev_release(struct device *dev){} static struct device rbd_root_dev = { .init_name = "rbd", .release = rbd_root_dev_release,}; static __printf(2, 3)void rbd_warn(struct rbd_device *rbd_dev, const char *fmt, ...){ struct va_format vaf; va_list args; va_start(args, fmt); vaf.fmt = fmt; vaf.va = &args; if (!rbd_dev) printk(KERN_WARNING "%s: %pV\n", RBD_DRV_NAME, &vaf); else if (rbd_dev->disk) printk(KERN_WARNING "%s: %s: %pV\n", RBD_DRV_NAME, rbd_dev->disk->disk_name, &vaf); else if (rbd_dev->spec && rbd_dev->spec->image_name) printk(KERN_WARNING "%s: image %s: %pV\n", RBD_DRV_NAME, rbd_dev->spec->image_name, &vaf); else if (rbd_dev->spec && rbd_dev->spec->image_id) printk(KERN_WARNING "%s: id %s: %pV\n", RBD_DRV_NAME, rbd_dev->spec->image_id, &vaf); else /* punt */ printk(KERN_WARNING "%s: rbd_dev %p: %pV\n", RBD_DRV_NAME, rbd_dev, &vaf); va_end(args);} #ifdef RBD_DEBUG#define rbd_assert(expr) \ if (unlikely(!(expr))) { \ printk(KERN_ERR "\nAssertion failure in %s() " \ "at line %d:\n\n" \ "\trbd_assert(%s);\n\n", \ __func__, __LINE__, #expr); \ BUG(); \ }#else /* !RBD_DEBUG */# define rbd_assert(expr) ((void) 0)#endif /* !RBD_DEBUG */ static int rbd_img_obj_request_submit(struct rbd_obj_request *obj_request);static void rbd_img_parent_read(struct rbd_obj_request *obj_request);static void rbd_dev_remove_parent(struct rbd_device *rbd_dev); static int rbd_dev_refresh(struct rbd_device *rbd_dev);static int rbd_dev_v2_header_onetime(struct rbd_device *rbd_dev);static int rbd_dev_v2_header_info(struct rbd_device *rbd_dev);static const char *rbd_dev_v2_snap_name(struct rbd_device *rbd_dev, u64 snap_id);static int _rbd_dev_v2_snap_size(struct rbd_device *rbd_dev, u64 snap_id, u8 *order, u64 *snap_size);static int _rbd_dev_v2_snap_features(struct rbd_device *rbd_dev, u64 snap_id, u64 *snap_features);static u64 rbd_snap_id_by_name(struct rbd_device *rbd_dev, const char *name); static int rbd_open(struct block_device *bdev, fmode_t mode){ struct rbd_device *rbd_dev = bdev->bd_disk->private_data; bool removing = false; if ((mode & FMODE_WRITE) && rbd_dev->mapping.read_only) return -EROFS; spin_lock_irq(&rbd_dev->lock); if (test_bit(RBD_DEV_FLAG_REMOVING, &rbd_dev->flags)) removing = true; else rbd_dev->open_count++; spin_unlock_irq(&rbd_dev->lock); if (removing) return -ENOENT; (void) get_device(&rbd_dev->dev); set_device_ro(bdev, rbd_dev->mapping.read_only); return 0;}Lines 1–500 of 5330. Load more
/* rbd.c -- Export ceph rados objects as a Linux block device based on drivers/block/osdblk.c: Copyright 2009 Red Hat, Inc. This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; see the file COPYING. If not, write to the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. For usage instructions, please refer to: Documentation/ABI/testing/sysfs-bus-rbd */ #include <linux/ceph/libceph.h> #include <linux/ceph/osd_client.h> #include <linux/ceph/mon_client.h> #include <linux/ceph/decode.h> #include <linux/parser.h> #include <linux/bsearch.h> #include <linux/kernel.h> #include <linux/device.h> #include <linux/module.h> #include <linux/fs.h> #include <linux/blkdev.h> #include <linux/slab.h> #include "rbd_types.h" #define RBD_DEBUG /* Activate rbd_assert() calls */ /* * The basic unit of block I/O is a sector. It is interpreted in a * number of contexts in Linux (blk, bio, genhd), but the default is * universally 512 bytes. These symbols are just slightly more * meaningful than the bare numbers they represent. */ #define SECTOR_SHIFT 9 #define SECTOR_SIZE (1ULL << SECTOR_SHIFT) /* * Increment the given counter and return its updated value. * If the counter is already 0 it will not be incremented. * If the counter is already at its maximum value returns * -EINVAL without updating it. */ static int atomic_inc_return_safe(atomic_t *v) { unsigned int counter; counter = (unsigned int)__atomic_add_unless(v, 1, 0); if (counter <= (unsigned int)INT_MAX) return (int)counter; atomic_dec(v); return -EINVAL; } /* Decrement the counter. Return the resulting value, or -EINVAL */ static int atomic_dec_return_safe(atomic_t *v) { int counter; counter = atomic_dec_return(v); if (counter >= 0) return counter; atomic_inc(v); return -EINVAL; } #define RBD_DRV_NAME "rbd" #define RBD_DRV_NAME_LONG "rbd (rados block device)" #define RBD_MINORS_PER_MAJOR 256 /* max minors per blkdev */ #define RBD_SNAP_DEV_NAME_PREFIX "snap_" #define RBD_MAX_SNAP_NAME_LEN \ (NAME_MAX - (sizeof (RBD_SNAP_DEV_NAME_PREFIX) - 1)) #define RBD_MAX_SNAP_COUNT 510 /* allows max snapc to fit in 4KB */ #define RBD_SNAP_HEAD_NAME "-" #define BAD_SNAP_INDEX U32_MAX /* invalid index into snap array */ /* This allows a single page to hold an image name sent by OSD */ #define RBD_IMAGE_NAME_LEN_MAX (PAGE_SIZE - sizeof (__le32) - 1) #define RBD_IMAGE_ID_LEN_MAX 64 #define RBD_OBJ_PREFIX_LEN_MAX 64 /* Feature bits */ #define RBD_FEATURE_LAYERING (1<<0) #define RBD_FEATURE_STRIPINGV2 (1<<1) #define RBD_FEATURES_ALL \ (RBD_FEATURE_LAYERING | RBD_FEATURE_STRIPINGV2) /* Features supported by this (client software) implementation. */ #define RBD_FEATURES_SUPPORTED (RBD_FEATURES_ALL) /* * An RBD device name will be "rbd#", where the "rbd" comes from * RBD_DRV_NAME above, and # is a unique integer identifier. * MAX_INT_FORMAT_WIDTH is used in ensuring DEV_NAME_LEN is big * enough to hold all possible device names. */ #define DEV_NAME_LEN 32 #define MAX_INT_FORMAT_WIDTH ((5 * sizeof (int)) / 2 + 1) /* * block device image metadata (in-memory version) */ struct rbd_image_header { /* These six fields never change for a given rbd image */ char *object_prefix; __u8 obj_order; __u8 crypt_type; __u8 comp_type; u64 stripe_unit; u64 stripe_count; u64 features; /* Might be changeable someday? */ /* The remaining fields need to be updated occasionally */ u64 image_size; struct ceph_snap_context *snapc; char *snap_names; /* format 1 only */ u64 *snap_sizes; /* format 1 only */ }; /* * An rbd image specification. * * The tuple (pool_id, image_id, snap_id) is sufficient to uniquely * identify an image. Each rbd_dev structure includes a pointer to * an rbd_spec structure that encapsulates this identity. * * Each of the id's in an rbd_spec has an associated name. For a * user-mapped image, the names are supplied and the id's associated * with them are looked up. For a layered image, a parent image is * defined by the tuple, and the names are looked up. * * An rbd_dev structure contains a parent_spec pointer which is * non-null if the image it represents is a child in a layered * image. This pointer will refer to the rbd_spec structure used * by the parent rbd_dev for its own identity (i.e., the structure * is shared between the parent and child). * * Since these structures are populated once, during the discovery * phase of image construction, they are effectively immutable so * we make no effort to synchronize access to them. * * Note that code herein does not assume the image name is known (it * could be a null pointer). */ struct rbd_spec { u64 pool_id; const char *pool_name; const char *image_id; const char *image_name; u64 snap_id; const char *snap_name; struct kref kref; }; /* * an instance of the client. multiple devices may share an rbd client. */ struct rbd_client { struct ceph_client *client; struct kref kref; struct list_head node; }; struct rbd_img_request; typedef void (*rbd_img_callback_t)(struct rbd_img_request *); #define BAD_WHICH U32_MAX /* Good which or bad which, which? */ struct rbd_obj_request; typedef void (*rbd_obj_callback_t)(struct rbd_obj_request *); enum obj_request_type { OBJ_REQUEST_NODATA, OBJ_REQUEST_BIO, OBJ_REQUEST_PAGES }; enum obj_req_flags { OBJ_REQ_DONE, /* completion flag: not done = 0, done = 1 */ OBJ_REQ_IMG_DATA, /* object usage: standalone = 0, image = 1 */ OBJ_REQ_KNOWN, /* EXISTS flag valid: no = 0, yes = 1 */ OBJ_REQ_EXISTS, /* target exists: no = 0, yes = 1 */ }; struct rbd_obj_request { const char *object_name; u64 offset; /* object start byte */ u64 length; /* bytes from offset */ unsigned long flags; /* * An object request associated with an image will have its * img_data flag set; a standalone object request will not. * * A standalone object request will have which == BAD_WHICH * and a null obj_request pointer. * * An object request initiated in support of a layered image * object (to check for its existence before a write) will * have which == BAD_WHICH and a non-null obj_request pointer. * * Finally, an object request for rbd image data will have * which != BAD_WHICH, and will have a non-null img_request * pointer. The value of which will be in the range * 0..(img_request->obj_request_count-1). */ union { struct rbd_obj_request *obj_request; /* STAT op */ struct { struct rbd_img_request *img_request; u64 img_offset; /* links for img_request->obj_requests list */ struct list_head links; }; }; u32 which; /* posn image request list */ enum obj_request_type type; union { struct bio *bio_list; struct { struct page **pages; u32 page_count; }; }; struct page **copyup_pages; u32 copyup_page_count; struct ceph_osd_request *osd_req; u64 xferred; /* bytes transferred */ int result; rbd_obj_callback_t callback; struct completion completion; struct kref kref; }; enum img_req_flags { IMG_REQ_WRITE, /* I/O direction: read = 0, write = 1 */ IMG_REQ_CHILD, /* initiator: block = 0, child image = 1 */ IMG_REQ_LAYERED, /* ENOENT handling: normal = 0, layered = 1 */ }; struct rbd_img_request { struct rbd_device *rbd_dev; u64 offset; /* starting image byte offset */ u64 length; /* byte count from offset */ unsigned long flags; union { u64 snap_id; /* for reads */ struct ceph_snap_context *snapc; /* for writes */ }; union { struct request *rq; /* block request */ struct rbd_obj_request *obj_request; /* obj req initiator */ }; struct page **copyup_pages; u32 copyup_page_count; spinlock_t completion_lock;/* protects next_completion */ u32 next_completion; rbd_img_callback_t callback; u64 xferred;/* aggregate bytes transferred */ int result; /* first nonzero obj_request result */ u32 obj_request_count; struct list_head obj_requests; /* rbd_obj_request structs */ struct kref kref; }; #define for_each_obj_request(ireq, oreq) \ list_for_each_entry(oreq, &(ireq)->obj_requests, links) #define for_each_obj_request_from(ireq, oreq) \ list_for_each_entry_from(oreq, &(ireq)->obj_requests, links) #define for_each_obj_request_safe(ireq, oreq, n) \ list_for_each_entry_safe_reverse(oreq, n, &(ireq)->obj_requests, links) struct rbd_mapping { u64 size; u64 features; bool read_only; }; /* * a single device */ struct rbd_device { int dev_id; /* blkdev unique id */ int major; /* blkdev assigned major */ struct gendisk *disk; /* blkdev's gendisk and rq */ u32 image_format; /* Either 1 or 2 */ struct rbd_client *rbd_client; char name[DEV_NAME_LEN]; /* blkdev name, e.g. rbd3 */ spinlock_t lock; /* queue, flags, open_count */ struct rbd_image_header header; unsigned long flags; /* possibly lock protected */ struct rbd_spec *spec; char *header_name; struct ceph_file_layout layout; struct ceph_osd_event *watch_event; struct rbd_obj_request *watch_request; struct rbd_spec *parent_spec; u64 parent_overlap; atomic_t parent_ref; struct rbd_device *parent; /* protects updating the header */ struct rw_semaphore header_rwsem; struct rbd_mapping mapping; struct list_head node; /* sysfs related */ struct device dev; unsigned long open_count; /* protected by lock */ }; /* * Flag bits for rbd_dev->flags. If atomicity is required, * rbd_dev->lock is used to protect access. * * Currently, only the "removing" flag (which is coupled with the * "open_count" field) requires atomic access. */ enum rbd_dev_flags { RBD_DEV_FLAG_EXISTS, /* mapped snapshot has not been deleted */ RBD_DEV_FLAG_REMOVING, /* this mapping is being removed */ }; static DEFINE_MUTEX(client_mutex); /* Serialize client creation */ static LIST_HEAD(rbd_dev_list); /* devices */ static DEFINE_SPINLOCK(rbd_dev_list_lock); static LIST_HEAD(rbd_client_list); /* clients */ static DEFINE_SPINLOCK(rbd_client_list_lock); /* Slab caches for frequently-allocated structures */ static struct kmem_cache *rbd_img_request_cache; static struct kmem_cache *rbd_obj_request_cache; static struct kmem_cache *rbd_segment_name_cache; static int rbd_img_request_submit(struct rbd_img_request *img_request); static void rbd_dev_device_release(struct device *dev); static ssize_t rbd_add(struct bus_type *bus, const char *buf, size_t count); static ssize_t rbd_remove(struct bus_type *bus, const char *buf, size_t count); static int rbd_dev_image_probe(struct rbd_device *rbd_dev, bool mapping); static void rbd_spec_put(struct rbd_spec *spec); static BUS_ATTR(add, S_IWUSR, NULL, rbd_add); static BUS_ATTR(remove, S_IWUSR, NULL, rbd_remove); static struct attribute *rbd_bus_attrs[] = { &bus_attr_add.attr, &bus_attr_remove.attr, NULL, }; ATTRIBUTE_GROUPS(rbd_bus); static struct bus_type rbd_bus_type = { .name = "rbd", .bus_groups = rbd_bus_groups, }; static void rbd_root_dev_release(struct device *dev) { } static struct device rbd_root_dev = { .init_name = "rbd", .release = rbd_root_dev_release, }; static __printf(2, 3) void rbd_warn(struct rbd_device *rbd_dev, const char *fmt, ...) { struct va_format vaf; va_list args; va_start(args, fmt); vaf.fmt = fmt; vaf.va = &args; if (!rbd_dev) printk(KERN_WARNING "%s: %pV\n", RBD_DRV_NAME, &vaf); else if (rbd_dev->disk) printk(KERN_WARNING "%s: %s: %pV\n", RBD_DRV_NAME, rbd_dev->disk->disk_name, &vaf); else if (rbd_dev->spec && rbd_dev->spec->image_name) printk(KERN_WARNING "%s: image %s: %pV\n", RBD_DRV_NAME, rbd_dev->spec->image_name, &vaf); else if (rbd_dev->spec && rbd_dev->spec->image_id) printk(KERN_WARNING "%s: id %s: %pV\n", RBD_DRV_NAME, rbd_dev->spec->image_id, &vaf); else /* punt */ printk(KERN_WARNING "%s: rbd_dev %p: %pV\n", RBD_DRV_NAME, rbd_dev, &vaf); va_end(args); } #ifdef RBD_DEBUG #define rbd_assert(expr) \ if (unlikely(!(expr))) { \ printk(KERN_ERR "\nAssertion failure in %s() " \ "at line %d:\n\n" \ "\trbd_assert(%s);\n\n", \ __func__, __LINE__, #expr); \ BUG(); \ } #else /* !RBD_DEBUG */ # define rbd_assert(expr) ((void) 0) #endif /* !RBD_DEBUG */ static int rbd_img_obj_request_submit(struct rbd_obj_request *obj_request); static void rbd_img_parent_read(struct rbd_obj_request *obj_request); static void rbd_dev_remove_parent(struct rbd_device *rbd_dev); static int rbd_dev_refresh(struct rbd_device *rbd_dev); static int rbd_dev_v2_header_onetime(struct rbd_device *rbd_dev); static int rbd_dev_v2_header_info(struct rbd_device *rbd_dev); static const char *rbd_dev_v2_snap_name(struct rbd_device *rbd_dev, u64 snap_id); static int _rbd_dev_v2_snap_size(struct rbd_device *rbd_dev, u64 snap_id, u8 *order, u64 *snap_size); static int _rbd_dev_v2_snap_features(struct rbd_device *rbd_dev, u64 snap_id, u64 *snap_features); static u64 rbd_snap_id_by_name(struct rbd_device *rbd_dev, const char *name); static int rbd_open(struct block_device *bdev, fmode_t mode) { struct rbd_device *rbd_dev = bdev->bd_disk->private_data; bool removing = false; if ((mode & FMODE_WRITE) && rbd_dev->mapping.read_only) return -EROFS; spin_lock_irq(&rbd_dev->lock); if (test_bit(RBD_DEV_FLAG_REMOVING, &rbd_dev->flags)) removing = true; else rbd_dev->open_count++; spin_unlock_irq(&rbd_dev->lock); if (removing) return -ENOENT; (void) get_device(&rbd_dev->dev); set_device_ro(bdev, rbd_dev->mapping.read_only); return 0; }