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; |
Alex Elder | 8986cb3 | 2012-11-08 08:01:39 -0600 | [diff] [blame^] | 174 | s32 rc; |
Yehuda Sadeh | 1fec709 | 2011-05-13 13:52:56 -0700 | [diff] [blame] | 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 | f5400b7 | 2012-11-01 10:17:15 -0500 | [diff] [blame] | 996 | if (!bi) { |
| 997 | rbd_warn(NULL, "bio_chain exhausted with %u left", len); |
Alex Elder | f7760da | 2012-10-20 22:17:27 -0500 | [diff] [blame] | 998 | goto out_err; /* EINVAL; ran out of bio's */ |
Alex Elder | f5400b7 | 2012-11-01 10:17:15 -0500 | [diff] [blame] | 999 | } |
Alex Elder | f7760da | 2012-10-20 22:17:27 -0500 | [diff] [blame] | 1000 | bi_size = min_t(unsigned int, bi->bi_size - off, len); |
| 1001 | bio = bio_clone_range(bi, off, bi_size, gfpmask); |
| 1002 | if (!bio) |
| 1003 | goto out_err; /* ENOMEM */ |
| 1004 | |
| 1005 | *end = bio; |
| 1006 | end = &bio->bi_next; |
| 1007 | |
| 1008 | off += bi_size; |
| 1009 | if (off == bi->bi_size) { |
| 1010 | bi = bi->bi_next; |
| 1011 | off = 0; |
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 | len -= bi_size; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1014 | } |
Alex Elder | f7760da | 2012-10-20 22:17:27 -0500 | [diff] [blame] | 1015 | *bio_src = bi; |
| 1016 | *offset = off; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1017 | |
Alex Elder | f7760da | 2012-10-20 22:17:27 -0500 | [diff] [blame] | 1018 | return chain; |
| 1019 | out_err: |
| 1020 | bio_chain_put(chain); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1021 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1022 | return NULL; |
| 1023 | } |
| 1024 | |
| 1025 | /* |
| 1026 | * helpers for osd request op vectors. |
| 1027 | */ |
Alex Elder | 57cfc10 | 2012-06-26 12:57:03 -0700 | [diff] [blame] | 1028 | static struct ceph_osd_req_op *rbd_create_rw_ops(int num_ops, |
| 1029 | int opcode, u32 payload_len) |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1030 | { |
Alex Elder | 57cfc10 | 2012-06-26 12:57:03 -0700 | [diff] [blame] | 1031 | struct ceph_osd_req_op *ops; |
| 1032 | |
| 1033 | ops = kzalloc(sizeof (*ops) * (num_ops + 1), GFP_NOIO); |
| 1034 | if (!ops) |
| 1035 | return NULL; |
| 1036 | |
| 1037 | ops[0].op = opcode; |
| 1038 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1039 | /* |
| 1040 | * op extent offset and length will be set later on |
| 1041 | * in calc_raw_layout() |
| 1042 | */ |
Alex Elder | 57cfc10 | 2012-06-26 12:57:03 -0700 | [diff] [blame] | 1043 | ops[0].payload_len = payload_len; |
| 1044 | |
| 1045 | return ops; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1046 | } |
| 1047 | |
| 1048 | static void rbd_destroy_ops(struct ceph_osd_req_op *ops) |
| 1049 | { |
| 1050 | kfree(ops); |
| 1051 | } |
| 1052 | |
Yehuda Sadeh | 1fec709 | 2011-05-13 13:52:56 -0700 | [diff] [blame] | 1053 | static void rbd_coll_end_req_index(struct request *rq, |
| 1054 | struct rbd_req_coll *coll, |
| 1055 | int index, |
Alex Elder | 8986cb3 | 2012-11-08 08:01:39 -0600 | [diff] [blame^] | 1056 | s32 ret, u64 len) |
Yehuda Sadeh | 1fec709 | 2011-05-13 13:52:56 -0700 | [diff] [blame] | 1057 | { |
| 1058 | struct request_queue *q; |
| 1059 | int min, max, i; |
| 1060 | |
Alex Elder | bd919d4 | 2012-07-13 20:35:11 -0500 | [diff] [blame] | 1061 | dout("rbd_coll_end_req_index %p index %d ret %d len %llu\n", |
Alex Elder | 8986cb3 | 2012-11-08 08:01:39 -0600 | [diff] [blame^] | 1062 | coll, index, (int)ret, (unsigned long long)len); |
Yehuda Sadeh | 1fec709 | 2011-05-13 13:52:56 -0700 | [diff] [blame] | 1063 | |
| 1064 | if (!rq) |
| 1065 | return; |
| 1066 | |
| 1067 | if (!coll) { |
| 1068 | blk_end_request(rq, ret, len); |
| 1069 | return; |
| 1070 | } |
| 1071 | |
| 1072 | q = rq->q; |
| 1073 | |
| 1074 | spin_lock_irq(q->queue_lock); |
| 1075 | coll->status[index].done = 1; |
| 1076 | coll->status[index].rc = ret; |
| 1077 | coll->status[index].bytes = len; |
| 1078 | max = min = coll->num_done; |
| 1079 | while (max < coll->total && coll->status[max].done) |
| 1080 | max++; |
| 1081 | |
| 1082 | for (i = min; i<max; i++) { |
Alex Elder | 8986cb3 | 2012-11-08 08:01:39 -0600 | [diff] [blame^] | 1083 | __blk_end_request(rq, (int)coll->status[i].rc, |
Yehuda Sadeh | 1fec709 | 2011-05-13 13:52:56 -0700 | [diff] [blame] | 1084 | coll->status[i].bytes); |
| 1085 | coll->num_done++; |
| 1086 | kref_put(&coll->kref, rbd_coll_release); |
| 1087 | } |
| 1088 | spin_unlock_irq(q->queue_lock); |
| 1089 | } |
| 1090 | |
Alex Elder | 725afc9 | 2012-11-08 08:01:39 -0600 | [diff] [blame] | 1091 | static void rbd_coll_end_req(struct rbd_request *rbd_req, |
Alex Elder | 8986cb3 | 2012-11-08 08:01:39 -0600 | [diff] [blame^] | 1092 | s32 ret, u64 len) |
Yehuda Sadeh | 1fec709 | 2011-05-13 13:52:56 -0700 | [diff] [blame] | 1093 | { |
Alex Elder | 725afc9 | 2012-11-08 08:01:39 -0600 | [diff] [blame] | 1094 | rbd_coll_end_req_index(rbd_req->rq, |
| 1095 | rbd_req->coll, rbd_req->coll_index, |
| 1096 | ret, len); |
Yehuda Sadeh | 1fec709 | 2011-05-13 13:52:56 -0700 | [diff] [blame] | 1097 | } |
| 1098 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1099 | /* |
| 1100 | * Send ceph osd request |
| 1101 | */ |
| 1102 | static int rbd_do_request(struct request *rq, |
Alex Elder | 0ce1a79 | 2012-07-03 16:01:18 -0500 | [diff] [blame] | 1103 | struct rbd_device *rbd_dev, |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1104 | struct ceph_snap_context *snapc, |
| 1105 | u64 snapid, |
Alex Elder | aded07e | 2012-07-03 16:01:18 -0500 | [diff] [blame] | 1106 | const char *object_name, u64 ofs, u64 len, |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1107 | struct bio *bio, |
| 1108 | struct page **pages, |
| 1109 | int num_pages, |
| 1110 | int flags, |
| 1111 | struct ceph_osd_req_op *ops, |
Yehuda Sadeh | 1fec709 | 2011-05-13 13:52:56 -0700 | [diff] [blame] | 1112 | struct rbd_req_coll *coll, |
| 1113 | int coll_index, |
Alex Elder | 5f29ddd | 2012-11-08 08:01:39 -0600 | [diff] [blame] | 1114 | void (*rbd_cb)(struct ceph_osd_request *, |
| 1115 | struct ceph_msg *), |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 1116 | struct ceph_osd_request **linger_req, |
| 1117 | u64 *ver) |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1118 | { |
Alex Elder | 5f29ddd | 2012-11-08 08:01:39 -0600 | [diff] [blame] | 1119 | struct ceph_osd_request *osd_req; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1120 | struct ceph_file_layout *layout; |
| 1121 | int ret; |
| 1122 | u64 bno; |
| 1123 | struct timespec mtime = CURRENT_TIME; |
Alex Elder | 725afc9 | 2012-11-08 08:01:39 -0600 | [diff] [blame] | 1124 | struct rbd_request *rbd_req; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1125 | struct ceph_osd_request_head *reqhead; |
Alex Elder | 1dbb439 | 2012-01-24 10:08:37 -0600 | [diff] [blame] | 1126 | struct ceph_osd_client *osdc; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1127 | |
Alex Elder | 725afc9 | 2012-11-08 08:01:39 -0600 | [diff] [blame] | 1128 | rbd_req = kzalloc(sizeof(*rbd_req), GFP_NOIO); |
| 1129 | if (!rbd_req) { |
Yehuda Sadeh | 1fec709 | 2011-05-13 13:52:56 -0700 | [diff] [blame] | 1130 | if (coll) |
| 1131 | rbd_coll_end_req_index(rq, coll, coll_index, |
Alex Elder | 8986cb3 | 2012-11-08 08:01:39 -0600 | [diff] [blame^] | 1132 | (s32)-ENOMEM, len); |
Yehuda Sadeh | 1fec709 | 2011-05-13 13:52:56 -0700 | [diff] [blame] | 1133 | return -ENOMEM; |
| 1134 | } |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1135 | |
Yehuda Sadeh | 1fec709 | 2011-05-13 13:52:56 -0700 | [diff] [blame] | 1136 | if (coll) { |
Alex Elder | 725afc9 | 2012-11-08 08:01:39 -0600 | [diff] [blame] | 1137 | rbd_req->coll = coll; |
| 1138 | rbd_req->coll_index = coll_index; |
Yehuda Sadeh | 1fec709 | 2011-05-13 13:52:56 -0700 | [diff] [blame] | 1139 | } |
| 1140 | |
Alex Elder | f7760da | 2012-10-20 22:17:27 -0500 | [diff] [blame] | 1141 | dout("rbd_do_request object_name=%s ofs=%llu len=%llu coll=%p[%d]\n", |
| 1142 | object_name, (unsigned long long) ofs, |
| 1143 | (unsigned long long) len, coll, coll_index); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1144 | |
Alex Elder | 0ce1a79 | 2012-07-03 16:01:18 -0500 | [diff] [blame] | 1145 | osdc = &rbd_dev->rbd_client->client->osdc; |
Alex Elder | 5f29ddd | 2012-11-08 08:01:39 -0600 | [diff] [blame] | 1146 | osd_req = ceph_osdc_alloc_request(osdc, flags, snapc, ops, |
Alex Elder | 1dbb439 | 2012-01-24 10:08:37 -0600 | [diff] [blame] | 1147 | false, GFP_NOIO, pages, bio); |
Alex Elder | 5f29ddd | 2012-11-08 08:01:39 -0600 | [diff] [blame] | 1148 | if (!osd_req) { |
Sage Weil | 4ad1262 | 2011-05-03 09:23:36 -0700 | [diff] [blame] | 1149 | ret = -ENOMEM; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1150 | goto done_pages; |
| 1151 | } |
| 1152 | |
Alex Elder | 5f29ddd | 2012-11-08 08:01:39 -0600 | [diff] [blame] | 1153 | osd_req->r_callback = rbd_cb; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1154 | |
Alex Elder | 725afc9 | 2012-11-08 08:01:39 -0600 | [diff] [blame] | 1155 | rbd_req->rq = rq; |
| 1156 | rbd_req->bio = bio; |
| 1157 | rbd_req->pages = pages; |
| 1158 | rbd_req->len = len; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1159 | |
Alex Elder | 5f29ddd | 2012-11-08 08:01:39 -0600 | [diff] [blame] | 1160 | osd_req->r_priv = rbd_req; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1161 | |
Alex Elder | 5f29ddd | 2012-11-08 08:01:39 -0600 | [diff] [blame] | 1162 | reqhead = osd_req->r_request->front.iov_base; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1163 | reqhead->snapid = cpu_to_le64(CEPH_NOSNAP); |
| 1164 | |
Alex Elder | 5f29ddd | 2012-11-08 08:01:39 -0600 | [diff] [blame] | 1165 | strncpy(osd_req->r_oid, object_name, sizeof(osd_req->r_oid)); |
| 1166 | osd_req->r_oid_len = strlen(osd_req->r_oid); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1167 | |
Alex Elder | 5f29ddd | 2012-11-08 08:01:39 -0600 | [diff] [blame] | 1168 | layout = &osd_req->r_file_layout; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1169 | memset(layout, 0, sizeof(*layout)); |
| 1170 | layout->fl_stripe_unit = cpu_to_le32(1 << RBD_MAX_OBJ_ORDER); |
| 1171 | layout->fl_stripe_count = cpu_to_le32(1); |
| 1172 | layout->fl_object_size = cpu_to_le32(1 << RBD_MAX_OBJ_ORDER); |
Alex Elder | 0d7dbfc | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 1173 | 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] | 1174 | ret = ceph_calc_raw_layout(osdc, layout, snapid, ofs, &len, &bno, |
Alex Elder | 5f29ddd | 2012-11-08 08:01:39 -0600 | [diff] [blame] | 1175 | osd_req, ops); |
Sage Weil | 6cae371 | 2012-09-24 21:02:47 -0700 | [diff] [blame] | 1176 | rbd_assert(ret == 0); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1177 | |
Alex Elder | 5f29ddd | 2012-11-08 08:01:39 -0600 | [diff] [blame] | 1178 | ceph_osdc_build_request(osd_req, ofs, &len, |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1179 | ops, |
| 1180 | snapc, |
| 1181 | &mtime, |
Alex Elder | 5f29ddd | 2012-11-08 08:01:39 -0600 | [diff] [blame] | 1182 | osd_req->r_oid, osd_req->r_oid_len); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1183 | |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 1184 | if (linger_req) { |
Alex Elder | 5f29ddd | 2012-11-08 08:01:39 -0600 | [diff] [blame] | 1185 | ceph_osdc_set_request_linger(osdc, osd_req); |
| 1186 | *linger_req = osd_req; |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 1187 | } |
| 1188 | |
Alex Elder | 5f29ddd | 2012-11-08 08:01:39 -0600 | [diff] [blame] | 1189 | ret = ceph_osdc_start_request(osdc, osd_req, false); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1190 | if (ret < 0) |
| 1191 | goto done_err; |
| 1192 | |
| 1193 | if (!rbd_cb) { |
Alex Elder | 5f29ddd | 2012-11-08 08:01:39 -0600 | [diff] [blame] | 1194 | u64 version; |
| 1195 | |
| 1196 | ret = ceph_osdc_wait_request(osdc, osd_req); |
| 1197 | version = le64_to_cpu(osd_req->r_reassert_version.version); |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 1198 | if (ver) |
Alex Elder | 5f29ddd | 2012-11-08 08:01:39 -0600 | [diff] [blame] | 1199 | *ver = version; |
| 1200 | dout("reassert_ver=%llu\n", (unsigned long long) version); |
| 1201 | ceph_osdc_put_request(osd_req); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1202 | } |
| 1203 | return ret; |
| 1204 | |
| 1205 | done_err: |
Alex Elder | 725afc9 | 2012-11-08 08:01:39 -0600 | [diff] [blame] | 1206 | bio_chain_put(rbd_req->bio); |
Alex Elder | 5f29ddd | 2012-11-08 08:01:39 -0600 | [diff] [blame] | 1207 | ceph_osdc_put_request(osd_req); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1208 | done_pages: |
Alex Elder | 8986cb3 | 2012-11-08 08:01:39 -0600 | [diff] [blame^] | 1209 | rbd_coll_end_req(rbd_req, (s32)ret, len); |
Alex Elder | 725afc9 | 2012-11-08 08:01:39 -0600 | [diff] [blame] | 1210 | kfree(rbd_req); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1211 | return ret; |
| 1212 | } |
| 1213 | |
| 1214 | /* |
| 1215 | * Ceph osd op callback |
| 1216 | */ |
Alex Elder | 5f29ddd | 2012-11-08 08:01:39 -0600 | [diff] [blame] | 1217 | static void rbd_req_cb(struct ceph_osd_request *osd_req, struct ceph_msg *msg) |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1218 | { |
Alex Elder | 5f29ddd | 2012-11-08 08:01:39 -0600 | [diff] [blame] | 1219 | struct rbd_request *rbd_req = osd_req->r_priv; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1220 | struct ceph_osd_reply_head *replyhead; |
| 1221 | struct ceph_osd_op *op; |
Alex Elder | 8986cb3 | 2012-11-08 08:01:39 -0600 | [diff] [blame^] | 1222 | s32 rc; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1223 | u64 bytes; |
| 1224 | int read_op; |
| 1225 | |
| 1226 | /* parse reply */ |
| 1227 | replyhead = msg->front.iov_base; |
| 1228 | WARN_ON(le32_to_cpu(replyhead->num_ops) == 0); |
| 1229 | op = (void *)(replyhead + 1); |
Alex Elder | 8986cb3 | 2012-11-08 08:01:39 -0600 | [diff] [blame^] | 1230 | rc = (s32)le32_to_cpu(replyhead->result); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1231 | bytes = le64_to_cpu(op->extent.length); |
Dan Carpenter | 895cfcc | 2012-06-06 09:15:33 -0500 | [diff] [blame] | 1232 | read_op = (le16_to_cpu(op->op) == CEPH_OSD_OP_READ); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1233 | |
Alex Elder | bd919d4 | 2012-07-13 20:35:11 -0500 | [diff] [blame] | 1234 | dout("rbd_req_cb bytes=%llu readop=%d rc=%d\n", |
| 1235 | (unsigned long long) bytes, read_op, (int) rc); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1236 | |
Alex Elder | 8986cb3 | 2012-11-08 08:01:39 -0600 | [diff] [blame^] | 1237 | if (rc == (s32)-ENOENT && read_op) { |
Alex Elder | 725afc9 | 2012-11-08 08:01:39 -0600 | [diff] [blame] | 1238 | zero_bio_chain(rbd_req->bio, 0); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1239 | rc = 0; |
Alex Elder | 725afc9 | 2012-11-08 08:01:39 -0600 | [diff] [blame] | 1240 | } else if (rc == 0 && read_op && bytes < rbd_req->len) { |
| 1241 | zero_bio_chain(rbd_req->bio, bytes); |
| 1242 | bytes = rbd_req->len; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1243 | } |
| 1244 | |
Alex Elder | 725afc9 | 2012-11-08 08:01:39 -0600 | [diff] [blame] | 1245 | rbd_coll_end_req(rbd_req, rc, bytes); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1246 | |
Alex Elder | 725afc9 | 2012-11-08 08:01:39 -0600 | [diff] [blame] | 1247 | if (rbd_req->bio) |
| 1248 | bio_chain_put(rbd_req->bio); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1249 | |
Alex Elder | 5f29ddd | 2012-11-08 08:01:39 -0600 | [diff] [blame] | 1250 | ceph_osdc_put_request(osd_req); |
Alex Elder | 725afc9 | 2012-11-08 08:01:39 -0600 | [diff] [blame] | 1251 | kfree(rbd_req); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1252 | } |
| 1253 | |
Alex Elder | 5f29ddd | 2012-11-08 08:01:39 -0600 | [diff] [blame] | 1254 | static void rbd_simple_req_cb(struct ceph_osd_request *osd_req, |
| 1255 | struct ceph_msg *msg) |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 1256 | { |
Alex Elder | 5f29ddd | 2012-11-08 08:01:39 -0600 | [diff] [blame] | 1257 | ceph_osdc_put_request(osd_req); |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 1258 | } |
| 1259 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1260 | /* |
| 1261 | * Do a synchronous ceph osd operation |
| 1262 | */ |
Alex Elder | 0ce1a79 | 2012-07-03 16:01:18 -0500 | [diff] [blame] | 1263 | static int rbd_req_sync_op(struct rbd_device *rbd_dev, |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1264 | struct ceph_snap_context *snapc, |
| 1265 | u64 snapid, |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1266 | int flags, |
Alex Elder | 913d2fd | 2012-06-26 12:57:03 -0700 | [diff] [blame] | 1267 | struct ceph_osd_req_op *ops, |
Alex Elder | aded07e | 2012-07-03 16:01:18 -0500 | [diff] [blame] | 1268 | const char *object_name, |
Alex Elder | f8d4de6 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 1269 | u64 ofs, u64 inbound_size, |
| 1270 | char *inbound, |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 1271 | struct ceph_osd_request **linger_req, |
| 1272 | u64 *ver) |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1273 | { |
| 1274 | int ret; |
| 1275 | struct page **pages; |
| 1276 | int num_pages; |
Alex Elder | 913d2fd | 2012-06-26 12:57:03 -0700 | [diff] [blame] | 1277 | |
Alex Elder | aafb230 | 2012-09-06 16:00:54 -0500 | [diff] [blame] | 1278 | rbd_assert(ops != NULL); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1279 | |
Alex Elder | f8d4de6 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 1280 | num_pages = calc_pages_for(ofs, inbound_size); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1281 | pages = ceph_alloc_page_vector(num_pages, GFP_KERNEL); |
Dan Carpenter | b8d0638 | 2010-10-11 21:14:23 +0200 | [diff] [blame] | 1282 | if (IS_ERR(pages)) |
| 1283 | return PTR_ERR(pages); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1284 | |
Alex Elder | 0ce1a79 | 2012-07-03 16:01:18 -0500 | [diff] [blame] | 1285 | ret = rbd_do_request(NULL, rbd_dev, snapc, snapid, |
Alex Elder | f8d4de6 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 1286 | object_name, ofs, inbound_size, NULL, |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1287 | pages, num_pages, |
| 1288 | flags, |
| 1289 | ops, |
Yehuda Sadeh | 1fec709 | 2011-05-13 13:52:56 -0700 | [diff] [blame] | 1290 | NULL, 0, |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 1291 | NULL, |
| 1292 | linger_req, ver); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1293 | if (ret < 0) |
Alex Elder | 913d2fd | 2012-06-26 12:57:03 -0700 | [diff] [blame] | 1294 | goto done; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1295 | |
Alex Elder | f8d4de6 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 1296 | if ((flags & CEPH_OSD_FLAG_READ) && inbound) |
| 1297 | ret = ceph_copy_from_page_vector(pages, inbound, ofs, ret); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1298 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1299 | done: |
| 1300 | ceph_release_page_vector(pages, num_pages); |
| 1301 | return ret; |
| 1302 | } |
| 1303 | |
| 1304 | /* |
| 1305 | * Do an asynchronous ceph osd operation |
| 1306 | */ |
| 1307 | static int rbd_do_op(struct request *rq, |
Alex Elder | 0ce1a79 | 2012-07-03 16:01:18 -0500 | [diff] [blame] | 1308 | struct rbd_device *rbd_dev, |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1309 | struct ceph_snap_context *snapc, |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1310 | u64 ofs, u64 len, |
Yehuda Sadeh | 1fec709 | 2011-05-13 13:52:56 -0700 | [diff] [blame] | 1311 | struct bio *bio, |
| 1312 | struct rbd_req_coll *coll, |
| 1313 | int coll_index) |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1314 | { |
| 1315 | char *seg_name; |
| 1316 | u64 seg_ofs; |
| 1317 | u64 seg_len; |
| 1318 | int ret; |
| 1319 | struct ceph_osd_req_op *ops; |
| 1320 | u32 payload_len; |
Alex Elder | ff2e4bb | 2012-10-10 18:59:29 -0700 | [diff] [blame] | 1321 | int opcode; |
| 1322 | int flags; |
Alex Elder | 4634246 | 2012-10-10 18:59:29 -0700 | [diff] [blame] | 1323 | u64 snapid; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1324 | |
Alex Elder | 65ccfe2 | 2012-08-09 10:33:26 -0700 | [diff] [blame] | 1325 | seg_name = rbd_segment_name(rbd_dev, ofs); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1326 | if (!seg_name) |
| 1327 | return -ENOMEM; |
Alex Elder | 65ccfe2 | 2012-08-09 10:33:26 -0700 | [diff] [blame] | 1328 | seg_len = rbd_segment_length(rbd_dev, ofs, len); |
| 1329 | seg_ofs = rbd_segment_offset(rbd_dev, ofs); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1330 | |
Alex Elder | ff2e4bb | 2012-10-10 18:59:29 -0700 | [diff] [blame] | 1331 | if (rq_data_dir(rq) == WRITE) { |
| 1332 | opcode = CEPH_OSD_OP_WRITE; |
| 1333 | flags = CEPH_OSD_FLAG_WRITE|CEPH_OSD_FLAG_ONDISK; |
Alex Elder | 4634246 | 2012-10-10 18:59:29 -0700 | [diff] [blame] | 1334 | snapid = CEPH_NOSNAP; |
Alex Elder | ff2e4bb | 2012-10-10 18:59:29 -0700 | [diff] [blame] | 1335 | payload_len = seg_len; |
| 1336 | } else { |
| 1337 | opcode = CEPH_OSD_OP_READ; |
| 1338 | flags = CEPH_OSD_FLAG_READ; |
Alex Elder | 4634246 | 2012-10-10 18:59:29 -0700 | [diff] [blame] | 1339 | snapc = NULL; |
Alex Elder | 0d7dbfc | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 1340 | snapid = rbd_dev->spec->snap_id; |
Alex Elder | ff2e4bb | 2012-10-10 18:59:29 -0700 | [diff] [blame] | 1341 | payload_len = 0; |
| 1342 | } |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1343 | |
Alex Elder | 57cfc10 | 2012-06-26 12:57:03 -0700 | [diff] [blame] | 1344 | ret = -ENOMEM; |
| 1345 | ops = rbd_create_rw_ops(1, opcode, payload_len); |
| 1346 | if (!ops) |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1347 | goto done; |
| 1348 | |
| 1349 | /* we've taken care of segment sizes earlier when we |
| 1350 | cloned the bios. We should never have a segment |
| 1351 | truncated at this point */ |
Alex Elder | aafb230 | 2012-09-06 16:00:54 -0500 | [diff] [blame] | 1352 | rbd_assert(seg_len == len); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1353 | |
| 1354 | ret = rbd_do_request(rq, rbd_dev, snapc, snapid, |
| 1355 | seg_name, seg_ofs, seg_len, |
| 1356 | bio, |
| 1357 | NULL, 0, |
| 1358 | flags, |
| 1359 | ops, |
Yehuda Sadeh | 1fec709 | 2011-05-13 13:52:56 -0700 | [diff] [blame] | 1360 | coll, coll_index, |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 1361 | rbd_req_cb, 0, NULL); |
Sage Weil | 11f7700 | 2011-05-12 16:13:54 -0700 | [diff] [blame] | 1362 | |
| 1363 | rbd_destroy_ops(ops); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1364 | done: |
| 1365 | kfree(seg_name); |
| 1366 | return ret; |
| 1367 | } |
| 1368 | |
| 1369 | /* |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1370 | * Request sync osd read |
| 1371 | */ |
Alex Elder | 0ce1a79 | 2012-07-03 16:01:18 -0500 | [diff] [blame] | 1372 | static int rbd_req_sync_read(struct rbd_device *rbd_dev, |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1373 | u64 snapid, |
Alex Elder | aded07e | 2012-07-03 16:01:18 -0500 | [diff] [blame] | 1374 | const char *object_name, |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1375 | u64 ofs, u64 len, |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 1376 | char *buf, |
| 1377 | u64 *ver) |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1378 | { |
Alex Elder | 913d2fd | 2012-06-26 12:57:03 -0700 | [diff] [blame] | 1379 | struct ceph_osd_req_op *ops; |
| 1380 | int ret; |
| 1381 | |
| 1382 | ops = rbd_create_rw_ops(1, CEPH_OSD_OP_READ, 0); |
| 1383 | if (!ops) |
| 1384 | return -ENOMEM; |
| 1385 | |
| 1386 | ret = rbd_req_sync_op(rbd_dev, NULL, |
Josh Durgin | b06e6a6 | 2011-11-21 18:16:52 -0800 | [diff] [blame] | 1387 | snapid, |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1388 | CEPH_OSD_FLAG_READ, |
Alex Elder | 913d2fd | 2012-06-26 12:57:03 -0700 | [diff] [blame] | 1389 | ops, object_name, ofs, len, buf, NULL, ver); |
| 1390 | rbd_destroy_ops(ops); |
| 1391 | |
| 1392 | return ret; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1393 | } |
| 1394 | |
| 1395 | /* |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 1396 | * Request sync osd watch |
| 1397 | */ |
Alex Elder | 0ce1a79 | 2012-07-03 16:01:18 -0500 | [diff] [blame] | 1398 | static int rbd_req_sync_notify_ack(struct rbd_device *rbd_dev, |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 1399 | u64 ver, |
Alex Elder | 7f0a24d | 2012-07-25 09:32:40 -0500 | [diff] [blame] | 1400 | u64 notify_id) |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 1401 | { |
| 1402 | struct ceph_osd_req_op *ops; |
Sage Weil | 11f7700 | 2011-05-12 16:13:54 -0700 | [diff] [blame] | 1403 | int ret; |
| 1404 | |
Alex Elder | 57cfc10 | 2012-06-26 12:57:03 -0700 | [diff] [blame] | 1405 | ops = rbd_create_rw_ops(1, CEPH_OSD_OP_NOTIFY_ACK, 0); |
| 1406 | if (!ops) |
| 1407 | return -ENOMEM; |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 1408 | |
Josh Durgin | a71b891 | 2011-12-05 18:10:44 -0800 | [diff] [blame] | 1409 | ops[0].watch.ver = cpu_to_le64(ver); |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 1410 | ops[0].watch.cookie = notify_id; |
| 1411 | ops[0].watch.flag = 0; |
| 1412 | |
Alex Elder | 0ce1a79 | 2012-07-03 16:01:18 -0500 | [diff] [blame] | 1413 | ret = rbd_do_request(NULL, rbd_dev, NULL, CEPH_NOSNAP, |
Alex Elder | 7f0a24d | 2012-07-25 09:32:40 -0500 | [diff] [blame] | 1414 | rbd_dev->header_name, 0, 0, NULL, |
Alex Elder | ad4f232 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 1415 | NULL, 0, |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 1416 | CEPH_OSD_FLAG_READ, |
| 1417 | ops, |
Yehuda Sadeh | 1fec709 | 2011-05-13 13:52:56 -0700 | [diff] [blame] | 1418 | NULL, 0, |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 1419 | rbd_simple_req_cb, 0, NULL); |
| 1420 | |
| 1421 | rbd_destroy_ops(ops); |
| 1422 | return ret; |
| 1423 | } |
| 1424 | |
| 1425 | static void rbd_watch_cb(u64 ver, u64 notify_id, u8 opcode, void *data) |
| 1426 | { |
Alex Elder | 0ce1a79 | 2012-07-03 16:01:18 -0500 | [diff] [blame] | 1427 | struct rbd_device *rbd_dev = (struct rbd_device *)data; |
Josh Durgin | a71b891 | 2011-12-05 18:10:44 -0800 | [diff] [blame] | 1428 | u64 hver; |
Sage Weil | 13143d2 | 2011-05-12 16:08:30 -0700 | [diff] [blame] | 1429 | int rc; |
| 1430 | |
Alex Elder | 0ce1a79 | 2012-07-03 16:01:18 -0500 | [diff] [blame] | 1431 | if (!rbd_dev) |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 1432 | return; |
| 1433 | |
Alex Elder | bd919d4 | 2012-07-13 20:35:11 -0500 | [diff] [blame] | 1434 | dout("rbd_watch_cb %s notify_id=%llu opcode=%u\n", |
| 1435 | rbd_dev->header_name, (unsigned long long) notify_id, |
| 1436 | (unsigned int) opcode); |
Alex Elder | 117973f | 2012-08-31 17:29:55 -0500 | [diff] [blame] | 1437 | rc = rbd_dev_refresh(rbd_dev, &hver); |
Sage Weil | 13143d2 | 2011-05-12 16:08:30 -0700 | [diff] [blame] | 1438 | if (rc) |
Alex Elder | 06ecc6c | 2012-11-01 10:17:15 -0500 | [diff] [blame] | 1439 | rbd_warn(rbd_dev, "got notification but failed to " |
| 1440 | " update snaps: %d\n", rc); |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 1441 | |
Alex Elder | 7f0a24d | 2012-07-25 09:32:40 -0500 | [diff] [blame] | 1442 | rbd_req_sync_notify_ack(rbd_dev, hver, notify_id); |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 1443 | } |
| 1444 | |
| 1445 | /* |
| 1446 | * Request sync osd watch |
| 1447 | */ |
Alex Elder | 0e6f322 | 2012-07-25 09:32:40 -0500 | [diff] [blame] | 1448 | static int rbd_req_sync_watch(struct rbd_device *rbd_dev) |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 1449 | { |
| 1450 | struct ceph_osd_req_op *ops; |
Alex Elder | 0ce1a79 | 2012-07-03 16:01:18 -0500 | [diff] [blame] | 1451 | struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc; |
Alex Elder | 57cfc10 | 2012-06-26 12:57:03 -0700 | [diff] [blame] | 1452 | int ret; |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 1453 | |
Alex Elder | 57cfc10 | 2012-06-26 12:57:03 -0700 | [diff] [blame] | 1454 | ops = rbd_create_rw_ops(1, CEPH_OSD_OP_WATCH, 0); |
| 1455 | if (!ops) |
| 1456 | return -ENOMEM; |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 1457 | |
| 1458 | ret = ceph_osdc_create_event(osdc, rbd_watch_cb, 0, |
Alex Elder | 0ce1a79 | 2012-07-03 16:01:18 -0500 | [diff] [blame] | 1459 | (void *)rbd_dev, &rbd_dev->watch_event); |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 1460 | if (ret < 0) |
| 1461 | goto fail; |
| 1462 | |
Alex Elder | 0e6f322 | 2012-07-25 09:32:40 -0500 | [diff] [blame] | 1463 | ops[0].watch.ver = cpu_to_le64(rbd_dev->header.obj_version); |
Alex Elder | 0ce1a79 | 2012-07-03 16:01:18 -0500 | [diff] [blame] | 1464 | ops[0].watch.cookie = cpu_to_le64(rbd_dev->watch_event->cookie); |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 1465 | ops[0].watch.flag = 1; |
| 1466 | |
Alex Elder | 0ce1a79 | 2012-07-03 16:01:18 -0500 | [diff] [blame] | 1467 | ret = rbd_req_sync_op(rbd_dev, NULL, |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 1468 | CEPH_NOSNAP, |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 1469 | CEPH_OSD_FLAG_WRITE | CEPH_OSD_FLAG_ONDISK, |
| 1470 | ops, |
Alex Elder | 0e6f322 | 2012-07-25 09:32:40 -0500 | [diff] [blame] | 1471 | rbd_dev->header_name, |
| 1472 | 0, 0, NULL, |
Alex Elder | 0ce1a79 | 2012-07-03 16:01:18 -0500 | [diff] [blame] | 1473 | &rbd_dev->watch_request, NULL); |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 1474 | |
| 1475 | if (ret < 0) |
| 1476 | goto fail_event; |
| 1477 | |
| 1478 | rbd_destroy_ops(ops); |
| 1479 | return 0; |
| 1480 | |
| 1481 | fail_event: |
Alex Elder | 0ce1a79 | 2012-07-03 16:01:18 -0500 | [diff] [blame] | 1482 | ceph_osdc_cancel_event(rbd_dev->watch_event); |
| 1483 | rbd_dev->watch_event = NULL; |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 1484 | fail: |
| 1485 | rbd_destroy_ops(ops); |
| 1486 | return ret; |
| 1487 | } |
| 1488 | |
Yehuda Sadeh | 79e3057c | 2011-07-12 16:56:57 -0700 | [diff] [blame] | 1489 | /* |
| 1490 | * Request sync osd unwatch |
| 1491 | */ |
Alex Elder | 070c633 | 2012-07-25 09:32:41 -0500 | [diff] [blame] | 1492 | static int rbd_req_sync_unwatch(struct rbd_device *rbd_dev) |
Yehuda Sadeh | 79e3057c | 2011-07-12 16:56:57 -0700 | [diff] [blame] | 1493 | { |
| 1494 | struct ceph_osd_req_op *ops; |
Alex Elder | 57cfc10 | 2012-06-26 12:57:03 -0700 | [diff] [blame] | 1495 | int ret; |
Yehuda Sadeh | 79e3057c | 2011-07-12 16:56:57 -0700 | [diff] [blame] | 1496 | |
Alex Elder | 57cfc10 | 2012-06-26 12:57:03 -0700 | [diff] [blame] | 1497 | ops = rbd_create_rw_ops(1, CEPH_OSD_OP_WATCH, 0); |
| 1498 | if (!ops) |
| 1499 | return -ENOMEM; |
Yehuda Sadeh | 79e3057c | 2011-07-12 16:56:57 -0700 | [diff] [blame] | 1500 | |
| 1501 | ops[0].watch.ver = 0; |
Alex Elder | 0ce1a79 | 2012-07-03 16:01:18 -0500 | [diff] [blame] | 1502 | ops[0].watch.cookie = cpu_to_le64(rbd_dev->watch_event->cookie); |
Yehuda Sadeh | 79e3057c | 2011-07-12 16:56:57 -0700 | [diff] [blame] | 1503 | ops[0].watch.flag = 0; |
| 1504 | |
Alex Elder | 0ce1a79 | 2012-07-03 16:01:18 -0500 | [diff] [blame] | 1505 | ret = rbd_req_sync_op(rbd_dev, NULL, |
Yehuda Sadeh | 79e3057c | 2011-07-12 16:56:57 -0700 | [diff] [blame] | 1506 | CEPH_NOSNAP, |
Yehuda Sadeh | 79e3057c | 2011-07-12 16:56:57 -0700 | [diff] [blame] | 1507 | CEPH_OSD_FLAG_WRITE | CEPH_OSD_FLAG_ONDISK, |
| 1508 | ops, |
Alex Elder | 070c633 | 2012-07-25 09:32:41 -0500 | [diff] [blame] | 1509 | rbd_dev->header_name, |
| 1510 | 0, 0, NULL, NULL, NULL); |
| 1511 | |
Yehuda Sadeh | 79e3057c | 2011-07-12 16:56:57 -0700 | [diff] [blame] | 1512 | |
| 1513 | rbd_destroy_ops(ops); |
Alex Elder | 0ce1a79 | 2012-07-03 16:01:18 -0500 | [diff] [blame] | 1514 | ceph_osdc_cancel_event(rbd_dev->watch_event); |
| 1515 | rbd_dev->watch_event = NULL; |
Yehuda Sadeh | 79e3057c | 2011-07-12 16:56:57 -0700 | [diff] [blame] | 1516 | return ret; |
| 1517 | } |
| 1518 | |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 1519 | /* |
Alex Elder | 3cb4a68 | 2012-06-26 12:57:03 -0700 | [diff] [blame] | 1520 | * Synchronous osd object method call |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1521 | */ |
Alex Elder | 0ce1a79 | 2012-07-03 16:01:18 -0500 | [diff] [blame] | 1522 | static int rbd_req_sync_exec(struct rbd_device *rbd_dev, |
Alex Elder | aded07e | 2012-07-03 16:01:18 -0500 | [diff] [blame] | 1523 | const char *object_name, |
| 1524 | const char *class_name, |
| 1525 | const char *method_name, |
Alex Elder | 3cb4a68 | 2012-06-26 12:57:03 -0700 | [diff] [blame] | 1526 | const char *outbound, |
| 1527 | size_t outbound_size, |
Alex Elder | f8d4de6 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 1528 | char *inbound, |
| 1529 | size_t inbound_size, |
Alex Elder | 3cb4a68 | 2012-06-26 12:57:03 -0700 | [diff] [blame] | 1530 | int flags, |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 1531 | u64 *ver) |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1532 | { |
| 1533 | struct ceph_osd_req_op *ops; |
Alex Elder | aded07e | 2012-07-03 16:01:18 -0500 | [diff] [blame] | 1534 | int class_name_len = strlen(class_name); |
| 1535 | int method_name_len = strlen(method_name); |
Alex Elder | 3cb4a68 | 2012-06-26 12:57:03 -0700 | [diff] [blame] | 1536 | int payload_size; |
Alex Elder | 57cfc10 | 2012-06-26 12:57:03 -0700 | [diff] [blame] | 1537 | int ret; |
| 1538 | |
Alex Elder | 3cb4a68 | 2012-06-26 12:57:03 -0700 | [diff] [blame] | 1539 | /* |
| 1540 | * Any input parameters required by the method we're calling |
| 1541 | * will be sent along with the class and method names as |
| 1542 | * part of the message payload. That data and its size are |
| 1543 | * supplied via the indata and indata_len fields (named from |
| 1544 | * the perspective of the server side) in the OSD request |
| 1545 | * operation. |
| 1546 | */ |
| 1547 | payload_size = class_name_len + method_name_len + outbound_size; |
| 1548 | ops = rbd_create_rw_ops(1, CEPH_OSD_OP_CALL, payload_size); |
Alex Elder | 57cfc10 | 2012-06-26 12:57:03 -0700 | [diff] [blame] | 1549 | if (!ops) |
| 1550 | return -ENOMEM; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1551 | |
Alex Elder | aded07e | 2012-07-03 16:01:18 -0500 | [diff] [blame] | 1552 | ops[0].cls.class_name = class_name; |
| 1553 | ops[0].cls.class_len = (__u8) class_name_len; |
| 1554 | ops[0].cls.method_name = method_name; |
| 1555 | ops[0].cls.method_len = (__u8) method_name_len; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1556 | ops[0].cls.argc = 0; |
Alex Elder | 3cb4a68 | 2012-06-26 12:57:03 -0700 | [diff] [blame] | 1557 | ops[0].cls.indata = outbound; |
| 1558 | ops[0].cls.indata_len = outbound_size; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1559 | |
Alex Elder | 0ce1a79 | 2012-07-03 16:01:18 -0500 | [diff] [blame] | 1560 | ret = rbd_req_sync_op(rbd_dev, NULL, |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1561 | CEPH_NOSNAP, |
Alex Elder | 3cb4a68 | 2012-06-26 12:57:03 -0700 | [diff] [blame] | 1562 | flags, ops, |
Alex Elder | f8d4de6 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 1563 | object_name, 0, inbound_size, inbound, |
| 1564 | NULL, ver); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1565 | |
| 1566 | rbd_destroy_ops(ops); |
| 1567 | |
| 1568 | dout("cls_exec returned %d\n", ret); |
| 1569 | return ret; |
| 1570 | } |
| 1571 | |
Yehuda Sadeh | 1fec709 | 2011-05-13 13:52:56 -0700 | [diff] [blame] | 1572 | static struct rbd_req_coll *rbd_alloc_coll(int num_reqs) |
| 1573 | { |
| 1574 | struct rbd_req_coll *coll = |
| 1575 | kzalloc(sizeof(struct rbd_req_coll) + |
| 1576 | sizeof(struct rbd_req_status) * num_reqs, |
| 1577 | GFP_ATOMIC); |
| 1578 | |
| 1579 | if (!coll) |
| 1580 | return NULL; |
| 1581 | coll->total = num_reqs; |
| 1582 | kref_init(&coll->kref); |
| 1583 | return coll; |
| 1584 | } |
| 1585 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1586 | /* |
| 1587 | * block device queue callback |
| 1588 | */ |
| 1589 | static void rbd_rq_fn(struct request_queue *q) |
| 1590 | { |
| 1591 | struct rbd_device *rbd_dev = q->queuedata; |
| 1592 | struct request *rq; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1593 | |
Alex Elder | 00f1f36 | 2012-02-07 12:03:36 -0600 | [diff] [blame] | 1594 | while ((rq = blk_fetch_request(q))) { |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1595 | struct bio *bio; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1596 | bool do_write; |
Alex Elder | bd919d4 | 2012-07-13 20:35:11 -0500 | [diff] [blame] | 1597 | unsigned int size; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1598 | u64 ofs; |
Yehuda Sadeh | 1fec709 | 2011-05-13 13:52:56 -0700 | [diff] [blame] | 1599 | int num_segs, cur_seg = 0; |
| 1600 | struct rbd_req_coll *coll; |
Josh Durgin | d1d2564 | 2011-12-05 14:03:05 -0800 | [diff] [blame] | 1601 | struct ceph_snap_context *snapc; |
Alex Elder | f7760da | 2012-10-20 22:17:27 -0500 | [diff] [blame] | 1602 | unsigned int bio_offset; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1603 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1604 | dout("fetched request\n"); |
| 1605 | |
| 1606 | /* filter out block requests we don't understand */ |
| 1607 | if ((rq->cmd_type != REQ_TYPE_FS)) { |
| 1608 | __blk_end_request_all(rq, 0); |
Alex Elder | 00f1f36 | 2012-02-07 12:03:36 -0600 | [diff] [blame] | 1609 | continue; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1610 | } |
| 1611 | |
| 1612 | /* deduce our operation (read, write) */ |
| 1613 | do_write = (rq_data_dir(rq) == WRITE); |
Alex Elder | f84344f | 2012-08-31 17:29:51 -0500 | [diff] [blame] | 1614 | if (do_write && rbd_dev->mapping.read_only) { |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1615 | __blk_end_request_all(rq, -EROFS); |
Alex Elder | 00f1f36 | 2012-02-07 12:03:36 -0600 | [diff] [blame] | 1616 | continue; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1617 | } |
| 1618 | |
| 1619 | spin_unlock_irq(q->queue_lock); |
| 1620 | |
Josh Durgin | d1d2564 | 2011-12-05 14:03:05 -0800 | [diff] [blame] | 1621 | down_read(&rbd_dev->header_rwsem); |
Josh Durgin | e88a36e | 2011-11-21 18:14:25 -0800 | [diff] [blame] | 1622 | |
Alex Elder | daba5fd | 2012-10-26 17:25:23 -0500 | [diff] [blame] | 1623 | if (!rbd_dev->exists) { |
Alex Elder | 0d7dbfc | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 1624 | rbd_assert(rbd_dev->spec->snap_id != CEPH_NOSNAP); |
Josh Durgin | e88a36e | 2011-11-21 18:14:25 -0800 | [diff] [blame] | 1625 | up_read(&rbd_dev->header_rwsem); |
Josh Durgin | d1d2564 | 2011-12-05 14:03:05 -0800 | [diff] [blame] | 1626 | dout("request for non-existent snapshot"); |
| 1627 | spin_lock_irq(q->queue_lock); |
| 1628 | __blk_end_request_all(rq, -ENXIO); |
| 1629 | continue; |
Josh Durgin | e88a36e | 2011-11-21 18:14:25 -0800 | [diff] [blame] | 1630 | } |
| 1631 | |
Josh Durgin | d1d2564 | 2011-12-05 14:03:05 -0800 | [diff] [blame] | 1632 | snapc = ceph_get_snap_context(rbd_dev->header.snapc); |
| 1633 | |
| 1634 | up_read(&rbd_dev->header_rwsem); |
| 1635 | |
Alex Elder | f7760da | 2012-10-20 22:17:27 -0500 | [diff] [blame] | 1636 | size = blk_rq_bytes(rq); |
| 1637 | ofs = blk_rq_pos(rq) * SECTOR_SIZE; |
| 1638 | bio = rq->bio; |
| 1639 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1640 | dout("%s 0x%x bytes at 0x%llx\n", |
| 1641 | do_write ? "write" : "read", |
Alex Elder | bd919d4 | 2012-07-13 20:35:11 -0500 | [diff] [blame] | 1642 | size, (unsigned long long) blk_rq_pos(rq) * SECTOR_SIZE); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1643 | |
Yehuda Sadeh | 1fec709 | 2011-05-13 13:52:56 -0700 | [diff] [blame] | 1644 | num_segs = rbd_get_num_segments(&rbd_dev->header, ofs, size); |
Alex Elder | df111be | 2012-08-09 10:33:26 -0700 | [diff] [blame] | 1645 | if (num_segs <= 0) { |
| 1646 | spin_lock_irq(q->queue_lock); |
| 1647 | __blk_end_request_all(rq, num_segs); |
| 1648 | ceph_put_snap_context(snapc); |
| 1649 | continue; |
| 1650 | } |
Yehuda Sadeh | 1fec709 | 2011-05-13 13:52:56 -0700 | [diff] [blame] | 1651 | coll = rbd_alloc_coll(num_segs); |
| 1652 | if (!coll) { |
| 1653 | spin_lock_irq(q->queue_lock); |
| 1654 | __blk_end_request_all(rq, -ENOMEM); |
Josh Durgin | d1d2564 | 2011-12-05 14:03:05 -0800 | [diff] [blame] | 1655 | ceph_put_snap_context(snapc); |
Alex Elder | 00f1f36 | 2012-02-07 12:03:36 -0600 | [diff] [blame] | 1656 | continue; |
Yehuda Sadeh | 1fec709 | 2011-05-13 13:52:56 -0700 | [diff] [blame] | 1657 | } |
| 1658 | |
Alex Elder | f7760da | 2012-10-20 22:17:27 -0500 | [diff] [blame] | 1659 | bio_offset = 0; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1660 | do { |
Alex Elder | f7760da | 2012-10-20 22:17:27 -0500 | [diff] [blame] | 1661 | u64 limit = rbd_segment_length(rbd_dev, ofs, size); |
| 1662 | unsigned int chain_size; |
| 1663 | struct bio *bio_chain; |
| 1664 | |
| 1665 | BUG_ON(limit > (u64) UINT_MAX); |
| 1666 | chain_size = (unsigned int) limit; |
Alex Elder | bd919d4 | 2012-07-13 20:35:11 -0500 | [diff] [blame] | 1667 | dout("rq->bio->bi_vcnt=%hu\n", rq->bio->bi_vcnt); |
Alex Elder | f7760da | 2012-10-20 22:17:27 -0500 | [diff] [blame] | 1668 | |
Yehuda Sadeh | 1fec709 | 2011-05-13 13:52:56 -0700 | [diff] [blame] | 1669 | kref_get(&coll->kref); |
Alex Elder | f7760da | 2012-10-20 22:17:27 -0500 | [diff] [blame] | 1670 | |
| 1671 | /* Pass a cloned bio chain via an osd request */ |
| 1672 | |
| 1673 | bio_chain = bio_chain_clone_range(&bio, |
| 1674 | &bio_offset, chain_size, |
| 1675 | GFP_ATOMIC); |
| 1676 | if (bio_chain) |
Alex Elder | 4634246 | 2012-10-10 18:59:29 -0700 | [diff] [blame] | 1677 | (void) rbd_do_op(rq, rbd_dev, snapc, |
Alex Elder | f7760da | 2012-10-20 22:17:27 -0500 | [diff] [blame] | 1678 | ofs, chain_size, |
| 1679 | bio_chain, coll, cur_seg); |
Alex Elder | 4634246 | 2012-10-10 18:59:29 -0700 | [diff] [blame] | 1680 | else |
Yehuda Sadeh | 1fec709 | 2011-05-13 13:52:56 -0700 | [diff] [blame] | 1681 | rbd_coll_end_req_index(rq, coll, cur_seg, |
Alex Elder | 8986cb3 | 2012-11-08 08:01:39 -0600 | [diff] [blame^] | 1682 | (s32)-ENOMEM, |
| 1683 | chain_size); |
Alex Elder | f7760da | 2012-10-20 22:17:27 -0500 | [diff] [blame] | 1684 | size -= chain_size; |
| 1685 | ofs += chain_size; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1686 | |
Yehuda Sadeh | 1fec709 | 2011-05-13 13:52:56 -0700 | [diff] [blame] | 1687 | cur_seg++; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1688 | } while (size > 0); |
Yehuda Sadeh | 1fec709 | 2011-05-13 13:52:56 -0700 | [diff] [blame] | 1689 | kref_put(&coll->kref, rbd_coll_release); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1690 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1691 | spin_lock_irq(q->queue_lock); |
Josh Durgin | d1d2564 | 2011-12-05 14:03:05 -0800 | [diff] [blame] | 1692 | |
| 1693 | ceph_put_snap_context(snapc); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1694 | } |
| 1695 | } |
| 1696 | |
| 1697 | /* |
| 1698 | * a queue callback. Makes sure that we don't create a bio that spans across |
| 1699 | * multiple osd objects. One exception would be with a single page bios, |
Alex Elder | f7760da | 2012-10-20 22:17:27 -0500 | [diff] [blame] | 1700 | * which we handle later at bio_chain_clone_range() |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1701 | */ |
| 1702 | static int rbd_merge_bvec(struct request_queue *q, struct bvec_merge_data *bmd, |
| 1703 | struct bio_vec *bvec) |
| 1704 | { |
| 1705 | struct rbd_device *rbd_dev = q->queuedata; |
Alex Elder | e5cfeed2 | 2012-10-20 22:17:27 -0500 | [diff] [blame] | 1706 | sector_t sector_offset; |
| 1707 | sector_t sectors_per_obj; |
| 1708 | sector_t obj_sector_offset; |
| 1709 | int ret; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1710 | |
Alex Elder | e5cfeed2 | 2012-10-20 22:17:27 -0500 | [diff] [blame] | 1711 | /* |
| 1712 | * Find how far into its rbd object the partition-relative |
| 1713 | * bio start sector is to offset relative to the enclosing |
| 1714 | * device. |
| 1715 | */ |
| 1716 | sector_offset = get_start_sect(bmd->bi_bdev) + bmd->bi_sector; |
| 1717 | sectors_per_obj = 1 << (rbd_dev->header.obj_order - SECTOR_SHIFT); |
| 1718 | obj_sector_offset = sector_offset & (sectors_per_obj - 1); |
Alex Elder | 593a9e7 | 2012-02-07 12:03:37 -0600 | [diff] [blame] | 1719 | |
Alex Elder | e5cfeed2 | 2012-10-20 22:17:27 -0500 | [diff] [blame] | 1720 | /* |
| 1721 | * Compute the number of bytes from that offset to the end |
| 1722 | * of the object. Account for what's already used by the bio. |
| 1723 | */ |
| 1724 | ret = (int) (sectors_per_obj - obj_sector_offset) << SECTOR_SHIFT; |
| 1725 | if (ret > bmd->bi_size) |
| 1726 | ret -= bmd->bi_size; |
| 1727 | else |
| 1728 | ret = 0; |
| 1729 | |
| 1730 | /* |
| 1731 | * Don't send back more than was asked for. And if the bio |
| 1732 | * was empty, let the whole thing through because: "Note |
| 1733 | * that a block device *must* allow a single page to be |
| 1734 | * added to an empty bio." |
| 1735 | */ |
| 1736 | rbd_assert(bvec->bv_len <= PAGE_SIZE); |
| 1737 | if (ret > (int) bvec->bv_len || !bmd->bi_size) |
| 1738 | ret = (int) bvec->bv_len; |
| 1739 | |
| 1740 | return ret; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1741 | } |
| 1742 | |
| 1743 | static void rbd_free_disk(struct rbd_device *rbd_dev) |
| 1744 | { |
| 1745 | struct gendisk *disk = rbd_dev->disk; |
| 1746 | |
| 1747 | if (!disk) |
| 1748 | return; |
| 1749 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1750 | if (disk->flags & GENHD_FL_UP) |
| 1751 | del_gendisk(disk); |
| 1752 | if (disk->queue) |
| 1753 | blk_cleanup_queue(disk->queue); |
| 1754 | put_disk(disk); |
| 1755 | } |
| 1756 | |
| 1757 | /* |
Alex Elder | 4156d99 | 2012-08-02 11:29:46 -0500 | [diff] [blame] | 1758 | * Read the complete header for the given rbd device. |
| 1759 | * |
| 1760 | * Returns a pointer to a dynamically-allocated buffer containing |
| 1761 | * the complete and validated header. Caller can pass the address |
| 1762 | * of a variable that will be filled in with the version of the |
| 1763 | * header object at the time it was read. |
| 1764 | * |
| 1765 | * Returns a pointer-coded errno if a failure occurs. |
| 1766 | */ |
| 1767 | static struct rbd_image_header_ondisk * |
| 1768 | rbd_dev_v1_header_read(struct rbd_device *rbd_dev, u64 *version) |
| 1769 | { |
| 1770 | struct rbd_image_header_ondisk *ondisk = NULL; |
| 1771 | u32 snap_count = 0; |
| 1772 | u64 names_size = 0; |
| 1773 | u32 want_count; |
| 1774 | int ret; |
| 1775 | |
| 1776 | /* |
| 1777 | * The complete header will include an array of its 64-bit |
| 1778 | * snapshot ids, followed by the names of those snapshots as |
| 1779 | * a contiguous block of NUL-terminated strings. Note that |
| 1780 | * the number of snapshots could change by the time we read |
| 1781 | * it in, in which case we re-read it. |
| 1782 | */ |
| 1783 | do { |
| 1784 | size_t size; |
| 1785 | |
| 1786 | kfree(ondisk); |
| 1787 | |
| 1788 | size = sizeof (*ondisk); |
| 1789 | size += snap_count * sizeof (struct rbd_image_snap_ondisk); |
| 1790 | size += names_size; |
| 1791 | ondisk = kmalloc(size, GFP_KERNEL); |
| 1792 | if (!ondisk) |
| 1793 | return ERR_PTR(-ENOMEM); |
| 1794 | |
| 1795 | ret = rbd_req_sync_read(rbd_dev, CEPH_NOSNAP, |
| 1796 | rbd_dev->header_name, |
| 1797 | 0, size, |
| 1798 | (char *) ondisk, version); |
| 1799 | |
| 1800 | if (ret < 0) |
| 1801 | goto out_err; |
| 1802 | if (WARN_ON((size_t) ret < size)) { |
| 1803 | ret = -ENXIO; |
Alex Elder | 06ecc6c | 2012-11-01 10:17:15 -0500 | [diff] [blame] | 1804 | rbd_warn(rbd_dev, "short header read (want %zd got %d)", |
| 1805 | size, ret); |
Alex Elder | 4156d99 | 2012-08-02 11:29:46 -0500 | [diff] [blame] | 1806 | goto out_err; |
| 1807 | } |
| 1808 | if (!rbd_dev_ondisk_valid(ondisk)) { |
| 1809 | ret = -ENXIO; |
Alex Elder | 06ecc6c | 2012-11-01 10:17:15 -0500 | [diff] [blame] | 1810 | rbd_warn(rbd_dev, "invalid header"); |
Alex Elder | 4156d99 | 2012-08-02 11:29:46 -0500 | [diff] [blame] | 1811 | goto out_err; |
| 1812 | } |
| 1813 | |
| 1814 | names_size = le64_to_cpu(ondisk->snap_names_len); |
| 1815 | want_count = snap_count; |
| 1816 | snap_count = le32_to_cpu(ondisk->snap_count); |
| 1817 | } while (snap_count != want_count); |
| 1818 | |
| 1819 | return ondisk; |
| 1820 | |
| 1821 | out_err: |
| 1822 | kfree(ondisk); |
| 1823 | |
| 1824 | return ERR_PTR(ret); |
| 1825 | } |
| 1826 | |
| 1827 | /* |
| 1828 | * reload the ondisk the header |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1829 | */ |
| 1830 | static int rbd_read_header(struct rbd_device *rbd_dev, |
| 1831 | struct rbd_image_header *header) |
| 1832 | { |
Alex Elder | 4156d99 | 2012-08-02 11:29:46 -0500 | [diff] [blame] | 1833 | struct rbd_image_header_ondisk *ondisk; |
| 1834 | u64 ver = 0; |
| 1835 | int ret; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1836 | |
Alex Elder | 4156d99 | 2012-08-02 11:29:46 -0500 | [diff] [blame] | 1837 | ondisk = rbd_dev_v1_header_read(rbd_dev, &ver); |
| 1838 | if (IS_ERR(ondisk)) |
| 1839 | return PTR_ERR(ondisk); |
| 1840 | ret = rbd_header_from_disk(header, ondisk); |
| 1841 | if (ret >= 0) |
| 1842 | header->obj_version = ver; |
| 1843 | kfree(ondisk); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1844 | |
Alex Elder | 4156d99 | 2012-08-02 11:29:46 -0500 | [diff] [blame] | 1845 | return ret; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1846 | } |
| 1847 | |
Alex Elder | 41f38c2 | 2012-10-25 23:34:40 -0500 | [diff] [blame] | 1848 | static void rbd_remove_all_snaps(struct rbd_device *rbd_dev) |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 1849 | { |
| 1850 | struct rbd_snap *snap; |
Alex Elder | a059329 | 2012-07-19 09:09:27 -0500 | [diff] [blame] | 1851 | struct rbd_snap *next; |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 1852 | |
Alex Elder | a059329 | 2012-07-19 09:09:27 -0500 | [diff] [blame] | 1853 | list_for_each_entry_safe(snap, next, &rbd_dev->snaps, node) |
Alex Elder | 41f38c2 | 2012-10-25 23:34:40 -0500 | [diff] [blame] | 1854 | rbd_remove_snap_dev(snap); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 1855 | } |
| 1856 | |
Alex Elder | 9478554 | 2012-10-09 13:50:17 -0700 | [diff] [blame] | 1857 | static void rbd_update_mapping_size(struct rbd_device *rbd_dev) |
| 1858 | { |
| 1859 | sector_t size; |
| 1860 | |
Alex Elder | 0d7dbfc | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 1861 | if (rbd_dev->spec->snap_id != CEPH_NOSNAP) |
Alex Elder | 9478554 | 2012-10-09 13:50:17 -0700 | [diff] [blame] | 1862 | return; |
| 1863 | |
| 1864 | size = (sector_t) rbd_dev->header.image_size / SECTOR_SIZE; |
| 1865 | dout("setting size to %llu sectors", (unsigned long long) size); |
| 1866 | rbd_dev->mapping.size = (u64) size; |
| 1867 | set_capacity(rbd_dev->disk, size); |
| 1868 | } |
| 1869 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1870 | /* |
| 1871 | * only read the first part of the ondisk header, without the snaps info |
| 1872 | */ |
Alex Elder | 117973f | 2012-08-31 17:29:55 -0500 | [diff] [blame] | 1873 | 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] | 1874 | { |
| 1875 | int ret; |
| 1876 | struct rbd_image_header h; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1877 | |
| 1878 | ret = rbd_read_header(rbd_dev, &h); |
| 1879 | if (ret < 0) |
| 1880 | return ret; |
| 1881 | |
Josh Durgin | a51aa0c | 2011-12-05 10:35:04 -0800 | [diff] [blame] | 1882 | down_write(&rbd_dev->header_rwsem); |
| 1883 | |
Alex Elder | 9478554 | 2012-10-09 13:50:17 -0700 | [diff] [blame] | 1884 | /* Update image size, and check for resize of mapped image */ |
| 1885 | rbd_dev->header.image_size = h.image_size; |
| 1886 | rbd_update_mapping_size(rbd_dev); |
Sage Weil | 9db4b3e | 2011-04-19 22:49:06 -0700 | [diff] [blame] | 1887 | |
Alex Elder | 849b426 | 2012-07-09 21:04:24 -0500 | [diff] [blame] | 1888 | /* rbd_dev->header.object_prefix shouldn't change */ |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1889 | kfree(rbd_dev->header.snap_sizes); |
Alex Elder | 849b426 | 2012-07-09 21:04:24 -0500 | [diff] [blame] | 1890 | kfree(rbd_dev->header.snap_names); |
Josh Durgin | d1d2564 | 2011-12-05 14:03:05 -0800 | [diff] [blame] | 1891 | /* osd requests may still refer to snapc */ |
| 1892 | ceph_put_snap_context(rbd_dev->header.snapc); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1893 | |
Alex Elder | b813623 | 2012-07-25 09:32:41 -0500 | [diff] [blame] | 1894 | if (hver) |
| 1895 | *hver = h.obj_version; |
Josh Durgin | a71b891 | 2011-12-05 18:10:44 -0800 | [diff] [blame] | 1896 | rbd_dev->header.obj_version = h.obj_version; |
Josh Durgin | 93a24e0 | 2011-12-05 10:41:28 -0800 | [diff] [blame] | 1897 | rbd_dev->header.image_size = h.image_size; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1898 | rbd_dev->header.snapc = h.snapc; |
| 1899 | rbd_dev->header.snap_names = h.snap_names; |
| 1900 | rbd_dev->header.snap_sizes = h.snap_sizes; |
Alex Elder | 849b426 | 2012-07-09 21:04:24 -0500 | [diff] [blame] | 1901 | /* Free the extra copy of the object prefix */ |
| 1902 | WARN_ON(strcmp(rbd_dev->header.object_prefix, h.object_prefix)); |
| 1903 | kfree(h.object_prefix); |
| 1904 | |
Alex Elder | 304f680 | 2012-08-31 17:29:52 -0500 | [diff] [blame] | 1905 | ret = rbd_dev_snaps_update(rbd_dev); |
| 1906 | if (!ret) |
| 1907 | ret = rbd_dev_snaps_register(rbd_dev); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 1908 | |
Josh Durgin | c666601 | 2011-11-21 17:11:12 -0800 | [diff] [blame] | 1909 | up_write(&rbd_dev->header_rwsem); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1910 | |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 1911 | return ret; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1912 | } |
| 1913 | |
Alex Elder | 117973f | 2012-08-31 17:29:55 -0500 | [diff] [blame] | 1914 | static int rbd_dev_refresh(struct rbd_device *rbd_dev, u64 *hver) |
Alex Elder | 1fe5e99 | 2012-07-25 09:32:41 -0500 | [diff] [blame] | 1915 | { |
| 1916 | int ret; |
| 1917 | |
Alex Elder | 117973f | 2012-08-31 17:29:55 -0500 | [diff] [blame] | 1918 | rbd_assert(rbd_image_format_valid(rbd_dev->image_format)); |
Alex Elder | 1fe5e99 | 2012-07-25 09:32:41 -0500 | [diff] [blame] | 1919 | mutex_lock_nested(&ctl_mutex, SINGLE_DEPTH_NESTING); |
Alex Elder | 117973f | 2012-08-31 17:29:55 -0500 | [diff] [blame] | 1920 | if (rbd_dev->image_format == 1) |
| 1921 | ret = rbd_dev_v1_refresh(rbd_dev, hver); |
| 1922 | else |
| 1923 | ret = rbd_dev_v2_refresh(rbd_dev, hver); |
Alex Elder | 1fe5e99 | 2012-07-25 09:32:41 -0500 | [diff] [blame] | 1924 | mutex_unlock(&ctl_mutex); |
| 1925 | |
| 1926 | return ret; |
| 1927 | } |
| 1928 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1929 | static int rbd_init_disk(struct rbd_device *rbd_dev) |
| 1930 | { |
| 1931 | struct gendisk *disk; |
| 1932 | struct request_queue *q; |
Alex Elder | 593a9e7 | 2012-02-07 12:03:37 -0600 | [diff] [blame] | 1933 | u64 segment_size; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1934 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1935 | /* create gendisk info */ |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1936 | disk = alloc_disk(RBD_MINORS_PER_MAJOR); |
| 1937 | if (!disk) |
Alex Elder | 1fcdb8a | 2012-08-29 17:11:06 -0500 | [diff] [blame] | 1938 | return -ENOMEM; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1939 | |
Alex Elder | f0f8cef | 2012-01-29 13:57:44 -0600 | [diff] [blame] | 1940 | snprintf(disk->disk_name, sizeof(disk->disk_name), RBD_DRV_NAME "%d", |
Alex Elder | de71a29 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 1941 | rbd_dev->dev_id); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1942 | disk->major = rbd_dev->major; |
| 1943 | disk->first_minor = 0; |
| 1944 | disk->fops = &rbd_bd_ops; |
| 1945 | disk->private_data = rbd_dev; |
| 1946 | |
| 1947 | /* init rq */ |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1948 | q = blk_init_queue(rbd_rq_fn, &rbd_dev->lock); |
| 1949 | if (!q) |
| 1950 | goto out_disk; |
Josh Durgin | 029bcbd | 2011-07-22 11:35:23 -0700 | [diff] [blame] | 1951 | |
Alex Elder | 593a9e7 | 2012-02-07 12:03:37 -0600 | [diff] [blame] | 1952 | /* We use the default size, but let's be explicit about it. */ |
| 1953 | blk_queue_physical_block_size(q, SECTOR_SIZE); |
| 1954 | |
Josh Durgin | 029bcbd | 2011-07-22 11:35:23 -0700 | [diff] [blame] | 1955 | /* set io sizes to object size */ |
Alex Elder | 593a9e7 | 2012-02-07 12:03:37 -0600 | [diff] [blame] | 1956 | segment_size = rbd_obj_bytes(&rbd_dev->header); |
| 1957 | blk_queue_max_hw_sectors(q, segment_size / SECTOR_SIZE); |
| 1958 | blk_queue_max_segment_size(q, segment_size); |
| 1959 | blk_queue_io_min(q, segment_size); |
| 1960 | blk_queue_io_opt(q, segment_size); |
Josh Durgin | 029bcbd | 2011-07-22 11:35:23 -0700 | [diff] [blame] | 1961 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1962 | blk_queue_merge_bvec(q, rbd_merge_bvec); |
| 1963 | disk->queue = q; |
| 1964 | |
| 1965 | q->queuedata = rbd_dev; |
| 1966 | |
| 1967 | rbd_dev->disk = disk; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1968 | |
Alex Elder | 12f0294 | 2012-08-29 17:11:07 -0500 | [diff] [blame] | 1969 | set_capacity(rbd_dev->disk, rbd_dev->mapping.size / SECTOR_SIZE); |
| 1970 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1971 | return 0; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1972 | out_disk: |
| 1973 | put_disk(disk); |
Alex Elder | 1fcdb8a | 2012-08-29 17:11:06 -0500 | [diff] [blame] | 1974 | |
| 1975 | return -ENOMEM; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1976 | } |
| 1977 | |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 1978 | /* |
| 1979 | sysfs |
| 1980 | */ |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1981 | |
Alex Elder | 593a9e7 | 2012-02-07 12:03:37 -0600 | [diff] [blame] | 1982 | static struct rbd_device *dev_to_rbd_dev(struct device *dev) |
| 1983 | { |
| 1984 | return container_of(dev, struct rbd_device, dev); |
| 1985 | } |
| 1986 | |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 1987 | static ssize_t rbd_size_show(struct device *dev, |
| 1988 | struct device_attribute *attr, char *buf) |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1989 | { |
Alex Elder | 593a9e7 | 2012-02-07 12:03:37 -0600 | [diff] [blame] | 1990 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
Josh Durgin | a51aa0c | 2011-12-05 10:35:04 -0800 | [diff] [blame] | 1991 | sector_t size; |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 1992 | |
Josh Durgin | a51aa0c | 2011-12-05 10:35:04 -0800 | [diff] [blame] | 1993 | down_read(&rbd_dev->header_rwsem); |
| 1994 | size = get_capacity(rbd_dev->disk); |
| 1995 | up_read(&rbd_dev->header_rwsem); |
| 1996 | |
| 1997 | return sprintf(buf, "%llu\n", (unsigned long long) size * SECTOR_SIZE); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1998 | } |
| 1999 | |
Alex Elder | 34b1318 | 2012-07-13 20:35:12 -0500 | [diff] [blame] | 2000 | /* |
| 2001 | * Note this shows the features for whatever's mapped, which is not |
| 2002 | * necessarily the base image. |
| 2003 | */ |
| 2004 | static ssize_t rbd_features_show(struct device *dev, |
| 2005 | struct device_attribute *attr, char *buf) |
| 2006 | { |
| 2007 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
| 2008 | |
| 2009 | return sprintf(buf, "0x%016llx\n", |
| 2010 | (unsigned long long) rbd_dev->mapping.features); |
| 2011 | } |
| 2012 | |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 2013 | static ssize_t rbd_major_show(struct device *dev, |
| 2014 | struct device_attribute *attr, char *buf) |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 2015 | { |
Alex Elder | 593a9e7 | 2012-02-07 12:03:37 -0600 | [diff] [blame] | 2016 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 2017 | |
| 2018 | return sprintf(buf, "%d\n", rbd_dev->major); |
| 2019 | } |
| 2020 | |
| 2021 | static ssize_t rbd_client_id_show(struct device *dev, |
| 2022 | struct device_attribute *attr, char *buf) |
| 2023 | { |
Alex Elder | 593a9e7 | 2012-02-07 12:03:37 -0600 | [diff] [blame] | 2024 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 2025 | |
Alex Elder | 1dbb439 | 2012-01-24 10:08:37 -0600 | [diff] [blame] | 2026 | return sprintf(buf, "client%lld\n", |
| 2027 | ceph_client_id(rbd_dev->rbd_client->client)); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 2028 | } |
| 2029 | |
| 2030 | static ssize_t rbd_pool_show(struct device *dev, |
| 2031 | struct device_attribute *attr, char *buf) |
| 2032 | { |
Alex Elder | 593a9e7 | 2012-02-07 12:03:37 -0600 | [diff] [blame] | 2033 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 2034 | |
Alex Elder | 0d7dbfc | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 2035 | return sprintf(buf, "%s\n", rbd_dev->spec->pool_name); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 2036 | } |
| 2037 | |
Alex Elder | 9bb2f33 | 2012-07-12 10:46:35 -0500 | [diff] [blame] | 2038 | static ssize_t rbd_pool_id_show(struct device *dev, |
| 2039 | struct device_attribute *attr, char *buf) |
| 2040 | { |
| 2041 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
| 2042 | |
Alex Elder | 0d7dbfc | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 2043 | return sprintf(buf, "%llu\n", |
| 2044 | (unsigned long long) rbd_dev->spec->pool_id); |
Alex Elder | 9bb2f33 | 2012-07-12 10:46:35 -0500 | [diff] [blame] | 2045 | } |
| 2046 | |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 2047 | static ssize_t rbd_name_show(struct device *dev, |
| 2048 | struct device_attribute *attr, char *buf) |
| 2049 | { |
Alex Elder | 593a9e7 | 2012-02-07 12:03:37 -0600 | [diff] [blame] | 2050 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 2051 | |
Alex Elder | a92ffdf | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 2052 | if (rbd_dev->spec->image_name) |
| 2053 | return sprintf(buf, "%s\n", rbd_dev->spec->image_name); |
| 2054 | |
| 2055 | return sprintf(buf, "(unknown)\n"); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 2056 | } |
| 2057 | |
Alex Elder | 589d30e | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 2058 | static ssize_t rbd_image_id_show(struct device *dev, |
| 2059 | struct device_attribute *attr, char *buf) |
| 2060 | { |
| 2061 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
| 2062 | |
Alex Elder | 0d7dbfc | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 2063 | return sprintf(buf, "%s\n", rbd_dev->spec->image_id); |
Alex Elder | 589d30e | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 2064 | } |
| 2065 | |
Alex Elder | 34b1318 | 2012-07-13 20:35:12 -0500 | [diff] [blame] | 2066 | /* |
| 2067 | * Shows the name of the currently-mapped snapshot (or |
| 2068 | * RBD_SNAP_HEAD_NAME for the base image). |
| 2069 | */ |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 2070 | static ssize_t rbd_snap_show(struct device *dev, |
| 2071 | struct device_attribute *attr, |
| 2072 | char *buf) |
| 2073 | { |
Alex Elder | 593a9e7 | 2012-02-07 12:03:37 -0600 | [diff] [blame] | 2074 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 2075 | |
Alex Elder | 0d7dbfc | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 2076 | return sprintf(buf, "%s\n", rbd_dev->spec->snap_name); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 2077 | } |
| 2078 | |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 2079 | /* |
| 2080 | * For an rbd v2 image, shows the pool id, image id, and snapshot id |
| 2081 | * for the parent image. If there is no parent, simply shows |
| 2082 | * "(no parent image)". |
| 2083 | */ |
| 2084 | static ssize_t rbd_parent_show(struct device *dev, |
| 2085 | struct device_attribute *attr, |
| 2086 | char *buf) |
| 2087 | { |
| 2088 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
| 2089 | struct rbd_spec *spec = rbd_dev->parent_spec; |
| 2090 | int count; |
| 2091 | char *bufp = buf; |
| 2092 | |
| 2093 | if (!spec) |
| 2094 | return sprintf(buf, "(no parent image)\n"); |
| 2095 | |
| 2096 | count = sprintf(bufp, "pool_id %llu\npool_name %s\n", |
| 2097 | (unsigned long long) spec->pool_id, spec->pool_name); |
| 2098 | if (count < 0) |
| 2099 | return count; |
| 2100 | bufp += count; |
| 2101 | |
| 2102 | count = sprintf(bufp, "image_id %s\nimage_name %s\n", spec->image_id, |
| 2103 | spec->image_name ? spec->image_name : "(unknown)"); |
| 2104 | if (count < 0) |
| 2105 | return count; |
| 2106 | bufp += count; |
| 2107 | |
| 2108 | count = sprintf(bufp, "snap_id %llu\nsnap_name %s\n", |
| 2109 | (unsigned long long) spec->snap_id, spec->snap_name); |
| 2110 | if (count < 0) |
| 2111 | return count; |
| 2112 | bufp += count; |
| 2113 | |
| 2114 | count = sprintf(bufp, "overlap %llu\n", rbd_dev->parent_overlap); |
| 2115 | if (count < 0) |
| 2116 | return count; |
| 2117 | bufp += count; |
| 2118 | |
| 2119 | return (ssize_t) (bufp - buf); |
| 2120 | } |
| 2121 | |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 2122 | static ssize_t rbd_image_refresh(struct device *dev, |
| 2123 | struct device_attribute *attr, |
| 2124 | const char *buf, |
| 2125 | size_t size) |
| 2126 | { |
Alex Elder | 593a9e7 | 2012-02-07 12:03:37 -0600 | [diff] [blame] | 2127 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
Alex Elder | b813623 | 2012-07-25 09:32:41 -0500 | [diff] [blame] | 2128 | int ret; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 2129 | |
Alex Elder | 117973f | 2012-08-31 17:29:55 -0500 | [diff] [blame] | 2130 | ret = rbd_dev_refresh(rbd_dev, NULL); |
Alex Elder | b813623 | 2012-07-25 09:32:41 -0500 | [diff] [blame] | 2131 | |
| 2132 | return ret < 0 ? ret : size; |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 2133 | } |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 2134 | |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 2135 | static DEVICE_ATTR(size, S_IRUGO, rbd_size_show, NULL); |
Alex Elder | 34b1318 | 2012-07-13 20:35:12 -0500 | [diff] [blame] | 2136 | static DEVICE_ATTR(features, S_IRUGO, rbd_features_show, NULL); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 2137 | static DEVICE_ATTR(major, S_IRUGO, rbd_major_show, NULL); |
| 2138 | static DEVICE_ATTR(client_id, S_IRUGO, rbd_client_id_show, NULL); |
| 2139 | static DEVICE_ATTR(pool, S_IRUGO, rbd_pool_show, NULL); |
Alex Elder | 9bb2f33 | 2012-07-12 10:46:35 -0500 | [diff] [blame] | 2140 | static DEVICE_ATTR(pool_id, S_IRUGO, rbd_pool_id_show, NULL); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 2141 | static DEVICE_ATTR(name, S_IRUGO, rbd_name_show, NULL); |
Alex Elder | 589d30e | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 2142 | static DEVICE_ATTR(image_id, S_IRUGO, rbd_image_id_show, NULL); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 2143 | static DEVICE_ATTR(refresh, S_IWUSR, NULL, rbd_image_refresh); |
| 2144 | static DEVICE_ATTR(current_snap, S_IRUGO, rbd_snap_show, NULL); |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 2145 | static DEVICE_ATTR(parent, S_IRUGO, rbd_parent_show, NULL); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 2146 | |
| 2147 | static struct attribute *rbd_attrs[] = { |
| 2148 | &dev_attr_size.attr, |
Alex Elder | 34b1318 | 2012-07-13 20:35:12 -0500 | [diff] [blame] | 2149 | &dev_attr_features.attr, |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 2150 | &dev_attr_major.attr, |
| 2151 | &dev_attr_client_id.attr, |
| 2152 | &dev_attr_pool.attr, |
Alex Elder | 9bb2f33 | 2012-07-12 10:46:35 -0500 | [diff] [blame] | 2153 | &dev_attr_pool_id.attr, |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 2154 | &dev_attr_name.attr, |
Alex Elder | 589d30e | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 2155 | &dev_attr_image_id.attr, |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 2156 | &dev_attr_current_snap.attr, |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 2157 | &dev_attr_parent.attr, |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 2158 | &dev_attr_refresh.attr, |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 2159 | NULL |
| 2160 | }; |
| 2161 | |
| 2162 | static struct attribute_group rbd_attr_group = { |
| 2163 | .attrs = rbd_attrs, |
| 2164 | }; |
| 2165 | |
| 2166 | static const struct attribute_group *rbd_attr_groups[] = { |
| 2167 | &rbd_attr_group, |
| 2168 | NULL |
| 2169 | }; |
| 2170 | |
| 2171 | static void rbd_sysfs_dev_release(struct device *dev) |
| 2172 | { |
| 2173 | } |
| 2174 | |
| 2175 | static struct device_type rbd_device_type = { |
| 2176 | .name = "rbd", |
| 2177 | .groups = rbd_attr_groups, |
| 2178 | .release = rbd_sysfs_dev_release, |
| 2179 | }; |
| 2180 | |
| 2181 | |
| 2182 | /* |
| 2183 | sysfs - snapshots |
| 2184 | */ |
| 2185 | |
| 2186 | static ssize_t rbd_snap_size_show(struct device *dev, |
| 2187 | struct device_attribute *attr, |
| 2188 | char *buf) |
| 2189 | { |
| 2190 | struct rbd_snap *snap = container_of(dev, struct rbd_snap, dev); |
| 2191 | |
Josh Durgin | 3591538 | 2011-12-05 18:25:13 -0800 | [diff] [blame] | 2192 | return sprintf(buf, "%llu\n", (unsigned long long)snap->size); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 2193 | } |
| 2194 | |
| 2195 | static ssize_t rbd_snap_id_show(struct device *dev, |
| 2196 | struct device_attribute *attr, |
| 2197 | char *buf) |
| 2198 | { |
| 2199 | struct rbd_snap *snap = container_of(dev, struct rbd_snap, dev); |
| 2200 | |
Josh Durgin | 3591538 | 2011-12-05 18:25:13 -0800 | [diff] [blame] | 2201 | return sprintf(buf, "%llu\n", (unsigned long long)snap->id); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 2202 | } |
| 2203 | |
Alex Elder | 34b1318 | 2012-07-13 20:35:12 -0500 | [diff] [blame] | 2204 | static ssize_t rbd_snap_features_show(struct device *dev, |
| 2205 | struct device_attribute *attr, |
| 2206 | char *buf) |
| 2207 | { |
| 2208 | struct rbd_snap *snap = container_of(dev, struct rbd_snap, dev); |
| 2209 | |
| 2210 | return sprintf(buf, "0x%016llx\n", |
| 2211 | (unsigned long long) snap->features); |
| 2212 | } |
| 2213 | |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 2214 | static DEVICE_ATTR(snap_size, S_IRUGO, rbd_snap_size_show, NULL); |
| 2215 | static DEVICE_ATTR(snap_id, S_IRUGO, rbd_snap_id_show, NULL); |
Alex Elder | 34b1318 | 2012-07-13 20:35:12 -0500 | [diff] [blame] | 2216 | static DEVICE_ATTR(snap_features, S_IRUGO, rbd_snap_features_show, NULL); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 2217 | |
| 2218 | static struct attribute *rbd_snap_attrs[] = { |
| 2219 | &dev_attr_snap_size.attr, |
| 2220 | &dev_attr_snap_id.attr, |
Alex Elder | 34b1318 | 2012-07-13 20:35:12 -0500 | [diff] [blame] | 2221 | &dev_attr_snap_features.attr, |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 2222 | NULL, |
| 2223 | }; |
| 2224 | |
| 2225 | static struct attribute_group rbd_snap_attr_group = { |
| 2226 | .attrs = rbd_snap_attrs, |
| 2227 | }; |
| 2228 | |
| 2229 | static void rbd_snap_dev_release(struct device *dev) |
| 2230 | { |
| 2231 | struct rbd_snap *snap = container_of(dev, struct rbd_snap, dev); |
| 2232 | kfree(snap->name); |
| 2233 | kfree(snap); |
| 2234 | } |
| 2235 | |
| 2236 | static const struct attribute_group *rbd_snap_attr_groups[] = { |
| 2237 | &rbd_snap_attr_group, |
| 2238 | NULL |
| 2239 | }; |
| 2240 | |
| 2241 | static struct device_type rbd_snap_device_type = { |
| 2242 | .groups = rbd_snap_attr_groups, |
| 2243 | .release = rbd_snap_dev_release, |
| 2244 | }; |
| 2245 | |
Alex Elder | 8b8fb99 | 2012-10-26 17:25:24 -0500 | [diff] [blame] | 2246 | static struct rbd_spec *rbd_spec_get(struct rbd_spec *spec) |
| 2247 | { |
| 2248 | kref_get(&spec->kref); |
| 2249 | |
| 2250 | return spec; |
| 2251 | } |
| 2252 | |
| 2253 | static void rbd_spec_free(struct kref *kref); |
| 2254 | static void rbd_spec_put(struct rbd_spec *spec) |
| 2255 | { |
| 2256 | if (spec) |
| 2257 | kref_put(&spec->kref, rbd_spec_free); |
| 2258 | } |
| 2259 | |
| 2260 | static struct rbd_spec *rbd_spec_alloc(void) |
| 2261 | { |
| 2262 | struct rbd_spec *spec; |
| 2263 | |
| 2264 | spec = kzalloc(sizeof (*spec), GFP_KERNEL); |
| 2265 | if (!spec) |
| 2266 | return NULL; |
| 2267 | kref_init(&spec->kref); |
| 2268 | |
| 2269 | rbd_spec_put(rbd_spec_get(spec)); /* TEMPORARY */ |
| 2270 | |
| 2271 | return spec; |
| 2272 | } |
| 2273 | |
| 2274 | static void rbd_spec_free(struct kref *kref) |
| 2275 | { |
| 2276 | struct rbd_spec *spec = container_of(kref, struct rbd_spec, kref); |
| 2277 | |
| 2278 | kfree(spec->pool_name); |
| 2279 | kfree(spec->image_id); |
| 2280 | kfree(spec->image_name); |
| 2281 | kfree(spec->snap_name); |
| 2282 | kfree(spec); |
| 2283 | } |
| 2284 | |
Alex Elder | c53d589 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 2285 | struct rbd_device *rbd_dev_create(struct rbd_client *rbdc, |
| 2286 | struct rbd_spec *spec) |
| 2287 | { |
| 2288 | struct rbd_device *rbd_dev; |
| 2289 | |
| 2290 | rbd_dev = kzalloc(sizeof (*rbd_dev), GFP_KERNEL); |
| 2291 | if (!rbd_dev) |
| 2292 | return NULL; |
| 2293 | |
| 2294 | spin_lock_init(&rbd_dev->lock); |
| 2295 | INIT_LIST_HEAD(&rbd_dev->node); |
| 2296 | INIT_LIST_HEAD(&rbd_dev->snaps); |
| 2297 | init_rwsem(&rbd_dev->header_rwsem); |
| 2298 | |
| 2299 | rbd_dev->spec = spec; |
| 2300 | rbd_dev->rbd_client = rbdc; |
| 2301 | |
| 2302 | return rbd_dev; |
| 2303 | } |
| 2304 | |
| 2305 | static void rbd_dev_destroy(struct rbd_device *rbd_dev) |
| 2306 | { |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 2307 | rbd_spec_put(rbd_dev->parent_spec); |
Alex Elder | c53d589 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 2308 | kfree(rbd_dev->header_name); |
| 2309 | rbd_put_client(rbd_dev->rbd_client); |
| 2310 | rbd_spec_put(rbd_dev->spec); |
| 2311 | kfree(rbd_dev); |
| 2312 | } |
| 2313 | |
Alex Elder | 304f680 | 2012-08-31 17:29:52 -0500 | [diff] [blame] | 2314 | static bool rbd_snap_registered(struct rbd_snap *snap) |
| 2315 | { |
| 2316 | bool ret = snap->dev.type == &rbd_snap_device_type; |
| 2317 | bool reg = device_is_registered(&snap->dev); |
| 2318 | |
| 2319 | rbd_assert(!ret ^ reg); |
| 2320 | |
| 2321 | return ret; |
| 2322 | } |
| 2323 | |
Alex Elder | 41f38c2 | 2012-10-25 23:34:40 -0500 | [diff] [blame] | 2324 | static void rbd_remove_snap_dev(struct rbd_snap *snap) |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 2325 | { |
| 2326 | list_del(&snap->node); |
Alex Elder | 304f680 | 2012-08-31 17:29:52 -0500 | [diff] [blame] | 2327 | if (device_is_registered(&snap->dev)) |
| 2328 | device_unregister(&snap->dev); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 2329 | } |
| 2330 | |
Alex Elder | 14e7085 | 2012-07-19 09:09:27 -0500 | [diff] [blame] | 2331 | static int rbd_register_snap_dev(struct rbd_snap *snap, |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 2332 | struct device *parent) |
| 2333 | { |
| 2334 | struct device *dev = &snap->dev; |
| 2335 | int ret; |
| 2336 | |
| 2337 | dev->type = &rbd_snap_device_type; |
| 2338 | dev->parent = parent; |
| 2339 | dev->release = rbd_snap_dev_release; |
Alex Elder | d4b125e | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 2340 | 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] | 2341 | dout("%s: registering device for snapshot %s\n", __func__, snap->name); |
| 2342 | |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 2343 | ret = device_register(dev); |
| 2344 | |
| 2345 | return ret; |
| 2346 | } |
| 2347 | |
Alex Elder | 4e891e0 | 2012-07-10 20:30:10 -0500 | [diff] [blame] | 2348 | 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] | 2349 | const char *snap_name, |
Alex Elder | 34b1318 | 2012-07-13 20:35:12 -0500 | [diff] [blame] | 2350 | u64 snap_id, u64 snap_size, |
| 2351 | u64 snap_features) |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 2352 | { |
Alex Elder | 4e891e0 | 2012-07-10 20:30:10 -0500 | [diff] [blame] | 2353 | struct rbd_snap *snap; |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 2354 | int ret; |
Alex Elder | 4e891e0 | 2012-07-10 20:30:10 -0500 | [diff] [blame] | 2355 | |
| 2356 | snap = kzalloc(sizeof (*snap), GFP_KERNEL); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 2357 | if (!snap) |
Alex Elder | 4e891e0 | 2012-07-10 20:30:10 -0500 | [diff] [blame] | 2358 | return ERR_PTR(-ENOMEM); |
| 2359 | |
| 2360 | ret = -ENOMEM; |
Alex Elder | c8d1842 | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 2361 | snap->name = kstrdup(snap_name, GFP_KERNEL); |
Alex Elder | 4e891e0 | 2012-07-10 20:30:10 -0500 | [diff] [blame] | 2362 | if (!snap->name) |
| 2363 | goto err; |
| 2364 | |
Alex Elder | c8d1842 | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 2365 | snap->id = snap_id; |
| 2366 | snap->size = snap_size; |
Alex Elder | 34b1318 | 2012-07-13 20:35:12 -0500 | [diff] [blame] | 2367 | snap->features = snap_features; |
Alex Elder | 4e891e0 | 2012-07-10 20:30:10 -0500 | [diff] [blame] | 2368 | |
| 2369 | return snap; |
| 2370 | |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 2371 | err: |
| 2372 | kfree(snap->name); |
| 2373 | kfree(snap); |
Alex Elder | 4e891e0 | 2012-07-10 20:30:10 -0500 | [diff] [blame] | 2374 | |
| 2375 | return ERR_PTR(ret); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 2376 | } |
| 2377 | |
Alex Elder | cd89212 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 2378 | static char *rbd_dev_v1_snap_info(struct rbd_device *rbd_dev, u32 which, |
| 2379 | u64 *snap_size, u64 *snap_features) |
| 2380 | { |
| 2381 | char *snap_name; |
| 2382 | |
| 2383 | rbd_assert(which < rbd_dev->header.snapc->num_snaps); |
| 2384 | |
| 2385 | *snap_size = rbd_dev->header.snap_sizes[which]; |
| 2386 | *snap_features = 0; /* No features for v1 */ |
| 2387 | |
| 2388 | /* Skip over names until we find the one we are looking for */ |
| 2389 | |
| 2390 | snap_name = rbd_dev->header.snap_names; |
| 2391 | while (which--) |
| 2392 | snap_name += strlen(snap_name) + 1; |
| 2393 | |
| 2394 | return snap_name; |
| 2395 | } |
| 2396 | |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 2397 | /* |
Alex Elder | 9d475de | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 2398 | * Get the size and object order for an image snapshot, or if |
| 2399 | * snap_id is CEPH_NOSNAP, gets this information for the base |
| 2400 | * image. |
| 2401 | */ |
| 2402 | static int _rbd_dev_v2_snap_size(struct rbd_device *rbd_dev, u64 snap_id, |
| 2403 | u8 *order, u64 *snap_size) |
| 2404 | { |
| 2405 | __le64 snapid = cpu_to_le64(snap_id); |
| 2406 | int ret; |
| 2407 | struct { |
| 2408 | u8 order; |
| 2409 | __le64 size; |
| 2410 | } __attribute__ ((packed)) size_buf = { 0 }; |
| 2411 | |
| 2412 | ret = rbd_req_sync_exec(rbd_dev, rbd_dev->header_name, |
| 2413 | "rbd", "get_size", |
| 2414 | (char *) &snapid, sizeof (snapid), |
| 2415 | (char *) &size_buf, sizeof (size_buf), |
| 2416 | CEPH_OSD_FLAG_READ, NULL); |
| 2417 | dout("%s: rbd_req_sync_exec returned %d\n", __func__, ret); |
| 2418 | if (ret < 0) |
| 2419 | return ret; |
| 2420 | |
| 2421 | *order = size_buf.order; |
| 2422 | *snap_size = le64_to_cpu(size_buf.size); |
| 2423 | |
| 2424 | dout(" snap_id 0x%016llx order = %u, snap_size = %llu\n", |
| 2425 | (unsigned long long) snap_id, (unsigned int) *order, |
| 2426 | (unsigned long long) *snap_size); |
| 2427 | |
| 2428 | return 0; |
| 2429 | } |
| 2430 | |
| 2431 | static int rbd_dev_v2_image_size(struct rbd_device *rbd_dev) |
| 2432 | { |
| 2433 | return _rbd_dev_v2_snap_size(rbd_dev, CEPH_NOSNAP, |
| 2434 | &rbd_dev->header.obj_order, |
| 2435 | &rbd_dev->header.image_size); |
| 2436 | } |
| 2437 | |
Alex Elder | 1e13019 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 2438 | static int rbd_dev_v2_object_prefix(struct rbd_device *rbd_dev) |
| 2439 | { |
| 2440 | void *reply_buf; |
| 2441 | int ret; |
| 2442 | void *p; |
| 2443 | |
| 2444 | reply_buf = kzalloc(RBD_OBJ_PREFIX_LEN_MAX, GFP_KERNEL); |
| 2445 | if (!reply_buf) |
| 2446 | return -ENOMEM; |
| 2447 | |
| 2448 | ret = rbd_req_sync_exec(rbd_dev, rbd_dev->header_name, |
| 2449 | "rbd", "get_object_prefix", |
| 2450 | NULL, 0, |
| 2451 | reply_buf, RBD_OBJ_PREFIX_LEN_MAX, |
| 2452 | CEPH_OSD_FLAG_READ, NULL); |
| 2453 | dout("%s: rbd_req_sync_exec returned %d\n", __func__, ret); |
| 2454 | if (ret < 0) |
| 2455 | goto out; |
Alex Elder | a0ea3a4 | 2012-10-10 21:19:13 -0700 | [diff] [blame] | 2456 | ret = 0; /* rbd_req_sync_exec() can return positive */ |
Alex Elder | 1e13019 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 2457 | |
| 2458 | p = reply_buf; |
| 2459 | rbd_dev->header.object_prefix = ceph_extract_encoded_string(&p, |
| 2460 | p + RBD_OBJ_PREFIX_LEN_MAX, |
| 2461 | NULL, GFP_NOIO); |
| 2462 | |
| 2463 | if (IS_ERR(rbd_dev->header.object_prefix)) { |
| 2464 | ret = PTR_ERR(rbd_dev->header.object_prefix); |
| 2465 | rbd_dev->header.object_prefix = NULL; |
| 2466 | } else { |
| 2467 | dout(" object_prefix = %s\n", rbd_dev->header.object_prefix); |
| 2468 | } |
| 2469 | |
| 2470 | out: |
| 2471 | kfree(reply_buf); |
| 2472 | |
| 2473 | return ret; |
| 2474 | } |
| 2475 | |
Alex Elder | b1b5402 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 2476 | static int _rbd_dev_v2_snap_features(struct rbd_device *rbd_dev, u64 snap_id, |
| 2477 | u64 *snap_features) |
| 2478 | { |
| 2479 | __le64 snapid = cpu_to_le64(snap_id); |
| 2480 | struct { |
| 2481 | __le64 features; |
| 2482 | __le64 incompat; |
| 2483 | } features_buf = { 0 }; |
Alex Elder | d889140 | 2012-10-09 13:50:17 -0700 | [diff] [blame] | 2484 | u64 incompat; |
Alex Elder | b1b5402 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 2485 | int ret; |
| 2486 | |
| 2487 | ret = rbd_req_sync_exec(rbd_dev, rbd_dev->header_name, |
| 2488 | "rbd", "get_features", |
| 2489 | (char *) &snapid, sizeof (snapid), |
| 2490 | (char *) &features_buf, sizeof (features_buf), |
| 2491 | CEPH_OSD_FLAG_READ, NULL); |
| 2492 | dout("%s: rbd_req_sync_exec returned %d\n", __func__, ret); |
| 2493 | if (ret < 0) |
| 2494 | return ret; |
Alex Elder | d889140 | 2012-10-09 13:50:17 -0700 | [diff] [blame] | 2495 | |
| 2496 | incompat = le64_to_cpu(features_buf.incompat); |
| 2497 | if (incompat & ~RBD_FEATURES_ALL) |
Alex Elder | b8f5c6e | 2012-11-01 08:39:26 -0500 | [diff] [blame] | 2498 | return -ENXIO; |
Alex Elder | d889140 | 2012-10-09 13:50:17 -0700 | [diff] [blame] | 2499 | |
Alex Elder | b1b5402 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 2500 | *snap_features = le64_to_cpu(features_buf.features); |
| 2501 | |
| 2502 | dout(" snap_id 0x%016llx features = 0x%016llx incompat = 0x%016llx\n", |
| 2503 | (unsigned long long) snap_id, |
| 2504 | (unsigned long long) *snap_features, |
| 2505 | (unsigned long long) le64_to_cpu(features_buf.incompat)); |
| 2506 | |
| 2507 | return 0; |
| 2508 | } |
| 2509 | |
| 2510 | static int rbd_dev_v2_features(struct rbd_device *rbd_dev) |
| 2511 | { |
| 2512 | return _rbd_dev_v2_snap_features(rbd_dev, CEPH_NOSNAP, |
| 2513 | &rbd_dev->header.features); |
| 2514 | } |
| 2515 | |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 2516 | static int rbd_dev_v2_parent_info(struct rbd_device *rbd_dev) |
| 2517 | { |
| 2518 | struct rbd_spec *parent_spec; |
| 2519 | size_t size; |
| 2520 | void *reply_buf = NULL; |
| 2521 | __le64 snapid; |
| 2522 | void *p; |
| 2523 | void *end; |
| 2524 | char *image_id; |
| 2525 | u64 overlap; |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 2526 | int ret; |
| 2527 | |
| 2528 | parent_spec = rbd_spec_alloc(); |
| 2529 | if (!parent_spec) |
| 2530 | return -ENOMEM; |
| 2531 | |
| 2532 | size = sizeof (__le64) + /* pool_id */ |
| 2533 | sizeof (__le32) + RBD_IMAGE_ID_LEN_MAX + /* image_id */ |
| 2534 | sizeof (__le64) + /* snap_id */ |
| 2535 | sizeof (__le64); /* overlap */ |
| 2536 | reply_buf = kmalloc(size, GFP_KERNEL); |
| 2537 | if (!reply_buf) { |
| 2538 | ret = -ENOMEM; |
| 2539 | goto out_err; |
| 2540 | } |
| 2541 | |
| 2542 | snapid = cpu_to_le64(CEPH_NOSNAP); |
| 2543 | ret = rbd_req_sync_exec(rbd_dev, rbd_dev->header_name, |
| 2544 | "rbd", "get_parent", |
| 2545 | (char *) &snapid, sizeof (snapid), |
| 2546 | (char *) reply_buf, size, |
| 2547 | CEPH_OSD_FLAG_READ, NULL); |
| 2548 | dout("%s: rbd_req_sync_exec returned %d\n", __func__, ret); |
| 2549 | if (ret < 0) |
| 2550 | goto out_err; |
| 2551 | |
| 2552 | ret = -ERANGE; |
| 2553 | p = reply_buf; |
| 2554 | end = (char *) reply_buf + size; |
| 2555 | ceph_decode_64_safe(&p, end, parent_spec->pool_id, out_err); |
| 2556 | if (parent_spec->pool_id == CEPH_NOPOOL) |
| 2557 | goto out; /* No parent? No problem. */ |
| 2558 | |
Alex Elder | 979ed48 | 2012-11-01 08:39:26 -0500 | [diff] [blame] | 2559 | image_id = ceph_extract_encoded_string(&p, end, NULL, GFP_KERNEL); |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 2560 | if (IS_ERR(image_id)) { |
| 2561 | ret = PTR_ERR(image_id); |
| 2562 | goto out_err; |
| 2563 | } |
| 2564 | parent_spec->image_id = image_id; |
| 2565 | ceph_decode_64_safe(&p, end, parent_spec->snap_id, out_err); |
| 2566 | ceph_decode_64_safe(&p, end, overlap, out_err); |
| 2567 | |
| 2568 | rbd_dev->parent_overlap = overlap; |
| 2569 | rbd_dev->parent_spec = parent_spec; |
| 2570 | parent_spec = NULL; /* rbd_dev now owns this */ |
| 2571 | out: |
| 2572 | ret = 0; |
| 2573 | out_err: |
| 2574 | kfree(reply_buf); |
| 2575 | rbd_spec_put(parent_spec); |
| 2576 | |
| 2577 | return ret; |
| 2578 | } |
| 2579 | |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 2580 | static char *rbd_dev_image_name(struct rbd_device *rbd_dev) |
| 2581 | { |
| 2582 | size_t image_id_size; |
| 2583 | char *image_id; |
| 2584 | void *p; |
| 2585 | void *end; |
| 2586 | size_t size; |
| 2587 | void *reply_buf = NULL; |
| 2588 | size_t len = 0; |
| 2589 | char *image_name = NULL; |
| 2590 | int ret; |
| 2591 | |
| 2592 | rbd_assert(!rbd_dev->spec->image_name); |
| 2593 | |
Alex Elder | 69e7a02 | 2012-11-01 08:39:26 -0500 | [diff] [blame] | 2594 | len = strlen(rbd_dev->spec->image_id); |
| 2595 | image_id_size = sizeof (__le32) + len; |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 2596 | image_id = kmalloc(image_id_size, GFP_KERNEL); |
| 2597 | if (!image_id) |
| 2598 | return NULL; |
| 2599 | |
| 2600 | p = image_id; |
| 2601 | end = (char *) image_id + image_id_size; |
Alex Elder | 69e7a02 | 2012-11-01 08:39:26 -0500 | [diff] [blame] | 2602 | ceph_encode_string(&p, end, rbd_dev->spec->image_id, (u32) len); |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 2603 | |
| 2604 | size = sizeof (__le32) + RBD_IMAGE_NAME_LEN_MAX; |
| 2605 | reply_buf = kmalloc(size, GFP_KERNEL); |
| 2606 | if (!reply_buf) |
| 2607 | goto out; |
| 2608 | |
| 2609 | ret = rbd_req_sync_exec(rbd_dev, RBD_DIRECTORY, |
| 2610 | "rbd", "dir_get_name", |
| 2611 | image_id, image_id_size, |
| 2612 | (char *) reply_buf, size, |
| 2613 | CEPH_OSD_FLAG_READ, NULL); |
| 2614 | if (ret < 0) |
| 2615 | goto out; |
| 2616 | p = reply_buf; |
| 2617 | end = (char *) reply_buf + size; |
| 2618 | image_name = ceph_extract_encoded_string(&p, end, &len, GFP_KERNEL); |
| 2619 | if (IS_ERR(image_name)) |
| 2620 | image_name = NULL; |
| 2621 | else |
| 2622 | dout("%s: name is %s len is %zd\n", __func__, image_name, len); |
| 2623 | out: |
| 2624 | kfree(reply_buf); |
| 2625 | kfree(image_id); |
| 2626 | |
| 2627 | return image_name; |
| 2628 | } |
| 2629 | |
| 2630 | /* |
| 2631 | * When a parent image gets probed, we only have the pool, image, |
| 2632 | * and snapshot ids but not the names of any of them. This call |
| 2633 | * is made later to fill in those names. It has to be done after |
| 2634 | * rbd_dev_snaps_update() has completed because some of the |
| 2635 | * information (in particular, snapshot name) is not available |
| 2636 | * until then. |
| 2637 | */ |
| 2638 | static int rbd_dev_probe_update_spec(struct rbd_device *rbd_dev) |
| 2639 | { |
| 2640 | struct ceph_osd_client *osdc; |
| 2641 | const char *name; |
| 2642 | void *reply_buf = NULL; |
| 2643 | int ret; |
| 2644 | |
| 2645 | if (rbd_dev->spec->pool_name) |
| 2646 | return 0; /* Already have the names */ |
| 2647 | |
| 2648 | /* Look up the pool name */ |
| 2649 | |
| 2650 | osdc = &rbd_dev->rbd_client->client->osdc; |
| 2651 | name = ceph_pg_pool_name_by_id(osdc->osdmap, rbd_dev->spec->pool_id); |
Alex Elder | 935dc89 | 2012-11-01 10:17:15 -0500 | [diff] [blame] | 2652 | if (!name) { |
| 2653 | rbd_warn(rbd_dev, "there is no pool with id %llu", |
| 2654 | rbd_dev->spec->pool_id); /* Really a BUG() */ |
| 2655 | return -EIO; |
| 2656 | } |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 2657 | |
| 2658 | rbd_dev->spec->pool_name = kstrdup(name, GFP_KERNEL); |
| 2659 | if (!rbd_dev->spec->pool_name) |
| 2660 | return -ENOMEM; |
| 2661 | |
| 2662 | /* Fetch the image name; tolerate failure here */ |
| 2663 | |
| 2664 | name = rbd_dev_image_name(rbd_dev); |
Alex Elder | 69e7a02 | 2012-11-01 08:39:26 -0500 | [diff] [blame] | 2665 | if (name) |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 2666 | rbd_dev->spec->image_name = (char *) name; |
Alex Elder | 69e7a02 | 2012-11-01 08:39:26 -0500 | [diff] [blame] | 2667 | else |
Alex Elder | 06ecc6c | 2012-11-01 10:17:15 -0500 | [diff] [blame] | 2668 | rbd_warn(rbd_dev, "unable to get image name"); |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 2669 | |
| 2670 | /* Look up the snapshot name. */ |
| 2671 | |
| 2672 | name = rbd_snap_name(rbd_dev, rbd_dev->spec->snap_id); |
| 2673 | if (!name) { |
Alex Elder | 935dc89 | 2012-11-01 10:17:15 -0500 | [diff] [blame] | 2674 | rbd_warn(rbd_dev, "no snapshot with id %llu", |
| 2675 | rbd_dev->spec->snap_id); /* Really a BUG() */ |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 2676 | ret = -EIO; |
| 2677 | goto out_err; |
| 2678 | } |
| 2679 | rbd_dev->spec->snap_name = kstrdup(name, GFP_KERNEL); |
| 2680 | if(!rbd_dev->spec->snap_name) |
| 2681 | goto out_err; |
| 2682 | |
| 2683 | return 0; |
| 2684 | out_err: |
| 2685 | kfree(reply_buf); |
| 2686 | kfree(rbd_dev->spec->pool_name); |
| 2687 | rbd_dev->spec->pool_name = NULL; |
| 2688 | |
| 2689 | return ret; |
| 2690 | } |
| 2691 | |
Alex Elder | 6e14b1a | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 2692 | 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] | 2693 | { |
| 2694 | size_t size; |
| 2695 | int ret; |
| 2696 | void *reply_buf; |
| 2697 | void *p; |
| 2698 | void *end; |
| 2699 | u64 seq; |
| 2700 | u32 snap_count; |
| 2701 | struct ceph_snap_context *snapc; |
| 2702 | u32 i; |
| 2703 | |
| 2704 | /* |
| 2705 | * We'll need room for the seq value (maximum snapshot id), |
| 2706 | * snapshot count, and array of that many snapshot ids. |
| 2707 | * For now we have a fixed upper limit on the number we're |
| 2708 | * prepared to receive. |
| 2709 | */ |
| 2710 | size = sizeof (__le64) + sizeof (__le32) + |
| 2711 | RBD_MAX_SNAP_COUNT * sizeof (__le64); |
| 2712 | reply_buf = kzalloc(size, GFP_KERNEL); |
| 2713 | if (!reply_buf) |
| 2714 | return -ENOMEM; |
| 2715 | |
| 2716 | ret = rbd_req_sync_exec(rbd_dev, rbd_dev->header_name, |
| 2717 | "rbd", "get_snapcontext", |
| 2718 | NULL, 0, |
| 2719 | reply_buf, size, |
Alex Elder | 6e14b1a | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 2720 | CEPH_OSD_FLAG_READ, ver); |
Alex Elder | 35d489f | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 2721 | dout("%s: rbd_req_sync_exec returned %d\n", __func__, ret); |
| 2722 | if (ret < 0) |
| 2723 | goto out; |
| 2724 | |
| 2725 | ret = -ERANGE; |
| 2726 | p = reply_buf; |
| 2727 | end = (char *) reply_buf + size; |
| 2728 | ceph_decode_64_safe(&p, end, seq, out); |
| 2729 | ceph_decode_32_safe(&p, end, snap_count, out); |
| 2730 | |
| 2731 | /* |
| 2732 | * Make sure the reported number of snapshot ids wouldn't go |
| 2733 | * beyond the end of our buffer. But before checking that, |
| 2734 | * make sure the computed size of the snapshot context we |
| 2735 | * allocate is representable in a size_t. |
| 2736 | */ |
| 2737 | if (snap_count > (SIZE_MAX - sizeof (struct ceph_snap_context)) |
| 2738 | / sizeof (u64)) { |
| 2739 | ret = -EINVAL; |
| 2740 | goto out; |
| 2741 | } |
| 2742 | if (!ceph_has_room(&p, end, snap_count * sizeof (__le64))) |
| 2743 | goto out; |
| 2744 | |
| 2745 | size = sizeof (struct ceph_snap_context) + |
| 2746 | snap_count * sizeof (snapc->snaps[0]); |
| 2747 | snapc = kmalloc(size, GFP_KERNEL); |
| 2748 | if (!snapc) { |
| 2749 | ret = -ENOMEM; |
| 2750 | goto out; |
| 2751 | } |
| 2752 | |
| 2753 | atomic_set(&snapc->nref, 1); |
| 2754 | snapc->seq = seq; |
| 2755 | snapc->num_snaps = snap_count; |
| 2756 | for (i = 0; i < snap_count; i++) |
| 2757 | snapc->snaps[i] = ceph_decode_64(&p); |
| 2758 | |
| 2759 | rbd_dev->header.snapc = snapc; |
| 2760 | |
| 2761 | dout(" snap context seq = %llu, snap_count = %u\n", |
| 2762 | (unsigned long long) seq, (unsigned int) snap_count); |
| 2763 | |
| 2764 | out: |
| 2765 | kfree(reply_buf); |
| 2766 | |
| 2767 | return 0; |
| 2768 | } |
| 2769 | |
Alex Elder | b8b1e2d | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 2770 | static char *rbd_dev_v2_snap_name(struct rbd_device *rbd_dev, u32 which) |
| 2771 | { |
| 2772 | size_t size; |
| 2773 | void *reply_buf; |
| 2774 | __le64 snap_id; |
| 2775 | int ret; |
| 2776 | void *p; |
| 2777 | void *end; |
Alex Elder | b8b1e2d | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 2778 | char *snap_name; |
| 2779 | |
| 2780 | size = sizeof (__le32) + RBD_MAX_SNAP_NAME_LEN; |
| 2781 | reply_buf = kmalloc(size, GFP_KERNEL); |
| 2782 | if (!reply_buf) |
| 2783 | return ERR_PTR(-ENOMEM); |
| 2784 | |
| 2785 | snap_id = cpu_to_le64(rbd_dev->header.snapc->snaps[which]); |
| 2786 | ret = rbd_req_sync_exec(rbd_dev, rbd_dev->header_name, |
| 2787 | "rbd", "get_snapshot_name", |
| 2788 | (char *) &snap_id, sizeof (snap_id), |
| 2789 | reply_buf, size, |
| 2790 | CEPH_OSD_FLAG_READ, NULL); |
| 2791 | dout("%s: rbd_req_sync_exec returned %d\n", __func__, ret); |
| 2792 | if (ret < 0) |
| 2793 | goto out; |
| 2794 | |
| 2795 | p = reply_buf; |
| 2796 | end = (char *) reply_buf + size; |
Alex Elder | e5c3553 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 2797 | snap_name = ceph_extract_encoded_string(&p, end, NULL, GFP_KERNEL); |
Alex Elder | b8b1e2d | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 2798 | if (IS_ERR(snap_name)) { |
| 2799 | ret = PTR_ERR(snap_name); |
| 2800 | goto out; |
| 2801 | } else { |
| 2802 | dout(" snap_id 0x%016llx snap_name = %s\n", |
| 2803 | (unsigned long long) le64_to_cpu(snap_id), snap_name); |
| 2804 | } |
| 2805 | kfree(reply_buf); |
| 2806 | |
| 2807 | return snap_name; |
| 2808 | out: |
| 2809 | kfree(reply_buf); |
| 2810 | |
| 2811 | return ERR_PTR(ret); |
| 2812 | } |
| 2813 | |
| 2814 | static char *rbd_dev_v2_snap_info(struct rbd_device *rbd_dev, u32 which, |
| 2815 | u64 *snap_size, u64 *snap_features) |
| 2816 | { |
| 2817 | __le64 snap_id; |
| 2818 | u8 order; |
| 2819 | int ret; |
| 2820 | |
| 2821 | snap_id = rbd_dev->header.snapc->snaps[which]; |
| 2822 | ret = _rbd_dev_v2_snap_size(rbd_dev, snap_id, &order, snap_size); |
| 2823 | if (ret) |
| 2824 | return ERR_PTR(ret); |
| 2825 | ret = _rbd_dev_v2_snap_features(rbd_dev, snap_id, snap_features); |
| 2826 | if (ret) |
| 2827 | return ERR_PTR(ret); |
| 2828 | |
| 2829 | return rbd_dev_v2_snap_name(rbd_dev, which); |
| 2830 | } |
| 2831 | |
| 2832 | static char *rbd_dev_snap_info(struct rbd_device *rbd_dev, u32 which, |
| 2833 | u64 *snap_size, u64 *snap_features) |
| 2834 | { |
| 2835 | if (rbd_dev->image_format == 1) |
| 2836 | return rbd_dev_v1_snap_info(rbd_dev, which, |
| 2837 | snap_size, snap_features); |
| 2838 | if (rbd_dev->image_format == 2) |
| 2839 | return rbd_dev_v2_snap_info(rbd_dev, which, |
| 2840 | snap_size, snap_features); |
| 2841 | return ERR_PTR(-EINVAL); |
| 2842 | } |
| 2843 | |
Alex Elder | 117973f | 2012-08-31 17:29:55 -0500 | [diff] [blame] | 2844 | static int rbd_dev_v2_refresh(struct rbd_device *rbd_dev, u64 *hver) |
| 2845 | { |
| 2846 | int ret; |
| 2847 | __u8 obj_order; |
| 2848 | |
| 2849 | down_write(&rbd_dev->header_rwsem); |
| 2850 | |
| 2851 | /* Grab old order first, to see if it changes */ |
| 2852 | |
| 2853 | obj_order = rbd_dev->header.obj_order, |
| 2854 | ret = rbd_dev_v2_image_size(rbd_dev); |
| 2855 | if (ret) |
| 2856 | goto out; |
| 2857 | if (rbd_dev->header.obj_order != obj_order) { |
| 2858 | ret = -EIO; |
| 2859 | goto out; |
| 2860 | } |
| 2861 | rbd_update_mapping_size(rbd_dev); |
| 2862 | |
| 2863 | ret = rbd_dev_v2_snap_context(rbd_dev, hver); |
| 2864 | dout("rbd_dev_v2_snap_context returned %d\n", ret); |
| 2865 | if (ret) |
| 2866 | goto out; |
| 2867 | ret = rbd_dev_snaps_update(rbd_dev); |
| 2868 | dout("rbd_dev_snaps_update returned %d\n", ret); |
| 2869 | if (ret) |
| 2870 | goto out; |
| 2871 | ret = rbd_dev_snaps_register(rbd_dev); |
| 2872 | dout("rbd_dev_snaps_register returned %d\n", ret); |
| 2873 | out: |
| 2874 | up_write(&rbd_dev->header_rwsem); |
| 2875 | |
| 2876 | return ret; |
| 2877 | } |
| 2878 | |
Alex Elder | 9d475de | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 2879 | /* |
Alex Elder | 3593815 | 2012-08-02 11:29:46 -0500 | [diff] [blame] | 2880 | * Scan the rbd device's current snapshot list and compare it to the |
| 2881 | * newly-received snapshot context. Remove any existing snapshots |
| 2882 | * not present in the new snapshot context. Add a new snapshot for |
| 2883 | * any snaphots in the snapshot context not in the current list. |
| 2884 | * And verify there are no changes to snapshots we already know |
| 2885 | * about. |
| 2886 | * |
| 2887 | * Assumes the snapshots in the snapshot context are sorted by |
| 2888 | * snapshot id, highest id first. (Snapshots in the rbd_dev's list |
| 2889 | * are also maintained in that order.) |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 2890 | */ |
Alex Elder | 304f680 | 2012-08-31 17:29:52 -0500 | [diff] [blame] | 2891 | static int rbd_dev_snaps_update(struct rbd_device *rbd_dev) |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 2892 | { |
Alex Elder | 3593815 | 2012-08-02 11:29:46 -0500 | [diff] [blame] | 2893 | struct ceph_snap_context *snapc = rbd_dev->header.snapc; |
| 2894 | const u32 snap_count = snapc->num_snaps; |
Alex Elder | 3593815 | 2012-08-02 11:29:46 -0500 | [diff] [blame] | 2895 | struct list_head *head = &rbd_dev->snaps; |
| 2896 | struct list_head *links = head->next; |
| 2897 | u32 index = 0; |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 2898 | |
Alex Elder | 9fcbb80 | 2012-08-23 23:48:49 -0500 | [diff] [blame] | 2899 | dout("%s: snap count is %u\n", __func__, (unsigned int) snap_count); |
Alex Elder | 3593815 | 2012-08-02 11:29:46 -0500 | [diff] [blame] | 2900 | while (index < snap_count || links != head) { |
| 2901 | u64 snap_id; |
| 2902 | struct rbd_snap *snap; |
Alex Elder | cd89212 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 2903 | char *snap_name; |
| 2904 | u64 snap_size = 0; |
| 2905 | u64 snap_features = 0; |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 2906 | |
Alex Elder | 3593815 | 2012-08-02 11:29:46 -0500 | [diff] [blame] | 2907 | snap_id = index < snap_count ? snapc->snaps[index] |
| 2908 | : CEPH_NOSNAP; |
| 2909 | snap = links != head ? list_entry(links, struct rbd_snap, node) |
| 2910 | : NULL; |
Alex Elder | aafb230 | 2012-09-06 16:00:54 -0500 | [diff] [blame] | 2911 | rbd_assert(!snap || snap->id != CEPH_NOSNAP); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 2912 | |
Alex Elder | 3593815 | 2012-08-02 11:29:46 -0500 | [diff] [blame] | 2913 | if (snap_id == CEPH_NOSNAP || (snap && snap->id > snap_id)) { |
| 2914 | struct list_head *next = links->next; |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 2915 | |
Alex Elder | 3593815 | 2012-08-02 11:29:46 -0500 | [diff] [blame] | 2916 | /* Existing snapshot not in the new snap context */ |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 2917 | |
Alex Elder | 0d7dbfc | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 2918 | if (rbd_dev->spec->snap_id == snap->id) |
Alex Elder | daba5fd | 2012-10-26 17:25:23 -0500 | [diff] [blame] | 2919 | rbd_dev->exists = false; |
Alex Elder | 41f38c2 | 2012-10-25 23:34:40 -0500 | [diff] [blame] | 2920 | rbd_remove_snap_dev(snap); |
Alex Elder | 9fcbb80 | 2012-08-23 23:48:49 -0500 | [diff] [blame] | 2921 | dout("%ssnap id %llu has been removed\n", |
Alex Elder | 0d7dbfc | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 2922 | rbd_dev->spec->snap_id == snap->id ? |
| 2923 | "mapped " : "", |
Alex Elder | 9fcbb80 | 2012-08-23 23:48:49 -0500 | [diff] [blame] | 2924 | (unsigned long long) snap->id); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 2925 | |
Alex Elder | 3593815 | 2012-08-02 11:29:46 -0500 | [diff] [blame] | 2926 | /* Done with this list entry; advance */ |
| 2927 | |
| 2928 | links = next; |
| 2929 | continue; |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 2930 | } |
Alex Elder | 3593815 | 2012-08-02 11:29:46 -0500 | [diff] [blame] | 2931 | |
Alex Elder | b8b1e2d | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 2932 | snap_name = rbd_dev_snap_info(rbd_dev, index, |
| 2933 | &snap_size, &snap_features); |
Alex Elder | cd89212 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 2934 | if (IS_ERR(snap_name)) |
| 2935 | return PTR_ERR(snap_name); |
| 2936 | |
Alex Elder | 9fcbb80 | 2012-08-23 23:48:49 -0500 | [diff] [blame] | 2937 | dout("entry %u: snap_id = %llu\n", (unsigned int) snap_count, |
| 2938 | (unsigned long long) snap_id); |
Alex Elder | 3593815 | 2012-08-02 11:29:46 -0500 | [diff] [blame] | 2939 | if (!snap || (snap_id != CEPH_NOSNAP && snap->id < snap_id)) { |
| 2940 | struct rbd_snap *new_snap; |
| 2941 | |
| 2942 | /* We haven't seen this snapshot before */ |
| 2943 | |
Alex Elder | c8d1842 | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 2944 | new_snap = __rbd_add_snap_dev(rbd_dev, snap_name, |
Alex Elder | cd89212 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 2945 | snap_id, snap_size, snap_features); |
Alex Elder | 9fcbb80 | 2012-08-23 23:48:49 -0500 | [diff] [blame] | 2946 | if (IS_ERR(new_snap)) { |
| 2947 | int err = PTR_ERR(new_snap); |
| 2948 | |
| 2949 | dout(" failed to add dev, error %d\n", err); |
| 2950 | |
| 2951 | return err; |
| 2952 | } |
Alex Elder | 3593815 | 2012-08-02 11:29:46 -0500 | [diff] [blame] | 2953 | |
| 2954 | /* New goes before existing, or at end of list */ |
| 2955 | |
Alex Elder | 9fcbb80 | 2012-08-23 23:48:49 -0500 | [diff] [blame] | 2956 | dout(" added dev%s\n", snap ? "" : " at end\n"); |
Alex Elder | 3593815 | 2012-08-02 11:29:46 -0500 | [diff] [blame] | 2957 | if (snap) |
| 2958 | list_add_tail(&new_snap->node, &snap->node); |
| 2959 | else |
Alex Elder | 523f325 | 2012-08-30 00:16:37 -0500 | [diff] [blame] | 2960 | list_add_tail(&new_snap->node, head); |
Alex Elder | 3593815 | 2012-08-02 11:29:46 -0500 | [diff] [blame] | 2961 | } else { |
| 2962 | /* Already have this one */ |
| 2963 | |
Alex Elder | 9fcbb80 | 2012-08-23 23:48:49 -0500 | [diff] [blame] | 2964 | dout(" already present\n"); |
| 2965 | |
Alex Elder | cd89212 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 2966 | rbd_assert(snap->size == snap_size); |
Alex Elder | aafb230 | 2012-09-06 16:00:54 -0500 | [diff] [blame] | 2967 | rbd_assert(!strcmp(snap->name, snap_name)); |
Alex Elder | cd89212 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 2968 | rbd_assert(snap->features == snap_features); |
Alex Elder | 3593815 | 2012-08-02 11:29:46 -0500 | [diff] [blame] | 2969 | |
| 2970 | /* Done with this list entry; advance */ |
| 2971 | |
| 2972 | links = links->next; |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 2973 | } |
Alex Elder | 3593815 | 2012-08-02 11:29:46 -0500 | [diff] [blame] | 2974 | |
| 2975 | /* Advance to the next entry in the snapshot context */ |
| 2976 | |
| 2977 | index++; |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 2978 | } |
Alex Elder | 9fcbb80 | 2012-08-23 23:48:49 -0500 | [diff] [blame] | 2979 | dout("%s: done\n", __func__); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 2980 | |
| 2981 | return 0; |
| 2982 | } |
| 2983 | |
Alex Elder | 304f680 | 2012-08-31 17:29:52 -0500 | [diff] [blame] | 2984 | /* |
| 2985 | * Scan the list of snapshots and register the devices for any that |
| 2986 | * have not already been registered. |
| 2987 | */ |
| 2988 | static int rbd_dev_snaps_register(struct rbd_device *rbd_dev) |
| 2989 | { |
| 2990 | struct rbd_snap *snap; |
| 2991 | int ret = 0; |
| 2992 | |
| 2993 | dout("%s called\n", __func__); |
Alex Elder | 86ff77b | 2012-08-31 17:29:53 -0500 | [diff] [blame] | 2994 | if (WARN_ON(!device_is_registered(&rbd_dev->dev))) |
| 2995 | return -EIO; |
Alex Elder | 304f680 | 2012-08-31 17:29:52 -0500 | [diff] [blame] | 2996 | |
| 2997 | list_for_each_entry(snap, &rbd_dev->snaps, node) { |
| 2998 | if (!rbd_snap_registered(snap)) { |
| 2999 | ret = rbd_register_snap_dev(snap, &rbd_dev->dev); |
| 3000 | if (ret < 0) |
| 3001 | break; |
| 3002 | } |
| 3003 | } |
| 3004 | dout("%s: returning %d\n", __func__, ret); |
| 3005 | |
| 3006 | return ret; |
| 3007 | } |
| 3008 | |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 3009 | static int rbd_bus_add_dev(struct rbd_device *rbd_dev) |
| 3010 | { |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 3011 | struct device *dev; |
Alex Elder | cd789ab | 2012-08-30 00:16:38 -0500 | [diff] [blame] | 3012 | int ret; |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 3013 | |
| 3014 | mutex_lock_nested(&ctl_mutex, SINGLE_DEPTH_NESTING); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 3015 | |
Alex Elder | cd789ab | 2012-08-30 00:16:38 -0500 | [diff] [blame] | 3016 | dev = &rbd_dev->dev; |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 3017 | dev->bus = &rbd_bus_type; |
| 3018 | dev->type = &rbd_device_type; |
| 3019 | dev->parent = &rbd_root_dev; |
| 3020 | dev->release = rbd_dev_release; |
Alex Elder | de71a29 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 3021 | dev_set_name(dev, "%d", rbd_dev->dev_id); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 3022 | ret = device_register(dev); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 3023 | |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 3024 | mutex_unlock(&ctl_mutex); |
Alex Elder | cd789ab | 2012-08-30 00:16:38 -0500 | [diff] [blame] | 3025 | |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 3026 | return ret; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 3027 | } |
| 3028 | |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 3029 | static void rbd_bus_del_dev(struct rbd_device *rbd_dev) |
| 3030 | { |
| 3031 | device_unregister(&rbd_dev->dev); |
| 3032 | } |
| 3033 | |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 3034 | static int rbd_init_watch_dev(struct rbd_device *rbd_dev) |
| 3035 | { |
| 3036 | int ret, rc; |
| 3037 | |
| 3038 | do { |
Alex Elder | 0e6f322 | 2012-07-25 09:32:40 -0500 | [diff] [blame] | 3039 | ret = rbd_req_sync_watch(rbd_dev); |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 3040 | if (ret == -ERANGE) { |
Alex Elder | 117973f | 2012-08-31 17:29:55 -0500 | [diff] [blame] | 3041 | rc = rbd_dev_refresh(rbd_dev, NULL); |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 3042 | if (rc < 0) |
| 3043 | return rc; |
| 3044 | } |
| 3045 | } while (ret == -ERANGE); |
| 3046 | |
| 3047 | return ret; |
| 3048 | } |
| 3049 | |
Alex Elder | e283930 | 2012-08-29 17:11:06 -0500 | [diff] [blame] | 3050 | static atomic64_t rbd_dev_id_max = ATOMIC64_INIT(0); |
Alex Elder | 1ddbe94 | 2012-01-29 13:57:44 -0600 | [diff] [blame] | 3051 | |
| 3052 | /* |
Alex Elder | 499afd5 | 2012-02-02 08:13:29 -0600 | [diff] [blame] | 3053 | * Get a unique rbd identifier for the given new rbd_dev, and add |
| 3054 | * 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] | 3055 | */ |
Alex Elder | e283930 | 2012-08-29 17:11:06 -0500 | [diff] [blame] | 3056 | static void rbd_dev_id_get(struct rbd_device *rbd_dev) |
Alex Elder | b7f23c3 | 2012-01-29 13:57:43 -0600 | [diff] [blame] | 3057 | { |
Alex Elder | e283930 | 2012-08-29 17:11:06 -0500 | [diff] [blame] | 3058 | rbd_dev->dev_id = atomic64_inc_return(&rbd_dev_id_max); |
Alex Elder | 499afd5 | 2012-02-02 08:13:29 -0600 | [diff] [blame] | 3059 | |
| 3060 | spin_lock(&rbd_dev_list_lock); |
| 3061 | list_add_tail(&rbd_dev->node, &rbd_dev_list); |
| 3062 | spin_unlock(&rbd_dev_list_lock); |
Alex Elder | e283930 | 2012-08-29 17:11:06 -0500 | [diff] [blame] | 3063 | dout("rbd_dev %p given dev id %llu\n", rbd_dev, |
| 3064 | (unsigned long long) rbd_dev->dev_id); |
Alex Elder | 1ddbe94 | 2012-01-29 13:57:44 -0600 | [diff] [blame] | 3065 | } |
Alex Elder | b7f23c3 | 2012-01-29 13:57:43 -0600 | [diff] [blame] | 3066 | |
Alex Elder | 1ddbe94 | 2012-01-29 13:57:44 -0600 | [diff] [blame] | 3067 | /* |
Alex Elder | 499afd5 | 2012-02-02 08:13:29 -0600 | [diff] [blame] | 3068 | * Remove an rbd_dev from the global list, and record that its |
| 3069 | * identifier is no longer in use. |
Alex Elder | 1ddbe94 | 2012-01-29 13:57:44 -0600 | [diff] [blame] | 3070 | */ |
Alex Elder | e283930 | 2012-08-29 17:11:06 -0500 | [diff] [blame] | 3071 | static void rbd_dev_id_put(struct rbd_device *rbd_dev) |
Alex Elder | 1ddbe94 | 2012-01-29 13:57:44 -0600 | [diff] [blame] | 3072 | { |
Alex Elder | d184f6b | 2012-01-29 13:57:44 -0600 | [diff] [blame] | 3073 | struct list_head *tmp; |
Alex Elder | de71a29 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 3074 | int rbd_id = rbd_dev->dev_id; |
Alex Elder | d184f6b | 2012-01-29 13:57:44 -0600 | [diff] [blame] | 3075 | int max_id; |
| 3076 | |
Alex Elder | aafb230 | 2012-09-06 16:00:54 -0500 | [diff] [blame] | 3077 | rbd_assert(rbd_id > 0); |
Alex Elder | 499afd5 | 2012-02-02 08:13:29 -0600 | [diff] [blame] | 3078 | |
Alex Elder | e283930 | 2012-08-29 17:11:06 -0500 | [diff] [blame] | 3079 | dout("rbd_dev %p released dev id %llu\n", rbd_dev, |
| 3080 | (unsigned long long) rbd_dev->dev_id); |
Alex Elder | 499afd5 | 2012-02-02 08:13:29 -0600 | [diff] [blame] | 3081 | spin_lock(&rbd_dev_list_lock); |
| 3082 | list_del_init(&rbd_dev->node); |
Alex Elder | d184f6b | 2012-01-29 13:57:44 -0600 | [diff] [blame] | 3083 | |
| 3084 | /* |
| 3085 | * If the id being "put" is not the current maximum, there |
| 3086 | * is nothing special we need to do. |
| 3087 | */ |
Alex Elder | e283930 | 2012-08-29 17:11:06 -0500 | [diff] [blame] | 3088 | if (rbd_id != atomic64_read(&rbd_dev_id_max)) { |
Alex Elder | d184f6b | 2012-01-29 13:57:44 -0600 | [diff] [blame] | 3089 | spin_unlock(&rbd_dev_list_lock); |
| 3090 | return; |
| 3091 | } |
| 3092 | |
| 3093 | /* |
| 3094 | * We need to update the current maximum id. Search the |
| 3095 | * list to find out what it is. We're more likely to find |
| 3096 | * the maximum at the end, so search the list backward. |
| 3097 | */ |
| 3098 | max_id = 0; |
| 3099 | list_for_each_prev(tmp, &rbd_dev_list) { |
| 3100 | struct rbd_device *rbd_dev; |
| 3101 | |
| 3102 | rbd_dev = list_entry(tmp, struct rbd_device, node); |
Alex Elder | b213e0b | 2012-10-10 21:19:13 -0700 | [diff] [blame] | 3103 | if (rbd_dev->dev_id > max_id) |
| 3104 | max_id = rbd_dev->dev_id; |
Alex Elder | d184f6b | 2012-01-29 13:57:44 -0600 | [diff] [blame] | 3105 | } |
Alex Elder | 499afd5 | 2012-02-02 08:13:29 -0600 | [diff] [blame] | 3106 | spin_unlock(&rbd_dev_list_lock); |
Alex Elder | b7f23c3 | 2012-01-29 13:57:43 -0600 | [diff] [blame] | 3107 | |
Alex Elder | 1ddbe94 | 2012-01-29 13:57:44 -0600 | [diff] [blame] | 3108 | /* |
Alex Elder | e283930 | 2012-08-29 17:11:06 -0500 | [diff] [blame] | 3109 | * 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] | 3110 | * which case it now accurately reflects the new maximum. |
| 3111 | * Be careful not to overwrite the maximum value in that |
| 3112 | * case. |
Alex Elder | 1ddbe94 | 2012-01-29 13:57:44 -0600 | [diff] [blame] | 3113 | */ |
Alex Elder | e283930 | 2012-08-29 17:11:06 -0500 | [diff] [blame] | 3114 | atomic64_cmpxchg(&rbd_dev_id_max, rbd_id, max_id); |
| 3115 | dout(" max dev id has been reset\n"); |
Alex Elder | b7f23c3 | 2012-01-29 13:57:43 -0600 | [diff] [blame] | 3116 | } |
| 3117 | |
Alex Elder | a725f65e | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 3118 | /* |
Alex Elder | e28fff26 | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 3119 | * Skips over white space at *buf, and updates *buf to point to the |
| 3120 | * first found non-space character (if any). Returns the length of |
Alex Elder | 593a9e7 | 2012-02-07 12:03:37 -0600 | [diff] [blame] | 3121 | * the token (string of non-white space characters) found. Note |
| 3122 | * that *buf must be terminated with '\0'. |
Alex Elder | e28fff26 | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 3123 | */ |
| 3124 | static inline size_t next_token(const char **buf) |
| 3125 | { |
| 3126 | /* |
| 3127 | * These are the characters that produce nonzero for |
| 3128 | * isspace() in the "C" and "POSIX" locales. |
| 3129 | */ |
| 3130 | const char *spaces = " \f\n\r\t\v"; |
| 3131 | |
| 3132 | *buf += strspn(*buf, spaces); /* Find start of token */ |
| 3133 | |
| 3134 | return strcspn(*buf, spaces); /* Return token length */ |
| 3135 | } |
| 3136 | |
| 3137 | /* |
| 3138 | * Finds the next token in *buf, and if the provided token buffer is |
| 3139 | * big enough, copies the found token into it. The result, if |
Alex Elder | 593a9e7 | 2012-02-07 12:03:37 -0600 | [diff] [blame] | 3140 | * copied, is guaranteed to be terminated with '\0'. Note that *buf |
| 3141 | * must be terminated with '\0' on entry. |
Alex Elder | e28fff26 | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 3142 | * |
| 3143 | * Returns the length of the token found (not including the '\0'). |
| 3144 | * Return value will be 0 if no token is found, and it will be >= |
| 3145 | * token_size if the token would not fit. |
| 3146 | * |
Alex Elder | 593a9e7 | 2012-02-07 12:03:37 -0600 | [diff] [blame] | 3147 | * 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] | 3148 | * found token. Note that this occurs even if the token buffer is |
| 3149 | * too small to hold it. |
| 3150 | */ |
| 3151 | static inline size_t copy_token(const char **buf, |
| 3152 | char *token, |
| 3153 | size_t token_size) |
| 3154 | { |
| 3155 | size_t len; |
| 3156 | |
| 3157 | len = next_token(buf); |
| 3158 | if (len < token_size) { |
| 3159 | memcpy(token, *buf, len); |
| 3160 | *(token + len) = '\0'; |
| 3161 | } |
| 3162 | *buf += len; |
| 3163 | |
| 3164 | return len; |
| 3165 | } |
| 3166 | |
| 3167 | /* |
Alex Elder | ea3352f | 2012-07-09 21:04:23 -0500 | [diff] [blame] | 3168 | * Finds the next token in *buf, dynamically allocates a buffer big |
| 3169 | * enough to hold a copy of it, and copies the token into the new |
| 3170 | * buffer. The copy is guaranteed to be terminated with '\0'. Note |
| 3171 | * that a duplicate buffer is created even for a zero-length token. |
| 3172 | * |
| 3173 | * Returns a pointer to the newly-allocated duplicate, or a null |
| 3174 | * pointer if memory for the duplicate was not available. If |
| 3175 | * the lenp argument is a non-null pointer, the length of the token |
| 3176 | * (not including the '\0') is returned in *lenp. |
| 3177 | * |
| 3178 | * If successful, the *buf pointer will be updated to point beyond |
| 3179 | * the end of the found token. |
| 3180 | * |
| 3181 | * Note: uses GFP_KERNEL for allocation. |
| 3182 | */ |
| 3183 | static inline char *dup_token(const char **buf, size_t *lenp) |
| 3184 | { |
| 3185 | char *dup; |
| 3186 | size_t len; |
| 3187 | |
| 3188 | len = next_token(buf); |
Alex Elder | 4caf35f | 2012-11-01 08:39:27 -0500 | [diff] [blame] | 3189 | dup = kmemdup(*buf, len + 1, GFP_KERNEL); |
Alex Elder | ea3352f | 2012-07-09 21:04:23 -0500 | [diff] [blame] | 3190 | if (!dup) |
| 3191 | return NULL; |
Alex Elder | ea3352f | 2012-07-09 21:04:23 -0500 | [diff] [blame] | 3192 | *(dup + len) = '\0'; |
| 3193 | *buf += len; |
| 3194 | |
| 3195 | if (lenp) |
| 3196 | *lenp = len; |
| 3197 | |
| 3198 | return dup; |
| 3199 | } |
| 3200 | |
| 3201 | /* |
Alex Elder | 859c31d | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 3202 | * Parse the options provided for an "rbd add" (i.e., rbd image |
| 3203 | * mapping) request. These arrive via a write to /sys/bus/rbd/add, |
| 3204 | * and the data written is passed here via a NUL-terminated buffer. |
| 3205 | * Returns 0 if successful or an error code otherwise. |
Alex Elder | d22f76e | 2012-07-12 10:46:35 -0500 | [diff] [blame] | 3206 | * |
Alex Elder | 859c31d | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 3207 | * The information extracted from these options is recorded in |
| 3208 | * the other parameters which return dynamically-allocated |
| 3209 | * structures: |
| 3210 | * ceph_opts |
| 3211 | * The address of a pointer that will refer to a ceph options |
| 3212 | * structure. Caller must release the returned pointer using |
| 3213 | * ceph_destroy_options() when it is no longer needed. |
| 3214 | * rbd_opts |
| 3215 | * Address of an rbd options pointer. Fully initialized by |
| 3216 | * this function; caller must release with kfree(). |
| 3217 | * spec |
| 3218 | * Address of an rbd image specification pointer. Fully |
| 3219 | * initialized by this function based on parsed options. |
| 3220 | * Caller must release with rbd_spec_put(). |
| 3221 | * |
| 3222 | * The options passed take this form: |
| 3223 | * <mon_addrs> <options> <pool_name> <image_name> [<snap_id>] |
| 3224 | * where: |
| 3225 | * <mon_addrs> |
| 3226 | * A comma-separated list of one or more monitor addresses. |
| 3227 | * A monitor address is an ip address, optionally followed |
| 3228 | * by a port number (separated by a colon). |
| 3229 | * I.e.: ip1[:port1][,ip2[:port2]...] |
| 3230 | * <options> |
| 3231 | * A comma-separated list of ceph and/or rbd options. |
| 3232 | * <pool_name> |
| 3233 | * The name of the rados pool containing the rbd image. |
| 3234 | * <image_name> |
| 3235 | * The name of the image in that pool to map. |
| 3236 | * <snap_id> |
| 3237 | * An optional snapshot id. If provided, the mapping will |
| 3238 | * present data from the image at the time that snapshot was |
| 3239 | * created. The image head is used if no snapshot id is |
| 3240 | * provided. Snapshot mappings are always read-only. |
Alex Elder | a725f65e | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 3241 | */ |
Alex Elder | 859c31d | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 3242 | static int rbd_add_parse_args(const char *buf, |
Alex Elder | dc79b11 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 3243 | struct ceph_options **ceph_opts, |
Alex Elder | 859c31d | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 3244 | struct rbd_options **opts, |
| 3245 | struct rbd_spec **rbd_spec) |
Alex Elder | a725f65e | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 3246 | { |
Alex Elder | d22f76e | 2012-07-12 10:46:35 -0500 | [diff] [blame] | 3247 | size_t len; |
Alex Elder | 859c31d | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 3248 | char *options; |
Alex Elder | 0ddebc0 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 3249 | const char *mon_addrs; |
| 3250 | size_t mon_addrs_size; |
Alex Elder | 859c31d | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 3251 | struct rbd_spec *spec = NULL; |
Alex Elder | 4e9afeb | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 3252 | struct rbd_options *rbd_opts = NULL; |
Alex Elder | 859c31d | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 3253 | struct ceph_options *copts; |
Alex Elder | dc79b11 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 3254 | int ret; |
Alex Elder | e28fff26 | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 3255 | |
| 3256 | /* The first four tokens are required */ |
| 3257 | |
Alex Elder | 7ef3214 | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 3258 | len = next_token(&buf); |
Alex Elder | 4fb5d671 | 2012-11-01 10:17:15 -0500 | [diff] [blame] | 3259 | if (!len) { |
| 3260 | rbd_warn(NULL, "no monitor address(es) provided"); |
| 3261 | return -EINVAL; |
| 3262 | } |
Alex Elder | 0ddebc0 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 3263 | mon_addrs = buf; |
Alex Elder | f28e565 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 3264 | mon_addrs_size = len + 1; |
Alex Elder | 7ef3214 | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 3265 | buf += len; |
Alex Elder | a725f65e | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 3266 | |
Alex Elder | dc79b11 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 3267 | ret = -EINVAL; |
Alex Elder | f28e565 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 3268 | options = dup_token(&buf, NULL); |
| 3269 | if (!options) |
Alex Elder | dc79b11 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 3270 | return -ENOMEM; |
Alex Elder | 4fb5d671 | 2012-11-01 10:17:15 -0500 | [diff] [blame] | 3271 | if (!*options) { |
| 3272 | rbd_warn(NULL, "no options provided"); |
| 3273 | goto out_err; |
| 3274 | } |
Alex Elder | a725f65e | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 3275 | |
Alex Elder | 859c31d | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 3276 | spec = rbd_spec_alloc(); |
| 3277 | if (!spec) |
Alex Elder | f28e565 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 3278 | goto out_mem; |
Alex Elder | 859c31d | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 3279 | |
| 3280 | spec->pool_name = dup_token(&buf, NULL); |
| 3281 | if (!spec->pool_name) |
| 3282 | goto out_mem; |
Alex Elder | 4fb5d671 | 2012-11-01 10:17:15 -0500 | [diff] [blame] | 3283 | if (!*spec->pool_name) { |
| 3284 | rbd_warn(NULL, "no pool name provided"); |
| 3285 | goto out_err; |
| 3286 | } |
Alex Elder | e28fff26 | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 3287 | |
Alex Elder | 69e7a02 | 2012-11-01 08:39:26 -0500 | [diff] [blame] | 3288 | spec->image_name = dup_token(&buf, NULL); |
Alex Elder | 859c31d | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 3289 | if (!spec->image_name) |
Alex Elder | f28e565 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 3290 | goto out_mem; |
Alex Elder | 4fb5d671 | 2012-11-01 10:17:15 -0500 | [diff] [blame] | 3291 | if (!*spec->image_name) { |
| 3292 | rbd_warn(NULL, "no image name provided"); |
| 3293 | goto out_err; |
| 3294 | } |
Alex Elder | e28fff26 | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 3295 | |
Alex Elder | f28e565 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 3296 | /* |
| 3297 | * Snapshot name is optional; default is to use "-" |
| 3298 | * (indicating the head/no snapshot). |
| 3299 | */ |
Alex Elder | 3feeb894 | 2012-08-31 17:29:52 -0500 | [diff] [blame] | 3300 | len = next_token(&buf); |
Alex Elder | 820a5f3 | 2012-07-09 21:04:24 -0500 | [diff] [blame] | 3301 | if (!len) { |
Alex Elder | 3feeb894 | 2012-08-31 17:29:52 -0500 | [diff] [blame] | 3302 | buf = RBD_SNAP_HEAD_NAME; /* No snapshot supplied */ |
| 3303 | len = sizeof (RBD_SNAP_HEAD_NAME) - 1; |
Alex Elder | f28e565 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 3304 | } else if (len > RBD_MAX_SNAP_NAME_LEN) { |
Alex Elder | dc79b11 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 3305 | ret = -ENAMETOOLONG; |
Alex Elder | f28e565 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 3306 | goto out_err; |
Alex Elder | 849b426 | 2012-07-09 21:04:24 -0500 | [diff] [blame] | 3307 | } |
Alex Elder | 4caf35f | 2012-11-01 08:39:27 -0500 | [diff] [blame] | 3308 | spec->snap_name = kmemdup(buf, len + 1, GFP_KERNEL); |
Alex Elder | 859c31d | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 3309 | if (!spec->snap_name) |
Alex Elder | f28e565 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 3310 | goto out_mem; |
Alex Elder | 859c31d | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 3311 | *(spec->snap_name + len) = '\0'; |
Alex Elder | e5c3553 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 3312 | |
Alex Elder | 0ddebc0 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 3313 | /* Initialize all rbd options to the defaults */ |
Alex Elder | e28fff26 | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 3314 | |
Alex Elder | 4e9afeb | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 3315 | rbd_opts = kzalloc(sizeof (*rbd_opts), GFP_KERNEL); |
| 3316 | if (!rbd_opts) |
| 3317 | goto out_mem; |
| 3318 | |
| 3319 | rbd_opts->read_only = RBD_READ_ONLY_DEFAULT; |
Alex Elder | d22f76e | 2012-07-12 10:46:35 -0500 | [diff] [blame] | 3320 | |
Alex Elder | 859c31d | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 3321 | copts = ceph_parse_options(options, mon_addrs, |
Alex Elder | 0ddebc0 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 3322 | mon_addrs + mon_addrs_size - 1, |
Alex Elder | 4e9afeb | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 3323 | parse_rbd_opts_token, rbd_opts); |
Alex Elder | 859c31d | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 3324 | if (IS_ERR(copts)) { |
| 3325 | ret = PTR_ERR(copts); |
Alex Elder | dc79b11 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 3326 | goto out_err; |
| 3327 | } |
Alex Elder | 859c31d | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 3328 | kfree(options); |
| 3329 | |
| 3330 | *ceph_opts = copts; |
Alex Elder | 4e9afeb | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 3331 | *opts = rbd_opts; |
Alex Elder | 859c31d | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 3332 | *rbd_spec = spec; |
Alex Elder | 0ddebc0 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 3333 | |
Alex Elder | dc79b11 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 3334 | return 0; |
Alex Elder | f28e565 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 3335 | out_mem: |
Alex Elder | dc79b11 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 3336 | ret = -ENOMEM; |
Alex Elder | d22f76e | 2012-07-12 10:46:35 -0500 | [diff] [blame] | 3337 | out_err: |
Alex Elder | 859c31d | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 3338 | kfree(rbd_opts); |
| 3339 | rbd_spec_put(spec); |
Alex Elder | f28e565 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 3340 | kfree(options); |
Alex Elder | d22f76e | 2012-07-12 10:46:35 -0500 | [diff] [blame] | 3341 | |
Alex Elder | dc79b11 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 3342 | return ret; |
Alex Elder | a725f65e | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 3343 | } |
| 3344 | |
Alex Elder | 589d30e | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 3345 | /* |
| 3346 | * An rbd format 2 image has a unique identifier, distinct from the |
| 3347 | * name given to it by the user. Internally, that identifier is |
| 3348 | * what's used to specify the names of objects related to the image. |
| 3349 | * |
| 3350 | * A special "rbd id" object is used to map an rbd image name to its |
| 3351 | * id. If that object doesn't exist, then there is no v2 rbd image |
| 3352 | * with the supplied name. |
| 3353 | * |
| 3354 | * This function will record the given rbd_dev's image_id field if |
| 3355 | * it can be determined, and in that case will return 0. If any |
| 3356 | * errors occur a negative errno will be returned and the rbd_dev's |
| 3357 | * image_id field will be unchanged (and should be NULL). |
| 3358 | */ |
| 3359 | static int rbd_dev_image_id(struct rbd_device *rbd_dev) |
| 3360 | { |
| 3361 | int ret; |
| 3362 | size_t size; |
| 3363 | char *object_name; |
| 3364 | void *response; |
| 3365 | void *p; |
| 3366 | |
| 3367 | /* |
Alex Elder | 2c0d0a1 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 3368 | * When probing a parent image, the image id is already |
| 3369 | * known (and the image name likely is not). There's no |
| 3370 | * need to fetch the image id again in this case. |
| 3371 | */ |
| 3372 | if (rbd_dev->spec->image_id) |
| 3373 | return 0; |
| 3374 | |
| 3375 | /* |
Alex Elder | 589d30e | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 3376 | * First, see if the format 2 image id file exists, and if |
| 3377 | * so, get the image's persistent id from it. |
| 3378 | */ |
Alex Elder | 69e7a02 | 2012-11-01 08:39:26 -0500 | [diff] [blame] | 3379 | size = sizeof (RBD_ID_PREFIX) + strlen(rbd_dev->spec->image_name); |
Alex Elder | 589d30e | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 3380 | object_name = kmalloc(size, GFP_NOIO); |
| 3381 | if (!object_name) |
| 3382 | return -ENOMEM; |
Alex Elder | 0d7dbfc | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 3383 | 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] | 3384 | dout("rbd id object name is %s\n", object_name); |
| 3385 | |
| 3386 | /* Response will be an encoded string, which includes a length */ |
| 3387 | |
| 3388 | size = sizeof (__le32) + RBD_IMAGE_ID_LEN_MAX; |
| 3389 | response = kzalloc(size, GFP_NOIO); |
| 3390 | if (!response) { |
| 3391 | ret = -ENOMEM; |
| 3392 | goto out; |
| 3393 | } |
| 3394 | |
| 3395 | ret = rbd_req_sync_exec(rbd_dev, object_name, |
| 3396 | "rbd", "get_id", |
| 3397 | NULL, 0, |
| 3398 | response, RBD_IMAGE_ID_LEN_MAX, |
| 3399 | CEPH_OSD_FLAG_READ, NULL); |
| 3400 | dout("%s: rbd_req_sync_exec returned %d\n", __func__, ret); |
| 3401 | if (ret < 0) |
| 3402 | goto out; |
Alex Elder | a0ea3a4 | 2012-10-10 21:19:13 -0700 | [diff] [blame] | 3403 | ret = 0; /* rbd_req_sync_exec() can return positive */ |
Alex Elder | 589d30e | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 3404 | |
| 3405 | p = response; |
Alex Elder | 0d7dbfc | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 3406 | rbd_dev->spec->image_id = ceph_extract_encoded_string(&p, |
Alex Elder | 589d30e | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 3407 | p + RBD_IMAGE_ID_LEN_MAX, |
Alex Elder | 979ed48 | 2012-11-01 08:39:26 -0500 | [diff] [blame] | 3408 | NULL, GFP_NOIO); |
Alex Elder | 0d7dbfc | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 3409 | if (IS_ERR(rbd_dev->spec->image_id)) { |
| 3410 | ret = PTR_ERR(rbd_dev->spec->image_id); |
| 3411 | rbd_dev->spec->image_id = NULL; |
Alex Elder | 589d30e | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 3412 | } else { |
Alex Elder | 0d7dbfc | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 3413 | dout("image_id is %s\n", rbd_dev->spec->image_id); |
Alex Elder | 589d30e | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 3414 | } |
| 3415 | out: |
| 3416 | kfree(response); |
| 3417 | kfree(object_name); |
| 3418 | |
| 3419 | return ret; |
| 3420 | } |
| 3421 | |
Alex Elder | a30b71b | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 3422 | static int rbd_dev_v1_probe(struct rbd_device *rbd_dev) |
| 3423 | { |
| 3424 | int ret; |
| 3425 | size_t size; |
| 3426 | |
| 3427 | /* Version 1 images have no id; empty string is used */ |
| 3428 | |
Alex Elder | 0d7dbfc | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 3429 | rbd_dev->spec->image_id = kstrdup("", GFP_KERNEL); |
| 3430 | if (!rbd_dev->spec->image_id) |
Alex Elder | a30b71b | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 3431 | return -ENOMEM; |
Alex Elder | a30b71b | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 3432 | |
| 3433 | /* Record the header object name for this rbd image. */ |
| 3434 | |
Alex Elder | 69e7a02 | 2012-11-01 08:39:26 -0500 | [diff] [blame] | 3435 | size = strlen(rbd_dev->spec->image_name) + sizeof (RBD_SUFFIX); |
Alex Elder | a30b71b | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 3436 | rbd_dev->header_name = kmalloc(size, GFP_KERNEL); |
| 3437 | if (!rbd_dev->header_name) { |
| 3438 | ret = -ENOMEM; |
| 3439 | goto out_err; |
| 3440 | } |
Alex Elder | 0d7dbfc | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 3441 | sprintf(rbd_dev->header_name, "%s%s", |
| 3442 | rbd_dev->spec->image_name, RBD_SUFFIX); |
Alex Elder | a30b71b | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 3443 | |
| 3444 | /* Populate rbd image metadata */ |
| 3445 | |
| 3446 | ret = rbd_read_header(rbd_dev, &rbd_dev->header); |
| 3447 | if (ret < 0) |
| 3448 | goto out_err; |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 3449 | |
| 3450 | /* Version 1 images have no parent (no layering) */ |
| 3451 | |
| 3452 | rbd_dev->parent_spec = NULL; |
| 3453 | rbd_dev->parent_overlap = 0; |
| 3454 | |
Alex Elder | a30b71b | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 3455 | rbd_dev->image_format = 1; |
| 3456 | |
| 3457 | dout("discovered version 1 image, header name is %s\n", |
| 3458 | rbd_dev->header_name); |
| 3459 | |
| 3460 | return 0; |
| 3461 | |
| 3462 | out_err: |
| 3463 | kfree(rbd_dev->header_name); |
| 3464 | rbd_dev->header_name = NULL; |
Alex Elder | 0d7dbfc | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 3465 | kfree(rbd_dev->spec->image_id); |
| 3466 | rbd_dev->spec->image_id = NULL; |
Alex Elder | a30b71b | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 3467 | |
| 3468 | return ret; |
| 3469 | } |
| 3470 | |
| 3471 | static int rbd_dev_v2_probe(struct rbd_device *rbd_dev) |
| 3472 | { |
| 3473 | size_t size; |
Alex Elder | 9d475de | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 3474 | int ret; |
Alex Elder | 6e14b1a | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 3475 | u64 ver = 0; |
Alex Elder | a30b71b | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 3476 | |
| 3477 | /* |
| 3478 | * Image id was filled in by the caller. Record the header |
| 3479 | * object name for this rbd image. |
| 3480 | */ |
Alex Elder | 979ed48 | 2012-11-01 08:39:26 -0500 | [diff] [blame] | 3481 | size = sizeof (RBD_HEADER_PREFIX) + strlen(rbd_dev->spec->image_id); |
Alex Elder | a30b71b | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 3482 | rbd_dev->header_name = kmalloc(size, GFP_KERNEL); |
| 3483 | if (!rbd_dev->header_name) |
| 3484 | return -ENOMEM; |
| 3485 | sprintf(rbd_dev->header_name, "%s%s", |
Alex Elder | 0d7dbfc | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 3486 | RBD_HEADER_PREFIX, rbd_dev->spec->image_id); |
Alex Elder | 9d475de | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 3487 | |
| 3488 | /* Get the size and object order for the image */ |
| 3489 | |
| 3490 | ret = rbd_dev_v2_image_size(rbd_dev); |
| 3491 | if (ret < 0) |
| 3492 | goto out_err; |
Alex Elder | 1e13019 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 3493 | |
| 3494 | /* Get the object prefix (a.k.a. block_name) for the image */ |
| 3495 | |
| 3496 | ret = rbd_dev_v2_object_prefix(rbd_dev); |
| 3497 | if (ret < 0) |
| 3498 | goto out_err; |
Alex Elder | b1b5402 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 3499 | |
Alex Elder | d889140 | 2012-10-09 13:50:17 -0700 | [diff] [blame] | 3500 | /* Get the and check features for the image */ |
Alex Elder | b1b5402 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 3501 | |
| 3502 | ret = rbd_dev_v2_features(rbd_dev); |
| 3503 | if (ret < 0) |
| 3504 | goto out_err; |
Alex Elder | 35d489f | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 3505 | |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 3506 | /* If the image supports layering, get the parent info */ |
| 3507 | |
| 3508 | if (rbd_dev->header.features & RBD_FEATURE_LAYERING) { |
| 3509 | ret = rbd_dev_v2_parent_info(rbd_dev); |
| 3510 | if (ret < 0) |
| 3511 | goto out_err; |
| 3512 | } |
| 3513 | |
Alex Elder | 6e14b1a | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 3514 | /* crypto and compression type aren't (yet) supported for v2 images */ |
Alex Elder | 35d489f | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 3515 | |
Alex Elder | 6e14b1a | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 3516 | rbd_dev->header.crypt_type = 0; |
| 3517 | rbd_dev->header.comp_type = 0; |
| 3518 | |
| 3519 | /* Get the snapshot context, plus the header version */ |
| 3520 | |
| 3521 | ret = rbd_dev_v2_snap_context(rbd_dev, &ver); |
Alex Elder | 35d489f | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 3522 | if (ret) |
| 3523 | goto out_err; |
Alex Elder | 6e14b1a | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 3524 | rbd_dev->header.obj_version = ver; |
| 3525 | |
Alex Elder | a30b71b | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 3526 | rbd_dev->image_format = 2; |
| 3527 | |
| 3528 | dout("discovered version 2 image, header name is %s\n", |
| 3529 | rbd_dev->header_name); |
| 3530 | |
Alex Elder | 3515297 | 2012-08-31 17:29:55 -0500 | [diff] [blame] | 3531 | return 0; |
Alex Elder | 9d475de | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 3532 | out_err: |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 3533 | rbd_dev->parent_overlap = 0; |
| 3534 | rbd_spec_put(rbd_dev->parent_spec); |
| 3535 | rbd_dev->parent_spec = NULL; |
Alex Elder | 9d475de | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 3536 | kfree(rbd_dev->header_name); |
| 3537 | rbd_dev->header_name = NULL; |
Alex Elder | 1e13019 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 3538 | kfree(rbd_dev->header.object_prefix); |
| 3539 | rbd_dev->header.object_prefix = NULL; |
Alex Elder | 9d475de | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 3540 | |
| 3541 | return ret; |
Alex Elder | a30b71b | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 3542 | } |
| 3543 | |
Alex Elder | 83a0626 | 2012-10-30 15:47:17 -0500 | [diff] [blame] | 3544 | static int rbd_dev_probe_finish(struct rbd_device *rbd_dev) |
| 3545 | { |
| 3546 | int ret; |
| 3547 | |
| 3548 | /* no need to lock here, as rbd_dev is not registered yet */ |
| 3549 | ret = rbd_dev_snaps_update(rbd_dev); |
| 3550 | if (ret) |
| 3551 | return ret; |
| 3552 | |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 3553 | ret = rbd_dev_probe_update_spec(rbd_dev); |
| 3554 | if (ret) |
| 3555 | goto err_out_snaps; |
| 3556 | |
Alex Elder | 83a0626 | 2012-10-30 15:47:17 -0500 | [diff] [blame] | 3557 | ret = rbd_dev_set_mapping(rbd_dev); |
| 3558 | if (ret) |
| 3559 | goto err_out_snaps; |
| 3560 | |
| 3561 | /* generate unique id: find highest unique id, add one */ |
| 3562 | rbd_dev_id_get(rbd_dev); |
| 3563 | |
| 3564 | /* Fill in the device name, now that we have its id. */ |
| 3565 | BUILD_BUG_ON(DEV_NAME_LEN |
| 3566 | < sizeof (RBD_DRV_NAME) + MAX_INT_FORMAT_WIDTH); |
| 3567 | sprintf(rbd_dev->name, "%s%d", RBD_DRV_NAME, rbd_dev->dev_id); |
| 3568 | |
| 3569 | /* Get our block major device number. */ |
| 3570 | |
| 3571 | ret = register_blkdev(0, rbd_dev->name); |
| 3572 | if (ret < 0) |
| 3573 | goto err_out_id; |
| 3574 | rbd_dev->major = ret; |
| 3575 | |
| 3576 | /* Set up the blkdev mapping. */ |
| 3577 | |
| 3578 | ret = rbd_init_disk(rbd_dev); |
| 3579 | if (ret) |
| 3580 | goto err_out_blkdev; |
| 3581 | |
| 3582 | ret = rbd_bus_add_dev(rbd_dev); |
| 3583 | if (ret) |
| 3584 | goto err_out_disk; |
| 3585 | |
| 3586 | /* |
| 3587 | * At this point cleanup in the event of an error is the job |
| 3588 | * of the sysfs code (initiated by rbd_bus_del_dev()). |
| 3589 | */ |
| 3590 | down_write(&rbd_dev->header_rwsem); |
| 3591 | ret = rbd_dev_snaps_register(rbd_dev); |
| 3592 | up_write(&rbd_dev->header_rwsem); |
| 3593 | if (ret) |
| 3594 | goto err_out_bus; |
| 3595 | |
| 3596 | ret = rbd_init_watch_dev(rbd_dev); |
| 3597 | if (ret) |
| 3598 | goto err_out_bus; |
| 3599 | |
| 3600 | /* Everything's ready. Announce the disk to the world. */ |
| 3601 | |
| 3602 | add_disk(rbd_dev->disk); |
| 3603 | |
| 3604 | pr_info("%s: added with size 0x%llx\n", rbd_dev->disk->disk_name, |
| 3605 | (unsigned long long) rbd_dev->mapping.size); |
| 3606 | |
| 3607 | return ret; |
| 3608 | err_out_bus: |
| 3609 | /* this will also clean up rest of rbd_dev stuff */ |
| 3610 | |
| 3611 | rbd_bus_del_dev(rbd_dev); |
| 3612 | |
| 3613 | return ret; |
| 3614 | err_out_disk: |
| 3615 | rbd_free_disk(rbd_dev); |
| 3616 | err_out_blkdev: |
| 3617 | unregister_blkdev(rbd_dev->major, rbd_dev->name); |
| 3618 | err_out_id: |
| 3619 | rbd_dev_id_put(rbd_dev); |
| 3620 | err_out_snaps: |
| 3621 | rbd_remove_all_snaps(rbd_dev); |
| 3622 | |
| 3623 | return ret; |
| 3624 | } |
| 3625 | |
Alex Elder | a30b71b | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 3626 | /* |
| 3627 | * Probe for the existence of the header object for the given rbd |
| 3628 | * device. For format 2 images this includes determining the image |
| 3629 | * id. |
| 3630 | */ |
| 3631 | static int rbd_dev_probe(struct rbd_device *rbd_dev) |
| 3632 | { |
| 3633 | int ret; |
| 3634 | |
| 3635 | /* |
| 3636 | * Get the id from the image id object. If it's not a |
| 3637 | * format 2 image, we'll get ENOENT back, and we'll assume |
| 3638 | * it's a format 1 image. |
| 3639 | */ |
| 3640 | ret = rbd_dev_image_id(rbd_dev); |
| 3641 | if (ret) |
| 3642 | ret = rbd_dev_v1_probe(rbd_dev); |
| 3643 | else |
| 3644 | ret = rbd_dev_v2_probe(rbd_dev); |
Alex Elder | 83a0626 | 2012-10-30 15:47:17 -0500 | [diff] [blame] | 3645 | if (ret) { |
Alex Elder | a30b71b | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 3646 | dout("probe failed, returning %d\n", ret); |
| 3647 | |
Alex Elder | 83a0626 | 2012-10-30 15:47:17 -0500 | [diff] [blame] | 3648 | return ret; |
| 3649 | } |
| 3650 | |
| 3651 | ret = rbd_dev_probe_finish(rbd_dev); |
| 3652 | if (ret) |
| 3653 | rbd_header_free(&rbd_dev->header); |
| 3654 | |
Alex Elder | a30b71b | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 3655 | return ret; |
| 3656 | } |
| 3657 | |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 3658 | static ssize_t rbd_add(struct bus_type *bus, |
| 3659 | const char *buf, |
| 3660 | size_t count) |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 3661 | { |
Alex Elder | cb8627c | 2012-07-09 21:04:23 -0500 | [diff] [blame] | 3662 | struct rbd_device *rbd_dev = NULL; |
Alex Elder | dc79b11 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 3663 | struct ceph_options *ceph_opts = NULL; |
Alex Elder | 4e9afeb | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 3664 | struct rbd_options *rbd_opts = NULL; |
Alex Elder | 859c31d | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 3665 | struct rbd_spec *spec = NULL; |
Alex Elder | 9d3997f | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 3666 | struct rbd_client *rbdc; |
Alex Elder | 27cc259 | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 3667 | struct ceph_osd_client *osdc; |
| 3668 | int rc = -ENOMEM; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 3669 | |
| 3670 | if (!try_module_get(THIS_MODULE)) |
| 3671 | return -ENODEV; |
| 3672 | |
Alex Elder | a725f65e | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 3673 | /* parse add command */ |
Alex Elder | 859c31d | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 3674 | rc = rbd_add_parse_args(buf, &ceph_opts, &rbd_opts, &spec); |
Alex Elder | dc79b11 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 3675 | if (rc < 0) |
Alex Elder | bd4ba65 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 3676 | goto err_out_module; |
Alex Elder | a725f65e | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 3677 | |
Alex Elder | 9d3997f | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 3678 | rbdc = rbd_get_client(ceph_opts); |
| 3679 | if (IS_ERR(rbdc)) { |
| 3680 | rc = PTR_ERR(rbdc); |
Alex Elder | 0ddebc0 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 3681 | goto err_out_args; |
Alex Elder | 9d3997f | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 3682 | } |
Alex Elder | c53d589 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 3683 | ceph_opts = NULL; /* rbd_dev client now owns this */ |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 3684 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 3685 | /* pick the pool */ |
Alex Elder | 9d3997f | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 3686 | osdc = &rbdc->client->osdc; |
Alex Elder | 859c31d | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 3687 | rc = ceph_pg_poolid_by_name(osdc->osdmap, spec->pool_name); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 3688 | if (rc < 0) |
| 3689 | goto err_out_client; |
Alex Elder | 859c31d | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 3690 | spec->pool_id = (u64) rc; |
| 3691 | |
Alex Elder | c53d589 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 3692 | rbd_dev = rbd_dev_create(rbdc, spec); |
Alex Elder | bd4ba65 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 3693 | if (!rbd_dev) |
| 3694 | goto err_out_client; |
Alex Elder | c53d589 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 3695 | rbdc = NULL; /* rbd_dev now owns this */ |
| 3696 | spec = NULL; /* rbd_dev now owns this */ |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 3697 | |
Alex Elder | bd4ba65 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 3698 | rbd_dev->mapping.read_only = rbd_opts->read_only; |
Alex Elder | c53d589 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 3699 | kfree(rbd_opts); |
| 3700 | rbd_opts = NULL; /* done with this */ |
Alex Elder | bd4ba65 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 3701 | |
Alex Elder | a30b71b | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 3702 | rc = rbd_dev_probe(rbd_dev); |
| 3703 | if (rc < 0) |
Alex Elder | c53d589 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 3704 | goto err_out_rbd_dev; |
Alex Elder | 05fd6f6 | 2012-08-29 17:11:07 -0500 | [diff] [blame] | 3705 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 3706 | return count; |
Alex Elder | c53d589 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 3707 | err_out_rbd_dev: |
| 3708 | rbd_dev_destroy(rbd_dev); |
Alex Elder | bd4ba65 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 3709 | err_out_client: |
Alex Elder | 9d3997f | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 3710 | rbd_put_client(rbdc); |
Alex Elder | 0ddebc0 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 3711 | err_out_args: |
Alex Elder | 78cea76 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 3712 | if (ceph_opts) |
| 3713 | ceph_destroy_options(ceph_opts); |
Alex Elder | 4e9afeb | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 3714 | kfree(rbd_opts); |
Alex Elder | 859c31d | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 3715 | rbd_spec_put(spec); |
Alex Elder | bd4ba65 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 3716 | err_out_module: |
| 3717 | module_put(THIS_MODULE); |
Alex Elder | 27cc259 | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 3718 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 3719 | dout("Error adding device %s\n", buf); |
Alex Elder | 27cc259 | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 3720 | |
| 3721 | return (ssize_t) rc; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 3722 | } |
| 3723 | |
Alex Elder | de71a29 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 3724 | static struct rbd_device *__rbd_get_dev(unsigned long dev_id) |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 3725 | { |
| 3726 | struct list_head *tmp; |
| 3727 | struct rbd_device *rbd_dev; |
| 3728 | |
Alex Elder | e124a82f | 2012-01-29 13:57:44 -0600 | [diff] [blame] | 3729 | spin_lock(&rbd_dev_list_lock); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 3730 | list_for_each(tmp, &rbd_dev_list) { |
| 3731 | rbd_dev = list_entry(tmp, struct rbd_device, node); |
Alex Elder | de71a29 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 3732 | if (rbd_dev->dev_id == dev_id) { |
Alex Elder | e124a82f | 2012-01-29 13:57:44 -0600 | [diff] [blame] | 3733 | spin_unlock(&rbd_dev_list_lock); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 3734 | return rbd_dev; |
Alex Elder | e124a82f | 2012-01-29 13:57:44 -0600 | [diff] [blame] | 3735 | } |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 3736 | } |
Alex Elder | e124a82f | 2012-01-29 13:57:44 -0600 | [diff] [blame] | 3737 | spin_unlock(&rbd_dev_list_lock); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 3738 | return NULL; |
| 3739 | } |
| 3740 | |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 3741 | static void rbd_dev_release(struct device *dev) |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 3742 | { |
Alex Elder | 593a9e7 | 2012-02-07 12:03:37 -0600 | [diff] [blame] | 3743 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 3744 | |
Alex Elder | 1dbb439 | 2012-01-24 10:08:37 -0600 | [diff] [blame] | 3745 | if (rbd_dev->watch_request) { |
| 3746 | struct ceph_client *client = rbd_dev->rbd_client->client; |
| 3747 | |
| 3748 | ceph_osdc_unregister_linger_request(&client->osdc, |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 3749 | rbd_dev->watch_request); |
Alex Elder | 1dbb439 | 2012-01-24 10:08:37 -0600 | [diff] [blame] | 3750 | } |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 3751 | if (rbd_dev->watch_event) |
Alex Elder | 070c633 | 2012-07-25 09:32:41 -0500 | [diff] [blame] | 3752 | rbd_req_sync_unwatch(rbd_dev); |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 3753 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 3754 | |
| 3755 | /* clean up and free blkdev */ |
| 3756 | rbd_free_disk(rbd_dev); |
| 3757 | unregister_blkdev(rbd_dev->major, rbd_dev->name); |
Alex Elder | 32eec68 | 2012-02-08 16:11:14 -0600 | [diff] [blame] | 3758 | |
Alex Elder | 2ac4e75 | 2012-07-10 20:30:10 -0500 | [diff] [blame] | 3759 | /* release allocated disk header fields */ |
| 3760 | rbd_header_free(&rbd_dev->header); |
| 3761 | |
Alex Elder | 32eec68 | 2012-02-08 16:11:14 -0600 | [diff] [blame] | 3762 | /* done with the id, and with the rbd_dev */ |
Alex Elder | e283930 | 2012-08-29 17:11:06 -0500 | [diff] [blame] | 3763 | rbd_dev_id_put(rbd_dev); |
Alex Elder | c53d589 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 3764 | rbd_assert(rbd_dev->rbd_client != NULL); |
| 3765 | rbd_dev_destroy(rbd_dev); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 3766 | |
| 3767 | /* release module ref */ |
| 3768 | module_put(THIS_MODULE); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 3769 | } |
| 3770 | |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 3771 | static ssize_t rbd_remove(struct bus_type *bus, |
| 3772 | const char *buf, |
| 3773 | size_t count) |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 3774 | { |
| 3775 | struct rbd_device *rbd_dev = NULL; |
| 3776 | int target_id, rc; |
| 3777 | unsigned long ul; |
| 3778 | int ret = count; |
| 3779 | |
| 3780 | rc = strict_strtoul(buf, 10, &ul); |
| 3781 | if (rc) |
| 3782 | return rc; |
| 3783 | |
| 3784 | /* convert to int; abort if we lost anything in the conversion */ |
| 3785 | target_id = (int) ul; |
| 3786 | if (target_id != ul) |
| 3787 | return -EINVAL; |
| 3788 | |
| 3789 | mutex_lock_nested(&ctl_mutex, SINGLE_DEPTH_NESTING); |
| 3790 | |
| 3791 | rbd_dev = __rbd_get_dev(target_id); |
| 3792 | if (!rbd_dev) { |
| 3793 | ret = -ENOENT; |
| 3794 | goto done; |
| 3795 | } |
| 3796 | |
Alex Elder | 42382b7 | 2012-11-16 09:29:16 -0600 | [diff] [blame] | 3797 | if (rbd_dev->open_count) { |
| 3798 | ret = -EBUSY; |
| 3799 | goto done; |
| 3800 | } |
| 3801 | |
Alex Elder | 41f38c2 | 2012-10-25 23:34:40 -0500 | [diff] [blame] | 3802 | rbd_remove_all_snaps(rbd_dev); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 3803 | rbd_bus_del_dev(rbd_dev); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 3804 | |
| 3805 | done: |
| 3806 | mutex_unlock(&ctl_mutex); |
Alex Elder | aafb230 | 2012-09-06 16:00:54 -0500 | [diff] [blame] | 3807 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 3808 | return ret; |
| 3809 | } |
| 3810 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 3811 | /* |
| 3812 | * create control files in sysfs |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 3813 | * /sys/bus/rbd/... |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 3814 | */ |
| 3815 | static int rbd_sysfs_init(void) |
| 3816 | { |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 3817 | int ret; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 3818 | |
Alex Elder | fed4c14 | 2012-02-07 12:03:36 -0600 | [diff] [blame] | 3819 | ret = device_register(&rbd_root_dev); |
Alex Elder | 2107978 | 2012-01-24 10:08:36 -0600 | [diff] [blame] | 3820 | if (ret < 0) |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 3821 | return ret; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 3822 | |
Alex Elder | fed4c14 | 2012-02-07 12:03:36 -0600 | [diff] [blame] | 3823 | ret = bus_register(&rbd_bus_type); |
| 3824 | if (ret < 0) |
| 3825 | device_unregister(&rbd_root_dev); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 3826 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 3827 | return ret; |
| 3828 | } |
| 3829 | |
| 3830 | static void rbd_sysfs_cleanup(void) |
| 3831 | { |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 3832 | bus_unregister(&rbd_bus_type); |
Alex Elder | fed4c14 | 2012-02-07 12:03:36 -0600 | [diff] [blame] | 3833 | device_unregister(&rbd_root_dev); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 3834 | } |
| 3835 | |
| 3836 | int __init rbd_init(void) |
| 3837 | { |
| 3838 | int rc; |
| 3839 | |
| 3840 | rc = rbd_sysfs_init(); |
| 3841 | if (rc) |
| 3842 | return rc; |
Alex Elder | f0f8cef | 2012-01-29 13:57:44 -0600 | [diff] [blame] | 3843 | pr_info("loaded " RBD_DRV_NAME_LONG "\n"); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 3844 | return 0; |
| 3845 | } |
| 3846 | |
| 3847 | void __exit rbd_exit(void) |
| 3848 | { |
| 3849 | rbd_sysfs_cleanup(); |
| 3850 | } |
| 3851 | |
| 3852 | module_init(rbd_init); |
| 3853 | module_exit(rbd_exit); |
| 3854 | |
| 3855 | MODULE_AUTHOR("Sage Weil <sage@newdream.net>"); |
| 3856 | MODULE_AUTHOR("Yehuda Sadeh <yehuda@hq.newdream.net>"); |
| 3857 | MODULE_DESCRIPTION("rados block device"); |
| 3858 | |
| 3859 | /* following authorship retained from original osdblk.c */ |
| 3860 | MODULE_AUTHOR("Jeff Garzik <jeff@garzik.org>"); |
| 3861 | |
| 3862 | MODULE_LICENSE("GPL"); |