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