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