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