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 | |
Ilya Dryomov | 8767b29 | 2017-03-02 19:56:57 +0100 | [diff] [blame] | 123 | #define RBD_FEATURE_LAYERING (1ULL<<0) |
| 124 | #define RBD_FEATURE_STRIPINGV2 (1ULL<<1) |
| 125 | #define RBD_FEATURE_EXCLUSIVE_LOCK (1ULL<<2) |
| 126 | #define RBD_FEATURE_DATA_POOL (1ULL<<7) |
| 127 | |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 128 | #define RBD_FEATURES_ALL (RBD_FEATURE_LAYERING | \ |
| 129 | RBD_FEATURE_STRIPINGV2 | \ |
Ilya Dryomov | 7e97332 | 2017-01-25 18:16:22 +0100 | [diff] [blame] | 130 | RBD_FEATURE_EXCLUSIVE_LOCK | \ |
| 131 | RBD_FEATURE_DATA_POOL) |
Alex Elder | d889140 | 2012-10-09 13:50:17 -0700 | [diff] [blame] | 132 | |
| 133 | /* Features supported by this (client software) implementation. */ |
| 134 | |
Alex Elder | 770eba6 | 2012-10-25 23:34:40 -0500 | [diff] [blame] | 135 | #define RBD_FEATURES_SUPPORTED (RBD_FEATURES_ALL) |
Alex Elder | d889140 | 2012-10-09 13:50:17 -0700 | [diff] [blame] | 136 | |
Alex Elder | 81a8979 | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 137 | /* |
| 138 | * An RBD device name will be "rbd#", where the "rbd" comes from |
| 139 | * RBD_DRV_NAME above, and # is a unique integer identifier. |
Alex Elder | 81a8979 | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 140 | */ |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 141 | #define DEV_NAME_LEN 32 |
| 142 | |
| 143 | /* |
| 144 | * block device image metadata (in-memory version) |
| 145 | */ |
| 146 | struct rbd_image_header { |
Alex Elder | f35a4de | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 147 | /* These six fields never change for a given rbd image */ |
Alex Elder | 849b426 | 2012-07-09 21:04:24 -0500 | [diff] [blame] | 148 | char *object_prefix; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 149 | __u8 obj_order; |
Alex Elder | f35a4de | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 150 | u64 stripe_unit; |
| 151 | u64 stripe_count; |
Ilya Dryomov | 7e97332 | 2017-01-25 18:16:22 +0100 | [diff] [blame] | 152 | s64 data_pool_id; |
Alex Elder | f35a4de | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 153 | u64 features; /* Might be changeable someday? */ |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 154 | |
Alex Elder | f84344f | 2012-08-31 17:29:51 -0500 | [diff] [blame] | 155 | /* The remaining fields need to be updated occasionally */ |
| 156 | u64 image_size; |
| 157 | struct ceph_snap_context *snapc; |
Alex Elder | f35a4de | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 158 | char *snap_names; /* format 1 only */ |
| 159 | u64 *snap_sizes; /* format 1 only */ |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 160 | }; |
| 161 | |
Alex Elder | 0d7dbfc | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 162 | /* |
| 163 | * An rbd image specification. |
| 164 | * |
| 165 | * The tuple (pool_id, image_id, snap_id) is sufficient to uniquely |
Alex Elder | c66c6e0 | 2012-11-01 08:39:26 -0500 | [diff] [blame] | 166 | * identify an image. Each rbd_dev structure includes a pointer to |
| 167 | * an rbd_spec structure that encapsulates this identity. |
| 168 | * |
| 169 | * Each of the id's in an rbd_spec has an associated name. For a |
| 170 | * user-mapped image, the names are supplied and the id's associated |
| 171 | * with them are looked up. For a layered image, a parent image is |
| 172 | * defined by the tuple, and the names are looked up. |
| 173 | * |
| 174 | * An rbd_dev structure contains a parent_spec pointer which is |
| 175 | * non-null if the image it represents is a child in a layered |
| 176 | * image. This pointer will refer to the rbd_spec structure used |
| 177 | * by the parent rbd_dev for its own identity (i.e., the structure |
| 178 | * is shared between the parent and child). |
| 179 | * |
| 180 | * Since these structures are populated once, during the discovery |
| 181 | * phase of image construction, they are effectively immutable so |
| 182 | * we make no effort to synchronize access to them. |
| 183 | * |
| 184 | * Note that code herein does not assume the image name is known (it |
| 185 | * could be a null pointer). |
Alex Elder | 0d7dbfc | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 186 | */ |
| 187 | struct rbd_spec { |
| 188 | u64 pool_id; |
Alex Elder | ecb4dc2 | 2013-04-26 09:43:47 -0500 | [diff] [blame] | 189 | const char *pool_name; |
Alex Elder | 0d7dbfc | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 190 | |
Alex Elder | ecb4dc2 | 2013-04-26 09:43:47 -0500 | [diff] [blame] | 191 | const char *image_id; |
| 192 | const char *image_name; |
Alex Elder | 0d7dbfc | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 193 | |
| 194 | u64 snap_id; |
Alex Elder | ecb4dc2 | 2013-04-26 09:43:47 -0500 | [diff] [blame] | 195 | const char *snap_name; |
Alex Elder | 0d7dbfc | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 196 | |
| 197 | struct kref kref; |
| 198 | }; |
| 199 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 200 | /* |
Alex Elder | f0f8cef | 2012-01-29 13:57:44 -0600 | [diff] [blame] | 201 | * an instance of the client. multiple devices may share an rbd client. |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 202 | */ |
| 203 | struct rbd_client { |
| 204 | struct ceph_client *client; |
| 205 | struct kref kref; |
| 206 | struct list_head node; |
| 207 | }; |
| 208 | |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 209 | struct rbd_img_request; |
| 210 | typedef void (*rbd_img_callback_t)(struct rbd_img_request *); |
| 211 | |
| 212 | #define BAD_WHICH U32_MAX /* Good which or bad which, which? */ |
| 213 | |
| 214 | struct rbd_obj_request; |
| 215 | typedef void (*rbd_obj_callback_t)(struct rbd_obj_request *); |
| 216 | |
Alex Elder | 9969ebc | 2013-01-18 12:31:10 -0600 | [diff] [blame] | 217 | enum obj_request_type { |
| 218 | OBJ_REQUEST_NODATA, OBJ_REQUEST_BIO, OBJ_REQUEST_PAGES |
| 219 | }; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 220 | |
Guangliang Zhao | 6d2940c | 2014-03-13 11:21:35 +0800 | [diff] [blame] | 221 | enum obj_operation_type { |
| 222 | OBJ_OP_WRITE, |
| 223 | OBJ_OP_READ, |
Guangliang Zhao | 90e98c5 | 2014-04-01 22:22:16 +0800 | [diff] [blame] | 224 | OBJ_OP_DISCARD, |
Guangliang Zhao | 6d2940c | 2014-03-13 11:21:35 +0800 | [diff] [blame] | 225 | }; |
| 226 | |
Alex Elder | 926f9b3 | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 227 | enum obj_req_flags { |
| 228 | OBJ_REQ_DONE, /* completion flag: not done = 0, done = 1 */ |
Alex Elder | 6365d33 | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 229 | OBJ_REQ_IMG_DATA, /* object usage: standalone = 0, image = 1 */ |
Alex Elder | 5679c59 | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 230 | OBJ_REQ_KNOWN, /* EXISTS flag valid: no = 0, yes = 1 */ |
| 231 | OBJ_REQ_EXISTS, /* target exists: no = 0, yes = 1 */ |
Alex Elder | 926f9b3 | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 232 | }; |
| 233 | |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 234 | struct rbd_obj_request { |
Ilya Dryomov | a90bb0c | 2017-01-25 18:16:23 +0100 | [diff] [blame] | 235 | u64 object_no; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 236 | u64 offset; /* object start byte */ |
| 237 | u64 length; /* bytes from offset */ |
Alex Elder | 926f9b3 | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 238 | unsigned long flags; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 239 | |
Alex Elder | c5b5ef6 | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 240 | /* |
| 241 | * An object request associated with an image will have its |
| 242 | * img_data flag set; a standalone object request will not. |
| 243 | * |
| 244 | * A standalone object request will have which == BAD_WHICH |
| 245 | * and a null obj_request pointer. |
| 246 | * |
| 247 | * An object request initiated in support of a layered image |
| 248 | * object (to check for its existence before a write) will |
| 249 | * have which == BAD_WHICH and a non-null obj_request pointer. |
| 250 | * |
| 251 | * Finally, an object request for rbd image data will have |
| 252 | * which != BAD_WHICH, and will have a non-null img_request |
| 253 | * pointer. The value of which will be in the range |
| 254 | * 0..(img_request->obj_request_count-1). |
| 255 | */ |
| 256 | union { |
| 257 | struct rbd_obj_request *obj_request; /* STAT op */ |
| 258 | struct { |
| 259 | struct rbd_img_request *img_request; |
| 260 | u64 img_offset; |
| 261 | /* links for img_request->obj_requests list */ |
| 262 | struct list_head links; |
| 263 | }; |
| 264 | }; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 265 | u32 which; /* posn image request list */ |
| 266 | |
| 267 | enum obj_request_type type; |
Alex Elder | 788e2df | 2013-01-17 12:25:27 -0600 | [diff] [blame] | 268 | union { |
| 269 | struct bio *bio_list; |
| 270 | struct { |
| 271 | struct page **pages; |
| 272 | u32 page_count; |
| 273 | }; |
| 274 | }; |
Alex Elder | 0eefd47 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 275 | struct page **copyup_pages; |
Alex Elder | ebda640 | 2013-05-10 16:29:22 -0500 | [diff] [blame] | 276 | u32 copyup_page_count; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 277 | |
| 278 | struct ceph_osd_request *osd_req; |
| 279 | |
| 280 | u64 xferred; /* bytes transferred */ |
Sage Weil | 1b83bef | 2013-02-25 16:11:12 -0800 | [diff] [blame] | 281 | int result; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 282 | |
| 283 | rbd_obj_callback_t callback; |
Alex Elder | 788e2df | 2013-01-17 12:25:27 -0600 | [diff] [blame] | 284 | struct completion completion; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 285 | |
| 286 | struct kref kref; |
| 287 | }; |
| 288 | |
Alex Elder | 0c42524 | 2013-02-08 09:55:49 -0600 | [diff] [blame] | 289 | enum img_req_flags { |
Alex Elder | 9849e98 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 290 | IMG_REQ_WRITE, /* I/O direction: read = 0, write = 1 */ |
| 291 | IMG_REQ_CHILD, /* initiator: block = 0, child image = 1 */ |
Alex Elder | d0b2e94 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 292 | IMG_REQ_LAYERED, /* ENOENT handling: normal = 0, layered = 1 */ |
Guangliang Zhao | 90e98c5 | 2014-04-01 22:22:16 +0800 | [diff] [blame] | 293 | IMG_REQ_DISCARD, /* discard: normal = 0, discard request = 1 */ |
Alex Elder | 0c42524 | 2013-02-08 09:55:49 -0600 | [diff] [blame] | 294 | }; |
| 295 | |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 296 | struct rbd_img_request { |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 297 | struct rbd_device *rbd_dev; |
| 298 | u64 offset; /* starting image byte offset */ |
| 299 | u64 length; /* byte count from offset */ |
Alex Elder | 0c42524 | 2013-02-08 09:55:49 -0600 | [diff] [blame] | 300 | unsigned long flags; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 301 | union { |
Alex Elder | 9849e98 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 302 | u64 snap_id; /* for reads */ |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 303 | struct ceph_snap_context *snapc; /* for writes */ |
Alex Elder | 9849e98 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 304 | }; |
| 305 | union { |
| 306 | struct request *rq; /* block request */ |
| 307 | struct rbd_obj_request *obj_request; /* obj req initiator */ |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 308 | }; |
Alex Elder | 3d7efd1 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 309 | struct page **copyup_pages; |
Alex Elder | ebda640 | 2013-05-10 16:29:22 -0500 | [diff] [blame] | 310 | u32 copyup_page_count; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 311 | spinlock_t completion_lock;/* protects next_completion */ |
| 312 | u32 next_completion; |
| 313 | rbd_img_callback_t callback; |
Alex Elder | 55f27e0 | 2013-04-10 12:34:25 -0500 | [diff] [blame] | 314 | u64 xferred;/* aggregate bytes transferred */ |
Alex Elder | a5a337d | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 315 | int result; /* first nonzero obj_request result */ |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 316 | |
| 317 | u32 obj_request_count; |
| 318 | struct list_head obj_requests; /* rbd_obj_request structs */ |
| 319 | |
| 320 | struct kref kref; |
| 321 | }; |
| 322 | |
| 323 | #define for_each_obj_request(ireq, oreq) \ |
Alex Elder | ef06f4d3 | 2013-02-08 09:55:48 -0600 | [diff] [blame] | 324 | list_for_each_entry(oreq, &(ireq)->obj_requests, links) |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 325 | #define for_each_obj_request_from(ireq, oreq) \ |
Alex Elder | ef06f4d3 | 2013-02-08 09:55:48 -0600 | [diff] [blame] | 326 | list_for_each_entry_from(oreq, &(ireq)->obj_requests, links) |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 327 | #define for_each_obj_request_safe(ireq, oreq, n) \ |
Alex Elder | ef06f4d3 | 2013-02-08 09:55:48 -0600 | [diff] [blame] | 328 | list_for_each_entry_safe_reverse(oreq, n, &(ireq)->obj_requests, links) |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 329 | |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 330 | enum rbd_watch_state { |
| 331 | RBD_WATCH_STATE_UNREGISTERED, |
| 332 | RBD_WATCH_STATE_REGISTERED, |
| 333 | RBD_WATCH_STATE_ERROR, |
| 334 | }; |
| 335 | |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 336 | enum rbd_lock_state { |
| 337 | RBD_LOCK_STATE_UNLOCKED, |
| 338 | RBD_LOCK_STATE_LOCKED, |
| 339 | RBD_LOCK_STATE_RELEASING, |
| 340 | }; |
| 341 | |
| 342 | /* WatchNotify::ClientId */ |
| 343 | struct rbd_client_id { |
| 344 | u64 gid; |
| 345 | u64 handle; |
| 346 | }; |
| 347 | |
Alex Elder | f84344f | 2012-08-31 17:29:51 -0500 | [diff] [blame] | 348 | struct rbd_mapping { |
Alex Elder | 99c1f08 | 2012-08-30 14:42:15 -0500 | [diff] [blame] | 349 | u64 size; |
Alex Elder | 34b1318 | 2012-07-13 20:35:12 -0500 | [diff] [blame] | 350 | u64 features; |
Alex Elder | f84344f | 2012-08-31 17:29:51 -0500 | [diff] [blame] | 351 | }; |
| 352 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 353 | /* |
| 354 | * a single device |
| 355 | */ |
| 356 | struct rbd_device { |
Alex Elder | de71a29 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 357 | int dev_id; /* blkdev unique id */ |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 358 | |
| 359 | int major; /* blkdev assigned major */ |
Ilya Dryomov | dd82fff | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 360 | int minor; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 361 | struct gendisk *disk; /* blkdev's gendisk and rq */ |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 362 | |
Alex Elder | a30b71b | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 363 | u32 image_format; /* Either 1 or 2 */ |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 364 | struct rbd_client *rbd_client; |
| 365 | |
| 366 | char name[DEV_NAME_LEN]; /* blkdev name, e.g. rbd3 */ |
| 367 | |
Alex Elder | b82d167 | 2013-01-14 12:43:31 -0600 | [diff] [blame] | 368 | spinlock_t lock; /* queue, flags, open_count */ |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 369 | |
| 370 | struct rbd_image_header header; |
Alex Elder | b82d167 | 2013-01-14 12:43:31 -0600 | [diff] [blame] | 371 | unsigned long flags; /* possibly lock protected */ |
Alex Elder | 0d7dbfc | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 372 | struct rbd_spec *spec; |
Ilya Dryomov | d147543 | 2015-06-22 13:24:48 +0300 | [diff] [blame] | 373 | struct rbd_options *opts; |
Mike Christie | 0d6d1e9c | 2016-08-18 18:38:45 +0200 | [diff] [blame] | 374 | char *config_info; /* add{,_single_major} string */ |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 375 | |
Ilya Dryomov | c41d13a | 2016-04-29 20:01:25 +0200 | [diff] [blame] | 376 | struct ceph_object_id header_oid; |
Ilya Dryomov | 922dab6 | 2016-05-26 01:15:02 +0200 | [diff] [blame] | 377 | struct ceph_object_locator header_oloc; |
Alex Elder | 971f839 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 378 | |
Ilya Dryomov | 1643dfa | 2016-08-12 15:45:52 +0200 | [diff] [blame] | 379 | struct ceph_file_layout layout; /* used for all rbd requests */ |
Alex Elder | 0903e87 | 2012-11-14 12:25:19 -0600 | [diff] [blame] | 380 | |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 381 | struct mutex watch_mutex; |
| 382 | enum rbd_watch_state watch_state; |
Ilya Dryomov | 922dab6 | 2016-05-26 01:15:02 +0200 | [diff] [blame] | 383 | struct ceph_osd_linger_request *watch_handle; |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 384 | u64 watch_cookie; |
| 385 | struct delayed_work watch_dwork; |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 386 | |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 387 | struct rw_semaphore lock_rwsem; |
| 388 | enum rbd_lock_state lock_state; |
Ilya Dryomov | cbbfb0f | 2017-04-13 12:17:38 +0200 | [diff] [blame] | 389 | char lock_cookie[32]; |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 390 | struct rbd_client_id owner_cid; |
| 391 | struct work_struct acquired_lock_work; |
| 392 | struct work_struct released_lock_work; |
| 393 | struct delayed_work lock_dwork; |
| 394 | struct work_struct unlock_work; |
| 395 | wait_queue_head_t lock_waitq; |
| 396 | |
Ilya Dryomov | 1643dfa | 2016-08-12 15:45:52 +0200 | [diff] [blame] | 397 | struct workqueue_struct *task_wq; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 398 | |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 399 | struct rbd_spec *parent_spec; |
| 400 | u64 parent_overlap; |
Alex Elder | a2acd00 | 2013-05-08 22:50:04 -0500 | [diff] [blame] | 401 | atomic_t parent_ref; |
Alex Elder | 2f82ee5 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 402 | struct rbd_device *parent; |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 403 | |
Christoph Hellwig | 7ad18af | 2015-01-13 17:20:04 +0100 | [diff] [blame] | 404 | /* Block layer tags. */ |
| 405 | struct blk_mq_tag_set tag_set; |
| 406 | |
Josh Durgin | c666601 | 2011-11-21 17:11:12 -0800 | [diff] [blame] | 407 | /* protects updating the header */ |
| 408 | struct rw_semaphore header_rwsem; |
Alex Elder | f84344f | 2012-08-31 17:29:51 -0500 | [diff] [blame] | 409 | |
| 410 | struct rbd_mapping mapping; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 411 | |
| 412 | struct list_head node; |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 413 | |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 414 | /* sysfs related */ |
| 415 | struct device dev; |
Alex Elder | b82d167 | 2013-01-14 12:43:31 -0600 | [diff] [blame] | 416 | unsigned long open_count; /* protected by lock */ |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 417 | }; |
| 418 | |
Alex Elder | b82d167 | 2013-01-14 12:43:31 -0600 | [diff] [blame] | 419 | /* |
Ilya Dryomov | 87c0fde | 2016-09-29 13:41:05 +0200 | [diff] [blame] | 420 | * Flag bits for rbd_dev->flags: |
| 421 | * - REMOVING (which is coupled with rbd_dev->open_count) is protected |
| 422 | * by rbd_dev->lock |
| 423 | * - BLACKLISTED is protected by rbd_dev->lock_rwsem |
Alex Elder | b82d167 | 2013-01-14 12:43:31 -0600 | [diff] [blame] | 424 | */ |
Alex Elder | 6d29290 | 2013-01-14 12:43:31 -0600 | [diff] [blame] | 425 | enum rbd_dev_flags { |
| 426 | RBD_DEV_FLAG_EXISTS, /* mapped snapshot has not been deleted */ |
Alex Elder | b82d167 | 2013-01-14 12:43:31 -0600 | [diff] [blame] | 427 | RBD_DEV_FLAG_REMOVING, /* this mapping is being removed */ |
Ilya Dryomov | 87c0fde | 2016-09-29 13:41:05 +0200 | [diff] [blame] | 428 | RBD_DEV_FLAG_BLACKLISTED, /* our ceph_client is blacklisted */ |
Alex Elder | 6d29290 | 2013-01-14 12:43:31 -0600 | [diff] [blame] | 429 | }; |
| 430 | |
Alex Elder | cfbf637 | 2013-05-31 17:40:45 -0500 | [diff] [blame] | 431 | static DEFINE_MUTEX(client_mutex); /* Serialize client creation */ |
Alex Elder | e124a82f | 2012-01-29 13:57:44 -0600 | [diff] [blame] | 432 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 433 | static LIST_HEAD(rbd_dev_list); /* devices */ |
Alex Elder | e124a82f | 2012-01-29 13:57:44 -0600 | [diff] [blame] | 434 | static DEFINE_SPINLOCK(rbd_dev_list_lock); |
| 435 | |
Alex Elder | 432b858 | 2012-01-29 13:57:44 -0600 | [diff] [blame] | 436 | static LIST_HEAD(rbd_client_list); /* clients */ |
| 437 | static DEFINE_SPINLOCK(rbd_client_list_lock); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 438 | |
Alex Elder | 78c2a44 | 2013-05-01 12:43:04 -0500 | [diff] [blame] | 439 | /* Slab caches for frequently-allocated structures */ |
| 440 | |
Alex Elder | 1c2a9df | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 441 | static struct kmem_cache *rbd_img_request_cache; |
Alex Elder | 868311b | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 442 | static struct kmem_cache *rbd_obj_request_cache; |
Alex Elder | 1c2a9df | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 443 | |
NeilBrown | f856dc3 | 2017-06-18 14:38:58 +1000 | [diff] [blame] | 444 | static struct bio_set *rbd_bio_clone; |
| 445 | |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 446 | static int rbd_major; |
Ilya Dryomov | f8a22fc | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 447 | static DEFINE_IDA(rbd_dev_id_ida); |
| 448 | |
Ilya Dryomov | f5ee37b | 2014-10-09 17:06:01 +0400 | [diff] [blame] | 449 | static struct workqueue_struct *rbd_wq; |
| 450 | |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 451 | /* |
| 452 | * Default to false for now, as single-major requires >= 0.75 version of |
| 453 | * userspace rbd utility. |
| 454 | */ |
| 455 | static bool single_major = false; |
| 456 | module_param(single_major, bool, S_IRUGO); |
| 457 | MODULE_PARM_DESC(single_major, "Use a single major number for all rbd devices (default: false)"); |
| 458 | |
Alex Elder | 3d7efd1 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 459 | static int rbd_img_request_submit(struct rbd_img_request *img_request); |
| 460 | |
Alex Elder | f0f8cef | 2012-01-29 13:57:44 -0600 | [diff] [blame] | 461 | static ssize_t rbd_add(struct bus_type *bus, const char *buf, |
| 462 | size_t count); |
| 463 | static ssize_t rbd_remove(struct bus_type *bus, const char *buf, |
| 464 | size_t count); |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 465 | static ssize_t rbd_add_single_major(struct bus_type *bus, const char *buf, |
| 466 | size_t count); |
| 467 | static ssize_t rbd_remove_single_major(struct bus_type *bus, const char *buf, |
| 468 | size_t count); |
Ilya Dryomov | 6d69bb53 | 2015-10-11 19:38:00 +0200 | [diff] [blame] | 469 | 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] | 470 | static void rbd_spec_put(struct rbd_spec *spec); |
Alex Elder | f0f8cef | 2012-01-29 13:57:44 -0600 | [diff] [blame] | 471 | |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 472 | static int rbd_dev_id_to_minor(int dev_id) |
| 473 | { |
Ilya Dryomov | 7e513d4 | 2013-12-16 19:26:32 +0200 | [diff] [blame] | 474 | return dev_id << RBD_SINGLE_MAJOR_PART_SHIFT; |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 475 | } |
| 476 | |
| 477 | static int minor_to_rbd_dev_id(int minor) |
| 478 | { |
Ilya Dryomov | 7e513d4 | 2013-12-16 19:26:32 +0200 | [diff] [blame] | 479 | return minor >> RBD_SINGLE_MAJOR_PART_SHIFT; |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 480 | } |
| 481 | |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 482 | static bool __rbd_is_lock_owner(struct rbd_device *rbd_dev) |
| 483 | { |
| 484 | return rbd_dev->lock_state == RBD_LOCK_STATE_LOCKED || |
| 485 | rbd_dev->lock_state == RBD_LOCK_STATE_RELEASING; |
| 486 | } |
| 487 | |
| 488 | static bool rbd_is_lock_owner(struct rbd_device *rbd_dev) |
| 489 | { |
| 490 | bool is_lock_owner; |
| 491 | |
| 492 | down_read(&rbd_dev->lock_rwsem); |
| 493 | is_lock_owner = __rbd_is_lock_owner(rbd_dev); |
| 494 | up_read(&rbd_dev->lock_rwsem); |
| 495 | return is_lock_owner; |
| 496 | } |
| 497 | |
Ilya Dryomov | 8767b29 | 2017-03-02 19:56:57 +0100 | [diff] [blame] | 498 | static ssize_t rbd_supported_features_show(struct bus_type *bus, char *buf) |
| 499 | { |
| 500 | return sprintf(buf, "0x%llx\n", RBD_FEATURES_SUPPORTED); |
| 501 | } |
| 502 | |
Greg Kroah-Hartman | b15a21d | 2013-08-23 14:24:28 -0700 | [diff] [blame] | 503 | static BUS_ATTR(add, S_IWUSR, NULL, rbd_add); |
| 504 | static BUS_ATTR(remove, S_IWUSR, NULL, rbd_remove); |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 505 | static BUS_ATTR(add_single_major, S_IWUSR, NULL, rbd_add_single_major); |
| 506 | static BUS_ATTR(remove_single_major, S_IWUSR, NULL, rbd_remove_single_major); |
Ilya Dryomov | 8767b29 | 2017-03-02 19:56:57 +0100 | [diff] [blame] | 507 | static BUS_ATTR(supported_features, S_IRUGO, rbd_supported_features_show, NULL); |
Greg Kroah-Hartman | b15a21d | 2013-08-23 14:24:28 -0700 | [diff] [blame] | 508 | |
| 509 | static struct attribute *rbd_bus_attrs[] = { |
| 510 | &bus_attr_add.attr, |
| 511 | &bus_attr_remove.attr, |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 512 | &bus_attr_add_single_major.attr, |
| 513 | &bus_attr_remove_single_major.attr, |
Ilya Dryomov | 8767b29 | 2017-03-02 19:56:57 +0100 | [diff] [blame] | 514 | &bus_attr_supported_features.attr, |
Greg Kroah-Hartman | b15a21d | 2013-08-23 14:24:28 -0700 | [diff] [blame] | 515 | NULL, |
Alex Elder | f0f8cef | 2012-01-29 13:57:44 -0600 | [diff] [blame] | 516 | }; |
Ilya Dryomov | 92c76dc | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 517 | |
| 518 | static umode_t rbd_bus_is_visible(struct kobject *kobj, |
| 519 | struct attribute *attr, int index) |
| 520 | { |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 521 | if (!single_major && |
| 522 | (attr == &bus_attr_add_single_major.attr || |
| 523 | attr == &bus_attr_remove_single_major.attr)) |
| 524 | return 0; |
| 525 | |
Ilya Dryomov | 92c76dc | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 526 | return attr->mode; |
| 527 | } |
| 528 | |
| 529 | static const struct attribute_group rbd_bus_group = { |
| 530 | .attrs = rbd_bus_attrs, |
| 531 | .is_visible = rbd_bus_is_visible, |
| 532 | }; |
| 533 | __ATTRIBUTE_GROUPS(rbd_bus); |
Alex Elder | f0f8cef | 2012-01-29 13:57:44 -0600 | [diff] [blame] | 534 | |
| 535 | static struct bus_type rbd_bus_type = { |
| 536 | .name = "rbd", |
Greg Kroah-Hartman | b15a21d | 2013-08-23 14:24:28 -0700 | [diff] [blame] | 537 | .bus_groups = rbd_bus_groups, |
Alex Elder | f0f8cef | 2012-01-29 13:57:44 -0600 | [diff] [blame] | 538 | }; |
| 539 | |
| 540 | static void rbd_root_dev_release(struct device *dev) |
| 541 | { |
| 542 | } |
| 543 | |
| 544 | static struct device rbd_root_dev = { |
| 545 | .init_name = "rbd", |
| 546 | .release = rbd_root_dev_release, |
| 547 | }; |
| 548 | |
Alex Elder | 06ecc6c | 2012-11-01 10:17:15 -0500 | [diff] [blame] | 549 | static __printf(2, 3) |
| 550 | void rbd_warn(struct rbd_device *rbd_dev, const char *fmt, ...) |
| 551 | { |
| 552 | struct va_format vaf; |
| 553 | va_list args; |
| 554 | |
| 555 | va_start(args, fmt); |
| 556 | vaf.fmt = fmt; |
| 557 | vaf.va = &args; |
| 558 | |
| 559 | if (!rbd_dev) |
| 560 | printk(KERN_WARNING "%s: %pV\n", RBD_DRV_NAME, &vaf); |
| 561 | else if (rbd_dev->disk) |
| 562 | printk(KERN_WARNING "%s: %s: %pV\n", |
| 563 | RBD_DRV_NAME, rbd_dev->disk->disk_name, &vaf); |
| 564 | else if (rbd_dev->spec && rbd_dev->spec->image_name) |
| 565 | printk(KERN_WARNING "%s: image %s: %pV\n", |
| 566 | RBD_DRV_NAME, rbd_dev->spec->image_name, &vaf); |
| 567 | else if (rbd_dev->spec && rbd_dev->spec->image_id) |
| 568 | printk(KERN_WARNING "%s: id %s: %pV\n", |
| 569 | RBD_DRV_NAME, rbd_dev->spec->image_id, &vaf); |
| 570 | else /* punt */ |
| 571 | printk(KERN_WARNING "%s: rbd_dev %p: %pV\n", |
| 572 | RBD_DRV_NAME, rbd_dev, &vaf); |
| 573 | va_end(args); |
| 574 | } |
| 575 | |
Alex Elder | aafb230 | 2012-09-06 16:00:54 -0500 | [diff] [blame] | 576 | #ifdef RBD_DEBUG |
| 577 | #define rbd_assert(expr) \ |
| 578 | if (unlikely(!(expr))) { \ |
| 579 | printk(KERN_ERR "\nAssertion failure in %s() " \ |
| 580 | "at line %d:\n\n" \ |
| 581 | "\trbd_assert(%s);\n\n", \ |
| 582 | __func__, __LINE__, #expr); \ |
| 583 | BUG(); \ |
| 584 | } |
| 585 | #else /* !RBD_DEBUG */ |
| 586 | # define rbd_assert(expr) ((void) 0) |
| 587 | #endif /* !RBD_DEBUG */ |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 588 | |
Ilya Dryomov | 2761713 | 2015-07-16 17:36:11 +0300 | [diff] [blame] | 589 | static void rbd_osd_copyup_callback(struct rbd_obj_request *obj_request); |
Alex Elder | b454e36 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 590 | 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] | 591 | static void rbd_img_parent_read(struct rbd_obj_request *obj_request); |
| 592 | static void rbd_dev_remove_parent(struct rbd_device *rbd_dev); |
Alex Elder | 8b3e1a5 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 593 | |
Alex Elder | cc4a38bd | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 594 | static int rbd_dev_refresh(struct rbd_device *rbd_dev); |
Alex Elder | 2df3fac | 2013-05-06 09:51:30 -0500 | [diff] [blame] | 595 | static int rbd_dev_v2_header_onetime(struct rbd_device *rbd_dev); |
Ilya Dryomov | a720ae0 | 2014-07-23 17:11:19 +0400 | [diff] [blame] | 596 | static int rbd_dev_header_info(struct rbd_device *rbd_dev); |
Ilya Dryomov | e8f59b5 | 2014-07-24 10:42:13 +0400 | [diff] [blame] | 597 | static int rbd_dev_v2_parent_info(struct rbd_device *rbd_dev); |
Alex Elder | 54cac61 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 598 | static const char *rbd_dev_v2_snap_name(struct rbd_device *rbd_dev, |
| 599 | u64 snap_id); |
Alex Elder | 2ad3d71 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 600 | static int _rbd_dev_v2_snap_size(struct rbd_device *rbd_dev, u64 snap_id, |
| 601 | u8 *order, u64 *snap_size); |
| 602 | static int _rbd_dev_v2_snap_features(struct rbd_device *rbd_dev, u64 snap_id, |
| 603 | u64 *snap_features); |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 604 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 605 | static int rbd_open(struct block_device *bdev, fmode_t mode) |
| 606 | { |
Alex Elder | f0f8cef | 2012-01-29 13:57:44 -0600 | [diff] [blame] | 607 | struct rbd_device *rbd_dev = bdev->bd_disk->private_data; |
Alex Elder | b82d167 | 2013-01-14 12:43:31 -0600 | [diff] [blame] | 608 | bool removing = false; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 609 | |
Alex Elder | a14ea26 | 2013-02-05 13:23:12 -0600 | [diff] [blame] | 610 | spin_lock_irq(&rbd_dev->lock); |
Alex Elder | b82d167 | 2013-01-14 12:43:31 -0600 | [diff] [blame] | 611 | if (test_bit(RBD_DEV_FLAG_REMOVING, &rbd_dev->flags)) |
| 612 | removing = true; |
| 613 | else |
| 614 | rbd_dev->open_count++; |
Alex Elder | a14ea26 | 2013-02-05 13:23:12 -0600 | [diff] [blame] | 615 | spin_unlock_irq(&rbd_dev->lock); |
Alex Elder | b82d167 | 2013-01-14 12:43:31 -0600 | [diff] [blame] | 616 | if (removing) |
| 617 | return -ENOENT; |
| 618 | |
Alex Elder | c3e946c | 2012-11-16 09:29:16 -0600 | [diff] [blame] | 619 | (void) get_device(&rbd_dev->dev); |
Alex Elder | 340c7a2 | 2012-08-10 13:12:07 -0700 | [diff] [blame] | 620 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 621 | return 0; |
| 622 | } |
| 623 | |
Al Viro | db2a144 | 2013-05-05 21:52:57 -0400 | [diff] [blame] | 624 | static void rbd_release(struct gendisk *disk, fmode_t mode) |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 625 | { |
| 626 | struct rbd_device *rbd_dev = disk->private_data; |
Alex Elder | b82d167 | 2013-01-14 12:43:31 -0600 | [diff] [blame] | 627 | unsigned long open_count_before; |
| 628 | |
Alex Elder | a14ea26 | 2013-02-05 13:23:12 -0600 | [diff] [blame] | 629 | spin_lock_irq(&rbd_dev->lock); |
Alex Elder | b82d167 | 2013-01-14 12:43:31 -0600 | [diff] [blame] | 630 | open_count_before = rbd_dev->open_count--; |
Alex Elder | a14ea26 | 2013-02-05 13:23:12 -0600 | [diff] [blame] | 631 | spin_unlock_irq(&rbd_dev->lock); |
Alex Elder | b82d167 | 2013-01-14 12:43:31 -0600 | [diff] [blame] | 632 | rbd_assert(open_count_before > 0); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 633 | |
Alex Elder | c3e946c | 2012-11-16 09:29:16 -0600 | [diff] [blame] | 634 | put_device(&rbd_dev->dev); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 635 | } |
| 636 | |
Guangliang Zhao | 131fd9f | 2013-09-24 11:25:36 +0800 | [diff] [blame] | 637 | static int rbd_ioctl_set_ro(struct rbd_device *rbd_dev, unsigned long arg) |
| 638 | { |
Ilya Dryomov | 1de797b | 2017-10-12 12:35:19 +0200 | [diff] [blame] | 639 | int ro; |
Guangliang Zhao | 131fd9f | 2013-09-24 11:25:36 +0800 | [diff] [blame] | 640 | |
Ilya Dryomov | 1de797b | 2017-10-12 12:35:19 +0200 | [diff] [blame] | 641 | if (get_user(ro, (int __user *)arg)) |
Guangliang Zhao | 131fd9f | 2013-09-24 11:25:36 +0800 | [diff] [blame] | 642 | return -EFAULT; |
| 643 | |
Ilya Dryomov | 1de797b | 2017-10-12 12:35:19 +0200 | [diff] [blame] | 644 | /* Snapshots can't be marked read-write */ |
Guangliang Zhao | 131fd9f | 2013-09-24 11:25:36 +0800 | [diff] [blame] | 645 | if (rbd_dev->spec->snap_id != CEPH_NOSNAP && !ro) |
| 646 | return -EROFS; |
| 647 | |
Ilya Dryomov | 1de797b | 2017-10-12 12:35:19 +0200 | [diff] [blame] | 648 | /* Let blkdev_roset() handle it */ |
| 649 | return -ENOTTY; |
Guangliang Zhao | 131fd9f | 2013-09-24 11:25:36 +0800 | [diff] [blame] | 650 | } |
| 651 | |
| 652 | static int rbd_ioctl(struct block_device *bdev, fmode_t mode, |
| 653 | unsigned int cmd, unsigned long arg) |
| 654 | { |
| 655 | struct rbd_device *rbd_dev = bdev->bd_disk->private_data; |
Ilya Dryomov | 1de797b | 2017-10-12 12:35:19 +0200 | [diff] [blame] | 656 | int ret; |
Guangliang Zhao | 131fd9f | 2013-09-24 11:25:36 +0800 | [diff] [blame] | 657 | |
Guangliang Zhao | 131fd9f | 2013-09-24 11:25:36 +0800 | [diff] [blame] | 658 | switch (cmd) { |
| 659 | case BLKROSET: |
| 660 | ret = rbd_ioctl_set_ro(rbd_dev, arg); |
| 661 | break; |
| 662 | default: |
| 663 | ret = -ENOTTY; |
| 664 | } |
| 665 | |
Guangliang Zhao | 131fd9f | 2013-09-24 11:25:36 +0800 | [diff] [blame] | 666 | return ret; |
| 667 | } |
| 668 | |
| 669 | #ifdef CONFIG_COMPAT |
| 670 | static int rbd_compat_ioctl(struct block_device *bdev, fmode_t mode, |
| 671 | unsigned int cmd, unsigned long arg) |
| 672 | { |
| 673 | return rbd_ioctl(bdev, mode, cmd, arg); |
| 674 | } |
| 675 | #endif /* CONFIG_COMPAT */ |
| 676 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 677 | static const struct block_device_operations rbd_bd_ops = { |
| 678 | .owner = THIS_MODULE, |
| 679 | .open = rbd_open, |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 680 | .release = rbd_release, |
Guangliang Zhao | 131fd9f | 2013-09-24 11:25:36 +0800 | [diff] [blame] | 681 | .ioctl = rbd_ioctl, |
| 682 | #ifdef CONFIG_COMPAT |
| 683 | .compat_ioctl = rbd_compat_ioctl, |
| 684 | #endif |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 685 | }; |
| 686 | |
| 687 | /* |
Alex Elder | 7262cfc | 2013-05-16 15:04:20 -0500 | [diff] [blame] | 688 | * Initialize an rbd client instance. Success or not, this function |
Alex Elder | cfbf637 | 2013-05-31 17:40:45 -0500 | [diff] [blame] | 689 | * consumes ceph_opts. Caller holds client_mutex. |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 690 | */ |
Alex Elder | f8c3892 | 2012-08-10 13:12:07 -0700 | [diff] [blame] | 691 | static struct rbd_client *rbd_client_create(struct ceph_options *ceph_opts) |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 692 | { |
| 693 | struct rbd_client *rbdc; |
| 694 | int ret = -ENOMEM; |
| 695 | |
Alex Elder | 37206ee | 2013-02-20 17:32:08 -0600 | [diff] [blame] | 696 | dout("%s:\n", __func__); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 697 | rbdc = kmalloc(sizeof(struct rbd_client), GFP_KERNEL); |
| 698 | if (!rbdc) |
| 699 | goto out_opt; |
| 700 | |
| 701 | kref_init(&rbdc->kref); |
| 702 | INIT_LIST_HEAD(&rbdc->node); |
| 703 | |
Ilya Dryomov | 74da4a0f | 2017-03-03 18:16:07 +0100 | [diff] [blame] | 704 | rbdc->client = ceph_create_client(ceph_opts, rbdc); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 705 | if (IS_ERR(rbdc->client)) |
Alex Elder | 08f7546 | 2013-05-29 11:19:00 -0500 | [diff] [blame] | 706 | goto out_rbdc; |
Alex Elder | 43ae470 | 2012-07-03 16:01:18 -0500 | [diff] [blame] | 707 | ceph_opts = NULL; /* Now rbdc->client is responsible for ceph_opts */ |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 708 | |
| 709 | ret = ceph_open_session(rbdc->client); |
| 710 | if (ret < 0) |
Alex Elder | 08f7546 | 2013-05-29 11:19:00 -0500 | [diff] [blame] | 711 | goto out_client; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 712 | |
Alex Elder | 432b858 | 2012-01-29 13:57:44 -0600 | [diff] [blame] | 713 | spin_lock(&rbd_client_list_lock); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 714 | list_add_tail(&rbdc->node, &rbd_client_list); |
Alex Elder | 432b858 | 2012-01-29 13:57:44 -0600 | [diff] [blame] | 715 | spin_unlock(&rbd_client_list_lock); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 716 | |
Alex Elder | 37206ee | 2013-02-20 17:32:08 -0600 | [diff] [blame] | 717 | dout("%s: rbdc %p\n", __func__, rbdc); |
Alex Elder | bc534d8 | 2012-01-29 13:57:44 -0600 | [diff] [blame] | 718 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 719 | return rbdc; |
Alex Elder | 08f7546 | 2013-05-29 11:19:00 -0500 | [diff] [blame] | 720 | out_client: |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 721 | ceph_destroy_client(rbdc->client); |
Alex Elder | 08f7546 | 2013-05-29 11:19:00 -0500 | [diff] [blame] | 722 | out_rbdc: |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 723 | kfree(rbdc); |
| 724 | out_opt: |
Alex Elder | 43ae470 | 2012-07-03 16:01:18 -0500 | [diff] [blame] | 725 | if (ceph_opts) |
| 726 | ceph_destroy_options(ceph_opts); |
Alex Elder | 37206ee | 2013-02-20 17:32:08 -0600 | [diff] [blame] | 727 | dout("%s: error %d\n", __func__, ret); |
| 728 | |
Vasiliy Kulikov | 28f259b | 2010-09-26 12:59:37 +0400 | [diff] [blame] | 729 | return ERR_PTR(ret); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 730 | } |
| 731 | |
Alex Elder | 2f82ee5 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 732 | static struct rbd_client *__rbd_get_client(struct rbd_client *rbdc) |
| 733 | { |
| 734 | kref_get(&rbdc->kref); |
| 735 | |
| 736 | return rbdc; |
| 737 | } |
| 738 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 739 | /* |
Alex Elder | 1f7ba33 | 2012-08-10 13:12:07 -0700 | [diff] [blame] | 740 | * Find a ceph client with specific addr and configuration. If |
| 741 | * found, bump its reference count. |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 742 | */ |
Alex Elder | 1f7ba33 | 2012-08-10 13:12:07 -0700 | [diff] [blame] | 743 | static struct rbd_client *rbd_client_find(struct ceph_options *ceph_opts) |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 744 | { |
| 745 | struct rbd_client *client_node; |
Alex Elder | 1f7ba33 | 2012-08-10 13:12:07 -0700 | [diff] [blame] | 746 | bool found = false; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 747 | |
Alex Elder | 43ae470 | 2012-07-03 16:01:18 -0500 | [diff] [blame] | 748 | if (ceph_opts->flags & CEPH_OPT_NOSHARE) |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 749 | return NULL; |
| 750 | |
Alex Elder | 1f7ba33 | 2012-08-10 13:12:07 -0700 | [diff] [blame] | 751 | spin_lock(&rbd_client_list_lock); |
| 752 | list_for_each_entry(client_node, &rbd_client_list, node) { |
| 753 | if (!ceph_compare_options(ceph_opts, client_node->client)) { |
Alex Elder | 2f82ee5 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 754 | __rbd_get_client(client_node); |
| 755 | |
Alex Elder | 1f7ba33 | 2012-08-10 13:12:07 -0700 | [diff] [blame] | 756 | found = true; |
| 757 | break; |
| 758 | } |
| 759 | } |
| 760 | spin_unlock(&rbd_client_list_lock); |
| 761 | |
| 762 | return found ? client_node : NULL; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 763 | } |
| 764 | |
| 765 | /* |
Ilya Dryomov | 210c104 | 2015-06-22 13:24:48 +0300 | [diff] [blame] | 766 | * (Per device) rbd map options |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 767 | */ |
| 768 | enum { |
Ilya Dryomov | b558418 | 2015-06-23 16:21:19 +0300 | [diff] [blame] | 769 | Opt_queue_depth, |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 770 | Opt_last_int, |
| 771 | /* int args above */ |
| 772 | Opt_last_string, |
| 773 | /* string args above */ |
Alex Elder | cc0538b | 2012-08-10 13:12:07 -0700 | [diff] [blame] | 774 | Opt_read_only, |
| 775 | Opt_read_write, |
Ilya Dryomov | 80de191 | 2016-09-20 14:23:17 +0200 | [diff] [blame] | 776 | Opt_lock_on_read, |
Ilya Dryomov | e010dd0 | 2017-04-13 12:17:39 +0200 | [diff] [blame] | 777 | Opt_exclusive, |
Ilya Dryomov | 210c104 | 2015-06-22 13:24:48 +0300 | [diff] [blame] | 778 | Opt_err |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 779 | }; |
| 780 | |
Alex Elder | 43ae470 | 2012-07-03 16:01:18 -0500 | [diff] [blame] | 781 | static match_table_t rbd_opts_tokens = { |
Ilya Dryomov | b558418 | 2015-06-23 16:21:19 +0300 | [diff] [blame] | 782 | {Opt_queue_depth, "queue_depth=%d"}, |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 783 | /* int args above */ |
| 784 | /* string args above */ |
Alex Elder | be466c1 | 2012-10-22 11:31:26 -0500 | [diff] [blame] | 785 | {Opt_read_only, "read_only"}, |
Alex Elder | cc0538b | 2012-08-10 13:12:07 -0700 | [diff] [blame] | 786 | {Opt_read_only, "ro"}, /* Alternate spelling */ |
| 787 | {Opt_read_write, "read_write"}, |
| 788 | {Opt_read_write, "rw"}, /* Alternate spelling */ |
Ilya Dryomov | 80de191 | 2016-09-20 14:23:17 +0200 | [diff] [blame] | 789 | {Opt_lock_on_read, "lock_on_read"}, |
Ilya Dryomov | e010dd0 | 2017-04-13 12:17:39 +0200 | [diff] [blame] | 790 | {Opt_exclusive, "exclusive"}, |
Ilya Dryomov | 210c104 | 2015-06-22 13:24:48 +0300 | [diff] [blame] | 791 | {Opt_err, NULL} |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 792 | }; |
| 793 | |
Alex Elder | 98571b5 | 2013-01-20 14:44:42 -0600 | [diff] [blame] | 794 | struct rbd_options { |
Ilya Dryomov | b558418 | 2015-06-23 16:21:19 +0300 | [diff] [blame] | 795 | int queue_depth; |
Alex Elder | 98571b5 | 2013-01-20 14:44:42 -0600 | [diff] [blame] | 796 | bool read_only; |
Ilya Dryomov | 80de191 | 2016-09-20 14:23:17 +0200 | [diff] [blame] | 797 | bool lock_on_read; |
Ilya Dryomov | e010dd0 | 2017-04-13 12:17:39 +0200 | [diff] [blame] | 798 | bool exclusive; |
Alex Elder | 98571b5 | 2013-01-20 14:44:42 -0600 | [diff] [blame] | 799 | }; |
| 800 | |
Ilya Dryomov | b558418 | 2015-06-23 16:21:19 +0300 | [diff] [blame] | 801 | #define RBD_QUEUE_DEPTH_DEFAULT BLKDEV_MAX_RQ |
Alex Elder | 98571b5 | 2013-01-20 14:44:42 -0600 | [diff] [blame] | 802 | #define RBD_READ_ONLY_DEFAULT false |
Ilya Dryomov | 80de191 | 2016-09-20 14:23:17 +0200 | [diff] [blame] | 803 | #define RBD_LOCK_ON_READ_DEFAULT false |
Ilya Dryomov | e010dd0 | 2017-04-13 12:17:39 +0200 | [diff] [blame] | 804 | #define RBD_EXCLUSIVE_DEFAULT false |
Alex Elder | 98571b5 | 2013-01-20 14:44:42 -0600 | [diff] [blame] | 805 | |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 806 | static int parse_rbd_opts_token(char *c, void *private) |
| 807 | { |
Alex Elder | 43ae470 | 2012-07-03 16:01:18 -0500 | [diff] [blame] | 808 | struct rbd_options *rbd_opts = private; |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 809 | substring_t argstr[MAX_OPT_ARGS]; |
| 810 | int token, intval, ret; |
| 811 | |
Alex Elder | 43ae470 | 2012-07-03 16:01:18 -0500 | [diff] [blame] | 812 | token = match_token(c, rbd_opts_tokens, argstr); |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 813 | if (token < Opt_last_int) { |
| 814 | ret = match_int(&argstr[0], &intval); |
| 815 | if (ret < 0) { |
Ilya Dryomov | 210c104 | 2015-06-22 13:24:48 +0300 | [diff] [blame] | 816 | pr_err("bad mount option arg (not int) at '%s'\n", c); |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 817 | return ret; |
| 818 | } |
| 819 | dout("got int token %d val %d\n", token, intval); |
| 820 | } else if (token > Opt_last_int && token < Opt_last_string) { |
Ilya Dryomov | 210c104 | 2015-06-22 13:24:48 +0300 | [diff] [blame] | 821 | dout("got string token %d val %s\n", token, argstr[0].from); |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 822 | } else { |
| 823 | dout("got token %d\n", token); |
| 824 | } |
| 825 | |
| 826 | switch (token) { |
Ilya Dryomov | b558418 | 2015-06-23 16:21:19 +0300 | [diff] [blame] | 827 | case Opt_queue_depth: |
| 828 | if (intval < 1) { |
| 829 | pr_err("queue_depth out of range\n"); |
| 830 | return -EINVAL; |
| 831 | } |
| 832 | rbd_opts->queue_depth = intval; |
| 833 | break; |
Alex Elder | cc0538b | 2012-08-10 13:12:07 -0700 | [diff] [blame] | 834 | case Opt_read_only: |
| 835 | rbd_opts->read_only = true; |
| 836 | break; |
| 837 | case Opt_read_write: |
| 838 | rbd_opts->read_only = false; |
| 839 | break; |
Ilya Dryomov | 80de191 | 2016-09-20 14:23:17 +0200 | [diff] [blame] | 840 | case Opt_lock_on_read: |
| 841 | rbd_opts->lock_on_read = true; |
| 842 | break; |
Ilya Dryomov | e010dd0 | 2017-04-13 12:17:39 +0200 | [diff] [blame] | 843 | case Opt_exclusive: |
| 844 | rbd_opts->exclusive = true; |
| 845 | break; |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 846 | default: |
Ilya Dryomov | 210c104 | 2015-06-22 13:24:48 +0300 | [diff] [blame] | 847 | /* libceph prints "bad option" msg */ |
| 848 | return -EINVAL; |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 849 | } |
Ilya Dryomov | 210c104 | 2015-06-22 13:24:48 +0300 | [diff] [blame] | 850 | |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 851 | return 0; |
| 852 | } |
| 853 | |
Guangliang Zhao | 6d2940c | 2014-03-13 11:21:35 +0800 | [diff] [blame] | 854 | static char* obj_op_name(enum obj_operation_type op_type) |
| 855 | { |
| 856 | switch (op_type) { |
| 857 | case OBJ_OP_READ: |
| 858 | return "read"; |
| 859 | case OBJ_OP_WRITE: |
| 860 | return "write"; |
Guangliang Zhao | 90e98c5 | 2014-04-01 22:22:16 +0800 | [diff] [blame] | 861 | case OBJ_OP_DISCARD: |
| 862 | return "discard"; |
Guangliang Zhao | 6d2940c | 2014-03-13 11:21:35 +0800 | [diff] [blame] | 863 | default: |
| 864 | return "???"; |
| 865 | } |
| 866 | } |
| 867 | |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 868 | /* |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 869 | * Get a ceph client with specific addr and configuration, if one does |
Alex Elder | 7262cfc | 2013-05-16 15:04:20 -0500 | [diff] [blame] | 870 | * not exist create it. Either way, ceph_opts is consumed by this |
| 871 | * function. |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 872 | */ |
Alex Elder | 9d3997f | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 873 | static struct rbd_client *rbd_get_client(struct ceph_options *ceph_opts) |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 874 | { |
Alex Elder | f8c3892 | 2012-08-10 13:12:07 -0700 | [diff] [blame] | 875 | struct rbd_client *rbdc; |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 876 | |
Alex Elder | cfbf637 | 2013-05-31 17:40:45 -0500 | [diff] [blame] | 877 | mutex_lock_nested(&client_mutex, SINGLE_DEPTH_NESTING); |
Alex Elder | 1f7ba33 | 2012-08-10 13:12:07 -0700 | [diff] [blame] | 878 | rbdc = rbd_client_find(ceph_opts); |
Alex Elder | 9d3997f | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 879 | if (rbdc) /* using an existing client */ |
Alex Elder | 43ae470 | 2012-07-03 16:01:18 -0500 | [diff] [blame] | 880 | ceph_destroy_options(ceph_opts); |
Alex Elder | 9d3997f | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 881 | else |
Alex Elder | f8c3892 | 2012-08-10 13:12:07 -0700 | [diff] [blame] | 882 | rbdc = rbd_client_create(ceph_opts); |
Alex Elder | cfbf637 | 2013-05-31 17:40:45 -0500 | [diff] [blame] | 883 | mutex_unlock(&client_mutex); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 884 | |
Alex Elder | 9d3997f | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 885 | return rbdc; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 886 | } |
| 887 | |
| 888 | /* |
| 889 | * Destroy ceph client |
Alex Elder | d23a4b3 | 2012-01-29 13:57:43 -0600 | [diff] [blame] | 890 | * |
Alex Elder | 432b858 | 2012-01-29 13:57:44 -0600 | [diff] [blame] | 891 | * Caller must hold rbd_client_list_lock. |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 892 | */ |
| 893 | static void rbd_client_release(struct kref *kref) |
| 894 | { |
| 895 | struct rbd_client *rbdc = container_of(kref, struct rbd_client, kref); |
| 896 | |
Alex Elder | 37206ee | 2013-02-20 17:32:08 -0600 | [diff] [blame] | 897 | dout("%s: rbdc %p\n", __func__, rbdc); |
Alex Elder | cd9d9f5 | 2012-04-04 13:35:44 -0500 | [diff] [blame] | 898 | spin_lock(&rbd_client_list_lock); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 899 | list_del(&rbdc->node); |
Alex Elder | cd9d9f5 | 2012-04-04 13:35:44 -0500 | [diff] [blame] | 900 | spin_unlock(&rbd_client_list_lock); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 901 | |
| 902 | ceph_destroy_client(rbdc->client); |
| 903 | kfree(rbdc); |
| 904 | } |
| 905 | |
| 906 | /* |
| 907 | * Drop reference to ceph client node. If it's not referenced anymore, release |
| 908 | * it. |
| 909 | */ |
Alex Elder | 9d3997f | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 910 | static void rbd_put_client(struct rbd_client *rbdc) |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 911 | { |
Alex Elder | c53d589 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 912 | if (rbdc) |
| 913 | kref_put(&rbdc->kref, rbd_client_release); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 914 | } |
| 915 | |
Alex Elder | a30b71b | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 916 | static bool rbd_image_format_valid(u32 image_format) |
| 917 | { |
| 918 | return image_format == 1 || image_format == 2; |
| 919 | } |
| 920 | |
Alex Elder | 8e94af8 | 2012-07-25 09:32:40 -0500 | [diff] [blame] | 921 | static bool rbd_dev_ondisk_valid(struct rbd_image_header_ondisk *ondisk) |
| 922 | { |
Alex Elder | 103a150 | 2012-08-02 11:29:45 -0500 | [diff] [blame] | 923 | size_t size; |
| 924 | u32 snap_count; |
| 925 | |
| 926 | /* The header has to start with the magic rbd header text */ |
| 927 | if (memcmp(&ondisk->text, RBD_HEADER_TEXT, sizeof (RBD_HEADER_TEXT))) |
| 928 | return false; |
| 929 | |
Alex Elder | db2388b | 2012-10-20 22:17:27 -0500 | [diff] [blame] | 930 | /* The bio layer requires at least sector-sized I/O */ |
| 931 | |
| 932 | if (ondisk->options.order < SECTOR_SHIFT) |
| 933 | return false; |
| 934 | |
| 935 | /* If we use u64 in a few spots we may be able to loosen this */ |
| 936 | |
| 937 | if (ondisk->options.order > 8 * sizeof (int) - 1) |
| 938 | return false; |
| 939 | |
Alex Elder | 103a150 | 2012-08-02 11:29:45 -0500 | [diff] [blame] | 940 | /* |
| 941 | * The size of a snapshot header has to fit in a size_t, and |
| 942 | * that limits the number of snapshots. |
| 943 | */ |
| 944 | snap_count = le32_to_cpu(ondisk->snap_count); |
| 945 | size = SIZE_MAX - sizeof (struct ceph_snap_context); |
| 946 | if (snap_count > size / sizeof (__le64)) |
| 947 | return false; |
| 948 | |
| 949 | /* |
| 950 | * Not only that, but the size of the entire the snapshot |
| 951 | * header must also be representable in a size_t. |
| 952 | */ |
| 953 | size -= snap_count * sizeof (__le64); |
| 954 | if ((u64) size < le64_to_cpu(ondisk->snap_names_len)) |
| 955 | return false; |
| 956 | |
| 957 | return true; |
Alex Elder | 8e94af8 | 2012-07-25 09:32:40 -0500 | [diff] [blame] | 958 | } |
| 959 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 960 | /* |
Ilya Dryomov | 5bc3fb1 | 2017-01-25 18:16:22 +0100 | [diff] [blame] | 961 | * returns the size of an object in the image |
| 962 | */ |
| 963 | static u32 rbd_obj_bytes(struct rbd_image_header *header) |
| 964 | { |
| 965 | return 1U << header->obj_order; |
| 966 | } |
| 967 | |
Ilya Dryomov | 263423f | 2017-01-25 18:16:22 +0100 | [diff] [blame] | 968 | static void rbd_init_layout(struct rbd_device *rbd_dev) |
| 969 | { |
| 970 | if (rbd_dev->header.stripe_unit == 0 || |
| 971 | rbd_dev->header.stripe_count == 0) { |
| 972 | rbd_dev->header.stripe_unit = rbd_obj_bytes(&rbd_dev->header); |
| 973 | rbd_dev->header.stripe_count = 1; |
| 974 | } |
| 975 | |
| 976 | rbd_dev->layout.stripe_unit = rbd_dev->header.stripe_unit; |
| 977 | rbd_dev->layout.stripe_count = rbd_dev->header.stripe_count; |
| 978 | rbd_dev->layout.object_size = rbd_obj_bytes(&rbd_dev->header); |
Ilya Dryomov | 7e97332 | 2017-01-25 18:16:22 +0100 | [diff] [blame] | 979 | rbd_dev->layout.pool_id = rbd_dev->header.data_pool_id == CEPH_NOPOOL ? |
| 980 | rbd_dev->spec->pool_id : rbd_dev->header.data_pool_id; |
Ilya Dryomov | 263423f | 2017-01-25 18:16:22 +0100 | [diff] [blame] | 981 | RCU_INIT_POINTER(rbd_dev->layout.pool_ns, NULL); |
| 982 | } |
| 983 | |
Ilya Dryomov | 5bc3fb1 | 2017-01-25 18:16:22 +0100 | [diff] [blame] | 984 | /* |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 985 | * Fill an rbd image header with information from the given format 1 |
| 986 | * on-disk header. |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 987 | */ |
Alex Elder | 662518b | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 988 | static int rbd_header_from_disk(struct rbd_device *rbd_dev, |
Alex Elder | 4156d99 | 2012-08-02 11:29:46 -0500 | [diff] [blame] | 989 | struct rbd_image_header_ondisk *ondisk) |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 990 | { |
Alex Elder | 662518b | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 991 | struct rbd_image_header *header = &rbd_dev->header; |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 992 | bool first_time = header->object_prefix == NULL; |
| 993 | struct ceph_snap_context *snapc; |
| 994 | char *object_prefix = NULL; |
| 995 | char *snap_names = NULL; |
| 996 | u64 *snap_sizes = NULL; |
Alex Elder | ccece23 | 2012-07-10 20:30:10 -0500 | [diff] [blame] | 997 | u32 snap_count; |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 998 | int ret = -ENOMEM; |
Alex Elder | 621901d | 2012-08-23 23:22:06 -0500 | [diff] [blame] | 999 | u32 i; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1000 | |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1001 | /* Allocate this now to avoid having to handle failure below */ |
| 1002 | |
| 1003 | if (first_time) { |
Ilya Dryomov | 848d796 | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 1004 | object_prefix = kstrndup(ondisk->object_prefix, |
| 1005 | sizeof(ondisk->object_prefix), |
| 1006 | GFP_KERNEL); |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1007 | if (!object_prefix) |
| 1008 | return -ENOMEM; |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1009 | } |
| 1010 | |
| 1011 | /* Allocate the snapshot context and fill it in */ |
Alex Elder | 6a52325 | 2012-07-19 17:12:59 -0500 | [diff] [blame] | 1012 | |
Alex Elder | 103a150 | 2012-08-02 11:29:45 -0500 | [diff] [blame] | 1013 | snap_count = le32_to_cpu(ondisk->snap_count); |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1014 | snapc = ceph_create_snap_context(snap_count, GFP_KERNEL); |
| 1015 | if (!snapc) |
| 1016 | goto out_err; |
| 1017 | snapc->seq = le64_to_cpu(ondisk->snap_seq); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1018 | if (snap_count) { |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1019 | struct rbd_image_snap_ondisk *snaps; |
Alex Elder | f785cc1 | 2012-08-23 23:22:06 -0500 | [diff] [blame] | 1020 | u64 snap_names_len = le64_to_cpu(ondisk->snap_names_len); |
| 1021 | |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1022 | /* We'll keep a copy of the snapshot names... */ |
Alex Elder | 621901d | 2012-08-23 23:22:06 -0500 | [diff] [blame] | 1023 | |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1024 | if (snap_names_len > (u64)SIZE_MAX) |
| 1025 | goto out_2big; |
| 1026 | snap_names = kmalloc(snap_names_len, GFP_KERNEL); |
| 1027 | if (!snap_names) |
Alex Elder | 6a52325 | 2012-07-19 17:12:59 -0500 | [diff] [blame] | 1028 | goto out_err; |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1029 | |
| 1030 | /* ...as well as the array of their sizes. */ |
Markus Elfring | 88a25a5 | 2016-09-11 12:21:25 +0200 | [diff] [blame] | 1031 | snap_sizes = kmalloc_array(snap_count, |
| 1032 | sizeof(*header->snap_sizes), |
| 1033 | GFP_KERNEL); |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1034 | if (!snap_sizes) |
| 1035 | goto out_err; |
| 1036 | |
Alex Elder | f785cc1 | 2012-08-23 23:22:06 -0500 | [diff] [blame] | 1037 | /* |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1038 | * Copy the names, and fill in each snapshot's id |
| 1039 | * and size. |
| 1040 | * |
Alex Elder | 99a41eb | 2013-05-06 09:51:30 -0500 | [diff] [blame] | 1041 | * Note that rbd_dev_v1_header_info() guarantees the |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1042 | * ondisk buffer we're working with has |
Alex Elder | f785cc1 | 2012-08-23 23:22:06 -0500 | [diff] [blame] | 1043 | * snap_names_len bytes beyond the end of the |
| 1044 | * snapshot id array, this memcpy() is safe. |
| 1045 | */ |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1046 | memcpy(snap_names, &ondisk->snaps[snap_count], snap_names_len); |
| 1047 | snaps = ondisk->snaps; |
| 1048 | for (i = 0; i < snap_count; i++) { |
| 1049 | snapc->snaps[i] = le64_to_cpu(snaps[i].id); |
| 1050 | snap_sizes[i] = le64_to_cpu(snaps[i].image_size); |
| 1051 | } |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1052 | } |
Alex Elder | 849b426 | 2012-07-09 21:04:24 -0500 | [diff] [blame] | 1053 | |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1054 | /* We won't fail any more, fill in the header */ |
Alex Elder | 6a52325 | 2012-07-19 17:12:59 -0500 | [diff] [blame] | 1055 | |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1056 | if (first_time) { |
| 1057 | header->object_prefix = object_prefix; |
| 1058 | header->obj_order = ondisk->options.order; |
Ilya Dryomov | 263423f | 2017-01-25 18:16:22 +0100 | [diff] [blame] | 1059 | rbd_init_layout(rbd_dev); |
Alex Elder | 662518b | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1060 | } else { |
| 1061 | ceph_put_snap_context(header->snapc); |
| 1062 | kfree(header->snap_names); |
| 1063 | kfree(header->snap_sizes); |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1064 | } |
| 1065 | |
| 1066 | /* The remaining fields always get updated (when we refresh) */ |
Alex Elder | 621901d | 2012-08-23 23:22:06 -0500 | [diff] [blame] | 1067 | |
Alex Elder | f84344f | 2012-08-31 17:29:51 -0500 | [diff] [blame] | 1068 | header->image_size = le64_to_cpu(ondisk->image_size); |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1069 | header->snapc = snapc; |
| 1070 | header->snap_names = snap_names; |
| 1071 | header->snap_sizes = snap_sizes; |
Alex Elder | 468521c | 2013-04-26 09:43:47 -0500 | [diff] [blame] | 1072 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1073 | return 0; |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1074 | out_2big: |
| 1075 | ret = -EIO; |
Alex Elder | 6a52325 | 2012-07-19 17:12:59 -0500 | [diff] [blame] | 1076 | out_err: |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1077 | kfree(snap_sizes); |
| 1078 | kfree(snap_names); |
| 1079 | ceph_put_snap_context(snapc); |
| 1080 | kfree(object_prefix); |
Alex Elder | ccece23 | 2012-07-10 20:30:10 -0500 | [diff] [blame] | 1081 | |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1082 | return ret; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1083 | } |
| 1084 | |
Alex Elder | 9682fc6 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 1085 | static const char *_rbd_dev_v1_snap_name(struct rbd_device *rbd_dev, u32 which) |
| 1086 | { |
| 1087 | const char *snap_name; |
| 1088 | |
| 1089 | rbd_assert(which < rbd_dev->header.snapc->num_snaps); |
| 1090 | |
| 1091 | /* Skip over names until we find the one we are looking for */ |
| 1092 | |
| 1093 | snap_name = rbd_dev->header.snap_names; |
| 1094 | while (which--) |
| 1095 | snap_name += strlen(snap_name) + 1; |
| 1096 | |
| 1097 | return kstrdup(snap_name, GFP_KERNEL); |
| 1098 | } |
| 1099 | |
Alex Elder | 30d1cff | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 1100 | /* |
| 1101 | * Snapshot id comparison function for use with qsort()/bsearch(). |
| 1102 | * Note that result is for snapshots in *descending* order. |
| 1103 | */ |
| 1104 | static int snapid_compare_reverse(const void *s1, const void *s2) |
| 1105 | { |
| 1106 | u64 snap_id1 = *(u64 *)s1; |
| 1107 | u64 snap_id2 = *(u64 *)s2; |
| 1108 | |
| 1109 | if (snap_id1 < snap_id2) |
| 1110 | return 1; |
| 1111 | return snap_id1 == snap_id2 ? 0 : -1; |
| 1112 | } |
| 1113 | |
| 1114 | /* |
| 1115 | * Search a snapshot context to see if the given snapshot id is |
| 1116 | * present. |
| 1117 | * |
| 1118 | * Returns the position of the snapshot id in the array if it's found, |
| 1119 | * or BAD_SNAP_INDEX otherwise. |
| 1120 | * |
| 1121 | * Note: The snapshot array is in kept sorted (by the osd) in |
| 1122 | * reverse order, highest snapshot id first. |
| 1123 | */ |
Alex Elder | 9682fc6 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 1124 | static u32 rbd_dev_snap_index(struct rbd_device *rbd_dev, u64 snap_id) |
| 1125 | { |
| 1126 | struct ceph_snap_context *snapc = rbd_dev->header.snapc; |
Alex Elder | 30d1cff | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 1127 | u64 *found; |
Alex Elder | 9682fc6 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 1128 | |
Alex Elder | 30d1cff | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 1129 | found = bsearch(&snap_id, &snapc->snaps, snapc->num_snaps, |
| 1130 | sizeof (snap_id), snapid_compare_reverse); |
Alex Elder | 9682fc6 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 1131 | |
Alex Elder | 30d1cff | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 1132 | return found ? (u32)(found - &snapc->snaps[0]) : BAD_SNAP_INDEX; |
Alex Elder | 9682fc6 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 1133 | } |
| 1134 | |
Alex Elder | 2ad3d71 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 1135 | static const char *rbd_dev_v1_snap_name(struct rbd_device *rbd_dev, |
| 1136 | u64 snap_id) |
Alex Elder | 54cac61 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 1137 | { |
| 1138 | u32 which; |
Josh Durgin | da6a6b6 | 2013-09-04 17:57:31 -0700 | [diff] [blame] | 1139 | const char *snap_name; |
Alex Elder | 54cac61 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 1140 | |
| 1141 | which = rbd_dev_snap_index(rbd_dev, snap_id); |
| 1142 | if (which == BAD_SNAP_INDEX) |
Josh Durgin | da6a6b6 | 2013-09-04 17:57:31 -0700 | [diff] [blame] | 1143 | return ERR_PTR(-ENOENT); |
Alex Elder | 54cac61 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 1144 | |
Josh Durgin | da6a6b6 | 2013-09-04 17:57:31 -0700 | [diff] [blame] | 1145 | snap_name = _rbd_dev_v1_snap_name(rbd_dev, which); |
| 1146 | return snap_name ? snap_name : ERR_PTR(-ENOMEM); |
Alex Elder | 54cac61 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 1147 | } |
| 1148 | |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 1149 | static const char *rbd_snap_name(struct rbd_device *rbd_dev, u64 snap_id) |
| 1150 | { |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 1151 | if (snap_id == CEPH_NOSNAP) |
| 1152 | return RBD_SNAP_HEAD_NAME; |
| 1153 | |
Alex Elder | 54cac61 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 1154 | rbd_assert(rbd_image_format_valid(rbd_dev->image_format)); |
| 1155 | if (rbd_dev->image_format == 1) |
| 1156 | return rbd_dev_v1_snap_name(rbd_dev, snap_id); |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 1157 | |
Alex Elder | 54cac61 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 1158 | return rbd_dev_v2_snap_name(rbd_dev, snap_id); |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 1159 | } |
| 1160 | |
Alex Elder | 2ad3d71 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 1161 | static int rbd_snap_size(struct rbd_device *rbd_dev, u64 snap_id, |
| 1162 | u64 *snap_size) |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1163 | { |
Alex Elder | 2ad3d71 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 1164 | rbd_assert(rbd_image_format_valid(rbd_dev->image_format)); |
| 1165 | if (snap_id == CEPH_NOSNAP) { |
| 1166 | *snap_size = rbd_dev->header.image_size; |
| 1167 | } else if (rbd_dev->image_format == 1) { |
| 1168 | u32 which; |
Alex Elder | 00f1f36 | 2012-02-07 12:03:36 -0600 | [diff] [blame] | 1169 | |
Alex Elder | 2ad3d71 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 1170 | which = rbd_dev_snap_index(rbd_dev, snap_id); |
| 1171 | if (which == BAD_SNAP_INDEX) |
| 1172 | return -ENOENT; |
Alex Elder | 00f1f36 | 2012-02-07 12:03:36 -0600 | [diff] [blame] | 1173 | |
Alex Elder | 2ad3d71 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 1174 | *snap_size = rbd_dev->header.snap_sizes[which]; |
| 1175 | } else { |
| 1176 | u64 size = 0; |
| 1177 | int ret; |
| 1178 | |
| 1179 | ret = _rbd_dev_v2_snap_size(rbd_dev, snap_id, NULL, &size); |
| 1180 | if (ret) |
| 1181 | return ret; |
| 1182 | |
| 1183 | *snap_size = size; |
| 1184 | } |
| 1185 | return 0; |
| 1186 | } |
| 1187 | |
| 1188 | static int rbd_snap_features(struct rbd_device *rbd_dev, u64 snap_id, |
| 1189 | u64 *snap_features) |
| 1190 | { |
| 1191 | rbd_assert(rbd_image_format_valid(rbd_dev->image_format)); |
| 1192 | if (snap_id == CEPH_NOSNAP) { |
| 1193 | *snap_features = rbd_dev->header.features; |
| 1194 | } else if (rbd_dev->image_format == 1) { |
| 1195 | *snap_features = 0; /* No features for format 1 */ |
| 1196 | } else { |
| 1197 | u64 features = 0; |
| 1198 | int ret; |
| 1199 | |
| 1200 | ret = _rbd_dev_v2_snap_features(rbd_dev, snap_id, &features); |
| 1201 | if (ret) |
| 1202 | return ret; |
| 1203 | |
| 1204 | *snap_features = features; |
| 1205 | } |
| 1206 | return 0; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1207 | } |
| 1208 | |
Alex Elder | d1cf578 | 2013-04-27 09:59:30 -0500 | [diff] [blame] | 1209 | static int rbd_dev_mapping_set(struct rbd_device *rbd_dev) |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1210 | { |
Alex Elder | 8f4b7d9 | 2013-05-06 07:40:30 -0500 | [diff] [blame] | 1211 | u64 snap_id = rbd_dev->spec->snap_id; |
Alex Elder | 2ad3d71 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 1212 | u64 size = 0; |
| 1213 | u64 features = 0; |
| 1214 | int ret; |
Alex Elder | 8b0241f | 2013-04-25 23:15:08 -0500 | [diff] [blame] | 1215 | |
Alex Elder | 2ad3d71 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 1216 | ret = rbd_snap_size(rbd_dev, snap_id, &size); |
| 1217 | if (ret) |
| 1218 | return ret; |
| 1219 | ret = rbd_snap_features(rbd_dev, snap_id, &features); |
| 1220 | if (ret) |
| 1221 | return ret; |
| 1222 | |
| 1223 | rbd_dev->mapping.size = size; |
| 1224 | rbd_dev->mapping.features = features; |
| 1225 | |
Alex Elder | 8b0241f | 2013-04-25 23:15:08 -0500 | [diff] [blame] | 1226 | return 0; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1227 | } |
| 1228 | |
Alex Elder | d1cf578 | 2013-04-27 09:59:30 -0500 | [diff] [blame] | 1229 | static void rbd_dev_mapping_clear(struct rbd_device *rbd_dev) |
| 1230 | { |
| 1231 | rbd_dev->mapping.size = 0; |
| 1232 | rbd_dev->mapping.features = 0; |
Alex Elder | 200a6a8 | 2013-04-28 23:32:34 -0500 | [diff] [blame] | 1233 | } |
| 1234 | |
Alex Elder | 65ccfe2 | 2012-08-09 10:33:26 -0700 | [diff] [blame] | 1235 | static u64 rbd_segment_offset(struct rbd_device *rbd_dev, u64 offset) |
| 1236 | { |
Ilya Dryomov | 5bc3fb1 | 2017-01-25 18:16:22 +0100 | [diff] [blame] | 1237 | u64 segment_size = rbd_obj_bytes(&rbd_dev->header); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1238 | |
Alex Elder | 65ccfe2 | 2012-08-09 10:33:26 -0700 | [diff] [blame] | 1239 | return offset & (segment_size - 1); |
| 1240 | } |
| 1241 | |
| 1242 | static u64 rbd_segment_length(struct rbd_device *rbd_dev, |
| 1243 | u64 offset, u64 length) |
| 1244 | { |
Ilya Dryomov | 5bc3fb1 | 2017-01-25 18:16:22 +0100 | [diff] [blame] | 1245 | u64 segment_size = rbd_obj_bytes(&rbd_dev->header); |
Alex Elder | 65ccfe2 | 2012-08-09 10:33:26 -0700 | [diff] [blame] | 1246 | |
| 1247 | offset &= segment_size - 1; |
| 1248 | |
Alex Elder | aafb230 | 2012-09-06 16:00:54 -0500 | [diff] [blame] | 1249 | rbd_assert(length <= U64_MAX - offset); |
Alex Elder | 65ccfe2 | 2012-08-09 10:33:26 -0700 | [diff] [blame] | 1250 | if (offset + length > segment_size) |
| 1251 | length = segment_size - offset; |
| 1252 | |
| 1253 | return length; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1254 | } |
| 1255 | |
| 1256 | /* |
| 1257 | * bio helpers |
| 1258 | */ |
| 1259 | |
| 1260 | static void bio_chain_put(struct bio *chain) |
| 1261 | { |
| 1262 | struct bio *tmp; |
| 1263 | |
| 1264 | while (chain) { |
| 1265 | tmp = chain; |
| 1266 | chain = chain->bi_next; |
| 1267 | bio_put(tmp); |
| 1268 | } |
| 1269 | } |
| 1270 | |
| 1271 | /* |
| 1272 | * zeros a bio chain, starting at specific offset |
| 1273 | */ |
| 1274 | static void zero_bio_chain(struct bio *chain, int start_ofs) |
| 1275 | { |
Kent Overstreet | 7988613 | 2013-11-23 17:19:00 -0800 | [diff] [blame] | 1276 | struct bio_vec bv; |
| 1277 | struct bvec_iter iter; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1278 | unsigned long flags; |
| 1279 | void *buf; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1280 | int pos = 0; |
| 1281 | |
| 1282 | while (chain) { |
Kent Overstreet | 7988613 | 2013-11-23 17:19:00 -0800 | [diff] [blame] | 1283 | bio_for_each_segment(bv, chain, iter) { |
| 1284 | if (pos + bv.bv_len > start_ofs) { |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1285 | int remainder = max(start_ofs - pos, 0); |
Kent Overstreet | 7988613 | 2013-11-23 17:19:00 -0800 | [diff] [blame] | 1286 | buf = bvec_kmap_irq(&bv, &flags); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1287 | memset(buf + remainder, 0, |
Kent Overstreet | 7988613 | 2013-11-23 17:19:00 -0800 | [diff] [blame] | 1288 | bv.bv_len - remainder); |
| 1289 | flush_dcache_page(bv.bv_page); |
Dan Carpenter | 85b5aaa | 2010-10-11 21:15:11 +0200 | [diff] [blame] | 1290 | bvec_kunmap_irq(buf, &flags); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1291 | } |
Kent Overstreet | 7988613 | 2013-11-23 17:19:00 -0800 | [diff] [blame] | 1292 | pos += bv.bv_len; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1293 | } |
| 1294 | |
| 1295 | chain = chain->bi_next; |
| 1296 | } |
| 1297 | } |
| 1298 | |
| 1299 | /* |
Alex Elder | b9434c5 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 1300 | * similar to zero_bio_chain(), zeros data defined by a page array, |
| 1301 | * starting at the given byte offset from the start of the array and |
| 1302 | * continuing up to the given end offset. The pages array is |
| 1303 | * assumed to be big enough to hold all bytes up to the end. |
| 1304 | */ |
| 1305 | static void zero_pages(struct page **pages, u64 offset, u64 end) |
| 1306 | { |
| 1307 | struct page **page = &pages[offset >> PAGE_SHIFT]; |
| 1308 | |
| 1309 | rbd_assert(end > offset); |
| 1310 | rbd_assert(end - offset <= (u64)SIZE_MAX); |
| 1311 | while (offset < end) { |
| 1312 | size_t page_offset; |
| 1313 | size_t length; |
| 1314 | unsigned long flags; |
| 1315 | void *kaddr; |
| 1316 | |
Geert Uytterhoeven | 491205a | 2013-05-13 20:35:37 -0500 | [diff] [blame] | 1317 | page_offset = offset & ~PAGE_MASK; |
| 1318 | length = min_t(size_t, PAGE_SIZE - page_offset, end - offset); |
Alex Elder | b9434c5 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 1319 | local_irq_save(flags); |
| 1320 | kaddr = kmap_atomic(*page); |
| 1321 | memset(kaddr + page_offset, 0, length); |
Alex Elder | e215605 | 2013-05-22 20:54:25 -0500 | [diff] [blame] | 1322 | flush_dcache_page(*page); |
Alex Elder | b9434c5 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 1323 | kunmap_atomic(kaddr); |
| 1324 | local_irq_restore(flags); |
| 1325 | |
| 1326 | offset += length; |
| 1327 | page++; |
| 1328 | } |
| 1329 | } |
| 1330 | |
| 1331 | /* |
Alex Elder | f7760da | 2012-10-20 22:17:27 -0500 | [diff] [blame] | 1332 | * Clone a portion of a bio, starting at the given byte offset |
| 1333 | * and continuing for the number of bytes indicated. |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1334 | */ |
Alex Elder | f7760da | 2012-10-20 22:17:27 -0500 | [diff] [blame] | 1335 | static struct bio *bio_clone_range(struct bio *bio_src, |
| 1336 | unsigned int offset, |
| 1337 | unsigned int len, |
| 1338 | gfp_t gfpmask) |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1339 | { |
Alex Elder | f7760da | 2012-10-20 22:17:27 -0500 | [diff] [blame] | 1340 | struct bio *bio; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1341 | |
NeilBrown | f856dc3 | 2017-06-18 14:38:58 +1000 | [diff] [blame] | 1342 | bio = bio_clone_fast(bio_src, gfpmask, rbd_bio_clone); |
Alex Elder | f7760da | 2012-10-20 22:17:27 -0500 | [diff] [blame] | 1343 | if (!bio) |
| 1344 | return NULL; /* ENOMEM */ |
| 1345 | |
Kent Overstreet | 5341a627 | 2013-08-07 14:31:11 -0700 | [diff] [blame] | 1346 | bio_advance(bio, offset); |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 1347 | bio->bi_iter.bi_size = len; |
Alex Elder | 542582f | 2012-08-09 10:33:25 -0700 | [diff] [blame] | 1348 | |
Alex Elder | f7760da | 2012-10-20 22:17:27 -0500 | [diff] [blame] | 1349 | return bio; |
| 1350 | } |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1351 | |
Alex Elder | f7760da | 2012-10-20 22:17:27 -0500 | [diff] [blame] | 1352 | /* |
| 1353 | * Clone a portion of a bio chain, starting at the given byte offset |
| 1354 | * into the first bio in the source chain and continuing for the |
| 1355 | * number of bytes indicated. The result is another bio chain of |
| 1356 | * exactly the given length, or a null pointer on error. |
| 1357 | * |
| 1358 | * The bio_src and offset parameters are both in-out. On entry they |
| 1359 | * refer to the first source bio and the offset into that bio where |
| 1360 | * the start of data to be cloned is located. |
| 1361 | * |
| 1362 | * On return, bio_src is updated to refer to the bio in the source |
| 1363 | * chain that contains first un-cloned byte, and *offset will |
| 1364 | * contain the offset of that byte within that bio. |
| 1365 | */ |
| 1366 | static struct bio *bio_chain_clone_range(struct bio **bio_src, |
| 1367 | unsigned int *offset, |
| 1368 | unsigned int len, |
| 1369 | gfp_t gfpmask) |
| 1370 | { |
| 1371 | struct bio *bi = *bio_src; |
| 1372 | unsigned int off = *offset; |
| 1373 | struct bio *chain = NULL; |
| 1374 | struct bio **end; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1375 | |
Alex Elder | f7760da | 2012-10-20 22:17:27 -0500 | [diff] [blame] | 1376 | /* Build up a chain of clone bios up to the limit */ |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1377 | |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 1378 | if (!bi || off >= bi->bi_iter.bi_size || !len) |
Alex Elder | f7760da | 2012-10-20 22:17:27 -0500 | [diff] [blame] | 1379 | return NULL; /* Nothing to clone */ |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1380 | |
Alex Elder | f7760da | 2012-10-20 22:17:27 -0500 | [diff] [blame] | 1381 | end = &chain; |
| 1382 | while (len) { |
| 1383 | unsigned int bi_size; |
| 1384 | struct bio *bio; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1385 | |
Alex Elder | f5400b7 | 2012-11-01 10:17:15 -0500 | [diff] [blame] | 1386 | if (!bi) { |
| 1387 | rbd_warn(NULL, "bio_chain exhausted with %u left", len); |
Alex Elder | f7760da | 2012-10-20 22:17:27 -0500 | [diff] [blame] | 1388 | goto out_err; /* EINVAL; ran out of bio's */ |
Alex Elder | f5400b7 | 2012-11-01 10:17:15 -0500 | [diff] [blame] | 1389 | } |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 1390 | 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] | 1391 | bio = bio_clone_range(bi, off, bi_size, gfpmask); |
| 1392 | if (!bio) |
| 1393 | goto out_err; /* ENOMEM */ |
| 1394 | |
| 1395 | *end = bio; |
| 1396 | end = &bio->bi_next; |
| 1397 | |
| 1398 | off += bi_size; |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 1399 | if (off == bi->bi_iter.bi_size) { |
Alex Elder | f7760da | 2012-10-20 22:17:27 -0500 | [diff] [blame] | 1400 | bi = bi->bi_next; |
| 1401 | off = 0; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1402 | } |
Alex Elder | f7760da | 2012-10-20 22:17:27 -0500 | [diff] [blame] | 1403 | len -= bi_size; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1404 | } |
Alex Elder | f7760da | 2012-10-20 22:17:27 -0500 | [diff] [blame] | 1405 | *bio_src = bi; |
| 1406 | *offset = off; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1407 | |
Alex Elder | f7760da | 2012-10-20 22:17:27 -0500 | [diff] [blame] | 1408 | return chain; |
| 1409 | out_err: |
| 1410 | bio_chain_put(chain); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1411 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1412 | return NULL; |
| 1413 | } |
| 1414 | |
Alex Elder | 926f9b3 | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 1415 | /* |
| 1416 | * The default/initial value for all object request flags is 0. For |
| 1417 | * each flag, once its value is set to 1 it is never reset to 0 |
| 1418 | * again. |
| 1419 | */ |
Alex Elder | 6365d33 | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 1420 | static void obj_request_img_data_set(struct rbd_obj_request *obj_request) |
| 1421 | { |
| 1422 | if (test_and_set_bit(OBJ_REQ_IMG_DATA, &obj_request->flags)) { |
Alex Elder | 6365d33 | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 1423 | struct rbd_device *rbd_dev; |
| 1424 | |
Alex Elder | 57acbaa | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 1425 | rbd_dev = obj_request->img_request->rbd_dev; |
Ilya Dryomov | 9584d50 | 2014-07-11 12:11:20 +0400 | [diff] [blame] | 1426 | rbd_warn(rbd_dev, "obj_request %p already marked img_data", |
Alex Elder | 6365d33 | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 1427 | obj_request); |
| 1428 | } |
| 1429 | } |
| 1430 | |
| 1431 | static bool obj_request_img_data_test(struct rbd_obj_request *obj_request) |
| 1432 | { |
| 1433 | smp_mb(); |
| 1434 | return test_bit(OBJ_REQ_IMG_DATA, &obj_request->flags) != 0; |
| 1435 | } |
| 1436 | |
Alex Elder | 57acbaa | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 1437 | static void obj_request_done_set(struct rbd_obj_request *obj_request) |
| 1438 | { |
| 1439 | if (test_and_set_bit(OBJ_REQ_DONE, &obj_request->flags)) { |
| 1440 | struct rbd_device *rbd_dev = NULL; |
| 1441 | |
| 1442 | if (obj_request_img_data_test(obj_request)) |
| 1443 | rbd_dev = obj_request->img_request->rbd_dev; |
Ilya Dryomov | 9584d50 | 2014-07-11 12:11:20 +0400 | [diff] [blame] | 1444 | rbd_warn(rbd_dev, "obj_request %p already marked done", |
Alex Elder | 57acbaa | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 1445 | obj_request); |
| 1446 | } |
| 1447 | } |
| 1448 | |
| 1449 | static bool obj_request_done_test(struct rbd_obj_request *obj_request) |
| 1450 | { |
| 1451 | smp_mb(); |
| 1452 | return test_bit(OBJ_REQ_DONE, &obj_request->flags) != 0; |
| 1453 | } |
| 1454 | |
Alex Elder | 5679c59 | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 1455 | /* |
| 1456 | * This sets the KNOWN flag after (possibly) setting the EXISTS |
| 1457 | * flag. The latter is set based on the "exists" value provided. |
| 1458 | * |
| 1459 | * Note that for our purposes once an object exists it never goes |
| 1460 | * away again. It's possible that the response from two existence |
| 1461 | * checks are separated by the creation of the target object, and |
| 1462 | * the first ("doesn't exist") response arrives *after* the second |
| 1463 | * ("does exist"). In that case we ignore the second one. |
| 1464 | */ |
| 1465 | static void obj_request_existence_set(struct rbd_obj_request *obj_request, |
| 1466 | bool exists) |
| 1467 | { |
| 1468 | if (exists) |
| 1469 | set_bit(OBJ_REQ_EXISTS, &obj_request->flags); |
| 1470 | set_bit(OBJ_REQ_KNOWN, &obj_request->flags); |
| 1471 | smp_mb(); |
| 1472 | } |
| 1473 | |
| 1474 | static bool obj_request_known_test(struct rbd_obj_request *obj_request) |
| 1475 | { |
| 1476 | smp_mb(); |
| 1477 | return test_bit(OBJ_REQ_KNOWN, &obj_request->flags) != 0; |
| 1478 | } |
| 1479 | |
| 1480 | static bool obj_request_exists_test(struct rbd_obj_request *obj_request) |
| 1481 | { |
| 1482 | smp_mb(); |
| 1483 | return test_bit(OBJ_REQ_EXISTS, &obj_request->flags) != 0; |
| 1484 | } |
| 1485 | |
Ilya Dryomov | 9638556 | 2014-06-10 13:53:29 +0400 | [diff] [blame] | 1486 | static bool obj_request_overlaps_parent(struct rbd_obj_request *obj_request) |
| 1487 | { |
| 1488 | struct rbd_device *rbd_dev = obj_request->img_request->rbd_dev; |
| 1489 | |
| 1490 | return obj_request->img_offset < |
| 1491 | round_up(rbd_dev->parent_overlap, rbd_obj_bytes(&rbd_dev->header)); |
| 1492 | } |
| 1493 | |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1494 | static void rbd_obj_request_get(struct rbd_obj_request *obj_request) |
| 1495 | { |
Alex Elder | 37206ee | 2013-02-20 17:32:08 -0600 | [diff] [blame] | 1496 | dout("%s: obj %p (was %d)\n", __func__, obj_request, |
Peter Zijlstra | 2c935bc | 2016-11-14 17:29:48 +0100 | [diff] [blame] | 1497 | kref_read(&obj_request->kref)); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1498 | kref_get(&obj_request->kref); |
| 1499 | } |
| 1500 | |
| 1501 | static void rbd_obj_request_destroy(struct kref *kref); |
| 1502 | static void rbd_obj_request_put(struct rbd_obj_request *obj_request) |
| 1503 | { |
| 1504 | rbd_assert(obj_request != NULL); |
Alex Elder | 37206ee | 2013-02-20 17:32:08 -0600 | [diff] [blame] | 1505 | dout("%s: obj %p (was %d)\n", __func__, obj_request, |
Peter Zijlstra | 2c935bc | 2016-11-14 17:29:48 +0100 | [diff] [blame] | 1506 | kref_read(&obj_request->kref)); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1507 | kref_put(&obj_request->kref, rbd_obj_request_destroy); |
| 1508 | } |
| 1509 | |
Alex Elder | 0f2d5be | 2014-04-26 14:21:44 +0400 | [diff] [blame] | 1510 | static void rbd_img_request_get(struct rbd_img_request *img_request) |
| 1511 | { |
| 1512 | dout("%s: img %p (was %d)\n", __func__, img_request, |
Peter Zijlstra | 2c935bc | 2016-11-14 17:29:48 +0100 | [diff] [blame] | 1513 | kref_read(&img_request->kref)); |
Alex Elder | 0f2d5be | 2014-04-26 14:21:44 +0400 | [diff] [blame] | 1514 | kref_get(&img_request->kref); |
| 1515 | } |
| 1516 | |
Alex Elder | e93f315 | 2013-05-08 22:50:04 -0500 | [diff] [blame] | 1517 | static bool img_request_child_test(struct rbd_img_request *img_request); |
| 1518 | static void rbd_parent_request_destroy(struct kref *kref); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1519 | static void rbd_img_request_destroy(struct kref *kref); |
| 1520 | static void rbd_img_request_put(struct rbd_img_request *img_request) |
| 1521 | { |
| 1522 | rbd_assert(img_request != NULL); |
Alex Elder | 37206ee | 2013-02-20 17:32:08 -0600 | [diff] [blame] | 1523 | dout("%s: img %p (was %d)\n", __func__, img_request, |
Peter Zijlstra | 2c935bc | 2016-11-14 17:29:48 +0100 | [diff] [blame] | 1524 | kref_read(&img_request->kref)); |
Alex Elder | e93f315 | 2013-05-08 22:50:04 -0500 | [diff] [blame] | 1525 | if (img_request_child_test(img_request)) |
| 1526 | kref_put(&img_request->kref, rbd_parent_request_destroy); |
| 1527 | else |
| 1528 | kref_put(&img_request->kref, rbd_img_request_destroy); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1529 | } |
| 1530 | |
| 1531 | static inline void rbd_img_obj_request_add(struct rbd_img_request *img_request, |
| 1532 | struct rbd_obj_request *obj_request) |
| 1533 | { |
Alex Elder | 25dcf95 | 2013-01-25 17:08:55 -0600 | [diff] [blame] | 1534 | rbd_assert(obj_request->img_request == NULL); |
| 1535 | |
Alex Elder | b155e86 | 2013-04-15 14:50:37 -0500 | [diff] [blame] | 1536 | /* Image request now owns object's original reference */ |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1537 | obj_request->img_request = img_request; |
Alex Elder | 25dcf95 | 2013-01-25 17:08:55 -0600 | [diff] [blame] | 1538 | obj_request->which = img_request->obj_request_count; |
Alex Elder | 6365d33 | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 1539 | rbd_assert(!obj_request_img_data_test(obj_request)); |
| 1540 | obj_request_img_data_set(obj_request); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1541 | rbd_assert(obj_request->which != BAD_WHICH); |
Alex Elder | 25dcf95 | 2013-01-25 17:08:55 -0600 | [diff] [blame] | 1542 | img_request->obj_request_count++; |
| 1543 | list_add_tail(&obj_request->links, &img_request->obj_requests); |
Alex Elder | 37206ee | 2013-02-20 17:32:08 -0600 | [diff] [blame] | 1544 | dout("%s: img %p obj %p w=%u\n", __func__, img_request, obj_request, |
| 1545 | obj_request->which); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1546 | } |
| 1547 | |
| 1548 | static inline void rbd_img_obj_request_del(struct rbd_img_request *img_request, |
| 1549 | struct rbd_obj_request *obj_request) |
| 1550 | { |
| 1551 | rbd_assert(obj_request->which != BAD_WHICH); |
Alex Elder | 25dcf95 | 2013-01-25 17:08:55 -0600 | [diff] [blame] | 1552 | |
Alex Elder | 37206ee | 2013-02-20 17:32:08 -0600 | [diff] [blame] | 1553 | dout("%s: img %p obj %p w=%u\n", __func__, img_request, obj_request, |
| 1554 | obj_request->which); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1555 | list_del(&obj_request->links); |
Alex Elder | 25dcf95 | 2013-01-25 17:08:55 -0600 | [diff] [blame] | 1556 | rbd_assert(img_request->obj_request_count > 0); |
| 1557 | img_request->obj_request_count--; |
| 1558 | rbd_assert(obj_request->which == img_request->obj_request_count); |
| 1559 | obj_request->which = BAD_WHICH; |
Alex Elder | 6365d33 | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 1560 | rbd_assert(obj_request_img_data_test(obj_request)); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1561 | rbd_assert(obj_request->img_request == img_request); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1562 | obj_request->img_request = NULL; |
Alex Elder | 25dcf95 | 2013-01-25 17:08:55 -0600 | [diff] [blame] | 1563 | obj_request->callback = NULL; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1564 | rbd_obj_request_put(obj_request); |
| 1565 | } |
| 1566 | |
| 1567 | static bool obj_request_type_valid(enum obj_request_type type) |
| 1568 | { |
| 1569 | switch (type) { |
Alex Elder | 9969ebc | 2013-01-18 12:31:10 -0600 | [diff] [blame] | 1570 | case OBJ_REQUEST_NODATA: |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1571 | case OBJ_REQUEST_BIO: |
Alex Elder | 788e2df | 2013-01-17 12:25:27 -0600 | [diff] [blame] | 1572 | case OBJ_REQUEST_PAGES: |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1573 | return true; |
| 1574 | default: |
| 1575 | return false; |
| 1576 | } |
| 1577 | } |
| 1578 | |
Ilya Dryomov | 4a17dad | 2016-09-13 21:08:10 +0200 | [diff] [blame] | 1579 | static void rbd_img_obj_callback(struct rbd_obj_request *obj_request); |
| 1580 | |
Ilya Dryomov | 980917f | 2016-09-12 18:59:42 +0200 | [diff] [blame] | 1581 | static void rbd_obj_request_submit(struct rbd_obj_request *obj_request) |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1582 | { |
Ilya Dryomov | 980917f | 2016-09-12 18:59:42 +0200 | [diff] [blame] | 1583 | struct ceph_osd_request *osd_req = obj_request->osd_req; |
| 1584 | |
Ilya Dryomov | a90bb0c | 2017-01-25 18:16:23 +0100 | [diff] [blame] | 1585 | dout("%s %p object_no %016llx %llu~%llu osd_req %p\n", __func__, |
| 1586 | obj_request, obj_request->object_no, obj_request->offset, |
Ilya Dryomov | 67e2b65 | 2017-01-25 18:16:22 +0100 | [diff] [blame] | 1587 | obj_request->length, osd_req); |
Ilya Dryomov | 4a17dad | 2016-09-13 21:08:10 +0200 | [diff] [blame] | 1588 | if (obj_request_img_data_test(obj_request)) { |
| 1589 | WARN_ON(obj_request->callback != rbd_img_obj_callback); |
| 1590 | rbd_img_request_get(obj_request->img_request); |
| 1591 | } |
Ilya Dryomov | 980917f | 2016-09-12 18:59:42 +0200 | [diff] [blame] | 1592 | ceph_osdc_start_request(osd_req->r_osdc, osd_req, false); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1593 | } |
| 1594 | |
| 1595 | static void rbd_img_request_complete(struct rbd_img_request *img_request) |
| 1596 | { |
Alex Elder | 55f27e0 | 2013-04-10 12:34:25 -0500 | [diff] [blame] | 1597 | |
Alex Elder | 37206ee | 2013-02-20 17:32:08 -0600 | [diff] [blame] | 1598 | dout("%s: img %p\n", __func__, img_request); |
Alex Elder | 55f27e0 | 2013-04-10 12:34:25 -0500 | [diff] [blame] | 1599 | |
| 1600 | /* |
| 1601 | * If no error occurred, compute the aggregate transfer |
| 1602 | * count for the image request. We could instead use |
| 1603 | * atomic64_cmpxchg() to update it as each object request |
| 1604 | * completes; not clear which way is better off hand. |
| 1605 | */ |
| 1606 | if (!img_request->result) { |
| 1607 | struct rbd_obj_request *obj_request; |
| 1608 | u64 xferred = 0; |
| 1609 | |
| 1610 | for_each_obj_request(img_request, obj_request) |
| 1611 | xferred += obj_request->xferred; |
| 1612 | img_request->xferred = xferred; |
| 1613 | } |
| 1614 | |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1615 | if (img_request->callback) |
| 1616 | img_request->callback(img_request); |
| 1617 | else |
| 1618 | rbd_img_request_put(img_request); |
| 1619 | } |
| 1620 | |
Alex Elder | 0c42524 | 2013-02-08 09:55:49 -0600 | [diff] [blame] | 1621 | /* |
| 1622 | * The default/initial value for all image request flags is 0. Each |
| 1623 | * is conditionally set to 1 at image request initialization time |
| 1624 | * and currently never change thereafter. |
| 1625 | */ |
| 1626 | static void img_request_write_set(struct rbd_img_request *img_request) |
| 1627 | { |
| 1628 | set_bit(IMG_REQ_WRITE, &img_request->flags); |
| 1629 | smp_mb(); |
| 1630 | } |
| 1631 | |
| 1632 | static bool img_request_write_test(struct rbd_img_request *img_request) |
| 1633 | { |
| 1634 | smp_mb(); |
| 1635 | return test_bit(IMG_REQ_WRITE, &img_request->flags) != 0; |
| 1636 | } |
| 1637 | |
Guangliang Zhao | 90e98c5 | 2014-04-01 22:22:16 +0800 | [diff] [blame] | 1638 | /* |
| 1639 | * Set the discard flag when the img_request is an discard request |
| 1640 | */ |
| 1641 | static void img_request_discard_set(struct rbd_img_request *img_request) |
| 1642 | { |
| 1643 | set_bit(IMG_REQ_DISCARD, &img_request->flags); |
| 1644 | smp_mb(); |
| 1645 | } |
| 1646 | |
| 1647 | static bool img_request_discard_test(struct rbd_img_request *img_request) |
| 1648 | { |
| 1649 | smp_mb(); |
| 1650 | return test_bit(IMG_REQ_DISCARD, &img_request->flags) != 0; |
| 1651 | } |
| 1652 | |
Alex Elder | 9849e98 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 1653 | static void img_request_child_set(struct rbd_img_request *img_request) |
| 1654 | { |
| 1655 | set_bit(IMG_REQ_CHILD, &img_request->flags); |
| 1656 | smp_mb(); |
| 1657 | } |
| 1658 | |
Alex Elder | e93f315 | 2013-05-08 22:50:04 -0500 | [diff] [blame] | 1659 | static void img_request_child_clear(struct rbd_img_request *img_request) |
| 1660 | { |
| 1661 | clear_bit(IMG_REQ_CHILD, &img_request->flags); |
| 1662 | smp_mb(); |
| 1663 | } |
| 1664 | |
Alex Elder | 9849e98 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 1665 | static bool img_request_child_test(struct rbd_img_request *img_request) |
| 1666 | { |
| 1667 | smp_mb(); |
| 1668 | return test_bit(IMG_REQ_CHILD, &img_request->flags) != 0; |
| 1669 | } |
| 1670 | |
Alex Elder | d0b2e94 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 1671 | static void img_request_layered_set(struct rbd_img_request *img_request) |
| 1672 | { |
| 1673 | set_bit(IMG_REQ_LAYERED, &img_request->flags); |
| 1674 | smp_mb(); |
| 1675 | } |
| 1676 | |
Alex Elder | a2acd00 | 2013-05-08 22:50:04 -0500 | [diff] [blame] | 1677 | static void img_request_layered_clear(struct rbd_img_request *img_request) |
| 1678 | { |
| 1679 | clear_bit(IMG_REQ_LAYERED, &img_request->flags); |
| 1680 | smp_mb(); |
| 1681 | } |
| 1682 | |
Alex Elder | d0b2e94 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 1683 | static bool img_request_layered_test(struct rbd_img_request *img_request) |
| 1684 | { |
| 1685 | smp_mb(); |
| 1686 | return test_bit(IMG_REQ_LAYERED, &img_request->flags) != 0; |
| 1687 | } |
| 1688 | |
Josh Durgin | 3b434a2a | 2014-04-04 17:32:15 -0700 | [diff] [blame] | 1689 | static enum obj_operation_type |
| 1690 | rbd_img_request_op_type(struct rbd_img_request *img_request) |
| 1691 | { |
| 1692 | if (img_request_write_test(img_request)) |
| 1693 | return OBJ_OP_WRITE; |
| 1694 | else if (img_request_discard_test(img_request)) |
| 1695 | return OBJ_OP_DISCARD; |
| 1696 | else |
| 1697 | return OBJ_OP_READ; |
| 1698 | } |
| 1699 | |
Alex Elder | 6e2a450 | 2013-03-27 09:16:30 -0500 | [diff] [blame] | 1700 | static void |
| 1701 | rbd_img_obj_request_read_callback(struct rbd_obj_request *obj_request) |
| 1702 | { |
Alex Elder | b9434c5 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 1703 | u64 xferred = obj_request->xferred; |
| 1704 | u64 length = obj_request->length; |
| 1705 | |
Alex Elder | 6e2a450 | 2013-03-27 09:16:30 -0500 | [diff] [blame] | 1706 | dout("%s: obj %p img %p result %d %llu/%llu\n", __func__, |
| 1707 | obj_request, obj_request->img_request, obj_request->result, |
Alex Elder | b9434c5 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 1708 | xferred, length); |
Alex Elder | 6e2a450 | 2013-03-27 09:16:30 -0500 | [diff] [blame] | 1709 | /* |
Josh Durgin | 17c1cc1 | 2013-08-26 17:55:38 -0700 | [diff] [blame] | 1710 | * ENOENT means a hole in the image. We zero-fill the entire |
| 1711 | * length of the request. A short read also implies zero-fill |
| 1712 | * to the end of the request. An error requires the whole |
| 1713 | * length of the request to be reported finished with an error |
| 1714 | * to the block layer. In each case we update the xferred |
| 1715 | * count to indicate the whole request was satisfied. |
Alex Elder | 6e2a450 | 2013-03-27 09:16:30 -0500 | [diff] [blame] | 1716 | */ |
Alex Elder | b9434c5 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 1717 | rbd_assert(obj_request->type != OBJ_REQUEST_NODATA); |
Alex Elder | 6e2a450 | 2013-03-27 09:16:30 -0500 | [diff] [blame] | 1718 | if (obj_request->result == -ENOENT) { |
Alex Elder | b9434c5 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 1719 | if (obj_request->type == OBJ_REQUEST_BIO) |
| 1720 | zero_bio_chain(obj_request->bio_list, 0); |
| 1721 | else |
| 1722 | zero_pages(obj_request->pages, 0, length); |
Alex Elder | 6e2a450 | 2013-03-27 09:16:30 -0500 | [diff] [blame] | 1723 | obj_request->result = 0; |
Alex Elder | b9434c5 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 1724 | } else if (xferred < length && !obj_request->result) { |
| 1725 | if (obj_request->type == OBJ_REQUEST_BIO) |
| 1726 | zero_bio_chain(obj_request->bio_list, xferred); |
| 1727 | else |
| 1728 | zero_pages(obj_request->pages, xferred, length); |
Alex Elder | 6e2a450 | 2013-03-27 09:16:30 -0500 | [diff] [blame] | 1729 | } |
Josh Durgin | 17c1cc1 | 2013-08-26 17:55:38 -0700 | [diff] [blame] | 1730 | obj_request->xferred = length; |
Alex Elder | 6e2a450 | 2013-03-27 09:16:30 -0500 | [diff] [blame] | 1731 | obj_request_done_set(obj_request); |
| 1732 | } |
| 1733 | |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1734 | static void rbd_obj_request_complete(struct rbd_obj_request *obj_request) |
| 1735 | { |
Alex Elder | 37206ee | 2013-02-20 17:32:08 -0600 | [diff] [blame] | 1736 | dout("%s: obj %p cb %p\n", __func__, obj_request, |
| 1737 | obj_request->callback); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1738 | if (obj_request->callback) |
| 1739 | obj_request->callback(obj_request); |
Alex Elder | 788e2df | 2013-01-17 12:25:27 -0600 | [diff] [blame] | 1740 | else |
| 1741 | complete_all(&obj_request->completion); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1742 | } |
| 1743 | |
Ilya Dryomov | 0dcc685 | 2016-09-26 15:43:52 +0200 | [diff] [blame] | 1744 | static void rbd_obj_request_error(struct rbd_obj_request *obj_request, int err) |
| 1745 | { |
| 1746 | obj_request->result = err; |
| 1747 | obj_request->xferred = 0; |
| 1748 | /* |
| 1749 | * kludge - mirror rbd_obj_request_submit() to match a put in |
| 1750 | * rbd_img_obj_callback() |
| 1751 | */ |
| 1752 | if (obj_request_img_data_test(obj_request)) { |
| 1753 | WARN_ON(obj_request->callback != rbd_img_obj_callback); |
| 1754 | rbd_img_request_get(obj_request->img_request); |
| 1755 | } |
| 1756 | obj_request_done_set(obj_request); |
| 1757 | rbd_obj_request_complete(obj_request); |
| 1758 | } |
| 1759 | |
Alex Elder | c47f937 | 2013-02-26 14:23:07 -0600 | [diff] [blame] | 1760 | static void rbd_osd_read_callback(struct rbd_obj_request *obj_request) |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1761 | { |
Alex Elder | 57acbaa | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 1762 | struct rbd_img_request *img_request = NULL; |
Alex Elder | a9e8ba2c | 2013-04-21 00:32:07 -0500 | [diff] [blame] | 1763 | struct rbd_device *rbd_dev = NULL; |
Alex Elder | 57acbaa | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 1764 | bool layered = false; |
| 1765 | |
| 1766 | if (obj_request_img_data_test(obj_request)) { |
| 1767 | img_request = obj_request->img_request; |
| 1768 | layered = img_request && img_request_layered_test(img_request); |
Alex Elder | a9e8ba2c | 2013-04-21 00:32:07 -0500 | [diff] [blame] | 1769 | rbd_dev = img_request->rbd_dev; |
Alex Elder | 57acbaa | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 1770 | } |
Alex Elder | 8b3e1a5 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 1771 | |
| 1772 | dout("%s: obj %p img %p result %d %llu/%llu\n", __func__, |
| 1773 | obj_request, img_request, obj_request->result, |
| 1774 | obj_request->xferred, obj_request->length); |
Alex Elder | a9e8ba2c | 2013-04-21 00:32:07 -0500 | [diff] [blame] | 1775 | if (layered && obj_request->result == -ENOENT && |
| 1776 | obj_request->img_offset < rbd_dev->parent_overlap) |
Alex Elder | 8b3e1a5 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 1777 | rbd_img_parent_read(obj_request); |
| 1778 | else if (img_request) |
Alex Elder | 6e2a450 | 2013-03-27 09:16:30 -0500 | [diff] [blame] | 1779 | rbd_img_obj_request_read_callback(obj_request); |
| 1780 | else |
| 1781 | obj_request_done_set(obj_request); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1782 | } |
| 1783 | |
Alex Elder | c47f937 | 2013-02-26 14:23:07 -0600 | [diff] [blame] | 1784 | static void rbd_osd_write_callback(struct rbd_obj_request *obj_request) |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1785 | { |
Sage Weil | 1b83bef | 2013-02-25 16:11:12 -0800 | [diff] [blame] | 1786 | dout("%s: obj %p result %d %llu\n", __func__, obj_request, |
| 1787 | obj_request->result, obj_request->length); |
| 1788 | /* |
Alex Elder | 8b3e1a5 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 1789 | * There is no such thing as a successful short write. Set |
| 1790 | * it to our originally-requested length. |
Sage Weil | 1b83bef | 2013-02-25 16:11:12 -0800 | [diff] [blame] | 1791 | */ |
| 1792 | obj_request->xferred = obj_request->length; |
Alex Elder | 0774130 | 2013-02-05 23:41:50 -0600 | [diff] [blame] | 1793 | obj_request_done_set(obj_request); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1794 | } |
| 1795 | |
Guangliang Zhao | 90e98c5 | 2014-04-01 22:22:16 +0800 | [diff] [blame] | 1796 | static void rbd_osd_discard_callback(struct rbd_obj_request *obj_request) |
| 1797 | { |
| 1798 | dout("%s: obj %p result %d %llu\n", __func__, obj_request, |
| 1799 | obj_request->result, obj_request->length); |
| 1800 | /* |
| 1801 | * There is no such thing as a successful short discard. Set |
| 1802 | * it to our originally-requested length. |
| 1803 | */ |
| 1804 | obj_request->xferred = obj_request->length; |
Josh Durgin | d0265de | 2014-04-07 16:54:10 -0700 | [diff] [blame] | 1805 | /* discarding a non-existent object is not a problem */ |
| 1806 | if (obj_request->result == -ENOENT) |
| 1807 | obj_request->result = 0; |
Guangliang Zhao | 90e98c5 | 2014-04-01 22:22:16 +0800 | [diff] [blame] | 1808 | obj_request_done_set(obj_request); |
| 1809 | } |
| 1810 | |
Alex Elder | fbfab53 | 2013-02-08 09:55:48 -0600 | [diff] [blame] | 1811 | /* |
| 1812 | * For a simple stat call there's nothing to do. We'll do more if |
| 1813 | * this is part of a write sequence for a layered image. |
| 1814 | */ |
Alex Elder | c47f937 | 2013-02-26 14:23:07 -0600 | [diff] [blame] | 1815 | static void rbd_osd_stat_callback(struct rbd_obj_request *obj_request) |
Alex Elder | fbfab53 | 2013-02-08 09:55:48 -0600 | [diff] [blame] | 1816 | { |
Alex Elder | 37206ee | 2013-02-20 17:32:08 -0600 | [diff] [blame] | 1817 | dout("%s: obj %p\n", __func__, obj_request); |
Alex Elder | fbfab53 | 2013-02-08 09:55:48 -0600 | [diff] [blame] | 1818 | obj_request_done_set(obj_request); |
| 1819 | } |
| 1820 | |
Ilya Dryomov | 2761713 | 2015-07-16 17:36:11 +0300 | [diff] [blame] | 1821 | static void rbd_osd_call_callback(struct rbd_obj_request *obj_request) |
| 1822 | { |
| 1823 | dout("%s: obj %p\n", __func__, obj_request); |
| 1824 | |
| 1825 | if (obj_request_img_data_test(obj_request)) |
| 1826 | rbd_osd_copyup_callback(obj_request); |
| 1827 | else |
| 1828 | obj_request_done_set(obj_request); |
| 1829 | } |
| 1830 | |
Ilya Dryomov | 85e084f | 2016-04-28 16:07:24 +0200 | [diff] [blame] | 1831 | static void rbd_osd_req_callback(struct ceph_osd_request *osd_req) |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1832 | { |
| 1833 | struct rbd_obj_request *obj_request = osd_req->r_priv; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1834 | u16 opcode; |
| 1835 | |
Ilya Dryomov | 85e084f | 2016-04-28 16:07:24 +0200 | [diff] [blame] | 1836 | dout("%s: osd_req %p\n", __func__, osd_req); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1837 | rbd_assert(osd_req == obj_request->osd_req); |
Alex Elder | 57acbaa | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 1838 | if (obj_request_img_data_test(obj_request)) { |
| 1839 | rbd_assert(obj_request->img_request); |
| 1840 | rbd_assert(obj_request->which != BAD_WHICH); |
| 1841 | } else { |
| 1842 | rbd_assert(obj_request->which == BAD_WHICH); |
| 1843 | } |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1844 | |
Sage Weil | 1b83bef | 2013-02-25 16:11:12 -0800 | [diff] [blame] | 1845 | if (osd_req->r_result < 0) |
| 1846 | obj_request->result = osd_req->r_result; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1847 | |
Alex Elder | c47f937 | 2013-02-26 14:23:07 -0600 | [diff] [blame] | 1848 | /* |
| 1849 | * We support a 64-bit length, but ultimately it has to be |
Christoph Hellwig | 7ad18af | 2015-01-13 17:20:04 +0100 | [diff] [blame] | 1850 | * passed to the block layer, which just supports a 32-bit |
| 1851 | * length field. |
Alex Elder | c47f937 | 2013-02-26 14:23:07 -0600 | [diff] [blame] | 1852 | */ |
Yan, Zheng | 7665d85 | 2016-01-07 16:48:57 +0800 | [diff] [blame] | 1853 | obj_request->xferred = osd_req->r_ops[0].outdata_len; |
Alex Elder | 8b3e1a5 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 1854 | rbd_assert(obj_request->xferred < (u64)UINT_MAX); |
Ilya Dryomov | 0ccd592 | 2014-02-25 16:22:28 +0200 | [diff] [blame] | 1855 | |
Alex Elder | 7952873 | 2013-04-03 21:32:51 -0500 | [diff] [blame] | 1856 | opcode = osd_req->r_ops[0].op; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1857 | switch (opcode) { |
| 1858 | case CEPH_OSD_OP_READ: |
Alex Elder | c47f937 | 2013-02-26 14:23:07 -0600 | [diff] [blame] | 1859 | rbd_osd_read_callback(obj_request); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1860 | break; |
Ilya Dryomov | 0ccd592 | 2014-02-25 16:22:28 +0200 | [diff] [blame] | 1861 | case CEPH_OSD_OP_SETALLOCHINT: |
Ilya Dryomov | e30b757 | 2015-10-07 17:27:17 +0200 | [diff] [blame] | 1862 | rbd_assert(osd_req->r_ops[1].op == CEPH_OSD_OP_WRITE || |
| 1863 | osd_req->r_ops[1].op == CEPH_OSD_OP_WRITEFULL); |
Ilya Dryomov | 0ccd592 | 2014-02-25 16:22:28 +0200 | [diff] [blame] | 1864 | /* fall through */ |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1865 | case CEPH_OSD_OP_WRITE: |
Ilya Dryomov | e30b757 | 2015-10-07 17:27:17 +0200 | [diff] [blame] | 1866 | case CEPH_OSD_OP_WRITEFULL: |
Alex Elder | c47f937 | 2013-02-26 14:23:07 -0600 | [diff] [blame] | 1867 | rbd_osd_write_callback(obj_request); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1868 | break; |
Alex Elder | fbfab53 | 2013-02-08 09:55:48 -0600 | [diff] [blame] | 1869 | case CEPH_OSD_OP_STAT: |
Alex Elder | c47f937 | 2013-02-26 14:23:07 -0600 | [diff] [blame] | 1870 | rbd_osd_stat_callback(obj_request); |
Alex Elder | fbfab53 | 2013-02-08 09:55:48 -0600 | [diff] [blame] | 1871 | break; |
Guangliang Zhao | 90e98c5 | 2014-04-01 22:22:16 +0800 | [diff] [blame] | 1872 | case CEPH_OSD_OP_DELETE: |
| 1873 | case CEPH_OSD_OP_TRUNCATE: |
| 1874 | case CEPH_OSD_OP_ZERO: |
| 1875 | rbd_osd_discard_callback(obj_request); |
| 1876 | break; |
Alex Elder | 36be9a7 | 2013-01-19 00:30:28 -0600 | [diff] [blame] | 1877 | case CEPH_OSD_OP_CALL: |
Ilya Dryomov | 2761713 | 2015-07-16 17:36:11 +0300 | [diff] [blame] | 1878 | rbd_osd_call_callback(obj_request); |
| 1879 | break; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1880 | default: |
Ilya Dryomov | a90bb0c | 2017-01-25 18:16:23 +0100 | [diff] [blame] | 1881 | rbd_warn(NULL, "unexpected OSD op: object_no %016llx opcode %d", |
| 1882 | obj_request->object_no, opcode); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1883 | break; |
| 1884 | } |
| 1885 | |
Alex Elder | 0774130 | 2013-02-05 23:41:50 -0600 | [diff] [blame] | 1886 | if (obj_request_done_test(obj_request)) |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1887 | rbd_obj_request_complete(obj_request); |
| 1888 | } |
| 1889 | |
Alex Elder | 9d4df01 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 1890 | 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] | 1891 | { |
Alex Elder | 8c042b0 | 2013-04-03 01:28:58 -0500 | [diff] [blame] | 1892 | struct ceph_osd_request *osd_req = obj_request->osd_req; |
Alex Elder | 430c28c | 2013-04-03 21:32:51 -0500 | [diff] [blame] | 1893 | |
Ilya Dryomov | 7c84883 | 2016-09-15 17:56:39 +0200 | [diff] [blame] | 1894 | rbd_assert(obj_request_img_data_test(obj_request)); |
| 1895 | osd_req->r_snapid = obj_request->img_request->snap_id; |
Alex Elder | 9d4df01 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 1896 | } |
| 1897 | |
| 1898 | static void rbd_osd_req_format_write(struct rbd_obj_request *obj_request) |
| 1899 | { |
Alex Elder | 9d4df01 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 1900 | struct ceph_osd_request *osd_req = obj_request->osd_req; |
Alex Elder | 9d4df01 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 1901 | |
Deepa Dinamani | 1134e09 | 2017-05-08 15:59:19 -0700 | [diff] [blame] | 1902 | ktime_get_real_ts(&osd_req->r_mtime); |
Ilya Dryomov | bb873b5 | 2016-05-26 00:29:52 +0200 | [diff] [blame] | 1903 | osd_req->r_data_offset = obj_request->offset; |
Alex Elder | 430c28c | 2013-04-03 21:32:51 -0500 | [diff] [blame] | 1904 | } |
| 1905 | |
Ilya Dryomov | bc81207 | 2017-01-25 18:16:23 +0100 | [diff] [blame] | 1906 | static struct ceph_osd_request * |
| 1907 | __rbd_osd_req_create(struct rbd_device *rbd_dev, |
| 1908 | struct ceph_snap_context *snapc, |
| 1909 | int num_ops, unsigned int flags, |
| 1910 | struct rbd_obj_request *obj_request) |
| 1911 | { |
| 1912 | struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc; |
| 1913 | struct ceph_osd_request *req; |
Ilya Dryomov | a90bb0c | 2017-01-25 18:16:23 +0100 | [diff] [blame] | 1914 | const char *name_format = rbd_dev->image_format == 1 ? |
| 1915 | RBD_V1_DATA_FORMAT : RBD_V2_DATA_FORMAT; |
Ilya Dryomov | bc81207 | 2017-01-25 18:16:23 +0100 | [diff] [blame] | 1916 | |
| 1917 | req = ceph_osdc_alloc_request(osdc, snapc, num_ops, false, GFP_NOIO); |
| 1918 | if (!req) |
| 1919 | return NULL; |
| 1920 | |
| 1921 | req->r_flags = flags; |
| 1922 | req->r_callback = rbd_osd_req_callback; |
| 1923 | req->r_priv = obj_request; |
| 1924 | |
| 1925 | req->r_base_oloc.pool = rbd_dev->layout.pool_id; |
Ilya Dryomov | a90bb0c | 2017-01-25 18:16:23 +0100 | [diff] [blame] | 1926 | if (ceph_oid_aprintf(&req->r_base_oid, GFP_NOIO, name_format, |
| 1927 | rbd_dev->header.object_prefix, obj_request->object_no)) |
Ilya Dryomov | bc81207 | 2017-01-25 18:16:23 +0100 | [diff] [blame] | 1928 | goto err_req; |
| 1929 | |
| 1930 | if (ceph_osdc_alloc_messages(req, GFP_NOIO)) |
| 1931 | goto err_req; |
| 1932 | |
| 1933 | return req; |
| 1934 | |
| 1935 | err_req: |
| 1936 | ceph_osdc_put_request(req); |
| 1937 | return NULL; |
| 1938 | } |
| 1939 | |
Ilya Dryomov | 0ccd592 | 2014-02-25 16:22:28 +0200 | [diff] [blame] | 1940 | /* |
| 1941 | * Create an osd request. A read request has one osd op (read). |
| 1942 | * A write request has either one (watch) or two (hint+write) osd ops. |
| 1943 | * (All rbd data writes are prefixed with an allocation hint op, but |
| 1944 | * technically osd watch is a write request, hence this distinction.) |
| 1945 | */ |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1946 | static struct ceph_osd_request *rbd_osd_req_create( |
| 1947 | struct rbd_device *rbd_dev, |
Guangliang Zhao | 6d2940c | 2014-03-13 11:21:35 +0800 | [diff] [blame] | 1948 | enum obj_operation_type op_type, |
Ilya Dryomov | deb236b | 2014-02-25 16:22:27 +0200 | [diff] [blame] | 1949 | unsigned int num_ops, |
Alex Elder | 430c28c | 2013-04-03 21:32:51 -0500 | [diff] [blame] | 1950 | struct rbd_obj_request *obj_request) |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1951 | { |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1952 | struct ceph_snap_context *snapc = NULL; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1953 | |
Guangliang Zhao | 90e98c5 | 2014-04-01 22:22:16 +0800 | [diff] [blame] | 1954 | if (obj_request_img_data_test(obj_request) && |
| 1955 | (op_type == OBJ_OP_DISCARD || op_type == OBJ_OP_WRITE)) { |
Alex Elder | 6365d33 | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 1956 | struct rbd_img_request *img_request = obj_request->img_request; |
Guangliang Zhao | 90e98c5 | 2014-04-01 22:22:16 +0800 | [diff] [blame] | 1957 | if (op_type == OBJ_OP_WRITE) { |
| 1958 | rbd_assert(img_request_write_test(img_request)); |
| 1959 | } else { |
| 1960 | rbd_assert(img_request_discard_test(img_request)); |
| 1961 | } |
Guangliang Zhao | 6d2940c | 2014-03-13 11:21:35 +0800 | [diff] [blame] | 1962 | snapc = img_request->snapc; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1963 | } |
| 1964 | |
Guangliang Zhao | 6d2940c | 2014-03-13 11:21:35 +0800 | [diff] [blame] | 1965 | 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] | 1966 | |
Ilya Dryomov | bc81207 | 2017-01-25 18:16:23 +0100 | [diff] [blame] | 1967 | return __rbd_osd_req_create(rbd_dev, snapc, num_ops, |
| 1968 | (op_type == OBJ_OP_WRITE || op_type == OBJ_OP_DISCARD) ? |
Ilya Dryomov | 54ea004 | 2017-02-11 18:48:41 +0100 | [diff] [blame] | 1969 | CEPH_OSD_FLAG_WRITE : CEPH_OSD_FLAG_READ, obj_request); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1970 | } |
| 1971 | |
Alex Elder | 0eefd47 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 1972 | /* |
Josh Durgin | d3246fb | 2014-04-07 16:49:21 -0700 | [diff] [blame] | 1973 | * Create a copyup osd request based on the information in the object |
| 1974 | * request supplied. A copyup request has two or three osd ops, a |
| 1975 | * copyup method call, potentially a hint op, and a write or truncate |
| 1976 | * or zero op. |
Alex Elder | 0eefd47 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 1977 | */ |
| 1978 | static struct ceph_osd_request * |
| 1979 | rbd_osd_req_create_copyup(struct rbd_obj_request *obj_request) |
| 1980 | { |
| 1981 | struct rbd_img_request *img_request; |
Josh Durgin | d3246fb | 2014-04-07 16:49:21 -0700 | [diff] [blame] | 1982 | int num_osd_ops = 3; |
Alex Elder | 0eefd47 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 1983 | |
| 1984 | rbd_assert(obj_request_img_data_test(obj_request)); |
| 1985 | img_request = obj_request->img_request; |
| 1986 | rbd_assert(img_request); |
Josh Durgin | d3246fb | 2014-04-07 16:49:21 -0700 | [diff] [blame] | 1987 | rbd_assert(img_request_write_test(img_request) || |
| 1988 | img_request_discard_test(img_request)); |
Alex Elder | 0eefd47 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 1989 | |
Josh Durgin | d3246fb | 2014-04-07 16:49:21 -0700 | [diff] [blame] | 1990 | if (img_request_discard_test(img_request)) |
| 1991 | num_osd_ops = 2; |
| 1992 | |
Ilya Dryomov | bc81207 | 2017-01-25 18:16:23 +0100 | [diff] [blame] | 1993 | return __rbd_osd_req_create(img_request->rbd_dev, |
| 1994 | img_request->snapc, num_osd_ops, |
Ilya Dryomov | 54ea004 | 2017-02-11 18:48:41 +0100 | [diff] [blame] | 1995 | CEPH_OSD_FLAG_WRITE, obj_request); |
Alex Elder | 0eefd47 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 1996 | } |
| 1997 | |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1998 | static void rbd_osd_req_destroy(struct ceph_osd_request *osd_req) |
| 1999 | { |
| 2000 | ceph_osdc_put_request(osd_req); |
| 2001 | } |
| 2002 | |
Ilya Dryomov | 6c696d8 | 2017-01-25 18:16:23 +0100 | [diff] [blame] | 2003 | static struct rbd_obj_request * |
| 2004 | rbd_obj_request_create(enum obj_request_type type) |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2005 | { |
| 2006 | struct rbd_obj_request *obj_request; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2007 | |
| 2008 | rbd_assert(obj_request_type_valid(type)); |
| 2009 | |
Ilya Dryomov | 5a60e87 | 2015-06-24 17:24:33 +0300 | [diff] [blame] | 2010 | obj_request = kmem_cache_zalloc(rbd_obj_request_cache, GFP_NOIO); |
Ilya Dryomov | 6c696d8 | 2017-01-25 18:16:23 +0100 | [diff] [blame] | 2011 | if (!obj_request) |
Alex Elder | f907ad5 | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 2012 | return NULL; |
Alex Elder | f907ad5 | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 2013 | |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2014 | obj_request->which = BAD_WHICH; |
| 2015 | obj_request->type = type; |
| 2016 | INIT_LIST_HEAD(&obj_request->links); |
Alex Elder | 788e2df | 2013-01-17 12:25:27 -0600 | [diff] [blame] | 2017 | init_completion(&obj_request->completion); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2018 | kref_init(&obj_request->kref); |
| 2019 | |
Ilya Dryomov | 67e2b65 | 2017-01-25 18:16:22 +0100 | [diff] [blame] | 2020 | dout("%s %p\n", __func__, obj_request); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2021 | return obj_request; |
| 2022 | } |
| 2023 | |
| 2024 | static void rbd_obj_request_destroy(struct kref *kref) |
| 2025 | { |
| 2026 | struct rbd_obj_request *obj_request; |
| 2027 | |
| 2028 | obj_request = container_of(kref, struct rbd_obj_request, kref); |
| 2029 | |
Alex Elder | 37206ee | 2013-02-20 17:32:08 -0600 | [diff] [blame] | 2030 | dout("%s: obj %p\n", __func__, obj_request); |
| 2031 | |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2032 | rbd_assert(obj_request->img_request == NULL); |
| 2033 | rbd_assert(obj_request->which == BAD_WHICH); |
| 2034 | |
| 2035 | if (obj_request->osd_req) |
| 2036 | rbd_osd_req_destroy(obj_request->osd_req); |
| 2037 | |
| 2038 | rbd_assert(obj_request_type_valid(obj_request->type)); |
| 2039 | switch (obj_request->type) { |
Alex Elder | 9969ebc | 2013-01-18 12:31:10 -0600 | [diff] [blame] | 2040 | case OBJ_REQUEST_NODATA: |
| 2041 | break; /* Nothing to do */ |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2042 | case OBJ_REQUEST_BIO: |
| 2043 | if (obj_request->bio_list) |
| 2044 | bio_chain_put(obj_request->bio_list); |
| 2045 | break; |
Alex Elder | 788e2df | 2013-01-17 12:25:27 -0600 | [diff] [blame] | 2046 | case OBJ_REQUEST_PAGES: |
Ilya Dryomov | 04dc923 | 2016-09-15 18:05:16 +0200 | [diff] [blame] | 2047 | /* img_data requests don't own their page array */ |
| 2048 | if (obj_request->pages && |
| 2049 | !obj_request_img_data_test(obj_request)) |
Alex Elder | 788e2df | 2013-01-17 12:25:27 -0600 | [diff] [blame] | 2050 | ceph_release_page_vector(obj_request->pages, |
| 2051 | obj_request->page_count); |
| 2052 | break; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2053 | } |
| 2054 | |
Alex Elder | 868311b | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 2055 | kmem_cache_free(rbd_obj_request_cache, obj_request); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2056 | } |
| 2057 | |
Alex Elder | fb65d228 | 2013-05-08 22:50:04 -0500 | [diff] [blame] | 2058 | /* It's OK to call this for a device with no parent */ |
| 2059 | |
| 2060 | static void rbd_spec_put(struct rbd_spec *spec); |
| 2061 | static void rbd_dev_unparent(struct rbd_device *rbd_dev) |
| 2062 | { |
| 2063 | rbd_dev_remove_parent(rbd_dev); |
| 2064 | rbd_spec_put(rbd_dev->parent_spec); |
| 2065 | rbd_dev->parent_spec = NULL; |
| 2066 | rbd_dev->parent_overlap = 0; |
| 2067 | } |
| 2068 | |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2069 | /* |
Alex Elder | a2acd00 | 2013-05-08 22:50:04 -0500 | [diff] [blame] | 2070 | * Parent image reference counting is used to determine when an |
| 2071 | * image's parent fields can be safely torn down--after there are no |
| 2072 | * more in-flight requests to the parent image. When the last |
| 2073 | * reference is dropped, cleaning them up is safe. |
| 2074 | */ |
| 2075 | static void rbd_dev_parent_put(struct rbd_device *rbd_dev) |
| 2076 | { |
| 2077 | int counter; |
| 2078 | |
| 2079 | if (!rbd_dev->parent_spec) |
| 2080 | return; |
| 2081 | |
| 2082 | counter = atomic_dec_return_safe(&rbd_dev->parent_ref); |
| 2083 | if (counter > 0) |
| 2084 | return; |
| 2085 | |
| 2086 | /* Last reference; clean up parent data structures */ |
| 2087 | |
| 2088 | if (!counter) |
| 2089 | rbd_dev_unparent(rbd_dev); |
| 2090 | else |
Ilya Dryomov | 9584d50 | 2014-07-11 12:11:20 +0400 | [diff] [blame] | 2091 | rbd_warn(rbd_dev, "parent reference underflow"); |
Alex Elder | a2acd00 | 2013-05-08 22:50:04 -0500 | [diff] [blame] | 2092 | } |
| 2093 | |
| 2094 | /* |
| 2095 | * If an image has a non-zero parent overlap, get a reference to its |
| 2096 | * parent. |
| 2097 | * |
| 2098 | * Returns true if the rbd device has a parent with a non-zero |
| 2099 | * overlap and a reference for it was successfully taken, or |
| 2100 | * false otherwise. |
| 2101 | */ |
| 2102 | static bool rbd_dev_parent_get(struct rbd_device *rbd_dev) |
| 2103 | { |
Ilya Dryomov | ae43e9d | 2015-01-19 18:13:43 +0300 | [diff] [blame] | 2104 | int counter = 0; |
Alex Elder | a2acd00 | 2013-05-08 22:50:04 -0500 | [diff] [blame] | 2105 | |
| 2106 | if (!rbd_dev->parent_spec) |
| 2107 | return false; |
| 2108 | |
Ilya Dryomov | ae43e9d | 2015-01-19 18:13:43 +0300 | [diff] [blame] | 2109 | down_read(&rbd_dev->header_rwsem); |
| 2110 | if (rbd_dev->parent_overlap) |
| 2111 | counter = atomic_inc_return_safe(&rbd_dev->parent_ref); |
| 2112 | up_read(&rbd_dev->header_rwsem); |
Alex Elder | a2acd00 | 2013-05-08 22:50:04 -0500 | [diff] [blame] | 2113 | |
| 2114 | if (counter < 0) |
Ilya Dryomov | 9584d50 | 2014-07-11 12:11:20 +0400 | [diff] [blame] | 2115 | rbd_warn(rbd_dev, "parent reference overflow"); |
Alex Elder | a2acd00 | 2013-05-08 22:50:04 -0500 | [diff] [blame] | 2116 | |
Ilya Dryomov | ae43e9d | 2015-01-19 18:13:43 +0300 | [diff] [blame] | 2117 | return counter > 0; |
Alex Elder | a2acd00 | 2013-05-08 22:50:04 -0500 | [diff] [blame] | 2118 | } |
| 2119 | |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2120 | /* |
| 2121 | * Caller is responsible for filling in the list of object requests |
| 2122 | * that comprises the image request, and the Linux request pointer |
| 2123 | * (if there is one). |
| 2124 | */ |
Alex Elder | cc344fa | 2013-02-19 12:25:56 -0600 | [diff] [blame] | 2125 | static struct rbd_img_request *rbd_img_request_create( |
| 2126 | struct rbd_device *rbd_dev, |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2127 | u64 offset, u64 length, |
Guangliang Zhao | 6d2940c | 2014-03-13 11:21:35 +0800 | [diff] [blame] | 2128 | enum obj_operation_type op_type, |
Josh Durgin | 4e752f0 | 2014-04-08 11:12:11 -0700 | [diff] [blame] | 2129 | struct ceph_snap_context *snapc) |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2130 | { |
| 2131 | struct rbd_img_request *img_request; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2132 | |
Ilya Dryomov | 7a716aa | 2014-08-05 11:25:54 +0400 | [diff] [blame] | 2133 | img_request = kmem_cache_alloc(rbd_img_request_cache, GFP_NOIO); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2134 | if (!img_request) |
| 2135 | return NULL; |
| 2136 | |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2137 | img_request->rq = NULL; |
| 2138 | img_request->rbd_dev = rbd_dev; |
| 2139 | img_request->offset = offset; |
| 2140 | img_request->length = length; |
Alex Elder | 0c42524 | 2013-02-08 09:55:49 -0600 | [diff] [blame] | 2141 | img_request->flags = 0; |
Guangliang Zhao | 90e98c5 | 2014-04-01 22:22:16 +0800 | [diff] [blame] | 2142 | if (op_type == OBJ_OP_DISCARD) { |
| 2143 | img_request_discard_set(img_request); |
| 2144 | img_request->snapc = snapc; |
| 2145 | } else if (op_type == OBJ_OP_WRITE) { |
Alex Elder | 0c42524 | 2013-02-08 09:55:49 -0600 | [diff] [blame] | 2146 | img_request_write_set(img_request); |
Josh Durgin | 4e752f0 | 2014-04-08 11:12:11 -0700 | [diff] [blame] | 2147 | img_request->snapc = snapc; |
Alex Elder | 0c42524 | 2013-02-08 09:55:49 -0600 | [diff] [blame] | 2148 | } else { |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2149 | img_request->snap_id = rbd_dev->spec->snap_id; |
Alex Elder | 0c42524 | 2013-02-08 09:55:49 -0600 | [diff] [blame] | 2150 | } |
Alex Elder | a2acd00 | 2013-05-08 22:50:04 -0500 | [diff] [blame] | 2151 | if (rbd_dev_parent_get(rbd_dev)) |
Alex Elder | d0b2e94 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 2152 | img_request_layered_set(img_request); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2153 | spin_lock_init(&img_request->completion_lock); |
| 2154 | img_request->next_completion = 0; |
| 2155 | img_request->callback = NULL; |
Alex Elder | a5a337d | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 2156 | img_request->result = 0; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2157 | img_request->obj_request_count = 0; |
| 2158 | INIT_LIST_HEAD(&img_request->obj_requests); |
| 2159 | kref_init(&img_request->kref); |
| 2160 | |
Alex Elder | 37206ee | 2013-02-20 17:32:08 -0600 | [diff] [blame] | 2161 | 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] | 2162 | obj_op_name(op_type), offset, length, img_request); |
Alex Elder | 37206ee | 2013-02-20 17:32:08 -0600 | [diff] [blame] | 2163 | |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2164 | return img_request; |
| 2165 | } |
| 2166 | |
| 2167 | static void rbd_img_request_destroy(struct kref *kref) |
| 2168 | { |
| 2169 | struct rbd_img_request *img_request; |
| 2170 | struct rbd_obj_request *obj_request; |
| 2171 | struct rbd_obj_request *next_obj_request; |
| 2172 | |
| 2173 | img_request = container_of(kref, struct rbd_img_request, kref); |
| 2174 | |
Alex Elder | 37206ee | 2013-02-20 17:32:08 -0600 | [diff] [blame] | 2175 | dout("%s: img %p\n", __func__, img_request); |
| 2176 | |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2177 | for_each_obj_request_safe(img_request, obj_request, next_obj_request) |
| 2178 | rbd_img_obj_request_del(img_request, obj_request); |
Alex Elder | 25dcf95 | 2013-01-25 17:08:55 -0600 | [diff] [blame] | 2179 | rbd_assert(img_request->obj_request_count == 0); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2180 | |
Alex Elder | a2acd00 | 2013-05-08 22:50:04 -0500 | [diff] [blame] | 2181 | if (img_request_layered_test(img_request)) { |
| 2182 | img_request_layered_clear(img_request); |
| 2183 | rbd_dev_parent_put(img_request->rbd_dev); |
| 2184 | } |
| 2185 | |
Josh Durgin | bef9545 | 2014-04-04 17:47:52 -0700 | [diff] [blame] | 2186 | if (img_request_write_test(img_request) || |
| 2187 | img_request_discard_test(img_request)) |
Alex Elder | 812164f8 | 2013-04-30 00:44:32 -0500 | [diff] [blame] | 2188 | ceph_put_snap_context(img_request->snapc); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2189 | |
Alex Elder | 1c2a9df | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 2190 | kmem_cache_free(rbd_img_request_cache, img_request); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2191 | } |
| 2192 | |
Alex Elder | e93f315 | 2013-05-08 22:50:04 -0500 | [diff] [blame] | 2193 | static struct rbd_img_request *rbd_parent_request_create( |
| 2194 | struct rbd_obj_request *obj_request, |
| 2195 | u64 img_offset, u64 length) |
| 2196 | { |
| 2197 | struct rbd_img_request *parent_request; |
| 2198 | struct rbd_device *rbd_dev; |
| 2199 | |
| 2200 | rbd_assert(obj_request->img_request); |
| 2201 | rbd_dev = obj_request->img_request->rbd_dev; |
| 2202 | |
Josh Durgin | 4e752f0 | 2014-04-08 11:12:11 -0700 | [diff] [blame] | 2203 | parent_request = rbd_img_request_create(rbd_dev->parent, img_offset, |
Guangliang Zhao | 6d2940c | 2014-03-13 11:21:35 +0800 | [diff] [blame] | 2204 | length, OBJ_OP_READ, NULL); |
Alex Elder | e93f315 | 2013-05-08 22:50:04 -0500 | [diff] [blame] | 2205 | if (!parent_request) |
| 2206 | return NULL; |
| 2207 | |
| 2208 | img_request_child_set(parent_request); |
| 2209 | rbd_obj_request_get(obj_request); |
| 2210 | parent_request->obj_request = obj_request; |
| 2211 | |
| 2212 | return parent_request; |
| 2213 | } |
| 2214 | |
| 2215 | static void rbd_parent_request_destroy(struct kref *kref) |
| 2216 | { |
| 2217 | struct rbd_img_request *parent_request; |
| 2218 | struct rbd_obj_request *orig_request; |
| 2219 | |
| 2220 | parent_request = container_of(kref, struct rbd_img_request, kref); |
| 2221 | orig_request = parent_request->obj_request; |
| 2222 | |
| 2223 | parent_request->obj_request = NULL; |
| 2224 | rbd_obj_request_put(orig_request); |
| 2225 | img_request_child_clear(parent_request); |
| 2226 | |
| 2227 | rbd_img_request_destroy(kref); |
| 2228 | } |
| 2229 | |
Alex Elder | 1217857 | 2013-02-08 09:55:49 -0600 | [diff] [blame] | 2230 | static bool rbd_img_obj_end_request(struct rbd_obj_request *obj_request) |
| 2231 | { |
Alex Elder | 6365d33 | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 2232 | struct rbd_img_request *img_request; |
Alex Elder | 1217857 | 2013-02-08 09:55:49 -0600 | [diff] [blame] | 2233 | unsigned int xferred; |
| 2234 | int result; |
Alex Elder | 8b3e1a5 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 2235 | bool more; |
Alex Elder | 1217857 | 2013-02-08 09:55:49 -0600 | [diff] [blame] | 2236 | |
Alex Elder | 6365d33 | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 2237 | rbd_assert(obj_request_img_data_test(obj_request)); |
| 2238 | img_request = obj_request->img_request; |
| 2239 | |
Alex Elder | 1217857 | 2013-02-08 09:55:49 -0600 | [diff] [blame] | 2240 | rbd_assert(obj_request->xferred <= (u64)UINT_MAX); |
| 2241 | xferred = (unsigned int)obj_request->xferred; |
| 2242 | result = obj_request->result; |
| 2243 | if (result) { |
| 2244 | struct rbd_device *rbd_dev = img_request->rbd_dev; |
Guangliang Zhao | 6d2940c | 2014-03-13 11:21:35 +0800 | [diff] [blame] | 2245 | enum obj_operation_type op_type; |
| 2246 | |
Guangliang Zhao | 90e98c5 | 2014-04-01 22:22:16 +0800 | [diff] [blame] | 2247 | if (img_request_discard_test(img_request)) |
| 2248 | op_type = OBJ_OP_DISCARD; |
| 2249 | else if (img_request_write_test(img_request)) |
| 2250 | op_type = OBJ_OP_WRITE; |
| 2251 | else |
| 2252 | op_type = OBJ_OP_READ; |
Alex Elder | 1217857 | 2013-02-08 09:55:49 -0600 | [diff] [blame] | 2253 | |
Ilya Dryomov | 9584d50 | 2014-07-11 12:11:20 +0400 | [diff] [blame] | 2254 | rbd_warn(rbd_dev, "%s %llx at %llx (%llx)", |
Guangliang Zhao | 6d2940c | 2014-03-13 11:21:35 +0800 | [diff] [blame] | 2255 | obj_op_name(op_type), obj_request->length, |
| 2256 | obj_request->img_offset, obj_request->offset); |
Ilya Dryomov | 9584d50 | 2014-07-11 12:11:20 +0400 | [diff] [blame] | 2257 | rbd_warn(rbd_dev, " result %d xferred %x", |
Alex Elder | 1217857 | 2013-02-08 09:55:49 -0600 | [diff] [blame] | 2258 | result, xferred); |
| 2259 | if (!img_request->result) |
| 2260 | img_request->result = result; |
Ilya Dryomov | 082a75d | 2015-04-25 15:56:15 +0300 | [diff] [blame] | 2261 | /* |
| 2262 | * Need to end I/O on the entire obj_request worth of |
| 2263 | * bytes in case of error. |
| 2264 | */ |
| 2265 | xferred = obj_request->length; |
Alex Elder | 1217857 | 2013-02-08 09:55:49 -0600 | [diff] [blame] | 2266 | } |
| 2267 | |
Alex Elder | 8b3e1a5 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 2268 | if (img_request_child_test(img_request)) { |
| 2269 | rbd_assert(img_request->obj_request != NULL); |
| 2270 | more = obj_request->which < img_request->obj_request_count - 1; |
| 2271 | } else { |
Christoph Hellwig | 2a842ac | 2017-06-03 09:38:04 +0200 | [diff] [blame] | 2272 | blk_status_t status = errno_to_blk_status(result); |
| 2273 | |
Alex Elder | 8b3e1a5 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 2274 | rbd_assert(img_request->rq != NULL); |
Christoph Hellwig | 7ad18af | 2015-01-13 17:20:04 +0100 | [diff] [blame] | 2275 | |
Christoph Hellwig | 2a842ac | 2017-06-03 09:38:04 +0200 | [diff] [blame] | 2276 | more = blk_update_request(img_request->rq, status, xferred); |
Christoph Hellwig | 7ad18af | 2015-01-13 17:20:04 +0100 | [diff] [blame] | 2277 | if (!more) |
Christoph Hellwig | 2a842ac | 2017-06-03 09:38:04 +0200 | [diff] [blame] | 2278 | __blk_mq_end_request(img_request->rq, status); |
Alex Elder | 8b3e1a5 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 2279 | } |
| 2280 | |
| 2281 | return more; |
Alex Elder | 1217857 | 2013-02-08 09:55:49 -0600 | [diff] [blame] | 2282 | } |
| 2283 | |
Alex Elder | 2169238 | 2013-04-05 01:27:12 -0500 | [diff] [blame] | 2284 | static void rbd_img_obj_callback(struct rbd_obj_request *obj_request) |
| 2285 | { |
| 2286 | struct rbd_img_request *img_request; |
| 2287 | u32 which = obj_request->which; |
| 2288 | bool more = true; |
| 2289 | |
Alex Elder | 6365d33 | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 2290 | rbd_assert(obj_request_img_data_test(obj_request)); |
Alex Elder | 2169238 | 2013-04-05 01:27:12 -0500 | [diff] [blame] | 2291 | img_request = obj_request->img_request; |
| 2292 | |
| 2293 | dout("%s: img %p obj %p\n", __func__, img_request, obj_request); |
| 2294 | rbd_assert(img_request != NULL); |
Alex Elder | 2169238 | 2013-04-05 01:27:12 -0500 | [diff] [blame] | 2295 | rbd_assert(img_request->obj_request_count > 0); |
| 2296 | rbd_assert(which != BAD_WHICH); |
| 2297 | rbd_assert(which < img_request->obj_request_count); |
Alex Elder | 2169238 | 2013-04-05 01:27:12 -0500 | [diff] [blame] | 2298 | |
| 2299 | spin_lock_irq(&img_request->completion_lock); |
| 2300 | if (which != img_request->next_completion) |
| 2301 | goto out; |
| 2302 | |
| 2303 | for_each_obj_request_from(img_request, obj_request) { |
Alex Elder | 2169238 | 2013-04-05 01:27:12 -0500 | [diff] [blame] | 2304 | rbd_assert(more); |
| 2305 | rbd_assert(which < img_request->obj_request_count); |
| 2306 | |
| 2307 | if (!obj_request_done_test(obj_request)) |
| 2308 | break; |
Alex Elder | 1217857 | 2013-02-08 09:55:49 -0600 | [diff] [blame] | 2309 | more = rbd_img_obj_end_request(obj_request); |
Alex Elder | 2169238 | 2013-04-05 01:27:12 -0500 | [diff] [blame] | 2310 | which++; |
| 2311 | } |
| 2312 | |
| 2313 | rbd_assert(more ^ (which == img_request->obj_request_count)); |
| 2314 | img_request->next_completion = which; |
| 2315 | out: |
| 2316 | spin_unlock_irq(&img_request->completion_lock); |
Alex Elder | 0f2d5be | 2014-04-26 14:21:44 +0400 | [diff] [blame] | 2317 | rbd_img_request_put(img_request); |
Alex Elder | 2169238 | 2013-04-05 01:27:12 -0500 | [diff] [blame] | 2318 | |
| 2319 | if (!more) |
| 2320 | rbd_img_request_complete(img_request); |
| 2321 | } |
| 2322 | |
Alex Elder | f1a4739 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2323 | /* |
Josh Durgin | 3b434a2a | 2014-04-04 17:32:15 -0700 | [diff] [blame] | 2324 | * Add individual osd ops to the given ceph_osd_request and prepare |
| 2325 | * them for submission. num_ops is the current number of |
| 2326 | * osd operations already to the object request. |
| 2327 | */ |
| 2328 | static void rbd_img_obj_request_fill(struct rbd_obj_request *obj_request, |
| 2329 | struct ceph_osd_request *osd_request, |
| 2330 | enum obj_operation_type op_type, |
| 2331 | unsigned int num_ops) |
| 2332 | { |
| 2333 | struct rbd_img_request *img_request = obj_request->img_request; |
| 2334 | struct rbd_device *rbd_dev = img_request->rbd_dev; |
| 2335 | u64 object_size = rbd_obj_bytes(&rbd_dev->header); |
| 2336 | u64 offset = obj_request->offset; |
| 2337 | u64 length = obj_request->length; |
| 2338 | u64 img_end; |
| 2339 | u16 opcode; |
| 2340 | |
| 2341 | if (op_type == OBJ_OP_DISCARD) { |
Josh Durgin | d3246fb | 2014-04-07 16:49:21 -0700 | [diff] [blame] | 2342 | if (!offset && length == object_size && |
| 2343 | (!img_request_layered_test(img_request) || |
| 2344 | !obj_request_overlaps_parent(obj_request))) { |
Josh Durgin | 3b434a2a | 2014-04-04 17:32:15 -0700 | [diff] [blame] | 2345 | opcode = CEPH_OSD_OP_DELETE; |
| 2346 | } else if ((offset + length == object_size)) { |
| 2347 | opcode = CEPH_OSD_OP_TRUNCATE; |
| 2348 | } else { |
| 2349 | down_read(&rbd_dev->header_rwsem); |
| 2350 | img_end = rbd_dev->header.image_size; |
| 2351 | up_read(&rbd_dev->header_rwsem); |
| 2352 | |
| 2353 | if (obj_request->img_offset + length == img_end) |
| 2354 | opcode = CEPH_OSD_OP_TRUNCATE; |
| 2355 | else |
| 2356 | opcode = CEPH_OSD_OP_ZERO; |
| 2357 | } |
| 2358 | } else if (op_type == OBJ_OP_WRITE) { |
Ilya Dryomov | e30b757 | 2015-10-07 17:27:17 +0200 | [diff] [blame] | 2359 | if (!offset && length == object_size) |
| 2360 | opcode = CEPH_OSD_OP_WRITEFULL; |
| 2361 | else |
| 2362 | opcode = CEPH_OSD_OP_WRITE; |
Josh Durgin | 3b434a2a | 2014-04-04 17:32:15 -0700 | [diff] [blame] | 2363 | osd_req_op_alloc_hint_init(osd_request, num_ops, |
| 2364 | object_size, object_size); |
| 2365 | num_ops++; |
| 2366 | } else { |
| 2367 | opcode = CEPH_OSD_OP_READ; |
| 2368 | } |
| 2369 | |
Ilya Dryomov | 7e868b6 | 2014-11-21 22:16:43 +0300 | [diff] [blame] | 2370 | if (opcode == CEPH_OSD_OP_DELETE) |
Yan, Zheng | 144cba1 | 2015-04-27 11:09:54 +0800 | [diff] [blame] | 2371 | osd_req_op_init(osd_request, num_ops, opcode, 0); |
Ilya Dryomov | 7e868b6 | 2014-11-21 22:16:43 +0300 | [diff] [blame] | 2372 | else |
| 2373 | osd_req_op_extent_init(osd_request, num_ops, opcode, |
| 2374 | offset, length, 0, 0); |
| 2375 | |
Josh Durgin | 3b434a2a | 2014-04-04 17:32:15 -0700 | [diff] [blame] | 2376 | if (obj_request->type == OBJ_REQUEST_BIO) |
| 2377 | osd_req_op_extent_osd_data_bio(osd_request, num_ops, |
| 2378 | obj_request->bio_list, length); |
| 2379 | else if (obj_request->type == OBJ_REQUEST_PAGES) |
| 2380 | osd_req_op_extent_osd_data_pages(osd_request, num_ops, |
| 2381 | obj_request->pages, length, |
| 2382 | offset & ~PAGE_MASK, false, false); |
| 2383 | |
| 2384 | /* Discards are also writes */ |
| 2385 | if (op_type == OBJ_OP_WRITE || op_type == OBJ_OP_DISCARD) |
| 2386 | rbd_osd_req_format_write(obj_request); |
| 2387 | else |
| 2388 | rbd_osd_req_format_read(obj_request); |
| 2389 | } |
| 2390 | |
| 2391 | /* |
Alex Elder | f1a4739 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2392 | * Split up an image request into one or more object requests, each |
| 2393 | * to a different object. The "type" parameter indicates whether |
| 2394 | * "data_desc" is the pointer to the head of a list of bio |
| 2395 | * structures, or the base of a page array. In either case this |
| 2396 | * function assumes data_desc describes memory sufficient to hold |
| 2397 | * all data described by the image request. |
| 2398 | */ |
| 2399 | static int rbd_img_request_fill(struct rbd_img_request *img_request, |
| 2400 | enum obj_request_type type, |
| 2401 | void *data_desc) |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2402 | { |
| 2403 | struct rbd_device *rbd_dev = img_request->rbd_dev; |
| 2404 | struct rbd_obj_request *obj_request = NULL; |
| 2405 | struct rbd_obj_request *next_obj_request; |
Jingoo Han | a158073 | 2013-08-09 13:04:35 +0900 | [diff] [blame] | 2406 | struct bio *bio_list = NULL; |
Alex Elder | f1a4739 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2407 | unsigned int bio_offset = 0; |
Jingoo Han | a158073 | 2013-08-09 13:04:35 +0900 | [diff] [blame] | 2408 | struct page **pages = NULL; |
Guangliang Zhao | 6d2940c | 2014-03-13 11:21:35 +0800 | [diff] [blame] | 2409 | enum obj_operation_type op_type; |
Alex Elder | 7da22d2 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 2410 | u64 img_offset; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2411 | u64 resid; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2412 | |
Alex Elder | f1a4739 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2413 | dout("%s: img %p type %d data_desc %p\n", __func__, img_request, |
| 2414 | (int)type, data_desc); |
Alex Elder | 37206ee | 2013-02-20 17:32:08 -0600 | [diff] [blame] | 2415 | |
Alex Elder | 7da22d2 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 2416 | img_offset = img_request->offset; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2417 | resid = img_request->length; |
Alex Elder | 4dda41d | 2013-02-20 21:59:33 -0600 | [diff] [blame] | 2418 | rbd_assert(resid > 0); |
Josh Durgin | 3b434a2a | 2014-04-04 17:32:15 -0700 | [diff] [blame] | 2419 | op_type = rbd_img_request_op_type(img_request); |
Alex Elder | f1a4739 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2420 | |
| 2421 | if (type == OBJ_REQUEST_BIO) { |
| 2422 | bio_list = data_desc; |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 2423 | rbd_assert(img_offset == |
| 2424 | bio_list->bi_iter.bi_sector << SECTOR_SHIFT); |
Guangliang Zhao | 90e98c5 | 2014-04-01 22:22:16 +0800 | [diff] [blame] | 2425 | } else if (type == OBJ_REQUEST_PAGES) { |
Alex Elder | f1a4739 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2426 | pages = data_desc; |
| 2427 | } |
| 2428 | |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2429 | while (resid) { |
Alex Elder | 2fa1232 | 2013-04-05 01:27:12 -0500 | [diff] [blame] | 2430 | struct ceph_osd_request *osd_req; |
Ilya Dryomov | a90bb0c | 2017-01-25 18:16:23 +0100 | [diff] [blame] | 2431 | u64 object_no = img_offset >> rbd_dev->header.obj_order; |
Ilya Dryomov | 67e2b65 | 2017-01-25 18:16:22 +0100 | [diff] [blame] | 2432 | u64 offset = rbd_segment_offset(rbd_dev, img_offset); |
| 2433 | u64 length = rbd_segment_length(rbd_dev, img_offset, resid); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2434 | |
Ilya Dryomov | 6c696d8 | 2017-01-25 18:16:23 +0100 | [diff] [blame] | 2435 | obj_request = rbd_obj_request_create(type); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2436 | if (!obj_request) |
| 2437 | goto out_unwind; |
Ilya Dryomov | 62054da | 2014-03-04 11:57:17 +0200 | [diff] [blame] | 2438 | |
Ilya Dryomov | a90bb0c | 2017-01-25 18:16:23 +0100 | [diff] [blame] | 2439 | obj_request->object_no = object_no; |
Ilya Dryomov | 67e2b65 | 2017-01-25 18:16:22 +0100 | [diff] [blame] | 2440 | obj_request->offset = offset; |
| 2441 | obj_request->length = length; |
| 2442 | |
Josh Durgin | 03507db | 2013-08-27 14:45:46 -0700 | [diff] [blame] | 2443 | /* |
| 2444 | * set obj_request->img_request before creating the |
| 2445 | * osd_request so that it gets the right snapc |
| 2446 | */ |
| 2447 | rbd_img_obj_request_add(img_request, obj_request); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2448 | |
Alex Elder | f1a4739 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2449 | if (type == OBJ_REQUEST_BIO) { |
| 2450 | unsigned int clone_size; |
| 2451 | |
| 2452 | rbd_assert(length <= (u64)UINT_MAX); |
| 2453 | clone_size = (unsigned int)length; |
| 2454 | obj_request->bio_list = |
| 2455 | bio_chain_clone_range(&bio_list, |
| 2456 | &bio_offset, |
| 2457 | clone_size, |
David Disseldorp | 2224d87 | 2016-04-05 11:13:39 +0200 | [diff] [blame] | 2458 | GFP_NOIO); |
Alex Elder | f1a4739 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2459 | if (!obj_request->bio_list) |
Ilya Dryomov | 62054da | 2014-03-04 11:57:17 +0200 | [diff] [blame] | 2460 | goto out_unwind; |
Guangliang Zhao | 90e98c5 | 2014-04-01 22:22:16 +0800 | [diff] [blame] | 2461 | } else if (type == OBJ_REQUEST_PAGES) { |
Alex Elder | f1a4739 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2462 | unsigned int page_count; |
| 2463 | |
| 2464 | obj_request->pages = pages; |
| 2465 | page_count = (u32)calc_pages_for(offset, length); |
| 2466 | obj_request->page_count = page_count; |
| 2467 | if ((offset + length) & ~PAGE_MASK) |
| 2468 | page_count--; /* more on last page */ |
| 2469 | pages += page_count; |
| 2470 | } |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2471 | |
Guangliang Zhao | 6d2940c | 2014-03-13 11:21:35 +0800 | [diff] [blame] | 2472 | osd_req = rbd_osd_req_create(rbd_dev, op_type, |
| 2473 | (op_type == OBJ_OP_WRITE) ? 2 : 1, |
| 2474 | obj_request); |
Alex Elder | 2fa1232 | 2013-04-05 01:27:12 -0500 | [diff] [blame] | 2475 | if (!osd_req) |
Ilya Dryomov | 62054da | 2014-03-04 11:57:17 +0200 | [diff] [blame] | 2476 | goto out_unwind; |
Josh Durgin | 3b434a2a | 2014-04-04 17:32:15 -0700 | [diff] [blame] | 2477 | |
Alex Elder | 2fa1232 | 2013-04-05 01:27:12 -0500 | [diff] [blame] | 2478 | obj_request->osd_req = osd_req; |
Alex Elder | 2169238 | 2013-04-05 01:27:12 -0500 | [diff] [blame] | 2479 | obj_request->callback = rbd_img_obj_callback; |
Alex Elder | 7da22d2 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 2480 | obj_request->img_offset = img_offset; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2481 | |
Josh Durgin | 3b434a2a | 2014-04-04 17:32:15 -0700 | [diff] [blame] | 2482 | rbd_img_obj_request_fill(obj_request, osd_req, op_type, 0); |
| 2483 | |
Alex Elder | 7da22d2 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 2484 | img_offset += length; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2485 | resid -= length; |
| 2486 | } |
| 2487 | |
| 2488 | return 0; |
| 2489 | |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2490 | out_unwind: |
| 2491 | for_each_obj_request_safe(img_request, obj_request, next_obj_request) |
Ilya Dryomov | 42dd037 | 2014-03-04 11:57:17 +0200 | [diff] [blame] | 2492 | rbd_img_obj_request_del(img_request, obj_request); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2493 | |
| 2494 | return -ENOMEM; |
| 2495 | } |
| 2496 | |
Alex Elder | 3d7efd1 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2497 | static void |
Ilya Dryomov | 2761713 | 2015-07-16 17:36:11 +0300 | [diff] [blame] | 2498 | rbd_osd_copyup_callback(struct rbd_obj_request *obj_request) |
Alex Elder | 0eefd47 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2499 | { |
| 2500 | struct rbd_img_request *img_request; |
| 2501 | struct rbd_device *rbd_dev; |
Alex Elder | ebda640 | 2013-05-10 16:29:22 -0500 | [diff] [blame] | 2502 | struct page **pages; |
Alex Elder | 0eefd47 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2503 | u32 page_count; |
| 2504 | |
Ilya Dryomov | 2761713 | 2015-07-16 17:36:11 +0300 | [diff] [blame] | 2505 | dout("%s: obj %p\n", __func__, obj_request); |
| 2506 | |
Josh Durgin | d3246fb | 2014-04-07 16:49:21 -0700 | [diff] [blame] | 2507 | rbd_assert(obj_request->type == OBJ_REQUEST_BIO || |
| 2508 | obj_request->type == OBJ_REQUEST_NODATA); |
Alex Elder | 0eefd47 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2509 | rbd_assert(obj_request_img_data_test(obj_request)); |
| 2510 | img_request = obj_request->img_request; |
| 2511 | rbd_assert(img_request); |
| 2512 | |
| 2513 | rbd_dev = img_request->rbd_dev; |
| 2514 | rbd_assert(rbd_dev); |
Alex Elder | 0eefd47 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2515 | |
Alex Elder | ebda640 | 2013-05-10 16:29:22 -0500 | [diff] [blame] | 2516 | pages = obj_request->copyup_pages; |
| 2517 | rbd_assert(pages != NULL); |
Alex Elder | 0eefd47 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2518 | obj_request->copyup_pages = NULL; |
Alex Elder | ebda640 | 2013-05-10 16:29:22 -0500 | [diff] [blame] | 2519 | page_count = obj_request->copyup_page_count; |
| 2520 | rbd_assert(page_count); |
| 2521 | obj_request->copyup_page_count = 0; |
| 2522 | ceph_release_page_vector(pages, page_count); |
Alex Elder | 0eefd47 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2523 | |
| 2524 | /* |
| 2525 | * We want the transfer count to reflect the size of the |
| 2526 | * original write request. There is no such thing as a |
| 2527 | * successful short write, so if the request was successful |
| 2528 | * we can just set it to the originally-requested length. |
| 2529 | */ |
| 2530 | if (!obj_request->result) |
| 2531 | obj_request->xferred = obj_request->length; |
| 2532 | |
Ilya Dryomov | 2761713 | 2015-07-16 17:36:11 +0300 | [diff] [blame] | 2533 | obj_request_done_set(obj_request); |
Alex Elder | 0eefd47 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2534 | } |
| 2535 | |
| 2536 | static void |
Alex Elder | 3d7efd1 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2537 | rbd_img_obj_parent_read_full_callback(struct rbd_img_request *img_request) |
| 2538 | { |
| 2539 | struct rbd_obj_request *orig_request; |
Alex Elder | 0eefd47 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2540 | struct ceph_osd_request *osd_req; |
Alex Elder | 0eefd47 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2541 | struct rbd_device *rbd_dev; |
Alex Elder | 3d7efd1 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2542 | struct page **pages; |
Josh Durgin | d3246fb | 2014-04-07 16:49:21 -0700 | [diff] [blame] | 2543 | enum obj_operation_type op_type; |
Alex Elder | ebda640 | 2013-05-10 16:29:22 -0500 | [diff] [blame] | 2544 | u32 page_count; |
Alex Elder | bbea1c1 | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 2545 | int img_result; |
Alex Elder | ebda640 | 2013-05-10 16:29:22 -0500 | [diff] [blame] | 2546 | u64 parent_length; |
Alex Elder | 3d7efd1 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2547 | |
| 2548 | rbd_assert(img_request_child_test(img_request)); |
| 2549 | |
| 2550 | /* First get what we need from the image request */ |
| 2551 | |
| 2552 | pages = img_request->copyup_pages; |
| 2553 | rbd_assert(pages != NULL); |
| 2554 | img_request->copyup_pages = NULL; |
Alex Elder | ebda640 | 2013-05-10 16:29:22 -0500 | [diff] [blame] | 2555 | page_count = img_request->copyup_page_count; |
| 2556 | rbd_assert(page_count); |
| 2557 | img_request->copyup_page_count = 0; |
Alex Elder | 3d7efd1 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2558 | |
| 2559 | orig_request = img_request->obj_request; |
| 2560 | rbd_assert(orig_request != NULL); |
Alex Elder | b91f09f | 2013-05-10 16:29:22 -0500 | [diff] [blame] | 2561 | rbd_assert(obj_request_type_valid(orig_request->type)); |
Alex Elder | bbea1c1 | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 2562 | img_result = img_request->result; |
Alex Elder | ebda640 | 2013-05-10 16:29:22 -0500 | [diff] [blame] | 2563 | parent_length = img_request->length; |
Ilya Dryomov | fa35511 | 2016-09-16 15:20:42 +0200 | [diff] [blame] | 2564 | rbd_assert(img_result || parent_length == img_request->xferred); |
Alex Elder | 3d7efd1 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2565 | rbd_img_request_put(img_request); |
| 2566 | |
Alex Elder | 91c6feb | 2013-05-06 17:40:32 -0500 | [diff] [blame] | 2567 | rbd_assert(orig_request->img_request); |
| 2568 | rbd_dev = orig_request->img_request->rbd_dev; |
Alex Elder | 3d7efd1 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2569 | rbd_assert(rbd_dev); |
Alex Elder | 3d7efd1 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2570 | |
Alex Elder | bbea1c1 | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 2571 | /* |
| 2572 | * If the overlap has become 0 (most likely because the |
| 2573 | * image has been flattened) we need to free the pages |
| 2574 | * and re-submit the original write request. |
| 2575 | */ |
| 2576 | if (!rbd_dev->parent_overlap) { |
Alex Elder | bbea1c1 | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 2577 | ceph_release_page_vector(pages, page_count); |
Ilya Dryomov | 980917f | 2016-09-12 18:59:42 +0200 | [diff] [blame] | 2578 | rbd_obj_request_submit(orig_request); |
| 2579 | return; |
Alex Elder | bbea1c1 | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 2580 | } |
| 2581 | |
| 2582 | if (img_result) |
Alex Elder | 0eefd47 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2583 | goto out_err; |
Alex Elder | 3d7efd1 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2584 | |
Alex Elder | 8785b1d | 2013-05-09 10:08:49 -0500 | [diff] [blame] | 2585 | /* |
| 2586 | * The original osd request is of no use to use any more. |
Ilya Dryomov | 0ccd592 | 2014-02-25 16:22:28 +0200 | [diff] [blame] | 2587 | * 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] | 2588 | * request. Allocate the new copyup osd request for the |
| 2589 | * original request, and release the old one. |
| 2590 | */ |
Alex Elder | bbea1c1 | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 2591 | img_result = -ENOMEM; |
Alex Elder | 0eefd47 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2592 | osd_req = rbd_osd_req_create_copyup(orig_request); |
| 2593 | if (!osd_req) |
| 2594 | goto out_err; |
Alex Elder | 8785b1d | 2013-05-09 10:08:49 -0500 | [diff] [blame] | 2595 | rbd_osd_req_destroy(orig_request->osd_req); |
Alex Elder | 0eefd47 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2596 | orig_request->osd_req = osd_req; |
| 2597 | orig_request->copyup_pages = pages; |
Alex Elder | ebda640 | 2013-05-10 16:29:22 -0500 | [diff] [blame] | 2598 | orig_request->copyup_page_count = page_count; |
Alex Elder | 3d7efd1 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2599 | |
Alex Elder | 0eefd47 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2600 | /* Initialize the copyup op */ |
| 2601 | |
| 2602 | 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] | 2603 | 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] | 2604 | false, false); |
| 2605 | |
Josh Durgin | d3246fb | 2014-04-07 16:49:21 -0700 | [diff] [blame] | 2606 | /* Add the other op(s) */ |
Ilya Dryomov | 0ccd592 | 2014-02-25 16:22:28 +0200 | [diff] [blame] | 2607 | |
Josh Durgin | d3246fb | 2014-04-07 16:49:21 -0700 | [diff] [blame] | 2608 | op_type = rbd_img_request_op_type(orig_request->img_request); |
| 2609 | rbd_img_obj_request_fill(orig_request, osd_req, op_type, 1); |
Alex Elder | 0eefd47 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2610 | |
| 2611 | /* All set, send it off. */ |
| 2612 | |
Ilya Dryomov | 980917f | 2016-09-12 18:59:42 +0200 | [diff] [blame] | 2613 | rbd_obj_request_submit(orig_request); |
| 2614 | return; |
Alex Elder | 0eefd47 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2615 | |
Alex Elder | 0eefd47 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2616 | out_err: |
Ilya Dryomov | fa35511 | 2016-09-16 15:20:42 +0200 | [diff] [blame] | 2617 | ceph_release_page_vector(pages, page_count); |
Ilya Dryomov | 0dcc685 | 2016-09-26 15:43:52 +0200 | [diff] [blame] | 2618 | rbd_obj_request_error(orig_request, img_result); |
Alex Elder | 3d7efd1 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2619 | } |
| 2620 | |
| 2621 | /* |
| 2622 | * Read from the parent image the range of data that covers the |
| 2623 | * entire target of the given object request. This is used for |
| 2624 | * satisfying a layered image write request when the target of an |
| 2625 | * object request from the image request does not exist. |
| 2626 | * |
| 2627 | * A page array big enough to hold the returned data is allocated |
| 2628 | * and supplied to rbd_img_request_fill() as the "data descriptor." |
| 2629 | * When the read completes, this page array will be transferred to |
| 2630 | * the original object request for the copyup operation. |
| 2631 | * |
Ilya Dryomov | c2e8241 | 2016-09-13 20:18:01 +0200 | [diff] [blame] | 2632 | * If an error occurs, it is recorded as the result of the original |
| 2633 | * object request in rbd_img_obj_exists_callback(). |
Alex Elder | 3d7efd1 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2634 | */ |
| 2635 | static int rbd_img_obj_parent_read_full(struct rbd_obj_request *obj_request) |
| 2636 | { |
Ilya Dryomov | 058aa99 | 2016-09-12 14:44:45 +0200 | [diff] [blame] | 2637 | struct rbd_device *rbd_dev = obj_request->img_request->rbd_dev; |
Alex Elder | 3d7efd1 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2638 | struct rbd_img_request *parent_request = NULL; |
Alex Elder | 3d7efd1 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2639 | u64 img_offset; |
| 2640 | u64 length; |
| 2641 | struct page **pages = NULL; |
| 2642 | u32 page_count; |
| 2643 | int result; |
| 2644 | |
Alex Elder | 3d7efd1 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2645 | rbd_assert(rbd_dev->parent != NULL); |
| 2646 | |
| 2647 | /* |
| 2648 | * Determine the byte range covered by the object in the |
| 2649 | * child image to which the original request was to be sent. |
| 2650 | */ |
| 2651 | img_offset = obj_request->img_offset - obj_request->offset; |
Ilya Dryomov | 5bc3fb1 | 2017-01-25 18:16:22 +0100 | [diff] [blame] | 2652 | length = rbd_obj_bytes(&rbd_dev->header); |
Alex Elder | 3d7efd1 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2653 | |
| 2654 | /* |
Alex Elder | a9e8ba2c | 2013-04-21 00:32:07 -0500 | [diff] [blame] | 2655 | * There is no defined parent data beyond the parent |
| 2656 | * overlap, so limit what we read at that boundary if |
| 2657 | * necessary. |
| 2658 | */ |
| 2659 | if (img_offset + length > rbd_dev->parent_overlap) { |
| 2660 | rbd_assert(img_offset < rbd_dev->parent_overlap); |
| 2661 | length = rbd_dev->parent_overlap - img_offset; |
| 2662 | } |
| 2663 | |
| 2664 | /* |
Alex Elder | 3d7efd1 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2665 | * Allocate a page array big enough to receive the data read |
| 2666 | * from the parent. |
| 2667 | */ |
| 2668 | page_count = (u32)calc_pages_for(0, length); |
Ilya Dryomov | 1e37f2f | 2017-11-06 11:33:36 +0100 | [diff] [blame] | 2669 | pages = ceph_alloc_page_vector(page_count, GFP_NOIO); |
Alex Elder | 3d7efd1 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2670 | if (IS_ERR(pages)) { |
| 2671 | result = PTR_ERR(pages); |
| 2672 | pages = NULL; |
| 2673 | goto out_err; |
| 2674 | } |
| 2675 | |
| 2676 | result = -ENOMEM; |
Alex Elder | e93f315 | 2013-05-08 22:50:04 -0500 | [diff] [blame] | 2677 | parent_request = rbd_parent_request_create(obj_request, |
| 2678 | img_offset, length); |
Alex Elder | 3d7efd1 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2679 | if (!parent_request) |
| 2680 | goto out_err; |
Alex Elder | 3d7efd1 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2681 | |
| 2682 | result = rbd_img_request_fill(parent_request, OBJ_REQUEST_PAGES, pages); |
| 2683 | if (result) |
| 2684 | goto out_err; |
Ilya Dryomov | 058aa99 | 2016-09-12 14:44:45 +0200 | [diff] [blame] | 2685 | |
Alex Elder | 3d7efd1 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2686 | parent_request->copyup_pages = pages; |
Alex Elder | ebda640 | 2013-05-10 16:29:22 -0500 | [diff] [blame] | 2687 | parent_request->copyup_page_count = page_count; |
Alex Elder | 3d7efd1 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2688 | parent_request->callback = rbd_img_obj_parent_read_full_callback; |
Ilya Dryomov | 058aa99 | 2016-09-12 14:44:45 +0200 | [diff] [blame] | 2689 | |
Alex Elder | 3d7efd1 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2690 | result = rbd_img_request_submit(parent_request); |
| 2691 | if (!result) |
| 2692 | return 0; |
| 2693 | |
| 2694 | parent_request->copyup_pages = NULL; |
Alex Elder | ebda640 | 2013-05-10 16:29:22 -0500 | [diff] [blame] | 2695 | parent_request->copyup_page_count = 0; |
Alex Elder | 3d7efd1 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2696 | parent_request->obj_request = NULL; |
| 2697 | rbd_obj_request_put(obj_request); |
| 2698 | out_err: |
| 2699 | if (pages) |
| 2700 | ceph_release_page_vector(pages, page_count); |
| 2701 | if (parent_request) |
| 2702 | rbd_img_request_put(parent_request); |
Alex Elder | 3d7efd1 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2703 | return result; |
| 2704 | } |
| 2705 | |
Alex Elder | c5b5ef6 | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 2706 | static void rbd_img_obj_exists_callback(struct rbd_obj_request *obj_request) |
| 2707 | { |
Alex Elder | c5b5ef6 | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 2708 | struct rbd_obj_request *orig_request; |
Alex Elder | 638f5ab | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 2709 | struct rbd_device *rbd_dev; |
Alex Elder | c5b5ef6 | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 2710 | int result; |
| 2711 | |
| 2712 | rbd_assert(!obj_request_img_data_test(obj_request)); |
| 2713 | |
| 2714 | /* |
| 2715 | * All we need from the object request is the original |
| 2716 | * request and the result of the STAT op. Grab those, then |
| 2717 | * we're done with the request. |
| 2718 | */ |
| 2719 | orig_request = obj_request->obj_request; |
| 2720 | obj_request->obj_request = NULL; |
Alex Elder | 912c317 | 2013-05-13 20:35:38 -0500 | [diff] [blame] | 2721 | rbd_obj_request_put(orig_request); |
Alex Elder | c5b5ef6 | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 2722 | rbd_assert(orig_request); |
| 2723 | rbd_assert(orig_request->img_request); |
| 2724 | |
| 2725 | result = obj_request->result; |
| 2726 | obj_request->result = 0; |
| 2727 | |
| 2728 | dout("%s: obj %p for obj %p result %d %llu/%llu\n", __func__, |
| 2729 | obj_request, orig_request, result, |
| 2730 | obj_request->xferred, obj_request->length); |
| 2731 | rbd_obj_request_put(obj_request); |
| 2732 | |
Alex Elder | 638f5ab | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 2733 | /* |
| 2734 | * If the overlap has become 0 (most likely because the |
Ilya Dryomov | 980917f | 2016-09-12 18:59:42 +0200 | [diff] [blame] | 2735 | * image has been flattened) we need to re-submit the |
| 2736 | * original request. |
Alex Elder | 638f5ab | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 2737 | */ |
| 2738 | rbd_dev = orig_request->img_request->rbd_dev; |
| 2739 | if (!rbd_dev->parent_overlap) { |
Ilya Dryomov | 980917f | 2016-09-12 18:59:42 +0200 | [diff] [blame] | 2740 | rbd_obj_request_submit(orig_request); |
| 2741 | return; |
Alex Elder | 638f5ab | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 2742 | } |
Alex Elder | c5b5ef6 | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 2743 | |
| 2744 | /* |
| 2745 | * Our only purpose here is to determine whether the object |
| 2746 | * exists, and we don't want to treat the non-existence as |
| 2747 | * an error. If something else comes back, transfer the |
| 2748 | * error to the original request and complete it now. |
| 2749 | */ |
| 2750 | if (!result) { |
| 2751 | obj_request_existence_set(orig_request, true); |
| 2752 | } else if (result == -ENOENT) { |
| 2753 | obj_request_existence_set(orig_request, false); |
Ilya Dryomov | c2e8241 | 2016-09-13 20:18:01 +0200 | [diff] [blame] | 2754 | } else { |
| 2755 | goto fail_orig_request; |
Alex Elder | c5b5ef6 | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 2756 | } |
| 2757 | |
| 2758 | /* |
| 2759 | * Resubmit the original request now that we have recorded |
| 2760 | * whether the target object exists. |
| 2761 | */ |
Ilya Dryomov | c2e8241 | 2016-09-13 20:18:01 +0200 | [diff] [blame] | 2762 | result = rbd_img_obj_request_submit(orig_request); |
| 2763 | if (result) |
| 2764 | goto fail_orig_request; |
| 2765 | |
| 2766 | return; |
| 2767 | |
| 2768 | fail_orig_request: |
Ilya Dryomov | 0dcc685 | 2016-09-26 15:43:52 +0200 | [diff] [blame] | 2769 | rbd_obj_request_error(orig_request, result); |
Alex Elder | c5b5ef6 | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 2770 | } |
| 2771 | |
| 2772 | static int rbd_img_obj_exists_submit(struct rbd_obj_request *obj_request) |
| 2773 | { |
Ilya Dryomov | 058aa99 | 2016-09-12 14:44:45 +0200 | [diff] [blame] | 2774 | struct rbd_device *rbd_dev = obj_request->img_request->rbd_dev; |
Alex Elder | c5b5ef6 | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 2775 | struct rbd_obj_request *stat_request; |
Ilya Dryomov | 710214e | 2016-09-15 17:53:32 +0200 | [diff] [blame] | 2776 | struct page **pages; |
Alex Elder | c5b5ef6 | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 2777 | u32 page_count; |
| 2778 | size_t size; |
| 2779 | int ret; |
| 2780 | |
Ilya Dryomov | 6c696d8 | 2017-01-25 18:16:23 +0100 | [diff] [blame] | 2781 | stat_request = rbd_obj_request_create(OBJ_REQUEST_PAGES); |
Ilya Dryomov | 710214e | 2016-09-15 17:53:32 +0200 | [diff] [blame] | 2782 | if (!stat_request) |
| 2783 | return -ENOMEM; |
| 2784 | |
Ilya Dryomov | a90bb0c | 2017-01-25 18:16:23 +0100 | [diff] [blame] | 2785 | stat_request->object_no = obj_request->object_no; |
| 2786 | |
Ilya Dryomov | 710214e | 2016-09-15 17:53:32 +0200 | [diff] [blame] | 2787 | stat_request->osd_req = rbd_osd_req_create(rbd_dev, OBJ_OP_READ, 1, |
| 2788 | stat_request); |
| 2789 | if (!stat_request->osd_req) { |
| 2790 | ret = -ENOMEM; |
| 2791 | goto fail_stat_request; |
| 2792 | } |
| 2793 | |
Alex Elder | c5b5ef6 | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 2794 | /* |
| 2795 | * The response data for a STAT call consists of: |
| 2796 | * le64 length; |
| 2797 | * struct { |
| 2798 | * le32 tv_sec; |
| 2799 | * le32 tv_nsec; |
| 2800 | * } mtime; |
| 2801 | */ |
| 2802 | size = sizeof (__le64) + sizeof (__le32) + sizeof (__le32); |
| 2803 | page_count = (u32)calc_pages_for(0, size); |
Ilya Dryomov | 1e37f2f | 2017-11-06 11:33:36 +0100 | [diff] [blame] | 2804 | pages = ceph_alloc_page_vector(page_count, GFP_NOIO); |
Ilya Dryomov | 710214e | 2016-09-15 17:53:32 +0200 | [diff] [blame] | 2805 | if (IS_ERR(pages)) { |
| 2806 | ret = PTR_ERR(pages); |
| 2807 | goto fail_stat_request; |
| 2808 | } |
Alex Elder | c5b5ef6 | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 2809 | |
Ilya Dryomov | 710214e | 2016-09-15 17:53:32 +0200 | [diff] [blame] | 2810 | osd_req_op_init(stat_request->osd_req, 0, CEPH_OSD_OP_STAT, 0); |
| 2811 | osd_req_op_raw_data_in_pages(stat_request->osd_req, 0, pages, size, 0, |
| 2812 | false, false); |
Alex Elder | c5b5ef6 | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 2813 | |
| 2814 | rbd_obj_request_get(obj_request); |
| 2815 | stat_request->obj_request = obj_request; |
| 2816 | stat_request->pages = pages; |
| 2817 | stat_request->page_count = page_count; |
Alex Elder | c5b5ef6 | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 2818 | stat_request->callback = rbd_img_obj_exists_callback; |
| 2819 | |
Ilya Dryomov | 980917f | 2016-09-12 18:59:42 +0200 | [diff] [blame] | 2820 | rbd_obj_request_submit(stat_request); |
| 2821 | return 0; |
Alex Elder | c5b5ef6 | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 2822 | |
Ilya Dryomov | 710214e | 2016-09-15 17:53:32 +0200 | [diff] [blame] | 2823 | fail_stat_request: |
| 2824 | rbd_obj_request_put(stat_request); |
Alex Elder | c5b5ef6 | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 2825 | return ret; |
| 2826 | } |
| 2827 | |
Ilya Dryomov | 70d045f | 2014-09-12 16:02:01 +0400 | [diff] [blame] | 2828 | static bool img_obj_request_simple(struct rbd_obj_request *obj_request) |
Alex Elder | b454e36 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2829 | { |
Ilya Dryomov | 058aa99 | 2016-09-12 14:44:45 +0200 | [diff] [blame] | 2830 | struct rbd_img_request *img_request = obj_request->img_request; |
| 2831 | struct rbd_device *rbd_dev = img_request->rbd_dev; |
Alex Elder | b454e36 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2832 | |
Ilya Dryomov | 70d045f | 2014-09-12 16:02:01 +0400 | [diff] [blame] | 2833 | /* Reads */ |
Josh Durgin | 1c22088 | 2014-04-04 17:49:12 -0700 | [diff] [blame] | 2834 | if (!img_request_write_test(img_request) && |
| 2835 | !img_request_discard_test(img_request)) |
Ilya Dryomov | 70d045f | 2014-09-12 16:02:01 +0400 | [diff] [blame] | 2836 | return true; |
Alex Elder | b454e36 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2837 | |
Ilya Dryomov | 70d045f | 2014-09-12 16:02:01 +0400 | [diff] [blame] | 2838 | /* Non-layered writes */ |
| 2839 | if (!img_request_layered_test(img_request)) |
| 2840 | return true; |
| 2841 | |
| 2842 | /* |
| 2843 | * Layered writes outside of the parent overlap range don't |
| 2844 | * share any data with the parent. |
| 2845 | */ |
| 2846 | if (!obj_request_overlaps_parent(obj_request)) |
| 2847 | return true; |
| 2848 | |
| 2849 | /* |
Guangliang Zhao | c622d22 | 2014-04-01 22:22:15 +0800 | [diff] [blame] | 2850 | * Entire-object layered writes - we will overwrite whatever |
| 2851 | * parent data there is anyway. |
| 2852 | */ |
| 2853 | if (!obj_request->offset && |
| 2854 | obj_request->length == rbd_obj_bytes(&rbd_dev->header)) |
| 2855 | return true; |
| 2856 | |
| 2857 | /* |
Ilya Dryomov | 70d045f | 2014-09-12 16:02:01 +0400 | [diff] [blame] | 2858 | * If the object is known to already exist, its parent data has |
| 2859 | * already been copied. |
| 2860 | */ |
| 2861 | if (obj_request_known_test(obj_request) && |
| 2862 | obj_request_exists_test(obj_request)) |
| 2863 | return true; |
| 2864 | |
| 2865 | return false; |
| 2866 | } |
| 2867 | |
| 2868 | static int rbd_img_obj_request_submit(struct rbd_obj_request *obj_request) |
| 2869 | { |
Ilya Dryomov | 058aa99 | 2016-09-12 14:44:45 +0200 | [diff] [blame] | 2870 | rbd_assert(obj_request_img_data_test(obj_request)); |
| 2871 | rbd_assert(obj_request_type_valid(obj_request->type)); |
| 2872 | rbd_assert(obj_request->img_request); |
| 2873 | |
Ilya Dryomov | 70d045f | 2014-09-12 16:02:01 +0400 | [diff] [blame] | 2874 | if (img_obj_request_simple(obj_request)) { |
Ilya Dryomov | 980917f | 2016-09-12 18:59:42 +0200 | [diff] [blame] | 2875 | rbd_obj_request_submit(obj_request); |
| 2876 | return 0; |
Alex Elder | b454e36 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2877 | } |
| 2878 | |
| 2879 | /* |
Alex Elder | 3d7efd1 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2880 | * It's a layered write. The target object might exist but |
| 2881 | * we may not know that yet. If we know it doesn't exist, |
| 2882 | * start by reading the data for the full target object from |
| 2883 | * 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] | 2884 | */ |
Ilya Dryomov | 70d045f | 2014-09-12 16:02:01 +0400 | [diff] [blame] | 2885 | if (obj_request_known_test(obj_request)) |
Alex Elder | 3d7efd1 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2886 | return rbd_img_obj_parent_read_full(obj_request); |
| 2887 | |
| 2888 | /* We don't know whether the target exists. Go find out. */ |
Alex Elder | b454e36 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2889 | |
| 2890 | return rbd_img_obj_exists_submit(obj_request); |
| 2891 | } |
| 2892 | |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2893 | static int rbd_img_request_submit(struct rbd_img_request *img_request) |
| 2894 | { |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2895 | struct rbd_obj_request *obj_request; |
Alex Elder | 46faeed | 2013-04-10 17:47:46 -0500 | [diff] [blame] | 2896 | struct rbd_obj_request *next_obj_request; |
Ilya Dryomov | 663ae2c | 2016-05-16 13:18:57 +0200 | [diff] [blame] | 2897 | int ret = 0; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2898 | |
Alex Elder | 37206ee | 2013-02-20 17:32:08 -0600 | [diff] [blame] | 2899 | dout("%s: img %p\n", __func__, img_request); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2900 | |
Ilya Dryomov | 663ae2c | 2016-05-16 13:18:57 +0200 | [diff] [blame] | 2901 | rbd_img_request_get(img_request); |
| 2902 | for_each_obj_request_safe(img_request, obj_request, next_obj_request) { |
Alex Elder | b454e36 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2903 | ret = rbd_img_obj_request_submit(obj_request); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2904 | if (ret) |
Ilya Dryomov | 663ae2c | 2016-05-16 13:18:57 +0200 | [diff] [blame] | 2905 | goto out_put_ireq; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2906 | } |
| 2907 | |
Ilya Dryomov | 663ae2c | 2016-05-16 13:18:57 +0200 | [diff] [blame] | 2908 | out_put_ireq: |
| 2909 | rbd_img_request_put(img_request); |
| 2910 | return ret; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2911 | } |
| 2912 | |
Alex Elder | 8b3e1a5 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 2913 | static void rbd_img_parent_read_callback(struct rbd_img_request *img_request) |
| 2914 | { |
| 2915 | struct rbd_obj_request *obj_request; |
Alex Elder | a9e8ba2c | 2013-04-21 00:32:07 -0500 | [diff] [blame] | 2916 | struct rbd_device *rbd_dev; |
| 2917 | u64 obj_end; |
Alex Elder | 02c74fb | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 2918 | u64 img_xferred; |
| 2919 | int img_result; |
Alex Elder | 8b3e1a5 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 2920 | |
| 2921 | rbd_assert(img_request_child_test(img_request)); |
| 2922 | |
Alex Elder | 02c74fb | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 2923 | /* First get what we need from the image request and release it */ |
| 2924 | |
Alex Elder | 8b3e1a5 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 2925 | obj_request = img_request->obj_request; |
Alex Elder | 02c74fb | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 2926 | img_xferred = img_request->xferred; |
| 2927 | img_result = img_request->result; |
| 2928 | rbd_img_request_put(img_request); |
| 2929 | |
| 2930 | /* |
| 2931 | * If the overlap has become 0 (most likely because the |
| 2932 | * image has been flattened) we need to re-submit the |
| 2933 | * original request. |
| 2934 | */ |
Alex Elder | a9e8ba2c | 2013-04-21 00:32:07 -0500 | [diff] [blame] | 2935 | rbd_assert(obj_request); |
| 2936 | rbd_assert(obj_request->img_request); |
Alex Elder | 02c74fb | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 2937 | rbd_dev = obj_request->img_request->rbd_dev; |
| 2938 | if (!rbd_dev->parent_overlap) { |
Ilya Dryomov | 980917f | 2016-09-12 18:59:42 +0200 | [diff] [blame] | 2939 | rbd_obj_request_submit(obj_request); |
| 2940 | return; |
Alex Elder | 02c74fb | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 2941 | } |
| 2942 | |
| 2943 | obj_request->result = img_result; |
Alex Elder | a9e8ba2c | 2013-04-21 00:32:07 -0500 | [diff] [blame] | 2944 | if (obj_request->result) |
| 2945 | goto out; |
| 2946 | |
| 2947 | /* |
| 2948 | * We need to zero anything beyond the parent overlap |
| 2949 | * boundary. Since rbd_img_obj_request_read_callback() |
| 2950 | * will zero anything beyond the end of a short read, an |
| 2951 | * easy way to do this is to pretend the data from the |
| 2952 | * parent came up short--ending at the overlap boundary. |
| 2953 | */ |
| 2954 | rbd_assert(obj_request->img_offset < U64_MAX - obj_request->length); |
| 2955 | obj_end = obj_request->img_offset + obj_request->length; |
Alex Elder | a9e8ba2c | 2013-04-21 00:32:07 -0500 | [diff] [blame] | 2956 | if (obj_end > rbd_dev->parent_overlap) { |
| 2957 | u64 xferred = 0; |
| 2958 | |
| 2959 | if (obj_request->img_offset < rbd_dev->parent_overlap) |
| 2960 | xferred = rbd_dev->parent_overlap - |
| 2961 | obj_request->img_offset; |
| 2962 | |
Alex Elder | 02c74fb | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 2963 | obj_request->xferred = min(img_xferred, xferred); |
Alex Elder | a9e8ba2c | 2013-04-21 00:32:07 -0500 | [diff] [blame] | 2964 | } else { |
Alex Elder | 02c74fb | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 2965 | obj_request->xferred = img_xferred; |
Alex Elder | a9e8ba2c | 2013-04-21 00:32:07 -0500 | [diff] [blame] | 2966 | } |
| 2967 | out: |
Alex Elder | 8b3e1a5 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 2968 | rbd_img_obj_request_read_callback(obj_request); |
| 2969 | rbd_obj_request_complete(obj_request); |
| 2970 | } |
| 2971 | |
| 2972 | static void rbd_img_parent_read(struct rbd_obj_request *obj_request) |
| 2973 | { |
Alex Elder | 8b3e1a5 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 2974 | struct rbd_img_request *img_request; |
| 2975 | int result; |
| 2976 | |
| 2977 | rbd_assert(obj_request_img_data_test(obj_request)); |
| 2978 | rbd_assert(obj_request->img_request != NULL); |
| 2979 | rbd_assert(obj_request->result == (s32) -ENOENT); |
Alex Elder | 5b2ab72 | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 2980 | rbd_assert(obj_request_type_valid(obj_request->type)); |
Alex Elder | 8b3e1a5 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 2981 | |
Alex Elder | 8b3e1a5 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 2982 | /* rbd_read_finish(obj_request, obj_request->length); */ |
Alex Elder | e93f315 | 2013-05-08 22:50:04 -0500 | [diff] [blame] | 2983 | img_request = rbd_parent_request_create(obj_request, |
Alex Elder | 8b3e1a5 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 2984 | obj_request->img_offset, |
Alex Elder | e93f315 | 2013-05-08 22:50:04 -0500 | [diff] [blame] | 2985 | obj_request->length); |
Alex Elder | 8b3e1a5 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 2986 | result = -ENOMEM; |
| 2987 | if (!img_request) |
| 2988 | goto out_err; |
| 2989 | |
Alex Elder | 5b2ab72 | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 2990 | if (obj_request->type == OBJ_REQUEST_BIO) |
| 2991 | result = rbd_img_request_fill(img_request, OBJ_REQUEST_BIO, |
| 2992 | obj_request->bio_list); |
| 2993 | else |
| 2994 | result = rbd_img_request_fill(img_request, OBJ_REQUEST_PAGES, |
| 2995 | obj_request->pages); |
Alex Elder | 8b3e1a5 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 2996 | if (result) |
| 2997 | goto out_err; |
| 2998 | |
| 2999 | img_request->callback = rbd_img_parent_read_callback; |
| 3000 | result = rbd_img_request_submit(img_request); |
| 3001 | if (result) |
| 3002 | goto out_err; |
| 3003 | |
| 3004 | return; |
| 3005 | out_err: |
| 3006 | if (img_request) |
| 3007 | rbd_img_request_put(img_request); |
| 3008 | obj_request->result = result; |
| 3009 | obj_request->xferred = 0; |
| 3010 | obj_request_done_set(obj_request); |
| 3011 | } |
| 3012 | |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3013 | static const struct rbd_client_id rbd_empty_cid; |
| 3014 | |
| 3015 | static bool rbd_cid_equal(const struct rbd_client_id *lhs, |
| 3016 | const struct rbd_client_id *rhs) |
| 3017 | { |
| 3018 | return lhs->gid == rhs->gid && lhs->handle == rhs->handle; |
| 3019 | } |
| 3020 | |
| 3021 | static struct rbd_client_id rbd_get_cid(struct rbd_device *rbd_dev) |
| 3022 | { |
| 3023 | struct rbd_client_id cid; |
| 3024 | |
| 3025 | mutex_lock(&rbd_dev->watch_mutex); |
| 3026 | cid.gid = ceph_client_gid(rbd_dev->rbd_client->client); |
| 3027 | cid.handle = rbd_dev->watch_cookie; |
| 3028 | mutex_unlock(&rbd_dev->watch_mutex); |
| 3029 | return cid; |
| 3030 | } |
| 3031 | |
| 3032 | /* |
| 3033 | * lock_rwsem must be held for write |
| 3034 | */ |
| 3035 | static void rbd_set_owner_cid(struct rbd_device *rbd_dev, |
| 3036 | const struct rbd_client_id *cid) |
| 3037 | { |
| 3038 | dout("%s rbd_dev %p %llu-%llu -> %llu-%llu\n", __func__, rbd_dev, |
| 3039 | rbd_dev->owner_cid.gid, rbd_dev->owner_cid.handle, |
| 3040 | cid->gid, cid->handle); |
| 3041 | rbd_dev->owner_cid = *cid; /* struct */ |
| 3042 | } |
| 3043 | |
| 3044 | static void format_lock_cookie(struct rbd_device *rbd_dev, char *buf) |
| 3045 | { |
| 3046 | mutex_lock(&rbd_dev->watch_mutex); |
| 3047 | sprintf(buf, "%s %llu", RBD_LOCK_COOKIE_PREFIX, rbd_dev->watch_cookie); |
| 3048 | mutex_unlock(&rbd_dev->watch_mutex); |
| 3049 | } |
| 3050 | |
| 3051 | /* |
| 3052 | * lock_rwsem must be held for write |
| 3053 | */ |
| 3054 | static int rbd_lock(struct rbd_device *rbd_dev) |
| 3055 | { |
| 3056 | struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc; |
| 3057 | struct rbd_client_id cid = rbd_get_cid(rbd_dev); |
| 3058 | char cookie[32]; |
| 3059 | int ret; |
| 3060 | |
Ilya Dryomov | cbbfb0f | 2017-04-13 12:17:38 +0200 | [diff] [blame] | 3061 | WARN_ON(__rbd_is_lock_owner(rbd_dev) || |
| 3062 | rbd_dev->lock_cookie[0] != '\0'); |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3063 | |
| 3064 | format_lock_cookie(rbd_dev, cookie); |
| 3065 | ret = ceph_cls_lock(osdc, &rbd_dev->header_oid, &rbd_dev->header_oloc, |
| 3066 | RBD_LOCK_NAME, CEPH_CLS_LOCK_EXCLUSIVE, cookie, |
| 3067 | RBD_LOCK_TAG, "", 0); |
| 3068 | if (ret) |
| 3069 | return ret; |
| 3070 | |
| 3071 | rbd_dev->lock_state = RBD_LOCK_STATE_LOCKED; |
Ilya Dryomov | cbbfb0f | 2017-04-13 12:17:38 +0200 | [diff] [blame] | 3072 | strcpy(rbd_dev->lock_cookie, cookie); |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3073 | rbd_set_owner_cid(rbd_dev, &cid); |
| 3074 | queue_work(rbd_dev->task_wq, &rbd_dev->acquired_lock_work); |
| 3075 | return 0; |
| 3076 | } |
| 3077 | |
| 3078 | /* |
| 3079 | * lock_rwsem must be held for write |
| 3080 | */ |
Ilya Dryomov | bbead74 | 2017-04-13 12:17:38 +0200 | [diff] [blame] | 3081 | static void rbd_unlock(struct rbd_device *rbd_dev) |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3082 | { |
| 3083 | struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc; |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3084 | int ret; |
| 3085 | |
Ilya Dryomov | cbbfb0f | 2017-04-13 12:17:38 +0200 | [diff] [blame] | 3086 | WARN_ON(!__rbd_is_lock_owner(rbd_dev) || |
| 3087 | rbd_dev->lock_cookie[0] == '\0'); |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3088 | |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3089 | ret = ceph_cls_unlock(osdc, &rbd_dev->header_oid, &rbd_dev->header_oloc, |
Ilya Dryomov | cbbfb0f | 2017-04-13 12:17:38 +0200 | [diff] [blame] | 3090 | RBD_LOCK_NAME, rbd_dev->lock_cookie); |
Ilya Dryomov | bbead74 | 2017-04-13 12:17:38 +0200 | [diff] [blame] | 3091 | if (ret && ret != -ENOENT) |
| 3092 | rbd_warn(rbd_dev, "failed to unlock: %d", ret); |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3093 | |
Ilya Dryomov | bbead74 | 2017-04-13 12:17:38 +0200 | [diff] [blame] | 3094 | /* treat errors as the image is unlocked */ |
| 3095 | rbd_dev->lock_state = RBD_LOCK_STATE_UNLOCKED; |
Ilya Dryomov | cbbfb0f | 2017-04-13 12:17:38 +0200 | [diff] [blame] | 3096 | rbd_dev->lock_cookie[0] = '\0'; |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3097 | rbd_set_owner_cid(rbd_dev, &rbd_empty_cid); |
| 3098 | queue_work(rbd_dev->task_wq, &rbd_dev->released_lock_work); |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3099 | } |
| 3100 | |
| 3101 | static int __rbd_notify_op_lock(struct rbd_device *rbd_dev, |
| 3102 | enum rbd_notify_op notify_op, |
| 3103 | struct page ***preply_pages, |
| 3104 | size_t *preply_len) |
| 3105 | { |
| 3106 | struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc; |
| 3107 | struct rbd_client_id cid = rbd_get_cid(rbd_dev); |
| 3108 | int buf_size = 4 + 8 + 8 + CEPH_ENCODING_START_BLK_LEN; |
| 3109 | char buf[buf_size]; |
| 3110 | void *p = buf; |
| 3111 | |
| 3112 | dout("%s rbd_dev %p notify_op %d\n", __func__, rbd_dev, notify_op); |
| 3113 | |
| 3114 | /* encode *LockPayload NotifyMessage (op + ClientId) */ |
| 3115 | ceph_start_encoding(&p, 2, 1, buf_size - CEPH_ENCODING_START_BLK_LEN); |
| 3116 | ceph_encode_32(&p, notify_op); |
| 3117 | ceph_encode_64(&p, cid.gid); |
| 3118 | ceph_encode_64(&p, cid.handle); |
| 3119 | |
| 3120 | return ceph_osdc_notify(osdc, &rbd_dev->header_oid, |
| 3121 | &rbd_dev->header_oloc, buf, buf_size, |
| 3122 | RBD_NOTIFY_TIMEOUT, preply_pages, preply_len); |
| 3123 | } |
| 3124 | |
| 3125 | static void rbd_notify_op_lock(struct rbd_device *rbd_dev, |
| 3126 | enum rbd_notify_op notify_op) |
| 3127 | { |
| 3128 | struct page **reply_pages; |
| 3129 | size_t reply_len; |
| 3130 | |
| 3131 | __rbd_notify_op_lock(rbd_dev, notify_op, &reply_pages, &reply_len); |
| 3132 | ceph_release_page_vector(reply_pages, calc_pages_for(0, reply_len)); |
| 3133 | } |
| 3134 | |
| 3135 | static void rbd_notify_acquired_lock(struct work_struct *work) |
| 3136 | { |
| 3137 | struct rbd_device *rbd_dev = container_of(work, struct rbd_device, |
| 3138 | acquired_lock_work); |
| 3139 | |
| 3140 | rbd_notify_op_lock(rbd_dev, RBD_NOTIFY_OP_ACQUIRED_LOCK); |
| 3141 | } |
| 3142 | |
| 3143 | static void rbd_notify_released_lock(struct work_struct *work) |
| 3144 | { |
| 3145 | struct rbd_device *rbd_dev = container_of(work, struct rbd_device, |
| 3146 | released_lock_work); |
| 3147 | |
| 3148 | rbd_notify_op_lock(rbd_dev, RBD_NOTIFY_OP_RELEASED_LOCK); |
| 3149 | } |
| 3150 | |
| 3151 | static int rbd_request_lock(struct rbd_device *rbd_dev) |
| 3152 | { |
| 3153 | struct page **reply_pages; |
| 3154 | size_t reply_len; |
| 3155 | bool lock_owner_responded = false; |
| 3156 | int ret; |
| 3157 | |
| 3158 | dout("%s rbd_dev %p\n", __func__, rbd_dev); |
| 3159 | |
| 3160 | ret = __rbd_notify_op_lock(rbd_dev, RBD_NOTIFY_OP_REQUEST_LOCK, |
| 3161 | &reply_pages, &reply_len); |
| 3162 | if (ret && ret != -ETIMEDOUT) { |
| 3163 | rbd_warn(rbd_dev, "failed to request lock: %d", ret); |
| 3164 | goto out; |
| 3165 | } |
| 3166 | |
| 3167 | if (reply_len > 0 && reply_len <= PAGE_SIZE) { |
| 3168 | void *p = page_address(reply_pages[0]); |
| 3169 | void *const end = p + reply_len; |
| 3170 | u32 n; |
| 3171 | |
| 3172 | ceph_decode_32_safe(&p, end, n, e_inval); /* num_acks */ |
| 3173 | while (n--) { |
| 3174 | u8 struct_v; |
| 3175 | u32 len; |
| 3176 | |
| 3177 | ceph_decode_need(&p, end, 8 + 8, e_inval); |
| 3178 | p += 8 + 8; /* skip gid and cookie */ |
| 3179 | |
| 3180 | ceph_decode_32_safe(&p, end, len, e_inval); |
| 3181 | if (!len) |
| 3182 | continue; |
| 3183 | |
| 3184 | if (lock_owner_responded) { |
| 3185 | rbd_warn(rbd_dev, |
| 3186 | "duplicate lock owners detected"); |
| 3187 | ret = -EIO; |
| 3188 | goto out; |
| 3189 | } |
| 3190 | |
| 3191 | lock_owner_responded = true; |
| 3192 | ret = ceph_start_decoding(&p, end, 1, "ResponseMessage", |
| 3193 | &struct_v, &len); |
| 3194 | if (ret) { |
| 3195 | rbd_warn(rbd_dev, |
| 3196 | "failed to decode ResponseMessage: %d", |
| 3197 | ret); |
| 3198 | goto e_inval; |
| 3199 | } |
| 3200 | |
| 3201 | ret = ceph_decode_32(&p); |
| 3202 | } |
| 3203 | } |
| 3204 | |
| 3205 | if (!lock_owner_responded) { |
| 3206 | rbd_warn(rbd_dev, "no lock owners detected"); |
| 3207 | ret = -ETIMEDOUT; |
| 3208 | } |
| 3209 | |
| 3210 | out: |
| 3211 | ceph_release_page_vector(reply_pages, calc_pages_for(0, reply_len)); |
| 3212 | return ret; |
| 3213 | |
| 3214 | e_inval: |
| 3215 | ret = -EINVAL; |
| 3216 | goto out; |
| 3217 | } |
| 3218 | |
| 3219 | static void wake_requests(struct rbd_device *rbd_dev, bool wake_all) |
| 3220 | { |
| 3221 | dout("%s rbd_dev %p wake_all %d\n", __func__, rbd_dev, wake_all); |
| 3222 | |
| 3223 | cancel_delayed_work(&rbd_dev->lock_dwork); |
| 3224 | if (wake_all) |
| 3225 | wake_up_all(&rbd_dev->lock_waitq); |
| 3226 | else |
| 3227 | wake_up(&rbd_dev->lock_waitq); |
| 3228 | } |
| 3229 | |
| 3230 | static int get_lock_owner_info(struct rbd_device *rbd_dev, |
| 3231 | struct ceph_locker **lockers, u32 *num_lockers) |
| 3232 | { |
| 3233 | struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc; |
| 3234 | u8 lock_type; |
| 3235 | char *lock_tag; |
| 3236 | int ret; |
| 3237 | |
| 3238 | dout("%s rbd_dev %p\n", __func__, rbd_dev); |
| 3239 | |
| 3240 | ret = ceph_cls_lock_info(osdc, &rbd_dev->header_oid, |
| 3241 | &rbd_dev->header_oloc, RBD_LOCK_NAME, |
| 3242 | &lock_type, &lock_tag, lockers, num_lockers); |
| 3243 | if (ret) |
| 3244 | return ret; |
| 3245 | |
| 3246 | if (*num_lockers == 0) { |
| 3247 | dout("%s rbd_dev %p no lockers detected\n", __func__, rbd_dev); |
| 3248 | goto out; |
| 3249 | } |
| 3250 | |
| 3251 | if (strcmp(lock_tag, RBD_LOCK_TAG)) { |
| 3252 | rbd_warn(rbd_dev, "locked by external mechanism, tag %s", |
| 3253 | lock_tag); |
| 3254 | ret = -EBUSY; |
| 3255 | goto out; |
| 3256 | } |
| 3257 | |
| 3258 | if (lock_type == CEPH_CLS_LOCK_SHARED) { |
| 3259 | rbd_warn(rbd_dev, "shared lock type detected"); |
| 3260 | ret = -EBUSY; |
| 3261 | goto out; |
| 3262 | } |
| 3263 | |
| 3264 | if (strncmp((*lockers)[0].id.cookie, RBD_LOCK_COOKIE_PREFIX, |
| 3265 | strlen(RBD_LOCK_COOKIE_PREFIX))) { |
| 3266 | rbd_warn(rbd_dev, "locked by external mechanism, cookie %s", |
| 3267 | (*lockers)[0].id.cookie); |
| 3268 | ret = -EBUSY; |
| 3269 | goto out; |
| 3270 | } |
| 3271 | |
| 3272 | out: |
| 3273 | kfree(lock_tag); |
| 3274 | return ret; |
| 3275 | } |
| 3276 | |
| 3277 | static int find_watcher(struct rbd_device *rbd_dev, |
| 3278 | const struct ceph_locker *locker) |
| 3279 | { |
| 3280 | struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc; |
| 3281 | struct ceph_watch_item *watchers; |
| 3282 | u32 num_watchers; |
| 3283 | u64 cookie; |
| 3284 | int i; |
| 3285 | int ret; |
| 3286 | |
| 3287 | ret = ceph_osdc_list_watchers(osdc, &rbd_dev->header_oid, |
| 3288 | &rbd_dev->header_oloc, &watchers, |
| 3289 | &num_watchers); |
| 3290 | if (ret) |
| 3291 | return ret; |
| 3292 | |
| 3293 | sscanf(locker->id.cookie, RBD_LOCK_COOKIE_PREFIX " %llu", &cookie); |
| 3294 | for (i = 0; i < num_watchers; i++) { |
| 3295 | if (!memcmp(&watchers[i].addr, &locker->info.addr, |
| 3296 | sizeof(locker->info.addr)) && |
| 3297 | watchers[i].cookie == cookie) { |
| 3298 | struct rbd_client_id cid = { |
| 3299 | .gid = le64_to_cpu(watchers[i].name.num), |
| 3300 | .handle = cookie, |
| 3301 | }; |
| 3302 | |
| 3303 | dout("%s rbd_dev %p found cid %llu-%llu\n", __func__, |
| 3304 | rbd_dev, cid.gid, cid.handle); |
| 3305 | rbd_set_owner_cid(rbd_dev, &cid); |
| 3306 | ret = 1; |
| 3307 | goto out; |
| 3308 | } |
| 3309 | } |
| 3310 | |
| 3311 | dout("%s rbd_dev %p no watchers\n", __func__, rbd_dev); |
| 3312 | ret = 0; |
| 3313 | out: |
| 3314 | kfree(watchers); |
| 3315 | return ret; |
| 3316 | } |
| 3317 | |
| 3318 | /* |
| 3319 | * lock_rwsem must be held for write |
| 3320 | */ |
| 3321 | static int rbd_try_lock(struct rbd_device *rbd_dev) |
| 3322 | { |
| 3323 | struct ceph_client *client = rbd_dev->rbd_client->client; |
| 3324 | struct ceph_locker *lockers; |
| 3325 | u32 num_lockers; |
| 3326 | int ret; |
| 3327 | |
| 3328 | for (;;) { |
| 3329 | ret = rbd_lock(rbd_dev); |
| 3330 | if (ret != -EBUSY) |
| 3331 | return ret; |
| 3332 | |
| 3333 | /* determine if the current lock holder is still alive */ |
| 3334 | ret = get_lock_owner_info(rbd_dev, &lockers, &num_lockers); |
| 3335 | if (ret) |
| 3336 | return ret; |
| 3337 | |
| 3338 | if (num_lockers == 0) |
| 3339 | goto again; |
| 3340 | |
| 3341 | ret = find_watcher(rbd_dev, lockers); |
| 3342 | if (ret) { |
| 3343 | if (ret > 0) |
| 3344 | ret = 0; /* have to request lock */ |
| 3345 | goto out; |
| 3346 | } |
| 3347 | |
| 3348 | rbd_warn(rbd_dev, "%s%llu seems dead, breaking lock", |
| 3349 | ENTITY_NAME(lockers[0].id.name)); |
| 3350 | |
| 3351 | ret = ceph_monc_blacklist_add(&client->monc, |
| 3352 | &lockers[0].info.addr); |
| 3353 | if (ret) { |
| 3354 | rbd_warn(rbd_dev, "blacklist of %s%llu failed: %d", |
| 3355 | ENTITY_NAME(lockers[0].id.name), ret); |
| 3356 | goto out; |
| 3357 | } |
| 3358 | |
| 3359 | ret = ceph_cls_break_lock(&client->osdc, &rbd_dev->header_oid, |
| 3360 | &rbd_dev->header_oloc, RBD_LOCK_NAME, |
| 3361 | lockers[0].id.cookie, |
| 3362 | &lockers[0].id.name); |
| 3363 | if (ret && ret != -ENOENT) |
| 3364 | goto out; |
| 3365 | |
| 3366 | again: |
| 3367 | ceph_free_lockers(lockers, num_lockers); |
| 3368 | } |
| 3369 | |
| 3370 | out: |
| 3371 | ceph_free_lockers(lockers, num_lockers); |
| 3372 | return ret; |
| 3373 | } |
| 3374 | |
| 3375 | /* |
| 3376 | * ret is set only if lock_state is RBD_LOCK_STATE_UNLOCKED |
| 3377 | */ |
| 3378 | static enum rbd_lock_state rbd_try_acquire_lock(struct rbd_device *rbd_dev, |
| 3379 | int *pret) |
| 3380 | { |
| 3381 | enum rbd_lock_state lock_state; |
| 3382 | |
| 3383 | down_read(&rbd_dev->lock_rwsem); |
| 3384 | dout("%s rbd_dev %p read lock_state %d\n", __func__, rbd_dev, |
| 3385 | rbd_dev->lock_state); |
| 3386 | if (__rbd_is_lock_owner(rbd_dev)) { |
| 3387 | lock_state = rbd_dev->lock_state; |
| 3388 | up_read(&rbd_dev->lock_rwsem); |
| 3389 | return lock_state; |
| 3390 | } |
| 3391 | |
| 3392 | up_read(&rbd_dev->lock_rwsem); |
| 3393 | down_write(&rbd_dev->lock_rwsem); |
| 3394 | dout("%s rbd_dev %p write lock_state %d\n", __func__, rbd_dev, |
| 3395 | rbd_dev->lock_state); |
| 3396 | if (!__rbd_is_lock_owner(rbd_dev)) { |
| 3397 | *pret = rbd_try_lock(rbd_dev); |
| 3398 | if (*pret) |
| 3399 | rbd_warn(rbd_dev, "failed to acquire lock: %d", *pret); |
| 3400 | } |
| 3401 | |
| 3402 | lock_state = rbd_dev->lock_state; |
| 3403 | up_write(&rbd_dev->lock_rwsem); |
| 3404 | return lock_state; |
| 3405 | } |
| 3406 | |
| 3407 | static void rbd_acquire_lock(struct work_struct *work) |
| 3408 | { |
| 3409 | struct rbd_device *rbd_dev = container_of(to_delayed_work(work), |
| 3410 | struct rbd_device, lock_dwork); |
| 3411 | enum rbd_lock_state lock_state; |
Kefeng Wang | 37f1325 | 2017-07-13 15:46:35 +0800 | [diff] [blame] | 3412 | int ret = 0; |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3413 | |
| 3414 | dout("%s rbd_dev %p\n", __func__, rbd_dev); |
| 3415 | again: |
| 3416 | lock_state = rbd_try_acquire_lock(rbd_dev, &ret); |
| 3417 | if (lock_state != RBD_LOCK_STATE_UNLOCKED || ret == -EBLACKLISTED) { |
| 3418 | if (lock_state == RBD_LOCK_STATE_LOCKED) |
| 3419 | wake_requests(rbd_dev, true); |
| 3420 | dout("%s rbd_dev %p lock_state %d ret %d - done\n", __func__, |
| 3421 | rbd_dev, lock_state, ret); |
| 3422 | return; |
| 3423 | } |
| 3424 | |
| 3425 | ret = rbd_request_lock(rbd_dev); |
| 3426 | if (ret == -ETIMEDOUT) { |
| 3427 | goto again; /* treat this as a dead client */ |
Ilya Dryomov | e010dd0 | 2017-04-13 12:17:39 +0200 | [diff] [blame] | 3428 | } else if (ret == -EROFS) { |
| 3429 | rbd_warn(rbd_dev, "peer will not release lock"); |
| 3430 | /* |
| 3431 | * If this is rbd_add_acquire_lock(), we want to fail |
| 3432 | * immediately -- reuse BLACKLISTED flag. Otherwise we |
| 3433 | * want to block. |
| 3434 | */ |
| 3435 | if (!(rbd_dev->disk->flags & GENHD_FL_UP)) { |
| 3436 | set_bit(RBD_DEV_FLAG_BLACKLISTED, &rbd_dev->flags); |
| 3437 | /* wake "rbd map --exclusive" process */ |
| 3438 | wake_requests(rbd_dev, false); |
| 3439 | } |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3440 | } else if (ret < 0) { |
| 3441 | rbd_warn(rbd_dev, "error requesting lock: %d", ret); |
| 3442 | mod_delayed_work(rbd_dev->task_wq, &rbd_dev->lock_dwork, |
| 3443 | RBD_RETRY_DELAY); |
| 3444 | } else { |
| 3445 | /* |
| 3446 | * lock owner acked, but resend if we don't see them |
| 3447 | * release the lock |
| 3448 | */ |
| 3449 | dout("%s rbd_dev %p requeueing lock_dwork\n", __func__, |
| 3450 | rbd_dev); |
| 3451 | mod_delayed_work(rbd_dev->task_wq, &rbd_dev->lock_dwork, |
| 3452 | msecs_to_jiffies(2 * RBD_NOTIFY_TIMEOUT * MSEC_PER_SEC)); |
| 3453 | } |
| 3454 | } |
| 3455 | |
| 3456 | /* |
| 3457 | * lock_rwsem must be held for write |
| 3458 | */ |
| 3459 | static bool rbd_release_lock(struct rbd_device *rbd_dev) |
| 3460 | { |
| 3461 | dout("%s rbd_dev %p read lock_state %d\n", __func__, rbd_dev, |
| 3462 | rbd_dev->lock_state); |
| 3463 | if (rbd_dev->lock_state != RBD_LOCK_STATE_LOCKED) |
| 3464 | return false; |
| 3465 | |
| 3466 | rbd_dev->lock_state = RBD_LOCK_STATE_RELEASING; |
| 3467 | downgrade_write(&rbd_dev->lock_rwsem); |
| 3468 | /* |
| 3469 | * Ensure that all in-flight IO is flushed. |
| 3470 | * |
| 3471 | * FIXME: ceph_osdc_sync() flushes the entire OSD client, which |
| 3472 | * may be shared with other devices. |
| 3473 | */ |
| 3474 | ceph_osdc_sync(&rbd_dev->rbd_client->client->osdc); |
| 3475 | up_read(&rbd_dev->lock_rwsem); |
| 3476 | |
| 3477 | down_write(&rbd_dev->lock_rwsem); |
| 3478 | dout("%s rbd_dev %p write lock_state %d\n", __func__, rbd_dev, |
| 3479 | rbd_dev->lock_state); |
| 3480 | if (rbd_dev->lock_state != RBD_LOCK_STATE_RELEASING) |
| 3481 | return false; |
| 3482 | |
Ilya Dryomov | bbead74 | 2017-04-13 12:17:38 +0200 | [diff] [blame] | 3483 | rbd_unlock(rbd_dev); |
| 3484 | /* |
| 3485 | * Give others a chance to grab the lock - we would re-acquire |
| 3486 | * almost immediately if we got new IO during ceph_osdc_sync() |
| 3487 | * otherwise. We need to ack our own notifications, so this |
| 3488 | * lock_dwork will be requeued from rbd_wait_state_locked() |
| 3489 | * after wake_requests() in rbd_handle_released_lock(). |
| 3490 | */ |
| 3491 | cancel_delayed_work(&rbd_dev->lock_dwork); |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3492 | return true; |
| 3493 | } |
| 3494 | |
| 3495 | static void rbd_release_lock_work(struct work_struct *work) |
| 3496 | { |
| 3497 | struct rbd_device *rbd_dev = container_of(work, struct rbd_device, |
| 3498 | unlock_work); |
| 3499 | |
| 3500 | down_write(&rbd_dev->lock_rwsem); |
| 3501 | rbd_release_lock(rbd_dev); |
| 3502 | up_write(&rbd_dev->lock_rwsem); |
| 3503 | } |
| 3504 | |
| 3505 | static void rbd_handle_acquired_lock(struct rbd_device *rbd_dev, u8 struct_v, |
| 3506 | void **p) |
| 3507 | { |
| 3508 | struct rbd_client_id cid = { 0 }; |
| 3509 | |
| 3510 | if (struct_v >= 2) { |
| 3511 | cid.gid = ceph_decode_64(p); |
| 3512 | cid.handle = ceph_decode_64(p); |
| 3513 | } |
| 3514 | |
| 3515 | dout("%s rbd_dev %p cid %llu-%llu\n", __func__, rbd_dev, cid.gid, |
| 3516 | cid.handle); |
| 3517 | if (!rbd_cid_equal(&cid, &rbd_empty_cid)) { |
| 3518 | down_write(&rbd_dev->lock_rwsem); |
| 3519 | if (rbd_cid_equal(&cid, &rbd_dev->owner_cid)) { |
| 3520 | /* |
| 3521 | * we already know that the remote client is |
| 3522 | * the owner |
| 3523 | */ |
| 3524 | up_write(&rbd_dev->lock_rwsem); |
| 3525 | return; |
| 3526 | } |
| 3527 | |
| 3528 | rbd_set_owner_cid(rbd_dev, &cid); |
| 3529 | downgrade_write(&rbd_dev->lock_rwsem); |
| 3530 | } else { |
| 3531 | down_read(&rbd_dev->lock_rwsem); |
| 3532 | } |
| 3533 | |
| 3534 | if (!__rbd_is_lock_owner(rbd_dev)) |
| 3535 | wake_requests(rbd_dev, false); |
| 3536 | up_read(&rbd_dev->lock_rwsem); |
| 3537 | } |
| 3538 | |
| 3539 | static void rbd_handle_released_lock(struct rbd_device *rbd_dev, u8 struct_v, |
| 3540 | void **p) |
| 3541 | { |
| 3542 | struct rbd_client_id cid = { 0 }; |
| 3543 | |
| 3544 | if (struct_v >= 2) { |
| 3545 | cid.gid = ceph_decode_64(p); |
| 3546 | cid.handle = ceph_decode_64(p); |
| 3547 | } |
| 3548 | |
| 3549 | dout("%s rbd_dev %p cid %llu-%llu\n", __func__, rbd_dev, cid.gid, |
| 3550 | cid.handle); |
| 3551 | if (!rbd_cid_equal(&cid, &rbd_empty_cid)) { |
| 3552 | down_write(&rbd_dev->lock_rwsem); |
| 3553 | if (!rbd_cid_equal(&cid, &rbd_dev->owner_cid)) { |
| 3554 | dout("%s rbd_dev %p unexpected owner, cid %llu-%llu != owner_cid %llu-%llu\n", |
| 3555 | __func__, rbd_dev, cid.gid, cid.handle, |
| 3556 | rbd_dev->owner_cid.gid, rbd_dev->owner_cid.handle); |
| 3557 | up_write(&rbd_dev->lock_rwsem); |
| 3558 | return; |
| 3559 | } |
| 3560 | |
| 3561 | rbd_set_owner_cid(rbd_dev, &rbd_empty_cid); |
| 3562 | downgrade_write(&rbd_dev->lock_rwsem); |
| 3563 | } else { |
| 3564 | down_read(&rbd_dev->lock_rwsem); |
| 3565 | } |
| 3566 | |
| 3567 | if (!__rbd_is_lock_owner(rbd_dev)) |
| 3568 | wake_requests(rbd_dev, false); |
| 3569 | up_read(&rbd_dev->lock_rwsem); |
| 3570 | } |
| 3571 | |
Ilya Dryomov | 3b77faa | 2017-04-13 12:17:39 +0200 | [diff] [blame] | 3572 | /* |
| 3573 | * Returns result for ResponseMessage to be encoded (<= 0), or 1 if no |
| 3574 | * ResponseMessage is needed. |
| 3575 | */ |
| 3576 | static int rbd_handle_request_lock(struct rbd_device *rbd_dev, u8 struct_v, |
| 3577 | void **p) |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3578 | { |
| 3579 | struct rbd_client_id my_cid = rbd_get_cid(rbd_dev); |
| 3580 | struct rbd_client_id cid = { 0 }; |
Ilya Dryomov | 3b77faa | 2017-04-13 12:17:39 +0200 | [diff] [blame] | 3581 | int result = 1; |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3582 | |
| 3583 | if (struct_v >= 2) { |
| 3584 | cid.gid = ceph_decode_64(p); |
| 3585 | cid.handle = ceph_decode_64(p); |
| 3586 | } |
| 3587 | |
| 3588 | dout("%s rbd_dev %p cid %llu-%llu\n", __func__, rbd_dev, cid.gid, |
| 3589 | cid.handle); |
| 3590 | if (rbd_cid_equal(&cid, &my_cid)) |
Ilya Dryomov | 3b77faa | 2017-04-13 12:17:39 +0200 | [diff] [blame] | 3591 | return result; |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3592 | |
| 3593 | down_read(&rbd_dev->lock_rwsem); |
Ilya Dryomov | 3b77faa | 2017-04-13 12:17:39 +0200 | [diff] [blame] | 3594 | if (__rbd_is_lock_owner(rbd_dev)) { |
| 3595 | if (rbd_dev->lock_state == RBD_LOCK_STATE_LOCKED && |
| 3596 | rbd_cid_equal(&rbd_dev->owner_cid, &rbd_empty_cid)) |
| 3597 | goto out_unlock; |
| 3598 | |
| 3599 | /* |
| 3600 | * encode ResponseMessage(0) so the peer can detect |
| 3601 | * a missing owner |
| 3602 | */ |
| 3603 | result = 0; |
| 3604 | |
| 3605 | if (rbd_dev->lock_state == RBD_LOCK_STATE_LOCKED) { |
Ilya Dryomov | e010dd0 | 2017-04-13 12:17:39 +0200 | [diff] [blame] | 3606 | if (!rbd_dev->opts->exclusive) { |
| 3607 | dout("%s rbd_dev %p queueing unlock_work\n", |
| 3608 | __func__, rbd_dev); |
| 3609 | queue_work(rbd_dev->task_wq, |
| 3610 | &rbd_dev->unlock_work); |
| 3611 | } else { |
| 3612 | /* refuse to release the lock */ |
| 3613 | result = -EROFS; |
| 3614 | } |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3615 | } |
| 3616 | } |
Ilya Dryomov | 3b77faa | 2017-04-13 12:17:39 +0200 | [diff] [blame] | 3617 | |
| 3618 | out_unlock: |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3619 | up_read(&rbd_dev->lock_rwsem); |
Ilya Dryomov | 3b77faa | 2017-04-13 12:17:39 +0200 | [diff] [blame] | 3620 | return result; |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3621 | } |
| 3622 | |
| 3623 | static void __rbd_acknowledge_notify(struct rbd_device *rbd_dev, |
| 3624 | u64 notify_id, u64 cookie, s32 *result) |
| 3625 | { |
| 3626 | struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc; |
| 3627 | int buf_size = 4 + CEPH_ENCODING_START_BLK_LEN; |
| 3628 | char buf[buf_size]; |
| 3629 | int ret; |
| 3630 | |
| 3631 | if (result) { |
| 3632 | void *p = buf; |
| 3633 | |
| 3634 | /* encode ResponseMessage */ |
| 3635 | ceph_start_encoding(&p, 1, 1, |
| 3636 | buf_size - CEPH_ENCODING_START_BLK_LEN); |
| 3637 | ceph_encode_32(&p, *result); |
| 3638 | } else { |
| 3639 | buf_size = 0; |
| 3640 | } |
| 3641 | |
| 3642 | ret = ceph_osdc_notify_ack(osdc, &rbd_dev->header_oid, |
| 3643 | &rbd_dev->header_oloc, notify_id, cookie, |
| 3644 | buf, buf_size); |
| 3645 | if (ret) |
| 3646 | rbd_warn(rbd_dev, "acknowledge_notify failed: %d", ret); |
| 3647 | } |
| 3648 | |
| 3649 | static void rbd_acknowledge_notify(struct rbd_device *rbd_dev, u64 notify_id, |
| 3650 | u64 cookie) |
| 3651 | { |
| 3652 | dout("%s rbd_dev %p\n", __func__, rbd_dev); |
| 3653 | __rbd_acknowledge_notify(rbd_dev, notify_id, cookie, NULL); |
| 3654 | } |
| 3655 | |
| 3656 | static void rbd_acknowledge_notify_result(struct rbd_device *rbd_dev, |
| 3657 | u64 notify_id, u64 cookie, s32 result) |
| 3658 | { |
| 3659 | dout("%s rbd_dev %p result %d\n", __func__, rbd_dev, result); |
| 3660 | __rbd_acknowledge_notify(rbd_dev, notify_id, cookie, &result); |
| 3661 | } |
Ilya Dryomov | 922dab6 | 2016-05-26 01:15:02 +0200 | [diff] [blame] | 3662 | |
| 3663 | static void rbd_watch_cb(void *arg, u64 notify_id, u64 cookie, |
| 3664 | u64 notifier_id, void *data, size_t data_len) |
Alex Elder | b8d7003 | 2012-11-30 17:53:04 -0600 | [diff] [blame] | 3665 | { |
Ilya Dryomov | 922dab6 | 2016-05-26 01:15:02 +0200 | [diff] [blame] | 3666 | struct rbd_device *rbd_dev = arg; |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3667 | void *p = data; |
| 3668 | void *const end = p + data_len; |
Ilya Dryomov | d4c2269 | 2016-09-06 11:15:48 +0200 | [diff] [blame] | 3669 | u8 struct_v = 0; |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3670 | u32 len; |
| 3671 | u32 notify_op; |
Alex Elder | b8d7003 | 2012-11-30 17:53:04 -0600 | [diff] [blame] | 3672 | int ret; |
| 3673 | |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3674 | dout("%s rbd_dev %p cookie %llu notify_id %llu data_len %zu\n", |
| 3675 | __func__, rbd_dev, cookie, notify_id, data_len); |
| 3676 | if (data_len) { |
| 3677 | ret = ceph_start_decoding(&p, end, 1, "NotifyMessage", |
| 3678 | &struct_v, &len); |
| 3679 | if (ret) { |
| 3680 | rbd_warn(rbd_dev, "failed to decode NotifyMessage: %d", |
| 3681 | ret); |
| 3682 | return; |
| 3683 | } |
Ilya Dryomov | 52bb1f9 | 2014-07-23 17:11:20 +0400 | [diff] [blame] | 3684 | |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3685 | notify_op = ceph_decode_32(&p); |
| 3686 | } else { |
| 3687 | /* legacy notification for header updates */ |
| 3688 | notify_op = RBD_NOTIFY_OP_HEADER_UPDATE; |
| 3689 | len = 0; |
| 3690 | } |
Alex Elder | b8d7003 | 2012-11-30 17:53:04 -0600 | [diff] [blame] | 3691 | |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3692 | dout("%s rbd_dev %p notify_op %u\n", __func__, rbd_dev, notify_op); |
| 3693 | switch (notify_op) { |
| 3694 | case RBD_NOTIFY_OP_ACQUIRED_LOCK: |
| 3695 | rbd_handle_acquired_lock(rbd_dev, struct_v, &p); |
| 3696 | rbd_acknowledge_notify(rbd_dev, notify_id, cookie); |
| 3697 | break; |
| 3698 | case RBD_NOTIFY_OP_RELEASED_LOCK: |
| 3699 | rbd_handle_released_lock(rbd_dev, struct_v, &p); |
| 3700 | rbd_acknowledge_notify(rbd_dev, notify_id, cookie); |
| 3701 | break; |
| 3702 | case RBD_NOTIFY_OP_REQUEST_LOCK: |
Ilya Dryomov | 3b77faa | 2017-04-13 12:17:39 +0200 | [diff] [blame] | 3703 | ret = rbd_handle_request_lock(rbd_dev, struct_v, &p); |
| 3704 | if (ret <= 0) |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3705 | rbd_acknowledge_notify_result(rbd_dev, notify_id, |
Ilya Dryomov | 3b77faa | 2017-04-13 12:17:39 +0200 | [diff] [blame] | 3706 | cookie, ret); |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3707 | else |
| 3708 | rbd_acknowledge_notify(rbd_dev, notify_id, cookie); |
| 3709 | break; |
| 3710 | case RBD_NOTIFY_OP_HEADER_UPDATE: |
| 3711 | ret = rbd_dev_refresh(rbd_dev); |
| 3712 | if (ret) |
| 3713 | rbd_warn(rbd_dev, "refresh failed: %d", ret); |
| 3714 | |
| 3715 | rbd_acknowledge_notify(rbd_dev, notify_id, cookie); |
| 3716 | break; |
| 3717 | default: |
| 3718 | if (rbd_is_lock_owner(rbd_dev)) |
| 3719 | rbd_acknowledge_notify_result(rbd_dev, notify_id, |
| 3720 | cookie, -EOPNOTSUPP); |
| 3721 | else |
| 3722 | rbd_acknowledge_notify(rbd_dev, notify_id, cookie); |
| 3723 | break; |
| 3724 | } |
Alex Elder | b8d7003 | 2012-11-30 17:53:04 -0600 | [diff] [blame] | 3725 | } |
| 3726 | |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3727 | static void __rbd_unregister_watch(struct rbd_device *rbd_dev); |
| 3728 | |
Ilya Dryomov | 922dab6 | 2016-05-26 01:15:02 +0200 | [diff] [blame] | 3729 | static void rbd_watch_errcb(void *arg, u64 cookie, int err) |
Ilya Dryomov | bb040aa | 2014-06-19 11:38:14 +0400 | [diff] [blame] | 3730 | { |
Ilya Dryomov | 922dab6 | 2016-05-26 01:15:02 +0200 | [diff] [blame] | 3731 | struct rbd_device *rbd_dev = arg; |
Ilya Dryomov | bb040aa | 2014-06-19 11:38:14 +0400 | [diff] [blame] | 3732 | |
Ilya Dryomov | 922dab6 | 2016-05-26 01:15:02 +0200 | [diff] [blame] | 3733 | rbd_warn(rbd_dev, "encountered watch error: %d", err); |
Ilya Dryomov | bb040aa | 2014-06-19 11:38:14 +0400 | [diff] [blame] | 3734 | |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3735 | down_write(&rbd_dev->lock_rwsem); |
| 3736 | rbd_set_owner_cid(rbd_dev, &rbd_empty_cid); |
| 3737 | up_write(&rbd_dev->lock_rwsem); |
Ilya Dryomov | bb040aa | 2014-06-19 11:38:14 +0400 | [diff] [blame] | 3738 | |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3739 | mutex_lock(&rbd_dev->watch_mutex); |
| 3740 | if (rbd_dev->watch_state == RBD_WATCH_STATE_REGISTERED) { |
| 3741 | __rbd_unregister_watch(rbd_dev); |
| 3742 | rbd_dev->watch_state = RBD_WATCH_STATE_ERROR; |
Ilya Dryomov | bb040aa | 2014-06-19 11:38:14 +0400 | [diff] [blame] | 3743 | |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3744 | queue_delayed_work(rbd_dev->task_wq, &rbd_dev->watch_dwork, 0); |
Ilya Dryomov | bb040aa | 2014-06-19 11:38:14 +0400 | [diff] [blame] | 3745 | } |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3746 | mutex_unlock(&rbd_dev->watch_mutex); |
Ilya Dryomov | bb040aa | 2014-06-19 11:38:14 +0400 | [diff] [blame] | 3747 | } |
| 3748 | |
| 3749 | /* |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3750 | * watch_mutex must be locked |
Alex Elder | 9969ebc | 2013-01-18 12:31:10 -0600 | [diff] [blame] | 3751 | */ |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3752 | static int __rbd_register_watch(struct rbd_device *rbd_dev) |
Alex Elder | 9969ebc | 2013-01-18 12:31:10 -0600 | [diff] [blame] | 3753 | { |
| 3754 | struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc; |
Ilya Dryomov | 922dab6 | 2016-05-26 01:15:02 +0200 | [diff] [blame] | 3755 | struct ceph_osd_linger_request *handle; |
Alex Elder | 9969ebc | 2013-01-18 12:31:10 -0600 | [diff] [blame] | 3756 | |
Ilya Dryomov | 922dab6 | 2016-05-26 01:15:02 +0200 | [diff] [blame] | 3757 | rbd_assert(!rbd_dev->watch_handle); |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3758 | dout("%s rbd_dev %p\n", __func__, rbd_dev); |
Alex Elder | 9969ebc | 2013-01-18 12:31:10 -0600 | [diff] [blame] | 3759 | |
Ilya Dryomov | 922dab6 | 2016-05-26 01:15:02 +0200 | [diff] [blame] | 3760 | handle = ceph_osdc_watch(osdc, &rbd_dev->header_oid, |
| 3761 | &rbd_dev->header_oloc, rbd_watch_cb, |
| 3762 | rbd_watch_errcb, rbd_dev); |
| 3763 | if (IS_ERR(handle)) |
| 3764 | return PTR_ERR(handle); |
Alex Elder | 9969ebc | 2013-01-18 12:31:10 -0600 | [diff] [blame] | 3765 | |
Ilya Dryomov | 922dab6 | 2016-05-26 01:15:02 +0200 | [diff] [blame] | 3766 | rbd_dev->watch_handle = handle; |
Ilya Dryomov | b30a01f | 2014-05-22 19:28:52 +0400 | [diff] [blame] | 3767 | return 0; |
Alex Elder | 9969ebc | 2013-01-18 12:31:10 -0600 | [diff] [blame] | 3768 | } |
| 3769 | |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3770 | /* |
| 3771 | * watch_mutex must be locked |
| 3772 | */ |
| 3773 | static void __rbd_unregister_watch(struct rbd_device *rbd_dev) |
Ilya Dryomov | fca2706 | 2013-12-16 18:02:40 +0200 | [diff] [blame] | 3774 | { |
Ilya Dryomov | 922dab6 | 2016-05-26 01:15:02 +0200 | [diff] [blame] | 3775 | struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc; |
| 3776 | int ret; |
Ilya Dryomov | b30a01f | 2014-05-22 19:28:52 +0400 | [diff] [blame] | 3777 | |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3778 | rbd_assert(rbd_dev->watch_handle); |
| 3779 | dout("%s rbd_dev %p\n", __func__, rbd_dev); |
Ilya Dryomov | b30a01f | 2014-05-22 19:28:52 +0400 | [diff] [blame] | 3780 | |
Ilya Dryomov | 922dab6 | 2016-05-26 01:15:02 +0200 | [diff] [blame] | 3781 | ret = ceph_osdc_unwatch(osdc, rbd_dev->watch_handle); |
| 3782 | if (ret) |
| 3783 | rbd_warn(rbd_dev, "failed to unwatch: %d", ret); |
Ilya Dryomov | b30a01f | 2014-05-22 19:28:52 +0400 | [diff] [blame] | 3784 | |
Ilya Dryomov | 922dab6 | 2016-05-26 01:15:02 +0200 | [diff] [blame] | 3785 | rbd_dev->watch_handle = NULL; |
Ilya Dryomov | c525f03 | 2016-04-28 16:07:26 +0200 | [diff] [blame] | 3786 | } |
| 3787 | |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3788 | static int rbd_register_watch(struct rbd_device *rbd_dev) |
Ilya Dryomov | c525f03 | 2016-04-28 16:07:26 +0200 | [diff] [blame] | 3789 | { |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3790 | int ret; |
Ilya Dryomov | 811c668 | 2016-04-15 16:22:16 +0200 | [diff] [blame] | 3791 | |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3792 | mutex_lock(&rbd_dev->watch_mutex); |
| 3793 | rbd_assert(rbd_dev->watch_state == RBD_WATCH_STATE_UNREGISTERED); |
| 3794 | ret = __rbd_register_watch(rbd_dev); |
| 3795 | if (ret) |
| 3796 | goto out; |
| 3797 | |
| 3798 | rbd_dev->watch_state = RBD_WATCH_STATE_REGISTERED; |
| 3799 | rbd_dev->watch_cookie = rbd_dev->watch_handle->linger_id; |
| 3800 | |
| 3801 | out: |
| 3802 | mutex_unlock(&rbd_dev->watch_mutex); |
| 3803 | return ret; |
| 3804 | } |
| 3805 | |
| 3806 | static void cancel_tasks_sync(struct rbd_device *rbd_dev) |
| 3807 | { |
| 3808 | dout("%s rbd_dev %p\n", __func__, rbd_dev); |
| 3809 | |
| 3810 | cancel_delayed_work_sync(&rbd_dev->watch_dwork); |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3811 | cancel_work_sync(&rbd_dev->acquired_lock_work); |
| 3812 | cancel_work_sync(&rbd_dev->released_lock_work); |
| 3813 | cancel_delayed_work_sync(&rbd_dev->lock_dwork); |
| 3814 | cancel_work_sync(&rbd_dev->unlock_work); |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3815 | } |
| 3816 | |
| 3817 | static void rbd_unregister_watch(struct rbd_device *rbd_dev) |
| 3818 | { |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3819 | WARN_ON(waitqueue_active(&rbd_dev->lock_waitq)); |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3820 | cancel_tasks_sync(rbd_dev); |
| 3821 | |
| 3822 | mutex_lock(&rbd_dev->watch_mutex); |
| 3823 | if (rbd_dev->watch_state == RBD_WATCH_STATE_REGISTERED) |
| 3824 | __rbd_unregister_watch(rbd_dev); |
| 3825 | rbd_dev->watch_state = RBD_WATCH_STATE_UNREGISTERED; |
| 3826 | mutex_unlock(&rbd_dev->watch_mutex); |
| 3827 | |
Ilya Dryomov | 811c668 | 2016-04-15 16:22:16 +0200 | [diff] [blame] | 3828 | ceph_osdc_flush_notifies(&rbd_dev->rbd_client->client->osdc); |
Ilya Dryomov | fca2706 | 2013-12-16 18:02:40 +0200 | [diff] [blame] | 3829 | } |
| 3830 | |
Ilya Dryomov | 14bb211 | 2017-04-13 12:17:38 +0200 | [diff] [blame] | 3831 | /* |
| 3832 | * lock_rwsem must be held for write |
| 3833 | */ |
| 3834 | static void rbd_reacquire_lock(struct rbd_device *rbd_dev) |
| 3835 | { |
| 3836 | struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc; |
| 3837 | char cookie[32]; |
| 3838 | int ret; |
| 3839 | |
| 3840 | WARN_ON(rbd_dev->lock_state != RBD_LOCK_STATE_LOCKED); |
| 3841 | |
| 3842 | format_lock_cookie(rbd_dev, cookie); |
| 3843 | ret = ceph_cls_set_cookie(osdc, &rbd_dev->header_oid, |
| 3844 | &rbd_dev->header_oloc, RBD_LOCK_NAME, |
| 3845 | CEPH_CLS_LOCK_EXCLUSIVE, rbd_dev->lock_cookie, |
| 3846 | RBD_LOCK_TAG, cookie); |
| 3847 | if (ret) { |
| 3848 | if (ret != -EOPNOTSUPP) |
| 3849 | rbd_warn(rbd_dev, "failed to update lock cookie: %d", |
| 3850 | ret); |
| 3851 | |
| 3852 | /* |
| 3853 | * Lock cookie cannot be updated on older OSDs, so do |
| 3854 | * a manual release and queue an acquire. |
| 3855 | */ |
| 3856 | if (rbd_release_lock(rbd_dev)) |
| 3857 | queue_delayed_work(rbd_dev->task_wq, |
| 3858 | &rbd_dev->lock_dwork, 0); |
| 3859 | } else { |
| 3860 | strcpy(rbd_dev->lock_cookie, cookie); |
| 3861 | } |
| 3862 | } |
| 3863 | |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3864 | static void rbd_reregister_watch(struct work_struct *work) |
| 3865 | { |
| 3866 | struct rbd_device *rbd_dev = container_of(to_delayed_work(work), |
| 3867 | struct rbd_device, watch_dwork); |
| 3868 | int ret; |
| 3869 | |
| 3870 | dout("%s rbd_dev %p\n", __func__, rbd_dev); |
| 3871 | |
| 3872 | mutex_lock(&rbd_dev->watch_mutex); |
Ilya Dryomov | 87c0fde | 2016-09-29 13:41:05 +0200 | [diff] [blame] | 3873 | if (rbd_dev->watch_state != RBD_WATCH_STATE_ERROR) { |
| 3874 | mutex_unlock(&rbd_dev->watch_mutex); |
Ilya Dryomov | 14bb211 | 2017-04-13 12:17:38 +0200 | [diff] [blame] | 3875 | return; |
Ilya Dryomov | 87c0fde | 2016-09-29 13:41:05 +0200 | [diff] [blame] | 3876 | } |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3877 | |
| 3878 | ret = __rbd_register_watch(rbd_dev); |
| 3879 | if (ret) { |
| 3880 | rbd_warn(rbd_dev, "failed to reregister watch: %d", ret); |
Ilya Dryomov | 4d73644 | 2016-09-29 14:23:12 +0200 | [diff] [blame] | 3881 | if (ret == -EBLACKLISTED || ret == -ENOENT) { |
Ilya Dryomov | 87c0fde | 2016-09-29 13:41:05 +0200 | [diff] [blame] | 3882 | set_bit(RBD_DEV_FLAG_BLACKLISTED, &rbd_dev->flags); |
Ilya Dryomov | 14bb211 | 2017-04-13 12:17:38 +0200 | [diff] [blame] | 3883 | wake_requests(rbd_dev, true); |
Ilya Dryomov | 87c0fde | 2016-09-29 13:41:05 +0200 | [diff] [blame] | 3884 | } else { |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3885 | queue_delayed_work(rbd_dev->task_wq, |
| 3886 | &rbd_dev->watch_dwork, |
| 3887 | RBD_RETRY_DELAY); |
Ilya Dryomov | 87c0fde | 2016-09-29 13:41:05 +0200 | [diff] [blame] | 3888 | } |
| 3889 | mutex_unlock(&rbd_dev->watch_mutex); |
Ilya Dryomov | 14bb211 | 2017-04-13 12:17:38 +0200 | [diff] [blame] | 3890 | return; |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3891 | } |
| 3892 | |
| 3893 | rbd_dev->watch_state = RBD_WATCH_STATE_REGISTERED; |
| 3894 | rbd_dev->watch_cookie = rbd_dev->watch_handle->linger_id; |
| 3895 | mutex_unlock(&rbd_dev->watch_mutex); |
| 3896 | |
Ilya Dryomov | 14bb211 | 2017-04-13 12:17:38 +0200 | [diff] [blame] | 3897 | down_write(&rbd_dev->lock_rwsem); |
| 3898 | if (rbd_dev->lock_state == RBD_LOCK_STATE_LOCKED) |
| 3899 | rbd_reacquire_lock(rbd_dev); |
| 3900 | up_write(&rbd_dev->lock_rwsem); |
| 3901 | |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3902 | ret = rbd_dev_refresh(rbd_dev); |
| 3903 | if (ret) |
| 3904 | rbd_warn(rbd_dev, "reregisteration refresh failed: %d", ret); |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3905 | } |
| 3906 | |
Alex Elder | 36be9a7 | 2013-01-19 00:30:28 -0600 | [diff] [blame] | 3907 | /* |
Alex Elder | f40eb34 | 2013-04-25 15:09:42 -0500 | [diff] [blame] | 3908 | * Synchronous osd object method call. Returns the number of bytes |
| 3909 | * returned in the outbound buffer, or a negative error code. |
Alex Elder | 36be9a7 | 2013-01-19 00:30:28 -0600 | [diff] [blame] | 3910 | */ |
| 3911 | static int rbd_obj_method_sync(struct rbd_device *rbd_dev, |
Ilya Dryomov | ecd4a68 | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 3912 | struct ceph_object_id *oid, |
| 3913 | struct ceph_object_locator *oloc, |
Alex Elder | 36be9a7 | 2013-01-19 00:30:28 -0600 | [diff] [blame] | 3914 | const char *method_name, |
Alex Elder | 4157976 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 3915 | const void *outbound, |
Alex Elder | 36be9a7 | 2013-01-19 00:30:28 -0600 | [diff] [blame] | 3916 | size_t outbound_size, |
Alex Elder | 4157976 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 3917 | void *inbound, |
Alex Elder | e2a58ee | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 3918 | size_t inbound_size) |
Alex Elder | 36be9a7 | 2013-01-19 00:30:28 -0600 | [diff] [blame] | 3919 | { |
Ilya Dryomov | ecd4a68 | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 3920 | struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc; |
| 3921 | struct page *req_page = NULL; |
| 3922 | struct page *reply_page; |
Alex Elder | 36be9a7 | 2013-01-19 00:30:28 -0600 | [diff] [blame] | 3923 | int ret; |
| 3924 | |
| 3925 | /* |
Alex Elder | 6010a45 | 2013-04-05 01:27:11 -0500 | [diff] [blame] | 3926 | * Method calls are ultimately read operations. The result |
| 3927 | * should placed into the inbound buffer provided. They |
| 3928 | * also supply outbound data--parameters for the object |
| 3929 | * method. Currently if this is present it will be a |
| 3930 | * snapshot id. |
Alex Elder | 36be9a7 | 2013-01-19 00:30:28 -0600 | [diff] [blame] | 3931 | */ |
Ilya Dryomov | ecd4a68 | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 3932 | if (outbound) { |
| 3933 | if (outbound_size > PAGE_SIZE) |
| 3934 | return -E2BIG; |
Alex Elder | 36be9a7 | 2013-01-19 00:30:28 -0600 | [diff] [blame] | 3935 | |
Ilya Dryomov | ecd4a68 | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 3936 | req_page = alloc_page(GFP_KERNEL); |
| 3937 | if (!req_page) |
| 3938 | return -ENOMEM; |
Alex Elder | 36be9a7 | 2013-01-19 00:30:28 -0600 | [diff] [blame] | 3939 | |
Ilya Dryomov | ecd4a68 | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 3940 | memcpy(page_address(req_page), outbound, outbound_size); |
Alex Elder | 04017e2 | 2013-04-05 14:46:02 -0500 | [diff] [blame] | 3941 | } |
Alex Elder | 430c28c | 2013-04-03 21:32:51 -0500 | [diff] [blame] | 3942 | |
Ilya Dryomov | ecd4a68 | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 3943 | reply_page = alloc_page(GFP_KERNEL); |
| 3944 | if (!reply_page) { |
| 3945 | if (req_page) |
| 3946 | __free_page(req_page); |
| 3947 | return -ENOMEM; |
| 3948 | } |
Alex Elder | 36be9a7 | 2013-01-19 00:30:28 -0600 | [diff] [blame] | 3949 | |
Ilya Dryomov | ecd4a68 | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 3950 | ret = ceph_osdc_call(osdc, oid, oloc, RBD_DRV_NAME, method_name, |
| 3951 | CEPH_OSD_FLAG_READ, req_page, outbound_size, |
| 3952 | reply_page, &inbound_size); |
| 3953 | if (!ret) { |
| 3954 | memcpy(inbound, page_address(reply_page), inbound_size); |
| 3955 | ret = inbound_size; |
| 3956 | } |
Alex Elder | 57385b5 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 3957 | |
Ilya Dryomov | ecd4a68 | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 3958 | if (req_page) |
| 3959 | __free_page(req_page); |
| 3960 | __free_page(reply_page); |
Alex Elder | 36be9a7 | 2013-01-19 00:30:28 -0600 | [diff] [blame] | 3961 | return ret; |
| 3962 | } |
| 3963 | |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3964 | /* |
| 3965 | * lock_rwsem must be held for read |
| 3966 | */ |
| 3967 | static void rbd_wait_state_locked(struct rbd_device *rbd_dev) |
| 3968 | { |
| 3969 | DEFINE_WAIT(wait); |
| 3970 | |
| 3971 | do { |
| 3972 | /* |
| 3973 | * Note the use of mod_delayed_work() in rbd_acquire_lock() |
| 3974 | * and cancel_delayed_work() in wake_requests(). |
| 3975 | */ |
| 3976 | dout("%s rbd_dev %p queueing lock_dwork\n", __func__, rbd_dev); |
| 3977 | queue_delayed_work(rbd_dev->task_wq, &rbd_dev->lock_dwork, 0); |
| 3978 | prepare_to_wait_exclusive(&rbd_dev->lock_waitq, &wait, |
| 3979 | TASK_UNINTERRUPTIBLE); |
| 3980 | up_read(&rbd_dev->lock_rwsem); |
| 3981 | schedule(); |
| 3982 | down_read(&rbd_dev->lock_rwsem); |
Ilya Dryomov | 87c0fde | 2016-09-29 13:41:05 +0200 | [diff] [blame] | 3983 | } while (rbd_dev->lock_state != RBD_LOCK_STATE_LOCKED && |
| 3984 | !test_bit(RBD_DEV_FLAG_BLACKLISTED, &rbd_dev->flags)); |
| 3985 | |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3986 | finish_wait(&rbd_dev->lock_waitq, &wait); |
| 3987 | } |
| 3988 | |
Christoph Hellwig | 7ad18af | 2015-01-13 17:20:04 +0100 | [diff] [blame] | 3989 | static void rbd_queue_workfn(struct work_struct *work) |
Ilya Dryomov | bc1ecc6 | 2014-08-04 18:04:39 +0400 | [diff] [blame] | 3990 | { |
Christoph Hellwig | 7ad18af | 2015-01-13 17:20:04 +0100 | [diff] [blame] | 3991 | struct request *rq = blk_mq_rq_from_pdu(work); |
| 3992 | struct rbd_device *rbd_dev = rq->q->queuedata; |
Ilya Dryomov | bc1ecc6 | 2014-08-04 18:04:39 +0400 | [diff] [blame] | 3993 | struct rbd_img_request *img_request; |
Josh Durgin | 4e752f0 | 2014-04-08 11:12:11 -0700 | [diff] [blame] | 3994 | struct ceph_snap_context *snapc = NULL; |
Ilya Dryomov | bc1ecc6 | 2014-08-04 18:04:39 +0400 | [diff] [blame] | 3995 | u64 offset = (u64)blk_rq_pos(rq) << SECTOR_SHIFT; |
| 3996 | u64 length = blk_rq_bytes(rq); |
Guangliang Zhao | 6d2940c | 2014-03-13 11:21:35 +0800 | [diff] [blame] | 3997 | enum obj_operation_type op_type; |
Josh Durgin | 4e752f0 | 2014-04-08 11:12:11 -0700 | [diff] [blame] | 3998 | u64 mapping_size; |
Ilya Dryomov | 80de191 | 2016-09-20 14:23:17 +0200 | [diff] [blame] | 3999 | bool must_be_locked; |
Ilya Dryomov | bc1ecc6 | 2014-08-04 18:04:39 +0400 | [diff] [blame] | 4000 | int result; |
| 4001 | |
Christoph Hellwig | aebf526 | 2017-01-31 16:57:31 +0100 | [diff] [blame] | 4002 | switch (req_op(rq)) { |
| 4003 | case REQ_OP_DISCARD: |
Ilya Dryomov | 6ac5695 | 2017-05-22 19:59:24 +0200 | [diff] [blame] | 4004 | case REQ_OP_WRITE_ZEROES: |
Christoph Hellwig | aebf526 | 2017-01-31 16:57:31 +0100 | [diff] [blame] | 4005 | op_type = OBJ_OP_DISCARD; |
| 4006 | break; |
| 4007 | case REQ_OP_WRITE: |
| 4008 | op_type = OBJ_OP_WRITE; |
| 4009 | break; |
| 4010 | case REQ_OP_READ: |
| 4011 | op_type = OBJ_OP_READ; |
| 4012 | break; |
| 4013 | default: |
| 4014 | dout("%s: non-fs request type %d\n", __func__, req_op(rq)); |
Christoph Hellwig | 7ad18af | 2015-01-13 17:20:04 +0100 | [diff] [blame] | 4015 | result = -EIO; |
| 4016 | goto err; |
| 4017 | } |
| 4018 | |
Ilya Dryomov | bc1ecc6 | 2014-08-04 18:04:39 +0400 | [diff] [blame] | 4019 | /* Ignore/skip any zero-length requests */ |
| 4020 | |
| 4021 | if (!length) { |
| 4022 | dout("%s: zero-length request\n", __func__); |
| 4023 | result = 0; |
| 4024 | goto err_rq; |
| 4025 | } |
| 4026 | |
Ilya Dryomov | 9568c93 | 2017-10-12 12:35:19 +0200 | [diff] [blame^] | 4027 | rbd_assert(op_type == OBJ_OP_READ || |
| 4028 | rbd_dev->spec->snap_id == CEPH_NOSNAP); |
Ilya Dryomov | bc1ecc6 | 2014-08-04 18:04:39 +0400 | [diff] [blame] | 4029 | |
| 4030 | /* |
| 4031 | * Quit early if the mapped snapshot no longer exists. It's |
| 4032 | * still possible the snapshot will have disappeared by the |
| 4033 | * time our request arrives at the osd, but there's no sense in |
| 4034 | * sending it if we already know. |
| 4035 | */ |
| 4036 | if (!test_bit(RBD_DEV_FLAG_EXISTS, &rbd_dev->flags)) { |
| 4037 | dout("request for non-existent snapshot"); |
| 4038 | rbd_assert(rbd_dev->spec->snap_id != CEPH_NOSNAP); |
| 4039 | result = -ENXIO; |
| 4040 | goto err_rq; |
| 4041 | } |
| 4042 | |
| 4043 | if (offset && length > U64_MAX - offset + 1) { |
| 4044 | rbd_warn(rbd_dev, "bad request range (%llu~%llu)", offset, |
| 4045 | length); |
| 4046 | result = -EINVAL; |
| 4047 | goto err_rq; /* Shouldn't happen */ |
| 4048 | } |
| 4049 | |
Christoph Hellwig | 7ad18af | 2015-01-13 17:20:04 +0100 | [diff] [blame] | 4050 | blk_mq_start_request(rq); |
| 4051 | |
Josh Durgin | 4e752f0 | 2014-04-08 11:12:11 -0700 | [diff] [blame] | 4052 | down_read(&rbd_dev->header_rwsem); |
| 4053 | mapping_size = rbd_dev->mapping.size; |
Guangliang Zhao | 6d2940c | 2014-03-13 11:21:35 +0800 | [diff] [blame] | 4054 | if (op_type != OBJ_OP_READ) { |
Josh Durgin | 4e752f0 | 2014-04-08 11:12:11 -0700 | [diff] [blame] | 4055 | snapc = rbd_dev->header.snapc; |
| 4056 | ceph_get_snap_context(snapc); |
| 4057 | } |
| 4058 | up_read(&rbd_dev->header_rwsem); |
| 4059 | |
| 4060 | if (offset + length > mapping_size) { |
Ilya Dryomov | bc1ecc6 | 2014-08-04 18:04:39 +0400 | [diff] [blame] | 4061 | rbd_warn(rbd_dev, "beyond EOD (%llu~%llu > %llu)", offset, |
Josh Durgin | 4e752f0 | 2014-04-08 11:12:11 -0700 | [diff] [blame] | 4062 | length, mapping_size); |
Ilya Dryomov | bc1ecc6 | 2014-08-04 18:04:39 +0400 | [diff] [blame] | 4063 | result = -EIO; |
| 4064 | goto err_rq; |
| 4065 | } |
| 4066 | |
Ilya Dryomov | f9bebd5 | 2017-04-13 12:17:39 +0200 | [diff] [blame] | 4067 | must_be_locked = |
| 4068 | (rbd_dev->header.features & RBD_FEATURE_EXCLUSIVE_LOCK) && |
| 4069 | (op_type != OBJ_OP_READ || rbd_dev->opts->lock_on_read); |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 4070 | if (must_be_locked) { |
| 4071 | down_read(&rbd_dev->lock_rwsem); |
Ilya Dryomov | 87c0fde | 2016-09-29 13:41:05 +0200 | [diff] [blame] | 4072 | if (rbd_dev->lock_state != RBD_LOCK_STATE_LOCKED && |
Ilya Dryomov | e010dd0 | 2017-04-13 12:17:39 +0200 | [diff] [blame] | 4073 | !test_bit(RBD_DEV_FLAG_BLACKLISTED, &rbd_dev->flags)) { |
| 4074 | if (rbd_dev->opts->exclusive) { |
| 4075 | rbd_warn(rbd_dev, "exclusive lock required"); |
| 4076 | result = -EROFS; |
| 4077 | goto err_unlock; |
| 4078 | } |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 4079 | rbd_wait_state_locked(rbd_dev); |
Ilya Dryomov | e010dd0 | 2017-04-13 12:17:39 +0200 | [diff] [blame] | 4080 | } |
Ilya Dryomov | 87c0fde | 2016-09-29 13:41:05 +0200 | [diff] [blame] | 4081 | if (test_bit(RBD_DEV_FLAG_BLACKLISTED, &rbd_dev->flags)) { |
| 4082 | result = -EBLACKLISTED; |
| 4083 | goto err_unlock; |
| 4084 | } |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 4085 | } |
| 4086 | |
Guangliang Zhao | 6d2940c | 2014-03-13 11:21:35 +0800 | [diff] [blame] | 4087 | img_request = rbd_img_request_create(rbd_dev, offset, length, op_type, |
Josh Durgin | 4e752f0 | 2014-04-08 11:12:11 -0700 | [diff] [blame] | 4088 | snapc); |
Ilya Dryomov | bc1ecc6 | 2014-08-04 18:04:39 +0400 | [diff] [blame] | 4089 | if (!img_request) { |
| 4090 | result = -ENOMEM; |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 4091 | goto err_unlock; |
Ilya Dryomov | bc1ecc6 | 2014-08-04 18:04:39 +0400 | [diff] [blame] | 4092 | } |
| 4093 | img_request->rq = rq; |
Ilya Dryomov | 70b16db | 2015-11-27 19:23:24 +0100 | [diff] [blame] | 4094 | snapc = NULL; /* img_request consumes a ref */ |
Ilya Dryomov | bc1ecc6 | 2014-08-04 18:04:39 +0400 | [diff] [blame] | 4095 | |
Guangliang Zhao | 90e98c5 | 2014-04-01 22:22:16 +0800 | [diff] [blame] | 4096 | if (op_type == OBJ_OP_DISCARD) |
| 4097 | result = rbd_img_request_fill(img_request, OBJ_REQUEST_NODATA, |
| 4098 | NULL); |
| 4099 | else |
| 4100 | result = rbd_img_request_fill(img_request, OBJ_REQUEST_BIO, |
| 4101 | rq->bio); |
Ilya Dryomov | bc1ecc6 | 2014-08-04 18:04:39 +0400 | [diff] [blame] | 4102 | if (result) |
| 4103 | goto err_img_request; |
| 4104 | |
| 4105 | result = rbd_img_request_submit(img_request); |
| 4106 | if (result) |
| 4107 | goto err_img_request; |
| 4108 | |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 4109 | if (must_be_locked) |
| 4110 | up_read(&rbd_dev->lock_rwsem); |
Ilya Dryomov | bc1ecc6 | 2014-08-04 18:04:39 +0400 | [diff] [blame] | 4111 | return; |
| 4112 | |
| 4113 | err_img_request: |
| 4114 | rbd_img_request_put(img_request); |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 4115 | err_unlock: |
| 4116 | if (must_be_locked) |
| 4117 | up_read(&rbd_dev->lock_rwsem); |
Ilya Dryomov | bc1ecc6 | 2014-08-04 18:04:39 +0400 | [diff] [blame] | 4118 | err_rq: |
| 4119 | if (result) |
| 4120 | rbd_warn(rbd_dev, "%s %llx at %llx result %d", |
Guangliang Zhao | 6d2940c | 2014-03-13 11:21:35 +0800 | [diff] [blame] | 4121 | obj_op_name(op_type), length, offset, result); |
SF Markus Elfring | e96a650 | 2014-11-02 15:20:59 +0100 | [diff] [blame] | 4122 | ceph_put_snap_context(snapc); |
Christoph Hellwig | 7ad18af | 2015-01-13 17:20:04 +0100 | [diff] [blame] | 4123 | err: |
Christoph Hellwig | 2a842ac | 2017-06-03 09:38:04 +0200 | [diff] [blame] | 4124 | blk_mq_end_request(rq, errno_to_blk_status(result)); |
Ilya Dryomov | bc1ecc6 | 2014-08-04 18:04:39 +0400 | [diff] [blame] | 4125 | } |
| 4126 | |
Christoph Hellwig | fc17b65 | 2017-06-03 09:38:05 +0200 | [diff] [blame] | 4127 | static blk_status_t rbd_queue_rq(struct blk_mq_hw_ctx *hctx, |
Christoph Hellwig | 7ad18af | 2015-01-13 17:20:04 +0100 | [diff] [blame] | 4128 | const struct blk_mq_queue_data *bd) |
Ilya Dryomov | bc1ecc6 | 2014-08-04 18:04:39 +0400 | [diff] [blame] | 4129 | { |
Christoph Hellwig | 7ad18af | 2015-01-13 17:20:04 +0100 | [diff] [blame] | 4130 | struct request *rq = bd->rq; |
| 4131 | struct work_struct *work = blk_mq_rq_to_pdu(rq); |
Ilya Dryomov | bc1ecc6 | 2014-08-04 18:04:39 +0400 | [diff] [blame] | 4132 | |
Christoph Hellwig | 7ad18af | 2015-01-13 17:20:04 +0100 | [diff] [blame] | 4133 | queue_work(rbd_wq, work); |
Christoph Hellwig | fc17b65 | 2017-06-03 09:38:05 +0200 | [diff] [blame] | 4134 | return BLK_STS_OK; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 4135 | } |
| 4136 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 4137 | static void rbd_free_disk(struct rbd_device *rbd_dev) |
| 4138 | { |
Ilya Dryomov | 5769ed0 | 2017-04-13 12:17:38 +0200 | [diff] [blame] | 4139 | blk_cleanup_queue(rbd_dev->disk->queue); |
| 4140 | blk_mq_free_tag_set(&rbd_dev->tag_set); |
| 4141 | put_disk(rbd_dev->disk); |
Alex Elder | a0cab92 | 2013-04-25 23:15:08 -0500 | [diff] [blame] | 4142 | rbd_dev->disk = NULL; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 4143 | } |
| 4144 | |
Alex Elder | 788e2df | 2013-01-17 12:25:27 -0600 | [diff] [blame] | 4145 | static int rbd_obj_read_sync(struct rbd_device *rbd_dev, |
Ilya Dryomov | fe5478e | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 4146 | struct ceph_object_id *oid, |
| 4147 | struct ceph_object_locator *oloc, |
| 4148 | void *buf, int buf_len) |
Alex Elder | 788e2df | 2013-01-17 12:25:27 -0600 | [diff] [blame] | 4149 | |
| 4150 | { |
Ilya Dryomov | fe5478e | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 4151 | struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc; |
| 4152 | struct ceph_osd_request *req; |
| 4153 | struct page **pages; |
| 4154 | int num_pages = calc_pages_for(0, buf_len); |
Alex Elder | 788e2df | 2013-01-17 12:25:27 -0600 | [diff] [blame] | 4155 | int ret; |
| 4156 | |
Ilya Dryomov | fe5478e | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 4157 | req = ceph_osdc_alloc_request(osdc, NULL, 1, false, GFP_KERNEL); |
| 4158 | if (!req) |
| 4159 | return -ENOMEM; |
Alex Elder | 788e2df | 2013-01-17 12:25:27 -0600 | [diff] [blame] | 4160 | |
Ilya Dryomov | fe5478e | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 4161 | ceph_oid_copy(&req->r_base_oid, oid); |
| 4162 | ceph_oloc_copy(&req->r_base_oloc, oloc); |
| 4163 | req->r_flags = CEPH_OSD_FLAG_READ; |
Alex Elder | 788e2df | 2013-01-17 12:25:27 -0600 | [diff] [blame] | 4164 | |
Ilya Dryomov | fe5478e | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 4165 | ret = ceph_osdc_alloc_messages(req, GFP_KERNEL); |
Alex Elder | 788e2df | 2013-01-17 12:25:27 -0600 | [diff] [blame] | 4166 | if (ret) |
Ilya Dryomov | fe5478e | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 4167 | goto out_req; |
Alex Elder | 788e2df | 2013-01-17 12:25:27 -0600 | [diff] [blame] | 4168 | |
Ilya Dryomov | fe5478e | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 4169 | pages = ceph_alloc_page_vector(num_pages, GFP_KERNEL); |
| 4170 | if (IS_ERR(pages)) { |
| 4171 | ret = PTR_ERR(pages); |
| 4172 | goto out_req; |
| 4173 | } |
Alex Elder | 1ceae7e | 2013-02-06 13:11:38 -0600 | [diff] [blame] | 4174 | |
Ilya Dryomov | fe5478e | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 4175 | osd_req_op_extent_init(req, 0, CEPH_OSD_OP_READ, 0, buf_len, 0, 0); |
| 4176 | osd_req_op_extent_osd_data_pages(req, 0, pages, buf_len, 0, false, |
| 4177 | true); |
Alex Elder | 788e2df | 2013-01-17 12:25:27 -0600 | [diff] [blame] | 4178 | |
Ilya Dryomov | fe5478e | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 4179 | ceph_osdc_start_request(osdc, req, false); |
| 4180 | ret = ceph_osdc_wait_request(osdc, req); |
| 4181 | if (ret >= 0) |
| 4182 | ceph_copy_from_page_vector(pages, buf, 0, ret); |
| 4183 | |
| 4184 | out_req: |
| 4185 | ceph_osdc_put_request(req); |
Alex Elder | 788e2df | 2013-01-17 12:25:27 -0600 | [diff] [blame] | 4186 | return ret; |
| 4187 | } |
| 4188 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 4189 | /* |
Alex Elder | 662518b | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 4190 | * Read the complete header for the given rbd device. On successful |
| 4191 | * return, the rbd_dev->header field will contain up-to-date |
| 4192 | * information about the image. |
Alex Elder | 4156d99 | 2012-08-02 11:29:46 -0500 | [diff] [blame] | 4193 | */ |
Alex Elder | 99a41eb | 2013-05-06 09:51:30 -0500 | [diff] [blame] | 4194 | static int rbd_dev_v1_header_info(struct rbd_device *rbd_dev) |
Alex Elder | 4156d99 | 2012-08-02 11:29:46 -0500 | [diff] [blame] | 4195 | { |
| 4196 | struct rbd_image_header_ondisk *ondisk = NULL; |
| 4197 | u32 snap_count = 0; |
| 4198 | u64 names_size = 0; |
| 4199 | u32 want_count; |
| 4200 | int ret; |
| 4201 | |
| 4202 | /* |
| 4203 | * The complete header will include an array of its 64-bit |
| 4204 | * snapshot ids, followed by the names of those snapshots as |
| 4205 | * a contiguous block of NUL-terminated strings. Note that |
| 4206 | * the number of snapshots could change by the time we read |
| 4207 | * it in, in which case we re-read it. |
| 4208 | */ |
| 4209 | do { |
| 4210 | size_t size; |
| 4211 | |
| 4212 | kfree(ondisk); |
| 4213 | |
| 4214 | size = sizeof (*ondisk); |
| 4215 | size += snap_count * sizeof (struct rbd_image_snap_ondisk); |
| 4216 | size += names_size; |
| 4217 | ondisk = kmalloc(size, GFP_KERNEL); |
| 4218 | if (!ondisk) |
Alex Elder | 662518b | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 4219 | return -ENOMEM; |
Alex Elder | 4156d99 | 2012-08-02 11:29:46 -0500 | [diff] [blame] | 4220 | |
Ilya Dryomov | fe5478e | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 4221 | ret = rbd_obj_read_sync(rbd_dev, &rbd_dev->header_oid, |
| 4222 | &rbd_dev->header_oloc, ondisk, size); |
Alex Elder | 4156d99 | 2012-08-02 11:29:46 -0500 | [diff] [blame] | 4223 | if (ret < 0) |
Alex Elder | 662518b | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 4224 | goto out; |
Alex Elder | c0cd10db | 2013-04-26 09:43:47 -0500 | [diff] [blame] | 4225 | if ((size_t)ret < size) { |
Alex Elder | 4156d99 | 2012-08-02 11:29:46 -0500 | [diff] [blame] | 4226 | ret = -ENXIO; |
Alex Elder | 06ecc6c | 2012-11-01 10:17:15 -0500 | [diff] [blame] | 4227 | rbd_warn(rbd_dev, "short header read (want %zd got %d)", |
| 4228 | size, ret); |
Alex Elder | 662518b | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 4229 | goto out; |
Alex Elder | 4156d99 | 2012-08-02 11:29:46 -0500 | [diff] [blame] | 4230 | } |
| 4231 | if (!rbd_dev_ondisk_valid(ondisk)) { |
| 4232 | ret = -ENXIO; |
Alex Elder | 06ecc6c | 2012-11-01 10:17:15 -0500 | [diff] [blame] | 4233 | rbd_warn(rbd_dev, "invalid header"); |
Alex Elder | 662518b | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 4234 | goto out; |
Alex Elder | 4156d99 | 2012-08-02 11:29:46 -0500 | [diff] [blame] | 4235 | } |
| 4236 | |
| 4237 | names_size = le64_to_cpu(ondisk->snap_names_len); |
| 4238 | want_count = snap_count; |
| 4239 | snap_count = le32_to_cpu(ondisk->snap_count); |
| 4240 | } while (snap_count != want_count); |
| 4241 | |
Alex Elder | 662518b | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 4242 | ret = rbd_header_from_disk(rbd_dev, ondisk); |
| 4243 | out: |
Alex Elder | 4156d99 | 2012-08-02 11:29:46 -0500 | [diff] [blame] | 4244 | kfree(ondisk); |
| 4245 | |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4246 | return ret; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 4247 | } |
| 4248 | |
Alex Elder | 15228ed | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 4249 | /* |
| 4250 | * Clear the rbd device's EXISTS flag if the snapshot it's mapped to |
| 4251 | * has disappeared from the (just updated) snapshot context. |
| 4252 | */ |
| 4253 | static void rbd_exists_validate(struct rbd_device *rbd_dev) |
| 4254 | { |
| 4255 | u64 snap_id; |
| 4256 | |
| 4257 | if (!test_bit(RBD_DEV_FLAG_EXISTS, &rbd_dev->flags)) |
| 4258 | return; |
| 4259 | |
| 4260 | snap_id = rbd_dev->spec->snap_id; |
| 4261 | if (snap_id == CEPH_NOSNAP) |
| 4262 | return; |
| 4263 | |
| 4264 | if (rbd_dev_snap_index(rbd_dev, snap_id) == BAD_SNAP_INDEX) |
| 4265 | clear_bit(RBD_DEV_FLAG_EXISTS, &rbd_dev->flags); |
| 4266 | } |
| 4267 | |
Josh Durgin | 9875201 | 2013-08-29 17:26:31 -0700 | [diff] [blame] | 4268 | static void rbd_dev_update_size(struct rbd_device *rbd_dev) |
| 4269 | { |
| 4270 | sector_t size; |
Josh Durgin | 9875201 | 2013-08-29 17:26:31 -0700 | [diff] [blame] | 4271 | |
| 4272 | /* |
Ilya Dryomov | 811c668 | 2016-04-15 16:22:16 +0200 | [diff] [blame] | 4273 | * If EXISTS is not set, rbd_dev->disk may be NULL, so don't |
| 4274 | * try to update its size. If REMOVING is set, updating size |
| 4275 | * is just useless work since the device can't be opened. |
Josh Durgin | 9875201 | 2013-08-29 17:26:31 -0700 | [diff] [blame] | 4276 | */ |
Ilya Dryomov | 811c668 | 2016-04-15 16:22:16 +0200 | [diff] [blame] | 4277 | if (test_bit(RBD_DEV_FLAG_EXISTS, &rbd_dev->flags) && |
| 4278 | !test_bit(RBD_DEV_FLAG_REMOVING, &rbd_dev->flags)) { |
Josh Durgin | 9875201 | 2013-08-29 17:26:31 -0700 | [diff] [blame] | 4279 | size = (sector_t)rbd_dev->mapping.size / SECTOR_SIZE; |
| 4280 | dout("setting size to %llu sectors", (unsigned long long)size); |
| 4281 | set_capacity(rbd_dev->disk, size); |
| 4282 | revalidate_disk(rbd_dev->disk); |
| 4283 | } |
| 4284 | } |
| 4285 | |
Alex Elder | cc4a38bd | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 4286 | static int rbd_dev_refresh(struct rbd_device *rbd_dev) |
Alex Elder | 1fe5e99 | 2012-07-25 09:32:41 -0500 | [diff] [blame] | 4287 | { |
Alex Elder | e627db0 | 2013-05-06 07:40:30 -0500 | [diff] [blame] | 4288 | u64 mapping_size; |
Alex Elder | 1fe5e99 | 2012-07-25 09:32:41 -0500 | [diff] [blame] | 4289 | int ret; |
| 4290 | |
Alex Elder | cfbf637 | 2013-05-31 17:40:45 -0500 | [diff] [blame] | 4291 | down_write(&rbd_dev->header_rwsem); |
Alex Elder | 3b5cf2a | 2013-05-29 11:18:59 -0500 | [diff] [blame] | 4292 | mapping_size = rbd_dev->mapping.size; |
Ilya Dryomov | a720ae0 | 2014-07-23 17:11:19 +0400 | [diff] [blame] | 4293 | |
| 4294 | ret = rbd_dev_header_info(rbd_dev); |
Ilya Dryomov | 52bb1f9 | 2014-07-23 17:11:20 +0400 | [diff] [blame] | 4295 | if (ret) |
Ilya Dryomov | 73e39e4 | 2015-01-08 20:18:22 +0300 | [diff] [blame] | 4296 | goto out; |
Alex Elder | 15228ed | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 4297 | |
Ilya Dryomov | e8f59b5 | 2014-07-24 10:42:13 +0400 | [diff] [blame] | 4298 | /* |
| 4299 | * If there is a parent, see if it has disappeared due to the |
| 4300 | * mapped image getting flattened. |
| 4301 | */ |
| 4302 | if (rbd_dev->parent) { |
| 4303 | ret = rbd_dev_v2_parent_info(rbd_dev); |
| 4304 | if (ret) |
Ilya Dryomov | 73e39e4 | 2015-01-08 20:18:22 +0300 | [diff] [blame] | 4305 | goto out; |
Ilya Dryomov | e8f59b5 | 2014-07-24 10:42:13 +0400 | [diff] [blame] | 4306 | } |
| 4307 | |
Ilya Dryomov | 5ff1108 | 2014-07-23 17:11:21 +0400 | [diff] [blame] | 4308 | if (rbd_dev->spec->snap_id == CEPH_NOSNAP) { |
Ilya Dryomov | 73e39e4 | 2015-01-08 20:18:22 +0300 | [diff] [blame] | 4309 | rbd_dev->mapping.size = rbd_dev->header.image_size; |
Ilya Dryomov | 5ff1108 | 2014-07-23 17:11:21 +0400 | [diff] [blame] | 4310 | } else { |
| 4311 | /* validate mapped snapshot's EXISTS flag */ |
| 4312 | rbd_exists_validate(rbd_dev); |
| 4313 | } |
Alex Elder | 15228ed | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 4314 | |
Ilya Dryomov | 73e39e4 | 2015-01-08 20:18:22 +0300 | [diff] [blame] | 4315 | out: |
Alex Elder | cfbf637 | 2013-05-31 17:40:45 -0500 | [diff] [blame] | 4316 | up_write(&rbd_dev->header_rwsem); |
Ilya Dryomov | 73e39e4 | 2015-01-08 20:18:22 +0300 | [diff] [blame] | 4317 | if (!ret && mapping_size != rbd_dev->mapping.size) |
Josh Durgin | 9875201 | 2013-08-29 17:26:31 -0700 | [diff] [blame] | 4318 | rbd_dev_update_size(rbd_dev); |
Alex Elder | 1fe5e99 | 2012-07-25 09:32:41 -0500 | [diff] [blame] | 4319 | |
Ilya Dryomov | 73e39e4 | 2015-01-08 20:18:22 +0300 | [diff] [blame] | 4320 | return ret; |
Alex Elder | 1fe5e99 | 2012-07-25 09:32:41 -0500 | [diff] [blame] | 4321 | } |
| 4322 | |
Christoph Hellwig | d6296d3 | 2017-05-01 10:19:08 -0600 | [diff] [blame] | 4323 | static int rbd_init_request(struct blk_mq_tag_set *set, struct request *rq, |
| 4324 | unsigned int hctx_idx, unsigned int numa_node) |
Christoph Hellwig | 7ad18af | 2015-01-13 17:20:04 +0100 | [diff] [blame] | 4325 | { |
| 4326 | struct work_struct *work = blk_mq_rq_to_pdu(rq); |
| 4327 | |
| 4328 | INIT_WORK(work, rbd_queue_workfn); |
| 4329 | return 0; |
| 4330 | } |
| 4331 | |
Eric Biggers | f363b08 | 2017-03-30 13:39:16 -0700 | [diff] [blame] | 4332 | static const struct blk_mq_ops rbd_mq_ops = { |
Christoph Hellwig | 7ad18af | 2015-01-13 17:20:04 +0100 | [diff] [blame] | 4333 | .queue_rq = rbd_queue_rq, |
Christoph Hellwig | 7ad18af | 2015-01-13 17:20:04 +0100 | [diff] [blame] | 4334 | .init_request = rbd_init_request, |
| 4335 | }; |
| 4336 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 4337 | static int rbd_init_disk(struct rbd_device *rbd_dev) |
| 4338 | { |
| 4339 | struct gendisk *disk; |
| 4340 | struct request_queue *q; |
Alex Elder | 593a9e7 | 2012-02-07 12:03:37 -0600 | [diff] [blame] | 4341 | u64 segment_size; |
Christoph Hellwig | 7ad18af | 2015-01-13 17:20:04 +0100 | [diff] [blame] | 4342 | int err; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 4343 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 4344 | /* create gendisk info */ |
Ilya Dryomov | 7e513d4 | 2013-12-16 19:26:32 +0200 | [diff] [blame] | 4345 | disk = alloc_disk(single_major ? |
| 4346 | (1 << RBD_SINGLE_MAJOR_PART_SHIFT) : |
| 4347 | RBD_MINORS_PER_MAJOR); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 4348 | if (!disk) |
Alex Elder | 1fcdb8a | 2012-08-29 17:11:06 -0500 | [diff] [blame] | 4349 | return -ENOMEM; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 4350 | |
Alex Elder | f0f8cef | 2012-01-29 13:57:44 -0600 | [diff] [blame] | 4351 | snprintf(disk->disk_name, sizeof(disk->disk_name), RBD_DRV_NAME "%d", |
Alex Elder | de71a29 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 4352 | rbd_dev->dev_id); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 4353 | disk->major = rbd_dev->major; |
Ilya Dryomov | dd82fff | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 4354 | disk->first_minor = rbd_dev->minor; |
Ilya Dryomov | 7e513d4 | 2013-12-16 19:26:32 +0200 | [diff] [blame] | 4355 | if (single_major) |
| 4356 | disk->flags |= GENHD_FL_EXT_DEVT; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 4357 | disk->fops = &rbd_bd_ops; |
| 4358 | disk->private_data = rbd_dev; |
| 4359 | |
Christoph Hellwig | 7ad18af | 2015-01-13 17:20:04 +0100 | [diff] [blame] | 4360 | memset(&rbd_dev->tag_set, 0, sizeof(rbd_dev->tag_set)); |
| 4361 | rbd_dev->tag_set.ops = &rbd_mq_ops; |
Ilya Dryomov | b558418 | 2015-06-23 16:21:19 +0300 | [diff] [blame] | 4362 | rbd_dev->tag_set.queue_depth = rbd_dev->opts->queue_depth; |
Christoph Hellwig | 7ad18af | 2015-01-13 17:20:04 +0100 | [diff] [blame] | 4363 | rbd_dev->tag_set.numa_node = NUMA_NO_NODE; |
Ilya Dryomov | b558418 | 2015-06-23 16:21:19 +0300 | [diff] [blame] | 4364 | 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] | 4365 | rbd_dev->tag_set.nr_hw_queues = 1; |
| 4366 | rbd_dev->tag_set.cmd_size = sizeof(struct work_struct); |
| 4367 | |
| 4368 | err = blk_mq_alloc_tag_set(&rbd_dev->tag_set); |
| 4369 | if (err) |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 4370 | goto out_disk; |
Josh Durgin | 029bcbd | 2011-07-22 11:35:23 -0700 | [diff] [blame] | 4371 | |
Christoph Hellwig | 7ad18af | 2015-01-13 17:20:04 +0100 | [diff] [blame] | 4372 | q = blk_mq_init_queue(&rbd_dev->tag_set); |
| 4373 | if (IS_ERR(q)) { |
| 4374 | err = PTR_ERR(q); |
| 4375 | goto out_tag_set; |
| 4376 | } |
| 4377 | |
Ilya Dryomov | d8a2c89 | 2015-03-24 16:15:17 +0300 | [diff] [blame] | 4378 | queue_flag_set_unlocked(QUEUE_FLAG_NONROT, q); |
| 4379 | /* QUEUE_FLAG_ADD_RANDOM is off by default for blk-mq */ |
Alex Elder | 593a9e7 | 2012-02-07 12:03:37 -0600 | [diff] [blame] | 4380 | |
Josh Durgin | 029bcbd | 2011-07-22 11:35:23 -0700 | [diff] [blame] | 4381 | /* set io sizes to object size */ |
Alex Elder | 593a9e7 | 2012-02-07 12:03:37 -0600 | [diff] [blame] | 4382 | segment_size = rbd_obj_bytes(&rbd_dev->header); |
| 4383 | blk_queue_max_hw_sectors(q, segment_size / SECTOR_SIZE); |
Ilya Dryomov | 0d9fde4 | 2015-10-07 16:09:35 +0200 | [diff] [blame] | 4384 | q->limits.max_sectors = queue_max_hw_sectors(q); |
Ilya Dryomov | d3834fe | 2015-06-12 19:19:02 +0300 | [diff] [blame] | 4385 | blk_queue_max_segments(q, segment_size / SECTOR_SIZE); |
Alex Elder | 593a9e7 | 2012-02-07 12:03:37 -0600 | [diff] [blame] | 4386 | blk_queue_max_segment_size(q, segment_size); |
| 4387 | blk_queue_io_min(q, segment_size); |
| 4388 | blk_queue_io_opt(q, segment_size); |
Josh Durgin | 029bcbd | 2011-07-22 11:35:23 -0700 | [diff] [blame] | 4389 | |
Guangliang Zhao | 90e98c5 | 2014-04-01 22:22:16 +0800 | [diff] [blame] | 4390 | /* enable the discard support */ |
| 4391 | queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, q); |
| 4392 | q->limits.discard_granularity = segment_size; |
| 4393 | q->limits.discard_alignment = segment_size; |
Jens Axboe | 2bb4cd5 | 2015-07-14 08:15:12 -0600 | [diff] [blame] | 4394 | blk_queue_max_discard_sectors(q, segment_size / SECTOR_SIZE); |
Ilya Dryomov | 6ac5695 | 2017-05-22 19:59:24 +0200 | [diff] [blame] | 4395 | blk_queue_max_write_zeroes_sectors(q, segment_size / SECTOR_SIZE); |
Guangliang Zhao | 90e98c5 | 2014-04-01 22:22:16 +0800 | [diff] [blame] | 4396 | |
Ronny Hegewald | bae818e | 2015-10-15 18:50:46 +0000 | [diff] [blame] | 4397 | if (!ceph_test_opt(rbd_dev->rbd_client->client, NOCRC)) |
Jan Kara | dc3b17c | 2017-02-02 15:56:50 +0100 | [diff] [blame] | 4398 | q->backing_dev_info->capabilities |= BDI_CAP_STABLE_WRITES; |
Ronny Hegewald | bae818e | 2015-10-15 18:50:46 +0000 | [diff] [blame] | 4399 | |
Ilya Dryomov | 5769ed0 | 2017-04-13 12:17:38 +0200 | [diff] [blame] | 4400 | /* |
| 4401 | * disk_release() expects a queue ref from add_disk() and will |
| 4402 | * put it. Hold an extra ref until add_disk() is called. |
| 4403 | */ |
| 4404 | WARN_ON(!blk_get_queue(q)); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 4405 | disk->queue = q; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 4406 | q->queuedata = rbd_dev; |
| 4407 | |
| 4408 | rbd_dev->disk = disk; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 4409 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 4410 | return 0; |
Christoph Hellwig | 7ad18af | 2015-01-13 17:20:04 +0100 | [diff] [blame] | 4411 | out_tag_set: |
| 4412 | blk_mq_free_tag_set(&rbd_dev->tag_set); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 4413 | out_disk: |
| 4414 | put_disk(disk); |
Christoph Hellwig | 7ad18af | 2015-01-13 17:20:04 +0100 | [diff] [blame] | 4415 | return err; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 4416 | } |
| 4417 | |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4418 | /* |
| 4419 | sysfs |
| 4420 | */ |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 4421 | |
Alex Elder | 593a9e7 | 2012-02-07 12:03:37 -0600 | [diff] [blame] | 4422 | static struct rbd_device *dev_to_rbd_dev(struct device *dev) |
| 4423 | { |
| 4424 | return container_of(dev, struct rbd_device, dev); |
| 4425 | } |
| 4426 | |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4427 | static ssize_t rbd_size_show(struct device *dev, |
| 4428 | struct device_attribute *attr, char *buf) |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 4429 | { |
Alex Elder | 593a9e7 | 2012-02-07 12:03:37 -0600 | [diff] [blame] | 4430 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4431 | |
Alex Elder | fc71d83 | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 4432 | return sprintf(buf, "%llu\n", |
| 4433 | (unsigned long long)rbd_dev->mapping.size); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 4434 | } |
| 4435 | |
Alex Elder | 34b1318 | 2012-07-13 20:35:12 -0500 | [diff] [blame] | 4436 | /* |
| 4437 | * Note this shows the features for whatever's mapped, which is not |
| 4438 | * necessarily the base image. |
| 4439 | */ |
| 4440 | static ssize_t rbd_features_show(struct device *dev, |
| 4441 | struct device_attribute *attr, char *buf) |
| 4442 | { |
| 4443 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
| 4444 | |
| 4445 | return sprintf(buf, "0x%016llx\n", |
Alex Elder | fc71d83 | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 4446 | (unsigned long long)rbd_dev->mapping.features); |
Alex Elder | 34b1318 | 2012-07-13 20:35:12 -0500 | [diff] [blame] | 4447 | } |
| 4448 | |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4449 | static ssize_t rbd_major_show(struct device *dev, |
| 4450 | struct device_attribute *attr, char *buf) |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 4451 | { |
Alex Elder | 593a9e7 | 2012-02-07 12:03:37 -0600 | [diff] [blame] | 4452 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4453 | |
Alex Elder | fc71d83 | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 4454 | if (rbd_dev->major) |
| 4455 | return sprintf(buf, "%d\n", rbd_dev->major); |
| 4456 | |
| 4457 | return sprintf(buf, "(none)\n"); |
Ilya Dryomov | dd82fff | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 4458 | } |
Alex Elder | fc71d83 | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 4459 | |
Ilya Dryomov | dd82fff | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 4460 | static ssize_t rbd_minor_show(struct device *dev, |
| 4461 | struct device_attribute *attr, char *buf) |
| 4462 | { |
| 4463 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
| 4464 | |
| 4465 | return sprintf(buf, "%d\n", rbd_dev->minor); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4466 | } |
| 4467 | |
Ilya Dryomov | 005a07bf | 2016-08-18 18:38:43 +0200 | [diff] [blame] | 4468 | static ssize_t rbd_client_addr_show(struct device *dev, |
| 4469 | struct device_attribute *attr, char *buf) |
| 4470 | { |
| 4471 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
| 4472 | struct ceph_entity_addr *client_addr = |
| 4473 | ceph_client_addr(rbd_dev->rbd_client->client); |
| 4474 | |
| 4475 | return sprintf(buf, "%pISpc/%u\n", &client_addr->in_addr, |
| 4476 | le32_to_cpu(client_addr->nonce)); |
| 4477 | } |
| 4478 | |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4479 | static ssize_t rbd_client_id_show(struct device *dev, |
| 4480 | struct device_attribute *attr, char *buf) |
| 4481 | { |
Alex Elder | 593a9e7 | 2012-02-07 12:03:37 -0600 | [diff] [blame] | 4482 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4483 | |
Alex Elder | 1dbb439 | 2012-01-24 10:08:37 -0600 | [diff] [blame] | 4484 | return sprintf(buf, "client%lld\n", |
Ilya Dryomov | 033268a | 2016-08-12 14:59:58 +0200 | [diff] [blame] | 4485 | ceph_client_gid(rbd_dev->rbd_client->client)); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4486 | } |
| 4487 | |
Mike Christie | 267fb90 | 2016-08-18 18:38:43 +0200 | [diff] [blame] | 4488 | static ssize_t rbd_cluster_fsid_show(struct device *dev, |
| 4489 | struct device_attribute *attr, char *buf) |
| 4490 | { |
| 4491 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
| 4492 | |
| 4493 | return sprintf(buf, "%pU\n", &rbd_dev->rbd_client->client->fsid); |
| 4494 | } |
| 4495 | |
Mike Christie | 0d6d1e9c | 2016-08-18 18:38:45 +0200 | [diff] [blame] | 4496 | static ssize_t rbd_config_info_show(struct device *dev, |
| 4497 | struct device_attribute *attr, char *buf) |
| 4498 | { |
| 4499 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
| 4500 | |
| 4501 | return sprintf(buf, "%s\n", rbd_dev->config_info); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4502 | } |
| 4503 | |
| 4504 | static ssize_t rbd_pool_show(struct device *dev, |
| 4505 | struct device_attribute *attr, char *buf) |
| 4506 | { |
Alex Elder | 593a9e7 | 2012-02-07 12:03:37 -0600 | [diff] [blame] | 4507 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4508 | |
Alex Elder | 0d7dbfc | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 4509 | return sprintf(buf, "%s\n", rbd_dev->spec->pool_name); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4510 | } |
| 4511 | |
Alex Elder | 9bb2f33 | 2012-07-12 10:46:35 -0500 | [diff] [blame] | 4512 | static ssize_t rbd_pool_id_show(struct device *dev, |
| 4513 | struct device_attribute *attr, char *buf) |
| 4514 | { |
| 4515 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
| 4516 | |
Alex Elder | 0d7dbfc | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 4517 | return sprintf(buf, "%llu\n", |
Alex Elder | fc71d83 | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 4518 | (unsigned long long) rbd_dev->spec->pool_id); |
Alex Elder | 9bb2f33 | 2012-07-12 10:46:35 -0500 | [diff] [blame] | 4519 | } |
| 4520 | |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4521 | static ssize_t rbd_name_show(struct device *dev, |
| 4522 | struct device_attribute *attr, char *buf) |
| 4523 | { |
Alex Elder | 593a9e7 | 2012-02-07 12:03:37 -0600 | [diff] [blame] | 4524 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4525 | |
Alex Elder | a92ffdf | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 4526 | if (rbd_dev->spec->image_name) |
| 4527 | return sprintf(buf, "%s\n", rbd_dev->spec->image_name); |
| 4528 | |
| 4529 | return sprintf(buf, "(unknown)\n"); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4530 | } |
| 4531 | |
Alex Elder | 589d30e | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 4532 | static ssize_t rbd_image_id_show(struct device *dev, |
| 4533 | struct device_attribute *attr, char *buf) |
| 4534 | { |
| 4535 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
| 4536 | |
Alex Elder | 0d7dbfc | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 4537 | return sprintf(buf, "%s\n", rbd_dev->spec->image_id); |
Alex Elder | 589d30e | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 4538 | } |
| 4539 | |
Alex Elder | 34b1318 | 2012-07-13 20:35:12 -0500 | [diff] [blame] | 4540 | /* |
| 4541 | * Shows the name of the currently-mapped snapshot (or |
| 4542 | * RBD_SNAP_HEAD_NAME for the base image). |
| 4543 | */ |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4544 | static ssize_t rbd_snap_show(struct device *dev, |
| 4545 | struct device_attribute *attr, |
| 4546 | char *buf) |
| 4547 | { |
Alex Elder | 593a9e7 | 2012-02-07 12:03:37 -0600 | [diff] [blame] | 4548 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4549 | |
Alex Elder | 0d7dbfc | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 4550 | return sprintf(buf, "%s\n", rbd_dev->spec->snap_name); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4551 | } |
| 4552 | |
Mike Christie | 92a5867 | 2016-08-18 18:38:44 +0200 | [diff] [blame] | 4553 | static ssize_t rbd_snap_id_show(struct device *dev, |
| 4554 | struct device_attribute *attr, char *buf) |
| 4555 | { |
| 4556 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
| 4557 | |
| 4558 | return sprintf(buf, "%llu\n", rbd_dev->spec->snap_id); |
| 4559 | } |
| 4560 | |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4561 | /* |
Ilya Dryomov | ff96128 | 2014-07-22 21:53:07 +0400 | [diff] [blame] | 4562 | * For a v2 image, shows the chain of parent images, separated by empty |
| 4563 | * lines. For v1 images or if there is no parent, shows "(no parent |
| 4564 | * image)". |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4565 | */ |
| 4566 | static ssize_t rbd_parent_show(struct device *dev, |
Ilya Dryomov | ff96128 | 2014-07-22 21:53:07 +0400 | [diff] [blame] | 4567 | struct device_attribute *attr, |
| 4568 | char *buf) |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4569 | { |
| 4570 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
Ilya Dryomov | ff96128 | 2014-07-22 21:53:07 +0400 | [diff] [blame] | 4571 | ssize_t count = 0; |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4572 | |
Ilya Dryomov | ff96128 | 2014-07-22 21:53:07 +0400 | [diff] [blame] | 4573 | if (!rbd_dev->parent) |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4574 | return sprintf(buf, "(no parent image)\n"); |
| 4575 | |
Ilya Dryomov | ff96128 | 2014-07-22 21:53:07 +0400 | [diff] [blame] | 4576 | for ( ; rbd_dev->parent; rbd_dev = rbd_dev->parent) { |
| 4577 | struct rbd_spec *spec = rbd_dev->parent_spec; |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4578 | |
Ilya Dryomov | ff96128 | 2014-07-22 21:53:07 +0400 | [diff] [blame] | 4579 | count += sprintf(&buf[count], "%s" |
| 4580 | "pool_id %llu\npool_name %s\n" |
| 4581 | "image_id %s\nimage_name %s\n" |
| 4582 | "snap_id %llu\nsnap_name %s\n" |
| 4583 | "overlap %llu\n", |
| 4584 | !count ? "" : "\n", /* first? */ |
| 4585 | spec->pool_id, spec->pool_name, |
| 4586 | spec->image_id, spec->image_name ?: "(unknown)", |
| 4587 | spec->snap_id, spec->snap_name, |
| 4588 | rbd_dev->parent_overlap); |
| 4589 | } |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4590 | |
Ilya Dryomov | ff96128 | 2014-07-22 21:53:07 +0400 | [diff] [blame] | 4591 | return count; |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4592 | } |
| 4593 | |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4594 | static ssize_t rbd_image_refresh(struct device *dev, |
| 4595 | struct device_attribute *attr, |
| 4596 | const char *buf, |
| 4597 | size_t size) |
| 4598 | { |
Alex Elder | 593a9e7 | 2012-02-07 12:03:37 -0600 | [diff] [blame] | 4599 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
Alex Elder | b813623 | 2012-07-25 09:32:41 -0500 | [diff] [blame] | 4600 | int ret; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 4601 | |
Alex Elder | cc4a38bd | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 4602 | ret = rbd_dev_refresh(rbd_dev); |
Alex Elder | e627db0 | 2013-05-06 07:40:30 -0500 | [diff] [blame] | 4603 | if (ret) |
Ilya Dryomov | 52bb1f9 | 2014-07-23 17:11:20 +0400 | [diff] [blame] | 4604 | return ret; |
Alex Elder | b813623 | 2012-07-25 09:32:41 -0500 | [diff] [blame] | 4605 | |
Ilya Dryomov | 52bb1f9 | 2014-07-23 17:11:20 +0400 | [diff] [blame] | 4606 | return size; |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4607 | } |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 4608 | |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4609 | static DEVICE_ATTR(size, S_IRUGO, rbd_size_show, NULL); |
Alex Elder | 34b1318 | 2012-07-13 20:35:12 -0500 | [diff] [blame] | 4610 | static DEVICE_ATTR(features, S_IRUGO, rbd_features_show, NULL); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4611 | static DEVICE_ATTR(major, S_IRUGO, rbd_major_show, NULL); |
Ilya Dryomov | dd82fff | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 4612 | static DEVICE_ATTR(minor, S_IRUGO, rbd_minor_show, NULL); |
Ilya Dryomov | 005a07bf | 2016-08-18 18:38:43 +0200 | [diff] [blame] | 4613 | static DEVICE_ATTR(client_addr, S_IRUGO, rbd_client_addr_show, NULL); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4614 | static DEVICE_ATTR(client_id, S_IRUGO, rbd_client_id_show, NULL); |
Mike Christie | 267fb90 | 2016-08-18 18:38:43 +0200 | [diff] [blame] | 4615 | static DEVICE_ATTR(cluster_fsid, S_IRUGO, rbd_cluster_fsid_show, NULL); |
Mike Christie | 0d6d1e9c | 2016-08-18 18:38:45 +0200 | [diff] [blame] | 4616 | static DEVICE_ATTR(config_info, S_IRUSR, rbd_config_info_show, NULL); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4617 | static DEVICE_ATTR(pool, S_IRUGO, rbd_pool_show, NULL); |
Alex Elder | 9bb2f33 | 2012-07-12 10:46:35 -0500 | [diff] [blame] | 4618 | static DEVICE_ATTR(pool_id, S_IRUGO, rbd_pool_id_show, NULL); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4619 | static DEVICE_ATTR(name, S_IRUGO, rbd_name_show, NULL); |
Alex Elder | 589d30e | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 4620 | static DEVICE_ATTR(image_id, S_IRUGO, rbd_image_id_show, NULL); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4621 | static DEVICE_ATTR(refresh, S_IWUSR, NULL, rbd_image_refresh); |
| 4622 | static DEVICE_ATTR(current_snap, S_IRUGO, rbd_snap_show, NULL); |
Mike Christie | 92a5867 | 2016-08-18 18:38:44 +0200 | [diff] [blame] | 4623 | static DEVICE_ATTR(snap_id, S_IRUGO, rbd_snap_id_show, NULL); |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4624 | static DEVICE_ATTR(parent, S_IRUGO, rbd_parent_show, NULL); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4625 | |
| 4626 | static struct attribute *rbd_attrs[] = { |
| 4627 | &dev_attr_size.attr, |
Alex Elder | 34b1318 | 2012-07-13 20:35:12 -0500 | [diff] [blame] | 4628 | &dev_attr_features.attr, |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4629 | &dev_attr_major.attr, |
Ilya Dryomov | dd82fff | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 4630 | &dev_attr_minor.attr, |
Ilya Dryomov | 005a07bf | 2016-08-18 18:38:43 +0200 | [diff] [blame] | 4631 | &dev_attr_client_addr.attr, |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4632 | &dev_attr_client_id.attr, |
Mike Christie | 267fb90 | 2016-08-18 18:38:43 +0200 | [diff] [blame] | 4633 | &dev_attr_cluster_fsid.attr, |
Mike Christie | 0d6d1e9c | 2016-08-18 18:38:45 +0200 | [diff] [blame] | 4634 | &dev_attr_config_info.attr, |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4635 | &dev_attr_pool.attr, |
Alex Elder | 9bb2f33 | 2012-07-12 10:46:35 -0500 | [diff] [blame] | 4636 | &dev_attr_pool_id.attr, |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4637 | &dev_attr_name.attr, |
Alex Elder | 589d30e | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 4638 | &dev_attr_image_id.attr, |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4639 | &dev_attr_current_snap.attr, |
Mike Christie | 92a5867 | 2016-08-18 18:38:44 +0200 | [diff] [blame] | 4640 | &dev_attr_snap_id.attr, |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4641 | &dev_attr_parent.attr, |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4642 | &dev_attr_refresh.attr, |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4643 | NULL |
| 4644 | }; |
| 4645 | |
| 4646 | static struct attribute_group rbd_attr_group = { |
| 4647 | .attrs = rbd_attrs, |
| 4648 | }; |
| 4649 | |
| 4650 | static const struct attribute_group *rbd_attr_groups[] = { |
| 4651 | &rbd_attr_group, |
| 4652 | NULL |
| 4653 | }; |
| 4654 | |
Ilya Dryomov | 6cac469 | 2015-10-16 20:11:25 +0200 | [diff] [blame] | 4655 | static void rbd_dev_release(struct device *dev); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4656 | |
Bhumika Goyal | b9942bc | 2017-02-11 12:14:38 +0530 | [diff] [blame] | 4657 | static const struct device_type rbd_device_type = { |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4658 | .name = "rbd", |
| 4659 | .groups = rbd_attr_groups, |
Ilya Dryomov | 6cac469 | 2015-10-16 20:11:25 +0200 | [diff] [blame] | 4660 | .release = rbd_dev_release, |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4661 | }; |
| 4662 | |
Alex Elder | 8b8fb99 | 2012-10-26 17:25:24 -0500 | [diff] [blame] | 4663 | static struct rbd_spec *rbd_spec_get(struct rbd_spec *spec) |
| 4664 | { |
| 4665 | kref_get(&spec->kref); |
| 4666 | |
| 4667 | return spec; |
| 4668 | } |
| 4669 | |
| 4670 | static void rbd_spec_free(struct kref *kref); |
| 4671 | static void rbd_spec_put(struct rbd_spec *spec) |
| 4672 | { |
| 4673 | if (spec) |
| 4674 | kref_put(&spec->kref, rbd_spec_free); |
| 4675 | } |
| 4676 | |
| 4677 | static struct rbd_spec *rbd_spec_alloc(void) |
| 4678 | { |
| 4679 | struct rbd_spec *spec; |
| 4680 | |
| 4681 | spec = kzalloc(sizeof (*spec), GFP_KERNEL); |
| 4682 | if (!spec) |
| 4683 | return NULL; |
Ilya Dryomov | 0407759 | 2014-07-23 17:11:20 +0400 | [diff] [blame] | 4684 | |
| 4685 | spec->pool_id = CEPH_NOPOOL; |
| 4686 | spec->snap_id = CEPH_NOSNAP; |
Alex Elder | 8b8fb99 | 2012-10-26 17:25:24 -0500 | [diff] [blame] | 4687 | kref_init(&spec->kref); |
| 4688 | |
Alex Elder | 8b8fb99 | 2012-10-26 17:25:24 -0500 | [diff] [blame] | 4689 | return spec; |
| 4690 | } |
| 4691 | |
| 4692 | static void rbd_spec_free(struct kref *kref) |
| 4693 | { |
| 4694 | struct rbd_spec *spec = container_of(kref, struct rbd_spec, kref); |
| 4695 | |
| 4696 | kfree(spec->pool_name); |
| 4697 | kfree(spec->image_id); |
| 4698 | kfree(spec->image_name); |
| 4699 | kfree(spec->snap_name); |
| 4700 | kfree(spec); |
| 4701 | } |
| 4702 | |
Ilya Dryomov | 1643dfa | 2016-08-12 15:45:52 +0200 | [diff] [blame] | 4703 | static void rbd_dev_free(struct rbd_device *rbd_dev) |
Ilya Dryomov | dd5ac32 | 2015-10-16 17:09:24 +0200 | [diff] [blame] | 4704 | { |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 4705 | WARN_ON(rbd_dev->watch_state != RBD_WATCH_STATE_UNREGISTERED); |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 4706 | WARN_ON(rbd_dev->lock_state != RBD_LOCK_STATE_UNLOCKED); |
Ilya Dryomov | dd5ac32 | 2015-10-16 17:09:24 +0200 | [diff] [blame] | 4707 | |
Ilya Dryomov | c41d13a | 2016-04-29 20:01:25 +0200 | [diff] [blame] | 4708 | ceph_oid_destroy(&rbd_dev->header_oid); |
Ilya Dryomov | 6b6dddb | 2016-08-05 16:15:38 +0200 | [diff] [blame] | 4709 | ceph_oloc_destroy(&rbd_dev->header_oloc); |
Mike Christie | 0d6d1e9c | 2016-08-18 18:38:45 +0200 | [diff] [blame] | 4710 | kfree(rbd_dev->config_info); |
Ilya Dryomov | c41d13a | 2016-04-29 20:01:25 +0200 | [diff] [blame] | 4711 | |
Ilya Dryomov | dd5ac32 | 2015-10-16 17:09:24 +0200 | [diff] [blame] | 4712 | rbd_put_client(rbd_dev->rbd_client); |
| 4713 | rbd_spec_put(rbd_dev->spec); |
| 4714 | kfree(rbd_dev->opts); |
| 4715 | kfree(rbd_dev); |
Ilya Dryomov | 1643dfa | 2016-08-12 15:45:52 +0200 | [diff] [blame] | 4716 | } |
| 4717 | |
| 4718 | static void rbd_dev_release(struct device *dev) |
| 4719 | { |
| 4720 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
| 4721 | bool need_put = !!rbd_dev->opts; |
| 4722 | |
| 4723 | if (need_put) { |
| 4724 | destroy_workqueue(rbd_dev->task_wq); |
| 4725 | ida_simple_remove(&rbd_dev_id_ida, rbd_dev->dev_id); |
| 4726 | } |
| 4727 | |
| 4728 | rbd_dev_free(rbd_dev); |
Ilya Dryomov | dd5ac32 | 2015-10-16 17:09:24 +0200 | [diff] [blame] | 4729 | |
| 4730 | /* |
| 4731 | * This is racy, but way better than putting module outside of |
| 4732 | * the release callback. The race window is pretty small, so |
| 4733 | * doing something similar to dm (dm-builtin.c) is overkill. |
| 4734 | */ |
| 4735 | if (need_put) |
| 4736 | module_put(THIS_MODULE); |
| 4737 | } |
| 4738 | |
Ilya Dryomov | 1643dfa | 2016-08-12 15:45:52 +0200 | [diff] [blame] | 4739 | static struct rbd_device *__rbd_dev_create(struct rbd_client *rbdc, |
| 4740 | struct rbd_spec *spec) |
Alex Elder | c53d589 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4741 | { |
| 4742 | struct rbd_device *rbd_dev; |
| 4743 | |
Ilya Dryomov | 1643dfa | 2016-08-12 15:45:52 +0200 | [diff] [blame] | 4744 | rbd_dev = kzalloc(sizeof(*rbd_dev), GFP_KERNEL); |
Alex Elder | c53d589 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4745 | if (!rbd_dev) |
| 4746 | return NULL; |
| 4747 | |
| 4748 | spin_lock_init(&rbd_dev->lock); |
| 4749 | INIT_LIST_HEAD(&rbd_dev->node); |
Alex Elder | c53d589 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4750 | init_rwsem(&rbd_dev->header_rwsem); |
| 4751 | |
Ilya Dryomov | 7e97332 | 2017-01-25 18:16:22 +0100 | [diff] [blame] | 4752 | rbd_dev->header.data_pool_id = CEPH_NOPOOL; |
Ilya Dryomov | c41d13a | 2016-04-29 20:01:25 +0200 | [diff] [blame] | 4753 | ceph_oid_init(&rbd_dev->header_oid); |
Ilya Dryomov | 431a02c | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 4754 | rbd_dev->header_oloc.pool = spec->pool_id; |
Ilya Dryomov | c41d13a | 2016-04-29 20:01:25 +0200 | [diff] [blame] | 4755 | |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 4756 | mutex_init(&rbd_dev->watch_mutex); |
| 4757 | rbd_dev->watch_state = RBD_WATCH_STATE_UNREGISTERED; |
| 4758 | INIT_DELAYED_WORK(&rbd_dev->watch_dwork, rbd_reregister_watch); |
| 4759 | |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 4760 | init_rwsem(&rbd_dev->lock_rwsem); |
| 4761 | rbd_dev->lock_state = RBD_LOCK_STATE_UNLOCKED; |
| 4762 | INIT_WORK(&rbd_dev->acquired_lock_work, rbd_notify_acquired_lock); |
| 4763 | INIT_WORK(&rbd_dev->released_lock_work, rbd_notify_released_lock); |
| 4764 | INIT_DELAYED_WORK(&rbd_dev->lock_dwork, rbd_acquire_lock); |
| 4765 | INIT_WORK(&rbd_dev->unlock_work, rbd_release_lock_work); |
| 4766 | init_waitqueue_head(&rbd_dev->lock_waitq); |
| 4767 | |
Ilya Dryomov | dd5ac32 | 2015-10-16 17:09:24 +0200 | [diff] [blame] | 4768 | rbd_dev->dev.bus = &rbd_bus_type; |
| 4769 | rbd_dev->dev.type = &rbd_device_type; |
| 4770 | rbd_dev->dev.parent = &rbd_root_dev; |
Ilya Dryomov | dd5ac32 | 2015-10-16 17:09:24 +0200 | [diff] [blame] | 4771 | device_initialize(&rbd_dev->dev); |
| 4772 | |
Alex Elder | c53d589 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4773 | rbd_dev->rbd_client = rbdc; |
Ilya Dryomov | d147543 | 2015-06-22 13:24:48 +0300 | [diff] [blame] | 4774 | rbd_dev->spec = spec; |
Alex Elder | 0903e87 | 2012-11-14 12:25:19 -0600 | [diff] [blame] | 4775 | |
Alex Elder | c53d589 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4776 | return rbd_dev; |
| 4777 | } |
| 4778 | |
Ilya Dryomov | 1643dfa | 2016-08-12 15:45:52 +0200 | [diff] [blame] | 4779 | /* |
| 4780 | * Create a mapping rbd_dev. |
| 4781 | */ |
| 4782 | static struct rbd_device *rbd_dev_create(struct rbd_client *rbdc, |
| 4783 | struct rbd_spec *spec, |
| 4784 | struct rbd_options *opts) |
| 4785 | { |
| 4786 | struct rbd_device *rbd_dev; |
| 4787 | |
| 4788 | rbd_dev = __rbd_dev_create(rbdc, spec); |
| 4789 | if (!rbd_dev) |
| 4790 | return NULL; |
| 4791 | |
| 4792 | rbd_dev->opts = opts; |
| 4793 | |
| 4794 | /* get an id and fill in device name */ |
| 4795 | rbd_dev->dev_id = ida_simple_get(&rbd_dev_id_ida, 0, |
| 4796 | minor_to_rbd_dev_id(1 << MINORBITS), |
| 4797 | GFP_KERNEL); |
| 4798 | if (rbd_dev->dev_id < 0) |
| 4799 | goto fail_rbd_dev; |
| 4800 | |
| 4801 | sprintf(rbd_dev->name, RBD_DRV_NAME "%d", rbd_dev->dev_id); |
| 4802 | rbd_dev->task_wq = alloc_ordered_workqueue("%s-tasks", WQ_MEM_RECLAIM, |
| 4803 | rbd_dev->name); |
| 4804 | if (!rbd_dev->task_wq) |
| 4805 | goto fail_dev_id; |
| 4806 | |
| 4807 | /* we have a ref from do_rbd_add() */ |
| 4808 | __module_get(THIS_MODULE); |
| 4809 | |
| 4810 | dout("%s rbd_dev %p dev_id %d\n", __func__, rbd_dev, rbd_dev->dev_id); |
| 4811 | return rbd_dev; |
| 4812 | |
| 4813 | fail_dev_id: |
| 4814 | ida_simple_remove(&rbd_dev_id_ida, rbd_dev->dev_id); |
| 4815 | fail_rbd_dev: |
| 4816 | rbd_dev_free(rbd_dev); |
| 4817 | return NULL; |
| 4818 | } |
| 4819 | |
Alex Elder | c53d589 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4820 | static void rbd_dev_destroy(struct rbd_device *rbd_dev) |
| 4821 | { |
Ilya Dryomov | dd5ac32 | 2015-10-16 17:09:24 +0200 | [diff] [blame] | 4822 | if (rbd_dev) |
| 4823 | put_device(&rbd_dev->dev); |
Alex Elder | c53d589 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4824 | } |
| 4825 | |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4826 | /* |
Alex Elder | 9d475de | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 4827 | * Get the size and object order for an image snapshot, or if |
| 4828 | * snap_id is CEPH_NOSNAP, gets this information for the base |
| 4829 | * image. |
| 4830 | */ |
| 4831 | static int _rbd_dev_v2_snap_size(struct rbd_device *rbd_dev, u64 snap_id, |
| 4832 | u8 *order, u64 *snap_size) |
| 4833 | { |
| 4834 | __le64 snapid = cpu_to_le64(snap_id); |
| 4835 | int ret; |
| 4836 | struct { |
| 4837 | u8 order; |
| 4838 | __le64 size; |
| 4839 | } __attribute__ ((packed)) size_buf = { 0 }; |
| 4840 | |
Ilya Dryomov | ecd4a68 | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 4841 | ret = rbd_obj_method_sync(rbd_dev, &rbd_dev->header_oid, |
| 4842 | &rbd_dev->header_oloc, "get_size", |
| 4843 | &snapid, sizeof(snapid), |
| 4844 | &size_buf, sizeof(size_buf)); |
Alex Elder | 36be9a7 | 2013-01-19 00:30:28 -0600 | [diff] [blame] | 4845 | dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret); |
Alex Elder | 9d475de | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 4846 | if (ret < 0) |
| 4847 | return ret; |
Alex Elder | 57385b5 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 4848 | if (ret < sizeof (size_buf)) |
| 4849 | return -ERANGE; |
Alex Elder | 9d475de | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 4850 | |
Josh Durgin | c354557 | 2013-08-28 17:08:10 -0700 | [diff] [blame] | 4851 | if (order) { |
Alex Elder | c86f86e | 2013-04-25 15:09:41 -0500 | [diff] [blame] | 4852 | *order = size_buf.order; |
Josh Durgin | c354557 | 2013-08-28 17:08:10 -0700 | [diff] [blame] | 4853 | dout(" order %u", (unsigned int)*order); |
| 4854 | } |
Alex Elder | 9d475de | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 4855 | *snap_size = le64_to_cpu(size_buf.size); |
| 4856 | |
Josh Durgin | c354557 | 2013-08-28 17:08:10 -0700 | [diff] [blame] | 4857 | dout(" snap_id 0x%016llx snap_size = %llu\n", |
| 4858 | (unsigned long long)snap_id, |
Alex Elder | 57385b5 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 4859 | (unsigned long long)*snap_size); |
Alex Elder | 9d475de | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 4860 | |
| 4861 | return 0; |
| 4862 | } |
| 4863 | |
| 4864 | static int rbd_dev_v2_image_size(struct rbd_device *rbd_dev) |
| 4865 | { |
| 4866 | return _rbd_dev_v2_snap_size(rbd_dev, CEPH_NOSNAP, |
| 4867 | &rbd_dev->header.obj_order, |
| 4868 | &rbd_dev->header.image_size); |
| 4869 | } |
| 4870 | |
Alex Elder | 1e13019 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 4871 | static int rbd_dev_v2_object_prefix(struct rbd_device *rbd_dev) |
| 4872 | { |
| 4873 | void *reply_buf; |
| 4874 | int ret; |
| 4875 | void *p; |
| 4876 | |
| 4877 | reply_buf = kzalloc(RBD_OBJ_PREFIX_LEN_MAX, GFP_KERNEL); |
| 4878 | if (!reply_buf) |
| 4879 | return -ENOMEM; |
| 4880 | |
Ilya Dryomov | ecd4a68 | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 4881 | ret = rbd_obj_method_sync(rbd_dev, &rbd_dev->header_oid, |
| 4882 | &rbd_dev->header_oloc, "get_object_prefix", |
| 4883 | NULL, 0, reply_buf, RBD_OBJ_PREFIX_LEN_MAX); |
Alex Elder | 36be9a7 | 2013-01-19 00:30:28 -0600 | [diff] [blame] | 4884 | dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret); |
Alex Elder | 1e13019 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 4885 | if (ret < 0) |
| 4886 | goto out; |
| 4887 | |
| 4888 | p = reply_buf; |
| 4889 | rbd_dev->header.object_prefix = ceph_extract_encoded_string(&p, |
Alex Elder | 57385b5 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 4890 | p + ret, NULL, GFP_NOIO); |
| 4891 | ret = 0; |
Alex Elder | 1e13019 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 4892 | |
| 4893 | if (IS_ERR(rbd_dev->header.object_prefix)) { |
| 4894 | ret = PTR_ERR(rbd_dev->header.object_prefix); |
| 4895 | rbd_dev->header.object_prefix = NULL; |
| 4896 | } else { |
| 4897 | dout(" object_prefix = %s\n", rbd_dev->header.object_prefix); |
| 4898 | } |
Alex Elder | 1e13019 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 4899 | out: |
| 4900 | kfree(reply_buf); |
| 4901 | |
| 4902 | return ret; |
| 4903 | } |
| 4904 | |
Alex Elder | b1b5402 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 4905 | static int _rbd_dev_v2_snap_features(struct rbd_device *rbd_dev, u64 snap_id, |
| 4906 | u64 *snap_features) |
| 4907 | { |
| 4908 | __le64 snapid = cpu_to_le64(snap_id); |
| 4909 | struct { |
| 4910 | __le64 features; |
| 4911 | __le64 incompat; |
Alex Elder | 4157976 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 4912 | } __attribute__ ((packed)) features_buf = { 0 }; |
Ilya Dryomov | d3767f0 | 2016-04-13 14:15:50 +0200 | [diff] [blame] | 4913 | u64 unsup; |
Alex Elder | b1b5402 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 4914 | int ret; |
| 4915 | |
Ilya Dryomov | ecd4a68 | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 4916 | ret = rbd_obj_method_sync(rbd_dev, &rbd_dev->header_oid, |
| 4917 | &rbd_dev->header_oloc, "get_features", |
| 4918 | &snapid, sizeof(snapid), |
| 4919 | &features_buf, sizeof(features_buf)); |
Alex Elder | 36be9a7 | 2013-01-19 00:30:28 -0600 | [diff] [blame] | 4920 | dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret); |
Alex Elder | b1b5402 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 4921 | if (ret < 0) |
| 4922 | return ret; |
Alex Elder | 57385b5 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 4923 | if (ret < sizeof (features_buf)) |
| 4924 | return -ERANGE; |
Alex Elder | d889140 | 2012-10-09 13:50:17 -0700 | [diff] [blame] | 4925 | |
Ilya Dryomov | d3767f0 | 2016-04-13 14:15:50 +0200 | [diff] [blame] | 4926 | unsup = le64_to_cpu(features_buf.incompat) & ~RBD_FEATURES_SUPPORTED; |
| 4927 | if (unsup) { |
| 4928 | rbd_warn(rbd_dev, "image uses unsupported features: 0x%llx", |
| 4929 | unsup); |
Alex Elder | b8f5c6e | 2012-11-01 08:39:26 -0500 | [diff] [blame] | 4930 | return -ENXIO; |
Ilya Dryomov | d3767f0 | 2016-04-13 14:15:50 +0200 | [diff] [blame] | 4931 | } |
Alex Elder | d889140 | 2012-10-09 13:50:17 -0700 | [diff] [blame] | 4932 | |
Alex Elder | b1b5402 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 4933 | *snap_features = le64_to_cpu(features_buf.features); |
| 4934 | |
| 4935 | dout(" snap_id 0x%016llx features = 0x%016llx incompat = 0x%016llx\n", |
Alex Elder | 57385b5 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 4936 | (unsigned long long)snap_id, |
| 4937 | (unsigned long long)*snap_features, |
| 4938 | (unsigned long long)le64_to_cpu(features_buf.incompat)); |
Alex Elder | b1b5402 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 4939 | |
| 4940 | return 0; |
| 4941 | } |
| 4942 | |
| 4943 | static int rbd_dev_v2_features(struct rbd_device *rbd_dev) |
| 4944 | { |
| 4945 | return _rbd_dev_v2_snap_features(rbd_dev, CEPH_NOSNAP, |
| 4946 | &rbd_dev->header.features); |
| 4947 | } |
| 4948 | |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4949 | static int rbd_dev_v2_parent_info(struct rbd_device *rbd_dev) |
| 4950 | { |
| 4951 | struct rbd_spec *parent_spec; |
| 4952 | size_t size; |
| 4953 | void *reply_buf = NULL; |
| 4954 | __le64 snapid; |
| 4955 | void *p; |
| 4956 | void *end; |
Alex Elder | 642a253 | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 4957 | u64 pool_id; |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4958 | char *image_id; |
Alex Elder | 3b5cf2a | 2013-05-29 11:18:59 -0500 | [diff] [blame] | 4959 | u64 snap_id; |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4960 | u64 overlap; |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4961 | int ret; |
| 4962 | |
| 4963 | parent_spec = rbd_spec_alloc(); |
| 4964 | if (!parent_spec) |
| 4965 | return -ENOMEM; |
| 4966 | |
| 4967 | size = sizeof (__le64) + /* pool_id */ |
| 4968 | sizeof (__le32) + RBD_IMAGE_ID_LEN_MAX + /* image_id */ |
| 4969 | sizeof (__le64) + /* snap_id */ |
| 4970 | sizeof (__le64); /* overlap */ |
| 4971 | reply_buf = kmalloc(size, GFP_KERNEL); |
| 4972 | if (!reply_buf) { |
| 4973 | ret = -ENOMEM; |
| 4974 | goto out_err; |
| 4975 | } |
| 4976 | |
Ilya Dryomov | 4d9b67c | 2014-07-24 10:42:13 +0400 | [diff] [blame] | 4977 | snapid = cpu_to_le64(rbd_dev->spec->snap_id); |
Ilya Dryomov | ecd4a68 | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 4978 | ret = rbd_obj_method_sync(rbd_dev, &rbd_dev->header_oid, |
| 4979 | &rbd_dev->header_oloc, "get_parent", |
| 4980 | &snapid, sizeof(snapid), reply_buf, size); |
Alex Elder | 36be9a7 | 2013-01-19 00:30:28 -0600 | [diff] [blame] | 4981 | dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret); |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4982 | if (ret < 0) |
| 4983 | goto out_err; |
| 4984 | |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4985 | p = reply_buf; |
Alex Elder | 57385b5 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 4986 | end = reply_buf + ret; |
| 4987 | ret = -ERANGE; |
Alex Elder | 642a253 | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 4988 | ceph_decode_64_safe(&p, end, pool_id, out_err); |
Alex Elder | 392a9da | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 4989 | if (pool_id == CEPH_NOPOOL) { |
| 4990 | /* |
| 4991 | * Either the parent never existed, or we have |
| 4992 | * record of it but the image got flattened so it no |
| 4993 | * longer has a parent. When the parent of a |
| 4994 | * layered image disappears we immediately set the |
| 4995 | * overlap to 0. The effect of this is that all new |
| 4996 | * requests will be treated as if the image had no |
| 4997 | * parent. |
| 4998 | */ |
| 4999 | if (rbd_dev->parent_overlap) { |
| 5000 | rbd_dev->parent_overlap = 0; |
Alex Elder | 392a9da | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 5001 | rbd_dev_parent_put(rbd_dev); |
| 5002 | pr_info("%s: clone image has been flattened\n", |
| 5003 | rbd_dev->disk->disk_name); |
| 5004 | } |
| 5005 | |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5006 | goto out; /* No parent? No problem. */ |
Alex Elder | 392a9da | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 5007 | } |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5008 | |
Alex Elder | 0903e87 | 2012-11-14 12:25:19 -0600 | [diff] [blame] | 5009 | /* The ceph file layout needs to fit pool id in 32 bits */ |
| 5010 | |
| 5011 | ret = -EIO; |
Alex Elder | 642a253 | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 5012 | if (pool_id > (u64)U32_MAX) { |
Ilya Dryomov | 9584d50 | 2014-07-11 12:11:20 +0400 | [diff] [blame] | 5013 | rbd_warn(NULL, "parent pool id too large (%llu > %u)", |
Alex Elder | 642a253 | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 5014 | (unsigned long long)pool_id, U32_MAX); |
Alex Elder | 57385b5 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 5015 | goto out_err; |
Alex Elder | c0cd10db | 2013-04-26 09:43:47 -0500 | [diff] [blame] | 5016 | } |
Alex Elder | 0903e87 | 2012-11-14 12:25:19 -0600 | [diff] [blame] | 5017 | |
Alex Elder | 979ed48 | 2012-11-01 08:39:26 -0500 | [diff] [blame] | 5018 | image_id = ceph_extract_encoded_string(&p, end, NULL, GFP_KERNEL); |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5019 | if (IS_ERR(image_id)) { |
| 5020 | ret = PTR_ERR(image_id); |
| 5021 | goto out_err; |
| 5022 | } |
Alex Elder | 3b5cf2a | 2013-05-29 11:18:59 -0500 | [diff] [blame] | 5023 | ceph_decode_64_safe(&p, end, snap_id, out_err); |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5024 | ceph_decode_64_safe(&p, end, overlap, out_err); |
| 5025 | |
Alex Elder | 3b5cf2a | 2013-05-29 11:18:59 -0500 | [diff] [blame] | 5026 | /* |
| 5027 | * The parent won't change (except when the clone is |
| 5028 | * flattened, already handled that). So we only need to |
| 5029 | * record the parent spec we have not already done so. |
| 5030 | */ |
| 5031 | if (!rbd_dev->parent_spec) { |
| 5032 | parent_spec->pool_id = pool_id; |
| 5033 | parent_spec->image_id = image_id; |
| 5034 | parent_spec->snap_id = snap_id; |
Alex Elder | 70cf49c | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 5035 | rbd_dev->parent_spec = parent_spec; |
| 5036 | parent_spec = NULL; /* rbd_dev now owns this */ |
Ilya Dryomov | fbba11b | 2014-06-27 21:46:33 +0400 | [diff] [blame] | 5037 | } else { |
| 5038 | kfree(image_id); |
Alex Elder | 3b5cf2a | 2013-05-29 11:18:59 -0500 | [diff] [blame] | 5039 | } |
| 5040 | |
| 5041 | /* |
Ilya Dryomov | cf32bd9 | 2015-01-19 22:57:39 +0300 | [diff] [blame] | 5042 | * We always update the parent overlap. If it's zero we issue |
| 5043 | * a warning, as we will proceed as if there was no parent. |
Alex Elder | 3b5cf2a | 2013-05-29 11:18:59 -0500 | [diff] [blame] | 5044 | */ |
Alex Elder | 3b5cf2a | 2013-05-29 11:18:59 -0500 | [diff] [blame] | 5045 | if (!overlap) { |
Alex Elder | 3b5cf2a | 2013-05-29 11:18:59 -0500 | [diff] [blame] | 5046 | if (parent_spec) { |
Ilya Dryomov | cf32bd9 | 2015-01-19 22:57:39 +0300 | [diff] [blame] | 5047 | /* refresh, careful to warn just once */ |
| 5048 | if (rbd_dev->parent_overlap) |
| 5049 | rbd_warn(rbd_dev, |
| 5050 | "clone now standalone (overlap became 0)"); |
Alex Elder | 3b5cf2a | 2013-05-29 11:18:59 -0500 | [diff] [blame] | 5051 | } else { |
Ilya Dryomov | cf32bd9 | 2015-01-19 22:57:39 +0300 | [diff] [blame] | 5052 | /* initial probe */ |
| 5053 | rbd_warn(rbd_dev, "clone is standalone (overlap 0)"); |
Alex Elder | 3b5cf2a | 2013-05-29 11:18:59 -0500 | [diff] [blame] | 5054 | } |
Alex Elder | 70cf49c | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 5055 | } |
Ilya Dryomov | cf32bd9 | 2015-01-19 22:57:39 +0300 | [diff] [blame] | 5056 | rbd_dev->parent_overlap = overlap; |
| 5057 | |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5058 | out: |
| 5059 | ret = 0; |
| 5060 | out_err: |
| 5061 | kfree(reply_buf); |
| 5062 | rbd_spec_put(parent_spec); |
| 5063 | |
| 5064 | return ret; |
| 5065 | } |
| 5066 | |
Alex Elder | cc070d5 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 5067 | static int rbd_dev_v2_striping_info(struct rbd_device *rbd_dev) |
| 5068 | { |
| 5069 | struct { |
| 5070 | __le64 stripe_unit; |
| 5071 | __le64 stripe_count; |
| 5072 | } __attribute__ ((packed)) striping_info_buf = { 0 }; |
| 5073 | size_t size = sizeof (striping_info_buf); |
| 5074 | void *p; |
| 5075 | u64 obj_size; |
| 5076 | u64 stripe_unit; |
| 5077 | u64 stripe_count; |
| 5078 | int ret; |
| 5079 | |
Ilya Dryomov | ecd4a68 | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 5080 | ret = rbd_obj_method_sync(rbd_dev, &rbd_dev->header_oid, |
| 5081 | &rbd_dev->header_oloc, "get_stripe_unit_count", |
| 5082 | NULL, 0, &striping_info_buf, size); |
Alex Elder | cc070d5 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 5083 | dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret); |
| 5084 | if (ret < 0) |
| 5085 | return ret; |
| 5086 | if (ret < size) |
| 5087 | return -ERANGE; |
| 5088 | |
| 5089 | /* |
| 5090 | * We don't actually support the "fancy striping" feature |
| 5091 | * (STRIPINGV2) yet, but if the striping sizes are the |
| 5092 | * defaults the behavior is the same as before. So find |
| 5093 | * out, and only fail if the image has non-default values. |
| 5094 | */ |
| 5095 | ret = -EINVAL; |
Ilya Dryomov | 5bc3fb1 | 2017-01-25 18:16:22 +0100 | [diff] [blame] | 5096 | obj_size = rbd_obj_bytes(&rbd_dev->header); |
Alex Elder | cc070d5 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 5097 | p = &striping_info_buf; |
| 5098 | stripe_unit = ceph_decode_64(&p); |
| 5099 | if (stripe_unit != obj_size) { |
| 5100 | rbd_warn(rbd_dev, "unsupported stripe unit " |
| 5101 | "(got %llu want %llu)", |
| 5102 | stripe_unit, obj_size); |
| 5103 | return -EINVAL; |
| 5104 | } |
| 5105 | stripe_count = ceph_decode_64(&p); |
| 5106 | if (stripe_count != 1) { |
| 5107 | rbd_warn(rbd_dev, "unsupported stripe count " |
| 5108 | "(got %llu want 1)", stripe_count); |
| 5109 | return -EINVAL; |
| 5110 | } |
Alex Elder | 500d0c0 | 2013-04-26 09:43:47 -0500 | [diff] [blame] | 5111 | rbd_dev->header.stripe_unit = stripe_unit; |
| 5112 | rbd_dev->header.stripe_count = stripe_count; |
Alex Elder | cc070d5 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 5113 | |
| 5114 | return 0; |
| 5115 | } |
| 5116 | |
Ilya Dryomov | 7e97332 | 2017-01-25 18:16:22 +0100 | [diff] [blame] | 5117 | static int rbd_dev_v2_data_pool(struct rbd_device *rbd_dev) |
| 5118 | { |
| 5119 | __le64 data_pool_id; |
| 5120 | int ret; |
| 5121 | |
| 5122 | ret = rbd_obj_method_sync(rbd_dev, &rbd_dev->header_oid, |
| 5123 | &rbd_dev->header_oloc, "get_data_pool", |
| 5124 | NULL, 0, &data_pool_id, sizeof(data_pool_id)); |
| 5125 | if (ret < 0) |
| 5126 | return ret; |
| 5127 | if (ret < sizeof(data_pool_id)) |
| 5128 | return -EBADMSG; |
| 5129 | |
| 5130 | rbd_dev->header.data_pool_id = le64_to_cpu(data_pool_id); |
| 5131 | WARN_ON(rbd_dev->header.data_pool_id == CEPH_NOPOOL); |
| 5132 | return 0; |
| 5133 | } |
| 5134 | |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 5135 | static char *rbd_dev_image_name(struct rbd_device *rbd_dev) |
| 5136 | { |
Ilya Dryomov | ecd4a68 | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 5137 | CEPH_DEFINE_OID_ONSTACK(oid); |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 5138 | size_t image_id_size; |
| 5139 | char *image_id; |
| 5140 | void *p; |
| 5141 | void *end; |
| 5142 | size_t size; |
| 5143 | void *reply_buf = NULL; |
| 5144 | size_t len = 0; |
| 5145 | char *image_name = NULL; |
| 5146 | int ret; |
| 5147 | |
| 5148 | rbd_assert(!rbd_dev->spec->image_name); |
| 5149 | |
Alex Elder | 69e7a02 | 2012-11-01 08:39:26 -0500 | [diff] [blame] | 5150 | len = strlen(rbd_dev->spec->image_id); |
| 5151 | image_id_size = sizeof (__le32) + len; |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 5152 | image_id = kmalloc(image_id_size, GFP_KERNEL); |
| 5153 | if (!image_id) |
| 5154 | return NULL; |
| 5155 | |
| 5156 | p = image_id; |
Alex Elder | 4157976 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 5157 | end = image_id + image_id_size; |
Alex Elder | 57385b5 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 5158 | ceph_encode_string(&p, end, rbd_dev->spec->image_id, (u32)len); |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 5159 | |
| 5160 | size = sizeof (__le32) + RBD_IMAGE_NAME_LEN_MAX; |
| 5161 | reply_buf = kmalloc(size, GFP_KERNEL); |
| 5162 | if (!reply_buf) |
| 5163 | goto out; |
| 5164 | |
Ilya Dryomov | ecd4a68 | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 5165 | ceph_oid_printf(&oid, "%s", RBD_DIRECTORY); |
| 5166 | ret = rbd_obj_method_sync(rbd_dev, &oid, &rbd_dev->header_oloc, |
| 5167 | "dir_get_name", image_id, image_id_size, |
| 5168 | reply_buf, size); |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 5169 | if (ret < 0) |
| 5170 | goto out; |
| 5171 | p = reply_buf; |
Alex Elder | f40eb34 | 2013-04-25 15:09:42 -0500 | [diff] [blame] | 5172 | end = reply_buf + ret; |
| 5173 | |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 5174 | image_name = ceph_extract_encoded_string(&p, end, &len, GFP_KERNEL); |
| 5175 | if (IS_ERR(image_name)) |
| 5176 | image_name = NULL; |
| 5177 | else |
| 5178 | dout("%s: name is %s len is %zd\n", __func__, image_name, len); |
| 5179 | out: |
| 5180 | kfree(reply_buf); |
| 5181 | kfree(image_id); |
| 5182 | |
| 5183 | return image_name; |
| 5184 | } |
| 5185 | |
Alex Elder | 2ad3d71 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 5186 | static u64 rbd_v1_snap_id_by_name(struct rbd_device *rbd_dev, const char *name) |
| 5187 | { |
| 5188 | struct ceph_snap_context *snapc = rbd_dev->header.snapc; |
| 5189 | const char *snap_name; |
| 5190 | u32 which = 0; |
| 5191 | |
| 5192 | /* Skip over names until we find the one we are looking for */ |
| 5193 | |
| 5194 | snap_name = rbd_dev->header.snap_names; |
| 5195 | while (which < snapc->num_snaps) { |
| 5196 | if (!strcmp(name, snap_name)) |
| 5197 | return snapc->snaps[which]; |
| 5198 | snap_name += strlen(snap_name) + 1; |
| 5199 | which++; |
| 5200 | } |
| 5201 | return CEPH_NOSNAP; |
| 5202 | } |
| 5203 | |
| 5204 | static u64 rbd_v2_snap_id_by_name(struct rbd_device *rbd_dev, const char *name) |
| 5205 | { |
| 5206 | struct ceph_snap_context *snapc = rbd_dev->header.snapc; |
| 5207 | u32 which; |
| 5208 | bool found = false; |
| 5209 | u64 snap_id; |
| 5210 | |
| 5211 | for (which = 0; !found && which < snapc->num_snaps; which++) { |
| 5212 | const char *snap_name; |
| 5213 | |
| 5214 | snap_id = snapc->snaps[which]; |
| 5215 | snap_name = rbd_dev_v2_snap_name(rbd_dev, snap_id); |
Josh Durgin | efadc98 | 2013-08-29 19:16:42 -0700 | [diff] [blame] | 5216 | if (IS_ERR(snap_name)) { |
| 5217 | /* ignore no-longer existing snapshots */ |
| 5218 | if (PTR_ERR(snap_name) == -ENOENT) |
| 5219 | continue; |
| 5220 | else |
| 5221 | break; |
| 5222 | } |
Alex Elder | 2ad3d71 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 5223 | found = !strcmp(name, snap_name); |
| 5224 | kfree(snap_name); |
| 5225 | } |
| 5226 | return found ? snap_id : CEPH_NOSNAP; |
| 5227 | } |
| 5228 | |
| 5229 | /* |
| 5230 | * Assumes name is never RBD_SNAP_HEAD_NAME; returns CEPH_NOSNAP if |
| 5231 | * no snapshot by that name is found, or if an error occurs. |
| 5232 | */ |
| 5233 | static u64 rbd_snap_id_by_name(struct rbd_device *rbd_dev, const char *name) |
| 5234 | { |
| 5235 | if (rbd_dev->image_format == 1) |
| 5236 | return rbd_v1_snap_id_by_name(rbd_dev, name); |
| 5237 | |
| 5238 | return rbd_v2_snap_id_by_name(rbd_dev, name); |
| 5239 | } |
| 5240 | |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 5241 | /* |
Ilya Dryomov | 0407759 | 2014-07-23 17:11:20 +0400 | [diff] [blame] | 5242 | * An image being mapped will have everything but the snap id. |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 5243 | */ |
Ilya Dryomov | 0407759 | 2014-07-23 17:11:20 +0400 | [diff] [blame] | 5244 | static int rbd_spec_fill_snap_id(struct rbd_device *rbd_dev) |
| 5245 | { |
| 5246 | struct rbd_spec *spec = rbd_dev->spec; |
| 5247 | |
| 5248 | rbd_assert(spec->pool_id != CEPH_NOPOOL && spec->pool_name); |
| 5249 | rbd_assert(spec->image_id && spec->image_name); |
| 5250 | rbd_assert(spec->snap_name); |
| 5251 | |
| 5252 | if (strcmp(spec->snap_name, RBD_SNAP_HEAD_NAME)) { |
| 5253 | u64 snap_id; |
| 5254 | |
| 5255 | snap_id = rbd_snap_id_by_name(rbd_dev, spec->snap_name); |
| 5256 | if (snap_id == CEPH_NOSNAP) |
| 5257 | return -ENOENT; |
| 5258 | |
| 5259 | spec->snap_id = snap_id; |
| 5260 | } else { |
| 5261 | spec->snap_id = CEPH_NOSNAP; |
| 5262 | } |
| 5263 | |
| 5264 | return 0; |
| 5265 | } |
| 5266 | |
| 5267 | /* |
| 5268 | * A parent image will have all ids but none of the names. |
| 5269 | * |
| 5270 | * All names in an rbd spec are dynamically allocated. It's OK if we |
| 5271 | * can't figure out the name for an image id. |
| 5272 | */ |
| 5273 | static int rbd_spec_fill_names(struct rbd_device *rbd_dev) |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 5274 | { |
Alex Elder | 2e9f7f1 | 2013-04-26 09:43:48 -0500 | [diff] [blame] | 5275 | struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc; |
| 5276 | struct rbd_spec *spec = rbd_dev->spec; |
| 5277 | const char *pool_name; |
| 5278 | const char *image_name; |
| 5279 | const char *snap_name; |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 5280 | int ret; |
| 5281 | |
Ilya Dryomov | 0407759 | 2014-07-23 17:11:20 +0400 | [diff] [blame] | 5282 | rbd_assert(spec->pool_id != CEPH_NOPOOL); |
| 5283 | rbd_assert(spec->image_id); |
| 5284 | rbd_assert(spec->snap_id != CEPH_NOSNAP); |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 5285 | |
Alex Elder | 2e9f7f1 | 2013-04-26 09:43:48 -0500 | [diff] [blame] | 5286 | /* 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] | 5287 | |
Alex Elder | 2e9f7f1 | 2013-04-26 09:43:48 -0500 | [diff] [blame] | 5288 | pool_name = ceph_pg_pool_name_by_id(osdc->osdmap, spec->pool_id); |
| 5289 | if (!pool_name) { |
| 5290 | rbd_warn(rbd_dev, "no pool with id %llu", spec->pool_id); |
Alex Elder | 935dc89 | 2012-11-01 10:17:15 -0500 | [diff] [blame] | 5291 | return -EIO; |
| 5292 | } |
Alex Elder | 2e9f7f1 | 2013-04-26 09:43:48 -0500 | [diff] [blame] | 5293 | pool_name = kstrdup(pool_name, GFP_KERNEL); |
| 5294 | if (!pool_name) |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 5295 | return -ENOMEM; |
| 5296 | |
| 5297 | /* Fetch the image name; tolerate failure here */ |
| 5298 | |
Alex Elder | 2e9f7f1 | 2013-04-26 09:43:48 -0500 | [diff] [blame] | 5299 | image_name = rbd_dev_image_name(rbd_dev); |
| 5300 | if (!image_name) |
Alex Elder | 06ecc6c | 2012-11-01 10:17:15 -0500 | [diff] [blame] | 5301 | rbd_warn(rbd_dev, "unable to get image name"); |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 5302 | |
Ilya Dryomov | 0407759 | 2014-07-23 17:11:20 +0400 | [diff] [blame] | 5303 | /* Fetch the snapshot name */ |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 5304 | |
Alex Elder | 2e9f7f1 | 2013-04-26 09:43:48 -0500 | [diff] [blame] | 5305 | snap_name = rbd_snap_name(rbd_dev, spec->snap_id); |
Josh Durgin | da6a6b6 | 2013-09-04 17:57:31 -0700 | [diff] [blame] | 5306 | if (IS_ERR(snap_name)) { |
| 5307 | ret = PTR_ERR(snap_name); |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 5308 | goto out_err; |
Alex Elder | 2e9f7f1 | 2013-04-26 09:43:48 -0500 | [diff] [blame] | 5309 | } |
| 5310 | |
| 5311 | spec->pool_name = pool_name; |
| 5312 | spec->image_name = image_name; |
| 5313 | spec->snap_name = snap_name; |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 5314 | |
| 5315 | return 0; |
Ilya Dryomov | 0407759 | 2014-07-23 17:11:20 +0400 | [diff] [blame] | 5316 | |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 5317 | out_err: |
Alex Elder | 2e9f7f1 | 2013-04-26 09:43:48 -0500 | [diff] [blame] | 5318 | kfree(image_name); |
| 5319 | kfree(pool_name); |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 5320 | return ret; |
| 5321 | } |
| 5322 | |
Alex Elder | cc4a38bd | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 5323 | static int rbd_dev_v2_snap_context(struct rbd_device *rbd_dev) |
Alex Elder | 35d489f | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5324 | { |
| 5325 | size_t size; |
| 5326 | int ret; |
| 5327 | void *reply_buf; |
| 5328 | void *p; |
| 5329 | void *end; |
| 5330 | u64 seq; |
| 5331 | u32 snap_count; |
| 5332 | struct ceph_snap_context *snapc; |
| 5333 | u32 i; |
| 5334 | |
| 5335 | /* |
| 5336 | * We'll need room for the seq value (maximum snapshot id), |
| 5337 | * snapshot count, and array of that many snapshot ids. |
| 5338 | * For now we have a fixed upper limit on the number we're |
| 5339 | * prepared to receive. |
| 5340 | */ |
| 5341 | size = sizeof (__le64) + sizeof (__le32) + |
| 5342 | RBD_MAX_SNAP_COUNT * sizeof (__le64); |
| 5343 | reply_buf = kzalloc(size, GFP_KERNEL); |
| 5344 | if (!reply_buf) |
| 5345 | return -ENOMEM; |
| 5346 | |
Ilya Dryomov | ecd4a68 | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 5347 | ret = rbd_obj_method_sync(rbd_dev, &rbd_dev->header_oid, |
| 5348 | &rbd_dev->header_oloc, "get_snapcontext", |
| 5349 | NULL, 0, reply_buf, size); |
Alex Elder | 36be9a7 | 2013-01-19 00:30:28 -0600 | [diff] [blame] | 5350 | dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret); |
Alex Elder | 35d489f | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5351 | if (ret < 0) |
| 5352 | goto out; |
| 5353 | |
Alex Elder | 35d489f | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5354 | p = reply_buf; |
Alex Elder | 57385b5 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 5355 | end = reply_buf + ret; |
| 5356 | ret = -ERANGE; |
Alex Elder | 35d489f | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5357 | ceph_decode_64_safe(&p, end, seq, out); |
| 5358 | ceph_decode_32_safe(&p, end, snap_count, out); |
| 5359 | |
| 5360 | /* |
| 5361 | * Make sure the reported number of snapshot ids wouldn't go |
| 5362 | * beyond the end of our buffer. But before checking that, |
| 5363 | * make sure the computed size of the snapshot context we |
| 5364 | * allocate is representable in a size_t. |
| 5365 | */ |
| 5366 | if (snap_count > (SIZE_MAX - sizeof (struct ceph_snap_context)) |
| 5367 | / sizeof (u64)) { |
| 5368 | ret = -EINVAL; |
| 5369 | goto out; |
| 5370 | } |
| 5371 | if (!ceph_has_room(&p, end, snap_count * sizeof (__le64))) |
| 5372 | goto out; |
Alex Elder | 468521c | 2013-04-26 09:43:47 -0500 | [diff] [blame] | 5373 | ret = 0; |
Alex Elder | 35d489f | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5374 | |
Alex Elder | 812164f8 | 2013-04-30 00:44:32 -0500 | [diff] [blame] | 5375 | snapc = ceph_create_snap_context(snap_count, GFP_KERNEL); |
Alex Elder | 35d489f | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5376 | if (!snapc) { |
| 5377 | ret = -ENOMEM; |
| 5378 | goto out; |
| 5379 | } |
Alex Elder | 35d489f | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5380 | snapc->seq = seq; |
Alex Elder | 35d489f | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5381 | for (i = 0; i < snap_count; i++) |
| 5382 | snapc->snaps[i] = ceph_decode_64(&p); |
| 5383 | |
Alex Elder | 49ece55 | 2013-05-06 08:37:00 -0500 | [diff] [blame] | 5384 | ceph_put_snap_context(rbd_dev->header.snapc); |
Alex Elder | 35d489f | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5385 | rbd_dev->header.snapc = snapc; |
| 5386 | |
| 5387 | dout(" snap context seq = %llu, snap_count = %u\n", |
Alex Elder | 57385b5 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 5388 | (unsigned long long)seq, (unsigned int)snap_count); |
Alex Elder | 35d489f | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5389 | out: |
| 5390 | kfree(reply_buf); |
| 5391 | |
Alex Elder | 57385b5 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 5392 | return ret; |
Alex Elder | 35d489f | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5393 | } |
| 5394 | |
Alex Elder | 54cac61 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 5395 | static const char *rbd_dev_v2_snap_name(struct rbd_device *rbd_dev, |
| 5396 | u64 snap_id) |
Alex Elder | b8b1e2d | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5397 | { |
| 5398 | size_t size; |
| 5399 | void *reply_buf; |
Alex Elder | 54cac61 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 5400 | __le64 snapid; |
Alex Elder | b8b1e2d | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5401 | int ret; |
| 5402 | void *p; |
| 5403 | void *end; |
Alex Elder | b8b1e2d | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5404 | char *snap_name; |
| 5405 | |
| 5406 | size = sizeof (__le32) + RBD_MAX_SNAP_NAME_LEN; |
| 5407 | reply_buf = kmalloc(size, GFP_KERNEL); |
| 5408 | if (!reply_buf) |
| 5409 | return ERR_PTR(-ENOMEM); |
| 5410 | |
Alex Elder | 54cac61 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 5411 | snapid = cpu_to_le64(snap_id); |
Ilya Dryomov | ecd4a68 | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 5412 | ret = rbd_obj_method_sync(rbd_dev, &rbd_dev->header_oid, |
| 5413 | &rbd_dev->header_oloc, "get_snapshot_name", |
| 5414 | &snapid, sizeof(snapid), reply_buf, size); |
Alex Elder | 36be9a7 | 2013-01-19 00:30:28 -0600 | [diff] [blame] | 5415 | dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret); |
Alex Elder | f40eb34 | 2013-04-25 15:09:42 -0500 | [diff] [blame] | 5416 | if (ret < 0) { |
| 5417 | snap_name = ERR_PTR(ret); |
Alex Elder | b8b1e2d | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5418 | goto out; |
Alex Elder | f40eb34 | 2013-04-25 15:09:42 -0500 | [diff] [blame] | 5419 | } |
Alex Elder | b8b1e2d | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5420 | |
| 5421 | p = reply_buf; |
Alex Elder | f40eb34 | 2013-04-25 15:09:42 -0500 | [diff] [blame] | 5422 | end = reply_buf + ret; |
Alex Elder | e5c3553 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5423 | snap_name = ceph_extract_encoded_string(&p, end, NULL, GFP_KERNEL); |
Alex Elder | f40eb34 | 2013-04-25 15:09:42 -0500 | [diff] [blame] | 5424 | if (IS_ERR(snap_name)) |
Alex Elder | b8b1e2d | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5425 | goto out; |
Alex Elder | b8b1e2d | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5426 | |
Alex Elder | f40eb34 | 2013-04-25 15:09:42 -0500 | [diff] [blame] | 5427 | dout(" snap_id 0x%016llx snap_name = %s\n", |
Alex Elder | 54cac61 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 5428 | (unsigned long long)snap_id, snap_name); |
Alex Elder | b8b1e2d | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5429 | out: |
| 5430 | kfree(reply_buf); |
| 5431 | |
Alex Elder | f40eb34 | 2013-04-25 15:09:42 -0500 | [diff] [blame] | 5432 | return snap_name; |
Alex Elder | b8b1e2d | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5433 | } |
| 5434 | |
Alex Elder | 2df3fac | 2013-05-06 09:51:30 -0500 | [diff] [blame] | 5435 | static int rbd_dev_v2_header_info(struct rbd_device *rbd_dev) |
Alex Elder | 117973f | 2012-08-31 17:29:55 -0500 | [diff] [blame] | 5436 | { |
Alex Elder | 2df3fac | 2013-05-06 09:51:30 -0500 | [diff] [blame] | 5437 | bool first_time = rbd_dev->header.object_prefix == NULL; |
Alex Elder | 117973f | 2012-08-31 17:29:55 -0500 | [diff] [blame] | 5438 | int ret; |
Alex Elder | 117973f | 2012-08-31 17:29:55 -0500 | [diff] [blame] | 5439 | |
Josh Durgin | 1617e40 | 2013-06-12 14:43:10 -0700 | [diff] [blame] | 5440 | ret = rbd_dev_v2_image_size(rbd_dev); |
| 5441 | if (ret) |
Alex Elder | cfbf637 | 2013-05-31 17:40:45 -0500 | [diff] [blame] | 5442 | return ret; |
Josh Durgin | 1617e40 | 2013-06-12 14:43:10 -0700 | [diff] [blame] | 5443 | |
Alex Elder | 2df3fac | 2013-05-06 09:51:30 -0500 | [diff] [blame] | 5444 | if (first_time) { |
| 5445 | ret = rbd_dev_v2_header_onetime(rbd_dev); |
| 5446 | if (ret) |
Alex Elder | cfbf637 | 2013-05-31 17:40:45 -0500 | [diff] [blame] | 5447 | return ret; |
Alex Elder | 2df3fac | 2013-05-06 09:51:30 -0500 | [diff] [blame] | 5448 | } |
| 5449 | |
Alex Elder | cc4a38bd | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 5450 | ret = rbd_dev_v2_snap_context(rbd_dev); |
Ilya Dryomov | d194cd1 | 2015-08-31 18:22:10 +0300 | [diff] [blame] | 5451 | if (ret && first_time) { |
| 5452 | kfree(rbd_dev->header.object_prefix); |
| 5453 | rbd_dev->header.object_prefix = NULL; |
| 5454 | } |
Alex Elder | 117973f | 2012-08-31 17:29:55 -0500 | [diff] [blame] | 5455 | |
| 5456 | return ret; |
| 5457 | } |
| 5458 | |
Ilya Dryomov | a720ae0 | 2014-07-23 17:11:19 +0400 | [diff] [blame] | 5459 | static int rbd_dev_header_info(struct rbd_device *rbd_dev) |
| 5460 | { |
| 5461 | rbd_assert(rbd_image_format_valid(rbd_dev->image_format)); |
| 5462 | |
| 5463 | if (rbd_dev->image_format == 1) |
| 5464 | return rbd_dev_v1_header_info(rbd_dev); |
| 5465 | |
| 5466 | return rbd_dev_v2_header_info(rbd_dev); |
| 5467 | } |
| 5468 | |
Alex Elder | 1ddbe94 | 2012-01-29 13:57:44 -0600 | [diff] [blame] | 5469 | /* |
Alex Elder | e28fff26 | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 5470 | * Skips over white space at *buf, and updates *buf to point to the |
| 5471 | * first found non-space character (if any). Returns the length of |
Alex Elder | 593a9e7 | 2012-02-07 12:03:37 -0600 | [diff] [blame] | 5472 | * the token (string of non-white space characters) found. Note |
| 5473 | * that *buf must be terminated with '\0'. |
Alex Elder | e28fff26 | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 5474 | */ |
| 5475 | static inline size_t next_token(const char **buf) |
| 5476 | { |
| 5477 | /* |
| 5478 | * These are the characters that produce nonzero for |
| 5479 | * isspace() in the "C" and "POSIX" locales. |
| 5480 | */ |
| 5481 | const char *spaces = " \f\n\r\t\v"; |
| 5482 | |
| 5483 | *buf += strspn(*buf, spaces); /* Find start of token */ |
| 5484 | |
| 5485 | return strcspn(*buf, spaces); /* Return token length */ |
| 5486 | } |
| 5487 | |
| 5488 | /* |
Alex Elder | ea3352f | 2012-07-09 21:04:23 -0500 | [diff] [blame] | 5489 | * Finds the next token in *buf, dynamically allocates a buffer big |
| 5490 | * enough to hold a copy of it, and copies the token into the new |
| 5491 | * buffer. The copy is guaranteed to be terminated with '\0'. Note |
| 5492 | * that a duplicate buffer is created even for a zero-length token. |
| 5493 | * |
| 5494 | * Returns a pointer to the newly-allocated duplicate, or a null |
| 5495 | * pointer if memory for the duplicate was not available. If |
| 5496 | * the lenp argument is a non-null pointer, the length of the token |
| 5497 | * (not including the '\0') is returned in *lenp. |
| 5498 | * |
| 5499 | * If successful, the *buf pointer will be updated to point beyond |
| 5500 | * the end of the found token. |
| 5501 | * |
| 5502 | * Note: uses GFP_KERNEL for allocation. |
| 5503 | */ |
| 5504 | static inline char *dup_token(const char **buf, size_t *lenp) |
| 5505 | { |
| 5506 | char *dup; |
| 5507 | size_t len; |
| 5508 | |
| 5509 | len = next_token(buf); |
Alex Elder | 4caf35f | 2012-11-01 08:39:27 -0500 | [diff] [blame] | 5510 | dup = kmemdup(*buf, len + 1, GFP_KERNEL); |
Alex Elder | ea3352f | 2012-07-09 21:04:23 -0500 | [diff] [blame] | 5511 | if (!dup) |
| 5512 | return NULL; |
Alex Elder | ea3352f | 2012-07-09 21:04:23 -0500 | [diff] [blame] | 5513 | *(dup + len) = '\0'; |
| 5514 | *buf += len; |
| 5515 | |
| 5516 | if (lenp) |
| 5517 | *lenp = len; |
| 5518 | |
| 5519 | return dup; |
| 5520 | } |
| 5521 | |
| 5522 | /* |
Alex Elder | 859c31d | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5523 | * Parse the options provided for an "rbd add" (i.e., rbd image |
| 5524 | * mapping) request. These arrive via a write to /sys/bus/rbd/add, |
| 5525 | * and the data written is passed here via a NUL-terminated buffer. |
| 5526 | * Returns 0 if successful or an error code otherwise. |
Alex Elder | d22f76e | 2012-07-12 10:46:35 -0500 | [diff] [blame] | 5527 | * |
Alex Elder | 859c31d | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5528 | * The information extracted from these options is recorded in |
| 5529 | * the other parameters which return dynamically-allocated |
| 5530 | * structures: |
| 5531 | * ceph_opts |
| 5532 | * The address of a pointer that will refer to a ceph options |
| 5533 | * structure. Caller must release the returned pointer using |
| 5534 | * ceph_destroy_options() when it is no longer needed. |
| 5535 | * rbd_opts |
| 5536 | * Address of an rbd options pointer. Fully initialized by |
| 5537 | * this function; caller must release with kfree(). |
| 5538 | * spec |
| 5539 | * Address of an rbd image specification pointer. Fully |
| 5540 | * initialized by this function based on parsed options. |
| 5541 | * Caller must release with rbd_spec_put(). |
| 5542 | * |
| 5543 | * The options passed take this form: |
| 5544 | * <mon_addrs> <options> <pool_name> <image_name> [<snap_id>] |
| 5545 | * where: |
| 5546 | * <mon_addrs> |
| 5547 | * A comma-separated list of one or more monitor addresses. |
| 5548 | * A monitor address is an ip address, optionally followed |
| 5549 | * by a port number (separated by a colon). |
| 5550 | * I.e.: ip1[:port1][,ip2[:port2]...] |
| 5551 | * <options> |
| 5552 | * A comma-separated list of ceph and/or rbd options. |
| 5553 | * <pool_name> |
| 5554 | * The name of the rados pool containing the rbd image. |
| 5555 | * <image_name> |
| 5556 | * The name of the image in that pool to map. |
| 5557 | * <snap_id> |
| 5558 | * An optional snapshot id. If provided, the mapping will |
| 5559 | * present data from the image at the time that snapshot was |
| 5560 | * created. The image head is used if no snapshot id is |
| 5561 | * provided. Snapshot mappings are always read-only. |
Alex Elder | a725f65e | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 5562 | */ |
Alex Elder | 859c31d | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5563 | static int rbd_add_parse_args(const char *buf, |
Alex Elder | dc79b11 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5564 | struct ceph_options **ceph_opts, |
Alex Elder | 859c31d | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5565 | struct rbd_options **opts, |
| 5566 | struct rbd_spec **rbd_spec) |
Alex Elder | a725f65e | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 5567 | { |
Alex Elder | d22f76e | 2012-07-12 10:46:35 -0500 | [diff] [blame] | 5568 | size_t len; |
Alex Elder | 859c31d | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5569 | char *options; |
Alex Elder | 0ddebc0 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5570 | const char *mon_addrs; |
Alex Elder | ecb4dc2 | 2013-04-26 09:43:47 -0500 | [diff] [blame] | 5571 | char *snap_name; |
Alex Elder | 0ddebc0 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5572 | size_t mon_addrs_size; |
Alex Elder | 859c31d | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5573 | struct rbd_spec *spec = NULL; |
Alex Elder | 4e9afeb | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5574 | struct rbd_options *rbd_opts = NULL; |
Alex Elder | 859c31d | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5575 | struct ceph_options *copts; |
Alex Elder | dc79b11 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5576 | int ret; |
Alex Elder | e28fff26 | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 5577 | |
| 5578 | /* The first four tokens are required */ |
| 5579 | |
Alex Elder | 7ef3214 | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 5580 | len = next_token(&buf); |
Alex Elder | 4fb5d671 | 2012-11-01 10:17:15 -0500 | [diff] [blame] | 5581 | if (!len) { |
| 5582 | rbd_warn(NULL, "no monitor address(es) provided"); |
| 5583 | return -EINVAL; |
| 5584 | } |
Alex Elder | 0ddebc0 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5585 | mon_addrs = buf; |
Alex Elder | f28e565 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5586 | mon_addrs_size = len + 1; |
Alex Elder | 7ef3214 | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 5587 | buf += len; |
Alex Elder | a725f65e | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 5588 | |
Alex Elder | dc79b11 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5589 | ret = -EINVAL; |
Alex Elder | f28e565 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5590 | options = dup_token(&buf, NULL); |
| 5591 | if (!options) |
Alex Elder | dc79b11 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5592 | return -ENOMEM; |
Alex Elder | 4fb5d671 | 2012-11-01 10:17:15 -0500 | [diff] [blame] | 5593 | if (!*options) { |
| 5594 | rbd_warn(NULL, "no options provided"); |
| 5595 | goto out_err; |
| 5596 | } |
Alex Elder | a725f65e | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 5597 | |
Alex Elder | 859c31d | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5598 | spec = rbd_spec_alloc(); |
| 5599 | if (!spec) |
Alex Elder | f28e565 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5600 | goto out_mem; |
Alex Elder | 859c31d | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5601 | |
| 5602 | spec->pool_name = dup_token(&buf, NULL); |
| 5603 | if (!spec->pool_name) |
| 5604 | goto out_mem; |
Alex Elder | 4fb5d671 | 2012-11-01 10:17:15 -0500 | [diff] [blame] | 5605 | if (!*spec->pool_name) { |
| 5606 | rbd_warn(NULL, "no pool name provided"); |
| 5607 | goto out_err; |
| 5608 | } |
Alex Elder | e28fff26 | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 5609 | |
Alex Elder | 69e7a02 | 2012-11-01 08:39:26 -0500 | [diff] [blame] | 5610 | spec->image_name = dup_token(&buf, NULL); |
Alex Elder | 859c31d | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5611 | if (!spec->image_name) |
Alex Elder | f28e565 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5612 | goto out_mem; |
Alex Elder | 4fb5d671 | 2012-11-01 10:17:15 -0500 | [diff] [blame] | 5613 | if (!*spec->image_name) { |
| 5614 | rbd_warn(NULL, "no image name provided"); |
| 5615 | goto out_err; |
| 5616 | } |
Alex Elder | e28fff26 | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 5617 | |
Alex Elder | f28e565 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5618 | /* |
| 5619 | * Snapshot name is optional; default is to use "-" |
| 5620 | * (indicating the head/no snapshot). |
| 5621 | */ |
Alex Elder | 3feeb894 | 2012-08-31 17:29:52 -0500 | [diff] [blame] | 5622 | len = next_token(&buf); |
Alex Elder | 820a5f3 | 2012-07-09 21:04:24 -0500 | [diff] [blame] | 5623 | if (!len) { |
Alex Elder | 3feeb894 | 2012-08-31 17:29:52 -0500 | [diff] [blame] | 5624 | buf = RBD_SNAP_HEAD_NAME; /* No snapshot supplied */ |
| 5625 | len = sizeof (RBD_SNAP_HEAD_NAME) - 1; |
Alex Elder | f28e565 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5626 | } else if (len > RBD_MAX_SNAP_NAME_LEN) { |
Alex Elder | dc79b11 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5627 | ret = -ENAMETOOLONG; |
Alex Elder | f28e565 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5628 | goto out_err; |
Alex Elder | 849b426 | 2012-07-09 21:04:24 -0500 | [diff] [blame] | 5629 | } |
Alex Elder | ecb4dc2 | 2013-04-26 09:43:47 -0500 | [diff] [blame] | 5630 | snap_name = kmemdup(buf, len + 1, GFP_KERNEL); |
| 5631 | if (!snap_name) |
Alex Elder | f28e565 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5632 | goto out_mem; |
Alex Elder | ecb4dc2 | 2013-04-26 09:43:47 -0500 | [diff] [blame] | 5633 | *(snap_name + len) = '\0'; |
| 5634 | spec->snap_name = snap_name; |
Alex Elder | e5c3553 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5635 | |
Alex Elder | 0ddebc0 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5636 | /* Initialize all rbd options to the defaults */ |
Alex Elder | e28fff26 | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 5637 | |
Alex Elder | 4e9afeb | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5638 | rbd_opts = kzalloc(sizeof (*rbd_opts), GFP_KERNEL); |
| 5639 | if (!rbd_opts) |
| 5640 | goto out_mem; |
| 5641 | |
| 5642 | rbd_opts->read_only = RBD_READ_ONLY_DEFAULT; |
Ilya Dryomov | b558418 | 2015-06-23 16:21:19 +0300 | [diff] [blame] | 5643 | rbd_opts->queue_depth = RBD_QUEUE_DEPTH_DEFAULT; |
Ilya Dryomov | 80de191 | 2016-09-20 14:23:17 +0200 | [diff] [blame] | 5644 | rbd_opts->lock_on_read = RBD_LOCK_ON_READ_DEFAULT; |
Ilya Dryomov | e010dd0 | 2017-04-13 12:17:39 +0200 | [diff] [blame] | 5645 | rbd_opts->exclusive = RBD_EXCLUSIVE_DEFAULT; |
Alex Elder | d22f76e | 2012-07-12 10:46:35 -0500 | [diff] [blame] | 5646 | |
Alex Elder | 859c31d | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5647 | copts = ceph_parse_options(options, mon_addrs, |
Alex Elder | 0ddebc0 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5648 | mon_addrs + mon_addrs_size - 1, |
Alex Elder | 4e9afeb | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5649 | parse_rbd_opts_token, rbd_opts); |
Alex Elder | 859c31d | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5650 | if (IS_ERR(copts)) { |
| 5651 | ret = PTR_ERR(copts); |
Alex Elder | dc79b11 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5652 | goto out_err; |
| 5653 | } |
Alex Elder | 859c31d | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5654 | kfree(options); |
| 5655 | |
| 5656 | *ceph_opts = copts; |
Alex Elder | 4e9afeb | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5657 | *opts = rbd_opts; |
Alex Elder | 859c31d | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5658 | *rbd_spec = spec; |
Alex Elder | 0ddebc0 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5659 | |
Alex Elder | dc79b11 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5660 | return 0; |
Alex Elder | f28e565 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5661 | out_mem: |
Alex Elder | dc79b11 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5662 | ret = -ENOMEM; |
Alex Elder | d22f76e | 2012-07-12 10:46:35 -0500 | [diff] [blame] | 5663 | out_err: |
Alex Elder | 859c31d | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5664 | kfree(rbd_opts); |
| 5665 | rbd_spec_put(spec); |
Alex Elder | f28e565 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5666 | kfree(options); |
Alex Elder | d22f76e | 2012-07-12 10:46:35 -0500 | [diff] [blame] | 5667 | |
Alex Elder | dc79b11 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5668 | return ret; |
Alex Elder | a725f65e | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 5669 | } |
| 5670 | |
Alex Elder | 589d30e | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 5671 | /* |
Ilya Dryomov | 30ba1f0 | 2014-05-13 11:19:27 +0400 | [diff] [blame] | 5672 | * Return pool id (>= 0) or a negative error code. |
| 5673 | */ |
| 5674 | static int rbd_add_get_pool_id(struct rbd_client *rbdc, const char *pool_name) |
| 5675 | { |
Ilya Dryomov | a319bf5 | 2015-05-15 12:02:17 +0300 | [diff] [blame] | 5676 | struct ceph_options *opts = rbdc->client->options; |
Ilya Dryomov | 30ba1f0 | 2014-05-13 11:19:27 +0400 | [diff] [blame] | 5677 | u64 newest_epoch; |
Ilya Dryomov | 30ba1f0 | 2014-05-13 11:19:27 +0400 | [diff] [blame] | 5678 | int tries = 0; |
| 5679 | int ret; |
| 5680 | |
| 5681 | again: |
| 5682 | ret = ceph_pg_poolid_by_name(rbdc->client->osdc.osdmap, pool_name); |
| 5683 | if (ret == -ENOENT && tries++ < 1) { |
Ilya Dryomov | d0b1970 | 2016-04-28 16:07:27 +0200 | [diff] [blame] | 5684 | ret = ceph_monc_get_version(&rbdc->client->monc, "osdmap", |
| 5685 | &newest_epoch); |
Ilya Dryomov | 30ba1f0 | 2014-05-13 11:19:27 +0400 | [diff] [blame] | 5686 | if (ret < 0) |
| 5687 | return ret; |
| 5688 | |
| 5689 | if (rbdc->client->osdc.osdmap->epoch < newest_epoch) { |
Ilya Dryomov | 7cca78c | 2016-04-28 16:07:28 +0200 | [diff] [blame] | 5690 | ceph_osdc_maybe_request_map(&rbdc->client->osdc); |
Ilya Dryomov | 30ba1f0 | 2014-05-13 11:19:27 +0400 | [diff] [blame] | 5691 | (void) ceph_monc_wait_osdmap(&rbdc->client->monc, |
Ilya Dryomov | a319bf5 | 2015-05-15 12:02:17 +0300 | [diff] [blame] | 5692 | newest_epoch, |
| 5693 | opts->mount_timeout); |
Ilya Dryomov | 30ba1f0 | 2014-05-13 11:19:27 +0400 | [diff] [blame] | 5694 | goto again; |
| 5695 | } else { |
| 5696 | /* the osdmap we have is new enough */ |
| 5697 | return -ENOENT; |
| 5698 | } |
| 5699 | } |
| 5700 | |
| 5701 | return ret; |
| 5702 | } |
| 5703 | |
Ilya Dryomov | e010dd0 | 2017-04-13 12:17:39 +0200 | [diff] [blame] | 5704 | static void rbd_dev_image_unlock(struct rbd_device *rbd_dev) |
| 5705 | { |
| 5706 | down_write(&rbd_dev->lock_rwsem); |
| 5707 | if (__rbd_is_lock_owner(rbd_dev)) |
| 5708 | rbd_unlock(rbd_dev); |
| 5709 | up_write(&rbd_dev->lock_rwsem); |
| 5710 | } |
| 5711 | |
| 5712 | static int rbd_add_acquire_lock(struct rbd_device *rbd_dev) |
| 5713 | { |
| 5714 | if (!(rbd_dev->header.features & RBD_FEATURE_EXCLUSIVE_LOCK)) { |
| 5715 | rbd_warn(rbd_dev, "exclusive-lock feature is not enabled"); |
| 5716 | return -EINVAL; |
| 5717 | } |
| 5718 | |
| 5719 | /* FIXME: "rbd map --exclusive" should be in interruptible */ |
| 5720 | down_read(&rbd_dev->lock_rwsem); |
| 5721 | rbd_wait_state_locked(rbd_dev); |
| 5722 | up_read(&rbd_dev->lock_rwsem); |
| 5723 | if (test_bit(RBD_DEV_FLAG_BLACKLISTED, &rbd_dev->flags)) { |
| 5724 | rbd_warn(rbd_dev, "failed to acquire exclusive lock"); |
| 5725 | return -EROFS; |
| 5726 | } |
| 5727 | |
| 5728 | return 0; |
| 5729 | } |
| 5730 | |
Ilya Dryomov | 30ba1f0 | 2014-05-13 11:19:27 +0400 | [diff] [blame] | 5731 | /* |
Alex Elder | 589d30e | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 5732 | * An rbd format 2 image has a unique identifier, distinct from the |
| 5733 | * name given to it by the user. Internally, that identifier is |
| 5734 | * what's used to specify the names of objects related to the image. |
| 5735 | * |
| 5736 | * A special "rbd id" object is used to map an rbd image name to its |
| 5737 | * id. If that object doesn't exist, then there is no v2 rbd image |
| 5738 | * with the supplied name. |
| 5739 | * |
| 5740 | * This function will record the given rbd_dev's image_id field if |
| 5741 | * it can be determined, and in that case will return 0. If any |
| 5742 | * errors occur a negative errno will be returned and the rbd_dev's |
| 5743 | * image_id field will be unchanged (and should be NULL). |
| 5744 | */ |
| 5745 | static int rbd_dev_image_id(struct rbd_device *rbd_dev) |
| 5746 | { |
| 5747 | int ret; |
| 5748 | size_t size; |
Ilya Dryomov | ecd4a68 | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 5749 | CEPH_DEFINE_OID_ONSTACK(oid); |
Alex Elder | 589d30e | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 5750 | void *response; |
Alex Elder | c0fba36 | 2013-04-25 23:15:08 -0500 | [diff] [blame] | 5751 | char *image_id; |
Alex Elder | 2f82ee5 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 5752 | |
Alex Elder | 589d30e | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 5753 | /* |
Alex Elder | 2c0d0a1 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 5754 | * When probing a parent image, the image id is already |
| 5755 | * known (and the image name likely is not). There's no |
Alex Elder | c0fba36 | 2013-04-25 23:15:08 -0500 | [diff] [blame] | 5756 | * need to fetch the image id again in this case. We |
| 5757 | * do still need to set the image format though. |
Alex Elder | 2c0d0a1 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 5758 | */ |
Alex Elder | c0fba36 | 2013-04-25 23:15:08 -0500 | [diff] [blame] | 5759 | if (rbd_dev->spec->image_id) { |
| 5760 | rbd_dev->image_format = *rbd_dev->spec->image_id ? 2 : 1; |
| 5761 | |
Alex Elder | 2c0d0a1 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 5762 | return 0; |
Alex Elder | c0fba36 | 2013-04-25 23:15:08 -0500 | [diff] [blame] | 5763 | } |
Alex Elder | 2c0d0a1 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 5764 | |
| 5765 | /* |
Alex Elder | 589d30e | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 5766 | * First, see if the format 2 image id file exists, and if |
| 5767 | * so, get the image's persistent id from it. |
| 5768 | */ |
Ilya Dryomov | ecd4a68 | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 5769 | ret = ceph_oid_aprintf(&oid, GFP_KERNEL, "%s%s", RBD_ID_PREFIX, |
| 5770 | rbd_dev->spec->image_name); |
| 5771 | if (ret) |
| 5772 | return ret; |
| 5773 | |
| 5774 | dout("rbd id object name is %s\n", oid.name); |
Alex Elder | 589d30e | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 5775 | |
| 5776 | /* Response will be an encoded string, which includes a length */ |
| 5777 | |
| 5778 | size = sizeof (__le32) + RBD_IMAGE_ID_LEN_MAX; |
| 5779 | response = kzalloc(size, GFP_NOIO); |
| 5780 | if (!response) { |
| 5781 | ret = -ENOMEM; |
| 5782 | goto out; |
| 5783 | } |
| 5784 | |
Alex Elder | c0fba36 | 2013-04-25 23:15:08 -0500 | [diff] [blame] | 5785 | /* If it doesn't exist we'll assume it's a format 1 image */ |
| 5786 | |
Ilya Dryomov | ecd4a68 | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 5787 | ret = rbd_obj_method_sync(rbd_dev, &oid, &rbd_dev->header_oloc, |
| 5788 | "get_id", NULL, 0, |
| 5789 | response, RBD_IMAGE_ID_LEN_MAX); |
Alex Elder | 36be9a7 | 2013-01-19 00:30:28 -0600 | [diff] [blame] | 5790 | dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret); |
Alex Elder | c0fba36 | 2013-04-25 23:15:08 -0500 | [diff] [blame] | 5791 | if (ret == -ENOENT) { |
| 5792 | image_id = kstrdup("", GFP_KERNEL); |
| 5793 | ret = image_id ? 0 : -ENOMEM; |
| 5794 | if (!ret) |
| 5795 | rbd_dev->image_format = 1; |
Ilya Dryomov | 7dd440c | 2014-09-11 18:49:18 +0400 | [diff] [blame] | 5796 | } else if (ret >= 0) { |
Alex Elder | c0fba36 | 2013-04-25 23:15:08 -0500 | [diff] [blame] | 5797 | void *p = response; |
Alex Elder | 589d30e | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 5798 | |
Alex Elder | c0fba36 | 2013-04-25 23:15:08 -0500 | [diff] [blame] | 5799 | image_id = ceph_extract_encoded_string(&p, p + ret, |
Alex Elder | 979ed48 | 2012-11-01 08:39:26 -0500 | [diff] [blame] | 5800 | NULL, GFP_NOIO); |
Duan Jiong | 461f758 | 2014-04-11 16:38:12 +0800 | [diff] [blame] | 5801 | ret = PTR_ERR_OR_ZERO(image_id); |
Alex Elder | c0fba36 | 2013-04-25 23:15:08 -0500 | [diff] [blame] | 5802 | if (!ret) |
| 5803 | rbd_dev->image_format = 2; |
Alex Elder | c0fba36 | 2013-04-25 23:15:08 -0500 | [diff] [blame] | 5804 | } |
| 5805 | |
| 5806 | if (!ret) { |
| 5807 | rbd_dev->spec->image_id = image_id; |
| 5808 | dout("image_id is %s\n", image_id); |
Alex Elder | 589d30e | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 5809 | } |
| 5810 | out: |
| 5811 | kfree(response); |
Ilya Dryomov | ecd4a68 | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 5812 | ceph_oid_destroy(&oid); |
Alex Elder | 589d30e | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 5813 | return ret; |
| 5814 | } |
| 5815 | |
Alex Elder | 3abef3b | 2013-05-13 20:35:37 -0500 | [diff] [blame] | 5816 | /* |
| 5817 | * Undo whatever state changes are made by v1 or v2 header info |
| 5818 | * call. |
| 5819 | */ |
Alex Elder | 6fd48b3 | 2013-04-28 23:32:34 -0500 | [diff] [blame] | 5820 | static void rbd_dev_unprobe(struct rbd_device *rbd_dev) |
| 5821 | { |
| 5822 | struct rbd_image_header *header; |
| 5823 | |
Ilya Dryomov | e69b8d4 | 2015-01-19 12:06:14 +0300 | [diff] [blame] | 5824 | rbd_dev_parent_put(rbd_dev); |
Alex Elder | 6fd48b3 | 2013-04-28 23:32:34 -0500 | [diff] [blame] | 5825 | |
| 5826 | /* Free dynamic fields from the header, then zero it out */ |
| 5827 | |
| 5828 | header = &rbd_dev->header; |
Alex Elder | 812164f8 | 2013-04-30 00:44:32 -0500 | [diff] [blame] | 5829 | ceph_put_snap_context(header->snapc); |
Alex Elder | 6fd48b3 | 2013-04-28 23:32:34 -0500 | [diff] [blame] | 5830 | kfree(header->snap_sizes); |
| 5831 | kfree(header->snap_names); |
| 5832 | kfree(header->object_prefix); |
| 5833 | memset(header, 0, sizeof (*header)); |
| 5834 | } |
| 5835 | |
Alex Elder | 2df3fac | 2013-05-06 09:51:30 -0500 | [diff] [blame] | 5836 | static int rbd_dev_v2_header_onetime(struct rbd_device *rbd_dev) |
Alex Elder | a30b71b | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 5837 | { |
| 5838 | int ret; |
Alex Elder | a30b71b | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 5839 | |
Alex Elder | 1e13019 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5840 | ret = rbd_dev_v2_object_prefix(rbd_dev); |
Alex Elder | 57385b5 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 5841 | if (ret) |
Alex Elder | 1e13019 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5842 | goto out_err; |
Alex Elder | b1b5402 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5843 | |
Alex Elder | 2df3fac | 2013-05-06 09:51:30 -0500 | [diff] [blame] | 5844 | /* |
| 5845 | * Get the and check features for the image. Currently the |
| 5846 | * features are assumed to never change. |
| 5847 | */ |
Alex Elder | b1b5402 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5848 | ret = rbd_dev_v2_features(rbd_dev); |
Alex Elder | 57385b5 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 5849 | if (ret) |
Alex Elder | b1b5402 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5850 | goto out_err; |
Alex Elder | 35d489f | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5851 | |
Alex Elder | cc070d5 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 5852 | /* If the image supports fancy striping, get its parameters */ |
| 5853 | |
| 5854 | if (rbd_dev->header.features & RBD_FEATURE_STRIPINGV2) { |
| 5855 | ret = rbd_dev_v2_striping_info(rbd_dev); |
| 5856 | if (ret < 0) |
| 5857 | goto out_err; |
| 5858 | } |
Alex Elder | a30b71b | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 5859 | |
Ilya Dryomov | 7e97332 | 2017-01-25 18:16:22 +0100 | [diff] [blame] | 5860 | if (rbd_dev->header.features & RBD_FEATURE_DATA_POOL) { |
| 5861 | ret = rbd_dev_v2_data_pool(rbd_dev); |
| 5862 | if (ret) |
| 5863 | goto out_err; |
| 5864 | } |
| 5865 | |
Ilya Dryomov | 263423f | 2017-01-25 18:16:22 +0100 | [diff] [blame] | 5866 | rbd_init_layout(rbd_dev); |
Alex Elder | 3515297 | 2012-08-31 17:29:55 -0500 | [diff] [blame] | 5867 | return 0; |
Ilya Dryomov | 263423f | 2017-01-25 18:16:22 +0100 | [diff] [blame] | 5868 | |
Alex Elder | 9d475de | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5869 | out_err: |
Alex Elder | 642a253 | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 5870 | rbd_dev->header.features = 0; |
Alex Elder | 1e13019 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5871 | kfree(rbd_dev->header.object_prefix); |
| 5872 | rbd_dev->header.object_prefix = NULL; |
Alex Elder | 9d475de | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5873 | return ret; |
Alex Elder | a30b71b | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 5874 | } |
| 5875 | |
Ilya Dryomov | 6d69bb53 | 2015-10-11 19:38:00 +0200 | [diff] [blame] | 5876 | /* |
| 5877 | * @depth is rbd_dev_image_probe() -> rbd_dev_probe_parent() -> |
| 5878 | * rbd_dev_image_probe() recursion depth, which means it's also the |
| 5879 | * length of the already discovered part of the parent chain. |
| 5880 | */ |
| 5881 | 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] | 5882 | { |
Alex Elder | 2f82ee5 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 5883 | struct rbd_device *parent = NULL; |
Alex Elder | 124afba | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 5884 | int ret; |
| 5885 | |
| 5886 | if (!rbd_dev->parent_spec) |
| 5887 | return 0; |
Alex Elder | 124afba | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 5888 | |
Ilya Dryomov | 6d69bb53 | 2015-10-11 19:38:00 +0200 | [diff] [blame] | 5889 | if (++depth > RBD_MAX_PARENT_CHAIN_LEN) { |
| 5890 | pr_info("parent chain is too long (%d)\n", depth); |
| 5891 | ret = -EINVAL; |
| 5892 | goto out_err; |
| 5893 | } |
| 5894 | |
Ilya Dryomov | 1643dfa | 2016-08-12 15:45:52 +0200 | [diff] [blame] | 5895 | parent = __rbd_dev_create(rbd_dev->rbd_client, rbd_dev->parent_spec); |
Ilya Dryomov | 1f2c665 | 2015-10-11 19:38:00 +0200 | [diff] [blame] | 5896 | if (!parent) { |
| 5897 | ret = -ENOMEM; |
Alex Elder | 124afba | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 5898 | goto out_err; |
Ilya Dryomov | 1f2c665 | 2015-10-11 19:38:00 +0200 | [diff] [blame] | 5899 | } |
| 5900 | |
| 5901 | /* |
| 5902 | * Images related by parent/child relationships always share |
| 5903 | * rbd_client and spec/parent_spec, so bump their refcounts. |
| 5904 | */ |
| 5905 | __rbd_get_client(rbd_dev->rbd_client); |
| 5906 | rbd_spec_get(rbd_dev->parent_spec); |
Alex Elder | 124afba | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 5907 | |
Ilya Dryomov | 6d69bb53 | 2015-10-11 19:38:00 +0200 | [diff] [blame] | 5908 | ret = rbd_dev_image_probe(parent, depth); |
Alex Elder | 124afba | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 5909 | if (ret < 0) |
| 5910 | goto out_err; |
Ilya Dryomov | 1f2c665 | 2015-10-11 19:38:00 +0200 | [diff] [blame] | 5911 | |
Alex Elder | 124afba | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 5912 | rbd_dev->parent = parent; |
Alex Elder | a2acd00 | 2013-05-08 22:50:04 -0500 | [diff] [blame] | 5913 | atomic_set(&rbd_dev->parent_ref, 1); |
Alex Elder | 124afba | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 5914 | return 0; |
Alex Elder | 124afba | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 5915 | |
Ilya Dryomov | 1f2c665 | 2015-10-11 19:38:00 +0200 | [diff] [blame] | 5916 | out_err: |
| 5917 | rbd_dev_unparent(rbd_dev); |
Markus Elfring | 1761b22 | 2015-11-23 20:16:45 +0100 | [diff] [blame] | 5918 | rbd_dev_destroy(parent); |
Alex Elder | 124afba | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 5919 | return ret; |
| 5920 | } |
| 5921 | |
Ilya Dryomov | 5769ed0 | 2017-04-13 12:17:38 +0200 | [diff] [blame] | 5922 | static void rbd_dev_device_release(struct rbd_device *rbd_dev) |
| 5923 | { |
| 5924 | clear_bit(RBD_DEV_FLAG_EXISTS, &rbd_dev->flags); |
| 5925 | rbd_dev_mapping_clear(rbd_dev); |
| 5926 | rbd_free_disk(rbd_dev); |
| 5927 | if (!single_major) |
| 5928 | unregister_blkdev(rbd_dev->major, rbd_dev->name); |
| 5929 | } |
| 5930 | |
Ilya Dryomov | 811c668 | 2016-04-15 16:22:16 +0200 | [diff] [blame] | 5931 | /* |
| 5932 | * rbd_dev->header_rwsem must be locked for write and will be unlocked |
| 5933 | * upon return. |
| 5934 | */ |
Alex Elder | 200a6a8 | 2013-04-28 23:32:34 -0500 | [diff] [blame] | 5935 | static int rbd_dev_device_setup(struct rbd_device *rbd_dev) |
Alex Elder | 124afba | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 5936 | { |
Alex Elder | 83a0626 | 2012-10-30 15:47:17 -0500 | [diff] [blame] | 5937 | int ret; |
Alex Elder | 83a0626 | 2012-10-30 15:47:17 -0500 | [diff] [blame] | 5938 | |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 5939 | /* Record our major and minor device numbers. */ |
Alex Elder | 83a0626 | 2012-10-30 15:47:17 -0500 | [diff] [blame] | 5940 | |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 5941 | if (!single_major) { |
| 5942 | ret = register_blkdev(0, rbd_dev->name); |
| 5943 | if (ret < 0) |
Ilya Dryomov | 1643dfa | 2016-08-12 15:45:52 +0200 | [diff] [blame] | 5944 | goto err_out_unlock; |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 5945 | |
| 5946 | rbd_dev->major = ret; |
| 5947 | rbd_dev->minor = 0; |
| 5948 | } else { |
| 5949 | rbd_dev->major = rbd_major; |
| 5950 | rbd_dev->minor = rbd_dev_id_to_minor(rbd_dev->dev_id); |
| 5951 | } |
Alex Elder | 83a0626 | 2012-10-30 15:47:17 -0500 | [diff] [blame] | 5952 | |
| 5953 | /* Set up the blkdev mapping. */ |
| 5954 | |
| 5955 | ret = rbd_init_disk(rbd_dev); |
| 5956 | if (ret) |
| 5957 | goto err_out_blkdev; |
| 5958 | |
Alex Elder | f35a4de | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 5959 | ret = rbd_dev_mapping_set(rbd_dev); |
Alex Elder | 83a0626 | 2012-10-30 15:47:17 -0500 | [diff] [blame] | 5960 | if (ret) |
| 5961 | goto err_out_disk; |
Ilya Dryomov | bc1ecc6 | 2014-08-04 18:04:39 +0400 | [diff] [blame] | 5962 | |
Alex Elder | f35a4de | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 5963 | set_capacity(rbd_dev->disk, rbd_dev->mapping.size / SECTOR_SIZE); |
Ilya Dryomov | 9568c93 | 2017-10-12 12:35:19 +0200 | [diff] [blame^] | 5964 | set_disk_ro(rbd_dev->disk, rbd_dev->opts->read_only); |
Alex Elder | f35a4de | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 5965 | |
Ilya Dryomov | 5769ed0 | 2017-04-13 12:17:38 +0200 | [diff] [blame] | 5966 | ret = dev_set_name(&rbd_dev->dev, "%d", rbd_dev->dev_id); |
Alex Elder | f35a4de | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 5967 | if (ret) |
Ilya Dryomov | f5ee37b | 2014-10-09 17:06:01 +0400 | [diff] [blame] | 5968 | goto err_out_mapping; |
Alex Elder | 83a0626 | 2012-10-30 15:47:17 -0500 | [diff] [blame] | 5969 | |
Alex Elder | 129b79d | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 5970 | set_bit(RBD_DEV_FLAG_EXISTS, &rbd_dev->flags); |
Ilya Dryomov | 811c668 | 2016-04-15 16:22:16 +0200 | [diff] [blame] | 5971 | up_write(&rbd_dev->header_rwsem); |
Ilya Dryomov | 5769ed0 | 2017-04-13 12:17:38 +0200 | [diff] [blame] | 5972 | return 0; |
Alex Elder | 2f82ee5 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 5973 | |
Alex Elder | f35a4de | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 5974 | err_out_mapping: |
| 5975 | rbd_dev_mapping_clear(rbd_dev); |
Alex Elder | 83a0626 | 2012-10-30 15:47:17 -0500 | [diff] [blame] | 5976 | err_out_disk: |
| 5977 | rbd_free_disk(rbd_dev); |
| 5978 | err_out_blkdev: |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 5979 | if (!single_major) |
| 5980 | unregister_blkdev(rbd_dev->major, rbd_dev->name); |
Ilya Dryomov | 811c668 | 2016-04-15 16:22:16 +0200 | [diff] [blame] | 5981 | err_out_unlock: |
| 5982 | up_write(&rbd_dev->header_rwsem); |
Alex Elder | 83a0626 | 2012-10-30 15:47:17 -0500 | [diff] [blame] | 5983 | return ret; |
| 5984 | } |
| 5985 | |
Alex Elder | 332bb12 | 2013-04-27 09:59:30 -0500 | [diff] [blame] | 5986 | static int rbd_dev_header_name(struct rbd_device *rbd_dev) |
| 5987 | { |
| 5988 | struct rbd_spec *spec = rbd_dev->spec; |
Ilya Dryomov | c41d13a | 2016-04-29 20:01:25 +0200 | [diff] [blame] | 5989 | int ret; |
Alex Elder | 332bb12 | 2013-04-27 09:59:30 -0500 | [diff] [blame] | 5990 | |
| 5991 | /* Record the header object name for this rbd image. */ |
| 5992 | |
| 5993 | rbd_assert(rbd_image_format_valid(rbd_dev->image_format)); |
Alex Elder | 332bb12 | 2013-04-27 09:59:30 -0500 | [diff] [blame] | 5994 | if (rbd_dev->image_format == 1) |
Ilya Dryomov | c41d13a | 2016-04-29 20:01:25 +0200 | [diff] [blame] | 5995 | ret = ceph_oid_aprintf(&rbd_dev->header_oid, GFP_KERNEL, "%s%s", |
| 5996 | spec->image_name, RBD_SUFFIX); |
Alex Elder | 332bb12 | 2013-04-27 09:59:30 -0500 | [diff] [blame] | 5997 | else |
Ilya Dryomov | c41d13a | 2016-04-29 20:01:25 +0200 | [diff] [blame] | 5998 | ret = ceph_oid_aprintf(&rbd_dev->header_oid, GFP_KERNEL, "%s%s", |
| 5999 | RBD_HEADER_PREFIX, spec->image_id); |
Alex Elder | 332bb12 | 2013-04-27 09:59:30 -0500 | [diff] [blame] | 6000 | |
Ilya Dryomov | c41d13a | 2016-04-29 20:01:25 +0200 | [diff] [blame] | 6001 | return ret; |
Alex Elder | 332bb12 | 2013-04-27 09:59:30 -0500 | [diff] [blame] | 6002 | } |
| 6003 | |
Alex Elder | 200a6a8 | 2013-04-28 23:32:34 -0500 | [diff] [blame] | 6004 | static void rbd_dev_image_release(struct rbd_device *rbd_dev) |
| 6005 | { |
Alex Elder | 6fd48b3 | 2013-04-28 23:32:34 -0500 | [diff] [blame] | 6006 | rbd_dev_unprobe(rbd_dev); |
Ilya Dryomov | fd22aef | 2017-04-13 12:17:37 +0200 | [diff] [blame] | 6007 | if (rbd_dev->opts) |
| 6008 | rbd_unregister_watch(rbd_dev); |
Alex Elder | 6fd48b3 | 2013-04-28 23:32:34 -0500 | [diff] [blame] | 6009 | rbd_dev->image_format = 0; |
| 6010 | kfree(rbd_dev->spec->image_id); |
| 6011 | rbd_dev->spec->image_id = NULL; |
Alex Elder | 200a6a8 | 2013-04-28 23:32:34 -0500 | [diff] [blame] | 6012 | } |
| 6013 | |
Alex Elder | a30b71b | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 6014 | /* |
| 6015 | * Probe for the existence of the header object for the given rbd |
Alex Elder | 1f3ef78 | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 6016 | * device. If this image is the one being mapped (i.e., not a |
| 6017 | * parent), initiate a watch on its header object before using that |
| 6018 | * object to get detailed information about the rbd image. |
Alex Elder | a30b71b | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 6019 | */ |
Ilya Dryomov | 6d69bb53 | 2015-10-11 19:38:00 +0200 | [diff] [blame] | 6020 | 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] | 6021 | { |
| 6022 | int ret; |
| 6023 | |
| 6024 | /* |
Alex Elder | 3abef3b | 2013-05-13 20:35:37 -0500 | [diff] [blame] | 6025 | * Get the id from the image id object. Unless there's an |
| 6026 | * error, rbd_dev->spec->image_id will be filled in with |
| 6027 | * a dynamically-allocated string, and rbd_dev->image_format |
| 6028 | * will be set to either 1 or 2. |
Alex Elder | a30b71b | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 6029 | */ |
| 6030 | ret = rbd_dev_image_id(rbd_dev); |
| 6031 | if (ret) |
Alex Elder | c0fba36 | 2013-04-25 23:15:08 -0500 | [diff] [blame] | 6032 | return ret; |
Alex Elder | c0fba36 | 2013-04-25 23:15:08 -0500 | [diff] [blame] | 6033 | |
Alex Elder | 332bb12 | 2013-04-27 09:59:30 -0500 | [diff] [blame] | 6034 | ret = rbd_dev_header_name(rbd_dev); |
| 6035 | if (ret) |
| 6036 | goto err_out_format; |
| 6037 | |
Ilya Dryomov | 6d69bb53 | 2015-10-11 19:38:00 +0200 | [diff] [blame] | 6038 | if (!depth) { |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 6039 | ret = rbd_register_watch(rbd_dev); |
Ilya Dryomov | 1fe4802 | 2015-03-05 10:47:22 +0300 | [diff] [blame] | 6040 | if (ret) { |
| 6041 | if (ret == -ENOENT) |
| 6042 | pr_info("image %s/%s does not exist\n", |
| 6043 | rbd_dev->spec->pool_name, |
| 6044 | rbd_dev->spec->image_name); |
Ilya Dryomov | c41d13a | 2016-04-29 20:01:25 +0200 | [diff] [blame] | 6045 | goto err_out_format; |
Ilya Dryomov | 1fe4802 | 2015-03-05 10:47:22 +0300 | [diff] [blame] | 6046 | } |
Alex Elder | 1f3ef78 | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 6047 | } |
Alex Elder | b644de2 | 2013-04-27 09:59:31 -0500 | [diff] [blame] | 6048 | |
Ilya Dryomov | a720ae0 | 2014-07-23 17:11:19 +0400 | [diff] [blame] | 6049 | ret = rbd_dev_header_info(rbd_dev); |
Alex Elder | 5655c4d | 2013-04-25 23:15:08 -0500 | [diff] [blame] | 6050 | if (ret) |
Alex Elder | b644de2 | 2013-04-27 09:59:31 -0500 | [diff] [blame] | 6051 | goto err_out_watch; |
Alex Elder | 83a0626 | 2012-10-30 15:47:17 -0500 | [diff] [blame] | 6052 | |
Ilya Dryomov | 0407759 | 2014-07-23 17:11:20 +0400 | [diff] [blame] | 6053 | /* |
| 6054 | * If this image is the one being mapped, we have pool name and |
| 6055 | * id, image name and id, and snap name - need to fill snap id. |
| 6056 | * Otherwise this is a parent image, identified by pool, image |
| 6057 | * and snap ids - need to fill in names for those ids. |
| 6058 | */ |
Ilya Dryomov | 6d69bb53 | 2015-10-11 19:38:00 +0200 | [diff] [blame] | 6059 | if (!depth) |
Ilya Dryomov | 0407759 | 2014-07-23 17:11:20 +0400 | [diff] [blame] | 6060 | ret = rbd_spec_fill_snap_id(rbd_dev); |
| 6061 | else |
| 6062 | ret = rbd_spec_fill_names(rbd_dev); |
Ilya Dryomov | 1fe4802 | 2015-03-05 10:47:22 +0300 | [diff] [blame] | 6063 | if (ret) { |
| 6064 | if (ret == -ENOENT) |
| 6065 | pr_info("snap %s/%s@%s does not exist\n", |
| 6066 | rbd_dev->spec->pool_name, |
| 6067 | rbd_dev->spec->image_name, |
| 6068 | rbd_dev->spec->snap_name); |
Alex Elder | 33dca39 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 6069 | goto err_out_probe; |
Ilya Dryomov | 1fe4802 | 2015-03-05 10:47:22 +0300 | [diff] [blame] | 6070 | } |
Alex Elder | 9bb81c9 | 2013-04-27 09:59:30 -0500 | [diff] [blame] | 6071 | |
Ilya Dryomov | e8f59b5 | 2014-07-24 10:42:13 +0400 | [diff] [blame] | 6072 | if (rbd_dev->header.features & RBD_FEATURE_LAYERING) { |
| 6073 | ret = rbd_dev_v2_parent_info(rbd_dev); |
| 6074 | if (ret) |
| 6075 | goto err_out_probe; |
| 6076 | |
| 6077 | /* |
| 6078 | * Need to warn users if this image is the one being |
| 6079 | * mapped and has a parent. |
| 6080 | */ |
Ilya Dryomov | 6d69bb53 | 2015-10-11 19:38:00 +0200 | [diff] [blame] | 6081 | if (!depth && rbd_dev->parent_spec) |
Ilya Dryomov | e8f59b5 | 2014-07-24 10:42:13 +0400 | [diff] [blame] | 6082 | rbd_warn(rbd_dev, |
| 6083 | "WARNING: kernel layering is EXPERIMENTAL!"); |
| 6084 | } |
| 6085 | |
Ilya Dryomov | 6d69bb53 | 2015-10-11 19:38:00 +0200 | [diff] [blame] | 6086 | ret = rbd_dev_probe_parent(rbd_dev, depth); |
Alex Elder | 30d60ba | 2013-05-06 09:51:30 -0500 | [diff] [blame] | 6087 | if (ret) |
| 6088 | goto err_out_probe; |
Alex Elder | 83a0626 | 2012-10-30 15:47:17 -0500 | [diff] [blame] | 6089 | |
Alex Elder | 30d60ba | 2013-05-06 09:51:30 -0500 | [diff] [blame] | 6090 | dout("discovered format %u image, header name is %s\n", |
Ilya Dryomov | c41d13a | 2016-04-29 20:01:25 +0200 | [diff] [blame] | 6091 | rbd_dev->image_format, rbd_dev->header_oid.name); |
Alex Elder | 30d60ba | 2013-05-06 09:51:30 -0500 | [diff] [blame] | 6092 | return 0; |
Ilya Dryomov | e8f59b5 | 2014-07-24 10:42:13 +0400 | [diff] [blame] | 6093 | |
Alex Elder | 6fd48b3 | 2013-04-28 23:32:34 -0500 | [diff] [blame] | 6094 | err_out_probe: |
| 6095 | rbd_dev_unprobe(rbd_dev); |
Alex Elder | b644de2 | 2013-04-27 09:59:31 -0500 | [diff] [blame] | 6096 | err_out_watch: |
Ilya Dryomov | 6d69bb53 | 2015-10-11 19:38:00 +0200 | [diff] [blame] | 6097 | if (!depth) |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 6098 | rbd_unregister_watch(rbd_dev); |
Alex Elder | 332bb12 | 2013-04-27 09:59:30 -0500 | [diff] [blame] | 6099 | err_out_format: |
| 6100 | rbd_dev->image_format = 0; |
Alex Elder | 5655c4d | 2013-04-25 23:15:08 -0500 | [diff] [blame] | 6101 | kfree(rbd_dev->spec->image_id); |
| 6102 | rbd_dev->spec->image_id = NULL; |
Alex Elder | 5655c4d | 2013-04-25 23:15:08 -0500 | [diff] [blame] | 6103 | return ret; |
Alex Elder | a30b71b | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 6104 | } |
| 6105 | |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 6106 | static ssize_t do_rbd_add(struct bus_type *bus, |
| 6107 | const char *buf, |
| 6108 | size_t count) |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6109 | { |
Alex Elder | cb8627c | 2012-07-09 21:04:23 -0500 | [diff] [blame] | 6110 | struct rbd_device *rbd_dev = NULL; |
Alex Elder | dc79b11 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 6111 | struct ceph_options *ceph_opts = NULL; |
Alex Elder | 4e9afeb | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 6112 | struct rbd_options *rbd_opts = NULL; |
Alex Elder | 859c31d | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 6113 | struct rbd_spec *spec = NULL; |
Alex Elder | 9d3997f | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 6114 | struct rbd_client *rbdc; |
Ilya Dryomov | b51c83c | 2015-10-15 15:38:57 +0200 | [diff] [blame] | 6115 | int rc; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6116 | |
| 6117 | if (!try_module_get(THIS_MODULE)) |
| 6118 | return -ENODEV; |
| 6119 | |
Alex Elder | a725f65e | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 6120 | /* parse add command */ |
Alex Elder | 859c31d | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 6121 | rc = rbd_add_parse_args(buf, &ceph_opts, &rbd_opts, &spec); |
Alex Elder | dc79b11 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 6122 | if (rc < 0) |
Ilya Dryomov | dd5ac32 | 2015-10-16 17:09:24 +0200 | [diff] [blame] | 6123 | goto out; |
Alex Elder | a725f65e | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 6124 | |
Alex Elder | 9d3997f | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 6125 | rbdc = rbd_get_client(ceph_opts); |
| 6126 | if (IS_ERR(rbdc)) { |
| 6127 | rc = PTR_ERR(rbdc); |
Alex Elder | 0ddebc0 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 6128 | goto err_out_args; |
Alex Elder | 9d3997f | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 6129 | } |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6130 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6131 | /* pick the pool */ |
Ilya Dryomov | 30ba1f0 | 2014-05-13 11:19:27 +0400 | [diff] [blame] | 6132 | rc = rbd_add_get_pool_id(rbdc, spec->pool_name); |
Ilya Dryomov | 1fe4802 | 2015-03-05 10:47:22 +0300 | [diff] [blame] | 6133 | if (rc < 0) { |
| 6134 | if (rc == -ENOENT) |
| 6135 | pr_info("pool %s does not exist\n", spec->pool_name); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6136 | goto err_out_client; |
Ilya Dryomov | 1fe4802 | 2015-03-05 10:47:22 +0300 | [diff] [blame] | 6137 | } |
Alex Elder | c0cd10db | 2013-04-26 09:43:47 -0500 | [diff] [blame] | 6138 | spec->pool_id = (u64)rc; |
Alex Elder | 859c31d | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 6139 | |
Ilya Dryomov | d147543 | 2015-06-22 13:24:48 +0300 | [diff] [blame] | 6140 | rbd_dev = rbd_dev_create(rbdc, spec, rbd_opts); |
Ilya Dryomov | b51c83c | 2015-10-15 15:38:57 +0200 | [diff] [blame] | 6141 | if (!rbd_dev) { |
| 6142 | rc = -ENOMEM; |
Alex Elder | bd4ba65 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 6143 | goto err_out_client; |
Ilya Dryomov | b51c83c | 2015-10-15 15:38:57 +0200 | [diff] [blame] | 6144 | } |
Alex Elder | c53d589 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 6145 | rbdc = NULL; /* rbd_dev now owns this */ |
| 6146 | spec = NULL; /* rbd_dev now owns this */ |
Ilya Dryomov | d147543 | 2015-06-22 13:24:48 +0300 | [diff] [blame] | 6147 | rbd_opts = NULL; /* rbd_dev now owns this */ |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6148 | |
Mike Christie | 0d6d1e9c | 2016-08-18 18:38:45 +0200 | [diff] [blame] | 6149 | rbd_dev->config_info = kstrdup(buf, GFP_KERNEL); |
| 6150 | if (!rbd_dev->config_info) { |
| 6151 | rc = -ENOMEM; |
| 6152 | goto err_out_rbd_dev; |
| 6153 | } |
| 6154 | |
Ilya Dryomov | 811c668 | 2016-04-15 16:22:16 +0200 | [diff] [blame] | 6155 | down_write(&rbd_dev->header_rwsem); |
Ilya Dryomov | 6d69bb53 | 2015-10-11 19:38:00 +0200 | [diff] [blame] | 6156 | rc = rbd_dev_image_probe(rbd_dev, 0); |
Mike Christie | 0d6d1e9c | 2016-08-18 18:38:45 +0200 | [diff] [blame] | 6157 | if (rc < 0) { |
| 6158 | up_write(&rbd_dev->header_rwsem); |
Alex Elder | c53d589 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 6159 | goto err_out_rbd_dev; |
Mike Christie | 0d6d1e9c | 2016-08-18 18:38:45 +0200 | [diff] [blame] | 6160 | } |
Alex Elder | 05fd6f6 | 2012-08-29 17:11:07 -0500 | [diff] [blame] | 6161 | |
Alex Elder | 7ce4eef | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 6162 | /* If we are mapping a snapshot it must be marked read-only */ |
Alex Elder | 7ce4eef | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 6163 | if (rbd_dev->spec->snap_id != CEPH_NOSNAP) |
Ilya Dryomov | 9568c93 | 2017-10-12 12:35:19 +0200 | [diff] [blame^] | 6164 | rbd_dev->opts->read_only = true; |
Alex Elder | 7ce4eef | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 6165 | |
Alex Elder | b536f69 | 2013-04-28 23:32:34 -0500 | [diff] [blame] | 6166 | rc = rbd_dev_device_setup(rbd_dev); |
Ilya Dryomov | fd22aef | 2017-04-13 12:17:37 +0200 | [diff] [blame] | 6167 | if (rc) |
Ilya Dryomov | 8b679ec | 2017-04-13 12:17:37 +0200 | [diff] [blame] | 6168 | goto err_out_image_probe; |
Alex Elder | b536f69 | 2013-04-28 23:32:34 -0500 | [diff] [blame] | 6169 | |
Ilya Dryomov | e010dd0 | 2017-04-13 12:17:39 +0200 | [diff] [blame] | 6170 | if (rbd_dev->opts->exclusive) { |
| 6171 | rc = rbd_add_acquire_lock(rbd_dev); |
| 6172 | if (rc) |
| 6173 | goto err_out_device_setup; |
Alex Elder | b536f69 | 2013-04-28 23:32:34 -0500 | [diff] [blame] | 6174 | } |
| 6175 | |
Ilya Dryomov | 5769ed0 | 2017-04-13 12:17:38 +0200 | [diff] [blame] | 6176 | /* Everything's ready. Announce the disk to the world. */ |
| 6177 | |
| 6178 | rc = device_add(&rbd_dev->dev); |
| 6179 | if (rc) |
Ilya Dryomov | e010dd0 | 2017-04-13 12:17:39 +0200 | [diff] [blame] | 6180 | goto err_out_image_lock; |
Ilya Dryomov | 5769ed0 | 2017-04-13 12:17:38 +0200 | [diff] [blame] | 6181 | |
| 6182 | add_disk(rbd_dev->disk); |
| 6183 | /* see rbd_init_disk() */ |
| 6184 | blk_put_queue(rbd_dev->disk->queue); |
| 6185 | |
| 6186 | spin_lock(&rbd_dev_list_lock); |
| 6187 | list_add_tail(&rbd_dev->node, &rbd_dev_list); |
| 6188 | spin_unlock(&rbd_dev_list_lock); |
| 6189 | |
| 6190 | pr_info("%s: capacity %llu features 0x%llx\n", rbd_dev->disk->disk_name, |
| 6191 | (unsigned long long)get_capacity(rbd_dev->disk) << SECTOR_SHIFT, |
| 6192 | rbd_dev->header.features); |
Ilya Dryomov | dd5ac32 | 2015-10-16 17:09:24 +0200 | [diff] [blame] | 6193 | rc = count; |
| 6194 | out: |
| 6195 | module_put(THIS_MODULE); |
| 6196 | return rc; |
Alex Elder | 3abef3b | 2013-05-13 20:35:37 -0500 | [diff] [blame] | 6197 | |
Ilya Dryomov | e010dd0 | 2017-04-13 12:17:39 +0200 | [diff] [blame] | 6198 | err_out_image_lock: |
| 6199 | rbd_dev_image_unlock(rbd_dev); |
Ilya Dryomov | 5769ed0 | 2017-04-13 12:17:38 +0200 | [diff] [blame] | 6200 | err_out_device_setup: |
| 6201 | rbd_dev_device_release(rbd_dev); |
Ilya Dryomov | 8b679ec | 2017-04-13 12:17:37 +0200 | [diff] [blame] | 6202 | err_out_image_probe: |
| 6203 | rbd_dev_image_release(rbd_dev); |
Alex Elder | c53d589 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 6204 | err_out_rbd_dev: |
| 6205 | rbd_dev_destroy(rbd_dev); |
Alex Elder | bd4ba65 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 6206 | err_out_client: |
Alex Elder | 9d3997f | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 6207 | rbd_put_client(rbdc); |
Alex Elder | 0ddebc0 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 6208 | err_out_args: |
Alex Elder | 859c31d | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 6209 | rbd_spec_put(spec); |
Ilya Dryomov | d147543 | 2015-06-22 13:24:48 +0300 | [diff] [blame] | 6210 | kfree(rbd_opts); |
Ilya Dryomov | dd5ac32 | 2015-10-16 17:09:24 +0200 | [diff] [blame] | 6211 | goto out; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6212 | } |
| 6213 | |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 6214 | static ssize_t rbd_add(struct bus_type *bus, |
| 6215 | const char *buf, |
| 6216 | size_t count) |
| 6217 | { |
| 6218 | if (single_major) |
| 6219 | return -EINVAL; |
| 6220 | |
| 6221 | return do_rbd_add(bus, buf, count); |
| 6222 | } |
| 6223 | |
| 6224 | static ssize_t rbd_add_single_major(struct bus_type *bus, |
| 6225 | const char *buf, |
| 6226 | size_t count) |
| 6227 | { |
| 6228 | return do_rbd_add(bus, buf, count); |
| 6229 | } |
| 6230 | |
Alex Elder | 05a46af | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 6231 | static void rbd_dev_remove_parent(struct rbd_device *rbd_dev) |
| 6232 | { |
Alex Elder | ad945fc | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 6233 | while (rbd_dev->parent) { |
Alex Elder | 05a46af | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 6234 | struct rbd_device *first = rbd_dev; |
| 6235 | struct rbd_device *second = first->parent; |
| 6236 | struct rbd_device *third; |
| 6237 | |
| 6238 | /* |
| 6239 | * Follow to the parent with no grandparent and |
| 6240 | * remove it. |
| 6241 | */ |
| 6242 | while (second && (third = second->parent)) { |
| 6243 | first = second; |
| 6244 | second = third; |
| 6245 | } |
Alex Elder | ad945fc | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 6246 | rbd_assert(second); |
Alex Elder | 8ad42cd | 2013-04-28 23:32:34 -0500 | [diff] [blame] | 6247 | rbd_dev_image_release(second); |
Ilya Dryomov | 8b679ec | 2017-04-13 12:17:37 +0200 | [diff] [blame] | 6248 | rbd_dev_destroy(second); |
Alex Elder | ad945fc | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 6249 | first->parent = NULL; |
| 6250 | first->parent_overlap = 0; |
| 6251 | |
| 6252 | rbd_assert(first->parent_spec); |
Alex Elder | 05a46af | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 6253 | rbd_spec_put(first->parent_spec); |
| 6254 | first->parent_spec = NULL; |
Alex Elder | 05a46af | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 6255 | } |
| 6256 | } |
| 6257 | |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 6258 | static ssize_t do_rbd_remove(struct bus_type *bus, |
| 6259 | const char *buf, |
| 6260 | size_t count) |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6261 | { |
| 6262 | struct rbd_device *rbd_dev = NULL; |
Alex Elder | 751cc0e | 2013-05-31 15:17:01 -0500 | [diff] [blame] | 6263 | struct list_head *tmp; |
| 6264 | int dev_id; |
Mike Christie | 0276dca | 2016-08-18 18:38:45 +0200 | [diff] [blame] | 6265 | char opt_buf[6]; |
Alex Elder | 82a442d | 2013-05-31 17:40:44 -0500 | [diff] [blame] | 6266 | bool already = false; |
Mike Christie | 0276dca | 2016-08-18 18:38:45 +0200 | [diff] [blame] | 6267 | bool force = false; |
Alex Elder | 0d8189e | 2013-04-27 09:59:30 -0500 | [diff] [blame] | 6268 | int ret; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6269 | |
Mike Christie | 0276dca | 2016-08-18 18:38:45 +0200 | [diff] [blame] | 6270 | dev_id = -1; |
| 6271 | opt_buf[0] = '\0'; |
| 6272 | sscanf(buf, "%d %5s", &dev_id, opt_buf); |
| 6273 | if (dev_id < 0) { |
| 6274 | pr_err("dev_id out of range\n"); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6275 | return -EINVAL; |
Mike Christie | 0276dca | 2016-08-18 18:38:45 +0200 | [diff] [blame] | 6276 | } |
| 6277 | if (opt_buf[0] != '\0') { |
| 6278 | if (!strcmp(opt_buf, "force")) { |
| 6279 | force = true; |
| 6280 | } else { |
| 6281 | pr_err("bad remove option at '%s'\n", opt_buf); |
| 6282 | return -EINVAL; |
| 6283 | } |
| 6284 | } |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6285 | |
Alex Elder | 751cc0e | 2013-05-31 15:17:01 -0500 | [diff] [blame] | 6286 | ret = -ENOENT; |
| 6287 | spin_lock(&rbd_dev_list_lock); |
| 6288 | list_for_each(tmp, &rbd_dev_list) { |
| 6289 | rbd_dev = list_entry(tmp, struct rbd_device, node); |
| 6290 | if (rbd_dev->dev_id == dev_id) { |
| 6291 | ret = 0; |
| 6292 | break; |
| 6293 | } |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6294 | } |
Alex Elder | 751cc0e | 2013-05-31 15:17:01 -0500 | [diff] [blame] | 6295 | if (!ret) { |
| 6296 | spin_lock_irq(&rbd_dev->lock); |
Mike Christie | 0276dca | 2016-08-18 18:38:45 +0200 | [diff] [blame] | 6297 | if (rbd_dev->open_count && !force) |
Alex Elder | 751cc0e | 2013-05-31 15:17:01 -0500 | [diff] [blame] | 6298 | ret = -EBUSY; |
| 6299 | else |
Alex Elder | 82a442d | 2013-05-31 17:40:44 -0500 | [diff] [blame] | 6300 | already = test_and_set_bit(RBD_DEV_FLAG_REMOVING, |
| 6301 | &rbd_dev->flags); |
Alex Elder | 751cc0e | 2013-05-31 15:17:01 -0500 | [diff] [blame] | 6302 | spin_unlock_irq(&rbd_dev->lock); |
| 6303 | } |
| 6304 | spin_unlock(&rbd_dev_list_lock); |
Alex Elder | 82a442d | 2013-05-31 17:40:44 -0500 | [diff] [blame] | 6305 | if (ret < 0 || already) |
Alex Elder | 1ba0f1e | 2013-05-31 15:17:01 -0500 | [diff] [blame] | 6306 | return ret; |
Alex Elder | 751cc0e | 2013-05-31 15:17:01 -0500 | [diff] [blame] | 6307 | |
Mike Christie | 0276dca | 2016-08-18 18:38:45 +0200 | [diff] [blame] | 6308 | if (force) { |
| 6309 | /* |
| 6310 | * Prevent new IO from being queued and wait for existing |
| 6311 | * IO to complete/fail. |
| 6312 | */ |
| 6313 | blk_mq_freeze_queue(rbd_dev->disk->queue); |
| 6314 | blk_set_queue_dying(rbd_dev->disk->queue); |
| 6315 | } |
| 6316 | |
Ilya Dryomov | 5769ed0 | 2017-04-13 12:17:38 +0200 | [diff] [blame] | 6317 | del_gendisk(rbd_dev->disk); |
| 6318 | spin_lock(&rbd_dev_list_lock); |
| 6319 | list_del_init(&rbd_dev->node); |
| 6320 | spin_unlock(&rbd_dev_list_lock); |
| 6321 | device_del(&rbd_dev->dev); |
Ilya Dryomov | fca2706 | 2013-12-16 18:02:40 +0200 | [diff] [blame] | 6322 | |
Ilya Dryomov | e010dd0 | 2017-04-13 12:17:39 +0200 | [diff] [blame] | 6323 | rbd_dev_image_unlock(rbd_dev); |
Ilya Dryomov | dd5ac32 | 2015-10-16 17:09:24 +0200 | [diff] [blame] | 6324 | rbd_dev_device_release(rbd_dev); |
Alex Elder | 8ad42cd | 2013-04-28 23:32:34 -0500 | [diff] [blame] | 6325 | rbd_dev_image_release(rbd_dev); |
Ilya Dryomov | 8b679ec | 2017-04-13 12:17:37 +0200 | [diff] [blame] | 6326 | rbd_dev_destroy(rbd_dev); |
Alex Elder | 1ba0f1e | 2013-05-31 15:17:01 -0500 | [diff] [blame] | 6327 | return count; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6328 | } |
| 6329 | |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 6330 | static ssize_t rbd_remove(struct bus_type *bus, |
| 6331 | const char *buf, |
| 6332 | size_t count) |
| 6333 | { |
| 6334 | if (single_major) |
| 6335 | return -EINVAL; |
| 6336 | |
| 6337 | return do_rbd_remove(bus, buf, count); |
| 6338 | } |
| 6339 | |
| 6340 | static ssize_t rbd_remove_single_major(struct bus_type *bus, |
| 6341 | const char *buf, |
| 6342 | size_t count) |
| 6343 | { |
| 6344 | return do_rbd_remove(bus, buf, count); |
| 6345 | } |
| 6346 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6347 | /* |
| 6348 | * create control files in sysfs |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 6349 | * /sys/bus/rbd/... |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6350 | */ |
| 6351 | static int rbd_sysfs_init(void) |
| 6352 | { |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 6353 | int ret; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6354 | |
Alex Elder | fed4c14 | 2012-02-07 12:03:36 -0600 | [diff] [blame] | 6355 | ret = device_register(&rbd_root_dev); |
Alex Elder | 2107978 | 2012-01-24 10:08:36 -0600 | [diff] [blame] | 6356 | if (ret < 0) |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 6357 | return ret; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6358 | |
Alex Elder | fed4c14 | 2012-02-07 12:03:36 -0600 | [diff] [blame] | 6359 | ret = bus_register(&rbd_bus_type); |
| 6360 | if (ret < 0) |
| 6361 | device_unregister(&rbd_root_dev); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6362 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6363 | return ret; |
| 6364 | } |
| 6365 | |
| 6366 | static void rbd_sysfs_cleanup(void) |
| 6367 | { |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 6368 | bus_unregister(&rbd_bus_type); |
Alex Elder | fed4c14 | 2012-02-07 12:03:36 -0600 | [diff] [blame] | 6369 | device_unregister(&rbd_root_dev); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6370 | } |
| 6371 | |
Alex Elder | 1c2a9df | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 6372 | static int rbd_slab_init(void) |
| 6373 | { |
| 6374 | rbd_assert(!rbd_img_request_cache); |
Geliang Tang | 03d9440 | 2016-03-13 15:17:32 +0800 | [diff] [blame] | 6375 | rbd_img_request_cache = KMEM_CACHE(rbd_img_request, 0); |
Alex Elder | 868311b | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 6376 | if (!rbd_img_request_cache) |
| 6377 | return -ENOMEM; |
| 6378 | |
| 6379 | rbd_assert(!rbd_obj_request_cache); |
Geliang Tang | 03d9440 | 2016-03-13 15:17:32 +0800 | [diff] [blame] | 6380 | rbd_obj_request_cache = KMEM_CACHE(rbd_obj_request, 0); |
Alex Elder | 78c2a44 | 2013-05-01 12:43:04 -0500 | [diff] [blame] | 6381 | if (!rbd_obj_request_cache) |
| 6382 | goto out_err; |
| 6383 | |
NeilBrown | f856dc3 | 2017-06-18 14:38:58 +1000 | [diff] [blame] | 6384 | rbd_assert(!rbd_bio_clone); |
| 6385 | rbd_bio_clone = bioset_create(BIO_POOL_SIZE, 0, 0); |
| 6386 | if (!rbd_bio_clone) |
| 6387 | goto out_err_clone; |
| 6388 | |
Ilya Dryomov | 6c696d8 | 2017-01-25 18:16:23 +0100 | [diff] [blame] | 6389 | return 0; |
Alex Elder | 1c2a9df | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 6390 | |
NeilBrown | f856dc3 | 2017-06-18 14:38:58 +1000 | [diff] [blame] | 6391 | out_err_clone: |
| 6392 | kmem_cache_destroy(rbd_obj_request_cache); |
| 6393 | rbd_obj_request_cache = NULL; |
Ilya Dryomov | 6c696d8 | 2017-01-25 18:16:23 +0100 | [diff] [blame] | 6394 | out_err: |
Alex Elder | 868311b | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 6395 | kmem_cache_destroy(rbd_img_request_cache); |
| 6396 | rbd_img_request_cache = NULL; |
Alex Elder | 1c2a9df | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 6397 | return -ENOMEM; |
| 6398 | } |
| 6399 | |
| 6400 | static void rbd_slab_exit(void) |
| 6401 | { |
Alex Elder | 868311b | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 6402 | rbd_assert(rbd_obj_request_cache); |
| 6403 | kmem_cache_destroy(rbd_obj_request_cache); |
| 6404 | rbd_obj_request_cache = NULL; |
| 6405 | |
Alex Elder | 1c2a9df | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 6406 | rbd_assert(rbd_img_request_cache); |
| 6407 | kmem_cache_destroy(rbd_img_request_cache); |
| 6408 | rbd_img_request_cache = NULL; |
NeilBrown | f856dc3 | 2017-06-18 14:38:58 +1000 | [diff] [blame] | 6409 | |
| 6410 | rbd_assert(rbd_bio_clone); |
| 6411 | bioset_free(rbd_bio_clone); |
| 6412 | rbd_bio_clone = NULL; |
Alex Elder | 1c2a9df | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 6413 | } |
| 6414 | |
Alex Elder | cc344fa | 2013-02-19 12:25:56 -0600 | [diff] [blame] | 6415 | static int __init rbd_init(void) |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6416 | { |
| 6417 | int rc; |
| 6418 | |
Alex Elder | 1e32d34 | 2013-01-30 11:13:33 -0600 | [diff] [blame] | 6419 | if (!libceph_compatible(NULL)) { |
| 6420 | rbd_warn(NULL, "libceph incompatibility (quitting)"); |
Alex Elder | 1e32d34 | 2013-01-30 11:13:33 -0600 | [diff] [blame] | 6421 | return -EINVAL; |
| 6422 | } |
Ilya Dryomov | e1b4d96 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 6423 | |
Alex Elder | 1c2a9df | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 6424 | rc = rbd_slab_init(); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6425 | if (rc) |
| 6426 | return rc; |
Ilya Dryomov | e1b4d96 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 6427 | |
Ilya Dryomov | f5ee37b | 2014-10-09 17:06:01 +0400 | [diff] [blame] | 6428 | /* |
| 6429 | * The number of active work items is limited by the number of |
Ilya Dryomov | f77303b | 2015-04-22 18:28:13 +0300 | [diff] [blame] | 6430 | * rbd devices * queue depth, so leave @max_active at default. |
Ilya Dryomov | f5ee37b | 2014-10-09 17:06:01 +0400 | [diff] [blame] | 6431 | */ |
| 6432 | rbd_wq = alloc_workqueue(RBD_DRV_NAME, WQ_MEM_RECLAIM, 0); |
| 6433 | if (!rbd_wq) { |
| 6434 | rc = -ENOMEM; |
| 6435 | goto err_out_slab; |
| 6436 | } |
| 6437 | |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 6438 | if (single_major) { |
| 6439 | rbd_major = register_blkdev(0, RBD_DRV_NAME); |
| 6440 | if (rbd_major < 0) { |
| 6441 | rc = rbd_major; |
Ilya Dryomov | f5ee37b | 2014-10-09 17:06:01 +0400 | [diff] [blame] | 6442 | goto err_out_wq; |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 6443 | } |
| 6444 | } |
| 6445 | |
Alex Elder | 1c2a9df | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 6446 | rc = rbd_sysfs_init(); |
| 6447 | if (rc) |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 6448 | goto err_out_blkdev; |
Alex Elder | 1c2a9df | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 6449 | |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 6450 | if (single_major) |
| 6451 | pr_info("loaded (major %d)\n", rbd_major); |
| 6452 | else |
| 6453 | pr_info("loaded\n"); |
| 6454 | |
Ilya Dryomov | e1b4d96 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 6455 | return 0; |
| 6456 | |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 6457 | err_out_blkdev: |
| 6458 | if (single_major) |
| 6459 | unregister_blkdev(rbd_major, RBD_DRV_NAME); |
Ilya Dryomov | f5ee37b | 2014-10-09 17:06:01 +0400 | [diff] [blame] | 6460 | err_out_wq: |
| 6461 | destroy_workqueue(rbd_wq); |
Ilya Dryomov | e1b4d96 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 6462 | err_out_slab: |
| 6463 | rbd_slab_exit(); |
Alex Elder | 1c2a9df | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 6464 | return rc; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6465 | } |
| 6466 | |
Alex Elder | cc344fa | 2013-02-19 12:25:56 -0600 | [diff] [blame] | 6467 | static void __exit rbd_exit(void) |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6468 | { |
Ilya Dryomov | ffe312c | 2014-05-20 15:46:04 +0400 | [diff] [blame] | 6469 | ida_destroy(&rbd_dev_id_ida); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6470 | rbd_sysfs_cleanup(); |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 6471 | if (single_major) |
| 6472 | unregister_blkdev(rbd_major, RBD_DRV_NAME); |
Ilya Dryomov | f5ee37b | 2014-10-09 17:06:01 +0400 | [diff] [blame] | 6473 | destroy_workqueue(rbd_wq); |
Alex Elder | 1c2a9df | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 6474 | rbd_slab_exit(); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6475 | } |
| 6476 | |
| 6477 | module_init(rbd_init); |
| 6478 | module_exit(rbd_exit); |
| 6479 | |
Alex Elder | d552c61 | 2013-05-31 20:13:09 -0500 | [diff] [blame] | 6480 | MODULE_AUTHOR("Alex Elder <elder@inktank.com>"); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6481 | MODULE_AUTHOR("Sage Weil <sage@newdream.net>"); |
| 6482 | MODULE_AUTHOR("Yehuda Sadeh <yehuda@hq.newdream.net>"); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6483 | /* following authorship retained from original osdblk.c */ |
| 6484 | MODULE_AUTHOR("Jeff Garzik <jeff@garzik.org>"); |
| 6485 | |
Ilya Dryomov | 90da258 | 2013-12-13 15:28:56 +0200 | [diff] [blame] | 6486 | MODULE_DESCRIPTION("RADOS Block Device (RBD) driver"); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6487 | MODULE_LICENSE("GPL"); |