Alex Elder | e2a58ee | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 1 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 2 | /* |
| 3 | rbd.c -- Export ceph rados objects as a Linux block device |
| 4 | |
| 5 | |
| 6 | based on drivers/block/osdblk.c: |
| 7 | |
| 8 | Copyright 2009 Red Hat, Inc. |
| 9 | |
| 10 | This program is free software; you can redistribute it and/or modify |
| 11 | it under the terms of the GNU General Public License as published by |
| 12 | the Free Software Foundation. |
| 13 | |
| 14 | This program is distributed in the hope that it will be useful, |
| 15 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 | GNU General Public License for more details. |
| 18 | |
| 19 | You should have received a copy of the GNU General Public License |
| 20 | along with this program; see the file COPYING. If not, write to |
| 21 | the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. |
| 22 | |
| 23 | |
| 24 | |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 25 | For usage instructions, please refer to: |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 26 | |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 27 | Documentation/ABI/testing/sysfs-bus-rbd |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 28 | |
| 29 | */ |
| 30 | |
| 31 | #include <linux/ceph/libceph.h> |
| 32 | #include <linux/ceph/osd_client.h> |
| 33 | #include <linux/ceph/mon_client.h> |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 34 | #include <linux/ceph/cls_lock_client.h> |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 35 | #include <linux/ceph/decode.h> |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 36 | #include <linux/parser.h> |
Alex Elder | 30d1cff | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 37 | #include <linux/bsearch.h> |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 38 | |
| 39 | #include <linux/kernel.h> |
| 40 | #include <linux/device.h> |
| 41 | #include <linux/module.h> |
Christoph Hellwig | 7ad18af | 2015-01-13 17:20:04 +0100 | [diff] [blame] | 42 | #include <linux/blk-mq.h> |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 43 | #include <linux/fs.h> |
| 44 | #include <linux/blkdev.h> |
Alex Elder | 1c2a9df | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 45 | #include <linux/slab.h> |
Ilya Dryomov | f8a22fc | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 46 | #include <linux/idr.h> |
Ilya Dryomov | bc1ecc6 | 2014-08-04 18:04:39 +0400 | [diff] [blame] | 47 | #include <linux/workqueue.h> |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 48 | |
| 49 | #include "rbd_types.h" |
| 50 | |
Alex Elder | aafb230 | 2012-09-06 16:00:54 -0500 | [diff] [blame] | 51 | #define RBD_DEBUG /* Activate rbd_assert() calls */ |
| 52 | |
Alex Elder | 593a9e7 | 2012-02-07 12:03:37 -0600 | [diff] [blame] | 53 | /* |
| 54 | * The basic unit of block I/O is a sector. It is interpreted in a |
| 55 | * number of contexts in Linux (blk, bio, genhd), but the default is |
| 56 | * universally 512 bytes. These symbols are just slightly more |
| 57 | * meaningful than the bare numbers they represent. |
| 58 | */ |
| 59 | #define SECTOR_SHIFT 9 |
| 60 | #define SECTOR_SIZE (1ULL << SECTOR_SHIFT) |
| 61 | |
Alex Elder | a2acd00 | 2013-05-08 22:50:04 -0500 | [diff] [blame] | 62 | /* |
| 63 | * Increment the given counter and return its updated value. |
| 64 | * If the counter is already 0 it will not be incremented. |
| 65 | * If the counter is already at its maximum value returns |
| 66 | * -EINVAL without updating it. |
| 67 | */ |
| 68 | static int atomic_inc_return_safe(atomic_t *v) |
| 69 | { |
| 70 | unsigned int counter; |
| 71 | |
| 72 | counter = (unsigned int)__atomic_add_unless(v, 1, 0); |
| 73 | if (counter <= (unsigned int)INT_MAX) |
| 74 | return (int)counter; |
| 75 | |
| 76 | atomic_dec(v); |
| 77 | |
| 78 | return -EINVAL; |
| 79 | } |
| 80 | |
| 81 | /* Decrement the counter. Return the resulting value, or -EINVAL */ |
| 82 | static int atomic_dec_return_safe(atomic_t *v) |
| 83 | { |
| 84 | int counter; |
| 85 | |
| 86 | counter = atomic_dec_return(v); |
| 87 | if (counter >= 0) |
| 88 | return counter; |
| 89 | |
| 90 | atomic_inc(v); |
| 91 | |
| 92 | return -EINVAL; |
| 93 | } |
| 94 | |
Alex Elder | f0f8cef | 2012-01-29 13:57:44 -0600 | [diff] [blame] | 95 | #define RBD_DRV_NAME "rbd" |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 96 | |
Ilya Dryomov | 7e513d4 | 2013-12-16 19:26:32 +0200 | [diff] [blame] | 97 | #define RBD_MINORS_PER_MAJOR 256 |
| 98 | #define RBD_SINGLE_MAJOR_PART_SHIFT 4 |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 99 | |
Ilya Dryomov | 6d69bb53 | 2015-10-11 19:38:00 +0200 | [diff] [blame] | 100 | #define RBD_MAX_PARENT_CHAIN_LEN 16 |
| 101 | |
Alex Elder | d4b125e | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 102 | #define RBD_SNAP_DEV_NAME_PREFIX "snap_" |
| 103 | #define RBD_MAX_SNAP_NAME_LEN \ |
| 104 | (NAME_MAX - (sizeof (RBD_SNAP_DEV_NAME_PREFIX) - 1)) |
| 105 | |
Alex Elder | 35d489f | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 106 | #define RBD_MAX_SNAP_COUNT 510 /* allows max snapc to fit in 4KB */ |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 107 | |
| 108 | #define RBD_SNAP_HEAD_NAME "-" |
| 109 | |
Alex Elder | 9682fc6 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 110 | #define BAD_SNAP_INDEX U32_MAX /* invalid index into snap array */ |
| 111 | |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 112 | /* This allows a single page to hold an image name sent by OSD */ |
| 113 | #define RBD_IMAGE_NAME_LEN_MAX (PAGE_SIZE - sizeof (__le32) - 1) |
Alex Elder | 1e13019 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 114 | #define RBD_IMAGE_ID_LEN_MAX 64 |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 115 | |
Alex Elder | 1e13019 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 116 | #define RBD_OBJ_PREFIX_LEN_MAX 64 |
Alex Elder | 589d30e | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 117 | |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 118 | #define RBD_NOTIFY_TIMEOUT 5 /* seconds */ |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 119 | #define RBD_RETRY_DELAY msecs_to_jiffies(1000) |
| 120 | |
Alex Elder | d889140 | 2012-10-09 13:50:17 -0700 | [diff] [blame] | 121 | /* Feature bits */ |
| 122 | |
Alex Elder | 5cbf6f12 | 2013-04-11 09:29:48 -0500 | [diff] [blame] | 123 | #define RBD_FEATURE_LAYERING (1<<0) |
| 124 | #define RBD_FEATURE_STRIPINGV2 (1<<1) |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 125 | #define RBD_FEATURE_EXCLUSIVE_LOCK (1<<2) |
| 126 | #define RBD_FEATURES_ALL (RBD_FEATURE_LAYERING | \ |
| 127 | RBD_FEATURE_STRIPINGV2 | \ |
| 128 | RBD_FEATURE_EXCLUSIVE_LOCK) |
Alex Elder | d889140 | 2012-10-09 13:50:17 -0700 | [diff] [blame] | 129 | |
| 130 | /* Features supported by this (client software) implementation. */ |
| 131 | |
Alex Elder | 770eba6 | 2012-10-25 23:34:40 -0500 | [diff] [blame] | 132 | #define RBD_FEATURES_SUPPORTED (RBD_FEATURES_ALL) |
Alex Elder | d889140 | 2012-10-09 13:50:17 -0700 | [diff] [blame] | 133 | |
Alex Elder | 81a8979 | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 134 | /* |
| 135 | * An RBD device name will be "rbd#", where the "rbd" comes from |
| 136 | * RBD_DRV_NAME above, and # is a unique integer identifier. |
Alex Elder | 81a8979 | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 137 | */ |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 138 | #define DEV_NAME_LEN 32 |
| 139 | |
| 140 | /* |
| 141 | * block device image metadata (in-memory version) |
| 142 | */ |
| 143 | struct rbd_image_header { |
Alex Elder | f35a4de | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 144 | /* These six fields never change for a given rbd image */ |
Alex Elder | 849b426 | 2012-07-09 21:04:24 -0500 | [diff] [blame] | 145 | char *object_prefix; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 146 | __u8 obj_order; |
| 147 | __u8 crypt_type; |
| 148 | __u8 comp_type; |
Alex Elder | f35a4de | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 149 | u64 stripe_unit; |
| 150 | u64 stripe_count; |
| 151 | u64 features; /* Might be changeable someday? */ |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 152 | |
Alex Elder | f84344f | 2012-08-31 17:29:51 -0500 | [diff] [blame] | 153 | /* The remaining fields need to be updated occasionally */ |
| 154 | u64 image_size; |
| 155 | struct ceph_snap_context *snapc; |
Alex Elder | f35a4de | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 156 | char *snap_names; /* format 1 only */ |
| 157 | u64 *snap_sizes; /* format 1 only */ |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 158 | }; |
| 159 | |
Alex Elder | 0d7dbfc | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 160 | /* |
| 161 | * An rbd image specification. |
| 162 | * |
| 163 | * The tuple (pool_id, image_id, snap_id) is sufficient to uniquely |
Alex Elder | c66c6e0 | 2012-11-01 08:39:26 -0500 | [diff] [blame] | 164 | * identify an image. Each rbd_dev structure includes a pointer to |
| 165 | * an rbd_spec structure that encapsulates this identity. |
| 166 | * |
| 167 | * Each of the id's in an rbd_spec has an associated name. For a |
| 168 | * user-mapped image, the names are supplied and the id's associated |
| 169 | * with them are looked up. For a layered image, a parent image is |
| 170 | * defined by the tuple, and the names are looked up. |
| 171 | * |
| 172 | * An rbd_dev structure contains a parent_spec pointer which is |
| 173 | * non-null if the image it represents is a child in a layered |
| 174 | * image. This pointer will refer to the rbd_spec structure used |
| 175 | * by the parent rbd_dev for its own identity (i.e., the structure |
| 176 | * is shared between the parent and child). |
| 177 | * |
| 178 | * Since these structures are populated once, during the discovery |
| 179 | * phase of image construction, they are effectively immutable so |
| 180 | * we make no effort to synchronize access to them. |
| 181 | * |
| 182 | * Note that code herein does not assume the image name is known (it |
| 183 | * could be a null pointer). |
Alex Elder | 0d7dbfc | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 184 | */ |
| 185 | struct rbd_spec { |
| 186 | u64 pool_id; |
Alex Elder | ecb4dc22 | 2013-04-26 09:43:47 -0500 | [diff] [blame] | 187 | const char *pool_name; |
Alex Elder | 0d7dbfc | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 188 | |
Alex Elder | ecb4dc22 | 2013-04-26 09:43:47 -0500 | [diff] [blame] | 189 | const char *image_id; |
| 190 | const char *image_name; |
Alex Elder | 0d7dbfc | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 191 | |
| 192 | u64 snap_id; |
Alex Elder | ecb4dc22 | 2013-04-26 09:43:47 -0500 | [diff] [blame] | 193 | const char *snap_name; |
Alex Elder | 0d7dbfc | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 194 | |
| 195 | struct kref kref; |
| 196 | }; |
| 197 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 198 | /* |
Alex Elder | f0f8cef | 2012-01-29 13:57:44 -0600 | [diff] [blame] | 199 | * an instance of the client. multiple devices may share an rbd client. |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 200 | */ |
| 201 | struct rbd_client { |
| 202 | struct ceph_client *client; |
| 203 | struct kref kref; |
| 204 | struct list_head node; |
| 205 | }; |
| 206 | |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 207 | struct rbd_img_request; |
| 208 | typedef void (*rbd_img_callback_t)(struct rbd_img_request *); |
| 209 | |
| 210 | #define BAD_WHICH U32_MAX /* Good which or bad which, which? */ |
| 211 | |
| 212 | struct rbd_obj_request; |
| 213 | typedef void (*rbd_obj_callback_t)(struct rbd_obj_request *); |
| 214 | |
Alex Elder | 9969ebc | 2013-01-18 12:31:10 -0600 | [diff] [blame] | 215 | enum obj_request_type { |
| 216 | OBJ_REQUEST_NODATA, OBJ_REQUEST_BIO, OBJ_REQUEST_PAGES |
| 217 | }; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 218 | |
Guangliang Zhao | 6d2940c | 2014-03-13 11:21:35 +0800 | [diff] [blame] | 219 | enum obj_operation_type { |
| 220 | OBJ_OP_WRITE, |
| 221 | OBJ_OP_READ, |
Guangliang Zhao | 90e98c5 | 2014-04-01 22:22:16 +0800 | [diff] [blame] | 222 | OBJ_OP_DISCARD, |
Guangliang Zhao | 6d2940c | 2014-03-13 11:21:35 +0800 | [diff] [blame] | 223 | }; |
| 224 | |
Alex Elder | 926f9b3 | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 225 | enum obj_req_flags { |
| 226 | OBJ_REQ_DONE, /* completion flag: not done = 0, done = 1 */ |
Alex Elder | 6365d33 | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 227 | OBJ_REQ_IMG_DATA, /* object usage: standalone = 0, image = 1 */ |
Alex Elder | 5679c59 | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 228 | OBJ_REQ_KNOWN, /* EXISTS flag valid: no = 0, yes = 1 */ |
| 229 | OBJ_REQ_EXISTS, /* target exists: no = 0, yes = 1 */ |
Alex Elder | 926f9b3 | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 230 | }; |
| 231 | |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 232 | struct rbd_obj_request { |
| 233 | const char *object_name; |
| 234 | u64 offset; /* object start byte */ |
| 235 | u64 length; /* bytes from offset */ |
Alex Elder | 926f9b3 | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 236 | unsigned long flags; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 237 | |
Alex Elder | c5b5ef6 | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 238 | /* |
| 239 | * An object request associated with an image will have its |
| 240 | * img_data flag set; a standalone object request will not. |
| 241 | * |
| 242 | * A standalone object request will have which == BAD_WHICH |
| 243 | * and a null obj_request pointer. |
| 244 | * |
| 245 | * An object request initiated in support of a layered image |
| 246 | * object (to check for its existence before a write) will |
| 247 | * have which == BAD_WHICH and a non-null obj_request pointer. |
| 248 | * |
| 249 | * Finally, an object request for rbd image data will have |
| 250 | * which != BAD_WHICH, and will have a non-null img_request |
| 251 | * pointer. The value of which will be in the range |
| 252 | * 0..(img_request->obj_request_count-1). |
| 253 | */ |
| 254 | union { |
| 255 | struct rbd_obj_request *obj_request; /* STAT op */ |
| 256 | struct { |
| 257 | struct rbd_img_request *img_request; |
| 258 | u64 img_offset; |
| 259 | /* links for img_request->obj_requests list */ |
| 260 | struct list_head links; |
| 261 | }; |
| 262 | }; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 263 | u32 which; /* posn image request list */ |
| 264 | |
| 265 | enum obj_request_type type; |
Alex Elder | 788e2df | 2013-01-17 12:25:27 -0600 | [diff] [blame] | 266 | union { |
| 267 | struct bio *bio_list; |
| 268 | struct { |
| 269 | struct page **pages; |
| 270 | u32 page_count; |
| 271 | }; |
| 272 | }; |
Alex Elder | 0eefd47 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 273 | struct page **copyup_pages; |
Alex Elder | ebda640 | 2013-05-10 16:29:22 -0500 | [diff] [blame] | 274 | u32 copyup_page_count; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 275 | |
| 276 | struct ceph_osd_request *osd_req; |
| 277 | |
| 278 | u64 xferred; /* bytes transferred */ |
Sage Weil | 1b83bef | 2013-02-25 16:11:12 -0800 | [diff] [blame] | 279 | int result; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 280 | |
| 281 | rbd_obj_callback_t callback; |
Alex Elder | 788e2df | 2013-01-17 12:25:27 -0600 | [diff] [blame] | 282 | struct completion completion; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 283 | |
| 284 | struct kref kref; |
| 285 | }; |
| 286 | |
Alex Elder | 0c42524 | 2013-02-08 09:55:49 -0600 | [diff] [blame] | 287 | enum img_req_flags { |
Alex Elder | 9849e98 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 288 | IMG_REQ_WRITE, /* I/O direction: read = 0, write = 1 */ |
| 289 | IMG_REQ_CHILD, /* initiator: block = 0, child image = 1 */ |
Alex Elder | d0b2e94 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 290 | IMG_REQ_LAYERED, /* ENOENT handling: normal = 0, layered = 1 */ |
Guangliang Zhao | 90e98c5 | 2014-04-01 22:22:16 +0800 | [diff] [blame] | 291 | IMG_REQ_DISCARD, /* discard: normal = 0, discard request = 1 */ |
Alex Elder | 0c42524 | 2013-02-08 09:55:49 -0600 | [diff] [blame] | 292 | }; |
| 293 | |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 294 | struct rbd_img_request { |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 295 | struct rbd_device *rbd_dev; |
| 296 | u64 offset; /* starting image byte offset */ |
| 297 | u64 length; /* byte count from offset */ |
Alex Elder | 0c42524 | 2013-02-08 09:55:49 -0600 | [diff] [blame] | 298 | unsigned long flags; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 299 | union { |
Alex Elder | 9849e98 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 300 | u64 snap_id; /* for reads */ |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 301 | struct ceph_snap_context *snapc; /* for writes */ |
Alex Elder | 9849e98 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 302 | }; |
| 303 | union { |
| 304 | struct request *rq; /* block request */ |
| 305 | struct rbd_obj_request *obj_request; /* obj req initiator */ |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 306 | }; |
Alex Elder | 3d7efd1 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 307 | struct page **copyup_pages; |
Alex Elder | ebda640 | 2013-05-10 16:29:22 -0500 | [diff] [blame] | 308 | u32 copyup_page_count; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 309 | spinlock_t completion_lock;/* protects next_completion */ |
| 310 | u32 next_completion; |
| 311 | rbd_img_callback_t callback; |
Alex Elder | 55f27e0 | 2013-04-10 12:34:25 -0500 | [diff] [blame] | 312 | u64 xferred;/* aggregate bytes transferred */ |
Alex Elder | a5a337d | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 313 | int result; /* first nonzero obj_request result */ |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 314 | |
| 315 | u32 obj_request_count; |
| 316 | struct list_head obj_requests; /* rbd_obj_request structs */ |
| 317 | |
| 318 | struct kref kref; |
| 319 | }; |
| 320 | |
| 321 | #define for_each_obj_request(ireq, oreq) \ |
Alex Elder | ef06f4d3 | 2013-02-08 09:55:48 -0600 | [diff] [blame] | 322 | list_for_each_entry(oreq, &(ireq)->obj_requests, links) |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 323 | #define for_each_obj_request_from(ireq, oreq) \ |
Alex Elder | ef06f4d3 | 2013-02-08 09:55:48 -0600 | [diff] [blame] | 324 | list_for_each_entry_from(oreq, &(ireq)->obj_requests, links) |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 325 | #define for_each_obj_request_safe(ireq, oreq, n) \ |
Alex Elder | ef06f4d3 | 2013-02-08 09:55:48 -0600 | [diff] [blame] | 326 | list_for_each_entry_safe_reverse(oreq, n, &(ireq)->obj_requests, links) |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 327 | |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 328 | enum rbd_watch_state { |
| 329 | RBD_WATCH_STATE_UNREGISTERED, |
| 330 | RBD_WATCH_STATE_REGISTERED, |
| 331 | RBD_WATCH_STATE_ERROR, |
| 332 | }; |
| 333 | |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 334 | enum rbd_lock_state { |
| 335 | RBD_LOCK_STATE_UNLOCKED, |
| 336 | RBD_LOCK_STATE_LOCKED, |
| 337 | RBD_LOCK_STATE_RELEASING, |
| 338 | }; |
| 339 | |
| 340 | /* WatchNotify::ClientId */ |
| 341 | struct rbd_client_id { |
| 342 | u64 gid; |
| 343 | u64 handle; |
| 344 | }; |
| 345 | |
Alex Elder | f84344f | 2012-08-31 17:29:51 -0500 | [diff] [blame] | 346 | struct rbd_mapping { |
Alex Elder | 99c1f08 | 2012-08-30 14:42:15 -0500 | [diff] [blame] | 347 | u64 size; |
Alex Elder | 34b1318 | 2012-07-13 20:35:12 -0500 | [diff] [blame] | 348 | u64 features; |
Alex Elder | f84344f | 2012-08-31 17:29:51 -0500 | [diff] [blame] | 349 | bool read_only; |
| 350 | }; |
| 351 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 352 | /* |
| 353 | * a single device |
| 354 | */ |
| 355 | struct rbd_device { |
Alex Elder | de71a29 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 356 | int dev_id; /* blkdev unique id */ |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 357 | |
| 358 | int major; /* blkdev assigned major */ |
Ilya Dryomov | dd82fff | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 359 | int minor; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 360 | struct gendisk *disk; /* blkdev's gendisk and rq */ |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 361 | |
Alex Elder | a30b71b | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 362 | u32 image_format; /* Either 1 or 2 */ |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 363 | struct rbd_client *rbd_client; |
| 364 | |
| 365 | char name[DEV_NAME_LEN]; /* blkdev name, e.g. rbd3 */ |
| 366 | |
Alex Elder | b82d167 | 2013-01-14 12:43:31 -0600 | [diff] [blame] | 367 | spinlock_t lock; /* queue, flags, open_count */ |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 368 | |
| 369 | struct rbd_image_header header; |
Alex Elder | b82d167 | 2013-01-14 12:43:31 -0600 | [diff] [blame] | 370 | unsigned long flags; /* possibly lock protected */ |
Alex Elder | 0d7dbfc | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 371 | struct rbd_spec *spec; |
Ilya Dryomov | d147543 | 2015-06-22 13:24:48 +0300 | [diff] [blame] | 372 | struct rbd_options *opts; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 373 | |
Ilya Dryomov | c41d13a | 2016-04-29 20:01:25 +0200 | [diff] [blame] | 374 | struct ceph_object_id header_oid; |
Ilya Dryomov | 922dab6 | 2016-05-26 01:15:02 +0200 | [diff] [blame] | 375 | struct ceph_object_locator header_oloc; |
Alex Elder | 971f839 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 376 | |
Ilya Dryomov | 1643dfa | 2016-08-12 15:45:52 +0200 | [diff] [blame] | 377 | struct ceph_file_layout layout; /* used for all rbd requests */ |
Alex Elder | 0903e87 | 2012-11-14 12:25:19 -0600 | [diff] [blame] | 378 | |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 379 | struct mutex watch_mutex; |
| 380 | enum rbd_watch_state watch_state; |
Ilya Dryomov | 922dab6 | 2016-05-26 01:15:02 +0200 | [diff] [blame] | 381 | struct ceph_osd_linger_request *watch_handle; |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 382 | u64 watch_cookie; |
| 383 | struct delayed_work watch_dwork; |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 384 | |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 385 | struct rw_semaphore lock_rwsem; |
| 386 | enum rbd_lock_state lock_state; |
| 387 | struct rbd_client_id owner_cid; |
| 388 | struct work_struct acquired_lock_work; |
| 389 | struct work_struct released_lock_work; |
| 390 | struct delayed_work lock_dwork; |
| 391 | struct work_struct unlock_work; |
| 392 | wait_queue_head_t lock_waitq; |
| 393 | |
Ilya Dryomov | 1643dfa | 2016-08-12 15:45:52 +0200 | [diff] [blame] | 394 | struct workqueue_struct *task_wq; |
| 395 | |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 396 | struct rbd_spec *parent_spec; |
| 397 | u64 parent_overlap; |
Alex Elder | a2acd00 | 2013-05-08 22:50:04 -0500 | [diff] [blame] | 398 | atomic_t parent_ref; |
Alex Elder | 2f82ee5 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 399 | struct rbd_device *parent; |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 400 | |
Christoph Hellwig | 7ad18af | 2015-01-13 17:20:04 +0100 | [diff] [blame] | 401 | /* Block layer tags. */ |
| 402 | struct blk_mq_tag_set tag_set; |
| 403 | |
Josh Durgin | c666601 | 2011-11-21 17:11:12 -0800 | [diff] [blame] | 404 | /* protects updating the header */ |
| 405 | struct rw_semaphore header_rwsem; |
Alex Elder | f84344f | 2012-08-31 17:29:51 -0500 | [diff] [blame] | 406 | |
| 407 | struct rbd_mapping mapping; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 408 | |
| 409 | struct list_head node; |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 410 | |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 411 | /* sysfs related */ |
| 412 | struct device dev; |
Alex Elder | b82d167 | 2013-01-14 12:43:31 -0600 | [diff] [blame] | 413 | unsigned long open_count; /* protected by lock */ |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 414 | }; |
| 415 | |
Alex Elder | b82d167 | 2013-01-14 12:43:31 -0600 | [diff] [blame] | 416 | /* |
| 417 | * Flag bits for rbd_dev->flags. If atomicity is required, |
| 418 | * rbd_dev->lock is used to protect access. |
| 419 | * |
| 420 | * Currently, only the "removing" flag (which is coupled with the |
| 421 | * "open_count" field) requires atomic access. |
| 422 | */ |
Alex Elder | 6d29290 | 2013-01-14 12:43:31 -0600 | [diff] [blame] | 423 | enum rbd_dev_flags { |
| 424 | RBD_DEV_FLAG_EXISTS, /* mapped snapshot has not been deleted */ |
Alex Elder | b82d167 | 2013-01-14 12:43:31 -0600 | [diff] [blame] | 425 | RBD_DEV_FLAG_REMOVING, /* this mapping is being removed */ |
Alex Elder | 6d29290 | 2013-01-14 12:43:31 -0600 | [diff] [blame] | 426 | }; |
| 427 | |
Alex Elder | cfbf637 | 2013-05-31 17:40:45 -0500 | [diff] [blame] | 428 | static DEFINE_MUTEX(client_mutex); /* Serialize client creation */ |
Alex Elder | e124a82f | 2012-01-29 13:57:44 -0600 | [diff] [blame] | 429 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 430 | static LIST_HEAD(rbd_dev_list); /* devices */ |
Alex Elder | e124a82f | 2012-01-29 13:57:44 -0600 | [diff] [blame] | 431 | static DEFINE_SPINLOCK(rbd_dev_list_lock); |
| 432 | |
Alex Elder | 432b858 | 2012-01-29 13:57:44 -0600 | [diff] [blame] | 433 | static LIST_HEAD(rbd_client_list); /* clients */ |
| 434 | static DEFINE_SPINLOCK(rbd_client_list_lock); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 435 | |
Alex Elder | 78c2a44 | 2013-05-01 12:43:04 -0500 | [diff] [blame] | 436 | /* Slab caches for frequently-allocated structures */ |
| 437 | |
Alex Elder | 1c2a9df | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 438 | static struct kmem_cache *rbd_img_request_cache; |
Alex Elder | 868311b | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 439 | static struct kmem_cache *rbd_obj_request_cache; |
Alex Elder | 78c2a44 | 2013-05-01 12:43:04 -0500 | [diff] [blame] | 440 | static struct kmem_cache *rbd_segment_name_cache; |
Alex Elder | 1c2a9df | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 441 | |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 442 | static int rbd_major; |
Ilya Dryomov | f8a22fc | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 443 | static DEFINE_IDA(rbd_dev_id_ida); |
| 444 | |
Ilya Dryomov | f5ee37b | 2014-10-09 17:06:01 +0400 | [diff] [blame] | 445 | static struct workqueue_struct *rbd_wq; |
| 446 | |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 447 | /* |
| 448 | * Default to false for now, as single-major requires >= 0.75 version of |
| 449 | * userspace rbd utility. |
| 450 | */ |
| 451 | static bool single_major = false; |
| 452 | module_param(single_major, bool, S_IRUGO); |
| 453 | MODULE_PARM_DESC(single_major, "Use a single major number for all rbd devices (default: false)"); |
| 454 | |
Alex Elder | 3d7efd1 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 455 | static int rbd_img_request_submit(struct rbd_img_request *img_request); |
| 456 | |
Alex Elder | f0f8cef | 2012-01-29 13:57:44 -0600 | [diff] [blame] | 457 | static ssize_t rbd_add(struct bus_type *bus, const char *buf, |
| 458 | size_t count); |
| 459 | static ssize_t rbd_remove(struct bus_type *bus, const char *buf, |
| 460 | size_t count); |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 461 | static ssize_t rbd_add_single_major(struct bus_type *bus, const char *buf, |
| 462 | size_t count); |
| 463 | static ssize_t rbd_remove_single_major(struct bus_type *bus, const char *buf, |
| 464 | size_t count); |
Ilya Dryomov | 6d69bb53 | 2015-10-11 19:38:00 +0200 | [diff] [blame] | 465 | static int rbd_dev_image_probe(struct rbd_device *rbd_dev, int depth); |
Alex Elder | a2acd00 | 2013-05-08 22:50:04 -0500 | [diff] [blame] | 466 | static void rbd_spec_put(struct rbd_spec *spec); |
Alex Elder | f0f8cef | 2012-01-29 13:57:44 -0600 | [diff] [blame] | 467 | |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 468 | static int rbd_dev_id_to_minor(int dev_id) |
| 469 | { |
Ilya Dryomov | 7e513d4 | 2013-12-16 19:26:32 +0200 | [diff] [blame] | 470 | return dev_id << RBD_SINGLE_MAJOR_PART_SHIFT; |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 471 | } |
| 472 | |
| 473 | static int minor_to_rbd_dev_id(int minor) |
| 474 | { |
Ilya Dryomov | 7e513d4 | 2013-12-16 19:26:32 +0200 | [diff] [blame] | 475 | return minor >> RBD_SINGLE_MAJOR_PART_SHIFT; |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 476 | } |
| 477 | |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 478 | static bool rbd_is_lock_supported(struct rbd_device *rbd_dev) |
| 479 | { |
| 480 | return (rbd_dev->header.features & RBD_FEATURE_EXCLUSIVE_LOCK) && |
| 481 | rbd_dev->spec->snap_id == CEPH_NOSNAP && |
| 482 | !rbd_dev->mapping.read_only; |
| 483 | } |
| 484 | |
| 485 | static bool __rbd_is_lock_owner(struct rbd_device *rbd_dev) |
| 486 | { |
| 487 | return rbd_dev->lock_state == RBD_LOCK_STATE_LOCKED || |
| 488 | rbd_dev->lock_state == RBD_LOCK_STATE_RELEASING; |
| 489 | } |
| 490 | |
| 491 | static bool rbd_is_lock_owner(struct rbd_device *rbd_dev) |
| 492 | { |
| 493 | bool is_lock_owner; |
| 494 | |
| 495 | down_read(&rbd_dev->lock_rwsem); |
| 496 | is_lock_owner = __rbd_is_lock_owner(rbd_dev); |
| 497 | up_read(&rbd_dev->lock_rwsem); |
| 498 | return is_lock_owner; |
| 499 | } |
| 500 | |
Greg Kroah-Hartman | b15a21d | 2013-08-23 14:24:28 -0700 | [diff] [blame] | 501 | static BUS_ATTR(add, S_IWUSR, NULL, rbd_add); |
| 502 | static BUS_ATTR(remove, S_IWUSR, NULL, rbd_remove); |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 503 | static BUS_ATTR(add_single_major, S_IWUSR, NULL, rbd_add_single_major); |
| 504 | static BUS_ATTR(remove_single_major, S_IWUSR, NULL, rbd_remove_single_major); |
Greg Kroah-Hartman | b15a21d | 2013-08-23 14:24:28 -0700 | [diff] [blame] | 505 | |
| 506 | static struct attribute *rbd_bus_attrs[] = { |
| 507 | &bus_attr_add.attr, |
| 508 | &bus_attr_remove.attr, |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 509 | &bus_attr_add_single_major.attr, |
| 510 | &bus_attr_remove_single_major.attr, |
Greg Kroah-Hartman | b15a21d | 2013-08-23 14:24:28 -0700 | [diff] [blame] | 511 | NULL, |
Alex Elder | f0f8cef | 2012-01-29 13:57:44 -0600 | [diff] [blame] | 512 | }; |
Ilya Dryomov | 92c76dc | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 513 | |
| 514 | static umode_t rbd_bus_is_visible(struct kobject *kobj, |
| 515 | struct attribute *attr, int index) |
| 516 | { |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 517 | if (!single_major && |
| 518 | (attr == &bus_attr_add_single_major.attr || |
| 519 | attr == &bus_attr_remove_single_major.attr)) |
| 520 | return 0; |
| 521 | |
Ilya Dryomov | 92c76dc | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 522 | return attr->mode; |
| 523 | } |
| 524 | |
| 525 | static const struct attribute_group rbd_bus_group = { |
| 526 | .attrs = rbd_bus_attrs, |
| 527 | .is_visible = rbd_bus_is_visible, |
| 528 | }; |
| 529 | __ATTRIBUTE_GROUPS(rbd_bus); |
Alex Elder | f0f8cef | 2012-01-29 13:57:44 -0600 | [diff] [blame] | 530 | |
| 531 | static struct bus_type rbd_bus_type = { |
| 532 | .name = "rbd", |
Greg Kroah-Hartman | b15a21d | 2013-08-23 14:24:28 -0700 | [diff] [blame] | 533 | .bus_groups = rbd_bus_groups, |
Alex Elder | f0f8cef | 2012-01-29 13:57:44 -0600 | [diff] [blame] | 534 | }; |
| 535 | |
| 536 | static void rbd_root_dev_release(struct device *dev) |
| 537 | { |
| 538 | } |
| 539 | |
| 540 | static struct device rbd_root_dev = { |
| 541 | .init_name = "rbd", |
| 542 | .release = rbd_root_dev_release, |
| 543 | }; |
| 544 | |
Alex Elder | 06ecc6c | 2012-11-01 10:17:15 -0500 | [diff] [blame] | 545 | static __printf(2, 3) |
| 546 | void rbd_warn(struct rbd_device *rbd_dev, const char *fmt, ...) |
| 547 | { |
| 548 | struct va_format vaf; |
| 549 | va_list args; |
| 550 | |
| 551 | va_start(args, fmt); |
| 552 | vaf.fmt = fmt; |
| 553 | vaf.va = &args; |
| 554 | |
| 555 | if (!rbd_dev) |
| 556 | printk(KERN_WARNING "%s: %pV\n", RBD_DRV_NAME, &vaf); |
| 557 | else if (rbd_dev->disk) |
| 558 | printk(KERN_WARNING "%s: %s: %pV\n", |
| 559 | RBD_DRV_NAME, rbd_dev->disk->disk_name, &vaf); |
| 560 | else if (rbd_dev->spec && rbd_dev->spec->image_name) |
| 561 | printk(KERN_WARNING "%s: image %s: %pV\n", |
| 562 | RBD_DRV_NAME, rbd_dev->spec->image_name, &vaf); |
| 563 | else if (rbd_dev->spec && rbd_dev->spec->image_id) |
| 564 | printk(KERN_WARNING "%s: id %s: %pV\n", |
| 565 | RBD_DRV_NAME, rbd_dev->spec->image_id, &vaf); |
| 566 | else /* punt */ |
| 567 | printk(KERN_WARNING "%s: rbd_dev %p: %pV\n", |
| 568 | RBD_DRV_NAME, rbd_dev, &vaf); |
| 569 | va_end(args); |
| 570 | } |
| 571 | |
Alex Elder | aafb230 | 2012-09-06 16:00:54 -0500 | [diff] [blame] | 572 | #ifdef RBD_DEBUG |
| 573 | #define rbd_assert(expr) \ |
| 574 | if (unlikely(!(expr))) { \ |
| 575 | printk(KERN_ERR "\nAssertion failure in %s() " \ |
| 576 | "at line %d:\n\n" \ |
| 577 | "\trbd_assert(%s);\n\n", \ |
| 578 | __func__, __LINE__, #expr); \ |
| 579 | BUG(); \ |
| 580 | } |
| 581 | #else /* !RBD_DEBUG */ |
| 582 | # define rbd_assert(expr) ((void) 0) |
| 583 | #endif /* !RBD_DEBUG */ |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 584 | |
Ilya Dryomov | 2761713 | 2015-07-16 17:36:11 +0300 | [diff] [blame] | 585 | static void rbd_osd_copyup_callback(struct rbd_obj_request *obj_request); |
Alex Elder | b454e36 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 586 | static int rbd_img_obj_request_submit(struct rbd_obj_request *obj_request); |
Alex Elder | 05a46af | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 587 | static void rbd_img_parent_read(struct rbd_obj_request *obj_request); |
| 588 | static void rbd_dev_remove_parent(struct rbd_device *rbd_dev); |
Alex Elder | 8b3e1a5 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 589 | |
Alex Elder | cc4a38bd | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 590 | static int rbd_dev_refresh(struct rbd_device *rbd_dev); |
Alex Elder | 2df3fac | 2013-05-06 09:51:30 -0500 | [diff] [blame] | 591 | static int rbd_dev_v2_header_onetime(struct rbd_device *rbd_dev); |
Ilya Dryomov | a720ae0 | 2014-07-23 17:11:19 +0400 | [diff] [blame] | 592 | static int rbd_dev_header_info(struct rbd_device *rbd_dev); |
Ilya Dryomov | e8f59b5 | 2014-07-24 10:42:13 +0400 | [diff] [blame] | 593 | static int rbd_dev_v2_parent_info(struct rbd_device *rbd_dev); |
Alex Elder | 54cac61 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 594 | static const char *rbd_dev_v2_snap_name(struct rbd_device *rbd_dev, |
| 595 | u64 snap_id); |
Alex Elder | 2ad3d71 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 596 | static int _rbd_dev_v2_snap_size(struct rbd_device *rbd_dev, u64 snap_id, |
| 597 | u8 *order, u64 *snap_size); |
| 598 | static int _rbd_dev_v2_snap_features(struct rbd_device *rbd_dev, u64 snap_id, |
| 599 | u64 *snap_features); |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 600 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 601 | static int rbd_open(struct block_device *bdev, fmode_t mode) |
| 602 | { |
Alex Elder | f0f8cef | 2012-01-29 13:57:44 -0600 | [diff] [blame] | 603 | struct rbd_device *rbd_dev = bdev->bd_disk->private_data; |
Alex Elder | b82d167 | 2013-01-14 12:43:31 -0600 | [diff] [blame] | 604 | bool removing = false; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 605 | |
Alex Elder | f84344f | 2012-08-31 17:29:51 -0500 | [diff] [blame] | 606 | if ((mode & FMODE_WRITE) && rbd_dev->mapping.read_only) |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 607 | return -EROFS; |
| 608 | |
Alex Elder | a14ea26 | 2013-02-05 13:23:12 -0600 | [diff] [blame] | 609 | spin_lock_irq(&rbd_dev->lock); |
Alex Elder | b82d167 | 2013-01-14 12:43:31 -0600 | [diff] [blame] | 610 | if (test_bit(RBD_DEV_FLAG_REMOVING, &rbd_dev->flags)) |
| 611 | removing = true; |
| 612 | else |
| 613 | rbd_dev->open_count++; |
Alex Elder | a14ea26 | 2013-02-05 13:23:12 -0600 | [diff] [blame] | 614 | spin_unlock_irq(&rbd_dev->lock); |
Alex Elder | b82d167 | 2013-01-14 12:43:31 -0600 | [diff] [blame] | 615 | if (removing) |
| 616 | return -ENOENT; |
| 617 | |
Alex Elder | c3e946c | 2012-11-16 09:29:16 -0600 | [diff] [blame] | 618 | (void) get_device(&rbd_dev->dev); |
Alex Elder | 340c7a2 | 2012-08-10 13:12:07 -0700 | [diff] [blame] | 619 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 620 | return 0; |
| 621 | } |
| 622 | |
Al Viro | db2a144 | 2013-05-05 21:52:57 -0400 | [diff] [blame] | 623 | static void rbd_release(struct gendisk *disk, fmode_t mode) |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 624 | { |
| 625 | struct rbd_device *rbd_dev = disk->private_data; |
Alex Elder | b82d167 | 2013-01-14 12:43:31 -0600 | [diff] [blame] | 626 | unsigned long open_count_before; |
| 627 | |
Alex Elder | a14ea26 | 2013-02-05 13:23:12 -0600 | [diff] [blame] | 628 | spin_lock_irq(&rbd_dev->lock); |
Alex Elder | b82d167 | 2013-01-14 12:43:31 -0600 | [diff] [blame] | 629 | open_count_before = rbd_dev->open_count--; |
Alex Elder | a14ea26 | 2013-02-05 13:23:12 -0600 | [diff] [blame] | 630 | spin_unlock_irq(&rbd_dev->lock); |
Alex Elder | b82d167 | 2013-01-14 12:43:31 -0600 | [diff] [blame] | 631 | rbd_assert(open_count_before > 0); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 632 | |
Alex Elder | c3e946c | 2012-11-16 09:29:16 -0600 | [diff] [blame] | 633 | put_device(&rbd_dev->dev); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 634 | } |
| 635 | |
Guangliang Zhao | 131fd9f | 2013-09-24 11:25:36 +0800 | [diff] [blame] | 636 | static int rbd_ioctl_set_ro(struct rbd_device *rbd_dev, unsigned long arg) |
| 637 | { |
Josh Durgin | 77f33c0 | 2013-09-30 17:09:54 -0700 | [diff] [blame] | 638 | int ret = 0; |
Guangliang Zhao | 131fd9f | 2013-09-24 11:25:36 +0800 | [diff] [blame] | 639 | int val; |
| 640 | bool ro; |
Josh Durgin | 77f33c0 | 2013-09-30 17:09:54 -0700 | [diff] [blame] | 641 | bool ro_changed = false; |
Guangliang Zhao | 131fd9f | 2013-09-24 11:25:36 +0800 | [diff] [blame] | 642 | |
Josh Durgin | 77f33c0 | 2013-09-30 17:09:54 -0700 | [diff] [blame] | 643 | /* get_user() may sleep, so call it before taking rbd_dev->lock */ |
Guangliang Zhao | 131fd9f | 2013-09-24 11:25:36 +0800 | [diff] [blame] | 644 | if (get_user(val, (int __user *)(arg))) |
| 645 | return -EFAULT; |
| 646 | |
| 647 | ro = val ? true : false; |
| 648 | /* Snapshot doesn't allow to write*/ |
| 649 | if (rbd_dev->spec->snap_id != CEPH_NOSNAP && !ro) |
| 650 | return -EROFS; |
| 651 | |
Josh Durgin | 77f33c0 | 2013-09-30 17:09:54 -0700 | [diff] [blame] | 652 | spin_lock_irq(&rbd_dev->lock); |
| 653 | /* prevent others open this device */ |
| 654 | if (rbd_dev->open_count > 1) { |
| 655 | ret = -EBUSY; |
| 656 | goto out; |
Guangliang Zhao | 131fd9f | 2013-09-24 11:25:36 +0800 | [diff] [blame] | 657 | } |
| 658 | |
Josh Durgin | 77f33c0 | 2013-09-30 17:09:54 -0700 | [diff] [blame] | 659 | if (rbd_dev->mapping.read_only != ro) { |
| 660 | rbd_dev->mapping.read_only = ro; |
| 661 | ro_changed = true; |
| 662 | } |
| 663 | |
| 664 | out: |
| 665 | spin_unlock_irq(&rbd_dev->lock); |
| 666 | /* set_disk_ro() may sleep, so call it after releasing rbd_dev->lock */ |
| 667 | if (ret == 0 && ro_changed) |
| 668 | set_disk_ro(rbd_dev->disk, ro ? 1 : 0); |
| 669 | |
| 670 | return ret; |
Guangliang Zhao | 131fd9f | 2013-09-24 11:25:36 +0800 | [diff] [blame] | 671 | } |
| 672 | |
| 673 | static int rbd_ioctl(struct block_device *bdev, fmode_t mode, |
| 674 | unsigned int cmd, unsigned long arg) |
| 675 | { |
| 676 | struct rbd_device *rbd_dev = bdev->bd_disk->private_data; |
| 677 | int ret = 0; |
| 678 | |
Guangliang Zhao | 131fd9f | 2013-09-24 11:25:36 +0800 | [diff] [blame] | 679 | switch (cmd) { |
| 680 | case BLKROSET: |
| 681 | ret = rbd_ioctl_set_ro(rbd_dev, arg); |
| 682 | break; |
| 683 | default: |
| 684 | ret = -ENOTTY; |
| 685 | } |
| 686 | |
Guangliang Zhao | 131fd9f | 2013-09-24 11:25:36 +0800 | [diff] [blame] | 687 | return ret; |
| 688 | } |
| 689 | |
| 690 | #ifdef CONFIG_COMPAT |
| 691 | static int rbd_compat_ioctl(struct block_device *bdev, fmode_t mode, |
| 692 | unsigned int cmd, unsigned long arg) |
| 693 | { |
| 694 | return rbd_ioctl(bdev, mode, cmd, arg); |
| 695 | } |
| 696 | #endif /* CONFIG_COMPAT */ |
| 697 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 698 | static const struct block_device_operations rbd_bd_ops = { |
| 699 | .owner = THIS_MODULE, |
| 700 | .open = rbd_open, |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 701 | .release = rbd_release, |
Guangliang Zhao | 131fd9f | 2013-09-24 11:25:36 +0800 | [diff] [blame] | 702 | .ioctl = rbd_ioctl, |
| 703 | #ifdef CONFIG_COMPAT |
| 704 | .compat_ioctl = rbd_compat_ioctl, |
| 705 | #endif |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 706 | }; |
| 707 | |
| 708 | /* |
Alex Elder | 7262cfc | 2013-05-16 15:04:20 -0500 | [diff] [blame] | 709 | * Initialize an rbd client instance. Success or not, this function |
Alex Elder | cfbf637 | 2013-05-31 17:40:45 -0500 | [diff] [blame] | 710 | * consumes ceph_opts. Caller holds client_mutex. |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 711 | */ |
Alex Elder | f8c3892 | 2012-08-10 13:12:07 -0700 | [diff] [blame] | 712 | static struct rbd_client *rbd_client_create(struct ceph_options *ceph_opts) |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 713 | { |
| 714 | struct rbd_client *rbdc; |
| 715 | int ret = -ENOMEM; |
| 716 | |
Alex Elder | 37206ee | 2013-02-20 17:32:08 -0600 | [diff] [blame] | 717 | dout("%s:\n", __func__); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 718 | rbdc = kmalloc(sizeof(struct rbd_client), GFP_KERNEL); |
| 719 | if (!rbdc) |
| 720 | goto out_opt; |
| 721 | |
| 722 | kref_init(&rbdc->kref); |
| 723 | INIT_LIST_HEAD(&rbdc->node); |
| 724 | |
Alex Elder | 43ae470 | 2012-07-03 16:01:18 -0500 | [diff] [blame] | 725 | rbdc->client = ceph_create_client(ceph_opts, rbdc, 0, 0); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 726 | if (IS_ERR(rbdc->client)) |
Alex Elder | 08f7546 | 2013-05-29 11:19:00 -0500 | [diff] [blame] | 727 | goto out_rbdc; |
Alex Elder | 43ae470 | 2012-07-03 16:01:18 -0500 | [diff] [blame] | 728 | ceph_opts = NULL; /* Now rbdc->client is responsible for ceph_opts */ |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 729 | |
| 730 | ret = ceph_open_session(rbdc->client); |
| 731 | if (ret < 0) |
Alex Elder | 08f7546 | 2013-05-29 11:19:00 -0500 | [diff] [blame] | 732 | goto out_client; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 733 | |
Alex Elder | 432b858 | 2012-01-29 13:57:44 -0600 | [diff] [blame] | 734 | spin_lock(&rbd_client_list_lock); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 735 | list_add_tail(&rbdc->node, &rbd_client_list); |
Alex Elder | 432b858 | 2012-01-29 13:57:44 -0600 | [diff] [blame] | 736 | spin_unlock(&rbd_client_list_lock); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 737 | |
Alex Elder | 37206ee | 2013-02-20 17:32:08 -0600 | [diff] [blame] | 738 | dout("%s: rbdc %p\n", __func__, rbdc); |
Alex Elder | bc534d8 | 2012-01-29 13:57:44 -0600 | [diff] [blame] | 739 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 740 | return rbdc; |
Alex Elder | 08f7546 | 2013-05-29 11:19:00 -0500 | [diff] [blame] | 741 | out_client: |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 742 | ceph_destroy_client(rbdc->client); |
Alex Elder | 08f7546 | 2013-05-29 11:19:00 -0500 | [diff] [blame] | 743 | out_rbdc: |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 744 | kfree(rbdc); |
| 745 | out_opt: |
Alex Elder | 43ae470 | 2012-07-03 16:01:18 -0500 | [diff] [blame] | 746 | if (ceph_opts) |
| 747 | ceph_destroy_options(ceph_opts); |
Alex Elder | 37206ee | 2013-02-20 17:32:08 -0600 | [diff] [blame] | 748 | dout("%s: error %d\n", __func__, ret); |
| 749 | |
Vasiliy Kulikov | 28f259b | 2010-09-26 12:59:37 +0400 | [diff] [blame] | 750 | return ERR_PTR(ret); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 751 | } |
| 752 | |
Alex Elder | 2f82ee5 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 753 | static struct rbd_client *__rbd_get_client(struct rbd_client *rbdc) |
| 754 | { |
| 755 | kref_get(&rbdc->kref); |
| 756 | |
| 757 | return rbdc; |
| 758 | } |
| 759 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 760 | /* |
Alex Elder | 1f7ba33 | 2012-08-10 13:12:07 -0700 | [diff] [blame] | 761 | * Find a ceph client with specific addr and configuration. If |
| 762 | * found, bump its reference count. |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 763 | */ |
Alex Elder | 1f7ba33 | 2012-08-10 13:12:07 -0700 | [diff] [blame] | 764 | static struct rbd_client *rbd_client_find(struct ceph_options *ceph_opts) |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 765 | { |
| 766 | struct rbd_client *client_node; |
Alex Elder | 1f7ba33 | 2012-08-10 13:12:07 -0700 | [diff] [blame] | 767 | bool found = false; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 768 | |
Alex Elder | 43ae470 | 2012-07-03 16:01:18 -0500 | [diff] [blame] | 769 | if (ceph_opts->flags & CEPH_OPT_NOSHARE) |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 770 | return NULL; |
| 771 | |
Alex Elder | 1f7ba33 | 2012-08-10 13:12:07 -0700 | [diff] [blame] | 772 | spin_lock(&rbd_client_list_lock); |
| 773 | list_for_each_entry(client_node, &rbd_client_list, node) { |
| 774 | if (!ceph_compare_options(ceph_opts, client_node->client)) { |
Alex Elder | 2f82ee5 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 775 | __rbd_get_client(client_node); |
| 776 | |
Alex Elder | 1f7ba33 | 2012-08-10 13:12:07 -0700 | [diff] [blame] | 777 | found = true; |
| 778 | break; |
| 779 | } |
| 780 | } |
| 781 | spin_unlock(&rbd_client_list_lock); |
| 782 | |
| 783 | return found ? client_node : NULL; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 784 | } |
| 785 | |
| 786 | /* |
Ilya Dryomov | 210c104 | 2015-06-22 13:24:48 +0300 | [diff] [blame] | 787 | * (Per device) rbd map options |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 788 | */ |
| 789 | enum { |
Ilya Dryomov | b558418 | 2015-06-23 16:21:19 +0300 | [diff] [blame] | 790 | Opt_queue_depth, |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 791 | Opt_last_int, |
| 792 | /* int args above */ |
| 793 | Opt_last_string, |
| 794 | /* string args above */ |
Alex Elder | cc0538b | 2012-08-10 13:12:07 -0700 | [diff] [blame] | 795 | Opt_read_only, |
| 796 | Opt_read_write, |
Ilya Dryomov | 210c104 | 2015-06-22 13:24:48 +0300 | [diff] [blame] | 797 | Opt_err |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 798 | }; |
| 799 | |
Alex Elder | 43ae470 | 2012-07-03 16:01:18 -0500 | [diff] [blame] | 800 | static match_table_t rbd_opts_tokens = { |
Ilya Dryomov | b558418 | 2015-06-23 16:21:19 +0300 | [diff] [blame] | 801 | {Opt_queue_depth, "queue_depth=%d"}, |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 802 | /* int args above */ |
| 803 | /* string args above */ |
Alex Elder | be466c1 | 2012-10-22 11:31:26 -0500 | [diff] [blame] | 804 | {Opt_read_only, "read_only"}, |
Alex Elder | cc0538b | 2012-08-10 13:12:07 -0700 | [diff] [blame] | 805 | {Opt_read_only, "ro"}, /* Alternate spelling */ |
| 806 | {Opt_read_write, "read_write"}, |
| 807 | {Opt_read_write, "rw"}, /* Alternate spelling */ |
Ilya Dryomov | 210c104 | 2015-06-22 13:24:48 +0300 | [diff] [blame] | 808 | {Opt_err, NULL} |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 809 | }; |
| 810 | |
Alex Elder | 98571b5 | 2013-01-20 14:44:42 -0600 | [diff] [blame] | 811 | struct rbd_options { |
Ilya Dryomov | b558418 | 2015-06-23 16:21:19 +0300 | [diff] [blame] | 812 | int queue_depth; |
Alex Elder | 98571b5 | 2013-01-20 14:44:42 -0600 | [diff] [blame] | 813 | bool read_only; |
| 814 | }; |
| 815 | |
Ilya Dryomov | b558418 | 2015-06-23 16:21:19 +0300 | [diff] [blame] | 816 | #define RBD_QUEUE_DEPTH_DEFAULT BLKDEV_MAX_RQ |
Alex Elder | 98571b5 | 2013-01-20 14:44:42 -0600 | [diff] [blame] | 817 | #define RBD_READ_ONLY_DEFAULT false |
| 818 | |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 819 | static int parse_rbd_opts_token(char *c, void *private) |
| 820 | { |
Alex Elder | 43ae470 | 2012-07-03 16:01:18 -0500 | [diff] [blame] | 821 | struct rbd_options *rbd_opts = private; |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 822 | substring_t argstr[MAX_OPT_ARGS]; |
| 823 | int token, intval, ret; |
| 824 | |
Alex Elder | 43ae470 | 2012-07-03 16:01:18 -0500 | [diff] [blame] | 825 | token = match_token(c, rbd_opts_tokens, argstr); |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 826 | if (token < Opt_last_int) { |
| 827 | ret = match_int(&argstr[0], &intval); |
| 828 | if (ret < 0) { |
Ilya Dryomov | 210c104 | 2015-06-22 13:24:48 +0300 | [diff] [blame] | 829 | pr_err("bad mount option arg (not int) at '%s'\n", c); |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 830 | return ret; |
| 831 | } |
| 832 | dout("got int token %d val %d\n", token, intval); |
| 833 | } else if (token > Opt_last_int && token < Opt_last_string) { |
Ilya Dryomov | 210c104 | 2015-06-22 13:24:48 +0300 | [diff] [blame] | 834 | dout("got string token %d val %s\n", token, argstr[0].from); |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 835 | } else { |
| 836 | dout("got token %d\n", token); |
| 837 | } |
| 838 | |
| 839 | switch (token) { |
Ilya Dryomov | b558418 | 2015-06-23 16:21:19 +0300 | [diff] [blame] | 840 | case Opt_queue_depth: |
| 841 | if (intval < 1) { |
| 842 | pr_err("queue_depth out of range\n"); |
| 843 | return -EINVAL; |
| 844 | } |
| 845 | rbd_opts->queue_depth = intval; |
| 846 | break; |
Alex Elder | cc0538b | 2012-08-10 13:12:07 -0700 | [diff] [blame] | 847 | case Opt_read_only: |
| 848 | rbd_opts->read_only = true; |
| 849 | break; |
| 850 | case Opt_read_write: |
| 851 | rbd_opts->read_only = false; |
| 852 | break; |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 853 | default: |
Ilya Dryomov | 210c104 | 2015-06-22 13:24:48 +0300 | [diff] [blame] | 854 | /* libceph prints "bad option" msg */ |
| 855 | return -EINVAL; |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 856 | } |
Ilya Dryomov | 210c104 | 2015-06-22 13:24:48 +0300 | [diff] [blame] | 857 | |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 858 | return 0; |
| 859 | } |
| 860 | |
Guangliang Zhao | 6d2940c | 2014-03-13 11:21:35 +0800 | [diff] [blame] | 861 | static char* obj_op_name(enum obj_operation_type op_type) |
| 862 | { |
| 863 | switch (op_type) { |
| 864 | case OBJ_OP_READ: |
| 865 | return "read"; |
| 866 | case OBJ_OP_WRITE: |
| 867 | return "write"; |
Guangliang Zhao | 90e98c5 | 2014-04-01 22:22:16 +0800 | [diff] [blame] | 868 | case OBJ_OP_DISCARD: |
| 869 | return "discard"; |
Guangliang Zhao | 6d2940c | 2014-03-13 11:21:35 +0800 | [diff] [blame] | 870 | default: |
| 871 | return "???"; |
| 872 | } |
| 873 | } |
| 874 | |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 875 | /* |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 876 | * Get a ceph client with specific addr and configuration, if one does |
Alex Elder | 7262cfc | 2013-05-16 15:04:20 -0500 | [diff] [blame] | 877 | * not exist create it. Either way, ceph_opts is consumed by this |
| 878 | * function. |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 879 | */ |
Alex Elder | 9d3997f | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 880 | static struct rbd_client *rbd_get_client(struct ceph_options *ceph_opts) |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 881 | { |
Alex Elder | f8c3892 | 2012-08-10 13:12:07 -0700 | [diff] [blame] | 882 | struct rbd_client *rbdc; |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 883 | |
Alex Elder | cfbf637 | 2013-05-31 17:40:45 -0500 | [diff] [blame] | 884 | mutex_lock_nested(&client_mutex, SINGLE_DEPTH_NESTING); |
Alex Elder | 1f7ba33 | 2012-08-10 13:12:07 -0700 | [diff] [blame] | 885 | rbdc = rbd_client_find(ceph_opts); |
Alex Elder | 9d3997f | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 886 | if (rbdc) /* using an existing client */ |
Alex Elder | 43ae470 | 2012-07-03 16:01:18 -0500 | [diff] [blame] | 887 | ceph_destroy_options(ceph_opts); |
Alex Elder | 9d3997f | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 888 | else |
Alex Elder | f8c3892 | 2012-08-10 13:12:07 -0700 | [diff] [blame] | 889 | rbdc = rbd_client_create(ceph_opts); |
Alex Elder | cfbf637 | 2013-05-31 17:40:45 -0500 | [diff] [blame] | 890 | mutex_unlock(&client_mutex); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 891 | |
Alex Elder | 9d3997f | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 892 | return rbdc; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 893 | } |
| 894 | |
| 895 | /* |
| 896 | * Destroy ceph client |
Alex Elder | d23a4b3 | 2012-01-29 13:57:43 -0600 | [diff] [blame] | 897 | * |
Alex Elder | 432b858 | 2012-01-29 13:57:44 -0600 | [diff] [blame] | 898 | * Caller must hold rbd_client_list_lock. |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 899 | */ |
| 900 | static void rbd_client_release(struct kref *kref) |
| 901 | { |
| 902 | struct rbd_client *rbdc = container_of(kref, struct rbd_client, kref); |
| 903 | |
Alex Elder | 37206ee | 2013-02-20 17:32:08 -0600 | [diff] [blame] | 904 | dout("%s: rbdc %p\n", __func__, rbdc); |
Alex Elder | cd9d9f5 | 2012-04-04 13:35:44 -0500 | [diff] [blame] | 905 | spin_lock(&rbd_client_list_lock); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 906 | list_del(&rbdc->node); |
Alex Elder | cd9d9f5 | 2012-04-04 13:35:44 -0500 | [diff] [blame] | 907 | spin_unlock(&rbd_client_list_lock); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 908 | |
| 909 | ceph_destroy_client(rbdc->client); |
| 910 | kfree(rbdc); |
| 911 | } |
| 912 | |
| 913 | /* |
| 914 | * Drop reference to ceph client node. If it's not referenced anymore, release |
| 915 | * it. |
| 916 | */ |
Alex Elder | 9d3997f | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 917 | static void rbd_put_client(struct rbd_client *rbdc) |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 918 | { |
Alex Elder | c53d589 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 919 | if (rbdc) |
| 920 | kref_put(&rbdc->kref, rbd_client_release); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 921 | } |
| 922 | |
Alex Elder | a30b71b | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 923 | static bool rbd_image_format_valid(u32 image_format) |
| 924 | { |
| 925 | return image_format == 1 || image_format == 2; |
| 926 | } |
| 927 | |
Alex Elder | 8e94af8 | 2012-07-25 09:32:40 -0500 | [diff] [blame] | 928 | static bool rbd_dev_ondisk_valid(struct rbd_image_header_ondisk *ondisk) |
| 929 | { |
Alex Elder | 103a150 | 2012-08-02 11:29:45 -0500 | [diff] [blame] | 930 | size_t size; |
| 931 | u32 snap_count; |
| 932 | |
| 933 | /* The header has to start with the magic rbd header text */ |
| 934 | if (memcmp(&ondisk->text, RBD_HEADER_TEXT, sizeof (RBD_HEADER_TEXT))) |
| 935 | return false; |
| 936 | |
Alex Elder | db2388b | 2012-10-20 22:17:27 -0500 | [diff] [blame] | 937 | /* The bio layer requires at least sector-sized I/O */ |
| 938 | |
| 939 | if (ondisk->options.order < SECTOR_SHIFT) |
| 940 | return false; |
| 941 | |
| 942 | /* If we use u64 in a few spots we may be able to loosen this */ |
| 943 | |
| 944 | if (ondisk->options.order > 8 * sizeof (int) - 1) |
| 945 | return false; |
| 946 | |
Alex Elder | 103a150 | 2012-08-02 11:29:45 -0500 | [diff] [blame] | 947 | /* |
| 948 | * The size of a snapshot header has to fit in a size_t, and |
| 949 | * that limits the number of snapshots. |
| 950 | */ |
| 951 | snap_count = le32_to_cpu(ondisk->snap_count); |
| 952 | size = SIZE_MAX - sizeof (struct ceph_snap_context); |
| 953 | if (snap_count > size / sizeof (__le64)) |
| 954 | return false; |
| 955 | |
| 956 | /* |
| 957 | * Not only that, but the size of the entire the snapshot |
| 958 | * header must also be representable in a size_t. |
| 959 | */ |
| 960 | size -= snap_count * sizeof (__le64); |
| 961 | if ((u64) size < le64_to_cpu(ondisk->snap_names_len)) |
| 962 | return false; |
| 963 | |
| 964 | return true; |
Alex Elder | 8e94af8 | 2012-07-25 09:32:40 -0500 | [diff] [blame] | 965 | } |
| 966 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 967 | /* |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 968 | * Fill an rbd image header with information from the given format 1 |
| 969 | * on-disk header. |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 970 | */ |
Alex Elder | 662518b | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 971 | static int rbd_header_from_disk(struct rbd_device *rbd_dev, |
Alex Elder | 4156d99 | 2012-08-02 11:29:46 -0500 | [diff] [blame] | 972 | struct rbd_image_header_ondisk *ondisk) |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 973 | { |
Alex Elder | 662518b | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 974 | struct rbd_image_header *header = &rbd_dev->header; |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 975 | bool first_time = header->object_prefix == NULL; |
| 976 | struct ceph_snap_context *snapc; |
| 977 | char *object_prefix = NULL; |
| 978 | char *snap_names = NULL; |
| 979 | u64 *snap_sizes = NULL; |
Alex Elder | ccece23 | 2012-07-10 20:30:10 -0500 | [diff] [blame] | 980 | u32 snap_count; |
Alex Elder | d2bb24e | 2012-07-26 23:37:14 -0500 | [diff] [blame] | 981 | size_t size; |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 982 | int ret = -ENOMEM; |
Alex Elder | 621901d | 2012-08-23 23:22:06 -0500 | [diff] [blame] | 983 | u32 i; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 984 | |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 985 | /* Allocate this now to avoid having to handle failure below */ |
| 986 | |
| 987 | if (first_time) { |
| 988 | size_t len; |
| 989 | |
| 990 | len = strnlen(ondisk->object_prefix, |
| 991 | sizeof (ondisk->object_prefix)); |
| 992 | object_prefix = kmalloc(len + 1, GFP_KERNEL); |
| 993 | if (!object_prefix) |
| 994 | return -ENOMEM; |
| 995 | memcpy(object_prefix, ondisk->object_prefix, len); |
| 996 | object_prefix[len] = '\0'; |
| 997 | } |
| 998 | |
| 999 | /* Allocate the snapshot context and fill it in */ |
Alex Elder | 6a52325 | 2012-07-19 17:12:59 -0500 | [diff] [blame] | 1000 | |
Alex Elder | 103a150 | 2012-08-02 11:29:45 -0500 | [diff] [blame] | 1001 | snap_count = le32_to_cpu(ondisk->snap_count); |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1002 | snapc = ceph_create_snap_context(snap_count, GFP_KERNEL); |
| 1003 | if (!snapc) |
| 1004 | goto out_err; |
| 1005 | snapc->seq = le64_to_cpu(ondisk->snap_seq); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1006 | if (snap_count) { |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1007 | struct rbd_image_snap_ondisk *snaps; |
Alex Elder | f785cc1 | 2012-08-23 23:22:06 -0500 | [diff] [blame] | 1008 | u64 snap_names_len = le64_to_cpu(ondisk->snap_names_len); |
| 1009 | |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1010 | /* We'll keep a copy of the snapshot names... */ |
Alex Elder | 621901d | 2012-08-23 23:22:06 -0500 | [diff] [blame] | 1011 | |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1012 | if (snap_names_len > (u64)SIZE_MAX) |
| 1013 | goto out_2big; |
| 1014 | snap_names = kmalloc(snap_names_len, GFP_KERNEL); |
| 1015 | if (!snap_names) |
Alex Elder | 6a52325 | 2012-07-19 17:12:59 -0500 | [diff] [blame] | 1016 | goto out_err; |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1017 | |
| 1018 | /* ...as well as the array of their sizes. */ |
| 1019 | |
| 1020 | size = snap_count * sizeof (*header->snap_sizes); |
| 1021 | snap_sizes = kmalloc(size, GFP_KERNEL); |
| 1022 | if (!snap_sizes) |
| 1023 | goto out_err; |
| 1024 | |
Alex Elder | f785cc1 | 2012-08-23 23:22:06 -0500 | [diff] [blame] | 1025 | /* |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1026 | * Copy the names, and fill in each snapshot's id |
| 1027 | * and size. |
| 1028 | * |
Alex Elder | 99a41eb | 2013-05-06 09:51:30 -0500 | [diff] [blame] | 1029 | * Note that rbd_dev_v1_header_info() guarantees the |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1030 | * ondisk buffer we're working with has |
Alex Elder | f785cc1 | 2012-08-23 23:22:06 -0500 | [diff] [blame] | 1031 | * snap_names_len bytes beyond the end of the |
| 1032 | * snapshot id array, this memcpy() is safe. |
| 1033 | */ |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1034 | memcpy(snap_names, &ondisk->snaps[snap_count], snap_names_len); |
| 1035 | snaps = ondisk->snaps; |
| 1036 | for (i = 0; i < snap_count; i++) { |
| 1037 | snapc->snaps[i] = le64_to_cpu(snaps[i].id); |
| 1038 | snap_sizes[i] = le64_to_cpu(snaps[i].image_size); |
| 1039 | } |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1040 | } |
Alex Elder | 849b426 | 2012-07-09 21:04:24 -0500 | [diff] [blame] | 1041 | |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1042 | /* We won't fail any more, fill in the header */ |
Alex Elder | 6a52325 | 2012-07-19 17:12:59 -0500 | [diff] [blame] | 1043 | |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1044 | if (first_time) { |
| 1045 | header->object_prefix = object_prefix; |
| 1046 | header->obj_order = ondisk->options.order; |
| 1047 | header->crypt_type = ondisk->options.crypt_type; |
| 1048 | header->comp_type = ondisk->options.comp_type; |
| 1049 | /* The rest aren't used for format 1 images */ |
| 1050 | header->stripe_unit = 0; |
| 1051 | header->stripe_count = 0; |
| 1052 | header->features = 0; |
Alex Elder | 662518b | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1053 | } else { |
| 1054 | ceph_put_snap_context(header->snapc); |
| 1055 | kfree(header->snap_names); |
| 1056 | kfree(header->snap_sizes); |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1057 | } |
| 1058 | |
| 1059 | /* The remaining fields always get updated (when we refresh) */ |
Alex Elder | 621901d | 2012-08-23 23:22:06 -0500 | [diff] [blame] | 1060 | |
Alex Elder | f84344f | 2012-08-31 17:29:51 -0500 | [diff] [blame] | 1061 | header->image_size = le64_to_cpu(ondisk->image_size); |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1062 | header->snapc = snapc; |
| 1063 | header->snap_names = snap_names; |
| 1064 | header->snap_sizes = snap_sizes; |
Alex Elder | 468521c | 2013-04-26 09:43:47 -0500 | [diff] [blame] | 1065 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1066 | return 0; |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1067 | out_2big: |
| 1068 | ret = -EIO; |
Alex Elder | 6a52325 | 2012-07-19 17:12:59 -0500 | [diff] [blame] | 1069 | out_err: |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1070 | kfree(snap_sizes); |
| 1071 | kfree(snap_names); |
| 1072 | ceph_put_snap_context(snapc); |
| 1073 | kfree(object_prefix); |
Alex Elder | ccece23 | 2012-07-10 20:30:10 -0500 | [diff] [blame] | 1074 | |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1075 | return ret; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1076 | } |
| 1077 | |
Alex Elder | 9682fc6 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 1078 | static const char *_rbd_dev_v1_snap_name(struct rbd_device *rbd_dev, u32 which) |
| 1079 | { |
| 1080 | const char *snap_name; |
| 1081 | |
| 1082 | rbd_assert(which < rbd_dev->header.snapc->num_snaps); |
| 1083 | |
| 1084 | /* Skip over names until we find the one we are looking for */ |
| 1085 | |
| 1086 | snap_name = rbd_dev->header.snap_names; |
| 1087 | while (which--) |
| 1088 | snap_name += strlen(snap_name) + 1; |
| 1089 | |
| 1090 | return kstrdup(snap_name, GFP_KERNEL); |
| 1091 | } |
| 1092 | |
Alex Elder | 30d1cff | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 1093 | /* |
| 1094 | * Snapshot id comparison function for use with qsort()/bsearch(). |
| 1095 | * Note that result is for snapshots in *descending* order. |
| 1096 | */ |
| 1097 | static int snapid_compare_reverse(const void *s1, const void *s2) |
| 1098 | { |
| 1099 | u64 snap_id1 = *(u64 *)s1; |
| 1100 | u64 snap_id2 = *(u64 *)s2; |
| 1101 | |
| 1102 | if (snap_id1 < snap_id2) |
| 1103 | return 1; |
| 1104 | return snap_id1 == snap_id2 ? 0 : -1; |
| 1105 | } |
| 1106 | |
| 1107 | /* |
| 1108 | * Search a snapshot context to see if the given snapshot id is |
| 1109 | * present. |
| 1110 | * |
| 1111 | * Returns the position of the snapshot id in the array if it's found, |
| 1112 | * or BAD_SNAP_INDEX otherwise. |
| 1113 | * |
| 1114 | * Note: The snapshot array is in kept sorted (by the osd) in |
| 1115 | * reverse order, highest snapshot id first. |
| 1116 | */ |
Alex Elder | 9682fc6 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 1117 | static u32 rbd_dev_snap_index(struct rbd_device *rbd_dev, u64 snap_id) |
| 1118 | { |
| 1119 | struct ceph_snap_context *snapc = rbd_dev->header.snapc; |
Alex Elder | 30d1cff | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 1120 | u64 *found; |
Alex Elder | 9682fc6 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 1121 | |
Alex Elder | 30d1cff | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 1122 | found = bsearch(&snap_id, &snapc->snaps, snapc->num_snaps, |
| 1123 | sizeof (snap_id), snapid_compare_reverse); |
Alex Elder | 9682fc6 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 1124 | |
Alex Elder | 30d1cff | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 1125 | return found ? (u32)(found - &snapc->snaps[0]) : BAD_SNAP_INDEX; |
Alex Elder | 9682fc6 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 1126 | } |
| 1127 | |
Alex Elder | 2ad3d71 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 1128 | static const char *rbd_dev_v1_snap_name(struct rbd_device *rbd_dev, |
| 1129 | u64 snap_id) |
Alex Elder | 54cac61 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 1130 | { |
| 1131 | u32 which; |
Josh Durgin | da6a6b6 | 2013-09-04 17:57:31 -0700 | [diff] [blame] | 1132 | const char *snap_name; |
Alex Elder | 54cac61 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 1133 | |
| 1134 | which = rbd_dev_snap_index(rbd_dev, snap_id); |
| 1135 | if (which == BAD_SNAP_INDEX) |
Josh Durgin | da6a6b6 | 2013-09-04 17:57:31 -0700 | [diff] [blame] | 1136 | return ERR_PTR(-ENOENT); |
Alex Elder | 54cac61 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 1137 | |
Josh Durgin | da6a6b6 | 2013-09-04 17:57:31 -0700 | [diff] [blame] | 1138 | snap_name = _rbd_dev_v1_snap_name(rbd_dev, which); |
| 1139 | return snap_name ? snap_name : ERR_PTR(-ENOMEM); |
Alex Elder | 54cac61 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 1140 | } |
| 1141 | |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 1142 | static const char *rbd_snap_name(struct rbd_device *rbd_dev, u64 snap_id) |
| 1143 | { |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 1144 | if (snap_id == CEPH_NOSNAP) |
| 1145 | return RBD_SNAP_HEAD_NAME; |
| 1146 | |
Alex Elder | 54cac61 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 1147 | rbd_assert(rbd_image_format_valid(rbd_dev->image_format)); |
| 1148 | if (rbd_dev->image_format == 1) |
| 1149 | return rbd_dev_v1_snap_name(rbd_dev, snap_id); |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 1150 | |
Alex Elder | 54cac61 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 1151 | return rbd_dev_v2_snap_name(rbd_dev, snap_id); |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 1152 | } |
| 1153 | |
Alex Elder | 2ad3d71 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 1154 | static int rbd_snap_size(struct rbd_device *rbd_dev, u64 snap_id, |
| 1155 | u64 *snap_size) |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1156 | { |
Alex Elder | 2ad3d71 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 1157 | rbd_assert(rbd_image_format_valid(rbd_dev->image_format)); |
| 1158 | if (snap_id == CEPH_NOSNAP) { |
| 1159 | *snap_size = rbd_dev->header.image_size; |
| 1160 | } else if (rbd_dev->image_format == 1) { |
| 1161 | u32 which; |
Alex Elder | 00f1f36 | 2012-02-07 12:03:36 -0600 | [diff] [blame] | 1162 | |
Alex Elder | 2ad3d71 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 1163 | which = rbd_dev_snap_index(rbd_dev, snap_id); |
| 1164 | if (which == BAD_SNAP_INDEX) |
| 1165 | return -ENOENT; |
Alex Elder | 00f1f36 | 2012-02-07 12:03:36 -0600 | [diff] [blame] | 1166 | |
Alex Elder | 2ad3d71 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 1167 | *snap_size = rbd_dev->header.snap_sizes[which]; |
| 1168 | } else { |
| 1169 | u64 size = 0; |
| 1170 | int ret; |
| 1171 | |
| 1172 | ret = _rbd_dev_v2_snap_size(rbd_dev, snap_id, NULL, &size); |
| 1173 | if (ret) |
| 1174 | return ret; |
| 1175 | |
| 1176 | *snap_size = size; |
| 1177 | } |
| 1178 | return 0; |
| 1179 | } |
| 1180 | |
| 1181 | static int rbd_snap_features(struct rbd_device *rbd_dev, u64 snap_id, |
| 1182 | u64 *snap_features) |
| 1183 | { |
| 1184 | rbd_assert(rbd_image_format_valid(rbd_dev->image_format)); |
| 1185 | if (snap_id == CEPH_NOSNAP) { |
| 1186 | *snap_features = rbd_dev->header.features; |
| 1187 | } else if (rbd_dev->image_format == 1) { |
| 1188 | *snap_features = 0; /* No features for format 1 */ |
| 1189 | } else { |
| 1190 | u64 features = 0; |
| 1191 | int ret; |
| 1192 | |
| 1193 | ret = _rbd_dev_v2_snap_features(rbd_dev, snap_id, &features); |
| 1194 | if (ret) |
| 1195 | return ret; |
| 1196 | |
| 1197 | *snap_features = features; |
| 1198 | } |
| 1199 | return 0; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1200 | } |
| 1201 | |
Alex Elder | d1cf578 | 2013-04-27 09:59:30 -0500 | [diff] [blame] | 1202 | static int rbd_dev_mapping_set(struct rbd_device *rbd_dev) |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1203 | { |
Alex Elder | 8f4b7d9 | 2013-05-06 07:40:30 -0500 | [diff] [blame] | 1204 | u64 snap_id = rbd_dev->spec->snap_id; |
Alex Elder | 2ad3d71 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 1205 | u64 size = 0; |
| 1206 | u64 features = 0; |
| 1207 | int ret; |
Alex Elder | 8b0241f | 2013-04-25 23:15:08 -0500 | [diff] [blame] | 1208 | |
Alex Elder | 2ad3d71 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 1209 | ret = rbd_snap_size(rbd_dev, snap_id, &size); |
| 1210 | if (ret) |
| 1211 | return ret; |
| 1212 | ret = rbd_snap_features(rbd_dev, snap_id, &features); |
| 1213 | if (ret) |
| 1214 | return ret; |
| 1215 | |
| 1216 | rbd_dev->mapping.size = size; |
| 1217 | rbd_dev->mapping.features = features; |
| 1218 | |
Alex Elder | 8b0241f | 2013-04-25 23:15:08 -0500 | [diff] [blame] | 1219 | return 0; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1220 | } |
| 1221 | |
Alex Elder | d1cf578 | 2013-04-27 09:59:30 -0500 | [diff] [blame] | 1222 | static void rbd_dev_mapping_clear(struct rbd_device *rbd_dev) |
| 1223 | { |
| 1224 | rbd_dev->mapping.size = 0; |
| 1225 | rbd_dev->mapping.features = 0; |
Alex Elder | 200a6a8 | 2013-04-28 23:32:34 -0500 | [diff] [blame] | 1226 | } |
| 1227 | |
Himangi Saraogi | 7d5079a | 2014-07-24 03:17:07 +0530 | [diff] [blame] | 1228 | static void rbd_segment_name_free(const char *name) |
| 1229 | { |
| 1230 | /* The explicit cast here is needed to drop the const qualifier */ |
| 1231 | |
| 1232 | kmem_cache_free(rbd_segment_name_cache, (void *)name); |
| 1233 | } |
| 1234 | |
Alex Elder | 98571b5 | 2013-01-20 14:44:42 -0600 | [diff] [blame] | 1235 | static const char *rbd_segment_name(struct rbd_device *rbd_dev, u64 offset) |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1236 | { |
Alex Elder | 65ccfe2 | 2012-08-09 10:33:26 -0700 | [diff] [blame] | 1237 | char *name; |
| 1238 | u64 segment; |
| 1239 | int ret; |
Josh Durgin | 3a96d5c | 2013-06-12 19:15:06 -0700 | [diff] [blame] | 1240 | char *name_format; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1241 | |
Alex Elder | 78c2a44 | 2013-05-01 12:43:04 -0500 | [diff] [blame] | 1242 | name = kmem_cache_alloc(rbd_segment_name_cache, GFP_NOIO); |
Alex Elder | 65ccfe2 | 2012-08-09 10:33:26 -0700 | [diff] [blame] | 1243 | if (!name) |
| 1244 | return NULL; |
| 1245 | segment = offset >> rbd_dev->header.obj_order; |
Josh Durgin | 3a96d5c | 2013-06-12 19:15:06 -0700 | [diff] [blame] | 1246 | name_format = "%s.%012llx"; |
| 1247 | if (rbd_dev->image_format == 2) |
| 1248 | name_format = "%s.%016llx"; |
Ilya Dryomov | 2d0ebc5 | 2014-01-27 17:40:18 +0200 | [diff] [blame] | 1249 | ret = snprintf(name, CEPH_MAX_OID_NAME_LEN + 1, name_format, |
Alex Elder | 65ccfe2 | 2012-08-09 10:33:26 -0700 | [diff] [blame] | 1250 | rbd_dev->header.object_prefix, segment); |
Ilya Dryomov | 2d0ebc5 | 2014-01-27 17:40:18 +0200 | [diff] [blame] | 1251 | if (ret < 0 || ret > CEPH_MAX_OID_NAME_LEN) { |
Alex Elder | 65ccfe2 | 2012-08-09 10:33:26 -0700 | [diff] [blame] | 1252 | pr_err("error formatting segment name for #%llu (%d)\n", |
| 1253 | segment, ret); |
Himangi Saraogi | 7d5079a | 2014-07-24 03:17:07 +0530 | [diff] [blame] | 1254 | rbd_segment_name_free(name); |
Alex Elder | 65ccfe2 | 2012-08-09 10:33:26 -0700 | [diff] [blame] | 1255 | name = NULL; |
| 1256 | } |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1257 | |
Alex Elder | 65ccfe2 | 2012-08-09 10:33:26 -0700 | [diff] [blame] | 1258 | return name; |
| 1259 | } |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1260 | |
Alex Elder | 65ccfe2 | 2012-08-09 10:33:26 -0700 | [diff] [blame] | 1261 | static u64 rbd_segment_offset(struct rbd_device *rbd_dev, u64 offset) |
| 1262 | { |
| 1263 | u64 segment_size = (u64) 1 << rbd_dev->header.obj_order; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1264 | |
Alex Elder | 65ccfe2 | 2012-08-09 10:33:26 -0700 | [diff] [blame] | 1265 | return offset & (segment_size - 1); |
| 1266 | } |
| 1267 | |
| 1268 | static u64 rbd_segment_length(struct rbd_device *rbd_dev, |
| 1269 | u64 offset, u64 length) |
| 1270 | { |
| 1271 | u64 segment_size = (u64) 1 << rbd_dev->header.obj_order; |
| 1272 | |
| 1273 | offset &= segment_size - 1; |
| 1274 | |
Alex Elder | aafb230 | 2012-09-06 16:00:54 -0500 | [diff] [blame] | 1275 | rbd_assert(length <= U64_MAX - offset); |
Alex Elder | 65ccfe2 | 2012-08-09 10:33:26 -0700 | [diff] [blame] | 1276 | if (offset + length > segment_size) |
| 1277 | length = segment_size - offset; |
| 1278 | |
| 1279 | return length; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1280 | } |
| 1281 | |
| 1282 | /* |
Josh Durgin | 029bcbd | 2011-07-22 11:35:23 -0700 | [diff] [blame] | 1283 | * returns the size of an object in the image |
| 1284 | */ |
| 1285 | static u64 rbd_obj_bytes(struct rbd_image_header *header) |
| 1286 | { |
| 1287 | return 1 << header->obj_order; |
| 1288 | } |
| 1289 | |
| 1290 | /* |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1291 | * bio helpers |
| 1292 | */ |
| 1293 | |
| 1294 | static void bio_chain_put(struct bio *chain) |
| 1295 | { |
| 1296 | struct bio *tmp; |
| 1297 | |
| 1298 | while (chain) { |
| 1299 | tmp = chain; |
| 1300 | chain = chain->bi_next; |
| 1301 | bio_put(tmp); |
| 1302 | } |
| 1303 | } |
| 1304 | |
| 1305 | /* |
| 1306 | * zeros a bio chain, starting at specific offset |
| 1307 | */ |
| 1308 | static void zero_bio_chain(struct bio *chain, int start_ofs) |
| 1309 | { |
Kent Overstreet | 7988613 | 2013-11-23 17:19:00 -0800 | [diff] [blame] | 1310 | struct bio_vec bv; |
| 1311 | struct bvec_iter iter; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1312 | unsigned long flags; |
| 1313 | void *buf; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1314 | int pos = 0; |
| 1315 | |
| 1316 | while (chain) { |
Kent Overstreet | 7988613 | 2013-11-23 17:19:00 -0800 | [diff] [blame] | 1317 | bio_for_each_segment(bv, chain, iter) { |
| 1318 | if (pos + bv.bv_len > start_ofs) { |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1319 | int remainder = max(start_ofs - pos, 0); |
Kent Overstreet | 7988613 | 2013-11-23 17:19:00 -0800 | [diff] [blame] | 1320 | buf = bvec_kmap_irq(&bv, &flags); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1321 | memset(buf + remainder, 0, |
Kent Overstreet | 7988613 | 2013-11-23 17:19:00 -0800 | [diff] [blame] | 1322 | bv.bv_len - remainder); |
| 1323 | flush_dcache_page(bv.bv_page); |
Dan Carpenter | 85b5aaa | 2010-10-11 21:15:11 +0200 | [diff] [blame] | 1324 | bvec_kunmap_irq(buf, &flags); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1325 | } |
Kent Overstreet | 7988613 | 2013-11-23 17:19:00 -0800 | [diff] [blame] | 1326 | pos += bv.bv_len; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1327 | } |
| 1328 | |
| 1329 | chain = chain->bi_next; |
| 1330 | } |
| 1331 | } |
| 1332 | |
| 1333 | /* |
Alex Elder | b9434c5 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 1334 | * similar to zero_bio_chain(), zeros data defined by a page array, |
| 1335 | * starting at the given byte offset from the start of the array and |
| 1336 | * continuing up to the given end offset. The pages array is |
| 1337 | * assumed to be big enough to hold all bytes up to the end. |
| 1338 | */ |
| 1339 | static void zero_pages(struct page **pages, u64 offset, u64 end) |
| 1340 | { |
| 1341 | struct page **page = &pages[offset >> PAGE_SHIFT]; |
| 1342 | |
| 1343 | rbd_assert(end > offset); |
| 1344 | rbd_assert(end - offset <= (u64)SIZE_MAX); |
| 1345 | while (offset < end) { |
| 1346 | size_t page_offset; |
| 1347 | size_t length; |
| 1348 | unsigned long flags; |
| 1349 | void *kaddr; |
| 1350 | |
Geert Uytterhoeven | 491205a | 2013-05-13 20:35:37 -0500 | [diff] [blame] | 1351 | page_offset = offset & ~PAGE_MASK; |
| 1352 | length = min_t(size_t, PAGE_SIZE - page_offset, end - offset); |
Alex Elder | b9434c5 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 1353 | local_irq_save(flags); |
| 1354 | kaddr = kmap_atomic(*page); |
| 1355 | memset(kaddr + page_offset, 0, length); |
Alex Elder | e215605 | 2013-05-22 20:54:25 -0500 | [diff] [blame] | 1356 | flush_dcache_page(*page); |
Alex Elder | b9434c5 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 1357 | kunmap_atomic(kaddr); |
| 1358 | local_irq_restore(flags); |
| 1359 | |
| 1360 | offset += length; |
| 1361 | page++; |
| 1362 | } |
| 1363 | } |
| 1364 | |
| 1365 | /* |
Alex Elder | f7760da | 2012-10-20 22:17:27 -0500 | [diff] [blame] | 1366 | * Clone a portion of a bio, starting at the given byte offset |
| 1367 | * and continuing for the number of bytes indicated. |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1368 | */ |
Alex Elder | f7760da | 2012-10-20 22:17:27 -0500 | [diff] [blame] | 1369 | static struct bio *bio_clone_range(struct bio *bio_src, |
| 1370 | unsigned int offset, |
| 1371 | unsigned int len, |
| 1372 | gfp_t gfpmask) |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1373 | { |
Alex Elder | f7760da | 2012-10-20 22:17:27 -0500 | [diff] [blame] | 1374 | struct bio *bio; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1375 | |
Kent Overstreet | 5341a62 | 2013-08-07 14:31:11 -0700 | [diff] [blame] | 1376 | bio = bio_clone(bio_src, gfpmask); |
Alex Elder | f7760da | 2012-10-20 22:17:27 -0500 | [diff] [blame] | 1377 | if (!bio) |
| 1378 | return NULL; /* ENOMEM */ |
| 1379 | |
Kent Overstreet | 5341a62 | 2013-08-07 14:31:11 -0700 | [diff] [blame] | 1380 | bio_advance(bio, offset); |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 1381 | bio->bi_iter.bi_size = len; |
Alex Elder | 542582f | 2012-08-09 10:33:25 -0700 | [diff] [blame] | 1382 | |
Alex Elder | f7760da | 2012-10-20 22:17:27 -0500 | [diff] [blame] | 1383 | return bio; |
| 1384 | } |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1385 | |
Alex Elder | f7760da | 2012-10-20 22:17:27 -0500 | [diff] [blame] | 1386 | /* |
| 1387 | * Clone a portion of a bio chain, starting at the given byte offset |
| 1388 | * into the first bio in the source chain and continuing for the |
| 1389 | * number of bytes indicated. The result is another bio chain of |
| 1390 | * exactly the given length, or a null pointer on error. |
| 1391 | * |
| 1392 | * The bio_src and offset parameters are both in-out. On entry they |
| 1393 | * refer to the first source bio and the offset into that bio where |
| 1394 | * the start of data to be cloned is located. |
| 1395 | * |
| 1396 | * On return, bio_src is updated to refer to the bio in the source |
| 1397 | * chain that contains first un-cloned byte, and *offset will |
| 1398 | * contain the offset of that byte within that bio. |
| 1399 | */ |
| 1400 | static struct bio *bio_chain_clone_range(struct bio **bio_src, |
| 1401 | unsigned int *offset, |
| 1402 | unsigned int len, |
| 1403 | gfp_t gfpmask) |
| 1404 | { |
| 1405 | struct bio *bi = *bio_src; |
| 1406 | unsigned int off = *offset; |
| 1407 | struct bio *chain = NULL; |
| 1408 | struct bio **end; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1409 | |
Alex Elder | f7760da | 2012-10-20 22:17:27 -0500 | [diff] [blame] | 1410 | /* Build up a chain of clone bios up to the limit */ |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1411 | |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 1412 | if (!bi || off >= bi->bi_iter.bi_size || !len) |
Alex Elder | f7760da | 2012-10-20 22:17:27 -0500 | [diff] [blame] | 1413 | return NULL; /* Nothing to clone */ |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1414 | |
Alex Elder | f7760da | 2012-10-20 22:17:27 -0500 | [diff] [blame] | 1415 | end = &chain; |
| 1416 | while (len) { |
| 1417 | unsigned int bi_size; |
| 1418 | struct bio *bio; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1419 | |
Alex Elder | f5400b7 | 2012-11-01 10:17:15 -0500 | [diff] [blame] | 1420 | if (!bi) { |
| 1421 | rbd_warn(NULL, "bio_chain exhausted with %u left", len); |
Alex Elder | f7760da | 2012-10-20 22:17:27 -0500 | [diff] [blame] | 1422 | goto out_err; /* EINVAL; ran out of bio's */ |
Alex Elder | f5400b7 | 2012-11-01 10:17:15 -0500 | [diff] [blame] | 1423 | } |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 1424 | bi_size = min_t(unsigned int, bi->bi_iter.bi_size - off, len); |
Alex Elder | f7760da | 2012-10-20 22:17:27 -0500 | [diff] [blame] | 1425 | bio = bio_clone_range(bi, off, bi_size, gfpmask); |
| 1426 | if (!bio) |
| 1427 | goto out_err; /* ENOMEM */ |
| 1428 | |
| 1429 | *end = bio; |
| 1430 | end = &bio->bi_next; |
| 1431 | |
| 1432 | off += bi_size; |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 1433 | if (off == bi->bi_iter.bi_size) { |
Alex Elder | f7760da | 2012-10-20 22:17:27 -0500 | [diff] [blame] | 1434 | bi = bi->bi_next; |
| 1435 | off = 0; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1436 | } |
Alex Elder | f7760da | 2012-10-20 22:17:27 -0500 | [diff] [blame] | 1437 | len -= bi_size; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1438 | } |
Alex Elder | f7760da | 2012-10-20 22:17:27 -0500 | [diff] [blame] | 1439 | *bio_src = bi; |
| 1440 | *offset = off; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1441 | |
Alex Elder | f7760da | 2012-10-20 22:17:27 -0500 | [diff] [blame] | 1442 | return chain; |
| 1443 | out_err: |
| 1444 | bio_chain_put(chain); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1445 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1446 | return NULL; |
| 1447 | } |
| 1448 | |
Alex Elder | 926f9b3 | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 1449 | /* |
| 1450 | * The default/initial value for all object request flags is 0. For |
| 1451 | * each flag, once its value is set to 1 it is never reset to 0 |
| 1452 | * again. |
| 1453 | */ |
Alex Elder | 6365d33 | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 1454 | static void obj_request_img_data_set(struct rbd_obj_request *obj_request) |
| 1455 | { |
| 1456 | if (test_and_set_bit(OBJ_REQ_IMG_DATA, &obj_request->flags)) { |
Alex Elder | 6365d33 | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 1457 | struct rbd_device *rbd_dev; |
| 1458 | |
Alex Elder | 57acbaa | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 1459 | rbd_dev = obj_request->img_request->rbd_dev; |
Ilya Dryomov | 9584d50 | 2014-07-11 12:11:20 +0400 | [diff] [blame] | 1460 | rbd_warn(rbd_dev, "obj_request %p already marked img_data", |
Alex Elder | 6365d33 | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 1461 | obj_request); |
| 1462 | } |
| 1463 | } |
| 1464 | |
| 1465 | static bool obj_request_img_data_test(struct rbd_obj_request *obj_request) |
| 1466 | { |
| 1467 | smp_mb(); |
| 1468 | return test_bit(OBJ_REQ_IMG_DATA, &obj_request->flags) != 0; |
| 1469 | } |
| 1470 | |
Alex Elder | 57acbaa | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 1471 | static void obj_request_done_set(struct rbd_obj_request *obj_request) |
| 1472 | { |
| 1473 | if (test_and_set_bit(OBJ_REQ_DONE, &obj_request->flags)) { |
| 1474 | struct rbd_device *rbd_dev = NULL; |
| 1475 | |
| 1476 | if (obj_request_img_data_test(obj_request)) |
| 1477 | rbd_dev = obj_request->img_request->rbd_dev; |
Ilya Dryomov | 9584d50 | 2014-07-11 12:11:20 +0400 | [diff] [blame] | 1478 | rbd_warn(rbd_dev, "obj_request %p already marked done", |
Alex Elder | 57acbaa | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 1479 | obj_request); |
| 1480 | } |
| 1481 | } |
| 1482 | |
| 1483 | static bool obj_request_done_test(struct rbd_obj_request *obj_request) |
| 1484 | { |
| 1485 | smp_mb(); |
| 1486 | return test_bit(OBJ_REQ_DONE, &obj_request->flags) != 0; |
| 1487 | } |
| 1488 | |
Alex Elder | 5679c59 | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 1489 | /* |
| 1490 | * This sets the KNOWN flag after (possibly) setting the EXISTS |
| 1491 | * flag. The latter is set based on the "exists" value provided. |
| 1492 | * |
| 1493 | * Note that for our purposes once an object exists it never goes |
| 1494 | * away again. It's possible that the response from two existence |
| 1495 | * checks are separated by the creation of the target object, and |
| 1496 | * the first ("doesn't exist") response arrives *after* the second |
| 1497 | * ("does exist"). In that case we ignore the second one. |
| 1498 | */ |
| 1499 | static void obj_request_existence_set(struct rbd_obj_request *obj_request, |
| 1500 | bool exists) |
| 1501 | { |
| 1502 | if (exists) |
| 1503 | set_bit(OBJ_REQ_EXISTS, &obj_request->flags); |
| 1504 | set_bit(OBJ_REQ_KNOWN, &obj_request->flags); |
| 1505 | smp_mb(); |
| 1506 | } |
| 1507 | |
| 1508 | static bool obj_request_known_test(struct rbd_obj_request *obj_request) |
| 1509 | { |
| 1510 | smp_mb(); |
| 1511 | return test_bit(OBJ_REQ_KNOWN, &obj_request->flags) != 0; |
| 1512 | } |
| 1513 | |
| 1514 | static bool obj_request_exists_test(struct rbd_obj_request *obj_request) |
| 1515 | { |
| 1516 | smp_mb(); |
| 1517 | return test_bit(OBJ_REQ_EXISTS, &obj_request->flags) != 0; |
| 1518 | } |
| 1519 | |
Ilya Dryomov | 9638556 | 2014-06-10 13:53:29 +0400 | [diff] [blame] | 1520 | static bool obj_request_overlaps_parent(struct rbd_obj_request *obj_request) |
| 1521 | { |
| 1522 | struct rbd_device *rbd_dev = obj_request->img_request->rbd_dev; |
| 1523 | |
| 1524 | return obj_request->img_offset < |
| 1525 | round_up(rbd_dev->parent_overlap, rbd_obj_bytes(&rbd_dev->header)); |
| 1526 | } |
| 1527 | |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1528 | static void rbd_obj_request_get(struct rbd_obj_request *obj_request) |
| 1529 | { |
Alex Elder | 37206ee | 2013-02-20 17:32:08 -0600 | [diff] [blame] | 1530 | dout("%s: obj %p (was %d)\n", __func__, obj_request, |
| 1531 | atomic_read(&obj_request->kref.refcount)); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1532 | kref_get(&obj_request->kref); |
| 1533 | } |
| 1534 | |
| 1535 | static void rbd_obj_request_destroy(struct kref *kref); |
| 1536 | static void rbd_obj_request_put(struct rbd_obj_request *obj_request) |
| 1537 | { |
| 1538 | rbd_assert(obj_request != NULL); |
Alex Elder | 37206ee | 2013-02-20 17:32:08 -0600 | [diff] [blame] | 1539 | dout("%s: obj %p (was %d)\n", __func__, obj_request, |
| 1540 | atomic_read(&obj_request->kref.refcount)); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1541 | kref_put(&obj_request->kref, rbd_obj_request_destroy); |
| 1542 | } |
| 1543 | |
Alex Elder | 0f2d5be | 2014-04-26 14:21:44 +0400 | [diff] [blame] | 1544 | static void rbd_img_request_get(struct rbd_img_request *img_request) |
| 1545 | { |
| 1546 | dout("%s: img %p (was %d)\n", __func__, img_request, |
| 1547 | atomic_read(&img_request->kref.refcount)); |
| 1548 | kref_get(&img_request->kref); |
| 1549 | } |
| 1550 | |
Alex Elder | e93f315 | 2013-05-08 22:50:04 -0500 | [diff] [blame] | 1551 | static bool img_request_child_test(struct rbd_img_request *img_request); |
| 1552 | static void rbd_parent_request_destroy(struct kref *kref); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1553 | static void rbd_img_request_destroy(struct kref *kref); |
| 1554 | static void rbd_img_request_put(struct rbd_img_request *img_request) |
| 1555 | { |
| 1556 | rbd_assert(img_request != NULL); |
Alex Elder | 37206ee | 2013-02-20 17:32:08 -0600 | [diff] [blame] | 1557 | dout("%s: img %p (was %d)\n", __func__, img_request, |
| 1558 | atomic_read(&img_request->kref.refcount)); |
Alex Elder | e93f315 | 2013-05-08 22:50:04 -0500 | [diff] [blame] | 1559 | if (img_request_child_test(img_request)) |
| 1560 | kref_put(&img_request->kref, rbd_parent_request_destroy); |
| 1561 | else |
| 1562 | kref_put(&img_request->kref, rbd_img_request_destroy); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1563 | } |
| 1564 | |
| 1565 | static inline void rbd_img_obj_request_add(struct rbd_img_request *img_request, |
| 1566 | struct rbd_obj_request *obj_request) |
| 1567 | { |
Alex Elder | 25dcf95 | 2013-01-25 17:08:55 -0600 | [diff] [blame] | 1568 | rbd_assert(obj_request->img_request == NULL); |
| 1569 | |
Alex Elder | b155e86 | 2013-04-15 14:50:37 -0500 | [diff] [blame] | 1570 | /* Image request now owns object's original reference */ |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1571 | obj_request->img_request = img_request; |
Alex Elder | 25dcf95 | 2013-01-25 17:08:55 -0600 | [diff] [blame] | 1572 | obj_request->which = img_request->obj_request_count; |
Alex Elder | 6365d33 | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 1573 | rbd_assert(!obj_request_img_data_test(obj_request)); |
| 1574 | obj_request_img_data_set(obj_request); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1575 | rbd_assert(obj_request->which != BAD_WHICH); |
Alex Elder | 25dcf95 | 2013-01-25 17:08:55 -0600 | [diff] [blame] | 1576 | img_request->obj_request_count++; |
| 1577 | list_add_tail(&obj_request->links, &img_request->obj_requests); |
Alex Elder | 37206ee | 2013-02-20 17:32:08 -0600 | [diff] [blame] | 1578 | dout("%s: img %p obj %p w=%u\n", __func__, img_request, obj_request, |
| 1579 | obj_request->which); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1580 | } |
| 1581 | |
| 1582 | static inline void rbd_img_obj_request_del(struct rbd_img_request *img_request, |
| 1583 | struct rbd_obj_request *obj_request) |
| 1584 | { |
| 1585 | rbd_assert(obj_request->which != BAD_WHICH); |
Alex Elder | 25dcf95 | 2013-01-25 17:08:55 -0600 | [diff] [blame] | 1586 | |
Alex Elder | 37206ee | 2013-02-20 17:32:08 -0600 | [diff] [blame] | 1587 | dout("%s: img %p obj %p w=%u\n", __func__, img_request, obj_request, |
| 1588 | obj_request->which); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1589 | list_del(&obj_request->links); |
Alex Elder | 25dcf95 | 2013-01-25 17:08:55 -0600 | [diff] [blame] | 1590 | rbd_assert(img_request->obj_request_count > 0); |
| 1591 | img_request->obj_request_count--; |
| 1592 | rbd_assert(obj_request->which == img_request->obj_request_count); |
| 1593 | obj_request->which = BAD_WHICH; |
Alex Elder | 6365d33 | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 1594 | rbd_assert(obj_request_img_data_test(obj_request)); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1595 | rbd_assert(obj_request->img_request == img_request); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1596 | obj_request->img_request = NULL; |
Alex Elder | 25dcf95 | 2013-01-25 17:08:55 -0600 | [diff] [blame] | 1597 | obj_request->callback = NULL; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1598 | rbd_obj_request_put(obj_request); |
| 1599 | } |
| 1600 | |
| 1601 | static bool obj_request_type_valid(enum obj_request_type type) |
| 1602 | { |
| 1603 | switch (type) { |
Alex Elder | 9969ebc | 2013-01-18 12:31:10 -0600 | [diff] [blame] | 1604 | case OBJ_REQUEST_NODATA: |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1605 | case OBJ_REQUEST_BIO: |
Alex Elder | 788e2df | 2013-01-17 12:25:27 -0600 | [diff] [blame] | 1606 | case OBJ_REQUEST_PAGES: |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1607 | return true; |
| 1608 | default: |
| 1609 | return false; |
| 1610 | } |
| 1611 | } |
| 1612 | |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1613 | static int rbd_obj_request_submit(struct ceph_osd_client *osdc, |
| 1614 | struct rbd_obj_request *obj_request) |
| 1615 | { |
Ilya Dryomov | 71c20a0 | 2014-06-19 11:38:14 +0400 | [diff] [blame] | 1616 | dout("%s %p\n", __func__, obj_request); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1617 | return ceph_osdc_start_request(osdc, obj_request->osd_req, false); |
| 1618 | } |
| 1619 | |
Ilya Dryomov | 71c20a0 | 2014-06-19 11:38:14 +0400 | [diff] [blame] | 1620 | static void rbd_obj_request_end(struct rbd_obj_request *obj_request) |
| 1621 | { |
| 1622 | dout("%s %p\n", __func__, obj_request); |
| 1623 | ceph_osdc_cancel_request(obj_request->osd_req); |
| 1624 | } |
| 1625 | |
| 1626 | /* |
| 1627 | * Wait for an object request to complete. If interrupted, cancel the |
| 1628 | * underlying osd request. |
Ilya Dryomov | 2894e1d | 2015-05-12 19:53:24 +0300 | [diff] [blame] | 1629 | * |
| 1630 | * @timeout: in jiffies, 0 means "wait forever" |
Ilya Dryomov | 71c20a0 | 2014-06-19 11:38:14 +0400 | [diff] [blame] | 1631 | */ |
Ilya Dryomov | 2894e1d | 2015-05-12 19:53:24 +0300 | [diff] [blame] | 1632 | static int __rbd_obj_request_wait(struct rbd_obj_request *obj_request, |
| 1633 | unsigned long timeout) |
Ilya Dryomov | 71c20a0 | 2014-06-19 11:38:14 +0400 | [diff] [blame] | 1634 | { |
Ilya Dryomov | 2894e1d | 2015-05-12 19:53:24 +0300 | [diff] [blame] | 1635 | long ret; |
Ilya Dryomov | 71c20a0 | 2014-06-19 11:38:14 +0400 | [diff] [blame] | 1636 | |
| 1637 | dout("%s %p\n", __func__, obj_request); |
Ilya Dryomov | 2894e1d | 2015-05-12 19:53:24 +0300 | [diff] [blame] | 1638 | ret = wait_for_completion_interruptible_timeout( |
| 1639 | &obj_request->completion, |
| 1640 | ceph_timeout_jiffies(timeout)); |
| 1641 | if (ret <= 0) { |
| 1642 | if (ret == 0) |
| 1643 | ret = -ETIMEDOUT; |
Ilya Dryomov | 71c20a0 | 2014-06-19 11:38:14 +0400 | [diff] [blame] | 1644 | rbd_obj_request_end(obj_request); |
Ilya Dryomov | 2894e1d | 2015-05-12 19:53:24 +0300 | [diff] [blame] | 1645 | } else { |
| 1646 | ret = 0; |
Ilya Dryomov | 71c20a0 | 2014-06-19 11:38:14 +0400 | [diff] [blame] | 1647 | } |
| 1648 | |
Ilya Dryomov | 2894e1d | 2015-05-12 19:53:24 +0300 | [diff] [blame] | 1649 | dout("%s %p ret %d\n", __func__, obj_request, (int)ret); |
| 1650 | return ret; |
| 1651 | } |
| 1652 | |
| 1653 | static int rbd_obj_request_wait(struct rbd_obj_request *obj_request) |
| 1654 | { |
| 1655 | return __rbd_obj_request_wait(obj_request, 0); |
| 1656 | } |
| 1657 | |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1658 | static void rbd_img_request_complete(struct rbd_img_request *img_request) |
| 1659 | { |
Alex Elder | 55f27e0 | 2013-04-10 12:34:25 -0500 | [diff] [blame] | 1660 | |
Alex Elder | 37206ee | 2013-02-20 17:32:08 -0600 | [diff] [blame] | 1661 | dout("%s: img %p\n", __func__, img_request); |
Alex Elder | 55f27e0 | 2013-04-10 12:34:25 -0500 | [diff] [blame] | 1662 | |
| 1663 | /* |
| 1664 | * If no error occurred, compute the aggregate transfer |
| 1665 | * count for the image request. We could instead use |
| 1666 | * atomic64_cmpxchg() to update it as each object request |
| 1667 | * completes; not clear which way is better off hand. |
| 1668 | */ |
| 1669 | if (!img_request->result) { |
| 1670 | struct rbd_obj_request *obj_request; |
| 1671 | u64 xferred = 0; |
| 1672 | |
| 1673 | for_each_obj_request(img_request, obj_request) |
| 1674 | xferred += obj_request->xferred; |
| 1675 | img_request->xferred = xferred; |
| 1676 | } |
| 1677 | |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1678 | if (img_request->callback) |
| 1679 | img_request->callback(img_request); |
| 1680 | else |
| 1681 | rbd_img_request_put(img_request); |
| 1682 | } |
| 1683 | |
Alex Elder | 0c42524 | 2013-02-08 09:55:49 -0600 | [diff] [blame] | 1684 | /* |
| 1685 | * The default/initial value for all image request flags is 0. Each |
| 1686 | * is conditionally set to 1 at image request initialization time |
| 1687 | * and currently never change thereafter. |
| 1688 | */ |
| 1689 | static void img_request_write_set(struct rbd_img_request *img_request) |
| 1690 | { |
| 1691 | set_bit(IMG_REQ_WRITE, &img_request->flags); |
| 1692 | smp_mb(); |
| 1693 | } |
| 1694 | |
| 1695 | static bool img_request_write_test(struct rbd_img_request *img_request) |
| 1696 | { |
| 1697 | smp_mb(); |
| 1698 | return test_bit(IMG_REQ_WRITE, &img_request->flags) != 0; |
| 1699 | } |
| 1700 | |
Guangliang Zhao | 90e98c5 | 2014-04-01 22:22:16 +0800 | [diff] [blame] | 1701 | /* |
| 1702 | * Set the discard flag when the img_request is an discard request |
| 1703 | */ |
| 1704 | static void img_request_discard_set(struct rbd_img_request *img_request) |
| 1705 | { |
| 1706 | set_bit(IMG_REQ_DISCARD, &img_request->flags); |
| 1707 | smp_mb(); |
| 1708 | } |
| 1709 | |
| 1710 | static bool img_request_discard_test(struct rbd_img_request *img_request) |
| 1711 | { |
| 1712 | smp_mb(); |
| 1713 | return test_bit(IMG_REQ_DISCARD, &img_request->flags) != 0; |
| 1714 | } |
| 1715 | |
Alex Elder | 9849e98 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 1716 | static void img_request_child_set(struct rbd_img_request *img_request) |
| 1717 | { |
| 1718 | set_bit(IMG_REQ_CHILD, &img_request->flags); |
| 1719 | smp_mb(); |
| 1720 | } |
| 1721 | |
Alex Elder | e93f315 | 2013-05-08 22:50:04 -0500 | [diff] [blame] | 1722 | static void img_request_child_clear(struct rbd_img_request *img_request) |
| 1723 | { |
| 1724 | clear_bit(IMG_REQ_CHILD, &img_request->flags); |
| 1725 | smp_mb(); |
| 1726 | } |
| 1727 | |
Alex Elder | 9849e98 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 1728 | static bool img_request_child_test(struct rbd_img_request *img_request) |
| 1729 | { |
| 1730 | smp_mb(); |
| 1731 | return test_bit(IMG_REQ_CHILD, &img_request->flags) != 0; |
| 1732 | } |
| 1733 | |
Alex Elder | d0b2e94 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 1734 | static void img_request_layered_set(struct rbd_img_request *img_request) |
| 1735 | { |
| 1736 | set_bit(IMG_REQ_LAYERED, &img_request->flags); |
| 1737 | smp_mb(); |
| 1738 | } |
| 1739 | |
Alex Elder | a2acd00 | 2013-05-08 22:50:04 -0500 | [diff] [blame] | 1740 | static void img_request_layered_clear(struct rbd_img_request *img_request) |
| 1741 | { |
| 1742 | clear_bit(IMG_REQ_LAYERED, &img_request->flags); |
| 1743 | smp_mb(); |
| 1744 | } |
| 1745 | |
Alex Elder | d0b2e94 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 1746 | static bool img_request_layered_test(struct rbd_img_request *img_request) |
| 1747 | { |
| 1748 | smp_mb(); |
| 1749 | return test_bit(IMG_REQ_LAYERED, &img_request->flags) != 0; |
| 1750 | } |
| 1751 | |
Josh Durgin | 3b434a2a | 2014-04-04 17:32:15 -0700 | [diff] [blame] | 1752 | static enum obj_operation_type |
| 1753 | rbd_img_request_op_type(struct rbd_img_request *img_request) |
| 1754 | { |
| 1755 | if (img_request_write_test(img_request)) |
| 1756 | return OBJ_OP_WRITE; |
| 1757 | else if (img_request_discard_test(img_request)) |
| 1758 | return OBJ_OP_DISCARD; |
| 1759 | else |
| 1760 | return OBJ_OP_READ; |
| 1761 | } |
| 1762 | |
Alex Elder | 6e2a450 | 2013-03-27 09:16:30 -0500 | [diff] [blame] | 1763 | static void |
| 1764 | rbd_img_obj_request_read_callback(struct rbd_obj_request *obj_request) |
| 1765 | { |
Alex Elder | b9434c5 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 1766 | u64 xferred = obj_request->xferred; |
| 1767 | u64 length = obj_request->length; |
| 1768 | |
Alex Elder | 6e2a450 | 2013-03-27 09:16:30 -0500 | [diff] [blame] | 1769 | dout("%s: obj %p img %p result %d %llu/%llu\n", __func__, |
| 1770 | obj_request, obj_request->img_request, obj_request->result, |
Alex Elder | b9434c5 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 1771 | xferred, length); |
Alex Elder | 6e2a450 | 2013-03-27 09:16:30 -0500 | [diff] [blame] | 1772 | /* |
Josh Durgin | 17c1cc1 | 2013-08-26 17:55:38 -0700 | [diff] [blame] | 1773 | * ENOENT means a hole in the image. We zero-fill the entire |
| 1774 | * length of the request. A short read also implies zero-fill |
| 1775 | * to the end of the request. An error requires the whole |
| 1776 | * length of the request to be reported finished with an error |
| 1777 | * to the block layer. In each case we update the xferred |
| 1778 | * count to indicate the whole request was satisfied. |
Alex Elder | 6e2a450 | 2013-03-27 09:16:30 -0500 | [diff] [blame] | 1779 | */ |
Alex Elder | b9434c5 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 1780 | rbd_assert(obj_request->type != OBJ_REQUEST_NODATA); |
Alex Elder | 6e2a450 | 2013-03-27 09:16:30 -0500 | [diff] [blame] | 1781 | if (obj_request->result == -ENOENT) { |
Alex Elder | b9434c5 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 1782 | if (obj_request->type == OBJ_REQUEST_BIO) |
| 1783 | zero_bio_chain(obj_request->bio_list, 0); |
| 1784 | else |
| 1785 | zero_pages(obj_request->pages, 0, length); |
Alex Elder | 6e2a450 | 2013-03-27 09:16:30 -0500 | [diff] [blame] | 1786 | obj_request->result = 0; |
Alex Elder | b9434c5 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 1787 | } else if (xferred < length && !obj_request->result) { |
| 1788 | if (obj_request->type == OBJ_REQUEST_BIO) |
| 1789 | zero_bio_chain(obj_request->bio_list, xferred); |
| 1790 | else |
| 1791 | zero_pages(obj_request->pages, xferred, length); |
Alex Elder | 6e2a450 | 2013-03-27 09:16:30 -0500 | [diff] [blame] | 1792 | } |
Josh Durgin | 17c1cc1 | 2013-08-26 17:55:38 -0700 | [diff] [blame] | 1793 | obj_request->xferred = length; |
Alex Elder | 6e2a450 | 2013-03-27 09:16:30 -0500 | [diff] [blame] | 1794 | obj_request_done_set(obj_request); |
| 1795 | } |
| 1796 | |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1797 | static void rbd_obj_request_complete(struct rbd_obj_request *obj_request) |
| 1798 | { |
Alex Elder | 37206ee | 2013-02-20 17:32:08 -0600 | [diff] [blame] | 1799 | dout("%s: obj %p cb %p\n", __func__, obj_request, |
| 1800 | obj_request->callback); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1801 | if (obj_request->callback) |
| 1802 | obj_request->callback(obj_request); |
Alex Elder | 788e2df | 2013-01-17 12:25:27 -0600 | [diff] [blame] | 1803 | else |
| 1804 | complete_all(&obj_request->completion); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1805 | } |
| 1806 | |
Alex Elder | c47f937 | 2013-02-26 14:23:07 -0600 | [diff] [blame] | 1807 | static void rbd_osd_read_callback(struct rbd_obj_request *obj_request) |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1808 | { |
Alex Elder | 57acbaa | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 1809 | struct rbd_img_request *img_request = NULL; |
Alex Elder | a9e8ba2c | 2013-04-21 00:32:07 -0500 | [diff] [blame] | 1810 | struct rbd_device *rbd_dev = NULL; |
Alex Elder | 57acbaa | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 1811 | bool layered = false; |
| 1812 | |
| 1813 | if (obj_request_img_data_test(obj_request)) { |
| 1814 | img_request = obj_request->img_request; |
| 1815 | layered = img_request && img_request_layered_test(img_request); |
Alex Elder | a9e8ba2c | 2013-04-21 00:32:07 -0500 | [diff] [blame] | 1816 | rbd_dev = img_request->rbd_dev; |
Alex Elder | 57acbaa | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 1817 | } |
Alex Elder | 8b3e1a5 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 1818 | |
| 1819 | dout("%s: obj %p img %p result %d %llu/%llu\n", __func__, |
| 1820 | obj_request, img_request, obj_request->result, |
| 1821 | obj_request->xferred, obj_request->length); |
Alex Elder | a9e8ba2c | 2013-04-21 00:32:07 -0500 | [diff] [blame] | 1822 | if (layered && obj_request->result == -ENOENT && |
| 1823 | obj_request->img_offset < rbd_dev->parent_overlap) |
Alex Elder | 8b3e1a5 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 1824 | rbd_img_parent_read(obj_request); |
| 1825 | else if (img_request) |
Alex Elder | 6e2a450 | 2013-03-27 09:16:30 -0500 | [diff] [blame] | 1826 | rbd_img_obj_request_read_callback(obj_request); |
| 1827 | else |
| 1828 | obj_request_done_set(obj_request); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1829 | } |
| 1830 | |
Alex Elder | c47f937 | 2013-02-26 14:23:07 -0600 | [diff] [blame] | 1831 | static void rbd_osd_write_callback(struct rbd_obj_request *obj_request) |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1832 | { |
Sage Weil | 1b83bef | 2013-02-25 16:11:12 -0800 | [diff] [blame] | 1833 | dout("%s: obj %p result %d %llu\n", __func__, obj_request, |
| 1834 | obj_request->result, obj_request->length); |
| 1835 | /* |
Alex Elder | 8b3e1a5 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 1836 | * There is no such thing as a successful short write. Set |
| 1837 | * it to our originally-requested length. |
Sage Weil | 1b83bef | 2013-02-25 16:11:12 -0800 | [diff] [blame] | 1838 | */ |
| 1839 | obj_request->xferred = obj_request->length; |
Alex Elder | 0774130 | 2013-02-05 23:41:50 -0600 | [diff] [blame] | 1840 | obj_request_done_set(obj_request); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1841 | } |
| 1842 | |
Guangliang Zhao | 90e98c5 | 2014-04-01 22:22:16 +0800 | [diff] [blame] | 1843 | static void rbd_osd_discard_callback(struct rbd_obj_request *obj_request) |
| 1844 | { |
| 1845 | dout("%s: obj %p result %d %llu\n", __func__, obj_request, |
| 1846 | obj_request->result, obj_request->length); |
| 1847 | /* |
| 1848 | * There is no such thing as a successful short discard. Set |
| 1849 | * it to our originally-requested length. |
| 1850 | */ |
| 1851 | obj_request->xferred = obj_request->length; |
Josh Durgin | d0265de | 2014-04-07 16:54:10 -0700 | [diff] [blame] | 1852 | /* discarding a non-existent object is not a problem */ |
| 1853 | if (obj_request->result == -ENOENT) |
| 1854 | obj_request->result = 0; |
Guangliang Zhao | 90e98c5 | 2014-04-01 22:22:16 +0800 | [diff] [blame] | 1855 | obj_request_done_set(obj_request); |
| 1856 | } |
| 1857 | |
Alex Elder | fbfab53 | 2013-02-08 09:55:48 -0600 | [diff] [blame] | 1858 | /* |
| 1859 | * For a simple stat call there's nothing to do. We'll do more if |
| 1860 | * this is part of a write sequence for a layered image. |
| 1861 | */ |
Alex Elder | c47f937 | 2013-02-26 14:23:07 -0600 | [diff] [blame] | 1862 | static void rbd_osd_stat_callback(struct rbd_obj_request *obj_request) |
Alex Elder | fbfab53 | 2013-02-08 09:55:48 -0600 | [diff] [blame] | 1863 | { |
Alex Elder | 37206ee | 2013-02-20 17:32:08 -0600 | [diff] [blame] | 1864 | dout("%s: obj %p\n", __func__, obj_request); |
Alex Elder | fbfab53 | 2013-02-08 09:55:48 -0600 | [diff] [blame] | 1865 | obj_request_done_set(obj_request); |
| 1866 | } |
| 1867 | |
Ilya Dryomov | 2761713 | 2015-07-16 17:36:11 +0300 | [diff] [blame] | 1868 | static void rbd_osd_call_callback(struct rbd_obj_request *obj_request) |
| 1869 | { |
| 1870 | dout("%s: obj %p\n", __func__, obj_request); |
| 1871 | |
| 1872 | if (obj_request_img_data_test(obj_request)) |
| 1873 | rbd_osd_copyup_callback(obj_request); |
| 1874 | else |
| 1875 | obj_request_done_set(obj_request); |
| 1876 | } |
| 1877 | |
Ilya Dryomov | 85e084f | 2016-04-28 16:07:24 +0200 | [diff] [blame] | 1878 | static void rbd_osd_req_callback(struct ceph_osd_request *osd_req) |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1879 | { |
| 1880 | struct rbd_obj_request *obj_request = osd_req->r_priv; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1881 | u16 opcode; |
| 1882 | |
Ilya Dryomov | 85e084f | 2016-04-28 16:07:24 +0200 | [diff] [blame] | 1883 | dout("%s: osd_req %p\n", __func__, osd_req); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1884 | rbd_assert(osd_req == obj_request->osd_req); |
Alex Elder | 57acbaa | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 1885 | if (obj_request_img_data_test(obj_request)) { |
| 1886 | rbd_assert(obj_request->img_request); |
| 1887 | rbd_assert(obj_request->which != BAD_WHICH); |
| 1888 | } else { |
| 1889 | rbd_assert(obj_request->which == BAD_WHICH); |
| 1890 | } |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1891 | |
Sage Weil | 1b83bef | 2013-02-25 16:11:12 -0800 | [diff] [blame] | 1892 | if (osd_req->r_result < 0) |
| 1893 | obj_request->result = osd_req->r_result; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1894 | |
Alex Elder | c47f937 | 2013-02-26 14:23:07 -0600 | [diff] [blame] | 1895 | /* |
| 1896 | * We support a 64-bit length, but ultimately it has to be |
Christoph Hellwig | 7ad18af | 2015-01-13 17:20:04 +0100 | [diff] [blame] | 1897 | * passed to the block layer, which just supports a 32-bit |
| 1898 | * length field. |
Alex Elder | c47f937 | 2013-02-26 14:23:07 -0600 | [diff] [blame] | 1899 | */ |
Yan, Zheng | 7665d85 | 2016-01-07 16:48:57 +0800 | [diff] [blame] | 1900 | obj_request->xferred = osd_req->r_ops[0].outdata_len; |
Alex Elder | 8b3e1a5 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 1901 | rbd_assert(obj_request->xferred < (u64)UINT_MAX); |
Ilya Dryomov | 0ccd592 | 2014-02-25 16:22:28 +0200 | [diff] [blame] | 1902 | |
Alex Elder | 7952873 | 2013-04-03 21:32:51 -0500 | [diff] [blame] | 1903 | opcode = osd_req->r_ops[0].op; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1904 | switch (opcode) { |
| 1905 | case CEPH_OSD_OP_READ: |
Alex Elder | c47f937 | 2013-02-26 14:23:07 -0600 | [diff] [blame] | 1906 | rbd_osd_read_callback(obj_request); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1907 | break; |
Ilya Dryomov | 0ccd592 | 2014-02-25 16:22:28 +0200 | [diff] [blame] | 1908 | case CEPH_OSD_OP_SETALLOCHINT: |
Ilya Dryomov | e30b757 | 2015-10-07 17:27:17 +0200 | [diff] [blame] | 1909 | rbd_assert(osd_req->r_ops[1].op == CEPH_OSD_OP_WRITE || |
| 1910 | osd_req->r_ops[1].op == CEPH_OSD_OP_WRITEFULL); |
Ilya Dryomov | 0ccd592 | 2014-02-25 16:22:28 +0200 | [diff] [blame] | 1911 | /* fall through */ |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1912 | case CEPH_OSD_OP_WRITE: |
Ilya Dryomov | e30b757 | 2015-10-07 17:27:17 +0200 | [diff] [blame] | 1913 | case CEPH_OSD_OP_WRITEFULL: |
Alex Elder | c47f937 | 2013-02-26 14:23:07 -0600 | [diff] [blame] | 1914 | rbd_osd_write_callback(obj_request); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1915 | break; |
Alex Elder | fbfab53 | 2013-02-08 09:55:48 -0600 | [diff] [blame] | 1916 | case CEPH_OSD_OP_STAT: |
Alex Elder | c47f937 | 2013-02-26 14:23:07 -0600 | [diff] [blame] | 1917 | rbd_osd_stat_callback(obj_request); |
Alex Elder | fbfab53 | 2013-02-08 09:55:48 -0600 | [diff] [blame] | 1918 | break; |
Guangliang Zhao | 90e98c5 | 2014-04-01 22:22:16 +0800 | [diff] [blame] | 1919 | case CEPH_OSD_OP_DELETE: |
| 1920 | case CEPH_OSD_OP_TRUNCATE: |
| 1921 | case CEPH_OSD_OP_ZERO: |
| 1922 | rbd_osd_discard_callback(obj_request); |
| 1923 | break; |
Alex Elder | 36be9a7 | 2013-01-19 00:30:28 -0600 | [diff] [blame] | 1924 | case CEPH_OSD_OP_CALL: |
Ilya Dryomov | 2761713 | 2015-07-16 17:36:11 +0300 | [diff] [blame] | 1925 | rbd_osd_call_callback(obj_request); |
| 1926 | break; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1927 | default: |
Ilya Dryomov | 9584d50 | 2014-07-11 12:11:20 +0400 | [diff] [blame] | 1928 | rbd_warn(NULL, "%s: unsupported op %hu", |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1929 | obj_request->object_name, (unsigned short) opcode); |
| 1930 | break; |
| 1931 | } |
| 1932 | |
Alex Elder | 0774130 | 2013-02-05 23:41:50 -0600 | [diff] [blame] | 1933 | if (obj_request_done_test(obj_request)) |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1934 | rbd_obj_request_complete(obj_request); |
| 1935 | } |
| 1936 | |
Alex Elder | 9d4df01 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 1937 | static void rbd_osd_req_format_read(struct rbd_obj_request *obj_request) |
Alex Elder | 430c28c | 2013-04-03 21:32:51 -0500 | [diff] [blame] | 1938 | { |
| 1939 | struct rbd_img_request *img_request = obj_request->img_request; |
Alex Elder | 8c042b0 | 2013-04-03 01:28:58 -0500 | [diff] [blame] | 1940 | struct ceph_osd_request *osd_req = obj_request->osd_req; |
Alex Elder | 430c28c | 2013-04-03 21:32:51 -0500 | [diff] [blame] | 1941 | |
Ilya Dryomov | bb873b539 | 2016-05-26 00:29:52 +0200 | [diff] [blame] | 1942 | if (img_request) |
| 1943 | osd_req->r_snapid = img_request->snap_id; |
Alex Elder | 9d4df01 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 1944 | } |
| 1945 | |
| 1946 | static void rbd_osd_req_format_write(struct rbd_obj_request *obj_request) |
| 1947 | { |
Alex Elder | 9d4df01 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 1948 | struct ceph_osd_request *osd_req = obj_request->osd_req; |
Alex Elder | 9d4df01 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 1949 | |
Ilya Dryomov | bb873b539 | 2016-05-26 00:29:52 +0200 | [diff] [blame] | 1950 | osd_req->r_mtime = CURRENT_TIME; |
| 1951 | osd_req->r_data_offset = obj_request->offset; |
Alex Elder | 430c28c | 2013-04-03 21:32:51 -0500 | [diff] [blame] | 1952 | } |
| 1953 | |
Ilya Dryomov | 0ccd592 | 2014-02-25 16:22:28 +0200 | [diff] [blame] | 1954 | /* |
| 1955 | * Create an osd request. A read request has one osd op (read). |
| 1956 | * A write request has either one (watch) or two (hint+write) osd ops. |
| 1957 | * (All rbd data writes are prefixed with an allocation hint op, but |
| 1958 | * technically osd watch is a write request, hence this distinction.) |
| 1959 | */ |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1960 | static struct ceph_osd_request *rbd_osd_req_create( |
| 1961 | struct rbd_device *rbd_dev, |
Guangliang Zhao | 6d2940c | 2014-03-13 11:21:35 +0800 | [diff] [blame] | 1962 | enum obj_operation_type op_type, |
Ilya Dryomov | deb236b | 2014-02-25 16:22:27 +0200 | [diff] [blame] | 1963 | unsigned int num_ops, |
Alex Elder | 430c28c | 2013-04-03 21:32:51 -0500 | [diff] [blame] | 1964 | struct rbd_obj_request *obj_request) |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1965 | { |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1966 | struct ceph_snap_context *snapc = NULL; |
| 1967 | struct ceph_osd_client *osdc; |
| 1968 | struct ceph_osd_request *osd_req; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1969 | |
Guangliang Zhao | 90e98c5 | 2014-04-01 22:22:16 +0800 | [diff] [blame] | 1970 | if (obj_request_img_data_test(obj_request) && |
| 1971 | (op_type == OBJ_OP_DISCARD || op_type == OBJ_OP_WRITE)) { |
Alex Elder | 6365d33 | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 1972 | struct rbd_img_request *img_request = obj_request->img_request; |
Guangliang Zhao | 90e98c5 | 2014-04-01 22:22:16 +0800 | [diff] [blame] | 1973 | if (op_type == OBJ_OP_WRITE) { |
| 1974 | rbd_assert(img_request_write_test(img_request)); |
| 1975 | } else { |
| 1976 | rbd_assert(img_request_discard_test(img_request)); |
| 1977 | } |
Guangliang Zhao | 6d2940c | 2014-03-13 11:21:35 +0800 | [diff] [blame] | 1978 | snapc = img_request->snapc; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1979 | } |
| 1980 | |
Guangliang Zhao | 6d2940c | 2014-03-13 11:21:35 +0800 | [diff] [blame] | 1981 | rbd_assert(num_ops == 1 || ((op_type == OBJ_OP_WRITE) && num_ops == 2)); |
Ilya Dryomov | deb236b | 2014-02-25 16:22:27 +0200 | [diff] [blame] | 1982 | |
| 1983 | /* Allocate and initialize the request, for the num_ops ops */ |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1984 | |
| 1985 | osdc = &rbd_dev->rbd_client->client->osdc; |
Ilya Dryomov | deb236b | 2014-02-25 16:22:27 +0200 | [diff] [blame] | 1986 | osd_req = ceph_osdc_alloc_request(osdc, snapc, num_ops, false, |
David Disseldorp | 2224d87 | 2016-04-05 11:13:39 +0200 | [diff] [blame] | 1987 | GFP_NOIO); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1988 | if (!osd_req) |
Ilya Dryomov | 13d1ad1 | 2016-04-27 14:15:51 +0200 | [diff] [blame] | 1989 | goto fail; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1990 | |
Guangliang Zhao | 90e98c5 | 2014-04-01 22:22:16 +0800 | [diff] [blame] | 1991 | if (op_type == OBJ_OP_WRITE || op_type == OBJ_OP_DISCARD) |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1992 | osd_req->r_flags = CEPH_OSD_FLAG_WRITE | CEPH_OSD_FLAG_ONDISK; |
Alex Elder | 430c28c | 2013-04-03 21:32:51 -0500 | [diff] [blame] | 1993 | else |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1994 | osd_req->r_flags = CEPH_OSD_FLAG_READ; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1995 | |
| 1996 | osd_req->r_callback = rbd_osd_req_callback; |
| 1997 | osd_req->r_priv = obj_request; |
| 1998 | |
Yan, Zheng | 7627151 | 2016-02-03 21:24:49 +0800 | [diff] [blame] | 1999 | osd_req->r_base_oloc.pool = rbd_dev->layout.pool_id; |
Ilya Dryomov | d30291b | 2016-04-29 19:54:20 +0200 | [diff] [blame] | 2000 | if (ceph_oid_aprintf(&osd_req->r_base_oid, GFP_NOIO, "%s", |
| 2001 | obj_request->object_name)) |
| 2002 | goto fail; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2003 | |
Ilya Dryomov | 13d1ad1 | 2016-04-27 14:15:51 +0200 | [diff] [blame] | 2004 | if (ceph_osdc_alloc_messages(osd_req, GFP_NOIO)) |
| 2005 | goto fail; |
| 2006 | |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2007 | return osd_req; |
Ilya Dryomov | 13d1ad1 | 2016-04-27 14:15:51 +0200 | [diff] [blame] | 2008 | |
| 2009 | fail: |
| 2010 | ceph_osdc_put_request(osd_req); |
| 2011 | return NULL; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2012 | } |
| 2013 | |
Alex Elder | 0eefd47 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2014 | /* |
Josh Durgin | d3246fb | 2014-04-07 16:49:21 -0700 | [diff] [blame] | 2015 | * Create a copyup osd request based on the information in the object |
| 2016 | * request supplied. A copyup request has two or three osd ops, a |
| 2017 | * copyup method call, potentially a hint op, and a write or truncate |
| 2018 | * or zero op. |
Alex Elder | 0eefd47 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2019 | */ |
| 2020 | static struct ceph_osd_request * |
| 2021 | rbd_osd_req_create_copyup(struct rbd_obj_request *obj_request) |
| 2022 | { |
| 2023 | struct rbd_img_request *img_request; |
| 2024 | struct ceph_snap_context *snapc; |
| 2025 | struct rbd_device *rbd_dev; |
| 2026 | struct ceph_osd_client *osdc; |
| 2027 | struct ceph_osd_request *osd_req; |
Josh Durgin | d3246fb | 2014-04-07 16:49:21 -0700 | [diff] [blame] | 2028 | int num_osd_ops = 3; |
Alex Elder | 0eefd47 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2029 | |
| 2030 | rbd_assert(obj_request_img_data_test(obj_request)); |
| 2031 | img_request = obj_request->img_request; |
| 2032 | rbd_assert(img_request); |
Josh Durgin | d3246fb | 2014-04-07 16:49:21 -0700 | [diff] [blame] | 2033 | rbd_assert(img_request_write_test(img_request) || |
| 2034 | img_request_discard_test(img_request)); |
Alex Elder | 0eefd47 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2035 | |
Josh Durgin | d3246fb | 2014-04-07 16:49:21 -0700 | [diff] [blame] | 2036 | if (img_request_discard_test(img_request)) |
| 2037 | num_osd_ops = 2; |
| 2038 | |
| 2039 | /* Allocate and initialize the request, for all the ops */ |
Alex Elder | 0eefd47 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2040 | |
| 2041 | snapc = img_request->snapc; |
| 2042 | rbd_dev = img_request->rbd_dev; |
| 2043 | osdc = &rbd_dev->rbd_client->client->osdc; |
Josh Durgin | d3246fb | 2014-04-07 16:49:21 -0700 | [diff] [blame] | 2044 | osd_req = ceph_osdc_alloc_request(osdc, snapc, num_osd_ops, |
David Disseldorp | 2224d87 | 2016-04-05 11:13:39 +0200 | [diff] [blame] | 2045 | false, GFP_NOIO); |
Alex Elder | 0eefd47 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2046 | if (!osd_req) |
Ilya Dryomov | 13d1ad1 | 2016-04-27 14:15:51 +0200 | [diff] [blame] | 2047 | goto fail; |
Alex Elder | 0eefd47 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2048 | |
| 2049 | osd_req->r_flags = CEPH_OSD_FLAG_WRITE | CEPH_OSD_FLAG_ONDISK; |
| 2050 | osd_req->r_callback = rbd_osd_req_callback; |
| 2051 | osd_req->r_priv = obj_request; |
| 2052 | |
Yan, Zheng | 7627151 | 2016-02-03 21:24:49 +0800 | [diff] [blame] | 2053 | osd_req->r_base_oloc.pool = rbd_dev->layout.pool_id; |
Ilya Dryomov | d30291b | 2016-04-29 19:54:20 +0200 | [diff] [blame] | 2054 | if (ceph_oid_aprintf(&osd_req->r_base_oid, GFP_NOIO, "%s", |
| 2055 | obj_request->object_name)) |
| 2056 | goto fail; |
Alex Elder | 0eefd47 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2057 | |
Ilya Dryomov | 13d1ad1 | 2016-04-27 14:15:51 +0200 | [diff] [blame] | 2058 | if (ceph_osdc_alloc_messages(osd_req, GFP_NOIO)) |
| 2059 | goto fail; |
| 2060 | |
Alex Elder | 0eefd47 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2061 | return osd_req; |
Ilya Dryomov | 13d1ad1 | 2016-04-27 14:15:51 +0200 | [diff] [blame] | 2062 | |
| 2063 | fail: |
| 2064 | ceph_osdc_put_request(osd_req); |
| 2065 | return NULL; |
Alex Elder | 0eefd47 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2066 | } |
| 2067 | |
| 2068 | |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2069 | static void rbd_osd_req_destroy(struct ceph_osd_request *osd_req) |
| 2070 | { |
| 2071 | ceph_osdc_put_request(osd_req); |
| 2072 | } |
| 2073 | |
| 2074 | /* object_name is assumed to be a non-null pointer and NUL-terminated */ |
| 2075 | |
| 2076 | static struct rbd_obj_request *rbd_obj_request_create(const char *object_name, |
| 2077 | u64 offset, u64 length, |
| 2078 | enum obj_request_type type) |
| 2079 | { |
| 2080 | struct rbd_obj_request *obj_request; |
| 2081 | size_t size; |
| 2082 | char *name; |
| 2083 | |
| 2084 | rbd_assert(obj_request_type_valid(type)); |
| 2085 | |
| 2086 | size = strlen(object_name) + 1; |
Ilya Dryomov | 5a60e87 | 2015-06-24 17:24:33 +0300 | [diff] [blame] | 2087 | name = kmalloc(size, GFP_NOIO); |
Alex Elder | f907ad5 | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 2088 | if (!name) |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2089 | return NULL; |
| 2090 | |
Ilya Dryomov | 5a60e87 | 2015-06-24 17:24:33 +0300 | [diff] [blame] | 2091 | obj_request = kmem_cache_zalloc(rbd_obj_request_cache, GFP_NOIO); |
Alex Elder | f907ad5 | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 2092 | if (!obj_request) { |
| 2093 | kfree(name); |
| 2094 | return NULL; |
| 2095 | } |
| 2096 | |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2097 | obj_request->object_name = memcpy(name, object_name, size); |
| 2098 | obj_request->offset = offset; |
| 2099 | obj_request->length = length; |
Alex Elder | 926f9b3 | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 2100 | obj_request->flags = 0; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2101 | obj_request->which = BAD_WHICH; |
| 2102 | obj_request->type = type; |
| 2103 | INIT_LIST_HEAD(&obj_request->links); |
Alex Elder | 788e2df | 2013-01-17 12:25:27 -0600 | [diff] [blame] | 2104 | init_completion(&obj_request->completion); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2105 | kref_init(&obj_request->kref); |
| 2106 | |
Alex Elder | 37206ee | 2013-02-20 17:32:08 -0600 | [diff] [blame] | 2107 | dout("%s: \"%s\" %llu/%llu %d -> obj %p\n", __func__, object_name, |
| 2108 | offset, length, (int)type, obj_request); |
| 2109 | |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2110 | return obj_request; |
| 2111 | } |
| 2112 | |
| 2113 | static void rbd_obj_request_destroy(struct kref *kref) |
| 2114 | { |
| 2115 | struct rbd_obj_request *obj_request; |
| 2116 | |
| 2117 | obj_request = container_of(kref, struct rbd_obj_request, kref); |
| 2118 | |
Alex Elder | 37206ee | 2013-02-20 17:32:08 -0600 | [diff] [blame] | 2119 | dout("%s: obj %p\n", __func__, obj_request); |
| 2120 | |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2121 | rbd_assert(obj_request->img_request == NULL); |
| 2122 | rbd_assert(obj_request->which == BAD_WHICH); |
| 2123 | |
| 2124 | if (obj_request->osd_req) |
| 2125 | rbd_osd_req_destroy(obj_request->osd_req); |
| 2126 | |
| 2127 | rbd_assert(obj_request_type_valid(obj_request->type)); |
| 2128 | switch (obj_request->type) { |
Alex Elder | 9969ebc | 2013-01-18 12:31:10 -0600 | [diff] [blame] | 2129 | case OBJ_REQUEST_NODATA: |
| 2130 | break; /* Nothing to do */ |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2131 | case OBJ_REQUEST_BIO: |
| 2132 | if (obj_request->bio_list) |
| 2133 | bio_chain_put(obj_request->bio_list); |
| 2134 | break; |
Alex Elder | 788e2df | 2013-01-17 12:25:27 -0600 | [diff] [blame] | 2135 | case OBJ_REQUEST_PAGES: |
| 2136 | if (obj_request->pages) |
| 2137 | ceph_release_page_vector(obj_request->pages, |
| 2138 | obj_request->page_count); |
| 2139 | break; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2140 | } |
| 2141 | |
Alex Elder | f907ad5 | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 2142 | kfree(obj_request->object_name); |
Alex Elder | 868311b | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 2143 | obj_request->object_name = NULL; |
| 2144 | kmem_cache_free(rbd_obj_request_cache, obj_request); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2145 | } |
| 2146 | |
Alex Elder | fb65d228 | 2013-05-08 22:50:04 -0500 | [diff] [blame] | 2147 | /* It's OK to call this for a device with no parent */ |
| 2148 | |
| 2149 | static void rbd_spec_put(struct rbd_spec *spec); |
| 2150 | static void rbd_dev_unparent(struct rbd_device *rbd_dev) |
| 2151 | { |
| 2152 | rbd_dev_remove_parent(rbd_dev); |
| 2153 | rbd_spec_put(rbd_dev->parent_spec); |
| 2154 | rbd_dev->parent_spec = NULL; |
| 2155 | rbd_dev->parent_overlap = 0; |
| 2156 | } |
| 2157 | |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2158 | /* |
Alex Elder | a2acd00 | 2013-05-08 22:50:04 -0500 | [diff] [blame] | 2159 | * Parent image reference counting is used to determine when an |
| 2160 | * image's parent fields can be safely torn down--after there are no |
| 2161 | * more in-flight requests to the parent image. When the last |
| 2162 | * reference is dropped, cleaning them up is safe. |
| 2163 | */ |
| 2164 | static void rbd_dev_parent_put(struct rbd_device *rbd_dev) |
| 2165 | { |
| 2166 | int counter; |
| 2167 | |
| 2168 | if (!rbd_dev->parent_spec) |
| 2169 | return; |
| 2170 | |
| 2171 | counter = atomic_dec_return_safe(&rbd_dev->parent_ref); |
| 2172 | if (counter > 0) |
| 2173 | return; |
| 2174 | |
| 2175 | /* Last reference; clean up parent data structures */ |
| 2176 | |
| 2177 | if (!counter) |
| 2178 | rbd_dev_unparent(rbd_dev); |
| 2179 | else |
Ilya Dryomov | 9584d50 | 2014-07-11 12:11:20 +0400 | [diff] [blame] | 2180 | rbd_warn(rbd_dev, "parent reference underflow"); |
Alex Elder | a2acd00 | 2013-05-08 22:50:04 -0500 | [diff] [blame] | 2181 | } |
| 2182 | |
| 2183 | /* |
| 2184 | * If an image has a non-zero parent overlap, get a reference to its |
| 2185 | * parent. |
| 2186 | * |
| 2187 | * Returns true if the rbd device has a parent with a non-zero |
| 2188 | * overlap and a reference for it was successfully taken, or |
| 2189 | * false otherwise. |
| 2190 | */ |
| 2191 | static bool rbd_dev_parent_get(struct rbd_device *rbd_dev) |
| 2192 | { |
Ilya Dryomov | ae43e9d | 2015-01-19 18:13:43 +0300 | [diff] [blame] | 2193 | int counter = 0; |
Alex Elder | a2acd00 | 2013-05-08 22:50:04 -0500 | [diff] [blame] | 2194 | |
| 2195 | if (!rbd_dev->parent_spec) |
| 2196 | return false; |
| 2197 | |
Ilya Dryomov | ae43e9d | 2015-01-19 18:13:43 +0300 | [diff] [blame] | 2198 | down_read(&rbd_dev->header_rwsem); |
| 2199 | if (rbd_dev->parent_overlap) |
| 2200 | counter = atomic_inc_return_safe(&rbd_dev->parent_ref); |
| 2201 | up_read(&rbd_dev->header_rwsem); |
Alex Elder | a2acd00 | 2013-05-08 22:50:04 -0500 | [diff] [blame] | 2202 | |
| 2203 | if (counter < 0) |
Ilya Dryomov | 9584d50 | 2014-07-11 12:11:20 +0400 | [diff] [blame] | 2204 | rbd_warn(rbd_dev, "parent reference overflow"); |
Alex Elder | a2acd00 | 2013-05-08 22:50:04 -0500 | [diff] [blame] | 2205 | |
Ilya Dryomov | ae43e9d | 2015-01-19 18:13:43 +0300 | [diff] [blame] | 2206 | return counter > 0; |
Alex Elder | a2acd00 | 2013-05-08 22:50:04 -0500 | [diff] [blame] | 2207 | } |
| 2208 | |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2209 | /* |
| 2210 | * Caller is responsible for filling in the list of object requests |
| 2211 | * that comprises the image request, and the Linux request pointer |
| 2212 | * (if there is one). |
| 2213 | */ |
Alex Elder | cc344fa | 2013-02-19 12:25:56 -0600 | [diff] [blame] | 2214 | static struct rbd_img_request *rbd_img_request_create( |
| 2215 | struct rbd_device *rbd_dev, |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2216 | u64 offset, u64 length, |
Guangliang Zhao | 6d2940c | 2014-03-13 11:21:35 +0800 | [diff] [blame] | 2217 | enum obj_operation_type op_type, |
Josh Durgin | 4e752f0 | 2014-04-08 11:12:11 -0700 | [diff] [blame] | 2218 | struct ceph_snap_context *snapc) |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2219 | { |
| 2220 | struct rbd_img_request *img_request; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2221 | |
Ilya Dryomov | 7a716aa | 2014-08-05 11:25:54 +0400 | [diff] [blame] | 2222 | img_request = kmem_cache_alloc(rbd_img_request_cache, GFP_NOIO); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2223 | if (!img_request) |
| 2224 | return NULL; |
| 2225 | |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2226 | img_request->rq = NULL; |
| 2227 | img_request->rbd_dev = rbd_dev; |
| 2228 | img_request->offset = offset; |
| 2229 | img_request->length = length; |
Alex Elder | 0c42524 | 2013-02-08 09:55:49 -0600 | [diff] [blame] | 2230 | img_request->flags = 0; |
Guangliang Zhao | 90e98c5 | 2014-04-01 22:22:16 +0800 | [diff] [blame] | 2231 | if (op_type == OBJ_OP_DISCARD) { |
| 2232 | img_request_discard_set(img_request); |
| 2233 | img_request->snapc = snapc; |
| 2234 | } else if (op_type == OBJ_OP_WRITE) { |
Alex Elder | 0c42524 | 2013-02-08 09:55:49 -0600 | [diff] [blame] | 2235 | img_request_write_set(img_request); |
Josh Durgin | 4e752f0 | 2014-04-08 11:12:11 -0700 | [diff] [blame] | 2236 | img_request->snapc = snapc; |
Alex Elder | 0c42524 | 2013-02-08 09:55:49 -0600 | [diff] [blame] | 2237 | } else { |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2238 | img_request->snap_id = rbd_dev->spec->snap_id; |
Alex Elder | 0c42524 | 2013-02-08 09:55:49 -0600 | [diff] [blame] | 2239 | } |
Alex Elder | a2acd00 | 2013-05-08 22:50:04 -0500 | [diff] [blame] | 2240 | if (rbd_dev_parent_get(rbd_dev)) |
Alex Elder | d0b2e94 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 2241 | img_request_layered_set(img_request); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2242 | spin_lock_init(&img_request->completion_lock); |
| 2243 | img_request->next_completion = 0; |
| 2244 | img_request->callback = NULL; |
Alex Elder | a5a337d | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 2245 | img_request->result = 0; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2246 | img_request->obj_request_count = 0; |
| 2247 | INIT_LIST_HEAD(&img_request->obj_requests); |
| 2248 | kref_init(&img_request->kref); |
| 2249 | |
Alex Elder | 37206ee | 2013-02-20 17:32:08 -0600 | [diff] [blame] | 2250 | dout("%s: rbd_dev %p %s %llu/%llu -> img %p\n", __func__, rbd_dev, |
Guangliang Zhao | 6d2940c | 2014-03-13 11:21:35 +0800 | [diff] [blame] | 2251 | obj_op_name(op_type), offset, length, img_request); |
Alex Elder | 37206ee | 2013-02-20 17:32:08 -0600 | [diff] [blame] | 2252 | |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2253 | return img_request; |
| 2254 | } |
| 2255 | |
| 2256 | static void rbd_img_request_destroy(struct kref *kref) |
| 2257 | { |
| 2258 | struct rbd_img_request *img_request; |
| 2259 | struct rbd_obj_request *obj_request; |
| 2260 | struct rbd_obj_request *next_obj_request; |
| 2261 | |
| 2262 | img_request = container_of(kref, struct rbd_img_request, kref); |
| 2263 | |
Alex Elder | 37206ee | 2013-02-20 17:32:08 -0600 | [diff] [blame] | 2264 | dout("%s: img %p\n", __func__, img_request); |
| 2265 | |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2266 | for_each_obj_request_safe(img_request, obj_request, next_obj_request) |
| 2267 | rbd_img_obj_request_del(img_request, obj_request); |
Alex Elder | 25dcf95 | 2013-01-25 17:08:55 -0600 | [diff] [blame] | 2268 | rbd_assert(img_request->obj_request_count == 0); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2269 | |
Alex Elder | a2acd00 | 2013-05-08 22:50:04 -0500 | [diff] [blame] | 2270 | if (img_request_layered_test(img_request)) { |
| 2271 | img_request_layered_clear(img_request); |
| 2272 | rbd_dev_parent_put(img_request->rbd_dev); |
| 2273 | } |
| 2274 | |
Josh Durgin | bef9545 | 2014-04-04 17:47:52 -0700 | [diff] [blame] | 2275 | if (img_request_write_test(img_request) || |
| 2276 | img_request_discard_test(img_request)) |
Alex Elder | 812164f8 | 2013-04-30 00:44:32 -0500 | [diff] [blame] | 2277 | ceph_put_snap_context(img_request->snapc); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2278 | |
Alex Elder | 1c2a9df | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 2279 | kmem_cache_free(rbd_img_request_cache, img_request); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2280 | } |
| 2281 | |
Alex Elder | e93f315 | 2013-05-08 22:50:04 -0500 | [diff] [blame] | 2282 | static struct rbd_img_request *rbd_parent_request_create( |
| 2283 | struct rbd_obj_request *obj_request, |
| 2284 | u64 img_offset, u64 length) |
| 2285 | { |
| 2286 | struct rbd_img_request *parent_request; |
| 2287 | struct rbd_device *rbd_dev; |
| 2288 | |
| 2289 | rbd_assert(obj_request->img_request); |
| 2290 | rbd_dev = obj_request->img_request->rbd_dev; |
| 2291 | |
Josh Durgin | 4e752f0 | 2014-04-08 11:12:11 -0700 | [diff] [blame] | 2292 | parent_request = rbd_img_request_create(rbd_dev->parent, img_offset, |
Guangliang Zhao | 6d2940c | 2014-03-13 11:21:35 +0800 | [diff] [blame] | 2293 | length, OBJ_OP_READ, NULL); |
Alex Elder | e93f315 | 2013-05-08 22:50:04 -0500 | [diff] [blame] | 2294 | if (!parent_request) |
| 2295 | return NULL; |
| 2296 | |
| 2297 | img_request_child_set(parent_request); |
| 2298 | rbd_obj_request_get(obj_request); |
| 2299 | parent_request->obj_request = obj_request; |
| 2300 | |
| 2301 | return parent_request; |
| 2302 | } |
| 2303 | |
| 2304 | static void rbd_parent_request_destroy(struct kref *kref) |
| 2305 | { |
| 2306 | struct rbd_img_request *parent_request; |
| 2307 | struct rbd_obj_request *orig_request; |
| 2308 | |
| 2309 | parent_request = container_of(kref, struct rbd_img_request, kref); |
| 2310 | orig_request = parent_request->obj_request; |
| 2311 | |
| 2312 | parent_request->obj_request = NULL; |
| 2313 | rbd_obj_request_put(orig_request); |
| 2314 | img_request_child_clear(parent_request); |
| 2315 | |
| 2316 | rbd_img_request_destroy(kref); |
| 2317 | } |
| 2318 | |
Alex Elder | 1217857 | 2013-02-08 09:55:49 -0600 | [diff] [blame] | 2319 | static bool rbd_img_obj_end_request(struct rbd_obj_request *obj_request) |
| 2320 | { |
Alex Elder | 6365d33 | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 2321 | struct rbd_img_request *img_request; |
Alex Elder | 1217857 | 2013-02-08 09:55:49 -0600 | [diff] [blame] | 2322 | unsigned int xferred; |
| 2323 | int result; |
Alex Elder | 8b3e1a5 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 2324 | bool more; |
Alex Elder | 1217857 | 2013-02-08 09:55:49 -0600 | [diff] [blame] | 2325 | |
Alex Elder | 6365d33 | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 2326 | rbd_assert(obj_request_img_data_test(obj_request)); |
| 2327 | img_request = obj_request->img_request; |
| 2328 | |
Alex Elder | 1217857 | 2013-02-08 09:55:49 -0600 | [diff] [blame] | 2329 | rbd_assert(obj_request->xferred <= (u64)UINT_MAX); |
| 2330 | xferred = (unsigned int)obj_request->xferred; |
| 2331 | result = obj_request->result; |
| 2332 | if (result) { |
| 2333 | struct rbd_device *rbd_dev = img_request->rbd_dev; |
Guangliang Zhao | 6d2940c | 2014-03-13 11:21:35 +0800 | [diff] [blame] | 2334 | enum obj_operation_type op_type; |
| 2335 | |
Guangliang Zhao | 90e98c5 | 2014-04-01 22:22:16 +0800 | [diff] [blame] | 2336 | if (img_request_discard_test(img_request)) |
| 2337 | op_type = OBJ_OP_DISCARD; |
| 2338 | else if (img_request_write_test(img_request)) |
| 2339 | op_type = OBJ_OP_WRITE; |
| 2340 | else |
| 2341 | op_type = OBJ_OP_READ; |
Alex Elder | 1217857 | 2013-02-08 09:55:49 -0600 | [diff] [blame] | 2342 | |
Ilya Dryomov | 9584d50 | 2014-07-11 12:11:20 +0400 | [diff] [blame] | 2343 | rbd_warn(rbd_dev, "%s %llx at %llx (%llx)", |
Guangliang Zhao | 6d2940c | 2014-03-13 11:21:35 +0800 | [diff] [blame] | 2344 | obj_op_name(op_type), obj_request->length, |
| 2345 | obj_request->img_offset, obj_request->offset); |
Ilya Dryomov | 9584d50 | 2014-07-11 12:11:20 +0400 | [diff] [blame] | 2346 | rbd_warn(rbd_dev, " result %d xferred %x", |
Alex Elder | 1217857 | 2013-02-08 09:55:49 -0600 | [diff] [blame] | 2347 | result, xferred); |
| 2348 | if (!img_request->result) |
| 2349 | img_request->result = result; |
Ilya Dryomov | 082a75d | 2015-04-25 15:56:15 +0300 | [diff] [blame] | 2350 | /* |
| 2351 | * Need to end I/O on the entire obj_request worth of |
| 2352 | * bytes in case of error. |
| 2353 | */ |
| 2354 | xferred = obj_request->length; |
Alex Elder | 1217857 | 2013-02-08 09:55:49 -0600 | [diff] [blame] | 2355 | } |
| 2356 | |
Alex Elder | f1a4739 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2357 | /* Image object requests don't own their page array */ |
| 2358 | |
| 2359 | if (obj_request->type == OBJ_REQUEST_PAGES) { |
| 2360 | obj_request->pages = NULL; |
| 2361 | obj_request->page_count = 0; |
| 2362 | } |
| 2363 | |
Alex Elder | 8b3e1a5 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 2364 | if (img_request_child_test(img_request)) { |
| 2365 | rbd_assert(img_request->obj_request != NULL); |
| 2366 | more = obj_request->which < img_request->obj_request_count - 1; |
| 2367 | } else { |
| 2368 | rbd_assert(img_request->rq != NULL); |
Christoph Hellwig | 7ad18af | 2015-01-13 17:20:04 +0100 | [diff] [blame] | 2369 | |
| 2370 | more = blk_update_request(img_request->rq, result, xferred); |
| 2371 | if (!more) |
| 2372 | __blk_mq_end_request(img_request->rq, result); |
Alex Elder | 8b3e1a5 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 2373 | } |
| 2374 | |
| 2375 | return more; |
Alex Elder | 1217857 | 2013-02-08 09:55:49 -0600 | [diff] [blame] | 2376 | } |
| 2377 | |
Alex Elder | 2169238 | 2013-04-05 01:27:12 -0500 | [diff] [blame] | 2378 | static void rbd_img_obj_callback(struct rbd_obj_request *obj_request) |
| 2379 | { |
| 2380 | struct rbd_img_request *img_request; |
| 2381 | u32 which = obj_request->which; |
| 2382 | bool more = true; |
| 2383 | |
Alex Elder | 6365d33 | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 2384 | rbd_assert(obj_request_img_data_test(obj_request)); |
Alex Elder | 2169238 | 2013-04-05 01:27:12 -0500 | [diff] [blame] | 2385 | img_request = obj_request->img_request; |
| 2386 | |
| 2387 | dout("%s: img %p obj %p\n", __func__, img_request, obj_request); |
| 2388 | rbd_assert(img_request != NULL); |
Alex Elder | 2169238 | 2013-04-05 01:27:12 -0500 | [diff] [blame] | 2389 | rbd_assert(img_request->obj_request_count > 0); |
| 2390 | rbd_assert(which != BAD_WHICH); |
| 2391 | rbd_assert(which < img_request->obj_request_count); |
Alex Elder | 2169238 | 2013-04-05 01:27:12 -0500 | [diff] [blame] | 2392 | |
| 2393 | spin_lock_irq(&img_request->completion_lock); |
| 2394 | if (which != img_request->next_completion) |
| 2395 | goto out; |
| 2396 | |
| 2397 | for_each_obj_request_from(img_request, obj_request) { |
Alex Elder | 2169238 | 2013-04-05 01:27:12 -0500 | [diff] [blame] | 2398 | rbd_assert(more); |
| 2399 | rbd_assert(which < img_request->obj_request_count); |
| 2400 | |
| 2401 | if (!obj_request_done_test(obj_request)) |
| 2402 | break; |
Alex Elder | 1217857 | 2013-02-08 09:55:49 -0600 | [diff] [blame] | 2403 | more = rbd_img_obj_end_request(obj_request); |
Alex Elder | 2169238 | 2013-04-05 01:27:12 -0500 | [diff] [blame] | 2404 | which++; |
| 2405 | } |
| 2406 | |
| 2407 | rbd_assert(more ^ (which == img_request->obj_request_count)); |
| 2408 | img_request->next_completion = which; |
| 2409 | out: |
| 2410 | spin_unlock_irq(&img_request->completion_lock); |
Alex Elder | 0f2d5be | 2014-04-26 14:21:44 +0400 | [diff] [blame] | 2411 | rbd_img_request_put(img_request); |
Alex Elder | 2169238 | 2013-04-05 01:27:12 -0500 | [diff] [blame] | 2412 | |
| 2413 | if (!more) |
| 2414 | rbd_img_request_complete(img_request); |
| 2415 | } |
| 2416 | |
Alex Elder | f1a4739 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2417 | /* |
Josh Durgin | 3b434a2a | 2014-04-04 17:32:15 -0700 | [diff] [blame] | 2418 | * Add individual osd ops to the given ceph_osd_request and prepare |
| 2419 | * them for submission. num_ops is the current number of |
| 2420 | * osd operations already to the object request. |
| 2421 | */ |
| 2422 | static void rbd_img_obj_request_fill(struct rbd_obj_request *obj_request, |
| 2423 | struct ceph_osd_request *osd_request, |
| 2424 | enum obj_operation_type op_type, |
| 2425 | unsigned int num_ops) |
| 2426 | { |
| 2427 | struct rbd_img_request *img_request = obj_request->img_request; |
| 2428 | struct rbd_device *rbd_dev = img_request->rbd_dev; |
| 2429 | u64 object_size = rbd_obj_bytes(&rbd_dev->header); |
| 2430 | u64 offset = obj_request->offset; |
| 2431 | u64 length = obj_request->length; |
| 2432 | u64 img_end; |
| 2433 | u16 opcode; |
| 2434 | |
| 2435 | if (op_type == OBJ_OP_DISCARD) { |
Josh Durgin | d3246fb | 2014-04-07 16:49:21 -0700 | [diff] [blame] | 2436 | if (!offset && length == object_size && |
| 2437 | (!img_request_layered_test(img_request) || |
| 2438 | !obj_request_overlaps_parent(obj_request))) { |
Josh Durgin | 3b434a2a | 2014-04-04 17:32:15 -0700 | [diff] [blame] | 2439 | opcode = CEPH_OSD_OP_DELETE; |
| 2440 | } else if ((offset + length == object_size)) { |
| 2441 | opcode = CEPH_OSD_OP_TRUNCATE; |
| 2442 | } else { |
| 2443 | down_read(&rbd_dev->header_rwsem); |
| 2444 | img_end = rbd_dev->header.image_size; |
| 2445 | up_read(&rbd_dev->header_rwsem); |
| 2446 | |
| 2447 | if (obj_request->img_offset + length == img_end) |
| 2448 | opcode = CEPH_OSD_OP_TRUNCATE; |
| 2449 | else |
| 2450 | opcode = CEPH_OSD_OP_ZERO; |
| 2451 | } |
| 2452 | } else if (op_type == OBJ_OP_WRITE) { |
Ilya Dryomov | e30b757 | 2015-10-07 17:27:17 +0200 | [diff] [blame] | 2453 | if (!offset && length == object_size) |
| 2454 | opcode = CEPH_OSD_OP_WRITEFULL; |
| 2455 | else |
| 2456 | opcode = CEPH_OSD_OP_WRITE; |
Josh Durgin | 3b434a2a | 2014-04-04 17:32:15 -0700 | [diff] [blame] | 2457 | osd_req_op_alloc_hint_init(osd_request, num_ops, |
| 2458 | object_size, object_size); |
| 2459 | num_ops++; |
| 2460 | } else { |
| 2461 | opcode = CEPH_OSD_OP_READ; |
| 2462 | } |
| 2463 | |
Ilya Dryomov | 7e868b6 | 2014-11-21 22:16:43 +0300 | [diff] [blame] | 2464 | if (opcode == CEPH_OSD_OP_DELETE) |
Yan, Zheng | 144cba1 | 2015-04-27 11:09:54 +0800 | [diff] [blame] | 2465 | osd_req_op_init(osd_request, num_ops, opcode, 0); |
Ilya Dryomov | 7e868b6 | 2014-11-21 22:16:43 +0300 | [diff] [blame] | 2466 | else |
| 2467 | osd_req_op_extent_init(osd_request, num_ops, opcode, |
| 2468 | offset, length, 0, 0); |
| 2469 | |
Josh Durgin | 3b434a2a | 2014-04-04 17:32:15 -0700 | [diff] [blame] | 2470 | if (obj_request->type == OBJ_REQUEST_BIO) |
| 2471 | osd_req_op_extent_osd_data_bio(osd_request, num_ops, |
| 2472 | obj_request->bio_list, length); |
| 2473 | else if (obj_request->type == OBJ_REQUEST_PAGES) |
| 2474 | osd_req_op_extent_osd_data_pages(osd_request, num_ops, |
| 2475 | obj_request->pages, length, |
| 2476 | offset & ~PAGE_MASK, false, false); |
| 2477 | |
| 2478 | /* Discards are also writes */ |
| 2479 | if (op_type == OBJ_OP_WRITE || op_type == OBJ_OP_DISCARD) |
| 2480 | rbd_osd_req_format_write(obj_request); |
| 2481 | else |
| 2482 | rbd_osd_req_format_read(obj_request); |
| 2483 | } |
| 2484 | |
| 2485 | /* |
Alex Elder | f1a4739 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2486 | * Split up an image request into one or more object requests, each |
| 2487 | * to a different object. The "type" parameter indicates whether |
| 2488 | * "data_desc" is the pointer to the head of a list of bio |
| 2489 | * structures, or the base of a page array. In either case this |
| 2490 | * function assumes data_desc describes memory sufficient to hold |
| 2491 | * all data described by the image request. |
| 2492 | */ |
| 2493 | static int rbd_img_request_fill(struct rbd_img_request *img_request, |
| 2494 | enum obj_request_type type, |
| 2495 | void *data_desc) |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2496 | { |
| 2497 | struct rbd_device *rbd_dev = img_request->rbd_dev; |
| 2498 | struct rbd_obj_request *obj_request = NULL; |
| 2499 | struct rbd_obj_request *next_obj_request; |
Jingoo Han | a158073 | 2013-08-09 13:04:35 +0900 | [diff] [blame] | 2500 | struct bio *bio_list = NULL; |
Alex Elder | f1a4739 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2501 | unsigned int bio_offset = 0; |
Jingoo Han | a158073 | 2013-08-09 13:04:35 +0900 | [diff] [blame] | 2502 | struct page **pages = NULL; |
Guangliang Zhao | 6d2940c | 2014-03-13 11:21:35 +0800 | [diff] [blame] | 2503 | enum obj_operation_type op_type; |
Alex Elder | 7da22d2 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 2504 | u64 img_offset; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2505 | u64 resid; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2506 | |
Alex Elder | f1a4739 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2507 | dout("%s: img %p type %d data_desc %p\n", __func__, img_request, |
| 2508 | (int)type, data_desc); |
Alex Elder | 37206ee | 2013-02-20 17:32:08 -0600 | [diff] [blame] | 2509 | |
Alex Elder | 7da22d2 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 2510 | img_offset = img_request->offset; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2511 | resid = img_request->length; |
Alex Elder | 4dda41d | 2013-02-20 21:59:33 -0600 | [diff] [blame] | 2512 | rbd_assert(resid > 0); |
Josh Durgin | 3b434a2a | 2014-04-04 17:32:15 -0700 | [diff] [blame] | 2513 | op_type = rbd_img_request_op_type(img_request); |
Alex Elder | f1a4739 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2514 | |
| 2515 | if (type == OBJ_REQUEST_BIO) { |
| 2516 | bio_list = data_desc; |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 2517 | rbd_assert(img_offset == |
| 2518 | bio_list->bi_iter.bi_sector << SECTOR_SHIFT); |
Guangliang Zhao | 90e98c5 | 2014-04-01 22:22:16 +0800 | [diff] [blame] | 2519 | } else if (type == OBJ_REQUEST_PAGES) { |
Alex Elder | f1a4739 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2520 | pages = data_desc; |
| 2521 | } |
| 2522 | |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2523 | while (resid) { |
Alex Elder | 2fa1232 | 2013-04-05 01:27:12 -0500 | [diff] [blame] | 2524 | struct ceph_osd_request *osd_req; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2525 | const char *object_name; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2526 | u64 offset; |
| 2527 | u64 length; |
| 2528 | |
Alex Elder | 7da22d2 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 2529 | object_name = rbd_segment_name(rbd_dev, img_offset); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2530 | if (!object_name) |
| 2531 | goto out_unwind; |
Alex Elder | 7da22d2 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 2532 | offset = rbd_segment_offset(rbd_dev, img_offset); |
| 2533 | length = rbd_segment_length(rbd_dev, img_offset, resid); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2534 | obj_request = rbd_obj_request_create(object_name, |
Alex Elder | f1a4739 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2535 | offset, length, type); |
Alex Elder | 78c2a44 | 2013-05-01 12:43:04 -0500 | [diff] [blame] | 2536 | /* object request has its own copy of the object name */ |
| 2537 | rbd_segment_name_free(object_name); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2538 | if (!obj_request) |
| 2539 | goto out_unwind; |
Ilya Dryomov | 62054da | 2014-03-04 11:57:17 +0200 | [diff] [blame] | 2540 | |
Josh Durgin | 03507db | 2013-08-27 14:45:46 -0700 | [diff] [blame] | 2541 | /* |
| 2542 | * set obj_request->img_request before creating the |
| 2543 | * osd_request so that it gets the right snapc |
| 2544 | */ |
| 2545 | rbd_img_obj_request_add(img_request, obj_request); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2546 | |
Alex Elder | f1a4739 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2547 | if (type == OBJ_REQUEST_BIO) { |
| 2548 | unsigned int clone_size; |
| 2549 | |
| 2550 | rbd_assert(length <= (u64)UINT_MAX); |
| 2551 | clone_size = (unsigned int)length; |
| 2552 | obj_request->bio_list = |
| 2553 | bio_chain_clone_range(&bio_list, |
| 2554 | &bio_offset, |
| 2555 | clone_size, |
David Disseldorp | 2224d87 | 2016-04-05 11:13:39 +0200 | [diff] [blame] | 2556 | GFP_NOIO); |
Alex Elder | f1a4739 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2557 | if (!obj_request->bio_list) |
Ilya Dryomov | 62054da | 2014-03-04 11:57:17 +0200 | [diff] [blame] | 2558 | goto out_unwind; |
Guangliang Zhao | 90e98c5 | 2014-04-01 22:22:16 +0800 | [diff] [blame] | 2559 | } else if (type == OBJ_REQUEST_PAGES) { |
Alex Elder | f1a4739 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2560 | unsigned int page_count; |
| 2561 | |
| 2562 | obj_request->pages = pages; |
| 2563 | page_count = (u32)calc_pages_for(offset, length); |
| 2564 | obj_request->page_count = page_count; |
| 2565 | if ((offset + length) & ~PAGE_MASK) |
| 2566 | page_count--; /* more on last page */ |
| 2567 | pages += page_count; |
| 2568 | } |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2569 | |
Guangliang Zhao | 6d2940c | 2014-03-13 11:21:35 +0800 | [diff] [blame] | 2570 | osd_req = rbd_osd_req_create(rbd_dev, op_type, |
| 2571 | (op_type == OBJ_OP_WRITE) ? 2 : 1, |
| 2572 | obj_request); |
Alex Elder | 2fa1232 | 2013-04-05 01:27:12 -0500 | [diff] [blame] | 2573 | if (!osd_req) |
Ilya Dryomov | 62054da | 2014-03-04 11:57:17 +0200 | [diff] [blame] | 2574 | goto out_unwind; |
Josh Durgin | 3b434a2a | 2014-04-04 17:32:15 -0700 | [diff] [blame] | 2575 | |
Alex Elder | 2fa1232 | 2013-04-05 01:27:12 -0500 | [diff] [blame] | 2576 | obj_request->osd_req = osd_req; |
Alex Elder | 2169238 | 2013-04-05 01:27:12 -0500 | [diff] [blame] | 2577 | obj_request->callback = rbd_img_obj_callback; |
Alex Elder | 7da22d2 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 2578 | obj_request->img_offset = img_offset; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2579 | |
Josh Durgin | 3b434a2a | 2014-04-04 17:32:15 -0700 | [diff] [blame] | 2580 | rbd_img_obj_request_fill(obj_request, osd_req, op_type, 0); |
| 2581 | |
| 2582 | rbd_img_request_get(img_request); |
| 2583 | |
Alex Elder | 7da22d2 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 2584 | img_offset += length; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2585 | resid -= length; |
| 2586 | } |
| 2587 | |
| 2588 | return 0; |
| 2589 | |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2590 | out_unwind: |
| 2591 | for_each_obj_request_safe(img_request, obj_request, next_obj_request) |
Ilya Dryomov | 42dd037 | 2014-03-04 11:57:17 +0200 | [diff] [blame] | 2592 | rbd_img_obj_request_del(img_request, obj_request); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2593 | |
| 2594 | return -ENOMEM; |
| 2595 | } |
| 2596 | |
Alex Elder | 3d7efd1 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2597 | static void |
Ilya Dryomov | 2761713 | 2015-07-16 17:36:11 +0300 | [diff] [blame] | 2598 | rbd_osd_copyup_callback(struct rbd_obj_request *obj_request) |
Alex Elder | 0eefd47 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2599 | { |
| 2600 | struct rbd_img_request *img_request; |
| 2601 | struct rbd_device *rbd_dev; |
Alex Elder | ebda640 | 2013-05-10 16:29:22 -0500 | [diff] [blame] | 2602 | struct page **pages; |
Alex Elder | 0eefd47 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2603 | u32 page_count; |
| 2604 | |
Ilya Dryomov | 2761713 | 2015-07-16 17:36:11 +0300 | [diff] [blame] | 2605 | dout("%s: obj %p\n", __func__, obj_request); |
| 2606 | |
Josh Durgin | d3246fb | 2014-04-07 16:49:21 -0700 | [diff] [blame] | 2607 | rbd_assert(obj_request->type == OBJ_REQUEST_BIO || |
| 2608 | obj_request->type == OBJ_REQUEST_NODATA); |
Alex Elder | 0eefd47 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2609 | rbd_assert(obj_request_img_data_test(obj_request)); |
| 2610 | img_request = obj_request->img_request; |
| 2611 | rbd_assert(img_request); |
| 2612 | |
| 2613 | rbd_dev = img_request->rbd_dev; |
| 2614 | rbd_assert(rbd_dev); |
Alex Elder | 0eefd47 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2615 | |
Alex Elder | ebda640 | 2013-05-10 16:29:22 -0500 | [diff] [blame] | 2616 | pages = obj_request->copyup_pages; |
| 2617 | rbd_assert(pages != NULL); |
Alex Elder | 0eefd47 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2618 | obj_request->copyup_pages = NULL; |
Alex Elder | ebda640 | 2013-05-10 16:29:22 -0500 | [diff] [blame] | 2619 | page_count = obj_request->copyup_page_count; |
| 2620 | rbd_assert(page_count); |
| 2621 | obj_request->copyup_page_count = 0; |
| 2622 | ceph_release_page_vector(pages, page_count); |
Alex Elder | 0eefd47 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2623 | |
| 2624 | /* |
| 2625 | * We want the transfer count to reflect the size of the |
| 2626 | * original write request. There is no such thing as a |
| 2627 | * successful short write, so if the request was successful |
| 2628 | * we can just set it to the originally-requested length. |
| 2629 | */ |
| 2630 | if (!obj_request->result) |
| 2631 | obj_request->xferred = obj_request->length; |
| 2632 | |
Ilya Dryomov | 2761713 | 2015-07-16 17:36:11 +0300 | [diff] [blame] | 2633 | obj_request_done_set(obj_request); |
Alex Elder | 0eefd47 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2634 | } |
| 2635 | |
| 2636 | static void |
Alex Elder | 3d7efd1 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2637 | rbd_img_obj_parent_read_full_callback(struct rbd_img_request *img_request) |
| 2638 | { |
| 2639 | struct rbd_obj_request *orig_request; |
Alex Elder | 0eefd47 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2640 | struct ceph_osd_request *osd_req; |
| 2641 | struct ceph_osd_client *osdc; |
| 2642 | struct rbd_device *rbd_dev; |
Alex Elder | 3d7efd1 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2643 | struct page **pages; |
Josh Durgin | d3246fb | 2014-04-07 16:49:21 -0700 | [diff] [blame] | 2644 | enum obj_operation_type op_type; |
Alex Elder | ebda640 | 2013-05-10 16:29:22 -0500 | [diff] [blame] | 2645 | u32 page_count; |
Alex Elder | bbea1c1 | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 2646 | int img_result; |
Alex Elder | ebda640 | 2013-05-10 16:29:22 -0500 | [diff] [blame] | 2647 | u64 parent_length; |
Alex Elder | 3d7efd1 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2648 | |
| 2649 | rbd_assert(img_request_child_test(img_request)); |
| 2650 | |
| 2651 | /* First get what we need from the image request */ |
| 2652 | |
| 2653 | pages = img_request->copyup_pages; |
| 2654 | rbd_assert(pages != NULL); |
| 2655 | img_request->copyup_pages = NULL; |
Alex Elder | ebda640 | 2013-05-10 16:29:22 -0500 | [diff] [blame] | 2656 | page_count = img_request->copyup_page_count; |
| 2657 | rbd_assert(page_count); |
| 2658 | img_request->copyup_page_count = 0; |
Alex Elder | 3d7efd1 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2659 | |
| 2660 | orig_request = img_request->obj_request; |
| 2661 | rbd_assert(orig_request != NULL); |
Alex Elder | b91f09f | 2013-05-10 16:29:22 -0500 | [diff] [blame] | 2662 | rbd_assert(obj_request_type_valid(orig_request->type)); |
Alex Elder | bbea1c1 | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 2663 | img_result = img_request->result; |
Alex Elder | ebda640 | 2013-05-10 16:29:22 -0500 | [diff] [blame] | 2664 | parent_length = img_request->length; |
| 2665 | rbd_assert(parent_length == img_request->xferred); |
Alex Elder | 3d7efd1 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2666 | rbd_img_request_put(img_request); |
| 2667 | |
Alex Elder | 91c6feb | 2013-05-06 17:40:32 -0500 | [diff] [blame] | 2668 | rbd_assert(orig_request->img_request); |
| 2669 | rbd_dev = orig_request->img_request->rbd_dev; |
Alex Elder | 3d7efd1 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2670 | rbd_assert(rbd_dev); |
Alex Elder | 3d7efd1 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2671 | |
Alex Elder | bbea1c1 | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 2672 | /* |
| 2673 | * If the overlap has become 0 (most likely because the |
| 2674 | * image has been flattened) we need to free the pages |
| 2675 | * and re-submit the original write request. |
| 2676 | */ |
| 2677 | if (!rbd_dev->parent_overlap) { |
| 2678 | struct ceph_osd_client *osdc; |
| 2679 | |
| 2680 | ceph_release_page_vector(pages, page_count); |
| 2681 | osdc = &rbd_dev->rbd_client->client->osdc; |
| 2682 | img_result = rbd_obj_request_submit(osdc, orig_request); |
| 2683 | if (!img_result) |
| 2684 | return; |
| 2685 | } |
| 2686 | |
| 2687 | if (img_result) |
Alex Elder | 0eefd47 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2688 | goto out_err; |
Alex Elder | 3d7efd1 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2689 | |
Alex Elder | 8785b1d | 2013-05-09 10:08:49 -0500 | [diff] [blame] | 2690 | /* |
| 2691 | * The original osd request is of no use to use any more. |
Ilya Dryomov | 0ccd592 | 2014-02-25 16:22:28 +0200 | [diff] [blame] | 2692 | * We need a new one that can hold the three ops in a copyup |
Alex Elder | 8785b1d | 2013-05-09 10:08:49 -0500 | [diff] [blame] | 2693 | * request. Allocate the new copyup osd request for the |
| 2694 | * original request, and release the old one. |
| 2695 | */ |
Alex Elder | bbea1c1 | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 2696 | img_result = -ENOMEM; |
Alex Elder | 0eefd47 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2697 | osd_req = rbd_osd_req_create_copyup(orig_request); |
| 2698 | if (!osd_req) |
| 2699 | goto out_err; |
Alex Elder | 8785b1d | 2013-05-09 10:08:49 -0500 | [diff] [blame] | 2700 | rbd_osd_req_destroy(orig_request->osd_req); |
Alex Elder | 0eefd47 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2701 | orig_request->osd_req = osd_req; |
| 2702 | orig_request->copyup_pages = pages; |
Alex Elder | ebda640 | 2013-05-10 16:29:22 -0500 | [diff] [blame] | 2703 | orig_request->copyup_page_count = page_count; |
Alex Elder | 3d7efd1 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2704 | |
Alex Elder | 0eefd47 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2705 | /* Initialize the copyup op */ |
| 2706 | |
| 2707 | osd_req_op_cls_init(osd_req, 0, CEPH_OSD_OP_CALL, "rbd", "copyup"); |
Alex Elder | ebda640 | 2013-05-10 16:29:22 -0500 | [diff] [blame] | 2708 | osd_req_op_cls_request_data_pages(osd_req, 0, pages, parent_length, 0, |
Alex Elder | 0eefd47 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2709 | false, false); |
| 2710 | |
Josh Durgin | d3246fb | 2014-04-07 16:49:21 -0700 | [diff] [blame] | 2711 | /* Add the other op(s) */ |
Ilya Dryomov | 0ccd592 | 2014-02-25 16:22:28 +0200 | [diff] [blame] | 2712 | |
Josh Durgin | d3246fb | 2014-04-07 16:49:21 -0700 | [diff] [blame] | 2713 | op_type = rbd_img_request_op_type(orig_request->img_request); |
| 2714 | rbd_img_obj_request_fill(orig_request, osd_req, op_type, 1); |
Alex Elder | 0eefd47 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2715 | |
| 2716 | /* All set, send it off. */ |
| 2717 | |
Alex Elder | 0eefd47 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2718 | osdc = &rbd_dev->rbd_client->client->osdc; |
Alex Elder | bbea1c1 | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 2719 | img_result = rbd_obj_request_submit(osdc, orig_request); |
| 2720 | if (!img_result) |
Alex Elder | 0eefd47 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2721 | return; |
| 2722 | out_err: |
| 2723 | /* Record the error code and complete the request */ |
| 2724 | |
Alex Elder | bbea1c1 | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 2725 | orig_request->result = img_result; |
Alex Elder | 0eefd47 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2726 | orig_request->xferred = 0; |
| 2727 | obj_request_done_set(orig_request); |
| 2728 | rbd_obj_request_complete(orig_request); |
Alex Elder | 3d7efd1 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2729 | } |
| 2730 | |
| 2731 | /* |
| 2732 | * Read from the parent image the range of data that covers the |
| 2733 | * entire target of the given object request. This is used for |
| 2734 | * satisfying a layered image write request when the target of an |
| 2735 | * object request from the image request does not exist. |
| 2736 | * |
| 2737 | * A page array big enough to hold the returned data is allocated |
| 2738 | * and supplied to rbd_img_request_fill() as the "data descriptor." |
| 2739 | * When the read completes, this page array will be transferred to |
| 2740 | * the original object request for the copyup operation. |
| 2741 | * |
| 2742 | * If an error occurs, record it as the result of the original |
| 2743 | * object request and mark it done so it gets completed. |
| 2744 | */ |
| 2745 | static int rbd_img_obj_parent_read_full(struct rbd_obj_request *obj_request) |
| 2746 | { |
| 2747 | struct rbd_img_request *img_request = NULL; |
| 2748 | struct rbd_img_request *parent_request = NULL; |
| 2749 | struct rbd_device *rbd_dev; |
| 2750 | u64 img_offset; |
| 2751 | u64 length; |
| 2752 | struct page **pages = NULL; |
| 2753 | u32 page_count; |
| 2754 | int result; |
| 2755 | |
| 2756 | rbd_assert(obj_request_img_data_test(obj_request)); |
Alex Elder | b91f09f | 2013-05-10 16:29:22 -0500 | [diff] [blame] | 2757 | rbd_assert(obj_request_type_valid(obj_request->type)); |
Alex Elder | 3d7efd1 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2758 | |
| 2759 | img_request = obj_request->img_request; |
| 2760 | rbd_assert(img_request != NULL); |
| 2761 | rbd_dev = img_request->rbd_dev; |
| 2762 | rbd_assert(rbd_dev->parent != NULL); |
| 2763 | |
| 2764 | /* |
| 2765 | * Determine the byte range covered by the object in the |
| 2766 | * child image to which the original request was to be sent. |
| 2767 | */ |
| 2768 | img_offset = obj_request->img_offset - obj_request->offset; |
| 2769 | length = (u64)1 << rbd_dev->header.obj_order; |
| 2770 | |
| 2771 | /* |
Alex Elder | a9e8ba2c | 2013-04-21 00:32:07 -0500 | [diff] [blame] | 2772 | * There is no defined parent data beyond the parent |
| 2773 | * overlap, so limit what we read at that boundary if |
| 2774 | * necessary. |
| 2775 | */ |
| 2776 | if (img_offset + length > rbd_dev->parent_overlap) { |
| 2777 | rbd_assert(img_offset < rbd_dev->parent_overlap); |
| 2778 | length = rbd_dev->parent_overlap - img_offset; |
| 2779 | } |
| 2780 | |
| 2781 | /* |
Alex Elder | 3d7efd1 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2782 | * Allocate a page array big enough to receive the data read |
| 2783 | * from the parent. |
| 2784 | */ |
| 2785 | page_count = (u32)calc_pages_for(0, length); |
| 2786 | pages = ceph_alloc_page_vector(page_count, GFP_KERNEL); |
| 2787 | if (IS_ERR(pages)) { |
| 2788 | result = PTR_ERR(pages); |
| 2789 | pages = NULL; |
| 2790 | goto out_err; |
| 2791 | } |
| 2792 | |
| 2793 | result = -ENOMEM; |
Alex Elder | e93f315 | 2013-05-08 22:50:04 -0500 | [diff] [blame] | 2794 | parent_request = rbd_parent_request_create(obj_request, |
| 2795 | img_offset, length); |
Alex Elder | 3d7efd1 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2796 | if (!parent_request) |
| 2797 | goto out_err; |
Alex Elder | 3d7efd1 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2798 | |
| 2799 | result = rbd_img_request_fill(parent_request, OBJ_REQUEST_PAGES, pages); |
| 2800 | if (result) |
| 2801 | goto out_err; |
| 2802 | parent_request->copyup_pages = pages; |
Alex Elder | ebda640 | 2013-05-10 16:29:22 -0500 | [diff] [blame] | 2803 | parent_request->copyup_page_count = page_count; |
Alex Elder | 3d7efd1 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2804 | |
| 2805 | parent_request->callback = rbd_img_obj_parent_read_full_callback; |
| 2806 | result = rbd_img_request_submit(parent_request); |
| 2807 | if (!result) |
| 2808 | return 0; |
| 2809 | |
| 2810 | parent_request->copyup_pages = NULL; |
Alex Elder | ebda640 | 2013-05-10 16:29:22 -0500 | [diff] [blame] | 2811 | parent_request->copyup_page_count = 0; |
Alex Elder | 3d7efd1 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2812 | parent_request->obj_request = NULL; |
| 2813 | rbd_obj_request_put(obj_request); |
| 2814 | out_err: |
| 2815 | if (pages) |
| 2816 | ceph_release_page_vector(pages, page_count); |
| 2817 | if (parent_request) |
| 2818 | rbd_img_request_put(parent_request); |
| 2819 | obj_request->result = result; |
| 2820 | obj_request->xferred = 0; |
| 2821 | obj_request_done_set(obj_request); |
| 2822 | |
| 2823 | return result; |
| 2824 | } |
| 2825 | |
Alex Elder | c5b5ef6 | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 2826 | static void rbd_img_obj_exists_callback(struct rbd_obj_request *obj_request) |
| 2827 | { |
Alex Elder | c5b5ef6 | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 2828 | struct rbd_obj_request *orig_request; |
Alex Elder | 638f5ab | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 2829 | struct rbd_device *rbd_dev; |
Alex Elder | c5b5ef6 | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 2830 | int result; |
| 2831 | |
| 2832 | rbd_assert(!obj_request_img_data_test(obj_request)); |
| 2833 | |
| 2834 | /* |
| 2835 | * All we need from the object request is the original |
| 2836 | * request and the result of the STAT op. Grab those, then |
| 2837 | * we're done with the request. |
| 2838 | */ |
| 2839 | orig_request = obj_request->obj_request; |
| 2840 | obj_request->obj_request = NULL; |
Alex Elder | 912c317 | 2013-05-13 20:35:38 -0500 | [diff] [blame] | 2841 | rbd_obj_request_put(orig_request); |
Alex Elder | c5b5ef6 | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 2842 | rbd_assert(orig_request); |
| 2843 | rbd_assert(orig_request->img_request); |
| 2844 | |
| 2845 | result = obj_request->result; |
| 2846 | obj_request->result = 0; |
| 2847 | |
| 2848 | dout("%s: obj %p for obj %p result %d %llu/%llu\n", __func__, |
| 2849 | obj_request, orig_request, result, |
| 2850 | obj_request->xferred, obj_request->length); |
| 2851 | rbd_obj_request_put(obj_request); |
| 2852 | |
Alex Elder | 638f5ab | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 2853 | /* |
| 2854 | * If the overlap has become 0 (most likely because the |
| 2855 | * image has been flattened) we need to free the pages |
| 2856 | * and re-submit the original write request. |
| 2857 | */ |
| 2858 | rbd_dev = orig_request->img_request->rbd_dev; |
| 2859 | if (!rbd_dev->parent_overlap) { |
| 2860 | struct ceph_osd_client *osdc; |
| 2861 | |
Alex Elder | 638f5ab | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 2862 | osdc = &rbd_dev->rbd_client->client->osdc; |
| 2863 | result = rbd_obj_request_submit(osdc, orig_request); |
| 2864 | if (!result) |
| 2865 | return; |
| 2866 | } |
Alex Elder | c5b5ef6 | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 2867 | |
| 2868 | /* |
| 2869 | * Our only purpose here is to determine whether the object |
| 2870 | * exists, and we don't want to treat the non-existence as |
| 2871 | * an error. If something else comes back, transfer the |
| 2872 | * error to the original request and complete it now. |
| 2873 | */ |
| 2874 | if (!result) { |
| 2875 | obj_request_existence_set(orig_request, true); |
| 2876 | } else if (result == -ENOENT) { |
| 2877 | obj_request_existence_set(orig_request, false); |
| 2878 | } else if (result) { |
| 2879 | orig_request->result = result; |
Alex Elder | 3d7efd1 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2880 | goto out; |
Alex Elder | c5b5ef6 | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 2881 | } |
| 2882 | |
| 2883 | /* |
| 2884 | * Resubmit the original request now that we have recorded |
| 2885 | * whether the target object exists. |
| 2886 | */ |
Alex Elder | b454e36 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2887 | orig_request->result = rbd_img_obj_request_submit(orig_request); |
Alex Elder | 3d7efd1 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2888 | out: |
Alex Elder | c5b5ef6 | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 2889 | if (orig_request->result) |
| 2890 | rbd_obj_request_complete(orig_request); |
Alex Elder | c5b5ef6 | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 2891 | } |
| 2892 | |
| 2893 | static int rbd_img_obj_exists_submit(struct rbd_obj_request *obj_request) |
| 2894 | { |
| 2895 | struct rbd_obj_request *stat_request; |
| 2896 | struct rbd_device *rbd_dev; |
| 2897 | struct ceph_osd_client *osdc; |
| 2898 | struct page **pages = NULL; |
| 2899 | u32 page_count; |
| 2900 | size_t size; |
| 2901 | int ret; |
| 2902 | |
| 2903 | /* |
| 2904 | * The response data for a STAT call consists of: |
| 2905 | * le64 length; |
| 2906 | * struct { |
| 2907 | * le32 tv_sec; |
| 2908 | * le32 tv_nsec; |
| 2909 | * } mtime; |
| 2910 | */ |
| 2911 | size = sizeof (__le64) + sizeof (__le32) + sizeof (__le32); |
| 2912 | page_count = (u32)calc_pages_for(0, size); |
| 2913 | pages = ceph_alloc_page_vector(page_count, GFP_KERNEL); |
| 2914 | if (IS_ERR(pages)) |
| 2915 | return PTR_ERR(pages); |
| 2916 | |
| 2917 | ret = -ENOMEM; |
| 2918 | stat_request = rbd_obj_request_create(obj_request->object_name, 0, 0, |
| 2919 | OBJ_REQUEST_PAGES); |
| 2920 | if (!stat_request) |
| 2921 | goto out; |
| 2922 | |
| 2923 | rbd_obj_request_get(obj_request); |
| 2924 | stat_request->obj_request = obj_request; |
| 2925 | stat_request->pages = pages; |
| 2926 | stat_request->page_count = page_count; |
| 2927 | |
| 2928 | rbd_assert(obj_request->img_request); |
| 2929 | rbd_dev = obj_request->img_request->rbd_dev; |
Guangliang Zhao | 6d2940c | 2014-03-13 11:21:35 +0800 | [diff] [blame] | 2930 | stat_request->osd_req = rbd_osd_req_create(rbd_dev, OBJ_OP_READ, 1, |
Ilya Dryomov | deb236b | 2014-02-25 16:22:27 +0200 | [diff] [blame] | 2931 | stat_request); |
Alex Elder | c5b5ef6 | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 2932 | if (!stat_request->osd_req) |
| 2933 | goto out; |
| 2934 | stat_request->callback = rbd_img_obj_exists_callback; |
| 2935 | |
Yan, Zheng | 144cba1 | 2015-04-27 11:09:54 +0800 | [diff] [blame] | 2936 | osd_req_op_init(stat_request->osd_req, 0, CEPH_OSD_OP_STAT, 0); |
Alex Elder | c5b5ef6 | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 2937 | osd_req_op_raw_data_in_pages(stat_request->osd_req, 0, pages, size, 0, |
| 2938 | false, false); |
Alex Elder | 9d4df01 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2939 | rbd_osd_req_format_read(stat_request); |
Alex Elder | c5b5ef6 | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 2940 | |
| 2941 | osdc = &rbd_dev->rbd_client->client->osdc; |
| 2942 | ret = rbd_obj_request_submit(osdc, stat_request); |
| 2943 | out: |
| 2944 | if (ret) |
| 2945 | rbd_obj_request_put(obj_request); |
| 2946 | |
| 2947 | return ret; |
| 2948 | } |
| 2949 | |
Ilya Dryomov | 70d045f | 2014-09-12 16:02:01 +0400 | [diff] [blame] | 2950 | static bool img_obj_request_simple(struct rbd_obj_request *obj_request) |
Alex Elder | b454e36 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2951 | { |
| 2952 | struct rbd_img_request *img_request; |
Alex Elder | a9e8ba2c | 2013-04-21 00:32:07 -0500 | [diff] [blame] | 2953 | struct rbd_device *rbd_dev; |
Alex Elder | b454e36 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2954 | |
| 2955 | rbd_assert(obj_request_img_data_test(obj_request)); |
| 2956 | |
| 2957 | img_request = obj_request->img_request; |
| 2958 | rbd_assert(img_request); |
Alex Elder | a9e8ba2c | 2013-04-21 00:32:07 -0500 | [diff] [blame] | 2959 | rbd_dev = img_request->rbd_dev; |
Alex Elder | b454e36 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2960 | |
Ilya Dryomov | 70d045f | 2014-09-12 16:02:01 +0400 | [diff] [blame] | 2961 | /* Reads */ |
Josh Durgin | 1c22088 | 2014-04-04 17:49:12 -0700 | [diff] [blame] | 2962 | if (!img_request_write_test(img_request) && |
| 2963 | !img_request_discard_test(img_request)) |
Ilya Dryomov | 70d045f | 2014-09-12 16:02:01 +0400 | [diff] [blame] | 2964 | return true; |
Alex Elder | b454e36 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2965 | |
Ilya Dryomov | 70d045f | 2014-09-12 16:02:01 +0400 | [diff] [blame] | 2966 | /* Non-layered writes */ |
| 2967 | if (!img_request_layered_test(img_request)) |
| 2968 | return true; |
| 2969 | |
| 2970 | /* |
| 2971 | * Layered writes outside of the parent overlap range don't |
| 2972 | * share any data with the parent. |
| 2973 | */ |
| 2974 | if (!obj_request_overlaps_parent(obj_request)) |
| 2975 | return true; |
| 2976 | |
| 2977 | /* |
Guangliang Zhao | c622d22 | 2014-04-01 22:22:15 +0800 | [diff] [blame] | 2978 | * Entire-object layered writes - we will overwrite whatever |
| 2979 | * parent data there is anyway. |
| 2980 | */ |
| 2981 | if (!obj_request->offset && |
| 2982 | obj_request->length == rbd_obj_bytes(&rbd_dev->header)) |
| 2983 | return true; |
| 2984 | |
| 2985 | /* |
Ilya Dryomov | 70d045f | 2014-09-12 16:02:01 +0400 | [diff] [blame] | 2986 | * If the object is known to already exist, its parent data has |
| 2987 | * already been copied. |
| 2988 | */ |
| 2989 | if (obj_request_known_test(obj_request) && |
| 2990 | obj_request_exists_test(obj_request)) |
| 2991 | return true; |
| 2992 | |
| 2993 | return false; |
| 2994 | } |
| 2995 | |
| 2996 | static int rbd_img_obj_request_submit(struct rbd_obj_request *obj_request) |
| 2997 | { |
| 2998 | if (img_obj_request_simple(obj_request)) { |
Alex Elder | b454e36 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2999 | struct rbd_device *rbd_dev; |
| 3000 | struct ceph_osd_client *osdc; |
| 3001 | |
| 3002 | rbd_dev = obj_request->img_request->rbd_dev; |
| 3003 | osdc = &rbd_dev->rbd_client->client->osdc; |
| 3004 | |
| 3005 | return rbd_obj_request_submit(osdc, obj_request); |
| 3006 | } |
| 3007 | |
| 3008 | /* |
Alex Elder | 3d7efd1 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 3009 | * It's a layered write. The target object might exist but |
| 3010 | * we may not know that yet. If we know it doesn't exist, |
| 3011 | * start by reading the data for the full target object from |
| 3012 | * the parent so we can use it for a copyup to the target. |
Alex Elder | b454e36 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 3013 | */ |
Ilya Dryomov | 70d045f | 2014-09-12 16:02:01 +0400 | [diff] [blame] | 3014 | if (obj_request_known_test(obj_request)) |
Alex Elder | 3d7efd1 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 3015 | return rbd_img_obj_parent_read_full(obj_request); |
| 3016 | |
| 3017 | /* We don't know whether the target exists. Go find out. */ |
Alex Elder | b454e36 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 3018 | |
| 3019 | return rbd_img_obj_exists_submit(obj_request); |
| 3020 | } |
| 3021 | |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 3022 | static int rbd_img_request_submit(struct rbd_img_request *img_request) |
| 3023 | { |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 3024 | struct rbd_obj_request *obj_request; |
Alex Elder | 46faeed | 2013-04-10 17:47:46 -0500 | [diff] [blame] | 3025 | struct rbd_obj_request *next_obj_request; |
Ilya Dryomov | 663ae2c | 2016-05-16 13:18:57 +0200 | [diff] [blame] | 3026 | int ret = 0; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 3027 | |
Alex Elder | 37206ee | 2013-02-20 17:32:08 -0600 | [diff] [blame] | 3028 | dout("%s: img %p\n", __func__, img_request); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 3029 | |
Ilya Dryomov | 663ae2c | 2016-05-16 13:18:57 +0200 | [diff] [blame] | 3030 | rbd_img_request_get(img_request); |
| 3031 | for_each_obj_request_safe(img_request, obj_request, next_obj_request) { |
Alex Elder | b454e36 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 3032 | ret = rbd_img_obj_request_submit(obj_request); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 3033 | if (ret) |
Ilya Dryomov | 663ae2c | 2016-05-16 13:18:57 +0200 | [diff] [blame] | 3034 | goto out_put_ireq; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 3035 | } |
| 3036 | |
Ilya Dryomov | 663ae2c | 2016-05-16 13:18:57 +0200 | [diff] [blame] | 3037 | out_put_ireq: |
| 3038 | rbd_img_request_put(img_request); |
| 3039 | return ret; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 3040 | } |
| 3041 | |
Alex Elder | 8b3e1a5 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 3042 | static void rbd_img_parent_read_callback(struct rbd_img_request *img_request) |
| 3043 | { |
| 3044 | struct rbd_obj_request *obj_request; |
Alex Elder | a9e8ba2c | 2013-04-21 00:32:07 -0500 | [diff] [blame] | 3045 | struct rbd_device *rbd_dev; |
| 3046 | u64 obj_end; |
Alex Elder | 02c74fb | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 3047 | u64 img_xferred; |
| 3048 | int img_result; |
Alex Elder | 8b3e1a5 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 3049 | |
| 3050 | rbd_assert(img_request_child_test(img_request)); |
| 3051 | |
Alex Elder | 02c74fb | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 3052 | /* First get what we need from the image request and release it */ |
| 3053 | |
Alex Elder | 8b3e1a5 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 3054 | obj_request = img_request->obj_request; |
Alex Elder | 02c74fb | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 3055 | img_xferred = img_request->xferred; |
| 3056 | img_result = img_request->result; |
| 3057 | rbd_img_request_put(img_request); |
| 3058 | |
| 3059 | /* |
| 3060 | * If the overlap has become 0 (most likely because the |
| 3061 | * image has been flattened) we need to re-submit the |
| 3062 | * original request. |
| 3063 | */ |
Alex Elder | a9e8ba2c | 2013-04-21 00:32:07 -0500 | [diff] [blame] | 3064 | rbd_assert(obj_request); |
| 3065 | rbd_assert(obj_request->img_request); |
Alex Elder | 02c74fb | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 3066 | rbd_dev = obj_request->img_request->rbd_dev; |
| 3067 | if (!rbd_dev->parent_overlap) { |
| 3068 | struct ceph_osd_client *osdc; |
Alex Elder | 8b3e1a5 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 3069 | |
Alex Elder | 02c74fb | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 3070 | osdc = &rbd_dev->rbd_client->client->osdc; |
| 3071 | img_result = rbd_obj_request_submit(osdc, obj_request); |
| 3072 | if (!img_result) |
| 3073 | return; |
| 3074 | } |
| 3075 | |
| 3076 | obj_request->result = img_result; |
Alex Elder | a9e8ba2c | 2013-04-21 00:32:07 -0500 | [diff] [blame] | 3077 | if (obj_request->result) |
| 3078 | goto out; |
| 3079 | |
| 3080 | /* |
| 3081 | * We need to zero anything beyond the parent overlap |
| 3082 | * boundary. Since rbd_img_obj_request_read_callback() |
| 3083 | * will zero anything beyond the end of a short read, an |
| 3084 | * easy way to do this is to pretend the data from the |
| 3085 | * parent came up short--ending at the overlap boundary. |
| 3086 | */ |
| 3087 | rbd_assert(obj_request->img_offset < U64_MAX - obj_request->length); |
| 3088 | obj_end = obj_request->img_offset + obj_request->length; |
Alex Elder | a9e8ba2c | 2013-04-21 00:32:07 -0500 | [diff] [blame] | 3089 | if (obj_end > rbd_dev->parent_overlap) { |
| 3090 | u64 xferred = 0; |
| 3091 | |
| 3092 | if (obj_request->img_offset < rbd_dev->parent_overlap) |
| 3093 | xferred = rbd_dev->parent_overlap - |
| 3094 | obj_request->img_offset; |
| 3095 | |
Alex Elder | 02c74fb | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 3096 | obj_request->xferred = min(img_xferred, xferred); |
Alex Elder | a9e8ba2c | 2013-04-21 00:32:07 -0500 | [diff] [blame] | 3097 | } else { |
Alex Elder | 02c74fb | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 3098 | obj_request->xferred = img_xferred; |
Alex Elder | a9e8ba2c | 2013-04-21 00:32:07 -0500 | [diff] [blame] | 3099 | } |
| 3100 | out: |
Alex Elder | 8b3e1a5 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 3101 | rbd_img_obj_request_read_callback(obj_request); |
| 3102 | rbd_obj_request_complete(obj_request); |
| 3103 | } |
| 3104 | |
| 3105 | static void rbd_img_parent_read(struct rbd_obj_request *obj_request) |
| 3106 | { |
Alex Elder | 8b3e1a5 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 3107 | struct rbd_img_request *img_request; |
| 3108 | int result; |
| 3109 | |
| 3110 | rbd_assert(obj_request_img_data_test(obj_request)); |
| 3111 | rbd_assert(obj_request->img_request != NULL); |
| 3112 | rbd_assert(obj_request->result == (s32) -ENOENT); |
Alex Elder | 5b2ab72 | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 3113 | rbd_assert(obj_request_type_valid(obj_request->type)); |
Alex Elder | 8b3e1a5 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 3114 | |
Alex Elder | 8b3e1a5 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 3115 | /* rbd_read_finish(obj_request, obj_request->length); */ |
Alex Elder | e93f315 | 2013-05-08 22:50:04 -0500 | [diff] [blame] | 3116 | img_request = rbd_parent_request_create(obj_request, |
Alex Elder | 8b3e1a5 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 3117 | obj_request->img_offset, |
Alex Elder | e93f315 | 2013-05-08 22:50:04 -0500 | [diff] [blame] | 3118 | obj_request->length); |
Alex Elder | 8b3e1a5 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 3119 | result = -ENOMEM; |
| 3120 | if (!img_request) |
| 3121 | goto out_err; |
| 3122 | |
Alex Elder | 5b2ab72 | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 3123 | if (obj_request->type == OBJ_REQUEST_BIO) |
| 3124 | result = rbd_img_request_fill(img_request, OBJ_REQUEST_BIO, |
| 3125 | obj_request->bio_list); |
| 3126 | else |
| 3127 | result = rbd_img_request_fill(img_request, OBJ_REQUEST_PAGES, |
| 3128 | obj_request->pages); |
Alex Elder | 8b3e1a5 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 3129 | if (result) |
| 3130 | goto out_err; |
| 3131 | |
| 3132 | img_request->callback = rbd_img_parent_read_callback; |
| 3133 | result = rbd_img_request_submit(img_request); |
| 3134 | if (result) |
| 3135 | goto out_err; |
| 3136 | |
| 3137 | return; |
| 3138 | out_err: |
| 3139 | if (img_request) |
| 3140 | rbd_img_request_put(img_request); |
| 3141 | obj_request->result = result; |
| 3142 | obj_request->xferred = 0; |
| 3143 | obj_request_done_set(obj_request); |
| 3144 | } |
| 3145 | |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3146 | static const struct rbd_client_id rbd_empty_cid; |
| 3147 | |
| 3148 | static bool rbd_cid_equal(const struct rbd_client_id *lhs, |
| 3149 | const struct rbd_client_id *rhs) |
| 3150 | { |
| 3151 | return lhs->gid == rhs->gid && lhs->handle == rhs->handle; |
| 3152 | } |
| 3153 | |
| 3154 | static struct rbd_client_id rbd_get_cid(struct rbd_device *rbd_dev) |
| 3155 | { |
| 3156 | struct rbd_client_id cid; |
| 3157 | |
| 3158 | mutex_lock(&rbd_dev->watch_mutex); |
| 3159 | cid.gid = ceph_client_gid(rbd_dev->rbd_client->client); |
| 3160 | cid.handle = rbd_dev->watch_cookie; |
| 3161 | mutex_unlock(&rbd_dev->watch_mutex); |
| 3162 | return cid; |
| 3163 | } |
| 3164 | |
| 3165 | /* |
| 3166 | * lock_rwsem must be held for write |
| 3167 | */ |
| 3168 | static void rbd_set_owner_cid(struct rbd_device *rbd_dev, |
| 3169 | const struct rbd_client_id *cid) |
| 3170 | { |
| 3171 | dout("%s rbd_dev %p %llu-%llu -> %llu-%llu\n", __func__, rbd_dev, |
| 3172 | rbd_dev->owner_cid.gid, rbd_dev->owner_cid.handle, |
| 3173 | cid->gid, cid->handle); |
| 3174 | rbd_dev->owner_cid = *cid; /* struct */ |
| 3175 | } |
| 3176 | |
| 3177 | static void format_lock_cookie(struct rbd_device *rbd_dev, char *buf) |
| 3178 | { |
| 3179 | mutex_lock(&rbd_dev->watch_mutex); |
| 3180 | sprintf(buf, "%s %llu", RBD_LOCK_COOKIE_PREFIX, rbd_dev->watch_cookie); |
| 3181 | mutex_unlock(&rbd_dev->watch_mutex); |
| 3182 | } |
| 3183 | |
| 3184 | /* |
| 3185 | * lock_rwsem must be held for write |
| 3186 | */ |
| 3187 | static int rbd_lock(struct rbd_device *rbd_dev) |
| 3188 | { |
| 3189 | struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc; |
| 3190 | struct rbd_client_id cid = rbd_get_cid(rbd_dev); |
| 3191 | char cookie[32]; |
| 3192 | int ret; |
| 3193 | |
| 3194 | WARN_ON(__rbd_is_lock_owner(rbd_dev)); |
| 3195 | |
| 3196 | format_lock_cookie(rbd_dev, cookie); |
| 3197 | ret = ceph_cls_lock(osdc, &rbd_dev->header_oid, &rbd_dev->header_oloc, |
| 3198 | RBD_LOCK_NAME, CEPH_CLS_LOCK_EXCLUSIVE, cookie, |
| 3199 | RBD_LOCK_TAG, "", 0); |
| 3200 | if (ret) |
| 3201 | return ret; |
| 3202 | |
| 3203 | rbd_dev->lock_state = RBD_LOCK_STATE_LOCKED; |
| 3204 | rbd_set_owner_cid(rbd_dev, &cid); |
| 3205 | queue_work(rbd_dev->task_wq, &rbd_dev->acquired_lock_work); |
| 3206 | return 0; |
| 3207 | } |
| 3208 | |
| 3209 | /* |
| 3210 | * lock_rwsem must be held for write |
| 3211 | */ |
| 3212 | static int rbd_unlock(struct rbd_device *rbd_dev) |
| 3213 | { |
| 3214 | struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc; |
| 3215 | char cookie[32]; |
| 3216 | int ret; |
| 3217 | |
| 3218 | WARN_ON(!__rbd_is_lock_owner(rbd_dev)); |
| 3219 | |
| 3220 | rbd_dev->lock_state = RBD_LOCK_STATE_UNLOCKED; |
| 3221 | |
| 3222 | format_lock_cookie(rbd_dev, cookie); |
| 3223 | ret = ceph_cls_unlock(osdc, &rbd_dev->header_oid, &rbd_dev->header_oloc, |
| 3224 | RBD_LOCK_NAME, cookie); |
| 3225 | if (ret && ret != -ENOENT) { |
| 3226 | rbd_warn(rbd_dev, "cls_unlock failed: %d", ret); |
| 3227 | return ret; |
| 3228 | } |
| 3229 | |
| 3230 | rbd_set_owner_cid(rbd_dev, &rbd_empty_cid); |
| 3231 | queue_work(rbd_dev->task_wq, &rbd_dev->released_lock_work); |
| 3232 | return 0; |
| 3233 | } |
| 3234 | |
| 3235 | static int __rbd_notify_op_lock(struct rbd_device *rbd_dev, |
| 3236 | enum rbd_notify_op notify_op, |
| 3237 | struct page ***preply_pages, |
| 3238 | size_t *preply_len) |
| 3239 | { |
| 3240 | struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc; |
| 3241 | struct rbd_client_id cid = rbd_get_cid(rbd_dev); |
| 3242 | int buf_size = 4 + 8 + 8 + CEPH_ENCODING_START_BLK_LEN; |
| 3243 | char buf[buf_size]; |
| 3244 | void *p = buf; |
| 3245 | |
| 3246 | dout("%s rbd_dev %p notify_op %d\n", __func__, rbd_dev, notify_op); |
| 3247 | |
| 3248 | /* encode *LockPayload NotifyMessage (op + ClientId) */ |
| 3249 | ceph_start_encoding(&p, 2, 1, buf_size - CEPH_ENCODING_START_BLK_LEN); |
| 3250 | ceph_encode_32(&p, notify_op); |
| 3251 | ceph_encode_64(&p, cid.gid); |
| 3252 | ceph_encode_64(&p, cid.handle); |
| 3253 | |
| 3254 | return ceph_osdc_notify(osdc, &rbd_dev->header_oid, |
| 3255 | &rbd_dev->header_oloc, buf, buf_size, |
| 3256 | RBD_NOTIFY_TIMEOUT, preply_pages, preply_len); |
| 3257 | } |
| 3258 | |
| 3259 | static void rbd_notify_op_lock(struct rbd_device *rbd_dev, |
| 3260 | enum rbd_notify_op notify_op) |
| 3261 | { |
| 3262 | struct page **reply_pages; |
| 3263 | size_t reply_len; |
| 3264 | |
| 3265 | __rbd_notify_op_lock(rbd_dev, notify_op, &reply_pages, &reply_len); |
| 3266 | ceph_release_page_vector(reply_pages, calc_pages_for(0, reply_len)); |
| 3267 | } |
| 3268 | |
| 3269 | static void rbd_notify_acquired_lock(struct work_struct *work) |
| 3270 | { |
| 3271 | struct rbd_device *rbd_dev = container_of(work, struct rbd_device, |
| 3272 | acquired_lock_work); |
| 3273 | |
| 3274 | rbd_notify_op_lock(rbd_dev, RBD_NOTIFY_OP_ACQUIRED_LOCK); |
| 3275 | } |
| 3276 | |
| 3277 | static void rbd_notify_released_lock(struct work_struct *work) |
| 3278 | { |
| 3279 | struct rbd_device *rbd_dev = container_of(work, struct rbd_device, |
| 3280 | released_lock_work); |
| 3281 | |
| 3282 | rbd_notify_op_lock(rbd_dev, RBD_NOTIFY_OP_RELEASED_LOCK); |
| 3283 | } |
| 3284 | |
| 3285 | static int rbd_request_lock(struct rbd_device *rbd_dev) |
| 3286 | { |
| 3287 | struct page **reply_pages; |
| 3288 | size_t reply_len; |
| 3289 | bool lock_owner_responded = false; |
| 3290 | int ret; |
| 3291 | |
| 3292 | dout("%s rbd_dev %p\n", __func__, rbd_dev); |
| 3293 | |
| 3294 | ret = __rbd_notify_op_lock(rbd_dev, RBD_NOTIFY_OP_REQUEST_LOCK, |
| 3295 | &reply_pages, &reply_len); |
| 3296 | if (ret && ret != -ETIMEDOUT) { |
| 3297 | rbd_warn(rbd_dev, "failed to request lock: %d", ret); |
| 3298 | goto out; |
| 3299 | } |
| 3300 | |
| 3301 | if (reply_len > 0 && reply_len <= PAGE_SIZE) { |
| 3302 | void *p = page_address(reply_pages[0]); |
| 3303 | void *const end = p + reply_len; |
| 3304 | u32 n; |
| 3305 | |
| 3306 | ceph_decode_32_safe(&p, end, n, e_inval); /* num_acks */ |
| 3307 | while (n--) { |
| 3308 | u8 struct_v; |
| 3309 | u32 len; |
| 3310 | |
| 3311 | ceph_decode_need(&p, end, 8 + 8, e_inval); |
| 3312 | p += 8 + 8; /* skip gid and cookie */ |
| 3313 | |
| 3314 | ceph_decode_32_safe(&p, end, len, e_inval); |
| 3315 | if (!len) |
| 3316 | continue; |
| 3317 | |
| 3318 | if (lock_owner_responded) { |
| 3319 | rbd_warn(rbd_dev, |
| 3320 | "duplicate lock owners detected"); |
| 3321 | ret = -EIO; |
| 3322 | goto out; |
| 3323 | } |
| 3324 | |
| 3325 | lock_owner_responded = true; |
| 3326 | ret = ceph_start_decoding(&p, end, 1, "ResponseMessage", |
| 3327 | &struct_v, &len); |
| 3328 | if (ret) { |
| 3329 | rbd_warn(rbd_dev, |
| 3330 | "failed to decode ResponseMessage: %d", |
| 3331 | ret); |
| 3332 | goto e_inval; |
| 3333 | } |
| 3334 | |
| 3335 | ret = ceph_decode_32(&p); |
| 3336 | } |
| 3337 | } |
| 3338 | |
| 3339 | if (!lock_owner_responded) { |
| 3340 | rbd_warn(rbd_dev, "no lock owners detected"); |
| 3341 | ret = -ETIMEDOUT; |
| 3342 | } |
| 3343 | |
| 3344 | out: |
| 3345 | ceph_release_page_vector(reply_pages, calc_pages_for(0, reply_len)); |
| 3346 | return ret; |
| 3347 | |
| 3348 | e_inval: |
| 3349 | ret = -EINVAL; |
| 3350 | goto out; |
| 3351 | } |
| 3352 | |
| 3353 | static void wake_requests(struct rbd_device *rbd_dev, bool wake_all) |
| 3354 | { |
| 3355 | dout("%s rbd_dev %p wake_all %d\n", __func__, rbd_dev, wake_all); |
| 3356 | |
| 3357 | cancel_delayed_work(&rbd_dev->lock_dwork); |
| 3358 | if (wake_all) |
| 3359 | wake_up_all(&rbd_dev->lock_waitq); |
| 3360 | else |
| 3361 | wake_up(&rbd_dev->lock_waitq); |
| 3362 | } |
| 3363 | |
| 3364 | static int get_lock_owner_info(struct rbd_device *rbd_dev, |
| 3365 | struct ceph_locker **lockers, u32 *num_lockers) |
| 3366 | { |
| 3367 | struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc; |
| 3368 | u8 lock_type; |
| 3369 | char *lock_tag; |
| 3370 | int ret; |
| 3371 | |
| 3372 | dout("%s rbd_dev %p\n", __func__, rbd_dev); |
| 3373 | |
| 3374 | ret = ceph_cls_lock_info(osdc, &rbd_dev->header_oid, |
| 3375 | &rbd_dev->header_oloc, RBD_LOCK_NAME, |
| 3376 | &lock_type, &lock_tag, lockers, num_lockers); |
| 3377 | if (ret) |
| 3378 | return ret; |
| 3379 | |
| 3380 | if (*num_lockers == 0) { |
| 3381 | dout("%s rbd_dev %p no lockers detected\n", __func__, rbd_dev); |
| 3382 | goto out; |
| 3383 | } |
| 3384 | |
| 3385 | if (strcmp(lock_tag, RBD_LOCK_TAG)) { |
| 3386 | rbd_warn(rbd_dev, "locked by external mechanism, tag %s", |
| 3387 | lock_tag); |
| 3388 | ret = -EBUSY; |
| 3389 | goto out; |
| 3390 | } |
| 3391 | |
| 3392 | if (lock_type == CEPH_CLS_LOCK_SHARED) { |
| 3393 | rbd_warn(rbd_dev, "shared lock type detected"); |
| 3394 | ret = -EBUSY; |
| 3395 | goto out; |
| 3396 | } |
| 3397 | |
| 3398 | if (strncmp((*lockers)[0].id.cookie, RBD_LOCK_COOKIE_PREFIX, |
| 3399 | strlen(RBD_LOCK_COOKIE_PREFIX))) { |
| 3400 | rbd_warn(rbd_dev, "locked by external mechanism, cookie %s", |
| 3401 | (*lockers)[0].id.cookie); |
| 3402 | ret = -EBUSY; |
| 3403 | goto out; |
| 3404 | } |
| 3405 | |
| 3406 | out: |
| 3407 | kfree(lock_tag); |
| 3408 | return ret; |
| 3409 | } |
| 3410 | |
| 3411 | static int find_watcher(struct rbd_device *rbd_dev, |
| 3412 | const struct ceph_locker *locker) |
| 3413 | { |
| 3414 | struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc; |
| 3415 | struct ceph_watch_item *watchers; |
| 3416 | u32 num_watchers; |
| 3417 | u64 cookie; |
| 3418 | int i; |
| 3419 | int ret; |
| 3420 | |
| 3421 | ret = ceph_osdc_list_watchers(osdc, &rbd_dev->header_oid, |
| 3422 | &rbd_dev->header_oloc, &watchers, |
| 3423 | &num_watchers); |
| 3424 | if (ret) |
| 3425 | return ret; |
| 3426 | |
| 3427 | sscanf(locker->id.cookie, RBD_LOCK_COOKIE_PREFIX " %llu", &cookie); |
| 3428 | for (i = 0; i < num_watchers; i++) { |
| 3429 | if (!memcmp(&watchers[i].addr, &locker->info.addr, |
| 3430 | sizeof(locker->info.addr)) && |
| 3431 | watchers[i].cookie == cookie) { |
| 3432 | struct rbd_client_id cid = { |
| 3433 | .gid = le64_to_cpu(watchers[i].name.num), |
| 3434 | .handle = cookie, |
| 3435 | }; |
| 3436 | |
| 3437 | dout("%s rbd_dev %p found cid %llu-%llu\n", __func__, |
| 3438 | rbd_dev, cid.gid, cid.handle); |
| 3439 | rbd_set_owner_cid(rbd_dev, &cid); |
| 3440 | ret = 1; |
| 3441 | goto out; |
| 3442 | } |
| 3443 | } |
| 3444 | |
| 3445 | dout("%s rbd_dev %p no watchers\n", __func__, rbd_dev); |
| 3446 | ret = 0; |
| 3447 | out: |
| 3448 | kfree(watchers); |
| 3449 | return ret; |
| 3450 | } |
| 3451 | |
| 3452 | /* |
| 3453 | * lock_rwsem must be held for write |
| 3454 | */ |
| 3455 | static int rbd_try_lock(struct rbd_device *rbd_dev) |
| 3456 | { |
| 3457 | struct ceph_client *client = rbd_dev->rbd_client->client; |
| 3458 | struct ceph_locker *lockers; |
| 3459 | u32 num_lockers; |
| 3460 | int ret; |
| 3461 | |
| 3462 | for (;;) { |
| 3463 | ret = rbd_lock(rbd_dev); |
| 3464 | if (ret != -EBUSY) |
| 3465 | return ret; |
| 3466 | |
| 3467 | /* determine if the current lock holder is still alive */ |
| 3468 | ret = get_lock_owner_info(rbd_dev, &lockers, &num_lockers); |
| 3469 | if (ret) |
| 3470 | return ret; |
| 3471 | |
| 3472 | if (num_lockers == 0) |
| 3473 | goto again; |
| 3474 | |
| 3475 | ret = find_watcher(rbd_dev, lockers); |
| 3476 | if (ret) { |
| 3477 | if (ret > 0) |
| 3478 | ret = 0; /* have to request lock */ |
| 3479 | goto out; |
| 3480 | } |
| 3481 | |
| 3482 | rbd_warn(rbd_dev, "%s%llu seems dead, breaking lock", |
| 3483 | ENTITY_NAME(lockers[0].id.name)); |
| 3484 | |
| 3485 | ret = ceph_monc_blacklist_add(&client->monc, |
| 3486 | &lockers[0].info.addr); |
| 3487 | if (ret) { |
| 3488 | rbd_warn(rbd_dev, "blacklist of %s%llu failed: %d", |
| 3489 | ENTITY_NAME(lockers[0].id.name), ret); |
| 3490 | goto out; |
| 3491 | } |
| 3492 | |
| 3493 | ret = ceph_cls_break_lock(&client->osdc, &rbd_dev->header_oid, |
| 3494 | &rbd_dev->header_oloc, RBD_LOCK_NAME, |
| 3495 | lockers[0].id.cookie, |
| 3496 | &lockers[0].id.name); |
| 3497 | if (ret && ret != -ENOENT) |
| 3498 | goto out; |
| 3499 | |
| 3500 | again: |
| 3501 | ceph_free_lockers(lockers, num_lockers); |
| 3502 | } |
| 3503 | |
| 3504 | out: |
| 3505 | ceph_free_lockers(lockers, num_lockers); |
| 3506 | return ret; |
| 3507 | } |
| 3508 | |
| 3509 | /* |
| 3510 | * ret is set only if lock_state is RBD_LOCK_STATE_UNLOCKED |
| 3511 | */ |
| 3512 | static enum rbd_lock_state rbd_try_acquire_lock(struct rbd_device *rbd_dev, |
| 3513 | int *pret) |
| 3514 | { |
| 3515 | enum rbd_lock_state lock_state; |
| 3516 | |
| 3517 | down_read(&rbd_dev->lock_rwsem); |
| 3518 | dout("%s rbd_dev %p read lock_state %d\n", __func__, rbd_dev, |
| 3519 | rbd_dev->lock_state); |
| 3520 | if (__rbd_is_lock_owner(rbd_dev)) { |
| 3521 | lock_state = rbd_dev->lock_state; |
| 3522 | up_read(&rbd_dev->lock_rwsem); |
| 3523 | return lock_state; |
| 3524 | } |
| 3525 | |
| 3526 | up_read(&rbd_dev->lock_rwsem); |
| 3527 | down_write(&rbd_dev->lock_rwsem); |
| 3528 | dout("%s rbd_dev %p write lock_state %d\n", __func__, rbd_dev, |
| 3529 | rbd_dev->lock_state); |
| 3530 | if (!__rbd_is_lock_owner(rbd_dev)) { |
| 3531 | *pret = rbd_try_lock(rbd_dev); |
| 3532 | if (*pret) |
| 3533 | rbd_warn(rbd_dev, "failed to acquire lock: %d", *pret); |
| 3534 | } |
| 3535 | |
| 3536 | lock_state = rbd_dev->lock_state; |
| 3537 | up_write(&rbd_dev->lock_rwsem); |
| 3538 | return lock_state; |
| 3539 | } |
| 3540 | |
| 3541 | static void rbd_acquire_lock(struct work_struct *work) |
| 3542 | { |
| 3543 | struct rbd_device *rbd_dev = container_of(to_delayed_work(work), |
| 3544 | struct rbd_device, lock_dwork); |
| 3545 | enum rbd_lock_state lock_state; |
| 3546 | int ret; |
| 3547 | |
| 3548 | dout("%s rbd_dev %p\n", __func__, rbd_dev); |
| 3549 | again: |
| 3550 | lock_state = rbd_try_acquire_lock(rbd_dev, &ret); |
| 3551 | if (lock_state != RBD_LOCK_STATE_UNLOCKED || ret == -EBLACKLISTED) { |
| 3552 | if (lock_state == RBD_LOCK_STATE_LOCKED) |
| 3553 | wake_requests(rbd_dev, true); |
| 3554 | dout("%s rbd_dev %p lock_state %d ret %d - done\n", __func__, |
| 3555 | rbd_dev, lock_state, ret); |
| 3556 | return; |
| 3557 | } |
| 3558 | |
| 3559 | ret = rbd_request_lock(rbd_dev); |
| 3560 | if (ret == -ETIMEDOUT) { |
| 3561 | goto again; /* treat this as a dead client */ |
| 3562 | } else if (ret < 0) { |
| 3563 | rbd_warn(rbd_dev, "error requesting lock: %d", ret); |
| 3564 | mod_delayed_work(rbd_dev->task_wq, &rbd_dev->lock_dwork, |
| 3565 | RBD_RETRY_DELAY); |
| 3566 | } else { |
| 3567 | /* |
| 3568 | * lock owner acked, but resend if we don't see them |
| 3569 | * release the lock |
| 3570 | */ |
| 3571 | dout("%s rbd_dev %p requeueing lock_dwork\n", __func__, |
| 3572 | rbd_dev); |
| 3573 | mod_delayed_work(rbd_dev->task_wq, &rbd_dev->lock_dwork, |
| 3574 | msecs_to_jiffies(2 * RBD_NOTIFY_TIMEOUT * MSEC_PER_SEC)); |
| 3575 | } |
| 3576 | } |
| 3577 | |
| 3578 | /* |
| 3579 | * lock_rwsem must be held for write |
| 3580 | */ |
| 3581 | static bool rbd_release_lock(struct rbd_device *rbd_dev) |
| 3582 | { |
| 3583 | dout("%s rbd_dev %p read lock_state %d\n", __func__, rbd_dev, |
| 3584 | rbd_dev->lock_state); |
| 3585 | if (rbd_dev->lock_state != RBD_LOCK_STATE_LOCKED) |
| 3586 | return false; |
| 3587 | |
| 3588 | rbd_dev->lock_state = RBD_LOCK_STATE_RELEASING; |
| 3589 | downgrade_write(&rbd_dev->lock_rwsem); |
| 3590 | /* |
| 3591 | * Ensure that all in-flight IO is flushed. |
| 3592 | * |
| 3593 | * FIXME: ceph_osdc_sync() flushes the entire OSD client, which |
| 3594 | * may be shared with other devices. |
| 3595 | */ |
| 3596 | ceph_osdc_sync(&rbd_dev->rbd_client->client->osdc); |
| 3597 | up_read(&rbd_dev->lock_rwsem); |
| 3598 | |
| 3599 | down_write(&rbd_dev->lock_rwsem); |
| 3600 | dout("%s rbd_dev %p write lock_state %d\n", __func__, rbd_dev, |
| 3601 | rbd_dev->lock_state); |
| 3602 | if (rbd_dev->lock_state != RBD_LOCK_STATE_RELEASING) |
| 3603 | return false; |
| 3604 | |
| 3605 | if (!rbd_unlock(rbd_dev)) |
| 3606 | /* |
| 3607 | * Give others a chance to grab the lock - we would re-acquire |
| 3608 | * almost immediately if we got new IO during ceph_osdc_sync() |
| 3609 | * otherwise. We need to ack our own notifications, so this |
| 3610 | * lock_dwork will be requeued from rbd_wait_state_locked() |
| 3611 | * after wake_requests() in rbd_handle_released_lock(). |
| 3612 | */ |
| 3613 | cancel_delayed_work(&rbd_dev->lock_dwork); |
| 3614 | |
| 3615 | return true; |
| 3616 | } |
| 3617 | |
| 3618 | static void rbd_release_lock_work(struct work_struct *work) |
| 3619 | { |
| 3620 | struct rbd_device *rbd_dev = container_of(work, struct rbd_device, |
| 3621 | unlock_work); |
| 3622 | |
| 3623 | down_write(&rbd_dev->lock_rwsem); |
| 3624 | rbd_release_lock(rbd_dev); |
| 3625 | up_write(&rbd_dev->lock_rwsem); |
| 3626 | } |
| 3627 | |
| 3628 | static void rbd_handle_acquired_lock(struct rbd_device *rbd_dev, u8 struct_v, |
| 3629 | void **p) |
| 3630 | { |
| 3631 | struct rbd_client_id cid = { 0 }; |
| 3632 | |
| 3633 | if (struct_v >= 2) { |
| 3634 | cid.gid = ceph_decode_64(p); |
| 3635 | cid.handle = ceph_decode_64(p); |
| 3636 | } |
| 3637 | |
| 3638 | dout("%s rbd_dev %p cid %llu-%llu\n", __func__, rbd_dev, cid.gid, |
| 3639 | cid.handle); |
| 3640 | if (!rbd_cid_equal(&cid, &rbd_empty_cid)) { |
| 3641 | down_write(&rbd_dev->lock_rwsem); |
| 3642 | if (rbd_cid_equal(&cid, &rbd_dev->owner_cid)) { |
| 3643 | /* |
| 3644 | * we already know that the remote client is |
| 3645 | * the owner |
| 3646 | */ |
| 3647 | up_write(&rbd_dev->lock_rwsem); |
| 3648 | return; |
| 3649 | } |
| 3650 | |
| 3651 | rbd_set_owner_cid(rbd_dev, &cid); |
| 3652 | downgrade_write(&rbd_dev->lock_rwsem); |
| 3653 | } else { |
| 3654 | down_read(&rbd_dev->lock_rwsem); |
| 3655 | } |
| 3656 | |
| 3657 | if (!__rbd_is_lock_owner(rbd_dev)) |
| 3658 | wake_requests(rbd_dev, false); |
| 3659 | up_read(&rbd_dev->lock_rwsem); |
| 3660 | } |
| 3661 | |
| 3662 | static void rbd_handle_released_lock(struct rbd_device *rbd_dev, u8 struct_v, |
| 3663 | void **p) |
| 3664 | { |
| 3665 | struct rbd_client_id cid = { 0 }; |
| 3666 | |
| 3667 | if (struct_v >= 2) { |
| 3668 | cid.gid = ceph_decode_64(p); |
| 3669 | cid.handle = ceph_decode_64(p); |
| 3670 | } |
| 3671 | |
| 3672 | dout("%s rbd_dev %p cid %llu-%llu\n", __func__, rbd_dev, cid.gid, |
| 3673 | cid.handle); |
| 3674 | if (!rbd_cid_equal(&cid, &rbd_empty_cid)) { |
| 3675 | down_write(&rbd_dev->lock_rwsem); |
| 3676 | if (!rbd_cid_equal(&cid, &rbd_dev->owner_cid)) { |
| 3677 | dout("%s rbd_dev %p unexpected owner, cid %llu-%llu != owner_cid %llu-%llu\n", |
| 3678 | __func__, rbd_dev, cid.gid, cid.handle, |
| 3679 | rbd_dev->owner_cid.gid, rbd_dev->owner_cid.handle); |
| 3680 | up_write(&rbd_dev->lock_rwsem); |
| 3681 | return; |
| 3682 | } |
| 3683 | |
| 3684 | rbd_set_owner_cid(rbd_dev, &rbd_empty_cid); |
| 3685 | downgrade_write(&rbd_dev->lock_rwsem); |
| 3686 | } else { |
| 3687 | down_read(&rbd_dev->lock_rwsem); |
| 3688 | } |
| 3689 | |
| 3690 | if (!__rbd_is_lock_owner(rbd_dev)) |
| 3691 | wake_requests(rbd_dev, false); |
| 3692 | up_read(&rbd_dev->lock_rwsem); |
| 3693 | } |
| 3694 | |
| 3695 | static bool rbd_handle_request_lock(struct rbd_device *rbd_dev, u8 struct_v, |
| 3696 | void **p) |
| 3697 | { |
| 3698 | struct rbd_client_id my_cid = rbd_get_cid(rbd_dev); |
| 3699 | struct rbd_client_id cid = { 0 }; |
| 3700 | bool need_to_send; |
| 3701 | |
| 3702 | if (struct_v >= 2) { |
| 3703 | cid.gid = ceph_decode_64(p); |
| 3704 | cid.handle = ceph_decode_64(p); |
| 3705 | } |
| 3706 | |
| 3707 | dout("%s rbd_dev %p cid %llu-%llu\n", __func__, rbd_dev, cid.gid, |
| 3708 | cid.handle); |
| 3709 | if (rbd_cid_equal(&cid, &my_cid)) |
| 3710 | return false; |
| 3711 | |
| 3712 | down_read(&rbd_dev->lock_rwsem); |
| 3713 | need_to_send = __rbd_is_lock_owner(rbd_dev); |
| 3714 | if (rbd_dev->lock_state == RBD_LOCK_STATE_LOCKED) { |
| 3715 | if (!rbd_cid_equal(&rbd_dev->owner_cid, &rbd_empty_cid)) { |
| 3716 | dout("%s rbd_dev %p queueing unlock_work\n", __func__, |
| 3717 | rbd_dev); |
| 3718 | queue_work(rbd_dev->task_wq, &rbd_dev->unlock_work); |
| 3719 | } |
| 3720 | } |
| 3721 | up_read(&rbd_dev->lock_rwsem); |
| 3722 | return need_to_send; |
| 3723 | } |
| 3724 | |
| 3725 | static void __rbd_acknowledge_notify(struct rbd_device *rbd_dev, |
| 3726 | u64 notify_id, u64 cookie, s32 *result) |
| 3727 | { |
| 3728 | struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc; |
| 3729 | int buf_size = 4 + CEPH_ENCODING_START_BLK_LEN; |
| 3730 | char buf[buf_size]; |
| 3731 | int ret; |
| 3732 | |
| 3733 | if (result) { |
| 3734 | void *p = buf; |
| 3735 | |
| 3736 | /* encode ResponseMessage */ |
| 3737 | ceph_start_encoding(&p, 1, 1, |
| 3738 | buf_size - CEPH_ENCODING_START_BLK_LEN); |
| 3739 | ceph_encode_32(&p, *result); |
| 3740 | } else { |
| 3741 | buf_size = 0; |
| 3742 | } |
| 3743 | |
| 3744 | ret = ceph_osdc_notify_ack(osdc, &rbd_dev->header_oid, |
| 3745 | &rbd_dev->header_oloc, notify_id, cookie, |
| 3746 | buf, buf_size); |
| 3747 | if (ret) |
| 3748 | rbd_warn(rbd_dev, "acknowledge_notify failed: %d", ret); |
| 3749 | } |
| 3750 | |
| 3751 | static void rbd_acknowledge_notify(struct rbd_device *rbd_dev, u64 notify_id, |
| 3752 | u64 cookie) |
| 3753 | { |
| 3754 | dout("%s rbd_dev %p\n", __func__, rbd_dev); |
| 3755 | __rbd_acknowledge_notify(rbd_dev, notify_id, cookie, NULL); |
| 3756 | } |
| 3757 | |
| 3758 | static void rbd_acknowledge_notify_result(struct rbd_device *rbd_dev, |
| 3759 | u64 notify_id, u64 cookie, s32 result) |
| 3760 | { |
| 3761 | dout("%s rbd_dev %p result %d\n", __func__, rbd_dev, result); |
| 3762 | __rbd_acknowledge_notify(rbd_dev, notify_id, cookie, &result); |
| 3763 | } |
| 3764 | |
Ilya Dryomov | 922dab6 | 2016-05-26 01:15:02 +0200 | [diff] [blame] | 3765 | static void rbd_watch_cb(void *arg, u64 notify_id, u64 cookie, |
| 3766 | u64 notifier_id, void *data, size_t data_len) |
Alex Elder | b8d7003 | 2012-11-30 17:53:04 -0600 | [diff] [blame] | 3767 | { |
Ilya Dryomov | 922dab6 | 2016-05-26 01:15:02 +0200 | [diff] [blame] | 3768 | struct rbd_device *rbd_dev = arg; |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3769 | void *p = data; |
| 3770 | void *const end = p + data_len; |
| 3771 | u8 struct_v; |
| 3772 | u32 len; |
| 3773 | u32 notify_op; |
Alex Elder | b8d7003 | 2012-11-30 17:53:04 -0600 | [diff] [blame] | 3774 | int ret; |
| 3775 | |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3776 | dout("%s rbd_dev %p cookie %llu notify_id %llu data_len %zu\n", |
| 3777 | __func__, rbd_dev, cookie, notify_id, data_len); |
| 3778 | if (data_len) { |
| 3779 | ret = ceph_start_decoding(&p, end, 1, "NotifyMessage", |
| 3780 | &struct_v, &len); |
| 3781 | if (ret) { |
| 3782 | rbd_warn(rbd_dev, "failed to decode NotifyMessage: %d", |
| 3783 | ret); |
| 3784 | return; |
| 3785 | } |
Ilya Dryomov | 52bb1f9 | 2014-07-23 17:11:20 +0400 | [diff] [blame] | 3786 | |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3787 | notify_op = ceph_decode_32(&p); |
| 3788 | } else { |
| 3789 | /* legacy notification for header updates */ |
| 3790 | notify_op = RBD_NOTIFY_OP_HEADER_UPDATE; |
| 3791 | len = 0; |
| 3792 | } |
Alex Elder | b8d7003 | 2012-11-30 17:53:04 -0600 | [diff] [blame] | 3793 | |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3794 | dout("%s rbd_dev %p notify_op %u\n", __func__, rbd_dev, notify_op); |
| 3795 | switch (notify_op) { |
| 3796 | case RBD_NOTIFY_OP_ACQUIRED_LOCK: |
| 3797 | rbd_handle_acquired_lock(rbd_dev, struct_v, &p); |
| 3798 | rbd_acknowledge_notify(rbd_dev, notify_id, cookie); |
| 3799 | break; |
| 3800 | case RBD_NOTIFY_OP_RELEASED_LOCK: |
| 3801 | rbd_handle_released_lock(rbd_dev, struct_v, &p); |
| 3802 | rbd_acknowledge_notify(rbd_dev, notify_id, cookie); |
| 3803 | break; |
| 3804 | case RBD_NOTIFY_OP_REQUEST_LOCK: |
| 3805 | if (rbd_handle_request_lock(rbd_dev, struct_v, &p)) |
| 3806 | /* |
| 3807 | * send ResponseMessage(0) back so the client |
| 3808 | * can detect a missing owner |
| 3809 | */ |
| 3810 | rbd_acknowledge_notify_result(rbd_dev, notify_id, |
| 3811 | cookie, 0); |
| 3812 | else |
| 3813 | rbd_acknowledge_notify(rbd_dev, notify_id, cookie); |
| 3814 | break; |
| 3815 | case RBD_NOTIFY_OP_HEADER_UPDATE: |
| 3816 | ret = rbd_dev_refresh(rbd_dev); |
| 3817 | if (ret) |
| 3818 | rbd_warn(rbd_dev, "refresh failed: %d", ret); |
| 3819 | |
| 3820 | rbd_acknowledge_notify(rbd_dev, notify_id, cookie); |
| 3821 | break; |
| 3822 | default: |
| 3823 | if (rbd_is_lock_owner(rbd_dev)) |
| 3824 | rbd_acknowledge_notify_result(rbd_dev, notify_id, |
| 3825 | cookie, -EOPNOTSUPP); |
| 3826 | else |
| 3827 | rbd_acknowledge_notify(rbd_dev, notify_id, cookie); |
| 3828 | break; |
| 3829 | } |
Alex Elder | b8d7003 | 2012-11-30 17:53:04 -0600 | [diff] [blame] | 3830 | } |
| 3831 | |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3832 | static void __rbd_unregister_watch(struct rbd_device *rbd_dev); |
| 3833 | |
Ilya Dryomov | 922dab6 | 2016-05-26 01:15:02 +0200 | [diff] [blame] | 3834 | static void rbd_watch_errcb(void *arg, u64 cookie, int err) |
Ilya Dryomov | bb040aa | 2014-06-19 11:38:14 +0400 | [diff] [blame] | 3835 | { |
Ilya Dryomov | 922dab6 | 2016-05-26 01:15:02 +0200 | [diff] [blame] | 3836 | struct rbd_device *rbd_dev = arg; |
Ilya Dryomov | bb040aa | 2014-06-19 11:38:14 +0400 | [diff] [blame] | 3837 | |
Ilya Dryomov | 922dab6 | 2016-05-26 01:15:02 +0200 | [diff] [blame] | 3838 | rbd_warn(rbd_dev, "encountered watch error: %d", err); |
Ilya Dryomov | bb040aa | 2014-06-19 11:38:14 +0400 | [diff] [blame] | 3839 | |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3840 | down_write(&rbd_dev->lock_rwsem); |
| 3841 | rbd_set_owner_cid(rbd_dev, &rbd_empty_cid); |
| 3842 | up_write(&rbd_dev->lock_rwsem); |
| 3843 | |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3844 | mutex_lock(&rbd_dev->watch_mutex); |
| 3845 | if (rbd_dev->watch_state == RBD_WATCH_STATE_REGISTERED) { |
| 3846 | __rbd_unregister_watch(rbd_dev); |
| 3847 | rbd_dev->watch_state = RBD_WATCH_STATE_ERROR; |
Ilya Dryomov | bb040aa | 2014-06-19 11:38:14 +0400 | [diff] [blame] | 3848 | |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3849 | queue_delayed_work(rbd_dev->task_wq, &rbd_dev->watch_dwork, 0); |
Ilya Dryomov | bb040aa | 2014-06-19 11:38:14 +0400 | [diff] [blame] | 3850 | } |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3851 | mutex_unlock(&rbd_dev->watch_mutex); |
Ilya Dryomov | bb040aa | 2014-06-19 11:38:14 +0400 | [diff] [blame] | 3852 | } |
| 3853 | |
| 3854 | /* |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3855 | * watch_mutex must be locked |
Alex Elder | 9969ebc | 2013-01-18 12:31:10 -0600 | [diff] [blame] | 3856 | */ |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3857 | static int __rbd_register_watch(struct rbd_device *rbd_dev) |
Alex Elder | 9969ebc | 2013-01-18 12:31:10 -0600 | [diff] [blame] | 3858 | { |
| 3859 | struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc; |
Ilya Dryomov | 922dab6 | 2016-05-26 01:15:02 +0200 | [diff] [blame] | 3860 | struct ceph_osd_linger_request *handle; |
Alex Elder | 9969ebc | 2013-01-18 12:31:10 -0600 | [diff] [blame] | 3861 | |
Ilya Dryomov | 922dab6 | 2016-05-26 01:15:02 +0200 | [diff] [blame] | 3862 | rbd_assert(!rbd_dev->watch_handle); |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3863 | dout("%s rbd_dev %p\n", __func__, rbd_dev); |
Alex Elder | 9969ebc | 2013-01-18 12:31:10 -0600 | [diff] [blame] | 3864 | |
Ilya Dryomov | 922dab6 | 2016-05-26 01:15:02 +0200 | [diff] [blame] | 3865 | handle = ceph_osdc_watch(osdc, &rbd_dev->header_oid, |
| 3866 | &rbd_dev->header_oloc, rbd_watch_cb, |
| 3867 | rbd_watch_errcb, rbd_dev); |
| 3868 | if (IS_ERR(handle)) |
| 3869 | return PTR_ERR(handle); |
Alex Elder | 9969ebc | 2013-01-18 12:31:10 -0600 | [diff] [blame] | 3870 | |
Ilya Dryomov | 922dab6 | 2016-05-26 01:15:02 +0200 | [diff] [blame] | 3871 | rbd_dev->watch_handle = handle; |
Ilya Dryomov | b30a01f | 2014-05-22 19:28:52 +0400 | [diff] [blame] | 3872 | return 0; |
Alex Elder | 9969ebc | 2013-01-18 12:31:10 -0600 | [diff] [blame] | 3873 | } |
| 3874 | |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3875 | /* |
| 3876 | * watch_mutex must be locked |
| 3877 | */ |
| 3878 | static void __rbd_unregister_watch(struct rbd_device *rbd_dev) |
Ilya Dryomov | fca2706 | 2013-12-16 18:02:40 +0200 | [diff] [blame] | 3879 | { |
Ilya Dryomov | 922dab6 | 2016-05-26 01:15:02 +0200 | [diff] [blame] | 3880 | struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc; |
| 3881 | int ret; |
Ilya Dryomov | b30a01f | 2014-05-22 19:28:52 +0400 | [diff] [blame] | 3882 | |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3883 | rbd_assert(rbd_dev->watch_handle); |
| 3884 | dout("%s rbd_dev %p\n", __func__, rbd_dev); |
Ilya Dryomov | b30a01f | 2014-05-22 19:28:52 +0400 | [diff] [blame] | 3885 | |
Ilya Dryomov | 922dab6 | 2016-05-26 01:15:02 +0200 | [diff] [blame] | 3886 | ret = ceph_osdc_unwatch(osdc, rbd_dev->watch_handle); |
| 3887 | if (ret) |
| 3888 | rbd_warn(rbd_dev, "failed to unwatch: %d", ret); |
Ilya Dryomov | b30a01f | 2014-05-22 19:28:52 +0400 | [diff] [blame] | 3889 | |
Ilya Dryomov | 922dab6 | 2016-05-26 01:15:02 +0200 | [diff] [blame] | 3890 | rbd_dev->watch_handle = NULL; |
Ilya Dryomov | c525f03 | 2016-04-28 16:07:26 +0200 | [diff] [blame] | 3891 | } |
| 3892 | |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3893 | static int rbd_register_watch(struct rbd_device *rbd_dev) |
Ilya Dryomov | c525f03 | 2016-04-28 16:07:26 +0200 | [diff] [blame] | 3894 | { |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3895 | int ret; |
Ilya Dryomov | 811c668 | 2016-04-15 16:22:16 +0200 | [diff] [blame] | 3896 | |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3897 | mutex_lock(&rbd_dev->watch_mutex); |
| 3898 | rbd_assert(rbd_dev->watch_state == RBD_WATCH_STATE_UNREGISTERED); |
| 3899 | ret = __rbd_register_watch(rbd_dev); |
| 3900 | if (ret) |
| 3901 | goto out; |
| 3902 | |
| 3903 | rbd_dev->watch_state = RBD_WATCH_STATE_REGISTERED; |
| 3904 | rbd_dev->watch_cookie = rbd_dev->watch_handle->linger_id; |
| 3905 | |
| 3906 | out: |
| 3907 | mutex_unlock(&rbd_dev->watch_mutex); |
| 3908 | return ret; |
| 3909 | } |
| 3910 | |
| 3911 | static void cancel_tasks_sync(struct rbd_device *rbd_dev) |
| 3912 | { |
| 3913 | dout("%s rbd_dev %p\n", __func__, rbd_dev); |
| 3914 | |
| 3915 | cancel_delayed_work_sync(&rbd_dev->watch_dwork); |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3916 | cancel_work_sync(&rbd_dev->acquired_lock_work); |
| 3917 | cancel_work_sync(&rbd_dev->released_lock_work); |
| 3918 | cancel_delayed_work_sync(&rbd_dev->lock_dwork); |
| 3919 | cancel_work_sync(&rbd_dev->unlock_work); |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3920 | } |
| 3921 | |
| 3922 | static void rbd_unregister_watch(struct rbd_device *rbd_dev) |
| 3923 | { |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3924 | WARN_ON(waitqueue_active(&rbd_dev->lock_waitq)); |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3925 | cancel_tasks_sync(rbd_dev); |
| 3926 | |
| 3927 | mutex_lock(&rbd_dev->watch_mutex); |
| 3928 | if (rbd_dev->watch_state == RBD_WATCH_STATE_REGISTERED) |
| 3929 | __rbd_unregister_watch(rbd_dev); |
| 3930 | rbd_dev->watch_state = RBD_WATCH_STATE_UNREGISTERED; |
| 3931 | mutex_unlock(&rbd_dev->watch_mutex); |
| 3932 | |
Ilya Dryomov | 811c668 | 2016-04-15 16:22:16 +0200 | [diff] [blame] | 3933 | ceph_osdc_flush_notifies(&rbd_dev->rbd_client->client->osdc); |
Ilya Dryomov | fca2706 | 2013-12-16 18:02:40 +0200 | [diff] [blame] | 3934 | } |
| 3935 | |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3936 | static void rbd_reregister_watch(struct work_struct *work) |
| 3937 | { |
| 3938 | struct rbd_device *rbd_dev = container_of(to_delayed_work(work), |
| 3939 | struct rbd_device, watch_dwork); |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3940 | bool was_lock_owner = false; |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3941 | int ret; |
| 3942 | |
| 3943 | dout("%s rbd_dev %p\n", __func__, rbd_dev); |
| 3944 | |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3945 | down_write(&rbd_dev->lock_rwsem); |
| 3946 | if (rbd_dev->lock_state == RBD_LOCK_STATE_LOCKED) |
| 3947 | was_lock_owner = rbd_release_lock(rbd_dev); |
| 3948 | |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3949 | mutex_lock(&rbd_dev->watch_mutex); |
| 3950 | if (rbd_dev->watch_state != RBD_WATCH_STATE_ERROR) |
| 3951 | goto fail_unlock; |
| 3952 | |
| 3953 | ret = __rbd_register_watch(rbd_dev); |
| 3954 | if (ret) { |
| 3955 | rbd_warn(rbd_dev, "failed to reregister watch: %d", ret); |
| 3956 | if (ret != -EBLACKLISTED) |
| 3957 | queue_delayed_work(rbd_dev->task_wq, |
| 3958 | &rbd_dev->watch_dwork, |
| 3959 | RBD_RETRY_DELAY); |
| 3960 | goto fail_unlock; |
| 3961 | } |
| 3962 | |
| 3963 | rbd_dev->watch_state = RBD_WATCH_STATE_REGISTERED; |
| 3964 | rbd_dev->watch_cookie = rbd_dev->watch_handle->linger_id; |
| 3965 | mutex_unlock(&rbd_dev->watch_mutex); |
| 3966 | |
| 3967 | ret = rbd_dev_refresh(rbd_dev); |
| 3968 | if (ret) |
| 3969 | rbd_warn(rbd_dev, "reregisteration refresh failed: %d", ret); |
| 3970 | |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3971 | if (was_lock_owner) { |
| 3972 | ret = rbd_try_lock(rbd_dev); |
| 3973 | if (ret) |
| 3974 | rbd_warn(rbd_dev, "reregisteration lock failed: %d", |
| 3975 | ret); |
| 3976 | } |
| 3977 | |
| 3978 | up_write(&rbd_dev->lock_rwsem); |
| 3979 | wake_requests(rbd_dev, true); |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3980 | return; |
| 3981 | |
| 3982 | fail_unlock: |
| 3983 | mutex_unlock(&rbd_dev->watch_mutex); |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3984 | up_write(&rbd_dev->lock_rwsem); |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3985 | } |
| 3986 | |
Alex Elder | 36be9a7 | 2013-01-19 00:30:28 -0600 | [diff] [blame] | 3987 | /* |
Alex Elder | f40eb34 | 2013-04-25 15:09:42 -0500 | [diff] [blame] | 3988 | * Synchronous osd object method call. Returns the number of bytes |
| 3989 | * returned in the outbound buffer, or a negative error code. |
Alex Elder | 36be9a7 | 2013-01-19 00:30:28 -0600 | [diff] [blame] | 3990 | */ |
| 3991 | static int rbd_obj_method_sync(struct rbd_device *rbd_dev, |
| 3992 | const char *object_name, |
| 3993 | const char *class_name, |
| 3994 | const char *method_name, |
Alex Elder | 4157976 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 3995 | const void *outbound, |
Alex Elder | 36be9a7 | 2013-01-19 00:30:28 -0600 | [diff] [blame] | 3996 | size_t outbound_size, |
Alex Elder | 4157976 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 3997 | void *inbound, |
Alex Elder | e2a58ee | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 3998 | size_t inbound_size) |
Alex Elder | 36be9a7 | 2013-01-19 00:30:28 -0600 | [diff] [blame] | 3999 | { |
Alex Elder | 2169238 | 2013-04-05 01:27:12 -0500 | [diff] [blame] | 4000 | struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc; |
Alex Elder | 36be9a7 | 2013-01-19 00:30:28 -0600 | [diff] [blame] | 4001 | struct rbd_obj_request *obj_request; |
Alex Elder | 36be9a7 | 2013-01-19 00:30:28 -0600 | [diff] [blame] | 4002 | struct page **pages; |
| 4003 | u32 page_count; |
| 4004 | int ret; |
| 4005 | |
| 4006 | /* |
Alex Elder | 6010a45 | 2013-04-05 01:27:11 -0500 | [diff] [blame] | 4007 | * Method calls are ultimately read operations. The result |
| 4008 | * should placed into the inbound buffer provided. They |
| 4009 | * also supply outbound data--parameters for the object |
| 4010 | * method. Currently if this is present it will be a |
| 4011 | * snapshot id. |
Alex Elder | 36be9a7 | 2013-01-19 00:30:28 -0600 | [diff] [blame] | 4012 | */ |
Alex Elder | 57385b5 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 4013 | page_count = (u32)calc_pages_for(0, inbound_size); |
Alex Elder | 36be9a7 | 2013-01-19 00:30:28 -0600 | [diff] [blame] | 4014 | pages = ceph_alloc_page_vector(page_count, GFP_KERNEL); |
| 4015 | if (IS_ERR(pages)) |
| 4016 | return PTR_ERR(pages); |
| 4017 | |
| 4018 | ret = -ENOMEM; |
Alex Elder | 6010a45 | 2013-04-05 01:27:11 -0500 | [diff] [blame] | 4019 | obj_request = rbd_obj_request_create(object_name, 0, inbound_size, |
Alex Elder | 36be9a7 | 2013-01-19 00:30:28 -0600 | [diff] [blame] | 4020 | OBJ_REQUEST_PAGES); |
| 4021 | if (!obj_request) |
| 4022 | goto out; |
| 4023 | |
| 4024 | obj_request->pages = pages; |
| 4025 | obj_request->page_count = page_count; |
| 4026 | |
Guangliang Zhao | 6d2940c | 2014-03-13 11:21:35 +0800 | [diff] [blame] | 4027 | obj_request->osd_req = rbd_osd_req_create(rbd_dev, OBJ_OP_READ, 1, |
Ilya Dryomov | deb236b | 2014-02-25 16:22:27 +0200 | [diff] [blame] | 4028 | obj_request); |
Alex Elder | 36be9a7 | 2013-01-19 00:30:28 -0600 | [diff] [blame] | 4029 | if (!obj_request->osd_req) |
| 4030 | goto out; |
| 4031 | |
Alex Elder | c99d2d4 | 2013-04-05 01:27:11 -0500 | [diff] [blame] | 4032 | osd_req_op_cls_init(obj_request->osd_req, 0, CEPH_OSD_OP_CALL, |
Alex Elder | 04017e2 | 2013-04-05 14:46:02 -0500 | [diff] [blame] | 4033 | class_name, method_name); |
| 4034 | if (outbound_size) { |
| 4035 | struct ceph_pagelist *pagelist; |
| 4036 | |
| 4037 | pagelist = kmalloc(sizeof (*pagelist), GFP_NOFS); |
| 4038 | if (!pagelist) |
| 4039 | goto out; |
| 4040 | |
| 4041 | ceph_pagelist_init(pagelist); |
| 4042 | ceph_pagelist_append(pagelist, outbound, outbound_size); |
| 4043 | osd_req_op_cls_request_data_pagelist(obj_request->osd_req, 0, |
| 4044 | pagelist); |
| 4045 | } |
Alex Elder | a4ce40a | 2013-04-05 01:27:12 -0500 | [diff] [blame] | 4046 | osd_req_op_cls_response_data_pages(obj_request->osd_req, 0, |
| 4047 | obj_request->pages, inbound_size, |
Alex Elder | 44cd188 | 2013-04-05 01:27:12 -0500 | [diff] [blame] | 4048 | 0, false, false); |
Alex Elder | 9d4df01 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 4049 | rbd_osd_req_format_read(obj_request); |
Alex Elder | 430c28c | 2013-04-03 21:32:51 -0500 | [diff] [blame] | 4050 | |
Alex Elder | 36be9a7 | 2013-01-19 00:30:28 -0600 | [diff] [blame] | 4051 | ret = rbd_obj_request_submit(osdc, obj_request); |
| 4052 | if (ret) |
| 4053 | goto out; |
| 4054 | ret = rbd_obj_request_wait(obj_request); |
| 4055 | if (ret) |
| 4056 | goto out; |
| 4057 | |
| 4058 | ret = obj_request->result; |
| 4059 | if (ret < 0) |
| 4060 | goto out; |
Alex Elder | 57385b5 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 4061 | |
| 4062 | rbd_assert(obj_request->xferred < (u64)INT_MAX); |
| 4063 | ret = (int)obj_request->xferred; |
Alex Elder | 903bb32 | 2013-02-06 13:11:38 -0600 | [diff] [blame] | 4064 | ceph_copy_from_page_vector(pages, inbound, 0, obj_request->xferred); |
Alex Elder | 36be9a7 | 2013-01-19 00:30:28 -0600 | [diff] [blame] | 4065 | out: |
| 4066 | if (obj_request) |
| 4067 | rbd_obj_request_put(obj_request); |
| 4068 | else |
| 4069 | ceph_release_page_vector(pages, page_count); |
| 4070 | |
| 4071 | return ret; |
| 4072 | } |
| 4073 | |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 4074 | /* |
| 4075 | * lock_rwsem must be held for read |
| 4076 | */ |
| 4077 | static void rbd_wait_state_locked(struct rbd_device *rbd_dev) |
| 4078 | { |
| 4079 | DEFINE_WAIT(wait); |
| 4080 | |
| 4081 | do { |
| 4082 | /* |
| 4083 | * Note the use of mod_delayed_work() in rbd_acquire_lock() |
| 4084 | * and cancel_delayed_work() in wake_requests(). |
| 4085 | */ |
| 4086 | dout("%s rbd_dev %p queueing lock_dwork\n", __func__, rbd_dev); |
| 4087 | queue_delayed_work(rbd_dev->task_wq, &rbd_dev->lock_dwork, 0); |
| 4088 | prepare_to_wait_exclusive(&rbd_dev->lock_waitq, &wait, |
| 4089 | TASK_UNINTERRUPTIBLE); |
| 4090 | up_read(&rbd_dev->lock_rwsem); |
| 4091 | schedule(); |
| 4092 | down_read(&rbd_dev->lock_rwsem); |
| 4093 | } while (rbd_dev->lock_state != RBD_LOCK_STATE_LOCKED); |
| 4094 | finish_wait(&rbd_dev->lock_waitq, &wait); |
| 4095 | } |
| 4096 | |
Christoph Hellwig | 7ad18af | 2015-01-13 17:20:04 +0100 | [diff] [blame] | 4097 | static void rbd_queue_workfn(struct work_struct *work) |
Ilya Dryomov | bc1ecc6 | 2014-08-04 18:04:39 +0400 | [diff] [blame] | 4098 | { |
Christoph Hellwig | 7ad18af | 2015-01-13 17:20:04 +0100 | [diff] [blame] | 4099 | struct request *rq = blk_mq_rq_from_pdu(work); |
| 4100 | struct rbd_device *rbd_dev = rq->q->queuedata; |
Ilya Dryomov | bc1ecc6 | 2014-08-04 18:04:39 +0400 | [diff] [blame] | 4101 | struct rbd_img_request *img_request; |
Josh Durgin | 4e752f0 | 2014-04-08 11:12:11 -0700 | [diff] [blame] | 4102 | struct ceph_snap_context *snapc = NULL; |
Ilya Dryomov | bc1ecc6 | 2014-08-04 18:04:39 +0400 | [diff] [blame] | 4103 | u64 offset = (u64)blk_rq_pos(rq) << SECTOR_SHIFT; |
| 4104 | u64 length = blk_rq_bytes(rq); |
Guangliang Zhao | 6d2940c | 2014-03-13 11:21:35 +0800 | [diff] [blame] | 4105 | enum obj_operation_type op_type; |
Josh Durgin | 4e752f0 | 2014-04-08 11:12:11 -0700 | [diff] [blame] | 4106 | u64 mapping_size; |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 4107 | bool must_be_locked = false; |
Ilya Dryomov | bc1ecc6 | 2014-08-04 18:04:39 +0400 | [diff] [blame] | 4108 | int result; |
| 4109 | |
Christoph Hellwig | 7ad18af | 2015-01-13 17:20:04 +0100 | [diff] [blame] | 4110 | if (rq->cmd_type != REQ_TYPE_FS) { |
| 4111 | dout("%s: non-fs request type %d\n", __func__, |
| 4112 | (int) rq->cmd_type); |
| 4113 | result = -EIO; |
| 4114 | goto err; |
| 4115 | } |
| 4116 | |
Mike Christie | c2df40d | 2016-06-05 14:32:17 -0500 | [diff] [blame] | 4117 | if (req_op(rq) == REQ_OP_DISCARD) |
Guangliang Zhao | 90e98c5 | 2014-04-01 22:22:16 +0800 | [diff] [blame] | 4118 | op_type = OBJ_OP_DISCARD; |
Mike Christie | c2df40d | 2016-06-05 14:32:17 -0500 | [diff] [blame] | 4119 | else if (req_op(rq) == REQ_OP_WRITE) |
Guangliang Zhao | 6d2940c | 2014-03-13 11:21:35 +0800 | [diff] [blame] | 4120 | op_type = OBJ_OP_WRITE; |
| 4121 | else |
| 4122 | op_type = OBJ_OP_READ; |
| 4123 | |
Ilya Dryomov | bc1ecc6 | 2014-08-04 18:04:39 +0400 | [diff] [blame] | 4124 | /* Ignore/skip any zero-length requests */ |
| 4125 | |
| 4126 | if (!length) { |
| 4127 | dout("%s: zero-length request\n", __func__); |
| 4128 | result = 0; |
| 4129 | goto err_rq; |
| 4130 | } |
| 4131 | |
Guangliang Zhao | 6d2940c | 2014-03-13 11:21:35 +0800 | [diff] [blame] | 4132 | /* Only reads are allowed to a read-only device */ |
Ilya Dryomov | bc1ecc6 | 2014-08-04 18:04:39 +0400 | [diff] [blame] | 4133 | |
Guangliang Zhao | 6d2940c | 2014-03-13 11:21:35 +0800 | [diff] [blame] | 4134 | if (op_type != OBJ_OP_READ) { |
Ilya Dryomov | bc1ecc6 | 2014-08-04 18:04:39 +0400 | [diff] [blame] | 4135 | if (rbd_dev->mapping.read_only) { |
| 4136 | result = -EROFS; |
| 4137 | goto err_rq; |
| 4138 | } |
| 4139 | rbd_assert(rbd_dev->spec->snap_id == CEPH_NOSNAP); |
| 4140 | } |
| 4141 | |
| 4142 | /* |
| 4143 | * Quit early if the mapped snapshot no longer exists. It's |
| 4144 | * still possible the snapshot will have disappeared by the |
| 4145 | * time our request arrives at the osd, but there's no sense in |
| 4146 | * sending it if we already know. |
| 4147 | */ |
| 4148 | if (!test_bit(RBD_DEV_FLAG_EXISTS, &rbd_dev->flags)) { |
| 4149 | dout("request for non-existent snapshot"); |
| 4150 | rbd_assert(rbd_dev->spec->snap_id != CEPH_NOSNAP); |
| 4151 | result = -ENXIO; |
| 4152 | goto err_rq; |
| 4153 | } |
| 4154 | |
| 4155 | if (offset && length > U64_MAX - offset + 1) { |
| 4156 | rbd_warn(rbd_dev, "bad request range (%llu~%llu)", offset, |
| 4157 | length); |
| 4158 | result = -EINVAL; |
| 4159 | goto err_rq; /* Shouldn't happen */ |
| 4160 | } |
| 4161 | |
Christoph Hellwig | 7ad18af | 2015-01-13 17:20:04 +0100 | [diff] [blame] | 4162 | blk_mq_start_request(rq); |
| 4163 | |
Josh Durgin | 4e752f0 | 2014-04-08 11:12:11 -0700 | [diff] [blame] | 4164 | down_read(&rbd_dev->header_rwsem); |
| 4165 | mapping_size = rbd_dev->mapping.size; |
Guangliang Zhao | 6d2940c | 2014-03-13 11:21:35 +0800 | [diff] [blame] | 4166 | if (op_type != OBJ_OP_READ) { |
Josh Durgin | 4e752f0 | 2014-04-08 11:12:11 -0700 | [diff] [blame] | 4167 | snapc = rbd_dev->header.snapc; |
| 4168 | ceph_get_snap_context(snapc); |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 4169 | must_be_locked = rbd_is_lock_supported(rbd_dev); |
Josh Durgin | 4e752f0 | 2014-04-08 11:12:11 -0700 | [diff] [blame] | 4170 | } |
| 4171 | up_read(&rbd_dev->header_rwsem); |
| 4172 | |
| 4173 | if (offset + length > mapping_size) { |
Ilya Dryomov | bc1ecc6 | 2014-08-04 18:04:39 +0400 | [diff] [blame] | 4174 | rbd_warn(rbd_dev, "beyond EOD (%llu~%llu > %llu)", offset, |
Josh Durgin | 4e752f0 | 2014-04-08 11:12:11 -0700 | [diff] [blame] | 4175 | length, mapping_size); |
Ilya Dryomov | bc1ecc6 | 2014-08-04 18:04:39 +0400 | [diff] [blame] | 4176 | result = -EIO; |
| 4177 | goto err_rq; |
| 4178 | } |
| 4179 | |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 4180 | if (must_be_locked) { |
| 4181 | down_read(&rbd_dev->lock_rwsem); |
| 4182 | if (rbd_dev->lock_state != RBD_LOCK_STATE_LOCKED) |
| 4183 | rbd_wait_state_locked(rbd_dev); |
| 4184 | } |
| 4185 | |
Guangliang Zhao | 6d2940c | 2014-03-13 11:21:35 +0800 | [diff] [blame] | 4186 | img_request = rbd_img_request_create(rbd_dev, offset, length, op_type, |
Josh Durgin | 4e752f0 | 2014-04-08 11:12:11 -0700 | [diff] [blame] | 4187 | snapc); |
Ilya Dryomov | bc1ecc6 | 2014-08-04 18:04:39 +0400 | [diff] [blame] | 4188 | if (!img_request) { |
| 4189 | result = -ENOMEM; |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 4190 | goto err_unlock; |
Ilya Dryomov | bc1ecc6 | 2014-08-04 18:04:39 +0400 | [diff] [blame] | 4191 | } |
| 4192 | img_request->rq = rq; |
Ilya Dryomov | 70b16db | 2015-11-27 19:23:24 +0100 | [diff] [blame] | 4193 | snapc = NULL; /* img_request consumes a ref */ |
Ilya Dryomov | bc1ecc6 | 2014-08-04 18:04:39 +0400 | [diff] [blame] | 4194 | |
Guangliang Zhao | 90e98c5 | 2014-04-01 22:22:16 +0800 | [diff] [blame] | 4195 | if (op_type == OBJ_OP_DISCARD) |
| 4196 | result = rbd_img_request_fill(img_request, OBJ_REQUEST_NODATA, |
| 4197 | NULL); |
| 4198 | else |
| 4199 | result = rbd_img_request_fill(img_request, OBJ_REQUEST_BIO, |
| 4200 | rq->bio); |
Ilya Dryomov | bc1ecc6 | 2014-08-04 18:04:39 +0400 | [diff] [blame] | 4201 | if (result) |
| 4202 | goto err_img_request; |
| 4203 | |
| 4204 | result = rbd_img_request_submit(img_request); |
| 4205 | if (result) |
| 4206 | goto err_img_request; |
| 4207 | |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 4208 | if (must_be_locked) |
| 4209 | up_read(&rbd_dev->lock_rwsem); |
Ilya Dryomov | bc1ecc6 | 2014-08-04 18:04:39 +0400 | [diff] [blame] | 4210 | return; |
| 4211 | |
| 4212 | err_img_request: |
| 4213 | rbd_img_request_put(img_request); |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 4214 | err_unlock: |
| 4215 | if (must_be_locked) |
| 4216 | up_read(&rbd_dev->lock_rwsem); |
Ilya Dryomov | bc1ecc6 | 2014-08-04 18:04:39 +0400 | [diff] [blame] | 4217 | err_rq: |
| 4218 | if (result) |
| 4219 | rbd_warn(rbd_dev, "%s %llx at %llx result %d", |
Guangliang Zhao | 6d2940c | 2014-03-13 11:21:35 +0800 | [diff] [blame] | 4220 | obj_op_name(op_type), length, offset, result); |
SF Markus Elfring | e96a650 | 2014-11-02 15:20:59 +0100 | [diff] [blame] | 4221 | ceph_put_snap_context(snapc); |
Christoph Hellwig | 7ad18af | 2015-01-13 17:20:04 +0100 | [diff] [blame] | 4222 | err: |
| 4223 | blk_mq_end_request(rq, result); |
Ilya Dryomov | bc1ecc6 | 2014-08-04 18:04:39 +0400 | [diff] [blame] | 4224 | } |
| 4225 | |
Christoph Hellwig | 7ad18af | 2015-01-13 17:20:04 +0100 | [diff] [blame] | 4226 | static int rbd_queue_rq(struct blk_mq_hw_ctx *hctx, |
| 4227 | const struct blk_mq_queue_data *bd) |
Ilya Dryomov | bc1ecc6 | 2014-08-04 18:04:39 +0400 | [diff] [blame] | 4228 | { |
Christoph Hellwig | 7ad18af | 2015-01-13 17:20:04 +0100 | [diff] [blame] | 4229 | struct request *rq = bd->rq; |
| 4230 | struct work_struct *work = blk_mq_rq_to_pdu(rq); |
Ilya Dryomov | bc1ecc6 | 2014-08-04 18:04:39 +0400 | [diff] [blame] | 4231 | |
Christoph Hellwig | 7ad18af | 2015-01-13 17:20:04 +0100 | [diff] [blame] | 4232 | queue_work(rbd_wq, work); |
| 4233 | return BLK_MQ_RQ_QUEUE_OK; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 4234 | } |
| 4235 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 4236 | static void rbd_free_disk(struct rbd_device *rbd_dev) |
| 4237 | { |
| 4238 | struct gendisk *disk = rbd_dev->disk; |
| 4239 | |
| 4240 | if (!disk) |
| 4241 | return; |
| 4242 | |
Alex Elder | a0cab92 | 2013-04-25 23:15:08 -0500 | [diff] [blame] | 4243 | rbd_dev->disk = NULL; |
| 4244 | if (disk->flags & GENHD_FL_UP) { |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 4245 | del_gendisk(disk); |
Alex Elder | a0cab92 | 2013-04-25 23:15:08 -0500 | [diff] [blame] | 4246 | if (disk->queue) |
| 4247 | blk_cleanup_queue(disk->queue); |
Christoph Hellwig | 7ad18af | 2015-01-13 17:20:04 +0100 | [diff] [blame] | 4248 | blk_mq_free_tag_set(&rbd_dev->tag_set); |
Alex Elder | a0cab92 | 2013-04-25 23:15:08 -0500 | [diff] [blame] | 4249 | } |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 4250 | put_disk(disk); |
| 4251 | } |
| 4252 | |
Alex Elder | 788e2df | 2013-01-17 12:25:27 -0600 | [diff] [blame] | 4253 | static int rbd_obj_read_sync(struct rbd_device *rbd_dev, |
| 4254 | const char *object_name, |
Alex Elder | 7097f8d | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 4255 | u64 offset, u64 length, void *buf) |
Alex Elder | 788e2df | 2013-01-17 12:25:27 -0600 | [diff] [blame] | 4256 | |
| 4257 | { |
Alex Elder | 2169238 | 2013-04-05 01:27:12 -0500 | [diff] [blame] | 4258 | struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc; |
Alex Elder | 788e2df | 2013-01-17 12:25:27 -0600 | [diff] [blame] | 4259 | struct rbd_obj_request *obj_request; |
Alex Elder | 788e2df | 2013-01-17 12:25:27 -0600 | [diff] [blame] | 4260 | struct page **pages = NULL; |
| 4261 | u32 page_count; |
Alex Elder | 1ceae7e | 2013-02-06 13:11:38 -0600 | [diff] [blame] | 4262 | size_t size; |
Alex Elder | 788e2df | 2013-01-17 12:25:27 -0600 | [diff] [blame] | 4263 | int ret; |
| 4264 | |
| 4265 | page_count = (u32) calc_pages_for(offset, length); |
| 4266 | pages = ceph_alloc_page_vector(page_count, GFP_KERNEL); |
| 4267 | if (IS_ERR(pages)) |
Jan Kara | a8d4205 | 2014-10-22 09:17:24 +0200 | [diff] [blame] | 4268 | return PTR_ERR(pages); |
Alex Elder | 788e2df | 2013-01-17 12:25:27 -0600 | [diff] [blame] | 4269 | |
| 4270 | ret = -ENOMEM; |
| 4271 | obj_request = rbd_obj_request_create(object_name, offset, length, |
Alex Elder | 36be9a7 | 2013-01-19 00:30:28 -0600 | [diff] [blame] | 4272 | OBJ_REQUEST_PAGES); |
Alex Elder | 788e2df | 2013-01-17 12:25:27 -0600 | [diff] [blame] | 4273 | if (!obj_request) |
| 4274 | goto out; |
| 4275 | |
| 4276 | obj_request->pages = pages; |
| 4277 | obj_request->page_count = page_count; |
| 4278 | |
Guangliang Zhao | 6d2940c | 2014-03-13 11:21:35 +0800 | [diff] [blame] | 4279 | obj_request->osd_req = rbd_osd_req_create(rbd_dev, OBJ_OP_READ, 1, |
Ilya Dryomov | deb236b | 2014-02-25 16:22:27 +0200 | [diff] [blame] | 4280 | obj_request); |
Alex Elder | 788e2df | 2013-01-17 12:25:27 -0600 | [diff] [blame] | 4281 | if (!obj_request->osd_req) |
| 4282 | goto out; |
| 4283 | |
Alex Elder | c99d2d4 | 2013-04-05 01:27:11 -0500 | [diff] [blame] | 4284 | osd_req_op_extent_init(obj_request->osd_req, 0, CEPH_OSD_OP_READ, |
| 4285 | offset, length, 0, 0); |
Alex Elder | 406e2c9 | 2013-04-15 14:50:36 -0500 | [diff] [blame] | 4286 | osd_req_op_extent_osd_data_pages(obj_request->osd_req, 0, |
Alex Elder | a4ce40a | 2013-04-05 01:27:12 -0500 | [diff] [blame] | 4287 | obj_request->pages, |
Alex Elder | 44cd188 | 2013-04-05 01:27:12 -0500 | [diff] [blame] | 4288 | obj_request->length, |
| 4289 | obj_request->offset & ~PAGE_MASK, |
| 4290 | false, false); |
Alex Elder | 9d4df01 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 4291 | rbd_osd_req_format_read(obj_request); |
Alex Elder | 430c28c | 2013-04-03 21:32:51 -0500 | [diff] [blame] | 4292 | |
Alex Elder | 788e2df | 2013-01-17 12:25:27 -0600 | [diff] [blame] | 4293 | ret = rbd_obj_request_submit(osdc, obj_request); |
| 4294 | if (ret) |
| 4295 | goto out; |
| 4296 | ret = rbd_obj_request_wait(obj_request); |
| 4297 | if (ret) |
| 4298 | goto out; |
| 4299 | |
| 4300 | ret = obj_request->result; |
| 4301 | if (ret < 0) |
| 4302 | goto out; |
Alex Elder | 1ceae7e | 2013-02-06 13:11:38 -0600 | [diff] [blame] | 4303 | |
| 4304 | rbd_assert(obj_request->xferred <= (u64) SIZE_MAX); |
| 4305 | size = (size_t) obj_request->xferred; |
Alex Elder | 903bb32 | 2013-02-06 13:11:38 -0600 | [diff] [blame] | 4306 | ceph_copy_from_page_vector(pages, buf, 0, size); |
Alex Elder | 7097f8d | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 4307 | rbd_assert(size <= (size_t)INT_MAX); |
| 4308 | ret = (int)size; |
Alex Elder | 788e2df | 2013-01-17 12:25:27 -0600 | [diff] [blame] | 4309 | out: |
| 4310 | if (obj_request) |
| 4311 | rbd_obj_request_put(obj_request); |
| 4312 | else |
| 4313 | ceph_release_page_vector(pages, page_count); |
| 4314 | |
| 4315 | return ret; |
| 4316 | } |
| 4317 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 4318 | /* |
Alex Elder | 662518b | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 4319 | * Read the complete header for the given rbd device. On successful |
| 4320 | * return, the rbd_dev->header field will contain up-to-date |
| 4321 | * information about the image. |
Alex Elder | 4156d99 | 2012-08-02 11:29:46 -0500 | [diff] [blame] | 4322 | */ |
Alex Elder | 99a41eb | 2013-05-06 09:51:30 -0500 | [diff] [blame] | 4323 | static int rbd_dev_v1_header_info(struct rbd_device *rbd_dev) |
Alex Elder | 4156d99 | 2012-08-02 11:29:46 -0500 | [diff] [blame] | 4324 | { |
| 4325 | struct rbd_image_header_ondisk *ondisk = NULL; |
| 4326 | u32 snap_count = 0; |
| 4327 | u64 names_size = 0; |
| 4328 | u32 want_count; |
| 4329 | int ret; |
| 4330 | |
| 4331 | /* |
| 4332 | * The complete header will include an array of its 64-bit |
| 4333 | * snapshot ids, followed by the names of those snapshots as |
| 4334 | * a contiguous block of NUL-terminated strings. Note that |
| 4335 | * the number of snapshots could change by the time we read |
| 4336 | * it in, in which case we re-read it. |
| 4337 | */ |
| 4338 | do { |
| 4339 | size_t size; |
| 4340 | |
| 4341 | kfree(ondisk); |
| 4342 | |
| 4343 | size = sizeof (*ondisk); |
| 4344 | size += snap_count * sizeof (struct rbd_image_snap_ondisk); |
| 4345 | size += names_size; |
| 4346 | ondisk = kmalloc(size, GFP_KERNEL); |
| 4347 | if (!ondisk) |
Alex Elder | 662518b | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 4348 | return -ENOMEM; |
Alex Elder | 4156d99 | 2012-08-02 11:29:46 -0500 | [diff] [blame] | 4349 | |
Ilya Dryomov | c41d13a | 2016-04-29 20:01:25 +0200 | [diff] [blame] | 4350 | ret = rbd_obj_read_sync(rbd_dev, rbd_dev->header_oid.name, |
Alex Elder | 7097f8d | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 4351 | 0, size, ondisk); |
Alex Elder | 4156d99 | 2012-08-02 11:29:46 -0500 | [diff] [blame] | 4352 | if (ret < 0) |
Alex Elder | 662518b | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 4353 | goto out; |
Alex Elder | c0cd10db | 2013-04-26 09:43:47 -0500 | [diff] [blame] | 4354 | if ((size_t)ret < size) { |
Alex Elder | 4156d99 | 2012-08-02 11:29:46 -0500 | [diff] [blame] | 4355 | ret = -ENXIO; |
Alex Elder | 06ecc6c | 2012-11-01 10:17:15 -0500 | [diff] [blame] | 4356 | rbd_warn(rbd_dev, "short header read (want %zd got %d)", |
| 4357 | size, ret); |
Alex Elder | 662518b | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 4358 | goto out; |
Alex Elder | 4156d99 | 2012-08-02 11:29:46 -0500 | [diff] [blame] | 4359 | } |
| 4360 | if (!rbd_dev_ondisk_valid(ondisk)) { |
| 4361 | ret = -ENXIO; |
Alex Elder | 06ecc6c | 2012-11-01 10:17:15 -0500 | [diff] [blame] | 4362 | rbd_warn(rbd_dev, "invalid header"); |
Alex Elder | 662518b | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 4363 | goto out; |
Alex Elder | 4156d99 | 2012-08-02 11:29:46 -0500 | [diff] [blame] | 4364 | } |
| 4365 | |
| 4366 | names_size = le64_to_cpu(ondisk->snap_names_len); |
| 4367 | want_count = snap_count; |
| 4368 | snap_count = le32_to_cpu(ondisk->snap_count); |
| 4369 | } while (snap_count != want_count); |
| 4370 | |
Alex Elder | 662518b | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 4371 | ret = rbd_header_from_disk(rbd_dev, ondisk); |
| 4372 | out: |
Alex Elder | 4156d99 | 2012-08-02 11:29:46 -0500 | [diff] [blame] | 4373 | kfree(ondisk); |
| 4374 | |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4375 | return ret; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 4376 | } |
| 4377 | |
Alex Elder | 15228ed | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 4378 | /* |
| 4379 | * Clear the rbd device's EXISTS flag if the snapshot it's mapped to |
| 4380 | * has disappeared from the (just updated) snapshot context. |
| 4381 | */ |
| 4382 | static void rbd_exists_validate(struct rbd_device *rbd_dev) |
| 4383 | { |
| 4384 | u64 snap_id; |
| 4385 | |
| 4386 | if (!test_bit(RBD_DEV_FLAG_EXISTS, &rbd_dev->flags)) |
| 4387 | return; |
| 4388 | |
| 4389 | snap_id = rbd_dev->spec->snap_id; |
| 4390 | if (snap_id == CEPH_NOSNAP) |
| 4391 | return; |
| 4392 | |
| 4393 | if (rbd_dev_snap_index(rbd_dev, snap_id) == BAD_SNAP_INDEX) |
| 4394 | clear_bit(RBD_DEV_FLAG_EXISTS, &rbd_dev->flags); |
| 4395 | } |
| 4396 | |
Josh Durgin | 9875201 | 2013-08-29 17:26:31 -0700 | [diff] [blame] | 4397 | static void rbd_dev_update_size(struct rbd_device *rbd_dev) |
| 4398 | { |
| 4399 | sector_t size; |
Josh Durgin | 9875201 | 2013-08-29 17:26:31 -0700 | [diff] [blame] | 4400 | |
| 4401 | /* |
Ilya Dryomov | 811c668 | 2016-04-15 16:22:16 +0200 | [diff] [blame] | 4402 | * If EXISTS is not set, rbd_dev->disk may be NULL, so don't |
| 4403 | * try to update its size. If REMOVING is set, updating size |
| 4404 | * is just useless work since the device can't be opened. |
Josh Durgin | 9875201 | 2013-08-29 17:26:31 -0700 | [diff] [blame] | 4405 | */ |
Ilya Dryomov | 811c668 | 2016-04-15 16:22:16 +0200 | [diff] [blame] | 4406 | if (test_bit(RBD_DEV_FLAG_EXISTS, &rbd_dev->flags) && |
| 4407 | !test_bit(RBD_DEV_FLAG_REMOVING, &rbd_dev->flags)) { |
Josh Durgin | 9875201 | 2013-08-29 17:26:31 -0700 | [diff] [blame] | 4408 | size = (sector_t)rbd_dev->mapping.size / SECTOR_SIZE; |
| 4409 | dout("setting size to %llu sectors", (unsigned long long)size); |
| 4410 | set_capacity(rbd_dev->disk, size); |
| 4411 | revalidate_disk(rbd_dev->disk); |
| 4412 | } |
| 4413 | } |
| 4414 | |
Alex Elder | cc4a38bd | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 4415 | static int rbd_dev_refresh(struct rbd_device *rbd_dev) |
Alex Elder | 1fe5e99 | 2012-07-25 09:32:41 -0500 | [diff] [blame] | 4416 | { |
Alex Elder | e627db0 | 2013-05-06 07:40:30 -0500 | [diff] [blame] | 4417 | u64 mapping_size; |
Alex Elder | 1fe5e99 | 2012-07-25 09:32:41 -0500 | [diff] [blame] | 4418 | int ret; |
| 4419 | |
Alex Elder | cfbf637 | 2013-05-31 17:40:45 -0500 | [diff] [blame] | 4420 | down_write(&rbd_dev->header_rwsem); |
Alex Elder | 3b5cf2a | 2013-05-29 11:18:59 -0500 | [diff] [blame] | 4421 | mapping_size = rbd_dev->mapping.size; |
Ilya Dryomov | a720ae0 | 2014-07-23 17:11:19 +0400 | [diff] [blame] | 4422 | |
| 4423 | ret = rbd_dev_header_info(rbd_dev); |
Ilya Dryomov | 52bb1f9 | 2014-07-23 17:11:20 +0400 | [diff] [blame] | 4424 | if (ret) |
Ilya Dryomov | 73e39e4 | 2015-01-08 20:18:22 +0300 | [diff] [blame] | 4425 | goto out; |
Alex Elder | 15228ed | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 4426 | |
Ilya Dryomov | e8f59b5 | 2014-07-24 10:42:13 +0400 | [diff] [blame] | 4427 | /* |
| 4428 | * If there is a parent, see if it has disappeared due to the |
| 4429 | * mapped image getting flattened. |
| 4430 | */ |
| 4431 | if (rbd_dev->parent) { |
| 4432 | ret = rbd_dev_v2_parent_info(rbd_dev); |
| 4433 | if (ret) |
Ilya Dryomov | 73e39e4 | 2015-01-08 20:18:22 +0300 | [diff] [blame] | 4434 | goto out; |
Ilya Dryomov | e8f59b5 | 2014-07-24 10:42:13 +0400 | [diff] [blame] | 4435 | } |
| 4436 | |
Ilya Dryomov | 5ff1108 | 2014-07-23 17:11:21 +0400 | [diff] [blame] | 4437 | if (rbd_dev->spec->snap_id == CEPH_NOSNAP) { |
Ilya Dryomov | 73e39e4 | 2015-01-08 20:18:22 +0300 | [diff] [blame] | 4438 | rbd_dev->mapping.size = rbd_dev->header.image_size; |
Ilya Dryomov | 5ff1108 | 2014-07-23 17:11:21 +0400 | [diff] [blame] | 4439 | } else { |
| 4440 | /* validate mapped snapshot's EXISTS flag */ |
| 4441 | rbd_exists_validate(rbd_dev); |
| 4442 | } |
Alex Elder | 15228ed | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 4443 | |
Ilya Dryomov | 73e39e4 | 2015-01-08 20:18:22 +0300 | [diff] [blame] | 4444 | out: |
Alex Elder | cfbf637 | 2013-05-31 17:40:45 -0500 | [diff] [blame] | 4445 | up_write(&rbd_dev->header_rwsem); |
Ilya Dryomov | 73e39e4 | 2015-01-08 20:18:22 +0300 | [diff] [blame] | 4446 | if (!ret && mapping_size != rbd_dev->mapping.size) |
Josh Durgin | 9875201 | 2013-08-29 17:26:31 -0700 | [diff] [blame] | 4447 | rbd_dev_update_size(rbd_dev); |
Alex Elder | 1fe5e99 | 2012-07-25 09:32:41 -0500 | [diff] [blame] | 4448 | |
Ilya Dryomov | 73e39e4 | 2015-01-08 20:18:22 +0300 | [diff] [blame] | 4449 | return ret; |
Alex Elder | 1fe5e99 | 2012-07-25 09:32:41 -0500 | [diff] [blame] | 4450 | } |
| 4451 | |
Christoph Hellwig | 7ad18af | 2015-01-13 17:20:04 +0100 | [diff] [blame] | 4452 | static int rbd_init_request(void *data, struct request *rq, |
| 4453 | unsigned int hctx_idx, unsigned int request_idx, |
| 4454 | unsigned int numa_node) |
| 4455 | { |
| 4456 | struct work_struct *work = blk_mq_rq_to_pdu(rq); |
| 4457 | |
| 4458 | INIT_WORK(work, rbd_queue_workfn); |
| 4459 | return 0; |
| 4460 | } |
| 4461 | |
| 4462 | static struct blk_mq_ops rbd_mq_ops = { |
| 4463 | .queue_rq = rbd_queue_rq, |
| 4464 | .map_queue = blk_mq_map_queue, |
| 4465 | .init_request = rbd_init_request, |
| 4466 | }; |
| 4467 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 4468 | static int rbd_init_disk(struct rbd_device *rbd_dev) |
| 4469 | { |
| 4470 | struct gendisk *disk; |
| 4471 | struct request_queue *q; |
Alex Elder | 593a9e7 | 2012-02-07 12:03:37 -0600 | [diff] [blame] | 4472 | u64 segment_size; |
Christoph Hellwig | 7ad18af | 2015-01-13 17:20:04 +0100 | [diff] [blame] | 4473 | int err; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 4474 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 4475 | /* create gendisk info */ |
Ilya Dryomov | 7e513d4 | 2013-12-16 19:26:32 +0200 | [diff] [blame] | 4476 | disk = alloc_disk(single_major ? |
| 4477 | (1 << RBD_SINGLE_MAJOR_PART_SHIFT) : |
| 4478 | RBD_MINORS_PER_MAJOR); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 4479 | if (!disk) |
Alex Elder | 1fcdb8a | 2012-08-29 17:11:06 -0500 | [diff] [blame] | 4480 | return -ENOMEM; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 4481 | |
Alex Elder | f0f8cef | 2012-01-29 13:57:44 -0600 | [diff] [blame] | 4482 | snprintf(disk->disk_name, sizeof(disk->disk_name), RBD_DRV_NAME "%d", |
Alex Elder | de71a29 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 4483 | rbd_dev->dev_id); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 4484 | disk->major = rbd_dev->major; |
Ilya Dryomov | dd82fff | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 4485 | disk->first_minor = rbd_dev->minor; |
Ilya Dryomov | 7e513d4 | 2013-12-16 19:26:32 +0200 | [diff] [blame] | 4486 | if (single_major) |
| 4487 | disk->flags |= GENHD_FL_EXT_DEVT; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 4488 | disk->fops = &rbd_bd_ops; |
| 4489 | disk->private_data = rbd_dev; |
| 4490 | |
Christoph Hellwig | 7ad18af | 2015-01-13 17:20:04 +0100 | [diff] [blame] | 4491 | memset(&rbd_dev->tag_set, 0, sizeof(rbd_dev->tag_set)); |
| 4492 | rbd_dev->tag_set.ops = &rbd_mq_ops; |
Ilya Dryomov | b558418 | 2015-06-23 16:21:19 +0300 | [diff] [blame] | 4493 | rbd_dev->tag_set.queue_depth = rbd_dev->opts->queue_depth; |
Christoph Hellwig | 7ad18af | 2015-01-13 17:20:04 +0100 | [diff] [blame] | 4494 | rbd_dev->tag_set.numa_node = NUMA_NO_NODE; |
Ilya Dryomov | b558418 | 2015-06-23 16:21:19 +0300 | [diff] [blame] | 4495 | rbd_dev->tag_set.flags = BLK_MQ_F_SHOULD_MERGE | BLK_MQ_F_SG_MERGE; |
Christoph Hellwig | 7ad18af | 2015-01-13 17:20:04 +0100 | [diff] [blame] | 4496 | rbd_dev->tag_set.nr_hw_queues = 1; |
| 4497 | rbd_dev->tag_set.cmd_size = sizeof(struct work_struct); |
| 4498 | |
| 4499 | err = blk_mq_alloc_tag_set(&rbd_dev->tag_set); |
| 4500 | if (err) |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 4501 | goto out_disk; |
Josh Durgin | 029bcbd | 2011-07-22 11:35:23 -0700 | [diff] [blame] | 4502 | |
Christoph Hellwig | 7ad18af | 2015-01-13 17:20:04 +0100 | [diff] [blame] | 4503 | q = blk_mq_init_queue(&rbd_dev->tag_set); |
| 4504 | if (IS_ERR(q)) { |
| 4505 | err = PTR_ERR(q); |
| 4506 | goto out_tag_set; |
| 4507 | } |
| 4508 | |
Ilya Dryomov | d8a2c89 | 2015-03-24 16:15:17 +0300 | [diff] [blame] | 4509 | queue_flag_set_unlocked(QUEUE_FLAG_NONROT, q); |
| 4510 | /* QUEUE_FLAG_ADD_RANDOM is off by default for blk-mq */ |
Alex Elder | 593a9e7 | 2012-02-07 12:03:37 -0600 | [diff] [blame] | 4511 | |
Josh Durgin | 029bcbd | 2011-07-22 11:35:23 -0700 | [diff] [blame] | 4512 | /* set io sizes to object size */ |
Alex Elder | 593a9e7 | 2012-02-07 12:03:37 -0600 | [diff] [blame] | 4513 | segment_size = rbd_obj_bytes(&rbd_dev->header); |
| 4514 | blk_queue_max_hw_sectors(q, segment_size / SECTOR_SIZE); |
Ilya Dryomov | 0d9fde4 | 2015-10-07 16:09:35 +0200 | [diff] [blame] | 4515 | q->limits.max_sectors = queue_max_hw_sectors(q); |
Ilya Dryomov | d3834fe | 2015-06-12 19:19:02 +0300 | [diff] [blame] | 4516 | blk_queue_max_segments(q, segment_size / SECTOR_SIZE); |
Alex Elder | 593a9e7 | 2012-02-07 12:03:37 -0600 | [diff] [blame] | 4517 | blk_queue_max_segment_size(q, segment_size); |
| 4518 | blk_queue_io_min(q, segment_size); |
| 4519 | blk_queue_io_opt(q, segment_size); |
Josh Durgin | 029bcbd | 2011-07-22 11:35:23 -0700 | [diff] [blame] | 4520 | |
Guangliang Zhao | 90e98c5 | 2014-04-01 22:22:16 +0800 | [diff] [blame] | 4521 | /* enable the discard support */ |
| 4522 | queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, q); |
| 4523 | q->limits.discard_granularity = segment_size; |
| 4524 | q->limits.discard_alignment = segment_size; |
Jens Axboe | 2bb4cd5 | 2015-07-14 08:15:12 -0600 | [diff] [blame] | 4525 | blk_queue_max_discard_sectors(q, segment_size / SECTOR_SIZE); |
Josh Durgin | b76f823 | 2014-04-07 16:52:03 -0700 | [diff] [blame] | 4526 | q->limits.discard_zeroes_data = 1; |
Guangliang Zhao | 90e98c5 | 2014-04-01 22:22:16 +0800 | [diff] [blame] | 4527 | |
Ronny Hegewald | bae818e | 2015-10-15 18:50:46 +0000 | [diff] [blame] | 4528 | if (!ceph_test_opt(rbd_dev->rbd_client->client, NOCRC)) |
| 4529 | q->backing_dev_info.capabilities |= BDI_CAP_STABLE_WRITES; |
| 4530 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 4531 | disk->queue = q; |
| 4532 | |
| 4533 | q->queuedata = rbd_dev; |
| 4534 | |
| 4535 | rbd_dev->disk = disk; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 4536 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 4537 | return 0; |
Christoph Hellwig | 7ad18af | 2015-01-13 17:20:04 +0100 | [diff] [blame] | 4538 | out_tag_set: |
| 4539 | blk_mq_free_tag_set(&rbd_dev->tag_set); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 4540 | out_disk: |
| 4541 | put_disk(disk); |
Christoph Hellwig | 7ad18af | 2015-01-13 17:20:04 +0100 | [diff] [blame] | 4542 | return err; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 4543 | } |
| 4544 | |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4545 | /* |
| 4546 | sysfs |
| 4547 | */ |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 4548 | |
Alex Elder | 593a9e7 | 2012-02-07 12:03:37 -0600 | [diff] [blame] | 4549 | static struct rbd_device *dev_to_rbd_dev(struct device *dev) |
| 4550 | { |
| 4551 | return container_of(dev, struct rbd_device, dev); |
| 4552 | } |
| 4553 | |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4554 | static ssize_t rbd_size_show(struct device *dev, |
| 4555 | struct device_attribute *attr, char *buf) |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 4556 | { |
Alex Elder | 593a9e7 | 2012-02-07 12:03:37 -0600 | [diff] [blame] | 4557 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4558 | |
Alex Elder | fc71d83 | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 4559 | return sprintf(buf, "%llu\n", |
| 4560 | (unsigned long long)rbd_dev->mapping.size); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 4561 | } |
| 4562 | |
Alex Elder | 34b1318 | 2012-07-13 20:35:12 -0500 | [diff] [blame] | 4563 | /* |
| 4564 | * Note this shows the features for whatever's mapped, which is not |
| 4565 | * necessarily the base image. |
| 4566 | */ |
| 4567 | static ssize_t rbd_features_show(struct device *dev, |
| 4568 | struct device_attribute *attr, char *buf) |
| 4569 | { |
| 4570 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
| 4571 | |
| 4572 | return sprintf(buf, "0x%016llx\n", |
Alex Elder | fc71d83 | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 4573 | (unsigned long long)rbd_dev->mapping.features); |
Alex Elder | 34b1318 | 2012-07-13 20:35:12 -0500 | [diff] [blame] | 4574 | } |
| 4575 | |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4576 | static ssize_t rbd_major_show(struct device *dev, |
| 4577 | struct device_attribute *attr, char *buf) |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 4578 | { |
Alex Elder | 593a9e7 | 2012-02-07 12:03:37 -0600 | [diff] [blame] | 4579 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4580 | |
Alex Elder | fc71d83 | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 4581 | if (rbd_dev->major) |
| 4582 | return sprintf(buf, "%d\n", rbd_dev->major); |
| 4583 | |
| 4584 | return sprintf(buf, "(none)\n"); |
Ilya Dryomov | dd82fff | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 4585 | } |
Alex Elder | fc71d83 | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 4586 | |
Ilya Dryomov | dd82fff | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 4587 | static ssize_t rbd_minor_show(struct device *dev, |
| 4588 | struct device_attribute *attr, char *buf) |
| 4589 | { |
| 4590 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
| 4591 | |
| 4592 | return sprintf(buf, "%d\n", rbd_dev->minor); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4593 | } |
| 4594 | |
| 4595 | static ssize_t rbd_client_id_show(struct device *dev, |
| 4596 | struct device_attribute *attr, char *buf) |
| 4597 | { |
Alex Elder | 593a9e7 | 2012-02-07 12:03:37 -0600 | [diff] [blame] | 4598 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4599 | |
Alex Elder | 1dbb439 | 2012-01-24 10:08:37 -0600 | [diff] [blame] | 4600 | return sprintf(buf, "client%lld\n", |
Ilya Dryomov | 033268a | 2016-08-12 14:59:58 +0200 | [diff] [blame] | 4601 | ceph_client_gid(rbd_dev->rbd_client->client)); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4602 | } |
| 4603 | |
| 4604 | static ssize_t rbd_pool_show(struct device *dev, |
| 4605 | struct device_attribute *attr, char *buf) |
| 4606 | { |
Alex Elder | 593a9e7 | 2012-02-07 12:03:37 -0600 | [diff] [blame] | 4607 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4608 | |
Alex Elder | 0d7dbfc | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 4609 | return sprintf(buf, "%s\n", rbd_dev->spec->pool_name); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4610 | } |
| 4611 | |
Alex Elder | 9bb2f33 | 2012-07-12 10:46:35 -0500 | [diff] [blame] | 4612 | static ssize_t rbd_pool_id_show(struct device *dev, |
| 4613 | struct device_attribute *attr, char *buf) |
| 4614 | { |
| 4615 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
| 4616 | |
Alex Elder | 0d7dbfc | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 4617 | return sprintf(buf, "%llu\n", |
Alex Elder | fc71d83 | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 4618 | (unsigned long long) rbd_dev->spec->pool_id); |
Alex Elder | 9bb2f33 | 2012-07-12 10:46:35 -0500 | [diff] [blame] | 4619 | } |
| 4620 | |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4621 | static ssize_t rbd_name_show(struct device *dev, |
| 4622 | struct device_attribute *attr, char *buf) |
| 4623 | { |
Alex Elder | 593a9e7 | 2012-02-07 12:03:37 -0600 | [diff] [blame] | 4624 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4625 | |
Alex Elder | a92ffdf | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 4626 | if (rbd_dev->spec->image_name) |
| 4627 | return sprintf(buf, "%s\n", rbd_dev->spec->image_name); |
| 4628 | |
| 4629 | return sprintf(buf, "(unknown)\n"); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4630 | } |
| 4631 | |
Alex Elder | 589d30e | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 4632 | static ssize_t rbd_image_id_show(struct device *dev, |
| 4633 | struct device_attribute *attr, char *buf) |
| 4634 | { |
| 4635 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
| 4636 | |
Alex Elder | 0d7dbfc | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 4637 | return sprintf(buf, "%s\n", rbd_dev->spec->image_id); |
Alex Elder | 589d30e | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 4638 | } |
| 4639 | |
Alex Elder | 34b1318 | 2012-07-13 20:35:12 -0500 | [diff] [blame] | 4640 | /* |
| 4641 | * Shows the name of the currently-mapped snapshot (or |
| 4642 | * RBD_SNAP_HEAD_NAME for the base image). |
| 4643 | */ |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4644 | static ssize_t rbd_snap_show(struct device *dev, |
| 4645 | struct device_attribute *attr, |
| 4646 | char *buf) |
| 4647 | { |
Alex Elder | 593a9e7 | 2012-02-07 12:03:37 -0600 | [diff] [blame] | 4648 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4649 | |
Alex Elder | 0d7dbfc | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 4650 | return sprintf(buf, "%s\n", rbd_dev->spec->snap_name); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4651 | } |
| 4652 | |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4653 | /* |
Ilya Dryomov | ff96128 | 2014-07-22 21:53:07 +0400 | [diff] [blame] | 4654 | * For a v2 image, shows the chain of parent images, separated by empty |
| 4655 | * lines. For v1 images or if there is no parent, shows "(no parent |
| 4656 | * image)". |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4657 | */ |
| 4658 | static ssize_t rbd_parent_show(struct device *dev, |
Ilya Dryomov | ff96128 | 2014-07-22 21:53:07 +0400 | [diff] [blame] | 4659 | struct device_attribute *attr, |
| 4660 | char *buf) |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4661 | { |
| 4662 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
Ilya Dryomov | ff96128 | 2014-07-22 21:53:07 +0400 | [diff] [blame] | 4663 | ssize_t count = 0; |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4664 | |
Ilya Dryomov | ff96128 | 2014-07-22 21:53:07 +0400 | [diff] [blame] | 4665 | if (!rbd_dev->parent) |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4666 | return sprintf(buf, "(no parent image)\n"); |
| 4667 | |
Ilya Dryomov | ff96128 | 2014-07-22 21:53:07 +0400 | [diff] [blame] | 4668 | for ( ; rbd_dev->parent; rbd_dev = rbd_dev->parent) { |
| 4669 | struct rbd_spec *spec = rbd_dev->parent_spec; |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4670 | |
Ilya Dryomov | ff96128 | 2014-07-22 21:53:07 +0400 | [diff] [blame] | 4671 | count += sprintf(&buf[count], "%s" |
| 4672 | "pool_id %llu\npool_name %s\n" |
| 4673 | "image_id %s\nimage_name %s\n" |
| 4674 | "snap_id %llu\nsnap_name %s\n" |
| 4675 | "overlap %llu\n", |
| 4676 | !count ? "" : "\n", /* first? */ |
| 4677 | spec->pool_id, spec->pool_name, |
| 4678 | spec->image_id, spec->image_name ?: "(unknown)", |
| 4679 | spec->snap_id, spec->snap_name, |
| 4680 | rbd_dev->parent_overlap); |
| 4681 | } |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4682 | |
Ilya Dryomov | ff96128 | 2014-07-22 21:53:07 +0400 | [diff] [blame] | 4683 | return count; |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4684 | } |
| 4685 | |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4686 | static ssize_t rbd_image_refresh(struct device *dev, |
| 4687 | struct device_attribute *attr, |
| 4688 | const char *buf, |
| 4689 | size_t size) |
| 4690 | { |
Alex Elder | 593a9e7 | 2012-02-07 12:03:37 -0600 | [diff] [blame] | 4691 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
Alex Elder | b813623 | 2012-07-25 09:32:41 -0500 | [diff] [blame] | 4692 | int ret; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 4693 | |
Alex Elder | cc4a38bd | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 4694 | ret = rbd_dev_refresh(rbd_dev); |
Alex Elder | e627db0 | 2013-05-06 07:40:30 -0500 | [diff] [blame] | 4695 | if (ret) |
Ilya Dryomov | 52bb1f9 | 2014-07-23 17:11:20 +0400 | [diff] [blame] | 4696 | return ret; |
Alex Elder | b813623 | 2012-07-25 09:32:41 -0500 | [diff] [blame] | 4697 | |
Ilya Dryomov | 52bb1f9 | 2014-07-23 17:11:20 +0400 | [diff] [blame] | 4698 | return size; |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4699 | } |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 4700 | |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4701 | static DEVICE_ATTR(size, S_IRUGO, rbd_size_show, NULL); |
Alex Elder | 34b1318 | 2012-07-13 20:35:12 -0500 | [diff] [blame] | 4702 | static DEVICE_ATTR(features, S_IRUGO, rbd_features_show, NULL); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4703 | static DEVICE_ATTR(major, S_IRUGO, rbd_major_show, NULL); |
Ilya Dryomov | dd82fff | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 4704 | static DEVICE_ATTR(minor, S_IRUGO, rbd_minor_show, NULL); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4705 | static DEVICE_ATTR(client_id, S_IRUGO, rbd_client_id_show, NULL); |
| 4706 | static DEVICE_ATTR(pool, S_IRUGO, rbd_pool_show, NULL); |
Alex Elder | 9bb2f33 | 2012-07-12 10:46:35 -0500 | [diff] [blame] | 4707 | static DEVICE_ATTR(pool_id, S_IRUGO, rbd_pool_id_show, NULL); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4708 | static DEVICE_ATTR(name, S_IRUGO, rbd_name_show, NULL); |
Alex Elder | 589d30e | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 4709 | static DEVICE_ATTR(image_id, S_IRUGO, rbd_image_id_show, NULL); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4710 | static DEVICE_ATTR(refresh, S_IWUSR, NULL, rbd_image_refresh); |
| 4711 | static DEVICE_ATTR(current_snap, S_IRUGO, rbd_snap_show, NULL); |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4712 | static DEVICE_ATTR(parent, S_IRUGO, rbd_parent_show, NULL); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4713 | |
| 4714 | static struct attribute *rbd_attrs[] = { |
| 4715 | &dev_attr_size.attr, |
Alex Elder | 34b1318 | 2012-07-13 20:35:12 -0500 | [diff] [blame] | 4716 | &dev_attr_features.attr, |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4717 | &dev_attr_major.attr, |
Ilya Dryomov | dd82fff | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 4718 | &dev_attr_minor.attr, |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4719 | &dev_attr_client_id.attr, |
| 4720 | &dev_attr_pool.attr, |
Alex Elder | 9bb2f33 | 2012-07-12 10:46:35 -0500 | [diff] [blame] | 4721 | &dev_attr_pool_id.attr, |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4722 | &dev_attr_name.attr, |
Alex Elder | 589d30e | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 4723 | &dev_attr_image_id.attr, |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4724 | &dev_attr_current_snap.attr, |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4725 | &dev_attr_parent.attr, |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4726 | &dev_attr_refresh.attr, |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4727 | NULL |
| 4728 | }; |
| 4729 | |
| 4730 | static struct attribute_group rbd_attr_group = { |
| 4731 | .attrs = rbd_attrs, |
| 4732 | }; |
| 4733 | |
| 4734 | static const struct attribute_group *rbd_attr_groups[] = { |
| 4735 | &rbd_attr_group, |
| 4736 | NULL |
| 4737 | }; |
| 4738 | |
Ilya Dryomov | 6cac469 | 2015-10-16 20:11:25 +0200 | [diff] [blame] | 4739 | static void rbd_dev_release(struct device *dev); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4740 | |
| 4741 | static struct device_type rbd_device_type = { |
| 4742 | .name = "rbd", |
| 4743 | .groups = rbd_attr_groups, |
Ilya Dryomov | 6cac469 | 2015-10-16 20:11:25 +0200 | [diff] [blame] | 4744 | .release = rbd_dev_release, |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4745 | }; |
| 4746 | |
Alex Elder | 8b8fb99 | 2012-10-26 17:25:24 -0500 | [diff] [blame] | 4747 | static struct rbd_spec *rbd_spec_get(struct rbd_spec *spec) |
| 4748 | { |
| 4749 | kref_get(&spec->kref); |
| 4750 | |
| 4751 | return spec; |
| 4752 | } |
| 4753 | |
| 4754 | static void rbd_spec_free(struct kref *kref); |
| 4755 | static void rbd_spec_put(struct rbd_spec *spec) |
| 4756 | { |
| 4757 | if (spec) |
| 4758 | kref_put(&spec->kref, rbd_spec_free); |
| 4759 | } |
| 4760 | |
| 4761 | static struct rbd_spec *rbd_spec_alloc(void) |
| 4762 | { |
| 4763 | struct rbd_spec *spec; |
| 4764 | |
| 4765 | spec = kzalloc(sizeof (*spec), GFP_KERNEL); |
| 4766 | if (!spec) |
| 4767 | return NULL; |
Ilya Dryomov | 0407759 | 2014-07-23 17:11:20 +0400 | [diff] [blame] | 4768 | |
| 4769 | spec->pool_id = CEPH_NOPOOL; |
| 4770 | spec->snap_id = CEPH_NOSNAP; |
Alex Elder | 8b8fb99 | 2012-10-26 17:25:24 -0500 | [diff] [blame] | 4771 | kref_init(&spec->kref); |
| 4772 | |
Alex Elder | 8b8fb99 | 2012-10-26 17:25:24 -0500 | [diff] [blame] | 4773 | return spec; |
| 4774 | } |
| 4775 | |
| 4776 | static void rbd_spec_free(struct kref *kref) |
| 4777 | { |
| 4778 | struct rbd_spec *spec = container_of(kref, struct rbd_spec, kref); |
| 4779 | |
| 4780 | kfree(spec->pool_name); |
| 4781 | kfree(spec->image_id); |
| 4782 | kfree(spec->image_name); |
| 4783 | kfree(spec->snap_name); |
| 4784 | kfree(spec); |
| 4785 | } |
| 4786 | |
Ilya Dryomov | 1643dfa | 2016-08-12 15:45:52 +0200 | [diff] [blame] | 4787 | static void rbd_dev_free(struct rbd_device *rbd_dev) |
Ilya Dryomov | dd5ac32 | 2015-10-16 17:09:24 +0200 | [diff] [blame] | 4788 | { |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 4789 | WARN_ON(rbd_dev->watch_state != RBD_WATCH_STATE_UNREGISTERED); |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 4790 | WARN_ON(rbd_dev->lock_state != RBD_LOCK_STATE_UNLOCKED); |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 4791 | |
Ilya Dryomov | c41d13a | 2016-04-29 20:01:25 +0200 | [diff] [blame] | 4792 | ceph_oid_destroy(&rbd_dev->header_oid); |
Ilya Dryomov | 6b6dddb | 2016-08-05 16:15:38 +0200 | [diff] [blame] | 4793 | ceph_oloc_destroy(&rbd_dev->header_oloc); |
Ilya Dryomov | c41d13a | 2016-04-29 20:01:25 +0200 | [diff] [blame] | 4794 | |
Ilya Dryomov | dd5ac32 | 2015-10-16 17:09:24 +0200 | [diff] [blame] | 4795 | rbd_put_client(rbd_dev->rbd_client); |
| 4796 | rbd_spec_put(rbd_dev->spec); |
| 4797 | kfree(rbd_dev->opts); |
| 4798 | kfree(rbd_dev); |
Ilya Dryomov | 1643dfa | 2016-08-12 15:45:52 +0200 | [diff] [blame] | 4799 | } |
| 4800 | |
| 4801 | static void rbd_dev_release(struct device *dev) |
| 4802 | { |
| 4803 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
| 4804 | bool need_put = !!rbd_dev->opts; |
| 4805 | |
| 4806 | if (need_put) { |
| 4807 | destroy_workqueue(rbd_dev->task_wq); |
| 4808 | ida_simple_remove(&rbd_dev_id_ida, rbd_dev->dev_id); |
| 4809 | } |
| 4810 | |
| 4811 | rbd_dev_free(rbd_dev); |
Ilya Dryomov | dd5ac32 | 2015-10-16 17:09:24 +0200 | [diff] [blame] | 4812 | |
| 4813 | /* |
| 4814 | * This is racy, but way better than putting module outside of |
| 4815 | * the release callback. The race window is pretty small, so |
| 4816 | * doing something similar to dm (dm-builtin.c) is overkill. |
| 4817 | */ |
| 4818 | if (need_put) |
| 4819 | module_put(THIS_MODULE); |
| 4820 | } |
| 4821 | |
Ilya Dryomov | 1643dfa | 2016-08-12 15:45:52 +0200 | [diff] [blame] | 4822 | static struct rbd_device *__rbd_dev_create(struct rbd_client *rbdc, |
| 4823 | struct rbd_spec *spec) |
Alex Elder | c53d589 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4824 | { |
| 4825 | struct rbd_device *rbd_dev; |
| 4826 | |
Ilya Dryomov | 1643dfa | 2016-08-12 15:45:52 +0200 | [diff] [blame] | 4827 | rbd_dev = kzalloc(sizeof(*rbd_dev), GFP_KERNEL); |
Alex Elder | c53d589 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4828 | if (!rbd_dev) |
| 4829 | return NULL; |
| 4830 | |
| 4831 | spin_lock_init(&rbd_dev->lock); |
| 4832 | INIT_LIST_HEAD(&rbd_dev->node); |
Alex Elder | c53d589 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4833 | init_rwsem(&rbd_dev->header_rwsem); |
| 4834 | |
Ilya Dryomov | c41d13a | 2016-04-29 20:01:25 +0200 | [diff] [blame] | 4835 | ceph_oid_init(&rbd_dev->header_oid); |
Ilya Dryomov | 922dab6 | 2016-05-26 01:15:02 +0200 | [diff] [blame] | 4836 | ceph_oloc_init(&rbd_dev->header_oloc); |
Ilya Dryomov | c41d13a | 2016-04-29 20:01:25 +0200 | [diff] [blame] | 4837 | |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 4838 | mutex_init(&rbd_dev->watch_mutex); |
| 4839 | rbd_dev->watch_state = RBD_WATCH_STATE_UNREGISTERED; |
| 4840 | INIT_DELAYED_WORK(&rbd_dev->watch_dwork, rbd_reregister_watch); |
| 4841 | |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 4842 | init_rwsem(&rbd_dev->lock_rwsem); |
| 4843 | rbd_dev->lock_state = RBD_LOCK_STATE_UNLOCKED; |
| 4844 | INIT_WORK(&rbd_dev->acquired_lock_work, rbd_notify_acquired_lock); |
| 4845 | INIT_WORK(&rbd_dev->released_lock_work, rbd_notify_released_lock); |
| 4846 | INIT_DELAYED_WORK(&rbd_dev->lock_dwork, rbd_acquire_lock); |
| 4847 | INIT_WORK(&rbd_dev->unlock_work, rbd_release_lock_work); |
| 4848 | init_waitqueue_head(&rbd_dev->lock_waitq); |
| 4849 | |
Ilya Dryomov | dd5ac32 | 2015-10-16 17:09:24 +0200 | [diff] [blame] | 4850 | rbd_dev->dev.bus = &rbd_bus_type; |
| 4851 | rbd_dev->dev.type = &rbd_device_type; |
| 4852 | rbd_dev->dev.parent = &rbd_root_dev; |
Ilya Dryomov | dd5ac32 | 2015-10-16 17:09:24 +0200 | [diff] [blame] | 4853 | device_initialize(&rbd_dev->dev); |
| 4854 | |
Alex Elder | c53d589 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4855 | rbd_dev->rbd_client = rbdc; |
Ilya Dryomov | d147543 | 2015-06-22 13:24:48 +0300 | [diff] [blame] | 4856 | rbd_dev->spec = spec; |
Alex Elder | 0903e87 | 2012-11-14 12:25:19 -0600 | [diff] [blame] | 4857 | |
Yan, Zheng | 7627151 | 2016-02-03 21:24:49 +0800 | [diff] [blame] | 4858 | rbd_dev->layout.stripe_unit = 1 << RBD_MAX_OBJ_ORDER; |
| 4859 | rbd_dev->layout.stripe_count = 1; |
| 4860 | rbd_dev->layout.object_size = 1 << RBD_MAX_OBJ_ORDER; |
| 4861 | rbd_dev->layout.pool_id = spec->pool_id; |
Yan, Zheng | 30c156d | 2016-02-14 11:24:31 +0800 | [diff] [blame] | 4862 | RCU_INIT_POINTER(rbd_dev->layout.pool_ns, NULL); |
Alex Elder | 0903e87 | 2012-11-14 12:25:19 -0600 | [diff] [blame] | 4863 | |
Alex Elder | c53d589 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4864 | return rbd_dev; |
| 4865 | } |
| 4866 | |
Ilya Dryomov | 1643dfa | 2016-08-12 15:45:52 +0200 | [diff] [blame] | 4867 | /* |
| 4868 | * Create a mapping rbd_dev. |
| 4869 | */ |
| 4870 | static struct rbd_device *rbd_dev_create(struct rbd_client *rbdc, |
| 4871 | struct rbd_spec *spec, |
| 4872 | struct rbd_options *opts) |
| 4873 | { |
| 4874 | struct rbd_device *rbd_dev; |
| 4875 | |
| 4876 | rbd_dev = __rbd_dev_create(rbdc, spec); |
| 4877 | if (!rbd_dev) |
| 4878 | return NULL; |
| 4879 | |
| 4880 | rbd_dev->opts = opts; |
| 4881 | |
| 4882 | /* get an id and fill in device name */ |
| 4883 | rbd_dev->dev_id = ida_simple_get(&rbd_dev_id_ida, 0, |
| 4884 | minor_to_rbd_dev_id(1 << MINORBITS), |
| 4885 | GFP_KERNEL); |
| 4886 | if (rbd_dev->dev_id < 0) |
| 4887 | goto fail_rbd_dev; |
| 4888 | |
| 4889 | sprintf(rbd_dev->name, RBD_DRV_NAME "%d", rbd_dev->dev_id); |
| 4890 | rbd_dev->task_wq = alloc_ordered_workqueue("%s-tasks", WQ_MEM_RECLAIM, |
| 4891 | rbd_dev->name); |
| 4892 | if (!rbd_dev->task_wq) |
| 4893 | goto fail_dev_id; |
| 4894 | |
| 4895 | /* we have a ref from do_rbd_add() */ |
| 4896 | __module_get(THIS_MODULE); |
| 4897 | |
| 4898 | dout("%s rbd_dev %p dev_id %d\n", __func__, rbd_dev, rbd_dev->dev_id); |
| 4899 | return rbd_dev; |
| 4900 | |
| 4901 | fail_dev_id: |
| 4902 | ida_simple_remove(&rbd_dev_id_ida, rbd_dev->dev_id); |
| 4903 | fail_rbd_dev: |
| 4904 | rbd_dev_free(rbd_dev); |
| 4905 | return NULL; |
| 4906 | } |
| 4907 | |
Alex Elder | c53d589 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4908 | static void rbd_dev_destroy(struct rbd_device *rbd_dev) |
| 4909 | { |
Ilya Dryomov | dd5ac32 | 2015-10-16 17:09:24 +0200 | [diff] [blame] | 4910 | if (rbd_dev) |
| 4911 | put_device(&rbd_dev->dev); |
Alex Elder | c53d589 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4912 | } |
| 4913 | |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4914 | /* |
Alex Elder | 9d475de | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 4915 | * Get the size and object order for an image snapshot, or if |
| 4916 | * snap_id is CEPH_NOSNAP, gets this information for the base |
| 4917 | * image. |
| 4918 | */ |
| 4919 | static int _rbd_dev_v2_snap_size(struct rbd_device *rbd_dev, u64 snap_id, |
| 4920 | u8 *order, u64 *snap_size) |
| 4921 | { |
| 4922 | __le64 snapid = cpu_to_le64(snap_id); |
| 4923 | int ret; |
| 4924 | struct { |
| 4925 | u8 order; |
| 4926 | __le64 size; |
| 4927 | } __attribute__ ((packed)) size_buf = { 0 }; |
| 4928 | |
Ilya Dryomov | c41d13a | 2016-04-29 20:01:25 +0200 | [diff] [blame] | 4929 | ret = rbd_obj_method_sync(rbd_dev, rbd_dev->header_oid.name, |
Alex Elder | 9d475de | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 4930 | "rbd", "get_size", |
Alex Elder | 4157976 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 4931 | &snapid, sizeof (snapid), |
Alex Elder | e2a58ee | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 4932 | &size_buf, sizeof (size_buf)); |
Alex Elder | 36be9a7 | 2013-01-19 00:30:28 -0600 | [diff] [blame] | 4933 | dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret); |
Alex Elder | 9d475de | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 4934 | if (ret < 0) |
| 4935 | return ret; |
Alex Elder | 57385b5 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 4936 | if (ret < sizeof (size_buf)) |
| 4937 | return -ERANGE; |
Alex Elder | 9d475de | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 4938 | |
Josh Durgin | c354557 | 2013-08-28 17:08:10 -0700 | [diff] [blame] | 4939 | if (order) { |
Alex Elder | c86f86e | 2013-04-25 15:09:41 -0500 | [diff] [blame] | 4940 | *order = size_buf.order; |
Josh Durgin | c354557 | 2013-08-28 17:08:10 -0700 | [diff] [blame] | 4941 | dout(" order %u", (unsigned int)*order); |
| 4942 | } |
Alex Elder | 9d475de | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 4943 | *snap_size = le64_to_cpu(size_buf.size); |
| 4944 | |
Josh Durgin | c354557 | 2013-08-28 17:08:10 -0700 | [diff] [blame] | 4945 | dout(" snap_id 0x%016llx snap_size = %llu\n", |
| 4946 | (unsigned long long)snap_id, |
Alex Elder | 57385b5 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 4947 | (unsigned long long)*snap_size); |
Alex Elder | 9d475de | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 4948 | |
| 4949 | return 0; |
| 4950 | } |
| 4951 | |
| 4952 | static int rbd_dev_v2_image_size(struct rbd_device *rbd_dev) |
| 4953 | { |
| 4954 | return _rbd_dev_v2_snap_size(rbd_dev, CEPH_NOSNAP, |
| 4955 | &rbd_dev->header.obj_order, |
| 4956 | &rbd_dev->header.image_size); |
| 4957 | } |
| 4958 | |
Alex Elder | 1e13019 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 4959 | static int rbd_dev_v2_object_prefix(struct rbd_device *rbd_dev) |
| 4960 | { |
| 4961 | void *reply_buf; |
| 4962 | int ret; |
| 4963 | void *p; |
| 4964 | |
| 4965 | reply_buf = kzalloc(RBD_OBJ_PREFIX_LEN_MAX, GFP_KERNEL); |
| 4966 | if (!reply_buf) |
| 4967 | return -ENOMEM; |
| 4968 | |
Ilya Dryomov | c41d13a | 2016-04-29 20:01:25 +0200 | [diff] [blame] | 4969 | ret = rbd_obj_method_sync(rbd_dev, rbd_dev->header_oid.name, |
Alex Elder | 4157976 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 4970 | "rbd", "get_object_prefix", NULL, 0, |
Alex Elder | e2a58ee | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 4971 | reply_buf, RBD_OBJ_PREFIX_LEN_MAX); |
Alex Elder | 36be9a7 | 2013-01-19 00:30:28 -0600 | [diff] [blame] | 4972 | dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret); |
Alex Elder | 1e13019 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 4973 | if (ret < 0) |
| 4974 | goto out; |
| 4975 | |
| 4976 | p = reply_buf; |
| 4977 | rbd_dev->header.object_prefix = ceph_extract_encoded_string(&p, |
Alex Elder | 57385b5 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 4978 | p + ret, NULL, GFP_NOIO); |
| 4979 | ret = 0; |
Alex Elder | 1e13019 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 4980 | |
| 4981 | if (IS_ERR(rbd_dev->header.object_prefix)) { |
| 4982 | ret = PTR_ERR(rbd_dev->header.object_prefix); |
| 4983 | rbd_dev->header.object_prefix = NULL; |
| 4984 | } else { |
| 4985 | dout(" object_prefix = %s\n", rbd_dev->header.object_prefix); |
| 4986 | } |
Alex Elder | 1e13019 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 4987 | out: |
| 4988 | kfree(reply_buf); |
| 4989 | |
| 4990 | return ret; |
| 4991 | } |
| 4992 | |
Alex Elder | b1b5402 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 4993 | static int _rbd_dev_v2_snap_features(struct rbd_device *rbd_dev, u64 snap_id, |
| 4994 | u64 *snap_features) |
| 4995 | { |
| 4996 | __le64 snapid = cpu_to_le64(snap_id); |
| 4997 | struct { |
| 4998 | __le64 features; |
| 4999 | __le64 incompat; |
Alex Elder | 4157976 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 5000 | } __attribute__ ((packed)) features_buf = { 0 }; |
Ilya Dryomov | d3767f0 | 2016-04-13 14:15:50 +0200 | [diff] [blame] | 5001 | u64 unsup; |
Alex Elder | b1b5402 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5002 | int ret; |
| 5003 | |
Ilya Dryomov | c41d13a | 2016-04-29 20:01:25 +0200 | [diff] [blame] | 5004 | ret = rbd_obj_method_sync(rbd_dev, rbd_dev->header_oid.name, |
Alex Elder | b1b5402 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5005 | "rbd", "get_features", |
Alex Elder | 4157976 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 5006 | &snapid, sizeof (snapid), |
Alex Elder | e2a58ee | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 5007 | &features_buf, sizeof (features_buf)); |
Alex Elder | 36be9a7 | 2013-01-19 00:30:28 -0600 | [diff] [blame] | 5008 | dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret); |
Alex Elder | b1b5402 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5009 | if (ret < 0) |
| 5010 | return ret; |
Alex Elder | 57385b5 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 5011 | if (ret < sizeof (features_buf)) |
| 5012 | return -ERANGE; |
Alex Elder | d889140 | 2012-10-09 13:50:17 -0700 | [diff] [blame] | 5013 | |
Ilya Dryomov | d3767f0 | 2016-04-13 14:15:50 +0200 | [diff] [blame] | 5014 | unsup = le64_to_cpu(features_buf.incompat) & ~RBD_FEATURES_SUPPORTED; |
| 5015 | if (unsup) { |
| 5016 | rbd_warn(rbd_dev, "image uses unsupported features: 0x%llx", |
| 5017 | unsup); |
Alex Elder | b8f5c6e | 2012-11-01 08:39:26 -0500 | [diff] [blame] | 5018 | return -ENXIO; |
Ilya Dryomov | d3767f0 | 2016-04-13 14:15:50 +0200 | [diff] [blame] | 5019 | } |
Alex Elder | d889140 | 2012-10-09 13:50:17 -0700 | [diff] [blame] | 5020 | |
Alex Elder | b1b5402 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5021 | *snap_features = le64_to_cpu(features_buf.features); |
| 5022 | |
| 5023 | dout(" snap_id 0x%016llx features = 0x%016llx incompat = 0x%016llx\n", |
Alex Elder | 57385b5 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 5024 | (unsigned long long)snap_id, |
| 5025 | (unsigned long long)*snap_features, |
| 5026 | (unsigned long long)le64_to_cpu(features_buf.incompat)); |
Alex Elder | b1b5402 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5027 | |
| 5028 | return 0; |
| 5029 | } |
| 5030 | |
| 5031 | static int rbd_dev_v2_features(struct rbd_device *rbd_dev) |
| 5032 | { |
| 5033 | return _rbd_dev_v2_snap_features(rbd_dev, CEPH_NOSNAP, |
| 5034 | &rbd_dev->header.features); |
| 5035 | } |
| 5036 | |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5037 | static int rbd_dev_v2_parent_info(struct rbd_device *rbd_dev) |
| 5038 | { |
| 5039 | struct rbd_spec *parent_spec; |
| 5040 | size_t size; |
| 5041 | void *reply_buf = NULL; |
| 5042 | __le64 snapid; |
| 5043 | void *p; |
| 5044 | void *end; |
Alex Elder | 642a253 | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 5045 | u64 pool_id; |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5046 | char *image_id; |
Alex Elder | 3b5cf2a | 2013-05-29 11:18:59 -0500 | [diff] [blame] | 5047 | u64 snap_id; |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5048 | u64 overlap; |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5049 | int ret; |
| 5050 | |
| 5051 | parent_spec = rbd_spec_alloc(); |
| 5052 | if (!parent_spec) |
| 5053 | return -ENOMEM; |
| 5054 | |
| 5055 | size = sizeof (__le64) + /* pool_id */ |
| 5056 | sizeof (__le32) + RBD_IMAGE_ID_LEN_MAX + /* image_id */ |
| 5057 | sizeof (__le64) + /* snap_id */ |
| 5058 | sizeof (__le64); /* overlap */ |
| 5059 | reply_buf = kmalloc(size, GFP_KERNEL); |
| 5060 | if (!reply_buf) { |
| 5061 | ret = -ENOMEM; |
| 5062 | goto out_err; |
| 5063 | } |
| 5064 | |
Ilya Dryomov | 4d9b67c | 2014-07-24 10:42:13 +0400 | [diff] [blame] | 5065 | snapid = cpu_to_le64(rbd_dev->spec->snap_id); |
Ilya Dryomov | c41d13a | 2016-04-29 20:01:25 +0200 | [diff] [blame] | 5066 | ret = rbd_obj_method_sync(rbd_dev, rbd_dev->header_oid.name, |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5067 | "rbd", "get_parent", |
Alex Elder | 4157976 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 5068 | &snapid, sizeof (snapid), |
Alex Elder | e2a58ee | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 5069 | reply_buf, size); |
Alex Elder | 36be9a7 | 2013-01-19 00:30:28 -0600 | [diff] [blame] | 5070 | dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret); |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5071 | if (ret < 0) |
| 5072 | goto out_err; |
| 5073 | |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5074 | p = reply_buf; |
Alex Elder | 57385b5 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 5075 | end = reply_buf + ret; |
| 5076 | ret = -ERANGE; |
Alex Elder | 642a253 | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 5077 | ceph_decode_64_safe(&p, end, pool_id, out_err); |
Alex Elder | 392a9da | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 5078 | if (pool_id == CEPH_NOPOOL) { |
| 5079 | /* |
| 5080 | * Either the parent never existed, or we have |
| 5081 | * record of it but the image got flattened so it no |
| 5082 | * longer has a parent. When the parent of a |
| 5083 | * layered image disappears we immediately set the |
| 5084 | * overlap to 0. The effect of this is that all new |
| 5085 | * requests will be treated as if the image had no |
| 5086 | * parent. |
| 5087 | */ |
| 5088 | if (rbd_dev->parent_overlap) { |
| 5089 | rbd_dev->parent_overlap = 0; |
Alex Elder | 392a9da | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 5090 | rbd_dev_parent_put(rbd_dev); |
| 5091 | pr_info("%s: clone image has been flattened\n", |
| 5092 | rbd_dev->disk->disk_name); |
| 5093 | } |
| 5094 | |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5095 | goto out; /* No parent? No problem. */ |
Alex Elder | 392a9da | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 5096 | } |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5097 | |
Alex Elder | 0903e87 | 2012-11-14 12:25:19 -0600 | [diff] [blame] | 5098 | /* The ceph file layout needs to fit pool id in 32 bits */ |
| 5099 | |
| 5100 | ret = -EIO; |
Alex Elder | 642a253 | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 5101 | if (pool_id > (u64)U32_MAX) { |
Ilya Dryomov | 9584d50 | 2014-07-11 12:11:20 +0400 | [diff] [blame] | 5102 | rbd_warn(NULL, "parent pool id too large (%llu > %u)", |
Alex Elder | 642a253 | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 5103 | (unsigned long long)pool_id, U32_MAX); |
Alex Elder | 57385b5 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 5104 | goto out_err; |
Alex Elder | c0cd10db | 2013-04-26 09:43:47 -0500 | [diff] [blame] | 5105 | } |
Alex Elder | 0903e87 | 2012-11-14 12:25:19 -0600 | [diff] [blame] | 5106 | |
Alex Elder | 979ed48 | 2012-11-01 08:39:26 -0500 | [diff] [blame] | 5107 | image_id = ceph_extract_encoded_string(&p, end, NULL, GFP_KERNEL); |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5108 | if (IS_ERR(image_id)) { |
| 5109 | ret = PTR_ERR(image_id); |
| 5110 | goto out_err; |
| 5111 | } |
Alex Elder | 3b5cf2a | 2013-05-29 11:18:59 -0500 | [diff] [blame] | 5112 | ceph_decode_64_safe(&p, end, snap_id, out_err); |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5113 | ceph_decode_64_safe(&p, end, overlap, out_err); |
| 5114 | |
Alex Elder | 3b5cf2a | 2013-05-29 11:18:59 -0500 | [diff] [blame] | 5115 | /* |
| 5116 | * The parent won't change (except when the clone is |
| 5117 | * flattened, already handled that). So we only need to |
| 5118 | * record the parent spec we have not already done so. |
| 5119 | */ |
| 5120 | if (!rbd_dev->parent_spec) { |
| 5121 | parent_spec->pool_id = pool_id; |
| 5122 | parent_spec->image_id = image_id; |
| 5123 | parent_spec->snap_id = snap_id; |
Alex Elder | 70cf49c | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 5124 | rbd_dev->parent_spec = parent_spec; |
| 5125 | parent_spec = NULL; /* rbd_dev now owns this */ |
Ilya Dryomov | fbba11b | 2014-06-27 21:46:33 +0400 | [diff] [blame] | 5126 | } else { |
| 5127 | kfree(image_id); |
Alex Elder | 3b5cf2a | 2013-05-29 11:18:59 -0500 | [diff] [blame] | 5128 | } |
| 5129 | |
| 5130 | /* |
Ilya Dryomov | cf32bd9 | 2015-01-19 22:57:39 +0300 | [diff] [blame] | 5131 | * We always update the parent overlap. If it's zero we issue |
| 5132 | * a warning, as we will proceed as if there was no parent. |
Alex Elder | 3b5cf2a | 2013-05-29 11:18:59 -0500 | [diff] [blame] | 5133 | */ |
Alex Elder | 3b5cf2a | 2013-05-29 11:18:59 -0500 | [diff] [blame] | 5134 | if (!overlap) { |
Alex Elder | 3b5cf2a | 2013-05-29 11:18:59 -0500 | [diff] [blame] | 5135 | if (parent_spec) { |
Ilya Dryomov | cf32bd9 | 2015-01-19 22:57:39 +0300 | [diff] [blame] | 5136 | /* refresh, careful to warn just once */ |
| 5137 | if (rbd_dev->parent_overlap) |
| 5138 | rbd_warn(rbd_dev, |
| 5139 | "clone now standalone (overlap became 0)"); |
Alex Elder | 3b5cf2a | 2013-05-29 11:18:59 -0500 | [diff] [blame] | 5140 | } else { |
Ilya Dryomov | cf32bd9 | 2015-01-19 22:57:39 +0300 | [diff] [blame] | 5141 | /* initial probe */ |
| 5142 | rbd_warn(rbd_dev, "clone is standalone (overlap 0)"); |
Alex Elder | 3b5cf2a | 2013-05-29 11:18:59 -0500 | [diff] [blame] | 5143 | } |
Alex Elder | 70cf49c | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 5144 | } |
Ilya Dryomov | cf32bd9 | 2015-01-19 22:57:39 +0300 | [diff] [blame] | 5145 | rbd_dev->parent_overlap = overlap; |
| 5146 | |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5147 | out: |
| 5148 | ret = 0; |
| 5149 | out_err: |
| 5150 | kfree(reply_buf); |
| 5151 | rbd_spec_put(parent_spec); |
| 5152 | |
| 5153 | return ret; |
| 5154 | } |
| 5155 | |
Alex Elder | cc070d5 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 5156 | static int rbd_dev_v2_striping_info(struct rbd_device *rbd_dev) |
| 5157 | { |
| 5158 | struct { |
| 5159 | __le64 stripe_unit; |
| 5160 | __le64 stripe_count; |
| 5161 | } __attribute__ ((packed)) striping_info_buf = { 0 }; |
| 5162 | size_t size = sizeof (striping_info_buf); |
| 5163 | void *p; |
| 5164 | u64 obj_size; |
| 5165 | u64 stripe_unit; |
| 5166 | u64 stripe_count; |
| 5167 | int ret; |
| 5168 | |
Ilya Dryomov | c41d13a | 2016-04-29 20:01:25 +0200 | [diff] [blame] | 5169 | ret = rbd_obj_method_sync(rbd_dev, rbd_dev->header_oid.name, |
Alex Elder | cc070d5 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 5170 | "rbd", "get_stripe_unit_count", NULL, 0, |
Alex Elder | e2a58ee | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 5171 | (char *)&striping_info_buf, size); |
Alex Elder | cc070d5 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 5172 | dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret); |
| 5173 | if (ret < 0) |
| 5174 | return ret; |
| 5175 | if (ret < size) |
| 5176 | return -ERANGE; |
| 5177 | |
| 5178 | /* |
| 5179 | * We don't actually support the "fancy striping" feature |
| 5180 | * (STRIPINGV2) yet, but if the striping sizes are the |
| 5181 | * defaults the behavior is the same as before. So find |
| 5182 | * out, and only fail if the image has non-default values. |
| 5183 | */ |
| 5184 | ret = -EINVAL; |
| 5185 | obj_size = (u64)1 << rbd_dev->header.obj_order; |
| 5186 | p = &striping_info_buf; |
| 5187 | stripe_unit = ceph_decode_64(&p); |
| 5188 | if (stripe_unit != obj_size) { |
| 5189 | rbd_warn(rbd_dev, "unsupported stripe unit " |
| 5190 | "(got %llu want %llu)", |
| 5191 | stripe_unit, obj_size); |
| 5192 | return -EINVAL; |
| 5193 | } |
| 5194 | stripe_count = ceph_decode_64(&p); |
| 5195 | if (stripe_count != 1) { |
| 5196 | rbd_warn(rbd_dev, "unsupported stripe count " |
| 5197 | "(got %llu want 1)", stripe_count); |
| 5198 | return -EINVAL; |
| 5199 | } |
Alex Elder | 500d0c0 | 2013-04-26 09:43:47 -0500 | [diff] [blame] | 5200 | rbd_dev->header.stripe_unit = stripe_unit; |
| 5201 | rbd_dev->header.stripe_count = stripe_count; |
Alex Elder | cc070d5 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 5202 | |
| 5203 | return 0; |
| 5204 | } |
| 5205 | |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 5206 | static char *rbd_dev_image_name(struct rbd_device *rbd_dev) |
| 5207 | { |
| 5208 | size_t image_id_size; |
| 5209 | char *image_id; |
| 5210 | void *p; |
| 5211 | void *end; |
| 5212 | size_t size; |
| 5213 | void *reply_buf = NULL; |
| 5214 | size_t len = 0; |
| 5215 | char *image_name = NULL; |
| 5216 | int ret; |
| 5217 | |
| 5218 | rbd_assert(!rbd_dev->spec->image_name); |
| 5219 | |
Alex Elder | 69e7a02 | 2012-11-01 08:39:26 -0500 | [diff] [blame] | 5220 | len = strlen(rbd_dev->spec->image_id); |
| 5221 | image_id_size = sizeof (__le32) + len; |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 5222 | image_id = kmalloc(image_id_size, GFP_KERNEL); |
| 5223 | if (!image_id) |
| 5224 | return NULL; |
| 5225 | |
| 5226 | p = image_id; |
Alex Elder | 4157976 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 5227 | end = image_id + image_id_size; |
Alex Elder | 57385b5 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 5228 | ceph_encode_string(&p, end, rbd_dev->spec->image_id, (u32)len); |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 5229 | |
| 5230 | size = sizeof (__le32) + RBD_IMAGE_NAME_LEN_MAX; |
| 5231 | reply_buf = kmalloc(size, GFP_KERNEL); |
| 5232 | if (!reply_buf) |
| 5233 | goto out; |
| 5234 | |
Alex Elder | 36be9a7 | 2013-01-19 00:30:28 -0600 | [diff] [blame] | 5235 | ret = rbd_obj_method_sync(rbd_dev, RBD_DIRECTORY, |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 5236 | "rbd", "dir_get_name", |
| 5237 | image_id, image_id_size, |
Alex Elder | e2a58ee | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 5238 | reply_buf, size); |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 5239 | if (ret < 0) |
| 5240 | goto out; |
| 5241 | p = reply_buf; |
Alex Elder | f40eb34 | 2013-04-25 15:09:42 -0500 | [diff] [blame] | 5242 | end = reply_buf + ret; |
| 5243 | |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 5244 | image_name = ceph_extract_encoded_string(&p, end, &len, GFP_KERNEL); |
| 5245 | if (IS_ERR(image_name)) |
| 5246 | image_name = NULL; |
| 5247 | else |
| 5248 | dout("%s: name is %s len is %zd\n", __func__, image_name, len); |
| 5249 | out: |
| 5250 | kfree(reply_buf); |
| 5251 | kfree(image_id); |
| 5252 | |
| 5253 | return image_name; |
| 5254 | } |
| 5255 | |
Alex Elder | 2ad3d71 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 5256 | static u64 rbd_v1_snap_id_by_name(struct rbd_device *rbd_dev, const char *name) |
| 5257 | { |
| 5258 | struct ceph_snap_context *snapc = rbd_dev->header.snapc; |
| 5259 | const char *snap_name; |
| 5260 | u32 which = 0; |
| 5261 | |
| 5262 | /* Skip over names until we find the one we are looking for */ |
| 5263 | |
| 5264 | snap_name = rbd_dev->header.snap_names; |
| 5265 | while (which < snapc->num_snaps) { |
| 5266 | if (!strcmp(name, snap_name)) |
| 5267 | return snapc->snaps[which]; |
| 5268 | snap_name += strlen(snap_name) + 1; |
| 5269 | which++; |
| 5270 | } |
| 5271 | return CEPH_NOSNAP; |
| 5272 | } |
| 5273 | |
| 5274 | static u64 rbd_v2_snap_id_by_name(struct rbd_device *rbd_dev, const char *name) |
| 5275 | { |
| 5276 | struct ceph_snap_context *snapc = rbd_dev->header.snapc; |
| 5277 | u32 which; |
| 5278 | bool found = false; |
| 5279 | u64 snap_id; |
| 5280 | |
| 5281 | for (which = 0; !found && which < snapc->num_snaps; which++) { |
| 5282 | const char *snap_name; |
| 5283 | |
| 5284 | snap_id = snapc->snaps[which]; |
| 5285 | snap_name = rbd_dev_v2_snap_name(rbd_dev, snap_id); |
Josh Durgin | efadc98 | 2013-08-29 19:16:42 -0700 | [diff] [blame] | 5286 | if (IS_ERR(snap_name)) { |
| 5287 | /* ignore no-longer existing snapshots */ |
| 5288 | if (PTR_ERR(snap_name) == -ENOENT) |
| 5289 | continue; |
| 5290 | else |
| 5291 | break; |
| 5292 | } |
Alex Elder | 2ad3d71 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 5293 | found = !strcmp(name, snap_name); |
| 5294 | kfree(snap_name); |
| 5295 | } |
| 5296 | return found ? snap_id : CEPH_NOSNAP; |
| 5297 | } |
| 5298 | |
| 5299 | /* |
| 5300 | * Assumes name is never RBD_SNAP_HEAD_NAME; returns CEPH_NOSNAP if |
| 5301 | * no snapshot by that name is found, or if an error occurs. |
| 5302 | */ |
| 5303 | static u64 rbd_snap_id_by_name(struct rbd_device *rbd_dev, const char *name) |
| 5304 | { |
| 5305 | if (rbd_dev->image_format == 1) |
| 5306 | return rbd_v1_snap_id_by_name(rbd_dev, name); |
| 5307 | |
| 5308 | return rbd_v2_snap_id_by_name(rbd_dev, name); |
| 5309 | } |
| 5310 | |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 5311 | /* |
Ilya Dryomov | 0407759 | 2014-07-23 17:11:20 +0400 | [diff] [blame] | 5312 | * An image being mapped will have everything but the snap id. |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 5313 | */ |
Ilya Dryomov | 0407759 | 2014-07-23 17:11:20 +0400 | [diff] [blame] | 5314 | static int rbd_spec_fill_snap_id(struct rbd_device *rbd_dev) |
| 5315 | { |
| 5316 | struct rbd_spec *spec = rbd_dev->spec; |
| 5317 | |
| 5318 | rbd_assert(spec->pool_id != CEPH_NOPOOL && spec->pool_name); |
| 5319 | rbd_assert(spec->image_id && spec->image_name); |
| 5320 | rbd_assert(spec->snap_name); |
| 5321 | |
| 5322 | if (strcmp(spec->snap_name, RBD_SNAP_HEAD_NAME)) { |
| 5323 | u64 snap_id; |
| 5324 | |
| 5325 | snap_id = rbd_snap_id_by_name(rbd_dev, spec->snap_name); |
| 5326 | if (snap_id == CEPH_NOSNAP) |
| 5327 | return -ENOENT; |
| 5328 | |
| 5329 | spec->snap_id = snap_id; |
| 5330 | } else { |
| 5331 | spec->snap_id = CEPH_NOSNAP; |
| 5332 | } |
| 5333 | |
| 5334 | return 0; |
| 5335 | } |
| 5336 | |
| 5337 | /* |
| 5338 | * A parent image will have all ids but none of the names. |
| 5339 | * |
| 5340 | * All names in an rbd spec are dynamically allocated. It's OK if we |
| 5341 | * can't figure out the name for an image id. |
| 5342 | */ |
| 5343 | static int rbd_spec_fill_names(struct rbd_device *rbd_dev) |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 5344 | { |
Alex Elder | 2e9f7f1 | 2013-04-26 09:43:48 -0500 | [diff] [blame] | 5345 | struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc; |
| 5346 | struct rbd_spec *spec = rbd_dev->spec; |
| 5347 | const char *pool_name; |
| 5348 | const char *image_name; |
| 5349 | const char *snap_name; |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 5350 | int ret; |
| 5351 | |
Ilya Dryomov | 0407759 | 2014-07-23 17:11:20 +0400 | [diff] [blame] | 5352 | rbd_assert(spec->pool_id != CEPH_NOPOOL); |
| 5353 | rbd_assert(spec->image_id); |
| 5354 | rbd_assert(spec->snap_id != CEPH_NOSNAP); |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 5355 | |
Alex Elder | 2e9f7f1 | 2013-04-26 09:43:48 -0500 | [diff] [blame] | 5356 | /* Get the pool name; we have to make our own copy of this */ |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 5357 | |
Alex Elder | 2e9f7f1 | 2013-04-26 09:43:48 -0500 | [diff] [blame] | 5358 | pool_name = ceph_pg_pool_name_by_id(osdc->osdmap, spec->pool_id); |
| 5359 | if (!pool_name) { |
| 5360 | rbd_warn(rbd_dev, "no pool with id %llu", spec->pool_id); |
Alex Elder | 935dc89 | 2012-11-01 10:17:15 -0500 | [diff] [blame] | 5361 | return -EIO; |
| 5362 | } |
Alex Elder | 2e9f7f1 | 2013-04-26 09:43:48 -0500 | [diff] [blame] | 5363 | pool_name = kstrdup(pool_name, GFP_KERNEL); |
| 5364 | if (!pool_name) |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 5365 | return -ENOMEM; |
| 5366 | |
| 5367 | /* Fetch the image name; tolerate failure here */ |
| 5368 | |
Alex Elder | 2e9f7f1 | 2013-04-26 09:43:48 -0500 | [diff] [blame] | 5369 | image_name = rbd_dev_image_name(rbd_dev); |
| 5370 | if (!image_name) |
Alex Elder | 06ecc6c | 2012-11-01 10:17:15 -0500 | [diff] [blame] | 5371 | rbd_warn(rbd_dev, "unable to get image name"); |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 5372 | |
Ilya Dryomov | 0407759 | 2014-07-23 17:11:20 +0400 | [diff] [blame] | 5373 | /* Fetch the snapshot name */ |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 5374 | |
Alex Elder | 2e9f7f1 | 2013-04-26 09:43:48 -0500 | [diff] [blame] | 5375 | snap_name = rbd_snap_name(rbd_dev, spec->snap_id); |
Josh Durgin | da6a6b6 | 2013-09-04 17:57:31 -0700 | [diff] [blame] | 5376 | if (IS_ERR(snap_name)) { |
| 5377 | ret = PTR_ERR(snap_name); |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 5378 | goto out_err; |
Alex Elder | 2e9f7f1 | 2013-04-26 09:43:48 -0500 | [diff] [blame] | 5379 | } |
| 5380 | |
| 5381 | spec->pool_name = pool_name; |
| 5382 | spec->image_name = image_name; |
| 5383 | spec->snap_name = snap_name; |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 5384 | |
| 5385 | return 0; |
Ilya Dryomov | 0407759 | 2014-07-23 17:11:20 +0400 | [diff] [blame] | 5386 | |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 5387 | out_err: |
Alex Elder | 2e9f7f1 | 2013-04-26 09:43:48 -0500 | [diff] [blame] | 5388 | kfree(image_name); |
| 5389 | kfree(pool_name); |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 5390 | return ret; |
| 5391 | } |
| 5392 | |
Alex Elder | cc4a38bd | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 5393 | static int rbd_dev_v2_snap_context(struct rbd_device *rbd_dev) |
Alex Elder | 35d489f | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5394 | { |
| 5395 | size_t size; |
| 5396 | int ret; |
| 5397 | void *reply_buf; |
| 5398 | void *p; |
| 5399 | void *end; |
| 5400 | u64 seq; |
| 5401 | u32 snap_count; |
| 5402 | struct ceph_snap_context *snapc; |
| 5403 | u32 i; |
| 5404 | |
| 5405 | /* |
| 5406 | * We'll need room for the seq value (maximum snapshot id), |
| 5407 | * snapshot count, and array of that many snapshot ids. |
| 5408 | * For now we have a fixed upper limit on the number we're |
| 5409 | * prepared to receive. |
| 5410 | */ |
| 5411 | size = sizeof (__le64) + sizeof (__le32) + |
| 5412 | RBD_MAX_SNAP_COUNT * sizeof (__le64); |
| 5413 | reply_buf = kzalloc(size, GFP_KERNEL); |
| 5414 | if (!reply_buf) |
| 5415 | return -ENOMEM; |
| 5416 | |
Ilya Dryomov | c41d13a | 2016-04-29 20:01:25 +0200 | [diff] [blame] | 5417 | ret = rbd_obj_method_sync(rbd_dev, rbd_dev->header_oid.name, |
Alex Elder | 4157976 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 5418 | "rbd", "get_snapcontext", NULL, 0, |
Alex Elder | e2a58ee | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 5419 | reply_buf, size); |
Alex Elder | 36be9a7 | 2013-01-19 00:30:28 -0600 | [diff] [blame] | 5420 | dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret); |
Alex Elder | 35d489f | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5421 | if (ret < 0) |
| 5422 | goto out; |
| 5423 | |
Alex Elder | 35d489f | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5424 | p = reply_buf; |
Alex Elder | 57385b5 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 5425 | end = reply_buf + ret; |
| 5426 | ret = -ERANGE; |
Alex Elder | 35d489f | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5427 | ceph_decode_64_safe(&p, end, seq, out); |
| 5428 | ceph_decode_32_safe(&p, end, snap_count, out); |
| 5429 | |
| 5430 | /* |
| 5431 | * Make sure the reported number of snapshot ids wouldn't go |
| 5432 | * beyond the end of our buffer. But before checking that, |
| 5433 | * make sure the computed size of the snapshot context we |
| 5434 | * allocate is representable in a size_t. |
| 5435 | */ |
| 5436 | if (snap_count > (SIZE_MAX - sizeof (struct ceph_snap_context)) |
| 5437 | / sizeof (u64)) { |
| 5438 | ret = -EINVAL; |
| 5439 | goto out; |
| 5440 | } |
| 5441 | if (!ceph_has_room(&p, end, snap_count * sizeof (__le64))) |
| 5442 | goto out; |
Alex Elder | 468521c | 2013-04-26 09:43:47 -0500 | [diff] [blame] | 5443 | ret = 0; |
Alex Elder | 35d489f | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5444 | |
Alex Elder | 812164f8 | 2013-04-30 00:44:32 -0500 | [diff] [blame] | 5445 | snapc = ceph_create_snap_context(snap_count, GFP_KERNEL); |
Alex Elder | 35d489f | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5446 | if (!snapc) { |
| 5447 | ret = -ENOMEM; |
| 5448 | goto out; |
| 5449 | } |
Alex Elder | 35d489f | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5450 | snapc->seq = seq; |
Alex Elder | 35d489f | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5451 | for (i = 0; i < snap_count; i++) |
| 5452 | snapc->snaps[i] = ceph_decode_64(&p); |
| 5453 | |
Alex Elder | 49ece55 | 2013-05-06 08:37:00 -0500 | [diff] [blame] | 5454 | ceph_put_snap_context(rbd_dev->header.snapc); |
Alex Elder | 35d489f | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5455 | rbd_dev->header.snapc = snapc; |
| 5456 | |
| 5457 | dout(" snap context seq = %llu, snap_count = %u\n", |
Alex Elder | 57385b5 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 5458 | (unsigned long long)seq, (unsigned int)snap_count); |
Alex Elder | 35d489f | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5459 | out: |
| 5460 | kfree(reply_buf); |
| 5461 | |
Alex Elder | 57385b5 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 5462 | return ret; |
Alex Elder | 35d489f | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5463 | } |
| 5464 | |
Alex Elder | 54cac61 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 5465 | static const char *rbd_dev_v2_snap_name(struct rbd_device *rbd_dev, |
| 5466 | u64 snap_id) |
Alex Elder | b8b1e2d | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5467 | { |
| 5468 | size_t size; |
| 5469 | void *reply_buf; |
Alex Elder | 54cac61 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 5470 | __le64 snapid; |
Alex Elder | b8b1e2d | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5471 | int ret; |
| 5472 | void *p; |
| 5473 | void *end; |
Alex Elder | b8b1e2d | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5474 | char *snap_name; |
| 5475 | |
| 5476 | size = sizeof (__le32) + RBD_MAX_SNAP_NAME_LEN; |
| 5477 | reply_buf = kmalloc(size, GFP_KERNEL); |
| 5478 | if (!reply_buf) |
| 5479 | return ERR_PTR(-ENOMEM); |
| 5480 | |
Alex Elder | 54cac61 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 5481 | snapid = cpu_to_le64(snap_id); |
Ilya Dryomov | c41d13a | 2016-04-29 20:01:25 +0200 | [diff] [blame] | 5482 | ret = rbd_obj_method_sync(rbd_dev, rbd_dev->header_oid.name, |
Alex Elder | b8b1e2d | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5483 | "rbd", "get_snapshot_name", |
Alex Elder | 54cac61 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 5484 | &snapid, sizeof (snapid), |
Alex Elder | e2a58ee | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 5485 | reply_buf, size); |
Alex Elder | 36be9a7 | 2013-01-19 00:30:28 -0600 | [diff] [blame] | 5486 | dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret); |
Alex Elder | f40eb34 | 2013-04-25 15:09:42 -0500 | [diff] [blame] | 5487 | if (ret < 0) { |
| 5488 | snap_name = ERR_PTR(ret); |
Alex Elder | b8b1e2d | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5489 | goto out; |
Alex Elder | f40eb34 | 2013-04-25 15:09:42 -0500 | [diff] [blame] | 5490 | } |
Alex Elder | b8b1e2d | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5491 | |
| 5492 | p = reply_buf; |
Alex Elder | f40eb34 | 2013-04-25 15:09:42 -0500 | [diff] [blame] | 5493 | end = reply_buf + ret; |
Alex Elder | e5c3553 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5494 | snap_name = ceph_extract_encoded_string(&p, end, NULL, GFP_KERNEL); |
Alex Elder | f40eb34 | 2013-04-25 15:09:42 -0500 | [diff] [blame] | 5495 | if (IS_ERR(snap_name)) |
Alex Elder | b8b1e2d | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5496 | goto out; |
Alex Elder | b8b1e2d | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5497 | |
Alex Elder | f40eb34 | 2013-04-25 15:09:42 -0500 | [diff] [blame] | 5498 | dout(" snap_id 0x%016llx snap_name = %s\n", |
Alex Elder | 54cac61 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 5499 | (unsigned long long)snap_id, snap_name); |
Alex Elder | b8b1e2d | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5500 | out: |
| 5501 | kfree(reply_buf); |
| 5502 | |
Alex Elder | f40eb34 | 2013-04-25 15:09:42 -0500 | [diff] [blame] | 5503 | return snap_name; |
Alex Elder | b8b1e2d | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5504 | } |
| 5505 | |
Alex Elder | 2df3fac | 2013-05-06 09:51:30 -0500 | [diff] [blame] | 5506 | static int rbd_dev_v2_header_info(struct rbd_device *rbd_dev) |
Alex Elder | 117973f | 2012-08-31 17:29:55 -0500 | [diff] [blame] | 5507 | { |
Alex Elder | 2df3fac | 2013-05-06 09:51:30 -0500 | [diff] [blame] | 5508 | bool first_time = rbd_dev->header.object_prefix == NULL; |
Alex Elder | 117973f | 2012-08-31 17:29:55 -0500 | [diff] [blame] | 5509 | int ret; |
Alex Elder | 117973f | 2012-08-31 17:29:55 -0500 | [diff] [blame] | 5510 | |
Josh Durgin | 1617e40 | 2013-06-12 14:43:10 -0700 | [diff] [blame] | 5511 | ret = rbd_dev_v2_image_size(rbd_dev); |
| 5512 | if (ret) |
Alex Elder | cfbf637 | 2013-05-31 17:40:45 -0500 | [diff] [blame] | 5513 | return ret; |
Josh Durgin | 1617e40 | 2013-06-12 14:43:10 -0700 | [diff] [blame] | 5514 | |
Alex Elder | 2df3fac | 2013-05-06 09:51:30 -0500 | [diff] [blame] | 5515 | if (first_time) { |
| 5516 | ret = rbd_dev_v2_header_onetime(rbd_dev); |
| 5517 | if (ret) |
Alex Elder | cfbf637 | 2013-05-31 17:40:45 -0500 | [diff] [blame] | 5518 | return ret; |
Alex Elder | 2df3fac | 2013-05-06 09:51:30 -0500 | [diff] [blame] | 5519 | } |
| 5520 | |
Alex Elder | cc4a38bd | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 5521 | ret = rbd_dev_v2_snap_context(rbd_dev); |
Ilya Dryomov | d194cd1 | 2015-08-31 18:22:10 +0300 | [diff] [blame] | 5522 | if (ret && first_time) { |
| 5523 | kfree(rbd_dev->header.object_prefix); |
| 5524 | rbd_dev->header.object_prefix = NULL; |
| 5525 | } |
Alex Elder | 117973f | 2012-08-31 17:29:55 -0500 | [diff] [blame] | 5526 | |
| 5527 | return ret; |
| 5528 | } |
| 5529 | |
Ilya Dryomov | a720ae0 | 2014-07-23 17:11:19 +0400 | [diff] [blame] | 5530 | static int rbd_dev_header_info(struct rbd_device *rbd_dev) |
| 5531 | { |
| 5532 | rbd_assert(rbd_image_format_valid(rbd_dev->image_format)); |
| 5533 | |
| 5534 | if (rbd_dev->image_format == 1) |
| 5535 | return rbd_dev_v1_header_info(rbd_dev); |
| 5536 | |
| 5537 | return rbd_dev_v2_header_info(rbd_dev); |
| 5538 | } |
| 5539 | |
Alex Elder | 1ddbe94 | 2012-01-29 13:57:44 -0600 | [diff] [blame] | 5540 | /* |
Alex Elder | e28fff26 | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 5541 | * Skips over white space at *buf, and updates *buf to point to the |
| 5542 | * first found non-space character (if any). Returns the length of |
Alex Elder | 593a9e7 | 2012-02-07 12:03:37 -0600 | [diff] [blame] | 5543 | * the token (string of non-white space characters) found. Note |
| 5544 | * that *buf must be terminated with '\0'. |
Alex Elder | e28fff26 | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 5545 | */ |
| 5546 | static inline size_t next_token(const char **buf) |
| 5547 | { |
| 5548 | /* |
| 5549 | * These are the characters that produce nonzero for |
| 5550 | * isspace() in the "C" and "POSIX" locales. |
| 5551 | */ |
| 5552 | const char *spaces = " \f\n\r\t\v"; |
| 5553 | |
| 5554 | *buf += strspn(*buf, spaces); /* Find start of token */ |
| 5555 | |
| 5556 | return strcspn(*buf, spaces); /* Return token length */ |
| 5557 | } |
| 5558 | |
| 5559 | /* |
Alex Elder | ea3352f | 2012-07-09 21:04:23 -0500 | [diff] [blame] | 5560 | * Finds the next token in *buf, dynamically allocates a buffer big |
| 5561 | * enough to hold a copy of it, and copies the token into the new |
| 5562 | * buffer. The copy is guaranteed to be terminated with '\0'. Note |
| 5563 | * that a duplicate buffer is created even for a zero-length token. |
| 5564 | * |
| 5565 | * Returns a pointer to the newly-allocated duplicate, or a null |
| 5566 | * pointer if memory for the duplicate was not available. If |
| 5567 | * the lenp argument is a non-null pointer, the length of the token |
| 5568 | * (not including the '\0') is returned in *lenp. |
| 5569 | * |
| 5570 | * If successful, the *buf pointer will be updated to point beyond |
| 5571 | * the end of the found token. |
| 5572 | * |
| 5573 | * Note: uses GFP_KERNEL for allocation. |
| 5574 | */ |
| 5575 | static inline char *dup_token(const char **buf, size_t *lenp) |
| 5576 | { |
| 5577 | char *dup; |
| 5578 | size_t len; |
| 5579 | |
| 5580 | len = next_token(buf); |
Alex Elder | 4caf35f | 2012-11-01 08:39:27 -0500 | [diff] [blame] | 5581 | dup = kmemdup(*buf, len + 1, GFP_KERNEL); |
Alex Elder | ea3352f | 2012-07-09 21:04:23 -0500 | [diff] [blame] | 5582 | if (!dup) |
| 5583 | return NULL; |
Alex Elder | ea3352f | 2012-07-09 21:04:23 -0500 | [diff] [blame] | 5584 | *(dup + len) = '\0'; |
| 5585 | *buf += len; |
| 5586 | |
| 5587 | if (lenp) |
| 5588 | *lenp = len; |
| 5589 | |
| 5590 | return dup; |
| 5591 | } |
| 5592 | |
| 5593 | /* |
Alex Elder | 859c31d | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5594 | * Parse the options provided for an "rbd add" (i.e., rbd image |
| 5595 | * mapping) request. These arrive via a write to /sys/bus/rbd/add, |
| 5596 | * and the data written is passed here via a NUL-terminated buffer. |
| 5597 | * Returns 0 if successful or an error code otherwise. |
Alex Elder | d22f76e | 2012-07-12 10:46:35 -0500 | [diff] [blame] | 5598 | * |
Alex Elder | 859c31d | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5599 | * The information extracted from these options is recorded in |
| 5600 | * the other parameters which return dynamically-allocated |
| 5601 | * structures: |
| 5602 | * ceph_opts |
| 5603 | * The address of a pointer that will refer to a ceph options |
| 5604 | * structure. Caller must release the returned pointer using |
| 5605 | * ceph_destroy_options() when it is no longer needed. |
| 5606 | * rbd_opts |
| 5607 | * Address of an rbd options pointer. Fully initialized by |
| 5608 | * this function; caller must release with kfree(). |
| 5609 | * spec |
| 5610 | * Address of an rbd image specification pointer. Fully |
| 5611 | * initialized by this function based on parsed options. |
| 5612 | * Caller must release with rbd_spec_put(). |
| 5613 | * |
| 5614 | * The options passed take this form: |
| 5615 | * <mon_addrs> <options> <pool_name> <image_name> [<snap_id>] |
| 5616 | * where: |
| 5617 | * <mon_addrs> |
| 5618 | * A comma-separated list of one or more monitor addresses. |
| 5619 | * A monitor address is an ip address, optionally followed |
| 5620 | * by a port number (separated by a colon). |
| 5621 | * I.e.: ip1[:port1][,ip2[:port2]...] |
| 5622 | * <options> |
| 5623 | * A comma-separated list of ceph and/or rbd options. |
| 5624 | * <pool_name> |
| 5625 | * The name of the rados pool containing the rbd image. |
| 5626 | * <image_name> |
| 5627 | * The name of the image in that pool to map. |
| 5628 | * <snap_id> |
| 5629 | * An optional snapshot id. If provided, the mapping will |
| 5630 | * present data from the image at the time that snapshot was |
| 5631 | * created. The image head is used if no snapshot id is |
| 5632 | * provided. Snapshot mappings are always read-only. |
Alex Elder | a725f65e | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 5633 | */ |
Alex Elder | 859c31d | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5634 | static int rbd_add_parse_args(const char *buf, |
Alex Elder | dc79b11 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5635 | struct ceph_options **ceph_opts, |
Alex Elder | 859c31d | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5636 | struct rbd_options **opts, |
| 5637 | struct rbd_spec **rbd_spec) |
Alex Elder | a725f65e | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 5638 | { |
Alex Elder | d22f76e | 2012-07-12 10:46:35 -0500 | [diff] [blame] | 5639 | size_t len; |
Alex Elder | 859c31d | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5640 | char *options; |
Alex Elder | 0ddebc0 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5641 | const char *mon_addrs; |
Alex Elder | ecb4dc22 | 2013-04-26 09:43:47 -0500 | [diff] [blame] | 5642 | char *snap_name; |
Alex Elder | 0ddebc0 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5643 | size_t mon_addrs_size; |
Alex Elder | 859c31d | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5644 | struct rbd_spec *spec = NULL; |
Alex Elder | 4e9afeb | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5645 | struct rbd_options *rbd_opts = NULL; |
Alex Elder | 859c31d | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5646 | struct ceph_options *copts; |
Alex Elder | dc79b11 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5647 | int ret; |
Alex Elder | e28fff26 | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 5648 | |
| 5649 | /* The first four tokens are required */ |
| 5650 | |
Alex Elder | 7ef3214 | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 5651 | len = next_token(&buf); |
Alex Elder | 4fb5d671 | 2012-11-01 10:17:15 -0500 | [diff] [blame] | 5652 | if (!len) { |
| 5653 | rbd_warn(NULL, "no monitor address(es) provided"); |
| 5654 | return -EINVAL; |
| 5655 | } |
Alex Elder | 0ddebc0 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5656 | mon_addrs = buf; |
Alex Elder | f28e565 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5657 | mon_addrs_size = len + 1; |
Alex Elder | 7ef3214 | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 5658 | buf += len; |
Alex Elder | a725f65e | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 5659 | |
Alex Elder | dc79b11 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5660 | ret = -EINVAL; |
Alex Elder | f28e565 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5661 | options = dup_token(&buf, NULL); |
| 5662 | if (!options) |
Alex Elder | dc79b11 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5663 | return -ENOMEM; |
Alex Elder | 4fb5d671 | 2012-11-01 10:17:15 -0500 | [diff] [blame] | 5664 | if (!*options) { |
| 5665 | rbd_warn(NULL, "no options provided"); |
| 5666 | goto out_err; |
| 5667 | } |
Alex Elder | a725f65e | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 5668 | |
Alex Elder | 859c31d | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5669 | spec = rbd_spec_alloc(); |
| 5670 | if (!spec) |
Alex Elder | f28e565 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5671 | goto out_mem; |
Alex Elder | 859c31d | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5672 | |
| 5673 | spec->pool_name = dup_token(&buf, NULL); |
| 5674 | if (!spec->pool_name) |
| 5675 | goto out_mem; |
Alex Elder | 4fb5d671 | 2012-11-01 10:17:15 -0500 | [diff] [blame] | 5676 | if (!*spec->pool_name) { |
| 5677 | rbd_warn(NULL, "no pool name provided"); |
| 5678 | goto out_err; |
| 5679 | } |
Alex Elder | e28fff26 | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 5680 | |
Alex Elder | 69e7a02 | 2012-11-01 08:39:26 -0500 | [diff] [blame] | 5681 | spec->image_name = dup_token(&buf, NULL); |
Alex Elder | 859c31d | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5682 | if (!spec->image_name) |
Alex Elder | f28e565 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5683 | goto out_mem; |
Alex Elder | 4fb5d671 | 2012-11-01 10:17:15 -0500 | [diff] [blame] | 5684 | if (!*spec->image_name) { |
| 5685 | rbd_warn(NULL, "no image name provided"); |
| 5686 | goto out_err; |
| 5687 | } |
Alex Elder | e28fff26 | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 5688 | |
Alex Elder | f28e565 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5689 | /* |
| 5690 | * Snapshot name is optional; default is to use "-" |
| 5691 | * (indicating the head/no snapshot). |
| 5692 | */ |
Alex Elder | 3feeb894 | 2012-08-31 17:29:52 -0500 | [diff] [blame] | 5693 | len = next_token(&buf); |
Alex Elder | 820a5f3 | 2012-07-09 21:04:24 -0500 | [diff] [blame] | 5694 | if (!len) { |
Alex Elder | 3feeb894 | 2012-08-31 17:29:52 -0500 | [diff] [blame] | 5695 | buf = RBD_SNAP_HEAD_NAME; /* No snapshot supplied */ |
| 5696 | len = sizeof (RBD_SNAP_HEAD_NAME) - 1; |
Alex Elder | f28e565 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5697 | } else if (len > RBD_MAX_SNAP_NAME_LEN) { |
Alex Elder | dc79b11 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5698 | ret = -ENAMETOOLONG; |
Alex Elder | f28e565 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5699 | goto out_err; |
Alex Elder | 849b426 | 2012-07-09 21:04:24 -0500 | [diff] [blame] | 5700 | } |
Alex Elder | ecb4dc22 | 2013-04-26 09:43:47 -0500 | [diff] [blame] | 5701 | snap_name = kmemdup(buf, len + 1, GFP_KERNEL); |
| 5702 | if (!snap_name) |
Alex Elder | f28e565 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5703 | goto out_mem; |
Alex Elder | ecb4dc22 | 2013-04-26 09:43:47 -0500 | [diff] [blame] | 5704 | *(snap_name + len) = '\0'; |
| 5705 | spec->snap_name = snap_name; |
Alex Elder | e5c3553 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5706 | |
Alex Elder | 0ddebc0 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5707 | /* Initialize all rbd options to the defaults */ |
Alex Elder | e28fff26 | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 5708 | |
Alex Elder | 4e9afeb | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5709 | rbd_opts = kzalloc(sizeof (*rbd_opts), GFP_KERNEL); |
| 5710 | if (!rbd_opts) |
| 5711 | goto out_mem; |
| 5712 | |
| 5713 | rbd_opts->read_only = RBD_READ_ONLY_DEFAULT; |
Ilya Dryomov | b558418 | 2015-06-23 16:21:19 +0300 | [diff] [blame] | 5714 | rbd_opts->queue_depth = RBD_QUEUE_DEPTH_DEFAULT; |
Alex Elder | d22f76e | 2012-07-12 10:46:35 -0500 | [diff] [blame] | 5715 | |
Alex Elder | 859c31d | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5716 | copts = ceph_parse_options(options, mon_addrs, |
Alex Elder | 0ddebc0 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5717 | mon_addrs + mon_addrs_size - 1, |
Alex Elder | 4e9afeb | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5718 | parse_rbd_opts_token, rbd_opts); |
Alex Elder | 859c31d | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5719 | if (IS_ERR(copts)) { |
| 5720 | ret = PTR_ERR(copts); |
Alex Elder | dc79b11 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5721 | goto out_err; |
| 5722 | } |
Alex Elder | 859c31d | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5723 | kfree(options); |
| 5724 | |
| 5725 | *ceph_opts = copts; |
Alex Elder | 4e9afeb | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5726 | *opts = rbd_opts; |
Alex Elder | 859c31d | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5727 | *rbd_spec = spec; |
Alex Elder | 0ddebc0 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5728 | |
Alex Elder | dc79b11 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5729 | return 0; |
Alex Elder | f28e565 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5730 | out_mem: |
Alex Elder | dc79b11 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5731 | ret = -ENOMEM; |
Alex Elder | d22f76e | 2012-07-12 10:46:35 -0500 | [diff] [blame] | 5732 | out_err: |
Alex Elder | 859c31d | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5733 | kfree(rbd_opts); |
| 5734 | rbd_spec_put(spec); |
Alex Elder | f28e565 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5735 | kfree(options); |
Alex Elder | d22f76e | 2012-07-12 10:46:35 -0500 | [diff] [blame] | 5736 | |
Alex Elder | dc79b11 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5737 | return ret; |
Alex Elder | a725f65e | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 5738 | } |
| 5739 | |
Alex Elder | 589d30e | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 5740 | /* |
Ilya Dryomov | 30ba1f0 | 2014-05-13 11:19:27 +0400 | [diff] [blame] | 5741 | * Return pool id (>= 0) or a negative error code. |
| 5742 | */ |
| 5743 | static int rbd_add_get_pool_id(struct rbd_client *rbdc, const char *pool_name) |
| 5744 | { |
Ilya Dryomov | a319bf5 | 2015-05-15 12:02:17 +0300 | [diff] [blame] | 5745 | struct ceph_options *opts = rbdc->client->options; |
Ilya Dryomov | 30ba1f0 | 2014-05-13 11:19:27 +0400 | [diff] [blame] | 5746 | u64 newest_epoch; |
Ilya Dryomov | 30ba1f0 | 2014-05-13 11:19:27 +0400 | [diff] [blame] | 5747 | int tries = 0; |
| 5748 | int ret; |
| 5749 | |
| 5750 | again: |
| 5751 | ret = ceph_pg_poolid_by_name(rbdc->client->osdc.osdmap, pool_name); |
| 5752 | if (ret == -ENOENT && tries++ < 1) { |
Ilya Dryomov | d0b1970 | 2016-04-28 16:07:27 +0200 | [diff] [blame] | 5753 | ret = ceph_monc_get_version(&rbdc->client->monc, "osdmap", |
| 5754 | &newest_epoch); |
Ilya Dryomov | 30ba1f0 | 2014-05-13 11:19:27 +0400 | [diff] [blame] | 5755 | if (ret < 0) |
| 5756 | return ret; |
| 5757 | |
| 5758 | if (rbdc->client->osdc.osdmap->epoch < newest_epoch) { |
Ilya Dryomov | 7cca78c | 2016-04-28 16:07:28 +0200 | [diff] [blame] | 5759 | ceph_osdc_maybe_request_map(&rbdc->client->osdc); |
Ilya Dryomov | 30ba1f0 | 2014-05-13 11:19:27 +0400 | [diff] [blame] | 5760 | (void) ceph_monc_wait_osdmap(&rbdc->client->monc, |
Ilya Dryomov | a319bf5 | 2015-05-15 12:02:17 +0300 | [diff] [blame] | 5761 | newest_epoch, |
| 5762 | opts->mount_timeout); |
Ilya Dryomov | 30ba1f0 | 2014-05-13 11:19:27 +0400 | [diff] [blame] | 5763 | goto again; |
| 5764 | } else { |
| 5765 | /* the osdmap we have is new enough */ |
| 5766 | return -ENOENT; |
| 5767 | } |
| 5768 | } |
| 5769 | |
| 5770 | return ret; |
| 5771 | } |
| 5772 | |
| 5773 | /* |
Alex Elder | 589d30e | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 5774 | * An rbd format 2 image has a unique identifier, distinct from the |
| 5775 | * name given to it by the user. Internally, that identifier is |
| 5776 | * what's used to specify the names of objects related to the image. |
| 5777 | * |
| 5778 | * A special "rbd id" object is used to map an rbd image name to its |
| 5779 | * id. If that object doesn't exist, then there is no v2 rbd image |
| 5780 | * with the supplied name. |
| 5781 | * |
| 5782 | * This function will record the given rbd_dev's image_id field if |
| 5783 | * it can be determined, and in that case will return 0. If any |
| 5784 | * errors occur a negative errno will be returned and the rbd_dev's |
| 5785 | * image_id field will be unchanged (and should be NULL). |
| 5786 | */ |
| 5787 | static int rbd_dev_image_id(struct rbd_device *rbd_dev) |
| 5788 | { |
| 5789 | int ret; |
| 5790 | size_t size; |
| 5791 | char *object_name; |
| 5792 | void *response; |
Alex Elder | c0fba36 | 2013-04-25 23:15:08 -0500 | [diff] [blame] | 5793 | char *image_id; |
Alex Elder | 2f82ee5 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 5794 | |
Alex Elder | 589d30e | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 5795 | /* |
Alex Elder | 2c0d0a1 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 5796 | * When probing a parent image, the image id is already |
| 5797 | * known (and the image name likely is not). There's no |
Alex Elder | c0fba36 | 2013-04-25 23:15:08 -0500 | [diff] [blame] | 5798 | * need to fetch the image id again in this case. We |
| 5799 | * do still need to set the image format though. |
Alex Elder | 2c0d0a1 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 5800 | */ |
Alex Elder | c0fba36 | 2013-04-25 23:15:08 -0500 | [diff] [blame] | 5801 | if (rbd_dev->spec->image_id) { |
| 5802 | rbd_dev->image_format = *rbd_dev->spec->image_id ? 2 : 1; |
| 5803 | |
Alex Elder | 2c0d0a1 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 5804 | return 0; |
Alex Elder | c0fba36 | 2013-04-25 23:15:08 -0500 | [diff] [blame] | 5805 | } |
Alex Elder | 2c0d0a1 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 5806 | |
| 5807 | /* |
Alex Elder | 589d30e | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 5808 | * First, see if the format 2 image id file exists, and if |
| 5809 | * so, get the image's persistent id from it. |
| 5810 | */ |
Alex Elder | 69e7a02 | 2012-11-01 08:39:26 -0500 | [diff] [blame] | 5811 | size = sizeof (RBD_ID_PREFIX) + strlen(rbd_dev->spec->image_name); |
Alex Elder | 589d30e | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 5812 | object_name = kmalloc(size, GFP_NOIO); |
| 5813 | if (!object_name) |
| 5814 | return -ENOMEM; |
Alex Elder | 0d7dbfc | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5815 | sprintf(object_name, "%s%s", RBD_ID_PREFIX, rbd_dev->spec->image_name); |
Alex Elder | 589d30e | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 5816 | dout("rbd id object name is %s\n", object_name); |
| 5817 | |
| 5818 | /* Response will be an encoded string, which includes a length */ |
| 5819 | |
| 5820 | size = sizeof (__le32) + RBD_IMAGE_ID_LEN_MAX; |
| 5821 | response = kzalloc(size, GFP_NOIO); |
| 5822 | if (!response) { |
| 5823 | ret = -ENOMEM; |
| 5824 | goto out; |
| 5825 | } |
| 5826 | |
Alex Elder | c0fba36 | 2013-04-25 23:15:08 -0500 | [diff] [blame] | 5827 | /* If it doesn't exist we'll assume it's a format 1 image */ |
| 5828 | |
Alex Elder | 36be9a7 | 2013-01-19 00:30:28 -0600 | [diff] [blame] | 5829 | ret = rbd_obj_method_sync(rbd_dev, object_name, |
Alex Elder | 4157976 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 5830 | "rbd", "get_id", NULL, 0, |
Alex Elder | e2a58ee | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 5831 | response, RBD_IMAGE_ID_LEN_MAX); |
Alex Elder | 36be9a7 | 2013-01-19 00:30:28 -0600 | [diff] [blame] | 5832 | dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret); |
Alex Elder | c0fba36 | 2013-04-25 23:15:08 -0500 | [diff] [blame] | 5833 | if (ret == -ENOENT) { |
| 5834 | image_id = kstrdup("", GFP_KERNEL); |
| 5835 | ret = image_id ? 0 : -ENOMEM; |
| 5836 | if (!ret) |
| 5837 | rbd_dev->image_format = 1; |
Ilya Dryomov | 7dd440c | 2014-09-11 18:49:18 +0400 | [diff] [blame] | 5838 | } else if (ret >= 0) { |
Alex Elder | c0fba36 | 2013-04-25 23:15:08 -0500 | [diff] [blame] | 5839 | void *p = response; |
Alex Elder | 589d30e | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 5840 | |
Alex Elder | c0fba36 | 2013-04-25 23:15:08 -0500 | [diff] [blame] | 5841 | image_id = ceph_extract_encoded_string(&p, p + ret, |
Alex Elder | 979ed48 | 2012-11-01 08:39:26 -0500 | [diff] [blame] | 5842 | NULL, GFP_NOIO); |
Duan Jiong | 461f758 | 2014-04-11 16:38:12 +0800 | [diff] [blame] | 5843 | ret = PTR_ERR_OR_ZERO(image_id); |
Alex Elder | c0fba36 | 2013-04-25 23:15:08 -0500 | [diff] [blame] | 5844 | if (!ret) |
| 5845 | rbd_dev->image_format = 2; |
Alex Elder | c0fba36 | 2013-04-25 23:15:08 -0500 | [diff] [blame] | 5846 | } |
| 5847 | |
| 5848 | if (!ret) { |
| 5849 | rbd_dev->spec->image_id = image_id; |
| 5850 | dout("image_id is %s\n", image_id); |
Alex Elder | 589d30e | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 5851 | } |
| 5852 | out: |
| 5853 | kfree(response); |
| 5854 | kfree(object_name); |
| 5855 | |
| 5856 | return ret; |
| 5857 | } |
| 5858 | |
Alex Elder | 3abef3b | 2013-05-13 20:35:37 -0500 | [diff] [blame] | 5859 | /* |
| 5860 | * Undo whatever state changes are made by v1 or v2 header info |
| 5861 | * call. |
| 5862 | */ |
Alex Elder | 6fd48b3 | 2013-04-28 23:32:34 -0500 | [diff] [blame] | 5863 | static void rbd_dev_unprobe(struct rbd_device *rbd_dev) |
| 5864 | { |
| 5865 | struct rbd_image_header *header; |
| 5866 | |
Ilya Dryomov | e69b8d4 | 2015-01-19 12:06:14 +0300 | [diff] [blame] | 5867 | rbd_dev_parent_put(rbd_dev); |
Alex Elder | 6fd48b3 | 2013-04-28 23:32:34 -0500 | [diff] [blame] | 5868 | |
| 5869 | /* Free dynamic fields from the header, then zero it out */ |
| 5870 | |
| 5871 | header = &rbd_dev->header; |
Alex Elder | 812164f8 | 2013-04-30 00:44:32 -0500 | [diff] [blame] | 5872 | ceph_put_snap_context(header->snapc); |
Alex Elder | 6fd48b3 | 2013-04-28 23:32:34 -0500 | [diff] [blame] | 5873 | kfree(header->snap_sizes); |
| 5874 | kfree(header->snap_names); |
| 5875 | kfree(header->object_prefix); |
| 5876 | memset(header, 0, sizeof (*header)); |
| 5877 | } |
| 5878 | |
Alex Elder | 2df3fac | 2013-05-06 09:51:30 -0500 | [diff] [blame] | 5879 | static int rbd_dev_v2_header_onetime(struct rbd_device *rbd_dev) |
Alex Elder | a30b71b | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 5880 | { |
| 5881 | int ret; |
Alex Elder | a30b71b | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 5882 | |
Alex Elder | 1e13019 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5883 | ret = rbd_dev_v2_object_prefix(rbd_dev); |
Alex Elder | 57385b5 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 5884 | if (ret) |
Alex Elder | 1e13019 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5885 | goto out_err; |
Alex Elder | b1b5402 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5886 | |
Alex Elder | 2df3fac | 2013-05-06 09:51:30 -0500 | [diff] [blame] | 5887 | /* |
| 5888 | * Get the and check features for the image. Currently the |
| 5889 | * features are assumed to never change. |
| 5890 | */ |
Alex Elder | b1b5402 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5891 | ret = rbd_dev_v2_features(rbd_dev); |
Alex Elder | 57385b5 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 5892 | if (ret) |
Alex Elder | b1b5402 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5893 | goto out_err; |
Alex Elder | 35d489f | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5894 | |
Alex Elder | cc070d5 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 5895 | /* If the image supports fancy striping, get its parameters */ |
| 5896 | |
| 5897 | if (rbd_dev->header.features & RBD_FEATURE_STRIPINGV2) { |
| 5898 | ret = rbd_dev_v2_striping_info(rbd_dev); |
| 5899 | if (ret < 0) |
| 5900 | goto out_err; |
| 5901 | } |
Alex Elder | 2df3fac | 2013-05-06 09:51:30 -0500 | [diff] [blame] | 5902 | /* No support for crypto and compression type format 2 images */ |
Alex Elder | a30b71b | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 5903 | |
Alex Elder | 3515297 | 2012-08-31 17:29:55 -0500 | [diff] [blame] | 5904 | return 0; |
Alex Elder | 9d475de | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5905 | out_err: |
Alex Elder | 642a253 | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 5906 | rbd_dev->header.features = 0; |
Alex Elder | 1e13019 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5907 | kfree(rbd_dev->header.object_prefix); |
| 5908 | rbd_dev->header.object_prefix = NULL; |
Alex Elder | 9d475de | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5909 | |
| 5910 | return ret; |
Alex Elder | a30b71b | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 5911 | } |
| 5912 | |
Ilya Dryomov | 6d69bb53 | 2015-10-11 19:38:00 +0200 | [diff] [blame] | 5913 | /* |
| 5914 | * @depth is rbd_dev_image_probe() -> rbd_dev_probe_parent() -> |
| 5915 | * rbd_dev_image_probe() recursion depth, which means it's also the |
| 5916 | * length of the already discovered part of the parent chain. |
| 5917 | */ |
| 5918 | static int rbd_dev_probe_parent(struct rbd_device *rbd_dev, int depth) |
Alex Elder | 83a0626 | 2012-10-30 15:47:17 -0500 | [diff] [blame] | 5919 | { |
Alex Elder | 2f82ee5 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 5920 | struct rbd_device *parent = NULL; |
Alex Elder | 124afba | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 5921 | int ret; |
| 5922 | |
| 5923 | if (!rbd_dev->parent_spec) |
| 5924 | return 0; |
Alex Elder | 124afba | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 5925 | |
Ilya Dryomov | 6d69bb53 | 2015-10-11 19:38:00 +0200 | [diff] [blame] | 5926 | if (++depth > RBD_MAX_PARENT_CHAIN_LEN) { |
| 5927 | pr_info("parent chain is too long (%d)\n", depth); |
| 5928 | ret = -EINVAL; |
| 5929 | goto out_err; |
| 5930 | } |
| 5931 | |
Ilya Dryomov | 1643dfa | 2016-08-12 15:45:52 +0200 | [diff] [blame] | 5932 | parent = __rbd_dev_create(rbd_dev->rbd_client, rbd_dev->parent_spec); |
Ilya Dryomov | 1f2c665 | 2015-10-11 19:38:00 +0200 | [diff] [blame] | 5933 | if (!parent) { |
| 5934 | ret = -ENOMEM; |
Alex Elder | 124afba | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 5935 | goto out_err; |
Ilya Dryomov | 1f2c665 | 2015-10-11 19:38:00 +0200 | [diff] [blame] | 5936 | } |
| 5937 | |
| 5938 | /* |
| 5939 | * Images related by parent/child relationships always share |
| 5940 | * rbd_client and spec/parent_spec, so bump their refcounts. |
| 5941 | */ |
| 5942 | __rbd_get_client(rbd_dev->rbd_client); |
| 5943 | rbd_spec_get(rbd_dev->parent_spec); |
Alex Elder | 124afba | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 5944 | |
Ilya Dryomov | 6d69bb53 | 2015-10-11 19:38:00 +0200 | [diff] [blame] | 5945 | ret = rbd_dev_image_probe(parent, depth); |
Alex Elder | 124afba | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 5946 | if (ret < 0) |
| 5947 | goto out_err; |
Ilya Dryomov | 1f2c665 | 2015-10-11 19:38:00 +0200 | [diff] [blame] | 5948 | |
Alex Elder | 124afba | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 5949 | rbd_dev->parent = parent; |
Alex Elder | a2acd00 | 2013-05-08 22:50:04 -0500 | [diff] [blame] | 5950 | atomic_set(&rbd_dev->parent_ref, 1); |
Alex Elder | 124afba | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 5951 | return 0; |
Alex Elder | 124afba | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 5952 | |
Ilya Dryomov | 1f2c665 | 2015-10-11 19:38:00 +0200 | [diff] [blame] | 5953 | out_err: |
| 5954 | rbd_dev_unparent(rbd_dev); |
Markus Elfring | 1761b22 | 2015-11-23 20:16:45 +0100 | [diff] [blame] | 5955 | rbd_dev_destroy(parent); |
Alex Elder | 124afba | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 5956 | return ret; |
| 5957 | } |
| 5958 | |
Ilya Dryomov | 811c668 | 2016-04-15 16:22:16 +0200 | [diff] [blame] | 5959 | /* |
| 5960 | * rbd_dev->header_rwsem must be locked for write and will be unlocked |
| 5961 | * upon return. |
| 5962 | */ |
Alex Elder | 200a6a8 | 2013-04-28 23:32:34 -0500 | [diff] [blame] | 5963 | static int rbd_dev_device_setup(struct rbd_device *rbd_dev) |
Alex Elder | 124afba | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 5964 | { |
Alex Elder | 83a0626 | 2012-10-30 15:47:17 -0500 | [diff] [blame] | 5965 | int ret; |
Alex Elder | 83a0626 | 2012-10-30 15:47:17 -0500 | [diff] [blame] | 5966 | |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 5967 | /* Record our major and minor device numbers. */ |
Alex Elder | 83a0626 | 2012-10-30 15:47:17 -0500 | [diff] [blame] | 5968 | |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 5969 | if (!single_major) { |
| 5970 | ret = register_blkdev(0, rbd_dev->name); |
| 5971 | if (ret < 0) |
Ilya Dryomov | 1643dfa | 2016-08-12 15:45:52 +0200 | [diff] [blame] | 5972 | goto err_out_unlock; |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 5973 | |
| 5974 | rbd_dev->major = ret; |
| 5975 | rbd_dev->minor = 0; |
| 5976 | } else { |
| 5977 | rbd_dev->major = rbd_major; |
| 5978 | rbd_dev->minor = rbd_dev_id_to_minor(rbd_dev->dev_id); |
| 5979 | } |
Alex Elder | 83a0626 | 2012-10-30 15:47:17 -0500 | [diff] [blame] | 5980 | |
| 5981 | /* Set up the blkdev mapping. */ |
| 5982 | |
| 5983 | ret = rbd_init_disk(rbd_dev); |
| 5984 | if (ret) |
| 5985 | goto err_out_blkdev; |
| 5986 | |
Alex Elder | f35a4de | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 5987 | ret = rbd_dev_mapping_set(rbd_dev); |
Alex Elder | 83a0626 | 2012-10-30 15:47:17 -0500 | [diff] [blame] | 5988 | if (ret) |
| 5989 | goto err_out_disk; |
Ilya Dryomov | bc1ecc6 | 2014-08-04 18:04:39 +0400 | [diff] [blame] | 5990 | |
Alex Elder | f35a4de | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 5991 | set_capacity(rbd_dev->disk, rbd_dev->mapping.size / SECTOR_SIZE); |
Josh Durgin | 22001f6 | 2013-09-30 20:10:04 -0700 | [diff] [blame] | 5992 | set_disk_ro(rbd_dev->disk, rbd_dev->mapping.read_only); |
Alex Elder | f35a4de | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 5993 | |
Ilya Dryomov | dd5ac32 | 2015-10-16 17:09:24 +0200 | [diff] [blame] | 5994 | dev_set_name(&rbd_dev->dev, "%d", rbd_dev->dev_id); |
| 5995 | ret = device_add(&rbd_dev->dev); |
Alex Elder | f35a4de | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 5996 | if (ret) |
Ilya Dryomov | f5ee37b | 2014-10-09 17:06:01 +0400 | [diff] [blame] | 5997 | goto err_out_mapping; |
Alex Elder | 83a0626 | 2012-10-30 15:47:17 -0500 | [diff] [blame] | 5998 | |
Alex Elder | 83a0626 | 2012-10-30 15:47:17 -0500 | [diff] [blame] | 5999 | /* Everything's ready. Announce the disk to the world. */ |
| 6000 | |
Alex Elder | 129b79d | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 6001 | set_bit(RBD_DEV_FLAG_EXISTS, &rbd_dev->flags); |
Ilya Dryomov | 811c668 | 2016-04-15 16:22:16 +0200 | [diff] [blame] | 6002 | up_write(&rbd_dev->header_rwsem); |
Alex Elder | 83a0626 | 2012-10-30 15:47:17 -0500 | [diff] [blame] | 6003 | |
Ilya Dryomov | 1643dfa | 2016-08-12 15:45:52 +0200 | [diff] [blame] | 6004 | spin_lock(&rbd_dev_list_lock); |
| 6005 | list_add_tail(&rbd_dev->node, &rbd_dev_list); |
| 6006 | spin_unlock(&rbd_dev_list_lock); |
| 6007 | |
Ilya Dryomov | 811c668 | 2016-04-15 16:22:16 +0200 | [diff] [blame] | 6008 | add_disk(rbd_dev->disk); |
Ilya Dryomov | ca7909e | 2016-08-18 18:38:41 +0200 | [diff] [blame^] | 6009 | pr_info("%s: capacity %llu features 0x%llx\n", rbd_dev->disk->disk_name, |
| 6010 | (unsigned long long)get_capacity(rbd_dev->disk) << SECTOR_SHIFT, |
| 6011 | rbd_dev->header.features); |
Alex Elder | 83a0626 | 2012-10-30 15:47:17 -0500 | [diff] [blame] | 6012 | |
| 6013 | return ret; |
Alex Elder | 2f82ee5 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 6014 | |
Alex Elder | f35a4de | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 6015 | err_out_mapping: |
| 6016 | rbd_dev_mapping_clear(rbd_dev); |
Alex Elder | 83a0626 | 2012-10-30 15:47:17 -0500 | [diff] [blame] | 6017 | err_out_disk: |
| 6018 | rbd_free_disk(rbd_dev); |
| 6019 | err_out_blkdev: |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 6020 | if (!single_major) |
| 6021 | unregister_blkdev(rbd_dev->major, rbd_dev->name); |
Ilya Dryomov | 811c668 | 2016-04-15 16:22:16 +0200 | [diff] [blame] | 6022 | err_out_unlock: |
| 6023 | up_write(&rbd_dev->header_rwsem); |
Alex Elder | 83a0626 | 2012-10-30 15:47:17 -0500 | [diff] [blame] | 6024 | return ret; |
| 6025 | } |
| 6026 | |
Alex Elder | 332bb12 | 2013-04-27 09:59:30 -0500 | [diff] [blame] | 6027 | static int rbd_dev_header_name(struct rbd_device *rbd_dev) |
| 6028 | { |
| 6029 | struct rbd_spec *spec = rbd_dev->spec; |
Ilya Dryomov | c41d13a | 2016-04-29 20:01:25 +0200 | [diff] [blame] | 6030 | int ret; |
Alex Elder | 332bb12 | 2013-04-27 09:59:30 -0500 | [diff] [blame] | 6031 | |
| 6032 | /* Record the header object name for this rbd image. */ |
| 6033 | |
| 6034 | rbd_assert(rbd_image_format_valid(rbd_dev->image_format)); |
| 6035 | |
Yan, Zheng | 7627151 | 2016-02-03 21:24:49 +0800 | [diff] [blame] | 6036 | rbd_dev->header_oloc.pool = rbd_dev->layout.pool_id; |
Alex Elder | 332bb12 | 2013-04-27 09:59:30 -0500 | [diff] [blame] | 6037 | if (rbd_dev->image_format == 1) |
Ilya Dryomov | c41d13a | 2016-04-29 20:01:25 +0200 | [diff] [blame] | 6038 | ret = ceph_oid_aprintf(&rbd_dev->header_oid, GFP_KERNEL, "%s%s", |
| 6039 | spec->image_name, RBD_SUFFIX); |
Alex Elder | 332bb12 | 2013-04-27 09:59:30 -0500 | [diff] [blame] | 6040 | else |
Ilya Dryomov | c41d13a | 2016-04-29 20:01:25 +0200 | [diff] [blame] | 6041 | ret = ceph_oid_aprintf(&rbd_dev->header_oid, GFP_KERNEL, "%s%s", |
| 6042 | RBD_HEADER_PREFIX, spec->image_id); |
Alex Elder | 332bb12 | 2013-04-27 09:59:30 -0500 | [diff] [blame] | 6043 | |
Ilya Dryomov | c41d13a | 2016-04-29 20:01:25 +0200 | [diff] [blame] | 6044 | return ret; |
Alex Elder | 332bb12 | 2013-04-27 09:59:30 -0500 | [diff] [blame] | 6045 | } |
| 6046 | |
Alex Elder | 200a6a8 | 2013-04-28 23:32:34 -0500 | [diff] [blame] | 6047 | static void rbd_dev_image_release(struct rbd_device *rbd_dev) |
| 6048 | { |
Alex Elder | 6fd48b3 | 2013-04-28 23:32:34 -0500 | [diff] [blame] | 6049 | rbd_dev_unprobe(rbd_dev); |
Alex Elder | 6fd48b3 | 2013-04-28 23:32:34 -0500 | [diff] [blame] | 6050 | rbd_dev->image_format = 0; |
| 6051 | kfree(rbd_dev->spec->image_id); |
| 6052 | rbd_dev->spec->image_id = NULL; |
| 6053 | |
Alex Elder | 200a6a8 | 2013-04-28 23:32:34 -0500 | [diff] [blame] | 6054 | rbd_dev_destroy(rbd_dev); |
| 6055 | } |
| 6056 | |
Alex Elder | a30b71b | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 6057 | /* |
| 6058 | * Probe for the existence of the header object for the given rbd |
Alex Elder | 1f3ef78 | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 6059 | * device. If this image is the one being mapped (i.e., not a |
| 6060 | * parent), initiate a watch on its header object before using that |
| 6061 | * object to get detailed information about the rbd image. |
Alex Elder | a30b71b | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 6062 | */ |
Ilya Dryomov | 6d69bb53 | 2015-10-11 19:38:00 +0200 | [diff] [blame] | 6063 | static int rbd_dev_image_probe(struct rbd_device *rbd_dev, int depth) |
Alex Elder | a30b71b | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 6064 | { |
| 6065 | int ret; |
| 6066 | |
| 6067 | /* |
Alex Elder | 3abef3b | 2013-05-13 20:35:37 -0500 | [diff] [blame] | 6068 | * Get the id from the image id object. Unless there's an |
| 6069 | * error, rbd_dev->spec->image_id will be filled in with |
| 6070 | * a dynamically-allocated string, and rbd_dev->image_format |
| 6071 | * will be set to either 1 or 2. |
Alex Elder | a30b71b | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 6072 | */ |
| 6073 | ret = rbd_dev_image_id(rbd_dev); |
| 6074 | if (ret) |
Alex Elder | c0fba36 | 2013-04-25 23:15:08 -0500 | [diff] [blame] | 6075 | return ret; |
Alex Elder | c0fba36 | 2013-04-25 23:15:08 -0500 | [diff] [blame] | 6076 | |
Alex Elder | 332bb12 | 2013-04-27 09:59:30 -0500 | [diff] [blame] | 6077 | ret = rbd_dev_header_name(rbd_dev); |
| 6078 | if (ret) |
| 6079 | goto err_out_format; |
| 6080 | |
Ilya Dryomov | 6d69bb53 | 2015-10-11 19:38:00 +0200 | [diff] [blame] | 6081 | if (!depth) { |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 6082 | ret = rbd_register_watch(rbd_dev); |
Ilya Dryomov | 1fe4802 | 2015-03-05 10:47:22 +0300 | [diff] [blame] | 6083 | if (ret) { |
| 6084 | if (ret == -ENOENT) |
| 6085 | pr_info("image %s/%s does not exist\n", |
| 6086 | rbd_dev->spec->pool_name, |
| 6087 | rbd_dev->spec->image_name); |
Ilya Dryomov | c41d13a | 2016-04-29 20:01:25 +0200 | [diff] [blame] | 6088 | goto err_out_format; |
Ilya Dryomov | 1fe4802 | 2015-03-05 10:47:22 +0300 | [diff] [blame] | 6089 | } |
Alex Elder | 1f3ef78 | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 6090 | } |
Alex Elder | b644de2 | 2013-04-27 09:59:31 -0500 | [diff] [blame] | 6091 | |
Ilya Dryomov | a720ae0 | 2014-07-23 17:11:19 +0400 | [diff] [blame] | 6092 | ret = rbd_dev_header_info(rbd_dev); |
Alex Elder | 5655c4d | 2013-04-25 23:15:08 -0500 | [diff] [blame] | 6093 | if (ret) |
Alex Elder | b644de2 | 2013-04-27 09:59:31 -0500 | [diff] [blame] | 6094 | goto err_out_watch; |
Alex Elder | 83a0626 | 2012-10-30 15:47:17 -0500 | [diff] [blame] | 6095 | |
Ilya Dryomov | 0407759 | 2014-07-23 17:11:20 +0400 | [diff] [blame] | 6096 | /* |
| 6097 | * If this image is the one being mapped, we have pool name and |
| 6098 | * id, image name and id, and snap name - need to fill snap id. |
| 6099 | * Otherwise this is a parent image, identified by pool, image |
| 6100 | * and snap ids - need to fill in names for those ids. |
| 6101 | */ |
Ilya Dryomov | 6d69bb53 | 2015-10-11 19:38:00 +0200 | [diff] [blame] | 6102 | if (!depth) |
Ilya Dryomov | 0407759 | 2014-07-23 17:11:20 +0400 | [diff] [blame] | 6103 | ret = rbd_spec_fill_snap_id(rbd_dev); |
| 6104 | else |
| 6105 | ret = rbd_spec_fill_names(rbd_dev); |
Ilya Dryomov | 1fe4802 | 2015-03-05 10:47:22 +0300 | [diff] [blame] | 6106 | if (ret) { |
| 6107 | if (ret == -ENOENT) |
| 6108 | pr_info("snap %s/%s@%s does not exist\n", |
| 6109 | rbd_dev->spec->pool_name, |
| 6110 | rbd_dev->spec->image_name, |
| 6111 | rbd_dev->spec->snap_name); |
Alex Elder | 33dca39 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 6112 | goto err_out_probe; |
Ilya Dryomov | 1fe4802 | 2015-03-05 10:47:22 +0300 | [diff] [blame] | 6113 | } |
Alex Elder | 9bb81c9 | 2013-04-27 09:59:30 -0500 | [diff] [blame] | 6114 | |
Ilya Dryomov | e8f59b5 | 2014-07-24 10:42:13 +0400 | [diff] [blame] | 6115 | if (rbd_dev->header.features & RBD_FEATURE_LAYERING) { |
| 6116 | ret = rbd_dev_v2_parent_info(rbd_dev); |
| 6117 | if (ret) |
| 6118 | goto err_out_probe; |
| 6119 | |
| 6120 | /* |
| 6121 | * Need to warn users if this image is the one being |
| 6122 | * mapped and has a parent. |
| 6123 | */ |
Ilya Dryomov | 6d69bb53 | 2015-10-11 19:38:00 +0200 | [diff] [blame] | 6124 | if (!depth && rbd_dev->parent_spec) |
Ilya Dryomov | e8f59b5 | 2014-07-24 10:42:13 +0400 | [diff] [blame] | 6125 | rbd_warn(rbd_dev, |
| 6126 | "WARNING: kernel layering is EXPERIMENTAL!"); |
| 6127 | } |
| 6128 | |
Ilya Dryomov | 6d69bb53 | 2015-10-11 19:38:00 +0200 | [diff] [blame] | 6129 | ret = rbd_dev_probe_parent(rbd_dev, depth); |
Alex Elder | 30d60ba | 2013-05-06 09:51:30 -0500 | [diff] [blame] | 6130 | if (ret) |
| 6131 | goto err_out_probe; |
Alex Elder | 83a0626 | 2012-10-30 15:47:17 -0500 | [diff] [blame] | 6132 | |
Alex Elder | 30d60ba | 2013-05-06 09:51:30 -0500 | [diff] [blame] | 6133 | dout("discovered format %u image, header name is %s\n", |
Ilya Dryomov | c41d13a | 2016-04-29 20:01:25 +0200 | [diff] [blame] | 6134 | rbd_dev->image_format, rbd_dev->header_oid.name); |
Alex Elder | 30d60ba | 2013-05-06 09:51:30 -0500 | [diff] [blame] | 6135 | return 0; |
Ilya Dryomov | e8f59b5 | 2014-07-24 10:42:13 +0400 | [diff] [blame] | 6136 | |
Alex Elder | 6fd48b3 | 2013-04-28 23:32:34 -0500 | [diff] [blame] | 6137 | err_out_probe: |
| 6138 | rbd_dev_unprobe(rbd_dev); |
Alex Elder | b644de2 | 2013-04-27 09:59:31 -0500 | [diff] [blame] | 6139 | err_out_watch: |
Ilya Dryomov | 6d69bb53 | 2015-10-11 19:38:00 +0200 | [diff] [blame] | 6140 | if (!depth) |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 6141 | rbd_unregister_watch(rbd_dev); |
Alex Elder | 332bb12 | 2013-04-27 09:59:30 -0500 | [diff] [blame] | 6142 | err_out_format: |
| 6143 | rbd_dev->image_format = 0; |
Alex Elder | 5655c4d | 2013-04-25 23:15:08 -0500 | [diff] [blame] | 6144 | kfree(rbd_dev->spec->image_id); |
| 6145 | rbd_dev->spec->image_id = NULL; |
Alex Elder | 5655c4d | 2013-04-25 23:15:08 -0500 | [diff] [blame] | 6146 | return ret; |
Alex Elder | a30b71b | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 6147 | } |
| 6148 | |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 6149 | static ssize_t do_rbd_add(struct bus_type *bus, |
| 6150 | const char *buf, |
| 6151 | size_t count) |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6152 | { |
Alex Elder | cb8627c | 2012-07-09 21:04:23 -0500 | [diff] [blame] | 6153 | struct rbd_device *rbd_dev = NULL; |
Alex Elder | dc79b11 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 6154 | struct ceph_options *ceph_opts = NULL; |
Alex Elder | 4e9afeb | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 6155 | struct rbd_options *rbd_opts = NULL; |
Alex Elder | 859c31d | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 6156 | struct rbd_spec *spec = NULL; |
Alex Elder | 9d3997f | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 6157 | struct rbd_client *rbdc; |
Alex Elder | 51344a3 | 2013-05-06 07:40:30 -0500 | [diff] [blame] | 6158 | bool read_only; |
Ilya Dryomov | b51c83c | 2015-10-15 15:38:57 +0200 | [diff] [blame] | 6159 | int rc; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6160 | |
| 6161 | if (!try_module_get(THIS_MODULE)) |
| 6162 | return -ENODEV; |
| 6163 | |
Alex Elder | a725f65e | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 6164 | /* parse add command */ |
Alex Elder | 859c31d | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 6165 | rc = rbd_add_parse_args(buf, &ceph_opts, &rbd_opts, &spec); |
Alex Elder | dc79b11 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 6166 | if (rc < 0) |
Ilya Dryomov | dd5ac32 | 2015-10-16 17:09:24 +0200 | [diff] [blame] | 6167 | goto out; |
Alex Elder | a725f65e | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 6168 | |
Alex Elder | 9d3997f | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 6169 | rbdc = rbd_get_client(ceph_opts); |
| 6170 | if (IS_ERR(rbdc)) { |
| 6171 | rc = PTR_ERR(rbdc); |
Alex Elder | 0ddebc0 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 6172 | goto err_out_args; |
Alex Elder | 9d3997f | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 6173 | } |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6174 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6175 | /* pick the pool */ |
Ilya Dryomov | 30ba1f0 | 2014-05-13 11:19:27 +0400 | [diff] [blame] | 6176 | rc = rbd_add_get_pool_id(rbdc, spec->pool_name); |
Ilya Dryomov | 1fe4802 | 2015-03-05 10:47:22 +0300 | [diff] [blame] | 6177 | if (rc < 0) { |
| 6178 | if (rc == -ENOENT) |
| 6179 | pr_info("pool %s does not exist\n", spec->pool_name); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6180 | goto err_out_client; |
Ilya Dryomov | 1fe4802 | 2015-03-05 10:47:22 +0300 | [diff] [blame] | 6181 | } |
Alex Elder | c0cd10db | 2013-04-26 09:43:47 -0500 | [diff] [blame] | 6182 | spec->pool_id = (u64)rc; |
Alex Elder | 859c31d | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 6183 | |
Ilya Dryomov | d147543 | 2015-06-22 13:24:48 +0300 | [diff] [blame] | 6184 | rbd_dev = rbd_dev_create(rbdc, spec, rbd_opts); |
Ilya Dryomov | b51c83c | 2015-10-15 15:38:57 +0200 | [diff] [blame] | 6185 | if (!rbd_dev) { |
| 6186 | rc = -ENOMEM; |
Alex Elder | bd4ba65 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 6187 | goto err_out_client; |
Ilya Dryomov | b51c83c | 2015-10-15 15:38:57 +0200 | [diff] [blame] | 6188 | } |
Alex Elder | c53d589 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 6189 | rbdc = NULL; /* rbd_dev now owns this */ |
| 6190 | spec = NULL; /* rbd_dev now owns this */ |
Ilya Dryomov | d147543 | 2015-06-22 13:24:48 +0300 | [diff] [blame] | 6191 | rbd_opts = NULL; /* rbd_dev now owns this */ |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6192 | |
Ilya Dryomov | 811c668 | 2016-04-15 16:22:16 +0200 | [diff] [blame] | 6193 | down_write(&rbd_dev->header_rwsem); |
Ilya Dryomov | 6d69bb53 | 2015-10-11 19:38:00 +0200 | [diff] [blame] | 6194 | rc = rbd_dev_image_probe(rbd_dev, 0); |
Alex Elder | a30b71b | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 6195 | if (rc < 0) |
Alex Elder | c53d589 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 6196 | goto err_out_rbd_dev; |
Alex Elder | 05fd6f6 | 2012-08-29 17:11:07 -0500 | [diff] [blame] | 6197 | |
Alex Elder | 7ce4eef | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 6198 | /* If we are mapping a snapshot it must be marked read-only */ |
| 6199 | |
Ilya Dryomov | d147543 | 2015-06-22 13:24:48 +0300 | [diff] [blame] | 6200 | read_only = rbd_dev->opts->read_only; |
Alex Elder | 7ce4eef | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 6201 | if (rbd_dev->spec->snap_id != CEPH_NOSNAP) |
| 6202 | read_only = true; |
| 6203 | rbd_dev->mapping.read_only = read_only; |
| 6204 | |
Alex Elder | b536f69 | 2013-04-28 23:32:34 -0500 | [diff] [blame] | 6205 | rc = rbd_dev_device_setup(rbd_dev); |
Alex Elder | 3abef3b | 2013-05-13 20:35:37 -0500 | [diff] [blame] | 6206 | if (rc) { |
Ilya Dryomov | e37180c | 2013-12-16 18:02:41 +0200 | [diff] [blame] | 6207 | /* |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 6208 | * rbd_unregister_watch() can't be moved into |
Ilya Dryomov | e37180c | 2013-12-16 18:02:41 +0200 | [diff] [blame] | 6209 | * rbd_dev_image_release() without refactoring, see |
| 6210 | * commit 1f3ef78861ac. |
| 6211 | */ |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 6212 | rbd_unregister_watch(rbd_dev); |
Alex Elder | 3abef3b | 2013-05-13 20:35:37 -0500 | [diff] [blame] | 6213 | rbd_dev_image_release(rbd_dev); |
Ilya Dryomov | dd5ac32 | 2015-10-16 17:09:24 +0200 | [diff] [blame] | 6214 | goto out; |
Alex Elder | 3abef3b | 2013-05-13 20:35:37 -0500 | [diff] [blame] | 6215 | } |
Alex Elder | b536f69 | 2013-04-28 23:32:34 -0500 | [diff] [blame] | 6216 | |
Ilya Dryomov | dd5ac32 | 2015-10-16 17:09:24 +0200 | [diff] [blame] | 6217 | rc = count; |
| 6218 | out: |
| 6219 | module_put(THIS_MODULE); |
| 6220 | return rc; |
Alex Elder | 3abef3b | 2013-05-13 20:35:37 -0500 | [diff] [blame] | 6221 | |
Alex Elder | c53d589 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 6222 | err_out_rbd_dev: |
Ilya Dryomov | 811c668 | 2016-04-15 16:22:16 +0200 | [diff] [blame] | 6223 | up_write(&rbd_dev->header_rwsem); |
Alex Elder | c53d589 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 6224 | rbd_dev_destroy(rbd_dev); |
Alex Elder | bd4ba65 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 6225 | err_out_client: |
Alex Elder | 9d3997f | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 6226 | rbd_put_client(rbdc); |
Alex Elder | 0ddebc0 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 6227 | err_out_args: |
Alex Elder | 859c31d | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 6228 | rbd_spec_put(spec); |
Ilya Dryomov | d147543 | 2015-06-22 13:24:48 +0300 | [diff] [blame] | 6229 | kfree(rbd_opts); |
Ilya Dryomov | dd5ac32 | 2015-10-16 17:09:24 +0200 | [diff] [blame] | 6230 | goto out; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6231 | } |
| 6232 | |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 6233 | static ssize_t rbd_add(struct bus_type *bus, |
| 6234 | const char *buf, |
| 6235 | size_t count) |
| 6236 | { |
| 6237 | if (single_major) |
| 6238 | return -EINVAL; |
| 6239 | |
| 6240 | return do_rbd_add(bus, buf, count); |
| 6241 | } |
| 6242 | |
| 6243 | static ssize_t rbd_add_single_major(struct bus_type *bus, |
| 6244 | const char *buf, |
| 6245 | size_t count) |
| 6246 | { |
| 6247 | return do_rbd_add(bus, buf, count); |
| 6248 | } |
| 6249 | |
Ilya Dryomov | dd5ac32 | 2015-10-16 17:09:24 +0200 | [diff] [blame] | 6250 | static void rbd_dev_device_release(struct rbd_device *rbd_dev) |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6251 | { |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6252 | rbd_free_disk(rbd_dev); |
Ilya Dryomov | 1643dfa | 2016-08-12 15:45:52 +0200 | [diff] [blame] | 6253 | |
| 6254 | spin_lock(&rbd_dev_list_lock); |
| 6255 | list_del_init(&rbd_dev->node); |
| 6256 | spin_unlock(&rbd_dev_list_lock); |
| 6257 | |
Alex Elder | 200a6a8 | 2013-04-28 23:32:34 -0500 | [diff] [blame] | 6258 | clear_bit(RBD_DEV_FLAG_EXISTS, &rbd_dev->flags); |
Ilya Dryomov | dd5ac32 | 2015-10-16 17:09:24 +0200 | [diff] [blame] | 6259 | device_del(&rbd_dev->dev); |
Alex Elder | 6d80b13 | 2013-05-06 07:40:30 -0500 | [diff] [blame] | 6260 | rbd_dev_mapping_clear(rbd_dev); |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 6261 | if (!single_major) |
| 6262 | unregister_blkdev(rbd_dev->major, rbd_dev->name); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6263 | } |
| 6264 | |
Alex Elder | 05a46af | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 6265 | static void rbd_dev_remove_parent(struct rbd_device *rbd_dev) |
| 6266 | { |
Alex Elder | ad945fc | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 6267 | while (rbd_dev->parent) { |
Alex Elder | 05a46af | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 6268 | struct rbd_device *first = rbd_dev; |
| 6269 | struct rbd_device *second = first->parent; |
| 6270 | struct rbd_device *third; |
| 6271 | |
| 6272 | /* |
| 6273 | * Follow to the parent with no grandparent and |
| 6274 | * remove it. |
| 6275 | */ |
| 6276 | while (second && (third = second->parent)) { |
| 6277 | first = second; |
| 6278 | second = third; |
| 6279 | } |
Alex Elder | ad945fc | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 6280 | rbd_assert(second); |
Alex Elder | 8ad42cd | 2013-04-28 23:32:34 -0500 | [diff] [blame] | 6281 | rbd_dev_image_release(second); |
Alex Elder | ad945fc | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 6282 | first->parent = NULL; |
| 6283 | first->parent_overlap = 0; |
| 6284 | |
| 6285 | rbd_assert(first->parent_spec); |
Alex Elder | 05a46af | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 6286 | rbd_spec_put(first->parent_spec); |
| 6287 | first->parent_spec = NULL; |
Alex Elder | 05a46af | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 6288 | } |
| 6289 | } |
| 6290 | |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 6291 | static ssize_t do_rbd_remove(struct bus_type *bus, |
| 6292 | const char *buf, |
| 6293 | size_t count) |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6294 | { |
| 6295 | struct rbd_device *rbd_dev = NULL; |
Alex Elder | 751cc0e | 2013-05-31 15:17:01 -0500 | [diff] [blame] | 6296 | struct list_head *tmp; |
| 6297 | int dev_id; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6298 | unsigned long ul; |
Alex Elder | 82a442d | 2013-05-31 17:40:44 -0500 | [diff] [blame] | 6299 | bool already = false; |
Alex Elder | 0d8189e | 2013-04-27 09:59:30 -0500 | [diff] [blame] | 6300 | int ret; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6301 | |
Jingoo Han | bb8e0e8 | 2013-09-11 14:20:07 -0700 | [diff] [blame] | 6302 | ret = kstrtoul(buf, 10, &ul); |
Alex Elder | 0d8189e | 2013-04-27 09:59:30 -0500 | [diff] [blame] | 6303 | if (ret) |
| 6304 | return ret; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6305 | |
| 6306 | /* convert to int; abort if we lost anything in the conversion */ |
Alex Elder | 751cc0e | 2013-05-31 15:17:01 -0500 | [diff] [blame] | 6307 | dev_id = (int)ul; |
| 6308 | if (dev_id != ul) |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6309 | return -EINVAL; |
| 6310 | |
Alex Elder | 751cc0e | 2013-05-31 15:17:01 -0500 | [diff] [blame] | 6311 | ret = -ENOENT; |
| 6312 | spin_lock(&rbd_dev_list_lock); |
| 6313 | list_for_each(tmp, &rbd_dev_list) { |
| 6314 | rbd_dev = list_entry(tmp, struct rbd_device, node); |
| 6315 | if (rbd_dev->dev_id == dev_id) { |
| 6316 | ret = 0; |
| 6317 | break; |
| 6318 | } |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6319 | } |
Alex Elder | 751cc0e | 2013-05-31 15:17:01 -0500 | [diff] [blame] | 6320 | if (!ret) { |
| 6321 | spin_lock_irq(&rbd_dev->lock); |
| 6322 | if (rbd_dev->open_count) |
| 6323 | ret = -EBUSY; |
| 6324 | else |
Alex Elder | 82a442d | 2013-05-31 17:40:44 -0500 | [diff] [blame] | 6325 | already = test_and_set_bit(RBD_DEV_FLAG_REMOVING, |
| 6326 | &rbd_dev->flags); |
Alex Elder | 751cc0e | 2013-05-31 15:17:01 -0500 | [diff] [blame] | 6327 | spin_unlock_irq(&rbd_dev->lock); |
| 6328 | } |
| 6329 | spin_unlock(&rbd_dev_list_lock); |
Alex Elder | 82a442d | 2013-05-31 17:40:44 -0500 | [diff] [blame] | 6330 | if (ret < 0 || already) |
Alex Elder | 1ba0f1e | 2013-05-31 15:17:01 -0500 | [diff] [blame] | 6331 | return ret; |
Alex Elder | 751cc0e | 2013-05-31 15:17:01 -0500 | [diff] [blame] | 6332 | |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 6333 | down_write(&rbd_dev->lock_rwsem); |
| 6334 | if (__rbd_is_lock_owner(rbd_dev)) |
| 6335 | rbd_unlock(rbd_dev); |
| 6336 | up_write(&rbd_dev->lock_rwsem); |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 6337 | rbd_unregister_watch(rbd_dev); |
Ilya Dryomov | fca2706 | 2013-12-16 18:02:40 +0200 | [diff] [blame] | 6338 | |
Josh Durgin | 9875201 | 2013-08-29 17:26:31 -0700 | [diff] [blame] | 6339 | /* |
| 6340 | * Don't free anything from rbd_dev->disk until after all |
| 6341 | * notifies are completely processed. Otherwise |
| 6342 | * rbd_bus_del_dev() will race with rbd_watch_cb(), resulting |
| 6343 | * in a potential use after free of rbd_dev->disk or rbd_dev. |
| 6344 | */ |
Ilya Dryomov | dd5ac32 | 2015-10-16 17:09:24 +0200 | [diff] [blame] | 6345 | rbd_dev_device_release(rbd_dev); |
Alex Elder | 8ad42cd | 2013-04-28 23:32:34 -0500 | [diff] [blame] | 6346 | rbd_dev_image_release(rbd_dev); |
Alex Elder | aafb230 | 2012-09-06 16:00:54 -0500 | [diff] [blame] | 6347 | |
Alex Elder | 1ba0f1e | 2013-05-31 15:17:01 -0500 | [diff] [blame] | 6348 | return count; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6349 | } |
| 6350 | |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 6351 | static ssize_t rbd_remove(struct bus_type *bus, |
| 6352 | const char *buf, |
| 6353 | size_t count) |
| 6354 | { |
| 6355 | if (single_major) |
| 6356 | return -EINVAL; |
| 6357 | |
| 6358 | return do_rbd_remove(bus, buf, count); |
| 6359 | } |
| 6360 | |
| 6361 | static ssize_t rbd_remove_single_major(struct bus_type *bus, |
| 6362 | const char *buf, |
| 6363 | size_t count) |
| 6364 | { |
| 6365 | return do_rbd_remove(bus, buf, count); |
| 6366 | } |
| 6367 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6368 | /* |
| 6369 | * create control files in sysfs |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 6370 | * /sys/bus/rbd/... |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6371 | */ |
| 6372 | static int rbd_sysfs_init(void) |
| 6373 | { |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 6374 | int ret; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6375 | |
Alex Elder | fed4c14 | 2012-02-07 12:03:36 -0600 | [diff] [blame] | 6376 | ret = device_register(&rbd_root_dev); |
Alex Elder | 2107978 | 2012-01-24 10:08:36 -0600 | [diff] [blame] | 6377 | if (ret < 0) |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 6378 | return ret; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6379 | |
Alex Elder | fed4c14 | 2012-02-07 12:03:36 -0600 | [diff] [blame] | 6380 | ret = bus_register(&rbd_bus_type); |
| 6381 | if (ret < 0) |
| 6382 | device_unregister(&rbd_root_dev); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6383 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6384 | return ret; |
| 6385 | } |
| 6386 | |
| 6387 | static void rbd_sysfs_cleanup(void) |
| 6388 | { |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 6389 | bus_unregister(&rbd_bus_type); |
Alex Elder | fed4c14 | 2012-02-07 12:03:36 -0600 | [diff] [blame] | 6390 | device_unregister(&rbd_root_dev); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6391 | } |
| 6392 | |
Alex Elder | 1c2a9df | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 6393 | static int rbd_slab_init(void) |
| 6394 | { |
| 6395 | rbd_assert(!rbd_img_request_cache); |
Geliang Tang | 03d9440 | 2016-03-13 15:17:32 +0800 | [diff] [blame] | 6396 | rbd_img_request_cache = KMEM_CACHE(rbd_img_request, 0); |
Alex Elder | 868311b | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 6397 | if (!rbd_img_request_cache) |
| 6398 | return -ENOMEM; |
| 6399 | |
| 6400 | rbd_assert(!rbd_obj_request_cache); |
Geliang Tang | 03d9440 | 2016-03-13 15:17:32 +0800 | [diff] [blame] | 6401 | rbd_obj_request_cache = KMEM_CACHE(rbd_obj_request, 0); |
Alex Elder | 78c2a44 | 2013-05-01 12:43:04 -0500 | [diff] [blame] | 6402 | if (!rbd_obj_request_cache) |
| 6403 | goto out_err; |
| 6404 | |
| 6405 | rbd_assert(!rbd_segment_name_cache); |
| 6406 | rbd_segment_name_cache = kmem_cache_create("rbd_segment_name", |
Ilya Dryomov | 2d0ebc5 | 2014-01-27 17:40:18 +0200 | [diff] [blame] | 6407 | CEPH_MAX_OID_NAME_LEN + 1, 1, 0, NULL); |
Alex Elder | 78c2a44 | 2013-05-01 12:43:04 -0500 | [diff] [blame] | 6408 | if (rbd_segment_name_cache) |
Alex Elder | 1c2a9df | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 6409 | return 0; |
Alex Elder | 78c2a44 | 2013-05-01 12:43:04 -0500 | [diff] [blame] | 6410 | out_err: |
Julia Lawall | 13bf283 | 2015-09-13 14:15:26 +0200 | [diff] [blame] | 6411 | kmem_cache_destroy(rbd_obj_request_cache); |
| 6412 | rbd_obj_request_cache = NULL; |
Alex Elder | 1c2a9df | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 6413 | |
Alex Elder | 868311b | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 6414 | kmem_cache_destroy(rbd_img_request_cache); |
| 6415 | rbd_img_request_cache = NULL; |
| 6416 | |
Alex Elder | 1c2a9df | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 6417 | return -ENOMEM; |
| 6418 | } |
| 6419 | |
| 6420 | static void rbd_slab_exit(void) |
| 6421 | { |
Alex Elder | 78c2a44 | 2013-05-01 12:43:04 -0500 | [diff] [blame] | 6422 | rbd_assert(rbd_segment_name_cache); |
| 6423 | kmem_cache_destroy(rbd_segment_name_cache); |
| 6424 | rbd_segment_name_cache = NULL; |
| 6425 | |
Alex Elder | 868311b | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 6426 | rbd_assert(rbd_obj_request_cache); |
| 6427 | kmem_cache_destroy(rbd_obj_request_cache); |
| 6428 | rbd_obj_request_cache = NULL; |
| 6429 | |
Alex Elder | 1c2a9df | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 6430 | rbd_assert(rbd_img_request_cache); |
| 6431 | kmem_cache_destroy(rbd_img_request_cache); |
| 6432 | rbd_img_request_cache = NULL; |
| 6433 | } |
| 6434 | |
Alex Elder | cc344fa | 2013-02-19 12:25:56 -0600 | [diff] [blame] | 6435 | static int __init rbd_init(void) |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6436 | { |
| 6437 | int rc; |
| 6438 | |
Alex Elder | 1e32d34 | 2013-01-30 11:13:33 -0600 | [diff] [blame] | 6439 | if (!libceph_compatible(NULL)) { |
| 6440 | rbd_warn(NULL, "libceph incompatibility (quitting)"); |
Alex Elder | 1e32d34 | 2013-01-30 11:13:33 -0600 | [diff] [blame] | 6441 | return -EINVAL; |
| 6442 | } |
Ilya Dryomov | e1b4d96 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 6443 | |
Alex Elder | 1c2a9df | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 6444 | rc = rbd_slab_init(); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6445 | if (rc) |
| 6446 | return rc; |
Ilya Dryomov | e1b4d96 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 6447 | |
Ilya Dryomov | f5ee37b | 2014-10-09 17:06:01 +0400 | [diff] [blame] | 6448 | /* |
| 6449 | * The number of active work items is limited by the number of |
Ilya Dryomov | f77303b | 2015-04-22 18:28:13 +0300 | [diff] [blame] | 6450 | * rbd devices * queue depth, so leave @max_active at default. |
Ilya Dryomov | f5ee37b | 2014-10-09 17:06:01 +0400 | [diff] [blame] | 6451 | */ |
| 6452 | rbd_wq = alloc_workqueue(RBD_DRV_NAME, WQ_MEM_RECLAIM, 0); |
| 6453 | if (!rbd_wq) { |
| 6454 | rc = -ENOMEM; |
| 6455 | goto err_out_slab; |
| 6456 | } |
| 6457 | |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 6458 | if (single_major) { |
| 6459 | rbd_major = register_blkdev(0, RBD_DRV_NAME); |
| 6460 | if (rbd_major < 0) { |
| 6461 | rc = rbd_major; |
Ilya Dryomov | f5ee37b | 2014-10-09 17:06:01 +0400 | [diff] [blame] | 6462 | goto err_out_wq; |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 6463 | } |
| 6464 | } |
| 6465 | |
Alex Elder | 1c2a9df | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 6466 | rc = rbd_sysfs_init(); |
| 6467 | if (rc) |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 6468 | goto err_out_blkdev; |
Alex Elder | 1c2a9df | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 6469 | |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 6470 | if (single_major) |
| 6471 | pr_info("loaded (major %d)\n", rbd_major); |
| 6472 | else |
| 6473 | pr_info("loaded\n"); |
| 6474 | |
Ilya Dryomov | e1b4d96 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 6475 | return 0; |
| 6476 | |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 6477 | err_out_blkdev: |
| 6478 | if (single_major) |
| 6479 | unregister_blkdev(rbd_major, RBD_DRV_NAME); |
Ilya Dryomov | f5ee37b | 2014-10-09 17:06:01 +0400 | [diff] [blame] | 6480 | err_out_wq: |
| 6481 | destroy_workqueue(rbd_wq); |
Ilya Dryomov | e1b4d96 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 6482 | err_out_slab: |
| 6483 | rbd_slab_exit(); |
Alex Elder | 1c2a9df | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 6484 | return rc; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6485 | } |
| 6486 | |
Alex Elder | cc344fa | 2013-02-19 12:25:56 -0600 | [diff] [blame] | 6487 | static void __exit rbd_exit(void) |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6488 | { |
Ilya Dryomov | ffe312c | 2014-05-20 15:46:04 +0400 | [diff] [blame] | 6489 | ida_destroy(&rbd_dev_id_ida); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6490 | rbd_sysfs_cleanup(); |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 6491 | if (single_major) |
| 6492 | unregister_blkdev(rbd_major, RBD_DRV_NAME); |
Ilya Dryomov | f5ee37b | 2014-10-09 17:06:01 +0400 | [diff] [blame] | 6493 | destroy_workqueue(rbd_wq); |
Alex Elder | 1c2a9df | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 6494 | rbd_slab_exit(); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6495 | } |
| 6496 | |
| 6497 | module_init(rbd_init); |
| 6498 | module_exit(rbd_exit); |
| 6499 | |
Alex Elder | d552c61 | 2013-05-31 20:13:09 -0500 | [diff] [blame] | 6500 | MODULE_AUTHOR("Alex Elder <elder@inktank.com>"); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6501 | MODULE_AUTHOR("Sage Weil <sage@newdream.net>"); |
| 6502 | MODULE_AUTHOR("Yehuda Sadeh <yehuda@hq.newdream.net>"); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6503 | /* following authorship retained from original osdblk.c */ |
| 6504 | MODULE_AUTHOR("Jeff Garzik <jeff@garzik.org>"); |
| 6505 | |
Ilya Dryomov | 90da258 | 2013-12-13 15:28:56 +0200 | [diff] [blame] | 6506 | MODULE_DESCRIPTION("RADOS Block Device (RBD) driver"); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6507 | MODULE_LICENSE("GPL"); |