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