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