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