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