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