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