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