Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1 | /* |
| 2 | rbd.c -- Export ceph rados objects as a Linux block device |
| 3 | |
| 4 | |
| 5 | based on drivers/block/osdblk.c: |
| 6 | |
| 7 | Copyright 2009 Red Hat, Inc. |
| 8 | |
| 9 | This program is free software; you can redistribute it and/or modify |
| 10 | it under the terms of the GNU General Public License as published by |
| 11 | the Free Software Foundation. |
| 12 | |
| 13 | This program is distributed in the hope that it will be useful, |
| 14 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | GNU General Public License for more details. |
| 17 | |
| 18 | You should have received a copy of the GNU General Public License |
| 19 | along with this program; see the file COPYING. If not, write to |
| 20 | the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. |
| 21 | |
| 22 | |
| 23 | |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 24 | For usage instructions, please refer to: |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 25 | |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 26 | Documentation/ABI/testing/sysfs-bus-rbd |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 27 | |
| 28 | */ |
| 29 | |
| 30 | #include <linux/ceph/libceph.h> |
| 31 | #include <linux/ceph/osd_client.h> |
| 32 | #include <linux/ceph/mon_client.h> |
| 33 | #include <linux/ceph/decode.h> |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 34 | #include <linux/parser.h> |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 35 | |
| 36 | #include <linux/kernel.h> |
| 37 | #include <linux/device.h> |
| 38 | #include <linux/module.h> |
| 39 | #include <linux/fs.h> |
| 40 | #include <linux/blkdev.h> |
| 41 | |
| 42 | #include "rbd_types.h" |
| 43 | |
Alex Elder | 593a9e7 | 2012-02-07 12:03:37 -0600 | [diff] [blame] | 44 | /* |
| 45 | * The basic unit of block I/O is a sector. It is interpreted in a |
| 46 | * number of contexts in Linux (blk, bio, genhd), but the default is |
| 47 | * universally 512 bytes. These symbols are just slightly more |
| 48 | * meaningful than the bare numbers they represent. |
| 49 | */ |
| 50 | #define SECTOR_SHIFT 9 |
| 51 | #define SECTOR_SIZE (1ULL << SECTOR_SHIFT) |
| 52 | |
Alex Elder | df111be | 2012-08-09 10:33:26 -0700 | [diff] [blame] | 53 | /* It might be useful to have this defined elsewhere too */ |
| 54 | |
| 55 | #define U64_MAX ((u64) (~0ULL)) |
| 56 | |
Alex Elder | f0f8cef | 2012-01-29 13:57:44 -0600 | [diff] [blame] | 57 | #define RBD_DRV_NAME "rbd" |
| 58 | #define RBD_DRV_NAME_LONG "rbd (rados block device)" |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 59 | |
| 60 | #define RBD_MINORS_PER_MAJOR 256 /* max minors per blkdev */ |
| 61 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 62 | #define RBD_MAX_SNAP_NAME_LEN 32 |
| 63 | #define RBD_MAX_OPT_LEN 1024 |
| 64 | |
| 65 | #define RBD_SNAP_HEAD_NAME "-" |
| 66 | |
Alex Elder | 81a8979 | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 67 | /* |
| 68 | * An RBD device name will be "rbd#", where the "rbd" comes from |
| 69 | * RBD_DRV_NAME above, and # is a unique integer identifier. |
| 70 | * MAX_INT_FORMAT_WIDTH is used in ensuring DEV_NAME_LEN is big |
| 71 | * enough to hold all possible device names. |
| 72 | */ |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 73 | #define DEV_NAME_LEN 32 |
Alex Elder | 81a8979 | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 74 | #define MAX_INT_FORMAT_WIDTH ((5 * sizeof (int)) / 2 + 1) |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 75 | |
Alex Elder | cc0538b | 2012-08-10 13:12:07 -0700 | [diff] [blame] | 76 | #define RBD_READ_ONLY_DEFAULT false |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 77 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 78 | /* |
| 79 | * block device image metadata (in-memory version) |
| 80 | */ |
| 81 | struct rbd_image_header { |
| 82 | u64 image_size; |
Alex Elder | 849b426 | 2012-07-09 21:04:24 -0500 | [diff] [blame] | 83 | char *object_prefix; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 84 | __u8 obj_order; |
| 85 | __u8 crypt_type; |
| 86 | __u8 comp_type; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 87 | struct ceph_snap_context *snapc; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 88 | u32 total_snaps; |
| 89 | |
| 90 | char *snap_names; |
| 91 | u64 *snap_sizes; |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 92 | |
| 93 | u64 obj_version; |
| 94 | }; |
| 95 | |
| 96 | struct rbd_options { |
Alex Elder | cc0538b | 2012-08-10 13:12:07 -0700 | [diff] [blame] | 97 | bool read_only; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 98 | }; |
| 99 | |
| 100 | /* |
Alex Elder | f0f8cef | 2012-01-29 13:57:44 -0600 | [diff] [blame] | 101 | * an instance of the client. multiple devices may share an rbd client. |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 102 | */ |
| 103 | struct rbd_client { |
| 104 | struct ceph_client *client; |
| 105 | struct kref kref; |
| 106 | struct list_head node; |
| 107 | }; |
| 108 | |
| 109 | /* |
Alex Elder | f0f8cef | 2012-01-29 13:57:44 -0600 | [diff] [blame] | 110 | * a request completion status |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 111 | */ |
Yehuda Sadeh | 1fec709 | 2011-05-13 13:52:56 -0700 | [diff] [blame] | 112 | struct rbd_req_status { |
| 113 | int done; |
| 114 | int rc; |
| 115 | u64 bytes; |
| 116 | }; |
| 117 | |
| 118 | /* |
| 119 | * a collection of requests |
| 120 | */ |
| 121 | struct rbd_req_coll { |
| 122 | int total; |
| 123 | int num_done; |
| 124 | struct kref kref; |
| 125 | struct rbd_req_status status[0]; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 126 | }; |
| 127 | |
Alex Elder | f0f8cef | 2012-01-29 13:57:44 -0600 | [diff] [blame] | 128 | /* |
| 129 | * a single io request |
| 130 | */ |
| 131 | struct rbd_request { |
| 132 | struct request *rq; /* blk layer request */ |
| 133 | struct bio *bio; /* cloned bio */ |
| 134 | struct page **pages; /* list of used pages */ |
| 135 | u64 len; |
| 136 | int coll_index; |
| 137 | struct rbd_req_coll *coll; |
| 138 | }; |
| 139 | |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 140 | struct rbd_snap { |
| 141 | struct device dev; |
| 142 | const char *name; |
Josh Durgin | 3591538 | 2011-12-05 18:25:13 -0800 | [diff] [blame] | 143 | u64 size; |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 144 | struct list_head node; |
| 145 | u64 id; |
| 146 | }; |
| 147 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 148 | /* |
| 149 | * a single device |
| 150 | */ |
| 151 | struct rbd_device { |
Alex Elder | de71a29 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 152 | int dev_id; /* blkdev unique id */ |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 153 | |
| 154 | int major; /* blkdev assigned major */ |
| 155 | struct gendisk *disk; /* blkdev's gendisk and rq */ |
| 156 | struct request_queue *q; |
| 157 | |
Alex Elder | f8c3892 | 2012-08-10 13:12:07 -0700 | [diff] [blame] | 158 | struct rbd_options rbd_opts; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 159 | struct rbd_client *rbd_client; |
| 160 | |
| 161 | char name[DEV_NAME_LEN]; /* blkdev name, e.g. rbd3 */ |
| 162 | |
| 163 | spinlock_t lock; /* queue lock */ |
| 164 | |
| 165 | struct rbd_image_header header; |
Alex Elder | 0bed54d | 2012-07-03 16:01:18 -0500 | [diff] [blame] | 166 | char *image_name; |
| 167 | size_t image_name_len; |
| 168 | char *header_name; |
Alex Elder | d22f76e | 2012-07-12 10:46:35 -0500 | [diff] [blame] | 169 | char *pool_name; |
Alex Elder | 9bb2f33 | 2012-07-12 10:46:35 -0500 | [diff] [blame] | 170 | int pool_id; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 171 | |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 172 | struct ceph_osd_event *watch_event; |
| 173 | struct ceph_osd_request *watch_request; |
| 174 | |
Josh Durgin | c666601 | 2011-11-21 17:11:12 -0800 | [diff] [blame] | 175 | /* protects updating the header */ |
| 176 | struct rw_semaphore header_rwsem; |
Josh Durgin | e88a36e | 2011-11-21 18:14:25 -0800 | [diff] [blame] | 177 | /* name of the snapshot this device reads from */ |
Alex Elder | 820a5f3 | 2012-07-09 21:04:24 -0500 | [diff] [blame] | 178 | char *snap_name; |
Josh Durgin | e88a36e | 2011-11-21 18:14:25 -0800 | [diff] [blame] | 179 | /* id of the snapshot this device reads from */ |
Josh Durgin | 77dfe99 | 2011-11-21 13:04:42 -0800 | [diff] [blame] | 180 | u64 snap_id; /* current snapshot id */ |
Josh Durgin | e88a36e | 2011-11-21 18:14:25 -0800 | [diff] [blame] | 181 | /* whether the snap_id this device reads from still exists */ |
| 182 | bool snap_exists; |
Alex Elder | cc0538b | 2012-08-10 13:12:07 -0700 | [diff] [blame] | 183 | bool read_only; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 184 | |
| 185 | struct list_head node; |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 186 | |
| 187 | /* list of snapshots */ |
| 188 | struct list_head snaps; |
| 189 | |
| 190 | /* sysfs related */ |
| 191 | struct device dev; |
| 192 | }; |
| 193 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 194 | static DEFINE_MUTEX(ctl_mutex); /* Serialize open/close/setup/teardown */ |
Alex Elder | e124a82f | 2012-01-29 13:57:44 -0600 | [diff] [blame] | 195 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 196 | static LIST_HEAD(rbd_dev_list); /* devices */ |
Alex Elder | e124a82f | 2012-01-29 13:57:44 -0600 | [diff] [blame] | 197 | static DEFINE_SPINLOCK(rbd_dev_list_lock); |
| 198 | |
Alex Elder | 432b858 | 2012-01-29 13:57:44 -0600 | [diff] [blame] | 199 | static LIST_HEAD(rbd_client_list); /* clients */ |
| 200 | static DEFINE_SPINLOCK(rbd_client_list_lock); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 201 | |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 202 | static int __rbd_init_snaps_header(struct rbd_device *rbd_dev); |
| 203 | static void rbd_dev_release(struct device *dev); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 204 | static ssize_t rbd_snap_add(struct device *dev, |
| 205 | struct device_attribute *attr, |
| 206 | const char *buf, |
| 207 | size_t count); |
Alex Elder | 14e7085 | 2012-07-19 09:09:27 -0500 | [diff] [blame] | 208 | static void __rbd_remove_snap_dev(struct rbd_snap *snap); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 209 | |
Alex Elder | f0f8cef | 2012-01-29 13:57:44 -0600 | [diff] [blame] | 210 | static ssize_t rbd_add(struct bus_type *bus, const char *buf, |
| 211 | size_t count); |
| 212 | static ssize_t rbd_remove(struct bus_type *bus, const char *buf, |
| 213 | size_t count); |
| 214 | |
| 215 | static struct bus_attribute rbd_bus_attrs[] = { |
| 216 | __ATTR(add, S_IWUSR, NULL, rbd_add), |
| 217 | __ATTR(remove, S_IWUSR, NULL, rbd_remove), |
| 218 | __ATTR_NULL |
| 219 | }; |
| 220 | |
| 221 | static struct bus_type rbd_bus_type = { |
| 222 | .name = "rbd", |
| 223 | .bus_attrs = rbd_bus_attrs, |
| 224 | }; |
| 225 | |
| 226 | static void rbd_root_dev_release(struct device *dev) |
| 227 | { |
| 228 | } |
| 229 | |
| 230 | static struct device rbd_root_dev = { |
| 231 | .init_name = "rbd", |
| 232 | .release = rbd_root_dev_release, |
| 233 | }; |
| 234 | |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 235 | |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 236 | static struct device *rbd_get_dev(struct rbd_device *rbd_dev) |
| 237 | { |
| 238 | return get_device(&rbd_dev->dev); |
| 239 | } |
| 240 | |
| 241 | static void rbd_put_dev(struct rbd_device *rbd_dev) |
| 242 | { |
| 243 | put_device(&rbd_dev->dev); |
| 244 | } |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 245 | |
Alex Elder | 1fe5e99 | 2012-07-25 09:32:41 -0500 | [diff] [blame] | 246 | static int rbd_refresh_header(struct rbd_device *rbd_dev, u64 *hver); |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 247 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 248 | static int rbd_open(struct block_device *bdev, fmode_t mode) |
| 249 | { |
Alex Elder | f0f8cef | 2012-01-29 13:57:44 -0600 | [diff] [blame] | 250 | struct rbd_device *rbd_dev = bdev->bd_disk->private_data; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 251 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 252 | if ((mode & FMODE_WRITE) && rbd_dev->read_only) |
| 253 | return -EROFS; |
| 254 | |
Alex Elder | 340c7a2 | 2012-08-10 13:12:07 -0700 | [diff] [blame] | 255 | rbd_get_dev(rbd_dev); |
| 256 | set_device_ro(bdev, rbd_dev->read_only); |
| 257 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 258 | return 0; |
| 259 | } |
| 260 | |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 261 | static int rbd_release(struct gendisk *disk, fmode_t mode) |
| 262 | { |
| 263 | struct rbd_device *rbd_dev = disk->private_data; |
| 264 | |
| 265 | rbd_put_dev(rbd_dev); |
| 266 | |
| 267 | return 0; |
| 268 | } |
| 269 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 270 | static const struct block_device_operations rbd_bd_ops = { |
| 271 | .owner = THIS_MODULE, |
| 272 | .open = rbd_open, |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 273 | .release = rbd_release, |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 274 | }; |
| 275 | |
| 276 | /* |
| 277 | * Initialize an rbd client instance. |
Alex Elder | 43ae470 | 2012-07-03 16:01:18 -0500 | [diff] [blame] | 278 | * We own *ceph_opts. |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 279 | */ |
Alex Elder | f8c3892 | 2012-08-10 13:12:07 -0700 | [diff] [blame] | 280 | static struct rbd_client *rbd_client_create(struct ceph_options *ceph_opts) |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 281 | { |
| 282 | struct rbd_client *rbdc; |
| 283 | int ret = -ENOMEM; |
| 284 | |
| 285 | dout("rbd_client_create\n"); |
| 286 | rbdc = kmalloc(sizeof(struct rbd_client), GFP_KERNEL); |
| 287 | if (!rbdc) |
| 288 | goto out_opt; |
| 289 | |
| 290 | kref_init(&rbdc->kref); |
| 291 | INIT_LIST_HEAD(&rbdc->node); |
| 292 | |
Alex Elder | bc534d8 | 2012-01-29 13:57:44 -0600 | [diff] [blame] | 293 | mutex_lock_nested(&ctl_mutex, SINGLE_DEPTH_NESTING); |
| 294 | |
Alex Elder | 43ae470 | 2012-07-03 16:01:18 -0500 | [diff] [blame] | 295 | rbdc->client = ceph_create_client(ceph_opts, rbdc, 0, 0); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 296 | if (IS_ERR(rbdc->client)) |
Alex Elder | bc534d8 | 2012-01-29 13:57:44 -0600 | [diff] [blame] | 297 | goto out_mutex; |
Alex Elder | 43ae470 | 2012-07-03 16:01:18 -0500 | [diff] [blame] | 298 | ceph_opts = NULL; /* Now rbdc->client is responsible for ceph_opts */ |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 299 | |
| 300 | ret = ceph_open_session(rbdc->client); |
| 301 | if (ret < 0) |
| 302 | goto out_err; |
| 303 | |
Alex Elder | 432b858 | 2012-01-29 13:57:44 -0600 | [diff] [blame] | 304 | spin_lock(&rbd_client_list_lock); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 305 | list_add_tail(&rbdc->node, &rbd_client_list); |
Alex Elder | 432b858 | 2012-01-29 13:57:44 -0600 | [diff] [blame] | 306 | spin_unlock(&rbd_client_list_lock); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 307 | |
Alex Elder | bc534d8 | 2012-01-29 13:57:44 -0600 | [diff] [blame] | 308 | mutex_unlock(&ctl_mutex); |
| 309 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 310 | dout("rbd_client_create created %p\n", rbdc); |
| 311 | return rbdc; |
| 312 | |
| 313 | out_err: |
| 314 | ceph_destroy_client(rbdc->client); |
Alex Elder | bc534d8 | 2012-01-29 13:57:44 -0600 | [diff] [blame] | 315 | out_mutex: |
| 316 | mutex_unlock(&ctl_mutex); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 317 | kfree(rbdc); |
| 318 | out_opt: |
Alex Elder | 43ae470 | 2012-07-03 16:01:18 -0500 | [diff] [blame] | 319 | if (ceph_opts) |
| 320 | ceph_destroy_options(ceph_opts); |
Vasiliy Kulikov | 28f259b | 2010-09-26 12:59:37 +0400 | [diff] [blame] | 321 | return ERR_PTR(ret); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 322 | } |
| 323 | |
| 324 | /* |
Alex Elder | 1f7ba33 | 2012-08-10 13:12:07 -0700 | [diff] [blame] | 325 | * Find a ceph client with specific addr and configuration. If |
| 326 | * found, bump its reference count. |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 327 | */ |
Alex Elder | 1f7ba33 | 2012-08-10 13:12:07 -0700 | [diff] [blame] | 328 | static struct rbd_client *rbd_client_find(struct ceph_options *ceph_opts) |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 329 | { |
| 330 | struct rbd_client *client_node; |
Alex Elder | 1f7ba33 | 2012-08-10 13:12:07 -0700 | [diff] [blame] | 331 | bool found = false; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 332 | |
Alex Elder | 43ae470 | 2012-07-03 16:01:18 -0500 | [diff] [blame] | 333 | if (ceph_opts->flags & CEPH_OPT_NOSHARE) |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 334 | return NULL; |
| 335 | |
Alex Elder | 1f7ba33 | 2012-08-10 13:12:07 -0700 | [diff] [blame] | 336 | spin_lock(&rbd_client_list_lock); |
| 337 | list_for_each_entry(client_node, &rbd_client_list, node) { |
| 338 | if (!ceph_compare_options(ceph_opts, client_node->client)) { |
| 339 | kref_get(&client_node->kref); |
| 340 | found = true; |
| 341 | break; |
| 342 | } |
| 343 | } |
| 344 | spin_unlock(&rbd_client_list_lock); |
| 345 | |
| 346 | return found ? client_node : NULL; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 347 | } |
| 348 | |
| 349 | /* |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 350 | * mount options |
| 351 | */ |
| 352 | enum { |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 353 | Opt_last_int, |
| 354 | /* int args above */ |
| 355 | Opt_last_string, |
| 356 | /* string args above */ |
Alex Elder | cc0538b | 2012-08-10 13:12:07 -0700 | [diff] [blame] | 357 | Opt_read_only, |
| 358 | Opt_read_write, |
| 359 | /* Boolean args above */ |
| 360 | Opt_last_bool, |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 361 | }; |
| 362 | |
Alex Elder | 43ae470 | 2012-07-03 16:01:18 -0500 | [diff] [blame] | 363 | static match_table_t rbd_opts_tokens = { |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 364 | /* int args above */ |
| 365 | /* string args above */ |
Alex Elder | cc0538b | 2012-08-10 13:12:07 -0700 | [diff] [blame] | 366 | {Opt_read_only, "read_only"}, |
| 367 | {Opt_read_only, "ro"}, /* Alternate spelling */ |
| 368 | {Opt_read_write, "read_write"}, |
| 369 | {Opt_read_write, "rw"}, /* Alternate spelling */ |
| 370 | /* Boolean args above */ |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 371 | {-1, NULL} |
| 372 | }; |
| 373 | |
| 374 | static int parse_rbd_opts_token(char *c, void *private) |
| 375 | { |
Alex Elder | 43ae470 | 2012-07-03 16:01:18 -0500 | [diff] [blame] | 376 | struct rbd_options *rbd_opts = private; |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 377 | substring_t argstr[MAX_OPT_ARGS]; |
| 378 | int token, intval, ret; |
| 379 | |
Alex Elder | 43ae470 | 2012-07-03 16:01:18 -0500 | [diff] [blame] | 380 | token = match_token(c, rbd_opts_tokens, argstr); |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 381 | if (token < 0) |
| 382 | return -EINVAL; |
| 383 | |
| 384 | if (token < Opt_last_int) { |
| 385 | ret = match_int(&argstr[0], &intval); |
| 386 | if (ret < 0) { |
| 387 | pr_err("bad mount option arg (not int) " |
| 388 | "at '%s'\n", c); |
| 389 | return ret; |
| 390 | } |
| 391 | dout("got int token %d val %d\n", token, intval); |
| 392 | } else if (token > Opt_last_int && token < Opt_last_string) { |
| 393 | dout("got string token %d val %s\n", token, |
| 394 | argstr[0].from); |
Alex Elder | cc0538b | 2012-08-10 13:12:07 -0700 | [diff] [blame] | 395 | } else if (token > Opt_last_string && token < Opt_last_bool) { |
| 396 | dout("got Boolean token %d\n", token); |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 397 | } else { |
| 398 | dout("got token %d\n", token); |
| 399 | } |
| 400 | |
| 401 | switch (token) { |
Alex Elder | cc0538b | 2012-08-10 13:12:07 -0700 | [diff] [blame] | 402 | case Opt_read_only: |
| 403 | rbd_opts->read_only = true; |
| 404 | break; |
| 405 | case Opt_read_write: |
| 406 | rbd_opts->read_only = false; |
| 407 | break; |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 408 | default: |
| 409 | BUG_ON(token); |
| 410 | } |
| 411 | return 0; |
| 412 | } |
| 413 | |
| 414 | /* |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 415 | * Get a ceph client with specific addr and configuration, if one does |
| 416 | * not exist create it. |
| 417 | */ |
Alex Elder | f8c3892 | 2012-08-10 13:12:07 -0700 | [diff] [blame] | 418 | static int rbd_get_client(struct rbd_device *rbd_dev, const char *mon_addr, |
| 419 | size_t mon_addr_len, char *options) |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 420 | { |
Alex Elder | f8c3892 | 2012-08-10 13:12:07 -0700 | [diff] [blame] | 421 | struct rbd_options *rbd_opts = &rbd_dev->rbd_opts; |
Alex Elder | 43ae470 | 2012-07-03 16:01:18 -0500 | [diff] [blame] | 422 | struct ceph_options *ceph_opts; |
Alex Elder | f8c3892 | 2012-08-10 13:12:07 -0700 | [diff] [blame] | 423 | struct rbd_client *rbdc; |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 424 | |
Alex Elder | cc0538b | 2012-08-10 13:12:07 -0700 | [diff] [blame] | 425 | rbd_opts->read_only = RBD_READ_ONLY_DEFAULT; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 426 | |
Alex Elder | 43ae470 | 2012-07-03 16:01:18 -0500 | [diff] [blame] | 427 | ceph_opts = ceph_parse_options(options, mon_addr, |
| 428 | mon_addr + mon_addr_len, |
| 429 | parse_rbd_opts_token, rbd_opts); |
Alex Elder | f8c3892 | 2012-08-10 13:12:07 -0700 | [diff] [blame] | 430 | if (IS_ERR(ceph_opts)) |
| 431 | return PTR_ERR(ceph_opts); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 432 | |
Alex Elder | 1f7ba33 | 2012-08-10 13:12:07 -0700 | [diff] [blame] | 433 | rbdc = rbd_client_find(ceph_opts); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 434 | if (rbdc) { |
Alex Elder | e6994d3d | 2012-01-29 13:57:44 -0600 | [diff] [blame] | 435 | /* using an existing client */ |
Alex Elder | 43ae470 | 2012-07-03 16:01:18 -0500 | [diff] [blame] | 436 | ceph_destroy_options(ceph_opts); |
Alex Elder | f8c3892 | 2012-08-10 13:12:07 -0700 | [diff] [blame] | 437 | } else { |
| 438 | rbdc = rbd_client_create(ceph_opts); |
| 439 | if (IS_ERR(rbdc)) |
| 440 | return PTR_ERR(rbdc); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 441 | } |
Alex Elder | f8c3892 | 2012-08-10 13:12:07 -0700 | [diff] [blame] | 442 | rbd_dev->rbd_client = rbdc; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 443 | |
Alex Elder | f8c3892 | 2012-08-10 13:12:07 -0700 | [diff] [blame] | 444 | return 0; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 445 | } |
| 446 | |
| 447 | /* |
| 448 | * Destroy ceph client |
Alex Elder | d23a4b3 | 2012-01-29 13:57:43 -0600 | [diff] [blame] | 449 | * |
Alex Elder | 432b858 | 2012-01-29 13:57:44 -0600 | [diff] [blame] | 450 | * Caller must hold rbd_client_list_lock. |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 451 | */ |
| 452 | static void rbd_client_release(struct kref *kref) |
| 453 | { |
| 454 | struct rbd_client *rbdc = container_of(kref, struct rbd_client, kref); |
| 455 | |
| 456 | dout("rbd_release_client %p\n", rbdc); |
Alex Elder | cd9d9f5 | 2012-04-04 13:35:44 -0500 | [diff] [blame] | 457 | spin_lock(&rbd_client_list_lock); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 458 | list_del(&rbdc->node); |
Alex Elder | cd9d9f5 | 2012-04-04 13:35:44 -0500 | [diff] [blame] | 459 | spin_unlock(&rbd_client_list_lock); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 460 | |
| 461 | ceph_destroy_client(rbdc->client); |
| 462 | kfree(rbdc); |
| 463 | } |
| 464 | |
| 465 | /* |
| 466 | * Drop reference to ceph client node. If it's not referenced anymore, release |
| 467 | * it. |
| 468 | */ |
| 469 | static void rbd_put_client(struct rbd_device *rbd_dev) |
| 470 | { |
| 471 | kref_put(&rbd_dev->rbd_client->kref, rbd_client_release); |
| 472 | rbd_dev->rbd_client = NULL; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 473 | } |
| 474 | |
Yehuda Sadeh | 1fec709 | 2011-05-13 13:52:56 -0700 | [diff] [blame] | 475 | /* |
| 476 | * Destroy requests collection |
| 477 | */ |
| 478 | static void rbd_coll_release(struct kref *kref) |
| 479 | { |
| 480 | struct rbd_req_coll *coll = |
| 481 | container_of(kref, struct rbd_req_coll, kref); |
| 482 | |
| 483 | dout("rbd_coll_release %p\n", coll); |
| 484 | kfree(coll); |
| 485 | } |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 486 | |
Alex Elder | 8e94af8 | 2012-07-25 09:32:40 -0500 | [diff] [blame] | 487 | static bool rbd_dev_ondisk_valid(struct rbd_image_header_ondisk *ondisk) |
| 488 | { |
Alex Elder | 103a150 | 2012-08-02 11:29:45 -0500 | [diff] [blame] | 489 | size_t size; |
| 490 | u32 snap_count; |
| 491 | |
| 492 | /* The header has to start with the magic rbd header text */ |
| 493 | if (memcmp(&ondisk->text, RBD_HEADER_TEXT, sizeof (RBD_HEADER_TEXT))) |
| 494 | return false; |
| 495 | |
| 496 | /* |
| 497 | * The size of a snapshot header has to fit in a size_t, and |
| 498 | * that limits the number of snapshots. |
| 499 | */ |
| 500 | snap_count = le32_to_cpu(ondisk->snap_count); |
| 501 | size = SIZE_MAX - sizeof (struct ceph_snap_context); |
| 502 | if (snap_count > size / sizeof (__le64)) |
| 503 | return false; |
| 504 | |
| 505 | /* |
| 506 | * Not only that, but the size of the entire the snapshot |
| 507 | * header must also be representable in a size_t. |
| 508 | */ |
| 509 | size -= snap_count * sizeof (__le64); |
| 510 | if ((u64) size < le64_to_cpu(ondisk->snap_names_len)) |
| 511 | return false; |
| 512 | |
| 513 | return true; |
Alex Elder | 8e94af8 | 2012-07-25 09:32:40 -0500 | [diff] [blame] | 514 | } |
| 515 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 516 | /* |
| 517 | * Create a new header structure, translate header format from the on-disk |
| 518 | * header. |
| 519 | */ |
| 520 | static int rbd_header_from_disk(struct rbd_image_header *header, |
Alex Elder | 4156d99 | 2012-08-02 11:29:46 -0500 | [diff] [blame] | 521 | struct rbd_image_header_ondisk *ondisk) |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 522 | { |
Alex Elder | ccece23 | 2012-07-10 20:30:10 -0500 | [diff] [blame] | 523 | u32 snap_count; |
Alex Elder | 58c17b0 | 2012-08-23 23:22:06 -0500 | [diff] [blame] | 524 | size_t len; |
Alex Elder | d2bb24e | 2012-07-26 23:37:14 -0500 | [diff] [blame] | 525 | size_t size; |
Alex Elder | 621901d | 2012-08-23 23:22:06 -0500 | [diff] [blame] | 526 | u32 i; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 527 | |
Alex Elder | 6a52325 | 2012-07-19 17:12:59 -0500 | [diff] [blame] | 528 | memset(header, 0, sizeof (*header)); |
| 529 | |
Alex Elder | 103a150 | 2012-08-02 11:29:45 -0500 | [diff] [blame] | 530 | snap_count = le32_to_cpu(ondisk->snap_count); |
| 531 | |
Alex Elder | 58c17b0 | 2012-08-23 23:22:06 -0500 | [diff] [blame] | 532 | len = strnlen(ondisk->object_prefix, sizeof (ondisk->object_prefix)); |
| 533 | header->object_prefix = kmalloc(len + 1, GFP_KERNEL); |
Alex Elder | 6a52325 | 2012-07-19 17:12:59 -0500 | [diff] [blame] | 534 | if (!header->object_prefix) |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 535 | return -ENOMEM; |
Alex Elder | 58c17b0 | 2012-08-23 23:22:06 -0500 | [diff] [blame] | 536 | memcpy(header->object_prefix, ondisk->object_prefix, len); |
| 537 | header->object_prefix[len] = '\0'; |
Alex Elder | 00f1f36 | 2012-02-07 12:03:36 -0600 | [diff] [blame] | 538 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 539 | if (snap_count) { |
Alex Elder | f785cc1 | 2012-08-23 23:22:06 -0500 | [diff] [blame] | 540 | u64 snap_names_len = le64_to_cpu(ondisk->snap_names_len); |
| 541 | |
Alex Elder | 621901d | 2012-08-23 23:22:06 -0500 | [diff] [blame] | 542 | /* Save a copy of the snapshot names */ |
| 543 | |
Alex Elder | f785cc1 | 2012-08-23 23:22:06 -0500 | [diff] [blame] | 544 | if (snap_names_len > (u64) SIZE_MAX) |
| 545 | return -EIO; |
| 546 | header->snap_names = kmalloc(snap_names_len, GFP_KERNEL); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 547 | if (!header->snap_names) |
Alex Elder | 6a52325 | 2012-07-19 17:12:59 -0500 | [diff] [blame] | 548 | goto out_err; |
Alex Elder | f785cc1 | 2012-08-23 23:22:06 -0500 | [diff] [blame] | 549 | /* |
| 550 | * Note that rbd_dev_v1_header_read() guarantees |
| 551 | * the ondisk buffer we're working with has |
| 552 | * snap_names_len bytes beyond the end of the |
| 553 | * snapshot id array, this memcpy() is safe. |
| 554 | */ |
| 555 | memcpy(header->snap_names, &ondisk->snaps[snap_count], |
| 556 | snap_names_len); |
Alex Elder | 6a52325 | 2012-07-19 17:12:59 -0500 | [diff] [blame] | 557 | |
Alex Elder | 621901d | 2012-08-23 23:22:06 -0500 | [diff] [blame] | 558 | /* Record each snapshot's size */ |
| 559 | |
Alex Elder | d2bb24e | 2012-07-26 23:37:14 -0500 | [diff] [blame] | 560 | size = snap_count * sizeof (*header->snap_sizes); |
| 561 | header->snap_sizes = kmalloc(size, GFP_KERNEL); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 562 | if (!header->snap_sizes) |
Alex Elder | 6a52325 | 2012-07-19 17:12:59 -0500 | [diff] [blame] | 563 | goto out_err; |
Alex Elder | 621901d | 2012-08-23 23:22:06 -0500 | [diff] [blame] | 564 | for (i = 0; i < snap_count; i++) |
| 565 | header->snap_sizes[i] = |
| 566 | le64_to_cpu(ondisk->snaps[i].image_size); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 567 | } else { |
Alex Elder | ccece23 | 2012-07-10 20:30:10 -0500 | [diff] [blame] | 568 | WARN_ON(ondisk->snap_names_len); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 569 | header->snap_names = NULL; |
| 570 | header->snap_sizes = NULL; |
| 571 | } |
Alex Elder | 849b426 | 2012-07-09 21:04:24 -0500 | [diff] [blame] | 572 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 573 | header->image_size = le64_to_cpu(ondisk->image_size); |
| 574 | header->obj_order = ondisk->options.order; |
| 575 | header->crypt_type = ondisk->options.crypt_type; |
| 576 | header->comp_type = ondisk->options.comp_type; |
Alex Elder | 6a52325 | 2012-07-19 17:12:59 -0500 | [diff] [blame] | 577 | header->total_snaps = snap_count; |
| 578 | |
Alex Elder | 621901d | 2012-08-23 23:22:06 -0500 | [diff] [blame] | 579 | /* Allocate and fill in the snapshot context */ |
| 580 | |
Alex Elder | 6a52325 | 2012-07-19 17:12:59 -0500 | [diff] [blame] | 581 | size = sizeof (struct ceph_snap_context); |
| 582 | size += snap_count * sizeof (header->snapc->snaps[0]); |
| 583 | header->snapc = kzalloc(size, GFP_KERNEL); |
| 584 | if (!header->snapc) |
| 585 | goto out_err; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 586 | |
| 587 | atomic_set(&header->snapc->nref, 1); |
Alex Elder | 505cbb9 | 2012-07-19 08:49:18 -0500 | [diff] [blame] | 588 | header->snapc->seq = le64_to_cpu(ondisk->snap_seq); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 589 | header->snapc->num_snaps = snap_count; |
Alex Elder | 621901d | 2012-08-23 23:22:06 -0500 | [diff] [blame] | 590 | for (i = 0; i < snap_count; i++) |
| 591 | header->snapc->snaps[i] = |
| 592 | le64_to_cpu(ondisk->snaps[i].id); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 593 | |
| 594 | return 0; |
| 595 | |
Alex Elder | 6a52325 | 2012-07-19 17:12:59 -0500 | [diff] [blame] | 596 | out_err: |
Alex Elder | 849b426 | 2012-07-09 21:04:24 -0500 | [diff] [blame] | 597 | kfree(header->snap_sizes); |
Alex Elder | ccece23 | 2012-07-10 20:30:10 -0500 | [diff] [blame] | 598 | header->snap_sizes = NULL; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 599 | kfree(header->snap_names); |
Alex Elder | ccece23 | 2012-07-10 20:30:10 -0500 | [diff] [blame] | 600 | header->snap_names = NULL; |
Alex Elder | 6a52325 | 2012-07-19 17:12:59 -0500 | [diff] [blame] | 601 | kfree(header->object_prefix); |
| 602 | header->object_prefix = NULL; |
Alex Elder | ccece23 | 2012-07-10 20:30:10 -0500 | [diff] [blame] | 603 | |
Alex Elder | 00f1f36 | 2012-02-07 12:03:36 -0600 | [diff] [blame] | 604 | return -ENOMEM; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 605 | } |
| 606 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 607 | static int snap_by_name(struct rbd_image_header *header, const char *snap_name, |
| 608 | u64 *seq, u64 *size) |
| 609 | { |
| 610 | int i; |
| 611 | char *p = header->snap_names; |
| 612 | |
Alex Elder | 00f1f36 | 2012-02-07 12:03:36 -0600 | [diff] [blame] | 613 | for (i = 0; i < header->total_snaps; i++) { |
| 614 | if (!strcmp(snap_name, p)) { |
| 615 | |
| 616 | /* Found it. Pass back its id and/or size */ |
| 617 | |
| 618 | if (seq) |
| 619 | *seq = header->snapc->snaps[i]; |
| 620 | if (size) |
| 621 | *size = header->snap_sizes[i]; |
| 622 | return i; |
| 623 | } |
| 624 | p += strlen(p) + 1; /* Skip ahead to the next name */ |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 625 | } |
Alex Elder | 00f1f36 | 2012-02-07 12:03:36 -0600 | [diff] [blame] | 626 | return -ENOENT; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 627 | } |
| 628 | |
Alex Elder | 0ce1a79 | 2012-07-03 16:01:18 -0500 | [diff] [blame] | 629 | static int rbd_header_set_snap(struct rbd_device *rbd_dev, u64 *size) |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 630 | { |
Alex Elder | 78dc447 | 2012-07-19 08:49:18 -0500 | [diff] [blame] | 631 | int ret; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 632 | |
Alex Elder | 0ce1a79 | 2012-07-03 16:01:18 -0500 | [diff] [blame] | 633 | down_write(&rbd_dev->header_rwsem); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 634 | |
Alex Elder | 0ce1a79 | 2012-07-03 16:01:18 -0500 | [diff] [blame] | 635 | if (!memcmp(rbd_dev->snap_name, RBD_SNAP_HEAD_NAME, |
Josh Durgin | cc9d734 | 2011-11-21 18:19:13 -0800 | [diff] [blame] | 636 | sizeof (RBD_SNAP_HEAD_NAME))) { |
Alex Elder | 0ce1a79 | 2012-07-03 16:01:18 -0500 | [diff] [blame] | 637 | rbd_dev->snap_id = CEPH_NOSNAP; |
Josh Durgin | e88a36e | 2011-11-21 18:14:25 -0800 | [diff] [blame] | 638 | rbd_dev->snap_exists = false; |
Alex Elder | cc0538b | 2012-08-10 13:12:07 -0700 | [diff] [blame] | 639 | rbd_dev->read_only = rbd_dev->rbd_opts.read_only; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 640 | if (size) |
Alex Elder | 78dc447 | 2012-07-19 08:49:18 -0500 | [diff] [blame] | 641 | *size = rbd_dev->header.image_size; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 642 | } else { |
Alex Elder | 78dc447 | 2012-07-19 08:49:18 -0500 | [diff] [blame] | 643 | u64 snap_id = 0; |
| 644 | |
| 645 | ret = snap_by_name(&rbd_dev->header, rbd_dev->snap_name, |
| 646 | &snap_id, size); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 647 | if (ret < 0) |
| 648 | goto done; |
Alex Elder | 78dc447 | 2012-07-19 08:49:18 -0500 | [diff] [blame] | 649 | rbd_dev->snap_id = snap_id; |
Josh Durgin | e88a36e | 2011-11-21 18:14:25 -0800 | [diff] [blame] | 650 | rbd_dev->snap_exists = true; |
Alex Elder | cc0538b | 2012-08-10 13:12:07 -0700 | [diff] [blame] | 651 | rbd_dev->read_only = true; /* No choice for snapshots */ |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 652 | } |
| 653 | |
| 654 | ret = 0; |
| 655 | done: |
Alex Elder | 0ce1a79 | 2012-07-03 16:01:18 -0500 | [diff] [blame] | 656 | up_write(&rbd_dev->header_rwsem); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 657 | return ret; |
| 658 | } |
| 659 | |
| 660 | static void rbd_header_free(struct rbd_image_header *header) |
| 661 | { |
Alex Elder | 849b426 | 2012-07-09 21:04:24 -0500 | [diff] [blame] | 662 | kfree(header->object_prefix); |
Alex Elder | d78fd7a | 2012-07-26 23:37:14 -0500 | [diff] [blame] | 663 | header->object_prefix = NULL; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 664 | kfree(header->snap_sizes); |
Alex Elder | d78fd7a | 2012-07-26 23:37:14 -0500 | [diff] [blame] | 665 | header->snap_sizes = NULL; |
Alex Elder | 849b426 | 2012-07-09 21:04:24 -0500 | [diff] [blame] | 666 | kfree(header->snap_names); |
Alex Elder | d78fd7a | 2012-07-26 23:37:14 -0500 | [diff] [blame] | 667 | header->snap_names = NULL; |
Josh Durgin | d1d2564 | 2011-12-05 14:03:05 -0800 | [diff] [blame] | 668 | ceph_put_snap_context(header->snapc); |
Alex Elder | d78fd7a | 2012-07-26 23:37:14 -0500 | [diff] [blame] | 669 | header->snapc = NULL; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 670 | } |
| 671 | |
Alex Elder | 65ccfe2 | 2012-08-09 10:33:26 -0700 | [diff] [blame^] | 672 | static char *rbd_segment_name(struct rbd_device *rbd_dev, u64 offset) |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 673 | { |
Alex Elder | 65ccfe2 | 2012-08-09 10:33:26 -0700 | [diff] [blame^] | 674 | char *name; |
| 675 | u64 segment; |
| 676 | int ret; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 677 | |
Alex Elder | 65ccfe2 | 2012-08-09 10:33:26 -0700 | [diff] [blame^] | 678 | name = kmalloc(RBD_MAX_SEG_NAME_LEN + 1, GFP_NOIO); |
| 679 | if (!name) |
| 680 | return NULL; |
| 681 | segment = offset >> rbd_dev->header.obj_order; |
| 682 | ret = snprintf(name, RBD_MAX_SEG_NAME_LEN, "%s.%012llx", |
| 683 | rbd_dev->header.object_prefix, segment); |
| 684 | if (ret < 0 || ret >= RBD_MAX_SEG_NAME_LEN) { |
| 685 | pr_err("error formatting segment name for #%llu (%d)\n", |
| 686 | segment, ret); |
| 687 | kfree(name); |
| 688 | name = NULL; |
| 689 | } |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 690 | |
Alex Elder | 65ccfe2 | 2012-08-09 10:33:26 -0700 | [diff] [blame^] | 691 | return name; |
| 692 | } |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 693 | |
Alex Elder | 65ccfe2 | 2012-08-09 10:33:26 -0700 | [diff] [blame^] | 694 | static u64 rbd_segment_offset(struct rbd_device *rbd_dev, u64 offset) |
| 695 | { |
| 696 | u64 segment_size = (u64) 1 << rbd_dev->header.obj_order; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 697 | |
Alex Elder | 65ccfe2 | 2012-08-09 10:33:26 -0700 | [diff] [blame^] | 698 | return offset & (segment_size - 1); |
| 699 | } |
| 700 | |
| 701 | static u64 rbd_segment_length(struct rbd_device *rbd_dev, |
| 702 | u64 offset, u64 length) |
| 703 | { |
| 704 | u64 segment_size = (u64) 1 << rbd_dev->header.obj_order; |
| 705 | |
| 706 | offset &= segment_size - 1; |
| 707 | |
| 708 | BUG_ON(length > U64_MAX - offset); |
| 709 | if (offset + length > segment_size) |
| 710 | length = segment_size - offset; |
| 711 | |
| 712 | return length; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 713 | } |
| 714 | |
Yehuda Sadeh | 1fec709 | 2011-05-13 13:52:56 -0700 | [diff] [blame] | 715 | static int rbd_get_num_segments(struct rbd_image_header *header, |
| 716 | u64 ofs, u64 len) |
| 717 | { |
Alex Elder | df111be | 2012-08-09 10:33:26 -0700 | [diff] [blame] | 718 | u64 start_seg; |
| 719 | u64 end_seg; |
| 720 | |
| 721 | if (!len) |
| 722 | return 0; |
| 723 | if (len - 1 > U64_MAX - ofs) |
| 724 | return -ERANGE; |
| 725 | |
| 726 | start_seg = ofs >> header->obj_order; |
| 727 | end_seg = (ofs + len - 1) >> header->obj_order; |
| 728 | |
Yehuda Sadeh | 1fec709 | 2011-05-13 13:52:56 -0700 | [diff] [blame] | 729 | return end_seg - start_seg + 1; |
| 730 | } |
| 731 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 732 | /* |
Josh Durgin | 029bcbd | 2011-07-22 11:35:23 -0700 | [diff] [blame] | 733 | * returns the size of an object in the image |
| 734 | */ |
| 735 | static u64 rbd_obj_bytes(struct rbd_image_header *header) |
| 736 | { |
| 737 | return 1 << header->obj_order; |
| 738 | } |
| 739 | |
| 740 | /* |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 741 | * bio helpers |
| 742 | */ |
| 743 | |
| 744 | static void bio_chain_put(struct bio *chain) |
| 745 | { |
| 746 | struct bio *tmp; |
| 747 | |
| 748 | while (chain) { |
| 749 | tmp = chain; |
| 750 | chain = chain->bi_next; |
| 751 | bio_put(tmp); |
| 752 | } |
| 753 | } |
| 754 | |
| 755 | /* |
| 756 | * zeros a bio chain, starting at specific offset |
| 757 | */ |
| 758 | static void zero_bio_chain(struct bio *chain, int start_ofs) |
| 759 | { |
| 760 | struct bio_vec *bv; |
| 761 | unsigned long flags; |
| 762 | void *buf; |
| 763 | int i; |
| 764 | int pos = 0; |
| 765 | |
| 766 | while (chain) { |
| 767 | bio_for_each_segment(bv, chain, i) { |
| 768 | if (pos + bv->bv_len > start_ofs) { |
| 769 | int remainder = max(start_ofs - pos, 0); |
| 770 | buf = bvec_kmap_irq(bv, &flags); |
| 771 | memset(buf + remainder, 0, |
| 772 | bv->bv_len - remainder); |
Dan Carpenter | 85b5aaa | 2010-10-11 21:15:11 +0200 | [diff] [blame] | 773 | bvec_kunmap_irq(buf, &flags); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 774 | } |
| 775 | pos += bv->bv_len; |
| 776 | } |
| 777 | |
| 778 | chain = chain->bi_next; |
| 779 | } |
| 780 | } |
| 781 | |
| 782 | /* |
| 783 | * bio_chain_clone - clone a chain of bios up to a certain length. |
| 784 | * might return a bio_pair that will need to be released. |
| 785 | */ |
| 786 | static struct bio *bio_chain_clone(struct bio **old, struct bio **next, |
| 787 | struct bio_pair **bp, |
| 788 | int len, gfp_t gfpmask) |
| 789 | { |
Alex Elder | 542582f | 2012-08-09 10:33:25 -0700 | [diff] [blame] | 790 | struct bio *old_chain = *old; |
| 791 | struct bio *new_chain = NULL; |
| 792 | struct bio *tail; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 793 | int total = 0; |
| 794 | |
| 795 | if (*bp) { |
| 796 | bio_pair_release(*bp); |
| 797 | *bp = NULL; |
| 798 | } |
| 799 | |
| 800 | while (old_chain && (total < len)) { |
Alex Elder | 542582f | 2012-08-09 10:33:25 -0700 | [diff] [blame] | 801 | struct bio *tmp; |
| 802 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 803 | tmp = bio_kmalloc(gfpmask, old_chain->bi_max_vecs); |
| 804 | if (!tmp) |
| 805 | goto err_out; |
Alex Elder | 542582f | 2012-08-09 10:33:25 -0700 | [diff] [blame] | 806 | gfpmask &= ~__GFP_WAIT; /* can't wait after the first */ |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 807 | |
| 808 | if (total + old_chain->bi_size > len) { |
| 809 | struct bio_pair *bp; |
| 810 | |
| 811 | /* |
| 812 | * this split can only happen with a single paged bio, |
| 813 | * split_bio will BUG_ON if this is not the case |
| 814 | */ |
| 815 | dout("bio_chain_clone split! total=%d remaining=%d" |
Alex Elder | bd919d4 | 2012-07-13 20:35:11 -0500 | [diff] [blame] | 816 | "bi_size=%u\n", |
| 817 | total, len - total, old_chain->bi_size); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 818 | |
| 819 | /* split the bio. We'll release it either in the next |
| 820 | call, or it will have to be released outside */ |
Alex Elder | 593a9e7 | 2012-02-07 12:03:37 -0600 | [diff] [blame] | 821 | bp = bio_split(old_chain, (len - total) / SECTOR_SIZE); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 822 | if (!bp) |
| 823 | goto err_out; |
| 824 | |
| 825 | __bio_clone(tmp, &bp->bio1); |
| 826 | |
| 827 | *next = &bp->bio2; |
| 828 | } else { |
| 829 | __bio_clone(tmp, old_chain); |
| 830 | *next = old_chain->bi_next; |
| 831 | } |
| 832 | |
| 833 | tmp->bi_bdev = NULL; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 834 | tmp->bi_next = NULL; |
Alex Elder | 542582f | 2012-08-09 10:33:25 -0700 | [diff] [blame] | 835 | if (new_chain) |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 836 | tail->bi_next = tmp; |
Alex Elder | 542582f | 2012-08-09 10:33:25 -0700 | [diff] [blame] | 837 | else |
| 838 | new_chain = tmp; |
| 839 | tail = tmp; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 840 | old_chain = old_chain->bi_next; |
| 841 | |
| 842 | total += tmp->bi_size; |
| 843 | } |
| 844 | |
| 845 | BUG_ON(total < len); |
| 846 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 847 | *old = old_chain; |
| 848 | |
| 849 | return new_chain; |
| 850 | |
| 851 | err_out: |
| 852 | dout("bio_chain_clone with err\n"); |
| 853 | bio_chain_put(new_chain); |
| 854 | return NULL; |
| 855 | } |
| 856 | |
| 857 | /* |
| 858 | * helpers for osd request op vectors. |
| 859 | */ |
Alex Elder | 57cfc10 | 2012-06-26 12:57:03 -0700 | [diff] [blame] | 860 | static struct ceph_osd_req_op *rbd_create_rw_ops(int num_ops, |
| 861 | int opcode, u32 payload_len) |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 862 | { |
Alex Elder | 57cfc10 | 2012-06-26 12:57:03 -0700 | [diff] [blame] | 863 | struct ceph_osd_req_op *ops; |
| 864 | |
| 865 | ops = kzalloc(sizeof (*ops) * (num_ops + 1), GFP_NOIO); |
| 866 | if (!ops) |
| 867 | return NULL; |
| 868 | |
| 869 | ops[0].op = opcode; |
| 870 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 871 | /* |
| 872 | * op extent offset and length will be set later on |
| 873 | * in calc_raw_layout() |
| 874 | */ |
Alex Elder | 57cfc10 | 2012-06-26 12:57:03 -0700 | [diff] [blame] | 875 | ops[0].payload_len = payload_len; |
| 876 | |
| 877 | return ops; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 878 | } |
| 879 | |
| 880 | static void rbd_destroy_ops(struct ceph_osd_req_op *ops) |
| 881 | { |
| 882 | kfree(ops); |
| 883 | } |
| 884 | |
Yehuda Sadeh | 1fec709 | 2011-05-13 13:52:56 -0700 | [diff] [blame] | 885 | static void rbd_coll_end_req_index(struct request *rq, |
| 886 | struct rbd_req_coll *coll, |
| 887 | int index, |
| 888 | int ret, u64 len) |
| 889 | { |
| 890 | struct request_queue *q; |
| 891 | int min, max, i; |
| 892 | |
Alex Elder | bd919d4 | 2012-07-13 20:35:11 -0500 | [diff] [blame] | 893 | dout("rbd_coll_end_req_index %p index %d ret %d len %llu\n", |
| 894 | coll, index, ret, (unsigned long long) len); |
Yehuda Sadeh | 1fec709 | 2011-05-13 13:52:56 -0700 | [diff] [blame] | 895 | |
| 896 | if (!rq) |
| 897 | return; |
| 898 | |
| 899 | if (!coll) { |
| 900 | blk_end_request(rq, ret, len); |
| 901 | return; |
| 902 | } |
| 903 | |
| 904 | q = rq->q; |
| 905 | |
| 906 | spin_lock_irq(q->queue_lock); |
| 907 | coll->status[index].done = 1; |
| 908 | coll->status[index].rc = ret; |
| 909 | coll->status[index].bytes = len; |
| 910 | max = min = coll->num_done; |
| 911 | while (max < coll->total && coll->status[max].done) |
| 912 | max++; |
| 913 | |
| 914 | for (i = min; i<max; i++) { |
| 915 | __blk_end_request(rq, coll->status[i].rc, |
| 916 | coll->status[i].bytes); |
| 917 | coll->num_done++; |
| 918 | kref_put(&coll->kref, rbd_coll_release); |
| 919 | } |
| 920 | spin_unlock_irq(q->queue_lock); |
| 921 | } |
| 922 | |
| 923 | static void rbd_coll_end_req(struct rbd_request *req, |
| 924 | int ret, u64 len) |
| 925 | { |
| 926 | rbd_coll_end_req_index(req->rq, req->coll, req->coll_index, ret, len); |
| 927 | } |
| 928 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 929 | /* |
| 930 | * Send ceph osd request |
| 931 | */ |
| 932 | static int rbd_do_request(struct request *rq, |
Alex Elder | 0ce1a79 | 2012-07-03 16:01:18 -0500 | [diff] [blame] | 933 | struct rbd_device *rbd_dev, |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 934 | struct ceph_snap_context *snapc, |
| 935 | u64 snapid, |
Alex Elder | aded07e | 2012-07-03 16:01:18 -0500 | [diff] [blame] | 936 | const char *object_name, u64 ofs, u64 len, |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 937 | struct bio *bio, |
| 938 | struct page **pages, |
| 939 | int num_pages, |
| 940 | int flags, |
| 941 | struct ceph_osd_req_op *ops, |
Yehuda Sadeh | 1fec709 | 2011-05-13 13:52:56 -0700 | [diff] [blame] | 942 | struct rbd_req_coll *coll, |
| 943 | int coll_index, |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 944 | void (*rbd_cb)(struct ceph_osd_request *req, |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 945 | struct ceph_msg *msg), |
| 946 | struct ceph_osd_request **linger_req, |
| 947 | u64 *ver) |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 948 | { |
| 949 | struct ceph_osd_request *req; |
| 950 | struct ceph_file_layout *layout; |
| 951 | int ret; |
| 952 | u64 bno; |
| 953 | struct timespec mtime = CURRENT_TIME; |
| 954 | struct rbd_request *req_data; |
| 955 | struct ceph_osd_request_head *reqhead; |
Alex Elder | 1dbb439 | 2012-01-24 10:08:37 -0600 | [diff] [blame] | 956 | struct ceph_osd_client *osdc; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 957 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 958 | req_data = kzalloc(sizeof(*req_data), GFP_NOIO); |
Yehuda Sadeh | 1fec709 | 2011-05-13 13:52:56 -0700 | [diff] [blame] | 959 | if (!req_data) { |
| 960 | if (coll) |
| 961 | rbd_coll_end_req_index(rq, coll, coll_index, |
| 962 | -ENOMEM, len); |
| 963 | return -ENOMEM; |
| 964 | } |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 965 | |
Yehuda Sadeh | 1fec709 | 2011-05-13 13:52:56 -0700 | [diff] [blame] | 966 | if (coll) { |
| 967 | req_data->coll = coll; |
| 968 | req_data->coll_index = coll_index; |
| 969 | } |
| 970 | |
Alex Elder | bd919d4 | 2012-07-13 20:35:11 -0500 | [diff] [blame] | 971 | dout("rbd_do_request object_name=%s ofs=%llu len=%llu\n", object_name, |
| 972 | (unsigned long long) ofs, (unsigned long long) len); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 973 | |
Alex Elder | 0ce1a79 | 2012-07-03 16:01:18 -0500 | [diff] [blame] | 974 | osdc = &rbd_dev->rbd_client->client->osdc; |
Alex Elder | 1dbb439 | 2012-01-24 10:08:37 -0600 | [diff] [blame] | 975 | req = ceph_osdc_alloc_request(osdc, flags, snapc, ops, |
| 976 | false, GFP_NOIO, pages, bio); |
Sage Weil | 4ad1262 | 2011-05-03 09:23:36 -0700 | [diff] [blame] | 977 | if (!req) { |
Sage Weil | 4ad1262 | 2011-05-03 09:23:36 -0700 | [diff] [blame] | 978 | ret = -ENOMEM; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 979 | goto done_pages; |
| 980 | } |
| 981 | |
| 982 | req->r_callback = rbd_cb; |
| 983 | |
| 984 | req_data->rq = rq; |
| 985 | req_data->bio = bio; |
| 986 | req_data->pages = pages; |
| 987 | req_data->len = len; |
| 988 | |
| 989 | req->r_priv = req_data; |
| 990 | |
| 991 | reqhead = req->r_request->front.iov_base; |
| 992 | reqhead->snapid = cpu_to_le64(CEPH_NOSNAP); |
| 993 | |
Alex Elder | aded07e | 2012-07-03 16:01:18 -0500 | [diff] [blame] | 994 | strncpy(req->r_oid, object_name, sizeof(req->r_oid)); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 995 | req->r_oid_len = strlen(req->r_oid); |
| 996 | |
| 997 | layout = &req->r_file_layout; |
| 998 | memset(layout, 0, sizeof(*layout)); |
| 999 | layout->fl_stripe_unit = cpu_to_le32(1 << RBD_MAX_OBJ_ORDER); |
| 1000 | layout->fl_stripe_count = cpu_to_le32(1); |
| 1001 | layout->fl_object_size = cpu_to_le32(1 << RBD_MAX_OBJ_ORDER); |
Alex Elder | 0ce1a79 | 2012-07-03 16:01:18 -0500 | [diff] [blame] | 1002 | layout->fl_pg_pool = cpu_to_le32(rbd_dev->pool_id); |
Alex Elder | 1dbb439 | 2012-01-24 10:08:37 -0600 | [diff] [blame] | 1003 | ceph_calc_raw_layout(osdc, layout, snapid, ofs, &len, &bno, |
| 1004 | req, ops); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1005 | |
| 1006 | ceph_osdc_build_request(req, ofs, &len, |
| 1007 | ops, |
| 1008 | snapc, |
| 1009 | &mtime, |
| 1010 | req->r_oid, req->r_oid_len); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1011 | |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 1012 | if (linger_req) { |
Alex Elder | 1dbb439 | 2012-01-24 10:08:37 -0600 | [diff] [blame] | 1013 | ceph_osdc_set_request_linger(osdc, req); |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 1014 | *linger_req = req; |
| 1015 | } |
| 1016 | |
Alex Elder | 1dbb439 | 2012-01-24 10:08:37 -0600 | [diff] [blame] | 1017 | ret = ceph_osdc_start_request(osdc, req, false); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1018 | if (ret < 0) |
| 1019 | goto done_err; |
| 1020 | |
| 1021 | if (!rbd_cb) { |
Alex Elder | 1dbb439 | 2012-01-24 10:08:37 -0600 | [diff] [blame] | 1022 | ret = ceph_osdc_wait_request(osdc, req); |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 1023 | if (ver) |
| 1024 | *ver = le64_to_cpu(req->r_reassert_version.version); |
Alex Elder | bd919d4 | 2012-07-13 20:35:11 -0500 | [diff] [blame] | 1025 | dout("reassert_ver=%llu\n", |
| 1026 | (unsigned long long) |
| 1027 | le64_to_cpu(req->r_reassert_version.version)); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1028 | ceph_osdc_put_request(req); |
| 1029 | } |
| 1030 | return ret; |
| 1031 | |
| 1032 | done_err: |
| 1033 | bio_chain_put(req_data->bio); |
| 1034 | ceph_osdc_put_request(req); |
| 1035 | done_pages: |
Yehuda Sadeh | 1fec709 | 2011-05-13 13:52:56 -0700 | [diff] [blame] | 1036 | rbd_coll_end_req(req_data, ret, len); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1037 | kfree(req_data); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1038 | return ret; |
| 1039 | } |
| 1040 | |
| 1041 | /* |
| 1042 | * Ceph osd op callback |
| 1043 | */ |
| 1044 | static void rbd_req_cb(struct ceph_osd_request *req, struct ceph_msg *msg) |
| 1045 | { |
| 1046 | struct rbd_request *req_data = req->r_priv; |
| 1047 | struct ceph_osd_reply_head *replyhead; |
| 1048 | struct ceph_osd_op *op; |
| 1049 | __s32 rc; |
| 1050 | u64 bytes; |
| 1051 | int read_op; |
| 1052 | |
| 1053 | /* parse reply */ |
| 1054 | replyhead = msg->front.iov_base; |
| 1055 | WARN_ON(le32_to_cpu(replyhead->num_ops) == 0); |
| 1056 | op = (void *)(replyhead + 1); |
| 1057 | rc = le32_to_cpu(replyhead->result); |
| 1058 | bytes = le64_to_cpu(op->extent.length); |
Dan Carpenter | 895cfcc | 2012-06-06 09:15:33 -0500 | [diff] [blame] | 1059 | read_op = (le16_to_cpu(op->op) == CEPH_OSD_OP_READ); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1060 | |
Alex Elder | bd919d4 | 2012-07-13 20:35:11 -0500 | [diff] [blame] | 1061 | dout("rbd_req_cb bytes=%llu readop=%d rc=%d\n", |
| 1062 | (unsigned long long) bytes, read_op, (int) rc); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1063 | |
| 1064 | if (rc == -ENOENT && read_op) { |
| 1065 | zero_bio_chain(req_data->bio, 0); |
| 1066 | rc = 0; |
| 1067 | } else if (rc == 0 && read_op && bytes < req_data->len) { |
| 1068 | zero_bio_chain(req_data->bio, bytes); |
| 1069 | bytes = req_data->len; |
| 1070 | } |
| 1071 | |
Yehuda Sadeh | 1fec709 | 2011-05-13 13:52:56 -0700 | [diff] [blame] | 1072 | rbd_coll_end_req(req_data, rc, bytes); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1073 | |
| 1074 | if (req_data->bio) |
| 1075 | bio_chain_put(req_data->bio); |
| 1076 | |
| 1077 | ceph_osdc_put_request(req); |
| 1078 | kfree(req_data); |
| 1079 | } |
| 1080 | |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 1081 | static void rbd_simple_req_cb(struct ceph_osd_request *req, struct ceph_msg *msg) |
| 1082 | { |
| 1083 | ceph_osdc_put_request(req); |
| 1084 | } |
| 1085 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1086 | /* |
| 1087 | * Do a synchronous ceph osd operation |
| 1088 | */ |
Alex Elder | 0ce1a79 | 2012-07-03 16:01:18 -0500 | [diff] [blame] | 1089 | static int rbd_req_sync_op(struct rbd_device *rbd_dev, |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1090 | struct ceph_snap_context *snapc, |
| 1091 | u64 snapid, |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1092 | int flags, |
Alex Elder | 913d2fd | 2012-06-26 12:57:03 -0700 | [diff] [blame] | 1093 | struct ceph_osd_req_op *ops, |
Alex Elder | aded07e | 2012-07-03 16:01:18 -0500 | [diff] [blame] | 1094 | const char *object_name, |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1095 | u64 ofs, u64 len, |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 1096 | char *buf, |
| 1097 | struct ceph_osd_request **linger_req, |
| 1098 | u64 *ver) |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1099 | { |
| 1100 | int ret; |
| 1101 | struct page **pages; |
| 1102 | int num_pages; |
Alex Elder | 913d2fd | 2012-06-26 12:57:03 -0700 | [diff] [blame] | 1103 | |
| 1104 | BUG_ON(ops == NULL); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1105 | |
| 1106 | num_pages = calc_pages_for(ofs , len); |
| 1107 | pages = ceph_alloc_page_vector(num_pages, GFP_KERNEL); |
Dan Carpenter | b8d0638 | 2010-10-11 21:14:23 +0200 | [diff] [blame] | 1108 | if (IS_ERR(pages)) |
| 1109 | return PTR_ERR(pages); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1110 | |
Alex Elder | 0ce1a79 | 2012-07-03 16:01:18 -0500 | [diff] [blame] | 1111 | ret = rbd_do_request(NULL, rbd_dev, snapc, snapid, |
Alex Elder | aded07e | 2012-07-03 16:01:18 -0500 | [diff] [blame] | 1112 | object_name, ofs, len, NULL, |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1113 | pages, num_pages, |
| 1114 | flags, |
| 1115 | ops, |
Yehuda Sadeh | 1fec709 | 2011-05-13 13:52:56 -0700 | [diff] [blame] | 1116 | NULL, 0, |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 1117 | NULL, |
| 1118 | linger_req, ver); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1119 | if (ret < 0) |
Alex Elder | 913d2fd | 2012-06-26 12:57:03 -0700 | [diff] [blame] | 1120 | goto done; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1121 | |
| 1122 | if ((flags & CEPH_OSD_FLAG_READ) && buf) |
| 1123 | ret = ceph_copy_from_page_vector(pages, buf, ofs, ret); |
| 1124 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1125 | done: |
| 1126 | ceph_release_page_vector(pages, num_pages); |
| 1127 | return ret; |
| 1128 | } |
| 1129 | |
| 1130 | /* |
| 1131 | * Do an asynchronous ceph osd operation |
| 1132 | */ |
| 1133 | static int rbd_do_op(struct request *rq, |
Alex Elder | 0ce1a79 | 2012-07-03 16:01:18 -0500 | [diff] [blame] | 1134 | struct rbd_device *rbd_dev, |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1135 | struct ceph_snap_context *snapc, |
| 1136 | u64 snapid, |
Alex Elder | d1f57ea | 2012-06-26 12:57:03 -0700 | [diff] [blame] | 1137 | int opcode, int flags, |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1138 | u64 ofs, u64 len, |
Yehuda Sadeh | 1fec709 | 2011-05-13 13:52:56 -0700 | [diff] [blame] | 1139 | struct bio *bio, |
| 1140 | struct rbd_req_coll *coll, |
| 1141 | int coll_index) |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1142 | { |
| 1143 | char *seg_name; |
| 1144 | u64 seg_ofs; |
| 1145 | u64 seg_len; |
| 1146 | int ret; |
| 1147 | struct ceph_osd_req_op *ops; |
| 1148 | u32 payload_len; |
| 1149 | |
Alex Elder | 65ccfe2 | 2012-08-09 10:33:26 -0700 | [diff] [blame^] | 1150 | seg_name = rbd_segment_name(rbd_dev, ofs); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1151 | if (!seg_name) |
| 1152 | return -ENOMEM; |
Alex Elder | 65ccfe2 | 2012-08-09 10:33:26 -0700 | [diff] [blame^] | 1153 | seg_len = rbd_segment_length(rbd_dev, ofs, len); |
| 1154 | seg_ofs = rbd_segment_offset(rbd_dev, ofs); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1155 | |
| 1156 | payload_len = (flags & CEPH_OSD_FLAG_WRITE ? seg_len : 0); |
| 1157 | |
Alex Elder | 57cfc10 | 2012-06-26 12:57:03 -0700 | [diff] [blame] | 1158 | ret = -ENOMEM; |
| 1159 | ops = rbd_create_rw_ops(1, opcode, payload_len); |
| 1160 | if (!ops) |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1161 | goto done; |
| 1162 | |
| 1163 | /* we've taken care of segment sizes earlier when we |
| 1164 | cloned the bios. We should never have a segment |
| 1165 | truncated at this point */ |
| 1166 | BUG_ON(seg_len < len); |
| 1167 | |
| 1168 | ret = rbd_do_request(rq, rbd_dev, snapc, snapid, |
| 1169 | seg_name, seg_ofs, seg_len, |
| 1170 | bio, |
| 1171 | NULL, 0, |
| 1172 | flags, |
| 1173 | ops, |
Yehuda Sadeh | 1fec709 | 2011-05-13 13:52:56 -0700 | [diff] [blame] | 1174 | coll, coll_index, |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 1175 | rbd_req_cb, 0, NULL); |
Sage Weil | 11f7700 | 2011-05-12 16:13:54 -0700 | [diff] [blame] | 1176 | |
| 1177 | rbd_destroy_ops(ops); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1178 | done: |
| 1179 | kfree(seg_name); |
| 1180 | return ret; |
| 1181 | } |
| 1182 | |
| 1183 | /* |
| 1184 | * Request async osd write |
| 1185 | */ |
| 1186 | static int rbd_req_write(struct request *rq, |
| 1187 | struct rbd_device *rbd_dev, |
| 1188 | struct ceph_snap_context *snapc, |
| 1189 | u64 ofs, u64 len, |
Yehuda Sadeh | 1fec709 | 2011-05-13 13:52:56 -0700 | [diff] [blame] | 1190 | struct bio *bio, |
| 1191 | struct rbd_req_coll *coll, |
| 1192 | int coll_index) |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1193 | { |
| 1194 | return rbd_do_op(rq, rbd_dev, snapc, CEPH_NOSNAP, |
| 1195 | CEPH_OSD_OP_WRITE, |
| 1196 | CEPH_OSD_FLAG_WRITE | CEPH_OSD_FLAG_ONDISK, |
Yehuda Sadeh | 1fec709 | 2011-05-13 13:52:56 -0700 | [diff] [blame] | 1197 | ofs, len, bio, coll, coll_index); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1198 | } |
| 1199 | |
| 1200 | /* |
| 1201 | * Request async osd read |
| 1202 | */ |
| 1203 | static int rbd_req_read(struct request *rq, |
| 1204 | struct rbd_device *rbd_dev, |
| 1205 | u64 snapid, |
| 1206 | u64 ofs, u64 len, |
Yehuda Sadeh | 1fec709 | 2011-05-13 13:52:56 -0700 | [diff] [blame] | 1207 | struct bio *bio, |
| 1208 | struct rbd_req_coll *coll, |
| 1209 | int coll_index) |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1210 | { |
| 1211 | return rbd_do_op(rq, rbd_dev, NULL, |
Josh Durgin | b06e6a6 | 2011-11-21 18:16:52 -0800 | [diff] [blame] | 1212 | snapid, |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1213 | CEPH_OSD_OP_READ, |
| 1214 | CEPH_OSD_FLAG_READ, |
Yehuda Sadeh | 1fec709 | 2011-05-13 13:52:56 -0700 | [diff] [blame] | 1215 | ofs, len, bio, coll, coll_index); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1216 | } |
| 1217 | |
| 1218 | /* |
| 1219 | * Request sync osd read |
| 1220 | */ |
Alex Elder | 0ce1a79 | 2012-07-03 16:01:18 -0500 | [diff] [blame] | 1221 | static int rbd_req_sync_read(struct rbd_device *rbd_dev, |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1222 | u64 snapid, |
Alex Elder | aded07e | 2012-07-03 16:01:18 -0500 | [diff] [blame] | 1223 | const char *object_name, |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1224 | u64 ofs, u64 len, |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 1225 | char *buf, |
| 1226 | u64 *ver) |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1227 | { |
Alex Elder | 913d2fd | 2012-06-26 12:57:03 -0700 | [diff] [blame] | 1228 | struct ceph_osd_req_op *ops; |
| 1229 | int ret; |
| 1230 | |
| 1231 | ops = rbd_create_rw_ops(1, CEPH_OSD_OP_READ, 0); |
| 1232 | if (!ops) |
| 1233 | return -ENOMEM; |
| 1234 | |
| 1235 | ret = rbd_req_sync_op(rbd_dev, NULL, |
Josh Durgin | b06e6a6 | 2011-11-21 18:16:52 -0800 | [diff] [blame] | 1236 | snapid, |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1237 | CEPH_OSD_FLAG_READ, |
Alex Elder | 913d2fd | 2012-06-26 12:57:03 -0700 | [diff] [blame] | 1238 | ops, object_name, ofs, len, buf, NULL, ver); |
| 1239 | rbd_destroy_ops(ops); |
| 1240 | |
| 1241 | return ret; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1242 | } |
| 1243 | |
| 1244 | /* |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 1245 | * Request sync osd watch |
| 1246 | */ |
Alex Elder | 0ce1a79 | 2012-07-03 16:01:18 -0500 | [diff] [blame] | 1247 | static int rbd_req_sync_notify_ack(struct rbd_device *rbd_dev, |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 1248 | u64 ver, |
Alex Elder | 7f0a24d | 2012-07-25 09:32:40 -0500 | [diff] [blame] | 1249 | u64 notify_id) |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 1250 | { |
| 1251 | struct ceph_osd_req_op *ops; |
Sage Weil | 11f7700 | 2011-05-12 16:13:54 -0700 | [diff] [blame] | 1252 | int ret; |
| 1253 | |
Alex Elder | 57cfc10 | 2012-06-26 12:57:03 -0700 | [diff] [blame] | 1254 | ops = rbd_create_rw_ops(1, CEPH_OSD_OP_NOTIFY_ACK, 0); |
| 1255 | if (!ops) |
| 1256 | return -ENOMEM; |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 1257 | |
Josh Durgin | a71b891 | 2011-12-05 18:10:44 -0800 | [diff] [blame] | 1258 | ops[0].watch.ver = cpu_to_le64(ver); |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 1259 | ops[0].watch.cookie = notify_id; |
| 1260 | ops[0].watch.flag = 0; |
| 1261 | |
Alex Elder | 0ce1a79 | 2012-07-03 16:01:18 -0500 | [diff] [blame] | 1262 | ret = rbd_do_request(NULL, rbd_dev, NULL, CEPH_NOSNAP, |
Alex Elder | 7f0a24d | 2012-07-25 09:32:40 -0500 | [diff] [blame] | 1263 | rbd_dev->header_name, 0, 0, NULL, |
Alex Elder | ad4f232 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 1264 | NULL, 0, |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 1265 | CEPH_OSD_FLAG_READ, |
| 1266 | ops, |
Yehuda Sadeh | 1fec709 | 2011-05-13 13:52:56 -0700 | [diff] [blame] | 1267 | NULL, 0, |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 1268 | rbd_simple_req_cb, 0, NULL); |
| 1269 | |
| 1270 | rbd_destroy_ops(ops); |
| 1271 | return ret; |
| 1272 | } |
| 1273 | |
| 1274 | static void rbd_watch_cb(u64 ver, u64 notify_id, u8 opcode, void *data) |
| 1275 | { |
Alex Elder | 0ce1a79 | 2012-07-03 16:01:18 -0500 | [diff] [blame] | 1276 | struct rbd_device *rbd_dev = (struct rbd_device *)data; |
Josh Durgin | a71b891 | 2011-12-05 18:10:44 -0800 | [diff] [blame] | 1277 | u64 hver; |
Sage Weil | 13143d2 | 2011-05-12 16:08:30 -0700 | [diff] [blame] | 1278 | int rc; |
| 1279 | |
Alex Elder | 0ce1a79 | 2012-07-03 16:01:18 -0500 | [diff] [blame] | 1280 | if (!rbd_dev) |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 1281 | return; |
| 1282 | |
Alex Elder | bd919d4 | 2012-07-13 20:35:11 -0500 | [diff] [blame] | 1283 | dout("rbd_watch_cb %s notify_id=%llu opcode=%u\n", |
| 1284 | rbd_dev->header_name, (unsigned long long) notify_id, |
| 1285 | (unsigned int) opcode); |
Alex Elder | 1fe5e99 | 2012-07-25 09:32:41 -0500 | [diff] [blame] | 1286 | rc = rbd_refresh_header(rbd_dev, &hver); |
Sage Weil | 13143d2 | 2011-05-12 16:08:30 -0700 | [diff] [blame] | 1287 | if (rc) |
Alex Elder | f0f8cef | 2012-01-29 13:57:44 -0600 | [diff] [blame] | 1288 | pr_warning(RBD_DRV_NAME "%d got notification but failed to " |
Alex Elder | 0ce1a79 | 2012-07-03 16:01:18 -0500 | [diff] [blame] | 1289 | " update snaps: %d\n", rbd_dev->major, rc); |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 1290 | |
Alex Elder | 7f0a24d | 2012-07-25 09:32:40 -0500 | [diff] [blame] | 1291 | rbd_req_sync_notify_ack(rbd_dev, hver, notify_id); |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 1292 | } |
| 1293 | |
| 1294 | /* |
| 1295 | * Request sync osd watch |
| 1296 | */ |
Alex Elder | 0e6f322 | 2012-07-25 09:32:40 -0500 | [diff] [blame] | 1297 | static int rbd_req_sync_watch(struct rbd_device *rbd_dev) |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 1298 | { |
| 1299 | struct ceph_osd_req_op *ops; |
Alex Elder | 0ce1a79 | 2012-07-03 16:01:18 -0500 | [diff] [blame] | 1300 | struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc; |
Alex Elder | 57cfc10 | 2012-06-26 12:57:03 -0700 | [diff] [blame] | 1301 | int ret; |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 1302 | |
Alex Elder | 57cfc10 | 2012-06-26 12:57:03 -0700 | [diff] [blame] | 1303 | ops = rbd_create_rw_ops(1, CEPH_OSD_OP_WATCH, 0); |
| 1304 | if (!ops) |
| 1305 | return -ENOMEM; |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 1306 | |
| 1307 | ret = ceph_osdc_create_event(osdc, rbd_watch_cb, 0, |
Alex Elder | 0ce1a79 | 2012-07-03 16:01:18 -0500 | [diff] [blame] | 1308 | (void *)rbd_dev, &rbd_dev->watch_event); |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 1309 | if (ret < 0) |
| 1310 | goto fail; |
| 1311 | |
Alex Elder | 0e6f322 | 2012-07-25 09:32:40 -0500 | [diff] [blame] | 1312 | ops[0].watch.ver = cpu_to_le64(rbd_dev->header.obj_version); |
Alex Elder | 0ce1a79 | 2012-07-03 16:01:18 -0500 | [diff] [blame] | 1313 | ops[0].watch.cookie = cpu_to_le64(rbd_dev->watch_event->cookie); |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 1314 | ops[0].watch.flag = 1; |
| 1315 | |
Alex Elder | 0ce1a79 | 2012-07-03 16:01:18 -0500 | [diff] [blame] | 1316 | ret = rbd_req_sync_op(rbd_dev, NULL, |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 1317 | CEPH_NOSNAP, |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 1318 | CEPH_OSD_FLAG_WRITE | CEPH_OSD_FLAG_ONDISK, |
| 1319 | ops, |
Alex Elder | 0e6f322 | 2012-07-25 09:32:40 -0500 | [diff] [blame] | 1320 | rbd_dev->header_name, |
| 1321 | 0, 0, NULL, |
Alex Elder | 0ce1a79 | 2012-07-03 16:01:18 -0500 | [diff] [blame] | 1322 | &rbd_dev->watch_request, NULL); |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 1323 | |
| 1324 | if (ret < 0) |
| 1325 | goto fail_event; |
| 1326 | |
| 1327 | rbd_destroy_ops(ops); |
| 1328 | return 0; |
| 1329 | |
| 1330 | fail_event: |
Alex Elder | 0ce1a79 | 2012-07-03 16:01:18 -0500 | [diff] [blame] | 1331 | ceph_osdc_cancel_event(rbd_dev->watch_event); |
| 1332 | rbd_dev->watch_event = NULL; |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 1333 | fail: |
| 1334 | rbd_destroy_ops(ops); |
| 1335 | return ret; |
| 1336 | } |
| 1337 | |
Yehuda Sadeh | 79e3057c | 2011-07-12 16:56:57 -0700 | [diff] [blame] | 1338 | /* |
| 1339 | * Request sync osd unwatch |
| 1340 | */ |
Alex Elder | 070c633 | 2012-07-25 09:32:41 -0500 | [diff] [blame] | 1341 | static int rbd_req_sync_unwatch(struct rbd_device *rbd_dev) |
Yehuda Sadeh | 79e3057c | 2011-07-12 16:56:57 -0700 | [diff] [blame] | 1342 | { |
| 1343 | struct ceph_osd_req_op *ops; |
Alex Elder | 57cfc10 | 2012-06-26 12:57:03 -0700 | [diff] [blame] | 1344 | int ret; |
Yehuda Sadeh | 79e3057c | 2011-07-12 16:56:57 -0700 | [diff] [blame] | 1345 | |
Alex Elder | 57cfc10 | 2012-06-26 12:57:03 -0700 | [diff] [blame] | 1346 | ops = rbd_create_rw_ops(1, CEPH_OSD_OP_WATCH, 0); |
| 1347 | if (!ops) |
| 1348 | return -ENOMEM; |
Yehuda Sadeh | 79e3057c | 2011-07-12 16:56:57 -0700 | [diff] [blame] | 1349 | |
| 1350 | ops[0].watch.ver = 0; |
Alex Elder | 0ce1a79 | 2012-07-03 16:01:18 -0500 | [diff] [blame] | 1351 | ops[0].watch.cookie = cpu_to_le64(rbd_dev->watch_event->cookie); |
Yehuda Sadeh | 79e3057c | 2011-07-12 16:56:57 -0700 | [diff] [blame] | 1352 | ops[0].watch.flag = 0; |
| 1353 | |
Alex Elder | 0ce1a79 | 2012-07-03 16:01:18 -0500 | [diff] [blame] | 1354 | ret = rbd_req_sync_op(rbd_dev, NULL, |
Yehuda Sadeh | 79e3057c | 2011-07-12 16:56:57 -0700 | [diff] [blame] | 1355 | CEPH_NOSNAP, |
Yehuda Sadeh | 79e3057c | 2011-07-12 16:56:57 -0700 | [diff] [blame] | 1356 | CEPH_OSD_FLAG_WRITE | CEPH_OSD_FLAG_ONDISK, |
| 1357 | ops, |
Alex Elder | 070c633 | 2012-07-25 09:32:41 -0500 | [diff] [blame] | 1358 | rbd_dev->header_name, |
| 1359 | 0, 0, NULL, NULL, NULL); |
| 1360 | |
Yehuda Sadeh | 79e3057c | 2011-07-12 16:56:57 -0700 | [diff] [blame] | 1361 | |
| 1362 | rbd_destroy_ops(ops); |
Alex Elder | 0ce1a79 | 2012-07-03 16:01:18 -0500 | [diff] [blame] | 1363 | ceph_osdc_cancel_event(rbd_dev->watch_event); |
| 1364 | rbd_dev->watch_event = NULL; |
Yehuda Sadeh | 79e3057c | 2011-07-12 16:56:57 -0700 | [diff] [blame] | 1365 | return ret; |
| 1366 | } |
| 1367 | |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 1368 | struct rbd_notify_info { |
Alex Elder | 0ce1a79 | 2012-07-03 16:01:18 -0500 | [diff] [blame] | 1369 | struct rbd_device *rbd_dev; |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 1370 | }; |
| 1371 | |
| 1372 | static void rbd_notify_cb(u64 ver, u64 notify_id, u8 opcode, void *data) |
| 1373 | { |
Alex Elder | 0ce1a79 | 2012-07-03 16:01:18 -0500 | [diff] [blame] | 1374 | struct rbd_device *rbd_dev = (struct rbd_device *)data; |
| 1375 | if (!rbd_dev) |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 1376 | return; |
| 1377 | |
Alex Elder | bd919d4 | 2012-07-13 20:35:11 -0500 | [diff] [blame] | 1378 | dout("rbd_notify_cb %s notify_id=%llu opcode=%u\n", |
| 1379 | rbd_dev->header_name, (unsigned long long) notify_id, |
| 1380 | (unsigned int) opcode); |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 1381 | } |
| 1382 | |
| 1383 | /* |
| 1384 | * Request sync osd notify |
| 1385 | */ |
Alex Elder | 4cb1625 | 2012-07-25 09:32:40 -0500 | [diff] [blame] | 1386 | static int rbd_req_sync_notify(struct rbd_device *rbd_dev) |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 1387 | { |
| 1388 | struct ceph_osd_req_op *ops; |
Alex Elder | 0ce1a79 | 2012-07-03 16:01:18 -0500 | [diff] [blame] | 1389 | struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc; |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 1390 | struct ceph_osd_event *event; |
| 1391 | struct rbd_notify_info info; |
| 1392 | int payload_len = sizeof(u32) + sizeof(u32); |
| 1393 | int ret; |
| 1394 | |
Alex Elder | 57cfc10 | 2012-06-26 12:57:03 -0700 | [diff] [blame] | 1395 | ops = rbd_create_rw_ops(1, CEPH_OSD_OP_NOTIFY, payload_len); |
| 1396 | if (!ops) |
| 1397 | return -ENOMEM; |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 1398 | |
Alex Elder | 0ce1a79 | 2012-07-03 16:01:18 -0500 | [diff] [blame] | 1399 | info.rbd_dev = rbd_dev; |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 1400 | |
| 1401 | ret = ceph_osdc_create_event(osdc, rbd_notify_cb, 1, |
| 1402 | (void *)&info, &event); |
| 1403 | if (ret < 0) |
| 1404 | goto fail; |
| 1405 | |
| 1406 | ops[0].watch.ver = 1; |
| 1407 | ops[0].watch.flag = 1; |
| 1408 | ops[0].watch.cookie = event->cookie; |
| 1409 | ops[0].watch.prot_ver = RADOS_NOTIFY_VER; |
| 1410 | ops[0].watch.timeout = 12; |
| 1411 | |
Alex Elder | 0ce1a79 | 2012-07-03 16:01:18 -0500 | [diff] [blame] | 1412 | ret = rbd_req_sync_op(rbd_dev, NULL, |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 1413 | CEPH_NOSNAP, |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 1414 | CEPH_OSD_FLAG_WRITE | CEPH_OSD_FLAG_ONDISK, |
| 1415 | ops, |
Alex Elder | 4cb1625 | 2012-07-25 09:32:40 -0500 | [diff] [blame] | 1416 | rbd_dev->header_name, |
| 1417 | 0, 0, NULL, NULL, NULL); |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 1418 | if (ret < 0) |
| 1419 | goto fail_event; |
| 1420 | |
| 1421 | ret = ceph_osdc_wait_event(event, CEPH_OSD_TIMEOUT_DEFAULT); |
| 1422 | dout("ceph_osdc_wait_event returned %d\n", ret); |
| 1423 | rbd_destroy_ops(ops); |
| 1424 | return 0; |
| 1425 | |
| 1426 | fail_event: |
| 1427 | ceph_osdc_cancel_event(event); |
| 1428 | fail: |
| 1429 | rbd_destroy_ops(ops); |
| 1430 | return ret; |
| 1431 | } |
| 1432 | |
| 1433 | /* |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1434 | * Request sync osd read |
| 1435 | */ |
Alex Elder | 0ce1a79 | 2012-07-03 16:01:18 -0500 | [diff] [blame] | 1436 | static int rbd_req_sync_exec(struct rbd_device *rbd_dev, |
Alex Elder | aded07e | 2012-07-03 16:01:18 -0500 | [diff] [blame] | 1437 | const char *object_name, |
| 1438 | const char *class_name, |
| 1439 | const char *method_name, |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1440 | const char *data, |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 1441 | int len, |
| 1442 | u64 *ver) |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1443 | { |
| 1444 | struct ceph_osd_req_op *ops; |
Alex Elder | aded07e | 2012-07-03 16:01:18 -0500 | [diff] [blame] | 1445 | int class_name_len = strlen(class_name); |
| 1446 | int method_name_len = strlen(method_name); |
Alex Elder | 57cfc10 | 2012-06-26 12:57:03 -0700 | [diff] [blame] | 1447 | int ret; |
| 1448 | |
| 1449 | ops = rbd_create_rw_ops(1, CEPH_OSD_OP_CALL, |
Alex Elder | aded07e | 2012-07-03 16:01:18 -0500 | [diff] [blame] | 1450 | class_name_len + method_name_len + len); |
Alex Elder | 57cfc10 | 2012-06-26 12:57:03 -0700 | [diff] [blame] | 1451 | if (!ops) |
| 1452 | return -ENOMEM; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1453 | |
Alex Elder | aded07e | 2012-07-03 16:01:18 -0500 | [diff] [blame] | 1454 | ops[0].cls.class_name = class_name; |
| 1455 | ops[0].cls.class_len = (__u8) class_name_len; |
| 1456 | ops[0].cls.method_name = method_name; |
| 1457 | ops[0].cls.method_len = (__u8) method_name_len; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1458 | ops[0].cls.argc = 0; |
| 1459 | ops[0].cls.indata = data; |
| 1460 | ops[0].cls.indata_len = len; |
| 1461 | |
Alex Elder | 0ce1a79 | 2012-07-03 16:01:18 -0500 | [diff] [blame] | 1462 | ret = rbd_req_sync_op(rbd_dev, NULL, |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1463 | CEPH_NOSNAP, |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1464 | CEPH_OSD_FLAG_WRITE | CEPH_OSD_FLAG_ONDISK, |
| 1465 | ops, |
Alex Elder | d1f57ea | 2012-06-26 12:57:03 -0700 | [diff] [blame] | 1466 | object_name, 0, 0, NULL, NULL, ver); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1467 | |
| 1468 | rbd_destroy_ops(ops); |
| 1469 | |
| 1470 | dout("cls_exec returned %d\n", ret); |
| 1471 | return ret; |
| 1472 | } |
| 1473 | |
Yehuda Sadeh | 1fec709 | 2011-05-13 13:52:56 -0700 | [diff] [blame] | 1474 | static struct rbd_req_coll *rbd_alloc_coll(int num_reqs) |
| 1475 | { |
| 1476 | struct rbd_req_coll *coll = |
| 1477 | kzalloc(sizeof(struct rbd_req_coll) + |
| 1478 | sizeof(struct rbd_req_status) * num_reqs, |
| 1479 | GFP_ATOMIC); |
| 1480 | |
| 1481 | if (!coll) |
| 1482 | return NULL; |
| 1483 | coll->total = num_reqs; |
| 1484 | kref_init(&coll->kref); |
| 1485 | return coll; |
| 1486 | } |
| 1487 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1488 | /* |
| 1489 | * block device queue callback |
| 1490 | */ |
| 1491 | static void rbd_rq_fn(struct request_queue *q) |
| 1492 | { |
| 1493 | struct rbd_device *rbd_dev = q->queuedata; |
| 1494 | struct request *rq; |
| 1495 | struct bio_pair *bp = NULL; |
| 1496 | |
Alex Elder | 00f1f36 | 2012-02-07 12:03:36 -0600 | [diff] [blame] | 1497 | while ((rq = blk_fetch_request(q))) { |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1498 | struct bio *bio; |
| 1499 | struct bio *rq_bio, *next_bio = NULL; |
| 1500 | bool do_write; |
Alex Elder | bd919d4 | 2012-07-13 20:35:11 -0500 | [diff] [blame] | 1501 | unsigned int size; |
| 1502 | u64 op_size = 0; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1503 | u64 ofs; |
Yehuda Sadeh | 1fec709 | 2011-05-13 13:52:56 -0700 | [diff] [blame] | 1504 | int num_segs, cur_seg = 0; |
| 1505 | struct rbd_req_coll *coll; |
Josh Durgin | d1d2564 | 2011-12-05 14:03:05 -0800 | [diff] [blame] | 1506 | struct ceph_snap_context *snapc; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1507 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1508 | dout("fetched request\n"); |
| 1509 | |
| 1510 | /* filter out block requests we don't understand */ |
| 1511 | if ((rq->cmd_type != REQ_TYPE_FS)) { |
| 1512 | __blk_end_request_all(rq, 0); |
Alex Elder | 00f1f36 | 2012-02-07 12:03:36 -0600 | [diff] [blame] | 1513 | continue; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1514 | } |
| 1515 | |
| 1516 | /* deduce our operation (read, write) */ |
| 1517 | do_write = (rq_data_dir(rq) == WRITE); |
| 1518 | |
| 1519 | size = blk_rq_bytes(rq); |
Alex Elder | 593a9e7 | 2012-02-07 12:03:37 -0600 | [diff] [blame] | 1520 | ofs = blk_rq_pos(rq) * SECTOR_SIZE; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1521 | rq_bio = rq->bio; |
| 1522 | if (do_write && rbd_dev->read_only) { |
| 1523 | __blk_end_request_all(rq, -EROFS); |
Alex Elder | 00f1f36 | 2012-02-07 12:03:36 -0600 | [diff] [blame] | 1524 | continue; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1525 | } |
| 1526 | |
| 1527 | spin_unlock_irq(q->queue_lock); |
| 1528 | |
Josh Durgin | d1d2564 | 2011-12-05 14:03:05 -0800 | [diff] [blame] | 1529 | down_read(&rbd_dev->header_rwsem); |
Josh Durgin | e88a36e | 2011-11-21 18:14:25 -0800 | [diff] [blame] | 1530 | |
Josh Durgin | d1d2564 | 2011-12-05 14:03:05 -0800 | [diff] [blame] | 1531 | if (rbd_dev->snap_id != CEPH_NOSNAP && !rbd_dev->snap_exists) { |
Josh Durgin | e88a36e | 2011-11-21 18:14:25 -0800 | [diff] [blame] | 1532 | up_read(&rbd_dev->header_rwsem); |
Josh Durgin | d1d2564 | 2011-12-05 14:03:05 -0800 | [diff] [blame] | 1533 | dout("request for non-existent snapshot"); |
| 1534 | spin_lock_irq(q->queue_lock); |
| 1535 | __blk_end_request_all(rq, -ENXIO); |
| 1536 | continue; |
Josh Durgin | e88a36e | 2011-11-21 18:14:25 -0800 | [diff] [blame] | 1537 | } |
| 1538 | |
Josh Durgin | d1d2564 | 2011-12-05 14:03:05 -0800 | [diff] [blame] | 1539 | snapc = ceph_get_snap_context(rbd_dev->header.snapc); |
| 1540 | |
| 1541 | up_read(&rbd_dev->header_rwsem); |
| 1542 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1543 | dout("%s 0x%x bytes at 0x%llx\n", |
| 1544 | do_write ? "write" : "read", |
Alex Elder | bd919d4 | 2012-07-13 20:35:11 -0500 | [diff] [blame] | 1545 | size, (unsigned long long) blk_rq_pos(rq) * SECTOR_SIZE); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1546 | |
Yehuda Sadeh | 1fec709 | 2011-05-13 13:52:56 -0700 | [diff] [blame] | 1547 | num_segs = rbd_get_num_segments(&rbd_dev->header, ofs, size); |
Alex Elder | df111be | 2012-08-09 10:33:26 -0700 | [diff] [blame] | 1548 | if (num_segs <= 0) { |
| 1549 | spin_lock_irq(q->queue_lock); |
| 1550 | __blk_end_request_all(rq, num_segs); |
| 1551 | ceph_put_snap_context(snapc); |
| 1552 | continue; |
| 1553 | } |
Yehuda Sadeh | 1fec709 | 2011-05-13 13:52:56 -0700 | [diff] [blame] | 1554 | coll = rbd_alloc_coll(num_segs); |
| 1555 | if (!coll) { |
| 1556 | spin_lock_irq(q->queue_lock); |
| 1557 | __blk_end_request_all(rq, -ENOMEM); |
Josh Durgin | d1d2564 | 2011-12-05 14:03:05 -0800 | [diff] [blame] | 1558 | ceph_put_snap_context(snapc); |
Alex Elder | 00f1f36 | 2012-02-07 12:03:36 -0600 | [diff] [blame] | 1559 | continue; |
Yehuda Sadeh | 1fec709 | 2011-05-13 13:52:56 -0700 | [diff] [blame] | 1560 | } |
| 1561 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1562 | do { |
| 1563 | /* a bio clone to be passed down to OSD req */ |
Alex Elder | bd919d4 | 2012-07-13 20:35:11 -0500 | [diff] [blame] | 1564 | dout("rq->bio->bi_vcnt=%hu\n", rq->bio->bi_vcnt); |
Alex Elder | 65ccfe2 | 2012-08-09 10:33:26 -0700 | [diff] [blame^] | 1565 | op_size = rbd_segment_length(rbd_dev, ofs, size); |
Yehuda Sadeh | 1fec709 | 2011-05-13 13:52:56 -0700 | [diff] [blame] | 1566 | kref_get(&coll->kref); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1567 | bio = bio_chain_clone(&rq_bio, &next_bio, &bp, |
| 1568 | op_size, GFP_ATOMIC); |
| 1569 | if (!bio) { |
Yehuda Sadeh | 1fec709 | 2011-05-13 13:52:56 -0700 | [diff] [blame] | 1570 | rbd_coll_end_req_index(rq, coll, cur_seg, |
| 1571 | -ENOMEM, op_size); |
| 1572 | goto next_seg; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1573 | } |
| 1574 | |
Yehuda Sadeh | 1fec709 | 2011-05-13 13:52:56 -0700 | [diff] [blame] | 1575 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1576 | /* init OSD command: write or read */ |
| 1577 | if (do_write) |
| 1578 | rbd_req_write(rq, rbd_dev, |
Josh Durgin | d1d2564 | 2011-12-05 14:03:05 -0800 | [diff] [blame] | 1579 | snapc, |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1580 | ofs, |
Yehuda Sadeh | 1fec709 | 2011-05-13 13:52:56 -0700 | [diff] [blame] | 1581 | op_size, bio, |
| 1582 | coll, cur_seg); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1583 | else |
| 1584 | rbd_req_read(rq, rbd_dev, |
Josh Durgin | 77dfe99 | 2011-11-21 13:04:42 -0800 | [diff] [blame] | 1585 | rbd_dev->snap_id, |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1586 | ofs, |
Yehuda Sadeh | 1fec709 | 2011-05-13 13:52:56 -0700 | [diff] [blame] | 1587 | op_size, bio, |
| 1588 | coll, cur_seg); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1589 | |
Yehuda Sadeh | 1fec709 | 2011-05-13 13:52:56 -0700 | [diff] [blame] | 1590 | next_seg: |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1591 | size -= op_size; |
| 1592 | ofs += op_size; |
| 1593 | |
Yehuda Sadeh | 1fec709 | 2011-05-13 13:52:56 -0700 | [diff] [blame] | 1594 | cur_seg++; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1595 | rq_bio = next_bio; |
| 1596 | } while (size > 0); |
Yehuda Sadeh | 1fec709 | 2011-05-13 13:52:56 -0700 | [diff] [blame] | 1597 | kref_put(&coll->kref, rbd_coll_release); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1598 | |
| 1599 | if (bp) |
| 1600 | bio_pair_release(bp); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1601 | spin_lock_irq(q->queue_lock); |
Josh Durgin | d1d2564 | 2011-12-05 14:03:05 -0800 | [diff] [blame] | 1602 | |
| 1603 | ceph_put_snap_context(snapc); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1604 | } |
| 1605 | } |
| 1606 | |
| 1607 | /* |
| 1608 | * a queue callback. Makes sure that we don't create a bio that spans across |
| 1609 | * multiple osd objects. One exception would be with a single page bios, |
| 1610 | * which we handle later at bio_chain_clone |
| 1611 | */ |
| 1612 | static int rbd_merge_bvec(struct request_queue *q, struct bvec_merge_data *bmd, |
| 1613 | struct bio_vec *bvec) |
| 1614 | { |
| 1615 | struct rbd_device *rbd_dev = q->queuedata; |
Alex Elder | 593a9e7 | 2012-02-07 12:03:37 -0600 | [diff] [blame] | 1616 | unsigned int chunk_sectors; |
| 1617 | sector_t sector; |
| 1618 | unsigned int bio_sectors; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1619 | int max; |
| 1620 | |
Alex Elder | 593a9e7 | 2012-02-07 12:03:37 -0600 | [diff] [blame] | 1621 | chunk_sectors = 1 << (rbd_dev->header.obj_order - SECTOR_SHIFT); |
| 1622 | sector = bmd->bi_sector + get_start_sect(bmd->bi_bdev); |
| 1623 | bio_sectors = bmd->bi_size >> SECTOR_SHIFT; |
| 1624 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1625 | max = (chunk_sectors - ((sector & (chunk_sectors - 1)) |
Alex Elder | 593a9e7 | 2012-02-07 12:03:37 -0600 | [diff] [blame] | 1626 | + bio_sectors)) << SECTOR_SHIFT; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1627 | if (max < 0) |
| 1628 | max = 0; /* bio_add cannot handle a negative return */ |
| 1629 | if (max <= bvec->bv_len && bio_sectors == 0) |
| 1630 | return bvec->bv_len; |
| 1631 | return max; |
| 1632 | } |
| 1633 | |
| 1634 | static void rbd_free_disk(struct rbd_device *rbd_dev) |
| 1635 | { |
| 1636 | struct gendisk *disk = rbd_dev->disk; |
| 1637 | |
| 1638 | if (!disk) |
| 1639 | return; |
| 1640 | |
| 1641 | rbd_header_free(&rbd_dev->header); |
| 1642 | |
| 1643 | if (disk->flags & GENHD_FL_UP) |
| 1644 | del_gendisk(disk); |
| 1645 | if (disk->queue) |
| 1646 | blk_cleanup_queue(disk->queue); |
| 1647 | put_disk(disk); |
| 1648 | } |
| 1649 | |
| 1650 | /* |
Alex Elder | 4156d99 | 2012-08-02 11:29:46 -0500 | [diff] [blame] | 1651 | * Read the complete header for the given rbd device. |
| 1652 | * |
| 1653 | * Returns a pointer to a dynamically-allocated buffer containing |
| 1654 | * the complete and validated header. Caller can pass the address |
| 1655 | * of a variable that will be filled in with the version of the |
| 1656 | * header object at the time it was read. |
| 1657 | * |
| 1658 | * Returns a pointer-coded errno if a failure occurs. |
| 1659 | */ |
| 1660 | static struct rbd_image_header_ondisk * |
| 1661 | rbd_dev_v1_header_read(struct rbd_device *rbd_dev, u64 *version) |
| 1662 | { |
| 1663 | struct rbd_image_header_ondisk *ondisk = NULL; |
| 1664 | u32 snap_count = 0; |
| 1665 | u64 names_size = 0; |
| 1666 | u32 want_count; |
| 1667 | int ret; |
| 1668 | |
| 1669 | /* |
| 1670 | * The complete header will include an array of its 64-bit |
| 1671 | * snapshot ids, followed by the names of those snapshots as |
| 1672 | * a contiguous block of NUL-terminated strings. Note that |
| 1673 | * the number of snapshots could change by the time we read |
| 1674 | * it in, in which case we re-read it. |
| 1675 | */ |
| 1676 | do { |
| 1677 | size_t size; |
| 1678 | |
| 1679 | kfree(ondisk); |
| 1680 | |
| 1681 | size = sizeof (*ondisk); |
| 1682 | size += snap_count * sizeof (struct rbd_image_snap_ondisk); |
| 1683 | size += names_size; |
| 1684 | ondisk = kmalloc(size, GFP_KERNEL); |
| 1685 | if (!ondisk) |
| 1686 | return ERR_PTR(-ENOMEM); |
| 1687 | |
| 1688 | ret = rbd_req_sync_read(rbd_dev, CEPH_NOSNAP, |
| 1689 | rbd_dev->header_name, |
| 1690 | 0, size, |
| 1691 | (char *) ondisk, version); |
| 1692 | |
| 1693 | if (ret < 0) |
| 1694 | goto out_err; |
| 1695 | if (WARN_ON((size_t) ret < size)) { |
| 1696 | ret = -ENXIO; |
| 1697 | pr_warning("short header read for image %s" |
| 1698 | " (want %zd got %d)\n", |
| 1699 | rbd_dev->image_name, size, ret); |
| 1700 | goto out_err; |
| 1701 | } |
| 1702 | if (!rbd_dev_ondisk_valid(ondisk)) { |
| 1703 | ret = -ENXIO; |
| 1704 | pr_warning("invalid header for image %s\n", |
| 1705 | rbd_dev->image_name); |
| 1706 | goto out_err; |
| 1707 | } |
| 1708 | |
| 1709 | names_size = le64_to_cpu(ondisk->snap_names_len); |
| 1710 | want_count = snap_count; |
| 1711 | snap_count = le32_to_cpu(ondisk->snap_count); |
| 1712 | } while (snap_count != want_count); |
| 1713 | |
| 1714 | return ondisk; |
| 1715 | |
| 1716 | out_err: |
| 1717 | kfree(ondisk); |
| 1718 | |
| 1719 | return ERR_PTR(ret); |
| 1720 | } |
| 1721 | |
| 1722 | /* |
| 1723 | * reload the ondisk the header |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1724 | */ |
| 1725 | static int rbd_read_header(struct rbd_device *rbd_dev, |
| 1726 | struct rbd_image_header *header) |
| 1727 | { |
Alex Elder | 4156d99 | 2012-08-02 11:29:46 -0500 | [diff] [blame] | 1728 | struct rbd_image_header_ondisk *ondisk; |
| 1729 | u64 ver = 0; |
| 1730 | int ret; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1731 | |
Alex Elder | 4156d99 | 2012-08-02 11:29:46 -0500 | [diff] [blame] | 1732 | ondisk = rbd_dev_v1_header_read(rbd_dev, &ver); |
| 1733 | if (IS_ERR(ondisk)) |
| 1734 | return PTR_ERR(ondisk); |
| 1735 | ret = rbd_header_from_disk(header, ondisk); |
| 1736 | if (ret >= 0) |
| 1737 | header->obj_version = ver; |
| 1738 | kfree(ondisk); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1739 | |
Alex Elder | 4156d99 | 2012-08-02 11:29:46 -0500 | [diff] [blame] | 1740 | return ret; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1741 | } |
| 1742 | |
| 1743 | /* |
| 1744 | * create a snapshot |
| 1745 | */ |
Alex Elder | 0ce1a79 | 2012-07-03 16:01:18 -0500 | [diff] [blame] | 1746 | static int rbd_header_add_snap(struct rbd_device *rbd_dev, |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1747 | const char *snap_name, |
| 1748 | gfp_t gfp_flags) |
| 1749 | { |
| 1750 | int name_len = strlen(snap_name); |
| 1751 | u64 new_snapid; |
| 1752 | int ret; |
Sage Weil | 916d4d6 | 2011-05-12 16:10:50 -0700 | [diff] [blame] | 1753 | void *data, *p, *e; |
Alex Elder | 1dbb439 | 2012-01-24 10:08:37 -0600 | [diff] [blame] | 1754 | struct ceph_mon_client *monc; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1755 | |
| 1756 | /* we should create a snapshot only if we're pointing at the head */ |
Alex Elder | 0ce1a79 | 2012-07-03 16:01:18 -0500 | [diff] [blame] | 1757 | if (rbd_dev->snap_id != CEPH_NOSNAP) |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1758 | return -EINVAL; |
| 1759 | |
Alex Elder | 0ce1a79 | 2012-07-03 16:01:18 -0500 | [diff] [blame] | 1760 | monc = &rbd_dev->rbd_client->client->monc; |
| 1761 | ret = ceph_monc_create_snapid(monc, rbd_dev->pool_id, &new_snapid); |
Alex Elder | bd919d4 | 2012-07-13 20:35:11 -0500 | [diff] [blame] | 1762 | dout("created snapid=%llu\n", (unsigned long long) new_snapid); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1763 | if (ret < 0) |
| 1764 | return ret; |
| 1765 | |
| 1766 | data = kmalloc(name_len + 16, gfp_flags); |
| 1767 | if (!data) |
| 1768 | return -ENOMEM; |
| 1769 | |
Sage Weil | 916d4d6 | 2011-05-12 16:10:50 -0700 | [diff] [blame] | 1770 | p = data; |
| 1771 | e = data + name_len + 16; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1772 | |
Sage Weil | 916d4d6 | 2011-05-12 16:10:50 -0700 | [diff] [blame] | 1773 | ceph_encode_string_safe(&p, e, snap_name, name_len, bad); |
| 1774 | ceph_encode_64_safe(&p, e, new_snapid, bad); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1775 | |
Alex Elder | 0bed54d | 2012-07-03 16:01:18 -0500 | [diff] [blame] | 1776 | ret = rbd_req_sync_exec(rbd_dev, rbd_dev->header_name, |
Alex Elder | 0ce1a79 | 2012-07-03 16:01:18 -0500 | [diff] [blame] | 1777 | "rbd", "snap_add", |
Alex Elder | d67d4be | 2012-07-13 20:35:11 -0500 | [diff] [blame] | 1778 | data, p - data, NULL); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1779 | |
Sage Weil | 916d4d6 | 2011-05-12 16:10:50 -0700 | [diff] [blame] | 1780 | kfree(data); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1781 | |
Alex Elder | 505cbb9 | 2012-07-19 08:49:18 -0500 | [diff] [blame] | 1782 | return ret < 0 ? ret : 0; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1783 | bad: |
| 1784 | return -ERANGE; |
| 1785 | } |
| 1786 | |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 1787 | static void __rbd_remove_all_snaps(struct rbd_device *rbd_dev) |
| 1788 | { |
| 1789 | struct rbd_snap *snap; |
Alex Elder | a059329 | 2012-07-19 09:09:27 -0500 | [diff] [blame] | 1790 | struct rbd_snap *next; |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 1791 | |
Alex Elder | a059329 | 2012-07-19 09:09:27 -0500 | [diff] [blame] | 1792 | list_for_each_entry_safe(snap, next, &rbd_dev->snaps, node) |
Alex Elder | 14e7085 | 2012-07-19 09:09:27 -0500 | [diff] [blame] | 1793 | __rbd_remove_snap_dev(snap); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 1794 | } |
| 1795 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1796 | /* |
| 1797 | * only read the first part of the ondisk header, without the snaps info |
| 1798 | */ |
Alex Elder | b813623 | 2012-07-25 09:32:41 -0500 | [diff] [blame] | 1799 | static int __rbd_refresh_header(struct rbd_device *rbd_dev, u64 *hver) |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1800 | { |
| 1801 | int ret; |
| 1802 | struct rbd_image_header h; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1803 | |
| 1804 | ret = rbd_read_header(rbd_dev, &h); |
| 1805 | if (ret < 0) |
| 1806 | return ret; |
| 1807 | |
Josh Durgin | a51aa0c | 2011-12-05 10:35:04 -0800 | [diff] [blame] | 1808 | down_write(&rbd_dev->header_rwsem); |
| 1809 | |
Sage Weil | 9db4b3e | 2011-04-19 22:49:06 -0700 | [diff] [blame] | 1810 | /* resized? */ |
Josh Durgin | 474ef7c | 2011-11-21 17:13:54 -0800 | [diff] [blame] | 1811 | if (rbd_dev->snap_id == CEPH_NOSNAP) { |
| 1812 | sector_t size = (sector_t) h.image_size / SECTOR_SIZE; |
| 1813 | |
| 1814 | dout("setting size to %llu sectors", (unsigned long long) size); |
| 1815 | set_capacity(rbd_dev->disk, size); |
| 1816 | } |
Sage Weil | 9db4b3e | 2011-04-19 22:49:06 -0700 | [diff] [blame] | 1817 | |
Alex Elder | 849b426 | 2012-07-09 21:04:24 -0500 | [diff] [blame] | 1818 | /* rbd_dev->header.object_prefix shouldn't change */ |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1819 | kfree(rbd_dev->header.snap_sizes); |
Alex Elder | 849b426 | 2012-07-09 21:04:24 -0500 | [diff] [blame] | 1820 | kfree(rbd_dev->header.snap_names); |
Josh Durgin | d1d2564 | 2011-12-05 14:03:05 -0800 | [diff] [blame] | 1821 | /* osd requests may still refer to snapc */ |
| 1822 | ceph_put_snap_context(rbd_dev->header.snapc); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1823 | |
Alex Elder | b813623 | 2012-07-25 09:32:41 -0500 | [diff] [blame] | 1824 | if (hver) |
| 1825 | *hver = h.obj_version; |
Josh Durgin | a71b891 | 2011-12-05 18:10:44 -0800 | [diff] [blame] | 1826 | rbd_dev->header.obj_version = h.obj_version; |
Josh Durgin | 93a24e0 | 2011-12-05 10:41:28 -0800 | [diff] [blame] | 1827 | rbd_dev->header.image_size = h.image_size; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1828 | rbd_dev->header.total_snaps = h.total_snaps; |
| 1829 | rbd_dev->header.snapc = h.snapc; |
| 1830 | rbd_dev->header.snap_names = h.snap_names; |
| 1831 | rbd_dev->header.snap_sizes = h.snap_sizes; |
Alex Elder | 849b426 | 2012-07-09 21:04:24 -0500 | [diff] [blame] | 1832 | /* Free the extra copy of the object prefix */ |
| 1833 | WARN_ON(strcmp(rbd_dev->header.object_prefix, h.object_prefix)); |
| 1834 | kfree(h.object_prefix); |
| 1835 | |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 1836 | ret = __rbd_init_snaps_header(rbd_dev); |
| 1837 | |
Josh Durgin | c666601 | 2011-11-21 17:11:12 -0800 | [diff] [blame] | 1838 | up_write(&rbd_dev->header_rwsem); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1839 | |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 1840 | return ret; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1841 | } |
| 1842 | |
Alex Elder | 1fe5e99 | 2012-07-25 09:32:41 -0500 | [diff] [blame] | 1843 | static int rbd_refresh_header(struct rbd_device *rbd_dev, u64 *hver) |
| 1844 | { |
| 1845 | int ret; |
| 1846 | |
| 1847 | mutex_lock_nested(&ctl_mutex, SINGLE_DEPTH_NESTING); |
| 1848 | ret = __rbd_refresh_header(rbd_dev, hver); |
| 1849 | mutex_unlock(&ctl_mutex); |
| 1850 | |
| 1851 | return ret; |
| 1852 | } |
| 1853 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1854 | static int rbd_init_disk(struct rbd_device *rbd_dev) |
| 1855 | { |
| 1856 | struct gendisk *disk; |
| 1857 | struct request_queue *q; |
| 1858 | int rc; |
Alex Elder | 593a9e7 | 2012-02-07 12:03:37 -0600 | [diff] [blame] | 1859 | u64 segment_size; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1860 | u64 total_size = 0; |
| 1861 | |
| 1862 | /* contact OSD, request size info about the object being mapped */ |
| 1863 | rc = rbd_read_header(rbd_dev, &rbd_dev->header); |
| 1864 | if (rc) |
| 1865 | return rc; |
| 1866 | |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 1867 | /* no need to lock here, as rbd_dev is not registered yet */ |
| 1868 | rc = __rbd_init_snaps_header(rbd_dev); |
| 1869 | if (rc) |
| 1870 | return rc; |
| 1871 | |
Josh Durgin | cc9d734 | 2011-11-21 18:19:13 -0800 | [diff] [blame] | 1872 | rc = rbd_header_set_snap(rbd_dev, &total_size); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1873 | if (rc) |
| 1874 | return rc; |
| 1875 | |
| 1876 | /* create gendisk info */ |
| 1877 | rc = -ENOMEM; |
| 1878 | disk = alloc_disk(RBD_MINORS_PER_MAJOR); |
| 1879 | if (!disk) |
| 1880 | goto out; |
| 1881 | |
Alex Elder | f0f8cef | 2012-01-29 13:57:44 -0600 | [diff] [blame] | 1882 | snprintf(disk->disk_name, sizeof(disk->disk_name), RBD_DRV_NAME "%d", |
Alex Elder | de71a29 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 1883 | rbd_dev->dev_id); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1884 | disk->major = rbd_dev->major; |
| 1885 | disk->first_minor = 0; |
| 1886 | disk->fops = &rbd_bd_ops; |
| 1887 | disk->private_data = rbd_dev; |
| 1888 | |
| 1889 | /* init rq */ |
| 1890 | rc = -ENOMEM; |
| 1891 | q = blk_init_queue(rbd_rq_fn, &rbd_dev->lock); |
| 1892 | if (!q) |
| 1893 | goto out_disk; |
Josh Durgin | 029bcbd | 2011-07-22 11:35:23 -0700 | [diff] [blame] | 1894 | |
Alex Elder | 593a9e7 | 2012-02-07 12:03:37 -0600 | [diff] [blame] | 1895 | /* We use the default size, but let's be explicit about it. */ |
| 1896 | blk_queue_physical_block_size(q, SECTOR_SIZE); |
| 1897 | |
Josh Durgin | 029bcbd | 2011-07-22 11:35:23 -0700 | [diff] [blame] | 1898 | /* set io sizes to object size */ |
Alex Elder | 593a9e7 | 2012-02-07 12:03:37 -0600 | [diff] [blame] | 1899 | segment_size = rbd_obj_bytes(&rbd_dev->header); |
| 1900 | blk_queue_max_hw_sectors(q, segment_size / SECTOR_SIZE); |
| 1901 | blk_queue_max_segment_size(q, segment_size); |
| 1902 | blk_queue_io_min(q, segment_size); |
| 1903 | blk_queue_io_opt(q, segment_size); |
Josh Durgin | 029bcbd | 2011-07-22 11:35:23 -0700 | [diff] [blame] | 1904 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1905 | blk_queue_merge_bvec(q, rbd_merge_bvec); |
| 1906 | disk->queue = q; |
| 1907 | |
| 1908 | q->queuedata = rbd_dev; |
| 1909 | |
| 1910 | rbd_dev->disk = disk; |
| 1911 | rbd_dev->q = q; |
| 1912 | |
| 1913 | /* finally, announce the disk to the world */ |
Alex Elder | 593a9e7 | 2012-02-07 12:03:37 -0600 | [diff] [blame] | 1914 | set_capacity(disk, total_size / SECTOR_SIZE); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1915 | add_disk(disk); |
| 1916 | |
| 1917 | pr_info("%s: added with size 0x%llx\n", |
| 1918 | disk->disk_name, (unsigned long long)total_size); |
| 1919 | return 0; |
| 1920 | |
| 1921 | out_disk: |
| 1922 | put_disk(disk); |
| 1923 | out: |
| 1924 | return rc; |
| 1925 | } |
| 1926 | |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 1927 | /* |
| 1928 | sysfs |
| 1929 | */ |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1930 | |
Alex Elder | 593a9e7 | 2012-02-07 12:03:37 -0600 | [diff] [blame] | 1931 | static struct rbd_device *dev_to_rbd_dev(struct device *dev) |
| 1932 | { |
| 1933 | return container_of(dev, struct rbd_device, dev); |
| 1934 | } |
| 1935 | |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 1936 | static ssize_t rbd_size_show(struct device *dev, |
| 1937 | struct device_attribute *attr, char *buf) |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1938 | { |
Alex Elder | 593a9e7 | 2012-02-07 12:03:37 -0600 | [diff] [blame] | 1939 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
Josh Durgin | a51aa0c | 2011-12-05 10:35:04 -0800 | [diff] [blame] | 1940 | sector_t size; |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 1941 | |
Josh Durgin | a51aa0c | 2011-12-05 10:35:04 -0800 | [diff] [blame] | 1942 | down_read(&rbd_dev->header_rwsem); |
| 1943 | size = get_capacity(rbd_dev->disk); |
| 1944 | up_read(&rbd_dev->header_rwsem); |
| 1945 | |
| 1946 | return sprintf(buf, "%llu\n", (unsigned long long) size * SECTOR_SIZE); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1947 | } |
| 1948 | |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 1949 | static ssize_t rbd_major_show(struct device *dev, |
| 1950 | struct device_attribute *attr, char *buf) |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1951 | { |
Alex Elder | 593a9e7 | 2012-02-07 12:03:37 -0600 | [diff] [blame] | 1952 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 1953 | |
| 1954 | return sprintf(buf, "%d\n", rbd_dev->major); |
| 1955 | } |
| 1956 | |
| 1957 | static ssize_t rbd_client_id_show(struct device *dev, |
| 1958 | struct device_attribute *attr, char *buf) |
| 1959 | { |
Alex Elder | 593a9e7 | 2012-02-07 12:03:37 -0600 | [diff] [blame] | 1960 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 1961 | |
Alex Elder | 1dbb439 | 2012-01-24 10:08:37 -0600 | [diff] [blame] | 1962 | return sprintf(buf, "client%lld\n", |
| 1963 | ceph_client_id(rbd_dev->rbd_client->client)); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 1964 | } |
| 1965 | |
| 1966 | static ssize_t rbd_pool_show(struct device *dev, |
| 1967 | struct device_attribute *attr, char *buf) |
| 1968 | { |
Alex Elder | 593a9e7 | 2012-02-07 12:03:37 -0600 | [diff] [blame] | 1969 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 1970 | |
| 1971 | return sprintf(buf, "%s\n", rbd_dev->pool_name); |
| 1972 | } |
| 1973 | |
Alex Elder | 9bb2f33 | 2012-07-12 10:46:35 -0500 | [diff] [blame] | 1974 | static ssize_t rbd_pool_id_show(struct device *dev, |
| 1975 | struct device_attribute *attr, char *buf) |
| 1976 | { |
| 1977 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
| 1978 | |
| 1979 | return sprintf(buf, "%d\n", rbd_dev->pool_id); |
| 1980 | } |
| 1981 | |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 1982 | static ssize_t rbd_name_show(struct device *dev, |
| 1983 | struct device_attribute *attr, char *buf) |
| 1984 | { |
Alex Elder | 593a9e7 | 2012-02-07 12:03:37 -0600 | [diff] [blame] | 1985 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 1986 | |
Alex Elder | 0bed54d | 2012-07-03 16:01:18 -0500 | [diff] [blame] | 1987 | return sprintf(buf, "%s\n", rbd_dev->image_name); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 1988 | } |
| 1989 | |
| 1990 | static ssize_t rbd_snap_show(struct device *dev, |
| 1991 | struct device_attribute *attr, |
| 1992 | char *buf) |
| 1993 | { |
Alex Elder | 593a9e7 | 2012-02-07 12:03:37 -0600 | [diff] [blame] | 1994 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 1995 | |
| 1996 | return sprintf(buf, "%s\n", rbd_dev->snap_name); |
| 1997 | } |
| 1998 | |
| 1999 | static ssize_t rbd_image_refresh(struct device *dev, |
| 2000 | struct device_attribute *attr, |
| 2001 | const char *buf, |
| 2002 | size_t size) |
| 2003 | { |
Alex Elder | 593a9e7 | 2012-02-07 12:03:37 -0600 | [diff] [blame] | 2004 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
Alex Elder | b813623 | 2012-07-25 09:32:41 -0500 | [diff] [blame] | 2005 | int ret; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 2006 | |
Alex Elder | 1fe5e99 | 2012-07-25 09:32:41 -0500 | [diff] [blame] | 2007 | ret = rbd_refresh_header(rbd_dev, NULL); |
Alex Elder | b813623 | 2012-07-25 09:32:41 -0500 | [diff] [blame] | 2008 | |
| 2009 | return ret < 0 ? ret : size; |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 2010 | } |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 2011 | |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 2012 | static DEVICE_ATTR(size, S_IRUGO, rbd_size_show, NULL); |
| 2013 | static DEVICE_ATTR(major, S_IRUGO, rbd_major_show, NULL); |
| 2014 | static DEVICE_ATTR(client_id, S_IRUGO, rbd_client_id_show, NULL); |
| 2015 | static DEVICE_ATTR(pool, S_IRUGO, rbd_pool_show, NULL); |
Alex Elder | 9bb2f33 | 2012-07-12 10:46:35 -0500 | [diff] [blame] | 2016 | static DEVICE_ATTR(pool_id, S_IRUGO, rbd_pool_id_show, NULL); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 2017 | static DEVICE_ATTR(name, S_IRUGO, rbd_name_show, NULL); |
| 2018 | static DEVICE_ATTR(refresh, S_IWUSR, NULL, rbd_image_refresh); |
| 2019 | static DEVICE_ATTR(current_snap, S_IRUGO, rbd_snap_show, NULL); |
| 2020 | static DEVICE_ATTR(create_snap, S_IWUSR, NULL, rbd_snap_add); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 2021 | |
| 2022 | static struct attribute *rbd_attrs[] = { |
| 2023 | &dev_attr_size.attr, |
| 2024 | &dev_attr_major.attr, |
| 2025 | &dev_attr_client_id.attr, |
| 2026 | &dev_attr_pool.attr, |
Alex Elder | 9bb2f33 | 2012-07-12 10:46:35 -0500 | [diff] [blame] | 2027 | &dev_attr_pool_id.attr, |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 2028 | &dev_attr_name.attr, |
| 2029 | &dev_attr_current_snap.attr, |
| 2030 | &dev_attr_refresh.attr, |
| 2031 | &dev_attr_create_snap.attr, |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 2032 | NULL |
| 2033 | }; |
| 2034 | |
| 2035 | static struct attribute_group rbd_attr_group = { |
| 2036 | .attrs = rbd_attrs, |
| 2037 | }; |
| 2038 | |
| 2039 | static const struct attribute_group *rbd_attr_groups[] = { |
| 2040 | &rbd_attr_group, |
| 2041 | NULL |
| 2042 | }; |
| 2043 | |
| 2044 | static void rbd_sysfs_dev_release(struct device *dev) |
| 2045 | { |
| 2046 | } |
| 2047 | |
| 2048 | static struct device_type rbd_device_type = { |
| 2049 | .name = "rbd", |
| 2050 | .groups = rbd_attr_groups, |
| 2051 | .release = rbd_sysfs_dev_release, |
| 2052 | }; |
| 2053 | |
| 2054 | |
| 2055 | /* |
| 2056 | sysfs - snapshots |
| 2057 | */ |
| 2058 | |
| 2059 | static ssize_t rbd_snap_size_show(struct device *dev, |
| 2060 | struct device_attribute *attr, |
| 2061 | char *buf) |
| 2062 | { |
| 2063 | struct rbd_snap *snap = container_of(dev, struct rbd_snap, dev); |
| 2064 | |
Josh Durgin | 3591538 | 2011-12-05 18:25:13 -0800 | [diff] [blame] | 2065 | return sprintf(buf, "%llu\n", (unsigned long long)snap->size); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 2066 | } |
| 2067 | |
| 2068 | static ssize_t rbd_snap_id_show(struct device *dev, |
| 2069 | struct device_attribute *attr, |
| 2070 | char *buf) |
| 2071 | { |
| 2072 | struct rbd_snap *snap = container_of(dev, struct rbd_snap, dev); |
| 2073 | |
Josh Durgin | 3591538 | 2011-12-05 18:25:13 -0800 | [diff] [blame] | 2074 | return sprintf(buf, "%llu\n", (unsigned long long)snap->id); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 2075 | } |
| 2076 | |
| 2077 | static DEVICE_ATTR(snap_size, S_IRUGO, rbd_snap_size_show, NULL); |
| 2078 | static DEVICE_ATTR(snap_id, S_IRUGO, rbd_snap_id_show, NULL); |
| 2079 | |
| 2080 | static struct attribute *rbd_snap_attrs[] = { |
| 2081 | &dev_attr_snap_size.attr, |
| 2082 | &dev_attr_snap_id.attr, |
| 2083 | NULL, |
| 2084 | }; |
| 2085 | |
| 2086 | static struct attribute_group rbd_snap_attr_group = { |
| 2087 | .attrs = rbd_snap_attrs, |
| 2088 | }; |
| 2089 | |
| 2090 | static void rbd_snap_dev_release(struct device *dev) |
| 2091 | { |
| 2092 | struct rbd_snap *snap = container_of(dev, struct rbd_snap, dev); |
| 2093 | kfree(snap->name); |
| 2094 | kfree(snap); |
| 2095 | } |
| 2096 | |
| 2097 | static const struct attribute_group *rbd_snap_attr_groups[] = { |
| 2098 | &rbd_snap_attr_group, |
| 2099 | NULL |
| 2100 | }; |
| 2101 | |
| 2102 | static struct device_type rbd_snap_device_type = { |
| 2103 | .groups = rbd_snap_attr_groups, |
| 2104 | .release = rbd_snap_dev_release, |
| 2105 | }; |
| 2106 | |
Alex Elder | 14e7085 | 2012-07-19 09:09:27 -0500 | [diff] [blame] | 2107 | static void __rbd_remove_snap_dev(struct rbd_snap *snap) |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 2108 | { |
| 2109 | list_del(&snap->node); |
| 2110 | device_unregister(&snap->dev); |
| 2111 | } |
| 2112 | |
Alex Elder | 14e7085 | 2012-07-19 09:09:27 -0500 | [diff] [blame] | 2113 | static int rbd_register_snap_dev(struct rbd_snap *snap, |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 2114 | struct device *parent) |
| 2115 | { |
| 2116 | struct device *dev = &snap->dev; |
| 2117 | int ret; |
| 2118 | |
| 2119 | dev->type = &rbd_snap_device_type; |
| 2120 | dev->parent = parent; |
| 2121 | dev->release = rbd_snap_dev_release; |
| 2122 | dev_set_name(dev, "snap_%s", snap->name); |
| 2123 | ret = device_register(dev); |
| 2124 | |
| 2125 | return ret; |
| 2126 | } |
| 2127 | |
Alex Elder | 4e891e0 | 2012-07-10 20:30:10 -0500 | [diff] [blame] | 2128 | static struct rbd_snap *__rbd_add_snap_dev(struct rbd_device *rbd_dev, |
| 2129 | int i, const char *name) |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 2130 | { |
Alex Elder | 4e891e0 | 2012-07-10 20:30:10 -0500 | [diff] [blame] | 2131 | struct rbd_snap *snap; |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 2132 | int ret; |
Alex Elder | 4e891e0 | 2012-07-10 20:30:10 -0500 | [diff] [blame] | 2133 | |
| 2134 | snap = kzalloc(sizeof (*snap), GFP_KERNEL); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 2135 | if (!snap) |
Alex Elder | 4e891e0 | 2012-07-10 20:30:10 -0500 | [diff] [blame] | 2136 | return ERR_PTR(-ENOMEM); |
| 2137 | |
| 2138 | ret = -ENOMEM; |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 2139 | snap->name = kstrdup(name, GFP_KERNEL); |
Alex Elder | 4e891e0 | 2012-07-10 20:30:10 -0500 | [diff] [blame] | 2140 | if (!snap->name) |
| 2141 | goto err; |
| 2142 | |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 2143 | snap->size = rbd_dev->header.snap_sizes[i]; |
| 2144 | snap->id = rbd_dev->header.snapc->snaps[i]; |
| 2145 | if (device_is_registered(&rbd_dev->dev)) { |
Alex Elder | 14e7085 | 2012-07-19 09:09:27 -0500 | [diff] [blame] | 2146 | ret = rbd_register_snap_dev(snap, &rbd_dev->dev); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 2147 | if (ret < 0) |
| 2148 | goto err; |
| 2149 | } |
Alex Elder | 4e891e0 | 2012-07-10 20:30:10 -0500 | [diff] [blame] | 2150 | |
| 2151 | return snap; |
| 2152 | |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 2153 | err: |
| 2154 | kfree(snap->name); |
| 2155 | kfree(snap); |
Alex Elder | 4e891e0 | 2012-07-10 20:30:10 -0500 | [diff] [blame] | 2156 | |
| 2157 | return ERR_PTR(ret); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 2158 | } |
| 2159 | |
| 2160 | /* |
Alex Elder | 3593815 | 2012-08-02 11:29:46 -0500 | [diff] [blame] | 2161 | * Scan the rbd device's current snapshot list and compare it to the |
| 2162 | * newly-received snapshot context. Remove any existing snapshots |
| 2163 | * not present in the new snapshot context. Add a new snapshot for |
| 2164 | * any snaphots in the snapshot context not in the current list. |
| 2165 | * And verify there are no changes to snapshots we already know |
| 2166 | * about. |
| 2167 | * |
| 2168 | * Assumes the snapshots in the snapshot context are sorted by |
| 2169 | * snapshot id, highest id first. (Snapshots in the rbd_dev's list |
| 2170 | * are also maintained in that order.) |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 2171 | */ |
| 2172 | static int __rbd_init_snaps_header(struct rbd_device *rbd_dev) |
| 2173 | { |
Alex Elder | 3593815 | 2012-08-02 11:29:46 -0500 | [diff] [blame] | 2174 | struct ceph_snap_context *snapc = rbd_dev->header.snapc; |
| 2175 | const u32 snap_count = snapc->num_snaps; |
| 2176 | char *snap_name = rbd_dev->header.snap_names; |
| 2177 | struct list_head *head = &rbd_dev->snaps; |
| 2178 | struct list_head *links = head->next; |
| 2179 | u32 index = 0; |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 2180 | |
Alex Elder | 3593815 | 2012-08-02 11:29:46 -0500 | [diff] [blame] | 2181 | while (index < snap_count || links != head) { |
| 2182 | u64 snap_id; |
| 2183 | struct rbd_snap *snap; |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 2184 | |
Alex Elder | 3593815 | 2012-08-02 11:29:46 -0500 | [diff] [blame] | 2185 | snap_id = index < snap_count ? snapc->snaps[index] |
| 2186 | : CEPH_NOSNAP; |
| 2187 | snap = links != head ? list_entry(links, struct rbd_snap, node) |
| 2188 | : NULL; |
| 2189 | BUG_ON(snap && snap->id == CEPH_NOSNAP); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 2190 | |
Alex Elder | 3593815 | 2012-08-02 11:29:46 -0500 | [diff] [blame] | 2191 | if (snap_id == CEPH_NOSNAP || (snap && snap->id > snap_id)) { |
| 2192 | struct list_head *next = links->next; |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 2193 | |
Alex Elder | 3593815 | 2012-08-02 11:29:46 -0500 | [diff] [blame] | 2194 | /* Existing snapshot not in the new snap context */ |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 2195 | |
Alex Elder | 3593815 | 2012-08-02 11:29:46 -0500 | [diff] [blame] | 2196 | if (rbd_dev->snap_id == snap->id) |
Josh Durgin | e88a36e | 2011-11-21 18:14:25 -0800 | [diff] [blame] | 2197 | rbd_dev->snap_exists = false; |
Alex Elder | 3593815 | 2012-08-02 11:29:46 -0500 | [diff] [blame] | 2198 | __rbd_remove_snap_dev(snap); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 2199 | |
Alex Elder | 3593815 | 2012-08-02 11:29:46 -0500 | [diff] [blame] | 2200 | /* Done with this list entry; advance */ |
| 2201 | |
| 2202 | links = next; |
| 2203 | continue; |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 2204 | } |
Alex Elder | 3593815 | 2012-08-02 11:29:46 -0500 | [diff] [blame] | 2205 | |
| 2206 | if (!snap || (snap_id != CEPH_NOSNAP && snap->id < snap_id)) { |
| 2207 | struct rbd_snap *new_snap; |
| 2208 | |
| 2209 | /* We haven't seen this snapshot before */ |
| 2210 | |
| 2211 | new_snap = __rbd_add_snap_dev(rbd_dev, index, |
| 2212 | snap_name); |
| 2213 | if (IS_ERR(new_snap)) |
| 2214 | return PTR_ERR(new_snap); |
| 2215 | |
| 2216 | /* New goes before existing, or at end of list */ |
| 2217 | |
| 2218 | if (snap) |
| 2219 | list_add_tail(&new_snap->node, &snap->node); |
| 2220 | else |
Alex Elder | 523f325 | 2012-08-30 00:16:37 -0500 | [diff] [blame] | 2221 | list_add_tail(&new_snap->node, head); |
Alex Elder | 3593815 | 2012-08-02 11:29:46 -0500 | [diff] [blame] | 2222 | } else { |
| 2223 | /* Already have this one */ |
| 2224 | |
| 2225 | BUG_ON(snap->size != rbd_dev->header.snap_sizes[index]); |
| 2226 | BUG_ON(strcmp(snap->name, snap_name)); |
| 2227 | |
| 2228 | /* Done with this list entry; advance */ |
| 2229 | |
| 2230 | links = links->next; |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 2231 | } |
Alex Elder | 3593815 | 2012-08-02 11:29:46 -0500 | [diff] [blame] | 2232 | |
| 2233 | /* Advance to the next entry in the snapshot context */ |
| 2234 | |
| 2235 | index++; |
| 2236 | snap_name += strlen(snap_name) + 1; |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 2237 | } |
| 2238 | |
| 2239 | return 0; |
| 2240 | } |
| 2241 | |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 2242 | static int rbd_bus_add_dev(struct rbd_device *rbd_dev) |
| 2243 | { |
Alex Elder | f0f8cef | 2012-01-29 13:57:44 -0600 | [diff] [blame] | 2244 | int ret; |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 2245 | struct device *dev; |
| 2246 | struct rbd_snap *snap; |
| 2247 | |
| 2248 | mutex_lock_nested(&ctl_mutex, SINGLE_DEPTH_NESTING); |
| 2249 | dev = &rbd_dev->dev; |
| 2250 | |
| 2251 | dev->bus = &rbd_bus_type; |
| 2252 | dev->type = &rbd_device_type; |
| 2253 | dev->parent = &rbd_root_dev; |
| 2254 | dev->release = rbd_dev_release; |
Alex Elder | de71a29 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 2255 | dev_set_name(dev, "%d", rbd_dev->dev_id); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 2256 | ret = device_register(dev); |
| 2257 | if (ret < 0) |
Alex Elder | f0f8cef | 2012-01-29 13:57:44 -0600 | [diff] [blame] | 2258 | goto out; |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 2259 | |
| 2260 | list_for_each_entry(snap, &rbd_dev->snaps, node) { |
Alex Elder | 14e7085 | 2012-07-19 09:09:27 -0500 | [diff] [blame] | 2261 | ret = rbd_register_snap_dev(snap, &rbd_dev->dev); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 2262 | if (ret < 0) |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 2263 | break; |
| 2264 | } |
Alex Elder | f0f8cef | 2012-01-29 13:57:44 -0600 | [diff] [blame] | 2265 | out: |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 2266 | mutex_unlock(&ctl_mutex); |
| 2267 | return ret; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 2268 | } |
| 2269 | |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 2270 | static void rbd_bus_del_dev(struct rbd_device *rbd_dev) |
| 2271 | { |
| 2272 | device_unregister(&rbd_dev->dev); |
| 2273 | } |
| 2274 | |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 2275 | static int rbd_init_watch_dev(struct rbd_device *rbd_dev) |
| 2276 | { |
| 2277 | int ret, rc; |
| 2278 | |
| 2279 | do { |
Alex Elder | 0e6f322 | 2012-07-25 09:32:40 -0500 | [diff] [blame] | 2280 | ret = rbd_req_sync_watch(rbd_dev); |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 2281 | if (ret == -ERANGE) { |
Alex Elder | 1fe5e99 | 2012-07-25 09:32:41 -0500 | [diff] [blame] | 2282 | rc = rbd_refresh_header(rbd_dev, NULL); |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 2283 | if (rc < 0) |
| 2284 | return rc; |
| 2285 | } |
| 2286 | } while (ret == -ERANGE); |
| 2287 | |
| 2288 | return ret; |
| 2289 | } |
| 2290 | |
Alex Elder | 1ddbe94 | 2012-01-29 13:57:44 -0600 | [diff] [blame] | 2291 | static atomic64_t rbd_id_max = ATOMIC64_INIT(0); |
| 2292 | |
| 2293 | /* |
Alex Elder | 499afd5 | 2012-02-02 08:13:29 -0600 | [diff] [blame] | 2294 | * Get a unique rbd identifier for the given new rbd_dev, and add |
| 2295 | * the rbd_dev to the global list. The minimum rbd id is 1. |
Alex Elder | 1ddbe94 | 2012-01-29 13:57:44 -0600 | [diff] [blame] | 2296 | */ |
Alex Elder | 499afd5 | 2012-02-02 08:13:29 -0600 | [diff] [blame] | 2297 | static void rbd_id_get(struct rbd_device *rbd_dev) |
Alex Elder | b7f23c3 | 2012-01-29 13:57:43 -0600 | [diff] [blame] | 2298 | { |
Alex Elder | de71a29 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 2299 | rbd_dev->dev_id = atomic64_inc_return(&rbd_id_max); |
Alex Elder | 499afd5 | 2012-02-02 08:13:29 -0600 | [diff] [blame] | 2300 | |
| 2301 | spin_lock(&rbd_dev_list_lock); |
| 2302 | list_add_tail(&rbd_dev->node, &rbd_dev_list); |
| 2303 | spin_unlock(&rbd_dev_list_lock); |
Alex Elder | 1ddbe94 | 2012-01-29 13:57:44 -0600 | [diff] [blame] | 2304 | } |
Alex Elder | b7f23c3 | 2012-01-29 13:57:43 -0600 | [diff] [blame] | 2305 | |
Alex Elder | 1ddbe94 | 2012-01-29 13:57:44 -0600 | [diff] [blame] | 2306 | /* |
Alex Elder | 499afd5 | 2012-02-02 08:13:29 -0600 | [diff] [blame] | 2307 | * Remove an rbd_dev from the global list, and record that its |
| 2308 | * identifier is no longer in use. |
Alex Elder | 1ddbe94 | 2012-01-29 13:57:44 -0600 | [diff] [blame] | 2309 | */ |
Alex Elder | 499afd5 | 2012-02-02 08:13:29 -0600 | [diff] [blame] | 2310 | static void rbd_id_put(struct rbd_device *rbd_dev) |
Alex Elder | 1ddbe94 | 2012-01-29 13:57:44 -0600 | [diff] [blame] | 2311 | { |
Alex Elder | d184f6b | 2012-01-29 13:57:44 -0600 | [diff] [blame] | 2312 | struct list_head *tmp; |
Alex Elder | de71a29 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 2313 | int rbd_id = rbd_dev->dev_id; |
Alex Elder | d184f6b | 2012-01-29 13:57:44 -0600 | [diff] [blame] | 2314 | int max_id; |
| 2315 | |
| 2316 | BUG_ON(rbd_id < 1); |
Alex Elder | 499afd5 | 2012-02-02 08:13:29 -0600 | [diff] [blame] | 2317 | |
| 2318 | spin_lock(&rbd_dev_list_lock); |
| 2319 | list_del_init(&rbd_dev->node); |
Alex Elder | d184f6b | 2012-01-29 13:57:44 -0600 | [diff] [blame] | 2320 | |
| 2321 | /* |
| 2322 | * If the id being "put" is not the current maximum, there |
| 2323 | * is nothing special we need to do. |
| 2324 | */ |
| 2325 | if (rbd_id != atomic64_read(&rbd_id_max)) { |
| 2326 | spin_unlock(&rbd_dev_list_lock); |
| 2327 | return; |
| 2328 | } |
| 2329 | |
| 2330 | /* |
| 2331 | * We need to update the current maximum id. Search the |
| 2332 | * list to find out what it is. We're more likely to find |
| 2333 | * the maximum at the end, so search the list backward. |
| 2334 | */ |
| 2335 | max_id = 0; |
| 2336 | list_for_each_prev(tmp, &rbd_dev_list) { |
| 2337 | struct rbd_device *rbd_dev; |
| 2338 | |
| 2339 | rbd_dev = list_entry(tmp, struct rbd_device, node); |
| 2340 | if (rbd_id > max_id) |
| 2341 | max_id = rbd_id; |
| 2342 | } |
Alex Elder | 499afd5 | 2012-02-02 08:13:29 -0600 | [diff] [blame] | 2343 | spin_unlock(&rbd_dev_list_lock); |
Alex Elder | b7f23c3 | 2012-01-29 13:57:43 -0600 | [diff] [blame] | 2344 | |
Alex Elder | 1ddbe94 | 2012-01-29 13:57:44 -0600 | [diff] [blame] | 2345 | /* |
Alex Elder | d184f6b | 2012-01-29 13:57:44 -0600 | [diff] [blame] | 2346 | * The max id could have been updated by rbd_id_get(), in |
| 2347 | * which case it now accurately reflects the new maximum. |
| 2348 | * Be careful not to overwrite the maximum value in that |
| 2349 | * case. |
Alex Elder | 1ddbe94 | 2012-01-29 13:57:44 -0600 | [diff] [blame] | 2350 | */ |
Alex Elder | d184f6b | 2012-01-29 13:57:44 -0600 | [diff] [blame] | 2351 | atomic64_cmpxchg(&rbd_id_max, rbd_id, max_id); |
Alex Elder | b7f23c3 | 2012-01-29 13:57:43 -0600 | [diff] [blame] | 2352 | } |
| 2353 | |
Alex Elder | a725f65e | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 2354 | /* |
Alex Elder | e28fff26 | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 2355 | * Skips over white space at *buf, and updates *buf to point to the |
| 2356 | * first found non-space character (if any). Returns the length of |
Alex Elder | 593a9e7 | 2012-02-07 12:03:37 -0600 | [diff] [blame] | 2357 | * the token (string of non-white space characters) found. Note |
| 2358 | * that *buf must be terminated with '\0'. |
Alex Elder | e28fff26 | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 2359 | */ |
| 2360 | static inline size_t next_token(const char **buf) |
| 2361 | { |
| 2362 | /* |
| 2363 | * These are the characters that produce nonzero for |
| 2364 | * isspace() in the "C" and "POSIX" locales. |
| 2365 | */ |
| 2366 | const char *spaces = " \f\n\r\t\v"; |
| 2367 | |
| 2368 | *buf += strspn(*buf, spaces); /* Find start of token */ |
| 2369 | |
| 2370 | return strcspn(*buf, spaces); /* Return token length */ |
| 2371 | } |
| 2372 | |
| 2373 | /* |
| 2374 | * Finds the next token in *buf, and if the provided token buffer is |
| 2375 | * big enough, copies the found token into it. The result, if |
Alex Elder | 593a9e7 | 2012-02-07 12:03:37 -0600 | [diff] [blame] | 2376 | * copied, is guaranteed to be terminated with '\0'. Note that *buf |
| 2377 | * must be terminated with '\0' on entry. |
Alex Elder | e28fff26 | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 2378 | * |
| 2379 | * Returns the length of the token found (not including the '\0'). |
| 2380 | * Return value will be 0 if no token is found, and it will be >= |
| 2381 | * token_size if the token would not fit. |
| 2382 | * |
Alex Elder | 593a9e7 | 2012-02-07 12:03:37 -0600 | [diff] [blame] | 2383 | * The *buf pointer will be updated to point beyond the end of the |
Alex Elder | e28fff26 | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 2384 | * found token. Note that this occurs even if the token buffer is |
| 2385 | * too small to hold it. |
| 2386 | */ |
| 2387 | static inline size_t copy_token(const char **buf, |
| 2388 | char *token, |
| 2389 | size_t token_size) |
| 2390 | { |
| 2391 | size_t len; |
| 2392 | |
| 2393 | len = next_token(buf); |
| 2394 | if (len < token_size) { |
| 2395 | memcpy(token, *buf, len); |
| 2396 | *(token + len) = '\0'; |
| 2397 | } |
| 2398 | *buf += len; |
| 2399 | |
| 2400 | return len; |
| 2401 | } |
| 2402 | |
| 2403 | /* |
Alex Elder | ea3352f | 2012-07-09 21:04:23 -0500 | [diff] [blame] | 2404 | * Finds the next token in *buf, dynamically allocates a buffer big |
| 2405 | * enough to hold a copy of it, and copies the token into the new |
| 2406 | * buffer. The copy is guaranteed to be terminated with '\0'. Note |
| 2407 | * that a duplicate buffer is created even for a zero-length token. |
| 2408 | * |
| 2409 | * Returns a pointer to the newly-allocated duplicate, or a null |
| 2410 | * pointer if memory for the duplicate was not available. If |
| 2411 | * the lenp argument is a non-null pointer, the length of the token |
| 2412 | * (not including the '\0') is returned in *lenp. |
| 2413 | * |
| 2414 | * If successful, the *buf pointer will be updated to point beyond |
| 2415 | * the end of the found token. |
| 2416 | * |
| 2417 | * Note: uses GFP_KERNEL for allocation. |
| 2418 | */ |
| 2419 | static inline char *dup_token(const char **buf, size_t *lenp) |
| 2420 | { |
| 2421 | char *dup; |
| 2422 | size_t len; |
| 2423 | |
| 2424 | len = next_token(buf); |
| 2425 | dup = kmalloc(len + 1, GFP_KERNEL); |
| 2426 | if (!dup) |
| 2427 | return NULL; |
| 2428 | |
| 2429 | memcpy(dup, *buf, len); |
| 2430 | *(dup + len) = '\0'; |
| 2431 | *buf += len; |
| 2432 | |
| 2433 | if (lenp) |
| 2434 | *lenp = len; |
| 2435 | |
| 2436 | return dup; |
| 2437 | } |
| 2438 | |
| 2439 | /* |
Alex Elder | 0bed54d | 2012-07-03 16:01:18 -0500 | [diff] [blame] | 2440 | * This fills in the pool_name, image_name, image_name_len, snap_name, |
Alex Elder | a725f65e | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 2441 | * rbd_dev, rbd_md_name, and name fields of the given rbd_dev, based |
| 2442 | * on the list of monitor addresses and other options provided via |
| 2443 | * /sys/bus/rbd/add. |
Alex Elder | d22f76e | 2012-07-12 10:46:35 -0500 | [diff] [blame] | 2444 | * |
| 2445 | * Note: rbd_dev is assumed to have been initially zero-filled. |
Alex Elder | a725f65e | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 2446 | */ |
| 2447 | static int rbd_add_parse_args(struct rbd_device *rbd_dev, |
| 2448 | const char *buf, |
Alex Elder | 7ef3214 | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 2449 | const char **mon_addrs, |
Alex Elder | 5214ecc | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 2450 | size_t *mon_addrs_size, |
Alex Elder | e28fff26 | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 2451 | char *options, |
Alex Elder | 0bed54d | 2012-07-03 16:01:18 -0500 | [diff] [blame] | 2452 | size_t options_size) |
Alex Elder | a725f65e | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 2453 | { |
Alex Elder | d22f76e | 2012-07-12 10:46:35 -0500 | [diff] [blame] | 2454 | size_t len; |
| 2455 | int ret; |
Alex Elder | e28fff26 | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 2456 | |
| 2457 | /* The first four tokens are required */ |
| 2458 | |
Alex Elder | 7ef3214 | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 2459 | len = next_token(&buf); |
| 2460 | if (!len) |
Alex Elder | a725f65e | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 2461 | return -EINVAL; |
Alex Elder | 5214ecc | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 2462 | *mon_addrs_size = len + 1; |
Alex Elder | 7ef3214 | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 2463 | *mon_addrs = buf; |
| 2464 | |
| 2465 | buf += len; |
Alex Elder | a725f65e | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 2466 | |
Alex Elder | e28fff26 | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 2467 | len = copy_token(&buf, options, options_size); |
| 2468 | if (!len || len >= options_size) |
| 2469 | return -EINVAL; |
Alex Elder | a725f65e | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 2470 | |
Alex Elder | bf3e5ae | 2012-07-09 21:04:23 -0500 | [diff] [blame] | 2471 | ret = -ENOMEM; |
Alex Elder | d22f76e | 2012-07-12 10:46:35 -0500 | [diff] [blame] | 2472 | rbd_dev->pool_name = dup_token(&buf, NULL); |
| 2473 | if (!rbd_dev->pool_name) |
Alex Elder | d22f76e | 2012-07-12 10:46:35 -0500 | [diff] [blame] | 2474 | goto out_err; |
Alex Elder | e28fff26 | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 2475 | |
Alex Elder | 0bed54d | 2012-07-03 16:01:18 -0500 | [diff] [blame] | 2476 | rbd_dev->image_name = dup_token(&buf, &rbd_dev->image_name_len); |
| 2477 | if (!rbd_dev->image_name) |
Alex Elder | bf3e5ae | 2012-07-09 21:04:23 -0500 | [diff] [blame] | 2478 | goto out_err; |
Alex Elder | e28fff26 | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 2479 | |
Alex Elder | cb8627c | 2012-07-09 21:04:23 -0500 | [diff] [blame] | 2480 | /* Create the name of the header object */ |
| 2481 | |
Alex Elder | 0bed54d | 2012-07-03 16:01:18 -0500 | [diff] [blame] | 2482 | rbd_dev->header_name = kmalloc(rbd_dev->image_name_len |
Alex Elder | bf3e5ae | 2012-07-09 21:04:23 -0500 | [diff] [blame] | 2483 | + sizeof (RBD_SUFFIX), |
| 2484 | GFP_KERNEL); |
Alex Elder | 0bed54d | 2012-07-03 16:01:18 -0500 | [diff] [blame] | 2485 | if (!rbd_dev->header_name) |
Alex Elder | cb8627c | 2012-07-09 21:04:23 -0500 | [diff] [blame] | 2486 | goto out_err; |
Alex Elder | 0bed54d | 2012-07-03 16:01:18 -0500 | [diff] [blame] | 2487 | sprintf(rbd_dev->header_name, "%s%s", rbd_dev->image_name, RBD_SUFFIX); |
Alex Elder | a725f65e | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 2488 | |
Alex Elder | e28fff26 | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 2489 | /* |
Alex Elder | 820a5f3 | 2012-07-09 21:04:24 -0500 | [diff] [blame] | 2490 | * The snapshot name is optional. If none is is supplied, |
| 2491 | * we use the default value. |
Alex Elder | e28fff26 | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 2492 | */ |
Alex Elder | 820a5f3 | 2012-07-09 21:04:24 -0500 | [diff] [blame] | 2493 | rbd_dev->snap_name = dup_token(&buf, &len); |
| 2494 | if (!rbd_dev->snap_name) |
| 2495 | goto out_err; |
| 2496 | if (!len) { |
| 2497 | /* Replace the empty name with the default */ |
| 2498 | kfree(rbd_dev->snap_name); |
| 2499 | rbd_dev->snap_name |
| 2500 | = kmalloc(sizeof (RBD_SNAP_HEAD_NAME), GFP_KERNEL); |
| 2501 | if (!rbd_dev->snap_name) |
| 2502 | goto out_err; |
| 2503 | |
Alex Elder | e28fff26 | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 2504 | memcpy(rbd_dev->snap_name, RBD_SNAP_HEAD_NAME, |
| 2505 | sizeof (RBD_SNAP_HEAD_NAME)); |
Alex Elder | 849b426 | 2012-07-09 21:04:24 -0500 | [diff] [blame] | 2506 | } |
Alex Elder | e28fff26 | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 2507 | |
Alex Elder | a725f65e | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 2508 | return 0; |
Alex Elder | d22f76e | 2012-07-12 10:46:35 -0500 | [diff] [blame] | 2509 | |
| 2510 | out_err: |
Alex Elder | 0bed54d | 2012-07-03 16:01:18 -0500 | [diff] [blame] | 2511 | kfree(rbd_dev->header_name); |
Alex Elder | d78fd7a | 2012-07-26 23:37:14 -0500 | [diff] [blame] | 2512 | rbd_dev->header_name = NULL; |
Alex Elder | 0bed54d | 2012-07-03 16:01:18 -0500 | [diff] [blame] | 2513 | kfree(rbd_dev->image_name); |
Alex Elder | d78fd7a | 2012-07-26 23:37:14 -0500 | [diff] [blame] | 2514 | rbd_dev->image_name = NULL; |
| 2515 | rbd_dev->image_name_len = 0; |
Alex Elder | d22f76e | 2012-07-12 10:46:35 -0500 | [diff] [blame] | 2516 | kfree(rbd_dev->pool_name); |
| 2517 | rbd_dev->pool_name = NULL; |
| 2518 | |
| 2519 | return ret; |
Alex Elder | a725f65e | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 2520 | } |
| 2521 | |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 2522 | static ssize_t rbd_add(struct bus_type *bus, |
| 2523 | const char *buf, |
| 2524 | size_t count) |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 2525 | { |
Alex Elder | cb8627c | 2012-07-09 21:04:23 -0500 | [diff] [blame] | 2526 | char *options; |
| 2527 | struct rbd_device *rbd_dev = NULL; |
Alex Elder | 7ef3214 | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 2528 | const char *mon_addrs = NULL; |
| 2529 | size_t mon_addrs_size = 0; |
Alex Elder | 27cc259 | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 2530 | struct ceph_osd_client *osdc; |
| 2531 | int rc = -ENOMEM; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 2532 | |
| 2533 | if (!try_module_get(THIS_MODULE)) |
| 2534 | return -ENODEV; |
| 2535 | |
Alex Elder | 27cc259 | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 2536 | options = kmalloc(count, GFP_KERNEL); |
| 2537 | if (!options) |
| 2538 | goto err_nomem; |
Alex Elder | cb8627c | 2012-07-09 21:04:23 -0500 | [diff] [blame] | 2539 | rbd_dev = kzalloc(sizeof(*rbd_dev), GFP_KERNEL); |
| 2540 | if (!rbd_dev) |
| 2541 | goto err_nomem; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 2542 | |
| 2543 | /* static rbd_device initialization */ |
| 2544 | spin_lock_init(&rbd_dev->lock); |
| 2545 | INIT_LIST_HEAD(&rbd_dev->node); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 2546 | INIT_LIST_HEAD(&rbd_dev->snaps); |
Josh Durgin | c666601 | 2011-11-21 17:11:12 -0800 | [diff] [blame] | 2547 | init_rwsem(&rbd_dev->header_rwsem); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 2548 | |
Alex Elder | d184f6b | 2012-01-29 13:57:44 -0600 | [diff] [blame] | 2549 | /* generate unique id: find highest unique id, add one */ |
Alex Elder | 499afd5 | 2012-02-02 08:13:29 -0600 | [diff] [blame] | 2550 | rbd_id_get(rbd_dev); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 2551 | |
Alex Elder | a725f65e | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 2552 | /* Fill in the device name, now that we have its id. */ |
Alex Elder | 81a8979 | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 2553 | BUILD_BUG_ON(DEV_NAME_LEN |
| 2554 | < sizeof (RBD_DRV_NAME) + MAX_INT_FORMAT_WIDTH); |
Alex Elder | de71a29 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 2555 | sprintf(rbd_dev->name, "%s%d", RBD_DRV_NAME, rbd_dev->dev_id); |
Alex Elder | e124a82f | 2012-01-29 13:57:44 -0600 | [diff] [blame] | 2556 | |
Alex Elder | a725f65e | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 2557 | /* parse add command */ |
Alex Elder | 7ef3214 | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 2558 | rc = rbd_add_parse_args(rbd_dev, buf, &mon_addrs, &mon_addrs_size, |
Alex Elder | e28fff26 | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 2559 | options, count); |
Alex Elder | a725f65e | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 2560 | if (rc) |
| 2561 | goto err_put_id; |
| 2562 | |
Alex Elder | f8c3892 | 2012-08-10 13:12:07 -0700 | [diff] [blame] | 2563 | rc = rbd_get_client(rbd_dev, mon_addrs, mon_addrs_size - 1, options); |
| 2564 | if (rc < 0) |
Alex Elder | f0f8cef | 2012-01-29 13:57:44 -0600 | [diff] [blame] | 2565 | goto err_put_id; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 2566 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 2567 | /* pick the pool */ |
Alex Elder | 1dbb439 | 2012-01-24 10:08:37 -0600 | [diff] [blame] | 2568 | osdc = &rbd_dev->rbd_client->client->osdc; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 2569 | rc = ceph_pg_poolid_by_name(osdc->osdmap, rbd_dev->pool_name); |
| 2570 | if (rc < 0) |
| 2571 | goto err_out_client; |
Alex Elder | 9bb2f33 | 2012-07-12 10:46:35 -0500 | [diff] [blame] | 2572 | rbd_dev->pool_id = rc; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 2573 | |
| 2574 | /* register our block device */ |
Alex Elder | 27cc259 | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 2575 | rc = register_blkdev(0, rbd_dev->name); |
| 2576 | if (rc < 0) |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 2577 | goto err_out_client; |
Alex Elder | 27cc259 | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 2578 | rbd_dev->major = rc; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 2579 | |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 2580 | rc = rbd_bus_add_dev(rbd_dev); |
| 2581 | if (rc) |
Yehuda Sadeh | 766fc43 | 2011-01-07 14:58:42 -0800 | [diff] [blame] | 2582 | goto err_out_blkdev; |
| 2583 | |
Alex Elder | 32eec68 | 2012-02-08 16:11:14 -0600 | [diff] [blame] | 2584 | /* |
| 2585 | * At this point cleanup in the event of an error is the job |
| 2586 | * of the sysfs code (initiated by rbd_bus_del_dev()). |
| 2587 | * |
| 2588 | * Set up and announce blkdev mapping. |
| 2589 | */ |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 2590 | rc = rbd_init_disk(rbd_dev); |
| 2591 | if (rc) |
Yehuda Sadeh | 766fc43 | 2011-01-07 14:58:42 -0800 | [diff] [blame] | 2592 | goto err_out_bus; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 2593 | |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 2594 | rc = rbd_init_watch_dev(rbd_dev); |
| 2595 | if (rc) |
| 2596 | goto err_out_bus; |
| 2597 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 2598 | return count; |
| 2599 | |
Yehuda Sadeh | 766fc43 | 2011-01-07 14:58:42 -0800 | [diff] [blame] | 2600 | err_out_bus: |
Yehuda Sadeh | 766fc43 | 2011-01-07 14:58:42 -0800 | [diff] [blame] | 2601 | /* this will also clean up rest of rbd_dev stuff */ |
| 2602 | |
| 2603 | rbd_bus_del_dev(rbd_dev); |
| 2604 | kfree(options); |
Yehuda Sadeh | 766fc43 | 2011-01-07 14:58:42 -0800 | [diff] [blame] | 2605 | return rc; |
| 2606 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 2607 | err_out_blkdev: |
| 2608 | unregister_blkdev(rbd_dev->major, rbd_dev->name); |
| 2609 | err_out_client: |
| 2610 | rbd_put_client(rbd_dev); |
Alex Elder | f0f8cef | 2012-01-29 13:57:44 -0600 | [diff] [blame] | 2611 | err_put_id: |
Alex Elder | cb8627c | 2012-07-09 21:04:23 -0500 | [diff] [blame] | 2612 | if (rbd_dev->pool_name) { |
Alex Elder | 820a5f3 | 2012-07-09 21:04:24 -0500 | [diff] [blame] | 2613 | kfree(rbd_dev->snap_name); |
Alex Elder | 0bed54d | 2012-07-03 16:01:18 -0500 | [diff] [blame] | 2614 | kfree(rbd_dev->header_name); |
| 2615 | kfree(rbd_dev->image_name); |
Alex Elder | cb8627c | 2012-07-09 21:04:23 -0500 | [diff] [blame] | 2616 | kfree(rbd_dev->pool_name); |
| 2617 | } |
Alex Elder | 499afd5 | 2012-02-02 08:13:29 -0600 | [diff] [blame] | 2618 | rbd_id_put(rbd_dev); |
Alex Elder | 27cc259 | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 2619 | err_nomem: |
Alex Elder | 27cc259 | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 2620 | kfree(rbd_dev); |
Alex Elder | cb8627c | 2012-07-09 21:04:23 -0500 | [diff] [blame] | 2621 | kfree(options); |
Alex Elder | 27cc259 | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 2622 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 2623 | dout("Error adding device %s\n", buf); |
| 2624 | module_put(THIS_MODULE); |
Alex Elder | 27cc259 | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 2625 | |
| 2626 | return (ssize_t) rc; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 2627 | } |
| 2628 | |
Alex Elder | de71a29 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 2629 | static struct rbd_device *__rbd_get_dev(unsigned long dev_id) |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 2630 | { |
| 2631 | struct list_head *tmp; |
| 2632 | struct rbd_device *rbd_dev; |
| 2633 | |
Alex Elder | e124a82f | 2012-01-29 13:57:44 -0600 | [diff] [blame] | 2634 | spin_lock(&rbd_dev_list_lock); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 2635 | list_for_each(tmp, &rbd_dev_list) { |
| 2636 | rbd_dev = list_entry(tmp, struct rbd_device, node); |
Alex Elder | de71a29 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 2637 | if (rbd_dev->dev_id == dev_id) { |
Alex Elder | e124a82f | 2012-01-29 13:57:44 -0600 | [diff] [blame] | 2638 | spin_unlock(&rbd_dev_list_lock); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 2639 | return rbd_dev; |
Alex Elder | e124a82f | 2012-01-29 13:57:44 -0600 | [diff] [blame] | 2640 | } |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 2641 | } |
Alex Elder | e124a82f | 2012-01-29 13:57:44 -0600 | [diff] [blame] | 2642 | spin_unlock(&rbd_dev_list_lock); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 2643 | return NULL; |
| 2644 | } |
| 2645 | |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 2646 | static void rbd_dev_release(struct device *dev) |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 2647 | { |
Alex Elder | 593a9e7 | 2012-02-07 12:03:37 -0600 | [diff] [blame] | 2648 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 2649 | |
Alex Elder | 1dbb439 | 2012-01-24 10:08:37 -0600 | [diff] [blame] | 2650 | if (rbd_dev->watch_request) { |
| 2651 | struct ceph_client *client = rbd_dev->rbd_client->client; |
| 2652 | |
| 2653 | ceph_osdc_unregister_linger_request(&client->osdc, |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 2654 | rbd_dev->watch_request); |
Alex Elder | 1dbb439 | 2012-01-24 10:08:37 -0600 | [diff] [blame] | 2655 | } |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 2656 | if (rbd_dev->watch_event) |
Alex Elder | 070c633 | 2012-07-25 09:32:41 -0500 | [diff] [blame] | 2657 | rbd_req_sync_unwatch(rbd_dev); |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 2658 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 2659 | rbd_put_client(rbd_dev); |
| 2660 | |
| 2661 | /* clean up and free blkdev */ |
| 2662 | rbd_free_disk(rbd_dev); |
| 2663 | unregister_blkdev(rbd_dev->major, rbd_dev->name); |
Alex Elder | 32eec68 | 2012-02-08 16:11:14 -0600 | [diff] [blame] | 2664 | |
| 2665 | /* done with the id, and with the rbd_dev */ |
Alex Elder | 820a5f3 | 2012-07-09 21:04:24 -0500 | [diff] [blame] | 2666 | kfree(rbd_dev->snap_name); |
Alex Elder | 0bed54d | 2012-07-03 16:01:18 -0500 | [diff] [blame] | 2667 | kfree(rbd_dev->header_name); |
Alex Elder | d22f76e | 2012-07-12 10:46:35 -0500 | [diff] [blame] | 2668 | kfree(rbd_dev->pool_name); |
Alex Elder | 0bed54d | 2012-07-03 16:01:18 -0500 | [diff] [blame] | 2669 | kfree(rbd_dev->image_name); |
Alex Elder | 32eec68 | 2012-02-08 16:11:14 -0600 | [diff] [blame] | 2670 | rbd_id_put(rbd_dev); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 2671 | kfree(rbd_dev); |
| 2672 | |
| 2673 | /* release module ref */ |
| 2674 | module_put(THIS_MODULE); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 2675 | } |
| 2676 | |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 2677 | static ssize_t rbd_remove(struct bus_type *bus, |
| 2678 | const char *buf, |
| 2679 | size_t count) |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 2680 | { |
| 2681 | struct rbd_device *rbd_dev = NULL; |
| 2682 | int target_id, rc; |
| 2683 | unsigned long ul; |
| 2684 | int ret = count; |
| 2685 | |
| 2686 | rc = strict_strtoul(buf, 10, &ul); |
| 2687 | if (rc) |
| 2688 | return rc; |
| 2689 | |
| 2690 | /* convert to int; abort if we lost anything in the conversion */ |
| 2691 | target_id = (int) ul; |
| 2692 | if (target_id != ul) |
| 2693 | return -EINVAL; |
| 2694 | |
| 2695 | mutex_lock_nested(&ctl_mutex, SINGLE_DEPTH_NESTING); |
| 2696 | |
| 2697 | rbd_dev = __rbd_get_dev(target_id); |
| 2698 | if (!rbd_dev) { |
| 2699 | ret = -ENOENT; |
| 2700 | goto done; |
| 2701 | } |
| 2702 | |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 2703 | __rbd_remove_all_snaps(rbd_dev); |
| 2704 | rbd_bus_del_dev(rbd_dev); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 2705 | |
| 2706 | done: |
| 2707 | mutex_unlock(&ctl_mutex); |
| 2708 | return ret; |
| 2709 | } |
| 2710 | |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 2711 | static ssize_t rbd_snap_add(struct device *dev, |
| 2712 | struct device_attribute *attr, |
| 2713 | const char *buf, |
| 2714 | size_t count) |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 2715 | { |
Alex Elder | 593a9e7 | 2012-02-07 12:03:37 -0600 | [diff] [blame] | 2716 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 2717 | int ret; |
| 2718 | char *name = kmalloc(count + 1, GFP_KERNEL); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 2719 | if (!name) |
| 2720 | return -ENOMEM; |
| 2721 | |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 2722 | snprintf(name, count, "%s", buf); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 2723 | |
| 2724 | mutex_lock_nested(&ctl_mutex, SINGLE_DEPTH_NESTING); |
| 2725 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 2726 | ret = rbd_header_add_snap(rbd_dev, |
| 2727 | name, GFP_KERNEL); |
| 2728 | if (ret < 0) |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 2729 | goto err_unlock; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 2730 | |
Alex Elder | b813623 | 2012-07-25 09:32:41 -0500 | [diff] [blame] | 2731 | ret = __rbd_refresh_header(rbd_dev, NULL); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 2732 | if (ret < 0) |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 2733 | goto err_unlock; |
| 2734 | |
| 2735 | /* shouldn't hold ctl_mutex when notifying.. notify might |
| 2736 | trigger a watch callback that would need to get that mutex */ |
| 2737 | mutex_unlock(&ctl_mutex); |
| 2738 | |
| 2739 | /* make a best effort, don't error if failed */ |
Alex Elder | 4cb1625 | 2012-07-25 09:32:40 -0500 | [diff] [blame] | 2740 | rbd_req_sync_notify(rbd_dev); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 2741 | |
| 2742 | ret = count; |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 2743 | kfree(name); |
| 2744 | return ret; |
| 2745 | |
| 2746 | err_unlock: |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 2747 | mutex_unlock(&ctl_mutex); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 2748 | kfree(name); |
| 2749 | return ret; |
| 2750 | } |
| 2751 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 2752 | /* |
| 2753 | * create control files in sysfs |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 2754 | * /sys/bus/rbd/... |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 2755 | */ |
| 2756 | static int rbd_sysfs_init(void) |
| 2757 | { |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 2758 | int ret; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 2759 | |
Alex Elder | fed4c14 | 2012-02-07 12:03:36 -0600 | [diff] [blame] | 2760 | ret = device_register(&rbd_root_dev); |
Alex Elder | 2107978 | 2012-01-24 10:08:36 -0600 | [diff] [blame] | 2761 | if (ret < 0) |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 2762 | return ret; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 2763 | |
Alex Elder | fed4c14 | 2012-02-07 12:03:36 -0600 | [diff] [blame] | 2764 | ret = bus_register(&rbd_bus_type); |
| 2765 | if (ret < 0) |
| 2766 | device_unregister(&rbd_root_dev); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 2767 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 2768 | return ret; |
| 2769 | } |
| 2770 | |
| 2771 | static void rbd_sysfs_cleanup(void) |
| 2772 | { |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 2773 | bus_unregister(&rbd_bus_type); |
Alex Elder | fed4c14 | 2012-02-07 12:03:36 -0600 | [diff] [blame] | 2774 | device_unregister(&rbd_root_dev); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 2775 | } |
| 2776 | |
| 2777 | int __init rbd_init(void) |
| 2778 | { |
| 2779 | int rc; |
| 2780 | |
| 2781 | rc = rbd_sysfs_init(); |
| 2782 | if (rc) |
| 2783 | return rc; |
Alex Elder | f0f8cef | 2012-01-29 13:57:44 -0600 | [diff] [blame] | 2784 | pr_info("loaded " RBD_DRV_NAME_LONG "\n"); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 2785 | return 0; |
| 2786 | } |
| 2787 | |
| 2788 | void __exit rbd_exit(void) |
| 2789 | { |
| 2790 | rbd_sysfs_cleanup(); |
| 2791 | } |
| 2792 | |
| 2793 | module_init(rbd_init); |
| 2794 | module_exit(rbd_exit); |
| 2795 | |
| 2796 | MODULE_AUTHOR("Sage Weil <sage@newdream.net>"); |
| 2797 | MODULE_AUTHOR("Yehuda Sadeh <yehuda@hq.newdream.net>"); |
| 2798 | MODULE_DESCRIPTION("rados block device"); |
| 2799 | |
| 2800 | /* following authorship retained from original osdblk.c */ |
| 2801 | MODULE_AUTHOR("Jeff Garzik <jeff@garzik.org>"); |
| 2802 | |
| 2803 | MODULE_LICENSE("GPL"); |