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