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